I am a bit puzzled why when initializing an instance of a class in Python, I cannot use class attributes. Here is the example:
class TestClass: ... shared_list = ['a', 'b', 'c'] ... def __init__(self): ... self.length = len(shared_list) Now
>>> TestClass.shared_list ['a', 'b', 'c'] So the list exists before any instance of the class appears, but
>>> 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 Am I missing something simple?
UPD: Thanks to everyone for help and a prompt response. I've cleared up my confusion: class doesn't define a scope in Python.
self.shared_listorTestClass.shared_listif you want the scopedshared_list