Context without manager

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Nov 27 01:43:14 EST 2023


On 27/11/23 5:03 pm, Grant Edwards wrote:
> I should probably have written "how to fool that <something> into
> working when he's not using a 'with' statement"

It should be possible to run the context protocol yourself.
Something like (warning, untested):

     class MyDeviceWrapper:

         def __init__(self):
             self.cm = device_open()
             self.device = self.cm.__enter__()

         # Other methods here for doing things with
         # self.device

         def close(self):
             self.cm.__exit__(None, None, None)

-- 
Greg



More information about the Python-list mailing list