PEP308: Yet another syntax proposal

holger krekel pyth at devel.trillke.net
Mon Feb 10 15:29:00 EST 2003


Dave Brueck wrote:
> On Mon, 10 Feb 2003, holger krekel wrote:
> 
> > > Short-circuit evaluation is never "necessary" in _any_ language - it's
> > > just very, very useful sometimes. Python's 'and' operator doesn't _have_
> > > to be a short-circuit operator, but it's far more useful because it is.
> > >
> > > > and therefore iif() unreasonable.
> > >
> > > Several of the previous examples have already illustrated this:
> > >
> > > z = iif(x, x.getPref(), 'Not specified')
> > >
> > > is broken without short-circuiting.
> >
> > yes, but it is easily written with an and-expression
> >
> >   z = not x and 'Not specified' or x.getPref()
> 
> Yikes! The above line of code is a great argument _in favor of_
> if-expressions on the basis of readability and maintainability. I couldn't
> encourage anyone to use that form.

It's the "inverse" of 

    z = obj.method() if hasattr(obj, 'method') else None

which i consider dubious compared to

    z = hasattr(obj, 'method') and obj.method()
 
> > and a ternary op would simply add another possibility.
> 
> ...and hopefully deprecate the usage of your above example, the "simulated
> conditional expression".

citing from my last mail:

    and a ternary op would simply add another possibility.

Even if the old way is bad it will be useful in other situations
Thus you still have two constructs.  In some cases PEP-308 is better,
in others the and-idiom.  But don't forget that *both* will be abused. 

    holger





More information about the Python-list mailing list