[Tutor] function return values

Phil phillor9 at gmail.com
Fri Dec 3 17:46:06 EST 2021


> Not that generator functions (functions containing a "yield" statement)
> are used _differently_ to normal functions.
>
>      def gen():
>          x = 1
>          while True:
>              yield x
>              x += 2
>
> This generates odd numbers. The _return_ of the function is an iterable,
> the generator:
>
>      g = gen()
>
> At this point the function _has not run_. If you print(g) you'll see a
> generator object. It is iterable, and a common way to use it is to
> iterable with a for-loop:
>
>      for n in g:
>          print(n)
>
> Each iteration of the loop rns the function until it yields, and you get
> what it yielded as the next value in the iteration. So the for-loop
> about assigns odd numbers to "n".
Thank Cameron for the extra generator information and the example that 
illustrates it's use well.
>      if (item, row, column_list) == (None, None, None):
>
> is a Boolean expression comparing 2 3-tuples.

Of course, and thank you for pointing that out.

-- 

Regards,
Phil



More information about the Tutor mailing list