Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. from itertools import count, cycle
  2. from collections import Iterable
  3. from functools import wraps
  4.  
  5. class genmethod:
  6. def __init__(self, g):
  7. self.g = g
  8. def __get__(self, instance, owner):
  9. gi = self.g(instance)
  10. @wraps(self.g)
  11. def wrapper(*a):
  12. return gi.send(a)
  13. next(gi)
  14. setattr(owner, self.g.__name__, wrapper)
  15. return wrapper
  16.  
  17. class Foo:
  18. @genmethod
  19. def __str__(self):
  20. yield
  21. for x in count():
  22. yield str(x)
  23.  
  24. @genmethod
  25. def __eq__(self):
  26. other = yield
  27. for x in cycle((True, False)):
  28. other = yield x
  29.  
  30. foo, bar = Foo(), Foo()
  31.  
  32. print(foo, foo, foo, foo)
  33. print(foo == bar, foo == bar, foo == bar, foo == bar)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement