Home > Backend Development > Python Tutorial > Let's talk about Python's coding style

Let's talk about Python's coding style

WBOY
Release: 2022-03-23 14:08:11
forward
1891 people have browsed it

This article brings you relevant knowledge about python, which mainly introduces the basic coding specifications of Python, including declaring the coding format, indentation rules, comment parts and use of blank lines, etc. ,I hope everyone has to help.

Let's talk about Python's coding style

Recommended learning: python tutorial

Python coding specification

Coding specification It exists in various programming languages, and it may not be very intuitive in some languages.

If you are a novice learning to write code, then memorizing the coding rules at the beginning will be helpful for future writing specifications. The impact is huge!

Let’s briefly introduce some coding standards that beginners should keep in mind. They are divided into several aspects. Let’s take a look!

Python adopts PEP 8 as the coding standard, where PEP is the abbreviation of Python Enhancement Proposal (Python Enhancement Proposal), 8 represents Is a style guide for Python code.

Let’s first look at the code in a picture
Lets talk about Pythons coding style
Comparing the two pieces of code in the picture above, we can find that the code they contain is exactly the same

but the right The code writing format on the side obviously looks more regular than the code segment on the left, and it will be easier and more enjoyable to read because it follows the most basic Python code writing specifications.

The following is divided into several parts to learn Python coding standards to make our code more beautiful and beautiful!


Declare encoding format

  • Generally speaking, declaring encoding format is required in scripts

  • If The python source code file does not declare the encoding format, and the python interpreter will use ASCII encoding by default

  • But this has the disadvantage that once non-ASCII encoded characters appear, the python interpreter will report an error

  • Taking UTF-8 as an example, the following two encoding format declarations are in compliance with the rules.

# -*- coding: utf-8 -*-
Copy after login
# coding = utf-8
Copy after login

Indentation rules

  • and other programming languages ​​​​(such as Java, C language) use curly brackets "{}" Different from separating code blocks, Python uses code indentation and colon (:) to distinguish the level between code blocks.

  • In Python, for class definitions, function definitions, flow control statements, exception handling statements, etc., the colon at the end of the line and the indentation of the next line indicate the start of the next code block , and the end of the indentation indicates the end of this code block.

  • Note that to indent code in Python, you can use spaces or the Tab key. But whether you type spaces manually or use the Tab key, usually uses a length of 4 spaces as an indentation amount (by default, a Tab key represents 4 spaces).

  • For Python indentation rules, beginners can understand it this way. Python requires that each line of code belonging to the same scope must have the same indentation amount, but what is the specific indentation amount? , there are no hard and fast rules.

Correct sample code:

a=1if a==1:
    print("正确")  # 缩进4个空白占位else:              # 与if对齐
    print("错误")   # 缩进4个空白占位
Copy after login

Wrong sample code:

a=1if a==1:
    print("正确") else:              
    print("错误")   
 print("end")   # 改正只需将这行代码前面的空格删除即可
Copy after login

Just remember one thing: Use 4 spaces for indentation uniformly To enter, do not use tabs, and do not mix tabs and spaces

Remember this, generally speaking, indentation will not cause too big a problem!


Comment part

Use # in Python to comment. When we use #, there should be a space after the

#
    # 注释部分 
    # 
    # 注释部分
Copy after login

When commenting inline , there should be at least two spaces in the middle

print("你好,世界")  # 注释
Copy after login

spaces

spaces General principles of use:

  • in binary There is one space on both sides of the operator. The spaces on both sides of the arithmetic operator can be used flexibly, but both sides must be consistent.
  • Do not add spaces before commas, semicolons, and colons, but you should add ( after them Unless at the end of the line)
  • In the parameter list of the function, there must be a space after the comma
  • In the parameter list of the function, the default value is not to add spaces around the equal sign
  • Left bracket After that, do not add spaces before the right bracket
  • Parameter list, there should be no spaces before the left bracket of index or slice

Normally, on both sides of the operator, function parameters It is recommended to use spaces to separate between and on both sides of commas.


Use blank lines

General principles for using blank lines:

    Encoding format declaration, module import, constant and Two empty lines between global variable declarations, top-level definitions and execution code
  • Two empty lines between top-level definitions, one empty line between method definitions
  • Within a function or method, you can place necessary Leave a blank line in place to enhance the sense of rhythm, but continuous blank lines should be avoided
Using necessary blank lines can increase the readability of the code, usually between top-level definitions (such as function or class definitions) Two empty lines, one empty line between method definitions, and one empty line where used to separate certain functions.


模块导入部分

导入总应该放在文件顶部,位于模块注释和文档字符串之后,模块全局变量和常量之前。

导入应该按照从最通用到最不通用的顺序分组,分组之间空一行:

  • 标准库导入
  • 第三方库导入
  • 应用程序指定导入

每个 import 语句只导入一个模块,尽量避免一次导入多个模块

#推荐import osimport sys
#不推荐import os,sys
Copy after login

命名规范

命名规范这一块的大家应该都比较熟悉了,但是不同的编程语言之间的明明规范也是有所区别的~

Python命名建议遵循的一般性原则:

  • 模块尽量使用小写命名,首字母保持小写,尽量不要用下划线
  • 类名使用驼峰(CamelCase)命名风格,首字母大写,私有类可用一个下划线开头
  • 函数名一律小写,如有多个单词,用下划线隔开
  • 私有函数可用一个下划线开头
  • 变量名尽量小写, 如有多个单词,用下划线隔开
  • 常量采用全大写,如有多个单词,使用下划线隔开

引号用法

Python中,输出语句中使用单双引号都是可以正确的,但是也有相应的编码规范

所以我们也不要随心所欲的添加引号,最好是遵循下面的规范!

引号使用的一般性原则:

  • 自然语言使用双引号
  • 机器标识使用单引号
  • 正则表达式使用双引号
  • 文档字符串 (docstring) 使用三个双引号

分号用法

Python跟其他几个主流编程语言的分号使用区别很大
Python的代码末尾不需要加分号,而Java和C#等都需要添加

不要在行尾添加分号,也不要用分号将两条命令放在同一行,例如:

# 不推荐print("Hello") ;  print("World")
Copy after login

推荐学习:python详细教程

The above is the detailed content of Let's talk about Python's coding style. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template