__iter__
and __next__
methods and raises the StopIteration
exception automatically behind the scenes. yield
statement (or the equivalent(?) yield expression) is used. for
and optional if
clause: >>> gen_expr = ( math.sqrt(i) for i in [3,-2,5,6] if i >= 0 ) >>> type(gen_expr) <class 'generator'>