Solution for RuntimeError: super(): No Arguments

Below are some of the solutions for RuntimeError: super(): No Arguments error in Python:

Explicitly Pass Class and Instance to super()

To address the change in super() behavior in Python 3, explicitly pass the current class and instance as arguments. For example:

Python3




class ParentClass:
    def __init__(self):
        print("ParentClass constructor")
 
class IntermediateClass(ParentClass):
    def __init__(self):
        super(IntermediateClass, self).__init__() 
        print("IntermediateClass constructor")
 
class ChildClass(IntermediateClass):
    def __init__(self):
        super(ChildClass, self).__init__()
        print("ChildClass constructor")
 
child_instance = ChildClass()


Output

ParentClass constructor
IntermediateClass constructor
ChildClass constructor


Resolve Multiple Inheritance Ambiguity

If your code involves multiple inheritance, carefully review the class hierarchy and ensure that the super() calls are properly aligned with the desired MRO. Adjust the order of the base classes if necessary.

Python3




class ParentA:
    def show(self):
        print("Method in ParentA")
 
class ParentB:
    def show(self):
        print("Method in ParentB")
 
class Child(ParentA, ParentB):
    def show(self):
        super(Child, self).show() 
        print("Method in Child")
 
 
child_instance = Child()
 
 
child_instance.show()


Output

Method in ParentA
Method in Child


Check and Correct Class Structure

Review your class structure and verify that the super() calls are placed correctly within the methods. Ensure that the inheritance chain is well-defined, and classes are designed according to the intended hierarchy.

Python3




class BaseClass:
    def __init__(self):
        print("BaseClass constructor")
 
class IntermediateClass(BaseClass):
    def __init__(self):
        super(IntermediateClass, self).__init__()
        print("IntermediateClass constructor")
 
class ChildClass(IntermediateClass):
    def __init__(self):
        super(ChildClass, self).__init__()
        print("ChildClass constructor")
 
 
child_instance = ChildClass()


Output

BaseClass constructor
IntermediateClass constructor
ChildClass constructor


Python Runtimeerror: Super() No Arguments

Python, a versatile programming language, provides developers with a powerful toolset for creating complex applications. However, like any programming language, it comes with its share of challenges. One such issue that developers might encounter is the “RuntimeError: super(): no arguments.” This error can be puzzling for those new to Python 3, but fear not; in this article, we’ll explore the nature of this error, understand why it occurs, and delve into different approaches to resolve it.

What is RuntimeError: super(): No Arguments Error?

The “RuntimeError: super(): no arguments” error is a common stumbling block for developers, especially when transitioning from Python 2 to Python 3. This error occurs when using the super() function without providing any arguments, causing confusion and disrupting the inheritance chain. Understanding the reasons behind this error is crucial for finding effective solutions.

Similar Reads

Why does RuntimeError: super(): No Arguments occur?

Below are some of the examples by which RuntimeError: super(): No Arguments in Python:...

Solution for RuntimeError: super(): No Arguments

...

Conclusion

...

Contact Us