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]

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.