With class as contextmanager
this.wiederkehr at gmail.com
Wed Jan 25 07:36:34 EST 2017
Thank you for your answers. Very appreciated.
I ended up doing it as follows:
def __init__(cls, *args):
cls.lock = Lock()
cls.instances = 
def register(cls, instance):
def __exit__(cls, exc_type, exc_val, exc_tb):
for instance in cls.instances:
cls.instances = 
if not self.__class__.lock.locked():
raise Exception("You have to use the context manager on
the Class Object!")
inst1 = Klass()
inst2 = Klass()
# on leaving the context the cleanup of each instance of Klass is called.
2017-01-25 3:21 GMT+01:00 Terry Reedy <tjreedy at udel.edu>:
> On 1/24/2017 4:31 PM, This Wiederkehr wrote:
> having a class definition:
>> class Test():
>> def __enter__(cls):
>> def __exit__(cls, exception_type, execption_value, callback):
>> now using this as a contextmanager does not work, even though Test is an
>> object and has the two required methods __enter__ and __exit__.
>> it fails with:
>> #Attribute Error: __enter__
>> This is not working because behind the scene it does something like:
>> But isn't this supposed to be working?
> No. Unqualified 'method' means instance method, not class method.
> One can simulate instance methods like so:
> >>> import types
> >>> def f(self): print('f called')
> >>> class C: pass
> >>> c = C()
> >>> c.m = types.MethodType(f, c)
> >>> c.m()
> f called
> However, this will not work for dunder methods where the lookup for the
> method starts with the class and not the object itself.
> >>> def h(self): return 1
> >>> c.__hash__ = types.MethodType(h, c)
> >>> hash(c) # calls type(c).__hash__, not c.__hash__
> Terry Jan Reedy
079 785 70 75
More information about the Python-list