我试图了解分配给 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__
?