Is there a way around coding in Python without the tab, indent & whitespace criteria?

前端 未结 29 979
小鲜肉
小鲜肉 2020-12-31 14:05

I want to start using Python for small projects but the fact that a misplaced tab or indent can throw a compile error is really getting on my nerves. Is there some type of s

相关标签:
29条回答
  • 2020-12-31 14:41

    If you don't want to use an IDE/text editor with automatic indenting, you can use the pindent.py script that comes in the Tools\Scripts directory. It's a preprocessor that can convert code like:

    def foobar(a, b):
    if a == b:
    a = a+1
    elif a < b:
    b = b-1
    if b > a: a = a-1
    end if
    else:
    print 'oops!'
    end if
    end def foobar
    

    into:

    def foobar(a, b):
       if a == b:
           a = a+1
       elif a < b:
           b = b-1
           if b > a: a = a-1
           # end if
       else:
           print 'oops!'
       # end if
    # end def foobar
    

    Which is valid python.

    0 讨论(0)
  • 2020-12-31 14:43

    I'm currently using NotePad++. Is there maybe an IDE that would take care of the tabs and indenting?

    I liked pydev extensions of eclipse for that.

    0 讨论(0)
  • 2020-12-31 14:43

    pybraces

    It's unsupported.

    0 讨论(0)
  • 2020-12-31 14:44

    Emacs! Seriously, its use of "tab is a command, not a character", is absolutely perfect for python development.

    0 讨论(0)
  • 2020-12-31 14:44

    Not really. There are a few ways to modify whitespace rules for a given line of code, but you will still need indent levels to determine scope.

    You can terminate statements with ; and then begin a new statement on the same line. (Which people often do when golfing.)

    If you want to break up a single line into multiple lines you can finish a line with the \ character which means the current line effectively continues from the first non-whitespace character of the next line. This visually appears violate the usual whitespace rules but is legal.

    My advice: don't use tabs if you are having tab/space confusion. Use spaces, and choose either 2 or 3 spaces as your indent level.

    A good editor will make it so you don't have to worry about this. (python-mode for emacs, for example, you can just use the tab key and it will keep you honest).

    0 讨论(0)
  • 2020-12-31 14:44

    Strange - No one mentioned GEdit (Gnome) or OpenKomodo (Windows, Mac, GNU/Linux...). Both of them are great!

    OpenKomodo especially deals with tabs and spaces very well. And - it's free. Whee! When I need a lighter weight thingy, I just use GEdit.

    Download OpenKomodo here - http://www.openkomodo.com/

    0 讨论(0)
提交回复
热议问题