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

Rhodri James rhodri at kynesim.co.uk
Fri May 11 13:14:28 EDT 2018


On 11/05/18 15:04, Jacco van Dorp wrote:
> A while ago, we had this gem:
> 
> 2018-04-06 8:19 GMT+02:00 Serhiy Storchaka<storchaka at gmail.com>:
>> Using currently supported syntax:
>>
>>      smooth_signal = [average for average in [0] for x in signal for average in [(1-decay)*average + decay*x]]
> Go ahead and understand that line in 1 go. It's currently legal syntax
> for a running average for a smoothing signal, which remembers
> something about it. (Subject: Proposal: A Reduce-Map Comprehension and
> a "last" builtin)
> 
> You're not allowed to work it out bit by bit, just understand the
> entire line or nothing. Any failure of yours proves my point.

Personally I thought it proved the point that you shouldn't be trying to 
squash things like that into a list comprehension in the first place, 
because

   average = 0
   smooth_signal = []
   for x in signal:
     average = (1 - decay) * average + decay * x
     smooth_signal.append(average)

is quite a bit more comprehensible.

-- 
Rhodri James *-* Kynesim Ltd


More information about the Python-ideas mailing list