Programming Languages

What is the purpose of the __init__ method in Python?

Easy
4
Added
The __init__ method is a special method in Python classes. It is called when an object is created from the class and allows the class to initialize the attributes of the class.

Solution Code

Programming Languages
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

# Example usage
person = Person("John", 30)
print(person.name)
print(person.age)
Explanation
This code snippet demonstrates how to use the __init__ method to initialize class attributes.

Guided Hints

Define a class with an __init__ method
Initialize attributes in the __init__ method