4 ms·
I find it a pain and error prone when having to change indentation in python code. E.g. when adding an 'if' in front of a block of code. So easy to miss a line
by jahnu 12y ago
I find it a pain and error prone when having to change indentation in python code. E.g. when adding an 'if' in front of a block of code. So easy to miss a line or mess up the indentation in the block itself and then you might not spot an error until run time.
- djur 12y agoI was under the impression that Python emits an error about inconsistent indentation at compile time (the initial parsing and interpretation of a script file), not runtime (+x time units later, after the program has started). Is that incorrect?
- lutusp 12y agoSuch errors are caught at compile time. I think the prior poster was speaking in a general sense in which runtime means any time after source file editing.
- jahnu 12y agoI'm talking about something like this.. def foo(): do_this() and_this() later I change it.. def foo(): if something(): do_this() and_this() oops... and_this() should have been in the if block and I won't find out till I run it. (imagine a much bigger more complex example of the above function) In large pieces of code this can be easy do. If you are forced to use parenthesis it's much more difficult to make this error. One could argue that experience prevents you from doing this but I have sadly found this not to be the case in practice.
- lutusp 12y ago> If you are forced to use parenthesis it's much more difficult to make this error. By "parentheses" you mean delimiters, which create visible boundaries to defined areas in code. Parentheses are an example of delimiters, but not all delimiters are parentheses. Bash has if ... then ... else ... fi Ruby has if ... else ... end C/C++/Java have (logical test) { controlled area }, nested to any practical depth. And so forth. Python doesn't. > One could argue that experience prevents you from doing this but I have sadly found this not to be the case in practice. This is an argument against complex functions that do a lot, as opposed to breaking program logic up into smaller blocks that are easier to understand and control. The old argument against this practice was that a large function that did everything was faster than the same logic broken into smaller blocks. A modern compiler will generally prevent this from happening.
- jahnu 12y agoWell I used the word parenthesis since the topic was Lisp. > This is an argument against complex functions that do a lot Functions large and complex enough to make this problem significant seem to be the reality I have to deal with when programming in the large. It's only my opinion but a language feature that improves my real world experience at no cost is a bonus.
- andreasvc 12y agoI fail to see your point. You could accidentally put a brace in the wrong spot, or use an if statement without braces in a C-like language, and it would stand out visually much less than the code you cite above.
- crististm 12y agoAfter you write a fair amount of Python code (<5000 lines I guess), that idiom will strike you immediately.
- andreasvc 12y agoIt's not a pain if you use an editor which lets you indent a whole block of code at once; e.g., in vim, use shift-V to select lines of code, then >> or << to in- or dedent. This is handy for all programming languages, of course.
- Symmetry 12y agoHaving groups of lines with the same indentation act as text objects helps too. http://vim.wikia.com/wiki/Indent_text_object http://vim.wikia.com/wiki/Indent_text_object