Context manager with class methods

Gavin Panella gavinpanella at
Thu Sep 22 06:21:28 EDT 2011


On Python 2.6 and 3.1 the following code works fine:

    class Foo(object):
        def __enter__(cls):
        def __exit__(cls, exc_type, exc_value, traceback):
    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__

Is this a regression or a deliberate change? Off the top of my head I
can't think that this pattern is particularly useful, but it seems
like something that ought to work.


More information about the Python-list mailing list