On 8 March 2016 at 16:02, Paul Moore <p.f.moore@gmail.com> wrote:
On 8 March 2016 at 14:17, Allan Clark <allan.clark@gmail.com> wrote:
> tl;dr What is support like for adding an 'as' clause to comprehension
> syntax? In order to allow map-then-filter, it might look like something
> this:
>     [y for x in numbers if abs(x) as y > 5]
> I wish to propose an extension to Python comprehension syntax in an attempt
> to make it applicable in more areas.

[snip, fair comment]
So what seems to me to be missing from your proposal is an explanation
of how extending the comprehension syntax is an improvement over not
using a comprehension at all.

Yeah that's a pretty fair observation.
You suggest

    [y for x in numbers if abs(x) as y > 5]

as a simpler alternative to

    [y for y in (abs(x) for x in numbers) if y > 5]

but I'd be much more likely to write

    results = []
    for x in numbers:
        y = abs(x)
        if y > 5:


    def bounded(it, lower):
        for val in it:
            absval = abs(val)
            if absval > lower:
                yield absval

    list(bounded(numbers, 5))

Again, fair. Not sure I can quite articulate why I would prefer a comprehension here.
A very weak argument would be that such code tends to change, and when it does it may get morphed back into something that *can* be done with a comprehension, but you might end up leaving it as a for-loop with append.
Obviously real world examples would be better than artificial ones, as
artificially simple examples make terse notation look better... And in
the example using a generator, you'd be able to give it a far more
meaningful name with a bit of real-life domain terminology

Yeah agreed.
I would note that in a real-world example you are giving a name to something that is forced to calculate *and* filter. So your name is going to end-up being something like, say "is_registered_and_voting_for", or "is_highest_tax_bracket_and_is_taxed". Which you might not actually write, and instead opt for something like "tax_amount". Even in your code for my artificial example your generator is named "bounded" but that does not sound like it is doing any filtering at all, it sounds like it is simply bounding all values. Of course to be fair, I didn't give a name for that at all, and probably I want to give a name for the resulting list. Although of course we both need to do that, but at least with a comprehension you only have to come up with a name for the result, not for the result and the associated generator.