Saturday, June 4, 2022

self in Python

In this post we’ll try to understand what is self in Python along with usage examples.

self variable

When you create a new instance of the class, for example-

obj = MyClass() 

a new object is created with its own memory and reference to that object is assigned to the variable obj.

self in Python is also a reference to the current object and it is similar to this in Java and C++. By using self you can access the attributes and methods of a class in python.

Following Python example clarifies how self refers to the current object.

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

  def display_data(self):

person1 = Person('John', 40)
person2 = Person('Jessica', 32)



As you can see from the output when displayData() method is called on the object person1 it displays the data person1 object is initialized with i.e. self is referring to the person1 object. When displayData() method is called on the object person2 it displays the data person2 object is initialized with i.e. self is referring to the person2 object.

Even in the __init__() function first argument is 'self'. When the constructor is called for initializing the object after it's creation object reference is passed to the first argument (self) of the constructor.

self is not a keyword in Python

All the methods in a class including __init__() must use self as the first parameter but naming this parameter "self" is more of a convention. In Python, self is not a reserved keyword, you can actually use any name as the first parameter of a method though that is not advisable.

Conventionally this parameter is called self in Python and that makes the code more readable.

Why self is not passed in method call

You would have observed in the above example that method call doesn’t include self as a parameter even though method definition does include self as parameter.

Method definition has self as parameter-

def display_data(self):

but when the method is called it is not passed-


To understand why method call still works you need to know a little bit about functions and methods in Python.

When you write any def statement with in a class it is a function definition which can be accessed using class name.

A method is a function that belongs to an object.

For example on printing the type of the following statements-

# method call using class name
# method call using object

Output is-

<class 'function'>
<class 'method'>

Coming back to the question how method is called with out an argument. In Python when you call a method using an object, that instance object is passed as the first argument of the function. So a method call person1.display_data() internally becomes Person.display_data(person1) and that person1 reference is assigned to self.

That's all for this topic self in Python. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page

Related Topics

  1. Class And Object in Python
  2. Name Mangling in Python
  3. Namespace And Variable Scope in Python
  4. Global Keyword in Python With Examples
  5. Python String join() Method

You may also like-

  1. Operator Overloading in Python
  2. Multiple Inheritance in Python
  3. Python Generator, Generator Expression, Yield Statement
  4. Ternary Operator in Python
  5. Java Collections Interview Questions And Answers
  6. Why no Multiple Inheritance in Java
  7. Check if Given String or Number is a Palindrome Java Program
  8. Spring Web MVC Tutorial

No comments:

Post a Comment