Code Explanation:
1. Defining the Class
class Flip:
A class named Flip is defined.
2. Defining the Method act
def act(self):
self.act = lambda: "flipped"
return "first"
This method performs two actions:
Creates an instance attribute named act
self.act = lambda: "flipped"
This assigns a new attribute to the object.
The new attribute overrides (shadows) the method act on this instance.
Returns a string
return "first"
3. Creating an Object
f = Flip()
An instance f of class Flip is created.
At this moment:
f.act → method (from class)
4. First Call: f.act()
f.act()
Step-by-step:
Python finds act on the class Flip.
The method is called.
Inside the method:
self.act = lambda: "flipped" creates an instance attribute.
The method returns "first".
Result of first call:
"first"
5. Second Call: f.act()
f.act()
Step-by-step:
Python looks for act on the instance f.
Finds the instance attribute:
f.act == lambda: "flipped"
The lambda function is called.
Returns "flipped".
Result of second call:
"flipped"
6. Printing Both Results
print(f.act(), f.act())
First f.act() → "first"
Second f.act() → "flipped"
7. Final Output
first flipped
Final Answer
✔ Output:
first flipped

0 Comments:
Post a Comment