On 06/03/18 22:17, Albert-Jan Roskam wrote:
> But the way you wrote it, the generator expression just "floats" 

Any expression can be used where a value is expected provided
that e3xpression produces a value of the required type.

A generator expression effectively produces a sequence and
the type of sequence is defined by the type of parentheses

"123" -> a string
[1,2,3] -> a list
(1,2,3) -> a tuple
{1,2,3} -> a set

So when a function requires an iterable sequence you just
provide the expression(any expression) that results in an

all(range(5))  # range 5 produces a range object which is iterable
all(n for n in [0,1,2,3,4])  # generator "equivalent" to the range

Similar things happen with tuples where the parens are actually

1,2,3   # a tuple of 3 numbers
(1,2,3) # the same tuple with parens to make it more obvious

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:

Reply via email to