Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the spaces in Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use the spaces of Python". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The use of spaces

Use spaces to indicate indentation instead of using tabs (Tab). This seems unreasonable to people who are accustomed to other programming languages, because most programmers use Tab to express indentation, but you should know that Python does not have the syntax of constructing a block of code with curly braces, as in Python or Java. In Python, both branch and loop structures use indentation to indicate which code belongs to the same level. Given that Python code is much more dependent on indentation and indentation width than many other languages. In different editors, the width of Tab may be 2, 4, or 8 characters, or even other more outrageous values, and using Tab to represent indentation can be a disaster for Python code.

Each level of indentation associated with grammar is represented by four spaces.

The number of characters per line should not exceed 79 characters, and if the expression is too long to occupy multiple lines, all lines except the first line should add 4 spaces to the normal indentation width.

The definitions of functions and classes are separated by two blank lines before and after the code.

In the same class, methods should be separated by a blank line.

A space should be left on the left and right sides of the binary operator, and only one space should be fine.

Identifier naming

PEP 8 advocates naming different identifiers in Python with different naming styles, so that when you read the code, you can use the name of the identifier to determine what role the identifier plays in Python (at this point, Python's own built-in modules and some third-party modules do not do very well).

Variables, functions, and properties should be spelled in lowercase letters, and if there are multiple words, underscores should be used to connect them.

Protected instance properties in the class should start with an underscore.

Private instance properties in the class should start with two underscores.

Classes and exceptions should be named with the first letter of each word capitalized.

Module-level constants should be in all uppercase letters, and if there are more than one word, connect with an underscore.

Class, the first parameter should be named self to represent the object itself.

Class, the first parameter should be named cls to represent the class itself.

Expressions and statements

In the Python Zen (which can be viewed using import this), there is a famous saying: "There should be one-- and preferably only one-- obvious way to do it." translated into Chinese is "there should be and preferably only one exact way to do something." the idea conveyed is also ubiquitous in PEP 8.

Use inline negative words instead of putting negative words in front of the whole expression. If an is not b, for example, is easier to understand than if not an is b.

Instead of checking the length to determine whether a string, list, etc., is None or has no elements, check it as if not x.

Even if there is only one line of code in the if branch, for loop, except exception catch, etc., do not write the code with if, for, except, etc., separate will make the code clearer.

The import statement is always placed at the beginning of the file.

When introducing modules, from math import sqrt is better than import math.

If you have multiple import statements, you should divide them into three parts, Python standard modules, third-party modules, and custom modules from top to bottom, and each part should be arranged alphabetically according to the name of the module.

This is the end of the content of "how to use Python spaces". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report