Python class init method is a reserved method in python classes. It represents a constructor in Python. When an object creates it is called and it allows the class to initialize the attributes of a class.
As you know if you know another object-oriented programming constructors are used to initialize the object’s state. It is initialized (assigns values) to the data members of the class when an object of the class is created.
Example __init__ in Python
Simple example code class with the init method in Python.
class Person:
# init method or constructor
def __init__(self, name):
self.name = name
# Sample Method
def greeting(self):
print('Hello,', self.name)
p = Person('John')
p.greeting()
Output:
Do comment if you have any doubts or suggestions on this Python basic tutorial.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Yo yo yo, what is up!