PEP 308 - suggestion for generalising the ternary operator

Bengt Richter bokr at oz.net
Wed Feb 12 16:46:21 EST 2003


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




More information about the Python-list mailing list