码迷,mamicode.com
首页 > 编程语言 > 详细

python的私有变量解析

时间:2014-05-03 17:37:55      阅读:333      评论:0      收藏:0      [点我收藏+]

标签:python   python培训   python正则表达式   python类   python实例对象变量   

在内的内部定义并使用,外部无法访问,以双下划线作为前作,定义后被python转为
_classname__变量名了

--------------------------------------------------------------------------------------
In [1]: class aa:
   ...: __x = 12 #私有变量_ _x
   ...: def px(self):
   ...: print ‘private __x‘, aa.__x #内部访问
   ...:

In [2]: a = aa()

In [3]: a.px()
private __x 12

In [4]: dir(a)
Out[4]: [‘__doc__‘, ‘__module__‘, ‘_aa__x‘, ‘px‘] # map成_classname__变量名了

In [5]: a.__x = 13#实例对象a的实例变量

In [6]: dir(a)
Out[6]: [‘__doc__‘, ‘__module__‘, ‘__x‘, ‘_aa__x‘, ‘px‘]

In [7]: print a.__x
13

In [8]:In [8]: print aa.__x
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-34f87438b5b5> in <module>()
----> 1 print aa.__x

AttributeError: class aa has no attribute ‘__x‘

In [9]:
-----------------------------------------------------------

python的私有变量解析,布布扣,bubuko.com

python的私有变量解析

标签:python   python培训   python正则表达式   python类   python实例对象变量   

原文地址:http://blog.csdn.net/jeapeducom/article/details/24884081

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!