I don’t understand why people complain about their Python code breaking because it relies on indentation instead of explicit {} syntax. I’ve never had an issue with it and it’s not just because I’m used to it because Python is the only language I use that relies on whitespace like that. I think the complainers just don’t know how to indent properly, which makes me really glad they’re writing in a language that forces them to instead of pushing unreadable garbage in other languages.
I’ve been coding around 25 years and got my start in perl. I absolutely hated python when I first used it. I use it all the time now. I still prefer my curly braces but I don’t have any trouble with python or mind the whitespace anymore. I just run it through ruff every save. I do the same with go everything goes through gofumpt. I really think a lot of it is a generational thing. Older people are just used to curly brackets.
I do get peoples complaints about the packaging. Unless you’re a dev already it’s a bit extra to deal with shuffling virtual environments because the system python environments almost never work out of the box, at least in the last few distros I’ve used. Once I adjusted though it’s no problem. I run half my dev stuff in toolboxes with their own environment anyway.
its just not as clear especially when u are indenting stuff inside stuff with a bunch of conditions everywhere its hard to read just from indentation, and with the () or {} u can just click on it and most text editors will show u from where to where its going.
Try finding a bug related to indentation in a 15 year old python codebase by the worst programmers on the planet. You won’t think that there’s no issues with it after that point. In any other language you literally just reformat and you’ll see the bug. That’s not the case in Python.
I once wrote a bot in python tdownloaded a ical file, looked for chances and informed me if found. The space indentation made it hell to follow the code in my opinion.
What about spaces made it hard? What language would have been easier? In curly brace languages, 99% of the time, a curly brace is followed by a line break and an indent. Python is similar except it’s typically a colon, line break, then indent.
What I have learned is: If the code is indented too deeply, it’s a code problem, not the language.
Correct, I linked the source of the quote. My implication is the general idea is applicable here. Is python one of these languages where it is idiomatic to nest code deeply?
Too much nested code was likely part of the problem, but not being able to easily tell where a block of coded ended without seeing the top of the block made it difficult to work with.
That’s fair! Takes time to get used to. Modern editors make this easier by highlighting the current indent level, or can even make the top X lines of the current closure “stick” to the top of the editor for those really long blocks.
Having those features would really have made it easier. I have only ever written my python code in Sublime Text. While it is a sublime text editor, at is not ideal (compared to how it is to write Java in IntelliJ) for Python. (Maybe with addons but I never delved into that more than a few.)
I mainly found it annoying while writing code, because the lack of braces makes it difficult to tell when a scope ends. Plenty times, I’ve wanted to add something to the end of a for-loop, but had too little indentation.
Usually this means I get a runtime error, because it can’t access some variable from the loop-scope. But worst case, it only executes once after the loop and I don’t notice the problem.
Another big thing I miss when not having explicit braces, is opening up new/anonymous scopes to isolate variables, which helps prevent mistakes down the line + reduces code complexity.
For example, this is a thing I do quite regularly:
let client = {
let client = new Client()
lettimeout = config.load("client.timeout")
client.set_timeout(timeout)
client //implicit return value of this scope when evaluated as an expression
}
client.request_something()
It allows me to visually group the initialization code for the client and I don’t need to have the timeout variable in scope afterwards. Depending on the language, you can also have the client variable mutable inside the scope and then immutable outside.
Yes, this could be pulled out as a function to achieve something similar, but in my experience people (including me) will often just not do that, because it’s only the timeout variable or whatever.
wanted to add something to the end of a for-loop, but had too little indentation
To address this, I prefer reducing length & depth of nested code, so the for/while is rarely ever not visible along with everything inside it. Others have success with editors that draw indentation lines.
opening up new/anonymous scopes
I occasionally use Python nested functions for this purpose
I can see why you’d prefer braces in that case. I actually personally prefer {} over indentation as a matter of opinion, I just see them both as working fine 99% of the time. I’d also definitely take indentation over some shenanigans like start/end to define scopes.
I don’t understand why people complain about their Python code breaking because it relies on indentation instead of explicit {} syntax. I’ve never had an issue with it and it’s not just because I’m used to it because Python is the only language I use that relies on whitespace like that. I think the complainers just don’t know how to indent properly, which makes me really glad they’re writing in a language that forces them to instead of pushing unreadable garbage in other languages.
I’ve been coding around 25 years and got my start in perl. I absolutely hated python when I first used it. I use it all the time now. I still prefer my curly braces but I don’t have any trouble with python or mind the whitespace anymore. I just run it through ruff every save. I do the same with go everything goes through gofumpt. I really think a lot of it is a generational thing. Older people are just used to curly brackets.
I do get peoples complaints about the packaging. Unless you’re a dev already it’s a bit extra to deal with shuffling virtual environments because the system python environments almost never work out of the box, at least in the last few distros I’ve used. Once I adjusted though it’s no problem. I run half my dev stuff in toolboxes with their own environment anyway.
its just not as clear especially when u are indenting stuff inside stuff with a bunch of conditions everywhere its hard to read just from indentation, and with the () or {} u can just click on it and most text editors will show u from where to where its going.
That’s an anti pattern in basically every language though. The fix is to simplify those conditionals, not use a curly-bracketed language.
Try finding a bug related to indentation in a 15 year old python codebase by the worst programmers on the planet. You won’t think that there’s no issues with it after that point. In any other language you literally just reformat and you’ll see the bug. That’s not the case in Python.
I have never had any problems with it either but don’t get me started on YAML.
I once wrote a bot in python tdownloaded a ical file, looked for chances and informed me if found. The space indentation made it hell to follow the code in my opinion.
I don’t get it because my phyton code is indented exactly the same as all my other code. Each block of code one tab in/out, how else would you do it?
What about spaces made it hard? What language would have been easier? In curly brace languages, 99% of the time, a curly brace is followed by a line break and an indent. Python is similar except it’s typically a colon, line break, then indent.
What I have learned is: If the code is indented too deeply, it’s a code problem, not the language.
Torvalds infamously wrote:
Torvalds wrote this about C. There’s plenty languages where it’s much more common and less of a complexity indicator to open up scopes.
Correct, I linked the source of the quote. My implication is the general idea is applicable here. Is python one of these languages where it is idiomatic to nest code deeply?
The Zen of Python:
From the python I have seen and written, deep nesting is avoided.
Too much nested code was likely part of the problem, but not being able to easily tell where a block of coded ended without seeing the top of the block made it difficult to work with.
That’s fair! Takes time to get used to. Modern editors make this easier by highlighting the current indent level, or can even make the top X lines of the current closure “stick” to the top of the editor for those really long blocks.
Having those features would really have made it easier. I have only ever written my python code in Sublime Text. While it is a sublime text editor, at is not ideal (compared to how it is to write Java in IntelliJ) for Python. (Maybe with addons but I never delved into that more than a few.)
I mainly found it annoying while writing code, because the lack of braces makes it difficult to tell when a scope ends. Plenty times, I’ve wanted to add something to the end of a for-loop, but had too little indentation.
Usually this means I get a runtime error, because it can’t access some variable from the loop-scope. But worst case, it only executes once after the loop and I don’t notice the problem.
Another big thing I miss when not having explicit braces, is opening up new/anonymous scopes to isolate variables, which helps prevent mistakes down the line + reduces code complexity.
For example, this is a thing I do quite regularly:
let client = { let client = new Client() let timeout = config.load("client.timeout") client.set_timeout(timeout) client //implicit return value of this scope when evaluated as an expression } client.request_something()
It allows me to visually group the initialization code for the client and I don’t need to have the
timeout
variable in scope afterwards. Depending on the language, you can also have theclient
variable mutable inside the scope and then immutable outside.Yes, this could be pulled out as a function to achieve something similar, but in my experience people (including me) will often just not do that, because it’s only the
timeout
variable or whatever.To address this, I prefer reducing length & depth of nested code, so the
for
/while
is rarely ever not visible along with everything inside it. Others have success with editors that draw indentation lines.I occasionally use Python nested functions for this purpose
I can see why you’d prefer braces in that case. I actually personally prefer {} over indentation as a matter of opinion, I just see them both as working fine 99% of the time. I’d also definitely take indentation over some shenanigans like
start
/end
to define scopes.