gpt4 book ai didi

python - 在嵌套类中使用 super()

转载 作者:IT老高 更新时间:2023-10-28 20:28:29 26 4
gpt4 key购买 nike

想象一下:

class A(object):
class B(object):
def __init__(self):
super(B, self).__init__()

这会产生错误:

NameError: global name B is not defined.

I've tried A.B, but then it says that A is not defined.

Update:

I've found the problem.

I've had a class like this:

class A(object):
class B(object):
def __init__(self):
super(B, self).__init__()

someattribute = B()

在该范围内,A 尚未定义。

最佳答案

我不确定为什么 A.B 不能正常工作,因为它应该..这是一些有效的 shell 输出:

>>> class A(object):
... class B(object):
... def __init__(self):
... super(A.B, self).__init__()
... def getB(self):
... return A.B()
...
>>> A().getB()
<__main__.B object at 0x100496410>

关于python - 在嵌套类中使用 super(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1825384/

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