Ternary Operator in Python

Erik Max Francis max at alcyone.com
Fri Apr 1 16:12:07 EST 2005


Ron_Adam wrote:

> I've used boolean opperations to do it.
> 
> 	result = (v == value) * first + (v != value) * second
> 
> Same as:
> 
> 	if v == value: result = first else: result = second

No, it isn't, because it isn't short circuiting.  If first or second had 
side effects, then the two would not be equivalent.

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
   If the sun comes up / And you're not home / I'll be strong
   -- India Arie



More information about the Python-list mailing list