Are there any pitfalls with using whitespace in Python?

后端 未结 17 1179
时光取名叫无心
时光取名叫无心 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:02

    Pitfalls

    • It can be annoying posting code snippets on web sites that ignore your indentation.

    • Its hard to see how multi-line anonymous functions (lambdas) can fit in with the syntax of the language.

    • It makes it hard to embed Python in HTML files to make templates in the way that PHP or C# can be embedded in PHP or ASP.NET pages. But that's not necessarily the best way to design templates anyway.

    • If your editor does not have sensible commands for block indent and outdent it will be tedious to realign code.

    Advantages

    • Forces even lazy programmers to produce legible code. I've seen examples of brace-language code that I had to spend hours reformatting to be able to read it...

    • Python programmers do not need to spend hours discussing whether braces should go at the ends of lines K&R style or on lines on their own in the Microsoft style.

    • Frees the brace characters for use for dictionary and set expressions.

    • Is automatically pretty legible

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

    The only trouble I've ever had is minor annoyances when I'm using code I wrote before I settled on whether I liked tabs or spaces, or cutting and posting code from a website.

    I think most decent editors these days have a convert tabs-to-spaces and back option. Textmate certainly does.

    Beyond that, the indentation has never caused me any trouble.

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

    It can be confusing in some editors where one line is indented with spaces and the next is indented with a tab. This is confusing as the indentation looks the same but causes an error.

    Also when your copying code, if your editor doesn't have a function to indent entire blocks, it could be annoying fixing all the indentation.

    But with a good editor and a bit of practice, this shouldn't be a problem. I personally really like the way Python uses white space.

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

    When python programmers don't follow the common convention of "Use 4 spaces per indentation level" defined in PEP 8. (If your a python programmer and haven't read it please do so)

    Then you run into copy paste issues.

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

    Long ago, in and environment far, far away, there were languages (such as RPG) that depended on the column structure of punch cards. This was a tedious and annoying system, and led to many errors, and newer languages such as BASIC, pascal, and so forth were designed without this dependency.

    A generation of programmers were trained on these languages and told repeatedly that the freedom to put anything anywhere was a wonderful feature of the newer languages, and they should be grateful. The freedom was used, abused, and calibrated (cf the IOCC) for many years.

    Now the pendulum has begun to swing back, but many people still remember that forced layout is bad in some way vague, and resist it.

    IMHO, the thing to do is to work with languages on their own terms, and not get hung up on tastes-great-less-filling battles.

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

    Some people say that they don't like python indentation, because it can cause errors, which would be immensely hard to detect in case if tabs and spaces are mixed. For example:

    1 if needFrobnicating:
    2    frobnicate()
    3 update()
    

    Depending on the tab width, line 3 may appear to be in the same block as line 2, or in the enclosing block. This won't cause runtime or compile error, but the program would do unexpected thing.

    Though I program in python for 10 years and never seen an error caused by mixing tabs and spaces

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