What this code is trying to do
-
Define a class A
-
Create an object obj
-
Print the object
The Problem in the Code
__init__() is a constructor.
Its job is to initialize the object, not return a value.
๐ Rule:
__init__() must always return None
What happens internally
-
Python creates a new object of class A
-
Python calls __init__(self)
-
Your __init__() returns 10
-
Python checks the return value
-
❌ Python raises an error because returning anything from __init__() is not allowed
❌ Actual Output (Error)
TypeError: __init__() should return None, not 'int'⚠️ Because of this error, print(obj) never executes.
✅ Correct Version
Output:
10Key Exam / Interview Point
-
__init__()
✔ Used for initialization
❌ Cannot return values -
Returning anything → TypeError


0 Comments:
Post a Comment