like list comprehensionsthere is always statement-based equivalent to generator expressionthough it sometimes renders substantially more code'join( upper(for in line split(if len( 'aabbbres 'for in line split()if len( res + upper(statement equivalentthis is also join res 'aabbbin this casethoughthe statement form isn' quite the same--it cannot produce items one at timeand it' also emulating the effect of the join that forces results to be produced all at once the true equivalent to generator expression would be generator function with yieldas the next section shows generator functions versus generator expressions let' recap what we've covered so far in this sectiongenerator functions function def statement that contains yield statement is turned into generator function when calledit returns new generator object with automatic retention of local scope and code positionan automatically created __iter__ method that simply returns itselfand an automatically created __next__ method (next in xthat starts the function or resumes it where it last left offand raises stopitera tion when finished producing results generator expressions comprehension expression enclosed in parentheses is known as generator expression when runit returns new generator object with the same automatically created method interface and state retention as generator function call' results --with an __iter__ method that simply returns itselfand _next__ method (next in xthat starts the implied loop or resumes it where it last left offand raises stopiteration when finished producing results the net effect is to produce results on demand in iteration contexts that employ these interfaces automatically as implied by some of the preceding sectionsthe same iteration can often be coded with either generator function or generator expression the following generator expressionfor examplerepeats each character in string four timesg ( for in 'spam'list( ['ssss''pppp''aaaa''mmmm' comprehensions and generations generator expression force generator to produce all results