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

前端 未结 29 980
小鲜肉
小鲜肉 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:45

    In Python, indentation is a semantic element as well as providing visual grouping for readability.

    Both space and tab can indicate indentation. This is unfortunate, because:

    • The interpretation(s) of a tab varies among editors and IDEs and is often configurable (and often configured).

    • OTOH, some editors are not configurable but apply their own rules for indentation.

    • Different sequences of spaces and tabs may be visually indistinguishable.

    • Cut and pastes can alter whitespace.

    So, unless you know that a given piece of code will only be modified by yourself with a single tool and an unvarying config, you must avoid tabs for indentation (configure your IDE) and make sure that you are warned if they are introduced (search for tabs in leading whitespace).

    And you can still expect to be bitten now and then, as long as arbitrary semantics are applied to control characters.

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

    Nope, there's no way around it, and it's by design:

    >>> from __future__ import braces
      File "<stdin>", line 1
    SyntaxError: not a chance
    

    Most Python programmers simply don't use tabs, but use spaces to indent instead, that way there's no editor-to-editor inconsistency.

    0 讨论(0)
  • 2020-12-31 14:46
    from __future__ import braces
    
    0 讨论(0)
  • 2020-12-31 14:48

    I was a bit reluctant to learn Python because of tabbing. However, I almost didn't notice it when I used Vim.

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

    I'm surprised no one has mentioned IDLE as a good default python editor. Nice syntax colors, handles indents, has intellisense, easy to adjust fonts, and it comes with the default download of python. Heck, I write mostly IronPython, but it's so nice & easy to edit in IDLE and run ipy from a command prompt.

    Oh, and what is the big deal about whitespace? Most easy to read C or C# is well indented, too, python just enforces a really simple formatting rule.

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

    Just use Ruby, it's much better than Python.

    http://www.ruby-lang.org

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