[Python-ideas] Introducing where clauses

Andrey Popp 8mayday at gmail.com
Mon Jun 22 14:46:44 CEST 2009


I think that producing a list of tuples (that is conceptually image of
mapping from some_iterable set) is basic operation.
But... ok, now we have three ways to produce list below:

    [(f(x), f(x)) for x in some_iterable if f(x) < 2]

1)
> def g(iterable):
>   for x in iterable:
>     y = f(x)
>     if y < 2:
>       yield (y, y)

2)
> [(y, y) for y in (f(x) for x in some_iterable) if y < 2]

3)
> map(lambda obj: (obj, obj),
>    filter(lambda y: y < 2,
>    map(f, some_iterable)))

And none of them does not look as obvious as

    [(f(x), f(x)) for x in some_iterable if f(x) < 2]

, doesn't it? While proposed variant with where-clause

    [(y, y) for x in some_iterable if y < 2 where y = f(x)]

looks more naturally than three suggested variants.

I give strong emphasis on that fact, that where-clause is only
syntactic sugar, suggested for better readability.



More information about the Python-ideas mailing list