python - How can an instance be iterated over? -


in datatstructures.py, there method values():

def values(self):     """iterate on values."""     item in self:         yield item[0] 

self instance of class; how can iterated over?

simple, has implement __iter__ method, e.g.

class test:     def __iter__(self):         yield 1         yield 2  >>> instance = test() >>> val in instance: ...     print val ... 1 2 

Comments