What does the tab key in python mean

For programmers, in fact, tabs and spaces are far more than just a question of "position".

The length of the tab may be inconsistent in different editors, so after setting indentation with tab in one editor, the indentation may be messed up in other editors. This problem does not occur with spaces, because spaces occupy one character position.

As we all know, Tab in ASCII code, the code is 9, and the space is 32. This means that when we press a Tab, even if it looks like 8 spaces (or 4 spaces, in different environments, Tab may display different effects), it is completely different to the computer. The same thing. This also means that for codes that use characters to describe the process, it is most likely to be a decisive difference.

Especially for a language that uses space indentation to distinguish code levels-Python.

Let's look at a piece of code.

classMyForm(Form):
 value1 =StringField('value1')
 value2 =StringField('value2')
 value3 =StringField('value3')  #This line uses Tab indentation
 submit =SubmitField('Submit')
 learn python ='QQ group:725479218'

It seems that this value3 variable is no different from other variables, but there is such an error-indentation error.

value3 =StringField('value3')
IndentationError: unexpected indent

In fact, Python does not force you to use Tab indentation or space indentation, and even the number of spaces is not mandatory, but it is absolutely! Tabs and spaces must not be mixed, so here, is there a big difference between spaces and tabs?

At this time, some children's shoes are about to be mentioned. Why have I never had such a problem with PyCharm (or other IDE)?

In fact, many IDEs have made various optimizations for the Tab key. One of them is to expand the Tab key to a space. That is to say, when you press Tab, the IDE actually helps you put a "9 "Is converted into four (or eight) "32". But be aware that not all IDEs do this kind of work for you! In the same way, for the editor Vim, which is so pure and unpretentious, it will definitely not help you do this kind of work.

Since Tab displays differently in different environments, the spaces are always the same. For some detailed typesetting and indentation (for example, if you want to align each line of comments), using spaces is more precise. It seems that using spaces to write code is better than using Tab.

**The benefits of spaces instead of Tab: **

The code is what you want in all cases. The tab only looks good when you and the code author's tab size is set to the same. Modifying the tab size does not solve this problem, because it is difficult for you to modify the tab size every time you open a file, and everyone usually has different habits (the POSIX/Unix standard tab should be 8 characters wide, Linus also stipulated The tab size of all codes in the Linux kernel is 8). If there are end-of-line comments, the tab size must be set to be the same as the author, which means that you need to modify the tab size frequently when you look at different codes. I have seen many codes, and the tab sizes used are from 2, 3, 4, 5, 6, 8, 16 or even 32. If you use a tab size different from the author, the appearance will be very undesirable.

Reliable IDEs can solve the problem of increasing and reducing indents in forward and backward directions. Even if it is four spaces, a backspace key can also retreat, so there is no problem in the convenience of use. ——If you complain that deleting adjustments cannot be effectively resolved, you need to study your editor. In fact, there are shortcut keys for increasing or decreasing indentation directly in mainstream editors. No matter it is tab or space or backspace, it is rarely used directly for indentation.

tab is a tab character rather than an indentation character, as it is used extensively in html pages <table 进行布局是个不好的编程习惯一样,在编程中大量使用制表符布局通常也不是个好习惯。

Under normal circumstances, team development must formulate a set of coding standards. In most teams, using 4 spaces instead of Tab is the default. Therefore, it is highly recommended that you use spaces instead of Tab. In addition, each IDE (editor) provides the function of automatically converting spaces into tabs. As long as you set it up, you can press the tab key to display 4 or more spaces.

Content expansion:

Use of Python_Tab key

  1. When entering an expression in the shell, as long as you press the Tab key, any variable (object, function, etc.) in the current namespace that matches the entered string will be found:
In[1]: an_orange =27
In[2]: an_pear =15
In[3]:an(press<Tab )
anorange an_pear and any
  1. Enter a period after any object to automatically complete the input of methods and properties:
In[1]: a =[1,2,3]
In[2]: a.<Tab 
a.append a.extend a.remove a.sort
a.count a.index a.pop a.reserve
  1. Applied to the module:
In[1]:import pandas
In[2]: pandas.<Tab 
pandas.cut pandas.core pandas.concat
  1. When entering anything that looks like a file path (even in a Python string), press the Tab key to find out what matches it in the computer's file system.

  2. The Tab key auto-completion function can be used for function keyword parameters.

So far, this article on what the tab key in python means is introduced here. For more related content on what the tab key in python means, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you all Support ZaLou.Cn a lot!

Recommended Posts

What does the tab key in python mean
What does rc1 mean in python
What does def in python do
​What are the numbers in Python?
What does np do in python
What is the function of adb in python
How does python call the key of a dictionary
What are the ways to open files in python
What is introspection in python
What is object-oriented in python
The usage of wheel in python
How does python change the environment
What is list comprehension in python
What is the use of Python
Talking about the modules in Python
The usage of tuples in python
Understanding the meaning of rb in python
What are the required parameters of python
The usage of Ajax in Python3 crawler
What is the scope of python variables
What is the id function of python
What is an anonymous function in Python
What are web development frameworks in python
How does Python handle the json module
What are the advantages of python language
What is the prospect of python development
What is a sequence table in Python
What is the function body of python
Python randomly shuffles the elements in the list
How to use the round function in python
Explain the list under Python multithreading in detail
An article to understand the yield in Python
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
How to use the format function in python
First acquainted with python, the grammatical rules in python
What is the difference between python and pycharm
The consequences of uninstalling python in ubuntu, very
How to install the downloaded module in python
The best way to judge the type in Python
Python is short-what does the spirit cage say
Functions in python
2.1 The Python Interpreter (python interpreter)
What is the difference between synchronous and asynchronous Python?
What is the advantage of python over corporate language
What are the methods for python to connect to mysql
How does python get input examples from the keyboard
How to understand the introduction of packages in Python
Python data visualization: who are the big names in Python?
How does python handle the program cannot be opened
How does python judge the module installation is complete
For the first time in history, Python surpassed Java!