PEP 308: ternary operator
Terry Hancock
hancock at anansispaceworks.com
Thu Feb 20 01:25:19 EST 2003
More information about the Python-list mailing list
Thu Feb 20 01:25:19 EST 2003
- Previous message (by thread): PEP 308: ternary operator
- Next message (by thread): PEP 308: ternary operator
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
(I've not been reading c.l.py for a few days, so I'm not sure which branch of this thread to reply to) I have always hated C's ternary operator syntax! I never used it that much, and every time I did I had to look up the syntax. I REALLY hope Python does not simply adopt this syntax, as I think it will go on torturing newbies and casual programmers like myself if it does. Here's why it bothers me: 1) Is it <cond>? <a>:<b> or <cond>: <a>?<b> I can never remember this! 2) Is it <a> ? <cond>: <b> or <cond>:<a>?<b>, or how about <a>:<cond>?<b> They all make exactly the same amount of sense to me. (Which is to say, not much). Note that I am not objecting to the functionality, but rather the symbols used to encode it. I find them highly arbitrary. Likewise, I find the current kludge of using <cond> and <a> or <b> to be annoyingly idiomatic. It works by a kind of trick, and not for the reason it reads well. It gets you when you're not expecting it, and surely that's a good reason not to use it. Again, this is for newbies and casual programmers, but my understanding of Python is that it's supposed to be newbie-friendly. I also find the (if <cond>: <a> else <b>) syntax confusing because it looks too much like the statement syntax, and I just can't warm up to the parentheses. I especially don't like the overloaded meanings of the keywords. I therefore would like to voice support for either of the following "new-keyword" options: d = <a> when <cond> else <b> This is my favorite -- thank you M. Pinard! I like the chaining which is superior to the C alternative of nesting the expressions. I also think this one reads the nicest, and does not cause confusion. Although: "where" is just as good as "when", BTW, and has the advantage of being used in nearly the same way in IDL. Not that we care, but I think it's true. I don't really prefer either over the other. The main thing I like is that it properly places the most common action (d=<a>) at the beginning where it reads nicely. Then we add the exceptional case onto the end. It has the feel of the try-except syntax in miniature. Each successive "when" introduces a fall-back. d = <cond> then <a> else <b> This is a concession. If you must have the <cond> <a> <b> order, please use this one. However, I don't like it as much, because: 1) It does not read as an expression in English. 2) "then", while not a Python keyword is part of an "if" block in so many other languages (especially Perl and shell languages), I feel it could be confusing to people switching back and forth. "when" does appears as a keyword in some languages, but is much more distinct (and also usually appears in expression syntaxes, I think -- just like "where" in IDL). Neither of these objections is a killer though -- I could learn the then-else syntax and be happy with it. I think that the improved clarity is worth the hassle of introducing a keyword. I would also like to say that I find a greater need for what the ternary operator does in Python than I do in C. I'm not sure why, but it comes up more often. Maybe I'm just remembering using it in Zope (where it is definitely useful in DTML -- and probably ZPT too). So far I've gotten away with the <cond> and <a> or <b> syntax, but I don't like the potential pitfalls -- it always adds an extra amount of testing to make sure I'm not fooling myself. Anyway, that's my two cents on it. Cheers, Terry -- Anansi Spaceworks http://www.anansispaceworks.com
- Previous message (by thread): PEP 308: ternary operator
- Next message (by thread): PEP 308: ternary operator
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list