Home Tutorials Python OOP Concepts Constructor
Constructor

Constructor


3. Constructor

A constructor in Python is usually the __init__() method, which runs automatically when an object is created. It is commonly used to initialize object attributes.

Syntax

class ClassName:
    def __init__(self, value):
        self.value = value

Example

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

emp = Employee("Anil", 45000)
print(emp.name)
print(emp.salary)

Output

Anil
45000
Example

🏋️ Test Yourself With Exercises

Take our quiz on Constructor to test your knowledge.

Browse Quizzes »