Google News
logo
Python Program to The __init__() Function
The `__init__()` function is a constructor method in Python classes. It is used to initialize the attributes of an object when it is created.

Here's an example Python program that demonstrates the use of the `__init__()` function :
Program :
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

person1 = Person("Alice", 25)
person2 = Person("Bob", 30)

print(person1.name)
print(person1.age)

print(person2.name)
print(person2.age)
Output :
Alice
25
Bob
30
In this example, we have defined a `Person` class with two attributes : `name` and `age`. The `__init__()` function is called when we create a new object of the `Person` class.

The `self` parameter refers to the object being created, and we use it to set the values of the `name` and `age` attributes. We create two objects of the `Person` class (`person1` and `person2`) and print their `name` and `age` attributes.