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

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