-2

What does init in python language

I was learning the python language and I am facing difficulty to understand init it. I tried to search about this on google but I am not cleared about this.

  • What is not clear about the documentation: https://docs.python.org/3/reference/datamodel.html#specialnames:~:text=object.__init__(self%5B%2C%20...%5D) ? Can you provide an example with an unclear output? – Luuk Dec 07 '22 at 13:08

2 Answers2

0

the init function is called everytime an object is created from a class. The init method lets the class initialize the object's attributes and serves no other purpose

0

In python, __ init __ is the keyword for creating a constructor of a class.

For example,

class Person:
        
        def __init__(self, name):
            self.name = name
        
        def introduce(self):
            print(f"Hi, I am {self.name}")

You can also create a file named as __ init __.py in a directory of python scripts/files in order to make it a python module.

  • `__init__` is not a *keyword*, a method named thusly also isn't a *constructor*. These words have defined meanings, and not the meaning you ascribe. – deceze Dec 07 '22 at 13:16