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 in Python by Piyush
69 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 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 in Python by Waseem
• 4,470 points
59 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 in Python by Mohammad
• 3,060 points
26 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,420 points
1,589 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 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 341 views
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 in Python by Mohammad
• 3,060 points
37 views
0 votes
1 answer