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,050 points
52 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
+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
• 66,880 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,140 points

Related Questions In Python

0 votes
2 answers

What do you mean by python scripting? What is a script and a module in python?

A scripting language is a programming language ...READ MORE

answered Mar 14 in Python by rajesh kumar
2,415 views
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,140 points
25 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
• 10,700 points
21 views
0 votes
1 answer

What does hash do in python?

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

answered Nov 14, 2018 in Python by charlie_brown
• 7,710 points
19 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
• 10,700 points
35 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,500 points
44 views
+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
• 66,880 points
232 views
+6 votes
4 answers

Python and Pygame: Difference

Python is a programming language. PyGame is a ...READ MORE

answered Oct 24, 2018 in Python by Priyaj
• 56,140 points
69 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.