TypeError: __init__() missing 1 required positional argument” error in Python

TypeError: __init__() missing 1 required positional argument

This error occurs when you try to call a function without providing the required number of arguments. In this case, the error is telling you that you are missing one required positional argument.

To fix the TypeError init missing 1 required positional argument error, you need to provide the missing argument when calling the function. To avoid this error happening in the future, make sure to always provide the required number of arguments when calling a function.

Possible causes

Here are some possible scenarios that could trigger this error:

Missing argument(s) when creating an object

If you create an instance of a class but forget to pass one or more required arguments to the constructor, Python raises this error. For example:

class MyClass:
    def __init__(self, arg1, arg2):
        self.arg1 = arg1
        self.arg2 = arg2

obj = MyClass()  # Missing required arguments

Incorrect number of arguments

Another possibility is that you provide a different number of arguments than expected. This can happen if you miscount the number of arguments or mistakenly pass additional arguments. For example:

class MyClass:
    def __init__(self, arg1, arg2):
        self.arg1 = arg1
        self.arg2 = arg2

obj = MyClass(arg1_value)  # Missing the second argument

Incorrect order of arguments

If you provide arguments in the wrong order when creating an object, it can lead to this error. Python assigns arguments based on their order, so passing arguments in the incorrect order can result in missing required arguments. For example:

class MyClass:
    def __init__(self, arg1, arg2):
        self.arg1 = arg1
        self.arg2 = arg2

obj = MyClass(arg2_value, arg1_value)  # Swapped the order of arguments

Subclass not calling the parent class constructor:

If you create a subclass that overrides the __init__() method and does not call the constructor of the parent class explicitly, it can cause this error. The subclass should invoke the parent class’s __init__() method using super().__init__() to ensure the initialization of inherited attributes. For example:

class ParentClass:
    def __init__(self, arg1):
        self.arg1 = arg1

class ChildClass(ParentClass):
    def __init__(self, arg1, arg2):
        self.arg2 = arg2
        # Missing super().__init__(arg1) to initialize the parent class

obj = ChildClass(arg1_value, arg2_value)

How to fix

  1. Identify the missing argument: First, review the error message to determine which argument is missing. The error message usually specifies the name of the missing argument, which can help you identify the issue.
  2. Check the class’s constructor (init method): Look at the class definition and locate the __init__() method, also known as the constructor. Identify the required arguments listed within the parentheses of the __init__() method.
  3. Provide the missing argument(s): Make sure that when you create an object from the class, you provide all the required arguments in the correct order. Pass the missing argument(s) when creating the object, matching the order and number of arguments specified in the constructor.
  4. Verify the argument order: Double-check that you’re passing the arguments in the correct order. If the arguments are not provided in the right order, Python may associate them with the wrong variables in the constructor.
  5. Ensure subclass calls the parent class’s constructor: If you are working with subclasses, verify that the subclass’s __init__() method includes a call to the parent class’s constructor using super().__init__(). This ensures that the necessary initialization from the parent class occurs. Place this call at the beginning of the subclass’s __init__() method and pass any required arguments to it.
  6. Run the code again: After making the necessary adjustments, run your code again to see if the error is resolved. If you followed the steps correctly, the error should no longer occur.