Python classes need to be instantiated, or called, before you can access their methods. If you forget to instantiate an object of a class and try to access a class method, you encounter an error saying “missing 1 required positional argument: ‘self’”.
In this guide, we talk about what this error means and why it is raised. We walk through an example of this error in action to help you learn how to fix it.
missing 1 required positional argument: ‘self’
Positional arguments refer to data that is passed into a function. In a class, every function must be given the value “self”. The value of “self” is similar to “this” in JavaScript. “self” represents the data stored in an object of a class.
When you call a class method without first instantiating an object of that class, you get an error. This is because “self” has no value until an object has been instantiated.
The most common mistakes that are made that cause the “missing 1 required positional argument: ‘self’” error are:
- Forgetting to instantiate an object of a class
- Using the incorrect syntax to instantiate a class
Let’s walk through each of these causes individually.
Cause #1: Forgetting to Instantiate an Object
An object must be instantiated before you can access a method in a class.
Define a class that stores information about a hero in a video game:
class Hero: def __init__(self, name, player_type): self.name = name self.player_type = player_type
Next, we add a function to our class. Functions inside of classes are called methods. This method prints out the name of a player and their player type:
def show_player(self): print("Player Name: " + self.name) print("Player Type: " + self.player_type)
Try to access our class so that we can create a player:
luke = Hero.show_player()
We have created an object that is assigned to the variable “luke”. This object is derived from the Hero class. We call the show_player()
method to show information about the player.
Let’s run our code altogether and see what happens:
Traceback (most recent call last): File "main.py", line 10, in <module> luke = Hero.show_player() TypeError: show_player() missing 1 required positional argument: 'self'
Our code fails. This is because we have not instantiated an object of Hero. Hero.show_player()
does not work because we haven’t created a Hero whose information can be displayed.
To solve this error, we first instantiate an object before we call show_player()
:
luke = Hero("Luke", "Mage") luke.show_player()
Run our code again:
Player Name: Luke Player Type: Mage
Our code runs successfully! We’ve first declared a variable called “luke” which stores information about a player called Luke. Luke’s player type is “Mage”. Now that we have instantiated that object, we can call the show_player()
method.
Cause #2: Incorrectly Instantiating a Class
The “missing 1 required positional argument: ‘self’” error can occur when you incorrectly instantiate a class. Consider the following code:
luke = Hero luke.show_player()
While this code is similar to our solution from the last example, it is incorrect. This is because we have not added any parenthesis after the word Hero. By missing these parentheses, our program does not know that we want to instantiate a class.
Solve this problem by adding parenthesis after Hero and specifying the required arguments, “name” and “player_type”:
luke = Hero("Luke", "Mage") luke.show_player()
Our code now runs successfully and returns information about our player:
Player Name: Luke Player Type: Mage
Conclusion
The “missing 1 required positional argument: ‘self’” error is raised when you do not instantiate an object of a class before calling a class method. This error is also raised when you incorrectly instantiate a class.
To solve this error, make sure that you first instantiate an object of a class before you try to access any of that class’ methods. Then, check to make sure you use the right syntax to instantiate an object.
Now you’re ready to solve this common error like an expert Python developer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.