NameError: name 'self' is not defined in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# NameError: name 'self' is not defined in Python [Solved]

The "NameError: name 'self' is not defined" occurs for multiple reasons:

  1. Forgetting to specify the self argument in a method.
  2. Trying to use self.attribute as a default value in a method argument.
  3. Trying to access self outside of a method.

nameerror name self is not defined

Here is an example of how the error occurs.

main.py
class Employee(): def __init__(self, first, last=self.first): self.first = first self.last = last # โ›”๏ธ NameError: name 'self' is not defined bob = Employee('bobby', 'hadz')

using self as default argument

The issue is that we're using the self argument as a default value in the method's definition.

Default arguments in Python are evaluated when the function is defined, however, self is an argument that represents the instance the method is called on, so it is available only when the method is called.

# Specify a default value inside the method's definition

If this is how you got the error, specify a default value inside the method's definition instead.

main.py
class Employee(): def __init__(self, first, last=None): self.first = first if last is None: self.last = self.first bob = Employee('bobby') print(bob.first) # ๐Ÿ‘‰๏ธ bobby print(bob.last) # ๐Ÿ‘‰๏ธ bobby

specify default value in method definition

We check if the last argument is None and if it is, we set it to the value of the first argument.

# Trying to access self outside of a method

You might also get the error if you try to access self outside of a method.

main.py
class Employee(): # โ›”๏ธ NameError: name 'self' is not defined self.salary = 123 def __init__(self, first, last=None): self.first = first self.last = last bob = Employee('bobby')

accessing self outside method

We tried to access self directly in the class, outside of a method which caused the error.

# Declare class variables without using the self. prefix

If you meant to declare a class variable, omit the self. prefix.

main.py
class Employee(): # ๐Ÿ‘‡๏ธ Declare class variable default_salary = 123 def __init__(self, first, last): self.first = first self.last = last alice = Employee('alice', 'smith') bob = Employee('bobby', 'hadz') print(alice.default_salary) # ๐Ÿ‘‰๏ธ 123 print(bob.default_salary) # ๐Ÿ‘‰๏ธ 123

declare class variable

The class has a default_salary class variable and first and last instance variables.

Class variables are shared by all instances and can be accessed directly on the class, e.g. Employee.default_salary.

Instance variables are unique to each instance you create by instantiating the class.

Regular methods get passed a self object that gives us a reference to the instance on which the method was called.

# Forgetting to specify self as an argument of a method that uses it

Another common cause of the error is forgetting to specify self as an argument of a method that uses it.

Make sure to specify self as the first argument in all class methods that make use of it.

main.py
class Employee(): default_salary = 123 def __init__(self, first, last): self.first = first self.last = last # ๐Ÿ‘‡๏ธ Specify self as first argument def greet(self, greet): return greet + self.first + ' ' + self.last alice = Employee('alice', 'smith') bob = Employee('bobby', 'hadz') print(alice.greet('hi ')) # ๐Ÿ‘‰๏ธ hi alice smith print(bob.greet('hey ')) # ๐Ÿ‘‰๏ธ hey bobby hadz print(alice.default_salary) # ๐Ÿ‘‰๏ธ 123 print(bob.default_salary) # ๐Ÿ‘‰๏ธ 123

The first and last instance variables are unique to each instance, but the instances share the same default_salary class variable.

Note that the name of the self argument is just a convention. In practice, you could use any other name, e.g. this.

However, this is not recommended as the convention is followed by most (if not all) Python developers and not following it would make your code more confusing and more difficult to read.

# Conclusion

To solve the "NameError: name 'self' is not defined", make sure:

  1. You haven't forgotten to specify the self argument in a method.
  2. You haven't accessed self outside of a method.
  3. You haven't used the self argument as a default value of another argument in a method.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev