Python paradigms

Andrew Dalke dalke at acm.org
Sun Apr 16 06:17:22 EDT 2000


Aahz Maruch wrote:
>def expression_if( test, true, false ):
>  if test: return true
>  else: return false
>
>x = expression_if( a is not None,
>   a[i].weeble, 0) + expression_if( b is not None, b[i].wombat, 0)


Suppose a is None.  The a[i].weeble will give an exception.

You can imagine:

def expression_if(test, true_string, false_string):
  # some nasty code to get the locals of the calling function
  if test:
    return eval(true_string with the given locals)
  else:
    return eval(false_string with the given locals

x = expression_if(a is not None, "a[i].weeble", "0") + ...

but that's just too ugly.

                    Andrew
                    dalke at acm.org






More information about the Python-list mailing list