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

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.

主站蜘蛛池模板: 张家川| 广灵县| 霞浦县| 综艺| 唐山市| 岚皋县| 玉环县| 正定县| 兴文县| 建德市| 曲麻莱县| 乌什县| 临邑县| 新平| 潜山县| 宝坻区| 蒲江县| 濉溪县| 田东县| 外汇| 长汀县| 武义县| 广水市| 黄骅市| 五指山市| 建瓯市| 仙桃市| 揭东县| 德江县| 都安| 巴林左旗| 肥城市| 广州市| 礼泉县| 湄潭县| 云浮市| 青河县| 斗六市| 鄯善县| 玉树县| 青神县|