As we know, python provides a flexible mechanism to access the data of a class. You can define various member data according to your actual requirement, class attribue, public data, and private data can all be used to satisfy the requirement.
To access the private data in the class, it seems that we have no other way unless a public function is defined to get the private data. In fact, Ptyhon also provides a sneak trick to thwart the acess to the private data by referring to it using its mangled name: _ClassName__attrName.
Following example shows the usage:
class Data:
__private = 10
if __name__ == "__main__":
x = Data()
print x._Data__private
|
阅读(750) | 评论(0) | 转发(0) |