One liners
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sat Dec 7 06:27:47 EST 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(t.data.keys()) 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 t.data.keys() 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(*(t.data.keys()) 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(*(t.data.keys()) for t in tracks))
ought to work even if tracks is empty.
--
Steven
More information about the Python-list
mailing list