python - How to return a value when destroying/cleaning-up an object instance -
When I started a class in Python, I can assume it. I then call method in class which does everything. Here's a snippet:
class TestClass (): def __init __ (self): self.counter = 0 def doSomething (self): self.counter = self.counter + 1 print 'Hiya "If __name__ ==" __main__ ": obj = TestClass () obj.do (something) Objkdo (something) Objkdo some () Print obj.counter
as you You can see, every time I call doSomething
method, it prints some text and gives an internal variable ie salary of salary. When I start the class, I set the Counter Variable to 0. When I delete the object, I want to return the internal counter
variable. What would be a good way to do this? I wanted to know any other ways in addition to different stuff like:
- variable
obj.counter like to reach directly
. - Creating a method like
getcounter
.
Thank you.
Acting on object destruction is typically frowned upon. Python provides a __ del __
function, but in some cases it can not be called.
What if you want to do something with the counter variable? Where does the data go?
Comments
Post a Comment