Open In App

Python Runtimeerror: Super() No Arguments

Last Updated : 20 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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

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

Change in super() Behavior in Python

Python3 introduced a more explicit approach to the super() function, requiring the developer to pass the current class and instance explicitly. Failing to do so results in the RuntimeError.

Python3




class A:
    @staticmethod
    def m() -> int:
        return 1
 
class B(A):
    @staticmethod
    def m() -> int:
        return super().m() 
 
B().m()


Output:

Hangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 11, in <module>
B().m()
File "Solution.py", line 9, in m
return super().m() # passes, but should not
RuntimeError: super(): no arguments

Multiple Inheritance Ambiguity

If your code involves multiple inheritance, not providing arguments to super() can lead to ambiguity in determining the method resolution order (MRO), causing the RuntimeError.

Python3




class Works(type):
    def __new__(cls, *args, **kwargs):
        print([cls,args]) # outputs [<class '__main__.Works'>, ()]
        return super().__new__(cls, args)
 
class DoesNotWork(type):
    def __new__(*args, **kwargs):
        print([args[0],args[:0]]) # outputs [<class '__main__.doesNotWork'>, ()]
        return super().__new__(args[0], args[:0])
 
DoesNotWork()


Output:

Hangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 11, in <module>
DoesNotWork() # gets "RuntimeError: super(): no arguments"
File "Solution.py", line 9, in __new__
return super().__new__(args[0], args[:0])
RuntimeError: super(): no arguments

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


Conclusion

The “RuntimeError: super(): no arguments” error in Python 3 can be a stumbling block for developers, but armed with an understanding of its origins and the right solutions, it can be overcome. By embracing the explicit nature of super() in Python 3, resolving multiple inheritance ambiguity, and ensuring a well-structured class hierarchy, developers can navigate through this error and produce robust and efficient Python code.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads