Solving Python TypeError: class() missing 1 required positional argument: ‘…’

Python TypeError: class() missing 1 required positional argument: ‘…’ is an error which occurs when you do not instantiate a class that you intend to use in python.

In today’s article I am going to present a set of possible solutions. In order to deal with a confusing error and explain why it takes place.

Exploring the Python TypeError: class() missing 1 required positional argument: ‘…’

This is an error which occurs when you do not instantiate a class that you intend to use in python.

Beware of mixing between different errors. Please make sure you are dealing with the same error.

                                                                       #
TypeError: attack() missing 1 required positional argument: '...'
                                                                       #

Bellow I will present a great solution that has worked for and fixed my problem.

The Method that fixed my issue : Correctly instantiate the python class

The error message tells us that we did not instantiate a class that we are using in python.

To illustrate this error we need an example, here is a simple code

                                                                       #
class Devices :
    def __init__(self):
        self. ...
       ...
    def myfunc(self):
       ....
        print ("...")
       ....

devices=Devices
devices.myfunc()
                                                                       #

The code above will print this error message

                                                                       #
TypeError: myfunc() missing 1 ... argument: 'self'
                                                                       #

Before we start talking about the solution, please make sure to use __init__ in your code , just like in my code above.

                                                                       #
    def __init__(self):
                                                                       #

The issue is that we did not instantiate myfunc(), the issue here is indirectly related to devices.myfunc()

The error is caused by this line.

                                                                       #
devices=Devices
                                                                       #

The line creates a reference to the class instead of instantiating it. devices=Devices should be replaced by

                                                                       #
devices=Devices()
                                                                       #

I hope the method have been helpful, I hope you solved the error already. Thank you for reading this to the end.

Summing-up : 

This is the end of this article guys, I hope one of these solutions worked for you and solved your error : Python TypeError class() missing 1 required positional argument: ‘…’.

I wish you good luck with your Python Journey. For donations you can use the red Kofi button above.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/