PEP 308 - ternary operator

Mike Thompson none
Mon Feb 10 23:49:44 EST 2003


"Jim Jinkins" <j-jinkins at usa.net> wrote in message
news:vXZ1a.9151$5l.1679097 at twister.midsouth.rr.com...
>
> If this is not acceptable my second choice is a functional form:
>     iif(cond, expr1, expr2)
>
> This is not a function, it is a built-in macro.
>

It looks like a function though.  Enough to be confusing to those new to Python
(like me).

I'm really too new to Python to participate ... but wait, in some ways that's
ok because I can give an "experienced C++, but still learning Python"
perspective. So what the hell, I'll propose something ... how about

x = ?(cond, expr1, expr2)     # no longer looks like a function call

perhaps, even

x = ?( cond, expr1, expr2 ?)    # <---- maybe ')?'

perhaps, simply

x = ? cond, expr1, expr2 ?    # didn't like this initially, but it grew on me

All these suggestions involve adding an operator and I had no idea about the
parseablity/ambiguity of such additions, but if one of these suggestions was ok
then it would offer the following:

    1. Clearly not a function
    2. Inline ordering
    3. No new reserved word.
    4. Use of '?' which will be a hint to the ?:-like behaviour
    5. Use of ? which carries a sense of conditionality for those without C/C++
backgrounds.

BTW, I've explicitly avoided something enclosed in ( ) alone, like say (?
....?), because that makes the use of these expressions look peculiar when used
as function arguments. Two lots of brackets would result .....

func(( ..... )) # too strange

I really do hope I haven't made an idiot of myself here.

BTW, from my reading of this topic, the archetypical use-case turns a word into
its plural by taking on an "s" ... or not ... depending on a condition.  The
winning option should make this easy/neat IMO.make

I.e. using the 3rd form I give above ...

print "widget%s loaded" % ?num > 0, "s", ""?

Cheers,

Mike







More information about the Python-list mailing list