Go: Python code indentation

Source: Internet
Author: User

accustomed to java,c++ kind of tolerance, beginner python, was it put the road to dismount, write if else, unexpectedly must I use indentation format, originally in Python can not use parentheses to represent the statement block, can not use the start/end of the flag to express, But by shrinking in, I'll pay attention later.

Whitespace is important in python. In fact the gap at the beginning of the line is important. It is called indentation. Whitespace (spaces and tabs) at the beginning of a logical line is used to determine the indentation level of logical lines, which is used to determine the grouping of statements.
This means that statements at the same level must have the same indentation. Each set of such statements is called a block. We will see examples of the usefulness of blocks in later chapters.


One thing you need to remember is that the wrong indentation throws an error. For example:

i = 5
print ' Value is ', I # error! Notice A single space at the start of the line
print ' I repeat, the value is ', I

When you run this program, you will get the following error:

File "whitespace.py", line 4
print ' Value is ', I # error! Notice A single space at the start of the line
^
Syntaxerror:invalid syntax

Notice that there is a space at the beginning of the second line. The error indicated by Python tells us that the syntax of the program is invalid, that the program is not written correctly. It tells you that you cannot arbitrarily start a new block of statements (except, of course, the main block you have been using). When you are able to use the new block, it will be described in detail in later chapters, such as control flow.

How to Indent
Do not use tabs and spaces to indent, as this does not work when crossing different platforms. I strongly recommend that you use a single tab or two or four spaces at each indent level.
Choose one of these three indentation styles. More importantly, choose a style and use it consistently, that is, use only this style.


Python indentation should be an advantage, it may not be a very habit at first, just slow down!
When you look at someone else's Python code, you'll feel the benefits of indentation in depth.
In addition, Python indentation can prevent a lot of code from piling up in a way that objectively promotes a good code style



The philosophy of PY is different from Perl/ruby. It will not give you a lot of features, and then warn you that this one is best not to use, the one to pay attention to. It thinks that whatever it gives you, it is for you, as long as it conforms to its grammatical norms, any use can be. If you can write bad code, that means there is a problem with the design. This is a responsible attitude.

{} can allow programmers to write bad code, which is inconsistent with the philosophy of py. Therefore, the PY in the design of the provision, with a contraction and not {} represents the program block.

There's nothing wrong with that, in the vast majority of cases, indentation is clearer than {}.

But the indenting of the PY is not drawbacks. Personal feeling there are two places inconvenient.

First, it is inconvenient to modify indentation when pasting code. But fortunately I have VI.

Second, HTML nested py code, the same time to maintain two sets of indentation, HTML and py. This is too difficult. I think this is the most important reason to hinder the PSP's popularity.

There is no denying that Python code cannot be reformatted once it is indented.

Go: Python code indentation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.