Code Explanation:
1. Defining the class
class A:
This line defines a class named A.
2. Overriding __new__
def __new__(cls):
return object()
__new__ is responsible for creating and returning a new object.
Instead of creating an instance of A, it returns a plain object instance.
This means the returned object is not of type A.
3. Defining __init__
def __init__(self):
print("init")
__init__ initializes an object only if the object returned by __new__
is an instance of the class (A).
Since __new__ returned a plain object, __init__ will not be called.
4. Creating an object
print(A())
A() calls __new__ first.
__new__ returns a plain object.
Python checks whether the returned object is an instance of A.
It is not, so __init__ is skipped.
The print statement prints the default representation of a plain object.
✅ Final Output
<object object at 0x...>

0 Comments:
Post a Comment