Answer the question
In order to leave comments, you need to log in
How in Python to ensure that the destructor code is called immediately after the instance is not needed?
As you know, there is no guarantee that __del__ will be called immediately after the object has no references left. But sometimes you want the resources used by an object to be freed immediately when the object is no longer needed. How to guarantee it?
Let's say we have a code:
def test()
a = Processor('file.txt')
a.process()
test()
test()
Answer the question
In order to leave comments, you need to log in
You need to add functionality to the ContextManager in order to be able to use the class with the with construct. See PEP 343 .
And what prevents at the end of the function to add an explicit closure?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question