3 ms·
Python specific IDEs will smartly indent code upon copy and paste.
by true_religion 9y ago
Python 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.
- pleasecalllater 9y agoNope. Just try insert code formatter with 8 spaces into code formatted with 4, or 2. IDE will make something that compiles, it doesn't mean that it works properly.
- true_religion 9y agoJust tried that---typing like mad in a wordpad, with 12 spaces of indentation, then copying into PyCharm and it worked. Mixed spaces and tabs as a next go, and it worked too---converting the pasted tabs into spaces of my style. Smart-tabbing is pretty smart. Granted, I knew that so long as the indents were proper (i.e it compiles), that pasting at that point would give valid code but... I can't see how using braces would have been different. Just because it compiles, doesn't mean it works. I've pasted javascript incorrectly multiple time to produce valid, but incorrect for my needs code.