4 ms·
The irony is that Python actually has open-braces, but they're spelled ":" instead of "{". And the syntax effectively enforces K&R style. When I write Python
by lisper 9y ago
The irony is that Python actually has open-braces, but they're spelled ":" instead of "{". And the syntax effectively enforces K&R style.
When I write Python I end every block with a "pass" statement so that emacs can auto-indent my code properly. The "pass" statement thus effectively becomes a close-brace. It drives Pythonistas into conniptions, but I never have to worry about reverse-engineering a block of code to figure out how to restore the proper block boundaries after a cut-and-paste has screwed up the indentation.
- deleted 9y ago[deleted]
- kamaal 9y agoThat's the bad part of not having braces. If boundaries exist they need to be clear. One shouldn't have to count the tabs that make up the level of indentation. Its already a challenge reading code. Counting invisible tabs makes it even worse.
- lisper 9y agoWell, you don't have to literally count them, you use the spatial reasoning hardware built in to your retina and visual cortex to just see that this bit of code is further over to the right than that bit of code. But of course that only works for pieces of code that are small enough to fit in your field of view.
- dorgo 9y agoOn the other hand high counts of invisible tabs may be a hint that the code needs refactoring..
- true_religion 9y agoPython specific IDEs will smartly indent code upon copy and paste.
- lisper 9y agoSure, but if you somehow manage to screw up the indentation then no IDE can possibly restore it for you because the information needed to do it is lost. And there are a LOT of ways that indentation can get screwed up. If your code ever leaves the Python ecosystem (e.g. gets published on the web) then all bets are off because the entire digital world outside of the Python-sphere is built on the assumption that whitespace is fungible.
- true_religion 9y agoI don't know about that. I've never screwed up identaton so much that I couldn't correct it simply by selecting everything I just pasted and hitting tab a couple times in my IDE (PyCharm). Similarly, copying code from StackOverflow, Github and random blogs have all worked without any issues. So it can't be as easy as you imply.
- lisper 9y agoWhat can I say? It happened to me a lot before I started using the pass trick.
- jonny_eh 9y agoHow does an IDE know if a given line belongs in an if block or not?
- true_religion 9y agoIt'll indent up to the most relevant valid block as possible, given your cursor at the moment. If you click within an if block, it'll indent into that if-block. The indentation strategy also looks to produce syntactically valid code, so long as what you are pasting lacked indentation errors.
- tomn 9y agoEnforces K&R style? I don't know what you mean... def foo(bar)\ : if (bar > 5)\ : return bar + 1; # else\ : return bar - 1; # #
- lisper 9y agoI'm not sure whether to laugh or cry
- your-nanny 9y agoThat is clever. Ugly, yes. But clever.