Python编程:从入门到实践(第3版)

> 📖 **一起读书吧!** 加入《Python编程:从入门到实践》共读营 👉 [点击参加](https://mp.weixin.qq.com/s/ehe2vMrfAFscRLqbM9TF-g)

本讲内容

  • 缩进与空白
  • 命名规范(变量、函数、类、模块)
  • 每行长度和换行
  • 导入(import)的顺序
  • 注释规范

学习目标

写出团队都愿意维护的代码 👔


1. 缩进

1
2
3
4
5
6
7
8
# ✅ 每级缩进用4个空格
def greet(name):
print(f"Hello, {name}")

# ❌ 不要用Tab(在VS Code中设置Tab自动转空格)
# print("hello")

# ❌ 不要混用Tab和空格

VS Code设置:设置 → Editor: Tab Size → 4,勾选"Insert Spaces"

2. 空白

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# ✅ 操作符两边加空格
x = 1
y = 2 + 3

# ❌ 紧贴
x=1
y=2+3

# ✅ 函数参数 / 索引周围不加空格
func(arg1, arg2)
arr[0]
dic['key']

# ❌ 不要这样
func( arg1, arg2 )
arr[ 0 ]

# ✅ 字典 / 赋值等号两边加空格
name = "晚枫"
dic = {'a': 1, 'b': 2}

# ✅ 多行对齐时用额外空格
name = "程序员晚枫"
age = 30 # 用空格对齐
city = "深圳"

官方文档:PEP 8 — Whitespace in Expressions and Statements

3. 命名规范

类型规范示例
变量小写下划线user_name, is_active
常量全大写下划线MAX_SIZE, DEFAULT_PORT
函数小写下划线get_user(), send_email()
驼峰(首字母大写)UserProfile, CarFactory
模块简短小写utils.py, json_handler.py
简短小写mypackage/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# ✅ 变量
user_name = "晚枫"
is_registered = True
item_count = 42

# ✅ 常量
MAX_RETRY = 3
API_BASE_URL = "https://api.example.com"

# ✅ 函数
def get_user_info(user_id):
pass

def calculate_total(items):
pass

# ✅ 类
class UserAccount:
pass

class DataProcessor:
pass

4. 每行长度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 每行不超过79字符
# 如果一行太长,用以下方式换行

# 方式一:括号包裹(推荐)
result = some_function(
arg1='value1',
arg2='value2',
arg3='value3'
)

# 方式二:反斜杠续行
total = first_variable + second_variable + \
third_variable + fourth_variable

# 方式三:隐式括号(字符串连接)
text = (
"这是第一段文字。"
"这是第二段文字。"
)

5. 导入顺序

1
2
3
4
5
6
7
8
9
10
11
# 标准库
import os
import sys
from collections import defaultdict

# 第三方库
import numpy as np
import pandas as pd

# 本地应用
from mymodule import MyClass

顺序:标准库 → 第三方 → 本地,且每组之间空一行。

6. 注释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# ✅ 单行注释:解释"为什么",不解释"是什么"
# 因为用户可能未验证,所以不发送邮件
if user.is_verified:
send_email()

# ❌ 不要写废话注释
# 将i加1
i += 1

# ✅ docstring:每个公开的类/函数都要写
def fetch_data(url, timeout=30):
"""
从URL获取数据。

Args:
url (str): 目标URL
timeout (int): 超时时间(秒),默认30

Returns:
dict: 解析后的JSON数据

Raises:
requests.RequestException: 网络错误时抛出
"""
pass

7. VS Code自动格式化

安装扩展:autopep8black

1
2
3
4
5
6
// .vscode/settings.json
{
"python.formatting.provider": "black",
"editor.formatOnSave": true,
"editor.defaultFormatter": "ms-python.python"
}

保存文件时自动格式化,手动格式化:Shift+Alt+F


📚 官方文档参考


🎓 AI 编程实战课程

程序员晚枫专注AI编程培训,通过 《30讲 · AI编程训练营》,让小白也能用AI做出实际项目。