A = X > Y ? X : Y

Fredrik Lundh effbot at telia.com
Thu Feb 10 03:26:47 EST 2000


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...)

</F>





More information about the Python-list mailing list