You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3.11中super()方法报错‘RuntimeError: super(): no arguments’求助

解决RuntimeError: super(): no arguments错误

错误原因

你在Child类的类顶层作用域直接调用了super().method_p1(),super()必须在类的方法(实例方法、类方法、静态方法)内部使用——它需要依赖方法提供的实例/类上下文来确定要调用的父类。

修正后的代码

class Parent:
    
    def __init__(self):
        print("This is a parent class constructor.")
    
    def method_p1(self):
        print("This is an parent instance method.")
    
    @classmethod
    def method_p2_class(cls):
        print("This is a parent class method.")
    
    @staticmethod
    def method_p2_static():
        print("This is a parent static method.")

class Child(Parent):
    
    def __init__(self):
        super().__init__()
        # 将super().method_p1()移到__init__方法内部,获取实例上下文
        super().method_p1()
        print("This is a child class constructor.")
    
    def method_c1(self):
        print("This is a child class instance method.")

child_obj = Child()
child_obj.method_p1()

其他可选实现

如果不想在构造方法里调用父类方法,也可以单独定义一个方法来封装调用逻辑:

class Child(Parent):
    
    def __init__(self):
        super().__init__()
        print("This is a child class constructor.")
    
    def method_c1(self):
        print("This is a child class instance method.")
    
    def invoke_parent_method(self):
        super().method_p1()

child_obj = Child()
child_obj.invoke_parent_method()
child_obj.method_p1()

修正后运行输出

This is a parent class constructor.
This is an parent instance method.
This is a child class constructor.
This is an parent instance method.

内容的提问来源于stack exchange,提问作者KrishnaSaran draksharapu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 05:00:08