[Python-Dev] vox populii illiterati
holger krekel
pyth@devel.trillke.net
Sun, 9 Feb 2003 18:57:32 +0100
Neal Norwitz wrote:
> On Sun, Feb 09, 2003 at 10:39:28AM -0500, Guido van Rossum wrote:
> > Is anybody at least collecting useful feedback? I'm willing to update
> > the PEP to at least mention sensible alternatives or arguments pro and
> > con, but I can't read the c.l.py messages.
>
> I have read most of the posts. It's a bit humorous to watch emotions
> get so stirred in both directions. It seems there are many
> python-devers following the discussion: Aahz, Andrew Koenig (ARK),
> Michael Hudson, Holger Krekel, and Tim have all posted. ARK seems
> to be the most pro, Aahz the most against. (I'm +0 on having
> a ternary operator in theory, -1 on PEP 308.)
>
> ARK can do the best job of summarizing the pro-side, but the
> strongest arguments I've seen are that using a ternary op can
> reduce bugs by not duplicating a variable on assignment:
>
> if cond:
> x = true_value
> else:
> x = false_value
>
> vs. (in C notation):
> x = cond ? true_value : false_value
>
> Since x is only mentioned once, you can't get it wrong.
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.
> Part of the reason for using if-expressions (the ternary op) is the
> programmer has a different mindset. They aren't thinking about
> control (as in an if statement). They are thinking about an
> expression and the ternary operator allows them to program what they
> are thinking.
Andrew Dalke posted a nice survey on 15 different ternary-op examples
in his 42.500 line C-code base (written by different programmers).
Although it's not about python code it hits a nerve because quite
some people come with a C background (different mindset) and miss
the ternary OP:
http://mail.python.org/pipermail/python-list/2003-February/145592.html
I think this link could be included in the PEP under something like
"Often (but not always) there are better Python-solutions than
with C regarding ternary OP.".
As no single person can follow all the c.l.py postings i think
we might "preprocess" and send Guido "important" links together with
a short summary (or just the latter) to be included in the PEP.
regards,
holger