我有点受这种行为混淆(使用Python 3.2)重整:Python的双下划线
class Bar:
pass
bar = Bar()
bar.__cache = None
print(vars(bar)) # {'__cache': None}
class Foo:
def __init__(self):
self.__cache = None
foo = Foo()
print(vars(foo)) # {'_Foo__cache': None}
我已经阅读了关于双下划线怎么引起属性名称是“错位”了一点,但我会期待同一个名字 - 在上述两种情况下都会发生。
What is the meaning of a single- and a double-underscore before an object name?
任何想法是怎么回事?
损坏的目的正是为了防止你的第二种情况正常工作。目的是隐藏外部代码的属性。 – millimoose