Code Explanation:
๐น 1. Class Definition
class Test:
✅ Explanation:
A class Test is created.
It overrides a powerful magic method: __getattribute__.
๐น 2. Overriding __getattribute__
def __getattribute__(self, name):
return self.x
✅ Explanation:
__getattribute__ is called for EVERY attribute access.
No matter what attribute you try to access (x, y, anything), this method runs.
๐ Important Behavior:
When you do:
obj.x
Python internally does:
obj.__getattribute__("x")
๐น 3. Object Creation
obj = Test()
✅ Explanation:
An object obj of class Test is created.
No attributes are defined yet.
๐น 4. Accessing obj.x
print(obj.x)
๐จ What happens step-by-step:
Step 1:
obj.x
→ calls:
__getattribute__(self, "x")
Step 2:
Inside method:
return self.x
BUT ⚠️
self.x again triggers:
__getattribute__(self, "x")
Step 3: Loop Starts ๐
This keeps happening:
__getattribute__ → self.x → __getattribute__ → self.x → ...
๐ Infinite recursion
๐น 5. Final Result
❌ Python stops execution with:
RecursionError: maximum recursion depth exceeded
๐ฏ Final Output
RecursionError

0 Comments:
Post a Comment