TypeError: missing 1 required positional argument: 'self'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

TypeError: missing 1 required positional argument: 'self' #

The Python "TypeError: missing 1 required positional argument: 'self'" occurs when we call a method on the class instead of on an instance of the class. To solve the error, instantiate the class first and call the method on the instance, e.g. emp1.get_name().

typeerror missing 1 required positional argument self

Here is an example of how the error occurs.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def get_name(self): return self.name # ⛔️ TypeError: Employee.get_name() missing 1 required positional argument: 'self' print(Employee.get_name())

We called the get_name method on the Employee class rather than on an instance of Employee which caused the error.

To solve the error, instantiate the class and call the method on the instance.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary def get_name(self): return self.name # ✅ instantiate class first emp1 = Employee('Alice', 100) # ✅ call method on class instance print(emp1.get_name()) # 👉️ "Alice"

We instantiated the class and called the get_name method on the instance.

When we instantiate a class, we should provide all the arguments that we have specified in the classes' __init__() method (other than self which is passed automatically).

When a class defines the __init__() method, the method is invoked when an instance is created.

If your class doesn't define an __init__() method, you don't have to pass any arguments when instantiating it.

If you pass arguments when instantiating a class, the arguments are passed on to the __init__() method.

Note that the first argument the __init__() method takes is self.

You could name this argument anything because the name self has no special meaning in Python.

self represents an instance of the class, so when we assign a variable as self.my_var = 'some value', we are declaring an instance variable - a variable unique to each instance.

The "TypeError: missing 1 required positional argument: 'self'" occurs because we specified the self argument in the get_name method.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary # 👇️ specified self arg def get_name(self): return self.name emp1 = Employee('Alice', 100) print(emp1.get_name()) # 👉️ "Alice"

If we call the get_name method on an instance of the class, Python automatically passes self to the class method.

If your method doesn't make use of the self argument, you can declare a static method.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary @staticmethod def get_name(): return 'Alice' print(Employee.get_name()) # 👉️ "Alice"

A static method does not receive an implicit first argument and can be called on the class or on an instance of the class.

You can also transform the method into a class method using the @classmethod decorator.

main.py
class Employee(): def __init__(self, name, salary): self.name = name self.salary = salary @classmethod def get_name(cls): print(cls) return 'Alice' print(Employee.get_name()) # 👉️ "Alice"

A class method gets passed the class as an implicit first argument, just like an instance method gets passed the instance.

You can call a class method on the class (Employee.get_name()) or on an instance of the class (Employee('Alice', 100).get_name()).

If you call a class method on an instance of the class, the instance is ignored except for its class.

Use the search field on my Home Page to filter through my more than 3,000 articles.