【发布时间】:2015-12-26 04:14:33
【问题描述】:
我有一个类层次结构 A Understanding Python super() with __init__() methods
#!/usr/bin/python
class A(object):
def __init__(self, v, v2):
self.v = v
self.v2 = v2
class B(A):
def __init__(self, v, v2):
# Do some processing
super(self.__class__, self).__init__(v, v2)
class C(B):
def hello():
print v, v2
b = B(3, 5)
print b.v
print b.v2
c = C(1,2)
print c
但是,我有一个超出最大递归的运行时错误
File "evenmore.py", line 12, in __init__
super(self.__class__, self).__init__(v, v2)
RuntimeError: maximum recursion depth exceeded while calling a Python object
可能出了什么问题?
【问题讨论】:
-
或者,如果您能够使用 Python 3,只需
super()(Python 3 会自动填充参数)。
标签: python recursion hierarchy superclass