I am unsuccessfully trying to get the magic with
-statement methods __enter__
and __exit__
running on class-level:
class Spam():
@classmethod
def __enter__(cls):
return cls
@classmethod
def __exit__(cls, typ, value, tb):
cls.cleanup_stuff()
with Spam:
pass
However, this will result in an AttributeError
:
Traceback (most recent call last):
File "./test.py", line 15, in <module>
with Spam:
AttributeError: __exit__
Is it possible to use the __enter__
and __exit__
methods on class-level anyway?