gpt4 book ai didi

python - 调用 super 的 init 时 Python 中的最大递归深度错误。

转载 作者:太空狗 更新时间:2023-10-29 18:05:55 25 4
gpt4 key购买 nike

<分区>

我有一个类层次结构 A <- B <- C,在 B 中,我需要在构造函数中进行一些处理,所以我从这篇文章中得出了这段代码: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

可能出了什么问题?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com