In Python, every class method's first parameter is " self ". It is the instance of a class and the first argument value of every class method. It helps you access a class's variables, attributes, and methods.
When we call the method using the object, the object's value is passed as the first parameter to the method itself. That's why the argument name is "self". The name "self" is conventional. We can name it as any valid identifier.
If we have specified a different name to the
self
argument and used the
self
identifier inside the class method, we receive the error -
NameError: name 'self' is not defined
.
This Python guide will discuss the above error in detail and learn how to resolve it. We will walk through the two most common scenarios where you may encounter this error in Python programming.
Python Error: NameError: name 'self' is not defined
The "self" is the conventional first argument name specified while defining a function or method for a class. While calling the method using an object, Python passes the object as the value to the self-argument.
Example
class Car:
def __init__(self, car_name, owner):
self.car_name = car_name
self.owner = owner
def show_detail(self):
print("Car Name:", self.car_name)
print("Car Owner:", self.owner)
# create car object
car1= Car("Honda Civic", 'Joe Smith')
# call the function
car1.show_detail()
Output
Car Name: Honda Civic
Car Owner: Joe Smith
In this example, the method
show_detail(self)
has an argument
self.
But when we call the method using the object
joe,
there we did not specify any argument
joe.show_detail().
This is because when we call a method using an object, there we do not need to specify the value for
self
argument.
Here, the value
joe
will act as the first argument value
self
to the method.
Common Error Example Scenario
Now let's discuss the two common example scenario where we can encounter the error -
"
NameError: name 'self' is not defined
"
- Misspelled the name - self.
- Calling self-value in the arguments.
1. Misspelled the Name - self
We will receive the error if we misspell the name
self
while defining the arguments for the method and using the name self inside the method.
Example
class Car:
def __init__(self, car_name, owner):
self.car_name = car_name
self.owner = owner
#missplet the name self
def show_detail(slf):
print("Car Name:", self.car_name)
print("Car Owner:", self.owner)
# create car object
car1 = Car("Honda Civic", 'Joe Smith')
# call the function
car1.show_detail()
Output
Traceback (most recent call last):
File "main.py", line 15, in
car1.show_detail()
File "main.py", line 8, in show_detail
print("Car Name:", self.car_name)
NameError: name 'self' is not defined
In the above example, we receive an error in the
show_detail(slf)
method. We try to access the
car_name
property using the
self
name inside the method. But in the method definition, we have misspelled the name
slf
.
Solution:
To solve the above problem, we need to ensure that we specify the correct
self
name while defining the function.
class Car:
def __init__(self, car_name, owner):
self.car_name = car_name
self.owner = owner
def show_detail(self):
print("Car Name:", self.car_name)
print("Car Owner:", self.owner)
# create car object
car1 = Car("Honda Civic", 'Joe Smith')
# call the function
car1.show_detail()
Output
Car Name: Honda Civic
Car Owner: Joe Smith
2. Calling the self-value in the Arguments
The value of self is passed to the method when we call the method using the object. If we try to pass the self as the default argument value, we will receive the error
NameError: name 'self' is not defined
.
Example
class Car:
def __init__(self, car_name, owner):
self.car_name = car_name
self.owner = owner
def show_detail(self):
print("Car Name:", self.car_name)
print("Car Owner:", self.owner)
# evaluating self at function definition
def change_owner(self, name= self.owner):
self.owner = name
# create car object
car1 = Car("Honda Civic", 'Joe Smith')
# change owner
car1.change_owner("Jose")
car1.show_detail()
Output
Traceback (most recent call last):
File "main.py", line 1, in
class Car:
File "main.py", line 11, in Car
def change_owner(self, name= self.owner):
NameError: name 'self' is not defined
In this example, we receive the error with
def change_owner(self, name= self.owner):
. While defining the method
change_owner()
, we have provided a default value to the
name
argument
self.owner
.
Python evaluates a function's or method's default arguments during the definition. The value of self is only assigned during the method calls. That's why in the method definition, Python could not evaluate the value of
self.owner
and raise the error.
Solution
We can not use self as a default parameter. To solve the above problem, we will give the
name
a value of
None
as a default argument, that will make the
name
as an optional argument.
class Car:
def __init__(self, car_name, owner):
self.car_name = car_name
self.owner = owner
def show_detail(self):
print("Car Name:", self.car_name)
print("Car Owner:", self.owner)
def change_owner(self, name= None):
if name==None:
self.owner=self.owner
else:
self.owner = name
# create car object
car1 = Car("Honda Civic", 'Joe Smith')
# change owner
car1.change_owner("Jose")
car1.show_detail()
Output
Car Name: Honda Civic
Car Owner: Jose
Conclusion
The error statement "
NameError: name 'self' is not defined
", is one of the most common errors many Python learners encounter while dealing with classes, methods, and objects.
The error statement has two sub-statements NameError, and "name 'self' is not defined". The
NameError
is a Python exception type that arises in
a program
when Python cannot find the identifier name in its scope. The statement "
name 'self' is not defined
" is the error message that arises with
NameError
when we use a name
self
that is not defined for a method, program, and default argument.
If you still get this error in your Python program, please share your code and query in the comment section. We will try to resolve it.
Good luck!
People are also reading:
- Python typeerror: ‘str’ object is not callable Solution
- Pass Statement in Python
- Python TypeError: ‘method’ object is not subscriptable Solution
- Looping Technique in Python
- Python Error: TypeError: ‘tuple’ object is not callable Solution
- Anonymous Function in Python
- Python typeerror: list indices must be integers or slices, not str Solution
- Exception Handling in Python
- Python NameError name is not defined Solution
- Multiple Inheritance in Python
Leave a Comment on this Post