Python Forum
what works with the with statement?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what works with the with statement?
#2
(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.')
Reply


Messages In This Thread
what works with the with statement? - by Skaperen - Jan-02-2022, 11:19 PM
RE: what works with the with statement? - by BashBedlam - Jan-03-2022, 01:00 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020