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
1,213 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
1 answer

what is the use of // operator in Python? Can anyone explain?

Hi, @Roshni, It is a Floor Divisionoperator, which ...READ MORE

answered Jun 23, 2020 in Python by Gitika
• 65,910 points
1,878 views
0 votes
0 answers

what is the use of args and kwargs in python?

can you an example for each? READ MORE

May 11, 2019 in Python by Waseem
• 4,540 points
582 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,230 points
701 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,023 views
0 votes
1 answer
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 21, 2019 in Python by Mohammad
• 3,230 points
1,117 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 21, 2019 in Python by Mohammad
• 3,230 points
1,817 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