Home >Backend Development >Python Tutorial >Is indentation mandatory in Python?

Is indentation mandatory in Python?

(*-*)浩
(*-*)浩Original
2019-06-12 13:57:3610371browse

Whitespace is important in Python. In fact, the white space at the beginning of the line is important. It's called indentation. The whitespace (spaces and tabs) at the beginning of a logical line is used to determine the indentation level of the logical line, and thus the grouping of statements. This means that statements at the same level must have the same indentation. Each group of such statements is called a block. So it is mandatory.

Is indentation mandatory in Python?

After graduating from college, Van Rossum joined CWI, an institution that focuses on academic research in mathematics and computers. This work experience was very important to him. His programming concepts, including his design ideas for Python, were initially formed here. (Recommended learning: Python video tutorial)

Regarding Van Rossum, let’s talk about his interview today.

Q: Why did you use forced indentation to divide program domains when designing the Python language?

If you are a C or Java programmer, you are used to using braces to divide program domains - in fact, most languages ​​do this - and you will see that Python uses indentation to do this. Things, you will immediately feel "this is too outrageous". This is how I felt when I learned Perl and Java and then used Python.

Van Rossum said this: This forced indentation is not a new concept. When I was programming in ABC language at CWI, this was what people did. I inherited this concept from the ABC language. However, a very old language like occam also uses this method. I don’t know who adopted it first. Maybe they are all original. This idea may also come from Don Knuth (Knuth, a famous computer scientist and author of the classic "The Art of Computer Programming"), who proposed this approach as early as 1974.

Van Rossum’s idea is that if something is established based on “common sense”, there is no need to get too hung up on it. Of course, this kind of common sense is his common sense. As for other people, everyone should be used to it. Moreover, Van Rossum likes the characteristics of ABC. He feels that this design completely ends the ultimate debate among C or Java programmers about whether to place the curly brace directly after the function name or to put it on a new line. In addition, he feels that such code is more readable. Strict formatting will result in cleaner code and will reduce the difference in code "layout" between different programmers. Why not?

So Python has continued this coding style and has become its own distinctive feature.

Q: If you want to become proficient in Python programming, do you have any suggestions?

Van Rossum said: Pragmatism. If you get too hung up on theoretical concepts like data encapsulation, access control, abstraction, or specification, you're not a real Python programmer. Moreover, you will waste time "fighting against language". What is being an enemy of language is that when you are using Python, you are still trying to continue the usage habit of Java. You should enjoy it and use its features. If you believe in instant gratification, extreme programming and agile development, then Python is your favorite.

We Python engineers also have a most commonly used saying. In English it is "Life is short, you need Python". In Chinese, this is what I often say, life is short, I use Python.

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of Is indentation mandatory in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn