Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python indent question
#1
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!
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Well, at least now I know. Appreciate the help. Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  getting unexpected indent errors trying to move cells up jensengt 4 821 Jun-28-2023, 12:05 PM
Last Post: deanhystad
  xml indent SubElements (wrapping) with multiple strings ctrldan 2 1,380 Jun-09-2023, 08:42 PM
Last Post: ctrldan
  IndentationError: unexpected indent dee 3 2,227 May-02-2022, 02:15 AM
Last Post: dee
  Avoid multiple repeat in indent Frankduc 8 2,789 Jan-18-2022, 05:46 PM
Last Post: Frankduc
  IndentationError: unexpected indent jk91 1 2,338 Feb-27-2020, 08:56 PM
Last Post: buran
  could not fix unexpected indent Bayan 1 3,149 Nov-08-2019, 01:45 PM
Last Post: ichabod801
  unable to indent json data dataplumber 4 2,939 Oct-22-2019, 01:55 PM
Last Post: dataplumber
  IndentationError: unexpected indent (Python) segs 8 17,120 Aug-11-2017, 03:13 PM
Last Post: segs

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020