Constructor
Introduction to OOP's
Class
Object
Constructor
Instance Variables
Class Variables
Instance Methods
self Keyword
Encapsulation
Public Members
Protected Members
Private Members
Inheritance
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
super() Function
Polymorphism
Method Overriding
Duck Typing
Abstraction
Abstract Class
Abstract Method
Class Method
Static Method
Special Methods
_str_ Method
Operator Overloading
Composition
Aggregation
Association
Method Resolution Order
Object Class
issubclass() and isinstance()
Property Decorator
Setter Method with @property
Destructor
Inner Class
Complete Mini Example
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
Executing...
❌ Error:
✅ Output:
// Click Run ▶ to execute
🏋️ Test Yourself With Exercises
Take our quiz on Constructor to test your knowledge.
Browse Quizzes »