Generators and Generator Functions >>def plus_minus(x): We are allowed to call next on yield x generators because generators are a yield -X type of iterator. >>t plus_minus(3) >>next(t) Calling next on a generator goes into 3 the function and evaluates to the first >>next(t) yield statement.The next time we -3 call next on that generator,it >>>t <generator object> resumes where it left off (just like >>next(t) calling next on any iterator!) StopIteration Once the generator hits a return statement,it raises a StopIterationGenerators and Generator Functions >>> def plus_minus(x): ... yield x ... yield -x >>> t = plus_minus(3) >>> next(t) 3 >>> next(t) -3 >>> t <generator object> >>> next(t) StopIteration We are allowed to call next on generators because generators are a type of iterator. Calling next on a generator goes into the function and evaluates to the first yield statement. The next time we call next on that generator, it resumes where it left off (just like calling next on any iterator!) Once the generator hits a return statement, it raises a StopIteration