为什么 Python 重新映射我的属性名称?
Why does Python remap my attribute name?
如果我运行这个在Python
class Boo(object):
def __init__(self, x):
self._x = x
self.__bingo = 3
self._info = {'x': x, 'y':42}
def __dir__(self):
return ['x']
@property
def x(self):
return self._x
@property
def z(self):
return self._x+1
def __getattr__(self, key):
return self._info[key]
b = Boo(44)
b.__dict__.keys()
然后打印
['_info', '_Boo__bingo', '_x']
为什么 __bingo
重命名了?双下划线属性是否保留?
它与 mangling 有关,python 的解释器所做的事情。这是一篇关于它的文章。
如果我运行这个在Python
class Boo(object):
def __init__(self, x):
self._x = x
self.__bingo = 3
self._info = {'x': x, 'y':42}
def __dir__(self):
return ['x']
@property
def x(self):
return self._x
@property
def z(self):
return self._x+1
def __getattr__(self, key):
return self._info[key]
b = Boo(44)
b.__dict__.keys()
然后打印
['_info', '_Boo__bingo', '_x']
为什么 __bingo
重命名了?双下划线属性是否保留?
它与 mangling 有关,python 的解释器所做的事情。这是一篇关于它的文章。