Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

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?

1 Answer

0 votes
by (25.1k points)

In python the self keyword is used to refer to the current object. If you have used other programming languages like java and c++, it is very similar to the 'this' keyword in other languages.

The init method is called the constructor, it is run whenever an Object of the current class is instantiated. It is used to set properties on an object when instantiating (creating) an object.

The self keyword allows us to refer to the properties of current object, to give you an example: 

class Person:

    def __init__(self, name):

        self.name = name

david = Person("David")

print(david.name)

Here's a video to help you get a more in-depth understanding about python and oop:

Related questions

0 votes
1 answer
asked Jun 27, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Feb 11, 2021 in Python by Rekha (2.2k points)
0 votes
1 answer
asked Nov 25, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...