Code Explanation:
1. Defining the class
class SelfDestruct:
This line defines a class named SelfDestruct.
2. Defining a method
def boom(self):
boom is an instance method.
It can be called using an object of SelfDestruct.
3. Deleting the method from the class
del SelfDestruct.boom
This line removes the method boom from the class itself.
After this executes:
SelfDestruct no longer has a method named boom.
All instances (existing and future) lose access to this method.
4. Returning a value
return "done"
The method returns the string "done".
This return still works because the method is already executing when it deletes itself.
5. Creating an instance
s = SelfDestruct()
An object s of class SelfDestruct is created.
At this moment, boom still exists on the class.
6. Calling the method
print(s.boom())
๐ What happens step by step:
Python finds boom in the class SelfDestruct.
The method starts executing.
del SelfDestruct.boom deletes the method from the class.
"done" is returned.
The result is printed.
✅ Final Output
done

0 Comments:
Post a Comment