Guest User

iterating a class

a guest
May 13th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.26 KB | None | 0 0
  1. class Foo():
  2.     a = 1
  3.     b = 2
  4.     c = 42
  5.     d = ['hello', 'world']
  6.  
  7.     def __iter__(self):
  8.         yield self.a
  9.         yield self.b
  10.         yield self.c
  11.         yield from self.d
  12.  
  13.  
  14. f = Foo()
  15. for x in f:
  16.     print(x)
  17.  
  18. # Should print the following:
  19. # 1
  20. # 2
  21. # 42
  22. # 'hello'
  23. # 'world'
Advertisement
Add Comment
Please, Sign In to add comment