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

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

    Getting your indentation to work correctly is going to be important in any language you use.

    Even though it won't affect the execution of the program in most other languages, incorrect indentation can be very confusing for anyone trying to read your program, so you need to invest the time in figuring out how to configure your editor to align things correctly.

    Python is pretty liberal in how it lets you indent. You can pick between tabs and spaces (but you really should use spaces) and can pick how many spaces. The only thing it requires is that you are consistent which ultimately is important no matter what language you use.

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

    I do not believe so, as Python is a whitespace-delimited language. Perhaps a text editor or IDE with auto-indentation would be of help. What are you currently using?

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

    Many Python IDEs and generally-capable text/source editors can handle the whitespace for you.

    However, it is best to just "let go" and enjoy the whitespace rules of Python. With some practice, they won't get into your way at all, and you will find they have many merits, the most important of which are:

    1. Because of the forced whitespace, Python code is simpler to understand. You will find that as you read code written by others, it is easier to grok than code in, say, Perl or PHP.
    2. Whitespace saves you quite a few keystrokes of control characters like { and }, which litter code written in C-like languages. Less {s and }s means, among other things, less RSI and wrist pain. This is not a matter to take lightly.
    0 讨论(0)
  • 2020-12-31 14:39

    The real answer to your question is that if you are going to use the language you need to learn its syntax. Just as an error in indenting python can generate a compiler error, an error using braces in various other languages can also generate a compiler error.

    Even worse it can be silently misinterpreted by the compiler to do the wrong thing. This is particularly dangerous when the indenting doesn't match the desired meaning. I.e. in many other languages:

    If(first condition)
       if (second condition)
          do something interesting;
    else
      do something different;
    

    Will lead to unpleasant surprises.

    Python forces you to write code that looks like what it does. This is a good thing for other programmers who have to read your code, or for you when you try to read your own code after a month or so.

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

    I find it hard to understand when people flag this as a problem with Python. I took to it immediately and actually find it's one of my favourite 'features' of the language :)

    In other languages I have two jobs: 1. Fix the braces so the computer can parse my code 2. Fix the indentation so I can parse my code.

    So in Python I have half as much to worry about ;-)

    (nb the only time I ever have problem with indendation is when Python code is in a blog and a forum that messes with the white-space but this is happening less and less as the apps get smarter)

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

    It's possible to write a pre-processor which takes randomly-indented code with pseudo-python keywords like "endif" and "endwhile" and properly indents things. I had to do this when using python as an "ASP-like" language, because the whole notion of "indentation" gets a bit fuzzy in such an environment.

    Of course, even with such a thing you really ought to indent sanely, at which point the conveter becomes superfluous.

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