[Python-Dev] vox populii illiterati

holger krekel pyth@devel.trillke.net
Mon, 10 Feb 2003 04:17:46 +0100


Guido van Rossum wrote:
> > Right.  OTOH there seems to be the fear (especially from the 
> > more experienced folks maintaining large code bases) that
> > the ternary op *will* be used in non-assignments despite often
> > beeing bad style.  E.g.  starting from 
> > 
> >     if obj.method():
> >         ...
> > 
> > and realizing that 'obj' might not have "method" some might write:
> > 
> >     if obj.method() if hasattr(obj, 'method') else False:
> >         ...
> > 
> > which many consider a bad thing to be valid.  There are a *lot*
> > of variations on this theme (with while/list-comps/lambda) and 
> > people have reacted with punctuation (?:),  new keywords (when) 
> > and any mixture between those.  Clearly, you don't need the 
> > ternary operator for the above because there is an obvious 
> > other solution:
> > 
> >     if hasattr(obj, 'method') and obj.method():
> >         ...
> >         
> > and thus people indicated in various threads that having the ternary op
> > prevents people from getting to better and easier solutions. 
> 
> I'm sorry, but I am not at all swayed by the argument that this can be
> misused.  That argument has been used against every new proposal since
> Python 1.0.  But the existing constructs can be misused just as well.
> You can't prevent a poor programmer from writing poor code.

Sure, any construct can be abused.  Still there are gradual differences.
For example, introducing a new statement usually isn't as harmful 
to readability as new forms of expressions can be.  The latter can 
be used almost eveywhere. 

> The example you give is particularly unlikely because the 'and' idiom
> is well established for this particular case.

If somebody knows the right idiom then he will use it, sure.  But
somebody learning the ternary-op ("oh i know that from C, cool") 
might not even get to the point of knowing about python's special
and/or behaviour ("Oh i know boolean operations from C, right"). 
The ternary op then becomes the "obvious way to do it".  Thus
i am afraid that

     if obj.method() if hasattr(obj, 'method') else False:

is not that unlikely for a C-programmer learning Python in 2004
(in a dimension where PEP308 is effective).  

regards,

    holger