Python: Make class iterable Python: Make class iterable python python

Python: Make class iterable


Add the __iter__ to the metaclass instead of the class itself (assuming Python 2.x):

class Foo(object):    bar = "bar"    baz = 1    class __metaclass__(type):        def __iter__(self):            for attr in dir(self):                if not attr.startswith("__"):                    yield attr

For Python 3.x, use

class MetaFoo(type):    def __iter__(self):        for attr in dir(self):            if not attr.startswith("__"):                yield attrclass Foo(metaclass=MetaFoo):    bar = "bar"    baz = 1


this is how we make a class object iterable. provide the class with a iter and a next() method, then you can iterate over class attributes or their values.you can leave the next() method if you want to, or you can define next() and raise StopIteration on some condition.

e.g:

class Book(object):      def __init__(self,title,author):          self.title = title          self.author = author      def __iter__(self):          for each in self.__dict__.values():              yield each>>> book  = Book('The Mill on the Floss','George Eliot')>>> for each in book: each...'George Eliot''The Mill on the Floss'

this class iterates over attribute value of class Book.A class object can be made iterable by providing it with a getitem method too.e.g:

class BenTen(object):    def __init__(self, bentenlist):        self.bentenlist = bentenlist            def __getitem__(self,index):        if index <5:            return self.bentenlist[index]        else:            raise IndexError('this is high enough')>>> bt_obj = BenTen([x for x in range(15)])>>>for each in bt_obj:each...01234

now when the object of BenTen class is used in a for-in loop, getitem is called with succesively higher index value, till it raises IndexError.


You can iterate over the class's unhidden attributes with for attr in (elem for elem in dir(Foo) if elem[:2] != '__').

A less horrible way to spell that is:

def class_iter(Class):    return (elem for elem in dir(Class) if elem[:2] != '__')

then

for attr in class_iter(Foo):    pass