Are there any pitfalls with using whitespace in Python?

后端 未结 17 1181
时光取名叫无心
时光取名叫无心 2021-01-06 17:36

At the moment I have never had a problem with whitespace in Python (although I\'ve only used it in two projects and I was the only programmer). What are some potential pitf

相关标签:
17条回答
  • 2021-01-06 18:05

    If your using emacs, set a hard tab length of 8 and a soft tab length of 4. This way you will be alterted to any extraneous tab characters. You should always uses 4 spaces instead of tabs.

    0 讨论(0)
  • 2021-01-06 18:07

    yeah there are some pitfalls, but most of the time, in practice, they turn out to be enemy windmills of the Quixotic style, i.e. imaginary, and nothing to worry about in reality.

    I would estimate that the pitfalls one is most likely to encounter are (including mitigating steps identified):

    1. working with others a.k.a. collaboration

      a. if you have others which for whatever reason refuse to adhere to PEP 8, then it could become a pain to maintain code. I've never seen this in practice once I point out to them the almost universal convention for python is indent level == four spaces

      b. get anyone/everyone you work with to accept the convention and have them figure out how to have their editor automatically do it (or better yet, if you use the same editor, show them how to configure it) such that copy-and-paste and stuff just works.

    2. having to invest in a "decent" editor other than your current preferred one, if your current preferred editor is not python friendly -- not really a pitfall, more an investment requirement to avoid the other pitfalls mentioned associated with copy-and-paste, re-factoring, etc. stop using Notepad and you'll thank yourself in the morning.

      a. your efficiency in editing the code will be much higher under an editor which understands python

      b. most modern code editors handle python decently. I myself prefer GNU Emacs, and recent versions come with excellent python-mode support out-of-the-box. The are plenty of other editors to explore, including many free alternatives and IDEs.

      c. python itself comes out of the box with a "smart" python editor, idle. Check it out if you are not familiar, as it is probably already available with your python install, and may even support python better than your current editor. PyCrust is another option for a python editor implemented in python, and comes as part of wxPython.

    3. some code generation or templating environments that incorporate python (think HTML generation or python CGI/WSGI apps) can have quirks

      a. most of them, if they touch python, have taken steps to minimize the nature of python as an issue, but it still pops up once in a while.

      b. if you encounter this, familiarize yourself with the steps that the framework authors have already taken to minimize the impact, and read their suggestions (and yes they will have some if it has ever been encountered in their project), and it will be simple to avoid the pitfalls related to python on this.

    0 讨论(0)
  • 2021-01-06 18:09

    If you use Eclipse as your IDE, you should take a look at PyDev; it handles indentation and spacing automatically. You can copy-paste from mixed-spacing sources, and it will convert them for you. Since I started learning the language, I've never once had to think about spacing.

    And it's really a non-issue; sane programmers indent anyway. With Python, you just do what you've always done, minus having to type and match braces.

    0 讨论(0)
  • 2021-01-06 18:10

    That actually kept me away from Python for a while. Coming from a strong C background, I felt like I was driving without a seat belt.

    It was aggravating when I was trying to fill up a snippet library in my editor with boilerplate, frequently used classes. I learn best by example, so I was grabbing as many interesting snippets as I could with the aim of writing a useful program while learning.

    After I got in the habit of re-formatting everything that I borrowed, it wasn't so bad. But it still felt really awkward. I had to get used to a dynamically typed language PLUS indentation controlling my code.

    It was quite a leap for me :)

    0 讨论(0)
  • 2021-01-06 18:10

    I used to think that the white space issues was just a question of getting used to it.

    Someone pointed out some serious flaws with Python indentation and I think they are quite valid and some subconcious understanding of these is what makes experienced programs nervious about the whole thing:-

    • Cut and paste just doesnt work anymore! You cannot cut boiler plate code from one app and drop it into another app.
    • Your editor becomes powerless to help you. With C/Jave etc. there are two things going on the "official" curly brackets indentation, and, the "unnofficial" white space indentation. Most editors are able reformat hte white space indentation to match the curly brackets nesting -- which gives you a string visual clue that something is wrong if the indentation is not what you expected. With pythons "space is syntax" paradigm your editor cannot help you.
    • The sheer pain of introducing another condition into already complex logic. Adding another if then else into an existing condition involves lots of silly error prone inserting of spaces on many lines line by hand.
    • Refactoring is a nightmare. Moving blocks of code around your classes is so painful its easier to put up with a "wrong" class structure than refactor it into a better one.
    0 讨论(0)
  • 2021-01-06 18:14

    Pick a good editor. You'd want features such as:

    1. Automatic indentation that mimics the last indented line
    2. Automatic indentation that you can control (tabs vs. spaces)
    3. Show whitespace characters
    4. Detection and mimicking of whitespace convention when loading a file

    For example, Vim lets me highlight tabs with these settings:

    set list
    set listchars=tab:\|_
    highlight SpecialKey ctermbg=Red guibg=Red
    highlight SpecialKey ctermfg=White guifg=White
    

    Which can be turned off at any time using:

    set nolist
    

    IMO, I dislike editor settings that convert tabs to spaces or vice versa, because you end up with a mix of tabs and spaces, which can be nasty.

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