Why does this code fail if there is no cls. before __TEXT
__TEXT = "abcde"
print(__TEXT)
class aaa():
@classmethod
def bbb(cls):
print(__TEXT)
aaa.bbb()
The output is:
abcde
Traceback (most recent call last):
File "<string>", line 9, in <module>
File "<string>", line 7, in bbb
NameError: name '_aaa__TEXT' is not defined
If you make __TEXT a class variable and try to reference it without the class prefix as follows:
class aaa():
__TEXT = "abcde"
@classmethod
def bbb(cls):
print(cls.__TEXT)
print(__TEXT)
x = aaa()
x.bbb()
You get the same error but it doesn't make sense:
abcde
Traceback (most recent call last):
File "<string>", line 10, in <module>
File "<string>", line 7, in bbb
NameError: name '_aaa__TEXT' is not defined