Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import count, cycle
- from collections import Iterable
- from functools import wraps
- class genmethod:
- def __init__(self, g):
- self.g = g
- def __get__(self, instance, owner):
- gi = self.g(instance)
- @wraps(self.g)
- def wrapper(*a):
- return gi.send(a)
- next(gi)
- setattr(owner, self.g.__name__, wrapper)
- return wrapper
- class Foo:
- @genmethod
- def __str__(self):
- yield
- for x in count():
- yield str(x)
- @genmethod
- def __eq__(self):
- other = yield
- for x in cycle((True, False)):
- other = yield x
- foo, bar = Foo(), Foo()
- print(foo, foo, foo, foo)
- print(foo == bar, foo == bar, foo == bar, foo == bar)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement