假设我有这样的课程:
class a(object):
pass
class b(a):
pass
class c(b):
pass
class d(c):
pass
class e(b):
pass
Run Code Online (Sandbox Code Playgroud)
我想要一个可以做类似事情的函数:
>>>get_ inheritance_tree(a)
>>>...b
>>>......c
>>>.........d
>>>......e
Run Code Online (Sandbox Code Playgroud) 我正在尝试运行此代码:
class A:
def __enter__(self):
print "enter"
def __exit__(self, *args):
print "exit"
def __init__(self, i):
self.i = i
with A(10) as a:
print a.i
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
enter
exit
Traceback (most recent call last):
File ".last_tmp.py", line 9, in <module>
print a.i
AttributeError: 'NoneType' object has no attribute 'i'
Run Code Online (Sandbox Code Playgroud)
我的语法有什么问题?