Jan-03-2022, 01:00 AM
(This post was last modified: Jan-03-2022, 01:00 AM by BashBedlam.)
(Jan-02-2022, 11:19 PM)Skaperen Wrote: is there a list of everything that works with the with statement? or, alternatively, everything that does not work with the with statement? i could not find anything at all, one way or the other, regarding how a particular function behaved, so i had to set up a test script.What you can search for is "Context Manager". I think that you'll get more information that way. I can't tell you what does or does not qualify as a context manager but if you want to have some fun fooling around with the idea, you can roll your own. Try this:
from time import time class Timer : # This is your context manager :) def __enter__ (self) : self.start = time () return self def __exit__ (self, *args) : end = time () self.interval = end - self.start with Timer () as tracker: for count in range (99999) : print ('Still working...') print(f'The loop took {tracker.interval:.3f} seconds.')