Code Explanation:
1. Defining the Class
class Demo:
This line defines a class named Demo
A class can contain variables and methods
2. First __init__ Method
def __init__(self):
print("Init")
__init__ is a constructor
It runs automatically when an object is created
This constructor prints "Init"
At this point, it exists only temporarily
3. Second __init__ Method
def __init__(self):
print("Constructor")
This is another constructor with the same name
In Python:
The last defined method with the same name overwrites the previous one
So, this constructor replaces the first __init__
4. Object Creation
obj = Demo()
An object obj of class Demo is created
Python calls the latest __init__ method
The first __init__ is ignored
5. Output
Constructor
Final Answer
Constructor


0 Comments:
Post a Comment