python解释器将__init__函数里的__z变量转成 _classname__z了,明确规则后外部依旧能够通过实力对象来訪问。
In [1]: class aa: ...: def __init__(self): ...: self.x = 10 ...: self.y = 11 ...: self.__z = 12 ...: In [2]: a = aa()In [3]: print a.x10In [4]: print a.y11In [5]: print a.__z---------------------------------------------------------------------------AttributeError Traceback (most recent call last)in ()----> 1 print a.__zAttributeError: aa instance has no attribute '__z'In [6]: dir(a)Out[6]: ['__doc__', '__init__', '__module__', '_aa__z', 'x', 'y']In [7]: print _aa.__z---------------------------------------------------------------------------NameError Traceback (most recent call last) in ()----> 1 print _aa.__zNameError: name '_aa' is not definedIn [8]: print _aa__z---------------------------------------------------------------------------NameError Traceback (most recent call last) in ()----> 1 print _aa__zNameError: name '_aa__z' is not definedIn [9]: print a._aa__z12In [10]: a.__z = 14In [11]: dir(a)Out[11]: ['__doc__', '__init__', '__module__', '__z', '_aa__z', 'x', 'y']In [12]: print a._aa__z12