Python Pit Daddy Code indent

Source: Internet
Author: User

These two days PYTHON-CN mailing list has a thread development of the special long, the topic is "Python code indentation is really pit dad" (address), the landlord in a computer with Komodo write code to another computer can not run, debugging only found that the code indentation problem, For this landlord said "really fire big"! And after the reply, can imagine, criticize landlord, education landlord, despise the landlord and so on is not a minority (after all, is the Python mailing list, are Python developers, enthusiasts).

Strict code indentation is a feature of the Python syntax, just as important as the curly braces in C, C + +, Java, C #, and so on, which are necessary in most cases. In many code specifications there are also requirements for code writing to follow certain rules of line wrapping and code indentation, but these requirements are purely convenient for people (programmers) to read, use or modify, for the compiler or interpreter, is completely blind. But for the Python interpreter, the indentation before each line of code has a syntactic and logical meaning. This feature of Python is also often controversial in Python users and non-Python users.

The origin of Python's code indentation, some say that things are inherited from ABC (not heard but very old language), some people say is to avoid curly braces, I guess the Python inventor is a whim of the decision, probably only he can explain the problem. In any case, as a language developed for more than 10 years, this grammatical rule is unlikely to change.

In fact, strict requirements (mandatory) of the code indentation, like a double-edged sword, there are advantages and disadvantages. The benefits are obvious, under strict code indentation, code is very neat and tidy, pleasing, improved readability, to a certain extent, also improved maintainability. Some people say that this kind of restraint is very advantageous to team development, of course, not necessarily, it depends on the disadvantage of forcing the code indentation. Python strict code indentation, for people who are transferred from other languages (now the first language of computer and related majors are C/s or Java, their grammatical style is basically always), may have to adapt for some time. Code indentation is very strict, if you do not follow the rules, careless words will appear grammatical errors, such as unexpected indent and so on. There are even logic errors that sometimes occur.

In fact, because of code indentation and then a syntax error or logic error, in my opinion there are two main cases, one is to mix tab and space indentation, and the other is the editor of the indentation of different processing. Here is an example of the code:

The notepad++ editor used in the diagram, the arrows represent a tab, the dots represent a space, the arrows and points are not displayed by default, and you need to enable this feature specifically in view-show symbols-display spaces and tabs. If there are no arrows and points, it is generally considered that the execution result should be a, actually show a, C, the reason is very simple,1 Tab,python will be considered to be 8 spaces . If you switch to another editor, print ' C ' will not appear in the same column as print ' B '. For the same text, simple modification, the different editors do save, may also cause indentation to appear different.

I think in order to avoid unnecessary trouble due to code shrinking, writing Python code should use a unique indentation (either tab, or space), using a fixed and unified editor, in addition, you should take advantage of some of the editor's features. For notepad++, there are two features that can be used in addition to the above shown spaces and tabs: One is, the blank operation under the Edit menu has two Options tab to space and space to tab, If the code for the tab to space, the Code column alignment is basically the same, the arrows become points, but the result is a; second, under Settings-preferences-language, you can choose " Replace with a space ", so that each time you press the TAB key will automatically convert to a space.

Other editors, as well as some of the corresponding processing techniques, such as those presented in the list, can be set up for VIM:

Set list
Set Listchars=tab:\|\, Trail:-,nbsp:_

More editor features, Google should have a lot more, here do not repeat.

Dealing with the problem of code indentation should be considered the basic skill of Python.

Syntax error in Python script run: Indentationerror:unindent does not match any outer indentation level

Problem
A python script, which would have run well, then wrote a few lines of code, and also ensured that each line was aligned, but when it ran, there was a syntax error:
Indentationerror:unindent does not match no outer indentation level

"Resolution Process"
1. The most common reason for this error is that there is indeed no alignment. But I'm going to look at the code based on the number of lines I've been wrong.
All with the TAB key, aligned well, there is no misaligned line number AH.
2. Think that the contents of the previous comment affect the syntax of the following statement, so the previous comment has also been deleted.
The result is still this syntax error.
3. After a long day of tossing, suddenly thought of, the current Python script all the characters are displayed to see if there are any special characters.

The currently used text editor notepad++, as if there is a setting that can display all the characters.
Found, in:
Show spaces and tabs, display symbols, view

Then you can see the problem:
The original error line number is 1580 rows:

But the source of the 1580 lines of the alignment with a dot point of space, and the previous lines of the alignment of the arrows used to indicate the TAB key, is mismatched, that is, the code alignment, mixed with the TAB key and space:


The new Python syntax, which is not supported by code alignment, is mixed with tab and space. So the above error message appears.

Knowing the reason, the solution is simple:
To the corresponding tab, are changed to a space, unified the style of alignment, you can.
In the notepad++, go:
Preferences, Settings:

Language--Replace with a Space (Tab key):

Can be implemented, for each subsequent tab input, are automatically converted to 4 spaces.

Summary

Python encountered Indentationerror, the first time to think, is not due to the TAB key and the use of space mix.

It is estimated that many people are also responsible for this kind of cause. Just remember to unify a bit.

Python Pit Daddy Code indent

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.