One liners

Steven D'Aprano steve+comp.lang.python at
Sat Dec 7 12:27:47 CET 2013

On Fri, 06 Dec 2013 22:27:00 -0500, Roy Smith wrote:

> Just for fun, I took a look through the Songza code base.  66 kloc of
> non-whitespace Python.  I found 192 ternary expressions.  Here's a few
> of the more bizarre ones (none of which I consider remotely readable):
> -------------------------------------------------- 
> extracols = ( sorted(set.union(*(set( for t in tracks)))
>               if tracks else [] )

[extra parentheses added so I can split the line over two]

I actually don't find that too bad, readability-wise. Not ideal, but I 
can follow it. However, I wonder why is converted to a set 
before being unpacked? Even assuming that data.keys are not necessarily 
unique, wouldn't building the union make them so?

extracols = ( sorted(set.union(*( for t in tracks)))
              if tracks else [] )

Also, you can get rid of the `if tracks` check altogether by using a 
bound method instead of an unbound method:

extracols = sorted(set().union(*( for t in tracks))

ought to work even if tracks is empty.


More information about the Python-list mailing list