the easiest way to create an iterator--that is to saya lazy sequence --in python is to define generator functionas was discussed in the entitled "callables simply use the yield statement within the body of function to define the places (usually in loopwhere values are produced ortechnicallythe easiest way is to use one of the many iterable objects already produced by built-ins or the standard library rather than programming custom one at all generator functions are syntax sugar for defining function that returns an iterator many objects have method named __iter__()which will return an iterator when it is calledgenerally via the iter(built-in functionor even more often simply by looping over the object ( for item in collectionwhat an iterator is is the object returned by call to iter(some thing)which itself has method named __iter__(that simply returns the object itselfand another method named __next__(the reason the iterable itself still has an __iter__(method is to make iter(idempotent that isthis identity should always hold (or raise typeerror("object is not iterable"))iter_seq iter(sequenceiter(iter_seq=iter_seq the above remarks are bit abstractso let us look at few concrete exampleslazy open(' -laziness md'iterate over lines of file '__iter__in dir(lazyand '__next__in dir(lazytrue plus map(lambda xx+ range( )plus iterate over deferred computations '__iter__in dir(plus and '__next__in dir(plus true def to ()for in range( )yield '__iter__in dir(to false '__iter__in dir(to ()and '__next__in dir(to ()true the iterator protocol