How to make a custom object iterable?
I have a list of custom-class objects (sample is below). Using: list(itertools.chain.from_iterable(myBigList)) I wanted to "merge" all of the stations sublists into one big list. So I thought I need to make my custom class an iterable. Here is a sample of my custom class. class direction(object) : def __init__(self, id) : self.id = id self.__stations = list() def __iter__(self): self.__i = 0 # iterable current item return iter(self.__stations) def __next__(self): if self.__i<len(self.__stations)-1: self.__i += 1 return self.__stations[self.__i] else: raise StopIteration I implemented __iter__