5 ms·
Isn't that a strawman? Wouldn't a more apt example be: def foo(): nums = [...] sum = 0 for x in nums: print(x)
by mangodrunk 8y ago
Isn't that a strawman? Wouldn't a more apt example be:
def foo():
nums = [...]
sum = 0
for x in nums:
print(x)
sum += x
I don't know python well, but it seems like there could be subtle issues with indentation that wouldn't cause a compiler error, but would cause the wrong output.
- jMyles 8y ago1) This is not the case that the author presented, though - it was specifically about 3 spaces instead of 4. This tells me that the author simply doesn't hang out with python people. 2) On the substance of your concern: I think the evidence is clear (although I'm aware of no study) that having both syntactical control characters (typical curlies) and style-only indentation is more likely to lead to the outcome that you present, because the eye will always gravitate to read by indentation, whether it's syntactical or not. So have the indentation be the syntax makes this problem more easily avoided, not less. IE: def foo() { nums = [...] sum = 0 for x in nums { print(x) } sum += x }
- maemilius 8y agoThe difference here is that the former (your example) is automatically correctable whereas the latter is not. If I highlighted that entire function in my editor of choice and hit TAB I'd get exactly the "correct" indentation. Almost any modern editor can automate this for you. It's one of the benefits of having a syntax for blocks. Yeah, it's extra typing for something you're going to do anyways, but now the computer can manage the style for you. Also, and this is just my personal experience, I've learned to read by those control characters. I have a really hard time reading python because I'm subconsciously looking for the control characters! I really think that part is really down to how you learned to program and what language you use on a daily basis. As an aside, I think something like this is much more demonstrative of the issue you're suggesting: def foo() { nums = [...] sum = 0 for x in nums { print(x)} sum += x } It's still automatically fixable, but it's way less immediately apparent that something is wrong with the indentation.
- jMyles 8y agoRight, so you do your automatic fix and you end up with indentation that's the same as the python example. Isn't this a case for syntactical indentation?
- heavyset_go 8y agoNo, your example is not an issue of using 3 spaces for indentation versus 4 spaces like the author described. That snippet is an example of not understanding indentation-based scoping at all.
- vldr 8y agoConsider C, where {} are optional for single-statement bodies: void foo() { ... for( ... ) printf(..) sum += x } In python it's at least more visually obvious that the second statement isn't part of the for-body.