- Python Data Structures and Algorithms
- Benjamin Baka
- 217字
- 2021-07-09 19:45:00
Data encapsulation and properties
Unless otherwise specified, all attributes and methods are accessible without restriction. This also means that everything defined in a base class is accessible from a derived class. This may cause problems when we are building object-oriented applications where we may want to hide the internal implementation of an object. This can lead to namespace conflicts between objects defined in derived classes with the base class. To prevent this, the methods we define private attributes with have a double underscore, such as __privateMethod(). These method names are automatically changed to _Classname__privateMethod() to prevent name conflicts with methods defined in base classes. Be aware that this does not strictly hide private attributes, rather it just provides a mechanism for preventing name conflicts.
It is recommended to use private attributes when using a class property to define mutable attributes. A property is a kind of attribute that rather than returning a stored value, computes its value when called. For example, we could redefine the exp() property with the following:
class Bexp(Aexp):
__base=3
def __exp(self):
return(x**cls.base)
In this chapter, we have looked at some of the fundamentals of the Python programming language, from basic operations to functions, classes, and objects in Python. In the next chapter, we will examine, in detail, the built-in data structures of Python.
- Vue.js快速入門與深入實(shí)戰(zhàn)
- Network Automation Cookbook
- Python網(wǎng)絡(luò)爬蟲從入門到實(shí)踐(第2版)
- The Computer Vision Workshop
- 老“碼”識(shí)途
- PostgreSQL 11從入門到精通(視頻教學(xué)版)
- 程序設(shè)計(jì)基礎(chǔ)教程:C語(yǔ)言
- Practical Game Design with Unity and Playmaker
- C# Multithreaded and Parallel Programming
- .NET 4.5 Parallel Extensions Cookbook
- Advanced UFT 12 for Test Engineers Cookbook
- 用Python動(dòng)手學(xué)統(tǒng)計(jì)學(xué)
- jQuery Essentials
- 流暢的Python
- PHP高性能開發(fā):基礎(chǔ)、框架與項(xiàng)目實(shí)戰(zhàn)