The value “self” is only available inside a method when a function is called and specified.
You cannot access “self” in the arguments specified to a method, or inside a function without specifying “self” as an argument. Otherwise, you see the error “NameError: name ‘self’ is not defined”.
In this guide, we talk about what this error means and why it is raised. We walk through a few code snippets to help you figure out how to solve this error in your code.
NameError: name ‘self’ is not defined
The “self” variable holds information about an object inside a class. All of the values that have been assigned to an object are available in the “self” variable.
“self” must be passed as an argument if you want to use it in a method. The “self” variable cannot be used in other arguments in a method because it is only accessible from within a method.
You encounter the “NameError: name ‘self’ is not defined” error if you:
- Do not specify “self” as an argument
- Use “self” as an argument in another argument
Let’s walk through each of these scenarios one-by-one.
Scenario #1: “self” is Not Listed as an Argument
“self” must be listed as an argument for it to be accessible in a method. “self” is not a global variable. It is local inside a class.
Write a program that holds information about movies in a class. We start by defining our class with a constructor that holds values about our movie:
class Movie: def __init__(self, name, year_released): self.name = name self.year_released = year_released
Our class can hold two values: the name of a movie and the year in which it was released. Next, we declare a method that lets us change the value of “year_released”:
def change_year(year_released): self.year_released = year_released print("{} was released in {}.".format(self.name, self.year_released))
To test our code, we create an object of our class. This object represents the movie Happy Gilmore, released in 1996:
happy_gilmore = Movie("Happy Gilmore") happy_gilmore.change_year(1996)
We have called the change_year()
method on our object so that we can set the year the movie was released to 1996. Let’s run our code and see if it works:
Traceback (most recent call last): File "main.py", line 11, in <module> happy_gilmore.change_year() File "main.py", line 7, in change_year self.year_released = year_released NameError: name 'self' is not defined
Our code returns an error.
This error is raised because we haven’t passed “self” as an argument to our method. We fix this error by adding “self” as the first argument in the change_year()
method:
def change_year(self, year_released): self.year_released = year_released print("{} was released in {}.".format(self.name, self.year_released))
Run our code again:
Happy Gilmore was released in 1996.
Our code runs successfully!
Scenario #2: Using “self” as an Argument in Another Argument
“self” is evaluated when a function is called. This means that you cannot have an argument that refers to “self” in the list of arguments specified in a function call.
Update our “year_released” method so that, if a different year of release is not specified, a message is printed to the console telling us that the year of a movie has not been changed.
We can do this by setting a default argument in our code:
def change_year(self, year_released=self.year_released): if year_released != self.year_released: self.year_released = year_released print("{} was released in {}.".format(self.name, self.year_released)) else: print("This movie has not been changed.") print(year_released)
“year_released” now has the default value of “self.year_released”. This means if we do not specify a value to which the year should be changed, a default value is set. If we do specify a value, the value we specify is used instead of the default.
If the value we specify is not equal to the value of “self.year_released”, the value of “self.year_released” is changed. Otherwise, a message is printed to the console telling the user that the movie has not been changed.
After our if
statement has been evaluated, the value of “year_released” is printed to the console.
Let’s run our code:
Traceback (most recent call last): File "main.py", line 1, in <module> class Movie: File "main.py", line 6, in Movie def change_year(self, year_released=self.year_released): NameError: name 'self' is not defined
Our code raises an error. This is because we’ve tried to use “self” in another argument in our list of arguments.
We can fix this error by setting the value of the “year_released” variable to “self.year_released” inside our function instead of in our list of arguments:
def change_year(self, year_released=None): if year_released != self.year_released: self.year_released = year_released print("{} was released in {}.".format(self.name, self.year_released)) else: year_released = self.year_released print("This movie has not been changed.") print(year_released)
In this code, we set “year_released” to be equal to “self.year_released” if we do not specify a value for “year_released” in our function call.
Let’s create a new Movie object to test out our code:
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
happy_gilmore = Movie("Happy Gilmore", 1995) happy_gilmore.change_year(1996)
We have incorrectly specified the year Happy Gilmore was released as 1995. We need to change it using the change_year()
method. Our code returns:
Happy Gilmore was released in 1996. 1996
Our code was successfully executed. Let’s test our code to see what happens if the value of “year_released” is already equal to the one we specify in the change_year()
method:
happy_gilmore = Movie("Happy Gilmore", 1996) happy_gilmore.change_year(1996)
Our code returns:
This movie has not been changed. 1996
Our code executes the else
statement in our code and then tells us that the movie has not been changed.
Conclusion
The “NameError: name ‘self’ is not defined” error is raised when you forget to specify “self” as a positional argument or when you use “self” in another argument in a list of arguments.
You solve this error by making sure that all methods in a function that use “self” include “self” in their list of arguments. If that does not work, make sure that no arguments in a list of arguments depend on “self” for their default values.
Now you’re ready to solve this Python error like a professional 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.