class Example:
my_list = [1, 2, 3]
def __init__(self):
self.my_list = my_list
example = Example()
print(example.my_list)
running the above code returns this NameError: name 'my_list' is not defined
.
class Example:
my_list = [1, 2, 3]
def __init__(self):
global my_list
self.my_list = my_list
example = Example()
Using global
doesn't seem to work either. What could be going on? why can't you instantiate
an attribute using global
?