mwh21 at cam.ac.uk
Wed Nov 24 12:23:38 EST 1999
Dennis Lee Bieber <wlfraed at ix.netcom.com> writes:
> On Wed, 24 Nov 1999 10:23:27 +0100, "Fredrik Lundh"
> <fredrik at pythonware.com> declaimed the following in comp.lang.python:
> > Brad Knotwell <knotwell at ix.netcom.com> wrote:
> > > Similarly, the only thing I've found missing from Python is C's
> > > ternary operator, it would be nice to be able to do something like
> > > the following:
> > >
> > > def max(x,y): return ((x>y) ? x : y)
> > now that was one lousy example ;-)
> Also potentially unsafe if either x or y is some expression when
> invoked. At least, I seem to recall some suggestion to always put macro
> arguments in parens...
> def max(x,y): return(((x)>(y)) ? (x) : (y))
Eh? This is Python, not some yucky character-based macro equipped UNIX
implementation language <wink>.
One thing that
x = (var and (ifexp,) or (elseexp,))
x = choose(var,ifexp,elseexp)
besides brevity is that the former will only evaluate one of ifexp or
elseexp, but that isn't going to matter for max (obviously...).
It can be handy in things like:
x = (hasattr(x,"foo") and (x.foo,) or (None,))
though in this case
x = getattr(x,"foo",None)
is clearly better...
More information about the Python-list