PEP 308: A PEP Writer's Experience - PRO

Bernhard Herzog bh at intevation.de
Tue Feb 11 09:33:22 EST 2003


> > holger krekel wrote:
> > 
> > > because it easily leads to
> > > 
> > >     if obj.method() if hasattr(obj, 'method') else None:
> > > 
> > > which you have a harder time to justify as nice, not?

Almost everybody -- and especially programmers coming from C! -- will
write that as

if hasattr(obj, 'method') and obj.method():

In C && and || are short circuiting but they always evaluate to 0 or 1.
The do not evaluate to one of their arguments as in Python.

I think the main reason "a and b or c" is used at all is because it's in
the FAQ and because it's always given as one of the work arounds when
the ifelse expression topic come up on c.l.py.

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/




More information about the Python-list mailing list