Chinaunix首页 | 论坛 | 博客
  • 博客访问: 218186
  • 博文数量: 20
  • 博客积分: 2016
  • 博客等级: 大尉
  • 技术积分: 660
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-16 00:15
文章分类

全部博文(20)

文章存档

2010年(1)

2008年(19)

我的朋友

分类: LINUX

2008-05-26 23:16:53

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) |
给主人留下些什么吧!~~