官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 望奎县| 江西省| 兰考县| 扎赉特旗| 衡阳县| 安阳市| 临澧县| 阳山县| 珠海市| 监利县| 莱西市| 遂昌县| 巴林左旗| 历史| 涟源市| 怀远县| 比如县| 花莲市| 谷城县| 盐亭县| 方城县| 长子县| 洱源县| 上虞市| 井研县| 巩留县| 吕梁市| 望城县| 印江| 云霄县| 仁化县| 黑水县| 商洛市| 五大连池市| 华亭县| 宽城| 南开区| 石棉县| 旺苍县| 汨罗市| 广昌县|