Python Constructor (init)

1. What is a Constructor?

A constructor is a special function in a class.

In Python, the constructor is called __init__().

In simple words:
👉 __init__() runs automatically
👉 It runs when an object is created
👉 It is used to set values for the object

2. Why Do We Use __init__()?

We use __init__() to:

  • Give initial values to object

  • Avoid writing extra code

  • Make class easy to use

3. Simple Constructor Example

Example


 

Output:

Object created

4. Constructor with Parameters

Example


 

5. What is self in Constructor?

self refers to the current object.

In simple words:

 

6. Multiple Objects with Constructor

Example


 

7. Default Values in Constructor

Example


 

8. Constructor in Real Example (Bank)

Example


 

9. Constructor vs Normal Function

Constructor Normal Function
Runs automatically Called manually
Named __init__ Any name
Used for setup Used for work

10. Common Beginner Mistakes

❌ Forgetting self


 

❌ Wrong.

❌ Calling Constructor Manually


 

❌ Not recommended.

11. Simple Practice Examples

Example 1: Book Class


 

Example 2: Rectangle Area


 

Example 3: User Profile


 

12. Summary (Python Constructor)

__init__() is a constructor
✔ Runs automatically
✔ Sets initial values
✔ Uses self
✔ Very important in OOP

📘 Perfect for Beginner eBook

This chapter is ideal for:

  • Python beginner books

  • School & college students

  • Self-learners

If you want next, I can write:

  • Destructor (__del__)

  • Inheritance

  • Encapsulation

  • Polymorphism

Just tell me 😊

You may also like...