-3

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.

  • 2
    it's basically a constructor...that automatically called when you create the object of a class – Anurag Dabas Mar 12 '21 at 16:43
  • 3
    Does this answer your question? [What is \_\_init\_\_.py for?](https://stackoverflow.com/questions/448271/what-is-init-py-for) – shark Mar 12 '21 at 16:43
  • 2
    https://stackoverflow.com/questions/625083/what-init-and-self-do-in-python – Samwise Mar 12 '21 at 16:45
  • 1
    I believe, this is a good question. I know but it already asked. The way he asked shows his interest in python. Hope my answer will help you @sharhan – Sivaram Rasathurai Mar 12 '21 at 16:56
  • @shark no, source files with the special name `__init__.py` have **nothing to do with** methods with the special name `__init__`. – Karl Knechtel Sep 05 '22 at 06:58

2 Answers2

2

I'm not an expert at constructing classes, but when you make a class you want to define what happens when the class is initialized. This way, if you define an object as said class the part under __init__() is executed.

Jeroen Vermunt
  • 672
  • 1
  • 6
  • 19
2

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.

Sivaram Rasathurai
  • 5,533
  • 3
  • 22
  • 45