[Python-ideas] Inline assignments using "given" clauses

Steven D'Aprano steve at pearwood.info
Sat May 12 22:27:26 EDT 2018


On Sat, May 12, 2018 at 01:13:05PM -0500, Tim Peters wrote:
> Just clarifying a fine point here:
> 
> [Steven D'Aprano <steve at pearwood.info>]
> > ...
> > average = 0
> > smooth_signal = [(average := (1-decay)*average + decay*x) for x in signal]
> > assert average == smooth_signal[-1]
> >
> > I'm not even sure if "given" will support this. Nick is arguing strongly
> > that bound targets should be local to the comprehension, and so I think
> > you can't even write this example at all with Nick's scoping rule.
> 
> You can't under Nick's proposal(s), at least not directly (there are
> always "tricks").  But it also blows up with UnboundLocalError (for
> the "average" in "(1-decay)*average") under the current PEP 572 (the
> ":=" PEP).

Yes, but I've sort of assumed that if PEP 572 has even a microscopic 
chance of being accepted, it will have to be changed, given that Guido 
has already stated that the only behaviour that makes sense is what you 
and I have been suggesting. Unless Guido has changed his mind, the 
relevant links are:

https://mail.python.org/pipermail/python-ideas/2018-May/050411.html

https://mail.python.org/pipermail/python-ideas/2018-May/050456.html

So maybe this is a tiny bit naughty (or a lot...) but I've just been 
ignoring what the PEP currently says and going by what it ought to say
:-)



-- 
Steve


More information about the Python-ideas mailing list