Python self

Filed Under: Python

In this lesson, we will study about the usage of Python self. It is often a point of debate among experts and a topic of confusion for beginners. Let’s try to come clean for everyone.

Now, this post is not meant to be an Introduction to Python but expects a very little understanding of Python syntax. For more posts on Python, see here.

Python self

Python is not a language which was made for Object-Oriented Programming paradigm in mind. So, it is not direct to create a static method in Python. Let’s see how this is done:


class Person:

    def static_method():
        print("This is a static method")

Now, to make a method which can operate on a real ‘Person’ object we need to provide it with a reference to the object. So, instead of passing the complete object of Person to its own class, we can use the self as:


class Person:
        def object_method(self):
        print("I can do something with self.")

python class self, python self variable

Next, let us look at how it can be used to access fields in the class constructor itself.

Python class self constructor

Python self can also be used to refer to a variable field within the class:


class Person:

    # name made in constructor
    def __init__(self, n):
        self.name = n

    def get_person_name(self):
        return self.name

In above snippet, self refers to the name variable of the entire Person class. Note that if we have a variable within a method, self will not work. That variable is simply existent only while that method is running and hence, is local to that method. For defining global fields (the variables of the complete class), we need to define them OUTSIDE the class methods. Refer python variable scope.

python self

Is self a keyword?

self is used in so many places in Python that people think it’s a keyword. But unlike this in C++, self is not a keyword.

We can actually use any name for the first parameter of a method, but it is strongly advised to stick to the convention of calling it self.

This means that the last class can be made as:


class Person:

    #name made in constructor
    def __init__(another, n):
        another.name = n;

    def get_person_name(another):
        return another.name

See how I used another this time? It works exactly the same way as self.

Should we pass self to a method?

Above explanation opens a new question, should we just pass self to a method? Let’s consider the class Person which contains a method something defined as:


def something(another, arg1, arg2):
    pass

If personMe is an instance of the class and personMe.something(arg1, arg2) is called, python internally converts it for us as:


Person.something(personMe, arg1, arg2)

The self variable refers to the object itself.

That’s all for python self and it’s usage in constructor and functions to get the current object reference.

For more Python posts, read here.

Comments

  1. Helgurd Mirza says:

    I thought you put something special about self here but was all the same as the others.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages