In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "Python uses tabs or spaces when writing code". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
For programmers, tabs and spaces are much more than just "standing" problems.
Tabs may not have the same length in different editors, so after setting indentation with tabs in one editor, indentation may be chaotic in other editors. This problem does not occur with spaces, because spaces occupy only one character position.
As we all know, Tab in ASCII code, the code is 9, and the space is 32. That is, when we press a Tab, even if it looks like eight spaces (or four spaces, depending on the context), it is something completely different to the computer. This also means that for code that uses characters to describe processes, it is likely to be the decisive difference.
This is especially true for Python, a language that uses space indentation to distinguish code hierarchies.
Let's look at a code.
class MyForm(Form): value1 = StringField ('value 1') value2 = StringField ('value 2') value3 = StringField ('value 3')#This line is indented Tab submit = SubmitField ('Submit ')
It looks like this value3 variable isn't different from any other variable, but there's this error--indentation error.
value3 = StringField('value3')IndentationError: unexpected indent
Python doesn't force you to indent with tabs or spaces, or even press a few spaces, but absolutely! Tabs and spaces must not be mixed, so here, is the difference between spaces and tabs very big?
At this time some students will say, I use PyCharm (or other IDE) how never appear such a problem?
In fact, many IDEs have made various optimizations to the Tab key, one of which is to expand the Tab key into spaces, that is, when you press Tab, the IDE actually helps you convert a "9" into four (or eight)"32". Be aware, however, that not all IDEs do this for you! Similarly, for a good pure editor Vim, it will definitely not help you do such work.
Since tabs behave differently in different contexts, spaces are always the same. For fine indenting (such as aligning comments on each line), spaces are also more precise. So it seems that it is indeed better to write code in spaces than tabs.
Benefits of replacing tabs with spaces:
Spaces are what you want the code to look like in all cases. Tabs look good only if you set the tab size to the same as the author. Changing tab size doesn't solve this problem, because it's hard to change tab size every time you open a file, and everyone usually has different habits (POSIX/Unix tabs should be 8 characters wide, Linus also stipulates that all code in the Linux kernel tab size is 8). If there are end-of-line comments, then the tab size must be set to the same as the author, which means that you need to change the tab size frequently to see different code. I've seen a lot of code that uses tab size ranging from 2, 3, 4, 5, 6, 8, 16, and even 32. If you use a tab size different from the author's, it won't look good.
Reliable IDEs can solve the problem of increasing and decreasing indentation in forward and backward, even if it is four spaces, a backspace key can also completely retreat, so there is no problem in terms of convenience of use. -- If complaining about deleting tweaks doesn't work, you need to research your editor. In fact, increasing and decreasing indentation has direct shortcut keys in mainstream editors, whether tab or space or backspace are rarely used directly for indentation.
tab is a tab character rather than an indent character, as is used extensively in html pages
Just as layout is a bad programming habit, heavy use of tab layouts in programming is generally not a good habit.
In general, teams develop a coding convention, and in most teams, using 4 spaces instead of tabs is the default. Therefore, it is very recommended that you use spaces instead of tabs. In addition, each IDE (editor) provides the function of tab automatic conversion of spaces. As long as you set it up, press the tab key to appear 4 or more spaces.
The content of "Python uses tabs or spaces when writing code" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.