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

Michal Wallace sabren at manifestation.com
Wed Feb 16 15:34:38 EST 2000


Fredrik Lundh wrote in message ...
>Michal Wallace <sabren at manifestation.com> wrote:
>> I betcha it can!... How about this?
>>
>> ###
>>
>> def  cStyleIIF(condition, iftrue, iffalse):
>>     if eval(condition):
>>         return eval(iftrue)
>>     else:
>>         return eval(iffalse)
>>
>>
>> x = 5
>> y = 20
>> a = cStyleIIF("x<y", "x", "y")
>>
>> ###
>>
>> You've got some extra overhead there, but if you really
>> want to do this on one line without evaluating both options
>> or using boolean short circuits, it might be worth it... :)
>
>the problem here is that you'll evaluate the
>expression in the function's own namespace,
>not the callers.
>
>(you can use trickery or guile to get around
>that, but I won't post that solution...)



Okay, I give up... How do you:

a) know what the caller's namespace even IS
b) evaluate something in that namespace?

Wouldn't that be useful in general for
experimenting with language enhancements,
even if the code were a little hairy?

-Michal





More information about the Python-list mailing list