0

Does "with ... as ...:" work with non-files?

For example, this code doesn't work: with int("5") as a: print(a)

21st Oct 2016, 11:54 PM
novice
1 Answer
+ 2
Actually it works with objects, where those objects provide __enter__ and __exit__ methods. http://effbot.org/zone/JUMP_LINK__&&__python__&&__JUMP_LINK-with-statement.htm If you defined your own integer object with those methods, you could also 'with' them.
22nd Oct 2016, 7:03 AM
Kirk Schafer
Kirk Schafer - avatar