Program - show class of custom sequence type ,iter , iterables and iterator using example of tuple


Topic: Program - show class of custom sequence type ,iter , iterables and iterator using example of tuple

Solution

class CustomTupleIter:
    """
    This is the space to do documentation related to class.
    """
    def __init__(self):
        self.list_ = (1,2,3,4)
    def __len__(self):
        return len(self.list_)
    def __getitem__(self, i):
        if isinstance(i, int):
            if i<0:
                i = len(self.list_) + i
            if i<0 or i>=len(self.list_):
                raise IndexError('Invalid Input')
            else:
                return self.list_[i]
    def __iter__(self):
        return self.CustomTupleIterator(self)
    class CustomTupleIterator:
        def __init__(self, other):
            self.count = 0
            self.other = other
        def __iter__(self):
            return self
        def __next__(self):
            if self.count < len(self.other.list_):
                self.count += 1
                return self.other.list_[self.count]
            else:
                raise StopIteration



List all Python Programs