Python indentation is very important in python coding. Because if your python indentation does not match any outer indentation level, the python code may not prompt any error when editing, but the error ( for example: expected an indented block ) will be thrown when the python code execute. This makes your python code debugging difficult.Generally, we use
Space as python indentation character, but which one is best python indentation character? We will discuss this issue in this article.
In different editors, the length of
Tab may be different, so after setting python indentation with
Tab in one editor, it may be disordered in other editors.
Space does not cause this problem because
Space takes up only one character’s space.
It is well known that in ASCII code, the
Tab character’s encoding is 9 and the
Space character’s encoding is 32. This means that when we press a
Tab, even if it looks like eight Spaces (or four Spaces, depending on the environment, tabs might look different), it’s something completely different to a computer.
Let’s look at a piece of python code as below.
class UserInfoForm(Form): username = StringField('username') password = StringField('password') # Below line is indented with Tab email = StringField('email') ok = SubmitField('OK')
It seems that this email variable is no different from other variables, but there is such an error – indentation error.
email = StringField('email') IndentationError: unexpected indent
In fact, python doesn’t force you to indent with tab or space. However, in PEP8, four spaces are recommended as indents. But absolutely! You can’t mix
At this time, some people will say, how can I never have such a problem with Pycharm (or other IDEs). In fact, many IDEs make various optimizations for the
Tab key. One important optimization is to expand the
Tab key into
Spaces. That is to say, when you press
Tab, IDE actually helps you convert a “9” into four (or eight) “32”. Note, that not all IDEs do this for you! Such as vim.
Tab displays differently in different environments and
Space is always the same. For fine typographic indentions (such as trying to line up each line of comments),
WhiteSpace is also more precise. So, it does look better to write code in
Spaces than in
The Benefits Of Replacing Tab with Space.
- Use Spaces to indent python code, your Python code will be what you want in all cases. Use Tabs to indent python code, your python code only looks good if you and the code author have the same tab size.
- Reliable ides solve the problem of increasing and decreasing indentation backward and forward, and even four Spaces, a backspace key can be completely backspaced, so there is no problem in terms of ease of use. If you complain that deleting adjustments aren’t working, you might want to take a look at your Python code editor. In fact, there are shortcut keys to increase or decrease indentation in mainstream editors. Tab, space, or backspace are rarely used for indentation directly.
- Tab is TAB character not indent character. Just as it is not a good programming habit to use < Table > for layout in HTML pages, it is not a good habit to use tab layout in programming.
- In general, team development needs to develop a set of coding specifications. In most teams, using four spaces instead of Tab is the default. So it’s very recommended that you use spaces instead of tabs.
- In addition, each IDE (Editor) provides the function of tab to spaces automatic conversion. As long as you set it, you can press the tab key to have four or more spaces.