我有点困惑为什么在Python中初始化类的实例时,我不能使用类属性。这是示例:
class TestClass:
... shared_list = ['a', 'b', 'c']
... def __init__(self):
...     self.length = len(shared_list)
现在
>>> TestClass.shared_list
['a', 'b', 'c']
因此该列表在类的任何实例出现之前就存在,但是
>>> tc = TestClass()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in __init__
NameError: global name 'shared_list' is not defined
我错过了一些简单的事情吗?
UPD:感谢大家的帮助和及时回复。我已经消除了我的困惑:类在 Python 中没有定义作用域。