Open In App

Nameerror: name self is not defined in Python

Last Updated : 31 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Python, a dynamically typed and object-oriented programming language, is lauded for its simplicity and readability. However, even the most seasoned developers can encounter stumbling blocks, and one such common hurdle is the “NameError: name ‘self’ is not defined.” In this article, we will see how to fix Nameerror: Name ‘self’ is not defined in Python.

What is Nameerror: Name ‘self’ Is Not Defined In Python?

The ‘NameError: name ‘self’ is not defined’ typically arises in the context of object-oriented programming (OOP) when working with classes and instances. ‘self’ is a convention in Python used to refer to the instance of a class, and encountering this error indicates that the interpreter cannot recognize the intended reference to the instance. Understanding the scenarios in which this error occurs is pivotal for devising effective solutions.

Why does Undefined Variable NameError Occurs in Python

Below are some reasons due to which Nameerror: Name ‘self‘ Is Not Defined Occurs in Python:

  • Missing self in Method Declaration
  • Incorrect Spelling of ‘self’
  • Accessing Instance Variables Without ‘self’
  • Incorrect Capitalization of ‘self’

Missing self in Method Declaration

In this example, ‘self’ is absent from the my_method declaration, causing a NameError when attempting to access instance attributes within the method.

Python3




class MyClass:
    def my_method(n):
      self.n=n
      print("Hello, World!")
 
obj = MyClass()
obj.my_method()


Output:

Hangup (SIGHUP)
Traceback (most recent call last):
  File "Solution.py", line 7, in <module>
    obj.my_method()
  File "Solution.py", line 3, in my_method
    self.n=n
NameError: name 'self' is not defined

Incorrect Spelling of ‘self’

A common typographical error, such as misspelling ‘self’ as ‘Slef,’ can lead to a NameError as the interpreter fails to recognize the intended reference.

Python3




class AnotherClass:
    def __init__(Slef, value):
        self.value = value
 
obj = AnotherClass(42
print(obj.value)


Output:

Hangup (SIGHUP)
Traceback (most recent call last):
  File "Solution.py", line 5, in <module>
    obj = AnotherClass(42)  
  File "Solution.py", line 3, in __init__
    self.value = value
NameError: name 'self' is not defined

Accessing Instance Variables Without ‘self’

Forgetting to use ‘self.’ when assigning values to instance variables within the constructor can result in the ‘self’ variable being undefined.

Python3




class YetAnotherClass:
    def __init__(s,name):
        self.name = name
 
obj = YetAnotherClass("John")
print(obj.name)


Output:

Hangup (SIGHUP)
Traceback (most recent call last):
  File "Solution.py", line 5, in <module>
    obj = YetAnotherClass("John")
  File "Solution.py", line 3, in __init__
    self.name = name 
NameError: name 'self' is not defined

Incorrect Capitalization of ‘self’

Python is case-sensitive, and using ‘Self’ instead of ‘self’ in the method signature will lead to a NameError.

Python3




class CapitalizedClass:
    def __init__(Self, value):  # Incorrect capitalization of 'self'
        self.value = value
 
obj = CapitalizedClass(10)
print(obj.value)


Output:

Hangup (SIGHUP)
Traceback (most recent call last):
  File "Solution.py", line 5, in <module>
    obj = CapitalizedClass(10) 
  File "Solution.py", line 3, in __init__
    self.value = value
NameError: name 'self' is not defined

Fix Nameerror: Name self’ Is Not Defined in Python

Below are some solution approaches to solve Nameerror: Name ‘self’ Is Not Defined error in Python:

  • Method Declaration
  • Spelling Check
  • Instance Variable Assignments
  • Capitalization Check

Python Method Declaration

In this example, the ‘self’ parameter is added to the method declaration of my_method in the MyClass class, ensuring proper referencing of instance attributes within the method.

Python3




class MyClass:
    def my_method(self,n):
      self.n=n
      print("Hello, World!")
 
obj = MyClass()
obj.my_method(6)


Output

Hello, World!


Spelling Check in Class Constructor

In this example, the spelling error ‘Slef’ is corrected to ‘self’ in the constructor of the AnotherClass class, allowing for the proper creation and manipulation of instances.

Python3




class AnotherClass:
    def __init__(self, value):
        self.value = value
 
obj = AnotherClass(42
print(obj.value)


Output

42


Instance Variable Assignments

In this example, the missing ‘self.’ is added before variable assignment in the constructor of the YetAnotherClass class, ensuring that the variable belongs to the instance.

Python3




class YetAnotherClass:
    def __init__(self,name):
        self.name = name
 
obj = YetAnotherClass("John")
print(obj.name)


Output

John


Capitalization Check

In this example, the incorrect capitalization ‘Self’ is corrected to ‘self’ in the constructor of the CapitalizedClass class, resolving the NameError and allowing proper instantiation.

Python3




class CapitalizedClass:
    def __init__(self, value): 
        self.value = value
 
obj = CapitalizedClass(10)
print(obj.value)


Output

10




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads