Context manager with class methods

Mel mwilson at the-wire.com
Thu Sep 22 14:53:21 CEST 2011


Mel wrote:
> This seems to work:
> 
> 
> 
> class MetaWith (type):
>     @classmethod
>     def __enter__(cls):
>         print("__enter__")
> 
>     @classmethod
>     def __exit__(cls, exc_type, exc_value, traceback):
>         print("__exit__")
> 
> class With (object):
>     __metaclass__ = MetaWith
>     
> with With:
>     pass

It seems to work equally well without the `@classmethod`s

	Mel.




More information about the Python-list mailing list