How can I call super in a method decorator in Python 3?

How to fill in ??? ?

 def ensure_finished(iterator): try: next(iterator) except StopIteration: return else: raise RuntimeError def derived_generator(method): def new_method(self, *args, **kwargs): x = method(self, *args, **kwargs) y = getattr(super(???, self), method.__name__)\ (*args, **kwargs) for a, b in zip(x, y): assert a is None and b is None yield ensure_finished(x) ensure_finished(y) return new_method 
+1
python super decorator python-decorators
Sep 18 '14 at 20:13
source share
1 answer

EDIT: This does not work for the reasons stated in the comments. I will leave it here so that the next guy trying to answer does not do the same (until a real answer appears).

You should use type(self)

Example I simplified your code a bit, but the entity should still be there

 def derived_generator(method): def new_method(self, *args, **kwargs): x = method(self, *args, **kwargs) y = getattr(super(type(self), self), method.__name__)\ (*args, **kwargs) for a, b in zip(y, x): yield a, b return new_method class BaseClass(object): def iterator(self): return [1, 2, 3] class ChildClass(BaseClass): @derived_generator def iterator(self): return [4, 5, 6] a = ChildClass() for x in a.iterator(): print(x) 
-one
Oct 23 '14 at 22:08
source share
— -



All Articles