I give up... was: A = X > Y ? X : Y

Andrew Csillag drew.csillag at starmedia.net
Wed Feb 16 22:02:29 EST 2000


Michal Wallace wrote:
> 
> >Even easier:
> >>>> x = 5
> >>>> y = 20
> >>>> a = x<y and x or y # a ? b : c --> a and b or c
> 
> :) yeah.. that post was from like a week ago, when we were
> talking about a way to do that without the boolean operators...
> Fredrik suggested that my IIF() routing could be made to
> work across namespaces, and I've been trying to figure
> it out ever since.. (well, at least somewhere in the back of
> my mind...)

Not too hard...  Just need to do the throw/catch/fiddle with traceback
dance:

import sys

def IIF(exp, t, f):
    try:
        raise "foo!"
    except:
        tb = sys.exc_traceback

    fr = tb.tb_frame.f_back
    locs = fr.f_locals
    globs = fr.f_globals

    expVal = eval(exp, locs, globs)
    if expVal:
        return eval(t, locs, globs)
    else:
        return eval(f, locs, globs)
    

def cli():
    a='0 lesser'
    b='1 greater'
    print IIF('b>a', 'b', 'a') #should print '1 greater'
    print IIF('b<a', 'b', 'a') #should print '0 lesser'

cli()

-- 
print(lambda(q,p):'pmt:$%0.2f'%(q*p/(p-1)))((lambda(a,r,n),t:(a*r/
t,pow(1+r/t,n*12)))(map(input,('amt:','%rate:','years:')),1200.0))




More information about the Python-list mailing list