Search code examples
pythonpython-class

what is ` __init__()` in a python class


I'm a novice in python programming. I've been jumping from one resource to another to really grasp a low-level understanding of what __init__() is which normally appears at the beginning of a python class. When do we use it? What's a practical implementation of it.


Solution

  • If you know any other language than python like java or c then init is the same as the constructor.

    Constructors are used to initializing the object’s state. The task of constructors is to initialize(assign values) to the data members of the class when an object of the class is created. Which are inside the constructor those will be executed in Object creation. It is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object.

    Please carefully read this article to fully understand the init()

    In addition to this article, If you have time please walk through these answers you will get a clear cut idea about constructor in python.