How to Fix Python TypeError: Missing 1 Required Positional Argument

Manav Narula Feb 02, 2024
  1. Not Instantiating an Object in Python
  2. Incorrectly Instantiating a Class Object in Python
  3. Fix the TypeError: missing 1 required positional argument: 'self' Error in Python
  4. Conclusion
How to Fix Python TypeError: Missing 1 Required Positional Argument

Classes are one of the fundamental features of Object-Oriented Programming languages. Every object belongs to some class in Python.

We can create our class as a blueprint to create objects of the same type. We use the class keyword to define a class in Python.

A very important feature in Python is using the self attribute while defining classes. The self attribute represents the object’s data and binds the arguments to the object.

This tutorial will discuss the TypeError: missing 1 required positional argument: 'self' error in Python and how we can solve it.

Let us discuss the situations where this error is raised.

Not Instantiating an Object in Python

Positional arguments refer to the data we provide a function with. For creating class objects, we use a constructor.

If the class requires any data, we must pass it as arguments in the constructor function.

This error is raised when we forget to instantiate the class object or wrongly instantiate the class instance.

See the code below.

class Delft:
    def __init__(self, name):
        self.name = name

    def fun(self):
        return self.name


m = Delft.fun()
print(m)

Output:

TypeError: fun() missing 1 required positional argument: 'self'

In the above example, we get the error because we forget to instantiate the instance of the class and try to access the function of the class. As a result, the self attribute is not defined and cannot bind the attributes to the object.

Note that this is a TypeError, which is raised in Python while performing some unsupported operation on a given data type. It is not supported to directly access the class method without creating an instance.

Incorrectly Instantiating a Class Object in Python

Another scenario where this error can occur is when we incorrectly instantiate the class object. This can be a small typing error like ignoring the parentheses or not providing the arguments.

For example:

class Delft:
    def __init__(self, name):
        self.name = name

    def fun(self):
        return self.name


m = Delft
a = m.fun()
print(a)

Output:

TypeError: fun() missing 1 required positional argument: 'self'

Let us now discuss how to fix this error in the section below.

Fix the TypeError: missing 1 required positional argument: 'self' Error in Python

To fix this error, we can create the class instance and use it to access the class method.

See the code below.

class Delft:
    def __init__(self, name):
        self.name = name

    def fun(self):
        return self.name


m = Delft("Stack")
a = m.fun()
print(a)

Output:

Stack

In the example, we first create an instance of the class. This is then used to access the class method to return some value and print it; the error is fixed.

Another fix involves the use of static methods. Static methods are functions bound to a particular class and not to an object.

We can use the @staticmethod decorator to create such methods.

For example:

class Delft:
    def __init__(self, name):
        self.name = name

    @staticmethod
    def fun(abc):
        return abc


a = Delft.fun("Stack")
print(a)

Output:

Stack

In the above example, we changed the fun() function to a static function. This way, we could use it without instantiating any class objects.

Conclusion

This tutorial discussed the TypeError: missing 1 required positional argument: 'self' error in Python and how we can solve it.

We discussed situations where this error might occur and how to fix the error by instantiating the object of the class. We can also use static methods to work around this error.

Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn

Related Article - Python TypeError

Related Article - Python Error