What does Python init and self do

+3 votes

In a method like:

def method(self, something):
    def __init__(?):
        ....
    ....

What does self do? What is it meant to be?

What does the __init__ method do and why is it necessary?

Oct 31, 2018 in Python by Anirudh
• 2,080 points
5,956 views

2 answers to this question.

+1 vote
Best answer

Hey @Anirudh! 

Self

The self keyword is used to access the attributes and methods of the class. self just represents an instance of the class. Let's see why self is used and how it works.

Let us consider this code:

class example:
    weekend = True
    def isholiday(self):
        if self.weekend:
            print ("Today is a holiday!!")
        else:
            print ("Not holiday")
    def change(self):
    if self.weekend:
        self.weekend = False
    else:
        self.weekend = True

a = example()
a.isholiday()

b = example()
b.change()
b.isholiday()

When an instance is created, weekend is set to True. There are 2 methods. isholiday() checks if weekend is True and if it is True, it prints "Today is a holiday!!" and if it is not True, it prints "Not holiday". And change() changes the value of weekend to False if it is True and to True if it is False. We create 2 instances 'a' and 'b' and run the methods. The output for the above code is:

#Output using self
Today is aholiday!!
Not holiday

Now we modify the above code and write it without using self:

class example:
    weekend = True
    def isholiday(example):
        if example.weekend:
            print ("Today is a holiday!!")
        else:
            print ("Not holiday")
    def change(example):
    if example.weekend:
        example.weekend = False
    else:
        example.weekend = True

a = example()
a.isholiday()

b = example()
b.change()
b.isholiday()

And the output for this code is:

#Output without using self
Today is a holiday!!
Today is a holiday!!

In the first code, the data of only the mentioned instance was changed and the data of other instance was untouched. But in second code, the data of both the instances of class example was changed. So the self is used to access the attributes and method on that instance only. 

__init__

__init__ is a reserved keyword in python used to initialize attributes of the class when an object of that class is created. It is also known as constructor in OOPC

answered Oct 31, 2018 by Omkar
• 69,210 points

selected Oct 31, 2018 by Priyaj
#Output using self
Today is a holiday!!
Today is a holiday!!

Hey! I ran the same code, I am getting the right output. I have attached the screenshot below

0 votes

_init_ is the constructor for a class. The self parameter refers to the instance of the object. The basic idea is that it is a special method which is automatically called when an object of that Class is created. You'll need to pass "self" to any class functions as the first argument if you want them to behave as non-static methods. "self" are instance variables for your class.

answered Oct 31, 2018 by Priyaj
• 58,090 points

Related Questions In Python

0 votes
1 answer

Can you explain what is the use _init_ and self in python?

Hi, The basic difference between these two are _init_ ...READ MORE

answered Aug 14, 2019 in Python by anonymous
1,209 views
0 votes
0 answers

What do __init__ and self do in Python? [duplicate]

During my Python lectures, I've come across a ...READ MORE

Aug 31, 2023 in Python by Edureka
• 220 points
97 views
0 votes
1 answer

What do "init" and "self" do in python ?

In Python, `init` and `self` are related ...READ MORE

answered Oct 5, 2023 in Python by anonymous
• 3,320 points
430 views
+1 vote
7 answers
0 votes
1 answer

What does the return statement do in Python?

The print() function is use to write ...READ MORE

answered Oct 1, 2018 in Python by SDeb
• 13,300 points
1,053 views
+1 vote
1 answer

What does the Raise keyword do in Python?

Hi! I think I can answer this - ...READ MORE

answered Jan 25, 2019 in Python by Nymeria
• 3,560 points
864 views
0 votes
2 answers
–1 vote
2 answers
+1 vote
2 answers

Does 'finally' always execute in Python?

The "finally" executes almost everytime. But there ...READ MORE

answered Aug 31, 2018 in Python by Omkar
• 69,210 points
5,373 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP