A = X > Y ? X : Y

Quinn Dunkan quinn at zloty.ugcs.caltech.edu
Tue Feb 8 22:55:35 EST 2000


On Tue, 08 Feb 2000 17:04:01 -0800, Curtis Jensen
<cjensen at be-research.ucsd.edu> wrote:
>I fear that this question has already been asked, but is there and
>equivalant one line command that is equivalant to the C command:
>
>a = x > y ? x : y

uh, how about a = max(x, y) ?

This isn't equivalent because both x and y are evaluated, but it is a lot
clearer.  If you're looking for a general equivalent to ?:, the short answer
is that there isn't one.  There are some ugly hacks, and an almost-equivalent
(a and b or c), all of which are in the faq.  Generally, though, you need to
do if else.  Yes it's more typing.  But most of the time it will be more
readable too.



More information about the Python-list mailing list