Python TypeError: Name() takes no arguments Solution

Posted in /  

Python TypeError: Name() takes no arguments Solution
vinaykhatri

Vinay Khatri
Last updated on November 23, 2024

    In Python, we use the class keyword to create a blueprint for an object. And inside the class, we can define a special method __init__(self) which is the constructor of the class, and get automatically called when we create the class object. If we misspell, forget, or define the __init__() method with no argument, and create a class object by specifying the initial value. We will encounter the error TypeError: ClassName() takes no arguments Error.

    In this Python guide, we will discuss the TypeError: ClassName() takes no arguments Error in Detail and see the different scenarios when you may encounter this error in your Python program.

    Let's get started with the error statement

    Python Error: TypeError: ClassName() takes no arguments

    The Error statement TypeError: ClassName() takes no arguments can be divided into two parts, Exception Type ( TypeError ) and Error Message ( ClassName() takes no arguments ).

    1. TypeError

    TypeError is a standard Python exception. It is raised in a Python program when we perform an invalid operation or function on a Python object. For example, passing argument value to a function or method that accepts no arguments will raise the TypeError with a specific Error Message.

    2. ClassName() takes no arguments

    ClassName() takes no arguments is the Error message telling us that the class ClassName() __init__() method does not accept any argument value. When we initialize an object for a class, the __init__() method gets invoked automatically. If we have defined some parameters for the __init__() method in the class definition, we can pass argument values for those parameters during object initialization.

    This Error generally occurs in a Python program when we create an object for a class and pass an argument value to the __init__() method where the __init__() method is not defined or accepts no argument.

    Example Scenario

    There could be two scenarios where you may encounter this error.

    1. Forget to define a __init__(self) method and create an object by passing arguments.
    2. Misspelled the __init__(self) method.

    Example 1 (Forget to define __init__(self) method)

    __init__() is a special method. It is the constructor for the class and gets called when we initialize an instance or object for the Class. It is not necessary to define the __init__() method for a class, but if we wish to initialize some initial values to a class object, there we need to specify the __init__() method.

    If we forget to define the __init__() method and try to initialize the class object with some argument values we will receive the TypeError: ClassName() takes no arguments Error.

    Example 1

    # create a class
    class Student:
        # forget to define the __init__() method
    
        def show_marks(self):
            print(f"Total Marks of {self.name} are {self.marks}")
    
        def show_details(self):
            print("Name: ", self.name)
            print("Age: ", self.age)
            print("Grade: ", self.grade)
            print("Total Marks: ", self.marks)
    
    #
    name, age , grade, marks = ["Rahul Kumar", 17, "11th", 893]
    
    # create an object
    rahul = Student(name, age, grade, marks)

    Output

    Traceback (most recent call last):
      File "main.py", line 19, in 
        rahul = Student(name, age, grade, marks)
    TypeError: Student() takes no arguments

    Break the code

    In this example, we are getting this error because, at object creation, we are passing 4 arguments value name, age, grade, marks to the Student() class, which is supposed to accept by the __init__() method.

    By default, every time we define a class and create its object, Python automatically creates the __init__() method for that object. But that automatically created __init__(self) method does not accept any argument value. So if we want to initialize some initial values to object properties, we need to define the __init__() method in our class and specify the parameters. So it can accept all the argument values assigned during the object creation.

    Solution

    To solve the above problem, all we need to do is define the __init__() method for the class Student and define the 4 parameters for 4 argument values name, age, grade, and marks.

    # create a class
    class Student:
        # define the __init__() method
        def __init__(self, name, age, grade, marks):
            self.name = name
            self.age = age
            self.grade = grade
            self.marks = marks
    
        def show_marks(self):
            print(f"Total Marks of {self.name} are {self.marks}")
    
        def show_details(self):
            print("Name: ", self.name)
            print("Age: ", self.age)
            print("Grade: ", self.grade)
            print("Total Marks: ", self.marks)
    
    #
    name, age , grade, marks = ["Rahul Kumar", 17, "11th", 893]
    
    # create an object
    rahul = Student(name, age, grade, marks)
    
    rahul.show_details()

    Example 2 (Misspelt the __init__(self) method)

    The method calling is similar to the function call. The same goes for the invoking of the __init__() method. As we do not call the __init__() method explicitly, it gets automatically called when we initialize the class object. Here the thing to keep in mind is that the __init__() is a reserved method, and when we use it in our class, we are just overriding the default __init__() method.

    If during defining a __init__() method if we misspelled it, that method will be treated as a completely different method, and it will be assumed that the class has no __init__() method.

    Example 2

    # create a class
    class Student:
        # misspell the __init__() method
        def __inti__(self, name, age, grade, marks):
            self.name = name
            self.age = age
            self.grade = grade
            self.marks = marks
    
        def show_marks(self):
            print(f"Total Marks of {self.name} are {self.marks}")
    
        def show_details(self):
            print("Name: ", self.name)
            print("Age: ", self.age)
            print("Grade: ", self.grade)
            print("Total Marks: ", self.marks)
    
    #
    name, age , grade, marks = ["Rahul Kumar", 17, "11th", 893]
    
    # initialize an object
    rahul = Student(name, age, grade, marks)
    
    rahul.show_details()

    Output

    Traceback (most recent call last):
        File "main.py", line 25, in <module>
            rahul = Student(name, age, grade, marks)
    TypeError: Student() takes no arguments

    Break the code

    In this example, we are getting the same error statement as we are receiving in the above example. If we analyze the code carefully, we will conclude that the core reason for this error is the same for both of the examples (Example 1 and Example 2).

    Even in this example, there is no __init__() method that is supposed to invoke and accept the argument sent by the object during object initialization. Although the mistake is completely different here, we have tried to define the __init__() method, but we misspelled it.

    Solution

    To solve the above example, all we need to do is correct the spelling of __init__() method. Becaue Python is a case-sensitive programming language.

    # create a class
    class Student:
        # correct the __init__() method
        def __init__(self, name, age, grade, marks):
            self.name = name
            self.age = age
            self.grade = grade
            self.marks = marks
    
        def show_marks(self):
            print(f"Total Marks of {self.name} are {self.marks}")
    
        def show_details(self):
            print("Name: ", self.name)
            print("Age: ", self.age)
            print("Grade: ", self.grade)
            print("Total Marks: ", self.marks)
    
    #
    name, age , grade, marks = ["Rahul Kumar", 17, "11th", 893]
    
    # initialize an object
    rahul = Student(name, age, grade, marks)
    
    rahul.show_details()

    Output

    Name: Rahul Kumar
    Age: 17
    Grade: 11th
    Total Marks: 893

    Conclusion

    The Python Error "TypeError: Name() takes no arguments" is raised when we forget to define the __init__() method or misspell it, and we try to pass some argument values to the init method during object initialization. It is not necessary that if you do not define or misspell the __init__() method and you will encounter this error. You will only encounter this error when you pass some initial argument values to the object during object creation, and there is no __init__() method defined in the class that can accept all those arguments.

    If you are getting this error in your Python program, you can share your code in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments