[Tutor] Regex not working as desired

Alan Gauld alan.gauld at yahoo.co.uk
Tue Mar 6 18:19:56 EST 2018


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
used.

"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
iterable.

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
optional:

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

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list