mhs*_*vat 5 nested-class python-2.7
以下代码成功打印OK:
class B(object):
def __init__(self):
super(B, self).__init__()
print 'OK'
class A(object):
def __init__(self):
self.B()
B = B
A()
Run Code Online (Sandbox Code Playgroud)
但是下面应该和上面一样工作 NameError: global name 'B' is not defined
class A(object):
def __init__(self):
self.B()
class B(object):
def __init__(self):
super(B, self).__init__()
print 'OK'
A()
Run Code Online (Sandbox Code Playgroud)
为什么?
BA在类使用范围内可用A.B:
class A(object):
def __init__(self):
self.B()
class B(object):
def __init__(self):
super(A.B, self).__init__()
print 'OK'
A()
Run Code Online (Sandbox Code Playgroud)