[Python-Dev] PEP 572: Assignment Expressions

Steven D'Aprano steve at pearwood.info
Tue May 1 03:00:53 EDT 2018


On Tue, May 01, 2018 at 11:04:55AM +1000, Chris Angelico wrote:

> To be fair, I don't see many people replacing "x = 1" with "for x in
> [1]: pass". Even though it IS going to have the same effect. :-)

Aside from the pass, that is precisely one of the current work-arounds 
for lack of binding-expressions inside comprehensions:

    # inefficient, and wrong when f(x) has side-effects
    [f(x) for x in iterable if f(x) > 1]

    # what we'd like
    [y for x in iterable if (y := f(x)) > 1]

    # a work-around
    [y for x in iterable for y in [f(x)] if y > 1]


I think that's even in your PEP, isn't it?



-- 
Steve


More information about the Python-Dev mailing list