Python Forum
Python indent question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python indent question (/thread-5767.html)



Python indent question - iicveji - Oct-20-2017

Hello. I am pretty new to Python and I've found something that is probably pretty simple but eludes me. The problem concerns enclosing existing code in a new loop structure.

I come from a Java background. One of the things I do all the time in Java is build a small section of code for testing and debugging purposes. When I have it clean I do a cut/paste into a loop (if-then,while,for,etc) where it will be called from. The nice thing about my Java IDE is that when I do a cut/paste it automatically corrects the indentation.

I've been developing my Python in Spyder, which does have Source-->Fix Indentation option, but when I cut/paste some sections of code it doesn't "fix" them correctly. My suspicion is that in Java this indentation fix is easier because of the enclosing brackets that clearly delineate the boundaries of the enclosing structure, whereas in Python it seems as though the indentation is the only delineation.

So my question is, is there a way to mark with brackets or some such the beginning and end of a section so that it would be possible to cut/paste code into it and have the editor recognize the edges?

Thanks!


RE: Python indent question - ichabod801 - Oct-20-2017

No, there is nothing that really delineates a block of code in Python except indentation.

I'm not sure about Spyder, but in other applications when I copy and paste it leaves the pasted code selected. Then I can use tools (usually tab and shift-tab) to indent or unindent the selected block to it's correct indentation level. But I always figure out the correct indentation level myself, I never use tools in the editor.


RE: Python indent question - iicveji - Oct-21-2017

Well, at least now I know. Appreciate the help. Thanks!