Python

P
PythonHub
Progress 0%

Python

  • Home
  • History of Python
  • Applications of Python
  • Introduction To Python
    • What is Python
    • Character Set
    • Tokens in Python
    • Python Execution Mode
    • Variable And Identifiers
    • Data Types in Python
    • Operators And Expressions
    • Constants in Python
    • Assignment Statement
    • Input / Output in Python
    • Simple Python Scripts
    • Namespace in Python
    • 📝 Assignments
  • Operators in Python
    • Arithmetic Operators
    • Assignment Operators
    • Shorthand Assignment Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Special Operators
    • 📝 Assignments
  • Input Output in Python
    • Accept Input
    • Output Formatting
    • 📝 Assignments
  • Conditional Statement
    • Decision Making
    • if Statement
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • Short Hand IF-ELSE
    • 📝 Assignments
  • Loops
    • Introduction to Loops
    • While Loop
    • Nested While Loop
    • 📝 While Loop Assignments
    • For Loop
    • For Loop Examples
    • Nested For Loop
    • Nested For Loop Examples
    • Infinite While Loops
    • Infinite For Loops
    • Break, Continue and Else in Loops
    • Difference Between For and While Loop
    • For Each Loop
    • 📝 For Each Assignments
    • 📝 All Loops Assignments
  • List
    • List in Python
    • Access List Elements
    • List Functions
    • Iterate (Loop) List
    • List Comprehension
    • 📝 Assignments
  • Tuple
    • Tuple in Python
    • Access Tuple Elements
    • Tuple Functions
    • Iterate (Loop) Tuple
    • Unpack Tuple
    • Tuple Comprehension
    • 📝 Assignments
  • Set
    • Set in Python
    • Access Set Elements
    • Set Methods
    • Iterate (Loop) Set
    • Pack/Unpack Set
    • Set Comprehension
    • 📝 Assignments
  • Dictionary
    • Dictionary
    • Access Dictionary Items
    • Dictionary Methods
    • Iterate (Loop) Dictionary
    • Formatting Dictionaries
    • Nested Dictionaries
    • Dictionary Comprehension
    • 📝 Assignments
  • Diff List Tuple Set Dictionary
    • List vs Tuple
    • List vs Set
    • List vs Dictionary
    • Tuple vs Set
    • Tuple vs Dictionary
    • Dictionary vs Set
    • 📝 Assignments
  • Exception
    • Error vs Exception
    • Types of Exception
    • Exception Handling
    • User Defined Exception
    • Logging Exception
    • 📝 Assignments
  • Functions
    • Introduction to Functions
    • Modular Programming
    • Types of Functions
    • Inbuilt Functions
    • Need For User-Defined Function
    • Elements of User Defined Function
    • Function Arguments
    • Nesting of Functions
    • Recursion
    • Global Local and Non Local
    • Python Lambda Functions
    • 📝 Assignments
  • Python Module
    • Introduction to Module
    • Inbuilt Modules in Python
    • User Defined Module
    • 📝 Assignments
  • File Handling
    • Introduction to Files
    • Create File
    • Read Files
    • Write to File
    • Rename File
    • Copy File
    • Move File
    • List Files in Directory
    • Binary Files
    • Zipping and Unzipping Files
    • 📝 Assignments
  • Strings
    • Basics of Strings
    • String Special Operators
    • String Formatting Operators
    • String Methods
    • 📝 Assignments
  • Regular Expressions
  • Python OOPS
    • Basics of Object Oriented
    • What are Classes and Objects?
    • Creating Class and Object
    • OOP vs Procedural Programming
    • Difference Between Classes and Objects
    • Constructors
    • Destructor
    • Built Class Methods and Attributes
    • Class and Instance Variables
    • Inheritance in Python
    • Single Inheritance
    • Multiple Inheritance
    • Multilevel Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance
    • Abstraction
    • Method Overriding
    • Abstract Method
    • Interfaces in Python
    • Abstract Class vs Interface
    • Public, Private and Protected
    • Overloading vs Overriding
    • Inheritance vs Composition
    • Encapsulation
    • Polymorphism
    • Inner Classes
    • 📝 Assignments
  • Advanced Python
    • Iterator in Python
    • Generator in Python
    • Decorator in Python
    • Type Hints in Python
    • Async/Await Programming
    • Dataclasses in Python
    • Context Managers in Python
    • Property Decorator in Python
    • __slots__ in Python
    • Enums in Python
    • Walrus Operator in Python
    • Match-Case in Python
    • 📝 Assignments
  • Python Standard Library
    • Collections Module
    • Itertools Module
    • Functools Module
    • Datetime Module
    • JSON Module
    • OS Module
    • Sys Module
    • Random Module
    • Math Module
    • 📝 Assignments
  • Python Testing
    • Unit Testing in Python
    • Pytest Framework
    • Mocking in Python
    • 📝 Assignments
  • Python Best Practices
    • PEP 8 Style Guide
    • Docstrings in Python
    • Logging in Python
    • Code Optimization Tips
    • Debugging Techniques
    • 📝 Assignments
  • MySQL Database in Python
    • Introduction to MySQL with Python
    • DBMS vs File System
    • Connecting to MySQL Database
    • Create Database in MySQL
    • Create Table in MySQL
    • Insert Data in MySQL
    • Insert Multiple Rows
    • Select Data from MySQL
    • WHERE Clause in MySQL
    • Update Data in MySQL
    • Delete Data from MySQL
    • Parameterized Queries
    • Transaction Management
    • Error Handling
    • Connection Pooling
    • MySQL Drivers Guide
    • Joins in MySQL
    • Aggregation Functions
    • Backup MySQL Database
    • Best Practices
    • 📝 Assignments
  • MySQL Database Operations
    • SELECT Statement
    • MySQL Operators
    • DDL Statements
    • DML Statements
    • Subqueries
    • JOIN Operations
    • Aggregation
    • Case Study
    • 📝 Assignments
  • Graphics in Python
  • Threads in Python
    • Introduction to Threads
    • Process vs Threads
    • Concurrent Programming & GIL
    • Uses of Threads
    • Creating Threads
    • Single Tasking
    • Multi Tasking
    • Thread Synchronization
    • 📝 Assignments
  • Interview Questions & Answers
  • Python Case Studies
  • Multiple Choice Questions
  • 📝 Practice Problems
Get in Touch
  • tech2dsm@gmail.com

© Sankalan Data Tech

Python Language Interactive Tutorial

Python: Constructors

Python Constructors - Complete Guide

Learn about constructors in Python — the special methods that initialize objects when they're created.

Created by Sankalan Data Tech Team Verified
Data Engineers, Analysts, Scientists & Trainers
Created by experienced Python developers, data engineers, and data scientists to make programming easy through practical examples, real-world experience, and clear explanations.
On this page:
  • What is a Constructor?
  • The __init__ Method
  • Default Constructor
  • Parameterized Constructor
  • Multiple Constructors
  • __init__ vs __new__
  • Best Practices
  • Try It Yourself
  • Quick Quiz
  • Frequently Asked Questions
Share this tutorial:
Twitter LinkedIn Facebook WhatsApp Reddit Telegram Email Copy Link
What You'll Learn Here
  • What is a constructor — the method that initializes objects
  • The __init__ method — Python's primary constructor
  • Default constructor — when you don't define __init__
  • Parameterized constructor — passing arguments to initialize objects
  • Multiple constructors — how to handle different initialization needs
  • Best practices — writing clean constructors

What is a Constructor?

A constructor is a special method that is automatically called when you create a new object. Its job is to initialize the object — to set up its initial state by assigning values to its attributes. Think of it as the "welcome" that every object gets when it's born.

In Python, the constructor is the __init__ method. It's what makes sure every new object starts with the right data. Without a constructor, you'd have to manually set up every object after creating it, which would be tedious and error-prone.

💡 Key concept: A constructor is a special method that initializes an object when it's created. It sets up the object's initial state and ensures it's ready to use.

The __init__ Method

1

Understanding the __init__ Method

The __init__ method is Python's constructor. It's called automatically when you create an object. Its purpose is to initialize the object's attributes and prepare it for use.

Definition: The __init__ method is a special method in Python classes that is called when an object is instantiated. It's used to initialize the object's attributes and set up its initial state.

# The __init__ method in action

class Book:
    def __init__(self, title, author, pages):
        """Initialize a new Book object"""
        print(f"Creating book: {title}")
        self.title = title
        self.author = author
        self.pages = pages
        self.is_open = False
    
    def open(self):
        self.is_open = True
        return f"Opening '{self.title}'"
    
    def close(self):
        self.is_open = False
        return f"Closing '{self.title}'"
    
    def get_info(self):
        status = "open" if self.is_open else "closed"
        return f"'{self.title}' by {self.author} - {self.pages} pages ({status})"

# The __init__ method is called when we create objects
book1 = Book("Python Guide", "John Smith", 350)
# Output: Creating book: Python Guide

book2 = Book("Data Science", "Jane Doe", 420)
# Output: Creating book: Data Science

# Each object is properly initialized
print(book1.get_info())  # 'Python Guide' by John Smith - 350 pages (closed)
print(book2.get_info())  # 'Data Science' by Jane Doe - 420 pages (closed)

# The __init__ method has set up each object correctly
book1.open()
print(book1.get_info())  # 'Python Guide' by John Smith - 350 pages (open)
                                

Key points about __init__:

  • Called automatically — you don't need to call it manually
  • First parameter is self — refers to the new object
  • Sets up attributes — assigns initial values to instance variables
  • Can have parameters — accepts values to customize the object
  • Can have default values — makes parameters optional
  • No return needed — returns the object automatically

What __init__ does:

  • Creates attributes — adds instance variables to the object
  • Assigns values — sets initial values for those attributes
  • Sets up state — prepares the object for use
  • Can include logic — can validate or transform input

Quick Check: What is the purpose of the __init__ method? (Answer: To initialize an object when it's created)

Default Constructor

2

When You Don't Define __init__

If you don't define an __init__ method in your class, Python provides a default constructor. This constructor does nothing except create the object. It doesn't set any attributes, so you'll need to add them manually after creation.

# Default constructor (no __init__ defined)

class EmptyClass:
    """A class without an __init__ method"""
    pass

# Python provides a default constructor
obj1 = EmptyClass()
obj2 = EmptyClass()

# The objects are created, but they have no attributes
print(obj1)  # <__main__.EmptyClass object at 0x...>
print(obj2)  # <__main__.EmptyClass object at 0x...>

# You can add attributes manually
obj1.name = "Object 1"
obj2.name = "Object 2"

print(obj1.name)  # Object 1
print(obj2.name)  # Object 2

# This works, but it's not ideal for most cases.
# It's better to define an __init__ method to set up attributes.

# Example with a class that only needs a default constructor
class Person:
    """A simple class using default constructor"""
    pass

# Create person objects
person1 = Person()
person2 = Person()

# Add attributes manually
person1.name = "Alice"
person1.age = 25
person2.name = "Bob"
person2.age = 30

print(f"{person1.name} is {person1.age} years old")
print(f"{person2.name} is {person2.age} years old")
                                

Default constructor key points:

  • Provided automatically — you don't need to write it
  • Does nothing — just creates the object
  • No attributes — doesn't set any instance variables
  • Useful for simple cases — when you don't need to initialize
  • Manual setup required — you'll need to add attributes later

Quick Check: What happens if you don't define __init__? (Answer: Python provides a default constructor that does nothing)

Parameterized Constructor

3

Passing Arguments to Initialize Objects

A parameterized constructor is an __init__ method that takes parameters. This allows you to pass values when creating an object, setting up its attributes with specific data.

# Parameterized constructors

class Employee:
    """An employee with a parameterized constructor"""
    
    def __init__(self, name, position, salary):
        """Initialize employee with provided values"""
        self.name = name
        self.position = position
        self.salary = salary
        self.years_at_company = 0
        
        # We can also perform validation
        if salary < 0:
            self.salary = 0
            print("Warning: Salary cannot be negative")
    
    def get_info(self):
        return f"{self.name} - {self.position}, Salary: ${self.salary}"

# Creating objects with different values
alice = Employee("Alice", "Developer", 75000)
bob = Employee("Bob", "Designer", 65000)
charlie = Employee("Charlie", "Manager", 85000)

# Each object has its own data
print(alice.get_info())  # Alice - Developer, Salary: $75000
print(bob.get_info())    # Bob - Designer, Salary: $65000
print(charlie.get_info()) # Charlie - Manager, Salary: $85000

# Example with default values
class Product:
    def __init__(self, name, price, category="General", in_stock=True):
        self.name = name
        self.price = price
        self.category = category
        self.in_stock = in_stock
    
    def get_info(self):
        status = "In Stock" if self.in_stock else "Out of Stock"
        return f"{self.name} (${self.price}) - {self.category} - {status}"

# Using default values
laptop = Product("Laptop", 999.99, "Electronics")
phone = Product("Phone", 599.99, category="Electronics", in_stock=False)
book = Product("Book", 19.99)

print(laptop.get_info())  # Laptop ($999.99) - Electronics - In Stock
print(phone.get_info())   # Phone ($599.99) - Electronics - Out of Stock
print(book.get_info())    # Book ($19.99) - General - In Stock
                                

Parameterized constructor key points:

  • Accepts arguments — values are passed when creating the object
  • Initializes with data — sets attributes based on parameters
  • Default values — can make some parameters optional
  • Validation — can check and correct input
  • Flexibility — allows different objects to have different initial states

Quick Check: What is a parameterized constructor? (Answer: An __init__ method that takes parameters to initialize objects)

Multiple Constructors

4

Handling Different Initialization Needs

Python doesn't support multiple __init__ methods directly (like some other languages do). However, there are several ways to achieve similar flexibility.

# Multiple ways to initialize objects

# Method 1: Using default parameters
class Rectangle:
    def __init__(self, width=0, height=0):
        self.width = width
        self.height = height
        self.area = width * height
    
    def get_info(self):
        return f"Rectangle: {self.width}x{self.height}, Area: {self.area}"

# Different ways to create rectangles
rect1 = Rectangle()               # Default: 0x0
rect2 = Rectangle(5)              # 5x0
rect3 = Rectangle(5, 3)           # 5x3

print(rect1.get_info())  # Rectangle: 0x0, Area: 0
print(rect2.get_info())  # Rectangle: 5x0, Area: 0
print(rect3.get_info())  # Rectangle: 5x3, Area: 15

# Method 2: Using class methods as alternative constructors
class Person:
    def __init__(self, name, age, city):
        self.name = name
        self.age = age
        self.city = city
    
    def get_info(self):
        return f"{self.name} ({self.age}) from {self.city}"
    
    # Alternative constructor from a dictionary
    @classmethod
    def from_dict(cls, data):
        return cls(data.get('name', 'Unknown'), 
                  data.get('age', 0), 
                  data.get('city', 'Unknown'))
    
    # Alternative constructor from a string
    @classmethod
    def from_string(cls, data_string):
        parts = data_string.split(',')
        name = parts[0].strip()
        age = int(parts[1].strip())
        city = parts[2].strip()
        return cls(name, age, city)

# Using different constructors
person1 = Person("Alice", 25, "NYC")
person2 = Person.from_dict({"name": "Bob", "age": 30, "city": "LA"})
person3 = Person.from_string("Charlie, 35, Chicago")

print(person1.get_info())  # Alice (25) from NYC
print(person2.get_info())  # Bob (30) from LA
print(person3.get_info())  # Charlie (35) from Chicago

# Method 3: Using *args and **kwargs
class FlexiblePerson:
    def __init__(self, *args, **kwargs):
        if len(args) == 3:
            # Positional arguments: name, age, city
            self.name, self.age, self.city = args
        elif len(args) == 2:
            # Positional arguments: name, age
            self.name, self.age = args
            self.city = "Unknown"
        else:
            # Keyword arguments
            self.name = kwargs.get('name', 'Unknown')
            self.age = kwargs.get('age', 0)
            self.city = kwargs.get('city', 'Unknown')
    
    def get_info(self):
        return f"{self.name} ({self.age}) from {self.city}"

# Different ways to create
fp1 = FlexiblePerson("Alice", 25, "NYC")
fp2 = FlexiblePerson("Bob", 30)
fp3 = FlexiblePerson(name="Charlie", city="Chicago")

print(fp1.get_info())  # Alice (25) from NYC
print(fp2.get_info())  # Bob (30) from Unknown
print(fp3.get_info())  # Charlie (0) from Chicago
                                

Multiple constructor techniques:

  • Default parameters — make arguments optional
  • Class methods — create alternative constructors with @classmethod
  • *args and **kwargs — handle variable arguments
  • Factory functions — separate functions that create objects

Quick Check: Can Python have multiple __init__ methods? (Answer: No, but you can use class methods as alternative constructors)

__init__ vs __new__

5

Understanding the Difference

In Python, there's another special method called __new__ that's sometimes confused with __init__. They serve different purposes: __new__ creates the object, and __init__ initializes it.

# __new__ vs __init__

class Person:
    def __new__(cls, name, age):
        """__new__ creates the object"""
        print(f"__new__ called: Creating a Person object for {name}")
        # Create the object
        instance = super().__new__(cls)
        return instance
    
    def __init__(self, name, age):
        """__init__ initializes the object"""
        print(f"__init__ called: Initializing {name}")
        self.name = name
        self.age = age
    
    def get_info(self):
        return f"{self.name} is {self.age} years old"

# Creating a Person object
print("Creating a Person...")
person = Person("Alice", 25)
print(person.get_info())

# Output:
# Creating a Person...
# __new__ called: Creating a Person object for Alice
# __init__ called: Initializing Alice
# Alice is 25 years old

# When to use __new__:
# - When you need to control object creation (singleton pattern)
# - When you're subclassing immutable types (like tuple or int)
# - When you need to return an existing object instead of a new one

# Example: Singleton pattern with __new__
class Singleton:
    _instance = None
    
    def __new__(cls):
        if cls._instance is None:
            print("Creating the singleton instance")
            cls._instance = super().__new__(cls)
        else:
            print("Returning existing singleton instance")
        return cls._instance
    
    def __init__(self):
        print("Initializing singleton")
        self.value = 0

# Testing the singleton
s1 = Singleton()  # Creating the singleton instance / Initializing singleton
s2 = Singleton()  # Returning existing singleton instance / Initializing singleton
s3 = Singleton()  # Returning existing singleton instance / Initializing singleton

print(s1 is s2)  # True
print(s2 is s3)  # True
                                

__init__ vs __new__ key differences:

  • __new__ — creates the object (called first)
  • __init__ — initializes the object (called second)
  • __new__ is a class method — receives cls as first parameter
  • __init__ is an instance method — receives self as first parameter
  • __new__ returns the object — __init__ returns nothing

Quick Check: What is the difference between __new__ and __init__? (Answer: __new__ creates the object; __init__ initializes it)

Best Practices for Constructors

6

Writing Clean Constructors

# Best practices for writing constructors

# 1. Keep it simple
# ✅ Good: Simple constructor with clear purpose
class User:
    def __init__(self, username, email):
        self.username = username
        self.email = email
        self.is_active = True

# ❌ Bad: Constructor doing too much
class UserBad:
    def __init__(self, username, email, data=None):
        self.username = username
        self.email = email
        self.is_active = True
        # Too much logic in constructor
        if data:
            self.process_data(data)
            self.validate_data()
            self.save_to_database()
            self.send_welcome_email()

# 2. Validate input when needed
class Product:
    def __init__(self, name, price):
        self.name = name.strip() if name else "Unknown"
        
        # Validate price
        if price < 0:
            raise ValueError("Price cannot be negative")
        self.price = price

# 3. Use default values wisely
class Book:
    def __init__(self, title, author, pages=0, isbn=None):
        self.title = title
        self.author = author
        self.pages = pages
        self.isbn = isbn if isbn else "Unknown"

# 4. Document your constructor
class Customer:
    """
    A class representing a customer.
    
    Attributes:
        name (str): Customer's full name
        email (str): Customer's email address
        tier (str): Membership tier (gold, silver, bronze)
    """
    def __init__(self, name, email, tier="bronze"):
        """
        Initialize a new customer.
        
        Args:
            name (str): Customer's full name
            email (str): Customer's email address
            tier (str): Membership tier (default: bronze)
        """
        self.name = name
        self.email = email
        self.tier = tier.lower()

# 5. Use type hints for clarity
class Employee:
    def __init__(self, name: str, position: str, salary: float) -> None:
        self.name = name
        self.position = position
        self.salary = salary

# 6. Keep constructor focused on initialization
class Order:
    def __init__(self, customer, items):
        self.customer = customer
        self.items = items
        self.total = sum(item.price for item in items)
        self.status = "pending"
    
    # Complex logic goes in other methods
    def process(self):
        self.status = "processing"
        # Processing logic...
    
    def complete(self):
        self.status = "completed"
        # Completion logic...
                                

Constructor best practices:

  • Keep it simple — constructor should only initialize attributes
  • Validate input — check for invalid values
  • Use defaults wisely — make optional parameters clear
  • Document your constructor — explain parameters and purpose
  • Use type hints — make the expected types clear
  • Avoid complex logic — leave business logic for other methods

Quick Check: What is the main rule for constructors? (Answer: Keep them focused on initialization, not complex logic)

Try It Yourself

Experiment with constructors in the editor below.

Loading Pyodide... 0%
Python Code Editor
========================================
CONSTRUCTORS PRACTICE
========================================

1. DEFAULT CONSTRUCTOR
Object created: <__main__.SimpleClass object at 0x...>

2. PARAMETERIZED CONSTRUCTOR
Alice (Grade 10) at Python Academy
Bob (Grade 9) at Data Science School

3. CONSTRUCTOR WITH VALIDATION
Warning: Balance cannot be negative. Setting to 0.
Account A001: $1000
Account A002: $0

4. ALTERNATIVE CONSTRUCTOR
Charlie is 35 years old

Constructors practice complete!
🏆

You've Got It!

You now understand constructors in Python. You know about __init__, default and parameterized constructors, and best practices for initializing objects.

Quick Quiz

Test what you've learned:

1. What is the purpose of the __init__ method?
2. What is a default constructor?
3. What is the difference between __new__ and __init__?
4. How can you have multiple constructors in Python?
5. What is a best practice for constructors?

Frequently Asked Questions

What is the difference between a constructor and a method? ▼

A constructor (__init__) is a special method that's called automatically when an object is created. Its purpose is to initialize the object. A regular method is called manually on an existing object to perform specific tasks. The constructor sets up the object; methods do things with it.

Can I call the constructor manually? ▼

Yes, you can call __init__ manually, but it's rarely needed. It's usually called automatically when you create an object. If you call it manually, you need to pass an existing object as the first parameter (self).

What's a common interview question about constructors? ▼

Common questions include: "What is the purpose of __init__ in Python?" "What is the difference between __new__ and __init__?" and "How do you implement multiple constructors in Python?"

Can I have multiple __init__ methods in one class? ▼

No, Python doesn't support method overloading in the same way as Java or C++. You can only have one __init__ method. However, you can use default parameters, class methods, or *args/**kwargs to achieve similar flexibility.

Should I always define __init__ for my classes? ▼

Not always. If your class doesn't need to initialize any attributes, you can skip it. Python will provide a default constructor. However, for most classes that hold data, it's a good practice to define __init__ to ensure proper initialization.

What happens if I don't define __init__? ▼

If you don't define __init__, Python provides a default constructor that does nothing except create the object. The object will be created, but it won't have any attributes set automatically. You'll need to add them manually after creation.

Where to Go From Here

Now that you understand constructors, check out these related topics:

Destructor

Learn about destructors and object cleanup.

Learn More →

Built Class Methods and Attributes

Learn about special methods and attributes in Python classes.

Learn More →

Class and Instance Variables

Learn the difference between class and instance variables.

Learn More →
Interview Resources
  • Python Syntax & Variables Interview Questions
  • Top SQL Interview Questions & Answers
  • SQL Joins: Displaying Data from Multiple Tables FAQ
  • Python Lists and Dictionaries Interview Questions
  • Python OOP Interview Questions
  • SQL Set Operators Interview Questions
Previous: Difference Between Classes and Objects Next: Destructor →