PEP 308 - suggestion for generalising the ternary operator

Damien Morton newsgroups1 at bitfurnace.com
Wed Feb 12 22:11:06 EST 2003


These "c and x or y" forms, while currently serving as the only way to
implement the conditional selection operator, are far from intuitive.

The goal of the PEP 308 discussion is to come up with a syntax for
conditional selection which is clear, consise, and intuitive. I dont think
obscure Pythonic workarounds are a good form to start from.


"Bengt Richter" <bokr at oz.net> wrote in message
news:b2efbd$cit$0 at 216.39.172.122...
> On Wed, 12 Feb 2003 11:54:05 -0500, "Damien Morton"
<newsgroups1 at bitfurnace.com> wrote:
>
> >My use cases recast into the forms you counterproposed:
> >
> Grabbing the opportunity for comparison with my latest & greatest,
> where {x} means "treat x as True in a logical expression, but don't
> change the value." Thus the ternary is
>     c and {x} or y
> or optionally for style symmetry
>     c and {x} or {y}
>
> >First form:
> >x = (y>0:sqrt(y), 0)
>  x = y>0 and {sqrt(y)} or 0
>
> >ext = (is_html(text):"html", "txt")
>  ext = is_html(text) and {"html"} or "txt" # {} not really necessary,
since "html" is guar True
>
> >print "widget%s loaded" % (num>0:"s", "")
>  print "widget%s loaded" % (num>0 and {"s"} or "") # {} not necessary
>
> >ext = (is_html(bytes):"html", is_mpeg(bytes):"mpg", is_jpeg(bytes):"jpg",
"txt")
>  ext = is_html(bytes) and {"html"} or is_mpeg(bytes) and {"mpg"} or
is_jpeg(bytes) and {"jpg"} or "txt"
>  # none of the {} were really necessary in the above
>
> >sign = (x<0:-1, x>0:1, 0)
>  sign = x<0 and {-1} or x>0 and {1} or {0} # none of the {}'s really
necessary
> >
>
> Just grabbing an opportunity for comparing ;-)
>
> Regards,
> Bengt Richter
> --
> http://mail.python.org/mailman/listinfo/python-list
>








More information about the Python-list mailing list