Last updated: Apr 10, 2024
Reading timeยท3 min
The "NameError: name 'self' is not defined" occurs for multiple reasons:
self
argument in a method.self.attribute
as a default value in a method argument.self
outside of a method.Here is an example of how the error occurs.
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')
The issue is that we're using the self
argument as a default value in the
method's definition.
self
is an argument that represents the instance the method is called on, so it is available only when the method is called.If this is how you got the error, specify a default value inside the method's definition instead.
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
We check if the last
argument is None and if it
is, we set it to the value of the first
argument.
self
outside of a methodYou might also get the error if you try to access self outside of a method.
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')
We tried to access self
directly in the class, outside of a method which
caused the error.
self.
prefixIf you meant to declare a class variable, omit the self.
prefix.
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
The class has a default_salary
class variable and first
and last
instance
variables.
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.
self
as an argument of a method that uses itAnother 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.
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.
To solve the "NameError: name 'self' is not defined", make sure:
self
argument in a method.self
outside of a method.self
argument as a default value of another argument
in a method.