gpt4 book ai didi

python 类在 __init__ 中使用 super 调用自身

转载 作者:行者123 更新时间:2023-12-04 03:14:37 28 4
gpt4 key购买 nike

我有一个这样的代码:

class sampleClass(object):
def __init__(self):
super(sampleClass, self).__init__()

我的问题是为什么它在 __init__ 下调用了自己的 super ?

我知道 super 用于调用父类的 __init__ ,(如果我错了,告诉我),但它有什么作用?

最佳答案

它不会调用 __init__它本身就是 super 的正确语法第一个参数必须是类本身( super 将调用其父类的类名 __init__ )。
在这里阅读更多 https://docs.python.org/3.6/library/functions.html#super

super([type[, object-or-type]])

Return a proxy object that delegatesmethod calls to a parent or sibling class of type. [the first argument]

关于python 类在 __init__ 中使用 super 调用自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42185212/

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