4 ms·
One thing I dislike in Python's syntax is ":" at the end of some composite statements, i.e. "if X:" or "else:". "else" is reserved word, why do I need to append
by nivertech 16y ago
One thing I dislike in Python's syntax is ":" at the end of some composite statements, i.e. "if X:" or "else:".
"else" is reserved word, why do I need to append ":"?
Also how to do one-liners? ;)
- erikano 16y agolambda functions are pretty neat for one-liners.
- jbail 16y agoYou can do one liners with lambda functions in Python. e.g: lambda x: x*2 http://diveintopython.org/power_of_introspection/lambda_functions.html http://diveintopython.org/power_of_introspection/lambda_func...
- ronnix 16y agoYou can have a one-line "if" statement [1]: if a == 42: print "Yes" ; do_stuff() It's true that you cannot have a "else" statement on the same line, however. But in some cases, you could use a conditional expression instead [2]: x = f() if a == 42 else g() [1] http://docs.python.org/reference/compound_stmts.html http://docs.python.org/reference/compound_stmts.html [2] http://docs.python.org/reference/expressions.html#conditional-expressions http://docs.python.org/reference/expressions.html#conditiona...
- nivertech 16y agoin this case plain old C syntax is much more readable: x = (a==42) ? f() : g();
- orangecat 16y agoIn this case I think the PEP8 spacing guidelines are counterproductive: f() if a==42 else g() looks much more readable to me than f() if a == 42 else g()
- pyre 16y agoI think that it's much more readable due to familiarity more than anything else. The C syntax for the ternary operator was the one thing that it took me the longest time to get used to when learning to program. I usually just ignored it because it was too hard to remember what it did. That said, I can easily read it now, but I still feel that the Python syntax is more understandable. Which is more readable: > condition ? value1 : value2 or > value1 if condition else value2 Maybe the latter is just more readable to me because I'm used to reading Perl all day long that looks like: > die unless condition; > return true if condition; > return false > if condition1 && condition2 && condition3; That said, I'm also used to reading: > return condition1 ? value1 > : condition2 ? value2 > : condition3 ? value3 > : value4; and I still find the Python ternary operator more readable: > return value1 if condition1 else > value2 if condition2 else > value3 if condition3 else > value4
- j_baker 16y agoPersonally, I still have to think about what the ternary operator means every time I see it. It's just so nonobvious.
- ronnix 16y agoYou can also write it this way, which is more similar to C: x = (a==42) and f() or g()
- deleted 16y ago[deleted]
- juanefren 16y agoAs a non expert developer, I hate to see one-liners (using lambda expressions) when I am inspecting open source code. So I would prefer they were not possible :)
- xiongchiamiov 16y agoWhile one-liners are almost always bad, the lack of non-crippled anonymous functions is the one thing that really bothers me about Python. They don't /have/ to be on one line; see Ruby's each blocks, for instance.
- koenigdavidmj 16y agoThe colon is there to make it easier on editors that want to do auto-indentation. Their logic can simply be that if the previous line ends with a colon, then add a new indentation level.
- nivertech 16y agoNow we supposed to help editors? ;) I thought it should be otherwise... editors supposed to be helping programmers. Programmers can learn syntax. Regular humans can't. Both Python and Ruby failed to provide a simple way of building internal DSLs. In Python humans need to be specific about indentation and ":" and in Ruby they need to remember all these stupid sigils.
- xi 16y agoJust like semantic indentation, ":" is ABC's heritage. It was discovered that newbie programmers comprehend block operations better when they end with ":". http://python-history.blogspot.com/2009/02/early-language-design-and-development.html http://python-history.blogspot.com/2009/02/early-language-de...
- nivertech 16y agoback in time, there was several schools for language syntax design for composite statements: 1. "Open" statements (Pascal): IF x THEN s1 ELSE s2; IF x THEN BEGIN s1; s2 END ELSE BEGIN s3; s4 END WHILE c DO s; WHILE c DO BEGIN s1; s2 END The problem with this design was need to use blocks (BEGIN ... END) and complex rules in case of nesting. 2. "Closed" statements (Algol 68) IF x THEN s1; s2 ELSE s3; s4 FI // END IF WHILE c DO s1; s2 OD // END WHILE You can see new kind of parenthesis IF-FI, DO-OD, etc. I more like "closed" syntax. But C syntax with it's brackets blocks "{...}" instead of "BEGIN...END" made this less an issue.
- j_baker 16y agoI think the FAQ answers this question nicely: http://docs.python.org/faq/design.html#why-are-colons-required-for-the-if-while-def-class-statements http://docs.python.org/faq/design.html#why-are-colons-requir...
- reinhardt 16y agoIf you like Python but not the ":", check out the Cobra language: http://cobra-language.com/samples/WordCount/ http://cobra-language.com/samples/WordCount/. FWIW I do like the ":", reminds me of a TODO list; in fact my plain text notes tend to look like Python ;)
- riobard 16y agoEvery once in a while this will be brought up and debated to death... See my last attempt in Feb 2009: http://mail.python.org/pipermail/python-ideas/2009-February/002642.html http://mail.python.org/pipermail/python-ideas/2009-February/...