the PHP ternary operator equivalent on Python

rurpy at yahoo.com rurpy at yahoo.com
Thu Nov 24 14:55:30 EST 2005


Fredrik Lundh wrote:
> rurpy at yahoo.com wrote:
>
> > I don't find your code any more readable than the OP's
> > equivalent code:
>
> the OP's question was
>
>     How you do this in a practic way without
>     the use of one-line code ?

I know.  But you compared the readability of code with
one-liners and long variable names with the readability
of code with no one-liners and short variable names.
I thought that most of the readability improvement was
due to the long-to-short name change.  So I posted what
I hoped was code equivalent to what you quoted, but
using your short names and a one-liner "then-if-else"
(on 3 lines :-)  It satisfied me that the readability
improvement was due to the short varible names.

> > The OPs code make one pass through the dict, your's makes
> > two.  I do not know what effect (if any) that has in the case of
> > a very large dict.
>
> the OPs code contains five reads and three writes, my and
> steven's versions contain four reads and three writes.  the
> yet-to-be-released ternary operator would remove one read
> from the OPs code, which makes both alternatives equivalent.
>
> dictionary accesses don't involve passes over the dictionary.

I was actually thinking of the effects of hardware
memory cache...

> there's a comment on readability and code understanding waiting
> to be made here, but I'll leave that for another day.




More information about the Python-list mailing list