How To Fix TypeError: __init__() missing 1 required positional argument

Many developers get the error message: TypeError: __init__() missing 1 required positional argument in Python. This is a common issue when working with a class. This article will explain the cause and give the solution. Scroll down to learn!

Cause of TypeError: __init__() missing 1 required positional argument

After defining a class in Python, you try to create a new object. However, the Python program fails to run and you get the error message: TypeError: __init__() missing 1 required positional argument.

Here is the code sample that leads to the issue:

# Create a class Student
class Student:
    def __init__(self, studentName):
        self.studentName = studentName

# Create a new object: student
student = Student()

# Print the name of the object student
print(student.studentName)

As you run the code, the error message occurs:

  student = Student()
TypeError: __init__() missing 1 required positional argument: 'studentName'

Please notice that the __init__() function of the class Student() requires a value for the studentName. If you pass no value to it, the TypeError will occur, preventing the Python program from running properly.

How to fix TypeError: __init__() missing 1 required positional argument

After determining the cause, we will move on to how to fix TypeError: __init__() missing 1 required positional argument.

There are 2 ways to fix the problem:

Add value when creating the object

As mentioned above, you have to add a value inside the () when creating the object. Otherwise, the __init__() function of the class can not work, which leads to the TypeError.

The syntax is like this:

class [Object]:    
   def __init__(self, objectName):        
     self.objectName = objectName
[variable] = [Object]([Name of the object])
print([variable].objectName)

The [Name of the object] should be in a string format. As you print the object name out, the output will be this string.

Here is the code sample:

# Create a class Student
class Student:
    def __init__(self, studentName):
        self.studentName = studentName

# Create an object student with the studentName: Andy
student = Student("Andy")

# Print the studentName of the object
print(student.studentName)

The output will be:

Andy

Add value when defining the __init__ () function

You don’t have to add value when creating the object. Another way is to create a first value in the definition of the __init__() function. In this way, a new object created will automatically take this first value for its attribute.

The syntax is like this:

class [Object]:    
   def __init__(self, objectName = [Name of the object]):        
     self.objectName = objectName
[variable] = [Object]([Name of the object])
print([variable].objectName)

Instead of passing the value inside the () when creating the object, you will move this value inside the objectName parameter of the __init__() function.

Here is the code sample:

# Create a class Student
class Student:
    def __init__(self, studentName="Andy"):
        self.studentName = studentName

# Create the object Student with the studentName: Andy
student = Student()

# Print the name of the object
print(student.studentName)

The output will be:

Andy

Of course, you can still assign a new value to the attribute of the object. If you don’t want your object to have the first value, simply pass a different value to the () when creating it.

Here is the sample:

# Create a class Student
class Student:
    def __init__(self, studentName="Andy"):
        self.studentName = studentName

# Create the object with the studentName: William instead of Andy
student = Student("William")

# Print the name of the object
print(student.studentName)

The output will be:

William

Summary

In conclusion, the TypeError: __init__() missing 1 required positional argument in Python occurs when you create an object without adding a value to it. The __init__() function will have no value to initialize the object. You can fix this problem by adding the value directly inside the () when initializing or setting the first value in the definition of the __init__() function.

Leave a Reply

Your email address will not be published. Required fields are marked *