For programmers, tabs and Spaces are much more than a matter of “position”.
The length of a TAB can vary from editor to editor, so if you indent a TAB in one editor, it might look like a mess in another. Whitespace does not cause this problem because whitespace takes up only one character.
As you all know, Tab is in ASCII, the code is 9, and the space is 32. This means that when we press a Tab, even if it looks like eight Spaces (or four Spaces, depending on the situation), it looks completely different to a computer. This also means that it is likely to be a decisive difference for code that uses characters to describe procedures.
This is especially true for Python, a language that uses Spaces to indent layers of code.
Let’s look at a piece of code.
class MyForm(Form):
value1 = StringField('value1')
value2 = StringField('value2')
value3 = StringField('value3') # this line is Tab indent
submit = SubmitField('Submit')
Copy the code
The value3 variable looks like any other variable, but there is an error — an indentation error.
value3 = StringField('value3')
IndentationError: unexpected indent
Copy the code
Python doesn’t force you to use Tab indentation or space indentation, or even a few Spaces, but it definitely does! You can never mix tabs and Spaces, so is there a big difference between Spaces and tabs?
At this time, some students are going to say, why did I never have such a problem when I used PyCharm (or other IDE)?
In fact, many ides make various optimizations for the Tab key. One of them is to expand the Tab key to a space, which means that when you press Tab, the IDE actually helps you convert one “9” into four (or eight) “32”. But be aware that not all ides do this for you! In the same way, Vim, a pristine editor, won’t do the same for you.
Since tabs behave differently in different environments, Spaces are always the same. Spaces are also more accurate for careful indentation (for example, when you want to line up comments). So it looks like it’s really better to write code with Spaces than with tabs.
Advantages of Spaces instead of tabs:
Whitespace in every case the code is what you want it to be. TAB code only looks good if you and the author of the code have the same TAB size. Changing the TAB size doesn’t solve this problem because it’s hard to change the TAB size every time you open a file, and everyone has different habits (POSIX/Unix tabs should be 8 characters wide, Linus also specifies TAB size 8 for all code in the Linux kernel. If there are end-of-line comments, the TAB size must be set to the same as the author, which means that you need to change the TAB size frequently for different code. I’ve seen a lot of code that uses TAB sizes ranging from 2,3,4,5,6,8,16 and even 32, and if you use a TAB size different from the author’s, it’s not going to look good.
Reliable IDE can solve the forward and backward increase and decrease the indentation problem, even four Spaces, a backspace key can also be all back, so there is no problem in the use of convenience. If you complain that deletion adjustments aren’t working, you need to study your editor. In fact, add or subtract indentation is directly available in mainstream editors, and neither TAB, space, nor backspace are used directly for indentation.
TAB is a TAB character, not an indent, as is used extensively in HTML pages
Just as layout is a bad habit, it is usually a bad habit to use TAB layouts extensively in your programming.
In general, teams have to develop a code specification, and on most teams, using four Spaces instead of tabs is the default. Therefore, it is highly recommended that you use Spaces instead of tabs. In addition, each IDE (editor) provides Tab automatic conversion of Spaces, as long as you set it up, press the Tab key can appear 4 or more Spaces
What do you think? Welcome to leave a message!