Tuesday, October 9, 2007

Required methods to make a class iterable

Really useful:
#required iterable elements
class MyIterator(object):

    def __iter__(self):
        return self.data.__iter__()

    def __len__(self):
        return len(self.data)

    def __contains__(self, v):
        return v in self.data

    def __getitem__(self, v):
        return self.data[v]

1 comment:

RobE said...

Thanks!