我正在经历一种模糊的作用域效应(对我来说),并试图找出解决它的规则:
#!/usr/bin/env python3
stuff = "the things"
__MORE_STUFF = "even more"
class Thing:
def __init__(self):
global __MORE_STUFF # doesn't help
print(stuff) # is OK
print(__MORE_STUFF) # fail!
Thing()
在.中的结果
$ python3 dunder.py
the things
Traceback (most recent call last):
File "dunder.py", line 12, in <module>
Thing()
File "dunder.py", line 10, in __init__
print(__MORE_STUFF) # fail!
NameError: name '_Thing__MORE_STUFF' is not defined
被认为是模块全局变量的变量被视为类级属性,如果未定义,则被标记为未定义.
我一直在试图查阅文档,但我似乎无法弄清楚这条规则是什么.
有谁能给我指给我合适的文件吗?