[Python-Dev] conditional expressions? (original) (raw)

Guido van Rossum guido@python.org
Mon, 15 Oct 2001 16🔞48 -0400


I misunderstood about the need for parens. I had only been watching the thread in python-list with one eye. I thought it was just some python-list riff-raff throwing around ideas. Then I saw that Tim was part of the rabble ;-). I still didn't notice the "I've implemented it but not yet checked it in" bit until someone else did and asked about it. I then went looking for a PEP but didn't see one. I hadn't seen any discussion on python-dev either. (Maybe I missed it because python-list and python-dev mail wind up in the same mailbox.)

OK. I guess you were thinking that the parentheses were required as for 'if' in C?

Guido> If I saw a piece of code that read

Guido> x = if if if x == 1 then y else z then p else q then a else b Guido> I would get a strong urge to commit illegal violence against the Guido> author. If on the other hand I saw Guido> x = if (if (if x == 1 then y else z) then p else q) then a else b Guido> I might be willing to sit down and figure out what it meant -- Guido> maybe with the help of a parentheses-balancing command in my Guido> editor. Python's block structured statement syntax keep you from composing just monstrosities at the statement level. I'm not sure what makes them more appealing at the expression level. How many levels of parens would be required before you had a "strong urge to commit illegal violence" against someone using a parenthesized conditional expression?

As soon as there is an if inside the condition of the first if. Even parentheses don't fix this, really.

I guess that's what it boils down to for me. You can easily write much more inscrutable code with the new conditional expression than you could before.

"You can write Fortran in any language" -- we've been through this before. Potential abuse is not enough to prevent it -- likely abuse is.

I think that will make Python somewhat less of a CP4E language. I don't think it falls into the same category of the more esoteric features of the language (generators, metaclasses, * methods, etc) that beginners can safely ignore. Conditional expressions are pretty basic stuff, and this makes it easier to write fairly inscrutable conditional expressions.

It also makes it easier to write what you thinkk, IMO.

Guido> Sorry, I still don't get this at all. A conditional Guido> expression is still an expression. Curly brances are a Guido> statement-level concept. What am I missing? Where is Guido> the similarity between the use of { } and ( ) in Guido> C/Java/C++/Perl?

From the computer's viewpoint, they are syntactically and semantically, different, I agree. From the point of view of a human trying to understand what's written I think they will serve much the same purpose, however. White space is viewed is being essential enough to understanding that Python uses it for block structure. In particular, I can't nest two compound statements on the same line. With this new addition to expressions, it appears you will be able to create arbitrarily complex expressions all on one line.

Previous versions of Python in no way impaired your ability to create arbitrarily complex expressions all on one line.

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