iterator pattern python uses two specific method calls and one raised exception to provide the iterator functionality throughout the language the first method of an iterable is the __iter__(methodwhich returns an iterator object nexta __next__(method must be provided by the iterator objectit is used to return the next element in the iterable lastlywhen all the elements have been iterated overthe iterator raises stopiteration exception we often see the iterable collection in python return an instance of itselfgiven that the class in question also implements the __next__(method and raises the stopiteration exception an iterator similar to the classic implementation we looked at before might look something like thisclass mylist(object)def __init__(self*args)self list list(argsself index def __iter__(self)return self def __next__(self)tryself index + return self list[self index except indexerrorraise stopiteration(if __name__ ="__main__"my_list mylist( for in my_listprint(ithis timeyou will notice that we used the python for loop to iterate over the elements in the listwhich is only possible because our mylist class has the functions required by the python iterator protocol