PEP 308: Obfuscated Nested Ternaries (Not)
Terry Reedy
tjreedy at udel.edu
Thu Feb 20 10:59:49 EST 2003
"Gerrit Holl" <gerrit at nl.linux.org> wrote in message
news:mailman.1045749596.2787.python-list at python.org...
> For the Obfuscated Contest: rewrite this peace of code
> using ternaries. Preferably with a lot of ('s and )'s :
Instead, I will rewrite so more clear than original.
seq = (
x > 0 ?
y > 0 ? (tl, br, b, br, r, br) :
y==0 ? (l, r, t, r, b, r) :
(bl, tr, t, tr, r, tr) :
x==0 ?
y > 0 ? (t, b, l, b, r, b) :
y==0 ? (tl, tr, br, bl, tl) :
(b, t, l, t, r, t) :
# x < 0
y > 0 ? (tr, bl, b, bl, l, bl) :
y==0 ? (r, l, t, l, b, l) :
(br, tl, t, tl, l, tl)
)
To me, this is clearer than the if:elif:else statement since it makes
it immediately obvious that seq and only seq is being assigned a
value. In your original form, one has to check that all targets are
the same. If the target were qxyz_ade[i3][j4], this would be even
more difficult. Assignment to one variable should be one statement.
Thanks for the choice to should what a ternary can do when properly
used and properly formatted. (I now favor the ?: form used by other
languages.)
Terry J. Reedy
Terry J. Reedy
More information about the Python-list
mailing list