t510.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. l = [1, 2, 3, 4]
  2. for i in l.__iter__():
  3. print i
  4. class MyIterable:
  5. def __init__(self, lst):
  6. self.x = 3
  7. self.iter = lst
  8. def __iter__(self):
  9. return self.iter.__iter__()
  10. mi = MyIterable([5, 6, 7])
  11. for i in mi.__iter__():
  12. print i
  13. for i in mi:
  14. print i
  15. class Counter:
  16. def __init__(self, low, high):
  17. self.current = low
  18. self.high = high
  19. def __iter__(self):
  20. return self
  21. def next(self):
  22. if self.current > self.high:
  23. raise StopIteration
  24. else:
  25. self.current += 1
  26. return self.current - 1
  27. for c in Counter(9, 12):
  28. print c
  29. # class SillyDictIter:
  30. # not reliable because order isn't guaranteed
  31. # def __init__(self):
  32. # self.w = {'f':1, 'o':2, 'g':3}
  33. # def __iter__(self):
  34. # return self.w.__iter__()
  35. # x = SillyDictIter()
  36. # for i in x:
  37. # print i
  38. class SillyTupleIter:
  39. def __init__(self,s):
  40. self.w = tuple(s)
  41. def __iter__(self):
  42. return self.w.__iter__()
  43. x = SillyTupleIter("foo")
  44. for i in x:
  45. print i