[Python-Dev] vox populii illiterati (original) (raw)

Guido van Rossum guido@python.org
Sun, 09 Feb 2003 20:54:17 -0500


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.

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

> 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.".

Many of his examples are like perfectly valid use of ?: in C++, and good to follow too. (In fact, it seems that ?: has an advantage over the PEP's proposal because the ?: stand out more than the 'if' and 'else' keywords.) And most examples that questionable are questionable not because ?: is being misused but because the programmer didn't think hard enough -- but there's no evidence that the ?: prevented them from thinking. I agree with his final conclusion that ?: just isn't used often enough to warrant its existence. But then he blows it again by pointing out that it can be abused. Well OF COURSE it can be abused. See above.

one of Dalke's anti-examples is interesting (slightly reformatted):

indices = [(long_function_name(atom->realAtom(), atomCount, reactionMapping) if atom != OXYGEN else -1) for atom in (atom1, atom2, atom3, atom4)]

If anything, this suggests that the infix-if form is not ideal, because it hides the if-part too much.

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.

I'm not sure that Dalke's post adds much evidence to warrant inclusion in the PEP.

--Guido van Rossum (home page: http://www.python.org/~guido/)