if does not evaluate

Nicolas Fleury nid_oizo at yahoo.com_remove_the_
Tue Jun 8 18:18:53 EDT 2004


Robert Brewer wrote:
> If your main desire is to code in some other language while still using
> Python, write your own VB-style IIf function:

All the solutions you propose don't work (except the long form).  You 
cannot code an "iif" function.  Test:

def iif(cond, expr1, expr2):
     if cond: return expr1
     return expr2
def foo(x): return iif(x == 0, 0, 1.0/x)
foo(4)
foo(0)

See PEP 308.

Regards,
Nicolas



More information about the Python-list mailing list