Understanding self in Python class

Working with class in Python means coming across methods that have self in there parameters. This self is slightly confusing especially for programmers who have migrated to Python from other objected oriented languages like Java, C++ etc. In this tutorial we will understand the concept of self in Python class and how Python manages it differently from other parameters.

Class variables and instance variables

Before starting with self lets first understand what are class variables and instance variables. If a variable is shared by each and every instance of a class then it is known as class variable.

class Student:
    name="Luffy"
    age=20

In the above example name and age are class variable of a class named Student. These variables can be used by any instance in the class Student. However, if a variable is kept by an instance for itself it is called instance variable.

Instance variables belongs solely to their respective instances. That means instance variables are different for every instance of a class or an object.

In the Student class example below, name and age are instance variables:

class Student:
    def __init__(self, name,age):
        self.name = name
        self.age = age

student1 = Student("Luffy", 19)
print(student1.name)
print(student1.age)

Output:

Luffy
19

In the above example a class Student is created along with instance variable name and self. In order to call the above class Student we have to create an object student1 of the class Student. The object created during class calling are called instance objects. We can create as many instance objects as we want of the class Student.

Now lets try to create the same instance object student1 but this time we will not use self as parameter.

class Student:
    def __init__(name,age):
        self.name = name
        self.age = age

student1 = Student("Luffy", 19)
print(student1.name) 
print(student1.age)

On running the above code we get an Error saying :

 student1 = Student("Luffy", 19)
TypeError: __init__() takes 2 positional arguments but 3 were given

The above error says required 2 positional arguments but 3 are given however we passed the exact 2 arguments name and age as asked. So what’s the problem ?

The problem is whenever a class object is called the instance is always passed as the first argument. When we use the word self it act as a container for that instance object.

What is self ?

So the word self in a class method always refers to the particular instance. If we run the same code above with just self included as a parameter to __init__ we get the following output:

Luffy
19

Thus self in Python represents the instance of the class. Self keyword is used to access attributes and methods of a class in Python. It binds the given arguments with the attributes.

You can use another parameter name in place of self however to increase the readability of code one must use the keyword ” self ” itself.

Now if you are thinking that how this instance object is magically making its way in as the first argument just take a look at this example:

class Student:
    def __init__(self,name):
        self.name = name
    def print_student(self):
        print(self.name)

student1 = Student("Luffy")
student2= Student("Zoro")
student1.print_student()
student2.print_student()

In this example we have created a class Student with just one instance variable name. This time we also have an instance method name print_student to print the name of the student. student1 and student2 are two instance object of the class student each with a different name and when we tried to print the two using method print_student we get the following output:

Output:

Luffy
Zoro

If we try to call print_student method but this time in class object namespace using the following code :

class Student:
    def __init__(self,name):
        self.name = name
    def print_student(self):
        print(self.name)

student1 = Student("Luffy")
student2= Student("Zoro")
#student1.print_student()
#student2.print_student()
Student.print_student()

We get an error saying :

    Student.print_student()
TypeError: print_student() missing 1 required positional argument: 'self'

Which means it is trying to informs us that we are missing self while calling the class object. Since self refers to the instance and we already have two of them with us named student1 and student2. Lets pass it and see the output:

class Student:
    def __init__(self,name):
        self.name = name
    def print_student(self):
        print(self.name)

student1 = Student("Luffy")
student2= Student("Zoro")
#student1.print_student()
#student2.print_student()
Student.print_student(student1)
Student.print_student(student2)

Output:

Luffy
Zoro

Thus with this example we can finally understand how this instance object is making its way in as the first argument.

Leave a Reply

Your email address will not be published. Required fields are marked *