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

0 votes

Hi, 

I used this code so many times

def method(self, abc):
    def __init__():
        ....
    ....

But I don't have the clear idea about _init_ and self.

Can You please explain that?

Thank you,

Aug 14, 2019 in Python by Piyush
174 views

1 answer to this question.

0 votes

Hi,

The basic difference between these two are

  1. _init_ is the constructor for a class.
  2. The self parameter refers to the instance of the object 
  3. The _init_ the method gets called when memory for the object is allocated
  4. It is important to use the self the parameter inside an object's method if you want to persist the value with the object.

class ABC:

     def _init_(self, x , y):

        self._x = x

        self._y = y

     

I hope this will help to overcome your doubts.
Thanks

answered Aug 14, 2019 by anonymous

Related Questions In Python

0 votes
0 answers

what is the use of args and kwargs in python?

can you an example for each? READ MORE

May 10, 2019 in Python by Waseem
• 4,530 points
113 views
0 votes
1 answer

What is the key difference between self in ruby and self in python?

Ruby and Python are actually very different languages (although ...READ MORE

answered Jul 30, 2019 in Python by Mohammad
• 3,210 points
65 views
+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
2,070 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 781 views
0 votes
0 answers
0 votes
1 answer

How can I use the power function in python 2.7?

print(pow(3,4)) #this will return the exponentiation of 3 ...READ MORE

answered May 20, 2019 in Python by Mohammad
• 3,210 points
80 views
0 votes
1 answer

What are the naming conventions for variables and data types in python?

There are certain rules that we have ...READ MORE

answered May 20, 2019 in Python by Mohammad
• 3,210 points
100 views