[Python-ideas] FW: Map-then-filter in comprehensions

MRAB python at mrabarnett.plus.com
Tue Mar 8 14:49:16 EST 2016


On 2016-03-08 19:07, Joseph Jevnik wrote:
> I was going to write `with expr as name` but I felt that people might
> find it too conficting with the other use of the `with` keyword. I
> personally find that it reads very well. We could always go with a new
> keyword like: `bind expr as name` but that seems pretty heavy. In
> defense of the `for expr as name` proposal, it does match pretty closely
> to the nested for statements in a comprehension.
>
Usually, 'for' is followed by the name(s) that will be bound. There's an 
example of that just before it!

"for x in ..." but "for ... as x".

> On Tue, Mar 8, 2016 at 2:01 PM, Chris Angelico <rosuav at gmail.com
> <mailto:rosuav at gmail.com>> wrote:
>
>     On Wed, Mar 9, 2016 at 5:57 AM, Joseph Jevnik <joejev at gmail.com
>     <mailto:joejev at gmail.com>> wrote:
>     > If we were to translate this to python syntax we could have something like:
>     > [r + 1 for n in range(1, 11) for n * 3 as r if r % 4 == 0]
>     > There is no reason that the name binding needs to be a part of the predicate
>     > expression, they can just be seperate clauses. I think the `for expr as
>     > name` is nice because it matches the order that comprehensions over multiple
>     > iterators are evaluated like: `[n for n in ns for m in ms]`.
>
>     That's somewhat more appealing. Not enthused about "for expr as name";
>     maybe "with expr as name"? Aside from not calling __enter__ and
>     __exit__, it's the same kind of operation that a with block does. But
>     the semantic difference isn't a good thing.
>



More information about the Python-ideas mailing list