【发布时间】:2013-06-07 09:33:32
【问题描述】:
我认为 Python 中的 class 语句会创建一个作用域。但是尝试这个简单的代码我得到一个错误:
class A():
foo = 1
def __init__(self):
print foo
a = A()
NameError: global name 'foo' is not defined
为什么不能从初始化程序访问foo?访问它的正确方法是什么?
【问题讨论】:
标签: python scope python-2.x