self insequence insequence def __iter__(self)return self def __next__(self) self insequence readline(while and 'warningnot in ll self insequence readline(if not lraise stopiteration return replace('\twarning'''with open(innameas infilewith open(outname" "as outfilefilter warningfilter(infilefor in filteroutfile write(lno doubt about itthat is so ugly and difficult to read that you may not even be able to tell what' going on we created an object that takes file object as inputand provides __next__ method like any iterator this __next__ method reads lines from the filediscarding them if they are not warning lines when it encounters warning lineit returns it then the for loop will call __next__ again to process the next warning line when we run out of lineswe raise stopiteration to tell the loop we're finished iterating it' pretty ugly compared to the other examplesbut it' also powerfulnow that we have class in our handswe can do whatever we want with it with that background behind uswe finally get to see generators in action this next example does exactly the same thing as the previous oneit creates an object with __next__ method that raises stopiteration when it' out of inputsimport sys innameoutname sys argv[ : def warnings_filter(insequence)for in insequenceif 'warningin lyield replace('\twarning'''with open(innameas infilewith open(outname" "as outfile