PEP 308 - suggestion for generalising the ternary operator

Damien Morton newsgroups1 at bitfurnace.com
Wed Feb 12 11:54:05 EST 2003


My use cases recast into the forms you counterproposed:

First form:
x = (y>0:sqrt(y), 0)
ext = (is_html(text):"html", "txt")
print "widget%s loaded" % (num>0:"s", "")
ext = (is_html(bytes):"html", is_mpeg(bytes):"mpg", is_jpeg(bytes):"jpg",
"txt")
sign = (x<0:-1, x>0:1, 0)

Second Form (using else clause):
x = (y>0:sqrt(y), else:0)
ext = (is_html(text):"html", else:"txt")
print "widget%s loaded" % (num>0: "s", else:"")
ext = (is_html(bytes):"html", is_mpeg(bytes):"mpg", is_jpeg(bytes):"jpg",
else:"txt")
sign = (x<0:-1, x>0:1, else:0)

Hmm, Of the two forms you proposed, I think I prefer the first one for its
consiseness.

Whilst the parralel to dictionary construction was intentional, the idea of
the "?" oeprator was to visually distinguish it from dictionaries, while
trying to evoke a similar effect.



"Oren Tirosh" <oren-py-l at hishome.net> wrote in message
news:20030212143111.GB61731 at hishome.net...
> On Wed, Feb 12, 2003 at 02:53:42AM -0800, damien  morton wrote:
> > One Python implimentation of the ternary operator expresses the
> > problem as selecting from a list of alternatives:
> > [tval,fval][not cond]
> >
> > I was thinking over how to generalise this into a more powerfull and
> > readable form and came up with this syntax:
> >
> > (cond1?value1, cond2?value2, cond3?value3, default_value)
> >
> > whose degenerate ternary operator form is:
> >
> > (cond? true_value, false_value)
> >
> > I think the parentheses should be mandatory.
>
> Hmm... If you replace ? with : the result looks surprisingly like
> a dictionary, with parens instead of curly braces:
>
> (cond:value, cond:value, cond:value, cond:value, default_value)
>
> Or maybe:
>
> (cond:value, cond:value, cond:value, cond:value, else:default_value)
>
> Oren
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>








More information about the Python-list mailing list