The ternary operator: more than one way to do it?

Thomas Wouters thomas at xs4all.net
Mon Mar 10 19:12:42 EST 2003


On Mon, Mar 10, 2003 at 12:23:38PM -0800, Erik Max Francis wrote:
> Paul Boddie wrote:

> The oft-misquoted Python precept isn't "There's only one way to do it,"
> it's more like, "There's (usually) only one _right_ way to do it." 

debian:~$ python -c 'import this'
The Zen of Python, by Tim Peters
[..]
There should be one-- and preferably only one --obvious way to do it.
[..]

> The addition of a conditional operator to Python would resolve the issue
> of which is the right way to do it (conditional evaluation within an
> expression with short circuiting), so on the contrary, its addition
> would not only be not un-Pythonic, but highly Pythonic.

You could say the same about assignment-in-expressions,
statements-in-lambda, statements-in-arbitrary-expressions, shell-like string
interpolation, the ability to 'supress' exceptions[2], 'true' private data,
direct memory access, splicing operators[1], and a dozen more ideas. Adding
features for the sake of having only one obvious way to do something seems
like the wrong way to do it. I will gladly use three extra lines in the
(very rare) cases where 'a or b' doesn't cut it for me, if that means it
becomes that much harder to write code that looks like:

pluswverbs = index == 1 ? pluswverbs ? length(pluswverbs) > 1 ? 
{{@pluswverbs[1], {i, verbname}}, @pluswverbs[2..$]} : {{@pluswverbs[1], {i, 
verbname}}} : {{{i, verbname}}} : (index == length(pluswverbs) ? 
{@pluswverbs[1..$ - 1], {@pluswverbs[$], {i, verbname}}} : (index > 
length(pluswverbs) ? {@pluswverbs, {{i, verbname}}} : {@pluswverbs[1..index
- 1], {@pluswverbs[index], {i, verbname}}, @pluswverbs[index + 1..$]}));

(It's not Python, it's MOO, written by me about 8 years ago :) [1] @ is the
splicing operator, which in-place expands lists in other lists (like list
concatenation), {}'s are like Python's []'s, and $ and .. inside [] are -1
and : in Python, respectively)

[2] MOO had another 'cool' feature, where you could have a sort-of
try/except expression. `backtick-expression-singlequote' would catch any
exceptions raised by -expression- and just return the exception object as
the result of the expression. Alternatively, an entire verb (method or
function) could be set '-d', which meant every expression that raised an
exception would instead return it. And exceptions were all false and unequal
and all that, of course. Sort of a cross-over between Python's exceptions
and Perl's 'undef'. Oh, and you could do some 'except'-like work inside the
'try'-expression too, to return the result of another expressoin if the
exception matched it, using `expression ! EXCEPTION => expression'. If you
really want to see it in action:

if (`({owner, perms, verbname} = verb_info(i, count))[1].wizard !
    E_INVIND => player:tell(verb, ": E_INVIND Raised at ", i, ":", verbname,
    "!")' && !i.owner.wizard && !(i.owner in {#38, #13398, #1560, #355,
    #19449, #71, #100, #111, #8772}) && !`this:registered_wizverbs(i, count)
    ! E_VERBNF => 1')

This actually came from the same verb as the previous example. It was well
before I learned C or Perl, let alone Python :)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!





More information about the Python-list mailing list