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,070 points
248 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
• 67,660 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
• 56,900 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 in Python by anonymous
64 views
+1 vote
3 answers
0 votes
1 answer

What does eval() in Python do?

The eval function lets a Python program ...READ MORE

answered Aug 24, 2018 in Python by Priyaj
• 56,900 points
55 views
0 votes
1 answer

What does hash do in python?

A hash is an fixed sized integer ...READ MORE

answered Oct 15, 2018 in Python by SDeb
• 13,180 points
37 views
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,180 points
104 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 in Python by Nymeria
• 3,520 points
75 views
0 votes
1 answer
+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
• 67,660 points
606 views