TypeError: missing 1 required positional argument: ‘self’

Are you facing the error “missing 1 required positional argument self“? Yes, you have come to the right place. Today I will show you how to solve this error.

We get this error “missing 1 required positional argument: self” when we call the method on the class instead of the instance of the class.

[Fixed] missing 1 required positional argument self

You can solve this error by just making the instance of the class and calling the method with the instance of the class. If you do not understand yet, don’t worry I will explain with an example.

[Fixed]: TypeError: __init__() missing 1 required positional argument

Example of missing 1 required positional argument self

We have a Class with the Name “Car” as shown in the code below:

class Car():
    def __init__(self, name, speed):
        self.name = name
        self.speed= speed

    def car_name(self):
        return self.name


#TypeError: Car.car_name() missing 1 required positional argument: 'self'
print(Car.car_name())

In the above code, we are calling the method “car_name()” with the class itself “Car”. If we will call the method like this then we will surely get this error “missing 1 required positional argument: self”.

How to solve this error in the above code example?

If you want to solve the error in the above code example then you will need to create the instance of the object first and then call the methods with the instance of the class.

class Car():
    def __init__(self, name, speed):
        self.name = name
        self.speed= speed

    def car_name(self):
        return self.name

# Creating the instance of the class
obj = Employee('Audi', 518)

# Calling the method on the Instance of the class
print(obj.car_name())

[Solution] missing 1 required positional argument: self

We can also solve this error by using the @classmethod decorator. When you decorate the method with the classmethod then you call the method on the class and on the instance of the class.

class Car():
    def __init__(self, name, speed):
        self.name = name
        self.speed= speed

    def car_name(cls):
        print(cls)
        return 'Alice'

# Calling the method on the class
print(Car.car_name())

Conclusion on TypeError: missing 1 required positional argument: ‘self’

Programmers, we discussed how we can solve the error “TypeError: missing 1 required positional argument: ‘self’“. I hope you have solved your issue. If you are still facing the error please let us know in the comments section.

The Python “TypeError: missing 1 required positional argument: ‘self’” occurs when we call a method on the class instead of on an instance of the class.

Leave a Reply