A = X > Y ? X : Y

Justin Sheehy dworkin at ccs.neu.edu
Tue Feb 8 21:24:41 EST 2000


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
recommended.

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
http://www.python.org/doc/FAQ.html#4.16
explains this quite well, and gives the correct expression.

-Justin

 




More information about the Python-list mailing list