Code Explanation:
1. Defining the Class
class Mask:
A class named Mask is defined.
2. Defining a Class Attribute
y = 10
y is a class attribute.
Normally, accessing m.y would return 10.
๐ 3. Overriding __getattribute__
def __getattribute__(self, name):
__getattribute__ is a special method.
It is called for every attribute access, without exception.
This includes access to:
instance attributes
class attributes
methods
even special attributes
4. Checking the Attribute Name
if name == "y":
return 50
If the requested attribute name is "y":
The method immediately returns 50.
Python does not continue normal attribute lookup.
5. Delegating Other Attributes Safely
return super().__getattribute__(name)
For all attributes except y:
Python calls the original object.__getattribute__.
This avoids infinite recursion.
This ensures normal behavior for other attributes.
6. Creating an Instance
m = Mask()
m = Mask()
An object m of class Mask is created.
7. Accessing m.y
print(m.y)
Step-by-step:
Python calls:
Mask.__getattribute__(m, "y")
The condition name == "y" is True.
The method returns 50.
The class attribute y = 10 is never accessed.
8. Final Output
50
50
✅ Final Answer
✔ Output:
50

0 Comments:
Post a Comment