我对这种行为感到有些困惑(使用Python 3.2):
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}
我已经了解了双下划线如何导致属性名被“破坏”,但我本来希望在上述两种情况下都有相同的名称破坏。
这里发生了什么事情,有什么想法吗?