我正在尝试理解Python类对象被赋值的变量与该类对象的__name__
属性之间的关系。例如:
In [1]: class Foo(object):
...: pass
...:
In [2]: Foo.__name__ = 'Bar'
In [3]: Foo.__name__
Out[3]: 'Bar'
In [4]: Foo
Out[4]: __main__.Bar
In [5]: Bar
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-5-962d3beb4fd6> in <module>()
----> 1 Bar
NameError: name 'Bar' is not defined
看起来我已经改变了类的__name__
属性,但我不能用那个名字来引用它。我知道这有点笼统,但有人能解释一下Foo
和Foo.__name__
之间的关系吗?