Context manager with class methods

Thomas Rachel nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915 at spamschutz.glglgl.de
Thu Sep 22 13:47:15 CEST 2011


Am 22.09.2011 12:21 schrieb Gavin Panella:
> Hi,
>
> On Python 2.6 and 3.1 the following code works fine:
>
>      class Foo(object):
>
>          @classmethod
>          def __enter__(cls):
>              print("__enter__")
>
>          @classmethod
>          def __exit__(cls, exc_type, exc_value, traceback):
>              print("__exit__")
>
>      with Foo: pass
>
> However, in 2.7 and 3.2 I get:
>
>      Traceback (most recent call last):
>        File "<stdin>", line 1, in<module>
>      AttributeError: __exit__

Same here.

But

     with Foo(): pass

works, and that is more important and more logical.


Thomas



More information about the Python-list mailing list