A = X > Y ? X : Y

Stidolph, David stidolph at origin.ea.com
Wed Feb 9 14:41:31 CET 2000

Why not just use the max method?

a = max(x,y)

It is more obvious what the idea is, and has the advantage of allowing for
more than two variables:

a = max(x,y,z)

The opposite function min is also available.

I realize that the intent may be to use the test for something different
than less than / greater than, but for readability I would suggest a
multi-line if..else statement.  We have to maintain the code we write and
most of us have to remember about the ? operation in C and take extra time.

David Stidolph

-----Original Message-----
From: Justin Sheehy [mailto:dworkin at ccs.neu.edu]
Sent: Tuesday, February 08, 2000 6:25 PM
To: python-list at python.org
Subject: Re: A = X > Y ? X : Y

Curtis Jensen <cjensen at be-research.ucsd.edu> writes:

> 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

cjc26 at nospam.cornell.edu (Cliff Crawford) writes:

> a = (x > y and [x] or [y])[0]

Brad Howes <bradh at mediaone.net> writes:

> Its in the FAQ -- and its not as pretty:
>   a = ( ( x > y ) and x ) or y

François Pinard <pinard at iro.umontreal.ca> writes:

> >   a = ( ( x > y ) and x ) or y
> If really in the FAQ, it should not be.  It surely does not work when x
> is 0, and y is less than 0.

Brad's example is incorrect, and is not in the FAQ.  Cliff's example
is the correct one, although I agree with him that it is not to be

People ought to look at the FAQ when they talk about it.  They also
ought to look in it if they fear that their questions have already
been asked.  ;-)

The entry at
explains this quite well, and gives the correct expression.




More information about the Python-list mailing list