Code Explanation:
1. Defining the Class
class Flip:
A class named Flip is defined.
2. Defining the Method act
def act(self):
self.act = 10
return 1
Inside this method, two things happen:
๐น Step 1: Assigning to self.act
self.act = 10
This creates an instance attribute named act.
It overrides (shadows) the class method act.
After this line runs, for this instance:
f.act == 10
๐น Step 2: Returning a Value
return 1
The method returns 1.
3. Creating an Object
f = Flip()
An instance f of class Flip is created.
Initially:
f.act → class method
4. First Call: f.act()
f.act()
Step-by-step:
Python looks for act in f.__dict__ → ❌ not found.
Looks in class Flip → finds method act.
Executes the method.
Inside method:
self.act = 10 creates an instance attribute.
Returns 1.
After this call:
f.__dict__ == {'act': 10}
So now:
f.act → 10
5. Printing
print(f.act(), f.act)
๐น First Part → f.act()
Uses the class method.
Returns 1.
๐น Second Part → f.act
Now act is an instance attribute.
Its value is 10.
It is not callable anymore.
So it prints 10.
6. Final Output
1 10
✅ Final Answer
✔ Output:
1 10

0 Comments:
Post a Comment