A constructor is a special method in a class that gets called automatically when a new object is created. In Python, the constructor method is __init__().
The __init__() method is used to initialize an object’s attributes when it is created.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hello, my name is {self.name} and I am {self.age} years old.")
# Creating an object
person1 = Person("Ankit", 25)
person1.greet() # Output: Hello, my name is Ankit and I am 25 years old.person1 = Person("Ankit", 25) is executed:__init__() is called automatically."Ankit" and 25 are assigned to self.name and self.age.person1 is now initialized with these values.Python supports three types of constructors:
1 . Default Constructor (No Parameters except self)
self.class Example:
def __init__(self):
print("Default constructor called")
obj = Example() # Output: Default constructor called2. Parameterized Constructor
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
car1 = Car("Tesla", "Model S")
print(car1.brand, car1.model) # Output: Tesla Model S3. Constructor with Default Values
class Laptop:
def __init__(self, brand="Dell"):
self.brand = brand
l1 = Laptop()
l2 = Laptop("HP")
print(l1.brand) # Output: Dell
print(l2.brand) # Output: HPA destructor is a special method that is called when an object is deleted or goes out of scope. In Python, the destructor method is __del__().
class Student:
def __init__(self, name):
self.name = name
print(f"Student {self.name} created!")
def __del__(self):
print(f"Student {self.name} is being deleted.")
# Creating an object
s1 = Student("Vivek")
# Deleting the object manually
del s1 # Output: Student Vivek is being deleted.del, the destructor is executed immediately.def create_student():
s2 = Student("Akash")
print("Inside function")
create_student()
print("Outside function")
# Output:
# Student Akash created!
# Inside function
# Student Emma is being deleted.
# Outside functionThe constructor (__init__()) is called when an object is created.
Types of constructors: Default, Parameterized, and with Default Values.
The destructor (__del__()) is called when an object is deleted or goes out of scope.
Sign in to join the discussion and post comments.
Sign inPython for Web Development
Python for Web Development is a comprehensive tutorial series covering the fundamentals of building web applications using Flask and Django. From setting up a project to working with databases, authentication, REST APIs, and deployment on cloud platforms, this series provides a solid foundation for developing secure and scalable web applications.
Python Basics
Python is a powerful, high-level programming language known for its simplicity and versatility. It is widely used in various fields, including web development, data science, artificial intelligence, automation, and more. This tutorial series is designed to take you from the basics of Python to more advanced topics, ensuring a strong foundation in programming.