TypeError: __init__() missing 1 required positional argument

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

The error “TypeError: __init__() missing 1 required positional argument” occurs when we forget to provide the required argument when creating the instance of the class.

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

If you want to solve this error then you need to make sure to specify the argument or you can also set the default value for the argument.

Let me share with you a Python example code for this error:

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

    def get_name(self):
        return self.name


# TypeError: Car.__init__() missing 1 required positional argument: 'name'

obj= Car()

If you run the above code, you will get the error “TypeError: init() missing 1 required positional argument” because we are not passing any value as an argument.

[Solved] TypeError: init() missing 1 required positional argument

One way to solve this error is to provide the value for the argument. Let me show you a Python code example.

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

    def get_name(self):
        return self.name

obj= Car("Audi")

Now, this code is fine and if you run it you will not get any errors. If you want to print this name you can add this line to the code

print(obj.name)

#Output = Audi

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

Another way to solve this error is to use the default value for the argument, if you forget to pass the value when creating the instance of the class then the default value will be assigned automatically.

[Fixed]: ModuleNotFoundError: No module named ‘sklearn’ in Python

You can set the default value as shown in the code below:

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

    def get_name(self):
        return self.name

obj= Car()
obj1= Car("Audi")

If we see the above code, we are creating two instances of the class Car. We are passing value for the argument for the ‘Obj1’ but not for the instance ‘Obj’.

If you try to print the obj.name our output will be:

print(obj.name)   #Output -> Toyota
print(obj1.name)  #Output -> Audi

Conclusion on TypeError: init() missing 1 required positional argument

Programmer, we discussed how we can solve this error “TypeError: init() missing 1 required positional argument“. You can easily solve this error by following the above solution. If you are still getting this error, please let us know in the comments section.

Leave a Reply