TypeError: method() missing 1 required positional argument: ‘self’

TypeError: method() missing 1 required positional argument: 'self'

Have you ever encountered the “TypeError: method() missing 1 required positional argument: ‘self'” error message while working with Python classes? If so, you’re not alone! This error message can be quite confusing, especially for beginners who are just starting to learn about object-oriented programming in Python.

The “TypeError: method() missing 1 required positional argument: ‘self'” is a Python error message caused by calling a class method without an instance of the class passed as the first argument. To fix, include the “self” parameter in the method definition and call the method on an instance of the class.

Python is a high-level, interpreted programming language that is widely used for web development, scientific computing, data analysis, artificial intelligence, and machine learning. It has many built-in data types, including strings, integers, lists, tuples, and dictionaries, and also supports object-oriented programming (OOP) concepts such as inheritance and polymorphism. OOP allows developers to organize their code into classes and objects, making it easier to manage and maintain.

However, while working with Python, you may come across an error message that says “TypeError: method() missing 1 required positional argument: ‘self’“. In this article, we will explore the cause of this error message and how to fix it.

What does “TypeError: method() missing 1 required positional argument: ‘self'” mean?

In Python, a method is a function that is associated with a class and can be called on an instance of that class. When you define a method in a class, you need to include the “self” parameter as the first argument. This parameter refers to the instance of the class on which the method is being called and allows you to access the instance’s attributes and methods.

The “TypeError: method() missing 1 required positional argument: ‘self'” error message occurs when you call a method on an instance of a class, but forget to include the “self” parameter. This means that the method is missing one required positional argument, which is the instance of the class on which the method is being called.

Reason 1:

Calling a method on the class instead of an instance of the class

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

    def learn(self):
        print(self.name + "Python")\

Person.learn()


Output:
   
   Person.learn()
TypeError: Person.learn() missing 1 required positional argument: 'self'

Process finished with exit code 1

Reference:

TypeError: method() missing 1 required positional argument: 'self'

In this example, we define a Person class with an init method that takes a name parameter and a learn method that prints a greeting using the name attribute. However, when we call the learn method on the Person class instead of an instance of the class, we get the “TypeError: method()

Solution

we need to create an instance of the Person class and call the learn method on that instance, like below:

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

    def learn(self):
        print(self.name + " promoted to advanced level in python")

person = Person("John")
person.learn()

output:

John promoted to advanced level in python

Process finished with exit code 0

Reference:

TypeError: method() missing 1 required positional argument: 'self'

Here, we create an instance of the Person class with the name “John” and assign it to the variable “person”. Then we call the learn method on the “person” instance, which prints the message “John promoted to advanced level in python”. By calling the method on an instance of the class rather than on the class itself, we avoid the “TypeError: method() missing 1 required positional argument: ‘self’” error message.

Reason 2:

When you try to call a method on an instance of a subclass, but you forget to Instantiating the class properly.

Example:

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

    def speak(self):
        print(self.name + " says hi")

dog = Animal()
dog.speak("Don")

Output:

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

Process finished with exit code 1

Reference:
TypeError: method() missing 1 required positional argument: 'self'

In this example, we define an Animal class with an init method that takes a name parameter and a speak method that prints a greeting using the name attribute. We are instantiating the class Animal and calling the method speak, Which failed with the error “”. This is because we have not provide any argument while instantiating the class Animal with (), Our program does not know that we want to instantiate a class.

Solution

To resolve the ERROR message, We can rewrite the above code as below

Example

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

def speak(self):
print(self.name + " says hi")

dog = Animal("Don")
dog.speak()

Output:

Don says hi

Process finished with exit code 0
TypeError: method() missing 1 required positional argument: 'self'

In the above example, We have rewritten the code to instantiating the class properly with all the arguments

Conclusion

In conclusion, the “TypeError: method() missing 1 required positional argument: ‘self'” error message can be a common issue for Python programmers, particularly those who are new to object-oriented programming. The error occurs when a method is called on a class without an instance of the class passed as the first argument, and it can be caused by a variety of mistakes, such as incorrectly instantiating the class

However, by understanding the cause of this error and following the steps to fix it, Python developers can avoid this error and continue to write robust and functional programs.

Read More:

Fix – attributeerror: module numpy has no attribute int

How to Convert JSON to YAML

How to Modify Python Global Variable inside a Function

What is [:] in python

Jerry Richard R
Follow me