导航

索引

模块 |

下一页 |

Python »

3.6.13 Documentation »

|

Python 文档目录

Python的新变化

Python 3.6 有什么新变化A

摘要 - 发布重点

新的特性

PEP 498: 格式化的字符串文字

PEP 526: 变量注释的语法

PEP 515: 数字文字中的下划线。

PEP 525: 异步生成器

PEP 530: 异步推导

PEP 487: 自定义类创建

PEP 487: 描述器协议的增强

PEP 519: 添加文件系统路径协议

PEP 495: 消除本地时间的歧义

PEP 529: 将Windows文件系统编码更改为UTF-8

PEP 528: 将Windows控制台编码更改为UTF-8

PEP 520: 保留类属性定义顺序

PEP 468: 保留关键字参数顺序

新的 dict 实现

PEP 523: 向CPython 添加框架评估API

PYTHONMALLOC 环境变量

DTrace 和 SystemTap 探测支持

其他语言特性修改

新增模块

secrets

改进的模块

array

ast

asyncio

binascii

cmath

collections

concurrent.futures

contextlib

datetime

decimal

distutils

email

encodings

enum

faulthandler

fileinput

hashlib

http.client

idlelib 与 IDLE

importlib

inspect

json

logging

math

multiprocessing

os

pathlib

pdb

pickle

pickletools

pydoc

random

re

readline

rlcompleter

shlex

site

sqlite3

socket

socketserver

ssl

statistics

struct

subprocess

sys

telnetlib

time

timeit

tkinter

回溯

tracemalloc

typing

unicodedata

unittest.mock

urllib.request

urllib.robotparser

venv

warnings

winreg

winsound

xmlrpc.client

zipfile

zlib

性能优化

构建和 C API 的改变

其他改进

弃用

新关键字

已弃用的 Python 行为

已弃用的 Python 模块、函数和方法

asynchat

asyncore

dbm

distutils

grp

importlib

os

re

ssl

tkinter

venv

xml

已弃用的 C API 函数和类型

弃用的构建选项

移除

API 与特性的移除

移植到Python 3.6

 ‘python’ 命令行为的变化

更改的Python API

C API 中的改变

CPython 字节码的改变

Python 3.6.2 中的重要变化

New make regen-all build target

Removal of make touch build target

Python 3.6.4 中的重要变化

Python 3.6.5 中的重要变化

Python 3.6.7 中的重要变化

Python 3.6.10 中的重要变化

Notable changes in Python 3.6.13

Python 3.5 有什么新变化

摘要 - 发布重点

新的特性

PEP 492 - 使用 async 和 await 语法实现协程

PEP 465 - 用于矩阵乘法的专用中缀运算符

PEP 448 - Additional Unpacking Generalizations

PEP 461 - percent formatting support for bytes and bytearray

PEP 484 - 类型提示

PEP 471 - os.scandir() function – a better and faster directory iterator

PEP 475: Retry system calls failing with EINTR

PEP 479: Change StopIteration handling inside generators

PEP 485: A function for testing approximate equality

PEP 486: Make the Python Launcher aware of virtual environments

PEP 488: Elimination of PYO files

PEP 489: Multi-phase extension module initialization

其他语言特性修改

新增模块

typing

zipapp

改进的模块

argparse

asyncio

bz2

cgi

cmath

code

collections

collections.abc

compileall

concurrent.futures

configparser

contextlib

csv

curses

dbm

difflib

distutils

doctest

email

enum

faulthandler

functools

glob

gzip

heapq

http

http.client

idlelib 与 IDLE

imaplib

imghdr

importlib

inspect

io

ipaddress

json

linecache

locale

logging

lzma

math

multiprocessing

operator

os

pathlib

pickle

poplib

re

readline

selectors

shutil

signal

smtpd

smtplib

sndhdr

socket

ssl

内存 BIO 支持

Application-Layer Protocol Negotiation Support

Other Changes

sqlite3

subprocess

sys

sysconfig

tarfile

threading

time

timeit

tkinter

回溯

types

unicodedata

unittest

unittest.mock

urllib

wsgiref

xmlrpc

xml.sax

zipfile

其他模块级更改

性能优化

构建和 C API 的改变

弃用

新关键字

已弃用的 Python 行为

不支持的操作系统

已弃用的 Python 模块、函数和方法

移除

API 与特性的移除

移植到Python 3.5

Python 行为的改变

更改的Python API

C API 中的改变

Python 3.5.4 的显著变化

New make regen-all build target

Removal of make touch build target

Python 3.4 有什么新变化

摘要 - 发布重点

新的特性

PEP 453: Explicit Bootstrapping of PIP in Python Installations

Bootstrapping pip By Default

文档更改

PEP 446: Newly Created File Descriptors Are Non-Inheritable

Improvements to Codec Handling

PEP 451: A ModuleSpec Type for the Import System

其他语言特性修改

新增模块

asyncio

ensurepip

enum

pathlib

selectors

statistics

tracemalloc

改进的模块

abc

aifc

argparse

audioop

base64

collections

colorsys

contextlib

dbm

dis

doctest

email

filecmp

functools

gc

glob

hashlib

hmac

html

http

idlelib 与 IDLE

importlib

inspect

ipaddress

logging

marshal

mmap

multiprocessing

operator

os

pdb

pickle

plistlib

poplib

pprint

pty

pydoc

re

resource

select

shelve

shutil

smtpd

smtplib

socket

sqlite3

ssl

stat

struct

subprocess

sunau

sys

tarfile

textwrap

threading

回溯

types

urllib

unittest

venv

wave

weakref

xml.etree

zipfile

CPython Implementation Changes

PEP 445: Customization of CPython Memory Allocators

PEP 442: Safe Object Finalization

PEP 456: Secure and Interchangeable Hash Algorithm

PEP 436: Argument Clinic

Other Build and C API Changes

其他改进

Significant Optimizations

弃用

Deprecations in the Python API

Deprecated Features

移除

不再支持的操作系统

API 与特性的移除

Code Cleanups

移植到 Python 3.4

 ‘python’ 命令行为的变化

更改的Python API

C API 中的改变

3.4.3 的变化

PEP 476: Enabling certificate verification by default for stdlib http clients

Python 3.3 有什么新变化

摘要 - 发布重点

PEP 405: 虚拟环境

PEP 420: 隐式命名空间包

PEP 3118: 新的内存视图实现和缓冲协议文档

相关特性

API changes

PEP 393: 灵活的字符串表示

Functionality

Performance and resource usage

PEP 397: 适用于Windows的Python启动器

PEP 3151: 重写 OS 和 IO 异常的层次结构

PEP 380: 委托给子生成器的语法

PEP 409: 清除异常上下文

PEP 414: 显式的Unicode文本

PEP 3155: 类和函数的限定名称

PEP 412: Key-Sharing Dictionary

PEP 362: 函数签名对象

PEP 421: 添加 sys.implementation

SimpleNamespace

Using importlib as the Implementation of Import

New APIs

Visible Changes

其他语言特性修改

A Finer-Grained Import Lock

Builtin functions and types

新增模块

faulthandler

ipaddress

lzma

改进的模块

abc

array

base64

binascii

bz2

codecs

collections

contextlib

crypt

curses

datetime

decimal

相关特性

API changes

email

Policy Framework

Provisional Policy with New Header API

Other API Changes

ftplib

functools

gc

hmac

http

html

imaplib

inspect

io

itertools

logging

math

mmap

multiprocessing

nntplib

os

pdb

pickle

pydoc

re

sched

select

shlex

shutil

signal

smtpd

smtplib

socket

socketserver

sqlite3

ssl

stat

struct

subprocess

sys

tarfile

tempfile

textwrap

threading

time

types

unittest

urllib

webbrowser

xml.etree.ElementTree

zlib

性能优化

构建和 C API 的改变

弃用

不支持的操作系统

已弃用的 Python 模块、函数和方法

已弃用的 C API 函数和类型

弃用的功能

移植到 Python 3.3

Porting Python code

Porting C code

Building C extensions

Command Line Switch Changes

Python 3.2 有什么新变化

PEP 384: 定义稳定的ABI

PEP 389: Argparse 命令行解析模块

PEP 391: 基于字典的日志配置

PEP 3148: concurrent.futures 模块

PEP 3147: PYC 仓库目录

PEP 3149: ABI Version Tagged .so Files

PEP 3333: Python Web服务器网关接口v1.0.1

其他语言特性修改

新增，改进和弃用的模块

email

elementtree

functools

itertools

collections

threading

datetime 和 time

math

abc

io

reprlib

logging

csv

contextlib

decimal and fractions

ftp

popen

select

gzip 和 zipfile

tarfile

hashlib

ast

os

shutil

sqlite3

html

socket

ssl

nntp

certificates

imaplib

http.client

unittest

random

poplib

asyncore

tempfile

inspect

pydoc

dis

dbm

ctypes

site

sysconfig

pdb

configparser

urllib.parse

mailbox

turtledemo

多线程

性能优化

Unicode

编解码器

文档

IDLE

代码库

构建和 C API 的改变

移植到 Python 3.2

Python 3.1 有什么新变化

PEP 372: 有序字典

PEP 378: 千位分隔符的格式说明符

其他语言特性修改

新增，改进和弃用的模块

性能优化

IDLE

构建和 C API 的改变

移植到 Python 3.1

Python 3.0 有什么新变化

常见的绊脚石

Print Is A Function

Views And Iterators Instead Of Lists

Ordering Comparisons

整数

Text Vs. Data Instead Of Unicode Vs. 8-bit

Overview Of Syntax Changes

新语法

修改的语法

移除的语法

Changes Already Present In Python 2.6

Library Changes

PEP 3101: A New Approach To String Formatting

Changes To Exceptions

Miscellaneous Other Changes

Operators And Special Methods

Builtins

构建和 C API 的改变

性能

移植 Python 3.0

Python 2.7 有什么新变化

Python 2.x的未来

Changes to the Handling of Deprecation Warnings

Python 3.1 Features

PEP 372: Adding an Ordered Dictionary to collections

PEP 378: 千位分隔符的格式说明符

PEP 389: The argparse Module for Parsing Command Lines

PEP 391: Dictionary-Based Configuration For Logging

PEP 3106: Dictionary Views

PEP 3137: The memoryview Object

其他语言特性修改

Interpreter Changes

性能优化

新增和改进的模块

新模块：importlib

新模块：sysconfig

ttk: Themed Widgets for Tk

更新的模块：unittest

更新的模块：ElementTree 1.3

构建和 C API 的改变

胶囊

特定于端口的更改：Windows

特定于端口的更改：Mac OS X

特定于 FreeBSD 的更改：

Other Changes and Fixes

移植到 Python 2.7

New Features Added to Python 2.7 Maintenance Releases

Two new environment variables for debug mode

PEP 434: IDLE Enhancement Exception for All Branches

PEP 466: Network Security Enhancements for Python 2.7

PEP 477: Backport ensurepip (PEP 453) to Python 2.7

Bootstrapping pip By Default

文档更改

PEP 476: Enabling certificate verification by default for stdlib http clients

PEP 493：适用于Python 2.7 的HTTPS验证迁移工具

New make regen-all build target

Removal of make touch build target

致谢

Python 2.6 有什么新变化

Python 3.0

开发过程的变化

New Issue Tracker: Roundup

新的文档格式：使用 Sphinx 的 reStructuredText

PEP 343: “with” 语句

Writing Context Managers

contextlib 模块

PEP 366: 从主模块显式相对导入

PEP 370: 分用户的 site-packages 目录

PEP 371: 多任务处理包

PEP 3101: 高级字符串格式

PEP 3105: print 改为函数

PEP 3110: 异常处理的变更

PEP 3112: 字节字面值

PEP 3116: 新 I/O 库

PEP 3118: 修改缓冲区协议

PEP 3119: 抽象基类

PEP 3127: 整型文字支持和语法

PEP 3129: 类装饰器

PEP 3141: A Type Hierarchy for Numbers

fractions 模块

其他语言特性修改

性能优化

Interpreter Changes

新增和改进的模块

ast 模块

future_builtins 模块

The json module: JavaScript Object Notation

plistlib 模块：属性列表解析器

ctypes Enhancements

Improved SSL Support

Deprecations and Removals

构建和 C API 的改变

特定于端口的更改：Windows

特定于端口的更改：Mac OS X

特定于端口的更改：IRIX

移植到Python 2.6

致谢

Python 2.5 有什么新变化

PEP 308: 条件表达式

PEP 309: 部分功能应用

PEP 314: Python软件包的元数据 v1.1

PEP 328: 绝对导入和相对导入

PEP 338: 将模块作为脚本执行

PEP 341: 统一 try/except/finally

PEP 342: 生成器的新特性

PEP 343: “with” 语句

Writing Context Managers

contextlib 模块

PEP 352: 异常作为新型的类

PEP 353: 使用ssize_t作为索引类型

PEP 357: ‘__index__’ 方法

其他语言特性修改

交互解释器变更

性能优化

新增，改进和删除的模块

ctypes 包

ElementTree 包

hashlib 包

sqlite3 包

wsgiref 包

构建和 C API 的改变

Port-Specific Changes

移植到Python 2.5

致谢

Python 2.4 有什么新变化

PEP 218: 内置集合对象

PEP 237: 统一长整数和整数

PEP 289: 生成器表达式

PEP 292: Simpler String Substitutions

PEP 318: Decorators for Functions and Methods

PEP 322: 反向迭代

PEP 324: 新的子进程模块

PEP 327: 十进数据类型

为什么需要十进制？

Decimal 类型

Context 类型

PEP 328: 多行导入

PEP 331: Locale-Independent Float/String Conversions

其他语言特性修改

性能优化

新增，改进和弃用的模块

cookielib

doctest

构建和 C API 的改变

Port-Specific Changes

移植到 Python 2.4

致谢

Python 2.3 有什么新变化

PEP 218: A Standard Set Datatype

PEP 255: Simple Generators

PEP 263: Source Code Encodings

PEP 273: 从ZIP压缩包导入模块

PEP 277: Unicode file name support for Windows NT

PEP 278: 通用换行支持

PEP 279: enumerate()

PEP 282: logging 包

PEP 285: 布尔类型

PEP 293: Codec Error Handling Callbacks

PEP 301: Distutils的软件包索引和元数据

PEP 302: New Import Hooks

PEP 305: 逗号分隔文件

PEP 307: Pickle Enhancements

扩展切片

其他语言特性修改

String Changes

性能优化

新增，改进和弃用的模块

Date/Time 类型

optparse 模块

Pymalloc: A Specialized Object Allocator

构建和 C API 的改变

Port-Specific Changes

Other Changes and Fixes

移植到 Python 2.3

致谢

Python 2.2 有什么新变化

概述

PEPs 252 and 253: Type and Class Changes

Old and New Classes

Descriptors

Multiple Inheritance: The Diamond Rule

Attribute Access

Related Links

PEP 234: Iterators

PEP 255: Simple Generators

PEP 237: 统一长整数和整数

PEP 238: Changing the Division Operator

Unicode Changes

PEP 227: Nested Scopes

新增和改进的模块

Interpreter Changes and Fixes

Other Changes and Fixes

致谢

Python 2.1 有什么新变化

概述

PEP 227: Nested Scopes

PEP 236: __future__ Directives

PEP 207: Rich Comparisons

PEP 230: Warning Framework

PEP 229: New Build System

PEP 205: Weak References

PEP 232: Function Attributes

PEP 235: Importing Modules on Case-Insensitive Platforms

PEP 217: Interactive Display Hook

PEP 208: New Coercion Model

PEP 241: Metadata in Python Packages

新增和改进的模块

Other Changes and Fixes

致谢

Python 2.0 有什么新变化

概述

What About Python 1.6?

新开发流程

Unicode

列表推导式

Augmented Assignment

字符串的方法

Garbage Collection of Cycles

其他核心变化

Minor Language Changes

Changes to Built-in Functions

移植 Python 2.0

扩展/嵌入更改

Distutils：使模块易于安装

XML 模块

SAX2 Support

DOM Support

Relationship to PyXML

模块更改

新增模块

IDLE 改进

删除和弃用的模块

致谢

更新日志

Python 3.6.13 final

安全

核心与内置

库

测试

Python 3.6.12 final

安全

库

Python 3.6.11 final

Python 3.6.11 release candidate 1

安全

核心与内置

库

Python 3.6.10 final

macOS

Python 3.6.10 release candidate 1

安全

库

Python 3.6.9 final

库

macOS

Python 3.6.9 release candidate 1

安全

库

文档

测试

macOS

Python 3.6.8 final

库

构建

C API

Python 3.6.8 release candidate 1

安全

核心与内置

库

文档

测试

构建

Windows

macOS

IDLE

工具/示例

Python 3.6.7 final

Python 3.6.7 release candidate 2

核心与内置

库

文档

测试

macOS

C API

Python 3.6.7 release candidate 1

安全

核心与内置

库

文档

测试

构建

Windows

macOS

IDLE

工具/示例

C API

Python 3.6.6 正式版

Python 3.6.6 RC 1

核心与内置

库

文档

测试

构建

Windows

macOS

IDLE

工具/示例

C API

Python 3.6.5 正式版

测试

构建

Python 3.6.5 rc1

安全

核心与内置

库

文档

测试

构建

Windows

macOS

IDLE

工具/示例

C API

Python 3.6.4 正式版

Python 3.6.4 rc1

核心与内置

库

文档

测试

构建

Windows

macOS

IDLE

工具/示例

C API

Python 3.6.3 正式版

库

构建

Python 3.6.3 rc1

安全

核心与内置

库

文档

测试

构建

Windows

IDLE

工具/示例

Python 3.6.2 正式版

Python 3.6.2 rc2

安全

Python 3.6.2 rc1

核心与内置

库

安全

库

IDLE

C API

构建

文档

工具/示例

测试

Windows

Python 3.6.1 正式版

核心与内置

构建

Python 3.6.1 rc1

核心与内置

库

IDLE

Windows

C API

文档

测试

构建

Python 3.6.0 正式版

Python 3.6.0 rc2

核心与内置

工具/示例

Windows

构建

Python 3.6.0 rc1

核心与内置

库

C API

文档

工具/示例

Python 3.6.0 beta 4

核心与内置

库

文档

测试

构建

Python 3.6.0 beta 3

核心与内置

库

Windows

构建

测试

Python 3.6.0 beta 2

核心与内置

库

Windows

C API

构建

测试

Python 3.6.0 beta 1

核心与内置

库

IDLE

C API

测试

构建

工具/示例

Windows

Python 3.6.0 alpha 4

核心与内置

库

IDLE

测试

Windows

构建

Python 3.6.0 alpha 3

核心与内置

库

安全

库

安全

库

IDLE

C API

构建

工具/示例

文档

测试

Python 3.6.0 alpha 2

核心与内置

库

安全

库

安全

库

IDLE

文档

测试

Windows

构建

Windows

C API

工具/示例

Python 3.6.0 alpha 1

核心与内置

库

安全

库

安全

库

安全

库

IDLE

文档

测试

构建

Windows

工具/示例

C API

Python 3.5.3 正式版

Python 3.5.3 rc1

核心与内置

库

安全

库

安全

库

IDLE

C API

文档

测试

工具/示例

Windows

构建

Python 3.5.2 正式版

核心与内置

测试

IDLE

Python 3.5.2 rc1

核心与内置

安全

库

安全

库

安全

库

安全

库

安全

库

IDLE

文档

测试

构建

Windows

工具/示例

Windows

Python 3.5.1 正式版

核心与内置

Windows

Python 3.5.1 rc1

核心与内置

库

IDLE

文档

测试

构建

Windows

工具/示例

Python 3.5.0 正式版

构建

Python 3.5.0 rc4

库

构建

Python 3.5.0 rc3

核心与内置

库

Python 3.5.0 rc2

核心与内置

库

Python 3.5.0 rc1

核心与内置

库

IDLE

文档

测试

Python 3.5.0 beta 4

核心与内置

库

构建

Python 3.5.0 beta 3

核心与内置

库

测试

文档

构建

Python 3.5.0 beta 2

核心与内置

库

Python 3.5.0 beta 1

核心与内置

库

IDLE

测试

文档

工具/示例

Python 3.5.0 alpha 4

核心与内置

库

构建

测试

工具/示例

C API

Python 3.5.0 alpha 3

核心与内置

库

构建

测试

工具/示例

Python 3.5.0 alpha 2

核心与内置

库

构建

C API

Windows

Python 3.5.0 alpha 1

核心与内置

库

IDLE

构建

C API

文档

测试

工具/示例

Windows

Python 教程

1. 课前甜点

2. 使用 Python 解释器

2.1. 调用解释器

2.1.1. 传入参数

2.1.2. 交互模式

2.2. 解释器的运行环境

2.2.1. 源文件的字符编码

3. Python 的非正式介绍

3.1. Python 作为计算器使用

3.1.1. 数字

3.1.2. 字符串

3.1.3. 列表

3.2. 走向编程的第一步

4. 其他流程控制工具

4.1. if Statements

4.2. for Statements

4.3. range() 函数

4.4. break and continue Statements, and else Clauses on Loops

4.5. pass Statements

4.6. 定义函数

4.7. 函数定义的更多形式

4.7.1. 参数默认值

4.7.2. 关键字参数

4.7.3. 任意的参数列表

4.7.4. 解包参数列表

4.7.5. Lambda 表达式

4.7.6. 文档字符串

4.7.7. 函数标注

4.8. 小插曲：编码风格

5. 数据结构

5.1. 列表的更多特性

5.1.1. 列表作为栈使用

5.1.2. 列表作为队列使用

5.1.3. 列表推导式

5.1.4. 嵌套的列表推导式

5.2. The del statement

5.3. 元组和序列

5.4. 集合

5.5. 字典

5.6. 循环的技巧

5.7. 深入条件控制

5.8. 比较序列和其他类型

6. 模块

6.1. 更多有关模块的信息

6.1.1. 以脚本的方式执行模块

6.1.2. 模块搜索路径

6.1.3. “编译过的”Python文件

6.2. 标准模块

6.3. dir() 函数

6.4. 包

6.4.1. 从包中导入 *

6.4.2. 子包参考

6.4.3. 多个目录中的包

7. 输入输出

7.1. 更漂亮的输出格式

7.1.1. 旧的字符串格式化方法

7.2. 读写文件

7.2.1. 文件对象的方法

7.2.2. 使用 json 保存结构化数据

8. 错误和异常

8.1. 语法错误

8.2. 异常

8.3. 处理异常

8.4. 抛出异常

8.5. 用户自定义异常

8.6. 定义清理操作

8.7. 预定义的清理操作

9. 类

9.1. 名称和对象

9.2. Python 作用域和命名空间

9.2.1. 作用域和命名空间示例

9.3. 初探类

9.3.1. 类定义语法

9.3.2. 类对象

9.3.3. 实例对象

9.3.4. 方法对象

9.3.5. 类和实例变量

9.4. 补充说明

9.5. 继承

9.5.1. 多重继承

9.6. 私有变量

9.7. 杂项说明

9.8. 迭代器

9.9. 生成器

9.10. 生成器表达式

10. 标准库简介

10.1. 操作系统接口

10.2. 文件通配符

10.3. 命令行参数

10.4. 错误输出重定向和程序终止

10.5. 字符串模式匹配

10.6. 数学

10.7. 互联网访问

10.8. 日期和时间

10.9. 数据压缩

10.10. 性能测量

10.11. 质量控制

10.12. 自带电池

11. 标准库简介 —— 第二部分

11.1. 格式化输出

11.2. 模板

11.3. 使用二进制数据记录格式

11.4. 多线程

11.5. 日志记录

11.6. 弱引用

11.7. 用于操作列表的工具

11.8. 十进制浮点运算

12. 虚拟环境和包

12.1. 概述

12.2. 创建虚拟环境

12.3. 使用pip管理包

13. 接下来？

14. 交互式编辑和编辑历史

14.1. Tab 补全和编辑历史

14.2. 默认交互式解释器的替代品

15. 浮点算术：争议和限制

15.1. 表示性错误

16. 附录

16.1. 交互模式

16.1.1. 错误处理

16.1.2. 可执行的Python脚本

16.1.3. 交互式启动文件

16.1.4. 定制模块

Python安装和使用

1. 命令行与环境

1.1. 命令行

1.1.1. 接口选项

1.1.2. 通用选项

1.1.3. 其他选项

1.1.4. 不应当使用的选项

1.2. 环境变量

1.2.1. 调试模式变量

2. 在类Unix环境下使用Python

2.1. 获得并安装Python的最新版本

2.1.1. 在Linux中

2.1.2. 在FreeBSD和OpenBSD上

2.1.3. 在OpenSolaris系统上

2.2. 构建Python

2.3. 与Python相关的路径和文件

2.4. 杂项

2.5. Editors and IDEs

3. 在Windows上使用 Python

3.1. Installing Python

3.1.1. Supported Versions

3.1.2. Installation Steps

3.1.3. 删除 MAX_PATH 限制

3.1.4. 无UI 安装

3.1.5. 免下载安装

3.1.6. 修改安装

3.1.7. 其他平台

3.2. 替代捆绑包

3.3. 配置Python

3.3.1. 附录：设置环境变量

3.3.2. 查找Python可执行文件

3.4. 适用于Windows的Python启动器

3.4.1. 入门

3.4.1.1. 从命令行

3.4.1.2. 从虚拟环境

3.4.1.3. 从脚本

3.4.1.4. 从文件关联

3.4.2. Shebang Lines

3.4.3. shebang lines 的参数

3.4.4. 自定义

3.4.4.1. 通过INI文件自定义

3.4.4.2. 自定义默认的Python版本

3.4.5. 诊断

3.5. 查找模块

3.6. 附加模块

3.6.1. PyWin32

3.6.2. cx_Freeze

3.6.3. WConio

3.7. 在Windows上编译Python

3.8. Embedded Distribution

3.8.1. Python应用程序

3.8.2. 嵌入Python

3.9. Other resources

4. 在苹果系统上使用 Python

4.1. 获取和安装 MacPython

4.1.1. 如何运行 Python 脚本

4.1.2. 运行有图形界面的脚本

4.1.3. 配置

4.2. IDE

4.3. 安装额外的 Python 包

4.4. Mac 上的图形界面编程

4.5. 在 Mac 上分发 Python 应用程序

4.6. 其他资源

Python语言参考

1. 概述

1.1. 其他实现

1.2. 标注

2. 词法分析

2.1. 行结构

2.1.1. 逻辑行

2.1.2. 物理行

2.1.3. 注释

2.1.4. 编码声明

2.1.5. 显式的行拼接

2.1.6. 隐式的行拼接

2.1.7. 空白行

2.1.8. 缩进

2.1.9. 形符之间的空白

2.2. 其他形符

2.3. 标识符和关键字

2.3.1. 关键字

2.3.2. 保留的标识符类

2.4. 字面值

2.4.1. 字符串和字节串字面值

2.4.2. 字符串字面值拼接

2.4.3. 格式化字符串字面值

2.4.4. 数字字面值

2.4.5. 整型数字面值

2.4.6. 浮点数字面值

2.4.7. 虚数字面值

2.5. 运算符

2.6. 分隔符

3. 数据模型

3.1. 对象、值与类型

3.2. 标准类型层级结构

3.3. 特殊方法名称

3.3.1. 基本定制

3.3.2. 自定义属性访问

3.3.2.1. 自定义模块属性访问

3.3.2.2. 实现描述器

3.3.2.3. 发起调用描述器

3.3.2.4. __slots__

3.3.2.4.1. 使用 __slots__ 的注意事项

3.3.3. 自定义类创建

3.3.3.1. 元类

3.3.3.2. 确定适当的元类

3.3.3.3. 准备类命名空间

3.3.3.4. 执行类主体

3.3.3.5. 创建类对象

3.3.3.6. 元类的作用

3.3.4. 自定义实例及子类检查

3.3.5. 模拟可调用对象

3.3.6. 模拟容器类型

3.3.7. 模拟数字类型

3.3.8. with 语句上下文管理器

3.3.9. 特殊方法查找

3.4. 协程

3.4.1. 可等待对象

3.4.2. 协程对象

3.4.3. 异步迭代器

3.4.4. 异步上下文管理器

4. 执行模型

4.1. 程序的结构

4.2. 命名与绑定

4.2.1. 名称的绑定

4.2.2. 名称的解析

4.2.3. 内置命名空间和受限的执行

4.2.4. 与动态特性的交互

4.3. 异常

5. 导入系统

5.1. importlib

5.2. 包

5.2.1. 常规包

5.2.2. 命名空间包

5.3. 搜索

5.3.1. 模块缓存

5.3.2. 查找器和加载器

5.3.3. 导入钩子

5.3.4. 元路径

5.4. 加载

5.4.1. 加载器

5.4.2. 子模块

5.4.3. 模块规格说明

5.4.4. 导入相关的模块属性

5.4.5. module.__path__

5.4.6. 模块的 repr

5.5. 基于路径的查找器

5.5.1. 路径条目查找器

5.5.2. 路径条目查找器协议

5.6. 替换标准导入系统

5.7. 有关 __main__ 的特殊事项

5.7.1. __main__.__spec__

5.8. 开放问题项

5.9. 参考文献

6. 表达式

6.1. 算术转换

6.2. 原子

6.2.1. 标识符（名称）

6.2.2. 字面值

6.2.3. 带圆括号的形式

6.2.4. 列表、集合与字典的显示

6.2.5. 列表显示

6.2.6. 集合显示

6.2.7. 字典显示

6.2.8. 生成器表达式

6.2.9. yield 表达式

6.2.9.1. 生成器-迭代器的方法

6.2.9.2. 例子

6.2.9.3. 异步生成器函数

6.2.9.4. 异步生成器-迭代器方法

6.3. 原型

6.3.1. 属性引用

6.3.2. 抽取

6.3.3. 切片

6.3.4. 调用

6.4. await 表达式

6.5. 幂运算符

6.6. 一元算术和位运算

6.7. 二元算术运算符

6.8. 移位运算

6.9. 二元位运算

6.10. 比较运算

6.10.1. 值比较

6.10.2. 成员检测运算

6.10.3. 标识号比较

6.11. 布尔运算

6.12. 条件表达式

6.13. lambda 表达式

6.14. 表达式列表

6.15. 求值顺序

6.16. 运算符优先级

7. 简单语句

7.1. 表达式语句

7.2. 赋值语句

7.2.1. 增强赋值语句

7.2.2. 带标注的赋值语句

7.3. The assert statement

7.4. The pass statement

7.5. The del statement

7.6. The return statement

7.7. The yield statement

7.8. The raise statement

7.9. The break statement

7.10. The continue statement

7.11. The import statement

7.11.1. future 语句

7.12. The global statement

7.13. The nonlocal statement

8. 复合语句

8.1. The if statement

8.2. The while statement

8.3. The for statement

8.4. The try statement

8.5. The with statement

8.6. 函数定义

8.7. 类定义

8.8. 协程

8.8.1. 协程函数定义

8.8.2. The async for statement

8.8.3. The async with statement

9. 最高层级组件

9.1. 完整的 Python 程序

9.2. 文件输入

9.3. 交互式输入

9.4. 表达式输入

10. 完整的语法规范

Python 标准库

1. 概述

2. 内置函数

3. 内置常量

3.1. 由 site 模块添加的常量

4. 内置类型

4.1. 逻辑值检测

4.2. Boolean Operations — and, or, not

4.3. 比较运算

4.4. 数字类型 — int, float, complex

4.4.1. 整数类型的按位运算

4.4.2. 整数类型的附加方法

4.4.3. 浮点类型的附加方法

4.4.4. 数字类型的哈希运算

4.5. 迭代器类型

4.5.1. 生成器类型

4.6. 序列类型 — list, tuple, range

4.6.1. 通用序列操作

4.6.2. 不可变序列类型

4.6.3. 可变序列类型

4.6.4. 列表

4.6.5. 元组

4.6.6. range 对象

4.7. 文本序列类型 — str

4.7.1. 字符串的方法

4.7.2. printf 风格的字符串格式化

4.8. 二进制序列类型 — bytes, bytearray, memoryview

4.8.1. bytes 对象

4.8.2. bytearray 对象

4.8.3. bytes 和 bytearray 操作

4.8.4. printf 风格的字节串格式化

4.8.5. 内存视图

4.9. 集合类型 — set, frozenset

4.10. 映射类型 — dict

4.10.1. 字典视图对象

4.11. 上下文管理器类型

4.12. 其他内置类型

4.12.1. 模块

4.12.2. 类与类实例

4.12.3. 函数

4.12.4. 方法

4.12.5. 代码对象

4.12.6. 类型对象

4.12.7. 空对象

4.12.8. 省略符对象

4.12.9. 未实现对象

4.12.10. 布尔值

4.12.11. 内部对象

4.13. 特殊属性

5. 内置异常

5.1. 基类

5.2. 具体异常

5.2.1. OS 异常

5.3. 警告

5.4. 异常层次结构

6. 文本处理服务

6.1. string — 常见的字符串操作

6.1.1. 字符串常量

6.1.2. 自定义字符串格式化

6.1.3. 格式字符串语法

6.1.3.1. 格式规格迷你语言

6.1.3.2. 格式示例

6.1.4. 模板字符串

6.1.5. 辅助函数

6.2. re — 正则表达式操作

6.2.1. 正则表达式语法

6.2.2. 模块内容

6.2.3. 正则表达式对象 （正则对象）

6.2.4. 匹配对象

6.2.5. 正则表达式例子

6.2.5.1. 检查对子

6.2.5.2. 模拟 scanf()

6.2.5.3. search() vs. match()

6.2.5.4. 建立一个电话本

6.2.5.5. 文字整理

6.2.5.6. 找到所有副词

6.2.5.7. 找到所有副词和位置

6.2.5.8. 原始字符记法

6.2.5.9. 写一个词法分析器

6.3. difflib — 计算差异的辅助工具

6.3.1. SequenceMatcher 对象

6.3.2. SequenceMatcher 的示例

6.3.3. Differ 对象

6.3.4. Differ 示例

6.3.5. difflib 的命令行接口

6.4. textwrap — 文本自动换行与填充

6.5. unicodedata — Unicode 数据库

6.6. stringprep — 因特网字符串预备

6.7. readline — GNU readline 接口

6.7.1. 初始化文件

6.7.2. 行缓冲区

6.7.3. 历史文件

6.7.4. 历史列表

6.7.5. 启动钩子

6.7.6. Completion

6.7.7. 示例

6.8. rlcompleter — GNU readline 的补全函数

6.8.1. Completer 对象

7. 二进制数据服务

7.1. struct — 将字节串解读为打包的二进制数据

7.1.1. 函数和异常

7.1.2. 格式字符串

7.1.2.1. 字节顺序，大小和对齐方式

7.1.2.2. 格式字符

7.1.2.3. 例子

7.1.3. 类

7.2. codecs — 编解码器注册和相关基类

7.2.1. 编解码器基类

7.2.1.1. 错误处理方案

7.2.1.2. 无状态的编码和解码

7.2.1.3. 增量式的编码和解码

7.2.1.3.1. IncrementalEncoder 对象

7.2.1.3.2. IncrementalDecoder 对象

7.2.1.4. 流式的编码和解码

7.2.1.4.1. StreamWriter 对象

7.2.1.4.2. StreamReader 对象

7.2.1.4.3. StreamReaderWriter 对象

7.2.1.4.4. StreamRecoder 对象

7.2.2. 编码格式与 Unicode

7.2.3. 标准编码

7.2.4. Python 专属的编码格式

7.2.4.1. 文字编码

7.2.4.2. 二进制转换

7.2.4.3. 文字转换

7.2.5. encodings.idna — 应用程序中的国际化域名

7.2.6. encodings.mbcs — Windows ANSI代码页

7.2.7. encodings.utf_8_sig — 带BOM签名的UTF-8编解码器

8. 数据类型

8.1. datetime — 基本的日期和时间类型

8.1.1. 有效的类型

8.1.2. timedelta 类对象

8.1.3. date 对象

8.1.4. datetime 对象

8.1.5. time 对象

8.1.6. tzinfo 对象

8.1.7. timezone 对象

8.1.8. strftime() 和 strptime() 的行为

8.2. calendar — 日历相关函数

8.3. collections — 容器数据类型

8.3.1. ChainMap 对象

8.3.1.1. ChainMap 例子和方法

8.3.2. Counter 对象

8.3.3. deque 对象

8.3.3.1. deque 用法

8.3.4. defaultdict 对象

8.3.4.1. defaultdict 例子

8.3.5. namedtuple() 命名元组的工厂函数

8.3.6. OrderedDict 对象

8.3.6.1. OrderedDict 例子和用法

8.3.7. UserDict 对象

8.3.8. UserList 对象

8.3.9. UserString 对象

8.4. collections.abc — 容器的抽象基类

8.4.1. 容器抽象基类

8.5. heapq — 堆队列算法

8.5.1. 基本示例

8.5.2. 优先队列实现说明

8.5.3. 理论

8.6. bisect — 数组二分查找算法

8.6.1. 搜索有序列表

8.6.2. 其他示例

8.7. array — 高效的数值数组

8.8. weakref — 弱引用

8.8.1. 弱引用对象

8.8.2. 示例

8.8.3. 终结器对象

8.8.4. 比较终结器与 __del__() 方法

8.9. types — 动态类型创建和内置类型名称

8.9.1. 动态类型创建

8.9.2. 标准解释器类型

8.9.3. 附加工具类和函数

8.9.4. 协程工具函数

8.10. copy — 浅层 (shallow) 和深层 (deep) 复制操作

8.11. pprint — 数据美化输出

8.11.1. PrettyPrinter 对象

8.11.2. 示例

8.12. reprlib — 另一种 repr() 实现

8.12.1. Repr 对象

8.12.2. 子类化 Repr 对象

8.13. enum — 枚举类型支持

8.13.1. 模块内容

8.13.2. 创建一个 Enum

8.13.3. 对枚举成员及其属性的程序化访问

8.13.4. 复制枚举成员和值

8.13.5. 确保唯一的枚举值

8.13.6. 使用自动设定的值

8.13.7. 迭代

8.13.8. 比较运算

8.13.9. 允许的枚举成员和属性

8.13.10. Restricted subclassing of enumerations

8.13.11. 封存

8.13.12. 可用 API

8.13.13. 派生的枚举

8.13.13.1. IntEnum

8.13.13.2. IntFlag

8.13.13.3. 标志

8.13.13.4. 其他事项

8.13.14. 有趣的示例

8.13.14.1. 省略值

8.13.14.1.1. 使用 auto

8.13.14.1.2. 使用 object

8.13.14.1.3. 使用描述性字符串

8.13.14.1.4. 使用自定义的 __new__()

8.13.14.2. OrderedEnum

8.13.14.3. DuplicateFreeEnum

8.13.14.4. Planet

8.13.15. 各种枚举有何区别？

8.13.15.1. 枚举类

8.13.15.2. 枚举成员（即实例）

8.13.15.3. 细节要点

8.13.15.3.1. 支持 __dunder__ 名称

8.13.15.3.2. 支持的 _sunder_ 名称

8.13.15.3.3. Enum 成员类型

8.13.15.3.4. Enum 类和成员的布尔值

8.13.15.3.5. 带有方法的 Enum 类

8.13.15.3.6. 组合 Flag 的成员

9. 数字和数学模块

9.1. numbers — 数字的抽象基类

9.1.1. 数字的层次

9.1.2. 类型接口注释。

9.1.2.1. 加入更多数字的ABC

9.1.2.2. 实现算术运算

9.2. math — 数学函数

9.2.1. 数论与表示函数

9.2.2. 幂函数与对数函数

9.2.3. 三角函数

9.2.4. 角度转换

9.2.5. 双曲函数

9.2.6. 特殊函数

9.2.7. 常量

9.3. cmath ——关于复数的数学函数

9.3.1. 到极坐标和从极坐标的转换

9.3.2. 幂函数与对数函数

9.3.3. 三角函数

9.3.4. 双曲函数

9.3.5. 分类函数

9.3.6. 常量

9.4. decimal — 十进制定点和浮点运算

9.4.1. 快速入门教程

9.4.2. Decimal 对象

9.4.2.1. 逻辑操作数

9.4.3. Context 对象

9.4.4. 常量

9.4.5. 舍入模式

9.4.6. 信号

9.4.7. 浮点数说明

9.4.7.1. 通过提升精度来缓解舍入误差

9.4.7.2. 特殊的值

9.4.8. 使用线程

9.4.9. 例程

9.4.10. Decimal FAQ

9.5. fractions — 分数

9.6. random — 生成伪随机数

9.6.1. 簿记功能

9.6.2. 整数用函数

9.6.3. 序列用函数

9.6.4. 实值分布

9.6.5. 替代生成器

9.6.6. 关于再现性的说明

9.6.7. 例子和配方

9.7. statistics — 数学统计函数

9.7.1. 平均值以及对中心位置的评估

9.7.2. 对分散程度的评估

9.7.3. 函数细节

9.7.4. 异常

10. 函数式编程模块

10.1. itertools — 为高效循环而创建迭代器的函数

10.1.1. Itertool函数

10.1.2. Itertools食谱

10.2. functools — 高阶函数和可调用对象上的操作

10.2.1. partial 对象

10.3. operator — 标准运算符替代函数

10.3.1. 将运算符映射到函数

10.3.2. Inplace Operators

11. 文件和目录访问

11.1. pathlib — 面向对象的文件系统路径

11.1.1. 基础使用

11.1.2. 纯路径

11.1.2.1. 通用性质

11.1.2.2. 运算符

11.1.2.3. 访问个别部分

11.1.2.4. 方法和特征属性

11.1.3. 具体路径

11.1.3.1. 方法

11.2. os.path — 常见路径操作

11.3. fileinput — 迭代来自多个输入流的行

11.4. stat — 解析 stat() 结果

11.5. filecmp — 文件及目录的比较

11.5.1. dircmp 类

11.6. tempfile — 生成临时文件和目录

11.6.1. 例子

11.6.2. 已弃用的函数和变量

11.7. glob — Unix 风格路径名模式扩展

11.8. fnmatch — Unix 文件名模式匹配

11.9. linecache — 随机读写文本行

11.10. shutil — 高阶文件操作

11.10.1. 目录和文件操作

11.10.1.1. copytree 示例

11.10.1.2. rmtree 示例

11.10.2. 归档操作

11.10.2.1. 归档程序示例

11.10.3. 查询输出终端的尺寸

11.11. macpath — Mac OS 9 路径操作函数

12. 数据持久化

12.1. pickle —— Python 对象序列化

12.1.1. 与其他 Python 模块间的关系

12.1.1.1. 与 marshal 间的关系

12.1.1.2. 与 json 模块的比较

12.1.2. 数据流格式

12.1.3. 模块接口

12.1.4. 可以被打包/解包的对象

12.1.5. 打包类实例

12.1.5.1. 持久化外部对象

12.1.5.2. Dispatch 表

12.1.5.3. 处理有状态的对象

12.1.6. 限制全局变量

12.1.7. 性能

12.1.8. 例子

12.2. copyreg — 注意 pickle 支持函数

12.2.1. 示例

12.3. shelve — Python 对象持久化

12.3.1. 限制

12.3.2. 示例

12.4. marshal — 内部 Python 对象序列化

12.5. dbm — Unix “数据库” 接口

12.5.1. dbm.gnu — GNU 对 dbm 的重解析

12.5.2. dbm.ndbm — 基于 ndbm 的接口

12.5.3. dbm.dumb — 便携式 DBM 实现

12.6. sqlite3 — SQLite 数据库 DB-API 2.0 接口模块

12.6.1. 模块函数和常量

12.6.2. 连接对象（Connection）

12.6.3. Cursor 对象

12.6.4. 行对象*Row*

12.6.5. 异常

12.6.6. SQLite 与 Python 类型

12.6.6.1. 概述

12.6.6.2. 使用适配器将额外的 Python 类型保存在 SQLite 数据库中。

12.6.6.2.1. 让对象自行调整

12.6.6.2.2. 注册可调用的适配器

12.6.6.3. 将SQLite 值转换为自定义Python 类型

12.6.6.4. 默认适配器和转换器

12.6.7. 控制事务

12.6.8. 有效使用 sqlite3

12.6.8.1. 使用快捷方式

12.6.8.2. 通过名称而不是索引访问索引

12.6.8.3. 使用连接作为上下文管理器

12.6.9. 常见问题

12.6.9.1. 多线程

13. 数据压缩和存档

13.1. zlib — 与 gzip 兼容的压缩

13.2. gzip — 对 gzip 格式的支持

13.2.1. 用法示例

13.3. bz2 — 对 bzip2 压缩算法的支持

13.3.1. 文件压缩和解压

13.3.2. 增量压缩和解压

13.3.3. 一次性压缩或解压

13.4. lzma — 用 LZMA 算法压缩

13.4.1. 读写压缩文件

13.4.2. 在内存中压缩和解压缩数据

13.4.3. 杂项

13.4.4. 指定自定义的过滤器链

13.4.5. 例子

13.5. zipfile — 使用ZIP存档

13.5.1. ZipFile 对象

13.5.2. PyZipFile 对象

13.5.3. ZipInfo 对象

13.5.4. 命令行界面

13.5.4.1. 命令行选项

13.6. tarfile — 读写tar归档文件

13.6.1. TarFile 对象

13.6.2. TarInfo 对象

13.6.3. 命令行界面

13.6.3.1. 命令行选项

13.6.4. 例子

13.6.5. 受支持的 tar 格式

13.6.6. Unicode 问题

14. 文件格式

14.1. csv — CSV 文件读写

14.1.1. 模块内容

14.1.2. 变种与格式参数

14.1.3. Reader 对象

14.1.4. Writer 对象

14.1.5. 例子

14.2. configparser — 配置文件解析器

14.2.1. 快速起步

14.2.2. 支持的数据类型

14.2.3. 回退值

14.2.4. 受支持的 INI 文件结构

14.2.5. 值的插入

14.2.6. 映射协议访问

14.2.7. 定制解析器行为

14.2.8. 旧式 API 示例

14.2.9. ConfigParser 对象

14.2.10. RawConfigParser 对象

14.2.11. 异常

14.3. netrc — netrc 文件处理

14.3.1. netrc 对象

14.4. xdrlib — 编码与解码 XDR 数据

14.4.1. Packer 对象

14.4.2. Unpacker 对象

14.4.3. 异常

14.5. plistlib — 生成与解析 Mac OS X .plist 文件

14.5.1. 例子

15. 加密服务

15.1. hashlib — 安全哈希与消息摘要

15.1.1. 哈希算法

15.1.2. SHAKE 可变长度摘要

15.1.3. 密钥派生

15.1.4. BLAKE2

15.1.4.1. 创建哈希对象

15.1.4.2. 常量

15.1.4.3. 例子

15.1.4.3.1. 简单哈希

15.1.4.3.2. 使用不同的摘要大小

15.1.4.3.3. 密钥哈希

15.1.4.3.4. 随机哈希

15.1.4.3.5. 个性化

15.1.4.3.6. 树形模式

15.1.4.4. 开发人员

15.2. hmac — 基于密钥的消息验证

15.3. secrets — 生成安全随机数字用于管理密码

15.3.1. 随机数

15.3.2. 生成凭据

15.3.2.1. 凭据应当使用多少个字节？

15.3.3. 其他功能

15.3.4. 应用技巧与最佳实践

16. 通用操作系统服务

16.1. os — 操作系统接口模块

16.1.1. 文件名，命令行参数，以及环境变量。

16.1.2. 进程参数

16.1.3. 创建文件对象

16.1.4. 文件描述符操作

16.1.4.1. 查询终端的尺寸

16.1.4.2. 文件描述符的继承

16.1.5. 文件和目录

16.1.5.1. Linux 扩展属性

16.1.6. 进程管理

16.1.7. 调度器接口

16.1.8. 其他系统信息

16.1.9. 随机数

16.2. io — 处理流的核心工具

16.2.1. 概述

16.2.1.1. 文本 I/O

16.2.1.2. 二进制 I/O

16.2.1.3. 原始 I/O

16.2.2. 高阶模块接口

16.2.2.1. 内存中的流

16.2.3. 类的层次结构

16.2.3.1. I/O 基类

16.2.3.2. 原始文件 I/O

16.2.3.3. 缓冲流

16.2.3.4. 文本 I/O

16.2.4. 性能

16.2.4.1. 二进制 I/O

16.2.4.2. 文本 I/O

16.2.4.3. 多线程

16.2.4.4. 可重入性

16.3. time — 时间的访问和转换

16.3.1. 函数

16.3.2. Clock ID 常量

16.3.3. 时区常量

16.4. argparse — 命令行选项、参数和子命令解析器

16.4.1. 示例

16.4.1.1. 创建一个解析器

16.4.1.2. 添加参数

16.4.1.3. 解析参数

16.4.2. ArgumentParser 对象

16.4.2.1. prog

16.4.2.2. usage

16.4.2.3. description

16.4.2.4. epilog

16.4.2.5. parents

16.4.2.6. formatter_class

16.4.2.7. prefix_chars

16.4.2.8. fromfile_prefix_chars

16.4.2.9. argument_default

16.4.2.10. allow_abbrev

16.4.2.11. conflict_handler

16.4.2.12. add_help

16.4.3. add_argument() 方法

16.4.3.1. name or flags

16.4.3.2. action

16.4.3.3. nargs

16.4.3.4. const

16.4.3.5. 默认值

16.4.3.6. type – 类型

16.4.3.7. choices

16.4.3.8. required

16.4.3.9. help

16.4.3.10. metavar

16.4.3.11. dest

16.4.3.12. Action 类

16.4.4. parse_args() 方法

16.4.4.1. 选项值语法

16.4.4.2. 无效的参数

16.4.4.3. 包含 - 的参数

16.4.4.4. 参数缩写（前缀匹配）

16.4.4.5. 在 sys.argv 以外

16.4.4.6. 命名空间对象

16.4.5. 其它实用工具

16.4.5.1. 子命令

16.4.5.2. FileType 对象

16.4.5.3. 参数组

16.4.5.4. 互斥

16.4.5.5. 解析器默认值

16.4.5.6. 打印帮助

16.4.5.7. 部分解析

16.4.5.8. 自定义文件解析

16.4.5.9. 退出方法

16.4.6. 升级 optparse 代码

16.5. getopt — C 风格的命令行选项解析器

16.6. 模块 logging — Python 的日志记录工具

16.6.1. Logger 对象

16.6.2. 日志级别

16.6.3. 处理器对象

16.6.4. 格式器对象

16.6.5. Filter 对象

16.6.6. LogRecord 属性

16.6.7. LogRecord 属性

16.6.8. LoggerAdapter 对象

16.6.9. 线程安全

16.6.10. 模块级别函数

16.6.11. 模块级属性

16.6.12. 与警告模块集成

16.7. logging.config — 日志记录配置

16.7.1. 配置函数

16.7.2. 配置字典架构

16.7.2.1. 字典架构细节

16.7.2.2. 增量配置

16.7.2.3. 对象连接

16.7.2.4. 用户定义对象

16.7.2.5. 访问外部对象

16.7.2.6. 访问内部对象

16.7.2.7. 导入解析与定制导入器

16.7.3. 配置文件格式

16.8. logging.handlers — 日志处理

16.8.1. StreamHandler

16.8.2. FileHandler

16.8.3. NullHandler

16.8.4. WatchedFileHandler

16.8.5. BaseRotatingHandler

16.8.6. RotatingFileHandler

16.8.7. TimedRotatingFileHandler

16.8.8. SocketHandler

16.8.9. DatagramHandler

16.8.10. SysLogHandler

16.8.11. NTEventLogHandler

16.8.12. SMTPHandler

16.8.13. MemoryHandler

16.8.14. HTTPHandler

16.8.15. QueueHandler

16.8.16. QueueListener

16.9. getpass — 便携式密码输入工具

16.10. curses — 终端字符单元显示的处理

16.10.1. 函数

16.10.2. Window 对象

16.10.3. 常量

16.11. curses.textpad — 用于 curses 程序的文本输入控件

16.11.1. 文本框对象

16.12. curses.ascii — 用于 ASCII 字符的工具

16.13. curses.panel — curses 的 panel 栈扩展

16.13.1. 函数

16.13.2. Panel 对象

16.14. platform — 获取底层平台的标识数据

16.14.1. 跨平台

16.14.2. Java平台

16.14.3. Windows平台

16.14.3.1. Win95/98 specific

16.14.4. Mac OS平台

16.14.5. Unix 平台

16.15. errno — 标准 errno 系统符号

16.16. ctypes — Python 的外部函数库

16.16.1. ctypes 教程

16.16.1.1. 载入动态连接库

16.16.1.2. 操作导入的动态链接库中的函数

16.16.1.3. 调用函数

16.16.1.4. 基础数据类型

16.16.1.5. 调用函数，继续

16.16.1.6. 使用自定义的数据类型调用函数

16.16.1.7. 指定必选参数的类型(函数原型)

16.16.1.8. 返回类型

16.16.1.9. 传递指针(或者传递引用)

16.16.1.10. 结构体和联合

16.16.1.11. 结构体/联合 字段对齐及字节顺序

16.16.1.12. 结构体和联合中的位域

16.16.1.13. 数组

16.16.1.14. 指针

16.16.1.15. 类型强制转换

16.16.1.16. 不完整类型

16.16.1.17. 回调函数

16.16.1.18. 访问 dll 中导出的值

16.16.1.19. 意外

16.16.1.20. 变长数据类型

16.16.2. ctypes 参考手册

16.16.2.1. 寻找动态链接库

16.16.2.2. 加载动态链接库

16.16.2.3. 外部函数

16.16.2.4. 函数原型

16.16.2.5. 工具函数

16.16.2.6. 数据类型

16.16.2.7. 基础数据类型

16.16.2.8. 结构化数据类型

16.16.2.9. 数组与指针

17. 并发执行

17.1. threading — 基于线程的并行

17.1.1. 线程本地数据

17.1.2. 线程对象

17.1.3. 锁对象

17.1.4. 递归锁对象

17.1.5. 条件对象

17.1.6. 信号量对象

17.1.6.1. Semaphore 例子

17.1.7. 事件对象

17.1.8. 定时器对象

17.1.9. 栅栏对象

17.1.10. Using locks, conditions, and semaphores in the with statement

17.2. multiprocessing — 基于进程的并行

17.2.1. 概述

17.2.1.1. Process 类

17.2.1.2. 上下文和启动方法

17.2.1.3. 在进程之间交换对象

17.2.1.4. 进程之间的同步

17.2.1.5. 在进程之间共享状态

17.2.1.6. 使用工作进程

17.2.2. 参考

17.2.2.1. Process 和异常

17.2.2.2. 管道和队列

17.2.2.3. 杂项

17.2.2.4. 连接对象（Connection）

17.2.2.5. 同步原语

17.2.2.6. 共享 ctypes 对象

17.2.2.6.1. multiprocessing.sharedctypes 模块

17.2.2.7. 数据管理器

17.2.2.7.1. 自定义管理器

17.2.2.7.2. 使用远程管理器

17.2.2.8. 代理对象

17.2.2.8.1. 清理

17.2.2.9. 进程池

17.2.2.10. 监听者及客户端

17.2.2.10.1. 地址格式

17.2.2.11. 认证密码

17.2.2.12. 日志记录

17.2.2.13. multiprocessing.dummy 模块

17.2.3. 编程指导

17.2.3.1. 所有启动方法

17.2.3.2. spawn 和 forkserver 启动方式

17.2.4. 例子

17.3. concurrent 包

17.4. concurrent.futures — 启动并行任务

17.4.1. Executor 对象

17.4.2. ThreadPoolExecutor

17.4.2.1. ThreadPoolExecutor 例子

17.4.3. ProcessPoolExecutor

17.4.3.1. ProcessPoolExecutor 例子

17.4.4. Future 对象

17.4.5. 模块函数

17.4.6. Exception类

17.5. subprocess — 子进程管理

17.5.1. 使用 subprocess 模块

17.5.1.1. 常用参数

17.5.1.2. Popen 构造函数

17.5.1.3. 异常

17.5.2. 安全考量

17.5.3. Popen 对象

17.5.4. Windows Popen 助手

17.5.4.1. Constants

17.5.5. 较旧的高阶 API

17.5.6. 使用 subprocess 模块替换旧函数

17.5.6.1. Replacing /bin/sh shell backquote

17.5.6.2. 替代 shell 管道

17.5.6.3. 替代 os.system()

17.5.6.4. 替代 os.spawn 函数族

17.5.6.5. 替代 os.popen(), os.popen2(), os.popen3()

17.5.6.6. 来自 popen2 模块的替代函数

17.5.7. 旧式的 Shell 发起函数

17.5.8. 注释

17.5.8.1. 在 Windows 上将参数列表转换为一个字符串

17.6. sched — 事件调度器

17.6.1. 调度器对象

17.7. queue — 一个同步的队列类

17.7.1. Queue对象

17.8. dummy_threading — 可直接替代 threading 模块。

17.9. _thread — 底层多线程 API

17.10. _dummy_thread — _thread 的替代模块

18. Interprocess Communication and Networking

18.1. socket — 底层网络接口

18.1.1. 套接字协议族

18.1.2. 模块内容

18.1.2.1. 异常

18.1.2.2. 常量

18.1.2.3. 函数

18.1.2.3.1. 创建套接字

18.1.2.3.2. 其他功能

18.1.3. 套接字对象

18.1.4. 关于套接字超时的说明

18.1.4.1. 超时与 connect 方法

18.1.4.2. 超时与 accept 方法

18.1.5. 示例

18.2. ssl — 套接字对象的TLS/SSL封装

18.2.1. 方法、常量和异常处理

18.2.1.1. 套接字创建

18.2.1.2. 上下文创建

18.2.1.3. 随机生成

18.2.1.4. 证书处理

18.2.1.5. 常量

18.2.2. SSL 套接字

18.2.3. SSL 上下文

18.2.4. 证书

18.2.4.1. 证书链

18.2.4.2. CA 证书

18.2.4.3. 合并的密钥和证书

18.2.4.4. 自签名证书

18.2.5. 例子

18.2.5.1. 检测 SSL 支持

18.2.5.2. 客户端操作

18.2.5.3. 服务器端操作

18.2.6. 关于非阻塞套接字的说明

18.2.7. 内存 BIO 支持

18.2.8. SSL 会话

18.2.9. 安全考量

18.2.9.1. 最佳默认值

18.2.9.2. 手动设置

18.2.9.2.1. 验证证书

18.2.9.2.2. 协议版本

18.2.9.2.3. 密码选择

18.2.9.3. 多进程

18.2.10. LibreSSL 支持

18.3. select — Waiting for I/O 完成

18.3.1. /dev/poll 轮询对象

18.3.2. 边缘触发和水平触发的轮询 (epoll) 对象

18.3.3. Poll 对象

18.3.4. Kqueue 对象

18.3.5. Kevent 对象

18.4. selectors — 高级 I/O 复用库

18.4.1. 概述

18.4.2. 类

18.4.3. 例子

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks

18.5.1. Base Event Loop

18.5.1.1. Run an event loop

18.5.1.2. Calls

18.5.1.3. Delayed calls

18.5.1.4. Futures

18.5.1.5. Tasks

18.5.1.6. Creating connections

18.5.1.7. Creating listening connections

18.5.1.8. Watch file descriptors

18.5.1.9. Low-level socket operations

18.5.1.10. Resolve host name

18.5.1.11. Connect pipes

18.5.1.12. UNIX signals

18.5.1.13. Executor

18.5.1.14. 错误处理API

18.5.1.15. Debug mode

18.5.1.16. Server

18.5.1.17. Handle

18.5.1.18. Event loop examples

18.5.1.18.1. call_soon() 的 Hello World 示例。

18.5.1.18.2. 使用 call_later() 来展示当前的日期

18.5.1.18.3. 监控一个文件描述符的读事件

18.5.1.18.4. 为SIGINT和SIGTERM设置信号处理器

18.5.2. 事件循环

18.5.2.1. 事件循环函数

18.5.2.2. 可用的事件循环

18.5.2.3. 平台支持

18.5.2.3.1. Windows

18.5.2.3.2. Mac OS X

18.5.2.4. 事件循环策略和默认策略

18.5.2.5. 事件循环策略接口

18.5.2.6. 访问全局循环策略

18.5.2.7. 自定义事件循环策略

18.5.3. Tasks and coroutines

18.5.3.1. 协程

18.5.3.1.1. Example: Hello World coroutine

18.5.3.1.2. Example: Coroutine displaying the current date

18.5.3.1.3. Example: Chain coroutines

18.5.3.2. InvalidStateError

18.5.3.3. TimeoutError

18.5.3.4. Future

18.5.3.4.1. Example: Future with run_until_complete()

18.5.3.4.2. Example: Future with run_forever()

18.5.3.5. Task

18.5.3.5.1. Example: Parallel execution of tasks

18.5.3.6. Task functions

18.5.4. Transports and protocols (callback based API)

18.5.4.1. 传输

18.5.4.1.1. BaseTransport

18.5.4.1.2. ReadTransport

18.5.4.1.3. WriteTransport

18.5.4.1.4. DatagramTransport

18.5.4.1.5. BaseSubprocessTransport

18.5.4.2. 协议

18.5.4.2.1. Protocol classes

18.5.4.2.2. Connection callbacks

18.5.4.2.3. Streaming protocols

18.5.4.2.4. Datagram protocols

18.5.4.2.5. Flow control callbacks

18.5.4.2.6. Coroutines and protocols

18.5.4.3. Protocol examples

18.5.4.3.1. TCP echo client protocol

18.5.4.3.2. TCP echo server protocol

18.5.4.3.3. UDP echo client protocol

18.5.4.3.4. UDP echo server protocol

18.5.4.3.5. Register an open socket to wait for data using a protocol

18.5.5. Streams (coroutine based API)

18.5.5.1. Stream functions

18.5.5.2. StreamReader

18.5.5.3. StreamWriter

18.5.5.4. StreamReaderProtocol

18.5.5.5. IncompleteReadError

18.5.5.6. LimitOverrunError

18.5.5.7. Stream examples

18.5.5.7.1. 使用流的 TCP 回显客户端

18.5.5.7.2. 使用流的 TCP 回显服务器

18.5.5.7.3. 获取 HTTP 标头

18.5.5.7.4. 注册一个打开的套接字以等待使用流的数据

18.5.6. Subprocess

18.5.6.1. Windows event loop

18.5.6.2. Create a subprocess: high-level API using Process

18.5.6.3. Create a subprocess: low-level API using subprocess.Popen

18.5.6.4. 常量

18.5.6.5. Process

18.5.6.6. Subprocess and threads

18.5.6.7. Subprocess examples

18.5.6.7.1. Subprocess using transport and protocol

18.5.6.7.2. Subprocess using streams

18.5.7. Synchronization primitives

18.5.7.1. Locks

18.5.7.1.1. Lock

18.5.7.1.2. Event

18.5.7.1.3. Condition

18.5.7.2. Semaphores

18.5.7.2.1. Semaphore

18.5.7.2.2. BoundedSemaphore

18.5.8. 队列集

18.5.8.1. 队列

18.5.8.2. PriorityQueue

18.5.8.3. LifoQueue

18.5.8.3.1. 异常

18.5.9. Develop with asyncio

18.5.9.1. Debug mode of asyncio

18.5.9.2. Cancellation

18.5.9.3. Concurrency and multithreading

18.5.9.4. Handle blocking functions correctly

18.5.9.5. 日志记录

18.5.9.6. Detect coroutine objects never scheduled

18.5.9.7. Detect exceptions never consumed

18.5.9.8. Chain coroutines correctly

18.5.9.9. Pending task destroyed

18.5.9.10. Close transports and event loops

18.6. asyncore — 异步socket处理器

18.6.1. asyncore 示例基本 HTTP 客户端

18.6.2. asyncore 示例基本回显服务器

18.7. asynchat — 异步 socket 指令/响应 处理器

18.7.1. asynchat 示例

18.8. signal — 设置异步事件处理程序

18.8.1. 一般规则

18.8.1.1. 执行 Python 信号处理程序

18.8.1.2. 信号与线程

18.8.2. 模块内容

18.8.3. 示例

18.9. mmap — 内存映射文件支持

19. 互联网数据处理

19.1. email — 电子邮件与 MIME 处理包

19.1.1. email.message: 表示一封电子邮件信息

19.1.2. email.parser: 解析电子邮件信息

19.1.2.1. FeedParser API

19.1.2.2. Parser API

19.1.2.3. 附加说明

19.1.3. email.generator: 生成 MIME 文档

19.1.4. email.policy: 策略对象

19.1.5. email.errors: 异常和缺陷类

19.1.6. email.headerregistry: 自定义标头对象

19.1.7. email.contentmanager: 管理 MIME 内容

19.1.7.1. 内容管理器实例

19.1.8. email: 示例

19.1.9. email.message.Message: 使用 compat32 API 来表示电子邮件消息

19.1.10. email.mime: Creating email and MIME objects from scratch

19.1.11. email.header: Internationalized headers

19.1.12. email.charset: Representing character sets

19.1.13. email.encoders: 编码器

19.1.14. email.utils: 其他工具

19.1.15. email.iterators: 迭代器

19.2. json — JSON 编码和解码器

19.2.1. 基本使用

19.2.2. 编码器和解码器

19.2.3. 异常

19.2.4. 标准符合性和互操作性

19.2.4.1. 字符编码

19.2.4.2. Infinite 和 NaN 数值。

19.2.4.3. 对象中的重复名称

19.2.4.4. 顶级非对象，非数组值

19.2.4.5. 实现限制

19.2.5. 命令行界面

19.2.5.1. 命令行选项

19.3. mailcap — Mailcap 文件处理

19.4. mailbox — Manipulate mailboxes in various formats

19.4.1. Mailbox 对象

19.4.1.1. Maildir

19.4.1.2. mbox

19.4.1.3. MH

19.4.1.4. Babyl

19.4.1.5. MMDF

19.4.2. Message objects

19.4.2.1. MaildirMessage

19.4.2.2. mboxMessage

19.4.2.3. MHMessage

19.4.2.4. BabylMessage

19.4.2.5. MMDFMessage

19.4.3. 异常

19.4.4. 例子

19.5. mimetypes — Map filenames to MIME types

19.5.1. MimeTypes Objects

19.6. base64 — Base16, Base32, Base64, Base85 数据编码

19.7. binhex — 对binhex4文件进行编码和解码

19.7.1. 注释

19.8. binascii — 二进制和 ASCII 码互转

19.9. quopri — 编码与解码经过 MIME 转码的可打印数据

19.10. uu — 对 uuencode 文件进行编码与解码

20. 结构化标记处理工具

20.1. html — 超文本标记语言支持

20.2. html.parser — 简单的 HTML 和 XHTML 解析器

20.2.1. HTML 解析器的示例程序

20.2.2. HTMLParser 方法

20.2.3. 例子

20.3. html.entities — HTML 一般实体的定义

20.4. XML处理模块

20.4.1. XML 漏洞

20.4.2. defusedxml 和 defusedexpat 软件包

20.5. xml.etree.ElementTree — ElementTree XML API

20.5.1. 教程

20.5.1.1. XML树和元素

20.5.1.2. 解析XML

20.5.1.3. Pull API进行非阻塞解析

20.5.1.4. 寻找有趣的元素

20.5.1.5. 修改XML文件

20.5.1.6. 构建XML文档

20.5.1.7. 使用命名空间解析XML

20.5.1.8. 其他资源

20.5.2. XPath支持

20.5.2.1. 示例

20.5.2.2. 支持的XPath语法

20.5.3. 参考

20.5.3.1. 函数

20.5.3.2. 元素对象

20.5.3.3. ElementTree 对象

20.5.3.4. QName Objects

20.5.3.5. TreeBuilder Objects

20.5.3.6. XMLParser对象

20.5.3.7. XMLPullParser对象

20.5.3.8. 异常

20.6. xml.dom — The Document Object Model API

20.6.1. 模块内容

20.6.2. Objects in the DOM

20.6.2.1. DOMImplementation Objects

20.6.2.2. 节点对象

20.6.2.3. 节点列表对象

20.6.2.4. 文档类型对象

20.6.2.5. 文档对象

20.6.2.6. 元素对象

20.6.2.7. Attr 对象

20.6.2.8. NamedNodeMap 对象

20.6.2.9. 注释对象

20.6.2.10. Text 和 CDATASection 对象

20.6.2.11. ProcessingInstruction 对象

20.6.2.12. 异常

20.6.3. 一致性

20.6.3.1. 类型映射

20.6.3.2. Accessor Methods

20.7. xml.dom.minidom — Minimal DOM implementation

20.7.1. DOM Objects

20.7.2. DOM Example

20.7.3. minidom and the DOM standard

20.8. xml.dom.pulldom — Support for building partial DOM trees

20.8.1. DOMEventStream Objects

20.9. xml.sax — Support for SAX2 parsers

20.9.1. SAXException Objects

20.10. xml.sax.handler — Base classes for SAX handlers

20.10.1. ContentHandler 对象

20.10.2. DTDHandler 对象

20.10.3. EntityResolver 对象

20.10.4. ErrorHandler 对象

20.11. xml.sax.saxutils — SAX 工具集

20.12. xml.sax.xmlreader — Interface for XML parsers

20.12.1. XMLReader 对象

20.12.2. IncrementalParser 对象

20.12.3. Locator 对象

20.12.4. InputSource 对象

20.12.5. The Attributes Interface

20.12.6. The AttributesNS Interface

20.13. xml.parsers.expat — Fast XML parsing using Expat

20.13.1. XMLParser对象

20.13.2. ExpatError Exceptions

20.13.3. 示例

20.13.4. Content Model Descriptions

20.13.5. Expat error constants

21. 互联网协议和支持

21.1. webbrowser — 方便的Web浏览器控制器

21.1.1. 浏览器控制器对象

21.2. cgi — Common Gateway Interface support

21.2.1. 概述

21.2.2. 使用cgi模块。

21.2.3. Higher Level Interface

21.2.4. 函数

21.2.5. Caring about security

21.2.6. Installing your CGI script on a Unix system

21.2.7. Testing your CGI script

21.2.8. Debugging CGI scripts

21.2.9. Common problems and solutions

21.3. cgitb — 用于 CGI 脚本的回溯管理器

21.4. wsgiref — WSGI Utilities and Reference Implementation

21.4.1. wsgiref.util – WSGI environment utilities

21.4.2. wsgiref.headers – WSGI response header tools

21.4.3. wsgiref.simple_server – a simple WSGI HTTP server

21.4.4. wsgiref.validate — WSGI conformance checker

21.4.5. wsgiref.handlers – server/gateway base classes

21.4.6. 例子

21.5. urllib — URL 处理模块

21.6. urllib.request — 用于打开 URL 的可扩展库

21.6.1. Request 对象

21.6.2. OpenerDirector 对象

21.6.3. BaseHandler 对象

21.6.4. HTTPRedirectHandler 对象

21.6.5. HTTPCookieProcessor 对象

21.6.6. ProxyHandler 对象

21.6.7. HTTPPasswordMgr 对象

21.6.8. HTTPPasswordMgrWithPriorAuth 对象

21.6.9. AbstractBasicAuthHandler 对象

21.6.10. HTTPBasicAuthHandler 对象

21.6.11. ProxyBasicAuthHandler 对象

21.6.12. AbstractDigestAuthHandler 对象

21.6.13. HTTPDigestAuthHandler 对象

21.6.14. ProxyDigestAuthHandler 对象

21.6.15. HTTPHandler 对象

21.6.16. HTTPSHandler 对象

21.6.17. FileHandler 对象

21.6.18. DataHandler 对象

21.6.19. FTPHandler 对象

21.6.20. CacheFTPHandler 对象

21.6.21. UnknownHandler 对象

21.6.22. HTTPErrorProcessor 对象

21.6.23. 例子

21.6.24. Legacy interface

21.6.25. urllib.request Restrictions

21.7. urllib.response — urllib 使用的 Response 类

21.8. urllib.parse — Parse URLs into components

21.8.1. URL 解析

21.8.2. 解析ASCII编码字节

21.8.3. 结构化解析结果

21.8.4. URL Quoting

21.9. urllib.error — urllib.request 引发的异常类

21.10. urllib.robotparser — robots.txt 语法分析程序

21.11. http — HTTP 模块

21.11.1. HTTP 状态码

21.12. http.client — HTTP 协议客户端

21.12.1. HTTPConnection 对象

21.12.2. HTTPResponse 对象

21.12.3. 例子

21.12.4. HTTPMessage Objects

21.13. ftplib — FTP 协议客户端

21.13.1. FTP 对象

21.13.2. FTP_TLS 对象

21.14. poplib — POP3 协议客户端

21.14.1. POP3 对象

21.14.2. POP3 示例

21.15. imaplib — IMAP4 协议客户端

21.15.1. IMAP4 Objects

21.15.2. IMAP4 Example

21.16. nntplib — NNTP protocol client

21.16.1. NNTP Objects

21.16.1.1. Attributes

21.16.1.2. 方法

21.16.2. 工具函数

21.17. smtplib —SMTP协议客户端

21.17.1. SMTP Objects

21.17.2. SMTP Example

21.18. smtpd — SMTP 服务器

21.18.1. SMTPServer 对象

21.18.2. DebuggingServer 对象

21.18.3. PureProxy对象

21.18.4. MailmanProxy 对象

21.18.5. SMTPChannel 对象

21.19. telnetlib — Telnet client

21.19.1. Telnet Objects

21.19.2. Telnet Example

21.20. uuid — UUID objects according to RFC 4122

21.20.1. 示例

21.21. socketserver — A framework for network servers

21.21.1. Server Creation Notes

21.21.2. Server 对象

21.21.3. Request Handler Objects

21.21.4. 例子

21.21.4.1. socketserver.TCPServer Example

21.21.4.2. socketserver.UDPServer Example

21.21.4.3. Asynchronous Mixins

21.22. http.server — HTTP 服务器

21.23. http.cookies — HTTP状态管理

21.23.1. Cookie 对象

21.23.2. Morsel 对象

21.23.3. 示例

21.24. http.cookiejar —— HTTP 客户端的 Cookie 处理

21.24.1. CookieJar 和 FileCookieJar 对象

21.24.2. FileCookieJar subclasses and co-operation with web browsers

21.24.3. CookiePolicy 对象

21.24.4. DefaultCookiePolicy 对象

21.24.5. Cookie 对象

21.24.6. 例子

21.25. xmlrpc — XMLRPC 服务端与客户端模块

21.26. xmlrpc.client — XML-RPC client access

21.26.1. ServerProxy 对象

21.26.2. DateTime 对象

21.26.3. Binary 对象

21.26.4. Fault 对象

21.26.5. ProtocolError 对象

21.26.6. MultiCall 对象

21.26.7. Convenience Functions

21.26.8. Example of Client Usage

21.26.9. Example of Client and Server Usage

21.27. xmlrpc.server — Basic XML-RPC servers

21.27.1. SimpleXMLRPCServer Objects

21.27.1.1. SimpleXMLRPCServer Example

21.27.2. CGIXMLRPCRequestHandler

21.27.3. Documenting XMLRPC server

21.27.4. DocXMLRPCServer Objects

21.27.5. DocCGIXMLRPCRequestHandler

21.28. ipaddress — IPv4/IPv6 操作库

21.28.1. 方便的工厂函数

21.28.2. IP 地址

21.28.2.1. 地址对象

21.28.2.2. Conversion to Strings and Integers

21.28.2.3. 运算符

21.28.2.3.1. Comparison operators

21.28.2.3.2. Arithmetic operators

21.28.3. IP Network definitions

21.28.3.1. Prefix, net mask and host mask

21.28.3.2. Network objects

21.28.3.3. 运算符

21.28.3.3.1. Logical operators

21.28.3.3.2. 迭代

21.28.3.3.3. Networks as containers of addresses

21.28.4. Interface objects

21.28.4.1. 运算符

21.28.4.1.1. Logical operators

21.28.5. Other Module Level Functions

21.28.6. Custom Exceptions

22. 多媒体服务

22.1. audioop — 处理原始音频数据

22.2. aifc — 读写 AIFF 和 AIFC 文件

22.3. sunau — 读写 Sun AU 文件

22.3.1. AU_read 对象

22.3.2. AU_write 对象

22.4. wave — 读写WAV格式文件

22.4.1. Wave_read对象

22.4.2. Wave_write 对象

22.5. chunk — 读取 IFF 分块数据

22.6. colorsys — 颜色系统间的转换

22.7. imghdr — 推测图像类型

22.8. sndhdr — 推测声音文件的类型

22.9. ossaudiodev — Access to OSS-compatible audio devices

22.9.1. Audio Device Objects

22.9.2. Mixer Device Objects

23. 国际化

23.1. gettext — 多语种国际化服务

23.1.1. GNU gettext API

23.1.2. 基于类的 API

23.1.2.1. NullTranslations 类

23.1.2.2. GNUTranslations 类

23.1.2.3. Solaris 消息编目支持

23.1.2.4. 编目构造器

23.1.3. 国际化 (I18N) 你的程序和模块

23.1.3.1. 本地化你的模块

23.1.3.2. 本地化你的应用程序

23.1.3.3. 即时更改语言

23.1.3.4. 延迟翻译

23.1.4. 致谢

23.2. locale — 国际化服务

23.2.1. Background, details, hints, tips and caveats

23.2.2. For extension writers and programs that embed Python

23.2.3. Access to message catalogs

24. 程序框架

24.1. turtle — 海龟绘图

24.1.1. 概述

24.1.2. 可用的 Turtle 和 Screen 方法概览

24.1.2.1. Turtle 方法

24.1.2.2. TurtleScreen/Screen 方法

24.1.3. RawTurtle/Turtle 方法和对应函数

24.1.3.1. 海龟动作

24.1.3.2. 获取海龟的状态

24.1.3.3. 度量单位设置

24.1.3.4. 画笔控制

24.1.3.4.1. 绘图状态

24.1.3.4.2. 颜色控制

24.1.3.4.3. 填充

24.1.3.4.4. 更多绘图控制

24.1.3.5. 海龟状态

24.1.3.5.1. 可见性

24.1.3.5.2. 外观

24.1.3.6. 使用事件

24.1.3.7. 特殊海龟方法

24.1.3.8. 复合形状

24.1.4. TurtleScreen/Screen 方法及对应函数

24.1.4.1. 窗口控制

24.1.4.2. 动画控制

24.1.4.3. 使用屏幕事件

24.1.4.4. 输入方法

24.1.4.5. 设置与特殊方法

24.1.4.6. Screen 专有方法, 而非继承自 TurtleScreen

24.1.5. 公共类

24.1.6. 帮助与配置

24.1.6.1. 如何使用帮助

24.1.6.2. 文档字符串翻译为不同的语言

24.1.6.3. 如何配置 Screen 和 Turtle

24.1.7. turtledemo — 演示脚本集

24.1.8. Python 2.6 之后的变化

24.1.9. Python 3.0 之后的变化

24.2. cmd — 支持面向行的命令解释器

24.2.1. Cmd 对象

24.2.2. Cmd 例子

24.3. shlex — Simple lexical analysis

24.3.1. shlex Objects

24.3.2. Parsing Rules

24.3.3. Improved Compatibility with Shells

25. Tk图形用户界面(GUI)

25.1. tkinter — Tcl/Tk的Python接口

25.1.1. Tkinter 模块

25.1.2. Tkinter Life Preserver

25.1.2.1. How To Use This Section

25.1.2.2. A Simple Hello World Program

25.1.3. A (Very) Quick Look at Tcl/Tk

25.1.4. Mapping Basic Tk into Tkinter

25.1.5. How Tk and Tkinter are Related

25.1.6. Handy Reference

25.1.6.1. Setting Options

25.1.6.2. The Packer

25.1.6.3. Packer Options

25.1.6.4. Coupling Widget Variables

25.1.6.5. The Window Manager

25.1.6.6. Tk Option Data Types

25.1.6.7. Bindings and Events

25.1.6.8. The index Parameter

25.1.6.9. Images

25.1.7. File Handlers

25.2. tkinter.ttk — Tk主题小部件

25.2.1. 使用 Ttk

25.2.2. Ttk 部件

25.2.3. 控件

25.2.3.1. 标准选项

25.2.3.2. 可滚动控件选项

25.2.3.3. 标签选项

25.2.3.4. 兼容性选项

25.2.3.5. 控件状态

25.2.3.6. ttk.Widget

25.2.4. 组合框

25.2.4.1. 选项

25.2.4.2. 虚拟事件

25.2.4.3. ttk.Combobox

25.2.5. 笔记本

25.2.5.1. 选项

25.2.5.2. Tab 选项

25.2.5.3. Tab Identifiers

25.2.5.4. Virtual Events

25.2.5.5. ttk.Notebook

25.2.6. Progressbar

25.2.6.1. 选项

25.2.6.2. ttk.Progressbar

25.2.7. Separator

25.2.7.1. 选项

25.2.8. Sizegrip

25.2.8.1. Platform-specific notes

25.2.8.2. Bugs

25.2.9. Treeview

25.2.9.1. 选项

25.2.9.2. Item Options

25.2.9.3. Tag Options

25.2.9.4. Column Identifiers

25.2.9.5. Virtual Events

25.2.9.6. ttk.Treeview

25.2.10. Ttk Styling

25.2.10.1. Layouts

25.3. tkinter.tix — Extension widgets for Tk

25.3.1. Using Tix

25.3.2. Tix Widgets

25.3.2.1. Basic Widgets

25.3.2.2. File Selectors

25.3.2.3. Hierarchical ListBox

25.3.2.4. Tabular ListBox

25.3.2.5. Manager Widgets

25.3.2.6. Image Types

25.3.2.7. Miscellaneous Widgets

25.3.2.8. Form Geometry Manager

25.3.3. Tix Commands

25.4. tkinter.scrolledtext — 滚动文字控件

25.5. IDLE

25.5.1. 目录

25.5.1.1. 文件目录 （命令行和编辑器）

25.5.1.2. 编辑目录（命令行和编辑器）

25.5.1.3. 格式菜单（仅 window 编辑器）

25.5.1.4. 运行菜单（仅 window 编辑器）

25.5.1.5. Shell 菜单（仅 window 编辑器）

25.5.1.6. 调试菜单（仅 window 编辑器）

25.5.1.7. 选项菜单（命令行和编辑器）

25.5.1.8. Window 菜单（命令行和编辑器）

25.5.1.9. 帮助菜单（命令行和编辑器）

25.5.1.10. 上下文菜单

25.5.2. 编辑和导航

25.5.2.1. 编辑窗口

25.5.2.2. 按键绑定

25.5.2.3. 自动缩进

25.5.2.4. 完成

25.5.2.5. 提示

25.5.2.6. Python Shell 窗口

25.5.2.7. 文本颜色

25.5.3. 启动和代码执行

25.5.3.1. 命令行语法

25.5.3.2. 启动失败

25.5.3.3. 运行用户代码

25.5.3.4. Shell中的用户输出

25.5.3.5. 开发 tkinter 应用程序

25.5.3.6. 在没有子进程的情况下运行

25.5.4. 帮助和偏好

25.5.4.1. 帮助资源

25.5.4.2. 偏好设定

25.5.4.3. macOS 上的IDLE

25.5.4.4. 扩展

25.6. 其他图形用户界面（GUI）包

26. 开发工具

26.1. typing — 类型标注支持

26.1.1. 类型别名

26.1.2. NewType

26.1.3. Callable

26.1.4. 泛型(Generic)

26.1.5. 用户定义的泛型类型

26.1.6. Any 类型

26.1.7. 类,函数和修饰器.

26.2. pydoc — 文档生成器和在线帮助系统

26.3. doctest — 测试交互性的Python示例

26.3.1. 简单用法：检查Docstrings中的示例

26.3.2. Simple Usage: Checking Examples in a Text File

26.3.3. How It Works

26.3.3.1. Which Docstrings Are Examined?

26.3.3.2. How are Docstring Examples Recognized?

26.3.3.3. What’s the Execution Context?

26.3.3.4. What About Exceptions?

26.3.3.5. Option Flags

26.3.3.6. Directives

26.3.3.7. 警告

26.3.4. Basic API

26.3.5. Unittest API

26.3.6. Advanced API

26.3.6.1. DocTest 对象

26.3.6.2. Example Objects

26.3.6.3. DocTestFinder 对象

26.3.6.4. DocTestParser 对象

26.3.6.5. DocTestRunner 对象

26.3.6.6. OutputChecker 对象

26.3.7. 调试

26.3.8. Soapbox

26.4. unittest — 单元测试框架

26.4.1. 基本实例

26.4.2. 命令行界面

26.4.2.1. 命令行选项

26.4.3. 探索性测试

26.4.4. 组织你的测试代码

26.4.5. 复用已有的测试代码

26.4.6. 跳过测试与预计的失败

26.4.7. Distinguishing test iterations using subtests

26.4.8. 类与函数

26.4.8.1. 测试用例

26.4.8.1.1. Deprecated aliases

26.4.8.2. Grouping tests

26.4.8.3. Loading and running tests

26.4.8.3.1. load_tests Protocol

26.4.9. Class and Module Fixtures

26.4.9.1. setUpClass and tearDownClass

26.4.9.2. setUpModule and tearDownModule

26.4.10. Signal Handling

26.5. unittest.mock — 模拟对象库

26.5.1. 快速上手

26.5.2. Mock 类

26.5.2.1. Calling

26.5.2.2. Deleting Attributes

26.5.2.3. Mock names and the name attribute

26.5.2.4. Attaching Mocks as Attributes

26.5.3. The patchers

26.5.3.1. patch

26.5.3.2. patch.object

26.5.3.3. patch.dict

26.5.3.4. patch.multiple

26.5.3.5. patch methods: start and stop

26.5.3.6. patch builtins

26.5.3.7. TEST_PREFIX

26.5.3.8. Nesting Patch Decorators

26.5.3.9. Where to patch

26.5.3.10. Patching Descriptors and Proxy Objects

26.5.4. MagicMock and magic method support

26.5.4.1. Mocking Magic Methods

26.5.4.2. Magic Mock

26.5.5. Helpers

26.5.5.1. sentinel

26.5.5.2. DEFAULT

26.5.5.3. call

26.5.5.4. create_autospec

26.5.5.5. ANY

26.5.5.6. FILTER_DIR

26.5.5.7. mock_open

26.5.5.8. Autospeccing

26.6. unittest.mock 上手指南

26.6.1. 使用 mock

26.6.1.1. 模拟方法调用

26.6.1.2. 对象上的方法调用的 mock

26.6.1.3. Mocking Classes

26.6.1.4. Naming your mocks

26.6.1.5. Tracking all Calls

26.6.1.6. Setting Return Values and Attributes

26.6.1.7. Raising exceptions with mocks

26.6.1.8. Side effect functions and iterables

26.6.1.9. Creating a Mock from an Existing Object

26.6.2. Patch Decorators

26.6.3. Further Examples

26.6.3.1. Mocking chained calls

26.6.3.2. Partial mocking

26.6.3.3. Mocking a Generator Method

26.6.3.4. Applying the same patch to every test method

26.6.3.5. Mocking Unbound Methods

26.6.3.6. Checking multiple calls with mock

26.6.3.7. Coping with mutable arguments

26.6.3.8. Nesting Patches

26.6.3.9. Mocking a dictionary with MagicMock

26.6.3.10. Mock subclasses and their attributes

26.6.3.11. Mocking imports with patch.dict

26.6.3.12. Tracking order of calls and less verbose call assertions

26.6.3.13. More complex argument matching

26.7. 2to3 - 自动将 Python 2 代码转为 Python 3 代码

26.7.1. 使用 2to3

26.7.2. 修复器

26.7.3. lib2to3 —— 2to3 支持库

26.8. test — Python回归测试包

26.8.1. Writing Unit Tests for the test package

26.8.2. Running tests using the command-line interface

26.9. test.support — Utilities for the Python test suite

27. 调试和分析

27.1. bdb — Debugger framework

27.2. faulthandler — Dump the Python traceback

27.2.1. Dumping the traceback

27.2.2. Fault handler state

27.2.3. Dumping the tracebacks after a timeout

27.2.4. Dumping the traceback on a user signal

27.2.5. Issue with file descriptors

27.2.6. 示例

27.3. pdb — Python的调试器

27.3.1. 调试器命令

27.4. Python 分析器

27.4.1. 分析器简介

27.4.2. 即时用户手册

27.4.3. profile 和 cProfile 模块参考

27.4.4. Stats 类

27.4.5. 什么是确定性性能分析？

27.4.6. 局限性

27.4.7. 准确估量

27.4.8. 使用自定义计时器

27.5. timeit — 测量小代码片段的执行时间

27.5.1. 基本示例

27.5.2. Python 接口

27.5.3. 命令行界面

27.5.4. 例子

27.6. trace — 跟踪Python语句执行

27.6.1. Command-Line Usage

27.6.1.1. Main options

27.6.1.2. Modifiers

27.6.1.3. Filters

27.6.2. 编程接口

27.7. tracemalloc — 跟踪内存分配

27.7.1. 例子

27.7.1.1. 显示前10项

27.7.1.2. 计算差异

27.7.1.3. Get the traceback of a memory block

27.7.1.4. Pretty top

27.7.2. API

27.7.2.1. 函数

27.7.2.2. 域过滤器

27.7.2.3. 过滤器

27.7.2.4. Frame

27.7.2.5. 快照

27.7.2.6. 统计

27.7.2.7. StatisticDiff

27.7.2.8. 跟踪

27.7.2.9. 回溯

28. 软件打包和分发

28.1. distutils — 构建和安装 Python 模块

28.2. ensurepip — Bootstrapping the pip installer

28.2.1. Command line interface

28.2.2. Module API

28.3. venv — 创建虚拟环境

28.3.1. 创建虚拟环境

28.3.2. API

28.3.3. 一个扩展 EnvBuilder 的例子

28.4. zipapp — Manage executable python zip archives

28.4.1. Basic Example

28.4.2. 命令行界面

28.4.3. Python API

28.4.4. 例子

28.4.5. Specifying the Interpreter

28.4.6. Creating Standalone Applications with zipapp

28.4.6.1. Making a Windows executable

28.4.6.2. Caveats

28.4.7. The Python Zip Application Archive Format

29. Python运行时服务

29.1. sys — 系统相关的参数和函数

29.2. sysconfig — Provide access to Python’s configuration information

29.2.1. 配置变量

29.2.2. 安装路径

29.2.3. 其他功能

29.2.4. Using sysconfig as a script

29.3. builtins — 内建对象

29.4. __main__ — 顶层脚本环境

29.5. warnings — Warning control

29.5.1. 警告类别

29.5.2. The Warnings Filter

29.5.2.1. Default Warning Filters

29.5.3. 暂时禁止警告

29.5.4. 测试警告

29.5.5. Updating Code For New Versions of Python

29.5.6. Available Functions

29.5.7. Available Context Managers

29.6. contextlib — Utilities for with-statement contexts

29.6.1. 工具

29.6.2. 例子和配方

29.6.2.1. Supporting a variable number of context managers

29.6.2.2. Simplifying support for single optional context managers

29.6.2.3. Catching exceptions from __enter__ methods

29.6.2.4. Cleaning up in an __enter__ implementation

29.6.2.5. Replacing any use of try-finally and flag variables

29.6.2.6. Using a context manager as a function decorator

29.6.3. Single use, reusable and reentrant context managers

29.6.3.1. Reentrant context managers

29.6.3.2. Reusable context managers

29.7. abc — 抽象基类

29.8. atexit — 退出处理器

29.8.1. atexit 示例

29.9. traceback — 打印或检索堆栈回溯

29.9.1. TracebackException Objects

29.9.2. StackSummary Objects

29.9.3. FrameSummary Objects

29.9.4. Traceback Examples

29.10. __future__ — Future 语句定义

29.11. gc — 垃圾回收器接口

29.12. inspect — 检查对象

29.12.1. 类型和成员

29.12.2. Retrieving source code

29.12.3. Introspecting callables with the Signature object

29.12.4. 类与函数

29.12.5. The interpreter stack

29.12.6. Fetching attributes statically

29.12.7. Current State of Generators and Coroutines

29.12.8. Code Objects Bit Flags

29.12.9. 命令行界面

29.13. site —— 指定 Site 的配置钩子

29.13.1. Readline configuration

29.13.2. 模块内容

29.14. fpectl — Floating point exception control

29.14.1. Example

29.14.2. Limitations and other considerations

30. 自定义 Python 解释器

30.1. code — 解释器基础类

30.1.1. 交互解释器对象

30.1.2. 交互式控制台对象

30.2. codeop — 编译Python代码

31. 导入模块

31.1. zipimport — 从 Zip 存档中导入模块

31.1.1. zipimporter 对象

31.1.2. 例子

31.2. pkgutil — 包扩展模块工具

31.3. modulefinder — 查找脚本使用的模块

31.3.1. ModuleFinder 的示例用法

31.4. runpy — Locating and executing Python modules

31.5. importlib — The implementation of import

31.5.1. 概述

31.5.2. 函数

31.5.3. importlib.abc —— 关于导入的抽象基类

31.5.4. importlib.machinery – Importers and path hooks

31.5.5. importlib.util – Utility code for importers

31.5.6. 例子

31.5.6.1. Importing programmatically

31.5.6.2. Checking if a module can be imported

31.5.6.3. Importing a source file directly

31.5.6.4. Setting up an importer

31.5.6.5. Approximating importlib.import_module()

32. Python 语言服务

32.1. parser — 访问 Python 解析树

32.1.1. 创建 ST 对象

32.1.2. 转换 ST 对象

32.1.3. Queries on ST Objects

32.1.4. 异常和错误处理

32.1.5. ST 对象

32.1.6. 示例: compile() 的模拟

32.2. ast — 抽象语法树

32.2.1. 节点类

32.2.2. 抽象文法

32.2.3. ast 中的辅助函数

32.3. symtable — Access to the compiler’s symbol tables

32.3.1. Generating Symbol Tables

32.3.2. Examining Symbol Tables

32.4. symbol — 与 Python 解析树一起使用的常量

32.5. token — 与Python解析树一起使用的常量

32.6. keyword — 检验Python关键字

32.7. tokenize – 对 Python 代码使用的标记解析器

32.7.1. 对输入进行解析标记

32.7.2. Command-Line Usage

32.7.3. 例子

32.8. tabnanny — 模糊缩进检测

32.9. pyclbr — Python class browser support

32.9.1. 类对象

32.9.2. 函数对象

32.10. py_compile — 编译 Python 源文件

32.11. compileall — Byte-compile Python libraries

32.11.1. Command-line use

32.11.2. Public functions

32.12. dis — Python 字节码反汇编器

32.12.1. 字节码分析

32.12.2. 分析函数

32.12.3. Python字节码说明

32.12.4. 操作码集合

32.13. pickletools — pickle 开发者工具集

32.13.1. 命令行语法

32.13.1.1. 命令行选项

32.13.2. 编程接口

33. 杂项服务

33.1. formatter — 通用格式化输出

33.1.1. The Formatter Interface

33.1.2. Formatter Implementations

33.1.3. The Writer Interface

33.1.4. Writer Implementations

34. Windows系统相关模块

34.1. msilib — Read and write Microsoft Installer files

34.1.1. Database Objects

34.1.2. View Objects

34.1.3. Summary Information Objects

34.1.4. Record Objects

34.1.5. Errors

34.1.6. CAB Objects

34.1.7. Directory Objects

34.1.8. 相关特性

34.1.9. GUI classes

34.1.10. Precomputed tables

34.2. msvcrt — 来自 MS VC++ 运行时的有用例程

34.2.1. 文件操作

34.2.2. 控制台 I/O

34.2.3. 其他函数

34.3. winreg — Windows 注册表访问

34.3.1. 函数

34.3.2. 常量

34.3.2.1. HKEY_* Constants

34.3.2.2. Access Rights

34.3.2.2.1. 64-bit Specific

34.3.2.3. Value Types

34.3.3. Registry Handle Objects

34.4. winsound — Sound-playing interface for Windows

35. Unix 专有服务

35.1. posix — 最常见的 POSIX 系统调用

35.1.1. 大文件支持

35.1.2. 重要的模块内容

35.2. pwd — 用户密码数据库

35.3. spwd — The shadow password database

35.4. grp — 组数据库

35.5. crypt — Function to check Unix passwords

35.5.1. Hashing Methods

35.5.2. Module Attributes

35.5.3. 模块函数

35.5.4. 例子

35.6. termios — POSIX 风格的 tty 控制

35.6.1. 示例

35.7. tty — 终端控制功能

35.8. pty — 伪终端工具

35.8.1. 示例

35.9. fcntl — The fcntl and ioctl system calls

35.10. pipes — 终端管道接口

35.10.1. 模板对象

35.11. resource — Resource usage information

35.11.1. Resource Limits

35.11.2. Resource Usage

35.12. nis — Sun 的 NIS (黄页) 接口

35.13. Unix syslog 库例程

35.13.1. 例子

35.13.1.1. 简单示例

36. 被取代的模块

36.1. optparse — 解析器的命令行选项

36.1.1. 背景

36.1.1.1. 术语

36.1.1.2. What are options for?

36.1.1.3. 位置位置

36.1.2. 教程

36.1.2.1. Understanding option actions

36.1.2.2. The store action

36.1.2.3. Handling boolean (flag) options

36.1.2.4. Other actions

36.1.2.5. 默认值

36.1.2.6. Generating help

36.1.2.6.1. Grouping Options

36.1.2.7. Printing a version string

36.1.2.8. How optparse handles errors

36.1.2.9. Putting it all together

36.1.3. 参考指南

36.1.3.1. 创建解析器

36.1.3.2. 填充解析器

36.1.3.3. 定义选项

36.1.3.4. Option attributes

36.1.3.5. Standard option actions

36.1.3.6. Standard option types

36.1.3.7. 解析参数

36.1.3.8. Querying and manipulating your option parser

36.1.3.9. Conflicts between options

36.1.3.10. 清理

36.1.3.11. Other methods

36.1.4. Option Callbacks

36.1.4.1. Defining a callback option

36.1.4.2. How callbacks are called

36.1.4.3. Raising errors in a callback

36.1.4.4. Callback example 1: trivial callback

36.1.4.5. Callback example 2: check option order

36.1.4.6. Callback example 3: check option order (generalized)

36.1.4.7. Callback example 4: check arbitrary condition

36.1.4.8. Callback example 5: fixed arguments

36.1.4.9. Callback example 6: variable arguments

36.1.5. Extending optparse

36.1.5.1. Adding new types

36.1.5.2. Adding new actions

36.2. imp — Access the import internals

36.2.1. 例子

37. 未创建文档的模块

37.1. 平台特定模块

扩展和嵌入 Python 解释器

推荐的第三方工具

不使用第三方工具创建扩展

1. 使用 C 或 C++ 扩展 Python

1.1. 一个简单的例子

1.2. 关于错误和异常

1.3. 回到例子

1.4. 模块方法表和初始化函数

1.5. 编译和链接

1.6. 在C中调用Python函数

1.7. 提取扩展函数的参数

1.8. 给扩展函数的关键字参数

1.9. 构造任意值

1.10. 引用计数

1.10.1. Python中的引用计数

1.10.2. 拥有规则

1.10.3. 危险的薄冰

1.10.4. NULL指针

1.11. 在C++中编写扩展

1.12. 给扩展模块提供C API

2. 自定义扩展类型：教程

2.1. 基础

2.2. Adding data and methods to the Basic example

2.3. Providing finer control over data attributes

2.4. Supporting cyclic garbage collection

2.5. Subclassing other types

3. 定义扩展类型：已分类主题

3.1. 终结和内存释放

3.2. 对象展示

3.3. 属性管理

3.3.1. 泛型属性管理

3.3.2. Type-specific Attribute Management

3.4. Object Comparison

3.5. Abstract Protocol Support

3.6. Weak Reference Support

3.7. 更多建议

4. 构建C/C++扩展

4.1. 使用distutils构建C和C++扩展

4.2. 发布你的扩展模块

5. 在Windows平台编译C和C++扩展

5.1. 菜谱式说明

5.2. Unix 和 Windows 之间的差异

5.3. DLL 的实际使用

在更大的应用程序中嵌入 CPython 运行时

1. 在其它应用程序嵌入 Python

1.1. Very High Level Embedding

1.2. Beyond Very High Level Embedding: An overview

1.3. 纯嵌入

1.4. Extending Embedded Python

1.5. 在 C++ 中嵌入 Python

1.6. 在类 Unix 系统中编译和链接

Python/C API 参考手册

概述

包含文件

对象、类型和引用计数

引用计数

Reference Count Details

类型

异常

嵌入Python

调试构建

稳定的应用程序二进制接口

The Very High Level Layer

引用计数

异常处理

Printing and clearing

抛出异常

警告

Querying the error indicator

Signal Handling

Exception Classes

异常对象

Unicode Exception Objects

递归控制

标准异常

标准警告类别

工具

操作系统实用程序

系统功能

过程控制

导入模块

数据 marshal 操作支持

解析参数并构建值变量

解析参数

字符串和缓存区

数字

其他对象

API 函数

创建变量

字符串转换与格式化

反射

编解码器注册与支持功能

Codec 查找API

用于Unicode编码错误处理程序的注册表API

抽象对象层

对象协议

数字协议

序列协议

映射协议

迭代器协议

缓冲协议

缓冲区结构

Buffer request types

request-independent fields

只读，格式

形状，步幅，子偏移量

连续性的请求

复合请求

复杂数组

NumPy-风格：形状和步幅

PIL-风格：形状，步幅和子偏移量

缓冲区相关函数

旧缓冲协议

具体的对象层

基本对象

类型对象

None 对象

数值对象

整数型对象

布尔对象

浮点数对象

复数对象

表示复数的C结构体

表示复数的Python对象

序列对象

bytes 对象

字节数组对象

类型检查宏

直接 API 函数

宏

Unicode对象和编码解码器

Unicode对象

Unicode类型

Unicode字符属性

Creating and accessing Unicode strings

Deprecated Py_UNICODE APIs

Locale Encoding

File System Encoding

wchar_t Support

Built-in Codecs

Generic Codecs

UTF-8 Codecs

UTF-32 Codecs

UTF-16 Codecs

UTF-7 Codecs

Unicode-Escape Codecs

Raw-Unicode-Escape Codecs

Latin-1 Codecs

ASCII Codecs

Character Map Codecs

MBCS codecs for Windows

Methods & Slots

Methods and Slot Functions

元组对象

结构序列对象

列表对象

容器对象

字典对象

集合对象

函数对象

函数对象

实例方法对象

方法对象

Cell 对象

代码对象

其他对象

文件对象

模块对象

Initializing C modules

Single-phase initialization

Multi-phase initialization

Low-level module creation functions

Support functions

Module lookup

迭代器对象

描述符对象

切片对象

Ellipsis 对象

MemoryView 对象

弱引用对象

胶囊

生成器对象

协程对象

DateTime 对象

Initialization, Finalization, and Threads

Initializing and finalizing the interpreter

Process-wide parameters

Thread State and the Global Interpreter Lock

Releasing the GIL from extension code

非Python创建的线程

高阶 API

Low-level API

Sub-interpreter support

错误和警告

异步通知

分析和跟踪

高级调试器支持

内存管理

概述

原始内存接口

内存接口

对象分配器

自定义内存分配器

pymalloc 分配器

自定义 pymalloc Arena 分配器

例子

对象实现支持

在堆上分配对象

Common Object Structures

类型对象

Number Object Structures

Mapping Object Structures

Sequence Object Structures

Buffer Object Structures

Async Object Structures

使对象类型支持循环垃圾回收

API 和 ABI 版本管理

分发 Python 模块

关键术语

开源许可与协作

安装工具

Reading the guide

我该如何…？

…为我的项目选择一个名字？

…创建和分发二进制扩展？

安装 Python 模块

关键术语

基本使用

我应如何 …？

… 在 Python 3.4 之前的 Python 版本中安装 pip ？

… 只为当前用户安装软件包？

… 安装科学计算类 Python 软件包？

… 使用并行安装的多个 Python 版本？

常见的安装问题

在 Linux 的系统 Python 版本上安装

未安装 pip

安装二进制编译扩展

Python 常用指引

将 Python 2 代码迁移到 Python 3

简要说明

详情

删除对Python 2.6及更早版本的支持

Make sure you specify the proper version support in your setup.py file

良好的测试覆盖率

了解Python 2 和 3之间的区别

更新代码

除法

文本与二进制数据

Use feature detection instead of version detection

Prevent compatibility regressions

Check which dependencies block your transition

Update your setup.py file to denote Python 3 compatibility

Use continuous integration to stay compatible

考虑使用可选的静态类型检查

将扩展模块移植到 Python 3

条件编译

对象API的更改

str/unicode 统一

long/int 统一

模块初始化和状态

CObject 替换为 Capsule

其他选项

用 Python 进行 Curses 编程

curses 是什么？

Python 的 curses 模块

开始和结束curses应用程序

Windows 和 Pad

显示文字

属性和颜色

用户输入

更多的信息

实现描述器

摘要

定义和简介

描述器协议

发起调用描述器

描述符示例

属性

函数和方法

静态方法和类方法

函数式编程指引

概述

形式证明

模块化

易于调试和测试

组合性

迭代器

支持迭代器的数据类型

生成器表达式和列表推导式

生成器

向生成器传递值

内置函数

itertools 模块

创建新的迭代器

对元素使用函数

选择元素

组合函数

为元素分组

functools 模块

operator 模块

小函数和 lambda 表达式

修订记录和致谢

参考文献

通用文献

Python 相关

Python 文档

日志 HOWTO

日志基础教程

什么时候使用 Logging

一个简单的例子

记录日志到文件

从多个模块记录日志

记录变量数据

更改显示消息的格式

在消息中显示日期/时间

后续步骤

进阶日志教程

记录流程

记录器

处理程序

格式化程序

配置日志记录

如果没有提供配置会发生什么

配置库的日志记录

日志级别

自定义级别

有用的处理程序

记录日志中引发的异常

使用任意对象作为消息

优化

日志操作手册

在多个模块中记录日志

在多个线程中记录日志

多个日志处理器以及多种格式化器

在多个地方记录日志

日志服务器配置示例

处理日志处理器的阻塞

通过网络发送和接收日志

在日志记录中添加上下文信息

使用日志适配器传递上下文信息

使用除字典之外的其它对象传递上下文信息

使用过滤器传递上下文信息

从多个进程记录至单个文件

轮换日志文件

使用其他日志格式化方式

Customizing LogRecord

Subclassing QueueHandler - a ZeroMQ example

Subclassing QueueListener - a ZeroMQ example

An example dictionary-based configuration

Using a rotator and namer to customize log rotation processing

A more elaborate multiprocessing example

Inserting a BOM into messages sent to a SysLogHandler

Implementing structured logging

Customizing handlers with dictConfig()

Using particular formatting styles throughout your application

Using LogRecord factories

Using custom message objects

Configuring filters with dictConfig()

Customized exception formatting

Speaking logging messages

缓冲日志消息并有条件地输出它们

通过配置使用UTC (GMT) 格式化时间

使用上下文管理器的可选的日志记录

正则表达式HOWTO

概述

简单模式

匹配字符

重复

使用正则表达式

编译正则表达式

反斜杠灾难

应用匹配

模块级别函数

编译标志

更多模式能力

更多元字符

分组

非捕获和命名组

前向断言

修改字符串

分割字符串

搜索和替换

常见问题

使用字符串方法

match() 和 search()

贪婪与非贪婪

使用 re.VERBOSE

反馈

套接字编程指南

套接字

历史

创建套接字

进程间通信

使用一个套接字

二进制数据

断开连接

套接字何时销毁

非阻塞的套接字

排序指南

基本排序

关键函数

Operator 模块函数

升序和降序

排序稳定性和排序复杂度

使用装饰-排序-去装饰的旧方法

使用 cmp 参数的旧方法

其它

Unicode 指南

Unicode 概述

History of Character Codes

定义

编码

参考文献

Python’s Unicode Support

The String Type

Converting to Bytes

Unicode Literals in Python Source Code

Unicode Properties

Unicode Regular Expressions

参考文献

Reading and Writing Unicode Data

Unicode filenames

Tips for Writing Unicode-aware Programs

Converting Between File Encodings

Files in an Unknown Encoding

参考文献

致谢

HOWTO 使用 urllib 包获取网络资源

概述

提取URL

数据

Headers

处理异常

URLError

HTTPError

错误代码

包装起来

数字1

Number 2

info and geturl

Openers and Handlers

基本认证

代理

Sockets and Layers

备注

Argparse 教程

概念

基础

位置参数介绍

可选参数介绍

短选项

结合位置参数和可选参数

进行一些小小的改进

矛盾的选项

后记

ipaddress模块介绍

创建 Address/Network/Interface 对象

关于IP版本的说明

IP主机地址

定义网络

主机接口

审查 Address/Network/Interface 对象

Network 作为 Address 列表

比较运算

将IP地址与其他模块一起使用

实例创建失败时获取更多详细信息

Argument Clinic How-To

The Goals Of Argument Clinic

Basic Concepts And Usage

Converting Your First Function

Advanced Topics

Symbolic default values

Renaming the C functions and variables generated by Argument Clinic

Converting functions using PyArg_UnpackTuple

Optional Groups

Using real Argument Clinic converters, instead of “legacy converters”

Py_buffer

Advanced converters

Parameter default values

The NULL default value

Expressions specified as default values

Using a return converter

Cloning existing functions

Calling Python code

Using a “self converter”

Writing a custom converter

Writing a custom return converter

METH_O and METH_NOARGS

tp_new and tp_init functions

Changing and redirecting Clinic’s output

The #ifdef trick

Using Argument Clinic in Python files

使用 DTrace 和 SystemTap 检测CPython

启用静态标记

静态DTrace探针

静态SystemTap标记

可用的静态标记

SystemTap Tapsets

例子

Python 常见问题

Python常见问题

一般信息

现实世界中的 Python

编程常见问题

一般问题

核心语言

数字和字符串

性能

序列（元组/列表）

Dictionaries

对象

模块

设计和历史常见问题

为什么Python使用缩进来分组语句？

为什么简单的算术运算得到奇怪的结果？

为什么浮点计算不准确？

为什么Python字符串是不可变的？

为什么必须在方法定义和调用中显式使用“self”？

为什么不能在表达式中赋值？

为什么Python对某些功能（例如list.index()）使用方法来实现，而其他功能（例如len(List)）使用函数实现？

为什么 join()是一个字符串方法而不是列表或元组方法？

异常有多快？

为什么Python中没有switch或case语句？

难道不能在解释器中模拟线程，而非得依赖特定于操作系统的线程实现吗？

为什么lambda表达式不能包含语句？

可以将Python编译为机器代码，C或其他语言吗？

Python如何管理内存？

为什么CPython不使用更传统的垃圾回收方案？

CPython退出时为什么不释放所有内存？

为什么有单独的元组和列表数据类型？

列表是如何在CPython中实现的？

字典是如何在CPython中实现的？

为什么字典key必须是不可变的？

为什么 list.sort() 没有返回排序列表？

如何在Python中指定和实施接口规范？

为什么没有goto？

为什么原始字符串（r-strings）不能以反斜杠结尾？

为什么Python没有属性赋值的“with”语句？

为什么 if/while/def/class语句需要冒号？

为什么Python在列表和元组的末尾允许使用逗号？

代码库和插件 FAQ

通用的代码库问题

通用任务

线程相关

输入输出

网络 / Internet 编程

数据库

数学和数字

扩展/嵌入常见问题

可以使用C语言中创建自己的函数吗？

可以使用C++语言中创建自己的函数吗？

C很难写，有没有其他选择？

如何在 C 中执行任意 Python 语句？

如何在 C 中对任意 Python 表达式求值？

如何从Python对象中提取C的值？

如何使用Py_BuildValue()创建任意长度的元组？

如何从C调用对象的方法？

如何捕获PyErr_Print()（或打印到stdout / stderr的任何内容）的输出？

如何从C访问用Python编写的模块？

如何在 Python 中对接 C ++ 对象？

我使用Setup文件添加了一个模块，为什么make失败了？

如何调试扩展？

我想在Linux系统上编译一个Python模块，但是缺少一些文件。为什么?

如何区分“输入不完整”和“输入无效”？

如何找到未定义的g++符号__builtin_new或__pure_virtual？

能否创建一个对象类，其中部分方法在C中实现，而其他方法在Python中实现（例如通过继承）？

Python在Windows上的常见问题

我怎样在Windows下运行一个Python程序？

我怎么让 Python 脚本可执行？

为什么有时候 Python 程序会启动缓慢？

我怎样使用 Python 脚本制作可执行文件？

*.pyd 文件和 DLL 文件相同吗？

我怎样将 Python 嵌入一个 Windows 程序？

如何让编辑器不要在我的 Python 源代码中插入 tab ？

如何在不阻塞的情况下检查按键？

图形用户界面（GUI）常见问题

图形界面常见问题

Python 是否有平台无关的图形界面工具包？

有哪些Python的GUI工具是某个平台专用的？

有关Tkinter的问题

“为什么我的电脑上安装了 Python ？”

什么是 Python？

为什么我的电脑上安装了 Python ？

我能删除 Python 吗？

术语对照表

文档说明

Python 文档的贡献者

解决 Bug

文档 Bug

使用 Python 的问题追踪系统

开始为 Python 贡献您的知识

Copyright

历史和许可证

该软件的历史

获取或以其他方式使用 Python 的条款和条件

用于 PYTHON 3.6.13 的 PSF 许可协议

用于 PYTHON 2.0 的 BEOPEN.COM 许可协议

用于 PYTHON 1.6.1 的 CNRI 许可协议

用于 PYTHON 0.9.0 至 1.2 的 CWI 许可协议

被收录软件的许可证与鸣谢

Mersenne Twister

套接字

Floating point exception control

异步套接字服务

Cookie 管理

执行追踪

UUencode 与 UUdecode 函数

XML 远程过程调用

test_epoll

Select kqueue

SipHash24

strtod and dtoa

OpenSSL

expat

libffi

zlib

cfuhash

libmpdec

导航

索引

模块 |

下一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

Python的新变化

这个“Python 有什么新变化？”系列内容会带您浏览 Python 大版本之间重要的变化。在新版发布后，如果您希望掌握最新变化，请务必阅读这些内容。

Python 3.6 有什么新变化A

摘要 - 发布重点

新的特性

其他语言特性修改

新增模块

改进的模块

性能优化

构建和 C API 的改变

其他改进

弃用

移除

移植到Python 3.6

Python 3.6.2 中的重要变化

Python 3.6.4 中的重要变化

Python 3.6.5 中的重要变化

Python 3.6.7 中的重要变化

Python 3.6.10 中的重要变化

Notable changes in Python 3.6.13

Python 3.5 有什么新变化

摘要 - 发布重点

新的特性

其他语言特性修改

新增模块

改进的模块

其他模块级更改

性能优化

构建和 C API 的改变

弃用

移除

移植到Python 3.5

Python 3.5.4 的显著变化

Python 3.4 有什么新变化

摘要 - 发布重点

新的特性

新增模块

改进的模块

CPython Implementation Changes

弃用

移除

移植到 Python 3.4

3.4.3 的变化

Python 3.3 有什么新变化

摘要 - 发布重点

PEP 405: 虚拟环境

PEP 420: 隐式命名空间包

PEP 3118: 新的内存视图实现和缓冲协议文档

PEP 393: 灵活的字符串表示

PEP 397: 适用于Windows的Python启动器

PEP 3151: 重写 OS 和 IO 异常的层次结构

PEP 380: 委托给子生成器的语法

PEP 409: 清除异常上下文

PEP 414: 显式的Unicode文本

PEP 3155: 类和函数的限定名称

PEP 412: Key-Sharing Dictionary

PEP 362: 函数签名对象

PEP 421: 添加 sys.implementation

Using importlib as the Implementation of Import

其他语言特性修改

A Finer-Grained Import Lock

Builtin functions and types

新增模块

改进的模块

性能优化

构建和 C API 的改变

弃用

移植到 Python 3.3

Python 3.2 有什么新变化

PEP 384: 定义稳定的ABI

PEP 389: Argparse 命令行解析模块

PEP 391: 基于字典的日志配置

PEP 3148: concurrent.futures 模块

PEP 3147: PYC 仓库目录

PEP 3149: ABI Version Tagged .so Files

PEP 3333: Python Web服务器网关接口v1.0.1

其他语言特性修改

新增，改进和弃用的模块

多线程

性能优化

Unicode

编解码器

文档

IDLE

代码库

构建和 C API 的改变

移植到 Python 3.2

Python 3.1 有什么新变化

PEP 372: 有序字典

PEP 378: 千位分隔符的格式说明符

其他语言特性修改

新增，改进和弃用的模块

性能优化

IDLE

构建和 C API 的改变

移植到 Python 3.1

Python 3.0 有什么新变化

常见的绊脚石

Overview Of Syntax Changes

Changes Already Present In Python 2.6

Library Changes

PEP 3101: A New Approach To String Formatting

Changes To Exceptions

Miscellaneous Other Changes

构建和 C API 的改变

性能

移植 Python 3.0

Python 2.7 有什么新变化

Python 2.x的未来

Changes to the Handling of Deprecation Warnings

Python 3.1 Features

PEP 372: Adding an Ordered Dictionary to collections

PEP 378: 千位分隔符的格式说明符

PEP 389: The argparse Module for Parsing Command Lines

PEP 391: Dictionary-Based Configuration For Logging

PEP 3106: Dictionary Views

PEP 3137: The memoryview Object

其他语言特性修改

新增和改进的模块

构建和 C API 的改变

Other Changes and Fixes

移植到 Python 2.7

New Features Added to Python 2.7 Maintenance Releases

致谢

Python 2.6 有什么新变化

Python 3.0

开发过程的变化

PEP 343: “with” 语句

PEP 366: 从主模块显式相对导入

PEP 370: 分用户的 site-packages 目录

PEP 371: 多任务处理包

PEP 3101: 高级字符串格式

PEP 3105: print 改为函数

PEP 3110: 异常处理的变更

PEP 3112: 字节字面值

PEP 3116: 新 I/O 库

PEP 3118: 修改缓冲区协议

PEP 3119: 抽象基类

PEP 3127: 整型文字支持和语法

PEP 3129: 类装饰器

PEP 3141: A Type Hierarchy for Numbers

其他语言特性修改

新增和改进的模块

Deprecations and Removals

构建和 C API 的改变

移植到Python 2.6

致谢

Python 2.5 有什么新变化

PEP 308: 条件表达式

PEP 309: 部分功能应用

PEP 314: Python软件包的元数据 v1.1

PEP 328: 绝对导入和相对导入

PEP 338: 将模块作为脚本执行

PEP 341: 统一 try/except/finally

PEP 342: 生成器的新特性

PEP 343: “with” 语句

PEP 352: 异常作为新型的类

PEP 353: 使用ssize_t作为索引类型

PEP 357: ‘__index__’ 方法

其他语言特性修改

新增，改进和删除的模块

构建和 C API 的改变

移植到Python 2.5

致谢

Python 2.4 有什么新变化

PEP 218: 内置集合对象

PEP 237: 统一长整数和整数

PEP 289: 生成器表达式

PEP 292: Simpler String Substitutions

PEP 318: Decorators for Functions and Methods

PEP 322: 反向迭代

PEP 324: 新的子进程模块

PEP 327: 十进数据类型

PEP 328: 多行导入

PEP 331: Locale-Independent Float/String Conversions

其他语言特性修改

新增，改进和弃用的模块

构建和 C API 的改变

移植到 Python 2.4

致谢

Python 2.3 有什么新变化

PEP 218: A Standard Set Datatype

PEP 255: Simple Generators

PEP 263: Source Code Encodings

PEP 273: 从ZIP压缩包导入模块

PEP 277: Unicode file name support for Windows NT

PEP 278: 通用换行支持

PEP 279: enumerate()

PEP 282: logging 包

PEP 285: 布尔类型

PEP 293: Codec Error Handling Callbacks

PEP 301: Distutils的软件包索引和元数据

PEP 302: New Import Hooks

PEP 305: 逗号分隔文件

PEP 307: Pickle Enhancements

扩展切片

其他语言特性修改

新增，改进和弃用的模块

Pymalloc: A Specialized Object Allocator

构建和 C API 的改变

Other Changes and Fixes

移植到 Python 2.3

致谢

Python 2.2 有什么新变化

概述

PEPs 252 and 253: Type and Class Changes

PEP 234: Iterators

PEP 255: Simple Generators

PEP 237: 统一长整数和整数

PEP 238: Changing the Division Operator

Unicode Changes

PEP 227: Nested Scopes

新增和改进的模块

Interpreter Changes and Fixes

Other Changes and Fixes

致谢

Python 2.1 有什么新变化

概述

PEP 227: Nested Scopes

PEP 236: __future__ Directives

PEP 207: Rich Comparisons

PEP 230: Warning Framework

PEP 229: New Build System

PEP 205: Weak References

PEP 232: Function Attributes

PEP 235: Importing Modules on Case-Insensitive Platforms

PEP 217: Interactive Display Hook

PEP 208: New Coercion Model

PEP 241: Metadata in Python Packages

新增和改进的模块

Other Changes and Fixes

致谢

Python 2.0 有什么新变化

概述

What About Python 1.6?

新开发流程

Unicode

列表推导式

Augmented Assignment

字符串的方法

Garbage Collection of Cycles

其他核心变化

移植 Python 2.0

扩展/嵌入更改

Distutils：使模块易于安装

XML 模块

模块更改

新增模块

IDLE 改进

删除和弃用的模块

致谢

这个“更新日志”是 Misc/NEWS.d [https://github.com/python/cpython/tree/3.6/Misc/NEWS.d] 目录下 构建文件 [https://pypi.org/project/blurb] 的 HTML 版本，它包含了对当前 Python 版本进行的 所有 重要的更改。

更新日志

Python 3.6.13 final

Python 3.6.12 final

Python 3.6.11 final

Python 3.6.11 release candidate 1

Python 3.6.10 final

Python 3.6.10 release candidate 1

Python 3.6.9 final

Python 3.6.9 release candidate 1

Python 3.6.8 final

Python 3.6.8 release candidate 1

Python 3.6.7 final

Python 3.6.7 release candidate 2

Python 3.6.7 release candidate 1

Python 3.6.6 正式版

Python 3.6.6 RC 1

Python 3.6.5 正式版

Python 3.6.5 rc1

Python 3.6.4 正式版

Python 3.6.4 rc1

Python 3.6.3 正式版

Python 3.6.3 rc1

Python 3.6.2 正式版

Python 3.6.2 rc2

Python 3.6.2 rc1

Python 3.6.1 正式版

Python 3.6.1 rc1

Python 3.6.0 正式版

Python 3.6.0 rc2

Python 3.6.0 rc1

Python 3.6.0 beta 4

Python 3.6.0 beta 3

Python 3.6.0 beta 2

Python 3.6.0 beta 1

Python 3.6.0 alpha 4

Python 3.6.0 alpha 3

Python 3.6.0 alpha 2

Python 3.6.0 alpha 1

Python 3.5.3 正式版

Python 3.5.3 rc1

Python 3.5.2 正式版

Python 3.5.2 rc1

Python 3.5.1 正式版

Python 3.5.1 rc1

Python 3.5.0 正式版

Python 3.5.0 rc4

Python 3.5.0 rc3

Python 3.5.0 rc2

Python 3.5.0 rc1

Python 3.5.0 beta 4

Python 3.5.0 beta 3

Python 3.5.0 beta 2

Python 3.5.0 beta 1

Python 3.5.0 alpha 4

Python 3.5.0 alpha 3

Python 3.5.0 alpha 2

Python 3.5.0 alpha 1

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

Python 3.6 有什么新变化A

作者

Elvis Pranskevichus <elvis@magic.io>, Yury Selivanov <yury@magic.io>

This article explains the new features in Python 3.6, compared to 3.5. Python 3.6 was released on December 23, 2016. For full details, see the changelog.

参见

PEP 494 [https://www.python.org/dev/peps/pep-0494] - Python 3.6发布计划

摘要 - 发布重点

新的语法特性：

PEP 498, 格式化的字符串文字

PEP 515, 数字文字中的下划线。

PEP 526 , 变量注释的语法。

PEP 525, 异步生成器。

PEP 530: 异步推导。

新的库模块：

secrets: PEP 506 – Secrets模块被加入Python标准库。

CPython 实现的改进：

根据 Raymond Hettinger 的提议 [https://mail.python.org/pipermail/python-dev/2012-December/123028.html] 已将 dict 类型重新实现为使用 更紧凑的表示形式 并类似于 PyPy dict implementation [https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html]。 这使得字典所使用的内存相对于 Python 3.5 版减少了 20% 到 25%。

类创建的定制过程通过 新协议 得到了简化。

类属性的定义顺序 现在会被保留。

现在 **kwargs 中的元素会与传给函数的关键字参数 保持对应顺序。

添加了 DTrace 和 SystemTap 探测支持

新的 PYTHONMALLOC 环境变量可被用来调试解释器的内存分配和访问错误。

标准库中的重大改进：

asyncio 模块获得了许多新特性，显著的可用性和性能提升，以及大量的问题修正。 从 Python 3.6 开始 asyncio 模块不再处于待定状态，它已被视为稳定 API。

实现了新的 文件系统路径协议 以支持 路径类对象。 所有操作路径的标准库函数已被更新为使用新的协议。

datetime 模块已获得对 本地时间消歧义 的支持。

typing 模块获得了大量的 改进。

tracemalloc 模块已被大幅重写，现在将被用于为 ResourceWarning 提供更好的输出，并为内存分配错误提供更好的诊断。 请参阅 PYTHONMALLOC 一节 来了解详情。

安全改进：

添加了 secrets 模块以简化适用于密码管理的高加密强度伪随机数的生成，例如账户验证、安全凭据等场景。

在 Linux 上，现在 os.urandom() 会阻塞直到系统的 urandom 熵池被初始化以提升安全性。 其理由参见 PEP 524 [https://www.python.org/dev/peps/pep-0524]。

hashlib 和 ssl 模块现在支持 OpenSSL 1.1.0。

ssl 模块的默认设置和特性集已得到改进。

hashlib 模块获得了对 BLAKE2, SHA-3 和 SHAKE 哈希算法以及 scrypt() 密钥派生函数的支持。

Windows改进：

PEP 528 和 PEP 529, 将Windows文件系统和控制台的编码更改为UTF-8

在交互式地使用 py.exe 启动器时，当用户未（通过命令行参数或配置文件）指定版本时不再优先选择 Python 2 而是选择 Python 3。 对声明行的处理则保持不变 —— 在这种情况下 “python” 还是指 Python 2。

python.exe 和 pythonw.exe 已被标记为支持长路径，这意味着不再有 260 个字符的路径长度限制。 详情参见 移除 MAX_PATH 限制。

可以添加一个 ._pth 文件来强制使用隔离模式和完整指定所有搜索路径来避免注册表和环境查找。 更多信息请参阅 相关文档。

现在 python36.zip 文件可以作为推断 PYTHONHOME 的标志。 更多信息请参阅 相关文档。

新的特性

PEP 498: 格式化的字符串文字

PEP 498 [https://www.python.org/dev/peps/pep-0498] 引入了一种新型的字符串字面值: f-字符串，或称 格式化字符串字面值。

格式化字符串字面值带有 'f' 前缀并且类似于 str.format() 所接受的格式字符串。 其中包含由花括号包围的替换字段。 替换字段属于表达式，它们会在运行时被求值，然后使用 format() 协议进行格式化:

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'
>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal("12.34567")
>>> f"result: {value:{width}.{precision}}"  # nested fields
'result:      12.35'

参见

PEP 498 [https://www.python.org/dev/peps/pep-0498] – 文字字符串插值。

PEP 由 Eric V. Smith 撰写并实现

Feature documentation.

PEP 526: 变量注释的语法

PEP 484 [https://www.python.org/dev/peps/pep-0484] 引入了函数形参类型标注即类型提示的标准。 这个 PEP 为 Python 添加了标注变量类型的语法，包括类变量和实例变量:

primes: List[int] = []

captain: str  # Note: no initial value!

class Starship:
    stats: Dict[str, int] = {}

与函数标注一样，Python 解释器不会为变量标注附加任何特殊含义，仅会将其保存在类或模块的 __annotations__ 属性中。

与静态类型语法的变量声明不同，标注语法的目的是通过抽象语法树和 __annotations__ 属性提供一个简单方式来为第三方工具和库指定结构化类型元数据。

参见

PEP 526 [https://www.python.org/dev/peps/pep-0526] – 变量注释的语法。

PEP 由 Ryan Gonzalez, Philip House, Ivan Levkivskyi, Lisa Roach, 和 Guido van Rossum 撰写，由 Ivan Levkivskyi 实现。

使用或将要使用此新语法的工具有: mypy [http://www.mypy-lang.org/], pytype [https://github.com/google/pytype], PyCharm 等等。

PEP 515: 数字文字中的下划线。

PEP 515 [https://www.python.org/dev/peps/pep-0515] 增加了在数字字面值中使用下划线的能力以改善可读性。 例如:

>>> 1_000_000_000_000_000
1000000000000000
>>> 0x_FF_FF_FF_FF
4294967295

单个下划线允许用在数码之间和任何数制指示符之后。 一行内不允许有开头、末尾或多个下划线。

字符串格式化 微语言现在也支持以 '_' 选项来表示用下划线作为浮点表示类型和整数表示类型 'd' 的千位分隔符。 对于整数表示类型 'b', 'o', 'x' 和 'X'，将每隔 4 个数码插入一个下划线:

>>> '{:_}'.format(1000000)
'1_000_000'
>>> '{:_x}'.format(0xFFFFFFFF)
'ffff_ffff'

参见

PEP 515 [https://www.python.org/dev/peps/pep-0515] – 数字文字中的下划线。

PEP 由 Georg Brandl 和 Serhiy Storchaka 撰写

PEP 525: 异步生成器

PEP 492 [https://www.python.org/dev/peps/pep-0492] 将对原生协程和 async / await 语法的支持引入到 Python 3.5 中。 但 Python 3.5 实现的一个明显限制是不可能在同一函数体中同时使用 await 和 yield。 在 Python 3.6 中此限制已被解除，这样就就能够定义 异步生成器:

async def ticker(delay, to):
    """Yield numbers from 0 to *to* every *delay* seconds."""
    for i in range(to):
        yield i
        await asyncio.sleep(delay)

这个新语法允许更快速且更精简的代码。

参见

PEP 525 [https://www.python.org/dev/peps/pep-0525] – 异步生成器

PEP 由 Yury Selivanov 撰写并实现

PEP 530: 异步推导

PEP 530 [https://www.python.org/dev/peps/pep-0530] 添加了对在列表、集合与字典推导式和生成器表达式中使用 async for 的支持:

result = [i async for i in aiter() if i % 2]

此外，await 表达式也在所有种类的推导式中得到支持:

result = [await fun() for fun in funcs if await condition()]

参见

PEP 530 [https://www.python.org/dev/peps/pep-0530] – 异步推导

PEP 由 Yury Selivanov 撰写并实现

PEP 487: 自定义类创建

现在可以在不使用元类的情况下自定义子类的创建。 当一个新的子类被创建时将在基类上调用新的 __init_subclass__ 类方法:

class PluginBase:
    subclasses = []

    def __init_subclass__(cls, **kwargs):
        super().__init_subclass__(**kwargs)
        cls.subclasses.append(cls)

class Plugin1(PluginBase):
    pass

class Plugin2(PluginBase):
    pass

为了让来自 __init_subclass__() 实现的零参数 super() 调用能正确工作，自定义元类必须保证新的 __classcell__ 命名空间入口被传播到 type.__new__ (如 创建类对象 中所描述)。

参见

PEP 487 [https://www.python.org/dev/peps/pep-0487] – 自定义类创建

PEP 由 Martin Teichmann 撰写并实现。

特性文档

PEP 487: 描述器协议的增强

PEP 487 [https://www.python.org/dev/peps/pep-0487] 扩展了描述器协议以包括新的可选方法 __set_name__()。 当创建一个新类时，这个新方法将在定义中包括的所有描述器上被调用，为它们提供对所定义类的引用以及在类命名中间中给予描述器的名称。 换句话说，描述器的实例现在能知道描述器在所有者类中的属性名称:

class IntField:
    def __get__(self, instance, owner):
        return instance.__dict__[self.name]

    def __set__(self, instance, value):
        if not isinstance(value, int):
            raise ValueError(f'expecting integer in {self.name}')
        instance.__dict__[self.name] = value

    # this is the new initializer:
    def __set_name__(self, owner, name):
        self.name = name

class Model:
    int_field = IntField()

参见

PEP 487 [https://www.python.org/dev/peps/pep-0487] – 自定义类创建

PEP 由 Martin Teichmann 撰写并实现。

特性文档

PEP 519: 添加文件系统路径协议

File system paths have historically been represented as str or bytes objects. This has led to people who write code which operate on file system paths to assume that such objects are only one of those two types (an int representing a file descriptor does not count as that is not a file path). Unfortunately that assumption prevents alternative object representations of file system paths like pathlib from working with pre-existing code, including Python’s standard library.

To fix this situation, a new interface represented by os.PathLike has been defined. By implementing the __fspath__() method, an object signals that it represents a path. An object can then provide a low-level representation of a file system path as a str or bytes object. This means an object is considered path-like if it implements os.PathLike or is a str or bytes object which represents a file system path. Code can use os.fspath(), os.fsdecode(), or os.fsencode() to explicitly get a str and/or bytes representation of a path-like object.

The built-in open() function has been updated to accept os.PathLike objects, as have all relevant functions in the os and os.path modules, and most other functions and classes in the standard library. The os.DirEntry class and relevant classes in pathlib have also been updated to implement os.PathLike.

The hope is that updating the fundamental functions for operating on file system paths will lead to third-party code to implicitly support all path-like objects without any code changes, or at least very minimal ones (e.g. calling os.fspath() at the beginning of code before operating on a path-like object).

Here are some examples of how the new interface allows for pathlib.Path to be used more easily and transparently with pre-existing code:

>>> import pathlib
>>> with open(pathlib.Path("README")) as f:
...     contents = f.read()
...
>>> import os.path
>>> os.path.splitext(pathlib.Path("some_file.txt"))
('some_file', '.txt')
>>> os.path.join("/a/b", pathlib.Path("c"))
'/a/b/c'
>>> import os
>>> os.fspath(pathlib.Path("some_file.txt"))
'some_file.txt'

（由 Brett Cannon, Ethan Furman, Dusty Phillips 和 Jelle Zijlstra 实现。）

参见

PEP 519 [https://www.python.org/dev/peps/pep-0519] – 添加文件系统路径协议

PEP 由 Brett Cannon 和 Koos Zevenhoven 撰写。

PEP 495: 消除本地时间的歧义

In most world locations, there have been and will be times when local clocks are moved back. In those times, intervals are introduced in which local clocks show the same time twice in the same day. In these situations, the information displayed on a local clock (or stored in a Python datetime instance) is insufficient to identify a particular moment in time.

PEP 495 [https://www.python.org/dev/peps/pep-0495] adds the new fold attribute to instances of datetime.datetime and datetime.time classes to differentiate between two moments in time for which local times are the same:

>>> u0 = datetime(2016, 11, 6, 4, tzinfo=timezone.utc)
>>> for i in range(4):
...     u = u0 + i*HOUR
...     t = u.astimezone(Eastern)
...     print(u.time(), 'UTC =', t.time(), t.tzname(), t.fold)
...
04:00:00 UTC = 00:00:00 EDT 0
05:00:00 UTC = 01:00:00 EDT 0
06:00:00 UTC = 01:00:00 EST 1
07:00:00 UTC = 02:00:00 EST 0

The values of the fold attribute have the value 0 for all instances except those that represent the second (chronologically) moment in time in an ambiguous case.

参见

PEP 495 [https://www.python.org/dev/peps/pep-0495] – 消除本地时间的歧义

PEP 由 Alexander Belopolsky 和 Tim Peters 撰写，由 Alexander Belopolsky 实现。

PEP 529: 将Windows文件系统编码更改为UTF-8

Representing filesystem paths is best performed with str (Unicode) rather than bytes. However, there are some situations where using bytes is sufficient and correct.

Prior to Python 3.6, data loss could result when using bytes paths on Windows. With this change, using bytes to represent paths is now supported on Windows, provided those bytes are encoded with the encoding returned by sys.getfilesystemencoding(), which now defaults to 'utf-8'.

Applications that do not use str to represent paths should use os.fsencode() and os.fsdecode() to ensure their bytes are correctly encoded. To revert to the previous behaviour, set PYTHONLEGACYWINDOWSFSENCODING or call sys._enablelegacywindowsfsencoding().

有关详细信息和可能需要的代码修改的讨论，请参见 PEP 529 [https://www.python.org/dev/peps/pep-0529] 。

PEP 528: 将Windows控制台编码更改为UTF-8

The default console on Windows will now accept all Unicode characters and provide correctly read str objects to Python code. sys.stdin, sys.stdout and sys.stderr now default to utf-8 encoding.

This change only applies when using an interactive console, and not when redirecting files or pipes. To revert to the previous behaviour for interactive console use, set PYTHONLEGACYWINDOWSSTDIO.

参见

PEP 528 [https://www.python.org/dev/peps/pep-0528] – 将Windows控制台编码更改为UTF-8

PEP 由 Steve Dower 撰写并实现。

PEP 520: 保留类属性定义顺序

Attributes in a class definition body have a natural ordering: the same order in which the names appear in the source. This order is now preserved in the new class’s __dict__ attribute.

Also, the effective default class execution namespace (returned from type.__prepare__()) is now an insertion-order-preserving mapping.

参见

PEP 520 [https://www.python.org/dev/peps/pep-0520] – 保留类属性定义顺序

PEP 由 Eric Snow 撰写并实现

PEP 468: 保留关键字参数顺序

**kwargs in a function signature is now guaranteed to be an insertion-order-preserving mapping.

参见

PEP 468 [https://www.python.org/dev/peps/pep-0468] – 保留关键字参数顺序

PEP 由 Eric Snow 撰写并实现

新的 dict 实现

The dict type now uses a “compact” representation based on a proposal by Raymond Hettinger [https://mail.python.org/pipermail/python-dev/2012-December/123028.html] which was first implemented by PyPy [https://morepypy.blogspot.com/2015/01/faster-more-memory-efficient-and-more.html]. The memory usage of the new dict() is between 20% and 25% smaller compared to Python 3.5.

The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon (this may change in the future, but it is desired to have this new dict implementation in the language for a few releases before changing the language spec to mandate order-preserving semantics for all current and future Python implementations; this also helps preserve backwards-compatibility with older versions of the language where random iteration order is still in effect, e.g. Python 3.5).

(Contributed by INADA Naoki in bpo-27350 [https://bugs.python.org/issue27350]. Idea originally suggested by Raymond Hettinger [https://mail.python.org/pipermail/python-dev/2012-December/123028.html].)

PEP 523: 向CPython 添加框架评估API

While Python provides extensive support to customize how code executes, one place it has not done so is in the evaluation of frame objects. If you wanted some way to intercept frame evaluation in Python there really wasn’t any way without directly manipulating function pointers for defined functions.

PEP 523 [https://www.python.org/dev/peps/pep-0523] changes this by providing an API to make frame evaluation pluggable at the C level. This will allow for tools such as debuggers and JITs to intercept frame evaluation before the execution of Python code begins. This enables the use of alternative evaluation implementations for Python code, tracking frame evaluation, etc.

This API is not part of the limited C API and is marked as private to signal that usage of this API is expected to be limited and only applicable to very select, low-level use-cases. Semantics of the API will change with Python as necessary.

参见

PEP 523 [https://www.python.org/dev/peps/pep-0523] – 向CPython 添加框架评估API

PEP 由 Brett Cannon 和 Dino Viehland 撰写。

PYTHONMALLOC 环境变量

The new PYTHONMALLOC environment variable allows setting the Python memory allocators and installing debug hooks.

It is now possible to install debug hooks on Python memory allocators on Python compiled in release mode using PYTHONMALLOC=debug. Effects of debug hooks:

新分配的内存中填充字节 0xCB

释放的内存中填充了字节 0xDB

Detect violations of the Python memory allocator API. For example, PyObject_Free() called on a memory block allocated by PyMem_Malloc().

在缓冲区开始之前检测写操作（缓冲区下溢）

在缓冲区结束后检测写操作（缓冲区溢出）

Check that the GIL is held when allocator functions of PYMEM_DOMAIN_OBJ (ex: PyObject_Malloc()) and PYMEM_DOMAIN_MEM (ex: PyMem_Malloc()) domains are called.

检查是否保留了 GIL 也是Python 3.6 的新特性。

See the PyMem_SetupDebugHooks() function for debug hooks on Python memory allocators.

It is now also possible to force the usage of the malloc() allocator of the C library for all Python memory allocations using PYTHONMALLOC=malloc. This is helpful when using external memory debuggers like Valgrind on a Python compiled in release mode.

On error, the debug hooks on Python memory allocators now use the tracemalloc module to get the traceback where a memory block was allocated.

Example of fatal error on buffer overflow using python3.6 -X tracemalloc=5 (store 5 frames in traces):

Debug memory block at address p=0x7fbcd41666f8: API 'o'
    4 bytes originally requested
    The 7 pad bytes at p-7 are FORBIDDENBYTE, as expected.
    The 8 pad bytes at tail=0x7fbcd41666fc are not all FORBIDDENBYTE (0xfb):
        at tail+0: 0x02 *** OUCH
        at tail+1: 0xfb
        at tail+2: 0xfb
        at tail+3: 0xfb
        at tail+4: 0xfb
        at tail+5: 0xfb
        at tail+6: 0xfb
        at tail+7: 0xfb
    The block was made by call #1233329 to debug malloc/realloc.
    Data at p: 1a 2b 30 00

Memory block allocated at (most recent call first):
  File "test/test_bytes.py", line 323
  File "unittest/case.py", line 600
  File "unittest/case.py", line 648
  File "unittest/suite.py", line 122
  File "unittest/suite.py", line 84

Fatal Python error: bad trailing pad byte

Current thread 0x00007fbcdbd32700 (most recent call first):
  File "test/test_bytes.py", line 323 in test_hex
  File "unittest/case.py", line 600 in run
  File "unittest/case.py", line 648 in __call__
  File "unittest/suite.py", line 122 in run
  File "unittest/suite.py", line 84 in __call__
  File "unittest/suite.py", line 122 in run
  File "unittest/suite.py", line 84 in __call__
  ...

（由 Victor Stinner 在 bpo-26516 [https://bugs.python.org/issue26516] 和 bpo-26564 [https://bugs.python.org/issue26564] 中贡献。）

DTrace 和 SystemTap 探测支持

Python can now be built --with-dtrace which enables static markers for the following events in the interpreter:

函数调用/返回

垃圾收集开始/完成

执行的代码行。

This can be used to instrument running interpreters in production, without the need to recompile specific debug builds or providing application-specific profiling/debugging code.

更多信息，请参见 使用 DTrace 和 SystemTap 检测CPython 。

当前的实现已在 Linux 和 macOS 上进行了测试。将来可能会添加其他标记。

(Contributed by Łukasz Langa in bpo-21590 [https://bugs.python.org/issue21590], based on patches by Jesús Cea Avión, David Malcolm, and Nikhil Benesch.)

其他语言特性修改

对Python 语言核心进行的小改动：

A global or nonlocal statement must now textually appear before the first use of the affected name in the same scope. Previously this was a SyntaxWarning.

It is now possible to set a special method to None to indicate that the corresponding operation is not available. For example, if a class sets __iter__() to None, the class is not iterable. (Contributed by Andrew Barnert and Ivan Levkivskyi in bpo-25958 [https://bugs.python.org/issue25958].)

Long sequences of repeated traceback lines are now abbreviated as "[Previous line repeated {count} more times]" (see 回溯 for an example). (Contributed by Emanuel Barry in bpo-26823 [https://bugs.python.org/issue26823].)

Import now raises the new exception ModuleNotFoundError (subclass of ImportError) when it cannot find a module. Code that currently checks for ImportError (in try-except) will still work. (Contributed by Eric Snow in bpo-15767 [https://bugs.python.org/issue15767].)

Class methods relying on zero-argument super() will now work correctly when called from metaclass methods during class creation. (Contributed by Martin Teichmann in bpo-23722 [https://bugs.python.org/issue23722].)

新增模块

secrets

The main purpose of the new secrets module is to provide an obvious way to reliably generate cryptographically strong pseudo-random values suitable for managing secrets, such as account authentication, tokens, and similar.

警告

Note that the pseudo-random generators in the random module should NOT be used for security purposes. Use secrets on Python 3.6+ and os.urandom() on Python 3.5 and earlier.

参见

PEP 506 [https://www.python.org/dev/peps/pep-0506] – Secrets模块被加入Python标准库

PEP 由 Steven D’Aprano 撰写并实现。

改进的模块

array

Exhausted iterators of array.array will now stay exhausted even if the iterated array is extended. This is consistent with the behavior of other mutable sequences.

（由 Serhiy Storchaka 在 bpo-26492 [https://bugs.python.org/issue26492] 中贡献。）

ast

The new ast.Constant AST node has been added. It can be used by external AST optimizers for the purposes of constant folding.

（由 Victor Stinner 在 bpo-26146 [https://bugs.python.org/issue26146] 中贡献。）

asyncio

Starting with Python 3.6 the asyncio module is no longer provisional and its API is considered stable.

Notable changes in the asyncio module since Python 3.5.0 (all backported to 3.5.x due to the provisional status):

The get_event_loop() function has been changed to always return the currently running loop when called from coroutines and callbacks. (Contributed by Yury Selivanov in bpo-28613 [https://bugs.python.org/issue28613].)

The ensure_future() function and all functions that use it, such as loop.run_until_complete(), now accept all kinds of awaitable objects. (Contributed by Yury Selivanov.)

New run_coroutine_threadsafe() function to submit coroutines to event loops from other threads. (Contributed by Vincent Michel.)

New Transport.is_closing() method to check if the transport is closing or closed. (Contributed by Yury Selivanov.)

The loop.create_server() method can now accept a list of hosts. (Contributed by Yann Sionneau.)

New loop.create_future() method to create Future objects. This allows alternative event loop implementations, such as uvloop [https://github.com/MagicStack/uvloop], to provide a faster asyncio.Future implementation. (Contributed by Yury Selivanov in bpo-27041 [https://bugs.python.org/issue27041].)

New loop.get_exception_handler() method to get the current exception handler. (Contributed by Yury Selivanov in bpo-27040 [https://bugs.python.org/issue27040].)

New StreamReader.readuntil() method to read data from the stream until a separator bytes sequence appears. (Contributed by Mark Korenberg.)

The performance of StreamReader.readexactly() has been improved. (Contributed by Mark Korenberg in bpo-28370 [https://bugs.python.org/issue28370].)

The loop.getaddrinfo() method is optimized to avoid calling the system getaddrinfo function if the address is already resolved. (Contributed by A. Jesse Jiryu Davis.)

The loop.stop() method has been changed to stop the loop immediately after the current iteration. Any new callbacks scheduled as a result of the last iteration will be discarded. (Contributed by Guido van Rossum in bpo-25593 [https://bugs.python.org/issue25593].)

Future.set_exception will now raise TypeError when passed an instance of the StopIteration exception. (Contributed by Chris Angelico in bpo-26221 [https://bugs.python.org/issue26221].)

New loop.connect_accepted_socket() method to be used by servers that accept connections outside of asyncio, but that use asyncio to handle them. (Contributed by Jim Fulton in bpo-27392 [https://bugs.python.org/issue27392].)

TCP_NODELAY flag is now set for all TCP transports by default. (Contributed by Yury Selivanov in bpo-27456 [https://bugs.python.org/issue27456].)

New loop.shutdown_asyncgens() to properly close pending asynchronous generators before closing the loop. (Contributed by Yury Selivanov in bpo-28003 [https://bugs.python.org/issue28003].)

Future and Task classes now have an optimized C implementation which makes asyncio code up to 30% faster. (Contributed by Yury Selivanov and INADA Naoki in bpo-26081 [https://bugs.python.org/issue26081] and bpo-28544 [https://bugs.python.org/issue28544].)

binascii

The b2a_base64() function now accepts an optional newline keyword argument to control whether the newline character is appended to the return value. (Contributed by Victor Stinner in bpo-25357 [https://bugs.python.org/issue25357].)

cmath

The new cmath.tau (τ) constant has been added. (Contributed by Lisa Roach in bpo-12345 [https://bugs.python.org/issue12345], see PEP 628 [https://www.python.org/dev/peps/pep-0628] for details.)

New constants: cmath.inf and cmath.nan to match math.inf and math.nan, and also cmath.infj and cmath.nanj to match the format used by complex repr. (Contributed by Mark Dickinson in bpo-23229 [https://bugs.python.org/issue23229].)

collections

The new Collection abstract base class has been added to represent sized iterable container classes. (Contributed by Ivan Levkivskyi, docs by Neil Girdhar in bpo-27598 [https://bugs.python.org/issue27598].)

The new Reversible abstract base class represents iterable classes that also provide the __reversed__() method. (Contributed by Ivan Levkivskyi in bpo-25987 [https://bugs.python.org/issue25987].)

The new AsyncGenerator abstract base class represents asynchronous generators. (Contributed by Yury Selivanov in bpo-28720 [https://bugs.python.org/issue28720].)

The namedtuple() function now accepts an optional keyword argument module, which, when specified, is used for the __module__ attribute of the returned named tuple class. (Contributed by Raymond Hettinger in bpo-17941 [https://bugs.python.org/issue17941].)

The verbose and rename arguments for namedtuple() are now keyword-only. (Contributed by Raymond Hettinger in bpo-25628 [https://bugs.python.org/issue25628].)

Recursive collections.deque instances can now be pickled. (Contributed by Serhiy Storchaka in bpo-26482 [https://bugs.python.org/issue26482].)

concurrent.futures

The ThreadPoolExecutor class constructor now accepts an optional thread_name_prefix argument to make it possible to customize the names of the threads created by the pool. (Contributed by Gregory P. Smith in bpo-27664 [https://bugs.python.org/issue27664].)

contextlib

The contextlib.AbstractContextManager class has been added to provide an abstract base class for context managers. It provides a sensible default implementation for __enter__() which returns self and leaves __exit__() an abstract method. A matching class has been added to the typing module as typing.ContextManager. (Contributed by Brett Cannon in bpo-25609 [https://bugs.python.org/issue25609].)

datetime

The datetime and time classes have the new fold attribute used to disambiguate local time when necessary. Many functions in the datetime have been updated to support local time disambiguation. See Local Time Disambiguation section for more information. (Contributed by Alexander Belopolsky in bpo-24773 [https://bugs.python.org/issue24773].)

The datetime.strftime() and date.strftime() methods now support ISO 8601 date directives %G, %u and %V. (Contributed by Ashley Anderson in bpo-12006 [https://bugs.python.org/issue12006].)

The datetime.isoformat() function now accepts an optional timespec argument that specifies the number of additional components of the time value to include. (Contributed by Alessandro Cucci and Alexander Belopolsky in bpo-19475 [https://bugs.python.org/issue19475].)

The datetime.combine() now accepts an optional tzinfo argument. (Contributed by Alexander Belopolsky in bpo-27661 [https://bugs.python.org/issue27661].)

decimal

New Decimal.as_integer_ratio() method that returns a pair (n, d) of integers that represent the given Decimal instance as a fraction, in lowest terms and with a positive denominator:

>>> Decimal('-3.14').as_integer_ratio()
(-157, 50)

（由 Stefan Krah 和 Mark Dickinson 在 bpo-25928 [https://bugs.python.org/issue25928] 中贡献。）

distutils

The default_format attribute has been removed from distutils.command.sdist.sdist and the formats attribute defaults to ['gztar']. Although not anticipated, any code relying on the presence of default_format may need to be adapted. See bpo-27819 [https://bugs.python.org/issue27819] for more details.

email

The new email API, enabled via the policy keyword to various constructors, is no longer provisional. The email documentation has been reorganized and rewritten to focus on the new API, while retaining the old documentation for the legacy API. (Contributed by R. David Murray in bpo-24277 [https://bugs.python.org/issue24277].)

The email.mime classes now all accept an optional policy keyword. (Contributed by Berker Peksag in bpo-27331 [https://bugs.python.org/issue27331].)

The DecodedGenerator now supports the policy keyword.

There is a new policy attribute, message_factory, that controls what class is used by default when the parser creates new message objects. For the email.policy.compat32 policy this is Message, for the new policies it is EmailMessage. (Contributed by R. David Murray in bpo-20476 [https://bugs.python.org/issue20476].)

encodings

On Windows, added the 'oem' encoding to use CP_OEMCP, and the 'ansi' alias for the existing 'mbcs' encoding, which uses the CP_ACP code page. (Contributed by Steve Dower in bpo-27959 [https://bugs.python.org/issue27959].)

enum

Two new enumeration base classes have been added to the enum module: Flag and IntFlags. Both are used to define constants that can be combined using the bitwise operators. (Contributed by Ethan Furman in bpo-23591 [https://bugs.python.org/issue23591].)

Many standard library modules have been updated to use the IntFlags class for their constants.

The new enum.auto value can be used to assign values to enum members automatically:

>>> from enum import Enum, auto
>>> class Color(Enum):
...     red = auto()
...     blue = auto()
...     green = auto()
...
>>> list(Color)
[<Color.red: 1>, <Color.blue: 2>, <Color.green: 3>]

faulthandler

On Windows, the faulthandler module now installs a handler for Windows exceptions: see faulthandler.enable(). (Contributed by Victor Stinner in bpo-23848 [https://bugs.python.org/issue23848].)

fileinput

hook_encoded() now supports the errors argument. (Contributed by Joseph Hackman in bpo-25788 [https://bugs.python.org/issue25788].)

hashlib

hashlib supports OpenSSL 1.1.0. The minimum recommend version is 1.0.2. (Contributed by Christian Heimes in bpo-26470 [https://bugs.python.org/issue26470].)

BLAKE2 hash functions were added to the module. blake2b() and blake2s() are always available and support the full feature set of BLAKE2. (Contributed by Christian Heimes in bpo-26798 [https://bugs.python.org/issue26798] based on code by Dmitry Chestnykh and Samuel Neves. Documentation written by Dmitry Chestnykh.)

The SHA-3 hash functions sha3_224(), sha3_256(), sha3_384(), sha3_512(), and SHAKE hash functions shake_128() and shake_256() were added. (Contributed by Christian Heimes in bpo-16113 [https://bugs.python.org/issue16113]. Keccak Code Package by Guido Bertoni, Joan Daemen, Michaël Peeters, Gilles Van Assche, and Ronny Van Keer.)

The password-based key derivation function scrypt() is now available with OpenSSL 1.1.0 and newer. (Contributed by Christian Heimes in bpo-27928 [https://bugs.python.org/issue27928].)

http.client

HTTPConnection.request() and endheaders() both now support chunked encoding request bodies. (Contributed by Demian Brecht and Rolf Krahl in bpo-12319 [https://bugs.python.org/issue12319].)

idlelib 与 IDLE

The idlelib package is being modernized and refactored to make IDLE look and work better and to make the code easier to understand, test, and improve. Part of making IDLE look better, especially on Linux and Mac, is using ttk widgets, mostly in the dialogs. As a result, IDLE no longer runs with tcl/tk 8.4. It now requires tcl/tk 8.5 or 8.6. We recommend running the latest release of either.

‘Modernizing’ includes renaming and consolidation of idlelib modules. The renaming of files with partial uppercase names is similar to the renaming of, for instance, Tkinter and TkFont to tkinter and tkinter.font in 3.0. As a result, imports of idlelib files that worked in 3.5 will usually not work in 3.6. At least a module name change will be needed (see idlelib/README.txt), sometimes more. (Name changes contributed by Al Swiegart and Terry Reedy in bpo-24225 [https://bugs.python.org/issue24225]. Most idlelib patches since have been and will be part of the process.)

In compensation, the eventual result with be that some idlelib classes will be easier to use, with better APIs and docstrings explaining them. Additional useful information will be added to idlelib when available.

在 3.6.2 中新增:

多个对自动补全的修正。 （由 Louie Lu 在 bpo-15786 [https://bugs.python.org/issue15786] 中贡献。）

在 3.6.3 中新增:

Module Browser (在 File 菜单中，之前称为 Class Browser) 现在会在最高层级函数和类之外显示嵌套的函数和类。 （由 Guilherme Polo, Cheryl Sabella 和 Terry Jan Reedy 在 bpo-1612262 [https://bugs.python.org/issue1612262] 中贡献。）

之前以扩展形式实现的 IDLE 特性已作为正常特性重新实现。 它们的设置已从 Extensions 选项卡移至其他对话框选项卡。 （由 Charles Wohlganger 和 Terry Jan Reedy 在 bpo-27099 [https://bugs.python.org/issue27099] 中实现。）

Settings 对话框 (Options 中的 Configure IDLE) 已经被部分重写以改进外观和功能。 （由 Cheryl Sabella 和 Terry Jan Reedy 在多个问题项中贡献。）

在 3.6.4 中新增:

字体样本现在包括一组非拉丁字符以便用户能更好地查看所选特定字体的效果。 （由 Terry Jan Reedy 在 bpo-13802 [https://bugs.python.org/issue13802] 中贡献。） 样本可以被修改以包括其他字符。 （由 Serhiy Storchaka 在 bpo-31860 [https://bugs.python.org/issue31860] 中贡献。）

在 3.6.6 中新增:

编辑器代码上下文选项已经过修改。 Box 会显示所有上下文行直到最大行数。 点击一个上下文行会使编辑器跳转到该行。 自定义主题的上下文颜色已添加到 Settings 对话框的 Highlights 选项卡。 （由 Cheryl Sabella 和 Terry Jan Reedy 在 bpo-33642 [https://bugs.python.org/issue33642], bpo-33768 [https://bugs.python.org/issue33768] 和 bpo-33679 [https://bugs.python.org/issue33679] 中贡献。）

在 Windows 上，会有新的 API 调用将 tk 对 DPI 的调整告知 Windows。 在 Windows 8.1+ 或 10 上，如果 Python 二进制码的 DPI 兼容属性未改变，并且监视器分辨率大于 96 DPI，这应该会令文本和线条更清晰。 否则的话它应该不造成影响。 （由 Terry Jan Reedy 在 bpo-33656 [https://bugs.python.org/issue33656] 中贡献。）

在 3.6.7 中新增:

超过 N 行（默认值为 50）的输出将被折叠为一个按钮。 N 可以在 Settings 对话框的 General 页的 PyShell 部分中进行修改。 数量较少但是超长的行可以通过在输出上右击来折叠。 被折叠的输出可通过双击按钮来展开，或是通过右击按钮来放入剪贴板或是单独的窗口。 （由 Tal Einat 在 bpo-1529353 [https://bugs.python.org/issue1529353] 中贡献。）

importlib

Import now raises the new exception ModuleNotFoundError (subclass of ImportError) when it cannot find a module. Code that current checks for ImportError (in try-except) will still work. (Contributed by Eric Snow in bpo-15767 [https://bugs.python.org/issue15767].)

importlib.util.LazyLoader now calls create_module() on the wrapped loader, removing the restriction that importlib.machinery.BuiltinImporter and importlib.machinery.ExtensionFileLoader couldn’t be used with importlib.util.LazyLoader.

importlib.util.cache_from_source(), importlib.util.source_from_cache(), and importlib.util.spec_from_file_location() now accept a path-like object.

inspect

The inspect.signature() function now reports the implicit .0 parameters generated by the compiler for comprehension and generator expression scopes as if they were positional-only parameters called implicit0. (Contributed by Jelle Zijlstra in bpo-19611 [https://bugs.python.org/issue19611].)

To reduce code churn when upgrading from Python 2.7 and the legacy inspect.getargspec() API, the previously documented deprecation of inspect.getfullargspec() has been reversed. While this function is convenient for single/source Python 2/3 code bases, the richer inspect.signature() interface remains the recommended approach for new code. (Contributed by Nick Coghlan in bpo-27172 [https://bugs.python.org/issue27172])

json

json.load() and json.loads() now support binary input. Encoded JSON should be represented using either UTF-8, UTF-16, or UTF-32. (Contributed by Serhiy Storchaka in bpo-17909 [https://bugs.python.org/issue17909].)

logging

The new WatchedFileHandler.reopenIfNeeded() method has been added to add the ability to check if the log file needs to be reopened. (Contributed by Marian Horban in bpo-24884 [https://bugs.python.org/issue24884].)

math

The tau (τ) constant has been added to the math and cmath modules. (Contributed by Lisa Roach in bpo-12345 [https://bugs.python.org/issue12345], see PEP 628 [https://www.python.org/dev/peps/pep-0628] for details.)

multiprocessing

Proxy Objects returned by multiprocessing.Manager() can now be nested. (Contributed by Davin Potts in bpo-6766 [https://bugs.python.org/issue6766].)

os

See the summary of PEP 519 for details on how the os and os.path modules now support path-like objects.

scandir() now supports bytes paths on Windows.

A new close() method allows explicitly closing a scandir() iterator. The scandir() iterator now supports the context manager protocol. If a scandir() iterator is neither exhausted nor explicitly closed a ResourceWarning will be emitted in its destructor. (Contributed by Serhiy Storchaka in bpo-25994 [https://bugs.python.org/issue25994].)

在 Linux 上，现在 os.urandom() 会阻塞直到系统的 urandom 熵池被初始化以提升安全性。 其理由参见 PEP 524 [https://www.python.org/dev/peps/pep-0524]。

The Linux getrandom() syscall (get random bytes) is now exposed as the new os.getrandom() function. (Contributed by Victor Stinner, part of the PEP 524 [https://www.python.org/dev/peps/pep-0524])

pathlib

pathlib now supports path-like objects. (Contributed by Brett Cannon in bpo-27186 [https://bugs.python.org/issue27186].)

See the summary of PEP 519 for details.

pdb

The Pdb class constructor has a new optional readrc argument to control whether .pdbrc files should be read.

pickle

Objects that need __new__ called with keyword arguments can now be pickled using pickle protocols older than protocol version 4. Protocol version 4 already supports this case. (Contributed by Serhiy Storchaka in bpo-24164 [https://bugs.python.org/issue24164].)

pickletools

pickletools.dis() now outputs the implicit memo index for the MEMOIZE opcode. (Contributed by Serhiy Storchaka in bpo-25382 [https://bugs.python.org/issue25382].)

pydoc

The pydoc module has learned to respect the MANPAGER environment variable. (Contributed by Matthias Klose in bpo-8637 [https://bugs.python.org/issue8637].)

help() and pydoc can now list named tuple fields in the order they were defined rather than alphabetically. (Contributed by Raymond Hettinger in bpo-24879 [https://bugs.python.org/issue24879].)

random

The new choices() function returns a list of elements of specified size from the given population with optional weights. (Contributed by Raymond Hettinger in bpo-18844 [https://bugs.python.org/issue18844].)

re

Added support of modifier spans in regular expressions. Examples: '(?i:p)ython' matches 'python' and 'Python', but not 'PYTHON'; '(?i)g(?-i:v)r' matches 'GvR' and 'gvr', but not 'GVR'. (Contributed by Serhiy Storchaka in bpo-433028 [https://bugs.python.org/issue433028].)

Match object groups can be accessed by __getitem__, which is equivalent to group(). So mo['name'] is now equivalent to mo.group('name'). (Contributed by Eric Smith in bpo-24454 [https://bugs.python.org/issue24454].)

Match objects now support index-like objects as group indices. (Contributed by Jeroen Demeyer and Xiang Zhang in bpo-27177 [https://bugs.python.org/issue27177].)

readline

Added set_auto_history() to enable or disable automatic addition of input to the history list. (Contributed by Tyler Crompton in bpo-26870 [https://bugs.python.org/issue26870].)

rlcompleter

Private and special attribute names now are omitted unless the prefix starts with underscores. A space or a colon is added after some completed keywords. (Contributed by Serhiy Storchaka in bpo-25011 [https://bugs.python.org/issue25011] and bpo-25209 [https://bugs.python.org/issue25209].)

shlex

The shlex has much improved shell compatibility through the new punctuation_chars argument to control which characters are treated as punctuation. (Contributed by Vinay Sajip in bpo-1521950 [https://bugs.python.org/issue1521950].)

site

When specifying paths to add to sys.path in a .pth file, you may now specify file paths on top of directories (e.g. zip files). (Contributed by Wolfgang Langner in bpo-26587 [https://bugs.python.org/issue26587]).

sqlite3

sqlite3.Cursor.lastrowid now supports the REPLACE statement. (Contributed by Alex LordThorsen in bpo-16864 [https://bugs.python.org/issue16864].)

socket

The ioctl() function now supports the SIO_LOOPBACK_FAST_PATH control code. (Contributed by Daniel Stokes in bpo-26536 [https://bugs.python.org/issue26536].)

The getsockopt() constants SO_DOMAIN, SO_PROTOCOL, SO_PEERSEC, and SO_PASSSEC are now supported. (Contributed by Christian Heimes in bpo-26907 [https://bugs.python.org/issue26907].)

The setsockopt() now supports the setsockopt(level, optname, None, optlen: int) form. (Contributed by Christian Heimes in bpo-27744 [https://bugs.python.org/issue27744].)

The socket module now supports the address family AF_ALG to interface with Linux Kernel crypto API. ALG_*, SOL_ALG and sendmsg_afalg() were added. (Contributed by Christian Heimes in bpo-27744 [https://bugs.python.org/issue27744] with support from Victor Stinner.)

New Linux constants TCP_USER_TIMEOUT and TCP_CONGESTION were added. (Contributed by Omar Sandoval, issue:26273).

socketserver

Servers based on the socketserver module, including those defined in http.server, xmlrpc.server and wsgiref.simple_server, now support the context manager protocol. (Contributed by Aviv Palivoda in bpo-26404 [https://bugs.python.org/issue26404].)

The wfile attribute of StreamRequestHandler classes now implements the io.BufferedIOBase writable interface. In particular, calling write() is now guaranteed to send the data in full. (Contributed by Martin Panter in bpo-26721 [https://bugs.python.org/issue26721].)

ssl

ssl supports OpenSSL 1.1.0. The minimum recommend version is 1.0.2. (Contributed by Christian Heimes in bpo-26470 [https://bugs.python.org/issue26470].)

3DES has been removed from the default cipher suites and ChaCha20 Poly1305 cipher suites have been added. (Contributed by Christian Heimes in bpo-27850 [https://bugs.python.org/issue27850] and bpo-27766 [https://bugs.python.org/issue27766].)

SSLContext has better default configuration for options and ciphers. (Contributed by Christian Heimes in bpo-28043 [https://bugs.python.org/issue28043].)

SSL session can be copied from one client-side connection to another with the new SSLSession class. TLS session resumption can speed up the initial handshake, reduce latency and improve performance (Contributed by Christian Heimes in bpo-19500 [https://bugs.python.org/issue19500] based on a draft by Alex Warhawk.)

The new get_ciphers() method can be used to get a list of enabled ciphers in order of cipher priority.

All constants and flags have been converted to IntEnum and IntFlags. (Contributed by Christian Heimes in bpo-28025 [https://bugs.python.org/issue28025].)

Server and client-side specific TLS protocols for SSLContext were added. (Contributed by Christian Heimes in bpo-28085 [https://bugs.python.org/issue28085].)

Added SSLContext.post_handshake_auth to enable and ssl.SSLSocket.verify_client_post_handshake() to initiate TLS 1.3 post-handshake authentication. (Contributed by Christian Heimes in bpo-34670 [https://bugs.python.org/issue34670].)

statistics

A new harmonic_mean() function has been added. (Contributed by Steven D’Aprano in bpo-27181 [https://bugs.python.org/issue27181].)

struct

struct now supports IEEE 754 half-precision floats via the 'e' format specifier. (Contributed by Eli Stevens, Mark Dickinson in bpo-11734 [https://bugs.python.org/issue11734].)

subprocess

subprocess.Popen destructor now emits a ResourceWarning warning if the child process is still running. Use the context manager protocol (with proc: ...) or explicitly call the wait() method to read the exit status of the child process. (Contributed by Victor Stinner in bpo-26741 [https://bugs.python.org/issue26741].)

The subprocess.Popen constructor and all functions that pass arguments through to it now accept encoding and errors arguments. Specifying either of these will enable text mode for the stdin, stdout and stderr streams. (Contributed by Steve Dower in bpo-6135 [https://bugs.python.org/issue6135].)

sys

The new getfilesystemencodeerrors() function returns the name of the error mode used to convert between Unicode filenames and bytes filenames. (Contributed by Steve Dower in bpo-27781 [https://bugs.python.org/issue27781].)

On Windows the return value of the getwindowsversion() function now includes the platform_version field which contains the accurate major version, minor version and build number of the current operating system, rather than the version that is being emulated for the process (Contributed by Steve Dower in bpo-27932 [https://bugs.python.org/issue27932].)

telnetlib

Telnet is now a context manager (contributed by Stéphane Wirtel in bpo-25485 [https://bugs.python.org/issue25485]).

time

The struct_time attributes tm_gmtoff and tm_zone are now available on all platforms.

timeit

The new Timer.autorange() convenience method has been added to call Timer.timeit() repeatedly so that the total run time is greater or equal to 200 milliseconds. (Contributed by Steven D’Aprano in bpo-6422 [https://bugs.python.org/issue6422].)

timeit now warns when there is substantial (4x) variance between best and worst times. (Contributed by Serhiy Storchaka in bpo-23552 [https://bugs.python.org/issue23552].)

tkinter

Added methods trace_add(), trace_remove() and trace_info() in the tkinter.Variable class. They replace old methods trace_variable(), trace(), trace_vdelete() and trace_vinfo() that use obsolete Tcl commands and might not work in future versions of Tcl. (Contributed by Serhiy Storchaka in bpo-22115 [https://bugs.python.org/issue22115]).

回溯

Both the traceback module and the interpreter’s builtin exception display now abbreviate long sequences of repeated lines in tracebacks as shown in the following example:

>>> def f(): f()
...
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in f
  File "<stdin>", line 1, in f
  File "<stdin>", line 1, in f
  [Previous line repeated 995 more times]
RecursionError: maximum recursion depth exceeded

（由 Emanuel Barry在 bpo-26823 [https://bugs.python.org/issue26823] 中贡献。）

tracemalloc

The tracemalloc module now supports tracing memory allocations in multiple different address spaces.

The new DomainFilter filter class has been added to filter block traces by their address space (domain).

（由 Victor Stinner 在 bpo-26588 [https://bugs.python.org/issue26588] 中贡献。）

typing

Since the typing module is provisional, all changes introduced in Python 3.6 have also been backported to Python 3.5.x.

The typing module has a much improved support for generic type aliases. For example Dict[str, Tuple[S, T]] is now a valid type annotation. (Contributed by Guido van Rossum in Github #195 [https://github.com/python/typing/pull/195].)

The typing.ContextManager class has been added for representing contextlib.AbstractContextManager. (Contributed by Brett Cannon in bpo-25609 [https://bugs.python.org/issue25609].)

The typing.Collection class has been added for representing collections.abc.Collection. (Contributed by Ivan Levkivskyi in bpo-27598 [https://bugs.python.org/issue27598].)

The typing.ClassVar type construct has been added to mark class variables. As introduced in PEP 526 [https://www.python.org/dev/peps/pep-0526], a variable annotation wrapped in ClassVar indicates that a given attribute is intended to be used as a class variable and should not be set on instances of that class. (Contributed by Ivan Levkivskyi in Github #280 [https://github.com/python/typing/pull/280].)

A new TYPE_CHECKING constant that is assumed to be True by the static type chekers, but is False at runtime. (Contributed by Guido van Rossum in Github #230 [https://github.com/python/typing/issues/230].)

A new NewType() helper function has been added to create lightweight distinct types for annotations:

from typing import NewType

UserId = NewType('UserId', int)
some_id = UserId(524313)

The static type checker will treat the new type as if it were a subclass of the original type. (Contributed by Ivan Levkivskyi in Github #189 [https://github.com/python/typing/issues/189].)

unicodedata

The unicodedata module now uses data from Unicode 9.0.0 [http://unicode.org/versions/Unicode9.0.0/]. (Contributed by Benjamin Peterson.)

unittest.mock

The Mock class has the following improvements:

Two new methods, Mock.assert_called() and Mock.assert_called_once() to check if the mock object was called. (Contributed by Amit Saha in bpo-26323 [https://bugs.python.org/issue26323].)

The Mock.reset_mock() method now has two optional keyword only arguments: return_value and side_effect. (Contributed by Kushal Das in bpo-21271 [https://bugs.python.org/issue21271].)

urllib.request

If a HTTP request has a file or iterable body (other than a bytes object) but no Content-Length header, rather than throwing an error, AbstractHTTPHandler now falls back to use chunked transfer encoding. (Contributed by Demian Brecht and Rolf Krahl in bpo-12319 [https://bugs.python.org/issue12319].)

urllib.robotparser

RobotFileParser now supports the Crawl-delay and Request-rate extensions. (Contributed by Nikolay Bogoychev in bpo-16099 [https://bugs.python.org/issue16099].)

venv

venv accepts a new parameter --prompt. This parameter provides an alternative prefix for the virtual environment. (Proposed by Łukasz Balcerzak and ported to 3.6 by Stéphane Wirtel in bpo-22829 [https://bugs.python.org/issue22829].)

warnings

A new optional source parameter has been added to the warnings.warn_explicit() function: the destroyed object which emitted a ResourceWarning. A source attribute has also been added to warnings.WarningMessage (contributed by Victor Stinner in bpo-26568 [https://bugs.python.org/issue26568] and bpo-26567 [https://bugs.python.org/issue26567]).

When a ResourceWarning warning is logged, the tracemalloc module is now used to try to retrieve the traceback where the destroyed object was allocated.

Example with the script example.py:

import warnings

def func():
    return open(__file__)

f = func()
f = None

Output of the command python3.6 -Wd -X tracemalloc=5 example.py:

example.py:7: ResourceWarning: unclosed file <_io.TextIOWrapper name='example.py' mode='r' encoding='UTF-8'>
  f = None
Object allocated at (most recent call first):
  File "example.py", lineno 4
    return open(__file__)
  File "example.py", lineno 6
    f = func()

The “Object allocated at” traceback is new and is only displayed if tracemalloc is tracing Python memory allocations and if the warnings module was already imported.

winreg

Added the 64-bit integer type REG_QWORD. (Contributed by Clement Rouault in bpo-23026 [https://bugs.python.org/issue23026].)

winsound

Allowed keyword arguments to be passed to Beep, MessageBeep, and PlaySound (bpo-27982 [https://bugs.python.org/issue27982]).

xmlrpc.client

The xmlrpc.client module now supports unmarshalling additional data types used by the Apache XML-RPC implementation for numerics and None. (Contributed by Serhiy Storchaka in bpo-26885 [https://bugs.python.org/issue26885].)

zipfile

A new ZipInfo.from_file() class method allows making a ZipInfo instance from a filesystem file. A new ZipInfo.is_dir() method can be used to check if the ZipInfo instance represents a directory. (Contributed by Thomas Kluyver in bpo-26039 [https://bugs.python.org/issue26039].)

The ZipFile.open() method can now be used to write data into a ZIP file, as well as for extracting data. (Contributed by Thomas Kluyver in bpo-26039 [https://bugs.python.org/issue26039].)

zlib

The compress() and decompress() functions now accept keyword arguments. (Contributed by Aviv Palivoda in bpo-26243 [https://bugs.python.org/issue26243] and Xiang Zhang in bpo-16764 [https://bugs.python.org/issue16764] respectively.)

性能优化

The Python interpreter now uses a 16-bit wordcode instead of bytecode which made a number of opcode optimizations possible. (Contributed by Demur Rumed with input and reviews from Serhiy Storchaka and Victor Stinner in bpo-26647 [https://bugs.python.org/issue26647] and bpo-28050 [https://bugs.python.org/issue28050].)

The asyncio.Future class now has an optimized C implementation. (Contributed by Yury Selivanov and INADA Naoki in bpo-26081 [https://bugs.python.org/issue26081].)

The asyncio.Task class now has an optimized C implementation. (Contributed by Yury Selivanov in bpo-28544 [https://bugs.python.org/issue28544].)

Various implementation improvements in the typing module (such as caching of generic types) allow up to 30 times performance improvements and reduced memory footprint.

The ASCII decoder is now up to 60 times as fast for error handlers surrogateescape, ignore and replace (Contributed by Victor Stinner in bpo-24870 [https://bugs.python.org/issue24870]).

The ASCII and the Latin1 encoders are now up to 3 times as fast for the error handler surrogateescape (Contributed by Victor Stinner in bpo-25227 [https://bugs.python.org/issue25227]).

The UTF-8 encoder is now up to 75 times as fast for error handlers ignore, replace, surrogateescape, surrogatepass (Contributed by Victor Stinner in bpo-25267 [https://bugs.python.org/issue25267]).

The UTF-8 decoder is now up to 15 times as fast for error handlers ignore, replace and surrogateescape (Contributed by Victor Stinner in bpo-25301 [https://bugs.python.org/issue25301]).

bytes % args is now up to 2 times faster. (Contributed by Victor Stinner in bpo-25349 [https://bugs.python.org/issue25349]).

bytearray % args is now between 2.5 and 5 times faster. (Contributed by Victor Stinner in bpo-25399 [https://bugs.python.org/issue25399]).

Optimize bytes.fromhex() and bytearray.fromhex(): they are now between 2x and 3.5x faster. (Contributed by Victor Stinner in bpo-25401 [https://bugs.python.org/issue25401]).

Optimize bytes.replace(b'', b'.') and bytearray.replace(b'', b'.'): up to 80% faster. (Contributed by Josh Snider in bpo-26574 [https://bugs.python.org/issue26574]).

Allocator functions of the PyMem_Malloc() domain (PYMEM_DOMAIN_MEM) now use the pymalloc memory allocator instead of malloc() function of the C library. The pymalloc allocator is optimized for objects smaller or equal to 512 bytes with a short lifetime, and use malloc() for larger memory blocks. (Contributed by Victor Stinner in bpo-26249 [https://bugs.python.org/issue26249]).

pickle.load() and pickle.loads() are now up to 10% faster when deserializing many small objects (Contributed by Victor Stinner in bpo-27056 [https://bugs.python.org/issue27056]).

Passing keyword arguments to a function has an overhead in comparison with passing positional arguments. Now in extension functions implemented with using Argument Clinic this overhead is significantly decreased. (Contributed by Serhiy Storchaka in bpo-27574 [https://bugs.python.org/issue27574]).

Optimized glob() and iglob() functions in the glob module; they are now about 3–6 times faster. (Contributed by Serhiy Storchaka in bpo-25596 [https://bugs.python.org/issue25596]).

Optimized globbing in pathlib by using os.scandir(); it is now about 1.5–4 times faster. (Contributed by Serhiy Storchaka in bpo-26032 [https://bugs.python.org/issue26032]).

xml.etree.ElementTree parsing, iteration and deepcopy performance has been significantly improved. (Contributed by Serhiy Storchaka in bpo-25638 [https://bugs.python.org/issue25638], bpo-25873 [https://bugs.python.org/issue25873], and bpo-25869 [https://bugs.python.org/issue25869].)

Creation of fractions.Fraction instances from floats and decimals is now 2 to 3 times faster. (Contributed by Serhiy Storchaka in bpo-25971 [https://bugs.python.org/issue25971].)

构建和 C API 的改变

Python now requires some C99 support in the toolchain to build. Most notably, Python now uses standard integer types and macros in place of custom macros like PY_LONG_LONG. For more information, see PEP 7 [https://www.python.org/dev/peps/pep-0007] and bpo-17884 [https://bugs.python.org/issue17884].

Cross-compiling CPython with the Android NDK and the Android API level set to 21 (Android 5.0 Lollipop) or greater runs successfully. While Android is not yet a supported platform, the Python test suite runs on the Android emulator with only about 16 tests failures. See the Android meta-issue bpo-26865 [https://bugs.python.org/issue26865].

The --enable-optimizations configure flag has been added. Turning it on will activate expensive optimizations like PGO. (Original patch by Alecsandru Patrascu of Intel in bpo-26359 [https://bugs.python.org/issue26359].)

The GIL must now be held when allocator functions of PYMEM_DOMAIN_OBJ (ex: PyObject_Malloc()) and PYMEM_DOMAIN_MEM (ex: PyMem_Malloc()) domains are called.

New Py_FinalizeEx() API which indicates if flushing buffered data failed. (Contributed by Martin Panter in bpo-5319 [https://bugs.python.org/issue5319].)

PyArg_ParseTupleAndKeywords() now supports positional-only parameters. Positional-only parameters are defined by empty names. (Contributed by Serhiy Storchaka in bpo-26282 [https://bugs.python.org/issue26282]).

PyTraceback_Print method now abbreviates long sequences of repeated lines as "[Previous line repeated {count} more times]". (Contributed by Emanuel Barry in bpo-26823 [https://bugs.python.org/issue26823].)

The new PyErr_SetImportErrorSubclass() function allows for specifying a subclass of ImportError to raise. (Contributed by Eric Snow in bpo-15767 [https://bugs.python.org/issue15767].)

The new PyErr_ResourceWarning() function can be used to generate a ResourceWarning providing the source of the resource allocation. (Contributed by Victor Stinner in bpo-26567 [https://bugs.python.org/issue26567].)

The new PyOS_FSPath() function returns the file system representation of a path-like object. (Contributed by Brett Cannon in bpo-27186 [https://bugs.python.org/issue27186].)

The PyUnicode_FSConverter() and PyUnicode_FSDecoder() functions will now accept path-like objects.

其他改进

When --version (short form: -V) is supplied twice, Python prints sys.version for detailed information.

$ ./python -VV
Python 3.6.0b4+ (3.6:223967b49e49+, Nov 21 2016, 20:55:04)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]

弃用

新关键字

async and await are not recommended to be used as variable, class, function or module names. Introduced by PEP 492 [https://www.python.org/dev/peps/pep-0492] in Python 3.5, they will become proper keywords in Python 3.7. Starting in Python 3.6, the use of async or await as names will generate a DeprecationWarning.

已弃用的 Python 行为

Raising the StopIteration exception inside a generator will now generate a DeprecationWarning, and will trigger a RuntimeError in Python 3.7. See PEP 479: Change StopIteration handling inside generators for details.

The __aiter__() method is now expected to return an asynchronous iterator directly instead of returning an awaitable as previously. Doing the former will trigger a DeprecationWarning. Backward compatibility will be removed in Python 3.7. (Contributed by Yury Selivanov in bpo-27243 [https://bugs.python.org/issue27243].)

A backslash-character pair that is not a valid escape sequence now generates a DeprecationWarning. Although this will eventually become a SyntaxError, that will not be for several Python releases. (Contributed by Emanuel Barry in bpo-27364 [https://bugs.python.org/issue27364].)

When performing a relative import, falling back on __name__ and __path__ from the calling module when __spec__ or __package__ are not defined now raises an ImportWarning. (Contributed by Rose Ames in bpo-25791 [https://bugs.python.org/issue25791].)

已弃用的 Python 模块、函数和方法

asynchat

The asynchat has been deprecated in favor of asyncio. (Contributed by Mariatta in bpo-25002 [https://bugs.python.org/issue25002].)

asyncore

The asyncore has been deprecated in favor of asyncio. (Contributed by Mariatta in bpo-25002 [https://bugs.python.org/issue25002].)

dbm

Unlike other dbm implementations, the dbm.dumb module creates databases with the 'rw' mode and allows modifying the database opened with the 'r' mode. This behavior is now deprecated and will be removed in 3.8. (Contributed by Serhiy Storchaka in bpo-21708 [https://bugs.python.org/issue21708].)

distutils

The undocumented extra_path argument to the Distribution constructor is now considered deprecated and will raise a warning if set. Support for this parameter will be removed in a future Python release. See bpo-27919 [https://bugs.python.org/issue27919] for details.

grp

The support of non-integer arguments in getgrgid() has been deprecated. (Contributed by Serhiy Storchaka in bpo-26129 [https://bugs.python.org/issue26129].)

importlib

The importlib.machinery.SourceFileLoader.load_module() and importlib.machinery.SourcelessFileLoader.load_module() methods are now deprecated. They were the only remaining implementations of importlib.abc.Loader.load_module() in importlib that had not been deprecated in previous versions of Python in favour of importlib.abc.Loader.exec_module().

The importlib.machinery.WindowsRegistryFinder class is now deprecated. As of 3.6.0, it is still added to sys.meta_path by default (on Windows), but this may change in future releases.

os

Undocumented support of general bytes-like objects as paths in os functions, compile() and similar functions is now deprecated. (Contributed by Serhiy Storchaka in bpo-25791 [https://bugs.python.org/issue25791] and bpo-26754 [https://bugs.python.org/issue26754].)

re

Support for inline flags (?letters) in the middle of the regular expression has been deprecated and will be removed in a future Python version. Flags at the start of a regular expression are still allowed. (Contributed by Serhiy Storchaka in bpo-22493 [https://bugs.python.org/issue22493].)

ssl

OpenSSL 0.9.8, 1.0.0 and 1.0.1 are deprecated and no longer supported. In the future the ssl module will require at least OpenSSL 1.0.2 or 1.1.0.

SSL-related arguments like certfile, keyfile and check_hostname in ftplib, http.client, imaplib, poplib, and smtplib have been deprecated in favor of context. (Contributed by Christian Heimes in bpo-28022 [https://bugs.python.org/issue28022].)

A couple of protocols and functions of the ssl module are now deprecated. Some features will no longer be available in future versions of OpenSSL. Other features are deprecated in favor of a different API. (Contributed by Christian Heimes in bpo-28022 [https://bugs.python.org/issue28022] and bpo-26470 [https://bugs.python.org/issue26470].)

tkinter

The tkinter.tix module is now deprecated. tkinter users should use tkinter.ttk instead.

venv

The pyvenv script has been deprecated in favour of python3 -m venv. This prevents confusion as to what Python interpreter pyvenv is connected to and thus what Python interpreter will be used by the virtual environment. (Contributed by Brett Cannon in bpo-25154 [https://bugs.python.org/issue25154].)

xml

As mitigation against DTD and external entity retrieval, the xml.dom.minidom and mod:xml.sax modules no longer process external entities by default. (Contributed by Christian Heimes in bpo-17239 [https://bugs.python.org/issue17239].)

已弃用的 C API 函数和类型

Undocumented functions PyUnicode_AsEncodedObject(), PyUnicode_AsDecodedObject(), PyUnicode_AsEncodedUnicode() and PyUnicode_AsDecodedUnicode() are deprecated now. Use the generic codec based API instead.

弃用的构建选项

The --with-system-ffi configure flag is now on by default on non-macOS UNIX platforms. It may be disabled by using --without-system-ffi, but using the flag is deprecated and will not be accepted in Python 3.7. macOS is unaffected by this change. Note that many OS distributors already use the --with-system-ffi flag when building their system Python.

移除

API 与特性的移除

Unknown escapes consisting of '\' and an ASCII letter in regular expressions will now cause an error. In replacement templates for re.sub() they are still allowed, but deprecated. The re.LOCALE flag can now only be used with binary patterns.

inspect.getmoduleinfo() was removed (was deprecated since CPython 3.3). inspect.getmodulename() should be used for obtaining the module name for a given path. (Contributed by Yury Selivanov in bpo-13248 [https://bugs.python.org/issue13248].)

traceback.Ignore class and traceback.usage, traceback.modname, traceback.fullmodname, traceback.find_lines_from_code, traceback.find_lines, traceback.find_strings, traceback.find_executable_lines methods were removed from the traceback module. They were undocumented methods deprecated since Python 3.2 and equivalent functionality is available from private methods.

The tk_menuBar() and tk_bindForTraversal() dummy methods in tkinter widget classes were removed (corresponding Tk commands were obsolete since Tk 4.0).

The open() method of the zipfile.ZipFile class no longer supports the 'U' mode (was deprecated since Python 3.4). Use io.TextIOWrapper for reading compressed text files in universal newlines mode.

The undocumented IN, CDROM, DLFCN, TYPES, CDIO, and STROPTS modules have been removed. They had been available in the platform specific Lib/plat-*/ directories, but were chronically out of date, inconsistently available across platforms, and unmaintained. The script that created these modules is still available in the source distribution at Tools/scripts/h2py.py [https://github.com/python/cpython/tree/3.6/Tools/scripts/h2py.py].

The deprecated asynchat.fifo class has been removed.

移植到Python 3.6

本节列出了先前描述的更改以及可能需要更改代码的其他错误修正.

 ‘python’ 命令行为的变化

The output of a special Python build with defined COUNT_ALLOCS, SHOW_ALLOC_COUNT or SHOW_TRACK_COUNT macros is now off by default. It can be re-enabled using the -X showalloccount option. It now outputs to stderr instead of stdout. (Contributed by Serhiy Storchaka in bpo-23034 [https://bugs.python.org/issue23034].)

更改的Python API

open() will no longer allow combining the 'U' mode flag with '+'. (Contributed by Jeff Balogh and John O’Connor in bpo-2091 [https://bugs.python.org/issue2091].)

sqlite3 no longer implicitly commits an open transaction before DDL statements.

On Linux, os.urandom() now blocks until the system urandom entropy pool is initialized to increase the security.

When importlib.abc.Loader.exec_module() is defined, importlib.abc.Loader.create_module() must also be defined.

PyErr_SetImportError() now sets TypeError when its msg argument is not set. Previously only NULL was returned.

The format of the co_lnotab attribute of code objects changed to support a negative line number delta. By default, Python does not emit bytecode with a negative line number delta. Functions using frame.f_lineno, PyFrame_GetLineNumber() or PyCode_Addr2Line() are not affected. Functions directly decoding co_lnotab should be updated to use a signed 8-bit integer type for the line number delta, but this is only required to support applications using a negative line number delta. See Objects/lnotab_notes.txt for the co_lnotab format and how to decode it, and see the PEP 511 [https://www.python.org/dev/peps/pep-0511] for the rationale.

The functions in the compileall module now return booleans instead of 1 or 0 to represent success or failure, respectively. Thanks to booleans being a subclass of integers, this should only be an issue if you7 were doing identity checks for 1 or 0. See bpo-25768 [https://bugs.python.org/issue25768].

Reading the port attribute of urllib.parse.urlsplit() and urlparse() results now raises ValueError for out-of-range values, rather than returning None. See bpo-20059 [https://bugs.python.org/issue20059].

The imp module now raises a DeprecationWarning instead of PendingDeprecationWarning.

The following modules have had missing APIs added to their __all__ attributes to match the documented APIs: calendar, cgi, csv, ElementTree, enum, fileinput, ftplib, logging, mailbox, mimetypes, optparse, plistlib, smtpd, subprocess, tarfile, threading and wave. This means they will export new symbols when import * is used. (Contributed by Joel Taddei and Jacek Kołodziej in bpo-23883 [https://bugs.python.org/issue23883].)

When performing a relative import, if __package__ does not compare equal to __spec__.parent then ImportWarning is raised. (Contributed by Brett Cannon in bpo-25791 [https://bugs.python.org/issue25791].)

When a relative import is performed and no parent package is known, then ImportError will be raised. Previously, SystemError could be raised. (Contributed by Brett Cannon in bpo-18018 [https://bugs.python.org/issue18018].)

Servers based on the socketserver module, including those defined in http.server, xmlrpc.server and wsgiref.simple_server, now only catch exceptions derived from Exception. Therefore if a request handler raises an exception like SystemExit or KeyboardInterrupt, handle_error() is no longer called, and the exception will stop a single-threaded server. (Contributed by Martin Panter in bpo-23430 [https://bugs.python.org/issue23430].)

spwd.getspnam() now raises a PermissionError instead of KeyError if the user doesn’t have privileges.

The socket.socket.close() method now raises an exception if an error (e.g. EBADF) was reported by the underlying system call. (Contributed by Martin Panter in bpo-26685 [https://bugs.python.org/issue26685].)

The decode_data argument for the smtpd.SMTPChannel and smtpd.SMTPServer constructors is now False by default. This means that the argument passed to process_message() is now a bytes object by default, and process_message() will be passed keyword arguments. Code that has already been updated in accordance with the deprecation warning generated by 3.5 will not be affected.

All optional arguments of the dump(), dumps(), load() and loads() functions and JSONEncoder and JSONDecoder class constructors in the json module are now keyword-only. (Contributed by Serhiy Storchaka in bpo-18726 [https://bugs.python.org/issue18726].)

type 的子类如果未重载 type.__new__，将不再能使用一个参数的形式来获取对象的类型。

As part of PEP 487 [https://www.python.org/dev/peps/pep-0487], the handling of keyword arguments passed to type (other than the metaclass hint, metaclass) is now consistently delegated to object.__init_subclass__(). This means that type.__new__() and type.__init__() both now accept arbitrary keyword arguments, but object.__init_subclass__() (which is called from type.__new__()) will reject them by default. Custom metaclasses accepting additional keyword arguments will need to adjust their calls to type.__new__() (whether direct or via super) accordingly.

In distutils.command.sdist.sdist, the default_format attribute has been removed and is no longer honored. Instead, the gzipped tarfile format is the default on all platforms and no platform-specific selection is made. In environments where distributions are built on Windows and zip distributions are required, configure the project with a setup.cfg file containing the following:

[sdist]
formats=zip

This behavior has also been backported to earlier Python versions by Setuptools 26.0.0.

In the urllib.request module and the http.client.HTTPConnection.request() method, if no Content-Length header field has been specified and the request body is a file object, it is now sent with HTTP 1.1 chunked encoding. If a file object has to be sent to a HTTP 1.0 server, the Content-Length value now has to be specified by the caller. (Contributed by Demian Brecht and Rolf Krahl with tweaks from Martin Panter in bpo-12319 [https://bugs.python.org/issue12319].)

The DictReader now returns rows of type OrderedDict. (Contributed by Steve Holden in bpo-27842 [https://bugs.python.org/issue27842].)

The crypt.METHOD_CRYPT will no longer be added to crypt.methods if unsupported by the platform. (Contributed by Victor Stinner in bpo-25287 [https://bugs.python.org/issue25287].)

The verbose and rename arguments for namedtuple() are now keyword-only. (Contributed by Raymond Hettinger in bpo-25628 [https://bugs.python.org/issue25628].)

On Linux, ctypes.util.find_library() now looks in LD_LIBRARY_PATH for shared libraries. (Contributed by Vinay Sajip in bpo-9998 [https://bugs.python.org/issue9998].)

The imaplib.IMAP4 class now handles flags containing the ']' character in messages sent from the server to improve real-world compatibility. (Contributed by Lita Cho in bpo-21815 [https://bugs.python.org/issue21815].)

The mmap.write() function now returns the number of bytes written like other write methods. (Contributed by Jakub Stasiak in bpo-26335 [https://bugs.python.org/issue26335].)

The pkgutil.iter_modules() and pkgutil.walk_packages() functions now return ModuleInfo named tuples. (Contributed by Ramchandra Apte in bpo-17211 [https://bugs.python.org/issue17211].)

re.sub() now raises an error for invalid numerical group references in replacement templates even if the pattern is not found in the string. The error message for invalid group references now includes the group index and the position of the reference. (Contributed by SilentGhost, Serhiy Storchaka in bpo-25953 [https://bugs.python.org/issue25953].)

zipfile.ZipFile will now raise NotImplementedError for unrecognized compression values. Previously a plain RuntimeError was raised. Additionally, calling ZipFile methods on a closed ZipFile or calling the write() method on a ZipFile created with mode 'r' will raise a ValueError. Previously, a RuntimeError was raised in those scenarios.

when custom metaclasses are combined with zero-argument super() or direct references from methods to the implicit __class__ closure variable, the implicit __classcell__ namespace entry must now be passed up to type.__new__ for initialisation. Failing to do so will result in a DeprecationWarning in Python 3.6 and a RuntimeError in Python 3.8.

With the introduction of ModuleNotFoundError, import system consumers may start expecting import system replacements to raise that more specific exception when appropriate, rather than the less-specific ImportError. To provide future compatibility with such consumers, implementors of alternative import systems that completely replace __import__() will need to update their implementations to raise the new subclass when a module can’t be found at all. Implementors of compliant plugins to the default import system shouldn’t need to make any changes, as the default import system will raise the new subclass when appropriate.

C API 中的改变

The PyMem_Malloc() allocator family now uses the pymalloc allocator rather than the system malloc(). Applications calling PyMem_Malloc() without holding the GIL can now crash. Set the PYTHONMALLOC environment variable to debug to validate the usage of memory allocators in your application. See bpo-26249 [https://bugs.python.org/issue26249].

Py_Exit() (and the main interpreter) now override the exit status with 120 if flushing buffered data failed. See bpo-5319 [https://bugs.python.org/issue5319].

CPython 字节码的改变

There have been several major changes to the bytecode in Python 3.6.

The Python interpreter now uses a 16-bit wordcode instead of bytecode. (Contributed by Demur Rumed with input and reviews from Serhiy Storchaka and Victor Stinner in bpo-26647 [https://bugs.python.org/issue26647] and bpo-28050 [https://bugs.python.org/issue28050].)

The new FORMAT_VALUE and BUILD_STRING opcodes as part of the formatted string literal implementation. (Contributed by Eric Smith in bpo-25483 [https://bugs.python.org/issue25483] and Serhiy Storchaka in bpo-27078 [https://bugs.python.org/issue27078].)

The new BUILD_CONST_KEY_MAP opcode to optimize the creation of dictionaries with constant keys. (Contributed by Serhiy Storchaka in bpo-27140 [https://bugs.python.org/issue27140].)

The function call opcodes have been heavily reworked for better performance and simpler implementation. The MAKE_FUNCTION, CALL_FUNCTION, CALL_FUNCTION_KW and BUILD_MAP_UNPACK_WITH_CALL opcodes have been modified, the new CALL_FUNCTION_EX and BUILD_TUPLE_UNPACK_WITH_CALL have been added, and CALL_FUNCTION_VAR, CALL_FUNCTION_VAR_KW and MAKE_CLOSURE opcodes have been removed. (Contributed by Demur Rumed in bpo-27095 [https://bugs.python.org/issue27095], and Serhiy Storchaka in bpo-27213 [https://bugs.python.org/issue27213], bpo-28257 [https://bugs.python.org/issue28257].)

The new SETUP_ANNOTATIONS and STORE_ANNOTATION opcodes have been added to support the new variable annotation syntax. (Contributed by Ivan Levkivskyi in bpo-27985 [https://bugs.python.org/issue27985].)

Python 3.6.2 中的重要变化

New make regen-all build target

To simplify cross-compilation, and to ensure that CPython can reliably be compiled without requiring an existing version of Python to already be available, the autotools-based build system no longer attempts to implicitly recompile generated files based on file modification times.

Instead, a new make regen-all command has been added to force regeneration of these files when desired (e.g. after an initial version of Python has already been built based on the pregenerated versions).

More selective regeneration targets are also defined - see Makefile.pre.in [https://github.com/python/cpython/tree/3.6/Makefile.pre.in] for details.

（由 Victor Stinner 在 bpo-23404 [https://bugs.python.org/issue23404] 中贡献。）

3.6.2 新版功能.

Removal of make touch build target

The make touch build target previously used to request implicit regeneration of generated files by updating their modification times has been removed.

It has been replaced by the new make regen-all target.

（由 Victor Stinner 在 bpo-23404 [https://bugs.python.org/issue23404] 中贡献。）

在 3.6.2 版更改.

Python 3.6.4 中的重要变化

The PyExc_RecursionErrorInst singleton that was part of the public API has been removed as its members being never cleared may cause a segfault during finalization of the interpreter. (Contributed by Xavier de Gaye in bpo-22898 [https://bugs.python.org/issue22898] and bpo-30697 [https://bugs.python.org/issue30697].)

Python 3.6.5 中的重要变化

The locale.localeconv() function now sets temporarily the LC_CTYPE locale to the LC_NUMERIC locale in some cases. (Contributed by Victor Stinner in bpo-31900 [https://bugs.python.org/issue31900].)

Python 3.6.7 中的重要变化

xml.dom.minidom and mod:xml.sax modules no longer process external entities by default. See also bpo-17239 [https://bugs.python.org/issue17239].

In 3.6.7 the tokenize module now implicitly emits a NEWLINE token when provided with input that does not have a trailing new line. This behavior now matches what the C tokenizer does internally. (Contributed by Ammar Askar in bpo-33899 [https://bugs.python.org/issue33899].)

Python 3.6.10 中的重要变化

出于重要的安全性考量，asyncio.loop.create_datagram_endpoint() 的 reuse_address 形参不再被支持。 这是由 UDP 中的套接字选项 SO_REUSEADDR 的行为导致的。 更多细节请参阅 loop.create_datagram_endpoint() 的文档。 （由 Kyle Stanley, Antoine Pitrou 和 Yury Selivanov 在 bpo-37228 [https://bugs.python.org/issue37228] 中贡献。。）

Notable changes in Python 3.6.13

早先的 Python 版本允许使用 ; 和 & 作为 urllib.parse.parse_qs() 和 urllib.parse.parse_qsl() 中 query 形参的分隔键。 出于安全考虑，也为了遵循更新的 W3C 推荐设置，这已被改为只允许单个分隔键，默认为 &。 这一改变还会影响 cgi.parse() 和 cgi.parse_multipart() 因为它们在内部使用了受影响的函数。 要了解更多细节，请查看它们各自的文档。 （由 Adam Goldschmidt, Senthil Kumaran 和 Ken Jin 在 bpo-42967 [https://bugs.python.org/issue42967] 中贡献。）

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

Python 3.5 有什么新变化

作者

Elvis Pranskevichus <elvis@magic.io>, Yury Selivanov <yury@magic.io>

This article explains the new features in Python 3.5, compared to 3.4. Python 3.5 was released on September 13, 2015.  See the changelog [https://docs.python.org/3.5/whatsnew/changelog.html] for a full list of changes.

参见

PEP 478 [https://www.python.org/dev/peps/pep-0478] - Python 3.5 发布计划

摘要 - 发布重点

新的语法特性：

PEP 492, 使用 async 和 await 语法实现协程。

PEP 465, 新的矩阵乘法运算符: a @ b.

PEP 448, additional unpacking generalizations.

新的库模块：

typing: PEP 484 – 类型提示.

zipapp: PEP 441 改进Python ZIP应用程序支持.

新的内置特性：

bytes % args, bytearray % args: PEP 461 – Adding % formatting to bytes and bytearray.

New bytes.hex(), bytearray.hex() and memoryview.hex() methods. (Contributed by Arnon Yaari in bpo-9951 [https://bugs.python.org/issue9951].)

memoryview now supports tuple indexing (including multi-dimensional). (Contributed by Antoine Pitrou in bpo-23632 [https://bugs.python.org/issue23632].)

Generators have a new gi_yieldfrom attribute, which returns the object being iterated by yield from expressions. (Contributed by Benno Leslie and Yury Selivanov in bpo-24450 [https://bugs.python.org/issue24450].)

A new RecursionError exception is now raised when maximum recursion depth is reached. (Contributed by Georg Brandl in bpo-19235 [https://bugs.python.org/issue19235].)

CPython 实现的改进：

When the LC_TYPE locale is the POSIX locale (C locale), sys.stdin and sys.stdout now use the surrogateescape error handler, instead of the strict error handler. (Contributed by Victor Stinner in bpo-19977 [https://bugs.python.org/issue19977].)

.pyo files are no longer used and have been replaced by a more flexible scheme that includes the optimization level explicitly in .pyc name. (See PEP 488 overview.)

Builtin and extension modules are now initialized in a multi-phase process, which is similar to how Python modules are loaded. (See PEP 489 overview.)

标准库中的重大改进：

collections.OrderedDict is now implemented in C, which makes it 4 to 100 times faster.

The ssl module gained support for Memory BIO, which decouples SSL protocol handling from network IO.

The new os.scandir() function provides a better and significantly faster way of directory traversal.

functools.lru_cache() has been mostly reimplemented in C, yielding much better performance.

The new subprocess.run() function provides a streamlined way to run subprocesses.

The traceback module has been significantly enhanced for improved performance and developer convenience.

安全改进：

SSLv3 is now disabled throughout the standard library. It can still be enabled by instantiating a ssl.SSLContext manually. (See bpo-22638 [https://bugs.python.org/issue22638] for more details; this change was backported to CPython 3.4 and 2.7.)

HTTP cookie parsing is now stricter, in order to protect against potential injection attacks. (Contributed by Antoine Pitrou in bpo-22796 [https://bugs.python.org/issue22796].)

Windows改进：

A new installer for Windows has replaced the old MSI. See 在Windows上使用 Python for more information.

Windows builds now use Microsoft Visual C++ 14.0, and extension modules should use the same.

Please read on for a comprehensive list of user-facing changes, including many other smaller improvements, CPython optimizations, deprecations, and potential porting issues.

新的特性

PEP 492 - 使用 async 和 await 语法实现协程

PEP 492 [https://www.python.org/dev/peps/pep-0492] greatly improves support for asynchronous programming in Python by adding awaitable objects, coroutine functions, asynchronous iteration, and asynchronous context managers.

Coroutine functions are declared using the new async def syntax:

>>> async def coro():
...     return 'spam'

Inside a coroutine function, the new await expression can be used to suspend coroutine execution until the result is available. Any object can be awaited, as long as it implements the awaitable protocol by defining the __await__() method.

PEP 492 also adds async for statement for convenient iteration over asynchronous iterables.

An example of a rudimentary HTTP client written using the new syntax:

import asyncio

async def http_get(domain):
    reader, writer = await asyncio.open_connection(domain, 80)

    writer.write(b'\r\n'.join([
        b'GET / HTTP/1.1',
        b'Host: %b' % domain.encode('latin-1'),
        b'Connection: close',
        b'', b''
    ]))

    async for line in reader:
        print('>>>', line)

    writer.close()

loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(http_get('example.com'))
finally:
    loop.close()

Similarly to asynchronous iteration, there is a new syntax for asynchronous context managers. The following script:

import asyncio

async def coro(name, lock):
    print('coro {}: waiting for lock'.format(name))
    async with lock:
        print('coro {}: holding the lock'.format(name))
        await asyncio.sleep(1)
        print('coro {}: releasing the lock'.format(name))

loop = asyncio.get_event_loop()
lock = asyncio.Lock()
coros = asyncio.gather(coro(1, lock), coro(2, lock))
try:
    loop.run_until_complete(coros)
finally:
    loop.close()

将输出：

coro 2: waiting for lock
coro 2: holding the lock
coro 1: waiting for lock
coro 2: releasing the lock
coro 1: holding the lock
coro 1: releasing the lock

Note that both async for and async with can only be used inside a coroutine function declared with async def.

Coroutine functions are intended to be run inside a compatible event loop, such as the asyncio loop.

注解

在 3.5.2 版更改: Starting with CPython 3.5.2, __aiter__ can directly return asynchronous iterators. Returning an awaitable object will result in a PendingDeprecationWarning.

See more details in the 异步迭代器 documentation section.

参见

PEP 492 [https://www.python.org/dev/peps/pep-0492] – 使用 async 和 await 语法实现协程

PEP 由 Yury Selivanov 撰写并实现

PEP 465 - 用于矩阵乘法的专用中缀运算符

PEP 465 [https://www.python.org/dev/peps/pep-0465] adds the @ infix operator for matrix multiplication. Currently, no builtin Python types implement the new operator, however, it can be implemented by defining __matmul__(), __rmatmul__(), and __imatmul__() for regular, reflected, and in-place matrix multiplication. The semantics of these methods is similar to that of methods defining other infix arithmetic operators.

矩阵乘法在数学，科学，工程学的许多领域中是一种常见的操作，使用 @ 运算符可以编写更简洁的代码：

S = (H @ beta - r).T @ inv(H @ V @ H.T) @ (H @ beta - r)

代替：

S = dot((dot(H, beta) - r).T,
        dot(inv(dot(dot(H, V), H.T)), dot(H, beta) - r))

NumPy 1.10 支持新的运算符：

>>> import numpy

>>> x = numpy.ones(3)
>>> x
array([ 1., 1., 1.])

>>> m = numpy.eye(3)
>>> m
array([[ 1., 0., 0.],
       [ 0., 1., 0.],
       [ 0., 0., 1.]])

>>> x @ m
array([ 1., 1., 1.])

参见

PEP 465 [https://www.python.org/dev/peps/pep-0465] – 用于矩阵乘法的专用中缀运算符

PEP 由 Nathaniel J. Smith 撰写，由 Benjamin Peterson 实现。

PEP 448 - Additional Unpacking Generalizations

PEP 448 [https://www.python.org/dev/peps/pep-0448] extends the allowed uses of the * iterable unpacking operator and ** dictionary unpacking operator. It is now possible to use an arbitrary number of unpackings in function calls:

>>> print(*[1], *[2], 3, *[4, 5])
1 2 3 4 5

>>> def fn(a, b, c, d):
...     print(a, b, c, d)
...

>>> fn(**{'a': 1, 'c': 3}, **{'b': 2, 'd': 4})
1 2 3 4

Similarly, tuple, list, set, and dictionary displays allow multiple unpackings (see 表达式列表 and 字典显示):

>>> *range(4), 4
(0, 1, 2, 3, 4)

>>> [*range(4), 4]
[0, 1, 2, 3, 4]

>>> {*range(4), 4, *(5, 6, 7)}
{0, 1, 2, 3, 4, 5, 6, 7}

>>> {'x': 1, **{'y': 2}}
{'x': 1, 'y': 2}

参见

PEP 448 [https://www.python.org/dev/peps/pep-0448] – Additional Unpacking Generalizations

PEP 由 Joshua Landau 撰写 ，由 Neil Girdhar，Thomas Wouters 和 Joshua Landau 实现。

PEP 461 - percent formatting support for bytes and bytearray

PEP 461 [https://www.python.org/dev/peps/pep-0461] adds support for the % interpolation operator to bytes and bytearray.

While interpolation is usually thought of as a string operation, there are cases where interpolation on bytes or bytearrays makes sense, and the work needed to make up for this missing functionality detracts from the overall readability of the code. This issue is particularly important when dealing with wire format protocols, which are often a mixture of binary and ASCII compatible text.

示例:

>>> b'Hello %b!' % b'World'
b'Hello World!'

>>> b'x=%i y=%f' % (1, 2.5)
b'x=1 y=2.500000'

Unicode is not allowed for %b, but it is accepted by %a (equivalent of repr(obj).encode('ascii', 'backslashreplace')):

>>> b'Hello %b!' % 'World'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: %b requires bytes, or an object that implements __bytes__, not 'str'

>>> b'price: %a' % '10€'
b"price: '10\\u20ac'"

Note that %s and %r conversion types, although supported, should only be used in codebases that need compatibility with Python 2.

参见

PEP 461 [https://www.python.org/dev/peps/pep-0461] – Adding % formatting to bytes and bytearray

PEP 由 Ethan Furman 撰写 ，由 Neil Schemenauer 和 Ethan Furman 实现。

PEP 484 - 类型提示

Function annotation syntax has been a Python feature since version 3.0 (PEP 3107 [https://www.python.org/dev/peps/pep-3107]), however the semantics of annotations has been left undefined.

Experience has shown that the majority of function annotation uses were to provide type hints to function parameters and return values. It became evident that it would be beneficial for Python users, if the standard library included the base definitions and tools for type annotations.

PEP 484 [https://www.python.org/dev/peps/pep-0484] introduces a provisional module to provide these standard definitions and tools, along with some conventions for situations where annotations are not available.

For example, here is a simple function whose argument and return type are declared in the annotations:

def greeting(name: str) -> str:
    return 'Hello ' + name

While these annotations are available at runtime through the usual __annotations__ attribute, no automatic type checking happens at runtime. Instead, it is assumed that a separate off-line type checker (e.g. mypy [http://mypy-lang.org]) will be used for on-demand source code analysis.

The type system supports unions, generic types, and a special type named Any which is consistent with (i.e. assignable to and from) all types.

参见

typing 模块文档

PEP 484 [https://www.python.org/dev/peps/pep-0484] – 类型提示

PEP 由 Guido van Rossum，Jukka Lehtosalo 和 Łukasz Langa 撰写，由 Guido van Rossum 实现。

PEP 483 [https://www.python.org/dev/peps/pep-0483] – 类型提示理论

PEP 由 Yury Selivanov 撰写

PEP 471 - os.scandir() function – a better and faster directory iterator

PEP 471 [https://www.python.org/dev/peps/pep-0471] adds a new directory iteration function, os.scandir(), to the standard library. Additionally, os.walk() is now implemented using scandir, which makes it 3 to 5 times faster on POSIX systems and 7 to 20 times faster on Windows systems. This is largely achieved by greatly reducing the number of calls to os.stat() required to walk a directory tree.

Additionally, scandir returns an iterator, as opposed to returning a list of file names, which improves memory efficiency when iterating over very large directories.

The following example shows a simple use of os.scandir() to display all the files (excluding directories) in the given path that don’t start with '.'. The entry.is_file() call will generally not make an additional system call:

for entry in os.scandir(path):
    if not entry.name.startswith('.') and entry.is_file():
        print(entry.name)

参见

PEP 471 [https://www.python.org/dev/peps/pep-0471] – os.scandir() function – a better and faster directory iterator

PEP 由 Ben Hoyt 在 Victor Stinner 的帮助下撰写并实现

PEP 475: Retry system calls failing with EINTR

An errno.EINTR error code is returned whenever a system call, that is waiting for I/O, is interrupted by a signal. Previously, Python would raise InterruptedError in such cases. This meant that, when writing a Python application, the developer had two choices:

Ignore the InterruptedError.

Handle the InterruptedError and attempt to restart the interrupted system call at every call site.

The first option makes an application fail intermittently. The second option adds a large amount of boilerplate that makes the code nearly unreadable. Compare:

print("Hello World")

和:

while True:
    try:
        print("Hello World")
        break
    except InterruptedError:
        continue

PEP 475 [https://www.python.org/dev/peps/pep-0475] implements automatic retry of system calls on EINTR. This removes the burden of dealing with EINTR or InterruptedError in user code in most situations and makes Python programs, including the standard library, more robust. Note that the system call is only retried if the signal handler does not raise an exception.

Below is a list of functions which are now retried when interrupted by a signal:

open() 和 io.open();

faulthandler 模块的功能

os functions: fchdir(), fchmod(), fchown(), fdatasync(), fstat(), fstatvfs(), fsync(), ftruncate(), mkfifo(), mknod(), open(), posix_fadvise(), posix_fallocate(), pread(), pwrite(), read(), readv(), sendfile(), wait3(), wait4(), wait(), waitid(), waitpid(), write(), writev();

特例: os.close() 和 os.dup2() 现在忽略 EINTR 错误; 不重试系统调用（请参阅PEP了解基本原理）

select 函数: devpoll.poll(), epoll.poll(), kqueue.control(), poll.poll(), select();

socket 类的方法: accept(), connect() (except for non-blocking sockets), recv(), recvfrom(), recvmsg(), send(), sendall(), sendmsg(), sendto();

signal.sigtimedwait() 和 signal.sigwaitinfo();

time.sleep().

参见

PEP 475 [https://www.python.org/dev/peps/pep-0475] – Retry system calls failing with EINTR

PEP and implementation written by Charles-François Natali and Victor Stinner, with the help of Antoine Pitrou (the French connection).

PEP 479: Change StopIteration handling inside generators

The interaction of generators and StopIteration in Python 3.4 and earlier was sometimes surprising, and could conceal obscure bugs. Previously, StopIteration raised accidentally inside a generator function was interpreted as the end of the iteration by the loop construct driving the generator.

PEP 479 [https://www.python.org/dev/peps/pep-0479] changes the behavior of generators: when a StopIteration exception is raised inside a generator, it is replaced with a RuntimeError before it exits the generator frame. The main goal of this change is to ease debugging in the situation where an unguarded next() call raises StopIteration and causes the iteration controlled by the generator to terminate silently. This is particularly pernicious in combination with the yield from construct.

This is a backwards incompatible change, so to enable the new behavior, a __future__ import is necessary:

>>> from __future__ import generator_stop

>>> def gen():
...     next(iter([]))
...     yield
...
>>> next(gen())
Traceback (most recent call last):
  File "<stdin>", line 2, in gen
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: generator raised StopIteration

Without a __future__ import, a PendingDeprecationWarning will be raised whenever a StopIteration exception is raised inside a generator.

参见

PEP 479 [https://www.python.org/dev/peps/pep-0479] – Change StopIteration handling inside generators

PEP 由 Chris Angelico 和 Guido van Rossum 撰写，由 Chris Angelico，Yury Selivanov 和 Nick Coghlan 实现。

PEP 485: A function for testing approximate equality

PEP 485 [https://www.python.org/dev/peps/pep-0485] adds the math.isclose() and cmath.isclose() functions which tell whether two values are approximately equal or “close” to each other. Whether or not two values are considered close is determined according to given absolute and relative tolerances. Relative tolerance is the maximum allowed difference between isclose arguments, relative to the larger absolute value:

>>> import math
>>> a = 5.0
>>> b = 4.99998
>>> math.isclose(a, b, rel_tol=1e-5)
True
>>> math.isclose(a, b, rel_tol=1e-6)
False

It is also possible to compare two values using absolute tolerance, which must be a non-negative value:

>>> import math
>>> a = 5.0
>>> b = 4.99998
>>> math.isclose(a, b, abs_tol=0.00003)
True
>>> math.isclose(a, b, abs_tol=0.00001)
False

参见

PEP 485 [https://www.python.org/dev/peps/pep-0485] —— 用于测试近似相等的函数

PEP 由 Christopher Barker 撰写，由 Chris Barker 和 Tal Einat 实现。

PEP 486: Make the Python Launcher aware of virtual environments

PEP 486 [https://www.python.org/dev/peps/pep-0486] makes the Windows launcher (see PEP 397 [https://www.python.org/dev/peps/pep-0397]) aware of an active virtual environment. When the default interpreter would be used and the VIRTUAL_ENV environment variable is set, the interpreter in the virtual environment will be used.

参见

PEP 486 [https://www.python.org/dev/peps/pep-0486] – Make the Python Launcher aware of virtual environments

PEP 由 Paul Moore 撰写并实现

PEP 488: Elimination of PYO files

PEP 488 [https://www.python.org/dev/peps/pep-0488] does away with the concept of .pyo files. This means that .pyc files represent both unoptimized and optimized bytecode. To prevent the need to constantly regenerate bytecode files, .pyc files now have an optional opt- tag in their name when the bytecode is optimized. This has the side-effect of no more bytecode file name clashes when running under either -O or -OO. Consequently, bytecode files generated from -O, and -OO may now exist simultaneously. importlib.util.cache_from_source() has an updated API to help with this change.

参见

PEP 488 [https://www.python.org/dev/peps/pep-0488] – Elimination of PYO files

PEP 由 Brett Cannon 撰写并实现。

PEP 489: Multi-phase extension module initialization

PEP 489 [https://www.python.org/dev/peps/pep-0489] updates extension module initialization to take advantage of the two step module loading mechanism introduced by PEP 451 [https://www.python.org/dev/peps/pep-0451] in Python 3.4.

This change brings the import semantics of extension modules that opt-in to using the new mechanism much closer to those of Python source and bytecode modules, including the ability to use any valid identifier as a module name, rather than being restricted to ASCII.

参见

PEP 489 [https://www.python.org/dev/peps/pep-0489] – Multi-phase extension module initialization

PEP 由 Petr Viktorin , Stefan Behnel 和 Nick Coghlan 撰写，由 Petr Viktorin 实现。

其他语言特性修改

对Python 语言核心进行的小改动：

Added the "namereplace" error handlers. The "backslashreplace" error handlers now work with decoding and translating. (Contributed by Serhiy Storchaka in bpo-19676 [https://bugs.python.org/issue19676] and bpo-22286 [https://bugs.python.org/issue22286].)

The -b option now affects comparisons of bytes with int. (Contributed by Serhiy Storchaka in bpo-23681 [https://bugs.python.org/issue23681].)

New Kazakh kz1048 and Tajik koi8_t codecs. (Contributed by Serhiy Storchaka in bpo-22682 [https://bugs.python.org/issue22682] and bpo-22681 [https://bugs.python.org/issue22681].)

Property docstrings are now writable. This is especially useful for collections.namedtuple() docstrings. (Contributed by Berker Peksag in bpo-24064 [https://bugs.python.org/issue24064].)

Circular imports involving relative imports are now supported. (Contributed by Brett Cannon and Antoine Pitrou in bpo-17636 [https://bugs.python.org/issue17636].)

新增模块

typing

The new typing provisional module provides standard definitions and tools for function type annotations. See Type Hints for more information.

zipapp

The new zipapp module (specified in PEP 441 [https://www.python.org/dev/peps/pep-0441]) provides an API and command line tool for creating executable Python Zip Applications, which were introduced in Python 2.6 in bpo-1739468 [https://bugs.python.org/issue1739468], but which were not well publicized, either at the time or since.

With the new module, bundling your application is as simple as putting all the files, including a __main__.py file, into a directory myapp and running:

$ python -m zipapp myapp
$ python myapp.pyz

The module implementation has been contributed by Paul Moore in bpo-23491 [https://bugs.python.org/issue23491].

参见

PEP 441 [https://www.python.org/dev/peps/pep-0441] – Improving Python ZIP Application Support

改进的模块

argparse

The ArgumentParser class now allows disabling abbreviated usage of long options by setting allow_abbrev to False. (Contributed by Jonathan Paugh, Steven Bethard, paul j3 and Daniel Eriksson in bpo-14910 [https://bugs.python.org/issue14910].)

asyncio

Since the asyncio module is provisional, all changes introduced in Python 3.5 have also been backported to Python 3.4.x.

Notable changes in the asyncio module since Python 3.4.0:

New debugging APIs: loop.set_debug() and loop.get_debug() methods. (Contributed by Victor Stinner.)

The proactor event loop now supports SSL. (Contributed by Antoine Pitrou and Victor Stinner in bpo-22560 [https://bugs.python.org/issue22560].)

A new loop.is_closed() method to check if the event loop is closed. (Contributed by Victor Stinner in bpo-21326 [https://bugs.python.org/issue21326].)

A new loop.create_task() to conveniently create and schedule a new Task for a coroutine. The create_task method is also used by all asyncio functions that wrap coroutines into tasks, such as asyncio.wait(), asyncio.gather(), etc. (Contributed by Victor Stinner.)

A new transport.get_write_buffer_limits() method to inquire for high- and low- water limits of the flow control. (Contributed by Victor Stinner.)

The async() function is deprecated in favor of ensure_future(). (Contributed by Yury Selivanov.)

New loop.set_task_factory() and loop.get_task_factory() methods to customize the task factory that loop.create_task() method uses. (Contributed by Yury Selivanov.)

New Queue.join() and Queue.task_done() queue methods. (Contributed by Victor Stinner.)

The JoinableQueue class was removed, in favor of the asyncio.Queue class. (Contributed by Victor Stinner.)

3.5.1 中的更新：

The ensure_future() function and all functions that use it, such as loop.run_until_complete(), now accept all kinds of awaitable objects. (Contributed by Yury Selivanov.)

New run_coroutine_threadsafe() function to submit coroutines to event loops from other threads. (Contributed by Vincent Michel.)

New Transport.is_closing() method to check if the transport is closing or closed. (Contributed by Yury Selivanov.)

The loop.create_server() method can now accept a list of hosts. (Contributed by Yann Sionneau.)

3.5.2 中的更新：

New loop.create_future() method to create Future objects. This allows alternative event loop implementations, such as uvloop [https://github.com/MagicStack/uvloop], to provide a faster asyncio.Future implementation. (Contributed by Yury Selivanov.)

New loop.get_exception_handler() method to get the current exception handler. (Contributed by Yury Selivanov.)

New StreamReader.readuntil() method to read data from the stream until a separator bytes sequence appears. (Contributed by Mark Korenberg.)

The loop.create_connection() and loop.create_server() methods are optimized to avoid calling the system getaddrinfo function if the address is already resolved. (Contributed by A. Jesse Jiryu Davis.)

The loop.sock_connect(sock, address) no longer requires the address to be resolved prior to the call. (Contributed by A. Jesse Jiryu Davis.)

bz2

The BZ2Decompressor.decompress method now accepts an optional max_length argument to limit the maximum size of decompressed data. (Contributed by Nikolaus Rath in bpo-15955 [https://bugs.python.org/issue15955].)

cgi

The FieldStorage class now supports the context manager protocol. (Contributed by Berker Peksag in bpo-20289 [https://bugs.python.org/issue20289].)

cmath

A new function isclose() provides a way to test for approximate equality. (Contributed by Chris Barker and Tal Einat in bpo-24270 [https://bugs.python.org/issue24270].)

code

The InteractiveInterpreter.showtraceback() method now prints the full chained traceback, just like the interactive interpreter. (Contributed by Claudiu Popa in bpo-17442 [https://bugs.python.org/issue17442].)

collections

The OrderedDict class is now implemented in C, which makes it 4 to 100 times faster. (Contributed by Eric Snow in bpo-16991 [https://bugs.python.org/issue16991].)

OrderedDict.items(), OrderedDict.keys(), OrderedDict.values() views now support reversed() iteration. (Contributed by Serhiy Storchaka in bpo-19505 [https://bugs.python.org/issue19505].)

The deque class now defines index(), insert(), and copy(), and supports the + and * operators. This allows deques to be recognized as a MutableSequence and improves their substitutability for lists. (Contributed by Raymond Hettinger in bpo-23704 [https://bugs.python.org/issue23704].)

Docstrings produced by namedtuple() can now be updated:

Point = namedtuple('Point', ['x', 'y'])
Point.__doc__ += ': Cartesian coodinate'
Point.x.__doc__ = 'abscissa'
Point.y.__doc__ = 'ordinate'

（由 Berker Peksag 在 bpo-24064 [https://bugs.python.org/issue24064] 中贡献。）

The UserString class now implements the __getnewargs__(), __rmod__(), casefold(), format_map(), isprintable(), and maketrans() methods to match the corresponding methods of str. (Contributed by Joe Jevnik in bpo-22189 [https://bugs.python.org/issue22189].)

collections.abc

The Sequence.index() method now accepts start and stop arguments to match the corresponding methods of tuple, list, etc. (Contributed by Devin Jeanpierre in bpo-23086 [https://bugs.python.org/issue23086].)

A new Generator abstract base class. (Contributed by Stefan Behnel in bpo-24018 [https://bugs.python.org/issue24018].)

New Awaitable, Coroutine, AsyncIterator, and AsyncIterable abstract base classes. (Contributed by Yury Selivanov in bpo-24184 [https://bugs.python.org/issue24184].)

For earlier Python versions, a backport of the new ABCs is available in an external PyPI package [https://pypi.org/project/backports_abc].

compileall

A new compileall option, -j N, allows running N workers simultaneously to perform parallel bytecode compilation. The compile_dir() function has a corresponding workers parameter. (Contributed by Claudiu Popa in bpo-16104 [https://bugs.python.org/issue16104].)

Another new option, -r, allows controlling the maximum recursion level for subdirectories. (Contributed by Claudiu Popa in bpo-19628 [https://bugs.python.org/issue19628].)

The -q command line option can now be specified more than once, in which case all output, including errors, will be suppressed. The corresponding quiet parameter in compile_dir(), compile_file(), and compile_path() can now accept an integer value indicating the level of output suppression. (Contributed by Thomas Kluyver in bpo-21338 [https://bugs.python.org/issue21338].)

concurrent.futures

The Executor.map() method now accepts a chunksize argument to allow batching of tasks to improve performance when ProcessPoolExecutor() is used. (Contributed by Dan O’Reilly in bpo-11271 [https://bugs.python.org/issue11271].)

The number of workers in the ThreadPoolExecutor constructor is optional now. The default value is 5 times the number of CPUs. (Contributed by Claudiu Popa in bpo-21527 [https://bugs.python.org/issue21527].)

configparser

configparser now provides a way to customize the conversion of values by specifying a dictionary of converters in the ConfigParser constructor, or by defining them as methods in ConfigParser subclasses. Converters defined in a parser instance are inherited by its section proxies.

示例:

>>> import configparser
>>> conv = {}
>>> conv['list'] = lambda v: [e.strip() for e in v.split() if e.strip()]
>>> cfg = configparser.ConfigParser(converters=conv)
>>> cfg.read_string("""
... [s]
... list = a b c d e f g
... """)
>>> cfg.get('s', 'list')
'a b c d e f g'
>>> cfg.getlist('s', 'list')
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> section = cfg['s']
>>> section.getlist('list')
['a', 'b', 'c', 'd', 'e', 'f', 'g']

（由 Łukasz Langa 在 bpo-18159 [https://bugs.python.org/issue18159] 中贡献。）

contextlib

The new redirect_stderr() context manager (similar to redirect_stdout()) makes it easier for utility scripts to handle inflexible APIs that write their output to sys.stderr and don’t provide any options to redirect it:

>>> import contextlib, io, logging
>>> f = io.StringIO()
>>> with contextlib.redirect_stderr(f):
...     logging.warning('warning')
...
>>> f.getvalue()
'WARNING:root:warning\n'

（由 Berker Peksag 在 bpo-22389 [https://bugs.python.org/issue22389] 中贡献。）

csv

The writerow() method now supports arbitrary iterables, not just sequences. (Contributed by Serhiy Storchaka in bpo-23171 [https://bugs.python.org/issue23171].)

curses

The new update_lines_cols() function updates the LINES and COLS environment variables. This is useful for detecting manual screen resizing. (Contributed by Arnon Yaari in bpo-4254 [https://bugs.python.org/issue4254].)

dbm

dumb.open always creates a new database when the flag has the value "n". (Contributed by Claudiu Popa in bpo-18039 [https://bugs.python.org/issue18039].)

difflib

The charset of HTML documents generated by HtmlDiff.make_file() can now be customized by using a new charset keyword-only argument. The default charset of HTML document changed from "ISO-8859-1" to "utf-8". (Contributed by Berker Peksag in bpo-2052 [https://bugs.python.org/issue2052].)

The diff_bytes() function can now compare lists of byte strings. This fixes a regression from Python 2. (Contributed by Terry J. Reedy and Greg Ward in bpo-17445 [https://bugs.python.org/issue17445].)

distutils

Both the build and build_ext commands now accept a -j option to enable parallel building of extension modules. (Contributed by Antoine Pitrou in bpo-5309 [https://bugs.python.org/issue5309].)

The distutils module now supports xz compression, and can be enabled by passing xztar as an argument to bdist --format. (Contributed by Serhiy Storchaka in bpo-16314 [https://bugs.python.org/issue16314].)

doctest

The DocTestSuite() function returns an empty unittest.TestSuite if module contains no docstrings, instead of raising ValueError. (Contributed by Glenn Jones in bpo-15916 [https://bugs.python.org/issue15916].)

email

A new policy option Policy.mangle_from_ controls whether or not lines that start with "From " in email bodies are prefixed with a ">" character by generators. The default is True for compat32 and False for all other policies. (Contributed by Milan Oberkirch in bpo-20098 [https://bugs.python.org/issue20098].)

A new Message.get_content_disposition() method provides easy access to a canonical value for the Content-Disposition header. (Contributed by Abhilash Raj in bpo-21083 [https://bugs.python.org/issue21083].)

A new policy option EmailPolicy.utf8 can be set to True to encode email headers using the UTF-8 charset instead of using encoded words. This allows Messages to be formatted according to RFC 6532 [https://tools.ietf.org/html/rfc6532.html] and used with an SMTP server that supports the RFC 6531 [https://tools.ietf.org/html/rfc6531.html] SMTPUTF8 extension. (Contributed by R. David Murray in bpo-24211 [https://bugs.python.org/issue24211].)

The mime.text.MIMEText constructor now accepts a charset.Charset instance. (Contributed by Claude Paroz and Berker Peksag in bpo-16324 [https://bugs.python.org/issue16324].)

enum

The Enum callable has a new parameter start to specify the initial number of enum values if only names are provided:

>>> Animal = enum.Enum('Animal', 'cat dog', start=10)
>>> Animal.cat
<Animal.cat: 10>
>>> Animal.dog
<Animal.dog: 11>

（由 Ethan Furman 在 bpo-21706 [https://bugs.python.org/issue21706] 中贡献。）

faulthandler

The enable(), register(), dump_traceback() and dump_traceback_later() functions now accept file descriptors in addition to file-like objects. (Contributed by Wei Wu in bpo-23566 [https://bugs.python.org/issue23566].)

functools

Most of the lru_cache() machinery is now implemented in C, making it significantly faster. (Contributed by Matt Joiner, Alexey Kachayev, and Serhiy Storchaka in bpo-14373 [https://bugs.python.org/issue14373].)

glob

The iglob() and glob() functions now support recursive search in subdirectories, using the "**" pattern. (Contributed by Serhiy Storchaka in bpo-13968 [https://bugs.python.org/issue13968].)

gzip

The mode argument of the GzipFile constructor now accepts "x" to request exclusive creation. (Contributed by Tim Heaney in bpo-19222 [https://bugs.python.org/issue19222].)

heapq

Element comparison in merge() can now be customized by passing a key function in a new optional key keyword argument, and a new optional reverse keyword argument can be used to reverse element comparison:

>>> import heapq
>>> a = ['9', '777', '55555']
>>> b = ['88', '6666']
>>> list(heapq.merge(a, b, key=len))
['9', '88', '777', '6666', '55555']
>>> list(heapq.merge(reversed(a), reversed(b), key=len, reverse=True))
['55555', '6666', '777', '88', '9']

（由 Raymond Hettinger 在 bpo-13742 [https://bugs.python.org/issue13742] 中贡献。）

http

A new HTTPStatus enum that defines a set of HTTP status codes, reason phrases and long descriptions written in English. (Contributed by Demian Brecht in bpo-21793 [https://bugs.python.org/issue21793].)

http.client

HTTPConnection.getresponse() now raises a RemoteDisconnected exception when a remote server connection is closed unexpectedly. Additionally, if a ConnectionError (of which RemoteDisconnected is a subclass) is raised, the client socket is now closed automatically, and will reconnect on the next request:

import http.client
conn = http.client.HTTPConnection('www.python.org')
for retries in range(3):
    try:
        conn.request('GET', '/')
        resp = conn.getresponse()
    except http.client.RemoteDisconnected:
        pass

（由 Martin Panter 在 bpo-3566 [https://bugs.python.org/issue3566] 中贡献。）

idlelib 与 IDLE

Since idlelib implements the IDLE shell and editor and is not intended for import by other programs, it gets improvements with every release. See Lib/idlelib/NEWS.txt for a cumulative list of changes since 3.4.0, as well as changes made in future 3.5.x releases. This file is also available from the IDLE Help ‣ About IDLE dialog.

imaplib

The IMAP4 class now supports the context manager protocol. When used in a with statement, the IMAP4 LOGOUT command will be called automatically at the end of the block. (Contributed by Tarek Ziadé and Serhiy Storchaka in bpo-4972 [https://bugs.python.org/issue4972].)

The imaplib module now supports RFC 5161 [https://tools.ietf.org/html/rfc5161.html] (ENABLE Extension) and RFC 6855 [https://tools.ietf.org/html/rfc6855.html] (UTF-8 Support) via the IMAP4.enable() method. A new IMAP4.utf8_enabled attribute tracks whether or not RFC 6855 [https://tools.ietf.org/html/rfc6855.html] support is enabled. (Contributed by Milan Oberkirch, R. David Murray, and Maciej Szulik in bpo-21800 [https://bugs.python.org/issue21800].)

The imaplib module now automatically encodes non-ASCII string usernames and passwords using UTF-8, as recommended by the RFCs. (Contributed by Milan Oberkirch in bpo-21800 [https://bugs.python.org/issue21800].)

imghdr

The what() function now recognizes the OpenEXR [http://www.openexr.com] format (contributed by Martin Vignali and Claudiu Popa in bpo-20295 [https://bugs.python.org/issue20295]), and the WebP [https://en.wikipedia.org/wiki/WebP] format (contributed by Fabrice Aneche and Claudiu Popa in bpo-20197 [https://bugs.python.org/issue20197].)

importlib

The util.LazyLoader class allows for lazy loading of modules in applications where startup time is important. (Contributed by Brett Cannon in bpo-17621 [https://bugs.python.org/issue17621].)

The abc.InspectLoader.source_to_code() method is now a static method. This makes it easier to initialize a module object with code compiled from a string by running exec(code, module.__dict__). (Contributed by Brett Cannon in bpo-21156 [https://bugs.python.org/issue21156].)

The new util.module_from_spec() function is now the preferred way to create a new module. As opposed to creating a types.ModuleType instance directly, this new function will set the various import-controlled attributes based on the passed-in spec object. (Contributed by Brett Cannon in bpo-20383 [https://bugs.python.org/issue20383].)

inspect

Both the Signature and Parameter classes are now picklable and hashable. (Contributed by Yury Selivanov in bpo-20726 [https://bugs.python.org/issue20726] and bpo-20334 [https://bugs.python.org/issue20334].)

A new BoundArguments.apply_defaults() method provides a way to set default values for missing arguments:

>>> def foo(a, b='ham', *args): pass
>>> ba = inspect.signature(foo).bind('spam')
>>> ba.apply_defaults()
>>> ba.arguments
OrderedDict([('a', 'spam'), ('b', 'ham'), ('args', ())])

（由 Yury Selivanov 在 bpo-24190 [https://bugs.python.org/issue24190] 中贡献。）

A new class method Signature.from_callable() makes subclassing of Signature easier. (Contributed by Yury Selivanov and Eric Snow in bpo-17373 [https://bugs.python.org/issue17373].)

The signature() function now accepts a follow_wrapped optional keyword argument, which, when set to False, disables automatic following of __wrapped__ links. (Contributed by Yury Selivanov in bpo-20691 [https://bugs.python.org/issue20691].)

A set of new functions to inspect coroutine functions and coroutine objects has been added: iscoroutine(), iscoroutinefunction(), isawaitable(), getcoroutinelocals(), and getcoroutinestate(). (Contributed by Yury Selivanov in bpo-24017 [https://bugs.python.org/issue24017] and bpo-24400 [https://bugs.python.org/issue24400].)

The stack(), trace(), getouterframes(), and getinnerframes() functions now return a list of named tuples. (Contributed by Daniel Shahaf in bpo-16808 [https://bugs.python.org/issue16808].)

io

A new BufferedIOBase.readinto1() method, that uses at most one call to the underlying raw stream’s RawIOBase.read() or RawIOBase.readinto() methods. (Contributed by Nikolaus Rath in bpo-20578 [https://bugs.python.org/issue20578].)

ipaddress

Both the IPv4Network and IPv6Network classes now accept an (address, netmask) tuple argument, so as to easily construct network objects from existing addresses:

>>> import ipaddress
>>> ipaddress.IPv4Network(('127.0.0.0', 8))
IPv4Network('127.0.0.0/8')
>>> ipaddress.IPv4Network(('127.0.0.0', '255.0.0.0'))
IPv4Network('127.0.0.0/8')

（由 Peter Moody 和 Antoine Pitrou 在 bpo-16531 [https://bugs.python.org/issue16531] 中贡献。）

A new reverse_pointer attribute for the IPv4Network and IPv6Network classes returns the name of the reverse DNS PTR record:

>>> import ipaddress
>>> addr = ipaddress.IPv4Address('127.0.0.1')
>>> addr.reverse_pointer
'1.0.0.127.in-addr.arpa'
>>> addr6 = ipaddress.IPv6Address('::1')
>>> addr6.reverse_pointer
'1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa'

（由 Leon Weber 在 bpo-20480 [https://bugs.python.org/issue20480] 中贡献。）

json

The json.tool command line interface now preserves the order of keys in JSON objects passed in input. The new --sort-keys option can be used to sort the keys alphabetically. (Contributed by Berker Peksag in bpo-21650 [https://bugs.python.org/issue21650].)

JSON decoder now raises JSONDecodeError instead of ValueError to provide better context information about the error. (Contributed by Serhiy Storchaka in bpo-19361 [https://bugs.python.org/issue19361].)

linecache

A new lazycache() function can be used to capture information about a non-file-based module to permit getting its lines later via getline(). This avoids doing I/O until a line is actually needed, without having to carry the module globals around indefinitely. (Contributed by Robert Collins in bpo-17911 [https://bugs.python.org/issue17911].)

locale

A new delocalize() function can be used to convert a string into a normalized number string, taking the LC_NUMERIC settings into account:

>>> import locale
>>> locale.setlocale(locale.LC_NUMERIC, 'de_DE.UTF-8')
'de_DE.UTF-8'
>>> locale.delocalize('1.234,56')
'1234.56'
>>> locale.setlocale(locale.LC_NUMERIC, 'en_US.UTF-8')
'en_US.UTF-8'
>>> locale.delocalize('1,234.56')
'1234.56'

（由 Cédric Krier 在 bpo-13918 [https://bugs.python.org/issue13918] 中贡献。）

logging

All logging methods (Logger log(), exception(), critical(), debug(), etc.), now accept exception instances as an exc_info argument, in addition to boolean values and exception tuples:

>>> import logging
>>> try:
...     1/0
... except ZeroDivisionError as ex:
...     logging.error('exception', exc_info=ex)
ERROR:root:exception

（由 Yury Selivanov 在 bpo-20537 [https://bugs.python.org/issue20537] 中贡献。）

The handlers.HTTPHandler class now accepts an optional ssl.SSLContext instance to configure SSL settings used in an HTTP connection. (Contributed by Alex Gaynor in bpo-22788 [https://bugs.python.org/issue22788].)

The handlers.QueueListener class now takes a respect_handler_level keyword argument which, if set to True, will pass messages to handlers taking handler levels into account. (Contributed by Vinay Sajip.)

lzma

The LZMADecompressor.decompress() method now accepts an optional max_length argument to limit the maximum size of decompressed data. (Contributed by Martin Panter in bpo-15955 [https://bugs.python.org/issue15955].)

math

Two new constants have been added to the math module: inf and nan. (Contributed by Mark Dickinson in bpo-23185 [https://bugs.python.org/issue23185].)

A new function isclose() provides a way to test for approximate equality. (Contributed by Chris Barker and Tal Einat in bpo-24270 [https://bugs.python.org/issue24270].)

A new gcd() function has been added. The fractions.gcd() function is now deprecated. (Contributed by Mark Dickinson and Serhiy Storchaka in bpo-22486 [https://bugs.python.org/issue22486].)

multiprocessing

sharedctypes.synchronized() objects now support the context manager protocol. (Contributed by Charles-François Natali in bpo-21565 [https://bugs.python.org/issue21565].)

operator

attrgetter(), itemgetter(), and methodcaller() objects now support pickling. (Contributed by Josh Rosenberg and Serhiy Storchaka in bpo-22955 [https://bugs.python.org/issue22955].)

New matmul() and imatmul() functions to perform matrix multiplication. (Contributed by Benjamin Peterson in bpo-21176 [https://bugs.python.org/issue21176].)

os

The new scandir() function returning an iterator of DirEntry objects has been added. If possible, scandir() extracts file attributes while scanning a directory, removing the need to perform subsequent system calls to determine file type or attributes, which may significantly improve performance. (Contributed by Ben Hoyt with the help of Victor Stinner in bpo-22524 [https://bugs.python.org/issue22524].)

On Windows, a new stat_result.st_file_attributes attribute is now available. It corresponds to the dwFileAttributes member of the BY_HANDLE_FILE_INFORMATION structure returned by GetFileInformationByHandle(). (Contributed by Ben Hoyt in bpo-21719 [https://bugs.python.org/issue21719].)

The urandom() function now uses the getrandom() syscall on Linux 3.17 or newer, and getentropy() on OpenBSD 5.6 and newer, removing the need to use /dev/urandom and avoiding failures due to potential file descriptor exhaustion. (Contributed by Victor Stinner in bpo-22181 [https://bugs.python.org/issue22181].)

New get_blocking() and set_blocking() functions allow getting and setting a file descriptor’s blocking mode (O_NONBLOCK.) (Contributed by Victor Stinner in bpo-22054 [https://bugs.python.org/issue22054].)

The truncate() and ftruncate() functions are now supported on Windows. (Contributed by Steve Dower in bpo-23668 [https://bugs.python.org/issue23668].)

There is a new os.path.commonpath() function returning the longest common sub-path of each passed pathname. Unlike the os.path.commonprefix() function, it always returns a valid path:

>>> os.path.commonprefix(['/usr/lib', '/usr/local/lib'])
'/usr/l'

>>> os.path.commonpath(['/usr/lib', '/usr/local/lib'])
'/usr'

（由 Rafik Draoui 和 Serhiy Storchaka 在 bpo-10395 [https://bugs.python.org/issue10395] 中贡献。）

pathlib

The new Path.samefile() method can be used to check whether the path points to the same file as another path, which can be either another Path object, or a string:

>>> import pathlib
>>> p1 = pathlib.Path('/etc/hosts')
>>> p2 = pathlib.Path('/etc/../etc/hosts')
>>> p1.samefile(p2)
True

（由 Vajrasky Kok 和 Antoine Pitrou 在 bpo-19775 [https://bugs.python.org/issue19775] 中贡献。）

The Path.mkdir() method now accepts a new optional exist_ok argument to match mkdir -p and os.makedirs() functionality. (Contributed by Berker Peksag in bpo-21539 [https://bugs.python.org/issue21539].)

There is a new Path.expanduser() method to expand ~ and ~user prefixes. (Contributed by Serhiy Storchaka and Claudiu Popa in bpo-19776 [https://bugs.python.org/issue19776].)

A new Path.home() class method can be used to get a Path instance representing the user’s home directory. (Contributed by Victor Salgado and Mayank Tripathi in bpo-19777 [https://bugs.python.org/issue19777].)

New Path.write_text(), Path.read_text(), Path.write_bytes(), Path.read_bytes() methods to simplify read/write operations on files.

The following code snippet will create or rewrite existing file ~/spam42:

>>> import pathlib
>>> p = pathlib.Path('~/spam42')
>>> p.expanduser().write_text('ham')
3

（由 Christopher Welborn 在 bpo-20218 [https://bugs.python.org/issue20218] 中贡献。）

pickle

Nested objects, such as unbound methods or nested classes, can now be pickled using pickle protocols older than protocol version 4. Protocol version 4 already supports these cases. (Contributed by Serhiy Storchaka in bpo-23611 [https://bugs.python.org/issue23611].)

poplib

A new POP3.utf8() command enables RFC 6856 [https://tools.ietf.org/html/rfc6856.html] (Internationalized Email) support, if a POP server supports it. (Contributed by Milan OberKirch in bpo-21804 [https://bugs.python.org/issue21804].)

re

References and conditional references to groups with fixed length are now allowed in lookbehind assertions:

>>> import re
>>> pat = re.compile(r'(a|b).(?<=\1)c')
>>> pat.match('aac')
<_sre.SRE_Match object; span=(0, 3), match='aac'>
>>> pat.match('bbc')
<_sre.SRE_Match object; span=(0, 3), match='bbc'>

（由 Serhiy Storchaka 在 bpo-9179 [https://bugs.python.org/issue9179] 中贡献。）

The number of capturing groups in regular expressions is no longer limited to 100. (Contributed by Serhiy Storchaka in bpo-22437 [https://bugs.python.org/issue22437].)

The sub() and subn() functions now replace unmatched groups with empty strings instead of raising an exception. (Contributed by Serhiy Storchaka in bpo-1519638 [https://bugs.python.org/issue1519638].)

The re.error exceptions have new attributes, msg, pattern, pos, lineno, and colno, that provide better context information about the error:

>>> re.compile("""
...     (?x)
...     .++
... """)
Traceback (most recent call last):
   ...
sre_constants.error: multiple repeat at position 16 (line 3, column 7)

（由 Serhiy Storchaka 在 bpo-22578 [https://bugs.python.org/issue22578] 中贡献。）

readline

A new append_history_file() function can be used to append the specified number of trailing elements in history to the given file. (Contributed by Bruno Cauet in bpo-22940 [https://bugs.python.org/issue22940].)

selectors

The new DevpollSelector supports efficient /dev/poll polling on Solaris. (Contributed by Giampaolo Rodola’ in bpo-18931 [https://bugs.python.org/issue18931].)

shutil

The move() function now accepts a copy_function argument, allowing, for example, the copy() function to be used instead of the default copy2() if there is a need to ignore file metadata when moving. (Contributed by Claudiu Popa in bpo-19840 [https://bugs.python.org/issue19840].)

The make_archive() function now supports the xztar format. (Contributed by Serhiy Storchaka in bpo-5411 [https://bugs.python.org/issue5411].)

signal

On Windows, the set_wakeup_fd() function now also supports socket handles. (Contributed by Victor Stinner in bpo-22018 [https://bugs.python.org/issue22018].)

Various SIG* constants in the signal module have been converted into Enums. This allows meaningful names to be printed during debugging, instead of integer “magic numbers”. (Contributed by Giampaolo Rodola’ in bpo-21076 [https://bugs.python.org/issue21076].)

smtpd

Both the SMTPServer and SMTPChannel classes now accept a decode_data keyword argument to determine if the DATA portion of the SMTP transaction is decoded using the "utf-8" codec or is instead provided to the SMTPServer.process_message() method as a byte string. The default is True for backward compatibility reasons, but will change to False in Python 3.6. If decode_data is set to False, the process_message method must be prepared to accept keyword arguments. (Contributed by Maciej Szulik in bpo-19662 [https://bugs.python.org/issue19662].)

The SMTPServer class now advertises the 8BITMIME extension (RFC 6152 [https://tools.ietf.org/html/rfc6152.html]) if decode_data has been set True. If the client specifies BODY=8BITMIME on the MAIL command, it is passed to SMTPServer.process_message() via the mail_options keyword. (Contributed by Milan Oberkirch and R. David Murray in bpo-21795 [https://bugs.python.org/issue21795].)

The SMTPServer class now also supports the SMTPUTF8 extension (RFC 6531 [https://tools.ietf.org/html/rfc6531.html]: Internationalized Email). If the client specified SMTPUTF8 BODY=8BITMIME on the MAIL command, they are passed to SMTPServer.process_message() via the mail_options keyword. It is the responsibility of the process_message method to correctly handle the SMTPUTF8 data. (Contributed by Milan Oberkirch in bpo-21725 [https://bugs.python.org/issue21725].)

It is now possible to provide, directly or via name resolution, IPv6 addresses in the SMTPServer constructor, and have it successfully connect. (Contributed by Milan Oberkirch in bpo-14758 [https://bugs.python.org/issue14758].)

smtplib

A new SMTP.auth() method provides a convenient way to implement custom authentication mechanisms. (Contributed by Milan Oberkirch in bpo-15014 [https://bugs.python.org/issue15014].)

The SMTP.set_debuglevel() method now accepts an additional debuglevel (2), which enables timestamps in debug messages. (Contributed by Gavin Chappell and Maciej Szulik in bpo-16914 [https://bugs.python.org/issue16914].)

Both the SMTP.sendmail() and SMTP.send_message() methods now support RFC 6531 [https://tools.ietf.org/html/rfc6531.html] (SMTPUTF8). (Contributed by Milan Oberkirch and R. David Murray in bpo-22027 [https://bugs.python.org/issue22027].)

sndhdr

The what() and whathdr() functions now return a namedtuple(). (Contributed by Claudiu Popa in bpo-18615 [https://bugs.python.org/issue18615].)

socket

Functions with timeouts now use a monotonic clock, instead of a system clock. (Contributed by Victor Stinner in bpo-22043 [https://bugs.python.org/issue22043].)

A new socket.sendfile() method allows sending a file over a socket by using the high-performance os.sendfile() function on UNIX, resulting in uploads being from 2 to 3 times faster than when using plain socket.send(). (Contributed by Giampaolo Rodola’ in bpo-17552 [https://bugs.python.org/issue17552].)

The socket.sendall() method no longer resets the socket timeout every time bytes are received or sent. The socket timeout is now the maximum total duration to send all data. (Contributed by Victor Stinner in bpo-23853 [https://bugs.python.org/issue23853].)

The backlog argument of the socket.listen() method is now optional. By default it is set to SOMAXCONN or to 128, whichever is less. (Contributed by Charles-François Natali in bpo-21455 [https://bugs.python.org/issue21455].)

ssl

内存 BIO 支持

（由 Geert Jansen 在 bpo-21965 [https://bugs.python.org/issue21965] 中贡献。）

The new SSLObject class has been added to provide SSL protocol support for cases when the network I/O capabilities of SSLSocket are not necessary or are suboptimal. SSLObject represents an SSL protocol instance, but does not implement any network I/O methods, and instead provides a memory buffer interface. The new MemoryBIO class can be used to pass data between Python and an SSL protocol instance.

The memory BIO SSL support is primarily intended to be used in frameworks implementing asynchronous I/O for which SSLSocket’s readiness model (“select/poll”) is inefficient.

A new SSLContext.wrap_bio() method can be used to create a new SSLObject instance.

Application-Layer Protocol Negotiation Support

（由 Benjamin Peterson 在 bpo-20188 [https://bugs.python.org/issue20188] 中贡献。）

Where OpenSSL support is present, the ssl module now implements the Application-Layer Protocol Negotiation TLS extension as described in RFC 7301 [https://tools.ietf.org/html/rfc7301.html].

The new SSLContext.set_alpn_protocols() can be used to specify which protocols a socket should advertise during the TLS handshake.

The new SSLSocket.selected_alpn_protocol() returns the protocol that was selected during the TLS handshake. The HAS_ALPN flag indicates whether ALPN support is present.

Other Changes

There is a new SSLSocket.version() method to query the actual protocol version in use. (Contributed by Antoine Pitrou in bpo-20421 [https://bugs.python.org/issue20421].)

The SSLSocket class now implements a SSLSocket.sendfile() method. (Contributed by Giampaolo Rodola’ in bpo-17552 [https://bugs.python.org/issue17552].)

The SSLSocket.send() method now raises either the ssl.SSLWantReadError or ssl.SSLWantWriteError exception on a non-blocking socket if the operation would block. Previously, it would return 0. (Contributed by Nikolaus Rath in bpo-20951 [https://bugs.python.org/issue20951].)

The cert_time_to_seconds() function now interprets the input time as UTC and not as local time, per RFC 5280 [https://tools.ietf.org/html/rfc5280.html]. Additionally, the return value is always an int. (Contributed by Akira Li in bpo-19940 [https://bugs.python.org/issue19940].)

New SSLObject.shared_ciphers() and SSLSocket.shared_ciphers() methods return the list of ciphers sent by the client during the handshake. (Contributed by Benjamin Peterson in bpo-23186 [https://bugs.python.org/issue23186].)

The SSLSocket.do_handshake(), SSLSocket.read(), SSLSocket.shutdown(), and SSLSocket.write() methods of the SSLSocket class no longer reset the socket timeout every time bytes are received or sent. The socket timeout is now the maximum total duration of the method. (Contributed by Victor Stinner in bpo-23853 [https://bugs.python.org/issue23853].)

The match_hostname() function now supports matching of IP addresses. (Contributed by Antoine Pitrou in bpo-23239 [https://bugs.python.org/issue23239].)

sqlite3

The Row class now fully supports the sequence protocol, in particular reversed() iteration and slice indexing. (Contributed by Claudiu Popa in bpo-10203 [https://bugs.python.org/issue10203]; by Lucas Sinclair, Jessica McKellar, and Serhiy Storchaka in bpo-13583 [https://bugs.python.org/issue13583].)

subprocess

The new run() function has been added. It runs the specified command and returns a CompletedProcess object, which describes a finished process. The new API is more consistent and is the recommended approach to invoking subprocesses in Python code that does not need to maintain compatibility with earlier Python versions. (Contributed by Thomas Kluyver in bpo-23342 [https://bugs.python.org/issue23342].)

示例:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n')

sys

A new set_coroutine_wrapper() function allows setting a global hook that will be called whenever a coroutine object is created by an async def function. A corresponding get_coroutine_wrapper() can be used to obtain a currently set wrapper. Both functions are provisional, and are intended for debugging purposes only. (Contributed by Yury Selivanov in bpo-24017 [https://bugs.python.org/issue24017].)

A new is_finalizing() function can be used to check if the Python interpreter is shutting down. (Contributed by Antoine Pitrou in bpo-22696 [https://bugs.python.org/issue22696].)

sysconfig

The name of the user scripts directory on Windows now includes the first two components of the Python version. (Contributed by Paul Moore in bpo-23437 [https://bugs.python.org/issue23437].)

tarfile

The mode argument of the open() function now accepts "x" to request exclusive creation. (Contributed by Berker Peksag in bpo-21717 [https://bugs.python.org/issue21717].)

The TarFile.extractall() and TarFile.extract() methods now take a keyword argument numeric_owner. If set to True, the extracted files and directories will be owned by the numeric uid and gid from the tarfile. If set to False (the default, and the behavior in versions prior to 3.5), they will be owned by the named user and group in the tarfile. (Contributed by Michael Vogt and Eric Smith in bpo-23193 [https://bugs.python.org/issue23193].)

The TarFile.list() now accepts an optional members keyword argument that can be set to a subset of the list returned by TarFile.getmembers(). (Contributed by Serhiy Storchaka in bpo-21549 [https://bugs.python.org/issue21549].)

threading

Both the Lock.acquire() and RLock.acquire() methods now use a monotonic clock for timeout management. (Contributed by Victor Stinner in bpo-22043 [https://bugs.python.org/issue22043].)

time

The monotonic() function is now always available. (Contributed by Victor Stinner in bpo-22043 [https://bugs.python.org/issue22043].)

timeit

A new command line option -u or --unit=U can be used to specify the time unit for the timer output. Supported options are usec, msec, or sec. (Contributed by Julian Gindi in bpo-18983 [https://bugs.python.org/issue18983].)

The timeit() function has a new globals parameter for specifying the namespace in which the code will be running. (Contributed by Ben Roberts in bpo-2527 [https://bugs.python.org/issue2527].)

tkinter

The tkinter._fix module used for setting up the Tcl/Tk environment on Windows has been replaced by a private function in the _tkinter module which makes no permanent changes to environment variables. (Contributed by Zachary Ware in bpo-20035 [https://bugs.python.org/issue20035].)

回溯

New walk_stack() and walk_tb() functions to conveniently traverse frame and traceback objects. (Contributed by Robert Collins in bpo-17911 [https://bugs.python.org/issue17911].)

New lightweight classes: TracebackException, StackSummary, and FrameSummary. (Contributed by Robert Collins in bpo-17911 [https://bugs.python.org/issue17911].)

Both the print_tb() and print_stack() functions now support negative values for the limit argument. (Contributed by Dmitry Kazakov in bpo-22619 [https://bugs.python.org/issue22619].)

types

A new coroutine() function to transform generator and generator-like objects into awaitables. (Contributed by Yury Selivanov in bpo-24017 [https://bugs.python.org/issue24017].)

A new type called CoroutineType, which is used for coroutine objects created by async def functions. (Contributed by Yury Selivanov in bpo-24400 [https://bugs.python.org/issue24400].)

unicodedata

The unicodedata module now uses data from Unicode 8.0.0 [http://unicode.org/versions/Unicode8.0.0/].

unittest

The TestLoader.loadTestsFromModule() method now accepts a keyword-only argument pattern which is passed to load_tests as the third argument. Found packages are now checked for load_tests regardless of whether their path matches pattern, because it is impossible for a package name to match the default pattern. (Contributed by Robert Collins and Barry A. Warsaw in bpo-16662 [https://bugs.python.org/issue16662].)

Unittest discovery errors now are exposed in the TestLoader.errors attribute of the TestLoader instance. (Contributed by Robert Collins in bpo-19746 [https://bugs.python.org/issue19746].)

A new command line option --locals to show local variables in tracebacks. (Contributed by Robert Collins in bpo-22936 [https://bugs.python.org/issue22936].)

unittest.mock

The Mock class has the following improvements:

The class constructor has a new unsafe parameter, which causes mock objects to raise AttributeError on attribute names starting with "assert". (Contributed by Kushal Das in bpo-21238 [https://bugs.python.org/issue21238].)

A new Mock.assert_not_called() method to check if the mock object was called. (Contributed by Kushal Das in bpo-21262 [https://bugs.python.org/issue21262].)

The MagicMock class now supports __truediv__(), __divmod__() and __matmul__() operators. (Contributed by Johannes Baiter in bpo-20968 [https://bugs.python.org/issue20968], and Håkan Lövdahl in bpo-23581 [https://bugs.python.org/issue23581] and bpo-23568 [https://bugs.python.org/issue23568].)

It is no longer necessary to explicitly pass create=True to the patch() function when patching builtin names. (Contributed by Kushal Das in bpo-17660 [https://bugs.python.org/issue17660].)

urllib

A new request.HTTPPasswordMgrWithPriorAuth class allows HTTP Basic Authentication credentials to be managed so as to eliminate unnecessary 401 response handling, or to unconditionally send credentials on the first request in order to communicate with servers that return a 404 response instead of a 401 if the Authorization header is not sent. (Contributed by Matej Cepl in bpo-19494 [https://bugs.python.org/issue19494] and Akshit Khurana in bpo-7159 [https://bugs.python.org/issue7159].)

A new quote_via argument for the parse.urlencode() function provides a way to control the encoding of query parts if needed. (Contributed by Samwyse and Arnon Yaari in bpo-13866 [https://bugs.python.org/issue13866].)

The request.urlopen() function accepts an ssl.SSLContext object as a context argument, which will be used for the HTTPS connection. (Contributed by Alex Gaynor in bpo-22366 [https://bugs.python.org/issue22366].)

The parse.urljoin() was updated to use the RFC 3986 [https://tools.ietf.org/html/rfc3986.html] semantics for the resolution of relative URLs, rather than RFC 1808 [https://tools.ietf.org/html/rfc1808.html] and RFC 2396 [https://tools.ietf.org/html/rfc2396.html]. (Contributed by Demian Brecht and Senthil Kumaran in bpo-22118 [https://bugs.python.org/issue22118].)

wsgiref

The headers argument of the headers.Headers class constructor is now optional. (Contributed by Pablo Torres Navarrete and SilentGhost in bpo-5800 [https://bugs.python.org/issue5800].)

xmlrpc

The client.ServerProxy class now supports the context manager protocol. (Contributed by Claudiu Popa in bpo-20627 [https://bugs.python.org/issue20627].)

The client.ServerProxy constructor now accepts an optional ssl.SSLContext instance. (Contributed by Alex Gaynor in bpo-22960 [https://bugs.python.org/issue22960].)

xml.sax

SAX parsers now support a character stream of the xmlreader.InputSource object. (Contributed by Serhiy Storchaka in bpo-2175 [https://bugs.python.org/issue2175].)

parseString() now accepts a str instance. (Contributed by Serhiy Storchaka in bpo-10590 [https://bugs.python.org/issue10590].)

zipfile

ZIP output can now be written to unseekable streams. (Contributed by Serhiy Storchaka in bpo-23252 [https://bugs.python.org/issue23252].)

The mode argument of ZipFile.open() method now accepts "x" to request exclusive creation. (Contributed by Serhiy Storchaka in bpo-21717 [https://bugs.python.org/issue21717].)

其他模块级更改

Many functions in the mmap, ossaudiodev, socket, ssl, and codecs modules now accept writable bytes-like objects. (Contributed by Serhiy Storchaka in bpo-23001 [https://bugs.python.org/issue23001].)

性能优化

The os.walk() function has been sped up by 3 to 5 times on POSIX systems, and by 7 to 20 times on Windows. This was done using the new os.scandir() function, which exposes file information from the underlying readdir or FindFirstFile/FindNextFile system calls. (Contributed by Ben Hoyt with help from Victor Stinner in bpo-23605 [https://bugs.python.org/issue23605].)

Construction of bytes(int) (filled by zero bytes) is faster and uses less memory for large objects. calloc() is used instead of malloc() to allocate memory for these objects. (Contributed by Victor Stinner in bpo-21233 [https://bugs.python.org/issue21233].)

Some operations on ipaddress IPv4Network and IPv6Network have been massively sped up, such as subnets(), supernet(), summarize_address_range(), collapse_addresses(). The speed up can range from 3 to 15 times. (Contributed by Antoine Pitrou, Michel Albert, and Markus in bpo-21486 [https://bugs.python.org/issue21486], bpo-21487 [https://bugs.python.org/issue21487], bpo-20826 [https://bugs.python.org/issue20826], bpo-23266 [https://bugs.python.org/issue23266].)

Pickling of ipaddress objects was optimized to produce significantly smaller output. (Contributed by Serhiy Storchaka in bpo-23133 [https://bugs.python.org/issue23133].)

Many operations on io.BytesIO are now 50% to 100% faster. (Contributed by Serhiy Storchaka in bpo-15381 [https://bugs.python.org/issue15381] and David Wilson in bpo-22003 [https://bugs.python.org/issue22003].)

The marshal.dumps() function is now faster: 65–85% with versions 3 and 4, 20–25% with versions 0 to 2 on typical data, and up to 5 times in best cases. (Contributed by Serhiy Storchaka in bpo-20416 [https://bugs.python.org/issue20416] and bpo-23344 [https://bugs.python.org/issue23344].)

The UTF-32 encoder is now 3 to 7 times faster. (Contributed by Serhiy Storchaka in bpo-15027 [https://bugs.python.org/issue15027].)

Regular expressions are now parsed up to 10% faster. (Contributed by Serhiy Storchaka in bpo-19380 [https://bugs.python.org/issue19380].)

The json.dumps() function was optimized to run with ensure_ascii=False as fast as with ensure_ascii=True. (Contributed by Naoki Inada in bpo-23206 [https://bugs.python.org/issue23206].)

The PyObject_IsInstance() and PyObject_IsSubclass() functions have been sped up in the common case that the second argument has type as its metaclass. (Contributed Georg Brandl by in bpo-22540 [https://bugs.python.org/issue22540].)

Method caching was slightly improved, yielding up to 5% performance improvement in some benchmarks. (Contributed by Antoine Pitrou in bpo-22847 [https://bugs.python.org/issue22847].)

Objects from the random module now use 50% less memory on 64-bit builds. (Contributed by Serhiy Storchaka in bpo-23488 [https://bugs.python.org/issue23488].)

The property() getter calls are up to 25% faster. (Contributed by Joe Jevnik in bpo-23910 [https://bugs.python.org/issue23910].)

Instantiation of fractions.Fraction is now up to 30% faster. (Contributed by Stefan Behnel in bpo-22464 [https://bugs.python.org/issue22464].)

String methods find(), rfind(), split(), partition() and the in string operator are now significantly faster for searching 1-character substrings. (Contributed by Serhiy Storchaka in bpo-23573 [https://bugs.python.org/issue23573].)

构建和 C API 的改变

增加了 calloc 函数

PyMem_RawCalloc(),

PyMem_Calloc(),

PyObject_Calloc().

(Victor Stinner 贡献于 bpo-21233 [https://bugs.python.org/issue21233].)

新的 encoding/decoding 帮助函数:

Py_DecodeLocale() (替代 _Py_char2wchar()),

Py_EncodeLocale() (替代 _Py_wchar2char()).

（由 Victor Stinner 在 bpo-18395 [https://bugs.python.org/issue18395] 中贡献。）

A new PyCodec_NameReplaceErrors() function to replace the unicode encode error with \N{...} escapes. (Contributed by Serhiy Storchaka in bpo-19676 [https://bugs.python.org/issue19676].)

A new PyErr_FormatV() function similar to PyErr_Format(), but accepts a va_list argument. (Contributed by Antoine Pitrou in bpo-18711 [https://bugs.python.org/issue18711].)

A new PyExc_RecursionError exception. (Contributed by Georg Brandl in bpo-19235 [https://bugs.python.org/issue19235].)

New PyModule_FromDefAndSpec(), PyModule_FromDefAndSpec2(), and PyModule_ExecDef() functions introduced by PEP 489 [https://www.python.org/dev/peps/pep-0489] – multi-phase extension module initialization. (Contributed by Petr Viktorin in bpo-24268 [https://bugs.python.org/issue24268].)

New PyNumber_MatrixMultiply() and PyNumber_InPlaceMatrixMultiply() functions to perform matrix multiplication. (Contributed by Benjamin Peterson in bpo-21176 [https://bugs.python.org/issue21176]. See also PEP 465 [https://www.python.org/dev/peps/pep-0465] for details.)

The PyTypeObject.tp_finalize slot is now part of the stable ABI.

Windows builds now require Microsoft Visual C++ 14.0, which is available as part of Visual Studio 2015 [https://www.visualstudio.com/].

Extension modules now include a platform information tag in their filename on some platforms (the tag is optional, and CPython will import extensions without it, although if the tag is present and mismatched, the extension won’t be loaded):

On Linux, extension module filenames end with .cpython-<major><minor>m-<architecture>-<os>.pyd:

<major> is the major number of the Python version; for Python 3.5 this is 3.

<minor> is the minor number of the Python version; for Python 3.5 this is 5.

<architecture> is the hardware architecture the extension module was built to run on. It’s most commonly either i386 for 32-bit Intel platforms or x86_64 for 64-bit Intel (and AMD) platforms.

<os> is always linux-gnu, except for extensions built to talk to the 32-bit ABI on 64-bit platforms, in which case it is linux-gnu32 (and <architecture> will be x86_64).

On Windows, extension module filenames end with <debug>.cp<major><minor>-<platform>.pyd:

<major> is the major number of the Python version; for Python 3.5 this is 3.

<minor> is the minor number of the Python version; for Python 3.5 this is 5.

<platform> is the platform the extension module was built for, either win32 for Win32, win_amd64 for Win64, win_ia64 for Windows Itanium 64, and win_arm for Windows on ARM.

If built in debug mode, <debug> will be _d, otherwise it will be blank.

On OS X platforms, extension module filenames now end with -darwin.so.

On all other platforms, extension module filenames are the same as they were with Python 3.4.

弃用

新关键字

async and await are not recommended to be used as variable, class, function or module names. Introduced by PEP 492 [https://www.python.org/dev/peps/pep-0492] in Python 3.5, they will become proper keywords in Python 3.7.

已弃用的 Python 行为

Raising the StopIteration exception inside a generator will now generate a silent PendingDeprecationWarning, which will become a non-silent deprecation warning in Python 3.6 and will trigger a RuntimeError in Python 3.7. See PEP 479: Change StopIteration handling inside generators for details.

不支持的操作系统

Windows XP is no longer supported by Microsoft, thus, per PEP 11 [https://www.python.org/dev/peps/pep-0011], CPython 3.5 is no longer officially supported on this OS.

已弃用的 Python 模块、函数和方法

The formatter module has now graduated to full deprecation and is still slated for removal in Python 3.6.

The asyncio.async() function is deprecated in favor of ensure_future().

The smtpd module has in the past always decoded the DATA portion of email messages using the utf-8 codec. This can now be controlled by the new decode_data keyword to SMTPServer. The default value is True, but this default is deprecated. Specify the decode_data keyword with an appropriate value to avoid the deprecation warning.

Directly assigning values to the key, value and coded_value of http.cookies.Morsel objects is deprecated. Use the set() method instead. In addition, the undocumented LegalChars parameter of set() is deprecated, and is now ignored.

Passing a format string as keyword argument format_string to the format() method of the string.Formatter class has been deprecated. (Contributed by Serhiy Storchaka in bpo-23671 [https://bugs.python.org/issue23671].)

The platform.dist() and platform.linux_distribution() functions are now deprecated. Linux distributions use too many different ways of describing themselves, so the functionality is left to a package. (Contributed by Vajrasky Kok and Berker Peksag in bpo-1322 [https://bugs.python.org/issue1322].)

The previously undocumented from_function and from_builtin methods of inspect.Signature are deprecated. Use the new Signature.from_callable() method instead. (Contributed by Yury Selivanov in bpo-24248 [https://bugs.python.org/issue24248].)

The inspect.getargspec() function is deprecated and scheduled to be removed in Python 3.6. (See bpo-20438 [https://bugs.python.org/issue20438] for details.)

The inspect getfullargspec(), getcallargs(), and formatargspec() functions are deprecated in favor of the inspect.signature() API. (Contributed by Yury Selivanov in bpo-20438 [https://bugs.python.org/issue20438].)

getargvalues() and formatargvalues() functions were inadvertently marked as deprecated with the release of Python 3.5.0.

Use of re.LOCALE flag with str patterns or re.ASCII is now deprecated. (Contributed by Serhiy Storchaka in bpo-22407 [https://bugs.python.org/issue22407].)

Use of unrecognized special sequences consisting of '\' and an ASCII letter in regular expression patterns and replacement patterns now raises a deprecation warning and will be forbidden in Python 3.6. (Contributed by Serhiy Storchaka in bpo-23622 [https://bugs.python.org/issue23622].)

The undocumented and unofficial use_load_tests default argument of the unittest.TestLoader.loadTestsFromModule() method now is deprecated and ignored. (Contributed by Robert Collins and Barry A. Warsaw in bpo-16662 [https://bugs.python.org/issue16662].)

移除

API 与特性的移除

The following obsolete and previously deprecated APIs and features have been removed:

The __version__ attribute has been dropped from the email package. The email code hasn’t been shipped separately from the stdlib for a long time, and the __version__ string was not updated in the last few releases.

The internal Netrc class in the ftplib module was deprecated in 3.4, and has now been removed. (Contributed by Matt Chaput in bpo-6623 [https://bugs.python.org/issue6623].)

The concept of .pyo files has been removed.

The JoinableQueue class in the provisional asyncio module was deprecated in 3.4.4 and is now removed. (Contributed by A. Jesse Jiryu Davis in bpo-23464 [https://bugs.python.org/issue23464].)

移植到Python 3.5

本节列出了先前描述的更改以及可能需要更改代码的其他错误修正.

Python 行为的改变

由于一个疏忽，之前的 Python 版本会错误地接受以下语法:

f(1 for x in [1], *args)
f(1 for x in [1], **kwargs)

Python 3.5 now correctly raises a SyntaxError, as generator expressions must be put in parentheses if not a sole argument to a function.

更改的Python API

PEP 475 [https://www.python.org/dev/peps/pep-0475]: System calls are now retried when interrupted by a signal instead of raising InterruptedError if the Python signal handler does not raise an exception.

Before Python 3.5, a datetime.time object was considered to be false if it represented midnight in UTC. This behavior was considered obscure and error-prone and has been removed in Python 3.5. See bpo-13936 [https://bugs.python.org/issue13936] for full details.

The ssl.SSLSocket.send() method now raises either ssl.SSLWantReadError or ssl.SSLWantWriteError on a non-blocking socket if the operation would block. Previously, it would return 0. (Contributed by Nikolaus Rath in bpo-20951 [https://bugs.python.org/issue20951].)

The __name__ attribute of generators is now set from the function name, instead of being set from the code name. Use gen.gi_code.co_name to retrieve the code name. Generators also have a new __qualname__ attribute, the qualified name, which is now used for the representation of a generator (repr(gen)). (Contributed by Victor Stinner in bpo-21205 [https://bugs.python.org/issue21205].)

The deprecated “strict” mode and argument of HTMLParser, HTMLParser.error(), and the HTMLParserError exception have been removed. (Contributed by Ezio Melotti in bpo-15114 [https://bugs.python.org/issue15114].) The convert_charrefs argument of HTMLParser is now True by default. (Contributed by Berker Peksag in bpo-21047 [https://bugs.python.org/issue21047].)

Although it is not formally part of the API, it is worth noting for porting purposes (ie: fixing tests) that error messages that were previously of the form “‘sometype’ does not support the buffer protocol” are now of the form “a bytes-like object is required, not ‘sometype’”. (Contributed by Ezio Melotti in bpo-16518 [https://bugs.python.org/issue16518].)

If the current directory is set to a directory that no longer exists then FileNotFoundError will no longer be raised and instead find_spec() will return None without caching None in sys.path_importer_cache, which is different than the typical case (bpo-22834 [https://bugs.python.org/issue22834]).

HTTP status code and messages from http.client and http.server were refactored into a common HTTPStatus enum. The values in http.client and http.server remain available for backwards compatibility. (Contributed by Demian Brecht in bpo-21793 [https://bugs.python.org/issue21793].)

When an import loader defines importlib.machinery.Loader.exec_module() it is now expected to also define create_module() (raises a DeprecationWarning now, will be an error in Python 3.6). If the loader inherits from importlib.abc.Loader then there is nothing to do, else simply define create_module() to return None. (Contributed by Brett Cannon in bpo-23014 [https://bugs.python.org/issue23014].)

The re.split() function always ignored empty pattern matches, so the "x*" pattern worked the same as "x+", and the "\b" pattern never worked. Now re.split() raises a warning if the pattern could match an empty string. For compatibility, use patterns that never match an empty string (e.g. "x+" instead of "x*"). Patterns that could only match an empty string (such as "\b") now raise an error. (Contributed by Serhiy Storchaka in bpo-22818 [https://bugs.python.org/issue22818].)

The http.cookies.Morsel dict-like interface has been made self consistent: morsel comparison now takes the key and value into account, copy() now results in a Morsel instance rather than a dict, and update() will now raise an exception if any of the keys in the update dictionary are invalid. In addition, the undocumented LegalChars parameter of set() is deprecated and is now ignored. (Contributed by Demian Brecht in bpo-2211 [https://bugs.python.org/issue2211].)

PEP 488 [https://www.python.org/dev/peps/pep-0488] has removed .pyo files from Python and introduced the optional opt- tag in .pyc file names. The importlib.util.cache_from_source() has gained an optimization parameter to help control the opt- tag. Because of this, the debug_override parameter of the function is now deprecated. .pyo files are also no longer supported as a file argument to the Python interpreter and thus serve no purpose when distributed on their own (i.e. sourceless code distribution). Due to the fact that the magic number for bytecode has changed in Python 3.5, all old .pyo files from previous versions of Python are invalid regardless of this PEP.

The socket module now exports the CAN_RAW_FD_FRAMES constant on linux 3.6 and greater.

The ssl.cert_time_to_seconds() function now interprets the input time as UTC and not as local time, per RFC 5280 [https://tools.ietf.org/html/rfc5280.html]. Additionally, the return value is always an int. (Contributed by Akira Li in bpo-19940 [https://bugs.python.org/issue19940].)

The pygettext.py Tool now uses the standard +NNNN format for timezones in the POT-Creation-Date header.

The smtplib module now uses sys.stderr instead of the previous module-level stderr variable for debug output. If your (test) program depends on patching the module-level variable to capture the debug output, you will need to update it to capture sys.stderr instead.

The str.startswith() and str.endswith() methods no longer return True when finding the empty string and the indexes are completely out of range. (Contributed by Serhiy Storchaka in bpo-24284 [https://bugs.python.org/issue24284].)

The inspect.getdoc() function now returns documentation strings inherited from base classes. Documentation strings no longer need to be duplicated if the inherited documentation is appropriate. To suppress an inherited string, an empty string must be specified (or the documentation may be filled in). This change affects the output of the pydoc module and the help() function. (Contributed by Serhiy Storchaka in bpo-15582 [https://bugs.python.org/issue15582].)

Nested functools.partial() calls are now flattened. If you were relying on the previous behavior, you can now either add an attribute to a functools.partial() object or you can create a subclass of functools.partial(). (Contributed by Alexander Belopolsky in bpo-7830 [https://bugs.python.org/issue7830].)

C API 中的改变

The undocumented format member of the (non-public) PyMemoryViewObject structure has been removed. All extensions relying on the relevant parts in memoryobject.h must be rebuilt.

The PyMemAllocator structure was renamed to PyMemAllocatorEx and a new calloc field was added.

Removed non-documented macro PyObject_REPR which leaked references. Use format character %R in PyUnicode_FromFormat()-like functions to format the repr() of the object. (Contributed by Serhiy Storchaka in bpo-22453 [https://bugs.python.org/issue22453].)

Because the lack of the __module__ attribute breaks pickling and introspection, a deprecation warning is now raised for builtin types without the __module__ attribute. This would be an AttributeError in the future. (Contributed by Serhiy Storchaka in bpo-20204 [https://bugs.python.org/issue20204].)

As part of the PEP 492 [https://www.python.org/dev/peps/pep-0492] implementation, the tp_reserved slot of PyTypeObject was replaced with a tp_as_async slot. Refer to 协程对象 for new types, structures and functions.

Python 3.5.4 的显著变化

New make regen-all build target

To simplify cross-compilation, and to ensure that CPython can reliably be compiled without requiring an existing version of Python to already be available, the autotools-based build system no longer attempts to implicitly recompile generated files based on file modification times.

Instead, a new make regen-all command has been added to force regeneration of these files when desired (e.g. after an initial version of Python has already been built based on the pregenerated versions).

More selective regeneration targets are also defined - see Makefile.pre.in [https://github.com/python/cpython/tree/3.6/Makefile.pre.in] for details.

（由 Victor Stinner 在 bpo-23404 [https://bugs.python.org/issue23404] 中贡献。）

3.5.4 新版功能.

Removal of make touch build target

The make touch build target previously used to request implicit regeneration of generated files by updating their modification times has been removed.

It has been replaced by the new make regen-all target.

（由 Victor Stinner 在 bpo-23404 [https://bugs.python.org/issue23404] 中贡献。）

在 3.5.4 版更改.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

Python 3.4 有什么新变化

作者

R. David Murray <rdmurray@bitdance.com> (Editor)

This article explains the new features in Python 3.4, compared to 3.3. Python 3.4 was released on March 16, 2014. For full details, see the changelog [https://docs.python.org/3.4/whatsnew/changelog.html].

参见

PEP 429 [https://www.python.org/dev/peps/pep-0429] – Python 3.4 发布计划

摘要 - 发布重点

新的语法特性：

No new syntax features were added in Python 3.4.

其他的新特性

pip 能够随时可用 (PEP 453 [https://www.python.org/dev/peps/pep-0453]).

Newly created file descriptors are non-inheritable (PEP 446 [https://www.python.org/dev/peps/pep-0446]).

command line option for isolated mode (bpo-16499 [https://bugs.python.org/issue16499]).

improvements in the handling of codecs that are not text encodings (multiple issues).

A ModuleSpec Type for the Import System (PEP 451 [https://www.python.org/dev/peps/pep-0451]). (Affects importer authors.)

The marshal format has been made more compact and efficient (bpo-16475 [https://bugs.python.org/issue16475]).

新的库模块：

asyncio: New provisional API for asynchronous IO (PEP 3156 [https://www.python.org/dev/peps/pep-3156]).

ensurepip: Bootstrapping the pip installer (PEP 453 [https://www.python.org/dev/peps/pep-0453]).

enum: Support for enumeration types (PEP 435 [https://www.python.org/dev/peps/pep-0435]).

pathlib: Object-oriented filesystem paths (PEP 428 [https://www.python.org/dev/peps/pep-0428]).

selectors: High-level and efficient I/O multiplexing, built upon the select module primitives (part of PEP 3156 [https://www.python.org/dev/peps/pep-3156]).

statistics: A basic numerically stable statistics library (PEP 450 [https://www.python.org/dev/peps/pep-0450]).

tracemalloc: Trace Python memory allocations (PEP 454 [https://www.python.org/dev/peps/pep-0454]).

Significantly improved library modules:

Single-dispatch generic functions in functools (PEP 443 [https://www.python.org/dev/peps/pep-0443]).

New pickle protocol 4 (PEP 3154 [https://www.python.org/dev/peps/pep-3154]).

multiprocessing now has an option to avoid using os.fork on Unix (bpo-8713 [https://bugs.python.org/issue8713]).

email has a new submodule, contentmanager, and a new Message subclass (EmailMessage) that simplify MIME handling (bpo-18891 [https://bugs.python.org/issue18891]).

The inspect and pydoc modules are now capable of correct introspection of a much wider variety of callable objects, which improves the output of the Python help() system.

The ipaddress module API has been declared stable

安全改进：

Secure and interchangeable hash algorithm (PEP 456 [https://www.python.org/dev/peps/pep-0456]).

Make newly created file descriptors non-inheritable (PEP 446 [https://www.python.org/dev/peps/pep-0446]) to avoid leaking file descriptors to child processes.

New command line option for isolated mode, (bpo-16499 [https://bugs.python.org/issue16499]).

multiprocessing now has an option to avoid using os.fork on Unix. spawn and forkserver are more secure because they avoid sharing data with child processes.

multiprocessing child processes on Windows no longer inherit all of the parent’s inheritable handles, only the necessary ones.

A new hashlib.pbkdf2_hmac() function provides the PKCS#5 password-based key derivation function 2 [https://en.wikipedia.org/wiki/PBKDF2].

TLSv1.1 and TLSv1.2 support for ssl.

Retrieving certificates from the Windows system cert store support for ssl.

Server-side SNI (Server Name Indication) support for ssl.

The ssl.SSLContext class has a lot of improvements.

All modules in the standard library that support SSL now support server certificate verification, including hostname matching (ssl.match_hostname()) and CRLs (Certificate Revocation lists, see ssl.SSLContext.load_verify_locations()).

CPython 实现的改进：

Safe object finalization (PEP 442 [https://www.python.org/dev/peps/pep-0442]).

Leveraging PEP 442 [https://www.python.org/dev/peps/pep-0442], in most cases module globals are no longer set to None during finalization (bpo-18214 [https://bugs.python.org/issue18214]).

Configurable memory allocators (PEP 445 [https://www.python.org/dev/peps/pep-0445]).

Argument Clinic (PEP 436 [https://www.python.org/dev/peps/pep-0436]).

Please read on for a comprehensive list of user-facing changes, including many other smaller improvements, CPython optimizations, deprecations, and potential porting issues.

新的特性

PEP 453: Explicit Bootstrapping of PIP in Python Installations

Bootstrapping pip By Default

The new ensurepip module (defined in PEP 453 [https://www.python.org/dev/peps/pep-0453]) provides a standard cross-platform mechanism to bootstrap the pip installer into Python installations and virtual environments. The version of pip included with Python 3.4.0 is pip 1.5.4, and future 3.4.x maintenance releases will update the bundled version to the latest version of pip that is available at the time of creating the release candidate.

By default, the commands pipX and pipX.Y will be installed on all platforms (where X.Y stands for the version of the Python installation), along with the pip Python package and its dependencies. On Windows and in virtual environments on all platforms, the unversioned pip command will also be installed. On other platforms, the system wide unversioned pip command typically refers to the separately installed Python 2 version.

The pyvenv command line utility and the venv module make use of the ensurepip module to make pip readily available in virtual environments. When using the command line utility, pip is installed by default, while when using the venv module API installation of pip must be requested explicitly.

For CPython source builds on POSIX systems, the make install and make altinstall commands bootstrap pip by default. This behaviour can be controlled through configure options, and overridden through Makefile options.

On Windows and Mac OS X, the CPython installers now default to installing pip along with CPython itself (users may opt out of installing it during the installation process). Window users will need to opt in to the automatic PATH modifications to have pip available from the command line by default, otherwise it can still be accessed through the Python launcher for Windows as py -m pip.

As discussed in the PEP [https://www.python.org/dev/peps/pep-0453/#recommendations-for-downstream-distributors], platform packagers may choose not to install these commands by default, as long as, when invoked, they provide clear and simple directions on how to install them on that platform (usually using the system package manager).

注解

To avoid conflicts between parallel Python 2 and Python 3 installations, only the versioned pip3 and pip3.4 commands are bootstrapped by default when ensurepip is invoked directly - the --default-pip option is needed to also request the unversioned pip command. pyvenv and the Windows installer ensure that the unqualified pip command is made available in those environments, and pip can always be invoked via the -m switch rather than directly to avoid ambiguity on systems with multiple Python installations.

文档更改

As part of this change, the 安装 Python 模块 and 分发 Python 模块 sections of the documentation have been completely redesigned as short getting started and FAQ documents. Most packaging documentation has now been moved out to the Python Packaging Authority maintained Python Packaging User Guide [https://packaging.python.org] and the documentation of the individual projects.

However, as this migration is currently still incomplete, the legacy versions of those guides remaining available as 安装Python模块（旧版） and 分发 Python 模块（遗留版本）.

参见

PEP 453 [https://www.python.org/dev/peps/pep-0453] – Python安装中pip的显式引导

PEP 由Donald Stufft 和 Nick Coghlan 撰写，由 Donald Stufft，Nick Coghlan，Martin von Löwis 和 Ned Deily 实现。

PEP 446: Newly Created File Descriptors Are Non-Inheritable

PEP 446 [https://www.python.org/dev/peps/pep-0446] makes newly created file descriptors non-inheritable. In general, this is the behavior an application will want: when launching a new process, having currently open files also open in the new process can lead to all sorts of hard to find bugs, and potentially to security issues.

However, there are occasions when inheritance is desired. To support these cases, the following new functions and methods are available:

os.get_inheritable(), os.set_inheritable()

os.get_handle_inheritable(), os.set_handle_inheritable()

socket.socket.get_inheritable(), socket.socket.set_inheritable()

参见

PEP 446 [https://www.python.org/dev/peps/pep-0446] – Make newly created file descriptors non-inheritable

PEP 由 Victor Stinner 撰写并实现。

Improvements to Codec Handling

Since it was first introduced, the codecs module has always been intended to operate as a type-neutral dynamic encoding and decoding system. However, its close coupling with the Python text model, especially the type restricted convenience methods on the builtin str, bytes and bytearray types, has historically obscured that fact.

As a key step in clarifying the situation, the codecs.encode() and codecs.decode() convenience functions are now properly documented in Python 2.7, 3.3 and 3.4. These functions have existed in the codecs module (and have been covered by the regression test suite) since Python 2.4, but were previously only discoverable through runtime introspection.

Unlike the convenience methods on str, bytes and bytearray, the codecs convenience functions support arbitrary codecs in both Python 2 and Python 3, rather than being limited to Unicode text encodings (in Python 3) or basestring <-> basestring conversions (in Python 2).

In Python 3.4, the interpreter is able to identify the known non-text encodings provided in the standard library and direct users towards these general purpose convenience functions when appropriate:

>>> b"abcdef".decode("hex")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: 'hex' is not a text encoding; use codecs.decode() to handle arbitrary codecs

>>> "hello".encode("rot13")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: 'rot13' is not a text encoding; use codecs.encode() to handle arbitrary codecs

>>> open("foo.txt", encoding="hex")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: 'hex' is not a text encoding; use codecs.open() to handle arbitrary codecs

In a related change, whenever it is feasible without breaking backwards compatibility, exceptions raised during encoding and decoding operations are wrapped in a chained exception of the same type that mentions the name of the codec responsible for producing the error:

>>> import codecs

>>> codecs.decode(b"abcdefgh", "hex")
Traceback (most recent call last):
  File "/usr/lib/python3.4/encodings/hex_codec.py", line 20, in hex_decode
    return (binascii.a2b_hex(input), len(input))
binascii.Error: Non-hexadecimal digit found

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
binascii.Error: decoding with 'hex' codec failed (Error: Non-hexadecimal digit found)

>>> codecs.encode("hello", "bz2")
Traceback (most recent call last):
  File "/usr/lib/python3.4/encodings/bz2_codec.py", line 17, in bz2_encode
    return (bz2.compress(input), len(input))
  File "/usr/lib/python3.4/bz2.py", line 498, in compress
    return comp.compress(data) + comp.flush()
TypeError: 'str' does not support the buffer interface

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: encoding with 'bz2' codec failed (TypeError: 'str' does not support the buffer interface)

Finally, as the examples above show, these improvements have permitted the restoration of the convenience aliases for the non-Unicode codecs that were themselves restored in Python 3.2. This means that encoding binary data to and from its hexadecimal representation (for example) can now be written as:

>>> from codecs import encode, decode
>>> encode(b"hello", "hex")
b'68656c6c6f'
>>> decode(b"68656c6c6f", "hex")
b'hello'

The binary and text transforms provided in the standard library are detailed in 二进制转换 and 文字转换.

(Contributed by Nick Coghlan in bpo-7475 [https://bugs.python.org/issue7475], bpo-17827 [https://bugs.python.org/issue17827], bpo-17828 [https://bugs.python.org/issue17828] and bpo-19619 [https://bugs.python.org/issue19619].)

PEP 451: A ModuleSpec Type for the Import System

PEP 451 [https://www.python.org/dev/peps/pep-0451] provides an encapsulation of the information about a module that the import machinery will use to load it (that is, a module specification). This helps simplify both the import implementation and several import-related APIs. The change is also a stepping stone for several future import-related improvements [https://mail.python.org/pipermail/python-dev/2013-November/130111.html].

The public-facing changes from the PEP are entirely backward-compatible. Furthermore, they should be transparent to everyone but importer authors. Key finder and loader methods have been deprecated, but they will continue working. New importers should use the new methods described in the PEP. Existing importers should be updated to implement the new methods. See the 弃用 section for a list of methods that should be replaced and their replacements.

其他语言特性修改

对Python 语言核心进行的小改动：

Unicode database updated to UCD version 6.3.

min() and max() now accept a default keyword-only argument that can be used to specify the value they return if the iterable they are evaluating has no elements. (Contributed by Julian Berman in bpo-18111 [https://bugs.python.org/issue18111].)

Module objects are now weakref’able.

Module __file__ attributes (and related values) should now always contain absolute paths by default, with the sole exception of __main__.__file__ when a script has been executed directly using a relative path. (Contributed by Brett Cannon in bpo-18416 [https://bugs.python.org/issue18416].)

All the UTF-* codecs (except UTF-7) now reject surrogates during both encoding and decoding unless the surrogatepass error handler is used, with the exception of the UTF-16 decoder (which accepts valid surrogate pairs) and the UTF-16 encoder (which produces them while encoding non-BMP characters). (Contributed by Victor Stinner, Kang-Hao (Kenny) Lu and Serhiy Storchaka in bpo-12892 [https://bugs.python.org/issue12892].)

New German EBCDIC codec cp273. (Contributed by Michael Bierenfeld and Andrew Kuchling in bpo-1097797 [https://bugs.python.org/issue1097797].)

New Ukrainian codec cp1125. (Contributed by Serhiy Storchaka in bpo-19668 [https://bugs.python.org/issue19668].)

bytes.join() and bytearray.join() now accept arbitrary buffer objects as arguments. (Contributed by Antoine Pitrou in bpo-15958 [https://bugs.python.org/issue15958].)

The int constructor now accepts any object that has an __index__ method for its base argument. (Contributed by Mark Dickinson in bpo-16772 [https://bugs.python.org/issue16772].)

Frame objects now have a clear() method that clears all references to local variables from the frame. (Contributed by Antoine Pitrou in bpo-17934 [https://bugs.python.org/issue17934].)

memoryview is now registered as a Sequence, and supports the reversed() builtin. (Contributed by Nick Coghlan and Claudiu Popa in bpo-18690 [https://bugs.python.org/issue18690] and bpo-19078 [https://bugs.python.org/issue19078].)

Signatures reported by help() have been modified and improved in several cases as a result of the introduction of Argument Clinic and other changes to the inspect and pydoc modules.

__length_hint__() is now part of the formal language specification (see PEP 424 [https://www.python.org/dev/peps/pep-0424]). (Contributed by Armin Ronacher in bpo-16148 [https://bugs.python.org/issue16148].)

新增模块

asyncio

The new asyncio module (defined in PEP 3156 [https://www.python.org/dev/peps/pep-3156]) provides a standard pluggable event loop model for Python, providing solid asynchronous IO support in the standard library, and making it easier for other event loop implementations to interoperate with the standard library and each other.

For Python 3.4, this module is considered a provisional API.

参见

PEP 3156 [https://www.python.org/dev/peps/pep-3156] – Asynchronous IO Support Rebooted: the “asyncio” Module

PEP 由 Guido van Rossum 领导编写和实现。

ensurepip

The new ensurepip module is the primary infrastructure for the PEP 453 [https://www.python.org/dev/peps/pep-0453] implementation. In the normal course of events end users will not need to interact with this module, but it can be used to manually bootstrap pip if the automated bootstrapping into an installation or virtual environment was declined.

ensurepip includes a bundled copy of pip, up-to-date as of the first release candidate of the release of CPython with which it ships (this applies to both maintenance releases and feature releases). ensurepip does not access the internet. If the installation has Internet access, after ensurepip is run the bundled pip can be used to upgrade pip to a more recent release than the bundled one. (Note that such an upgraded version of pip is considered to be a separately installed package and will not be removed if Python is uninstalled.)

The module is named ensurepip because if called when pip is already installed, it does nothing. It also has an --upgrade option that will cause it to install the bundled copy of pip if the existing installed version of pip is older than the bundled copy.

enum

The new enum module (defined in PEP 435 [https://www.python.org/dev/peps/pep-0435]) provides a standard implementation of enumeration types, allowing other modules (such as socket) to provide more informative error messages and better debugging support by replacing opaque integer constants with backwards compatible enumeration values.

参见

PEP 435 [https://www.python.org/dev/peps/pep-0435] – Adding an Enum type to the Python standard library

PEP 由 Barry Warsaw，Eli Bendersky 和 Ethan Furman 撰写 ，由 Ethan Furman 实现。

pathlib

The new pathlib module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths, which provide purely computational operations without I/O, and concrete paths, which inherit from pure paths but also provide I/O operations.

For Python 3.4, this module is considered a provisional API.

参见

PEP 428 [https://www.python.org/dev/peps/pep-0428] – The pathlib module – object-oriented filesystem paths

PEP 由 Antoine Pitrou 撰写并实现

selectors

The new selectors module (created as part of implementing PEP 3156 [https://www.python.org/dev/peps/pep-3156]) allows high-level and efficient I/O multiplexing, built upon the select module primitives.

statistics

The new statistics module (defined in PEP 450 [https://www.python.org/dev/peps/pep-0450]) offers some core statistics functionality directly in the standard library. This module supports calculation of the mean, median, mode, variance and standard deviation of a data series.

参见

PEP 450 [https://www.python.org/dev/peps/pep-0450] – Adding A Statistics Module To The Standard Library

PEP 由 Steven D’Aprano 撰写并实现。

tracemalloc

The new tracemalloc module (defined in PEP 454 [https://www.python.org/dev/peps/pep-0454]) is a debug tool to trace memory blocks allocated by Python. It provides the following information:

Trace where an object was allocated

Statistics on allocated memory blocks per filename and per line number: total size, number and average size of allocated memory blocks

Compute the differences between two snapshots to detect memory leaks

参见

PEP 454 [https://www.python.org/dev/peps/pep-0454] – Add a new tracemalloc module to trace Python memory allocations

PEP 由 Victor Stinner 撰写并实现

改进的模块

abc

New function abc.get_cache_token() can be used to know when to invalidate caches that are affected by changes in the object graph. (Contributed by Łukasz Langa in bpo-16832 [https://bugs.python.org/issue16832].)

New class ABC has ABCMeta as its meta class. Using ABC as a base class has essentially the same effect as specifying metaclass=abc.ABCMeta, but is simpler to type and easier to read. (Contributed by Bruno Dupuis in bpo-16049 [https://bugs.python.org/issue16049].)

aifc

The getparams() method now returns a namedtuple rather than a plain tuple. (Contributed by Claudiu Popa in bpo-17818 [https://bugs.python.org/issue17818].)

aifc.open() now supports the context management protocol: when used in a with block, the close() method of the returned object will be called automatically at the end of the block. (Contributed by Serhiy Storchacha in bpo-16486 [https://bugs.python.org/issue16486].)

The writeframesraw() and writeframes() methods now accept any bytes-like object. (Contributed by Serhiy Storchaka in bpo-8311 [https://bugs.python.org/issue8311].)

argparse

The FileType class now accepts encoding and errors arguments, which are passed through to open(). (Contributed by Lucas Maystre in bpo-11175 [https://bugs.python.org/issue11175].)

audioop

audioop now supports 24-bit samples. (Contributed by Serhiy Storchaka in bpo-12866 [https://bugs.python.org/issue12866].)

New byteswap() function converts big-endian samples to little-endian and vice versa. (Contributed by Serhiy Storchaka in bpo-19641 [https://bugs.python.org/issue19641].)

All audioop functions now accept any bytes-like object. Strings are not accepted: they didn’t work before, now they raise an error right away. (Contributed by Serhiy Storchaka in bpo-16685 [https://bugs.python.org/issue16685].)

base64

The encoding and decoding functions in base64 now accept any bytes-like object in cases where it previously required a bytes or bytearray instance. (Contributed by Nick Coghlan in bpo-17839 [https://bugs.python.org/issue17839].)

New functions a85encode(), a85decode(), b85encode(), and b85decode() provide the ability to encode and decode binary data from and to Ascii85 and the git/mercurial Base85 formats, respectively. The a85 functions have options that can be used to make them compatible with the variants of the Ascii85 encoding, including the Adobe variant. (Contributed by Martin Morrison, the Mercurial project, Serhiy Storchaka, and Antoine Pitrou in bpo-17618 [https://bugs.python.org/issue17618].)

collections

The ChainMap.new_child() method now accepts an m argument specifying the child map to add to the chain. This allows an existing mapping and/or a custom mapping type to be used for the child. (Contributed by Vinay Sajip in bpo-16613 [https://bugs.python.org/issue16613].)

colorsys

The number of digits in the coefficients for the RGB — YIQ conversions have been expanded so that they match the FCC NTSC versions. The change in results should be less than 1% and may better match results found elsewhere. (Contributed by Brian Landers and Serhiy Storchaka in bpo-14323 [https://bugs.python.org/issue14323].)

contextlib

The new contextlib.suppress context manager helps to clarify the intent of code that deliberately suppresses exceptions from a single statement. (Contributed by Raymond Hettinger in bpo-15806 [https://bugs.python.org/issue15806] and Zero Piraeus in bpo-19266 [https://bugs.python.org/issue19266].)

The new contextlib.redirect_stdout() context manager makes it easier for utility scripts to handle inflexible APIs that write their output to sys.stdout and don’t provide any options to redirect it. Using the context manager, the sys.stdout output can be redirected to any other stream or, in conjunction with io.StringIO, to a string. The latter can be especially useful, for example, to capture output from a function that was written to implement a command line interface. It is recommended only for utility scripts because it affects the global state of sys.stdout. (Contributed by Raymond Hettinger in bpo-15805 [https://bugs.python.org/issue15805].)

The contextlib documentation has also been updated to include a discussion of the differences between single use, reusable and reentrant context managers.

dbm

dbm.open() objects now support the context management protocol. When used in a with statement, the close method of the database object will be called automatically at the end of the block. (Contributed by Claudiu Popa and Nick Coghlan in bpo-19282 [https://bugs.python.org/issue19282].)

dis

Functions show_code(), dis(), distb(), and disassemble() now accept a keyword-only file argument that controls where they write their output.

The dis module is now built around an Instruction class that provides object oriented access to the details of each individual bytecode operation.

A new method, get_instructions(), provides an iterator that emits the Instruction stream for a given piece of Python code. Thus it is now possible to write a program that inspects and manipulates a bytecode object in ways different from those provided by the dis module itself. For example:

>>> import dis
>>> for instr in dis.get_instructions(lambda x: x + 1):
...     print(instr.opname)
LOAD_FAST
LOAD_CONST
BINARY_ADD
RETURN_VALUE

The various display tools in the dis module have been rewritten to use these new components.

In addition, a new application-friendly class Bytecode provides an object-oriented API for inspecting bytecode in both in human-readable form and for iterating over instructions. The Bytecode constructor takes the same arguments that get_instruction() does (plus an optional current_offset), and the resulting object can be iterated to produce Instruction objects. But it also has a dis method, equivalent to calling dis on the constructor argument, but returned as a multi-line string:

>>> bytecode = dis.Bytecode(lambda x: x + 1, current_offset=3)
>>> for instr in bytecode:
...     print('{} ({})'.format(instr.opname, instr.opcode))
LOAD_FAST (124)
LOAD_CONST (100)
BINARY_ADD (23)
RETURN_VALUE (83)
>>> bytecode.dis().splitlines()
['  1           0 LOAD_FAST                0 (x)',
 '      -->     3 LOAD_CONST               1 (1)',
 '              6 BINARY_ADD',
 '              7 RETURN_VALUE']

Bytecode also has a class method, from_traceback(), that provides the ability to manipulate a traceback (that is, print(Bytecode.from_traceback(tb).dis()) is equivalent to distb(tb)).

(Contributed by Nick Coghlan, Ryan Kelly and Thomas Kluyver in bpo-11816 [https://bugs.python.org/issue11816] and Claudiu Popa in bpo-17916 [https://bugs.python.org/issue17916].)

New function stack_effect() computes the effect on the Python stack of a given opcode and argument, information that is not otherwise available. (Contributed by Larry Hastings in bpo-19722 [https://bugs.python.org/issue19722].)

doctest

A new option flag, FAIL_FAST, halts test running as soon as the first failure is detected. (Contributed by R. David Murray and Daniel Urban in bpo-16522 [https://bugs.python.org/issue16522].)

The doctest command line interface now uses argparse, and has two new options, -o and -f. -o allows doctest options to be specified on the command line, and -f is a shorthand for -o FAIL_FAST (to parallel the similar option supported by the unittest CLI). (Contributed by R. David Murray in bpo-11390 [https://bugs.python.org/issue11390].)

doctest will now find doctests in extension module __doc__ strings. (Contributed by Zachary Ware in bpo-3158 [https://bugs.python.org/issue3158].)

email

as_string() now accepts a policy argument to override the default policy of the message when generating a string representation of it. This means that as_string can now be used in more circumstances, instead of having to create and use a generator in order to pass formatting parameters to its flatten method. (Contributed by R. David Murray in bpo-18600 [https://bugs.python.org/issue18600].)

New method as_bytes() added to produce a bytes representation of the message in a fashion similar to how as_string produces a string representation. It does not accept the maxheaderlen argument, but does accept the unixfrom and policy arguments. The Message __bytes__() method calls it, meaning that bytes(mymsg) will now produce the intuitive result: a bytes object containing the fully formatted message. (Contributed by R. David Murray in bpo-18600 [https://bugs.python.org/issue18600].)

The Message.set_param() message now accepts a replace keyword argument. When specified, the associated header will be updated without changing its location in the list of headers. For backward compatibility, the default is False. (Contributed by R. David Murray in bpo-18891 [https://bugs.python.org/issue18891].)

A pair of new subclasses of Message have been added (EmailMessage and MIMEPart), along with a new sub-module, contentmanager and a new policy attribute content_manager. All documentation is currently in the new module, which is being added as part of email’s new provisional API. These classes provide a number of new methods that make extracting content from and inserting content into email messages much easier. For details, see the contentmanager documentation and the email: 示例. These API additions complete the bulk of the work that was planned as part of the email6 project. The currently provisional API is scheduled to become final in Python 3.5 (possibly with a few minor additions in the area of error handling). (Contributed by R. David Murray in bpo-18891 [https://bugs.python.org/issue18891].)

filecmp

A new clear_cache() function provides the ability to clear the filecmp comparison cache, which uses os.stat() information to determine if the file has changed since the last compare. This can be used, for example, if the file might have been changed and re-checked in less time than the resolution of a particular filesystem’s file modification time field. (Contributed by Mark Levitt in bpo-18149 [https://bugs.python.org/issue18149].)

New module attribute DEFAULT_IGNORES provides the list of directories that are used as the default value for the ignore parameter of the dircmp() function. (Contributed by Eli Bendersky in bpo-15442 [https://bugs.python.org/issue15442].)

functools

The new partialmethod() descriptor brings partial argument application to descriptors, just as partial() provides for normal callables. The new descriptor also makes it easier to get arbitrary callables (including partial() instances) to behave like normal instance methods when included in a class definition. (Contributed by Alon Horev and Nick Coghlan in bpo-4331 [https://bugs.python.org/issue4331].)

The new singledispatch() decorator brings support for single-dispatch generic functions to the Python standard library. Where object oriented programming focuses on grouping multiple operations on a common set of data into a class, a generic function focuses on grouping multiple implementations of an operation that allows it to work with different kinds of data.

参见

PEP 443 [https://www.python.org/dev/peps/pep-0443] – Single-dispatch generic functions

PEP 由 Łukasz Langa 撰写并实现。

total_ordering() now supports a return value of NotImplemented from the underlying comparison function. (Contributed by Katie Miller in bpo-10042 [https://bugs.python.org/issue10042].)

A pure-python version of the partial() function is now in the stdlib; in CPython it is overridden by the C accelerated version, but it is available for other implementations to use. (Contributed by Brian Thorne in bpo-12428 [https://bugs.python.org/issue12428].)

gc

New function get_stats() returns a list of three per-generation dictionaries containing the collections statistics since interpreter startup. (Contributed by Antoine Pitrou in bpo-16351 [https://bugs.python.org/issue16351].)

glob

A new function escape() provides a way to escape special characters in a filename so that they do not become part of the globbing expansion but are instead matched literally. (Contributed by Serhiy Storchaka in bpo-8402 [https://bugs.python.org/issue8402].)

hashlib

A new hashlib.pbkdf2_hmac() function provides the PKCS#5 password-based key derivation function 2 [https://en.wikipedia.org/wiki/PBKDF2]. (Contributed by Christian Heimes in bpo-18582 [https://bugs.python.org/issue18582].)

The name attribute of hashlib hash objects is now a formally supported interface. It has always existed in CPython’s hashlib (although it did not return lower case names for all supported hashes), but it was not a public interface and so some other Python implementations have not previously supported it. (Contributed by Jason R. Coombs in bpo-18532 [https://bugs.python.org/issue18532].)

hmac

hmac now accepts bytearray as well as bytes for the key argument to the new() function, and the msg parameter to both the new() function and the update() method now accepts any type supported by the hashlib module. (Contributed by Jonas Borgström in bpo-18240 [https://bugs.python.org/issue18240].)

The digestmod argument to the hmac.new() function may now be any hash digest name recognized by hashlib. In addition, the current behavior in which the value of digestmod defaults to MD5 is deprecated: in a future version of Python there will be no default value. (Contributed by Christian Heimes in bpo-17276 [https://bugs.python.org/issue17276].)

With the addition of block_size and name attributes (and the formal documentation of the digest_size attribute), the hmac module now conforms fully to the PEP 247 [https://www.python.org/dev/peps/pep-0247] API. (Contributed by Christian Heimes in bpo-18775 [https://bugs.python.org/issue18775].)

html

New function unescape() function converts HTML5 character references to the corresponding Unicode characters. (Contributed by Ezio Melotti in bpo-2927 [https://bugs.python.org/issue2927].)

HTMLParser accepts a new keyword argument convert_charrefs that, when True, automatically converts all character references. For backward-compatibility, its value defaults to False, but it will change to True in a future version of Python, so you are invited to set it explicitly and update your code to use this new feature. (Contributed by Ezio Melotti in bpo-13633 [https://bugs.python.org/issue13633].)

The strict argument of HTMLParser is now deprecated. (Contributed by Ezio Melotti in bpo-15114 [https://bugs.python.org/issue15114].)

http

send_error() now accepts an optional additional explain parameter which can be used to provide an extended error description, overriding the hardcoded default if there is one. This extended error description will be formatted using the error_message_format attribute and sent as the body of the error response. (Contributed by Karl Cow in bpo-12921 [https://bugs.python.org/issue12921].)

The http.server command line interface now has a -b/--bind option that causes the server to listen on a specific address. (Contributed by Malte Swart in bpo-17764 [https://bugs.python.org/issue17764].)

idlelib 与 IDLE

Since idlelib implements the IDLE shell and editor and is not intended for import by other programs, it gets improvements with every release. See Lib/idlelib/NEWS.txt for a cumulative list of changes since 3.3.0, as well as changes made in future 3.4.x releases. This file is also available from the IDLE Help ‣ About IDLE dialog.

importlib

The InspectLoader ABC defines a new method, source_to_code() that accepts source data and a path and returns a code object. The default implementation is equivalent to compile(data, path, 'exec', dont_inherit=True). (Contributed by Eric Snow and Brett Cannon in bpo-15627 [https://bugs.python.org/issue15627].)

InspectLoader also now has a default implementation for the get_code() method. However, it will normally be desirable to override the default implementation for performance reasons. (Contributed by Brett Cannon in bpo-18072 [https://bugs.python.org/issue18072].)

The reload() function has been moved from imp to importlib as part of the imp module deprecation. (Contributed by Berker Peksag in bpo-18193 [https://bugs.python.org/issue18193].)

importlib.util now has a MAGIC_NUMBER attribute providing access to the bytecode version number. This replaces the get_magic() function in the deprecated imp module. (Contributed by Brett Cannon in bpo-18192 [https://bugs.python.org/issue18192].)

New importlib.util functions cache_from_source() and source_from_cache() replace the same-named functions in the deprecated imp module. (Contributed by Brett Cannon in bpo-18194 [https://bugs.python.org/issue18194].)

The importlib bootstrap NamespaceLoader now conforms to the InspectLoader ABC, which means that runpy and python -m can now be used with namespace packages. (Contributed by Brett Cannon in bpo-18058 [https://bugs.python.org/issue18058].)

importlib.util has a new function decode_source() that decodes source from bytes using universal newline processing. This is useful for implementing InspectLoader.get_source() methods.

importlib.machinery.ExtensionFileLoader now has a get_filename() method. This was inadvertently omitted in the original implementation. (Contributed by Eric Snow in bpo-19152 [https://bugs.python.org/issue19152].)

inspect

The inspect module now offers a basic command line interface to quickly display source code and other information for modules, classes and functions. (Contributed by Claudiu Popa and Nick Coghlan in bpo-18626 [https://bugs.python.org/issue18626].)

unwrap() makes it easy to unravel wrapper function chains created by functools.wraps() (and any other API that sets the __wrapped__ attribute on a wrapper function). (Contributed by Daniel Urban, Aaron Iles and Nick Coghlan in bpo-13266 [https://bugs.python.org/issue13266].)

As part of the implementation of the new enum module, the inspect module now has substantially better support for custom __dir__ methods and dynamic class attributes provided through metaclasses. (Contributed by Ethan Furman in bpo-18929 [https://bugs.python.org/issue18929] and bpo-19030 [https://bugs.python.org/issue19030].)

getfullargspec() and getargspec() now use the signature() API. This allows them to support a much broader range of callables, including those with __signature__ attributes, those with metadata provided by argument clinic, functools.partial() objects and more. Note that, unlike signature(), these functions still ignore __wrapped__ attributes, and report the already bound first argument for bound methods, so it is still necessary to update your code to use signature() directly if those features are desired. (Contributed by Yury Selivanov in bpo-17481 [https://bugs.python.org/issue17481].)

signature() now supports duck types of CPython functions, which adds support for functions compiled with Cython. (Contributed by Stefan Behnel and Yury Selivanov in bpo-17159 [https://bugs.python.org/issue17159].)

ipaddress

ipaddress was added to the standard library in Python 3.3 as a provisional API. With the release of Python 3.4, this qualification has been removed: ipaddress is now considered a stable API, covered by the normal standard library requirements to maintain backwards compatibility.

A new is_global property is True if an address is globally routeable. (Contributed by Peter Moody in bpo-17400 [https://bugs.python.org/issue17400].)

logging

The TimedRotatingFileHandler has a new atTime parameter that can be used to specify the time of day when rollover should happen. (Contributed by Ronald Oussoren in bpo-9556 [https://bugs.python.org/issue9556].)

SocketHandler and DatagramHandler now support Unix domain sockets (by setting port to None). (Contributed by Vinay Sajip in commit ce46195b56a9.)

fileConfig() now accepts a configparser.RawConfigParser subclass instance for the fname parameter. This facilitates using a configuration file when logging configuration is just a part of the overall application configuration, or where the application modifies the configuration before passing it to fileConfig(). (Contributed by Vinay Sajip in bpo-16110 [https://bugs.python.org/issue16110].)

Logging configuration data received from a socket via the logging.config.listen() function can now be validated before being processed by supplying a verification function as the argument to the new verify keyword argument. (Contributed by Vinay Sajip in bpo-15452 [https://bugs.python.org/issue15452].)

marshal

The default marshal version has been bumped to 3. The code implementing the new version restores the Python2 behavior of recording only one copy of interned strings and preserving the interning on deserialization, and extends this “one copy” ability to any object type (including handling recursive references). This reduces both the size of .pyc files and the amount of memory a module occupies in memory when it is loaded from a .pyc (or .pyo) file. (Contributed by Kristján Valur Jónsson in bpo-16475 [https://bugs.python.org/issue16475], with additional speedups by Antoine Pitrou in bpo-19219 [https://bugs.python.org/issue19219].)

mmap

mmap objects can now be weakrefed. (Contributed by Valerie Lambert in bpo-4885 [https://bugs.python.org/issue4885].)

multiprocessing

On Unix two new start methods, spawn and forkserver, have been added for starting processes using multiprocessing. These make the mixing of processes with threads more robust, and the spawn method matches the semantics that multiprocessing has always used on Windows. New function get_all_start_methods() reports all start methods available on the platform, get_start_method() reports the current start method, and set_start_method() sets the start method. (Contributed by Richard Oudkerk in bpo-8713 [https://bugs.python.org/issue8713].)

multiprocessing also now has the concept of a context, which determines how child processes are created. New function get_context() returns a context that uses a specified start method. It has the same API as the multiprocessing module itself, so you can use it to create Pools and other objects that will operate within that context. This allows a framework and an application or different parts of the same application to use multiprocessing without interfering with each other. (Contributed by Richard Oudkerk in bpo-18999 [https://bugs.python.org/issue18999].)

Except when using the old fork start method, child processes no longer inherit unneeded handles/file descriptors from their parents (part of bpo-8713 [https://bugs.python.org/issue8713]).

multiprocessing now relies on runpy (which implements the -m switch) to initialise __main__ appropriately in child processes when using the spawn or forkserver start methods. This resolves some edge cases where combining multiprocessing, the -m command line switch, and explicit relative imports could cause obscure failures in child processes. (Contributed by Nick Coghlan in bpo-19946 [https://bugs.python.org/issue19946].)

operator

New function length_hint() provides an implementation of the specification for how the __length_hint__() special method should be used, as part of the PEP 424 [https://www.python.org/dev/peps/pep-0424] formal specification of this language feature. (Contributed by Armin Ronacher in bpo-16148 [https://bugs.python.org/issue16148].)

There is now a pure-python version of the operator module available for reference and for use by alternate implementations of Python. (Contributed by Zachary Ware in bpo-16694 [https://bugs.python.org/issue16694].)

os

There are new functions to get and set the inheritable flag of a file descriptor (os.get_inheritable(), os.set_inheritable()) or a Windows handle (os.get_handle_inheritable(), os.set_handle_inheritable()).

New function cpu_count() reports the number of CPUs available on the platform on which Python is running (or None if the count can’t be determined). The multiprocessing.cpu_count() function is now implemented in terms of this function). (Contributed by Trent Nelson, Yogesh Chaudhari, Victor Stinner, and Charles-François Natali in bpo-17914 [https://bugs.python.org/issue17914].)

os.path.samestat() is now available on the Windows platform (and the os.path.samefile() implementation is now shared between Unix and Windows). (Contributed by Brian Curtin in bpo-11939 [https://bugs.python.org/issue11939].)

os.path.ismount() now recognizes volumes mounted below a drive root on Windows. (Contributed by Tim Golden in bpo-9035 [https://bugs.python.org/issue9035].)

os.open() supports two new flags on platforms that provide them, O_PATH (un-opened file descriptor), and O_TMPFILE (unnamed temporary file; as of 3.4.0 release available only on Linux systems with a kernel version of 3.11 or newer that have uapi headers). (Contributed by Christian Heimes in bpo-18673 [https://bugs.python.org/issue18673] and Benjamin Peterson, respectively.)

pdb

pdb has been enhanced to handle generators, yield, and yield from in a more useful fashion. This is especially helpful when debugging asyncio based programs. (Contributed by Andrew Svetlov and Xavier de Gaye in bpo-16596 [https://bugs.python.org/issue16596].)

The print command has been removed from pdb, restoring access to the Python print() function from the pdb command line. Python2’s pdb did not have a print command; instead, entering print executed the print statement. In Python3 print was mistakenly made an alias for the pdb p command. p, however, prints the repr of its argument, not the str like the Python2 print command did. Worse, the Python3 pdb print command shadowed the Python3 print function, making it inaccessible at the pdb prompt. (Contributed by Connor Osborn in bpo-18764 [https://bugs.python.org/issue18764].)

pickle

pickle now supports (but does not use by default) a new pickle protocol, protocol 4. This new protocol addresses a number of issues that were present in previous protocols, such as the serialization of nested classes, very large strings and containers, and classes whose __new__() method takes keyword-only arguments. It also provides some efficiency improvements.

参见

PEP 3154 [https://www.python.org/dev/peps/pep-3154] – Pickle protocol 4

PEP 由 Antoine Pitrou 撰写，并由 Alexandre Vassalotti 实现

plistlib

plistlib now has an API that is similar to the standard pattern for stdlib serialization protocols, with new load(), dump(), loads(), and dumps() functions. (The older API is now deprecated.) In addition to the already supported XML plist format (FMT_XML), it also now supports the binary plist format (FMT_BINARY). (Contributed by Ronald Oussoren and others in bpo-14455 [https://bugs.python.org/issue14455].)

poplib

Two new methods have been added to poplib: capa(), which returns the list of capabilities advertised by the POP server, and stls(), which switches a clear-text POP3 session into an encrypted POP3 session if the POP server supports it. (Contributed by Lorenzo Catucci in bpo-4473 [https://bugs.python.org/issue4473].)

pprint

The pprint module’s PrettyPrinter class and its pformat(), and pprint() functions have a new option, compact, that controls how the output is formatted. Currently setting compact to True means that sequences will be printed with as many sequence elements as will fit within width on each (indented) line. (Contributed by Serhiy Storchaka in bpo-19132 [https://bugs.python.org/issue19132].)

Long strings are now wrapped using Python’s normal line continuation syntax. (Contributed by Antoine Pitrou in bpo-17150 [https://bugs.python.org/issue17150].)

pty

pty.spawn() now returns the status value from os.waitpid() on the child process, instead of None. (Contributed by Gregory P. Smith.)

pydoc

The pydoc module is now based directly on the inspect.signature() introspection API, allowing it to provide signature information for a wider variety of callable objects. This change also means that __wrapped__ attributes are now taken into account when displaying help information. (Contributed by Larry Hastings in bpo-19674 [https://bugs.python.org/issue19674].)

The pydoc module no longer displays the self parameter for already bound methods. Instead, it aims to always display the exact current signature of the supplied callable. (Contributed by Larry Hastings in bpo-20710 [https://bugs.python.org/issue20710].)

In addition to the changes that have been made to pydoc directly, its handling of custom __dir__ methods and various descriptor behaviours has also been improved substantially by the underlying changes in the inspect module.

As the help() builtin is based on pydoc, the above changes also affect the behaviour of help().

re

New fullmatch() function and regex.fullmatch() method anchor the pattern at both ends of the string to match. This provides a way to be explicit about the goal of the match, which avoids a class of subtle bugs where $ characters get lost during code changes or the addition of alternatives to an existing regular expression. (Contributed by Matthew Barnett in bpo-16203 [https://bugs.python.org/issue16203].)

The repr of regex objects now includes the pattern and the flags; the repr of match objects now includes the start, end, and the part of the string that matched. (Contributed by Hugo Lopes Tavares and Serhiy Storchaka in bpo-13592 [https://bugs.python.org/issue13592] and bpo-17087 [https://bugs.python.org/issue17087].)

resource

New prlimit() function, available on Linux platforms with a kernel version of 2.6.36 or later and glibc of 2.13 or later, provides the ability to query or set the resource limits for processes other than the one making the call. (Contributed by Christian Heimes in bpo-16595 [https://bugs.python.org/issue16595].)

On Linux kernel version 2.6.36 or later, there are also some new Linux specific constants: RLIMIT_MSGQUEUE, RLIMIT_NICE, RLIMIT_RTPRIO, RLIMIT_RTTIME, and RLIMIT_SIGPENDING. (Contributed by Christian Heimes in bpo-19324 [https://bugs.python.org/issue19324].)

On FreeBSD version 9 and later, there some new FreeBSD specific constants: RLIMIT_SBSIZE, RLIMIT_SWAP, and RLIMIT_NPTS. (Contributed by Claudiu Popa in bpo-19343 [https://bugs.python.org/issue19343].)

select

epoll objects now support the context management protocol. When used in a with statement, the close() method will be called automatically at the end of the block. (Contributed by Serhiy Storchaka in bpo-16488 [https://bugs.python.org/issue16488].)

devpoll objects now have fileno() and close() methods, as well as a new attribute closed. (Contributed by Victor Stinner in bpo-18794 [https://bugs.python.org/issue18794].)

shelve

Shelf instances may now be used in with statements, and will be automatically closed at the end of the with block. (Contributed by Filip Gruszczyński in bpo-13896 [https://bugs.python.org/issue13896].)

shutil

copyfile() now raises a specific Error subclass, SameFileError, when the source and destination are the same file, which allows an application to take appropriate action on this specific error. (Contributed by Atsuo Ishimoto and Hynek Schlawack in bpo-1492704 [https://bugs.python.org/issue1492704].)

smtpd

The SMTPServer and SMTPChannel classes now accept a map keyword argument which, if specified, is passed in to asynchat.async_chat as its map argument. This allows an application to avoid affecting the global socket map. (Contributed by Vinay Sajip in bpo-11959 [https://bugs.python.org/issue11959].)

smtplib

SMTPException is now a subclass of OSError, which allows both socket level errors and SMTP protocol level errors to be caught in one try/except statement by code that only cares whether or not an error occurred. (Contributed by Ned Jackson Lovely in bpo-2118 [https://bugs.python.org/issue2118].)

socket

The socket module now supports the CAN_BCM protocol on platforms that support it. (Contributed by Brian Thorne in bpo-15359 [https://bugs.python.org/issue15359].)

Socket objects have new methods to get or set their inheritable flag, get_inheritable() and set_inheritable().

The socket.AF_* and socket.SOCK_* constants are now enumeration values using the new enum module. This allows meaningful names to be printed during debugging, instead of integer “magic numbers”.

The AF_LINK constant is now available on BSD and OSX.

inet_pton() and inet_ntop() are now supported on Windows. (Contributed by Atsuo Ishimoto in bpo-7171 [https://bugs.python.org/issue7171].)

sqlite3

A new boolean parameter to the connect() function, uri, can be used to indicate that the database parameter is a uri (see the SQLite URI documentation [https://www.sqlite.org/uri.html]). (Contributed by poq in bpo-13773 [https://bugs.python.org/issue13773].)

ssl

PROTOCOL_TLSv1_1 and PROTOCOL_TLSv1_2 (TLSv1.1 and TLSv1.2 support) have been added; support for these protocols is only available if Python is linked with OpenSSL 1.0.1 or later. (Contributed by Michele Orrù and Antoine Pitrou in bpo-16692 [https://bugs.python.org/issue16692].)

New function create_default_context() provides a standard way to obtain an SSLContext whose settings are intended to be a reasonable balance between compatibility and security. These settings are more stringent than the defaults provided by the SSLContext constructor, and may be adjusted in the future, without prior deprecation, if best-practice security requirements change. The new recommended best practice for using stdlib libraries that support SSL is to use create_default_context() to obtain an SSLContext object, modify it if needed, and then pass it as the context argument of the appropriate stdlib API. (Contributed by Christian Heimes in bpo-19689 [https://bugs.python.org/issue19689].)

SSLContext method load_verify_locations() accepts a new optional argument cadata, which can be used to provide PEM or DER encoded certificates directly via strings or bytes, respectively. (Contributed by Christian Heimes in bpo-18138 [https://bugs.python.org/issue18138].)

New function get_default_verify_paths() returns a named tuple of the paths and environment variables that the set_default_verify_paths() method uses to set OpenSSL’s default cafile and capath. This can be an aid in debugging default verification issues. (Contributed by Christian Heimes in bpo-18143 [https://bugs.python.org/issue18143].)

SSLContext has a new method, cert_store_stats(), that reports the number of loaded X.509 certs, X.509 CA certs, and certificate revocation lists (crls), as well as a get_ca_certs() method that returns a list of the loaded CA certificates. (Contributed by Christian Heimes in bpo-18147 [https://bugs.python.org/issue18147].)

If OpenSSL 0.9.8 or later is available, SSLContext has a new attribute verify_flags that can be used to control the certificate verification process by setting it to some combination of the new constants VERIFY_DEFAULT, VERIFY_CRL_CHECK_LEAF, VERIFY_CRL_CHECK_CHAIN, or VERIFY_X509_STRICT. OpenSSL does not do any CRL verification by default. (Contributed by Christien Heimes in bpo-8813 [https://bugs.python.org/issue8813].)

New SSLContext method load_default_certs() loads a set of default “certificate authority” (CA) certificates from default locations, which vary according to the platform. It can be used to load both TLS web server authentication certificates (purpose=SERVER_AUTH) for a client to use to verify a server, and certificates for a server to use in verifying client certificates (purpose=CLIENT_AUTH). (Contributed by Christian Heimes in bpo-19292 [https://bugs.python.org/issue19292].)

Two new windows-only functions, enum_certificates() and enum_crls() provide the ability to retrieve certificates, certificate information, and CRLs from the Windows cert store. (Contributed by Christian Heimes in bpo-17134 [https://bugs.python.org/issue17134].)

Support for server-side SNI (Server Name Indication) using the new ssl.SSLContext.set_servername_callback() method. (Contributed by Daniel Black in bpo-8109 [https://bugs.python.org/issue8109].)

The dictionary returned by SSLSocket.getpeercert() contains additional X509v3 extension items: crlDistributionPoints, calIssuers, and OCSP URIs. (Contributed by Christian Heimes in bpo-18379 [https://bugs.python.org/issue18379].)

stat

The stat module is now backed by a C implementation in _stat. A C implementation is required as most of the values aren’t standardized and are platform-dependent. (Contributed by Christian Heimes in bpo-11016 [https://bugs.python.org/issue11016].)

The module supports new ST_MODE flags, S_IFDOOR, S_IFPORT, and S_IFWHT. (Contributed by Christian Hiemes in bpo-11016 [https://bugs.python.org/issue11016].)

struct

New function iter_unpack and a new struct.Struct.iter_unpack() method on compiled formats provide streamed unpacking of a buffer containing repeated instances of a given format of data. (Contributed by Antoine Pitrou in bpo-17804 [https://bugs.python.org/issue17804].)

subprocess

check_output() now accepts an input argument that can be used to provide the contents of stdin for the command that is run. (Contributed by Zack Weinberg in bpo-16624 [https://bugs.python.org/issue16624].)

getstatus() and getstatusoutput() now work on Windows. This change was actually inadvertently made in 3.3.4. (Contributed by Tim Golden in bpo-10197 [https://bugs.python.org/issue10197].)

sunau

The getparams() method now returns a namedtuple rather than a plain tuple. (Contributed by Claudiu Popa in bpo-18901 [https://bugs.python.org/issue18901].)

sunau.open() now supports the context management protocol: when used in a with block, the close method of the returned object will be called automatically at the end of the block. (Contributed by Serhiy Storchaka in bpo-18878 [https://bugs.python.org/issue18878].)

AU_write.setsampwidth() now supports 24 bit samples, thus adding support for writing 24 sample using the module. (Contributed by Serhiy Storchaka in bpo-19261 [https://bugs.python.org/issue19261].)

The writeframesraw() and writeframes() methods now accept any bytes-like object. (Contributed by Serhiy Storchaka in bpo-8311 [https://bugs.python.org/issue8311].)

sys

New function sys.getallocatedblocks() returns the current number of blocks allocated by the interpreter. (In CPython with the default --with-pymalloc setting, this is allocations made through the PyObject_Malloc() API.) This can be useful for tracking memory leaks, especially if automated via a test suite. (Contributed by Antoine Pitrou in bpo-13390 [https://bugs.python.org/issue13390].)

When the Python interpreter starts in interactive mode, it checks for an __interactivehook__ attribute on the sys module. If the attribute exists, its value is called with no arguments just before interactive mode is started. The check is made after the PYTHONSTARTUP file is read, so it can be set there. The site module sets it to a function that enables tab completion and history saving (in ~/.python-history) if the platform supports readline. If you do not want this (new) behavior, you can override it in PYTHONSTARTUP, sitecustomize, or usercustomize by deleting this attribute from sys (or setting it to some other callable). (Contributed by Éric Araujo and Antoine Pitrou in bpo-5845 [https://bugs.python.org/issue5845].)

tarfile

The tarfile module now supports a simple 命令行界面 when called as a script directly or via -m. This can be used to create and extract tarfile archives. (Contributed by Berker Peksag in bpo-13477 [https://bugs.python.org/issue13477].)

textwrap

The TextWrapper class has two new attributes/constructor arguments: max_lines, which limits the number of lines in the output, and placeholder, which is a string that will appear at the end of the output if it has been truncated because of max_lines. Building on these capabilities, a new convenience function shorten() collapses all of the whitespace in the input to single spaces and produces a single line of a given width that ends with the placeholder (by default, [...]). (Contributed by Antoine Pitrou and Serhiy Storchaka in bpo-18585 [https://bugs.python.org/issue18585] and bpo-18725 [https://bugs.python.org/issue18725].)

threading

The Thread object representing the main thread can be obtained from the new main_thread() function. In normal conditions this will be the thread from which the Python interpreter was started. (Contributed by Andrew Svetlov in bpo-18882 [https://bugs.python.org/issue18882].)

回溯

A new traceback.clear_frames() function takes a traceback object and clears the local variables in all of the frames it references, reducing the amount of memory consumed. (Contributed by Andrew Kuchling in bpo-1565525 [https://bugs.python.org/issue1565525].)

types

A new DynamicClassAttribute() descriptor provides a way to define an attribute that acts normally when looked up through an instance object, but which is routed to the class __getattr__ when looked up through the class. This allows one to have properties active on a class, and have virtual attributes on the class with the same name (see Enum for an example). (Contributed by Ethan Furman in bpo-19030 [https://bugs.python.org/issue19030].)

urllib

urllib.request now supports data: URLs via the DataHandler class. (Contributed by Mathias Panzenböck in bpo-16423 [https://bugs.python.org/issue16423].)

The http method that will be used by a Request class can now be specified by setting a method class attribute on the subclass. (Contributed by Jason R Coombs in bpo-18978 [https://bugs.python.org/issue18978].)

Request objects are now reusable: if the full_url or data attributes are modified, all relevant internal properties are updated. This means, for example, that it is now possible to use the same Request object in more than one OpenerDirector.open() call with different data arguments, or to modify a Request’s url rather than recomputing it from scratch. There is also a new remove_header() method that can be used to remove headers from a Request. (Contributed by Alexey Kachayev in bpo-16464 [https://bugs.python.org/issue16464], Daniel Wozniak in bpo-17485 [https://bugs.python.org/issue17485], and Damien Brecht and Senthil Kumaran in bpo-17272 [https://bugs.python.org/issue17272].)

HTTPError objects now have a headers attribute that provides access to the HTTP response headers associated with the error. (Contributed by Berker Peksag in bpo-15701 [https://bugs.python.org/issue15701].)

unittest

The TestCase class has a new method, subTest(), that produces a context manager whose with block becomes a “sub-test”. This context manager allows a test method to dynamically generate subtests by, say, calling the subTest context manager inside a loop. A single test method can thereby produce an indefinite number of separately-identified and separately-counted tests, all of which will run even if one or more of them fail. For example:

class NumbersTest(unittest.TestCase):
    def test_even(self):
        for i in range(6):
            with self.subTest(i=i):
                self.assertEqual(i % 2, 0)

will result in six subtests, each identified in the unittest verbose output with a label consisting of the variable name i and a particular value for that variable (i=0, i=1, etc). See Distinguishing test iterations using subtests for the full version of this example. (Contributed by Antoine Pitrou in bpo-16997 [https://bugs.python.org/issue16997].)

unittest.main() now accepts an iterable of test names for defaultTest, where previously it only accepted a single test name as a string. (Contributed by Jyrki Pulliainen in bpo-15132 [https://bugs.python.org/issue15132].)

If SkipTest is raised during test discovery (that is, at the module level in the test file), it is now reported as a skip instead of an error. (Contributed by Zach Ware in bpo-16935 [https://bugs.python.org/issue16935].)

discover() now sorts the discovered files to provide consistent test ordering. (Contributed by Martin Melin and Jeff Ramnani in bpo-16709 [https://bugs.python.org/issue16709].)

TestSuite now drops references to tests as soon as the test has been run, if the test is successful. On Python interpreters that do garbage collection, this allows the tests to be garbage collected if nothing else is holding a reference to the test. It is possible to override this behavior by creating a TestSuite subclass that defines a custom _removeTestAtIndex method. (Contributed by Tom Wardill, Matt McClure, and Andrew Svetlov in bpo-11798 [https://bugs.python.org/issue11798].)

A new test assertion context-manager, assertLogs(), will ensure that a given block of code emits a log message using the logging module. By default the message can come from any logger and have a priority of INFO or higher, but both the logger name and an alternative minimum logging level may be specified. The object returned by the context manager can be queried for the LogRecords and/or formatted messages that were logged. (Contributed by Antoine Pitrou in bpo-18937 [https://bugs.python.org/issue18937].)

Test discovery now works with namespace packages (Contributed by Claudiu Popa in bpo-17457 [https://bugs.python.org/issue17457].)

unittest.mock objects now inspect their specification signatures when matching calls, which means an argument can now be matched by either position or name, instead of only by position. (Contributed by Antoine Pitrou in bpo-17015 [https://bugs.python.org/issue17015].)

mock_open() objects now have readline and readlines methods. (Contributed by Toshio Kuratomi in bpo-17467 [https://bugs.python.org/issue17467].)

venv

venv now includes activation scripts for the csh and fish shells. (Contributed by Andrew Svetlov in bpo-15417 [https://bugs.python.org/issue15417].)

EnvBuilder and the create() convenience function take a new keyword argument with_pip, which defaults to False, that controls whether or not EnvBuilder ensures that pip is installed in the virtual environment. (Contributed by Nick Coghlan in bpo-19552 [https://bugs.python.org/issue19552] as part of the PEP 453 [https://www.python.org/dev/peps/pep-0453] implementation.)

wave

The getparams() method now returns a namedtuple rather than a plain tuple. (Contributed by Claudiu Popa in bpo-17487 [https://bugs.python.org/issue17487].)

wave.open() now supports the context management protocol. (Contributed by Claudiu Popa in bpo-17616 [https://bugs.python.org/issue17616].)

wave can now write output to unseekable files. (Contributed by David Jones, Guilherme Polo, and Serhiy Storchaka in bpo-5202 [https://bugs.python.org/issue5202].)

The writeframesraw() and writeframes() methods now accept any bytes-like object. (Contributed by Serhiy Storchaka in bpo-8311 [https://bugs.python.org/issue8311].)

weakref

New WeakMethod class simulates weak references to bound methods. (Contributed by Antoine Pitrou in bpo-14631 [https://bugs.python.org/issue14631].)

New finalize class makes it possible to register a callback to be invoked when an object is garbage collected, without needing to carefully manage the lifecycle of the weak reference itself. (Contributed by Richard Oudkerk in bpo-15528 [https://bugs.python.org/issue15528].)

The callback, if any, associated with a ref is now exposed via the __callback__ attribute. (Contributed by Mark Dickinson in bpo-17643 [https://bugs.python.org/issue17643].)

xml.etree

A new parser, XMLPullParser, allows a non-blocking applications to parse XML documents. An example can be seen at Pull API进行非阻塞解析. (Contributed by Antoine Pitrou in bpo-17741 [https://bugs.python.org/issue17741].)

The xml.etree.ElementTree tostring() and tostringlist() functions, and the ElementTree write() method, now have a short_empty_elements keyword-only parameter providing control over whether elements with no content are written in abbreviated (<tag />) or expanded (<tag></tag>) form. (Contributed by Ariel Poliak and Serhiy Storchaka in bpo-14377 [https://bugs.python.org/issue14377].)

zipfile

The writepy() method of the PyZipFile class has a new filterfunc option that can be used to control which directories and files are added to the archive. For example, this could be used to exclude test files from the archive. (Contributed by Christian Tismer in bpo-19274 [https://bugs.python.org/issue19274].)

The allowZip64 parameter to ZipFile and PyZipfile is now True by default. (Contributed by William Mallard in bpo-17201 [https://bugs.python.org/issue17201].)

CPython Implementation Changes

PEP 445: Customization of CPython Memory Allocators

PEP 445 [https://www.python.org/dev/peps/pep-0445] adds new C level interfaces to customize memory allocation in the CPython interpreter.

参见

PEP 445 [https://www.python.org/dev/peps/pep-0445] – Add new APIs to customize Python memory allocators

PEP 由 Victor Stinner 撰写并实现。

PEP 442: Safe Object Finalization

PEP 442 [https://www.python.org/dev/peps/pep-0442] removes the current limitations and quirks of object finalization in CPython. With it, objects with __del__() methods, as well as generators with finally clauses, can be finalized when they are part of a reference cycle.

As part of this change, module globals are no longer forcibly set to None during interpreter shutdown in most cases, instead relying on the normal operation of the cyclic garbage collector. This avoids a whole class of interpreter-shutdown-time errors, usually involving __del__ methods, that have plagued Python since the cyclic GC was first introduced.

参见

PEP 442 [https://www.python.org/dev/peps/pep-0442] – Safe object finalization

PEP 由 Antoine Pitrou 撰写并实现

PEP 456: Secure and Interchangeable Hash Algorithm

PEP 456 [https://www.python.org/dev/peps/pep-0456] follows up on earlier security fix work done on Python’s hash algorithm to address certain DOS attacks to which public facing APIs backed by dictionary lookups may be subject. (See bpo-14621 [https://bugs.python.org/issue14621] for the start of the current round of improvements.) The PEP unifies CPython’s hash code to make it easier for a packager to substitute a different hash algorithm, and switches Python’s default implementation to a SipHash implementation on platforms that have a 64 bit data type. Any performance differences in comparison with the older FNV algorithm are trivial.

The PEP adds additional fields to the sys.hash_info struct sequence to describe the hash algorithm in use by the currently executing binary. Otherwise, the PEP does not alter any existing CPython APIs.

PEP 436: Argument Clinic

“Argument Clinic” (PEP 436 [https://www.python.org/dev/peps/pep-0436]) is now part of the CPython build process and can be used to simplify the process of defining and maintaining accurate signatures for builtins and standard library extension modules implemented in C.

Some standard library extension modules have been converted to use Argument Clinic in Python 3.4, and pydoc and inspect have been updated accordingly.

It is expected that signature metadata for programmatic introspection will be added to additional callables implemented in C as part of Python 3.4 maintenance releases.

注解

The Argument Clinic PEP is not fully up to date with the state of the implementation. This has been deemed acceptable by the release manager and core development team in this case, as Argument Clinic will not be made available as a public API for third party use in Python 3.4.

参见

PEP 436 [https://www.python.org/dev/peps/pep-0436] – The Argument Clinic DSL

PEP 由 Larry Hastings 撰写并实现

Other Build and C API Changes

The new PyType_GetSlot() function has been added to the stable ABI, allowing retrieval of function pointers from named type slots when using the limited API. (Contributed by Martin von Löwis in bpo-17162 [https://bugs.python.org/issue17162].)

The new Py_SetStandardStreamEncoding() pre-initialization API allows applications embedding the CPython interpreter to reliably force a particular encoding and error handler for the standard streams. (Contributed by Bastien Montagne and Nick Coghlan in bpo-16129 [https://bugs.python.org/issue16129].)

Most Python C APIs that don’t mutate string arguments are now correctly marked as accepting const char * rather than char *. (Contributed by Serhiy Storchaka in bpo-1772673 [https://bugs.python.org/issue1772673].)

A new shell version of python-config can be used even when a python interpreter is not available (for example, in cross compilation scenarios).

PyUnicode_FromFormat() now supports width and precision specifications for %s, %A, %U, %V, %S, and %R. (Contributed by Ysj Ray and Victor Stinner in bpo-7330 [https://bugs.python.org/issue7330].)

New function PyStructSequence_InitType2() supplements the existing PyStructSequence_InitType() function. The difference is that it returns 0 on success and -1 on failure.

The CPython source can now be compiled using the address sanity checking features of recent versions of GCC and clang: the false alarms in the small object allocator have been silenced. (Contributed by Dhiru Kholia in bpo-18596 [https://bugs.python.org/issue18596].)

The Windows build now uses Address Space Layout Randomization [https://en.wikipedia.org/wiki/Address_space_layout_randomization] and Data Execution Prevention [https://en.wikipedia.org/wiki/Data_Execution_Prevention]. (Contributed by Christian Heimes in bpo-16632 [https://bugs.python.org/issue16632].)

New function PyObject_LengthHint() is the C API equivalent of operator.length_hint(). (Contributed by Armin Ronacher in bpo-16148 [https://bugs.python.org/issue16148].)

其他改进

The python command has a new option, -I, which causes it to run in “isolated mode”, which means that sys.path contains neither the script’s directory nor the user’s site-packages directory, and all PYTHON* environment variables are ignored (it implies both -s and -E). Other restrictions may also be applied in the future, with the goal being to isolate the execution of a script from the user’s environment. This is appropriate, for example, when Python is used to run a system script. On most POSIX systems it can and should be used in the #! line of system scripts. (Contributed by Christian Heimes in bpo-16499 [https://bugs.python.org/issue16499].)

Tab-completion is now enabled by default in the interactive interpreter on systems that support readline. History is also enabled by default, and is written to (and read from) the file ~/.python-history. (Contributed by Antoine Pitrou and Éric Araujo in bpo-5845 [https://bugs.python.org/issue5845].)

Invoking the Python interpreter with --version now outputs the version to standard output instead of standard error (bpo-18338 [https://bugs.python.org/issue18338]). Similar changes were made to argparse (bpo-18920 [https://bugs.python.org/issue18920]) and other modules that have script-like invocation capabilities (bpo-18922 [https://bugs.python.org/issue18922]).

The CPython Windows installer now adds .py to the PATHEXT variable when extensions are registered, allowing users to run a python script at the windows command prompt by just typing its name without the .py extension. (Contributed by Paul Moore in bpo-18569 [https://bugs.python.org/issue18569].)

A new make target coverage-report [https://devguide.python.org/coverage/#measuring-coverage-of-c-code-with-gcov-and-lcov] will build python, run the test suite, and generate an HTML coverage report for the C codebase using gcov and lcov [http://ltp.sourceforge.net/coverage/lcov.php].

The -R option to the python regression test suite now also checks for memory allocation leaks, using sys.getallocatedblocks(). (Contributed by Antoine Pitrou in bpo-13390 [https://bugs.python.org/issue13390].)

python -m now works with namespace packages.

The stat module is now implemented in C, which means it gets the values for its constants from the C header files, instead of having the values hard-coded in the python module as was previously the case.

Loading multiple python modules from a single OS module (.so, .dll) now works correctly (previously it silently returned the first python module in the file). (Contributed by Václav Šmilauer in bpo-16421 [https://bugs.python.org/issue16421].)

A new opcode, LOAD_CLASSDEREF, has been added to fix a bug in the loading of free variables in class bodies that could be triggered by certain uses of __prepare__. (Contributed by Benjamin Peterson in bpo-17853 [https://bugs.python.org/issue17853].)

A number of MemoryError-related crashes were identified and fixed by Victor Stinner using his PEP 445 [https://www.python.org/dev/peps/pep-0445]-based pyfailmalloc tool (bpo-18408 [https://bugs.python.org/issue18408], bpo-18520 [https://bugs.python.org/issue18520]).

The pyvenv command now accepts a --copies option to use copies rather than symlinks even on systems where symlinks are the default. (Contributed by Vinay Sajip in bpo-18807 [https://bugs.python.org/issue18807].)

The pyvenv command also accepts a --without-pip option to suppress the otherwise-automatic bootstrapping of pip into the virtual environment. (Contributed by Nick Coghlan in bpo-19552 [https://bugs.python.org/issue19552] as part of the PEP 453 [https://www.python.org/dev/peps/pep-0453] implementation.)

The encoding name is now optional in the value set for the PYTHONIOENCODING environment variable. This makes it possible to set just the error handler, without changing the default encoding. (Contributed by Serhiy Storchaka in bpo-18818 [https://bugs.python.org/issue18818].)

The bz2, lzma, and gzip module open functions now support x (exclusive creation) mode. (Contributed by Tim Heaney and Vajrasky Kok in bpo-19201 [https://bugs.python.org/issue19201], bpo-19222 [https://bugs.python.org/issue19222], and bpo-19223 [https://bugs.python.org/issue19223].)

Significant Optimizations

The UTF-32 decoder is now 3x to 4x faster. (Contributed by Serhiy Storchaka in bpo-14625 [https://bugs.python.org/issue14625].)

The cost of hash collisions for sets is now reduced. Each hash table probe now checks a series of consecutive, adjacent key/hash pairs before continuing to make random probes through the hash table. This exploits cache locality to make collision resolution less expensive. The collision resolution scheme can be described as a hybrid of linear probing and open addressing. The number of additional linear probes defaults to nine. This can be changed at compile-time by defining LINEAR_PROBES to be any value. Set LINEAR_PROBES=0 to turn-off linear probing entirely. (Contributed by Raymond Hettinger in bpo-18771 [https://bugs.python.org/issue18771].)

The interpreter starts about 30% faster. A couple of measures lead to the speedup. The interpreter loads fewer modules on startup, e.g. the re, collections and locale modules and their dependencies are no longer imported by default. The marshal module has been improved to load compiled Python code faster. (Contributed by Antoine Pitrou, Christian Heimes and Victor Stinner in bpo-19219 [https://bugs.python.org/issue19219], bpo-19218 [https://bugs.python.org/issue19218], bpo-19209 [https://bugs.python.org/issue19209], bpo-19205 [https://bugs.python.org/issue19205] and bpo-9548 [https://bugs.python.org/issue9548].)

bz2.BZ2File is now as fast or faster than the Python2 version for most cases. lzma.LZMAFile has also been optimized. (Contributed by Serhiy Storchaka and Nadeem Vawda in bpo-16034 [https://bugs.python.org/issue16034].)

random.getrandbits() is 20%-40% faster for small integers (the most common use case). (Contributed by Serhiy Storchaka in bpo-16674 [https://bugs.python.org/issue16674].)

By taking advantage of the new storage format for strings, pickling of strings is now significantly faster. (Contributed by Victor Stinner and Antoine Pitrou in bpo-15596 [https://bugs.python.org/issue15596].)

A performance issue in io.FileIO.readall() has been solved. This particularly affects Windows, and significantly speeds up the case of piping significant amounts of data through subprocess. (Contributed by Richard Oudkerk in bpo-15758 [https://bugs.python.org/issue15758].)

html.escape() is now 10x faster. (Contributed by Matt Bryant in bpo-18020 [https://bugs.python.org/issue18020].)

On Windows, the native VirtualAlloc is now used instead of the CRT malloc in obmalloc. Artificial benchmarks show about a 3% memory savings.

os.urandom() now uses a lazily-opened persistent file descriptor so as to avoid using many file descriptors when run in parallel from multiple threads. (Contributed by Antoine Pitrou in bpo-18756 [https://bugs.python.org/issue18756].)

弃用

This section covers various APIs and other features that have been deprecated in Python 3.4, and will be removed in Python 3.5 or later. In most (but not all) cases, using the deprecated APIs will produce a DeprecationWarning when the interpreter is run with deprecation warnings enabled (for example, by using -Wd).

Deprecations in the Python API

As mentioned in PEP 451: A ModuleSpec Type for the Import System, a number of importlib methods and functions are deprecated: importlib.find_loader() is replaced by importlib.util.find_spec(); importlib.machinery.PathFinder.find_module() is replaced by importlib.machinery.PathFinder.find_spec(); importlib.abc.MetaPathFinder.find_module() is replaced by importlib.abc.MetaPathFinder.find_spec(); importlib.abc.PathEntryFinder.find_loader() and find_module() are replaced by importlib.abc.PathEntryFinder.find_spec(); all of the xxxLoader ABC load_module methods (importlib.abc.Loader.load_module(), importlib.abc.InspectLoader.load_module(), importlib.abc.FileLoader.load_module(), importlib.abc.SourceLoader.load_module()) should no longer be implemented, instead loaders should implement an exec_module method (importlib.abc.Loader.exec_module(), importlib.abc.InspectLoader.exec_module() importlib.abc.SourceLoader.exec_module()) and let the import system take care of the rest; and importlib.abc.Loader.module_repr(), importlib.util.module_for_loader(), importlib.util.set_loader(), and importlib.util.set_package() are no longer needed because their functions are now handled automatically by the import system.

The imp module is pending deprecation. To keep compatibility with Python 2/3 code bases, the module’s removal is currently not scheduled.

The formatter module is pending deprecation and is slated for removal in Python 3.6.

MD5 as the default digestmod for the hmac.new() function is deprecated. Python 3.6 will require an explicit digest name or constructor as digestmod argument.

The internal Netrc class in the ftplib module has been documented as deprecated in its docstring for quite some time. It now emits a DeprecationWarning and will be removed completely in Python 3.5.

The undocumented endtime argument to subprocess.Popen.wait() should not have been exposed and is hopefully not in use; it is deprecated and will mostly likely be removed in Python 3.5.

The strict argument of HTMLParser is deprecated.

The plistlib readPlist(), writePlist(), readPlistFromBytes(), and writePlistToBytes() functions are deprecated in favor of the corresponding new functions load(), dump(), loads(), and dumps(). Data() is deprecated in favor of just using the bytes constructor.

The sysconfig key SO is deprecated, it has been replaced by EXT_SUFFIX.

The U mode accepted by various open functions is deprecated. In Python3 it does not do anything useful, and should be replaced by appropriate uses of io.TextIOWrapper (if needed) and its newline argument.

The parser argument of xml.etree.ElementTree.iterparse() has been deprecated, as has the html argument of XMLParser(). To prepare for the removal of the latter, all arguments to XMLParser should be passed by keyword.

Deprecated Features

Running IDLE with the -n flag (no subprocess) is deprecated. However, the feature will not be removed until bpo-18823 [https://bugs.python.org/issue18823] is resolved.

The site module adding a “site-python” directory to sys.path, if it exists, is deprecated (bpo-19375 [https://bugs.python.org/issue19375]).

移除

不再支持的操作系统

从源代码和构建工具中删除了对以下操作系统的支持：

OS/2 (bpo-16135 [https://bugs.python.org/issue16135]).

Windows 2000（变更集e52df05b496a）。

Windows系统中 COMSPEC 指向 command.com 的版本（ bpo-14470 [https://bugs.python.org/issue14470] ）。

VMS (bpo-16136 [https://bugs.python.org/issue16136]).

API 与特性的移除

The following obsolete and previously deprecated APIs and features have been removed:

The unmaintained Misc/TextMate and Misc/vim directories have been removed (see the devguide [https://devguide.python.org] for suggestions on what to use instead).

The SO makefile macro is removed (it was replaced by the SHLIB_SUFFIX and EXT_SUFFIX macros) (bpo-16754 [https://bugs.python.org/issue16754]).

The PyThreadState.tick_counter field has been removed; its value has been meaningless since Python 3.2, when the “new GIL” was introduced (bpo-19199 [https://bugs.python.org/issue19199]).

PyLoader and PyPycLoader have been removed from importlib. (Contributed by Taras Lyapun in bpo-15641 [https://bugs.python.org/issue15641].)

The strict argument to HTTPConnection and HTTPSConnection has been removed. HTTP 0.9-style “Simple Responses” are no longer supported.

The deprecated urllib.request.Request getter and setter methods add_data, has_data, get_data, get_type, get_host, get_selector, set_proxy, get_origin_req_host, and is_unverifiable have been removed (use direct attribute access instead).

Support for loading the deprecated TYPE_INT64 has been removed from marshal. (Contributed by Dan Riti in bpo-15480 [https://bugs.python.org/issue15480].)

inspect.Signature: positional-only parameters are now required to have a valid name.

object.__format__() no longer accepts non-empty format strings, it now raises a TypeError instead. Using a non-empty string has been deprecated since Python 3.2. This change has been made to prevent a situation where previously working (but incorrect) code would start failing if an object gained a __format__ method, which means that your code may now raise a TypeError if you are using an 's' format code with objects that do not have a __format__ method that handles it. See bpo-7994 [https://bugs.python.org/issue7994] for background.

difflib.SequenceMatcher.isbjunk() and difflib.SequenceMatcher.isbpopular() were deprecated in 3.2, and have now been removed: use x in sm.bjunk and x in sm.bpopular, where sm is a SequenceMatcher object (bpo-13248 [https://bugs.python.org/issue13248]).

Code Cleanups

The unused and undocumented internal Scanner class has been removed from the pydoc module.

The private and effectively unused _gestalt module has been removed, along with the private platform functions _mac_ver_lookup, _mac_ver_gstalt, and _bcd2str, which would only have ever been called on badly broken OSX systems (see bpo-18393 [https://bugs.python.org/issue18393]).

The hardcoded copies of certain stat constants that were included in the tarfile module namespace have been removed.

移植到 Python 3.4

本节列出了先前描述的更改以及可能需要更改代码的其他错误修正.

 ‘python’ 命令行为的变化

In a posix shell, setting the PATH environment variable to an empty value is equivalent to not setting it at all. However, setting PYTHONPATH to an empty value was not equivalent to not setting it at all: setting PYTHONPATH to an empty value was equivalent to setting it to ., which leads to confusion when reasoning by analogy to how PATH works. The behavior now conforms to the posix convention for PATH.

The [X refs, Y blocks] output of a debug (--with-pydebug) build of the CPython interpreter is now off by default. It can be re-enabled using the -X showrefcount option. (Contributed by Ezio Melotti in bpo-17323 [https://bugs.python.org/issue17323].)

The python command and most stdlib scripts (as well as argparse) now output --version information to stdout instead of stderr (for issue list see 其他改进 above).

更改的Python API

The ABCs defined in importlib.abc now either raise the appropriate exception or return a default value instead of raising NotImplementedError blindly. This will only affect code calling super() and falling through all the way to the ABCs. For compatibility, catch both NotImplementedError or the appropriate exception as needed.

The module type now initializes the __package__ and __loader__ attributes to None by default. To determine if these attributes were set in a backwards-compatible fashion, use e.g. getattr(module, '__loader__', None) is not None. (bpo-17115 [https://bugs.python.org/issue17115].)

importlib.util.module_for_loader() now sets __loader__ and __package__ unconditionally to properly support reloading. If this is not desired then you will need to set these attributes manually. You can use importlib.util.module_to_load() for module management.

Import now resets relevant attributes (e.g. __name__, __loader__, __package__, __file__, __cached__) unconditionally when reloading. Note that this restores a pre-3.3 behavior in that it means a module is re-found when re-loaded (bpo-19413 [https://bugs.python.org/issue19413]).

Frozen packages no longer set __path__ to a list containing the package name, they now set it to an empty list. The previous behavior could cause the import system to do the wrong thing on submodule imports if there was also a directory with the same name as the frozen package. The correct way to determine if a module is a package or not is to use hasattr(module, '__path__') (bpo-18065 [https://bugs.python.org/issue18065]).

Frozen modules no longer define a __file__ attribute. It’s semantically incorrect for frozen modules to set the attribute as they are not loaded from any explicit location. If you must know that a module comes from frozen code then you can see if the module’s __spec__.location is set to 'frozen', check if the loader is a subclass of importlib.machinery.FrozenImporter, or if Python 2 compatibility is necessary you can use imp.is_frozen().

py_compile.compile() now raises FileExistsError if the file path it would write to is a symlink or a non-regular file. This is to act as a warning that import will overwrite those files with a regular file regardless of what type of file path they were originally.

importlib.abc.SourceLoader.get_source() no longer raises ImportError when the source code being loaded triggers a SyntaxError or UnicodeDecodeError. As ImportError is meant to be raised only when source code cannot be found but it should, it was felt to be over-reaching/overloading of that meaning when the source code is found but improperly structured. If you were catching ImportError before and wish to continue to ignore syntax or decoding issues, catch all three exceptions now.

functools.update_wrapper() and functools.wraps() now correctly set the __wrapped__ attribute to the function being wrapped, even if that function also had its __wrapped__ attribute set. This means __wrapped__ attributes now correctly link a stack of decorated functions rather than every __wrapped__ attribute in the chain referring to the innermost function. Introspection libraries that assumed the previous behaviour was intentional can use inspect.unwrap() to access the first function in the chain that has no __wrapped__ attribute.

inspect.getfullargspec() has been reimplemented on top of inspect.signature() and hence handles a much wider variety of callable objects than it did in the past. It is expected that additional builtin and extension module callables will gain signature metadata over the course of the Python 3.4 series. Code that assumes that inspect.getfullargspec() will fail on non-Python callables may need to be adjusted accordingly.

importlib.machinery.PathFinder now passes on the current working directory to objects in sys.path_hooks for the empty string. This results in sys.path_importer_cache never containing '', thus iterating through sys.path_importer_cache based on sys.path will not find all keys. A module’s __file__ when imported in the current working directory will also now have an absolute path, including when using -m with the interpreter (except for __main__.__file__ when a script has been executed directly using a relative path) (Contributed by Brett Cannon in bpo-18416 [https://bugs.python.org/issue18416]). is specified on the command-line) (bpo-18416 [https://bugs.python.org/issue18416]).

The removal of the strict argument to HTTPConnection and HTTPSConnection changes the meaning of the remaining arguments if you are specifying them positionally rather than by keyword. If you’ve been paying attention to deprecation warnings your code should already be specifying any additional arguments via keywords.

Strings between from __future__ import ... statements now always raise a SyntaxError. Previously if there was no leading docstring, an interstitial string would sometimes be ignored. This brings CPython into compliance with the language spec; Jython and PyPy already were. (bpo-17434 [https://bugs.python.org/issue17434]).

ssl.SSLSocket.getpeercert() and ssl.SSLSocket.do_handshake() now raise an OSError with ENOTCONN when the SSLSocket is not connected, instead of the previous behavior of raising an AttributeError. In addition, getpeercert() will raise a ValueError if the handshake has not yet been done.

base64.b32decode() now raises a binascii.Error when the input string contains non-b32-alphabet characters, instead of a TypeError. This particular TypeError was missed when the other TypeErrors were converted. (Contributed by Serhiy Storchaka in bpo-18011 [https://bugs.python.org/issue18011].) Note: this change was also inadvertently applied in Python 3.3.3.

The file attribute is now automatically closed when the creating cgi.FieldStorage instance is garbage collected. If you were pulling the file object out separately from the cgi.FieldStorage instance and not keeping the instance alive, then you should either store the entire cgi.FieldStorage instance or read the contents of the file before the cgi.FieldStorage instance is garbage collected.

Calling read or write on a closed SSL socket now raises an informative ValueError rather than the previous more mysterious AttributeError (bpo-9177 [https://bugs.python.org/issue9177]).

slice.indices() no longer produces an OverflowError for huge values. As a consequence of this fix, slice.indices() now raises a ValueError if given a negative length; previously it returned nonsense values (bpo-14794 [https://bugs.python.org/issue14794]).

The complex constructor, unlike the cmath functions, was incorrectly accepting float values if an object’s __complex__ special method returned one. This now raises a TypeError. (bpo-16290 [https://bugs.python.org/issue16290].)

The int constructor in 3.2 and 3.3 erroneously accepts float values for the base parameter. It is unlikely anyone was doing this, but if so, it will now raise a TypeError (bpo-16772 [https://bugs.python.org/issue16772]).

Defaults for keyword-only arguments are now evaluated after defaults for regular keyword arguments, instead of before. Hopefully no one wrote any code that depends on the previous buggy behavior (bpo-16967 [https://bugs.python.org/issue16967]).

Stale thread states are now cleared after fork(). This may cause some system resources to be released that previously were incorrectly kept perpetually alive (for example, database connections kept in thread-local storage). (bpo-17094 [https://bugs.python.org/issue17094].)

Parameter names in __annotations__ dicts are now mangled properly, similarly to __kwdefaults__. (Contributed by Yury Selivanov in bpo-20625 [https://bugs.python.org/issue20625].)

hashlib.hash.name now always returns the identifier in lower case. Previously some builtin hashes had uppercase names, but now that it is a formal public interface the naming has been made consistent (bpo-18532 [https://bugs.python.org/issue18532]).

Because unittest.TestSuite now drops references to tests after they are run, test harnesses that re-use a TestSuite to re-run a set of tests may fail. Test suites should not be re-used in this fashion since it means state is retained between test runs, breaking the test isolation that unittest is designed to provide. However, if the lack of isolation is considered acceptable, the old behavior can be restored by creating a TestSuite subclass that defines a _removeTestAtIndex method that does nothing (see TestSuite.__iter__()) (bpo-11798 [https://bugs.python.org/issue11798]).

unittest now uses argparse for command line parsing. There are certain invalid command forms that used to work that are no longer allowed; in theory this should not cause backward compatibility issues since the disallowed command forms didn’t make any sense and are unlikely to be in use.

The re.split(), re.findall(), and re.sub() functions, and the group() and groups() methods of match objects now always return a bytes object when the string to be matched is a bytes-like object. Previously the return type matched the input type, so if your code was depending on the return value being, say, a bytearray, you will need to change your code.

audioop functions now raise an error immediately if passed string input, instead of failing randomly later on (bpo-16685 [https://bugs.python.org/issue16685]).

The new convert_charrefs argument to HTMLParser currently defaults to False for backward compatibility, but will eventually be changed to default to True. It is recommended that you add this keyword, with the appropriate value, to any HTMLParser calls in your code (bpo-13633 [https://bugs.python.org/issue13633]).

Since the digestmod argument to the hmac.new() function will in the future have no default, all calls to hmac.new() should be changed to explicitly specify a digestmod (bpo-17276 [https://bugs.python.org/issue17276]).

Calling sysconfig.get_config_var() with the SO key, or looking SO up in the results of a call to sysconfig.get_config_vars() is deprecated. This key should be replaced by EXT_SUFFIX or SHLIB_SUFFIX, depending on the context (bpo-19555 [https://bugs.python.org/issue19555]).

Any calls to open functions that specify U should be modified. U is ineffective in Python3 and will eventually raise an error if used. Depending on the function, the equivalent of its old Python2 behavior can be achieved using either a newline argument, or if necessary by wrapping the stream in TextIOWrapper to use its newline argument (bpo-15204 [https://bugs.python.org/issue15204]).

If you use pyvenv in a script and desire that pip not be installed, you must add --without-pip to your command invocation.

The default behavior of json.dump() and json.dumps() when an indent is specified has changed: it no longer produces trailing spaces after the item separating commas at the ends of lines. This will matter only if you have tests that are doing white-space-sensitive comparisons of such output (bpo-16333 [https://bugs.python.org/issue16333]).

doctest now looks for doctests in extension module __doc__ strings, so if your doctest test discovery includes extension modules that have things that look like doctests in them you may see test failures you’ve never seen before when running your tests (bpo-3158 [https://bugs.python.org/issue3158]).

The collections.abc module has been slightly refactored as part of the Python startup improvements. As a consequence of this, it is no longer the case that importing collections automatically imports collections.abc. If your program depended on the (undocumented) implicit import, you will need to add an explicit import collections.abc (bpo-20784 [https://bugs.python.org/issue20784]).

C API 中的改变

PyEval_EvalFrameEx(), PyObject_Repr(), and PyObject_Str(), along with some other internal C APIs, now include a debugging assertion that ensures they are not used in situations where they may silently discard a currently active exception. In cases where discarding the active exception is expected and desired (for example, because it has already been saved locally with PyErr_Fetch() or is being deliberately replaced with a different exception), an explicit PyErr_Clear() call will be needed to avoid triggering the assertion when invoking these operations (directly or indirectly) and running against a version of Python that is compiled with assertions enabled.

PyErr_SetImportError() now sets TypeError when its msg argument is not set. Previously only NULL was returned with no exception set.

The result of the PyOS_ReadlineFunctionPointer callback must now be a string allocated by PyMem_RawMalloc() or PyMem_RawRealloc(), or NULL if an error occurred, instead of a string allocated by PyMem_Malloc() or PyMem_Realloc() (bpo-16742 [https://bugs.python.org/issue16742])

PyThread_set_key_value() now always set the value. In Python 3.3, the function did nothing if the key already exists (if the current value is a non-NULL pointer).

The f_tstate (thread state) field of the PyFrameObject structure has been removed to fix a bug: see bpo-14432 [https://bugs.python.org/issue14432] for the rationale.

3.4.3 的变化

PEP 476: Enabling certificate verification by default for stdlib http clients

http.client and modules which use it, such as urllib.request and xmlrpc.client, will now verify that the server presents a certificate which is signed by a CA in the platform trust store and whose hostname matches the hostname being requested by default, significantly improving security for many applications.

For applications which require the old previous behavior, they can pass an alternate context:

import urllib.request
import ssl

# This disables all verification
context = ssl._create_unverified_context()

# This allows using a specific certificate for the host, which doesn't need
# to be in the trust store
context = ssl.create_default_context(cafile="/path/to/file.crt")

urllib.request.urlopen("https://invalid-cert", context=context)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

Python 3.3 有什么新变化

本文介绍了Python 3.3相比 3.2 的新增特性。Python 3.3于2012年9月29日发布。有关完整详细信息，请参见 changelog [https://docs.python.org/3.3/whatsnew/changelog.html]。

参见

PEP 398 [https://www.python.org/dev/peps/pep-0398] - Python 3.3 发布计划

摘要 - 发布重点

新的语法特性：

New yield from expression for generator delegation.

The u'unicode' syntax is accepted again for str objects.

新的库模块：

faulthandler （帮助调试低级崩溃）

ipaddress (high-level objects representing IP addresses and masks)

lzma (compress data using the XZ / LZMA algorithm)

unittest.mock (replace parts of your system under test with mock objects)

venv (Python virtual environments, as in the popular virtualenv package)

新的内置特性：

重写 I/O 异常的层次结构.

实现的改进：

基于 importlib 重写 import machinery

More compact unicode strings.

More compact attribute dictionaries.

显着改进的库模块：

C Accelerator for the decimal module.

Better unicode handling in the email module (provisional).

安全改进：

Hash randomization is switched on by default.

Please read on for a comprehensive list of user-facing changes.

PEP 405: 虚拟环境

Virtual environments help create separate Python setups while sharing a system-wide base install, for ease of maintenance. Virtual environments have their own set of private site packages (i.e. locally-installed libraries), and are optionally segregated from the system-wide site packages. Their concept and implementation are inspired by the popular virtualenv third-party package, but benefit from tighter integration with the interpreter core.

This PEP adds the venv module for programmatic access, and the pyvenv script for command-line access and administration. The Python interpreter checks for a pyvenv.cfg, file whose existence signals the base of a virtual environment’s directory tree.

参见

PEP 405 [https://www.python.org/dev/peps/pep-0405] - Python虚拟环境

PEP written by Carl Meyer; implementation by Carl Meyer and Vinay Sajip

PEP 420: 隐式命名空间包

Native support for package directories that don’t require __init__.py marker files and can automatically span multiple path segments (inspired by various third party approaches to namespace packages, as described in PEP 420 [https://www.python.org/dev/peps/pep-0420])

参见

PEP 420 [https://www.python.org/dev/peps/pep-0420] - 隐式命名空间包

PEP 由 Eric V. Smith 撰写，由 Eric V. Smith 和 Barry Warsaw 实现

PEP 3118: 新的内存视图实现和缓冲协议文档

The implementation of PEP 3118 [https://www.python.org/dev/peps/pep-3118] has been significantly improved.

The new memoryview implementation comprehensively fixes all ownership and lifetime issues of dynamically allocated fields in the Py_buffer struct that led to multiple crash reports. Additionally, several functions that crashed or returned incorrect results for non-contiguous or multi-dimensional input have been fixed.

The memoryview object now has a PEP-3118 compliant getbufferproc() that checks the consumer’s request type. Many new features have been added, most of them work in full generality for non-contiguous arrays and arrays with suboffsets.

The documentation has been updated, clearly spelling out responsibilities for both exporters and consumers. Buffer request flags are grouped into basic and compound flags. The memory layout of non-contiguous and multi-dimensional NumPy-style arrays is explained.

相关特性

All native single character format specifiers in struct module syntax (optionally prefixed with ‘@’) are now supported.

With some restrictions, the cast() method allows changing of format and shape of C-contiguous arrays.

Multi-dimensional list representations are supported for any array type.

Multi-dimensional comparisons are supported for any array type.

One-dimensional memoryviews of hashable (read-only) types with formats B, b or c are now hashable. (Contributed by Antoine Pitrou in bpo-13411 [https://bugs.python.org/issue13411].)

Arbitrary slicing of any 1-D arrays type is supported. For example, it is now possible to reverse a memoryview in O(1) by using a negative step.

API changes

The maximum number of dimensions is officially limited to 64.

The representation of empty shape, strides and suboffsets is now an empty tuple instead of None.

Accessing a memoryview element with format ‘B’ (unsigned bytes) now returns an integer (in accordance with the struct module syntax). For returning a bytes object the view must be cast to ‘c’ first.

memoryview comparisons now use the logical structure of the operands and compare all array elements by value. All format strings in struct module syntax are supported. Views with unrecognised format strings are still permitted, but will always compare as unequal, regardless of view contents.

For further changes see Build and C API Changes and Porting C code.

（由 Stefan Krah 在 bpo-10181 [https://bugs.python.org/issue10181] 中贡献。）

参见

PEP 3118 [https://www.python.org/dev/peps/pep-3118] - 修改缓冲区协议

PEP 393: 灵活的字符串表示

The Unicode string type is changed to support multiple internal representations, depending on the character with the largest Unicode ordinal (1, 2, or 4 bytes) in the represented string. This allows a space-efficient representation in common cases, but gives access to full UCS-4 on all systems. For compatibility with existing APIs, several representations may exist in parallel; over time, this compatibility should be phased out.

On the Python side, there should be no downside to this change.

On the C API side, PEP 393 is fully backward compatible. The legacy API should remain available at least five years. Applications using the legacy API will not fully benefit of the memory reduction, or - worse - may use a bit more memory, because Python may have to maintain two versions of each string (in the legacy format and in the new efficient storage).

Functionality

Changes introduced by PEP 393 [https://www.python.org/dev/peps/pep-0393] are the following:

Python now always supports the full range of Unicode code points, including non-BMP ones (i.e. from U+0000 to U+10FFFF). The distinction between narrow and wide builds no longer exists and Python now behaves like a wide build, even under Windows.

With the death of narrow builds, the problems specific to narrow builds have also been fixed, for example:

len() now always returns 1 for non-BMP characters, so len('\U0010FFFF') == 1;

surrogate pairs are not recombined in string literals, so '\uDBFF\uDFFF' != '\U0010FFFF';

indexing or slicing non-BMP characters returns the expected value, so '\U0010FFFF'[0] now returns '\U0010FFFF' and not '\uDBFF';

all other functions in the standard library now correctly handle non-BMP code points.

The value of sys.maxunicode is now always 1114111 (0x10FFFF in hexadecimal). The PyUnicode_GetMax() function still returns either 0xFFFF or 0x10FFFF for backward compatibility, and it should not be used with the new Unicode API (see bpo-13054 [https://bugs.python.org/issue13054]).

The ./configure flag --with-wide-unicode has been removed.

Performance and resource usage

The storage of Unicode strings now depends on the highest code point in the string:

pure ASCII and Latin1 strings (U+0000-U+00FF) use 1 byte per code point;

BMP strings (U+0000-U+FFFF) use 2 bytes per code point;

non-BMP strings (U+10000-U+10FFFF) use 4 bytes per code point.

The net effect is that for most applications, memory usage of string storage should decrease significantly - especially compared to former wide unicode builds - as, in many cases, strings will be pure ASCII even in international contexts (because many strings store non-human language data, such as XML fragments, HTTP headers, JSON-encoded data, etc.). We also hope that it will, for the same reasons, increase CPU cache efficiency on non-trivial applications. The memory usage of Python 3.3 is two to three times smaller than Python 3.2, and a little bit better than Python 2.7, on a Django benchmark (see the PEP for details).

参见

PEP 393 [https://www.python.org/dev/peps/pep-0393] - 灵活的字符串表示

PEP written by Martin von Löwis; implementation by Torsten Becker and Martin von Löwis.

PEP 397: 适用于Windows的Python启动器

The Python 3.3 Windows installer now includes a py launcher application that can be used to launch Python applications in a version independent fashion.

This launcher is invoked implicitly when double-clicking *.py files. If only a single Python version is installed on the system, that version will be used to run the file. If multiple versions are installed, the most recent version is used by default, but this can be overridden by including a Unix-style “shebang line” in the Python script.

The launcher can also be used explicitly from the command line as the py application. Running py follows the same version selection rules as implicitly launching scripts, but a more specific version can be selected by passing appropriate arguments (such as -3 to request Python 3 when Python 2 is also installed, or -2.6 to specifically request an earlier Python version when a more recent version is installed).

In addition to the launcher, the Windows installer now includes an option to add the newly installed Python to the system PATH. (Contributed by Brian Curtin in bpo-3561 [https://bugs.python.org/issue3561].)

参见

PEP 397 [https://www.python.org/dev/peps/pep-0397] - 适用于Windows的Python启动器

PEP written by Mark Hammond and Martin v. Löwis; implementation by Vinay Sajip.

Launcher documentation: 适用于Windows的Python启动器

Installer PATH modification: 查找Python可执行文件

PEP 3151: 重写 OS 和 IO 异常的层次结构

The hierarchy of exceptions raised by operating system errors is now both simplified and finer-grained.

You don’t have to worry anymore about choosing the appropriate exception type between OSError, IOError, EnvironmentError, WindowsError, mmap.error, socket.error or select.error. All these exception types are now only one: OSError. The other names are kept as aliases for compatibility reasons.

Also, it is now easier to catch a specific error condition. Instead of inspecting the errno attribute (or args[0]) for a particular constant from the errno module, you can catch the adequate OSError subclass. The available subclasses are the following:

BlockingIOError

ChildProcessError

ConnectionError

FileExistsError

FileNotFoundError

InterruptedError

IsADirectoryError

NotADirectoryError

PermissionError

ProcessLookupError

TimeoutError

And the ConnectionError itself has finer-grained subclasses:

BrokenPipeError

ConnectionAbortedError

ConnectionRefusedError

ConnectionResetError

Thanks to the new exceptions, common usages of the errno can now be avoided. For example, the following code written for Python 3.2:

from errno import ENOENT, EACCES, EPERM

try:
    with open("document.txt") as f:
        content = f.read()
except IOError as err:
    if err.errno == ENOENT:
        print("document.txt file is missing")
    elif err.errno in (EACCES, EPERM):
        print("You are not allowed to read document.txt")
    else:
        raise

can now be written without the errno import and without manual inspection of exception attributes:

try:
    with open("document.txt") as f:
        content = f.read()
except FileNotFoundError:
    print("document.txt file is missing")
except PermissionError:
    print("You are not allowed to read document.txt")

参见

PEP 3151 [https://www.python.org/dev/peps/pep-3151] - 重写 OS 和 IO 异常的层次结构

PEP 由 Antoine Pitrou 撰写并实现

PEP 380: 委托给子生成器的语法

PEP 380 adds the yield from expression, allowing a generator to delegate part of its operations to another generator. This allows a section of code containing yield to be factored out and placed in another generator. Additionally, the subgenerator is allowed to return with a value, and the value is made available to the delegating generator.

While designed primarily for use in delegating to a subgenerator, the yield from expression actually allows delegation to arbitrary subiterators.

For simple iterators, yield from iterable is essentially just a shortened form of for item in iterable: yield item:

>>> def g(x):
...     yield from range(x, 0, -1)
...     yield from range(x)
...
>>> list(g(5))
[5, 4, 3, 2, 1, 0, 1, 2, 3, 4]

However, unlike an ordinary loop, yield from allows subgenerators to receive sent and thrown values directly from the calling scope, and return a final value to the outer generator:

>>> def accumulate():
...     tally = 0
...     while 1:
...         next = yield
...         if next is None:
...             return tally
...         tally += next
...
>>> def gather_tallies(tallies):
...     while 1:
...         tally = yield from accumulate()
...         tallies.append(tally)
...
>>> tallies = []
>>> acc = gather_tallies(tallies)
>>> next(acc)  # Ensure the accumulator is ready to accept values
>>> for i in range(4):
...     acc.send(i)
...
>>> acc.send(None)  # Finish the first tally
>>> for i in range(5):
...     acc.send(i)
...
>>> acc.send(None)  # Finish the second tally
>>> tallies
[6, 10]

The main principle driving this change is to allow even generators that are designed to be used with the send and throw methods to be split into multiple subgenerators as easily as a single large function can be split into multiple subfunctions.

参见

PEP 380 [https://www.python.org/dev/peps/pep-0380] - 委托给子生成器的语法

PEP 由 Greg Ewing 撰写，由 Greg Ewing 实现。由 Renaud Blanch，Ryan Kelly 和 Nick Coghlan 集成到3.3，由 Zbigniew Jędrzejewski-Szmek 和 Nick Coghlan 编写文档

PEP 409: 清除异常上下文

PEP 409 introduces new syntax that allows the display of the chained exception context to be disabled. This allows cleaner error messages in applications that convert between exception types:

>>> class D:
...     def __init__(self, extra):
...         self._extra_attributes = extra
...     def __getattr__(self, attr):
...         try:
...             return self._extra_attributes[attr]
...         except KeyError:
...             raise AttributeError(attr) from None
...
>>> D({}).x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 8, in __getattr__
AttributeError: x

Without the from None suffix to suppress the cause, the original exception would be displayed by default:

>>> class C:
...     def __init__(self, extra):
...         self._extra_attributes = extra
...     def __getattr__(self, attr):
...         try:
...             return self._extra_attributes[attr]
...         except KeyError:
...             raise AttributeError(attr)
...
>>> C({}).x
Traceback (most recent call last):
  File "<stdin>", line 6, in __getattr__
KeyError: 'x'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 8, in __getattr__
AttributeError: x

No debugging capability is lost, as the original exception context remains available if needed (for example, if an intervening library has incorrectly suppressed valuable underlying details):

>>> try:
...     D({}).x
... except AttributeError as exc:
...     print(repr(exc.__context__))
...
KeyError('x',)

参见

PEP 409 [https://www.python.org/dev/peps/pep-0409] - 清除异常上下文

PEP 由 Ethan Furman 撰写 ，由 Ethan Furman 和 Nick Coghlan 实现。

PEP 414: 显式的Unicode文本

To ease the transition from Python 2 for Unicode aware Python applications that make heavy use of Unicode literals, Python 3.3 once again supports the “u” prefix for string literals. This prefix has no semantic significance in Python 3, it is provided solely to reduce the number of purely mechanical changes in migrating to Python 3, making it easier for developers to focus on the more significant semantic changes (such as the stricter default separation of binary and text data).

参见

PEP 414 [https://www.python.org/dev/peps/pep-0414] - 显式的Unicode文本

PEP 由 Armin Ronacher 撰写

PEP 3155: 类和函数的限定名称

Functions and class objects have a new __qualname__ attribute representing the “path” from the module top-level to their definition. For global functions and classes, this is the same as __name__. For other functions and classes, it provides better information about where they were actually defined, and how they might be accessible from the global scope.

Example with (non-bound) methods:

>>> class C:
...     def meth(self):
...         pass
>>> C.meth.__name__
'meth'
>>> C.meth.__qualname__
'C.meth'

Example with nested classes:

>>> class C:
...     class D:
...         def meth(self):
...             pass
...
>>> C.D.__name__
'D'
>>> C.D.__qualname__
'C.D'
>>> C.D.meth.__name__
'meth'
>>> C.D.meth.__qualname__
'C.D.meth'

Example with nested functions:

>>> def outer():
...     def inner():
...         pass
...     return inner
...
>>> outer().__name__
'inner'
>>> outer().__qualname__
'outer.<locals>.inner'

The string representation of those objects is also changed to include the new, more precise information:

>>> str(C.D)
"<class '__main__.C.D'>"
>>> str(C.D.meth)
'<function C.D.meth at 0x7f46b9fe31e0>'

参见

PEP 3155 [https://www.python.org/dev/peps/pep-3155] - 类和函数的限定名称

PEP 由 Antoine Pitrou 撰写并实现

PEP 412: Key-Sharing Dictionary

Dictionaries used for the storage of objects’ attributes are now able to share part of their internal storage between each other (namely, the part which stores the keys and their respective hashes). This reduces the memory consumption of programs creating many instances of non-builtin types.

参见

PEP 412 [https://www.python.org/dev/peps/pep-0412] - Key-Sharing Dictionary

PEP 由 Mark Shannon 撰写并实现。

PEP 362: 函数签名对象

A new function inspect.signature() makes introspection of python callables easy and straightforward. A broad range of callables is supported: python functions, decorated or not, classes, and functools.partial() objects. New classes inspect.Signature, inspect.Parameter and inspect.BoundArguments hold information about the call signatures, such as, annotations, default values, parameters kinds, and bound arguments, which considerably simplifies writing decorators and any code that validates or amends calling signatures or arguments.

参见

PEP 362 [https://www.python.org/dev/peps/pep-0362]: - 函数签名对象

PEP 由 Brett Cannon，Yury Selivanov，Larry Hastings，Jiwon Seo 撰写，由 Yury Selivanov 实现

PEP 421: 添加 sys.implementation

A new attribute on the sys module exposes details specific to the implementation of the currently running interpreter. The initial set of attributes on sys.implementation are name, version, hexversion, and cache_tag.

The intention of sys.implementation is to consolidate into one namespace the implementation-specific data used by the standard library. This allows different Python implementations to share a single standard library code base much more easily. In its initial state, sys.implementation holds only a small portion of the implementation-specific data. Over time that ratio will shift in order to make the standard library more portable.

One example of improved standard library portability is cache_tag. As of Python 3.3, sys.implementation.cache_tag is used by importlib to support PEP 3147 [https://www.python.org/dev/peps/pep-3147] compliance. Any Python implementation that uses importlib for its built-in import system may use cache_tag to control the caching behavior for modules.

SimpleNamespace

The implementation of sys.implementation also introduces a new type to Python: types.SimpleNamespace. In contrast to a mapping-based namespace, like dict, SimpleNamespace is attribute-based, like object. However, unlike object, SimpleNamespace instances are writable. This means that you can add, remove, and modify the namespace through normal attribute access.

参见

PEP 421 [https://www.python.org/dev/peps/pep-0421] - 添加 sys.implementation

PEP 由 Eric Snow 撰写并实现

Using importlib as the Implementation of Import

bpo-2377 [https://bugs.python.org/issue2377] - Replace __import__ w/ importlib.__import__ bpo-13959 [https://bugs.python.org/issue13959] - Re-implement parts of imp in pure Python bpo-14605 [https://bugs.python.org/issue14605] - Make import machinery explicit bpo-14646 [https://bugs.python.org/issue14646] - Require loaders set __loader__ and __package__

The __import__() function is now powered by importlib.__import__(). This work leads to the completion of “phase 2” of PEP 302 [https://www.python.org/dev/peps/pep-0302]. There are multiple benefits to this change. First, it has allowed for more of the machinery powering import to be exposed instead of being implicit and hidden within the C code. It also provides a single implementation for all Python VMs supporting Python 3.3 to use, helping to end any VM-specific deviations in import semantics. And finally it eases the maintenance of import, allowing for future growth to occur.

For the common user, there should be no visible change in semantics. For those whose code currently manipulates import or calls import programmatically, the code changes that might possibly be required are covered in the Porting Python code section of this document.

New APIs

One of the large benefits of this work is the exposure of what goes into making the import statement work. That means the various importers that were once implicit are now fully exposed as part of the importlib package.

The abstract base classes defined in importlib.abc have been expanded to properly delineate between meta path finders and path entry finders by introducing importlib.abc.MetaPathFinder and importlib.abc.PathEntryFinder, respectively. The old ABC of importlib.abc.Finder is now only provided for backwards-compatibility and does not enforce any method requirements.

In terms of finders, importlib.machinery.FileFinder exposes the mechanism used to search for source and bytecode files of a module. Previously this class was an implicit member of sys.path_hooks.

For loaders, the new abstract base class importlib.abc.FileLoader helps write a loader that uses the file system as the storage mechanism for a module’s code. The loader for source files (importlib.machinery.SourceFileLoader), sourceless bytecode files (importlib.machinery.SourcelessFileLoader), and extension modules (importlib.machinery.ExtensionFileLoader) are now available for direct use.

ImportError now has name and path attributes which are set when there is relevant data to provide. The message for failed imports will also provide the full name of the module now instead of just the tail end of the module’s name.

The importlib.invalidate_caches() function will now call the method with the same name on all finders cached in sys.path_importer_cache to help clean up any stored state as necessary.

Visible Changes

For potential required changes to code, see the Porting Python code section.

Beyond the expanse of what importlib now exposes, there are other visible changes to import. The biggest is that sys.meta_path and sys.path_hooks now store all of the meta path finders and path entry hooks used by import. Previously the finders were implicit and hidden within the C code of import instead of being directly exposed. This means that one can now easily remove or change the order of the various finders to fit one’s needs.

Another change is that all modules have a __loader__ attribute, storing the loader used to create the module. PEP 302 [https://www.python.org/dev/peps/pep-0302] has been updated to make this attribute mandatory for loaders to implement, so in the future once 3rd-party loaders have been updated people will be able to rely on the existence of the attribute. Until such time, though, import is setting the module post-load.

Loaders are also now expected to set the __package__ attribute from PEP 366 [https://www.python.org/dev/peps/pep-0366]. Once again, import itself is already setting this on all loaders from importlib and import itself is setting the attribute post-load.

None is now inserted into sys.path_importer_cache when no finder can be found on sys.path_hooks. Since imp.NullImporter is not directly exposed on sys.path_hooks it could no longer be relied upon to always be available to use as a value representing no finder found.

All other changes relate to semantic changes which should be taken into consideration when updating code for Python 3.3, and thus should be read about in the Porting Python code section of this document.

(Implementation by Brett Cannon)

其他语言特性修改

对Python 语言核心进行的小改动：

Added support for Unicode name aliases and named sequences. Both unicodedata.lookup() and '\N{...}' now resolve name aliases, and unicodedata.lookup() resolves named sequences too.

（由 Ezio Melotti 在 bpo-12753 [https://bugs.python.org/issue12753] 中贡献。）

Unicode database updated to UCD version 6.1.0

Equality comparisons on range() objects now return a result reflecting the equality of the underlying sequences generated by those range objects. (bpo-13201 [https://bugs.python.org/issue13201])

The count(), find(), rfind(), index() and rindex() methods of bytes and bytearray objects now accept an integer between 0 and 255 as their first argument.

（由 Petri Lehtinen 在 bpo-12170 [https://bugs.python.org/issue12170] 中贡献。）

The rjust(), ljust(), and center() methods of bytes and bytearray now accept a bytearray for the fill argument. (Contributed by Petri Lehtinen in bpo-12380 [https://bugs.python.org/issue12380].)

New methods have been added to list and bytearray: copy() and clear() (bpo-10516 [https://bugs.python.org/issue10516]). Consequently, MutableSequence now also defines a clear() method (bpo-11388 [https://bugs.python.org/issue11388]).

Raw bytes literals can now be written rb"..." as well as br"...".

（由 Antoine Pitrou 在 bpo-13748 [https://bugs.python.org/issue13748] 中贡献。）

dict.setdefault() now does only one lookup for the given key, making it atomic when used with built-in types.

（由 Filip Gruszczyński 在 bpo-13521 [https://bugs.python.org/issue13521] 中贡献。）

The error messages produced when a function call does not match the function signature have been significantly improved.

（由 Benjamin Peterson 贡献。）

A Finer-Grained Import Lock

Previous versions of CPython have always relied on a global import lock. This led to unexpected annoyances, such as deadlocks when importing a module would trigger code execution in a different thread as a side-effect. Clumsy workarounds were sometimes employed, such as the PyImport_ImportModuleNoBlock() C API function.

In Python 3.3, importing a module takes a per-module lock. This correctly serializes importation of a given module from multiple threads (preventing the exposure of incompletely initialized modules), while eliminating the aforementioned annoyances.

（由 Antoine Pitrou 在 bpo-9260 [https://bugs.python.org/issue9260] 中贡献。）

Builtin functions and types

open() gets a new opener parameter: the underlying file descriptor for the file object is then obtained by calling opener with (file, flags). It can be used to use custom flags like os.O_CLOEXEC for example. The 'x' mode was added: open for exclusive creation, failing if the file already exists.

print(): added the flush keyword argument. If the flush keyword argument is true, the stream is forcibly flushed.

hash(): hash randomization is enabled by default, see object.__hash__() and PYTHONHASHSEED.

The str type gets a new casefold() method: return a casefolded copy of the string, casefolded strings may be used for caseless matching. For example, 'ß'.casefold() returns 'ss'.

The sequence documentation has been substantially rewritten to better explain the binary/text sequence distinction and to provide specific documentation sections for the individual builtin sequence types (bpo-4966 [https://bugs.python.org/issue4966]).

新增模块

faulthandler

This new debug module faulthandler contains functions to dump Python tracebacks explicitly, on a fault (a crash like a segmentation fault), after a timeout, or on a user signal. Call faulthandler.enable() to install fault handlers for the SIGSEGV, SIGFPE, SIGABRT, SIGBUS, and SIGILL signals. You can also enable them at startup by setting the PYTHONFAULTHANDLER environment variable or by using -X faulthandler command line option.

Example of a segmentation fault on Linux:

$ python -q -X faulthandler
>>> import ctypes
>>> ctypes.string_at(0)
Fatal Python error: Segmentation fault

Current thread 0x00007fb899f39700:
  File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at
  File "<stdin>", line 1 in <module>
Segmentation fault

ipaddress

The new ipaddress module provides tools for creating and manipulating objects representing IPv4 and IPv6 addresses, networks and interfaces (i.e. an IP address associated with a specific IP subnet).

（由 Google 和 Peter Moody 在 bpo-3144 [https://bugs.python.org/issue3144] 中贡献。）

lzma

The newly-added lzma module provides data compression and decompression using the LZMA algorithm, including support for the .xz and .lzma file formats.

（由 Nadeem Vawda 和 Per Øyvind Karlsen 在 bpo-16531 [https://bugs.python.org/issue16531] 中贡献。）

改进的模块

abc

Improved support for abstract base classes containing descriptors composed with abstract methods. The recommended approach to declaring abstract descriptors is now to provide __isabstractmethod__ as a dynamically updated property. The built-in descriptors have been updated accordingly.

abc.abstractproperty has been deprecated, use property with abc.abstractmethod() instead.

abc.abstractclassmethod has been deprecated, use classmethod with abc.abstractmethod() instead.

abc.abstractstaticmethod has been deprecated, use staticmethod with abc.abstractmethod() instead.

（由 Pablo Galindo 在 bpo-11610 [https://bugs.python.org/issue11610] 中贡献。）

abc.ABCMeta.register() now returns the registered subclass, which means it can now be used as a class decorator (bpo-10868 [https://bugs.python.org/issue10868]).

array

The array module supports the long long type using q and Q type codes.

（由 Oren Tirosh 和 Hirokazu Yamamoto 在 bpo-1172711 [https://bugs.python.org/issue1172711] 中贡献。）

base64

ASCII-only Unicode strings are now accepted by the decoding functions of the base64 modern interface. For example, base64.b64decode('YWJj') returns b'abc'. (Contributed by Catalin Iacob in bpo-13641 [https://bugs.python.org/issue13641].)

binascii

In addition to the binary objects they normally accept, the a2b_ functions now all also accept ASCII-only strings as input. (Contributed by Antoine Pitrou in bpo-13637 [https://bugs.python.org/issue13637].)

bz2

The bz2 module has been rewritten from scratch. In the process, several new features have been added:

New bz2.open() function: open a bzip2-compressed file in binary or text mode.

bz2.BZ2File can now read from and write to arbitrary file-like objects, by means of its constructor’s fileobj argument.

（由 Nadeem Vawda 在 bpo-5863 [https://bugs.python.org/issue5863] 中贡献。）

bz2.BZ2File and bz2.decompress() can now decompress multi-stream inputs (such as those produced by the pbzip2 tool). bz2.BZ2File can now also be used to create this type of file, using the 'a' (append) mode.

（由 Nir Aides 在 bpo-1625 [https://bugs.python.org/issue1625] 中贡献。）

bz2.BZ2File now implements all of the io.BufferedIOBase API, except for the detach() and truncate() methods.

codecs

The mbcs codec has been rewritten to handle correctly replace and ignore error handlers on all Windows versions. The mbcs codec now supports all error handlers, instead of only replace to encode and ignore to decode.

A new Windows-only codec has been added: cp65001 (bpo-13216 [https://bugs.python.org/issue13216]). It is the Windows code page 65001 (Windows UTF-8, CP_UTF8). For example, it is used by sys.stdout if the console output code page is set to cp65001 (e.g., using chcp 65001 command).

Multibyte CJK decoders now resynchronize faster. They only ignore the first byte of an invalid byte sequence. For example, b'\xff\n'.decode('gb2312', 'replace') now returns a \n after the replacement character.

(bpo-12016 [https://bugs.python.org/issue12016])

Incremental CJK codec encoders are no longer reset at each call to their encode() methods. For example:

>>> import codecs
>>> encoder = codecs.getincrementalencoder('hz')('strict')
>>> b''.join(encoder.encode(x) for x in '\u52ff\u65bd\u65bc\u4eba\u3002 Bye.')
b'~{NpJ)l6HK!#~} Bye.'

This example gives b'~{Np~}~{J)~}~{l6~}~{HK~}~{!#~} Bye.' with older Python versions.

(bpo-12100 [https://bugs.python.org/issue12100])

The unicode_internal codec has been deprecated.

collections

Addition of a new ChainMap class to allow treating a number of mappings as a single unit. (Written by Raymond Hettinger for bpo-11089 [https://bugs.python.org/issue11089], made public in bpo-11297 [https://bugs.python.org/issue11297].)

The abstract base classes have been moved in a new collections.abc module, to better differentiate between the abstract and the concrete collections classes. Aliases for ABCs are still present in the collections module to preserve existing imports. (bpo-11085 [https://bugs.python.org/issue11085])

The Counter class now supports the unary + and - operators, as well as the in-place operators +=, -=, |=, and &=. (Contributed by Raymond Hettinger in bpo-13121 [https://bugs.python.org/issue13121].)

contextlib

ExitStack now provides a solid foundation for programmatic manipulation of context managers and similar cleanup functionality. Unlike the previous contextlib.nested API (which was deprecated and removed), the new API is designed to work correctly regardless of whether context managers acquire their resources in their __init__ method (for example, file objects) or in their __enter__ method (for example, synchronisation objects from the threading module).

(bpo-13585 [https://bugs.python.org/issue13585])

crypt

Addition of salt and modular crypt format (hashing method) and the mksalt() function to the crypt module.

(bpo-10924 [https://bugs.python.org/issue10924])

curses

If the curses module is linked to the ncursesw library, use Unicode functions when Unicode strings or characters are passed (e.g. waddwstr()), and bytes functions otherwise (e.g. waddstr()).

Use the locale encoding instead of utf-8 to encode Unicode strings.

curses.window has a new curses.window.encoding attribute.

The curses.window class has a new get_wch() method to get a wide character

The curses module has a new unget_wch() function to push a wide character so the next get_wch() will return it

（由 Iñigo Serna 在 bpo-6755 [https://bugs.python.org/issue6755] 中贡献。）

datetime

Equality comparisons between naive and aware datetime instances now return False instead of raising TypeError (bpo-15006 [https://bugs.python.org/issue15006]).

New datetime.datetime.timestamp() method: Return POSIX timestamp corresponding to the datetime instance.

The datetime.datetime.strftime() method supports formatting years older than 1000.

The datetime.datetime.astimezone() method can now be called without arguments to convert datetime instance to the system timezone.

decimal

bpo-7652 [https://bugs.python.org/issue7652] - integrate fast native decimal arithmetic.

C-module and libmpdec written by Stefan Krah.

The new C version of the decimal module integrates the high speed libmpdec library for arbitrary precision correctly-rounded decimal floating point arithmetic. libmpdec conforms to IBM’s General Decimal Arithmetic Specification.

Performance gains range from 10x for database applications to 100x for numerically intensive applications. These numbers are expected gains for standard precisions used in decimal floating point arithmetic. Since the precision is user configurable, the exact figures may vary. For example, in integer bignum arithmetic the differences can be significantly higher.

The following table is meant as an illustration. Benchmarks are available at http://www.bytereef.org/mpdecimal/quickstart.html.

decimal.py

_decimal

加速

pi

42.02秒

0.345秒

120倍

telco

172.19秒

5.68秒

30倍

psycopg

3.57秒

0.29秒

12倍

相关特性

The FloatOperation signal optionally enables stricter semantics for mixing floats and Decimals.

If Python is compiled without threads, the C version automatically disables the expensive thread local context machinery. In this case, the variable HAVE_THREADS is set to False.

API changes

C模块上下文限制（如下表），具体取决于计算机体系结构：

32位

64位

MAX_PREC

425000000

999999999999999999

MAX_EMAX

425000000

999999999999999999

MIN_EMIN

-425000000

-999999999999999999

In the context templates (DefaultContext, BasicContext and ExtendedContext) the magnitude of Emax and Emin has changed to 999999.

The Decimal constructor in decimal.py does not observe the context limits and converts values with arbitrary exponents or precision exactly. Since the C version has internal limits, the following scheme is used: If possible, values are converted exactly, otherwise InvalidOperation is raised and the result is NaN. In the latter case it is always possible to use create_decimal() in order to obtain a rounded or inexact value.

The power function in decimal.py is always correctly-rounded. In the C version, it is defined in terms of the correctly-rounded exp() and ln() functions, but the final result is only “almost always correctly rounded”.

In the C version, the context dictionary containing the signals is a MutableMapping. For speed reasons, flags and traps always refer to the same MutableMapping that the context was initialized with. If a new signal dictionary is assigned, flags and traps are updated with the new values, but they do not reference the RHS dictionary.

Pickling a Context produces a different output in order to have a common interchange format for the Python and C versions.

The order of arguments in the Context constructor has been changed to match the order displayed by repr().

The watchexp parameter in the quantize() method is deprecated.

email

Policy Framework

The email package now has a policy framework. A Policy is an object with several methods and properties that control how the email package behaves. The primary policy for Python 3.3 is the Compat32 policy, which provides backward compatibility with the email package in Python 3.2. A policy can be specified when an email message is parsed by a parser, or when a Message object is created, or when an email is serialized using a generator. Unless overridden, a policy passed to a parser is inherited by all the Message object and sub-objects created by the parser. By default a generator will use the policy of the Message object it is serializing. The default policy is compat32.

The minimum set of controls implemented by all policy objects are:

max_line_length

The maximum length, excluding the linesep character(s), individual lines may have when a Message is serialized. Defaults to 78.

linesep

The character used to separate individual lines when a Message is serialized. Defaults to \n.

cte_type

7bit or 8bit. 8bit applies only to a Bytes generator, and means that non-ASCII may be used where allowed by the protocol (or where it exists in the original input).

raise_on_defect

Causes a parser to raise error when defects are encountered instead of adding them to the Message object’s defects list.

A new policy instance, with new settings, is created using the clone() method of policy objects. clone takes any of the above controls as keyword arguments. Any control not specified in the call retains its default value. Thus you can create a policy that uses \r\n linesep characters like this:

mypolicy = compat32.clone(linesep='\r\n')

Policies can be used to make the generation of messages in the format needed by your application simpler. Instead of having to remember to specify linesep='\r\n' in all the places you call a generator, you can specify it once, when you set the policy used by the parser or the Message, whichever your program uses to create Message objects. On the other hand, if you need to generate messages in multiple forms, you can still specify the parameters in the appropriate generator call. Or you can have custom policy instances for your different cases, and pass those in when you create the generator.

Provisional Policy with New Header API

While the policy framework is worthwhile all by itself, the main motivation for introducing it is to allow the creation of new policies that implement new features for the email package in a way that maintains backward compatibility for those who do not use the new policies. Because the new policies introduce a new API, we are releasing them in Python 3.3 as a provisional policy. Backwards incompatible changes (up to and including removal of the code) may occur if deemed necessary by the core developers.

The new policies are instances of EmailPolicy, and add the following additional controls:

refold_source

Controls whether or not headers parsed by a parser are refolded by the generator. It can be none, long, or all. The default is long, which means that source headers with a line longer than max_line_length get refolded. none means no line get refolded, and all means that all lines get refolded.

header_factory

A callable that take a name and value and produces a custom header object.

The header_factory is the key to the new features provided by the new policies. When one of the new policies is used, any header retrieved from a Message object is an object produced by the header_factory, and any time you set a header on a Message it becomes an object produced by header_factory. All such header objects have a name attribute equal to the header name. Address and Date headers have additional attributes that give you access to the parsed data of the header. This means you can now do things like this:

>>> m = Message(policy=SMTP)
>>> m['To'] = 'Éric <foo@example.com>'
>>> m['to']
'Éric <foo@example.com>'
>>> m['to'].addresses
(Address(display_name='Éric', username='foo', domain='example.com'),)
>>> m['to'].addresses[0].username
'foo'
>>> m['to'].addresses[0].display_name
'Éric'
>>> m['Date'] = email.utils.localtime()
>>> m['Date'].datetime
datetime.datetime(2012, 5, 25, 21, 39, 24, 465484, tzinfo=datetime.timezone(datetime.timedelta(-1, 72000), 'EDT'))
>>> m['Date']
'Fri, 25 May 2012 21:44:27 -0400'
>>> print(m)
To: =?utf-8?q?=C3=89ric?= <foo@example.com>
Date: Fri, 25 May 2012 21:44:27 -0400

You will note that the unicode display name is automatically encoded as utf-8 when the message is serialized, but that when the header is accessed directly, you get the unicode version. This eliminates any need to deal with the email.header decode_header() or make_header() functions.

You can also create addresses from parts:

>>> m['cc'] = [Group('pals', [Address('Bob', 'bob', 'example.com'),
...                           Address('Sally', 'sally', 'example.com')]),
...            Address('Bonzo', addr_spec='bonz@laugh.com')]
>>> print(m)
To: =?utf-8?q?=C3=89ric?= <foo@example.com>
Date: Fri, 25 May 2012 21:44:27 -0400
cc: pals: Bob <bob@example.com>, Sally <sally@example.com>;, Bonzo <bonz@laugh.com>

Decoding to unicode is done automatically:

>>> m2 = message_from_string(str(m))
>>> m2['to']
'Éric <foo@example.com>'

When you parse a message, you can use the addresses and groups attributes of the header objects to access the groups and individual addresses:

>>> m2['cc'].addresses
(Address(display_name='Bob', username='bob', domain='example.com'), Address(display_name='Sally', username='sally', domain='example.com'), Address(display_name='Bonzo', username='bonz', domain='laugh.com'))
>>> m2['cc'].groups
(Group(display_name='pals', addresses=(Address(display_name='Bob', username='bob', domain='example.com'), Address(display_name='Sally', username='sally', domain='example.com')), Group(display_name=None, addresses=(Address(display_name='Bonzo', username='bonz', domain='laugh.com'),))

In summary, if you use one of the new policies, header manipulation works the way it ought to: your application works with unicode strings, and the email package transparently encodes and decodes the unicode to and from the RFC standard Content Transfer Encodings.

Other API Changes

New BytesHeaderParser, added to the parser module to complement HeaderParser and complete the Bytes API.

New utility functions:

format_datetime(): given a datetime, produce a string formatted for use in an email header.

parsedate_to_datetime(): given a date string from an email header, convert it into an aware datetime, or a naive datetime if the offset is -0000.

localtime(): With no argument, returns the current local time as an aware datetime using the local timezone. Given an aware datetime, converts it into an aware datetime using the local timezone.

ftplib

ftplib.FTP now accepts a source_address keyword argument to specify the (host, port) to use as the source address in the bind call when creating the outgoing socket. (Contributed by Giampaolo Rodolà in bpo-8594 [https://bugs.python.org/issue8594].)

The FTP_TLS class now provides a new ccc() function to revert control channel back to plaintext. This can be useful to take advantage of firewalls that know how to handle NAT with non-secure FTP without opening fixed ports. (Contributed by Giampaolo Rodolà in bpo-12139 [https://bugs.python.org/issue12139].)

Added ftplib.FTP.mlsd() method which provides a parsable directory listing format and deprecates ftplib.FTP.nlst() and ftplib.FTP.dir(). (Contributed by Giampaolo Rodolà in bpo-11072 [https://bugs.python.org/issue11072].)

functools

The functools.lru_cache() decorator now accepts a typed keyword argument (that defaults to False to ensure that it caches values of different types that compare equal in separate cache slots. (Contributed by Raymond Hettinger in bpo-13227 [https://bugs.python.org/issue13227].)

gc

It is now possible to register callbacks invoked by the garbage collector before and after collection using the new callbacks list.

hmac

A new compare_digest() function has been added to prevent side channel attacks on digests through timing analysis. (Contributed by Nick Coghlan and Christian Heimes in bpo-15061 [https://bugs.python.org/issue15061].)

http

http.server.BaseHTTPRequestHandler now buffers the headers and writes them all at once when end_headers() is called. A new method flush_headers() can be used to directly manage when the accumulated headers are sent. (Contributed by Andrew Schaaf in bpo-3709 [https://bugs.python.org/issue3709].)

http.server now produces valid HTML 4.01 strict output. (Contributed by Ezio Melotti in bpo-13295 [https://bugs.python.org/issue13295].)

http.client.HTTPResponse now has a readinto() method, which means it can be used as an io.RawIOBase class. (Contributed by John Kuhn in bpo-13464 [https://bugs.python.org/issue13464].)

html

html.parser.HTMLParser is now able to parse broken markup without raising errors, therefore the strict argument of the constructor and the HTMLParseError exception are now deprecated. The ability to parse broken markup is the result of a number of bug fixes that are also available on the latest bug fix releases of Python 2.7/3.2. (Contributed by Ezio Melotti in bpo-15114 [https://bugs.python.org/issue15114], and bpo-14538 [https://bugs.python.org/issue14538], bpo-13993 [https://bugs.python.org/issue13993], bpo-13960 [https://bugs.python.org/issue13960], bpo-13358 [https://bugs.python.org/issue13358], bpo-1745761 [https://bugs.python.org/issue1745761], bpo-755670 [https://bugs.python.org/issue755670], bpo-13357 [https://bugs.python.org/issue13357], bpo-12629 [https://bugs.python.org/issue12629], bpo-1200313 [https://bugs.python.org/issue1200313], bpo-670664 [https://bugs.python.org/issue670664], bpo-13273 [https://bugs.python.org/issue13273], bpo-12888 [https://bugs.python.org/issue12888], bpo-7311 [https://bugs.python.org/issue7311].)

A new html5 dictionary that maps HTML5 named character references to the equivalent Unicode character(s) (e.g. html5['gt;'] == '>') has been added to the html.entities module. The dictionary is now also used by HTMLParser. (Contributed by Ezio Melotti in bpo-11113 [https://bugs.python.org/issue11113] and bpo-15156 [https://bugs.python.org/issue15156].)

imaplib

The IMAP4_SSL constructor now accepts an SSLContext parameter to control parameters of the secure channel.

（由 Sijin Joseph 在 bpo-8808 [https://bugs.python.org/issue8808] 中贡献。）

inspect

A new getclosurevars() function has been added. This function reports the current binding of all names referenced from the function body and where those names were resolved, making it easier to verify correct internal state when testing code that relies on stateful closures.

由 Meador Inge 和 Nick Coghlan 在 bpo-13062 [https://bugs.python.org/issue13062] 中贡献。

A new getgeneratorlocals() function has been added. This function reports the current binding of local variables in the generator’s stack frame, making it easier to verify correct internal state when testing generators.

（由 Meador Inge 在 bpo-15153 [https://bugs.python.org/issue15153] 中贡献。）

io

The open() function has a new 'x' mode that can be used to exclusively create a new file, and raise a FileExistsError if the file already exists. It is based on the C11 ‘x’ mode to fopen().

（由 David Townshend 在 bpo-12760 [https://bugs.python.org/issue12760] 中贡献。）

The constructor of the TextIOWrapper class has a new write_through optional argument. If write_through is True, calls to write() are guaranteed not to be buffered: any data written on the TextIOWrapper object is immediately handled to its underlying binary buffer.

itertools

accumulate() now takes an optional func argument for providing a user-supplied binary function.

logging

The basicConfig() function now supports an optional handlers argument taking an iterable of handlers to be added to the root logger.

A class level attribute append_nul has been added to SysLogHandler to allow control of the appending of the NUL (\000) byte to syslog records, since for some deamons it is required while for others it is passed through to the log.

math

The math module has a new function, log2(), which returns the base-2 logarithm of x.

(Written by Mark Dickinson in bpo-11888 [https://bugs.python.org/issue11888].)

mmap

The read() method is now more compatible with other file-like objects: if the argument is omitted or specified as None, it returns the bytes from the current file position to the end of the mapping. (Contributed by Petri Lehtinen in bpo-12021 [https://bugs.python.org/issue12021].)

multiprocessing

The new multiprocessing.connection.wait() function allows polling multiple objects (such as connections, sockets and pipes) with a timeout. (Contributed by Richard Oudkerk in bpo-12328 [https://bugs.python.org/issue12328].)

multiprocessing.Connection objects can now be transferred over multiprocessing connections. (Contributed by Richard Oudkerk in bpo-4892 [https://bugs.python.org/issue4892].)

multiprocessing.Process now accepts a daemon keyword argument to override the default behavior of inheriting the daemon flag from the parent process (bpo-6064 [https://bugs.python.org/issue6064]).

New attribute multiprocessing.Process.sentinel allows a program to wait on multiple Process objects at one time using the appropriate OS primitives (for example, select on posix systems).

New methods multiprocessing.pool.Pool.starmap() and starmap_async() provide itertools.starmap() equivalents to the existing multiprocessing.pool.Pool.map() and map_async() functions. (Contributed by Hynek Schlawack in bpo-12708 [https://bugs.python.org/issue12708].)

nntplib

The nntplib.NNTP class now supports the context management protocol to unconditionally consume socket.error exceptions and to close the NNTP connection when done:

>>> from nntplib import NNTP
>>> with NNTP('news.gmane.org') as n:
...     n.group('gmane.comp.python.committers')
...
('211 1755 1 1755 gmane.comp.python.committers', 1755, 1, 1755, 'gmane.comp.python.committers')
>>>

（由 Giampaolo Rodolà 在 bpo-9795 [https://bugs.python.org/issue9795] 中贡献。）

os

The os module has a new pipe2() function that makes it possible to create a pipe with O_CLOEXEC or O_NONBLOCK flags set atomically. This is especially useful to avoid race conditions in multi-threaded programs.

The os module has a new sendfile() function which provides an efficient “zero-copy” way for copying data from one file (or socket) descriptor to another. The phrase “zero-copy” refers to the fact that all of the copying of data between the two descriptors is done entirely by the kernel, with no copying of data into userspace buffers. sendfile() can be used to efficiently copy data from a file on disk to a network socket, e.g. for downloading a file.

(Patch submitted by Ross Lagerwall and Giampaolo Rodolà in bpo-10882 [https://bugs.python.org/issue10882].)

To avoid race conditions like symlink attacks and issues with temporary files and directories, it is more reliable (and also faster) to manipulate file descriptors instead of file names. Python 3.3 enhances existing functions and introduces new functions to work on file descriptors (bpo-4761 [https://bugs.python.org/issue4761], bpo-10755 [https://bugs.python.org/issue10755] and bpo-14626 [https://bugs.python.org/issue14626]).

The os module has a new fwalk() function similar to walk() except that it also yields file descriptors referring to the directories visited. This is especially useful to avoid symlink races.

The following functions get new optional dir_fd (paths relative to directory descriptors) and/or follow_symlinks (not following symlinks): access(), chflags(), chmod(), chown(), link(), lstat(), mkdir(), mkfifo(), mknod(), open(), readlink(), remove(), rename(), replace(), rmdir(), stat(), symlink(), unlink(), utime(). Platform support for using these parameters can be checked via the sets os.supports_dir_fd and os.supports_follows_symlinks.

The following functions now support a file descriptor for their path argument: chdir(), chmod(), chown(), execve(), listdir(), pathconf(), exists(), stat(), statvfs(), utime(). Platform support for this can be checked via the os.supports_fd set.

access() accepts an effective_ids keyword argument to turn on using the effective uid/gid rather than the real uid/gid in the access check. Platform support for this can be checked via the supports_effective_ids set.

The os module has two new functions: getpriority() and setpriority(). They can be used to get or set process niceness/priority in a fashion similar to os.nice() but extended to all processes instead of just the current one.

(Patch submitted by Giampaolo Rodolà in bpo-10784 [https://bugs.python.org/issue10784].)

The new os.replace() function allows cross-platform renaming of a file with overwriting the destination. With os.rename(), an existing destination file is overwritten under POSIX, but raises an error under Windows. (Contributed by Antoine Pitrou in bpo-8828 [https://bugs.python.org/issue8828].)

The stat family of functions (stat(), fstat(), and lstat()) now support reading a file’s timestamps with nanosecond precision. Symmetrically, utime() can now write file timestamps with nanosecond precision. (Contributed by Larry Hastings in bpo-14127 [https://bugs.python.org/issue14127].)

The new os.get_terminal_size() function queries the size of the terminal attached to a file descriptor. See also shutil.get_terminal_size(). (Contributed by Zbigniew Jędrzejewski-Szmek in bpo-13609 [https://bugs.python.org/issue13609].)

New functions to support Linux extended attributes (bpo-12720 [https://bugs.python.org/issue12720]): getxattr(), listxattr(), removexattr(), setxattr().

New interface to the scheduler. These functions control how a process is allocated CPU time by the operating system. New functions: sched_get_priority_max(), sched_get_priority_min(), sched_getaffinity(), sched_getparam(), sched_getscheduler(), sched_rr_get_interval(), sched_setaffinity(), sched_setparam(), sched_setscheduler(), sched_yield(),

New functions to control the file system:

posix_fadvise(): Announces an intention to access data in a specific pattern thus allowing the kernel to make optimizations.

posix_fallocate(): Ensures that enough disk space is allocated for a file.

sync(): Force write of everything to disk.

Additional new posix functions:

lockf(): Apply, test or remove a POSIX lock on an open file descriptor.

pread(): Read from a file descriptor at an offset, the file offset remains unchanged.

pwrite(): Write to a file descriptor from an offset, leaving the file offset unchanged.

readv(): Read from a file descriptor into a number of writable buffers.

truncate(): Truncate the file corresponding to path, so that it is at most length bytes in size.

waitid(): Wait for the completion of one or more child processes.

writev(): Write the contents of buffers to a file descriptor, where buffers is an arbitrary sequence of buffers.

getgrouplist() (bpo-9344 [https://bugs.python.org/issue9344]): Return list of group ids that specified user belongs to.

times() and uname(): Return type changed from a tuple to a tuple-like object with named attributes.

Some platforms now support additional constants for the lseek() function, such as os.SEEK_HOLE and os.SEEK_DATA.

New constants RTLD_LAZY, RTLD_NOW, RTLD_GLOBAL, RTLD_LOCAL, RTLD_NODELETE, RTLD_NOLOAD, and RTLD_DEEPBIND are available on platforms that support them. These are for use with the sys.setdlopenflags() function, and supersede the similar constants defined in ctypes and DLFCN. (Contributed by Victor Stinner in bpo-13226 [https://bugs.python.org/issue13226].)

os.symlink() now accepts (and ignores) the target_is_directory keyword argument on non-Windows platforms, to ease cross-platform support.

pdb

Tab-completion is now available not only for command names, but also their arguments. For example, for the break command, function and file names are completed.

（由 Georg Brandl 在 bpo-14210 [https://bugs.python.org/issue14210] 中贡献）

pickle

pickle.Pickler objects now have an optional dispatch_table attribute allowing per-pickler reduction functions to be set.

（由 Richard Oudkerk 在 bpo-14166 [https://bugs.python.org/issue14166] 中贡献。）

pydoc

The Tk GUI and the serve() function have been removed from the pydoc module: pydoc -g and serve() have been deprecated in Python 3.2.

re

str regular expressions now support \u and \U escapes.

（由 Serhiy Storchaka 在 bpo-3665 [https://bugs.python.org/issue3665] 中贡献。）

sched

run() now accepts a blocking parameter which when set to false makes the method execute the scheduled events due to expire soonest (if any) and then return immediately. This is useful in case you want to use the scheduler in non-blocking applications. (Contributed by Giampaolo Rodolà in bpo-13449 [https://bugs.python.org/issue13449].)

scheduler class can now be safely used in multi-threaded environments. (Contributed by Josiah Carlson and Giampaolo Rodolà in bpo-8684 [https://bugs.python.org/issue8684].)

timefunc and delayfunct parameters of scheduler class constructor are now optional and defaults to time.time() and time.sleep() respectively. (Contributed by Chris Clark in bpo-13245 [https://bugs.python.org/issue13245].)

enter() and enterabs() argument parameter is now optional. (Contributed by Chris Clark in bpo-13245 [https://bugs.python.org/issue13245].)

enter() and enterabs() now accept a kwargs parameter. (Contributed by Chris Clark in bpo-13245 [https://bugs.python.org/issue13245].)

select

Solaris and derivative platforms have a new class select.devpoll for high performance asynchronous sockets via /dev/poll. (Contributed by Jesús Cea Avión in bpo-6397 [https://bugs.python.org/issue6397].)

shlex

The previously undocumented helper function quote from the pipes modules has been moved to the shlex module and documented. quote() properly escapes all characters in a string that might be otherwise given special meaning by the shell.

shutil

新的函数：

disk_usage(): provides total, used and free disk space statistics. (Contributed by Giampaolo Rodolà in bpo-12442 [https://bugs.python.org/issue12442].)

chown(): allows one to change user and/or group of the given path also specifying the user/group names and not only their numeric ids. (Contributed by Sandro Tosi in bpo-12191 [https://bugs.python.org/issue12191].)

shutil.get_terminal_size(): returns the size of the terminal window to which the interpreter is attached. (Contributed by Zbigniew Jędrzejewski-Szmek in bpo-13609 [https://bugs.python.org/issue13609].)

copy2() and copystat() now preserve file timestamps with nanosecond precision on platforms that support it. They also preserve file “extended attributes” on Linux. (Contributed by Larry Hastings in bpo-14127 [https://bugs.python.org/issue14127] and bpo-15238 [https://bugs.python.org/issue15238].)

Several functions now take an optional symlinks argument: when that parameter is true, symlinks aren’t dereferenced and the operation instead acts on the symlink itself (or creates one, if relevant). (Contributed by Hynek Schlawack in bpo-12715 [https://bugs.python.org/issue12715].)

When copying files to a different file system, move() now handles symlinks the way the posix mv command does, recreating the symlink rather than copying the target file contents. (Contributed by Jonathan Niehof in bpo-9993 [https://bugs.python.org/issue9993].) move() now also returns the dst argument as its result.

rmtree() is now resistant to symlink attacks on platforms which support the new dir_fd parameter in os.open() and os.unlink(). (Contributed by Martin von Löwis and Hynek Schlawack in bpo-4489 [https://bugs.python.org/issue4489].)

signal

The signal module has new functions:

pthread_sigmask(): fetch and/or change the signal mask of the calling thread (Contributed by Jean-Paul Calderone in bpo-8407 [https://bugs.python.org/issue8407]);

pthread_kill(): send a signal to a thread;

sigpending(): examine pending functions;

sigwait(): wait a signal;

sigwaitinfo(): wait for a signal, returning detailed information about it;

sigtimedwait(): like sigwaitinfo() but with a timeout.

The signal handler writes the signal number as a single byte instead of a nul byte into the wakeup file descriptor. So it is possible to wait more than one signal and know which signals were raised.

signal.signal() and signal.siginterrupt() raise an OSError, instead of a RuntimeError: OSError has an errno attribute.

smtpd

The smtpd module now supports RFC 5321 [https://tools.ietf.org/html/rfc5321.html] (extended SMTP) and RFC 1870 [https://tools.ietf.org/html/rfc1870.html] (size extension). Per the standard, these extensions are enabled if and only if the client initiates the session with an EHLO command.

(Initial ELHO support by Alberto Trevino. Size extension by Juhana Jauhiainen. Substantial additional work on the patch contributed by Michele Orrù and Dan Boswell. bpo-8739 [https://bugs.python.org/issue8739])

smtplib

The SMTP, SMTP_SSL, and LMTP classes now accept a source_address keyword argument to specify the (host, port) to use as the source address in the bind call when creating the outgoing socket. (Contributed by Paulo Scardine in bpo-11281 [https://bugs.python.org/issue11281].)

SMTP now supports the context management protocol, allowing an SMTP instance to be used in a with statement. (Contributed by Giampaolo Rodolà in bpo-11289 [https://bugs.python.org/issue11289].)

The SMTP_SSL constructor and the starttls() method now accept an SSLContext parameter to control parameters of the secure channel. (Contributed by Kasun Herath in bpo-8809 [https://bugs.python.org/issue8809].)

socket

The socket class now exposes additional methods to process ancillary data when supported by the underlying platform:

sendmsg()

recvmsg()

recvmsg_into()

(Contributed by David Watson in bpo-6560 [https://bugs.python.org/issue6560], based on an earlier patch by Heiko Wundram)

The socket class now supports the PF_CAN protocol family (https://en.wikipedia.org/wiki/Socketcan), on Linux (https://lwn.net/Articles/253425).

(Contributed by Matthias Fuchs, updated by Tiago Gonçalves in bpo-10141 [https://bugs.python.org/issue10141].)

The socket class now supports the PF_RDS protocol family (https://en.wikipedia.org/wiki/Reliable_Datagram_Sockets and https://oss.oracle.com/projects/rds/).

The socket class now supports the PF_SYSTEM protocol family on OS X. (Contributed by Michael Goderbauer in bpo-13777 [https://bugs.python.org/issue13777].)

New function sethostname() allows the hostname to be set on unix systems if the calling process has sufficient privileges. (Contributed by Ross Lagerwall in bpo-10866 [https://bugs.python.org/issue10866].)

socketserver

BaseServer now has an overridable method service_actions() that is called by the serve_forever() method in the service loop. ForkingMixIn now uses this to clean up zombie child processes. (Contributed by Justin Warkentin in bpo-11109 [https://bugs.python.org/issue11109].)

sqlite3

New sqlite3.Connection method set_trace_callback() can be used to capture a trace of all sql commands processed by sqlite. (Contributed by Torsten Landschoff in bpo-11688 [https://bugs.python.org/issue11688].)

ssl

The ssl module has two new random generation functions:

RAND_bytes(): generate cryptographically strong pseudo-random bytes.

RAND_pseudo_bytes(): generate pseudo-random bytes.

（由 Victor Stinner 在 bpo-12049 [https://bugs.python.org/issue12049] 中贡献。）

The ssl module now exposes a finer-grained exception hierarchy in order to make it easier to inspect the various kinds of errors. (Contributed by Antoine Pitrou in bpo-11183 [https://bugs.python.org/issue11183].)

load_cert_chain() now accepts a password argument to be used if the private key is encrypted. (Contributed by Adam Simpkins in bpo-12803 [https://bugs.python.org/issue12803].)

Diffie-Hellman key exchange, both regular and Elliptic Curve-based, is now supported through the load_dh_params() and set_ecdh_curve() methods. (Contributed by Antoine Pitrou in bpo-13626 [https://bugs.python.org/issue13626] and bpo-13627 [https://bugs.python.org/issue13627].)

SSL sockets have a new get_channel_binding() method allowing the implementation of certain authentication mechanisms such as SCRAM-SHA-1-PLUS. (Contributed by Jacek Konieczny in bpo-12551 [https://bugs.python.org/issue12551].)

You can query the SSL compression algorithm used by an SSL socket, thanks to its new compression() method. The new attribute OP_NO_COMPRESSION can be used to disable compression. (Contributed by Antoine Pitrou in bpo-13634 [https://bugs.python.org/issue13634].)

Support has been added for the Next Protocol Negotiation extension using the ssl.SSLContext.set_npn_protocols() method. (Contributed by Colin Marc in bpo-14204 [https://bugs.python.org/issue14204].)

SSL errors can now be introspected more easily thanks to library and reason attributes. (Contributed by Antoine Pitrou in bpo-14837 [https://bugs.python.org/issue14837].)

The get_server_certificate() function now supports IPv6. (Contributed by Charles-François Natali in bpo-11811 [https://bugs.python.org/issue11811].)

New attribute OP_CIPHER_SERVER_PREFERENCE allows setting SSLv3 server sockets to use the server’s cipher ordering preference rather than the client’s (bpo-13635 [https://bugs.python.org/issue13635]).

stat

The undocumented tarfile.filemode function has been moved to stat.filemode(). It can be used to convert a file’s mode to a string of the form ‘-rwxrwxrwx’.

（由 Giampaolo Rodolà 在 bpo-14807 [https://bugs.python.org/issue14807] 中贡献。）

struct

The struct module now supports ssize_t and size_t via the new codes n and N, respectively. (Contributed by Antoine Pitrou in bpo-3163 [https://bugs.python.org/issue3163].)

subprocess

Command strings can now be bytes objects on posix platforms. (Contributed by Victor Stinner in bpo-8513 [https://bugs.python.org/issue8513].)

A new constant DEVNULL allows suppressing output in a platform-independent fashion. (Contributed by Ross Lagerwall in bpo-5870 [https://bugs.python.org/issue5870].)

sys

The sys module has a new thread_info struct sequence holding informations about the thread implementation (bpo-11223 [https://bugs.python.org/issue11223]).

tarfile

tarfile now supports lzma encoding via the lzma module. (Contributed by Lars Gustäbel in bpo-5689 [https://bugs.python.org/issue5689].)

tempfile

tempfile.SpooledTemporaryFile’s truncate() method now accepts a size parameter. (Contributed by Ryan Kelly in bpo-9957 [https://bugs.python.org/issue9957].)

textwrap

The textwrap module has a new indent() that makes it straightforward to add a common prefix to selected lines in a block of text (bpo-13857 [https://bugs.python.org/issue13857]).

threading

threading.Condition, threading.Semaphore, threading.BoundedSemaphore, threading.Event, and threading.Timer, all of which used to be factory functions returning a class instance, are now classes and may be subclassed. (Contributed by Éric Araujo in bpo-10968 [https://bugs.python.org/issue10968].)

The threading.Thread constructor now accepts a daemon keyword argument to override the default behavior of inheriting the deamon flag value from the parent thread (bpo-6064 [https://bugs.python.org/issue6064]).

The formerly private function _thread.get_ident is now available as the public function threading.get_ident(). This eliminates several cases of direct access to the _thread module in the stdlib. Third party code that used _thread.get_ident should likewise be changed to use the new public interface.

time

The PEP 418 [https://www.python.org/dev/peps/pep-0418] added new functions to the time module:

get_clock_info(): Get information on a clock.

monotonic(): Monotonic clock (cannot go backward), not affected by system clock updates.

perf_counter(): Performance counter with the highest available resolution to measure a short duration.

process_time(): Sum of the system and user CPU time of the current process.

Other new functions:

clock_getres(), clock_gettime() and clock_settime() functions with CLOCK_xxx constants. (Contributed by Victor Stinner in bpo-10278 [https://bugs.python.org/issue10278].)

To improve cross platform consistency, sleep() now raises a ValueError when passed a negative sleep value. Previously this was an error on posix, but produced an infinite sleep on Windows.

types

Add a new types.MappingProxyType class: Read-only proxy of a mapping. (bpo-14386 [https://bugs.python.org/issue14386])

The new functions types.new_class() and types.prepare_class() provide support for PEP 3115 compliant dynamic type creation. (bpo-14588 [https://bugs.python.org/issue14588])

unittest

assertRaises(), assertRaisesRegex(), assertWarns(), and assertWarnsRegex() now accept a keyword argument msg when used as context managers. (Contributed by Ezio Melotti and Winston Ewert in bpo-10775 [https://bugs.python.org/issue10775].)

unittest.TestCase.run() now returns the TestResult object.

urllib

The Request class, now accepts a method argument used by get_method() to determine what HTTP method should be used. For example, this will send a 'HEAD' request:

>>> urlopen(Request('https://www.python.org', method='HEAD'))

(bpo-1673007 [https://bugs.python.org/issue1673007])

webbrowser

The webbrowser module supports more “browsers”: Google Chrome (named chrome, chromium, chrome-browser or chromium-browser depending on the version and operating system), and the generic launchers xdg-open, from the FreeDesktop.org project, and gvfs-open, which is the default URI handler for GNOME 3. (The former contributed by Arnaud Calmettes in bpo-13620 [https://bugs.python.org/issue13620], the latter by Matthias Klose in bpo-14493 [https://bugs.python.org/issue14493].)

xml.etree.ElementTree

The xml.etree.ElementTree module now imports its C accelerator by default; there is no longer a need to explicitly import xml.etree.cElementTree (this module stays for backwards compatibility, but is now deprecated). In addition, the iter family of methods of Element has been optimized (rewritten in C). The module’s documentation has also been greatly improved with added examples and a more detailed reference.

zlib

New attribute zlib.Decompress.eof makes it possible to distinguish between a properly-formed compressed stream and an incomplete or truncated one. (Contributed by Nadeem Vawda in bpo-12646 [https://bugs.python.org/issue12646].)

New attribute zlib.ZLIB_RUNTIME_VERSION reports the version string of the underlying zlib library that is loaded at runtime. (Contributed by Torsten Landschoff in bpo-12306 [https://bugs.python.org/issue12306].)

性能优化

Major performance enhancements have been added:

Thanks to PEP 393 [https://www.python.org/dev/peps/pep-0393], some operations on Unicode strings have been optimized:

the memory footprint is divided by 2 to 4 depending on the text

encode an ASCII string to UTF-8 doesn’t need to encode characters anymore, the UTF-8 representation is shared with the ASCII representation

the UTF-8 encoder has been optimized

repeating a single ASCII letter and getting a substring of an ASCII string is 4 times faster

UTF-8 is now 2x to 4x faster. UTF-16 encoding is now up to 10x faster.

(Contributed by Serhiy Storchaka, bpo-14624 [https://bugs.python.org/issue14624], bpo-14738 [https://bugs.python.org/issue14738] and bpo-15026 [https://bugs.python.org/issue15026].)

构建和 C API 的改变

Changes to Python’s build process and to the C API include:

新的 PEP 3118 [https://www.python.org/dev/peps/pep-3118] 相关功能：

PyMemoryView_FromMemory()

PEP 393 [https://www.python.org/dev/peps/pep-0393] 添加了新的 Unicode 类型，宏和函数

高阶 API

PyUnicode_CopyCharacters()

PyUnicode_FindChar()

PyUnicode_GetLength(), PyUnicode_GET_LENGTH

PyUnicode_New()

PyUnicode_Substring()

PyUnicode_ReadChar(), PyUnicode_WriteChar()

低阶 API:

Py_UCS1, Py_UCS2, Py_UCS4 types

PyASCIIObject 和 PyCompactUnicodeObject 结构

PyUnicode_READY

PyUnicode_FromKindAndData()

PyUnicode_AsUCS4(), PyUnicode_AsUCS4Copy()

PyUnicode_DATA, PyUnicode_1BYTE_DATA, PyUnicode_2BYTE_DATA, PyUnicode_4BYTE_DATA

PyUnicode_KIND 使用 PyUnicode_Kind 枚举: PyUnicode_WCHAR_KIND, PyUnicode_1BYTE_KIND, PyUnicode_2BYTE_KIND, PyUnicode_4BYTE_KIND

PyUnicode_READ, PyUnicode_READ_CHAR, PyUnicode_WRITE

PyUnicode_MAX_CHAR_VALUE

PyArg_ParseTuple 现在接受 c 格式的 bytearray (bpo-12380 [https://bugs.python.org/issue12380])。

弃用

不支持的操作系统

由于缺少维护人员，不再支持 OS/2 和 VMS 系统 。

由于维护负担，将 COMSPEC 设置为 command.com 的 Windows平台（含Windows 2000）不再受支持。

OSF支持在3.2中被弃用，现在已经被完全删除。

已弃用的 Python 模块、函数和方法

Passing a non-empty string to object.__format__() is deprecated, and will produce a TypeError in Python 3.4 (bpo-9856 [https://bugs.python.org/issue9856]).

The unicode_internal codec has been deprecated because of the PEP 393 [https://www.python.org/dev/peps/pep-0393], use UTF-8, UTF-16 (utf-16-le or utf-16-be), or UTF-32 (utf-32-le or utf-32-be)

ftplib.FTP.nlst() and ftplib.FTP.dir(): use ftplib.FTP.mlsd()

platform.popen(): use the subprocess module. Check especially the 使用 subprocess 模块替换旧函数 section (bpo-11377 [https://bugs.python.org/issue11377]).

bpo-13374 [https://bugs.python.org/issue13374]: The Windows bytes API has been deprecated in the os module. Use Unicode filenames, instead of bytes filenames, to not depend on the ANSI code page anymore and to support any filename.

bpo-13988 [https://bugs.python.org/issue13988]: The xml.etree.cElementTree module is deprecated. The accelerator is used automatically whenever available.

The behaviour of time.clock() depends on the platform: use the new time.perf_counter() or time.process_time() function instead, depending on your requirements, to have a well defined behaviour.

The os.stat_float_times() function is deprecated.

abc module:

abc.abstractproperty has been deprecated, use property with abc.abstractmethod() instead.

abc.abstractclassmethod has been deprecated, use classmethod with abc.abstractmethod() instead.

abc.abstractstaticmethod has been deprecated, use staticmethod with abc.abstractmethod() instead.

importlib package:

importlib.abc.SourceLoader.path_mtime() is now deprecated in favour of importlib.abc.SourceLoader.path_stats() as bytecode files now store both the modification time and size of the source file the bytecode file was compiled from.

已弃用的 C API 函数和类型

Py_UNICODE 已经在 PEP 393 [https://www.python.org/dev/peps/pep-0393] 弃用，并将于 Python 4 中移除。所有使用此类型的函数都已弃用：

Unicode functions and methods using Py_UNICODE and Py_UNICODE* types:

PyUnicode_FromUnicode: 使用 PyUnicode_FromWideChar() 或 PyUnicode_FromKindAndData()

PyUnicode_AS_UNICODE, PyUnicode_AsUnicode(), PyUnicode_AsUnicodeAndSize(): 使用 PyUnicode_AsWideCharString()

PyUnicode_AS_DATA: 使用 PyUnicode_DATA 以及 PyUnicode_READ 和 PyUnicode_WRITE

PyUnicode_GET_SIZE, PyUnicode_GetSize(): 使用 PyUnicode_GET_LENGTH 或 PyUnicode_GetLength()

PyUnicode_GET_DATA_SIZE: 使用 PyUnicode_GET_LENGTH(str) * PyUnicode_KIND(str) (仅适用于现成的字符串)

PyUnicode_AsUnicodeCopy(): 使用 PyUnicode_AsUCS4Copy() 或 PyUnicode_AsWideCharString()

PyUnicode_GetMax()

Functions and macros manipulating Py_UNICODE* strings:

Py_UNICODE_strlen: 使用 PyUnicode_GetLength() 或 PyUnicode_GET_LENGTH

Py_UNICODE_strcat: 使用 PyUnicode_CopyCharacters() 或 PyUnicode_FromFormat()

Py_UNICODE_strcpy, Py_UNICODE_strncpy, Py_UNICODE_COPY: 使用 PyUnicode_CopyCharacters() 或 PyUnicode_Substring()

Py_UNICODE_strcmp: 使用 PyUnicode_Compare()

Py_UNICODE_strncmp: 使用 PyUnicode_Tailmatch()

Py_UNICODE_strchr, Py_UNICODE_strrchr: 使用 PyUnicode_FindChar()

Py_UNICODE_FILL: 使用 PyUnicode_Fill()

Py_UNICODE_MATCH

编码器:

PyUnicode_Encode(): 使用 PyUnicode_AsEncodedObject()

PyUnicode_EncodeUTF7()

PyUnicode_EncodeUTF8(): 使用 PyUnicode_AsUTF8() 或 PyUnicode_AsUTF8String()

PyUnicode_EncodeUTF32()

PyUnicode_EncodeUTF16()

PyUnicode_EncodeUnicodeEscape:() 使用 PyUnicode_AsUnicodeEscapeString()

PyUnicode_EncodeRawUnicodeEscape:() 使用 PyUnicode_AsRawUnicodeEscapeString()

PyUnicode_EncodeLatin1(): 使用 PyUnicode_AsLatin1String()

PyUnicode_EncodeASCII(): 使用 PyUnicode_AsASCIIString()

PyUnicode_EncodeCharmap()

PyUnicode_TranslateCharmap()

PyUnicode_EncodeMBCS(): 使用 PyUnicode_AsMBCSString() 或 PyUnicode_EncodeCodePage() (和 CP_ACP code_page)

PyUnicode_EncodeDecimal(), PyUnicode_TransformDecimalToASCII()

弃用的功能

The array module’s 'u' format code is now deprecated and will be removed in Python 4 together with the rest of the (Py_UNICODE) API.

移植到 Python 3.3

本节列出了先前描述的更改以及可能需要更改代码的其他错误修正.

Porting Python code

Hash randomization is enabled by default. Set the PYTHONHASHSEED environment variable to 0 to disable hash randomization. See also the object.__hash__() method.

bpo-12326 [https://bugs.python.org/issue12326]: On Linux, sys.platform doesn’t contain the major version anymore. It is now always ‘linux’, instead of ‘linux2’ or ‘linux3’ depending on the Linux version used to build Python. Replace sys.platform == ‘linux2’ with sys.platform.startswith(‘linux’), or directly sys.platform == ‘linux’ if you don’t need to support older Python versions.

bpo-13847 [https://bugs.python.org/issue13847], bpo-14180 [https://bugs.python.org/issue14180]: time and datetime: OverflowError is now raised instead of ValueError if a timestamp is out of range. OSError is now raised if C functions gmtime() or localtime() failed.

The default finders used by import now utilize a cache of what is contained within a specific directory. If you create a Python source file or sourceless bytecode file, make sure to call importlib.invalidate_caches() to clear out the cache for the finders to notice the new file.

ImportError now uses the full name of the module that was attempted to be imported. Doctests that check ImportErrors’ message will need to be updated to use the full name of the module instead of just the tail of the name.

The index argument to __import__() now defaults to 0 instead of -1 and no longer support negative values. It was an oversight when PEP 328 [https://www.python.org/dev/peps/pep-0328] was implemented that the default value remained -1. If you need to continue to perform a relative import followed by an absolute import, then perform the relative import using an index of 1, followed by another import using an index of 0. It is preferred, though, that you use importlib.import_module() rather than call __import__() directly.

__import__() no longer allows one to use an index value other than 0 for top-level modules. E.g. __import__('sys', level=1) is now an error.

Because sys.meta_path and sys.path_hooks now have finders on them by default, you will most likely want to use list.insert() instead of list.append() to add to those lists.

Because None is now inserted into sys.path_importer_cache, if you are clearing out entries in the dictionary of paths that do not have a finder, you will need to remove keys paired with values of None and imp.NullImporter to be backwards-compatible. This will lead to extra overhead on older versions of Python that re-insert None into sys.path_importer_cache where it represents the use of implicit finders, but semantically it should not change anything.

importlib.abc.Finder no longer specifies a find_module() abstract method that must be implemented. If you were relying on subclasses to implement that method, make sure to check for the method’s existence first. You will probably want to check for find_loader() first, though, in the case of working with path entry finders.

pkgutil has been converted to use importlib internally. This eliminates many edge cases where the old behaviour of the PEP 302 import emulation failed to match the behaviour of the real import system. The import emulation itself is still present, but is now deprecated. The pkgutil.iter_importers() and pkgutil.walk_packages() functions special case the standard import hooks so they are still supported even though they do not provide the non-standard iter_modules() method.

A longstanding RFC-compliance bug (bpo-1079 [https://bugs.python.org/issue1079]) in the parsing done by email.header.decode_header() has been fixed. Code that uses the standard idiom to convert encoded headers into unicode (str(make_header(decode_header(h))) will see no change, but code that looks at the individual tuples returned by decode_header will see that whitespace that precedes or follows ASCII sections is now included in the ASCII section. Code that builds headers using make_header should also continue to work without change, since make_header continues to add whitespace between ASCII and non-ASCII sections if it is not already present in the input strings.

email.utils.formataddr() now does the correct content transfer encoding when passed non-ASCII display names. Any code that depended on the previous buggy behavior that preserved the non-ASCII unicode in the formatted output string will need to be changed (bpo-1690608 [https://bugs.python.org/issue1690608]).

poplib.POP3.quit() may now raise protocol errors like all other poplib methods. Code that assumes quit does not raise poplib.error_proto errors may need to be changed if errors on quit are encountered by a particular application (bpo-11291 [https://bugs.python.org/issue11291]).

The strict argument to email.parser.Parser, deprecated since Python 2.4, has finally been removed.

The deprecated method unittest.TestCase.assertSameElements has been removed.

The deprecated variable time.accept2dyear has been removed.

The deprecated Context._clamp attribute has been removed from the decimal module. It was previously replaced by the public attribute clamp. (See bpo-8540 [https://bugs.python.org/issue8540].)

The undocumented internal helper class SSLFakeFile has been removed from smtplib, since its functionality has long been provided directly by socket.socket.makefile().

Passing a negative value to time.sleep() on Windows now raises an error instead of sleeping forever. It has always raised an error on posix.

The ast.__version__ constant has been removed. If you need to make decisions affected by the AST version, use sys.version_info to make the decision.

Code that used to work around the fact that the threading module used factory functions by subclassing the private classes will need to change to subclass the now-public classes.

The undocumented debugging machinery in the threading module has been removed, simplifying the code. This should have no effect on production code, but is mentioned here in case any application debug frameworks were interacting with it (bpo-13550 [https://bugs.python.org/issue13550]).

Porting C code

In the course of changes to the buffer API the undocumented smalltable member of the Py_buffer structure has been removed and the layout of the PyMemoryViewObject has changed.

All extensions relying on the relevant parts in memoryobject.h or object.h must be rebuilt.

Due to PEP 393, the Py_UNICODE type and all functions using this type are deprecated (but will stay available for at least five years). If you were using low-level Unicode APIs to construct and access unicode objects and you want to benefit of the memory footprint reduction provided by PEP 393, you have to convert your code to the new Unicode API.

However, if you only have been using high-level functions such as PyUnicode_Concat(), PyUnicode_Join() or PyUnicode_FromFormat(), your code will automatically take advantage of the new unicode representations.

PyImport_GetMagicNumber() now returns -1 upon failure.

As a negative value for the level argument to __import__() is no longer valid, the same now holds for PyImport_ImportModuleLevel(). This also means that the value of level used by PyImport_ImportModuleEx() is now 0 instead of -1.

Building C extensions

The range of possible file names for C extensions has been narrowed. Very rarely used spellings have been suppressed: under POSIX, files named xxxmodule.so, xxxmodule.abi3.so and xxxmodule.cpython-*.so are no longer recognized as implementing the xxx module. If you had been generating such files, you have to switch to the other spellings (i.e., remove the module string from the file names).

(implemented in bpo-14040 [https://bugs.python.org/issue14040].)

Command Line Switch Changes

The -Q command-line flag and related artifacts have been removed. Code checking sys.flags.division_warning will need updating.

（bpo-14807 [https://bugs.python.org/issue14807]，由 Éric Araujo 贡献。）

When python is started with -S, import site will no longer add site-specific paths to the module search paths. In previous versions, it did.

(bpo-11591 [https://bugs.python.org/issue11591], contributed by Carl Meyer with editions by Éric Araujo.)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

Python 3.2 有什么新变化

作者

Raymond Hettinger

This article explains the new features in Python 3.2 as compared to 3.1. It focuses on a few highlights and gives a few examples. For full details, see the Misc/NEWS [https://hg.python.org/cpython/file/3.2/Misc/NEWS] file.

参见

PEP 392 [https://www.python.org/dev/peps/pep-0392] - Python 3.2 发布计划

PEP 384: 定义稳定的ABI

In the past, extension modules built for one Python version were often not usable with other Python versions. Particularly on Windows, every feature release of Python required rebuilding all extension modules that one wanted to use. This requirement was the result of the free access to Python interpreter internals that extension modules could use.

With Python 3.2, an alternative approach becomes available: extension modules which restrict themselves to a limited API (by defining Py_LIMITED_API) cannot use many of the internals, but are constrained to a set of API functions that are promised to be stable for several releases. As a consequence, extension modules built for 3.2 in that mode will also work with 3.3, 3.4, and so on. Extension modules that make use of details of memory structures can still be built, but will need to be recompiled for every feature release.

参见

PEP 384 [https://www.python.org/dev/peps/pep-0384] - 定义稳定的ABI

PEP 由 Martin von Löwis 撰写

PEP 389: Argparse 命令行解析模块

A new module for command line parsing, argparse, was introduced to overcome the limitations of optparse which did not provide support for positional arguments (not just options), subcommands, required options and other common patterns of specifying and validating options.

This module has already had widespread success in the community as a third-party module. Being more fully featured than its predecessor, the argparse module is now the preferred module for command-line processing. The older module is still being kept available because of the substantial amount of legacy code that depends on it.

Here’s an annotated example parser showing features like limiting results to a set of choices, specifying a metavar in the help screen, validating that one or more positional arguments is present, and making a required option:

import argparse
parser = argparse.ArgumentParser(
            description = 'Manage servers',         # main description for help
            epilog = 'Tested on Solaris and Linux') # displayed after help
parser.add_argument('action',                       # argument name
            choices = ['deploy', 'start', 'stop'],  # three allowed values
            help = 'action on each target')         # help msg
parser.add_argument('targets',
            metavar = 'HOSTNAME',                   # var name used in help msg
            nargs = '+',                            # require one or more targets
            help = 'url for target machines')       # help msg explanation
parser.add_argument('-u', '--user',                 # -u or --user option
            required = True,                        # make it a required argument
            help = 'login as user')

Example of calling the parser on a command string:

>>> cmd = 'deploy sneezy.example.com sleepy.example.com -u skycaptain'
>>> result = parser.parse_args(cmd.split())
>>> result.action
'deploy'
>>> result.targets
['sneezy.example.com', 'sleepy.example.com']
>>> result.user
'skycaptain'

Example of the parser’s automatically generated help:

>>> parser.parse_args('-h'.split())

usage: manage_cloud.py [-h] -u USER
                       {deploy,start,stop} HOSTNAME [HOSTNAME ...]

Manage servers

positional arguments:
  {deploy,start,stop}   action on each target
  HOSTNAME              url for target machines

optional arguments:
  -h, --help            show this help message and exit
  -u USER, --user USER  login as user

Tested on Solaris and Linux

An especially nice argparse feature is the ability to define subparsers, each with their own argument patterns and help displays:

import argparse
parser = argparse.ArgumentParser(prog='HELM')
subparsers = parser.add_subparsers()

parser_l = subparsers.add_parser('launch', help='Launch Control')   # first subgroup
parser_l.add_argument('-m', '--missiles', action='store_true')
parser_l.add_argument('-t', '--torpedos', action='store_true')

parser_m = subparsers.add_parser('move', help='Move Vessel',        # second subgroup
                                 aliases=('steer', 'turn'))         # equivalent names
parser_m.add_argument('-c', '--course', type=int, required=True)
parser_m.add_argument('-s', '--speed', type=int, default=0)

$ ./helm.py --help                         # top level help (launch and move)
$ ./helm.py launch --help                  # help for launch options
$ ./helm.py launch --missiles              # set missiles=True and torpedos=False
$ ./helm.py steer --course 180 --speed 5   # set movement parameters

参见

PEP 389 [https://www.python.org/dev/peps/pep-0389] - 新的命令行解析模块

PEP 由 Steven Bethard 撰写

升级 optparse 代码 for details on the differences from optparse.

PEP 391: 基于字典的日志配置

The logging module provided two kinds of configuration, one style with function calls for each option or another style driven by an external file saved in a ConfigParser format. Those options did not provide the flexibility to create configurations from JSON or YAML files, nor did they support incremental configuration, which is needed for specifying logger options from a command line.

To support a more flexible style, the module now offers logging.config.dictConfig() for specifying logging configuration with plain Python dictionaries. The configuration options include formatters, handlers, filters, and loggers. Here’s a working example of a configuration dictionary:

{"version": 1,
 "formatters": {"brief": {"format": "%(levelname)-8s: %(name)-15s: %(message)s"},
                "full": {"format": "%(asctime)s %(name)-15s %(levelname)-8s %(message)s"}
                },
 "handlers": {"console": {
                   "class": "logging.StreamHandler",
                   "formatter": "brief",
                   "level": "INFO",
                   "stream": "ext://sys.stdout"},
              "console_priority": {
                   "class": "logging.StreamHandler",
                   "formatter": "full",
                   "level": "ERROR",
                   "stream": "ext://sys.stderr"}
              },
 "root": {"level": "DEBUG", "handlers": ["console", "console_priority"]}}

If that dictionary is stored in a file called conf.json, it can be loaded and called with code like this:

>>> import json, logging.config
>>> with open('conf.json') as f:
...     conf = json.load(f)
...
>>> logging.config.dictConfig(conf)
>>> logging.info("Transaction completed normally")
INFO    : root           : Transaction completed normally
>>> logging.critical("Abnormal termination")
2011-02-17 11:14:36,694 root            CRITICAL Abnormal termination

参见

PEP 391 [https://www.python.org/dev/peps/pep-0391] - 基于字典的日志配置

PEP 由 Vinay Sajip 撰写

PEP 3148: concurrent.futures 模块

Code for creating and managing concurrency is being collected in a new top-level namespace, concurrent. Its first member is a futures package which provides a uniform high-level interface for managing threads and processes.

The design for concurrent.futures was inspired by the java.util.concurrent package. In that model, a running call and its result are represented by a Future object that abstracts features common to threads, processes, and remote procedure calls. That object supports status checks (running or done), timeouts, cancellations, adding callbacks, and access to results or exceptions.

The primary offering of the new module is a pair of executor classes for launching and managing calls. The goal of the executors is to make it easier to use existing tools for making parallel calls. They save the effort needed to setup a pool of resources, launch the calls, create a results queue, add time-out handling, and limit the total number of threads, processes, or remote procedure calls.

Ideally, each application should share a single executor across multiple components so that process and thread limits can be centrally managed. This solves the design challenge that arises when each component has its own competing strategy for resource management.

Both classes share a common interface with three methods: submit() for scheduling a callable and returning a Future object; map() for scheduling many asynchronous calls at a time, and shutdown() for freeing resources. The class is a context manager and can be used in a with statement to assure that resources are automatically released when currently pending futures are done executing.

A simple of example of ThreadPoolExecutor is a launch of four parallel threads for copying files:

import concurrent.futures, shutil
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as e:
    e.submit(shutil.copy, 'src1.txt', 'dest1.txt')
    e.submit(shutil.copy, 'src2.txt', 'dest2.txt')
    e.submit(shutil.copy, 'src3.txt', 'dest3.txt')
    e.submit(shutil.copy, 'src3.txt', 'dest4.txt')

参见

PEP 3148 [https://www.python.org/dev/peps/pep-3148] – futures - 异步执行指令

PEP 由 Brian Quinlan 撰写

Code for Threaded Parallel URL reads, an example using threads to fetch multiple web pages in parallel.

Code for computing prime numbers in parallel, an example demonstrating ProcessPoolExecutor.

PEP 3147: PYC 仓库目录

Python’s scheme for caching bytecode in .pyc files did not work well in environments with multiple Python interpreters. If one interpreter encountered a cached file created by another interpreter, it would recompile the source and overwrite the cached file, thus losing the benefits of caching.

The issue of “pyc fights” has become more pronounced as it has become commonplace for Linux distributions to ship with multiple versions of Python. These conflicts also arise with CPython alternatives such as Unladen Swallow.

To solve this problem, Python’s import machinery has been extended to use distinct filenames for each interpreter. Instead of Python 3.2 and Python 3.3 and Unladen Swallow each competing for a file called “mymodule.pyc”, they will now look for “mymodule.cpython-32.pyc”, “mymodule.cpython-33.pyc”, and “mymodule.unladen10.pyc”. And to prevent all of these new files from cluttering source directories, the pyc files are now collected in a “__pycache__” directory stored under the package directory.

Aside from the filenames and target directories, the new scheme has a few aspects that are visible to the programmer:

Imported modules now have a __cached__ attribute which stores the name of the actual file that was imported:

>>> import collections
>>> collections.__cached__
'c:/py32/lib/__pycache__/collections.cpython-32.pyc'

The tag that is unique to each interpreter is accessible from the imp module:

>>> import imp
>>> imp.get_tag()
'cpython-32'

Scripts that try to deduce source filename from the imported file now need to be smarter. It is no longer sufficient to simply strip the “c” from a “.pyc” filename. Instead, use the new functions in the imp module:

>>> imp.source_from_cache('c:/py32/lib/__pycache__/collections.cpython-32.pyc')
'c:/py32/lib/collections.py'
>>> imp.cache_from_source('c:/py32/lib/collections.py')
'c:/py32/lib/__pycache__/collections.cpython-32.pyc'

The py_compile and compileall modules have been updated to reflect the new naming convention and target directory. The command-line invocation of compileall has new options: -i for specifying a list of files and directories to compile and -b which causes bytecode files to be written to their legacy location rather than __pycache__.

The importlib.abc module has been updated with new abstract base classes for loading bytecode files. The obsolete ABCs, PyLoader and PyPycLoader, have been deprecated (instructions on how to stay Python 3.1 compatible are included with the documentation).

参见

PEP 3147 [https://www.python.org/dev/peps/pep-3147] - PYC 仓库目录

PEP 由 Barry Warsaw 撰写

PEP 3149: ABI Version Tagged .so Files

The PYC repository directory allows multiple bytecode cache files to be co-located. This PEP implements a similar mechanism for shared object files by giving them a common directory and distinct names for each version.

The common directory is “pyshared” and the file names are made distinct by identifying the Python implementation (such as CPython, PyPy, Jython, etc.), the major and minor version numbers, and optional build flags (such as “d” for debug, “m” for pymalloc, “u” for wide-unicode). For an arbitrary package “foo”, you may see these files when the distribution package is installed:

/usr/share/pyshared/foo.cpython-32m.so
/usr/share/pyshared/foo.cpython-33md.so

In Python itself, the tags are accessible from functions in the sysconfig module:

>>> import sysconfig
>>> sysconfig.get_config_var('SOABI')       # find the version tag
'cpython-32mu'
>>> sysconfig.get_config_var('EXT_SUFFIX')  # find the full filename extension
'.cpython-32mu.so'

参见

PEP 3149 [https://www.python.org/dev/peps/pep-3149] - ABI Version Tagged .so Files

PEP 由 Barry Warsaw 撰写

PEP 3333: Python Web服务器网关接口v1.0.1

This informational PEP clarifies how bytes/text issues are to be handled by the WSGI protocol. The challenge is that string handling in Python 3 is most conveniently handled with the str type even though the HTTP protocol is itself bytes oriented.

The PEP differentiates so-called native strings that are used for request/response headers and metadata versus byte strings which are used for the bodies of requests and responses.

The native strings are always of type str but are restricted to code points between U+0000 through U+00FF which are translatable to bytes using Latin-1 encoding. These strings are used for the keys and values in the environment dictionary and for response headers and statuses in the start_response() function. They must follow RFC 2616 [https://tools.ietf.org/html/rfc2616.html] with respect to encoding. That is, they must either be ISO-8859-1 characters or use RFC 2047 [https://tools.ietf.org/html/rfc2047.html] MIME encoding.

For developers porting WSGI applications from Python 2, here are the salient points:

If the app already used strings for headers in Python 2, no change is needed.

If instead, the app encoded output headers or decoded input headers, then the headers will need to be re-encoded to Latin-1. For example, an output header encoded in utf-8 was using h.encode('utf-8') now needs to convert from bytes to native strings using h.encode('utf-8').decode('latin-1').

Values yielded by an application or sent using the write() method must be byte strings. The start_response() function and environ must use native strings. The two cannot be mixed.

For server implementers writing CGI-to-WSGI pathways or other CGI-style protocols, the users must to be able access the environment using native strings even though the underlying platform may have a different convention. To bridge this gap, the wsgiref module has a new function, wsgiref.handlers.read_environ() for transcoding CGI variables from os.environ into native strings and returning a new dictionary.

参见

PEP 3333 [https://www.python.org/dev/peps/pep-3333] - Python Web服务器网关接口v1.0.1

PEP 由 Phillip Eby 撰写

其他语言特性修改

对Python 语言核心进行的小改动：

String formatting for format() and str.format() gained new capabilities for the format character #. Previously, for integers in binary, octal, or hexadecimal, it caused the output to be prefixed with ‘0b’, ‘0o’, or ‘0x’ respectively. Now it can also handle floats, complex, and Decimal, causing the output to always have a decimal point even when no digits follow it.

>>> format(20, '#o')
'0o24'
>>> format(12.34, '#5.0f')
'  12.'

(Suggested by Mark Dickinson and implemented by Eric Smith in bpo-7094 [https://bugs.python.org/issue7094].)

There is also a new str.format_map() method that extends the capabilities of the existing str.format() method by accepting arbitrary mapping objects. This new method makes it possible to use string formatting with any of Python’s many dictionary-like objects such as defaultdict, Shelf, ConfigParser, or dbm. It is also useful with custom dict subclasses that normalize keys before look-up or that supply a __missing__() method for unknown keys:

>>> import shelve
>>> d = shelve.open('tmp.shl')
>>> 'The {project_name} status is {status} as of {date}'.format_map(d)
'The testing project status is green as of February 15, 2011'

>>> class LowerCasedDict(dict):
...     def __getitem__(self, key):
...         return dict.__getitem__(self, key.lower())
>>> lcd = LowerCasedDict(part='widgets', quantity=10)
>>> 'There are {QUANTITY} {Part} in stock'.format_map(lcd)
'There are 10 widgets in stock'

>>> class PlaceholderDict(dict):
...     def __missing__(self, key):
...         return '<{}>'.format(key)
>>> 'Hello {name}, welcome to {location}'.format_map(PlaceholderDict())
'Hello <name>, welcome to <location>'

(Suggested by Raymond Hettinger and implemented by Eric Smith in bpo-6081 [https://bugs.python.org/issue6081].)

The interpreter can now be started with a quiet option, -q, to prevent the copyright and version information from being displayed in the interactive mode. The option can be introspected using the sys.flags attribute:

$ python -q
>>> sys.flags
sys.flags(debug=0, division_warning=0, inspect=0, interactive=0,
optimize=0, dont_write_bytecode=0, no_user_site=0, no_site=0,
ignore_environment=0, verbose=0, bytes_warning=0, quiet=1)

（由 Marcin Wojdyr 在 bpo-1772833 [https://bugs.python.org/issue1772833] 中贡献。）

The hasattr() function works by calling getattr() and detecting whether an exception is raised. This technique allows it to detect methods created dynamically by __getattr__() or __getattribute__() which would otherwise be absent from the class dictionary. Formerly, hasattr would catch any exception, possibly masking genuine errors. Now, hasattr has been tightened to only catch AttributeError and let other exceptions pass through:

>>> class A:
...     @property
...     def f(self):
...         return 1 // 0
...
>>> a = A()
>>> hasattr(a, 'f')
Traceback (most recent call last):
  ...
ZeroDivisionError: integer division or modulo by zero

(Discovered by Yury Selivanov and fixed by Benjamin Peterson; bpo-9666 [https://bugs.python.org/issue9666].)

The str() of a float or complex number is now the same as its repr(). Previously, the str() form was shorter but that just caused confusion and is no longer needed now that the shortest possible repr() is displayed by default:

>>> import math
>>> repr(math.pi)
'3.141592653589793'
>>> str(math.pi)
'3.141592653589793'

(Proposed and implemented by Mark Dickinson; bpo-9337 [https://bugs.python.org/issue9337].)

memoryview objects now have a release() method and they also now support the context management protocol. This allows timely release of any resources that were acquired when requesting a buffer from the original object.

>>> with memoryview(b'abcdefgh') as v:
...     print(v.tolist())
[97, 98, 99, 100, 101, 102, 103, 104]

(Added by Antoine Pitrou; bpo-9757 [https://bugs.python.org/issue9757].)

Previously it was illegal to delete a name from the local namespace if it occurs as a free variable in a nested block:

def outer(x):
    def inner():
        return x
    inner()
    del x

This is now allowed. Remember that the target of an except clause is cleared, so this code which used to work with Python 2.6, raised a SyntaxError with Python 3.1 and now works again:

def f():
    def print_error():
        print(e)
    try:
        something
    except Exception as e:
        print_error()
        # implicit "del e" here

(See bpo-4617 [https://bugs.python.org/issue4617].)

The internal structsequence tool now creates subclasses of tuple. This means that C structures like those returned by os.stat(), time.gmtime(), and sys.version_info now work like a named tuple and now work with functions and methods that expect a tuple as an argument. This is a big step forward in making the C structures as flexible as their pure Python counterparts:

>>> import sys
>>> isinstance(sys.version_info, tuple)
True
>>> 'Version %d.%d.%d %s(%d)' % sys.version_info
'Version 3.2.0 final(0)'

(Suggested by Arfrever Frehtes Taifersar Arahesis and implemented by Benjamin Peterson in bpo-8413 [https://bugs.python.org/issue8413].)

Warnings are now easier to control using the PYTHONWARNINGS environment variable as an alternative to using -W at the command line:

$ export PYTHONWARNINGS='ignore::RuntimeWarning::,once::UnicodeWarning::'

(Suggested by Barry Warsaw and implemented by Philip Jenvey in bpo-7301 [https://bugs.python.org/issue7301].)

A new warning category, ResourceWarning, has been added. It is emitted when potential issues with resource consumption or cleanup are detected. It is silenced by default in normal release builds but can be enabled through the means provided by the warnings module, or on the command line.

A ResourceWarning is issued at interpreter shutdown if the gc.garbage list isn’t empty, and if gc.DEBUG_UNCOLLECTABLE is set, all uncollectable objects are printed. This is meant to make the programmer aware that their code contains object finalization issues.

A ResourceWarning is also issued when a file object is destroyed without having been explicitly closed. While the deallocator for such object ensures it closes the underlying operating system resource (usually, a file descriptor), the delay in deallocating the object could produce various issues, especially under Windows. Here is an example of enabling the warning from the command line:

$ python -q -Wdefault
>>> f = open("foo", "wb")
>>> del f
__main__:1: ResourceWarning: unclosed file <_io.BufferedWriter name='foo'>

(Added by Antoine Pitrou and Georg Brandl in bpo-10093 [https://bugs.python.org/issue10093] and bpo-477863 [https://bugs.python.org/issue477863].)

range objects now support index and count methods. This is part of an effort to make more objects fully implement the collections.Sequence abstract base class. As a result, the language will have a more uniform API. In addition, range objects now support slicing and negative indices, even with values larger than sys.maxsize. This makes range more interoperable with lists:

>>> range(0, 100, 2).count(10)
1
>>> range(0, 100, 2).index(10)
5
>>> range(0, 100, 2)[5]
10
>>> range(0, 100, 2)[0:5]
range(0, 10, 2)

（由 Daniel Stutzbach 在 bpo-9213 [https://bugs.python.org/issue9213] 中贡献，由 Alexander Belopolsky 在 bpo-2690 [https://bugs.python.org/issue2690] 中贡献，由 Nick Coghlan 在 bpo-10889 [https://bugs.python.org/issue10889] 中贡献。）

The callable() builtin function from Py2.x was resurrected. It provides a concise, readable alternative to using an abstract base class in an expression like isinstance(x, collections.Callable):

>>> callable(max)
True
>>> callable(20)
False

（见 bpo-10518 [https://bugs.python.org/issue10518] ）

Python’s import mechanism can now load modules installed in directories with non-ASCII characters in the path name. This solved an aggravating problem with home directories for users with non-ASCII characters in their usernames.

(Required extensive work by Victor Stinner in bpo-9425 [https://bugs.python.org/issue9425].)

新增，改进和弃用的模块

Python’s standard library has undergone significant maintenance efforts and quality improvements.

The biggest news for Python 3.2 is that the email package, mailbox module, and nntplib modules now work correctly with the bytes/text model in Python 3. For the first time, there is correct handling of messages with mixed encodings.

Throughout the standard library, there has been more careful attention to encodings and text versus bytes issues. In particular, interactions with the operating system are now better able to exchange non-ASCII data using the Windows MBCS encoding, locale-aware encodings, or UTF-8.

Another significant win is the addition of substantially better support for SSL connections and security certificates.

In addition, more classes now implement a context manager to support convenient and reliable resource clean-up using a with statement.

email

The usability of the email package in Python 3 has been mostly fixed by the extensive efforts of R. David Murray. The problem was that emails are typically read and stored in the form of bytes rather than str text, and they may contain multiple encodings within a single email. So, the email package had to be extended to parse and generate email messages in bytes format.

New functions message_from_bytes() and message_from_binary_file(), and new classes BytesFeedParser and BytesParser allow binary message data to be parsed into model objects.

Given bytes input to the model, get_payload() will by default decode a message body that has a Content-Transfer-Encoding of 8bit using the charset specified in the MIME headers and return the resulting string.

Given bytes input to the model, Generator will convert message bodies that have a Content-Transfer-Encoding of 8bit to instead have a 7bit Content-Transfer-Encoding.

Headers with unencoded non-ASCII bytes are deemed to be RFC 2047 [https://tools.ietf.org/html/rfc2047.html]-encoded using the unknown-8bit character set.

A new class BytesGenerator produces bytes as output, preserving any unchanged non-ASCII data that was present in the input used to build the model, including message bodies with a Content-Transfer-Encoding of 8bit.

The smtplib SMTP class now accepts a byte string for the msg argument to the sendmail() method, and a new method, send_message() accepts a Message object and can optionally obtain the from_addr and to_addrs addresses directly from the object.

(Proposed and implemented by R. David Murray, bpo-4661 [https://bugs.python.org/issue4661] and bpo-10321 [https://bugs.python.org/issue10321].)

elementtree

The xml.etree.ElementTree package and its xml.etree.cElementTree counterpart have been updated to version 1.3.

Several new and useful functions and methods have been added:

xml.etree.ElementTree.fromstringlist() which builds an XML document from a sequence of fragments

xml.etree.ElementTree.register_namespace() for registering a global namespace prefix

xml.etree.ElementTree.tostringlist() for string representation including all sublists

xml.etree.ElementTree.Element.extend() for appending a sequence of zero or more elements

xml.etree.ElementTree.Element.iterfind() searches an element and subelements

xml.etree.ElementTree.Element.itertext() creates a text iterator over an element and its subelements

xml.etree.ElementTree.TreeBuilder.end() closes the current element

xml.etree.ElementTree.TreeBuilder.doctype() handles a doctype declaration

两个方法被弃用：

xml.etree.ElementTree.getchildren() 被 list(elem) 替代。

xml.etree.ElementTree.getiterator() 被 Element.iter 替代。

For details of the update, see Introducing ElementTree [http://effbot.org/zone/elementtree-13-intro.htm] on Fredrik Lundh’s website.

（由 Florent Xicluna 和 Fredrik Lundh 在 bpo-1772833 [https://bugs.python.org/issue1772833] 中贡献。）

functools

The functools module includes a new decorator for caching function calls. functools.lru_cache() can save repeated queries to an external resource whenever the results are expected to be the same.

For example, adding a caching decorator to a database query function can save database accesses for popular searches:

>>> import functools
>>> @functools.lru_cache(maxsize=300)
... def get_phone_number(name):
...     c = conn.cursor()
...     c.execute('SELECT phonenumber FROM phonelist WHERE name=?', (name,))
...     return c.fetchone()[0]

>>> for name in user_requests:
...     get_phone_number(name)        # cached lookup

To help with choosing an effective cache size, the wrapped function is instrumented for tracking cache statistics:

>>> get_phone_number.cache_info()
CacheInfo(hits=4805, misses=980, maxsize=300, currsize=300)

If the phonelist table gets updated, the outdated contents of the cache can be cleared with:

>>> get_phone_number.cache_clear()

(Contributed by Raymond Hettinger and incorporating design ideas from Jim Baker, Miki Tebeka, and Nick Coghlan; see recipe 498245 [https://code.activestate.com/recipes/498245], recipe 577479 [https://code.activestate.com/recipes/577479], bpo-10586 [https://bugs.python.org/issue10586], and bpo-10593 [https://bugs.python.org/issue10593].)

The functools.wraps() decorator now adds a __wrapped__ attribute pointing to the original callable function. This allows wrapped functions to be introspected. It also copies __annotations__ if defined. And now it also gracefully skips over missing attributes such as __doc__ which might not be defined for the wrapped callable.

In the above example, the cache can be removed by recovering the original function:

>>> get_phone_number = get_phone_number.__wrapped__    # uncached function

(By Nick Coghlan and Terrence Cole; bpo-9567 [https://bugs.python.org/issue9567], bpo-3445 [https://bugs.python.org/issue3445], and bpo-8814 [https://bugs.python.org/issue8814].)

To help write classes with rich comparison methods, a new decorator functools.total_ordering() will use existing equality and inequality methods to fill in the remaining methods.

For example, supplying __eq__ and __lt__ will enable total_ordering() to fill-in __le__, __gt__ and __ge__:

@total_ordering
class Student:
    def __eq__(self, other):
        return ((self.lastname.lower(), self.firstname.lower()) ==
                (other.lastname.lower(), other.firstname.lower()))

    def __lt__(self, other):
        return ((self.lastname.lower(), self.firstname.lower()) <
                (other.lastname.lower(), other.firstname.lower()))

With the total_ordering decorator, the remaining comparison methods are filled in automatically.

（由 Raymond Hettinger 贡献。）

To aid in porting programs from Python 2, the functools.cmp_to_key() function converts an old-style comparison function to modern key function:

>>> # locale-aware sort order
>>> sorted(iterable, key=cmp_to_key(locale.strcoll))

For sorting examples and a brief sorting tutorial, see the Sorting HowTo [https://wiki.python.org/moin/HowTo/Sorting/] tutorial.

（由 Raymond Hettinger 贡献。）

itertools

The itertools module has a new accumulate() function modeled on APL’s scan operator and Numpy’s accumulate function:

>>> from itertools import accumulate
>>> list(accumulate([8, 2, 50]))
[8, 10, 60]

>>> prob_dist = [0.1, 0.4, 0.2, 0.3]
>>> list(accumulate(prob_dist))      # cumulative probability distribution
[0.1, 0.5, 0.7, 1.0]

For an example using accumulate(), see the examples for the random module.

(Contributed by Raymond Hettinger and incorporating design suggestions from Mark Dickinson.)

collections

The collections.Counter class now has two forms of in-place subtraction, the existing -= operator for saturating subtraction [https://en.wikipedia.org/wiki/Saturation_arithmetic] and the new subtract() method for regular subtraction. The former is suitable for multisets [https://en.wikipedia.org/wiki/Multiset] which only have positive counts, and the latter is more suitable for use cases that allow negative counts:

>>> from collections import Counter
>>> tally = Counter(dogs=5, cats=3)
>>> tally -= Counter(dogs=2, cats=8)    # saturating subtraction
>>> tally
Counter({'dogs': 3})

>>> tally = Counter(dogs=5, cats=3)
>>> tally.subtract(dogs=2, cats=8)      # regular subtraction
>>> tally
Counter({'dogs': 3, 'cats': -5})

（由 Raymond Hettinger 贡献。）

The collections.OrderedDict class has a new method move_to_end() which takes an existing key and moves it to either the first or last position in the ordered sequence.

The default is to move an item to the last position. This is equivalent of renewing an entry with od[k] = od.pop(k).

A fast move-to-end operation is useful for resequencing entries. For example, an ordered dictionary can be used to track order of access by aging entries from the oldest to the most recently accessed.

>>> from collections import OrderedDict
>>> d = OrderedDict.fromkeys(['a', 'b', 'X', 'd', 'e'])
>>> list(d)
['a', 'b', 'X', 'd', 'e']
>>> d.move_to_end('X')
>>> list(d)
['a', 'b', 'd', 'e', 'X']

（由 Raymond Hettinger 贡献。）

The collections.deque class grew two new methods count() and reverse() that make them more substitutable for list objects:

>>> from collections import deque
>>> d = deque('simsalabim')
>>> d.count('s')
2
>>> d.reverse()
>>> d
deque(['m', 'i', 'b', 'a', 'l', 'a', 's', 'm', 'i', 's'])

（由 Raymond Hettinger 贡献。）

threading

The threading module has a new Barrier synchronization class for making multiple threads wait until all of them have reached a common barrier point. Barriers are useful for making sure that a task with multiple preconditions does not run until all of the predecessor tasks are complete.

Barriers can work with an arbitrary number of threads. This is a generalization of a Rendezvous [https://en.wikipedia.org/wiki/Synchronous_rendezvous] which is defined for only two threads.

Implemented as a two-phase cyclic barrier, Barrier objects are suitable for use in loops. The separate filling and draining phases assure that all threads get released (drained) before any one of them can loop back and re-enter the barrier. The barrier fully resets after each cycle.

Example of using barriers:

from threading import Barrier, Thread

def get_votes(site):
    ballots = conduct_election(site)
    all_polls_closed.wait()        # do not count until all polls are closed
    totals = summarize(ballots)
    publish(site, totals)

all_polls_closed = Barrier(len(sites))
for site in sites:
    Thread(target=get_votes, args=(site,)).start()

In this example, the barrier enforces a rule that votes cannot be counted at any polling site until all polls are closed. Notice how a solution with a barrier is similar to one with threading.Thread.join(), but the threads stay alive and continue to do work (summarizing ballots) after the barrier point is crossed.

If any of the predecessor tasks can hang or be delayed, a barrier can be created with an optional timeout parameter. Then if the timeout period elapses before all the predecessor tasks reach the barrier point, all waiting threads are released and a BrokenBarrierError exception is raised:

def get_votes(site):
    ballots = conduct_election(site)
    try:
        all_polls_closed.wait(timeout=midnight - time.now())
    except BrokenBarrierError:
        lockbox = seal_ballots(ballots)
        queue.put(lockbox)
    else:
        totals = summarize(ballots)
        publish(site, totals)

In this example, the barrier enforces a more robust rule. If some election sites do not finish before midnight, the barrier times-out and the ballots are sealed and deposited in a queue for later handling.

See Barrier Synchronization Patterns [https://parlab.eecs.berkeley.edu/wiki/_media/patterns/paraplop_g1_3.pdf] for more examples of how barriers can be used in parallel computing. Also, there is a simple but thorough explanation of barriers in The Little Book of Semaphores [http://greenteapress.com/semaphores/downey08semaphores.pdf], section 3.6.

(Contributed by Kristján Valur Jónsson with an API review by Jeffrey Yasskin in bpo-8777 [https://bugs.python.org/issue8777].)

datetime 和 time

The datetime module has a new type timezone that implements the tzinfo interface by returning a fixed UTC offset and timezone name. This makes it easier to create timezone-aware datetime objects:

>>> from datetime import datetime, timezone

>>> datetime.now(timezone.utc)
datetime.datetime(2010, 12, 8, 21, 4, 2, 923754, tzinfo=datetime.timezone.utc)

>>> datetime.strptime("01/01/2000 12:00 +0000", "%m/%d/%Y %H:%M %z")
datetime.datetime(2000, 1, 1, 12, 0, tzinfo=datetime.timezone.utc)

Also, timedelta objects can now be multiplied by float and divided by float and int objects. And timedelta objects can now divide one another.

The datetime.date.strftime() method is no longer restricted to years after 1900. The new supported year range is from 1000 to 9999 inclusive.

Whenever a two-digit year is used in a time tuple, the interpretation has been governed by time.accept2dyear. The default is True which means that for a two-digit year, the century is guessed according to the POSIX rules governing the %y strptime format.

Starting with Py3.2, use of the century guessing heuristic will emit a DeprecationWarning. Instead, it is recommended that time.accept2dyear be set to False so that large date ranges can be used without guesswork:

>>> import time, warnings
>>> warnings.resetwarnings()      # remove the default warning filters

>>> time.accept2dyear = True      # guess whether 11 means 11 or 2011
>>> time.asctime((11, 1, 1, 12, 34, 56, 4, 1, 0))
Warning (from warnings module):
  ...
DeprecationWarning: Century info guessed for a 2-digit year.
'Fri Jan  1 12:34:56 2011'

>>> time.accept2dyear = False     # use the full range of allowable dates
>>> time.asctime((11, 1, 1, 12, 34, 56, 4, 1, 0))
'Fri Jan  1 12:34:56 11'

Several functions now have significantly expanded date ranges. When time.accept2dyear is false, the time.asctime() function will accept any year that fits in a C int, while the time.mktime() and time.strftime() functions will accept the full range supported by the corresponding operating system functions.

(Contributed by Alexander Belopolsky and Victor Stinner in bpo-1289118 [https://bugs.python.org/issue1289118], bpo-5094 [https://bugs.python.org/issue5094], bpo-6641 [https://bugs.python.org/issue6641], bpo-2706 [https://bugs.python.org/issue2706], bpo-1777412 [https://bugs.python.org/issue1777412], bpo-8013 [https://bugs.python.org/issue8013], and bpo-10827 [https://bugs.python.org/issue10827].)

math

The math module has been updated with six new functions inspired by the C99 standard.

The isfinite() function provides a reliable and fast way to detect special values. It returns True for regular numbers and False for Nan or Infinity:

>>> from math import isfinite
>>> [isfinite(x) for x in (123, 4.56, float('Nan'), float('Inf'))]
[True, True, False, False]

The expm1() function computes e**x-1 for small values of x without incurring the loss of precision that usually accompanies the subtraction of nearly equal quantities:

>>> from math import expm1
>>> expm1(0.013671875)   # more accurate way to compute e**x-1 for a small x
0.013765762467652909

The erf() function computes a probability integral or Gaussian error function [https://en.wikipedia.org/wiki/Error_function]. The complementary error function, erfc(), is 1 - erf(x):

>>> from math import erf, erfc, sqrt
>>> erf(1.0/sqrt(2.0))   # portion of normal distribution within 1 standard deviation
0.682689492137086
>>> erfc(1.0/sqrt(2.0))  # portion of normal distribution outside 1 standard deviation
0.31731050786291404
>>> erf(1.0/sqrt(2.0)) + erfc(1.0/sqrt(2.0))
1.0

The gamma() function is a continuous extension of the factorial function. See https://en.wikipedia.org/wiki/Gamma_function for details. Because the function is related to factorials, it grows large even for small values of x, so there is also a lgamma() function for computing the natural logarithm of the gamma function:

>>> from math import gamma, lgamma
>>> gamma(7.0)           # six factorial
720.0
>>> lgamma(801.0)        # log(800 factorial)
4551.950730698041

（由 Mark Dickinson 贡献）

abc

The abc module now supports abstractclassmethod() and abstractstaticmethod().

These tools make it possible to define an abstract base class that requires a particular classmethod() or staticmethod() to be implemented:

class Temperature(metaclass=abc.ABCMeta):
    @abc.abstractclassmethod
    def from_fahrenheit(cls, t):
        ...
    @abc.abstractclassmethod
    def from_celsius(cls, t):
        ...

(Patch submitted by Daniel Urban; bpo-5867 [https://bugs.python.org/issue5867].)

io

The io.BytesIO has a new method, getbuffer(), which provides functionality similar to memoryview(). It creates an editable view of the data without making a copy. The buffer’s random access and support for slice notation are well-suited to in-place editing:

>>> REC_LEN, LOC_START, LOC_LEN = 34, 7, 11

>>> def change_location(buffer, record_number, location):
...     start = record_number * REC_LEN + LOC_START
...     buffer[start: start+LOC_LEN] = location

>>> import io

>>> byte_stream = io.BytesIO(
...     b'G3805  storeroom  Main chassis    '
...     b'X7899  shipping   Reserve cog     '
...     b'L6988  receiving  Primary sprocket'
... )
>>> buffer = byte_stream.getbuffer()
>>> change_location(buffer, 1, b'warehouse  ')
>>> change_location(buffer, 0, b'showroom   ')
>>> print(byte_stream.getvalue())
b'G3805  showroom   Main chassis    '
b'X7899  warehouse  Reserve cog     '
b'L6988  receiving  Primary sprocket'

（由 Antoine Pitrou 在 bpo-5506 [https://bugs.python.org/issue5506] 中贡献。）

reprlib

When writing a __repr__() method for a custom container, it is easy to forget to handle the case where a member refers back to the container itself. Python’s builtin objects such as list and set handle self-reference by displaying “…” in the recursive part of the representation string.

To help write such __repr__() methods, the reprlib module has a new decorator, recursive_repr(), for detecting recursive calls to __repr__() and substituting a placeholder string instead:

>>> class MyList(list):
...     @recursive_repr()
...     def __repr__(self):
...         return '<' + '|'.join(map(repr, self)) + '>'
...
>>> m = MyList('abc')
>>> m.append(m)
>>> m.append('x')
>>> print(m)
<'a'|'b'|'c'|...|'x'>

（由 Raymond Hettinger 在 bpo-9826 [https://bugs.python.org/issue9826] 和 bpo-9826 [https://bugs.python.org/issue9826] 中贡献。）

logging

In addition to dictionary-based configuration described above, the logging package has many other improvements.

The logging documentation has been augmented by a basic tutorial, an advanced tutorial, and a cookbook of logging recipes. These documents are the fastest way to learn about logging.

The logging.basicConfig() set-up function gained a style argument to support three different types of string formatting. It defaults to “%” for traditional %-formatting, can be set to “{” for the new str.format() style, or can be set to “$” for the shell-style formatting provided by string.Template. The following three configurations are equivalent:

>>> from logging import basicConfig
>>> basicConfig(style='%', format="%(name)s -> %(levelname)s: %(message)s")
>>> basicConfig(style='{', format="{name} -> {levelname} {message}")
>>> basicConfig(style='$', format="$name -> $levelname: $message")

If no configuration is set-up before a logging event occurs, there is now a default configuration using a StreamHandler directed to sys.stderr for events of WARNING level or higher. Formerly, an event occurring before a configuration was set-up would either raise an exception or silently drop the event depending on the value of logging.raiseExceptions. The new default handler is stored in logging.lastResort.

The use of filters has been simplified. Instead of creating a Filter object, the predicate can be any Python callable that returns True or False.

There were a number of other improvements that add flexibility and simplify configuration. See the module documentation for a full listing of changes in Python 3.2.

csv

The csv module now supports a new dialect, unix_dialect, which applies quoting for all fields and a traditional Unix style with '\n' as the line terminator. The registered dialect name is unix.

The csv.DictWriter has a new method, writeheader() for writing-out an initial row to document the field names:

>>> import csv, sys
>>> w = csv.DictWriter(sys.stdout, ['name', 'dept'], dialect='unix')
>>> w.writeheader()
"name","dept"
>>> w.writerows([
...     {'name': 'tom', 'dept': 'accounting'},
...     {'name': 'susan', 'dept': 'Salesl'}])
"tom","accounting"
"susan","sales"

(New dialect suggested by Jay Talbot in bpo-5975 [https://bugs.python.org/issue5975], and the new method suggested by Ed Abraham in bpo-1537721 [https://bugs.python.org/issue1537721].)

contextlib

There is a new and slightly mind-blowing tool ContextDecorator that is helpful for creating a context manager that does double duty as a function decorator.

As a convenience, this new functionality is used by contextmanager() so that no extra effort is needed to support both roles.

The basic idea is that both context managers and function decorators can be used for pre-action and post-action wrappers. Context managers wrap a group of statements using a with statement, and function decorators wrap a group of statements enclosed in a function. So, occasionally there is a need to write a pre-action or post-action wrapper that can be used in either role.

For example, it is sometimes useful to wrap functions or groups of statements with a logger that can track the time of entry and time of exit. Rather than writing both a function decorator and a context manager for the task, the contextmanager() provides both capabilities in a single definition:

from contextlib import contextmanager
import logging

logging.basicConfig(level=logging.INFO)

@contextmanager
def track_entry_and_exit(name):
    logging.info('Entering: %s', name)
    yield
    logging.info('Exiting: %s', name)

Formerly, this would have only been usable as a context manager:

with track_entry_and_exit('widget loader'):
    print('Some time consuming activity goes here')
    load_widget()

Now, it can be used as a decorator as well:

@track_entry_and_exit('widget loader')
def activity():
    print('Some time consuming activity goes here')
    load_widget()

Trying to fulfill two roles at once places some limitations on the technique. Context managers normally have the flexibility to return an argument usable by a with statement, but there is no parallel for function decorators.

In the above example, there is not a clean way for the track_entry_and_exit context manager to return a logging instance for use in the body of enclosed statements.

（由 Michael Foord 在 bpo-9110 [https://bugs.python.org/issue9110] 中贡献。）

decimal and fractions

Mark Dickinson crafted an elegant and efficient scheme for assuring that different numeric datatypes will have the same hash value whenever their actual values are equal (bpo-8188 [https://bugs.python.org/issue8188]):

assert hash(Fraction(3, 2)) == hash(1.5) == \
       hash(Decimal("1.5")) == hash(complex(1.5, 0))

Some of the hashing details are exposed through a new attribute, sys.hash_info, which describes the bit width of the hash value, the prime modulus, the hash values for infinity and nan, and the multiplier used for the imaginary part of a number:

>>> sys.hash_info
sys.hash_info(width=64, modulus=2305843009213693951, inf=314159, nan=0, imag=1000003)

An early decision to limit the inter-operability of various numeric types has been relaxed. It is still unsupported (and ill-advised) to have implicit mixing in arithmetic expressions such as Decimal('1.1') + float('1.1') because the latter loses information in the process of constructing the binary float. However, since existing floating point value can be converted losslessly to either a decimal or rational representation, it makes sense to add them to the constructor and to support mixed-type comparisons.

The decimal.Decimal constructor now accepts float objects directly so there in no longer a need to use the from_float() method (bpo-8257 [https://bugs.python.org/issue8257]).

Mixed type comparisons are now fully supported so that Decimal objects can be directly compared with float and fractions.Fraction (bpo-2531 [https://bugs.python.org/issue2531] and bpo-8188 [https://bugs.python.org/issue8188]).

Similar changes were made to fractions.Fraction so that the from_float() and from_decimal() methods are no longer needed (bpo-8294 [https://bugs.python.org/issue8294]):

>>> from decimal import Decimal
>>> from fractions import Fraction
>>> Decimal(1.1)
Decimal('1.100000000000000088817841970012523233890533447265625')
>>> Fraction(1.1)
Fraction(2476979795053773, 2251799813685248)

Another useful change for the decimal module is that the Context.clamp attribute is now public. This is useful in creating contexts that correspond to the decimal interchange formats specified in IEEE 754 (see bpo-8540 [https://bugs.python.org/issue8540]).

（由 Mark Dickinson 和 Raymond Hettinger贡献。）

ftp

The ftplib.FTP class now supports the context management protocol to unconditionally consume socket.error exceptions and to close the FTP connection when done:

>>> from ftplib import FTP
>>> with FTP("ftp1.at.proftpd.org") as ftp:
        ftp.login()
        ftp.dir()

'230 Anonymous login ok, restrictions apply.'
dr-xr-xr-x   9 ftp      ftp           154 May  6 10:43 .
dr-xr-xr-x   9 ftp      ftp           154 May  6 10:43 ..
dr-xr-xr-x   5 ftp      ftp          4096 May  6 10:43 CentOS
dr-xr-xr-x   3 ftp      ftp            18 Jul 10  2008 Fedora

Other file-like objects such as mmap.mmap and fileinput.input() also grew auto-closing context managers:

with fileinput.input(files=('log1.txt', 'log2.txt')) as f:
    for line in f:
        process(line)

（由 Tarek Ziadé 和 Giampaolo Rodolà 在 bpo-4972`贡献，由 Georg Brandl 在 :issue:`8046 [https://bugs.python.org/issue4972`贡献，由 Georg Brandl 在 :issue:`8046] 和 bpo-1286 [https://bugs.python.org/issue1286] 贡献。）

The FTP_TLS class now accepts a context parameter, which is a ssl.SSLContext object allowing bundling SSL configuration options, certificates and private keys into a single (potentially long-lived) structure.

（由 Giampaolo Rodolà 在 bpo-8806 [https://bugs.python.org/issue8806] 中贡献。）

popen

The os.popen() and subprocess.Popen() functions now support with statements for auto-closing of the file descriptors.

（由 Antoine Pitrou 和 Brian Curtin 在 bpo-7461 [https://bugs.python.org/issue7461] 和 bpo-10554 [https://bugs.python.org/issue10554] 中贡献。）

select

The select module now exposes a new, constant attribute, PIPE_BUF, which gives the minimum number of bytes which are guaranteed not to block when select.select() says a pipe is ready for writing.

>>> import select
>>> select.PIPE_BUF
512

(Available on Unix systems. Patch by Sébastien Sablé in bpo-9862 [https://bugs.python.org/issue9862])

gzip 和 zipfile

gzip.GzipFile now implements the io.BufferedIOBase abstract base class (except for truncate()). It also has a peek() method and supports unseekable as well as zero-padded file objects.

The gzip module also gains the compress() and decompress() functions for easier in-memory compression and decompression. Keep in mind that text needs to be encoded as bytes before compressing and decompressing:

>>> import gzip
>>> s = 'Three shall be the number thou shalt count, '
>>> s += 'and the number of the counting shall be three'
>>> b = s.encode()                        # convert to utf-8
>>> len(b)
89
>>> c = gzip.compress(b)
>>> len(c)
77
>>> gzip.decompress(c).decode()[:42]      # decompress and convert to text
'Three shall be the number thou shalt count'

（由 Anand B. Pillai 在 bpo-3488 [https://bugs.python.org/issue3488] 中贡献，由Antoine Pitrou, Nir Aides 和 Brian Curtin 在 bpo-9962 [https://bugs.python.org/issue9962]，bpo-1675951 [https://bugs.python.org/issue1675951] ，bpo-7471 [https://bugs.python.org/issue7471] 和 bpo-2846 [https://bugs.python.org/issue2846] 中贡献。）

Also, the zipfile.ZipExtFile class was reworked internally to represent files stored inside an archive. The new implementation is significantly faster and can be wrapped in an io.BufferedReader object for more speedups. It also solves an issue where interleaved calls to read and readline gave the wrong results.

(Patch submitted by Nir Aides in bpo-7610 [https://bugs.python.org/issue7610].)

tarfile

The TarFile class can now be used as a context manager. In addition, its add() method has a new option, filter, that controls which files are added to the archive and allows the file metadata to be edited.

The new filter option replaces the older, less flexible exclude parameter which is now deprecated. If specified, the optional filter parameter needs to be a keyword argument. The user-supplied filter function accepts a TarInfo object and returns an updated TarInfo object, or if it wants the file to be excluded, the function can return None:

>>> import tarfile, glob

>>> def myfilter(tarinfo):
...     if tarinfo.isfile():             # only save real files
...         tarinfo.uname = 'monty'      # redact the user name
...         return tarinfo

>>> with tarfile.open(name='myarchive.tar.gz', mode='w:gz') as tf:
...     for filename in glob.glob('*.txt'):
...         tf.add(filename, filter=myfilter)
...     tf.list()
-rw-r--r-- monty/501        902 2011-01-26 17:59:11 annotations.txt
-rw-r--r-- monty/501        123 2011-01-26 17:59:11 general_questions.txt
-rw-r--r-- monty/501       3514 2011-01-26 17:59:11 prion.txt
-rw-r--r-- monty/501        124 2011-01-26 17:59:11 py_todo.txt
-rw-r--r-- monty/501       1399 2011-01-26 17:59:11 semaphore_notes.txt

(Proposed by Tarek Ziadé and implemented by Lars Gustäbel in bpo-6856 [https://bugs.python.org/issue6856].)

hashlib

The hashlib module has two new constant attributes listing the hashing algorithms guaranteed to be present in all implementations and those available on the current implementation:

>>> import hashlib

>>> hashlib.algorithms_guaranteed
{'sha1', 'sha224', 'sha384', 'sha256', 'sha512', 'md5'}

>>> hashlib.algorithms_available
{'md2', 'SHA256', 'SHA512', 'dsaWithSHA', 'mdc2', 'SHA224', 'MD4', 'sha256',
'sha512', 'ripemd160', 'SHA1', 'MDC2', 'SHA', 'SHA384', 'MD2',
'ecdsa-with-SHA1','md4', 'md5', 'sha1', 'DSA-SHA', 'sha224',
'dsaEncryption', 'DSA', 'RIPEMD160', 'sha', 'MD5', 'sha384'}

（由 Carl Chenet 在 bpo-7418 [https://bugs.python.org/issue7418] 中建议。）

ast

The ast module has a wonderful a general-purpose tool for safely evaluating expression strings using the Python literal syntax. The ast.literal_eval() function serves as a secure alternative to the builtin eval() function which is easily abused. Python 3.2 adds bytes and set literals to the list of supported types: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None.

>>> from ast import literal_eval

>>> request = "{'req': 3, 'func': 'pow', 'args': (2, 0.5)}"
>>> literal_eval(request)
{'args': (2, 0.5), 'req': 3, 'func': 'pow'}

>>> request = "os.system('do something harmful')"
>>> literal_eval(request)
Traceback (most recent call last):
  ...
ValueError: malformed node or string: <_ast.Call object at 0x101739a10>

（由Benjamin Peterson 和 Georg Brandl 实现。）

os

Different operating systems use various encodings for filenames and environment variables. The os module provides two new functions, fsencode() and fsdecode(), for encoding and decoding filenames:

>>> import os
>>> filename = 'Sehenswürdigkeiten'
>>> os.fsencode(filename)
b'Sehensw\xc3\xbcrdigkeiten'

Some operating systems allow direct access to encoded bytes in the environment. If so, the os.supports_bytes_environ constant will be true.

For direct access to encoded environment variables (if available), use the new os.getenvb() function or use os.environb which is a bytes version of os.environ.

（由 Victor Stinner 贡献。）

shutil

The shutil.copytree() function has two new options:

ignore_dangling_symlinks: when symlinks=False so that the function copies a file pointed to by a symlink, not the symlink itself. This option will silence the error raised if the file doesn’t exist.

copy_function: is a callable that will be used to copy files. shutil.copy2() is used by default.

（由 Tarek Ziadé 贡献。）

In addition, the shutil module now supports archiving operations for zipfiles, uncompressed tarfiles, gzipped tarfiles, and bzipped tarfiles. And there are functions for registering additional archiving file formats (such as xz compressed tarfiles or custom formats).

The principal functions are make_archive() and unpack_archive(). By default, both operate on the current directory (which can be set by os.chdir()) and on any sub-directories. The archive filename needs to be specified with a full pathname. The archiving step is non-destructive (the original files are left unchanged).

>>> import shutil, pprint

>>> os.chdir('mydata')  # change to the source directory
>>> f = shutil.make_archive('/var/backup/mydata',
...                         'zip')      # archive the current directory
>>> f                                   # show the name of archive
'/var/backup/mydata.zip'
>>> os.chdir('tmp')                     # change to an unpacking
>>> shutil.unpack_archive('/var/backup/mydata.zip')  # recover the data

>>> pprint.pprint(shutil.get_archive_formats())  # display known formats
[('bztar', "bzip2'ed tar-file"),
 ('gztar', "gzip'ed tar-file"),
 ('tar', 'uncompressed tar file'),
 ('zip', 'ZIP file')]

>>> shutil.register_archive_format(     # register a new archive format
...     name='xz',
...     function=xz.compress,           # callable archiving function
...     extra_args=[('level', 8)],      # arguments to the function
...     description='xz compression'
... )

（由 Tarek Ziadé 贡献。）

sqlite3

The sqlite3 module was updated to pysqlite version 2.6.0. It has two new capabilities.

The sqlite3.Connection.in_transit attribute is true if there is an active transaction for uncommitted changes.

The sqlite3.Connection.enable_load_extension() and sqlite3.Connection.load_extension() methods allows you to load SQLite extensions from “.so” files. One well-known extension is the fulltext-search extension distributed with SQLite.

（由 R. David Murray 和 Shashwat Anand 在 bpo-4739 [https://bugs.python.org/issue4739] 中贡献。）

html

A new html module was introduced with only a single function, escape(), which is used for escaping reserved characters from HTML markup:

>>> import html
>>> html.escape('x > 2 && x < 7')
'x &gt; 2 &amp;&amp; x &lt; 7'

socket

The socket module has two new improvements.

Socket objects now have a detach() method which puts the socket into closed state without actually closing the underlying file descriptor. The latter can then be reused for other purposes. (Added by Antoine Pitrou; bpo-8524 [https://bugs.python.org/issue8524].)

socket.create_connection() now supports the context management protocol to unconditionally consume socket.error exceptions and to close the socket when done. (Contributed by Giampaolo Rodolà; bpo-9794 [https://bugs.python.org/issue9794].)

ssl

The ssl module added a number of features to satisfy common requirements for secure (encrypted, authenticated) internet connections:

A new class, SSLContext, serves as a container for persistent SSL data, such as protocol settings, certificates, private keys, and various other options. It includes a wrap_socket() for creating an SSL socket from an SSL context.

A new function, ssl.match_hostname(), supports server identity verification for higher-level protocols by implementing the rules of HTTPS (from RFC 2818 [https://tools.ietf.org/html/rfc2818.html]) which are also suitable for other protocols.

The ssl.wrap_socket() constructor function now takes a ciphers argument. The ciphers string lists the allowed encryption algorithms using the format described in the OpenSSL documentation [https://www.openssl.org/docs/apps/ciphers.html#CIPHER-LIST-FORMAT].

When linked against recent versions of OpenSSL, the ssl module now supports the Server Name Indication extension to the TLS protocol, allowing multiple “virtual hosts” using different certificates on a single IP port. This extension is only supported in client mode, and is activated by passing the server_hostname argument to ssl.SSLContext.wrap_socket().

Various options have been added to the ssl module, such as OP_NO_SSLv2 which disables the insecure and obsolete SSLv2 protocol.

The extension now loads all the OpenSSL ciphers and digest algorithms. If some SSL certificates cannot be verified, they are reported as an “unknown algorithm” error.

The version of OpenSSL being used is now accessible using the module attributes ssl.OPENSSL_VERSION (a string), ssl.OPENSSL_VERSION_INFO (a 5-tuple), and ssl.OPENSSL_VERSION_NUMBER (an integer).

（由 Antoine Pitrou 在 bpo-1589 [https://bugs.python.org/issue1589], bpo-8322 [https://bugs.python.org/issue8322], bpo-5639 [https://bugs.python.org/issue5639], bpo-4870 [https://bugs.python.org/issue4870], bpo-8484 [https://bugs.python.org/issue8484] 和 bpo-8321 [https://bugs.python.org/issue8321] 中贡献。）

nntp

The nntplib module has a revamped implementation with better bytes and text semantics as well as more practical APIs. These improvements break compatibility with the nntplib version in Python 3.1, which was partly dysfunctional in itself.

Support for secure connections through both implicit (using nntplib.NNTP_SSL) and explicit (using nntplib.NNTP.starttls()) TLS has also been added.

（由 Antoine Pitrou 在 bpo-9360 [https://bugs.python.org/issue9360] 中贡献，由 Andrew Vant 在 bpo-1926 [https://bugs.python.org/issue1926] 中贡献。）

certificates

http.client.HTTPSConnection, urllib.request.HTTPSHandler and urllib.request.urlopen() now take optional arguments to allow for server certificate checking against a set of Certificate Authorities, as recommended in public uses of HTTPS.

(Added by Antoine Pitrou, bpo-9003 [https://bugs.python.org/issue9003].)

imaplib

Support for explicit TLS on standard IMAP4 connections has been added through the new imaplib.IMAP4.starttls method.

（由 Lorenzo M. Catucci 和 Antoine Pitrou 在 bpo-4471 [https://bugs.python.org/issue4471] 中贡献。）

http.client

There were a number of small API improvements in the http.client module. The old-style HTTP 0.9 simple responses are no longer supported and the strict parameter is deprecated in all classes.

The HTTPConnection and HTTPSConnection classes now have a source_address parameter for a (host, port) tuple indicating where the HTTP connection is made from.

Support for certificate checking and HTTPS virtual hosts were added to HTTPSConnection.

The request() method on connection objects allowed an optional body argument so that a file object could be used to supply the content of the request. Conveniently, the body argument now also accepts an iterable object so long as it includes an explicit Content-Length header. This extended interface is much more flexible than before.

To establish an HTTPS connection through a proxy server, there is a new set_tunnel() method that sets the host and port for HTTP Connect tunneling.

To match the behavior of http.server, the HTTP client library now also encodes headers with ISO-8859-1 (Latin-1) encoding. It was already doing that for incoming headers, so now the behavior is consistent for both incoming and outgoing traffic. (See work by Armin Ronacher in bpo-10980 [https://bugs.python.org/issue10980].)

unittest

The unittest module has a number of improvements supporting test discovery for packages, easier experimentation at the interactive prompt, new testcase methods, improved diagnostic messages for test failures, and better method names.

The command-line call python -m unittest can now accept file paths instead of module names for running specific tests (bpo-10620 [https://bugs.python.org/issue10620]). The new test discovery can find tests within packages, locating any test importable from the top-level directory. The top-level directory can be specified with the -t option, a pattern for matching files with -p, and a directory to start discovery with -s:

$ python -m unittest discover -s my_proj_dir -p _test.py

（由 Michael Foord 贡献）

Experimentation at the interactive prompt is now easier because the unittest.case.TestCase class can now be instantiated without arguments:

>>> from unittest import TestCase
>>> TestCase().assertEqual(pow(2, 3), 8)

（由 Michael Foord 贡献）

The unittest module has two new methods, assertWarns() and assertWarnsRegex() to verify that a given warning type is triggered by the code under test:

with self.assertWarns(DeprecationWarning):
    legacy_function('XYZ')

（由 Antoine Pitrou 在 bpo-9754 [https://bugs.python.org/issue9754] 中贡献。）

Another new method, assertCountEqual() is used to compare two iterables to determine if their element counts are equal (whether the same elements are present with the same number of occurrences regardless of order):

def test_anagram(self):
    self.assertCountEqual('algorithm', 'logarithm')

（由 Raymond Hettinger 贡献。）

A principal feature of the unittest module is an effort to produce meaningful diagnostics when a test fails. When possible, the failure is recorded along with a diff of the output. This is especially helpful for analyzing log files of failed test runs. However, since diffs can sometime be voluminous, there is a new maxDiff attribute that sets maximum length of diffs displayed.

In addition, the method names in the module have undergone a number of clean-ups.

For example, assertRegex() is the new name for assertRegexpMatches() which was misnamed because the test uses re.search(), not re.match(). Other methods using regular expressions are now named using short form “Regex” in preference to “Regexp” – this matches the names used in other unittest implementations, matches Python’s old name for the re module, and it has unambiguous camel-casing.

（由 Raymond Hettinger 贡献并由 Ezio Melotti 实现。）

To improve consistency, some long-standing method aliases are being deprecated in favor of the preferred names:

旧名称

首选名称

assert_()

assertTrue()

assertEquals()

assertEqual()

assertNotEquals()

assertNotEqual()

assertAlmostEquals()

assertAlmostEqual()

assertNotAlmostEquals()

assertNotAlmostEqual()

Likewise, the TestCase.fail* methods deprecated in Python 3.1 are expected to be removed in Python 3.3. Also see the Deprecated aliases section in the unittest documentation.

（由 Ezio Melotti 在 bpo-9424 [https://bugs.python.org/issue9424] 中贡献。）

The assertDictContainsSubset() method was deprecated because it was misimplemented with the arguments in the wrong order. This created hard-to-debug optical illusions where tests like TestCase().assertDictContainsSubset({'a':1, 'b':2}, {'a':1}) would fail.

（由 Raymond Hettinger 贡献。）

random

The integer methods in the random module now do a better job of producing uniform distributions. Previously, they computed selections with int(n*random()) which had a slight bias whenever n was not a power of two. Now, multiple selections are made from a range up to the next power of two and a selection is kept only when it falls within the range 0 <= x < n. The functions and methods affected are randrange(), randint(), choice(), shuffle() and sample().

（由 Raymond Hettinger 在 bpo-9025 [https://bugs.python.org/issue9025] 中贡献。）

poplib

POP3_SSL class now accepts a context parameter, which is a ssl.SSLContext object allowing bundling SSL configuration options, certificates and private keys into a single (potentially long-lived) structure.

（由 Giampaolo Rodolà 在 bpo-8807 [https://bugs.python.org/issue8807] 中贡献。）

asyncore

asyncore.dispatcher now provides a handle_accepted() method returning a (sock, addr) pair which is called when a connection has actually been established with a new remote endpoint. This is supposed to be used as a replacement for old handle_accept() and avoids the user to call accept() directly.

（由 Giampaolo Rodolà 在 bpo-6706 [https://bugs.python.org/issue6706] 中贡献。）

tempfile

The tempfile module has a new context manager, TemporaryDirectory which provides easy deterministic cleanup of temporary directories:

with tempfile.TemporaryDirectory() as tmpdirname:
    print('created temporary dir:', tmpdirname)

由 Neil Schemenauer 和 Nick Coghlan 在 bpo-13062 [https://bugs.python.org/issue13062] 中贡献。

inspect

The inspect module has a new function getgeneratorstate() to easily identify the current state of a generator-iterator:

>>> from inspect import getgeneratorstate
>>> def gen():
...     yield 'demo'
>>> g = gen()
>>> getgeneratorstate(g)
'GEN_CREATED'
>>> next(g)
'demo'
>>> getgeneratorstate(g)
'GEN_SUSPENDED'
>>> next(g, None)
>>> getgeneratorstate(g)
'GEN_CLOSED'

由 Rodolpho Eckhardt 和 Nick Coghlan 在 bpo-10220 [https://bugs.python.org/issue10220] 中贡献。

To support lookups without the possibility of activating a dynamic attribute, the inspect module has a new function, getattr_static(). Unlike hasattr(), this is a true read-only search, guaranteed not to change state while it is searching:

>>> class A:
...     @property
...     def f(self):
...         print('Running')
...         return 10
...
>>> a = A()
>>> getattr(a, 'f')
Running
10
>>> inspect.getattr_static(a, 'f')
<property object at 0x1022bd788>

（由 Michael Foord 贡献）

pydoc

The pydoc module now provides a much-improved Web server interface, as well as a new command-line option -b to automatically open a browser window to display that server:

$ pydoc3.2 -b

（由 Ron Adam 在 bpo-2001 [https://bugs.python.org/issue2001] 中贡献。）

dis

The dis module gained two new functions for inspecting code, code_info() and show_code(). Both provide detailed code object information for the supplied function, method, source code string or code object. The former returns a string and the latter prints it:

>>> import dis, random
>>> dis.show_code(random.choice)
Name:              choice
Filename:          /Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/random.py
Argument count:    2
Kw-only arguments: 0
Number of locals:  3
Stack size:        11
Flags:             OPTIMIZED, NEWLOCALS, NOFREE
Constants:
   0: 'Choose a random element from a non-empty sequence.'
   1: 'Cannot choose from an empty sequence'
Names:
   0: _randbelow
   1: len
   2: ValueError
   3: IndexError
Variable names:
   0: self
   1: seq
   2: i

In addition, the dis() function now accepts string arguments so that the common idiom dis(compile(s, '', 'eval')) can be shortened to dis(s):

>>> dis('3*x+1 if x%2==1 else x//2')
  1           0 LOAD_NAME                0 (x)
              3 LOAD_CONST               0 (2)
              6 BINARY_MODULO
              7 LOAD_CONST               1 (1)
             10 COMPARE_OP               2 (==)
             13 POP_JUMP_IF_FALSE       28
             16 LOAD_CONST               2 (3)
             19 LOAD_NAME                0 (x)
             22 BINARY_MULTIPLY
             23 LOAD_CONST               1 (1)
             26 BINARY_ADD
             27 RETURN_VALUE
        >>   28 LOAD_NAME                0 (x)
             31 LOAD_CONST               0 (2)
             34 BINARY_FLOOR_DIVIDE
             35 RETURN_VALUE

Taken together, these improvements make it easier to explore how CPython is implemented and to see for yourself what the language syntax does under-the-hood.

（由 Nick Coghlan 在 bpo-9147 [https://bugs.python.org/issue9147] 中贡献。）

dbm

All database modules now support the get() and setdefault() methods.

（由 Ray Allen 在 bpo-9523 [https://bugs.python.org/issue9523] 中建议。）

ctypes

A new type, ctypes.c_ssize_t represents the C ssize_t datatype.

site

The site module has three new functions useful for reporting on the details of a given Python installation.

getsitepackages() lists all global site-packages directories.

getuserbase() reports on the user’s base directory where data can be stored.

getusersitepackages() reveals the user-specific site-packages directory path.

>>> import site
>>> site.getsitepackages()
['/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages',
 '/Library/Frameworks/Python.framework/Versions/3.2/lib/site-python',
 '/Library/Python/3.2/site-packages']
>>> site.getuserbase()
'/Users/raymondhettinger/Library/Python/3.2'
>>> site.getusersitepackages()
'/Users/raymondhettinger/Library/Python/3.2/lib/python/site-packages'

Conveniently, some of site’s functionality is accessible directly from the command-line:

$ python -m site --user-base
/Users/raymondhettinger/.local
$ python -m site --user-site
/Users/raymondhettinger/.local/lib/python3.2/site-packages

（由 Tarek Ziadé 在 bpo-6693 [https://bugs.python.org/issue6693] 中贡献。）

sysconfig

The new sysconfig module makes it straightforward to discover installation paths and configuration variables that vary across platforms and installations.

The module offers access simple access functions for platform and version information:

get_platform() returning values like linux-i586 or macosx-10.6-ppc.

get_python_version() returns a Python version string such as “3.2”.

It also provides access to the paths and variables corresponding to one of seven named schemes used by distutils. Those include posix_prefix, posix_home, posix_user, nt, nt_user, os2, os2_home:

get_paths() makes a dictionary containing installation paths for the current installation scheme.

get_config_vars() returns a dictionary of platform specific variables.

还有一个方便的命令行界面：

C:\Python32>python -m sysconfig
Platform: "win32"
Python version: "3.2"
Current installation scheme: "nt"

Paths:
        data = "C:\Python32"
        include = "C:\Python32\Include"
        platinclude = "C:\Python32\Include"
        platlib = "C:\Python32\Lib\site-packages"
        platstdlib = "C:\Python32\Lib"
        purelib = "C:\Python32\Lib\site-packages"
        scripts = "C:\Python32\Scripts"
        stdlib = "C:\Python32\Lib"

Variables:
        BINDIR = "C:\Python32"
        BINLIBDEST = "C:\Python32\Lib"
        EXE = ".exe"
        INCLUDEPY = "C:\Python32\Include"
        LIBDEST = "C:\Python32\Lib"
        SO = ".pyd"
        VERSION = "32"
        abiflags = ""
        base = "C:\Python32"
        exec_prefix = "C:\Python32"
        platbase = "C:\Python32"
        prefix = "C:\Python32"
        projectbase = "C:\Python32"
        py_version = "3.2"
        py_version_nodot = "32"
        py_version_short = "3.2"
        srcdir = "C:\Python32"
        userbase = "C:\Documents and Settings\Raymond\Application Data\Python"

（由TarekZiadé 移出Distutils。）

pdb

The pdb debugger module gained a number of usability improvements:

pdb.py now has a -c option that executes commands as given in a .pdbrc script file.

A .pdbrc script file can contain continue and next commands that continue debugging.

The Pdb class constructor now accepts a nosigint argument.

New commands: l(list), ll(long list) and source for listing source code.

New commands: display and undisplay for showing or hiding the value of an expression if it has changed.

New command: interact for starting an interactive interpreter containing the global and local names found in the current scope.

Breakpoints can be cleared by breakpoint number.

（由Georg Brandl, Antonio Cuni 和 Ilya Sandler 贡献。）

configparser

The configparser module was modified to improve usability and predictability of the default parser and its supported INI syntax. The old ConfigParser class was removed in favor of SafeConfigParser which has in turn been renamed to ConfigParser. Support for inline comments is now turned off by default and section or option duplicates are not allowed in a single configuration source.

Config parsers gained a new API based on the mapping protocol:

>>> parser = ConfigParser()
>>> parser.read_string("""
... [DEFAULT]
... location = upper left
... visible = yes
... editable = no
... color = blue
...
... [main]
... title = Main Menu
... color = green
...
... [options]
... title = Options
... """)
>>> parser['main']['color']
'green'
>>> parser['main']['editable']
'no'
>>> section = parser['options']
>>> section['title']
'Options'
>>> section['title'] = 'Options (editable: %(editable)s)'
>>> section['title']
'Options (editable: no)'

The new API is implemented on top of the classical API, so custom parser subclasses should be able to use it without modifications.

The INI file structure accepted by config parsers can now be customized. Users can specify alternative option/value delimiters and comment prefixes, change the name of the DEFAULT section or switch the interpolation syntax.

There is support for pluggable interpolation including an additional interpolation handler ExtendedInterpolation:

>>> parser = ConfigParser(interpolation=ExtendedInterpolation())
>>> parser.read_dict({'buildout': {'directory': '/home/ambv/zope9'},
...                   'custom': {'prefix': '/usr/local'}})
>>> parser.read_string("""
... [buildout]
... parts =
...   zope9
...   instance
... find-links =
...   ${buildout:directory}/downloads/dist
...
... [zope9]
... recipe = plone.recipe.zope9install
... location = /opt/zope
...
... [instance]
... recipe = plone.recipe.zope9instance
... zope9-location = ${zope9:location}
... zope-conf = ${custom:prefix}/etc/zope.conf
... """)
>>> parser['buildout']['find-links']
'\n/home/ambv/zope9/downloads/dist'
>>> parser['instance']['zope-conf']
'/usr/local/etc/zope.conf'
>>> instance = parser['instance']
>>> instance['zope-conf']
'/usr/local/etc/zope.conf'
>>> instance['zope9-location']
'/opt/zope'

A number of smaller features were also introduced, like support for specifying encoding in read operations, specifying fallback values for get-functions, or reading directly from dictionaries and strings.

(All changes contributed by Łukasz Langa.)

urllib.parse

A number of usability improvements were made for the urllib.parse module.

The urlparse() function now supports IPv6 [https://en.wikipedia.org/wiki/IPv6] addresses as described in RFC 2732 [https://tools.ietf.org/html/rfc2732.html]:

>>> import urllib.parse
>>> urllib.parse.urlparse('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/')
ParseResult(scheme='http',
            netloc='[dead:beef:cafe:5417:affe:8FA3:deaf:feed]',
            path='/foo/',
            params='',
            query='',
            fragment='')

The urldefrag() function now returns a named tuple:

>>> r = urllib.parse.urldefrag('http://python.org/about/#target')
>>> r
DefragResult(url='http://python.org/about/', fragment='target')
>>> r[0]
'http://python.org/about/'
>>> r.fragment
'target'

And, the urlencode() function is now much more flexible, accepting either a string or bytes type for the query argument. If it is a string, then the safe, encoding, and error parameters are sent to quote_plus() for encoding:

>>> urllib.parse.urlencode([
...      ('type', 'telenovela'),
...      ('name', '¿Dónde Está Elisa?')],
...      encoding='latin-1')
'type=telenovela&name=%BFD%F3nde+Est%E1+Elisa%3F'

As detailed in 解析ASCII编码字节, all the urllib.parse functions now accept ASCII-encoded byte strings as input, so long as they are not mixed with regular strings. If ASCII-encoded byte strings are given as parameters, the return types will also be an ASCII-encoded byte strings:

>>> urllib.parse.urlparse(b'http://www.python.org:80/about/')
ParseResultBytes(scheme=b'http', netloc=b'www.python.org:80',
                 path=b'/about/', params=b'', query=b'', fragment=b'')

(Work by Nick Coghlan, Dan Mahn, and Senthil Kumaran in bpo-2987 [https://bugs.python.org/issue2987], bpo-5468 [https://bugs.python.org/issue5468], and bpo-9873 [https://bugs.python.org/issue9873].)

mailbox

Thanks to a concerted effort by R. David Murray, the mailbox module has been fixed for Python 3.2. The challenge was that mailbox had been originally designed with a text interface, but email messages are best represented with bytes because various parts of a message may have different encodings.

The solution harnessed the email package’s binary support for parsing arbitrary email messages. In addition, the solution required a number of API changes.

As expected, the add() method for mailbox.Mailbox objects now accepts binary input.

StringIO and text file input are deprecated. Also, string input will fail early if non-ASCII characters are used. Previously it would fail when the email was processed in a later step.

There is also support for binary output. The get_file() method now returns a file in the binary mode (where it used to incorrectly set the file to text-mode). There is also a new get_bytes() method that returns a bytes representation of a message corresponding to a given key.

It is still possible to get non-binary output using the old API’s get_string() method, but that approach is not very useful. Instead, it is best to extract messages from a Message object or to load them from binary input.

(Contributed by R. David Murray, with efforts from Steffen Daode Nurpmeso and an initial patch by Victor Stinner in bpo-9124 [https://bugs.python.org/issue9124].)

turtledemo

The demonstration code for the turtle module was moved from the Demo directory to main library. It includes over a dozen sample scripts with lively displays. Being on sys.path, it can now be run directly from the command-line:

$ python -m turtledemo

(Moved from the Demo directory by Alexander Belopolsky in bpo-10199 [https://bugs.python.org/issue10199].)

多线程

The mechanism for serializing execution of concurrently running Python threads (generally known as the GIL or Global Interpreter Lock) has been rewritten. Among the objectives were more predictable switching intervals and reduced overhead due to lock contention and the number of ensuing system calls. The notion of a “check interval” to allow thread switches has been abandoned and replaced by an absolute duration expressed in seconds. This parameter is tunable through sys.setswitchinterval(). It currently defaults to 5 milliseconds.

Additional details about the implementation can be read from a python-dev mailing-list message [https://mail.python.org/pipermail/python-dev/2009-October/093321.html] (however, “priority requests” as exposed in this message have not been kept for inclusion).

（由 Antoine Pitrou 贡献。）

Regular and recursive locks now accept an optional timeout argument to their acquire() method. (Contributed by Antoine Pitrou; bpo-7316 [https://bugs.python.org/issue7316].)

Similarly, threading.Semaphore.acquire() also gained a timeout argument. (Contributed by Torsten Landschoff; bpo-850728 [https://bugs.python.org/issue850728].)

Regular and recursive lock acquisitions can now be interrupted by signals on platforms using Pthreads. This means that Python programs that deadlock while acquiring locks can be successfully killed by repeatedly sending SIGINT to the process (by pressing Ctrl+C in most shells). (Contributed by Reid Kleckner; bpo-8844 [https://bugs.python.org/issue8844].)

性能优化

A number of small performance enhancements have been added:

Python’s peephole optimizer now recognizes patterns such x in {1, 2, 3} as being a test for membership in a set of constants. The optimizer recasts the set as a frozenset and stores the pre-built constant.

Now that the speed penalty is gone, it is practical to start writing membership tests using set-notation. This style is both semantically clear and operationally fast:

extension = name.rpartition('.')[2]
if extension in {'xml', 'html', 'xhtml', 'css'}:
    handle(name)

(Patch and additional tests contributed by Dave Malcolm; bpo-6690 [https://bugs.python.org/issue6690]).

Serializing and unserializing data using the pickle module is now several times faster.

（由 Alexandre Vassalotti, Antoine Pitrou 和 Unladen Swallow 团队在 bpo-9410 [https://bugs.python.org/issue9410] 和 bpo-3873 [https://bugs.python.org/issue3873] 中贡献。）

The Timsort algorithm [https://en.wikipedia.org/wiki/Timsort] used in list.sort() and sorted() now runs faster and uses less memory when called with a key function. Previously, every element of a list was wrapped with a temporary object that remembered the key value associated with each element. Now, two arrays of keys and values are sorted in parallel. This saves the memory consumed by the sort wrappers, and it saves time lost to delegating comparisons.

(Patch by Daniel Stutzbach in bpo-9915 [https://bugs.python.org/issue9915].)

JSON decoding performance is improved and memory consumption is reduced whenever the same string is repeated for multiple keys. Also, JSON encoding now uses the C speedups when the sort_keys argument is true.

（由Antoine Pitrou 在 bpo-7451 [https://bugs.python.org/issue7451] 中贡献，由 Raymond Hettinger 和 Antoine Pitrou 在 bpo-10314 [https://bugs.python.org/issue10314] 中贡献。）

Recursive locks (created with the threading.RLock() API) now benefit from a C implementation which makes them as fast as regular locks, and between 10x and 15x faster than their previous pure Python implementation.

（由 Antoine Pitrou 在 bpo-3001 [https://bugs.python.org/issue3001] 中贡献。）

The fast-search algorithm in stringlib is now used by the split(), rsplit(), splitlines() and replace() methods on bytes, bytearray and str objects. Likewise, the algorithm is also used by rfind(), rindex(), rsplit() and rpartition().

(Patch by Florent Xicluna in bpo-7622 [https://bugs.python.org/issue7622] and bpo-7462 [https://bugs.python.org/issue7462].)

Integer to string conversions now work two “digits” at a time, reducing the number of division and modulo operations.

(bpo-6713 [https://bugs.python.org/issue6713] by Gawain Bolton, Mark Dickinson, and Victor Stinner.)

There were several other minor optimizations. Set differencing now runs faster when one operand is much larger than the other (patch by Andress Bennetts in bpo-8685 [https://bugs.python.org/issue8685]). The array.repeat() method has a faster implementation (bpo-1569291 [https://bugs.python.org/issue1569291] by Alexander Belopolsky). The BaseHTTPRequestHandler has more efficient buffering (bpo-3709 [https://bugs.python.org/issue3709] by Andrew Schaaf). The operator.attrgetter() function has been sped-up (bpo-10160 [https://bugs.python.org/issue10160] by Christos Georgiou). And ConfigParser loads multi-line arguments a bit faster (bpo-7113 [https://bugs.python.org/issue7113] by Łukasz Langa).

Unicode

Python has been updated to Unicode 6.0.0 [http://unicode.org/versions/Unicode6.0.0/]. The update to the standard adds over 2,000 new characters including emoji [https://en.wikipedia.org/wiki/Emoji] symbols which are important for mobile phones.

In addition, the updated standard has altered the character properties for two Kannada characters (U+0CF1, U+0CF2) and one New Tai Lue numeric character (U+19DA), making the former eligible for use in identifiers while disqualifying the latter. For more information, see Unicode Character Database Changes [http://www.unicode.org/versions/Unicode6.0.0/#Database_Changes].

编解码器

Support was added for cp720 Arabic DOS encoding (bpo-1616979 [https://bugs.python.org/issue1616979]).

MBCS encoding no longer ignores the error handler argument. In the default strict mode, it raises an UnicodeDecodeError when it encounters an undecodable byte sequence and an UnicodeEncodeError for an unencodable character.

The MBCS codec supports 'strict' and 'ignore' error handlers for decoding, and 'strict' and 'replace' for encoding.

To emulate Python3.1 MBCS encoding, select the 'ignore' handler for decoding and the 'replace' handler for encoding.

On Mac OS X, Python decodes command line arguments with 'utf-8' rather than the locale encoding.

By default, tarfile uses 'utf-8' encoding on Windows (instead of 'mbcs') and the 'surrogateescape' error handler on all operating systems.

文档

The documentation continues to be improved.

A table of quick links has been added to the top of lengthy sections such as 内置函数. In the case of itertools, the links are accompanied by tables of cheatsheet-style summaries to provide an overview and memory jog without having to read all of the docs.

In some cases, the pure Python source code can be a helpful adjunct to the documentation, so now many modules now feature quick links to the latest version of the source code. For example, the functools module documentation has a quick link at the top labeled:

源代码 Lib/functools.py [https://github.com/python/cpython/tree/3.6/Lib/functools.py].

（由 Raymond Hettinger 贡献，参见 rationale [https://rhettinger.wordpress.com/2011/01/28/open-your-source-more/]。）

The docs now contain more examples and recipes. In particular, re module has an extensive section, 正则表达式例子. Likewise, the itertools module continues to be updated with new Itertools食谱.

The datetime module now has an auxiliary implementation in pure Python. No functionality was changed. This just provides an easier-to-read alternate implementation.

（由 Alexander Belopolsky 在 bpo-9528 [https://bugs.python.org/issue9528] 中贡献。）

The unmaintained Demo directory has been removed. Some demos were integrated into the documentation, some were moved to the Tools/demo directory, and others were removed altogether.

（由 Georg Brandl 在 bpo-7962 [https://bugs.python.org/issue7962] 中贡献）

IDLE

The format menu now has an option to clean source files by stripping trailing whitespace.

（由 Raymond Hettinger 在 bpo-5150 [https://bugs.python.org/issue5150] 中贡献。）

IDLE on Mac OS X now works with both Carbon AquaTk and Cocoa AquaTk.

（由 Kevin Walzer, Ned Deily 和 Ronald Oussoren 在 bpo-6075 [https://bugs.python.org/issue6075] 中贡献。）

代码库

In addition to the existing Subversion code repository at http://svn.python.org there is now a Mercurial [https://www.mercurial-scm.org/] repository at https://hg.python.org/.

After the 3.2 release, there are plans to switch to Mercurial as the primary repository. This distributed version control system should make it easier for members of the community to create and share external changesets. See PEP 385 [https://www.python.org/dev/peps/pep-0385] for details.

To learn to use the new version control system, see the tutorial by Joel Spolsky [http://hginit.com] or the Guide to Mercurial Workflows [https://www.mercurial-scm.org/guide].

构建和 C API 的改变

Changes to Python’s build process and to the C API include:

The idle, pydoc and 2to3 scripts are now installed with a version-specific suffix on make altinstall (bpo-10679 [https://bugs.python.org/issue10679]).

The C functions that access the Unicode Database now accept and return characters from the full Unicode range, even on narrow unicode builds (Py_UNICODE_TOLOWER, Py_UNICODE_ISDECIMAL, and others). A visible difference in Python is that unicodedata.numeric() now returns the correct value for large code points, and repr() may consider more characters as printable.

(Reported by Bupjoe Lee and fixed by Amaury Forgeot D’Arc; bpo-5127 [https://bugs.python.org/issue5127].)

Computed gotos are now enabled by default on supported compilers (which are detected by the configure script). They can still be disabled selectively by specifying --without-computed-gotos.

（由 Antoine Pitrou 在 bpo-9203 [https://bugs.python.org/issue9203] 中贡献。）

The option --with-wctype-functions was removed. The built-in unicode database is now used for all functions.

（由 Amaury Forgeot d’Arc 在 bpo-9210 [https://bugs.python.org/issue9210] 中贡献。）

Hash values are now values of a new type, Py_hash_t, which is defined to be the same size as a pointer. Previously they were of type long, which on some 64-bit operating systems is still only 32 bits long. As a result of this fix, set and dict can now hold more than 2**32 entries on builds with 64-bit pointers (previously, they could grow to that size but their performance degraded catastrophically).

(Suggested by Raymond Hettinger and implemented by Benjamin Peterson; bpo-9778 [https://bugs.python.org/issue9778].)

A new macro Py_VA_COPY copies the state of the variable argument list. It is equivalent to C99 va_copy but available on all Python platforms (bpo-2443 [https://bugs.python.org/issue2443]).

A new C API function PySys_SetArgvEx() allows an embedded interpreter to set sys.argv without also modifying sys.path (bpo-5753 [https://bugs.python.org/issue5753]).

PyEval_CallObject is now only available in macro form. The function declaration, which was kept for backwards compatibility reasons, is now removed – the macro was introduced in 1997 (bpo-8276 [https://bugs.python.org/issue8276]).

There is a new function PyLong_AsLongLongAndOverflow() which is analogous to PyLong_AsLongAndOverflow(). They both serve to convert Python int into a native fixed-width type while providing detection of cases where the conversion won’t fit (bpo-7767 [https://bugs.python.org/issue7767]).

The PyUnicode_CompareWithASCIIString() function now returns not equal if the Python string is NUL terminated.

There is a new function PyErr_NewExceptionWithDoc() that is like PyErr_NewException() but allows a docstring to be specified. This lets C exceptions have the same self-documenting capabilities as their pure Python counterparts (bpo-7033 [https://bugs.python.org/issue7033]).

When compiled with the --with-valgrind option, the pymalloc allocator will be automatically disabled when running under Valgrind. This gives improved memory leak detection when running under Valgrind, while taking advantage of pymalloc at other times (bpo-2422 [https://bugs.python.org/issue2422]).

Removed the O? format from the PyArg_Parse functions. The format is no longer used and it had never been documented (bpo-8837 [https://bugs.python.org/issue8837]).

There were a number of other small changes to the C-API. See the Misc/NEWS [https://github.com/python/cpython/tree/3.6/Misc/NEWS] file for a complete list.

Also, there were a number of updates to the Mac OS X build, see Mac/BuildScript/README.txt [https://github.com/python/cpython/tree/3.6/Mac/BuildScript/README.txt] for details. For users running a 32/64-bit build, there is a known problem with the default Tcl/Tk on Mac OS X 10.6. Accordingly, we recommend installing an updated alternative such as ActiveState Tcl/Tk 8.5.9 [https://www.activestate.com/activetcl/downloads]. See https://www.python.org/download/mac/tcltk/ for additional details.

移植到 Python 3.2

This section lists previously described changes and other bugfixes that may require changes to your code:

The configparser module has a number of clean-ups. The major change is to replace the old ConfigParser class with long-standing preferred alternative SafeConfigParser. In addition there are a number of smaller incompatibilities:

The interpolation syntax is now validated on get() and set() operations. In the default interpolation scheme, only two tokens with percent signs are valid: %(name)s and %%, the latter being an escaped percent sign.

The set() and add_section() methods now verify that values are actual strings. Formerly, unsupported types could be introduced unintentionally.

Duplicate sections or options from a single source now raise either DuplicateSectionError or DuplicateOptionError. Formerly, duplicates would silently overwrite a previous entry.

Inline comments are now disabled by default so now the ; character can be safely used in values.

Comments now can be indented. Consequently, for ; or # to appear at the start of a line in multiline values, it has to be interpolated. This keeps comment prefix characters in values from being mistaken as comments.

"" is now a valid value and is no longer automatically converted to an empty string. For empty strings, use "option =" in a line.

The nntplib module was reworked extensively, meaning that its APIs are often incompatible with the 3.1 APIs.

bytearray objects can no longer be used as filenames; instead, they should be converted to bytes.

The array.tostring() and array.fromstring() have been renamed to array.tobytes() and array.frombytes() for clarity. The old names have been deprecated. (See bpo-8990 [https://bugs.python.org/issue8990].)

PyArg_Parse*() 函数:

“t#” format has been removed: use “s#” or “s*” instead

“w” and “w#” formats has been removed: use “w*” instead

The PyCObject type, deprecated in 3.1, has been removed. To wrap opaque C pointers in Python objects, the PyCapsule API should be used instead; the new type has a well-defined interface for passing typing safety information and a less complicated signature for calling a destructor.

The sys.setfilesystemencoding() function was removed because it had a flawed design.

The random.seed() function and method now salt string seeds with an sha512 hash function. To access the previous version of seed in order to reproduce Python 3.1 sequences, set the version argument to 1, random.seed(s, version=1).

The previously deprecated string.maketrans() function has been removed in favor of the static methods bytes.maketrans() and bytearray.maketrans(). This change solves the confusion around which types were supported by the string module. Now, str, bytes, and bytearray each have their own maketrans and translate methods with intermediate translation tables of the appropriate type.

（由Georg Brandl在 bpo-5675 [https://bugs.python.org/issue5675] 中贡献）

The previously deprecated contextlib.nested() function has been removed in favor of a plain with statement which can accept multiple context managers. The latter technique is faster (because it is built-in), and it does a better job finalizing multiple context managers when one of them raises an exception:

with open('mylog.txt') as infile, open('a.out', 'w') as outfile:
    for line in infile:
        if '<critical>' in line:
            outfile.write(line)

(Contributed by Georg Brandl and Mattias Brändström; appspot issue 53094 [https://codereview.appspot.com/53094].)

struct.pack() now only allows bytes for the s string pack code. Formerly, it would accept text arguments and implicitly encode them to bytes using UTF-8. This was problematic because it made assumptions about the correct encoding and because a variable-length encoding can fail when writing to fixed length segment of a structure.

Code such as struct.pack('<6sHHBBB', 'GIF87a', x, y) should be rewritten with to use bytes instead of text, struct.pack('<6sHHBBB', b'GIF87a', x, y).

(Discovered by David Beazley and fixed by Victor Stinner; bpo-10783 [https://bugs.python.org/issue10783].)

The xml.etree.ElementTree class now raises an xml.etree.ElementTree.ParseError when a parse fails. Previously it raised an xml.parsers.expat.ExpatError.

The new, longer str() value on floats may break doctests which rely on the old output format.

In subprocess.Popen, the default value for close_fds is now True under Unix; under Windows, it is True if the three standard streams are set to None, False otherwise. Previously, close_fds was always False by default, which produced difficult to solve bugs or race conditions when open file descriptors would leak into the child process.

Support for legacy HTTP 0.9 has been removed from urllib.request and http.client. Such support is still present on the server side (in http.server).

（由 Antoine Pitrou 在 bpo-10711 [https://bugs.python.org/issue10711] 中贡献。）

SSL sockets in timeout mode now raise socket.timeout when a timeout occurs, rather than a generic SSLError.

（由 Antoine Pitrou 在 bpo-10272 [https://bugs.python.org/issue10272] 中贡献。）

The misleading functions PyEval_AcquireLock() and PyEval_ReleaseLock() have been officially deprecated. The thread-state aware APIs (such as PyEval_SaveThread() and PyEval_RestoreThread()) should be used instead.

Due to security risks, asyncore.handle_accept() has been deprecated, and a new function, asyncore.handle_accepted(), was added to replace it.

（由 Giampaolo Rodola 在 bpo-6706 [https://bugs.python.org/issue6706] 中贡献。）

Due to the new GIL implementation, PyEval_InitThreads() cannot be called before Py_Initialize() anymore.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

Python 3.1 有什么新变化

作者

Raymond Hettinger

This article explains the new features in Python 3.1, compared to 3.0.

PEP 372: 有序字典

Regular Python dictionaries iterate over key/value pairs in arbitrary order. Over the years, a number of authors have written alternative implementations that remember the order that the keys were originally inserted. Based on the experiences from those implementations, a new collections.OrderedDict class has been introduced.

The OrderedDict API is substantially the same as regular dictionaries but will iterate over keys and values in a guaranteed order depending on when a key was first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.

The standard library now supports use of ordered dictionaries in several modules. The configparser module uses them by default. This lets configuration files be read, modified, and then written back in their original order. The _asdict() method for collections.namedtuple() now returns an ordered dictionary with the values appearing in the same order as the underlying tuple indicies. The json module is being built-out with an object_pairs_hook to allow OrderedDicts to be built by the decoder. Support was also added for third-party tools like PyYAML [http://pyyaml.org/].

参见

PEP 372 [https://www.python.org/dev/peps/pep-0372] - 有序字典

PEP 由 Armin Ronacher 和 Raymond Hettinger 撰写，由 Raymond Hettinger 实现。

PEP 378: 千位分隔符的格式说明符

The built-in format() function and the str.format() method use a mini-language that now includes a simple, non-locale aware way to format a number with a thousands separator. That provides a way to humanize a program’s output, improving its professional appearance and readability:

>>> format(1234567, ',d')
'1,234,567'
>>> format(1234567.89, ',.2f')
'1,234,567.89'
>>> format(12345.6 + 8901234.12j, ',f')
'12,345.600000+8,901,234.120000j'
>>> format(Decimal('1234567.89'), ',f')
'1,234,567.89'

The supported types are int, float, complex and decimal.Decimal.

Discussions are underway about how to specify alternative separators like dots, spaces, apostrophes, or underscores. Locale-aware applications should use the existing n format specifier which already has some support for thousands separators.

参见

PEP 378 [https://www.python.org/dev/peps/pep-0378] - 千位分隔符的格式说明符

PEP 由 Raymond Hettinger 撰写，并由 Eric Smith 和 Mark Dickinson 实现

其他语言特性修改

对Python 语言核心进行的小改动：

Directories and zip archives containing a __main__.py file can now be executed directly by passing their name to the interpreter. The directory/zipfile is automatically inserted as the first entry in sys.path. (Suggestion and initial patch by Andy Chu; revised patch by Phillip J. Eby and Nick Coghlan; bpo-1739468 [https://bugs.python.org/issue1739468].)

The int() type gained a bit_length method that returns the number of bits necessary to represent its argument in binary:

>>> n = 37
>>> bin(37)
'0b100101'
>>> n.bit_length()
6
>>> n = 2**123-1
>>> n.bit_length()
123
>>> (n+1).bit_length()
124

（由 Fredrik Johansson, Victor Stinner, Raymond Hettinger 和 Mark Dickinson 在 bpo-36326 [https://bugs.python.org/issue36326] 中贡献。）

The fields in format() strings can now be automatically numbered:

>>> 'Sir {} of {}'.format('Gallahad', 'Camelot')
'Sir Gallahad of Camelot'

Formerly, the string would have required numbered fields such as: 'Sir {0} of {1}'.

（由 Eric Smith在 bpo-5237 [https://bugs.python.org/issue5237] 中贡献）

The string.maketrans() function is deprecated and is replaced by new static methods, bytes.maketrans() and bytearray.maketrans(). This change solves the confusion around which types were supported by the string module. Now, str, bytes, and bytearray each have their own maketrans and translate methods with intermediate translation tables of the appropriate type.

（由Georg Brandl在 bpo-5675 [https://bugs.python.org/issue5675] 中贡献）

The syntax of the with statement now allows multiple context managers in a single statement:

>>> with open('mylog.txt') as infile, open('a.out', 'w') as outfile:
...     for line in infile:
...         if '<critical>' in line:
...             outfile.write(line)

With the new syntax, the contextlib.nested() function is no longer needed and is now deprecated.

(Contributed by Georg Brandl and Mattias Brändström; appspot issue 53094 [https://codereview.appspot.com/53094].)

round(x, n) now returns an integer if x is an integer. Previously it returned a float:

>>> round(1123, -2)
1100

（由 Mark Dickinson在 bpo-4707 [https://bugs.python.org/issue4707] 贡献）

Python now uses David Gay’s algorithm for finding the shortest floating point representation that doesn’t change its value. This should help mitigate some of the confusion surrounding binary floating point numbers.

The significance is easily seen with a number like 1.1 which does not have an exact equivalent in binary floating point. Since there is no exact equivalent, an expression like float('1.1') evaluates to the nearest representable value which is 0x1.199999999999ap+0 in hex or 1.100000000000000088817841970012523233890533447265625 in decimal. That nearest value was and still is used in subsequent floating point calculations.

What is new is how the number gets displayed. Formerly, Python used a simple approach. The value of repr(1.1) was computed as format(1.1, '.17g') which evaluated to '1.1000000000000001'. The advantage of using 17 digits was that it relied on IEEE-754 guarantees to assure that eval(repr(1.1)) would round-trip exactly to its original value. The disadvantage is that many people found the output to be confusing (mistaking intrinsic limitations of binary floating point representation as being a problem with Python itself).

The new algorithm for repr(1.1) is smarter and returns '1.1'. Effectively, it searches all equivalent string representations (ones that get stored with the same underlying float value) and returns the shortest representation.

The new algorithm tends to emit cleaner representations when possible, but it does not change the underlying values. So, it is still the case that 1.1 + 2.2 != 3.3 even though the representations may suggest otherwise.

The new algorithm depends on certain features in the underlying floating point implementation. If the required features are not found, the old algorithm will continue to be used. Also, the text pickle protocols assure cross-platform portability by using the old algorithm.

（由 Eric Smith 和 Mark Dickinson 在 bpo-1580 [https://bugs.python.org/issue1580] 贡献）

新增，改进和弃用的模块

Added a collections.Counter class to support convenient counting of unique items in a sequence or iterable:

>>> Counter(['red', 'blue', 'red', 'green', 'blue', 'blue'])
Counter({'blue': 3, 'red': 2, 'green': 1})

（由 Raymond Hettinger 在 bpo-1696199 [https://bugs.python.org/issue1696199] 中贡献。）

Added a new module, tkinter.ttk for access to the Tk themed widget set. The basic idea of ttk is to separate, to the extent possible, the code implementing a widget’s behavior from the code implementing its appearance.

（由 Guilherme Polo 在 bpo-2983 [https://bugs.python.org/issue2983] 中贡献。）

The gzip.GzipFile and bz2.BZ2File classes now support the context management protocol:

>>> # Automatically close file after writing
>>> with gzip.GzipFile(filename, "wb") as f:
...     f.write(b"xxx")

（由 Antoine Pitrou 贡献。）

The decimal module now supports methods for creating a decimal object from a binary float. The conversion is exact but can sometimes be surprising:

>>> Decimal.from_float(1.1)
Decimal('1.100000000000000088817841970012523233890533447265625')

The long decimal result shows the actual binary fraction being stored for 1.1. The fraction has many digits because 1.1 cannot be exactly represented in binary.

（由Raymond Hettinger 和 Mark Dickinson贡献。）

The itertools module grew two new functions. The itertools.combinations_with_replacement() function is one of four for generating combinatorics including permutations and Cartesian products. The itertools.compress() function mimics its namesake from APL. Also, the existing itertools.count() function now has an optional step argument and can accept any type of counting sequence including fractions.Fraction and decimal.Decimal:

>>> [p+q for p,q in combinations_with_replacement('LOVE', 2)]
['LL', 'LO', 'LV', 'LE', 'OO', 'OV', 'OE', 'VV', 'VE', 'EE']

>>> list(compress(data=range(10), selectors=[0,0,1,1,0,1,0,1,0,0]))
[2, 3, 5, 7]

>>> c = count(start=Fraction(1,2), step=Fraction(1,6))
>>> [next(c), next(c), next(c), next(c)]
[Fraction(1, 2), Fraction(2, 3), Fraction(5, 6), Fraction(1, 1)]

（由 Raymond Hettinger 贡献。）

collections.namedtuple() now supports a keyword argument rename which lets invalid fieldnames be automatically converted to positional names in the form _0, _1, etc. This is useful when the field names are being created by an external source such as a CSV header, SQL field list, or user input:

>>> query = input()
SELECT region, dept, count(*) FROM main GROUPBY region, dept

>>> cursor.execute(query)
>>> query_fields = [desc[0] for desc in cursor.description]
>>> UserQuery = namedtuple('UserQuery', query_fields, rename=True)
>>> pprint.pprint([UserQuery(*row) for row in cursor])
[UserQuery(region='South', dept='Shipping', _2=185),
 UserQuery(region='North', dept='Accounting', _2=37),
 UserQuery(region='West', dept='Sales', _2=419)]

（由 Raymond Hettinger 在 bpo-1818 [https://bugs.python.org/issue1818] 中贡献。）

The re.sub(), re.subn() and re.split() functions now accept a flags parameter.

（由 Gregory Smith 贡献）

The logging module now implements a simple logging.NullHandler class for applications that are not using logging but are calling library code that does. Setting-up a null handler will suppress spurious warnings such as “No handlers could be found for logger foo”:

>>> h = logging.NullHandler()
>>> logging.getLogger("foo").addHandler(h)

（由 Vinay Sajip 在 bpo-4384 [https://bugs.python.org/issue4384] 中贡献。）

The runpy module which supports the -m command line switch now supports the execution of packages by looking for and executing a __main__ submodule when a package name is supplied.

（由 Andi Vajda 在 bpo-4195 [https://bugs.python.org/issue4195] 中贡献。）

The pdb module can now access and display source code loaded via zipimport (or any other conformant PEP 302 [https://www.python.org/dev/peps/pep-0302] loader).

（由 Alexander Belopolsky 在 bpo-4201 [https://bugs.python.org/issue4201] 中贡献。）

functools.partial objects can now be pickled.

(Suggested by Antoine Pitrou and Jesse Noller. Implemented by Jack Diederich; bpo-5228 [https://bugs.python.org/issue5228].)

Add pydoc help topics for symbols so that help('@') works as expected in the interactive environment.

（由 David Laban 在 bpo-4739 [https://bugs.python.org/issue4739] 中贡献。）

The unittest module now supports skipping individual tests or classes of tests. And it supports marking a test as an expected failure, a test that is known to be broken, but shouldn’t be counted as a failure on a TestResult:

class TestGizmo(unittest.TestCase):

    @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
    def test_gizmo_on_windows(self):
        ...

    @unittest.expectedFailure
    def test_gimzo_without_required_library(self):
        ...

Also, tests for exceptions have been builtout to work with context managers using the with statement:

def test_division_by_zero(self):
    with self.assertRaises(ZeroDivisionError):
        x / 0

In addition, several new assertion methods were added including assertSetEqual(), assertDictEqual(), assertDictContainsSubset(), assertListEqual(), assertTupleEqual(), assertSequenceEqual(), assertRaisesRegexp(), assertIsNone(), and assertIsNotNone().

（由Benjamin Peterson 和 Antoine Pitrou 贡献。）

The io module has three new constants for the seek() method SEEK_SET, SEEK_CUR, and SEEK_END.

The sys.version_info tuple is now a named tuple:

>>> sys.version_info
sys.version_info(major=3, minor=1, micro=0, releaselevel='alpha', serial=2)

（由 Ross Light 在 bpo-4285 [https://bugs.python.org/issue4285] 中贡献。）

The nntplib and imaplib modules now support IPv6.

（由 Derek Morr 在 bpo-14807 [https://bugs.python.org/issue14807] 和 bpo-1664 [https://bugs.python.org/issue1664] 中贡献。）

The pickle module has been adapted for better interoperability with Python 2.x when used with protocol 2 or lower. The reorganization of the standard library changed the formal reference for many objects. For example, __builtin__.set in Python 2 is called builtins.set in Python 3. This change confounded efforts to share data between different versions of Python. But now when protocol 2 or lower is selected, the pickler will automatically use the old Python 2 names for both loading and dumping. This remapping is turned-on by default but can be disabled with the fix_imports option:

>>> s = {1, 2, 3}
>>> pickle.dumps(s, protocol=0)
b'c__builtin__\nset\np0\n((lp1\nL1L\naL2L\naL3L\natp2\nRp3\n.'
>>> pickle.dumps(s, protocol=0, fix_imports=False)
b'cbuiltins\nset\np0\n((lp1\nL1L\naL2L\naL3L\natp2\nRp3\n.'

An unfortunate but unavoidable side-effect of this change is that protocol 2 pickles produced by Python 3.1 won’t be readable with Python 3.0. The latest pickle protocol, protocol 3, should be used when migrating data between Python 3.x implementations, as it doesn’t attempt to remain compatible with Python 2.x.

（由 Alexandre Vassalotti 和 Antoine Pitrou 在 bpo-6137 [https://bugs.python.org/issue6137] 中贡献。）

A new module, importlib was added. It provides a complete, portable, pure Python reference implementation of the import statement and its counterpart, the __import__() function. It represents a substantial step forward in documenting and defining the actions that take place during imports.

（由 Brett Cannon 贡献。）

性能优化

Major performance enhancements have been added:

The new I/O library (as defined in PEP 3116 [https://www.python.org/dev/peps/pep-3116]) was mostly written in Python and quickly proved to be a problematic bottleneck in Python 3.0. In Python 3.1, the I/O library has been entirely rewritten in C and is 2 to 20 times faster depending on the task at hand. The pure Python version is still available for experimentation purposes through the _pyio module.

（由 Amaury Forgeot d’Arc 和 Antoine Pitrou 贡献。）

Added a heuristic so that tuples and dicts containing only untrackable objects are not tracked by the garbage collector. This can reduce the size of collections and therefore the garbage collection overhead on long-running programs, depending on their particular use of datatypes.

（由 Antoine Pitrou 在 bpo-4688 [https://bugs.python.org/issue4688] 中贡献。）

Enabling a configure option named --with-computed-gotos on compilers that support it (notably: gcc, SunPro, icc), the bytecode evaluation loop is compiled with a new dispatch mechanism which gives speedups of up to 20%, depending on the system, the compiler, and the benchmark.

（由 Antoine Pitrou 以及其他一些参与者在 bpo-4753 [https://bugs.python.org/issue4753] 中贡献。）

The decoding of UTF-8, UTF-16 and LATIN-1 is now two to four times faster.

（由 Antoine Pitrou 和 Amaury Forgeot d’Arc 在 bpo-4868 [https://bugs.python.org/issue4868] 中贡献。）

The json module now has a C extension to substantially improve its performance. In addition, the API was modified so that json works only with str, not with bytes. That change makes the module closely match the JSON specification [http://json.org/] which is defined in terms of Unicode.

（由 Bob Ippolito 在 bpo-4136 [https://bugs.python.org/issue4136] 中贡献。并由 Antoine Pitrou 和 Benjamin Peterson 转换为Py3.1）

Unpickling now interns the attribute names of pickled objects. This saves memory and allows pickles to be smaller.

（由 Jake McGuire 和 Antoine Pitrou 在 bpo-5084 [https://bugs.python.org/issue5084] 中贡献。）

IDLE

IDLE’s format menu now provides an option to strip trailing whitespace from a source file.

（由 Roger D. Serwy 在 bpo-5150 [https://bugs.python.org/issue5150] 中贡献。）

构建和 C API 的改变

Changes to Python’s build process and to the C API include:

Integers are now stored internally either in base 2**15 or in base 2**30, the base being determined at build time. Previously, they were always stored in base 2**15. Using base 2**30 gives significant performance improvements on 64-bit machines, but benchmark results on 32-bit machines have been mixed. Therefore, the default is to use base 2**30 on 64-bit machines and base 2**15 on 32-bit machines; on Unix, there’s a new configure option --enable-big-digits that can be used to override this default.

Apart from the performance improvements this change should be invisible to end users, with one exception: for testing and debugging purposes there’s a new sys.int_info that provides information about the internal format, giving the number of bits per digit and the size in bytes of the C type used to store each digit:

>>> import sys
>>> sys.int_info
sys.int_info(bits_per_digit=30, sizeof_digit=4)

（由 Mark Dickinson在 bpo-4258 [https://bugs.python.org/issue4258] 贡献）

The PyLong_AsUnsignedLongLong() function now handles a negative pylong by raising OverflowError instead of TypeError.

（由 Mark Dickinson 和 Lisandro Dalcrin 在 bpo-5175 [https://bugs.python.org/issue5175] 中贡献。）

Deprecated PyNumber_Int(). Use PyNumber_Long() instead.

（由 Mark Dickinson在 bpo-4910 [https://bugs.python.org/issue4910] 贡献）

Added a new PyOS_string_to_double() function to replace the deprecated functions PyOS_ascii_strtod() and PyOS_ascii_atof().

（由 Mark Dickinson在 bpo-5914 [https://bugs.python.org/issue5914] 贡献）

Added PyCapsule as a replacement for the PyCObject API. The principal difference is that the new type has a well defined interface for passing typing safety information and a less complicated signature for calling a destructor. The old type had a problematic API and is now deprecated.

（由 Larry Hastings 在 bpo-5630 [https://bugs.python.org/issue5630] 中贡献。）

移植到 Python 3.1

This section lists previously described changes and other bugfixes that may require changes to your code:

The new floating point string representations can break existing doctests. For example:

def e():
    '''Compute the base of natural logarithms.

    >>> e()
    2.7182818284590451

    '''
    return sum(1/math.factorial(x) for x in reversed(range(30)))

doctest.testmod()

**********************************************************************
Failed example:
    e()
Expected:
    2.7182818284590451
Got:
    2.718281828459045
**********************************************************************

The automatic name remapping in the pickle module for protocol 2 or lower can make Python 3.1 pickles unreadable in Python 3.0. One solution is to use protocol 3. Another solution is to set the fix_imports option to False. See the discussion above for more details.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

Python 3.0 有什么新变化

作者

Guido van Rossum

This article explains the new features in Python 3.0, compared to 2.6. Python 3.0, also known as “Python 3000” or “Py3K”, is the first ever intentionally backwards incompatible Python release. There are more changes than in a typical release, and more that are important for all Python users. Nevertheless, after digesting the changes, you’ll find that Python really hasn’t changed all that much – by and large, we’re mostly fixing well-known annoyances and warts, and removing a lot of old cruft.

This article doesn’t attempt to provide a complete specification of all new features, but instead tries to give a convenient overview. For full details, you should refer to the documentation for Python 3.0, and/or the many PEPs referenced in the text. If you want to understand the complete implementation and design rationale for a particular feature, PEPs usually have more details than the regular documentation; but note that PEPs usually are not kept up-to-date once a feature has been fully implemented.

Due to time constraints this document is not as complete as it should have been. As always for a new release, the Misc/NEWS file in the source distribution contains a wealth of detailed information about every small thing that was changed.

常见的绊脚石

This section lists those few changes that are most likely to trip you up if you’re used to Python 2.5.

Print Is A Function

The print statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old print statement (PEP 3105 [https://www.python.org/dev/peps/pep-3105]). Examples:

Old: print "The answer is", 2*2
New: print("The answer is", 2*2)

Old: print x,           # Trailing comma suppresses newline
New: print(x, end=" ")  # Appends a space instead of a newline

Old: print              # Prints a newline
New: print()            # You must call the function!

Old: print >>sys.stderr, "fatal error"
New: print("fatal error", file=sys.stderr)

Old: print (x, y)       # prints repr((x, y))
New: print((x, y))      # Not the same as print(x, y)!

You can also customize the separator between items, e.g.:

print("There are <", 2**32, "> possibilities!", sep="")

which produces:

There are <4294967296> possibilities!

注意

The print() function doesn’t support the “softspace” feature of the old print statement. For example, in Python 2.x, print "A\n", "B" would write "A\nB\n"; but in Python 3.0, print("A\n", "B") writes "A\n B\n".

Initially, you’ll be finding yourself typing the old print x a lot in interactive mode. Time to retrain your fingers to type print(x) instead!

When using the 2to3 source-to-source conversion tool, all print statements are automatically converted to print() function calls, so this is mostly a non-issue for larger projects.

Views And Iterators Instead Of Lists

Some well-known APIs no longer return lists:

dict methods dict.keys(), dict.items() and dict.values() return “views” instead of lists. For example, this no longer works: k = d.keys(); k.sort(). Use k = sorted(d) instead (this works in Python 2.5 too and is just as efficient).

Also, the dict.iterkeys(), dict.iteritems() and dict.itervalues() methods are no longer supported.

map() and filter() return iterators. If you really need a list and the input sequences are all of equal length, a quick fix is to wrap map() in list(), e.g. list(map(...)), but a better fix is often to use a list comprehension (especially when the original code uses lambda), or rewriting the code so it doesn’t need a list at all. Particularly tricky is map() invoked for the side effects of the function; the correct transformation is to use a regular for loop (since creating a list would just be wasteful).

If the input sequences are not of equal length, map() will stop at the termination of the shortest of the sequences. For full compatibility with map() from Python 2.x, also wrap the sequences in itertools.zip_longest(), e.g. map(func, *sequences) becomes list(map(func, itertools.zip_longest(*sequences))).

range() now behaves like xrange() used to behave, except it works with values of arbitrary size. The latter no longer exists.

zip() now returns an iterator.

Ordering Comparisons

Python 3.0 has simplified the rules for ordering comparisons:

The ordering comparison operators (<, <=, >=, >) raise a TypeError exception when the operands don’t have a meaningful natural ordering. Thus, expressions like 1 < '', 0 > None or len <= len are no longer valid, and e.g. None < None raises TypeError instead of returning False. A corollary is that sorting a heterogeneous list no longer makes sense – all the elements must be comparable to each other. Note that this does not apply to the == and != operators: objects of different incomparable types always compare unequal to each other.

builtin.sorted() and list.sort() no longer accept the cmp argument providing a comparison function. Use the key argument instead. N.B. the key and reverse arguments are now “keyword-only”.

The cmp() function should be treated as gone, and the __cmp__() special method is no longer supported. Use __lt__() for sorting, __eq__() with __hash__(), and other rich comparisons as needed. (If you really need the cmp() functionality, you could use the expression (a > b) - (a < b) as the equivalent for cmp(a, b).)

整数

PEP 237 [https://www.python.org/dev/peps/pep-0237]: Essentially, long renamed to int. That is, there is only one built-in integral type, named int; but it behaves mostly like the old long type.

PEP 238 [https://www.python.org/dev/peps/pep-0238]: An expression like 1/2 returns a float. Use 1//2 to get the truncating behavior. (The latter syntax has existed for years, at least since Python 2.2.)

The sys.maxint constant was removed, since there is no longer a limit to the value of integers. However, sys.maxsize can be used as an integer larger than any practical list or string index. It conforms to the implementation’s “natural” integer size and is typically the same as sys.maxint in previous releases on the same platform (assuming the same build options).

The repr() of a long integer doesn’t include the trailing L anymore, so code that unconditionally strips that character will chop off the last digit instead. (Use str() instead.)

Octal literals are no longer of the form 0720; use 0o720 instead.

Text Vs. Data Instead Of Unicode Vs. 8-bit

Everything you thought you knew about binary data and Unicode has changed.

Python 3.0 uses the concepts of text and (binary) data instead of Unicode strings and 8-bit strings. All text is Unicode; however encoded Unicode is represented as binary data. The type used to hold text is str, the type used to hold data is bytes. The biggest difference with the 2.x situation is that any attempt to mix text and data in Python 3.0 raises TypeError, whereas if you were to mix Unicode and 8-bit strings in Python 2.x, it would work if the 8-bit string happened to contain only 7-bit (ASCII) bytes, but you would get UnicodeDecodeError if it contained non-ASCII values. This value-specific behavior has caused numerous sad faces over the years.

As a consequence of this change in philosophy, pretty much all code that uses Unicode, encodings or binary data most likely has to change. The change is for the better, as in the 2.x world there were numerous bugs having to do with mixing encoded and unencoded text. To be prepared in Python 2.x, start using unicode for all unencoded text, and str for binary or encoded data only. Then the 2to3 tool will do most of the work for you.

You can no longer use u"..." literals for Unicode text. However, you must use b"..." literals for binary data.

As the str and bytes types cannot be mixed, you must always explicitly convert between them. Use str.encode() to go from str to bytes, and bytes.decode() to go from bytes to str. You can also use bytes(s, encoding=...) and str(b, encoding=...), respectively.

Like str, the bytes type is immutable. There is a separate mutable type to hold buffered binary data, bytearray. Nearly all APIs that accept bytes also accept bytearray. The mutable API is based on collections.MutableSequence.

All backslashes in raw string literals are interpreted literally. This means that '\U' and '\u' escapes in raw strings are not treated specially. For example, r'\u20ac' is a string of 6 characters in Python 3.0, whereas in 2.6, ur'\u20ac' was the single “euro” character. (Of course, this change only affects raw string literals; the euro character is '\u20ac' in Python 3.0.)

The built-in basestring abstract type was removed. Use str instead. The str and bytes types don’t have functionality enough in common to warrant a shared base class. The 2to3 tool (see below) replaces every occurrence of basestring with str.

Files opened as text files (still the default mode for open()) always use an encoding to map between strings (in memory) and bytes (on disk). Binary files (opened with a b in the mode argument) always use bytes in memory. This means that if a file is opened using an incorrect mode or encoding, I/O will likely fail loudly, instead of silently producing incorrect data. It also means that even Unix users will have to specify the correct mode (text or binary) when opening a file. There is a platform-dependent default encoding, which on Unixy platforms can be set with the LANG environment variable (and sometimes also with some other platform-specific locale-related environment variables). In many cases, but not all, the system default is UTF-8; you should never count on this default. Any application reading or writing more than pure ASCII text should probably have a way to override the encoding. There is no longer any need for using the encoding-aware streams in the codecs module.

The initial values of sys.stdin, sys.stdout and sys.stderr are now unicode-only text files (i.e., they are instances of io.TextIOBase). To read and write bytes data with these streams, you need to use their io.TextIOBase.buffer attribute.

Filenames are passed to and returned from APIs as (Unicode) strings. This can present platform-specific problems because on some platforms filenames are arbitrary byte strings. (On the other hand, on Windows filenames are natively stored as Unicode.) As a work-around, most APIs (e.g. open() and many functions in the os module) that take filenames accept bytes objects as well as strings, and a few APIs have a way to ask for a bytes return value. Thus, os.listdir() returns a list of bytes instances if the argument is a bytes instance, and os.getcwdb() returns the current working directory as a bytes instance. Note that when os.listdir() returns a list of strings, filenames that cannot be decoded properly are omitted rather than raising UnicodeError.

Some system APIs like os.environ and sys.argv can also present problems when the bytes made available by the system is not interpretable using the default encoding. Setting the LANG variable and rerunning the program is probably the best approach.

PEP 3138 [https://www.python.org/dev/peps/pep-3138]: The repr() of a string no longer escapes non-ASCII characters. It still escapes control characters and code points with non-printable status in the Unicode standard, however.

PEP 3120 [https://www.python.org/dev/peps/pep-3120]: The default source encoding is now UTF-8.

PEP 3131 [https://www.python.org/dev/peps/pep-3131]: Non-ASCII letters are now allowed in identifiers. (However, the standard library remains ASCII-only with the exception of contributor names in comments.)

The StringIO and cStringIO modules are gone. Instead, import the io module and use io.StringIO or io.BytesIO for text and data respectively.

See also the Unicode 指南, which was updated for Python 3.0.

Overview Of Syntax Changes

This section gives a brief overview of every syntactic change in Python 3.0.

新语法

PEP 3107 [https://www.python.org/dev/peps/pep-3107]: Function argument and return value annotations. This provides a standardized way of annotating a function’s parameters and return value. There are no semantics attached to such annotations except that they can be introspected at runtime using the __annotations__ attribute. The intent is to encourage experimentation through metaclasses, decorators or frameworks.

PEP 3102 [https://www.python.org/dev/peps/pep-3102]: Keyword-only arguments. Named parameters occurring after *args in the parameter list must be specified using keyword syntax in the call. You can also use a bare * in the parameter list to indicate that you don’t accept a variable-length argument list, but you do have keyword-only arguments.

Keyword arguments are allowed after the list of base classes in a class definition. This is used by the new convention for specifying a metaclass (see next section), but can be used for other purposes as well, as long as the metaclass supports it.

PEP 3104 [https://www.python.org/dev/peps/pep-3104]: nonlocal statement. Using nonlocal x you can now assign directly to a variable in an outer (but non-global) scope. nonlocal is a new reserved word.

PEP 3132 [https://www.python.org/dev/peps/pep-3132]: Extended Iterable Unpacking. You can now write things like a, b, *rest = some_sequence. And even *rest, a = stuff. The rest object is always a (possibly empty) list; the right-hand side may be any iterable. Example:

(a, *rest, b) = range(5)

This sets a to 0, b to 4, and rest to [1, 2, 3].

Dictionary comprehensions: {k: v for k, v in stuff} means the same thing as dict(stuff) but is more flexible. (This is PEP 274 [https://www.python.org/dev/peps/pep-0274] vindicated. :-)

Set literals, e.g. {1, 2}. Note that {} is an empty dictionary; use set() for an empty set. Set comprehensions are also supported; e.g., {x for x in stuff} means the same thing as set(stuff) but is more flexible.

New octal literals, e.g. 0o720 (already in 2.6). The old octal literals (0720) are gone.

New binary literals, e.g. 0b1010 (already in 2.6), and there is a new corresponding built-in function, bin().

Bytes literals are introduced with a leading b or B, and there is a new corresponding built-in function, bytes().

修改的语法

PEP 3109 [https://www.python.org/dev/peps/pep-3109] and PEP 3134 [https://www.python.org/dev/peps/pep-3134]: new raise statement syntax: raise [expr [from expr]]. See below.

as and with are now reserved words. (Since 2.6, actually.)

True, False, and None are reserved words. (2.6 partially enforced the restrictions on None already.)

Change from except exc, var to except exc as var. See PEP 3110 [https://www.python.org/dev/peps/pep-3110].

PEP 3115 [https://www.python.org/dev/peps/pep-3115]: New Metaclass Syntax. Instead of:

class C:
    __metaclass__ = M
    ...

you must now use:

class C(metaclass=M):
    ...

The module-global __metaclass__ variable is no longer supported. (It was a crutch to make it easier to default to new-style classes without deriving every class from object.)

List comprehensions no longer support the syntactic form [... for var in item1, item2, ...]. Use [... for var in (item1, item2, ...)] instead. Also note that list comprehensions have different semantics: they are closer to syntactic sugar for a generator expression inside a list() constructor, and in particular the loop control variables are no longer leaked into the surrounding scope.

The ellipsis (...) can be used as an atomic expression anywhere. (Previously it was only allowed in slices.) Also, it must now be spelled as .... (Previously it could also be spelled as . . ., by a mere accident of the grammar.)

移除的语法

PEP 3113 [https://www.python.org/dev/peps/pep-3113]: Tuple parameter unpacking removed. You can no longer write def foo(a, (b, c)): .... Use def foo(a, b_c): b, c = b_c instead.

Removed backticks (use repr() instead).

Removed <> (use != instead).

Removed keyword: exec() is no longer a keyword; it remains as a function. (Fortunately the function syntax was also accepted in 2.x.) Also note that exec() no longer takes a stream argument; instead of exec(f) you can use exec(f.read()).

Integer literals no longer support a trailing l or L.

String literals no longer support a leading u or U.

The from module import * syntax is only allowed at the module level, no longer inside functions.

The only acceptable syntax for relative imports is from .[module] import name. All import forms not starting with . are interpreted as absolute imports. (PEP 328 [https://www.python.org/dev/peps/pep-0328])

Classic classes are gone.

Changes Already Present In Python 2.6

Since many users presumably make the jump straight from Python 2.5 to Python 3.0, this section reminds the reader of new features that were originally designed for Python 3.0 but that were back-ported to Python 2.6. The corresponding sections in Python 2.6 有什么新变化 should be consulted for longer descriptions.

PEP 343: “with” 语句. The with statement is now a standard feature and no longer needs to be imported from the __future__. Also check out Writing Context Managers and contextlib 模块.

PEP 366: 从主模块显式相对导入. This enhances the usefulness of the -m option when the referenced module lives in a package.

PEP 370: 分用户的 site-packages 目录.

PEP 371: 多任务处理包.

PEP 3101: 高级字符串格式. Note: the 2.6 description mentions the format() method for both 8-bit and Unicode strings. In 3.0, only the str type (text strings with Unicode support) supports this method; the bytes type does not. The plan is to eventually make this the only API for string formatting, and to start deprecating the % operator in Python 3.1.

PEP 3105: print 改为函数. This is now a standard feature and no longer needs to be imported from __future__. More details were given above.

PEP 3110: 异常处理的变更. The except exc as var syntax is now standard and except exc, var is no longer supported. (Of course, the as var part is still optional.)

PEP 3112: 字节字面值. The b"..." string literal notation (and its variants like b'...', b"""...""", and br"...") now produces a literal of type bytes.

PEP 3116: 新 I/O 库. The io module is now the standard way of doing file I/O. The built-in open() function is now an alias for io.open() and has additional keyword arguments encoding, errors, newline and closefd. Also note that an invalid mode argument now raises ValueError, not IOError. The binary file object underlying a text file object can be accessed as f.buffer (but beware that the text object maintains a buffer of itself in order to speed up the encoding and decoding operations).

PEP 3118: 修改缓冲区协议. The old builtin buffer() is now really gone; the new builtin memoryview() provides (mostly) similar functionality.

PEP 3119: 抽象基类. The abc module and the ABCs defined in the collections module plays a somewhat more prominent role in the language now, and built-in collection types like dict and list conform to the collections.MutableMapping and collections.MutableSequence ABCs, respectively.

PEP 3127: 整型文字支持和语法. As mentioned above, the new octal literal notation is the only one supported, and binary literals have been added.

PEP 3129: 类装饰器.

PEP 3141: A Type Hierarchy for Numbers. The numbers module is another new use of ABCs, defining Python’s “numeric tower”. Also note the new fractions module which implements numbers.Rational.

Library Changes

Due to time constraints, this document does not exhaustively cover the very extensive changes to the standard library. PEP 3108 [https://www.python.org/dev/peps/pep-3108] is the reference for the major changes to the library. Here’s a capsule review:

Many old modules were removed. Some, like gopherlib (no longer used) and md5 (replaced by hashlib), were already deprecated by PEP 4 [https://www.python.org/dev/peps/pep-0004]. Others were removed as a result of the removal of support for various platforms such as Irix, BeOS and Mac OS 9 (see PEP 11 [https://www.python.org/dev/peps/pep-0011]). Some modules were also selected for removal in Python 3.0 due to lack of use or because a better replacement exists. See PEP 3108 [https://www.python.org/dev/peps/pep-3108] for an exhaustive list.

The bsddb3 package was removed because its presence in the core standard library has proved over time to be a particular burden for the core developers due to testing instability and Berkeley DB’s release schedule. However, the package is alive and well, externally maintained at https://www.jcea.es/programacion/pybsddb.htm.

Some modules were renamed because their old name disobeyed PEP 8 [https://www.python.org/dev/peps/pep-0008], or for various other reasons. Here’s the list:

旧名称

新名称

_winreg

winreg

ConfigParser

configparser

copy_reg

copyreg

队列

queue

SocketServer

socketserver

markupbase

_markupbase

repr

reprlib

test.test_support

test.support

A common pattern in Python 2.x is to have one version of a module implemented in pure Python, with an optional accelerated version implemented as a C extension; for example, pickle and cPickle. This places the burden of importing the accelerated version and falling back on the pure Python version on each user of these modules. In Python 3.0, the accelerated versions are considered implementation details of the pure Python versions. Users should always import the standard version, which attempts to import the accelerated version and falls back to the pure Python version. The pickle / cPickle pair received this treatment. The profile module is on the list for 3.1. The StringIO module has been turned into a class in the io module.

Some related modules have been grouped into packages, and usually the submodule names have been simplified. The resulting new packages are:

dbm (anydbm, dbhash, dbm, dumbdbm, gdbm, whichdb).

html (HTMLParser, htmlentitydefs).

http (httplib, BaseHTTPServer, CGIHTTPServer, SimpleHTTPServer, Cookie, cookielib).

tkinter (all Tkinter-related modules except turtle). The target audience of turtle doesn’t really care about tkinter. Also note that as of Python 2.6, the functionality of turtle has been greatly enhanced.

urllib (urllib, urllib2, urlparse, robotparse).

xmlrpc (xmlrpclib, DocXMLRPCServer, SimpleXMLRPCServer).

Some other changes to standard library modules, not covered by PEP 3108 [https://www.python.org/dev/peps/pep-3108]:

Killed sets. Use the built-in set() class.

Cleanup of the sys module: removed sys.exitfunc(), sys.exc_clear(), sys.exc_type, sys.exc_value, sys.exc_traceback. (Note that sys.last_type etc. remain.)

Cleanup of the array.array type: the read() and write() methods are gone; use fromfile() and tofile() instead. Also, the 'c' typecode for array is gone – use either 'b' for bytes or 'u' for Unicode characters.

Cleanup of the operator module: removed sequenceIncludes() and isCallable().

Cleanup of the thread module: acquire_lock() and release_lock() are gone; use acquire() and release() instead.

Cleanup of the random module: removed the jumpahead() API.

The new module is gone.

The functions os.tmpnam(), os.tempnam() and os.tmpfile() have been removed in favor of the tempfile module.

The tokenize module has been changed to work with bytes. The main entry point is now tokenize.tokenize(), instead of generate_tokens.

string.letters and its friends (string.lowercase and string.uppercase) are gone. Use string.ascii_letters etc. instead. (The reason for the removal is that string.letters and friends had locale-specific behavior, which is a bad idea for such attractively-named global “constants”.)

Renamed module __builtin__ to builtins (removing the underscores, adding an ‘s’). The __builtins__ variable found in most global namespaces is unchanged. To modify a builtin, you should use builtins, not __builtins__!

PEP 3101 [https://www.python.org/dev/peps/pep-3101]: A New Approach To String Formatting

A new system for built-in string formatting operations replaces the % string formatting operator. (However, the % operator is still supported; it will be deprecated in Python 3.1 and removed from the language at some later time.) Read PEP 3101 [https://www.python.org/dev/peps/pep-3101] for the full scoop.

Changes To Exceptions

The APIs for raising and catching exception have been cleaned up and new powerful features added:

PEP 352 [https://www.python.org/dev/peps/pep-0352]: All exceptions must be derived (directly or indirectly) from BaseException. This is the root of the exception hierarchy. This is not new as a recommendation, but the requirement to inherit from BaseException is new. (Python 2.6 still allowed classic classes to be raised, and placed no restriction on what you can catch.) As a consequence, string exceptions are finally truly and utterly dead.

Almost all exceptions should actually derive from Exception; BaseException should only be used as a base class for exceptions that should only be handled at the top level, such as SystemExit or KeyboardInterrupt. The recommended idiom for handling all exceptions except for this latter category is to use except Exception.

StandardError was removed.

Exceptions no longer behave as sequences. Use the args attribute instead.

PEP 3109 [https://www.python.org/dev/peps/pep-3109]: Raising exceptions. You must now use raise Exception(args) instead of raise Exception, args. Additionally, you can no longer explicitly specify a traceback; instead, if you have to do this, you can assign directly to the __traceback__ attribute (see below).

PEP 3110 [https://www.python.org/dev/peps/pep-3110]: Catching exceptions. You must now use except SomeException as variable instead of except SomeException, variable. Moreover, the variable is explicitly deleted when the except block is left.

PEP 3134 [https://www.python.org/dev/peps/pep-3134]: Exception chaining. There are two cases: implicit chaining and explicit chaining. Implicit chaining happens when an exception is raised in an except or finally handler block. This usually happens due to a bug in the handler block; we call this a secondary exception. In this case, the original exception (that was being handled) is saved as the __context__ attribute of the secondary exception. Explicit chaining is invoked with this syntax:

raise SecondaryException() from primary_exception

(where primary_exception is any expression that produces an exception object, probably an exception that was previously caught). In this case, the primary exception is stored on the __cause__ attribute of the secondary exception. The traceback printed when an unhandled exception occurs walks the chain of __cause__ and __context__ attributes and prints a separate traceback for each component of the chain, with the primary exception at the top. (Java users may recognize this behavior.)

PEP 3134 [https://www.python.org/dev/peps/pep-3134]: Exception objects now store their traceback as the __traceback__ attribute. This means that an exception object now contains all the information pertaining to an exception, and there are fewer reasons to use sys.exc_info() (though the latter is not removed).

A few exception messages are improved when Windows fails to load an extension module. For example, error code 193 is now %1 is not a valid Win32 application. Strings now deal with non-English locales.

Miscellaneous Other Changes

Operators And Special Methods

!= now returns the opposite of ==, unless == returns NotImplemented.

The concept of “unbound methods” has been removed from the language. When referencing a method as a class attribute, you now get a plain function object.

__getslice__(), __setslice__() and __delslice__() were killed. The syntax a[i:j] now translates to a.__getitem__(slice(i, j)) (or __setitem__() or __delitem__(), when used as an assignment or deletion target, respectively).

PEP 3114 [https://www.python.org/dev/peps/pep-3114]: the standard next() method has been renamed to __next__().

The __oct__() and __hex__() special methods are removed – oct() and hex() use __index__() now to convert the argument to an integer.

Removed support for __members__ and __methods__.

The function attributes named func_X have been renamed to use the __X__ form, freeing up these names in the function attribute namespace for user-defined attributes. To wit, func_closure, func_code, func_defaults, func_dict, func_doc, func_globals, func_name were renamed to __closure__, __code__, __defaults__, __dict__, __doc__, __globals__, __name__, respectively.

__nonzero__() is now __bool__().

Builtins

PEP 3135 [https://www.python.org/dev/peps/pep-3135]: New super(). You can now invoke super() without arguments and (assuming this is in a regular instance method defined inside a class statement) the right class and instance will automatically be chosen. With arguments, the behavior of super() is unchanged.

PEP 3111 [https://www.python.org/dev/peps/pep-3111]: raw_input() was renamed to input(). That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped. It raises EOFError if the input is terminated prematurely. To get the old behavior of input(), use eval(input()).

A new built-in function next() was added to call the __next__() method on an object.

The round() function rounding strategy and return type have changed. Exact halfway cases are now rounded to the nearest even result instead of away from zero. (For example, round(2.5) now returns 2 rather than 3.) round(x[, n]) now delegates to x.__round__([n]) instead of always returning a float. It generally returns an integer when called with a single argument and a value of the same type as x when called with two arguments.

Moved intern() to sys.intern().

Removed: apply(). Instead of apply(f, args) use f(*args).

Removed callable(). Instead of callable(f) you can use isinstance(f, collections.Callable). The operator.isCallable() function is also gone.

Removed coerce(). This function no longer serves a purpose now that classic classes are gone.

Removed execfile(). Instead of execfile(fn) use exec(open(fn).read()).

Removed the file type. Use open(). There are now several different kinds of streams that open can return in the io module.

Removed reduce(). Use functools.reduce() if you really need it; however, 99 percent of the time an explicit for loop is more readable.

Removed reload(). Use imp.reload().

Removed. dict.has_key() – use the in operator instead.

构建和 C API 的改变

Due to time constraints, here is a very incomplete list of changes to the C API.

Support for several platforms was dropped, including but not limited to Mac OS 9, BeOS, RISCOS, Irix, and Tru64.

PEP 3118 [https://www.python.org/dev/peps/pep-3118]: New Buffer API.

PEP 3121 [https://www.python.org/dev/peps/pep-3121]: Extension Module Initialization & Finalization.

PEP 3123 [https://www.python.org/dev/peps/pep-3123]: Making PyObject_HEAD conform to standard C.

No more C API support for restricted execution.

PyNumber_Coerce(), PyNumber_CoerceEx(), PyMember_Get(), and PyMember_Set() C APIs are removed.

New C API PyImport_ImportModuleNoBlock(), works like PyImport_ImportModule() but won’t block on the import lock (returning an error instead).

Renamed the boolean conversion C-level slot and method: nb_nonzero is now nb_bool.

Removed METH_OLDARGS and WITH_CYCLE_GC from the C API.

性能

The net result of the 3.0 generalizations is that Python 3.0 runs the pystone benchmark around 10% slower than Python 2.5. Most likely the biggest cause is the removal of special-casing for small integers. There’s room for improvement, but it will happen after 3.0 is released!

移植 Python 3.0

For porting existing Python 2.5 or 2.6 source code to Python 3.0, the best strategy is the following:

(Prerequisite:) Start with excellent test coverage.

Port to Python 2.6. This should be no more work than the average port from Python 2.x to Python 2.(x+1). Make sure all your tests pass.

(Still using 2.6:) Turn on the -3 command line switch. This enables warnings about features that will be removed (or change) in 3.0. Run your test suite again, and fix code that you get warnings about until there are no warnings left, and all your tests still pass.

Run the 2to3 source-to-source translator over your source code tree. (See 2to3 - 自动将 Python 2 代码转为 Python 3 代码 for more on this tool.) Run the result of the translation under Python 3.0. Manually fix up any remaining issues, fixing problems until all tests pass again.

It is not recommended to try to write source code that runs unchanged under both Python 2.6 and 3.0; you’d have to use a very contorted coding style, e.g. avoiding print statements, metaclasses, and much more. If you are maintaining a library that needs to support both Python 2.6 and Python 3.0, the best approach is to modify step 3 above by editing the 2.6 version of the source code and running the 2to3 translator again, rather than editing the 3.0 version of the source code.

For porting C extensions to Python 3.0, please see 将扩展模块移植到 Python 3.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

Python 2.7 有什么新变化

作者

A.M. Kuchling (amk at amk.ca)

本文介绍了Python 2.7 的新功能。 Python 2.7 于2010年7月3日发布。

Numeric handling has been improved in many ways, for both floating-point numbers and for the Decimal class. There are some useful additions to the standard library, such as a greatly enhanced unittest module, the argparse module for parsing command-line options, convenient OrderedDict and Counter classes in the collections module, and many other improvements.

Python 2.7 is planned to be the last of the 2.x releases, so we worked on making it a good release for the long term. To help with porting to Python 3, several new features from the Python 3.x series have been included in 2.7.

This article doesn’t attempt to provide a complete specification of the new features, but instead provides a convenient overview. For full details, you should refer to the documentation for Python 2.7 at https://docs.python.org. If you want to understand the rationale for the design and implementation, refer to the PEP for a particular new feature or the issue on https://bugs.python.org in which a change was discussed. Whenever possible, “What’s New in Python” links to the bug/patch item for each change.

Python 2.x的未来

Python 2.7 是 2.x 系列中的最后一个主版本，因为Python 维护人员已将新功能开发工作的重点转移到了 Python 3.x 系列中。这意味着，尽管 Python 2 会继续修复bug并更新，以便在新的硬件和支持操作系统版本上正确构建，但不会有新的功能发布。

然而，尽管在 Python 2.7 和 Python 3 之间有一个很大的公共子集，并且迁移到该公共子集或直接迁移到 Python 3 所涉及的许多更改可以安全地自动化完成。但是一些其他更改（特别是那些与Unicode处理相关的更改）可能需要仔细考虑，并且最好用自动化回归测试套件进行健壮性测试，以便有效地迁移。

这意味着 Python2.7 将长期保留，为尚未移植到 Python 3 的生产系统提供一个稳定且受支持的基础平台。Python 2.7系列的预期完整生命周期在 PEP 373 [https://www.python.org/dev/peps/pep-0373] 中有详细介绍。

长期保留 2.7 版的的一些关键后果：

如上所述，与早期的2.x版本相比，2.7版本的维护时间更长。目前，预计核心开发团队将继续支持Python 2.7（接收安全更新和其他错误修复），直到至少2020年（首次发布后10年，相比之下，通常的支持期为18–24个月）。

随着 Python 2.7 标准库的老化，有效地利用 Python 包索引（直接或通过重新分发者）对 Python 2 用户来说变得更加重要。除了各种任务的第三方包之外，可用的包还包括与 Python 2 兼容的 Python 3 标准库中的新模块和功能的后端移植，以及各种工具和库，这些工具和库可以让用户更容易迁移到 Python 3。 Python 包用户指南 [https://packaging.python.org] 提供了从 Python 包索引的下载和安装软件的指导。

虽然现在增强 Python 2 的首选方法是在Python包索引上发布新包，但这种方法不一定适用于所有情况，尤其是与网络安全相关的情况。在一些特殊情况下，如果在PyPI上发布新的或更新的包无法得到充分的处理，则可以使用Python增强建议过程来提出直接在Python 2标准库中添加新功能。任何此类添加及其添加的维护版本将在下面的 New Features Added to Python 2.7 Maintenance Releases 部分中注明。

对于希望从 Python2 迁移到 Python3 的项目，或者对于希望同时支持 Python2 和 Python3 用户的库和框架开发人员，可以使用各种工具和指南来帮助决定合适的方法并管理所涉及的一些技术细节。建议从 将 Python 2 代码迁移到 Python 3 操作指南开始。

Changes to the Handling of Deprecation Warnings

For Python 2.7, a policy decision was made to silence warnings only of interest to developers by default. DeprecationWarning and its descendants are now ignored unless otherwise requested, preventing users from seeing warnings triggered by an application. This change was also made in the branch that became Python 3.2. (Discussed on stdlib-sig and carried out in bpo-7319 [https://bugs.python.org/issue7319].)

In previous releases, DeprecationWarning messages were enabled by default, providing Python developers with a clear indication of where their code may break in a future major version of Python.

However, there are increasingly many users of Python-based applications who are not directly involved in the development of those applications. DeprecationWarning messages are irrelevant to such users, making them worry about an application that’s actually working correctly and burdening application developers with responding to these concerns.

You can re-enable display of DeprecationWarning messages by running Python with the -Wdefault (short form: -Wd) switch, or by setting the PYTHONWARNINGS environment variable to "default" (or "d") before running Python. Python code can also re-enable them by calling warnings.simplefilter('default').

The unittest module also automatically reenables deprecation warnings when running tests.

Python 3.1 Features

就像 Python2.6 集成了 Python3.0 的特性一样，2.7版也集成了 Python3.1 中的一些新特性。2.x 系列继续提供迁移到3.x系列的工具。

3.1 功能的部分列表，这些功能已反向移植到 2.7：

The syntax for set literals ({1,2,3} is a mutable set).

Dictionary and set comprehensions ({i: i*2 for i in range(3)}).

Multiple context managers in a single with statement.

A new version of the io library, rewritten in C for performance.

The ordered-dictionary type described in PEP 372: Adding an Ordered Dictionary to collections.

The new "," format specifier described in PEP 378: 千位分隔符的格式说明符.

The memoryview object.

A small subset of the importlib module, described below.

The repr() of a float x is shorter in many cases: it’s now based on the shortest decimal string that’s guaranteed to round back to x. As in previous versions of Python, it’s guaranteed that float(repr(x)) recovers x.

Float-to-string and string-to-float conversions are correctly rounded. The round() function is also now correctly rounded.

The PyCapsule type, used to provide a C API for extension modules.

The PyLong_AsLongAndOverflow() C API function.

Other new Python3-mode warnings include:

operator.isCallable() and operator.sequenceIncludes(), which are not supported in 3.x, now trigger warnings.

The -3 switch now automatically enables the -Qwarn switch that causes warnings about using classic division with integers and long integers.

PEP 372: Adding an Ordered Dictionary to collections

Regular Python dictionaries iterate over key/value pairs in arbitrary order. Over the years, a number of authors have written alternative implementations that remember the order that the keys were originally inserted. Based on the experiences from those implementations, 2.7 introduces a new OrderedDict class in the collections module.

The OrderedDict API provides the same interface as regular dictionaries but iterates over keys and values in a guaranteed order depending on when a key was first inserted:

>>> from collections import OrderedDict
>>> d = OrderedDict([('first', 1),
...                  ('second', 2),
...                  ('third', 3)])
>>> d.items()
[('first', 1), ('second', 2), ('third', 3)]

If a new entry overwrites an existing entry, the original insertion position is left unchanged:

>>> d['second'] = 4
>>> d.items()
[('first', 1), ('second', 4), ('third', 3)]

Deleting an entry and reinserting it will move it to the end:

>>> del d['second']
>>> d['second'] = 5
>>> d.items()
[('first', 1), ('third', 3), ('second', 5)]

The popitem() method has an optional last argument that defaults to True. If last is true, the most recently added key is returned and removed; if it’s false, the oldest key is selected:

>>> od = OrderedDict([(x,0) for x in range(20)])
>>> od.popitem()
(19, 0)
>>> od.popitem()
(18, 0)
>>> od.popitem(last=False)
(0, 0)
>>> od.popitem(last=False)
(1, 0)

Comparing two ordered dictionaries checks both the keys and values, and requires that the insertion order was the same:

>>> od1 = OrderedDict([('first', 1),
...                    ('second', 2),
...                    ('third', 3)])
>>> od2 = OrderedDict([('third', 3),
...                    ('first', 1),
...                    ('second', 2)])
>>> od1 == od2
False
>>> # Move 'third' key to the end
>>> del od2['third']; od2['third'] = 3
>>> od1 == od2
True

Comparing an OrderedDict with a regular dictionary ignores the insertion order and just compares the keys and values.

How does the OrderedDict work? It maintains a doubly-linked list of keys, appending new keys to the list as they’re inserted. A secondary dictionary maps keys to their corresponding list node, so deletion doesn’t have to traverse the entire linked list and therefore remains O(1).

The standard library now supports use of ordered dictionaries in several modules.

The ConfigParser module uses them by default, meaning that configuration files can now be read, modified, and then written back in their original order.

The _asdict() method for collections.namedtuple() now returns an ordered dictionary with the values appearing in the same order as the underlying tuple indices.

The json module’s JSONDecoder class constructor was extended with an object_pairs_hook parameter to allow OrderedDict instances to be built by the decoder. Support was also added for third-party tools like PyYAML [http://pyyaml.org/].

参见

PEP 372 [https://www.python.org/dev/peps/pep-0372] - 将有序词典添加到集合中

PEP 由 Armin Ronacher 和 Raymond Hettinger 撰写，由 Raymond Hettinger 实现。

PEP 378: 千位分隔符的格式说明符

To make program output more readable, it can be useful to add separators to large numbers, rendering them as 18,446,744,073,709,551,616 instead of 18446744073709551616.

The fully general solution for doing this is the locale module, which can use different separators (“,” in North America, “.” in Europe) and different grouping sizes, but locale is complicated to use and unsuitable for multi-threaded applications where different threads are producing output for different locales.

Therefore, a simple comma-grouping mechanism has been added to the mini-language used by the str.format() method. When formatting a floating-point number, simply include a comma between the width and the precision:

>>> '{:20,.2f}'.format(18446744073709551616.0)
'18,446,744,073,709,551,616.00'

When formatting an integer, include the comma after the width:

>>> '{:20,d}'.format(18446744073709551616)
'18,446,744,073,709,551,616'

This mechanism is not adaptable at all; commas are always used as the separator and the grouping is always into three-digit groups. The comma-formatting mechanism isn’t as general as the locale module, but it’s easier to use.

参见

PEP 378 [https://www.python.org/dev/peps/pep-0378] - 千位分隔符的格式说明符

PEP 由 Raymond Hettinger 撰写，并由 Eric Smith 实现

PEP 389: The argparse Module for Parsing Command Lines

The argparse module for parsing command-line arguments was added as a more powerful replacement for the optparse module.

This means Python now supports three different modules for parsing command-line arguments: getopt, optparse, and argparse. The getopt module closely resembles the C library’s getopt() function, so it remains useful if you’re writing a Python prototype that will eventually be rewritten in C. optparse becomes redundant, but there are no plans to remove it because there are many scripts still using it, and there’s no automated way to update these scripts. (Making the argparse API consistent with optparse’s interface was discussed but rejected as too messy and difficult.)

In short, if you’re writing a new script and don’t need to worry about compatibility with earlier versions of Python, use argparse instead of optparse.

以下是为示例代码:

import argparse

parser = argparse.ArgumentParser(description='Command-line example.')

# Add optional switches
parser.add_argument('-v', action='store_true', dest='is_verbose',
                    help='produce verbose output')
parser.add_argument('-o', action='store', dest='output',
                    metavar='FILE',
                    help='direct output to FILE instead of stdout')
parser.add_argument('-C', action='store', type=int, dest='context',
                    metavar='NUM', default=0,
                    help='display NUM lines of added context')

# Allow any number of additional arguments.
parser.add_argument(nargs='*', action='store', dest='inputs',
                    help='input filenames (default is stdin)')

args = parser.parse_args()
print args.__dict__

Unless you override it, -h and --help switches are automatically added, and produce neatly formatted output:

-> ./python.exe argparse-example.py --help
usage: argparse-example.py [-h] [-v] [-o FILE] [-C NUM] [inputs [inputs ...]]

Command-line example.

positional arguments:
  inputs      input filenames (default is stdin)

optional arguments:
  -h, --help  show this help message and exit
  -v          produce verbose output
  -o FILE     direct output to FILE instead of stdout
  -C NUM      display NUM lines of added context

As with optparse, the command-line switches and arguments are returned as an object with attributes named by the dest parameters:

-> ./python.exe argparse-example.py -v
{'output': None,
 'is_verbose': True,
 'context': 0,
 'inputs': []}

-> ./python.exe argparse-example.py -v -o /tmp/output -C 4 file1 file2
{'output': '/tmp/output',
 'is_verbose': True,
 'context': 4,
 'inputs': ['file1', 'file2']}

argparse has much fancier validation than optparse; you can specify an exact number of arguments as an integer, 0 or more arguments by passing '*', 1 or more by passing '+', or an optional argument with '?'. A top-level parser can contain sub-parsers to define subcommands that have different sets of switches, as in svn commit, svn checkout, etc. You can specify an argument’s type as FileType, which will automatically open files for you and understands that '-' means standard input or output.

参见

argparse documentation

argparse 模块的文档页面。

升级 optparse 代码

Part of the Python documentation, describing how to convert code that uses optparse.

PEP 389 [https://www.python.org/dev/peps/pep-0389] - argparse - 新的命令行解析模块

PEP 由 Steven Bethard 撰写并实现。

PEP 391: Dictionary-Based Configuration For Logging

The logging module is very flexible; applications can define a tree of logging subsystems, and each logger in this tree can filter out certain messages, format them differently, and direct messages to a varying number of handlers.

All this flexibility can require a lot of configuration. You can write Python statements to create objects and set their properties, but a complex set-up requires verbose but boring code. logging also supports a fileConfig() function that parses a file, but the file format doesn’t support configuring filters, and it’s messier to generate programmatically.

Python 2.7 adds a dictConfig() function that uses a dictionary to configure logging. There are many ways to produce a dictionary from different sources: construct one with code; parse a file containing JSON; or use a YAML parsing library if one is installed. For more information see 配置函数.

The following example configures two loggers, the root logger and a logger named “network”. Messages sent to the root logger will be sent to the system log using the syslog protocol, and messages to the “network” logger will be written to a network.log file that will be rotated once the log reaches 1MB.

import logging
import logging.config

configdict = {
 'version': 1,    # Configuration schema in use; must be 1 for now
 'formatters': {
     'standard': {
         'format': ('%(asctime)s %(name)-15s '
                    '%(levelname)-8s %(message)s')}},

 'handlers': {'netlog': {'backupCount': 10,
                     'class': 'logging.handlers.RotatingFileHandler',
                     'filename': '/logs/network.log',
                     'formatter': 'standard',
                     'level': 'INFO',
                     'maxBytes': 1000000},
              'syslog': {'class': 'logging.handlers.SysLogHandler',
                         'formatter': 'standard',
                         'level': 'ERROR'}},

 # Specify all the subordinate loggers
 'loggers': {
             'network': {
                         'handlers': ['netlog']
             }
 },
 # Specify properties of the root logger
 'root': {
          'handlers': ['syslog']
 },
}

# Set up configuration
logging.config.dictConfig(configdict)

# As an example, log two error messages
logger = logging.getLogger('/')
logger.error('Database not found')

netlogger = logging.getLogger('network')
netlogger.error('Connection failed')

Three smaller enhancements to the logging module, all implemented by Vinay Sajip, are:

The SysLogHandler class now supports syslogging over TCP. The constructor has a socktype parameter giving the type of socket to use, either socket.SOCK_DGRAM for UDP or socket.SOCK_STREAM for TCP. The default protocol remains UDP.

Logger instances gained a getChild() method that retrieves a descendant logger using a relative path. For example, once you retrieve a logger by doing log = getLogger('app'), calling log.getChild('network.listen') is equivalent to getLogger('app.network.listen').

The LoggerAdapter class gained an isEnabledFor() method that takes a level and returns whether the underlying logger would process a message of that level of importance.

参见

PEP 391 [https://www.python.org/dev/peps/pep-0391] - 基于字典的日志配置

PEP 由 Vinay Sajip 撰写并实现

PEP 3106: Dictionary Views

The dictionary methods keys(), values(), and items() are different in Python 3.x. They return an object called a view instead of a fully materialized list.

It’s not possible to change the return values of keys(), values(), and items() in Python 2.7 because too much code would break. Instead the 3.x versions were added under the new names viewkeys(), viewvalues(), and viewitems().

>>> d = dict((i*10, chr(65+i)) for i in range(26))
>>> d
{0: 'A', 130: 'N', 10: 'B', 140: 'O', 20: ..., 250: 'Z'}
>>> d.viewkeys()
dict_keys([0, 130, 10, 140, 20, 150, 30, ..., 250])

Views can be iterated over, but the key and item views also behave like sets. The & operator performs intersection, and | performs a union:

>>> d1 = dict((i*10, chr(65+i)) for i in range(26))
>>> d2 = dict((i**.5, i) for i in range(1000))
>>> d1.viewkeys() & d2.viewkeys()
set([0.0, 10.0, 20.0, 30.0])
>>> d1.viewkeys() | range(0, 30)
set([0, 1, 130, 3, 4, 5, 6, ..., 120, 250])

The view keeps track of the dictionary and its contents change as the dictionary is modified:

>>> vk = d.viewkeys()
>>> vk
dict_keys([0, 130, 10, ..., 250])
>>> d[260] = '&'
>>> vk
dict_keys([0, 130, 260, 10, ..., 250])

However, note that you can’t add or remove keys while you’re iterating over the view:

>>> for k in vk:
...     d[k*2] = k
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration

You can use the view methods in Python 2.x code, and the 2to3 converter will change them to the standard keys(), values(), and items() methods.

参见

PEP 3106 [https://www.python.org/dev/peps/pep-3106] - 改造 dict.keys(), .values() 和 .items()

PEP written by Guido van Rossum. Backported to 2.7 by Alexandre Vassalotti; bpo-1967 [https://bugs.python.org/issue1967].

PEP 3137: The memoryview Object

The memoryview object provides a view of another object’s memory content that matches the bytes type’s interface.

>>> import string
>>> m = memoryview(string.letters)
>>> m
<memory at 0x37f850>
>>> len(m)           # Returns length of underlying object
52
>>> m[0], m[25], m[26]   # Indexing returns one byte
('a', 'z', 'A')
>>> m2 = m[0:26]         # Slicing returns another memoryview
>>> m2
<memory at 0x37f080>

The content of the view can be converted to a string of bytes or a list of integers:

>>> m2.tobytes()
'abcdefghijklmnopqrstuvwxyz'
>>> m2.tolist()
[97, 98, 99, 100, 101, 102, 103, ... 121, 122]
>>>

memoryview objects allow modifying the underlying object if it’s a mutable object.

>>> m2[0] = 75
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot modify read-only memory
>>> b = bytearray(string.letters)  # Creating a mutable object
>>> b
bytearray(b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
>>> mb = memoryview(b)
>>> mb[0] = '*'         # Assign to view, changing the bytearray.
>>> b[0:5]              # The bytearray has been changed.
bytearray(b'*bcde')
>>>

参见

PEP 3137 [https://www.python.org/dev/peps/pep-3137] - 不变字节和可变缓冲区

PEP written by Guido van Rossum. Implemented by Travis Oliphant, Antoine Pitrou and others. Backported to 2.7 by Antoine Pitrou; bpo-2396 [https://bugs.python.org/issue2396].

其他语言特性修改

对Python 语言核心进行的小改动：

The syntax for set literals has been backported from Python 3.x. Curly brackets are used to surround the contents of the resulting mutable set; set literals are distinguished from dictionaries by not containing colons and values. {} continues to represent an empty dictionary; use set() for an empty set.

>>> {1, 2, 3, 4, 5}
set([1, 2, 3, 4, 5])
>>> set() # empty set
set([])
>>> {}    # empty dict
{}

Backported by Alexandre Vassalotti; bpo-2335 [https://bugs.python.org/issue2335].

Dictionary and set comprehensions are another feature backported from 3.x, generalizing list/generator comprehensions to use the literal syntax for sets and dictionaries.

>>> {x: x*x for x in range(6)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
>>> {('a'*x) for x in range(6)}
set(['', 'a', 'aa', 'aaa', 'aaaa', 'aaaaa'])

Backported by Alexandre Vassalotti; bpo-2333 [https://bugs.python.org/issue2333].

The with statement can now use multiple context managers in one statement. Context managers are processed from left to right and each one is treated as beginning a new with statement. This means that:

with A() as a, B() as b:
    ... suite of statements ...

相当于:

with A() as a:
    with B() as b:
        ... suite of statements ...

The contextlib.nested() function provides a very similar function, so it’s no longer necessary and has been deprecated.

(Proposed in https://codereview.appspot.com/53094; implemented by Georg Brandl.)

Conversions between floating-point numbers and strings are now correctly rounded on most platforms. These conversions occur in many different places: str() on floats and complex numbers; the float and complex constructors; numeric formatting; serializing and deserializing floats and complex numbers using the marshal, pickle and json modules; parsing of float and imaginary literals in Python code; and Decimal-to-float conversion.

Related to this, the repr() of a floating-point number x now returns a result based on the shortest decimal string that’s guaranteed to round back to x under correct rounding (with round-half-to-even rounding mode). Previously it gave a string based on rounding x to 17 decimal digits.

The rounding library responsible for this improvement works on Windows and on Unix platforms using the gcc, icc, or suncc compilers. There may be a small number of platforms where correct operation of this code cannot be guaranteed, so the code is not used on such systems. You can find out which code is being used by checking sys.float_repr_style, which will be short if the new code is in use and legacy if it isn’t.

Implemented by Eric Smith and Mark Dickinson, using David Gay’s dtoa.c library; bpo-7117 [https://bugs.python.org/issue7117].

Conversions from long integers and regular integers to floating point now round differently, returning the floating-point number closest to the number. This doesn’t matter for small integers that can be converted exactly, but for large numbers that will unavoidably lose precision, Python 2.7 now approximates more closely. For example, Python 2.6 computed the following:

>>> n = 295147905179352891391
>>> float(n)
2.9514790517935283e+20
>>> n - long(float(n))
65535L

Python 2.7’s floating-point result is larger, but much closer to the true value:

>>> n = 295147905179352891391
>>> float(n)
2.9514790517935289e+20
>>> n - long(float(n))
-1L

(Implemented by Mark Dickinson; bpo-3166 [https://bugs.python.org/issue3166].)

Integer division is also more accurate in its rounding behaviours. (Also implemented by Mark Dickinson; bpo-1811 [https://bugs.python.org/issue1811].)

Implicit coercion for complex numbers has been removed; the interpreter will no longer ever attempt to call a __coerce__() method on complex objects. (Removed by Meador Inge and Mark Dickinson; bpo-5211 [https://bugs.python.org/issue5211].)

The str.format() method now supports automatic numbering of the replacement fields. This makes using str.format() more closely resemble using %s formatting:

>>> '{}:{}:{}'.format(2009, 04, 'Sunday')
'2009:4:Sunday'
>>> '{}:{}:{day}'.format(2009, 4, day='Sunday')
'2009:4:Sunday'

The auto-numbering takes the fields from left to right, so the first {...} specifier will use the first argument to str.format(), the next specifier will use the next argument, and so on. You can’t mix auto-numbering and explicit numbering – either number all of your specifier fields or none of them – but you can mix auto-numbering and named fields, as in the second example above. (Contributed by Eric Smith; bpo-5237 [https://bugs.python.org/issue5237].)

Complex numbers now correctly support usage with format(), and default to being right-aligned. Specifying a precision or comma-separation applies to both the real and imaginary parts of the number, but a specified field width and alignment is applied to the whole of the resulting 1.5+3j output. (Contributed by Eric Smith; bpo-1588 [https://bugs.python.org/issue1588] and bpo-7988 [https://bugs.python.org/issue7988].)

The ‘F’ format code now always formats its output using uppercase characters, so it will now produce ‘INF’ and ‘NAN’. (Contributed by Eric Smith; bpo-3382 [https://bugs.python.org/issue3382].)

A low-level change: the object.__format__() method now triggers a PendingDeprecationWarning if it’s passed a format string, because the __format__() method for object converts the object to a string representation and formats that. Previously the method silently applied the format string to the string representation, but that could hide mistakes in Python code. If you’re supplying formatting information such as an alignment or precision, presumably you’re expecting the formatting to be applied in some object-specific way. (Fixed by Eric Smith; bpo-7994 [https://bugs.python.org/issue7994].)

The int() and long() types gained a bit_length method that returns the number of bits necessary to represent its argument in binary:

>>> n = 37
>>> bin(n)
'0b100101'
>>> n.bit_length()
6
>>> n = 2**123-1
>>> n.bit_length()
123
>>> (n+1).bit_length()
124

(Contributed by Fredrik Johansson and Victor Stinner; bpo-3439 [https://bugs.python.org/issue3439].)

The import statement will no longer try an absolute import if a relative import (e.g. from .os import sep) fails. This fixes a bug, but could possibly break certain import statements that were only working by accident. (Fixed by Meador Inge; bpo-7902 [https://bugs.python.org/issue7902].)

It’s now possible for a subclass of the built-in unicode type to override the __unicode__() method. (Implemented by Victor Stinner; bpo-1583863 [https://bugs.python.org/issue1583863].)

The bytearray type’s translate() method now accepts None as its first argument. (Fixed by Georg Brandl; bpo-4759 [https://bugs.python.org/issue4759].)

When using @classmethod and @staticmethod to wrap methods as class or static methods, the wrapper object now exposes the wrapped function as their __func__ attribute. (Contributed by Amaury Forgeot d’Arc, after a suggestion by George Sakkis; bpo-5982 [https://bugs.python.org/issue5982].)

When a restricted set of attributes were set using __slots__, deleting an unset attribute would not raise AttributeError as you would expect. Fixed by Benjamin Peterson; bpo-7604 [https://bugs.python.org/issue7604].)

Two new encodings are now supported: “cp720”, used primarily for Arabic text; and “cp858”, a variant of CP 850 that adds the euro symbol. (CP720 contributed by Alexander Belchenko and Amaury Forgeot d’Arc in bpo-1616979 [https://bugs.python.org/issue1616979]; CP858 contributed by Tim Hatch in bpo-8016 [https://bugs.python.org/issue8016].)

The file object will now set the filename attribute on the IOError exception when trying to open a directory on POSIX platforms (noted by Jan Kaliszewski; bpo-4764 [https://bugs.python.org/issue4764]), and now explicitly checks for and forbids writing to read-only file objects instead of trusting the C library to catch and report the error (fixed by Stefan Krah; bpo-5677 [https://bugs.python.org/issue5677]).

The Python tokenizer now translates line endings itself, so the compile() built-in function now accepts code using any line-ending convention. Additionally, it no longer requires that the code end in a newline.

Extra parentheses in function definitions are illegal in Python 3.x, meaning that you get a syntax error from def f((x)): pass. In Python3-warning mode, Python 2.7 will now warn about this odd usage. (Noted by James Lingard; bpo-7362 [https://bugs.python.org/issue7362].)

It’s now possible to create weak references to old-style class objects. New-style classes were always weak-referenceable. (Fixed by Antoine Pitrou; bpo-8268 [https://bugs.python.org/issue8268].)

When a module object is garbage-collected, the module’s dictionary is now only cleared if no one else is holding a reference to the dictionary (bpo-7140 [https://bugs.python.org/issue7140]).

Interpreter Changes

A new environment variable, PYTHONWARNINGS, allows controlling warnings. It should be set to a string containing warning settings, equivalent to those used with the -W switch, separated by commas. (Contributed by Brian Curtin; bpo-7301 [https://bugs.python.org/issue7301].)

For example, the following setting will print warnings every time they occur, but turn warnings from the Cookie module into an error. (The exact syntax for setting an environment variable varies across operating systems and shells.)

export PYTHONWARNINGS=all,error:::Cookie:0

性能优化

Several performance enhancements have been added:

A new opcode was added to perform the initial setup for with statements, looking up the __enter__() and __exit__() methods. (Contributed by Benjamin Peterson.)

The garbage collector now performs better for one common usage pattern: when many objects are being allocated without deallocating any of them. This would previously take quadratic time for garbage collection, but now the number of full garbage collections is reduced as the number of objects on the heap grows. The new logic only performs a full garbage collection pass when the middle generation has been collected 10 times and when the number of survivor objects from the middle generation exceeds 10% of the number of objects in the oldest generation. (Suggested by Martin von Löwis and implemented by Antoine Pitrou; bpo-4074 [https://bugs.python.org/issue4074].)

The garbage collector tries to avoid tracking simple containers which can’t be part of a cycle. In Python 2.7, this is now true for tuples and dicts containing atomic types (such as ints, strings, etc.). Transitively, a dict containing tuples of atomic types won’t be tracked either. This helps reduce the cost of each garbage collection by decreasing the number of objects to be considered and traversed by the collector. (Contributed by Antoine Pitrou; bpo-4688 [https://bugs.python.org/issue4688].)

Long integers are now stored internally either in base 2**15 or in base 2**30, the base being determined at build time. Previously, they were always stored in base 2**15. Using base 2**30 gives significant performance improvements on 64-bit machines, but benchmark results on 32-bit machines have been mixed. Therefore, the default is to use base 2**30 on 64-bit machines and base 2**15 on 32-bit machines; on Unix, there’s a new configure option --enable-big-digits that can be used to override this default.

Apart from the performance improvements this change should be invisible to end users, with one exception: for testing and debugging purposes there’s a new structseq sys.long_info that provides information about the internal format, giving the number of bits per digit and the size in bytes of the C type used to store each digit:

>>> import sys
>>> sys.long_info
sys.long_info(bits_per_digit=30, sizeof_digit=4)

（由 Mark Dickinson在 bpo-4258 [https://bugs.python.org/issue4258] 贡献）

Another set of changes made long objects a few bytes smaller: 2 bytes smaller on 32-bit systems and 6 bytes on 64-bit. (Contributed by Mark Dickinson; bpo-5260 [https://bugs.python.org/issue5260].)

The division algorithm for long integers has been made faster by tightening the inner loop, doing shifts instead of multiplications, and fixing an unnecessary extra iteration. Various benchmarks show speedups of between 50% and 150% for long integer divisions and modulo operations. (Contributed by Mark Dickinson; bpo-5512 [https://bugs.python.org/issue5512].) Bitwise operations are also significantly faster (initial patch by Gregory Smith; bpo-1087418 [https://bugs.python.org/issue1087418]).

The implementation of % checks for the left-side operand being a Python string and special-cases it; this results in a 1–3% performance increase for applications that frequently use % with strings, such as templating libraries. (Implemented by Collin Winter; bpo-5176 [https://bugs.python.org/issue5176].)

List comprehensions with an if condition are compiled into faster bytecode. (Patch by Antoine Pitrou, back-ported to 2.7 by Jeffrey Yasskin; bpo-4715 [https://bugs.python.org/issue4715].)

Converting an integer or long integer to a decimal string was made faster by special-casing base 10 instead of using a generalized conversion function that supports arbitrary bases. (Patch by Gawain Bolton; bpo-6713 [https://bugs.python.org/issue6713].)

The split(), replace(), rindex(), rpartition(), and rsplit() methods of string-like types (strings, Unicode strings, and bytearray objects) now use a fast reverse-search algorithm instead of a character-by-character scan. This is sometimes faster by a factor of 10. (Added by Florent Xicluna; bpo-7462 [https://bugs.python.org/issue7462] and bpo-7622 [https://bugs.python.org/issue7622].)

The pickle and cPickle modules now automatically intern the strings used for attribute names, reducing memory usage of the objects resulting from unpickling. (Contributed by Jake McGuire; bpo-5084 [https://bugs.python.org/issue5084].)

The cPickle module now special-cases dictionaries, nearly halving the time required to pickle them. (Contributed by Collin Winter; bpo-5670 [https://bugs.python.org/issue5670].)

新增和改进的模块

As in every release, Python’s standard library received a number of enhancements and bug fixes. Here’s a partial list of the most notable changes, sorted alphabetically by module name. Consult the Misc/NEWS file in the source tree for a more complete list of changes, or look through the Subversion logs for all the details.

The bdb module’s base debugging class Bdb gained a feature for skipping modules. The constructor now takes an iterable containing glob-style patterns such as django.*; the debugger will not step into stack frames from a module that matches one of these patterns. (Contributed by Maru Newby after a suggestion by Senthil Kumaran; bpo-5142 [https://bugs.python.org/issue5142].)

The binascii module now supports the buffer API, so it can be used with memoryview instances and other similar buffer objects. (Backported from 3.x by Florent Xicluna; bpo-7703 [https://bugs.python.org/issue7703].)

Updated module: the bsddb module has been updated from 4.7.2devel9 to version 4.8.4 of the pybsddb package [https://www.jcea.es/programacion/pybsddb.htm]. The new version features better Python 3.x compatibility, various bug fixes, and adds several new BerkeleyDB flags and methods. (Updated by Jesús Cea Avión; bpo-8156 [https://bugs.python.org/issue8156]. The pybsddb changelog can be read at http://hg.jcea.es/pybsddb/file/tip/ChangeLog.)

The bz2 module’s BZ2File now supports the context management protocol, so you can write with bz2.BZ2File(...) as f:. (Contributed by Hagen Fürstenau; bpo-3860 [https://bugs.python.org/issue3860].)

New class: the Counter class in the collections module is useful for tallying data. Counter instances behave mostly like dictionaries but return zero for missing keys instead of raising a KeyError:

>>> from collections import Counter
>>> c = Counter()
>>> for letter in 'here is a sample of english text':
...   c[letter] += 1
...
>>> c
Counter({' ': 6, 'e': 5, 's': 3, 'a': 2, 'i': 2, 'h': 2,
'l': 2, 't': 2, 'g': 1, 'f': 1, 'm': 1, 'o': 1, 'n': 1,
'p': 1, 'r': 1, 'x': 1})
>>> c['e']
5
>>> c['z']
0

There are three additional Counter methods. most_common() returns the N most common elements and their counts. elements() returns an iterator over the contained elements, repeating each element as many times as its count. subtract() takes an iterable and subtracts one for each element instead of adding; if the argument is a dictionary or another Counter, the counts are subtracted.

>>> c.most_common(5)
[(' ', 6), ('e', 5), ('s', 3), ('a', 2), ('i', 2)]
>>> c.elements() ->
   'a', 'a', ' ', ' ', ' ', ' ', ' ', ' ',
   'e', 'e', 'e', 'e', 'e', 'g', 'f', 'i', 'i',
   'h', 'h', 'm', 'l', 'l', 'o', 'n', 'p', 's',
   's', 's', 'r', 't', 't', 'x'
>>> c['e']
5
>>> c.subtract('very heavy on the letter e')
>>> c['e']    # Count is now lower
-1

Contributed by Raymond Hettinger; bpo-1696199 [https://bugs.python.org/issue1696199].

New class: OrderedDict is described in the earlier section PEP 372: Adding an Ordered Dictionary to collections.

New method: The deque data type now has a count() method that returns the number of contained elements equal to the supplied argument x, and a reverse() method that reverses the elements of the deque in-place. deque also exposes its maximum length as the read-only maxlen attribute. (Both features added by Raymond Hettinger.)

The namedtuple class now has an optional rename parameter. If rename is true, field names that are invalid because they’ve been repeated or aren’t legal Python identifiers will be renamed to legal names that are derived from the field’s position within the list of fields:

>>> from collections import namedtuple
>>> T = namedtuple('T', ['field1', '$illegal', 'for', 'field2'], rename=True)
>>> T._fields
('field1', '_1', '_2', 'field2')

(Added by Raymond Hettinger; bpo-1818 [https://bugs.python.org/issue1818].)

Finally, the Mapping abstract base class now returns NotImplemented if a mapping is compared to another type that isn’t a Mapping. (Fixed by Daniel Stutzbach; bpo-8729 [https://bugs.python.org/issue8729].)

Constructors for the parsing classes in the ConfigParser module now take an allow_no_value parameter, defaulting to false; if true, options without values will be allowed. For example:

>>> import ConfigParser, StringIO
>>> sample_config = """
... [mysqld]
... user = mysql
... pid-file = /var/run/mysqld/mysqld.pid
... skip-bdb
... """
>>> config = ConfigParser.RawConfigParser(allow_no_value=True)
>>> config.readfp(StringIO.StringIO(sample_config))
>>> config.get('mysqld', 'user')
'mysql'
>>> print config.get('mysqld', 'skip-bdb')
None
>>> print config.get('mysqld', 'unknown')
Traceback (most recent call last):
  ...
NoOptionError: No option 'unknown' in section: 'mysqld'

(Contributed by Mats Kindahl; bpo-7005 [https://bugs.python.org/issue7005].)

Deprecated function: contextlib.nested(), which allows handling more than one context manager with a single with statement, has been deprecated, because the with statement now supports multiple context managers.

The cookielib module now ignores cookies that have an invalid version field, one that doesn’t contain an integer value. (Fixed by John J. Lee; bpo-3924 [https://bugs.python.org/issue3924].)

The copy module’s deepcopy() function will now correctly copy bound instance methods. (Implemented by Robert Collins; bpo-1515 [https://bugs.python.org/issue1515].)

The ctypes module now always converts None to a C NULL pointer for arguments declared as pointers. (Changed by Thomas Heller; bpo-4606 [https://bugs.python.org/issue4606].) The underlying libffi library [https://sourceware.org/libffi/] has been updated to version 3.0.9, containing various fixes for different platforms. (Updated by Matthias Klose; bpo-8142 [https://bugs.python.org/issue8142].)

New method: the datetime module’s timedelta class gained a total_seconds() method that returns the number of seconds in the duration. (Contributed by Brian Quinlan; bpo-5788 [https://bugs.python.org/issue5788].)

New method: the Decimal class gained a from_float() class method that performs an exact conversion of a floating-point number to a Decimal. This exact conversion strives for the closest decimal approximation to the floating-point representation’s value; the resulting decimal value will therefore still include the inaccuracy, if any. For example, Decimal.from_float(0.1) returns Decimal('0.1000000000000000055511151231257827021181583404541015625'). (Implemented by Raymond Hettinger; bpo-4796 [https://bugs.python.org/issue4796].)

Comparing instances of Decimal with floating-point numbers now produces sensible results based on the numeric values of the operands. Previously such comparisons would fall back to Python’s default rules for comparing objects, which produced arbitrary results based on their type. Note that you still cannot combine Decimal and floating-point in other operations such as addition, since you should be explicitly choosing how to convert between float and Decimal. (Fixed by Mark Dickinson; bpo-2531 [https://bugs.python.org/issue2531].)

The constructor for Decimal now accepts floating-point numbers (added by Raymond Hettinger; bpo-8257 [https://bugs.python.org/issue8257]) and non-European Unicode characters such as Arabic-Indic digits (contributed by Mark Dickinson; bpo-6595 [https://bugs.python.org/issue6595]).

Most of the methods of the Context class now accept integers as well as Decimal instances; the only exceptions are the canonical() and is_canonical() methods. (Patch by Juan José Conti; bpo-7633 [https://bugs.python.org/issue7633].)

When using Decimal instances with a string’s format() method, the default alignment was previously left-alignment. This has been changed to right-alignment, which is more sensible for numeric types. (Changed by Mark Dickinson; bpo-6857 [https://bugs.python.org/issue6857].)

Comparisons involving a signaling NaN value (or sNAN) now signal InvalidOperation instead of silently returning a true or false value depending on the comparison operator. Quiet NaN values (or NaN) are now hashable. (Fixed by Mark Dickinson; bpo-7279 [https://bugs.python.org/issue7279].)

The difflib module now produces output that is more compatible with modern diff/patch tools through one small change, using a tab character instead of spaces as a separator in the header giving the filename. (Fixed by Anatoly Techtonik; bpo-7585 [https://bugs.python.org/issue7585].)

The Distutils sdist command now always regenerates the MANIFEST file, since even if the MANIFEST.in or setup.py files haven’t been modified, the user might have created some new files that should be included. (Fixed by Tarek Ziadé; bpo-8688 [https://bugs.python.org/issue8688].)

The doctest module’s IGNORE_EXCEPTION_DETAIL flag will now ignore the name of the module containing the exception being tested. (Patch by Lennart Regebro; bpo-7490 [https://bugs.python.org/issue7490].)

The email module’s Message class will now accept a Unicode-valued payload, automatically converting the payload to the encoding specified by output_charset. (Added by R. David Murray; bpo-1368247 [https://bugs.python.org/issue1368247].)

The Fraction class now accepts a single float or Decimal instance, or two rational numbers, as arguments to its constructor. (Implemented by Mark Dickinson; rationals added in bpo-5812 [https://bugs.python.org/issue5812], and float/decimal in bpo-8294 [https://bugs.python.org/issue8294].)

Ordering comparisons (<, <=, >, >=) between fractions and complex numbers now raise a TypeError. This fixes an oversight, making the Fraction match the other numeric types.

New class: FTP_TLS in the ftplib module provides secure FTP connections using TLS encapsulation of authentication as well as subsequent control and data transfers. (Contributed by Giampaolo Rodola; bpo-2054 [https://bugs.python.org/issue2054].)

The storbinary() method for binary uploads can now restart uploads thanks to an added rest parameter (patch by Pablo Mouzo; bpo-6845 [https://bugs.python.org/issue6845].)

New class decorator: total_ordering() in the functools module takes a class that defines an __eq__() method and one of __lt__(), __le__(), __gt__(), or __ge__(), and generates the missing comparison methods. Since the __cmp__() method is being deprecated in Python 3.x, this decorator makes it easier to define ordered classes. (Added by Raymond Hettinger; bpo-5479 [https://bugs.python.org/issue5479].)

New function: cmp_to_key() will take an old-style comparison function that expects two arguments and return a new callable that can be used as the key parameter to functions such as sorted(), min() and max(), etc. The primary intended use is to help with making code compatible with Python 3.x. (Added by Raymond Hettinger.)

New function: the gc module’s is_tracked() returns true if a given instance is tracked by the garbage collector, false otherwise. (Contributed by Antoine Pitrou; bpo-4688 [https://bugs.python.org/issue4688].)

The gzip module’s GzipFile now supports the context management protocol, so you can write with gzip.GzipFile(...) as f: (contributed by Hagen Fürstenau; bpo-3860 [https://bugs.python.org/issue3860]), and it now implements the io.BufferedIOBase ABC, so you can wrap it with io.BufferedReader for faster processing (contributed by Nir Aides; bpo-7471 [https://bugs.python.org/issue7471]). It’s also now possible to override the modification time recorded in a gzipped file by providing an optional timestamp to the constructor. (Contributed by Jacques Frechet; bpo-4272 [https://bugs.python.org/issue4272].)

Files in gzip format can be padded with trailing zero bytes; the gzip module will now consume these trailing bytes. (Fixed by Tadek Pietraszek and Brian Curtin; bpo-2846 [https://bugs.python.org/issue2846].)

New attribute: the hashlib module now has an algorithms attribute containing a tuple naming the supported algorithms. In Python 2.7, hashlib.algorithms contains ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'). (Contributed by Carl Chenet; bpo-7418 [https://bugs.python.org/issue7418].)

The default HTTPResponse class used by the httplib module now supports buffering, resulting in much faster reading of HTTP responses. (Contributed by Kristján Valur Jónsson; bpo-4879 [https://bugs.python.org/issue4879].)

The HTTPConnection and HTTPSConnection classes now support a source_address parameter, a (host, port) 2-tuple giving the source address that will be used for the connection. (Contributed by Eldon Ziegler; bpo-3972 [https://bugs.python.org/issue3972].)

The ihooks module now supports relative imports. Note that ihooks is an older module for customizing imports, superseded by the imputil module added in Python 2.0. (Relative import support added by Neil Schemenauer.)

The imaplib module now supports IPv6 addresses. (Contributed by Derek Morr; bpo-1655 [https://bugs.python.org/issue1655].)

New function: the inspect module’s getcallargs() takes a callable and its positional and keyword arguments, and figures out which of the callable’s parameters will receive each argument, returning a dictionary mapping argument names to their values. For example:

>>> from inspect import getcallargs
>>> def f(a, b=1, *pos, **named):
...     pass
>>> getcallargs(f, 1, 2, 3)
{'a': 1, 'b': 2, 'pos': (3,), 'named': {}}
>>> getcallargs(f, a=2, x=4)
{'a': 2, 'b': 1, 'pos': (), 'named': {'x': 4}}
>>> getcallargs(f)
Traceback (most recent call last):
...
TypeError: f() takes at least 1 argument (0 given)

（由 Georg Sakkis 在 bpo-3135 [https://bugs.python.org/issue3135] 中贡献）

Updated module: The io library has been upgraded to the version shipped with Python 3.1. For 3.1, the I/O library was entirely rewritten in C and is 2 to 20 times faster depending on the task being performed. The original Python version was renamed to the _pyio module.

One minor resulting change: the io.TextIOBase class now has an errors attribute giving the error setting used for encoding and decoding errors (one of 'strict', 'replace', 'ignore').

The io.FileIO class now raises an OSError when passed an invalid file descriptor. (Implemented by Benjamin Peterson; bpo-4991 [https://bugs.python.org/issue4991].) The truncate() method now preserves the file position; previously it would change the file position to the end of the new file. (Fixed by Pascal Chambon; bpo-6939 [https://bugs.python.org/issue6939].)

New function: itertools.compress(data, selectors) takes two iterators. Elements of data are returned if the corresponding value in selectors is true:

itertools.compress('ABCDEF', [1,0,1,0,1,1]) =>
  A, C, E, F

New function: itertools.combinations_with_replacement(iter, r) returns all the possible r-length combinations of elements from the iterable iter. Unlike combinations(), individual elements can be repeated in the generated combinations:

itertools.combinations_with_replacement('abc', 2) =>
  ('a', 'a'), ('a', 'b'), ('a', 'c'),
  ('b', 'b'), ('b', 'c'), ('c', 'c')

Note that elements are treated as unique depending on their position in the input, not their actual values.

The itertools.count() function now has a step argument that allows incrementing by values other than 1. count() also now allows keyword arguments, and using non-integer values such as floats or Decimal instances. (Implemented by Raymond Hettinger; bpo-5032 [https://bugs.python.org/issue5032].)

itertools.combinations() and itertools.product() previously raised ValueError for values of r larger than the input iterable. This was deemed a specification error, so they now return an empty iterator. (Fixed by Raymond Hettinger; bpo-4816 [https://bugs.python.org/issue4816].)

Updated module: The json module was upgraded to version 2.0.9 of the simplejson package, which includes a C extension that makes encoding and decoding faster. (Contributed by Bob Ippolito; bpo-4136 [https://bugs.python.org/issue4136].)

To support the new collections.OrderedDict type, json.load() now has an optional object_pairs_hook parameter that will be called with any object literal that decodes to a list of pairs. (Contributed by Raymond Hettinger; bpo-5381 [https://bugs.python.org/issue5381].)

The mailbox module’s Maildir class now records the timestamp on the directories it reads, and only re-reads them if the modification time has subsequently changed. This improves performance by avoiding unneeded directory scans. (Fixed by A.M. Kuchling and Antoine Pitrou; bpo-1607951 [https://bugs.python.org/issue1607951], bpo-6896 [https://bugs.python.org/issue6896].)

New functions: the math module gained erf() and erfc() for the error function and the complementary error function, expm1() which computes e**x - 1 with more precision than using exp() and subtracting 1, gamma() for the Gamma function, and lgamma() for the natural log of the Gamma function. (Contributed by Mark Dickinson and nirinA raseliarison; bpo-3366 [https://bugs.python.org/issue3366].)

The multiprocessing module’s Manager* classes can now be passed a callable that will be called whenever a subprocess is started, along with a set of arguments that will be passed to the callable. (Contributed by lekma; bpo-5585 [https://bugs.python.org/issue5585].)

The Pool class, which controls a pool of worker processes, now has an optional maxtasksperchild parameter. Worker processes will perform the specified number of tasks and then exit, causing the Pool to start a new worker. This is useful if tasks may leak memory or other resources, or if some tasks will cause the worker to become very large. (Contributed by Charles Cazabon; bpo-6963 [https://bugs.python.org/issue6963].)

The nntplib module now supports IPv6 addresses. (Contributed by Derek Morr; bpo-1664 [https://bugs.python.org/issue1664].)

New functions: the os module wraps the following POSIX system calls: getresgid() and getresuid(), which return the real, effective, and saved GIDs and UIDs; setresgid() and setresuid(), which set real, effective, and saved GIDs and UIDs to new values; initgroups(), which initialize the group access list for the current process. (GID/UID functions contributed by Travis H.; bpo-6508 [https://bugs.python.org/issue6508]. Support for initgroups added by Jean-Paul Calderone; bpo-7333 [https://bugs.python.org/issue7333].)

The os.fork() function now re-initializes the import lock in the child process; this fixes problems on Solaris when fork() is called from a thread. (Fixed by Zsolt Cserna; bpo-7242 [https://bugs.python.org/issue7242].)

In the os.path module, the normpath() and abspath() functions now preserve Unicode; if their input path is a Unicode string, the return value is also a Unicode string. (normpath() fixed by Matt Giuca in bpo-5827 [https://bugs.python.org/issue5827]; abspath() fixed by Ezio Melotti in bpo-3426 [https://bugs.python.org/issue3426].)

The pydoc module now has help for the various symbols that Python uses. You can now do help('<<') or help('@'), for example. (Contributed by David Laban; bpo-4739 [https://bugs.python.org/issue4739].)

The re module’s split(), sub(), and subn() now accept an optional flags argument, for consistency with the other functions in the module. (Added by Gregory P. Smith.)

New function: run_path() in the runpy module will execute the code at a provided path argument. path can be the path of a Python source file (example.py), a compiled bytecode file (example.pyc), a directory (./package/), or a zip archive (example.zip). If a directory or zip path is provided, it will be added to the front of sys.path and the module __main__ will be imported. It’s expected that the directory or zip contains a __main__.py; if it doesn’t, some other __main__.py might be imported from a location later in sys.path. This makes more of the machinery of runpy available to scripts that want to mimic the way Python’s command line processes an explicit path name. (Added by Nick Coghlan; bpo-6816 [https://bugs.python.org/issue6816].)

New function: in the shutil module, make_archive() takes a filename, archive type (zip or tar-format), and a directory path, and creates an archive containing the directory’s contents. (Added by Tarek Ziadé.)

shutil’s copyfile() and copytree() functions now raise a SpecialFileError exception when asked to copy a named pipe. Previously the code would treat named pipes like a regular file by opening them for reading, and this would block indefinitely. (Fixed by Antoine Pitrou; bpo-3002 [https://bugs.python.org/issue3002].)

The signal module no longer re-installs the signal handler unless this is truly necessary, which fixes a bug that could make it impossible to catch the EINTR signal robustly. (Fixed by Charles-Francois Natali; bpo-8354 [https://bugs.python.org/issue8354].)

New functions: in the site module, three new functions return various site- and user-specific paths. getsitepackages() returns a list containing all global site-packages directories, getusersitepackages() returns the path of the user’s site-packages directory, and getuserbase() returns the value of the USER_BASE environment variable, giving the path to a directory that can be used to store data. (Contributed by Tarek Ziadé; bpo-6693 [https://bugs.python.org/issue6693].)

The site module now reports exceptions occurring when the sitecustomize module is imported, and will no longer catch and swallow the KeyboardInterrupt exception. (Fixed by Victor Stinner; bpo-3137 [https://bugs.python.org/issue3137].)

The create_connection() function gained a source_address parameter, a (host, port) 2-tuple giving the source address that will be used for the connection. (Contributed by Eldon Ziegler; bpo-3972 [https://bugs.python.org/issue3972].)

The recv_into() and recvfrom_into() methods will now write into objects that support the buffer API, most usefully the bytearray and memoryview objects. (Implemented by Antoine Pitrou; bpo-8104 [https://bugs.python.org/issue8104].)

The SocketServer module’s TCPServer class now supports socket timeouts and disabling the Nagle algorithm. The disable_nagle_algorithm class attribute defaults to False; if overridden to be true, new request connections will have the TCP_NODELAY option set to prevent buffering many small sends into a single TCP packet. The timeout class attribute can hold a timeout in seconds that will be applied to the request socket; if no request is received within that time, handle_timeout() will be called and handle_request() will return. (Contributed by Kristján Valur Jónsson; bpo-6192 [https://bugs.python.org/issue6192] and bpo-6267 [https://bugs.python.org/issue6267].)

Updated module: the sqlite3 module has been updated to version 2.6.0 of the pysqlite package [https://github.com/ghaering/pysqlite]. Version 2.6.0 includes a number of bugfixes, and adds the ability to load SQLite extensions from shared libraries. Call the enable_load_extension(True) method to enable extensions, and then call load_extension() to load a particular shared library. (Updated by Gerhard Häring.)

The ssl module’s SSLSocket objects now support the buffer API, which fixed a test suite failure (fix by Antoine Pitrou; bpo-7133 [https://bugs.python.org/issue7133]) and automatically set OpenSSL’s SSL_MODE_AUTO_RETRY, which will prevent an error code being returned from recv() operations that trigger an SSL renegotiation (fix by Antoine Pitrou; bpo-8222 [https://bugs.python.org/issue8222]).

The ssl.wrap_socket() constructor function now takes a ciphers argument that’s a string listing the encryption algorithms to be allowed; the format of the string is described in the OpenSSL documentation [https://www.openssl.org/docs/manmaster/man1/ciphers.html#CIPHER-LIST-FORMAT]. (Added by Antoine Pitrou; bpo-8322 [https://bugs.python.org/issue8322].)

Another change makes the extension load all of OpenSSL’s ciphers and digest algorithms so that they’re all available. Some SSL certificates couldn’t be verified, reporting an “unknown algorithm” error. (Reported by Beda Kosata, and fixed by Antoine Pitrou; bpo-8484 [https://bugs.python.org/issue8484].)

The version of OpenSSL being used is now available as the module attributes ssl.OPENSSL_VERSION (a string), ssl.OPENSSL_VERSION_INFO (a 5-tuple), and ssl.OPENSSL_VERSION_NUMBER (an integer). (Added by Antoine Pitrou; bpo-8321 [https://bugs.python.org/issue8321].)

The struct module will no longer silently ignore overflow errors when a value is too large for a particular integer format code (one of bBhHiIlLqQ); it now always raises a struct.error exception. (Changed by Mark Dickinson; bpo-1523 [https://bugs.python.org/issue1523].) The pack() function will also attempt to use __index__() to convert and pack non-integers before trying the __int__() method or reporting an error. (Changed by Mark Dickinson; bpo-8300 [https://bugs.python.org/issue8300].)

New function: the subprocess module’s check_output() runs a command with a specified set of arguments and returns the command’s output as a string when the command runs without error, or raises a CalledProcessError exception otherwise.

>>> subprocess.check_output(['df', '-h', '.'])
'Filesystem     Size   Used  Avail Capacity  Mounted on\n
/dev/disk0s2    52G    49G   3.0G    94%    /\n'

>>> subprocess.check_output(['df', '-h', '/bogus'])
  ...
subprocess.CalledProcessError: Command '['df', '-h', '/bogus']' returned non-zero exit status 1

（由 Gregory P. Smith 贡献）

The subprocess module will now retry its internal system calls on receiving an EINTR signal. (Reported by several people; final patch by Gregory P. Smith in bpo-1068268 [https://bugs.python.org/issue1068268].)

New function: is_declared_global() in the symtable module returns true for variables that are explicitly declared to be global, false for ones that are implicitly global. (Contributed by Jeremy Hylton.)

The syslog module will now use the value of sys.argv[0] as the identifier instead of the previous default value of 'python'. (Changed by Sean Reifschneider; bpo-8451 [https://bugs.python.org/issue8451].)

The sys.version_info value is now a named tuple, with attributes named major, minor, micro, releaselevel, and serial. (Contributed by Ross Light; bpo-4285 [https://bugs.python.org/issue4285].)

sys.getwindowsversion() also returns a named tuple, with attributes named major, minor, build, platform, service_pack, service_pack_major, service_pack_minor, suite_mask, and product_type. (Contributed by Brian Curtin; bpo-7766 [https://bugs.python.org/issue7766].)

The tarfile module’s default error handling has changed, to no longer suppress fatal errors. The default error level was previously 0, which meant that errors would only result in a message being written to the debug log, but because the debug log is not activated by default, these errors go unnoticed. The default error level is now 1, which raises an exception if there’s an error. (Changed by Lars Gustäbel; bpo-7357 [https://bugs.python.org/issue7357].)

tarfile now supports filtering the TarInfo objects being added to a tar file. When you call add(), you may supply an optional filter argument that’s a callable. The filter callable will be passed the TarInfo for every file being added, and can modify and return it. If the callable returns None, the file will be excluded from the resulting archive. This is more powerful than the existing exclude argument, which has therefore been deprecated. (Added by Lars Gustäbel; bpo-6856 [https://bugs.python.org/issue6856].) The TarFile class also now supports the context management protocol. (Added by Lars Gustäbel; bpo-7232 [https://bugs.python.org/issue7232].)

The wait() method of the threading.Event class now returns the internal flag on exit. This means the method will usually return true because wait() is supposed to block until the internal flag becomes true. The return value will only be false if a timeout was provided and the operation timed out. (Contributed by Tim Lesher; bpo-1674032 [https://bugs.python.org/issue1674032].)

The Unicode database provided by the unicodedata module is now used internally to determine which characters are numeric, whitespace, or represent line breaks. The database also includes information from the Unihan.txt data file (patch by Anders Chrigström and Amaury Forgeot d’Arc; bpo-1571184 [https://bugs.python.org/issue1571184]) and has been updated to version 5.2.0 (updated by Florent Xicluna; bpo-8024 [https://bugs.python.org/issue8024]).

The urlparse module’s urlsplit() now handles unknown URL schemes in a fashion compliant with RFC 3986 [https://tools.ietf.org/html/rfc3986.html]: if the URL is of the form "<something>://...", the text before the :// is treated as the scheme, even if it’s a made-up scheme that the module doesn’t know about. This change may break code that worked around the old behaviour. For example, Python 2.6.4 or 2.5 will return the following:

>>> import urlparse
>>> urlparse.urlsplit('invented://host/filename?query')
('invented', '', '//host/filename?query', '', '')

Python 2.7 (and Python 2.6.5) will return:

>>> import urlparse
>>> urlparse.urlsplit('invented://host/filename?query')
('invented', 'host', '/filename?query', '', '')

(Python 2.7 actually produces slightly different output, since it returns a named tuple instead of a standard tuple.)

The urlparse module also supports IPv6 literal addresses as defined by RFC 2732 [https://tools.ietf.org/html/rfc2732.html] (contributed by Senthil Kumaran; bpo-2987 [https://bugs.python.org/issue2987]).

>>> urlparse.urlparse('http://[1080::8:800:200C:417A]/foo')
ParseResult(scheme='http', netloc='[1080::8:800:200C:417A]',
            path='/foo', params='', query='', fragment='')

New class: the WeakSet class in the weakref module is a set that only holds weak references to its elements; elements will be removed once there are no references pointing to them. (Originally implemented in Python 3.x by Raymond Hettinger, and backported to 2.7 by Michael Foord.)

The ElementTree library, xml.etree, no longer escapes ampersands and angle brackets when outputting an XML processing instruction (which looks like <?xml-stylesheet href="#style1"?>) or comment (which looks like <!-- comment -->). (Patch by Neil Muller; bpo-2746 [https://bugs.python.org/issue2746].)

The XML-RPC client and server, provided by the xmlrpclib and SimpleXMLRPCServer modules, have improved performance by supporting HTTP/1.1 keep-alive and by optionally using gzip encoding to compress the XML being exchanged. The gzip compression is controlled by the encode_threshold attribute of SimpleXMLRPCRequestHandler, which contains a size in bytes; responses larger than this will be compressed. (Contributed by Kristján Valur Jónsson; bpo-6267 [https://bugs.python.org/issue6267].)

The zipfile module’s ZipFile now supports the context management protocol, so you can write with zipfile.ZipFile(...) as f:. (Contributed by Brian Curtin; bpo-5511 [https://bugs.python.org/issue5511].)

zipfile now also supports archiving empty directories and extracts them correctly. (Fixed by Kuba Wieczorek; bpo-4710 [https://bugs.python.org/issue4710].) Reading files out of an archive is faster, and interleaving read() and readline() now works correctly. (Contributed by Nir Aides; bpo-7610 [https://bugs.python.org/issue7610].)

The is_zipfile() function now accepts a file object, in addition to the path names accepted in earlier versions. (Contributed by Gabriel Genellina; bpo-4756 [https://bugs.python.org/issue4756].)

The writestr() method now has an optional compress_type parameter that lets you override the default compression method specified in the ZipFile constructor. (Contributed by Ronald Oussoren; bpo-6003 [https://bugs.python.org/issue6003].)

新模块：importlib

Python 3.1 includes the importlib package, a re-implementation of the logic underlying Python’s import statement. importlib is useful for implementors of Python interpreters and to users who wish to write new importers that can participate in the import process. Python 2.7 doesn’t contain the complete importlib package, but instead has a tiny subset that contains a single function, import_module().

import_module(name, package=None) imports a module. name is a string containing the module or package’s name. It’s possible to do relative imports by providing a string that begins with a . character, such as ..utils.errors. For relative imports, the package argument must be provided and is the name of the package that will be used as the anchor for the relative import. import_module() both inserts the imported module into sys.modules and returns the module object.

这是一些例子:

>>> from importlib import import_module
>>> anydbm = import_module('anydbm')  # Standard absolute import
>>> anydbm
<module 'anydbm' from '/p/python/Lib/anydbm.py'>
>>> # Relative import
>>> file_util = import_module('..file_util', 'distutils.command')
>>> file_util
<module 'distutils.file_util' from '/python/Lib/distutils/file_util.pyc'>

importlib was implemented by Brett Cannon and introduced in Python 3.1.

新模块：sysconfig

The sysconfig module has been pulled out of the Distutils package, becoming a new top-level module in its own right. sysconfig provides functions for getting information about Python’s build process: compiler switches, installation paths, the platform name, and whether Python is running from its source directory.

Some of the functions in the module are:

get_config_var() returns variables from Python’s Makefile and the pyconfig.h file.

get_config_vars() returns a dictionary containing all of the configuration variables.

get_path() returns the configured path for a particular type of module: the standard library, site-specific modules, platform-specific modules, etc.

is_python_build() returns true if you’re running a binary from a Python source tree, and false otherwise.

Consult the sysconfig documentation for more details and for a complete list of functions.

The Distutils package and sysconfig are now maintained by Tarek Ziadé, who has also started a Distutils2 package (source repository at https://hg.python.org/distutils2/) for developing a next-generation version of Distutils.

ttk: Themed Widgets for Tk

Tcl/Tk 8.5 includes a set of themed widgets that re-implement basic Tk widgets but have a more customizable appearance and can therefore more closely resemble the native platform’s widgets. This widget set was originally called Tile, but was renamed to Ttk (for “themed Tk”) on being added to Tcl/Tck release 8.5.

To learn more, read the ttk module documentation. You may also wish to read the Tcl/Tk manual page describing the Ttk theme engine, available at https://www.tcl.tk/man/tcl8.5/TkCmd/ttk_intro.htm. Some screenshots of the Python/Ttk code in use are at https://code.google.com/archive/p/python-ttk/wikis/Screenshots.wiki.

The ttk module was written by Guilherme Polo and added in bpo-2983 [https://bugs.python.org/issue2983]. An alternate version called Tile.py, written by Martin Franklin and maintained by Kevin Walzer, was proposed for inclusion in bpo-2618 [https://bugs.python.org/issue2618], but the authors argued that Guilherme Polo’s work was more comprehensive.

更新的模块：unittest

The unittest module was greatly enhanced; many new features were added. Most of these features were implemented by Michael Foord, unless otherwise noted. The enhanced version of the module is downloadable separately for use with Python versions 2.4 to 2.6, packaged as the unittest2 package, from https://pypi.org/project/unittest2.

When used from the command line, the module can automatically discover tests. It’s not as fancy as py.test [http://pytest.org] or nose [https://nose.readthedocs.io/], but provides a simple way to run tests kept within a set of package directories. For example, the following command will search the test/ subdirectory for any importable test files named test*.py:

python -m unittest discover -s test

Consult the unittest module documentation for more details. (Developed in bpo-6001 [https://bugs.python.org/issue6001].)

The main() function supports some other new options:

-b or --buffer will buffer the standard output and standard error streams during each test. If the test passes, any resulting output will be discarded; on failure, the buffered output will be displayed.

-c or --catch will cause the control-C interrupt to be handled more gracefully. Instead of interrupting the test process immediately, the currently running test will be completed and then the partial results up to the interruption will be reported. If you’re impatient, a second press of control-C will cause an immediate interruption.

This control-C handler tries to avoid causing problems when the code being tested or the tests being run have defined a signal handler of their own, by noticing that a signal handler was already set and calling it. If this doesn’t work for you, there’s a removeHandler() decorator that can be used to mark tests that should have the control-C handling disabled.

-f or --failfast makes test execution stop immediately when a test fails instead of continuing to execute further tests. (Suggested by Cliff Dyer and implemented by Michael Foord; bpo-8074 [https://bugs.python.org/issue8074].)

The progress messages now show ‘x’ for expected failures and ‘u’ for unexpected successes when run in verbose mode. (Contributed by Benjamin Peterson.)

Test cases can raise the SkipTest exception to skip a test (bpo-1034053 [https://bugs.python.org/issue1034053]).

The error messages for assertEqual(), assertTrue(), and assertFalse() failures now provide more information. If you set the longMessage attribute of your TestCase classes to true, both the standard error message and any additional message you provide will be printed for failures. (Added by Michael Foord; bpo-5663 [https://bugs.python.org/issue5663].)

The assertRaises() method now returns a context handler when called without providing a callable object to run. For example, you can write this:

with self.assertRaises(KeyError):
    {}['foo']

(Implemented by Antoine Pitrou; bpo-4444 [https://bugs.python.org/issue4444].)

Module- and class-level setup and teardown fixtures are now supported. Modules can contain setUpModule() and tearDownModule() functions. Classes can have setUpClass() and tearDownClass() methods that must be defined as class methods (using @classmethod or equivalent). These functions and methods are invoked when the test runner switches to a test case in a different module or class.

The methods addCleanup() and doCleanups() were added. addCleanup() lets you add cleanup functions that will be called unconditionally (after setUp() if setUp() fails, otherwise after tearDown()). This allows for much simpler resource allocation and deallocation during tests (bpo-5679 [https://bugs.python.org/issue5679]).

A number of new methods were added that provide more specialized tests. Many of these methods were written by Google engineers for use in their test suites; Gregory P. Smith, Michael Foord, and GvR worked on merging them into Python’s version of unittest.

assertIsNone() and assertIsNotNone() take one expression and verify that the result is or is not None.

assertIs() and assertIsNot() take two values and check whether the two values evaluate to the same object or not. (Added by Michael Foord; bpo-2578 [https://bugs.python.org/issue2578].)

assertIsInstance() and assertNotIsInstance() check whether the resulting object is an instance of a particular class, or of one of a tuple of classes. (Added by Georg Brandl; bpo-7031 [https://bugs.python.org/issue7031].)

assertGreater(), assertGreaterEqual(), assertLess(), and assertLessEqual() compare two quantities.

assertMultiLineEqual() compares two strings, and if they’re not equal, displays a helpful comparison that highlights the differences in the two strings. This comparison is now used by default when Unicode strings are compared with assertEqual().

assertRegexpMatches() and assertNotRegexpMatches() checks whether the first argument is a string matching or not matching the regular expression provided as the second argument (bpo-8038 [https://bugs.python.org/issue8038]).

assertRaisesRegexp() checks whether a particular exception is raised, and then also checks that the string representation of the exception matches the provided regular expression.

assertIn() and assertNotIn() tests whether first is or is not in second.

assertItemsEqual() tests whether two provided sequences contain the same elements.

assertSetEqual() compares whether two sets are equal, and only reports the differences between the sets in case of error.

Similarly, assertListEqual() and assertTupleEqual() compare the specified types and explain any differences without necessarily printing their full values; these methods are now used by default when comparing lists and tuples using assertEqual(). More generally, assertSequenceEqual() compares two sequences and can optionally check whether both sequences are of a particular type.

assertDictEqual() compares two dictionaries and reports the differences; it’s now used by default when you compare two dictionaries using assertEqual(). assertDictContainsSubset() checks whether all of the key/value pairs in first are found in second.

assertAlmostEqual() and assertNotAlmostEqual() test whether first and second are approximately equal. This method can either round their difference to an optionally-specified number of places (the default is 7) and compare it to zero, or require the difference to be smaller than a supplied delta value.

loadTestsFromName() properly honors the suiteClass attribute of the TestLoader. (Fixed by Mark Roddy; bpo-6866 [https://bugs.python.org/issue6866].)

A new hook lets you extend the assertEqual() method to handle new data types. The addTypeEqualityFunc() method takes a type object and a function. The function will be used when both of the objects being compared are of the specified type. This function should compare the two objects and raise an exception if they don’t match; it’s a good idea for the function to provide additional information about why the two objects aren’t matching, much as the new sequence comparison methods do.

unittest.main() now takes an optional exit argument. If false, main() doesn’t call sys.exit(), allowing main() to be used from the interactive interpreter. (Contributed by J. Pablo Fernández; bpo-3379 [https://bugs.python.org/issue3379].)

TestResult has new startTestRun() and stopTestRun() methods that are called immediately before and after a test run. (Contributed by Robert Collins; bpo-5728 [https://bugs.python.org/issue5728].)

With all these changes, the unittest.py was becoming awkwardly large, so the module was turned into a package and the code split into several files (by Benjamin Peterson). This doesn’t affect how the module is imported or used.

参见

http://www.voidspace.org.uk/python/articles/unittest2.shtml

Describes the new features, how to use them, and the rationale for various design decisions. (By Michael Foord.)

更新的模块：ElementTree 1.3

The version of the ElementTree library included with Python was updated to version 1.3. Some of the new features are:

The various parsing functions now take a parser keyword argument giving an XMLParser instance that will be used. This makes it possible to override the file’s internal encoding:

p = ET.XMLParser(encoding='utf-8')
t = ET.XML("""<root/>""", parser=p)

Errors in parsing XML now raise a ParseError exception, whose instances have a position attribute containing a (line, column) tuple giving the location of the problem.

ElementTree’s code for converting trees to a string has been significantly reworked, making it roughly twice as fast in many cases. The ElementTree.write() and Element.write() methods now have a method parameter that can be “xml” (the default), “html”, or “text”. HTML mode will output empty elements as <empty></empty> instead of <empty/>, and text mode will skip over elements and only output the text chunks. If you set the tag attribute of an element to None but leave its children in place, the element will be omitted when the tree is written out, so you don’t need to do more extensive rearrangement to remove a single element.

Namespace handling has also been improved. All xmlns:<whatever> declarations are now output on the root element, not scattered throughout the resulting XML. You can set the default namespace for a tree by setting the default_namespace attribute and can register new prefixes with register_namespace(). In XML mode, you can use the true/false xml_declaration parameter to suppress the XML declaration.

New Element method: extend() appends the items from a sequence to the element’s children. Elements themselves behave like sequences, so it’s easy to move children from one element to another:

from xml.etree import ElementTree as ET

t = ET.XML("""<list>
  <item>1</item> <item>2</item>  <item>3</item>
</list>""")
new = ET.XML('<root/>')
new.extend(t)

# Outputs <root><item>1</item>...</root>
print ET.tostring(new)

New Element method: iter() yields the children of the element as a generator. It’s also possible to write for child in elem: to loop over an element’s children. The existing method getiterator() is now deprecated, as is getchildren() which constructs and returns a list of children.

New Element method: itertext() yields all chunks of text that are descendants of the element. For example:

t = ET.XML("""<list>
  <item>1</item> <item>2</item>  <item>3</item>
</list>""")

# Outputs ['\n  ', '1', ' ', '2', '  ', '3', '\n']
print list(t.itertext())

Deprecated: using an element as a Boolean (i.e., if elem:) would return true if the element had any children, or false if there were no children. This behaviour is confusing – None is false, but so is a childless element? – so it will now trigger a FutureWarning. In your code, you should be explicit: write len(elem) != 0 if you’re interested in the number of children, or elem is not None.

Fredrik Lundh develops ElementTree and produced the 1.3 version; you can read his article describing 1.3 at http://effbot.org/zone/elementtree-13-intro.htm. Florent Xicluna updated the version included with Python, after discussions on python-dev and in bpo-6472 [https://bugs.python.org/issue6472].)

构建和 C API 的改变

Changes to Python’s build process and to the C API include:

The latest release of the GNU Debugger, GDB 7, can be scripted using Python [https://sourceware.org/gdb/current/onlinedocs/gdb/Python.html]. When you begin debugging an executable program P, GDB will look for a file named P-gdb.py and automatically read it. Dave Malcolm contributed a python-gdb.py that adds a number of commands useful when debugging Python itself. For example, py-up and py-down go up or down one Python stack frame, which usually corresponds to several C stack frames. py-print prints the value of a Python variable, and py-bt prints the Python stack trace. (Added as a result of bpo-8032 [https://bugs.python.org/issue8032].)

If you use the .gdbinit file provided with Python, the “pyo” macro in the 2.7 version now works correctly when the thread being debugged doesn’t hold the GIL; the macro now acquires it before printing. (Contributed by Victor Stinner; bpo-3632 [https://bugs.python.org/issue3632].)

Py_AddPendingCall() is now thread-safe, letting any worker thread submit notifications to the main Python thread. This is particularly useful for asynchronous IO operations. (Contributed by Kristján Valur Jónsson; bpo-4293 [https://bugs.python.org/issue4293].)

New function: PyCode_NewEmpty() creates an empty code object; only the filename, function name, and first line number are required. This is useful for extension modules that are attempting to construct a more useful traceback stack. Previously such extensions needed to call PyCode_New(), which had many more arguments. (Added by Jeffrey Yasskin.)

New function: PyErr_NewExceptionWithDoc() creates a new exception class, just as the existing PyErr_NewException() does, but takes an extra char * argument containing the docstring for the new exception class. (Added by ‘lekma’ on the Python bug tracker; bpo-7033 [https://bugs.python.org/issue7033].)

New function: PyFrame_GetLineNumber() takes a frame object and returns the line number that the frame is currently executing. Previously code would need to get the index of the bytecode instruction currently executing, and then look up the line number corresponding to that address. (Added by Jeffrey Yasskin.)

New functions: PyLong_AsLongAndOverflow() and PyLong_AsLongLongAndOverflow() approximates a Python long integer as a C long or long long. If the number is too large to fit into the output type, an overflow flag is set and returned to the caller. (Contributed by Case Van Horsen; bpo-7528 [https://bugs.python.org/issue7528] and bpo-7767 [https://bugs.python.org/issue7767].)

New function: stemming from the rewrite of string-to-float conversion, a new PyOS_string_to_double() function was added. The old PyOS_ascii_strtod() and PyOS_ascii_atof() functions are now deprecated.

New function: PySys_SetArgvEx() sets the value of sys.argv and can optionally update sys.path to include the directory containing the script named by sys.argv[0] depending on the value of an updatepath parameter.

This function was added to close a security hole for applications that embed Python. The old function, PySys_SetArgv(), would always update sys.path, and sometimes it would add the current directory. This meant that, if you ran an application embedding Python in a directory controlled by someone else, attackers could put a Trojan-horse module in the directory (say, a file named os.py) that your application would then import and run.

If you maintain a C/C++ application that embeds Python, check whether you’re calling PySys_SetArgv() and carefully consider whether the application should be using PySys_SetArgvEx() with updatepath set to false.

Security issue reported as CVE-2008-5983 [https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983]; discussed in bpo-5753 [https://bugs.python.org/issue5753], and fixed by Antoine Pitrou.

New macros: the Python header files now define the following macros: Py_ISALNUM, Py_ISALPHA, Py_ISDIGIT, Py_ISLOWER, Py_ISSPACE, Py_ISUPPER, Py_ISXDIGIT, Py_TOLOWER, and Py_TOUPPER. All of these functions are analogous to the C standard macros for classifying characters, but ignore the current locale setting, because in several places Python needs to analyze characters in a locale-independent way. (Added by Eric Smith; bpo-5793 [https://bugs.python.org/issue5793].)

Removed function: PyEval_CallObject is now only available as a macro. A function version was being kept around to preserve ABI linking compatibility, but that was in 1997; it can certainly be deleted by now. (Removed by Antoine Pitrou; bpo-8276 [https://bugs.python.org/issue8276].)

New format codes: the PyFormat_FromString(), PyFormat_FromStringV(), and PyErr_Format() functions now accept %lld and %llu format codes for displaying C’s long long types. (Contributed by Mark Dickinson; bpo-7228 [https://bugs.python.org/issue7228].)

The complicated interaction between threads and process forking has been changed. Previously, the child process created by os.fork() might fail because the child is created with only a single thread running, the thread performing the os.fork(). If other threads were holding a lock, such as Python’s import lock, when the fork was performed, the lock would still be marked as “held” in the new process. But in the child process nothing would ever release the lock, since the other threads weren’t replicated, and the child process would no longer be able to perform imports.

Python 2.7 acquires the import lock before performing an os.fork(), and will also clean up any locks created using the threading module. C extension modules that have internal locks, or that call fork() themselves, will not benefit from this clean-up.

(Fixed by Thomas Wouters; bpo-1590864 [https://bugs.python.org/issue1590864].)

The Py_Finalize() function now calls the internal threading._shutdown() function; this prevents some exceptions from being raised when an interpreter shuts down. (Patch by Adam Olsen; bpo-1722344 [https://bugs.python.org/issue1722344].)

When using the PyMemberDef structure to define attributes of a type, Python will no longer let you try to delete or set a T_STRING_INPLACE attribute.

Global symbols defined by the ctypes module are now prefixed with Py, or with _ctypes. (Implemented by Thomas Heller; bpo-3102 [https://bugs.python.org/issue3102].)

New configure option: the --with-system-expat switch allows building the pyexpat module to use the system Expat library. (Contributed by Arfrever Frehtes Taifersar Arahesis; bpo-7609 [https://bugs.python.org/issue7609].)

New configure option: the --with-valgrind option will now disable the pymalloc allocator, which is difficult for the Valgrind memory-error detector to analyze correctly. Valgrind will therefore be better at detecting memory leaks and overruns. (Contributed by James Henstridge; bpo-2422 [https://bugs.python.org/issue2422].)

New configure option: you can now supply an empty string to --with-dbmliborder= in order to disable all of the various DBM modules. (Added by Arfrever Frehtes Taifersar Arahesis; bpo-6491 [https://bugs.python.org/issue6491].)

The configure script now checks for floating-point rounding bugs on certain 32-bit Intel chips and defines a X87_DOUBLE_ROUNDING preprocessor definition. No code currently uses this definition, but it’s available if anyone wishes to use it. (Added by Mark Dickinson; bpo-2937 [https://bugs.python.org/issue2937].)

configure also now sets a LDCXXSHARED Makefile variable for supporting C++ linking. (Contributed by Arfrever Frehtes Taifersar Arahesis; bpo-1222585 [https://bugs.python.org/issue1222585].)

The build process now creates the necessary files for pkg-config support. (Contributed by Clinton Roy; bpo-3585 [https://bugs.python.org/issue3585].)

The build process now supports Subversion 1.7. (Contributed by Arfrever Frehtes Taifersar Arahesis; bpo-6094 [https://bugs.python.org/issue6094].)

胶囊

Python 3.1 adds a new C datatype, PyCapsule, for providing a C API to an extension module. A capsule is essentially the holder of a C void * pointer, and is made available as a module attribute; for example, the socket module’s API is exposed as socket.CAPI, and unicodedata exposes ucnhash_CAPI. Other extensions can import the module, access its dictionary to get the capsule object, and then get the void * pointer, which will usually point to an array of pointers to the module’s various API functions.

There is an existing data type already used for this, PyCObject, but it doesn’t provide type safety. Evil code written in pure Python could cause a segmentation fault by taking a PyCObject from module A and somehow substituting it for the PyCObject in module B. Capsules know their own name, and getting the pointer requires providing the name:

void *vtable;

if (!PyCapsule_IsValid(capsule, "mymodule.CAPI") {
        PyErr_SetString(PyExc_ValueError, "argument type invalid");
        return NULL;
}

vtable = PyCapsule_GetPointer(capsule, "mymodule.CAPI");

You are assured that vtable points to whatever you’re expecting. If a different capsule was passed in, PyCapsule_IsValid() would detect the mismatched name and return false. Refer to 给扩展模块提供C API for more information on using these objects.

Python 2.7 now uses capsules internally to provide various extension-module APIs, but the PyCObject_AsVoidPtr() was modified to handle capsules, preserving compile-time compatibility with the CObject interface. Use of PyCObject_AsVoidPtr() will signal a PendingDeprecationWarning, which is silent by default.

Implemented in Python 3.1 and backported to 2.7 by Larry Hastings; discussed in bpo-5630 [https://bugs.python.org/issue5630].

特定于端口的更改：Windows

The msvcrt module now contains some constants from the crtassem.h header file: CRT_ASSEMBLY_VERSION, VC_ASSEMBLY_PUBLICKEYTOKEN, and LIBRARIES_ASSEMBLY_NAME_PREFIX. (Contributed by David Cournapeau; bpo-4365 [https://bugs.python.org/issue4365].)

The _winreg module for accessing the registry now implements the CreateKeyEx() and DeleteKeyEx() functions, extended versions of previously-supported functions that take several extra arguments. The DisableReflectionKey(), EnableReflectionKey(), and QueryReflectionKey() were also tested and documented. (Implemented by Brian Curtin: bpo-7347 [https://bugs.python.org/issue7347].)

The new _beginthreadex() API is used to start threads, and the native thread-local storage functions are now used. (Contributed by Kristján Valur Jónsson; bpo-3582 [https://bugs.python.org/issue3582].)

The os.kill() function now works on Windows. The signal value can be the constants CTRL_C_EVENT, CTRL_BREAK_EVENT, or any integer. The first two constants will send Control-C and Control-Break keystroke events to subprocesses; any other value will use the TerminateProcess() API. (Contributed by Miki Tebeka; bpo-1220212 [https://bugs.python.org/issue1220212].)

The os.listdir() function now correctly fails for an empty path. (Fixed by Hirokazu Yamamoto; bpo-5913 [https://bugs.python.org/issue5913].)

The mimelib module will now read the MIME database from the Windows registry when initializing. (Patch by Gabriel Genellina; bpo-4969 [https://bugs.python.org/issue4969].)

特定于端口的更改：Mac OS X

The path /Library/Python/2.7/site-packages is now appended to sys.path, in order to share added packages between the system installation and a user-installed copy of the same version. (Changed by Ronald Oussoren; bpo-4865 [https://bugs.python.org/issue4865].)

在 2.7.13 版更改: As of 2.7.13, this change was removed. /Library/Python/2.7/site-packages, the site-packages directory used by the Apple-supplied system Python 2.7 is no longer appended to sys.path for user-installed Pythons such as from the python.org installers. As of macOS 10.12, Apple changed how the system site-packages directory is configured, which could cause installation of pip components, like setuptools, to fail. Packages installed for the system Python will no longer be shared with user-installed Pythons. (bpo-28440 [https://bugs.python.org/issue28440])

特定于 FreeBSD 的更改：

FreeBSD 7.1’s SO_SETFIB constant, used with getsockopt()/setsockopt() to select an alternate routing table, is now available in the socket module. (Added by Kyle VanderBeek; bpo-8235 [https://bugs.python.org/issue8235].)

Other Changes and Fixes

Two benchmark scripts, iobench and ccbench, were added to the Tools directory. iobench measures the speed of the built-in file I/O objects returned by open() while performing various operations, and ccbench is a concurrency benchmark that tries to measure computing throughput, thread switching latency, and IO processing bandwidth when performing several tasks using a varying number of threads.

The Tools/i18n/msgfmt.py script now understands plural forms in .po files. (Fixed by Martin von Löwis; bpo-5464 [https://bugs.python.org/issue5464].)

When importing a module from a .pyc or .pyo file with an existing .py counterpart, the co_filename attributes of the resulting code objects are overwritten when the original filename is obsolete. This can happen if the file has been renamed, moved, or is accessed through different paths. (Patch by Ziga Seilnacht and Jean-Paul Calderone; bpo-1180193 [https://bugs.python.org/issue1180193].)

The regrtest.py script now takes a --randseed= switch that takes an integer that will be used as the random seed for the -r option that executes tests in random order. The -r option also reports the seed that was used (Added by Collin Winter.)

Another regrtest.py switch is -j, which takes an integer specifying how many tests run in parallel. This allows reducing the total runtime on multi-core machines. This option is compatible with several other options, including the -R switch which is known to produce long runtimes. (Added by Antoine Pitrou, bpo-6152 [https://bugs.python.org/issue6152].) This can also be used with a new -F switch that runs selected tests in a loop until they fail. (Added by Antoine Pitrou; bpo-7312 [https://bugs.python.org/issue7312].)

When executed as a script, the py_compile.py module now accepts '-' as an argument, which will read standard input for the list of filenames to be compiled. (Contributed by Piotr Ożarowski; bpo-8233 [https://bugs.python.org/issue8233].)

移植到 Python 2.7

This section lists previously described changes and other bugfixes that may require changes to your code:

The range() function processes its arguments more consistently; it will now call __int__() on non-float, non-integer arguments that are supplied to it. (Fixed by Alexander Belopolsky; bpo-1533 [https://bugs.python.org/issue1533].)

The string format() method changed the default precision used for floating-point and complex numbers from 6 decimal places to 12, which matches the precision used by str(). (Changed by Eric Smith; bpo-5920 [https://bugs.python.org/issue5920].)

Because of an optimization for the with statement, the special methods __enter__() and __exit__() must belong to the object’s type, and cannot be directly attached to the object’s instance. This affects new-style classes (derived from object) and C extension types. (bpo-6101 [https://bugs.python.org/issue6101].)

Due to a bug in Python 2.6, the exc_value parameter to __exit__() methods was often the string representation of the exception, not an instance. This was fixed in 2.7, so exc_value will be an instance as expected. (Fixed by Florent Xicluna; bpo-7853 [https://bugs.python.org/issue7853].)

When a restricted set of attributes were set using __slots__, deleting an unset attribute would not raise AttributeError as you would expect. Fixed by Benjamin Peterson; bpo-7604 [https://bugs.python.org/issue7604].)

In the standard library:

Operations with datetime instances that resulted in a year falling outside the supported range didn’t always raise OverflowError. Such errors are now checked more carefully and will now raise the exception. (Reported by Mark Leander, patch by Anand B. Pillai and Alexander Belopolsky; bpo-7150 [https://bugs.python.org/issue7150].)

When using Decimal instances with a string’s format() method, the default alignment was previously left-alignment. This has been changed to right-alignment, which might change the output of your programs. (Changed by Mark Dickinson; bpo-6857 [https://bugs.python.org/issue6857].)

Comparisons involving a signaling NaN value (or sNAN) now signal InvalidOperation instead of silently returning a true or false value depending on the comparison operator. Quiet NaN values (or NaN) are now hashable. (Fixed by Mark Dickinson; bpo-7279 [https://bugs.python.org/issue7279].)

The ElementTree library, xml.etree, no longer escapes ampersands and angle brackets when outputting an XML processing instruction (which looks like <?xml-stylesheet href=”#style1”?>) or comment (which looks like <!– comment –>). (Patch by Neil Muller; bpo-2746 [https://bugs.python.org/issue2746].)

The readline() method of StringIO objects now does nothing when a negative length is requested, as other file-like objects do. (bpo-7348 [https://bugs.python.org/issue7348]).

The syslog module will now use the value of sys.argv[0] as the identifier instead of the previous default value of 'python'. (Changed by Sean Reifschneider; bpo-8451 [https://bugs.python.org/issue8451].)

The tarfile module’s default error handling has changed, to no longer suppress fatal errors. The default error level was previously 0, which meant that errors would only result in a message being written to the debug log, but because the debug log is not activated by default, these errors go unnoticed. The default error level is now 1, which raises an exception if there’s an error. (Changed by Lars Gustäbel; bpo-7357 [https://bugs.python.org/issue7357].)

The urlparse module’s urlsplit() now handles unknown URL schemes in a fashion compliant with RFC 3986 [https://tools.ietf.org/html/rfc3986.html]: if the URL is of the form "<something>://...", the text before the :// is treated as the scheme, even if it’s a made-up scheme that the module doesn’t know about. This change may break code that worked around the old behaviour. For example, Python 2.6.4 or 2.5 will return the following:

>>> import urlparse
>>> urlparse.urlsplit('invented://host/filename?query')
('invented', '', '//host/filename?query', '', '')

Python 2.7 (and Python 2.6.5) will return:

>>> import urlparse
>>> urlparse.urlsplit('invented://host/filename?query')
('invented', 'host', '/filename?query', '', '')

(Python 2.7 actually produces slightly different output, since it returns a named tuple instead of a standard tuple.)

对于C扩展：

C extensions that use integer format codes with the PyArg_Parse* family of functions will now raise a TypeError exception instead of triggering a DeprecationWarning (bpo-5080 [https://bugs.python.org/issue5080]).

Use the new PyOS_string_to_double() function instead of the old PyOS_ascii_strtod() and PyOS_ascii_atof() functions, which are now deprecated.

对于嵌入Python的应用程序：

The PySys_SetArgvEx() function was added, letting applications close a security hole when the existing PySys_SetArgv() function was used. Check whether you’re calling PySys_SetArgv() and carefully consider whether the application should be using PySys_SetArgvEx() with updatepath set to false.

New Features Added to Python 2.7 Maintenance Releases

New features may be added to Python 2.7 maintenance releases when the situation genuinely calls for it. Any such additions must go through the Python Enhancement Proposal process, and make a compelling case for why they can’t be adequately addressed by either adding the new feature solely to Python 3, or else by publishing it on the Python Package Index.

In addition to the specific proposals listed below, there is a general exemption allowing new -3 warnings to be added in any Python 2.7 maintenance release.

Two new environment variables for debug mode

In debug mode, the [xxx refs] statistic is not written by default, the PYTHONSHOWREFCOUNT environment variable now must also be set. (Contributed by Victor Stinner; bpo-31733 [https://bugs.python.org/issue31733].)

When Python is compiled with COUNT_ALLOC defined, allocation counts are no longer dumped by default anymore: the PYTHONSHOWALLOCCOUNT environment variable must now also be set. Moreover, allocation counts are now dumped into stderr, rather than stdout. (Contributed by Victor Stinner; bpo-31692 [https://bugs.python.org/issue31692].)

2.7.15 新版功能.

PEP 434: IDLE Enhancement Exception for All Branches

PEP 434 [https://www.python.org/dev/peps/pep-0434] describes a general exemption for changes made to the IDLE development environment shipped along with Python. This exemption makes it possible for the IDLE developers to provide a more consistent user experience across all supported versions of Python 2 and 3.

For details of any IDLE changes, refer to the NEWS file for the specific release.

PEP 466: Network Security Enhancements for Python 2.7

PEP 466 [https://www.python.org/dev/peps/pep-0466] describes a number of network security enhancement proposals that have been approved for inclusion in Python 2.7 maintenance releases, with the first of those changes appearing in the Python 2.7.7 release.

PEP 466 [https://www.python.org/dev/peps/pep-0466] Python 2.7.7 中添加的相关功能：

hmac.compare_digest() was backported from Python 3 to make a timing attack resistant comparison operation available to Python 2 applications. (Contributed by Alex Gaynor; bpo-21306 [https://bugs.python.org/issue21306].)

OpenSSL 1.0.1g was upgraded in the official Windows installers published on python.org. (Contributed by Zachary Ware; bpo-21462 [https://bugs.python.org/issue21462].)

PEP 466 [https://www.python.org/dev/peps/pep-0466] Python 2.7.8 中添加的相关功能：

hashlib.pbkdf2_hmac() was backported from Python 3 to make a hashing algorithm suitable for secure password storage broadly available to Python 2 applications. (Contributed by Alex Gaynor; bpo-21304 [https://bugs.python.org/issue21304].)

OpenSSL 1.0.1h was upgraded for the official Windows installers published on python.org. (contributed by Zachary Ware in bpo-21671 [https://bugs.python.org/issue21671] for CVE-2014-0224)

PEP 466 [https://www.python.org/dev/peps/pep-0466] Python 2.7.9 中添加的相关功能：

Most of Python 3.4’s ssl module was backported. This means ssl now supports Server Name Indication, TLS1.x settings, access to the platform certificate store, the SSLContext class, and other features. (Contributed by Alex Gaynor and David Reid; bpo-21308 [https://bugs.python.org/issue21308].)

Refer to the “Version added: 2.7.9” notes in the module documentation for specific details.

os.urandom() was changed to cache a file descriptor to /dev/urandom instead of reopening /dev/urandom on every call. (Contributed by Alex Gaynor; bpo-21305 [https://bugs.python.org/issue21305].)

hashlib.algorithms_guaranteed and hashlib.algorithms_available were backported from Python 3 to make it easier for Python 2 applications to select the strongest available hash algorithm. (Contributed by Alex Gaynor in bpo-21307 [https://bugs.python.org/issue21307])

PEP 477: Backport ensurepip (PEP 453) to Python 2.7

PEP 477 [https://www.python.org/dev/peps/pep-0477] approves the inclusion of the PEP 453 [https://www.python.org/dev/peps/pep-0453] ensurepip module and the improved documentation that was enabled by it in the Python 2.7 maintenance releases, appearing first in the Python 2.7.9 release.

Bootstrapping pip By Default

The new ensurepip module (defined in PEP 453 [https://www.python.org/dev/peps/pep-0453]) provides a standard cross-platform mechanism to bootstrap the pip installer into Python installations. The version of pip included with Python 2.7.9 is pip 1.5.6, and future 2.7.x maintenance releases will update the bundled version to the latest version of pip that is available at the time of creating the release candidate.

By default, the commands pip, pipX and pipX.Y will be installed on all platforms (where X.Y stands for the version of the Python installation), along with the pip Python package and its dependencies.

For CPython source builds on POSIX systems, the make install and make altinstall commands do not bootstrap pip by default. This behaviour can be controlled through configure options, and overridden through Makefile options.

On Windows and Mac OS X, the CPython installers now default to installing pip along with CPython itself (users may opt out of installing it during the installation process). Window users will need to opt in to the automatic PATH modifications to have pip available from the command line by default, otherwise it can still be accessed through the Python launcher for Windows as py -m pip.

As discussed in the PEP [https://www.python.org/dev/peps/pep-0477/#disabling-ensurepip-by-downstream-distributors], platform packagers may choose not to install these commands by default, as long as, when invoked, they provide clear and simple directions on how to install them on that platform (usually using the system package manager).

文档更改

As part of this change, the 安装 Python 模块 and 分发 Python 模块 sections of the documentation have been completely redesigned as short getting started and FAQ documents. Most packaging documentation has now been moved out to the Python Packaging Authority maintained Python Packaging User Guide [http://packaging.python.org] and the documentation of the individual projects.

However, as this migration is currently still incomplete, the legacy versions of those guides remaining available as 安装Python模块（旧版） and 分发 Python 模块（遗留版本）.

参见

PEP 453 [https://www.python.org/dev/peps/pep-0453] – Python安装中pip的显式引导

PEP 由Donald Stufft 和 Nick Coghlan 撰写，由 Donald Stufft，Nick Coghlan，Martin von Löwis 和 Ned Deily 实现。

PEP 476: Enabling certificate verification by default for stdlib http clients

PEP 476 [https://www.python.org/dev/peps/pep-0476] updated httplib and modules which use it, such as urllib2 and xmlrpclib, to now verify that the server presents a certificate which is signed by a Certificate Authority in the platform trust store and whose hostname matches the hostname being requested by default, significantly improving security for many applications. This change was made in the Python 2.7.9 release.

For applications which require the old previous behavior, they can pass an alternate context:

import urllib2
import ssl

# This disables all verification
context = ssl._create_unverified_context()

# This allows using a specific certificate for the host, which doesn't need
# to be in the trust store
context = ssl.create_default_context(cafile="/path/to/file.crt")

urllib2.urlopen("https://invalid-cert", context=context)

PEP 493：适用于Python 2.7 的HTTPS验证迁移工具

PEP 493 [https://www.python.org/dev/peps/pep-0493] provides additional migration tools to support a more incremental infrastructure upgrade process for environments containing applications and services relying on the historically permissive processing of server certificates when establishing client HTTPS connections. These additions were made in the Python 2.7.12 release.

These tools are intended for use in cases where affected applications and services can’t be modified to explicitly pass a more permissive SSL context when establishing the connection.

For applications and services which can’t be modified at all, the new PYTHONHTTPSVERIFY environment variable may be set to 0 to revert an entire Python process back to the default permissive behaviour of Python 2.7.8 and earlier.

For cases where the connection establishment code can’t be modified, but the overall application can be, the new ssl._https_verify_certificates() function can be used to adjust the default behaviour at runtime.

New make regen-all build target

To simplify cross-compilation, and to ensure that CPython can reliably be compiled without requiring an existing version of Python to already be available, the autotools-based build system no longer attempts to implicitly recompile generated files based on file modification times.

Instead, a new make regen-all command has been added to force regeneration of these files when desired (e.g. after an initial version of Python has already been built based on the pregenerated versions).

More selective regeneration targets are also defined - see Makefile.pre.in [https://github.com/python/cpython/tree/3.6/Makefile.pre.in] for details.

（由 Victor Stinner 在 bpo-23404 [https://bugs.python.org/issue23404] 中贡献。）

2.7.14 新版功能.

Removal of make touch build target

The make touch build target previously used to request implicit regeneration of generated files by updating their modification times has been removed.

It has been replaced by the new make regen-all target.

（由 Victor Stinner 在 bpo-23404 [https://bugs.python.org/issue23404] 中贡献。）

在 2.7.14 版更改.

致谢

作者要感谢以下人员为本文的各种草案提供建议，更正和帮助： Nick Coghlan, Philip Jenvey, Ryan Lovett, R. David Murray, Hugh Secker-Walker.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

Python 2.6 有什么新变化

作者

A.M. Kuchling (amk at amk.ca)

本文介绍了 Python 2.6 的新特性，它发布于 2008 年 10 月 1 日。 发布日程说明见 PEP 361 [https://www.python.org/dev/peps/pep-0361]。

The major theme of Python 2.6 is preparing the migration path to Python 3.0, a major redesign of the language. Whenever possible, Python 2.6 incorporates new features and syntax from 3.0 while remaining compatible with existing code by not removing older features or syntax. When it’s not possible to do that, Python 2.6 tries to do what it can, adding compatibility functions in a future_builtins module and a -3 switch to warn about usages that will become unsupported in 3.0.

Some significant new packages have been added to the standard library, such as the multiprocessing and json modules, but there aren’t many new features that aren’t related to Python 3.0 in some way.

Python 2.6 also sees a number of improvements and bugfixes throughout the source. A search through the change logs finds there were 259 patches applied and 612 bugs fixed between Python 2.5 and 2.6. Both figures are likely to be underestimates.

This article doesn’t attempt to provide a complete specification of the new features, but instead provides a convenient overview. For full details, you should refer to the documentation for Python 2.6. If you want to understand the rationale for the design and implementation, refer to the PEP for a particular new feature. Whenever possible, “What’s New in Python” links to the bug/patch item for each change.

Python 3.0

The development cycle for Python versions 2.6 and 3.0 was synchronized, with the alpha and beta releases for both versions being made on the same days. The development of 3.0 has influenced many features in 2.6.

Python 3.0 is a far-ranging redesign of Python that breaks compatibility with the 2.x series. This means that existing Python code will need some conversion in order to run on Python 3.0. However, not all the changes in 3.0 necessarily break compatibility. In cases where new features won’t cause existing code to break, they’ve been backported to 2.6 and are described in this document in the appropriate place. Some of the 3.0-derived features are:

A __complex__() method for converting objects to a complex number.

Alternate syntax for catching exceptions: except TypeError as exc.

The addition of functools.reduce() as a synonym for the built-in reduce() function.

Python 3.0 adds several new built-in functions and changes the semantics of some existing builtins. Functions that are new in 3.0 such as bin() have simply been added to Python 2.6, but existing builtins haven’t been changed; instead, the future_builtins module has versions with the new 3.0 semantics. Code written to be compatible with 3.0 can do from future_builtins import hex, map as necessary.

A new command-line switch, -3, enables warnings about features that will be removed in Python 3.0. You can run code with this switch to see how much work will be necessary to port code to 3.0. The value of this switch is available to Python code as the boolean variable sys.py3kwarning, and to C extension code as Py_Py3kWarningFlag.

参见

The 3xxx series of PEPs, which contains proposals for Python 3.0. PEP 3000 [https://www.python.org/dev/peps/pep-3000] describes the development process for Python 3.0. Start with PEP 3100 [https://www.python.org/dev/peps/pep-3100] that describes the general goals for Python 3.0, and then explore the higher-numbered PEPS that propose specific features.

开发过程的变化

While 2.6 was being developed, the Python development process underwent two significant changes: we switched from SourceForge’s issue tracker to a customized Roundup installation, and the documentation was converted from LaTeX to reStructuredText.

New Issue Tracker: Roundup

For a long time, the Python developers had been growing increasingly annoyed by SourceForge’s bug tracker. SourceForge’s hosted solution doesn’t permit much customization; for example, it wasn’t possible to customize the life cycle of issues.

The infrastructure committee of the Python Software Foundation therefore posted a call for issue trackers, asking volunteers to set up different products and import some of the bugs and patches from SourceForge. Four different trackers were examined: Jira [https://www.atlassian.com/software/jira/], Launchpad [https://launchpad.net/], Roundup [http://roundup.sourceforge.net/], and Trac [https://trac.edgewall.org/]. The committee eventually settled on Jira and Roundup as the two candidates. Jira is a commercial product that offers no-cost hosted instances to free-software projects; Roundup is an open-source project that requires volunteers to administer it and a server to host it.

After posting a call for volunteers, a new Roundup installation was set up at https://bugs.python.org. One installation of Roundup can host multiple trackers, and this server now also hosts issue trackers for Jython and for the Python web site. It will surely find other uses in the future. Where possible, this edition of “What’s New in Python” links to the bug/patch item for each change.

Hosting of the Python bug tracker is kindly provided by Upfront Systems [http://www.upfrontsystems.co.za/] of Stellenbosch, South Africa. Martin von Löwis put a lot of effort into importing existing bugs and patches from SourceForge; his scripts for this import operation are at http://svn.python.org/view/tracker/importer/ and may be useful to other projects wishing to move from SourceForge to Roundup.

参见

https://bugs.python.org

Python 的错误追踪器

http://bugs.jython.org:

Jython 的错误追踪器

http://roundup.sourceforge.net/

Roundup 下载和文档。

http://svn.python.org/view/tracker/importer/

Martin von Löwis 的转换脚本。

新的文档格式：使用 Sphinx 的 reStructuredText

The Python documentation was written using LaTeX since the project started around 1989. In the 1980s and early 1990s, most documentation was printed out for later study, not viewed online. LaTeX was widely used because it provided attractive printed output while remaining straightforward to write once the basic rules of the markup were learned.

Today LaTeX is still used for writing publications destined for printing, but the landscape for programming tools has shifted. We no longer print out reams of documentation; instead, we browse through it online and HTML has become the most important format to support. Unfortunately, converting LaTeX to HTML is fairly complicated and Fred L. Drake Jr., the long-time Python documentation editor, spent a lot of time maintaining the conversion process. Occasionally people would suggest converting the documentation into SGML and later XML, but performing a good conversion is a major task and no one ever committed the time required to finish the job.

During the 2.6 development cycle, Georg Brandl put a lot of effort into building a new toolchain for processing the documentation. The resulting package is called Sphinx, and is available from http://sphinx-doc.org/.

Sphinx concentrates on HTML output, producing attractively styled and modern HTML; printed output is still supported through conversion to LaTeX. The input format is reStructuredText, a markup syntax supporting custom extensions and directives that is commonly used in the Python community.

Sphinx is a standalone package that can be used for writing, and almost two dozen other projects (listed on the Sphinx web site [http://sphinx-doc.org/examples.html]) have adopted Sphinx as their documentation tool.

参见

Documenting Python [https://devguide.python.org/documenting/]

描述如何编写Python文档。

Sphinx [http://sphinx-doc.org/]

Sphinx工具链的文档和代码。

Docutils [http://docutils.sourceforge.net]

reStructuredText 的基础解析器和工具集。

PEP 343: “with” 语句

The previous version, Python 2.5, added the ‘with’ statement as an optional feature, to be enabled by a from __future__ import with_statement directive. In 2.6 the statement no longer needs to be specially enabled; this means that with is now always a keyword. The rest of this section is a copy of the corresponding section from the “What’s New in Python 2.5” document; if you’re familiar with the ‘with’ statement from Python 2.5, you can skip this section.

The ‘with’ statement clarifies code that previously would use try...finally blocks to ensure that clean-up code is executed. In this section, I’ll discuss the statement as it will commonly be used. In the next section, I’ll examine the implementation details and show how to write objects for use with this statement.

The ‘with’ statement is a control-flow structure whose basic structure is:

with expression [as variable]:
    with-block

The expression is evaluated, and it should result in an object that supports the context management protocol (that is, has __enter__() and __exit__() methods).

The object’s __enter__() is called before with-block is executed and therefore can run set-up code. It also may return a value that is bound to the name variable, if given. (Note carefully that variable is not assigned the result of expression.)

After execution of the with-block is finished, the object’s __exit__() method is called, even if the block raised an exception, and can therefore run clean-up code.

Some standard Python objects now support the context management protocol and can be used with the ‘with’ statement. File objects are one example:

with open('/etc/passwd', 'r') as f:
    for line in f:
        print line
        ... more processing code ...

After this statement has executed, the file object in f will have been automatically closed, even if the for loop raised an exception part-way through the block.

注解

In this case, f is the same object created by open(), because file.__enter__() returns self.

The threading module’s locks and condition variables also support the ‘with’ statement:

lock = threading.Lock()
with lock:
    # Critical section of code
    ...

The lock is acquired before the block is executed and always released once the block is complete.

The localcontext() function in the decimal module makes it easy to save and restore the current decimal context, which encapsulates the desired precision and rounding characteristics for computations:

from decimal import Decimal, Context, localcontext

# Displays with default precision of 28 digits
v = Decimal('578')
print v.sqrt()

with localcontext(Context(prec=16)):
    # All code in this block uses a precision of 16 digits.
    # The original context is restored on exiting the block.
    print v.sqrt()

Writing Context Managers

Under the hood, the ‘with’ statement is fairly complicated. Most people will only use ‘with’ in company with existing objects and don’t need to know these details, so you can skip the rest of this section if you like. Authors of new objects will need to understand the details of the underlying implementation and should keep reading.

A high-level explanation of the context management protocol is:

The expression is evaluated and should result in an object called a “context manager”. The context manager must have __enter__() and __exit__() methods.

The context manager’s __enter__() method is called. The value returned is assigned to VAR. If no as VAR clause is present, the value is simply discarded.

The code in BLOCK is executed.

If BLOCK raises an exception, the context manager’s __exit__() method is called with three arguments, the exception details (type, value, traceback, the same values returned by sys.exc_info(), which can also be None if no exception occurred). The method’s return value controls whether an exception is re-raised: any false value re-raises the exception, and True will result in suppressing it. You’ll only rarely want to suppress the exception, because if you do the author of the code containing the ‘with’ statement will never realize anything went wrong.

If BLOCK didn’t raise an exception, the __exit__() method is still called, but type, value, and traceback are all None.

Let’s think through an example. I won’t present detailed code but will only sketch the methods necessary for a database that supports transactions.

(For people unfamiliar with database terminology: a set of changes to the database are grouped into a transaction. Transactions can be either committed, meaning that all the changes are written into the database, or rolled back, meaning that the changes are all discarded and the database is unchanged. See any database textbook for more information.)

Let’s assume there’s an object representing a database connection. Our goal will be to let the user write code like this:

db_connection = DatabaseConnection()
with db_connection as cursor:
    cursor.execute('insert into ...')
    cursor.execute('delete from ...')
    # ... more operations ...

The transaction should be committed if the code in the block runs flawlessly or rolled back if there’s an exception. Here’s the basic interface for DatabaseConnection that I’ll assume:

class DatabaseConnection:
    # Database interface
    def cursor(self):
        "Returns a cursor object and starts a new transaction"
    def commit(self):
        "Commits current transaction"
    def rollback(self):
        "Rolls back current transaction"

The __enter__() method is pretty easy, having only to start a new transaction. For this application the resulting cursor object would be a useful result, so the method will return it. The user can then add as cursor to their ‘with’ statement to bind the cursor to a variable name.

class DatabaseConnection:
    ...
    def __enter__(self):
        # Code to start a new transaction
        cursor = self.cursor()
        return cursor

The __exit__() method is the most complicated because it’s where most of the work has to be done. The method has to check if an exception occurred. If there was no exception, the transaction is committed. The transaction is rolled back if there was an exception.

In the code below, execution will just fall off the end of the function, returning the default value of None. None is false, so the exception will be re-raised automatically. If you wished, you could be more explicit and add a return statement at the marked location.

class DatabaseConnection:
    ...
    def __exit__(self, type, value, tb):
        if tb is None:
            # No exception, so commit
            self.commit()
        else:
            # Exception occurred, so rollback.
            self.rollback()
            # return False

contextlib 模块

The contextlib module provides some functions and a decorator that are useful when writing objects for use with the ‘with’ statement.

The decorator is called contextmanager(), and lets you write a single generator function instead of defining a new class. The generator should yield exactly one value. The code up to the yield will be executed as the __enter__() method, and the value yielded will be the method’s return value that will get bound to the variable in the ‘with’ statement’s as clause, if any. The code after the yield will be executed in the __exit__() method. Any exception raised in the block will be raised by the yield statement.

Using this decorator, our database example from the previous section could be written as:

from contextlib import contextmanager

@contextmanager
def db_transaction(connection):
    cursor = connection.cursor()
    try:
        yield cursor
    except:
        connection.rollback()
        raise
    else:
        connection.commit()

db = DatabaseConnection()
with db_transaction(db) as cursor:
    ...

The contextlib module also has a nested(mgr1, mgr2, ...) function that combines a number of context managers so you don’t need to write nested ‘with’ statements. In this example, the single ‘with’ statement both starts a database transaction and acquires a thread lock:

lock = threading.Lock()
with nested (db_transaction(db), lock) as (cursor, locked):
    ...

Finally, the closing() function returns its argument so that it can be bound to a variable, and calls the argument’s .close() method at the end of the block.

import urllib, sys
from contextlib import closing

with closing(urllib.urlopen('http://www.yahoo.com')) as f:
    for line in f:
        sys.stdout.write(line)

参见

PEP 343 [https://www.python.org/dev/peps/pep-0343] - “with” 语句

PEP written by Guido van Rossum and Nick Coghlan; implemented by Mike Bland, Guido van Rossum, and Neal Norwitz. The PEP shows the code generated for a ‘with’ statement, which can be helpful in learning how the statement works.

contextlib 模块的文档。

PEP 366: 从主模块显式相对导入

Python’s -m switch allows running a module as a script. When you ran a module that was located inside a package, relative imports didn’t work correctly.

The fix for Python 2.6 adds a __package__ attribute to modules. When this attribute is present, relative imports will be relative to the value of this attribute instead of the __name__ attribute.

PEP 302-style importers can then set __package__ as necessary. The runpy module that implements the -m switch now does this, so relative imports will now work correctly in scripts running from inside a package.

PEP 370: 分用户的 site-packages 目录

When you run Python, the module search path sys.path usually includes a directory whose path ends in "site-packages". This directory is intended to hold locally-installed packages available to all users using a machine or a particular site installation.

Python 2.6 introduces a convention for user-specific site directories. The directory varies depending on the platform:

Unix and Mac OS X: ~/.local/

Windows: %APPDATA%/Python

Within this directory, there will be version-specific subdirectories, such as lib/python2.6/site-packages on Unix/Mac OS and Python26/site-packages on Windows.

If you don’t like the default directory, it can be overridden by an environment variable. PYTHONUSERBASE sets the root directory used for all Python versions supporting this feature. On Windows, the directory for application-specific data can be changed by setting the APPDATA environment variable. You can also modify the site.py file for your Python installation.

The feature can be disabled entirely by running Python with the -s option or setting the PYTHONNOUSERSITE environment variable.

参见

PEP 370 [https://www.python.org/dev/peps/pep-0370] – 分用户的 site-packages 目录

PEP 由 Christian Heimes 撰写并实现

PEP 371: 多任务处理包

The new multiprocessing package lets Python programs create new processes that will perform a computation and return a result to the parent. The parent and child processes can communicate using queues and pipes, synchronize their operations using locks and semaphores, and can share simple arrays of data.

The multiprocessing module started out as an exact emulation of the threading module using processes instead of threads. That goal was discarded along the path to Python 2.6, but the general approach of the module is still similar. The fundamental class is the Process, which is passed a callable object and a collection of arguments. The start() method sets the callable running in a subprocess, after which you can call the is_alive() method to check whether the subprocess is still running and the join() method to wait for the process to exit.

Here’s a simple example where the subprocess will calculate a factorial. The function doing the calculation is written strangely so that it takes significantly longer when the input argument is a multiple of 4.

import time
from multiprocessing import Process, Queue

def factorial(queue, N):
    "Compute a factorial."
    # If N is a multiple of 4, this function will take much longer.
    if (N % 4) == 0:
        time.sleep(.05 * N/4)

    # Calculate the result
    fact = 1L
    for i in range(1, N+1):
        fact = fact * i

    # Put the result on the queue
    queue.put(fact)

if __name__ == '__main__':
    queue = Queue()

    N = 5

    p = Process(target=factorial, args=(queue, N))
    p.start()
    p.join()

    result = queue.get()
    print 'Factorial', N, '=', result

A Queue is used to communicate the result of the factorial. The Queue object is stored in a global variable. The child process will use the value of the variable when the child was created; because it’s a Queue, parent and child can use the object to communicate. (If the parent were to change the value of the global variable, the child’s value would be unaffected, and vice versa.)

Two other classes, Pool and Manager, provide higher-level interfaces. Pool will create a fixed number of worker processes, and requests can then be distributed to the workers by calling apply() or apply_async() to add a single request, and map() or map_async() to add a number of requests. The following code uses a Pool to spread requests across 5 worker processes and retrieve a list of results:

from multiprocessing import Pool

def factorial(N, dictionary):
    "Compute a factorial."
    ...
p = Pool(5)
result = p.map(factorial, range(1, 1000, 10))
for v in result:
    print v

This produces the following output:

1
39916800
51090942171709440000
8222838654177922817725562880000000
33452526613163807108170062053440751665152000000000
...

The other high-level interface, the Manager class, creates a separate server process that can hold master copies of Python data structures. Other processes can then access and modify these data structures using proxy objects. The following example creates a shared dictionary by calling the dict() method; the worker processes then insert values into the dictionary. (Locking is not done for you automatically, which doesn’t matter in this example. Manager’s methods also include Lock(), RLock(), and Semaphore() to create shared locks.)

import time
from multiprocessing import Pool, Manager

def factorial(N, dictionary):
    "Compute a factorial."
    # Calculate the result
    fact = 1L
    for i in range(1, N+1):
        fact = fact * i

    # Store result in dictionary
    dictionary[N] = fact

if __name__ == '__main__':
    p = Pool(5)
    mgr = Manager()
    d = mgr.dict()         # Create shared dictionary

    # Run tasks using the pool
    for N in range(1, 1000, 10):
        p.apply_async(factorial, (N, d))

    # Mark pool as closed -- no more tasks can be added.
    p.close()

    # Wait for tasks to exit
    p.join()

    # Output results
    for k, v in sorted(d.items()):
        print k, v

This will produce the output:

1 1
11 39916800
21 51090942171709440000
31 8222838654177922817725562880000000
41 33452526613163807108170062053440751665152000000000
51 15511187532873822802242430164693032110632597200169861120000...

参见

multiprocessing 模块的文档。

PEP 371 [https://www.python.org/dev/peps/pep-0371] - 添加多任务处理包

PEP 由 Jesse Noller 和 Richard Oudkerk 撰写，由 Richard Oudkerk 和 Jesse Noller 实现

PEP 3101: 高级字符串格式

In Python 3.0, the % operator is supplemented by a more powerful string formatting method, format(). Support for the str.format() method has been backported to Python 2.6.

In 2.6, both 8-bit and Unicode strings have a .format() method that treats the string as a template and takes the arguments to be formatted. The formatting template uses curly brackets ({, }) as special characters:

>>> # Substitute positional argument 0 into the string.
>>> "User ID: {0}".format("root")
'User ID: root'
>>> # Use the named keyword arguments
>>> "User ID: {uid}   Last seen: {last_login}".format(
...    uid="root",
...    last_login = "5 Mar 2008 07:20")
'User ID: root   Last seen: 5 Mar 2008 07:20'

Curly brackets can be escaped by doubling them:

>>> "Empty dict: {{}}".format()
"Empty dict: {}"

Field names can be integers indicating positional arguments, such as {0}, {1}, etc. or names of keyword arguments. You can also supply compound field names that read attributes or access dictionary keys:

>>> import sys
>>> print 'Platform: {0.platform}\nPython version: {0.version}'.format(sys)
Platform: darwin
Python version: 2.6a1+ (trunk:61261M, Mar  5 2008, 20:29:41)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)]'

>>> import mimetypes
>>> 'Content-type: {0[.mp4]}'.format(mimetypes.types_map)
'Content-type: video/mp4'

Note that when using dictionary-style notation such as [.mp4], you don’t need to put any quotation marks around the string; it will look up the value using .mp4 as the key. Strings beginning with a number will be converted to an integer. You can’t write more complicated expressions inside a format string.

So far we’ve shown how to specify which field to substitute into the resulting string. The precise formatting used is also controllable by adding a colon followed by a format specifier. For example:

>>> # Field 0: left justify, pad to 15 characters
>>> # Field 1: right justify, pad to 6 characters
>>> fmt = '{0:15} ${1:>6}'
>>> fmt.format('Registration', 35)
'Registration    $    35'
>>> fmt.format('Tutorial', 50)
'Tutorial        $    50'
>>> fmt.format('Banquet', 125)
'Banquet         $   125'

Format specifiers can reference other fields through nesting:

>>> fmt = '{0:{1}}'
>>> width = 15
>>> fmt.format('Invoice #1234', width)
'Invoice #1234  '
>>> width = 35
>>> fmt.format('Invoice #1234', width)
'Invoice #1234                      '

可以指定所需宽度内的字段对齐方式：

字符

效果

< (默认)

左对齐

>

右对齐

^

居中对齐

=

（仅适用于数字类型）在符号后加空格。

Format specifiers can also include a presentation type, which controls how the value is formatted. For example, floating-point numbers can be formatted as a general number or in exponential notation:

>>> '{0:g}'.format(3.75)
'3.75'
>>> '{0:e}'.format(3.75)
'3.750000e+00'

A variety of presentation types are available. Consult the 2.6 documentation for a complete list; here’s a sample:

b

二进制。输出以2为底的数字。

c

字符。在打印之前将整数转换为相应的Unicode字符。

d

十进制整数。 输出以 10 为基数的数字。

o

八进制格式。 输出以 8 为基数的数字。

x

十六进制格式。 输出以 16 为基数的数字，使用小写字母表示 9 以上的数码。

e

指数表示法。用字母 ‘e’ 以科学计数法打印数字以表示指数。

g

General format. This prints the number as a fixed-point number, unless the number is too large, in which case it switches to ‘e’ exponent notation.

n

Number. This is the same as ‘g’ (for floats) or ‘d’ (for integers), except that it uses the current locale setting to insert the appropriate number separator characters.

%

Percentage. Multiplies the number by 100 and displays in fixed (‘f’) format, followed by a percent sign.

Classes and types can define a __format__() method to control how they’re formatted. It receives a single argument, the format specifier:

def __format__(self, format_spec):
    if isinstance(format_spec, unicode):
        return unicode(str(self))
    else:
        return str(self)

There’s also a format() builtin that will format a single value. It calls the type’s __format__() method with the provided specifier:

>>> format(75.6564, '.2f')
'75.66'

参见

格式字符串语法

格式字段的参考文档。

PEP 3101 [https://www.python.org/dev/peps/pep-3101] - 高级字符串格式

PEP 由 Eric V. Smith 撰写并实现

PEP 3105: print 改为函数

The print statement becomes the print() function in Python 3.0. Making print() a function makes it possible to replace the function by doing def print(...) or importing a new function from somewhere else.

Python 2.6 has a __future__ import that removes print as language syntax, letting you use the functional form instead. For example:

>>> from __future__ import print_function
>>> print('# of entries', len(dictionary), file=sys.stderr)

The signature of the new function is:

def print(*args, sep=' ', end='\n', file=None)

The parameters are:

args: positional arguments whose values will be printed out.

sep: the separator, which will be printed between arguments.

end: the ending text, which will be printed after all of the arguments have been output.

file: the file object to which the output will be sent.

参见

PEP 3105 [https://www.python.org/dev/peps/pep-3105]: print 改为函数

PEP 由 Georg Brandl 撰写

PEP 3110: 异常处理的变更

One error that Python programmers occasionally make is writing the following code:

try:
    ...
except TypeError, ValueError:  # Wrong!
    ...

The author is probably trying to catch both TypeError and ValueError exceptions, but this code actually does something different: it will catch TypeError and bind the resulting exception object to the local name "ValueError". The ValueError exception will not be caught at all. The correct code specifies a tuple of exceptions:

try:
    ...
except (TypeError, ValueError):
    ...

This error happens because the use of the comma here is ambiguous: does it indicate two different nodes in the parse tree, or a single node that’s a tuple?

Python 3.0 makes this unambiguous by replacing the comma with the word “as”. To catch an exception and store the exception object in the variable exc, you must write:

try:
    ...
except TypeError as exc:
    ...

Python 3.0 will only support the use of “as”, and therefore interprets the first example as catching two different exceptions. Python 2.6 supports both the comma and “as”, so existing code will continue to work. We therefore suggest using “as” when writing new Python code that will only be executed with 2.6.

参见

PEP 3110 [https://www.python.org/dev/peps/pep-3110] - 在 Python 3000 中捕获异常

PEP 由 Collin Winter 撰写并实现

PEP 3112: 字节字面值

Python 3.0 adopts Unicode as the language’s fundamental string type and denotes 8-bit literals differently, either as b'string' or using a bytes constructor. For future compatibility, Python 2.6 adds bytes as a synonym for the str type, and it also supports the b'' notation.

The 2.6 str differs from 3.0’s bytes type in various ways; most notably, the constructor is completely different. In 3.0, bytes([65, 66, 67]) is 3 elements long, containing the bytes representing ABC; in 2.6, bytes([65, 66, 67]) returns the 12-byte string representing the str() of the list.

The primary use of bytes in 2.6 will be to write tests of object type such as isinstance(x, bytes). This will help the 2to3 converter, which can’t tell whether 2.x code intends strings to contain either characters or 8-bit bytes; you can now use either bytes or str to represent your intention exactly, and the resulting code will also be correct in Python 3.0.

There’s also a __future__ import that causes all string literals to become Unicode strings. This means that \u escape sequences can be used to include Unicode characters:

from __future__ import unicode_literals

s = ('\u751f\u3080\u304e\u3000\u751f\u3054'
     '\u3081\u3000\u751f\u305f\u307e\u3054')

print len(s)               # 12 Unicode characters

At the C level, Python 3.0 will rename the existing 8-bit string type, called PyStringObject in Python 2.x, to PyBytesObject. Python 2.6 uses #define to support using the names PyBytesObject(), PyBytes_Check(), PyBytes_FromStringAndSize(), and all the other functions and macros used with strings.

Instances of the bytes type are immutable just as strings are. A new bytearray type stores a mutable sequence of bytes:

>>> bytearray([65, 66, 67])
bytearray(b'ABC')
>>> b = bytearray(u'\u21ef\u3244', 'utf-8')
>>> b
bytearray(b'\xe2\x87\xaf\xe3\x89\x84')
>>> b[0] = '\xe3'
>>> b
bytearray(b'\xe3\x87\xaf\xe3\x89\x84')
>>> unicode(str(b), 'utf-8')
u'\u31ef \u3244'

Byte arrays support most of the methods of string types, such as startswith()/endswith(), find()/rfind(), and some of the methods of lists, such as append(), pop(), and reverse().

>>> b = bytearray('ABC')
>>> b.append('d')
>>> b.append(ord('e'))
>>> b
bytearray(b'ABCde')

There’s also a corresponding C API, with PyByteArray_FromObject(), PyByteArray_FromStringAndSize(), and various other functions.

参见

PEP 3112 [https://www.python.org/dev/peps/pep-3112] - Python 3000 中的字节字面值

PEP 由 Jason Orendorff 撰写， 补丁2.6 由 Christian Heimes 撰写。

PEP 3116: 新 I/O 库

Python’s built-in file objects support a number of methods, but file-like objects don’t necessarily support all of them. Objects that imitate files usually support read() and write(), but they may not support readline(), for example. Python 3.0 introduces a layered I/O library in the io module that separates buffering and text-handling features from the fundamental read and write operations.

There are three levels of abstract base classes provided by the io module:

RawIOBase defines raw I/O operations: read(), readinto(), write(), seek(), tell(), truncate(), and close(). Most of the methods of this class will often map to a single system call. There are also readable(), writable(), and seekable() methods for determining what operations a given object will allow.

Python 3.0 has concrete implementations of this class for files and sockets, but Python 2.6 hasn’t restructured its file and socket objects in this way.

BufferedIOBase is an abstract base class that buffers data in memory to reduce the number of system calls used, making I/O processing more efficient. It supports all of the methods of RawIOBase, and adds a raw attribute holding the underlying raw object.

There are five concrete classes implementing this ABC. BufferedWriter and BufferedReader are for objects that support write-only or read-only usage that have a seek() method for random access. BufferedRandom objects support read and write access upon the same underlying stream, and BufferedRWPair is for objects such as TTYs that have both read and write operations acting upon unconnected streams of data. The BytesIO class supports reading, writing, and seeking over an in-memory buffer.

TextIOBase: Provides functions for reading and writing strings (remember, strings will be Unicode in Python 3.0), and supporting universal newlines. TextIOBase defines the readline() method and supports iteration upon objects.

There are two concrete implementations. TextIOWrapper wraps a buffered I/O object, supporting all of the methods for text I/O and adding a buffer attribute for access to the underlying object. StringIO simply buffers everything in memory without ever writing anything to disk.

(In Python 2.6, io.StringIO is implemented in pure Python, so it’s pretty slow. You should therefore stick with the existing StringIO module or cStringIO for now. At some point Python 3.0’s io module will be rewritten into C for speed, and perhaps the C implementation will be backported to the 2.x releases.)

In Python 2.6, the underlying implementations haven’t been restructured to build on top of the io module’s classes. The module is being provided to make it easier to write code that’s forward-compatible with 3.0, and to save developers the effort of writing their own implementations of buffering and text I/O.

参见

PEP 3116 [https://www.python.org/dev/peps/pep-3116] - 新 I/O

PEP written by Daniel Stutzbach, Mike Verdone, and Guido van Rossum. Code by Guido van Rossum, Georg Brandl, Walter Doerwald, Jeremy Hylton, Martin von Löwis, Tony Lownds, and others.

PEP 3118: 修改缓冲区协议

The buffer protocol is a C-level API that lets Python types exchange pointers into their internal representations. A memory-mapped file can be viewed as a buffer of characters, for example, and this lets another module such as re treat memory-mapped files as a string of characters to be searched.

The primary users of the buffer protocol are numeric-processing packages such as NumPy, which expose the internal representation of arrays so that callers can write data directly into an array instead of going through a slower API. This PEP updates the buffer protocol in light of experience from NumPy development, adding a number of new features such as indicating the shape of an array or locking a memory region.

The most important new C API function is PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags), which takes an object and a set of flags, and fills in the Py_buffer structure with information about the object’s memory representation. Objects can use this operation to lock memory in place while an external caller could be modifying the contents, so there’s a corresponding PyBuffer_Release(Py_buffer *view) to indicate that the external caller is done.

The flags argument to PyObject_GetBuffer() specifies constraints upon the memory returned. Some examples are:

PyBUF_WRITABLE indicates that the memory must be writable.

PyBUF_LOCK requests a read-only or exclusive lock on the memory.

PyBUF_C_CONTIGUOUS and PyBUF_F_CONTIGUOUS requests a C-contiguous (last dimension varies the fastest) or Fortran-contiguous (first dimension varies the fastest) array layout.

Two new argument codes for PyArg_ParseTuple(), s* and z*, return locked buffer objects for a parameter.

参见

PEP 3118 [https://www.python.org/dev/peps/pep-3118] - 修改缓冲区协议

PEP 由 Travis Oliphant 和 Carl Banks 撰写，由 Travis Oliphant 实现。

PEP 3119: 抽象基类

Some object-oriented languages such as Java support interfaces, declaring that a class has a given set of methods or supports a given access protocol. Abstract Base Classes (or ABCs) are an equivalent feature for Python. The ABC support consists of an abc module containing a metaclass called ABCMeta, special handling of this metaclass by the isinstance() and issubclass() builtins, and a collection of basic ABCs that the Python developers think will be widely useful. Future versions of Python will probably add more ABCs.

Let’s say you have a particular class and wish to know whether it supports dictionary-style access. The phrase “dictionary-style” is vague, however. It probably means that accessing items with obj[1] works. Does it imply that setting items with obj[2] = value works? Or that the object will have keys(), values(), and items() methods? What about the iterative variants such as iterkeys()? copy() and update()? Iterating over the object with iter()?

The Python 2.6 collections module includes a number of different ABCs that represent these distinctions. Iterable indicates that a class defines __iter__(), and Container means the class defines a __contains__() method and therefore supports x in y expressions. The basic dictionary interface of getting items, setting items, and keys(), values(), and items(), is defined by the MutableMapping ABC.

You can derive your own classes from a particular ABC to indicate they support that ABC’s interface:

import collections

class Storage(collections.MutableMapping):
    ...

Alternatively, you could write the class without deriving from the desired ABC and instead register the class by calling the ABC’s register() method:

import collections

class Storage:
    ...

collections.MutableMapping.register(Storage)

For classes that you write, deriving from the ABC is probably clearer. The register() method is useful when you’ve written a new ABC that can describe an existing type or class, or if you want to declare that some third-party class implements an ABC. For example, if you defined a PrintableType ABC, it’s legal to do:

# Register Python's types
PrintableType.register(int)
PrintableType.register(float)
PrintableType.register(str)

Classes should obey the semantics specified by an ABC, but Python can’t check this; it’s up to the class author to understand the ABC’s requirements and to implement the code accordingly.

To check whether an object supports a particular interface, you can now write:

def func(d):
    if not isinstance(d, collections.MutableMapping):
        raise ValueError("Mapping object expected, not %r" % d)

Don’t feel that you must now begin writing lots of checks as in the above example. Python has a strong tradition of duck-typing, where explicit type-checking is never done and code simply calls methods on an object, trusting that those methods will be there and raising an exception if they aren’t. Be judicious in checking for ABCs and only do it where it’s absolutely necessary.

You can write your own ABCs by using abc.ABCMeta as the metaclass in a class definition:

from abc import ABCMeta, abstractmethod

class Drawable():
    __metaclass__ = ABCMeta

    @abstractmethod
    def draw(self, x, y, scale=1.0):
        pass

    def draw_doubled(self, x, y):
        self.draw(x, y, scale=2.0)

class Square(Drawable):
    def draw(self, x, y, scale):
        ...

In the Drawable ABC above, the draw_doubled() method renders the object at twice its size and can be implemented in terms of other methods described in Drawable. Classes implementing this ABC therefore don’t need to provide their own implementation of draw_doubled(), though they can do so. An implementation of draw() is necessary, though; the ABC can’t provide a useful generic implementation.

You can apply the @abstractmethod decorator to methods such as draw() that must be implemented; Python will then raise an exception for classes that don’t define the method. Note that the exception is only raised when you actually try to create an instance of a subclass lacking the method:

>>> class Circle(Drawable):
...     pass
...
>>> c = Circle()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Circle with abstract methods draw
>>>

Abstract data attributes can be declared using the @abstractproperty decorator:

from abc import abstractproperty
...

@abstractproperty
def readonly(self):
   return self._x

Subclasses must then define a readonly() property.

参见

PEP 3119 [https://www.python.org/dev/peps/pep-3119] - 引入抽象基类

PEP written by Guido van Rossum and Talin. Implemented by Guido van Rossum. Backported to 2.6 by Benjamin Aranguren, with Alex Martelli.

PEP 3127: 整型文字支持和语法

Python 3.0 changes the syntax for octal (base-8) integer literals, prefixing them with “0o” or “0O” instead of a leading zero, and adds support for binary (base-2) integer literals, signalled by a “0b” or “0B” prefix.

Python 2.6 doesn’t drop support for a leading 0 signalling an octal number, but it does add support for “0o” and “0b”:

>>> 0o21, 2*8 + 1
(17, 17)
>>> 0b101111
47

The oct() builtin still returns numbers prefixed with a leading zero, and a new bin() builtin returns the binary representation for a number:

>>> oct(42)
'052'
>>> future_builtins.oct(42)
'0o52'
>>> bin(173)
'0b10101101'

The int() and long() builtins will now accept the “0o” and “0b” prefixes when base-8 or base-2 are requested, or when the base argument is zero (signalling that the base used should be determined from the string):

>>> int ('0o52', 0)
42
>>> int('1101', 2)
13
>>> int('0b1101', 2)
13
>>> int('0b1101', 0)
13

参见

PEP 3127 [https://www.python.org/dev/peps/pep-3127] - 整型文字支持和语法

PEP written by Patrick Maupin; backported to 2.6 by Eric Smith.

PEP 3129: 类装饰器

Decorators have been extended from functions to classes. It’s now legal to write:

@foo
@bar
class A:
  pass

这相当于:

class A:
  pass

A = foo(bar(A))

参见

PEP 3129 [https://www.python.org/dev/peps/pep-3129] - 类装饰器

PEP 由 Collin Winter 撰写并实现

PEP 3141: A Type Hierarchy for Numbers

Python 3.0 adds several abstract base classes for numeric types inspired by Scheme’s numeric tower. These classes were backported to 2.6 as the numbers module.

The most general ABC is Number. It defines no operations at all, and only exists to allow checking if an object is a number by doing isinstance(obj, Number).

Complex is a subclass of Number. Complex numbers can undergo the basic operations of addition, subtraction, multiplication, division, and exponentiation, and you can retrieve the real and imaginary parts and obtain a number’s conjugate. Python’s built-in complex type is an implementation of Complex.

Real further derives from Complex, and adds operations that only work on real numbers: floor(), trunc(), rounding, taking the remainder mod N, floor division, and comparisons.

Rational numbers derive from Real, have numerator and denominator properties, and can be converted to floats. Python 2.6 adds a simple rational-number class, Fraction, in the fractions module. (It’s called Fraction instead of Rational to avoid a name clash with numbers.Rational.)

Integral numbers derive from Rational, and can be shifted left and right with << and >>, combined using bitwise operations such as & and |, and can be used as array indexes and slice boundaries.

In Python 3.0, the PEP slightly redefines the existing builtins round(), math.floor(), math.ceil(), and adds a new one, math.trunc(), that’s been backported to Python 2.6. math.trunc() rounds toward zero, returning the closest Integral that’s between the function’s argument and zero.

参见

PEP 3141 [https://www.python.org/dev/peps/pep-3141] - A Type Hierarchy for Numbers

PEP 由 Jeffrey Yasskin 撰写

Scheme’s numerical tower [https://www.gnu.org/software/guile/manual/html_node/Numerical-Tower.html#Numerical-Tower], from the Guile manual.

Scheme’s number datatypes [http://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_sec_6.2] from the R5RS Scheme specification.

fractions 模块

To fill out the hierarchy of numeric types, the fractions module provides a rational-number class. Rational numbers store their values as a numerator and denominator forming a fraction, and can exactly represent numbers such as 2/3 that floating-point numbers can only approximate.

The Fraction constructor takes two Integral values that will be the numerator and denominator of the resulting fraction.

>>> from fractions import Fraction
>>> a = Fraction(2, 3)
>>> b = Fraction(2, 5)
>>> float(a), float(b)
(0.66666666666666663, 0.40000000000000002)
>>> a+b
Fraction(16, 15)
>>> a/b
Fraction(5, 3)

For converting floating-point numbers to rationals, the float type now has an as_integer_ratio() method that returns the numerator and denominator for a fraction that evaluates to the same floating-point value:

>>> (2.5) .as_integer_ratio()
(5, 2)
>>> (3.1415) .as_integer_ratio()
(7074029114692207L, 2251799813685248L)
>>> (1./3) .as_integer_ratio()
(6004799503160661L, 18014398509481984L)

Note that values that can only be approximated by floating-point numbers, such as 1./3, are not simplified to the number being approximated; the fraction attempts to match the floating-point value exactly.

The fractions module is based upon an implementation by Sjoerd Mullender that was in Python’s Demo/classes/ directory for a long time. This implementation was significantly updated by Jeffrey Yasskin.

其他语言特性修改

对Python 语言核心进行的小改动：

Directories and zip archives containing a __main__.py file can now be executed directly by passing their name to the interpreter. The directory or zip archive is automatically inserted as the first entry in sys.path. (Suggestion and initial patch by Andy Chu, subsequently revised by Phillip J. Eby and Nick Coghlan; bpo-1739468 [https://bugs.python.org/issue1739468].)

The hasattr() function was catching and ignoring all errors, under the assumption that they meant a __getattr__() method was failing somehow and the return value of hasattr() would therefore be False. This logic shouldn’t be applied to KeyboardInterrupt and SystemExit, however; Python 2.6 will no longer discard such exceptions when hasattr() encounters them. (Fixed by Benjamin Peterson; bpo-2196 [https://bugs.python.org/issue2196].)

When calling a function using the ** syntax to provide keyword arguments, you are no longer required to use a Python dictionary; any mapping will now work:

>>> def f(**kw):
...    print sorted(kw)
...
>>> ud=UserDict.UserDict()
>>> ud['a'] = 1
>>> ud['b'] = 'string'
>>> f(**ud)
['a', 'b']

（由 Alexander Belopolsky 在 bpo-1686487 [https://bugs.python.org/issue1686487] 中贡献。）

It’s also become legal to provide keyword arguments after a *args argument to a function call.

>>> def f(*args, **kw):
...     print args, kw
...
>>> f(1,2,3, *(4,5,6), keyword=13)
(1, 2, 3, 4, 5, 6) {'keyword': 13}

Previously this would have been a syntax error. (Contributed by Amaury Forgeot d’Arc; bpo-3473 [https://bugs.python.org/issue3473].)

A new builtin, next(iterator, [default]) returns the next item from the specified iterator. If the default argument is supplied, it will be returned if iterator has been exhausted; otherwise, the StopIteration exception will be raised. (Backported in bpo-2719 [https://bugs.python.org/issue2719].)

Tuples now have index() and count() methods matching the list type’s index() and count() methods:

>>> t = (0,1,2,3,4,0,1,2)
>>> t.index(3)
3
>>> t.count(0)
2

（由 Raymond Hettinger 贡献）

The built-in types now have improved support for extended slicing syntax, accepting various combinations of (start, stop, step). Previously, the support was partial and certain corner cases wouldn’t work. (Implemented by Thomas Wouters.)

Properties now have three attributes, getter, setter and deleter, that are decorators providing useful shortcuts for adding a getter, setter or deleter function to an existing property. You would use them like this:

class C(object):
    @property
    def x(self):
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

    @x.deleter
    def x(self):
        del self._x

class D(C):
    @C.x.getter
    def x(self):
        return self._x * 2

    @x.setter
    def x(self, value):
        self._x = value / 2

Several methods of the built-in set types now accept multiple iterables: intersection(), intersection_update(), union(), update(), difference() and difference_update().

>>> s=set('1234567890')
>>> s.intersection('abc123', 'cdf246')  # Intersection between all inputs
set(['2'])
>>> s.difference('246', '789')
set(['1', '0', '3', '5'])

（由 Raymond Hettinger 贡献。）

Many floating-point features were added. The float() function will now turn the string nan into an IEEE 754 Not A Number value, and +inf and -inf into positive or negative infinity. This works on any platform with IEEE 754 semantics. (Contributed by Christian Heimes; bpo-1635 [https://bugs.python.org/issue1635].)

Other functions in the math module, isinf() and isnan(), return true if their floating-point argument is infinite or Not A Number. (bpo-1640 [https://bugs.python.org/issue1640])

Conversion functions were added to convert floating-point numbers into hexadecimal strings (bpo-3008 [https://bugs.python.org/issue3008]). These functions convert floats to and from a string representation without introducing rounding errors from the conversion between decimal and binary. Floats have a hex() method that returns a string representation, and the float.fromhex() method converts a string back into a number:

>>> a = 3.75
>>> a.hex()
'0x1.e000000000000p+1'
>>> float.fromhex('0x1.e000000000000p+1')
3.75
>>> b=1./3
>>> b.hex()
'0x1.5555555555555p-2'

A numerical nicety: when creating a complex number from two floats on systems that support signed zeros (-0 and +0), the complex() constructor will now preserve the sign of the zero. (Fixed by Mark T. Dickinson; bpo-1507 [https://bugs.python.org/issue1507].)

Classes that inherit a __hash__() method from a parent class can set __hash__ = None to indicate that the class isn’t hashable. This will make hash(obj) raise a TypeError and the class will not be indicated as implementing the Hashable ABC.

You should do this when you’ve defined a __cmp__() or __eq__() method that compares objects by their value rather than by identity. All objects have a default hash method that uses id(obj) as the hash value. There’s no tidy way to remove the __hash__() method inherited from a parent class, so assigning None was implemented as an override. At the C level, extensions can set tp_hash to PyObject_HashNotImplemented(). (Fixed by Nick Coghlan and Amaury Forgeot d’Arc; bpo-2235 [https://bugs.python.org/issue2235].)

The GeneratorExit exception now subclasses BaseException instead of Exception. This means that an exception handler that does except Exception: will not inadvertently catch GeneratorExit. (Contributed by Chad Austin; bpo-1537 [https://bugs.python.org/issue1537].)

Generator objects now have a gi_code attribute that refers to the original code object backing the generator. (Contributed by Collin Winter; bpo-1473257 [https://bugs.python.org/issue1473257].)

The compile() built-in function now accepts keyword arguments as well as positional parameters. (Contributed by Thomas Wouters; bpo-1444529 [https://bugs.python.org/issue1444529].)

The complex() constructor now accepts strings containing parenthesized complex numbers, meaning that complex(repr(cplx)) will now round-trip values. For example, complex('(3+4j)') now returns the value (3+4j). (bpo-1491866 [https://bugs.python.org/issue1491866])

The string translate() method now accepts None as the translation table parameter, which is treated as the identity transformation. This makes it easier to carry out operations that only delete characters. (Contributed by Bengt Richter and implemented by Raymond Hettinger; bpo-1193128 [https://bugs.python.org/issue1193128].)

The built-in dir() function now checks for a __dir__() method on the objects it receives. This method must return a list of strings containing the names of valid attributes for the object, and lets the object control the value that dir() produces. Objects that have __getattr__() or __getattribute__() methods can use this to advertise pseudo-attributes they will honor. (bpo-1591665 [https://bugs.python.org/issue1591665])

Instance method objects have new attributes for the object and function comprising the method; the new synonym for im_self is __self__, and im_func is also available as __func__. The old names are still supported in Python 2.6, but are gone in 3.0.

An obscure change: when you use the locals() function inside a class statement, the resulting dictionary no longer returns free variables. (Free variables, in this case, are variables referenced in the class statement that aren’t attributes of the class.)

性能优化

The warnings module has been rewritten in C. This makes it possible to invoke warnings from the parser, and may also make the interpreter’s startup faster. (Contributed by Neal Norwitz and Brett Cannon; bpo-1631171 [https://bugs.python.org/issue1631171].)

Type objects now have a cache of methods that can reduce the work required to find the correct method implementation for a particular class; once cached, the interpreter doesn’t need to traverse base classes to figure out the right method to call. The cache is cleared if a base class or the class itself is modified, so the cache should remain correct even in the face of Python’s dynamic nature. (Original optimization implemented by Armin Rigo, updated for Python 2.6 by Kevin Jacobs; bpo-1700288 [https://bugs.python.org/issue1700288].)

By default, this change is only applied to types that are included with the Python core. Extension modules may not necessarily be compatible with this cache, so they must explicitly add Py_TPFLAGS_HAVE_VERSION_TAG to the module’s tp_flags field to enable the method cache. (To be compatible with the method cache, the extension module’s code must not directly access and modify the tp_dict member of any of the types it implements. Most modules don’t do this, but it’s impossible for the Python interpreter to determine that. See bpo-1878 [https://bugs.python.org/issue1878] for some discussion.)

Function calls that use keyword arguments are significantly faster by doing a quick pointer comparison, usually saving the time of a full string comparison. (Contributed by Raymond Hettinger, after an initial implementation by Antoine Pitrou; bpo-1819 [https://bugs.python.org/issue1819].)

All of the functions in the struct module have been rewritten in C, thanks to work at the Need For Speed sprint. (Contributed by Raymond Hettinger.)

Some of the standard built-in types now set a bit in their type objects. This speeds up checking whether an object is a subclass of one of these types. (Contributed by Neal Norwitz.)

Unicode strings now use faster code for detecting whitespace and line breaks; this speeds up the split() method by about 25% and splitlines() by 35%. (Contributed by Antoine Pitrou.) Memory usage is reduced by using pymalloc for the Unicode string’s data.

The with statement now stores the __exit__() method on the stack, producing a small speedup. (Implemented by Jeffrey Yasskin.)

To reduce memory usage, the garbage collector will now clear internal free lists when garbage-collecting the highest generation of objects. This may return memory to the operating system sooner.

Interpreter Changes

Two command-line options have been reserved for use by other Python implementations. The -J switch has been reserved for use by Jython for Jython-specific options, such as switches that are passed to the underlying JVM. -X has been reserved for options specific to a particular implementation of Python such as CPython, Jython, or IronPython. If either option is used with Python 2.6, the interpreter will report that the option isn’t currently used.

Python can now be prevented from writing .pyc or .pyo files by supplying the -B switch to the Python interpreter, or by setting the PYTHONDONTWRITEBYTECODE environment variable before running the interpreter. This setting is available to Python programs as the sys.dont_write_bytecode variable, and Python code can change the value to modify the interpreter’s behaviour. (Contributed by Neal Norwitz and Georg Brandl.)

The encoding used for standard input, output, and standard error can be specified by setting the PYTHONIOENCODING environment variable before running the interpreter. The value should be a string in the form <encoding> or <encoding>:<errorhandler>. The encoding part specifies the encoding’s name, e.g. utf-8 or latin-1; the optional errorhandler part specifies what to do with characters that can’t be handled by the encoding, and should be one of “error”, “ignore”, or “replace”. (Contributed by Martin von Löwis.)

新增和改进的模块

As in every release, Python’s standard library received a number of enhancements and bug fixes. Here’s a partial list of the most notable changes, sorted alphabetically by module name. Consult the Misc/NEWS file in the source tree for a more complete list of changes, or look through the Subversion logs for all the details.

The asyncore and asynchat modules are being actively maintained again, and a number of patches and bugfixes were applied. (Maintained by Josiah Carlson; see bpo-1736190 [https://bugs.python.org/issue1736190] for one patch.)

The bsddb module also has a new maintainer, Jesús Cea Avión, and the package is now available as a standalone package. The web page for the package is www.jcea.es/programacion/pybsddb.htm [https://www.jcea.es/programacion/pybsddb.htm]. The plan is to remove the package from the standard library in Python 3.0, because its pace of releases is much more frequent than Python’s.

The bsddb.dbshelve module now uses the highest pickling protocol available, instead of restricting itself to protocol 1. (Contributed by W. Barnes.)

The cgi module will now read variables from the query string of an HTTP POST request. This makes it possible to use form actions with URLs that include query strings such as “/cgi-bin/add.py?category=1”. (Contributed by Alexandre Fiori and Nubis; bpo-1817 [https://bugs.python.org/issue1817].)

The parse_qs() and parse_qsl() functions have been relocated from the cgi module to the urlparse module. The versions still available in the cgi module will trigger PendingDeprecationWarning messages in 2.6 (bpo-600362 [https://bugs.python.org/issue600362]).

The cmath module underwent extensive revision, contributed by Mark Dickinson and Christian Heimes. Five new functions were added:

polar() converts a complex number to polar form, returning the modulus and argument of the complex number.

rect() does the opposite, turning a modulus, argument pair back into the corresponding complex number.

phase() returns the argument (also called the angle) of a complex number.

isnan() returns True if either the real or imaginary part of its argument is a NaN.

isinf() returns True if either the real or imaginary part of its argument is infinite.

The revisions also improved the numerical soundness of the cmath module. For all functions, the real and imaginary parts of the results are accurate to within a few units of least precision (ulps) whenever possible. See bpo-1381 [https://bugs.python.org/issue1381] for the details. The branch cuts for asinh(), atanh(): and atan() have also been corrected.

The tests for the module have been greatly expanded; nearly 2000 new test cases exercise the algebraic functions.

On IEEE 754 platforms, the cmath module now handles IEEE 754 special values and floating-point exceptions in a manner consistent with Annex ‘G’ of the C99 standard.

A new data type in the collections module: namedtuple(typename, fieldnames) is a factory function that creates subclasses of the standard tuple whose fields are accessible by name as well as index. For example:

>>> var_type = collections.namedtuple('variable',
...             'id name type size')
>>> # Names are separated by spaces or commas.
>>> # 'id, name, type, size' would also work.
>>> var_type._fields
('id', 'name', 'type', 'size')

>>> var = var_type(1, 'frequency', 'int', 4)
>>> print var[0], var.id    # Equivalent
1 1
>>> print var[2], var.type  # Equivalent
int int
>>> var._asdict()
{'size': 4, 'type': 'int', 'id': 1, 'name': 'frequency'}
>>> v2 = var._replace(name='amplitude')
>>> v2
variable(id=1, name='amplitude', type='int', size=4)

Several places in the standard library that returned tuples have been modified to return namedtuple instances. For example, the Decimal.as_tuple() method now returns a named tuple with sign, digits, and exponent fields.

（由 Raymond Hettinger 贡献。）

Another change to the collections module is that the deque type now supports an optional maxlen parameter; if supplied, the deque’s size will be restricted to no more than maxlen items. Adding more items to a full deque causes old items to be discarded.

>>> from collections import deque
>>> dq=deque(maxlen=3)
>>> dq
deque([], maxlen=3)
>>> dq.append(1); dq.append(2); dq.append(3)
>>> dq
deque([1, 2, 3], maxlen=3)
>>> dq.append(4)
>>> dq
deque([2, 3, 4], maxlen=3)

（由 Raymond Hettinger 贡献。）

The Cookie module’s Morsel objects now support an httponly attribute. In some browsers. cookies with this attribute set cannot be accessed or manipulated by JavaScript code. (Contributed by Arvin Schnell; bpo-1638033 [https://bugs.python.org/issue1638033].)

A new window method in the curses module, chgat(), changes the display attributes for a certain number of characters on a single line. (Contributed by Fabian Kreutz.)

# Boldface text starting at y=0,x=21
# and affecting the rest of the line.
stdscr.chgat(0, 21, curses.A_BOLD)

The Textbox class in the curses.textpad module now supports editing in insert mode as well as overwrite mode. Insert mode is enabled by supplying a true value for the insert_mode parameter when creating the Textbox instance.

The datetime module’s strftime() methods now support a %f format code that expands to the number of microseconds in the object, zero-padded on the left to six places. (Contributed by Skip Montanaro; bpo-1158 [https://bugs.python.org/issue1158].)

The decimal module was updated to version 1.66 of the General Decimal Specification [http://speleotrove.com/decimal/decarith.html]. New features include some methods for some basic mathematical functions such as exp() and log10():

>>> Decimal(1).exp()
Decimal("2.718281828459045235360287471")
>>> Decimal("2.7182818").ln()
Decimal("0.9999999895305022877376682436")
>>> Decimal(1000).log10()
Decimal("3")

The as_tuple() method of Decimal objects now returns a named tuple with sign, digits, and exponent fields.

(Implemented by Facundo Batista and Mark Dickinson. Named tuple support added by Raymond Hettinger.)

The difflib module’s SequenceMatcher class now returns named tuples representing matches, with a, b, and size attributes. (Contributed by Raymond Hettinger.)

An optional timeout parameter, specifying a timeout measured in seconds, was added to the ftplib.FTP class constructor as well as the connect() method. (Added by Facundo Batista.) Also, the FTP class’s storbinary() and storlines() now take an optional callback parameter that will be called with each block of data after the data has been sent. (Contributed by Phil Schwartz; bpo-1221598 [https://bugs.python.org/issue1221598].)

The reduce() built-in function is also available in the functools module. In Python 3.0, the builtin has been dropped and reduce() is only available from functools; currently there are no plans to drop the builtin in the 2.x series. (Patched by Christian Heimes; bpo-1739906 [https://bugs.python.org/issue1739906].)

When possible, the getpass module will now use /dev/tty to print a prompt message and read the password, falling back to standard error and standard input. If the password may be echoed to the terminal, a warning is printed before the prompt is displayed. (Contributed by Gregory P. Smith.)

The glob.glob() function can now return Unicode filenames if a Unicode path was used and Unicode filenames are matched within the directory. (bpo-1001604 [https://bugs.python.org/issue1001604])

A new function in the heapq module, merge(iter1, iter2, ...), takes any number of iterables returning data in sorted order, and returns a new generator that returns the contents of all the iterators, also in sorted order. For example:

>>> list(heapq.merge([1, 3, 5, 9], [2, 8, 16]))
[1, 2, 3, 5, 8, 9, 16]

Another new function, heappushpop(heap, item), pushes item onto heap, then pops off and returns the smallest item. This is more efficient than making a call to heappush() and then heappop().

heapq is now implemented to only use less-than comparison, instead of the less-than-or-equal comparison it previously used. This makes heapq’s usage of a type match the list.sort() method. (Contributed by Raymond Hettinger.)

An optional timeout parameter, specifying a timeout measured in seconds, was added to the httplib.HTTPConnection and HTTPSConnection class constructors. (Added by Facundo Batista.)

Most of the inspect module’s functions, such as getmoduleinfo() and getargs(), now return named tuples. In addition to behaving like tuples, the elements of the return value can also be accessed as attributes. (Contributed by Raymond Hettinger.)

Some new functions in the module include isgenerator(), isgeneratorfunction(), and isabstract().

The itertools module gained several new functions.

izip_longest(iter1, iter2, ...[, fillvalue]) makes tuples from each of the elements; if some of the iterables are shorter than others, the missing values are set to fillvalue. For example:

>>> tuple(itertools.izip_longest([1,2,3], [1,2,3,4,5]))
((1, 1), (2, 2), (3, 3), (None, 4), (None, 5))

product(iter1, iter2, ..., [repeat=N]) returns the Cartesian product of the supplied iterables, a set of tuples containing every possible combination of the elements returned from each iterable.

>>> list(itertools.product([1,2,3], [4,5,6]))
[(1, 4), (1, 5), (1, 6),
 (2, 4), (2, 5), (2, 6),
 (3, 4), (3, 5), (3, 6)]

The optional repeat keyword argument is used for taking the product of an iterable or a set of iterables with themselves, repeated N times. With a single iterable argument, N-tuples are returned:

>>> list(itertools.product([1,2], repeat=3))
[(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2),
 (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]

With two iterables, 2N-tuples are returned.

>>> list(itertools.product([1,2], [3,4], repeat=2))
[(1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 3), (1, 3, 2, 4),
 (1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 3), (1, 4, 2, 4),
 (2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 3), (2, 3, 2, 4),
 (2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 3), (2, 4, 2, 4)]

combinations(iterable, r) returns sub-sequences of length r from the elements of iterable.

>>> list(itertools.combinations('123', 2))
[('1', '2'), ('1', '3'), ('2', '3')]
>>> list(itertools.combinations('123', 3))
[('1', '2', '3')]
>>> list(itertools.combinations('1234', 3))
[('1', '2', '3'), ('1', '2', '4'),
 ('1', '3', '4'), ('2', '3', '4')]

permutations(iter[, r]) returns all the permutations of length r of the iterable’s elements. If r is not specified, it will default to the number of elements produced by the iterable.

>>> list(itertools.permutations([1,2,3,4], 2))
[(1, 2), (1, 3), (1, 4),
 (2, 1), (2, 3), (2, 4),
 (3, 1), (3, 2), (3, 4),
 (4, 1), (4, 2), (4, 3)]

itertools.chain(*iterables) is an existing function in itertools that gained a new constructor in Python 2.6. itertools.chain.from_iterable(iterable) takes a single iterable that should return other iterables. chain() will then return all the elements of the first iterable, then all the elements of the second, and so on.

>>> list(itertools.chain.from_iterable([[1,2,3], [4,5,6]]))
[1, 2, 3, 4, 5, 6]

(All contributed by Raymond Hettinger.)

The logging module’s FileHandler class and its subclasses WatchedFileHandler, RotatingFileHandler, and TimedRotatingFileHandler now have an optional delay parameter to their constructors. If delay is true, opening of the log file is deferred until the first emit() call is made. (Contributed by Vinay Sajip.)

TimedRotatingFileHandler also has a utc constructor parameter. If the argument is true, UTC time will be used in determining when midnight occurs and in generating filenames; otherwise local time will be used.

Several new functions were added to the math module:

isinf() and isnan() determine whether a given float is a (positive or negative) infinity or a NaN (Not a Number), respectively.

copysign() copies the sign bit of an IEEE 754 number, returning the absolute value of x combined with the sign bit of y. For example, math.copysign(1, -0.0) returns -1.0. (Contributed by Christian Heimes.)

factorial() computes the factorial of a number. (Contributed by Raymond Hettinger; bpo-2138 [https://bugs.python.org/issue2138].)

fsum() adds up the stream of numbers from an iterable, and is careful to avoid loss of precision through using partial sums. (Contributed by Jean Brouwers, Raymond Hettinger, and Mark Dickinson; bpo-2819 [https://bugs.python.org/issue2819].)

acosh(), asinh() and atanh() compute the inverse hyperbolic functions.

log1p() returns the natural logarithm of 1+x (base e).

trunc() rounds a number toward zero, returning the closest Integral that’s between the function’s argument and zero. Added as part of the backport of PEP 3141’s type hierarchy for numbers.

The math module has been improved to give more consistent behaviour across platforms, especially with respect to handling of floating-point exceptions and IEEE 754 special values.

Whenever possible, the module follows the recommendations of the C99 standard about 754’s special values. For example, sqrt(-1.) should now give a ValueError across almost all platforms, while sqrt(float('NaN')) should return a NaN on all IEEE 754 platforms. Where Annex ‘F’ of the C99 standard recommends signaling ‘divide-by-zero’ or ‘invalid’, Python will raise ValueError. Where Annex ‘F’ of the C99 standard recommends signaling ‘overflow’, Python will raise OverflowError. (See bpo-711019 [https://bugs.python.org/issue711019] and bpo-1640 [https://bugs.python.org/issue1640].)

（由 Christian Heimes 和 Mark Dickinson 贡献。）

mmap objects now have a rfind() method that searches for a substring beginning at the end of the string and searching backwards. The find() method also gained an end parameter giving an index at which to stop searching. (Contributed by John Lenton.)

The operator module gained a methodcaller() function that takes a name and an optional set of arguments, returning a callable that will call the named function on any arguments passed to it. For example:

>>> # Equivalent to lambda s: s.replace('old', 'new')
>>> replacer = operator.methodcaller('replace', 'old', 'new')
>>> replacer('old wine in old bottles')
'new wine in new bottles'

（由 Gregory Petrosyan 提供建议，之后由 Georg Brandl 贡献。）

The attrgetter() function now accepts dotted names and performs the corresponding attribute lookups:

>>> inst_name = operator.attrgetter(
...        '__class__.__name__')
>>> inst_name('')
'str'
>>> inst_name(help)
'_Helper'

（由 Barry Warsaw 提供建议，之后由 Georg Brandl 贡献。）

The os module now wraps several new system calls. fchmod(fd, mode) and fchown(fd, uid, gid) change the mode and ownership of an opened file, and lchmod(path, mode) changes the mode of a symlink. (Contributed by Georg Brandl and Christian Heimes.)

chflags() and lchflags() are wrappers for the corresponding system calls (where they’re available), changing the flags set on a file. Constants for the flag values are defined in the stat module; some possible values include UF_IMMUTABLE to signal the file may not be changed and UF_APPEND to indicate that data can only be appended to the file. (Contributed by M. Levinson.)

os.closerange(low, high) efficiently closes all file descriptors from low to high, ignoring any errors and not including high itself. This function is now used by the subprocess module to make starting processes faster. (Contributed by Georg Brandl; bpo-1663329 [https://bugs.python.org/issue1663329].)

The os.environ object’s clear() method will now unset the environment variables using os.unsetenv() in addition to clearing the object’s keys. (Contributed by Martin Horcicka; bpo-1181 [https://bugs.python.org/issue1181].)

The os.walk() function now has a followlinks parameter. If set to True, it will follow symlinks pointing to directories and visit the directory’s contents. For backward compatibility, the parameter’s default value is false. Note that the function can fall into an infinite recursion if there’s a symlink that points to a parent directory. (bpo-1273829 [https://bugs.python.org/issue1273829])

In the os.path module, the splitext() function has been changed to not split on leading period characters. This produces better results when operating on Unix’s dot-files. For example, os.path.splitext('.ipython') now returns ('.ipython', '') instead of ('', '.ipython'). (bpo-1115886 [https://bugs.python.org/issue1115886])

A new function, os.path.relpath(path, start='.'), returns a relative path from the start path, if it’s supplied, or from the current working directory to the destination path. (Contributed by Richard Barran; bpo-1339796 [https://bugs.python.org/issue1339796].)

On Windows, os.path.expandvars() will now expand environment variables given in the form “%var%”, and “~user” will be expanded into the user’s home directory path. (Contributed by Josiah Carlson; bpo-957650 [https://bugs.python.org/issue957650].)

The Python debugger provided by the pdb module gained a new command: “run” restarts the Python program being debugged and can optionally take new command-line arguments for the program. (Contributed by Rocky Bernstein; bpo-1393667 [https://bugs.python.org/issue1393667].)

The pdb.post_mortem() function, used to begin debugging a traceback, will now use the traceback returned by sys.exc_info() if no traceback is supplied. (Contributed by Facundo Batista; bpo-1106316 [https://bugs.python.org/issue1106316].)

The pickletools module now has an optimize() function that takes a string containing a pickle and removes some unused opcodes, returning a shorter pickle that contains the same data structure. (Contributed by Raymond Hettinger.)

A get_data() function was added to the pkgutil module that returns the contents of resource files included with an installed Python package. For example:

>>> import pkgutil
>>> print pkgutil.get_data('test', 'exception_hierarchy.txt')
BaseException
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- GeneratorExit
 +-- Exception
      +-- StopIteration
      +-- StandardError
 ...

（由 Paul Moore 在 bpo-2439 [https://bugs.python.org/issue2439] 中贡献。）

The pyexpat module’s Parser objects now allow setting their buffer_size attribute to change the size of the buffer used to hold character data. (Contributed by Achim Gaedke; bpo-1137 [https://bugs.python.org/issue1137].)

The Queue module now provides queue variants that retrieve entries in different orders. The PriorityQueue class stores queued items in a heap and retrieves them in priority order, and LifoQueue retrieves the most recently added entries first, meaning that it behaves like a stack. (Contributed by Raymond Hettinger.)

The random module’s Random objects can now be pickled on a 32-bit system and unpickled on a 64-bit system, and vice versa. Unfortunately, this change also means that Python 2.6’s Random objects can’t be unpickled correctly on earlier versions of Python. (Contributed by Shawn Ligocki; bpo-1727780 [https://bugs.python.org/issue1727780].)

The new triangular(low, high, mode) function returns random numbers following a triangular distribution. The returned values are between low and high, not including high itself, and with mode as the most frequently occurring value in the distribution. (Contributed by Wladmir van der Laan and Raymond Hettinger; bpo-1681432 [https://bugs.python.org/issue1681432].)

Long regular expression searches carried out by the re module will check for signals being delivered, so time-consuming searches can now be interrupted. (Contributed by Josh Hoyt and Ralf Schmitt; bpo-846388 [https://bugs.python.org/issue846388].)

The regular expression module is implemented by compiling bytecodes for a tiny regex-specific virtual machine. Untrusted code could create malicious strings of bytecode directly and cause crashes, so Python 2.6 includes a verifier for the regex bytecode. (Contributed by Guido van Rossum from work for Google App Engine; bpo-3487 [https://bugs.python.org/issue3487].)

The rlcompleter module’s Completer.complete() method will now ignore exceptions triggered while evaluating a name. (Fixed by Lorenz Quack; bpo-2250 [https://bugs.python.org/issue2250].)

The sched module’s scheduler instances now have a read-only queue attribute that returns the contents of the scheduler’s queue, represented as a list of named tuples with the fields (time, priority, action, argument). (Contributed by Raymond Hettinger; bpo-1861 [https://bugs.python.org/issue1861].)

The select module now has wrapper functions for the Linux epoll() and BSD kqueue() system calls. modify() method was added to the existing poll objects; pollobj.modify(fd, eventmask) takes a file descriptor or file object and an event mask, modifying the recorded event mask for that file. (Contributed by Christian Heimes; bpo-1657 [https://bugs.python.org/issue1657].)

The shutil.copytree() function now has an optional ignore argument that takes a callable object. This callable will receive each directory path and a list of the directory’s contents, and returns a list of names that will be ignored, not copied.

The shutil module also provides an ignore_patterns() function for use with this new parameter. ignore_patterns() takes an arbitrary number of glob-style patterns and returns a callable that will ignore any files and directories that match any of these patterns. The following example copies a directory tree, but skips both .svn directories and Emacs backup files, which have names ending with ‘~’:

shutil.copytree('Doc/library', '/tmp/library',
                ignore=shutil.ignore_patterns('*~', '.svn'))

（由 Tarek Ziadé 在 bpo-2663 [https://bugs.python.org/issue2663] 中贡献。）

Integrating signal handling with GUI handling event loops like those used by Tkinter or GTk+ has long been a problem; most software ends up polling, waking up every fraction of a second to check if any GUI events have occurred. The signal module can now make this more efficient. Calling signal.set_wakeup_fd(fd) sets a file descriptor to be used; when a signal is received, a byte is written to that file descriptor. There’s also a C-level function, PySignal_SetWakeupFd(), for setting the descriptor.

Event loops will use this by opening a pipe to create two descriptors, one for reading and one for writing. The writable descriptor will be passed to set_wakeup_fd(), and the readable descriptor will be added to the list of descriptors monitored by the event loop via select() or poll(). On receiving a signal, a byte will be written and the main event loop will be woken up, avoiding the need to poll.

（由 Adam Olsen 在 bpo-1583 [https://bugs.python.org/issue1583] 中贡献。）

The siginterrupt() function is now available from Python code, and allows changing whether signals can interrupt system calls or not. (Contributed by Ralf Schmitt.)

The setitimer() and getitimer() functions have also been added (where they’re available). setitimer() allows setting interval timers that will cause a signal to be delivered to the process after a specified time, measured in wall-clock time, consumed process time, or combined process+system time. (Contributed by Guilherme Polo; bpo-2240 [https://bugs.python.org/issue2240].)

The smtplib module now supports SMTP over SSL thanks to the addition of the SMTP_SSL class. This class supports an interface identical to the existing SMTP class. (Contributed by Monty Taylor.) Both class constructors also have an optional timeout parameter that specifies a timeout for the initial connection attempt, measured in seconds. (Contributed by Facundo Batista.)

An implementation of the LMTP protocol (RFC 2033 [https://tools.ietf.org/html/rfc2033.html]) was also added to the module. LMTP is used in place of SMTP when transferring e-mail between agents that don’t manage a mail queue. (LMTP implemented by Leif Hedstrom; bpo-957003 [https://bugs.python.org/issue957003].)

SMTP.starttls() now complies with RFC 3207 [https://tools.ietf.org/html/rfc3207.html] and forgets any knowledge obtained from the server not obtained from the TLS negotiation itself. (Patch contributed by Bill Fenner; bpo-829951 [https://bugs.python.org/issue829951].)

The socket module now supports TIPC (http://tipc.sourceforge.net/), a high-performance non-IP-based protocol designed for use in clustered environments. TIPC addresses are 4- or 5-tuples. (Contributed by Alberto Bertogli; bpo-1646 [https://bugs.python.org/issue1646].)

A new function, create_connection(), takes an address and connects to it using an optional timeout value, returning the connected socket object. This function also looks up the address’s type and connects to it using IPv4 or IPv6 as appropriate. Changing your code to use create_connection() instead of socket(socket.AF_INET, ...) may be all that’s required to make your code work with IPv6.

The base classes in the SocketServer module now support calling a handle_timeout() method after a span of inactivity specified by the server’s timeout attribute. (Contributed by Michael Pomraning.) The serve_forever() method now takes an optional poll interval measured in seconds, controlling how often the server will check for a shutdown request. (Contributed by Pedro Werneck and Jeffrey Yasskin; bpo-742598 [https://bugs.python.org/issue742598], bpo-1193577 [https://bugs.python.org/issue1193577].)

The sqlite3 module, maintained by Gerhard Häring, has been updated from version 2.3.2 in Python 2.5 to version 2.4.1.

The struct module now supports the C99 _Bool type, using the format character '?'. (Contributed by David Remahl.)

The Popen objects provided by the subprocess module now have terminate(), kill(), and send_signal() methods. On Windows, send_signal() only supports the SIGTERM signal, and all these methods are aliases for the Win32 API function TerminateProcess(). (Contributed by Christian Heimes.)

A new variable in the sys module, float_info, is an object containing information derived from the float.h file about the platform’s floating-point support. Attributes of this object include mant_dig (number of digits in the mantissa), epsilon (smallest difference between 1.0 and the next largest value representable), and several others. (Contributed by Christian Heimes; bpo-1534 [https://bugs.python.org/issue1534].)

Another new variable, dont_write_bytecode, controls whether Python writes any .pyc or .pyo files on importing a module. If this variable is true, the compiled files are not written. The variable is initially set on start-up by supplying the -B switch to the Python interpreter, or by setting the PYTHONDONTWRITEBYTECODE environment variable before running the interpreter. Python code can subsequently change the value of this variable to control whether bytecode files are written or not. (Contributed by Neal Norwitz and Georg Brandl.)

Information about the command-line arguments supplied to the Python interpreter is available by reading attributes of a named tuple available as sys.flags. For example, the verbose attribute is true if Python was executed in verbose mode, debug is true in debugging mode, etc. These attributes are all read-only. (Contributed by Christian Heimes.)

A new function, getsizeof(), takes a Python object and returns the amount of memory used by the object, measured in bytes. Built-in objects return correct results; third-party extensions may not, but can define a __sizeof__() method to return the object’s size. (Contributed by Robert Schuppenies; bpo-2898 [https://bugs.python.org/issue2898].)

It’s now possible to determine the current profiler and tracer functions by calling sys.getprofile() and sys.gettrace(). (Contributed by Georg Brandl; bpo-1648 [https://bugs.python.org/issue1648].)

The tarfile module now supports POSIX.1-2001 (pax) tarfiles in addition to the POSIX.1-1988 (ustar) and GNU tar formats that were already supported. The default format is GNU tar; specify the format parameter to open a file using a different format:

tar = tarfile.open("output.tar", "w",
                   format=tarfile.PAX_FORMAT)

The new encoding and errors parameters specify an encoding and an error handling scheme for character conversions. 'strict', 'ignore', and 'replace' are the three standard ways Python can handle errors,; 'utf-8' is a special value that replaces bad characters with their UTF-8 representation. (Character conversions occur because the PAX format supports Unicode filenames, defaulting to UTF-8 encoding.)

The TarFile.add() method now accepts an exclude argument that’s a function that can be used to exclude certain filenames from an archive. The function must take a filename and return true if the file should be excluded or false if it should be archived. The function is applied to both the name initially passed to add() and to the names of files in recursively-added directories.

(All changes contributed by Lars Gustäbel).

An optional timeout parameter was added to the telnetlib.Telnet class constructor, specifying a timeout measured in seconds. (Added by Facundo Batista.)

The tempfile.NamedTemporaryFile class usually deletes the temporary file it created when the file is closed. This behaviour can now be changed by passing delete=False to the constructor. (Contributed by Damien Miller; bpo-1537850 [https://bugs.python.org/issue1537850].)

A new class, SpooledTemporaryFile, behaves like a temporary file but stores its data in memory until a maximum size is exceeded. On reaching that limit, the contents will be written to an on-disk temporary file. (Contributed by Dustin J. Mitchell.)

The NamedTemporaryFile and SpooledTemporaryFile classes both work as context managers, so you can write with tempfile.NamedTemporaryFile() as tmp: .... (Contributed by Alexander Belopolsky; bpo-2021 [https://bugs.python.org/issue2021].)

The test.test_support module gained a number of context managers useful for writing tests. EnvironmentVarGuard() is a context manager that temporarily changes environment variables and automatically restores them to their old values.

Another context manager, TransientResource, can surround calls to resources that may or may not be available; it will catch and ignore a specified list of exceptions. For example, a network test may ignore certain failures when connecting to an external web site:

with test_support.TransientResource(IOError,
                                errno=errno.ETIMEDOUT):
    f = urllib.urlopen('https://sf.net')
    ...

Finally, check_warnings() resets the warning module’s warning filters and returns an object that will record all warning messages triggered (bpo-3781 [https://bugs.python.org/issue3781]):

with test_support.check_warnings() as wrec:
    warnings.simplefilter("always")
    # ... code that triggers a warning ...
    assert str(wrec.message) == "function is outdated"
    assert len(wrec.warnings) == 1, "Multiple warnings raised"

（由 Brett Cannon 贡献。）

The textwrap module can now preserve existing whitespace at the beginnings and ends of the newly-created lines by specifying drop_whitespace=False as an argument:

>>> S = """This  sentence  has a bunch   of
...   extra   whitespace."""
>>> print textwrap.fill(S, width=15)
This  sentence
has a bunch
of    extra
whitespace.
>>> print textwrap.fill(S, drop_whitespace=False, width=15)
This  sentence
  has a bunch
   of    extra
   whitespace.
>>>

（由 Dwayne Bailey 在 bpo-1581073 [https://bugs.python.org/issue1581073] 中贡献。）

The threading module API is being changed to use properties such as daemon instead of setDaemon() and isDaemon() methods, and some methods have been renamed to use underscores instead of camel-case; for example, the activeCount() method is renamed to active_count(). Both the 2.6 and 3.0 versions of the module support the same properties and renamed methods, but don’t remove the old methods. No date has been set for the deprecation of the old APIs in Python 3.x; the old APIs won’t be removed in any 2.x version. (Carried out by several people, most notably Benjamin Peterson.)

The threading module’s Thread objects gained an ident property that returns the thread’s identifier, a nonzero integer. (Contributed by Gregory P. Smith; bpo-2871 [https://bugs.python.org/issue2871].)

The timeit module now accepts callables as well as strings for the statement being timed and for the setup code. Two convenience functions were added for creating Timer instances: repeat(stmt, setup, time, repeat, number) and timeit(stmt, setup, time, number) create an instance and call the corresponding method. (Contributed by Erik Demaine; bpo-1533909 [https://bugs.python.org/issue1533909].)

The Tkinter module now accepts lists and tuples for options, separating the elements by spaces before passing the resulting value to Tcl/Tk. (Contributed by Guilherme Polo; bpo-2906 [https://bugs.python.org/issue2906].)

The turtle module for turtle graphics was greatly enhanced by Gregor Lingl. New features in the module include:

Better animation of turtle movement and rotation.

Control over turtle movement using the new delay(), tracer(), and speed() methods.

The ability to set new shapes for the turtle, and to define a new coordinate system.

Turtles now have an undo() method that can roll back actions.

Simple support for reacting to input events such as mouse and keyboard activity, making it possible to write simple games.

A turtle.cfg file can be used to customize the starting appearance of the turtle’s screen.

The module’s docstrings can be replaced by new docstrings that have been translated into another language.

(bpo-1513695 [https://bugs.python.org/issue1513695])

An optional timeout parameter was added to the urllib.urlopen() function and the urllib.ftpwrapper class constructor, as well as the urllib2.urlopen() function. The parameter specifies a timeout measured in seconds. For example:

>>> u = urllib2.urlopen("http://slow.example.com",
                        timeout=3)
Traceback (most recent call last):
  ...
urllib2.URLError: <urlopen error timed out>
>>>

(Added by Facundo Batista.)

The Unicode database provided by the unicodedata module has been updated to version 5.1.0. (Updated by Martin von Löwis; bpo-3811 [https://bugs.python.org/issue3811].)

The warnings module’s formatwarning() and showwarning() gained an optional line argument that can be used to supply the line of source code. (Added as part of bpo-1631171 [https://bugs.python.org/issue1631171], which re-implemented part of the warnings module in C code.)

A new function, catch_warnings(), is a context manager intended for testing purposes that lets you temporarily modify the warning filters and then restore their original values (bpo-3781 [https://bugs.python.org/issue3781]).

The XML-RPC SimpleXMLRPCServer and DocXMLRPCServer classes can now be prevented from immediately opening and binding to their socket by passing False as the bind_and_activate constructor parameter. This can be used to modify the instance’s allow_reuse_address attribute before calling the server_bind() and server_activate() methods to open the socket and begin listening for connections. (Contributed by Peter Parente; bpo-1599845 [https://bugs.python.org/issue1599845].)

SimpleXMLRPCServer also has a _send_traceback_header attribute; if true, the exception and formatted traceback are returned as HTTP headers “X-Exception” and “X-Traceback”. This feature is for debugging purposes only and should not be used on production servers because the tracebacks might reveal passwords or other sensitive information. (Contributed by Alan McIntyre as part of his project for Google’s Summer of Code 2007.)

The xmlrpclib module no longer automatically converts datetime.date and datetime.time to the xmlrpclib.DateTime type; the conversion semantics were not necessarily correct for all applications. Code using xmlrpclib should convert date and time instances. (bpo-1330538 [https://bugs.python.org/issue1330538]) The code can also handle dates before 1900 (contributed by Ralf Schmitt; bpo-2014 [https://bugs.python.org/issue2014]) and 64-bit integers represented by using <i8> in XML-RPC responses (contributed by Riku Lindblad; bpo-2985 [https://bugs.python.org/issue2985]).

The zipfile module’s ZipFile class now has extract() and extractall() methods that will unpack a single file or all the files in the archive to the current directory, or to a specified directory:

z = zipfile.ZipFile('python-251.zip')

# Unpack a single file, writing it relative
# to the /tmp directory.
z.extract('Python/sysmodule.c', '/tmp')

# Unpack all the files in the archive.
z.extractall()

（由 Alan McIntyre 在 bpo-467924 [https://bugs.python.org/issue467924] 中贡献。）

The open(), read() and extract() methods can now take either a filename or a ZipInfo object. This is useful when an archive accidentally contains a duplicated filename. (Contributed by Graham Horler; bpo-1775025 [https://bugs.python.org/issue1775025].)

Finally, zipfile now supports using Unicode filenames for archived files. (Contributed by Alexey Borzenkov; bpo-1734346 [https://bugs.python.org/issue1734346].)

ast 模块

The ast module provides an Abstract Syntax Tree representation of Python code, and Armin Ronacher contributed a set of helper functions that perform a variety of common tasks. These will be useful for HTML templating packages, code analyzers, and similar tools that process Python code.

The parse() function takes an expression and returns an AST. The dump() function outputs a representation of a tree, suitable for debugging:

import ast

t = ast.parse("""
d = {}
for i in 'abcdefghijklm':
    d[i + i] = ord(i) - ord('a') + 1
print d
""")
print ast.dump(t)

This outputs a deeply nested tree:

Module(body=[
  Assign(targets=[
    Name(id='d', ctx=Store())
   ], value=Dict(keys=[], values=[]))
  For(target=Name(id='i', ctx=Store()),
      iter=Str(s='abcdefghijklm'), body=[
    Assign(targets=[
      Subscript(value=
        Name(id='d', ctx=Load()),
          slice=
          Index(value=
            BinOp(left=Name(id='i', ctx=Load()), op=Add(),
             right=Name(id='i', ctx=Load()))), ctx=Store())
     ], value=
     BinOp(left=
      BinOp(left=
       Call(func=
        Name(id='ord', ctx=Load()), args=[
          Name(id='i', ctx=Load())
         ], keywords=[], starargs=None, kwargs=None),
       op=Sub(), right=Call(func=
        Name(id='ord', ctx=Load()), args=[
          Str(s='a')
         ], keywords=[], starargs=None, kwargs=None)),
       op=Add(), right=Num(n=1)))
    ], orelse=[])
   Print(dest=None, values=[
     Name(id='d', ctx=Load())
   ], nl=True)
 ])

The literal_eval() method takes a string or an AST representing a literal expression, parses and evaluates it, and returns the resulting value. A literal expression is a Python expression containing only strings, numbers, dictionaries, etc. but no statements or function calls. If you need to evaluate an expression but cannot accept the security risk of using an eval() call, literal_eval() will handle it safely:

>>> literal = '("a", "b", {2:4, 3:8, 1:2})'
>>> print ast.literal_eval(literal)
('a', 'b', {1: 2, 2: 4, 3: 8})
>>> print ast.literal_eval('"a" + "b"')
Traceback (most recent call last):
  ...
ValueError: malformed string

The module also includes NodeVisitor and NodeTransformer classes for traversing and modifying an AST, and functions for common transformations such as changing line numbers.

future_builtins 模块

Python 3.0 makes many changes to the repertoire of built-in functions, and most of the changes can’t be introduced in the Python 2.x series because they would break compatibility. The future_builtins module provides versions of these built-in functions that can be imported when writing 3.0-compatible code.

The functions in this module currently include:

ascii(obj): equivalent to repr(). In Python 3.0, repr() will return a Unicode string, while ascii() will return a pure ASCII bytestring.

filter(predicate, iterable), map(func, iterable1, ...): the 3.0 versions return iterators, unlike the 2.x builtins which return lists.

hex(value), oct(value): instead of calling the __hex__() or __oct__() methods, these versions will call the __index__() method and convert the result to hexadecimal or octal. oct() will use the new 0o notation for its result.

The json module: JavaScript Object Notation

The new json module supports the encoding and decoding of Python types in JSON (Javascript Object Notation). JSON is a lightweight interchange format often used in web applications. For more information about JSON, see http://www.json.org.

json comes with support for decoding and encoding most built-in Python types. The following example encodes and decodes a dictionary:

>>> import json
>>> data = {"spam": "foo", "parrot": 42}
>>> in_json = json.dumps(data) # Encode the data
>>> in_json
'{"parrot": 42, "spam": "foo"}'
>>> json.loads(in_json) # Decode into a Python object
{"spam": "foo", "parrot": 42}

It’s also possible to write your own decoders and encoders to support more types. Pretty-printing of the JSON strings is also supported.

json (originally called simplejson) was written by Bob Ippolito.

plistlib 模块：属性列表解析器

The .plist format is commonly used on Mac OS X to store basic data types (numbers, strings, lists, and dictionaries) by serializing them into an XML-based format. It resembles the XML-RPC serialization of data types.

Despite being primarily used on Mac OS X, the format has nothing Mac-specific about it and the Python implementation works on any platform that Python supports, so the plistlib module has been promoted to the standard library.

Using the module is simple:

import sys
import plistlib
import datetime

# Create data structure
data_struct = dict(lastAccessed=datetime.datetime.now(),
                   version=1,
                   categories=('Personal','Shared','Private'))

# Create string containing XML.
plist_str = plistlib.writePlistToString(data_struct)
new_struct = plistlib.readPlistFromString(plist_str)
print data_struct
print new_struct

# Write data structure to a file and read it back.
plistlib.writePlist(data_struct, '/tmp/customizations.plist')
new_struct = plistlib.readPlist('/tmp/customizations.plist')

# read/writePlist accepts file-like objects as well as paths.
plistlib.writePlist(data_struct, sys.stdout)

ctypes Enhancements

Thomas Heller continued to maintain and enhance the ctypes module.

ctypes now supports a c_bool datatype that represents the C99 bool type. (Contributed by David Remahl; bpo-1649190 [https://bugs.python.org/issue1649190].)

The ctypes string, buffer and array types have improved support for extended slicing syntax, where various combinations of (start, stop, step) are supplied. (Implemented by Thomas Wouters.)

All ctypes data types now support from_buffer() and from_buffer_copy() methods that create a ctypes instance based on a provided buffer object. from_buffer_copy() copies the contents of the object, while from_buffer() will share the same memory area.

A new calling convention tells ctypes to clear the errno or Win32 LastError variables at the outset of each wrapped call. (Implemented by Thomas Heller; bpo-1798 [https://bugs.python.org/issue1798].)

You can now retrieve the Unix errno variable after a function call. When creating a wrapped function, you can supply use_errno=True as a keyword parameter to the DLL() function and then call the module-level methods set_errno() and get_errno() to set and retrieve the error value.

The Win32 LastError variable is similarly supported by the DLL(), OleDLL(), and WinDLL() functions. You supply use_last_error=True as a keyword parameter and then call the module-level methods set_last_error() and get_last_error().

The byref() function, used to retrieve a pointer to a ctypes instance, now has an optional offset parameter that is a byte count that will be added to the returned pointer.

Improved SSL Support

Bill Janssen made extensive improvements to Python 2.6’s support for the Secure Sockets Layer by adding a new module, ssl, that’s built atop the OpenSSL [https://www.openssl.org/] library. This new module provides more control over the protocol negotiated, the X.509 certificates used, and has better support for writing SSL servers (as opposed to clients) in Python. The existing SSL support in the socket module hasn’t been removed and continues to work, though it will be removed in Python 3.0.

To use the new module, you must first create a TCP connection in the usual way and then pass it to the ssl.wrap_socket() function. It’s possible to specify whether a certificate is required, and to obtain certificate info by calling the getpeercert() method.

参见

ssl 模块的文档。

Deprecations and Removals

String exceptions have been removed. Attempting to use them raises a TypeError.

Changes to the Exception interface as dictated by PEP 352 [https://www.python.org/dev/peps/pep-0352] continue to be made. For 2.6, the message attribute is being deprecated in favor of the args attribute.

(3.0-warning mode) Python 3.0 will feature a reorganized standard library that will drop many outdated modules and rename others. Python 2.6 running in 3.0-warning mode will warn about these modules when they are imported.

The list of deprecated modules is: audiodev, bgenlocations, buildtools, bundlebuilder, Canvas, compiler, dircache, dl, fpformat, gensuitemodule, ihooks, imageop, imgfile, linuxaudiodev, mhlib, mimetools, multifile, new, pure, statvfs, sunaudiodev, test.testall, and toaiff.

gopherlib 模块已被移除。

The MimeWriter module and mimify module have been deprecated; use the email package instead.

The md5 module has been deprecated; use the hashlib module instead.

The posixfile module has been deprecated; fcntl.lockf() provides better locking.

The popen2 module has been deprecated; use the subprocess module.

rgbimg 模块已被移除。

The sets module has been deprecated; it’s better to use the built-in set and frozenset types.

The sha module has been deprecated; use the hashlib module instead.

构建和 C API 的改变

Changes to Python’s build process and to the C API include:

Python now must be compiled with C89 compilers (after 19 years!). This means that the Python source tree has dropped its own implementations of memmove() and strerror(), which are in the C89 standard library.

Python 2.6 can be built with Microsoft Visual Studio 2008 (version 9.0), and this is the new default compiler. See the PCbuild directory for the build files. (Implemented by Christian Heimes.)

On Mac OS X, Python 2.6 can be compiled as a 4-way universal build. The configure script can take a --with-universal-archs=[32-bit|64-bit|all] switch, controlling whether the binaries are built for 32-bit architectures (x86, PowerPC), 64-bit (x86-64 and PPC-64), or both. (Contributed by Ronald Oussoren.)

The BerkeleyDB module now has a C API object, available as bsddb.db.api. This object can be used by other C extensions that wish to use the bsddb module for their own purposes. (Contributed by Duncan Grisby.)

The new buffer interface, previously described in the PEP 3118 section, adds PyObject_GetBuffer() and PyBuffer_Release(), as well as a few other functions.

Python’s use of the C stdio library is now thread-safe, or at least as thread-safe as the underlying library is. A long-standing potential bug occurred if one thread closed a file object while another thread was reading from or writing to the object. In 2.6 file objects have a reference count, manipulated by the PyFile_IncUseCount() and PyFile_DecUseCount() functions. File objects can’t be closed unless the reference count is zero. PyFile_IncUseCount() should be called while the GIL is still held, before carrying out an I/O operation using the FILE * pointer, and PyFile_DecUseCount() should be called immediately after the GIL is re-acquired. (Contributed by Antoine Pitrou and Gregory P. Smith.)

Importing modules simultaneously in two different threads no longer deadlocks; it will now raise an ImportError. A new API function, PyImport_ImportModuleNoBlock(), will look for a module in sys.modules first, then try to import it after acquiring an import lock. If the import lock is held by another thread, an ImportError is raised. (Contributed by Christian Heimes.)

Several functions return information about the platform’s floating-point support. PyFloat_GetMax() returns the maximum representable floating point value, and PyFloat_GetMin() returns the minimum positive value. PyFloat_GetInfo() returns an object containing more information from the float.h file, such as "mant_dig" (number of digits in the mantissa), "epsilon" (smallest difference between 1.0 and the next largest value representable), and several others. (Contributed by Christian Heimes; bpo-1534 [https://bugs.python.org/issue1534].)

C functions and methods that use PyComplex_AsCComplex() will now accept arguments that have a __complex__() method. In particular, the functions in the cmath module will now accept objects with this method. This is a backport of a Python 3.0 change. (Contributed by Mark Dickinson; bpo-1675423 [https://bugs.python.org/issue1675423].)

Python’s C API now includes two functions for case-insensitive string comparisons, PyOS_stricmp(char*, char*) and PyOS_strnicmp(char*, char*, Py_ssize_t). (Contributed by Christian Heimes; bpo-1635 [https://bugs.python.org/issue1635].)

Many C extensions define their own little macro for adding integers and strings to the module’s dictionary in the init* function. Python 2.6 finally defines standard macros for adding values to a module, PyModule_AddStringMacro and PyModule_AddIntMacro(). (Contributed by Christian Heimes.)

Some macros were renamed in both 3.0 and 2.6 to make it clearer that they are macros, not functions. Py_Size() became Py_SIZE(), Py_Type() became Py_TYPE(), and Py_Refcnt() became Py_REFCNT(). The mixed-case macros are still available in Python 2.6 for backward compatibility. (bpo-1629 [https://bugs.python.org/issue1629])

Distutils now places C extensions it builds in a different directory when running on a debug version of Python. (Contributed by Collin Winter; bpo-1530959 [https://bugs.python.org/issue1530959].)

Several basic data types, such as integers and strings, maintain internal free lists of objects that can be re-used. The data structures for these free lists now follow a naming convention: the variable is always named free_list, the counter is always named numfree, and a macro Py<typename>_MAXFREELIST is always defined.

A new Makefile target, “make patchcheck”, prepares the Python source tree for making a patch: it fixes trailing whitespace in all modified .py files, checks whether the documentation has been changed, and reports whether the Misc/ACKS and Misc/NEWS files have been updated. (Contributed by Brett Cannon.)

Another new target, “make profile-opt”, compiles a Python binary using GCC’s profile-guided optimization. It compiles Python with profiling enabled, runs the test suite to obtain a set of profiling results, and then compiles using these results for optimization. (Contributed by Gregory P. Smith.)

特定于端口的更改：Windows

The support for Windows 95, 98, ME and NT4 has been dropped. Python 2.6 requires at least Windows 2000 SP4.

The new default compiler on Windows is Visual Studio 2008 (version 9.0). The build directories for Visual Studio 2003 (version 7.1) and 2005 (version 8.0) were moved into the PC/ directory. The new PCbuild directory supports cross compilation for X64, debug builds and Profile Guided Optimization (PGO). PGO builds are roughly 10% faster than normal builds. (Contributed by Christian Heimes with help from Amaury Forgeot d’Arc and Martin von Löwis.)

The msvcrt module now supports both the normal and wide char variants of the console I/O API. The getwch() function reads a keypress and returns a Unicode value, as does the getwche() function. The putwch() function takes a Unicode character and writes it to the console. (Contributed by Christian Heimes.)

os.path.expandvars() will now expand environment variables in the form “%var%”, and “~user” will be expanded into the user’s home directory path. (Contributed by Josiah Carlson; bpo-957650 [https://bugs.python.org/issue957650].)

The socket module’s socket objects now have an ioctl() method that provides a limited interface to the WSAIoctl() system interface.

The _winreg module now has a function, ExpandEnvironmentStrings(), that expands environment variable references such as %NAME% in an input string. The handle objects provided by this module now support the context protocol, so they can be used in with statements. (Contributed by Christian Heimes.)

_winreg also has better support for x64 systems, exposing the DisableReflectionKey(), EnableReflectionKey(), and QueryReflectionKey() functions, which enable and disable registry reflection for 32-bit processes running on 64-bit systems. (bpo-1753245 [https://bugs.python.org/issue1753245])

The msilib module’s Record object gained GetInteger() and GetString() methods that return field values as an integer or a string. (Contributed by Floris Bruynooghe; bpo-2125 [https://bugs.python.org/issue2125].)

特定于端口的更改：Mac OS X

现在，在编译Python的框架版本时，可以为 configure 脚本添加 --with-framework-name= 选项来指定要使用的框架名称。

macfs 模块已被删除。这反过来要求删除 macostools.touched() 函数，因为它依赖于 macfs 模块。 (bpo-1490190 [https://bugs.python.org/issue1490190])

许多其他 Mac OS 模块已弃用并将在 Python 3.0 中被删除: _builtinSuites, aepack, aetools, aetypes, applesingle, appletrawmain, appletrunner, argvemulator, Audio_mac, autoGIL, Carbon, cfmfile, CodeWarrior, ColorPicker, EasyDialogs, Explorer, Finder, FrameWork, findertools, ic, icglue, icopen, macerrors, MacOS, macfs, macostools, macresource, MiniAEFrame, Nav, Netscape, OSATerminology, pimp, PixMapWrapper, StdSuites, SystemEvents, Terminal 和 terminalcommand。

特定于端口的更改：IRIX

许多旧的 IRIX 专用模块已被弃用，并将在Python 3.0中删除： al 和 AL, cd, cddb, cdplayer, CL 和 cl, DEVICE, ERRNO, FILE, FL 和 fl, flp, fm, GET, GLWS, GL 和 gl, IN, IOCTL, jpeg, panelparser, readcd, SV 和 sv, torgb, videoreader, 和 WAIT.

移植到Python 2.6

This section lists previously described changes and other bugfixes that may require changes to your code:

Classes that aren’t supposed to be hashable should set __hash__ = None in their definitions to indicate the fact.

String exceptions have been removed. Attempting to use them raises a TypeError.

The __init__() method of collections.deque now clears any existing contents of the deque before adding elements from the iterable. This change makes the behavior match list.__init__().

object.__init__() previously accepted arbitrary arguments and keyword arguments, ignoring them. In Python 2.6, this is no longer allowed and will result in a TypeError. This will affect __init__() methods that end up calling the corresponding method on object (perhaps through using super()). See bpo-1683368 [https://bugs.python.org/issue1683368] for discussion.

The Decimal constructor now accepts leading and trailing whitespace when passed a string. Previously it would raise an InvalidOperation exception. On the other hand, the create_decimal() method of Context objects now explicitly disallows extra whitespace, raising a ConversionSyntax exception.

Due to an implementation accident, if you passed a file path to the built-in __import__() function, it would actually import the specified file. This was never intended to work, however, and the implementation now explicitly checks for this case and raises an ImportError.

C API: the PyImport_Import() and PyImport_ImportModule() functions now default to absolute imports, not relative imports. This will affect C extensions that import other modules.

C API: extension data types that shouldn’t be hashable should define their tp_hash slot to PyObject_HashNotImplemented().

The socket module exception socket.error now inherits from IOError. Previously it wasn’t a subclass of StandardError but now it is, through IOError. (Implemented by Gregory P. Smith; bpo-1706815 [https://bugs.python.org/issue1706815].)

The xmlrpclib module no longer automatically converts datetime.date and datetime.time to the xmlrpclib.DateTime type; the conversion semantics were not necessarily correct for all applications. Code using xmlrpclib should convert date and time instances. (bpo-1330538 [https://bugs.python.org/issue1330538])

(3.0-warning mode) The Exception class now warns when accessed using slicing or index access; having Exception behave like a tuple is being phased out.

(3.0-warning mode) inequality comparisons between two dictionaries or two objects that don’t implement comparison methods are reported as warnings. dict1 == dict2 still works, but dict1 < dict2 is being phased out.

Comparisons between cells, which are an implementation detail of Python’s scoping rules, also cause warnings because such comparisons are forbidden entirely in 3.0.

致谢

作者感谢以下人员对本文各种草稿给予的建议，更正和协助： Georg Brandl, Steve Brown, Nick Coghlan, Ralph Corderoy, Jim Jewett, Kent Johnson, Chris Lambacher, Martin Michlmayr, Antoine Pitrou, Brian Warner.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

Python 2.5 有什么新变化

作者

A.M. Kuchling

This article explains the new features in Python 2.5. The final release of Python 2.5 is scheduled for August 2006; PEP 356 [https://www.python.org/dev/peps/pep-0356] describes the planned release schedule.

The changes in Python 2.5 are an interesting mix of language and library improvements. The library enhancements will be more important to Python’s user community, I think, because several widely-useful packages were added. New modules include ElementTree for XML processing (xml.etree), the SQLite database module (sqlite), and the ctypes module for calling C functions.

The language changes are of middling significance. Some pleasant new features were added, but most of them aren’t features that you’ll use every day. Conditional expressions were finally added to the language using a novel syntax; see section PEP 308: 条件表达式. The new ‘with’ statement will make writing cleanup code easier (section PEP 343: “with” 语句). Values can now be passed into generators (section PEP 342: 生成器的新特性). Imports are now visible as either absolute or relative (section PEP 328: 绝对导入和相对导入). Some corner cases of exception handling are handled better (section PEP 341: 统一 try/except/finally). All these improvements are worthwhile, but they’re improvements to one specific language feature or another; none of them are broad modifications to Python’s semantics.

As well as the language and library additions, other improvements and bugfixes were made throughout the source tree. A search through the SVN change logs finds there were 353 patches applied and 458 bugs fixed between Python 2.4 and 2.5. (Both figures are likely to be underestimates.)

This article doesn’t try to be a complete specification of the new features; instead changes are briefly introduced using helpful examples. For full details, you should always refer to the documentation for Python 2.5 at https://docs.python.org. If you want to understand the complete implementation and design rationale, refer to the PEP for a particular new feature.

Comments, suggestions, and error reports for this document are welcome; please e-mail them to the author or open a bug in the Python bug tracker.

PEP 308: 条件表达式

For a long time, people have been requesting a way to write conditional expressions, which are expressions that return value A or value B depending on whether a Boolean value is true or false. A conditional expression lets you write a single assignment statement that has the same effect as the following:

if condition:
    x = true_value
else:
    x = false_value

There have been endless tedious discussions of syntax on both python-dev and comp.lang.python. A vote was even held that found the majority of voters wanted conditional expressions in some form, but there was no syntax that was preferred by a clear majority. Candidates included C’s cond ? true_v : false_v, if cond then true_v else false_v, and 16 other variations.

Guido van Rossum 最终选择了一种令人惊讶的语法：

x = true_value if condition else false_value

Evaluation is still lazy as in existing Boolean expressions, so the order of evaluation jumps around a bit. The condition expression in the middle is evaluated first, and the true_value expression is evaluated only if the condition was true. Similarly, the false_value expression is only evaluated when the condition is false.

This syntax may seem strange and backwards; why does the condition go in the middle of the expression, and not in the front as in C’s c ? x : y? The decision was checked by applying the new syntax to the modules in the standard library and seeing how the resulting code read. In many cases where a conditional expression is used, one value seems to be the ‘common case’ and one value is an ‘exceptional case’, used only on rarer occasions when the condition isn’t met. The conditional syntax makes this pattern a bit more obvious:

contents = ((doc + '\n') if doc else '')

I read the above statement as meaning “here contents is usually assigned a value of doc+'\n'; sometimes doc is empty, in which special case an empty string is returned.” I doubt I will use conditional expressions very often where there isn’t a clear common and uncommon case.

There was some discussion of whether the language should require surrounding conditional expressions with parentheses. The decision was made to not require parentheses in the Python language’s grammar, but as a matter of style I think you should always use them. Consider these two statements:

# First version -- no parens
level = 1 if logging else 0

# Second version -- with parens
level = (1 if logging else 0)

In the first version, I think a reader’s eye might group the statement into ‘level = 1’, ‘if logging’, ‘else 0’, and think that the condition decides whether the assignment to level is performed. The second version reads better, in my opinion, because it makes it clear that the assignment is always performed and the choice is being made between two values.

Another reason for including the brackets: a few odd combinations of list comprehensions and lambdas could look like incorrect conditional expressions. See PEP 308 [https://www.python.org/dev/peps/pep-0308] for some examples. If you put parentheses around your conditional expressions, you won’t run into this case.

参见

PEP 308 [https://www.python.org/dev/peps/pep-0308] - 条件表达式

PEP 由 Guido van Rossum 和 Raymond D 撰写，由 Thomas Wouters 实现。

PEP 309: 部分功能应用

The functools module is intended to contain tools for functional-style programming.

One useful tool in this module is the partial() function. For programs written in a functional style, you’ll sometimes want to construct variants of existing functions that have some of the parameters filled in. Consider a Python function f(a, b, c); you could create a new function g(b, c) that was equivalent to f(1, b, c). This is called “partial function application”.

partial() takes the arguments (function, arg1, arg2, ... kwarg1=value1, kwarg2=value2). The resulting object is callable, so you can just call it to invoke function with the filled-in arguments.

这里有一个很小但很现实的例子:

import functools

def log (message, subsystem):
    "Write the contents of 'message' to the specified subsystem."
    print '%s: %s' % (subsystem, message)
    ...

server_log = functools.partial(log, subsystem='server')
server_log('Unable to open socket')

Here’s another example, from a program that uses PyGTK. Here a context-sensitive pop-up menu is being constructed dynamically. The callback provided for the menu option is a partially applied version of the open_item() method, where the first argument has been provided.

...
class Application:
    def open_item(self, path):
       ...
    def init (self):
        open_func = functools.partial(self.open_item, item_path)
        popup_menu.append( ("Open", open_func, 1) )

Another function in the functools module is the update_wrapper(wrapper, wrapped) function that helps you write well-behaved decorators. update_wrapper() copies the name, module, and docstring attribute to a wrapper function so that tracebacks inside the wrapped function are easier to understand. For example, you might write:

def my_decorator(f):
    def wrapper(*args, **kwds):
        print 'Calling decorated function'
        return f(*args, **kwds)
    functools.update_wrapper(wrapper, f)
    return wrapper

wraps() is a decorator that can be used inside your own decorators to copy the wrapped function’s information. An alternate version of the previous example would be:

def my_decorator(f):
    @functools.wraps(f)
    def wrapper(*args, **kwds):
        print 'Calling decorated function'
        return f(*args, **kwds)
    return wrapper

参见

PEP 309 [https://www.python.org/dev/peps/pep-0309] - Partial Function Application

PEP由 Peter Harris 提出并撰写；由 Hye-Shik Chang 和 Nick Coghlan 实现，并由 Raymond Hettinger 适配。

PEP 314: Python软件包的元数据 v1.1

Some simple dependency support was added to Distutils. The setup() function now has requires, provides, and obsoletes keyword parameters. When you build a source distribution using the sdist command, the dependency information will be recorded in the PKG-INFO file.

Another new keyword parameter is download_url, which should be set to a URL for the package’s source code. This means it’s now possible to look up an entry in the package index, determine the dependencies for a package, and download the required packages.

VERSION = '1.0'
setup(name='PyPackage',
      version=VERSION,
      requires=['numarray', 'zlib (>=1.1.4)'],
      obsoletes=['OldPackage']
      download_url=('http://www.example.com/pypackage/dist/pkg-%s.tar.gz'
                    % VERSION),
     )

Another new enhancement to the Python package index at https://pypi.org is storing source and binary archives for a package. The new upload Distutils command will upload a package to the repository.

Before a package can be uploaded, you must be able to build a distribution using the sdist Distutils command. Once that works, you can run python setup.py upload to add your package to the PyPI archive. Optionally you can GPG-sign the package by supplying the --sign and --identity options.

Package uploading was implemented by Martin von Löwis and Richard Jones.

参见

PEP 314 [https://www.python.org/dev/peps/pep-0314] - Python软件包的元数据 v1.1

PEP 由 A.M. Kuchling, Richard Jones 和 Fred Drake 提出并撰写，由 Richard Jones 和 Fred Drake 实现

PEP 328: 绝对导入和相对导入

The simpler part of PEP 328 was implemented in Python 2.4: parentheses could now be used to enclose the names imported from a module using the from ... import ... statement, making it easier to import many different names.

The more complicated part has been implemented in Python 2.5: importing a module can be specified to use absolute or package-relative imports. The plan is to move toward making absolute imports the default in future versions of Python.

Let’s say you have a package directory like this:

pkg/
pkg/__init__.py
pkg/main.py
pkg/string.py

This defines a package named pkg containing the pkg.main and pkg.string submodules.

Consider the code in the main.py module. What happens if it executes the statement import string? In Python 2.4 and earlier, it will first look in the package’s directory to perform a relative import, finds pkg/string.py, imports the contents of that file as the pkg.string module, and that module is bound to the name string in the pkg.main module’s namespace.

That’s fine if pkg.string was what you wanted. But what if you wanted Python’s standard string module? There’s no clean way to ignore pkg.string and look for the standard module; generally you had to look at the contents of sys.modules, which is slightly unclean. Holger Krekel’s py.std package provides a tidier way to perform imports from the standard library, import py; py.std.string.join(), but that package isn’t available on all Python installations.

Reading code which relies on relative imports is also less clear, because a reader may be confused about which module, string or pkg.string, is intended to be used. Python users soon learned not to duplicate the names of standard library modules in the names of their packages’ submodules, but you can’t protect against having your submodule’s name being used for a new module added in a future version of Python.

In Python 2.5, you can switch import’s behaviour to absolute imports using a from __future__ import absolute_import directive. This absolute-import behaviour will become the default in a future version (probably Python 2.7). Once absolute imports are the default, import string will always find the standard library’s version. It’s suggested that users should begin using absolute imports as much as possible, so it’s preferable to begin writing from pkg import string in your code.

Relative imports are still possible by adding a leading period to the module name when using the from ... import form:

# Import names from pkg.string
from .string import name1, name2
# Import pkg.string
from . import string

This imports the string module relative to the current package, so in pkg.main this will import name1 and name2 from pkg.string. Additional leading periods perform the relative import starting from the parent of the current package. For example, code in the A.B.C module can do:

from . import D                 # Imports A.B.D
from .. import E                # Imports A.E
from ..F import G               # Imports A.F.G

Leading periods cannot be used with the import modname form of the import statement, only the from ... import form.

参见

PEP 328 [https://www.python.org/dev/peps/pep-0328] - 导入：多行和绝对/相对导入

PEP 由 Aahz 撰写，由 Thomas Wouters 实现。

https://pylib.readthedocs.org/

The py library by Holger Krekel, which contains the py.std package.

PEP 338: 将模块作为脚本执行

The -m switch added in Python 2.4 to execute a module as a script gained a few more abilities. Instead of being implemented in C code inside the Python interpreter, the switch now uses an implementation in a new module, runpy.

The runpy module implements a more sophisticated import mechanism so that it’s now possible to run modules in a package such as pychecker.checker. The module also supports alternative import mechanisms such as the zipimport module. This means you can add a .zip archive’s path to sys.path and then use the -m switch to execute code from the archive.

参见

PEP 338 [https://www.python.org/dev/peps/pep-0338] - 将模块作为脚本执行

PEP 由 Nick Coghlan 撰写并实现。

PEP 341: 统一 try/except/finally

Until Python 2.5, the try statement came in two flavours. You could use a finally block to ensure that code is always executed, or one or more except blocks to catch specific exceptions. You couldn’t combine both except blocks and a finally block, because generating the right bytecode for the combined version was complicated and it wasn’t clear what the semantics of the combined statement should be.

Guido van Rossum spent some time working with Java, which does support the equivalent of combining except blocks and a finally block, and this clarified what the statement should mean. In Python 2.5, you can now write:

try:
    block-1 ...
except Exception1:
    handler-1 ...
except Exception2:
    handler-2 ...
else:
    else-block
finally:
    final-block

The code in block-1 is executed. If the code raises an exception, the various except blocks are tested: if the exception is of class Exception1, handler-1 is executed; otherwise if it’s of class Exception2, handler-2 is executed, and so forth. If no exception is raised, the else-block is executed.

No matter what happened previously, the final-block is executed once the code block is complete and any raised exceptions handled. Even if there’s an error in an exception handler or the else-block and a new exception is raised, the code in the final-block is still run.

参见

PEP 341 [https://www.python.org/dev/peps/pep-0341] - 统一 try-except 和 try-finally

PEP 由 Georg Brandl 撰写，由 Thomas Lee 实现。

PEP 342: 生成器的新特性

Python 2.5 adds a simple way to pass values into a generator. As introduced in Python 2.3, generators only produce output; once a generator’s code was invoked to create an iterator, there was no way to pass any new information into the function when its execution is resumed. Sometimes the ability to pass in some information would be useful. Hackish solutions to this include making the generator’s code look at a global variable and then changing the global variable’s value, or passing in some mutable object that callers then modify.

To refresh your memory of basic generators, here’s a simple example:

def counter (maximum):
    i = 0
    while i < maximum:
        yield i
        i += 1

When you call counter(10), the result is an iterator that returns the values from 0 up to 9. On encountering the yield statement, the iterator returns the provided value and suspends the function’s execution, preserving the local variables. Execution resumes on the following call to the iterator’s next() method, picking up after the yield statement.

In Python 2.3, yield was a statement; it didn’t return any value. In 2.5, yield is now an expression, returning a value that can be assigned to a variable or otherwise operated on:

val = (yield i)

I recommend that you always put parentheses around a yield expression when you’re doing something with the returned value, as in the above example. The parentheses aren’t always necessary, but it’s easier to always add them instead of having to remember when they’re needed.

(PEP 342 [https://www.python.org/dev/peps/pep-0342] explains the exact rules, which are that a yield-expression must always be parenthesized except when it occurs at the top-level expression on the right-hand side of an assignment. This means you can write val = yield i but have to use parentheses when there’s an operation, as in val = (yield i) + 12.)

Values are sent into a generator by calling its send(value) method. The generator’s code is then resumed and the yield expression returns the specified value. If the regular next() method is called, the yield returns None.

Here’s the previous example, modified to allow changing the value of the internal counter.

def counter (maximum):
    i = 0
    while i < maximum:
        val = (yield i)
        # If value provided, change counter
        if val is not None:
            i = val
        else:
            i += 1

And here’s an example of changing the counter:

>>> it = counter(10)
>>> print it.next()
0
>>> print it.next()
1
>>> print it.send(8)
8
>>> print it.next()
9
>>> print it.next()
Traceback (most recent call last):
  File "t.py", line 15, in ?
    print it.next()
StopIteration

yield will usually return None, so you should always check for this case. Don’t just use its value in expressions unless you’re sure that the send() method will be the only method used to resume your generator function.

In addition to send(), there are two other new methods on generators:

throw(type, value=None, traceback=None) is used to raise an exception inside the generator; the exception is raised by the yield expression where the generator’s execution is paused.

close() raises a new GeneratorExit exception inside the generator to terminate the iteration. On receiving this exception, the generator’s code must either raise GeneratorExit or StopIteration. Catching the GeneratorExit exception and returning a value is illegal and will trigger a RuntimeError; if the function raises some other exception, that exception is propagated to the caller. close() will also be called by Python’s garbage collector when the generator is garbage-collected.

If you need to run cleanup code when a GeneratorExit occurs, I suggest using a try: ... finally: suite instead of catching GeneratorExit.

这些改变的累积效应是，让生成器从单向的信息生产者变成了既是生产者，又是消费者。

Generators also become coroutines, a more generalized form of subroutines. Subroutines are entered at one point and exited at another point (the top of the function, and a return statement), but coroutines can be entered, exited, and resumed at many different points (the yield statements). We’ll have to figure out patterns for using coroutines effectively in Python.

The addition of the close() method has one side effect that isn’t obvious. close() is called when a generator is garbage-collected, so this means the generator’s code gets one last chance to run before the generator is destroyed. This last chance means that try...finally statements in generators can now be guaranteed to work; the finally clause will now always get a chance to run. The syntactic restriction that you couldn’t mix yield statements with a try...finally suite has therefore been removed. This seems like a minor bit of language trivia, but using generators and try...finally is actually necessary in order to implement the with statement described by PEP 343. I’ll look at this new statement in the following section.

Another even more esoteric effect of this change: previously, the gi_frame attribute of a generator was always a frame object. It’s now possible for gi_frame to be None once the generator has been exhausted.

参见

PEP 342 [https://www.python.org/dev/peps/pep-0342] - 通过增强型生成器实现协程

PEP 由 Guido van Rossum 和 Phillip J. Eby 撰写，由 Phillip J. Eby 实现。包括一些更高级的使用生成器作为协程的示例。

这些功能的早期版本在 PEP 288 [https://www.python.org/dev/peps/pep-0288] （由 Raymond Hettinger 撰写） 和 PEP 325 [https://www.python.org/dev/peps/pep-0325] （由 Samuele Pedroni 撰写）中提出。

https://en.wikipedia.org/wiki/Coroutine

协程的Wikipedia条目。

http://www.sidhe.org/~dan/blog/archives/000178.html

An explanation of coroutines from a Perl point of view, written by Dan Sugalski.

PEP 343: “with” 语句

The ‘with’ statement clarifies code that previously would use try...finally blocks to ensure that clean-up code is executed. In this section, I’ll discuss the statement as it will commonly be used. In the next section, I’ll examine the implementation details and show how to write objects for use with this statement.

The ‘with’ statement is a new control-flow structure whose basic structure is:

with expression [as variable]:
    with-block

The expression is evaluated, and it should result in an object that supports the context management protocol (that is, has __enter__() and __exit__() methods.

The object’s __enter__() is called before with-block is executed and therefore can run set-up code. It also may return a value that is bound to the name variable, if given. (Note carefully that variable is not assigned the result of expression.)

After execution of the with-block is finished, the object’s __exit__() method is called, even if the block raised an exception, and can therefore run clean-up code.

To enable the statement in Python 2.5, you need to add the following directive to your module:

from __future__ import with_statement

该语句在Python 2.6 中始终启用。

Some standard Python objects now support the context management protocol and can be used with the ‘with’ statement. File objects are one example:

with open('/etc/passwd', 'r') as f:
    for line in f:
        print line
        ... more processing code ...

After this statement has executed, the file object in f will have been automatically closed, even if the for loop raised an exception part-way through the block.

注解

In this case, f is the same object created by open(), because file.__enter__() returns self.

The threading module’s locks and condition variables also support the ‘with’ statement:

lock = threading.Lock()
with lock:
    # Critical section of code
    ...

The lock is acquired before the block is executed and always released once the block is complete.

The new localcontext() function in the decimal module makes it easy to save and restore the current decimal context, which encapsulates the desired precision and rounding characteristics for computations:

from decimal import Decimal, Context, localcontext

# Displays with default precision of 28 digits
v = Decimal('578')
print v.sqrt()

with localcontext(Context(prec=16)):
    # All code in this block uses a precision of 16 digits.
    # The original context is restored on exiting the block.
    print v.sqrt()

Writing Context Managers

Under the hood, the ‘with’ statement is fairly complicated. Most people will only use ‘with’ in company with existing objects and don’t need to know these details, so you can skip the rest of this section if you like. Authors of new objects will need to understand the details of the underlying implementation and should keep reading.

A high-level explanation of the context management protocol is:

The expression is evaluated and should result in an object called a “context manager”. The context manager must have __enter__() and __exit__() methods.

The context manager’s __enter__() method is called. The value returned is assigned to VAR. If no 'as VAR' clause is present, the value is simply discarded.

The code in BLOCK is executed.

If BLOCK raises an exception, the __exit__(type, value, traceback) is called with the exception details, the same values returned by sys.exc_info(). The method’s return value controls whether the exception is re-raised: any false value re-raises the exception, and True will result in suppressing it. You’ll only rarely want to suppress the exception, because if you do the author of the code containing the ‘with’ statement will never realize anything went wrong.

If BLOCK didn’t raise an exception, the __exit__() method is still called, but type, value, and traceback are all None.

Let’s think through an example. I won’t present detailed code but will only sketch the methods necessary for a database that supports transactions.

(For people unfamiliar with database terminology: a set of changes to the database are grouped into a transaction. Transactions can be either committed, meaning that all the changes are written into the database, or rolled back, meaning that the changes are all discarded and the database is unchanged. See any database textbook for more information.)

Let’s assume there’s an object representing a database connection. Our goal will be to let the user write code like this:

db_connection = DatabaseConnection()
with db_connection as cursor:
    cursor.execute('insert into ...')
    cursor.execute('delete from ...')
    # ... more operations ...

The transaction should be committed if the code in the block runs flawlessly or rolled back if there’s an exception. Here’s the basic interface for DatabaseConnection that I’ll assume:

class DatabaseConnection:
    # Database interface
    def cursor (self):
        "Returns a cursor object and starts a new transaction"
    def commit (self):
        "Commits current transaction"
    def rollback (self):
        "Rolls back current transaction"

The __enter__() method is pretty easy, having only to start a new transaction. For this application the resulting cursor object would be a useful result, so the method will return it. The user can then add as cursor to their ‘with’ statement to bind the cursor to a variable name.

class DatabaseConnection:
    ...
    def __enter__ (self):
        # Code to start a new transaction
        cursor = self.cursor()
        return cursor

The __exit__() method is the most complicated because it’s where most of the work has to be done. The method has to check if an exception occurred. If there was no exception, the transaction is committed. The transaction is rolled back if there was an exception.

In the code below, execution will just fall off the end of the function, returning the default value of None. None is false, so the exception will be re-raised automatically. If you wished, you could be more explicit and add a return statement at the marked location.

class DatabaseConnection:
    ...
    def __exit__ (self, type, value, tb):
        if tb is None:
            # No exception, so commit
            self.commit()
        else:
            # Exception occurred, so rollback.
            self.rollback()
            # return False

contextlib 模块

The new contextlib module provides some functions and a decorator that are useful for writing objects for use with the ‘with’ statement.

The decorator is called contextmanager(), and lets you write a single generator function instead of defining a new class. The generator should yield exactly one value. The code up to the yield will be executed as the __enter__() method, and the value yielded will be the method’s return value that will get bound to the variable in the ‘with’ statement’s as clause, if any. The code after the yield will be executed in the __exit__() method. Any exception raised in the block will be raised by the yield statement.

Our database example from the previous section could be written using this decorator as:

from contextlib import contextmanager

@contextmanager
def db_transaction (connection):
    cursor = connection.cursor()
    try:
        yield cursor
    except:
        connection.rollback()
        raise
    else:
        connection.commit()

db = DatabaseConnection()
with db_transaction(db) as cursor:
    ...

The contextlib module also has a nested(mgr1, mgr2, ...) function that combines a number of context managers so you don’t need to write nested ‘with’ statements. In this example, the single ‘with’ statement both starts a database transaction and acquires a thread lock:

lock = threading.Lock()
with nested (db_transaction(db), lock) as (cursor, locked):
    ...

Finally, the closing(object) function returns object so that it can be bound to a variable, and calls object.close at the end of the block.

import urllib, sys
from contextlib import closing

with closing(urllib.urlopen('http://www.yahoo.com')) as f:
    for line in f:
        sys.stdout.write(line)

参见

PEP 343 [https://www.python.org/dev/peps/pep-0343] - “with” 语句

PEP written by Guido van Rossum and Nick Coghlan; implemented by Mike Bland, Guido van Rossum, and Neal Norwitz. The PEP shows the code generated for a ‘with’ statement, which can be helpful in learning how the statement works.

contextlib 模块的文档。

PEP 352: 异常作为新型的类

Exception classes can now be new-style classes, not just classic classes, and the built-in Exception class and all the standard built-in exceptions (NameError, ValueError, etc.) are now new-style classes.

The inheritance hierarchy for exceptions has been rearranged a bit. In 2.5, the inheritance relationships are:

BaseException       # New in Python 2.5
|- KeyboardInterrupt
|- SystemExit
|- Exception
   |- (all other current built-in exceptions)

This rearrangement was done because people often want to catch all exceptions that indicate program errors. KeyboardInterrupt and SystemExit aren’t errors, though, and usually represent an explicit action such as the user hitting Control-C or code calling sys.exit(). A bare except: will catch all exceptions, so you commonly need to list KeyboardInterrupt and SystemExit in order to re-raise them. The usual pattern is:

try:
    ...
except (KeyboardInterrupt, SystemExit):
    raise
except:
    # Log error...
    # Continue running program...

In Python 2.5, you can now write except Exception to achieve the same result, catching all the exceptions that usually indicate errors but leaving KeyboardInterrupt and SystemExit alone. As in previous versions, a bare except: still catches all exceptions.

The goal for Python 3.0 is to require any class raised as an exception to derive from BaseException or some descendant of BaseException, and future releases in the Python 2.x series may begin to enforce this constraint. Therefore, I suggest you begin making all your exception classes derive from Exception now. It’s been suggested that the bare except: form should be removed in Python 3.0, but Guido van Rossum hasn’t decided whether to do this or not.

Raising of strings as exceptions, as in the statement raise "Error occurred", is deprecated in Python 2.5 and will trigger a warning. The aim is to be able to remove the string-exception feature in a few releases.

参见

PEP 352 [https://www.python.org/dev/peps/pep-0352] - 异常所需的超类

PEP 由 Brett Cannon 和 Guido van Rossum 撰写，由 Brett Cannon 实现

PEP 353: 使用ssize_t作为索引类型

A wide-ranging change to Python’s C API, using a new Py_ssize_t type definition instead of int, will permit the interpreter to handle more data on 64-bit platforms. This change doesn’t affect Python’s capacity on 32-bit platforms.

Various pieces of the Python interpreter used C’s int type to store sizes or counts; for example, the number of items in a list or tuple were stored in an int. The C compilers for most 64-bit platforms still define int as a 32-bit type, so that meant that lists could only hold up to 2**31 - 1 = 2147483647 items. (There are actually a few different programming models that 64-bit C compilers can use – see http://www.unix.org/version2/whatsnew/lp64_wp.html for a discussion – but the most commonly available model leaves int as 32 bits.)

A limit of 2147483647 items doesn’t really matter on a 32-bit platform because you’ll run out of memory before hitting the length limit. Each list item requires space for a pointer, which is 4 bytes, plus space for a PyObject representing the item. 2147483647*4 is already more bytes than a 32-bit address space can contain.

It’s possible to address that much memory on a 64-bit platform, however. The pointers for a list that size would only require 16 GiB of space, so it’s not unreasonable that Python programmers might construct lists that large. Therefore, the Python interpreter had to be changed to use some type other than int, and this will be a 64-bit type on 64-bit platforms. The change will cause incompatibilities on 64-bit machines, so it was deemed worth making the transition now, while the number of 64-bit users is still relatively small. (In 5 or 10 years, we may all be on 64-bit machines, and the transition would be more painful then.)

This change most strongly affects authors of C extension modules. Python strings and container types such as lists and tuples now use Py_ssize_t to store their size. Functions such as PyList_Size() now return Py_ssize_t. Code in extension modules may therefore need to have some variables changed to Py_ssize_t.

The PyArg_ParseTuple() and Py_BuildValue() functions have a new conversion code, n, for Py_ssize_t. PyArg_ParseTuple()’s s# and t# still output int by default, but you can define the macro PY_SSIZE_T_CLEAN before including Python.h to make them return Py_ssize_t.

PEP 353 [https://www.python.org/dev/peps/pep-0353] has a section on conversion guidelines that extension authors should read to learn about supporting 64-bit platforms.

参见

PEP 353 [https://www.python.org/dev/peps/pep-0353] - 使用ssize_t作为索引类型

PEP 由 Martin von Löwis 撰写并实现。

PEP 357: ‘__index__’ 方法

The NumPy developers had a problem that could only be solved by adding a new special method, __index__(). When using slice notation, as in [start:stop:step], the values of the start, stop, and step indexes must all be either integers or long integers. NumPy defines a variety of specialized integer types corresponding to unsigned and signed integers of 8, 16, 32, and 64 bits, but there was no way to signal that these types could be used as slice indexes.

Slicing can’t just use the existing __int__() method because that method is also used to implement coercion to integers. If slicing used __int__(), floating-point numbers would also become legal slice indexes and that’s clearly an undesirable behaviour.

Instead, a new special method called __index__() was added. It takes no arguments and returns an integer giving the slice index to use. For example:

class C:
    def __index__ (self):
        return self.value

The return value must be either a Python integer or long integer. The interpreter will check that the type returned is correct, and raises a TypeError if this requirement isn’t met.

A corresponding nb_index slot was added to the C-level PyNumberMethods structure to let C extensions implement this protocol. PyNumber_Index(obj) can be used in extension code to call the __index__() function and retrieve its result.

参见

PEP 357 [https://www.python.org/dev/peps/pep-0357] - 允许将任何对象用于切片

PEP 由 Travis Oliphant 撰写并实现。

其他语言特性修改

Here are all of the changes that Python 2.5 makes to the core Python language.

The dict type has a new hook for letting subclasses provide a default value when a key isn’t contained in the dictionary. When a key isn’t found, the dictionary’s __missing__(key) method will be called. This hook is used to implement the new defaultdict class in the collections module. The following example defines a dictionary that returns zero for any missing key:

class zerodict (dict):
    def __missing__ (self, key):
        return 0

d = zerodict({1:1, 2:2})
print d[1], d[2]   # Prints 1, 2
print d[3], d[4]   # Prints 0, 0

Both 8-bit and Unicode strings have new partition(sep) and rpartition(sep) methods that simplify a common use case.

The find(S) method is often used to get an index which is then used to slice the string and obtain the pieces that are before and after the separator. partition(sep) condenses this pattern into a single method call that returns a 3-tuple containing the substring before the separator, the separator itself, and the substring after the separator. If the separator isn’t found, the first element of the tuple is the entire string and the other two elements are empty. rpartition(sep) also returns a 3-tuple but starts searching from the end of the string; the r stands for ‘reverse’.

几个例子:

>>> ('http://www.python.org').partition('://')
('http', '://', 'www.python.org')
>>> ('file:/usr/share/doc/index.html').partition('://')
('file:/usr/share/doc/index.html', '', '')
>>> (u'Subject: a quick question').partition(':')
(u'Subject', u':', u' a quick question')
>>> 'www.python.org'.rpartition('.')
('www.python', '.', 'org')
>>> 'www.python.org'.rpartition(':')
('', '', 'www.python.org')

(Implemented by Fredrik Lundh following a suggestion by Raymond Hettinger.)

The startswith() and endswith() methods of string types now accept tuples of strings to check for.

def is_image_file (filename):
    return filename.endswith(('.gif', '.jpg', '.tiff'))

(Implemented by Georg Brandl following a suggestion by Tom Lynn.)

The min() and max() built-in functions gained a key keyword parameter analogous to the key argument for sort(). This parameter supplies a function that takes a single argument and is called for every value in the list; min()/max() will return the element with the smallest/largest return value from this function. For example, to find the longest string in a list, you can do:

L = ['medium', 'longest', 'short']
# Prints 'longest'
print max(L, key=len)
# Prints 'short', because lexicographically 'short' has the largest value
print max(L)

（由 Steven Bethard 和 Raymond Hettinger 贡献。）

Two new built-in functions, any() and all(), evaluate whether an iterator contains any true or false values. any() returns True if any value returned by the iterator is true; otherwise it will return False. all() returns True only if all of the values returned by the iterator evaluate as true. (Suggested by Guido van Rossum, and implemented by Raymond Hettinger.)

The result of a class’s __hash__() method can now be either a long integer or a regular integer. If a long integer is returned, the hash of that value is taken. In earlier versions the hash value was required to be a regular integer, but in 2.5 the id() built-in was changed to always return non-negative numbers, and users often seem to use id(self) in __hash__() methods (though this is discouraged).

ASCII is now the default encoding for modules. It’s now a syntax error if a module contains string literals with 8-bit characters but doesn’t have an encoding declaration. In Python 2.4 this triggered a warning, not a syntax error. See PEP 263 [https://www.python.org/dev/peps/pep-0263] for how to declare a module’s encoding; for example, you might add a line like this near the top of the source file:

# -*- coding: latin1 -*-

A new warning, UnicodeWarning, is triggered when you attempt to compare a Unicode string and an 8-bit string that can’t be converted to Unicode using the default ASCII encoding. The result of the comparison is false:

>>> chr(128) == unichr(128)   # Can't convert chr(128) to Unicode
__main__:1: UnicodeWarning: Unicode equal comparison failed
  to convert both arguments to Unicode - interpreting them
  as being unequal
False
>>> chr(127) == unichr(127)   # chr(127) can be converted
True

Previously this would raise a UnicodeDecodeError exception, but in 2.5 this could result in puzzling problems when accessing a dictionary. If you looked up unichr(128) and chr(128) was being used as a key, you’d get a UnicodeDecodeError exception. Other changes in 2.5 resulted in this exception being raised instead of suppressed by the code in dictobject.c that implements dictionaries.

Raising an exception for such a comparison is strictly correct, but the change might have broken code, so instead UnicodeWarning was introduced.

（由 Marc-André Lemburg 实现。）

One error that Python programmers sometimes make is forgetting to include an __init__.py module in a package directory. Debugging this mistake can be confusing, and usually requires running Python with the -v switch to log all the paths searched. In Python 2.5, a new ImportWarning warning is triggered when an import would have picked up a directory as a package but no __init__.py was found. This warning is silently ignored by default; provide the -Wd option when running the Python executable to display the warning message. (Implemented by Thomas Wouters.)

The list of base classes in a class definition can now be empty. As an example, this is now legal:

class C():
    pass

（由 Brett Cannon 实现。）

交互解释器变更

In the interactive interpreter, quit and exit have long been strings so that new users get a somewhat helpful message when they try to quit:

>>> quit
'Use Ctrl-D (i.e. EOF) to exit.'

In Python 2.5, quit and exit are now objects that still produce string representations of themselves, but are also callable. Newbies who try quit() or exit() will now exit the interpreter as they expect. (Implemented by Georg Brandl.)

The Python executable now accepts the standard long options --help and --version; on Windows, it also accepts the /? option for displaying a help message. (Implemented by Georg Brandl.)

性能优化

Several of the optimizations were developed at the NeedForSpeed sprint, an event held in Reykjavik, Iceland, from May 21–28 2006. The sprint focused on speed enhancements to the CPython implementation and was funded by EWT LLC with local support from CCP Games. Those optimizations added at this sprint are specially marked in the following list.

When they were introduced in Python 2.4, the built-in set and frozenset types were built on top of Python’s dictionary type. In 2.5 the internal data structure has been customized for implementing sets, and as a result sets will use a third less memory and are somewhat faster. (Implemented by Raymond Hettinger.)

The speed of some Unicode operations, such as finding substrings, string splitting, and character map encoding and decoding, has been improved. (Substring search and splitting improvements were added by Fredrik Lundh and Andrew Dalke at the NeedForSpeed sprint. Character maps were improved by Walter Dörwald and Martin von Löwis.)

The long(str, base) function is now faster on long digit strings because fewer intermediate results are calculated. The peak is for strings of around 800–1000 digits where the function is 6 times faster. (Contributed by Alan McIntyre and committed at the NeedForSpeed sprint.)

It’s now illegal to mix iterating over a file with for line in file and calling the file object’s read()/readline()/readlines() methods. Iteration uses an internal buffer and the read*() methods don’t use that buffer. Instead they would return the data following the buffer, causing the data to appear out of order. Mixing iteration and these methods will now trigger a ValueError from the read*() method. (Implemented by Thomas Wouters.)

The struct module now compiles structure format strings into an internal representation and caches this representation, yielding a 20% speedup. (Contributed by Bob Ippolito at the NeedForSpeed sprint.)

The re module got a 1 or 2% speedup by switching to Python’s allocator functions instead of the system’s malloc() and free(). (Contributed by Jack Diederich at the NeedForSpeed sprint.)

The code generator’s peephole optimizer now performs simple constant folding in expressions. If you write something like a = 2+3, the code generator will do the arithmetic and produce code corresponding to a = 5. (Proposed and implemented by Raymond Hettinger.)

Function calls are now faster because code objects now keep the most recently finished frame (a “zombie frame”) in an internal field of the code object, reusing it the next time the code object is invoked. (Original patch by Michael Hudson, modified by Armin Rigo and Richard Jones; committed at the NeedForSpeed sprint.) Frame objects are also slightly smaller, which may improve cache locality and reduce memory usage a bit. (Contributed by Neal Norwitz.)

Python’s built-in exceptions are now new-style classes, a change that speeds up instantiation considerably. Exception handling in Python 2.5 is therefore about 30% faster than in 2.4. (Contributed by Richard Jones, Georg Brandl and Sean Reifschneider at the NeedForSpeed sprint.)

Importing now caches the paths tried, recording whether they exist or not so that the interpreter makes fewer open() and stat() calls on startup. (Contributed by Martin von Löwis and Georg Brandl.)

新增，改进和删除的模块

The standard library received many enhancements and bug fixes in Python 2.5. Here’s a partial list of the most notable changes, sorted alphabetically by module name. Consult the Misc/NEWS file in the source tree for a more complete list of changes, or look through the SVN logs for all the details.

The audioop module now supports the a-LAW encoding, and the code for u-LAW encoding has been improved. (Contributed by Lars Immisch.)

The codecs module gained support for incremental codecs. The codec.lookup() function now returns a CodecInfo instance instead of a tuple. CodecInfo instances behave like a 4-tuple to preserve backward compatibility but also have the attributes encode, decode, incrementalencoder, incrementaldecoder, streamwriter, and streamreader. Incremental codecs can receive input and produce output in multiple chunks; the output is the same as if the entire input was fed to the non-incremental codec. See the codecs module documentation for details. (Designed and implemented by Walter Dörwald.)

The collections module gained a new type, defaultdict, that subclasses the standard dict type. The new type mostly behaves like a dictionary but constructs a default value when a key isn’t present, automatically adding it to the dictionary for the requested key value.

The first argument to defaultdict’s constructor is a factory function that gets called whenever a key is requested but not found. This factory function receives no arguments, so you can use built-in type constructors such as list() or int(). For example, you can make an index of words based on their initial letter like this:

words = """Nel mezzo del cammin di nostra vita
mi ritrovai per una selva oscura
che la diritta via era smarrita""".lower().split()

index = defaultdict(list)

for w in words:
    init_letter = w[0]
    index[init_letter].append(w)

打印 index 导致以下输出:

defaultdict(<type 'list'>, {'c': ['cammin', 'che'], 'e': ['era'],
        'd': ['del', 'di', 'diritta'], 'm': ['mezzo', 'mi'],
        'l': ['la'], 'o': ['oscura'], 'n': ['nel', 'nostra'],
        'p': ['per'], 's': ['selva', 'smarrita'],
        'r': ['ritrovai'], 'u': ['una'], 'v': ['vita', 'via']}

（由 Guido van Rossum 贡献。）

The deque double-ended queue type supplied by the collections module now has a remove(value) method that removes the first occurrence of value in the queue, raising ValueError if the value isn’t found. (Contributed by Raymond Hettinger.)

New module: The contextlib module contains helper functions for use with the new ‘with’ statement. See section contextlib 模块 for more about this module.

New module: The cProfile module is a C implementation of the existing profile module that has much lower overhead. The module’s interface is the same as profile: you run cProfile.run('main()') to profile a function, can save profile data to a file, etc. It’s not yet known if the Hotshot profiler, which is also written in C but doesn’t match the profile module’s interface, will continue to be maintained in future versions of Python. (Contributed by Armin Rigo.)

Also, the pstats module for analyzing the data measured by the profiler now supports directing the output to any file object by supplying a stream argument to the Stats constructor. (Contributed by Skip Montanaro.)

The csv module, which parses files in comma-separated value format, received several enhancements and a number of bugfixes. You can now set the maximum size in bytes of a field by calling the csv.field_size_limit(new_limit) function; omitting the new_limit argument will return the currently-set limit. The reader class now has a line_num attribute that counts the number of physical lines read from the source; records can span multiple physical lines, so line_num is not the same as the number of records read.

The CSV parser is now stricter about multi-line quoted fields. Previously, if a line ended within a quoted field without a terminating newline character, a newline would be inserted into the returned field. This behavior caused problems when reading files that contained carriage return characters within fields, so the code was changed to return the field without inserting newlines. As a consequence, if newlines embedded within fields are important, the input should be split into lines in a manner that preserves the newline characters.

（由Skip Montanaro 和 Andrew McNamara 贡献。）

The datetime class in the datetime module now has a strptime(string, format) method for parsing date strings, contributed by Josh Spoerri. It uses the same format characters as time.strptime() and time.strftime():

from datetime import datetime

ts = datetime.strptime('10:13:15 2006-03-07',
                       '%H:%M:%S %Y-%m-%d')

The SequenceMatcher.get_matching_blocks() method in the difflib module now guarantees to return a minimal list of blocks describing matching subsequences. Previously, the algorithm would occasionally break a block of matching elements into two list entries. (Enhancement by Tim Peters.)

The doctest module gained a SKIP option that keeps an example from being executed at all. This is intended for code snippets that are usage examples intended for the reader and aren’t actually test cases.

An encoding parameter was added to the testfile() function and the DocFileSuite class to specify the file’s encoding. This makes it easier to use non-ASCII characters in tests contained within a docstring. (Contributed by Bjorn Tillenius.)

email 包已经升级到 4.0 （由 Barry Warsaw 贡献）

The fileinput module was made more flexible. Unicode filenames are now supported, and a mode parameter that defaults to "r" was added to the input() function to allow opening files in binary or universal newlines mode. Another new parameter, openhook, lets you use a function other than open() to open the input files. Once you’re iterating over the set of files, the FileInput object’s new fileno() returns the file descriptor for the currently opened file. (Contributed by Georg Brandl.)

In the gc module, the new get_count() function returns a 3-tuple containing the current collection counts for the three GC generations. This is accounting information for the garbage collector; when these counts reach a specified threshold, a garbage collection sweep will be made. The existing gc.collect() function now takes an optional generation argument of 0, 1, or 2 to specify which generation to collect. (Contributed by Barry Warsaw.)

The nsmallest() and nlargest() functions in the heapq module now support a key keyword parameter similar to the one provided by the min()/max() functions and the sort() methods. For example:

>>> import heapq
>>> L = ["short", 'medium', 'longest', 'longer still']
>>> heapq.nsmallest(2, L)  # Return two lowest elements, lexicographically
['longer still', 'longest']
>>> heapq.nsmallest(2, L, key=len)   # Return two shortest elements
['short', 'medium']

（由 Raymond Hettinger 贡献。）

The itertools.islice() function now accepts None for the start and step arguments. This makes it more compatible with the attributes of slice objects, so that you can now write the following:

s = slice(5)     # Create slice object
itertools.islice(iterable, s.start, s.stop, s.step)

（由 Raymond Hettinger 贡献。）

The format() function in the locale module has been modified and two new functions were added, format_string() and currency().

The format() function’s val parameter could previously be a string as long as no more than one %char specifier appeared; now the parameter must be exactly one %char specifier with no surrounding text. An optional monetary parameter was also added which, if True, will use the locale’s rules for formatting currency in placing a separator between groups of three digits.

To format strings with multiple %char specifiers, use the new format_string() function that works like format() but also supports mixing %char specifiers with arbitrary text.

A new currency() function was also added that formats a number according to the current locale’s settings.

（由Georg Brandl 贡献。）

The mailbox module underwent a massive rewrite to add the capability to modify mailboxes in addition to reading them. A new set of classes that include mbox, MH, and Maildir are used to read mailboxes, and have an add(message) method to add messages, remove(key) to remove messages, and lock()/unlock() to lock/unlock the mailbox. The following example converts a maildir-format mailbox into an mbox-format one:

import mailbox

# 'factory=None' uses email.Message.Message as the class representing
# individual messages.
src = mailbox.Maildir('maildir', factory=None)
dest = mailbox.mbox('/tmp/mbox')

for msg in src:
    dest.add(msg)

(Contributed by Gregory K. Johnson. Funding was provided by Google’s 2005 Summer of Code.)

New module: the msilib module allows creating Microsoft Installer .msi files and CAB files. Some support for reading the .msi database is also included. (Contributed by Martin von Löwis.)

The nis module now supports accessing domains other than the system default domain by supplying a domain argument to the nis.match() and nis.maps() functions. (Contributed by Ben Bell.)

The operator module’s itemgetter() and attrgetter() functions now support multiple fields. A call such as operator.attrgetter('a', 'b') will return a function that retrieves the a and b attributes. Combining this new feature with the sort() method’s key parameter lets you easily sort lists using multiple fields. (Contributed by Raymond Hettinger.)

The optparse module was updated to version 1.5.1 of the Optik library. The OptionParser class gained an epilog attribute, a string that will be printed after the help message, and a destroy() method to break reference cycles created by the object. (Contributed by Greg Ward.)

The os module underwent several changes. The stat_float_times variable now defaults to true, meaning that os.stat() will now return time values as floats. (This doesn’t necessarily mean that os.stat() will return times that are precise to fractions of a second; not all systems support such precision.)

Constants named os.SEEK_SET, os.SEEK_CUR, and os.SEEK_END have been added; these are the parameters to the os.lseek() function. Two new constants for locking are os.O_SHLOCK and os.O_EXLOCK.

Two new functions, wait3() and wait4(), were added. They’re similar the waitpid() function which waits for a child process to exit and returns a tuple of the process ID and its exit status, but wait3() and wait4() return additional information. wait3() doesn’t take a process ID as input, so it waits for any child process to exit and returns a 3-tuple of process-id, exit-status, resource-usage as returned from the resource.getrusage() function. wait4(pid) does take a process ID. (Contributed by Chad J. Schroeder.)

On FreeBSD, the os.stat() function now returns times with nanosecond resolution, and the returned object now has st_gen and st_birthtime. The st_flags attribute is also available, if the platform supports it. (Contributed by Antti Louko and Diego Pettenò.)

The Python debugger provided by the pdb module can now store lists of commands to execute when a breakpoint is reached and execution stops. Once breakpoint #1 has been created, enter commands 1 and enter a series of commands to be executed, finishing the list with end. The command list can include commands that resume execution, such as continue or next. (Contributed by Grégoire Dooms.)

The pickle and cPickle modules no longer accept a return value of None from the __reduce__() method; the method must return a tuple of arguments instead. The ability to return None was deprecated in Python 2.4, so this completes the removal of the feature.

The pkgutil module, containing various utility functions for finding packages, was enhanced to support PEP 302’s import hooks and now also works for packages stored in ZIP-format archives. (Contributed by Phillip J. Eby.)

The pybench benchmark suite by Marc-André Lemburg is now included in the Tools/pybench directory. The pybench suite is an improvement on the commonly used pystone.py program because pybench provides a more detailed measurement of the interpreter’s speed. It times particular operations such as function calls, tuple slicing, method lookups, and numeric operations, instead of performing many different operations and reducing the result to a single number as pystone.py does.

The pyexpat module now uses version 2.0 of the Expat parser. (Contributed by Trent Mick.)

The Queue class provided by the Queue module gained two new methods. join() blocks until all items in the queue have been retrieved and all processing work on the items have been completed. Worker threads call the other new method, task_done(), to signal that processing for an item has been completed. (Contributed by Raymond Hettinger.)

The old regex and regsub modules, which have been deprecated ever since Python 2.0, have finally been deleted. Other deleted modules: statcache, tzparse, whrandom.

Also deleted: the lib-old directory, which includes ancient modules such as dircmp and ni, was removed. lib-old wasn’t on the default sys.path, so unless your programs explicitly added the directory to sys.path, this removal shouldn’t affect your code.

The rlcompleter module is no longer dependent on importing the readline module and therefore now works on non-Unix platforms. (Patch from Robert Kiendl.)

The SimpleXMLRPCServer and DocXMLRPCServer classes now have a rpc_paths attribute that constrains XML-RPC operations to a limited set of URL paths; the default is to allow only '/' and '/RPC2'. Setting rpc_paths to None or an empty tuple disables this path checking.

The socket module now supports AF_NETLINK sockets on Linux, thanks to a patch from Philippe Biondi. Netlink sockets are a Linux-specific mechanism for communications between a user-space process and kernel code; an introductory article about them is at https://www.linuxjournal.com/article/7356. In Python code, netlink addresses are represented as a tuple of 2 integers, (pid, group_mask).

Two new methods on socket objects, recv_into(buffer) and recvfrom_into(buffer), store the received data in an object that supports the buffer protocol instead of returning the data as a string. This means you can put the data directly into an array or a memory-mapped file.

Socket objects also gained getfamily(), gettype(), and getproto() accessor methods to retrieve the family, type, and protocol values for the socket.

New module: the spwd module provides functions for accessing the shadow password database on systems that support shadow passwords.

The struct is now faster because it compiles format strings into Struct objects with pack() and unpack() methods. This is similar to how the re module lets you create compiled regular expression objects. You can still use the module-level pack() and unpack() functions; they’ll create Struct objects and cache them. Or you can use Struct instances directly:

s = struct.Struct('ih3s')

data = s.pack(1972, 187, 'abc')
year, number, name = s.unpack(data)

You can also pack and unpack data to and from buffer objects directly using the pack_into(buffer, offset, v1, v2, ...) and unpack_from(buffer, offset) methods. This lets you store data directly into an array or a memory-mapped file.

(Struct objects were implemented by Bob Ippolito at the NeedForSpeed sprint. Support for buffer objects was added by Martin Blais, also at the NeedForSpeed sprint.)

The Python developers switched from CVS to Subversion during the 2.5 development process. Information about the exact build version is available as the sys.subversion variable, a 3-tuple of (interpreter-name, branch-name, revision-range). For example, at the time of writing my copy of 2.5 was reporting ('CPython', 'trunk', '45313:45315').

This information is also available to C extensions via the Py_GetBuildInfo() function that returns a string of build information like this: "trunk:45355:45356M, Apr 13 2006, 07:42:19". (Contributed by Barry Warsaw.)

Another new function, sys._current_frames(), returns the current stack frames for all running threads as a dictionary mapping thread identifiers to the topmost stack frame currently active in that thread at the time the function is called. (Contributed by Tim Peters.)

The TarFile class in the tarfile module now has an extractall() method that extracts all members from the archive into the current working directory. It’s also possible to set a different directory as the extraction target, and to unpack only a subset of the archive’s members.

The compression used for a tarfile opened in stream mode can now be autodetected using the mode 'r|*'. (Contributed by Lars Gustäbel.)

The threading module now lets you set the stack size used when new threads are created. The stack_size([*size*]) function returns the currently configured stack size, and supplying the optional size parameter sets a new value. Not all platforms support changing the stack size, but Windows, POSIX threading, and OS/2 all do. (Contributed by Andrew MacIntyre.)

The unicodedata module has been updated to use version 4.1.0 of the Unicode character database. Version 3.2.0 is required by some specifications, so it’s still available as unicodedata.ucd_3_2_0.

New module: the uuid module generates universally unique identifiers (UUIDs) according to RFC 4122 [https://tools.ietf.org/html/rfc4122.html]. The RFC defines several different UUID versions that are generated from a starting string, from system properties, or purely randomly. This module contains a UUID class and functions named uuid1(), uuid3(), uuid4(), and uuid5() to generate different versions of UUID. (Version 2 UUIDs are not specified in RFC 4122 [https://tools.ietf.org/html/rfc4122.html] and are not supported by this module.)

>>> import uuid
>>> # make a UUID based on the host ID and current time
>>> uuid.uuid1()
UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')

>>> # make a UUID using an MD5 hash of a namespace UUID and a name
>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')

>>> # make a random UUID
>>> uuid.uuid4()
UUID('16fd2706-8baf-433b-82eb-8c7fada847da')

>>> # make a UUID using a SHA-1 hash of a namespace UUID and a name
>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')

（由 Ka-Ping Yee 贡献。）

The weakref module’s WeakKeyDictionary and WeakValueDictionary types gained new methods for iterating over the weak references contained in the dictionary. iterkeyrefs() and keyrefs() methods were added to WeakKeyDictionary, and itervaluerefs() and valuerefs() were added to WeakValueDictionary. (Contributed by Fred L. Drake, Jr.)

The webbrowser module received a number of enhancements. It’s now usable as a script with python -m webbrowser, taking a URL as the argument; there are a number of switches to control the behaviour (-n for a new browser window, -t for a new tab). New module-level functions, open_new() and open_new_tab(), were added to support this. The module’s open() function supports an additional feature, an autoraise parameter that signals whether to raise the open window when possible. A number of additional browsers were added to the supported list such as Firefox, Opera, Konqueror, and elinks. (Contributed by Oleg Broytmann and Georg Brandl.)

The xmlrpclib module now supports returning datetime objects for the XML-RPC date type. Supply use_datetime=True to the loads() function or the Unmarshaller class to enable this feature. (Contributed by Skip Montanaro.)

The zipfile module now supports the ZIP64 version of the format, meaning that a .zip archive can now be larger than 4 GiB and can contain individual files larger than 4 GiB. (Contributed by Ronald Oussoren.)

The zlib module’s Compress and Decompress objects now support a copy() method that makes a copy of the object’s internal state and returns a new Compress or Decompress object. (Contributed by Chris AtLee.)

ctypes 包

The ctypes package, written by Thomas Heller, has been added to the standard library. ctypes lets you call arbitrary functions in shared libraries or DLLs. Long-time users may remember the dl module, which provides functions for loading shared libraries and calling functions in them. The ctypes package is much fancier.

To load a shared library or DLL, you must create an instance of the CDLL class and provide the name or path of the shared library or DLL. Once that’s done, you can call arbitrary functions by accessing them as attributes of the CDLL object.

import ctypes

libc = ctypes.CDLL('libc.so.6')
result = libc.printf("Line of output\n")

Type constructors for the various C types are provided: c_int(), c_float(), c_double(), c_char_p() (equivalent to char *), and so forth. Unlike Python’s types, the C versions are all mutable; you can assign to their value attribute to change the wrapped value. Python integers and strings will be automatically converted to the corresponding C types, but for other types you must call the correct type constructor. (And I mean must; getting it wrong will often result in the interpreter crashing with a segmentation fault.)

You shouldn’t use c_char_p() with a Python string when the C function will be modifying the memory area, because Python strings are supposed to be immutable; breaking this rule will cause puzzling bugs. When you need a modifiable memory area, use create_string_buffer():

s = "this is a string"
buf = ctypes.create_string_buffer(s)
libc.strfry(buf)

C functions are assumed to return integers, but you can set the restype attribute of the function object to change this:

>>> libc.atof('2.71828')
-1783957616
>>> libc.atof.restype = ctypes.c_double
>>> libc.atof('2.71828')
2.71828

ctypes also provides a wrapper for Python’s C API as the ctypes.pythonapi object. This object does not release the global interpreter lock before calling a function, because the lock must be held when calling into the interpreter’s code. There’s a py_object() type constructor that will create a PyObject * pointer. A simple usage:

import ctypes

d = {}
ctypes.pythonapi.PyObject_SetItem(ctypes.py_object(d),
          ctypes.py_object("abc"),  ctypes.py_object(1))
# d is now {'abc', 1}.

Don’t forget to use py_object(); if it’s omitted you end up with a segmentation fault.

ctypes has been around for a while, but people still write and distribution hand-coded extension modules because you can’t rely on ctypes being present. Perhaps developers will begin to write Python wrappers atop a library accessed through ctypes instead of extension modules, now that ctypes is included with core Python.

参见

http://starship.python.net/crew/theller/ctypes/

ctypes的主页，含教程， 参考和常见问题解答。

ctypes 模块的文档。

ElementTree 包

A subset of Fredrik Lundh’s ElementTree library for processing XML has been added to the standard library as xml.etree. The available modules are ElementTree, ElementPath, and ElementInclude from ElementTree 1.2.6. The cElementTree accelerator module is also included.

The rest of this section will provide a brief overview of using ElementTree. Full documentation for ElementTree is available at http://effbot.org/zone/element-index.htm.

ElementTree represents an XML document as a tree of element nodes. The text content of the document is stored as the text and tail attributes of (This is one of the major differences between ElementTree and the Document Object Model; in the DOM there are many different types of node, including TextNode.)

The most commonly used parsing function is parse(), that takes either a string (assumed to contain a filename) or a file-like object and returns an ElementTree instance:

from xml.etree import ElementTree as ET

tree = ET.parse('ex-1.xml')

feed = urllib.urlopen(
          'http://planet.python.org/rss10.xml')
tree = ET.parse(feed)

Once you have an ElementTree instance, you can call its getroot() method to get the root Element node.

There’s also an XML() function that takes a string literal and returns an Element node (not an ElementTree). This function provides a tidy way to incorporate XML fragments, approaching the convenience of an XML literal:

svg = ET.XML("""<svg width="10px" version="1.0">
             </svg>""")
svg.set('height', '320px')
svg.append(elem1)

Each XML element supports some dictionary-like and some list-like access methods. Dictionary-like operations are used to access attribute values, and list-like operations are used to access child nodes.

运算

结果：

elem[n]

返回第n个子元素

elem[m:n]

返回第m至第n个子元素的列表。

len(elem)

返回子元素的个数

list(elem)

返回子元素的列表

elem.append(elem2)

将 elem2 添加为子级。

elem.insert(index, elem2)

在指定位置插入 elem2 。

del elem[n]

删除第n个子元素

elem.keys()

返回属性名称的列表。

elem.get(name)

返回 name 属性的值。

elem.set(name, value)

为 name 属性设置新值

elem.attrib

检索包含属性的字典。

del elem.attrib[name]

Deletes 属性 name

Comments and processing instructions are also represented as Element nodes. To check if a node is a comment or processing instructions:

if elem.tag is ET.Comment:
    ...
elif elem.tag is ET.ProcessingInstruction:
    ...

To generate XML output, you should call the ElementTree.write() method. Like parse(), it can take either a string or a file-like object:

# Encoding is US-ASCII
tree.write('output.xml')

# Encoding is UTF-8
f = open('output.xml', 'w')
tree.write(f, encoding='utf-8')

(Caution: the default encoding used for output is ASCII. For general XML work, where an element’s name may contain arbitrary Unicode characters, ASCII isn’t a very useful encoding because it will raise an exception if an element’s name contains any characters with values greater than 127. Therefore, it’s best to specify a different encoding such as UTF-8 that can handle any Unicode character.)

This section is only a partial description of the ElementTree interfaces. Please read the package’s official documentation for more details.

参见

http://effbot.org/zone/element-index.htm

ElementTree 的官方文档

hashlib 包

A new hashlib module, written by Gregory P. Smith, has been added to replace the md5 and sha modules. hashlib adds support for additional secure hashes (SHA-224, SHA-256, SHA-384, and SHA-512). When available, the module uses OpenSSL for fast platform optimized implementations of algorithms.

The old md5 and sha modules still exist as wrappers around hashlib to preserve backwards compatibility. The new module’s interface is very close to that of the old modules, but not identical. The most significant difference is that the constructor functions for creating new hashing objects are named differently.

# Old versions
h = md5.md5()
h = md5.new()

# New version
h = hashlib.md5()

# Old versions
h = sha.sha()
h = sha.new()

# New version
h = hashlib.sha1()

# Hash that weren't previously available
h = hashlib.sha224()
h = hashlib.sha256()
h = hashlib.sha384()
h = hashlib.sha512()

# Alternative form
h = hashlib.new('md5')          # Provide algorithm as a string

Once a hash object has been created, its methods are the same as before: update(string) hashes the specified string into the current digest state, digest() and hexdigest() return the digest value as a binary string or a string of hex digits, and copy() returns a new hashing object with the same digest state.

参见

hashlib 模块的文档。

sqlite3 包

The pysqlite module (http://www.pysqlite.org), a wrapper for the SQLite embedded database, has been added to the standard library under the package name sqlite3.

SQLite 是一个C语言库，它可以提供一种轻量级的基于磁盘的数据库，这种数据库不需要独立的服务器进程，也允许需要使用一种非标准的 SQL 查询语言来访问它。一些应用程序可以使用 SQLite 作为内部数据存储。可以用它来创建一个应用程序原型，然后再迁移到更大的数据库，比如 PostgreSQL 或 Oracle。

pysqlite was written by Gerhard Häring and provides a SQL interface compliant with the DB-API 2.0 specification described by PEP 249 [https://www.python.org/dev/peps/pep-0249].

If you’re compiling the Python source yourself, note that the source tree doesn’t include the SQLite code, only the wrapper module. You’ll need to have the SQLite libraries and headers installed before compiling Python, and the build process will compile the module when the necessary headers are available.

To use the module, you must first create a Connection object that represents the database. Here the data will be stored in the /tmp/example file:

conn = sqlite3.connect('/tmp/example')

你也可以使用 :memory: 来创建一个内存中的数据库

Once you have a Connection, you can create a Cursor object and call its execute() method to perform SQL commands:

c = conn.cursor()

# Create table
c.execute('''create table stocks
(date text, trans text, symbol text,
 qty real, price real)''')

# Insert a row of data
c.execute("""insert into stocks
          values ('2006-01-05','BUY','RHAT',100,35.14)""")

Usually your SQL operations will need to use values from Python variables. You shouldn’t assemble your query using Python’s string operations because doing so is insecure; it makes your program vulnerable to an SQL injection attack.

Instead, use the DB-API’s parameter substitution. Put ? as a placeholder wherever you want to use a value, and then provide a tuple of values as the second argument to the cursor’s execute() method. (Other database modules may use a different placeholder, such as %s or :1.) For example:

# Never do this -- insecure!
symbol = 'IBM'
c.execute("... where symbol = '%s'" % symbol)

# Do this instead
t = (symbol,)
c.execute('select * from stocks where symbol=?', t)

# Larger example
for t in (('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
          ('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00),
          ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
         ):
    c.execute('insert into stocks values (?,?,?,?,?)', t)

To retrieve data after executing a SELECT statement, you can either treat the cursor as an iterator, call the cursor’s fetchone() method to retrieve a single matching row, or call fetchall() to get a list of the matching rows.

下面是一个使用迭代器形式的例子：

>>> c = conn.cursor()
>>> c.execute('select * from stocks order by price')
>>> for row in c:
...    print row
...
(u'2006-01-05', u'BUY', u'RHAT', 100, 35.140000000000001)
(u'2006-03-28', u'BUY', u'IBM', 1000, 45.0)
(u'2006-04-06', u'SELL', u'IBM', 500, 53.0)
(u'2006-04-05', u'BUY', u'MSOFT', 1000, 72.0)
>>>

For more information about the SQL dialect supported by SQLite, see https://www.sqlite.org.

参见

http://www.pysqlite.org

pysqlite 的主页

https://www.sqlite.org

SQLite的主页；它的文档详细描述了它所支持的 SQL 方言的语法和可用的数据类型。

sqlite3 模块的文档。

PEP 249 [https://www.python.org/dev/peps/pep-0249] - DB-API 2.0 规范

Marc-André Lemburg 写的 PEP。

wsgiref 包

The Web Server Gateway Interface (WSGI) v1.0 defines a standard interface between web servers and Python web applications and is described in PEP 333 [https://www.python.org/dev/peps/pep-0333]. The wsgiref package is a reference implementation of the WSGI specification.

The package includes a basic HTTP server that will run a WSGI application; this server is useful for debugging but isn’t intended for production use. Setting up a server takes only a few lines of code:

from wsgiref import simple_server

wsgi_app = ...

host = ''
port = 8000
httpd = simple_server.make_server(host, port, wsgi_app)
httpd.serve_forever()

参见

http://www.wsgi.org

WSGI相关资源的核心网站。

PEP 333 [https://www.python.org/dev/peps/pep-0333] - Python Web服务器网关接口 v1.0

PEP 由 Phillip J. Eby 撰写

构建和 C API 的改变

Changes to Python’s build process and to the C API include:

The Python source tree was converted from CVS to Subversion, in a complex migration procedure that was supervised and flawlessly carried out by Martin von Löwis. The procedure was developed as PEP 347 [https://www.python.org/dev/peps/pep-0347].

Coverity, a company that markets a source code analysis tool called Prevent, provided the results of their examination of the Python source code. The analysis found about 60 bugs that were quickly fixed. Many of the bugs were refcounting problems, often occurring in error-handling code. See https://scan.coverity.com for the statistics.

The largest change to the C API came from PEP 353 [https://www.python.org/dev/peps/pep-0353], which modifies the interpreter to use a Py_ssize_t type definition instead of int. See the earlier section PEP 353: 使用ssize_t作为索引类型 for a discussion of this change.

The design of the bytecode compiler has changed a great deal, no longer generating bytecode by traversing the parse tree. Instead the parse tree is converted to an abstract syntax tree (or AST), and it is the abstract syntax tree that’s traversed to produce the bytecode.

It’s possible for Python code to obtain AST objects by using the compile() built-in and specifying _ast.PyCF_ONLY_AST as the value of the flags parameter:

from _ast import PyCF_ONLY_AST
ast = compile("""a=0
for i in range(10):
    a += i
""", "<string>", 'exec', PyCF_ONLY_AST)

assignment = ast.body[0]
for_loop = ast.body[1]

No official documentation has been written for the AST code yet, but PEP 339 [https://www.python.org/dev/peps/pep-0339] discusses the design. To start learning about the code, read the definition of the various AST nodes in Parser/Python.asdl. A Python script reads this file and generates a set of C structure definitions in Include/Python-ast.h. The PyParser_ASTFromString() and PyParser_ASTFromFile(), defined in Include/pythonrun.h, take Python source as input and return the root of an AST representing the contents. This AST can then be turned into a code object by PyAST_Compile(). For more information, read the source code, and then ask questions on python-dev.

The AST code was developed under Jeremy Hylton’s management, and implemented by (in alphabetical order) Brett Cannon, Nick Coghlan, Grant Edwards, John Ehresman, Kurt Kaiser, Neal Norwitz, Tim Peters, Armin Rigo, and Neil Schemenauer, plus the participants in a number of AST sprints at conferences such as PyCon.

Evan Jones’s patch to obmalloc, first described in a talk at PyCon DC 2005, was applied. Python 2.4 allocated small objects in 256K-sized arenas, but never freed arenas. With this patch, Python will free arenas when they’re empty. The net effect is that on some platforms, when you allocate many objects, Python’s memory usage may actually drop when you delete them and the memory may be returned to the operating system. (Implemented by Evan Jones, and reworked by Tim Peters.)

Note that this change means extension modules must be more careful when allocating memory. Python’s API has many different functions for allocating memory that are grouped into families. For example, PyMem_Malloc(), PyMem_Realloc(), and PyMem_Free() are one family that allocates raw memory, while PyObject_Malloc(), PyObject_Realloc(), and PyObject_Free() are another family that’s supposed to be used for creating Python objects.

Previously these different families all reduced to the platform’s malloc() and free() functions. This meant it didn’t matter if you got things wrong and allocated memory with the PyMem() function but freed it with the PyObject() function. With 2.5’s changes to obmalloc, these families now do different things and mismatches will probably result in a segfault. You should carefully test your C extension modules with Python 2.5.

The built-in set types now have an official C API. Call PySet_New() and PyFrozenSet_New() to create a new set, PySet_Add() and PySet_Discard() to add and remove elements, and PySet_Contains() and PySet_Size() to examine the set’s state. (Contributed by Raymond Hettinger.)

C code can now obtain information about the exact revision of the Python interpreter by calling the Py_GetBuildInfo() function that returns a string of build information like this: "trunk:45355:45356M, Apr 13 2006, 07:42:19". (Contributed by Barry Warsaw.)

Two new macros can be used to indicate C functions that are local to the current file so that a faster calling convention can be used. Py_LOCAL(type) declares the function as returning a value of the specified type and uses a fast-calling qualifier. Py_LOCAL_INLINE(type) does the same thing and also requests the function be inlined. If PY_LOCAL_AGGRESSIVE() is defined before python.h is included, a set of more aggressive optimizations are enabled for the module; you should benchmark the results to find out if these optimizations actually make the code faster. (Contributed by Fredrik Lundh at the NeedForSpeed sprint.)

PyErr_NewException(name, base, dict) can now accept a tuple of base classes as its base argument. (Contributed by Georg Brandl.)

The PyErr_Warn() function for issuing warnings is now deprecated in favour of PyErr_WarnEx(category, message, stacklevel) which lets you specify the number of stack frames separating this function and the caller. A stacklevel of 1 is the function calling PyErr_WarnEx(), 2 is the function above that, and so forth. (Added by Neal Norwitz.)

The CPython interpreter is still written in C, but the code can now be compiled with a C++ compiler without errors. (Implemented by Anthony Baxter, Martin von Löwis, Skip Montanaro.)

The PyRange_New() function was removed. It was never documented, never used in the core code, and had dangerously lax error checking. In the unlikely case that your extensions were using it, you can replace it by something like the following:

range = PyObject_CallFunction((PyObject*) &PyRange_Type, "lll",
                              start, stop, step);

Port-Specific Changes

MacOS X (10.3 and higher): dynamic loading of modules now uses the dlopen() function instead of MacOS-specific functions.

MacOS X: an --enable-universalsdk switch was added to the configure script that compiles the interpreter as a universal binary able to run on both PowerPC and Intel processors. (Contributed by Ronald Oussoren; bpo-2573 [https://bugs.python.org/issue2573].)

Windows: .dll is no longer supported as a filename extension for extension modules. .pyd is now the only filename extension that will be searched for.

移植到Python 2.5

This section lists previously described changes that may require changes to your code:

ASCII is now the default encoding for modules. It’s now a syntax error if a module contains string literals with 8-bit characters but doesn’t have an encoding declaration. In Python 2.4 this triggered a warning, not a syntax error.

Previously, the gi_frame attribute of a generator was always a frame object. Because of the PEP 342 [https://www.python.org/dev/peps/pep-0342] changes described in section PEP 342: 生成器的新特性, it’s now possible for gi_frame to be None.

A new warning, UnicodeWarning, is triggered when you attempt to compare a Unicode string and an 8-bit string that can’t be converted to Unicode using the default ASCII encoding. Previously such comparisons would raise a UnicodeDecodeError exception.

Library: the csv module is now stricter about multi-line quoted fields. If your files contain newlines embedded within fields, the input should be split into lines in a manner which preserves the newline characters.

Library: the locale module’s format() function’s would previously accept any string as long as no more than one %char specifier appeared. In Python 2.5, the argument must be exactly one %char specifier with no surrounding text.

Library: The pickle and cPickle modules no longer accept a return value of None from the __reduce__() method; the method must return a tuple of arguments instead. The modules also no longer accept the deprecated bin keyword parameter.

Library: The SimpleXMLRPCServer and DocXMLRPCServer classes now have a rpc_paths attribute that constrains XML-RPC operations to a limited set of URL paths; the default is to allow only '/' and '/RPC2'. Setting rpc_paths to None or an empty tuple disables this path checking.

C API: Many functions now use Py_ssize_t instead of int to allow processing more data on 64-bit machines. Extension code may need to make the same change to avoid warnings and to support 64-bit machines. See the earlier section PEP 353: 使用ssize_t作为索引类型 for a discussion of this change.

C API: The obmalloc changes mean that you must be careful to not mix usage of the PyMem_*() and PyObject_*() families of functions. Memory allocated with one family’s *_Malloc() must be freed with the corresponding family’s *_Free() function.

致谢

作者感谢以下人员对本文各种草稿给予的建议，更正和协助： Georg Brandl, Nick Coghlan, Phillip J. Eby, Lars Gustäbel, Raymond Hettinger, Ralf W. Grosse-Kunstleve, Kent Johnson, Iain Lowe, Martin von Löwis, Fredrik Lundh, Andrew McNamara, Skip Montanaro, Gustavo Niemeyer, Paul Prescod, James Pryor, Mike Rovner, Scott Weikart, Barry Warsaw, Thomas Wouters.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

Python 2.4 有什么新变化

作者

A.M. Kuchling

本文介绍了2005年3月30日发布的 Python 2.4.1 的新功能。

Python 2.4 is a medium-sized release. It doesn’t introduce as many changes as the radical Python 2.2, but introduces more features than the conservative 2.3 release. The most significant new language features are function decorators and generator expressions; most other changes are to the standard library.

According to the CVS change logs, there were 481 patches applied and 502 bugs fixed between Python 2.3 and 2.4. Both figures are likely to be underestimates.

This article doesn’t attempt to provide a complete specification of every single new feature, but instead provides a brief introduction to each feature. For full details, you should refer to the documentation for Python 2.4, such as the Python Library Reference and the Python Reference Manual. Often you will be referred to the PEP for a particular new feature for explanations of the implementation and design rationale.

PEP 218: 内置集合对象

Python 2.3 introduced the sets module. C implementations of set data types have now been added to the Python core as two new built-in types, set(iterable) and frozenset(iterable). They provide high speed operations for membership testing, for eliminating duplicates from sequences, and for mathematical operations like unions, intersections, differences, and symmetric differences.

>>> a = set('abracadabra')              # form a set from a string
>>> 'z' in a                            # fast membership testing
False
>>> a                                   # unique letters in a
set(['a', 'r', 'b', 'c', 'd'])
>>> ''.join(a)                          # convert back into a string
'arbcd'

>>> b = set('alacazam')                 # form a second set
>>> a - b                               # letters in a but not in b
set(['r', 'd', 'b'])
>>> a | b                               # letters in either a or b
set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'])
>>> a & b                               # letters in both a and b
set(['a', 'c'])
>>> a ^ b                               # letters in a or b but not both
set(['r', 'd', 'b', 'm', 'z', 'l'])

>>> a.add('z')                          # add a new element
>>> a.update('wxy')                     # add multiple new elements
>>> a
set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z'])
>>> a.remove('x')                       # take one element out
>>> a
set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z'])

The frozenset() type is an immutable version of set(). Since it is immutable and hashable, it may be used as a dictionary key or as a member of another set.

The sets module remains in the standard library, and may be useful if you wish to subclass the Set or ImmutableSet classes. There are currently no plans to deprecate the module.

参见

PEP 218 [https://www.python.org/dev/peps/pep-0218] - 添加内置Set对象类型

最初由 Greg Wilson 提出，由 Raymond Hettinger 最终实现。

PEP 237: 统一长整数和整数

The lengthy transition process for this PEP, begun in Python 2.2, takes another step forward in Python 2.4. In 2.3, certain integer operations that would behave differently after int/long unification triggered FutureWarning warnings and returned values limited to 32 or 64 bits (depending on your platform). In 2.4, these expressions no longer produce a warning and instead produce a different result that’s usually a long integer.

The problematic expressions are primarily left shifts and lengthy hexadecimal and octal constants. For example, 2 << 32 results in a warning in 2.3, evaluating to 0 on 32-bit platforms. In Python 2.4, this expression now returns the correct answer, 8589934592.

参见

PEP 237 [https://www.python.org/dev/peps/pep-0237] - 统一长整数和整数

原始PEP由 Moshe Zadka 和 GvR 撰写，2.4 的变更由 Kalle Svensson 实现。

PEP 289: 生成器表达式

The iterator feature introduced in Python 2.2 and the itertools module make it easier to write programs that loop through large data sets without having the entire data set in memory at one time. List comprehensions don’t fit into this picture very well because they produce a Python list object containing all of the items. This unavoidably pulls all of the objects into memory, which can be a problem if your data set is very large. When trying to write a functionally-styled program, it would be natural to write something like:

links = [link for link in get_all_links() if not link.followed]
for link in links:
    ...

代替：

for link in get_all_links():
    if link.followed:
        continue
    ...

The first form is more concise and perhaps more readable, but if you’re dealing with a large number of link objects you’d have to write the second form to avoid having all link objects in memory at the same time.

Generator expressions work similarly to list comprehensions but don’t materialize the entire list; instead they create a generator that will return elements one by one. The above example could be written as:

links = (link for link in get_all_links() if not link.followed)
for link in links:
    ...

Generator expressions always have to be written inside parentheses, as in the above example. The parentheses signalling a function call also count, so if you want to create an iterator that will be immediately passed to a function you could write:

print sum(obj.count for obj in list_all_objects())

Generator expressions differ from list comprehensions in various small ways. Most notably, the loop variable (obj in the above example) is not accessible outside of the generator expression. List comprehensions leave the variable assigned to its last value; future versions of Python will change this, making list comprehensions match generator expressions in this respect.

参见

PEP 289 [https://www.python.org/dev/peps/pep-0289] - 生成器表达式

Proposed by Raymond Hettinger and implemented by Jiwon Seo with early efforts steered by Hye-Shik Chang.

PEP 292: Simpler String Substitutions

Some new classes in the standard library provide an alternative mechanism for substituting variables into strings; this style of substitution may be better for applications where untrained users need to edit templates.

The usual way of substituting variables by name is the % operator:

>>> '%(page)i: %(title)s' % {'page':2, 'title': 'The Best of Times'}
'2: The Best of Times'

When writing the template string, it can be easy to forget the i or s after the closing parenthesis. This isn’t a big problem if the template is in a Python module, because you run the code, get an “Unsupported format character” ValueError, and fix the problem. However, consider an application such as Mailman where template strings or translations are being edited by users who aren’t aware of the Python language. The format string’s syntax is complicated to explain to such users, and if they make a mistake, it’s difficult to provide helpful feedback to them.

PEP 292 adds a Template class to the string module that uses $ to indicate a substitution:

>>> import string
>>> t = string.Template('$page: $title')
>>> t.substitute({'page':2, 'title': 'The Best of Times'})
'2: The Best of Times'

If a key is missing from the dictionary, the substitute() method will raise a KeyError. There’s also a safe_substitute() method that ignores missing keys:

>>> t = string.Template('$page: $title')
>>> t.safe_substitute({'page':3})
'3: $title'

参见

PEP 292 [https://www.python.org/dev/peps/pep-0292] - Simpler String Substitutions

由 Barry Warsaw 撰写并实现

PEP 318: Decorators for Functions and Methods

Python 2.2 extended Python’s object model by adding static methods and class methods, but it didn’t extend Python’s syntax to provide any new way of defining static or class methods. Instead, you had to write a def statement in the usual way, and pass the resulting method to a staticmethod() or classmethod() function that would wrap up the function as a method of the new type. Your code would look like this:

class C:
   def meth (cls):
       ...

   meth = classmethod(meth)   # Rebind name to wrapped-up class method

If the method was very long, it would be easy to miss or forget the classmethod() invocation after the function body.

The intention was always to add some syntax to make such definitions more readable, but at the time of 2.2’s release a good syntax was not obvious. Today a good syntax still isn’t obvious but users are asking for easier access to the feature; a new syntactic feature has been added to meet this need.

The new feature is called “function decorators”. The name comes from the idea that classmethod(), staticmethod(), and friends are storing additional information on a function object; they’re decorating functions with more details.

The notation borrows from Java and uses the '@' character as an indicator. Using the new syntax, the example above would be written:

class C:

   @classmethod
   def meth (cls):
       ...

The @classmethod is shorthand for the meth=classmethod(meth) assignment. More generally, if you have the following:

@A
@B
@C
def f ():
    ...

It’s equivalent to the following pre-decorator code:

def f(): ...
f = A(B(C(f)))

Decorators must come on the line before a function definition, one decorator per line, and can’t be on the same line as the def statement, meaning that @A def f(): ... is illegal. You can only decorate function definitions, either at the module level or inside a class; you can’t decorate class definitions.

A decorator is just a function that takes the function to be decorated as an argument and returns either the same function or some new object. The return value of the decorator need not be callable (though it typically is), unless further decorators will be applied to the result. It’s easy to write your own decorators. The following simple example just sets an attribute on the function object:

>>> def deco(func):
...    func.attr = 'decorated'
...    return func
...
>>> @deco
... def f(): pass
...
>>> f
<function f at 0x402ef0d4>
>>> f.attr
'decorated'
>>>

As a slightly more realistic example, the following decorator checks that the supplied argument is an integer:

def require_int (func):
    def wrapper (arg):
        assert isinstance(arg, int)
        return func(arg)

    return wrapper

@require_int
def p1 (arg):
    print arg

@require_int
def p2(arg):
    print arg*2

An example in PEP 318 [https://www.python.org/dev/peps/pep-0318] contains a fancier version of this idea that lets you both specify the required type and check the returned type.

Decorator functions can take arguments. If arguments are supplied, your decorator function is called with only those arguments and must return a new decorator function; this function must take a single function and return a function, as previously described. In other words, @A @B @C(args) becomes:

def f(): ...
_deco = C(args)
f = A(B(_deco(f)))

Getting this right can be slightly brain-bending, but it’s not too difficult.

A small related change makes the func_name attribute of functions writable. This attribute is used to display function names in tracebacks, so decorators should change the name of any new function that’s constructed and returned.

参见

PEP 318 [https://www.python.org/dev/peps/pep-0318] - Decorators for Functions, Methods and Classes

Written by Kevin D. Smith, Jim Jewett, and Skip Montanaro. Several people wrote patches implementing function decorators, but the one that was actually checked in was patch #979728, written by Mark Russell.

https://wiki.python.org/moin/PythonDecoratorLibrary

该Wiki页面包含几个装饰器示例。

PEP 322: 反向迭代

A new built-in function, reversed(seq), takes a sequence and returns an iterator that loops over the elements of the sequence in reverse order.

>>> for i in reversed(xrange(1,4)):
...    print i
...
3
2
1

Compared to extended slicing, such as range(1,4)[::-1], reversed() is easier to read, runs faster, and uses substantially less memory.

Note that reversed() only accepts sequences, not arbitrary iterators. If you want to reverse an iterator, first convert it to a list with list().

>>> input = open('/etc/passwd', 'r')
>>> for line in reversed(list(input)):
...   print line
...
root:*:0:0:System Administrator:/var/root:/bin/tcsh
  ...

参见

PEP 322 [https://www.python.org/dev/peps/pep-0322] - 反向迭代

由 Raymond Hettinger 撰写并实现。

PEP 324: 新的子进程模块

The standard library provides a number of ways to execute a subprocess, offering different features and different levels of complexity. os.system(command) is easy to use, but slow (it runs a shell process which executes the command) and dangerous (you have to be careful about escaping the shell’s metacharacters). The popen2 module offers classes that can capture standard output and standard error from the subprocess, but the naming is confusing. The subprocess module cleans this up, providing a unified interface that offers all the features you might need.

Instead of popen2’s collection of classes, subprocess contains a single class called Popen whose constructor supports a number of different keyword arguments.

class Popen(args, bufsize=0, executable=None,
            stdin=None, stdout=None, stderr=None,
            preexec_fn=None, close_fds=False, shell=False,
            cwd=None, env=None, universal_newlines=False,
            startupinfo=None, creationflags=0):

args is commonly a sequence of strings that will be the arguments to the program executed as the subprocess. (If the shell argument is true, args can be a string which will then be passed on to the shell for interpretation, just as os.system() does.)

stdin, stdout, and stderr specify what the subprocess’s input, output, and error streams will be. You can provide a file object or a file descriptor, or you can use the constant subprocess.PIPE to create a pipe between the subprocess and the parent.

The constructor has a number of handy options:

close_fds requests that all file descriptors be closed before running the subprocess.

cwd specifies the working directory in which the subprocess will be executed (defaulting to whatever the parent’s working directory is).

env is a dictionary specifying environment variables.

preexec_fn is a function that gets called before the child is started.

universal_newlines opens the child’s input and output using Python’s universal newlines feature.

Once you’ve created the Popen instance, you can call its wait() method to pause until the subprocess has exited, poll() to check if it’s exited without pausing, or communicate(data) to send the string data to the subprocess’s standard input. communicate(data) then reads any data that the subprocess has sent to its standard output or standard error, returning a tuple (stdout_data, stderr_data).

call() is a shortcut that passes its arguments along to the Popen constructor, waits for the command to complete, and returns the status code of the subprocess. It can serve as a safer analog to os.system():

sts = subprocess.call(['dpkg', '-i', '/tmp/new-package.deb'])
if sts == 0:
    # Success
    ...
else:
    # dpkg returned an error
    ...

The command is invoked without use of the shell. If you really do want to use the shell, you can add shell=True as a keyword argument and provide a string instead of a sequence:

sts = subprocess.call('dpkg -i /tmp/new-package.deb', shell=True)

The PEP takes various examples of shell and Python code and shows how they’d be translated into Python code that uses subprocess. Reading this section of the PEP is highly recommended.

参见

PEP 324 [https://www.python.org/dev/peps/pep-0324] - 子进程 - 新的进程模块

由 Peter Åstrand 在 Fredrik Lundh 等人的协助下撰写并实现。

PEP 327: 十进数据类型

Python has always supported floating-point (FP) numbers, based on the underlying C double type, as a data type. However, while most programming languages provide a floating-point type, many people (even programmers) are unaware that floating-point numbers don’t represent certain decimal fractions accurately. The new Decimal type can represent these fractions accurately, up to a user-specified precision limit.

为什么需要十进制？

The limitations arise from the representation used for floating-point numbers. FP numbers are made up of three components:

The sign, which is positive or negative.

The mantissa, which is a single-digit binary number followed by a fractional part. For example, 1.01 in base-2 notation is 1 + 0/2 + 1/4, or 1.25 in decimal notation.

The exponent, which tells where the decimal point is located in the number represented.

For example, the number 1.25 has positive sign, a mantissa value of 1.01 (in binary), and an exponent of 0 (the decimal point doesn’t need to be shifted). The number 5 has the same sign and mantissa, but the exponent is 2 because the mantissa is multiplied by 4 (2 to the power of the exponent 2); 1.25 * 4 equals 5.

Modern systems usually provide floating-point support that conforms to a standard called IEEE 754. C’s double type is usually implemented as a 64-bit IEEE 754 number, which uses 52 bits of space for the mantissa. This means that numbers can only be specified to 52 bits of precision. If you’re trying to represent numbers whose expansion repeats endlessly, the expansion is cut off after 52 bits. Unfortunately, most software needs to produce output in base 10, and common fractions in base 10 are often repeating decimals in binary. For example, 1.1 decimal is binary 1.0001100110011 ...; .1 = 1/16 + 1/32 + 1/256 plus an infinite number of additional terms. IEEE 754 has to chop off that infinitely repeated decimal after 52 digits, so the representation is slightly inaccurate.

Sometimes you can see this inaccuracy when the number is printed:

>>> 1.1
1.1000000000000001

The inaccuracy isn’t always visible when you print the number because the FP-to-decimal-string conversion is provided by the C library, and most C libraries try to produce sensible output. Even if it’s not displayed, however, the inaccuracy is still there and subsequent operations can magnify the error.

For many applications this doesn’t matter. If I’m plotting points and displaying them on my monitor, the difference between 1.1 and 1.1000000000000001 is too small to be visible. Reports often limit output to a certain number of decimal places, and if you round the number to two or three or even eight decimal places, the error is never apparent. However, for applications where it does matter, it’s a lot of work to implement your own custom arithmetic routines.

因此，创建了 Decimal 类型。

Decimal 类型

A new module, decimal, was added to Python’s standard library. It contains two classes, Decimal and Context. Decimal instances represent numbers, and Context instances are used to wrap up various settings such as the precision and default rounding mode.

Decimal instances are immutable, like regular Python integers and FP numbers; once it’s been created, you can’t change the value an instance represents. Decimal instances can be created from integers or strings:

>>> import decimal
>>> decimal.Decimal(1972)
Decimal("1972")
>>> decimal.Decimal("1.1")
Decimal("1.1")

You can also provide tuples containing the sign, the mantissa represented as a tuple of decimal digits, and the exponent:

>>> decimal.Decimal((1, (1, 4, 7, 5), -2))
Decimal("-14.75")

Cautionary note: the sign bit is a Boolean value, so 0 is positive and 1 is negative.

Converting from floating-point numbers poses a bit of a problem: should the FP number representing 1.1 turn into the decimal number for exactly 1.1, or for 1.1 plus whatever inaccuracies are introduced? The decision was to dodge the issue and leave such a conversion out of the API. Instead, you should convert the floating-point number into a string using the desired precision and pass the string to the Decimal constructor:

>>> f = 1.1
>>> decimal.Decimal(str(f))
Decimal("1.1")
>>> decimal.Decimal('%.12f' % f)
Decimal("1.100000000000")

Once you have Decimal instances, you can perform the usual mathematical operations on them. One limitation: exponentiation requires an integer exponent:

>>> a = decimal.Decimal('35.72')
>>> b = decimal.Decimal('1.73')
>>> a+b
Decimal("37.45")
>>> a-b
Decimal("33.99")
>>> a*b
Decimal("61.7956")
>>> a/b
Decimal("20.64739884393063583815028902")
>>> a ** 2
Decimal("1275.9184")
>>> a**b
Traceback (most recent call last):
  ...
decimal.InvalidOperation: x ** (non-integer)

You can combine Decimal instances with integers, but not with floating-point numbers:

>>> a + 4
Decimal("39.72")
>>> a + 4.5
Traceback (most recent call last):
  ...
TypeError: You can interact Decimal only with int, long or Decimal data types.
>>>

Decimal numbers can be used with the math and cmath modules, but note that they’ll be immediately converted to floating-point numbers before the operation is performed, resulting in a possible loss of precision and accuracy. You’ll also get back a regular floating-point number and not a Decimal.

>>> import math, cmath
>>> d = decimal.Decimal('123456789012.345')
>>> math.sqrt(d)
351364.18288201344
>>> cmath.sqrt(-d)
351364.18288201344j

Decimal instances have a sqrt() method that returns a Decimal, but if you need other things such as trigonometric functions you’ll have to implement them.

>>> d.sqrt()
Decimal("351364.1828820134592177245001")

Context 类型

Instances of the Context class encapsulate several settings for decimal operations:

prec is the precision, the number of decimal places.

rounding specifies the rounding mode. The decimal module has constants for the various possibilities: ROUND_DOWN, ROUND_CEILING, ROUND_HALF_EVEN, and various others.

traps is a dictionary specifying what happens on encountering certain error conditions: either an exception is raised or a value is returned. Some examples of error conditions are division by zero, loss of precision, and overflow.

There’s a thread-local default context available by calling getcontext(); you can change the properties of this context to alter the default precision, rounding, or trap handling. The following example shows the effect of changing the precision of the default context:

>>> decimal.getcontext().prec
28
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal("0.1428571428571428571428571429")
>>> decimal.getcontext().prec = 9
>>> decimal.Decimal(1) / decimal.Decimal(7)
Decimal("0.142857143")

The default action for error conditions is selectable; the module can either return a special value such as infinity or not-a-number, or exceptions can be raised:

>>> decimal.Decimal(1) / decimal.Decimal(0)
Traceback (most recent call last):
  ...
decimal.DivisionByZero: x / 0
>>> decimal.getcontext().traps[decimal.DivisionByZero] = False
>>> decimal.Decimal(1) / decimal.Decimal(0)
Decimal("Infinity")
>>>

The Context instance also has various methods for formatting numbers such as to_eng_string() and to_sci_string().

For more information, see the documentation for the decimal module, which includes a quick-start tutorial and a reference.

参见

PEP 327 [https://www.python.org/dev/peps/pep-0327] - 十进数据类型

由 Facundo Batista 撰写，由Facundo Batista, Eric Price, Raymond Hettinger, Aahz 和 Tim Peters 实现。

http://www.lahey.com/float.htm

The article uses Fortran code to illustrate many of the problems that floating-point inaccuracy can cause.

http://speleotrove.com/decimal/

A description of a decimal-based representation. This representation is being proposed as a standard, and underlies the new Python decimal type. Much of this material was written by Mike Cowlishaw, designer of the Rexx language.

PEP 328: 多行导入

One language change is a small syntactic tweak aimed at making it easier to import many names from a module. In a from module import names statement, names is a sequence of names separated by commas. If the sequence is very long, you can either write multiple imports from the same module, or you can use backslashes to escape the line endings like this:

from SimpleXMLRPCServer import SimpleXMLRPCServer,\
            SimpleXMLRPCRequestHandler,\
            CGIXMLRPCRequestHandler,\
            resolve_dotted_attribute

The syntactic change in Python 2.4 simply allows putting the names within parentheses. Python ignores newlines within a parenthesized expression, so the backslashes are no longer needed:

from SimpleXMLRPCServer import (SimpleXMLRPCServer,
                                SimpleXMLRPCRequestHandler,
                                CGIXMLRPCRequestHandler,
                                resolve_dotted_attribute)

The PEP also proposes that all import statements be absolute imports, with a leading . character to indicate a relative import. This part of the PEP was not implemented for Python 2.4, but was completed for Python 2.5.

参见

PEP 328 [https://www.python.org/dev/peps/pep-0328] - 导入：多行和绝对/相对导入

由 Aahz 撰写，多行导入由 Dima Dorfman 实现。

PEP 331: Locale-Independent Float/String Conversions

The locale modules lets Python software select various conversions and display conventions that are localized to a particular country or language. However, the module was careful to not change the numeric locale because various functions in Python’s implementation required that the numeric locale remain set to the 'C' locale. Often this was because the code was using the C library’s atof() function.

Not setting the numeric locale caused trouble for extensions that used third-party C libraries, however, because they wouldn’t have the correct locale set. The motivating example was GTK+, whose user interface widgets weren’t displaying numbers in the current locale.

The solution described in the PEP is to add three new functions to the Python API that perform ASCII-only conversions, ignoring the locale setting:

PyOS_ascii_strtod(str, ptr) and PyOS_ascii_atof(str, ptr) both convert a string to a C double.

PyOS_ascii_formatd(buffer, buf_len, format, d) converts a double to an ASCII string.

The code for these functions came from the GLib library (https://developer.gnome.org/glib/stable/), whose developers kindly relicensed the relevant functions and donated them to the Python Software Foundation. The locale module can now change the numeric locale, letting extensions such as GTK+ produce the correct results.

参见

PEP 331 [https://www.python.org/dev/peps/pep-0331] - Locale-Independent Float/String Conversions

由Christian R. Reis撰写，由 Gustavo Carneiro 实现。

其他语言特性修改

Here are all of the changes that Python 2.4 makes to the core Python language.

Decorators for functions and methods were added (PEP 318 [https://www.python.org/dev/peps/pep-0318]).

Built-in set() and frozenset() types were added (PEP 218 [https://www.python.org/dev/peps/pep-0218]). Other new built-ins include the reversed(seq) function (PEP 322 [https://www.python.org/dev/peps/pep-0322]).

Generator expressions were added (PEP 289 [https://www.python.org/dev/peps/pep-0289]).

Certain numeric expressions no longer return values restricted to 32 or 64 bits (PEP 237 [https://www.python.org/dev/peps/pep-0237]).

You can now put parentheses around the list of names in a from module import names statement (PEP 328 [https://www.python.org/dev/peps/pep-0328]).

The dict.update() method now accepts the same argument forms as the dict constructor. This includes any mapping, any iterable of key/value pairs, and keyword arguments. (Contributed by Raymond Hettinger.)

The string methods ljust(), rjust(), and center() now take an optional argument for specifying a fill character other than a space. (Contributed by Raymond Hettinger.)

Strings also gained an rsplit() method that works like the split() method but splits from the end of the string. (Contributed by Sean Reifschneider.)

>>> 'www.python.org'.split('.', 1)
['www', 'python.org']
'www.python.org'.rsplit('.', 1)
['www.python', 'org']

Three keyword parameters, cmp, key, and reverse, were added to the sort() method of lists. These parameters make some common usages of sort() simpler. All of these parameters are optional.

For the cmp parameter, the value should be a comparison function that takes two parameters and returns -1, 0, or +1 depending on how the parameters compare. This function will then be used to sort the list. Previously this was the only parameter that could be provided to sort().

key should be a single-parameter function that takes a list element and returns a comparison key for the element. The list is then sorted using the comparison keys. The following example sorts a list case-insensitively:

>>> L = ['A', 'b', 'c', 'D']
>>> L.sort()                 # Case-sensitive sort
>>> L
['A', 'D', 'b', 'c']
>>> # Using 'key' parameter to sort list
>>> L.sort(key=lambda x: x.lower())
>>> L
['A', 'b', 'c', 'D']
>>> # Old-fashioned way
>>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
>>> L
['A', 'b', 'c', 'D']

The last example, which uses the cmp parameter, is the old way to perform a case-insensitive sort. It works but is slower than using a key parameter. Using key calls lower() method once for each element in the list while using cmp will call it twice for each comparison, so using key saves on invocations of the lower() method.

For simple key functions and comparison functions, it is often possible to avoid a lambda expression by using an unbound method instead. For example, the above case-insensitive sort is best written as:

>>> L.sort(key=str.lower)
>>> L
['A', 'b', 'c', 'D']

Finally, the reverse parameter takes a Boolean value. If the value is true, the list will be sorted into reverse order. Instead of L.sort(); L.reverse(), you can now write L.sort(reverse=True).

The results of sorting are now guaranteed to be stable. This means that two entries with equal keys will be returned in the same order as they were input. For example, you can sort a list of people by name, and then sort the list by age, resulting in a list sorted by age where people with the same age are in name-sorted order.

(All changes to sort() contributed by Raymond Hettinger.)

There is a new built-in function sorted(iterable) that works like the in-place list.sort() method but can be used in expressions. The differences are:

the input may be any iterable;

a newly formed copy is sorted, leaving the original intact; and

the expression returns the new sorted copy

>>> L = [9,7,8,3,2,4,1,6,5]
>>> [10+i for i in sorted(L)]       # usable in a list comprehension
[11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> L                               # original is left unchanged
[9,7,8,3,2,4,1,6,5]
>>> sorted('Monty Python')          # any iterable may be an input
[' ', 'M', 'P', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y', 'y']

>>> # List the contents of a dict sorted by key values
>>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5)
>>> for k, v in sorted(colormap.iteritems()):
...     print k, v
...
black 4
blue 2
green 3
red 1
yellow 5

（由 Raymond Hettinger 贡献。）

Integer operations will no longer trigger an OverflowWarning. The OverflowWarning warning will disappear in Python 2.5.

The interpreter gained a new switch, -m, that takes a name, searches for the corresponding module on sys.path, and runs the module as a script. For example, you can now run the Python profiler with python -m profile. (Contributed by Nick Coghlan.)

The eval(expr, globals, locals) and execfile(filename, globals, locals) functions and the exec statement now accept any mapping type for the locals parameter. Previously this had to be a regular Python dictionary. (Contributed by Raymond Hettinger.)

The zip() built-in function and itertools.izip() now return an empty list if called with no arguments. Previously they raised a TypeError exception. This makes them more suitable for use with variable length argument lists:

>>> def transpose(array):
...    return zip(*array)
...
>>> transpose([(1,2,3), (4,5,6)])
[(1, 4), (2, 5), (3, 6)]
>>> transpose([])
[]

（由 Raymond Hettinger 贡献。）

Encountering a failure while importing a module no longer leaves a partially-initialized module object in sys.modules. The incomplete module object left behind would fool further imports of the same module into succeeding, leading to confusing errors. (Fixed by Tim Peters.)

None is now a constant; code that binds a new value to the name None is now a syntax error. (Contributed by Raymond Hettinger.)

性能优化

The inner loops for list and tuple slicing were optimized and now run about one-third faster. The inner loops for dictionaries were also optimized, resulting in performance boosts for keys(), values(), items(), iterkeys(), itervalues(), and iteritems(). (Contributed by Raymond Hettinger.)

The machinery for growing and shrinking lists was optimized for speed and for space efficiency. Appending and popping from lists now runs faster due to more efficient code paths and less frequent use of the underlying system realloc(). List comprehensions also benefit. list.extend() was also optimized and no longer converts its argument into a temporary list before extending the base list. (Contributed by Raymond Hettinger.)

list(), tuple(), map(), filter(), and zip() now run several times faster with non-sequence arguments that supply a __len__() method. (Contributed by Raymond Hettinger.)

The methods list.__getitem__(), dict.__getitem__(), and dict.__contains__() are now implemented as method_descriptor objects rather than wrapper_descriptor objects. This form of access doubles their performance and makes them more suitable for use as arguments to functionals: map(mydict.__getitem__, keylist). (Contributed by Raymond Hettinger.)

Added a new opcode, LIST_APPEND, that simplifies the generated bytecode for list comprehensions and speeds them up by about a third. (Contributed by Raymond Hettinger.)

The peephole bytecode optimizer has been improved to produce shorter, faster bytecode; remarkably, the resulting bytecode is more readable. (Enhanced by Raymond Hettinger.)

String concatenations in statements of the form s = s + "abc" and s += "abc" are now performed more efficiently in certain circumstances. This optimization won’t be present in other Python implementations such as Jython, so you shouldn’t rely on it; using the join() method of strings is still recommended when you want to efficiently glue a large number of strings together. (Contributed by Armin Rigo.)

The net result of the 2.4 optimizations is that Python 2.4 runs the pystone benchmark around 5% faster than Python 2.3 and 35% faster than Python 2.2. (pystone is not a particularly good benchmark, but it’s the most commonly used measurement of Python’s performance. Your own applications may show greater or smaller benefits from Python 2.4.)

新增，改进和弃用的模块

As usual, Python’s standard library received a number of enhancements and bug fixes. Here’s a partial list of the most notable changes, sorted alphabetically by module name. Consult the Misc/NEWS file in the source tree for a more complete list of changes, or look through the CVS logs for all the details.

The asyncore module’s loop() function now has a count parameter that lets you perform a limited number of passes through the polling loop. The default is still to loop forever.

The base64 module now has more complete RFC 3548 [https://tools.ietf.org/html/rfc3548.html] support for Base64, Base32, and Base16 encoding and decoding, including optional case folding and optional alternative alphabets. (Contributed by Barry Warsaw.)

The bisect module now has an underlying C implementation for improved performance. (Contributed by Dmitry Vasiliev.)

由 Hye-Shik Chang 维护的东亚编解码器的 CJKCodecs 集合已集成到 2.4 中。新的编码为：

汉语（台湾）:: gb2312, gbk, gb18030, big5hkscs, hz

汉语（大陆）: big5, cp950

日语: cp932, euc-jis-2004, euc-jp, euc-jisx0213, iso-2022-jp,

iso-2022-jp-1, iso-2022-jp-2, iso-2022-jp-3, iso-2022-jp-ext, iso-2022-jp-2004, shift-jis, shift-jisx0213, shift-jis-2004

韩语: cp949, euc-kr, johab, iso-2022-kr

添加了其他一些新的编码：HP Roman8, ISO_8859-11, ISO_8859-16, PCTP-154 和 TIS-620

The UTF-8 and UTF-16 codecs now cope better with receiving partial input. Previously the StreamReader class would try to read more data, making it impossible to resume decoding from the stream. The read() method will now return as much data as it can and future calls will resume decoding where previous ones left off. (Implemented by Walter Dörwald.)

There is a new collections module for various specialized collection datatypes. Currently it contains just one type, deque, a double-ended queue that supports efficiently adding and removing elements from either end:

>>> from collections import deque
>>> d = deque('ghi')        # make a new deque with three items
>>> d.append('j')           # add a new entry to the right side
>>> d.appendleft('f')       # add a new entry to the left side
>>> d                       # show the representation of the deque
deque(['f', 'g', 'h', 'i', 'j'])
>>> d.pop()                 # return and remove the rightmost item
'j'
>>> d.popleft()             # return and remove the leftmost item
'f'
>>> list(d)                 # list the contents of the deque
['g', 'h', 'i']
>>> 'h' in d                # search the deque
True

Several modules, such as the Queue and threading modules, now take advantage of collections.deque for improved performance. (Contributed by Raymond Hettinger.)

The ConfigParser classes have been enhanced slightly. The read() method now returns a list of the files that were successfully parsed, and the set() method raises TypeError if passed a value argument that isn’t a string. (Contributed by John Belmonte and David Goodger.)

The curses module now supports the ncurses extension use_default_colors(). On platforms where the terminal supports transparency, this makes it possible to use a transparent background. (Contributed by Jörg Lehmann.)

The difflib module now includes an HtmlDiff class that creates an HTML table showing a side by side comparison of two versions of a text. (Contributed by Dan Gass.)

The email package was updated to version 3.0, which dropped various deprecated APIs and removes support for Python versions earlier than 2.3. The 3.0 version of the package uses a new incremental parser for MIME messages, available in the email.FeedParser module. The new parser doesn’t require reading the entire message into memory, and doesn’t raise exceptions if a message is malformed; instead it records any problems in the defect attribute of the message. (Developed by Anthony Baxter, Barry Warsaw, Thomas Wouters, and others.)

The heapq module has been converted to C. The resulting tenfold improvement in speed makes the module suitable for handling high volumes of data. In addition, the module has two new functions nlargest() and nsmallest() that use heaps to find the N largest or smallest values in a dataset without the expense of a full sort. (Contributed by Raymond Hettinger.)

The httplib module now contains constants for HTTP status codes defined in various HTTP-related RFC documents. Constants have names such as OK, CREATED, CONTINUE, and MOVED_PERMANENTLY; use pydoc to get a full list. (Contributed by Andrew Eland.)

The imaplib module now supports IMAP’s THREAD command (contributed by Yves Dionne) and new deleteacl() and myrights() methods (contributed by Arnaud Mazin).

The itertools module gained a groupby(iterable[, *func*]) function. iterable is something that can be iterated over to return a stream of elements, and the optional func parameter is a function that takes an element and returns a key value; if omitted, the key is simply the element itself. groupby() then groups the elements into subsequences which have matching values of the key, and returns a series of 2-tuples containing the key value and an iterator over the subsequence.

Here’s an example to make this clearer. The key function simply returns whether a number is even or odd, so the result of groupby() is to return consecutive runs of odd or even numbers.

>>> import itertools
>>> L = [2, 4, 6, 7, 8, 9, 11, 12, 14]
>>> for key_val, it in itertools.groupby(L, lambda x: x % 2):
...    print key_val, list(it)
...
0 [2, 4, 6]
1 [7]
0 [8]
1 [9, 11]
0 [12, 14]
>>>

groupby() is typically used with sorted input. The logic for groupby() is similar to the Unix uniq filter which makes it handy for eliminating, counting, or identifying duplicate elements:

>>> word = 'abracadabra'
>>> letters = sorted(word)   # Turn string into a sorted list of letters
>>> letters
['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r']
>>> for k, g in itertools.groupby(letters):
...    print k, list(g)
...
a ['a', 'a', 'a', 'a', 'a']
b ['b', 'b']
c ['c']
d ['d']
r ['r', 'r']
>>> # List unique letters
>>> [k for k, g in groupby(letters)]
['a', 'b', 'c', 'd', 'r']
>>> # Count letter occurrences
>>> [(k, len(list(g))) for k, g in groupby(letters)]
[('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)]

（由 Hye-Shik Chang 贡献。）

itertools also gained a function named tee(iterator, N) that returns N independent iterators that replicate iterator. If N is omitted, the default is 2.

>>> L = [1,2,3]
>>> i1, i2 = itertools.tee(L)
>>> i1,i2
(<itertools.tee object at 0x402c2080>, <itertools.tee object at 0x402c2090>)
>>> list(i1)               # Run the first iterator to exhaustion
[1, 2, 3]
>>> list(i2)               # Run the second iterator to exhaustion
[1, 2, 3]

Note that tee() has to keep copies of the values returned by the iterator; in the worst case, it may need to keep all of them. This should therefore be used carefully if the leading iterator can run far ahead of the trailing iterator in a long stream of inputs. If the separation is large, then you might as well use list() instead. When the iterators track closely with one another, tee() is ideal. Possible applications include bookmarking, windowing, or lookahead iterators. (Contributed by Raymond Hettinger.)

A number of functions were added to the locale module, such as bind_textdomain_codeset() to specify a particular encoding and a family of l*gettext() functions that return messages in the chosen encoding. (Contributed by Gustavo Niemeyer.)

Some keyword arguments were added to the logging package’s basicConfig() function to simplify log configuration. The default behavior is to log messages to standard error, but various keyword arguments can be specified to log to a particular file, change the logging format, or set the logging level. For example:

import logging
logging.basicConfig(filename='/var/log/application.log',
    level=0,  # Log all messages
    format='%(levelname):%(process):%(thread):%(message)')

Other additions to the logging package include a log(level, msg) convenience method, as well as a TimedRotatingFileHandler class that rotates its log files at a timed interval. The module already had RotatingFileHandler, which rotated logs once the file exceeded a certain size. Both classes derive from a new BaseRotatingHandler class that can be used to implement other rotating handlers.

（更改由 Vinay Sajip 实现。）

The marshal module now shares interned strings on unpacking a data structure. This may shrink the size of certain pickle strings, but the primary effect is to make .pyc files significantly smaller. (Contributed by Martin von Löwis.)

The nntplib module’s NNTP class gained description() and descriptions() methods to retrieve newsgroup descriptions for a single group or for a range of groups. (Contributed by Jürgen A. Erhard.)

Two new functions were added to the operator module, attrgetter(attr) and itemgetter(index). Both functions return callables that take a single argument and return the corresponding attribute or item; these callables make excellent data extractors when used with map() or sorted(). For example:

>>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
>>> map(operator.itemgetter(0), L)
['c', 'd', 'a', 'b']
>>> map(operator.itemgetter(1), L)
[2, 1, 4, 3]
>>> sorted(L, key=operator.itemgetter(1)) # Sort list by second tuple item
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]

（由 Raymond Hettinger 贡献。）

The optparse module was updated in various ways. The module now passes its messages through gettext.gettext(), making it possible to internationalize Optik’s help and error messages. Help messages for options can now include the string '%default', which will be replaced by the option’s default value. (Contributed by Greg Ward.)

The long-term plan is to deprecate the rfc822 module in some future Python release in favor of the email package. To this end, the email.Utils.formatdate() function has been changed to make it usable as a replacement for rfc822.formatdate(). You may want to write new e-mail processing code with this in mind. (Change implemented by Anthony Baxter.)

A new urandom(n) function was added to the os module, returning a string containing n bytes of random data. This function provides access to platform-specific sources of randomness such as /dev/urandom on Linux or the Windows CryptoAPI. (Contributed by Trevor Perrin.)

Another new function: os.path.lexists(path) returns true if the file specified by path exists, whether or not it’s a symbolic link. This differs from the existing os.path.exists(path) function, which returns false if path is a symlink that points to a destination that doesn’t exist. (Contributed by Beni Cherniavsky.)

A new getsid() function was added to the posix module that underlies the os module. (Contributed by J. Raynor.)

The poplib module now supports POP over SSL. (Contributed by Hector Urtubia.)

The profile module can now profile C extension functions. (Contributed by Nick Bastin.)

The random module has a new method called getrandbits(N) that returns a long integer N bits in length. The existing randrange() method now uses getrandbits() where appropriate, making generation of arbitrarily large random numbers more efficient. (Contributed by Raymond Hettinger.)

The regular expression language accepted by the re module was extended with simple conditional expressions, written as (?(group)A|B). group is either a numeric group ID or a group name defined with (?P<group>...) earlier in the expression. If the specified group matched, the regular expression pattern A will be tested against the string; if the group didn’t match, the pattern B will be used instead. (Contributed by Gustavo Niemeyer.)

The re module is also no longer recursive, thanks to a massive amount of work by Gustavo Niemeyer. In a recursive regular expression engine, certain patterns result in a large amount of C stack space being consumed, and it was possible to overflow the stack. For example, if you matched a 30000-byte string of a characters against the expression (a|b)+, one stack frame was consumed per character. Python 2.3 tried to check for stack overflow and raise a RuntimeError exception, but certain patterns could sidestep the checking and if you were unlucky Python could segfault. Python 2.4’s regular expression engine can match this pattern without problems.

The signal module now performs tighter error-checking on the parameters to the signal.signal() function. For example, you can’t set a handler on the SIGKILL signal; previous versions of Python would quietly accept this, but 2.4 will raise a RuntimeError exception.

Two new functions were added to the socket module. socketpair() returns a pair of connected sockets and getservbyport(port) looks up the service name for a given port number. (Contributed by Dave Cole and Barry Warsaw.)

The sys.exitfunc() function has been deprecated. Code should be using the existing atexit module, which correctly handles calling multiple exit functions. Eventually sys.exitfunc() will become a purely internal interface, accessed only by atexit.

The tarfile module now generates GNU-format tar files by default. (Contributed by Lars Gustäbel.)

The threading module now has an elegantly simple way to support thread-local data. The module contains a local class whose attribute values are local to different threads.

import threading

data = threading.local()
data.number = 42
data.url = ('www.python.org', 80)

Other threads can assign and retrieve their own values for the number and url attributes. You can subclass local to initialize attributes or to add methods. (Contributed by Jim Fulton.)

The timeit module now automatically disables periodic garbage collection during the timing loop. This change makes consecutive timings more comparable. (Contributed by Raymond Hettinger.)

The weakref module now supports a wider variety of objects including Python functions, class instances, sets, frozensets, deques, arrays, files, sockets, and regular expression pattern objects. (Contributed by Raymond Hettinger.)

The xmlrpclib module now supports a multi-call extension for transmitting multiple XML-RPC calls in a single HTTP operation. (Contributed by Brian Quinlan.)

mpz, rotor 和 xreadlines 模块已被移除。

cookielib

The cookielib library supports client-side handling for HTTP cookies, mirroring the Cookie module’s server-side cookie support. Cookies are stored in cookie jars; the library transparently stores cookies offered by the web server in the cookie jar, and fetches the cookie from the jar when connecting to the server. As in web browsers, policy objects control whether cookies are accepted or not.

In order to store cookies across sessions, two implementations of cookie jars are provided: one that stores cookies in the Netscape format so applications can use the Mozilla or Lynx cookie files, and one that stores cookies in the same format as the Perl libwww library.

urllib2 has been changed to interact with cookielib: HTTPCookieProcessor manages a cookie jar that is used when accessing URLs.

该模块由 John J. Lee 贡献。

doctest

The doctest module underwent considerable refactoring thanks to Edward Loper and Tim Peters. Testing can still be as simple as running doctest.testmod(), but the refactorings allow customizing the module’s operation in various ways

The new DocTestFinder class extracts the tests from a given object’s docstrings:

def f (x, y):
    """>>> f(2,2)
4
>>> f(3,2)
6
    """
    return x*y

finder = doctest.DocTestFinder()

# Get list of DocTest instances
tests = finder.find(f)

The new DocTestRunner class then runs individual tests and can produce a summary of the results:

runner = doctest.DocTestRunner()
for t in tests:
    tried, failed = runner.run(t)

runner.summarize(verbose=1)

The above example produces the following output:

1 items passed all tests:
   2 tests in f
2 tests in 1 items.
2 passed and 0 failed.
Test passed.

DocTestRunner uses an instance of the OutputChecker class to compare the expected output with the actual output. This class takes a number of different flags that customize its behaviour; ambitious users can also write a completely new subclass of OutputChecker.

The default output checker provides a number of handy features. For example, with the doctest.ELLIPSIS option flag, an ellipsis (...) in the expected output matches any substring, making it easier to accommodate outputs that vary in minor ways:

def o (n):
    """>>> o(1)
<__main__.C instance at 0x...>
>>>
"""

Another special string, <BLANKLINE>, matches a blank line:

def p (n):
    """>>> p(1)
<BLANKLINE>
>>>
"""

Another new capability is producing a diff-style display of the output by specifying the doctest.REPORT_UDIFF (unified diffs), doctest.REPORT_CDIFF (context diffs), or doctest.REPORT_NDIFF (delta-style) option flags. For example:

def g (n):
    """>>> g(4)
here
is
a
lengthy
>>>"""
    L = 'here is a rather lengthy list of words'.split()
    for word in L[:n]:
        print word

Running the above function’s tests with doctest.REPORT_UDIFF specified, you get the following output:

**********************************************************************
File "t.py", line 15, in g
Failed example:
    g(4)
Differences (unified diff with -expected +actual):
    @@ -2,3 +2,3 @@
     is
     a
    -lengthy
    +rather
**********************************************************************

构建和 C API 的改变

Some of the changes to Python’s build process and to the C API are:

Three new convenience macros were added for common return values from extension functions: Py_RETURN_NONE, Py_RETURN_TRUE, and Py_RETURN_FALSE. (Contributed by Brett Cannon.)

Another new macro, Py_CLEAR(obj), decreases the reference count of obj and sets obj to the null pointer. (Contributed by Jim Fulton.)

A new function, PyTuple_Pack(N, obj1, obj2, ..., objN), constructs tuples from a variable length argument list of Python objects. (Contributed by Raymond Hettinger.)

A new function, PyDict_Contains(d, k), implements fast dictionary lookups without masking exceptions raised during the look-up process. (Contributed by Raymond Hettinger.)

The Py_IS_NAN(X) macro returns 1 if its float or double argument X is a NaN. (Contributed by Tim Peters.)

C code can avoid unnecessary locking by using the new PyEval_ThreadsInitialized() function to tell if any thread operations have been performed. If this function returns false, no lock operations are needed. (Contributed by Nick Coghlan.)

A new function, PyArg_VaParseTupleAndKeywords(), is the same as PyArg_ParseTupleAndKeywords() but takes a va_list instead of a number of arguments. (Contributed by Greg Chapman.)

A new method flag, METH_COEXISTS, allows a function defined in slots to co-exist with a PyCFunction having the same name. This can halve the access time for a method such as set.__contains__(). (Contributed by Raymond Hettinger.)

Python can now be built with additional profiling for the interpreter itself, intended as an aid to people developing the Python core. Providing --enable-profiling to the configure script will let you profile the interpreter with gprof, and providing the --with-tsc switch enables profiling using the Pentium’s Time-Stamp-Counter register. Note that the --with-tsc switch is slightly misnamed, because the profiling feature also works on the PowerPC platform, though that processor architecture doesn’t call that register “the TSC register”. (Contributed by Jeremy Hylton.)

The tracebackobject type has been renamed to PyTracebackObject.

Port-Specific Changes

The Windows port now builds under MSVC++ 7.1 as well as version 6. (Contributed by Martin von Löwis.)

移植到 Python 2.4

This section lists previously described changes that may require changes to your code:

Left shifts and hexadecimal/octal constants that are too large no longer trigger a FutureWarning and return a value limited to 32 or 64 bits; instead they return a long integer.

Integer operations will no longer trigger an OverflowWarning. The OverflowWarning warning will disappear in Python 2.5.

The zip() built-in function and itertools.izip() now return an empty list instead of raising a TypeError exception if called with no arguments.

You can no longer compare the date and datetime instances provided by the datetime module. Two instances of different classes will now always be unequal, and relative comparisons (<, >) will raise a TypeError.

dircache.listdir() now passes exceptions to the caller instead of returning empty lists.

LexicalHandler.startDTD() used to receive the public and system IDs in the wrong order. This has been corrected; applications relying on the wrong order need to be fixed.

fcntl.ioctl() now warns if the mutate argument is omitted and relevant.

The tarfile module now generates GNU-format tar files by default.

Encountering a failure while importing a module no longer leaves a partially-initialized module object in sys.modules.

None is now a constant; code that binds a new value to the name None is now a syntax error.

The signals.signal() function now raises a RuntimeError exception for certain illegal values; previously these errors would pass silently. For example, you can no longer set a handler on the SIGKILL signal.

致谢

作者感谢以下人员对本文各种草稿给予的建议，更正和协助：Koray Can, Hye-Shik Chang, Michael Dyck, Raymond Hettinger, Brian Hurt, Hamish Lawson, Fredrik Lundh, Sean Reifschneider, Sadruddin Rejeb.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

Python 2.3 有什么新变化

作者

A.M. Kuchling

本文介绍了 Python 2.3 的新特性。 Python 2.3 发布于 2003 年 7 月 29 日。

The main themes for Python 2.3 are polishing some of the features added in 2.2, adding various small but useful enhancements to the core language, and expanding the standard library. The new object model introduced in the previous version has benefited from 18 months of bugfixes and from optimization efforts that have improved the performance of new-style classes. A few new built-in functions have been added such as sum() and enumerate(). The in operator can now be used for substring searches (e.g. "ab" in "abc" returns True).

Some of the many new library features include Boolean, set, heap, and date/time data types, the ability to import modules from ZIP-format archives, metadata support for the long-awaited Python catalog, an updated version of IDLE, and modules for logging messages, wrapping text, parsing CSV files, processing command-line options, using BerkeleyDB databases… the list of new and enhanced modules is lengthy.

This article doesn’t attempt to provide a complete specification of the new features, but instead provides a convenient overview. For full details, you should refer to the documentation for Python 2.3, such as the Python Library Reference and the Python Reference Manual. If you want to understand the complete implementation and design rationale, refer to the PEP for a particular new feature.

PEP 218: A Standard Set Datatype

The new sets module contains an implementation of a set datatype. The Set class is for mutable sets, sets that can have members added and removed. The ImmutableSet class is for sets that can’t be modified, and instances of ImmutableSet can therefore be used as dictionary keys. Sets are built on top of dictionaries, so the elements within a set must be hashable.

下面是一个简单的例子:

>>> import sets
>>> S = sets.Set([1,2,3])
>>> S
Set([1, 2, 3])
>>> 1 in S
True
>>> 0 in S
False
>>> S.add(5)
>>> S.remove(3)
>>> S
Set([1, 2, 5])
>>>

The union and intersection of sets can be computed with the union() and intersection() methods; an alternative notation uses the bitwise operators & and |. Mutable sets also have in-place versions of these methods, union_update() and intersection_update().

>>> S1 = sets.Set([1,2,3])
>>> S2 = sets.Set([4,5,6])
>>> S1.union(S2)
Set([1, 2, 3, 4, 5, 6])
>>> S1 | S2                  # Alternative notation
Set([1, 2, 3, 4, 5, 6])
>>> S1.intersection(S2)
Set([])
>>> S1 & S2                  # Alternative notation
Set([])
>>> S1.union_update(S2)
>>> S1
Set([1, 2, 3, 4, 5, 6])
>>>

It’s also possible to take the symmetric difference of two sets. This is the set of all elements in the union that aren’t in the intersection. Another way of putting it is that the symmetric difference contains all elements that are in exactly one set. Again, there’s an alternative notation (^), and an in-place version with the ungainly name symmetric_difference_update().

>>> S1 = sets.Set([1,2,3,4])
>>> S2 = sets.Set([3,4,5,6])
>>> S1.symmetric_difference(S2)
Set([1, 2, 5, 6])
>>> S1 ^ S2
Set([1, 2, 5, 6])
>>>

There are also issubset() and issuperset() methods for checking whether one set is a subset or superset of another:

>>> S1 = sets.Set([1,2,3])
>>> S2 = sets.Set([2,3])
>>> S2.issubset(S1)
True
>>> S1.issubset(S2)
False
>>> S1.issuperset(S2)
True
>>>

参见

PEP 218 [https://www.python.org/dev/peps/pep-0218] - 添加内置Set对象类型

PEP 由 Greg V. Wilson 撰写 ; 由 Greg V. Wilson, Alex Martelli 和 GvR 实现。

PEP 255: Simple Generators

In Python 2.2, generators were added as an optional feature, to be enabled by a from __future__ import generators directive. In 2.3 generators no longer need to be specially enabled, and are now always present; this means that yield is now always a keyword. The rest of this section is a copy of the description of generators from the “What’s New in Python 2.2” document; if you read it back when Python 2.2 came out, you can skip the rest of this section.

You’re doubtless familiar with how function calls work in Python or C. When you call a function, it gets a private namespace where its local variables are created. When the function reaches a return statement, the local variables are destroyed and the resulting value is returned to the caller. A later call to the same function will get a fresh new set of local variables. But, what if the local variables weren’t thrown away on exiting a function? What if you could later resume the function where it left off? This is what generators provide; they can be thought of as resumable functions.

Here’s the simplest example of a generator function:

def generate_ints(N):
    for i in range(N):
        yield i

A new keyword, yield, was introduced for generators. Any function containing a yield statement is a generator function; this is detected by Python’s bytecode compiler which compiles the function specially as a result.

When you call a generator function, it doesn’t return a single value; instead it returns a generator object that supports the iterator protocol. On executing the yield statement, the generator outputs the value of i, similar to a return statement. The big difference between yield and a return statement is that on reaching a yield the generator’s state of execution is suspended and local variables are preserved. On the next call to the generator’s .next() method, the function will resume executing immediately after the yield statement. (For complicated reasons, the yield statement isn’t allowed inside the try block of a try…finally statement; read PEP 255 [https://www.python.org/dev/peps/pep-0255] for a full explanation of the interaction between yield and exceptions.)

Here’s a sample usage of the generate_ints() generator:

>>> gen = generate_ints(3)
>>> gen
<generator object at 0x8117f90>
>>> gen.next()
0
>>> gen.next()
1
>>> gen.next()
2
>>> gen.next()
Traceback (most recent call last):
  File "stdin", line 1, in ?
  File "stdin", line 2, in generate_ints
StopIteration

You could equally write for i in generate_ints(5), or a,b,c = generate_ints(3).

Inside a generator function, the return statement can only be used without a value, and signals the end of the procession of values; afterwards the generator cannot return any further values. return with a value, such as return 5, is a syntax error inside a generator function. The end of the generator’s results can also be indicated by raising StopIteration manually, or by just letting the flow of execution fall off the bottom of the function.

You could achieve the effect of generators manually by writing your own class and storing all the local variables of the generator as instance variables. For example, returning a list of integers could be done by setting self.count to 0, and having the next() method increment self.count and return it. However, for a moderately complicated generator, writing a corresponding class would be much messier. Lib/test/test_generators.py contains a number of more interesting examples. The simplest one implements an in-order traversal of a tree using generators recursively.

# A recursive generator that generates Tree leaves in in-order.
def inorder(t):
    if t:
        for x in inorder(t.left):
            yield x
        yield t.label
        for x in inorder(t.right):
            yield x

Two other examples in Lib/test/test_generators.py produce solutions for the N-Queens problem (placing $N$ queens on an $NxN$ chess board so that no queen threatens another) and the Knight’s Tour (a route that takes a knight to every square of an $NxN$ chessboard without visiting any square twice).

The idea of generators comes from other programming languages, especially Icon (https://www.cs.arizona.edu/icon/), where the idea of generators is central. In Icon, every expression and function call behaves like a generator. One example from “An Overview of the Icon Programming Language” at https://www.cs.arizona.edu/icon/docs/ipd266.htm gives an idea of what this looks like:

sentence := "Store it in the neighboring harbor"
if (i := find("or", sentence)) > 5 then write(i)

In Icon the find() function returns the indexes at which the substring “or” is found: 3, 23, 33. In the if statement, i is first assigned a value of 3, but 3 is less than 5, so the comparison fails, and Icon retries it with the second value of 23. 23 is greater than 5, so the comparison now succeeds, and the code prints the value 23 to the screen.

Python doesn’t go nearly as far as Icon in adopting generators as a central concept. Generators are considered part of the core Python language, but learning or using them isn’t compulsory; if they don’t solve any problems that you have, feel free to ignore them. One novel feature of Python’s interface as compared to Icon’s is that a generator’s state is represented as a concrete object (the iterator) that can be passed around to other functions or stored in a data structure.

参见

PEP 255 [https://www.python.org/dev/peps/pep-0255] - 简单生成器

Written by Neil Schemenauer, Tim Peters, Magnus Lie Hetland. Implemented mostly by Neil Schemenauer and Tim Peters, with other fixes from the Python Labs crew.

PEP 263: Source Code Encodings

Python source files can now be declared as being in different character set encodings. Encodings are declared by including a specially formatted comment in the first or second line of the source file. For example, a UTF-8 file can be declared with:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

Without such an encoding declaration, the default encoding used is 7-bit ASCII. Executing or importing modules that contain string literals with 8-bit characters and have no encoding declaration will result in a DeprecationWarning being signalled by Python 2.3; in 2.4 this will be a syntax error.

The encoding declaration only affects Unicode string literals, which will be converted to Unicode using the specified encoding. Note that Python identifiers are still restricted to ASCII characters, so you can’t have variable names that use characters outside of the usual alphanumerics.

参见

PEP 263 [https://www.python.org/dev/peps/pep-0263] - Defining Python Source Code Encodings

由 Marc-André Lemburg 和 Martin von Löwis 撰写 ; 由 Suzuki Hisao 和 Martin von Löwis 实现。

PEP 273: 从ZIP压缩包导入模块

The new zipimport module adds support for importing modules from a ZIP-format archive. You don’t need to import the module explicitly; it will be automatically imported if a ZIP archive’s filename is added to sys.path. For example:

amk@nyman:~/src/python$ unzip -l /tmp/example.zip
Archive:  /tmp/example.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
     8467  11-26-02 22:30   jwzthreading.py
 --------                   -------
     8467                   1 file
amk@nyman:~/src/python$ ./python
Python 2.3 (#1, Aug 1 2003, 19:54:32)
>>> import sys
>>> sys.path.insert(0, '/tmp/example.zip')  # Add .zip file to front of path
>>> import jwzthreading
>>> jwzthreading.__file__
'/tmp/example.zip/jwzthreading.py'
>>>

An entry in sys.path can now be the filename of a ZIP archive. The ZIP archive can contain any kind of files, but only files named *.py, *.pyc, or *.pyo can be imported. If an archive only contains *.py files, Python will not attempt to modify the archive by adding the corresponding *.pyc file, meaning that if a ZIP archive doesn’t contain *.pyc files, importing may be rather slow.

A path within the archive can also be specified to only import from a subdirectory; for example, the path /tmp/example.zip/lib/ would only import from the lib/ subdirectory within the archive.

参见

PEP 273 [https://www.python.org/dev/peps/pep-0273] -  从ZIP压缩包导入模块

Written by James C. Ahlstrom, who also provided an implementation. Python 2.3 follows the specification in PEP 273 [https://www.python.org/dev/peps/pep-0273], but uses an implementation written by Just van Rossum that uses the import hooks described in PEP 302 [https://www.python.org/dev/peps/pep-0302]. See section PEP 302: New Import Hooks for a description of the new import hooks.

PEP 277: Unicode file name support for Windows NT

On Windows NT, 2000, and XP, the system stores file names as Unicode strings. Traditionally, Python has represented file names as byte strings, which is inadequate because it renders some file names inaccessible.

Python now allows using arbitrary Unicode strings (within the limitations of the file system) for all functions that expect file names, most notably the open() built-in function. If a Unicode string is passed to os.listdir(), Python now returns a list of Unicode strings. A new function, os.getcwdu(), returns the current directory as a Unicode string.

Byte strings still work as file names, and on Windows Python will transparently convert them to Unicode using the mbcs encoding.

Other systems also allow Unicode strings as file names but convert them to byte strings before passing them to the system, which can cause a UnicodeError to be raised. Applications can test whether arbitrary Unicode strings are supported as file names by checking os.path.supports_unicode_filenames, a Boolean value.

Under MacOS, os.listdir() may now return Unicode filenames.

参见

PEP 277 [https://www.python.org/dev/peps/pep-0277] - Unicode file name support for Windows NT

由 Neil Hodgson 撰写 ; 由 Neil Hodgson, Martin von Löwis 和 Mark Hammond 实现。

PEP 278: 通用换行支持

The three major operating systems used today are Microsoft Windows, Apple’s Macintosh OS, and the various Unix derivatives. A minor irritation of cross-platform work is that these three platforms all use different characters to mark the ends of lines in text files. Unix uses the linefeed (ASCII character 10), MacOS uses the carriage return (ASCII character 13), and Windows uses a two-character sequence of a carriage return plus a newline.

Python’s file objects can now support end of line conventions other than the one followed by the platform on which Python is running. Opening a file with the mode 'U' or 'rU' will open a file for reading in universal newlines mode. All three line ending conventions will be translated to a '\n' in the strings returned by the various file methods such as read() and readline().

Universal newline support is also used when importing modules and when executing a file with the execfile() function. This means that Python modules can be shared between all three operating systems without needing to convert the line-endings.

This feature can be disabled when compiling Python by specifying the --without-universal-newlines switch when running Python’s configure script.

参见

PEP 278 [https://www.python.org/dev/peps/pep-0278] - 通用换行支持

由 Jack Jansen 撰写并实现。

PEP 279: enumerate()

A new built-in function, enumerate(), will make certain loops a bit clearer. enumerate(thing), where thing is either an iterator or a sequence, returns an iterator that will return (0, thing[0]), (1, thing[1]), (2, thing[2]), and so forth.

A common idiom to change every element of a list looks like this:

for i in range(len(L)):
    item = L[i]
    # ... compute some result based on item ...
    L[i] = result

可以使用 enumerate() 重写为：

for i, item in enumerate(L):
    # ... compute some result based on item ...
    L[i] = result

参见

PEP 279 [https://www.python.org/dev/peps/pep-0279] - 内置函数 enumerate()

由 Raymond Hettinger 撰写并实现。

PEP 282: logging 包

A standard package for writing logs, logging, has been added to Python 2.3. It provides a powerful and flexible mechanism for generating logging output which can then be filtered and processed in various ways. A configuration file written in a standard format can be used to control the logging behavior of a program. Python includes handlers that will write log records to standard error or to a file or socket, send them to the system log, or even e-mail them to a particular address; of course, it’s also possible to write your own handler classes.

The Logger class is the primary class. Most application code will deal with one or more Logger objects, each one used by a particular subsystem of the application. Each Logger is identified by a name, and names are organized into a hierarchy using . as the component separator. For example, you might have Logger instances named server, server.auth and server.network. The latter two instances are below server in the hierarchy. This means that if you turn up the verbosity for server or direct server messages to a different handler, the changes will also apply to records logged to server.auth and server.network. There’s also a root Logger that’s the parent of all other loggers.

For simple uses, the logging package contains some convenience functions that always use the root log:

import logging

logging.debug('Debugging information')
logging.info('Informational message')
logging.warning('Warning:config file %s not found', 'server.conf')
logging.error('Error occurred')
logging.critical('Critical error -- shutting down')

This produces the following output:

WARNING:root:Warning:config file server.conf not found
ERROR:root:Error occurred
CRITICAL:root:Critical error -- shutting down

In the default configuration, informational and debugging messages are suppressed and the output is sent to standard error. You can enable the display of informational and debugging messages by calling the setLevel() method on the root logger.

Notice the warning() call’s use of string formatting operators; all of the functions for logging messages take the arguments (msg, arg1, arg2, ...) and log the string resulting from msg % (arg1, arg2, ...).

There’s also an exception() function that records the most recent traceback. Any of the other functions will also record the traceback if you specify a true value for the keyword argument exc_info.

def f():
    try:    1/0
    except: logging.exception('Problem recorded')

f()

This produces the following output:

ERROR:root:Problem recorded
Traceback (most recent call last):
  File "t.py", line 6, in f
    1/0
ZeroDivisionError: integer division or modulo by zero

Slightly more advanced programs will use a logger other than the root logger. The getLogger(name) function is used to get a particular log, creating it if it doesn’t exist yet. getLogger(None) returns the root logger.

log = logging.getLogger('server')
 ...
log.info('Listening on port %i', port)
 ...
log.critical('Disk full')
 ...

Log records are usually propagated up the hierarchy, so a message logged to server.auth is also seen by server and root, but a Logger can prevent this by setting its propagate attribute to False.

There are more classes provided by the logging package that can be customized. When a Logger instance is told to log a message, it creates a LogRecord instance that is sent to any number of different Handler instances. Loggers and handlers can also have an attached list of filters, and each filter can cause the LogRecord to be ignored or can modify the record before passing it along. When they’re finally output, LogRecord instances are converted to text by a Formatter class. All of these classes can be replaced by your own specially-written classes.

With all of these features the logging package should provide enough flexibility for even the most complicated applications. This is only an incomplete overview of its features, so please see the package’s reference documentation for all of the details. Reading PEP 282 [https://www.python.org/dev/peps/pep-0282] will also be helpful.

参见

PEP 282 [https://www.python.org/dev/peps/pep-0282] - Logging 系统

由 Vinay Sajip 和 Trent Mick 撰写 ; 由 Vinay Sajip 实现。

PEP 285: 布尔类型

A Boolean type was added to Python 2.3. Two new constants were added to the __builtin__ module, True and False. (True and False constants were added to the built-ins in Python 2.2.1, but the 2.2.1 versions are simply set to integer values of 1 and 0 and aren’t a different type.)

The type object for this new type is named bool; the constructor for it takes any Python value and converts it to True or False.

>>> bool(1)
True
>>> bool(0)
False
>>> bool([])
False
>>> bool( (1,) )
True

Most of the standard library modules and built-in functions have been changed to return Booleans.

>>> obj = []
>>> hasattr(obj, 'append')
True
>>> isinstance(obj, list)
True
>>> isinstance(obj, tuple)
False

Python’s Booleans were added with the primary goal of making code clearer. For example, if you’re reading a function and encounter the statement return 1, you might wonder whether the 1 represents a Boolean truth value, an index, or a coefficient that multiplies some other quantity. If the statement is return True, however, the meaning of the return value is quite clear.

Python’s Booleans were not added for the sake of strict type-checking. A very strict language such as Pascal would also prevent you performing arithmetic with Booleans, and would require that the expression in an if statement always evaluate to a Boolean result. Python is not this strict and never will be, as PEP 285 [https://www.python.org/dev/peps/pep-0285] explicitly says. This means you can still use any expression in an if statement, even ones that evaluate to a list or tuple or some random object. The Boolean type is a subclass of the int class so that arithmetic using a Boolean still works.

>>> True + 1
2
>>> False + 1
1
>>> False * 75
0
>>> True * 75
75

To sum up True and False in a sentence: they’re alternative ways to spell the integer values 1 and 0, with the single difference that str() and repr() return the strings 'True' and 'False' instead of '1' and '0'.

参见

PEP 285 [https://www.python.org/dev/peps/pep-0285] - 添加布尔类型

由 GvR 撰写并实现。

PEP 293: Codec Error Handling Callbacks

When encoding a Unicode string into a byte string, unencodable characters may be encountered. So far, Python has allowed specifying the error processing as either “strict” (raising UnicodeError), “ignore” (skipping the character), or “replace” (using a question mark in the output string), with “strict” being the default behavior. It may be desirable to specify alternative processing of such errors, such as inserting an XML character reference or HTML entity reference into the converted string.

Python now has a flexible framework to add different processing strategies. New error handlers can be added with codecs.register_error(), and codecs then can access the error handler with codecs.lookup_error(). An equivalent C API has been added for codecs written in C. The error handler gets the necessary state information such as the string being converted, the position in the string where the error was detected, and the target encoding. The handler can then either raise an exception or return a replacement string.

Two additional error handlers have been implemented using this framework: “backslashreplace” uses Python backslash quoting to represent unencodable characters and “xmlcharrefreplace” emits XML character references.

参见

PEP 293 [https://www.python.org/dev/peps/pep-0293] - Codec Error Handling Callbacks

由 Walter Dörwald 撰写并实现。

PEP 301: Distutils的软件包索引和元数据

Support for the long-requested Python catalog makes its first appearance in 2.3.

The heart of the catalog is the new Distutils register command. Running python setup.py register will collect the metadata describing a package, such as its name, version, maintainer, description, &c., and send it to a central catalog server. The resulting catalog is available from https://pypi.org.

To make the catalog a bit more useful, a new optional classifiers keyword argument has been added to the Distutils setup() function. A list of Trove [http://catb.org/~esr/trove/]-style strings can be supplied to help classify the software.

Here’s an example setup.py with classifiers, written to be compatible with older versions of the Distutils:

from distutils import core
kw = {'name': "Quixote",
      'version': "0.5.1",
      'description': "A highly Pythonic Web application framework",
      # ...
      }

if (hasattr(core, 'setup_keywords') and
    'classifiers' in core.setup_keywords):
    kw['classifiers'] = \
        ['Topic :: Internet :: WWW/HTTP :: Dynamic Content',
         'Environment :: No Input/Output (Daemon)',
         'Intended Audience :: Developers'],

core.setup(**kw)

The full list of classifiers can be obtained by running python setup.py register --list-classifiers.

参见

PEP 301 [https://www.python.org/dev/peps/pep-0301] - Distutils 的软件包索引和元数据

由 Richard Jones 撰写并实现。

PEP 302: New Import Hooks

While it’s been possible to write custom import hooks ever since the ihooks module was introduced in Python 1.3, no one has ever been really happy with it because writing new import hooks is difficult and messy. There have been various proposed alternatives such as the imputil and iu modules, but none of them has ever gained much acceptance, and none of them were easily usable from C code.

PEP 302 [https://www.python.org/dev/peps/pep-0302] borrows ideas from its predecessors, especially from Gordon McMillan’s iu module. Three new items are added to the sys module:

sys.path_hooks is a list of callable objects; most often they’ll be classes. Each callable takes a string containing a path and either returns an importer object that will handle imports from this path or raises an ImportError exception if it can’t handle this path.

sys.path_importer_cache caches importer objects for each path, so sys.path_hooks will only need to be traversed once for each path.

sys.meta_path is a list of importer objects that will be traversed before sys.path is checked. This list is initially empty, but user code can add objects to it. Additional built-in and frozen modules can be imported by an object added to this list.

Importer objects must have a single method, find_module(fullname, path=None). fullname will be a module or package name, e.g. string or distutils.core. find_module() must return a loader object that has a single method, load_module(fullname), that creates and returns the corresponding module object.

Pseudo-code for Python’s new import logic, therefore, looks something like this (simplified a bit; see PEP 302 [https://www.python.org/dev/peps/pep-0302] for the full details):

for mp in sys.meta_path:
    loader = mp(fullname)
    if loader is not None:
        <module> = loader.load_module(fullname)

for path in sys.path:
    for hook in sys.path_hooks:
        try:
            importer = hook(path)
        except ImportError:
            # ImportError, so try the other path hooks
            pass
        else:
            loader = importer.find_module(fullname)
            <module> = loader.load_module(fullname)

# Not found!
raise ImportError

参见

PEP 302 [https://www.python.org/dev/peps/pep-0302] - 新导入钩

由 Just van Rossum 和 Paul Moore 撰写 ; 由 Just van Rossum 实现。

PEP 305: 逗号分隔文件

Comma-separated files are a format frequently used for exporting data from databases and spreadsheets. Python 2.3 adds a parser for comma-separated files.

Comma-separated format is deceptively simple at first glance:

Costs,150,200,3.95

Read a line and call line.split(','): what could be simpler? But toss in string data that can contain commas, and things get more complicated:

"Costs",150,200,3.95,"Includes taxes, shipping, and sundry items"

A big ugly regular expression can parse this, but using the new csv package is much simpler:

import csv

input = open('datafile', 'rb')
reader = csv.reader(input)
for line in reader:
    print line

The reader() function takes a number of different options. The field separator isn’t limited to the comma and can be changed to any character, and so can the quoting and line-ending characters.

Different dialects of comma-separated files can be defined and registered; currently there are two dialects, both used by Microsoft Excel. A separate csv.writer class will generate comma-separated files from a succession of tuples or lists, quoting strings that contain the delimiter.

参见

该实现在“Python 增强提议” - PEP 305 (CSV 文件 API) 中被提出

由 Kevin Altis, Dave Cole, Andrew McNamara, Skip Montanaro, Cliff Wells 撰写并实现。

PEP 307: Pickle Enhancements

The pickle and cPickle modules received some attention during the 2.3 development cycle. In 2.2, new-style classes could be pickled without difficulty, but they weren’t pickled very compactly; PEP 307 [https://www.python.org/dev/peps/pep-0307] quotes a trivial example where a new-style class results in a pickled string three times longer than that for a classic class.

The solution was to invent a new pickle protocol. The pickle.dumps() function has supported a text-or-binary flag for a long time. In 2.3, this flag is redefined from a Boolean to an integer: 0 is the old text-mode pickle format, 1 is the old binary format, and now 2 is a new 2.3-specific format. A new constant, pickle.HIGHEST_PROTOCOL, can be used to select the fanciest protocol available.

Unpickling is no longer considered a safe operation. 2.2’s pickle provided hooks for trying to prevent unsafe classes from being unpickled (specifically, a __safe_for_unpickling__ attribute), but none of this code was ever audited and therefore it’s all been ripped out in 2.3. You should not unpickle untrusted data in any version of Python.

To reduce the pickling overhead for new-style classes, a new interface for customizing pickling was added using three special methods: __getstate__(), __setstate__(), and __getnewargs__(). Consult PEP 307 [https://www.python.org/dev/peps/pep-0307] for the full semantics of these methods.

As a way to compress pickles yet further, it’s now possible to use integer codes instead of long strings to identify pickled classes. The Python Software Foundation will maintain a list of standardized codes; there’s also a range of codes for private use. Currently no codes have been specified.

参见

PEP 307 [https://www.python.org/dev/peps/pep-0307] - Extensions to the pickle protocol

PEP 由 Guido van Rossum 和 Tim Peters 撰写和实现。

扩展切片

Ever since Python 1.4, the slicing syntax has supported an optional third “step” or “stride” argument. For example, these are all legal Python syntax: L[1:10:2], L[:-1:1], L[::-1]. This was added to Python at the request of the developers of Numerical Python, which uses the third argument extensively. However, Python’s built-in list, tuple, and string sequence types have never supported this feature, raising a TypeError if you tried it. Michael Hudson contributed a patch to fix this shortcoming.

For example, you can now easily extract the elements of a list that have even indexes:

>>> L = range(10)
>>> L[::2]
[0, 2, 4, 6, 8]

Negative values also work to make a copy of the same list in reverse order:

>>> L[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

This also works for tuples, arrays, and strings:

>>> s='abcd'
>>> s[::2]
'ac'
>>> s[::-1]
'dcba'

If you have a mutable sequence such as a list or an array you can assign to or delete an extended slice, but there are some differences between assignment to extended and regular slices. Assignment to a regular slice can be used to change the length of the sequence:

>>> a = range(3)
>>> a
[0, 1, 2]
>>> a[1:3] = [4, 5, 6]
>>> a
[0, 4, 5, 6]

Extended slices aren’t this flexible. When assigning to an extended slice, the list on the right hand side of the statement must contain the same number of items as the slice it is replacing:

>>> a = range(4)
>>> a
[0, 1, 2, 3]
>>> a[::2]
[0, 2]
>>> a[::2] = [0, -1]
>>> a
[0, 1, -1, 3]
>>> a[::2] = [0,1,2]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: attempt to assign sequence of size 3 to extended slice of size 2

Deletion is more straightforward:

>>> a = range(4)
>>> a
[0, 1, 2, 3]
>>> a[::2]
[0, 2]
>>> del a[::2]
>>> a
[1, 3]

One can also now pass slice objects to the __getitem__() methods of the built-in sequences:

>>> range(10).__getitem__(slice(0, 5, 2))
[0, 2, 4]

Or use slice objects directly in subscripts:

>>> range(10)[slice(0, 5, 2)]
[0, 2, 4]

To simplify implementing sequences that support extended slicing, slice objects now have a method indices(length) which, given the length of a sequence, returns a (start, stop, step) tuple that can be passed directly to range(). indices() handles omitted and out-of-bounds indices in a manner consistent with regular slices (and this innocuous phrase hides a welter of confusing details!). The method is intended to be used like this:

class FakeSeq:
    ...
    def calc_item(self, i):
        ...
    def __getitem__(self, item):
        if isinstance(item, slice):
            indices = item.indices(len(self))
            return FakeSeq([self.calc_item(i) for i in range(*indices)])
        else:
            return self.calc_item(i)

From this example you can also see that the built-in slice object is now the type object for the slice type, and is no longer a function. This is consistent with Python 2.2, where int, str, etc., underwent the same change.

其他语言特性修改

Here are all of the changes that Python 2.3 makes to the core Python language.

The yield statement is now always a keyword, as described in section PEP 255: Simple Generators of this document.

A new built-in function enumerate() was added, as described in section PEP 279: enumerate() of this document.

Two new constants, True and False were added along with the built-in bool type, as described in section PEP 285: 布尔类型 of this document.

The int() type constructor will now return a long integer instead of raising an OverflowError when a string or floating-point number is too large to fit into an integer. This can lead to the paradoxical result that isinstance(int(expression), int) is false, but that seems unlikely to cause problems in practice.

Built-in types now support the extended slicing syntax, as described in section 扩展切片 of this document.

A new built-in function, sum(iterable, start=0), adds up the numeric items in the iterable object and returns their sum. sum() only accepts numbers, meaning that you can’t use it to concatenate a bunch of strings. (Contributed by Alex Martelli.)

list.insert(pos, value) used to insert value at the front of the list when pos was negative. The behaviour has now been changed to be consistent with slice indexing, so when pos is -1 the value will be inserted before the last element, and so forth.

list.index(value), which searches for value within the list and returns its index, now takes optional start and stop arguments to limit the search to only part of the list.

Dictionaries have a new method, pop(key[, *default*]), that returns the value corresponding to key and removes that key/value pair from the dictionary. If the requested key isn’t present in the dictionary, default is returned if it’s specified and KeyError raised if it isn’t.

>>> d = {1:2}
>>> d
{1: 2}
>>> d.pop(4)
Traceback (most recent call last):
  File "stdin", line 1, in ?
KeyError: 4
>>> d.pop(1)
2
>>> d.pop(1)
Traceback (most recent call last):
  File "stdin", line 1, in ?
KeyError: 'pop(): dictionary is empty'
>>> d
{}
>>>

There’s also a new class method, dict.fromkeys(iterable, value), that creates a dictionary with keys taken from the supplied iterator iterable and all values set to value, defaulting to None.

(Patches contributed by Raymond Hettinger.)

Also, the dict() constructor now accepts keyword arguments to simplify creating small dictionaries:

>>> dict(red=1, blue=2, green=3, black=4)
{'blue': 2, 'black': 4, 'green': 3, 'red': 1}

（由 Just van Rossum 贡献。）

The assert statement no longer checks the __debug__ flag, so you can no longer disable assertions by assigning to __debug__. Running Python with the -O switch will still generate code that doesn’t execute any assertions.

Most type objects are now callable, so you can use them to create new objects such as functions, classes, and modules. (This means that the new module can be deprecated in a future Python version, because you can now use the type objects available in the types module.) For example, you can create a new module object with the following code:

>>> import types
>>> m = types.ModuleType('abc','docstring')
>>> m
<module 'abc' (built-in)>
>>> m.__doc__
'docstring'

A new warning, PendingDeprecationWarning was added to indicate features which are in the process of being deprecated. The warning will not be printed by default. To check for use of features that will be deprecated in the future, supply -Walways::PendingDeprecationWarning:: on the command line or use warnings.filterwarnings().

The process of deprecating string-based exceptions, as in raise "Error occurred", has begun. Raising a string will now trigger PendingDeprecationWarning.

Using None as a variable name will now result in a SyntaxWarning warning. In a future version of Python, None may finally become a keyword.

The xreadlines() method of file objects, introduced in Python 2.1, is no longer necessary because files now behave as their own iterator. xreadlines() was originally introduced as a faster way to loop over all the lines in a file, but now you can simply write for line in file_obj. File objects also have a new read-only encoding attribute that gives the encoding used by the file; Unicode strings written to the file will be automatically converted to bytes using the given encoding.

The method resolution order used by new-style classes has changed, though you’ll only notice the difference if you have a really complicated inheritance hierarchy. Classic classes are unaffected by this change. Python 2.2 originally used a topological sort of a class’s ancestors, but 2.3 now uses the C3 algorithm as described in the paper “A Monotonic Superclass Linearization for Dylan” [http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.19.3910]. To understand the motivation for this change, read Michele Simionato’s article “Python 2.3 Method Resolution Order” [http://www.phyast.pitt.edu/~micheles/mro.html], or read the thread on python-dev starting with the message at https://mail.python.org/pipermail/python-dev/2002-October/029035.html. Samuele Pedroni first pointed out the problem and also implemented the fix by coding the C3 algorithm.

Python runs multithreaded programs by switching between threads after executing N bytecodes. The default value for N has been increased from 10 to 100 bytecodes, speeding up single-threaded applications by reducing the switching overhead. Some multithreaded applications may suffer slower response time, but that’s easily fixed by setting the limit back to a lower number using sys.setcheckinterval(N). The limit can be retrieved with the new sys.getcheckinterval() function.

One minor but far-reaching change is that the names of extension types defined by the modules included with Python now contain the module and a '.' in front of the type name. For example, in Python 2.2, if you created a socket and printed its __class__, you’d get this output:

>>> s = socket.socket()
>>> s.__class__
<type 'socket'>

In 2.3, you get this:

>>> s.__class__
<type '_socket.socket'>

One of the noted incompatibilities between old- and new-style classes has been removed: you can now assign to the __name__ and __bases__ attributes of new-style classes. There are some restrictions on what can be assigned to __bases__ along the lines of those relating to assigning to an instance’s __class__ attribute.

String Changes

The in operator now works differently for strings. Previously, when evaluating X in Y where X and Y are strings, X could only be a single character. That’s now changed; X can be a string of any length, and X in Y will return True if X is a substring of Y. If X is the empty string, the result is always True.

>>> 'ab' in 'abcd'
True
>>> 'ad' in 'abcd'
False
>>> '' in 'abcd'
True

Note that this doesn’t tell you where the substring starts; if you need that information, use the find() string method.

The strip(), lstrip(), and rstrip() string methods now have an optional argument for specifying the characters to strip. The default is still to remove all whitespace characters:

>>> '   abc '.strip()
'abc'
>>> '><><abc<><><>'.strip('<>')
'abc'
>>> '><><abc<><><>\n'.strip('<>')
'abc<><><>\n'
>>> u'\u4000\u4001abc\u4000'.strip(u'\u4000')
u'\u4001abc'
>>>

(Suggested by Simon Brunning and implemented by Walter Dörwald.)

The startswith() and endswith() string methods now accept negative numbers for the start and end parameters.

Another new string method is zfill(), originally a function in the string module. zfill() pads a numeric string with zeros on the left until it’s the specified width. Note that the % operator is still more flexible and powerful than zfill().

>>> '45'.zfill(4)
'0045'
>>> '12345'.zfill(4)
'12345'
>>> 'goofy'.zfill(6)
'0goofy'

（由 Walter Dörwald 贡献。）

A new type object, basestring, has been added. Both 8-bit strings and Unicode strings inherit from this type, so isinstance(obj, basestring) will return True for either kind of string. It’s a completely abstract type, so you can’t create basestring instances.

Interned strings are no longer immortal and will now be garbage-collected in the usual way when the only reference to them is from the internal dictionary of interned strings. (Implemented by Oren Tirosh.)

性能优化

The creation of new-style class instances has been made much faster; they’re now faster than classic classes!

The sort() method of list objects has been extensively rewritten by Tim Peters, and the implementation is significantly faster.

Multiplication of large long integers is now much faster thanks to an implementation of Karatsuba multiplication, an algorithm that scales better than the O(n*n) required for the grade-school multiplication algorithm. (Original patch by Christopher A. Craig, and significantly reworked by Tim Peters.)

The SET_LINENO opcode is now gone. This may provide a small speed increase, depending on your compiler’s idiosyncrasies. See section Other Changes and Fixes for a longer explanation. (Removed by Michael Hudson.)

xrange() objects now have their own iterator, making for i in xrange(n) slightly faster than for i in range(n). (Patch by Raymond Hettinger.)

A number of small rearrangements have been made in various hotspots to improve performance, such as inlining a function or removing some code. (Implemented mostly by GvR, but lots of people have contributed single changes.)

The net result of the 2.3 optimizations is that Python 2.3 runs the pystone benchmark around 25% faster than Python 2.2.

新增，改进和弃用的模块

As usual, Python’s standard library received a number of enhancements and bug fixes. Here’s a partial list of the most notable changes, sorted alphabetically by module name. Consult the Misc/NEWS file in the source tree for a more complete list of changes, or look through the CVS logs for all the details.

The array module now supports arrays of Unicode characters using the 'u' format character. Arrays also now support using the += assignment operator to add another array’s contents, and the *= assignment operator to repeat an array. (Contributed by Jason Orendorff.)

The bsddb module has been replaced by version 4.1.6 of the PyBSDDB [http://pybsddb.sourceforge.net] package, providing a more complete interface to the transactional features of the BerkeleyDB library.

The old version of the module has been renamed to bsddb185 and is no longer built automatically; you’ll have to edit Modules/Setup to enable it. Note that the new bsddb package is intended to be compatible with the old module, so be sure to file bugs if you discover any incompatibilities. When upgrading to Python 2.3, if the new interpreter is compiled with a new version of the underlying BerkeleyDB library, you will almost certainly have to convert your database files to the new version. You can do this fairly easily with the new scripts db2pickle.py and pickle2db.py which you will find in the distribution’s Tools/scripts directory. If you’ve already been using the PyBSDDB package and importing it as bsddb3, you will have to change your import statements to import it as bsddb.

The new bz2 module is an interface to the bz2 data compression library. bz2-compressed data is usually smaller than corresponding zlib-compressed data. (Contributed by Gustavo Niemeyer.)

A set of standard date/time types has been added in the new datetime module. See the following section for more details.

The Distutils Extension class now supports an extra constructor argument named depends for listing additional source files that an extension depends on. This lets Distutils recompile the module if any of the dependency files are modified. For example, if sampmodule.c includes the header file sample.h, you would create the Extension object like this:

ext = Extension("samp",
                sources=["sampmodule.c"],
                depends=["sample.h"])

Modifying sample.h would then cause the module to be recompiled. (Contributed by Jeremy Hylton.)

Other minor changes to Distutils: it now checks for the CC, CFLAGS, CPP, LDFLAGS, and CPPFLAGS environment variables, using them to override the settings in Python’s configuration (contributed by Robert Weber).

Previously the doctest module would only search the docstrings of public methods and functions for test cases, but it now also examines private ones as well. The DocTestSuite() function creates a unittest.TestSuite object from a set of doctest tests.

The new gc.get_referents(object) function returns a list of all the objects referenced by object.

The getopt module gained a new function, gnu_getopt(), that supports the same arguments as the existing getopt() function but uses GNU-style scanning mode. The existing getopt() stops processing options as soon as a non-option argument is encountered, but in GNU-style mode processing continues, meaning that options and arguments can be mixed. For example:

>>> getopt.getopt(['-f', 'filename', 'output', '-v'], 'f:v')
([('-f', 'filename')], ['output', '-v'])
>>> getopt.gnu_getopt(['-f', 'filename', 'output', '-v'], 'f:v')
([('-f', 'filename'), ('-v', '')], ['output'])

（由 Peter Åstrand 贡献。）

The grp, pwd, and resource modules now return enhanced tuples:

>>> import grp
>>> g = grp.getgrnam('amk')
>>> g.gr_name, g.gr_gid
('amk', 500)

The gzip module can now handle files exceeding 2 GiB.

The new heapq module contains an implementation of a heap queue algorithm. A heap is an array-like data structure that keeps items in a partially sorted order such that, for every index k, heap[k] <= heap[2*k+1] and heap[k] <= heap[2*k+2]. This makes it quick to remove the smallest item, and inserting a new item while maintaining the heap property is O(lg n). (See https://xlinux.nist.gov/dads//HTML/priorityque.html for more information about the priority queue data structure.)

The heapq module provides heappush() and heappop() functions for adding and removing items while maintaining the heap property on top of some other mutable Python sequence type. Here’s an example that uses a Python list:

>>> import heapq
>>> heap = []
>>> for item in [3, 7, 5, 11, 1]:
...    heapq.heappush(heap, item)
...
>>> heap
[1, 3, 5, 11, 7]
>>> heapq.heappop(heap)
1
>>> heapq.heappop(heap)
3
>>> heap
[5, 7, 11]

（由 Kevin O’Connor 贡献。）

The IDLE integrated development environment has been updated using the code from the IDLEfork project (http://idlefork.sourceforge.net). The most notable feature is that the code being developed is now executed in a subprocess, meaning that there’s no longer any need for manual reload() operations. IDLE’s core code has been incorporated into the standard library as the idlelib package.

The imaplib module now supports IMAP over SSL. (Contributed by Piers Lauder and Tino Lange.)

The itertools contains a number of useful functions for use with iterators, inspired by various functions provided by the ML and Haskell languages. For example, itertools.ifilter(predicate, iterator) returns all elements in the iterator for which the function predicate() returns True, and itertools.repeat(obj, N) returns obj N times. There are a number of other functions in the module; see the package’s reference documentation for details. (Contributed by Raymond Hettinger.)

Two new functions in the math module, degrees(rads) and radians(degs), convert between radians and degrees. Other functions in the math module such as math.sin() and math.cos() have always required input values measured in radians. Also, an optional base argument was added to math.log() to make it easier to compute logarithms for bases other than e and 10. (Contributed by Raymond Hettinger.)

Several new POSIX functions (getpgid(), killpg(), lchown(), loadavg(), major(), makedev(), minor(), and mknod()) were added to the posix module that underlies the os module. (Contributed by Gustavo Niemeyer, Geert Jansen, and Denis S. Otkidach.)

In the os module, the *stat() family of functions can now report fractions of a second in a timestamp. Such time stamps are represented as floats, similar to the value returned by time.time().

During testing, it was found that some applications will break if time stamps are floats. For compatibility, when using the tuple interface of the stat_result time stamps will be represented as integers. When using named fields (a feature first introduced in Python 2.2), time stamps are still represented as integers, unless os.stat_float_times() is invoked to enable float return values:

>>> os.stat("/tmp").st_mtime
1034791200
>>> os.stat_float_times(True)
>>> os.stat("/tmp").st_mtime
1034791200.6335014

In Python 2.4, the default will change to always returning floats.

Application developers should enable this feature only if all their libraries work properly when confronted with floating point time stamps, or if they use the tuple API. If used, the feature should be activated on an application level instead of trying to enable it on a per-use basis.

The optparse module contains a new parser for command-line arguments that can convert option values to a particular Python type and will automatically generate a usage message. See the following section for more details.

The old and never-documented linuxaudiodev module has been deprecated, and a new version named ossaudiodev has been added. The module was renamed because the OSS sound drivers can be used on platforms other than Linux, and the interface has also been tidied and brought up to date in various ways. (Contributed by Greg Ward and Nicholas FitzRoy-Dale.)

The new platform module contains a number of functions that try to determine various properties of the platform you’re running on. There are functions for getting the architecture, CPU type, the Windows OS version, and even the Linux distribution version. (Contributed by Marc-André Lemburg.)

The parser objects provided by the pyexpat module can now optionally buffer character data, resulting in fewer calls to your character data handler and therefore faster performance. Setting the parser object’s buffer_text attribute to True will enable buffering.

The sample(population, k) function was added to the random module. population is a sequence or xrange object containing the elements of a population, and sample() chooses k elements from the population without replacing chosen elements. k can be any value up to len(population). For example:

>>> days = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'St', 'Sn']
>>> random.sample(days, 3)      # Choose 3 elements
['St', 'Sn', 'Th']
>>> random.sample(days, 7)      # Choose 7 elements
['Tu', 'Th', 'Mo', 'We', 'St', 'Fr', 'Sn']
>>> random.sample(days, 7)      # Choose 7 again
['We', 'Mo', 'Sn', 'Fr', 'Tu', 'St', 'Th']
>>> random.sample(days, 8)      # Can't choose eight
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "random.py", line 414, in sample
      raise ValueError, "sample larger than population"
ValueError: sample larger than population
>>> random.sample(xrange(1,10000,2), 10)   # Choose ten odd nos. under 10000
[3407, 3805, 1505, 7023, 2401, 2267, 9733, 3151, 8083, 9195]

The random module now uses a new algorithm, the Mersenne Twister, implemented in C. It’s faster and more extensively studied than the previous algorithm.

(All changes contributed by Raymond Hettinger.)

The readline module also gained a number of new functions: get_history_item(), get_current_history_length(), and redisplay().

The rexec and Bastion modules have been declared dead, and attempts to import them will fail with a RuntimeError. New-style classes provide new ways to break out of the restricted execution environment provided by rexec, and no one has interest in fixing them or time to do so. If you have applications using rexec, rewrite them to use something else.

(Sticking with Python 2.2 or 2.1 will not make your applications any safer because there are known bugs in the rexec module in those versions. To repeat: if you’re using rexec, stop using it immediately.)

The rotor module has been deprecated because the algorithm it uses for encryption is not believed to be secure. If you need encryption, use one of the several AES Python modules that are available separately.

The shutil module gained a move(src, dest) function that recursively moves a file or directory to a new location.

Support for more advanced POSIX signal handling was added to the signal but then removed again as it proved impossible to make it work reliably across platforms.

The socket module now supports timeouts. You can call the settimeout(t) method on a socket object to set a timeout of t seconds. Subsequent socket operations that take longer than t seconds to complete will abort and raise a socket.timeout exception.

The original timeout implementation was by Tim O’Malley. Michael Gilfix integrated it into the Python socket module and shepherded it through a lengthy review. After the code was checked in, Guido van Rossum rewrote parts of it. (This is a good example of a collaborative development process in action.)

On Windows, the socket module now ships with Secure Sockets Layer (SSL) support.

The value of the C PYTHON_API_VERSION macro is now exposed at the Python level as sys.api_version. The current exception can be cleared by calling the new sys.exc_clear() function.

The new tarfile module allows reading from and writing to tar-format archive files. (Contributed by Lars Gustäbel.)

The new textwrap module contains functions for wrapping strings containing paragraphs of text. The wrap(text, width) function takes a string and returns a list containing the text split into lines of no more than the chosen width. The fill(text, width) function returns a single string, reformatted to fit into lines no longer than the chosen width. (As you can guess, fill() is built on top of wrap(). For example:

>>> import textwrap
>>> paragraph = "Not a whit, we defy augury: ... more text ..."
>>> textwrap.wrap(paragraph, 60)
["Not a whit, we defy augury: there's a special providence in",
 "the fall of a sparrow. If it be now, 'tis not to come; if it",
 ...]
>>> print textwrap.fill(paragraph, 35)
Not a whit, we defy augury: there's
a special providence in the fall of
a sparrow. If it be now, 'tis not
to come; if it be not to come, it
will be now; if it be not now, yet
it will come: the readiness is all.
>>>

The module also contains a TextWrapper class that actually implements the text wrapping strategy. Both the TextWrapper class and the wrap() and fill() functions support a number of additional keyword arguments for fine-tuning the formatting; consult the module’s documentation for details. (Contributed by Greg Ward.)

The thread and threading modules now have companion modules, dummy_thread and dummy_threading, that provide a do-nothing implementation of the thread module’s interface for platforms where threads are not supported. The intention is to simplify thread-aware modules (ones that don’t rely on threads to run) by putting the following code at the top:

try:
    import threading as _threading
except ImportError:
    import dummy_threading as _threading

In this example, _threading is used as the module name to make it clear that the module being used is not necessarily the actual threading module. Code can call functions and use classes in _threading whether or not threads are supported, avoiding an if statement and making the code slightly clearer. This module will not magically make multithreaded code run without threads; code that waits for another thread to return or to do something will simply hang forever.

The time module’s strptime() function has long been an annoyance because it uses the platform C library’s strptime() implementation, and different platforms sometimes have odd bugs. Brett Cannon contributed a portable implementation that’s written in pure Python and should behave identically on all platforms.

The new timeit module helps measure how long snippets of Python code take to execute. The timeit.py file can be run directly from the command line, or the module’s Timer class can be imported and used directly. Here’s a short example that figures out whether it’s faster to convert an 8-bit string to Unicode by appending an empty Unicode string to it or by using the unicode() function:

import timeit

timer1 = timeit.Timer('unicode("abc")')
timer2 = timeit.Timer('"abc" + u""')

# Run three trials
print timer1.repeat(repeat=3, number=100000)
print timer2.repeat(repeat=3, number=100000)

# On my laptop this outputs:
# [0.36831796169281006, 0.37441694736480713, 0.35304892063140869]
# [0.17574405670166016, 0.18193507194519043, 0.17565798759460449]

The Tix module has received various bug fixes and updates for the current version of the Tix package.

The Tkinter module now works with a thread-enabled version of Tcl. Tcl’s threading model requires that widgets only be accessed from the thread in which they’re created; accesses from another thread can cause Tcl to panic. For certain Tcl interfaces, Tkinter will now automatically avoid this when a widget is accessed from a different thread by marshalling a command, passing it to the correct thread, and waiting for the results. Other interfaces can’t be handled automatically but Tkinter will now raise an exception on such an access so that you can at least find out about the problem. See https://mail.python.org/pipermail/python-dev/2002-December/031107.html for a more detailed explanation of this change. (Implemented by Martin von Löwis.)

Calling Tcl methods through _tkinter no longer returns only strings. Instead, if Tcl returns other objects those objects are converted to their Python equivalent, if one exists, or wrapped with a _tkinter.Tcl_Obj object if no Python equivalent exists. This behavior can be controlled through the wantobjects() method of tkapp objects.

When using _tkinter through the Tkinter module (as most Tkinter applications will), this feature is always activated. It should not cause compatibility problems, since Tkinter would always convert string results to Python types where possible.

If any incompatibilities are found, the old behavior can be restored by setting the wantobjects variable in the Tkinter module to false before creating the first tkapp object.

import Tkinter
Tkinter.wantobjects = 0

Any breakage caused by this change should be reported as a bug.

The UserDict module has a new DictMixin class which defines all dictionary methods for classes that already have a minimum mapping interface. This greatly simplifies writing classes that need to be substitutable for dictionaries, such as the classes in the shelve module.

Adding the mix-in as a superclass provides the full dictionary interface whenever the class defines __getitem__(), __setitem__(), __delitem__(), and keys(). For example:

>>> import UserDict
>>> class SeqDict(UserDict.DictMixin):
...     """Dictionary lookalike implemented with lists."""
...     def __init__(self):
...         self.keylist = []
...         self.valuelist = []
...     def __getitem__(self, key):
...         try:
...             i = self.keylist.index(key)
...         except ValueError:
...             raise KeyError
...         return self.valuelist[i]
...     def __setitem__(self, key, value):
...         try:
...             i = self.keylist.index(key)
...             self.valuelist[i] = value
...         except ValueError:
...             self.keylist.append(key)
...             self.valuelist.append(value)
...     def __delitem__(self, key):
...         try:
...             i = self.keylist.index(key)
...         except ValueError:
...             raise KeyError
...         self.keylist.pop(i)
...         self.valuelist.pop(i)
...     def keys(self):
...         return list(self.keylist)
...
>>> s = SeqDict()
>>> dir(s)      # See that other dictionary methods are implemented
['__cmp__', '__contains__', '__delitem__', '__doc__', '__getitem__',
 '__init__', '__iter__', '__len__', '__module__', '__repr__',
 '__setitem__', 'clear', 'get', 'has_key', 'items', 'iteritems',
 'iterkeys', 'itervalues', 'keylist', 'keys', 'pop', 'popitem',
 'setdefault', 'update', 'valuelist', 'values']

（由 Raymond Hettinger 贡献。）

The DOM implementation in xml.dom.minidom can now generate XML output in a particular encoding by providing an optional encoding argument to the toxml() and toprettyxml() methods of DOM nodes.

The xmlrpclib module now supports an XML-RPC extension for handling nil data values such as Python’s None. Nil values are always supported on unmarshalling an XML-RPC response. To generate requests containing None, you must supply a true value for the allow_none parameter when creating a Marshaller instance.

The new DocXMLRPCServer module allows writing self-documenting XML-RPC servers. Run it in demo mode (as a program) to see it in action. Pointing the Web browser to the RPC server produces pydoc-style documentation; pointing xmlrpclib to the server allows invoking the actual methods. (Contributed by Brian Quinlan.)

Support for internationalized domain names (RFCs 3454, 3490, 3491, and 3492) has been added. The “idna” encoding can be used to convert between a Unicode domain name and the ASCII-compatible encoding (ACE) of that name.

>{}>{}> u"www.Alliancefrançaise.nu".encode("idna")
'www.xn--alliancefranaise-npb.nu'

The socket module has also been extended to transparently convert Unicode hostnames to the ACE version before passing them to the C library. Modules that deal with hostnames such as httplib and ftplib) also support Unicode host names; httplib also sends HTTP Host headers using the ACE version of the domain name. urllib supports Unicode URLs with non-ASCII host names as long as the path part of the URL is ASCII only.

To implement this change, the stringprep module, the mkstringprep tool and the punycode encoding have been added.

Date/Time 类型

Date and time types suitable for expressing timestamps were added as the datetime module. The types don’t support different calendars or many fancy features, and just stick to the basics of representing time.

The three primary types are: date, representing a day, month, and year; time, consisting of hour, minute, and second; and datetime, which contains all the attributes of both date and time. There’s also a timedelta class representing differences between two points in time, and time zone logic is implemented by classes inheriting from the abstract tzinfo class.

You can create instances of date and time by either supplying keyword arguments to the appropriate constructor, e.g. datetime.date(year=1972, month=10, day=15), or by using one of a number of class methods. For example, the date.today() class method returns the current local date.

Once created, instances of the date/time classes are all immutable. There are a number of methods for producing formatted strings from objects:

>>> import datetime
>>> now = datetime.datetime.now()
>>> now.isoformat()
'2002-12-30T21:27:03.994956'
>>> now.ctime()  # Only available on date, datetime
'Mon Dec 30 21:27:03 2002'
>>> now.strftime('%Y %d %b')
'2002 30 Dec'

The replace() method allows modifying one or more fields of a date or datetime instance, returning a new instance:

>>> d = datetime.datetime.now()
>>> d
datetime.datetime(2002, 12, 30, 22, 15, 38, 827738)
>>> d.replace(year=2001, hour = 12)
datetime.datetime(2001, 12, 30, 12, 15, 38, 827738)
>>>

Instances can be compared, hashed, and converted to strings (the result is the same as that of isoformat()). date and datetime instances can be subtracted from each other, and added to timedelta instances. The largest missing feature is that there’s no standard library support for parsing strings and getting back a date or datetime.

For more information, refer to the module’s reference documentation. (Contributed by Tim Peters.)

optparse 模块

The getopt module provides simple parsing of command-line arguments. The new optparse module (originally named Optik) provides more elaborate command-line parsing that follows the Unix conventions, automatically creates the output for --help, and can perform different actions for different options.

You start by creating an instance of OptionParser and telling it what your program’s options are.

import sys
from optparse import OptionParser

op = OptionParser()
op.add_option('-i', '--input',
              action='store', type='string', dest='input',
              help='set input filename')
op.add_option('-l', '--length',
              action='store', type='int', dest='length',
              help='set maximum length of output')

Parsing a command line is then done by calling the parse_args() method.

options, args = op.parse_args(sys.argv[1:])
print options
print args

This returns an object containing all of the option values, and a list of strings containing the remaining arguments.

Invoking the script with the various arguments now works as you’d expect it to. Note that the length argument is automatically converted to an integer.

$ ./python opt.py -i data arg1
<Values at 0x400cad4c: {'input': 'data', 'length': None}>
['arg1']
$ ./python opt.py --input=data --length=4
<Values at 0x400cad2c: {'input': 'data', 'length': 4}>
[]
$

The help message is automatically generated for you:

$ ./python opt.py --help
usage: opt.py [options]

options:
  -h, --help            show this help message and exit
  -iINPUT, --input=INPUT
                        set input filename
  -lLENGTH, --length=LENGTH
                        set maximum length of output
$

有关更多详细信息，请参见模块的文档。

Optik was written by Greg Ward, with suggestions from the readers of the Getopt SIG.

Pymalloc: A Specialized Object Allocator

Pymalloc, a specialized object allocator written by Vladimir Marangozov, was a feature added to Python 2.1. Pymalloc is intended to be faster than the system malloc() and to have less memory overhead for allocation patterns typical of Python programs. The allocator uses C’s malloc() function to get large pools of memory and then fulfills smaller memory requests from these pools.

In 2.1 and 2.2, pymalloc was an experimental feature and wasn’t enabled by default; you had to explicitly enable it when compiling Python by providing the --with-pymalloc option to the configure script. In 2.3, pymalloc has had further enhancements and is now enabled by default; you’ll have to supply --without-pymalloc to disable it.

This change is transparent to code written in Python; however, pymalloc may expose bugs in C extensions. Authors of C extension modules should test their code with pymalloc enabled, because some incorrect code may cause core dumps at runtime.

There’s one particularly common error that causes problems. There are a number of memory allocation functions in Python’s C API that have previously just been aliases for the C library’s malloc() and free(), meaning that if you accidentally called mismatched functions the error wouldn’t be noticeable. When the object allocator is enabled, these functions aren’t aliases of malloc() and free() any more, and calling the wrong function to free memory may get you a core dump. For example, if memory was allocated using PyObject_Malloc(), it has to be freed using PyObject_Free(), not free(). A few modules included with Python fell afoul of this and had to be fixed; doubtless there are more third-party modules that will have the same problem.

As part of this change, the confusing multiple interfaces for allocating memory have been consolidated down into two API families. Memory allocated with one family must not be manipulated with functions from the other family. There is one family for allocating chunks of memory and another family of functions specifically for allocating Python objects.

To allocate and free an undistinguished chunk of memory use the “raw memory” family: PyMem_Malloc(), PyMem_Realloc(), and PyMem_Free().

The “object memory” family is the interface to the pymalloc facility described above and is biased towards a large number of “small” allocations: PyObject_Malloc(), PyObject_Realloc(), and PyObject_Free().

To allocate and free Python objects, use the “object” family PyObject_New(), PyObject_NewVar(), and PyObject_Del().

Thanks to lots of work by Tim Peters, pymalloc in 2.3 also provides debugging features to catch memory overwrites and doubled frees in both extension modules and in the interpreter itself. To enable this support, compile a debugging version of the Python interpreter by running configure with --with-pydebug.

To aid extension writers, a header file Misc/pymemcompat.h is distributed with the source to Python 2.3 that allows Python extensions to use the 2.3 interfaces to memory allocation while compiling against any version of Python since 1.5.2. You would copy the file from Python’s source distribution and bundle it with the source of your extension.

参见

https://hg.python.org/cpython/file/default/Objects/obmalloc.c

For the full details of the pymalloc implementation, see the comments at the top of the file Objects/obmalloc.c in the Python source code. The above link points to the file within the python.org SVN browser.

构建和 C API 的改变

Changes to Python’s build process and to the C API include:

The cycle detection implementation used by the garbage collection has proven to be stable, so it’s now been made mandatory. You can no longer compile Python without it, and the --with-cycle-gc switch to configure has been removed.

Python can now optionally be built as a shared library (libpython2.3.so) by supplying --enable-shared when running Python’s configure script. (Contributed by Ondrej Palkovsky.)

The DL_EXPORT and DL_IMPORT macros are now deprecated. Initialization functions for Python extension modules should now be declared using the new macro PyMODINIT_FUNC, while the Python core will generally use the PyAPI_FUNC and PyAPI_DATA macros.

The interpreter can be compiled without any docstrings for the built-in functions and modules by supplying --without-doc-strings to the configure script. This makes the Python executable about 10% smaller, but will also mean that you can’t get help for Python’s built-ins. (Contributed by Gustavo Niemeyer.)

The PyArg_NoArgs() macro is now deprecated, and code that uses it should be changed. For Python 2.2 and later, the method definition table can specify the METH_NOARGS flag, signalling that there are no arguments, and the argument checking can then be removed. If compatibility with pre-2.2 versions of Python is important, the code could use PyArg_ParseTuple(args, "") instead, but this will be slower than using METH_NOARGS.

PyArg_ParseTuple() accepts new format characters for various sizes of unsigned integers: B for unsigned char, H for unsigned short int, I for unsigned int, and K for unsigned long long.

A new function, PyObject_DelItemString(mapping, char *key) was added as shorthand for PyObject_DelItem(mapping, PyString_New(key)).

File objects now manage their internal string buffer differently, increasing it exponentially when needed. This results in the benchmark tests in Lib/test/test_bufio.py speeding up considerably (from 57 seconds to 1.7 seconds, according to one measurement).

It’s now possible to define class and static methods for a C extension type by setting either the METH_CLASS or METH_STATIC flags in a method’s PyMethodDef structure.

Python now includes a copy of the Expat XML parser’s source code, removing any dependence on a system version or local installation of Expat.

If you dynamically allocate type objects in your extension, you should be aware of a change in the rules relating to the __module__ and __name__ attributes. In summary, you will want to ensure the type’s dictionary contains a '__module__' key; making the module name the part of the type name leading up to the final period will no longer have the desired effect. For more detail, read the API reference documentation or the source.

Port-Specific Changes

Support for a port to IBM’s OS/2 using the EMX runtime environment was merged into the main Python source tree. EMX is a POSIX emulation layer over the OS/2 system APIs. The Python port for EMX tries to support all the POSIX-like capability exposed by the EMX runtime, and mostly succeeds; fork() and fcntl() are restricted by the limitations of the underlying emulation layer. The standard OS/2 port, which uses IBM’s Visual Age compiler, also gained support for case-sensitive import semantics as part of the integration of the EMX port into CVS. (Contributed by Andrew MacIntyre.)

On MacOS, most toolbox modules have been weaklinked to improve backward compatibility. This means that modules will no longer fail to load if a single routine is missing on the current OS version. Instead calling the missing routine will raise an exception. (Contributed by Jack Jansen.)

The RPM spec files, found in the Misc/RPM/ directory in the Python source distribution, were updated for 2.3. (Contributed by Sean Reifschneider.)

Other new platforms now supported by Python include AtheOS (http://atheos.cx/), GNU/Hurd, and OpenVMS.

Other Changes and Fixes

As usual, there were a bunch of other improvements and bugfixes scattered throughout the source tree. A search through the CVS change logs finds there were 523 patches applied and 514 bugs fixed between Python 2.2 and 2.3. Both figures are likely to be underestimates.

Some of the more notable changes are:

If the PYTHONINSPECT environment variable is set, the Python interpreter will enter the interactive prompt after running a Python program, as if Python had been invoked with the -i option. The environment variable can be set before running the Python interpreter, or it can be set by the Python program as part of its execution.

The regrtest.py script now provides a way to allow “all resources except foo.” A resource name passed to the -u option can now be prefixed with a hyphen ('-') to mean “remove this resource.” For example, the option ‘-uall,-bsddb’ could be used to enable the use of all resources except bsddb.

The tools used to build the documentation now work under Cygwin as well as Unix.

The SET_LINENO opcode has been removed. Back in the mists of time, this opcode was needed to produce line numbers in tracebacks and support trace functions (for, e.g., pdb). Since Python 1.5, the line numbers in tracebacks have been computed using a different mechanism that works with “python -O”. For Python 2.3 Michael Hudson implemented a similar scheme to determine when to call the trace function, removing the need for SET_LINENO entirely.

It would be difficult to detect any resulting difference from Python code, apart from a slight speed up when Python is run without -O.

C extensions that access the f_lineno field of frame objects should instead call PyCode_Addr2Line(f->f_code, f->f_lasti). This will have the added effect of making the code work as desired under “python -O” in earlier versions of Python.

A nifty new feature is that trace functions can now assign to the f_lineno attribute of frame objects, changing the line that will be executed next. A jump command has been added to the pdb debugger taking advantage of this new feature. (Implemented by Richie Hindle.)

移植到 Python 2.3

This section lists previously described changes that may require changes to your code:

yield is now always a keyword; if it’s used as a variable name in your code, a different name must be chosen.

For strings X and Y, X in Y now works if X is more than one character long.

The int() type constructor will now return a long integer instead of raising an OverflowError when a string or floating-point number is too large to fit into an integer.

If you have Unicode strings that contain 8-bit characters, you must declare the file’s encoding (UTF-8, Latin-1, or whatever) by adding a comment to the top of the file. See section PEP 263: Source Code Encodings for more information.

Calling Tcl methods through _tkinter no longer returns only strings. Instead, if Tcl returns other objects those objects are converted to their Python equivalent, if one exists, or wrapped with a _tkinter.Tcl_Obj object if no Python equivalent exists.

Large octal and hex literals such as 0xffffffff now trigger a FutureWarning. Currently they’re stored as 32-bit numbers and result in a negative value, but in Python 2.4 they’ll become positive long integers.

There are a few ways to fix this warning. If you really need a positive number, just add an L to the end of the literal. If you’re trying to get a 32-bit integer with low bits set and have previously used an expression such as ~(1 << 31), it’s probably clearest to start with all bits set and clear the desired upper bits. For example, to clear just the top bit (bit 31), you could write 0xffffffffL &~(1L<<31).

You can no longer disable assertions by assigning to __debug__.

The Distutils setup() function has gained various new keyword arguments such as depends. Old versions of the Distutils will abort if passed unknown keywords. A solution is to check for the presence of the new get_distutil_options() function in your setup.py and only uses the new keywords with a version of the Distutils that supports them:

from distutils import core

kw = {'sources': 'foo.c', ...}
if hasattr(core, 'get_distutil_options'):
    kw['depends'] = ['foo.h']
ext = Extension(**kw)

Using None as a variable name will now result in a SyntaxWarning warning.

Names of extension types defined by the modules included with Python now contain the module and a '.' in front of the type name.

致谢

作者要感谢以下人员为本文的各种草案提供建议，更正和帮助： Jeff Bauer, Simon Brunning, Brett Cannon, Michael Chermside, Andrew Dalke, Scott David Daniels, Fred L. Drake, Jr., David Fraser, Kelly Gerber, Raymond Hettinger, Michael Hudson, Chris Lambert, Detlef Lannert, Martin von Löwis, Andrew MacIntyre, Lalo Martins, Chad Netzer, Gustavo Niemeyer, Neal Norwitz, Hans Nowak, Chris Reedy, Francesco Ricciardi, Vinay Sajip, Neil Schemenauer, Roman Suzi, Jason Tishler, Just van Rossum.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

Python 2.2 有什么新变化

作者

A.M. Kuchling

概述

This article explains the new features in Python 2.2.2, released on October 14, 2002. Python 2.2.2 is a bugfix release of Python 2.2, originally released on December 21, 2001.

Python 2.2 can be thought of as the “cleanup release”. There are some features such as generators and iterators that are completely new, but most of the changes, significant and far-reaching though they may be, are aimed at cleaning up irregularities and dark corners of the language design.

This article doesn’t attempt to provide a complete specification of the new features, but instead provides a convenient overview. For full details, you should refer to the documentation for Python 2.2, such as the Python Library Reference [https://docs.python.org/2.2/lib/lib.html] and the Python Reference Manual [https://docs.python.org/2.2/ref/ref.html]. If you want to understand the complete implementation and design rationale for a change, refer to the PEP for a particular new feature.

PEPs 252 and 253: Type and Class Changes

The largest and most far-reaching changes in Python 2.2 are to Python’s model of objects and classes. The changes should be backward compatible, so it’s likely that your code will continue to run unchanged, but the changes provide some amazing new capabilities. Before beginning this, the longest and most complicated section of this article, I’ll provide an overview of the changes and offer some comments.

A long time ago I wrote a Web page listing flaws in Python’s design. One of the most significant flaws was that it’s impossible to subclass Python types implemented in C. In particular, it’s not possible to subclass built-in types, so you can’t just subclass, say, lists in order to add a single useful method to them. The UserList module provides a class that supports all of the methods of lists and that can be subclassed further, but there’s lots of C code that expects a regular Python list and won’t accept a UserList instance.

Python 2.2 fixes this, and in the process adds some exciting new capabilities. A brief summary:

You can subclass built-in types such as lists and even integers, and your subclasses should work in every place that requires the original type.

It’s now possible to define static and class methods, in addition to the instance methods available in previous versions of Python.

It’s also possible to automatically call methods on accessing or setting an instance attribute by using a new mechanism called properties. Many uses of __getattr__() can be rewritten to use properties instead, making the resulting code simpler and faster. As a small side benefit, attributes can now have docstrings, too.

The list of legal attributes for an instance can be limited to a particular set using slots, making it possible to safeguard against typos and perhaps make more optimizations possible in future versions of Python.

Some users have voiced concern about all these changes. Sure, they say, the new features are neat and lend themselves to all sorts of tricks that weren’t possible in previous versions of Python, but they also make the language more complicated. Some people have said that they’ve always recommended Python for its simplicity, and feel that its simplicity is being lost.

Personally, I think there’s no need to worry. Many of the new features are quite esoteric, and you can write a lot of Python code without ever needed to be aware of them. Writing a simple class is no more difficult than it ever was, so you don’t need to bother learning or teaching them unless they’re actually needed. Some very complicated tasks that were previously only possible from C will now be possible in pure Python, and to my mind that’s all for the better.

I’m not going to attempt to cover every single corner case and small change that were required to make the new features work. Instead this section will paint only the broad strokes. See section Related Links, “Related Links”, for further sources of information about Python 2.2’s new object model.

Old and New Classes

First, you should know that Python 2.2 really has two kinds of classes: classic or old-style classes, and new-style classes. The old-style class model is exactly the same as the class model in earlier versions of Python. All the new features described in this section apply only to new-style classes. This divergence isn’t intended to last forever; eventually old-style classes will be dropped, possibly in Python 3.0.

So how do you define a new-style class? You do it by subclassing an existing new-style class. Most of Python’s built-in types, such as integers, lists, dictionaries, and even files, are new-style classes now. A new-style class named object, the base class for all built-in types, has also been added so if no built-in type is suitable, you can just subclass object:

class C(object):
    def __init__ (self):
        ...
    ...

This means that class statements that don’t have any base classes are always classic classes in Python 2.2. (Actually you can also change this by setting a module-level variable named __metaclass__ — see PEP 253 [https://www.python.org/dev/peps/pep-0253] for the details — but it’s easier to just subclass object.)

The type objects for the built-in types are available as built-ins, named using a clever trick. Python has always had built-in functions named int(), float(), and str(). In 2.2, they aren’t functions any more, but type objects that behave as factories when called.

>>> int
<type 'int'>
>>> int('123')
123

To make the set of types complete, new type objects such as dict() and file() have been added. Here’s a more interesting example, adding a lock() method to file objects:

class LockableFile(file):
    def lock (self, operation, length=0, start=0, whence=0):
        import fcntl
        return fcntl.lockf(self.fileno(), operation,
                           length, start, whence)

The now-obsolete posixfile module contained a class that emulated all of a file object’s methods and also added a lock() method, but this class couldn’t be passed to internal functions that expected a built-in file, something which is possible with our new LockableFile.

Descriptors

In previous versions of Python, there was no consistent way to discover what attributes and methods were supported by an object. There were some informal conventions, such as defining __members__ and __methods__ attributes that were lists of names, but often the author of an extension type or a class wouldn’t bother to define them. You could fall back on inspecting the __dict__ of an object, but when class inheritance or an arbitrary __getattr__() hook were in use this could still be inaccurate.

The one big idea underlying the new class model is that an API for describing the attributes of an object using descriptors has been formalized. Descriptors specify the value of an attribute, stating whether it’s a method or a field. With the descriptor API, static methods and class methods become possible, as well as more exotic constructs.

Attribute descriptors are objects that live inside class objects, and have a few attributes of their own:

__name__ is the attribute’s name.

__doc__ is the attribute’s docstring.

__get__(object) is a method that retrieves the attribute value from object.

__set__(object, value) sets the attribute on object to value.

__delete__(object, value) deletes the value attribute of object.

For example, when you write obj.x, the steps that Python actually performs are:

descriptor = obj.__class__.x
descriptor.__get__(obj)

For methods, descriptor.__get__() returns a temporary object that’s callable, and wraps up the instance and the method to be called on it. This is also why static methods and class methods are now possible; they have descriptors that wrap up just the method, or the method and the class. As a brief explanation of these new kinds of methods, static methods aren’t passed the instance, and therefore resemble regular functions. Class methods are passed the class of the object, but not the object itself. Static and class methods are defined like this:

class C(object):
    def f(arg1, arg2):
        ...
    f = staticmethod(f)

    def g(cls, arg1, arg2):
        ...
    g = classmethod(g)

The staticmethod() function takes the function f(), and returns it wrapped up in a descriptor so it can be stored in the class object. You might expect there to be special syntax for creating such methods (def static f, defstatic f(), or something like that) but no such syntax has been defined yet; that’s been left for future versions of Python.

More new features, such as slots and properties, are also implemented as new kinds of descriptors, and it’s not difficult to write a descriptor class that does something novel. For example, it would be possible to write a descriptor class that made it possible to write Eiffel-style preconditions and postconditions for a method. A class that used this feature might be defined like this:

from eiffel import eiffelmethod

class C(object):
    def f(self, arg1, arg2):
        # The actual function
        ...
    def pre_f(self):
        # Check preconditions
        ...
    def post_f(self):
        # Check postconditions
        ...

    f = eiffelmethod(f, pre_f, post_f)

Note that a person using the new eiffelmethod() doesn’t have to understand anything about descriptors. This is why I think the new features don’t increase the basic complexity of the language. There will be a few wizards who need to know about it in order to write eiffelmethod() or the ZODB or whatever, but most users will just write code on top of the resulting libraries and ignore the implementation details.

Multiple Inheritance: The Diamond Rule

Multiple inheritance has also been made more useful through changing the rules under which names are resolved. Consider this set of classes (diagram taken from PEP 253 [https://www.python.org/dev/peps/pep-0253] by Guido van Rossum):

      class A:
        ^ ^  def save(self): ...
       /   \
      /     \
     /       \
    /         \
class B     class C:
    ^         ^  def save(self): ...
     \       /
      \     /
       \   /
        \ /
      class D

The lookup rule for classic classes is simple but not very smart; the base classes are searched depth-first, going from left to right. A reference to D.save() will search the classes D, B, and then A, where save() would be found and returned. C.save() would never be found at all. This is bad, because if C’s save() method is saving some internal state specific to C, not calling it will result in that state never getting saved.

New-style classes follow a different algorithm that’s a bit more complicated to explain, but does the right thing in this situation. (Note that Python 2.3 changes this algorithm to one that produces the same results in most cases, but produces more useful results for really complicated inheritance graphs.)

List all the base classes, following the classic lookup rule and include a class multiple times if it’s visited repeatedly. In the above example, the list of visited classes is [D, B, A, C, A].

Scan the list for duplicated classes. If any are found, remove all but one occurrence, leaving the last one in the list. In the above example, the list becomes [D, B, C, A] after dropping duplicates.

Following this rule, referring to D.save() will return C.save(), which is the behaviour we’re after. This lookup rule is the same as the one followed by Common Lisp. A new built-in function, super(), provides a way to get at a class’s superclasses without having to reimplement Python’s algorithm. The most commonly used form will be super(class, obj), which returns a bound superclass object (not the actual class object). This form will be used in methods to call a method in the superclass; for example, D’s save() method would look like this:

class D (B,C):
    def save (self):
        # Call superclass .save()
        super(D, self).save()
        # Save D's private information here
        ...

super() can also return unbound superclass objects when called as super(class) or super(class1, class2), but this probably won’t often be useful.

Attribute Access

A fair number of sophisticated Python classes define hooks for attribute access using __getattr__(); most commonly this is done for convenience, to make code more readable by automatically mapping an attribute access such as obj.parent into a method call such as obj.get_parent. Python 2.2 adds some new ways of controlling attribute access.

First, __getattr__(attr_name) is still supported by new-style classes, and nothing about it has changed. As before, it will be called when an attempt is made to access obj.foo and no attribute named foo is found in the instance’s dictionary.

New-style classes also support a new method, __getattribute__(attr_name). The difference between the two methods is that __getattribute__() is always called whenever any attribute is accessed, while the old __getattr__() is only called if foo isn’t found in the instance’s dictionary.

However, Python 2.2’s support for properties will often be a simpler way to trap attribute references. Writing a __getattr__() method is complicated because to avoid recursion you can’t use regular attribute accesses inside them, and instead have to mess around with the contents of __dict__. __getattr__() methods also end up being called by Python when it checks for other methods such as __repr__() or __coerce__(), and so have to be written with this in mind. Finally, calling a function on every attribute access results in a sizable performance loss.

property is a new built-in type that packages up three functions that get, set, or delete an attribute, and a docstring. For example, if you want to define a size attribute that’s computed, but also settable, you could write:

class C(object):
    def get_size (self):
        result = ... computation ...
        return result
    def set_size (self, size):
        ... compute something based on the size
        and set internal state appropriately ...

    # Define a property.  The 'delete this attribute'
    # method is defined as None, so the attribute
    # can't be deleted.
    size = property(get_size, set_size,
                    None,
                    "Storage size of this instance")

That is certainly clearer and easier to write than a pair of __getattr__()/__setattr__() methods that check for the size attribute and handle it specially while retrieving all other attributes from the instance’s __dict__. Accesses to size are also the only ones which have to perform the work of calling a function, so references to other attributes run at their usual speed.

Finally, it’s possible to constrain the list of attributes that can be referenced on an object using the new __slots__ class attribute. Python objects are usually very dynamic; at any time it’s possible to define a new attribute on an instance by just doing obj.new_attr=1. A new-style class can define a class attribute named __slots__ to limit the legal attributes to a particular set of names. An example will make this clear:

>>> class C(object):
...     __slots__ = ('template', 'name')
...
>>> obj = C()
>>> print obj.template
None
>>> obj.template = 'Test'
>>> print obj.template
Test
>>> obj.newattr = None
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'C' object has no attribute 'newattr'

Note how you get an AttributeError on the attempt to assign to an attribute not listed in __slots__.

Related Links

This section has just been a quick overview of the new features, giving enough of an explanation to start you programming, but many details have been simplified or ignored. Where should you go to get a more complete picture?

https://docs.python.org/dev/howto/descriptor.html is a lengthy tutorial introduction to the descriptor features, written by Guido van Rossum. If my description has whetted your appetite, go read this tutorial next, because it goes into much more detail about the new features while still remaining quite easy to read.

Next, there are two relevant PEPs, PEP 252 [https://www.python.org/dev/peps/pep-0252] and PEP 253 [https://www.python.org/dev/peps/pep-0253]. PEP 252 [https://www.python.org/dev/peps/pep-0252] is titled “Making Types Look More Like Classes”, and covers the descriptor API. PEP 253 [https://www.python.org/dev/peps/pep-0253] is titled “Subtyping Built-in Types”, and describes the changes to type objects that make it possible to subtype built-in objects. PEP 253 [https://www.python.org/dev/peps/pep-0253] is the more complicated PEP of the two, and at a few points the necessary explanations of types and meta-types may cause your head to explode. Both PEPs were written and implemented by Guido van Rossum, with substantial assistance from the rest of the Zope Corp. team.

Finally, there’s the ultimate authority: the source code. Most of the machinery for the type handling is in Objects/typeobject.c, but you should only resort to it after all other avenues have been exhausted, including posting a question to python-list or python-dev.

PEP 234: Iterators

Another significant addition to 2.2 is an iteration interface at both the C and Python levels. Objects can define how they can be looped over by callers.

In Python versions up to 2.1, the usual way to make for item in obj work is to define a __getitem__() method that looks something like this:

def __getitem__(self, index):
    return <next item>

__getitem__() is more properly used to define an indexing operation on an object so that you can write obj[5] to retrieve the sixth element. It’s a bit misleading when you’re using this only to support for loops. Consider some file-like object that wants to be looped over; the index parameter is essentially meaningless, as the class probably assumes that a series of __getitem__() calls will be made with index incrementing by one each time. In other words, the presence of the __getitem__() method doesn’t mean that using file[5] to randomly access the sixth element will work, though it really should.

In Python 2.2, iteration can be implemented separately, and __getitem__() methods can be limited to classes that really do support random access. The basic idea of iterators is simple. A new built-in function, iter(obj) or iter(C, sentinel), is used to get an iterator. iter(obj) returns an iterator for the object obj, while iter(C, sentinel) returns an iterator that will invoke the callable object C until it returns sentinel to signal that the iterator is done.

Python classes can define an __iter__() method, which should create and return a new iterator for the object; if the object is its own iterator, this method can just return self. In particular, iterators will usually be their own iterators. Extension types implemented in C can implement a tp_iter function in order to return an iterator, and extension types that want to behave as iterators can define a tp_iternext function.

So, after all this, what do iterators actually do? They have one required method, next(), which takes no arguments and returns the next value. When there are no more values to be returned, calling next() should raise the StopIteration exception.

>>> L = [1,2,3]
>>> i = iter(L)
>>> print i
<iterator object at 0x8116870>
>>> i.next()
1
>>> i.next()
2
>>> i.next()
3
>>> i.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
StopIteration
>>>

In 2.2, Python’s for statement no longer expects a sequence; it expects something for which iter() will return an iterator. For backward compatibility and convenience, an iterator is automatically constructed for sequences that don’t implement __iter__() or a tp_iter slot, so for i in [1,2,3] will still work. Wherever the Python interpreter loops over a sequence, it’s been changed to use the iterator protocol. This means you can do things like this:

>>> L = [1,2,3]
>>> i = iter(L)
>>> a,b,c = i
>>> a,b,c
(1, 2, 3)

Iterator support has been added to some of Python’s basic types. Calling iter() on a dictionary will return an iterator which loops over its keys:

>>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
...      'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
>>> for key in m: print key, m[key]
...
Mar 3
Feb 2
Aug 8
Sep 9
May 5
Jun 6
Jul 7
Jan 1
Apr 4
Nov 11
Dec 12
Oct 10

That’s just the default behaviour. If you want to iterate over keys, values, or key/value pairs, you can explicitly call the iterkeys(), itervalues(), or iteritems() methods to get an appropriate iterator. In a minor related change, the in operator now works on dictionaries, so key in dict is now equivalent to dict.has_key(key).

Files also provide an iterator, which calls the readline() method until there are no more lines in the file. This means you can now read each line of a file using code like this:

for line in file:
    # do something for each line
    ...

Note that you can only go forward in an iterator; there’s no way to get the previous element, reset the iterator, or make a copy of it. An iterator object could provide such additional capabilities, but the iterator protocol only requires a next() method.

参见

PEP 234 [https://www.python.org/dev/peps/pep-0234] - Iterators

由 Ka-Ping Yee 和 GvR 撰写；由 Python Labs 小组（主要由 GvR 和 Tim Peters）实现。

PEP 255: Simple Generators

Generators are another new feature, one that interacts with the introduction of iterators.

You’re doubtless familiar with how function calls work in Python or C. When you call a function, it gets a private namespace where its local variables are created. When the function reaches a return statement, the local variables are destroyed and the resulting value is returned to the caller. A later call to the same function will get a fresh new set of local variables. But, what if the local variables weren’t thrown away on exiting a function? What if you could later resume the function where it left off? This is what generators provide; they can be thought of as resumable functions.

Here’s the simplest example of a generator function:

def generate_ints(N):
    for i in range(N):
        yield i

A new keyword, yield, was introduced for generators. Any function containing a yield statement is a generator function; this is detected by Python’s bytecode compiler which compiles the function specially as a result. Because a new keyword was introduced, generators must be explicitly enabled in a module by including a from __future__ import generators statement near the top of the module’s source code. In Python 2.3 this statement will become unnecessary.

When you call a generator function, it doesn’t return a single value; instead it returns a generator object that supports the iterator protocol. On executing the yield statement, the generator outputs the value of i, similar to a return statement. The big difference between yield and a return statement is that on reaching a yield the generator’s state of execution is suspended and local variables are preserved. On the next call to the generator’s next() method, the function will resume executing immediately after the yield statement. (For complicated reasons, the yield statement isn’t allowed inside the try block of a try…finally statement; read PEP 255 [https://www.python.org/dev/peps/pep-0255] for a full explanation of the interaction between yield and exceptions.)

Here’s a sample usage of the generate_ints() generator:

>>> gen = generate_ints(3)
>>> gen
<generator object at 0x8117f90>
>>> gen.next()
0
>>> gen.next()
1
>>> gen.next()
2
>>> gen.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in generate_ints
StopIteration

You could equally write for i in generate_ints(5), or a,b,c = generate_ints(3).

Inside a generator function, the return statement can only be used without a value, and signals the end of the procession of values; afterwards the generator cannot return any further values. return with a value, such as return 5, is a syntax error inside a generator function. The end of the generator’s results can also be indicated by raising StopIteration manually, or by just letting the flow of execution fall off the bottom of the function.

You could achieve the effect of generators manually by writing your own class and storing all the local variables of the generator as instance variables. For example, returning a list of integers could be done by setting self.count to 0, and having the next() method increment self.count and return it. However, for a moderately complicated generator, writing a corresponding class would be much messier. Lib/test/test_generators.py contains a number of more interesting examples. The simplest one implements an in-order traversal of a tree using generators recursively.

# A recursive generator that generates Tree leaves in in-order.
def inorder(t):
    if t:
        for x in inorder(t.left):
            yield x
        yield t.label
        for x in inorder(t.right):
            yield x

Two other examples in Lib/test/test_generators.py produce solutions for the N-Queens problem (placing $N$ queens on an $NxN$ chess board so that no queen threatens another) and the Knight’s Tour (a route that takes a knight to every square of an $NxN$ chessboard without visiting any square twice).

The idea of generators comes from other programming languages, especially Icon (https://www.cs.arizona.edu/icon/), where the idea of generators is central. In Icon, every expression and function call behaves like a generator. One example from “An Overview of the Icon Programming Language” at https://www.cs.arizona.edu/icon/docs/ipd266.htm gives an idea of what this looks like:

sentence := "Store it in the neighboring harbor"
if (i := find("or", sentence)) > 5 then write(i)

In Icon the find() function returns the indexes at which the substring “or” is found: 3, 23, 33. In the if statement, i is first assigned a value of 3, but 3 is less than 5, so the comparison fails, and Icon retries it with the second value of 23. 23 is greater than 5, so the comparison now succeeds, and the code prints the value 23 to the screen.

Python doesn’t go nearly as far as Icon in adopting generators as a central concept. Generators are considered a new part of the core Python language, but learning or using them isn’t compulsory; if they don’t solve any problems that you have, feel free to ignore them. One novel feature of Python’s interface as compared to Icon’s is that a generator’s state is represented as a concrete object (the iterator) that can be passed around to other functions or stored in a data structure.

参见

PEP 255 [https://www.python.org/dev/peps/pep-0255] - 简单生成器

Written by Neil Schemenauer, Tim Peters, Magnus Lie Hetland. Implemented mostly by Neil Schemenauer and Tim Peters, with other fixes from the Python Labs crew.

PEP 237: 统一长整数和整数

In recent versions, the distinction between regular integers, which are 32-bit values on most machines, and long integers, which can be of arbitrary size, was becoming an annoyance. For example, on platforms that support files larger than 2**32 bytes, the tell() method of file objects has to return a long integer. However, there were various bits of Python that expected plain integers and would raise an error if a long integer was provided instead. For example, in Python 1.5, only regular integers could be used as a slice index, and 'abc'[1L:] would raise a TypeError exception with the message ‘slice index must be int’.

Python 2.2 will shift values from short to long integers as required. The ‘L’ suffix is no longer needed to indicate a long integer literal, as now the compiler will choose the appropriate type. (Using the ‘L’ suffix will be discouraged in future 2.x versions of Python, triggering a warning in Python 2.4, and probably dropped in Python 3.0.) Many operations that used to raise an OverflowError will now return a long integer as their result. For example:

>>> 1234567890123
1234567890123L
>>> 2 ** 64
18446744073709551616L

In most cases, integers and long integers will now be treated identically. You can still distinguish them with the type() built-in function, but that’s rarely needed.

参见

PEP 237 [https://www.python.org/dev/peps/pep-0237] - 统一长整数和整数

由 Moshe Zadka 和 Guido van Rossum 撰写 ; 大部分由 Guido van Rossum 实现。

PEP 238: Changing the Division Operator

The most controversial change in Python 2.2 heralds the start of an effort to fix an old design flaw that’s been in Python from the beginning. Currently Python’s division operator, /, behaves like C’s division operator when presented with two integer arguments: it returns an integer result that’s truncated down when there would be a fractional part. For example, 3/2 is 1, not 1.5, and (-1)/2 is -1, not -0.5. This means that the results of division can vary unexpectedly depending on the type of the two operands and because Python is dynamically typed, it can be difficult to determine the possible types of the operands.

(The controversy is over whether this is really a design flaw, and whether it’s worth breaking existing code to fix this. It’s caused endless discussions on python-dev, and in July 2001 erupted into a storm of acidly sarcastic postings on comp.lang.python. I won’t argue for either side here and will stick to describing what’s implemented in 2.2. Read PEP 238 [https://www.python.org/dev/peps/pep-0238] for a summary of arguments and counter-arguments.)

Because this change might break code, it’s being introduced very gradually. Python 2.2 begins the transition, but the switch won’t be complete until Python 3.0.

First, I’ll borrow some terminology from PEP 238 [https://www.python.org/dev/peps/pep-0238]. “True division” is the division that most non-programmers are familiar with: 3/2 is 1.5, 1/4 is 0.25, and so forth. “Floor division” is what Python’s / operator currently does when given integer operands; the result is the floor of the value returned by true division. “Classic division” is the current mixed behaviour of /; it returns the result of floor division when the operands are integers, and returns the result of true division when one of the operands is a floating-point number.

Here are the changes 2.2 introduces:

A new operator, //, is the floor division operator. (Yes, we know it looks like C++’s comment symbol.) // always performs floor division no matter what the types of its operands are, so 1 // 2 is 0 and 1.0 // 2.0 is also 0.0.

// is always available in Python 2.2; you don’t need to enable it using a __future__ statement.

By including a from __future__ import division in a module, the / operator will be changed to return the result of true division, so 1/2 is 0.5. Without the __future__ statement, / still means classic division. The default meaning of / will not change until Python 3.0.

Classes can define methods called __truediv__() and __floordiv__() to overload the two division operators. At the C level, there are also slots in the PyNumberMethods structure so extension types can define the two operators.

Python 2.2 supports some command-line arguments for testing whether code will work with the changed division semantics. Running python with -Q warn will cause a warning to be issued whenever division is applied to two integers. You can use this to find code that’s affected by the change and fix it. By default, Python 2.2 will simply perform classic division without a warning; the warning will be turned on by default in Python 2.3.

参见

PEP 238 [https://www.python.org/dev/peps/pep-0238] - Changing the Division Operator

由 Moshe Zadka 和 Guido van Rossum 撰写 ; 由 Guido van Rossum 实现。

Unicode Changes

Python’s Unicode support has been enhanced a bit in 2.2. Unicode strings are usually stored as UCS-2, as 16-bit unsigned integers. Python 2.2 can also be compiled to use UCS-4, 32-bit unsigned integers, as its internal encoding by supplying --enable-unicode=ucs4 to the configure script. (It’s also possible to specify --disable-unicode to completely disable Unicode support.)

When built to use UCS-4 (a “wide Python”), the interpreter can natively handle Unicode characters from U+000000 to U+110000, so the range of legal values for the unichr() function is expanded accordingly. Using an interpreter compiled to use UCS-2 (a “narrow Python”), values greater than 65535 will still cause unichr() to raise a ValueError exception. This is all described in PEP 261 [https://www.python.org/dev/peps/pep-0261], “Support for ‘wide’ Unicode characters”; consult it for further details.

Another change is simpler to explain. Since their introduction, Unicode strings have supported an encode() method to convert the string to a selected encoding such as UTF-8 or Latin-1. A symmetric decode([*encoding*]) method has been added to 8-bit strings (though not to Unicode strings) in 2.2. decode() assumes that the string is in the specified encoding and decodes it, returning whatever is returned by the codec.

Using this new feature, codecs have been added for tasks not directly related to Unicode. For example, codecs have been added for uu-encoding, MIME’s base64 encoding, and compression with the zlib module:

>>> s = """Here is a lengthy piece of redundant, overly verbose,
... and repetitive text.
... """
>>> data = s.encode('zlib')
>>> data
'x\x9c\r\xc9\xc1\r\x80 \x10\x04\xc0?Ul...'
>>> data.decode('zlib')
'Here is a lengthy piece of redundant, overly verbose,\nand repetitive text.\n'
>>> print s.encode('uu')
begin 666 <data>
M2&5R92!I<R!A(&QE;F=T:'D@<&EE8V4@;V8@<F5D=6YD86YT+"!O=F5R;'D@
>=F5R8F]S92P*86YD(')E<&5T:71I=F4@=&5X="X*

end
>>> "sheesh".encode('rot-13')
'furrfu'

To convert a class instance to Unicode, a __unicode__() method can be defined by a class, analogous to __str__().

encode(), decode(), and __unicode__() were implemented by Marc-André Lemburg. The changes to support using UCS-4 internally were implemented by Fredrik Lundh and Martin von Löwis.

参见

PEP 261 [https://www.python.org/dev/peps/pep-0261] - Support for ‘wide’ Unicode characters

Written by Paul Prescod.

PEP 227: Nested Scopes

In Python 2.1, statically nested scopes were added as an optional feature, to be enabled by a from __future__ import nested_scopes directive. In 2.2 nested scopes no longer need to be specially enabled, and are now always present. The rest of this section is a copy of the description of nested scopes from my “What’s New in Python 2.1” document; if you read it when 2.1 came out, you can skip the rest of this section.

The largest change introduced in Python 2.1, and made complete in 2.2, is to Python’s scoping rules. In Python 2.0, at any given time there are at most three namespaces used to look up variable names: local, module-level, and the built-in namespace. This often surprised people because it didn’t match their intuitive expectations. For example, a nested recursive function definition doesn’t work:

def f():
    ...
    def g(value):
        ...
        return g(value-1) + 1
    ...

The function g() will always raise a NameError exception, because the binding of the name g isn’t in either its local namespace or in the module-level namespace. This isn’t much of a problem in practice (how often do you recursively define interior functions like this?), but this also made using the lambda statement clumsier, and this was a problem in practice. In code which uses lambda you can often find local variables being copied by passing them as the default values of arguments.

def find(self, name):
    "Return list of any entries equal to 'name'"
    L = filter(lambda x, name=name: x == name,
               self.list_attribute)
    return L

The readability of Python code written in a strongly functional style suffers greatly as a result.

The most significant change to Python 2.2 is that static scoping has been added to the language to fix this problem. As a first effect, the name=name default argument is now unnecessary in the above example. Put simply, when a given variable name is not assigned a value within a function (by an assignment, or the def, class, or import statements), references to the variable will be looked up in the local namespace of the enclosing scope. A more detailed explanation of the rules, and a dissection of the implementation, can be found in the PEP.

This change may cause some compatibility problems for code where the same variable name is used both at the module level and as a local variable within a function that contains further function definitions. This seems rather unlikely though, since such code would have been pretty confusing to read in the first place.

One side effect of the change is that the from module import * and exec statements have been made illegal inside a function scope under certain conditions. The Python reference manual has said all along that from module import * is only legal at the top level of a module, but the CPython interpreter has never enforced this before. As part of the implementation of nested scopes, the compiler which turns Python source into bytecodes has to generate different code to access variables in a containing scope. from module import * and exec make it impossible for the compiler to figure this out, because they add names to the local namespace that are unknowable at compile time. Therefore, if a function contains function definitions or lambda expressions with free variables, the compiler will flag this by raising a SyntaxError exception.

To make the preceding explanation a bit clearer, here’s an example:

x = 1
def f():
    # The next line is a syntax error
    exec 'x=2'
    def g():
        return x

Line 4 containing the exec statement is a syntax error, since exec would define a new local variable named x whose value should be accessed by g().

This shouldn’t be much of a limitation, since exec is rarely used in most Python code (and when it is used, it’s often a sign of a poor design anyway).

参见

PEP 227 [https://www.python.org/dev/peps/pep-0227] - Statically Nested Scopes

由 Jeremy Hylton 撰写并实现。

新增和改进的模块

The xmlrpclib module was contributed to the standard library by Fredrik Lundh, providing support for writing XML-RPC clients. XML-RPC is a simple remote procedure call protocol built on top of HTTP and XML. For example, the following snippet retrieves a list of RSS channels from the O’Reilly Network, and then lists the recent headlines for one channel:

import xmlrpclib
s = xmlrpclib.Server(
      'http://www.oreillynet.com/meerkat/xml-rpc/server.php')
channels = s.meerkat.getChannels()
# channels is a list of dictionaries, like this:
# [{'id': 4, 'title': 'Freshmeat Daily News'}
#  {'id': 190, 'title': '32Bits Online'},
#  {'id': 4549, 'title': '3DGamers'}, ... ]

# Get the items for one channel
items = s.meerkat.getItems( {'channel': 4} )

# 'items' is another list of dictionaries, like this:
# [{'link': 'http://freshmeat.net/releases/52719/',
#   'description': 'A utility which converts HTML to XSL FO.',
#   'title': 'html2fo 0.3 (Default)'}, ... ]

The SimpleXMLRPCServer module makes it easy to create straightforward XML-RPC servers. See http://www.xmlrpc.com/ for more information about XML-RPC.

The new hmac module implements the HMAC algorithm described by RFC 2104 [https://tools.ietf.org/html/rfc2104.html]. (Contributed by Gerhard Häring.)

Several functions that originally returned lengthy tuples now return pseudo-sequences that still behave like tuples but also have mnemonic attributes such as memberst_mtime or tm_year. The enhanced functions include stat(), fstat(), statvfs(), and fstatvfs() in the os module, and localtime(), gmtime(), and strptime() in the time module.

For example, to obtain a file’s size using the old tuples, you’d end up writing something like file_size = os.stat(filename)[stat.ST_SIZE], but now this can be written more clearly as file_size = os.stat(filename).st_size.

The original patch for this feature was contributed by Nick Mathewson.

The Python profiler has been extensively reworked and various errors in its output have been corrected. (Contributed by Fred L. Drake, Jr. and Tim Peters.)

The socket module can be compiled to support IPv6; specify the --enable-ipv6 option to Python’s configure script. (Contributed by Jun-ichiro “itojun” Hagino.)

Two new format characters were added to the struct module for 64-bit integers on platforms that support the C long long type. q is for a signed 64-bit integer, and Q is for an unsigned one. The value is returned in Python’s long integer type. (Contributed by Tim Peters.)

In the interpreter’s interactive mode, there’s a new built-in function help() that uses the pydoc module introduced in Python 2.1 to provide interactive help. help(object) displays any available help text about object. help() with no argument puts you in an online help utility, where you can enter the names of functions, classes, or modules to read their help text. (Contributed by Guido van Rossum, using Ka-Ping Yee’s pydoc module.)

Various bugfixes and performance improvements have been made to the SRE engine underlying the re module. For example, the re.sub() and re.split() functions have been rewritten in C. Another contributed patch speeds up certain Unicode character ranges by a factor of two, and a new finditer() method that returns an iterator over all the non-overlapping matches in a given string. (SRE is maintained by Fredrik Lundh. The BIGCHARSET patch was contributed by Martin von Löwis.)

The smtplib module now supports RFC 2487 [https://tools.ietf.org/html/rfc2487.html], “Secure SMTP over TLS”, so it’s now possible to encrypt the SMTP traffic between a Python program and the mail transport agent being handed a message. smtplib also supports SMTP authentication. (Contributed by Gerhard Häring.)

The imaplib module, maintained by Piers Lauder, has support for several new extensions: the NAMESPACE extension defined in RFC 2342 [https://tools.ietf.org/html/rfc2342.html], SORT, GETACL and SETACL. (Contributed by Anthony Baxter and Michel Pelletier.)

The rfc822 module’s parsing of email addresses is now compliant with RFC 2822 [https://tools.ietf.org/html/rfc2822.html], an update to RFC 822 [https://tools.ietf.org/html/rfc822.html]. (The module’s name is not going to be changed to rfc2822.) A new package, email, has also been added for parsing and generating e-mail messages. (Contributed by Barry Warsaw, and arising out of his work on Mailman.)

The difflib module now contains a new Differ class for producing human-readable lists of changes (a “delta”) between two sequences of lines of text. There are also two generator functions, ndiff() and restore(), which respectively return a delta from two sequences, or one of the original sequences from a delta. (Grunt work contributed by David Goodger, from ndiff.py code by Tim Peters who then did the generatorization.)

New constants ascii_letters, ascii_lowercase, and ascii_uppercase were added to the string module. There were several modules in the standard library that used string.letters to mean the ranges A-Za-z, but that assumption is incorrect when locales are in use, because string.letters varies depending on the set of legal characters defined by the current locale. The buggy modules have all been fixed to use ascii_letters instead. (Reported by an unknown person; fixed by Fred L. Drake, Jr.)

The mimetypes module now makes it easier to use alternative MIME-type databases by the addition of a MimeTypes class, which takes a list of filenames to be parsed. (Contributed by Fred L. Drake, Jr.)

A Timer class was added to the threading module that allows scheduling an activity to happen at some future time. (Contributed by Itamar Shtull-Trauring.)

Interpreter Changes and Fixes

Some of the changes only affect people who deal with the Python interpreter at the C level because they’re writing Python extension modules, embedding the interpreter, or just hacking on the interpreter itself. If you only write Python code, none of the changes described here will affect you very much.

Profiling and tracing functions can now be implemented in C, which can operate at much higher speeds than Python-based functions and should reduce the overhead of profiling and tracing. This will be of interest to authors of development environments for Python. Two new C functions were added to Python’s API, PyEval_SetProfile() and PyEval_SetTrace(). The existing sys.setprofile() and sys.settrace() functions still exist, and have simply been changed to use the new C-level interface. (Contributed by Fred L. Drake, Jr.)

Another low-level API, primarily of interest to implementors of Python debuggers and development tools, was added. PyInterpreterState_Head() and PyInterpreterState_Next() let a caller walk through all the existing interpreter objects; PyInterpreterState_ThreadHead() and PyThreadState_Next() allow looping over all the thread states for a given interpreter. (Contributed by David Beazley.)

The C-level interface to the garbage collector has been changed to make it easier to write extension types that support garbage collection and to debug misuses of the functions. Various functions have slightly different semantics, so a bunch of functions had to be renamed. Extensions that use the old API will still compile but will not participate in garbage collection, so updating them for 2.2 should be considered fairly high priority.

To upgrade an extension module to the new API, perform the following steps:

Rename Py_TPFLAGS_GC() to PyTPFLAGS_HAVE_GC().

Use PyObject_GC_New() or PyObject_GC_NewVar() to allocate

objects, and PyObject_GC_Del() to deallocate them.

Rename PyObject_GC_Init() to PyObject_GC_Track() and

PyObject_GC_Fini() to PyObject_GC_UnTrack().

Remove PyGC_HEAD_SIZE() from object size calculations.

Remove calls to PyObject_AS_GC() and PyObject_FROM_GC().

A new et format sequence was added to PyArg_ParseTuple(); et takes both a parameter and an encoding name, and converts the parameter to the given encoding if the parameter turns out to be a Unicode string, or leaves it alone if it’s an 8-bit string, assuming it to already be in the desired encoding. This differs from the es format character, which assumes that 8-bit strings are in Python’s default ASCII encoding and converts them to the specified new encoding. (Contributed by M.-A. Lemburg, and used for the MBCS support on Windows described in the following section.)

A different argument parsing function, PyArg_UnpackTuple(), has been added that’s simpler and presumably faster. Instead of specifying a format string, the caller simply gives the minimum and maximum number of arguments expected, and a set of pointers to PyObject* variables that will be filled in with argument values.

Two new flags METH_NOARGS and METH_O are available in method definition tables to simplify implementation of methods with no arguments or a single untyped argument. Calling such methods is more efficient than calling a corresponding method that uses METH_VARARGS. Also, the old METH_OLDARGS style of writing C methods is now officially deprecated.

Two new wrapper functions, PyOS_snprintf() and PyOS_vsnprintf() were added to provide cross-platform implementations for the relatively new snprintf() and vsnprintf() C lib APIs. In contrast to the standard sprintf() and vsprintf() functions, the Python versions check the bounds of the buffer used to protect against buffer overruns. (Contributed by M.-A. Lemburg.)

The _PyTuple_Resize() function has lost an unused parameter, so now it takes 2 parameters instead of 3. The third argument was never used, and can simply be discarded when porting code from earlier versions to Python 2.2.

Other Changes and Fixes

As usual there were a bunch of other improvements and bugfixes scattered throughout the source tree. A search through the CVS change logs finds there were 527 patches applied and 683 bugs fixed between Python 2.1 and 2.2; 2.2.1 applied 139 patches and fixed 143 bugs; 2.2.2 applied 106 patches and fixed 82 bugs. These figures are likely to be underestimates.

Some of the more notable changes are:

The code for the MacOS port for Python, maintained by Jack Jansen, is now kept in the main Python CVS tree, and many changes have been made to support MacOS X.

The most significant change is the ability to build Python as a framework, enabled by supplying the --enable-framework option to the configure script when compiling Python. According to Jack Jansen, “This installs a self-contained Python installation plus the OS X framework “glue” into /Library/Frameworks/Python.framework (or another location of choice). For now there is little immediate added benefit to this (actually, there is the disadvantage that you have to change your PATH to be able to find Python), but it is the basis for creating a full-blown Python application, porting the MacPython IDE, possibly using Python as a standard OSA scripting language and much more.”

Most of the MacPython toolbox modules, which interface to MacOS APIs such as windowing, QuickTime, scripting, etc. have been ported to OS X, but they’ve been left commented out in setup.py. People who want to experiment with these modules can uncomment them manually.

Keyword arguments passed to built-in functions that don’t take them now cause a TypeError exception to be raised, with the message “function takes no keyword arguments”.

Weak references, added in Python 2.1 as an extension module, are now part of the core because they’re used in the implementation of new-style classes. The ReferenceError exception has therefore moved from the weakref module to become a built-in exception.

A new script, Tools/scripts/cleanfuture.py by Tim Peters, automatically removes obsolete __future__ statements from Python source code.

An additional flags argument has been added to the built-in function compile(), so the behaviour of __future__ statements can now be correctly observed in simulated shells, such as those presented by IDLE and other development environments. This is described in PEP 264 [https://www.python.org/dev/peps/pep-0264]. (Contributed by Michael Hudson.)

The new license introduced with Python 1.6 wasn’t GPL-compatible. This is fixed by some minor textual changes to the 2.2 license, so it’s now legal to embed Python inside a GPLed program again. Note that Python itself is not GPLed, but instead is under a license that’s essentially equivalent to the BSD license, same as it always was. The license changes were also applied to the Python 2.0.1 and 2.1.1 releases.

When presented with a Unicode filename on Windows, Python will now convert it to an MBCS encoded string, as used by the Microsoft file APIs. As MBCS is explicitly used by the file APIs, Python’s choice of ASCII as the default encoding turns out to be an annoyance. On Unix, the locale’s character set is used if locale.nl_langinfo(CODESET) is available. (Windows support was contributed by Mark Hammond with assistance from Marc-André Lemburg. Unix support was added by Martin von Löwis.)

Large file support is now enabled on Windows. (Contributed by Tim Peters.)

The Tools/scripts/ftpmirror.py script now parses a .netrc file, if you have one. (Contributed by Mike Romberg.)

Some features of the object returned by the xrange() function are now deprecated, and trigger warnings when they’re accessed; they’ll disappear in Python 2.3. xrange objects tried to pretend they were full sequence types by supporting slicing, sequence multiplication, and the in operator, but these features were rarely used and therefore buggy. The tolist() method and the start, stop, and step attributes are also being deprecated. At the C level, the fourth argument to the PyRange_New() function, repeat, has also been deprecated.

There were a bunch of patches to the dictionary implementation, mostly to fix potential core dumps if a dictionary contains objects that sneakily changed their hash value, or mutated the dictionary they were contained in. For a while python-dev fell into a gentle rhythm of Michael Hudson finding a case that dumped core, Tim Peters fixing the bug, Michael finding another case, and round and round it went.

On Windows, Python can now be compiled with Borland C thanks to a number of patches contributed by Stephen Hansen, though the result isn’t fully functional yet. (But this is progress…)

Another Windows enhancement: Wise Solutions generously offered PythonLabs use of their InstallerMaster 8.1 system. Earlier PythonLabs Windows installers used Wise 5.0a, which was beginning to show its age. (Packaged up by Tim Peters.)

Files ending in .pyw can now be imported on Windows. .pyw is a Windows-only thing, used to indicate that a script needs to be run using PYTHONW.EXE instead of PYTHON.EXE in order to prevent a DOS console from popping up to display the output. This patch makes it possible to import such scripts, in case they’re also usable as modules. (Implemented by David Bolen.)

On platforms where Python uses the C dlopen() function to load extension modules, it’s now possible to set the flags used by dlopen() using the sys.getdlopenflags() and sys.setdlopenflags() functions. (Contributed by Bram Stolk.)

The pow() built-in function no longer supports 3 arguments when floating-point numbers are supplied. pow(x, y, z) returns (x**y) % z, but this is never useful for floating point numbers, and the final result varies unpredictably depending on the platform. A call such as pow(2.0, 8.0, 7.0) will now raise a TypeError exception.

致谢

作者要感谢以下人员为本文的各种草案提供建议，更正和帮助： Fred Bremmer, Keith Briggs, Andrew Dalke, Fred L. Drake, Jr., Carel Fellinger, David Goodger, Mark Hammond, Stephen Hansen, Michael Hudson, Jack Jansen, Marc-André Lemburg, Martin von Löwis, Fredrik Lundh, Michael McLay, Nick Mathewson, Paul Moore, Gustavo Niemeyer, Don O’Donnell, Joonas Paalasma, Tim Peters, Jens Quade, Tom Reinhardt, Neil Schemenauer, Guido van Rossum, Greg Ward, Edward Welbourne.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

Python 2.1 有什么新变化

作者

A.M. Kuchling

概述

This article explains the new features in Python 2.1. While there aren’t as many changes in 2.1 as there were in Python 2.0, there are still some pleasant surprises in store. 2.1 is the first release to be steered through the use of Python Enhancement Proposals, or PEPs, so most of the sizable changes have accompanying PEPs that provide more complete documentation and a design rationale for the change. This article doesn’t attempt to document the new features completely, but simply provides an overview of the new features for Python programmers. Refer to the Python 2.1 documentation, or to the specific PEP, for more details about any new feature that particularly interests you.

One recent goal of the Python development team has been to accelerate the pace of new releases, with a new release coming every 6 to 9 months. 2.1 is the first release to come out at this faster pace, with the first alpha appearing in January, 3 months after the final version of 2.0 was released.

Python 2.1 的最终版本于2001年4月17日发布。

PEP 227: Nested Scopes

The largest change in Python 2.1 is to Python’s scoping rules. In Python 2.0, at any given time there are at most three namespaces used to look up variable names: local, module-level, and the built-in namespace. This often surprised people because it didn’t match their intuitive expectations. For example, a nested recursive function definition doesn’t work:

def f():
    ...
    def g(value):
        ...
        return g(value-1) + 1
    ...

The function g() will always raise a NameError exception, because the binding of the name g isn’t in either its local namespace or in the module-level namespace. This isn’t much of a problem in practice (how often do you recursively define interior functions like this?), but this also made using the lambda statement clumsier, and this was a problem in practice. In code which uses lambda you can often find local variables being copied by passing them as the default values of arguments.

def find(self, name):
    "Return list of any entries equal to 'name'"
    L = filter(lambda x, name=name: x == name,
               self.list_attribute)
    return L

The readability of Python code written in a strongly functional style suffers greatly as a result.

The most significant change to Python 2.1 is that static scoping has been added to the language to fix this problem. As a first effect, the name=name default argument is now unnecessary in the above example. Put simply, when a given variable name is not assigned a value within a function (by an assignment, or the def, class, or import statements), references to the variable will be looked up in the local namespace of the enclosing scope. A more detailed explanation of the rules, and a dissection of the implementation, can be found in the PEP.

This change may cause some compatibility problems for code where the same variable name is used both at the module level and as a local variable within a function that contains further function definitions. This seems rather unlikely though, since such code would have been pretty confusing to read in the first place.

One side effect of the change is that the from module import * and exec statements have been made illegal inside a function scope under certain conditions. The Python reference manual has said all along that from module import * is only legal at the top level of a module, but the CPython interpreter has never enforced this before. As part of the implementation of nested scopes, the compiler which turns Python source into bytecodes has to generate different code to access variables in a containing scope. from module import * and exec make it impossible for the compiler to figure this out, because they add names to the local namespace that are unknowable at compile time. Therefore, if a function contains function definitions or lambda expressions with free variables, the compiler will flag this by raising a SyntaxError exception.

To make the preceding explanation a bit clearer, here’s an example:

x = 1
def f():
    # The next line is a syntax error
    exec 'x=2'
    def g():
        return x

Line 4 containing the exec statement is a syntax error, since exec would define a new local variable named x whose value should be accessed by g().

This shouldn’t be much of a limitation, since exec is rarely used in most Python code (and when it is used, it’s often a sign of a poor design anyway).

Compatibility concerns have led to nested scopes being introduced gradually; in Python 2.1, they aren’t enabled by default, but can be turned on within a module by using a future statement as described in PEP 236. (See the following section for further discussion of PEP 236.) In Python 2.2, nested scopes will become the default and there will be no way to turn them off, but users will have had all of 2.1’s lifetime to fix any breakage resulting from their introduction.

参见

PEP 227 [https://www.python.org/dev/peps/pep-0227] - Statically Nested Scopes

由 Jeremy Hylton 撰写并实现。

PEP 236: __future__ Directives

The reaction to nested scopes was widespread concern about the dangers of breaking code with the 2.1 release, and it was strong enough to make the Pythoneers take a more conservative approach. This approach consists of introducing a convention for enabling optional functionality in release N that will become compulsory in release N+1.

The syntax uses a from...import statement using the reserved module name __future__. Nested scopes can be enabled by the following statement:

from __future__ import nested_scopes

While it looks like a normal import statement, it’s not; there are strict rules on where such a future statement can be put. They can only be at the top of a module, and must precede any Python code or regular import statements. This is because such statements can affect how the Python bytecode compiler parses code and generates bytecode, so they must precede any statement that will result in bytecodes being produced.

参见

PEP 236 [https://www.python.org/dev/peps/pep-0236] - Back to the __future__

由 Tim Peters 撰写，主要由 Jeremy Hylton 实现。

PEP 207: Rich Comparisons

In earlier versions, Python’s support for implementing comparisons on user-defined classes and extension types was quite simple. Classes could implement a __cmp__() method that was given two instances of a class, and could only return 0 if they were equal or +1 or -1 if they weren’t; the method couldn’t raise an exception or return anything other than a Boolean value. Users of Numeric Python often found this model too weak and restrictive, because in the number-crunching programs that numeric Python is used for, it would be more useful to be able to perform elementwise comparisons of two matrices, returning a matrix containing the results of a given comparison for each element. If the two matrices are of different sizes, then the compare has to be able to raise an exception to signal the error.

In Python 2.1, rich comparisons were added in order to support this need. Python classes can now individually overload each of the <, <=, >, >=, ==, and != operations. The new magic method names are:

运算

方法名称

<

__lt__()

<=

__le__()

>

__gt__()

>=

__ge__()

==

__eq__()

!=

__ne__()

(The magic methods are named after the corresponding Fortran operators .LT.. .LE., &c. Numeric programmers are almost certainly quite familiar with these names and will find them easy to remember.)

Each of these magic methods is of the form method(self, other), where self will be the object on the left-hand side of the operator, while other will be the object on the right-hand side. For example, the expression A < B will cause A.__lt__(B) to be called.

Each of these magic methods can return anything at all: a Boolean, a matrix, a list, or any other Python object. Alternatively they can raise an exception if the comparison is impossible, inconsistent, or otherwise meaningless.

The built-in cmp(A,B) function can use the rich comparison machinery, and now accepts an optional argument specifying which comparison operation to use; this is given as one of the strings "<", "<=", ">", ">=", "==", or "!=". If called without the optional third argument, cmp() will only return -1, 0, or +1 as in previous versions of Python; otherwise it will call the appropriate method and can return any Python object.

There are also corresponding changes of interest to C programmers; there’s a new slot tp_richcmp in type objects and an API for performing a given rich comparison. I won’t cover the C API here, but will refer you to PEP 207, or to 2.1’s C API documentation, for the full list of related functions.

参见

PEP 207 [https://www.python.org/dev/peps/pep-0207] - Rich Comparisons

Written by Guido van Rossum, heavily based on earlier work by David Ascher, and implemented by Guido van Rossum.

PEP 230: Warning Framework

Over its 10 years of existence, Python has accumulated a certain number of obsolete modules and features along the way. It’s difficult to know when a feature is safe to remove, since there’s no way of knowing how much code uses it — perhaps no programs depend on the feature, or perhaps many do. To enable removing old features in a more structured way, a warning framework was added. When the Python developers want to get rid of a feature, it will first trigger a warning in the next version of Python. The following Python version can then drop the feature, and users will have had a full release cycle to remove uses of the old feature.

Python 2.1 adds the warning framework to be used in this scheme. It adds a warnings module that provide functions to issue warnings, and to filter out warnings that you don’t want to be displayed. Third-party modules can also use this framework to deprecate old features that they no longer wish to support.

For example, in Python 2.1 the regex module is deprecated, so importing it causes a warning to be printed:

>>> import regex
__main__:1: DeprecationWarning: the regex module
         is deprecated; please use the re module
>>>

Warnings can be issued by calling the warnings.warn() function:

warnings.warn("feature X no longer supported")

The first parameter is the warning message; an additional optional parameters can be used to specify a particular warning category.

Filters can be added to disable certain warnings; a regular expression pattern can be applied to the message or to the module name in order to suppress a warning. For example, you may have a program that uses the regex module and not want to spare the time to convert it to use the re module right now. The warning can be suppressed by calling

import warnings
warnings.filterwarnings(action = 'ignore',
                        message='.*regex module is deprecated',
                        category=DeprecationWarning,
                        module = '__main__')

This adds a filter that will apply only to warnings of the class DeprecationWarning triggered in the __main__ module, and applies a regular expression to only match the message about the regex module being deprecated, and will cause such warnings to be ignored. Warnings can also be printed only once, printed every time the offending code is executed, or turned into exceptions that will cause the program to stop (unless the exceptions are caught in the usual way, of course).

Functions were also added to Python’s C API for issuing warnings; refer to PEP 230 or to Python’s API documentation for the details.

参见

PEP 5 [https://www.python.org/dev/peps/pep-0005] - Guidelines for Language Evolution

Written by Paul Prescod, to specify procedures to be followed when removing old features from Python. The policy described in this PEP hasn’t been officially adopted, but the eventual policy probably won’t be too different from Prescod’s proposal.

PEP 230 [https://www.python.org/dev/peps/pep-0230] - Warning Framework

由 Guido van Rossum 撰写并实现。

PEP 229: New Build System

When compiling Python, the user had to go in and edit the Modules/Setup file in order to enable various additional modules; the default set is relatively small and limited to modules that compile on most Unix platforms. This means that on Unix platforms with many more features, most notably Linux, Python installations often don’t contain all useful modules they could.

Python 2.0 added the Distutils, a set of modules for distributing and installing extensions. In Python 2.1, the Distutils are used to compile much of the standard library of extension modules, autodetecting which ones are supported on the current machine. It’s hoped that this will make Python installations easier and more featureful.

Instead of having to edit the Modules/Setup file in order to enable modules, a setup.py script in the top directory of the Python source distribution is run at build time, and attempts to discover which modules can be enabled by examining the modules and header files on the system. If a module is configured in Modules/Setup, the setup.py script won’t attempt to compile that module and will defer to the Modules/Setup file’s contents. This provides a way to specific any strange command-line flags or libraries that are required for a specific platform.

In another far-reaching change to the build mechanism, Neil Schemenauer restructured things so Python now uses a single makefile that isn’t recursive, instead of makefiles in the top directory and in each of the Python/, Parser/, Objects/, and Modules/ subdirectories. This makes building Python faster and also makes hacking the Makefiles clearer and simpler.

参见

PEP 229 [https://www.python.org/dev/peps/pep-0229] - Using Distutils to Build Python

由 A.M. Kuchling 撰写并实现。

PEP 205: Weak References

Weak references, available through the weakref module, are a minor but useful new data type in the Python programmer’s toolbox.

Storing a reference to an object (say, in a dictionary or a list) has the side effect of keeping that object alive forever. There are a few specific cases where this behaviour is undesirable, object caches being the most common one, and another being circular references in data structures such as trees.

For example, consider a memoizing function that caches the results of another function f(x) by storing the function’s argument and its result in a dictionary:

_cache = {}
def memoize(x):
    if _cache.has_key(x):
        return _cache[x]

    retval = f(x)

    # Cache the returned object
    _cache[x] = retval

    return retval

This version works for simple things such as integers, but it has a side effect; the _cache dictionary holds a reference to the return values, so they’ll never be deallocated until the Python process exits and cleans up. This isn’t very noticeable for integers, but if f() returns an object, or a data structure that takes up a lot of memory, this can be a problem.

Weak references provide a way to implement a cache that won’t keep objects alive beyond their time. If an object is only accessible through weak references, the object will be deallocated and the weak references will now indicate that the object it referred to no longer exists. A weak reference to an object obj is created by calling wr = weakref.ref(obj). The object being referred to is returned by calling the weak reference as if it were a function: wr(). It will return the referenced object, or None if the object no longer exists.

This makes it possible to write a memoize() function whose cache doesn’t keep objects alive, by storing weak references in the cache.

_cache = {}
def memoize(x):
    if _cache.has_key(x):
        obj = _cache[x]()
        # If weak reference object still exists,
        # return it
        if obj is not None: return obj

    retval = f(x)

    # Cache a weak reference
    _cache[x] = weakref.ref(retval)

    return retval

The weakref module also allows creating proxy objects which behave like weak references — an object referenced only by proxy objects is deallocated – but instead of requiring an explicit call to retrieve the object, the proxy transparently forwards all operations to the object as long as the object still exists. If the object is deallocated, attempting to use a proxy will cause a weakref.ReferenceError exception to be raised.

proxy = weakref.proxy(obj)
proxy.attr   # Equivalent to obj.attr
proxy.meth() # Equivalent to obj.meth()
del obj
proxy.attr   # raises weakref.ReferenceError

参见

PEP 205 [https://www.python.org/dev/peps/pep-0205] - 弱引用

由 Fred L. Drake, Jr 撰写并实现。

PEP 232: Function Attributes

In Python 2.1, functions can now have arbitrary information attached to them. People were often using docstrings to hold information about functions and methods, because the __doc__ attribute was the only way of attaching any information to a function. For example, in the Zope Web application server, functions are marked as safe for public access by having a docstring, and in John Aycock’s SPARK parsing framework, docstrings hold parts of the BNF grammar to be parsed. This overloading is unfortunate, since docstrings are really intended to hold a function’s documentation; for example, it means you can’t properly document functions intended for private use in Zope.

Arbitrary attributes can now be set and retrieved on functions using the regular Python syntax:

def f(): pass

f.publish = 1
f.secure = 1
f.grammar = "A ::= B (C D)*"

The dictionary containing attributes can be accessed as the function’s __dict__. Unlike the __dict__ attribute of class instances, in functions you can actually assign a new dictionary to __dict__, though the new value is restricted to a regular Python dictionary; you can’t be tricky and set it to a UserDict instance, or any other random object that behaves like a mapping.

参见

PEP 232 [https://www.python.org/dev/peps/pep-0232] - Function Attributes

PEP 由 Barry Warsaw 撰写并实现

PEP 235: Importing Modules on Case-Insensitive Platforms

Some operating systems have filesystems that are case-insensitive, MacOS and Windows being the primary examples; on these systems, it’s impossible to distinguish the filenames FILE.PY and file.py, even though they do store the file’s name in its original case (they’re case-preserving, too).

In Python 2.1, the import statement will work to simulate case-sensitivity on case-insensitive platforms. Python will now search for the first case-sensitive match by default, raising an ImportError if no such file is found, so import file will not import a module named FILE.PY. Case-insensitive matching can be requested by setting the PYTHONCASEOK environment variable before starting the Python interpreter.

PEP 217: Interactive Display Hook

When using the Python interpreter interactively, the output of commands is displayed using the built-in repr() function. In Python 2.1, the variable sys.displayhook() can be set to a callable object which will be called instead of repr(). For example, you can set it to a special pretty-printing function:

>>> # Create a recursive data structure
... L = [1,2,3]
>>> L.append(L)
>>> L # Show Python's default output
[1, 2, 3, [...]]
>>> # Use pprint.pprint() as the display function
... import sys, pprint
>>> sys.displayhook = pprint.pprint
>>> L
[1, 2, 3,  <Recursion on list with id=135143996>]
>>>

参见

PEP 217 [https://www.python.org/dev/peps/pep-0217] - Display Hook for Interactive Use

由 Moshe Zadka 撰写并实现

PEP 208: New Coercion Model

How numeric coercion is done at the C level was significantly modified. This will only affect the authors of C extensions to Python, allowing them more flexibility in writing extension types that support numeric operations.

Extension types can now set the type flag Py_TPFLAGS_CHECKTYPES in their PyTypeObject structure to indicate that they support the new coercion model. In such extension types, the numeric slot functions can no longer assume that they’ll be passed two arguments of the same type; instead they may be passed two arguments of differing types, and can then perform their own internal coercion. If the slot function is passed a type it can’t handle, it can indicate the failure by returning a reference to the Py_NotImplemented singleton value. The numeric functions of the other type will then be tried, and perhaps they can handle the operation; if the other type also returns Py_NotImplemented, then a TypeError will be raised. Numeric methods written in Python can also return Py_NotImplemented, causing the interpreter to act as if the method did not exist (perhaps raising a TypeError, perhaps trying another object’s numeric methods).

参见

PEP 208 [https://www.python.org/dev/peps/pep-0208] - Reworking the Coercion Model

Written and implemented by Neil Schemenauer, heavily based upon earlier work by Marc-André Lemburg. Read this to understand the fine points of how numeric operations will now be processed at the C level.

PEP 241: Metadata in Python Packages

A common complaint from Python users is that there’s no single catalog of all the Python modules in existence. T. Middleton’s Vaults of Parnassus at http://www.vex.net/parnassus/ are the largest catalog of Python modules, but registering software at the Vaults is optional, and many people don’t bother.

As a first small step toward fixing the problem, Python software packaged using the Distutils sdist command will include a file named PKG-INFO containing information about the package such as its name, version, and author (metadata, in cataloguing terminology). PEP 241 contains the full list of fields that can be present in the PKG-INFO file. As people began to package their software using Python 2.1, more and more packages will include metadata, making it possible to build automated cataloguing systems and experiment with them. With the result experience, perhaps it’ll be possible to design a really good catalog and then build support for it into Python 2.2. For example, the Distutils sdist and bdist_* commands could support an upload option that would automatically upload your package to a catalog server.

You can start creating packages containing PKG-INFO even if you’re not using Python 2.1, since a new release of the Distutils will be made for users of earlier Python versions. Version 1.0.2 of the Distutils includes the changes described in PEP 241, as well as various bugfixes and enhancements. It will be available from the Distutils SIG at https://www.python.org/community/sigs/current/distutils-sig/.

参见

PEP 241 [https://www.python.org/dev/peps/pep-0241] - Metadata for Python Software Packages

由 A.M. Kuchling 撰写并实现。

PEP 243 [https://www.python.org/dev/peps/pep-0243] - Module Repository Upload Mechanism

Written by Sean Reifschneider, this draft PEP describes a proposed mechanism for uploading Python packages to a central server.

新增和改进的模块

Ka-Ping Yee contributed two new modules: inspect.py, a module for getting information about live Python code, and pydoc.py, a module for interactively converting docstrings to HTML or text. As a bonus, Tools/scripts/pydoc, which is now automatically installed, uses pydoc.py to display documentation given a Python module, package, or class name. For example, pydoc xml.dom displays the following:

Python Library Documentation: package xml.dom in xml

NAME
    xml.dom - W3C Document Object Model implementation for Python.

FILE
    /usr/local/lib/python2.1/xml/dom/__init__.pyc

DESCRIPTION
    The Python mapping of the Document Object Model is documented in the
    Python Library Reference in the section on the xml.dom package.

    This package contains the following modules:
      ...

pydoc also includes a Tk-based interactive help browser. pydoc quickly becomes addictive; try it out!

Two different modules for unit testing were added to the standard library. The doctest module, contributed by Tim Peters, provides a testing framework based on running embedded examples in docstrings and comparing the results against the expected output. PyUnit, contributed by Steve Purcell, is a unit testing framework inspired by JUnit, which was in turn an adaptation of Kent Beck’s Smalltalk testing framework. See http://pyunit.sourceforge.net/ for more information about PyUnit.

The difflib module contains a class, SequenceMatcher, which compares two sequences and computes the changes required to transform one sequence into the other. For example, this module can be used to write a tool similar to the Unix diff program, and in fact the sample program Tools/scripts/ndiff.py demonstrates how to write such a script.

curses.panel, a wrapper for the panel library, part of ncurses and of SYSV curses, was contributed by Thomas Gellekum. The panel library provides windows with the additional feature of depth. Windows can be moved higher or lower in the depth ordering, and the panel library figures out where panels overlap and which sections are visible.

The PyXML package has gone through a few releases since Python 2.0, and Python 2.1 includes an updated version of the xml package. Some of the noteworthy changes include support for Expat 1.2 and later versions, the ability for Expat parsers to handle files in any encoding supported by Python, and various bugfixes for SAX, DOM, and the minidom module.

Ping also contributed another hook for handling uncaught exceptions. sys.excepthook() can be set to a callable object. When an exception isn’t caught by any try…except blocks, the exception will be passed to sys.excepthook(), which can then do whatever it likes. At the Ninth Python Conference, Ping demonstrated an application for this hook: printing an extended traceback that not only lists the stack frames, but also lists the function arguments and the local variables for each frame.

Various functions in the time module, such as asctime() and localtime(), require a floating point argument containing the time in seconds since the epoch. The most common use of these functions is to work with the current time, so the floating point argument has been made optional; when a value isn’t provided, the current time will be used. For example, log file entries usually need a string containing the current time; in Python 2.1, time.asctime() can be used, instead of the lengthier time.asctime(time.localtime(time.time())) that was previously required.

This change was proposed and implemented by Thomas Wouters.

The ftplib module now defaults to retrieving files in passive mode, because passive mode is more likely to work from behind a firewall. This request came from the Debian bug tracking system, since other Debian packages use ftplib to retrieve files and then don’t work from behind a firewall. It’s deemed unlikely that this will cause problems for anyone, because Netscape defaults to passive mode and few people complain, but if passive mode is unsuitable for your application or network setup, call set_pasv(0) on FTP objects to disable passive mode.

Support for raw socket access has been added to the socket module, contributed by Grant Edwards.

The pstats module now contains a simple interactive statistics browser for displaying timing profiles for Python programs, invoked when the module is run as a script. Contributed by Eric S. Raymond.

A new implementation-dependent function, sys._getframe([depth]), has been added to return a given frame object from the current call stack. sys._getframe() returns the frame at the top of the call stack; if the optional integer argument depth is supplied, the function returns the frame that is depth calls below the top of the stack. For example, sys._getframe(1) returns the caller’s frame object.

This function is only present in CPython, not in Jython or the .NET implementation. Use it for debugging, and resist the temptation to put it into production code.

Other Changes and Fixes

There were relatively few smaller changes made in Python 2.1 due to the shorter release cycle. A search through the CVS change logs turns up 117 patches applied, and 136 bugs fixed; both figures are likely to be underestimates. Some of the more notable changes are:

A specialized object allocator is now optionally available, that should be faster than the system malloc() and have less memory overhead. The allocator uses C’s malloc() function to get large pools of memory, and then fulfills smaller memory requests from these pools. It can be enabled by providing the --with-pymalloc option to the configure script; see Objects/obmalloc.c for the implementation details.

Authors of C extension modules should test their code with the object allocator enabled, because some incorrect code may break, causing core dumps at runtime. There are a bunch of memory allocation functions in Python’s C API that have previously been just aliases for the C library’s malloc() and free(), meaning that if you accidentally called mismatched functions, the error wouldn’t be noticeable. When the object allocator is enabled, these functions aren’t aliases of malloc() and free() any more, and calling the wrong function to free memory will get you a core dump. For example, if memory was allocated using PyMem_New(), it has to be freed using PyMem_Del(), not free(). A few modules included with Python fell afoul of this and had to be fixed; doubtless there are more third-party modules that will have the same problem.

The object allocator was contributed by Vladimir Marangozov.

The speed of line-oriented file I/O has been improved because people often complain about its lack of speed, and because it’s often been used as a naïve benchmark. The readline() method of file objects has therefore been rewritten to be much faster. The exact amount of the speedup will vary from platform to platform depending on how slow the C library’s getc() was, but is around 66%, and potentially much faster on some particular operating systems. Tim Peters did much of the benchmarking and coding for this change, motivated by a discussion in comp.lang.python.

A new module and method for file objects was also added, contributed by Jeff Epler. The new method, xreadlines(), is similar to the existing xrange() built-in. xreadlines() returns an opaque sequence object that only supports being iterated over, reading a line on every iteration but not reading the entire file into memory as the existing readlines() method does. You’d use it like this:

for line in sys.stdin.xreadlines():
    # ... do something for each line ...
    ...

For a fuller discussion of the line I/O changes, see the python-dev summary for January 1–15, 2001 at https://mail.python.org/pipermail/python-dev/2001-January/.

A new method, popitem(), was added to dictionaries to enable destructively iterating through the contents of a dictionary; this can be faster for large dictionaries because there’s no need to construct a list containing all the keys or values. D.popitem() removes a random (key, value) pair from the dictionary D and returns it as a 2-tuple. This was implemented mostly by Tim Peters and Guido van Rossum, after a suggestion and preliminary patch by Moshe Zadka.

Modules can now control which names are imported when from module import * is used, by defining an __all__ attribute containing a list of names that will be imported. One common complaint is that if the module imports other modules such as sys or string, from module import * will add them to the importing module’s namespace. To fix this, simply list the public names in __all__:

# List public names
__all__ = ['Database', 'open']

A stricter version of this patch was first suggested and implemented by Ben Wolfson, but after some python-dev discussion, a weaker final version was checked in.

Applying repr() to strings previously used octal escapes for non-printable characters; for example, a newline was '\012'. This was a vestigial trace of Python’s C ancestry, but today octal is of very little practical use. Ka-Ping Yee suggested using hex escapes instead of octal ones, and using the \n, \t, \r escapes for the appropriate characters, and implemented this new formatting.

Syntax errors detected at compile-time can now raise exceptions containing the filename and line number of the error, a pleasant side effect of the compiler reorganization done by Jeremy Hylton.

C extensions which import other modules have been changed to use PyImport_ImportModule(), which means that they will use any import hooks that have been installed. This is also encouraged for third-party extensions that need to import some other module from C code.

The size of the Unicode character database was shrunk by another 340K thanks to Fredrik Lundh.

Some new ports were contributed: MacOS X (by Steven Majewski), Cygwin (by Jason Tishler); RISCOS (by Dietmar Schwertberger); Unixware 7 (by Billy G. Allie).

And there’s the usual list of minor bugfixes, minor memory leaks, docstring edits, and other tweaks, too lengthy to be worth itemizing; see the CVS logs for the full details if you want them.

致谢

作者要感谢以下人员对本文的各种草案提出建议： Graeme Cross, David Goodger, Jay Graves, Michael Hudson, Marc-André Lemburg, Fredrik Lundh, Neil Schemenauer, Thomas Wouters.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

Python 2.0 有什么新变化

作者

A.M. Kuchling and Moshe Zadka

概述

A new release of Python, version 2.0, was released on October 16, 2000. This article covers the exciting new features in 2.0, highlights some other useful changes, and points out a few incompatible changes that may require rewriting code.

Python’s development never completely stops between releases, and a steady flow of bug fixes and improvements are always being submitted. A host of minor fixes, a few optimizations, additional docstrings, and better error messages went into 2.0; to list them all would be impossible, but they’re certainly significant. Consult the publicly-available CVS logs if you want to see the full list. This progress is due to the five developers working for PythonLabs are now getting paid to spend their days fixing bugs, and also due to the improved communication resulting from moving to SourceForge.

What About Python 1.6?

Python 1.6 can be thought of as the Contractual Obligations Python release. After the core development team left CNRI in May 2000, CNRI requested that a 1.6 release be created, containing all the work on Python that had been performed at CNRI. Python 1.6 therefore represents the state of the CVS tree as of May 2000, with the most significant new feature being Unicode support. Development continued after May, of course, so the 1.6 tree received a few fixes to ensure that it’s forward-compatible with Python 2.0. 1.6 is therefore part of Python’s evolution, and not a side branch.

So, should you take much interest in Python 1.6? Probably not. The 1.6final and 2.0beta1 releases were made on the same day (September 5, 2000), the plan being to finalize Python 2.0 within a month or so. If you have applications to maintain, there seems little point in breaking things by moving to 1.6, fixing them, and then having another round of breakage within a month by moving to 2.0; you’re better off just going straight to 2.0. Most of the really interesting features described in this document are only in 2.0, because a lot of work was done between May and September.

新开发流程

The most important change in Python 2.0 may not be to the code at all, but to how Python is developed: in May 2000 the Python developers began using the tools made available by SourceForge for storing source code, tracking bug reports, and managing the queue of patch submissions. To report bugs or submit patches for Python 2.0, use the bug tracking and patch manager tools available from Python’s project page, located at https://sourceforge.net/projects/python/.

The most important of the services now hosted at SourceForge is the Python CVS tree, the version-controlled repository containing the source code for Python. Previously, there were roughly 7 or so people who had write access to the CVS tree, and all patches had to be inspected and checked in by one of the people on this short list. Obviously, this wasn’t very scalable. By moving the CVS tree to SourceForge, it became possible to grant write access to more people; as of September 2000 there were 27 people able to check in changes, a fourfold increase. This makes possible large-scale changes that wouldn’t be attempted if they’d have to be filtered through the small group of core developers. For example, one day Peter Schneider-Kamp took it into his head to drop K&R C compatibility and convert the C source for Python to ANSI C. After getting approval on the python-dev mailing list, he launched into a flurry of checkins that lasted about a week, other developers joined in to help, and the job was done. If there were only 5 people with write access, probably that task would have been viewed as “nice, but not worth the time and effort needed” and it would never have gotten done.

The shift to using SourceForge’s services has resulted in a remarkable increase in the speed of development. Patches now get submitted, commented on, revised by people other than the original submitter, and bounced back and forth between people until the patch is deemed worth checking in. Bugs are tracked in one central location and can be assigned to a specific person for fixing, and we can count the number of open bugs to measure progress. This didn’t come without a cost: developers now have more e-mail to deal with, more mailing lists to follow, and special tools had to be written for the new environment. For example, SourceForge sends default patch and bug notification e-mail messages that are completely unhelpful, so Ka-Ping Yee wrote an HTML screen-scraper that sends more useful messages.

The ease of adding code caused a few initial growing pains, such as code was checked in before it was ready or without getting clear agreement from the developer group. The approval process that has emerged is somewhat similar to that used by the Apache group. Developers can vote +1, +0, -0, or -1 on a patch; +1 and -1 denote acceptance or rejection, while +0 and -0 mean the developer is mostly indifferent to the change, though with a slight positive or negative slant. The most significant change from the Apache model is that the voting is essentially advisory, letting Guido van Rossum, who has Benevolent Dictator For Life status, know what the general opinion is. He can still ignore the result of a vote, and approve or reject a change even if the community disagrees with him.

Producing an actual patch is the last step in adding a new feature, and is usually easy compared to the earlier task of coming up with a good design. Discussions of new features can often explode into lengthy mailing list threads, making the discussion hard to follow, and no one can read every posting to python-dev. Therefore, a relatively formal process has been set up to write Python Enhancement Proposals (PEPs), modelled on the Internet RFC process. PEPs are draft documents that describe a proposed new feature, and are continually revised until the community reaches a consensus, either accepting or rejecting the proposal. Quoting from the introduction to PEP 1, “PEP Purpose and Guidelines”:

PEP stands for Python Enhancement Proposal. A PEP is a design document providing information to the Python community, or describing a new feature for Python. The PEP should provide a concise technical specification of the feature and a rationale for the feature.

We intend PEPs to be the primary mechanisms for proposing new features, for collecting community input on an issue, and for documenting the design decisions that have gone into Python. The PEP author is responsible for building consensus within the community and documenting dissenting opinions.

Read the rest of PEP 1 for the details of the PEP editorial process, style, and format. PEPs are kept in the Python CVS tree on SourceForge, though they’re not part of the Python 2.0 distribution, and are also available in HTML form from https://www.python.org/dev/peps/. As of September 2000, there are 25 PEPS, ranging from PEP 201, “Lockstep Iteration”, to PEP 225, “Elementwise/Objectwise Operators”.

Unicode

The largest new feature in Python 2.0 is a new fundamental data type: Unicode strings. Unicode uses 16-bit numbers to represent characters instead of the 8-bit number used by ASCII, meaning that 65,536 distinct characters can be supported.

The final interface for Unicode support was arrived at through countless often-stormy discussions on the python-dev mailing list, and mostly implemented by Marc-André Lemburg, based on a Unicode string type implementation by Fredrik Lundh. A detailed explanation of the interface was written up as PEP 100 [https://www.python.org/dev/peps/pep-0100], “Python Unicode Integration”. This article will simply cover the most significant points about the Unicode interfaces.

In Python source code, Unicode strings are written as u"string". Arbitrary Unicode characters can be written using a new escape sequence, \uHHHH, where HHHH is a 4-digit hexadecimal number from 0000 to FFFF. The existing \xHHHH escape sequence can also be used, and octal escapes can be used for characters up to U+01FF, which is represented by \777.

Unicode strings, just like regular strings, are an immutable sequence type. They can be indexed and sliced, but not modified in place. Unicode strings have an encode( [encoding] ) method that returns an 8-bit string in the desired encoding. Encodings are named by strings, such as 'ascii', 'utf-8', 'iso-8859-1', or whatever. A codec API is defined for implementing and registering new encodings that are then available throughout a Python program. If an encoding isn’t specified, the default encoding is usually 7-bit ASCII, though it can be changed for your Python installation by calling the sys.setdefaultencoding(encoding) function in a customized version of site.py.

Combining 8-bit and Unicode strings always coerces to Unicode, using the default ASCII encoding; the result of 'a' + u'bc' is u'abc'.

New built-in functions have been added, and existing built-ins modified to support Unicode:

unichr(ch) returns a Unicode string 1 character long, containing the character ch.

ord(u), where u is a 1-character regular or Unicode string, returns the number of the character as an integer.

unicode(string [, encoding]  [, errors] ) creates a Unicode string from an 8-bit string. encoding is a string naming the encoding to use. The errors parameter specifies the treatment of characters that are invalid for the current encoding; passing 'strict' as the value causes an exception to be raised on any encoding error, while 'ignore' causes errors to be silently ignored and 'replace' uses U+FFFD, the official replacement character, in case of any problems.

The exec statement, and various built-ins such as eval(), getattr(), and setattr() will also accept Unicode strings as well as regular strings. (It’s possible that the process of fixing this missed some built-ins; if you find a built-in function that accepts strings but doesn’t accept Unicode strings at all, please report it as a bug.)

A new module, unicodedata, provides an interface to Unicode character properties. For example, unicodedata.category(u'A') returns the 2-character string ‘Lu’, the ‘L’ denoting it’s a letter, and ‘u’ meaning that it’s uppercase. unicodedata.bidirectional(u'\u0660') returns ‘AN’, meaning that U+0660 is an Arabic number.

The codecs module contains functions to look up existing encodings and register new ones. Unless you want to implement a new encoding, you’ll most often use the codecs.lookup(encoding) function, which returns a 4-element tuple: (encode_func, decode_func, stream_reader, stream_writer).

encode_func is a function that takes a Unicode string, and returns a 2-tuple (string, length). string is an 8-bit string containing a portion (perhaps all) of the Unicode string converted into the given encoding, and length tells you how much of the Unicode string was converted.

decode_func is the opposite of encode_func, taking an 8-bit string and returning a 2-tuple (ustring, length), consisting of the resulting Unicode string ustring and the integer length telling how much of the 8-bit string was consumed.

stream_reader is a class that supports decoding input from a stream. stream_reader(file_obj) returns an object that supports the read(), readline(), and readlines() methods. These methods will all translate from the given encoding and return Unicode strings.

stream_writer, similarly, is a class that supports encoding output to a stream. stream_writer(file_obj) returns an object that supports the write() and writelines() methods. These methods expect Unicode strings, translating them to the given encoding on output.

For example, the following code writes a Unicode string into a file, encoding it as UTF-8:

import codecs

unistr = u'\u0660\u2000ab ...'

(UTF8_encode, UTF8_decode,
 UTF8_streamreader, UTF8_streamwriter) = codecs.lookup('UTF-8')

output = UTF8_streamwriter( open( '/tmp/output', 'wb') )
output.write( unistr )
output.close()

The following code would then read UTF-8 input from the file:

input = UTF8_streamreader( open( '/tmp/output', 'rb') )
print repr(input.read())
input.close()

Unicode-aware regular expressions are available through the re module, which has a new underlying implementation called SRE written by Fredrik Lundh of Secret Labs AB.

A -U command line option was added which causes the Python compiler to interpret all string literals as Unicode string literals. This is intended to be used in testing and future-proofing your Python code, since some future version of Python may drop support for 8-bit strings and provide only Unicode strings.

列表推导式

Lists are a workhorse data type in Python, and many programs manipulate a list at some point. Two common operations on lists are to loop over them, and either pick out the elements that meet a certain criterion, or apply some function to each element. For example, given a list of strings, you might want to pull out all the strings containing a given substring, or strip off trailing whitespace from each line.

The existing map() and filter() functions can be used for this purpose, but they require a function as one of their arguments. This is fine if there’s an existing built-in function that can be passed directly, but if there isn’t, you have to create a little function to do the required work, and Python’s scoping rules make the result ugly if the little function needs additional information. Take the first example in the previous paragraph, finding all the strings in the list containing a given substring. You could write the following to do it:

# Given the list L, make a list of all strings
# containing the substring S.
sublist = filter( lambda s, substring=S:
                     string.find(s, substring) != -1,
                  L)

Because of Python’s scoping rules, a default argument is used so that the anonymous function created by the lambda statement knows what substring is being searched for. List comprehensions make this cleaner:

sublist = [ s for s in L if string.find(s, S) != -1 ]

List comprehensions have the form:

[ expression for expr in sequence1
             for expr2 in sequence2 ...
             for exprN in sequenceN
             if condition ]

The for…in clauses contain the sequences to be iterated over. The sequences do not have to be the same length, because they are not iterated over in parallel, but from left to right; this is explained more clearly in the following paragraphs. The elements of the generated list will be the successive values of expression. The final if clause is optional; if present, expression is only evaluated and added to the result if condition is true.

To make the semantics very clear, a list comprehension is equivalent to the following Python code:

for expr1 in sequence1:
    for expr2 in sequence2:
    ...
        for exprN in sequenceN:
             if (condition):
                  # Append the value of
                  # the expression to the
                  # resulting list.

This means that when there are multiple for…in clauses, the resulting list will be equal to the product of the lengths of all the sequences. If you have two lists of length 3, the output list is 9 elements long:

seq1 = 'abc'
seq2 = (1,2,3)
>>> [ (x,y) for x in seq1 for y in seq2]
[('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 1),
('c', 2), ('c', 3)]

To avoid introducing an ambiguity into Python’s grammar, if expression is creating a tuple, it must be surrounded with parentheses. The first list comprehension below is a syntax error, while the second one is correct:

# Syntax error
[ x,y for x in seq1 for y in seq2]
# Correct
[ (x,y) for x in seq1 for y in seq2]

The idea of list comprehensions originally comes from the functional programming language Haskell (https://www.haskell.org). Greg Ewing argued most effectively for adding them to Python and wrote the initial list comprehension patch, which was then discussed for a seemingly endless time on the python-dev mailing list and kept up-to-date by Skip Montanaro.

Augmented Assignment

Augmented assignment operators, another long-requested feature, have been added to Python 2.0. Augmented assignment operators include +=, -=, *=, and so forth. For example, the statement a += 2 increments the value of the variable a by 2, equivalent to the slightly lengthier a = a + 2.

The full list of supported assignment operators is +=, -=, *=, /=, %=, **=, &=, |=, ^=, >>=, and <<=. Python classes can override the augmented assignment operators by defining methods named __iadd__(), __isub__(), etc. For example, the following Number class stores a number and supports using += to create a new instance with an incremented value.

class Number:
    def __init__(self, value):
        self.value = value
    def __iadd__(self, increment):
        return Number( self.value + increment)

n = Number(5)
n += 3
print n.value

The __iadd__() special method is called with the value of the increment, and should return a new instance with an appropriately modified value; this return value is bound as the new value of the variable on the left-hand side.

Augmented assignment operators were first introduced in the C programming language, and most C-derived languages, such as awk, C++, Java, Perl, and PHP also support them. The augmented assignment patch was implemented by Thomas Wouters.

字符串的方法

Until now string-manipulation functionality was in the string module, which was usually a front-end for the strop module written in C. The addition of Unicode posed a difficulty for the strop module, because the functions would all need to be rewritten in order to accept either 8-bit or Unicode strings. For functions such as string.replace(), which takes 3 string arguments, that means eight possible permutations, and correspondingly complicated code.

Instead, Python 2.0 pushes the problem onto the string type, making string manipulation functionality available through methods on both 8-bit strings and Unicode strings.

>>> 'andrew'.capitalize()
'Andrew'
>>> 'hostname'.replace('os', 'linux')
'hlinuxtname'
>>> 'moshe'.find('sh')
2

One thing that hasn’t changed, a noteworthy April Fools’ joke notwithstanding, is that Python strings are immutable. Thus, the string methods return new strings, and do not modify the string on which they operate.

The old string module is still around for backwards compatibility, but it mostly acts as a front-end to the new string methods.

Two methods which have no parallel in pre-2.0 versions, although they did exist in JPython for quite some time, are startswith() and endswith(). s.startswith(t) is equivalent to s[:len(t)] == t, while s.endswith(t) is equivalent to s[-len(t):] == t.

One other method which deserves special mention is join(). The join() method of a string receives one parameter, a sequence of strings, and is equivalent to the string.join() function from the old string module, with the arguments reversed. In other words, s.join(seq) is equivalent to the old string.join(seq, s).

Garbage Collection of Cycles

The C implementation of Python uses reference counting to implement garbage collection. Every Python object maintains a count of the number of references pointing to itself, and adjusts the count as references are created or destroyed. Once the reference count reaches zero, the object is no longer accessible, since you need to have a reference to an object to access it, and if the count is zero, no references exist any longer.

Reference counting has some pleasant properties: it’s easy to understand and implement, and the resulting implementation is portable, fairly fast, and reacts well with other libraries that implement their own memory handling schemes. The major problem with reference counting is that it sometimes doesn’t realise that objects are no longer accessible, resulting in a memory leak. This happens when there are cycles of references.

Consider the simplest possible cycle, a class instance which has a reference to itself:

instance = SomeClass()
instance.myself = instance

After the above two lines of code have been executed, the reference count of instance is 2; one reference is from the variable named 'instance', and the other is from the myself attribute of the instance.

If the next line of code is del instance, what happens? The reference count of instance is decreased by 1, so it has a reference count of 1; the reference in the myself attribute still exists. Yet the instance is no longer accessible through Python code, and it could be deleted. Several objects can participate in a cycle if they have references to each other, causing all of the objects to be leaked.

Python 2.0 fixes this problem by periodically executing a cycle detection algorithm which looks for inaccessible cycles and deletes the objects involved. A new gc module provides functions to perform a garbage collection, obtain debugging statistics, and tuning the collector’s parameters.

Running the cycle detection algorithm takes some time, and therefore will result in some additional overhead. It is hoped that after we’ve gotten experience with the cycle collection from using 2.0, Python 2.1 will be able to minimize the overhead with careful tuning. It’s not yet obvious how much performance is lost, because benchmarking this is tricky and depends crucially on how often the program creates and destroys objects. The detection of cycles can be disabled when Python is compiled, if you can’t afford even a tiny speed penalty or suspect that the cycle collection is buggy, by specifying the --without-cycle-gc switch when running the configure script.

Several people tackled this problem and contributed to a solution. An early implementation of the cycle detection approach was written by Toby Kelsey. The current algorithm was suggested by Eric Tiedemann during a visit to CNRI, and Guido van Rossum and Neil Schemenauer wrote two different implementations, which were later integrated by Neil. Lots of other people offered suggestions along the way; the March 2000 archives of the python-dev mailing list contain most of the relevant discussion, especially in the threads titled “Reference cycle collection for Python” and “Finalization again”.

其他核心变化

Various minor changes have been made to Python’s syntax and built-in functions. None of the changes are very far-reaching, but they’re handy conveniences.

Minor Language Changes

A new syntax makes it more convenient to call a given function with a tuple of arguments and/or a dictionary of keyword arguments. In Python 1.5 and earlier, you’d use the apply() built-in function: apply(f, args, kw) calls the function f() with the argument tuple args and the keyword arguments in the dictionary kw. apply() is the same in 2.0, but thanks to a patch from Greg Ewing, f(*args, **kw) is a shorter and clearer way to achieve the same effect. This syntax is symmetrical with the syntax for defining functions:

def f(*args, **kw):
    # args is a tuple of positional args,
    # kw is a dictionary of keyword args
    ...

The print statement can now have its output directed to a file-like object by following the print with >> file, similar to the redirection operator in Unix shells. Previously you’d either have to use the write() method of the file-like object, which lacks the convenience and simplicity of print, or you could assign a new value to sys.stdout and then restore the old value. For sending output to standard error, it’s much easier to write this:

print >> sys.stderr, "Warning: action field not supplied"

Modules can now be renamed on importing them, using the syntax import module as name or from module import name as othername. The patch was submitted by Thomas Wouters.

A new format style is available when using the % operator; ‘%r’ will insert the repr() of its argument. This was also added from symmetry considerations, this time for symmetry with the existing ‘%s’ format style, which inserts the str() of its argument. For example, '%r %s' % ('abc', 'abc') returns a string containing 'abc' abc.

Previously there was no way to implement a class that overrode Python’s built-in in operator and implemented a custom version. obj in seq returns true if obj is present in the sequence seq; Python computes this by simply trying every index of the sequence until either obj is found or an IndexError is encountered. Moshe Zadka contributed a patch which adds a __contains__() magic method for providing a custom implementation for in. Additionally, new built-in objects written in C can define what in means for them via a new slot in the sequence protocol.

Earlier versions of Python used a recursive algorithm for deleting objects. Deeply nested data structures could cause the interpreter to fill up the C stack and crash; Christian Tismer rewrote the deletion logic to fix this problem. On a related note, comparing recursive objects recursed infinitely and crashed; Jeremy Hylton rewrote the code to no longer crash, producing a useful result instead. For example, after this code:

a = []
b = []
a.append(a)
b.append(b)

The comparison a==b returns true, because the two recursive data structures are isomorphic. See the thread “trashcan and PR#7” in the April 2000 archives of the python-dev mailing list for the discussion leading up to this implementation, and some useful relevant links. Note that comparisons can now also raise exceptions. In earlier versions of Python, a comparison operation such as cmp(a,b) would always produce an answer, even if a user-defined __cmp__() method encountered an error, since the resulting exception would simply be silently swallowed.

Work has been done on porting Python to 64-bit Windows on the Itanium processor, mostly by Trent Mick of ActiveState. (Confusingly, sys.platform is still 'win32' on Win64 because it seems that for ease of porting, MS Visual C++ treats code as 32 bit on Itanium.) PythonWin also supports Windows CE; see the Python CE page at http://pythonce.sourceforge.net/ for more information.

Another new platform is Darwin/MacOS X; initial support for it is in Python 2.0. Dynamic loading works, if you specify “configure –with-dyld –with-suffix=.x”. Consult the README in the Python source distribution for more instructions.

An attempt has been made to alleviate one of Python’s warts, the often-confusing NameError exception when code refers to a local variable before the variable has been assigned a value. For example, the following code raises an exception on the print statement in both 1.5.2 and 2.0; in 1.5.2 a NameError exception is raised, while 2.0 raises a new UnboundLocalError exception. UnboundLocalError is a subclass of NameError, so any existing code that expects NameError to be raised should still work.

def f():
    print "i=",i
    i = i + 1
f()

Two new exceptions, TabError and IndentationError, have been introduced. They’re both subclasses of SyntaxError, and are raised when Python code is found to be improperly indented.

Changes to Built-in Functions

A new built-in, zip(seq1, seq2, ...), has been added. zip() returns a list of tuples where each tuple contains the i-th element from each of the argument sequences. The difference between zip() and map(None, seq1, seq2) is that map() pads the sequences with None if the sequences aren’t all of the same length, while zip() truncates the returned list to the length of the shortest argument sequence.

The int() and long() functions now accept an optional “base” parameter when the first argument is a string. int('123', 10) returns 123, while int('123', 16) returns 291. int(123, 16) raises a TypeError exception with the message “can’t convert non-string with explicit base”.

A new variable holding more detailed version information has been added to the sys module. sys.version_info is a tuple (major, minor, micro, level, serial) For example, in a hypothetical 2.0.1beta1, sys.version_info would be (2, 0, 1, 'beta', 1). level is a string such as "alpha", "beta", or "final" for a final release.

Dictionaries have an odd new method, setdefault(key, default), which behaves similarly to the existing get() method. However, if the key is missing, setdefault() both returns the value of default as get() would do, and also inserts it into the dictionary as the value for key. Thus, the following lines of code:

if dict.has_key( key ): return dict[key]
else:
    dict[key] = []
    return dict[key]

can be reduced to a single return dict.setdefault(key, []) statement.

The interpreter sets a maximum recursion depth in order to catch runaway recursion before filling the C stack and causing a core dump or GPF.. Previously this limit was fixed when you compiled Python, but in 2.0 the maximum recursion depth can be read and modified using sys.getrecursionlimit() and sys.setrecursionlimit(). The default value is 1000, and a rough maximum value for a given platform can be found by running a new script, Misc/find_recursionlimit.py.

移植 Python 2.0

New Python releases try hard to be compatible with previous releases, and the record has been pretty good. However, some changes are considered useful enough, usually because they fix initial design decisions that turned out to be actively mistaken, that breaking backward compatibility can’t always be avoided. This section lists the changes in Python 2.0 that may cause old Python code to break.

The change which will probably break the most code is tightening up the arguments accepted by some methods. Some methods would take multiple arguments and treat them as a tuple, particularly various list methods such as append() and insert(). In earlier versions of Python, if L is a list, L.append( 1,2 ) appends the tuple (1,2) to the list. In Python 2.0 this causes a TypeError exception to be raised, with the message: ‘append requires exactly 1 argument; 2 given’. The fix is to simply add an extra set of parentheses to pass both values as a tuple: L.append( (1,2) ).

The earlier versions of these methods were more forgiving because they used an old function in Python’s C interface to parse their arguments; 2.0 modernizes them to use PyArg_ParseTuple(), the current argument parsing function, which provides more helpful error messages and treats multi-argument calls as errors. If you absolutely must use 2.0 but can’t fix your code, you can edit Objects/listobject.c and define the preprocessor symbol NO_STRICT_LIST_APPEND to preserve the old behaviour; this isn’t recommended.

Some of the functions in the socket module are still forgiving in this way. For example, socket.connect( ('hostname', 25) )() is the correct form, passing a tuple representing an IP address, but socket.connect( 'hostname', 25 )() also works. socket.connect_ex() and socket.bind() are similarly easy-going. 2.0alpha1 tightened these functions up, but because the documentation actually used the erroneous multiple argument form, many people wrote code which would break with the stricter checking. GvR backed out the changes in the face of public reaction, so for the socket module, the documentation was fixed and the multiple argument form is simply marked as deprecated; it will be tightened up again in a future Python version.

The \x escape in string literals now takes exactly 2 hex digits. Previously it would consume all the hex digits following the ‘x’ and take the lowest 8 bits of the result, so \x123456 was equivalent to \x56.

The AttributeError and NameError exceptions have a more friendly error message, whose text will be something like 'Spam' instance has no attribute 'eggs' or name 'eggs' is not defined. Previously the error message was just the missing attribute name eggs, and code written to take advantage of this fact will break in 2.0.

Some work has been done to make integers and long integers a bit more interchangeable. In 1.5.2, large-file support was added for Solaris, to allow reading files larger than 2 GiB; this made the tell() method of file objects return a long integer instead of a regular integer. Some code would subtract two file offsets and attempt to use the result to multiply a sequence or slice a string, but this raised a TypeError. In 2.0, long integers can be used to multiply or slice a sequence, and it’ll behave as you’d intuitively expect it to; 3L * 'abc' produces ‘abcabcabc’, and (0,1,2,3)[2L:4L] produces (2,3). Long integers can also be used in various contexts where previously only integers were accepted, such as in the seek() method of file objects, and in the formats supported by the % operator (%d, %i, %x, etc.). For example, "%d" % 2L**64 will produce the string 18446744073709551616.

The subtlest long integer change of all is that the str() of a long integer no longer has a trailing ‘L’ character, though repr() still includes it. The ‘L’ annoyed many people who wanted to print long integers that looked just like regular integers, since they had to go out of their way to chop off the character. This is no longer a problem in 2.0, but code which does str(longval)[:-1] and assumes the ‘L’ is there, will now lose the final digit.

Taking the repr() of a float now uses a different formatting precision than str(). repr() uses %.17g format string for C’s sprintf(), while str() uses %.12g as before. The effect is that repr() may occasionally show more decimal places than str(), for certain numbers. For example, the number 8.1 can’t be represented exactly in binary, so repr(8.1) is '8.0999999999999996', while str(8.1) is '8.1'.

The -X command-line option, which turned all standard exceptions into strings instead of classes, has been removed; the standard exceptions will now always be classes. The exceptions module containing the standard exceptions was translated from Python to a built-in C module, written by Barry Warsaw and Fredrik Lundh.

扩展/嵌入更改

Some of the changes are under the covers, and will only be apparent to people writing C extension modules or embedding a Python interpreter in a larger application. If you aren’t dealing with Python’s C API, you can safely skip this section.

The version number of the Python C API was incremented, so C extensions compiled for 1.5.2 must be recompiled in order to work with 2.0. On Windows, it’s not possible for Python 2.0 to import a third party extension built for Python 1.5.x due to how Windows DLLs work, so Python will raise an exception and the import will fail.

Users of Jim Fulton’s ExtensionClass module will be pleased to find out that hooks have been added so that ExtensionClasses are now supported by isinstance() and issubclass(). This means you no longer have to remember to write code such as if type(obj) == myExtensionClass, but can use the more natural if isinstance(obj, myExtensionClass).

The Python/importdl.c file, which was a mass of #ifdefs to support dynamic loading on many different platforms, was cleaned up and reorganised by Greg Stein. importdl.c is now quite small, and platform-specific code has been moved into a bunch of Python/dynload_*.c files. Another cleanup: there were also a number of my*.h files in the Include/ directory that held various portability hacks; they’ve been merged into a single file, Include/pyport.h.

Vladimir Marangozov’s long-awaited malloc restructuring was completed, to make it easy to have the Python interpreter use a custom allocator instead of C’s standard malloc(). For documentation, read the comments in Include/pymem.h and Include/objimpl.h. For the lengthy discussions during which the interface was hammered out, see the Web archives of the ‘patches’ and ‘python-dev’ lists at python.org.

Recent versions of the GUSI development environment for MacOS support POSIX threads. Therefore, Python’s POSIX threading support now works on the Macintosh. Threading support using the user-space GNU pth library was also contributed.

Threading support on Windows was enhanced, too. Windows supports thread locks that use kernel objects only in case of contention; in the common case when there’s no contention, they use simpler functions which are an order of magnitude faster. A threaded version of Python 1.5.2 on NT is twice as slow as an unthreaded version; with the 2.0 changes, the difference is only 10%. These improvements were contributed by Yakov Markovitch.

Python 2.0’s source now uses only ANSI C prototypes, so compiling Python now requires an ANSI C compiler, and can no longer be done using a compiler that only supports K&R C.

Previously the Python virtual machine used 16-bit numbers in its bytecode, limiting the size of source files. In particular, this affected the maximum size of literal lists and dictionaries in Python source; occasionally people who are generating Python code would run into this limit. A patch by Charles G. Waldman raises the limit from 2^16 to 2^{32}.

Three new convenience functions intended for adding constants to a module’s dictionary at module initialization time were added: PyModule_AddObject(), PyModule_AddIntConstant(), and PyModule_AddStringConstant(). Each of these functions takes a module object, a null-terminated C string containing the name to be added, and a third argument for the value to be assigned to the name. This third argument is, respectively, a Python object, a C long, or a C string.

A wrapper API was added for Unix-style signal handlers. PyOS_getsig() gets a signal handler and PyOS_setsig() will set a new handler.

Distutils：使模块易于安装

Before Python 2.0, installing modules was a tedious affair – there was no way to figure out automatically where Python is installed, or what compiler options to use for extension modules. Software authors had to go through an arduous ritual of editing Makefiles and configuration files, which only really work on Unix and leave Windows and MacOS unsupported. Python users faced wildly differing installation instructions which varied between different extension packages, which made administering a Python installation something of a chore.

The SIG for distribution utilities, shepherded by Greg Ward, has created the Distutils, a system to make package installation much easier. They form the distutils package, a new part of Python’s standard library. In the best case, installing a Python module from source will require the same steps: first you simply mean unpack the tarball or zip archive, and the run “python setup.py install”. The platform will be automatically detected, the compiler will be recognized, C extension modules will be compiled, and the distribution installed into the proper directory. Optional command-line arguments provide more control over the installation process, the distutils package offers many places to override defaults – separating the build from the install, building or installing in non-default directories, and more.

In order to use the Distutils, you need to write a setup.py script. For the simple case, when the software contains only .py files, a minimal setup.py can be just a few lines long:

from distutils.core import setup
setup (name = "foo", version = "1.0",
       py_modules = ["module1", "module2"])

The setup.py file isn’t much more complicated if the software consists of a few packages:

from distutils.core import setup
setup (name = "foo", version = "1.0",
       packages = ["package", "package.subpackage"])

A C extension can be the most complicated case; here’s an example taken from the PyXML package:

from distutils.core import setup, Extension

expat_extension = Extension('xml.parsers.pyexpat',
     define_macros = [('XML_NS', None)],
     include_dirs = [ 'extensions/expat/xmltok',
                      'extensions/expat/xmlparse' ],
     sources = [ 'extensions/pyexpat.c',
                 'extensions/expat/xmltok/xmltok.c',
                 'extensions/expat/xmltok/xmlrole.c', ]
       )
setup (name = "PyXML", version = "0.5.4",
       ext_modules =[ expat_extension ] )

The Distutils can also take care of creating source and binary distributions. The “sdist” command, run by “python setup.py sdist’, builds a source distribution such as foo-1.0.tar.gz. Adding new commands isn’t difficult, “bdist_rpm” and “bdist_wininst” commands have already been contributed to create an RPM distribution and a Windows installer for the software, respectively. Commands to create other distribution formats such as Debian packages and Solaris .pkg files are in various stages of development.

All this is documented in a new manual, Distributing Python Modules, that joins the basic set of Python documentation.

XML 模块

Python 1.5.2 included a simple XML parser in the form of the xmllib module, contributed by Sjoerd Mullender. Since 1.5.2’s release, two different interfaces for processing XML have become common: SAX2 (version 2 of the Simple API for XML) provides an event-driven interface with some similarities to xmllib, and the DOM (Document Object Model) provides a tree-based interface, transforming an XML document into a tree of nodes that can be traversed and modified. Python 2.0 includes a SAX2 interface and a stripped-down DOM interface as part of the xml package. Here we will give a brief overview of these new interfaces; consult the Python documentation or the source code for complete details. The Python XML SIG is also working on improved documentation.

SAX2 Support

SAX defines an event-driven interface for parsing XML. To use SAX, you must write a SAX handler class. Handler classes inherit from various classes provided by SAX, and override various methods that will then be called by the XML parser. For example, the startElement() and endElement() methods are called for every starting and end tag encountered by the parser, the characters() method is called for every chunk of character data, and so forth.

The advantage of the event-driven approach is that the whole document doesn’t have to be resident in memory at any one time, which matters if you are processing really huge documents. However, writing the SAX handler class can get very complicated if you’re trying to modify the document structure in some elaborate way.

For example, this little example program defines a handler that prints a message for every starting and ending tag, and then parses the file hamlet.xml using it:

from xml import sax

class SimpleHandler(sax.ContentHandler):
    def startElement(self, name, attrs):
        print 'Start of element:', name, attrs.keys()

    def endElement(self, name):
        print 'End of element:', name

# Create a parser object
parser = sax.make_parser()

# Tell it what handler to use
handler = SimpleHandler()
parser.setContentHandler( handler )

# Parse a file!
parser.parse( 'hamlet.xml' )

For more information, consult the Python documentation, or the XML HOWTO at http://pyxml.sourceforge.net/topics/howto/xml-howto.html.

DOM Support

The Document Object Model is a tree-based representation for an XML document. A top-level Document instance is the root of the tree, and has a single child which is the top-level Element instance. This Element has children nodes representing character data and any sub-elements, which may have further children of their own, and so forth. Using the DOM you can traverse the resulting tree any way you like, access element and attribute values, insert and delete nodes, and convert the tree back into XML.

The DOM is useful for modifying XML documents, because you can create a DOM tree, modify it by adding new nodes or rearranging subtrees, and then produce a new XML document as output. You can also construct a DOM tree manually and convert it to XML, which can be a more flexible way of producing XML output than simply writing <tag1>…</tag1> to a file.

The DOM implementation included with Python lives in the xml.dom.minidom module. It’s a lightweight implementation of the Level 1 DOM with support for XML namespaces. The parse() and parseString() convenience functions are provided for generating a DOM tree:

from xml.dom import minidom
doc = minidom.parse('hamlet.xml')

doc is a Document instance. Document, like all the other DOM classes such as Element and Text, is a subclass of the Node base class. All the nodes in a DOM tree therefore support certain common methods, such as toxml() which returns a string containing the XML representation of the node and its children. Each class also has special methods of its own; for example, Element and Document instances have a method to find all child elements with a given tag name. Continuing from the previous 2-line example:

perslist = doc.getElementsByTagName( 'PERSONA' )
print perslist[0].toxml()
print perslist[1].toxml()

For the Hamlet XML file, the above few lines output:

<PERSONA>CLAUDIUS, king of Denmark. </PERSONA>
<PERSONA>HAMLET, son to the late, and nephew to the present king.</PERSONA>

The root element of the document is available as doc.documentElement, and its children can be easily modified by deleting, adding, or removing nodes:

root = doc.documentElement

# Remove the first child
root.removeChild( root.childNodes[0] )

# Move the new first child to the end
root.appendChild( root.childNodes[0] )

# Insert the new first child (originally,
# the third child) before the 20th child.
root.insertBefore( root.childNodes[0], root.childNodes[20] )

Again, I will refer you to the Python documentation for a complete listing of the different Node classes and their various methods.

Relationship to PyXML

The XML Special Interest Group has been working on XML-related Python code for a while. Its code distribution, called PyXML, is available from the SIG’s Web pages at https://www.python.org/community/sigs/current/xml-sig. The PyXML distribution also used the package name xml. If you’ve written programs that used PyXML, you’re probably wondering about its compatibility with the 2.0 xml package.

The answer is that Python 2.0’s xml package isn’t compatible with PyXML, but can be made compatible by installing a recent version PyXML. Many applications can get by with the XML support that is included with Python 2.0, but more complicated applications will require that the full PyXML package will be installed. When installed, PyXML versions 0.6.0 or greater will replace the xml package shipped with Python, and will be a strict superset of the standard package, adding a bunch of additional features. Some of the additional features in PyXML include:

4DOM, a full DOM implementation from FourThought, Inc.

The xmlproc validating parser, written by Lars Marius Garshol.

The sgmlop parser accelerator module, written by Fredrik Lundh.

模块更改

Lots of improvements and bugfixes were made to Python’s extensive standard library; some of the affected modules include readline, ConfigParser, cgi, calendar, posix, readline, xmllib, aifc, chunk, wave, random, shelve, and nntplib. Consult the CVS logs for the exact patch-by-patch details.

Brian Gallew contributed OpenSSL support for the socket module. OpenSSL is an implementation of the Secure Socket Layer, which encrypts the data being sent over a socket. When compiling Python, you can edit Modules/Setup to include SSL support, which adds an additional function to the socket module: socket.ssl(socket, keyfile, certfile), which takes a socket object and returns an SSL socket. The httplib and urllib modules were also changed to support https:// URLs, though no one has implemented FTP or SMTP over SSL.

The httplib module has been rewritten by Greg Stein to support HTTP/1.1. Backward compatibility with the 1.5 version of httplib is provided, though using HTTP/1.1 features such as pipelining will require rewriting code to use a different set of interfaces.

The Tkinter module now supports Tcl/Tk version 8.1, 8.2, or 8.3, and support for the older 7.x versions has been dropped. The Tkinter module now supports displaying Unicode strings in Tk widgets. Also, Fredrik Lundh contributed an optimization which makes operations like create_line and create_polygon much faster, especially when using lots of coordinates.

The curses module has been greatly extended, starting from Oliver Andrich’s enhanced version, to provide many additional functions from ncurses and SYSV curses, such as colour, alternative character set support, pads, and mouse support. This means the module is no longer compatible with operating systems that only have BSD curses, but there don’t seem to be any currently maintained OSes that fall into this category.

As mentioned in the earlier discussion of 2.0’s Unicode support, the underlying implementation of the regular expressions provided by the re module has been changed. SRE, a new regular expression engine written by Fredrik Lundh and partially funded by Hewlett Packard, supports matching against both 8-bit strings and Unicode strings.

新增模块

A number of new modules were added. We’ll simply list them with brief descriptions; consult the 2.0 documentation for the details of a particular module.

atexit: For registering functions to be called before the Python interpreter exits. Code that currently sets sys.exitfunc directly should be changed to use the atexit module instead, importing atexit and calling atexit.register() with the function to be called on exit. (Contributed by Skip Montanaro.)

codecs, encodings, unicodedata: Added as part of the new Unicode support.

filecmp: Supersedes the old cmp, cmpcache and dircmp modules, which have now become deprecated. (Contributed by Gordon MacMillan and Moshe Zadka.)

gettext: This module provides internationalization (I18N) and localization (L10N) support for Python programs by providing an interface to the GNU gettext message catalog library. (Integrated by Barry Warsaw, from separate contributions by Martin von Löwis, Peter Funk, and James Henstridge.)

linuxaudiodev: Support for the /dev/audio device on Linux, a twin to the existing sunaudiodev module. (Contributed by Peter Bosch, with fixes by Jeremy Hylton.)

mmap: An interface to memory-mapped files on both Windows and Unix. A file’s contents can be mapped directly into memory, at which point it behaves like a mutable string, so its contents can be read and modified. They can even be passed to functions that expect ordinary strings, such as the re module. (Contributed by Sam Rushing, with some extensions by A.M. Kuchling.)

pyexpat: An interface to the Expat XML parser. (Contributed by Paul Prescod.)

robotparser: Parse a robots.txt file, which is used for writing Web spiders that politely avoid certain areas of a Web site. The parser accepts the contents of a robots.txt file, builds a set of rules from it, and can then answer questions about the fetchability of a given URL. (Contributed by Skip Montanaro.)

tabnanny: A module/script to check Python source code for ambiguous indentation. (Contributed by Tim Peters.)

UserString: A base class useful for deriving objects that behave like strings.

webbrowser: A module that provides a platform independent way to launch a web browser on a specific URL. For each platform, various browsers are tried in a specific order. The user can alter which browser is launched by setting the BROWSER environment variable. (Originally inspired by Eric S. Raymond’s patch to urllib which added similar functionality, but the final module comes from code originally implemented by Fred Drake as Tools/idle/BrowserControl.py, and adapted for the standard library by Fred.)

_winreg: An interface to the Windows registry. _winreg is an adaptation of functions that have been part of PythonWin since 1995, but has now been added to the core distribution, and enhanced to support Unicode. _winreg was written by Bill Tutt and Mark Hammond.

zipfile: A module for reading and writing ZIP-format archives. These are archives produced by PKZIP on DOS/Windows or zip on Unix, not to be confused with gzip-format files (which are supported by the gzip module) (Contributed by James C. Ahlstrom.)

imputil: A module that provides a simpler way for writing customized import hooks, in comparison to the existing ihooks module. (Implemented by Greg Stein, with much discussion on python-dev along the way.)

IDLE 改进

IDLE is the official Python cross-platform IDE, written using Tkinter. Python 2.0 includes IDLE 0.6, which adds a number of new features and improvements. A partial list:

UI improvements and optimizations, especially in the area of syntax highlighting and auto-indentation.

The class browser now shows more information, such as the top level functions in a module.

Tab width is now a user settable option. When opening an existing Python file, IDLE automatically detects the indentation conventions, and adapts.

There is now support for calling browsers on various platforms, used to open the Python documentation in a browser.

IDLE now has a command line, which is largely similar to the vanilla Python interpreter.

Call tips were added in many places.

IDLE can now be installed as a package.

In the editor window, there is now a line/column bar at the bottom.

Three new keystroke commands: Check module (Alt-F5), Import module (F5) and Run script (Ctrl-F5).

删除和弃用的模块

A few modules have been dropped because they’re obsolete, or because there are now better ways to do the same thing. The stdwin module is gone; it was for a platform-independent windowing toolkit that’s no longer developed.

A number of modules have been moved to the lib-old subdirectory: cmp, cmpcache, dircmp, dump, find, grep, packmail, poly, util, whatsound, zmod. If you have code which relies on a module that’s been moved to lib-old, you can simply add that directory to sys.path to get them back, but you’re encouraged to update any code that uses these modules.

致谢

作者要感谢以下人士对本文的各种草稿提出建议： David Bolen, Mark Hammond, Gregg Hauser, Jeremy Hylton, Fredrik Lundh, Detlef Lannert, Aahz Maruch, Skip Montanaro, Vladimir Marangozov, Tobias Polzin, Guido van Rossum, Neil Schemenauer, and Russ Schmidt.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

更新日志

Python 3.6.13 final

Release date: 2021-02-15

安全

bpo-42967 [https://bugs.python.org/issue42967]: Fix web cache poisoning vulnerability by defaulting the query args separator to &, and allowing the user to choose a custom separator.

bpo-42938 [https://bugs.python.org/issue42938]: 在计算 ctypes.c_double 和 ctypes.c_longdouble 值的表示形式时避免静态缓冲区。

bpo-42103 [https://bugs.python.org/issue42103]: Prevented potential DoS attack via CPU and RAM exhaustion when processing malformed Apple Property List files in binary format.

bpo-42051 [https://bugs.python.org/issue42051]：模块 plistlib 不再接受 XML 属性列表文件中的实体声明（避免引发 XML 脆弱性）。此更改不应该影响用户使用，因为实体声明在常规属性列表文件中不被使用。

bpo-40791 [https://bugs.python.org/issue40791]: Add volatile to the accumulator variable in hmac.compare_digest, making constant-time-defeating optimizations less likely.

核心与内置

bpo-35560 [https://bugs.python.org/issue35560]: 修复了在调试版中使用 “n” 格式进行零留空和小宽度的浮点格式化时 format() 内的一个断言错误。 发布版未受影响。 补丁作者 Karthikeyan Singaravelan。

库

bpo-42103 [https://bugs.python.org/issue42103]: InvalidFileException and RecursionError are now the only errors caused by loading malformed binary Plist file (previously ValueError and TypeError could be raised in some specific cases).

测试

bpo-42794 [https://bugs.python.org/issue42794]: Update test_nntplib to use offical group name of news.aioe.org for testing. Patch by Dong-hee Na.

bpo-41944 [https://bugs.python.org/issue41944]: Tests for CJK codecs no longer call eval() on content received via HTTP.

Python 3.6.12 final

发布日期：2020-08-15

安全

bpo-29778 [https://bugs.python.org/issue29778]: Ensure python3.dll is loaded from correct locations when Python is embedded (CVE-2020-15523).

bpo-41004 [https://bugs.python.org/issue41004]: CVE-2020-14422: The __hash__() methods of ipaddress.IPv4Interface and ipaddress.IPv6Interface incorrectly generated constant hash values of 32 and 128 respectively. This resulted in always causing hash collisions. The fix uses hash() to generate hash values for the tuple of (address, mask length, network address).

bpo-39603 [https://bugs.python.org/issue39603]: Prevent http header injection by rejecting control characters in http.client.putrequest(…).

库

bpo-41288 [https://bugs.python.org/issue41288]: Unpickling invalid NEWOBJ_EX opcode with the C implementation raises now UnpicklingError instead of crashing.

bpo-39017 [https://bugs.python.org/issue39017]: Avoid infinite loop when reading specially crafted TAR files using the tarfile module (CVE-2019-20907).

Python 3.6.11 final

发布日期: 2020-06-27

There were no new changes in version 3.6.11.

Python 3.6.11 release candidate 1

Release date: 2020-06-17

安全

bpo-39073 [https://bugs.python.org/issue39073]: Disallow CR or LF in email.headerregistry.Address arguments to guard against header injection attacks.

bpo-38576 [https://bugs.python.org/issue38576]: Disallow control characters in hostnames in http.client, addressing CVE-2019-18348. Such potentially malicious header injection URLs now cause a InvalidURL to be raised.

bpo-39503 [https://bugs.python.org/issue39503]: CVE-2020-8492: The AbstractBasicAuthHandler class of the urllib.request module uses an inefficient regular expression which can be exploited by an attacker to cause a denial of service. Fix the regex to prevent the catastrophic backtracking. Vulnerability reported by Ben Caller and Matt Schwager.

bpo-39401 [https://bugs.python.org/issue39401]: Avoid unsafe load of api-ms-win-core-path-l1-1-0.dll at startup on Windows 7.

核心与内置

bpo-39510 [https://bugs.python.org/issue39510]: Fix segfault in readinto() method on closed BufferedReader.

bpo-39421 [https://bugs.python.org/issue39421]: Fix possible crashes when operating with the functions in the heapq module and custom comparison operators.

库

bpo-39503 [https://bugs.python.org/issue39503]: AbstractBasicAuthHandler of urllib.request now parses all WWW-Authenticate HTTP headers and accepts multiple challenges per header: use the realm of the first Basic challenge.

Python 3.6.10 final

发布日期: 2019-12-18

macOS

bpo-38295 [https://bugs.python.org/issue38295]: Prevent failure of test_relative_path in test_py_compile on macOS Catalina.

Python 3.6.10 release candidate 1

Release date: 2019-12-11

安全

bpo-38945 [https://bugs.python.org/issue38945]: Newline characters have been escaped when performing uu encoding to prevent them from overflowing into to content section of the encoded file. This prevents malicious or accidental modification of data during the decoding process.

bpo-37228 [https://bugs.python.org/issue37228]: Due to significant security concerns, the reuse_address parameter of asyncio.loop.create_datagram_endpoint() is no longer supported. This is because of the behavior of SO_REUSEADDR in UDP. For more details, see the documentation for loop.create_datagram_endpoint(). (Contributed by Kyle Stanley, Antoine Pitrou, and Yury Selivanov in bpo-37228 [https://bugs.python.org/issue37228].)

bpo-38804 [https://bugs.python.org/issue38804]: Fixes a ReDoS vulnerability in http.cookiejar. Patch by Ben Caller.

bpo-38243 [https://bugs.python.org/issue38243]: 在将文档页渲染为 HTML 时会对 xmlrpc.server.DocXMLRPCServer 的服务器标题进行转义。 （由 Dong-hee Na 在 bpo-38243 [https://bugs.python.org/issue38243] 中贡献。）

bpo-38174 [https://bugs.python.org/issue38174]: 将厂商化的 expat 库版本更新至 2.2.8，这解决了 CVE-2019-15903。

bpo-37461 [https://bugs.python.org/issue37461]: Fix an infinite loop when parsing specially crafted email headers. Patch by Abhilash Raj.

bpo-34155 [https://bugs.python.org/issue34155]: Fix parsing of invalid email addresses with more than one @ (e.g. a@b@c.com.) to not return the part before 2nd @ as valid email address. Patch by maxking & jpic.

库

bpo-38216 [https://bugs.python.org/issue38216]: Allow the rare code that wants to send invalid http requests from the http.client library a way to do so. The fixes for bpo-30458 [https://bugs.python.org/issue30458] led to breakage for some projects that were relying on this ability to test their own behavior in the face of bad requests.

bpo-36564 [https://bugs.python.org/issue36564]: Fix infinite loop in email header folding logic that would be triggered when an email policy’s max_line_length is not long enough to include the required markup and any values in the message. Patch by Paul Ganssle

Python 3.6.9 final

Release date: 2019-07-02

库

bpo-37437 [https://bugs.python.org/issue37437]: Update vendorized expat version to 2.2.7.

macOS

bpo-34602 [https://bugs.python.org/issue34602]: Avoid test suite failures on macOS by no longer calling resource.setrlimit to increase the process stack size limit at runtime. The runtime change is no longer needed since the interpreter is being built with a larger default stack size.

Python 3.6.9 release candidate 1

Release date: 2019-06-18

安全

bpo-35907 [https://bugs.python.org/issue35907]: CVE-2019-9948: Avoid file reading by disallowing local-file:// and local_file:// URL schemes in URLopener().open() and URLopener().retrieve() of urllib.request.

bpo-36742 [https://bugs.python.org/issue36742]: 于函数 urlsplit() 中修复了对于预规格化字符的不当处理过程。

bpo-30458 [https://bugs.python.org/issue30458]: Address CVE-2019-9740 by disallowing URL paths with embedded whitespace or control characters through into the underlying http client request. Such potentially malicious header injection URLs now cause an http.client.InvalidURL exception to be raised.

bpo-36216 [https://bugs.python.org/issue36216]: Changes urlsplit() to raise ValueError when the URL contains characters that decompose under IDNA encoding (NFKC-normalization) into characters that affect how the URL is parsed.

bpo-33529 [https://bugs.python.org/issue33529]: Prevent fold function used in email header encoding from entering infinite loop when there are too many non-ASCII characters in a header.

bpo-35746 [https://bugs.python.org/issue35746]: [CVE-2019-5010] Fix a NULL pointer deref in ssl module. The cert parser did not handle CRL distribution points with empty DP or URI correctly. A malicious or buggy certificate can result into segfault. Vulnerability (TALOS-2018-0758) reported by Colin Read and Nicolas Edet of Cisco.

bpo-35121 [https://bugs.python.org/issue35121]: Don’t send cookies of domain A without Domain attribute to domain B when domain A is a suffix match of domain B while using a cookiejar with http.cookiejar.DefaultCookiePolicy policy. Patch by Karthikeyan Singaravelan.

库

bpo-35643 [https://bugs.python.org/issue35643]: 修复了 Modules/_sha3/cleanup.py 中的一个 SyntaxWarning: invalid escape sequence。 补丁作者 Mickaël Schoentgen。

bpo-35121 [https://bugs.python.org/issue35121]: Don’t set cookie for a request when the request path is a prefix match of the cookie’s path attribute but doesn’t end with “/”. Patch by Karthikeyan Singaravelan.

文档

bpo-35605 [https://bugs.python.org/issue35605]: Fix documentation build for sphinx<1.6. Patch by Anthony Sottile.

bpo-35564 [https://bugs.python.org/issue35564]: Explicitly set master_doc variable in conf.py for compliance with Sphinx 2.0

测试

bpo-36816 [https://bugs.python.org/issue36816]: Update Lib/test/selfsigned_pythontestdotnet.pem to match self-signed.pythontest.net’s new TLS certificate.

bpo-35925 [https://bugs.python.org/issue35925]: Skip specific nntplib and ssl networking tests when they would otherwise fail due to a modern OS or distro with a default OpenSSL policy of rejecting connections to servers with weak certificates or disabling TLS below TLSv1.2.

bpo-27313 [https://bugs.python.org/issue27313]: Avoid test_ttk_guionly ComboboxTest failure with macOS Cocoa Tk.

bpo-32947 [https://bugs.python.org/issue32947]: test_ssl fixes for TLS 1.3 and OpenSSL 1.1.1.

macOS

bpo-34602 [https://bugs.python.org/issue34602]: Avoid failures setting macOS stack resource limit with resource.setrlimit. This reverts an earlier fix for bpo-18075 [https://bugs.python.org/issue18075] which forced a non-default stack size when building the interpreter executable on macOS.

Python 3.6.8 final

发布日期: 2018-12-23

库

bpo-31715 [https://bugs.python.org/issue31715]: Associate .mjs file extension with application/javascript MIME Type.

构建

bpo-35499 [https://bugs.python.org/issue35499]: make profile-opt no longer replaces CFLAGS_NODIST with CFLAGS. It now adds profile-guided optimization (PGO) flags to CFLAGS_NODIST: existing CFLAGS_NODIST flags are kept.

bpo-35257 [https://bugs.python.org/issue35257]: Avoid leaking the linker flags from Link Time Optimizations (LTO) into distutils when compiling C extensions.

C API

bpo-35259 [https://bugs.python.org/issue35259]: 基于 Py_LIMITED_API 有条件地声明 Py_FinalizeEx() (3.6 中新增)。 补丁作者 Arthur Neufeld。

Python 3.6.8 release candidate 1

发布日期: 2018-12-11

安全

bpo-34812 [https://bugs.python.org/issue34812]: The -I command line option (run Python in isolated mode) is now also copied by the multiprocessing and distutils modules when spawning child processes. Previously, only -E and -s options (enabled by -I) were copied.

bpo-34791 [https://bugs.python.org/issue34791]: The xml.sax and xml.dom.domreg no longer use environment variables to override parser implementations when sys.flags.ignore_environment is set by -E or -I arguments.

核心与内置

bpo-35444 [https://bugs.python.org/issue35444]: Fixed error handling in pickling methods when fail to look up builtin “getattr”.

bpo-35436 [https://bugs.python.org/issue35436]: Fix various issues with memory allocation error handling. Patch by Zackery Spytz.

bpo-35357 [https://bugs.python.org/issue35357]: Internal attributes’ names of unittest.mock._Call and unittest.mock.MagicProxy (name, parent & from_kall) are now prefixed with _mock_ in order to prevent clashes with widely used object attributes. Fixed minor typo in test function name.

bpo-35372 [https://bugs.python.org/issue35372]: Fixed the code page decoder for input longer than 2 GiB containing undecodable bytes.

bpo-33954 [https://bugs.python.org/issue33954]: For str.format(), float.__format__() and complex.__format__() methods for non-ASCII decimal point when using the “n” formatter.

bpo-35214 [https://bugs.python.org/issue35214]: Fixed an out of bounds memory access when parsing a truncated unicode escape sequence at the end of a string such as '\N'. It would read one byte beyond the end of the memory allocation.

bpo-35214 [https://bugs.python.org/issue35214]: The interpreter and extension modules have had annotations added so that they work properly under clang’s Memory Sanitizer. A new configure flag –with-memory-sanitizer has been added to make test builds of this nature easier to perform.

bpo-35193 [https://bugs.python.org/issue35193]: Fix an off by one error in the bytecode peephole optimizer where it could read bytes beyond the end of bounds of an array when removing unreachable code. This bug was present in every release of Python 3.6 until now.

bpo-29341 [https://bugs.python.org/issue29341]: Clarify in the docstrings of os methods that path-like objects are also accepted as input parameters.

bpo-35050 [https://bugs.python.org/issue35050]: socket: Fix off-by-one bug in length check for AF_ALG name and type.

bpo-34974 [https://bugs.python.org/issue34974]: bytes and bytearray constructors no longer convert unexpected exceptions (e.g. MemoryError and KeyboardInterrupt) to TypeError.

bpo-34973 [https://bugs.python.org/issue34973]: Fixed crash in bytes() when the list argument is mutated while it is iterated.

bpo-34824 [https://bugs.python.org/issue34824]: Fix a possible null pointer dereference in Modules/_ssl.c. Patch by Zackery Spytz.

bpo-1621 [https://bugs.python.org/issue1621]: Do not assume signed integer overflow behavior (C undefined behavior) when performing set hash table resizing.

库

bpo-35052 [https://bugs.python.org/issue35052]: Fix xml.dom.minidom cloneNode() on a document with an entity: pass the correct arguments to the user data handler of an entity.

bpo-35330 [https://bugs.python.org/issue35330]: When a Mock instance was used to wrap an object, if side_effect is used in one of the mocks of it methods, don’t call the original implementation and return the result of using the side effect the same way that it is done with return_value.

bpo-34172 [https://bugs.python.org/issue34172]: Revert the fix for this issue previously released in 3.6.7 pending further investigation: Fix a reference issue inside multiprocessing.Pool that caused the pool to remain alive if it was deleted without being closed or terminated explicitly.

bpo-10496 [https://bugs.python.org/issue10496]: posixpath.expanduser() now returns the input path unchanged if the HOME environment variable is not set and the current user has no home directory (if the current user identifier doesn’t exist in the password database). This change fix the site module if the current user doesn’t exist in the password database (if the user has no home directory).

bpo-35310 [https://bugs.python.org/issue35310]: Fix a bug in select.select() where, in some cases, the file descriptor sequences were returned unmodified after a signal interruption, even though the file descriptors might not be ready yet. select.select() will now always return empty lists if a timeout has occurred. Patch by Oran Avraham.

bpo-35380 [https://bugs.python.org/issue35380]: Enable TCP_NODELAY on Windows for proactor asyncio event loop.

bpo-35371 [https://bugs.python.org/issue35371]: Fixed possible crash in os.utime() on Windows when pass incorrect arguments.

bpo-27903 [https://bugs.python.org/issue27903]: Fix ResourceWarning in platform.dist() on SuSE and Caldera OpenLinux. Patch by Ville Skyttä.

bpo-28604 [https://bugs.python.org/issue28604]: locale.localeconv() now sets temporarily the LC_CTYPE locale to the LC_MONETARY locale if the two locales are different and monetary strings are non-ASCII. This temporary change affects other threads.

bpo-35277 [https://bugs.python.org/issue35277]: Update ensurepip to install pip 18.1 and setuptools 40.6.2.

bpo-35226 [https://bugs.python.org/issue35226]: Recursively check arguments when testing for equality of unittest.mock.call objects and add note that tracking of parameters used to create ancestors of mocks in mock_calls is not possible.

bpo-35189 [https://bugs.python.org/issue35189]: Modify the following fnctl function to retry if interrupted by a signal (EINTR): flock, lockf, fnctl

bpo-35062 [https://bugs.python.org/issue35062]: Fix incorrect parsing of _io.IncrementalNewlineDecoder’s translate argument.

bpo-35079 [https://bugs.python.org/issue35079]: Improve difflib.SequenceManager.get_matching_blocks doc by adding ‘non-overlapping’ and changing ‘!=’ to ‘<’.

bpo-35017 [https://bugs.python.org/issue35017]: socketserver.BaseServer.serve_forever() now exits immediately if it’s shutdown() method is called while it is polling for new events.

bpo-31047 [https://bugs.python.org/issue31047]: Fix ntpath.abspath regression where it didn’t remove a trailing separator on Windows. Patch by Tim Graham.

bpo-34794 [https://bugs.python.org/issue34794]: Fixed a leak in Tkinter when pass the Python wrapper around Tcl_Obj back to Tcl/Tk.

bpo-35008 [https://bugs.python.org/issue35008]: Fixed references leaks when call the __setstate__() method of xml.etree.ElementTree.Element in the C implementation for already initialized element.

bpo-23420 [https://bugs.python.org/issue23420]: Verify the value for the parameter ‘-s’ of the cProfile CLI. Patch by Robert Kuska

bpo-16965 [https://bugs.python.org/issue16965]: The 2to3 execfile fixer now opens the file with mode 'rb'. Patch by Zackery Spytz.

bpo-34966 [https://bugs.python.org/issue34966]: pydoc now supports aliases not only to methods defined in the end class, but also to inherited methods. The docstring is not duplicated for aliases.

bpo-34941 [https://bugs.python.org/issue34941]: Methods find(), findtext() and findall() of the Element class in the xml.etree.ElementTree module are now able to find children which are instances of Element subclasses.

bpo-34936 [https://bugs.python.org/issue34936]: Fix TclError in tkinter.Spinbox.selection_element(). Patch by Juliette Monsel.

bpo-34900 [https://bugs.python.org/issue34900]: Fixed unittest.TestCase.debug() when used to call test methods with subtests. Patch by Bruno Oliveira.

bpo-34866 [https://bugs.python.org/issue34866]: Adding max_num_fields to cgi.FieldStorage to make DOS attacks harder by limiting the number of MiniFieldStorage objects created by FieldStorage.

bpo-34738 [https://bugs.python.org/issue34738]: ZIP files created by distutils will now include entries for directories.

bpo-31177 [https://bugs.python.org/issue31177]: Fix bug that prevented using reset_mock on mock instances with deleted attributes

bpo-34604 [https://bugs.python.org/issue34604]: Fix possible mojibake in the error message of pwd.getpwnam and grp.getgrnam using string representation because of invisible characters or trailing whitespaces. Patch by William Grzybowski.

bpo-34574 [https://bugs.python.org/issue34574]: OrderedDict iterators are not exhausted during pickling anymore. Patch by Sergey Fedoseev.

bpo-34052 [https://bugs.python.org/issue34052]: sqlite3.Connection.create_aggregate(), sqlite3.Connection.create_function(), sqlite3.Connection.set_authorizer(), sqlite3.Connection.set_progress_handler() methods raises TypeError when unhashable objects are passed as callable. These methods now don’t pass such objects to SQLite API. Previous behavior could lead to segfaults. Patch by Sergey Fedoseev.

bpo-29877 [https://bugs.python.org/issue29877]: compileall: import ProcessPoolExecutor only when needed, preventing hangs on low resource platforms

bpo-22005 [https://bugs.python.org/issue22005]: Implemented unpickling instances of datetime, date and time pickled by Python 2. encoding='latin1' should be used for successful decoding.

文档

bpo-35089 [https://bugs.python.org/issue35089]: Remove mention of typing.io and typing.re. Their types should be imported from typing directly.

bpo-35038 [https://bugs.python.org/issue35038]: Fix the documentation about an unexisting f_restricted attribute in the frame object. Patch by Stéphane Wirtel

bpo-35035 [https://bugs.python.org/issue35035]: Rename documentation for email.utils to email.utils.rst.

bpo-34967 [https://bugs.python.org/issue34967]: Use app.add_object_type() instead of the deprecated Sphinx function app.description_unit()

bpo-33594 [https://bugs.python.org/issue33594]: Document getargspec, from_function and from_builtin as deprecated in their respective docstring, and include version since deprecation in DeprecationWarning message.

bpo-32613 [https://bugs.python.org/issue32613]: Update the faq/windows.html to use the py command from PEP 397 instead of python.

测试

bpo-33725 [https://bugs.python.org/issue33725]: test_multiprocessing_fork may crash on recent versions of macOS. Until the issue is resolved, skip the test on macOS.

bpo-35352 [https://bugs.python.org/issue35352]: Modify test_asyncio to use the certificate set from the test directory.

bpo-35317 [https://bugs.python.org/issue35317]: Fix mktime() overflow error in test_email: run test_localtime_daylight_true_dst_true() and test_localtime_daylight_false_dst_true() with a specific timezone.

bpo-21263 [https://bugs.python.org/issue21263]: After several reports that test_gdb does not work properly on macOS and since gdb is not shipped by default anymore, test_gdb is now skipped on macOS when LLVM Clang has been used to compile Python. Patch by Lysandros Nikolaou

bpo-34279 [https://bugs.python.org/issue34279]: regrtest issue a warning when no tests have been executed in a particular test file. Also, a new final result state is issued if no test have been executed across all test files. Patch by Pablo Galindo.

构建

bpo-35351 [https://bugs.python.org/issue35351]: When building Python with clang and LTO, LTO flags are no longer passed into CFLAGS to build third-party C extensions through distutils.

bpo-35139 [https://bugs.python.org/issue35139]: Fix a compiler error when statically linking pyexpat in Modules/Setup.

bpo-35011 [https://bugs.python.org/issue35011]: Restores the use of pyexpatns.h to isolate our embedded copy of the expat C library so that its symbols do not conflict at link or dynamic loading time with an embedding application or other extension modules with their own version of libexpat.

bpo-28015 [https://bugs.python.org/issue28015]: Have –with-lto works correctly with clang.

bpo-33015 [https://bugs.python.org/issue33015]: Fix an undefined behaviour in the pthread implementation of PyThread_start_new_thread(): add a function wrapper to always return NULL.

bpo-31625 [https://bugs.python.org/issue31625]: Stop using ranlib on static libraries. Instead, we assume ar supports the ‘s’ flag.

bpo-31354 [https://bugs.python.org/issue31354]: Allow –with-lto to be used on all builds, not just make profile-opt.

Windows

bpo-35401 [https://bugs.python.org/issue35401]: Updates Windows build to OpenSSL 1.0.2q

bpo-32890 [https://bugs.python.org/issue32890]: Fix usage of GetLastError() instead of errno in os.execve() and os.truncate().

macOS

bpo-15663 [https://bugs.python.org/issue15663]: The macOS 10.6+ installer now provides a private copy of Tcl/Tk 8.6, like the 10.9+ installer does.

bpo-35402 [https://bugs.python.org/issue35402]: Update macOS installer to use Tcl/Tk 8.6.9.1. [NOTE: This change was reverted for the released python.org 3.6.8 macOS installers due to regressions found in Tk 8.6.9.1. For now, the installers provide Tcl/Tk 8.6.8.]

bpo-35401 [https://bugs.python.org/issue35401]: Update macOS installer to use OpenSSL 1.0.2q.

bpo-35025 [https://bugs.python.org/issue35025]: Properly guard the use of the CLOCK_GETTIME et al. macros in timemodule on macOS.

bpo-24658 [https://bugs.python.org/issue24658]: On macOS, fix reading from and writing into a file with a size larger than 2 GiB.

IDLE

bpo-35213 [https://bugs.python.org/issue35213]: Where appropriate, use ‘macOS’ in idlelib.

bpo-34864 [https://bugs.python.org/issue34864]: On macOS, warn if the system preference “Prefer tabs when opening documents” is set to “Always”.

bpo-34864 [https://bugs.python.org/issue34864]: Document two IDLE on MacOS issues. The System Preferences Dock “prefer tabs always” setting disables some IDLE features. Menus are a bit different than as described for Windows and Linux.

bpo-35202 [https://bugs.python.org/issue35202]: Remove unused imports from lib/idlelib

bpo-33000 [https://bugs.python.org/issue33000]: Document that IDLE’s shell has no line limit. A program that runs indefinitely can overfill memory.

bpo-23220 [https://bugs.python.org/issue23220]: Explain how IDLE’s Shell displays output.

bpo-35099 [https://bugs.python.org/issue35099]: Improve the doc about IDLE running user code. The section is renamed from “IDLE – console differences” is renamed “Running user code”. It mostly covers the implications of using custom sys.stdxxx objects.

bpo-35097 [https://bugs.python.org/issue35097]: Add IDLE doc subsection explaining editor windows. Topics include opening, title and status bar, .py* extension, and running.

bpo-35093 [https://bugs.python.org/issue35093]: Document the IDLE document viewer in the IDLE doc. Add a paragraph in “Help and preferences”, “Help sources” subsection.

bpo-35088 [https://bugs.python.org/issue35088]: Update idlelib.help.copy_string docstring. We now use git and backporting instead of hg and forward merging.

bpo-35087 [https://bugs.python.org/issue35087]: Update idlelib help files for the current doc build. The main change is the elimination of chapter-section numbers.

工具/示例

bpo-34989 [https://bugs.python.org/issue34989]: python-gdb.py now handles errors on computing the line number of a Python frame.

Python 3.6.7 final

发布日期: 2018-10-20

There were no new changes in version 3.6.7.

Python 3.6.7 release candidate 2

发布日期: 2018-10-13

核心与内置

bpo-34879 [https://bugs.python.org/issue34879]: Fix a possible null pointer dereference in bytesobject.c. Patch by Zackery Spytz.

bpo-34320 [https://bugs.python.org/issue34320]: 修复问题——dict(od) 不会复制 OrderedDict 的迭代顺序。

库

bpo-34769 [https://bugs.python.org/issue34769]: Fix for async generators not finalizing when event loop is in debug mode and garbage collector runs in another thread.

bpo-34922 [https://bugs.python.org/issue34922]: Fixed integer overflow in the digest() and hexdigest() methods for the SHAKE algorithm in the hashlib module.

bpo-34871 [https://bugs.python.org/issue34871]: Fix inspect module polluted sys.modules when parsing __text_signature__ of callable.

bpo-34872 [https://bugs.python.org/issue34872]: Fix self-cancellation in C implementation of asyncio.Task

bpo-34819 [https://bugs.python.org/issue34819]: 使用一个单一时钟计算 Executor.map() 和 as_completed() 中的超时，以防止当系统时钟被修改时因偏离导致的超时。

bpo-34521 [https://bugs.python.org/issue34521]: Use socket.CMSG_SPACE() to calculate ancillary data size instead of socket.CMSG_LEN() in multiprocessing.reduction.recvfds() as RFC 3542 [https://tools.ietf.org/html/rfc3542.html] requires the use of the former for portable applications.

bpo-34282 [https://bugs.python.org/issue34282]: Fix enum members getting shadowed by parent attributes.

bpo-34172 [https://bugs.python.org/issue34172]: Fix a reference issue inside multiprocessing.Pool that caused the pool to remain alive if it was deleted without being closed or terminated explicitly.

bpo-33729 [https://bugs.python.org/issue33729]: Fixed issues with arguments parsing in hashlib.

文档

bpo-32174 [https://bugs.python.org/issue32174]: chm document displays non-ASCII charaters properly on some MBCS Windows systems.

测试

bpo-32962 [https://bugs.python.org/issue32962]: Fixed test_gdb when Python is compiled with flags -mcet -fcf-protection -O0.

macOS

bpo-34370 [https://bugs.python.org/issue34370]: Revert to using the released Tk 8.6.8 with macOS installers instead of the Tk 8.6.x development snapshot used with 3.7.1rc1 and 3.6.7rc1. The snapshot introduced at least one significant regression (bpo-34927 [https://bugs.python.org/issue34927]).

C API

bpo-34910 [https://bugs.python.org/issue34910]: Ensure that PyObject_Print() always returns -1 on error. Patch by Zackery Spytz.

Python 3.6.7 release candidate 1

发布日期: 2018-09-26

安全

bpo-17239 [https://bugs.python.org/issue17239]: The xml.sax and xml.dom.minidom parsers no longer processes external entities by default. External DTD and ENTITY declarations no longer load files or create network connections.

bpo-34623 [https://bugs.python.org/issue34623]: CVE-2018-14647: The C accelerated _elementtree module now initializes hash randomization salt from _Py_HashSecret instead of libexpat’s default CSPRNG.

bpo-34405 [https://bugs.python.org/issue34405]: Updated to OpenSSL 1.0.2p for Windows builds.

bpo-33871 [https://bugs.python.org/issue33871]: 修复了使用 os.sendfile() 在macOS上发送部分文件的错误。使用 trailers 参数可能导致从输入文件发送的字节数多于指定的数量。

bpo-32533 [https://bugs.python.org/issue32533]: Fixed thread-safety of error handling in _ssl.

核心与内置

bpo-34735 [https://bugs.python.org/issue34735]: Fix a memory leak in Modules/timemodule.c. Patch by Zackery Spytz.

bpo-34588 [https://bugs.python.org/issue34588]: Fix an off-by-one in the recursive call pruning feature of traceback formatting.

bpo-34527 [https://bugs.python.org/issue34527]: On FreeBSD, Py_DecodeLocale() and Py_EncodeLocale() now also forces the ASCII encoding if the LC_CTYPE locale is “POSIX”, not only if the LC_CTYPE locale is “C”.

bpo-34400 [https://bugs.python.org/issue34400]: Fix undefined behavior in parsetok.c. Patch by Zackery Spytz.

bpo-34377 [https://bugs.python.org/issue34377]: 更新valgrind的抑制列表，使用 _PyObject_Free/_PyObject_Realloc 代替 PyObject_Free/PyObject_Realloc 。

bpo-24618 [https://bugs.python.org/issue24618]: 修复了当创建代码对象时使用的变量名元组太小或参数太多导致读取无效内存的问题。

bpo-34068 [https://bugs.python.org/issue34068]: 在调用 io.IOBase.close() 时确保 closed 属性未设置有效的异常。 由Zackery Spytz和Serhiy Storchaka修复。

bpo-34080 [https://bugs.python.org/issue34080]: 修复了编译器在标记化过程中引发一些不常见错误时内存泄漏的问题。

bpo-34066 [https://bugs.python.org/issue34066]: 调用 open() 和进入 with open() 的 with 语句块内时禁止使用 Ctrl-C 中断。

bpo-33956 [https://bugs.python.org/issue33956]: Update vendored Expat library copy to version 2.2.5.

bpo-24596 [https://bugs.python.org/issue24596]: 执行 PyErr_Print() 之前，在 PyRun_SimpleFileExFlags() 内减少模块对象的引用。 由Zackery Spytz修复。

bpo-33451 [https://bugs.python.org/issue33451]: 执行 PyEval_EvalCode() 前先关闭直接执行的pyc文件。

bpo-33312 [https://bugs.python.org/issue33312]: Fixed clang ubsan (undefined behavior sanitizer) warnings in dictobject.c by adjusting how the internal struct _dictkeysobject shared keys structure is declared.

bpo-25750 [https://bugs.python.org/issue25750]: Fix rare Python crash due to bad refcounting in type_getattro() if a descriptor deletes itself from the class. Patch by Jeroen Demeyer.

bpo-25862 [https://bugs.python.org/issue25862]: 修复 io.TextIOWrapper 的 tell() 方法内的错误断言。

库

bpo-34670 [https://bugs.python.org/issue34670]: Add SSLContext.post_handshake_auth and SSLSocket.verify_client_post_handshake for TLS 1.3’s post handshake authentication feature.

bpo-34652 [https://bugs.python.org/issue34652]: Ensure os.lchmod() is never defined on Linux.

bpo-34625 [https://bugs.python.org/issue34625]: Update vendorized expat library version to 2.2.6.

bpo-32270 [https://bugs.python.org/issue32270]: The subprocess module no longer mistakenly closes redirected fds even when they were in pass_fds when outside of the default {0, 1, 2} set.

bpo-34610 [https://bugs.python.org/issue34610]: Fixed iterator of multiprocessing.managers.DictProxy.

bpo-34421 [https://bugs.python.org/issue34421]: Fix distutils logging for non-ASCII strings. This caused installation issues on Windows.

bpo-34604 [https://bugs.python.org/issue34604]: Fix possible mojibake in the error message of pwd.getpwnam and grp.getgrnam. Patch by William Grzybowski.

bpo-34530 [https://bugs.python.org/issue34530]: distutils.spawn.find_executable() now falls back on os.defpath if the PATH environment variable is not set.

bpo-34563 [https://bugs.python.org/issue34563]: On Windows, fix multiprocessing.Connection for very large read: fix _winapi.PeekNamedPipe() and _winapi.ReadFile() for read larger than INT_MAX (usually 2^31-1).

bpo-13312 [https://bugs.python.org/issue13312]: Avoids a possible integer underflow (undefined behavior) in the time module’s year handling code when passed a very low negative year value.

bpo-34472 [https://bugs.python.org/issue34472]: Improved compatibility for streamed files in zipfile. Previously an optional signature was not being written and certain ZIP applications were not supported. Patch by Silas Sewell.

bpo-6700 [https://bugs.python.org/issue6700]: Fix inspect.getsourcelines for module level frames/tracebacks. Patch by Vladimir Matveev.

bpo-32947 [https://bugs.python.org/issue32947]: Add OP_ENABLE_MIDDLEBOX_COMPAT and test workaround for TLSv1.3 for future compatibility with OpenSSL 1.1.1.

bpo-34341 [https://bugs.python.org/issue34341]: Appending to the ZIP archive with the ZIP64 extension no longer grows the size of extra fields of existing entries.

bpo-18540 [https://bugs.python.org/issue18540]: The imaplib.IMAP4 and imaplib.IMAP4_SSL classes now resolve to the local host IP correctly when the default value of host parameter ('') is used.

bpo-34246 [https://bugs.python.org/issue34246]: smtplib.SMTP.send_message() no longer modifies the content of the mail_options argument. Patch by Pablo S. Blum de Aguiar.

bpo-31047 [https://bugs.python.org/issue31047]: Fix ntpath.abspath for invalid paths on windows. Patch by Franz Woellert.

bpo-34263 [https://bugs.python.org/issue34263]: asyncio’s event loop will not pass timeouts longer than one day to epoll/select etc.

bpo-32215 [https://bugs.python.org/issue32215]: Fix performance regression in sqlite3 when a DML statement appeared in a different line than the rest of the SQL query.

bpo-19891 [https://bugs.python.org/issue19891]: Ignore errors caused by missing / non-writable homedir while writing history during exit of an interactive session. Patch by Anthony Sottile.

bpo-940286 [https://bugs.python.org/issue940286]: pydoc’s Helper.showtopic() method now prints the cross references of a topic correctly.

bpo-34164 [https://bugs.python.org/issue34164]: base64.b32decode() could raise UnboundLocalError or OverflowError for incorrect padding. Now it always raises base64.Error in these cases.

bpo-33967 [https://bugs.python.org/issue33967]: functools.singledispatch now raises TypeError instead of IndexError when no positional arguments are passed.

bpo-34054 [https://bugs.python.org/issue34054]: The multiprocessing module now uses the monotonic clock time.monotonic() instead of the system clock time.time() to implement timeout.

bpo-34010 [https://bugs.python.org/issue34010]: Fixed a performance regression for reading streams with tarfile. The buffered read should use a list, instead of appending to a bytes object.

bpo-34019 [https://bugs.python.org/issue34019]: webbrowser: Correct the arguments passed to Opera Browser when opening a new URL using the webbrowser module. Patch by Bumsik Kim.

bpo-33978 [https://bugs.python.org/issue33978]: Closed existing logging handlers before reconfiguration via fileConfig and dictConfig. Patch by Karthikeyan Singaravelan.

bpo-14117 [https://bugs.python.org/issue14117]: Make minor tweaks to turtledemo. The ‘wikipedia’ example is now ‘rosette’, decribing what it draws. The ‘penrose’ print output is reduced. The’1024’ output of ‘tree’ is eliminated.

bpo-33974 [https://bugs.python.org/issue33974]: Fixed passing lists and tuples of strings containing special characters ", \, {, } and \n as options to ttk widgets.

bpo-27500 [https://bugs.python.org/issue27500]: Fix getaddrinfo to resolve IPv6 addresses correctly.

bpo-24567 [https://bugs.python.org/issue24567]: Improve random.choices() to handle subnormal input weights that could occasionally trigger an IndexError.

bpo-33871 [https://bugs.python.org/issue33871]: Fixed integer overflow in os.readv() and os.writev() and in os.sendfile() with headers or trailers arguments (on BSD-based OSes and macOS).

bpo-33899 [https://bugs.python.org/issue33899]: Tokenize module now implicitly emits a NEWLINE when provided with input that does not have a trailing new line. This behavior now matches what the C tokenizer does internally. Contributed by Ammar Askar.

bpo-33916 [https://bugs.python.org/issue33916]: bz2 and lzma: When Decompressor.__init__() is called twice, free the old lock to not leak memory.

bpo-32568 [https://bugs.python.org/issue32568]: Make select.epoll() and its documentation consistent regarding sizehint and flags.

bpo-33663 [https://bugs.python.org/issue33663]: Convert content length to string before putting to header.

bpo-26544 [https://bugs.python.org/issue26544]: Fixed implementation of platform.libc_ver(). It almost always returned version ‘2.9’ for glibc.

bpo-27397 [https://bugs.python.org/issue27397]: Make email module properly handle invalid-length base64 strings.

bpo-33476 [https://bugs.python.org/issue33476]: Fix _header_value_parser.py when address group is missing final ‘;’. Contributed by Enrique Perez-Terron

bpo-33570 [https://bugs.python.org/issue33570]: Change TLS 1.3 cipher suite settings for compatibility with OpenSSL 1.1.1-pre6 and newer. OpenSSL 1.1.1 will have TLS 1.3 cipers enabled by default.

bpo-33365 [https://bugs.python.org/issue33365]: Print the header values besides the header keys instead just the header keys if debuglevel is set to >0 in http.client. Patch by Marco Strigl.

bpo-33336 [https://bugs.python.org/issue33336]: imaplib now allows MOVE command in IMAP4.uid() (RFC 6851: IMAP MOVE Extension) and potentially as a name of supported method of IMAP4 object.

bpo-32356 [https://bugs.python.org/issue32356]: asyncio.transport.resume_reading() and pause_reading() are now idempotent.

bpo-31608 [https://bugs.python.org/issue31608]: Raise a TypeError instead of crashing if a collections.deque subclass returns a non-deque from __new__. Patch by Oren Milman.

bpo-29456 [https://bugs.python.org/issue29456]: Fix bugs in hangul normalization: u1176, u11a7 and u11c3

文档

bpo-28617 [https://bugs.python.org/issue28617]: Fixed info in the stdtypes docs concerning the types that support membership tests.

bpo-34065 [https://bugs.python.org/issue34065]: Fix wrongly written basicConfig documentation markup syntax

bpo-33847 [https://bugs.python.org/issue33847]: Add ‘@’ operator entry to index.

bpo-25041 [https://bugs.python.org/issue25041]: Document AF_PACKET in the socket module.

测试

bpo-34587 [https://bugs.python.org/issue34587]: test_socket: Remove RDSTest.testCongestion(). The test tries to fill the receiver’s socket buffer and expects an error. But the RDS protocol doesn’t require that. Moreover, the Linux implementation of RDS expects that the producer of the messages reduces its rate, it’s not the role of the receiver to trigger an error. The test fails on Fedora 28 by design, so just remove it.

bpo-34661 [https://bugs.python.org/issue34661]: Fix test_shutil if unzip doesn’t support -t.

bpo-34200 [https://bugs.python.org/issue34200]: Fixed non-deterministic flakiness of test_pkg by not using the scary test.support.module_cleanup() logic to save and restore sys.modules contents between test cases.

bpo-34594 [https://bugs.python.org/issue34594]: Fix usage of hardcoded errno values in the tests.

bpo-34542 [https://bugs.python.org/issue34542]: Use 3072 RSA keys and SHA-256 signature for test certs and keys.

bpo-34391 [https://bugs.python.org/issue34391]: Fix ftplib test for TLS 1.3 by reading from data socket.

bpo-34399 [https://bugs.python.org/issue34399]: Update all RSA keys and DH params to use at least 2048 bits.

bpo-33746 [https://bugs.python.org/issue33746]: Fix test_unittest when run in verbose mode.

bpo-33901 [https://bugs.python.org/issue33901]: Fix test_dbm_gnu on macOS with gdbm 1.15: add a larger value to make sure that the file size changes.

bpo-33873 [https://bugs.python.org/issue33873]: Fix a bug in regrtest that caused an extra test to run if –huntrleaks/-R was used. Exit with error in case that invalid parameters are specified to –huntrleaks/-R (at least one warmup run and one repetition must be used).

bpo-32663 [https://bugs.python.org/issue32663]: Making sure the SMTPUTF8SimTests class of tests gets run in test_smtplib.py.

构建

bpo-34710 [https://bugs.python.org/issue34710]: Fixed SSL module build with OpenSSL & pedantic CFLAGS.

bpo-34582 [https://bugs.python.org/issue34582]: Add JUnit XML output for regression tests and update Azure DevOps builds.

bpo-34121 [https://bugs.python.org/issue34121]: Fix detection of C11 atomic support on clang.

bpo-30345 [https://bugs.python.org/issue30345]: Add -g to LDFLAGS when compiling with LTO to get debug symbols.

Windows

bpo-34770 [https://bugs.python.org/issue34770]: Fix a possible null pointer dereference in pyshellext.cpp.

bpo-34603 [https://bugs.python.org/issue34603]: Fix returning structs from functions produced by MSVC

bpo-34225 [https://bugs.python.org/issue34225]: Ensure INCLUDE and LIB directories do not end with a backslash.

bpo-34006 [https://bugs.python.org/issue34006]: Revert line length limit for Windows help docs. The line-length limit is not needed because the pages appear in a separate app rather than on a browser tab. It can also interact badly with the DPI setting.

bpo-31546 [https://bugs.python.org/issue31546]: Restore running PyOS_InputHook while waiting for user input at the prompt. The restores integration of interactive GUI windows (such as Matplotlib figures) with the prompt on Windows.

bpo-30237 [https://bugs.python.org/issue30237]: Output error when ReadConsole is canceled by CancelSynchronousIo instead of crashing.

bpo-29097 [https://bugs.python.org/issue29097]: Fix bug where datetime.fromtimestamp() erronously throws an OSError on Windows for values between 0 and 86400. Patch by Ammar Askar.

macOS

bpo-34370 [https://bugs.python.org/issue34370]: Have macOS 10.9+ installer builds for 3.7.1rc and 3.6.7rc use a development snapshot of Tk 8.6 (post-8.6.8) to mitigate certain scroller issues seen with IDLE and tkinter apps.

bpo-34405 [https://bugs.python.org/issue34405]: Update to OpenSSL 1.0.2p for macOS installer builds.

bpo-31903 [https://bugs.python.org/issue31903]: In _scproxy, drop the GIL when calling into SystemConfiguration to avoid deadlocks.

IDLE

bpo-34548 [https://bugs.python.org/issue34548]: Use configured color theme for read-only text views.

bpo-1529353 [https://bugs.python.org/issue1529353]: Enable “squeezing” of long outputs in the shell, to avoid performance degradation and to clean up the history without losing it. Squeezed outputs may be copied, viewed in a separate window, and “unsqueezed”.

bpo-34047 [https://bugs.python.org/issue34047]: Fixed mousewheel scrolling direction on macOS.

bpo-34275 [https://bugs.python.org/issue34275]: Make IDLE calltips always visible on Mac. Some MacOS-tk combinations need .update_idletasks(). Patch by Kevin Walzer.

bpo-34120 [https://bugs.python.org/issue34120]: Fix unresponsiveness after closing certain windows and dialogs.

bpo-33975 [https://bugs.python.org/issue33975]: Avoid small type when running htests. Since part of the purpose of human-viewed tests is to determine that widgets look right, it is important that they look the same for testing as when running IDLE.

bpo-33905 [https://bugs.python.org/issue33905]: Add test for idlelib.stackview.StackBrowser.

bpo-33924 [https://bugs.python.org/issue33924]: Change mainmenu.menudefs key ‘windows’ to ‘window’. Every other menudef key is lowercase version of main menu entry.

bpo-33906 [https://bugs.python.org/issue33906]: Rename idlelib.windows as window Match Window on the main menu and remove last plural module name.

bpo-33917 [https://bugs.python.org/issue33917]: Fix and document idlelib/idle_test/template.py. The revised file compiles, runs, and tests OK. idle_test/README.txt explains how to use it to create new IDLE test files.

bpo-33904 [https://bugs.python.org/issue33904]: IDLE: In rstrip, rename class RstripExtension as Rstrip

bpo-33907 [https://bugs.python.org/issue33907]: For consistency and clarity, rename an IDLE module and classes. Module calltips and its class CallTips are now calltip and Calltip. In module calltip_w, class CallTip is now CalltipWindow.

bpo-33856 [https://bugs.python.org/issue33856]: Add “help” in the welcome message of IDLE

bpo-33839 [https://bugs.python.org/issue33839]: IDLE: refactor ToolTip and CallTip and add documentation and tests

bpo-33855 [https://bugs.python.org/issue33855]: Minimally test all IDLE modules. Add missing files, import module, instantiate classes, and check coverage. Check existing files.

工具/示例

bpo-32962 [https://bugs.python.org/issue32962]: python-gdb now catches UnicodeDecodeError exceptions when calling string().

bpo-32962 [https://bugs.python.org/issue32962]: python-gdb now catches ValueError on read_var(): when Python has no debug symbols for example.

C API

bpo-23927 [https://bugs.python.org/issue23927]: Fixed SystemError in PyArg_ParseTupleAndKeywords() when the w* format unit is used for optional parameter.

Python 3.6.6 正式版

发布日期: 2018-06-27

在 3.6.6 版本中没有新的更改。

Python 3.6.6 RC 1

发布日期: 2018-06-11

核心与内置

bpo-33786 [https://bugs.python.org/issue33786]: Fix asynchronous generators to handle GeneratorExit in athrow() correctly

bpo-30654 [https://bugs.python.org/issue30654]: Fixed reset of the SIGINT handler to SIG_DFL on interpreter shutdown even when there was a custom handler set previously. Patch by Philipp Kerling.

bpo-33622 [https://bugs.python.org/issue33622]: Fixed a leak when the garbage collector fails to add an object with the __del__ method or referenced by it into the gc.garbage list. PyGC_Collect() can now be called when an exception is set and preserves it.

bpo-31849 [https://bugs.python.org/issue31849]: Fix signed/unsigned comparison warning in pyhash.c.

bpo-33391 [https://bugs.python.org/issue33391]: Fix a leak in set_symmetric_difference().

bpo-28055 [https://bugs.python.org/issue28055]: Fix unaligned accesses in siphash24(). Patch by Rolf Eike Beer.

bpo-33231 [https://bugs.python.org/issue33231]: 修复 normalizestring() 中潜在的内存泄漏。

bpo-29922 [https://bugs.python.org/issue29922]: Improved error messages in ‘async with’ when __aenter__() or __aexit__() return non-awaitable object.

bpo-33199 [https://bugs.python.org/issue33199]: Fix ma_version_tag in dict implementation is uninitialized when copying from key-sharing dict.

bpo-33041 [https://bugs.python.org/issue33041]: Fixed jumping when the function contains an async for loop.

bpo-32282 [https://bugs.python.org/issue32282]: Fix an unnecessary ifdef in the include of VersionHelpers.h in socketmodule on Windows.

bpo-21983 [https://bugs.python.org/issue21983]: Fix a crash in ctypes.cast() in case the type argument is a ctypes structured data type. Patch by Eryk Sun and Oren Milman.

库

bpo-30167 [https://bugs.python.org/issue30167]: Prevent site.main() exception if PYTHONSTARTUP is set. Patch by Steve Weber.

bpo-33812 [https://bugs.python.org/issue33812]: Datetime instance d with non-None tzinfo, but with d.tzinfo.utcoffset(d) returning None is now treated as naive by the astimezone() method.

bpo-30805 [https://bugs.python.org/issue30805]: Avoid race condition with debug logging

bpo-33767 [https://bugs.python.org/issue33767]: The concatenation (+) and repetition (*) sequence operations now raise TypeError instead of SystemError when performed on mmap.mmap objects. Patch by Zackery Spytz.

bpo-32684 [https://bugs.python.org/issue32684]: Fix gather to propagate cancellation of itself even with return_exceptions.

bpo-33674 [https://bugs.python.org/issue33674]: Fix a race condition in SSLProtocol.connection_made() of asyncio.sslproto: start immediately the handshake instead of using call_soon(). Previously, data_received() could be called before the handshake started, causing the handshake to hang or fail.

bpo-31647 [https://bugs.python.org/issue31647]: Fixed bug where calling write_eof() on a _SelectorSocketTransport after it’s already closed raises AttributeError.

bpo-33672 [https://bugs.python.org/issue33672]: Fix Task.__repr__ crash with Cython’s bogus coroutines

bpo-33469 [https://bugs.python.org/issue33469]: Fix RuntimeError after closing loop that used run_in_executor

bpo-11874 [https://bugs.python.org/issue11874]: Use a better regex when breaking usage into wrappable parts. Avoids bogus assertion errors from custom metavar strings.

bpo-30877 [https://bugs.python.org/issue30877]: Fixed a bug in the Python implementation of the JSON decoder that prevented the cache of parsed strings from clearing after finishing the decoding. Based on patch by c-fos.

bpo-33548 [https://bugs.python.org/issue33548]: tempfile._candidate_tempdir_list should consider common TEMP locations

bpo-33542 [https://bugs.python.org/issue33542]: Prevent uuid.get_node from using a DUID instead of a MAC on Windows. Patch by Zvi Effron

bpo-26819 [https://bugs.python.org/issue26819]: Fix race condition with ReadTransport.resume_reading in Windows proactor event loop.

bpo-28556 [https://bugs.python.org/issue28556]: Minor fixes in typing module: add annotations to NamedTuple.__new__, pass *args and **kwds in Generic.__new__. Original PRs by Paulius Šarka and Chad Dombrova.

bpo-20087 [https://bugs.python.org/issue20087]: Updated alias mapping with glibc 2.27 supported locales.

bpo-33422 [https://bugs.python.org/issue33422]: Fix trailing quotation marks getting deleted when looking up byte/string literals on pydoc. Patch by Andrés Delfino.

bpo-33197 [https://bugs.python.org/issue33197]: Update error message when constructing invalid inspect.Parameters Patch by Dong-hee Na.

bpo-33383 [https://bugs.python.org/issue33383]: Fixed crash in the get() method of the dbm.ndbm database object when it is called with a single argument.

bpo-33329 [https://bugs.python.org/issue33329]: Fix multiprocessing regression on newer glibcs

bpo-991266 [https://bugs.python.org/issue991266]: Fix quoting of the Comment attribute of http.cookies.SimpleCookie.

bpo-33131 [https://bugs.python.org/issue33131]: Upgrade bundled version of pip to 10.0.1.

bpo-33308 [https://bugs.python.org/issue33308]: Fixed a crash in the parser module when converting an ST object to a tree of tuples or lists with line_info=False and col_info=True.

bpo-33263 [https://bugs.python.org/issue33263]: Fix FD leak in _SelectorSocketTransport Patch by Vlad Starostin.

bpo-33256 [https://bugs.python.org/issue33256]: Fix display of <module> call in the html produced by cgitb.html(). Patch by Stéphane Blondon.

bpo-33203 [https://bugs.python.org/issue33203]: random.Random.choice() now raises IndexError for empty sequences consistently even when called from subclasses without a getrandbits() implementation.

bpo-33224 [https://bugs.python.org/issue33224]: Update difflib.mdiff() for PEP 479. Convert an uncaught StopIteration in a generator into a return-statement.

bpo-33209 [https://bugs.python.org/issue33209]: End framing at the end of C implementation of pickle.Pickler.dump().

bpo-32861 [https://bugs.python.org/issue32861]: The urllib.robotparser’s __str__ representation now includes wildcard entries and the “Crawl-delay” and “Request-rate” fields. Patch by Michael Lazar.

bpo-33096 [https://bugs.python.org/issue33096]: Allow ttk.Treeview.insert to insert iid that has a false boolean value. Note iid=0 and iid=False would be same. Patch by Garvit Khatri.

bpo-33127 [https://bugs.python.org/issue33127]: ssl模块现在使用LibreSSL 2.7.1编译。

bpo-33021 [https://bugs.python.org/issue33021]: Release the GIL during fstat() calls, avoiding hang of all threads when calling mmap.mmap(), os.urandom(), and random.seed(). Patch by Nir Soffer.

bpo-27683 [https://bugs.python.org/issue27683]: Fix a regression in ipaddress that result of hosts() is empty when the network is constructed by a tuple containing an integer mask and only 1 bit left for addresses.

bpo-32844 [https://bugs.python.org/issue32844]: Fix wrong redirection of a low descriptor (0 or 1) to stderr in subprocess if another low descriptor is closed.

bpo-31908 [https://bugs.python.org/issue31908]: Fix output of cover files for trace module command-line tool. Previously emitted cover files only when --missing option was used. Patch by Michael Selik.

bpo-31457 [https://bugs.python.org/issue31457]: If nested log adapters are used, the inner process() methods are no longer omitted.

bpo-16865 [https://bugs.python.org/issue16865]: Support arrays >=2GiB in ctypes. Patch by Segev Finer.

bpo-31238 [https://bugs.python.org/issue31238]: pydoc: the stop() method of the private ServerThread class now waits until DocServer.serve_until_quit() completes and then explicitly sets its docserver attribute to None to break a reference cycle.

文档

bpo-33503 [https://bugs.python.org/issue33503]: 修复错误的pypi链接

bpo-33421 [https://bugs.python.org/issue33421]: 为``typing.AsyncContextManager``添加缺失的文档。

bpo-33378 [https://bugs.python.org/issue33378]: Add Korean language switcher for https://docs.python.org/3/

bpo-33276 [https://bugs.python.org/issue33276]: Clarify that the __path__ attribute on modules cannot be just any value.

bpo-33201 [https://bugs.python.org/issue33201]: Modernize documentation for writing C extension types.

bpo-33195 [https://bugs.python.org/issue33195]: Deprecate Py_UNICODE usage in c-api/arg document. Py_UNICODE related APIs are deprecated since Python 3.3, but it is missed in the document.

bpo-33126 [https://bugs.python.org/issue33126]: Document PyBuffer_ToContiguous().

bpo-27212 [https://bugs.python.org/issue27212]: Modify documentation for the islice() recipe to consume initial values up to the start index.

bpo-28247 [https://bugs.python.org/issue28247]: Update zipapp documentation to describe how to make standalone applications.

bpo-18802 [https://bugs.python.org/issue18802]: Documentation changes for ipaddress. Patch by Jon Foster and Berker Peksag.

bpo-27428 [https://bugs.python.org/issue27428]: Update documentation to clarify that WindowsRegistryFinder implements MetaPathFinder. (Patch by Himanshu Lakhara)

bpo-8243 [https://bugs.python.org/issue8243]: Add a note about curses.addch and curses.addstr exception behavior when writing outside a window, or pad.

bpo-31432 [https://bugs.python.org/issue31432]: Clarify meaning of CERT_NONE, CERT_OPTIONAL, and CERT_REQUIRED flags for ssl.SSLContext.verify_mode.

测试

bpo-33655 [https://bugs.python.org/issue33655]: 忽略运行在BSD平台的ZFS上时test_posix_fallocate的失败。

bpo-19417 [https://bugs.python.org/issue19417]: Add test_bdb.py.

构建

bpo-5755 [https://bugs.python.org/issue5755]: Move -Wstrict-prototypes option to CFLAGS_NODIST from OPT. This option emitted annoying warnings when building extension modules written in C++.

bpo-33614 [https://bugs.python.org/issue33614]: Ensures module definition files for the stable ABI on Windows are correctly regenerated.

bpo-33522 [https://bugs.python.org/issue33522]: Enable CI builds on Visual Studio Team Services at https://python.visualstudio.com/cpython

bpo-33012 [https://bugs.python.org/issue33012]: Add -Wno-cast-function-type for gcc 8 for silencing warnings about function casts like casting to PyCFunction in method definition lists.

bpo-33394 [https://bugs.python.org/issue33394]: Enable the verbose build for extension modules, when GNU make is passed macros on the command line.

Windows

bpo-33184 [https://bugs.python.org/issue33184]: 更新Windows 构建以使用OpenSSL 1.0.2o.

macOS

bpo-33184 [https://bugs.python.org/issue33184]: 在MacOS安装程序中将OpenSSL更新为1.0.2o.

IDLE

bpo-33656 [https://bugs.python.org/issue33656]: On Windows, add API call saying that tk scales for DPI. On Windows 8.1+ or 10, with DPI compatibility properties of the Python binary unchanged, and a monitor resolution greater than 96 DPI, this should make text and lines sharper. It should otherwise have no effect.

bpo-33768 [https://bugs.python.org/issue33768]: Clicking on a context line moves that line to the top of the editor window.

bpo-33763 [https://bugs.python.org/issue33763]: IDLE: Use read-only text widget for code context instead of label widget.

bpo-33664 [https://bugs.python.org/issue33664]: Scroll IDLE editor text by lines. Previously, the mouse wheel and scrollbar slider moved text by a fixed number of pixels, resulting in partial lines at the top of the editor box. The change also applies to the shell and grep output windows, but not to read-only text views.

bpo-33679 [https://bugs.python.org/issue33679]: Enable theme-specific color configuration for Code Context. Use the Highlights tab to see the setting for built-in themes or add settings to custom themes.

bpo-33642 [https://bugs.python.org/issue33642]: Display up to maxlines non-blank lines for Code Context. If there is no current context, show a single blank line.

bpo-33628 [https://bugs.python.org/issue33628]: IDLE: Cleanup codecontext.py and its test.

bpo-33564 [https://bugs.python.org/issue33564]: IDLE’s code context now recognizes async as a block opener.

bpo-29706 [https://bugs.python.org/issue29706]: IDLE now colors async and await as keywords in 3.6. They become full keywords in 3.7.

bpo-21474 [https://bugs.python.org/issue21474]: Update word/identifier definition from ascii to unicode. In text and entry boxes, this affects selection by double-click, movement left/right by control-left/right, and deletion left/right by control-BACKSPACE/DEL.

bpo-33204 [https://bugs.python.org/issue33204]: IDLE: consistently color invalid string prefixes. A ‘u’ string prefix cannot be paired with either ‘r’ or ‘f’. Consistently color as much of the prefix, starting at the right, as is valid. Revise and extend colorizer test.

bpo-32831 [https://bugs.python.org/issue32831]: Add docstrings and tests for codecontext.

工具/示例

bpo-33189 [https://bugs.python.org/issue33189]: pygettext.py now recognizes only literal strings as docstrings and translatable strings, and rejects bytes literals and f-string expressions.

bpo-31920 [https://bugs.python.org/issue31920]: Fixed handling directories as arguments in the pygettext script. Based on patch by Oleg Krasnikov.

bpo-29673 [https://bugs.python.org/issue29673]: Fix pystackv and pystack gdbinit macros.

bpo-32885 [https://bugs.python.org/issue32885]: Add an -n flag for Tools/scripts/pathfix.py to disable automatic backup creation (files with ~ suffix).

bpo-31583 [https://bugs.python.org/issue31583]: Fix 2to3 for using with –add-suffix option but without –output-dir option for relative path to files in current directory.

C API

bpo-32374 [https://bugs.python.org/issue32374]: Document that m_traverse for multi-phase initialized modules can be called with m_state=NULL, and add a sanity check

Python 3.6.5 正式版

发布日期: 2018-03-28

测试

bpo-32872 [https://bugs.python.org/issue32872]: Avoid regrtest compatibility issue with namespace packages.

构建

bpo-33163 [https://bugs.python.org/issue33163]: Upgrade pip to 9.0.3 and setuptools to v39.0.1.

Python 3.6.5 rc1

发布日期: 2018-03-13

安全

bpo-33001 [https://bugs.python.org/issue33001]: Minimal fix to prevent buffer overrun in os.symlink on Windows

bpo-32981 [https://bugs.python.org/issue32981]: Regexes in difflib and poplib were vulnerable to catastrophic backtracking. These regexes formed potential DOS vectors (REDOS). They have been refactored. This resolves CVE-2018-1060 and CVE-2018-1061. Patch by Jamie Davis.

核心与内置

bpo-33026 [https://bugs.python.org/issue33026]: Fixed jumping out of “with” block by setting f_lineno.

bpo-17288 [https://bugs.python.org/issue17288]: Prevent jumps from ‘return’ and ‘exception’ trace events.

bpo-32889 [https://bugs.python.org/issue32889]: Update Valgrind suppression list to account for the rename of Py_ADDRESS_IN_RANG to address_in_range.

bpo-32650 [https://bugs.python.org/issue32650]: Pdb and other debuggers dependent on bdb.py will correctly step over (next command) native coroutines. Patch by Pablo Galindo.

bpo-32685 [https://bugs.python.org/issue32685]: Improve suggestion when the Python 2 form of print statement is either present on the same line as the header of a compound statement or else terminated by a semi-colon instead of a newline. Patch by Nitish Chandra.

bpo-32583 [https://bugs.python.org/issue32583]: Fix possible crashing in builtin Unicode decoders caused by write out-of-bound errors when using customized decode error handlers.

bpo-26163 [https://bugs.python.org/issue26163]: Improved frozenset() hash to create more distinct hash values when faced with datasets containing many similar values.

bpo-27169 [https://bugs.python.org/issue27169]: The __debug__ constant is now optimized out at compile time. This fixes also bpo-22091 [https://bugs.python.org/issue22091].

bpo-32329 [https://bugs.python.org/issue32329]: sys.flags.hash_randomization is now properly set to 0 when hash randomization is turned off by PYTHONHASHSEED=0.

bpo-30416 [https://bugs.python.org/issue30416]: The optimizer is now protected from spending much time doing complex calculations and consuming much memory for creating large constants in constant folding.

bpo-18533 [https://bugs.python.org/issue18533]: repr() on a dict containing its own values() or items() no longer raises RecursionError; OrderedDict similarly. Instead, use ..., as for other recursive structures. Patch by Ben North.

bpo-32028 [https://bugs.python.org/issue32028]: Leading whitespace is now correctly ignored when generating suggestions for converting Py2 print statements to Py3 builtin print function calls. Patch by Sanyam Khurana.

bpo-32137 [https://bugs.python.org/issue32137]: The repr of deeply nested dict now raises a RecursionError instead of crashing due to a stack overflow.

库

bpo-33064 [https://bugs.python.org/issue33064]: lib2to3 now properly supports trailing commas after *args and **kwargs in function signatures.

bpo-31804 [https://bugs.python.org/issue31804]: Avoid failing in multiprocessing.Process if the standard streams are closed or None at exit.

bpo-33037 [https://bugs.python.org/issue33037]: Skip sending/receiving data after SSL transport closing.

bpo-30353 [https://bugs.python.org/issue30353]: Fix ctypes pass-by-value for structs on 64-bit Cygwin/MinGW.

bpo-33009 [https://bugs.python.org/issue33009]: Fix inspect.signature() for single-parameter partialmethods.

bpo-32969 [https://bugs.python.org/issue32969]: Expose several missing constants in zlib and fix corresponding documentation.

bpo-32713 [https://bugs.python.org/issue32713]: Fixed tarfile.itn handling of out-of-bounds float values. Patch by Joffrey Fuhrer.

bpo-30622 [https://bugs.python.org/issue30622]: The ssl module now detects missing NPN support in LibreSSL.

bpo-32922 [https://bugs.python.org/issue32922]: dbm.open() now encodes filename with the filesystem encoding rather than default encoding.

bpo-32859 [https://bugs.python.org/issue32859]: In os.dup2, don’t check every call whether the dup3 syscall exists or not.

bpo-21060 [https://bugs.python.org/issue21060]: Rewrite confusing message from setup.py upload from “No dist file created in earlier command” to the more helpful “Must create and upload files in one command”.

bpo-32857 [https://bugs.python.org/issue32857]: In tkinter, after_cancel(None) now raises a ValueError instead of canceling the first scheduled function. Patch by Cheryl Sabella.

bpo-32852 [https://bugs.python.org/issue32852]: Make sure sys.argv remains as a list when running trace.

bpo-32841 [https://bugs.python.org/issue32841]: Fixed asyncio.Condition issue which silently ignored cancellation after notifying and cancelling a conditional lock. Patch by Bar Harel.

bpo-31787 [https://bugs.python.org/issue31787]: Fixed refleaks of __init__() methods in various modules. (Contributed by Oren Milman)

bpo-30157 [https://bugs.python.org/issue30157]: Fixed guessing quote and delimiter in csv.Sniffer.sniff() when only the last field is quoted. Patch by Jake Davis.

bpo-32394 [https://bugs.python.org/issue32394]: socket: Remove TCP_FASTOPEN, TCP_KEEPCNT flags on older version Windows during run-time.

bpo-32777 [https://bugs.python.org/issue32777]: Fix a rare but potential pre-exec child process deadlock in subprocess on POSIX systems when marking file descriptors inheritable on exec in the child process. This bug appears to have been introduced in 3.4.

bpo-32647 [https://bugs.python.org/issue32647]: The ctypes module used to depend on indirect linking for dlopen. The shared extension is now explicitly linked against libdl on platforms with dl.

bpo-32734 [https://bugs.python.org/issue32734]: Fixed asyncio.Lock() safety issue which allowed acquiring and locking the same lock multiple times, without it being free. Patch by Bar Harel.

bpo-32727 [https://bugs.python.org/issue32727]: Do not include name field in SMTP envelope from address. Patch by Stéphane Wirtel

bpo-27931 [https://bugs.python.org/issue27931]: Fix email address header parsing error when the username is an empty quoted string. Patch by Xiang Zhang.

bpo-32304 [https://bugs.python.org/issue32304]: distutils’ upload command no longer corrupts tar files ending with a CR byte, and no longer tries to convert CR to CRLF in any of the upload text fields.

bpo-32502 [https://bugs.python.org/issue32502]: uuid.uuid1 no longer raises an exception if a 64-bit hardware address is encountered.

bpo-31848 [https://bugs.python.org/issue31848]: Fix the error handling in Aifc_read.initfp() when the SSND chunk is not found. Patch by Zackery Spytz.

bpo-32555 [https://bugs.python.org/issue32555]: On FreeBSD and Solaris, os.strerror() now always decode the byte string from the current locale encoding, rather than using ASCII/surrogateescape in some cases.

bpo-32521 [https://bugs.python.org/issue32521]: The nis module is now compatible with new libnsl and headers location.

bpo-32473 [https://bugs.python.org/issue32473]: Improve ABCMeta._dump_registry() output readability

bpo-32521 [https://bugs.python.org/issue32521]: glibc has removed Sun RPC. Use replacement libtirpc headers and library in nis module.

bpo-32228 [https://bugs.python.org/issue32228]: Ensure that truncate() preserves the file position (as reported by tell()) after writes longer than the buffer size.

bpo-26133 [https://bugs.python.org/issue26133]: Don’t unsubscribe signals in asyncio UNIX event loop on interpreter shutdown.

bpo-32185 [https://bugs.python.org/issue32185]: The SSL module no longer sends IP addresses in SNI TLS extension on platforms with OpenSSL 1.0.2+ or inet_pton.

bpo-32323 [https://bugs.python.org/issue32323]: urllib.parse.urlsplit() does not convert zone-id (scope) to lower case for scoped IPv6 addresses in hostnames now.

bpo-32302 [https://bugs.python.org/issue32302]: Fix bdist_wininst of distutils for CRT v142: it binary compatible with CRT v140.

bpo-32255 [https://bugs.python.org/issue32255]: A single empty field is now always quoted when written into a CSV file. This allows to distinguish an empty row from a row consisting of a single empty field. Patch by Licht Takeuchi.

bpo-32277 [https://bugs.python.org/issue32277]: Raise NotImplementedError instead of SystemError on platforms where chmod(..., follow_symlinks=False) is not supported. Patch by Anthony Sottile.

bpo-32199 [https://bugs.python.org/issue32199]: The getnode() ip getter now uses ‘ip link’ instead of ‘ip link list’.

bpo-27456 [https://bugs.python.org/issue27456]: Ensure TCP_NODELAY is set on Linux. Tests by Victor Stinner.

bpo-31900 [https://bugs.python.org/issue31900]: The locale.localeconv() function now sets temporarily the LC_CTYPE locale to the LC_NUMERIC locale to decode decimal_point and thousands_sep byte strings if they are non-ASCII or longer than 1 byte, and the LC_NUMERIC locale is different than the LC_CTYPE locale. This temporary change affects other threads.

Same change for the str.format() method when formatting a number (int, float, float and subclasses) with the n type (ex: '{:n}'.format(1234)).

bpo-31802 [https://bugs.python.org/issue31802]: Importing native path module (posixpath, ntpath) now works even if the os module still is not imported.

文档

bpo-17232 [https://bugs.python.org/issue17232]: Clarify docs for -O and -OO. Patch by Terry Reedy.

bpo-32800 [https://bugs.python.org/issue32800]: Update link to w3c doc for xml default namespaces.

bpo-8722 [https://bugs.python.org/issue8722]: Document __getattr__() behavior when property get() method raises AttributeError.

bpo-32614 [https://bugs.python.org/issue32614]: Modify RE examples in documentation to use raw strings to prevent DeprecationWarning and add text to REGEX HOWTO to highlight the deprecation.

bpo-31972 [https://bugs.python.org/issue31972]: Improve docstrings for pathlib.PurePath subclasses.

bpo-17799 [https://bugs.python.org/issue17799]: Explain real behaviour of sys.settrace and sys.setprofile and their C-API counterparts regarding which type of events are received in each function. Patch by Pablo Galindo Salgado.

测试

bpo-32517 [https://bugs.python.org/issue32517]: Fix failing test_asyncio on macOS 10.12.2+ due to transport of KqueueSelector loop was not being closed.

bpo-32721 [https://bugs.python.org/issue32721]: Fix test_hashlib to not fail if the _md5 module is not built.

bpo-32252 [https://bugs.python.org/issue32252]: Fix faulthandler_suppress_crash_report() used to prevent core dump files when testing crashes. getrlimit() returns zero on success.

bpo-31518 [https://bugs.python.org/issue31518]: Debian Unstable has disabled TLS 1.0 and 1.1 for SSLv23_METHOD(). Change TLS/SSL protocol of some tests to PROTOCOL_TLS or PROTOCOL_TLSv1_2 to make them pass on Debian.

构建

bpo-32635 [https://bugs.python.org/issue32635]: Fix segfault of the crypt module when libxcrypt is provided instead of libcrypt at the system.

Windows

bpo-33016 [https://bugs.python.org/issue33016]: Fix potential use of uninitialized memory in nt._getfinalpathname

bpo-32903 [https://bugs.python.org/issue32903]: Fix a memory leak in os.chdir() on Windows if the current directory is set to a UNC path.

bpo-31966 [https://bugs.python.org/issue31966]: Fixed WindowsConsoleIO.write() for writing empty data.

bpo-32409 [https://bugs.python.org/issue32409]: Ensures activate.bat can handle Unicode contents.

bpo-32457 [https://bugs.python.org/issue32457]: Improves handling of denormalized executable path when launching Python.

bpo-32370 [https://bugs.python.org/issue32370]: Use the correct encoding for ipconfig output in the uuid module. Patch by Segev Finer.

bpo-29248 [https://bugs.python.org/issue29248]: Fix os.readlink() on Windows, which was mistakenly treating the PrintNameOffset field of the reparse data buffer as a number of characters instead of bytes. Patch by Craig Holmquist and SSE4.

bpo-32588 [https://bugs.python.org/issue32588]: Create standalone _distutils_findvs module.

macOS

bpo-32726 [https://bugs.python.org/issue32726]: Provide an additional, more modern macOS installer variant that supports macOS 10.9+ systems in 64-bit mode only. Upgrade the supplied third-party libraries to OpenSSL 1.0.2n, XZ 5.2.3, and SQLite 3.22.0. The 10.9+ installer now links with and supplies its own copy of Tcl/Tk 8.6.8.

IDLE

bpo-32984 [https://bugs.python.org/issue32984]: Set __file__ while running a startup file. Like Python, IDLE optionally runs one startup file in the Shell window before presenting the first interactive input prompt. For IDLE, -s runs a file named in environmental variable IDLESTARTUP or PYTHONSTARTUP; -r file runs file. Python sets __file__ to the startup file name before running the file and unsets it before the first prompt. IDLE now does the same when run normally, without the -n option.

bpo-32940 [https://bugs.python.org/issue32940]: Simplify and rename StringTranslatePseudoMapping in pyparse.

bpo-32916 [https://bugs.python.org/issue32916]: Change str to code in pyparse.

bpo-32905 [https://bugs.python.org/issue32905]: Remove unused code in pyparse module.

bpo-32874 [https://bugs.python.org/issue32874]: Add tests for pyparse.

bpo-32837 [https://bugs.python.org/issue32837]: Using the system and place-dependent default encoding for open() is a bad idea for IDLE’s system and location-independent files.

bpo-32826 [https://bugs.python.org/issue32826]: Add “encoding=utf-8” to open() in IDLE’s test_help_about. GUI test test_file_buttons() only looks at initial ascii-only lines, but failed on systems where open() defaults to ‘ascii’ because readline() internally reads and decodes far enough ahead to encounter a non-ascii character in CREDITS.txt.

bpo-32765 [https://bugs.python.org/issue32765]: Update configdialog General tab docstring to add new widgets to the widget list.

工具/示例

bpo-24960 [https://bugs.python.org/issue24960]: 2to3 and lib2to3 can now read pickled grammar files using pkgutil.get_data() rather than probing the filesystem. This lets 2to3 and lib2to3 work when run from a zipfile.

bpo-32222 [https://bugs.python.org/issue32222]: Fix pygettext not extracting docstrings for functions with type annotated arguments. Patch by Toby Harradine.

C API

bpo-29084 [https://bugs.python.org/issue29084]: Undocumented C API for OrderedDict has been excluded from the limited C API. It was added by mistake and actually never worked in the limited C API.

Python 3.6.4 正式版

发布日期: 2017-12-18

从 3.6.4 rc1 到 3.6.4 正式版中没有新的代码更改。

Python 3.6.4 rc1

发布日期: 2017-12-05

核心与内置

bpo-32176 [https://bugs.python.org/issue32176]: co_flags.CO_NOFREE is now always set correctly by the code object constructor based on freevars and cellvars, rather than needing to be set correctly by the caller. This ensures it will be cleared automatically when additional cell references are injected into a modified code object and function.

bpo-31949 [https://bugs.python.org/issue31949]: Fixed several issues in printing tracebacks (PyTraceBack_Print()).

Setting sys.tracebacklimit to 0 or less now suppresses printing tracebacks.

Setting sys.tracebacklimit to None now causes using the default limit.

Setting sys.tracebacklimit to an integer larger than LONG_MAX now means using the limit LONG_MAX rather than the default limit.

Fixed integer overflows in the case of more than 2**31 traceback items on Windows.

Fixed output errors handling.

bpo-30696 [https://bugs.python.org/issue30696]: Fix the interactive interpreter looping endlessly when no memory.

bpo-20047 [https://bugs.python.org/issue20047]: Bytearray methods partition() and rpartition() now accept only bytes-like objects as separator, as documented. In particular they now raise TypeError rather of returning a bogus result when an integer is passed as a separator.

bpo-31852 [https://bugs.python.org/issue31852]: Fix a segmentation fault caused by a combination of the async soft keyword and continuation lines.

bpo-21720 [https://bugs.python.org/issue21720]: BytesWarning no longer emitted when the fromlist argument of __import__() or the __all__ attribute of the module contain bytes instances.

bpo-31825 [https://bugs.python.org/issue31825]: Fixed OverflowError in the ‘unicode-escape’ codec and in codecs.escape_decode() when decode an escaped non-ascii byte.

bpo-28603 [https://bugs.python.org/issue28603]: Print the full context/cause chain of exceptions on interpreter exit, even if an exception in the chain is unhashable or compares equal to later ones. Patch by Zane Bitter.

bpo-31786 [https://bugs.python.org/issue31786]: Fix timeout rounding in the select module to round correctly negative timeouts between -1.0 and 0.0. The functions now block waiting for events as expected. Previously, the call was incorrectly non-blocking. Patch by Pablo Galindo.

bpo-31642 [https://bugs.python.org/issue31642]: Restored blocking “from package import module” by setting sys.modules[“package.module”] to None.

bpo-31626 [https://bugs.python.org/issue31626]: Fixed a bug in debug memory allocator. There was a write to freed memory after shrinking a memory block.

bpo-31619 [https://bugs.python.org/issue31619]: Fixed a ValueError when convert a string with large number of underscores to integer with binary base.

bpo-31592 [https://bugs.python.org/issue31592]: Fixed an assertion failure in Python parser in case of a bad unicodedata.normalize(). Patch by Oren Milman.

bpo-31588 [https://bugs.python.org/issue31588]: Raise a TypeError with a helpful error message when class creation fails due to a metaclass with a bad __prepare__() method. Patch by Oren Milman.

bpo-31566 [https://bugs.python.org/issue31566]: Fix an assertion failure in _warnings.warn() in case of a bad __name__ global. Patch by Oren Milman.

bpo-31505 [https://bugs.python.org/issue31505]: Fix an assertion failure in json, in case _json.make_encoder() received a bad encoder() argument. Patch by Oren Milman.

bpo-31492 [https://bugs.python.org/issue31492]: Fix assertion failures in case of failing to import from a module with a bad __name__ attribute, and in case of failing to access an attribute of such a module. Patch by Oren Milman.

bpo-31490 [https://bugs.python.org/issue31490]: Fix an assertion failure in ctypes class definition, in case the class has an attribute whose name is specified in _anonymous_ but not in _fields_. Patch by Oren Milman.

bpo-31478 [https://bugs.python.org/issue31478]: Fix an assertion failure in _random.Random.seed() in case the argument has a bad __abs__() method. Patch by Oren Milman.

bpo-31315 [https://bugs.python.org/issue31315]: Fix an assertion failure in imp.create_dynamic(), when spec.name is not a string. Patch by Oren Milman.

bpo-31311 [https://bugs.python.org/issue31311]: Fix a crash in the __setstate__() method of ctypes._CData, in case of a bad __dict__. Patch by Oren Milman.

bpo-31293 [https://bugs.python.org/issue31293]: Fix crashes in true division and multiplication of a timedelta object by a float with a bad as_integer_ratio() method. Patch by Oren Milman.

bpo-31285 [https://bugs.python.org/issue31285]: Fix an assertion failure in warnings.warn_explicit, when the return value of the received loader’s get_source() has a bad splitlines() method. Patch by Oren Milman.

bpo-30817 [https://bugs.python.org/issue30817]: PyErr_PrintEx() clears now the ignored exception that may be raised by _PySys_SetObjectId(), for example when no memory.

库

bpo-28556 [https://bugs.python.org/issue28556]: Two minor fixes for typing module: allow shallow copying instances of generic classes, improve interaction of __init_subclass__ with generics. Original PRs by Ivan Levkivskyi.

bpo-27240 [https://bugs.python.org/issue27240]: The header folding algorithm for the new email policies has been rewritten, which also fixes bpo-30788 [https://bugs.python.org/issue30788], bpo-31831 [https://bugs.python.org/issue31831], and bpo-32182 [https://bugs.python.org/issue32182]. In particular, RFC2231 folding is now done correctly.

bpo-32186 [https://bugs.python.org/issue32186]: io.FileIO.readall() and io.FileIO.read() now release the GIL when getting the file size. Fixed hang of all threads with inaccessible NFS server. Patch by Nir Soffer.

bpo-12239 [https://bugs.python.org/issue12239]: Make msilib.SummaryInformation.GetProperty() return None when the value of property is VT_EMPTY. Initial patch by Mark Mc Mahon.

bpo-31325 [https://bugs.python.org/issue31325]: Fix wrong usage of collections.namedtuple() in the RobotFileParser.parse() method.

Initial patch by Robin Wellner.

bpo-12382 [https://bugs.python.org/issue12382]: msilib.OpenDatabase() now raises a better exception message when it couldn’t open or create an MSI file. Initial patch by William Tisäter.

bpo-32110 [https://bugs.python.org/issue32110]: codecs.StreamReader.read(n) now returns not more than n characters/bytes for non-negative n. This makes it compatible with read() methods of other file-like objects.

bpo-32072 [https://bugs.python.org/issue32072]: Fixed issues with binary plists:

Fixed saving bytearrays.

Identical objects will be saved only once.

Equal references will be load as identical objects.

Added support for saving and loading recursive data structures.

bpo-32034 [https://bugs.python.org/issue32034]: Make asyncio.IncompleteReadError and LimitOverrunError pickleable.

bpo-32015 [https://bugs.python.org/issue32015]: Fixed the looping of asyncio in the case of reconnection the socket during waiting async read/write from/to the socket.

bpo-32011 [https://bugs.python.org/issue32011]: Restored support of loading marshal files with the TYPE_INT64 code. These files can be produced in Python 2.7.

bpo-31970 [https://bugs.python.org/issue31970]: Reduce performance overhead of asyncio debug mode.

bpo-9678 [https://bugs.python.org/issue9678]: Fixed determining the MAC address in the uuid module:

在NetBSD和OpenBSD系统上使用ifconfig命令

Using arp on Linux, FreeBSD, NetBSD and OpenBSD.

Based on patch by Takayuki Shimizukawa.

bpo-30057 [https://bugs.python.org/issue30057]: Fix potential missed signal in signal.signal().

bpo-31933 [https://bugs.python.org/issue31933]: Fix Blake2 params leaf_size and node_offset on big endian platforms. Patch by Jack O’Connor.

bpo-31927 [https://bugs.python.org/issue31927]: Fixed compilation of the socket module on NetBSD 8. Fixed assertion failure or reading arbitrary data when parse a AF_BLUETOOTH address on NetBSD and DragonFly BSD.

bpo-27666 [https://bugs.python.org/issue27666]: Fixed stack corruption in curses.box() and curses.ungetmouse() when the size of types chtype or mmask_t is less than the size of C long. curses.box() now accepts characters as arguments. Based on patch by Steve Fink.

bpo-31897 [https://bugs.python.org/issue31897]: plistlib now catches more errors when read binary plists and raises InvalidFileException instead of unexpected exceptions.

bpo-25720 [https://bugs.python.org/issue25720]: Fix the method for checking pad state of curses WINDOW. Patch by Masayuki Yamamoto.

bpo-31893 [https://bugs.python.org/issue31893]: Fixed the layout of the kqueue_event structure on OpenBSD and NetBSD. Fixed the comparison of the kqueue_event objects.

bpo-31891 [https://bugs.python.org/issue31891]: Fixed building the curses module on NetBSD.

bpo-28416 [https://bugs.python.org/issue28416]: Instances of pickle.Pickler subclass with the persistent_id() method and pickle.Unpickler subclass with the persistent_load() method no longer create reference cycles.

bpo-28326 [https://bugs.python.org/issue28326]: Fix multiprocessing.Process when stdout and/or stderr is closed or None.

bpo-31457 [https://bugs.python.org/issue31457]: If nested log adapters are used, the inner process() methods are no longer omitted.

bpo-31457 [https://bugs.python.org/issue31457]: The manager property on LoggerAdapter objects is now properly settable.

bpo-31806 [https://bugs.python.org/issue31806]: Fix timeout rounding in time.sleep(), threading.Lock.acquire() and socket.socket.settimeout() to round correctly negative timeouts between -1.0 and 0.0. The functions now block waiting for events as expected. Previously, the call was incorrectly non-blocking. Patch by Pablo Galindo.

bpo-28603 [https://bugs.python.org/issue28603]: traceback: Fix a TypeError that occurred during printing of exception tracebacks when either the current exception or an exception in its context/cause chain is unhashable. Patch by Zane Bitter.

bpo-30058 [https://bugs.python.org/issue30058]: Fixed buffer overflow in select.kqueue.control().

bpo-31770 [https://bugs.python.org/issue31770]: Prevent a crash when calling the __init__() method of a sqlite3.Cursor object more than once. Patch by Oren Milman.

bpo-31672 [https://bugs.python.org/issue31672]: idpattern in string.Template matched some non-ASCII characters. Now it uses -i regular expression local flag to avoid non-ASCII characters.

bpo-31764 [https://bugs.python.org/issue31764]: Prevent a crash in sqlite3.Cursor.close() in case the Cursor object is uninitialized. Patch by Oren Milman.

bpo-31752 [https://bugs.python.org/issue31752]: Fix possible crash in timedelta constructor called with custom integers.

bpo-31701 [https://bugs.python.org/issue31701]: On Windows, faulthandler.enable() now ignores MSC and COM exceptions.

bpo-31728 [https://bugs.python.org/issue31728]: Prevent crashes in _elementtree due to unsafe cleanup of Element.text and Element.tail. Patch by Oren Milman.

bpo-31620 [https://bugs.python.org/issue31620]: an empty asyncio.Queue now doesn’t leak memory when queue.get pollers timeout

bpo-31632 [https://bugs.python.org/issue31632]: Fix method set_protocol() of class _SSLProtocolTransport in asyncio module. This method was previously modifying a wrong reference to the protocol.

bpo-31675 [https://bugs.python.org/issue31675]: Fixed memory leaks in Tkinter’s methods splitlist() and split() when pass a string larger than 2 GiB.

bpo-31673 [https://bugs.python.org/issue31673]: Fixed typo in the name of Tkinter’s method adderrorinfo().

bpo-30806 [https://bugs.python.org/issue30806]: Fix the string representation of a netrc object.

bpo-15037 [https://bugs.python.org/issue15037]: Added a workaround for getkey() in curses for ncurses 5.7 and earlier.

bpo-25351 [https://bugs.python.org/issue25351]: Avoid venv activate failures with undefined variables

bpo-25532 [https://bugs.python.org/issue25532]: inspect.unwrap() will now only try to unwrap an object sys.getrecursionlimit() times, to protect against objects which create a new object on every attribute access.

bpo-30347 [https://bugs.python.org/issue30347]: Stop crashes when concurrently iterate over itertools.groupby() iterators.

bpo-31516 [https://bugs.python.org/issue31516]: threading.current_thread() should not return a dummy thread at shutdown.

bpo-31351 [https://bugs.python.org/issue31351]: python -m ensurepip now exits with non-zero exit code if pip bootstrapping has failed.

bpo-31482 [https://bugs.python.org/issue31482]: random.seed() now works with bytes in version=1

bpo-31334 [https://bugs.python.org/issue31334]: Fix poll.poll([timeout]) in the select module for arbitrary negative timeouts on all OSes where it can only be a non-negative integer or -1. Patch by Riccardo Coccioli.

bpo-31310 [https://bugs.python.org/issue31310]: multiprocessing’s semaphore tracker should be launched again if crashed.

bpo-31308 [https://bugs.python.org/issue31308]: Make multiprocessing’s forkserver process immune to Ctrl-C and other user interruptions. If it crashes, restart it when necessary.

文档

bpo-32105 [https://bugs.python.org/issue32105]: Added asyncio.BaseEventLoop.connect_accepted_socket versionadded marker.

bpo-31537 [https://bugs.python.org/issue31537]: Fix incorrect usage of get_history_length in readline documentation example code. Patch by Brad Smith.

bpo-30085 [https://bugs.python.org/issue30085]: The operator functions without double underscores are preferred for clarity. The one with underscores are only kept for back-compatibility.

测试

bpo-31380 [https://bugs.python.org/issue31380]: Skip test_httpservers test_undecodable_file on macOS: fails on APFS.

bpo-31705 [https://bugs.python.org/issue31705]: Skip test_socket.test_sha256() on Linux kernel older than 4.5. The test fails with ENOKEY on kernel 3.10 (on ppc64le). A fix was merged into the kernel 4.5.

bpo-31174 [https://bugs.python.org/issue31174]: Fix test_tools.test_unparse: DirectoryTestCase now stores the names sample to always test the same files. It prevents false alarms when hunting reference leaks.

bpo-30695 [https://bugs.python.org/issue30695]: Add the set_nomemory(start, stop) and remove_mem_hooks() functions to the _testcapi module.

构建

bpo-32059 [https://bugs.python.org/issue32059]: detect_modules() in setup.py now also searches the sysroot paths when cross-compiling.

bpo-31957 [https://bugs.python.org/issue31957]: Fixes Windows SDK version detection when building for Windows.

bpo-31609 [https://bugs.python.org/issue31609]: Fixes quotes in PCbuild/clean.bat

bpo-31934 [https://bugs.python.org/issue31934]: Abort the build when building out of a not clean source tree.

bpo-31926 [https://bugs.python.org/issue31926]: Fixed Argument Clinic sometimes causing compilation errors when there was more than one function and/or method in a .c file with the same name.

bpo-28791 [https://bugs.python.org/issue28791]: 更新 Windows 构建以使用 SQLite 3.21.0.

bpo-28791 [https://bugs.python.org/issue28791]: 更新 OS X 安装程序以使用 SQLite 3.21.0.

bpo-22140 [https://bugs.python.org/issue22140]: Prevent double substitution of prefix in python-config.sh.

bpo-31536 [https://bugs.python.org/issue31536]: Avoid wholesale rebuild after make regen-all if nothing changed.

Windows

bpo-1102 [https://bugs.python.org/issue1102]: Return None when View.Fetch() returns ERROR_NO_MORE_ITEMS instead of raising MSIError.

Initial patch by Anthony Tuininga.

bpo-31944 [https://bugs.python.org/issue31944]: Fixes Modify button in Apps and Features dialog.

macOS

bpo-31392 [https://bugs.python.org/issue31392]: 在MacOS安装程序中将OpenSSL更新为1.0.2m。

IDLE

bpo-32207 [https://bugs.python.org/issue32207]: Improve tk event exception tracebacks in IDLE. When tk event handling is driven by IDLE’s run loop, a confusing and distracting queue.EMPTY traceback context is no longer added to tk event exception tracebacks. The traceback is now the same as when event handling is driven by user code. Patch based on a suggestion by Serhiy Storchaka.

bpo-32164 [https://bugs.python.org/issue32164]: Delete unused file idlelib/tabbedpages.py. Use of TabbedPageSet in configdialog was replaced by ttk.Notebook.

bpo-32100 [https://bugs.python.org/issue32100]: IDLE: Fix old and new bugs in pathbrowser; improve tests. Patch mostly by Cheryl Sabella.

bpo-31858 [https://bugs.python.org/issue31858]: IDLE – Restrict shell prompt manipulation to the shell. Editor and output windows only see an empty last prompt line. This simplifies the code and fixes a minor bug when newline is inserted. Sys.ps1, if present, is read on Shell start-up, but is not set or changed.

bpo-31860 [https://bugs.python.org/issue31860]: The font sample in the IDLE configuration dialog is now editable. Changes persist while IDLE remains open

bpo-31836 [https://bugs.python.org/issue31836]: Test_code_module now passes if run after test_idle, which sets ps1.

The code module uses sys.ps1 if present or sets it to ‘>>> ‘ if not. Test_code_module now properly tests both behaviors. Ditto for ps2.

bpo-28603 [https://bugs.python.org/issue28603]: Fix a TypeError that caused a shell restart when printing a traceback that includes an exception that is unhashable. Patch by Zane Bitter.

bpo-13802 [https://bugs.python.org/issue13802]: Use non-Latin characters in the IDLE’s Font settings sample. Even if one selects a font that defines a limited subset of the unicode Basic Multilingual Plane, tcl/tk will use other fonts that define a character. The expanded example give users of non-Latin characters a better idea of what they might see in IDLE’s shell and editors. To make room for the expanded sample, frames on the Font tab are re-arranged. The Font/Tabs help explains a bit about the additions.

bpo-31460 [https://bugs.python.org/issue31460]: Simplify the API of IDLE’s Module Browser.

Passing a widget instead of an flist with a root widget opens the option of creating a browser frame that is only part of a window. Passing a full file name instead of pieces assumed to come from a .py file opens the possibility of browsing python files that do not end in .py.

bpo-31649 [https://bugs.python.org/issue31649]: IDLE - Make _htest, _utest parameters keyword only.

bpo-31559 [https://bugs.python.org/issue31559]: Remove test order dependence in idle_test.test_browser.

bpo-31459 [https://bugs.python.org/issue31459]: Rename IDLE’s module browser from Class Browser to Module Browser. The original module-level class and method browser became a module browser, with the addition of module-level functions, years ago. Nested classes and functions were added yesterday. For back-compatibility, the virtual event <<open-class-browser>>, which appears on the Keys tab of the Settings dialog, is not changed. Patch by Cheryl Sabella.

bpo-31500 [https://bugs.python.org/issue31500]: Default fonts now are scaled on HiDPI displays.

bpo-1612262 [https://bugs.python.org/issue1612262]: IDLE module browser now shows nested classes and functions. Original patches for code and tests by Guilherme Polo and Cheryl Sabella, respectively.

工具/示例

bpo-30722 [https://bugs.python.org/issue30722]: Make redemo work with Python 3.6 and newer versions.

Also, remove the LOCALE option since it doesn’t work with string patterns in Python 3.

Patch by Christoph Sarnowski.

C API

bpo-20891 [https://bugs.python.org/issue20891]: Fix PyGILState_Ensure(). When PyGILState_Ensure() is called in a non-Python thread before PyEval_InitThreads(), only call PyEval_InitThreads() after calling PyThreadState_New() to fix a crash.

bpo-31532 [https://bugs.python.org/issue31532]: Fix memory corruption due to allocator mix in getpath.c between Py_GetPath() and Py_SetPath()

bpo-30697 [https://bugs.python.org/issue30697]: The PyExc_RecursionErrorInst singleton is removed and PyErr_NormalizeException() does not use it anymore. This singleton is persistent and its members being never cleared may cause a segfault during finalization of the interpreter. See also bpo-22898 [https://bugs.python.org/issue22898].

Python 3.6.3 正式版

发布日期: 2017-10-03

库

bpo-31641 [https://bugs.python.org/issue31641]: Re-allow arbitrary iterables in concurrent.futures.as_completed(). Fixes regression in 3.6.3rc1.

构建

bpo-31662 [https://bugs.python.org/issue31662]: Fix typos in Windows uploadrelease.bat script. Fix Windows Doc build issues in Doc/make.bat.

bpo-31423 [https://bugs.python.org/issue31423]: Fix building the PDF documentation with newer versions of Sphinx.

Python 3.6.3 rc1

发布日期: 2017-09-18

安全

bpo-29781 [https://bugs.python.org/issue29781]: SSLObject.version() now correctly returns None when handshake over BIO has not been performed yet.

bpo-30947 [https://bugs.python.org/issue30947]: Upgrade libexpat embedded copy from version 2.2.1 to 2.2.3 to get security fixes.

核心与内置

bpo-31471 [https://bugs.python.org/issue31471]: Fix an assertion failure in subprocess.Popen() on Windows, in case the env argument has a bad keys() method. Patch by Oren Milman.

bpo-31418 [https://bugs.python.org/issue31418]: Fix an assertion failure in PyErr_WriteUnraisable() in case of an exception with a bad __module__ attribute. Patch by Oren Milman.

bpo-31416 [https://bugs.python.org/issue31416]: Fix assertion failures in case of a bad warnings.filters or warnings.defaultaction. Patch by Oren Milman.

bpo-31411 [https://bugs.python.org/issue31411]: Raise a TypeError instead of SystemError in case warnings.onceregistry is not a dictionary. Patch by Oren Milman.

bpo-31373 [https://bugs.python.org/issue31373]: Fix several possible instances of undefined behavior due to floating-point demotions.

bpo-30465 [https://bugs.python.org/issue30465]: Location information (lineno and col_offset) in f-strings is now (mostly) correct. This fixes tools like flake8 from showing warnings on the wrong line (typically the first line of the file).

bpo-31343 [https://bugs.python.org/issue31343]: Include sys/sysmacros.h for major(), minor(), and makedev(). GNU C libray plans to remove the functions from sys/types.h.

bpo-31291 [https://bugs.python.org/issue31291]: Fix an assertion failure in zipimport.zipimporter.get_data on Windows, when the return value of pathname.replace('/','\\') isn’t a string. Patch by Oren Milman.

bpo-31271 [https://bugs.python.org/issue31271]: Fix an assertion failure in the write() method of io.TextIOWrapper, when the encoder doesn’t return a bytes object. Patch by Oren Milman.

bpo-31243 [https://bugs.python.org/issue31243]: Fix a crash in some methods of io.TextIOWrapper, when the decoder’s state is invalid. Patch by Oren Milman.

bpo-30721 [https://bugs.python.org/issue30721]: print now shows correct usage hint for using Python 2 redirection syntax. Patch by Sanyam Khurana.

bpo-31070 [https://bugs.python.org/issue31070]: Fix a race condition in importlib _get_module_lock().

bpo-31095 [https://bugs.python.org/issue31095]: Fix potential crash during GC caused by tp_dealloc which doesn’t call PyObject_GC_UnTrack().

bpo-31071 [https://bugs.python.org/issue31071]: Avoid masking original TypeError in call with * unpacking when other arguments are passed.

bpo-30978 [https://bugs.python.org/issue30978]: str.format_map() now passes key lookup exceptions through. Previously any exception was replaced with a KeyError exception.

bpo-30808 [https://bugs.python.org/issue30808]: Use _Py_atomic API for concurrency-sensitive signal state.

bpo-30876 [https://bugs.python.org/issue30876]: Relative import from unloaded package now reimports the package instead of failing with SystemError. Relative import from non-package now fails with ImportError rather than SystemError.

bpo-30703 [https://bugs.python.org/issue30703]: Improve signal delivery.

Avoid using Py_AddPendingCall from signal handler, to avoid calling signal-unsafe functions. The tests I’m adding here fail without the rest of the patch, on Linux and OS X. This means our signal delivery logic had defects (some signals could be lost).

bpo-30765 [https://bugs.python.org/issue30765]: Avoid blocking in pthread_mutex_lock() when PyThread_acquire_lock() is asked not to block.

bpo-31161 [https://bugs.python.org/issue31161]: Make sure the ‘Missing parentheses’ syntax error message is only applied to SyntaxError, not to subclasses. Patch by Martijn Pieters.

bpo-30814 [https://bugs.python.org/issue30814]: Fixed a race condition when import a submodule from a package.

bpo-30597 [https://bugs.python.org/issue30597]: print now shows expected input in custom error message when used as a Python 2 statement. Patch by Sanyam Khurana.

库

bpo-31499 [https://bugs.python.org/issue31499]: xml.etree: Fix a crash when a parser is part of a reference cycle.

bpo-28556 [https://bugs.python.org/issue28556]: typing.get_type_hints now finds the right globalns for classes and modules by default (when no globalns was specified by the caller).

bpo-28556 [https://bugs.python.org/issue28556]: Speed improvements to the typing module. Original PRs by Ivan Levkivskyi and Mitar.

bpo-31544 [https://bugs.python.org/issue31544]: The C accelerator module of ElementTree ignored exceptions raised when looking up TreeBuilder target methods in XMLParser().

bpo-31234 [https://bugs.python.org/issue31234]: socket.create_connection() now fixes manually a reference cycle: clear the variable storing the last exception on success.

bpo-31457 [https://bugs.python.org/issue31457]: LoggerAdapter objects can now be nested.

bpo-31400 [https://bugs.python.org/issue31400]: Improves SSL error handling to avoid losing error numbers.

bpo-28958 [https://bugs.python.org/issue28958]: ssl.SSLContext() now uses OpenSSL error information when a context cannot be instantiated.

bpo-27340 [https://bugs.python.org/issue27340]: SSLSocket.sendall() now uses memoryview to create slices of data. This fixes support for all bytes-like object. It is also more efficient and avoids costly copies.

bpo-31178 [https://bugs.python.org/issue31178]: Fix string concatenation bug in rare error path in the subprocess module

bpo-31350 [https://bugs.python.org/issue31350]: Micro-optimize asyncio._get_running_loop() to become up to 10% faster.

bpo-31170 [https://bugs.python.org/issue31170]: expat: Update libexpat from 2.2.3 to 2.2.4. Fix copying of partial characters for UTF-8 input (libexpat bug 115): https://github.com/libexpat/libexpat/issues/115

bpo-29136 [https://bugs.python.org/issue29136]: Add TLS 1.3 cipher suites and OP_NO_TLSv1_3.

bpo-29212 [https://bugs.python.org/issue29212]: Fix concurrent.futures.thread.ThreadPoolExecutor threads to have a non repr() based thread name by default when no thread_name_prefix is supplied. They will now identify themselves as “ThreadPoolExecutor-y_n”.

bpo-9146 [https://bugs.python.org/issue9146]: Fix a segmentation fault in _hashopenssl when standard hash functions such as md5 are not available in the linked OpenSSL library. As in some special FIPS-140 build environments.

bpo-27144 [https://bugs.python.org/issue27144]: The map() and as_completed() iterators in concurrent.futures now avoid keeping a reference to yielded objects.

bpo-10746 [https://bugs.python.org/issue10746]: Fix ctypes producing wrong PEP 3118 type codes for integer types.

bpo-22536 [https://bugs.python.org/issue22536]: The subprocess module now sets the filename when FileNotFoundError is raised on POSIX systems due to the executable or cwd not being found.

bpo-31249 [https://bugs.python.org/issue31249]: concurrent.futures: WorkItem.run() used by ThreadPoolExecutor now breaks a reference cycle between an exception object and the WorkItem object.

bpo-31247 [https://bugs.python.org/issue31247]: xmlrpc.server now explicitly breaks reference cycles when using sys.exc_info() in code handling exceptions.

bpo-30102 [https://bugs.python.org/issue30102]: The ssl and hashlib modules now call OPENSSL_add_all_algorithms_noconf() on OpenSSL < 1.1.0. The function detects CPU features and enables optimizations on some CPU architectures such as POWER8. Patch is based on research from Gustavo Serra Scalet.

bpo-31185 [https://bugs.python.org/issue31185]: Fixed miscellaneous errors in asyncio speedup module.

bpo-31135 [https://bugs.python.org/issue31135]: ttk: fix the destroy() method of LabeledScale and OptionMenu classes. Call the parent destroy() method even if the used attribute doesn’t exist. The LabeledScale.destroy() method now also explicitly clears label and scale attributes to help the garbage collector to destroy all widgets.

bpo-31107 [https://bugs.python.org/issue31107]: Fix copyreg._slotnames() mangled attribute calculation for classes whose name begins with an underscore. Patch by Shane Harvey.

bpo-31061 [https://bugs.python.org/issue31061]: Fixed a crash when using asyncio and threads.

bpo-30502 [https://bugs.python.org/issue30502]: Fix handling of long oids in ssl. Based on patch by Christian Heimes.

bpo-30119 [https://bugs.python.org/issue30119]: ftplib.FTP.putline() now throws ValueError on commands that contains CR or LF. Patch by Dong-hee Na.

bpo-30595 [https://bugs.python.org/issue30595]: multiprocessing.Queue.get() with a timeout now polls its reader in non-blocking mode if it succeeded to acquire the lock but the acquire took longer than the timeout.

bpo-29403 [https://bugs.python.org/issue29403]: Fix unittest.mock’s autospec to not fail on method-bound builtin functions. Patch by Aaron Gallagher.

bpo-30961 [https://bugs.python.org/issue30961]: Fix decrementing a borrowed reference in tracemalloc.

bpo-25684 [https://bugs.python.org/issue25684]: Change ttk.OptionMenu radiobuttons to be unique across instances of OptionMenu.

bpo-30886 [https://bugs.python.org/issue30886]: Fix multiprocessing.Queue.join_thread(): it now waits until the thread completes, even if the thread was started by the same process which created the queue.

bpo-29854 [https://bugs.python.org/issue29854]: Fix segfault in readline when using readline’s history-size option. Patch by Nir Soffer.

bpo-30319 [https://bugs.python.org/issue30319]: socket.close() now ignores ECONNRESET error.

bpo-30828 [https://bugs.python.org/issue30828]: Fix out of bounds write in asyncio.CFuture.remove_done_callback().

bpo-30807 [https://bugs.python.org/issue30807]: signal.setitimer() may disable the timer when passed a tiny value.

Tiny values (such as 1e-6) are valid non-zero values for setitimer(), which is specified as taking microsecond-resolution intervals. However, on some platform, our conversion routine could convert 1e-6 into a zero interval, therefore disabling the timer instead of (re-)scheduling it.

bpo-30441 [https://bugs.python.org/issue30441]: Fix bug when modifying os.environ while iterating over it

bpo-30532 [https://bugs.python.org/issue30532]: Fix email header value parser dropping folding white space in certain cases.

bpo-30879 [https://bugs.python.org/issue30879]: os.listdir() and os.scandir() now emit bytes names when called with bytes-like argument.

bpo-30746 [https://bugs.python.org/issue30746]: Prohibited the ‘=’ character in environment variable names in os.putenv() and os.spawn*().

bpo-29755 [https://bugs.python.org/issue29755]: Fixed the lgettext() family of functions in the gettext module. They now always return bytes.

文档

bpo-31294 [https://bugs.python.org/issue31294]: Fix incomplete code snippet in the ZeroMQSocketListener and ZeroMQSocketHandler examples and adapt them to Python 3.

bpo-21649 [https://bugs.python.org/issue21649]: Add RFC 7525 and Mozilla server side TLS links to SSL documentation.

bpo-30803 [https://bugs.python.org/issue30803]: Clarify doc on truth value testing. Original patch by Peter Thomassen.

测试

bpo-31320 [https://bugs.python.org/issue31320]: Silence traceback in test_ssl

bpo-25674 [https://bugs.python.org/issue25674]: Remove sha256.tbs-internet.com ssl test

bpo-30715 [https://bugs.python.org/issue30715]: Address ALPN callback changes for OpenSSL 1.1.0f. The latest version behaves like OpenSSL 1.0.2 and no longer aborts handshake.

bpo-30822 [https://bugs.python.org/issue30822]: regrtest: Exclude tzdata from regrtest –all. When running the test suite using –use=all / -u all, exclude tzdata since it makes test_datetime too slow (15-20 min on some buildbots) which then times out on some buildbots. Fix also regrtest command line parser to allow passing -u extralargefile to run test_zipfile64.

构建

bpo-30854 [https://bugs.python.org/issue30854]: Fix compile error when compiling –without-threads. Patch by Masayuki Yamamoto.

Windows

bpo-30389 [https://bugs.python.org/issue30389]: Adds detection of Visual Studio 2017 to distutils on Windows.

bpo-31340 [https://bugs.python.org/issue31340]: Change to building with MSVC v141 (included with Visual Studio 2017)

bpo-30581 [https://bugs.python.org/issue30581]: os.cpu_count() now returns the correct number of processors on Windows when the number of logical processors is greater than 64.

bpo-30731 [https://bugs.python.org/issue30731]: Add a missing xmlns to python.manifest so that it matches the schema.

IDLE

bpo-31493 [https://bugs.python.org/issue31493]: IDLE code context – fix code update and font update timers.

Canceling timers prevents a warning message when test_idle completes.

bpo-31488 [https://bugs.python.org/issue31488]: IDLE - Update non-key options in former extension classes. When applying configdialog changes, call .reload for each feature class. Change ParenMatch so updated options affect existing instances attached to existing editor windows.

bpo-31477 [https://bugs.python.org/issue31477]: IDLE - Improve rstrip entry in doc. Strip trailing whitespace strips more than blank spaces. Multiline string literals are not skipped.

bpo-31480 [https://bugs.python.org/issue31480]: IDLE - make tests pass with zzdummy extension disabled by default.

bpo-31421 [https://bugs.python.org/issue31421]: Document how IDLE runs tkinter programs. IDLE calls tcl/tk update in the background in order to make live

interaction and experimentation with tkinter applications much easier.

bpo-31414 [https://bugs.python.org/issue31414]: IDLE – fix tk entry box tests by deleting first. Adding to an int entry is not the same as deleting and inserting because int(‘’) will fail.

bpo-31051 [https://bugs.python.org/issue31051]: Rearrange IDLE configdialog GenPage into Window, Editor, and Help sections.

bpo-30617 [https://bugs.python.org/issue30617]: IDLE - Add docstrings and tests for outwin subclass of editor.

Move some data and functions from the class to module level. Patch by Cheryl Sabella.

bpo-31287 [https://bugs.python.org/issue31287]: IDLE - Do not modify tkinter.message in test_configdialog.

bpo-27099 [https://bugs.python.org/issue27099]: Convert IDLE’s built-in ‘extensions’ to regular features.

About 10 IDLE features were implemented as supposedly optional extensions. Their different behavior could be confusing or worse for users and not good for maintenance. Hence the conversion.

The main difference for users is that user configurable key bindings for builtin features are now handled uniformly. Now, editing a binding in a keyset only affects its value in the keyset. All bindings are defined together in the system-specific default keysets in config-extensions.def. All custom keysets are saved as a whole in config-extension.cfg. All take effect as soon as one clicks Apply or Ok.

The affected events are ‘<<force-open-completions>>’, ‘<<expand-word>>’, ‘<<force-open-calltip>>’, ‘<<flash-paren>>’, ‘<<format-paragraph>>’, ‘<<run-module>>’, ‘<<check-module>>’, and ‘<<zoom-height>>’. Any (global) customizations made before 3.6.3 will not affect their keyset-specific customization after 3.6.3. and vice versa.

Inital patch by Charles Wohlganger.

bpo-31206 [https://bugs.python.org/issue31206]: IDLE: Factor HighPage(Frame) class from ConfigDialog. Patch by Cheryl Sabella.

bpo-31001 [https://bugs.python.org/issue31001]: Add tests for configdialog highlight tab. Patch by Cheryl Sabella.

bpo-31205 [https://bugs.python.org/issue31205]: IDLE: Factor KeysPage(Frame) class from ConfigDialog. The slightly modified tests continue to pass. Patch by Cheryl Sabella.

bpo-31130 [https://bugs.python.org/issue31130]: IDLE – stop leaks in test_configdialog. Initial patch by Victor Stinner.

bpo-31002 [https://bugs.python.org/issue31002]: Add tests for configdialog keys tab. Patch by Cheryl Sabella.

bpo-19903 [https://bugs.python.org/issue19903]: IDLE: Calltips use inspect.signature instead of inspect.getfullargspec. This improves calltips for builtins converted to use Argument Clinic. Patch by Louie Lu.

bpo-31083 [https://bugs.python.org/issue31083]: IDLE - Add an outline of a TabPage class in configdialog. Update existing classes to match outline. Initial patch by Cheryl Sabella.

bpo-31050 [https://bugs.python.org/issue31050]: Factor GenPage(Frame) class from ConfigDialog. The slightly modified tests continue to pass. Patch by Cheryl Sabella.

bpo-31004 [https://bugs.python.org/issue31004]: IDLE - Factor FontPage(Frame) class from ConfigDialog.

Slightly modified tests continue to pass. Fix General tests. Patch mostly by Cheryl Sabella.

bpo-30781 [https://bugs.python.org/issue30781]: IDLE - Use ttk widgets in ConfigDialog. Patches by Terry Jan Reedy and Cheryl Sabella.

bpo-31060 [https://bugs.python.org/issue31060]: IDLE - Finish rearranging methods of ConfigDialog Grouping methods pertaining to each tab and the buttons will aid writing tests and improving the tabs and will enable splitting the groups into classes.

bpo-30853 [https://bugs.python.org/issue30853]: IDLE – Factor a VarTrace class out of ConfigDialog.

Instance tracers manages pairs consisting of a tk variable and a callback function. When tracing is turned on, setting the variable calls the function. Test coverage for the new class is 100%.

bpo-31003 [https://bugs.python.org/issue31003]: IDLE: Add more tests for General tab.

bpo-30993 [https://bugs.python.org/issue30993]: IDLE - Improve configdialog font page and tests.

In configdialog: Document causal pathways in create_font_tab docstring. Simplify some attribute names. Move set_samples calls to var_changed_font (idea from Cheryl Sabella). Move related functions to positions after the create widgets function.

In test_configdialog: Fix test_font_set so not order dependent. Fix renamed test_indent_scale so it tests the widget. Adjust tests for movement of set_samples call. Add tests for load functions. Put all font tests in one class and tab indent tests in another. Except for two lines, these tests completely cover the related functions.

bpo-30981 [https://bugs.python.org/issue30981]: IDLE – Add more configdialog font page tests.

bpo-28523 [https://bugs.python.org/issue28523]: IDLE: replace ‘colour’ with ‘color’ in configdialog.

bpo-30917 [https://bugs.python.org/issue30917]: Add tests for idlelib.config.IdleConf. Increase coverage from 46% to 96%. Patch by Louie Lu.

bpo-30934 [https://bugs.python.org/issue30934]: Document coverage details for idlelib tests.

Add section to idlelib/idle-test/README.txt.

Include check that branches are taken both ways.

Exclude IDLE-specific code that does not run during unit tests.

bpo-30913 [https://bugs.python.org/issue30913]: IDLE: Document ConfigDialog tk Vars, methods, and widgets in docstrings This will facilitate improving the dialog and splitting up the class. Original patch by Cheryl Sabella.

bpo-30899 [https://bugs.python.org/issue30899]: IDLE: Add tests for ConfigParser subclasses in config. Patch by Louie Lu.

bpo-30881 [https://bugs.python.org/issue30881]: IDLE: Add docstrings to browser.py. Patch by Cheryl Sabella.

bpo-30851 [https://bugs.python.org/issue30851]: IDLE: Remove unused variables in configdialog. One is a duplicate, one is set but cannot be altered by users. Patch by Cheryl Sabella.

bpo-30870 [https://bugs.python.org/issue30870]: IDLE: In Settings dialog, select font with Up, Down keys as well as mouse. Initial patch by Louie Lu.

bpo-8231 [https://bugs.python.org/issue8231]: IDLE: call config.IdleConf.GetUserCfgDir only once.

bpo-30779 [https://bugs.python.org/issue30779]: IDLE: Factor ConfigChanges class from configdialog, put in config; test. * In config, put dump test code in a function; run it and unittest in ‘if __name__ == ‘__main__’. * Add class config.ConfigChanges based on changes_class_v4.py on bpo issue. * Add class test_config.ChangesTest, partly using configdialog_tests_v1.py. * Revise configdialog to use ConfigChanges; see tracker msg297804. * Revise test_configdialog to match configdialog changes. * Remove configdialog functions unused or moved to ConfigChanges. Cheryl Sabella contributed parts of the patch.

bpo-30777 [https://bugs.python.org/issue30777]: IDLE: configdialog - Add docstrings and fix comments. Patch by Cheryl Sabella.

bpo-30495 [https://bugs.python.org/issue30495]: IDLE: Improve textview with docstrings, PEP8 names, and more tests. Patch by Cheryl Sabella.

bpo-30723 [https://bugs.python.org/issue30723]: IDLE: Make several improvements to parenmatch. Add ‘parens’ style to highlight both opener and closer. Make ‘default’ style, which is not default, a synonym for ‘opener’. Make time-delay work the same with all styles. Add help for config dialog extensions tab, including help for parenmatch. Add new tests. Original patch by Charles Wohlganger.

bpo-30674 [https://bugs.python.org/issue30674]: IDLE: add docstrings to grep module. Patch by Cheryl Sabella

bpo-21519 [https://bugs.python.org/issue21519]: IDLE’s basic custom key entry dialog now detects duplicates properly. Original patch by Saimadhav Heblikar.

bpo-29910 [https://bugs.python.org/issue29910]: IDLE no longer deletes a character after commenting out a region by a key shortcut. Add return 'break' for this and other potential conflicts between IDLE and default key bindings.

bpo-30728 [https://bugs.python.org/issue30728]: Review and change idlelib.configdialog names. Lowercase method and attribute names. Replace ‘colour’ with ‘color’, expand overly cryptic names, delete unneeded underscores. Replace import * with specific imports. Patches by Cheryl Sabella.

bpo-6739 [https://bugs.python.org/issue6739]: IDLE: Verify user-entered key sequences by trying to bind them with tk. Add tests for all 3 validation functions. Original patch by G Polo. Tests added by Cheryl Sabella.

工具/示例

bpo-30983 [https://bugs.python.org/issue30983]: gdb integration commands (py-bt, etc.) work on optimized shared builds now, too. PEP 523 introduced _PyEval_EvalFrameDefault which inlines PyEval_EvalFrameEx on non-debug shared builds. This broke the ability to use py-bt, py-up, and a few other Python-specific gdb integrations. The problem is fixed by only looking for _PyEval_EvalFrameDefault frames in python-gdb.py. Original patch by Bruno “Polaco” Penteado.

Python 3.6.2 正式版

发布日期: 2017-07-17

No changes since release candidate 2

Python 3.6.2 rc2

发布日期: 2017-07-07

安全

bpo-30730 [https://bugs.python.org/issue30730]: Prevent environment variables injection in subprocess on Windows. Prevent passing other environment variables and command arguments.

bpo-30694 [https://bugs.python.org/issue30694]: Upgrade expat copy from 2.2.0 to 2.2.1 to get fixes of multiple security vulnerabilities including: CVE-2017-9233 (External entity infinite loop DoS), CVE-2016-9063 (Integer overflow, re-fix), CVE-2016-0718 (Fix regression bugs from 2.2.0’s fix to CVE-2016-0718) and CVE-2012-0876 (Counter hash flooding with SipHash). Note: the CVE-2016-5300 (Use os-specific entropy sources like getrandom) doesn’t impact Python, since Python already gets entropy from the OS to set the expat secret using XML_SetHashSalt().

bpo-30500 [https://bugs.python.org/issue30500]: Fix urllib.parse.splithost() to correctly parse fragments. For example, splithost('//127.0.0.1#@evil.com/') now correctly returns the 127.0.0.1 host, instead of treating @evil.com as the host in an authentication (login@host).

Python 3.6.2 rc1

发布日期: 2017-06-17

核心与内置

bpo-30682 [https://bugs.python.org/issue30682]: Removed a too-strict assertion that failed for certain f-strings, such as eval(“f’\n’”) and eval(“f’\r’”).

bpo-30604 [https://bugs.python.org/issue30604]: Move co_extra_freefuncs to not be per-thread to avoid crashes

bpo-29104 [https://bugs.python.org/issue29104]: Fixed parsing backslashes in f-strings.

bpo-27945 [https://bugs.python.org/issue27945]: Fixed various segfaults with dict when input collections are mutated during searching, inserting or comparing. Based on patches by Duane Griffin and Tim Mitchell.

bpo-25794 [https://bugs.python.org/issue25794]: Fixed type.__setattr__() and type.__delattr__() for non-interned attribute names. Based on patch by Eryk Sun.

bpo-30039 [https://bugs.python.org/issue30039]: If a KeyboardInterrupt happens when the interpreter is in the middle of resuming a chain of nested ‘yield from’ or ‘await’ calls, it’s now correctly delivered to the innermost frame.

bpo-12414 [https://bugs.python.org/issue12414]: sys.getsizeof() on a code object now returns the sizes which includes the code struct and sizes of objects which it references. Patch by Dong-hee Na.

bpo-29949 [https://bugs.python.org/issue29949]: Fix memory usage regression of set and frozenset object.

bpo-29935 [https://bugs.python.org/issue29935]: Fixed error messages in the index() method of tuple, list and deque when pass indices of wrong type.

bpo-29859 [https://bugs.python.org/issue29859]: Show correct error messages when any of the pthread_* calls in thread_pthread.h fails.

bpo-28876 [https://bugs.python.org/issue28876]: bool(range) works even if len(range) raises OverflowError.

bpo-29600 [https://bugs.python.org/issue29600]: Fix wrapping coroutine return values in StopIteration.

bpo-28856 [https://bugs.python.org/issue28856]: Fix an oversight that %b format for bytes should support objects follow the buffer protocol.

bpo-29714 [https://bugs.python.org/issue29714]: Fix a regression that bytes format may fail when containing zero bytes inside.

bpo-29478 [https://bugs.python.org/issue29478]: If max_line_length=None is specified while using the Compat32 policy, it is no longer ignored. Patch by Mircea Cosbuc.

库

bpo-30616 [https://bugs.python.org/issue30616]: Functional API of enum allows to create empty enums. Patched by Dong-hee Na

bpo-30038 [https://bugs.python.org/issue30038]: Fix race condition between signal delivery and wakeup file descriptor. Patch by Nathaniel Smith.

bpo-23894 [https://bugs.python.org/issue23894]: lib2to3 now recognizes rb'...' and f'...' strings.

bpo-23890 [https://bugs.python.org/issue23890]: unittest.TestCase.assertRaises() now manually breaks a reference cycle to not keep objects alive longer than expected.

bpo-30149 [https://bugs.python.org/issue30149]: inspect.signature() now supports callables with variable-argument parameters wrapped with partialmethod. Patch by Dong-hee Na.

bpo-30645 [https://bugs.python.org/issue30645]: Fix path calculation in imp.load_package(), fixing it for cases when a package is only shipped with bytecodes. Patch by Alexandru Ardelean.

bpo-29931 [https://bugs.python.org/issue29931]: Fixed comparison check for ipaddress.ip_interface objects. Patch by Sanjay Sundaresan.

bpo-30605 [https://bugs.python.org/issue30605]: re.compile() no longer raises a BytesWarning when compiling a bytes instance with misplaced inline modifier. Patch by Roy Williams.

安全

bpo-29591 [https://bugs.python.org/issue29591]: Update expat copy from 2.1.1 to 2.2.0 to get fixes of CVE-2016-0718 and CVE-2016-4472. See https://sourceforge.net/p/expat/bugs/537/ for more information.

库

bpo-24484 [https://bugs.python.org/issue24484]: Avoid race condition in multiprocessing cleanup (#2159)

bpo-28994 [https://bugs.python.org/issue28994]: The traceback no longer displayed for SystemExit raised in a callback registered by atexit.

bpo-30508 [https://bugs.python.org/issue30508]: Don’t log exceptions if Task/Future “cancel()” method was called.

bpo-28556 [https://bugs.python.org/issue28556]: Updates to typing module: Add generic AsyncContextManager, add support for ContextManager on all versions. Original PRs by Jelle Zijlstra and Ivan Levkivskyi

bpo-29870 [https://bugs.python.org/issue29870]: Fix ssl sockets leaks when connection is aborted in asyncio/ssl implementation. Patch by Michaël Sghaïer.

bpo-29743 [https://bugs.python.org/issue29743]: Closing transport during handshake process leaks open socket. Patch by Nikolay Kim

bpo-27585 [https://bugs.python.org/issue27585]: Fix waiter cancellation in asyncio.Lock. Patch by Mathieu Sornay.

bpo-30418 [https://bugs.python.org/issue30418]: On Windows, subprocess.Popen.communicate() now also ignore EINVAL on stdin.write() if the child process is still running but closed the pipe.

bpo-29822 [https://bugs.python.org/issue29822]: inspect.isabstract() now works during __init_subclass__. Patch by Nate Soares.

bpo-29581 [https://bugs.python.org/issue29581]: ABCMeta.__new__ now accepts **kwargs, allowing abstract base classes to use keyword parameters in __init_subclass__. Patch by Nate Soares.

bpo-30557 [https://bugs.python.org/issue30557]: faulthandler now correctly filters and displays exception codes on Windows

bpo-30378 [https://bugs.python.org/issue30378]: Fix the problem that logging.handlers.SysLogHandler cannot handle IPv6 addresses.

bpo-29960 [https://bugs.python.org/issue29960]: Preserve generator state when _random.Random.setstate() raises an exception. Patch by Bryan Olson.

bpo-30414 [https://bugs.python.org/issue30414]: multiprocessing.Queue._feed background running thread do not break from main loop on exception.

bpo-30003 [https://bugs.python.org/issue30003]: Fix handling escape characters in HZ codec. Based on patch by Ma Lin.

bpo-30301 [https://bugs.python.org/issue30301]: Fix AttributeError when using SimpleQueue.empty() under spawn and forkserver start methods.

bpo-30329 [https://bugs.python.org/issue30329]: imaplib and poplib now catch the Windows socket WSAEINVAL error (code 10022) on shutdown(SHUT_RDWR): An invalid operation was attempted. This error occurs sometimes on SSL connections.

bpo-30375 [https://bugs.python.org/issue30375]: Warnings emitted when compile a regular expression now always point to the line in the user code. Previously they could point into inners of the re module if emitted from inside of groups or conditionals.

bpo-30048 [https://bugs.python.org/issue30048]: Fixed Task.cancel() can be ignored when the task is running coroutine and the coroutine returned without any more await.

bpo-30266 [https://bugs.python.org/issue30266]: contextlib.AbstractContextManager now supports anti-registration by setting __enter__ = None or __exit__ = None, following the pattern introduced in bpo-25958 [https://bugs.python.org/issue25958]. Patch by Jelle Zijlstra.

bpo-30298 [https://bugs.python.org/issue30298]: Weaken the condition of deprecation warnings for inline modifiers. Now allowed several subsequential inline modifiers at the start of the pattern (e.g. '(?i)(?s)...'). In verbose mode whitespaces and comments now are allowed before and between inline modifiers (e.g. '(?x) (?i) (?s)...').

bpo-29990 [https://bugs.python.org/issue29990]: Fix range checking in GB18030 decoder. Original patch by Ma Lin.

bpo-26293 [https://bugs.python.org/issue26293]: Change resulted because of zipfile breakage. (See also: bpo-29094 [https://bugs.python.org/issue29094])

bpo-30243 [https://bugs.python.org/issue30243]: Removed the __init__ methods of _json’s scanner and encoder. Misusing them could cause memory leaks or crashes. Now scanner and encoder objects are completely initialized in the __new__ methods.

bpo-30185 [https://bugs.python.org/issue30185]: Avoid KeyboardInterrupt tracebacks in forkserver helper process when Ctrl-C is received.

bpo-28556 [https://bugs.python.org/issue28556]: Various updates to typing module: add typing.NoReturn type, use WrapperDescriptorType, minor bug-fixes. Original PRs by Jim Fasarakis-Hilliard and Ivan Levkivskyi.

bpo-30205 [https://bugs.python.org/issue30205]: Fix getsockname() for unbound AF_UNIX sockets on Linux.

bpo-30070 [https://bugs.python.org/issue30070]: Fixed leaks and crashes in errors handling in the parser module.

bpo-30061 [https://bugs.python.org/issue30061]: Fixed crashes in IOBase methods __next__() and readlines() when readline() or __next__() respectively return non-sizeable object. Fixed possible other errors caused by not checking results of PyObject_Size(), PySequence_Size(), or PyMapping_Size().

bpo-30017 [https://bugs.python.org/issue30017]: Allowed calling the close() method of the zip entry writer object multiple times. Writing to a closed writer now always produces a ValueError.

bpo-30068 [https://bugs.python.org/issue30068]: _io._IOBase.readlines will check if it’s closed first when hint is present.

bpo-29694 [https://bugs.python.org/issue29694]: Fixed race condition in pathlib mkdir with flags parents=True. Patch by Armin Rigo.

bpo-29692 [https://bugs.python.org/issue29692]: Fixed arbitrary unchaining of RuntimeError exceptions in contextlib.contextmanager. Patch by Siddharth Velankar.

bpo-29998 [https://bugs.python.org/issue29998]: Pickling and copying ImportError now preserves name and path attributes.

bpo-29953 [https://bugs.python.org/issue29953]: Fixed memory leaks in the replace() method of datetime and time objects when pass out of bound fold argument.

bpo-29942 [https://bugs.python.org/issue29942]: Fix a crash in itertools.chain.from_iterable when encountering long runs of empty iterables.

bpo-27863 [https://bugs.python.org/issue27863]: Fixed multiple crashes in ElementTree caused by race conditions and wrong types.

bpo-28699 [https://bugs.python.org/issue28699]: Fixed a bug in pools in multiprocessing.pool that raising an exception at the very first of an iterable may swallow the exception or make the program hang. Patch by Davin Potts and Xiang Zhang.

bpo-25803 [https://bugs.python.org/issue25803]: Avoid incorrect errors raised by Path.mkdir(exist_ok=True) when the OS gives priority to errors such as EACCES over EEXIST.

bpo-29861 [https://bugs.python.org/issue29861]: Release references to tasks, their arguments and their results as soon as they are finished in multiprocessing.Pool.

bpo-29884 [https://bugs.python.org/issue29884]: faulthandler: Restore the old sigaltstack during teardown. Patch by Christophe Zeitouny.

bpo-25455 [https://bugs.python.org/issue25455]: Fixed crashes in repr of recursive buffered file-like objects.

bpo-29800 [https://bugs.python.org/issue29800]: Fix crashes in partial.__repr__ if the keys of partial.keywords are not strings. Patch by Michael Seifert.

bpo-29742 [https://bugs.python.org/issue29742]: get_extra_info() raises exception if get called on closed ssl transport. Patch by Nikolay Kim.

bpo-8256 [https://bugs.python.org/issue8256]: Fixed possible failing or crashing input() if attributes “encoding” or “errors” of sys.stdin or sys.stdout are not set or are not strings.

bpo-28298 [https://bugs.python.org/issue28298]: Fix a bug that prevented array ‘Q’, ‘L’ and ‘I’ from accepting big intables (objects that have __int__) as elements. Patch by Oren Milman.

bpo-28231 [https://bugs.python.org/issue28231]: The zipfile module now accepts path-like objects for external paths.

bpo-26915 [https://bugs.python.org/issue26915]: index() and count() methods of collections.abc.Sequence now check identity before checking equality when do comparisons.

bpo-29615 [https://bugs.python.org/issue29615]: SimpleXMLRPCDispatcher no longer chains KeyError (or any other exception) to exception(s) raised in the dispatched methods. Patch by Petr Motejlek.

bpo-30177 [https://bugs.python.org/issue30177]: path.resolve(strict=False) no longer cuts the path after the first element not present in the filesystem. Patch by Antoine Pietri.

IDLE

bpo-15786 [https://bugs.python.org/issue15786]: Fix several problems with IDLE’s autocompletion box. The following should now work: clicking on selection box items; using the scrollbar; selecting an item by hitting Return. Hangs on MacOSX should no longer happen. Patch by Louie Lu.

bpo-25514 [https://bugs.python.org/issue25514]: Add doc subsubsection about IDLE failure to start. Popup no-connection message directs users to this section.

bpo-30642 [https://bugs.python.org/issue30642]: Fix reference leaks in IDLE tests. Patches by Louie Lu and Terry Jan Reedy.

bpo-30495 [https://bugs.python.org/issue30495]: Add docstrings for textview.py and use PEP8 names. Patches by Cheryl Sabella and Terry Jan Reedy.

bpo-30290 [https://bugs.python.org/issue30290]: Help-about: use pep8 names and add tests. Increase coverage to 100%. Patches by Louie Lu, Cheryl Sabella, and Terry Jan Reedy.

bpo-30303 [https://bugs.python.org/issue30303]: Add _utest option to textview; add new tests. Increase coverage to 100%. Patches by Louie Lu and Terry Jan Reedy.

C API

bpo-27867 [https://bugs.python.org/issue27867]: Function PySlice_GetIndicesEx() no longer replaced with a macro if Py_LIMITED_API is not set.

构建

bpo-29941 [https://bugs.python.org/issue29941]: Add --with-assertions configure flag to explicitly enable C assert() checks. Defaults to off. --with-pydebug implies --with-assertions.

bpo-28787 [https://bugs.python.org/issue28787]: Fix out-of-tree builds of Python when configured with --with--dtrace.

bpo-29243 [https://bugs.python.org/issue29243]: Prevent unnecessary rebuilding of Python during make test, make install and some other make targets when configured with --enable-optimizations.

bpo-23404 [https://bugs.python.org/issue23404]: Don’t regenerate generated files based on file modification time anymore: the action is now explicit. Replace make touch with make regen-all.

bpo-29643 [https://bugs.python.org/issue29643]: Fix --enable-optimization didn’t work.

文档

bpo-30176 [https://bugs.python.org/issue30176]: Add missing attribute related constants in curses documentation.

bpo-30052 [https://bugs.python.org/issue30052]: the link targets for bytes() and bytearray() are now their respective type definitions, rather than the corresponding builtin function entries. Use bytes and bytearray to reference the latter.

In order to ensure this and future cross-reference updates are applied automatically, the daily documentation builds now disable the default output caching features in Sphinx.

bpo-26985 [https://bugs.python.org/issue26985]: Add missing info of code object in inspect documentation.

工具/示例

bpo-29367 [https://bugs.python.org/issue29367]: python-gdb.py now supports also method-wrapper (wrapperobject) objects.

测试

bpo-30357 [https://bugs.python.org/issue30357]: test_thread: setUp() now uses support.threading_setup() and support.threading_cleanup() to wait until threads complete to avoid random side effects on following tests. Initial patch written by Grzegorz Grzywacz.

bpo-30197 [https://bugs.python.org/issue30197]: Enhanced functions swap_attr() and swap_item() in the test.support module. They now work when delete replaced attribute or item inside the with statement. The old value of the attribute or item (or None if it doesn’t exist) now will be assigned to the target of the “as” clause, if there is one.

Windows

bpo-30687 [https://bugs.python.org/issue30687]: Locate msbuild.exe on Windows when building rather than vcvarsall.bat

bpo-30450 [https://bugs.python.org/issue30450]: The build process on Windows no longer depends on Subversion, instead pulling external code from GitHub via a Python script. If Python 3.6 is not found on the system (via py -3.6), NuGet is used to download a copy of 32-bit Python.

Python 3.6.1 正式版

发布日期: 2017-03-21

核心与内置

bpo-29723 [https://bugs.python.org/issue29723]: The sys.path[0] initialization change for bpo-29139 [https://bugs.python.org/issue29139] caused a regression by revealing an inconsistency in how sys.path is initialized when executing __main__ from a zipfile, directory, or other import location. The interpreter now consistently avoids ever adding the import location’s parent directory to sys.path, and ensures no other sys.path entries are inadvertently modified when inserting the import location named on the command line.

构建

bpo-27593 [https://bugs.python.org/issue27593]: fix format of git information used in sys.version

Fix incompatible comment in python.h

Python 3.6.1 rc1

发布日期: 2017-03-04

核心与内置

bpo-28893 [https://bugs.python.org/issue28893]: Set correct __cause__ for errors about invalid awaitables returned from __aiter__ and __anext__.

bpo-29683 [https://bugs.python.org/issue29683]: Fixes to memory allocation in _PyCode_SetExtra. Patch by Brian Coleman.

bpo-29684 [https://bugs.python.org/issue29684]: Fix minor regression of PyEval_CallObjectWithKeywords. It should raise TypeError when kwargs is not a dict. But it might cause segv when args=NULL and kwargs is not a dict.

bpo-28598 [https://bugs.python.org/issue28598]: Support __rmod__ for subclasses of str being called before str.__mod__. Patch by Martijn Pieters.

bpo-29607 [https://bugs.python.org/issue29607]: Fix stack_effect computation for CALL_FUNCTION_EX. Patch by Matthieu Dartiailh.

bpo-29602 [https://bugs.python.org/issue29602]: Fix incorrect handling of signed zeros in complex constructor for complex subclasses and for inputs having a __complex__ method. Patch by Serhiy Storchaka.

bpo-29347 [https://bugs.python.org/issue29347]: Fixed possibly dereferencing undefined pointers when creating weakref objects.

bpo-29438 [https://bugs.python.org/issue29438]: Fixed use-after-free problem in key sharing dict.

bpo-29319 [https://bugs.python.org/issue29319]: Prevent RunMainFromImporter overwriting sys.path[0].

bpo-29337 [https://bugs.python.org/issue29337]: Fixed possible BytesWarning when compare the code objects. Warnings could be emitted at compile time.

bpo-29327 [https://bugs.python.org/issue29327]: Fixed a crash when pass the iterable keyword argument to sorted().

bpo-29034 [https://bugs.python.org/issue29034]: Fix memory leak and use-after-free in os module (path_converter).

bpo-29159 [https://bugs.python.org/issue29159]: Fix regression in bytes(x) when x.__index__() raises Exception.

bpo-28932 [https://bugs.python.org/issue28932]: Do not include <sys/random.h> if it does not exist.

bpo-25677 [https://bugs.python.org/issue25677]: Correct the positioning of the syntax error caret for indented blocks. Based on patch by Michael Layzell.

bpo-29000 [https://bugs.python.org/issue29000]: Fixed bytes formatting of octals with zero padding in alternate form.

bpo-26919 [https://bugs.python.org/issue26919]: On Android, operating system data is now always encoded/decoded to/from UTF-8, instead of the locale encoding to avoid inconsistencies with os.fsencode() and os.fsdecode() which are already using UTF-8.

bpo-28991 [https://bugs.python.org/issue28991]: functools.lru_cache() was susceptible to an obscure reentrancy bug triggerable by a monkey-patched len() function.

bpo-28739 [https://bugs.python.org/issue28739]: f-string expressions are no longer accepted as docstrings and by ast.literal_eval() even if they do not include expressions.

bpo-28512 [https://bugs.python.org/issue28512]: Fixed setting the offset attribute of SyntaxError by PyErr_SyntaxLocationEx() and PyErr_SyntaxLocationObject().

bpo-28918 [https://bugs.python.org/issue28918]: Fix the cross compilation of xxlimited when Python has been built with Py_DEBUG defined.

bpo-28731 [https://bugs.python.org/issue28731]: Optimize _PyDict_NewPresized() to create correct size dict. Improve speed of dict literal with constant keys up to 30%.

库

bpo-29169 [https://bugs.python.org/issue29169]: Update zlib to 1.2.11.

bpo-29623 [https://bugs.python.org/issue29623]: Allow use of path-like object as a single argument in ConfigParser.read(). Patch by David Ellis.

bpo-28963 [https://bugs.python.org/issue28963]: Fix out of bound iteration in asyncio.Future.remove_done_callback implemented in C.

bpo-29704 [https://bugs.python.org/issue29704]: asyncio.subprocess.SubprocessStreamProtocol no longer closes before all pipes are closed.

bpo-29271 [https://bugs.python.org/issue29271]: Fix Task.current_task and Task.all_tasks implemented in C to accept None argument as their pure Python implementation.

bpo-29703 [https://bugs.python.org/issue29703]: Fix asyncio to support instantiation of new event loops in child processes.

bpo-29376 [https://bugs.python.org/issue29376]: Fix assertion error in threading._DummyThread.is_alive().

bpo-28624 [https://bugs.python.org/issue28624]: Add a test that checks that cwd parameter of Popen() accepts PathLike objects. Patch by Sayan Chowdhury.

bpo-28518 [https://bugs.python.org/issue28518]: Start a transaction implicitly before a DML statement. Patch by Aviv Palivoda.

bpo-29532 [https://bugs.python.org/issue29532]: Altering a kwarg dictionary passed to functools.partial() no longer affects a partial object after creation.

bpo-29110 [https://bugs.python.org/issue29110]: Fix file object leak in aifc.open() when file is given as a filesystem path and is not in valid AIFF format. Patch by Anthony Zhang.

bpo-28556 [https://bugs.python.org/issue28556]: Various updates to typing module: typing.Counter, typing.ChainMap, improved ABC caching, etc. Original PRs by Jelle Zijlstra, Ivan Levkivskyi, Manuel Krebber, and Łukasz Langa.

bpo-29100 [https://bugs.python.org/issue29100]: Fix datetime.fromtimestamp() regression introduced in Python 3.6.0: check minimum and maximum years.

bpo-29519 [https://bugs.python.org/issue29519]: Fix weakref spewing exceptions during interpreter shutdown when used with a rare combination of multiprocessing and custom codecs.

bpo-29416 [https://bugs.python.org/issue29416]: Prevent infinite loop in pathlib.Path.mkdir

bpo-29444 [https://bugs.python.org/issue29444]: Fixed out-of-bounds buffer access in the group() method of the match object. Based on patch by WGH.

bpo-29335 [https://bugs.python.org/issue29335]: Fix subprocess.Popen.wait() when the child process has exited to a stopped instead of terminated state (ex: when under ptrace).

bpo-29290 [https://bugs.python.org/issue29290]: Fix a regression in argparse that help messages would wrap at non-breaking spaces.

bpo-28735 [https://bugs.python.org/issue28735]: Fixed the comparison of mock.MagickMock with mock.ANY.

bpo-29316 [https://bugs.python.org/issue29316]: Restore the provisional status of typing module, add corresponding note to documentation. Patch by Ivan L.

bpo-29219 [https://bugs.python.org/issue29219]: Fixed infinite recursion in the repr of uninitialized ctypes.CDLL instances.

bpo-29011 [https://bugs.python.org/issue29011]: Fix an important omission by adding Deque to the typing module.

bpo-28969 [https://bugs.python.org/issue28969]: Fixed race condition in C implementation of functools.lru_cache. KeyError could be raised when cached function with full cache was simultaneously called from differen threads with the same uncached arguments.

bpo-29142 [https://bugs.python.org/issue29142]: In urllib.request, suffixes in no_proxy environment variable with leading dots could match related hostnames again (e.g. .b.c matches a.b.c). Patch by Milan Oberkirch.

bpo-28961 [https://bugs.python.org/issue28961]: Fix unittest.mock._Call helper: don’t ignore the name parameter anymore. Patch written by Jiajun Huang.

bpo-29203 [https://bugs.python.org/issue29203]: functools.lru_cache() now respects PEP 468 and preserves the order of keyword arguments. f(a=1, b=2) is now cached separately from f(b=2, a=1) since both calls could potentially give different results.

bpo-15812 [https://bugs.python.org/issue15812]: inspect.getframeinfo() now correctly shows the first line of a context. Patch by Sam Breese.

bpo-29094 [https://bugs.python.org/issue29094]: Offsets in a ZIP file created with extern file object and modes “w” and “x” now are relative to the start of the file.

bpo-29085 [https://bugs.python.org/issue29085]: Allow random.Random.seed() to use high quality OS randomness rather than the pid and time.

bpo-29061 [https://bugs.python.org/issue29061]: Fixed bug in secrets.randbelow() which would hang when given a negative input. Patch by Brendan Donegan.

bpo-29079 [https://bugs.python.org/issue29079]: Prevent infinite loop in pathlib.resolve() on Windows

bpo-13051 [https://bugs.python.org/issue13051]: Fixed recursion errors in large or resized curses.textpad.Textbox. Based on patch by Tycho Andersen.

bpo-29119 [https://bugs.python.org/issue29119]: Fix weakrefs in the pure python version of collections.OrderedDict move_to_end() method. Contributed by Andra Bogildea.

bpo-9770 [https://bugs.python.org/issue9770]: curses.ascii predicates now work correctly with negative integers.

bpo-28427 [https://bugs.python.org/issue28427]: old keys should not remove new values from WeakValueDictionary when collecting from another thread.

bpo-28923 [https://bugs.python.org/issue28923]: Remove editor artifacts from Tix.py.

bpo-29055 [https://bugs.python.org/issue29055]: Neaten-up empty population error on random.choice() by suppressing the upstream exception.

bpo-28871 [https://bugs.python.org/issue28871]: Fixed a crash when deallocate deep ElementTree.

bpo-19542 [https://bugs.python.org/issue19542]: Fix bugs in WeakValueDictionary.setdefault() and WeakValueDictionary.pop() when a GC collection happens in another thread.

bpo-20191 [https://bugs.python.org/issue20191]: Fixed a crash in resource.prlimit() when passing a sequence that doesn’t own its elements as limits.

bpo-28779 [https://bugs.python.org/issue28779]: multiprocessing.set_forkserver_preload() would crash the forkserver process if a preloaded module instantiated some multiprocessing objects such as locks.

bpo-28847 [https://bugs.python.org/issue28847]: dbm.dumb now supports reading read-only files and no longer writes the index file when it is not changed.

bpo-26937 [https://bugs.python.org/issue26937]: The chown() method of the tarfile.TarFile class does not fail now when the grp module cannot be imported, as for example on Android platforms.

IDLE

bpo-29071 [https://bugs.python.org/issue29071]: IDLE colors f-string prefixes (but not invalid ur prefixes).

bpo-28572 [https://bugs.python.org/issue28572]: Add 10% to coverage of IDLE’s test_configdialog. Update and augment description of the configuration system.

Windows

bpo-29579 [https://bugs.python.org/issue29579]: Removes readme.txt from the installer

bpo-29326 [https://bugs.python.org/issue29326]: Ignores blank lines in ._pth files (Patch by Alexey Izbyshev)

bpo-28164 [https://bugs.python.org/issue28164]: Correctly handle special console filenames (patch by Eryk Sun)

bpo-29409 [https://bugs.python.org/issue29409]: Implement PEP 529 for io.FileIO (Patch by Eryk Sun)

bpo-29392 [https://bugs.python.org/issue29392]: Prevent crash when passing invalid arguments into msvcrt module.

bpo-25778 [https://bugs.python.org/issue25778]: winreg does not truncate string correctly (Patch by Eryk Sun)

bpo-28896 [https://bugs.python.org/issue28896]: Deprecate WindowsRegistryFinder and disable it by default.

C API

bpo-27867 [https://bugs.python.org/issue27867]: Function PySlice_GetIndicesEx() is replaced with a macro if Py_LIMITED_API is not set or set to the value between 0x03050400 and 0x03060000 (not including) or 0x03060100 or higher.

bpo-29083 [https://bugs.python.org/issue29083]: Fixed the declaration of some public API functions. PyArg_VaParse() and PyArg_VaParseTupleAndKeywords() were not available in limited API. PyArg_ValidateKeywordArguments(), PyArg_UnpackTuple() and Py_BuildValue() were not available in limited API of version < 3.3 when PY_SSIZE_T_CLEAN is defined.

bpo-29058 [https://bugs.python.org/issue29058]: All stable API extensions added after Python 3.2 are now available only when Py_LIMITED_API is set to the PY_VERSION_HEX value of the minimum Python version supporting this API.

文档

bpo-28929 [https://bugs.python.org/issue28929]: Link the documentation to its source file on GitHub.

bpo-25008 [https://bugs.python.org/issue25008]: Document smtpd.py as effectively deprecated and add a pointer to aiosmtpd, a third-party asyncio-based replacement.

bpo-26355 [https://bugs.python.org/issue26355]: Add canonical header link on each page to corresponding major version of the documentation. Patch by Matthias Bussonnier.

bpo-29349 [https://bugs.python.org/issue29349]: Fix Python 2 syntax in code for building the documentation.

测试

bpo-28087 [https://bugs.python.org/issue28087]: Skip test_asyncore and test_eintr poll failures on macOS. Skip some tests of select.poll when running on macOS due to unresolved issues with the underlying system poll function on some macOS versions.

bpo-29571 [https://bugs.python.org/issue29571]: to match the behaviour of the re.LOCALE flag, test_re.test_locale_flag now uses locale.getpreferredencoding(False) to determine the candidate encoding for the test regex (allowing it to correctly skip the test when the default locale encoding is a multi-byte encoding)

bpo-28950 [https://bugs.python.org/issue28950]: Disallow -j0 to be combined with -T/-l in regrtest command line arguments.

bpo-28683 [https://bugs.python.org/issue28683]: Fix the tests that bind() a unix socket and raise PermissionError on Android for a non-root user.

bpo-26939 [https://bugs.python.org/issue26939]: Add the support.setswitchinterval() function to fix test_functools hanging on the Android armv7 qemu emulator.

构建

bpo-27593 [https://bugs.python.org/issue27593]: sys.version and the platform module python_build(), python_branch(), and python_revision() functions now use git information rather than hg when building from a repo.

bpo-29572 [https://bugs.python.org/issue29572]: 更新Windows 构建和OS X安装程序以使用OpenSSL 1.0.2k。

bpo-26851 [https://bugs.python.org/issue26851]: Set Android compilation and link flags.

bpo-28768 [https://bugs.python.org/issue28768]: Fix implicit declaration of function _setmode. Patch by Masayuki Yamamoto

bpo-29080 [https://bugs.python.org/issue29080]: Removes hard dependency on hg.exe from PCBuild/build.bat

bpo-23903 [https://bugs.python.org/issue23903]: Added missed names to PC/python3.def.

bpo-28762 [https://bugs.python.org/issue28762]: lockf() is available on Android API level 24, but the F_LOCK macro is not defined in android-ndk-r13.

bpo-28538 [https://bugs.python.org/issue28538]: Fix the compilation error that occurs because if_nameindex() is available on Android API level 24, but the if_nameindex structure is not defined.

bpo-20211 [https://bugs.python.org/issue20211]: Do not add the directory for installing C header files and the directory for installing object code libraries to the cross compilation search paths. Original patch by Thomas Petazzoni.

bpo-28849 [https://bugs.python.org/issue28849]: Do not define sys.implementation._multiarch on Android.

Python 3.6.0 正式版

发布日期: 2016-12-23

No changes since release candidate 2

Python 3.6.0 rc2

发布日期: 2016-12-16

核心与内置

bpo-28147 [https://bugs.python.org/issue28147]: Fix a memory leak in split-table dictionaries: setattr() must not convert combined table into split table. Patch written by INADA Naoki.

bpo-28990 [https://bugs.python.org/issue28990]: Fix asyncio SSL hanging if connection is closed before handshake is completed. (Patch by HoHo-Ho)

工具/示例

bpo-28770 [https://bugs.python.org/issue28770]: Fix python-gdb.py for fastcalls.

Windows

bpo-28896 [https://bugs.python.org/issue28896]: Deprecate WindowsRegistryFinder.

构建

bpo-28898 [https://bugs.python.org/issue28898]: Prevent gdb build errors due to HAVE_LONG_LONG redefinition.

Python 3.6.0 rc1

发布日期: 2016-12-06

核心与内置

bpo-23722 [https://bugs.python.org/issue23722]: Rather than silently producing a class that doesn’t support zero-argument super() in methods, failing to pass the new __classcell__ namespace entry up to type.__new__ now results in a DeprecationWarning and a class that supports zero-argument super().

bpo-28797 [https://bugs.python.org/issue28797]: Modifying the class __dict__ inside the __set_name__ method of a descriptor that is used inside that class no longer prevents calling the __set_name__ method of other descriptors.

bpo-28782 [https://bugs.python.org/issue28782]: Fix a bug in the implementation yield from when checking if the next instruction is YIELD_FROM. Regression introduced by WORDCODE (bpo-26647 [https://bugs.python.org/issue26647]).

库

bpo-27030 [https://bugs.python.org/issue27030]: Unknown escapes in re.sub() replacement template are allowed again. But they still are deprecated and will be disabled in 3.7.

bpo-28835 [https://bugs.python.org/issue28835]: Fix a regression introduced in warnings.catch_warnings(): call warnings.showwarning() if it was overriden inside the context manager.

bpo-27172 [https://bugs.python.org/issue27172]: To assist with upgrades from 2.7, the previously documented deprecation of inspect.getfullargspec() has been reversed. This decision may be revisited again after the Python 2.7 branch is no longer officially supported.

bpo-26273 [https://bugs.python.org/issue26273]: Add new socket.TCP_CONGESTION (Linux 2.6.13) and socket.TCP_USER_TIMEOUT (Linux 2.6.37) constants. Patch written by Omar Sandoval.

bpo-24142 [https://bugs.python.org/issue24142]: Reading a corrupt config file left configparser in an invalid state. Original patch by Florian Höch.

bpo-28843 [https://bugs.python.org/issue28843]: Fix asyncio C Task to handle exceptions __traceback__.

C API

bpo-28808 [https://bugs.python.org/issue28808]: PyUnicode_CompareWithASCIIString() now never raises exceptions.

文档

bpo-23722 [https://bugs.python.org/issue23722]: The data model reference and the porting section in the What’s New guide now cover the additional __classcell__ handling needed for custom metaclasses to fully support PEP 487 and zero-argument super().

工具/示例

bpo-28023 [https://bugs.python.org/issue28023]: Fix python-gdb.py didn’t support new dict implementation.

Python 3.6.0 beta 4

发布日期: 2016-11-21

核心与内置

bpo-28532 [https://bugs.python.org/issue28532]: Show sys.version when -V option is supplied twice.

bpo-27100 [https://bugs.python.org/issue27100]: The with-statement now checks for __enter__ before it checks for __exit__. This gives less confusing error messages when both methods are missing. Patch by Jonathan Ellington.

bpo-28746 [https://bugs.python.org/issue28746]: Fix the set_inheritable() file descriptor method on platforms that do not have the ioctl FIOCLEX and FIONCLEX commands.

bpo-26920 [https://bugs.python.org/issue26920]: Fix not getting the locale’s charset upon initializing the interpreter, on platforms that do not have langinfo.

bpo-28648 [https://bugs.python.org/issue28648]: Fixed crash in Py_DecodeLocale() in debug build on Mac OS X when decode astral characters. Patch by Xiang Zhang.

bpo-19398 [https://bugs.python.org/issue19398]: Extra slash no longer added to sys.path components in case of empty compile-time PYTHONPATH components.

bpo-28665 [https://bugs.python.org/issue28665]: Improve speed of the STORE_DEREF opcode by 40%.

bpo-28583 [https://bugs.python.org/issue28583]: PyDict_SetDefault didn’t combine split table when needed. Patch by Xiang Zhang.

bpo-27243 [https://bugs.python.org/issue27243]: Change PendingDeprecationWarning -> DeprecationWarning. As it was agreed in the issue, __aiter__ returning an awaitable should result in PendingDeprecationWarning in 3.5 and in DeprecationWarning in 3.6.

bpo-26182 [https://bugs.python.org/issue26182]: Fix a refleak in code that raises DeprecationWarning.

bpo-28721 [https://bugs.python.org/issue28721]: Fix asynchronous generators aclose() and athrow() to handle StopAsyncIteration propagation properly.

库

bpo-28752 [https://bugs.python.org/issue28752]: Restored the __reduce__() methods of datetime objects.

bpo-28727 [https://bugs.python.org/issue28727]: Regular expression patterns, _sre.SRE_Pattern objects created by re.compile(), become comparable (only x==y and x!=y operators). This change should fix the bpo-18383 [https://bugs.python.org/issue18383]: don’t duplicate warning filters when the warnings module is reloaded (thing usually only done in unit tests).

bpo-20572 [https://bugs.python.org/issue20572]: The subprocess.Popen.wait method’s undocumented endtime parameter now raises a DeprecationWarning.

bpo-25659 [https://bugs.python.org/issue25659]: In ctypes, prevent a crash calling the from_buffer() and from_buffer_copy() methods on abstract classes like Array.

bpo-19717 [https://bugs.python.org/issue19717]: Makes Path.resolve() succeed on paths that do not exist. Patch by Vajrasky Kok

bpo-28563 [https://bugs.python.org/issue28563]: Fixed possible DoS and arbitrary code execution when handle plural form selections in the gettext module. The expression parser now supports exact syntax supported by GNU gettext.

bpo-28387 [https://bugs.python.org/issue28387]: Fixed possible crash in _io.TextIOWrapper deallocator when the garbage collector is invoked in other thread. Based on patch by Sebastian Cufre.

bpo-28600 [https://bugs.python.org/issue28600]: Optimize loop.call_soon.

bpo-28613 [https://bugs.python.org/issue28613]: Fix get_event_loop() return the current loop if called from coroutines/callbacks.

bpo-28634 [https://bugs.python.org/issue28634]: Fix asyncio.isfuture() to support unittest.Mock.

bpo-26081 [https://bugs.python.org/issue26081]: Fix refleak in _asyncio.Future.__iter__().throw.

bpo-28639 [https://bugs.python.org/issue28639]: Fix inspect.isawaitable to always return bool Patch by Justin Mayfield.

bpo-28652 [https://bugs.python.org/issue28652]: Make loop methods reject socket kinds they do not support.

bpo-28653 [https://bugs.python.org/issue28653]: Fix a refleak in functools.lru_cache.

bpo-28703 [https://bugs.python.org/issue28703]: Fix asyncio.iscoroutinefunction to handle Mock objects.

bpo-28704 [https://bugs.python.org/issue28704]: Fix create_unix_server to support Path-like objects (PEP 519).

bpo-28720 [https://bugs.python.org/issue28720]: Add collections.abc.AsyncGenerator.

文档

bpo-28513 [https://bugs.python.org/issue28513]: Documented command-line interface of zipfile.

测试

bpo-28666 [https://bugs.python.org/issue28666]: Now test.support.rmtree is able to remove unwritable or unreadable directories.

bpo-23839 [https://bugs.python.org/issue23839]: Various caches now are cleared before running every test file.

构建

bpo-10656 [https://bugs.python.org/issue10656]: Fix out-of-tree building on AIX. Patch by Tristan Carel and Michael Haubenwallner.

bpo-26359 [https://bugs.python.org/issue26359]: Rename –with-optimiations to –enable-optimizations.

bpo-28676 [https://bugs.python.org/issue28676]: Prevent missing ‘getentropy’ declaration warning on macOS. Patch by Gareth Rees.

Python 3.6.0 beta 3

发布日期: 2016-10-31

核心与内置

bpo-28128 [https://bugs.python.org/issue28128]: Deprecation warning for invalid str and byte escape sequences now prints better information about where the error occurs. Patch by Serhiy Storchaka and Eric Smith.

bpo-28509 [https://bugs.python.org/issue28509]: dict.update() no longer allocate unnecessary large memory.

bpo-28426 [https://bugs.python.org/issue28426]: Fixed potential crash in PyUnicode_AsDecodedObject() in debug build.

bpo-28517 [https://bugs.python.org/issue28517]: Fixed of-by-one error in the peephole optimizer that caused keeping unreachable code.

bpo-28214 [https://bugs.python.org/issue28214]: Improved exception reporting for problematic __set_name__ attributes.

bpo-23782 [https://bugs.python.org/issue23782]: Fixed possible memory leak in _PyTraceback_Add() and exception loss in PyTraceBack_Here().

bpo-28471 [https://bugs.python.org/issue28471]: Fix “Python memory allocator called without holding the GIL” crash in socket.setblocking.

库

bpo-27517 [https://bugs.python.org/issue27517]: LZMA compressor and decompressor no longer raise exceptions if given empty data twice. Patch by Benjamin Fogle.

bpo-28549 [https://bugs.python.org/issue28549]: Fixed segfault in curses’s addch() with ncurses6.

bpo-28449 [https://bugs.python.org/issue28449]: tarfile.open() with mode “r” or “r:” now tries to open a tar file with compression before trying to open it without compression. Otherwise it had 50% chance failed with ignore_zeros=True.

bpo-23262 [https://bugs.python.org/issue23262]: The webbrowser module now supports Firefox 36+ and derived browsers. Based on patch by Oleg Broytman.

bpo-27939 [https://bugs.python.org/issue27939]: Fixed bugs in tkinter.ttk.LabeledScale and tkinter.Scale caused by representing the scale as float value internally in Tk. tkinter.IntVar now works if float value is set to underlying Tk variable.

bpo-18844 [https://bugs.python.org/issue18844]: The various ways of specifying weights for random.choices() now produce the same result sequences.

bpo-28255 [https://bugs.python.org/issue28255]: calendar.TextCalendar().prmonth() no longer prints a space at the start of new line after printing a month’s calendar. Patch by Xiang Zhang.

bpo-20491 [https://bugs.python.org/issue20491]: The textwrap.TextWrapper class now honors non-breaking spaces. Based on patch by Kaarle Ritvanen.

bpo-28353 [https://bugs.python.org/issue28353]: os.fwalk() no longer fails on broken links.

bpo-28430 [https://bugs.python.org/issue28430]: Fix iterator of C implemented asyncio.Future doesn’t accept non-None value is passed to it.send(val).

bpo-27025 [https://bugs.python.org/issue27025]: Generated names for Tkinter widgets now start by the “!” prefix for readability.

bpo-25464 [https://bugs.python.org/issue25464]: Fixed HList.header_exists() in tkinter.tix module by addin a workaround to Tix library bug.

bpo-28488 [https://bugs.python.org/issue28488]: shutil.make_archive() no longer adds entry “./” to ZIP archive.

bpo-25953 [https://bugs.python.org/issue25953]: re.sub() now raises an error for invalid numerical group reference in replacement template even if the pattern is not found in the string. Error message for invalid group reference now includes the group index and the position of the reference. Based on patch by SilentGhost.

bpo-18219 [https://bugs.python.org/issue18219]: Optimize csv.DictWriter for large number of columns. Patch by Mariatta Wijaya.

bpo-28448 [https://bugs.python.org/issue28448]: Fix C implemented asyncio.Future didn’t work on Windows.

bpo-28480 [https://bugs.python.org/issue28480]: Fix error building socket module when multithreading is disabled.

bpo-24452 [https://bugs.python.org/issue24452]: Make webbrowser support Chrome on Mac OS X.

bpo-20766 [https://bugs.python.org/issue20766]: Fix references leaked by pdb in the handling of SIGINT handlers.

bpo-28492 [https://bugs.python.org/issue28492]: Fix how StopIteration exception is raised in _asyncio.Future.

bpo-28500 [https://bugs.python.org/issue28500]: Fix asyncio to handle async gens GC from another thread.

bpo-26923 [https://bugs.python.org/issue26923]: Fix asyncio.Gather to refuse being cancelled once all children are done. Patch by Johannes Ebke.

bpo-26796 [https://bugs.python.org/issue26796]: Don’t configure the number of workers for default threadpool executor. Initial patch by Hans Lawrenz.

bpo-28544 [https://bugs.python.org/issue28544]: Implement asyncio.Task in C.

Windows

bpo-28522 [https://bugs.python.org/issue28522]: Fixes mishandled buffer reallocation in getpathp.c

构建

bpo-28444 [https://bugs.python.org/issue28444]: Fix missing extensions modules when cross compiling.

bpo-28208 [https://bugs.python.org/issue28208]: 更新Windows 构建和OS X 安装程序以使用SQLite 3.14.2.

bpo-28248 [https://bugs.python.org/issue28248]: 更新Windows 构建和OS X安装程序以使用OpenSSL 1.0.2j.

测试

bpo-26944 [https://bugs.python.org/issue26944]: Fix test_posix for Android where ‘id -G’ is entirely wrong or missing the effective gid.

bpo-28409 [https://bugs.python.org/issue28409]: regrtest: fix the parser of command line arguments.

Python 3.6.0 beta 2

发布日期: 2016-10-10

核心与内置

bpo-28183 [https://bugs.python.org/issue28183]: Optimize and cleanup dict iteration.

bpo-26081 [https://bugs.python.org/issue26081]: Added C implementation of asyncio.Future. Original patch by Yury Selivanov.

bpo-28379 [https://bugs.python.org/issue28379]: Added sanity checks and tests for PyUnicode_CopyCharacters(). Patch by Xiang Zhang.

bpo-28376 [https://bugs.python.org/issue28376]: The type of long range iterator is now registered as Iterator. Patch by Oren Milman.

bpo-28376 [https://bugs.python.org/issue28376]: Creating instances of range_iterator by calling range_iterator type now is deprecated. Patch by Oren Milman.

bpo-28376 [https://bugs.python.org/issue28376]: The constructor of range_iterator now checks that step is not 0. Patch by Oren Milman.

bpo-26906 [https://bugs.python.org/issue26906]: Resolving special methods of uninitialized type now causes implicit initialization of the type instead of a fail.

bpo-18287 [https://bugs.python.org/issue18287]: PyType_Ready() now checks that tp_name is not NULL. Original patch by Niklas Koep.

bpo-24098 [https://bugs.python.org/issue24098]: Fixed possible crash when AST is changed in process of compiling it.

bpo-28201 [https://bugs.python.org/issue28201]: Dict reduces possibility of 2nd conflict in hash table when hashes have same lower bits.

bpo-28350 [https://bugs.python.org/issue28350]: String constants with null character no longer interned.

bpo-26617 [https://bugs.python.org/issue26617]: Fix crash when GC runs during weakref callbacks.

bpo-27942 [https://bugs.python.org/issue27942]: String constants now interned recursively in tuples and frozensets.

bpo-21578 [https://bugs.python.org/issue21578]: Fixed misleading error message when ImportError called with invalid keyword args.

bpo-28203 [https://bugs.python.org/issue28203]: Fix incorrect type in complex(1.0, {2:3}) error message. Patch by Soumya Sharma.

bpo-28086 [https://bugs.python.org/issue28086]: Single var-positional argument of tuple subtype was passed unscathed to the C-defined function. Now it is converted to exact tuple.

bpo-28214 [https://bugs.python.org/issue28214]: Now __set_name__ is looked up on the class instead of the instance.

bpo-27955 [https://bugs.python.org/issue27955]: Fallback on reading /dev/urandom device when the getrandom() syscall fails with EPERM, for example when blocked by SECCOMP.

bpo-28192 [https://bugs.python.org/issue28192]: Don’t import readline in isolated mode.

Upgrade internal unicode databases to Unicode version 9.0.0.

bpo-28131 [https://bugs.python.org/issue28131]: Fix a regression in zipimport’s compile_source(). zipimport should use the same optimization level as the interpreter.

bpo-28126 [https://bugs.python.org/issue28126]: Replace Py_MEMCPY with memcpy(). Visual Studio can properly optimize memcpy().

bpo-28120 [https://bugs.python.org/issue28120]: Fix dict.pop() for splitted dictionary when trying to remove a “pending key” (Not yet inserted in split-table). Patch by Xiang Zhang.

bpo-26182 [https://bugs.python.org/issue26182]: Raise DeprecationWarning when async and await keywords are used as variable/attribute/class/function name.

库

bpo-27998 [https://bugs.python.org/issue27998]: Fixed bytes path support in os.scandir() on Windows. Patch by Eryk Sun.

bpo-28317 [https://bugs.python.org/issue28317]: The disassembler now decodes FORMAT_VALUE argument.

bpo-26293 [https://bugs.python.org/issue26293]: Fixed writing ZIP files that starts not from the start of the file. Offsets in ZIP file now are relative to the start of the archive in conforming to the specification.

bpo-28380 [https://bugs.python.org/issue28380]: unittest.mock Mock autospec functions now properly support assert_called, assert_not_called, and assert_called_once.

bpo-27181 [https://bugs.python.org/issue27181]: remove statistics.geometric_mean and defer until 3.7.

bpo-28229 [https://bugs.python.org/issue28229]: lzma module now supports pathlib.

bpo-28321 [https://bugs.python.org/issue28321]: Fixed writing non-BMP characters with binary format in plistlib.

bpo-28225 [https://bugs.python.org/issue28225]: bz2 module now supports pathlib. Initial patch by Ethan Furman.

bpo-28227 [https://bugs.python.org/issue28227]: gzip now supports pathlib. Patch by Ethan Furman.

bpo-27358 [https://bugs.python.org/issue27358]: Optimized merging var-keyword arguments and improved error message when passing a non-mapping as a var-keyword argument.

bpo-28257 [https://bugs.python.org/issue28257]: Improved error message when passing a non-iterable as a var-positional argument. Added opcode BUILD_TUPLE_UNPACK_WITH_CALL.

bpo-28322 [https://bugs.python.org/issue28322]: Fixed possible crashes when unpickle itertools objects from incorrect pickle data. Based on patch by John Leitch.

bpo-28228 [https://bugs.python.org/issue28228]: imghdr now supports pathlib.

bpo-28226 [https://bugs.python.org/issue28226]: compileall now supports pathlib.

bpo-28314 [https://bugs.python.org/issue28314]: Fix function declaration (C flags) for the getiterator() method of xml.etree.ElementTree.Element.

bpo-28148 [https://bugs.python.org/issue28148]: Stop using localtime() and gmtime() in the time module.

Introduced platform independent _PyTime_localtime API that is similar to POSIX localtime_r, but available on all platforms. Patch by Ed Schouten.

bpo-28253 [https://bugs.python.org/issue28253]: Fixed calendar functions for extreme months: 0001-01 and 9999-12.

Methods itermonthdays() and itermonthdays2() are reimplemented so that they don’t call itermonthdates() which can cause datetime.date under/overflow.

bpo-28275 [https://bugs.python.org/issue28275]: Fixed possible use after free in the decompress() methods of the LZMADecompressor and BZ2Decompressor classes. Original patch by John Leitch.

bpo-27897 [https://bugs.python.org/issue27897]: Fixed possible crash in sqlite3.Connection.create_collation() if pass invalid string-like object as a name. Patch by Xiang Zhang.

bpo-18844 [https://bugs.python.org/issue18844]: random.choices() now has k as a keyword-only argument to improve the readability of common cases and come into line with the signature used in other languages.

bpo-18893 [https://bugs.python.org/issue18893]: Fix invalid exception handling in Lib/ctypes/macholib/dyld.py. Patch by Madison May.

bpo-27611 [https://bugs.python.org/issue27611]: Fixed support of default root window in the tkinter.tix module. Added the master parameter in the DisplayStyle constructor.

bpo-27348 [https://bugs.python.org/issue27348]: In the traceback module, restore the formatting of exception messages like “Exception: None”. This fixes a regression introduced in 3.5a2.

bpo-25651 [https://bugs.python.org/issue25651]: Allow falsy values to be used for msg parameter of subTest().

bpo-27778 [https://bugs.python.org/issue27778]: Fix a memory leak in os.getrandom() when the getrandom() is interrupted by a signal and a signal handler raises a Python exception.

bpo-28200 [https://bugs.python.org/issue28200]: Fix memory leak on Windows in the os module (fix path_converter() function).

bpo-25400 [https://bugs.python.org/issue25400]: RobotFileParser now correctly returns default values for crawl_delay and request_rate. Initial patch by Peter Wirtz.

bpo-27932 [https://bugs.python.org/issue27932]: Prevent memory leak in win32_ver().

Fix UnboundLocalError in socket._sendfile_use_sendfile.

bpo-28075 [https://bugs.python.org/issue28075]: Check for ERROR_ACCESS_DENIED in Windows implementation of os.stat(). Patch by Eryk Sun.

bpo-22493 [https://bugs.python.org/issue22493]: Warning message emitted by using inline flags in the middle of regular expression now contains a (truncated) regex pattern. Patch by Tim Graham.

bpo-25270 [https://bugs.python.org/issue25270]: Prevent codecs.escape_encode() from raising SystemError when an empty bytestring is passed.

bpo-28181 [https://bugs.python.org/issue28181]: Get antigravity over HTTPS. Patch by Kaartic Sivaraam.

bpo-25895 [https://bugs.python.org/issue25895]: Enable WebSocket URL schemes in urllib.parse.urljoin. Patch by Gergely Imreh and Markus Holtermann.

bpo-28114 [https://bugs.python.org/issue28114]: Fix a crash in parse_envlist() when env contains byte strings. Patch by Eryk Sun.

bpo-27599 [https://bugs.python.org/issue27599]: Fixed buffer overrun in binascii.b2a_qp() and binascii.a2b_qp().

bpo-27906 [https://bugs.python.org/issue27906]: Fix socket accept exhaustion during high TCP traffic. Patch by Kevin Conway.

bpo-28174 [https://bugs.python.org/issue28174]: Handle when SO_REUSEPORT isn’t properly supported. Patch by Seth Michael Larson.

bpo-26654 [https://bugs.python.org/issue26654]: Inspect functools.partial in asyncio.Handle.__repr__. Patch by iceboy.

bpo-26909 [https://bugs.python.org/issue26909]: Fix slow pipes IO in asyncio. Patch by INADA Naoki.

bpo-28176 [https://bugs.python.org/issue28176]: Fix callbacks race in asyncio.SelectorLoop.sock_connect.

bpo-27759 [https://bugs.python.org/issue27759]: Fix selectors incorrectly retain invalid file descriptors. Patch by Mark Williams.

bpo-28368 [https://bugs.python.org/issue28368]: Refuse monitoring processes if the child watcher has no loop attached. Patch by Vincent Michel.

bpo-28369 [https://bugs.python.org/issue28369]: Raise RuntimeError when transport’s FD is used with add_reader, add_writer, etc.

bpo-28370 [https://bugs.python.org/issue28370]: Speedup asyncio.StreamReader.readexactly. Patch by Коренберг Марк.

bpo-28371 [https://bugs.python.org/issue28371]: Deprecate passing asyncio.Handles to run_in_executor.

bpo-28372 [https://bugs.python.org/issue28372]: Fix asyncio to support formatting of non-python coroutines.

bpo-28399 [https://bugs.python.org/issue28399]: Remove UNIX socket from FS before binding. Patch by Коренберг Марк.

bpo-27972 [https://bugs.python.org/issue27972]: Prohibit Tasks to await on themselves.

Windows

bpo-28402 [https://bugs.python.org/issue28402]: Adds signed catalog files for stdlib on Windows.

bpo-28333 [https://bugs.python.org/issue28333]: Enables Unicode for ps1/ps2 and input() prompts. (Patch by Eryk Sun)

bpo-28251 [https://bugs.python.org/issue28251]: Improvements to help manuals on Windows.

bpo-28110 [https://bugs.python.org/issue28110]: launcher.msi has different product codes between 32-bit and 64-bit

bpo-28161 [https://bugs.python.org/issue28161]: Opening CON for write access fails

bpo-28162 [https://bugs.python.org/issue28162]: WindowsConsoleIO readall() fails if first line starts with Ctrl+Z

bpo-28163 [https://bugs.python.org/issue28163]: WindowsConsoleIO fileno() passes wrong flags to _open_osfhandle

bpo-28164 [https://bugs.python.org/issue28164]: _PyIO_get_console_type fails for various paths

bpo-28137 [https://bugs.python.org/issue28137]: Renames Windows path file to ._pth

bpo-28138 [https://bugs.python.org/issue28138]: Windows ._pth file should allow import site

C API

bpo-28426 [https://bugs.python.org/issue28426]: Deprecated undocumented functions PyUnicode_AsEncodedObject(), PyUnicode_AsDecodedObject(), PyUnicode_AsDecodedUnicode() and PyUnicode_AsEncodedUnicode().

构建

bpo-28258 [https://bugs.python.org/issue28258]: Fixed build with Estonian locale (python-config and distclean targets in Makefile). Patch by Arfrever Frehtes Taifersar Arahesis.

bpo-26661 [https://bugs.python.org/issue26661]: setup.py now detects system libffi with multiarch wrapper.

bpo-15819 [https://bugs.python.org/issue15819]: Remove redundant include search directory option for building outside the source tree.

测试

bpo-28217 [https://bugs.python.org/issue28217]: Adds _testconsole module to test console input.

Python 3.6.0 beta 1

发布日期： 2016-09-12

核心与内置

bpo-23722 [https://bugs.python.org/issue23722]: The __class__ cell used by zero-argument super() is now initialized from type.__new__ rather than __build_class__, so class methods relying on that will now work correctly when called from metaclass methods during class creation. Patch by Martin Teichmann.

bpo-25221 [https://bugs.python.org/issue25221]: Fix corrupted result from PyLong_FromLong(0) when Python is compiled with NSMALLPOSINTS = 0.

bpo-27080 [https://bugs.python.org/issue27080]: Implement formatting support for PEP 515. Initial patch by Chris Angelico.

bpo-27199 [https://bugs.python.org/issue27199]: In tarfile, expose copyfileobj bufsize to improve throughput. Patch by Jason Fried.

bpo-27948 [https://bugs.python.org/issue27948]: In f-strings, only allow backslashes inside the braces (where the expressions are). This is a breaking change from the 3.6 alpha releases, where backslashes are allowed anywhere in an f-string. Also, require that expressions inside f-strings be enclosed within literal braces, and not escapes like f'\x7b"hi"\x7d'.

bpo-28046 [https://bugs.python.org/issue28046]: Remove platform-specific directories from sys.path.

bpo-28071 [https://bugs.python.org/issue28071]: Add early-out for differencing from an empty set.

bpo-25758 [https://bugs.python.org/issue25758]: Prevents zipimport from unnecessarily encoding a filename (patch by Eryk Sun)

bpo-25856 [https://bugs.python.org/issue25856]: The __module__ attribute of extension classes and functions now is interned. This leads to more compact pickle data with protocol 4.

bpo-27213 [https://bugs.python.org/issue27213]: Rework CALL_FUNCTION* opcodes to produce shorter and more efficient bytecode. Patch by Demur Rumed, design by Serhiy Storchaka, reviewed by Serhiy Storchaka and Victor Stinner.

bpo-26331 [https://bugs.python.org/issue26331]: Implement tokenizing support for PEP 515. Patch by Georg Brandl.

bpo-27999 [https://bugs.python.org/issue27999]: Make “global after use” a SyntaxError, and ditto for nonlocal. Patch by Ivan Levkivskyi.

bpo-28003 [https://bugs.python.org/issue28003]: Implement PEP 525 – Asynchronous Generators.

bpo-27985 [https://bugs.python.org/issue27985]: Implement PEP 526 – Syntax for Variable Annotations. Patch by Ivan Levkivskyi.

bpo-26058 [https://bugs.python.org/issue26058]: Add a new private version to the builtin dict type, incremented at each dictionary creation and at each dictionary change. Implementation of the PEP 509.

bpo-27364 [https://bugs.python.org/issue27364]: A backslash-character pair that is not a valid escape sequence now generates a DeprecationWarning. Patch by Emanuel Barry.

bpo-27350 [https://bugs.python.org/issue27350]: dict implementation is changed like PyPy. It is more compact and preserves insertion order. (Concept developed by Raymond Hettinger and patch by Inada Naoki.)

bpo-27911 [https://bugs.python.org/issue27911]: Remove unnecessary error checks in exec_builtin_or_dynamic().

bpo-27078 [https://bugs.python.org/issue27078]: Added BUILD_STRING opcode. Optimized f-strings evaluation.

bpo-17884 [https://bugs.python.org/issue17884]: Python now requires systems with inttypes.h and stdint.h

bpo-27961 [https://bugs.python.org/issue27961]: Require platforms to support long long. Python hasn’t compiled without long long for years, so this is basically a formality.

bpo-27355 [https://bugs.python.org/issue27355]: Removed support for Windows CE. It was never finished, and Windows CE is no longer a relevant platform for Python.

Implement PEP 523.

bpo-27870 [https://bugs.python.org/issue27870]: A left shift of zero by a large integer no longer attempts to allocate large amounts of memory.

bpo-25402 [https://bugs.python.org/issue25402]: In int-to-decimal-string conversion, improve the estimate of the intermediate memory required, and remove an unnecessarily strict overflow check. Patch by Serhiy Storchaka.

bpo-27214 [https://bugs.python.org/issue27214]: In long_invert, be more careful about modifying object returned by long_add, and remove an unnecessary check for small longs. Thanks Oren Milman for analysis and patch.

bpo-27506 [https://bugs.python.org/issue27506]: Support passing the bytes/bytearray.translate() “delete” argument by keyword.

bpo-27812 [https://bugs.python.org/issue27812]: Properly clear out a generator’s frame’s backreference to the generator to prevent crashes in frame.clear().

bpo-27811 [https://bugs.python.org/issue27811]: Fix a crash when a coroutine that has not been awaited is finalized with warnings-as-errors enabled.

bpo-27587 [https://bugs.python.org/issue27587]: Fix another issue found by PVS-Studio: Null pointer check after use of ‘def’ in _PyState_AddModule(). Initial patch by Christian Heimes.

bpo-27792 [https://bugs.python.org/issue27792]: The modulo operation applied to bool and other int subclasses now always returns an int. Previously the return type depended on the input values. Patch by Xiang Zhang.

bpo-26984 [https://bugs.python.org/issue26984]: int() now always returns an instance of exact int.

bpo-25604 [https://bugs.python.org/issue25604]: Fix a minor bug in integer true division; this bug could potentially have caused off-by-one-ulp results on platforms with unreliable ldexp implementations.

bpo-24254 [https://bugs.python.org/issue24254]: Make class definition namespace ordered by default.

bpo-27662 [https://bugs.python.org/issue27662]: Fix an overflow check in List_New: the original code was checking against Py_SIZE_MAX instead of the correct upper bound of Py_SSIZE_T_MAX. Patch by Xiang Zhang.

bpo-27782 [https://bugs.python.org/issue27782]: Multi-phase extension module import now correctly allows the m_methods field to be used to add module level functions to instances of non-module types returned from Py_create_mod. Patch by Xiang Zhang.

bpo-27936 [https://bugs.python.org/issue27936]: The round() function accepted a second None argument for some types but not for others. Fixed the inconsistency by accepting None for all numeric types.

bpo-27487 [https://bugs.python.org/issue27487]: Warn if a submodule argument to “python -m” or runpy.run_module() is found in sys.modules after parent packages are imported, but before the submodule is executed.

bpo-27157 [https://bugs.python.org/issue27157]: Make only type() itself accept the one-argument form. Patch by Eryk Sun and Emanuel Barry.

bpo-27558 [https://bugs.python.org/issue27558]: Fix a SystemError in the implementation of “raise” statement. In a brand new thread, raise a RuntimeError since there is no active exception to reraise. Patch written by Xiang Zhang.

bpo-28008 [https://bugs.python.org/issue28008]: Implement PEP 530 – asynchronous comprehensions.

bpo-27942 [https://bugs.python.org/issue27942]: Fix memory leak in codeobject.c

库

bpo-28732 [https://bugs.python.org/issue28732]: Fix crash in os.spawnv() with no elements in args

bpo-28485 [https://bugs.python.org/issue28485]: Always raise ValueError for negative compileall.compile_dir(workers=…) parameter, even when multithreading is unavailable.

bpo-28037 [https://bugs.python.org/issue28037]: Use sqlite3_get_autocommit() instead of setting Connection->inTransaction manually.

bpo-25283 [https://bugs.python.org/issue25283]: Attributes tm_gmtoff and tm_zone are now available on all platforms in the return values of time.localtime() and time.gmtime().

bpo-24454 [https://bugs.python.org/issue24454]: Regular expression match object groups are now accessible using __getitem__. “mo[x]” is equivalent to “mo.group(x)”.

bpo-10740 [https://bugs.python.org/issue10740]: sqlite3 no longer implicitly commit an open transaction before DDL statements.

bpo-17941 [https://bugs.python.org/issue17941]: Add a module parameter to collections.namedtuple().

bpo-22493 [https://bugs.python.org/issue22493]: Inline flags now should be used only at the start of the regular expression. Deprecation warning is emitted if uses them in the middle of the regular expression.

bpo-26885 [https://bugs.python.org/issue26885]: xmlrpc now supports unmarshalling additional data types used by Apache XML-RPC implementation for numerics and None.

bpo-28070 [https://bugs.python.org/issue28070]: Fixed parsing inline verbose flag in regular expressions.

bpo-19500 [https://bugs.python.org/issue19500]: Add client-side SSL session resumption to the ssl module.

bpo-28022 [https://bugs.python.org/issue28022]: Deprecate ssl-related arguments in favor of SSLContext. The deprecation include manual creation of SSLSocket and certfile/keyfile (or similar) in ftplib, httplib, imaplib, smtplib, poplib and urllib.

bpo-28043 [https://bugs.python.org/issue28043]: SSLContext has improved default settings: OP_NO_SSLv2, OP_NO_SSLv3, OP_NO_COMPRESSION, OP_CIPHER_SERVER_PREFERENCE, OP_SINGLE_DH_USE, OP_SINGLE_ECDH_USE and HIGH ciphers without MD5.

bpo-24693 [https://bugs.python.org/issue24693]: Changed some RuntimeError’s in the zipfile module to more appropriate types. Improved some error messages and debugging output.

bpo-17909 [https://bugs.python.org/issue17909]: json.load and json.loads now support binary input encoded as UTF-8, UTF-16 or UTF-32. Patch by Serhiy Storchaka.

bpo-27137 [https://bugs.python.org/issue27137]: the pure Python fallback implementation of functools.partial now matches the behaviour of its accelerated C counterpart for subclassing, pickling and text representation purposes. Patch by Emanuel Barry and Serhiy Storchaka.

Fix possible integer overflows and crashes in the mmap module with unusual usage patterns.

bpo-1703178 [https://bugs.python.org/issue1703178]: Fix the ability to pass the –link-objects option to the distutils build_ext command.

bpo-28019 [https://bugs.python.org/issue28019]: itertools.count() no longer rounds non-integer step in range between 1.0 and 2.0 to 1.

bpo-18401 [https://bugs.python.org/issue18401]: Pdb now supports the ‘readrc’ keyword argument to control whether .pdbrc files should be read. Patch by Martin Matusiak and Sam Kimbrel.

bpo-25969 [https://bugs.python.org/issue25969]: Update the lib2to3 grammar to handle the unpacking generalizations added in 3.5.

bpo-14977 [https://bugs.python.org/issue14977]: mailcap now respects the order of the lines in the mailcap files (“first match”), as required by RFC 1542. Patch by Michael Lazar.

bpo-28082 [https://bugs.python.org/issue28082]: Convert re flag constants to IntFlag.

bpo-28025 [https://bugs.python.org/issue28025]: Convert all ssl module constants to IntEnum and IntFlags. SSLContext properties now return flags and enums.

bpo-23591 [https://bugs.python.org/issue23591]: Add Flag, IntFlag, and auto() to enum module.

bpo-433028 [https://bugs.python.org/issue433028]: Added support of modifier spans in regular expressions.

bpo-24594 [https://bugs.python.org/issue24594]: Validates persist parameter when opening MSI database

bpo-17582 [https://bugs.python.org/issue17582]: xml.etree.ElementTree nows preserves whitespaces in attributes (Patch by Duane Griffin. Reviewed and approved by Stefan Behnel.)

bpo-28047 [https://bugs.python.org/issue28047]: Fixed calculation of line length used for the base64 CTE in the new email policies.

bpo-27576 [https://bugs.python.org/issue27576]: Fix call order in OrderedDict.__init__().

email.generator.DecodedGenerator now supports the policy keyword.

bpo-28027 [https://bugs.python.org/issue28027]: Remove undocumented modules from Lib/plat-*: IN, CDROM, DLFCN, TYPES, CDIO, and STROPTS.

bpo-27445 [https://bugs.python.org/issue27445]: Don’t pass str(_charset) to MIMEText.set_payload(). Patch by Claude Paroz.

bpo-24277 [https://bugs.python.org/issue24277]: The new email API is no longer provisional, and the docs have been reorganized and rewritten to emphasize the new API.

bpo-22450 [https://bugs.python.org/issue22450]: urllib now includes an Accept: */* header among the default headers. This makes the results of REST API requests more consistent and predictable especially when proxy servers are involved.

lib2to3.pgen3.driver.load_grammar() now creates a stable cache file between runs given the same Grammar.txt input regardless of the hash randomization setting.

bpo-28005 [https://bugs.python.org/issue28005]: Allow ImportErrors in encoding implementation to propagate.

bpo-26667 [https://bugs.python.org/issue26667]: Support path-like objects in importlib.util.

bpo-27570 [https://bugs.python.org/issue27570]: Avoid zero-length memcpy() etc calls with null source pointers in the “ctypes” and “array” modules.

bpo-22233 [https://bugs.python.org/issue22233]: Break email header lines only on the RFC specified CR and LF characters, not on arbitrary unicode line breaks. This also fixes a bug in HTTP header parsing.

bpo-27331 [https://bugs.python.org/issue27331]: The email.mime classes now all accept an optional policy keyword.

bpo-27988 [https://bugs.python.org/issue27988]: Fix email iter_attachments incorrect mutation of payload list.

bpo-16113 [https://bugs.python.org/issue16113]: Add SHA-3 and SHAKE support to hashlib module.

Eliminate a tautological-pointer-compare warning in _scproxy.c.

bpo-27776 [https://bugs.python.org/issue27776]: The os.urandom() function does now block on Linux 3.17 and newer until the system urandom entropy pool is initialized to increase the security. This change is part of the PEP 524 [https://www.python.org/dev/peps/pep-0524].

bpo-27778 [https://bugs.python.org/issue27778]: Expose the Linux getrandom() syscall as a new os.getrandom() function. This change is part of the PEP 524 [https://www.python.org/dev/peps/pep-0524].

bpo-27691 [https://bugs.python.org/issue27691]: Fix ssl module’s parsing of GEN_RID subject alternative name fields in X.509 certs.

bpo-18844 [https://bugs.python.org/issue18844]: Add random.choices().

bpo-25761 [https://bugs.python.org/issue25761]: Improved error reporting about truncated pickle data in C implementation of unpickler. UnpicklingError is now raised instead of AttributeError and ValueError in some cases.

bpo-26798 [https://bugs.python.org/issue26798]: Add BLAKE2 (blake2b and blake2s) to hashlib.

bpo-26032 [https://bugs.python.org/issue26032]: Optimized globbing in pathlib by using os.scandir(); it is now about 1.5–4 times faster.

bpo-25596 [https://bugs.python.org/issue25596]: Optimized glob() and iglob() functions in the glob module; they are now about 3–6 times faster.

bpo-27928 [https://bugs.python.org/issue27928]: Add scrypt (password-based key derivation function) to hashlib module (requires OpenSSL 1.1.0).

bpo-27850 [https://bugs.python.org/issue27850]: Remove 3DES from ssl module’s default cipher list to counter measure sweet32 attack (CVE-2016-2183).

bpo-27766 [https://bugs.python.org/issue27766]: Add ChaCha20 Poly1305 to ssl module’s default cipher list. (Required OpenSSL 1.1.0 or LibreSSL).

bpo-25387 [https://bugs.python.org/issue25387]: Check return value of winsound.MessageBeep.

bpo-27866 [https://bugs.python.org/issue27866]: Add SSLContext.get_ciphers() method to get a list of all enabled ciphers.

bpo-27744 [https://bugs.python.org/issue27744]: Add AF_ALG (Linux Kernel crypto) to socket module.

bpo-26470 [https://bugs.python.org/issue26470]: Port ssl and hashlib module to OpenSSL 1.1.0.

bpo-11620 [https://bugs.python.org/issue11620]: Fix support for SND_MEMORY in winsound.PlaySound. Based on a patch by Tim Lesher.

bpo-11734 [https://bugs.python.org/issue11734]: Add support for IEEE 754 half-precision floats to the struct module. Based on a patch by Eli Stevens.

bpo-27919 [https://bugs.python.org/issue27919]: Deprecated extra_path distribution option in distutils packaging.

bpo-23229 [https://bugs.python.org/issue23229]: Add new cmath constants: cmath.inf and cmath.nan to match math.inf and math.nan, and also cmath.infj and cmath.nanj to match the format used by complex repr.

bpo-27842 [https://bugs.python.org/issue27842]: The csv.DictReader now returns rows of type OrderedDict. (Contributed by Steve Holden.)

Remove support for passing a file descriptor to os.access. It never worked but previously didn’t raise.

bpo-12885 [https://bugs.python.org/issue12885]: Fix error when distutils encounters symlink.

bpo-27881 [https://bugs.python.org/issue27881]: Fixed possible bugs when setting sqlite3.Connection.isolation_level. Based on patch by Xiang Zhang.

bpo-27861 [https://bugs.python.org/issue27861]: Fixed a crash in sqlite3.Connection.cursor() when a factory creates not a cursor. Patch by Xiang Zhang.

bpo-19884 [https://bugs.python.org/issue19884]: Avoid spurious output on OS X with Gnu Readline.

bpo-27706 [https://bugs.python.org/issue27706]: Restore deterministic behavior of random.Random().seed() for string seeds using seeding version 1. Allows sequences of calls to random() to exactly match those obtained in Python 2. Patch by Nofar Schnider.

bpo-10513 [https://bugs.python.org/issue10513]: Fix a regression in Connection.commit(). Statements should not be reset after a commit.

bpo-12319 [https://bugs.python.org/issue12319]: Chunked transfer encoding support added to http.client.HTTPConnection requests. The urllib.request.AbstractHTTPHandler class does not enforce a Content-Length header any more. If a HTTP request has a file or iterable body, but no Content-Length header, the library now falls back to use chunked transfer-encoding.

A new version of typing.py from https://github.com/python/typing: - Collection (only for 3.6) (bpo-27598 [https://bugs.python.org/issue27598]) - Add FrozenSet to __all__ (upstream #261) - fix crash in _get_type_vars() (upstream #259) - Remove the dict constraint in ForwardRef._eval_type (upstream #252)

bpo-27832 [https://bugs.python.org/issue27832]: Make _normalize parameter to Fraction constuctor keyword-only, so that Fraction(2, 3, 4) now raises TypeError.

bpo-27539 [https://bugs.python.org/issue27539]: Fix unnormalised Fraction.__pow__ result in the case of negative exponent and negative base.

bpo-21718 [https://bugs.python.org/issue21718]: cursor.description is now available for queries using CTEs.

bpo-27819 [https://bugs.python.org/issue27819]: In distutils sdists, simply produce the “gztar” (gzipped tar format) distributions on all platforms unless “formats” is supplied.

bpo-2466 [https://bugs.python.org/issue2466]: posixpath.ismount now correctly recognizes mount points which the user does not have permission to access.

bpo-9998 [https://bugs.python.org/issue9998]: On Linux, ctypes.util.find_library now looks in LD_LIBRARY_PATH for shared libraries.

bpo-27573 [https://bugs.python.org/issue27573]: exit message for code.interact is now configurable.

bpo-27930 [https://bugs.python.org/issue27930]: Improved behaviour of logging.handlers.QueueListener. Thanks to Paulo Andrade and Petr Viktorin for the analysis and patch.

bpo-6766 [https://bugs.python.org/issue6766]: Distributed reference counting added to multiprocessing to support nesting of shared values / proxy objects.

bpo-21201 [https://bugs.python.org/issue21201]: Improves readability of multiprocessing error message. Thanks to Wojciech Walczak for patch.

asyncio: 在各Transport类中添加了 set_protocol / get_protocol 方法

bpo-27456 [https://bugs.python.org/issue27456]: asyncio: Set TCP_NODELAY by default.

IDLE

bpo-15308 [https://bugs.python.org/issue15308]: Add ‘interrupt execution’ (^C) to Shell menu. Patch by Roger Serwy, updated by Bayard Randel.

bpo-27922 [https://bugs.python.org/issue27922]: Stop IDLE tests from ‘flashing’ gui widgets on the screen.

bpo-27891 [https://bugs.python.org/issue27891]: Consistently group and sort imports within idlelib modules.

bpo-17642 [https://bugs.python.org/issue17642]: add larger font sizes for classroom projection.

在 IDLE 帮助窗口的标题中加入版本号

bpo-25564 [https://bugs.python.org/issue25564]: In section on IDLE – console differences, mention that using exec means that __builtins__ is defined for each statement.

bpo-27821 [https://bugs.python.org/issue27821]: Fix 3.6.0a3 regression that prevented custom key sets from being selected when no custom theme was defined.

C API

bpo-26900 [https://bugs.python.org/issue26900]: Excluded underscored names and other private API from limited API.

bpo-26027 [https://bugs.python.org/issue26027]: Add support for path-like objects in PyUnicode_FSConverter() & PyUnicode_FSDecoder().

测试

bpo-27427 [https://bugs.python.org/issue27427]: Additional tests for the math module. Patch by Francisco Couzo.

bpo-27953 [https://bugs.python.org/issue27953]: Skip math and cmath tests that fail on OS X 10.4 due to a poor libm implementation of tan.

bpo-26040 [https://bugs.python.org/issue26040]: Improve test_math and test_cmath coverage and rigour. Patch by Jeff Allen.

bpo-27787 [https://bugs.python.org/issue27787]: Call gc.collect() before checking each test for “dangling threads”, since the dangling threads are weak references.

构建

bpo-27566 [https://bugs.python.org/issue27566]: Fix clean target in freeze makefile (patch by Lisa Roach)

bpo-27705 [https://bugs.python.org/issue27705]: Update message in validate_ucrtbase.py

bpo-27976 [https://bugs.python.org/issue27976]: Deprecate building _ctypes with the bundled copy of libffi on non-OSX UNIX platforms.

bpo-27983 [https://bugs.python.org/issue27983]: Cause lack of llvm-profdata tool when using clang as required for PGO linking to be a configure time error rather than make time when --with-optimizations is enabled. Also improve our ability to find the llvm-profdata tool on MacOS and some Linuxes.

bpo-21590 [https://bugs.python.org/issue21590]: Support for DTrace and SystemTap probes.

bpo-26307 [https://bugs.python.org/issue26307]: The profile-opt build now applies PGO to the built-in modules.

bpo-26359 [https://bugs.python.org/issue26359]: Add the –with-optimizations flag to turn on LTO and PGO build support when available.

bpo-27917 [https://bugs.python.org/issue27917]: Set platform triplets for Android builds.

bpo-25825 [https://bugs.python.org/issue25825]: Update references to the $(LIBPL) installation path on AIX. This path was changed in 3.2a4.

更新 OS X 安装包，使用SQLite 3.14.1 和 XZ 5.2.2版本。

bpo-21122 [https://bugs.python.org/issue21122]: Fix LTO builds on OS X.

bpo-17128 [https://bugs.python.org/issue17128]: Build OS X installer with a private copy of OpenSSL. Also provide a sample Install Certificates command script to install a set of root certificates from the third-party certifi module.

工具/示例

bpo-27952 [https://bugs.python.org/issue27952]: Get Tools/scripts/fixcid.py working with Python 3 and the current “re” module, avoid invalid Python backslash escapes, and fix a bug parsing escaped C quote signs.

Windows

bpo-28065 [https://bugs.python.org/issue28065]: Update xz dependency to 5.2.2 and build it from source.

bpo-25144 [https://bugs.python.org/issue25144]: Ensures TargetDir is set before continuing with custom install.

bpo-1602 [https://bugs.python.org/issue1602]: Windows console doesn’t input or print Unicode (PEP 528)

bpo-27781 [https://bugs.python.org/issue27781]: Change file system encoding on Windows to UTF-8 (PEP 529)

bpo-27731 [https://bugs.python.org/issue27731]: Opt-out of MAX_PATH on Windows 10

bpo-6135 [https://bugs.python.org/issue6135]: Adds encoding and errors parameters to subprocess.

bpo-27959 [https://bugs.python.org/issue27959]: Adds oem encoding, alias ansi to mbcs, move aliasmbcs to codec lookup.

bpo-27982 [https://bugs.python.org/issue27982]: The functions of the winsound module now accept keyword arguments.

bpo-20366 [https://bugs.python.org/issue20366]: Build full text search support into SQLite on Windows.

bpo-27756 [https://bugs.python.org/issue27756]: Adds new icons for Python files and processes on Windows. Designs by Cherry Wang.

bpo-27883 [https://bugs.python.org/issue27883]: 在Windows上将sqlite更新到3.14.1.0

Python 3.6.0 alpha 4

发布日期: 2016-08-15

核心与内置

bpo-27704 [https://bugs.python.org/issue27704]: Optimized creating bytes and bytearray from byte-like objects and iterables. Speed up to 3 times for short objects. Original patch by Naoki Inada.

bpo-26823 [https://bugs.python.org/issue26823]: Large sections of repeated lines in tracebacks are now abbreviated as “[Previous line repeated {count} more times]” by the builtin traceback rendering. Patch by Emanuel Barry.

bpo-27574 [https://bugs.python.org/issue27574]: Decreased an overhead of parsing keyword arguments in functions implemented with using Argument Clinic.

bpo-22557 [https://bugs.python.org/issue22557]: Now importing already imported modules is up to 2.5 times faster.

bpo-17596 [https://bugs.python.org/issue17596]: Include <wincrypt.h> to help with Min GW building.

bpo-17599 [https://bugs.python.org/issue17599]: On Windows, rename the privately defined REPARSE_DATA_BUFFER structure to avoid conflicting with the definition from Min GW.

bpo-27507 [https://bugs.python.org/issue27507]: Add integer overflow check in bytearray.extend(). Patch by Xiang Zhang.

bpo-27581 [https://bugs.python.org/issue27581]: Don’t rely on wrapping for overflow check in PySequence_Tuple(). Patch by Xiang Zhang.

bpo-1621 [https://bugs.python.org/issue1621]: Avoid signed integer overflow in list and tuple operations. Patch by Xiang Zhang.

bpo-27419 [https://bugs.python.org/issue27419]: Standard __import__() no longer look up “__import__” in globals or builtins for importing submodules or “from import”. Fixed a crash if raise a warning about unabling to resolve package from __spec__ or __package__.

bpo-27083 [https://bugs.python.org/issue27083]: Respect the PYTHONCASEOK environment variable under Windows.

bpo-27514 [https://bugs.python.org/issue27514]: Make having too many statically nested blocks a SyntaxError instead of SystemError.

bpo-27366 [https://bugs.python.org/issue27366]: Implemented PEP 487 (Simpler customization of class creation). Upon subclassing, the __init_subclass__ classmethod is called on the base class. Descriptors are initialized with __set_name__ after class creation.

库

bpo-26027 [https://bugs.python.org/issue26027]: Add PEP 519/__fspath__() support to the os and os.path modules. Includes code from Jelle Zijlstra. (See also: bpo-27524 [https://bugs.python.org/issue27524])

bpo-27598 [https://bugs.python.org/issue27598]: Add Collections to collections.abc. Patch by Ivan Levkivskyi, docs by Neil Girdhar.

bpo-25958 [https://bugs.python.org/issue25958]: Support “anti-registration” of special methods from various ABCs, like __hash__, __iter__ or __len__. All these (and several more) can be set to None in an implementation class and the behavior will be as if the method is not defined at all. (Previously, this mechanism existed only for __hash__, to make mutable classes unhashable.) Code contributed by Andrew Barnert and Ivan Levkivskyi.

bpo-16764 [https://bugs.python.org/issue16764]: Support keyword arguments to zlib.decompress(). Patch by Xiang Zhang.

bpo-27736 [https://bugs.python.org/issue27736]: Prevent segfault after interpreter re-initialization due to ref count problem introduced in code for bpo-27038 [https://bugs.python.org/issue27038] in 3.6.0a3. Patch by Xiang Zhang.

bpo-25628 [https://bugs.python.org/issue25628]: The verbose and rename parameters for collections.namedtuple are now keyword-only.

bpo-12345 [https://bugs.python.org/issue12345]: Add mathematical constant tau to math and cmath. See also PEP 628.

bpo-26823 [https://bugs.python.org/issue26823]: traceback.StackSummary.format now abbreviates large sections of repeated lines as “[Previous line repeated {count} more times]” (this change then further affects other traceback display operations in the module). Patch by Emanuel Barry.

bpo-27664 [https://bugs.python.org/issue27664]: Add to concurrent.futures.thread.ThreadPoolExecutor() the ability to specify a thread name prefix.

bpo-27181 [https://bugs.python.org/issue27181]: Add geometric_mean and harmonic_mean to statistics module.

bpo-27573 [https://bugs.python.org/issue27573]: code.interact now prints an message when exiting.

bpo-6422 [https://bugs.python.org/issue6422]: Add autorange method to timeit.Timer objects.

bpo-27773 [https://bugs.python.org/issue27773]: Correct some memory management errors server_hostname in _ssl.wrap_socket().

bpo-26750 [https://bugs.python.org/issue26750]: unittest.mock.create_autospec() now works properly for subclasses of property() and other data descriptors. Removes the never publicly used, never documented unittest.mock.DescriptorTypes tuple.

bpo-26754 [https://bugs.python.org/issue26754]: Undocumented support of general bytes-like objects as path in compile() and similar functions is now deprecated.

bpo-26800 [https://bugs.python.org/issue26800]: Undocumented support of general bytes-like objects as paths in os functions is now deprecated.

bpo-26981 [https://bugs.python.org/issue26981]: Add _order_ compatibility shim to enum.Enum for Python 2/3 code bases.

bpo-27661 [https://bugs.python.org/issue27661]: Added tzinfo keyword argument to datetime.combine.

In the curses module, raise an error if window.getstr() or window.instr() is passed a negative value.

bpo-27783 [https://bugs.python.org/issue27783]: Fix possible usage of uninitialized memory in operator.methodcaller.

bpo-27774 [https://bugs.python.org/issue27774]: Fix possible Py_DECREF on unowned object in _sre.

bpo-27760 [https://bugs.python.org/issue27760]: Fix possible integer overflow in binascii.b2a_qp.

bpo-27758 [https://bugs.python.org/issue27758]: Fix possible integer overflow in the _csv module for large record lengths.

bpo-27568 [https://bugs.python.org/issue27568]: Prevent HTTPoxy attack (CVE-2016-1000110). Ignore the HTTP_PROXY variable when REQUEST_METHOD environment is set, which indicates that the script is in CGI mode.

bpo-7063 [https://bugs.python.org/issue7063]: Remove dead code from the “array” module’s slice handling. Patch by Chuck.

bpo-27656 [https://bugs.python.org/issue27656]: Do not assume sched.h defines any SCHED_* constants.

bpo-27130 [https://bugs.python.org/issue27130]: In the “zlib” module, fix handling of large buffers (typically 4 GiB) when compressing and decompressing. Previously, inputs were limited to 4 GiB, and compression and decompression operations did not properly handle results of 4 GiB.

bpo-24773 [https://bugs.python.org/issue24773]: Implemented PEP 495 (Local Time Disambiguation).

Expose the EPOLLEXCLUSIVE constant (when it is defined) in the select module.

bpo-27567 [https://bugs.python.org/issue27567]: Expose the EPOLLRDHUP and POLLRDHUP constants in the select module.

bpo-1621 [https://bugs.python.org/issue1621]: Avoid signed int negation overflow in the “audioop” module.

bpo-27533 [https://bugs.python.org/issue27533]: Release GIL in nt._isdir

bpo-17711 [https://bugs.python.org/issue17711]: Fixed unpickling by the persistent ID with protocol 0. Original patch by Alexandre Vassalotti.

bpo-27522 [https://bugs.python.org/issue27522]: Avoid an unintentional reference cycle in email.feedparser.

bpo-27512 [https://bugs.python.org/issue27512]: Fix a segfault when os.fspath() called an __fspath__() method that raised an exception. Patch by Xiang Zhang.

IDLE

bpo-27714 [https://bugs.python.org/issue27714]: text_textview and test_autocomplete now pass when re-run in the same process. This occurs when test_idle fails when run with the -w option but without -jn. Fix warning from test_config.

bpo-27621 [https://bugs.python.org/issue27621]: Put query response validation error messages in the query box itself instead of in a separate messagebox. Redo tests to match. Add Mac OSX refinements. Original patch by Mark Roseman.

bpo-27620 [https://bugs.python.org/issue27620]: Escape key now closes Query box as cancelled.

bpo-27609 [https://bugs.python.org/issue27609]: IDLE: tab after initial whitespace should tab, not autocomplete. This fixes problem with writing docstrings at least twice indented.

bpo-27609 [https://bugs.python.org/issue27609]: Explicitly return None when there are also non-None returns. In a few cases, reverse a condition and eliminate a return.

bpo-25507 [https://bugs.python.org/issue25507]: IDLE no longer runs buggy code because of its tkinter imports. Users must include the same imports required to run directly in Python.

bpo-27173 [https://bugs.python.org/issue27173]: Add ‘IDLE Modern Unix’ to the built-in key sets. Make the default key set depend on the platform. Add tests for the changes to the config module.

bpo-27452 [https://bugs.python.org/issue27452]: add line counter and crc to IDLE configHandler test dump.

测试

bpo-25805 [https://bugs.python.org/issue25805]: Skip a test in test_pkgutil as needed that doesn’t work when __name__ == __main__. Patch by SilentGhost.

bpo-27472 [https://bugs.python.org/issue27472]: Add test.support.unix_shell as the path to the default shell.

bpo-27369 [https://bugs.python.org/issue27369]: In test_pyexpat, avoid testing an error message detail that changed in Expat 2.2.0.

bpo-27594 [https://bugs.python.org/issue27594]: Prevent assertion error when running test_ast with coverage enabled: ensure code object has a valid first line number. Patch suggested by Ivan Levkivskyi.

Windows

bpo-27647 [https://bugs.python.org/issue27647]: Update bundled Tcl/Tk to 8.6.6.

bpo-27610 [https://bugs.python.org/issue27610]: Adds PEP 514 metadata to Windows installer

bpo-27469 [https://bugs.python.org/issue27469]: Adds a shell extension to the launcher so that drag and drop works correctly.

bpo-27309 [https://bugs.python.org/issue27309]: Enables proper Windows styles in python[w].exe manifest.

构建

bpo-27713 [https://bugs.python.org/issue27713]: Suppress spurious build warnings when updating importlib’s bootstrap files. Patch by Xiang Zhang

bpo-25825 [https://bugs.python.org/issue25825]: Correct the references to Modules/python.exp, which is required on AIX. The references were accidentally changed in 3.5.0a1.

bpo-27453 [https://bugs.python.org/issue27453]: CPP invocation in configure must use CPPFLAGS. Patch by Chi Hsuan Yen.

bpo-27641 [https://bugs.python.org/issue27641]: The configure script now inserts comments into the makefile to prevent the pgen and _freeze_importlib executables from being cross-compiled.

bpo-26662 [https://bugs.python.org/issue26662]: Set PYTHON_FOR_GEN in configure as the Python program to be used for file generation during the build.

bpo-10910 [https://bugs.python.org/issue10910]: Avoid C++ compilation errors on FreeBSD and OS X. Also update FreedBSD version checks for the original ctype UTF-8 workaround.

Python 3.6.0 alpha 3

发布日期: 2016-07-11

核心与内置

bpo-27473 [https://bugs.python.org/issue27473]: Fixed possible integer overflow in bytes and bytearray concatenations. Patch by Xiang Zhang.

bpo-23034 [https://bugs.python.org/issue23034]: The output of a special Python build with defined COUNT_ALLOCS, SHOW_ALLOC_COUNT or SHOW_TRACK_COUNT macros is now off by default. It can be re-enabled using the “-X showalloccount” option. It now outputs to stderr instead of stdout.

bpo-27443 [https://bugs.python.org/issue27443]: __length_hint__() of bytearray iterators no longer return a negative integer for a resized bytearray.

bpo-27007 [https://bugs.python.org/issue27007]: The fromhex() class methods of bytes and bytearray subclasses now return an instance of corresponding subclass.

库

bpo-26844 [https://bugs.python.org/issue26844]: Fix error message for imp.find_module() to refer to ‘path’ instead of ‘name’. Patch by Lev Maximov.

bpo-23804 [https://bugs.python.org/issue23804]: Fix SSL zero-length recv() calls to not block and not raise an error about unclean EOF.

bpo-27466 [https://bugs.python.org/issue27466]: Change time format returned by http.cookie.time2netscape, confirming the netscape cookie format and making it consistent with documentation.

bpo-21708 [https://bugs.python.org/issue21708]: Deprecated dbm.dumb behavior that differs from common dbm behavior: creating a database in ‘r’ and ‘w’ modes and modifying a database in ‘r’ mode.

bpo-26721 [https://bugs.python.org/issue26721]: Change the socketserver.StreamRequestHandler.wfile attribute to implement BufferedIOBase. In particular, the write() method no longer does partial writes.

bpo-22115 [https://bugs.python.org/issue22115]: Added methods trace_add, trace_remove and trace_info in the tkinter.Variable class. They replace old methods trace_variable, trace, trace_vdelete and trace_vinfo that use obsolete Tcl commands and might not work in future versions of Tcl. Fixed old tracing methods: trace_vdelete() with wrong mode no longer break tracing, trace_vinfo() now always returns a list of pairs of strings, tracing in the “u” mode now works.

bpo-26243 [https://bugs.python.org/issue26243]: Only the level argument to zlib.compress() is keyword argument now. The first argument is positional-only.

bpo-27038 [https://bugs.python.org/issue27038]: Expose the DirEntry type as os.DirEntry. Code patch by Jelle Zijlstra.

bpo-27186 [https://bugs.python.org/issue27186]: Update os.fspath()/PyOS_FSPath() to check the return value of __fspath__() to be either str or bytes.

bpo-18726 [https://bugs.python.org/issue18726]: All optional parameters of the dump(), dumps(), load() and loads() functions and JSONEncoder and JSONDecoder class constructors in the json module are now keyword-only.

bpo-27319 [https://bugs.python.org/issue27319]: Methods selection_set(), selection_add(), selection_remove() and selection_toggle() of ttk.TreeView now allow passing multiple items as multiple arguments instead of passing them as a tuple. Deprecated undocumented ability of calling the selection() method with arguments.

bpo-27079 [https://bugs.python.org/issue27079]: Fixed curses.ascii functions isblank(), iscntrl() and ispunct().

bpo-27294 [https://bugs.python.org/issue27294]: Numerical state in the repr for Tkinter event objects is now represented as a combination of known flags.

bpo-27177 [https://bugs.python.org/issue27177]: Match objects in the re module now support index-like objects as group indices. Based on patches by Jeroen Demeyer and Xiang Zhang.

bpo-26754 [https://bugs.python.org/issue26754]: Some functions (compile() etc) accepted a filename argument encoded as an iterable of integers. Now only strings and byte-like objects are accepted.

bpo-26536 [https://bugs.python.org/issue26536]: socket.ioctl now supports SIO_LOOPBACK_FAST_PATH. Patch by Daniel Stokes.

bpo-27048 [https://bugs.python.org/issue27048]: Prevents distutils failing on Windows when environment variables contain non-ASCII characters

bpo-27330 [https://bugs.python.org/issue27330]: Fixed possible leaks in the ctypes module.

bpo-27238 [https://bugs.python.org/issue27238]: Got rid of bare excepts in the turtle module. Original patch by Jelle Zijlstra.

bpo-27122 [https://bugs.python.org/issue27122]: When an exception is raised within the context being managed by a contextlib.ExitStack() and one of the exit stack generators catches and raises it in a chain, do not re-raise the original exception when exiting, let the new chained one through. This avoids the PEP 479 bug described in issue25782.

安全

bpo-27278 [https://bugs.python.org/issue27278]: Fix os.urandom() implementation using getrandom() on Linux. Truncate size to INT_MAX and loop until we collected enough random bytes, instead of casting a directly Py_ssize_t to int.

库

bpo-16864 [https://bugs.python.org/issue16864]: sqlite3.Cursor.lastrowid now supports REPLACE statement. Initial patch by Alex LordThorsen.

bpo-26386 [https://bugs.python.org/issue26386]: Fixed ttk.TreeView selection operations with item id’s containing spaces.

bpo-8637 [https://bugs.python.org/issue8637]: Honor a pager set by the env var MANPAGER (in preference to one set by the env var PAGER).

安全

bpo-22636 [https://bugs.python.org/issue22636]: Avoid shell injection problems with ctypes.util.find_library().

库

bpo-16182 [https://bugs.python.org/issue16182]: Fix various functions in the “readline” module to use the locale encoding, and fix get_begidx() and get_endidx() to return code point indexes.

bpo-27392 [https://bugs.python.org/issue27392]: Add loop.connect_accepted_socket(). Patch by Jim Fulton.

IDLE

bpo-27477 [https://bugs.python.org/issue27477]: IDLE search dialogs now use ttk widgets.

bpo-27173 [https://bugs.python.org/issue27173]: Add ‘IDLE Modern Unix’ to the built-in key sets. Make the default key set depend on the platform. Add tests for the changes to the config module.

bpo-27452 [https://bugs.python.org/issue27452]: make command line “idle-test> python test_help.py” work. __file__ is relative when python is started in the file’s directory.

bpo-27452 [https://bugs.python.org/issue27452]: add line counter and crc to IDLE configHandler test dump.

bpo-27380 [https://bugs.python.org/issue27380]: IDLE: add query.py with base Query dialog and ttk widgets. Module had subclasses SectionName, ModuleName, and HelpSource, which are used to get information from users by configdialog and file =>Load Module. Each subclass has itw own validity checks. Using ModuleName allows users to edit bad module names instead of starting over. Add tests and delete the two files combined into the new one.

bpo-27372 [https://bugs.python.org/issue27372]: Test_idle no longer changes the locale.

bpo-27365 [https://bugs.python.org/issue27365]: Allow non-ascii chars in IDLE NEWS.txt, for contributor names.

bpo-27245 [https://bugs.python.org/issue27245]: IDLE: Cleanly delete custom themes and key bindings. Previously, when IDLE was started from a console or by import, a cascade of warnings was emitted. Patch by Serhiy Storchaka.

bpo-24137 [https://bugs.python.org/issue24137]: Run IDLE, test_idle, and htest with tkinter default root disabled. Fix code and tests that fail with this restriction. Fix htests to not create a second and redundant root and mainloop.

bpo-27310 [https://bugs.python.org/issue27310]: Fix IDLE.app failure to launch on OS X due to vestigial import.

C API

bpo-26754 [https://bugs.python.org/issue26754]: PyUnicode_FSDecoder() accepted a filename argument encoded as an iterable of integers. Now only strings and byte-like objects are accepted.

构建

bpo-28066 [https://bugs.python.org/issue28066]: Fix the logic that searches build directories for generated include files when building outside the source tree.

bpo-27442 [https://bugs.python.org/issue27442]: Expose the Android API level that python was built against, in sysconfig.get_config_vars() as ‘ANDROID_API_LEVEL’.

bpo-27434 [https://bugs.python.org/issue27434]: The interpreter that runs the cross-build, found in PATH, must now be of the same feature version (e.g. 3.6) as the source being built.

bpo-26930 [https://bugs.python.org/issue26930]: 更新Windows 构建以使用OpenSSL OpenSSL 1.0.2h.

bpo-23968 [https://bugs.python.org/issue23968]: Rename the platform directory from plat-$(MACHDEP) to plat-$(PLATFORM_TRIPLET). Rename the config directory (LIBPL) from config-$(LDVERSION) to config-$(LDVERSION)-$(PLATFORM_TRIPLET). Install the platform specific _sysconfigdata module into the platform directory and rename it to include the ABIFLAGS.

在 GNU/Hurd 系统中不使用大文件支持。

工具/示例

bpo-27332 [https://bugs.python.org/issue27332]: Fixed the type of the first argument of module-level functions generated by Argument Clinic. Patch by Petr Viktorin.

bpo-27418 [https://bugs.python.org/issue27418]: Fixed Tools/importbench/importbench.py.

文档

bpo-19489 [https://bugs.python.org/issue19489]: Moved the search box from the sidebar to the header and footer of each page. Patch by Ammar Askar.

bpo-27285 [https://bugs.python.org/issue27285]: Update documentation to reflect the deprecation of pyvenv and normalize on the term “virtual environment”. Patch by Steve Piercy.

测试

bpo-27027 [https://bugs.python.org/issue27027]: Added test.support.is_android that is True when this is an Android build.

Python 3.6.0 alpha 2

发布日期: 2016-06-13

核心与内置

bpo-27095 [https://bugs.python.org/issue27095]: Simplified MAKE_FUNCTION and removed MAKE_CLOSURE opcodes. Patch by Demur Rumed.

bpo-27190 [https://bugs.python.org/issue27190]: Raise NotSupportedError if sqlite3 is older than 3.3.1. Patch by Dave Sawyer.

bpo-27286 [https://bugs.python.org/issue27286]: Fixed compiling BUILD_MAP_UNPACK_WITH_CALL opcode. Calling function with generalized unpacking (PEP 448) and conflicting keyword names could cause undefined behavior.

bpo-27140 [https://bugs.python.org/issue27140]: Added BUILD_CONST_KEY_MAP opcode.

bpo-27186 [https://bugs.python.org/issue27186]: Add support for os.PathLike objects to open() (part of PEP 519).

bpo-27066 [https://bugs.python.org/issue27066]: Fixed SystemError if a custom opener (for open()) returns a negative number without setting an exception.

bpo-26983 [https://bugs.python.org/issue26983]: float() now always return an instance of exact float. The deprecation warning is emitted if __float__ returns an instance of a strict subclass of float. In a future versions of Python this can be an error.

bpo-27097 [https://bugs.python.org/issue27097]: Python interpreter is now about 7% faster due to optimized instruction decoding. Based on patch by Demur Rumed.

bpo-26647 [https://bugs.python.org/issue26647]: Python interpreter now uses 16-bit wordcode instead of bytecode. Patch by Demur Rumed.

bpo-23275 [https://bugs.python.org/issue23275]: Allow assigning to an empty target list in round brackets: () = iterable.

bpo-27243 [https://bugs.python.org/issue27243]: Update the __aiter__ protocol: instead of returning an awaitable that resolves to an asynchronous iterator, the asynchronous iterator should be returned directly. Doing the former will trigger a PendingDeprecationWarning.

库

Comment out socket (SO_REUSEPORT) and posix (O_SHLOCK, O_EXLOCK) constants exposed on the API which are not implemented on GNU/Hurd. They would not work at runtime anyway.

bpo-27025 [https://bugs.python.org/issue27025]: Generated names for Tkinter widgets are now more meaningful and recognizable.

bpo-25455 [https://bugs.python.org/issue25455]: Fixed crashes in repr of recursive ElementTree.Element and functools.partial objects.

bpo-27294 [https://bugs.python.org/issue27294]: Improved repr for Tkinter event objects.

bpo-20508 [https://bugs.python.org/issue20508]: Improve exception message of IPv{4,6}Network.__getitem__. Patch by Gareth Rees.

安全

bpo-26556 [https://bugs.python.org/issue26556]: Update expat to 2.1.1, fixes CVE-2015-1283.

Fix TLS stripping vulnerability in smtplib, CVE-2016-0772. Reported by Team Oststrom.

库

bpo-21386 [https://bugs.python.org/issue21386]: Implement missing IPv4Address.is_global property. It was documented since 07a5610bae9d. Initial patch by Roger Luethi.

bpo-27029 [https://bugs.python.org/issue27029]: Removed deprecated support of universal newlines mode from ZipFile.open().

bpo-27030 [https://bugs.python.org/issue27030]: Unknown escapes consisting of '\' and an ASCII letter in regular expressions now are errors. The re.LOCALE flag now can be used only with bytes patterns.

bpo-27186 [https://bugs.python.org/issue27186]: Add os.PathLike support to DirEntry (part of PEP 519). Initial patch by Jelle Zijlstra.

bpo-20900 [https://bugs.python.org/issue20900]: distutils register command now decodes HTTP responses correctly. Initial patch by ingrid.

bpo-27186 [https://bugs.python.org/issue27186]: Add os.PathLike support to pathlib, removing its provisional status (part of PEP 519). Initial patch by Dusty Phillips.

bpo-27186 [https://bugs.python.org/issue27186]: Add support for os.PathLike objects to os.fsencode() and os.fsdecode() (part of PEP 519).

bpo-27186 [https://bugs.python.org/issue27186]: Introduce os.PathLike and os.fspath() (part of PEP 519).

A new version of typing.py provides several new classes and features: @overload outside stubs, Reversible, DefaultDict, Text, ContextManager, Type[], NewType(), TYPE_CHECKING, and numerous bug fixes (note that some of the new features are not yet implemented in mypy or other static analyzers). Also classes for PEP 492 (Awaitable, AsyncIterable, AsyncIterator) have been added (in fact they made it into 3.5.1 but were never mentioned).

bpo-25738 [https://bugs.python.org/issue25738]: Stop http.server.BaseHTTPRequestHandler.send_error() from sending a message body for 205 Reset Content. Also, don’t send Content header fields in responses that don’t have a body. Patch by Susumu Koshiba.

bpo-21313 [https://bugs.python.org/issue21313]: Fix the “platform” module to tolerate when sys.version contains truncated build information.

安全

bpo-26839 [https://bugs.python.org/issue26839]: On Linux, os.urandom() now calls getrandom() with GRND_NONBLOCK to fall back on reading /dev/urandom if the urandom entropy pool is not initialized yet. Patch written by Colm Buckley.

库

bpo-23883 [https://bugs.python.org/issue23883]: Added missing APIs to __all__ to match the documented APIs for the following modules: cgi, mailbox, mimetypes, plistlib and smtpd. Patches by Jacek Kołodziej.

bpo-27164 [https://bugs.python.org/issue27164]: In the zlib module, allow decompressing raw Deflate streams with a predefined zdict. Based on patch by Xiang Zhang.

bpo-24291 [https://bugs.python.org/issue24291]: Fix wsgiref.simple_server.WSGIRequestHandler to completely write data to the client. Previously it could do partial writes and truncate data. Also, wsgiref.handler.ServerHandler can now handle stdout doing partial writes, but this is deprecated.

bpo-21272 [https://bugs.python.org/issue21272]: Use _sysconfigdata.py to initialize distutils.sysconfig.

bpo-19611 [https://bugs.python.org/issue19611]: inspect now reports the implicit .0 parameters generated by the compiler for comprehension and generator expression scopes as if they were positional-only parameters called implicit0. Patch by Jelle Zijlstra.

bpo-26809 [https://bugs.python.org/issue26809]: Add __all__ to string. Patch by Emanuel Barry.

bpo-26373 [https://bugs.python.org/issue26373]: subprocess.Popen.communicate now correctly ignores BrokenPipeError when the child process dies before .communicate() is called in more/all circumstances.

signal, socket, and ssl module IntEnum constant name lookups now return a consistent name for values having multiple names. Ex: signal.Signals(6) now refers to itself as signal.SIGALRM rather than flipping between that and signal.SIGIOT based on the interpreter’s hash randomization seed.

bpo-27167 [https://bugs.python.org/issue27167]: Clarify the subprocess.CalledProcessError error message text when the child process died due to a signal.

bpo-25931 [https://bugs.python.org/issue25931]: Don’t define socketserver.Forking* names on platforms such as Windows that do not support os.fork().

bpo-21776 [https://bugs.python.org/issue21776]: distutils.upload now correctly handles HTTPError. Initial patch by Claudiu Popa.

bpo-26526 [https://bugs.python.org/issue26526]: Replace custom parse tree validation in the parser module with a simple DFA validator.

bpo-27114 [https://bugs.python.org/issue27114]: Fix SSLContext._load_windows_store_certs fails with PermissionError

bpo-18383 [https://bugs.python.org/issue18383]: Avoid creating duplicate filters when using filterwarnings and simplefilter. Based on patch by Alex Shkop.

bpo-23026 [https://bugs.python.org/issue23026]: winreg.QueryValueEx() now return an integer for REG_QWORD type.

bpo-26741 [https://bugs.python.org/issue26741]: subprocess.Popen destructor now emits a ResourceWarning warning if the child process is still running.

bpo-27056 [https://bugs.python.org/issue27056]: Optimize pickle.load() and pickle.loads(), up to 10% faster to deserialize a lot of small objects.

bpo-21271 [https://bugs.python.org/issue21271]: New keyword only parameters in reset_mock call.

IDLE

bpo-5124 [https://bugs.python.org/issue5124]: Paste with text selected now replaces the selection on X11. This matches how paste works on Windows, Mac, most modern Linux apps, and ttk widgets. Original patch by Serhiy Storchaka.

bpo-24750 [https://bugs.python.org/issue24750]: Switch all scrollbars in IDLE to ttk versions. Where needed, minimal tests are added to cover changes.

bpo-24759 [https://bugs.python.org/issue24759]: IDLE requires tk 8.5 and availability ttk widgets. Delete now unneeded tk version tests and code for older versions. Add test for IDLE syntax colorizer.

bpo-27239 [https://bugs.python.org/issue27239]: idlelib.macosx.isXyzTk functions initialize as needed.

bpo-27262 [https://bugs.python.org/issue27262]: move Aqua unbinding code, which enable context menus, to macosx.

bpo-24759 [https://bugs.python.org/issue24759]: Make clear in idlelib.idle_test.__init__ that the directory is a private implementation of test.test_idle and tool for maintainers.

bpo-27196 [https://bugs.python.org/issue27196]: Stop ‘ThemeChanged’ warnings when running IDLE tests. These persisted after other warnings were suppressed in #20567. Apply Serhiy Storchaka’s update_idletasks solution to four test files. Record this additional advice in idle_test/README.txt

bpo-20567 [https://bugs.python.org/issue20567]: Revise idle_test/README.txt with advice about avoiding tk warning messages from tests. Apply advice to several IDLE tests.

bpo-24225 [https://bugs.python.org/issue24225]: Update idlelib/README.txt with new file names and event handlers.

bpo-27156 [https://bugs.python.org/issue27156]: Remove obsolete code not used by IDLE.

bpo-27117 [https://bugs.python.org/issue27117]: Make colorizer htest and turtledemo work with dark themes. Move code for configuring text widget colors to a new function.

bpo-24225 [https://bugs.python.org/issue24225]: Rename many idlelib/*.py and idle_test/test_*.py files. Edit files to replace old names with new names when the old name referred to the module rather than the class it contained. See the issue and IDLE section in What’s New in 3.6 for more.

bpo-26673 [https://bugs.python.org/issue26673]: When tk reports font size as 0, change to size 10. Such fonts on Linux prevented the configuration dialog from opening.

bpo-21939 [https://bugs.python.org/issue21939]: Add test for IDLE’s percolator. Original patch by Saimadhav Heblikar.

bpo-21676 [https://bugs.python.org/issue21676]: Add test for IDLE’s replace dialog. Original patch by Saimadhav Heblikar.

bpo-18410 [https://bugs.python.org/issue18410]: Add test for IDLE’s search dialog. Original patch by Westley Martínez.

bpo-21703 [https://bugs.python.org/issue21703]: Add test for undo delegator. Patch mostly by Saimadhav Heblikar .

bpo-27044 [https://bugs.python.org/issue27044]: Add ConfigDialog.remove_var_callbacks to stop memory leaks.

bpo-23977 [https://bugs.python.org/issue23977]: Add more asserts to test_delegator.

文档

bpo-16484 [https://bugs.python.org/issue16484]: Change the default PYTHONDOCS URL to “https:”, and fix the resulting links to use lowercase. Patch by Sean Rodman, test by Kaushik Nadikuditi.

bpo-24136 [https://bugs.python.org/issue24136]: Document the new PEP 448 unpacking syntax of 3.5.

bpo-22558 [https://bugs.python.org/issue22558]: Add remaining doc links to source code for Python-coded modules. Patch by Yoni Lavi.

测试

bpo-25285 [https://bugs.python.org/issue25285]: regrtest now uses subprocesses when the -j1 command line option is used: each test file runs in a fresh child process. Before, the -j1 option was ignored.

bpo-25285 [https://bugs.python.org/issue25285]: Tools/buildbot/test.bat script now uses -j1 by default to run each test file in fresh child process.

Windows

bpo-27064 [https://bugs.python.org/issue27064]: The py.exe launcher now defaults to Python 3. The Windows launcher py.exe no longer prefers an installed Python 2 version over Python 3 by default when used interactively.

构建

bpo-27229 [https://bugs.python.org/issue27229]: Fix the cross-compiling pgen rule for in-tree builds. Patch by Xavier de Gaye.

bpo-26930 [https://bugs.python.org/issue26930]: 更新OS X 10.5+（仅32位）安装程序以构建并链接OpenSSL 1.0.2h。

Windows

bpo-17500 [https://bugs.python.org/issue17500]: Remove unused and outdated icons. (See also: https://github.com/python/pythondotorg/issues/945)

C API

bpo-27186 [https://bugs.python.org/issue27186]: Add the PyOS_FSPath() function (part of PEP 519).

bpo-26282 [https://bugs.python.org/issue26282]: PyArg_ParseTupleAndKeywords() now supports positional-only parameters.

工具/示例

bpo-26282 [https://bugs.python.org/issue26282]: Argument Clinic now supports positional-only and keyword parameters in the same function.

Python 3.6.0 alpha 1

发布日期: 2016-05-16

核心与内置

bpo-20041 [https://bugs.python.org/issue20041]: Fixed TypeError when frame.f_trace is set to None. Patch by Xavier de Gaye.

bpo-26168 [https://bugs.python.org/issue26168]: Fixed possible refleaks in failing Py_BuildValue() with the “N” format unit.

bpo-26991 [https://bugs.python.org/issue26991]: Fix possible refleak when creating a function with annotations.

bpo-27039 [https://bugs.python.org/issue27039]: Fixed bytearray.remove() for values greater than 127. Based on patch by Joe Jevnik.

bpo-23640 [https://bugs.python.org/issue23640]: int.from_bytes() no longer bypasses constructors for subclasses.

bpo-27005 [https://bugs.python.org/issue27005]: Optimized the float.fromhex() class method for exact float. It is now 2 times faster.

bpo-18531 [https://bugs.python.org/issue18531]: Single var-keyword argument of dict subtype was passed unscathed to the C-defined function. Now it is converted to exact dict.

bpo-26811 [https://bugs.python.org/issue26811]: gc.get_objects() no longer contains a broken tuple with NULL pointer.

bpo-20120 [https://bugs.python.org/issue20120]: Use RawConfigParser for .pypirc parsing, removing support for interpolation unintentionally added with move to Python 3. Behavior no longer does any interpolation in .pypirc files, matching behavior in Python 2.7 and Setuptools 19.0.

bpo-26249 [https://bugs.python.org/issue26249]: Memory functions of the PyMem_Malloc() domain (PYMEM_DOMAIN_MEM) now use the pymalloc allocator rather than system malloc(). Applications calling PyMem_Malloc() without holding the GIL can now crash: use PYTHONMALLOC=debug environment variable to validate the usage of memory allocators in your application.

bpo-26802 [https://bugs.python.org/issue26802]: Optimize function calls only using unpacking like func(*tuple) (no other positional argument, no keyword): avoid copying the tuple. Patch written by Joe Jevnik.

bpo-26659 [https://bugs.python.org/issue26659]: Make the builtin slice type support cycle collection.

bpo-26718 [https://bugs.python.org/issue26718]: super.__init__ no longer leaks memory if called multiple times. NOTE: A direct call of super.__init__ is not endorsed!

bpo-27138 [https://bugs.python.org/issue27138]: Fix the doc comment for FileFinder.find_spec().

bpo-27147 [https://bugs.python.org/issue27147]: Mention PEP 420 in the importlib docs.

bpo-25339 [https://bugs.python.org/issue25339]: PYTHONIOENCODING now has priority over locale in setting the error handler for stdin and stdout.

bpo-26494 [https://bugs.python.org/issue26494]: Fixed crash on iterating exhausting iterators. Affected classes are generic sequence iterators, iterators of str, bytes, bytearray, list, tuple, set, frozenset, dict, OrderedDict, corresponding views and os.scandir() iterator.

bpo-26574 [https://bugs.python.org/issue26574]: Optimize bytes.replace(b'', b'.') and bytearray.replace(b'', b'.'). Patch written by Josh Snider.

bpo-26581 [https://bugs.python.org/issue26581]: If coding cookie is specified multiple times on a line in Python source code file, only the first one is taken to account.

bpo-19711 [https://bugs.python.org/issue19711]: Add tests for reloading namespace packages.

bpo-21099 [https://bugs.python.org/issue21099]: Switch applicable importlib tests to use PEP 451 API.

bpo-26563 [https://bugs.python.org/issue26563]: Debug hooks on Python memory allocators now raise a fatal error if functions of the PyMem_Malloc() family are called without holding the GIL.

bpo-26564 [https://bugs.python.org/issue26564]: On error, the debug hooks on Python memory allocators now use the tracemalloc module to get the traceback where a memory block was allocated.

bpo-26558 [https://bugs.python.org/issue26558]: The debug hooks on Python memory allocator PyObject_Malloc() now detect when functions are called without holding the GIL.

bpo-26516 [https://bugs.python.org/issue26516]: Add PYTHONMALLOC environment variable to set the Python memory allocators and/or install debug hooks.

bpo-26516 [https://bugs.python.org/issue26516]: The PyMem_SetupDebugHooks() function can now also be used on Python compiled in release mode.

bpo-26516 [https://bugs.python.org/issue26516]: The PYTHONMALLOCSTATS environment variable can now also be used on Python compiled in release mode. It now has no effect if set to an empty string.

bpo-26516 [https://bugs.python.org/issue26516]: In debug mode, debug hooks are now also installed on Python memory allocators when Python is configured without pymalloc.

bpo-26464 [https://bugs.python.org/issue26464]: Fix str.translate() when string is ASCII and first replacements removes character, but next replacement uses a non-ASCII character or a string longer than 1 character. Regression introduced in Python 3.5.0.

bpo-22836 [https://bugs.python.org/issue22836]: Ensure exception reports from PyErr_Display() and PyErr_WriteUnraisable() are sensible even when formatting them produces secondary errors. This affects the reports produced by sys.__excepthook__() and when __del__() raises an exception.

bpo-26302 [https://bugs.python.org/issue26302]: Correct behavior to reject comma as a legal character for cookie names.

bpo-26136 [https://bugs.python.org/issue26136]: Upgrade the warning when a generator raises StopIteration from PendingDeprecationWarning to DeprecationWarning. Patch by Anish Shah.

bpo-26204 [https://bugs.python.org/issue26204]: The compiler now ignores all constant statements: bytes, str, int, float, complex, name constants (None, False, True), Ellipsis and ast.Constant; not only str and int. For example, 1.0 is now ignored in def f(): 1.0.

bpo-4806 [https://bugs.python.org/issue4806]: Avoid masking the original TypeError exception when using star (*) unpacking in function calls. Based on patch by Hagen Fürstenau and Daniel Urban.

bpo-26146 [https://bugs.python.org/issue26146]: Add a new kind of AST node: ast.Constant. It can be used by external AST optimizers, but the compiler does not emit directly such node.

bpo-23601 [https://bugs.python.org/issue23601]: Sped-up allocation of dict key objects by using Python’s small object allocator. (Contributed by Julian Taylor.)

bpo-18018 [https://bugs.python.org/issue18018]: Import raises ImportError instead of SystemError if a relative import is attempted without a known parent package.

bpo-25843 [https://bugs.python.org/issue25843]: When compiling code, don’t merge constants if they are equal but have a different types. For example, f1, f2 = lambda: 1, lambda: 1.0 is now correctly compiled to two different functions: f1() returns 1 (int) and f2() returns 1.0 (float), even if 1 and 1.0 are equal.

bpo-26107 [https://bugs.python.org/issue26107]: The format of the co_lnotab attribute of code objects changes to support negative line number delta.

bpo-26154 [https://bugs.python.org/issue26154]: Add a new private _PyThreadState_UncheckedGet() function to get the current Python thread state, but don’t issue a fatal error if it is NULL. This new function must be used instead of accessing directly the _PyThreadState_Current variable. The variable is no more exposed since Python 3.5.1 to hide the exact implementation of atomic C types, to avoid compiler issues.

bpo-25791 [https://bugs.python.org/issue25791]: If __package__ != __spec__.parent or if neither __package__ or __spec__ are defined then ImportWarning is raised.

bpo-22995 [https://bugs.python.org/issue22995]: [UPDATE] Comment out the one of the pickleability tests in _PyObject_GetState() due to regressions observed in Cython-based projects.

bpo-25961 [https://bugs.python.org/issue25961]: Disallowed null characters in the type name.

bpo-25973 [https://bugs.python.org/issue25973]: Fix segfault when an invalid nonlocal statement binds a name starting with two underscores.

bpo-22995 [https://bugs.python.org/issue22995]: Instances of extension types with a state that aren’t subclasses of list or dict and haven’t implemented any pickle-related methods (__reduce__, __reduce_ex__, __getnewargs__, __getnewargs_ex__, or __getstate__), can no longer be pickled. Including memoryview.

bpo-20440 [https://bugs.python.org/issue20440]: Massive replacing unsafe attribute setting code with special macro Py_SETREF.

bpo-25766 [https://bugs.python.org/issue25766]: Special method __bytes__() now works in str subclasses.

bpo-25421 [https://bugs.python.org/issue25421]: __sizeof__ methods of builtin types now use dynamic basic size. This allows sys.getsize() to work correctly with their subclasses with __slots__ defined.

bpo-25709 [https://bugs.python.org/issue25709]: Fixed problem with in-place string concatenation and utf-8 cache.

bpo-5319 [https://bugs.python.org/issue5319]: New Py_FinalizeEx() API allowing Python to set an exit status of 120 on failure to flush buffered streams.

bpo-25485 [https://bugs.python.org/issue25485]: telnetlib.Telnet is now a context manager.

bpo-24097 [https://bugs.python.org/issue24097]: Fixed crash in object.__reduce__() if slot name is freed inside __getattr__.

bpo-24731 [https://bugs.python.org/issue24731]: Fixed crash on converting objects with special methods __bytes__, __trunc__, and __float__ returning instances of subclasses of bytes, int, and float to subclasses of bytes, int, and float correspondingly.

bpo-25630 [https://bugs.python.org/issue25630]: Fix a possible segfault during argument parsing in functions that accept filesystem paths.

bpo-23564 [https://bugs.python.org/issue23564]: Fixed a partially broken sanity check in the _posixsubprocess internals regarding how fds_to_pass were passed to the child. The bug had no actual impact as subprocess.py already avoided it.

bpo-25388 [https://bugs.python.org/issue25388]: Fixed tokenizer crash when processing undecodable source code with a null byte.

bpo-25462 [https://bugs.python.org/issue25462]: The hash of the key now is calculated only once in most operations in C implementation of OrderedDict.

bpo-22995 [https://bugs.python.org/issue22995]: Default implementation of __reduce__ and __reduce_ex__ now rejects builtin types with not defined __new__.

bpo-24802 [https://bugs.python.org/issue24802]: Avoid buffer overreads when int(), float(), compile(), exec() and eval() are passed bytes-like objects. These objects are not necessarily terminated by a null byte, but the functions assumed they were.

bpo-25555 [https://bugs.python.org/issue25555]: Fix parser and AST: fill lineno and col_offset of “arg” node when compiling AST from Python objects.

bpo-24726 [https://bugs.python.org/issue24726]: Fixed a crash and leaking NULL in repr() of OrderedDict that was mutated by direct calls of dict methods.

bpo-25449 [https://bugs.python.org/issue25449]: Iterating OrderedDict with keys with unstable hash now raises KeyError in C implementations as well as in Python implementation.

bpo-25395 [https://bugs.python.org/issue25395]: Fixed crash when highly nested OrderedDict structures were garbage collected.

bpo-25401 [https://bugs.python.org/issue25401]: Optimize bytes.fromhex() and bytearray.fromhex(): they are now between 2x and 3.5x faster.

bpo-25399 [https://bugs.python.org/issue25399]: Optimize bytearray % args using the new private _PyBytesWriter API. Formatting is now between 2.5 and 5 times faster.

bpo-25274 [https://bugs.python.org/issue25274]: sys.setrecursionlimit() now raises a RecursionError if the new recursion limit is too low depending at the current recursion depth. Modify also the “lower-water mark” formula to make it monotonic. This mark is used to decide when the overflowed flag of the thread state is reset.

bpo-24402 [https://bugs.python.org/issue24402]: Fix input() to prompt to the redirected stdout when sys.stdout.fileno() fails.

bpo-25349 [https://bugs.python.org/issue25349]: Optimize bytes % args using the new private _PyBytesWriter API. Formatting is now up to 2 times faster.

bpo-24806 [https://bugs.python.org/issue24806]: Prevent builtin types that are not allowed to be subclassed from being subclassed through multiple inheritance.

bpo-25301 [https://bugs.python.org/issue25301]: The UTF-8 decoder is now up to 15 times as fast for error handlers: ignore, replace and surrogateescape.

bpo-24848 [https://bugs.python.org/issue24848]: Fixed a number of bugs in UTF-7 decoding of misformed data.

bpo-25267 [https://bugs.python.org/issue25267]: The UTF-8 encoder is now up to 75 times as fast for error handlers: ignore, replace, surrogateescape, surrogatepass. Patch co-written with Serhiy Storchaka.

bpo-25280 [https://bugs.python.org/issue25280]: Import trace messages emitted in verbose (-v) mode are no longer formatted twice.

bpo-25227 [https://bugs.python.org/issue25227]: Optimize ASCII and latin1 encoders with the surrogateescape error handler: the encoders are now up to 3 times as fast. Initial patch written by Serhiy Storchaka.

bpo-25003 [https://bugs.python.org/issue25003]: On Solaris 11.3 or newer, os.urandom() now uses the getrandom() function instead of the getentropy() function. The getentropy() function is blocking to generate very good quality entropy, os.urandom() doesn’t need such high-quality entropy.

bpo-9232 [https://bugs.python.org/issue9232]: Modify Python’s grammar to allow trailing commas in the argument list of a function declaration. For example, “def f(*, a = 3,): pass” is now legal. Patch from Mark Dickinson.

bpo-24965 [https://bugs.python.org/issue24965]: Implement PEP 498 “Literal String Interpolation”. This allows you to embed expressions inside f-strings, which are converted to normal strings at run time. Given x=3, then f’value={x}’ == ‘value=3’. Patch by Eric V. Smith.

bpo-26478 [https://bugs.python.org/issue26478]: Fix semantic bugs when using binary operators with dictionary views and tuples.

bpo-26171 [https://bugs.python.org/issue26171]: Fix possible integer overflow and heap corruption in zipimporter.get_data().

bpo-25660 [https://bugs.python.org/issue25660]: Fix TAB key behaviour in REPL with readline.

bpo-26288 [https://bugs.python.org/issue26288]: Optimize PyLong_AsDouble.

bpo-26289 [https://bugs.python.org/issue26289]: Optimize floor and modulo division for single-digit longs. Microbenchmarks show 2-2.5x improvement. Built-in ‘divmod’ function is now also ~10% faster. (See also: bpo-26315 [https://bugs.python.org/issue26315])

bpo-25887 [https://bugs.python.org/issue25887]: Raise a RuntimeError when a coroutine object is awaited more than once.

库

bpo-27057 [https://bugs.python.org/issue27057]: Fix os.set_inheritable() on Android, ioctl() is blocked by SELinux and fails with EACCESS. The function now falls back to fcntl(). Patch written by Michał Bednarski.

bpo-27014 [https://bugs.python.org/issue27014]: Fix infinite recursion using typing.py. Thanks to Kalle Tuure!

bpo-27031 [https://bugs.python.org/issue27031]: Removed dummy methods in Tkinter widget classes: tk_menuBar() and tk_bindForTraversal().

bpo-14132 [https://bugs.python.org/issue14132]: Fix urllib.request redirect handling when the target only has a query string. Original fix by Ján Janech.

bpo-17214 [https://bugs.python.org/issue17214]: The “urllib.request” module now percent-encodes non-ASCII bytes found in redirect target URLs. Some servers send Location header fields with non-ASCII bytes, but “http.client” requires the request target to be ASCII-encodable, otherwise a UnicodeEncodeError is raised. Based on patch by Christian Heimes.

bpo-27033 [https://bugs.python.org/issue27033]: The default value of the decode_data parameter for smtpd.SMTPChannel and smtpd.SMTPServer constructors is changed to False.

bpo-27034 [https://bugs.python.org/issue27034]: Removed deprecated class asynchat.fifo.

bpo-26870 [https://bugs.python.org/issue26870]: Added readline.set_auto_history(), which can stop entries being automatically added to the history list. Based on patch by Tyler Crompton.

bpo-26039 [https://bugs.python.org/issue26039]: zipfile.ZipFile.open() can now be used to write data into a ZIP file, as well as for extracting data. Patch by Thomas Kluyver.

bpo-26892 [https://bugs.python.org/issue26892]: Honor debuglevel flag in urllib.request.HTTPHandler. Patch contributed by Chi Hsuan Yen.

bpo-22274 [https://bugs.python.org/issue22274]: In the subprocess module, allow stderr to be redirected to stdout even when stdout is not redirected. Patch by Akira Li.

bpo-26807 [https://bugs.python.org/issue26807]: mock_open ‘files’ no longer error on readline at end of file. Patch from Yolanda Robla.

bpo-25745 [https://bugs.python.org/issue25745]: Fixed leaking a userptr in curses panel destructor.

bpo-26977 [https://bugs.python.org/issue26977]: Removed unnecessary, and ignored, call to sum of squares helper in statistics.pvariance.

bpo-26002 [https://bugs.python.org/issue26002]: Use bisect in statistics.median instead of a linear search. Patch by Upendra Kuma.

bpo-25974 [https://bugs.python.org/issue25974]: Make use of new Decimal.as_integer_ratio() method in statistics module. Patch by Stefan Krah.

bpo-26996 [https://bugs.python.org/issue26996]: Add secrets module as described in PEP 506.

bpo-26881 [https://bugs.python.org/issue26881]: The modulefinder module now supports extended opcode arguments.

bpo-23815 [https://bugs.python.org/issue23815]: Fixed crashes related to directly created instances of types in _tkinter and curses.panel modules.

bpo-17765 [https://bugs.python.org/issue17765]: weakref.ref() no longer silently ignores keyword arguments. Patch by Georg Brandl.

bpo-26873 [https://bugs.python.org/issue26873]: xmlrpc now raises ResponseError on unsupported type tags instead of silently return incorrect result.

bpo-26915 [https://bugs.python.org/issue26915]: The __contains__ methods in the collections ABCs now check for identity before checking equality. This better matches the behavior of the concrete classes, allows sensible handling of NaNs, and makes it easier to reason about container invariants.

bpo-26711 [https://bugs.python.org/issue26711]: Fixed the comparison of plistlib.Data with other types.

bpo-24114 [https://bugs.python.org/issue24114]: Fix an uninitialized variable in ctypes.util.

The bug only occurs on SunOS when the ctypes implementation searches for the crle program. Patch by Xiang Zhang. Tested on SunOS by Kees Bos.

bpo-26864 [https://bugs.python.org/issue26864]: In urllib.request, change the proxy bypass host checking against no_proxy to be case-insensitive, and to not match unrelated host names that happen to have a bypassed hostname as a suffix. Patch by Xiang Zhang.

bpo-24902 [https://bugs.python.org/issue24902]: Print server URL on http.server startup. Initial patch by Felix Kaiser.

bpo-25788 [https://bugs.python.org/issue25788]: fileinput.hook_encoded() now supports an “errors” argument for passing to open. Original patch by Joseph Hackman.

bpo-26634 [https://bugs.python.org/issue26634]: recursive_repr() now sets __qualname__ of wrapper. Patch by Xiang Zhang.

bpo-26804 [https://bugs.python.org/issue26804]: urllib.request will prefer lower_case proxy environment variables over UPPER_CASE or Mixed_Case ones. Patch contributed by Hans-Peter Jansen.

bpo-26837 [https://bugs.python.org/issue26837]: assertSequenceEqual() now correctly outputs non-stringified differing items (like bytes in the -b mode). This affects assertListEqual() and assertTupleEqual().

bpo-26041 [https://bugs.python.org/issue26041]: Remove “will be removed in Python 3.7” from deprecation messages of platform.dist() and platform.linux_distribution(). Patch by Kumaripaba Miyurusara Athukorala.

bpo-26822 [https://bugs.python.org/issue26822]: itemgetter, attrgetter and methodcaller objects no longer silently ignore keyword arguments.

bpo-26733 [https://bugs.python.org/issue26733]: Disassembling a class now disassembles class and static methods. Patch by Xiang Zhang.

bpo-26801 [https://bugs.python.org/issue26801]: Fix error handling in shutil.get_terminal_size(), catch AttributeError instead of NameError. Patch written by Emanuel Barry.

bpo-24838 [https://bugs.python.org/issue24838]: tarfile’s ustar and gnu formats now correctly calculate name and link field limits for multibyte character encodings like utf-8.

安全

bpo-26657 [https://bugs.python.org/issue26657]: Fix directory traversal vulnerability with http.server on Windows. This fixes a regression that was introduced in 3.3.4rc1 and 3.4.0rc1. Based on patch by Philipp Hagemeister.

库

bpo-26717 [https://bugs.python.org/issue26717]: Stop encoding Latin-1-ized WSGI paths with UTF-8. Patch by Anthony Sottile.

bpo-26782 [https://bugs.python.org/issue26782]: Add STARTUPINFO to subprocess.__all__ on Windows.

bpo-26404 [https://bugs.python.org/issue26404]: Add context manager to socketserver. Patch by Aviv Palivoda.

bpo-26735 [https://bugs.python.org/issue26735]: Fix os.urandom() on Solaris 11.3 and newer when reading more than 1,024 bytes: call getrandom() multiple times with a limit of 1024 bytes per call.

bpo-26585 [https://bugs.python.org/issue26585]: Eliminate http.server._quote_html() and use html.escape(quote=False). Patch by Xiang Zhang.

bpo-26685 [https://bugs.python.org/issue26685]: Raise OSError if closing a socket fails.

bpo-16329 [https://bugs.python.org/issue16329]: Add .webm to mimetypes.types_map. Patch by Giampaolo Rodola’.

bpo-13952 [https://bugs.python.org/issue13952]: Add .csv to mimetypes.types_map. Patch by Geoff Wilson.

bpo-26587 [https://bugs.python.org/issue26587]: the site module now allows .pth files to specify files to be added to sys.path (e.g. zip files).

bpo-25609 [https://bugs.python.org/issue25609]: Introduce contextlib.AbstractContextManager and typing.ContextManager.

bpo-26709 [https://bugs.python.org/issue26709]: Fixed Y2038 problem in loading binary PLists.

bpo-23735 [https://bugs.python.org/issue23735]: Handle terminal resizing with Readline 6.3+ by installing our own SIGWINCH handler. Patch by Eric Price.

bpo-25951 [https://bugs.python.org/issue25951]: Change SSLSocket.sendall() to return None, as explicitly documented for plain socket objects. Patch by Aviv Palivoda.

bpo-26586 [https://bugs.python.org/issue26586]: In http.server, respond with “413 Request header fields too large” if there are too many header fields to parse, rather than killing the connection and raising an unhandled exception. Patch by Xiang Zhang.

bpo-26676 [https://bugs.python.org/issue26676]: Added missing XMLPullParser to ElementTree.__all__.

bpo-22854 [https://bugs.python.org/issue22854]: Change BufferedReader.writable() and BufferedWriter.readable() to always return False.

bpo-26492 [https://bugs.python.org/issue26492]: Exhausted iterator of array.array now conforms with the behavior of iterators of other mutable sequences: it lefts exhausted even if iterated array is extended.

bpo-26641 [https://bugs.python.org/issue26641]: doctest.DocFileTest and doctest.testfile() now support packages (module splitted into multiple directories) for the package parameter.

bpo-25195 [https://bugs.python.org/issue25195]: Fix a regression in mock.MagicMock. _Call is a subclass of tuple (changeset 3603bae63c13 only works for classes) so we need to implement __ne__ ourselves. Patch by Andrew Plummer.

bpo-26644 [https://bugs.python.org/issue26644]: Raise ValueError rather than SystemError when a negative length is passed to SSLSocket.recv() or read().

bpo-23804 [https://bugs.python.org/issue23804]: Fix SSL recv(0) and read(0) methods to return zero bytes instead of up to 1024.

bpo-26616 [https://bugs.python.org/issue26616]: Fixed a bug in datetime.astimezone() method.

bpo-26637 [https://bugs.python.org/issue26637]: The importlib module now emits an ImportError rather than a TypeError if __import__() is tried during the Python shutdown process but sys.path is already cleared (set to None).

bpo-21925 [https://bugs.python.org/issue21925]: warnings.formatwarning() now catches exceptions when calling linecache.getline() and tracemalloc.get_object_traceback() to be able to log ResourceWarning emitted late during the Python shutdown process.

bpo-23848 [https://bugs.python.org/issue23848]: On Windows, faulthandler.enable() now also installs an exception handler to dump the traceback of all Python threads on any Windows exception, not only on UNIX signals (SIGSEGV, SIGFPE, SIGABRT).

bpo-26530 [https://bugs.python.org/issue26530]: Add C functions _PyTraceMalloc_Track() and _PyTraceMalloc_Untrack() to track memory blocks using the tracemalloc module. Add _PyTraceMalloc_GetTraceback() to get the traceback of an object.

bpo-26588 [https://bugs.python.org/issue26588]: The _tracemalloc now supports tracing memory allocations of multiple address spaces (domains).

bpo-24266 [https://bugs.python.org/issue24266]: Ctrl+C during Readline history search now cancels the search mode when compiled with Readline 7.

bpo-26590 [https://bugs.python.org/issue26590]: Implement a safe finalizer for the _socket.socket type. It now releases the GIL to close the socket.

bpo-18787 [https://bugs.python.org/issue18787]: spwd.getspnam() now raises a PermissionError if the user doesn’t have privileges.

bpo-26560 [https://bugs.python.org/issue26560]: Avoid potential ValueError in BaseHandler.start_response. Initial patch by Peter Inglesby.

bpo-26567 [https://bugs.python.org/issue26567]: Add a new function PyErr_ResourceWarning() function to pass the destroyed object. Add a source attribute to warnings.WarningMessage. Add warnings._showwarnmsg() which uses tracemalloc to get the traceback where source object was allocated.

安全

bpo-26313 [https://bugs.python.org/issue26313]: ssl.py _load_windows_store_certs fails if windows cert store is empty. Patch by Baji.

库

bpo-26569 [https://bugs.python.org/issue26569]: Fix pyclbr.readmodule() and pyclbr.readmodule_ex() to support importing packages.

bpo-26499 [https://bugs.python.org/issue26499]: Account for remaining Content-Length in HTTPResponse.readline() and read1(). Based on patch by Silent Ghost. Also document that HTTPResponse now supports these methods.

bpo-25320 [https://bugs.python.org/issue25320]: Handle sockets in directories unittest discovery is scanning. Patch from Victor van den Elzen.

bpo-16181 [https://bugs.python.org/issue16181]: cookiejar.http2time() now returns None if year is higher than datetime.MAXYEAR.

bpo-26513 [https://bugs.python.org/issue26513]: Fixes platform module detection of Windows Server

bpo-23718 [https://bugs.python.org/issue23718]: Fixed parsing time in week 0 before Jan 1. Original patch by Tamás Bence Gedai.

bpo-26323 [https://bugs.python.org/issue26323]: Add Mock.assert_called() and Mock.assert_called_once() methods to unittest.mock. Patch written by Amit Saha.

bpo-20589 [https://bugs.python.org/issue20589]: Invoking Path.owner() and Path.group() on Windows now raise NotImplementedError instead of ImportError.

bpo-26177 [https://bugs.python.org/issue26177]: Fixed the keys() method for Canvas and Scrollbar widgets.

bpo-15068 [https://bugs.python.org/issue15068]: Got rid of excessive buffering in fileinput. The bufsize parameter is now deprecated and ignored.

bpo-19475 [https://bugs.python.org/issue19475]: Added an optional argument timespec to the datetime isoformat() method to choose the precision of the time component.

bpo-2202 [https://bugs.python.org/issue2202]: Fix UnboundLocalError in AbstractDigestAuthHandler.get_algorithm_impls. Initial patch by Mathieu Dupuy.

bpo-26167 [https://bugs.python.org/issue26167]: Minimized overhead in copy.copy() and copy.deepcopy(). Optimized copying and deepcopying bytearrays, NotImplemented, slices, short lists, tuples, dicts, sets.

bpo-25718 [https://bugs.python.org/issue25718]: Fixed pickling and copying the accumulate() iterator with total is None.

bpo-26475 [https://bugs.python.org/issue26475]: Fixed debugging output for regular expressions with the (?x) flag.

bpo-26482 [https://bugs.python.org/issue26482]: Allowed pickling recursive dequeues.

bpo-26335 [https://bugs.python.org/issue26335]: Make mmap.write() return the number of bytes written like other write methods. Patch by Jakub Stasiak.

bpo-26457 [https://bugs.python.org/issue26457]: Fixed the subnets() methods in IP network classes for the case when resulting prefix length is equal to maximal prefix length. Based on patch by Xiang Zhang.

bpo-26385 [https://bugs.python.org/issue26385]: Remove the file if the internal open() call in NamedTemporaryFile() fails. Patch by Silent Ghost.

bpo-26402 [https://bugs.python.org/issue26402]: Fix XML-RPC client to retry when the server shuts down a persistent connection. This was a regression related to the new http.client.RemoteDisconnected exception in 3.5.0a4.

bpo-25913 [https://bugs.python.org/issue25913]: Leading <~ is optional now in base64.a85decode() with adobe=True. Patch by Swati Jaiswal.

bpo-26186 [https://bugs.python.org/issue26186]: Remove an invalid type check in importlib.util.LazyLoader.

bpo-26367 [https://bugs.python.org/issue26367]: importlib.__import__() raises ImportError like builtins.__import__() when level is specified but without an accompanying package specified.

bpo-26309 [https://bugs.python.org/issue26309]: In the “socketserver” module, shut down the request (closing the connected socket) when verify_request() returns false. Patch by Aviv Palivoda.

bpo-23430 [https://bugs.python.org/issue23430]: Change the socketserver module to only catch exceptions raised from a request handler that are derived from Exception (instead of BaseException). Therefore SystemExit and KeyboardInterrupt no longer trigger the handle_error() method, and will now to stop a single-threaded server.

安全

bpo-25939 [https://bugs.python.org/issue25939]: On Windows open the cert store readonly in ssl.enum_certificates.

库

bpo-25995 [https://bugs.python.org/issue25995]: os.walk() no longer uses FDs proportional to the tree depth.

bpo-25994 [https://bugs.python.org/issue25994]: Added the close() method and the support of the context manager protocol for the os.scandir() iterator.

bpo-23992 [https://bugs.python.org/issue23992]: multiprocessing: make MapResult not fail-fast upon exception.

bpo-26243 [https://bugs.python.org/issue26243]: Support keyword arguments to zlib.compress(). Patch by Aviv Palivoda.

bpo-26117 [https://bugs.python.org/issue26117]: The os.scandir() iterator now closes file descriptor not only when the iteration is finished, but when it was failed with error.

bpo-25949 [https://bugs.python.org/issue25949]: __dict__ for an OrderedDict instance is now created only when needed.

bpo-25911 [https://bugs.python.org/issue25911]: Restored support of bytes paths in os.walk() on Windows.

bpo-26045 [https://bugs.python.org/issue26045]: Add UTF-8 suggestion to error message when posting a non-Latin-1 string with http.client.

bpo-26039 [https://bugs.python.org/issue26039]: Added zipfile.ZipInfo.from_file() and zipinfo.ZipInfo.is_dir(). Patch by Thomas Kluyver.

bpo-12923 [https://bugs.python.org/issue12923]: Reset FancyURLopener’s redirect counter even if there is an exception. Based on patches by Brian Brazil and Daniel Rocco.

bpo-25945 [https://bugs.python.org/issue25945]: Fixed a crash when unpickle the functools.partial object with wrong state. Fixed a leak in failed functools.partial constructor. “args” and “keywords” attributes of functools.partial have now always types tuple and dict correspondingly.

bpo-26202 [https://bugs.python.org/issue26202]: copy.deepcopy() now correctly copies range() objects with non-atomic attributes.

bpo-23076 [https://bugs.python.org/issue23076]: Path.glob() now raises a ValueError if it’s called with an invalid pattern. Patch by Thomas Nyberg.

bpo-19883 [https://bugs.python.org/issue19883]: Fixed possible integer overflows in zipimport.

bpo-26227 [https://bugs.python.org/issue26227]: On Windows, getnameinfo(), gethostbyaddr() and gethostbyname_ex() functions of the socket module now decode the hostname from the ANSI code page rather than UTF-8.

bpo-26099 [https://bugs.python.org/issue26099]: The site module now writes an error into stderr if sitecustomize module can be imported but executing the module raise an ImportError. Same change for usercustomize.

bpo-26147 [https://bugs.python.org/issue26147]: xmlrpc now works with strings not encodable with used non-UTF-8 encoding.

bpo-25935 [https://bugs.python.org/issue25935]: Garbage collector now breaks reference loops with OrderedDict.

bpo-16620 [https://bugs.python.org/issue16620]: Fixed AttributeError in msilib.Directory.glob().

bpo-26013 [https://bugs.python.org/issue26013]: Added compatibility with broken protocol 2 pickles created in old Python 3 versions (3.4.3 and lower).

bpo-26129 [https://bugs.python.org/issue26129]: Deprecated accepting non-integers in grp.getgrgid().

bpo-25850 [https://bugs.python.org/issue25850]: Use cross-compilation by default for 64-bit Windows.

bpo-25822 [https://bugs.python.org/issue25822]: Add docstrings to the fields of urllib.parse results. Patch contributed by Swati Jaiswal.

bpo-22642 [https://bugs.python.org/issue22642]: Convert trace module option parsing mechanism to argparse. Patch contributed by SilentGhost.

bpo-24705 [https://bugs.python.org/issue24705]: Fix sysconfig._parse_makefile not expanding ${} vars appearing before $() vars.

bpo-26069 [https://bugs.python.org/issue26069]: Remove the deprecated apis in the trace module.

bpo-22138 [https://bugs.python.org/issue22138]: Fix mock.patch behavior when patching descriptors. Restore original values after patching. Patch contributed by Sean McCully.

bpo-25672 [https://bugs.python.org/issue25672]: In the ssl module, enable the SSL_MODE_RELEASE_BUFFERS mode option if it is safe to do so.

bpo-26012 [https://bugs.python.org/issue26012]: Don’t traverse into symlinks for ** pattern in pathlib.Path.[r]glob().

bpo-24120 [https://bugs.python.org/issue24120]: Ignore PermissionError when traversing a tree with pathlib.Path.[r]glob(). Patch by Ulrich Petri.

bpo-21815 [https://bugs.python.org/issue21815]: Accept ] characters in the data portion of imap responses, in order to handle the flags with square brackets accepted and produced by servers such as gmail.

bpo-25447 [https://bugs.python.org/issue25447]: fileinput now uses sys.stdin as-is if it does not have a buffer attribute (restores backward compatibility).

bpo-25971 [https://bugs.python.org/issue25971]: Optimized creating Fractions from floats by 2 times and from Decimals by 3 times.

bpo-25802 [https://bugs.python.org/issue25802]: Document as deprecated the remaining implementations of importlib.abc.Loader.load_module().

bpo-25928 [https://bugs.python.org/issue25928]: Add Decimal.as_integer_ratio().

bpo-25447 [https://bugs.python.org/issue25447]: Copying the lru_cache() wrapper object now always works, independently from the type of the wrapped object (by returning the original object unchanged).

bpo-25768 [https://bugs.python.org/issue25768]: Have the functions in compileall return booleans instead of ints and add proper documentation and tests for the return values.

bpo-24103 [https://bugs.python.org/issue24103]: Fixed possible use after free in ElementTree.XMLPullParser.

bpo-25860 [https://bugs.python.org/issue25860]: os.fwalk() no longer skips remaining directories when error occurs. Original patch by Samson Lee.

bpo-25914 [https://bugs.python.org/issue25914]: Fixed and simplified OrderedDict.__sizeof__.

bpo-25869 [https://bugs.python.org/issue25869]: Optimized deepcopying ElementTree; it is now 20 times faster.

bpo-25873 [https://bugs.python.org/issue25873]: Optimized iterating ElementTree. Iterating elements Element.iter() is now 40% faster, iterating text Element.itertext() is now up to 2.5 times faster.

bpo-25902 [https://bugs.python.org/issue25902]: Fixed various refcount issues in ElementTree iteration.

bpo-22227 [https://bugs.python.org/issue22227]: The TarFile iterator is reimplemented using generator. This implementation is simpler that using class.

bpo-25638 [https://bugs.python.org/issue25638]: Optimized ElementTree.iterparse(); it is now 2x faster. Optimized ElementTree parsing; it is now 10% faster.

bpo-25761 [https://bugs.python.org/issue25761]: Improved detecting errors in broken pickle data.

bpo-25717 [https://bugs.python.org/issue25717]: Restore the previous behaviour of tolerating most fstat() errors when opening files. This was a regression in 3.5a1, and stopped anonymous temporary files from working in special cases.

bpo-24903 [https://bugs.python.org/issue24903]: Fix regression in number of arguments compileall accepts when ‘-d’ is specified. The check on the number of arguments has been dropped completely as it never worked correctly anyway.

bpo-25764 [https://bugs.python.org/issue25764]: In the subprocess module, preserve any exception caused by fork() failure when preexec_fn is used.

bpo-25771 [https://bugs.python.org/issue25771]: Tweak the exception message for importlib.util.resolve_name() when ‘package’ isn’t specified but necessary.

bpo-6478 [https://bugs.python.org/issue6478]: _strptime’s regexp cache now is reset after changing timezone with time.tzset().

bpo-14285 [https://bugs.python.org/issue14285]: When executing a package with the “python -m package” option, and package initialization fails, a proper traceback is now reported. The “runpy” module now lets exceptions from package initialization pass back to the caller, rather than raising ImportError.

bpo-19771 [https://bugs.python.org/issue19771]: Also in runpy and the “-m” option, omit the irrelevant message “… is a package and cannot be directly executed” if the package could not even be initialized (e.g. due to a bad *.pyc file).

bpo-25177 [https://bugs.python.org/issue25177]: Fixed problem with the mean of very small and very large numbers. As a side effect, statistics.mean and statistics.variance should be significantly faster.

bpo-25718 [https://bugs.python.org/issue25718]: Fixed copying object with state with boolean value is false.

bpo-10131 [https://bugs.python.org/issue10131]: Fixed deep copying of minidom documents. Based on patch by Marian Ganisin.

bpo-7990 [https://bugs.python.org/issue7990]: dir() on ElementTree.Element now lists properties: “tag”, “text”, “tail” and “attrib”. Original patch by Santoso Wijaya.

bpo-25725 [https://bugs.python.org/issue25725]: Fixed a reference leak in pickle.loads() when unpickling invalid data including tuple instructions.

bpo-25663 [https://bugs.python.org/issue25663]: In the Readline completer, avoid listing duplicate global names, and search the global namespace before searching builtins.

bpo-25688 [https://bugs.python.org/issue25688]: Fixed file leak in ElementTree.iterparse() raising an error.

bpo-23914 [https://bugs.python.org/issue23914]: Fixed SystemError raised by unpickler on broken pickle data.

bpo-25691 [https://bugs.python.org/issue25691]: Fixed crash on deleting ElementTree.Element attributes.

bpo-25624 [https://bugs.python.org/issue25624]: ZipFile now always writes a ZIP_STORED header for directory entries. Patch by Dingyuan Wang.

bpo-25626 [https://bugs.python.org/issue25626]: Change three zlib functions to accept sizes that fit in Py_ssize_t, but internally cap those sizes to UINT_MAX. This resolves a regression in 3.5 where GzipFile.read() failed to read chunks larger than 2 or 4 GiB. The change affects the zlib.Decompress.decompress() max_length parameter, the zlib.decompress() bufsize parameter, and the zlib.Decompress.flush() length parameter.

bpo-25583 [https://bugs.python.org/issue25583]: Avoid incorrect errors raised by os.makedirs(exist_ok=True) when the OS gives priority to errors such as EACCES over EEXIST.

bpo-25593 [https://bugs.python.org/issue25593]: Change semantics of EventLoop.stop() in asyncio.

bpo-6973 [https://bugs.python.org/issue6973]: When we know a subprocess.Popen process has died, do not allow the send_signal(), terminate(), or kill() methods to do anything as they could potentially signal a different process.

bpo-23883 [https://bugs.python.org/issue23883]: Added missing APIs to __all__ to match the documented APIs for the following modules: calendar, csv, enum, fileinput, ftplib, logging, optparse, tarfile, threading and wave. Also added a test.support.check__all__() helper. Patches by Jacek Kołodziej, Mauro S. M. Rodrigues and Joel Taddei.

bpo-25590 [https://bugs.python.org/issue25590]: In the Readline completer, only call getattr() once per attribute. Also complete names of attributes such as properties and slots which are listed by dir() but not yet created on an instance.

bpo-25498 [https://bugs.python.org/issue25498]: Fix a crash when garbage-collecting ctypes objects created by wrapping a memoryview. This was a regression made in 3.5a1. Based on patch by Eryksun.

bpo-25584 [https://bugs.python.org/issue25584]: Added “escape” to the __all__ list in the glob module.

bpo-25584 [https://bugs.python.org/issue25584]: Fixed recursive glob() with patterns starting with **.

bpo-25446 [https://bugs.python.org/issue25446]: Fix regression in smtplib’s AUTH LOGIN support.

bpo-18010 [https://bugs.python.org/issue18010]: Fix the pydoc web server’s module search function to handle exceptions from importing packages.

bpo-25554 [https://bugs.python.org/issue25554]: Got rid of circular references in regular expression parsing.

bpo-18973 [https://bugs.python.org/issue18973]: Command-line interface of the calendar module now uses argparse instead of optparse.

bpo-25510 [https://bugs.python.org/issue25510]: fileinput.FileInput.readline() now returns b’’ instead of ‘’ at the end if the FileInput was opened with binary mode. Patch by Ryosuke Ito.

bpo-25503 [https://bugs.python.org/issue25503]: Fixed inspect.getdoc() for inherited docstrings of properties. Original patch by John Mark Vandenberg.

bpo-25515 [https://bugs.python.org/issue25515]: Always use os.urandom as a source of randomness in uuid.uuid4.

bpo-21827 [https://bugs.python.org/issue21827]: Fixed textwrap.dedent() for the case when largest common whitespace is a substring of smallest leading whitespace. Based on patch by Robert Li.

bpo-25447 [https://bugs.python.org/issue25447]: The lru_cache() wrapper objects now can be copied and pickled (by returning the original object unchanged).

bpo-25390 [https://bugs.python.org/issue25390]: typing: Don’t crash on Union[str, Pattern].

bpo-25441 [https://bugs.python.org/issue25441]: asyncio: Raise error from drain() when socket is closed.

bpo-25410 [https://bugs.python.org/issue25410]: Cleaned up and fixed minor bugs in C implementation of OrderedDict.

bpo-25411 [https://bugs.python.org/issue25411]: Improved Unicode support in SMTPHandler through better use of the email package. Thanks to user simon04 for the patch.

Move the imp module from a PendingDeprecationWarning to DeprecationWarning.

bpo-25407 [https://bugs.python.org/issue25407]: Remove mentions of the formatter module being removed in Python 3.6.

bpo-25406 [https://bugs.python.org/issue25406]: Fixed a bug in C implementation of OrderedDict.move_to_end() that caused segmentation fault or hang in iterating after moving several items to the start of ordered dict.

bpo-25382 [https://bugs.python.org/issue25382]: pickletools.dis() now outputs implicit memo index for the MEMOIZE opcode.

bpo-25357 [https://bugs.python.org/issue25357]: Add an optional newline parameter to binascii.b2a_base64(). base64.b64encode() uses it to avoid a memory copy.

bpo-24164 [https://bugs.python.org/issue24164]: Objects that need calling __new__ with keyword arguments, can now be pickled using pickle protocols older than protocol version 4.

bpo-25364 [https://bugs.python.org/issue25364]: zipfile now works in threads disabled builds.

bpo-25328 [https://bugs.python.org/issue25328]: smtpd’s SMTPChannel now correctly raises a ValueError if both decode_data and enable_SMTPUTF8 are set to true.

bpo-16099 [https://bugs.python.org/issue16099]: RobotFileParser now supports Crawl-delay and Request-rate extensions. Patch by Nikolay Bogoychev.

bpo-25316 [https://bugs.python.org/issue25316]: distutils raises OSError instead of DistutilsPlatformError when MSVC is not installed.

bpo-25380 [https://bugs.python.org/issue25380]: Fixed protocol for the STACK_GLOBAL opcode in pickletools.opcodes.

bpo-23972 [https://bugs.python.org/issue23972]: Updates asyncio datagram create method allowing reuseport and reuseaddr socket options to be set prior to binding the socket. Mirroring the existing asyncio create_server method the reuseaddr option for datagram sockets defaults to True if the O/S is ‘posix’ (except if the platform is Cygwin). Patch by Chris Laws.

bpo-25304 [https://bugs.python.org/issue25304]: Add asyncio.run_coroutine_threadsafe(). This lets you submit a coroutine to a loop from another thread, returning a concurrent.futures.Future. By Vincent Michel.

bpo-25232 [https://bugs.python.org/issue25232]: Fix CGIRequestHandler to split the query from the URL at the first question mark (?) rather than the last. Patch from Xiang Zhang.

bpo-24657 [https://bugs.python.org/issue24657]: Prevent CGIRequestHandler from collapsing slashes in the query part of the URL as if it were a path. Patch from Xiang Zhang.

bpo-25287 [https://bugs.python.org/issue25287]: Don’t add crypt.METHOD_CRYPT to crypt.methods if it’s not supported. Check if it is supported, it may not be supported on OpenBSD for example.

bpo-23600 [https://bugs.python.org/issue23600]: Default implementation of tzinfo.fromutc() was returning wrong results in some cases.

bpo-25203 [https://bugs.python.org/issue25203]: Failed readline.set_completer_delims() no longer left the module in inconsistent state.

bpo-25011 [https://bugs.python.org/issue25011]: rlcompleter now omits private and special attribute names unless the prefix starts with underscores.

bpo-25209 [https://bugs.python.org/issue25209]: rlcompleter now can add a space or a colon after completed keyword.

bpo-22241 [https://bugs.python.org/issue22241]: timezone.utc name is now plain ‘UTC’, not ‘UTC-00:00’.

bpo-23517 [https://bugs.python.org/issue23517]: fromtimestamp() and utcfromtimestamp() methods of datetime.datetime now round microseconds to nearest with ties going to nearest even integer (ROUND_HALF_EVEN), as round(float), instead of rounding towards -Infinity (ROUND_FLOOR).

bpo-23552 [https://bugs.python.org/issue23552]: Timeit now warns when there is substantial (4x) variance between best and worst times. Patch from Serhiy Storchaka.

bpo-24633 [https://bugs.python.org/issue24633]: site-packages/README -> README.txt.

bpo-24879 [https://bugs.python.org/issue24879]: help() and pydoc can now list named tuple fields in the order they were defined rather than alphabetically. The ordering is determined by the _fields attribute if present.

bpo-24874 [https://bugs.python.org/issue24874]: Improve speed of itertools.cycle() and make its pickle more compact.

Fix crash in itertools.cycle.__setstate__() when the first argument wasn’t a list.

bpo-20059 [https://bugs.python.org/issue20059]: urllib.parse raises ValueError on all invalid ports. Patch by Martin Panter.

bpo-24360 [https://bugs.python.org/issue24360]: Improve __repr__ of argparse.Namespace() for invalid identifiers. Patch by Matthias Bussonnier.

bpo-23426 [https://bugs.python.org/issue23426]: run_setup was broken in distutils. Patch from Alexander Belopolsky.

bpo-13938 [https://bugs.python.org/issue13938]: 2to3 converts StringTypes to a tuple. Patch from Mark Hammond.

bpo-2091 [https://bugs.python.org/issue2091]: open() accepted a ‘U’ mode string containing ‘+’, but ‘U’ can only be used with ‘r’. Patch from Jeff Balogh and John O’Connor.

bpo-8585 [https://bugs.python.org/issue8585]: improved tests for zipimporter2. Patch from Mark Lawrence.

bpo-18622 [https://bugs.python.org/issue18622]: unittest.mock.mock_open().reset_mock would recurse infinitely. Patch from Nicola Palumbo and Laurent De Buyst.

bpo-24426 [https://bugs.python.org/issue24426]: Fast searching optimization in regular expressions now works for patterns that starts with capturing groups. Fast searching optimization now can’t be disabled at compile time.

bpo-23661 [https://bugs.python.org/issue23661]: unittest.mock side_effects can now be exceptions again. This was a regression vs Python 3.4. Patch from Ignacio Rossi

bpo-13248 [https://bugs.python.org/issue13248]: Remove deprecated inspect.getmoduleinfo function.

bpo-25578 [https://bugs.python.org/issue25578]: Fix (another) memory leak in SSLSocket.getpeercer().

bpo-25530 [https://bugs.python.org/issue25530]: Disable the vulnerable SSLv3 protocol by default when creating ssl.SSLContext.

bpo-25569 [https://bugs.python.org/issue25569]: Fix memory leak in SSLSocket.getpeercert().

bpo-25471 [https://bugs.python.org/issue25471]: Sockets returned from accept() shouldn’t appear to be nonblocking.

bpo-25319 [https://bugs.python.org/issue25319]: When threading.Event is reinitialized, the underlying condition should use a regular lock rather than a recursive lock.

Skip getaddrinfo if host is already resolved. Patch by A. Jesse Jiryu Davis.

bpo-26050 [https://bugs.python.org/issue26050]: Add asyncio.StreamReader.readuntil() method. Patch by Марк Коренберг.

bpo-25924 [https://bugs.python.org/issue25924]: Avoid unnecessary serialization of getaddrinfo(3) calls on OS X versions 10.5 or higher. Original patch by A. Jesse Jiryu Davis.

bpo-26406 [https://bugs.python.org/issue26406]: Avoid unnecessary serialization of getaddrinfo(3) calls on current versions of OpenBSD and NetBSD. Patch by A. Jesse Jiryu Davis.

bpo-26848 [https://bugs.python.org/issue26848]: Fix asyncio/subprocess.communicate() to handle empty input. Patch by Jack O’Connor.

bpo-27040 [https://bugs.python.org/issue27040]: Add loop.get_exception_handler method

bpo-27041 [https://bugs.python.org/issue27041]: asyncio: Add loop.create_future method

IDLE

bpo-20640 [https://bugs.python.org/issue20640]: Add tests for idlelib.configHelpSourceEdit. Patch by Saimadhav Heblikar.

In the ‘IDLE-console differences’ section of the IDLE doc, clarify how running with IDLE affects sys.modules and the standard streams.

bpo-25507 [https://bugs.python.org/issue25507]: fix incorrect change in IOBinding that prevented printing. Augment IOBinding htest to include all major IOBinding functions.

bpo-25905 [https://bugs.python.org/issue25905]: Revert unwanted conversion of ‘ to ’ RIGHT SINGLE QUOTATION MARK in README.txt and open this and NEWS.txt with ‘ascii’. Re-encode CREDITS.txt to utf-8 and open it with ‘utf-8’.

bpo-15348 [https://bugs.python.org/issue15348]: Stop the debugger engine (normally in a user process) before closing the debugger window (running in the IDLE process). This prevents the RuntimeErrors that were being caught and ignored.

bpo-24455 [https://bugs.python.org/issue24455]: Prevent IDLE from hanging when a) closing the shell while the debugger is active (15347); b) closing the debugger with the [X] button (15348); and c) activating the debugger when already active (24455). The patch by Mark Roseman does this by making two changes. 1. Suspend and resume the gui.interaction method with the tcl vwait mechanism intended for this purpose (instead of root.mainloop & .quit). 2. In gui.run, allow any existing interaction to terminate first.

Change ‘The program’ to ‘Your program’ in an IDLE ‘kill program?’ message to make it clearer that the program referred to is the currently running user program, not IDLE itself.

bpo-24750 [https://bugs.python.org/issue24750]: Improve the appearance of the IDLE editor window status bar. Patch by Mark Roseman.

bpo-25313 [https://bugs.python.org/issue25313]: Change the handling of new built-in text color themes to better address the compatibility problem introduced by the addition of IDLE Dark. Consistently use the revised idleConf.CurrentTheme everywhere in idlelib.

bpo-24782 [https://bugs.python.org/issue24782]: Extension configuration is now a tab in the IDLE Preferences dialog rather than a separate dialog. The former tabs are now a sorted list. Patch by Mark Roseman.

bpo-22726 [https://bugs.python.org/issue22726]: Re-activate the config dialog help button with some content about the other buttons and the new IDLE Dark theme.

bpo-24820 [https://bugs.python.org/issue24820]: IDLE now has an ‘IDLE Dark’ built-in text color theme. It is more or less IDLE Classic inverted, with a cobalt blue background. Strings, comments, keywords, … are still green, red, orange, … . To use it with IDLEs released before November 2015, hit the ‘Save as New Custom Theme’ button and enter a new name, such as ‘Custom Dark’. The custom theme will work with any IDLE release, and can be modified.

bpo-25224 [https://bugs.python.org/issue25224]: README.txt is now an idlelib index for IDLE developers and curious users. The previous user content is now in the IDLE doc chapter. ‘IDLE’ now means ‘Integrated Development and Learning Environment’.

bpo-24820 [https://bugs.python.org/issue24820]: Users can now set breakpoint colors in Settings -> Custom Highlighting. Original patch by Mark Roseman.

bpo-24972 [https://bugs.python.org/issue24972]: Inactive selection background now matches active selection background, as configured by users, on all systems. Found items are now always highlighted on Windows. Initial patch by Mark Roseman.

bpo-24570 [https://bugs.python.org/issue24570]: Idle: make calltip and completion boxes appear on Macs affected by a tk regression. Initial patch by Mark Roseman.

bpo-24988 [https://bugs.python.org/issue24988]: Idle ScrolledList context menus (used in debugger) now work on Mac Aqua. Patch by Mark Roseman.

bpo-24801 [https://bugs.python.org/issue24801]: Make right-click for context menu work on Mac Aqua. Patch by Mark Roseman.

bpo-25173 [https://bugs.python.org/issue25173]: Associate tkinter messageboxes with a specific widget. For Mac OSX, make them a ‘sheet’. Patch by Mark Roseman.

bpo-25198 [https://bugs.python.org/issue25198]: Enhance the initial html viewer now used for Idle Help. Properly indent fixed-pitch text (patch by Mark Roseman). Give code snippet a very Sphinx-like light blueish-gray background. Re-use initial width and height set by users for shell and editor. When the Table of Contents (TOC) menu is used, put the section header at the top of the screen.

bpo-25225 [https://bugs.python.org/issue25225]: Condense and rewrite Idle doc section on text colors.

bpo-21995 [https://bugs.python.org/issue21995]: Explain some differences between IDLE and console Python.

bpo-22820 [https://bugs.python.org/issue22820]: Explain need for print when running file from Idle editor.

bpo-25224 [https://bugs.python.org/issue25224]: Doc: augment Idle feature list and no-subprocess section.

bpo-25219 [https://bugs.python.org/issue25219]: Update doc for Idle command line options. Some were missing and notes were not correct.

bpo-24861 [https://bugs.python.org/issue24861]: Most of idlelib is private and subject to change. Use idleib.idle.* to start Idle. See idlelib.__init__.__doc__.

bpo-25199 [https://bugs.python.org/issue25199]: Idle: add synchronization comments for future maintainers.

bpo-16893 [https://bugs.python.org/issue16893]: Replace help.txt with help.html for Idle doc display. The new idlelib/help.html is rstripped Doc/build/html/library/idle.html. It looks better than help.txt and will better document Idle as released. The tkinter html viewer that works for this file was written by Rose Roseman. The now unused EditorWindow.HelpDialog class and helt.txt file are deprecated.

bpo-24199 [https://bugs.python.org/issue24199]: Deprecate unused idlelib.idlever with possible removal in 3.6.

bpo-24790 [https://bugs.python.org/issue24790]: Remove extraneous code (which also create 2 & 3 conflicts).

文档

bpo-26736 [https://bugs.python.org/issue26736]: Used HTTPS for external links in the documentation if possible.

bpo-6953 [https://bugs.python.org/issue6953]: Rework the Readline module documentation to group related functions together, and add more details such as what underlying Readline functions and variables are accessed.

bpo-23606 [https://bugs.python.org/issue23606]: Adds note to ctypes documentation regarding cdll.msvcrt.

bpo-24952 [https://bugs.python.org/issue24952]: Clarify the default size argument of stack_size() in the “threading” and “_thread” modules. Patch from Mattip.

bpo-26014 [https://bugs.python.org/issue26014]: Update 3.x packaging documentation: * “See also” links to the new docs are now provided in the legacy pages * links to setuptools documentation have been updated

测试

bpo-21916 [https://bugs.python.org/issue21916]: Added tests for the turtle module. Patch by ingrid, Gregory Loyse and Jelle Zijlstra.

bpo-26295 [https://bugs.python.org/issue26295]: When using “python3 -m test –testdir=TESTDIR”, regrtest doesn’t add “test.” prefix to test module names.

bpo-26523 [https://bugs.python.org/issue26523]: The multiprocessing thread pool (multiprocessing.dummy.Pool) was untested.

bpo-26015 [https://bugs.python.org/issue26015]: Added new tests for pickling iterators of mutable sequences.

bpo-26325 [https://bugs.python.org/issue26325]: Added test.support.check_no_resource_warning() to check that no ResourceWarning is emitted.

bpo-25940 [https://bugs.python.org/issue25940]: Changed test_ssl to use its internal local server more. This avoids relying on svn.python.org, which recently changed root certificate.

bpo-25616 [https://bugs.python.org/issue25616]: Tests for OrderedDict are extracted from test_collections into separate file test_ordered_dict.

bpo-25449 [https://bugs.python.org/issue25449]: Added tests for OrderedDict subclasses.

bpo-25188 [https://bugs.python.org/issue25188]: Add -P/–pgo to test.regrtest to suppress error output when running the test suite for the purposes of a PGO build. Initial patch by Alecsandru Patrascu.

bpo-22806 [https://bugs.python.org/issue22806]: Add python -m test --list-tests command to list tests.

bpo-18174 [https://bugs.python.org/issue18174]: python -m test --huntrleaks ... now also checks for leak of file descriptors. Patch written by Richard Oudkerk.

bpo-25260 [https://bugs.python.org/issue25260]: Fix python -m test --coverage on Windows. Remove the list of ignored directories.

PCbuild\rt.bat now accepts an unlimited number of arguments to pass along to regrtest.py. Previously there was a limit of 9.

bpo-26583 [https://bugs.python.org/issue26583]: Skip test_timestamp_overflow in test_import if bytecode files cannot be written.

构建

bpo-21277 [https://bugs.python.org/issue21277]: Don’t try to link _ctypes with a ffi_convenience library.

bpo-26884 [https://bugs.python.org/issue26884]: Fix linking extension modules for cross builds. Patch by Xavier de Gaye.

bpo-26932 [https://bugs.python.org/issue26932]: Fixed support of RTLD_* constants defined as enum values, not via macros (in particular on Android). Patch by Chi Hsuan Yen.

bpo-22359 [https://bugs.python.org/issue22359]: Disable the rules for running _freeze_importlib and pgen when cross-compiling. The output of these programs is normally saved with the source code anyway, and is still regenerated when doing a native build. Patch by Xavier de Gaye.

bpo-21668 [https://bugs.python.org/issue21668]: Link audioop, _datetime, _ctypes_test modules to libm, except on Mac OS X. Patch written by Chi Hsuan Yen.

bpo-25702 [https://bugs.python.org/issue25702]: A –with-lto configure option has been added that will enable link time optimizations at build time during a make profile-opt. Some compilers and toolchains are known to not produce stable code when using LTO, be sure to test things thoroughly before relying on it. It can provide a few % speed up over profile-opt alone.

bpo-26624 [https://bugs.python.org/issue26624]: Adds validation of ucrtbase[d].dll version with warning for old versions.

bpo-17603 [https://bugs.python.org/issue17603]: Avoid error about nonexistant fileblocks.o file by using a lower-level check for st_blocks in struct stat.

bpo-26079 [https://bugs.python.org/issue26079]: Fixing the build output folder for tix-8.4.3.6. Patch by Bjoern Thiel.

bpo-26465 [https://bugs.python.org/issue26465]: 更新Windows 构建OpenSSL 1.0.2g.

bpo-25348 [https://bugs.python.org/issue25348]: Added --pgo and --pgo-job arguments to PCbuild\build.bat for building with Profile-Guided Optimization. The old PCbuild\build_pgo.bat script is removed.

bpo-25827 [https://bugs.python.org/issue25827]: Add support for building with ICC to configure, including a new --with-icc flag.

bpo-25696 [https://bugs.python.org/issue25696]: Fix installation of Python on UNIX with make -j9.

bpo-24986 [https://bugs.python.org/issue24986]: It is now possible to build Python on Windows without errors when external libraries are not available.

bpo-24421 [https://bugs.python.org/issue24421]: Compile Modules/_math.c once, before building extensions. Previously it could fail to compile properly if the math and cmath builds were concurrent.

bpo-26465 [https://bugs.python.org/issue26465]: 更新 OS X 10.5+（仅32位）安装程序以构建并链接 OpenSSL 1.0.2g.

bpo-26268 [https://bugs.python.org/issue26268]: 更新Windows 构建OpenSSL 1.0.2f.

bpo-25136 [https://bugs.python.org/issue25136]: Support Apple Xcode 7’s new textual SDK stub libraries.

bpo-24324 [https://bugs.python.org/issue24324]: Do not enable unreachable code warnings when using gcc as the option does not work correctly in older versions of gcc and has been silently removed as of gcc-4.5.

Windows

bpo-27053 [https://bugs.python.org/issue27053]: Updates make_zip.py to correctly generate library ZIP file.

bpo-26268 [https://bugs.python.org/issue26268]: Update the prepare_ssl.py script to handle OpenSSL releases that don’t include the contents of the include directory (that is, 1.0.2e and later).

bpo-26071 [https://bugs.python.org/issue26071]: bdist_wininst created binaries fail to start and find 32bit Python

bpo-26073 [https://bugs.python.org/issue26073]: Update the list of magic numbers in launcher

bpo-26065 [https://bugs.python.org/issue26065]: Excludes venv from library when generating embeddable distro.

bpo-25022 [https://bugs.python.org/issue25022]: Removed very outdated PC/example_nt/ directory.

工具/示例

bpo-26799 [https://bugs.python.org/issue26799]: Fix python-gdb.py: don’t get C types once when the Python code is loaded, but get C types on demand. The C types can change if python-gdb.py is loaded before the Python executable. Patch written by Thomas Ilsche.

bpo-26271 [https://bugs.python.org/issue26271]: Fix the Freeze tool to properly use flags passed through configure. Patch by Daniel Shaulov.

bpo-26489 [https://bugs.python.org/issue26489]: Add dictionary unpacking support to Tools/parser/unparse.py. Patch by Guo Ci Teo.

bpo-26316 [https://bugs.python.org/issue26316]: Fix variable name typo in Argument Clinic.

bpo-25440 [https://bugs.python.org/issue25440]: Fix output of python-config –extension-suffix.

bpo-25154 [https://bugs.python.org/issue25154]: The pyvenv script has been deprecated in favour of python3 -m venv.

C API

bpo-26312 [https://bugs.python.org/issue26312]: SystemError is now raised in all programming bugs with using PyArg_ParseTupleAndKeywords(). RuntimeError did raised before in some programming bugs.

bpo-26198 [https://bugs.python.org/issue26198]: ValueError is now raised instead of TypeError on buffer overflow in parsing “es#” and “et#” format units. SystemError is now raised instead of TypeError on programmatical error in parsing format string.

Python 3.5.3 正式版

发布日期: 2017-01-17

There were no code changes between 3.5.3rc1 and 3.5.3 final.

Python 3.5.3 rc1

发布日期: 2017-01-02

核心与内置

bpo-29073 [https://bugs.python.org/issue29073]: bytearray formatting no longer truncates on first null byte.

bpo-28932 [https://bugs.python.org/issue28932]: Do not include <sys/random.h> if it does not exist.

bpo-28147 [https://bugs.python.org/issue28147]: Fix a memory leak in split-table dictionaries: setattr() must not convert combined table into split table.

bpo-25677 [https://bugs.python.org/issue25677]: Correct the positioning of the syntax error caret for indented blocks. Based on patch by Michael Layzell.

bpo-29000 [https://bugs.python.org/issue29000]: Fixed bytes formatting of octals with zero padding in alternate form.

bpo-28512 [https://bugs.python.org/issue28512]: Fixed setting the offset attribute of SyntaxError by PyErr_SyntaxLocationEx() and PyErr_SyntaxLocationObject().

bpo-28991 [https://bugs.python.org/issue28991]: functools.lru_cache() was susceptible to an obscure reentrancy bug caused by a monkey-patched len() function.

bpo-28648 [https://bugs.python.org/issue28648]: Fixed crash in Py_DecodeLocale() in debug build on Mac OS X when decode astral characters. Patch by Xiang Zhang.

bpo-19398 [https://bugs.python.org/issue19398]: Extra slash no longer added to sys.path components in case of empty compile-time PYTHONPATH components.

bpo-28426 [https://bugs.python.org/issue28426]: Fixed potential crash in PyUnicode_AsDecodedObject() in debug build.

bpo-23782 [https://bugs.python.org/issue23782]: Fixed possible memory leak in _PyTraceback_Add() and exception loss in PyTraceBack_Here().

bpo-28379 [https://bugs.python.org/issue28379]: Added sanity checks and tests for PyUnicode_CopyCharacters(). Patch by Xiang Zhang.

bpo-28376 [https://bugs.python.org/issue28376]: The type of long range iterator is now registered as Iterator. Patch by Oren Milman.

bpo-28376 [https://bugs.python.org/issue28376]: The constructor of range_iterator now checks that step is not 0. Patch by Oren Milman.

bpo-26906 [https://bugs.python.org/issue26906]: Resolving special methods of uninitialized type now causes implicit initialization of the type instead of a fail.

bpo-18287 [https://bugs.python.org/issue18287]: PyType_Ready() now checks that tp_name is not NULL. Original patch by Niklas Koep.

bpo-24098 [https://bugs.python.org/issue24098]: Fixed possible crash when AST is changed in process of compiling it.

bpo-28350 [https://bugs.python.org/issue28350]: String constants with null character no longer interned.

bpo-26617 [https://bugs.python.org/issue26617]: Fix crash when GC runs during weakref callbacks.

bpo-27942 [https://bugs.python.org/issue27942]: String constants now interned recursively in tuples and frozensets.

bpo-21578 [https://bugs.python.org/issue21578]: Fixed misleading error message when ImportError called with invalid keyword args.

bpo-28203 [https://bugs.python.org/issue28203]: Fix incorrect type in error message from complex(1.0, {2:3}). Patch by Soumya Sharma.

bpo-27955 [https://bugs.python.org/issue27955]: Fallback on reading /dev/urandom device when the getrandom() syscall fails with EPERM, for example when blocked by SECCOMP.

bpo-28131 [https://bugs.python.org/issue28131]: Fix a regression in zipimport’s compile_source(). zipimport should use the same optimization level as the interpreter.

bpo-25221 [https://bugs.python.org/issue25221]: Fix corrupted result from PyLong_FromLong(0) when Python is compiled with NSMALLPOSINTS = 0.

bpo-25758 [https://bugs.python.org/issue25758]: Prevents zipimport from unnecessarily encoding a filename (patch by Eryk Sun)

bpo-28189 [https://bugs.python.org/issue28189]: dictitems_contains no longer swallows compare errors. (Patch by Xiang Zhang)

bpo-27812 [https://bugs.python.org/issue27812]: Properly clear out a generator’s frame’s backreference to the generator to prevent crashes in frame.clear().

bpo-27811 [https://bugs.python.org/issue27811]: Fix a crash when a coroutine that has not been awaited is finalized with warnings-as-errors enabled.

bpo-27587 [https://bugs.python.org/issue27587]: Fix another issue found by PVS-Studio: Null pointer check after use of ‘def’ in _PyState_AddModule(). Initial patch by Christian Heimes.

bpo-26020 [https://bugs.python.org/issue26020]: set literal evaluation order did not match documented behaviour.

bpo-27782 [https://bugs.python.org/issue27782]: Multi-phase extension module import now correctly allows the m_methods field to be used to add module level functions to instances of non-module types returned from Py_create_mod. Patch by Xiang Zhang.

bpo-27936 [https://bugs.python.org/issue27936]: The round() function accepted a second None argument for some types but not for others. Fixed the inconsistency by accepting None for all numeric types.

bpo-27487 [https://bugs.python.org/issue27487]: Warn if a submodule argument to “python -m” or runpy.run_module() is found in sys.modules after parent packages are imported, but before the submodule is executed.

bpo-27558 [https://bugs.python.org/issue27558]: Fix a SystemError in the implementation of “raise” statement. In a brand new thread, raise a RuntimeError since there is no active exception to reraise. Patch written by Xiang Zhang.

bpo-27419 [https://bugs.python.org/issue27419]: Standard __import__() no longer look up “__import__” in globals or builtins for importing submodules or “from import”. Fixed handling an error of non-string package name.

bpo-27083 [https://bugs.python.org/issue27083]: Respect the PYTHONCASEOK environment variable under Windows.

bpo-27514 [https://bugs.python.org/issue27514]: Make having too many statically nested blocks a SyntaxError instead of SystemError.

bpo-27473 [https://bugs.python.org/issue27473]: Fixed possible integer overflow in bytes and bytearray concatenations. Patch by Xiang Zhang.

bpo-27507 [https://bugs.python.org/issue27507]: Add integer overflow check in bytearray.extend(). Patch by Xiang Zhang.

bpo-27581 [https://bugs.python.org/issue27581]: Don’t rely on wrapping for overflow check in PySequence_Tuple(). Patch by Xiang Zhang.

bpo-27443 [https://bugs.python.org/issue27443]: __length_hint__() of bytearray iterators no longer return a negative integer for a resized bytearray.

bpo-27942 [https://bugs.python.org/issue27942]: Fix memory leak in codeobject.c

库

bpo-15812 [https://bugs.python.org/issue15812]: inspect.getframeinfo() now correctly shows the first line of a context. Patch by Sam Breese.

bpo-29094 [https://bugs.python.org/issue29094]: Offsets in a ZIP file created with extern file object and modes “w” and “x” now are relative to the start of the file.

bpo-13051 [https://bugs.python.org/issue13051]: Fixed recursion errors in large or resized curses.textpad.Textbox. Based on patch by Tycho Andersen.

bpo-29119 [https://bugs.python.org/issue29119]: Fix weakrefs in the pure python version of collections.OrderedDict move_to_end() method. Contributed by Andra Bogildea.

bpo-9770 [https://bugs.python.org/issue9770]: curses.ascii predicates now work correctly with negative integers.

bpo-28427 [https://bugs.python.org/issue28427]: old keys should not remove new values from WeakValueDictionary when collecting from another thread.

bpo-28923 [https://bugs.python.org/issue28923]: Remove editor artifacts from Tix.py.

bpo-28871 [https://bugs.python.org/issue28871]: Fixed a crash when deallocate deep ElementTree.

bpo-19542 [https://bugs.python.org/issue19542]: Fix bugs in WeakValueDictionary.setdefault() and WeakValueDictionary.pop() when a GC collection happens in another thread.

bpo-20191 [https://bugs.python.org/issue20191]: Fixed a crash in resource.prlimit() when pass a sequence that doesn’t own its elements as limits.

bpo-28779 [https://bugs.python.org/issue28779]: multiprocessing.set_forkserver_preload() would crash the forkserver process if a preloaded module instantiated some multiprocessing objects such as locks.

bpo-28847 [https://bugs.python.org/issue28847]: dbm.dumb now supports reading read-only files and no longer writes the index file when it is not changed.

bpo-25659 [https://bugs.python.org/issue25659]: In ctypes, prevent a crash calling the from_buffer() and from_buffer_copy() methods on abstract classes like Array.

bpo-28732 [https://bugs.python.org/issue28732]: Fix crash in os.spawnv() with no elements in args

bpo-28485 [https://bugs.python.org/issue28485]: Always raise ValueError for negative compileall.compile_dir(workers=…) parameter, even when multithreading is unavailable.

bpo-28387 [https://bugs.python.org/issue28387]: Fixed possible crash in _io.TextIOWrapper deallocator when the garbage collector is invoked in other thread. Based on patch by Sebastian Cufre.

bpo-27517 [https://bugs.python.org/issue27517]: LZMA compressor and decompressor no longer raise exceptions if given empty data twice. Patch by Benjamin Fogle.

bpo-28549 [https://bugs.python.org/issue28549]: Fixed segfault in curses’s addch() with ncurses6.

bpo-28449 [https://bugs.python.org/issue28449]: tarfile.open() with mode “r” or “r:” now tries to open a tar file with compression before trying to open it without compression. Otherwise it had 50% chance failed with ignore_zeros=True.

bpo-23262 [https://bugs.python.org/issue23262]: The webbrowser module now supports Firefox 36+ and derived browsers. Based on patch by Oleg Broytman.

bpo-27939 [https://bugs.python.org/issue27939]: Fixed bugs in tkinter.ttk.LabeledScale and tkinter.Scale caused by representing the scale as float value internally in Tk. tkinter.IntVar now works if float value is set to underlying Tk variable.

bpo-28255 [https://bugs.python.org/issue28255]: calendar.TextCalendar().prmonth() no longer prints a space at the start of new line after printing a month’s calendar. Patch by Xiang Zhang.

bpo-20491 [https://bugs.python.org/issue20491]: The textwrap.TextWrapper class now honors non-breaking spaces. Based on patch by Kaarle Ritvanen.

bpo-28353 [https://bugs.python.org/issue28353]: os.fwalk() no longer fails on broken links.

bpo-25464 [https://bugs.python.org/issue25464]: Fixed HList.header_exists() in tkinter.tix module by addin a workaround to Tix library bug.

bpo-28488 [https://bugs.python.org/issue28488]: shutil.make_archive() no longer add entry “./” to ZIP archive.

bpo-24452 [https://bugs.python.org/issue24452]: Make webbrowser support Chrome on Mac OS X.

bpo-20766 [https://bugs.python.org/issue20766]: Fix references leaked by pdb in the handling of SIGINT handlers.

bpo-26293 [https://bugs.python.org/issue26293]: Fixed writing ZIP files that starts not from the start of the file. Offsets in ZIP file now are relative to the start of the archive in conforming to the specification.

bpo-28321 [https://bugs.python.org/issue28321]: Fixed writing non-BMP characters with binary format in plistlib.

bpo-28322 [https://bugs.python.org/issue28322]: Fixed possible crashes when unpickle itertools objects from incorrect pickle data. Based on patch by John Leitch.

Fix possible integer overflows and crashes in the mmap module with unusual usage patterns.

bpo-1703178 [https://bugs.python.org/issue1703178]: Fix the ability to pass the –link-objects option to the distutils build_ext command.

bpo-28253 [https://bugs.python.org/issue28253]: Fixed calendar functions for extreme months: 0001-01 and 9999-12.

Methods itermonthdays() and itermonthdays2() are reimplemented so that they don’t call itermonthdates() which can cause datetime.date under/overflow.

bpo-28275 [https://bugs.python.org/issue28275]: Fixed possible use after free in the decompress() methods of the LZMADecompressor and BZ2Decompressor classes. Original patch by John Leitch.

bpo-27897 [https://bugs.python.org/issue27897]: Fixed possible crash in sqlite3.Connection.create_collation() if pass invalid string-like object as a name. Patch by Xiang Zhang.

bpo-18893 [https://bugs.python.org/issue18893]: Fix invalid exception handling in Lib/ctypes/macholib/dyld.py. Patch by Madison May.

bpo-27611 [https://bugs.python.org/issue27611]: Fixed support of default root window in the tkinter.tix module.

bpo-27348 [https://bugs.python.org/issue27348]: In the traceback module, restore the formatting of exception messages like “Exception: None”. This fixes a regression introduced in 3.5a2.

bpo-25651 [https://bugs.python.org/issue25651]: Allow falsy values to be used for msg parameter of subTest().

bpo-27932 [https://bugs.python.org/issue27932]: Prevent memory leak in win32_ver().

Fix UnboundLocalError in socket._sendfile_use_sendfile.

bpo-28075 [https://bugs.python.org/issue28075]: Check for ERROR_ACCESS_DENIED in Windows implementation of os.stat(). Patch by Eryk Sun.

bpo-25270 [https://bugs.python.org/issue25270]: Prevent codecs.escape_encode() from raising SystemError when an empty bytestring is passed.

bpo-28181 [https://bugs.python.org/issue28181]: Get antigravity over HTTPS. Patch by Kaartic Sivaraam.

bpo-25895 [https://bugs.python.org/issue25895]: Enable WebSocket URL schemes in urllib.parse.urljoin. Patch by Gergely Imreh and Markus Holtermann.

bpo-27599 [https://bugs.python.org/issue27599]: Fixed buffer overrun in binascii.b2a_qp() and binascii.a2b_qp().

bpo-19003 [https://bugs.python.org/issue19003]: m email.generator now replaces only \r and/or \n line endings, per the RFC, instead of all unicode line endings.

bpo-28019 [https://bugs.python.org/issue28019]: itertools.count() no longer rounds non-integer step in range between 1.0 and 2.0 to 1.

bpo-25969 [https://bugs.python.org/issue25969]: Update the lib2to3 grammar to handle the unpacking generalizations added in 3.5.

bpo-14977 [https://bugs.python.org/issue14977]: mailcap now respects the order of the lines in the mailcap files (“first match”), as required by RFC 1542. Patch by Michael Lazar.

bpo-24594 [https://bugs.python.org/issue24594]: Validates persist parameter when opening MSI database

bpo-17582 [https://bugs.python.org/issue17582]: xml.etree.ElementTree nows preserves whitespaces in attributes (Patch by Duane Griffin. Reviewed and approved by Stefan Behnel.)

bpo-28047 [https://bugs.python.org/issue28047]: Fixed calculation of line length used for the base64 CTE in the new email policies.

bpo-27445 [https://bugs.python.org/issue27445]: Don’t pass str(_charset) to MIMEText.set_payload(). Patch by Claude Paroz.

bpo-22450 [https://bugs.python.org/issue22450]: urllib now includes an Accept: */* header among the default headers. This makes the results of REST API requests more consistent and predictable especially when proxy servers are involved.

lib2to3.pgen3.driver.load_grammar() now creates a stable cache file between runs given the same Grammar.txt input regardless of the hash randomization setting.

bpo-27570 [https://bugs.python.org/issue27570]: Avoid zero-length memcpy() etc calls with null source pointers in the “ctypes” and “array” modules.

bpo-22233 [https://bugs.python.org/issue22233]: Break email header lines only on the RFC specified CR and LF characters, not on arbitrary unicode line breaks. This also fixes a bug in HTTP header parsing.

bpo-27988 [https://bugs.python.org/issue27988]: Fix email iter_attachments incorrect mutation of payload list.

bpo-27691 [https://bugs.python.org/issue27691]: Fix ssl module’s parsing of GEN_RID subject alternative name fields in X.509 certs.

bpo-27850 [https://bugs.python.org/issue27850]: Remove 3DES from ssl module’s default cipher list to counter measure sweet32 attack (CVE-2016-2183).

bpo-27766 [https://bugs.python.org/issue27766]: Add ChaCha20 Poly1305 to ssl module’s default cipher list. (Required OpenSSL 1.1.0 or LibreSSL).

bpo-26470 [https://bugs.python.org/issue26470]: Port ssl and hashlib module to OpenSSL 1.1.0.

Remove support for passing a file descriptor to os.access. It never worked but previously didn’t raise.

bpo-12885 [https://bugs.python.org/issue12885]: Fix error when distutils encounters symlink.

bpo-27881 [https://bugs.python.org/issue27881]: Fixed possible bugs when setting sqlite3.Connection.isolation_level. Based on patch by Xiang Zhang.

bpo-27861 [https://bugs.python.org/issue27861]: Fixed a crash in sqlite3.Connection.cursor() when a factory creates not a cursor. Patch by Xiang Zhang.

bpo-19884 [https://bugs.python.org/issue19884]: Avoid spurious output on OS X with Gnu Readline.

bpo-27706 [https://bugs.python.org/issue27706]: Restore deterministic behavior of random.Random().seed() for string seeds using seeding version 1. Allows sequences of calls to random() to exactly match those obtained in Python 2. Patch by Nofar Schnider.

bpo-10513 [https://bugs.python.org/issue10513]: Fix a regression in Connection.commit(). Statements should not be reset after a commit.

A new version of typing.py from https://github.com/python/typing: Collection (only for 3.6) (bpo-27598 [https://bugs.python.org/issue27598]). Add FrozenSet to __all__ (upstream #261). Fix crash in _get_type_vars() (upstream #259). Remove the dict constraint in ForwardRef._eval_type (upstream #252).

bpo-27539 [https://bugs.python.org/issue27539]: Fix unnormalised Fraction.__pow__ result in the case of negative exponent and negative base.

bpo-21718 [https://bugs.python.org/issue21718]: cursor.description is now available for queries using CTEs.

bpo-2466 [https://bugs.python.org/issue2466]: posixpath.ismount now correctly recognizes mount points which the user does not have permission to access.

bpo-27773 [https://bugs.python.org/issue27773]: Correct some memory management errors server_hostname in _ssl.wrap_socket().

bpo-26750 [https://bugs.python.org/issue26750]: unittest.mock.create_autospec() now works properly for subclasses of property() and other data descriptors.

In the curses module, raise an error if window.getstr() or window.instr() is passed a negative value.

bpo-27783 [https://bugs.python.org/issue27783]: Fix possible usage of uninitialized memory in operator.methodcaller.

bpo-27774 [https://bugs.python.org/issue27774]: Fix possible Py_DECREF on unowned object in _sre.

bpo-27760 [https://bugs.python.org/issue27760]: Fix possible integer overflow in binascii.b2a_qp.

bpo-27758 [https://bugs.python.org/issue27758]: Fix possible integer overflow in the _csv module for large record lengths.

bpo-27568 [https://bugs.python.org/issue27568]: Prevent HTTPoxy attack (CVE-2016-1000110). Ignore the HTTP_PROXY variable when REQUEST_METHOD environment is set, which indicates that the script is in CGI mode.

bpo-27656 [https://bugs.python.org/issue27656]: Do not assume sched.h defines any SCHED_* constants.

bpo-27130 [https://bugs.python.org/issue27130]: In the “zlib” module, fix handling of large buffers (typically 4 GiB) when compressing and decompressing. Previously, inputs were limited to 4 GiB, and compression and decompression operations did not properly handle results of 4 GiB.

bpo-27533 [https://bugs.python.org/issue27533]: Release GIL in nt._isdir

bpo-17711 [https://bugs.python.org/issue17711]: Fixed unpickling by the persistent ID with protocol 0. Original patch by Alexandre Vassalotti.

bpo-27522 [https://bugs.python.org/issue27522]: Avoid an unintentional reference cycle in email.feedparser.

bpo-26844 [https://bugs.python.org/issue26844]: Fix error message for imp.find_module() to refer to ‘path’ instead of ‘name’. Patch by Lev Maximov.

bpo-23804 [https://bugs.python.org/issue23804]: Fix SSL zero-length recv() calls to not block and not raise an error about unclean EOF.

bpo-27466 [https://bugs.python.org/issue27466]: Change time format returned by http.cookie.time2netscape, confirming the netscape cookie format and making it consistent with documentation.

bpo-26664 [https://bugs.python.org/issue26664]: Fix activate.fish by removing mis-use of $.

bpo-22115 [https://bugs.python.org/issue22115]: Fixed tracing Tkinter variables: trace_vdelete() with wrong mode no longer break tracing, trace_vinfo() now always returns a list of pairs of strings, tracing in the “u” mode now works.

Fix a scoping issue in importlib.util.LazyLoader which triggered an UnboundLocalError when lazy-loading a module that was already put into sys.modules.

bpo-27079 [https://bugs.python.org/issue27079]: Fixed curses.ascii functions isblank(), iscntrl() and ispunct().

bpo-26754 [https://bugs.python.org/issue26754]: Some functions (compile() etc) accepted a filename argument encoded as an iterable of integers. Now only strings and byte-like objects are accepted.

bpo-27048 [https://bugs.python.org/issue27048]: Prevents distutils failing on Windows when environment variables contain non-ASCII characters

bpo-27330 [https://bugs.python.org/issue27330]: Fixed possible leaks in the ctypes module.

bpo-27238 [https://bugs.python.org/issue27238]: Got rid of bare excepts in the turtle module. Original patch by Jelle Zijlstra.

bpo-27122 [https://bugs.python.org/issue27122]: When an exception is raised within the context being managed by a contextlib.ExitStack() and one of the exit stack generators catches and raises it in a chain, do not re-raise the original exception when exiting, let the new chained one through. This avoids the PEP 479 bug described in issue25782.

安全

bpo-27278 [https://bugs.python.org/issue27278]: Fix os.urandom() implementation using getrandom() on Linux. Truncate size to INT_MAX and loop until we collected enough random bytes, instead of casting a directly Py_ssize_t to int.

库

bpo-26386 [https://bugs.python.org/issue26386]: Fixed ttk.TreeView selection operations with item id’s containing spaces.

安全

bpo-22636 [https://bugs.python.org/issue22636]: Avoid shell injection problems with ctypes.util.find_library().

库

bpo-16182 [https://bugs.python.org/issue16182]: Fix various functions in the “readline” module to use the locale encoding, and fix get_begidx() and get_endidx() to return code point indexes.

bpo-27392 [https://bugs.python.org/issue27392]: Add loop.connect_accepted_socket(). Patch by Jim Fulton.

bpo-27930 [https://bugs.python.org/issue27930]: Improved behaviour of logging.handlers.QueueListener. Thanks to Paulo Andrade and Petr Viktorin for the analysis and patch.

bpo-21201 [https://bugs.python.org/issue21201]: Improves readability of multiprocessing error message. Thanks to Wojciech Walczak for patch.

bpo-27456 [https://bugs.python.org/issue27456]: asyncio: Set TCP_NODELAY by default.

bpo-27906 [https://bugs.python.org/issue27906]: Fix socket accept exhaustion during high TCP traffic. Patch by Kevin Conway.

bpo-28174 [https://bugs.python.org/issue28174]: Handle when SO_REUSEPORT isn’t properly supported. Patch by Seth Michael Larson.

bpo-26654 [https://bugs.python.org/issue26654]: Inspect functools.partial in asyncio.Handle.__repr__. Patch by iceboy.

bpo-26909 [https://bugs.python.org/issue26909]: Fix slow pipes IO in asyncio. Patch by INADA Naoki.

bpo-28176 [https://bugs.python.org/issue28176]: Fix callbacks race in asyncio.SelectorLoop.sock_connect.

bpo-27759 [https://bugs.python.org/issue27759]: Fix selectors incorrectly retain invalid file descriptors. Patch by Mark Williams.

bpo-28368 [https://bugs.python.org/issue28368]: Refuse monitoring processes if the child watcher has no loop attached. Patch by Vincent Michel.

bpo-28369 [https://bugs.python.org/issue28369]: Raise RuntimeError when transport’s FD is used with add_reader, add_writer, etc.

bpo-28370 [https://bugs.python.org/issue28370]: Speedup asyncio.StreamReader.readexactly. Patch by Коренберг Марк.

bpo-28371 [https://bugs.python.org/issue28371]: Deprecate passing asyncio.Handles to run_in_executor.

bpo-28372 [https://bugs.python.org/issue28372]: Fix asyncio to support formatting of non-python coroutines.

bpo-28399 [https://bugs.python.org/issue28399]: Remove UNIX socket from FS before binding. Patch by Коренберг Марк.

bpo-27972 [https://bugs.python.org/issue27972]: Prohibit Tasks to await on themselves.

bpo-26923 [https://bugs.python.org/issue26923]: Fix asyncio.Gather to refuse being cancelled once all children are done. Patch by Johannes Ebke.

bpo-26796 [https://bugs.python.org/issue26796]: Don’t configure the number of workers for default threadpool executor. Initial patch by Hans Lawrenz.

bpo-28600 [https://bugs.python.org/issue28600]: Optimize loop.call_soon().

bpo-28613 [https://bugs.python.org/issue28613]: Fix get_event_loop() return the current loop if called from coroutines/callbacks.

bpo-28639 [https://bugs.python.org/issue28639]: Fix inspect.isawaitable to always return bool Patch by Justin Mayfield.

bpo-28652 [https://bugs.python.org/issue28652]: Make loop methods reject socket kinds they do not support.

bpo-28653 [https://bugs.python.org/issue28653]: Fix a refleak in functools.lru_cache.

bpo-28703 [https://bugs.python.org/issue28703]: Fix asyncio.iscoroutinefunction to handle Mock objects.

bpo-24142 [https://bugs.python.org/issue24142]: Reading a corrupt config file left the parser in an invalid state. Original patch by Florian Höch.

bpo-28990 [https://bugs.python.org/issue28990]: Fix SSL hanging if connection is closed before handshake completed. (Patch by HoHo-Ho)

IDLE

bpo-15308 [https://bugs.python.org/issue15308]: Add ‘interrupt execution’ (^C) to Shell menu. Patch by Roger Serwy, updated by Bayard Randel.

bpo-27922 [https://bugs.python.org/issue27922]: Stop IDLE tests from ‘flashing’ gui widgets on the screen.

在 IDLE 帮助窗口的标题中加入版本号

bpo-25564 [https://bugs.python.org/issue25564]: In section on IDLE – console differences, mention that using exec means that __builtins__ is defined for each statement.

bpo-27714 [https://bugs.python.org/issue27714]: text_textview and test_autocomplete now pass when re-run in the same process. This occurs when test_idle fails when run with the -w option but without -jn. Fix warning from test_config.

bpo-25507 [https://bugs.python.org/issue25507]: IDLE no longer runs buggy code because of its tkinter imports. Users must include the same imports required to run directly in Python.

bpo-27452 [https://bugs.python.org/issue27452]: add line counter and crc to IDLE configHandler test dump.

bpo-27365 [https://bugs.python.org/issue27365]: Allow non-ascii chars in IDLE NEWS.txt, for contributor names.

bpo-27245 [https://bugs.python.org/issue27245]: IDLE: Cleanly delete custom themes and key bindings. Previously, when IDLE was started from a console or by import, a cascade of warnings was emitted. Patch by Serhiy Storchaka.

C API

bpo-28808 [https://bugs.python.org/issue28808]: PyUnicode_CompareWithASCIIString() now never raises exceptions.

bpo-26754 [https://bugs.python.org/issue26754]: PyUnicode_FSDecoder() accepted a filename argument encoded as an iterable of integers. Now only strings and bytes-like objects are accepted.

文档

bpo-28513 [https://bugs.python.org/issue28513]: Documented command-line interface of zipfile.

测试

bpo-28950 [https://bugs.python.org/issue28950]: Disallow -j0 to be combined with -T/-l/-M in regrtest command line arguments.

bpo-28666 [https://bugs.python.org/issue28666]: Now test.support.rmtree is able to remove unwritable or unreadable directories.

bpo-23839 [https://bugs.python.org/issue23839]: Various caches now are cleared before running every test file.

bpo-28409 [https://bugs.python.org/issue28409]: regrtest: fix the parser of command line arguments.

bpo-27787 [https://bugs.python.org/issue27787]: Call gc.collect() before checking each test for “dangling threads”, since the dangling threads are weak references.

bpo-27369 [https://bugs.python.org/issue27369]: In test_pyexpat, avoid testing an error message detail that changed in Expat 2.2.0.

工具/示例

bpo-27952 [https://bugs.python.org/issue27952]: Get Tools/scripts/fixcid.py working with Python 3 and the current “re” module, avoid invalid Python backslash escapes, and fix a bug parsing escaped C quote signs.

bpo-27332 [https://bugs.python.org/issue27332]: Fixed the type of the first argument of module-level functions generated by Argument Clinic. Patch by Petr Viktorin.

bpo-27418 [https://bugs.python.org/issue27418]: Fixed Tools/importbench/importbench.py.

Windows

bpo-28251 [https://bugs.python.org/issue28251]: Improvements to help manuals on Windows.

bpo-28110 [https://bugs.python.org/issue28110]: launcher.msi has different product codes between 32-bit and 64-bit

bpo-25144 [https://bugs.python.org/issue25144]: Ensures TargetDir is set before continuing with custom install.

bpo-27469 [https://bugs.python.org/issue27469]: Adds a shell extension to the launcher so that drag and drop works correctly.

bpo-27309 [https://bugs.python.org/issue27309]: Enabled proper Windows styles in python[w].exe manifest.

构建

bpo-29080 [https://bugs.python.org/issue29080]: Removes hard dependency on hg.exe from PCBuild/build.bat

bpo-23903 [https://bugs.python.org/issue23903]: Added missed names to PC/python3.def.

bpo-10656 [https://bugs.python.org/issue10656]: Fix out-of-tree building on AIX. Patch by Tristan Carel and Michael Haubenwallner.

bpo-26359 [https://bugs.python.org/issue26359]: Rename –with-optimiations to –enable-optimizations.

bpo-28444 [https://bugs.python.org/issue28444]: Fix missing extensions modules when cross compiling.

bpo-28248 [https://bugs.python.org/issue28248]: 更新Windows 构建和OS X安装程序以使用OpenSSL 1.0.2j.

bpo-28258 [https://bugs.python.org/issue28258]: Fixed build with Estonian locale (python-config and distclean targets in Makefile). Patch by Arfrever Frehtes Taifersar Arahesis.

bpo-26661 [https://bugs.python.org/issue26661]: setup.py now detects system libffi with multiarch wrapper.

bpo-28066 [https://bugs.python.org/issue28066]: Fix the logic that searches build directories for generated include files when building outside the source tree.

bpo-15819 [https://bugs.python.org/issue15819]: Remove redundant include search directory option for building outside the source tree.

bpo-27566 [https://bugs.python.org/issue27566]: Fix clean target in freeze makefile (patch by Lisa Roach)

bpo-27705 [https://bugs.python.org/issue27705]: Update message in validate_ucrtbase.py

bpo-27983 [https://bugs.python.org/issue27983]: Cause lack of llvm-profdata tool when using clang as required for PGO linking to be a configure time error rather than make time when --with-optimizations is enabled. Also improve our ability to find the llvm-profdata tool on MacOS and some Linuxes.

bpo-26307 [https://bugs.python.org/issue26307]: The profile-opt build now applies PGO to the built-in modules.

bpo-26359 [https://bugs.python.org/issue26359]: Add the –with-optimizations configure flag.

bpo-27713 [https://bugs.python.org/issue27713]: Suppress spurious build warnings when updating importlib’s bootstrap files. Patch by Xiang Zhang

bpo-25825 [https://bugs.python.org/issue25825]: Correct the references to Modules/python.exp and ld_so_aix, which are required on AIX. This updates references to an installation path that was changed in 3.2a4, and undoes changed references to the build tree that were made in 3.5.0a1.

bpo-27453 [https://bugs.python.org/issue27453]: CPP invocation in configure must use CPPFLAGS. Patch by Chi Hsuan Yen.

bpo-27641 [https://bugs.python.org/issue27641]: The configure script now inserts comments into the makefile to prevent the pgen and _freeze_importlib executables from being cross-compiled.

bpo-26662 [https://bugs.python.org/issue26662]: Set PYTHON_FOR_GEN in configure as the Python program to be used for file generation during the build.

bpo-10910 [https://bugs.python.org/issue10910]: Avoid C++ compilation errors on FreeBSD and OS X. Also update FreedBSD version checks for the original ctype UTF-8 workaround.

bpo-28676 [https://bugs.python.org/issue28676]: Prevent missing ‘getentropy’ declaration warning on macOS. Patch by Gareth Rees.

Python 3.5.2 正式版

发布日期: 2016-06-26

核心与内置

bpo-26930 [https://bugs.python.org/issue26930]: 更新Windows 构建以使用OpenSSL OpenSSL 1.0.2h.

测试

bpo-26867 [https://bugs.python.org/issue26867]: Ubuntu’s openssl OP_NO_SSLv3 is forced on by default; fix test.

IDLE

bpo-27365 [https://bugs.python.org/issue27365]: Allow non-ascii in idlelib/NEWS.txt - minimal part for 3.5.2.

Python 3.5.2 rc1

发布日期: 2016-06-12

核心与内置

bpo-27066 [https://bugs.python.org/issue27066]: Fixed SystemError if a custom opener (for open()) returns a negative number without setting an exception.

bpo-20041 [https://bugs.python.org/issue20041]: Fixed TypeError when frame.f_trace is set to None. Patch by Xavier de Gaye.

bpo-26168 [https://bugs.python.org/issue26168]: Fixed possible refleaks in failing Py_BuildValue() with the “N” format unit.

bpo-26991 [https://bugs.python.org/issue26991]: Fix possible refleak when creating a function with annotations.

bpo-27039 [https://bugs.python.org/issue27039]: Fixed bytearray.remove() for values greater than 127. Patch by Joe Jevnik.

bpo-23640 [https://bugs.python.org/issue23640]: int.from_bytes() no longer bypasses constructors for subclasses.

bpo-26811 [https://bugs.python.org/issue26811]: gc.get_objects() no longer contains a broken tuple with NULL pointer.

bpo-20120 [https://bugs.python.org/issue20120]: Use RawConfigParser for .pypirc parsing, removing support for interpolation unintentionally added with move to Python 3. Behavior no longer does any interpolation in .pypirc files, matching behavior in Python 2.7 and Setuptools 19.0.

bpo-26659 [https://bugs.python.org/issue26659]: Make the builtin slice type support cycle collection.

bpo-26718 [https://bugs.python.org/issue26718]: super.__init__ no longer leaks memory if called multiple times. NOTE: A direct call of super.__init__ is not endorsed!

bpo-25339 [https://bugs.python.org/issue25339]: PYTHONIOENCODING now has priority over locale in setting the error handler for stdin and stdout.

bpo-26494 [https://bugs.python.org/issue26494]: Fixed crash on iterating exhausting iterators. Affected classes are generic sequence iterators, iterators of str, bytes, bytearray, list, tuple, set, frozenset, dict, OrderedDict, corresponding views and os.scandir() iterator.

bpo-26581 [https://bugs.python.org/issue26581]: If coding cookie is specified multiple times on a line in Python source code file, only the first one is taken to account.

bpo-26464 [https://bugs.python.org/issue26464]: Fix str.translate() when string is ASCII and first replacements removes character, but next replacement uses a non-ASCII character or a string longer than 1 character. Regression introduced in Python 3.5.0.

bpo-22836 [https://bugs.python.org/issue22836]: Ensure exception reports from PyErr_Display() and PyErr_WriteUnraisable() are sensible even when formatting them produces secondary errors. This affects the reports produced by sys.__excepthook__() and when __del__() raises an exception.

bpo-26302 [https://bugs.python.org/issue26302]: Correct behavior to reject comma as a legal character for cookie names.

bpo-4806 [https://bugs.python.org/issue4806]: Avoid masking the original TypeError exception when using star (*) unpacking in function calls. Based on patch by Hagen Fürstenau and Daniel Urban.

bpo-27138 [https://bugs.python.org/issue27138]: Fix the doc comment for FileFinder.find_spec().

bpo-26154 [https://bugs.python.org/issue26154]: Add a new private _PyThreadState_UncheckedGet() function to get the current Python thread state, but don’t issue a fatal error if it is NULL. This new function must be used instead of accessing directly the _PyThreadState_Current variable. The variable is no more exposed since Python 3.5.1 to hide the exact implementation of atomic C types, to avoid compiler issues.

bpo-26194 [https://bugs.python.org/issue26194]: Deque.insert() gave odd results for bounded deques that had reached their maximum size. Now an IndexError will be raised when attempting to insert into a full deque.

bpo-25843 [https://bugs.python.org/issue25843]: When compiling code, don’t merge constants if they are equal but have a different types. For example, f1, f2 = lambda: 1, lambda: 1.0 is now correctly compiled to two different functions: f1() returns 1 (int) and f2() returns 1.0 (int), even if 1 and 1.0 are equal.

bpo-22995 [https://bugs.python.org/issue22995]: [UPDATE] Comment out the one of the pickleability tests in _PyObject_GetState() due to regressions observed in Cython-based projects.

bpo-25961 [https://bugs.python.org/issue25961]: Disallowed null characters in the type name.

bpo-25973 [https://bugs.python.org/issue25973]: Fix segfault when an invalid nonlocal statement binds a name starting with two underscores.

bpo-22995 [https://bugs.python.org/issue22995]: Instances of extension types with a state that aren’t subclasses of list or dict and haven’t implemented any pickle-related methods (__reduce__, __reduce_ex__, __getnewargs__, __getnewargs_ex__, or __getstate__), can no longer be pickled. Including memoryview.

bpo-20440 [https://bugs.python.org/issue20440]: Massive replacing unsafe attribute setting code with special macro Py_SETREF.

bpo-25766 [https://bugs.python.org/issue25766]: Special method __bytes__() now works in str subclasses.

bpo-25421 [https://bugs.python.org/issue25421]: __sizeof__ methods of builtin types now use dynamic basic size. This allows sys.getsize() to work correctly with their subclasses with __slots__ defined.

bpo-25709 [https://bugs.python.org/issue25709]: Fixed problem with in-place string concatenation and utf-8 cache.

bpo-27147 [https://bugs.python.org/issue27147]: Mention PEP 420 in the importlib docs.

bpo-24097 [https://bugs.python.org/issue24097]: Fixed crash in object.__reduce__() if slot name is freed inside __getattr__.

bpo-24731 [https://bugs.python.org/issue24731]: Fixed crash on converting objects with special methods __bytes__, __trunc__, and __float__ returning instances of subclasses of bytes, int, and float to subclasses of bytes, int, and float correspondingly.

bpo-26478 [https://bugs.python.org/issue26478]: Fix semantic bugs when using binary operators with dictionary views and tuples.

bpo-26171 [https://bugs.python.org/issue26171]: Fix possible integer overflow and heap corruption in zipimporter.get_data().

bpo-25660 [https://bugs.python.org/issue25660]: Fix TAB key behaviour in REPL with readline.

bpo-25887 [https://bugs.python.org/issue25887]: Raise a RuntimeError when a coroutine object is awaited more than once.

bpo-27243 [https://bugs.python.org/issue27243]: Update the __aiter__ protocol: instead of returning an awaitable that resolves to an asynchronous iterator, the asynchronous iterator should be returned directly. Doing the former will trigger a PendingDeprecationWarning.

安全

bpo-26556 [https://bugs.python.org/issue26556]: Update expat to 2.1.1, fixes CVE-2015-1283.

Fix TLS stripping vulnerability in smtplib, CVE-2016-0772. Reported by Team Oststrom

库

bpo-21386 [https://bugs.python.org/issue21386]: Implement missing IPv4Address.is_global property. It was documented since 07a5610bae9d. Initial patch by Roger Luethi.

bpo-20900 [https://bugs.python.org/issue20900]: distutils register command now decodes HTTP responses correctly. Initial patch by ingrid.

A new version of typing.py provides several new classes and features: @overload outside stubs, DefaultDict, Text, ContextManager, Type[], NewType(), TYPE_CHECKING, and numerous bug fixes (note that some of the new features are not yet implemented in mypy or other static analyzers). Also classes for PEP 492 (Awaitable, AsyncIterable, AsyncIterator) have been added (in fact they made it into 3.5.1 but were never mentioned).

bpo-25738 [https://bugs.python.org/issue25738]: Stop http.server.BaseHTTPRequestHandler.send_error() from sending a message body for 205 Reset Content. Also, don’t send Content header fields in responses that don’t have a body. Patch by Susumu Koshiba.

bpo-21313 [https://bugs.python.org/issue21313]: Fix the “platform” module to tolerate when sys.version contains truncated build information.

安全

bpo-26839 [https://bugs.python.org/issue26839]: On Linux, os.urandom() now calls getrandom() with GRND_NONBLOCK to fall back on reading /dev/urandom if the urandom entropy pool is not initialized yet. Patch written by Colm Buckley.

库

bpo-27164 [https://bugs.python.org/issue27164]: In the zlib module, allow decompressing raw Deflate streams with a predefined zdict. Based on patch by Xiang Zhang.

bpo-24291 [https://bugs.python.org/issue24291]: Fix wsgiref.simple_server.WSGIRequestHandler to completely write data to the client. Previously it could do partial writes and truncate data. Also, wsgiref.handler.ServerHandler can now handle stdout doing partial writes, but this is deprecated.

bpo-26809 [https://bugs.python.org/issue26809]: Add __all__ to string. Patch by Emanuel Barry.

bpo-26373 [https://bugs.python.org/issue26373]: subprocess.Popen.communicate now correctly ignores BrokenPipeError when the child process dies before .communicate() is called in more/all circumstances.

bpo-21776 [https://bugs.python.org/issue21776]: distutils.upload now correctly handles HTTPError. Initial patch by Claudiu Popa.

bpo-27114 [https://bugs.python.org/issue27114]: Fix SSLContext._load_windows_store_certs fails with PermissionError

bpo-18383 [https://bugs.python.org/issue18383]: Avoid creating duplicate filters when using filterwarnings and simplefilter. Based on patch by Alex Shkop.

bpo-27057 [https://bugs.python.org/issue27057]: Fix os.set_inheritable() on Android, ioctl() is blocked by SELinux and fails with EACCESS. The function now falls back to fcntl(). Patch written by Michał Bednarski.

bpo-27014 [https://bugs.python.org/issue27014]: Fix infinite recursion using typing.py. Thanks to Kalle Tuure!

bpo-14132 [https://bugs.python.org/issue14132]: Fix urllib.request redirect handling when the target only has a query string. Original fix by Ján Janech.

bpo-17214 [https://bugs.python.org/issue17214]: The “urllib.request” module now percent-encodes non-ASCII bytes found in redirect target URLs. Some servers send Location header fields with non-ASCII bytes, but “http.client” requires the request target to be ASCII-encodable, otherwise a UnicodeEncodeError is raised. Based on patch by Christian Heimes.

bpo-26892 [https://bugs.python.org/issue26892]: Honor debuglevel flag in urllib.request.HTTPHandler. Patch contributed by Chi Hsuan Yen.

bpo-22274 [https://bugs.python.org/issue22274]: In the subprocess module, allow stderr to be redirected to stdout even when stdout is not redirected. Patch by Akira Li.

bpo-26807 [https://bugs.python.org/issue26807]: mock_open ‘files’ no longer error on readline at end of file. Patch from Yolanda Robla.

bpo-25745 [https://bugs.python.org/issue25745]: Fixed leaking a userptr in curses panel destructor.

bpo-26977 [https://bugs.python.org/issue26977]: Removed unnecessary, and ignored, call to sum of squares helper in statistics.pvariance.

bpo-26881 [https://bugs.python.org/issue26881]: The modulefinder module now supports extended opcode arguments.

bpo-23815 [https://bugs.python.org/issue23815]: Fixed crashes related to directly created instances of types in _tkinter and curses.panel modules.

bpo-17765 [https://bugs.python.org/issue17765]: weakref.ref() no longer silently ignores keyword arguments. Patch by Georg Brandl.

bpo-26873 [https://bugs.python.org/issue26873]: xmlrpc now raises ResponseError on unsupported type tags instead of silently return incorrect result.

bpo-26711 [https://bugs.python.org/issue26711]: Fixed the comparison of plistlib.Data with other types.

bpo-24114 [https://bugs.python.org/issue24114]: Fix an uninitialized variable in ctypes.util.

The bug only occurs on SunOS when the ctypes implementation searches for the crle program. Patch by Xiang Zhang. Tested on SunOS by Kees Bos.

bpo-26864 [https://bugs.python.org/issue26864]: In urllib.request, change the proxy bypass host checking against no_proxy to be case-insensitive, and to not match unrelated host names that happen to have a bypassed hostname as a suffix. Patch by Xiang Zhang.

bpo-26634 [https://bugs.python.org/issue26634]: recursive_repr() now sets __qualname__ of wrapper. Patch by Xiang Zhang.

bpo-26804 [https://bugs.python.org/issue26804]: urllib.request will prefer lower_case proxy environment variables over UPPER_CASE or Mixed_Case ones. Patch contributed by Hans-Peter Jansen.

bpo-26837 [https://bugs.python.org/issue26837]: assertSequenceEqual() now correctly outputs non-stringified differing items (like bytes in the -b mode). This affects assertListEqual() and assertTupleEqual().

bpo-26041 [https://bugs.python.org/issue26041]: Remove “will be removed in Python 3.7” from deprecation messages of platform.dist() and platform.linux_distribution(). Patch by Kumaripaba Miyurusara Athukorala.

bpo-26822 [https://bugs.python.org/issue26822]: itemgetter, attrgetter and methodcaller objects no longer silently ignore keyword arguments.

bpo-26733 [https://bugs.python.org/issue26733]: Disassembling a class now disassembles class and static methods. Patch by Xiang Zhang.

bpo-26801 [https://bugs.python.org/issue26801]: Fix error handling in shutil.get_terminal_size(), catch AttributeError instead of NameError. Patch written by Emanuel Barry.

bpo-24838 [https://bugs.python.org/issue24838]: tarfile’s ustar and gnu formats now correctly calculate name and link field limits for multibyte character encodings like utf-8.

安全

bpo-26657 [https://bugs.python.org/issue26657]: Fix directory traversal vulnerability with http.server on Windows. This fixes a regression that was introduced in 3.3.4rc1 and 3.4.0rc1. Based on patch by Philipp Hagemeister.

库

bpo-26717 [https://bugs.python.org/issue26717]: Stop encoding Latin-1-ized WSGI paths with UTF-8. Patch by Anthony Sottile.

bpo-26735 [https://bugs.python.org/issue26735]: Fix os.urandom() on Solaris 11.3 and newer when reading more than 1,024 bytes: call getrandom() multiple times with a limit of 1024 bytes per call.

bpo-16329 [https://bugs.python.org/issue16329]: Add .webm to mimetypes.types_map. Patch by Giampaolo Rodola’.

bpo-13952 [https://bugs.python.org/issue13952]: Add .csv to mimetypes.types_map. Patch by Geoff Wilson.

bpo-26709 [https://bugs.python.org/issue26709]: Fixed Y2038 problem in loading binary PLists.

bpo-23735 [https://bugs.python.org/issue23735]: Handle terminal resizing with Readline 6.3+ by installing our own SIGWINCH handler. Patch by Eric Price.

bpo-26586 [https://bugs.python.org/issue26586]: In http.server, respond with “413 Request header fields too large” if there are too many header fields to parse, rather than killing the connection and raising an unhandled exception. Patch by Xiang Zhang.

bpo-22854 [https://bugs.python.org/issue22854]: Change BufferedReader.writable() and BufferedWriter.readable() to always return False.

bpo-25195 [https://bugs.python.org/issue25195]: Fix a regression in mock.MagicMock. _Call is a subclass of tuple (changeset 3603bae63c13 only works for classes) so we need to implement __ne__ ourselves. Patch by Andrew Plummer.

bpo-26644 [https://bugs.python.org/issue26644]: Raise ValueError rather than SystemError when a negative length is passed to SSLSocket.recv() or read().

bpo-23804 [https://bugs.python.org/issue23804]: Fix SSL recv(0) and read(0) methods to return zero bytes instead of up to 1024.

bpo-26616 [https://bugs.python.org/issue26616]: Fixed a bug in datetime.astimezone() method.

bpo-21925 [https://bugs.python.org/issue21925]: warnings.formatwarning() now catches exceptions on linecache.getline(...) to be able to log ResourceWarning emitted late during the Python shutdown process.

bpo-24266 [https://bugs.python.org/issue24266]: Ctrl+C during Readline history search now cancels the search mode when compiled with Readline 7.

bpo-26560 [https://bugs.python.org/issue26560]: Avoid potential ValueError in BaseHandler.start_response. Initial patch by Peter Inglesby.

安全

bpo-26313 [https://bugs.python.org/issue26313]: ssl.py _load_windows_store_certs fails if windows cert store is empty. Patch by Baji.

库

bpo-26569 [https://bugs.python.org/issue26569]: Fix pyclbr.readmodule() and pyclbr.readmodule_ex() to support importing packages.

bpo-26499 [https://bugs.python.org/issue26499]: Account for remaining Content-Length in HTTPResponse.readline() and read1(). Based on patch by Silent Ghost. Also document that HTTPResponse now supports these methods.

bpo-25320 [https://bugs.python.org/issue25320]: Handle sockets in directories unittest discovery is scanning. Patch from Victor van den Elzen.

bpo-16181 [https://bugs.python.org/issue16181]: cookiejar.http2time() now returns None if year is higher than datetime.MAXYEAR.

bpo-26513 [https://bugs.python.org/issue26513]: Fixes platform module detection of Windows Server

bpo-23718 [https://bugs.python.org/issue23718]: Fixed parsing time in week 0 before Jan 1. Original patch by Tamás Bence Gedai.

bpo-20589 [https://bugs.python.org/issue20589]: Invoking Path.owner() and Path.group() on Windows now raise NotImplementedError instead of ImportError.

bpo-26177 [https://bugs.python.org/issue26177]: Fixed the keys() method for Canvas and Scrollbar widgets.

bpo-15068 [https://bugs.python.org/issue15068]: Got rid of excessive buffering in the fileinput module. The bufsize parameter is no longer used.

bpo-2202 [https://bugs.python.org/issue2202]: Fix UnboundLocalError in AbstractDigestAuthHandler.get_algorithm_impls. Initial patch by Mathieu Dupuy.

bpo-25718 [https://bugs.python.org/issue25718]: Fixed pickling and copying the accumulate() iterator with total is None.

bpo-26475 [https://bugs.python.org/issue26475]: Fixed debugging output for regular expressions with the (?x) flag.

bpo-26457 [https://bugs.python.org/issue26457]: Fixed the subnets() methods in IP network classes for the case when resulting prefix length is equal to maximal prefix length. Based on patch by Xiang Zhang.

bpo-26385 [https://bugs.python.org/issue26385]: Remove the file if the internal open() call in NamedTemporaryFile() fails. Patch by Silent Ghost.

bpo-26402 [https://bugs.python.org/issue26402]: Fix XML-RPC client to retry when the server shuts down a persistent connection. This was a regression related to the new http.client.RemoteDisconnected exception in 3.5.0a4.

bpo-25913 [https://bugs.python.org/issue25913]: Leading <~ is optional now in base64.a85decode() with adobe=True. Patch by Swati Jaiswal.

bpo-26186 [https://bugs.python.org/issue26186]: Remove an invalid type check in importlib.util.LazyLoader.

bpo-26367 [https://bugs.python.org/issue26367]: importlib.__import__() raises SystemError like builtins.__import__() when level is specified but without an accompanying package specified.

bpo-26309 [https://bugs.python.org/issue26309]: In the “socketserver” module, shut down the request (closing the connected socket) when verify_request() returns false. Patch by Aviv Palivoda.

安全

bpo-25939 [https://bugs.python.org/issue25939]: On Windows open the cert store readonly in ssl.enum_certificates.

库

bpo-25995 [https://bugs.python.org/issue25995]: os.walk() no longer uses FDs proportional to the tree depth.

bpo-26117 [https://bugs.python.org/issue26117]: The os.scandir() iterator now closes file descriptor not only when the iteration is finished, but when it was failed with error.

bpo-25911 [https://bugs.python.org/issue25911]: Restored support of bytes paths in os.walk() on Windows.

bpo-26045 [https://bugs.python.org/issue26045]: Add UTF-8 suggestion to error message when posting a non-Latin-1 string with http.client.

bpo-12923 [https://bugs.python.org/issue12923]: Reset FancyURLopener’s redirect counter even if there is an exception. Based on patches by Brian Brazil and Daniel Rocco.

bpo-25945 [https://bugs.python.org/issue25945]: Fixed a crash when unpickle the functools.partial object with wrong state. Fixed a leak in failed functools.partial constructor. “args” and “keywords” attributes of functools.partial have now always types tuple and dict correspondingly.

bpo-26202 [https://bugs.python.org/issue26202]: copy.deepcopy() now correctly copies range() objects with non-atomic attributes.

bpo-23076 [https://bugs.python.org/issue23076]: Path.glob() now raises a ValueError if it’s called with an invalid pattern. Patch by Thomas Nyberg.

bpo-19883 [https://bugs.python.org/issue19883]: Fixed possible integer overflows in zipimport.

bpo-26227 [https://bugs.python.org/issue26227]: On Windows, getnameinfo(), gethostbyaddr() and gethostbyname_ex() functions of the socket module now decode the hostname from the ANSI code page rather than UTF-8.

bpo-26147 [https://bugs.python.org/issue26147]: xmlrpc now works with strings not encodable with used non-UTF-8 encoding.

bpo-25935 [https://bugs.python.org/issue25935]: Garbage collector now breaks reference loops with OrderedDict.

bpo-16620 [https://bugs.python.org/issue16620]: Fixed AttributeError in msilib.Directory.glob().

bpo-26013 [https://bugs.python.org/issue26013]: Added compatibility with broken protocol 2 pickles created in old Python 3 versions (3.4.3 and lower).

bpo-25850 [https://bugs.python.org/issue25850]: Use cross-compilation by default for 64-bit Windows.

bpo-17633 [https://bugs.python.org/issue17633]: Improve zipimport’s support for namespace packages.

bpo-24705 [https://bugs.python.org/issue24705]: Fix sysconfig._parse_makefile not expanding ${} vars appearing before $() vars.

bpo-22138 [https://bugs.python.org/issue22138]: Fix mock.patch behavior when patching descriptors. Restore original values after patching. Patch contributed by Sean McCully.

bpo-25672 [https://bugs.python.org/issue25672]: In the ssl module, enable the SSL_MODE_RELEASE_BUFFERS mode option if it is safe to do so.

bpo-26012 [https://bugs.python.org/issue26012]: Don’t traverse into symlinks for ** pattern in pathlib.Path.[r]glob().

bpo-24120 [https://bugs.python.org/issue24120]: Ignore PermissionError when traversing a tree with pathlib.Path.[r]glob(). Patch by Ulrich Petri.

bpo-25447 [https://bugs.python.org/issue25447]: fileinput now uses sys.stdin as-is if it does not have a buffer attribute (restores backward compatibility).

bpo-25447 [https://bugs.python.org/issue25447]: Copying the lru_cache() wrapper object now always works, independently from the type of the wrapped object (by returning the original object unchanged).

bpo-24103 [https://bugs.python.org/issue24103]: Fixed possible use after free in ElementTree.XMLPullParser.

bpo-25860 [https://bugs.python.org/issue25860]: os.fwalk() no longer skips remaining directories when error occurs. Original patch by Samson Lee.

bpo-25914 [https://bugs.python.org/issue25914]: Fixed and simplified OrderedDict.__sizeof__.

bpo-25902 [https://bugs.python.org/issue25902]: Fixed various refcount issues in ElementTree iteration.

bpo-25717 [https://bugs.python.org/issue25717]: Restore the previous behaviour of tolerating most fstat() errors when opening files. This was a regression in 3.5a1, and stopped anonymous temporary files from working in special cases.

bpo-24903 [https://bugs.python.org/issue24903]: Fix regression in number of arguments compileall accepts when ‘-d’ is specified. The check on the number of arguments has been dropped completely as it never worked correctly anyway.

bpo-25764 [https://bugs.python.org/issue25764]: In the subprocess module, preserve any exception caused by fork() failure when preexec_fn is used.

bpo-6478 [https://bugs.python.org/issue6478]: _strptime’s regexp cache now is reset after changing timezone with time.tzset().

bpo-14285 [https://bugs.python.org/issue14285]: When executing a package with the “python -m package” option, and package initialization fails, a proper traceback is now reported. The “runpy” module now lets exceptions from package initialization pass back to the caller, rather than raising ImportError.

bpo-19771 [https://bugs.python.org/issue19771]: Also in runpy and the “-m” option, omit the irrelevant message “… is a package and cannot be directly executed” if the package could not even be initialized (e.g. due to a bad *.pyc file).

bpo-25177 [https://bugs.python.org/issue25177]: Fixed problem with the mean of very small and very large numbers. As a side effect, statistics.mean and statistics.variance should be significantly faster.

bpo-25718 [https://bugs.python.org/issue25718]: Fixed copying object with state with boolean value is false.

bpo-10131 [https://bugs.python.org/issue10131]: Fixed deep copying of minidom documents. Based on patch by Marian Ganisin.

bpo-25725 [https://bugs.python.org/issue25725]: Fixed a reference leak in pickle.loads() when unpickling invalid data including tuple instructions.

bpo-25663 [https://bugs.python.org/issue25663]: In the Readline completer, avoid listing duplicate global names, and search the global namespace before searching builtins.

bpo-25688 [https://bugs.python.org/issue25688]: Fixed file leak in ElementTree.iterparse() raising an error.

bpo-23914 [https://bugs.python.org/issue23914]: Fixed SystemError raised by unpickler on broken pickle data.

bpo-25691 [https://bugs.python.org/issue25691]: Fixed crash on deleting ElementTree.Element attributes.

bpo-25624 [https://bugs.python.org/issue25624]: ZipFile now always writes a ZIP_STORED header for directory entries. Patch by Dingyuan Wang.

Skip getaddrinfo if host is already resolved. Patch by A. Jesse Jiryu Davis.

bpo-26050 [https://bugs.python.org/issue26050]: Add asyncio.StreamReader.readuntil() method. Patch by Марк Коренберг.

bpo-25924 [https://bugs.python.org/issue25924]: Avoid unnecessary serialization of getaddrinfo(3) calls on OS X versions 10.5 or higher. Original patch by A. Jesse Jiryu Davis.

bpo-26406 [https://bugs.python.org/issue26406]: Avoid unnecessary serialization of getaddrinfo(3) calls on current versions of OpenBSD and NetBSD. Patch by A. Jesse Jiryu Davis.

bpo-26848 [https://bugs.python.org/issue26848]: Fix asyncio/subprocess.communicate() to handle empty input. Patch by Jack O’Connor.

bpo-27040 [https://bugs.python.org/issue27040]: Add loop.get_exception_handler method

bpo-27041 [https://bugs.python.org/issue27041]: asyncio: Add loop.create_future method

bpo-27223 [https://bugs.python.org/issue27223]: asyncio: Fix _read_ready and _write_ready to respect _conn_lost. Patch by Łukasz Langa.

bpo-22970 [https://bugs.python.org/issue22970]: asyncio: Fix inconsistency cancelling Condition.wait. Patch by David Coles.

IDLE

bpo-5124 [https://bugs.python.org/issue5124]: Paste with text selected now replaces the selection on X11. This matches how paste works on Windows, Mac, most modern Linux apps, and ttk widgets. Original patch by Serhiy Storchaka.

bpo-24759 [https://bugs.python.org/issue24759]: Make clear in idlelib.idle_test.__init__ that the directory is a private implementation of test.test_idle and tool for maintainers.

bpo-27196 [https://bugs.python.org/issue27196]: Stop ‘ThemeChanged’ warnings when running IDLE tests. These persisted after other warnings were suppressed in #20567. Apply Serhiy Storchaka’s update_idletasks solution to four test files. Record this additional advice in idle_test/README.txt

bpo-20567 [https://bugs.python.org/issue20567]: Revise idle_test/README.txt with advice about avoiding tk warning messages from tests. Apply advice to several IDLE tests.

bpo-27117 [https://bugs.python.org/issue27117]: Make colorizer htest and turtledemo work with dark themes. Move code for configuring text widget colors to a new function.

bpo-26673 [https://bugs.python.org/issue26673]: When tk reports font size as 0, change to size 10. Such fonts on Linux prevented the configuration dialog from opening.

bpo-21939 [https://bugs.python.org/issue21939]: Add test for IDLE’s percolator. Original patch by Saimadhav Heblikar.

bpo-21676 [https://bugs.python.org/issue21676]: Add test for IDLE’s replace dialog. Original patch by Saimadhav Heblikar.

bpo-18410 [https://bugs.python.org/issue18410]: Add test for IDLE’s search dialog. Original patch by Westley Martínez.

bpo-21703 [https://bugs.python.org/issue21703]: Add test for IDLE’s undo delegator. Original patch by Saimadhav Heblikar .

bpo-27044 [https://bugs.python.org/issue27044]: Add ConfigDialog.remove_var_callbacks to stop memory leaks.

bpo-23977 [https://bugs.python.org/issue23977]: Add more asserts to test_delegator.

bpo-20640 [https://bugs.python.org/issue20640]: Add tests for idlelib.configHelpSourceEdit. Patch by Saimadhav Heblikar.

In the ‘IDLE-console differences’ section of the IDLE doc, clarify how running with IDLE affects sys.modules and the standard streams.

bpo-25507 [https://bugs.python.org/issue25507]: fix incorrect change in IOBinding that prevented printing. Augment IOBinding htest to include all major IOBinding functions.

bpo-25905 [https://bugs.python.org/issue25905]: Revert unwanted conversion of ‘ to ’ RIGHT SINGLE QUOTATION MARK in README.txt and open this and NEWS.txt with ‘ascii’. Re-encode CREDITS.txt to utf-8 and open it with ‘utf-8’.

文档

bpo-19489 [https://bugs.python.org/issue19489]: Moved the search box from the sidebar to the header and footer of each page. Patch by Ammar Askar.

bpo-24136 [https://bugs.python.org/issue24136]: Document the new PEP 448 unpacking syntax of 3.5.

bpo-26736 [https://bugs.python.org/issue26736]: Used HTTPS for external links in the documentation if possible.

bpo-6953 [https://bugs.python.org/issue6953]: Rework the Readline module documentation to group related functions together, and add more details such as what underlying Readline functions and variables are accessed.

bpo-23606 [https://bugs.python.org/issue23606]: Adds note to ctypes documentation regarding cdll.msvcrt.

bpo-25500 [https://bugs.python.org/issue25500]: Fix documentation to not claim that __import__ is searched for in the global scope.

bpo-26014 [https://bugs.python.org/issue26014]: Update 3.x packaging documentation: * “See also” links to the new docs are now provided in the legacy pages * links to setuptools documentation have been updated

测试

bpo-21916 [https://bugs.python.org/issue21916]: Added tests for the turtle module. Patch by ingrid, Gregory Loyse and Jelle Zijlstra.

bpo-26523 [https://bugs.python.org/issue26523]: The multiprocessing thread pool (multiprocessing.dummy.Pool) was untested.

bpo-26015 [https://bugs.python.org/issue26015]: Added new tests for pickling iterators of mutable sequences.

bpo-26325 [https://bugs.python.org/issue26325]: Added test.support.check_no_resource_warning() to check that no ResourceWarning is emitted.

bpo-25940 [https://bugs.python.org/issue25940]: Changed test_ssl to use self-signed.pythontest.net. This avoids relying on svn.python.org, which recently changed root certificate.

bpo-25616 [https://bugs.python.org/issue25616]: Tests for OrderedDict are extracted from test_collections into separate file test_ordered_dict.

bpo-26583 [https://bugs.python.org/issue26583]: Skip test_timestamp_overflow in test_import if bytecode files cannot be written.

构建

bpo-26884 [https://bugs.python.org/issue26884]: Fix linking extension modules for cross builds. Patch by Xavier de Gaye.

bpo-22359 [https://bugs.python.org/issue22359]: Disable the rules for running _freeze_importlib and pgen when cross-compiling. The output of these programs is normally saved with the source code anyway, and is still regenerated when doing a native build. Patch by Xavier de Gaye.

bpo-27229 [https://bugs.python.org/issue27229]: Fix the cross-compiling pgen rule for in-tree builds. Patch by Xavier de Gaye.

bpo-21668 [https://bugs.python.org/issue21668]: Link audioop, _datetime, _ctypes_test modules to libm, except on Mac OS X. Patch written by Xavier de Gaye.

bpo-25702 [https://bugs.python.org/issue25702]: A –with-lto configure option has been added that will enable link time optimizations at build time during a make profile-opt. Some compilers and toolchains are known to not produce stable code when using LTO, be sure to test things thoroughly before relying on it. It can provide a few % speed up over profile-opt alone.

bpo-26624 [https://bugs.python.org/issue26624]: Adds validation of ucrtbase[d].dll version with warning for old versions.

bpo-17603 [https://bugs.python.org/issue17603]: Avoid error about nonexistant fileblocks.o file by using a lower-level check for st_blocks in struct stat.

bpo-26079 [https://bugs.python.org/issue26079]: Fixing the build output folder for tix-8.4.3.6. Patch by Bjoern Thiel.

bpo-26465 [https://bugs.python.org/issue26465]: 更新Windows 构建OpenSSL 1.0.2g.

bpo-24421 [https://bugs.python.org/issue24421]: Compile Modules/_math.c once, before building extensions. Previously it could fail to compile properly if the math and cmath builds were concurrent.

bpo-25348 [https://bugs.python.org/issue25348]: Added --pgo and --pgo-job arguments to PCbuild\build.bat for building with Profile-Guided Optimization. The old PCbuild\build_pgo.bat script is now deprecated, and simply calls PCbuild\build.bat --pgo %*.

bpo-25827 [https://bugs.python.org/issue25827]: Add support for building with ICC to configure, including a new --with-icc flag.

bpo-25696 [https://bugs.python.org/issue25696]: Fix installation of Python on UNIX with make -j9.

bpo-26930 [https://bugs.python.org/issue26930]: 更新OS X 10.5+（仅32位）安装程序以构建并链接OpenSSL 1.0.2h。

bpo-26268 [https://bugs.python.org/issue26268]: 更新Windows 构建OpenSSL 1.0.2f.

bpo-25136 [https://bugs.python.org/issue25136]: Support Apple Xcode 7’s new textual SDK stub libraries.

bpo-24324 [https://bugs.python.org/issue24324]: Do not enable unreachable code warnings when using gcc as the option does not work correctly in older versions of gcc and has been silently removed as of gcc-4.5.

Windows

bpo-27053 [https://bugs.python.org/issue27053]: Updates make_zip.py to correctly generate library ZIP file.

bpo-26268 [https://bugs.python.org/issue26268]: Update the prepare_ssl.py script to handle OpenSSL releases that don’t include the contents of the include directory (that is, 1.0.2e and later).

bpo-26071 [https://bugs.python.org/issue26071]: bdist_wininst created binaries fail to start and find 32bit Python

bpo-26073 [https://bugs.python.org/issue26073]: Update the list of magic numbers in launcher

bpo-26065 [https://bugs.python.org/issue26065]: Excludes venv from library when generating embeddable distro.

工具/示例

bpo-26799 [https://bugs.python.org/issue26799]: Fix python-gdb.py: don’t get C types once when the Python code is loaded, but get C types on demand. The C types can change if python-gdb.py is loaded before the Python executable. Patch written by Thomas Ilsche.

bpo-26271 [https://bugs.python.org/issue26271]: Fix the Freeze tool to properly use flags passed through configure. Patch by Daniel Shaulov.

bpo-26489 [https://bugs.python.org/issue26489]: Add dictionary unpacking support to Tools/parser/unparse.py. Patch by Guo Ci Teo.

bpo-26316 [https://bugs.python.org/issue26316]: Fix variable name typo in Argument Clinic.

Windows

bpo-17500 [https://bugs.python.org/issue17500]: Remove unused and outdated icons. (See also: https://github.com/python/pythondotorg/issues/945)

Python 3.5.1 正式版

发布日期: 2015-12-06

核心与内置

bpo-25709 [https://bugs.python.org/issue25709]: Fixed problem with in-place string concatenation and utf-8 cache.

Windows

bpo-25715 [https://bugs.python.org/issue25715]: Python 3.5.1 installer shows wrong upgrade path and incorrect logic for launcher detection.

Python 3.5.1 rc1

发布日期: 2015-11-22

核心与内置

bpo-25630 [https://bugs.python.org/issue25630]: Fix a possible segfault during argument parsing in functions that accept filesystem paths.

bpo-23564 [https://bugs.python.org/issue23564]: Fixed a partially broken sanity check in the _posixsubprocess internals regarding how fds_to_pass were passed to the child. The bug had no actual impact as subprocess.py already avoided it.

bpo-25388 [https://bugs.python.org/issue25388]: Fixed tokenizer crash when processing undecodable source code with a null byte.

bpo-25462 [https://bugs.python.org/issue25462]: The hash of the key now is calculated only once in most operations in C implementation of OrderedDict.

bpo-22995 [https://bugs.python.org/issue22995]: Default implementation of __reduce__ and __reduce_ex__ now rejects builtin types with not defined __new__.

bpo-25555 [https://bugs.python.org/issue25555]: Fix parser and AST: fill lineno and col_offset of “arg” node when compiling AST from Python objects.

bpo-24802 [https://bugs.python.org/issue24802]: Avoid buffer overreads when int(), float(), compile(), exec() and eval() are passed bytes-like objects. These objects are not necessarily terminated by a null byte, but the functions assumed they were.

bpo-24726 [https://bugs.python.org/issue24726]: Fixed a crash and leaking NULL in repr() of OrderedDict that was mutated by direct calls of dict methods.

bpo-25449 [https://bugs.python.org/issue25449]: Iterating OrderedDict with keys with unstable hash now raises KeyError in C implementations as well as in Python implementation.

bpo-25395 [https://bugs.python.org/issue25395]: Fixed crash when highly nested OrderedDict structures were garbage collected.

bpo-25274 [https://bugs.python.org/issue25274]: sys.setrecursionlimit() now raises a RecursionError if the new recursion limit is too low depending at the current recursion depth. Modify also the “lower-water mark” formula to make it monotonic. This mark is used to decide when the overflowed flag of the thread state is reset.

bpo-24402 [https://bugs.python.org/issue24402]: Fix input() to prompt to the redirected stdout when sys.stdout.fileno() fails.

bpo-24806 [https://bugs.python.org/issue24806]: Prevent builtin types that are not allowed to be subclassed from being subclassed through multiple inheritance.

bpo-24848 [https://bugs.python.org/issue24848]: Fixed a number of bugs in UTF-7 decoding of misformed data.

bpo-25280 [https://bugs.python.org/issue25280]: Import trace messages emitted in verbose (-v) mode are no longer formatted twice.

bpo-25003 [https://bugs.python.org/issue25003]: On Solaris 11.3 or newer, os.urandom() now uses the getrandom() function instead of the getentropy() function. The getentropy() function is blocking to generate very good quality entropy, os.urandom() doesn’t need such high-quality entropy.

bpo-25182 [https://bugs.python.org/issue25182]: The stdprinter (used as sys.stderr before the io module is imported at startup) now uses the backslashreplace error handler.

bpo-25131 [https://bugs.python.org/issue25131]: Make the line number and column offset of set/dict literals and comprehensions correspond to the opening brace.

bpo-25150 [https://bugs.python.org/issue25150]: Hide the private _Py_atomic_xxx symbols from the public Python.h header to fix a compilation error with OpenMP. PyThreadState_GET() becomes an alias to PyThreadState_Get() to avoid ABI incompatibilities.

库

bpo-25626 [https://bugs.python.org/issue25626]: Change three zlib functions to accept sizes that fit in Py_ssize_t, but internally cap those sizes to UINT_MAX. This resolves a regression in 3.5 where GzipFile.read() failed to read chunks larger than 2 or 4 GiB. The change affects the zlib.Decompress.decompress() max_length parameter, the zlib.decompress() bufsize parameter, and the zlib.Decompress.flush() length parameter.

bpo-25583 [https://bugs.python.org/issue25583]: Avoid incorrect errors raised by os.makedirs(exist_ok=True) when the OS gives priority to errors such as EACCES over EEXIST.

bpo-25593 [https://bugs.python.org/issue25593]: Change semantics of EventLoop.stop() in asyncio.

bpo-6973 [https://bugs.python.org/issue6973]: When we know a subprocess.Popen process has died, do not allow the send_signal(), terminate(), or kill() methods to do anything as they could potentially signal a different process.

bpo-25590 [https://bugs.python.org/issue25590]: In the Readline completer, only call getattr() once per attribute.

bpo-25498 [https://bugs.python.org/issue25498]: Fix a crash when garbage-collecting ctypes objects created by wrapping a memoryview. This was a regression made in 3.5a1. Based on patch by Eryksun.

bpo-25584 [https://bugs.python.org/issue25584]: Added “escape” to the __all__ list in the glob module.

bpo-25584 [https://bugs.python.org/issue25584]: Fixed recursive glob() with patterns starting with **.

bpo-25446 [https://bugs.python.org/issue25446]: Fix regression in smtplib’s AUTH LOGIN support.

bpo-18010 [https://bugs.python.org/issue18010]: Fix the pydoc web server’s module search function to handle exceptions from importing packages.

bpo-25554 [https://bugs.python.org/issue25554]: Got rid of circular references in regular expression parsing.

bpo-25510 [https://bugs.python.org/issue25510]: fileinput.FileInput.readline() now returns b’’ instead of ‘’ at the end if the FileInput was opened with binary mode. Patch by Ryosuke Ito.

bpo-25503 [https://bugs.python.org/issue25503]: Fixed inspect.getdoc() for inherited docstrings of properties. Original patch by John Mark Vandenberg.

bpo-25515 [https://bugs.python.org/issue25515]: Always use os.urandom as a source of randomness in uuid.uuid4.

bpo-21827 [https://bugs.python.org/issue21827]: Fixed textwrap.dedent() for the case when largest common whitespace is a substring of smallest leading whitespace. Based on patch by Robert Li.

bpo-25447 [https://bugs.python.org/issue25447]: The lru_cache() wrapper objects now can be copied and pickled (by returning the original object unchanged).

bpo-25390 [https://bugs.python.org/issue25390]: typing: Don’t crash on Union[str, Pattern].

bpo-25441 [https://bugs.python.org/issue25441]: asyncio: Raise error from drain() when socket is closed.

bpo-25410 [https://bugs.python.org/issue25410]: Cleaned up and fixed minor bugs in C implementation of OrderedDict.

bpo-25411 [https://bugs.python.org/issue25411]: Improved Unicode support in SMTPHandler through better use of the email package. Thanks to user simon04 for the patch.

bpo-25407 [https://bugs.python.org/issue25407]: Remove mentions of the formatter module being removed in Python 3.6.

bpo-25406 [https://bugs.python.org/issue25406]: Fixed a bug in C implementation of OrderedDict.move_to_end() that caused segmentation fault or hang in iterating after moving several items to the start of ordered dict.

bpo-25364 [https://bugs.python.org/issue25364]: zipfile now works in threads disabled builds.

bpo-25328 [https://bugs.python.org/issue25328]: smtpd’s SMTPChannel now correctly raises a ValueError if both decode_data and enable_SMTPUTF8 are set to true.

bpo-25316 [https://bugs.python.org/issue25316]: distutils raises OSError instead of DistutilsPlatformError when MSVC is not installed.

bpo-25380 [https://bugs.python.org/issue25380]: Fixed protocol for the STACK_GLOBAL opcode in pickletools.opcodes.

bpo-23972 [https://bugs.python.org/issue23972]: Updates asyncio datagram create method allowing reuseport and reuseaddr socket options to be set prior to binding the socket. Mirroring the existing asyncio create_server method the reuseaddr option for datagram sockets defaults to True if the O/S is ‘posix’ (except if the platform is Cygwin). Patch by Chris Laws.

bpo-25304 [https://bugs.python.org/issue25304]: Add asyncio.run_coroutine_threadsafe(). This lets you submit a coroutine to a loop from another thread, returning a concurrent.futures.Future. By Vincent Michel.

bpo-25232 [https://bugs.python.org/issue25232]: Fix CGIRequestHandler to split the query from the URL at the first question mark (?) rather than the last. Patch from Xiang Zhang.

bpo-24657 [https://bugs.python.org/issue24657]: Prevent CGIRequestHandler from collapsing slashes in the query part of the URL as if it were a path. Patch from Xiang Zhang.

bpo-24483 [https://bugs.python.org/issue24483]: C implementation of functools.lru_cache() now calculates key’s hash only once.

bpo-22958 [https://bugs.python.org/issue22958]: Constructor and update method of weakref.WeakValueDictionary now accept the self and the dict keyword arguments.

bpo-22609 [https://bugs.python.org/issue22609]: Constructor of collections.UserDict now accepts the self keyword argument.

bpo-25111 [https://bugs.python.org/issue25111]: Fixed comparison of traceback.FrameSummary.

bpo-25262 [https://bugs.python.org/issue25262]: Added support for BINBYTES8 opcode in Python implementation of unpickler. Highest 32 bits of 64-bit size for BINUNICODE8 and BINBYTES8 opcodes no longer silently ignored on 32-bit platforms in C implementation.

bpo-25034 [https://bugs.python.org/issue25034]: Fix string.Formatter problem with auto-numbering and nested format_specs. Patch by Anthon van der Neut.

bpo-25233 [https://bugs.python.org/issue25233]: Rewrite the guts of asyncio.Queue and asyncio.Semaphore to be more understandable and correct.

bpo-25203 [https://bugs.python.org/issue25203]: Failed readline.set_completer_delims() no longer left the module in inconsistent state.

bpo-23600 [https://bugs.python.org/issue23600]: Default implementation of tzinfo.fromutc() was returning wrong results in some cases.

bpo-23329 [https://bugs.python.org/issue23329]: Allow the ssl module to be built with older versions of LibreSSL.

Prevent overflow in _Unpickler_Read.

bpo-25047 [https://bugs.python.org/issue25047]: The XML encoding declaration written by Element Tree now respects the letter case given by the user. This restores the ability to write encoding names in uppercase like “UTF-8”, which worked in Python 2.

bpo-25135 [https://bugs.python.org/issue25135]: Make deque_clear() safer by emptying the deque before clearing. This helps avoid possible reentrancy issues.

bpo-19143 [https://bugs.python.org/issue19143]: platform module now reads Windows version from kernel32.dll to avoid compatibility shims.

bpo-25092 [https://bugs.python.org/issue25092]: Fix datetime.strftime() failure when errno was already set to EINVAL.

bpo-23517 [https://bugs.python.org/issue23517]: Fix rounding in fromtimestamp() and utcfromtimestamp() methods of datetime.datetime: microseconds are now rounded to nearest with ties going to nearest even integer (ROUND_HALF_EVEN), instead of being rounding towards minus infinity (ROUND_FLOOR). It’s important that these methods use the same rounding mode than datetime.timedelta to keep the property: (datetime(1970,1,1) + timedelta(seconds=t)) == datetime.utcfromtimestamp(t). It also the rounding mode used by round(float) for example.

bpo-25155 [https://bugs.python.org/issue25155]: Fix datetime.datetime.now() and datetime.datetime.utcnow() on Windows to support date after year 2038. It was a regression introduced in Python 3.5.0.

bpo-25108 [https://bugs.python.org/issue25108]: Omitted internal frames in traceback functions print_stack(), format_stack(), and extract_stack() called without arguments.

bpo-25118 [https://bugs.python.org/issue25118]: Fix a regression of Python 3.5.0 in os.waitpid() on Windows.

bpo-24684 [https://bugs.python.org/issue24684]: socket.socket.getaddrinfo() now calls PyUnicode_AsEncodedString() instead of calling the encode() method of the host, to handle correctly custom string with an encode() method which doesn’t return a byte string. The encoder of the IDNA codec is now called directly instead of calling the encode() method of the string.

bpo-25060 [https://bugs.python.org/issue25060]: Correctly compute stack usage of the BUILD_MAP opcode.

bpo-24857 [https://bugs.python.org/issue24857]: Comparing call_args to a long sequence now correctly returns a boolean result instead of raising an exception. Patch by A Kaptur.

bpo-23144 [https://bugs.python.org/issue23144]: Make sure that HTMLParser.feed() returns all the data, even when convert_charrefs is True.

bpo-24982 [https://bugs.python.org/issue24982]: shutil.make_archive() with the “zip” format now adds entries for directories (including empty directories) in ZIP file.

bpo-25019 [https://bugs.python.org/issue25019]: Fixed a crash caused by setting non-string key of expat parser. Based on patch by John Leitch.

bpo-16180 [https://bugs.python.org/issue16180]: Exit pdb if file has syntax error, instead of trapping user in an infinite loop. Patch by Xavier de Gaye.

bpo-24891 [https://bugs.python.org/issue24891]: Fix a race condition at Python startup if the file descriptor of stdin (0), stdout (1) or stderr (2) is closed while Python is creating sys.stdin, sys.stdout and sys.stderr objects. These attributes are now set to None if the creation of the object failed, instead of raising an OSError exception. Initial patch written by Marco Paolini.

bpo-24992 [https://bugs.python.org/issue24992]: Fix error handling and a race condition (related to garbage collection) in collections.OrderedDict constructor.

bpo-24881 [https://bugs.python.org/issue24881]: Fixed setting binary mode in Python implementation of FileIO on Windows and Cygwin. Patch from Akira Li.

bpo-25578 [https://bugs.python.org/issue25578]: Fix (another) memory leak in SSLSocket.getpeercer().

bpo-25530 [https://bugs.python.org/issue25530]: Disable the vulnerable SSLv3 protocol by default when creating ssl.SSLContext.

bpo-25569 [https://bugs.python.org/issue25569]: Fix memory leak in SSLSocket.getpeercert().

bpo-25471 [https://bugs.python.org/issue25471]: Sockets returned from accept() shouldn’t appear to be nonblocking.

bpo-25319 [https://bugs.python.org/issue25319]: When threading.Event is reinitialized, the underlying condition should use a regular lock rather than a recursive lock.

bpo-21112 [https://bugs.python.org/issue21112]: Fix regression in unittest.expectedFailure on subclasses. Patch from Berker Peksag.

bpo-24764 [https://bugs.python.org/issue24764]: cgi.FieldStorage.read_multi() now ignores the Content-Length header in part headers. Patch written by Peter Landry and reviewed by Pierre Quentel.

bpo-24913 [https://bugs.python.org/issue24913]: Fix overrun error in deque.index(). Found by John Leitch and Bryce Darling.

bpo-24774 [https://bugs.python.org/issue24774]: Fix docstring in http.server.test. Patch from Chiu-Hsiang Hsu.

bpo-21159 [https://bugs.python.org/issue21159]: Improve message in configparser.InterpolationMissingOptionError. Patch from Łukasz Langa.

bpo-20362 [https://bugs.python.org/issue20362]: Honour TestCase.longMessage correctly in assertRegex. Patch from Ilia Kurenkov.

bpo-23572 [https://bugs.python.org/issue23572]: Fixed functools.singledispatch on classes with falsy metaclasses. Patch by Ethan Furman.

asyncio: ensure_future() now accepts awaitable objects.

IDLE

bpo-15348 [https://bugs.python.org/issue15348]: Stop the debugger engine (normally in a user process) before closing the debugger window (running in the IDLE process). This prevents the RuntimeErrors that were being caught and ignored.

bpo-24455 [https://bugs.python.org/issue24455]: Prevent IDLE from hanging when a) closing the shell while the debugger is active (15347); b) closing the debugger with the [X] button (15348); and c) activating the debugger when already active (24455). The patch by Mark Roseman does this by making two changes. 1. Suspend and resume the gui.interaction method with the tcl vwait mechanism intended for this purpose (instead of root.mainloop & .quit). 2. In gui.run, allow any existing interaction to terminate first.

Change ‘The program’ to ‘Your program’ in an IDLE ‘kill program?’ message to make it clearer that the program referred to is the currently running user program, not IDLE itself.

bpo-24750 [https://bugs.python.org/issue24750]: Improve the appearance of the IDLE editor window status bar. Patch by Mark Roseman.

bpo-25313 [https://bugs.python.org/issue25313]: Change the handling of new built-in text color themes to better address the compatibility problem introduced by the addition of IDLE Dark. Consistently use the revised idleConf.CurrentTheme everywhere in idlelib.

bpo-24782 [https://bugs.python.org/issue24782]: Extension configuration is now a tab in the IDLE Preferences dialog rather than a separate dialog. The former tabs are now a sorted list. Patch by Mark Roseman.

bpo-22726 [https://bugs.python.org/issue22726]: Re-activate the config dialog help button with some content about the other buttons and the new IDLE Dark theme.

bpo-24820 [https://bugs.python.org/issue24820]: IDLE now has an ‘IDLE Dark’ built-in text color theme. It is more or less IDLE Classic inverted, with a cobalt blue background. Strings, comments, keywords, … are still green, red, orange, … . To use it with IDLEs released before November 2015, hit the ‘Save as New Custom Theme’ button and enter a new name, such as ‘Custom Dark’. The custom theme will work with any IDLE release, and can be modified.

bpo-25224 [https://bugs.python.org/issue25224]: README.txt is now an idlelib index for IDLE developers and curious users. The previous user content is now in the IDLE doc chapter. ‘IDLE’ now means ‘Integrated Development and Learning Environment’.

bpo-24820 [https://bugs.python.org/issue24820]: Users can now set breakpoint colors in Settings -> Custom Highlighting. Original patch by Mark Roseman.

bpo-24972 [https://bugs.python.org/issue24972]: Inactive selection background now matches active selection background, as configured by users, on all systems. Found items are now always highlighted on Windows. Initial patch by Mark Roseman.

bpo-24570 [https://bugs.python.org/issue24570]: Idle: make calltip and completion boxes appear on Macs affected by a tk regression. Initial patch by Mark Roseman.

bpo-24988 [https://bugs.python.org/issue24988]: Idle ScrolledList context menus (used in debugger) now work on Mac Aqua. Patch by Mark Roseman.

bpo-24801 [https://bugs.python.org/issue24801]: Make right-click for context menu work on Mac Aqua. Patch by Mark Roseman.

bpo-25173 [https://bugs.python.org/issue25173]: Associate tkinter messageboxes with a specific widget. For Mac OSX, make them a ‘sheet’. Patch by Mark Roseman.

bpo-25198 [https://bugs.python.org/issue25198]: Enhance the initial html viewer now used for Idle Help. Properly indent fixed-pitch text (patch by Mark Roseman). Give code snippet a very Sphinx-like light blueish-gray background. Re-use initial width and height set by users for shell and editor. When the Table of Contents (TOC) menu is used, put the section header at the top of the screen.

bpo-25225 [https://bugs.python.org/issue25225]: Condense and rewrite Idle doc section on text colors.

bpo-21995 [https://bugs.python.org/issue21995]: Explain some differences between IDLE and console Python.

bpo-22820 [https://bugs.python.org/issue22820]: Explain need for print when running file from Idle editor.

bpo-25224 [https://bugs.python.org/issue25224]: Doc: augment Idle feature list and no-subprocess section.

bpo-25219 [https://bugs.python.org/issue25219]: Update doc for Idle command line options. Some were missing and notes were not correct.

bpo-24861 [https://bugs.python.org/issue24861]: Most of idlelib is private and subject to change. Use idleib.idle.* to start Idle. See idlelib.__init__.__doc__.

bpo-25199 [https://bugs.python.org/issue25199]: Idle: add synchronization comments for future maintainers.

bpo-16893 [https://bugs.python.org/issue16893]: Replace help.txt with help.html for Idle doc display. The new idlelib/help.html is rstripped Doc/build/html/library/idle.html. It looks better than help.txt and will better document Idle as released. The tkinter html viewer that works for this file was written by Mark Roseman. The now unused EditorWindow.HelpDialog class and helt.txt file are deprecated.

bpo-24199 [https://bugs.python.org/issue24199]: Deprecate unused idlelib.idlever with possible removal in 3.6.

bpo-24790 [https://bugs.python.org/issue24790]: Remove extraneous code (which also create 2 & 3 conflicts).

文档

bpo-22558 [https://bugs.python.org/issue22558]: Add remaining doc links to source code for Python-coded modules. Patch by Yoni Lavi.

bpo-12067 [https://bugs.python.org/issue12067]: Rewrite Comparisons section in the Expressions chapter of the language reference. Some of the details of comparing mixed types were incorrect or ambiguous. NotImplemented is only relevant at a lower level than the Expressions chapter. Added details of comparing range() objects, and default behaviour and consistency suggestions for user-defined classes. Patch from Andy Maier.

bpo-24952 [https://bugs.python.org/issue24952]: Clarify the default size argument of stack_size() in the “threading” and “_thread” modules. Patch from Mattip.

bpo-23725 [https://bugs.python.org/issue23725]: Overhaul tempfile docs. Note deprecated status of mktemp. Patch from Zbigniew Jędrzejewski-Szmek.

bpo-24808 [https://bugs.python.org/issue24808]: Update the types of some PyTypeObject fields. Patch by Joseph Weston.

bpo-22812 [https://bugs.python.org/issue22812]: Fix unittest discovery examples. Patch from Pam McA’Nulty.

测试

bpo-25449 [https://bugs.python.org/issue25449]: Added tests for OrderedDict subclasses.

bpo-25099 [https://bugs.python.org/issue25099]: Make test_compileall not fail when an entry on sys.path cannot be written to (commonly seen in administrative installs on Windows).

bpo-23919 [https://bugs.python.org/issue23919]: Prevents assert dialogs appearing in the test suite.

PCbuild\rt.bat now accepts an unlimited number of arguments to pass along to regrtest.py. Previously there was a limit of 9.

构建

bpo-24915 [https://bugs.python.org/issue24915]: Add LLVM support for PGO builds and use the test suite to generate the profile data. Initial patch by Alecsandru Patrascu of Intel.

bpo-24910 [https://bugs.python.org/issue24910]: Windows MSIs now have unique display names.

bpo-24986 [https://bugs.python.org/issue24986]: It is now possible to build Python on Windows without errors when external libraries are not available.

Windows

bpo-25450 [https://bugs.python.org/issue25450]: Updates shortcuts to start Python in installation directory.

bpo-25164 [https://bugs.python.org/issue25164]: Changes default all-users install directory to match per-user directory.

bpo-25143 [https://bugs.python.org/issue25143]: Improves installer error messages for unsupported platforms.

bpo-25163 [https://bugs.python.org/issue25163]: Display correct directory in installer when using non-default settings.

bpo-25361 [https://bugs.python.org/issue25361]: Disables use of SSE2 instructions in Windows 32-bit build

bpo-25089 [https://bugs.python.org/issue25089]: Adds logging to installer for case where launcher is not selected on upgrade.

bpo-25165 [https://bugs.python.org/issue25165]: Windows uninstallation should not remove launcher if other versions remain

bpo-25112 [https://bugs.python.org/issue25112]: py.exe launcher is missing icons

bpo-25102 [https://bugs.python.org/issue25102]: Windows installer does not precompile for -O or -OO.

bpo-25081 [https://bugs.python.org/issue25081]: Makes Back button in installer go back to upgrade page when upgrading.

bpo-25091 [https://bugs.python.org/issue25091]: Increases font size of the installer.

bpo-25126 [https://bugs.python.org/issue25126]: Clarifies that the non-web installer will download some components.

bpo-25213 [https://bugs.python.org/issue25213]: Restores requestedExecutionLevel to manifest to disable UAC virtualization.

bpo-25022 [https://bugs.python.org/issue25022]: Removed very outdated PC/example_nt/ directory.

工具/示例

bpo-25440 [https://bugs.python.org/issue25440]: Fix output of python-config –extension-suffix.

Python 3.5.0 正式版

发布日期: 2015-09-13

构建

bpo-25071 [https://bugs.python.org/issue25071]: Windows installer should not require TargetDir parameter when installing quietly.

Python 3.5.0 rc4

发布日期: 2015-09-09

库

bpo-25029 [https://bugs.python.org/issue25029]: Fixes MemoryError in test_strptime.

构建

bpo-25027 [https://bugs.python.org/issue25027]: Reverts partial-static build options and adds vcruntime140.dll to Windows installation.

Python 3.5.0 rc3

发布日期: 2015-09-07

核心与内置

bpo-24305 [https://bugs.python.org/issue24305]: Prevent import subsystem stack frames from being counted by the warnings.warn(stacklevel=) parameter.

bpo-24912 [https://bugs.python.org/issue24912]: Prevent __class__ assignment to immutable built-in objects.

bpo-24975 [https://bugs.python.org/issue24975]: Fix AST compilation for PEP 448 syntax.

库

bpo-24917 [https://bugs.python.org/issue24917]: time_strftime() buffer over-read.

bpo-24748 [https://bugs.python.org/issue24748]: To resolve a compatibility problem found with py2exe and pywin32, imp.load_dynamic() once again ignores previously loaded modules to support Python modules replacing themselves with extension modules. Patch by Petr Viktorin.

bpo-24635 [https://bugs.python.org/issue24635]: Fixed a bug in typing.py where isinstance([], typing.Iterable) would return True once, then False on subsequent calls.

bpo-24989 [https://bugs.python.org/issue24989]: Fixed buffer overread in BytesIO.readline() if a position is set beyond size. Based on patch by John Leitch.

bpo-24913 [https://bugs.python.org/issue24913]: Fix overrun error in deque.index(). Found by John Leitch and Bryce Darling.

Python 3.5.0 rc2

发布日期: 2015-08-25

核心与内置

bpo-24769 [https://bugs.python.org/issue24769]: Interpreter now starts properly when dynamic loading is disabled. Patch by Petr Viktorin.

bpo-21167 [https://bugs.python.org/issue21167]: NAN operations are now handled correctly when python is compiled with ICC even if -fp-model strict is not specified.

bpo-24492 [https://bugs.python.org/issue24492]: A “package” lacking a __name__ attribute when trying to perform a from .. import ... statement will trigger an ImportError instead of an AttributeError.

库

bpo-24847 [https://bugs.python.org/issue24847]: Removes vcruntime140.dll dependency from Tcl/Tk.

bpo-24839 [https://bugs.python.org/issue24839]: platform._syscmd_ver raises DeprecationWarning

bpo-24867 [https://bugs.python.org/issue24867]: Fix Task.get_stack() for ‘async def’ coroutines

Python 3.5.0 rc1

发布日期: 2015-08-09

核心与内置

bpo-24667 [https://bugs.python.org/issue24667]: Resize odict in all cases that the underlying dict resizes.

库

bpo-24824 [https://bugs.python.org/issue24824]: Signatures of codecs.encode() and codecs.decode() now are compatible with pydoc.

bpo-24634 [https://bugs.python.org/issue24634]: Importing uuid should not try to load libc on Windows

bpo-24798 [https://bugs.python.org/issue24798]: _msvccompiler.py doesn’t properly support manifests

bpo-4395 [https://bugs.python.org/issue4395]: Better testing and documentation of binary operators. Patch by Martin Panter.

bpo-23973 [https://bugs.python.org/issue23973]: Update typing.py from GitHub repo.

bpo-23004 [https://bugs.python.org/issue23004]: mock_open() now reads binary data correctly when the type of read_data is bytes. Initial patch by Aaron Hill.

bpo-23888 [https://bugs.python.org/issue23888]: Handle fractional time in cookie expiry. Patch by ssh.

bpo-23652 [https://bugs.python.org/issue23652]: Make it possible to compile the select module against the libc headers from the Linux Standard Base, which do not include some EPOLL macros. Patch by Matt Frank.

bpo-22932 [https://bugs.python.org/issue22932]: Fix timezones in email.utils.formatdate. Patch from Dmitry Shachnev.

bpo-23779 [https://bugs.python.org/issue23779]: imaplib raises TypeError if authenticator tries to abort. Patch from Craig Holmquist.

bpo-23319 [https://bugs.python.org/issue23319]: Fix ctypes.BigEndianStructure, swap correctly bytes. Patch written by Matthieu Gautier.

bpo-23254 [https://bugs.python.org/issue23254]: Document how to close the TCPServer listening socket. Patch from Martin Panter.

bpo-19450 [https://bugs.python.org/issue19450]: 更新Windows和OS X安装程序以使用SQLite 3.8.11

bpo-17527 [https://bugs.python.org/issue17527]: Add PATCH to wsgiref.validator. Patch from Luca Sbardella.

bpo-24791 [https://bugs.python.org/issue24791]: Fix grammar regression for call syntax: ‘g(*a or b)’.

IDLE

bpo-23672 [https://bugs.python.org/issue23672]: Allow Idle to edit and run files with astral chars in name. Patch by Mohd Sanad Zaki Rizvi.

bpo-24745 [https://bugs.python.org/issue24745]: Idle editor default font. Switch from Courier to platform-sensitive TkFixedFont. This should not affect current customized font selections. If there is a problem, edit $HOME/.idlerc/config-main.cfg and remove ‘fontxxx’ entries from [Editor Window]. Patch by Mark Roseman.

bpo-21192 [https://bugs.python.org/issue21192]: Idle editor. When a file is run, put its name in the restart bar. Do not print false prompts. Original patch by Adnan Umer.

bpo-13884 [https://bugs.python.org/issue13884]: Idle menus. Remove tearoff lines. Patch by Roger Serwy.

文档

bpo-24129 [https://bugs.python.org/issue24129]: Clarify the reference documentation for name resolution. This includes removing the assumption that readers will be familiar with the name resolution scheme Python used prior to the introduction of lexical scoping for function namespaces. Patch by Ivan Levkivskyi.

bpo-20769 [https://bugs.python.org/issue20769]: Improve reload() docs. Patch by Dorian Pula.

bpo-23589 [https://bugs.python.org/issue23589]: Remove duplicate sentence from the FAQ. Patch by Yongzhi Pan.

bpo-24729 [https://bugs.python.org/issue24729]: Correct IO tutorial to match implementation regarding encoding parameter to open function.

测试

bpo-24751 [https://bugs.python.org/issue24751]: When running regrtest with the -w command line option, a test run is no longer marked as a failure if all tests succeed when re-run.

Python 3.5.0 beta 4

发布日期: 2015-07-26

核心与内置

bpo-23573 [https://bugs.python.org/issue23573]: Restored optimization of bytes.rfind() and bytearray.rfind() for single-byte argument on Linux.

bpo-24569 [https://bugs.python.org/issue24569]: Make PEP 448 dictionary evaluation more consistent.

bpo-24583 [https://bugs.python.org/issue24583]: Fix crash when set is mutated while being updated.

bpo-24407 [https://bugs.python.org/issue24407]: Fix crash when dict is mutated while being updated.

bpo-24619 [https://bugs.python.org/issue24619]: New approach for tokenizing async/await. As a consequence, it is now possible to have one-line ‘async def foo(): await ..’ functions.

bpo-24687 [https://bugs.python.org/issue24687]: Plug refleak on SyntaxError in function parameters annotations.

bpo-15944 [https://bugs.python.org/issue15944]: memoryview: Allow arbitrary formats when casting to bytes. Patch by Martin Panter.

库

bpo-23441 [https://bugs.python.org/issue23441]: rcompleter now prints a tab character instead of displaying possible completions for an empty word. Initial patch by Martin Sekera.

bpo-24683 [https://bugs.python.org/issue24683]: Fixed crashes in _json functions called with arguments of inappropriate type.

bpo-21697 [https://bugs.python.org/issue21697]: shutil.copytree() now correctly handles symbolic links that point to directories. Patch by Eduardo Seabra and Thomas Kluyver.

bpo-14373 [https://bugs.python.org/issue14373]: Fixed segmentation fault when gc.collect() is called during constructing lru_cache (C implementation).

bpo-24695 [https://bugs.python.org/issue24695]: Fix a regression in traceback.print_exception(). If exc_traceback is None we shouldn’t print a traceback header like described in the documentation.

bpo-24620 [https://bugs.python.org/issue24620]: Random.setstate() now validates the value of state last element.

bpo-22485 [https://bugs.python.org/issue22485]: Fixed an issue that caused inspect.getsource to return incorrect results on nested functions.

bpo-22153 [https://bugs.python.org/issue22153]: Improve unittest docs. Patch from Martin Panter and evilzero.

bpo-24580 [https://bugs.python.org/issue24580]: Symbolic group references to open group in re patterns now are explicitly forbidden as well as numeric group references.

bpo-24206 [https://bugs.python.org/issue24206]: Fixed __eq__ and __ne__ methods of inspect classes.

bpo-24631 [https://bugs.python.org/issue24631]: Fixed regression in the timeit module with multiline setup.

bpo-18622 [https://bugs.python.org/issue18622]: unittest.mock.mock_open().reset_mock would recurse infinitely. Patch from Nicola Palumbo and Laurent De Buyst.

bpo-23661 [https://bugs.python.org/issue23661]: unittest.mock side_effects can now be exceptions again. This was a regression vs Python 3.4. Patch from Ignacio Rossi

bpo-24608 [https://bugs.python.org/issue24608]: chunk.Chunk.read() now always returns bytes, not str.

bpo-18684 [https://bugs.python.org/issue18684]: Fixed reading out of the buffer in the re module.

bpo-24259 [https://bugs.python.org/issue24259]: tarfile now raises a ReadError if an archive is truncated inside a data segment.

bpo-15014 [https://bugs.python.org/issue15014]: SMTP.auth() and SMTP.login() now support RFC 4954’s optional initial-response argument to the SMTP AUTH command.

bpo-24669 [https://bugs.python.org/issue24669]: Fix inspect.getsource() for ‘async def’ functions. Patch by Kai Groner.

bpo-24688 [https://bugs.python.org/issue24688]: ast.get_docstring() for ‘async def’ functions.

构建

bpo-24603 [https://bugs.python.org/issue24603]: 更新Windows 构建和OS X 10.5 安装程序以使用OpenSSL 1.0.2d.

Python 3.5.0 beta 3

发布日期: 2015-07-05

核心与内置

bpo-24467 [https://bugs.python.org/issue24467]: Fixed possible buffer over-read in bytearray. The bytearray object now always allocates place for trailing null byte and it’s buffer now is always null-terminated.

Upgrade to Unicode 8.0.0.

bpo-24345 [https://bugs.python.org/issue24345]: Add Py_tp_finalize slot for the stable ABI.

bpo-24400 [https://bugs.python.org/issue24400]: Introduce a distinct type for PEP 492 coroutines; add types.CoroutineType, inspect.getcoroutinestate, inspect.getcoroutinelocals; coroutines no longer use CO_GENERATOR flag; sys.set_coroutine_wrapper works only for ‘async def’ coroutines; inspect.iscoroutine no longer uses collections.abc.Coroutine, it’s intended to test for pure ‘async def’ coroutines only; add new opcode: GET_YIELD_FROM_ITER; fix generators wrapper used in types.coroutine to be instance of collections.abc.Generator; collections.abc.Awaitable and collections.abc.Coroutine can no longer be used to detect generator-based coroutines–use inspect.isawaitable instead.

bpo-24450 [https://bugs.python.org/issue24450]: Add gi_yieldfrom to generators and cr_await to coroutines. Contributed by Benno Leslie and Yury Selivanov.

bpo-19235 [https://bugs.python.org/issue19235]: Add new RecursionError exception. Patch by Georg Brandl.

库

bpo-21750 [https://bugs.python.org/issue21750]: mock_open.read_data can now be read from each instance, as it could in Python 3.3.

bpo-24552 [https://bugs.python.org/issue24552]: Fix use after free in an error case of the _pickle module.

bpo-24514 [https://bugs.python.org/issue24514]: tarfile now tolerates number fields consisting of only whitespace.

bpo-19176 [https://bugs.python.org/issue19176]: Fixed doctype() related bugs in C implementation of ElementTree. A deprecation warning no longer issued by XMLParser subclass with default doctype() method. Direct call of doctype() now issues a warning. Parser’s doctype() now is not called if target’s doctype() is called. Based on patch by Martin Panter.

bpo-20387 [https://bugs.python.org/issue20387]: Restore semantic round-trip correctness in tokenize/untokenize for tab-indented blocks.

bpo-24456 [https://bugs.python.org/issue24456]: Fixed possible buffer over-read in adpcm2lin() and lin2adpcm() functions of the audioop module.

bpo-24336 [https://bugs.python.org/issue24336]: The contextmanager decorator now works with functions with keyword arguments called “func” and “self”. Patch by Martin Panter.

bpo-24522 [https://bugs.python.org/issue24522]: Fix possible integer overflow in json accelerator module.

bpo-24489 [https://bugs.python.org/issue24489]: ensure a previously set C errno doesn’t disturb cmath.polar().

bpo-24408 [https://bugs.python.org/issue24408]: Fixed AttributeError in measure() and metrics() methods of tkinter.Font.

bpo-14373 [https://bugs.python.org/issue14373]: C implementation of functools.lru_cache() now can be used with methods.

bpo-24347 [https://bugs.python.org/issue24347]: Set KeyError if PyDict_GetItemWithError returns NULL.

bpo-24348 [https://bugs.python.org/issue24348]: Drop superfluous incref/decref.

bpo-24359 [https://bugs.python.org/issue24359]: Check for changed OrderedDict size during iteration.

bpo-24368 [https://bugs.python.org/issue24368]: Support keyword arguments in OrderedDict methods.

bpo-24362 [https://bugs.python.org/issue24362]: Simplify the C OrderedDict fast nodes resize logic.

bpo-24377 [https://bugs.python.org/issue24377]: Fix a ref leak in OrderedDict.__repr__.

bpo-24369 [https://bugs.python.org/issue24369]: Defend against key-changes during iteration.

测试

bpo-24373 [https://bugs.python.org/issue24373]: _testmultiphase and xxlimited now use tp_traverse and tp_finalize to avoid reference leaks encountered when combining tp_dealloc with PyType_FromSpec (see bpo-16690 [https://bugs.python.org/issue16690] for details)

文档

bpo-24458 [https://bugs.python.org/issue24458]: Update documentation to cover multi-phase initialization for extension modules (PEP 489). Patch by Petr Viktorin.

bpo-24351 [https://bugs.python.org/issue24351]: Clarify what is meant by “identifier” in the context of string.Template instances.

构建

bpo-24432 [https://bugs.python.org/issue24432]: 更新Windows 构建和OS X 10.5 安装程序以使用OpenSSL 1.0.2c.

Python 3.5.0 beta 2

发布日期: 2015-05-31

核心与内置

bpo-24284 [https://bugs.python.org/issue24284]: The startswith and endswith methods of the str class no longer return True when finding the empty string and the indexes are completely out of range.

bpo-24115 [https://bugs.python.org/issue24115]: Update uses of PyObject_IsTrue(), PyObject_Not(), PyObject_IsInstance(), PyObject_RichCompareBool() and _PyDict_Contains() to check for and handle errors correctly.

bpo-24328 [https://bugs.python.org/issue24328]: Fix importing one character extension modules.

bpo-11205 [https://bugs.python.org/issue11205]: In dictionary displays, evaluate the key before the value.

bpo-24285 [https://bugs.python.org/issue24285]: Fixed regression that prevented importing extension modules from inside packages. Patch by Petr Viktorin.

库

bpo-23247 [https://bugs.python.org/issue23247]: Fix a crash in the StreamWriter.reset() of CJK codecs.

bpo-24270 [https://bugs.python.org/issue24270]: Add math.isclose() and cmath.isclose() functions as per PEP 485. Contributed by Chris Barker and Tal Einat.

bpo-5633 [https://bugs.python.org/issue5633]: Fixed timeit when the statement is a string and the setup is not.

bpo-24326 [https://bugs.python.org/issue24326]: Fixed audioop.ratecv() with non-default weightB argument. Original patch by David Moore.

bpo-16991 [https://bugs.python.org/issue16991]: Add a C implementation of OrderedDict.

bpo-23934 [https://bugs.python.org/issue23934]: Fix inspect.signature to fail correctly for builtin types lacking signature information. Initial patch by James Powell.

Python 3.5.0 beta 1

发布日期: 2015-05-24

核心与内置

bpo-24276 [https://bugs.python.org/issue24276]: Fixed optimization of property descriptor getter.

bpo-24268 [https://bugs.python.org/issue24268]: PEP 489: Multi-phase extension module initialization. Patch by Petr Viktorin.

bpo-23955 [https://bugs.python.org/issue23955]: Add pyvenv.cfg option to suppress registry/environment lookup for generating sys.path on Windows.

bpo-24257 [https://bugs.python.org/issue24257]: Fixed system error in the comparison of faked types.SimpleNamespace.

bpo-22939 [https://bugs.python.org/issue22939]: Fixed integer overflow in iterator object. Patch by Clement Rouault.

bpo-23985 [https://bugs.python.org/issue23985]: Fix a possible buffer overrun when deleting a slice from the front of a bytearray and then appending some other bytes data.

bpo-24102 [https://bugs.python.org/issue24102]: Fixed exception type checking in standard error handlers.

bpo-15027 [https://bugs.python.org/issue15027]: The UTF-32 encoder is now 3x to 7x faster.

bpo-23290 [https://bugs.python.org/issue23290]: Optimize set_merge() for cases where the target is empty. (Contributed by Serhiy Storchaka.)

bpo-2292 [https://bugs.python.org/issue2292]: PEP 448: Additional Unpacking Generalizations.

bpo-24096 [https://bugs.python.org/issue24096]: Make warnings.warn_explicit more robust against mutation of the warnings.filters list.

bpo-23996 [https://bugs.python.org/issue23996]: Avoid a crash when a delegated generator raises an unnormalized StopIteration exception. Patch by Stefan Behnel.

bpo-23910 [https://bugs.python.org/issue23910]: Optimize property() getter calls. Patch by Joe Jevnik.

bpo-23911 [https://bugs.python.org/issue23911]: Move path-based importlib bootstrap code to a separate frozen module.

bpo-24192 [https://bugs.python.org/issue24192]: Fix namespace package imports.

bpo-24022 [https://bugs.python.org/issue24022]: Fix tokenizer crash when processing undecodable source code.

bpo-9951 [https://bugs.python.org/issue9951]: Added a hex() method to bytes, bytearray, and memoryview.

bpo-22906 [https://bugs.python.org/issue22906]: PEP 479: Change StopIteration handling inside generators.

bpo-24017 [https://bugs.python.org/issue24017]: PEP 492: Coroutines with async and await syntax.

库

bpo-14373 [https://bugs.python.org/issue14373]: Added C implementation of functools.lru_cache(). Based on patches by Matt Joiner and Alexey Kachayev.

bpo-24230 [https://bugs.python.org/issue24230]: The tempfile module now accepts bytes for prefix, suffix and dir parameters and returns bytes in such situations (matching the os module APIs).

bpo-22189 [https://bugs.python.org/issue22189]: collections.UserString now supports __getnewargs__(), __rmod__(), casefold(), format_map(), isprintable(), and maketrans(). Patch by Joe Jevnik.

bpo-24244 [https://bugs.python.org/issue24244]: Prevents termination when an invalid format string is encountered on Windows in strftime.

bpo-23973 [https://bugs.python.org/issue23973]: PEP 484: Add the typing module.

bpo-23086 [https://bugs.python.org/issue23086]: The collections.abc.Sequence() abstract base class added start and stop parameters to the index() mixin. Patch by Devin Jeanpierre.

bpo-20035 [https://bugs.python.org/issue20035]: Replaced the tkinter._fix module used for setting up the Tcl/Tk environment on Windows with a private function in the _tkinter module that makes no permanent changes to the environment.

bpo-24257 [https://bugs.python.org/issue24257]: Fixed segmentation fault in sqlite3.Row constructor with faked cursor type.

bpo-15836 [https://bugs.python.org/issue15836]: assertRaises(), assertRaisesRegex(), assertWarns() and assertWarnsRegex() assertments now check the type of the first argument to prevent possible user error. Based on patch by Daniel Wagner-Hall.

bpo-9858 [https://bugs.python.org/issue9858]: Add missing method stubs to _io.RawIOBase. Patch by Laura Rupprecht.

bpo-22955 [https://bugs.python.org/issue22955]: attrgetter, itemgetter and methodcaller objects in the operator module now support pickling. Added readable and evaluable repr for these objects. Based on patch by Josh Rosenberg.

bpo-22107 [https://bugs.python.org/issue22107]: tempfile.gettempdir() and tempfile.mkdtemp() now try again when a directory with the chosen name already exists on Windows as well as on Unix. tempfile.mkstemp() now fails early if parent directory is not valid (not exists or is a file) on Windows.

bpo-23780 [https://bugs.python.org/issue23780]: Improved error message in os.path.join() with single argument.

bpo-6598 [https://bugs.python.org/issue6598]: Increased time precision and random number range in email.utils.make_msgid() to strengthen the uniqueness of the message ID.

bpo-24091 [https://bugs.python.org/issue24091]: Fixed various crashes in corner cases in C implementation of ElementTree.

bpo-21931 [https://bugs.python.org/issue21931]: msilib.FCICreate() now raises TypeError in the case of a bad argument instead of a ValueError with a bogus FCI error number. Patch by Jeffrey Armstrong.

bpo-13866 [https://bugs.python.org/issue13866]: quote_via argument added to urllib.parse.urlencode.

bpo-20098 [https://bugs.python.org/issue20098]: New mangle_from policy option for email, default True for compat32, but False for all other policies.

bpo-24211 [https://bugs.python.org/issue24211]: The email library now supports RFC 6532: it can generate headers using utf-8 instead of encoded words.

bpo-16314 [https://bugs.python.org/issue16314]: Added support for the LZMA compression in distutils.

bpo-21804 [https://bugs.python.org/issue21804]: poplib now supports RFC 6856 (UTF8).

bpo-18682 [https://bugs.python.org/issue18682]: Optimized pprint functions for builtin scalar types.

bpo-22027 [https://bugs.python.org/issue22027]: smtplib now supports RFC 6531 (SMTPUTF8).

bpo-23488 [https://bugs.python.org/issue23488]: Random generator objects now consume 2x less memory on 64-bit.

bpo-1322 [https://bugs.python.org/issue1322]: platform.dist() and platform.linux_distribution() functions are now deprecated. Initial patch by Vajrasky Kok.

bpo-22486 [https://bugs.python.org/issue22486]: Added the math.gcd() function. The fractions.gcd() function now is deprecated. Based on patch by Mark Dickinson.

bpo-24064 [https://bugs.python.org/issue24064]: Property() docstrings are now writeable. (Patch by Berker Peksag.)

bpo-22681 [https://bugs.python.org/issue22681]: Added support for the koi8_t encoding.

bpo-22682 [https://bugs.python.org/issue22682]: Added support for the kz1048 encoding.

bpo-23796 [https://bugs.python.org/issue23796]: peek and read1 methods of BufferedReader now raise ValueError if they called on a closed object. Patch by John Hergenroeder.

bpo-21795 [https://bugs.python.org/issue21795]: smtpd now supports the 8BITMIME extension whenever the new decode_data constructor argument is set to False.

bpo-24155 [https://bugs.python.org/issue24155]: optimize heapq.heapify() for better cache performance when heapifying large lists.

bpo-21800 [https://bugs.python.org/issue21800]: imaplib now supports RFC 5161 (enable), RFC 6855 (utf8/internationalized email) and automatically encodes non-ASCII usernames and passwords to UTF8.

bpo-20274 [https://bugs.python.org/issue20274]: When calling a _sqlite.Connection, it now complains if passed any keyword arguments. Previously it silently ignored them.

bpo-20274 [https://bugs.python.org/issue20274]: Remove ignored and erroneous “kwargs” parameters from three METH_VARARGS methods on _sqlite.Connection.

bpo-24134 [https://bugs.python.org/issue24134]: assertRaises(), assertRaisesRegex(), assertWarns() and assertWarnsRegex() checks now emits a deprecation warning when callable is None or keyword arguments except msg is passed in the context manager mode.

bpo-24018 [https://bugs.python.org/issue24018]: Add a collections.abc.Generator abstract base class. Contributed by Stefan Behnel.

bpo-23880 [https://bugs.python.org/issue23880]: Tkinter’s getint() and getdouble() now support Tcl_Obj. Tkinter’s getdouble() now supports any numbers (in particular int).

bpo-22619 [https://bugs.python.org/issue22619]: Added negative limit support in the traceback module. Based on patch by Dmitry Kazakov.

bpo-24094 [https://bugs.python.org/issue24094]: Fix possible crash in json.encode with poorly behaved dict subclasses.

bpo-9246 [https://bugs.python.org/issue9246]: On POSIX, os.getcwd() now supports paths longer than 1025 bytes. Patch written by William Orr.

bpo-17445 [https://bugs.python.org/issue17445]: add difflib.diff_bytes() to support comparison of byte strings (fixes a regression from Python 2).

bpo-23917 [https://bugs.python.org/issue23917]: Fall back to sequential compilation when ProcessPoolExecutor doesn’t exist. Patch by Claudiu Popa.

bpo-23008 [https://bugs.python.org/issue23008]: Fixed resolving attributes with boolean value is False in pydoc.

Fix asyncio issue 235: LifoQueue and PriorityQueue’s put didn’t increment unfinished tasks (this bug was introduced when JoinableQueue was merged with Queue).

bpo-23908 [https://bugs.python.org/issue23908]: os functions now reject paths with embedded null character on Windows instead of silently truncating them.

bpo-23728 [https://bugs.python.org/issue23728]: binascii.crc_hqx() could return an integer outside of the range 0-0xffff for empty data.

bpo-23887 [https://bugs.python.org/issue23887]: urllib.error.HTTPError now has a proper repr() representation. Patch by Berker Peksag.

asyncio: New event loop APIs: set_task_factory() and get_task_factory().

asyncio: async() function is deprecated in favour of ensure_future().

bpo-24178 [https://bugs.python.org/issue24178]: asyncio.Lock, Condition, Semaphore, and BoundedSemaphore support new ‘async with’ syntax. Contributed by Yury Selivanov.

bpo-24179 [https://bugs.python.org/issue24179]: Support ‘async for’ for asyncio.StreamReader. Contributed by Yury Selivanov.

bpo-24184 [https://bugs.python.org/issue24184]: Add AsyncIterator and AsyncIterable ABCs to collections.abc. Contributed by Yury Selivanov.

bpo-22547 [https://bugs.python.org/issue22547]: Implement informative __repr__ for inspect.BoundArguments. Contributed by Yury Selivanov.

bpo-24190 [https://bugs.python.org/issue24190]: Implement inspect.BoundArgument.apply_defaults() method. Contributed by Yury Selivanov.

bpo-20691 [https://bugs.python.org/issue20691]: Add ‘follow_wrapped’ argument to inspect.Signature.from_callable() and inspect.signature(). Contributed by Yury Selivanov.

bpo-24248 [https://bugs.python.org/issue24248]: Deprecate inspect.Signature.from_function() and inspect.Signature.from_builtin().

bpo-23898 [https://bugs.python.org/issue23898]: Fix inspect.classify_class_attrs() to support attributes with overloaded __eq__ and __bool__. Patch by Mike Bayer.

bpo-24298 [https://bugs.python.org/issue24298]: Fix inspect.signature() to correctly unwrap wrappers around bound methods.

IDLE

bpo-23184 [https://bugs.python.org/issue23184]: remove unused names and imports in idlelib. Initial patch by Al Sweigart.

测试

bpo-21520 [https://bugs.python.org/issue21520]: test_zipfile no longer fails if the word ‘bad’ appears anywhere in the name of the current directory.

bpo-9517 [https://bugs.python.org/issue9517]: Move script_helper into the support package. Patch by Christie Wilson.

文档

bpo-22155 [https://bugs.python.org/issue22155]: Add File Handlers subsection with createfilehandler to tkinter doc. Remove obsolete example from FAQ. Patch by Martin Panter.

bpo-24029 [https://bugs.python.org/issue24029]: Document the name binding behavior for submodule imports.

bpo-24077 [https://bugs.python.org/issue24077]: Fix typo in man page for -I command option: -s, not -S

工具/示例

bpo-24000 [https://bugs.python.org/issue24000]: Improved Argument Clinic’s mapping of converters to legacy “format units”. Updated the documentation to match.

bpo-24001 [https://bugs.python.org/issue24001]: Argument Clinic converters now use accept={type} instead of types={‘type’} to specify the types the converter accepts.

bpo-23330 [https://bugs.python.org/issue23330]: h2py now supports arbitrary filenames in #include.

bpo-24031 [https://bugs.python.org/issue24031]: make patchcheck now supports git checkouts, too.

Python 3.5.0 alpha 4

发布日期: 2015-04-19

核心与内置

bpo-22980 [https://bugs.python.org/issue22980]: Under Linux, GNU/KFreeBSD and the Hurd, C extensions now include the architecture triplet in the extension name, to make it easy to test builds for different ABIs in the same working tree. Under OS X, the extension name now includes PEP 3149-style information.

bpo-22631 [https://bugs.python.org/issue22631]: Added Linux-specific socket constant CAN_RAW_FD_FRAMES. Patch courtesy of Joe Jevnik.

bpo-23731 [https://bugs.python.org/issue23731]: Implement PEP 488: removal of .pyo files.

bpo-23726 [https://bugs.python.org/issue23726]: Don’t enable GC for user subclasses of non-GC types that don’t add any new fields. Patch by Eugene Toder.

bpo-23309 [https://bugs.python.org/issue23309]: Avoid a deadlock at shutdown if a daemon thread is aborted while it is holding a lock to a buffered I/O object, and the main thread tries to use the same I/O object (typically stdout or stderr). A fatal error is emitted instead.

bpo-22977 [https://bugs.python.org/issue22977]: Fixed formatting Windows error messages on Wine. Patch by Martin Panter.

bpo-23466 [https://bugs.python.org/issue23466]: %c, %o, %x, and %X in bytes formatting now raise TypeError on non-integer input.

bpo-24044 [https://bugs.python.org/issue24044]: Fix possible null pointer dereference in list.sort in out of memory conditions.

bpo-21354 [https://bugs.python.org/issue21354]: PyCFunction_New function is exposed by python DLL again.

库

bpo-23840 [https://bugs.python.org/issue23840]: tokenize.open() now closes the temporary binary file on error to fix a resource warning.

bpo-16914 [https://bugs.python.org/issue16914]: new debuglevel 2 in smtplib adds timestamps to debug output.

bpo-7159 [https://bugs.python.org/issue7159]: urllib.request now supports sending auth credentials automatically after the first 401. This enhancement is a superset of the enhancement from bpo-19494 [https://bugs.python.org/issue19494] and supersedes that change.

bpo-23703 [https://bugs.python.org/issue23703]: Fix a regression in urljoin() introduced in 901e4e52b20a. Patch by Demian Brecht.

bpo-4254 [https://bugs.python.org/issue4254]: Adds _curses.update_lines_cols(). Patch by Arnon Yaari

bpo-19933 [https://bugs.python.org/issue19933]: Provide default argument for ndigits in round. Patch by Vajrasky Kok.

bpo-23193 [https://bugs.python.org/issue23193]: Add a numeric_owner parameter to tarfile.TarFile.extract and tarfile.TarFile.extractall. Patch by Michael Vogt and Eric Smith.

bpo-23342 [https://bugs.python.org/issue23342]: Add a subprocess.run() function than returns a CalledProcess instance for a more consistent API than the existing call* functions.

bpo-21217 [https://bugs.python.org/issue21217]: inspect.getsourcelines() now tries to compute the start and end lines from the code object, fixing an issue when a lambda function is used as decorator argument. Patch by Thomas Ballinger and Allison Kaptur.

bpo-24521 [https://bugs.python.org/issue24521]: Fix possible integer overflows in the pickle module.

bpo-22931 [https://bugs.python.org/issue22931]: Allow ‘[‘ and ‘]’ in cookie values.

The keywords attribute of functools.partial is now always a dictionary.

bpo-23811 [https://bugs.python.org/issue23811]: Add missing newline to the PyCompileError error message. Patch by Alex Shkop.

bpo-21116 [https://bugs.python.org/issue21116]: Avoid blowing memory when allocating a multiprocessing shared array that’s larger than 50% of the available RAM. Patch by Médéric Boquien.

bpo-22982 [https://bugs.python.org/issue22982]: Improve BOM handling when seeking to multiple positions of a writable text file.

bpo-23464 [https://bugs.python.org/issue23464]: Removed deprecated asyncio JoinableQueue.

bpo-23529 [https://bugs.python.org/issue23529]: Limit the size of decompressed data when reading from GzipFile, BZ2File or LZMAFile. This defeats denial of service attacks using compressed bombs (i.e. compressed payloads which decompress to a huge size). Patch by Martin Panter and Nikolaus Rath.

bpo-21859 [https://bugs.python.org/issue21859]: Added Python implementation of io.FileIO.

bpo-23865 [https://bugs.python.org/issue23865]: close() methods in multiple modules now are idempotent and more robust at shutdown. If they need to release multiple resources, all are released even if errors occur.

bpo-23400 [https://bugs.python.org/issue23400]: Raise same exception on both Python 2 and 3 if sem_open is not available. Patch by Davin Potts.

bpo-10838 [https://bugs.python.org/issue10838]: The subprocess now module includes SubprocessError and TimeoutError in its list of exported names for the users wild enough to use from subprocess import *.

bpo-23411 [https://bugs.python.org/issue23411]: Added DefragResult, ParseResult, SplitResult, DefragResultBytes, ParseResultBytes, and SplitResultBytes to urllib.parse.__all__. Patch by Martin Panter.

bpo-23881 [https://bugs.python.org/issue23881]: urllib.request.ftpwrapper constructor now closes the socket if the FTP connection failed to fix a ResourceWarning.

bpo-23853 [https://bugs.python.org/issue23853]: socket.socket.sendall() does no more reset the socket timeout each time data is sent successfully. The socket timeout is now the maximum total duration to send all data.

bpo-22721 [https://bugs.python.org/issue22721]: An order of multiline pprint output of set or dict containing orderable and non-orderable elements no longer depends on iteration order of set or dict.

bpo-15133 [https://bugs.python.org/issue15133]: _tkinter.tkapp.getboolean() now supports Tcl_Obj and always returns bool. tkinter.BooleanVar now validates input values (accepted bool, int, str, and Tcl_Obj). tkinter.BooleanVar.get() now always returns bool.

bpo-10590 [https://bugs.python.org/issue10590]: xml.sax.parseString() now supports string argument.

bpo-23338 [https://bugs.python.org/issue23338]: Fixed formatting ctypes error messages on Cygwin. Patch by Makoto Kato.

bpo-15582 [https://bugs.python.org/issue15582]: inspect.getdoc() now follows inheritance chains.

bpo-2175 [https://bugs.python.org/issue2175]: SAX parsers now support a character stream of InputSource object.

bpo-16840 [https://bugs.python.org/issue16840]: Tkinter now supports 64-bit integers added in Tcl 8.4 and arbitrary precision integers added in Tcl 8.5.

bpo-23834 [https://bugs.python.org/issue23834]: Fix socket.sendto(), use the C Py_ssize_t type to store the result of sendto() instead of the C int type.

bpo-23618 [https://bugs.python.org/issue23618]: socket.socket.connect() now waits until the connection completes instead of raising InterruptedError if the connection is interrupted by signals, signal handlers don’t raise an exception and the socket is blocking or has a timeout. socket.socket.connect() still raise InterruptedError for non-blocking sockets.

bpo-21526 [https://bugs.python.org/issue21526]: Tkinter now supports new boolean type in Tcl 8.5.

bpo-23836 [https://bugs.python.org/issue23836]: Fix the faulthandler module to handle reentrant calls to its signal handlers.

bpo-23838 [https://bugs.python.org/issue23838]: linecache now clears the cache and returns an empty result on MemoryError.

bpo-10395 [https://bugs.python.org/issue10395]: Added os.path.commonpath(). Implemented in posixpath and ntpath. Based on patch by Rafik Draoui.

bpo-23611 [https://bugs.python.org/issue23611]: Serializing more “lookupable” objects (such as unbound methods or nested classes) now are supported with pickle protocols < 4.

bpo-13583 [https://bugs.python.org/issue13583]: sqlite3.Row now supports slice indexing.

bpo-18473 [https://bugs.python.org/issue18473]: Fixed 2to3 and 3to2 compatible pickle mappings. Fixed ambiguous reverse mappings. Added many new mappings. Import mapping is no longer applied to modules already mapped with full name mapping.

bpo-23485 [https://bugs.python.org/issue23485]: select.select() is now retried automatically with the recomputed timeout when interrupted by a signal, except if the signal handler raises an exception. This change is part of the PEP 475.

bpo-23752 [https://bugs.python.org/issue23752]: When built from an existing file descriptor, io.FileIO() now only calls fstat() once. Before fstat() was called twice, which was not necessary.

bpo-23704 [https://bugs.python.org/issue23704]: collections.deque() objects now support __add__, __mul__, and __imul__().

bpo-23171 [https://bugs.python.org/issue23171]: csv.Writer.writerow() now supports arbitrary iterables.

bpo-23745 [https://bugs.python.org/issue23745]: The new email header parser now handles duplicate MIME parameter names without error, similar to how get_param behaves.

bpo-22117 [https://bugs.python.org/issue22117]: Fix os.utime(), it now rounds the timestamp towards minus infinity (-inf) instead of rounding towards zero.

bpo-23310 [https://bugs.python.org/issue23310]: Fix MagicMock’s initializer to work with __methods__, just like configure_mock(). Patch by Kasia Jachim.

构建

bpo-23817 [https://bugs.python.org/issue23817]: FreeBSD now uses “1.0” in the SOVERSION as other operating systems, instead of just “1”.

bpo-23501 [https://bugs.python.org/issue23501]: Argument Clinic now generates code into separate files by default.

测试

bpo-23799 [https://bugs.python.org/issue23799]: Added test.support.start_threads() for running and cleaning up multiple threads.

bpo-22390 [https://bugs.python.org/issue22390]: test.regrtest now emits a warning if temporary files or directories are left after running a test.

工具/示例

bpo-18128 [https://bugs.python.org/issue18128]: pygettext now uses standard +NNNN format in the POT-Creation-Date header.

bpo-23935 [https://bugs.python.org/issue23935]: Argument Clinic’s understanding of format units accepting bytes, bytearrays, and buffers is now consistent with both the documentation and the implementation.

bpo-23944 [https://bugs.python.org/issue23944]: Argument Clinic now wraps long impl prototypes at column 78.

bpo-20586 [https://bugs.python.org/issue20586]: Argument Clinic now ensures that functions without docstrings have signatures.

bpo-23492 [https://bugs.python.org/issue23492]: Argument Clinic now generates argument parsing code with PyArg_Parse instead of PyArg_ParseTuple if possible.

bpo-23500 [https://bugs.python.org/issue23500]: Argument Clinic is now smarter about generating the “#ifndef” (empty) definition of the methoddef macro: it’s only generated once, even if Argument Clinic processes the same symbol multiple times, and it’s emitted at the end of all processing rather than immediately after the first use.

C API

bpo-23998 [https://bugs.python.org/issue23998]: PyImport_ReInitLock() now checks for lock allocation error

Python 3.5.0 alpha 3

发布日期: 2015-03-28

核心与内置

bpo-23573 [https://bugs.python.org/issue23573]: Increased performance of string search operations (str.find, str.index, str.count, the in operator, str.split, str.partition) with arguments of different kinds (UCS1, UCS2, UCS4).

bpo-23753 [https://bugs.python.org/issue23753]: Python doesn’t support anymore platforms without stat() or fstat(), these functions are always required.

bpo-23681 [https://bugs.python.org/issue23681]: The -b option now affects comparisons of bytes with int.

bpo-23632 [https://bugs.python.org/issue23632]: Memoryviews now allow tuple indexing (including for multi-dimensional memoryviews).

bpo-23192 [https://bugs.python.org/issue23192]: Fixed generator lambdas. Patch by Bruno Cauet.

bpo-23629 [https://bugs.python.org/issue23629]: Fix the default __sizeof__ implementation for variable-sized objects.

库

bpo-14260 [https://bugs.python.org/issue14260]: The groupindex attribute of regular expression pattern object now is non-modifiable mapping.

bpo-23792 [https://bugs.python.org/issue23792]: Ignore KeyboardInterrupt when the pydoc pager is active. This mimics the behavior of the standard unix pagers, and prevents pipepager from shutting down while the pager itself is still running.

bpo-23775 [https://bugs.python.org/issue23775]: pprint() of OrderedDict now outputs the same representation as repr().

bpo-23765 [https://bugs.python.org/issue23765]: Removed IsBadStringPtr calls in ctypes

bpo-22364 [https://bugs.python.org/issue22364]: Improved some re error messages using regex for hints.

bpo-23742 [https://bugs.python.org/issue23742]: ntpath.expandvars() no longer loses unbalanced single quotes.

bpo-21717 [https://bugs.python.org/issue21717]: The zipfile.ZipFile.open function now supports ‘x’ (exclusive creation) mode.

bpo-21802 [https://bugs.python.org/issue21802]: The reader in BufferedRWPair now is closed even when closing writer failed in BufferedRWPair.close().

bpo-23622 [https://bugs.python.org/issue23622]: Unknown escapes in regular expressions that consist of '\' and ASCII letter now raise a deprecation warning and will be forbidden in Python 3.6.

bpo-23671 [https://bugs.python.org/issue23671]: string.Template now allows specifying the “self” parameter as a keyword argument. string.Formatter now allows specifying the “self” and the “format_string” parameters as keyword arguments.

bpo-23502 [https://bugs.python.org/issue23502]: The pprint module now supports mapping proxies.

bpo-17530 [https://bugs.python.org/issue17530]: pprint now wraps long bytes objects and bytearrays.

bpo-22687 [https://bugs.python.org/issue22687]: Fixed some corner cases in breaking words in tetxtwrap. Got rid of quadratic complexity in breaking long words.

bpo-4727 [https://bugs.python.org/issue4727]: The copy module now uses pickle protocol 4 (PEP 3154) and supports copying of instances of classes whose __new__ method takes keyword-only arguments.

bpo-23491 [https://bugs.python.org/issue23491]: Added a zipapp module to support creating executable zip file archives of Python code. Registered “.pyz” and “.pyzw” extensions on Windows for these archives (PEP 441).

bpo-23657 [https://bugs.python.org/issue23657]: Avoid explicit checks for str in zipapp, adding support for pathlib.Path objects as arguments.

bpo-23688 [https://bugs.python.org/issue23688]: Added support of arbitrary bytes-like objects and avoided unnecessary copying of memoryview in gzip.GzipFile.write(). Original patch by Wolfgang Maier.

bpo-23252 [https://bugs.python.org/issue23252]: Added support for writing ZIP files to unseekable streams.

bpo-23647 [https://bugs.python.org/issue23647]: Increase imaplib’s MAXLINE to accommodate modern mailbox sizes.

bpo-23539 [https://bugs.python.org/issue23539]: If body is None, http.client.HTTPConnection.request now sets Content-Length to 0 for PUT, POST, and PATCH headers to avoid 411 errors from some web servers.

bpo-22351 [https://bugs.python.org/issue22351]: The nntplib.NNTP constructor no longer leaves the connection and socket open until the garbage collector cleans them up. Patch by Martin Panter.

bpo-23704 [https://bugs.python.org/issue23704]: collections.deque() objects now support methods for index(), insert(), and copy(). This allows deques to be registered as a MutableSequence and it improves their substitutability for lists.

bpo-23715 [https://bugs.python.org/issue23715]: signal.sigwaitinfo() and signal.sigtimedwait() are now retried when interrupted by a signal not in the sigset parameter, if the signal handler does not raise an exception. signal.sigtimedwait() recomputes the timeout with a monotonic clock when it is retried.

bpo-23001 [https://bugs.python.org/issue23001]: Few functions in modules mmap, ossaudiodev, socket, ssl, and codecs, that accepted only read-only bytes-like object now accept writable bytes-like object too.

bpo-23646 [https://bugs.python.org/issue23646]: If time.sleep() is interrupted by a signal, the sleep is now retried with the recomputed delay, except if the signal handler raises an exception (PEP 475).

bpo-23136 [https://bugs.python.org/issue23136]: _strptime now uniformly handles all days in week 0, including Dec 30 of previous year. Based on patch by Jim Carroll.

bpo-23700 [https://bugs.python.org/issue23700]: Iterator of NamedTemporaryFile now keeps a reference to NamedTemporaryFile instance. Patch by Bohuslav Kabrda.

bpo-22903 [https://bugs.python.org/issue22903]: The fake test case created by unittest.loader when it fails importing a test module is now picklable.

bpo-22181 [https://bugs.python.org/issue22181]: On Linux, os.urandom() now uses the new getrandom() syscall if available, syscall introduced in the Linux kernel 3.17. It is more reliable and more secure, because it avoids the need of a file descriptor and waits until the kernel has enough entropy.

bpo-2211 [https://bugs.python.org/issue2211]: Updated the implementation of the http.cookies.Morsel class. Setting attributes key, value and coded_value directly now is deprecated. update() and setdefault() now transform and check keys. Comparing for equality now takes into account attributes key, value and coded_value. copy() now returns a Morsel, not a dict. repr() now contains all attributes. Optimized checking keys and quoting values. Added new tests. Original patch by Demian Brecht.

bpo-18983 [https://bugs.python.org/issue18983]: Allow selection of output units in timeit. Patch by Julian Gindi.

bpo-23631 [https://bugs.python.org/issue23631]: Fix traceback.format_list when a traceback has been mutated.

bpo-23568 [https://bugs.python.org/issue23568]: Add rdivmod support to MagicMock() objects. Patch by Håkan Lövdahl.

bpo-2052 [https://bugs.python.org/issue2052]: Add charset parameter to HtmlDiff.make_file().

bpo-23668 [https://bugs.python.org/issue23668]: Support os.truncate and os.ftruncate on Windows.

bpo-23138 [https://bugs.python.org/issue23138]: Fixed parsing cookies with absent keys or values in cookiejar. Patch by Demian Brecht.

bpo-23051 [https://bugs.python.org/issue23051]: multiprocessing.Pool methods imap() and imap_unordered() now handle exceptions raised by an iterator. Patch by Alon Diamant and Davin Potts.

bpo-23581 [https://bugs.python.org/issue23581]: Add matmul support to MagicMock. Patch by Håkan Lövdahl.

bpo-23566 [https://bugs.python.org/issue23566]: enable(), register(), dump_traceback() and dump_traceback_later() functions of faulthandler now accept file descriptors. Patch by Wei Wu.

bpo-22928 [https://bugs.python.org/issue22928]: Disabled HTTP header injections in http.client. Original patch by Demian Brecht.

bpo-23615 [https://bugs.python.org/issue23615]: Modules bz2, tarfile and tokenize now can be reloaded with imp.reload(). Patch by Thomas Kluyver.

bpo-23605 [https://bugs.python.org/issue23605]: os.walk() now calls os.scandir() instead of os.listdir(). The usage of os.scandir() reduces the number of calls to os.stat(). Initial patch written by Ben Hoyt.

构建

bpo-23585 [https://bugs.python.org/issue23585]: make patchcheck will ensure the interpreter is built.

测试

bpo-23583 [https://bugs.python.org/issue23583]: Added tests for standard IO streams in IDLE.

bpo-22289 [https://bugs.python.org/issue22289]: Prevent test_urllib2net failures due to ftp connection timeout.

工具/示例

bpo-22826 [https://bugs.python.org/issue22826]: The result of open() in Tools/freeze/bkfile.py is now better compatible with regular files (in particular it now supports the context management protocol).

Python 3.5.0 alpha 2

发布日期: 2015-03-09

核心与内置

bpo-23571 [https://bugs.python.org/issue23571]: PyObject_Call() and PyCFunction_Call() now raise a SystemError if a function returns a result and raises an exception. The SystemError is chained to the previous exception.

库

bpo-22524 [https://bugs.python.org/issue22524]: New os.scandir() function, part of the PEP 471: “os.scandir() function – a better and faster directory iterator”. Patch written by Ben Hoyt.

bpo-23103 [https://bugs.python.org/issue23103]: Reduced the memory consumption of IPv4Address and IPv6Address.

bpo-21793 [https://bugs.python.org/issue21793]: BaseHTTPRequestHandler again logs response code as numeric, not as stringified enum. Patch by Demian Brecht.

bpo-23476 [https://bugs.python.org/issue23476]: In the ssl module, enable OpenSSL’s X509_V_FLAG_TRUSTED_FIRST flag on certificate stores when it is available.

bpo-23576 [https://bugs.python.org/issue23576]: Avoid stalling in SSL reads when EOF has been reached in the SSL layer but the underlying connection hasn’t been closed.

bpo-23504 [https://bugs.python.org/issue23504]: Added an __all__ to the types module.

bpo-23563 [https://bugs.python.org/issue23563]: Optimized utility functions in urllib.parse.

bpo-7830 [https://bugs.python.org/issue7830]: Flatten nested functools.partial.

bpo-20204 [https://bugs.python.org/issue20204]: Added the __module__ attribute to _tkinter classes.

bpo-19980 [https://bugs.python.org/issue19980]: Improved help() for non-recognized strings. help(‘’) now shows the help on str. help(‘help’) now shows the help on help(). Original patch by Mark Lawrence.

bpo-23521 [https://bugs.python.org/issue23521]: Corrected pure python implementation of timedelta division.

Eliminated OverflowError from timedelta * float for some floats; Corrected rounding in timedelta true division.

bpo-21619 [https://bugs.python.org/issue21619]: Popen objects no longer leave a zombie after exit in the with statement if the pipe was broken. Patch by Martin Panter.

bpo-22936 [https://bugs.python.org/issue22936]: Make it possible to show local variables in tracebacks for both the traceback module and unittest.

bpo-15955 [https://bugs.python.org/issue15955]: Add an option to limit the output size in bz2.decompress(). Patch by Nikolaus Rath.

bpo-6639 [https://bugs.python.org/issue6639]: Module-level turtle functions no longer raise TclError after closing the window.

bpo-814253 [https://bugs.python.org/issue814253]: Group references and conditional group references now work in lookbehind assertions in regular expressions. (See also: bpo-9179 [https://bugs.python.org/issue9179])

bpo-23215 [https://bugs.python.org/issue23215]: Multibyte codecs with custom error handlers that ignores errors consumed too much memory and raised SystemError or MemoryError. Original patch by Aleksi Torhamo.

bpo-5700 [https://bugs.python.org/issue5700]: io.FileIO() called flush() after closing the file. flush() was not called in close() if closefd=False.

bpo-23374 [https://bugs.python.org/issue23374]: Fixed pydoc failure with non-ASCII files when stdout encoding differs from file system encoding (e.g. on Mac OS).

bpo-23481 [https://bugs.python.org/issue23481]: Remove RC4 from the SSL module’s default cipher list.

bpo-21548 [https://bugs.python.org/issue21548]: Fix pydoc.synopsis() and pydoc.apropos() on modules with empty docstrings.

bpo-22885 [https://bugs.python.org/issue22885]: Fixed arbitrary code execution vulnerability in the dbm.dumb module. Original patch by Claudiu Popa.

bpo-23239 [https://bugs.python.org/issue23239]: ssl.match_hostname() now supports matching of IP addresses.

bpo-23146 [https://bugs.python.org/issue23146]: Fix mishandling of absolute Windows paths with forward slashes in pathlib.

bpo-23096 [https://bugs.python.org/issue23096]: Pickle representation of floats with protocol 0 now is the same for both Python and C implementations.

bpo-19105 [https://bugs.python.org/issue19105]: pprint now more efficiently uses free space at the right.

bpo-14910 [https://bugs.python.org/issue14910]: Add allow_abbrev parameter to argparse.ArgumentParser. Patch by Jonathan Paugh, Steven Bethard, paul j3 and Daniel Eriksson.

bpo-21717 [https://bugs.python.org/issue21717]: tarfile.open() now supports ‘x’ (exclusive creation) mode.

bpo-23344 [https://bugs.python.org/issue23344]: marshal.dumps() is now 20-25% faster on average.

bpo-20416 [https://bugs.python.org/issue20416]: marshal.dumps() with protocols 3 and 4 is now 40-50% faster on average.

bpo-23421 [https://bugs.python.org/issue23421]: Fixed compression in tarfile CLI. Patch by wdv4758h.

bpo-23367 [https://bugs.python.org/issue23367]: Fix possible overflows in the unicodedata module.

bpo-23361 [https://bugs.python.org/issue23361]: Fix possible overflow in Windows subprocess creation code.

logging.handlers.QueueListener now takes a respect_handler_level keyword argument which, if set to True, will pass messages to handlers taking handler levels into account.

bpo-19705 [https://bugs.python.org/issue19705]: turtledemo now has a visual sorting algorithm demo. Original patch from Jason Yeo.

bpo-23801 [https://bugs.python.org/issue23801]: Fix issue where cgi.FieldStorage did not always ignore the entire preamble to a multipart body.

构建

bpo-23445 [https://bugs.python.org/issue23445]: pydebug builds now use “gcc -Og” where possible, to make the resulting executable faster.

bpo-23686 [https://bugs.python.org/issue23686]: 在OS X 10.5 安装程序中将OpenSSL更新为1.0.2a.

C API

bpo-20204 [https://bugs.python.org/issue20204]: Deprecation warning is now raised for builtin types without the __module__ attribute.

Windows

bpo-23465 [https://bugs.python.org/issue23465]: Implement PEP 486 - Make the Python Launcher aware of virtual environments. Patch by Paul Moore.

bpo-23437 [https://bugs.python.org/issue23437]: Make user scripts directory versioned on Windows. Patch by Paul Moore.

Python 3.5.0 alpha 1

发布日期: 2015-02-08

核心与内置

bpo-23285 [https://bugs.python.org/issue23285]: PEP 475 - EINTR handling.

bpo-22735 [https://bugs.python.org/issue22735]: Fix many edge cases (including crashes) involving custom mro() implementations.

bpo-22896 [https://bugs.python.org/issue22896]: Avoid using PyObject_AsCharBuffer(), PyObject_AsReadBuffer() and PyObject_AsWriteBuffer().

bpo-21295 [https://bugs.python.org/issue21295]: Revert some changes (bpo-16795 [https://bugs.python.org/issue16795]) to AST line numbers and column offsets that constituted a regression.

bpo-22986 [https://bugs.python.org/issue22986]: Allow changing an object’s __class__ between a dynamic type and static type in some cases.

bpo-15859 [https://bugs.python.org/issue15859]: PyUnicode_EncodeFSDefault(), PyUnicode_EncodeMBCS() and PyUnicode_EncodeCodePage() now raise an exception if the object is not a Unicode object. For PyUnicode_EncodeFSDefault(), it was already the case on platforms other than Windows. Patch written by Campbell Barton.

bpo-21408 [https://bugs.python.org/issue21408]: The default __ne__() now returns NotImplemented if __eq__() returned NotImplemented. Original patch by Martin Panter.

bpo-23321 [https://bugs.python.org/issue23321]: Fixed a crash in str.decode() when error handler returned replacement string longer than malformed input data.

bpo-22286 [https://bugs.python.org/issue22286]: The “backslashreplace” error handlers now works with decoding and translating.

bpo-23253 [https://bugs.python.org/issue23253]: Delay-load ShellExecute[AW] in os.startfile for reduced startup overhead on Windows.

bpo-22038 [https://bugs.python.org/issue22038]: pyatomic.h now uses stdatomic.h or GCC built-in functions for atomic memory access if available. Patch written by Vitor de Lima and Gustavo Temple.

bpo-20284 [https://bugs.python.org/issue20284]: %-interpolation (aka printf) formatting added for bytes and bytearray.

bpo-23048 [https://bugs.python.org/issue23048]: Fix jumping out of an infinite while loop in the pdb.

bpo-20335 [https://bugs.python.org/issue20335]: bytes constructor now raises TypeError when encoding or errors is specified with non-string argument. Based on patch by Renaud Blanch.

bpo-22834 [https://bugs.python.org/issue22834]: If the current working directory ends up being set to a non-existent directory then import will no longer raise FileNotFoundError.

bpo-22869 [https://bugs.python.org/issue22869]: Move the interpreter startup & shutdown code to a new dedicated pylifecycle.c module

bpo-22847 [https://bugs.python.org/issue22847]: Improve method cache efficiency.

bpo-22335 [https://bugs.python.org/issue22335]: Fix crash when trying to enlarge a bytearray to 0x7fffffff bytes on a 32-bit platform.

bpo-22653 [https://bugs.python.org/issue22653]: Fix an assertion failure in debug mode when doing a reentrant dict insertion in debug mode.

bpo-22643 [https://bugs.python.org/issue22643]: Fix integer overflow in Unicode case operations (upper, lower, title, swapcase, casefold).

bpo-17636 [https://bugs.python.org/issue17636]: Circular imports involving relative imports are now supported.

bpo-22604 [https://bugs.python.org/issue22604]: Fix assertion error in debug mode when dividing a complex number by (nan+0j).

bpo-21052 [https://bugs.python.org/issue21052]: Do not raise ImportWarning when sys.path_hooks or sys.meta_path are set to None.

bpo-16518 [https://bugs.python.org/issue16518]: Use ‘bytes-like object required’ in error messages that previously used the far more cryptic “‘x’ does not support the buffer protocol.

bpo-22470 [https://bugs.python.org/issue22470]: Fixed integer overflow issues in “backslashreplace”, “xmlcharrefreplace”, and “surrogatepass” error handlers.

bpo-22540 [https://bugs.python.org/issue22540]: speed up PyObject_IsInstance and PyObject_IsSubclass in the common case that the second argument has metaclass type.

bpo-18711 [https://bugs.python.org/issue18711]: Add a new PyErr_FormatV function, similar to PyErr_Format but accepting a va_list argument.

bpo-22520 [https://bugs.python.org/issue22520]: Fix overflow checking when generating the repr of a unicode object.

bpo-22519 [https://bugs.python.org/issue22519]: Fix overflow checking in PyBytes_Repr.

bpo-22518 [https://bugs.python.org/issue22518]: Fix integer overflow issues in latin-1 encoding.

bpo-16324 [https://bugs.python.org/issue16324]: _charset parameter of MIMEText now also accepts email.charset.Charset instances. Initial patch by Claude Paroz.

bpo-1764286 [https://bugs.python.org/issue1764286]: Fix inspect.getsource() to support decorated functions. Patch by Claudiu Popa.

bpo-18554 [https://bugs.python.org/issue18554]: os.__all__ includes posix functions.

bpo-21391 [https://bugs.python.org/issue21391]: Use os.path.abspath in the shutil module.

bpo-11471 [https://bugs.python.org/issue11471]: avoid generating a JUMP_FORWARD instruction at the end of an if-block if there is no else-clause. Original patch by Eugene Toder.

bpo-22215 [https://bugs.python.org/issue22215]: Now ValueError is raised instead of TypeError when str or bytes argument contains not permitted null character or byte.

bpo-22258 [https://bugs.python.org/issue22258]: Fix the internal function set_inheritable() on Illumos. This platform exposes the function ioctl(FIOCLEX), but calling it fails with errno is ENOTTY: “Inappropriate ioctl for device”. set_inheritable() now falls back to the slower fcntl() (F_GETFD and then F_SETFD).

bpo-21389 [https://bugs.python.org/issue21389]: Displaying the __qualname__ of the underlying function in the repr of a bound method.

bpo-22206 [https://bugs.python.org/issue22206]: Using pthread, PyThread_create_key() now sets errno to ENOMEM and returns -1 (error) on integer overflow.

bpo-20184 [https://bugs.python.org/issue20184]: Argument Clinic based signature introspection added for 30 of the builtin functions.

bpo-22116 [https://bugs.python.org/issue22116]: C functions and methods (of the ‘builtin_function_or_method’ type) can now be weakref’ed. Patch by Wei Wu.

bpo-22077 [https://bugs.python.org/issue22077]: Improve index error messages for bytearrays, bytes, lists, and tuples by adding ‘or slices’. Added ‘, not <typename>’ for bytearrays. Original patch by Claudiu Popa.

bpo-20179 [https://bugs.python.org/issue20179]: Apply Argument Clinic to bytes and bytearray. Patch by Tal Einat.

bpo-22082 [https://bugs.python.org/issue22082]: Clear interned strings in slotdefs.

升级 Unicode 数据库到 7.0.0 版本。

bpo-21897 [https://bugs.python.org/issue21897]: Fix a crash with the f_locals attribute with closure variables when frame.clear() has been called.

bpo-21205 [https://bugs.python.org/issue21205]: Add a new __qualname__ attribute to generator, the qualified name, and use it in the representation of a generator (repr(gen)). The default name of the generator (__name__ attribute) is now get from the function instead of the code. Use gen.gi_code.co_name to get the name of the code.

bpo-21669 [https://bugs.python.org/issue21669]: With the aid of heuristics in SyntaxError.__init__, the parser now attempts to generate more meaningful (or at least more search engine friendly) error messages when “exec” and “print” are used as statements.

bpo-21642 [https://bugs.python.org/issue21642]: In the conditional if-else expression, allow an integer written with no space between itself and the else keyword (e.g. True if 42else False) to be valid syntax.

bpo-21523 [https://bugs.python.org/issue21523]: Fix over-pessimistic computation of the stack effect of some opcodes in the compiler. This also fixes a quadratic compilation time issue noticeable when compiling code with a large number of “and” and “or” operators.

bpo-21418 [https://bugs.python.org/issue21418]: Fix a crash in the builtin function super() when called without argument and without current frame (ex: embedded Python).

bpo-21425 [https://bugs.python.org/issue21425]: Fix flushing of standard streams in the interactive interpreter.

bpo-21435 [https://bugs.python.org/issue21435]: In rare cases, when running finalizers on objects in cyclic trash a bad pointer dereference could occur due to a subtle flaw in internal iteration logic.

bpo-21377 [https://bugs.python.org/issue21377]: PyBytes_Concat() now tries to concatenate in-place when the first argument has a reference count of 1. Patch by Nikolaus Rath.

bpo-20355 [https://bugs.python.org/issue20355]: -W command line options now have higher priority than the PYTHONWARNINGS environment variable. Patch by Arfrever.

bpo-21274 [https://bugs.python.org/issue21274]: Define PATH_MAX for GNU/Hurd in Python/pythonrun.c.

bpo-20904 [https://bugs.python.org/issue20904]: Support setting FPU precision on m68k.

bpo-21209 [https://bugs.python.org/issue21209]: Fix sending tuples to custom generator objects with the yield from syntax.

bpo-21193 [https://bugs.python.org/issue21193]: pow(a, b, c) now raises ValueError rather than TypeError when b is negative. Patch by Josh Rosenberg.

bpo-21176 [https://bugs.python.org/issue21176]: PEP 465: Add the ‘@’ operator for matrix multiplication.

bpo-21134 [https://bugs.python.org/issue21134]: Fix segfault when str is called on an uninitialized UnicodeEncodeError, UnicodeDecodeError, or UnicodeTranslateError object.

bpo-19537 [https://bugs.python.org/issue19537]: Fix PyUnicode_DATA() alignment under m68k. Patch by Andreas Schwab.

bpo-20929 [https://bugs.python.org/issue20929]: Add a type cast to avoid shifting a negative number.

bpo-20731 [https://bugs.python.org/issue20731]: Properly position in source code files even if they are opened in text mode. Patch by Serhiy Storchaka.

bpo-20637 [https://bugs.python.org/issue20637]: Key-sharing now also works for instance dictionaries of subclasses. Patch by Peter Ingebretson.

bpo-8297 [https://bugs.python.org/issue8297]: Attributes missing from modules now include the module name in the error text. Original patch by ysj.ray.

bpo-19995 [https://bugs.python.org/issue19995]: %c, %o, %x, and %X now raise TypeError on non-integer input.

bpo-19655 [https://bugs.python.org/issue19655]: The ASDL parser - used by the build process to generate code for managing the Python AST in C - was rewritten. The new parser is self contained and does not require to carry long the spark.py parser-generator library; spark.py was removed from the source base.

bpo-12546 [https://bugs.python.org/issue12546]: Allow \x00 to be used as a fill character when using str, int, float, and complex __format__ methods.

bpo-20480 [https://bugs.python.org/issue20480]: Add ipaddress.reverse_pointer. Patch by Leon Weber.

bpo-13598 [https://bugs.python.org/issue13598]: Modify string.Formatter to support auto-numbering of replacement fields. It now matches the behavior of str.format() in this regard. Patches by Phil Elson and Ramchandra Apte.

bpo-8931 [https://bugs.python.org/issue8931]: Make alternate formatting (‘#’) for type ‘c’ raise an exception. In versions prior to 3.5, ‘#’ with ‘c’ had no effect. Now specifying it is an error. Patch by Torsten Landschoff.

bpo-23165 [https://bugs.python.org/issue23165]: Perform overflow checks before allocating memory in the _Py_char2wchar function.

库

bpo-23399 [https://bugs.python.org/issue23399]: pyvenv creates relative symlinks where possible.

bpo-20289 [https://bugs.python.org/issue20289]: cgi.FieldStorage() now supports the context management protocol.

bpo-13128 [https://bugs.python.org/issue13128]: Print response headers for CONNECT requests when debuglevel > 0. Patch by Demian Brecht.

bpo-15381 [https://bugs.python.org/issue15381]: Optimized io.BytesIO to make less allocations and copyings.

bpo-22818 [https://bugs.python.org/issue22818]: Splitting on a pattern that could match an empty string now raises a warning. Patterns that can only match empty strings are now rejected.

bpo-23099 [https://bugs.python.org/issue23099]: Closing io.BytesIO with exported buffer is rejected now to prevent corrupting exported buffer.

bpo-23326 [https://bugs.python.org/issue23326]: Removed __ne__ implementations. Since fixing default __ne__ implementation in bpo-21408 [https://bugs.python.org/issue21408] they are redundant.

bpo-23363 [https://bugs.python.org/issue23363]: Fix possible overflow in itertools.permutations.

bpo-23364 [https://bugs.python.org/issue23364]: Fix possible overflow in itertools.product.

bpo-23366 [https://bugs.python.org/issue23366]: Fixed possible integer overflow in itertools.combinations.

bpo-23369 [https://bugs.python.org/issue23369]: Fixed possible integer overflow in _json.encode_basestring_ascii.

bpo-23353 [https://bugs.python.org/issue23353]: Fix the exception handling of generators in PyEval_EvalFrameEx(). At entry, save or swap the exception state even if PyEval_EvalFrameEx() is called with throwflag=0. At exit, the exception state is now always restored or swapped, not only if why is WHY_YIELD or WHY_RETURN. Patch co-written with Antoine Pitrou.

bpo-14099 [https://bugs.python.org/issue14099]: Restored support of writing ZIP files to tellable but non-seekable streams.

bpo-14099 [https://bugs.python.org/issue14099]: Writing to ZipFile and reading multiple ZipExtFiles is threadsafe now.

bpo-19361 [https://bugs.python.org/issue19361]: JSON decoder now raises JSONDecodeError instead of ValueError.

bpo-18518 [https://bugs.python.org/issue18518]: timeit now rejects statements which can’t be compiled outside a function or a loop (e.g. “return” or “break”).

bpo-23094 [https://bugs.python.org/issue23094]: Fixed readline with frames in Python implementation of pickle.

bpo-23268 [https://bugs.python.org/issue23268]: Fixed bugs in the comparison of ipaddress classes.

bpo-21408 [https://bugs.python.org/issue21408]: Removed incorrect implementations of __ne__() which didn’t returned NotImplemented if __eq__() returned NotImplemented. The default __ne__() now works correctly.

bpo-19996 [https://bugs.python.org/issue19996]: email.feedparser.FeedParser now handles (malformed) headers with no key rather than assuming the body has started.

bpo-20188 [https://bugs.python.org/issue20188]: Support Application-Layer Protocol Negotiation (ALPN) in the ssl module.

bpo-23133 [https://bugs.python.org/issue23133]: Pickling of ipaddress objects now produces more compact and portable representation.

bpo-23248 [https://bugs.python.org/issue23248]: 从最新的OpenSSL git master更新SSL错误代码。

bpo-23266 [https://bugs.python.org/issue23266]: Much faster implementation of ipaddress.collapse_addresses() when there are many non-consecutive addresses.

bpo-23098 [https://bugs.python.org/issue23098]: 64-bit dev_t is now supported in the os module.

bpo-21817 [https://bugs.python.org/issue21817]: When an exception is raised in a task submitted to a ProcessPoolExecutor, the remote traceback is now displayed in the parent process. Patch by Claudiu Popa.

bpo-15955 [https://bugs.python.org/issue15955]: Add an option to limit output size when decompressing LZMA data. Patch by Nikolaus Rath and Martin Panter.

bpo-23250 [https://bugs.python.org/issue23250]: In the http.cookies module, capitalize “HttpOnly” and “Secure” as they are written in the standard.

bpo-23063 [https://bugs.python.org/issue23063]: In the distutils’ check command, fix parsing of reST with code or code-block directives.

bpo-23209 [https://bugs.python.org/issue23209]: selectors.BaseSelector.get_key() now raises a RuntimeError if the selector is closed. And selectors.BaseSelector.close() now clears its internal reference to the selector mapping to break a reference cycle. Initial patch written by Martin Richard. (See also: bpo-23225 [https://bugs.python.org/issue23225])

bpo-17911 [https://bugs.python.org/issue17911]: Provide a way to seed the linecache for a PEP-302 module without actually loading the code.

bpo-17911 [https://bugs.python.org/issue17911]: Provide a new object API for traceback, including the ability to not lookup lines at all until the traceback is actually rendered, without any trace of the original objects being kept alive.

bpo-19777 [https://bugs.python.org/issue19777]: Provide a home() classmethod on Path objects. Contributed by Victor Salgado and Mayank Tripathi.

bpo-23206 [https://bugs.python.org/issue23206]: Make json.dumps(..., ensure_ascii=False) as fast as the default case of ensure_ascii=True. Patch by Naoki Inada.

bpo-23185 [https://bugs.python.org/issue23185]: Add math.inf and math.nan constants.

bpo-23186 [https://bugs.python.org/issue23186]: Add ssl.SSLObject.shared_ciphers() and ssl.SSLSocket.shared_ciphers() to fetch the client’s list ciphers sent at handshake.

bpo-23143 [https://bugs.python.org/issue23143]: Remove compatibility with OpenSSLs older than 0.9.8.

bpo-23132 [https://bugs.python.org/issue23132]: Improve performance and introspection support of comparison methods created by functool.total_ordering.

bpo-19776 [https://bugs.python.org/issue19776]: Add an expanduser() method on Path objects.

bpo-23112 [https://bugs.python.org/issue23112]: Fix SimpleHTTPServer to correctly carry the query string and fragment when it redirects to add a trailing slash.

bpo-21793 [https://bugs.python.org/issue21793]: Added http.HTTPStatus enums (i.e. HTTPStatus.OK, HTTPStatus.NOT_FOUND). Patch by Demian Brecht.

bpo-23093 [https://bugs.python.org/issue23093]: In the io, module allow more operations to work on detached streams.

bpo-23111 [https://bugs.python.org/issue23111]: In the ftplib, make ssl.PROTOCOL_SSLv23 the default protocol version.

bpo-22585 [https://bugs.python.org/issue22585]: On OpenBSD 5.6 and newer, os.urandom() now calls getentropy(), instead of reading /dev/urandom, to get pseudo-random bytes.

bpo-19104 [https://bugs.python.org/issue19104]: pprint now produces evaluable output for wrapped strings.

bpo-23071 [https://bugs.python.org/issue23071]: Added missing names to codecs.__all__. Patch by Martin Panter.

bpo-22783 [https://bugs.python.org/issue22783]: Pickling now uses the NEWOBJ opcode instead of the NEWOBJ_EX opcode if possible.

bpo-15513 [https://bugs.python.org/issue15513]: Added a __sizeof__ implementation for pickle classes.

bpo-19858 [https://bugs.python.org/issue19858]: pickletools.optimize() now aware of the MEMOIZE opcode, can produce more compact result and no longer produces invalid output if input data contains MEMOIZE opcodes together with PUT or BINPUT opcodes.

bpo-22095 [https://bugs.python.org/issue22095]: Fixed HTTPConnection.set_tunnel with default port. The port value in the host header was set to “None”. Patch by Demian Brecht.

bpo-23016 [https://bugs.python.org/issue23016]: A warning no longer produces an AttributeError when the program is run with pythonw.exe.

bpo-21775 [https://bugs.python.org/issue21775]: shutil.copytree(): fix crash when copying to VFAT. An exception handler assumed that OSError objects always have a ‘winerror’ attribute. That is not the case, so the exception handler itself raised AttributeError when run on Linux (and, presumably, any other non-Windows OS). Patch by Greg Ward.

bpo-1218234 [https://bugs.python.org/issue1218234]: Fix inspect.getsource() to load updated source of reloaded module. Initial patch by Berker Peksag.

bpo-21740 [https://bugs.python.org/issue21740]: Support wrapped callables in doctest. Patch by Claudiu Popa.

bpo-23009 [https://bugs.python.org/issue23009]: Make sure selectors.EpollSelecrtor.select() works when no FD is registered.

bpo-22959 [https://bugs.python.org/issue22959]: In the constructor of http.client.HTTPSConnection, prefer the context’s check_hostname attribute over the check_hostname parameter.

bpo-22696 [https://bugs.python.org/issue22696]: Add function sys.is_finalizing() to know about interpreter shutdown.

bpo-16043 [https://bugs.python.org/issue16043]: Add a default limit for the amount of data xmlrpclib.gzip_decode will return. This resolves CVE-2013-1753.

bpo-14099 [https://bugs.python.org/issue14099]: ZipFile.open() no longer reopen the underlying file. Objects returned by ZipFile.open() can now operate independently of the ZipFile even if the ZipFile was created by passing in a file-like object as the first argument to the constructor.

bpo-22966 [https://bugs.python.org/issue22966]: Fix __pycache__ pyc file name clobber when pyc_compile is asked to compile a source file containing multiple dots in the source file name.

bpo-21971 [https://bugs.python.org/issue21971]: Update turtledemo doc and add module to the index.

bpo-21032 [https://bugs.python.org/issue21032]: Fixed socket leak if HTTPConnection.getresponse() fails. Original patch by Martin Panter.

bpo-22407 [https://bugs.python.org/issue22407]: Deprecated the use of re.LOCALE flag with str patterns or re.ASCII. It was newer worked.

bpo-22902 [https://bugs.python.org/issue22902]: The “ip” command is now used on Linux to determine MAC address in uuid.getnode(). Pach by Bruno Cauet.

bpo-22960 [https://bugs.python.org/issue22960]: Add a context argument to xmlrpclib.ServerProxy constructor.

bpo-22389 [https://bugs.python.org/issue22389]: Add contextlib.redirect_stderr().

bpo-21356 [https://bugs.python.org/issue21356]: Make ssl.RAND_egd() optional to support LibreSSL. The availability of the function is checked during the compilation. Patch written by Bernard Spil.

bpo-22915 [https://bugs.python.org/issue22915]: SAX parser now supports files opened with file descriptor or bytes path.

bpo-22609 [https://bugs.python.org/issue22609]: Constructors and update methods of mapping classes in the collections module now accept the self keyword argument.

bpo-22940 [https://bugs.python.org/issue22940]: Add readline.append_history_file.

bpo-19676 [https://bugs.python.org/issue19676]: Added the “namereplace” error handler.

bpo-22788 [https://bugs.python.org/issue22788]: Add context parameter to logging.handlers.HTTPHandler.

bpo-22921 [https://bugs.python.org/issue22921]: Allow SSLContext to take the hostname parameter even if OpenSSL doesn’t support SNI.

bpo-22894 [https://bugs.python.org/issue22894]: TestCase.subTest() would cause the test suite to be stopped when in failfast mode, even in the absence of failures.

bpo-22796 [https://bugs.python.org/issue22796]: HTTP cookie parsing is now stricter, in order to protect against potential injection attacks.

bpo-22370 [https://bugs.python.org/issue22370]: Windows detection in pathlib is now more robust.

bpo-22841 [https://bugs.python.org/issue22841]: Reject coroutines in asyncio add_signal_handler(). Patch by Ludovic.Gasc.

bpo-19494 [https://bugs.python.org/issue19494]: Added urllib.request.HTTPBasicPriorAuthHandler. Patch by Matej Cepl.

bpo-22578 [https://bugs.python.org/issue22578]: Added attributes to the re.error class.

bpo-22849 [https://bugs.python.org/issue22849]: Fix possible double free in the io.TextIOWrapper constructor.

bpo-12728 [https://bugs.python.org/issue12728]: Different Unicode characters having the same uppercase but different lowercase are now matched in case-insensitive regular expressions.

bpo-22821 [https://bugs.python.org/issue22821]: Fixed fcntl() with integer argument on 64-bit big-endian platforms.

bpo-21650 [https://bugs.python.org/issue21650]: Add an --sort-keys option to json.tool CLI.

bpo-22824 [https://bugs.python.org/issue22824]: Updated reprlib output format for sets to use set literals. Patch contributed by Berker Peksag.

bpo-22824 [https://bugs.python.org/issue22824]: Updated reprlib output format for arrays to display empty arrays without an unnecessary empty list. Suggested by Serhiy Storchaka.

bpo-22406 [https://bugs.python.org/issue22406]: Fixed the uu_codec codec incorrectly ported to 3.x. Based on patch by Martin Panter.

bpo-17293 [https://bugs.python.org/issue17293]: uuid.getnode() now determines MAC address on AIX using netstat. Based on patch by Aivars Kalvāns.

bpo-22769 [https://bugs.python.org/issue22769]: Fixed ttk.Treeview.tag_has() when called without arguments.

bpo-22417 [https://bugs.python.org/issue22417]: Verify certificates by default in httplib (PEP 476).

bpo-22775 [https://bugs.python.org/issue22775]: Fixed unpickling of http.cookies.SimpleCookie with protocol 2 and above. Patch by Tim Graham.

bpo-22776 [https://bugs.python.org/issue22776]: Brought excluded code into the scope of a try block in SysLogHandler.emit().

bpo-22665 [https://bugs.python.org/issue22665]: Add missing get_terminal_size and SameFileError to shutil.__all__.

bpo-6623 [https://bugs.python.org/issue6623]: Remove deprecated Netrc class in the ftplib module. Patch by Matt Chaput.

bpo-17381 [https://bugs.python.org/issue17381]: Fixed handling of case-insensitive ranges in regular expressions.

bpo-22410 [https://bugs.python.org/issue22410]: Module level functions in the re module now cache compiled locale-dependent regular expressions taking into account the locale.

bpo-22759 [https://bugs.python.org/issue22759]: Query methods on pathlib.Path() (exists(), is_dir(), etc.) now return False when the underlying stat call raises NotADirectoryError.

bpo-8876 [https://bugs.python.org/issue8876]: distutils now falls back to copying files when hard linking doesn’t work. This allows use with special filesystems such as VirtualBox shared folders.

bpo-22217 [https://bugs.python.org/issue22217]: Implemented reprs of classes in the zipfile module.

bpo-22457 [https://bugs.python.org/issue22457]: Honour load_tests in the start_dir of discovery.

bpo-18216 [https://bugs.python.org/issue18216]: gettext now raises an error when a .mo file has an unsupported major version number. Patch by Aaron Hill.

bpo-13918 [https://bugs.python.org/issue13918]: Provide a locale.delocalize() function which can remove locale-specific number formatting from a string representing a number, without then converting it to a specific type. Patch by Cédric Krier.

bpo-22676 [https://bugs.python.org/issue22676]: Make the pickling of global objects which don’t have a __module__ attribute less slow.

bpo-18853 [https://bugs.python.org/issue18853]: Fixed ResourceWarning in shlex.__nain__.

bpo-9351 [https://bugs.python.org/issue9351]: Defaults set with set_defaults on an argparse subparser are no longer ignored when also set on the parent parser.

bpo-7559 [https://bugs.python.org/issue7559]: unittest test loading ImportErrors are reported as import errors with their import exception rather than as attribute errors after the import has already failed.

bpo-19746 [https://bugs.python.org/issue19746]: Make it possible to examine the errors from unittest discovery without executing the test suite. The new errors attribute on TestLoader exposes these non-fatal errors encountered during discovery.

bpo-21991 [https://bugs.python.org/issue21991]: Make email.headerregistry’s header ‘params’ attributes be read-only (MappingProxyType). Previously the dictionary was modifiable but a new one was created on each access of the attribute.

bpo-22638 [https://bugs.python.org/issue22638]: SSLv3 is now disabled throughout the standard library. It can still be enabled by instantiating a SSLContext manually.

bpo-22641 [https://bugs.python.org/issue22641]: In asyncio, the default SSL context for client connections is now created using ssl.create_default_context(), for stronger security.

bpo-17401 [https://bugs.python.org/issue17401]: Include closefd in io.FileIO repr.

bpo-21338 [https://bugs.python.org/issue21338]: Add silent mode for compileall. quiet parameters of compile_{dir, file, path} functions now have a multilevel value. Also, -q option of the CLI now have a multilevel value. Patch by Thomas Kluyver.

bpo-20152 [https://bugs.python.org/issue20152]: Convert the array and cmath modules to Argument Clinic.

bpo-18643 [https://bugs.python.org/issue18643]: Add socket.socketpair() on Windows.

bpo-22435 [https://bugs.python.org/issue22435]: Fix a file descriptor leak when socketserver bind fails.

bpo-13096 [https://bugs.python.org/issue13096]: Fixed segfault in CTypes POINTER handling of large values.

bpo-11694 [https://bugs.python.org/issue11694]: Raise ConversionError in xdrlib as documented. Patch by Filip Gruszczyński and Claudiu Popa.

bpo-19380 [https://bugs.python.org/issue19380]: Optimized parsing of regular expressions.

bpo-1519638 [https://bugs.python.org/issue1519638]: Now unmatched groups are replaced with empty strings in re.sub() and re.subn().

bpo-18615 [https://bugs.python.org/issue18615]: sndhdr.what/whathdr now return a namedtuple.

bpo-22462 [https://bugs.python.org/issue22462]: Fix pyexpat’s creation of a dummy frame to make it appear in exception tracebacks.

bpo-21965 [https://bugs.python.org/issue21965]: Add support for in-memory SSL to the ssl module. Patch by Geert Jansen.

bpo-21173 [https://bugs.python.org/issue21173]: Fix len() on a WeakKeyDictionary when .clear() was called with an iterator alive.

bpo-11866 [https://bugs.python.org/issue11866]: Eliminated race condition in the computation of names for new threads.

bpo-21905 [https://bugs.python.org/issue21905]: Avoid RuntimeError in pickle.whichmodule() when sys.modules is mutated while iterating. Patch by Olivier Grisel.

bpo-11271 [https://bugs.python.org/issue11271]: concurrent.futures.Executor.map() now takes a chunksize argument to allow batching of tasks in child processes and improve performance of ProcessPoolExecutor. Patch by Dan O’Reilly.

bpo-21883 [https://bugs.python.org/issue21883]: os.path.join() and os.path.relpath() now raise a TypeError with more helpful error message for unsupported or mismatched types of arguments.

bpo-22219 [https://bugs.python.org/issue22219]: The zipfile module CLI now adds entries for directories (including empty directories) in ZIP file.

bpo-22449 [https://bugs.python.org/issue22449]: In the ssl.SSLContext.load_default_certs, consult the environmental variables SSL_CERT_DIR and SSL_CERT_FILE on Windows.

bpo-22508 [https://bugs.python.org/issue22508]: The email.__version__ variable has been removed; the email code is no longer shipped separately from the stdlib, and __version__ hasn’t been updated in several releases.

bpo-20076 [https://bugs.python.org/issue20076]: Added non derived UTF-8 aliases to locale aliases table.

bpo-20079 [https://bugs.python.org/issue20079]: Added locales supported in glibc 2.18 to locale alias table.

bpo-20218 [https://bugs.python.org/issue20218]: Added convenience methods read_text/write_text and read_bytes/ write_bytes to pathlib.Path objects.

bpo-22396 [https://bugs.python.org/issue22396]: On 32-bit AIX platform, don’t expose os.posix_fadvise() nor os.posix_fallocate() because their prototypes in system headers are wrong.

bpo-22517 [https://bugs.python.org/issue22517]: When an io.BufferedRWPair object is deallocated, clear its weakrefs.

bpo-22437 [https://bugs.python.org/issue22437]: Number of capturing groups in regular expression is no longer limited by 100.

bpo-17442 [https://bugs.python.org/issue17442]: InteractiveInterpreter now displays the full chained traceback in its showtraceback method, to match the built in interactive interpreter.

bpo-23392 [https://bugs.python.org/issue23392]: Added tests for marshal C API that works with FILE*.

bpo-10510 [https://bugs.python.org/issue10510]: distutils register and upload methods now use HTML standards compliant CRLF line endings.

bpo-9850 [https://bugs.python.org/issue9850]: Fixed macpath.join() for empty first component. Patch by Oleg Oshmyan.

bpo-5309 [https://bugs.python.org/issue5309]: distutils’ build and build_ext commands now accept a -j option to enable parallel building of extension modules.

bpo-22448 [https://bugs.python.org/issue22448]: Improve canceled timer handles cleanup to prevent unbound memory usage. Patch by Joshua Moore-Oliva.

bpo-22427 [https://bugs.python.org/issue22427]: TemporaryDirectory no longer attempts to clean up twice when used in the with statement in generator.

bpo-22362 [https://bugs.python.org/issue22362]: Forbidden ambiguous octal escapes out of range 0-0o377 in regular expressions.

bpo-20912 [https://bugs.python.org/issue20912]: Now directories added to ZIP file have correct Unix and MS-DOS directory attributes.

bpo-21866 [https://bugs.python.org/issue21866]: ZipFile.close() no longer writes ZIP64 central directory records if allowZip64 is false.

bpo-22278 [https://bugs.python.org/issue22278]: Fix urljoin problem with relative urls, a regression observed after changes to issue22118 were submitted.

bpo-22415 [https://bugs.python.org/issue22415]: Fixed debugging output of the GROUPREF_EXISTS opcode in the re module. Removed trailing spaces in debugging output.

bpo-22423 [https://bugs.python.org/issue22423]: Unhandled exception in thread no longer causes unhandled AttributeError when sys.stderr is None.

bpo-21332 [https://bugs.python.org/issue21332]: Ensure that bufsize=1 in subprocess.Popen() selects line buffering, rather than block buffering. Patch by Akira Li.

bpo-21091 [https://bugs.python.org/issue21091]: Fix API bug: email.message.EmailMessage.is_attachment is now a method.

bpo-21079 [https://bugs.python.org/issue21079]: Fix email.message.EmailMessage.is_attachment to return the correct result when the header has parameters as well as a value.

bpo-22247 [https://bugs.python.org/issue22247]: Add NNTPError to nntplib.__all__.

bpo-22366 [https://bugs.python.org/issue22366]: urllib.request.urlopen will accept a context object (SSLContext) as an argument which will then be used for HTTPS connection. Patch by Alex Gaynor.

bpo-4180 [https://bugs.python.org/issue4180]: The warnings registries are now reset when the filters are modified.

bpo-22419 [https://bugs.python.org/issue22419]: Limit the length of incoming HTTP request in wsgiref server to 65536 bytes and send a 414 error code for higher lengths. Patch contributed by Devin Cook.

Lax cookie parsing in http.cookies could be a security issue when combined with non-standard cookie handling in some Web browsers. Reported by Sergey Bobrov.

bpo-20537 [https://bugs.python.org/issue20537]: logging methods now accept an exception instance as well as a Boolean value or exception tuple. Thanks to Yury Selivanov for the patch.

bpo-22384 [https://bugs.python.org/issue22384]: An exception in Tkinter callback no longer crashes the program when it is run with pythonw.exe.

bpo-22168 [https://bugs.python.org/issue22168]: Prevent turtle AttributeError with non-default Canvas on OS X.

bpo-21147 [https://bugs.python.org/issue21147]: sqlite3 now raises an exception if the request contains a null character instead of truncating it. Based on patch by Victor Stinner.

bpo-13968 [https://bugs.python.org/issue13968]: The glob module now supports recursive search in subdirectories using the ** pattern.

bpo-21951 [https://bugs.python.org/issue21951]: Fixed a crash in Tkinter on AIX when called Tcl command with empty string or tuple argument.

bpo-21951 [https://bugs.python.org/issue21951]: Tkinter now most likely raises MemoryError instead of crash if the memory allocation fails.

bpo-22338 [https://bugs.python.org/issue22338]: Fix a crash in the json module on memory allocation failure.

bpo-12410 [https://bugs.python.org/issue12410]: imaplib.IMAP4 now supports the context management protocol. Original patch by Tarek Ziadé.

bpo-21270 [https://bugs.python.org/issue21270]: We now override tuple methods in mock.call objects so that they can be used as normal call attributes.

bpo-16662 [https://bugs.python.org/issue16662]: load_tests() is now unconditionally run when it is present in a package’s __init__.py. TestLoader.loadTestsFromModule() still accepts use_load_tests, but it is deprecated and ignored. A new keyword-only attribute pattern is added and documented. Patch given by Robert Collins, tweaked by Barry Warsaw.

bpo-22226 [https://bugs.python.org/issue22226]: First letter no longer is stripped from the “status” key in the result of Treeview.heading().

bpo-19524 [https://bugs.python.org/issue19524]: Fixed resource leak in the HTTP connection when an invalid response is received. Patch by Martin Panter.

bpo-20421 [https://bugs.python.org/issue20421]: Add a .version() method to SSL sockets exposing the actual protocol version in use.

bpo-19546 [https://bugs.python.org/issue19546]: configparser exceptions no longer expose implementation details. Chained KeyErrors are removed, which leads to cleaner tracebacks. Patch by Claudiu Popa.

bpo-22051 [https://bugs.python.org/issue22051]: turtledemo no longer reloads examples to re-run them. Initialization of variables and gui setup should be done in main(), which is called each time a demo is run, but not on import.

bpo-21933 [https://bugs.python.org/issue21933]: Turtledemo users can change the code font size with a menu selection or control(command) ‘-‘ or ‘+’ or control-mousewheel. Original patch by Lita Cho.

bpo-21597 [https://bugs.python.org/issue21597]: The separator between the turtledemo text pane and the drawing canvas can now be grabbed and dragged with a mouse. The code text pane can be widened to easily view or copy the full width of the text. The canvas can be widened on small screens. Original patches by Jan Kanis and Lita Cho.

bpo-18132 [https://bugs.python.org/issue18132]: Turtledemo buttons no longer disappear when the window is shrunk. Original patches by Jan Kanis and Lita Cho.

bpo-22043 [https://bugs.python.org/issue22043]: time.monotonic() is now always available. threading.Lock.acquire(), threading.RLock.acquire() and socket operations now use a monotonic clock, instead of the system clock, when a timeout is used.

bpo-21527 [https://bugs.python.org/issue21527]: Add a default number of workers to ThreadPoolExecutor equal to 5 times the number of CPUs. Patch by Claudiu Popa.

bpo-22216 [https://bugs.python.org/issue22216]: smtplib now resets its state more completely after a quit. The most obvious consequence of the previous behavior was a STARTTLS failure during a connect/starttls/quit/connect/starttls sequence.

bpo-22098 [https://bugs.python.org/issue22098]: ctypes’ BigEndianStructure and LittleEndianStructure now define an empty __slots__ so that subclasses don’t always get an instance dict. Patch by Claudiu Popa.

bpo-22185 [https://bugs.python.org/issue22185]: Fix an occasional RuntimeError in threading.Condition.wait() caused by mutation of the waiters queue without holding the lock. Patch by Doug Zongker.

bpo-22287 [https://bugs.python.org/issue22287]: On UNIX, _PyTime_gettimeofday() now uses clock_gettime(CLOCK_REALTIME) if available. As a side effect, Python now depends on the librt library on Solaris and on Linux (only with glibc older than 2.17).

bpo-22182 [https://bugs.python.org/issue22182]: Use e.args to unpack exceptions correctly in distutils.file_util.move_file. Patch by Claudiu Popa.

The webbrowser module now uses subprocess’s start_new_session=True rather than a potentially risky preexec_fn=os.setsid call.

bpo-22042 [https://bugs.python.org/issue22042]: signal.set_wakeup_fd(fd) now raises an exception if the file descriptor is in blocking mode.

bpo-16808 [https://bugs.python.org/issue16808]: inspect.stack() now returns a named tuple instead of a tuple. Patch by Daniel Shahaf.

bpo-22236 [https://bugs.python.org/issue22236]: Fixed Tkinter images copying operations in NoDefaultRoot mode.

bpo-2527 [https://bugs.python.org/issue2527]: Add a globals argument to timeit functions, in order to override the globals namespace in which the timed code is executed. Patch by Ben Roberts.

bpo-22118 [https://bugs.python.org/issue22118]: Switch urllib.parse to use RFC 3986 semantics for the resolution of relative URLs, rather than RFCs 1808 and 2396. Patch by Demian Brecht.

bpo-21549 [https://bugs.python.org/issue21549]: Added the “members” parameter to TarFile.list().

bpo-19628 [https://bugs.python.org/issue19628]: Allow compileall recursion depth to be specified with a -r option.

bpo-15696 [https://bugs.python.org/issue15696]: Add a __sizeof__ implementation for mmap objects on Windows.

bpo-22068 [https://bugs.python.org/issue22068]: Avoided reference loops with Variables and Fonts in Tkinter.

bpo-22165 [https://bugs.python.org/issue22165]: SimpleHTTPRequestHandler now supports undecodable file names.

bpo-15381 [https://bugs.python.org/issue15381]: Optimized line reading in io.BytesIO.

bpo-8797 [https://bugs.python.org/issue8797]: Raise HTTPError on failed Basic Authentication immediately. Initial patch by Sam Bull.

bpo-20729 [https://bugs.python.org/issue20729]: Restored the use of lazy iterkeys()/itervalues()/iteritems() in the mailbox module.

bpo-21448 [https://bugs.python.org/issue21448]: Changed FeedParser feed() to avoid O(N**2) behavior when parsing long line. Original patch by Raymond Hettinger.

bpo-22184 [https://bugs.python.org/issue22184]: The functools LRU Cache decorator factory now gives an earlier and clearer error message when the user forgets the required parameters.

bpo-17923 [https://bugs.python.org/issue17923]: glob() patterns ending with a slash no longer match non-dirs on AIX. Based on patch by Delhallt.

bpo-21725 [https://bugs.python.org/issue21725]: Added support for RFC 6531 (SMTPUTF8) in smtpd.

bpo-22176 [https://bugs.python.org/issue22176]: Update the ctypes module’s libffi to v3.1. This release adds support for the Linux AArch64 and POWERPC ELF ABIv2 little endian architectures.

bpo-5411 [https://bugs.python.org/issue5411]: Added support for the “xztar” format in the shutil module.

bpo-21121 [https://bugs.python.org/issue21121]: Don’t force 3rd party C extensions to be built with <<<<<<< HEAD -Werror=declaration-after-statement. ======= -Werror=declaration-after-statement. >>>>>>> 3f819ca138… bpo-35110 [https://bugs.python.org/issue35110]: Fix unintentional spaces around hyphens and dashes. (GH-10231)

bpo-21975 [https://bugs.python.org/issue21975]: Fixed crash when using uninitialized sqlite3.Row (in particular when unpickling pickled sqlite3.Row). sqlite3.Row is now initialized in the __new__() method.

bpo-20170 [https://bugs.python.org/issue20170]: Convert posixmodule to use Argument Clinic.

bpo-21539 [https://bugs.python.org/issue21539]: Add an exists_ok argument to Pathlib.mkdir() to mimic mkdir -p and os.makedirs() functionality. When true, ignore FileExistsErrors. Patch by Berker Peksag.

bpo-22127 [https://bugs.python.org/issue22127]: Bypass IDNA for pure-ASCII host names in the socket module (in particular for numeric IPs).

bpo-21047 [https://bugs.python.org/issue21047]: set the default value for the convert_charrefs argument of HTMLParser to True. Patch by Berker Peksag.

在 html.entities 包中增加 __all__ 变量。

bpo-15114 [https://bugs.python.org/issue15114]: the strict mode and argument of HTMLParser, HTMLParser.error, and the HTMLParserError exception have been removed.

bpo-22085 [https://bugs.python.org/issue22085]: 在 Tkinter 中删除了对 TK 8.3 的支持。

bpo-21580 [https://bugs.python.org/issue21580]: Now Tkinter correctly handles bytes arguments passed to Tk. In particular this allows initializing images from binary data.

bpo-22003 [https://bugs.python.org/issue22003]: When initialized from a bytes object, io.BytesIO() now defers making a copy until it is mutated, improving performance and memory use on some use cases. Patch by David Wilson.

bpo-22018 [https://bugs.python.org/issue22018]: On Windows, signal.set_wakeup_fd() now also supports sockets. A side effect is that Python depends to the WinSock library.

bpo-22054 [https://bugs.python.org/issue22054]: Add os.get_blocking() and os.set_blocking() functions to get and set the blocking mode of a file descriptor (False if the O_NONBLOCK flag is set, True otherwise). These functions are not available on Windows.

bpo-17172 [https://bugs.python.org/issue17172]: Make turtledemo start as active on OS X even when run with subprocess. Patch by Lita Cho.

bpo-21704 [https://bugs.python.org/issue21704]: Fix build error for _multiprocessing when semaphores are not available. Patch by Arfrever Frehtes Taifersar Arahesis.

bpo-20173 [https://bugs.python.org/issue20173]: Convert sha1, sha256, sha512 and md5 to ArgumentClinic. Patch by Vajrasky Kok.

Fix repr(_socket.socket) on Windows 64-bit: don’t fail with OverflowError on closed socket. repr(socket.socket) already works fine.

bpo-22033 [https://bugs.python.org/issue22033]: Reprs of most Python implemented classes now contain actual class name instead of hardcoded one.

bpo-21947 [https://bugs.python.org/issue21947]: The dis module can now disassemble generator-iterator objects based on their gi_code attribute. Patch by Clement Rouault.

bpo-16133 [https://bugs.python.org/issue16133]: The asynchat.async_chat.handle_read() method now ignores BlockingIOError exceptions.

bpo-22044 [https://bugs.python.org/issue22044]: Fixed premature DECREF in call_tzinfo_method. Patch by Tom Flanagan.

bpo-19884 [https://bugs.python.org/issue19884]: readline: Disable the meta modifier key if stdout is not a terminal to not write the ANSI sequence "\033[1034h" into stdout. This sequence is used on some terminal (ex: TERM=xterm-256color”) to enable support of 8 bit characters.

bpo-4350 [https://bugs.python.org/issue4350]: Removed a number of out-of-dated and non-working for a long time Tkinter methods.

bpo-6167 [https://bugs.python.org/issue6167]: Scrollbar.activate() now returns the name of active element if the argument is not specified. Scrollbar.set() now always accepts only 2 arguments.

bpo-15275 [https://bugs.python.org/issue15275]: Clean up and speed up the ntpath module.

bpo-21888 [https://bugs.python.org/issue21888]: plistlib’s load() and loads() now work if the fmt parameter is specified.

bpo-22032 [https://bugs.python.org/issue22032]: __qualname__ instead of __name__ is now always used to format fully qualified class names of Python implemented classes.

bpo-22031 [https://bugs.python.org/issue22031]: Reprs now always use hexadecimal format with the “0x” prefix when contain an id in form ” at 0x…”.

bpo-22018 [https://bugs.python.org/issue22018]: signal.set_wakeup_fd() now raises an OSError instead of a ValueError on fstat() failure.

bpo-21044 [https://bugs.python.org/issue21044]: tarfile.open() now handles fileobj with an integer ‘name’ attribute. Based on patch by Antoine Pietri.

bpo-21966 [https://bugs.python.org/issue21966]: Respect -q command-line option when code module is ran.

bpo-19076 [https://bugs.python.org/issue19076]: Don’t pass the redundant ‘file’ argument to self.error().

bpo-16382 [https://bugs.python.org/issue16382]: Improve exception message of warnings.warn() for bad category. Initial patch by Phil Elson.

bpo-21932 [https://bugs.python.org/issue21932]: os.read() now uses a Py_ssize_t() type instead of int for the size to support reading more than 2 GB at once. On Windows, the size is truncated to INT_MAX. As any call to os.read(), the OS may read less bytes than the number of requested bytes.

bpo-21942 [https://bugs.python.org/issue21942]: Fixed source file viewing in pydoc’s server mode on Windows.

bpo-11259 [https://bugs.python.org/issue11259]: asynchat.async_chat().set_terminator() now raises a ValueError if the number of received bytes is negative.

bpo-12523 [https://bugs.python.org/issue12523]: asynchat.async_chat.push() now raises a TypeError if it doesn’t get a bytes string

bpo-21707 [https://bugs.python.org/issue21707]: Add missing kwonlyargcount argument to ModuleFinder.replace_paths_in_code().

bpo-20639 [https://bugs.python.org/issue20639]: calling Path.with_suffix(‘’) allows removing the suffix again. Patch by July Tikhonov.

bpo-21714 [https://bugs.python.org/issue21714]: Disallow the construction of invalid paths using Path.with_name(). Original patch by Antony Lee.

bpo-15014 [https://bugs.python.org/issue15014]: Added ‘auth’ method to smtplib to make implementing auth mechanisms simpler, and used it internally in the login method.

bpo-21151 [https://bugs.python.org/issue21151]: Fixed a segfault in the winreg module when None is passed as a REG_BINARY value to SetValueEx. Patch by John Ehresman.

bpo-21090 [https://bugs.python.org/issue21090]: io.FileIO.readall() does not ignore I/O errors anymore. Before, it ignored I/O errors if at least the first C call read() succeed.

bpo-5800 [https://bugs.python.org/issue5800]: headers parameter of wsgiref.headers.Headers is now optional. Initial patch by Pablo Torres Navarrete and SilentGhost.

bpo-21781 [https://bugs.python.org/issue21781]: ssl.RAND_add() now supports strings longer than 2 GB.

bpo-21679 [https://bugs.python.org/issue21679]: Prevent extraneous fstat() calls during open(). Patch by Bohuslav Kabrda.

bpo-21863 [https://bugs.python.org/issue21863]: cProfile now displays the module name of C extension functions, in addition to their own name.

bpo-11453 [https://bugs.python.org/issue11453]: asyncore: emit a ResourceWarning when an unclosed file_wrapper object is destroyed. The destructor now closes the file if needed. The close() method can now be called twice: the second call does nothing.

bpo-21858 [https://bugs.python.org/issue21858]: Better handling of Python exceptions in the sqlite3 module.

bpo-21476 [https://bugs.python.org/issue21476]: Make sure the email.parser.BytesParser TextIOWrapper is discarded after parsing, so the input file isn’t unexpectedly closed.

bpo-20295 [https://bugs.python.org/issue20295]: imghdr now recognizes OpenEXR format images.

bpo-21729 [https://bugs.python.org/issue21729]: Used the “with” statement in the dbm.dumb module to ensure files closing. Patch by Claudiu Popa.

bpo-21491 [https://bugs.python.org/issue21491]: socketserver: Fix a race condition in child processes reaping.

bpo-21719 [https://bugs.python.org/issue21719]: Added the st_file_attributes field to os.stat_result on Windows.

bpo-21832 [https://bugs.python.org/issue21832]: Require named tuple inputs to be exact strings.

bpo-21722 [https://bugs.python.org/issue21722]: The distutils “upload” command now exits with a non-zero return code when uploading fails. Patch by Martin Dengler.

bpo-21723 [https://bugs.python.org/issue21723]: asyncio.Queue: support any type of number (ex: float) for the maximum size. Patch written by Vajrasky Kok.

bpo-21711 [https://bugs.python.org/issue21711]: support for “site-python” directories has now been removed from the site module (it was deprecated in 3.4).

bpo-17552 [https://bugs.python.org/issue17552]: new socket.sendfile() method allowing a file to be sent over a socket by using high-performance os.sendfile() on UNIX. Patch by Giampaolo Rodola’.

bpo-18039 [https://bugs.python.org/issue18039]: dbm.dump.open() now always creates a new database when the flag has the value ‘n’. Patch by Claudiu Popa.

bpo-21326 [https://bugs.python.org/issue21326]: Add a new is_closed() method to asyncio.BaseEventLoop. run_forever() and run_until_complete() methods of asyncio.BaseEventLoop now raise an exception if the event loop was closed.

bpo-21766 [https://bugs.python.org/issue21766]: Prevent a security hole in CGIHTTPServer by URL unquoting paths before checking for a CGI script at that path.

bpo-21310 [https://bugs.python.org/issue21310]: Fixed possible resource leak in failed open().

bpo-21256 [https://bugs.python.org/issue21256]: Printout of keyword args should be in deterministic order in a mock function call. This will help to write better doctests.

bpo-21677 [https://bugs.python.org/issue21677]: Fixed chaining nonnormalized exceptions in io close() methods.

bpo-11709 [https://bugs.python.org/issue11709]: Fix the pydoc.help function to not fail when sys.stdin is not a valid file.

bpo-21515 [https://bugs.python.org/issue21515]: tempfile.TemporaryFile now uses os.O_TMPFILE flag is available.

bpo-13223 [https://bugs.python.org/issue13223]: Fix pydoc.writedoc so that the HTML documentation for methods that use ‘self’ in the example code is generated correctly.

bpo-21463 [https://bugs.python.org/issue21463]: In urllib.request, fix pruning of the FTP cache.

bpo-21618 [https://bugs.python.org/issue21618]: The subprocess module could fail to close open fds that were inherited by the calling process and already higher than POSIX resource limits would otherwise allow. On systems with a functioning /proc/self/fd or /dev/fd interface the max is now ignored and all fds are closed.

bpo-20383 [https://bugs.python.org/issue20383]: Introduce importlib.util.module_from_spec() as the preferred way to create a new module.

bpo-21552 [https://bugs.python.org/issue21552]: Fixed possible integer overflow of too long string lengths in the tkinter module on 64-bit platforms.

bpo-14315 [https://bugs.python.org/issue14315]: The zipfile module now ignores extra fields in the central directory that are too short to be parsed instead of letting a struct.unpack error bubble up as this “bad data” appears in many real world zip files in the wild and is ignored by other zip tools.

bpo-13742 [https://bugs.python.org/issue13742]: Added “key” and “reverse” parameters to heapq.merge(). (First draft of patch contributed by Simon Sapin.)

bpo-21402 [https://bugs.python.org/issue21402]: tkinter.ttk now works when default root window is not set.

bpo-3015 [https://bugs.python.org/issue3015]: _tkinter.create() now creates tkapp object with wantobject=1 by default.

bpo-10203 [https://bugs.python.org/issue10203]: sqlite3.Row now truly supports sequence protocol. In particular it supports reverse() and negative indices. Original patch by Claudiu Popa.

bpo-18807 [https://bugs.python.org/issue18807]: If copying (no symlinks) specified for a venv, then the python interpreter aliases (python, python3) are now created by copying rather than symlinking.

bpo-20197 [https://bugs.python.org/issue20197]: Added support for the WebP image type in the imghdr module. Patch by Fabrice Aneche and Claudiu Popa.

bpo-21513 [https://bugs.python.org/issue21513]: Speedup some properties of IP addresses (IPv4Address, IPv6Address) such as .is_private or .is_multicast.

bpo-21137 [https://bugs.python.org/issue21137]: Improve the repr for threading.Lock() and its variants by showing the “locked” or “unlocked” status. Patch by Berker Peksag.

bpo-21538 [https://bugs.python.org/issue21538]: The plistlib module now supports loading of binary plist files when reference or offset size is not a power of two.

bpo-21455 [https://bugs.python.org/issue21455]: Add a default backlog to socket.listen().

bpo-21525 [https://bugs.python.org/issue21525]: Most Tkinter methods which accepted tuples now accept lists too.

bpo-22166 [https://bugs.python.org/issue22166]: With the assistance of a new internal _codecs._forget_codec helping function, test_codecs now clears the encoding caches to avoid the appearance of a reference leak

bpo-22236 [https://bugs.python.org/issue22236]: Tkinter tests now don’t reuse default root window. New root window is created for every test class.

bpo-10744 [https://bugs.python.org/issue10744]: Fix PEP 3118 format strings on ctypes objects with a nontrivial shape.

bpo-20826 [https://bugs.python.org/issue20826]: Optimize ipaddress.collapse_addresses().

bpo-21487 [https://bugs.python.org/issue21487]: Optimize ipaddress.summarize_address_range() and ipaddress.{IPv4Network,IPv6Network}.subnets().

bpo-21486 [https://bugs.python.org/issue21486]: Optimize parsing of netmasks in ipaddress.IPv4Network and ipaddress.IPv6Network.

bpo-13916 [https://bugs.python.org/issue13916]: Disallowed the surrogatepass error handler for non UTF-* encodings.

bpo-20998 [https://bugs.python.org/issue20998]: Fixed re.fullmatch() of repeated single character pattern with ignore case. Original patch by Matthew Barnett.

bpo-21075 [https://bugs.python.org/issue21075]: fileinput.FileInput now reads bytes from standard stream if binary mode is specified. Patch by Sam Kimbrel.

bpo-19775 [https://bugs.python.org/issue19775]: Add a samefile() method to pathlib Path objects. Initial patch by Vajrasky Kok.

bpo-21226 [https://bugs.python.org/issue21226]: Set up modules properly in PyImport_ExecCodeModuleObject (and friends).

bpo-21398 [https://bugs.python.org/issue21398]: Fix a unicode error in the pydoc pager when the documentation contains characters not encodable to the stdout encoding.

bpo-16531 [https://bugs.python.org/issue16531]: ipaddress.IPv4Network and ipaddress.IPv6Network now accept an (address, netmask) tuple argument, so as to easily construct network objects from existing addresses.

bpo-21156 [https://bugs.python.org/issue21156]: importlib.abc.InspectLoader.source_to_code() is now a staticmethod.

bpo-21424 [https://bugs.python.org/issue21424]: Simplified and optimized heaqp.nlargest() and nmsmallest() to make fewer tuple comparisons.

bpo-21396 [https://bugs.python.org/issue21396]: Fix TextIOWrapper(…, write_through=True) to not force a flush() on the underlying binary stream. Patch by akira.

bpo-18314 [https://bugs.python.org/issue18314]: Unlink now removes junctions on Windows. Patch by Kim Gräsman

bpo-21088 [https://bugs.python.org/issue21088]: Bugfix for curses.window.addch() regression in 3.4.0. In porting to Argument Clinic, the first two arguments were reversed.

bpo-21407 [https://bugs.python.org/issue21407]: _decimal: The module now supports function signatures.

bpo-10650 [https://bugs.python.org/issue10650]: Remove the non-standard ‘watchexp’ parameter from the Decimal.quantize() method in the Python version. It had never been present in the C version.

bpo-21469 [https://bugs.python.org/issue21469]: Reduced the risk of false positives in robotparser by checking to make sure that robots.txt has been read or does not exist prior to returning True in can_fetch().

bpo-19414 [https://bugs.python.org/issue19414]: Have the OrderedDict mark deleted links as unusable. This gives an early failure if the link is deleted during iteration.

bpo-21421 [https://bugs.python.org/issue21421]: Add __slots__ to the MappingViews ABC. Patch by Josh Rosenberg.

bpo-21101 [https://bugs.python.org/issue21101]: Eliminate double hashing in the C speed-up code for collections.Counter().

bpo-21321 [https://bugs.python.org/issue21321]: itertools.islice() now releases the reference to the source iterator when the slice is exhausted. Patch by Anton Afanasyev.

bpo-21057 [https://bugs.python.org/issue21057]: TextIOWrapper now allows the underlying binary stream’s read() or read1() method to return an arbitrary bytes-like object (such as a memoryview). Patch by Nikolaus Rath.

bpo-20951 [https://bugs.python.org/issue20951]: SSLSocket.send() now raises either SSLWantReadError or SSLWantWriteError on a non-blocking socket if the operation would block. Previously, it would return 0. Patch by Nikolaus Rath.

bpo-13248 [https://bugs.python.org/issue13248]: removed previously deprecated asyncore.dispatcher __getattr__ cheap inheritance hack.

bpo-9815 [https://bugs.python.org/issue9815]: assertRaises now tries to clear references to local variables in the exception’s traceback.

bpo-19940 [https://bugs.python.org/issue19940]: ssl.cert_time_to_seconds() now interprets the given time string in the UTC timezone (as specified in RFC 5280), not the local timezone.

bpo-13204 [https://bugs.python.org/issue13204]: Calling sys.flags.__new__ would crash the interpreter, now it raises a TypeError.

bpo-19385 [https://bugs.python.org/issue19385]: Make operations on a closed dbm.dumb database always raise the same exception.

bpo-21207 [https://bugs.python.org/issue21207]: Detect when the os.urandom cached fd has been closed or replaced, and open it anew.

bpo-21291 [https://bugs.python.org/issue21291]: subprocess’s Popen.wait() is now thread safe so that multiple threads may be calling wait() or poll() on a Popen instance at the same time without losing the Popen.returncode value.

bpo-21127 [https://bugs.python.org/issue21127]: Path objects can now be instantiated from str subclass instances (such as numpy.str_).

bpo-15002 [https://bugs.python.org/issue15002]: urllib.response object to use _TemporaryFileWrapper (and _TemporaryFileCloser) facility. Provides a better way to handle file descriptor close. Patch contributed by Christian Theune.

bpo-12220 [https://bugs.python.org/issue12220]: mindom now raises a custom ValueError indicating it doesn’t support spaces in URIs instead of letting a ‘split’ ValueError bubble up.

bpo-21068 [https://bugs.python.org/issue21068]: The ssl.PROTOCOL* constants are now enum members.

bpo-21276 [https://bugs.python.org/issue21276]: posixmodule: Don’t define USE_XATTRS on KFreeBSD and the Hurd.

bpo-21262 [https://bugs.python.org/issue21262]: New method assert_not_called for Mock. It raises AssertionError if the mock has been called.

bpo-21238 [https://bugs.python.org/issue21238]: New keyword argument unsafe to Mock. It raises AttributeError incase of an attribute startswith assert or assret.

bpo-20896 [https://bugs.python.org/issue20896]: ssl.get_server_certificate() now uses PROTOCOL_SSLv23, not PROTOCOL_SSLv3, for maximum compatibility.

bpo-21239 [https://bugs.python.org/issue21239]: patch.stopall() didn’t work deterministically when the same name was patched more than once.

bpo-21203 [https://bugs.python.org/issue21203]: Updated fileConfig and dictConfig to remove inconsistencies. Thanks to Jure Koren for the patch.

bpo-21222 [https://bugs.python.org/issue21222]: Passing name keyword argument to mock.create_autospec now works.

bpo-21197 [https://bugs.python.org/issue21197]: Add lib64 -> lib symlink in venvs on 64-bit non-OS X POSIX.

bpo-17498 [https://bugs.python.org/issue17498]: Some SMTP servers disconnect after certain errors, violating strict RFC conformance. Instead of losing the error code when we issue the subsequent RSET, smtplib now returns the error code and defers raising the SMTPServerDisconnected error until the next command is issued.

bpo-17826 [https://bugs.python.org/issue17826]: setting an iterable side_effect on a mock function created by create_autospec now works. Patch by Kushal Das.

bpo-7776 [https://bugs.python.org/issue7776]: Fix Host: header and reconnection when using http.client.HTTPConnection.set_tunnel(). Patch by Nikolaus Rath.

bpo-20968 [https://bugs.python.org/issue20968]: unittest.mock.MagicMock now supports division. Patch by Johannes Baiter.

bpo-21529 [https://bugs.python.org/issue21529]: Fix arbitrary memory access in JSONDecoder.raw_decode with a negative second parameter. Bug reported by Guido Vranken. (See also: CVE-2014-4616)

bpo-21169 [https://bugs.python.org/issue21169]: getpass now handles non-ascii characters that the input stream encoding cannot encode by re-encoding using the replace error handler.

bpo-21171 [https://bugs.python.org/issue21171]: Fixed undocumented filter API of the rot13 codec. Patch by Berker Peksag.

bpo-20539 [https://bugs.python.org/issue20539]: Improved math.factorial error message for large positive inputs and changed exception type (OverflowError -> ValueError) for large negative inputs.

bpo-21172 [https://bugs.python.org/issue21172]: isinstance check relaxed from dict to collections.Mapping.

bpo-21155 [https://bugs.python.org/issue21155]: asyncio.EventLoop.create_unix_server() now raises a ValueError if path and sock are specified at the same time.

bpo-21136 [https://bugs.python.org/issue21136]: Avoid unnecessary normalization of Fractions resulting from power and other operations. Patch by Raymond Hettinger.

bpo-17621 [https://bugs.python.org/issue17621]: Introduce importlib.util.LazyLoader.

bpo-21076 [https://bugs.python.org/issue21076]: signal module constants were turned into enums. Patch by Giampaolo Rodola’.

bpo-20636 [https://bugs.python.org/issue20636]: Improved the repr of Tkinter widgets.

bpo-19505 [https://bugs.python.org/issue19505]: The items, keys, and values views of OrderedDict now support reverse iteration using reversed().

bpo-21149 [https://bugs.python.org/issue21149]: Improved thread-safety in logging cleanup during interpreter shutdown. Thanks to Devin Jeanpierre for the patch.

bpo-21058 [https://bugs.python.org/issue21058]: Fix a leak of file descriptor in tempfile.NamedTemporaryFile(), close the file descriptor if io.open() fails

bpo-21200 [https://bugs.python.org/issue21200]: Return None from pkgutil.get_loader() when __spec__ is missing.

bpo-21013 [https://bugs.python.org/issue21013]: Enhance ssl.create_default_context() when used for server side sockets to provide better security by default.

bpo-20145 [https://bugs.python.org/issue20145]: assertRaisesRegex and assertWarnsRegex now raise a TypeError if the second argument is not a string or compiled regex.

bpo-20633 [https://bugs.python.org/issue20633]: Replace relative import by absolute import.

bpo-20980 [https://bugs.python.org/issue20980]: Stop wrapping exception when using ThreadPool.

bpo-21082 [https://bugs.python.org/issue21082]: In os.makedirs, do not set the process-wide umask. Note this changes behavior of makedirs when exist_ok=True.

bpo-20990 [https://bugs.python.org/issue20990]: Fix issues found by pyflakes for multiprocessing.

bpo-21015 [https://bugs.python.org/issue21015]: SSL contexts will now automatically select an elliptic curve for ECDH key exchange on OpenSSL 1.0.2 and later, and otherwise default to “prime256v1”.

bpo-21000 [https://bugs.python.org/issue21000]: Improve the command-line interface of json.tool.

bpo-20995 [https://bugs.python.org/issue20995]: Enhance default ciphers used by the ssl module to enable better security and prioritize perfect forward secrecy.

bpo-20884 [https://bugs.python.org/issue20884]: Don’t assume that __file__ is defined on importlib.__init__.

bpo-21499 [https://bugs.python.org/issue21499]: Ignore __builtins__ in several test_importlib.test_api tests.

bpo-20627 [https://bugs.python.org/issue20627]: xmlrpc.client.ServerProxy is now a context manager.

bpo-19165 [https://bugs.python.org/issue19165]: The formatter module now raises DeprecationWarning instead of PendingDeprecationWarning.

bpo-13936 [https://bugs.python.org/issue13936]: Remove the ability of datetime.time instances to be considered false in boolean contexts.

bpo-18931 [https://bugs.python.org/issue18931]: selectors module now supports /dev/poll on Solaris. Patch by Giampaolo Rodola’.

bpo-19977 [https://bugs.python.org/issue19977]: When the LC_TYPE locale is the POSIX locale (C locale), sys.stdin and sys.stdout are now using the surrogateescape error handler, instead of the strict error handler.

bpo-20574 [https://bugs.python.org/issue20574]: Implement incremental decoder for cp65001 code (Windows code page 65001, Microsoft UTF-8).

bpo-20879 [https://bugs.python.org/issue20879]: Delay the initialization of encoding and decoding tables for base32, ascii85 and base85 codecs in the base64 module, and delay the initialization of the unquote_to_bytes() table of the urllib.parse module, to not waste memory if these modules are not used.

bpo-19157 [https://bugs.python.org/issue19157]: Include the broadcast address in the usuable hosts for IPv6 in ipaddress.

bpo-11599 [https://bugs.python.org/issue11599]: When an external command (e.g. compiler) fails, distutils now prints out the whole command line (instead of just the command name) if the environment variable DISTUTILS_DEBUG is set.

bpo-4931 [https://bugs.python.org/issue4931]: distutils should not produce unhelpful “error: None” messages anymore. distutils.util.grok_environment_error is kept but doc-deprecated.

bpo-20875 [https://bugs.python.org/issue20875]: Prevent possible gzip “‘read’ is not defined” NameError. Patch by Claudiu Popa.

bpo-11558 [https://bugs.python.org/issue11558]: email.message.Message.attach now returns a more useful error message if attach is called on a message for which is_multipart is False.

bpo-20283 [https://bugs.python.org/issue20283]: RE pattern methods now accept the string keyword parameters as documented. The pattern and source keyword parameters are left as deprecated aliases.

bpo-20778 [https://bugs.python.org/issue20778]: Fix modulefinder to work with bytecode-only modules.

bpo-20791 [https://bugs.python.org/issue20791]: copy.copy() now doesn’t make a copy when the input is a bytes object. Initial patch by Peter Otten.

bpo-19748 [https://bugs.python.org/issue19748]: On AIX, time.mktime() now raises an OverflowError for year outsize range [1902; 2037].

bpo-19573 [https://bugs.python.org/issue19573]: inspect.signature: Use enum for parameter kind constants.

bpo-20726 [https://bugs.python.org/issue20726]: inspect.signature: Make Signature and Parameter picklable.

bpo-17373 [https://bugs.python.org/issue17373]: Add inspect.Signature.from_callable method.

bpo-20378 [https://bugs.python.org/issue20378]: Improve repr of inspect.Signature and inspect.Parameter.

bpo-20816 [https://bugs.python.org/issue20816]: Fix inspect.getcallargs() to raise correct TypeError for missing keyword-only arguments. Patch by Jeremiah Lowin.

bpo-20817 [https://bugs.python.org/issue20817]: Fix inspect.getcallargs() to fail correctly if more than 3 arguments are missing. Patch by Jeremiah Lowin.

bpo-6676 [https://bugs.python.org/issue6676]: Ensure a meaningful exception is raised when attempting to parse more than one XML document per pyexpat xmlparser instance. (Original patches by Hirokazu Yamamoto and Amaury Forgeot d’Arc, with suggested wording by David Gutteridge)

bpo-21117 [https://bugs.python.org/issue21117]: Fix inspect.signature to better support functools.partial. Due to the specifics of functools.partial implementation, positional-or-keyword arguments passed as keyword arguments become keyword-only.

bpo-20334 [https://bugs.python.org/issue20334]: inspect.Signature and inspect.Parameter are now hashable. Thanks to Antony Lee for bug reports and suggestions.

bpo-15916 [https://bugs.python.org/issue15916]: doctest.DocTestSuite returns an empty unittest.TestSuite instead of raising ValueError if it finds no tests

bpo-21209 [https://bugs.python.org/issue21209]: Fix asyncio.tasks.CoroWrapper to workaround a bug in yield-from implementation in CPythons prior to 3.4.1.

asyncio: Add gi_{frame,running,code} properties to CoroWrapper (upstream bpo-163 [https://bugs.python.org/issue163]).

bpo-21311 [https://bugs.python.org/issue21311]: Avoid exception in _osx_support with non-standard compiler configurations. Patch by John Szakmeister.

bpo-11571 [https://bugs.python.org/issue11571]: Ensure that the turtle window becomes the topmost window when launched on OS X.

bpo-21801 [https://bugs.python.org/issue21801]: Validate that __signature__ is None or an instance of Signature.

bpo-21923 [https://bugs.python.org/issue21923]: Prevent AttributeError in distutils.sysconfig.customize_compiler due to possible uninitialized _config_vars.

bpo-21323 [https://bugs.python.org/issue21323]: Fix http.server to again handle scripts in CGI subdirectories, broken by the fix for security bpo-19435 [https://bugs.python.org/issue19435]. Patch by Zach Byrne.

bpo-22733 [https://bugs.python.org/issue22733]: Fix ffi_prep_args not zero-extending argument values correctly on 64-bit Windows.

bpo-23302 [https://bugs.python.org/issue23302]: Default to TCP_NODELAY=1 upon establishing an HTTPConnection. Removed use of hard-coded MSS as it’s an optimization that’s no longer needed with Nagle disabled.

IDLE

bpo-20577 [https://bugs.python.org/issue20577]: Configuration of the max line length for the FormatParagraph extension has been moved from the General tab of the Idle preferences dialog to the FormatParagraph tab of the Config Extensions dialog. Patch by Tal Einat.

bpo-16893 [https://bugs.python.org/issue16893]: Update Idle doc chapter to match current Idle and add new information.

bpo-3068 [https://bugs.python.org/issue3068]: Add Idle extension configuration dialog to Options menu. Changes are written to HOME/.idlerc/config-extensions.cfg. Original patch by Tal Einat.

bpo-16233 [https://bugs.python.org/issue16233]: A module browser (File : Class Browser, Alt+C) requires an editor window with a filename. When Class Browser is requested otherwise, from a shell, output window, or ‘Untitled’ editor, Idle no longer displays an error box. It now pops up an Open Module box (Alt+M). If a valid name is entered and a module is opened, a corresponding browser is also opened.

bpo-4832 [https://bugs.python.org/issue4832]: Save As to type Python files automatically adds .py to the name you enter (even if your system does not display it). Some systems automatically add .txt when type is Text files.

bpo-21986 [https://bugs.python.org/issue21986]: Code objects are not normally pickled by the pickle module. To match this, they are no longer pickled when running under Idle.

bpo-17390 [https://bugs.python.org/issue17390]: Adjust Editor window title; remove ‘Python’, move version to end.

bpo-14105 [https://bugs.python.org/issue14105]: Idle debugger breakpoints no longer disappear when inserting or deleting lines.

bpo-17172 [https://bugs.python.org/issue17172]: Turtledemo can now be run from Idle. Currently, the entry is on the Help menu, but it may move to Run. Patch by Ramchandra Apt and Lita Cho.

bpo-21765 [https://bugs.python.org/issue21765]: Add support for non-ascii identifiers to HyperParser.

bpo-21940 [https://bugs.python.org/issue21940]: Add unittest for WidgetRedirector. Initial patch by Saimadhav Heblikar.

bpo-18592 [https://bugs.python.org/issue18592]: Add unittest for SearchDialogBase. Patch by Phil Webster.

bpo-21694 [https://bugs.python.org/issue21694]: Add unittest for ParenMatch. Patch by Saimadhav Heblikar.

bpo-21686 [https://bugs.python.org/issue21686]: add unittest for HyperParser. Original patch by Saimadhav Heblikar.

bpo-12387 [https://bugs.python.org/issue12387]: Add missing upper(lower)case versions of default Windows key bindings for Idle so Caps Lock does not disable them. Patch by Roger Serwy.

bpo-21695 [https://bugs.python.org/issue21695]: Closing a Find-in-files output window while the search is still in progress no longer closes Idle.

bpo-18910 [https://bugs.python.org/issue18910]: Add unittest for textView. Patch by Phil Webster.

bpo-18292 [https://bugs.python.org/issue18292]: Add unittest for AutoExpand. Patch by Saihadhav Heblikar.

bpo-18409 [https://bugs.python.org/issue18409]: Add unittest for AutoComplete. Patch by Phil Webster.

bpo-21477 [https://bugs.python.org/issue21477]: htest.py - Improve framework, complete set of tests. Patches by Saimadhav Heblikar

bpo-18104 [https://bugs.python.org/issue18104]: Add idlelib/idle_test/htest.py with a few sample tests to begin consolidating and improving human-validated tests of Idle. Change other files as needed to work with htest. Running the module as __main__ runs all tests.

bpo-21139 [https://bugs.python.org/issue21139]: Change default paragraph width to 72, the PEP 8 recommendation.

bpo-21284 [https://bugs.python.org/issue21284]: Paragraph reformat test passes after user changes reformat width.

bpo-17654 [https://bugs.python.org/issue17654]: Ensure IDLE menus are customized properly on OS X for non-framework builds and for all variants of Tk.

bpo-23180 [https://bugs.python.org/issue23180]: Rename IDLE “Windows” menu item to “Window”. Patch by Al Sweigart.

构建

bpo-15506 [https://bugs.python.org/issue15506]: Use standard PKG_PROG_PKG_CONFIG autoconf macro in the configure script.

bpo-22935 [https://bugs.python.org/issue22935]: Allow the ssl module to be compiled if openssl doesn’t support SSL 3.

bpo-22592 [https://bugs.python.org/issue22592]: 放弃对 Borland C 编译器构建Python的支持。distutils模块仍然支持它来构建扩展。

bpo-22591 [https://bugs.python.org/issue22591]: 放弃对 MS-DOS 的支持，尤其是 DJGPP 编译器（GCC的MS-DOS端口）。

bpo-16537 [https://bugs.python.org/issue16537]: Check whether self.extensions is empty in setup.py. Patch by Jonathan Hosmer.

bpo-22359 [https://bugs.python.org/issue22359]: Remove incorrect uses of recursive make. Patch by Jonas Wagner.

bpo-21958 [https://bugs.python.org/issue21958]: Define HAVE_ROUND when building with Visual Studio 2013 and above. Patch by Zachary Turner.

bpo-18093 [https://bugs.python.org/issue18093]: the programs that embed the CPython runtime are now in a separate “Programs” directory, rather than being kept in the Modules directory.

bpo-15759 [https://bugs.python.org/issue15759]: “make suspicious”, “make linkcheck” and “make doctest” in Doc/ now display special message when and only when there are failures.

bpo-21141 [https://bugs.python.org/issue21141]: The Windows build process no longer attempts to find Perl, instead relying on OpenSSL source being configured and ready to build. The PCbuild\build_ssl.py script has been re-written and re-named to PCbuild\prepare_ssl.py, and takes care of configuring OpenSSL source for both 32 and 64 bit platforms. OpenSSL sources obtained from svn.python.org will always be pre-configured and ready to build.

bpo-21037 [https://bugs.python.org/issue21037]: Add a build option to enable AddressSanitizer support.

bpo-19962 [https://bugs.python.org/issue19962]: The Windows build process now creates “python.bat” in the root of the source tree, which passes all arguments through to the most recently built interpreter.

bpo-21285 [https://bugs.python.org/issue21285]: Refactor and fix curses configure check to always search in a ncursesw directory.

bpo-15234 [https://bugs.python.org/issue15234]: For BerkelyDB and Sqlite, only add the found library and include directories if they aren’t already being searched. This avoids an explicit runtime library dependency.

bpo-17861 [https://bugs.python.org/issue17861]: Tools/scripts/generate_opcode_h.py automatically regenerates Include/opcode.h from Lib/opcode.py if the latter gets any change.

bpo-20644 [https://bugs.python.org/issue20644]: OS X installer build support for documentation build changes in 3.4.1: assume externally supplied sphinx-build is available in /usr/bin.

bpo-20022 [https://bugs.python.org/issue20022]: Eliminate use of deprecated bundlebuilder in OS X builds.

bpo-15968 [https://bugs.python.org/issue15968]: Incorporated Tcl, Tk, and Tix builds into the Windows build solution.

bpo-17095 [https://bugs.python.org/issue17095]: Fix Modules/Setup shared support.

bpo-21811 [https://bugs.python.org/issue21811]: Anticipated fixes to support OS X versions > 10.9.

bpo-21166 [https://bugs.python.org/issue21166]: Prevent possible segfaults and other random failures of python <<<<<<< HEAD --generate-posix-vars in pybuilddir.txt build target. ======= –generate-posix-vars in pybuilddir.txt build target. >>>>>>> 3f819ca138… bpo-35110 [https://bugs.python.org/issue35110]: Fix unintentional spaces around hyphens and dashes. (GH-10231)

bpo-18096 [https://bugs.python.org/issue18096]: Fix library order returned by python-config.

bpo-17219 [https://bugs.python.org/issue17219]: Add library build dir for Python extension cross-builds.

bpo-22919 [https://bugs.python.org/issue22919]: Windows build updated to support VC 14.0 (Visual Studio 2015), which will be used for the official release.

bpo-21236 [https://bugs.python.org/issue21236]: Build _msi.pyd with cabinet.lib instead of fci.lib

bpo-17128 [https://bugs.python.org/issue17128]: Use private version of OpenSSL for OS X 10.5+ installer.

C API

bpo-14203 [https://bugs.python.org/issue14203]: Remove obsolete support for view==NULL in PyBuffer_FillInfo(), bytearray_getbuffer(), bytesiobuf_getbuffer() and array_buffer_getbuf(). All functions now raise BufferError in that case.

bpo-22445 [https://bugs.python.org/issue22445]: PyBuffer_IsContiguous() now implements precise contiguity tests, compatible with NumPy’s NPY_RELAXED_STRIDES_CHECKING compilation flag. Previously the function reported false negatives for corner cases.

bpo-22079 [https://bugs.python.org/issue22079]: PyType_Ready() now checks that statically allocated type has no dynamically allocated bases.

bpo-22453 [https://bugs.python.org/issue22453]: Removed non-documented macro PyObject_REPR().

bpo-18395 [https://bugs.python.org/issue18395]: Rename _Py_char2wchar() to Py_DecodeLocale(), rename _Py_wchar2char() to Py_EncodeLocale(), and document these functions.

bpo-21233 [https://bugs.python.org/issue21233]: Add new C functions: PyMem_RawCalloc(), PyMem_Calloc(), PyObject_Calloc(), _PyObject_GC_Calloc(). bytes(int) is now using calloc() instead of malloc() for large objects which is faster and use less memory.

bpo-20942 [https://bugs.python.org/issue20942]: PyImport_ImportFrozenModuleObject() no longer sets __file__ to match what importlib does; this affects _frozen_importlib as well as any module loaded using imp.init_frozen().

文档

bpo-19548 [https://bugs.python.org/issue19548]: Update the codecs module documentation to better cover the distinction between text encodings and other codecs, together with other clarifications. Patch by Martin Panter.

bpo-22394 [https://bugs.python.org/issue22394]: Doc/Makefile now supports make venv PYTHON=../python to create a venv for generating the documentation, e.g., make html PYTHON=venv/bin/python3.

bpo-21514 [https://bugs.python.org/issue21514]: The documentation of the json module now refers to new JSON RFC 7159 instead of obsoleted RFC 4627.

bpo-21777 [https://bugs.python.org/issue21777]: The binary sequence methods on bytes and bytearray are now documented explicitly, rather than assuming users will be able to derive the expected behaviour from the behaviour of the corresponding str methods.

bpo-6916 [https://bugs.python.org/issue6916]: undocument deprecated asynchat.fifo class.

bpo-17386 [https://bugs.python.org/issue17386]: Expanded functionality of the Doc/make.bat script to make it much more comparable to Doc/Makefile.

bpo-21312 [https://bugs.python.org/issue21312]: Update the thread_foobar.h template file to include newer threading APIs. Patch by Jack McCracken.

bpo-21043 [https://bugs.python.org/issue21043]: Remove the recommendation for specific CA organizations and to mention the ability to load the OS certificates.

bpo-20765 [https://bugs.python.org/issue20765]: Add missing documentation for PurePath.with_name() and PurePath.with_suffix().

bpo-19407 [https://bugs.python.org/issue19407]: New package installation and distribution guides based on the Python Packaging Authority tools. Existing guides have been retained as legacy links from the distutils docs, as they still contain some required reference material for tool developers that isn’t recorded anywhere else.

bpo-19697 [https://bugs.python.org/issue19697]: Document cases where __main__.__spec__ is None.

测试

bpo-18982 [https://bugs.python.org/issue18982]: Add tests for CLI of the calendar module.

bpo-19548 [https://bugs.python.org/issue19548]: Added some additional checks to test_codecs to ensure that statements in the updated documentation remain accurate. Patch by Martin Panter.

bpo-22838 [https://bugs.python.org/issue22838]: All test_re tests now work with unittest test discovery.

bpo-22173 [https://bugs.python.org/issue22173]: Update lib2to3 tests to use unittest test discovery.

bpo-16000 [https://bugs.python.org/issue16000]: Convert test_curses to use unittest.

bpo-21456 [https://bugs.python.org/issue21456]: Skip two tests in test_urllib2net.py if _ssl module not present. Patch by Remi Pointel.

bpo-20746 [https://bugs.python.org/issue20746]: Fix test_pdb to run in refleak mode (-R). Patch by Xavier de Gaye.

bpo-22060 [https://bugs.python.org/issue22060]: test_ctypes has been somewhat cleaned up and simplified; it now uses unittest test discovery to find its tests.

bpo-22104 [https://bugs.python.org/issue22104]: regrtest.py no longer holds a reference to the suite of tests loaded from test modules that don’t define test_main().

bpo-22111 [https://bugs.python.org/issue22111]: Assorted cleanups in test_imaplib. Patch by Milan Oberkirch.

bpo-22002 [https://bugs.python.org/issue22002]: Added load_package_tests function to test.support and used it to implement/augment test discovery in test_asyncio, test_email, test_importlib, test_json, and test_tools.

bpo-21976 [https://bugs.python.org/issue21976]: Fix test_ssl to accept LibreSSL version strings. Thanks to William Orr.

bpo-21918 [https://bugs.python.org/issue21918]: Converted test_tools from a module to a package containing separate test files for each tested script.

bpo-9554 [https://bugs.python.org/issue9554]: Use modern unittest features in test_argparse. Initial patch by Denver Coneybeare and Radu Voicilas.

bpo-20155 [https://bugs.python.org/issue20155]: Changed HTTP method names in failing tests in test_httpservers so that packet filtering software (specifically Windows Base Filtering Engine) does not interfere with the transaction semantics expected by the tests.

bpo-19493 [https://bugs.python.org/issue19493]: Refactored the ctypes test package to skip tests explicitly rather than silently.

bpo-18492 [https://bugs.python.org/issue18492]: All resources are now allowed when tests are not run by regrtest.py.

bpo-21634 [https://bugs.python.org/issue21634]: Fix pystone micro-benchmark: use floor division instead of true division to benchmark integers instead of floating point numbers. Set pystone version to 1.2. Patch written by Lennart Regebro.

bpo-21605 [https://bugs.python.org/issue21605]: Added tests for Tkinter images.

bpo-21493 [https://bugs.python.org/issue21493]: Added test for ntpath.expanduser(). Original patch by Claudiu Popa.

bpo-19925 [https://bugs.python.org/issue19925]: Added tests for the spwd module. Original patch by Vajrasky Kok.

bpo-21522 [https://bugs.python.org/issue21522]: Added Tkinter tests for Listbox.itemconfigure(), PanedWindow.paneconfigure(), and Menu.entryconfigure().

bpo-17756 [https://bugs.python.org/issue17756]: Fix test_code test when run from the installed location.

bpo-17752 [https://bugs.python.org/issue17752]: Fix distutils tests when run from the installed location.

bpo-18604 [https://bugs.python.org/issue18604]: Consolidated checks for GUI availability. All platforms now at least check whether Tk can be instantiated when the GUI resource is requested.

bpo-21275 [https://bugs.python.org/issue21275]: 修复KFreeBSD上的套接字测试。

bpo-21223 [https://bugs.python.org/issue21223]: Pass test_site/test_startup_imports when some of the extensions are built as builtins.

bpo-20635 [https://bugs.python.org/issue20635]: 添加了针对Tk几何管理器的测试。

增加freeze包的测试用例。

bpo-20743 [https://bugs.python.org/issue20743]: 修复test_tcl中的引用泄漏。

bpo-21097 [https://bugs.python.org/issue21097]: 将test_namespace_pkgs 移至 test_importlib。

bpo-21503 [https://bugs.python.org/issue21503]: Use test_both() consistently in test_importlib.

bpo-20939 [https://bugs.python.org/issue20939]: Avoid various network test failures due to new redirect of http://www.python.org/ to https://www.python.org: use http://www.example.com instead.

bpo-20668 [https://bugs.python.org/issue20668]: asyncio tests no longer rely on tests.txt file. (Patch by Vajrasky Kok)

bpo-21093 [https://bugs.python.org/issue21093]: Prevent failures of ctypes test_macholib on OS X if a copy of libz exists in $HOME/lib or /usr/local/lib.

bpo-22770 [https://bugs.python.org/issue22770]: Prevent some Tk segfaults on OS X when running gui tests.

bpo-23211 [https://bugs.python.org/issue23211]: Workaround test_logging failure on some OS X 10.6 systems.

bpo-23345 [https://bugs.python.org/issue23345]: Prevent test_ssl failures with large OpenSSL patch level values (like 0.9.8zc).

工具/示例

bpo-22314 [https://bugs.python.org/issue22314]: pydoc now works when the LINES environment variable is set.

bpo-22615 [https://bugs.python.org/issue22615]: Argument Clinic now supports the “type” argument for the int converter. This permits using the int converter with enums and typedefs.

bpo-20076 [https://bugs.python.org/issue20076]: The makelocalealias.py script no longer ignores UTF-8 mapping.

bpo-20079 [https://bugs.python.org/issue20079]: The makelocalealias.py script now can parse the SUPPORTED file from glibc sources and supports command line options for source paths.

bpo-22201 [https://bugs.python.org/issue22201]: Command-line interface of the zipfile module now correctly extracts ZIP files with directory entries. Patch by Ryan Wilson.

bpo-22120 [https://bugs.python.org/issue22120]: For functions using an unsigned integer return converter, Argument Clinic now generates a cast to that type for the comparison to -1 in the generated code. (This suppresses a compilation warning.)

bpo-18974 [https://bugs.python.org/issue18974]: Tools/scripts/diff.py now uses argparse instead of optparse.

bpo-21906 [https://bugs.python.org/issue21906]: 让Tools/scripts/md5sum.py可以工作在Python 3。由Zachary Ware提供补丁。

bpo-21629 [https://bugs.python.org/issue21629]: Fix Argument Clinic’s “–converters” feature.

为2to3添加 yield from 的支持。

为2to3添加对PEP 465矩阵乘法运算符的支持。

bpo-16047 [https://bugs.python.org/issue16047]: Fix module exception list and __file__ handling in freeze. Patch by Meador Inge.

bpo-11824 [https://bugs.python.org/issue11824]: Consider ABI tags in freeze. Patch by Meador Inge.

bpo-20535 [https://bugs.python.org/issue20535]: PYTHONWARNING no longer affects the run_tests.py script. Patch by Arfrever Frehtes Taifersar Arahesis.

Windows

bpo-23260 [https://bugs.python.org/issue23260]: 更新Windows安装器

The bundled version of Tcl/Tk has been updated to 8.6.3. The most visible result of this change is the addition of new native file dialogs when running on Windows Vista or newer. See Tcl/Tk’s TIP 432 for more information. Also, this version of Tcl/Tk includes support for Windows 10.

bpo-17896 [https://bugs.python.org/issue17896]: The Windows build scripts now expect external library sources to be in PCbuild\..\externals rather than PCbuild\..\...

bpo-17717 [https://bugs.python.org/issue17717]: The Windows build scripts now use a copy of NASM pulled from svn.python.org to build OpenSSL.

bpo-21907 [https://bugs.python.org/issue21907]: Improved the batch scripts provided for building Python.

bpo-22644 [https://bugs.python.org/issue22644]: OpenSSL的捆绑版本已更新到1.0.1j。

bpo-10747 [https://bugs.python.org/issue10747]: Use versioned labels in the Windows start menu. Patch by Olive Kilburn.

bpo-22980 [https://bugs.python.org/issue22980]: .pyd files with a version and platform tag (for example, “.cp35-win32.pyd”) will now be loaded in preference to those without tags.

（有关旧版本的信息，请参阅HISTORY文件。)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python的新变化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

Python 教程

Python 是一种易于学习又功能强大的编程语言。它提供了高效的高层次的数据结构，还有简单有效的面向对象编程。Python 优雅的语法和动态类型，以及解释型语言的本质，使它成为在很多领域多数平台上写脚本和快速开发应用的理想语言。

多数平台上的 Python 解释器以及丰富的标准库的源码和可执行文件，都可以在 Python 官网 https://www.python.org/ 免费自由地下载并分享。这个网站上也提供一些链接，包括第三方 Python 模块、程序、工具等，以及额外的文档。

Python 解释器易于扩展，可以使用 C 或 C++（或者其他可以从 C 调用的语言）扩展新的功能和数据类型。Python 也可用作可定制化软件中的扩展程序语言。

这个教程非正式地介绍 Python 语言和系统的基本概念和功能。最好在阅读的时候有一个 Python 解释器做一些练习，不过所有的例子都是相互独立的，所以这个教程也可以离线阅读。

有关标准的对象和模块，参阅 Python 标准库。Python语言参考 提供了更正式的语言定义。要写 C 或者 C++ 扩展，参考 扩展和嵌入 Python 解释器 和 Python/C API 参考手册。也有不少书籍深入讲解 Python。

这个教程并不试图完整包含每一个功能，甚至常用功能可能也没有全部涉及。这个教程只介绍 Python 中最值得注意的功能，也会让你体会到这个语言的风格特色。学习完这个教程，你将能够阅读和编写 Python 模块和程序，也可以开始学习更多的 Python 库模块，详见 Python 标准库。

术语对照表 也很值得一读。

1. 课前甜点

2. 使用 Python 解释器

2.1. 调用解释器

2.1.1. 传入参数

2.1.2. 交互模式

2.2. 解释器的运行环境

2.2.1. 源文件的字符编码

3. Python 的非正式介绍

3.1. Python 作为计算器使用

3.1.1. 数字

3.1.2. 字符串

3.1.3. 列表

3.2. 走向编程的第一步

4. 其他流程控制工具

4.1. if Statements

4.2. for Statements

4.3. range() 函数

4.4. break and continue Statements, and else Clauses on Loops

4.5. pass Statements

4.6. 定义函数

4.7. 函数定义的更多形式

4.7.1. 参数默认值

4.7.2. 关键字参数

4.7.3. 任意的参数列表

4.7.4. 解包参数列表

4.7.5. Lambda 表达式

4.7.6. 文档字符串

4.7.7. 函数标注

4.8. 小插曲：编码风格

5. 数据结构

5.1. 列表的更多特性

5.1.1. 列表作为栈使用

5.1.2. 列表作为队列使用

5.1.3. 列表推导式

5.1.4. 嵌套的列表推导式

5.2. The del statement

5.3. 元组和序列

5.4. 集合

5.5. 字典

5.6. 循环的技巧

5.7. 深入条件控制

5.8. 比较序列和其他类型

6. 模块

6.1. 更多有关模块的信息

6.1.1. 以脚本的方式执行模块

6.1.2. 模块搜索路径

6.1.3. “编译过的”Python文件

6.2. 标准模块

6.3. dir() 函数

6.4. 包

6.4.1. 从包中导入 *

6.4.2. 子包参考

6.4.3. 多个目录中的包

7. 输入输出

7.1. 更漂亮的输出格式

7.1.1. 旧的字符串格式化方法

7.2. 读写文件

7.2.1. 文件对象的方法

7.2.2. 使用 json 保存结构化数据

8. 错误和异常

8.1. 语法错误

8.2. 异常

8.3. 处理异常

8.4. 抛出异常

8.5. 用户自定义异常

8.6. 定义清理操作

8.7. 预定义的清理操作

9. 类

9.1. 名称和对象

9.2. Python 作用域和命名空间

9.2.1. 作用域和命名空间示例

9.3. 初探类

9.3.1. 类定义语法

9.3.2. 类对象

9.3.3. 实例对象

9.3.4. 方法对象

9.3.5. 类和实例变量

9.4. 补充说明

9.5. 继承

9.5.1. 多重继承

9.6. 私有变量

9.7. 杂项说明

9.8. 迭代器

9.9. 生成器

9.10. 生成器表达式

10. 标准库简介

10.1. 操作系统接口

10.2. 文件通配符

10.3. 命令行参数

10.4. 错误输出重定向和程序终止

10.5. 字符串模式匹配

10.6. 数学

10.7. 互联网访问

10.8. 日期和时间

10.9. 数据压缩

10.10. 性能测量

10.11. 质量控制

10.12. 自带电池

11. 标准库简介 —— 第二部分

11.1. 格式化输出

11.2. 模板

11.3. 使用二进制数据记录格式

11.4. 多线程

11.5. 日志记录

11.6. 弱引用

11.7. 用于操作列表的工具

11.8. 十进制浮点运算

12. 虚拟环境和包

12.1. 概述

12.2. 创建虚拟环境

12.3. 使用pip管理包

13. 接下来？

14. 交互式编辑和编辑历史

14.1. Tab 补全和编辑历史

14.2. 默认交互式解释器的替代品

15. 浮点算术：争议和限制

15.1. 表示性错误

16. 附录

16.1. 交互模式

16.1.1. 错误处理

16.1.2. 可执行的Python脚本

16.1.3. 交互式启动文件

16.1.4. 定制模块

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

1. 课前甜点

如果你经常在电脑上工作，总会有些任务会想让它自动化。比如，对一大堆文本文件进行查找替换，对很多照片文件按照比较复杂的规则重命名并放入不同的文件夹。也可能你想写一个小型的数据库应用，一个特定的图形界面应用，或者一个简单的游戏。

如果你是专业的软件开发人员，你可能需要编写一些 C/C++/Java 库，但总觉得通常的 编写、编译、测试、再次编译 流程太慢了。可能给这样的库写一组测试，就是很麻烦的工作了。或许你写了个软件，可以支持插件扩展语言，但你不想为了自己这一个应用，专门设计和实现一种新语言了。

那么，Python 正好能满足你的需要。

对于这些任务，你也可以写 Unix shell 脚本或者 Windows 批处理完成，但是 shell 脚本最擅长移动文件和替换文本，并不适合 GUI 界面或者游戏开发。你可以写一个 C/C++/Java 程序，但是可能初稿都要很长的开发时间。Python 的使用则更加简单，可以在 Windows，Mac OS X，以及 Unix 操作系统上使用，而且可以帮你更快地完成工作。

Python 很容易使用，但它是一种真正的编程语言，提供了很多数据结构，也支持大型程序，远超 shell 脚本或批处理文件的功能。Python 还提供比 C 语言更多的错误检查，而且作为一种 “超高级语言”，它有高级的内置数据类型，比如灵活的数组和字典。正因为这些更加通用的数据类型，Python 能够应付更多的问题，超过 Awk 甚至 Perl，而且很多东西在 Python 中至少和那些语言同样简单。

Python 允许你将程序划分为能在其他的 Python 程序中重复利用的模块。它内置了很多的标准模块，你可以在此基础上开发程序——也可以作为例子，开始学习 Python 编程。例如，一切内置模块提供诸如文件输入输出、系统调用、套接字、甚至图形界面接口工作包比如 Tk。

Python 是一种解释型语言，在程序开发阶段可以为你节省大量时间，因为不需要编译和链接。解释器可以交互式使用，这样就可以方便地尝试语言特性，写一些一次性的程序，或者在自底向上的程序开发中测试功能。它也是一个顺手的桌面计算器。

Python 程序的书写是紧凑而易读的。Python 代码通常比同样功能的 C，C++，Java 代码要短很多，有如下几个原因：

高级数据类型允许在一个表达式中表示复杂的操作；

代码块的划分是按照缩进而不是成对的花括号；

不需要预先定义变量或参数。

Python 是 “可扩展的”：如果你知道怎么写 C 语言程序，就能很容易地给解释器添加新的内置函数或模块，不论是让关键的操作以最高速度运行，还是把 Python 程序链接到只提供预编译程序的库（比如硬件相关的图形库）。一旦你真正链接上了，就能在 Python 解释器中扩展或者控制 C 语言编写的应用了。

顺便提一下，这种语言的名字（python 一词直译为 “蟒蛇”）得名自 BBC 节目 “Monty Python的飞行马戏团” ，而与爬行动物没有关系。在文档中用 Monty Python 来开玩笑不只是被允许的，还是被推荐的！

现在你已经对 Python 跃跃欲试了，想要深入了解一些细节了。因为学习语言的最佳方式是使用它，本教程邀请你一边阅读，一边在 Python 解释器中玩耍。

在下一章节，会讲解使用解释器的方法。看起来相当枯燥，但是对于尝试后续的例子来说，是非常关键的。

教程的其他部分将通过示例介绍 Python 语言和系统中的不同功能，开始是比较简单的表达式、语句和数据类型，然后是函数和模块，最终接触一些高级概念，比如异常、用户定义的类。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

2. 使用 Python 解释器

2.1. 调用解释器

The Python interpreter is usually installed as /usr/local/bin/python3.6 on those machines where it is available; putting /usr/local/bin in your Unix shell’s search path makes it possible to start it by typing the command:

python3.6

就能运行了 1 。安装时可以选择安装目录，所以解释器也可能在别的地方；可以问问你身边的 Python 大牛，或者你的系统管理员。（比如 /usr/local/python 也是比较常用的备选路径）

On Windows machines, the Python installation is usually placed in C:\Python36, though you can change this when you’re running the installer. To add this directory to your path, you can type the following command into the command prompt in a DOS box:

set path=%path%;C:\python36

在主提示符中输入文件结束字符（在 Unix 系统中是 Control-D，Windows 系统中是 Control-Z）就退出解释器并返回退出状态为0。如果这样不管用，你还可以写这个命令退出：quit()。

The interpreter’s line-editing features include interactive editing, history substitution and code completion on systems that support readline. Perhaps the quickest check to see whether command line editing is supported is typing Control-P to the first Python prompt you get. If it beeps, you have command line editing; see Appendix 交互式编辑和编辑历史 for an introduction to the keys. If nothing appears to happen, or if ^P is echoed, command line editing isn’t available; you’ll only be able to use backspace to remove characters from the current line.

解释器运行的时候有点像 Unix 命令行：在一个标准输入 tty 设备上调用，它能交互式地读取和执行命令；调用时提供文件名参数，或者有个文件重定向到标准输入的话，它就会读取和执行文件中的 脚本。

另一种启动解释器的方式是 python -c command [arg] ...，其中 command 要换成想执行的指令，就像命令行的 -c 选项。由于 Python 代码中经常会包含对终端来说比较特殊的字符，通常情况下都建议用英文单引号把 command 括起来。

有些 Python 模块也可以作为脚本使用。可以这样输入：python -m module [arg] ...，这会执行 module 的源文件，就跟你在命令行把路径写全了一样。

在运行脚本的时候，有时可能也会需要在运行后进入交互模式。这种时候在文件参数前，加上选项 -i 就可以了。

关于所有的命令行选项，请参考 命令行与环境。

2.1.1. 传入参数

如果可能的话，解释器会读取命令行参数，转化为字符串列表存入 sys 模块中的 argv 变量中。执行命令 import sys 你可以导入这个模块并访问这个列表。这个列表最少也会有一个元素；如果没有给定输入参数，sys.argv[0] 就是个空字符串。如果给定的脚本名是 '-' (表示标准输入)，sys.argv[0] 就是 '-'。使用 -c command 时，sys.argv[0] 就会是 '-c'。如果使用选项 -m module，sys.argv[0] 就是包含目录的模块全名。在 -c command 或 -m module 之后的选项不会被解释器处理，而会直接留在 sys.argv 中给命令或模块来处理。

2.1.2. 交互模式

在终端（tty）输入并执行指令时，我们说解释器是运行在 交互模式（interactive mode）。在这种模式中，它会显示 主提示符（primary prompt），提示输入下一条指令，通常用三个大于号（>>>）表示；连续输入行的时候，它会显示 次要提示符，默认是三个点（...）。进入解释器时，它会先显示欢迎信息、版本信息、版权声明，然后就会出现提示符：

$ python3.6
Python 3.6 (default, Sep 16 2015, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

多行指令需要在连续的多行中输入。比如，以 if 为例：

>>> the_world_is_flat = True
>>> if the_world_is_flat:
...     print("Be careful not to fall off!")
...
Be careful not to fall off!

有关交互模式的更多内容，请见 交互模式。

2.2. 解释器的运行环境

2.2.1. 源文件的字符编码

默认情况下，Python 源码文件以 UTF-8 编码方式处理。在这种编码方式中，世界上大多数语言的字符都可以同时用于字符串字面值、变量或函数名称以及注释中——尽管标准库中只用常规的 ASCII 字符作为变量或函数名，而且任何可移植的代码都应该遵守此约定。要正确显示这些字符，你的编辑器必须能识别 UTF-8 编码，而且必须使用能支持打开的文件中所有字符的字体。

如果不使用默认编码，要声明文件所使用的编码，文件的 第一 行要写成特殊的注释。语法如下所示：

# -*- coding: encoding -*-

其中 encoding 可以是 Python 支持的任意一种 codecs。

比如，要声明使用 Windows-1252 编码，你的源码文件要写成：

# -*- coding: cp1252 -*-

关于 第一行 规则的一种例外情况是，源码以 UNIX “shebang” 行 开头。这种情况下，编码声明就要写在文件的第二行。例如：

#!/usr/bin/env python3
# -*- coding: cp1252 -*-

备注

1

在Unix系统中，Python 3.x解释器默认安装后的执行文件并不叫作 python，这样才不会与同时安装的Python 2.x冲突。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

3. Python 的非正式介绍

在下面的例子中，通过提示符 (>>> 与 …) 的出现与否来区分输入和输出：如果你想复现这些例子，当提示符出现后，你必须在提示符后键入例子中的每一个词；不以提示符开头的那些行是解释器的输出。注意例子中某行中出现第二个提示符意味着你必须键入一个空白行；这是用来结束多行命令的。

这个手册中的许多例子都包含注释，甚至交互性命令中也有。Python中的注释以井号 # 开头，并且一直延伸到该文本行结束为止。注释可以出现在一行的开头或者是空白和代码的后边，但是不能出现在字符串中间。字符串中的井号就是井号。因为注释是用来阐明代码的，不会被 Python 解释，所以在键入这些例子时，注释是可以被忽略的。

几个例子:

# this is the first comment
spam = 1  # and this is the second comment
          # ... and now a third!
text = "# This is not a comment because it's inside quotes."

3.1. Python 作为计算器使用

让我们尝试一些简单的 Python 命令。启动解释器，等待界面中的提示符，>>> （这应该花不了多少时间）。

3.1.1. 数字

解释器就像一个简单的计算器一样：你可以在里面输入一个表达式然后它会写出答案。 表达式的语法很直接：运算符 +、-、*、/ 的用法和其他大部分语言一样（比如 Pascal 或者 C 语言）；括号 (()) 用来分组。比如:

>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5  # division always returns a floating point number
1.6

整数（比如 2、4、20 ）有 int 类型，有小数部分的（比如 5.0、1.6 ）有 float 类型。在这个手册的后半部分我们会看到更多的数值类型。

除法运算 (/) 永远返回浮点数类型。如果要做 floor division 得到一个整数结果（忽略小数部分）你可以使用 // 运算符；如果要计算余数，可以使用 %

>>> 17 / 3  # classic division returns a float
5.666666666666667
>>>
>>> 17 // 3  # floor division discards the fractional part
5
>>> 17 % 3  # the % operator returns the remainder of the division
2
>>> 5 * 3 + 2  # result * divisor + remainder
17

在Python中，可以使用 ** 运算符来计算乘方 1

>>> 5 ** 2  # 5 squared
25
>>> 2 ** 7  # 2 to the power of 7
128

等号 (=) 用于给一个变量赋值。然后在下一个交互提示符之前不会有结果显示出来:

>>> width = 20
>>> height = 5 * 9
>>> width * height
900

如果一个变量未定义（未赋值），试图使用它时会向你提示错误:

>>> n  # try to access an undefined variable
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined

Python中提供浮点数的完整支持；包含多种混合类型运算数的运算会把整数转换为浮点数:

>>> 4 * 3.75 - 1
14.0

在交互模式下，上一次打印出来的表达式被赋值给变量 _。这意味着当你把Python用作桌面计算器时，继续计算会相对简单，比如:

>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06

这个变量应该被使用者当作是只读类型。不要向它显式地赋值——你会创建一个和它名字相同独立的本地变量，它会使用魔法行为屏蔽内部变量。

除了 int 和 float，Python也支持其他类型的数字，例如 Decimal 或者 Fraction。Python 也内置对 复数 的支持，使用后缀 j 或者 J 就可以表示虚数部分（例如 3+5j ）。

3.1.2. 字符串

除了数字，Python 也可以操作字符串。字符串有多种形式，可以使用单引号（'...'），双引号（"..."）都可以获得同样的结果 2。反斜杠 \ 可以用来转义:

>>> 'spam eggs'  # single quotes
'spam eggs'
>>> 'doesn\'t'  # use \' to escape the single quote...
"doesn't"
>>> "doesn't"  # ...or use double quotes instead
"doesn't"
>>> '"Yes," they said.'
'"Yes," they said.'
>>> "\"Yes,\" they said."
'"Yes," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'

在交互式解释器中，输出的字符串外面会加上引号，特殊字符会使用反斜杠来转义。 虽然有时这看起来会与输入不一样（外面所加的引号可能会改变），但两个字符串是相同的。 如果字符串中有单引号而没有双引号，该字符串外将加双引号来表示，否则就加单引号。 print() 函数会生成可读性更强的输出，即略去两边的引号，并且打印出经过转义的特殊字符:

>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> print('"Isn\'t," they said.')
"Isn't," they said.
>>> s = 'First line.\nSecond line.'  # \n means newline
>>> s  # without print(), \n is included in the output
'First line.\nSecond line.'
>>> print(s)  # with print(), \n produces a new line
First line.
Second line.

如果你不希望前置了 \ 的字符转义成特殊字符，可以使用 原始字符串 方式，在引号前添加 r 即可:

>>> print('C:\some\name')  # here \n means newline!
C:\some
ame
>>> print(r'C:\some\name')  # note the r before the quote
C:\some\name

字符串字面值可以跨行连续输入。一种方式是用三重引号："""...""" 或 '''...'''。字符串中的回车换行会自动包含到字符串中，如果不想包含，在行尾添加一个 \ 即可。如下例:

print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")

将产生如下输出（注意最开始的换行没有包括进来）:

Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to

字符串可以用 + 进行连接（粘到一起），也可以用 * 进行重复:

>>> # 3 times 'un', followed by 'ium'
>>> 3 * 'un' + 'ium'
'unununium'

相邻的两个或多个 字符串字面值 （引号引起来的字符）将会自动连接到一起.

>>> 'Py' 'thon'
'Python'

把很长的字符串拆开分别输入的时候尤其有用:

>>> text = ('Put several strings within parentheses '
...         'to have them joined together.')
>>> text
'Put several strings within parentheses to have them joined together.'

只能对两个字面值这样操作，变量或表达式不行:

>>> prefix = 'Py'
>>> prefix 'thon'  # can't concatenate a variable and a string literal
  ...
SyntaxError: invalid syntax
>>> ('un' * 3) 'ium'
  ...
SyntaxError: invalid syntax

如果你想连接变量，或者连接变量和字面值，可以用 + 号:

>>> prefix + 'thon'
'Python'

字符串是可以被 索引 （下标访问）的，第一个字符索引是 0。单个字符并没有特殊的类型，只是一个长度为一的字符串:

>>> word = 'Python'
>>> word[0]  # character in position 0
'P'
>>> word[5]  # character in position 5
'n'

索引也可以用负数，这种会从右边开始数:

>>> word[-1]  # last character
'n'
>>> word[-2]  # second-last character
'o'
>>> word[-6]
'P'

注意 -0 和 0 是一样的，所以负数索引从 -1 开始。

除了索引，字符串还支持 切片。索引可以得到单个字符，而 切片 可以获取子字符串:

>>> word[0:2]  # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5]  # characters from position 2 (included) to 5 (excluded)
'tho'

注意切片的开始总是被包括在结果中，而结束不被包括。这使得 s[:i] + s[i:] 总是等于 s

>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
'Python'

切片的索引有默认值；省略开始索引时默认为0，省略结束索引时默认为到字符串的结束:

>>> word[:2]   # character from the beginning to position 2 (excluded)
'Py'
>>> word[4:]   # characters from position 4 (included) to the end
'on'
>>> word[-2:]  # characters from the second-last (included) to the end
'on'

您也可以这么理解切片：将索引视作指向字符 之间 ，第一个字符的左侧标为0，最后一个字符的右侧标为 n ，其中 n 是字符串长度。例如:

 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
 0   1   2   3   4   5   6
-6  -5  -4  -3  -2  -1

第一行数标注了字符串 0…6 的索引的位置，第二行标注了对应的负的索引。那么从 i 到 j 的切片就包括了标有 i 和 j 的位置之间的所有字符。

对于使用非负索引的切片，如果索引不越界，那么得到的切片长度就是起止索引之差。例如， word[1:3] 的长度为2。

试图使用过大的索引会产生一个错误:

>>> word[42]  # the word only has 6 characters
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range

但是，切片中的越界索引会被自动处理:

>>> word[4:42]
'on'
>>> word[42:]
''

Python 中的字符串不能被修改，它们是 immutable 的。因此，向字符串的某个索引位置赋值会产生一个错误:

>>> word[0] = 'J'
  ...
TypeError: 'str' object does not support item assignment
>>> word[2:] = 'py'
  ...
TypeError: 'str' object does not support item assignment

如果需要一个不同的字符串，应当新建一个:

>>> 'J' + word[1:]
'Jython'
>>> word[:2] + 'py'
'Pypy'

内建函数 len() 返回一个字符串的长度:

>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34

参见

文本序列类型 — str

字符串是一种 序列类型 ，因此也支持序列类型的各种操作。

字符串的方法

字符串支持许多变换和查找的方法。

格式化字符串字面值

内嵌表达式的字符串字面值。

格式字符串语法

使用 str.format() 进行字符串格式化。

printf 风格的字符串格式化

这里详述了使用 % 运算符进行字符串格式化。

3.1.3. 列表

Python 中可以通过组合一些值得到多种 复合 数据类型。其中最常用的 列表 ，可以通过方括号括起、逗号分隔的一组值（元素）得到。一个 列表 可以包含不同类型的元素，但通常使用时各个元素类型相同:

>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]

Like strings (and all other built-in sequence type), lists can be indexed and sliced:

>>> squares[0]  # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:]  # slicing returns a new list
[9, 16, 25]

所有的切片操作都返回一个新列表，这个新列表包含所需要的元素。就是说，如下的切片会返回列表的一个新的(浅)拷贝:

>>> squares[:]
[1, 4, 9, 16, 25]

列表同样支持拼接操作:

>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

与 immutable 的字符串不同, 列表是一个 mutable 类型，就是说，它自己的内容可以改变:

>>> cubes = [1, 8, 27, 65, 125]  # something's wrong here
>>> 4 ** 3  # the cube of 4 is 64, not 65!
64
>>> cubes[3] = 64  # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]

你也可以在列表末尾通过 append() 方法 来添加新元素（我们将在后面介绍有关方法的详情）:

>>> cubes.append(216)  # add the cube of 6
>>> cubes.append(7 ** 3)  # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]

给切片赋值也是可以的，这样甚至可以改变列表大小，或者把列表整个清空:

>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters
[]

内置函数 len() 也可以作用到列表上:

>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4

也可以嵌套列表 (创建包含其他列表的列表), 比如说:

>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'

3.2. 走向编程的第一步

Of course, we can use Python for more complicated tasks than adding two and two together. For instance, we can write an initial sub-sequence of the Fibonacci series as follows:

>>> # Fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while b < 10:
...     print(b)
...     a, b = b, a+b
...
1
1
2
3
5
8

这个例子引入了几个新的特性。

第一行含有一个 多重赋值: 变量 a 和 b 同时得到了新值 0 和 1. 最后一行又用了一次多重赋值, 这展示出了右手边的表达式，在任何赋值发生之前就被求值了。右手边的表达式是从左到右被求值的。

The while loop executes as long as the condition (here: b < 10) remains true. In Python, like in C, any non-zero integer value is true; zero is false. The condition may also be a string or list value, in fact any sequence; anything with a non-zero length is true, empty sequences are false. The test used in the example is a simple comparison. The standard comparison operators are written the same as in C: < (less than), > (greater than), == (equal to), <= (less than or equal to), >= (greater than or equal to) and != (not equal to).

循环体 是 缩进的 ：缩进是 Python 组织语句的方式。在交互式命令行里，你得给每个缩进的行敲下 Tab 键或者（多个）空格键。实际上用文本编辑器的话，你要准备更复杂的输入方式；所有像样的文本编辑器都有自动缩进的设置。交互式命令行里，当一个组合的语句输入时, 需要在最后敲一个空白行表示完成（因为语法分析器猜不出来你什么时候打的是最后一行）。注意，在同一块语句中的每一行，都要缩进相同的长度。

print() 函数将所有传进来的参数值打印出来. 它和直接输入你要显示的表达式(比如我们之前在计算器的例子里做的)不一样， print() 能处理多个参数，包括浮点数，字符串。 字符串会打印不带引号的内容, 并且在参数项之间会插入一个空格, 这样你就可以很好的把东西格式化, 像这样:

>>> i = 256*256
>>> print('The value of i is', i)
The value of i is 65536

关键字参数 end 可以用来取消输出后面的换行, 或使用另外一个字符串来结尾:

>>> a, b = 0, 1
>>> while b < 1000:
...     print(b, end=',')
...     a, b = b, a+b
...
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,

备注

1

因为 ** 比 - 有更高的优先级, 所以 -3**2 会被解释成 -(3**2) ，因此结果是 -9. 为了避免这个并且得到结果 9, 你可以用这个式子 (-3)**2.

2

和其他语言不一样的是, 特殊字符比如说 \n 在单引号 ('...') 和双引号 ("...") 里有一样的意义. 这两种引号唯一的区别是，你不需要在单引号里转义双引号 " (但是你必须把单引号转义成 \') ， 反之亦然.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

4. 其他流程控制工具

Besides the while statement just introduced, Python knows the usual control flow statements known from other languages, with some twists.

4.1. if Statements

可能最为人所熟知的编程语句就是 if 语句了。例如

>>> x = int(input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
...     x = 0
...     print('Negative changed to zero')
... elif x == 0:
...     print('Zero')
... elif x == 1:
...     print('Single')
... else:
...     print('More')
...
More

There can be zero or more elif parts, and the else part is optional. The keyword ‘elif’ is short for ‘else if’, and is useful to avoid excessive indentation. An if … elif … elif … sequence is a substitute for the switch or case statements found in other languages.

4.2. for Statements

The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. For example (no pun intended):

>>> # Measure some strings:
... words = ['cat', 'window', 'defenestrate']
>>> for w in words:
...     print(w, len(w))
...
cat 3
window 6
defenestrate 12

如果在循环内需要修改序列中的值（比如重复某些选中的元素），推荐你先拷贝一份副本。对序列进行循环不代表制作了一个副本进行操作。切片操作使这件事非常简单：

>>> for w in words[:]:  # Loop over a slice copy of the entire list.
...     if len(w) > 6:
...         words.insert(0, w)
...
>>> words
['defenestrate', 'cat', 'window', 'defenestrate']

如果写成 for w in words:，这个示例就会创建无限长的列表，一次又一次重复地插入 defenestrate。

4.3. range() 函数

如果你确实需要遍历一个数字序列，内置函数 range() 会派上用场。它生成算术级数:

>>> for i in range(5):
...     print(i)
...
0
1
2
3
4

给定的终止数值并不在要生成的序列里；range(10) 会生成10个值，并且是以合法的索引生成一个长度为10的序列。range也可以以另一个数字开头，或者以指定的幅度增加（甚至是负数；有时这也被叫做 ‘步进’）

range(5, 10)
   5, 6, 7, 8, 9

range(0, 10, 3)
   0, 3, 6, 9

range(-10, -100, -30)
  -10, -40, -70

要以序列的索引来迭代，您可以将 range() 和 len() 组合如下:

>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
...     print(i, a[i])
...
0 Mary
1 had
2 a
3 little
4 lamb

然而，在大多数这类情况下，使用 enumerate() 函数比较方便，请参见 循环的技巧 。

如果你只打印 range，会出现奇怪的结果:

>>> print(range(10))
range(0, 10)

range() 所返回的对象在许多方面表现得像一个列表，但实际上却并不是。此对象会在你迭代它时基于所希望的序列返回连续的项，但它没有真正生成列表，这样就能节省空间。

我们说这样的对象是 可迭代的 ，也就是说，适合作为函数和结构体的参数，这些函数和结构体期望在迭代结束之前可以从中获取连续的元素。我们已经看到 for 语句就是这样一个迭代器。函数 list() 是另外一个；它从可迭代对象中创建列表。

>>> list(range(5))
[0, 1, 2, 3, 4]

后面，我们会看到更多返回可迭代对象的函数，和以可迭代对象作为参数的函数。

4.4. break and continue Statements, and else Clauses on Loops

break 语句，和 C 中的类似，用于跳出最近的 for 或 while 循环.

Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement. This is exemplified by the following loop, which searches for prime numbers:

>>> for n in range(2, 10):
...     for x in range(2, n):
...         if n % x == 0:
...             print(n, 'equals', x, '*', n//x)
...             break
...     else:
...         # loop fell through without finding a factor
...         print(n, 'is a prime number')
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

（是的，这是正确的代码。仔细看： else 子句属于 for 循环， 不属于 if 语句。）

When used with a loop, the else clause has more in common with the else clause of a try statement than it does that of if statements: a try statement’s else clause runs when no exception occurs, and a loop’s else clause runs when no break occurs. For more on the try statement and exceptions, see 处理异常.

continue 语句也是借鉴自 C 语言，表示继续循环中的下一次迭代:

>>> for num in range(2, 10):
...     if num % 2 == 0:
...         print("Found an even number", num)
...         continue
...     print("Found a number", num)
Found an even number 2
Found a number 3
Found an even number 4
Found a number 5
Found an even number 6
Found a number 7
Found an even number 8
Found a number 9

4.5. pass Statements

pass 语句什么也不做。当语法上需要一个语句，但程序需要什么动作也不做时，可以使用它。例如:

>>> while True:
...     pass  # Busy-wait for keyboard interrupt (Ctrl+C)
...

这通常用于创建最小的类:

>>> class MyEmptyClass:
...     pass
...

Another place pass can be used is as a place-holder for a function or conditional body when you are working on new code, allowing you to keep thinking at a more abstract level. The pass is silently ignored:

>>> def initlog(*args):
...     pass   # Remember to implement this!
...

4.6. 定义函数

我们可以创建一个输出任意范围内 Fibonacci 数列的函数:

>>> def fib(n):    # write Fibonacci series up to n
...     """Print a Fibonacci series up to n."""
...     a, b = 0, 1
...     while a < n:
...         print(a, end=' ')
...         a, b = b, a+b
...     print()
...
>>> # Now call the function we just defined:
... fib(2000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

关键字 def 引入一个函数 定义。它必须后跟函数名称和带括号的形式参数列表。构成函数体的语句从下一行开始，并且必须缩进。

函数体的第一个语句可以（可选的）是字符串文字；这个字符串文字是函数的文档字符串或 docstring 。（有关文档字符串的更多信息，请参阅 文档字符串 部分）有些工具使用文档字符串自动生成在线或印刷文档，或者让用户以交互式的形式浏览代码；在你编写的代码中包含文档字符串是一种很好的做法，所以要养成习惯。

The execution of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the local symbol tables of enclosing functions, then in the global symbol table, and finally in the table of built-in names. Thus, global variables cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced.

在函数被调用时，实际参数（实参）会被引入被调用函数的本地符号表中；因此，实参是通过 按值调用 传递的（其中 值 始终是对象 引用 而不是对象的值）。1 当一个函数调用另外一个函数时，将会为该调用创建一个新的本地符号表。

函数定义会把函数名引入当前的符号表中。函数名称的值具有解释器将其识别为用户定义函数的类型。这个值可以分配给另一个名称，该名称也可以作为一个函数使用。这用作一般的重命名机制:

>>> fib
<function fib at 10042ed0>
>>> f = fib
>>> f(100)
0 1 1 2 3 5 8 13 21 34 55 89

如果你学过其他语言，你可能会认为 fib 不是函数而是一个过程，因为它并不返回值。事实上，即使没有 return 语句的函数也会返回一个值，尽管它是一个相当无聊的值。这个值称为 None （它是内置名称）。一般来说解释器不会打印出单独的返回值 None ，如果你真想看到它，你可以使用 print()

>>> fib(0)
>>> print(fib(0))
None

写一个返回斐波那契数列的列表（而不是把它打印出来）的函数，非常简单:

>>> def fib2(n):  # return Fibonacci series up to n
...     """Return a list containing the Fibonacci series up to n."""
...     result = []
...     a, b = 0, 1
...     while a < n:
...         result.append(a)    # see below
...         a, b = b, a+b
...     return result
...
>>> f100 = fib2(100)    # call it
>>> f100                # write the result
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

此示例中，像往常一样，演示了一些新的 Python 功能:

The return statement returns with a value from a function. return without an expression argument returns None. Falling off the end of a function also returns None.

result.append(a) 语句调用了列表对象 result 的 方法 。方法是“属于”一个对象的函数，它被命名为 obj.methodname ，其中 obj 是某个对象（也可能是一个表达式）， methodname 是由对象类型中定义的方法的名称。不同的类型可以定义不同的方法。不同类型的方法可以有相同的名称而不会引起歧义。（可以使用 类 定义自己的对象类型和方法，请参阅 类 ）示例中的方法 append() 是为列表对象定义的；它会在列表的最后添加一个新的元素。在这个示例中它相当于 result = result + [a] ，但更高效。

4.7. 函数定义的更多形式

给函数定义有可变数目的参数也是可行的。这里有三种形式，可以组合使用。

4.7.1. 参数默认值

最有用的形式是对一个或多个参数指定一个默认值。这样创建的函数，可以用比定义时允许的更少的参数调用，比如:

def ask_ok(prompt, retries=4, reminder='Please try again!'):
    while True:
        ok = input(prompt)
        if ok in ('y', 'ye', 'yes'):
            return True
        if ok in ('n', 'no', 'nop', 'nope'):
            return False
        retries = retries - 1
        if retries < 0:
            raise ValueError('invalid user response')
        print(reminder)

这个函数可以通过几种方式调用:

只给出必需的参数：ask_ok('Do you really want to quit?')

给出一个可选的参数：ask_ok('OK to overwrite the file?', 2)

或者给出所有的参数：ask_ok('OK to overwrite the file?', 2, 'Come on, only yes or no!')

这个示例还介绍了 in 关键字。它可以测试一个序列是否包含某个值。

默认值是在 定义过程 中在函数定义处计算的，所以

i = 5

def f(arg=i):
    print(arg)

i = 6
f()

会打印 5。

重要警告： 默认值只会执行一次。这条规则在默认值为可变对象（列表、字典以及大多数类实例）时很重要。比如，下面的函数会存储在后续调用中传递给它的参数:

def f(a, L=[]):
    L.append(a)
    return L

print(f(1))
print(f(2))
print(f(3))

这将打印出

[1]
[1, 2]
[1, 2, 3]

如果你不想要在后续调用之间共享默认值，你可以这样写这个函数:

def f(a, L=None):
    if L is None:
        L = []
    L.append(a)
    return L

4.7.2. 关键字参数

也可以使用形如 kwarg=value 的 关键字参数 来调用函数。例如下面的函数:

def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
    print("-- This parrot wouldn't", action, end=' ')
    print("if you put", voltage, "volts through it.")
    print("-- Lovely plumage, the", type)
    print("-- It's", state, "!")

接受一个必需的参数（voltage）和三个可选的参数（state, action，和 type）。这个函数可以通过下面的任何一种方式调用:

parrot(1000)                                          # 1 positional argument
parrot(voltage=1000)                                  # 1 keyword argument
parrot(voltage=1000000, action='VOOOOOM')             # 2 keyword arguments
parrot(action='VOOOOOM', voltage=1000000)             # 2 keyword arguments
parrot('a million', 'bereft of life', 'jump')         # 3 positional arguments
parrot('a thousand', state='pushing up the daisies')  # 1 positional, 1 keyword

但下面的函数调用都是无效的:

parrot()                     # required argument missing
parrot(voltage=5.0, 'dead')  # non-keyword argument after a keyword argument
parrot(110, voltage=220)     # duplicate value for the same argument
parrot(actor='John Cleese')  # unknown keyword argument

在函数调用中，关键字参数必须跟随在位置参数的后面。传递的所有关键字参数必须与函数接受的其中一个参数匹配（比如 actor 不是函数 parrot 的有效参数），它们的顺序并不重要。这也包括非可选参数，（比如 parrot(voltage=1000) 也是有效的）。不能对同一个参数多次赋值。下面是一个因为此限制而失败的例子:

>>> def function(a):
...     pass
...
>>> function(0, a=0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function() got multiple values for keyword argument 'a'

When a final formal parameter of the form **name is present, it receives a dictionary (see 映射类型 — dict) containing all keyword arguments except for those corresponding to a formal parameter. This may be combined with a formal parameter of the form *name (described in the next subsection) which receives a tuple containing the positional arguments beyond the formal parameter list. (*name must occur before **name.) For example, if we define a function like this:

def cheeseshop(kind, *arguments, **keywords):
    print("-- Do you have any", kind, "?")
    print("-- I'm sorry, we're all out of", kind)
    for arg in arguments:
        print(arg)
    print("-" * 40)
    for kw in keywords:
        print(kw, ":", keywords[kw])

它可以像这样调用:

cheeseshop("Limburger", "It's very runny, sir.",
           "It's really very, VERY runny, sir.",
           shopkeeper="Michael Palin",
           client="John Cleese",
           sketch="Cheese Shop Sketch")

当然它会打印:

-- Do you have any Limburger ?
-- I'm sorry, we're all out of Limburger
It's very runny, sir.
It's really very, VERY runny, sir.
----------------------------------------
shopkeeper : Michael Palin
client : John Cleese
sketch : Cheese Shop Sketch

注意打印时关键字参数的顺序保证与调用函数时提供它们的顺序是相匹配的。

4.7.3. 任意的参数列表

最后，最不常用的选项是可以使用任意数量的参数调用函数。这些参数会被包含在一个元组里（参见 元组和序列 ）。在可变数量的参数之前，可能会出现零个或多个普通参数。:

def write_multiple_items(file, separator, *args):
    file.write(separator.join(args))

一般来说，这些 可变参数 将在形式参数列表的末尾，因为它们收集传递给函数的所有剩余输入参数。出现在 *args 参数之后的任何形式参数都是 ‘仅关键字参数’，也就是说它们只能作为关键字参数而不能是位置参数。:

>>> def concat(*args, sep="/"):
...     return sep.join(args)
...
>>> concat("earth", "mars", "venus")
'earth/mars/venus'
>>> concat("earth", "mars", "venus", sep=".")
'earth.mars.venus'

4.7.4. 解包参数列表

The reverse situation occurs when the arguments are already in a list or tuple but need to be unpacked for a function call requiring separate positional arguments. For instance, the built-in range() function expects separate start and stop arguments. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple:

>>> list(range(3, 6))            # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> list(range(*args))            # call with arguments unpacked from a list
[3, 4, 5]

In the same fashion, dictionaries can deliver keyword arguments with the **-operator:

>>> def parrot(voltage, state='a stiff', action='voom'):
...     print("-- This parrot wouldn't", action, end=' ')
...     print("if you put", voltage, "volts through it.", end=' ')
...     print("E's", state, "!")
...
>>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
>>> parrot(**d)
-- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !

4.7.5. Lambda 表达式

可以用 lambda 关键字来创建一个小的匿名函数。这个函数返回两个参数的和： lambda a, b: a+b 。Lambda函数可以在需要函数对象的任何地方使用。它们在语法上限于单个表达式。从语义上来说，它们只是正常函数定义的语法糖。与嵌套函数定义一样，lambda函数可以引用所包含域的变量:

>>> def make_incrementor(n):
...     return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43

上面的例子使用一个lambda表达式来返回一个函数。另一个用法是传递一个小函数作为参数:

>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
>>> pairs.sort(key=lambda pair: pair[1])
>>> pairs
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]

4.7.6. 文档字符串

以下是有关文档字符串的内容和格式的一些约定。

第一行应该是对象目的的简要概述。为简洁起见，它不应显式声明对象的名称或类型，因为这些可通过其他方式获得（除非名称恰好是描述函数操作的动词）。这一行应以大写字母开头，以句点结尾。

如果文档字符串中有更多行，则第二行应为空白，从而在视觉上将摘要与其余描述分开。后面几行应该是一个或多个段落，描述对象的调用约定，它的副作用等。

Python 解析器不会从 Python 中删除多行字符串文字的缩进，因此处理文档的工具必须在需要时删除缩进。 这是使用以下约定完成的。 文档字符串第一行 之后 的第一个非空行确定整个文档字符串的缩进量。（我们不能使用第一行，因为它通常与字符串的开头引号相邻，因此它的缩进在字符串文字中不明显。）然后从字符串的所有行的开头剥离与该缩进 “等效” 的空格。 缩进更少的行不应该出现，但是如果它们出现，则应该剥离它们的所有前导空格。 应在转化制表符为空格后测试空格的等效性（通常转化为8个空格）。

下面是一个多行文档字符串的例子:

>>> def my_function():
...     """Do nothing, but document it.
...
...     No, really, it doesn't do anything.
...     """
...     pass
...
>>> print(my_function.__doc__)
Do nothing, but document it.

    No, really, it doesn't do anything.

4.7.7. 函数标注

函数标注 是关于用户自定义函数中使用的类型的完全可选元数据信息（有关详情请参阅 PEP 3107 [https://www.python.org/dev/peps/pep-3107] 和 PEP 484 [https://www.python.org/dev/peps/pep-0484] ）。

Annotations are stored in the __annotations__ attribute of the function as a dictionary and have no effect on any other part of the function. Parameter annotations are defined by a colon after the parameter name, followed by an expression evaluating to the value of the annotation. Return annotations are defined by a literal ->, followed by an expression, between the parameter list and the colon denoting the end of the def statement. The following example has a positional argument, a keyword argument, and the return value annotated:

>>> def f(ham: str, eggs: str = 'eggs') -> str:
...     print("Annotations:", f.__annotations__)
...     print("Arguments:", ham, eggs)
...     return ham + ' and ' + eggs
...
>>> f('spam')
Annotations: {'ham': <class 'str'>, 'return': <class 'str'>, 'eggs': <class 'str'>}
Arguments: spam eggs
'spam and eggs'

4.8. 小插曲：编码风格

现在你将要写更长，更复杂的 Python 代码，是时候讨论一下 代码风格 了。 大多数语言都能以不同的风格被编写（或更准确地说，被格式化）；有些比其他的更具有可读性。 能让其他人轻松阅读你的代码总是一个好主意，采用一种好的编码风格对此有很大帮助。

对于Python，PEP 8 [https://www.python.org/dev/peps/pep-0008] 已经成为大多数项目所遵循的风格指南；它促进了一种非常易读且令人赏心悦目的编码风格。每个Python开发人员都应该在某个时候阅读它；以下是为你提取的最重要的几个要点：

使用4个空格缩进，不要使用制表符。

4个空格是一个在小缩进（允许更大的嵌套深度）和大缩进（更容易阅读）的一种很好的折中方案。制表符会引入混乱，最好不要使用它。

换行，使一行不超过79个字符。

这有助于使用小型显示器的用户，并且可以在较大的显示器上并排放置多个代码文件。

使用空行分隔函数和类，以及函数内的较大的代码块。

如果可能，把注释放到单独的一行。

使用文档字符串。

在运算符前后和逗号后使用空格，但不能直接在括号内使用： a = f(1, 2) + g(3, 4)。

Name your classes and functions consistently; the convention is to use CamelCase for classes and lower_case_with_underscores for functions and methods. Always use self as the name for the first method argument (see 初探类 for more on classes and methods).

如果你的代码旨在用于国际环境，请不要使用花哨的编码。Python 默认的 UTF-8 或者纯 ASCII 在任何情况下都能有最好的表现。

同样，哪怕只有很小的可能，遇到说不同语言的人阅读或维护代码，也不要在标识符中使用非ASCII字符。

备注

1

实际上，通过对象引用调用 会是一个更好的表述，因为如果传递的是可变对象，则调用者将看到被调用者对其做出的任何更改（插入到列表中的元素）。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

5. 数据结构

本章节将详细介绍一些您已经了解的内容，并添加了一些新内容。

5.1. 列表的更多特性

列表数据类型还有很多的方法。这里是列表对象方法的清单：

list.append(x)

在列表的末尾添加一个元素。相当于 a[len(a):] = [x] 。

list.extend(iterable)

使用可迭代对象中的所有元素来扩展列表。相当于 a[len(a):] = iterable 。

list.insert(i, x)

在给定的位置插入一个元素。第一个参数是要插入的元素的索引，所以 a.insert(0, x) 插入列表头部， a.insert(len(a), x) 等同于 a.append(x) 。

list.remove(x)

Remove the first item from the list whose value is x. It is an error if there is no such item.

list.pop([i])

删除列表中给定位置的元素并返回它。如果没有给定位置，a.pop() 将会删除并返回列表中的最后一个元素。（ 方法签名中 i 两边的方括号表示这个参数是可选的，而不是要你输入方括号。你会在 Python 参考库中经常看到这种表示方法)。

list.clear()

移除列表中的所有元素。等价于``del a[:]``

list.index(x[, start[, end]])

Return zero-based index in the list of the first item whose value is x. Raises a ValueError if there is no such item.

可选参数 start 和 end 是切片符号，用于将搜索限制为列表的特定子序列。返回的索引是相对于整个序列的开始计算的，而不是 start 参数。

list.count(x)

返回元素 x 在列表中出现的次数。

list.sort(key=None, reverse=False)

对列表中的元素进行排序（参数可用于自定义排序，解释请参见 sorted()）。

list.reverse()

翻转列表中的元素。

list.copy()

返回列表的一个浅拷贝，等价于 a[:]。

多数列表方法示例：

>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
>>> fruits.count('apple')
2
>>> fruits.count('tangerine')
0
>>> fruits.index('banana')
3
>>> fruits.index('banana', 4)  # Find next banana starting a position 4
6
>>> fruits.reverse()
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
>>> fruits.append('grape')
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
>>> fruits.sort()
>>> fruits
['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
>>> fruits.pop()
'pear'

你可能已经注意到，像 insert ，remove 或者 sort 方法，只修改列表，没有打印出返回值——它们返回默认值 None 。1 这是Python中所有可变数据结构的设计原则。

5.1.1. 列表作为栈使用

列表方法使得列表作为堆栈非常容易，最后一个插入，最先取出（“后进先出”）。要添加一个元素到堆栈的顶端，使用 append() 。要从堆栈顶部取出一个元素，使用 pop() ，不用指定索引。例如

>>> stack = [3, 4, 5]
>>> stack.append(6)
>>> stack.append(7)
>>> stack
[3, 4, 5, 6, 7]
>>> stack.pop()
7
>>> stack
[3, 4, 5, 6]
>>> stack.pop()
6
>>> stack.pop()
5
>>> stack
[3, 4]

5.1.2. 列表作为队列使用

列表也可以用作队列，其中先添加的元素被最先取出 (“先进先出”)；然而列表用作这个目的相当低效。因为在列表的末尾添加和弹出元素非常快，但是在列表的开头插入或弹出元素却很慢 (因为所有的其他元素都必须移动一位)。

若要实现一个队列，可使用 collections.deque，它被设计成可以快速地从两端添加或弹出元素。例如

>>> from collections import deque
>>> queue = deque(["Eric", "John", "Michael"])
>>> queue.append("Terry")           # Terry arrives
>>> queue.append("Graham")          # Graham arrives
>>> queue.popleft()                 # The first to arrive now leaves
'Eric'
>>> queue.popleft()                 # The second to arrive now leaves
'John'
>>> queue                           # Remaining queue in order of arrival
deque(['Michael', 'Terry', 'Graham'])

5.1.3. 列表推导式

列表推导式提供了一个更简单的创建列表的方法。常见的用法是把某种操作应用于序列或可迭代对象的每个元素上，然后使用其结果来创建列表，或者通过满足某些特定条件元素来创建子序列。

例如，假设我们想创建一个平方列表，像这样

>>> squares = []
>>> for x in range(10):
...     squares.append(x**2)
...
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

注意这里创建（或被重写）的名为 x 的变量在for循环后仍然存在。我们可以计算平方列表的值而不会产生任何副作用

squares = list(map(lambda x: x**2, range(10)))

或者，等价于

squares = [x**2 for x in range(10)]

上面这种写法更加简洁易读。

A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it. For example, this listcomp combines the elements of two lists if they are not equal:

>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

而它等价于

>>> combs = []
>>> for x in [1,2,3]:
...     for y in [3,1,4]:
...         if x != y:
...             combs.append((x, y))
...
>>> combs
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

注意在上面两个代码片段中， for 和 if 的顺序是相同的。

如果表达式是一个元组（例如上面的 (x, y)），那么就必须加上括号

>>> vec = [-4, -2, 0, 2, 4]
>>> # create a new list with the values doubled
>>> [x*2 for x in vec]
[-8, -4, 0, 4, 8]
>>> # filter the list to exclude negative numbers
>>> [x for x in vec if x >= 0]
[0, 2, 4]
>>> # apply a function to all the elements
>>> [abs(x) for x in vec]
[4, 2, 0, 2, 4]
>>> # call a method on each element
>>> freshfruit = ['  banana', '  loganberry ', 'passion fruit  ']
>>> [weapon.strip() for weapon in freshfruit]
['banana', 'loganberry', 'passion fruit']
>>> # create a list of 2-tuples like (number, square)
>>> [(x, x**2) for x in range(6)]
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
>>> # the tuple must be parenthesized, otherwise an error is raised
>>> [x, x**2 for x in range(6)]
  File "<stdin>", line 1, in <module>
    [x, x**2 for x in range(6)]
               ^
SyntaxError: invalid syntax
>>> # flatten a list using a listcomp with two 'for'
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

列表推导式可以使用复杂的表达式和嵌套函数

>>> from math import pi
>>> [str(round(pi, i)) for i in range(1, 6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']

5.1.4. 嵌套的列表推导式

列表推导式中的初始表达式可以是任何表达式，包括另一个列表推导式。

考虑下面这个 3x4的矩阵，它由3个长度为4的列表组成

>>> matrix = [
...     [1, 2, 3, 4],
...     [5, 6, 7, 8],
...     [9, 10, 11, 12],
... ]

下面的列表推导式将交换其行和列

>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

如上节所示，嵌套的列表推导式是基于跟随其后的 for 进行求值的，所以这个例子等价于:

>>> transposed = []
>>> for i in range(4):
...     transposed.append([row[i] for row in matrix])
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

反过来说，也等价于

>>> transposed = []
>>> for i in range(4):
...     # the following 3 lines implement the nested listcomp
...     transposed_row = []
...     for row in matrix:
...         transposed_row.append(row[i])
...     transposed.append(transposed_row)
...
>>> transposed
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

实际应用中，你应该会更喜欢使用内置函数去组成复杂的流程语句。 zip() 函数将会很好地处理这种情况

>>> list(zip(*matrix))
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]

关于本行中星号的详细说明，参见 解包参数列表。

5.2. The del statement

There is a way to remove an item from a list given its index instead of its value: the del statement. This differs from the pop() method which returns a value. The del statement can also be used to remove slices from a list or clear the entire list (which we did earlier by assignment of an empty list to the slice). For example:

>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]

del 也可以删除整个变量

>>> del a

此后再引用 a 时会报错（直到另一个值被赋给它）。我们会在后面了解到 del 的其他用法。

5.3. 元组和序列

我们看到列表和字符串有很多共同特性，例如索引和切片操作。他们是 序列 数据类型（参见 序列类型 — list, tuple, range）中的两种。随着 Python 语言的发展，其他的序列类型也会被加入其中。这里介绍另一种标准序列类型: 元组。

一个元组由几个被逗号隔开的值组成，例如

>>> t = 12345, 54321, 'hello!'
>>> t[0]
12345
>>> t
(12345, 54321, 'hello!')
>>> # Tuples may be nested:
... u = t, (1, 2, 3, 4, 5)
>>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
>>> # Tuples are immutable:
... t[0] = 88888
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> # but they can contain mutable objects:
... v = ([1, 2, 3], [3, 2, 1])
>>> v
([1, 2, 3], [3, 2, 1])

如你所见，元组在输出时总是被圆括号包围的，以便正确表示嵌套元组。输入时圆括号可有可无，不过经常会是必须的（如果这个元组是一个更大的表达式的一部分）。给元组中的一个单独的元素赋值是不允许的，当然你可以创建包含可变对象的元组，例如列表。

虽然元组可能看起来与列表很像，但它们通常是在不同的场景被使用，并且有着不同的用途。元组是 immutable ，其序列通常包含不同种类的元素，并且通过解包（这一节下面会解释）或者索引来访问（如果是 namedtuples 的话甚至还可以通过属性访问）。列表是 mutable ，并且列表中的元素一般是同种类型的，并且通过迭代访问。

一个特殊的问题是构造包含0个或1个元素的元组：为了适应这种情况，语法有一些额外的改变。空元组可以直接被一对空圆括号创建，含有一个元素的元组可以通过在这个元素后添加一个逗号来构建（圆括号里只有一个值的话不够明确）。丑陋，但是有效。例如

>>> empty = ()
>>> singleton = 'hello',    # <-- note trailing comma
>>> len(empty)
0
>>> len(singleton)
1
>>> singleton
('hello',)

语句 t = 12345, 54321, 'hello!' 是 元组打包 的一个例子：值 12345, 54321 和 'hello!' 被打包进元组。其逆操作也是允许的

>>> x, y, z = t

这被称为 序列解包 也是很恰当的，因为解包操作的等号右侧可以是任何序列。序列解包要求等号左侧的变量数与右侧序列里所含的元素数相同。注意多重赋值其实也只是元组打包和序列解包的组合。

5.4. 集合

Python也包含有 集合 类型。集合是由不重复元素组成的无序的集。它的基本用法包括成员检测和消除重复元素。集合对象也支持像 联合，交集，差集，对称差分等数学运算。

花括号或 set() 函数可以用来创建集合。注意：要创建一个空集合你只能用 set() 而不能用 {}，因为后者是创建一个空字典，这种数据结构我们会在下一节进行讨论。

以下是一些简单的示例

>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket)                      # show that duplicates have been removed
{'orange', 'banana', 'pear', 'apple'}
>>> 'orange' in basket                 # fast membership testing
True
>>> 'crabgrass' in basket
False

>>> # Demonstrate set operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # letters in a but not in b
{'r', 'd', 'b'}
>>> a | b                              # letters in a or b or both
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # letters in both a and b
{'a', 'c'}
>>> a ^ b                              # letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}

类似于 列表推导式，集合也支持推导式形式

>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
{'r', 'd'}

5.5. 字典

另一个非常有用的 Python 內置数据类型是 字典 (参见 映射类型 — dict)。字典在其他语言里可能会被叫做 联合内存 或 联合数组。与以连续整数为索引的序列不同，字典是以 关键字 为索引的，关键字可以是任意不可变类型，通常是字符串或数字。如果一个元组只包含字符串、数字或元组，那么这个元组也可以用作关键字。但如果元组直接或间接地包含了可变对象，那么它就不能用作关键字。列表不能用作关键字，因为列表可以通过索引、切片或 append() 和 extend() 之类的方法来改变。

It is best to think of a dictionary as an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary). A pair of braces creates an empty dictionary: {}. Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the dictionary; this is also the way dictionaries are written on output.

字典主要的操作是使用关键字存储和解析值。也可以用 del 来删除一个键值对。如果你使用了一个已经存在的关键字来存储值，那么之前与这个关键字关联的值就会被遗忘。用一个不存在的键来取值则会报错。

Performing list(d.keys()) on a dictionary returns a list of all the keys used in the dictionary, in arbitrary order (if you want it sorted, just use sorted(d.keys()) instead). 2 To check whether a single key is in the dictionary, use the in keyword.

以下是使用字典的一些简单示例

>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}
>>> list(tel.keys())
['irv', 'guido', 'jack']
>>> sorted(tel.keys())
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
>>> 'jack' not in tel
False

dict() 构造函数可以直接从键值对序列里创建字典。

>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}

此外，字典推导式可以从任意的键值表达式中创建字典

>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}

当关键字是简单字符串时，有时直接通过关键字参数来指定键值对更方便

>>> dict(sape=4139, guido=4127, jack=4098)
{'sape': 4139, 'jack': 4098, 'guido': 4127}

5.6. 循环的技巧

当在字典中循环时，用 items() 方法可将关键字和对应的值同时取出

>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
...     print(k, v)
...
gallahad the pure
robin the brave

当在序列中循环时，用 enumerate() 函数可以将索引位置和其对应的值同时取出

>>> for i, v in enumerate(['tic', 'tac', 'toe']):
...     print(i, v)
...
0 tic
1 tac
2 toe

当同时在两个或更多序列中循环时，可以用 zip() 函数将其内元素一一匹配。

>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
...     print('What is your {0}?  It is {1}.'.format(q, a))
...
What is your name?  It is lancelot.
What is your quest?  It is the holy grail.
What is your favorite color?  It is blue.

如果要逆向循环一个序列，可以先正向定位序列，然后调用 reversed() 函数

>>> for i in reversed(range(1, 10, 2)):
...     print(i)
...
9
7
5
3
1

如果要按某个指定顺序循环一个序列，可以用 sorted() 函数，它可以在不改动原序列的基础上返回一个新的排好序的序列

>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
...     print(f)
...
apple
banana
orange
pear

有时可能会想在循环时修改列表内容，一般来说改为创建一个新列表是比较简单且安全的

>>> import math
>>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
>>> filtered_data = []
>>> for value in raw_data:
...     if not math.isnan(value):
...         filtered_data.append(value)
...
>>> filtered_data
[56.2, 51.7, 55.3, 52.5, 47.8]

5.7. 深入条件控制

while 和 if 条件句中可以使用任意操作，而不仅仅是比较操作。

比较操作符 in 和 not in 校验一个值是否在（或不在）一个序列里。操作符 is 和 is not 比较两个对象是不是同一个对象，这只对像列表这样的可变对象比较重要。所有的比较操作符都有相同的优先级，且这个优先级比数值运算符低。

比较操作可以传递。例如 a < b == c 会校验是否 a 小于 b 并且 b 等于 c。

比较操作可以通过布尔运算符 and 和 or 来组合，并且比较操作（或其他任何布尔运算）的结果都可以用 not 来取反。这些操作符的优先级低于比较操作符；在它们之中，not 优先级最高， or 优先级最低，因此 A and not B or C 等价于 (A and (not B)) or C。和之前一样，你也可以在这种式子里使用圆括号。

布尔运算符 and 和 or 也被称为 短路 运算符：它们的参数从左至右解析，一旦可以确定结果解析就会停止。例如，如果 A 和 C 为真而 B 为假，那么 A and B and C 不会解析 C。当用作普通值而非布尔值时，短路操作符的返回值通常是最后一个变量。

也可以把比较操作或者逻辑表达式的结果赋值给一个变量，例如

>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
>>> non_null = string1 or string2 or string3
>>> non_null
'Trondheim'

注意 Python 与 C 不同，赋值操作不能发生在表达式内部。C程序员可能会对此抱怨，但它避免了一类C程序中常见的错误：想在表达式中写 == 时却写成了 =。

5.8. 比较序列和其他类型

序列对象可以与相同类型的其他对象比较。它们使用 字典顺序 进行比较：首先比较两个序列的第一个元素，如果不同，那么这就决定了比较操作的结果。如果它们相同，就再比较每个序列的第二个元素，以此类推，直到有一个序列被耗尽。如果要比较的两个元素本身就是相同类型的序列，那么就递归进行字典顺序比较。如果两个序列中所有的元素都相等，那么我们认为这两个序列相等。如果一个序列是另一个序列的初始子序列，那么短序列就小于（少于）另一个。字典顺序对字符串来说，是使用单字符的 Unicode 码的顺序。下面是同类型序列之间比较的例子

(1, 2, 3)              < (1, 2, 4)
[1, 2, 3]              < [1, 2, 4]
'ABC' < 'C' < 'Pascal' < 'Python'
(1, 2, 3, 4)           < (1, 2, 4)
(1, 2)                 < (1, 2, -1)
(1, 2, 3)             == (1.0, 2.0, 3.0)
(1, 2, ('aa', 'ab'))   < (1, 2, ('abc', 'a'), 4)

注意对不同类型对象来说，只要待比较对象提供了合适的比较方法，就可以使用 < 和 > 来比较。例如，混合数值类型是通过他们的数值进行比较的，所以 0 等于 0.0，等等。否则，解释器将抛出一个 TypeError 异常，而不是随便给出一个结果。

备注

1

别的语言可能会返回一个可变对象，他们允许方法连续执行，例如 d->insert("a")->remove("b")->sort();。

2

Calling d.keys() will return a dictionary view object. It supports operations like membership test and iteration, but its contents are not independent of the original dictionary – it is only a view.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

6. 模块

如果你从Python解释器退出并再次进入，之前的定义（函数和变量）都会丢失。因此，如果你想编写一个稍长些的程序，最好使用文本编辑器为解释器准备输入并将该文件作为输入运行。这被称作编写 脚本 。随着程序变得越来越长，你或许会想把它拆分成几个文件，以方便维护。你亦或想在不同的程序中使用一个便捷的函数， 而不必把这个函数复制到每一个程序中去。

为支持这些，Python有一种方法可以把定义放在一个文件里，并在脚本或解释器的交互式实例中使用它们。这样的文件被称作 模块 ；模块中的定义可以 导入 到其它模块或者 主 模块（你在顶级和计算器模式下执行的脚本中可以访问的变量集合）。

模块是一个包含Python定义和语句的文件。文件名就是模块名后跟文件后缀 .py 。在一个模块内部，模块名（作为一个字符串）可以通过全局变量 __name__ 的值获得。例如，使用你最喜爱的文本编辑器在当前目录下创建一个名为 fibo.py 的文件， 文件中含有以下内容:

# Fibonacci numbers module

def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while b < n:
        print(b, end=' ')
        a, b = b, a+b
    print()

def fib2(n):   # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while b < n:
        result.append(b)
        a, b = b, a+b
    return result

现在进入Python解释器，并用以下命令导入该模块:

>>> import fibo

在当前的符号表中，这并不会直接进入到定义在 fibo 函数内的名称；它只是进入到模块名 fibo 中。你可以用模块名访问这些函数:

>>> fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> fibo.fib2(100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> fibo.__name__
'fibo'

如果你想经常使用某个函数，你可以把它赋值给一个局部变量:

>>> fib = fibo.fib
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

6.1. 更多有关模块的信息

模块可以包含可执行的语句以及函数定义。这些语句用于初始化模块。它们仅在模块 第一次 在 import 语句中被导入时才执行。 1 (当文件被当作脚本运行时，它们也会执行。)

每个模块都有它自己的私有符号表，该表用作模块中定义的所有函数的全局符号表。因此，模块的作者可以在模块内使用全局变量，而不必担心与用户的全局变量发生意外冲突。另一方面，如果你知道自己在做什么，则可以用跟访问模块内的函数的同样标记方法，去访问一个模块的全局变量，modname.itemname。

模块可以导入其它模块。习惯上但不要求把所有 import 语句放在模块（或脚本）的开头。被导入的模块名存放在调入模块的全局符号表中。

import 语句有一个变体，它可以把名字从一个被调模块内直接导入到现模块的符号表里。例如:

>>> from fibo import fib, fib2
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

这并不会把被调模块名引入到局部变量表里（因此在这个例子里，fibo 是未被定义的）。

还有一个变体甚至可以导入模块内定义的所有名称:

>>> from fibo import *
>>> fib(500)
1 1 2 3 5 8 13 21 34 55 89 144 233 377

这会调入所有非以下划线（_）开头的名称。 在多数情况下，Python程序员都不会使用这个功能，因为它在解释器中引入了一组未知的名称，而它们很可能会覆盖一些你已经定义过的东西。

注意通常情况下从一个模块或者包内调入 * 的做法是不太被接受的， 因为这通常会导致代码的可读性很差。不过，在交互式编译器中为了节省打字可以这么用。

If the module name is followed by as, then the name following as is bound directly to the imported module.

>>> import fibo as fib
>>> fib.fib(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

这会和 import fibo 方式一样有效地调入模块， 唯一的区别是它以 fib 的名称存在的。

这种方式也可以在用到 from 的时候使用，并会有类似的效果:

>>> from fibo import fib as fibonacci
>>> fibonacci(500)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

注解

出于效率的考虑，每个模块在每个解释器会话中只被导入一次。因此，如果你更改了你的模块，则必须重新启动解释器， 或者，如果它只是一个要交互式地测试的模块，请使用 importlib.reload()，例如 import importlib; importlib.reload(modulename)。

6.1.1. 以脚本的方式执行模块

当你用下面方式运行一个Python模块:

python fibo.py <arguments>

这项操作将执行模块里的代码，和导入模块一样，但会把 __name__ 赋值为 "__main__"。 也就是把下列代码添加到模块末尾：

if __name__ == "__main__":
    import sys
    fib(int(sys.argv[1]))

你既可以把这个文件当作脚本又可当作一个可调入的模块来使用， 因为那段解析命令行的代码只有在当模块是以“main”文件的方式执行的时候才会运行:

$ python fibo.py 50
1 1 2 3 5 8 13 21 34

如果模块是被导入的，那些代码是不运行的:

>>> import fibo
>>>

这经常用于为模块提供一个方便的用户接口，或用于测试（以脚本的方式运行模块从而执行一些测试套件）。

6.1.2. 模块搜索路径

当一个名为 spam 的模块被导入的时候，解释器首先寻找具有该名称的内置模块。如果没有找到，然后解释器从 sys.path 变量给出的目录列表里寻找名为 spam.py 的文件。sys.path 初始有这些目录地址:

包含输入脚本的目录（或者未指定文件时的当前目录）。

PYTHONPATH （一个包含目录名称的列表，它和shell变量 PATH 有一样的语法）。

取决于安装的默认设置

注解

在支持符号链接的文件系统上，包含输入脚本的目录是在追加符号链接后才计算出来的。换句话说，包含符号链接的目录并 没有 被添加到模块的搜索路径上。

在初始化后，Python程序可以更改 sys.path。包含正在运行脚本的文件目录被放在搜索路径的开头处， 在标准库路径之前。这意味着将加载此目录里的脚本，而不是标准库中的同名模块。 除非有意更换，否则这是错误。更多信息请参阅 标准模块。

6.1.3. “编译过的”Python文件

为了加速模块载入，Python在 __pycache__ 目录里缓存了每个模块的编译后版本，名称为 module.version.pyc ，其中名称中的版本字段对编译文件的格式进行编码； 它一般使用Python版本号。例如，在CPython版本3.3中，spam.py的编译版本将被缓存为 __pycache__/spam.cpython-33.pyc。此命名约定允许来自不同发行版和不同版本的Python的已编译模块共存。

Python根据编译版本检查源的修改日期，以查看它是否已过期并需要重新编译。这是一个完全自动化的过程。此外，编译的模块与平台无关，因此可以在具有不同体系结构的系统之间共享相同的库。

Python在两种情况下不会检查缓存。首先，对于从命令行直接载入的模块，它从来都是重新编译并且不存储编译结果；其次，如果没有源模块，它不会检查缓存。为了支持无源文件（仅编译）发行版本， 编译模块必须是在源目录下，并且绝对不能有源模块。

给专业人士的一些小建议:

你可以在Python命令中使用 -O 或者 -OO 开关， 以减小编译后模块的大小。 -O 开关去除断言语句，-OO 开关同时去除断言语句和 __doc__ 字符串。由于有些程序可能依赖于这些，你应当只在清楚自己在做什么时才使用这个选项。“优化过的”模块有一个 opt- 标签并且通常小些。将来的发行版本或许会更改优化的效果。

一个从 .pyc 文件读出的程序并不会比它从 .py 读出时运行的更快，.pyc 文件唯一快的地方在于载入速度。

compileall 模块可以为一个目录下的所有模块创建.pyc文件。

关于这个过程，PEP 3147 [https://www.python.org/dev/peps/pep-3147] 中有更多细节，包括一个决策流程图。

6.2. 标准模块

Python附带了一个标准模块库，在单独的文档Python库参考（以下称为“库参考”）中进行了描述。一些模块内置于解释器中；它们提供对不属于语言核心但仍然内置的操作的访问，以提高效率或提供对系统调用等操作系统原语的访问。这些模块的集合是一个配置选项，它也取决于底层平台。例如，winreg 模块只在Windows操作系统上提供。一个特别值得注意的模块 sys，它被内嵌到每一个Python解释器中。变量 sys.ps1 和 sys.ps2 定义用作主要和辅助提示的字符串:

>>> import sys
>>> sys.ps1
'>>> '
>>> sys.ps2
'... '
>>> sys.ps1 = 'C> '
C> print('Yuck!')
Yuck!
C>

这两个变量只有在编译器是交互模式下才被定义。

sys.path 变量是一个字符串列表，用于确定解释器的模块搜索路径。该变量被初始化为从环境变量 PYTHONPATH 获取的默认路径，或者如果 PYTHONPATH 未设置，则从内置默认路径初始化。你可以使用标准列表操作对其进行修改:

>>> import sys
>>> sys.path.append('/ufs/guido/lib/python')

6.3. dir() 函数

内置函数 dir() 用于查找模块定义的名称。 它返回一个排序过的字符串列表:

>>> import fibo, sys
>>> dir(fibo)
['__name__', 'fib', 'fib2']
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__loader__', '__name__',
 '__package__', '__stderr__', '__stdin__', '__stdout__',
 '_clear_type_cache', '_current_frames', '_debugmallocstats', '_getframe',
 '_home', '_mercurial', '_xoptions', 'abiflags', 'api_version', 'argv',
 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder',
 'call_tracing', 'callstats', 'copyright', 'displayhook',
 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix',
 'executable', 'exit', 'flags', 'float_info', 'float_repr_style',
 'getcheckinterval', 'getdefaultencoding', 'getdlopenflags',
 'getfilesystemencoding', 'getobjects', 'getprofile', 'getrecursionlimit',
 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettotalrefcount',
 'gettrace', 'hash_info', 'hexversion', 'implementation', 'int_info',
 'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path',
 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1',
 'setcheckinterval', 'setdlopenflags', 'setprofile', 'setrecursionlimit',
 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout',
 'thread_info', 'version', 'version_info', 'warnoptions']

如果没有参数，dir() 会列出你当前定义的名称:

>>> a = [1, 2, 3, 4, 5]
>>> import fibo
>>> fib = fibo.fib
>>> dir()
['__builtins__', '__name__', 'a', 'fib', 'fibo', 'sys']

注意：它列出所有类型的名称：变量，模块，函数，等等。

dir() 不会列出内置函数和变量的名称。如果你想要这些，它们的定义是在标准模块 builtins 中:

>>> import builtins
>>> dir(builtins)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException',
 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning',
 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError',
 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning',
 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False',
 'FileExistsError', 'FileNotFoundError', 'FloatingPointError',
 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError',
 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError',
 'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented',
 'NotImplementedError', 'OSError', 'OverflowError',
 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError',
 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning',
 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError',
 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError',
 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError',
 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
 'ValueError', 'Warning', 'ZeroDivisionError', '_', '__build_class__',
 '__debug__', '__doc__', '__import__', '__name__', '__package__', 'abs',
 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable',
 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits',
 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit',
 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr',
 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass',
 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview',
 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property',
 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice',
 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars',
 'zip']

6.4. 包

包是一种通过用“带点号的模块名”来构造 Python 模块命名空间的方法。 例如，模块名 A.B 表示 A 包中名为 B 的子模块。正如模块的使用使得不同模块的作者不必担心彼此的全局变量名称一样，使用加点的模块名可以使得 NumPy 或 Pillow 等多模块软件包的作者不必担心彼此的模块名称一样。

假设你想为声音文件和声音数据的统一处理，设计一个模块集合（一个“包”）。由于存在很多不同的声音文件格式（通常由它们的扩展名来识别，例如：.wav， .aiff， .au），因此为了不同文件格式间的转换，你可能需要创建和维护一个不断增长的模块集合。 你可能还想对声音数据还做很多不同的处理（例如，混声，添加回声，使用均衡器功能，创造人工立体声效果）， 因此为了实现这些处理，你将另外写一个无穷尽的模块流。这是你的包的可能结构（以分层文件系统的形式表示）：

sound/                          Top-level package
      __init__.py               Initialize the sound package
      formats/                  Subpackage for file format conversions
              __init__.py
              wavread.py
              wavwrite.py
              aiffread.py
              aiffwrite.py
              auread.py
              auwrite.py
              ...
      effects/                  Subpackage for sound effects
              __init__.py
              echo.py
              surround.py
              reverse.py
              ...
      filters/                  Subpackage for filters
              __init__.py
              equalizer.py
              vocoder.py
              karaoke.py
              ...

当导入这个包时，Python搜索 sys.path 里的目录，查找包的子目录。

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

包的用户可以从包中导入单个模块，例如:

import sound.effects.echo

这会加载子模块 sound.effects.echo 。但引用它时必须使用它的全名。

sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)

导入子模块的另一种方法是

from sound.effects import echo

这也会加载子模块 echo ，并使其在没有包前缀的情况下可用，因此可以按如下方式使用:

echo.echofilter(input, output, delay=0.7, atten=4)

另一种形式是直接导入所需的函数或变量:

from sound.effects.echo import echofilter

同样，这也会加载子模块 echo，但这会使其函数 echofilter() 直接可用:

echofilter(input, output, delay=0.7, atten=4)

请注意，当使用 from package import item 时，item可以是包的子模块（或子包），也可以是包中定义的其他名称，如函数，类或变量。 import 语句首先测试是否在包中定义了item；如果没有，它假定它是一个模块并尝试加载它。如果找不到它，则引发 ImportError 异常。

相反，当使用 import item.subitem.subsubitem 这样的语法时，除了最后一项之外的每一项都必须是一个包；最后一项可以是模块或包，但不能是前一项中定义的类或函数或变量。

6.4.1. 从包中导入 *

当用户写 from sound.effects import * 会发生什么？理想情况下，人们希望这会以某种方式传递给文件系统，找到包中存在哪些子模块，并将它们全部导入。这可能需要很长时间，导入子模块可能会产生不必要的副作用，这种副作用只有在显式导入子模块时才会发生。

唯一的解决方案是让包作者提供一个包的显式索引。import 语句使用下面的规范：如果一个包的 __init__.py 代码定义了一个名为 __all__ 的列表，它会被视为在遇到 from package import * 时应该导入的模块名列表。在发布该包的新版本时，包作者可以决定是否让此列表保持更新。包作者如果认为从他们的包中导入 * 的操作没有必要被使用，也可以决定不支持此列表。例如，文件 sound/effects/__init__.py 可以包含以下代码:

__all__ = ["echo", "surround", "reverse"]

这意味着 from sound.effects import * 将导入 sound 包的三个命名子模块。

如果没有定义 __all__，from sound.effects import * 语句 不会 从包 sound.effects 中导入所有子模块到当前命名空间；它只确保导入了包 sound.effects （可能运行任何在 __init__.py 中的初始化代码），然后导入包中定义的任何名称。 这包括 __init__.py 定义的任何名称（以及显式加载的子模块）。它还包括由之前的 import 语句显式加载的包的任何子模块。思考下面的代码:

import sound.effects.echo
import sound.effects.surround
from sound.effects import *

在这个例子中， echo 和 surround 模块是在执行 from...import 语句时导入到当前命名空间中的，因为它们定义在 sound.effects 包中。（这在定义了 __all__ 时也有效。）

虽然某些模块被设计为在使用 import * 时只导出遵循某些模式的名称，但在生产代码中它仍然被认为是不好的做法。

Remember, there is nothing wrong with using from Package import specific_submodule! In fact, this is the recommended notation unless the importing module needs to use submodules with the same name from different packages.

6.4.2. 子包参考

当包被构造成子包时（与示例中的 sound 包一样），你可以使用绝对导入来引用兄弟包的子模块。例如，如果模块 sound.filters.vocoder 需要在 sound.effects 包中使用 echo 模块，它可以使用 from sound.effects import echo 。

你还可以使用import语句的 from module import name 形式编写相对导入。这些导入使用前导点来指示相对导入中涉及的当前包和父包。例如，从 surround 模块，你可以使用:

from . import echo
from .. import formats
from ..filters import equalizer

请注意，相对导入是基于当前模块的名称进行导入的。由于主模块的名称总是 "__main__" ，因此用作Python应用程序主模块的模块必须始终使用绝对导入。

6.4.3. 多个目录中的包

包支持另一个特殊属性， __path__ 。它被初始化为一个列表，其中包含在执行该文件中的代码之前保存包的文件 __init__.py 的目录的名称。这个变量可以修改；这样做会影响将来对包中包含的模块和子包的搜索。

虽然通常不需要此功能，但它可用于扩展程序包中的模块集。

备注

1

实际上，函数定义也是“被执行”的“语句”；模块级函数定义的执行在模块的全局符号表中输入该函数名。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

7. 输入输出

有几种方法可以显示程序的输出；数据可以以人类可读的形式打印出来，或者写入文件以供将来使用。本章将讨论一些可能性。

7.1. 更漂亮的输出格式

到目前为止，我们遇到了两种写入值的方法：表达式语句 和 print() 函数。（第三种是使用文件对象的 write() 方法；标准输出文件可以作为 sys.stdout 引用。更多相关信息可参考标准库指南。）

Often you’ll want more control over the formatting of your output than simply printing space-separated values. There are two ways to format your output; the first way is to do all the string handling yourself; using string slicing and concatenation operations you can create any layout you can imagine. The string type has some methods that perform useful operations for padding strings to a given column width; these will be discussed shortly. The second way is to use formatted string literals, or the str.format() method.

The string module contains a Template class which offers yet another way to substitute values into strings.

One question remains, of course: how do you convert values to strings? Luckily, Python has ways to convert any value to a string: pass it to the repr() or str() functions.

str() 函数是用于返回人类可读的值的表示，而 repr() 是用于生成解释器可读的表示（如果没有等效的语法，则会强制执行 SyntaxError）对于没有人类可读性的表示的对象， str() 将返回和 repr() 一样的值。很多值使用任一函数都具有相同的表示，比如数字或类似列表和字典的结构。特殊的是字符串有两个不同的表示。

几个例子:

>>> s = 'Hello, world.'
>>> str(s)
'Hello, world.'
>>> repr(s)
"'Hello, world.'"
>>> str(1/7)
'0.14285714285714285'
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
>>> print(s)
The value of x is 32.5, and y is 40000...
>>> # The repr() of a string adds string quotes and backslashes:
... hello = 'hello, world\n'
>>> hellos = repr(hello)
>>> print(hellos)
'hello, world\n'
>>> # The argument to repr() may be any Python object:
... repr((x, y, ('spam', 'eggs')))
"(32.5, 40000, ('spam', 'eggs'))"

Here are two ways to write a table of squares and cubes:

>>> for x in range(1, 11):
...     print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ')
...     # Note use of 'end' on previous line
...     print(repr(x*x*x).rjust(4))
...
 1   1    1
 2   4    8
 3   9   27
 4  16   64
 5  25  125
 6  36  216
 7  49  343
 8  64  512
 9  81  729
10 100 1000

>>> for x in range(1, 11):
...     print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x))
...
 1   1    1
 2   4    8
 3   9   27
 4  16   64
 5  25  125
 6  36  216
 7  49  343
 8  64  512
 9  81  729
10 100 1000

(Note that in the first example, one space between each column was added by the way print() works: by default it adds spaces between its arguments.)

This example demonstrates the str.rjust() method of string objects, which right-justifies a string in a field of a given width by padding it with spaces on the left. There are similar methods str.ljust() and str.center(). These methods do not write anything, they just return a new string. If the input string is too long, they don’t truncate it, but return it unchanged; this will mess up your column lay-out but that’s usually better than the alternative, which would be lying about a value. (If you really want truncation you can always add a slice operation, as in x.ljust(n)[:n].)

还有另外一个方法，str.zfill() ，它会在数字字符串的左边填充零。它能识别正负号:

>>> '12'.zfill(5)
'00012'
>>> '-3.14'.zfill(7)
'-003.14'
>>> '3.14159265359'.zfill(5)
'3.14159265359'

str.format() 方法的基本用法如下所示:

>>> print('We are the {} who say "{}!"'.format('knights', 'Ni'))
We are the knights who say "Ni!"

花括号和其中的字符（称为格式字段）将替换为传递给 str.format() 方法的对象。花括号中的数字可用来表示传递给 str.format() 方法的对象的位置。

>>> print('{0} and {1}'.format('spam', 'eggs'))
spam and eggs
>>> print('{1} and {0}'.format('spam', 'eggs'))
eggs and spam

如果在 str.format() 方法中使用关键字参数，则使用参数的名称引用它们的值。:

>>> print('This {food} is {adjective}.'.format(
...       food='spam', adjective='absolutely horrible'))
This spam is absolutely horrible.

位置和关键字参数可以任意组合:

>>> print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
                                                       other='Georg'))
The story of Bill, Manfred, and Georg.

'!a' (apply ascii()), '!s' (apply str()) and '!r' (apply repr()) can be used to convert the value before it is formatted:

>>> contents = 'eels'
>>> print('My hovercraft is full of {}.'.format(contents))
My hovercraft is full of eels.
>>> print('My hovercraft is full of {!r}.'.format(contents))
My hovercraft is full of 'eels'.

An optional ':' and format specifier can follow the field name. This allows greater control over how the value is formatted. The following example rounds Pi to three places after the decimal.

>>> import math
>>> print('The value of PI is approximately {0:.3f}.'.format(math.pi))
The value of PI is approximately 3.142.

Passing an integer after the ':' will cause that field to be a minimum number of characters wide. This is useful for making tables pretty.

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
>>> for name, phone in table.items():
...     print('{0:10} ==> {1:10d}'.format(name, phone))
...
Jack       ==>       4098
Dcab       ==>       7678
Sjoerd     ==>       4127

If you have a really long format string that you don’t want to split up, it would be nice if you could reference the variables to be formatted by name instead of by position. This can be done by simply passing the dict and using square brackets '[]' to access the keys

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '
...       'Dcab: {0[Dcab]:d}'.format(table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678

这也可以通过使用 ‘**’ 符号将 table 作为关键字参数传递。

>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
>>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))
Jack: 4098; Sjoerd: 4127; Dcab: 8637678

这在与内置函数 vars() 结合使用时非常有用，它会返回包含所有局部变量的字典。

关于使用 str.format() 进行字符串格式化的完整概述，请参阅 格式字符串语法 。

7.1.1. 旧的字符串格式化方法

The % operator can also be used for string formatting. It interprets the left argument much like a sprintf()-style format string to be applied to the right argument, and returns the string resulting from this formatting operation. For example:

>>> import math
>>> print('The value of PI is approximately %5.3f.' % math.pi)
The value of PI is approximately 3.142.

可在 printf 风格的字符串格式化 部分找到更多信息。

7.2. 读写文件

open() 返回一个 file object，最常用的有两个参数： open(filename, mode)。

>>> f = open('workfile', 'w')

第一个参数是包含文件名的字符串。第二个参数是另一个字符串，其中包含一些描述文件使用方式的字符。mode 可以是 'r' ，表示文件只能读取，'w' 表示只能写入（已存在的同名文件会被删除），还有 'a' 表示打开文件以追加内容；任何写入的数据会自动添加到文件的末尾。'r+' 表示打开文件进行读写。mode 参数是可选的；省略时默认为 'r'。

通常文件是以 text mode 打开的，这意味着从文件中读取或写入字符串时，都会以指定的编码方式进行编码。如果未指定编码格式，默认值与平台相关 (参见 open())。在mode 中追加的 'b' 则以 binary mode 打开文件：现在数据是以字节对象的形式进行读写的。这个模式应该用于所有不包含文本的文件。

在文本模式下读取时，默认会把平台特定的行结束符 (Unix 上的 \n, Windows 上的 \r\n) 转换为 \n。在文本模式下写入时，默认会把出现的 \n 转换回平台特定的结束符。这样在幕后修改文件数据对文本文件来说没有问题，但是会破坏二进制数据例如 JPEG 或 EXE 文件中的数据。请一定要注意在读写此类文件时应使用二进制模式。

It is good practice to use the with keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point. Using with is also much shorter than writing equivalent try-finally blocks:

>>> with open('workfile') as f:
...     read_data = f.read()
>>> f.closed
True

如果你没有使用 with 关键字，那么你应该调用 f.close() 来关闭文件并立即释放它使用的所有系统资源。如果你没有显式地关闭文件，Python的垃圾回收器最终将销毁该对象并为你关闭打开的文件，但这个文件可能会保持打开状态一段时间。另外一个风险是不同的Python实现会在不同的时间进行清理。

通过 with 语句或者调用 f.close() 关闭文件对象后，尝试使用该文件对象将自动失败。:

>>> f.close()
>>> f.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file.

7.2.1. 文件对象的方法

本节中剩下的例子将假定你已创建名为 f 的文件对象。

To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string (in text mode) or bytes object (in binary mode). size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, at most size bytes are read and returned. If the end of the file has been reached, f.read() will return an empty string ('').

>>> f.read()
'This is the entire file.\n'
>>> f.read()
''

f.readline() 从文件中读取一行；换行符（\n）留在字符串的末尾，如果文件不以换行符结尾，则在文件的最后一行省略。这使得返回值明确无误；如果 f.readline() 返回一个空的字符串，则表示已经到达了文件末尾，而空行使用 '\n' 表示，该字符串只包含一个换行符。:

>>> f.readline()
'This is the first line of the file.\n'
>>> f.readline()
'Second line of the file\n'
>>> f.readline()
''

要从文件中读取行，你可以循环遍历文件对象。这是内存高效，快速的，并简化代码:

>>> for line in f:
...     print(line, end='')
...
This is the first line of the file.
Second line of the file

如果你想以列表的形式读取文件中的所有行，你也可以使用 list(f) 或 f.readlines()。

f.write(string) 会把 string 的内容写入到文件中，并返回写入的字符数。:

>>> f.write('This is a test\n')
15

在写入其他类型的对象之前，需要先把它们转化为字符串（在文本模式下）或者字节对象（在二进制模式下）:

>>> value = ('the answer', 42)
>>> s = str(value)  # convert the tuple to string
>>> f.write(s)
18

f.tell() 返回一个整数，给出文件对象在文件中的当前位置，表示为二进制模式下时从文件开始的字节数，以及文本模式下的意义不明的数字。

To change the file object’s position, use f.seek(offset, from_what). The position is computed from adding offset to a reference point; the reference point is selected by the from_what argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current file position, and 2 uses the end of the file as the reference point. from_what can be omitted and defaults to 0, using the beginning of the file as the reference point.

>>> f = open('workfile', 'rb+')
>>> f.write(b'0123456789abcdef')
16
>>> f.seek(5)      # Go to the 6th byte in the file
5
>>> f.read(1)
b'5'
>>> f.seek(-3, 2)  # Go to the 3rd byte before the end
13
>>> f.read(1)
b'd'

在文本文件（那些在模式字符串中没有 b 的打开的文件）中，只允许相对于文件开头搜索（使用 seek(0, 2) 搜索到文件末尾是个例外）并且唯一有效的 offset 值是那些能从 f.tell() 中返回的或者是零。其他 offset 值都会产生未定义的行为。

文件对象有一些额外的方法，例如 isatty() 和 truncate() ，它们使用频率较低；有关文件对象的完整指南请参阅库参考。

7.2.2. 使用 json 保存结构化数据

字符串可以很轻松地写入文件并从文件中读取出来。数字可能会费点劲，因为 read() 方法只能返回字符串，这些字符串必须传递给类似 int() 的函数，它会接受类似 '123' 这样的字符串并返回其数字值 123。当你想保存诸如嵌套列表和字典这样更复杂的数据类型时，手动解析和序列化会变得复杂。

Python 允许你使用称为 JSON (JavaScript Object Notation) [http://json.org] 的流行数据交换格式，而不是让用户不断的编写和调试代码以将复杂的数据类型保存到文件中。名为 json 的标准模块可以采用 Python 数据层次结构，并将它们转化为字符串表示形式；这个过程称为 serializing 。从字符串表示中重建数据称为 deserializing 。在序列化和反序列化之间，表示对象的字符串可能已存储在文件或数据中，或通过网络连接发送到某个远程机器。

注解

JSON格式通常被现代应用程序用于允许数据交换。许多程序员已经熟悉它，这使其成为互操作性的良好选择。

如果你有一个对象 x ，你可以用一行简单的代码来查看它的 JSON 字符串表示:

>>> import json
>>> json.dumps([1, 'simple', 'list'])
'[1, "simple", "list"]'

dumps() 函数的另一个变体叫做 dump() ，它只是将对象序列化为 text file 。因此，如果 f 是一个 text file 对象，我们可以这样做:

json.dump(x, f)

要再次解码对象，如果 f 是一个打开的以供阅读的 text file 对象:

x = json.load(f)

这种简单的序列化技术可以处理列表和字典，但是在JSON中序列化任意类的实例需要额外的努力。 json 模块的参考包含对此的解释。

参见

pickle - 封存模块

与 JSON 不同，pickle 是一种允许对任意复杂 Python 对象进行序列化的协议。因此，它为 Python 所特有，不能用于与其他语言编写的应用程序通信。默认情况下它也是不安全的：如果数据是由熟练的攻击者精心设计的，则反序列化来自不受信任来源的 pickle 数据可以执行任意代码。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

8. 错误和异常

到目前为止，我们还没有提到错误消息，但是如果你已经尝试过那些例子，你可能已经看过了一些错误消息。 目前（至少）有两种可区分的错误：语法错误 和 异常。

8.1. 语法错误

语法错误又称解析错误，可能是你在学习Python 时最容易遇到的错误:

>>> while True print('Hello world')
  File "<stdin>", line 1
    while True print('Hello world')
                   ^
SyntaxError: invalid syntax

解析器会输出出现语法错误的那一行，并显示一个“箭头”，指向这行里面检测到的第一个错误。 错误是由箭头指示的位置 上面 的 token 引起的（或者至少是在这里被检测出的）：在示例中，在 print() 这个函数中检测到了错误，因为在它前面少了个冒号 (':') 。文件名和行号也会被输出，以便输入来自脚本文件时你能知道去哪检查。

8.2. 异常

即使语句或表达式在语法上是正确的，但在尝试执行时，它仍可能会引发错误。 在执行时检测到的错误被称为 异常，异常不一定会导致严重后果：你将很快学会如何在 Python 程序中处理它们。 但是，大多数异常并不会被程序处理，此时会显示如下所示的错误信息:

>>> 10 * (1/0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> 4 + spam*3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'spam' is not defined
>>> '2' + 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly

错误信息的最后一行告诉我们程序遇到了什么类型的错误。异常有不同的类型，而其类型名称将会作为错误信息的一部分中打印出来：上述示例中的异常类型依次是：ZeroDivisionError， NameError 和 TypeError。作为异常类型打印的字符串是发生的内置异常的名称。对于所有内置异常都是如此，但对于用户定义的异常则不一定如此（虽然这是一个有用的规范）。标准的异常类型是内置的标识符（而不是保留关键字）。

这一行的剩下的部分根据异常类型及其原因提供详细信息。

错误信息的前一部分以堆栈回溯的形式显示发生异常时的上下文。通常它包含列出源代码行的堆栈回溯；但是它不会显示从标准输入中读取的行。

内置异常 列出了内置异常和它们的含义。

8.3. 处理异常

可以编写处理所选异常的程序。请看下面的例子，它会要求用户一直输入，直到输入的是一个有效的整数，但允许用户中断程序（使用 Control-C 或操作系统支持的其他操作）；请注意用户引起的中断可以通过引发 KeyboardInterrupt 异常来指示。:

>>> while True:
...     try:
...         x = int(input("Please enter a number: "))
...         break
...     except ValueError:
...         print("Oops!  That was no valid number.  Try again...")
...

try 语句的工作原理如下：

首先，执行 try 子句 （try 和 except 关键字之间的（多行）语句）。

如果没有异常发生，则跳过 except 子句 并完成 try 语句的执行。

如果在执行 try 子句时发生了异常，则跳过该子句中剩下的部分。 然后，如果异常的类型和 except 关键字后面的异常匹配，则执行 except 子句，然后继续执行 try 语句之后的代码。

如果发生的异常和 except 子句中指定的异常不匹配，则将其传递到外部的 try 语句中；如果没有找到处理程序，则它是一个 未处理异常，执行将停止并显示如上所示的消息。

A try statement may have more than one except clause, to specify handlers for different exceptions. At most one handler will be executed. Handlers only handle exceptions that occur in the corresponding try clause, not in other handlers of the same try statement. An except clause may name multiple exceptions as a parenthesized tuple, for example:

... except (RuntimeError, TypeError, NameError):
...     pass

如果发生的异常和 except 子句中的类是同一个类或者是它的基类，则异常和 except 子句中的类是兼容的（但反过来则不成立 — 列出派生类的 except 子句与基类不兼容）。 例如，下面的代码将依次打印 B, C, D

class B(Exception):
    pass

class C(B):
    pass

class D(C):
    pass

for cls in [B, C, D]:
    try:
        raise cls()
    except D:
        print("D")
    except C:
        print("C")
    except B:
        print("B")

请注意如果 except 子句被颠倒（把 except B 放到第一个），它将打印 B，B，B — 即第一个匹配的 except 子句被触发。

最后的 except 子句可以省略异常名，以用作通配符。但请谨慎使用，因为以这种方式很容易掩盖真正的编程错误！它还可用于打印错误消息，然后重新引发异常（同样允许调用者处理异常）:

import sys

try:
    f = open('myfile.txt')
    s = f.readline()
    i = int(s.strip())
except OSError as err:
    print("OS error: {0}".format(err))
except ValueError:
    print("Could not convert data to an integer.")
except:
    print("Unexpected error:", sys.exc_info()[0])
    raise

try … except 语句有一个可选的 else 子句，在使用时必须放在所有的 except 子句后面。对于在 try 子句不引发异常时必须执行的代码来说很有用。 例如:

for arg in sys.argv[1:]:
    try:
        f = open(arg, 'r')
    except OSError:
        print('cannot open', arg)
    else:
        print(arg, 'has', len(f.readlines()), 'lines')
        f.close()

The use of the else clause is better than adding additional code to the try clause because it avoids accidentally catching an exception that wasn’t raised by the code being protected by the try … except statement.

发生异常时，它可能具有关联值，也称为异常 参数 。参数的存在和类型取决于异常类型。

except 子句可以在异常名称后面指定一个变量。这个变量和一个异常实例绑定，它的参数存储在 instance.args 中。为了方便起见，异常实例定义了 __str__() ，因此可以直接打印参数而无需引用 .args 。也可以在抛出之前首先实例化异常，并根据需要向其添加任何属性。:

>>> try:
...     raise Exception('spam', 'eggs')
... except Exception as inst:
...     print(type(inst))    # the exception instance
...     print(inst.args)     # arguments stored in .args
...     print(inst)          # __str__ allows args to be printed directly,
...                          # but may be overridden in exception subclasses
...     x, y = inst.args     # unpack args
...     print('x =', x)
...     print('y =', y)
...
<class 'Exception'>
('spam', 'eggs')
('spam', 'eggs')
x = spam
y = eggs

如果异常有参数，则它们将作为未处理异常的消息的最后一部分（’详细信息’）打印。

异常处理程序不仅处理 try 子句中遇到的异常，还处理 try 子句中调用（即使是间接地）的函数内部发生的异常。例如:

>>> def this_fails():
...     x = 1/0
...
>>> try:
...     this_fails()
... except ZeroDivisionError as err:
...     print('Handling run-time error:', err)
...
Handling run-time error: division by zero

8.4. 抛出异常

raise 语句允许程序员强制发生指定的异常。例如:

>>> raise NameError('HiThere')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: HiThere

raise 唯一的参数就是要抛出的异常。这个参数必须是一个异常实例或者是一个异常类（派生自 Exception 的类）。如果传递的是一个异常类，它将通过调用没有参数的构造函数来隐式实例化:

raise ValueError  # shorthand for 'raise ValueError()'

如果你需要确定是否引发了异常但不打算处理它，则可以使用更简单的 raise 语句形式重新引发异常

>>> try:
...     raise NameError('HiThere')
... except NameError:
...     print('An exception flew by!')
...     raise
...
An exception flew by!
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
NameError: HiThere

8.5. 用户自定义异常

程序可以通过创建新的异常类来命名它们自己的异常（有关Python 类的更多信息，请参阅 类）。异常通常应该直接或间接地从 Exception 类派生。

可以定义异常类，它可以执行任何其他类可以执行的任何操作，但通常保持简单，只提供一些属性，这些属性允许处理程序为异常提取有关错误的信息。 在创建可能引发多个不同错误的模块时，通常的做法是为该模块定义的异常创建基类，并为不同错误条件创建特定异常类的子类:

class Error(Exception):
    """Base class for exceptions in this module."""
    pass

class InputError(Error):
    """Exception raised for errors in the input.

    Attributes:
        expression -- input expression in which the error occurred
        message -- explanation of the error
    """

    def __init__(self, expression, message):
        self.expression = expression
        self.message = message

class TransitionError(Error):
    """Raised when an operation attempts a state transition that's not
    allowed.

    Attributes:
        previous -- state at beginning of transition
        next -- attempted new state
        message -- explanation of why the specific transition is not allowed
    """

    def __init__(self, previous, next, message):
        self.previous = previous
        self.next = next
        self.message = message

大多数异常都定义为名称以“Error”结尾，类似于标准异常的命名。

许多标准模块定义了它们自己的异常，以报告它们定义的函数中可能出现的错误。有关类的更多信息，请参见 类 章节。

8.6. 定义清理操作

try 语句有另一个可选子句，用于定义必须在所有情况下执行的清理操作。例如:

>>> try:
...     raise KeyboardInterrupt
... finally:
...     print('Goodbye, world!')
...
Goodbye, world!
KeyboardInterrupt
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>

A finally clause is always executed before leaving the try statement, whether an exception has occurred or not. When an exception has occurred in the try clause and has not been handled by an except clause (or it has occurred in an except or else clause), it is re-raised after the finally clause has been executed. The finally clause is also executed “on the way out” when any other clause of the try statement is left via a break, continue or return statement. A more complicated example:

>>> def divide(x, y):
...     try:
...         result = x / y
...     except ZeroDivisionError:
...         print("division by zero!")
...     else:
...         print("result is", result)
...     finally:
...         print("executing finally clause")
...
>>> divide(2, 1)
result is 2.0
executing finally clause
>>> divide(2, 0)
division by zero!
executing finally clause
>>> divide("2", "1")
executing finally clause
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in divide
TypeError: unsupported operand type(s) for /: 'str' and 'str'

As you can see, the finally clause is executed in any event. The TypeError raised by dividing two strings is not handled by the except clause and therefore re-raised after the finally clause has been executed.

在实际应用程序中，finally 子句对于释放外部资源（例如文件或者网络连接）非常有用，无论是否成功使用资源。

8.7. 预定义的清理操作

某些对象定义了在不再需要该对象时要执行的标准清理操作，无论使用该对象的操作是成功还是失败，清理操作都会被执行。 请查看下面的示例，它尝试打开一个文件并把其内容打印到屏幕上。:

for line in open("myfile.txt"):
    print(line, end="")

这个代码的问题在于，它在这部分代码执行完后，会使文件在一段不确定的时间内处于打开状态。这在简单脚本中不是问题，但对于较大的应用程序来说可能是个问题。 with 语句允许像文件这样的对象能够以一种确保它们得到及时和正确的清理的方式使用。:

with open("myfile.txt") as f:
    for line in f:
        print(line, end="")

执行完语句后，即使在处理行时遇到问题，文件 f 也始终会被关闭。和文件一样，提供预定义清理操作的对象将在其文档中指出这一点。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

9. 类

类提供了一种组合数据和功能的方法。 创建一个新类意味着创建一个新的对象 类型，从而允许创建一个该类型的新 实例 。 每个类的实例可以拥有保存自己状态的属性。 一个类的实例也可以有改变自己状态的（定义在类中的）方法。

和其他编程语言相比，Python 用非常少的新语法和语义将类加入到语言中。它是 C++ 和 Modula-3 中类机制的结合。Python 的类提供了面向对象编程的所有标准特性：类继承机制允许多个基类，派生类可以覆盖它基类的任何方法，一个方法可以调用基类中相同名称的的方法。对象可以包含任意数量和类型的数据。和模块一样，类也拥有 Python 天然的动态特性：它们在运行时创建，可以在创建后修改。

在 C++ 术语中，通常类成员（包括数据成员）是 public (例外见下文 私有变量)，所有成员函数都是 virtual。 与在 Modula-3 中一样，没有用于从其方法引用对象成员的简写：方法函数使用表示对象的显式第一个参数声明，该参数由调用隐式提供。 与 Smalltalk 一样，类本身也是对象。 这为导入和重命名提供了语义。 与 C++ 和 Modula-3 不同，内置类型可以用作用户扩展的基类。 此外，与 C++ 一样，大多数具有特殊语法（算术运算符，下标等）的内置运算符都可以为类实例而重新定义。

（由于缺乏关于类的公认术语，我会偶尔使用 Smalltalk 和 C++ 的用辞。 我还会使用 Modula-3 的术语，因为其面向对象的语义比 C++ 更接近 Python，但我预计少有读者听说过它。）

9.1. 名称和对象

对象具有个性，多个名称（在多个作用域内）可以绑定到同一个对象。 这在其他语言中称为别名。 乍一看 Python 时通常不会理解这一点，在处理不可变的基本类型（数字，字符串，元组）时可以安全地忽略它。 但是，别名对涉及可变对象，如列表，字典和大多数其他类型的 Python 代码的语义可能会产生惊人的影响。 通常这样做是为了使程序受益，因为别名在某些方面表现得像指针。 例如，传递一个对象的代价很小，因为实现只传递一个指针；如果函数修改了作为参数传递的对象，调用者将看到更改 — 这就不需要像 Pascal 中那样使用两个不同的参数传递机制。

9.2. Python 作用域和命名空间

在介绍类之前，我首先要告诉你一些 Python 的作用域规则。类定义对命名空间有一些巧妙的技巧，你需要知道作用域和命名空间如何工作才能完全理解正在发生的事情。顺便说一下，关于这个主题的知识对任何高级 Python 程序员都很有用。

让我们从一些定义开始。

namespace （命名空间）是一个从名字到对象的映射。 大部分命名空间当前都由 Python 字典实现，但一般情况下基本不会去关注它们（除了要面对性能问题时），而且也有可能在将来更改。 下面是几个命名空间的例子：存放内置函数的集合（包含 abs() 这样的函数，和内建的异常等）；模块中的全局名称；函数调用中的局部名称。 从某种意义上说，对象的属性集合也是一种命名空间的形式。 关于命名空间的重要一点是，不同命名空间中的名称之间绝对没有关系；例如，两个不同的模块都可以定义一个 maximize 函数而不会产生混淆 — 模块的用户必须在其前面加上模块名称。

顺便说明一下，我把任何跟在一个点号之后的名称都称为 属性 — 例如，在表达式 z.real 中，real 是对象 z 的一个属性。按严格的说法，对模块中名称的引用属于属性引用：在表达式 modname.funcname 中，modname 是一个模块对象而 funcname 是它的一个属性。在此情况下在模块的属性和模块中定义的全局名称之间正好存在一个直观的映射：它们共享相同的命名空间！ 1

属性可以是只读或者可写的。如果为后者，那么对属性的赋值是可行的。模块属性是可以写，你可以写出 modname.the_answer = 42 。可写的属性同样可以用 del 语句删除。例如， del modname.the_answer 将会从名为 modname 的对象中移除 the_answer 属性。

在不同时刻创建的命名空间拥有不同的生存期。包含内置名称的命名空间是在 Python 解释器启动时创建的，永远不会被删除。模块的全局命名空间在模块定义被读入时创建；通常，模块命名空间也会持续到解释器退出。被解释器的顶层调用执行的语句，从一个脚本文件读取或交互式地读取，被认为是 __main__ 模块调用的一部分，因此它们拥有自己的全局命名空间。（内置名称实际上也存在于一个模块中；这个模块称作 builtins 。）

一个函数的本地命名空间在这个函数被调用时创建，并在函数返回或抛出一个不在函数内部处理的错误时被删除。（事实上，比起描述到底发生了什么，忘掉它更好。）当然，每次递归调用都会有它自己的本地命名空间。

一个 作用域 是一个命名空间可直接访问的 Python 程序的文本区域。 这里的 “可直接访问” 意味着对名称的非限定引用会尝试在命名空间中查找名称。

作用域被静态确定，但被动态使用。 在程序运行的任何时间，至少有三个命名空间可被直接访问的嵌套作用域：

最先搜索的最内部作用域包含局部名称

从最近的封闭作用域开始搜索的任何封闭函数的作用域包含非局部名称，也包括非全局名称

倒数第二个作用域包含当前模块的全局名称

最外面的作用域（最后搜索）是包含内置名称的命名空间

如果一个名称被声明为全局变量，则所有引用和赋值将直接指向包含该模块的全局名称的中间作用域。 要重新绑定在最内层作用域以外找到的变量，可以使用 nonlocal 语句声明为非本地变量。 如果没有被声明为非本地变量，这些变量将是只读的（尝试写入这样的变量只会在最内层作用域中创建一个 新的 局部变量，而同名的外部变量保持不变）。

通常，当前局部作为域将（按字面文本）引用当前函数的局部名称。 在函数以外，局部作用域将引用与全局作用域相一致的命名空间：模块的命名空间。 类定义将在局部命名空间内再放置另一个命名空间。

重要的是应该意识到作用域是按字面文本来确定的：在一个模块内定义的函数的全局作用域就是该模块的命名空间，无论该函数从什么地方或以什么别名被调用。 另一方面，实际的名称搜索是在运行时动态完成的 — 但是，Python 正在朝着“编译时静态名称解析”的方向发展，因此不要过于依赖动态名称解析！ （事实上，局部变量已经是被静态确定了。）

A special quirk of Python is that – if no global statement is in effect – assignments to names always go into the innermost scope. Assignments do not copy data — they just bind names to objects. The same is true for deletions: the statement del x removes the binding of x from the namespace referenced by the local scope. In fact, all operations that introduce new names use the local scope: in particular, import statements and function definitions bind the module or function name in the local scope.

global 语句可被用来表明特定变量生存于全局作用域并且应当在其中被重新绑定；nonlocal 语句表明特定变量生存于外层作用域中并且应当在其中被重新绑定。

9.2.1. 作用域和命名空间示例

这个例子演示了如何引用不同作用域和名称空间，以及 global 和 nonlocal 会如何影响变量绑定:

def scope_test():
    def do_local():
        spam = "local spam"

    def do_nonlocal():
        nonlocal spam
        spam = "nonlocal spam"

    def do_global():
        global spam
        spam = "global spam"

    spam = "test spam"
    do_local()
    print("After local assignment:", spam)
    do_nonlocal()
    print("After nonlocal assignment:", spam)
    do_global()
    print("After global assignment:", spam)

scope_test()
print("In global scope:", spam)

示例代码的输出是：

After local assignment: test spam
After nonlocal assignment: nonlocal spam
After global assignment: nonlocal spam
In global scope: global spam

请注意 局部 赋值（这是默认状态）不会改变 scope_test 对 spam 的绑定。 nonlocal 赋值会改变 scope_test 对 spam 的绑定，而 global 赋值会改变模块层级的绑定。

您还可以在 global 赋值之前看到之前没有 spam 的绑定。

9.3. 初探类

类引入了一些新语法，三种新对象类型和一些新语义。

9.3.1. 类定义语法

最简单的类定义看起来像这样:

class ClassName:
    <statement-1>
    .
    .
    .
    <statement-N>

类定义与函数定义 (def 语句) 一样必须被执行才会起作用。 （你可以尝试将类定义放在 if 语句的一个分支或是函数的内部。）

在实践中，类定义内的语句通常都是函数定义，但也允许有其他语句，有时还很有用 — 我们会稍后再回来说明这个问题。 在类内部的函数定义通常具有一种特别形式的参数列表，这是方法调用的约定规范所指明的 — 这个问题也将在稍后再说明。

当进入类定义时，将创建一个新的命名空间，并将其用作局部作用域 — 因此，所有对局部变量的赋值都是在这个新命名空间之内。 特别的，函数定义会绑定到这里的新函数名称。

当（从结尾处）正常离开类定义时，将创建一个 类对象。 这基本上是一个包围在类定义所创建命名空间内容周围的包装器；我们将在下一节了解有关类对象的更多信息。 原始的（在进入类定义之前起作用的）局部作用域将重新生效，类对象将在这里被绑定到类定义头所给出的类名称 (在这个示例中为 ClassName)。

9.3.2. 类对象

类对象支持两种操作：属性引用和实例化。

属性引用 使用 Python 中所有属性引用所使用的标准语法: obj.name。 有效的属性名称是类对象被创建时存在于类命名空间中的所有名称。 因此，如果类定义是这样的:

class MyClass:
    """A simple example class"""
    i = 12345

    def f(self):
        return 'hello world'

那么 MyClass.i 和 MyClass.f 就是有效的属性引用，将分别返回一个整数和一个函数对象。 类属性也可以被赋值，因此可以通过赋值来更改 MyClass.i 的值。 __doc__ 也是一个有效的属性，将返回所属类的文档字符串: "A simple example class"。

类的 实例化 使用函数表示法。 可以把类对象视为是返回该类的一个新实例的不带参数的函数。 举例来说（假设使用上述的类）:

x = MyClass()

创建类的新 实例 并将此对象分配给局部变量 x。

实例化操作（“调用”类对象）会创建一个空对象。 许多类喜欢创建带有特定初始状态的自定义实例。 为此类定义可能包含一个名为 __init__() 的特殊方法，就像这样:

def __init__(self):
    self.data = []

当一个类定义了 __init__() 方法时，类的实例化操作会自动为新创建的类实例发起调用 __init__()。 因此在这个示例中，可以通过以下语句获得一个经初始化的新实例:

x = MyClass()

当然，__init__() 方法还可以有额外参数以实现更高灵活性。 在这种情况下，提供给类实例化运算符的参数将被传递给 __init__()。 例如，:

>>> class Complex:
...     def __init__(self, realpart, imagpart):
...         self.r = realpart
...         self.i = imagpart
...
>>> x = Complex(3.0, -4.5)
>>> x.r, x.i
(3.0, -4.5)

9.3.3. 实例对象

现在我们能用实例对象做什么？ 实例对象理解的唯一操作是属性引用。 有两种有效的属性名称：数据属性和方法。

数据属性 对应于 Smalltalk 中的“实例变量”，以及 C++ 中的“数据成员”。 数据属性不需要声明；像局部变量一样，它们将在第一次被赋值时产生。 例如，如果 x 是上面创建的 MyClass 的实例，则以下代码段将打印数值 16，且不保留任何追踪信息:

x.counter = 1
while x.counter < 10:
    x.counter = x.counter * 2
print(x.counter)
del x.counter

另一类实例属性引用称为 方法。 方法是“从属于”对象的函数。 （在 Python 中，方法这个术语并不是类实例所特有的：其他对象也可以有方法。 例如，列表对象具有 append, insert, remove, sort 等方法。 然而，在以下讨论中，我们使用方法一词将专指类实例对象的方法，除非另外显式地说明。）

实例对象的有效方法名称依赖于其所属的类。 根据定义，一个类中所有是函数对象的属性都是定义了其实例的相应方法。 因此在我们的示例中，x.f 是有效的方法引用，因为 MyClass.f 是一个函数，而 x.i 不是方法，因为 MyClass.i 不是一个函数。 但是 x.f 与 MyClass.f 并不是一回事 — 它是一个 方法对象，不是函数对象。

9.3.4. 方法对象

通常，方法在绑定后立即被调用:

x.f()

在 MyClass 示例中，这将返回字符串 'hello world'。 但是，立即调用一个方法并不是必须的: x.f 是一个方法对象，它可以被保存起来以后再调用。 例如:

xf = x.f
while True:
    print(xf())

将继续打印 hello world，直到结束。

当一个方法被调用时到底发生了什么？ 你可能已经注意到上面调用 x.f() 时并没有带参数，虽然 f() 的函数定义指定了一个参数。 这个参数发生了什么事？ 当不带参数地调用一个需要参数的函数时 Python 肯定会引发异常 — 即使参数实际未被使用…

实际上，你可能已经猜到了答案：方法的特殊之处就在于实例对象会作为函数的第一个参数被传入。 在我们的示例中，调用 x.f() 其实就相当于 MyClass.f(x)。 总之，调用一个具有 n 个参数的方法就相当于调用再多一个参数的对应函数，这个参数值为方法所属实例对象，位置在其他参数之前。

如果你仍然无法理解方法的运作原理，那么查看实现细节可能会澄清问题。 当一个实例的非数据属性被引用时，将搜索实例所属的类。 如果名称表示一个属于函数对象的有效类属性，会通过合并打包（指向）实例对象和函数对象到一个抽象对象中的方式来创建一个方法对象：这个抽象对象就是方法对象。 当附带参数列表调用方法对象时，将基于实例对象和参数列表构建一个新的参数列表，并使用这个新参数列表调用相应的函数对象。

9.3.5. 类和实例变量

一般来说，实例变量用于每个实例的唯一数据，而类变量用于类的所有实例共享的属性和方法:

class Dog:

    kind = 'canine'         # class variable shared by all instances

    def __init__(self, name):
        self.name = name    # instance variable unique to each instance

>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.kind                  # shared by all dogs
'canine'
>>> e.kind                  # shared by all dogs
'canine'
>>> d.name                  # unique to d
'Fido'
>>> e.name                  # unique to e
'Buddy'

正如 名称和对象 中已讨论过的，共享数据可能在涉及 mutable 对象例如列表和字典的时候导致令人惊讶的结果。 例如以下代码中的 tricks 列表不应该被用作类变量，因为所有的 Dog 实例将只共享一个单独的列表:

class Dog:

    tricks = []             # mistaken use of a class variable

    def __init__(self, name):
        self.name = name

    def add_trick(self, trick):
        self.tricks.append(trick)

>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks                # unexpectedly shared by all dogs
['roll over', 'play dead']

正确的类设计应该使用实例变量:

class Dog:

    def __init__(self, name):
        self.name = name
        self.tricks = []    # creates a new empty list for each dog

    def add_trick(self, trick):
        self.tricks.append(trick)

>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks
['roll over']
>>> e.tricks
['play dead']

9.4. 补充说明

数据属性会覆盖掉具有相同名称的方法属性；为了避免会在大型程序中导致难以发现的错误的意外名称冲突，明智的做法是使用某种约定来最小化冲突的发生几率。 可能的约定包括方法名称使用大写字母，属性名称加上独特的短字符串前缀（或许只加一个下划线），或者是用动词来命名方法，而用名词来命名数据属性。

数据属性可以被方法以及一个对象的普通用户（“客户端”）所引用。 换句话说，类不能用于实现纯抽象数据类型。 实际上，在 Python 中没有任何东西能强制隐藏数据 — 它是完全基于约定的。 （而在另一方面，用 C 语言编写的 Python 实现则可以完全隐藏实现细节，并在必要时控制对象的访问；此特性可以通过用 C 编写 Python 扩展来使用。）

客户端应当谨慎地使用数据属性 — 客户端可能通过直接操作数据属性的方式破坏由方法所维护的固定变量。 请注意客户端可以向一个实例对象添加他们自己的数据属性而不会影响方法的可用性，只要保证避免名称冲突 — 再次提醒，在此使用命名约定可以省去许多令人头痛的麻烦。

在方法内部引用数据属性（或其他方法！）并没有简便方式。 我发现这实际上提升了方法的可读性：当浏览一个方法代码时，不会存在混淆局部变量和实例变量的机会。

方法的第一个参数常常被命名为 self。 这也不过就是一个约定: self 这一名称在 Python 中绝对没有特殊含义。 但是要注意，不遵循此约定会使得你的代码对其他 Python 程序员来说缺乏可读性，而且也可以想像一个 类浏览器 程序的编写可能会依赖于这样的约定。

任何一个作为类属性的函数都为该类的实例定义了一个相应方法。 函数定义的文本并非必须包含于类定义之内：将一个函数对象赋值给一个局部变量也是可以的。 例如:

# Function defined outside the class
def f1(self, x, y):
    return min(x, x+y)

class C:
    f = f1

    def g(self):
        return 'hello world'

    h = g

现在 f, g 和 h 都是 C 类的引用函数对象的属性，因而它们就都是 C 的实例的方法 — 其中 h 完全等同于 g。 但请注意，本示例的做法通常只会令程序的阅读者感到迷惑。

方法可以通过使用 self 参数的方法属性调用其他方法:

class Bag:
    def __init__(self):
        self.data = []

    def add(self, x):
        self.data.append(x)

    def addtwice(self, x):
        self.add(x)
        self.add(x)

方法可以通过与普通函数相同的方式引用全局名称。 与方法相关联的全局作用域就是包含其定义的模块。 （类永远不会被作为全局作用域。） 虽然我们很少会有充分的理由在方法中使用全局作用域，但全局作用域存在许多合法的使用场景：举个例子，导入到全局作用域的函数和模块可以被方法所使用，在其中定义的函数和类也一样。 通常，包含该方法的类本身是在全局作用域中定义的，而在下一节中我们将会发现为何方法需要引用其所属类的很好的理由。

每个值都是一个对象，因此具有 类 （也称为 类型），并存储为 object.__class__ 。

9.5. 继承

当然，如果不支持继承，语言特性就不值得称为“类”。派生类定义的语法如下所示:

class DerivedClassName(BaseClassName):
    <statement-1>
    .
    .
    .
    <statement-N>

名称 BaseClassName 必须定义于包含派生类定义的作用域中。 也允许用其他任意表达式代替基类名称所在的位置。 这有时也可能会用得上，例如，当基类定义在另一个模块中的时候:

class DerivedClassName(modname.BaseClassName):

派生类定义的执行过程与基类相同。 当构造类对象时，基类会被记住。 此信息将被用来解析属性引用：如果请求的属性在类中找不到，搜索将转往基类中进行查找。 如果基类本身也派生自其他某个类，则此规则将被递归地应用。

派生类的实例化没有任何特殊之处: DerivedClassName() 会创建该类的一个新实例。 方法引用将按以下方式解析：搜索相应的类属性，如有必要将按基类继承链逐步向下查找，如果产生了一个函数对象则方法引用就生效。

派生类可能会重载其基类的方法。 因为方法在调用同一对象的其他方法时没有特殊权限，调用同一基类中定义的另一方法的基类方法最终可能会调用覆盖它的派生类的方法。 （对 C++ 程序员的提示：Python 中所有的方法实际上都是 virtual 方法。）

在派生类中的重载方法实际上可能想要扩展而非简单地替换同名的基类方法。 有一种方式可以简单地直接调用基类方法：即调用 BaseClassName.methodname(self, arguments)。 有时这对客户端来说也是有用的。 （请注意仅当此基类可在全局作用域中以 BaseClassName 的名称被访问时方可使用此方式。）

Python有两个内置函数可被用于继承机制：

使用 isinstance() 来检查一个实例的类型: isinstance(obj, int) 仅会在 obj.__class__ 为 int 或某个派生自 int 的类时为 True。

使用 issubclass() 来检查类的继承关系: issubclass(bool, int) 为 True，因为 bool 是 int 的子类。 但是，issubclass(float, int) 为 False，因为 float 不是 int 的子类。

9.5.1. 多重继承

Python 也支持一种多重继承。 带有多个基类的类定义语句如下所示:

class DerivedClassName(Base1, Base2, Base3):
    <statement-1>
    .
    .
    .
    <statement-N>

对于多数应用来说，在最简单的情况下，你可以认为搜索从父类所继承属性的操作是深度优先、从左至右的，当层次结构中存在重叠时不会在同一个类中搜索两次。 因此，如果某一属性在 DerivedClassName 中未找到，则会到 Base1 中搜索它，然后（递归地）到 Base1 的基类中搜索，如果在那里未找到，再到 Base2 中搜索，依此类推。

真实情况比这个更复杂一些；方法解析顺序会动态改变以支持对 super() 的协同调用。 这种方式在某些其他多重继承型语言中被称为后续方法调用，它比单继承型语言中的 super 调用更强大。

动态改变顺序是有必要的，因为所有多重继承的情况都会显示出一个或更多的菱形关联（即至少有一个父类可通过多条路径被最底层类所访问）。 例如，所有类都是继承自 object，因此任何多重继承的情况都提供了一条以上的路径可以通向 object。 为了确保基类不会被访问一次以上，动态算法会用一种特殊方式将搜索顺序线性化， 保留每个类所指定的从左至右的顺序，只调用每个父类一次，并且保持单调（即一个类可以被子类化而不影响其父类的优先顺序）。 总而言之，这些特性使得设计具有多重继承的可靠且可扩展的类成为可能。 要了解更多细节，请参阅 https://www.python.org/download/releases/2.3/mro/。

9.6. 私有变量

那种仅限从一个对象内部访问的“私有”实例变量在 Python 中并不存在。 但是，大多数 Python 代码都遵循这样一个约定：带有一个下划线的名称 (例如 _spam) 应该被当作是 API 的非公有部分 (无论它是函数、方法或是数据成员)。 这应当被视为一个实现细节，可能不经通知即加以改变。

由于存在对于类私有成员的有效使用场景（例如避免名称与子类所定义的名称相冲突），因此存在对此种机制的有限支持，称为 名称改写。 任何形式为 __spam 的标识符（至少带有两个前缀下划线，至多一个后缀下划线）的文本将被替换为 _classname__spam，其中 classname 为去除了前缀下划线的当前类名称。 这种改写不考虑标识符的句法位置，只要它出现在类定义内部就会进行。

名称改写有助于让子类重载方法而不破坏类内方法调用。例如:

class Mapping:
    def __init__(self, iterable):
        self.items_list = []
        self.__update(iterable)

    def update(self, iterable):
        for item in iterable:
            self.items_list.append(item)

    __update = update   # private copy of original update() method

class MappingSubclass(Mapping):

    def update(self, keys, values):
        # provides new signature for update()
        # but does not break __init__()
        for item in zip(keys, values):
            self.items_list.append(item)

上面的示例即使在 MappingSubclass 引入了一个 __update 标识符的情况下也不会出错，因为它会在 Mapping 类中被替换为 _Mapping__update 而在 MappingSubclass 类中被替换为 _MappingSubclass__update。

请注意，改写规则的设计主要是为了避免意外冲突；访问或修改被视为私有的变量仍然是可能的。这在特殊情况下甚至会很有用，例如在调试器中。

请注意传递给 exec() 或 eval() 的代码不会将发起调用类的类名视作当前类；这类似于 global 语句的效果，因此这种效果仅限于同时经过字节码编译的代码。 同样的限制也适用于 getattr(), setattr() 和 delattr()，以及对于 __dict__ 的直接引用。

9.7. 杂项说明

有时会需要使用类似于 Pascal 的“record”或 C 的“struct”这样的数据类型，将一些命名数据项捆绑在一起。 这种情况适合定义一个空类:

class Employee:
    pass

john = Employee()  # Create an empty employee record

# Fill the fields of the record
john.name = 'John Doe'
john.dept = 'computer lab'
john.salary = 1000

一段需要特定抽象数据类型的 Python 代码往往可以被传入一个模拟了该数据类型的方法的类作为替代。 例如，如果你有一个基于文件对象来格式化某些数据的函数，你可以定义一个带有 read() 和 readline() 方法从字符串缓存获取数据的类，并将其作为参数传入。

实例方法对象也具有属性: m.__self__ 就是带有 m() 方法的实例对象，而 m.__func__ 则是该方法所对应的函数对象。

9.8. 迭代器

到目前为止，您可能已经注意到大多数容器对象都可以使用 for 语句:

for element in [1, 2, 3]:
    print(element)
for element in (1, 2, 3):
    print(element)
for key in {'one':1, 'two':2}:
    print(key)
for char in "123":
    print(char)
for line in open("myfile.txt"):
    print(line, end='')

This style of access is clear, concise, and convenient. The use of iterators pervades and unifies Python. Behind the scenes, the for statement calls iter() on the container object. The function returns an iterator object that defines the method __next__() which accesses elements in the container one at a time. When there are no more elements, __next__() raises a StopIteration exception which tells the for loop to terminate. You can call the __next__() method using the next() built-in function; this example shows how it all works:

>>> s = 'abc'
>>> it = iter(s)
>>> it
<iterator object at 0x00A1DB50>
>>> next(it)
'a'
>>> next(it)
'b'
>>> next(it)
'c'
>>> next(it)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    next(it)
StopIteration

看过迭代器协议的幕后机制，给你的类添加迭代器行为就很容易了。 定义一个 __iter__() 方法来返回一个带有 __next__() 方法的对象。 如果类已定义了 __next__()，则 __iter__() 可以简单地返回 self:

class Reverse:
    """Iterator for looping over a sequence backwards."""
    def __init__(self, data):
        self.data = data
        self.index = len(data)

    def __iter__(self):
        return self

    def __next__(self):
        if self.index == 0:
            raise StopIteration
        self.index = self.index - 1
        return self.data[self.index]

>>> rev = Reverse('spam')
>>> iter(rev)
<__main__.Reverse object at 0x00A1DB50>
>>> for char in rev:
...     print(char)
...
m
a
p
s

9.9. 生成器

Generator 是一个用于创建迭代器的简单而强大的工具。 它们的写法类似标准的函数，但当它们要返回数据时会使用 yield 语句。 每次对生成器调用 next() 时，它会从上次离开位置恢复执行（它会记住上次执行语句时的所有数据值）。 显示如何非常容易地创建生成器的示例如下:

def reverse(data):
    for index in range(len(data)-1, -1, -1):
        yield data[index]

>>> for char in reverse('golf'):
...     print(char)
...
f
l
o
g

可以用生成器来完成的操作同样可以用前一节所描述的基于类的迭代器来完成。 但生成器的写法更为紧凑，因为它会自动创建 __iter__() 和 __next__() 方法。

另一个关键特性在于局部变量和执行状态会在每次调用之间自动保存。 这使得该函数相比使用 self.index 和 self.data 这种实例变量的方式更易编写且更为清晰。

除了会自动创建方法和保存程序状态，当生成器终结时，它们还会自动引发 StopIteration。 这些特性结合在一起，使得创建迭代器能与编写常规函数一样容易。

9.10. 生成器表达式

某些简单的生成器可以写成简洁的表达式代码，所用语法类似列表推导式，但外层为圆括号而非方括号。 这种表达式被设计用于生成器将立即被外层函数所使用的情况。 生成器表达式相比完整的生成器更紧凑但较不灵活，相比等效的列表推导式则更为节省内存。

示例:

>>> sum(i*i for i in range(10))                 # sum of squares
285

>>> xvec = [10, 20, 30]
>>> yvec = [7, 5, 3]
>>> sum(x*y for x,y in zip(xvec, yvec))         # dot product
260

>>> from math import pi, sin
>>> sine_table = {x: sin(x*pi/180) for x in range(0, 91)}

>>> unique_words = set(word  for line in page  for word in line.split())

>>> valedictorian = max((student.gpa, student.name) for student in graduates)

>>> data = 'golf'
>>> list(data[i] for i in range(len(data)-1, -1, -1))
['f', 'l', 'o', 'g']

备注

1

存在一个例外。 模块对象有一个秘密的只读属性 __dict__，它返回用于实现模块命名空间的字典；__dict__ 是属性但不是全局名称。 显然，使用这个将违反命名空间实现的抽象，应当仅被用于事后调试器之类的场合。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

10. 标准库简介

10.1. 操作系统接口

os 模块提供了许多与操作系统交互的函数:

>>> import os
>>> os.getcwd()      # Return the current working directory
'C:\\Python36'
>>> os.chdir('/server/accesslogs')   # Change current working directory
>>> os.system('mkdir today')   # Run the command mkdir in the system shell
0

一定要使用 import os 而不是 from os import * 。这将避免内建的 open() 函数被 os.open() 隐式替换掉，它们的使用方式大不相同。

内置的 dir() 和 help() 函数可用作交互式辅助工具，用于处理大型模块，如 os:

>>> import os
>>> dir(os)
<returns a list of all module functions>
>>> help(os)
<returns an extensive manual page created from the module's docstrings>

对于日常文件和目录管理任务， shutil 模块提供了更易于使用的更高级别的接口:

>>> import shutil
>>> shutil.copyfile('data.db', 'archive.db')
'archive.db'
>>> shutil.move('/build/executables', 'installdir')
'installdir'

10.2. 文件通配符

glob 模块提供了一个在目录中使用通配符搜索创建文件列表的函数:

>>> import glob
>>> glob.glob('*.py')
['primes.py', 'random.py', 'quote.py']

10.3. 命令行参数

通用实用程序脚本通常需要处理命令行参数。这些参数作为列表存储在 sys 模块的 argv 属性中。例如，以下输出来自在命令行运行 python demo.py one two three

>>> import sys
>>> print(sys.argv)
['demo.py', 'one', 'two', 'three']

The getopt module processes sys.argv using the conventions of the Unix getopt() function. More powerful and flexible command line processing is provided by the argparse module.

10.4. 错误输出重定向和程序终止

sys 模块还具有 stdin ， stdout 和 stderr 的属性。后者对于发出警告和错误消息非常有用，即使在 stdout 被重定向后也可以看到它们:

>>> sys.stderr.write('Warning, log file not found starting a new one\n')
Warning, log file not found starting a new one

终止脚本的最直接方法是使用 sys.exit() 。

10.5. 字符串模式匹配

re 模块为高级字符串处理提供正则表达式工具。对于复杂的匹配和操作，正则表达式提供简洁，优化的解决方案:

>>> import re
>>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
['foot', 'fell', 'fastest']
>>> re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')
'cat in the hat'

当只需要简单的功能时，首选字符串方法因为它们更容易阅读和调试:

>>> 'tea for too'.replace('too', 'two')
'tea for two'

10.6. 数学

math 模块提供对浮点数学的底层C库函数的访问:

>>> import math
>>> math.cos(math.pi / 4)
0.70710678118654757
>>> math.log(1024, 2)
10.0

random 模块提供了进行随机选择的工具:

>>> import random
>>> random.choice(['apple', 'pear', 'banana'])
'apple'
>>> random.sample(range(100), 10)   # sampling without replacement
[30, 83, 16, 4, 8, 81, 41, 50, 18, 33]
>>> random.random()    # random float
0.17970987693706186
>>> random.randrange(6)    # random integer chosen from range(6)
4

statistics 模块计算数值数据的基本统计属性（均值，中位数，方差等）:

>>> import statistics
>>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
>>> statistics.mean(data)
1.6071428571428572
>>> statistics.median(data)
1.25
>>> statistics.variance(data)
1.3720238095238095

SciPy项目 <https://scipy.org> 有许多其他模块用于数值计算。

10.7. 互联网访问

有许多模块可用于访问互联网和处理互联网协议。其中两个最简单的 urllib.request 用于从URL检索数据，以及 smtplib 用于发送邮件:

>>> from urllib.request import urlopen
>>> with urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl') as response:
...     for line in response:
...         line = line.decode('utf-8')  # Decoding the binary data to text.
...         if 'EST' in line or 'EDT' in line:  # look for Eastern Time
...             print(line)

<BR>Nov. 25, 09:43:32 PM EST

>>> import smtplib
>>> server = smtplib.SMTP('localhost')
>>> server.sendmail('soothsayer@example.org', 'jcaesar@example.org',
... """To: jcaesar@example.org
... From: soothsayer@example.org
...
... Beware the Ides of March.
... """)
>>> server.quit()

（请注意，第二个示例需要在localhost上运行的邮件服务器。）

10.8. 日期和时间

datetime 模块提供了以简单和复杂的方式操作日期和时间的类。虽然支持日期和时间算法，但实现的重点是有效的成员提取以进行输出格式化和操作。该模块还支持可感知时区的对象。

>>> # dates are easily constructed and formatted
>>> from datetime import date
>>> now = date.today()
>>> now
datetime.date(2003, 12, 2)
>>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
'12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'

>>> # dates support calendar arithmetic
>>> birthday = date(1964, 7, 31)
>>> age = now - birthday
>>> age.days
14368

10.9. 数据压缩

常见的数据存档和压缩格式由模块直接支持，包括：zlib, gzip, bz2, lzma, zipfile 和 tarfile。:

>>> import zlib
>>> s = b'witch which has which witches wrist watch'
>>> len(s)
41
>>> t = zlib.compress(s)
>>> len(t)
37
>>> zlib.decompress(t)
b'witch which has which witches wrist watch'
>>> zlib.crc32(s)
226805979

10.10. 性能测量

一些Python用户对了解同一问题的不同方法的相对性能产生了浓厚的兴趣。 Python提供了一种可以立即回答这些问题的测量工具。

例如，元组封包和拆包功能相比传统的交换参数可能更具吸引力。timeit 模块可以快速演示在运行效率方面一定的优势:

>>> from timeit import Timer
>>> Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
0.57535828626024577
>>> Timer('a,b = b,a', 'a=1; b=2').timeit()
0.54962537085770791

与 timeit 的精细粒度级别相反， profile 和 pstats 模块提供了用于在较大的代码块中识别时间关键部分的工具。

10.11. 质量控制

开发高质量软件的一种方法是在开发过程中为每个函数编写测试，并在开发过程中经常运行这些测试。

doctest 模块提供了一个工具，用于扫描模块并验证程序文档字符串中嵌入的测试。测试构造就像将典型调用及其结果剪切并粘贴到文档字符串一样简单。这通过向用户提供示例来改进文档，并且它允许doctest模块确保代码保持对文档的真实:

def average(values):
    """Computes the arithmetic mean of a list of numbers.

    >>> print(average([20, 30, 70]))
    40.0
    """
    return sum(values) / len(values)

import doctest
doctest.testmod()   # automatically validate the embedded tests

unittest 模块不像 doctest 模块那样易于使用，但它允许在一个单独的文件中维护更全面的测试集:

import unittest

class TestStatisticalFunctions(unittest.TestCase):

    def test_average(self):
        self.assertEqual(average([20, 30, 70]), 40.0)
        self.assertEqual(round(average([1, 5, 7]), 1), 4.3)
        with self.assertRaises(ZeroDivisionError):
            average([])
        with self.assertRaises(TypeError):
            average(20, 30, 70)

unittest.main()  # Calling from the command line invokes all tests

10.12. 自带电池

Python有“自带电池”的理念。通过其包的复杂和强大功能可以最好地看到这一点。例如:

xmlrpc.client 和 xmlrpc.server 模块使得实现远程过程调用变成了小菜一碟。 尽管存在于模块名称中，但用户不需要直接了解或处理 XML。

 email 包是一个用于管理电子邮件的库，包括MIME和其他符合 RFC 2822 [https://tools.ietf.org/html/rfc2822.html] 规范的邮件文档。与 smtplib 和 poplib 不同（它们实际上做的是发送和接收消息），电子邮件包提供完整的工具集，用于构建或解码复杂的消息结构（包括附件）以及实现互联网编码和标头协议。

 json 包为解析这种流行的数据交换格式提供了强大的支持。 csv 模块支持以逗号分隔值格式直接读取和写入文件，这种格式通常为数据库和电子表格所支持。 XML 处理由 xml.etree.ElementTree ， xml.dom 和 xml.sax 包支持。这些模块和软件包共同大大简化了 Python 应用程序和其他工具之间的数据交换。

 sqlite3 模块是 SQLite 数据库库的包装器，提供了一个可以使用稍微非标准的 SQL 语法更新和访问的持久数据库。

国际化由许多模块支持，包括 gettext ， locale ，以及 codecs 包。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

11. 标准库简介 —— 第二部分

第二部分涵盖了专业编程所需要的更高级的模块。这些模块很少用在小脚本中。

11.1. 格式化输出

reprlib 模块提供了一个定制化版本的 repr() 函数，用于缩略显示大型或深层嵌套的容器对象:

>>> import reprlib
>>> reprlib.repr(set('supercalifragilisticexpialidocious'))
"{'a', 'c', 'd', 'e', 'f', 'g', ...}"

pprint 模块提供了更加复杂的打印控制，其输出的内置对象和用户自定义对象能够被解释器直接读取。当输出结果过长而需要折行时，“美化输出机制”会添加换行符和缩进，以更清楚地展示数据结构:

>>> import pprint
>>> t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta',
...     'yellow'], 'blue']]]
...
>>> pprint.pprint(t, width=30)
[[[['black', 'cyan'],
   'white',
   ['green', 'red']],
  [['magenta', 'yellow'],
   'blue']]]

textwrap 模块能够格式化文本段落，以适应给定的屏幕宽度:

>>> import textwrap
>>> doc = """The wrap() method is just like fill() except that it returns
... a list of strings instead of one big string with newlines to separate
... the wrapped lines."""
...
>>> print(textwrap.fill(doc, width=40))
The wrap() method is just like fill()
except that it returns a list of strings
instead of one big string with newlines
to separate the wrapped lines.

locale 模块处理与特定地域文化相关的数据格式。locale 模块的 format 函数包含一个 grouping 属性，可直接将数字格式化为带有组分隔符的样式:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'English_United States.1252')
'English_United States.1252'
>>> conv = locale.localeconv()          # get a mapping of conventions
>>> x = 1234567.8
>>> locale.format("%d", x, grouping=True)
'1,234,567'
>>> locale.format_string("%s%.*f", (conv['currency_symbol'],
...                      conv['frac_digits'], x), grouping=True)
'$1,234,567.80'

11.2. 模板

string 模块包含一个通用的 Template 类，具有适用于最终用户的简化语法。它允许用户在不更改应用逻辑的情况下定制自己的应用。

上述格式化操作是通过占位符实现的，占位符由 $ 加上合法的 Python 标识符（只能包含字母、数字和下划线）构成。一旦使用花括号将占位符括起来，就可以在后面直接跟上更多的字母和数字而无需空格分割。$$ 将被转义成单个字符 $:

>>> from string import Template
>>> t = Template('${village}folk send $$10 to $cause.')
>>> t.substitute(village='Nottingham', cause='the ditch fund')
'Nottinghamfolk send $10 to the ditch fund.'

如果在字典或关键字参数中未提供某个占位符的值，那么 substitute() 方法将抛出 KeyError。对于邮件合并类型的应用，用户提供的数据有可能是不完整的，此时使用 safe_substitute() 方法更加合适 —— 如果数据缺失，它会直接将占位符原样保留。

>>> t = Template('Return the $item to $owner.')
>>> d = dict(item='unladen swallow')
>>> t.substitute(d)
Traceback (most recent call last):
  ...
KeyError: 'owner'
>>> t.safe_substitute(d)
'Return the unladen swallow to $owner.'

Template 的子类可以自定义分隔符。例如，以下是某个照片浏览器的批量重命名功能，采用了百分号作为日期、照片序号和照片格式的占位符:

>>> import time, os.path
>>> photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']
>>> class BatchRename(Template):
...     delimiter = '%'
>>> fmt = input('Enter rename style (%d-date %n-seqnum %f-format):  ')
Enter rename style (%d-date %n-seqnum %f-format):  Ashley_%n%f

>>> t = BatchRename(fmt)
>>> date = time.strftime('%d%b%y')
>>> for i, filename in enumerate(photofiles):
...     base, ext = os.path.splitext(filename)
...     newname = t.substitute(d=date, n=i, f=ext)
...     print('{0} --> {1}'.format(filename, newname))

img_1074.jpg --> Ashley_0.jpg
img_1076.jpg --> Ashley_1.jpg
img_1077.jpg --> Ashley_2.jpg

模板的另一个应用是将程序逻辑与多样的格式化输出细节分离开来。这使得对 XML 文件、纯文本报表和 HTML 网络报表使用自定义模板成为可能。

11.3. 使用二进制数据记录格式

struct 模块提供了 pack() 和 unpack() 函数，用于处理不定长度的二进制记录格式。下面的例子展示了在不使用 zipfile 模块的情况下，如何循环遍历一个 ZIP 文件的所有头信息。Pack 代码 "H" 和 "I" 分别代表两字节和四字节无符号整数。"<" 代表它们是标准尺寸的小端字节序:

import struct

with open('myfile.zip', 'rb') as f:
    data = f.read()

start = 0
for i in range(3):                      # show the first 3 file headers
    start += 14
    fields = struct.unpack('<IIIHH', data[start:start+16])
    crc32, comp_size, uncomp_size, filenamesize, extra_size = fields

    start += 16
    filename = data[start:start+filenamesize]
    start += filenamesize
    extra = data[start:start+extra_size]
    print(filename, hex(crc32), comp_size, uncomp_size)

    start += extra_size + comp_size     # skip to the next header

11.4. 多线程

线程是一种对于非顺序依赖的多个任务进行解耦的技术。多线程可以提高应用的响应效率，当接收用户输入的同时，保持其他任务在后台运行。一个有关的应用场景是，将 I/O 和计算运行在两个并行的线程中。

以下代码展示了高阶的 threading 模块如何在后台运行任务，且不影响主程序的继续运行:

import threading, zipfile

class AsyncZip(threading.Thread):
    def __init__(self, infile, outfile):
        threading.Thread.__init__(self)
        self.infile = infile
        self.outfile = outfile

    def run(self):
        f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
        f.write(self.infile)
        f.close()
        print('Finished background zip of:', self.infile)

background = AsyncZip('mydata.txt', 'myarchive.zip')
background.start()
print('The main program continues to run in foreground.')

background.join()    # Wait for the background task to finish
print('Main program waited until background was done.')

多线程应用面临的主要挑战是，相互协调的多个线程之间需要共享数据或其他资源。为此，threading 模块提供了多个同步操作原语，包括线程锁、事件、条件变量和信号量。

尽管这些工具非常强大，但微小的设计错误却可以导致一些难以复现的问题。因此，实现多任务协作的首选方法是将所有对资源的请求集中到一个线程中，然后使用 queue 模块向该线程供应来自其他线程的请求。 应用程序使用 Queue 对象进行线程间通信和协调，更易于设计，更易读，更可靠。

11.5. 日志记录

logging 模块提供功能齐全且灵活的日志记录系统。在最简单的情况下，日志消息被发送到文件或 sys.stderr

import logging
logging.debug('Debugging information')
logging.info('Informational message')
logging.warning('Warning:config file %s not found', 'server.conf')
logging.error('Error occurred')
logging.critical('Critical error -- shutting down')

这会产生以下输出:

WARNING:root:Warning:config file server.conf not found
ERROR:root:Error occurred
CRITICAL:root:Critical error -- shutting down

默认情况下，informational 和 debugging 消息被压制，输出会发送到标准错误流。其他输出选项包括将消息转发到电子邮件，数据报，套接字或 HTTP 服务器。新的过滤器可以根据消息优先级选择不同的路由方式：DEBUG，INFO，WARNING，ERROR，和 CRITICAL。

日志系统可以直接从 Python 配置，也可以从用户配置文件加载，以便自定义日志记录而无需更改应用程序。

11.6. 弱引用

Python 会自动进行内存管理（对大多数对象进行引用计数并使用 garbage collection 来清除循环引用）。 当某个对象的最后一个引用被移除后不久就会释放其所占用的内存。

此方式对大多数应用来说都适用，但偶尔也必须在对象持续被其他对象所使用时跟踪它们。 不幸的是，跟踪它们将创建一个会令其永久化的引用。 weakref 模块提供的工具可以不必创建引用就能跟踪对象。 当对象不再需要时，它将自动从一个弱引用表中被移除，并为弱引用对象触发一个回调。 典型应用包括对创建开销较大的对象进行缓存:

>>> import weakref, gc
>>> class A:
...     def __init__(self, value):
...         self.value = value
...     def __repr__(self):
...         return str(self.value)
...
>>> a = A(10)                   # create a reference
>>> d = weakref.WeakValueDictionary()
>>> d['primary'] = a            # does not create a reference
>>> d['primary']                # fetch the object if it is still alive
10
>>> del a                       # remove the one reference
>>> gc.collect()                # run garbage collection right away
0
>>> d['primary']                # entry was automatically removed
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    d['primary']                # entry was automatically removed
  File "C:/python36/lib/weakref.py", line 46, in __getitem__
    o = self.data[key]()
KeyError: 'primary'

11.7. 用于操作列表的工具

许多对于数据结构的需求可以通过内置列表类型来满足。 但是，有时也会需要具有不同效费比的替代实现。

array 模块提供了一种 array() 对象，它类似于列表，但只能存储类型一致的数据且存储密集更高。 下面的例子演示了一个以两个字节为存储单元的无符号二进制数值的数组 (类型码为 "H")，而对于普通列表来说，每个条目存储为标准 Python 的 int 对象通常要占用16 个字节:

>>> from array import array
>>> a = array('H', [4000, 10, 700, 22222])
>>> sum(a)
26932
>>> a[1:3]
array('H', [10, 700])

collections 模块提供了一种 deque() 对象，它类似于列表，但从左端添加和弹出的速度较快，而在中间查找的速度较慢。 此种对象适用于实现队列和广度优先树搜索:

>>> from collections import deque
>>> d = deque(["task1", "task2", "task3"])
>>> d.append("task4")
>>> print("Handling", d.popleft())
Handling task1

unsearched = deque([starting_node])
def breadth_first_search(unsearched):
    node = unsearched.popleft()
    for m in gen_moves(node):
        if is_goal(m):
            return m
        unsearched.append(m)

在替代的列表实现以外，标准库也提供了其他工具，例如 bisect 模块具有用于操作有序列表的函数:

>>> import bisect
>>> scores = [(100, 'perl'), (200, 'tcl'), (400, 'lua'), (500, 'python')]
>>> bisect.insort(scores, (300, 'ruby'))
>>> scores
[(100, 'perl'), (200, 'tcl'), (300, 'ruby'), (400, 'lua'), (500, 'python')]

heapq 模块提供了基于常规列表来实现堆的函数。 最小值的条目总是保持在位置零。 这对于需要重复访问最小元素而不希望运行完整列表排序的应用来说非常有用:

>>> from heapq import heapify, heappop, heappush
>>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
>>> heapify(data)                      # rearrange the list into heap order
>>> heappush(data, -5)                 # add a new entry
>>> [heappop(data) for i in range(3)]  # fetch the three smallest entries
[-5, 0, 1]

11.8. 十进制浮点运算

decimal 模块提供了一种 Decimal 数据类型用于十进制浮点运算。 相比内置的 float 二进制浮点实现，该类特别适用于

财务应用和其他需要精确十进制表示的用途，

控制精度，

控制四舍五入以满足法律或监管要求，

跟踪有效小数位，或

用户期望结果与手工完成的计算相匹配的应用程序。

例如，使用十进制浮点和二进制浮点数计算70美分手机和5％税的总费用，会产生的不同结果。如果结果四舍五入到最接近的分数差异会更大:

>>> from decimal import *
>>> round(Decimal('0.70') * Decimal('1.05'), 2)
Decimal('0.74')
>>> round(.70 * 1.05, 2)
0.73

Decimal 表示的结果会保留尾部的零，并根据具有两个有效位的被乘数自动推出四个有效位。 Decimal 可以模拟手工运算来避免当二进制浮点数无法精确表示十进制数时会导致的问题。

精确表示特性使得 Decimal 类能够执行对于二进制浮点数来说不适用的模运算和相等性检测:

>>> Decimal('1.00') % Decimal('.10')
Decimal('0.00')
>>> 1.00 % 0.10
0.09999999999999995

>>> sum([Decimal('0.1')]*10) == Decimal('1.0')
True
>>> sum([0.1]*10) == 1.0
False

decimal 模块提供了运算所需要的足够精度:

>>> getcontext().prec = 36
>>> Decimal(1) / Decimal(7)
Decimal('0.142857142857142857142857142857142857')

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

12. 虚拟环境和包

12.1. 概述

Python应用程序通常会使用不在标准库内的软件包和模块。应用程序有时需要特定版本的库，因为应用程序可能需要修复特定的错误，或者可以使用库的过时版本的接口编写应用程序。

这意味着一个Python安装可能无法满足每个应用程序的要求。如果应用程序A需要特定模块的1.0版本但应用程序B需要2.0版本，则需求存在冲突，安装版本1.0或2.0将导致某一个应用程序无法运行。

这个问题的解决方案是创建一个 virtual environment，一个目录树，其中安装有特定Python版本，以及许多其他包。

然后，不同的应用将可以使用不同的虚拟环境。 要解决先前需求相冲突的例子，应用程序 A 可以拥有自己的 安装了 1.0 版本的虚拟环境，而应用程序 B 则拥有安装了 2.0 版本的另一个虚拟环境。 如果应用程序 B 要求将某个库升级到 3.0 版本，也不会影响应用程序 A 的环境。

12.2. 创建虚拟环境

用于创建和管理虚拟环境的模块称为 venv。venv 通常会安装你可用的最新版本的 Python。如果您的系统上有多个版本的 Python，您可以通过运行 python3 或您想要的任何版本来选择特定的Python版本。

要创建虚拟环境，请确定要放置它的目录，并将 venv 模块作为脚本运行目录路径:

python3 -m venv tutorial-env

如果它不存在，这将创建 tutorial-env 目录，并在其中创建包含Python解释器，标准库和各种支持文件的副本的目录。

创建虚拟环境后，您可以激活它。

在Windows上，运行:

tutorial-env\Scripts\activate.bat

在Unix或MacOS上，运行:

source tutorial-env/bin/activate

（这个脚本是为bash shell编写的。如果你使用 csh 或 fish shell，你应该改用 activate.csh 或 activate.fish 脚本。）

激活虚拟环境将改变shell的命令提示来显示您正在使用的虚拟环境，以及改变当前环境，使运行``python``时能使用特定版本的Python安装。例如：

$ source ~/envs/tutorial-env/bin/activate
(tutorial-env) $ python
Python 3.5.1 (default, May  6 2016, 10:59:36)
  ...
>>> import sys
>>> sys.path
['', '/usr/local/lib/python35.zip', ...,
'~/envs/tutorial-env/lib/python3.5/site-packages']
>>>

12.3. 使用pip管理包

你可以使用一个名为 pip 的程序来安装、升级和移除软件包。默认情况下 pip 将从 Python Package Index <https://pypi.org> 安装软件包。你可以在浏览器中访问 Python Package Index 或是使用 pip 受限的搜索功能:

(tutorial-env) $ pip search astronomy
skyfield               - Elegant astronomy for Python
gary                   - Galactic astronomy and gravitational dynamics.
novas                  - The United States Naval Observatory NOVAS astronomy library
astroobs               - Provides astronomy ephemeris to plan telescope observations
PyAstronomy            - A collection of astronomy related tools for Python.
...

pip 有许多子命令：“search”、“install”、“uninstall”、“freeze”等等。（请参阅 安装 Python 模块 指南以了解 pip 的完整文档。）

您可以通过指定包的名称来安装最新版本的包：

(tutorial-env) $ pip install novas
Collecting novas
  Downloading novas-3.1.1.3.tar.gz (136kB)
Installing collected packages: novas
  Running setup.py install for novas
Successfully installed novas-3.1.1.3

您还可以通过提供包名称后跟 == 和版本号来安装特定版本的包：

(tutorial-env) $ pip install requests==2.6.0
Collecting requests==2.6.0
  Using cached requests-2.6.0-py2.py3-none-any.whl
Installing collected packages: requests
Successfully installed requests-2.6.0

如果你重新运行这个命令，pip 会注意到已经安装了所请求的版本并且什么都不做。您可以提供不同的版本号来获取该版本，或者您可以运行 pip install --upgrade 将软件包升级到最新版本：

(tutorial-env) $ pip install --upgrade requests
Collecting requests
Installing collected packages: requests
  Found existing installation: requests 2.6.0
    Uninstalling requests-2.6.0:
      Successfully uninstalled requests-2.6.0
Successfully installed requests-2.7.0

pip uninstall 后跟一个或多个包名称将从虚拟环境中删除包。

pip show 将显示有关特定包的信息：

(tutorial-env) $ pip show requests
---
Metadata-Version: 2.0
Name: requests
Version: 2.7.0
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me@kennethreitz.com
License: Apache 2.0
Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages
Requires:

pip list 将显示虚拟环境中安装的所有软件包：

(tutorial-env) $ pip list
novas (3.1.1.3)
numpy (1.9.2)
pip (7.0.3)
requests (2.7.0)
setuptools (16.0)

pip freeze` 将生成一个类似的已安装包列表，但输出使用 pip install 期望的格式。一个常见的约定是将此列表放在 requirements.txt 文件中：

(tutorial-env) $ pip freeze > requirements.txt
(tutorial-env) $ cat requirements.txt
novas==3.1.1.3
numpy==1.9.2
requests==2.7.0

然后可以将 requirements.txt 提交给版本控制并作为应用程序的一部分提供。然后用户可以使用 install -r 安装所有必需的包：

(tutorial-env) $ pip install -r requirements.txt
Collecting novas==3.1.1.3 (from -r requirements.txt (line 1))
  ...
Collecting numpy==1.9.2 (from -r requirements.txt (line 2))
  ...
Collecting requests==2.7.0 (from -r requirements.txt (line 3))
  ...
Installing collected packages: novas, numpy, requests
  Running setup.py install for novas
Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0

pip 有更多选择。有关 pip 的完整文档，请参阅 安装 Python 模块 指南。当您编写一个包并希望在 Python 包索引中使它可用时，请参考 分发 Python 模块 指南。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

13. 接下来？

阅读本教程可能会增强您对使用Python的兴趣 - 您应该热衷于应用Python来解决您的实际问题。你应该去哪里了解更多？

本教程是Python文档集的一部分。其他文档：

Python 标准库:

您应该浏览本手册，该手册提供了有关标准库中的类型，功能和模块的完整（尽管简洁）参考资料。标准的Python发行版包含 很多 的附加代码。有些模块可以读取Unix邮箱，通过HTTP检索文档，生成随机数，解析命令行选项，编写CGI程序，压缩数据以及许多其他任务。浏览标准库参考可以了解更多可用的内容。

安装 Python 模块 解释了怎么安装由其他Python开发者编写的模块。

Python语言参考: Python的语法和语义的详细解释。尽管阅读完非常繁重，但作为语言本身的完整指南是有用的。

更多Python资源：

https://www.python.org ：主要的Python网站。它包含代码，文档以及指向Web上与Python相关的页面的链接。该网站世界很多地区都有镜像，如欧洲，日本和澳大利亚；镜像可能比主站点更快，具体取决于您的地理位置。

https://docs.python.org ：快速访问Python的文档。

https://pypi.org: The Python Package Index, previously also nicknamed the Cheese Shop, is an index of user-created Python modules that are available for download. Once you begin releasing code, you can register it here so that others can find it.

https://code.activestate.com/recipes/langs/python/ ：Python Cookbook是一个相当大的代码示例集，更多的模块和有用的脚本。特别值得注意的贡献收集在一本名为Python Cookbook（O’Reilly＆Associates，ISBN 0-596-00797-3）的书中。

http://www.pyvideo.org 从会议和用户组会议中收集与Python相关的视频的链接。

https://scipy.org ：Ecientific Python项目包括用于快速阵列计算和操作的模块，以及用于诸如线性代数，傅里叶变换，非线性求解器，随机数分布，统计分析等的一系列包。

对于与Python相关的问题和问题报告，您可以发布到新闻组 comp.lang.python ，或者将它们发送到邮件列表python-list@python.org。新闻组和邮件列表是互通的，因此发布到一个地方将自动转发给另一个。每天有数百个帖子，询问（和回答）问题，建议新功能，以及宣布新模块。邮件列表档案可在 https://mail.python.org/pipermail/ 上找到。

在发问之前，请务必查看以下列表 常见问题 （或简写为 FAQ）。常见问题包含了很多一次又一次问到的问题及答案，并且可能已经包含了您的问题的解决方案。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

14. 交互式编辑和编辑历史

某些版本的 Python 解释器支持编辑当前输入行和编辑历史记录，类似 Korn shell 和 GNU Bash shell 的功能 。这个功能使用了 GNU Readline [https://tiswww.case.edu/php/chet/readline/rltop.html] 来实现，一个支持多种编辑方式的库。这个库有它自己的文档，在这里我们就不重复说明了。

14.1. Tab 补全和编辑历史

在解释器启动的时候，补全变量和模块名的功能将 自动打开，以便在按下 Tab 键的时候调用补全函数。它会查看 Python 语句名称，当前局部变量和可用的模块名称。处理像 string.a 的表达式，它会求值在最后一个 '.' 之前的表达式，接着根据求值结果对象的属性给出补全建议。如果拥有 __getattr__() 方法的对象是表达式的一部分，注意这可能会执行程序定义的代码。默认配置下会把编辑历史记录保存在用户目录下名为 .python_history 的文件。在下一次 Python 解释器会话期间，编辑历史记录仍旧可用。

14.2. 默认交互式解释器的替代品

Python 解释器与早期版本的相比，向前迈进了一大步；无论怎样，还有些希望的功能：如果能在编辑连续行时建议缩进（解析器知道接下来是否需要缩进符号），那将很棒。补全机制可以使用解释器的符号表。有命令去检查（甚至建议）括号，引号以及其他符号是否匹配。

一个可选的增强型交互式解释器是 IPython [https://ipython.org/]，它已经存在了有一段时间，它具有 tab 补全，探索对象和高级历史记录管理功能。它还可以彻底定制并嵌入到其他应用程序中。另一个相似的增强型交互式环境是 bpython [http://www.bpython-interpreter.org/]。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

15. 浮点算术：争议和限制

浮点数在计算机硬件中表示为以 2 为基数（二进制）的小数。举例而言，十进制的小数

0.125

等于 1/10 + 2/100 + 5/1000 ，同理，二进制的小数

0.001

等于0/2 + 0/4 + 1/8。这两个小数具有相同的值，唯一真正的区别是第一个是以 10 为基数的小数表示法，第二个则是 2 为基数。

不幸的是，大多数的十进制小数都不能精确地表示为二进制小数。这导致在大多数情况下，你输入的十进制浮点数都只能近似地以二进制浮点数形式储存在计算机中。

用十进制来理解这个问题显得更加容易一些。考虑分数 1/3 。我们可以得到它在十进制下的一个近似值

0.3

或者，更近似的，:

0.33

或者，更近似的，:

0.333

以此类推。结果是无论你写下多少的数字，它都永远不会等于 1/3 ，只是更加更加地接近 1/3 。

同样的道理，无论你使用多少位以 2 为基数的数码，十进制的 0.1 都无法精确地表示为一个以 2 为基数的小数。 在以 2 为基数的情况下， 1/10 是一个无限循环小数

0.0001100110011001100110011001100110011001100110011...

在任何一个位置停下，你都只能得到一个近似值。因此，在今天的大部分架构上，浮点数都只能近似地使用二进制小数表示，对应分数的分子使用每 8 字节的前 53 位表示，分母则表示为 2 的幂次。在 1/10 这个例子中，相应的二进制分数是 3602879701896397 / 2 ** 55 ，它很接近 1/10 ，但并不是 1/10 。

大部分用户都不会意识到这个差异的存在，因为 Python 只会打印计算机中存储的二进制值的十进制近似值。在大部分计算机中，如果 Python 想把 0.1 的二进制对应的精确十进制打印出来，将会变成这样

>>> 0.1
0.1000000000000000055511151231257827021181583404541015625

这比大多数人认为有用的数字更多，因此Python通过显示舍入值来保持可管理的位数

>>> 1 / 10
0.1

牢记，即使输出的结果看起来好像就是 1/10 的精确值，实际储存的值只是最接近 1/10 的计算机可表示的二进制分数。

有趣的是，有许多不同的十进制数共享相同的最接近的近似二进制小数。例如， 0.1 、 0.10000000000000001 、 0.1000000000000000055511151231257827021181583404541015625 全都近似于 3602879701896397 / 2 ** 55 。由于所有这些十进制值都具有相同的近似值，因此可以显示其中任何一个，同时仍然保留不变的 eval(repr(x)) == x 。

在历史上，Python 提示符和内置的 repr() 函数会选择具有 17 位有效数字的来显示，即 0.10000000000000001。 从 Python 3.1 开始，Python（在大多数系统上）现在能够选择这些表示中最短的并简单地显示 0.1 。

请注意这种情况是二进制浮点数的本质特性：它不是 Python 的错误，也不是你代码中的错误。 你会在所有支持你的硬件中的浮点运算的语言中发现同样的情况（虽然某些语言在默认状态或所有输出模块下都不会 显示 这种差异）。

想要更美观的输出，你可能会希望使用字符串格式化来产生限定长度的有效位数:

>>> format(math.pi, '.12g')  # give 12 significant digits
'3.14159265359'

>>> format(math.pi, '.2f')   # give 2 digits after the point
'3.14'

>>> repr(math.pi)
'3.141592653589793'

必须重点了解的是，这在实际上只是一个假象：你只是将真正的机器码值进行了舍入操作再 显示 而已。

一个假象还可能导致另一个假象。 例如，由于这个 0.1 并非真正的 1/10，将三个 0.1 的值相加也不一定能恰好得到 0.3:

>>> .1 + .1 + .1 == .3
False

而且，由于这个 0.1 无法精确表示 1/10 的值而这个 0.3 也无法精确表示 3/10 的值，使用 round() 函数进行预先舍入也是没用的:

>>> round(.1, 1) + round(.1, 1) + round(.1, 1) == round(.3, 1)
False

虽然这些小数无法精确表示其所要代表的实际值，round() 函数还是可以用来“事后舍入”，使得实际的结果值可以做相互比较:

>>> round(.1 + .1 + .1, 10) == round(.3, 10)
True

二进制浮点运算会造成许多这样的“意外”。 有关 “0.1” 的问题会在下面的“表示性错误”一节中更详细地描述。 请参阅 浮点数的危险性 [http://www.lahey.com/float.htm] 一文了解有关其他常见意外现象的更详细介绍。

正如那篇文章的结尾所言，“对此问题并无简单的答案。” 但是也不必过于担心浮点数的问题！ Python 浮点运算中的错误是从浮点运算硬件继承而来，而在大多数机器上每次浮点运算得到的 2**53 数码位都会被作为 1 个整体来处理。 这对大多数任务来说都已足够，但你确实需要记住它并非十进制算术，且每次浮点运算都可能会导致新的舍入错误。

虽然病态的情况确实存在，但对于大多数正常的浮点运算使用来说，你只需简单地将最终显示的结果舍入为你期望的十进制数值即可得到你期望的结果。 str() 通常已足够，对于更精度的控制可参看 格式字符串语法 中 str.format() 方法的格式描述符。

对于需要精确十进制表示的使用场景，请尝试使用 decimal 模块，该模块实现了适合会计应用和高精度应用的十进制运算。

另一种形式的精确运算由 fractions 模块提供支持，该模块实现了基于有理数的算术运算（因此可以精确表示像 1/3 这样的数值）。

如果你是浮点运算的重度用户，你应该看一下数值运算 Python 包 NumPy 以及由 SciPy 项目所提供的许多其它数学和统计运算包。 参见 <https://scipy.org>。

Python 也提供了一些工具，可以在你真的 想要 知道一个浮点数精确值的少数情况下提供帮助。 例如 float.as_integer_ratio() 方法会将浮点数表示为一个分数:

>>> x = 3.14159
>>> x.as_integer_ratio()
(3537115888337719, 1125899906842624)

由于这是一个精确的比值，它可以被用来无损地重建原始值:

>>> x == 3537115888337719 / 1125899906842624
True

float.hex() 方法会以十六进制（以 16 为基数）来表示浮点数，同样能给出保存在你的计算机中的精确值:

>>> x.hex()
'0x1.921f9f01b866ep+1'

这种精确的十六进制表示法可被用来精确地重建浮点值:

>>> x == float.fromhex('0x1.921f9f01b866ep+1')
True

由于这种表示法是精确的，它适用于跨越不同版本（平台无关）的 Python 移植数值，以及与支持相同格式的其他语言（例如 Java 和 C99）交换数据.

另一个有用的工具是 math.fsum() 函数，它有助于减少求和过程中的精度损失。 它会在数值被添加到总计值的时候跟踪“丢失的位”。 这可以很好地保持总计值的精确度， 使得错误不会积累到能影响结果总数的程度:

>>> sum([0.1] * 10) == 1.0
False
>>> math.fsum([0.1] * 10) == 1.0
True

15.1. 表示性错误

本小节将详细解释 “0.1” 的例子，并说明你可以怎样亲自对此类情况进行精确分析。 假定前提是已基本熟悉二进制浮点表示法。

表示性错误 是指某些（其实是大多数）十进制小数无法以二进制（以 2 为基数的计数制）精确表示这一事实造成的错误。 这就是为什么 Python（或者 Perl、C、C++、Java、Fortran 以及许多其他语言）经常不会显示你所期待的精确十进制数值的主要原因。

为什么会这样？ 1/10 是无法用二进制小数精确表示的。 目前（2000年11月）几乎所有使用 IEEE-754 浮点运算标准的机器以及几乎所有系统平台都会将 Python 浮点数映射为 IEEE-754 “双精度类型”。 754 双精度类型包含 53 位精度，因此在输入时，计算会尽量将 0.1 转换为以 J/2**N 形式所能表示的最接近分数，其中 J 为恰好包含 53 个二进制位的整数。 重新将

1 / 10 ~= J / (2**N)

写为

J ~= 2**N / 10

并且由于 J 恰好有 53 位 (即 >= 2**52 但 < 2**53)，N 的最佳值为 56:

>>> 2**52 <=  2**56 // 10  < 2**53
True

也就是说，56 是唯一的 N 值能令 J 恰好有 53 位。 这样 J 的最佳可能值就是经过舍入的商:

>>> q, r = divmod(2**56, 10)
>>> r
6

由于余数超过 10 的一半，最佳近似值可通过四舍五入获得:

>>> q+1
7205759403792794

这样在 754 双精度下 1/10 的最佳近似值为:

7205759403792794 / 2 ** 56

分子和分母都除以二则结果小数为:

3602879701896397 / 2 ** 55

请注意由于我们做了向上舍入，这个结果实际上略大于 1/10；如果我们没有向上舍入，则商将会略小于 1/10。 但无论如何它都不会是 精确的 1/10！

因此计算永远不会“看到”1/10：它实际看到的就是上面所给出的小数，它所能达到的最佳 754 双精度近似值:

>>> 0.1 * 2 ** 55
3602879701896397.0

如果我们将该小数乘以 10**55，我们可以看到该值输出为 55 位的十进制数:

>>> 3602879701896397 * 10 ** 55 // 2 ** 55
1000000000000000055511151231257827021181583404541015625

这意味着存储在计算机中的确切数值等于十进制数值 0.1000000000000000055511151231257827021181583404541015625。 许多语言（包括较旧版本的 Python）都不会显示这个完整的十进制数值，而是将结果舍入为 17 位有效数字:

>>> format(0.1, '.17f')
'0.10000000000000001'

fractions 和 decimal 模块可令进行此类计算更加容易:

>>> from decimal import Decimal
>>> from fractions import Fraction

>>> Fraction.from_float(0.1)
Fraction(3602879701896397, 36028797018963968)

>>> (0.1).as_integer_ratio()
(3602879701896397, 36028797018963968)

>>> Decimal.from_float(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')

>>> format(Decimal.from_float(0.1), '.17')
'0.10000000000000001'

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

16. 附录

16.1. 交互模式

16.1.1. 错误处理

当发生错误时，解释器会打印错误信息和错误堆栈。在交互模式下，将返回到主命令提示符；如果输入内容来自文件，在打印错误堆栈之后，程序会以非零状态退出。（这里所说的错误不包括 try 语句中由 except 所捕获的异常。）有些错误是无条件致命的，会导致程序以非零状态退出；比如内部逻辑矛盾或内存耗尽。所有错误信息都会被写入标准错误流；而命令的正常输出则被写入标准输出流。

将中断字符（通常为 Control-C 或 Delete ）键入主要或辅助提示会取消输入并返回主提示符。 1 在执行命令时键入中断引发的 KeyboardInterrupt 异常，可以由 try 语句处理。

16.1.2. 可执行的Python脚本

在BSD等类Unix系统上，Python脚本可以直接执行，就像shell脚本一样，第一行添加:

#!/usr/bin/env python3.5

（假设解释器位于用户的 PATH ）脚本的开头，并将文件设置为可执行。 #! 必须是文件的前两个字符。在某些平台上，第一行必须以Unix样式的行结尾（'\n'）结束，而不是以Windows（'\r\n'）行结尾。请注意，散列或磅字符 '#' 在Python中代表注释开始。

可以使用 chmod 命令为脚本提供可执行模式或权限。

$ chmod +x myscript.py

在Windows系统上，没有“可执行模式”的概念。 Python安装程序自动将 .py 文件与 python.exe 相关联，这样双击Python文件就会将其作为脚本运行。扩展也可以是 .pyw ，在这种情况下，会隐藏通常出现的控制台窗口。

16.1.3. 交互式启动文件

当您以交互方式使用Python时，每次启动解释器时都会执行一些标准命令，这通常很方便。您可以通过将名为 PYTHONSTARTUP 的环境变量设置为包含启动命令的文件名来实现。这类似于Unix shell的 .profile 功能。

This file is only read in interactive sessions, not when Python reads commands from a script, and not when /dev/tty is given as the explicit source of commands (which otherwise behaves like an interactive session). It is executed in the same namespace where interactive commands are executed, so that objects that it defines or imports can be used without qualification in the interactive session. You can also change the prompts sys.ps1 and sys.ps2 in this file.

如果你想从当前目录中读取一个额外的启动文件，你可以使用像 if os.path.isfile('.pythonrc.py'): exec(open('.pythonrc.py').read()) 这样的代码在全局启动文件中对它进行编程。如果要在脚本中使用启动文件，则必须在脚本中显式执行此操作:

import os
filename = os.environ.get('PYTHONSTARTUP')
if filename and os.path.isfile(filename):
    with open(filename) as fobj:
        startup_file = fobj.read()
    exec(startup_file)

16.1.4. 定制模块

Python提供了两个钩子来让你自定义它：sitecustomize 和 usercustomize。要查看其工作原理，首先需要找到用户site-packages目录的位置。启动Python并运行此代码:

>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.5/site-packages'

现在，您可以在该目录中创建一个名为 usercustomize.py 的文件，并将所需内容放入其中。它会影响Python的每次启动，除非它以 -s 选项启动，以禁用自动导入。

sitecustomize 以相同的方式工作，但通常由计算机管理员在全局 site-packages 目录中创建，并在 usercustomize 之前被导入。有关详情请参阅 site 模块的文档。

备注

1

GNU Readline 包的问题可能会阻止这种情况。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 教程 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

Python安装和使用

这一部分文档专门介绍关于在不同平台上设置Python环境、调用解释器以及让使用Python更容易的一些事情的有用信息。

1. 命令行与环境

1.1. 命令行

1.1.1. 接口选项

1.1.2. 通用选项

1.1.3. 其他选项

1.1.4. 不应当使用的选项

1.2. 环境变量

1.2.1. 调试模式变量

2. 在类Unix环境下使用Python

2.1. 获得并安装Python的最新版本

2.1.1. 在Linux中

2.1.2. 在FreeBSD和OpenBSD上

2.1.3. 在OpenSolaris系统上

2.2. 构建Python

2.3. 与Python相关的路径和文件

2.4. 杂项

2.5. Editors and IDEs

3. 在Windows上使用 Python

3.1. Installing Python

3.1.1. Supported Versions

3.1.2. Installation Steps

3.1.3. 删除 MAX_PATH 限制

3.1.4. 无UI 安装

3.1.5. 免下载安装

3.1.6. 修改安装

3.1.7. 其他平台

3.2. 替代捆绑包

3.3. 配置Python

3.3.1. 附录：设置环境变量

3.3.2. 查找Python可执行文件

3.4. 适用于Windows的Python启动器

3.4.1. 入门

3.4.1.1. 从命令行

3.4.1.2. 从虚拟环境

3.4.1.3. 从脚本

3.4.1.4. 从文件关联

3.4.2. Shebang Lines

3.4.3. shebang lines 的参数

3.4.4. 自定义

3.4.4.1. 通过INI文件自定义

3.4.4.2. 自定义默认的Python版本

3.4.5. 诊断

3.5. 查找模块

3.6. 附加模块

3.6.1. PyWin32

3.6.2. cx_Freeze

3.6.3. WConio

3.7. 在Windows上编译Python

3.8. Embedded Distribution

3.8.1. Python应用程序

3.8.2. 嵌入Python

3.9. Other resources

4. 在苹果系统上使用 Python

4.1. 获取和安装 MacPython

4.1.1. 如何运行 Python 脚本

4.1.2. 运行有图形界面的脚本

4.1.3. 配置

4.2. IDE

4.3. 安装额外的 Python 包

4.4. Mac 上的图形界面编程

4.5. 在 Mac 上分发 Python 应用程序

4.6. 其他资源

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python安装和使用 »

|

1. 命令行与环境

CPython 解析器会扫描命令行与环境用于获取各种设置信息。

CPython implementation detail: 其他实现的命令行方案可能有所不同。 更多相关资源请参阅 其他实现。

1.1. 命令行

对 Python 发起调用时，你可以指定以下的任意选项:

python [-bBdEhiIOqsSuvVWx?] [-c command | -m module-name | script | - ] [args]

当然最常见的用例就是简单地启动执行一个脚本:

python myscript.py

1.1.1. 接口选项

解释器接口类似于 UNIX shell，但提供了一些额外的发起调用方法:

当调用时附带连接到某个 tty 设备的标准输入时，它会提示输入命令并执行它们，直到读入一个 EOF（文件结束字符，其产生方式是在 UNIX 中按 Ctrl-D 或在 Windows 中按 Ctrl-Z, Enter。）

当调用时附带一个文件名参数或以一个文件作为标准输入时，它会从该文件读取并执行脚本程序。

当调用时附带一个目录名参数时，它会从该目录读取并执行具有适当名称的脚本程序。

当调用时附带 -c command 时，它会执行 command 所给出的 Python 语句。 在这里 command 可以包含以换行符分隔的多条语句。 请注意前导空格在 Python 语句中是有重要作用的！

当调用时附带 -m module-name 时，会在 Python 模块路径中查找指定的模块，并将其作为脚本程序执行。

在非交互模式下，会对全部输入先解析再执行。

一个接口选项会终结解释器所读入的选项列表，后续的所有参数将被放入 sys.argv – 请注意其中首个元素即第零项 (sys.argv[0]) 会是一个表示程序源的字符串。

-c <command>

执行 command 中的 Python 代码。 command 可以为一条或以换行符分隔的多条语句，其中前导空格像在普通模块代码中一样具有作用。

如果给出此选项，sys.argv 的首个元素将为 "-c" 并且当前目录将被加入 sys.path 的开头（以允许该目录中的模块作为最高层级模块被导入）。

-m <module-name>

在 sys.path 中搜索指定名称的模块并将其内容作为 __main__ 模块来执行。

由于该参数为 module 名称，你不应给出文件扩展名 (.py)。 模块名称应为绝对有效的 Python 模块名称，但具体实现可能并不总是强制要求这一点（例如它可能允许你使用包含连字符的名称）。

包名称（包括命名空间包）也允许使用。 当所提供的是包名称而非普通模块名称时，解释器将把 <pkg>.__main__ 作为主模块来执行。 此行为特意被设计为与作为脚本参数传递给解释器的目录和 zip 文件的处理方式类似。

注解

此选项不适用于内置模块和以 C 编写的扩展模块，因为它们并没有对应的 Python 模块文件。 但是它仍然适用于预编译的模块，即使没有可用的初始源文件。

如果给出此选项，sys.argv 的首个元素将为模块文件的完整路径 (在定位模块文件期间，首个元素将设为 "-m")。 与 -c 选项一样，当前目录将被加入 sys.path 的开头。

许多标准库模块都包含作为脚本执行时会被发起调用的代码。 其中的一个例子是 timeit 模块:

python -mtimeit -s 'setup here' 'benchmarked code here'
python -mtimeit -h # for details

参见

runpy.run_module()

Python 代码可以直接使用的等效功能

PEP 338 [https://www.python.org/dev/peps/pep-0338] – 将模块作为脚本执行

在 3.1 版更改: 提供包名称来运行 __main__ 子模块。

在 3.4 版更改: 同样支持命名空间包

-

从标准输入 (sys.stdin) 读取命令。 如果标准输入为一个终端，则使用 -i。

如果给出此选项，sys.argv 的首个元素将为 "-" 并且当前目录将被加入 sys.path 的开头。

<script>

执行 script 中的 Python 代码，该参数应为一个（绝对或相对）文件系统路径，指向某个 Python 文件、包含 __main__.py 文件的目录，或包含 __main__.py 文件的 zip 文件。

如果给出此选项，sys.argv 的首个元素将为在命令行中指定的脚本名称。

如果脚本名称直接指向一个 Python 文件，则包含该文件的目录将被加入 sys.path 的开头，并且该文件会被作为 __main__ 模块来执行。

如果脚本名称指向一个目录或 zip 文件，则脚本名称将被加入 sys.path 的开头，并且该位置中的 __main__.py 文件会被作为 __main__ 模块来执行。

参见

runpy.run_path()

Python 代码可以直接使用的等效功能

如果没有给出接口选项，则使用 -i，sys.argv[0] 将为空字符串 ("")，并且当前目录会被加入 sys.path 的开头。 此外，tab 补全和历史编辑会自动启用，如果你的系统平台支持此功能的话 (参见 Readline configuration)。

参见

调用解释器

在 3.4 版更改: 自动启用 tab 补全和历史编辑。

1.1.2. 通用选项

-?

-h

--help

打印全部命令行选项的简短描述。

-V

--version

打印 Python 版本号并退出。 示例输出信息如下:

Python 3.6.0b2+

如果重复给出，则打印有关构建的更多信息，例如:

Python 3.6.0b2+ (3.6:84a3c5003510+, Oct 26 2016, 02:33:55)
[GCC 6.2.0 20161005]

3.6 新版功能: -VV 选项。

1.1.3. 其他选项

-b

在将 bytes 或 bytearray 与 str 或是将 bytes 与 int 比较时发出警告。 如果重复给出该选项 (-bb) 则会引发错误。

在 3.5 版更改: 影响 bytes 与 int 的比较。

-B

如果给出此选项，Python 将不会试图在导入源模块时写入 .pyc 文件。 另请参阅 PYTHONDONTWRITEBYTECODE。

-d

Turn on parser debugging output (for wizards only, depending on compilation options). See also PYTHONDEBUG.

-E

忽略所有 PYTHON* 环境变量，例如可能已设置的 PYTHONPATH 和 PYTHONHOME。

-i

当有脚本被作为首个参数传入或使用了 -c 选项时，在执行脚本或命令之后进入交互模式，即使是在 sys.stdin 并不是一个终端的时候。 PYTHONSTARTUP 文件不会被读取。

这一选项的用处是在脚本引发异常时检查全局变量或者栈跟踪。 另请参阅 PYTHONINSPECT。

-I

在隔离模式下运行 Python。 这将同时应用 -E 和 -s。 在隔离模式下 sys.path 既不包含脚本所在目录也不包含用户的 site-packages 目录。 所有 PYTHON* 环境变量也会被忽略。 还可以施加更进一步的限制以防止用户注入恶意代码。

3.4 新版功能.

-O

移除 assert 语句以及任何以 __debug__ 的值作为条件的代码。 通过在 .pyc 扩展名之前添加 .opt-1 来扩充已编译文件 (bytecode) 的文件名 (参见 PEP 488 [https://www.python.org/dev/peps/pep-0488])。 另请参阅 PYTHONOPTIMIZE。

在 3.5 版更改: 依据 PEP 488 [https://www.python.org/dev/peps/pep-0488] 修改 .pyc 文件名。

-OO

在启用 -O 的同时丢弃文档字符串。 通过在 .pyc 扩展名之前添加 .opt-2 来扩展已编译文件 (bytecode) 的文件名 (参见 PEP 488 [https://www.python.org/dev/peps/pep-0488])。

在 3.5 版更改: 依据 PEP 488 [https://www.python.org/dev/peps/pep-0488] 修改 .pyc 文件名。

-q

即使在交互模式下也不显示版权和版本信息。

3.2 新版功能.

-R

Kept for compatibility. On Python 3.3 and greater, hash randomization is turned on by default.

在先前的 Python 版本中，此选项会开启哈希随机化，这样 str, bytes 和 datetime 的 __hash__() 值将会使用一个不可预测的随机值来“加盐”。 虽然它们在单个 Python 进程中会保持不变，但在重复发起的 Python 调用之间则是不可预测的。

哈希随机化旨在针对由精心选择的输入引起的拒绝服务攻击提供防护，这种输入利用了构造 dict 在最坏情况下的性能即 O(n^2) 复杂度。 详情请参阅 http://www.ocert.org/advisories/ocert-2011-003.html。

PYTHONHASHSEED 允许你为哈希种子密码设置一个固定值。

3.2.3 新版功能.

-s

不要将 用户 site-packages 目录 添加到 sys.path。

参见

PEP 370 [https://www.python.org/dev/peps/pep-0370] – 分用户的 site-packages 目录

-S

禁用 site 的导入及其所附带的基于站点对 sys.path 的操作。 如果 site 会在稍后被显式地导入也会禁用这些操作 (如果你希望触发它们则应调用 site.main())。

-u

Force the binary layer of the stdout and stderr streams (which is available as their buffer attribute) to be unbuffered. The text I/O layer will still be line-buffered if writing to the console, or block-buffered if redirected to a non-interactive file.

另请参阅 PYTHONUNBUFFERED。

-v

每当一个模块被初始化时打印一条信息，显示其加载位置（文件名或内置模块）。 当重复给出时 (-vv)，为搜索模块时所检查的每个文件都打印一条消息。 此外还提供退出时有关模块清理的信息A。 另请参阅 PYTHONVERBOSE。

-W arg

警告控制。 Python 的警告机制在默认情况下会向 sys.stderr 打印警告消息。 典型的警告消息具有如下形式：

file:line: category: message

默认情况下，每个警告都对于其发生所在的每个源行都会打印一次。 此选项可控制警告打印的频繁程度。

可以给出多个 -W 选项；当某个警告能与多个选项匹配时，将执行最后一个匹配选项的操作。 无效的 -W 选项将被忽略（但是，在发出第一个警告时会打印有关无效选项的警告消息）。

Warnings can also be controlled from within a Python program using the warnings module.

The simplest form of argument is one of the following action strings (or a unique abbreviation):

ignore

Ignore all warnings.

default

Explicitly request the default behavior (printing each warning once per source line).

all

Print a warning each time it occurs (this may generate many messages if a warning is triggered repeatedly for the same source line, such as inside a loop).

module

Print each warning only the first time it occurs in each module.

once

Print each warning only the first time it occurs in the program.

error

Raise an exception instead of printing a warning message.

The full form of argument is:

action:message:category:module:line

Here, action is as explained above but only applies to messages that match the remaining fields. Empty fields match all values; trailing empty fields may be omitted. The message field matches the start of the warning message printed; this match is case-insensitive. The category field matches the warning category. This must be a class name; the match tests whether the actual warning category of the message is a subclass of the specified warning category. The full class name must be given. The module field matches the (fully-qualified) module name; this match is case-sensitive. The line field matches the line number, where zero matches all line numbers and is thus equivalent to an omitted line number.

参见

warnings – the warnings module

PEP 230 [https://www.python.org/dev/peps/pep-0230] – Warning framework

PYTHONWARNINGS

-x

跳过源中第一行，以允许使用非 Unix 形式的 #!cmd。 这适用于 DOS 专属的破解操作。

-X

保留用于各种具体实现专属的选项。 CPython 目前定义了下列可用的值：

-X faulthandler 启用 faulthandler；

-X showrefcount 当程序结束或在交互解释器中的每条语句之后输出总引用计数和已使用内存块计数。 此选项仅在调试版本中有效。

-X tracemalloc 使用 tracemalloc 模块启动对 Python 内存分配的跟踪。 默认情况下，只有最近的帧会保存在跟踪的回溯信息中。 使用 -X tracemalloc=NFRAME 以启动限定回溯 NFRAME 帧的跟踪。 请参阅 tracemalloc.start() 了解详情。

-X showalloccount 当程序结束时输出每种类型的已分配对象的总数。 此选项仅当 Python 在定义了 COUNT_ALLOCS 后构建时才会生效。

它还允许传入任意值并通过 sys._xoptions 字典来提取这些值。

在 3.2 版更改: 增加了 -X 选项。

3.3 新版功能: -X faulthandler 选项。

3.4 新版功能: -X showrefcount 与 -X tracemalloc 选项。

3.6 新版功能: -X showalloccount 选项。

1.1.4. 不应当使用的选项

-J

保留给 Jython [http://www.jython.org/] 使用。

1.2. 环境变量

这些环境变量会影响 Python 的行为，它们是在命令行开关之前被处理的，但 -E 或 -I 除外。 根据约定，当存在冲突时命令行开关会覆盖环境变量的设置。

PYTHONHOME

更改标准 Python 库的位置。 默认情况下库是在 prefix/lib/pythonversion 和 exec_prefix/lib/pythonversion 中搜索，其中 prefix 和 exec_prefix 是由安装位置确定的目录，默认都位于 /usr/local。

当 PYTHONHOME 被设为单个目录时，它的值会同时替代 prefix 和 exec_prefix。 要为两者指定不同的值，请将 PYTHONHOME 设为 prefix:exec_prefix。

PYTHONPATH

增加模块文件默认搜索路径。 所用格式与终端的 PATH 相同：一个或多个由 os.pathsep 分隔的目录路径名称（例如 Unix 上用冒号而在 Windows 上用分号）。 默认忽略不存在的目录。

除了普通目录之外，单个 PYTHONPATH 条目可以引用包含纯Python模块的zip文件（源代码或编译形式）。无法从zip文件导入扩展模块。

默认索引路径依赖于安装路径，但通常都是以 prefix/lib/pythonversion 开始 (参见上文中的 PYTHONHOME)。 它 总是 会被添加到 PYTHONPATH。

有一个附加目录将被插入到索引路径的 PYTHONPATH 之前，正如上文中 接口选项 所描述的。 搜索路径可以在 Python 程序内作为变量 sys.path 来进行操作。

PYTHONSTARTUP

这如果是一个可读文件的名称，该文件中的 Python 命令会在交互模式的首个提示符显示之前被执行。 该文件会在与交互式命令执行所在的同一命名空间中被执行，因此其中所定义或导入的对象可以在交互式会话中无限制地使用。 你还可以在这个文件中修改提示符 sys.ps1 和 sys.ps2 以及钩子 sys.__interactivehook__。

PYTHONOPTIMIZE

这如果被设为一个非空字符串，它就相当于指定 -O 选项。 如果设为一个整数，则它就相当于多次指定 -O。

PYTHONDEBUG

此变量如果被设为一个非空字符串，它就相当于指定 -d 选项。 如果设为一个整数，则它就相当于多次指定 -d。

PYTHONINSPECT

此变量如果被设为一个非空字符串，它就相当于指定 -i 选项。

此变量也可由 Python 代码使用 os.environ 来修改以在程序终结时强制检查模式。

PYTHONUNBUFFERED

此变量如果被设为一个非空字符串，它就相当于指定 -u 选项。

PYTHONVERBOSE

此变量如果被设为一个非空字符串，它就相当于指定 -v 选项。 如果设为一个整数，则它就相当于多次指定 -v。

PYTHONCASEOK

如果设置此变量，Python 将忽略 import 语句中的大小写。 这仅在 Windows 和 OS X 上有效。

PYTHONDONTWRITEBYTECODE

此变量如果被设为一个非空字符串，Python 将不会尝试在导入源模块时写入 .pyc 文件。 这相当于指定 -B 选项。

PYTHONHASHSEED

如果此变量未设置或设为 random，将使用一个随机值作为 str, bytes 和 datetime 对象哈希运算的种子。

如果 PYTHONHASHSEED 被设为一个整数值，它将被作为固定的种子数用来生成哈希随机化所涵盖的类型的 hash() 结果。

它的目的是允许可复现的哈希运算，例如用于解释器本身的自我检测，或允许一组 python 进程共享哈希值。

该整数必须为一个 [0,4294967295] 范围内的十进制数。 指定数值 0 将禁用哈希随机化。

3.2.3 新版功能.

PYTHONIOENCODING

如果此变量在运行解释器之前被设置，它会覆盖通过 encodingname:errorhandler 语法设置的 stdin/stdout/stderr 所用编码。 encodingname 和 :errorhandler 部分都是可选项，与在 str.encode() 中的含义相同。

对于 stderr，:errorhandler 部分会被忽略；处理程序将总是为 'backslashreplace'。

在 3.4 版更改: “encodingname” 部分现在是可选的。

在 3.6 版更改: 在 Windows 上，对于交互式控制台缓冲区会忽略此变量所指定的编码，除非还指定了 PYTHONLEGACYWINDOWSSTDIO。 通过标准流重定向的文件和管道则不受其影响。

PYTHONNOUSERSITE

如果设置了此变量，Python 将不会把 用户 site-packages 目录 添加到 sys.path。

参见

PEP 370 [https://www.python.org/dev/peps/pep-0370] – 分用户的 site-packages 目录

PYTHONUSERBASE

定义 用户基准目录，它会在执行 python setup.py install --user 时被用来计算 用户 site-packages 目录 的路径以及 Distutils 安装路径。

参见

PEP 370 [https://www.python.org/dev/peps/pep-0370] – 分用户的 site-packages 目录

PYTHONEXECUTABLE

如果设置了此环境变量，则 sys.argv[0] 将被设为此变量的值而不是通过 C 运行时所获得的值。 仅在 Mac OS X 上起作用。

PYTHONWARNINGS

This is equivalent to the -W option. If set to a comma separated string, it is equivalent to specifying -W multiple times.

PYTHONFAULTHANDLER

如果此环境变量被设为一个非空字符串，faulthandler.enable() 会在启动时被调用：为 SIGSEGV, SIGFPE, SIGABRT, SIGBUS 和 SIGILL 等信号安装一个处理句柄以转储 Python 回溯信息。 此变量等价于 -X faulthandler 选项。

3.3 新版功能.

PYTHONTRACEMALLOC

如果此环境变量被设为一个非空字符串，则会使用 tracemalloc 模块启动对 Python 内存分配的跟踪。 该变量的值是保存于跟踪的回溯信息中的最大帧数。 例如，PYTHONTRACEMALLOC=1 只保存最近的帧。 请参阅 tracemalloc.start() 了解详情。

3.4 新版功能.

PYTHONASYNCIODEBUG

如果此变量被设为一个非空字符串，则会启用 asyncio 模块的 调试模式。

3.4 新版功能.

PYTHONMALLOC

设置 Python 内存分配器和/或安装调试钩子。

设置 Python 所使用的内存分配器族群：

malloc: 对所有域 (PYMEM_DOMAIN_RAW, PYMEM_DOMAIN_MEM, PYMEM_DOMAIN_OBJ) 使用 C 库的 malloc() 函数。

pymalloc: 对 PYMEM_DOMAIN_MEM 和 PYMEM_DOMAIN_OBJ 域使用 pymalloc 分配器 而对 PYMEM_DOMAIN_RAW 域使用 malloc() 函数。

安装调试钩子：

debug: install debug hooks on top of the default memory allocator

malloc_debug: same as malloc but also install debug hooks

pymalloc_debug: same as pymalloc but also install debug hooks

When Python is compiled in release mode, the default is pymalloc. When compiled in debug mode, the default is pymalloc_debug and the debug hooks are used automatically.

If Python is configured without pymalloc support, pymalloc and pymalloc_debug are not available, the default is malloc in release mode and malloc_debug in debug mode.

See the PyMem_SetupDebugHooks() function for debug hooks on Python memory allocators.

3.6 新版功能.

PYTHONMALLOCSTATS

如果设为一个非空字符串，Python 将在每次创建新的 pymalloc 对象区域以及在关闭时打印 pymalloc 内存分配器 的统计信息。

如果 PYTHONMALLOC 环境变量被用来强制开启 C 库的 malloc() 分配器，或者如果 Python 的配置不支持 pymalloc，则此变量将被忽略。

在 3.6 版更改: 此变量现在也可以被用于在发布模式下编译的 Python。 如果它被设置为一个空字符串则将没有任何效果。

PYTHONLEGACYWINDOWSFSENCODING

如果设为一个非空字符串，则默认的文件系统编码和错误模式将分别恢复为它们在 3.6 版之前的值 ‘mbcs’ 和 ‘replace’。 否则会使用新的默认值 ‘utf-8’ 和 ‘surrogatepass’。

这也可以在运行时通过 sys._enablelegacywindowsfsencoding() 来启用。

Availability: Windows

3.6 新版功能: 有关更多详细信息，请参阅 PEP 529 [https://www.python.org/dev/peps/pep-0529]。

PYTHONLEGACYWINDOWSSTDIO

如果设为一个非空字符串，则不使用新的控制台读取器和写入器。 这意味着 Unicode 字符将根据活动控制台的代码页进行编码，而不是使用 utf-8。

如果标准流被重定向（到文件或管道）而不是指向控制台缓冲区则该变量会被忽略。

Availability: Windows

3.6 新版功能.

1.2.1. 调试模式变量

设置这些变量只会在Python的调试版本中产生影响，也就是说，如果Python配置了 --with-pydebug 构建选项。

PYTHONTHREADDEBUG

如果设置，Python将打印线程调试信息。

PYTHONDUMPREFS

如果设置，Python在关闭解释器，及转储对象和引用计数后仍将保持活动。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python安装和使用 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python安装和使用 »

|

2. 在类Unix环境下使用Python

2.1. 获得并安装Python的最新版本

2.1.1. 在Linux中

Python预装在大多数Linux发行版上，并作为一个包提供给所有其他用户。 但是，您可能想要使用的某些功能在发行版提供的软件包中不可用。这时您可以从源代码轻松编译最新版本的Python。

如果Python没有预先安装并且不在发行版提供的库中，您可以轻松地为自己使用的发行版创建包。 参阅以下链接：

参见

https://www.debian.org/doc/manuals/maint-guide/first.en.html

对于Debian用户

https://en.opensuse.org/Portal:Packaging

对于OpenSuse用户

https://docs.fedoraproject.org/en-US/Fedora_Draft_Documentation/0.1/html/RPM_Guide/ch-creating-rpms.html

对于Fedora用户

http://www.slackbook.org/html/package-management-making-packages.html

对于Slackware用户

2.1.2. 在FreeBSD和OpenBSD上

FreeBSD用户，使用以下命令添加包:

pkg install python3

OpenBSD用户，使用以下命令添加包:

pkg_add -r python

pkg_add ftp://ftp.openbsd.org/pub/OpenBSD/4.2/packages/<insert your architecture here>/python-<version>.tgz

例如：i386用户获取Python 2.5.1的可用版本:

pkg_add ftp://ftp.openbsd.org/pub/OpenBSD/4.2/packages/i386/python-2.5.1p2.tgz

2.1.3. 在OpenSolaris系统上

你可以从 OpenCSW [https://www.opencsw.org/] 获取、安装及使用各种版本的Python。比如 pkgutil -i python27 。

2.2. 构建Python

如果你想自己编译CPython，首先要做的是获取 source [https://www.python.org/downloads/source/] 。您可以下载最新版本的源代码，也可以直接提取最新的 clone [https://devguide.python.org/setup/#getting-the-source-code] 。 （如果你想要制作补丁，则需要克隆代码。）

构建过程包括通常:

./configure
make
make install

注：特定Unix平台的配置选项和注意事项通常记录在Python源代码树根目录的 README.rst [https://github.com/python/cpython/tree/3.6/README.rst] 文件中。

警告

make install 可以覆盖或伪装 python3 二进制文件。因此，建议使用 make altinstall 而不是 make install ，因为后者只安装了 exec_prefix/bin/pythonversion 。

2.3. 与Python相关的路径和文件

这取决于本地安装惯例； prefix （ ${prefix} ）和 exec_prefix （ ${exec_prefix} ） 取决于安装，应解释为GNU软件；它们可能相同。

例如，在大多数Linux系统上，两者的默认值是 /usr 。

文件/目录

含义

exec_prefix/bin/python3

解释器的推荐位置

prefix/lib/pythonversion, exec_prefix/lib/pythonversion

包含标准模块的目录的推荐位置

prefix/include/pythonversion, exec_prefix/include/pythonversion

包含开发Python扩展和嵌入解释器所需的include文件的目录的推荐位置

2.4. 杂项

要在Unix上使用Python脚本，需要添加可执行权限，例如：

$ chmod +x script

并在脚本的顶部放置一个合适的Shebang线。一个很好的选择通常是:

#!/usr/bin/env python3

将在整个 PATH 中搜索Python解释器。但是，某些Unix系统可能没有 env 命令，因此可能需要将 /usr/bin/python3 硬编码为解释器路径。

要在Python脚本中使用shell命令，请查看 subprocess 模块。

2.5. Editors and IDEs

There are a number of IDEs that support Python programming language. Many editors and IDEs provide syntax highlighting, debugging tools, and PEP 8 [https://www.python.org/dev/peps/pep-0008] checks.

Please go to Python Editors [https://wiki.python.org/moin/PythonEditors] and Integrated Development Environments [https://wiki.python.org/moin/IntegratedDevelopmentEnvironments] for a comprehensive list.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python安装和使用 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python安装和使用 »

|

3. 在Windows上使用 Python

本文档旨在概述在 Microsoft Windows 上使用 Python 时应了解的特定于 Windows 的行为。

3.1. Installing Python

与大多数UNIX系统和服务不同，Windows系统没有预安装Python。为了使Python可用，很多年来CPython 团队已经编译了每一个 release [https://www.python.org/download/releases/] 的Windows安装程序（MSI 包）。这些安装程序主要用于每个用户单独安装Python时，添加核心解释器和库。安装程序还可以为一台机器的所有用户安装，并且可以为应用程序本地分发提供单独的zip文件。

3.1.1. Supported Versions

如 PEP 11 [https://www.python.org/dev/peps/pep-0011] 中所述，Python 仅支持微软产品支持生命周期内的Windows 版本。这意味着 Python 3.6 支持Windows Vista和更新版本。如果需要Windows XP支持，请安装Python 3.4。

3.1.2. Installation Steps

四个 Python 3.6 安装程序可供下载 - 32位和64位版本的各有两个。 web installer （网络安装包）是一个小的初始化工具，它将在安装过程中，根据需要自动下载所需的组件。 offline installer （离线安装包）内含默认安装所需的组件，可选择功能仍需要Internet连接下载。请参阅 免下载安装 以了解在安装过程中避免下载的其他方法。

启动安装程序后，可以选择以下两个选项之一：

如果选择“Install Now（立即安装）”：

您 不 需要成为管理员（除非需要对C运行库进行系统更新，或者为所有用户安装 适用于Windows的Python启动器 ）

Python将安装到您的用户目录中

适用于Windows的Python启动器 将根据第一页底部的选项安装

将安装标准库，测试套件，启动器和pip

如果选择将安装目录将添加到 PATH

快捷方式仅对当前用户可见

选择“自定义安装”将允许您选择：要安装的功能、安装位置、其他选项或安装后的操作。如果要安装调试符号或二进制文件，您需要使用此选项。

如要为全部用户安装，应选择“自定义安装”。在这种情况下:

您可能需要提供管理凭据或批准

Python 将安装到Program Files目录中

适用于Windows的Python启动器 将安装到Windows目录中

安装期间可以选择可选功能

标准库可以预编译为字节码

如果选中，安装目录将添加到系统 PATH

快捷方式所有用户可用

3.1.3. 删除 MAX_PATH 限制

历史上Windows的路径长度限制为260个字符。这意味着长于此的路径将无法解决并导致错误。

在最新版本的Windows中，此限制可以扩展到大约32,000个字符。但需要管理员权限激活 “启用Win32长路径” 组策略，或将注册表值 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem@LongPathsEnabled 设置为 1。

This allows the open() function, the os module and most other path functionality to accept and return paths longer than 260 characters when using strings. (Use of bytes as paths is deprecated on Windows, and this feature is not available when using bytes.)

更改上述选项后，无需进一步配置。

在 3.6 版更改: Python中启用了对长路径的支持。

3.1.4. 无UI 安装

安装程序UI中的所有选项也可以从命令行指定，允许脚本安装程序在许多机器上复制安装，而无需用户交互。还可以在不禁用UI的情况下设置这些选项，以更改一些默认值。

要完全隐藏安装程序UI并静默安装Python，请使用 /quiet 选项。要跳过用户交互但仍然显示进度和错误，请使用 /passive 选项。可以通过 /uninstall 选项立即开始删除Python - - 不会显示任何提示。

所有其他选项都传递为 name=value ，其中值通常是 0 来禁用某个特性， 1 来启用某个特性或路径。可用选项的完整列表如下所示。

名称

描述

默认值

InstallAllUsers

执行全局安装。

0

TargetDir

安装目录

基于InstallAllUsers选择

DefaultAllUsersTargetDir

所有用户安装的默认安装目录

%ProgramFiles%\Python X.Y 或 %ProgramFiles(x86)%\Python X.Y

DefaultJustForMeTargetDir

仅为我安装的默认安装目录

%LocalAppData%\Programs\PythonXY or %LocalAppData%\Programs\PythonXY-32

DefaultCustomTargetDir

UI中显示的默认自定义安装目录

（空）

AssociateFiles

如果还安装了启动器，则创建文件关联。

1

CompileAll

将所有 .py 文件编译为 .pyc 。

0

PrependPath

将install和Scripts目录添加到 PATH 以及将 .PY 添加到 PATHEXT

0

Shortcuts

如果已安装，为解释器，文档和IDLE创建快捷方式

1

Include_doc

安装Python手册

1

Include_debug

安装调试二进制文件

0

Include_dev

安装开发人员头文件和库

1

Include_exe

安装 python.exe 及相关文件

1

Include_launcher

安装 适用于Windows的Python启动器 .

1

InstallLauncherAllUsers

为所有用户安装 适用于Windows的Python启动器 。

1

Include_lib

安装标准库和扩展模块

1

Include_pip

安装捆绑的pip和setuptools

1

Include_symbols

安装调试符号(*.pdb)

0

Include_tcltk

安装Tcl/Tk 支持和IDLE

1

Include_test

安装标准库测试套件

1

Include_tools

安装实用程序脚本

1

LauncherOnly

仅安装启动器。这将覆盖大多数其他选项。

0

SimpleInstall

禁用大多数安装UI

0

SimpleInstallDescription

使用简化安装UI时显示的自定义消息。

（空）

例如，要以静默方式全局安装默认的Python，您可以（在命令提示符>）使用以下命令:

python-3.6.0.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0

要允许用户在没有测试套件的情况下轻松安装Python的个人副本，可以使用以下命令提供快捷方式。这将显示一个简化的初始页面，不允许自定义:

python-3.6.0.exe InstallAllUsers=0 Include_launcher=0 Include_test=0
    SimpleInstall=1 SimpleInstallDescription="Just for me, no test suite."

（请注意，省略启动器也会省略文件关联，并且仅在全局安装包含启动器时才建议用于每用户安装。）

上面列出的选项也可以在一个名为 unattend.xml 的文件中与可执行文件一起提供。此文件指定选项和值的列表。作为属性提供的值，（如果可能）它将转换为数字。作为文本提供的值，始终保留为字符串。此示例文件设置与上一示例采用相同的选项：

<Options>
    <Option Name="InstallAllUsers" Value="no" />
    <Option Name="Include_launcher" Value="0" />
    <Option Name="Include_test" Value="no" />
    <Option Name="SimpleInstall" Value="yes" />
    <Option Name="SimpleInstallDescription">Just for me, no test suite</Option>
</Options>

3.1.5. 免下载安装

由于下载的初始安装包中未包含Python的某些可选功能，如果选择安装这些功能可能需要Internet连接。为了避免这种需要，可以按需下载所有可能的组件，以创建一个完整的布局，该布局将不再需要internet连接，而不管所选择的特性是什么。请注意，此下载可能比要求的要大，但是如果要执行大量安装，则拥有本地缓存​​的副本非常有用。

Execute the following command from Command Prompt to download all possible required files. Remember to substitute python-3.6.0.exe for the actual name of your installer, and to create layouts in their own directories to avoid collisions between files with the same name.

python-3.6.0.exe /layout [optional target directory]

您也可以指定 /quiet 选项来隐藏进度显示。

3.1.6. 修改安装

安装Python后，您可以通过Windows中的“程序和功能”工具添加或删除功能。选择Python条目并选择“卸载/更改”以在维护模式下打开安装程序。

“修改” 允许您通过修改复选框来添加或删除功能 - 未更改的复选框将不会安装或删除任何内容。在此模式下无法更改某些选项，例如安装目录；要修改这些，您需要完全删除然后重新安装Python。

“修复” 将使用当前设置验证应安装的所有文件，并替换已删除或修改的任何文件

“卸载” 将完全删除Python，但 适用于Windows的Python启动器 除外，它在“程序和功能”中有自己的条目。

3.1.7. 其他平台

随着Python的不断发展，不再支持以前曾经支持的一些平台（由于缺少用户或开发人员）。检查 PEP 11 [https://www.python.org/dev/peps/pep-0011] 了解所有不支持的平台的详细信息。

Windows CE [http://pythonce.sourceforge.net/] 仍然受支持。

Cygwin [https://cygwin.com/] 安装包也提供安装Python解释器 (cf. Cygwin package source [ftp://ftp.uni-erlangen.de/pub/pc/gnuwin32/cygwin/mirrors/cygnus/release/python], Maintainer releases [http://www.tishler.net/jason/software/python/])

有关具有预编译安装程序平台的详细信息，请参阅 Python for Windows [https://www.python.org/downloads/windows/]

参见

Python on XP [http://dooling.com/index.php/2006/03/14/python-on-xp-7-minutes-to-hello-world/]

“7 Minutes to “Hello World!”” by Richard Dooling, 2006

Installing on Windows [http://www.diveintopython.net/installing_python/windows.html]

in “Dive into Python: Python from novice to pro [http://www.diveintopython.net/]” by Mark Pilgrim, 2004, ISBN 1-59059-356-1

For Windows users [http://python.swaroopch.com/installation.html#installation-on-windows]

in “Installing Python” in “A Byte of Python [http://python.swaroopch.com/]” by Swaroop C H, 2003

3.2. 替代捆绑包

除了标准的CPython发行版之外，还有一些包含附加功能的修改包。以下是热门版本及其主要功能的列表：

ActivePython [https://www.activestate.com/activepython/]

具有多平台兼容性的安装程序，文档，PyWin32

Anaconda [https://www.continuum.io/downloads/]

流行的科学模块（如numpy，scipy和pandas）和 conda 包管理器。

Canopy [https://www.enthought.com/products/canopy/]

具有编辑器和其他开发工具的“全面的Python分析环境”。

WinPython [https://winpython.github.io/]

特定于Windows的发行版，包含用于构建包的预构建科学包和工具。

请注意，这些软件包可能不包含最新版本的Python或其他库，并且不由核心Python团队维护或支持。

3.3. 配置Python

要从命令提示符方便地运行Python，您可以考虑在Windows中更改一些默认环境变量。虽然安装程序提供了为您配置PATH和PATHEXT变量的选项，但这仅适用于单版本、全局安装。如果您经常使用多个版本的Python，请考虑使用 适用于Windows的Python启动器 。

3.3.1. 附录：设置环境变量

Windows允许在用户级别和系统级别永久配置环境变量，或临时在命令提示符中配置环境变量。

要临时设置环境变量，请打开命令提示符并使用 set 命令：

C:\>set PATH=C:\Program Files\Python 3.6;%PATH%
C:\>set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib
C:\>python

这些环境变量的更改将应用​​于在该控制台中执行的任何其他命令，并且，由该控制台启动的任何应用程序都继承设这些设置。

在百分号中包含的变量名将被现有值替换，允许在开始或结束时添加新值。通过将包含 python.exe 的目录添加到开头来修改 PATH 是确保启动正确版本的Python的常用方法。

要永久修改默认环境变量，请单击“开始”并搜索“编辑环境变量”，或打开“系统属性” Advanced system settings ，然后单击 Environment Variables 按钮。在此对话框中，您可以添加或修改用户和系统变量。要更改系统变量，您需要对计算机进行无限制访问（即管理员权限）。

注解

Windows会将用户变量串联的系统变量 之后 ，这可能会在修改 PATH 时导致意外结果。

PYTHONPATH 变量被Python 2和Python 3的所有版本使用，因此除非它只包含与所有已安装的Python版本兼容的代码，否则不要永久配置此变量。

参见

https://support.microsoft.com/kb/100843

Windows NT 的环境变量

https://technet.microsoft.com/en-us/library/cc754250.aspx

用于临时修改环境变量的SET命令

https://technet.microsoft.com/en-us/library/cc755104.aspx

用于永久修改环境变量的SETX命令

https://support.microsoft.com/kb/310519

如何在Windows XP中管理环境变量

https://www.chem.gla.ac.uk/~louis/software/faq/q1.html

设置环境变量（For Windows 2000/NT），Louis J. Farrugia

3.3.2. 查找Python可执行文件

在 3.5 版更改.

除了使用自动创建的Python解释器的开始菜单项之外，您可能还想在命令提示符下启动Python。安装程序有一个选项可以为您设置。

在安装程序的第一页上，可以选择标记为“将Python添加到环境变量”的选项，以使安装程序将安装位置添加到 PATH 。还添加了 Scripts\ 文件夹的位置。这允许你输入 python 来运行解释器，并且 pip 用于包安装程序。因此，您还可以使用命令行选项执行脚本，请参阅 命令行 文档。

如果在安装时未启用此选项，则始终可以重新运行安装程序，选择“修改”并启用它。或者，您可以使用 附录：设置环境变量 的方法手动修改 PATH 。您需要将Python安装目录添加到 PATH 环境变量中，该内容与其他条目用分号分隔。示例变量可能如下所示（假设前两个条目已经存在）:

C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Python 3.6

3.4. 适用于Windows的Python启动器

3.3 新版功能.

用于Windows的Python启动器是一个实用程序，可帮助定位和执行不同的Python版本。它允许脚本（或命令行）指示特定Python版本的首选项，并将定位并执行该版本。

与 PATH 变量不同，启动器将正确选择最合适的Python版本。它更倾向于按用户安装而不是系统安装，并按语言版本排序，而不是使用最新安装的版本。

3.4.1. 入门

3.4.1.1. 从命令行

在 3.6 版更改.

全局安装Python 3.3及更高版本将把启动器放在你的 PATH 上。启动程序与所有可用的Python版本兼容，因此安装哪个版本无关紧要。要检查启动程序是否可用，请在命令提示符中执行以下命令：

py

您应该会发现已安装的最新版本的Python已启动 - 它可以正常退出，并且将指定的任何其他命令行参数直接发送到Python。

如果您安装了多个版本的Python（例如，2.7和 3.6 ），您会注意到Python 3.6 启动 - 如果要启动Python 2.7，尝试命令：

py -2.7

如果您想使用 Python 2.x 的最新版本，请尝试以下命令：

py -2

你会发现Python 2.x 的最新版本已启动。

如果您看到以下错误，则表明您没有安装启动器：

'py' is not recognized as an internal or external command,
operable program or batch file.

除非在安装时选择了该选项，单个用户安装的Python不会将启动程序添加到 PATH 。

3.4.1.2. 从虚拟环境

3.5 新版功能.

如果启动程序运行时没有明确的Python版本，并且虚拟环境（使用标准库创建 venv 模块或外部 virtualenv 工具）处于活动状态，则启动程序将运行虚拟环境的解释器而不是全局的。要运行全局解释器，请停用虚拟环境，或显式指定全局Python版本。

3.4.1.3. 从脚本

让我们创建一个测试Python脚本 - 创建一个名为``hello.py``的文件，其中包含以下内容

#! python
import sys
sys.stdout.write("hello from Python %s\n" % (sys.version,))

从hello.py所在的目录中，执行以下命令：

py hello.py

您应该注意到最新的Python 2.x安装的版本号已打印出来。现在尝试将第一行更改为：

#! python3

现在，重新执行该命令应该打印最新的Python 3.x信息。与上面的命令行示例一样，你可以更明确的指定版本限定符。假设您安装了Python 2.6，请尝试将第一行更改为 #! python2.6 ，你会发现打印的2.6版本信息。

请注意，与交互式使用不同，裸“python”将使用您已安装的Python 2.x的最新版本。这是为了向后兼容及兼容Unix，其中命令 python 通常是指Python 2。

3.4.1.4. 从文件关联

安装时应该将启动器与Python文件（即 .py, .pyw, .pyc 文件）相关联。这意味着当您从Windows资源管理器中双击其中一个文件时，将使用启动程序，因此您可以使用上述相同的工具让脚本指定应使用的版本。

这样做的主要好处是，单个启动程序可以同时支持多个Python版本，具体取决于第一行的内容。

3.4.2. Shebang Lines

如果脚本文件的第一行以 #! 开头，则称为 “shebang” 行。Linux和其他类Unix操作系统都有对这些行的本机支持，它们通常在此类系统上用来指示应该如何执行脚本。这个启动器允许在Windows上对Python脚本使用相同的工具，上面的示例演示了它们的使用。

为了允许Python脚本中的shebang行在Unix和Windows之间移植，该启动器支持许多“虚拟”命令来指定要使用的解释器。支持的虚拟命令是：

/usr/bin/env python

/usr/bin/python

/usr/local/bin/python

python

例如，如果脚本开始的第一行

#! /usr/bin/python

将找到并使用默认的Python。因为在Unix上编写的许多Python脚本已经有了这一行，你应该发现这些脚本可以由启动器使用而无需修改。如果您在Windows上编写一个新脚本，希望在Unix上有用，那么您应该使用以 /usr 开头的一个shebang行。

Any of the above virtual commands can be suffixed with an explicit version (either just the major version, or the major and minor version) - for example /usr/bin/python2.7 - which will cause that specific version to be located and used.

shebang line的 /usr/bin/env 形式还有一个特殊属性。在寻找已安装的Python解释器之前，此表单将搜索可执行文件 PATH 以获取Python可执行文件。这对应于Unix中 env 程序的行为，该程序将在 PATH 执行搜索。

3.4.3. shebang lines 的参数

shebang lines 还可以指定要传递给Python解释器的其他选项。例如，如果你有一个shebang lines ：

#! /usr/bin/python -v

然后Python将以 -v 选项启动

3.4.4. 自定义

3.4.4.1. 通过INI文件自定义

启动程序将搜索两个.ini文件 - 在当前用户的 “application data” 目录中搜索 py.ini （即通过使用 CSIDL_LOCAL_APPDATA 调用Windows函数 SHGetFolderPath 返回的目录）以及与启动器位于同一目录中的 py.ini 。相同的.ini文件既用于启动器的“控制台”版本（即 py.exe），也用于“windows”版本（即pyw.exe）

“应用程序目录”中指定的自定义优先于可执行文件旁边.ini文件的自定义，因此对启动程序旁边的.ini文件不具有写访问权限的用户可以覆盖该全局.ini文件中的命令。

3.4.4.2. 自定义默认的Python版本

In some cases, a version qualifier can be included in a command to dictate which version of Python will be used by the command. A version qualifier starts with a major version number and can optionally be followed by a period (‘.’) and a minor version specifier. If the minor qualifier is specified, it may optionally be followed by “-32” to indicate the 32-bit implementation of that version be used.

例如，一个shebang line 的 #!python 行没有版本限定符，而 #!python3 有一个版本限定符，它只指定一个主要版本。

If no version qualifiers are found in a command, the environment variable PY_PYTHON can be set to specify the default version qualifier - the default value is “2”. Note this value could specify just a major version (e.g. “2”) or a major.minor qualifier (e.g. “2.6”), or even major.minor-32.

如果没有找到次要版本限定符，则可以设置环境变量 PY_PYTHON{major} （其中 {major} 是上面确定的当前主要版本限定符）以指定完整版本。如果没有找到这样的选项，启动器将枚举已安装的Python版本并使用为主要版本找到的最新次要版本，尽管不能保证，但该版本可能是该系列中最新安装的版本。

On 64-bit Windows with both 32-bit and 64-bit implementations of the same (major.minor) Python version installed, the 64-bit version will always be preferred. This will be true for both 32-bit and 64-bit implementations of the launcher - a 32-bit launcher will prefer to execute a 64-bit Python installation of the specified version if available. This is so the behavior of the launcher can be predicted knowing only what versions are installed on the PC and without regard to the order in which they were installed (i.e., without knowing whether a 32 or 64-bit version of Python and corresponding launcher was installed last). As noted above, an optional “-32” suffix can be used on a version specifier to change this behaviour.

示例：

如果没有设置相关选项，命令 python 和 python2 将使用安装的最新Python 2.x版本，命令 python3 将使用最新安装的Python 3.x.

命令 python3.1 和 python2.7 根本不会查阅任何选项，因为版本已完全指定。

如果 PY_PYTHON=3 ，命令``python`` 和 python3 都将使用最新安装的Python 3版本。

如果 PY_PYTHON=3.1-32 ，命令 python 将使用3.1的32位实现，而命令 python3 将使用最新安装的Python（PY_PYTHON根本没有被视为指定了主要版本。）

如果 PY_PYTHON=3 且 PY_PYTHON3=3.1，命令 python 和 python3 都将特别使用3.1

除环境变量外，还可以在启动程序使用的.INI文件中配置相同的设置。 INI文件中的部分称为 [defaults] ，键名称将与没有前导 PY_ 前缀的环境变量相同（并注意INI文件中的键名不区分大小写） 。）环境变量的内容将覆盖INI文件中指定的内容。

例如:

设置 PY_PYTHON=3.1 等同于包含以下内容的INI文件：

[defaults]
python=3.1

设置 PY_PYTHON=3 和 PY_PYTHON3=3.1 相当于包含以下内容的INI文件：

[defaults]
python=3
python3=3.1

3.4.5. 诊断

如果设置了环境变量 PYLAUNCH_DEBUG （任何值），启动器将诊断信息打印到stderr（即：控制台）。虽然这些信息同时具有冗长 和 简洁性，但它应该允许您查看Python的版本、选择特定版本的原因以及用于执行目标Python的确切命令行。

3.5. 查找模块

Python通常将其库（以及您的site-packages文件夹）存储在安装目录中。因此，如果您已将Python安装到 C:\Python\ ，则默认库将驻留在 C:\Python\Lib\ 中，第三方模块存储在 C:\Python\Lib\site-packages\ 。

To completely override sys.path, create a ._pth file with the same name as the DLL (python36._pth) or the executable (python._pth) and specify one line for each path to add to sys.path. The file based on the DLL name overrides the one based on the executable, which allows paths to be restricted for any program loading the runtime if desired.

当文件存在时，将忽略所有注册表和环境变量，启用隔离模式，并且：除非文件中的一行指定 import site ，否则不会导入 site 。以 ＃ 开头的空白路径和行将被忽略。每个路径可以是绝对的或相对于文件的位置。不允许使用除 site 以外的导入语句，并且不能指定任意代码。

Note that .pth files (without leading underscore) will be processed normally by the site module.

当找不到 ._pth 文件时， sys.path 是如何在Windows上填充的：

在开始时，添加一个空条目，该条目对应于当前目录。

如果环境变量 PYTHONPATH 存在，如 环境变量 中所述，则接下来添加其条目。请注意，在Windows上，此变量中的路径必须用分号分隔，以区别于驱动器标识符中使用的冒号（ C:\ 等）。

附加的 “application paths” 可以同时添加到注册表` HKEY_CURRENT_USER` 和 HKEY_LOCAL_MACHINE 分支下的:samp:\SOFTWARE\Python\PythonCore\{version}\PythonPath 中作为子键。以分号分隔的路径字符串作为默认值的子键将导致每个路径添加到 sys.path 。（请注意，所有已知的安装程序都只使用HKLM，因此HKCU通常为空。）

如果设置了环境变量 PYTHONHOME ，则将其假定为 “Python 主目录” 。否则，主Python可执行文件的路径用于定位 “landmark 文件” （ Lib\os.py 或 pythonXY.zip ）以推断 ”Python 主目录“ 。如果找到了Python主目录，则基于该文件夹将相关的子目录添加到 sys.path （Lib , plat-win 等）。否则，核心Python路径是从存储在注册表中的PythonPath构造的。

如果找不到Python Home，也没有指定 PYTHONPATH 环境变量，并且找不到注册表项，则使用具有相对条目的默认路径（例如 .\Lib; .\plat-win 等等）。

如果在主可执行文件旁边或在可执行文件上一级的目录中找到 pyvenv.cfg 文件，则以下变体适用：

如果``home`` 是一个绝对路径，并且 PYTHONHOME 未设置，则在推断起始位置时使用此路径而不是主可执行文件的路径

这一切的最终结果是：

运行 python.exe ，或主Python目录中的任何其他.exe（安装版本，或直接来自PCbuild目录）时，推导出核心路径，并忽略注册表中的核心路径。始终读取注册表中的其他“应用程序路径”。

当Python托管在另一个.exe（不同的目录，通过COM嵌入等）时，将不会推断出“Python Home”，因此使用了来自注册表的核心路径。始终读取注册表中的其他“应用程序路径”。

如果Python找不到它的主目录并且没有注册表值（冻结的.exe，一些非常奇怪的安装设置），那么你会得到一条带有一些默认但相对的路径的路径。

对于那些想要将Python捆绑到其应用程序或发行版中的人，以下建议将防止与其他安装冲突：

在您的可执行文件中包含一个 ._pth 文件，其中包含目录。这将忽略注册表和环境变量中列出的路径，并忽略 site ，除非列出 import site 。

If you are loading python3.dll or python36.dll in your own executable, explicitly call Py_SetPath() or (at least) Py_SetProgramName() before Py_Initialize().

清除 和/或 覆盖 PYTHONPATH 并在启动来自应用程序的 python.exe 之前设置 PYTHONHOME 。

如果您不能使用前面的建议（例如，您是一个允许人们直接运行：file:python.exe`的分发版），请确保安装目录中存在landmark 文件（:file:`Lib\os.py）。（请注意，在zip文件中不会检测到该文件，但会检测到正确命名的zip文件。）

这些将确保系统范围安装中的文件不会优先于与应用程序捆绑在一起的标准库的副本。否则，用户可能会在使用您的应用程序时遇到问题请注意，第一个建议是最好的，因为其他建议可能仍然容易受到注册表和用户站点包中的非标准路径的影响。

在 3.6 版更改:

添加 ._pth 文件支持并从 pyvenv.cfg 中删除 applocal 选项

当直接与可执行文件相邻时，添加 pythonXX.zip 作为潜在的 landmark 。

3.6 版后已移除:

在 Modules （不是 PythonPath ）下的注册表中指定的模块可以通过以下方式导入 importlib.machinery.WindowsRegistryFinder 。在Windows上，此查找程序在3.6.0及更早版本的可用，但可能需要在将来显式添加到 sys.meta_path

3.6. 附加模块

尽管Python的目标是在所有平台中都可移植，但是Windows有一些独特的特性。在标准库和外部都有一些模块和代码片段在使用这些特性。

特定于Windows的标准模块记录在 Windows系统相关模块 中。

3.6.1. PyWin32

Mark Hammond 的 PyWin32 [https://pypi.org/project/pywin32] 模块是一组用于高级Windows特定支持的模块。这包括以下实用程序：

Component Object Model [https://www.microsoft.com/com/] (COM)

Win32 API 调用

注册

事件日志

Microsoft Foundation Classes [https://msdn.microsoft.com/en-us/library/fe1cf721%28VS.80%29.aspx] (MFC) 用户界面

PythonWin [https://web.archive.org/web/20060524042422/https://www.python.org/windows/pythonwin/] 是PyWin32附带的一个示例MFC应用程序。它是一个内置调试器的可嵌入IDE。

参见

Win32 How Do I…? [http://timgolden.me.uk/python/win32_how_do_i.html]

by Tim Golden

Python and COM [http://www.boddie.org.uk/python/COM.html]

by David and Paul Boddie

3.6.2. cx_Freeze

cx_Freeze [http://cx-freeze.sourceforge.net/] is a distutils extension (see 扩展 Distutils) which wraps Python scripts into executable Windows programs (*.exe files). When you have done this, you can distribute your application without requiring your users to install Python.

3.6.3. WConio

由于Python的高级终端处理层 curses 仅限于类Unix系统，因此Windows还有一个独立的库：用于Python的Windows 控制台 I/O .

WConio [http://newcenturycomputers.net/projects/wconio.html] 是Turbo-C的 CONIO.H 装饰器,用于创建文本用户界面。

3.7. 在Windows上编译Python

如果你想自己编译CPython，首先要做的是获取 source [https://www.python.org/downloads/source/]. 。您可以下载最新版本的源代码，也可以重新签出 checkout [https://devguide.python.org/setup/#getting-the-source-code]. 。

源代码树包含Microsoft Visual Studio 2015的构建解决方案和项目文件，它是用于构建官方Python版本的编译器。这些文件位于 PCbuild 目录中。

检查 PCbuild/readme.txt 以获取有关构建过程的一般信息。

有关扩展模块，请参阅 在Windows平台编译C和C++扩展 。

参见

Python + Windows + distutils + SWIG + gcc MinGW [http://sebsauvage.net/python/mingw.html]

或 “Creating Python extensions in C/C++ with SWIG and compiling them with MinGW gcc under Windows” 或 “Installing Python extension with distutils and without Microsoft Visual C++” by Sébastien Sauvage, 2003

MingW – Python extensions [http://oldwiki.mingw.org/index.php/Python%20extensions]

by Trent Apted et al, 2007

3.8. Embedded Distribution

3.5 新版功能.

嵌入式发行版是一个包含最小Python环境的ZIP文件。它旨在作为另一个应用程序的一部分，而不是由最终用户直接访问。

When extracted, the embedded distribution is (almost) fully isolated from the user’s system, including environment variables, system registry settings, and installed packages. The standard library is included as pre-compiled and optimized .pyc files in a ZIP, and python3.dll, python36.dll, python.exe and pythonw.exe are all provided. Tcl/tk (including all dependants, such as Idle), pip and the Python documentation are not included.

注解

嵌入式发行版不包括 Microsoft C Runtime [https://www.microsoft.com/en-us/download/details.aspx?id=48145] ，应用程序安装程序负责提供此功能。运行时可能已经预先安装在用户的系统上或通过Windows Update自动安装，并且可以通过在系统目录中找到 ucrtbase.dll 来检测

第三方软件包应该由应用程序与嵌入式发行版一起安装。这个发行版不支持像常规Python安装那样使用pip来管理依赖关系，不过可以小心地将pip包含进来并使用它进行自动更新。通常，第三方包应该作为应用程序的一部分(“打包”)处理，以便开发人员在向用户提供更新之前能够确保与新版本兼容。

下面描述了这个发行版的两个推荐用例。

3.8.1. Python应用程序

用Python编写的应用程序并不一定要求用户了解这一事实。在这种情况下，可以使用嵌入式发行版在安装包中包含Python的私有版本。根据它应该有多透明(或者相反，它应该看起来有多专业)，有两个选项。

使用专门的可执行文件作为启动程序需要一些编码，但为用户提供了最透明的体验。使用定制的启动器，没有明显的迹象表明程序是在Python上运行的：图标可以定制，公司和版本信息可以指定，文件关联可以正常运行。在大多数情况下，自定义启动程序应该只需使用硬编码的命令行就能调用 Py_Main

更简单的方法是提供批处理文件或生成的快捷方式，使用所需的命令行参数直接调用 python.exe 或 pythonw.exe 。在这种情况下，应用程序将显示为Python而不是其实际名称，并且用户可能无法将其与其他正在运行的Python进程或文件关联区分开来。

对于后一种方法，包应该与Python可执行文件一起作为目录安装，以确保它们在路径上可用。使用专用的启动器，包可以位于其他位置，因为在启动应用程序之前有机会指定搜索路径。

3.8.2. 嵌入Python

用本地代码编写的应用程序通常需要某种形式的脚本语言，嵌入式Python发行版可以用于此目的。通常，应用程序的大部分都是本机代码，某些部分将调用 python.exe 或直接使用 python3.dll 。无论是哪种情况，将嵌入的发行版解压缩到应用程序安装的子目录中就足以提供可加载的Python解释器。

与应用程序使用一样，包可以安装到任何位置，因为在初始化解释器之前有机会指定搜索路径。否则，使用嵌入式发行版和常规安装之间没有根本区别。

3.9. Other resources

参见

Python Programming On Win32 [http://shop.oreilly.com/product/9781565926219.do]

“Help for Windows Programmers” by Mark Hammond and Andy Robinson, O’Reilly Media, 2000, ISBN 1-56592-621-8

A Python for Windows Tutorial [http://www.imladris.com/Scripts/PythonForWindows.html]

by Amanda Birmingham, 2004

PEP 397 [https://www.python.org/dev/peps/pep-0397] - Python launcher for Windows

The proposal for the launcher to be included in the Python distribution.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python安装和使用 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python安装和使用 »

|

4. 在苹果系统上使用 Python

作者

Bob Savage <bobsavage@mac.com>

运行 Mac OS X 的 Macintosh上 的 Python 原则上与任何其他 Unix 平台上的 Python 非常相似，但是还有一些额外的功能，例如 IDE 和包管理器，值得一提。

4.1. 获取和安装 MacPython

Mac OS X 10.8 附带 Apple 预安装的 Python 2.7 。 如果你愿意，可以从 Python 网站（ https://www.python.org ）安装最新版本的 Python 3 。 Python 的当前“通用二进制”版本可以在 Mac 的新 Intel 和传统 PPC CPU 上本地运行。

你安装后得到的东西有：

A MacPython 3.6 folder in your Applications folder. In here you find IDLE, the development environment that is a standard part of official Python distributions; PythonLauncher, which handles double-clicking Python scripts from the Finder; and the “Build Applet” tool, which allows you to package Python scripts as standalone applications on your system.

框架 /Library/Frameworks/Python.framework ，包括 Python 可执行文件和库。安装程序将此位置添加到 shell 路径。 要卸载 MacPython ，你可以简单地移除这三个项目。 Python 可执行文件的符号链接放在 /usr/local/bin/ 中。

Apple 提供的 Python 版本分别安装在 /System/Library/Frameworks/Python.framework 和 /usr/bin/python 中。 你永远不应修改或删除这些内容，因为它们由 Apple 控制并由 Apple 或第三方软件使用。 请记住，如果你选择从 python.org 安装较新的 Python 版本，那么你的计算机上将安装两个不同但都有用的 Python ，因此你的路径和用法与你想要执行的操作一致非常重要。

IDLE 包含一个帮助菜单，允许你访问 Python 文档。 如果您是 Python 的新手，你应该开始阅读该文档中的教程介绍。

如果你熟悉其他 Unix 平台上的 Python ，那么你应该阅读有关从 Unix shell 运行 Python 脚本的部分。

4.1.1. 如何运行 Python 脚本

在 Mac OS X 上开始使用 Python 的最佳方法是通过 IDLE 集成开发环境，参见 IDE 部分，并在 IDE 运行时使用“帮助”菜单。

If you want to run Python scripts from the Terminal window command line or from the Finder you first need an editor to create your script. Mac OS X comes with a number of standard Unix command line editors, vim and emacs among them. If you want a more Mac-like editor, BBEdit or TextWrangler from Bare Bones Software (see http://www.barebones.com/products/bbedit/index.html) are good choices, as is TextMate (see https://macromates.com/). Other editors include Gvim (http://macvim.org) and Aquamacs (http://aquamacs.org/).

要从终端窗口运行脚本，必须确保:file:/usr/local/bin 位于 shell 搜索路径中。

要从 Finder 运行你的脚本，你有两个选择：

把脚本拖拽到 PythonLauncher

选择 PythonLauncher 作为通过 finder Info 窗口打开脚本（或任何 .py 脚本）的默认应用程序，然后双击脚本。 PythonLauncher 有各种首选项来控制脚本的启动方式。 拖拽方式允许你为一次调用更改这些选项，或使用其“首选项”菜单全局更改内容。

4.1.2. 运行有图形界面的脚本

对于旧版本的 Python ，你需要注意一个 Mac OS X 的怪异之处：与 Aqua 窗口管理器通信的程序（换而言之，任何具有图形界面的程序）需要以特殊方式运行。 使用 pythonw 而不是 python 来启动这样的脚本。

With Python 3.6, you can use either python or pythonw.

4.1.3. 配置

OS X 上的 Python 遵循所有标准的 Unix 环境变量，例如 PYTHONPATH ，但是为 Finder 启动的程序设置这些变量是非标准的，因为 Finder 在启动时不读取你的 .profile 或 .cshrc 。你需要创建一个文件 ~/.MacOSX/environment.plist 。 有关详细信息，请参阅 Apple 的技术文档 QA1067 。

更多关于在 MacPython 中安装 Python 包的信息，参阅 安装额外的 Python 包 部分。

4.2. IDE

MacPython ships with the standard IDLE development environment. A good introduction to using IDLE can be found at https://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html.

4.3. 安装额外的 Python 包

有几个方法可以安装额外的 Python 包：

可以通过标准的 Python distutils 模式（ python setup.py install ）安装软件包。

许多包也可以通过 setuptools 扩展或 pip 包装器安装，请参阅 https://pip.pypa.io/ 。

4.4. Mac 上的图形界面编程

使用 Python 在 Mac 上构建 GUI 应用程序有多种选择。

PyObjC is a Python binding to Apple’s Objective-C/Cocoa framework, which is the foundation of most modern Mac development. Information on PyObjC is available from https://pythonhosted.org/pyobjc/.

标准的 Python GUI 工具包是 tkinter ，基于跨平台的 Tk 工具包（ https://www.tcl.tk ）。 Apple 的 OS X 捆绑了 Aqua 原生版本的 Tk ，最新版本可以从 https://www.activestate.com 下载和安装；它也可以从源代码构建。

wxPython is another popular cross-platform GUI toolkit that runs natively on Mac OS X. Packages and documentation are available from http://www.wxpython.org.

PyQt 是另一种流行的跨平台 GUI 工具包，可在 Mac OS X 上本机运行。更多信息可在 https://riverbankcomputing.com/software/pyqt/intro 上找到。

4.5. 在 Mac 上分发 Python 应用程序

The “Build Applet” tool that is placed in the MacPython 3.6 folder is fine for packaging small Python scripts on your own machine to run as a standard Mac application. This tool, however, is not robust enough to distribute Python applications to other users.

在 Mac 上部署独立 Python 应用程序的标准工具是 py2app 。有关安装和使用 py2app 的更多信息，请访问 http://undefined.org/python/#py2app 。

4.6. 其他资源

MacPython 邮件列表是 Mac 上 Python 用户和开发人员的优秀支持资源：

https://www.python.org/community/sigs/current/pythonmac-sig/

另一个有用的资源是 MacPython wiki ：

https://wiki.python.org/moin/MacPython

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python安装和使用 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

Python语言参考

本参考手册描述了 Python 的语法和“核心语义”。本参考是简洁的，但试图做到准确和完整。 非必要的内建对象类型和内建函数、模块的语义描述在 Python 标准库 中。有关该语言的非正式介绍，请参阅 Python 教程 。对 C 或 C++ 程序员，还有两个额外的手册： 扩展和嵌入 Python 解释器 概述了如何编写一个 Python 扩展模块，Python/C API 参考手册 详细介绍了 C/C++ 中可用的接口。

1. 概述

1.1. 其他实现

1.2. 标注

2. 词法分析

2.1. 行结构

2.2. 其他形符

2.3. 标识符和关键字

2.4. 字面值

2.5. 运算符

2.6. 分隔符

3. 数据模型

3.1. 对象、值与类型

3.2. 标准类型层级结构

3.3. 特殊方法名称

3.4. 协程

4. 执行模型

4.1. 程序的结构

4.2. 命名与绑定

4.3. 异常

5. 导入系统

5.1. importlib

5.2. 包

5.3. 搜索

5.4. 加载

5.5. 基于路径的查找器

5.6. 替换标准导入系统

5.7. 有关 __main__ 的特殊事项

5.8. 开放问题项

5.9. 参考文献

6. 表达式

6.1. 算术转换

6.2. 原子

6.3. 原型

6.4. await 表达式

6.5. 幂运算符

6.6. 一元算术和位运算

6.7. 二元算术运算符

6.8. 移位运算

6.9. 二元位运算

6.10. 比较运算

6.11. 布尔运算

6.12. 条件表达式

6.13. lambda 表达式

6.14. 表达式列表

6.15. 求值顺序

6.16. 运算符优先级

7. 简单语句

7.1. 表达式语句

7.2. 赋值语句

7.3. The assert statement

7.4. The pass statement

7.5. The del statement

7.6. The return statement

7.7. The yield statement

7.8. The raise statement

7.9. The break statement

7.10. The continue statement

7.11. The import statement

7.12. The global statement

7.13. The nonlocal statement

8. 复合语句

8.1. The if statement

8.2. The while statement

8.3. The for statement

8.4. The try statement

8.5. The with statement

8.6. 函数定义

8.7. 类定义

8.8. 协程

9. 最高层级组件

9.1. 完整的 Python 程序

9.2. 文件输入

9.3. 交互式输入

9.4. 表达式输入

10. 完整的语法规范

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

1. 概述

本参考手册是对 Python 编程语言的描述。并不适宜作为教程使用。

我希望尽可能地保证内容精确无误，但还是选择使用自然词句进行描述，正式的规格定义仅用于句法和词法解析。这样应该能使文档对于普通人来说更易理解，但也可能导致一些歧义。因此，如果你是来自火星并且想凭借这份文档把 Python 重新实现一遍，也许有时需要自行猜测，实际上最终大概会得到一个十分不同的语言。而在另一方面，如果你正在使用 Python 并且想了解有关该语言特定领域的精确规则，你应该能够在这里找到它们。如果你希望查看对该语言更正式的定义，也许你可以花些时间自己写上一份 — 或者发明一台克隆机器 :-)

在语言参考文档里加入过多的实现细节是很危险的 — 具体实现可能发生改变，对同一语言的其他实现可能使用不同的方式。而在另一方面，CPython 是得到广泛使用的 Python 实现 (然而其他一些实现的拥护者也在增加)，其中的特殊细节有时也值得一提，特别是当其实现方式导致额外的限制时。因此，你会发现在正文里不时会跳出来一些简短的 “实现注释”。

每种 Python 实现都带有一些内置和标准的模块。相关的文档可参见 Python 标准库 索引。少数内置模块也会在此提及，如果它们同语言描述存在明显的关联。

1.1. 其他实现

虽然官方 Python 实现差不多得到最广泛的欢迎，但也有一些其他实现对特定领域的用户来说更具吸引力。

知名的实现包括:

CPython

这是最早出现并持续维护的 Python 实现，以 C 语言编写。新的语言特性通常在此率先添加。

Jython

以 Java 语言编写的 Python 实现。此实现可以作为 Java 应用的一个脚本语言，或者可以用来创建需要 Java 类库支持的应用。想了解更多信息可访问 Jython 网站 [http://www.jython.org/]。

Python for .NET

此实现实际上使用了 CPython 实现，但是属于 .NET 托管应用并且可以引入 .NET 类库。它的创造者是 Brian Lloyd。想了解详情可访问 Python for .NET 主页 [https://pythonnet.github.io/]。

IronPython

另一个 .NET 的 Python 实现，与 Python.NET 不同点在于它是生成 IL 的完全 Python 实现，并且将 Python 代码直接编译为 .NET 程序集。它的创造者就是当初创造 Jython 的 Jim Hugunin。想了解详情可访问 IronPython 网站 [http://ironpython.net/]。

PyPy

完全使用 Python 语言编写的 Python 实现。它支持多个其他实现所没有的高级特性，例如非栈式支持和 JIT 编译器等。此项目的目标之一是通过允许方便地修改解释器 (因为它是用 Python 编写的)，鼓励该对语言本身进行试验。想了解详情可访问 PyPy 项目主页 [http://pypy.org/]。

以上这些实现都可能在某些方面与此参考文档手册的描述有所差异，或是引入了超出标准 Python 文档范围的特定信息。请参考它们各自的专门文档，以确定你正在使用的这个实现有哪些你需要了解的东西。

1.2. 标注

句法和词法解析的描述采用经过改进的 BNF 语法标注。这包含以下定义样式:

name      ::=  lc_letter (lc_letter | "_")*
lc_letter ::=  "a"..."z"

第一行表示 name 是一个 lc_letter 之后跟零个或多个 lc_letter 和下划线。而一个 lc_letter 则是任意单个 'a' 至 'z' 字符。(实际上在本文档中始终采用此规则来定义词法和语法规则的名称。)

每条规则的开头是一个名称 (即该规则所定义的名称) 加上 ::=。竖线 (|) 被用来分隔可选项；它是此标注中最灵活的操作符。星号 (*) 表示前一项的零次或多次重复；类似地，加号 (+) 表示一次或多次重复，而由方括号括起的内容 ([ ]) 表示出现零次或一次 (或者说，这部分内容是可选的)。* 和 + 操作符的绑定是最紧密的；圆括号用于分组。固定字符串包含在引号内。空格的作用仅限于分隔形符。每条规则通常为一行；有许多个可选项的规则可能会以竖线为界分为多行。

在词法定义中 (如上述示例)，还额外使用了两个约定: 由三个点号分隔的两个字符字面值表示在指定 (闭) 区间范围内的任意单个 ASCII 字符。由尖括号 (<...>) 括起来的内容是对于所定义符号的非正式描述；即可以在必要时用来说明 ‘控制字符’ 的意图。

虽然所用的标注方式几乎相同，但是词法定义和句法定义是存在很大区别的: 词法定义作用于输入源中单独的字符，而句法定义则作用于由词法分析所生成的形符流。在下一章节 (“词法分析”) 中使用的 BNF 全部都是词法定义；在之后的章节中使用的则是句法定义。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

2. 词法分析

Python 程序由一个 解析器 读取。输入到解析器的是一个由 词法分析器 所生成的 形符 流，本章将描述词法分析器是如何将一个文件拆分为一个个形符的。

Python 会将读取的程序文本转为 Unicode 码点；源文件的文本编码可由编码声明指定，默认为 UTF-8，详情见 PEP 3120 [https://www.python.org/dev/peps/pep-3120]。如果源文件无法被解码，将会引发 SyntaxError。

2.1. 行结构

一个 Python 程序可分为许多 逻辑行。

2.1.1. 逻辑行

逻辑行的结束是以 NEWLINE 形符表示的。语句不能跨越逻辑行的边界，除非其语法允许包含 NEWLINE (例如复合语句可由多行子语句组成)。一个逻辑行可由一个或多个 物理行 按照明确或隐含的 行拼接 规则构成。

2.1.2. 物理行

物理行是以一个行终止序列结束的字符序列。在源文件和字符串中，可以使用任何标准平台上的行终止序列 - Unix 所用的 ASCII 字符 LF (换行), Windows 所用的 ASCII 字符序列 CR LF (回车加换行), 或者旧 Macintosh 所用的 ASCII 字符 CR (回车)。所有这些形式均可使用，无论具体平台。输入的结束也会被作为最后一个物理行的隐含终止标志。

当嵌入 Python 时，源码字符串传入 Python API 应使用标准 C 的传统换行符 (即 \n，表示 ASCII 字符 LF 作为行终止标志)。

2.1.3. 注释

A comment starts with a hash character (#) that is not part of a string literal, and ends at the end of the physical line. A comment signifies the end of the logical line unless the implicit line joining rules are invoked. Comments are ignored by the syntax; they are not tokens.

2.1.4. 编码声明

如果一条注释位于 Python 脚本的第一或第二行，并且匹配正则表达式 coding[=:]\s*([-\w.]+)，这条注释会被作为编码声明来处理；上述表达式的第一组指定了源码文件的编码。编码声明必须独占一行。如果它是在第二行，则第一行也必须是注释。推荐的编码声明形式如下

# -*- coding: <encoding-name> -*-

这也是 GNU Emacs 认可的形式，以及

# vim:fileencoding=<encoding-name>

这是 Bram Moolenaar 的 VIM 认可的形式。

如果没有编码声明，则默认编码为 UTF-8。此外，如果文件的首字节为 UTF-8 字节顺序标志 (b'\xef\xbb\xbf')，文件编码也声明为 UTF-8 (这是 Microsoft 的 notepad 等软件支持的形式)。

编码声明指定的编码名称必须是 Python 所认可的编码。所有词法分析将使用此编码，包括语义字符串、注释和标识符。

2.1.5. 显式的行拼接

两个或更多个物理行可使用反斜杠字符 (\) 拼接为一个逻辑行，规则如下: 当一个物理行以一个不在字符串或注释内的反斜杠结尾时，它将与下一行拼接构成一个单独的逻辑行，反斜杠及其后的换行符会被删除。例如:

if 1900 < year < 2100 and 1 <= month <= 12 \
   and 1 <= day <= 31 and 0 <= hour < 24 \
   and 0 <= minute < 60 and 0 <= second < 60:   # Looks like a valid date
        return 1

以反斜杠结束的行不能带有注释。反斜杠不能用来拼接注释。反斜杠不能用来拼接形符，字符串除外 (即原文字符串以外的形符不能用反斜杠分隔到两个物理行)。不允许有原文字符串以外的反斜杠存在于物理行的其他位置。

2.1.6. 隐式的行拼接

圆括号、方括号或花括号以内的表达式允许分成多个物理行，无需使用反斜杠。例如:

month_names = ['Januari', 'Februari', 'Maart',      # These are the
               'April',   'Mei',      'Juni',       # Dutch names
               'Juli',    'Augustus', 'September',  # for the months
               'Oktober', 'November', 'December']   # of the year

隐式的行拼接可以带有注释。后续行的缩进不影响程序结构。后续行也允许为空白行。隐式拼接的行之间不会有 NEWLINE 形符。隐式拼接的行也可以出现于三引号字符串中 (见下)；此情况下这些行不允许带有注释。

2.1.7. 空白行

一个只包含空格符，制表符，进纸符或者注释的逻辑行会被忽略 (即不生成 NEWLINE 形符)。在交互模式输入语句时，对空白行的处理可能会因读取-求值-打印循环的具体实现方式而存在差异。在标准交互模式解释器中，一个完全空白的逻辑行 (即连空格或注释都没有) 将会结束一条多行复合语句。

2.1.8. 缩进

一个逻辑行开头处的空白 (空格符和制表符) 被用来计算该行的缩进等级，以决定语句段落的组织结构。

制表符会被 (从左至右) 替换为一至八个空格，这样缩进的空格总数为八的倍数 (这是为了与 Unix 所用的规则一致)。首个非空白字符之前的空格总数将确定该行的缩进层次。一个缩进不可使用反斜杠进行多行拼接；首个反斜杠之前的空格将确定缩进层次。

在一个源文件中如果混合使用制表符和空格符缩进，并使得确定缩进层次需要依赖于制表符对应的空格数量设置，则被视为不合规则；此情况将会引发 TabError。

跨平台兼容性注释: 由于非 UNIX 平台上文本编辑器本身的特性，在一个源文件中混合使用制表符和空格符是不明智的。另外也要注意不同平台还可能会显式地限制最大缩进层级。

行首有时可能会有一个进纸符；它在上述缩进层级计算中会被忽略。处于行首空格内其他位置的进纸符的效果未定义 (例如它可能导致空格计数重置为零)。

多个连续行各自的缩进层级将会被放入一个堆栈用来生成 INDENT 和 DEDENT 形符，具体说明如下。

在读取文件的第一行之前，先向堆栈推入一个零值；它将不再被弹出。被推入栈的层级数值从底至顶持续增加。每个逻辑行开头的行缩进层级将与栈顶行比较。如果相同，则不做处理。如果新行层级较高，则会被推入栈顶，并生成一个 INDENT 形符。如果新行层级较低，则 应当 是栈中的层级数值之一；栈中高于该层级的所有数值都将被弹出，每弹出一级数值生成一个 DEDENT 形符。在文件末尾，栈中剩余的每个大于零的数值生成一个 DEDENT 形符。

这是一个正确 (但令人迷惑) 的Python 代码缩进示例:

def perm(l):
        # Compute the list of all permutations of l
    if len(l) <= 1:
                  return [l]
    r = []
    for i in range(len(l)):
             s = l[:i] + l[i+1:]
             p = perm(s)
             for x in p:
              r.append(l[i:i+1] + x)
    return r

以下示例显示了各种缩进错误:

 def perm(l):                       # error: first line indented
for i in range(len(l)):             # error: not indented
    s = l[:i] + l[i+1:]
        p = perm(l[:i] + l[i+1:])   # error: unexpected indent
        for x in p:
                r.append(l[i:i+1] + x)
            return r                # error: inconsistent dedent

(实际上，前三个错误会被解析器发现；只有最后一个错误是由词法分析器发现的 — return r 的缩进无法匹配弹出栈的缩进层级。)

2.1.9. 形符之间的空白

除非是在逻辑行的开头或字符串内，空格符、制表符和进纸符等空白符都同样可以用来分隔形符。如果两个形符彼此相连会被解析为一个不同的形符，则需要使用空白来分隔 (例如 ab 是一个形符，而 a b 是两个形符)。

2.2. 其他形符

除了 NEWLINE, INDENT 和 DEDENT，还存在以下类别的形符: 标识符, 关键字, 字面值, 运算符 以及 分隔符。 空白字符 (之前讨论过的行终止符除外) 不属于形符，而是用来分隔形符。如果存在二义性，将从左至右读取尽可能长的合法字符串组成一个形符。

2.3. 标识符和关键字

标识符 (或者叫做 名称) 由以下词法定义进行描述。

Python 中的标识符语法是基于 Unicode 标准附件 UAX-31，并加入了下文所定义的细化与修改；更多细节还可参见 PEP 3131 [https://www.python.org/dev/peps/pep-3131] 。

在 ASCII 范围内 (U+0001..U+007F)，可用于标识符的字符与 Python 2.x 一致: 大写和小写字母 A 至 Z，下划线 _ 以及数字 0 至 9，但不可以数字打头。

Python 3.0 引入了 ASCII 范围以外的额外字符 (见 PEP 3131 [https://www.python.org/dev/peps/pep-3131])。这些字符的分类使用包含于 unicodedata 模块中的 Unicode 字符数据库版本。

标识符的长度没有限制。对大小写敏感。

identifier   ::=  xid_start xid_continue*
id_start     ::=  <all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property>
id_continue  ::=  <all characters in id_start, plus characters in the categories Mn, Mc, Nd, Pc and others with the Other_ID_Continue property>
xid_start    ::=  <all characters in id_start whose NFKC normalization is in "id_start xid_continue*">
xid_continue ::=  <all characters in id_continue whose NFKC normalization is in "id_continue*">

上文所用 Unicode 类别码的含义:

Lu - 大写字母

Ll - 小写字母

Lt - 词首大写字母

Lm - 修饰字母

Lo - 其他字母

Nl - 字母数字

Mn - 非空白标识

Mc - 含空白标识

Nd - 十进制数字

Pc - 连接标点

Other_ID_Start - explicit list of characters in PropList.txt [http://www.unicode.org/Public/9.0.0/ucd/PropList.txt] to support backwards compatibility

Other_ID_Continue - 同上

所有标识符在解析时会被转换为规范形式 NFKC；标识符的比较都是基于 NFKC。

A non-normative HTML file listing all valid identifier characters for Unicode 4.1 can be found at https://www.dcl.hpi.uni-potsdam.de/home/loewis/table-3131.html.

2.3.1. 关键字

以下标识符被作为语言的保留字或称 关键字，不可被用作普通标识符。关键字的拼写必须与这里列出的完全一致。

False      class      finally    is         return
None       continue   for        lambda     try
True       def        from       nonlocal   while
and        del        global     not        with
as         elif       if         or         yield
assert     else       import     pass
break      except     in         raise

2.3.2. 保留的标识符类

某些标识符类 (除了关键字) 具有特殊的含义。这些标识符类的命名模式是以下划线字符打头和结尾:

_*

不会被 from module import * 导入。特殊标识符 _ 在交互式解释器中被用来存放最近一次求值结果；它保存在 builtins 模块中。当不处于交互模式时，_ 无特殊含义也没有预定义。参见 The import statement。

注解

_ 作为名称常用于连接国际化文本；请参看 gettext 模块文档了解有关此约定的详情。

__*__

System-defined names. These names are defined by the interpreter and its implementation (including the standard library). Current system names are discussed in the 特殊方法名称 section and elsewhere. More will likely be defined in future versions of Python. Any use of __*__ names, in any context, that does not follow explicitly documented use, is subject to breakage without warning.

__*

类的私有名称。这种名称在类定义中使用时，会以一种混合形式重写以避免在基类及派生类的 “私有” 属性之间出现名称冲突。参见 标识符（名称）。

2.4. 字面值

字面值用于表示一些内置类型的常量。

2.4.1. 字符串和字节串字面值

字符串字面值由以下词法定义进行描述:

stringliteral   ::=  [stringprefix](shortstring | longstring)
stringprefix    ::=  "r" | "u" | "R" | "U" | "f" | "F"
                     | "fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | "Rf" | "RF"
shortstring     ::=  "'" shortstringitem* "'" | '"' shortstringitem* '"'
longstring      ::=  "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
shortstringitem ::=  shortstringchar | stringescapeseq
longstringitem  ::=  longstringchar | stringescapeseq
shortstringchar ::=  <any source character except "\" or newline or the quote>
longstringchar  ::=  <any source character except "\">
stringescapeseq ::=  "\" <any source character>

bytesliteral   ::=  bytesprefix(shortbytes | longbytes)
bytesprefix    ::=  "b" | "B" | "br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB"
shortbytes     ::=  "'" shortbytesitem* "'" | '"' shortbytesitem* '"'
longbytes      ::=  "'''" longbytesitem* "'''" | '"""' longbytesitem* '"""'
shortbytesitem ::=  shortbyteschar | bytesescapeseq
longbytesitem  ::=  longbyteschar | bytesescapeseq
shortbyteschar ::=  <any ASCII character except "\" or newline or the quote>
longbyteschar  ::=  <any ASCII character except "\">
bytesescapeseq ::=  "\" <any ASCII character>

这些条目中未提及的一个语法限制是 stringprefix 或 bytesprefix 与字面值的剩余部分之间不允许有空白。源字符集是由编码声明定义的；如果源文件中没有编码声明则默认为 UTF-8；参见 编码声明。

自然语言描述: 两种字面值都可以用成对单引号 (') 或双引号 (") 来标示首尾。它们也可以用成对的连续三个单引号或双引号来标示首尾 (这通常被称为 三引号字符串)。反斜杠 (\) 字符被用来对特殊含义的字符进行转义，例如换行，反斜杠本身或是引号等字符。

字节串字面值总是带有前缀 'b' 或 'B'；它们生成 bytes 类型而非 str 类型的实例。它们只能包含 ASCII 字符；字节对应数值在128及以上必须以转义形式来表示。

字符串和字节串字面值都可以带有前缀 'r' 或 'R'；这种字符串被称为 原始字符串 其中的反斜杠会被当作其本身的字面字符来处理。因此在原始字符串字面值中，'\U' 和 '\u' 转义形式不会被特殊对待。由于 Python 2.x 的原始统一码字面值的特性与 Python 3.x 不一致，'ur' 语法已不再被支持。

3.3 新版功能: 新加入了表示原始字节串的 'rb' 前缀，与 'br' 的意义相同。

3.3 新版功能: 对旧式统一码字面值 (u'value') 的支持被重新引入以简化 Python 2.x 和 3.x 代码库的同步维护。详情见 PEP 414 [https://www.python.org/dev/peps/pep-0414]。

包含 'f' 或 'F' 前缀的字符串字面值称为 格式化字符串字面值；参见 格式化字符串字面值。'f' 可与 'r' 连用，但不能与 'b' 或 'u' 连用，因此存在原始格式化字符串，但不存在格式化字节串字面值。

在三引号字面值中，允许存在未经转义的换行和引号 (并原样保留)，除非是未经转义的连续三引号，这标示着字面值的结束。 (“引号” 是用来标示字面值的字符，即 ' 或 "。)

除非带有 'r' 或 'R' 前缀，字符串和字节串字面值中的转义序列会基于类似标准 C 中的转义规则来解读。可用的转义序列如下:

转义序列

含义

注释

\newline

反斜杠加换行全被忽略

\\

反斜杠 (\)

\'

单引号 (')

\"

双引号 (")

\a

ASCII 响铃 (BEL)

\b

ASCII 退格 (BS)

\f

ASCII 进纸 (FF)

\n

ASCII 换行 (LF)

\r

ASCII 回车 (CR)

\t

ASCII 水平制表 (TAB)

\v

ASCII 垂直制表 (VT)

\ooo

八进制数 ooo 码位的字符

(1,3)

\xhh

十六进制数 hh 码位的字符

(2,3)

仅在字符串字面值中可用的转义序列如下:

转义序列

含义

注释

\N{name}

Unicode 数据库中名称为 name 的字符

(4)

\uxxxx

16位十六进制数 xxxx 码位的字符

(5)

\Uxxxxxxxx

32位16进制数 xxxxxxxx 码位的字符

(6)

注释:

与标准 C 一致，接受最多三个八进制数码。

与标准 C 不同，要求必须为两个十六进制数码。

在字节串字面值中，十六进制数和八进制数转义码以相应数值代表每个字节。在字符串字面值中，这些转义码以相应数值代表每个 Unicode 字符。

在 3.3 版更改: 加入了对别名 1 的支持。

要求必须为四个十六进制数码。

此方式可用来表示任意 Unicode 字符。要求必须为八个十六进制数码。

与标准 C 不同，所有无法识别的转义序列将原样保留在字符串中，也就是说，反斜杠会在结果中保留。(这种方式在调试时很有用: 如果输错了一个转义序列，更容易在输出结果中识别错误。) 另外要注意的一个关键点是：专用于字符串字面值中的转义序列如果在字节串字面值中出现，会被归类为无法识别的转义序列。

在 3.6 版更改: 无法识别的转义序列会引发 DeprecationWarning。从未来某个 Python 版本开始将会引发 SyntaxError。

即使在原始字面值中，引号也可以加上反斜杠转义符，但反斜杠会保留在输出结果中；例如 r"\"" 是一个有效的字符串字面值，包含两个字符: 一个反斜杠和一个双引号；而 r"\" 不是一个有效的字符串字面值 (即便是原始字符串也不能以奇数个反斜杠结束)。特别地，一个原始字面值不能以单个反斜杠结束 (因为此反斜杠会转义其后的引号字符)。还要注意一个反斜杠加一个换行在字面值中会被解释为两个字符，而 不是 一个连续行。

2.4.2. 字符串字面值拼接

多个相邻的字符串或字节串字面值 (以空白符分隔)，所用的引号可以彼此不同，其含义等同于全部拼接为一体。因此， "hello" 'world' 等同于 "helloworld"。此特性可以减少反斜杠的使用，以方便地将很长的字符串分成多个物理行，甚至每部分字符串还可分别加注释，例如:

re.compile("[A-Za-z_]"       # letter or underscore
           "[A-Za-z0-9_]*"   # letter, digit or underscore
          )

注意此特性是在句法层面定义的，但是在编译时实现。在运行时拼接字符串表达式必须使用 ‘+’ 运算符。还要注意字面值拼接时每个部分可以使用不同的引号风格 (甚至混合使用原始字符串和三引号字符串)，格式化字符串字面值也可与普通字符串字面值拼接。

2.4.3. 格式化字符串字面值

3.6 新版功能.

格式化字符串字面值 或称 f-string 是带有 'f' 或 'F' 前缀的字符串字面值。这种字符串可包含替换字段，即以 {} 标示的表达式。而其他字符串字面值总是一个常量，格式化字符串字面值实际上是会在运行时被求值的表达式。

转义序列会像在普通字符串字面值中一样被解码 (除非字面值还被标示为原始字符串)。解码之后，字符串内容所用的语法如下:

f_string          ::=  (literal_char | "{{" | "}}" | replacement_field)*
replacement_field ::=  "{" f_expression ["!" conversion] [":" format_spec] "}"
f_expression      ::=  (conditional_expression | "*" or_expr)
                         ("," conditional_expression | "," "*" or_expr)* [","]
                       | yield_expression
conversion        ::=  "s" | "r" | "a"
format_spec       ::=  (literal_char | NULL | replacement_field)*
literal_char      ::=  <any code point except "{", "}" or NULL>

字符串在花括号以外的部分按其字面值处理，除了双重花括号 '{{' 或 '}}' 会被替换为相应的单个花括号。单个左花括号 '{' 标示一个替换字段，它以一个 Python 表达式打头，表达式之后可能有一个以叹号 '!' 标示的转换字段。之后还可能带有一个以冒号 ':' 标示的格式说明符。替换字段以一个右花括号 '}' 作为结束。

格式化字符串字面值中的表达式会被当作正常的包含在圆括号中的 Python 表达式一样处理，但有少数例外。空表达式不被允许，lambda 表达式必须显式地加上圆括号。替换表达式可以包含换行 (例如在三引号字符串中)，但是不能包含注释。每个表达式会在格式化字符串字面值所包含的位置按照从左至右的顺序被求值。

An await expression and comprehensions containing an async for clause are illegal in the expression in formatted string literals. (The reason is a problem with the implementation — this restriction is lifted in Python 3.7).

如果指定了转换符，表达式的求值结果会先转换再格式化。转换符 '!s' 即对结果调用 str()，'!r' 为调用 repr()，而 '!a' 为调用 ascii()。

在此之后结果会使用 format() 协议进行格式化。格式说明符会被传入表达式或转换结果的 __format__() 方法。如果省略格式说明符则会传入一个空字符串。然后格式化结果会包含在整个字符串最终的值当中。

顶层的格式说明符可以包含有嵌套的替换字段。这些嵌套字段也可以包含有自己的转换字段和 格式说明符，但不可再包含更深层嵌套的替换字段。这里的 格式说明符微型语言 与字符串 .format() 方法所使用的相同。

格式化字符串字面值可以拼接，但是一个替换字段不能拆分到多个字面值。

一些格式化字符串字面值的示例:

>>> name = "Fred"
>>> f"He said his name is {name!r}."
"He said his name is 'Fred'."
>>> f"He said his name is {repr(name)}."  # repr() is equivalent to !r
"He said his name is 'Fred'."
>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal("12.34567")
>>> f"result: {value:{width}.{precision}}"  # nested fields
'result:      12.35'
>>> today = datetime(year=2017, month=1, day=27)
>>> f"{today:%B %d, %Y}"  # using date format specifier
'January 27, 2017'
>>> number = 1024
>>> f"{number:#0x}"  # using integer format specifier
'0x400'

与正常字符串字面值采用相同语法导致的一个结果就是替换字段中的字符不能与外部的格式化字符串字面值所用的引号相冲突:

f"abc {a["x"]} def"    # error: outer string literal ended prematurely
f"abc {a['x']} def"    # workaround: use different quoting

格式表达式中不允许有反斜杠，这会引发错误:

f"newline: {ord('\n')}"  # raises SyntaxError

想包含需要用反斜杠转义的值，可以创建一个临时变量。

>>> newline = ord('\n')
>>> f"newline: {newline}"
'newline: 10'

格式化字符串字面值不可用作文档字符串，即便其中没有包含表达式。

>>> def foo():
...     f"Not a docstring"
...
>>> foo.__doc__ is None
True

另请参见 PEP 498 [https://www.python.org/dev/peps/pep-0498] 了解加入格式化字符串字面值的提议，以及使用了相关的格式字符串机制的 str.format()。

2.4.4. 数字字面值

数字字面值有三种类型: 整型数、浮点数和虚数。没有专门的复数字面值 (复数可由一个实数加一个虚数合成)。

注意数字字面值并不包含正负号；-1 这样的负数实际上是由单目运算符 ‘-‘ 和字面值 1 合成的。

2.4.5. 整型数字面值

整型数字面值由以下词法定义进行描述:

integer      ::=  decinteger | bininteger | octinteger | hexinteger
decinteger   ::=  nonzerodigit (["_"] digit)* | "0"+ (["_"] "0")*
bininteger   ::=  "0" ("b" | "B") (["_"] bindigit)+
octinteger   ::=  "0" ("o" | "O") (["_"] octdigit)+
hexinteger   ::=  "0" ("x" | "X") (["_"] hexdigit)+
nonzerodigit ::=  "1"..."9"
digit        ::=  "0"..."9"
bindigit     ::=  "0" | "1"
octdigit     ::=  "0"..."7"
hexdigit     ::=  digit | "a"..."f" | "A"..."F"

整型数字面值的长度没有限制，能一直大到占满可用内存。

在确定数字大小时字面值中的下划线会被忽略。它们可用来将数码分组以提高可读性。一个下划线可放在数码之间，也可放在基数说明符例如 0x 之后。

注意非零的十进制数开头不允许有额外的零。这是为了避免与 Python 在版本 3.0 之前所使用的 C 风格八进制字面值相混淆。

一些整型数字面值的示例如下:

7     2147483647                        0o177    0b100110111
3     79228162514264337593543950336     0o377    0xdeadbeef
      100_000_000_000                   0b_1110_0101

在 3.6 版更改: 允许在字面值中使用下划线进行分组。

2.4.6. 浮点数字面值

浮点数字面值由以下词法定义进行描述:

floatnumber   ::=  pointfloat | exponentfloat
pointfloat    ::=  [digitpart] fraction | digitpart "."
exponentfloat ::=  (digitpart | pointfloat) exponent
digitpart     ::=  digit (["_"] digit)*
fraction      ::=  "." digitpart
exponent      ::=  ("e" | "E") ["+" | "-"] digitpart

注意整型数部分和指数部分在解析时总是以 10 为基数。例如，077e010 是合法的，且表示的数值与 77e10 相同。浮点数字面值允许的范围依赖于具体实现。对于整型数字面值，支持以下划线进行分组。

一些浮点数字面值的示例如下:

3.14    10.    .001    1e100    3.14e-10    0e0    3.14_15_93

在 3.6 版更改: 允许在字面值中使用下划线进行分组。

2.4.7. 虚数字面值

虚数字面值由以下词法定义进行描述:

imagnumber ::=  (floatnumber | digitpart) ("j" | "J")

一个虚数字面值将生成一个实部为 0.0 的复数。复数是以一对浮点数来表示的，它们的取值范围相同。要创建一个实部不为零的复数，就加上一个浮点数，例如 (3+4j)。一些虚数字面值的示例如下:

3.14j   10.j    10j     .001j   1e100j   3.14e-10j   3.14_15_93j

2.5. 运算符

以下形符属于运算符:

+       -       *       **      /       //      %      @
<<      >>      &       |       ^       ~
<       >       <=      >=      ==      !=

2.6. 分隔符

以下形符在语法中归类为分隔符:

(       )       [       ]       {       }
,       :       .       ;       @       =       ->
+=      -=      *=      /=      //=     %=      @=
&=      |=      ^=      >>=     <<=     **=

句点也可出现于浮点数和虚数字面值中。连续三个句点有表示一个省略符的特殊含义。以上列表的后半部分为增强赋值操作符，在词法中作为分隔符，但也起到运算作用。

以下可打印 ASCII 字符作为其他形符的组成部分时具有特殊含义，或是对词法分析器有重要意义:

'       "       #       \

以下可打印 ASCII 字符不在 Python 词法中使用。如果出现于字符串字面值和注释之外将无条件地引发错误:

$       ?       `

备注

1

http://www.unicode.org/Public/9.0.0/ucd/NameAliases.txt

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

3. 数据模型

3.1. 对象、值与类型

Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects. (In a sense, and in conformance to Von Neumann’s model of a “stored program computer,” code is also represented by objects.)

每个对象都有各自的编号、类型和值。一个对象被创建后，它的 编号 就绝不会改变；你可以将其理解为该对象在内存中的地址。 ‘is’ 运算符可以比较两个对象的编号是否相同；id() 函数能返回一个代表其编号的整型数。

CPython implementation detail: 在 CPython 中，id(x) 就是存放 x 的内存的地址。

对象的类型决定该对象所支持的操作 (例如 “对象是否有长度属性？”) 并且定义了该类型的对象可能的取值。type() 函数能返回一个对象的类型 (类型本身也是对象)。与编号一样，一个对象的 类型 也是不可改变的。1

有些对象的 值 可以改变。值可以改变的对象被称为 可变的；值不可以改变的对象就被称为 不可变的。(一个不可变容器对象如果包含对可变对象的引用，当后者的值改变时，前者的值也会改变；但是该容器仍属于不可变对象，因为它所包含的对象集是不会改变的。因此，不可变并不严格等同于值不能改变，实际含义要更微妙。) 一个对象的可变性是由其类型决定的；例如，数字、字符串和元组是不可变的，而字典和列表是可变的。

对象绝不会被显式地销毁；然而，当无法访问时它们可能会被作为垃圾回收。允许具体的实现推迟垃圾回收或完全省略此机制 — 如何实现垃圾回收是实现的质量问题，只要可访问的对象不会被回收即可。

CPython implementation detail: CPython 目前使用带有 (可选) 延迟检测循环链接垃圾的引用计数方案，会在对象不可访问时立即回收其中的大部分，但不保证回收包含循环引用的垃圾。请查看 gc 模块的文档了解如何控制循环垃圾的收集相关信息。其他实现会有不同的行为方式，CPython 现有方式也可能改变。不要依赖不可访问对象的立即终结机制 (所以你应当总是显式地关闭文件)。

注意：使用实现的跟踪或调试功能可能令正常情况下会被回收的对象继续存活。还要注意通过 ‘try…except’ 语句捕捉异常也可能令对象保持存活。

有些对象包含对 “外部” 资源的引用，例如打开文件或窗口。当对象被作为垃圾回收时这些资源也应该会被释放，但由于垃圾回收并不确保发生，这些对象还提供了明确地释放外部资源的操作，通常为一个 close() 方法。强烈推荐在程序中显式关闭此类对象。’try…finally’ 语句和 ‘with’ 语句提供了进行此种操作的更便捷方式。

有些对象包含对其他对象的引用；它们被称为 容器。容器的例子有元组、列表和字典等。这些引用是容器对象值的组成部分。在多数情况下，当谈论一个容器的值时，我们是指所包含对象的值而不是其编号；但是，当我们谈论一个容器的可变性时，则仅指其直接包含的对象的编号。因此，如果一个不可变容器 (例如元组) 包含对一个可变对象的引用，则当该可变对象被改变时容器的值也会改变。

类型会影响对象行为的几乎所有方面。甚至对象编号的重要性也在某种程度上受到影响: 对于不可变类型，会得出新值的运算实际上会返回对相同类型和取值的任一现有对象的引用，而对于可变类型来说这是不允许的。例如在 a = 1; b = 1 之后，a 和 b 可能会也可能不会指向同一个值为一的对象，这取决于具体实现，但是在 c = []; d = [] 之后，c 和 d 保证会指向两个不同、单独的新建空列表。(请注意 c = d = [] 则是将同一个对象赋值给 c 和 d。)

3.2. 标准类型层级结构

以下是 Python 内置类型的列表。扩展模块 (具体实现会以 C, Java 或其他语言编写) 可以定义更多的类型。未来版本的 Python 可能会加入更多的类型 (例如有理数、高效存储的整型数组等等)，不过新增类型往往都是通过标准库来提供的。

以下部分类型的描述中包含有 ‘特殊属性列表’ 段落。这些属性提供对具体实现的访问而非通常使用。它们的定义在未来可能会改变。

None

此类型只有一种取值。是一个具有此值的单独对象。此对象通过内置名称 None 访问。在许多情况下它被用来表示空值，例如未显式指明返回值的函数将返回 None。它的逻辑值为假。

NotImplemented

此类型只有一种取值。是一个具有此值的单独对象。此对象通过内置名称 NotImplemented 访问。数值方法和丰富比较方法如未实现指定运算符表示的运算则应返回此值。(解释器会根据指定运算符继续尝试反向运算或其他回退操作)。它的逻辑值为真。

详情参见 实现算术运算。

Ellipsis

此类型只有一种取值。是一个具有此值的单独对象。此对象通过字面值 ... 或内置名称 Ellipsis 访问。它的逻辑值为真。

numbers.Number

此类对象由数字字面值创建，并会被作为算术运算符和算术内置函数的返回结果。数字对象是不可变的；一旦创建其值就不再改变。Python 中的数字当然非常类似数学中的数字，但也受限于计算机中的数字表示方法。

Python 区分整型数、浮点型数和复数:

numbers.Integral

此类对象表示数学中整数集合的成员 (包括正数和负数)。

整型数可细分为两种类型:

整型 (int)

此类对象表示任意大小的数字，仅受限于可用的内存 (包括虚拟内存)。在变换和掩码运算中会以二进制表示，负数会以 2 的补码表示，看起来像是符号位向左延伸补满空位。

布尔型 (bool)

此类对象表示逻辑值 False 和 True。代表 False 和 True 值的两个对象是唯二的布尔对象。布尔类型是整型的子类型，两个布尔值在各种场合的行为分别类似于数值 0 和 1，例外情况只有在转换为字符串时分别返回字符串 "False" 或 "True"。

整型数表示规则的目的是在涉及负整型数的变换和掩码运算时提供最为合理的解释。

numbers.Real (float)

此类对象表示机器级的双精度浮点数。其所接受的取值范围和溢出处理将受制于底层的机器架构 (以及 C 或 Java 实现)。Python 不支持单精度浮点数；支持后者通常的理由是节省处理器和内存消耗，但这点节省相对于在 Python 中使用对象的开销来说太过微不足道，因此没有理由包含两种浮点数而令该语言变得复杂。

numbers.Complex (complex)

此类对象以一对机器级的双精度浮点数来表示复数值。有关浮点数的附带规则对其同样有效。一个复数值 z 的实部和虚部可通过只读属性 z.real 和 z.imag 来获取。

序列

此类对象表示以非负整数作为索引的有限有序集。内置函数 len() 可返回一个序列的条目数量。当一个序列的长度为 n 时，索引集包含数字 0, 1, …, n-1。序列 a 的条目 i 可通过 a[i] 选择。

序列还支持切片: a[i:j] 选择索引号为 k 的所有条目，i <= k < j。当用作表达式时，序列的切片就是一个与序列类型相同的新序列。新序列的索引还是从 0 开始。

有些序列还支持带有第三个 “step” 形参的 “扩展切片”: a[i:j:k] 选择 a 中索引号为 x 的所有条目，x = i + n*k, n >= 0 且 i <= x < j。

序列可根据其可变性来加以区分:

不可变序列

不可变序列类型的对象一旦创建就不能再改变。(如果对象包含对其他对象的引用，其中的可变对象就是可以改变的；但是，一个不可变对象所直接引用的对象集是不能改变的。)

以下类型属于不可变对象:

字符串

字符串是由 Unicode 码位值组成的序列。范围在 U+0000 - U+10FFFF 之内的所有码位值都可在字符串中使用。Python 没有 char 类型；而是将字符串中的每个码位表示为一个长度为 1 的字符串对象。内置函数 ord() 可将一个码位由字符串形式转换成一个范围在 0 - 10FFFF 之内的整型数；chr() 可将一个范围在 0 - 10FFFF 之内的整型数转换为长度为 1 的对应字符串对象。str.encode() 可以使用指定的文本编码将 str 转换为 bytes，而 bytes.decode() 则可以实现反向的解码。

元组

一个元组中的条目可以是任意 Python 对象。包含两个或以上条目的元组由逗号分隔的表达式构成。只有一个条目的元组 (‘单项元组’) 可通过在表达式后加一个逗号来构成 (一个表达式本身不能创建为元组，因为圆括号要用来设置表达式分组)。一个空元组可通过一对内容为空的圆括号创建。

字节串

字节串对象是不可变的数组。其中每个条目都是一个 8 位字节，以取值范围 0 <= x < 256 的整型数表示。字节串字面值 (例如 b'abc') 和内置的 bytes() 构造器可被用来创建字节串对象。字节串对象还可以通过 decode() 方法解码为字符串。

可变序列

可变序列在被创建后仍可被改变。下标和切片标注可被用作赋值和 del (删除) 语句的目标。

目前有两种内生可变序列类型:

列表

列表中的条目可以是任意 Python 对象。列表由用方括号括起并由逗号分隔的多个表达式构成。(注意创建长度为 0 或 1 的列表无需使用特殊规则。)

字节数组

字节数组对象属于可变数组。可以通过内置的 bytearray() 构造器来创建。除了是可变的 (因而也是不可哈希的)，在其他方面字节数组提供的接口和功能都与不可变的 bytes 对象一致。

扩展模块 array 提供了一个额外的可变序列类型示例，collections 模块也是如此。

集合类型

此类对象表示由不重复且不可变对象组成的无序且有限的集合。因此它们不能通过下标来索引。但是它们可被迭代，也可用内置函数 len() 返回集合中的条目数。集合常见的用处是快速成员检测，去除序列中的重复项，以及进行交、并、差和对称差等数学运算。

对于集合元素所采用的不可变规则与字典的键相同。注意数字类型遵循正常的数字比较规则: 如果两个数字相等 (例如 1 和 1.0)，则同一集合中只能包含其中一个。

目前有两种内生集合类型:

集合

此类对象表示可变集合。它们可通过内置的 set() 构造器创建，并且创建之后可以通过方法进行修改，例如 add()。

冻结集合

此类对象表示不可变集合。它们可通过内置的 frozenset() 构造器创建。由于 frozenset 对象不可变且 hashable，它可以被用作另一个集合的元素或是字典的键。

映射

此类对象表示由任意索引集合所索引的对象的集合。通过下标 a[k] 可在映射 a 中选择索引为 k 的条目；这可以在表达式中使用，也可作为赋值或 del 语句的目标。内置函数 len() 可返回一个映射中的条目数。

目前只有一种内生映射类型:

字典

此类对象表示由几乎任意值作为索引的有限个对象的集合。不可作为键的值类型只有包含列表或字典或其他可变类型，通过值而非对象编号进行比较的值，其原因在于高效的字典实现需要使用键的哈希值以保持一致性。用作键的数字类型遵循正常的数字比较规则: 如果两个数字相等 (例如 1 和 1.0) 则它们均可来用来索引同一个字典条目。

字典是可变的；它们可通过 {...} 标注来创建 (参见 字典显示 小节)。

扩展模块 dbm.ndbm 和 dbm.gnu 提供了额外的映射类型示例，collections 模块也是如此。

可调用类型

此类型可以被应用于函数调用操作 (参见 调用 小节):

用户定义函数

用户定义函数对象可通过函数定义来创建 (参见 函数定义 小节)。它被调用时应附带一个参数列表，其中包含的条目应与函数所定义的形参列表一致。

特殊属性:

属性

含义

__doc__

The function’s documentation string, or None if unavailable; not inherited by subclasses

可写

__name__

The function’s name

可写

__qualname__

The function’s qualified name

3.3 新版功能.

可写

__module__

该函数所属模块的名称，没有则为 None。

可写

__defaults__

A tuple containing default argument values for those arguments that have defaults, or None if no arguments have a default value

可写

__code__

表示编译后的函数体的代码对象。

可写

__globals__

对存放该函数中全局变量的字典的引用 — 函数所属模块的全局命名空间。

只读

__dict__

命名空间支持的函数属性。

可写

__closure__

None or a tuple of cells that contain bindings for the function’s free variables.

只读

__annotations__

包含参数标注的字典。字典的键是参数名，如存在返回标注则为 'return'。

可写

__kwdefaults__

仅包含关键字参数默认值的字典。

可写

大部分标有 “Writable” 的属性均会检查赋值的类型。

函数对象也支持获取和设置任意属性，例如这可以被用来给函数附加元数据。使用正规的属性点号标注获取和设置此类属性。注意当前实现仅支持用户定义函数属性。未来可能会增加支持内置函数属性。

有关函数定义的额外信息可以从其代码对象中提取；参见下文对内部类型的描述。

实例方法

实例方法用于结合类、类实例和任何可调用对象 (通常为用户定义函数)。

特殊的只读属性: __self__ 为类实例对象本身，__func__ 为函数对象；__doc__ 为方法的文档 (与 __func__.__doc__ 作用相同)；__name__ 为方法名称 (与 __func__.__name__ 作用相同)；__module__ 为方法所属模块的名称，没有则为 None。

方法还支持获取 (但不能设置) 下层函数对象的任意函数属性。

用户定义方法对象可在获取一个类的属性时被创建 (也可能通过该类的一个实例)，如果该属性为用户定义函数对象或类方法对象。

当通过从类实例获取一个用户定义函数对象的方式创建一个实例方法对象时，类实例对象的 __self__ 属性即为该实例，并会绑定方法对象。该新建方法的 __func__ 属性就是原来的函数对象。

当通过从类或实例获取另一个方法对象的方式创建一个用户定义方法对象时，其行为将等同于一个函数对象，例外的只有新实例的 __func__ 属性将不是原来的方法对象，而是其 __func__ 属性。

当通过从类或实例获取一个类方法对象的方式创建一个实例对象时，实例对象的 __self__ 属性为该类本身，其 __func__ 属性为类方法对应的下层函数对象。

当一个实例方法对象被调用时，会调用对应的下层函数 (__func__)，并将类实例 (__self__) 插入参数列表的开头。例如，当 C 是一个包含了 f() 函数定义的类，而 x 是 C 的一个实例，则调用 x.f(1) 就等同于调用 C.f(x, 1)。

当一个实例方法对象是衍生自一个类方法对象时，保存在 __self__ 中的 “类实例” 实际上会是该类本身，因此无论是调用 x.f(1) 还是 C.f(1) 都等同于调用 f(C,1)，其中 f 为对应的下层函数。

请注意从函数对象到实例方法对象的变换会在每一次从实例获取属性时发生。在某些情况下，一种高效的优化方式是将属性赋值给一个本地变量并调用该本地变量。还要注意这样的变换只发生于用户定义函数；其他可调用对象 (以及所有不可调用对象) 在被获取时都不会发生变换。还有一个需要关注的要点是作为一个类实例属性的用户定义函数不会被转换为绑定方法；这样的变换 仅当 函数是类属性时才会发生。

生成器函数

A function or method which uses the yield statement (see section The yield statement) is called a generator function. Such a function, when called, always returns an iterator object which can be used to execute the body of the function: calling the iterator’s iterator.__next__() method will cause the function to execute until it provides a value using the yield statement. When the function executes a return statement or falls off the end, a StopIteration exception is raised and the iterator will have reached the end of the set of values to be returned.

协程函数

使用 async def 来定义的函数或方法就被称为 协程函数。这样的函数在被调用时会返回一个 coroutine 对象。它可能包含 await 表达式以及 async with 和 async for 语句。详情可参见 协程对象 一节。

异步生成器函数

使用 async def 来定义并包含 yield 语句的函数或方法就被称为 异步生成器函数。这样的函数在被调用时会返回一个异步迭代器对象，该对象可在 async for 语句中用来执行函数体。

调用异步迭代器的 aiterator.__anext__() 方法将会返回一个 awaitable，此对象会在被等待时执行直到使用 yield 表达式输出一个值。当函数执行时到空的 return 语句或是最后一条语句时，将会引发 StopAsyncIteration 异常，异步迭代器也会到达要输出的值集合的末尾。

内置函数

内置函数对象是对于 C 函数的外部封装。内置函数的例子包括 len() 和 math.sin() (math 是一个标准内置模块)。内置函数参数的数量和类型由 C 函数决定。特殊的只读属性: __doc__ 是函数的文档字符串，如果没有则为 None; __name__ 是函数的名称; __self__ 设定为 None (参见下一条目); __module__ 是函数所属模块的名称，如果没有则为 None。

内置方法

此类型实际上是内置函数的另一种形式，只不过还包含了一个传入 C 函数的对象作为隐式的额外参数。内置方法的一个例子是 alist.append()，其中 alist 为一个列表对象。在此示例中，特殊的只读属性 __self__ 会被设为 alist 所标记的对象。

类

类是可调用的。此种对象通常是作为“工厂”来创建自身的实例，类也可以有重载 __new__() 的变体类型。调用的参数会传给 __new__()，而且通常也会传给 __init__() 来初始化新的实例。

类实例

任意类的实例通过在所属类中定义 __call__() 方法即能成为可调用的对象。

模块

Modules are a basic organizational unit of Python code, and are created by the import system as invoked either by the import statement (see import), or by calling functions such as importlib.import_module() and built-in __import__(). A module object has a namespace implemented by a dictionary object (this is the dictionary referenced by the __globals__ attribute of functions defined in the module). Attribute references are translated to lookups in this dictionary, e.g., m.x is equivalent to m.__dict__["x"]. A module object does not contain the code object used to initialize the module (since it isn’t needed once the initialization is done).

属性赋值会更新模块的命名空间字典，例如 m.x = 1 等同于 m.__dict__["x"] = 1。

预定义的 (可写) 属性: __name__ 为模块的名称; __doc__ 为模块的文档字符串，如果没有则为 None; __annotations__ (可选) 为一个包含 变量标注 的字典，它是在模块体执行时获取的; __file__ 是模块对应的被加载文件的路径名，如果它是加载自一个文件的话。某些类型的模块可能没有 __file__ 属性，例如 C 模块是静态链接到解释器内部的; 对于从一个共享库动态加载的扩展模块来说该属性为该共享库文件的路径名。

特殊的只读属性: __dict__ 为以字典对象表示的模块命名空间。

CPython implementation detail: 由于 CPython 清理模块字典的设定，当模块离开作用域时模块字典将会被清理，即使该字典还有活动的引用。想避免此问题，可复制该字典或保持模块状态以直接使用其字典。

自定义类

自定义类这种类型一般通过类定义来创建 (参见 类定义 一节)。每个类都有通过一个字典对象实现的独立命名空间。类属性引用会被转化为在此字典中查找，例如 C.x 会被转化为 C.__dict__["x"] (不过也存在一些钩子对象以允许其他定位属性的方式)。当未在其中发现某个属性名称时，会继续在基类中查找。这种基类查找使用 C3 方法解析顺序，即使存在 ‘钻石形’ 继承结构即有多条继承路径连到一个共同祖先也能保持正确的行为。有关 Python 使用的 C3 MRO 的详情可查看配合 2.3 版发布的文档 https://www.python.org/download/releases/2.3/mro/.

当一个类属性引用 (假设类名为 C) 会产生一个类方法对象时，它将转化为一个 __self__ 属性为 C 的实例方法对象。当其会产生一个静态方法对象时，它将转化为该静态方法对象所封装的对象。从类的 __dict__ 所包含内容以外获取属性的其他方式请参看 实现描述器 一节。

类属性赋值会更新类的字典，但不会更新基类的字典。

类对象可被调用 (见上文) 以产生一个类实例 (见下文)。

特殊属性: __name__ 为类的名称; __module__ 为类所在模块的名称; __dict__ 为包含类命名空间的字典; __bases__ 为包含基类的元组，按其在基类列表中的出现顺序排列; __doc__ 为类的文档字符串，如果没有则为 None; __annotations__ (可选) 为一个包含 变量标注 的字典，它是在类体执行时获取的。

类实例

类实例可通过调用类对象来创建 (见上文)。每个类实例都有通过一个字典对象实现的独立命名空间，属性引用会首先在此字典中查找。当未在其中发现某个属性，而实例对应的类中有该属性时，会继续在类属性中查找。如果找到的类属性为一个用户定义函数对象，它会被转化为实例方法对象，其 __self__ 属性即该实例。静态方法和类方法对象也会被转化；参见上文 “Classes” 一节。要了解其他通过类实例来获取相应类属性的方式可参见 实现描述器 一节，这样得到的属性可能与实际存放于类的 __dict__ 中的对象不同。如果未找到类属性，而对象对应的类具有 __getattr__() 方法，则会调用该方法来满足查找要求。

属性赋值和删除会更新实例的字典，但不会更新对应类的字典。如果类具有 __setattr__() 或 __delattr__() 方法，则将调用方法而不再直接更新实例的字典。

如果类实例具有某些特殊名称的方法，就可以伪装为数字、序列或映射。参见 特殊方法名称 一节。

特殊属性: __dict__ 为属性字典; __class__ 为实例对应的类。

I/O 对象 (或称文件对象)

file object 表示一个打开的文件。有多种快捷方式可用来创建文件对象: open() 内置函数，以及 os.popen(), os.fdopen() 和 socket 对象的 makefile() 方法 (还可能使用某些扩展模块所提供的其他函数或方法)。

sys.stdin, sys.stdout 和 sys.stderr 会初始化为对应于解释器标准输入、输出和错误流的文件对象；它们都会以文本模式打开，因此都遵循 io.TextIOBase 抽象类所定义的接口。

内部类型

某些由解释器内部使用的类型也被暴露给用户。它们的定义可能随未来解释器版本的更新而变化，为内容完整起见在此处一并介绍。

代码对象

代码对象表示 编译为字节的 可执行 Python 代码，或称 bytecode。代码对象和函数对象的区别在于函数对象包含对函数全局对象 (函数所属的模块) 的显式引用，而代码对象不包含上下文；而且默认参数值会存放于函数对象而不是代码对象内 (因为它们表示在运行时算出的值)。与函数对象不同，代码对象不可变，也不包含对可变对象的引用 (不论是直接还是间接)。

Special read-only attributes: co_name gives the function name; co_argcount is the number of positional arguments (including arguments with default values); co_nlocals is the number of local variables used by the function (including arguments); co_varnames is a tuple containing the names of the local variables (starting with the argument names); co_cellvars is a tuple containing the names of local variables that are referenced by nested functions; co_freevars is a tuple containing the names of free variables; co_code is a string representing the sequence of bytecode instructions; co_consts is a tuple containing the literals used by the bytecode; co_names is a tuple containing the names used by the bytecode; co_filename is the filename from which the code was compiled; co_firstlineno is the first line number of the function; co_lnotab is a string encoding the mapping from bytecode offsets to line numbers (for details see the source code of the interpreter); co_stacksize is the required stack size (including local variables); co_flags is an integer encoding a number of flags for the interpreter.

以下是可用于 co_flags 的标志位定义：如果函数使用 *arguments 语法来接受任意数量的位置参数，则 0x04 位被设置；如果函数使用 **keywords 语法来接受任意数量的关键字参数，则 0x08 位被设置；如果函数是一个生成器，则 0x20 位被设置。

未来特性声明 (from __future__ import division) 也使用 co_flags 中的标志位来指明代码对象的编译是否启用特定的特性: 如果函数编译时启用未来除法特性则设置 0x2000 位; 在更早的 Python 版本中则使用 0x10 和 0x1000 位。

co_flags 中的其他位被保留为内部使用。

如果代码对象表示一个函数，co_consts 中的第一项将是函数的文档字符串，如果未定义则为 None。

帧对象

Frame objects represent execution frames. They may occur in traceback objects (see below).

特殊的只读属性: f_back 为前一堆栈帧 (指向调用者)，如是最底层堆栈帧则为 None; f_code 为此帧中所执行的代码对象; f_locals 为用于查找本地变量的字典; f_globals 则用于查找全局变量; f_builtins 用于查找内置 (固有) 名称; f_lasti 给出精确指令 (这是代码对象的字节码字符串的一个索引)。

Special writable attributes: f_trace, if not None, is a function called at the start of each source code line (this is used by the debugger); f_lineno is the current line number of the frame — writing to this from within a trace function jumps to the given line (only for the bottom-most frame). A debugger can implement a Jump command (aka Set Next Statement) by writing to f_lineno.

帧对象支持一个方法:

frame.clear()

此方法清除该帧持有的全部对本地变量的引用。而且如果该帧属于一个生成器，生成器会被完成。这有助于打破包含帧对象的循环引用 (例如当捕获一个异常并保存其回溯在之后使用)。

如果该帧当前正在执行则会引发 RuntimeError。

3.4 新版功能.

回溯对象

Traceback objects represent a stack trace of an exception. A traceback object is created when an exception occurs. When the search for an exception handler unwinds the execution stack, at each unwound level a traceback object is inserted in front of the current traceback. When an exception handler is entered, the stack trace is made available to the program. (See section The try statement.) It is accessible as the third item of the tuple returned by sys.exc_info(). When the program contains no suitable handler, the stack trace is written (nicely formatted) to the standard error stream; if the interpreter is interactive, it is also made available to the user as sys.last_traceback.

Special read-only attributes: tb_next is the next level in the stack trace (towards the frame where the exception occurred), or None if there is no next level; tb_frame points to the execution frame of the current level; tb_lineno gives the line number where the exception occurred; tb_lasti indicates the precise instruction. The line number and last instruction in the traceback may differ from the line number of its frame object if the exception occurred in a try statement with no matching except clause or with a finally clause.

切片对象

切片对象用来表示 __getitem__() 方法用到的切片。 该对象也可使用内置的 slice() 函数来创建。

特殊的只读属性: start 为下界; stop 为上界; step 为步长值; 各值如省略则为 None。这些属性可具有任意类型。

切片对象支持一个方法:

slice.indices(self, length)

此方法接受一个整型参数 length 并计算在切片对象被应用到 length 指定长度的条目序列时切片的相关信息应如何描述。 其返回值为三个整型数组成的元组；这些数分别为切片的 start 和 stop 索引号以及 step 步长值。索引号缺失或越界则按照与正规切片相一致的方式处理。

静态方法对象

静态方法对象提供了一种避免上文所述将函数对象转换为方法对象的方式。静态方法对象为对任意其他对象的封装，通常用来封装用户定义方法对象。当从类或类实例获取一个静态方法对象时，实际返回的对象是封装的对象，它不会被进一步转换。静态方法对象自身不是可调用的，但它们所封装的对象通常都是可调用的。静态方法对象可通过内置的 staticmethod() 构造器来创建。

类方法对象

类方法对象和静态方法一样是对其他对象的封装，会改变从类或类实例获取该对象的方式。类方法对象在此类获取操作中的行为已在上文 “用户定义方法” 一节中描述。类方法对象可通过内置的 classmethod() 构造器来创建。

3.3. 特殊方法名称

一个类可以通过定义具有特殊名称的方法来实现由特殊语法所引发的特定操作 (例如算术运算或下标与切片)。这是 Python 实现 操作符重载 的方式，允许每个类自行定义基于操作符的特定行为。例如，如果一个类定义了名为 __getitem__() 的方法，并且 x 为该类的一个实例，则 x[i] 基本就等同于 type(x).__getitem__(x, i)。除非有说明例外情况，在没有定义适当方法的情况下尝试执行一种操作将引发一个异常 (通常为 AttributeError 或 TypeError)。

将一个特殊方法设为 None 表示对应的操作不可用。例如，如果一个类将 __iter__() 设为 None，则该类就是不可迭代的，因此对其实例调用 iter() 将引发一个 TypeError (而不会回退至 __getitem__()). 2

在实现模拟任何内置类型的类时，很重要的一点是模拟的实现程度对于被模拟对象来说应当是有意义的。例如，提取单个元素的操作对于某些序列来说是适宜的，但提取切片可能就没有意义。(这种情况的一个实例是 W3C 的文档对象模型中的 NodeList 接口。)

3.3.1. 基本定制

object.__new__(cls[, ...])

调用以创建一个 cls 类的新实例。__new__() 是一个静态方法 (因为是特例所以你不需要显式地声明)，它会将所请求实例所属的类作为第一个参数。其余的参数会被传递给对象构造器表达式 (对类的调用)。__new__() 的返回值应为新对象实例 (通常是 cls 的实例)。

典型的实现会附带适宜的参数使用 super().__new__(cls[, ...])，通过超类的 __new__() 方法来创建一个类的新实例，然后根据需要修改新创建的实例再将其返回。

如果 __new__() 返回一个 cls 的实例，则新实例的 __init__() 方法会在之后被执行，例如 __init__(self[, ...])，其中 self 为新实例，其余的参数与被传递给 __new__() 的相同。

如果 __new__() 未返回一个 cls 的实例，则新实例的 __init__() 方法就不会被执行。

__new__() 的目的主要是允许不可变类型的子类 (例如 int, str 或 tuple) 定制实例创建过程。它也常会在自定义元类中被重载以便定制类创建过程。

object.__init__(self[, ...])

在实例 (通过 __new__()) 被创建之后，返回调用者之前调用。其参数与传递给类构造器表达式的参数相同。一个基类如果有 __init__() 方法，则其所派生的类如果也有 __init__() 方法，就必须显式地调用它以确保实例基类部分的正确初始化；例如: super().__init__([args...]).

因为对象是由 __new__() 和 __init__() 协作构造完成的 (由 __new__() 创建，并由 __init__() 定制)，所以 __init__() 返回的值只能是 None，否则会在运行时引发 TypeError。

object.__del__(self)

在实例将被销毁时调用。 这还被称为终结器或析构器（不适当）。 如果一个基类具有 __del__() 方法，则其所派生的类如果也有 __del__() 方法，就必须显式地调用它以确保实例基类部分的正确清除。

__del__() 方法可以 (但不推荐!) 通过创建一个该实例的新引用来推迟其销毁。这被称为对象 重生。__del__() 是否会在重生的对象将被销毁时再次被调用是由具体实现决定的 ；当前的 CPython 实现只会调用一次。

当解释器退出时不会确保为仍然存在的对象调用 __del__() 方法。

注解

del x 并不直接调用 x.__del__() — 前者会将 x 的引用计数减一，而后者仅会在 x 的引用计数变为零时被调用。

CPython implementation detail: It is possible for a reference cycle to prevent the reference count of an object from going to zero. In this case, the cycle will be later detected and deleted by the cyclic garbage collector. A common cause of reference cycles is when an exception has been caught in a local variable. The frame’s locals then reference the exception, which references its own traceback, which references the locals of all frames caught in the traceback.

参见

gc 模块的文档。

警告

由于调用 __del__() 方法时周边状况已不确定，在其执行期间发生的异常将被忽略，改为打印一个警告到 sys.stderr。特别地：

__del__() 可在任意代码被执行时启用，包括来自任意线程的代码。如果 __del__() 需要接受锁或启用其他阻塞资源，可能会发生死锁，例如该资源已被为执行 __del__() 而中断的代码所获取。

__del__() 可以在解释器关闭阶段被执行。因此，它需要访问的全局变量（包含其他模块）可能已被删除或设为 None。Python 会保证先删除模块中名称以单个下划线打头的全局变量再删除其他全局变量；如果已不存在其他对此类全局变量的引用，这有助于确保导入的模块在 __del__() 方法被调用时仍然可用。

object.__repr__(self)

由 repr() 内置函数调用以输出一个对象的“官方”字符串表示。如果可能，这应类似一个有效的 Python 表达式，能被用来重建具有相同取值的对象（只要有适当的环境）。如果这不可能，则应返回形式如 <...some useful description...> 的字符串。返回值必须是一个字符串对象。如果一个类定义了 __repr__() 但未定义 __str__()，则在需要该类的实例的“非正式”字符串表示时也会使用 __repr__()。

此方法通常被用于调试，因此确保其表示的内容包含丰富信息且无歧义是很重要的。

object.__str__(self)

通过 str(object) 以及内置函数 format() 和 print() 调用以生成一个对象的“非正式”或格式良好的字符串表示。返回值必须为一个 字符串 对象。

此方法与 object.__repr__() 的不同点在于 __str__() 并不预期返回一个有效的 Python 表达式：可以使用更方便或更准确的描述信息。

内置类型 object 所定义的默认实现会调用 object.__repr__()。

object.__bytes__(self)

通过 bytes 调用以生成一个对象的字节串表示。这应该返回一个 bytes 对象。

object.__format__(self, format_spec)

Called by the format() built-in function, and by extension, evaluation of formatted string literals and the str.format() method, to produce a “formatted” string representation of an object. The format_spec argument is a string that contains a description of the formatting options desired. The interpretation of the format_spec argument is up to the type implementing __format__(), however most classes will either delegate formatting to one of the built-in types, or use a similar formatting option syntax.

请参看 格式规格迷你语言 了解标准格式化语法的描述。

返回值必须为一个字符串对象。

在 3.4 版更改: object 本身的 __format__ 方法如果被传入任何非空字符，将会引发一个 TypeError。

object.__lt__(self, other)

object.__le__(self, other)

object.__eq__(self, other)

object.__ne__(self, other)

object.__gt__(self, other)

object.__ge__(self, other)

以上这些被称为“富比较”方法。运算符号与方法名称的对应关系如下：x<y 调用 x.__lt__(y)、x<=y 调用 x.__le__(y)、x==y 调用 x.__eq__(y)、x!=y 调用 x.__ne__(y)、x>y 调用 x.__gt__(y)、x>=y 调用 x.__ge__(y)。

如果指定的参数对没有相应的实现，富比较方法可能会返回单例对象 NotImplemented。按照惯例，成功的比较会返回 False 或 True。不过实际上这些方法可以返回任意值，因此如果比较运算符是要用于布尔值判断（例如作为 if 语句的条件），Python 会对返回值调用 bool() 以确定结果为真还是假。

在默认情况下 __ne__() 会委托给 __eq__() 并将结果取反，除非结果为 NotImplemented。比较运算符之间没有其他隐含关系，例如 (x<y or x==y) 为真并不意味着 x<=y。要根据单根运算自动生成排序操作，请参看 functools.total_ordering()。

请查看 __hash__() 的相关段落，了解创建可支持自定义比较运算并可用作字典键的 hashable 对象时要注意的一些事项。

这些方法并没有对调参数版本（在左边参数不支持该操作但右边参数支持时使用）；而是 __lt__() 和 __gt__() 互为对方的反射， __le__() 和 __ge__() 互为对方的反射，而 __eq__() 和 __ne__() 则是它们自己的反射。如果两个操作数的类型不同，且右操作数类型是左操作数类型的直接或间接子类，则优先选择右操作数的反射方法，否则优先选择左操作数的方法。虚拟子类不会被考虑。

object.__hash__(self)

通过内置函数 hash() 调用以对哈希集的成员进行操作，属于哈希集的类型包括 set、frozenset 以及 dict。__hash__() 应该返回一个整数。对象比较结果相同所需的唯一特征属性是其具有相同的哈希值；建议的做法是把参与比较的对象全部组件的哈希值混在一起，即将它们打包为一个元组并对该元组做哈希运算。例如:

def __hash__(self):
    return hash((self.name, self.nick, self.color))

注解

hash() 会从一个对象自定义的 __hash__() 方法返回值中截断为 Py_ssize_t 的大小。通常对 64 位构建为 8 字节，对 32 位构建为 4 字节。如果一个对象的 __hash__() 必须在不同位大小的构建上进行互操作，请确保检查全部所支持构建的宽度。做到这一点的简单方法是使用 python -c "import sys; print(sys.hash_info.width)"。

如果一个类没有定义 __eq__() 方法，那么也不应该定义 __hash__() 操作；如果它定义了 __eq__() 但没有定义 __hash__()，则其实例将不可被用作可哈希集的项。如果一个类定义了可变对象并实现了 __eq__() 方法，则不应该实现 __hash__()，因为可哈希集的实现要求键的哈希集是不可变的（如果对象的哈希值发生改变，它将处于错误的哈希桶中）。

用户定义的类默认带有 __eq__() 和 __hash__() 方法；使用它们与任何对象（自己除外）比较必定不相等，并且 x.__hash__() 会返回一个恰当的值以确保 x == y 同时意味着 x is y 且 hash(x) == hash(y)。

A class that overrides __eq__() and does not define __hash__() will have its __hash__() implicitly set to None. When the __hash__() method of a class is None, instances of the class will raise an appropriate TypeError when a program attempts to retrieve their hash value, and will also be correctly identified as unhashable when checking isinstance(obj, collections.Hashable).

如果一个重载了 __eq__() 的类需要保留来自父类的 __hash__() 实现，则必须通过设置 __hash__ = <ParentClass>.__hash__ 来显式地告知解释器。

If a class that does not override __eq__() wishes to suppress hash support, it should include __hash__ = None in the class definition. A class which defines its own __hash__() that explicitly raises a TypeError would be incorrectly identified as hashable by an isinstance(obj, collections.Hashable) call.

注解

在默认情况下，str、bytes 和 datetime 对象的 __hash__() 值会使用一个不可预知的随机值“加盐”。虽然它们会在一个单独 Python 进程中保持不变，它们的哈希值在重复运行的 Python 之间是不可预测的。

这种做法是为了防止以下形式的拒绝服务攻击：通过仔细选择输入来利用字典插入操作在最坏情况下的执行效率即 O(n^2) 复杂度。详情见 http://www.ocert.org/advisories/ocert-2011-003.html

Changing hash values affects the iteration order of dicts, sets and other mappings. Python has never made guarantees about this ordering (and it typically varies between 32-bit and 64-bit builds).

另见 PYTHONHASHSEED.

在 3.3 版更改: 默认启用哈希随机化。

object.__bool__(self)

调用此方法以实现真值检测以及内置的 bool() 操作；应该返回 False 或 True。如果未定义此方法，则会查找并调用 __len__() 并在其返回非零值时视对象的逻辑值为真。如果一个类既未定义 __len__() 也未定义 __bool__() 则视其所有实例的逻辑值为真。

3.3.2. 自定义属性访问

可以定义下列方法来自定义对类实例属性访问（x.name 的使用、赋值或删除）的具体含义.

object.__getattr__(self, name)

当默认属性访问因引发 AttributeError 而失败时被调用 (可能是调用 __getattribute__() 时由于 name 不是一个实例属性或 self 的类关系树中的属性而引发了 AttributeError；或者是对 name 特性属性调用 __get__() 时引发了 AttributeError)。此方法应当返回（找到的）属性值或是引发一个 AttributeError 异常。

请注意如果属性是通过正常机制找到的，__getattr__() 就不会被调用。（这是在 __getattr__() 和 __setattr__() 之间故意设置的不对称性。）这既是出于效率理由也是因为不这样设置的话 __getattr__() 将无法访问实例的其他属性。要注意至少对于实例变量来说，你不必在实例属性字典中插入任何值（而是通过插入到其他对象）就可以模拟对它的完全控制。请参阅下面的 __getattribute__() 方法了解真正获取对属性访问的完全控制权的办法。

object.__getattribute__(self, name)

此方法会无条件地被调用以实现对类实例属性的访问。如果类还定义了 __getattr__()，则后者不会被调用，除非 __getattribute__() 显式地调用它或是引发了 AttributeError。此方法应当返回（找到的）属性值或是引发一个 AttributeError 异常。为了避免此方法中的无限递归，其实现应该总是调用具有相同名称的基类方法来访问它所需要的任何属性，例如 object.__getattribute__(self, name)。

注解

此方法在作为通过特定语法或内置函数隐式地调用的结果的情况下查找特殊方法时仍可能会被跳过。参见 特殊方法查找。

object.__setattr__(self, name, value)

此方法在一个属性被尝试赋值时被调用。这个调用会取代正常机制（即将值保存到实例字典）。 name 为属性名称， value 为要赋给属性的值。

如果 __setattr__() 想要赋值给一个实例属性，它应该调用同名的基类方法，例如 object.__setattr__(self, name, value)。

object.__delattr__(self, name)

类似于 __setattr__() 但其作用为删除而非赋值。此方法应该仅在 del obj.name 对于该对象有意义时才被实现。

object.__dir__(self)

此方法会在对相应对象调用 dir() 时被调用。返回值必须为一个序列。 dir() 会把返回的序列转换为列表并对其排序。

3.3.2.1. 自定义模块属性访问

想要更细致地自定义模块的行为（设置属性和特性属性等待），可以将模块对象的 __class__ 属性设置为一个 types.ModuleType 的子类。例如:

import sys
from types import ModuleType

class VerboseModule(ModuleType):
    def __repr__(self):
        return f'Verbose {self.__name__}'

    def __setattr__(self, attr, value):
        print(f'Setting {attr}...')
        setattr(self, attr, value)

sys.modules[__name__].__class__ = VerboseModule

注解

Setting module __class__ only affects lookups made using the attribute access syntax – directly accessing the module globals (whether by code within the module, or via a reference to the module’s globals dictionary) is unaffected.

在 3.5 版更改: __class__ 模块属性改为可写。

3.3.2.2. 实现描述器

以下方法仅当一个包含该方法的类（称为 描述器 类）的实例出现于一个 所有者 类中的时候才会起作用（该描述器必须在所有者类或其某个上级类的字典中）。在以下示例中，“属性”指的是名称为所有者类 __dict__ 中的特征属性的键名的属性。

object.__get__(self, instance, owner)

调用此方法以获取所有者类的属性（类属性访问）或该类的实例的属性（实例属性访问）。所有者 是指所有者类，而 实例 是指被用来访问属性的实例，如果是 所有者 被用来访问属性时则为 None。此方法应当返回（计算出的）属性值或是引发一个 AttributeError 异常。

object.__set__(self, instance, value)

调用此方法以设置 instance 指定的所有者类的实例的属性为新值 value。

object.__delete__(self, instance)

调用此方法以删除 instance 指定的所有者类的实例的属性。

object.__set_name__(self, owner, name)

在所有者类 owner 创建时被调用。描述器会被赋值给 name。

3.6 新版功能.

属性 __objclass__ 会被 inspect 模块解读为指定此对象定义所在的类（正确设置此属性有助于动态类属性的运行时内省）。对于可调用对象来说，它可以指明预期或要求提供一个特定类型（或子类）的实例作为第一个位置参数（例如，CPython 会为实现于 C 中的未绑定方法设置此属性）。

3.3.2.3. 发起调用描述器

总的说来，描述器就是具有“绑定行为”的对象属性，其属性访问已被描述器协议中的方法所重载，包括 __get__(), __set__() 和 __delete__()。如果一个对象定义了以上方法中的任意一个，它就被称为描述器。

属性访问的默认行为是从一个对象的字典中获取、设置或删除属性。例如，a.x 的查找顺序会从 a.__dict__['x'] 开始，然后是 type(a).__dict__['x']，接下来依次查找 type(a) 的上级基类，不包括元类。

但是，如果找到的值是定义了某个描述器方法的对象，则 Python 可能会重载默认行为并转而发起调用描述器方法。这具体发生在优先级链的哪个环节则要根据所定义的描述器方法及其被调用的方式来决定。

描述器发起调用的开始点是一个绑定 a.x。参数的组合方式依 a 而定:

直接调用

最简单但最不常见的调用方式是用户代码直接发起调用一个描述器方法: x.__get__(a)。

实例绑定

如果绑定到一个对象实例，a.x 会被转换为调用: type(a).__dict__['x'].__get__(a, type(a))。

类绑定

如果绑定到一个类，A.x 会被转换为调用: A.__dict__['x'].__get__(None, A)。

超绑定

如果 a 是 super 的一个实例，则绑定 super(B, obj).m() 会在 obj.__class__.__mro__ 中搜索 B 的直接上级基类 A 然后通过以下调用发起调用描述器: A.__dict__['m'].__get__(obj, obj.__class__)。

对于实例绑定，发起描述器调用的优先级取决于定义了哪些描述器方法。一个描述器可以定义 __get__()、__set__() 和 __delete__() 的任意组合。如果它没有定义 __get__()，则访问属性会返回描述器对象自身，除非对象的实例字典中有相应属性值。如果描述器定义了 __set__() 和/或 __delete__()，则它是一个数据描述器；如果以上两个都未定义，则它是一个非数据描述器。通常，数据描述器会同时定义 __get__() 和 __set__()，而非数据描述器只有 __get__() 方法。定义了 __set__() 和 __get__() 的数据描述器总是会重载实例字典中的定义。与之相对的，非数据描述器可被实例所重载。

Python 方法 (包括 staticmethod() 和 classmethod()) 都是作为非描述器来实现的。因此实例可以重定义并重载方法。这允许单个实例获得与相同类的其他实例不一样的行为。

property() 函数是作为数据描述器来实现的。因此实例不能重载特性属性的行为。

3.3.2.4. __slots__

__slots__ 允许我们显式地声明数据成员（例如特征属性）并禁止创建 __dict__ 和 __weakref__ (除非是在 __slots__ 中显式地声明或是在父类中可用。)

The space saved over using __dict__ can be significant.

object.__slots__

这个类变量可赋值为字符串、可迭代对象或由实例使用的变量名构成的字符串序列。 __slots__ 会为已声明的变量保留空间，并阻止自动为每个实例创建 __dict__ 和 __weakref__。

3.3.2.4.1. 使用 __slots__ 的注意事项

当继承自一个未定义 __slots__ 的类时，实例的 __dict__ 和 __weakref__ 属性将总是可访问。

没有 __dict__ 变量，实例就不能给未在 __slots__ 定义中列出的新变量赋值。尝试给一个未列出的变量名赋值将引发 AttributeError。新变量需要动态赋值，就要将 '__dict__' 加入到 __slots__ 声明的字符串序列中。

如果未给每个实例设置 __weakref__ 变量，定义了 __slots__ 的类就不支持对其实际的弱引用。如果需要弱引用支持，就要将 '__weakref__' 加入到 __slots__ 声明的字符串序列中。

__slots__ 是通过为每个变量名创建描述器 (实现描述器) 在类层级上实现的。因此，类属性不能被用来为通过 __slots__ 定义的实例变量设置默认值；否则，类属性就会覆盖描述器赋值。

__slots__ 声明的作用不只限于定义它的类。在父类中声明的 __slots__ 在其子类中同样可用。不过，子类将会获得 __dict__ 和 __weakref__ 除非它们也定义了 __slots__ (其中应该仅包含对任何 额外 名称的声明位置)。

如果一个类定义的位置在某个基类中也有定义，则由基类位置定义的实例变量将不可访问（除非通过直接从基类获取其描述器的方式）。这会使得程序的含义变成未定义。未来可能会添加一个防止此情况的检查。

非空的 __slots__ 不适用于派生自“可变长度”内置类型例如 int、bytes 和 tuple 的派生类。

任何非字符串可迭代对象都可以被赋值给 __slots__。映射也可以被使用；不过，未来可能会分别赋给每个键具有特殊含义的值。

__class__ 赋值仅在两个类具有相同的 __slots__ 时才会起作用。

带有多个父类声明位置的多重继承也是可用的，但仅允许一个父类具有由声明位置创建的属性（其他基类必须具有空的位置布局） —— 违反规则将引发 TypeError。

3.3.3. 自定义类创建

当一个类继承其他类时，那个类的 __init_subclass__ 会被调用。这样就可以编写能够改变子类行为的类。这与类装饰器有紧密的关联，但是类装饰器是影响它们所应用的特定类，而 __init_subclass__ 则只作用于定义了该方法的类所派生的子类。

classmethod object.__init_subclass__(cls)

当所在类派生子类时此方法就会被调用。cls 将指向新的子类。如果定义为一个普通实例方法，此方法将被隐式地转换为类方法。

传入一个新类的关键字参数会被传给父类的 __init_subclass__。为了与其他使用 __init_subclass__ 的类兼容，应当根据需要去掉部分关键字参数再将其余的传给基类，例如:

class Philosopher:
    def __init_subclass__(cls, default_name, **kwargs):
        super().__init_subclass__(**kwargs)
        cls.default_name = default_name

class AustralianPhilosopher(Philosopher, default_name="Bruce"):
    pass

object.__init_subclass__ 的默认实现什么都不做，只在带任意参数调用时引发一个错误。

注解

元类提示 metaclass 将被其它类型机制消耗掉，并不会被传给 __init_subclass__ 的实现。实际的元类（而非显式的提示）可通过 type(cls) 访问。

3.6 新版功能.

3.3.3.1. 元类

默认情况下，类是使用 type() 来构建的。类体会在一个新的命名空间内执行，类名会被局部绑定到 type(name, bases, namespace) 的结果。

类创建过程可通过在定义行传入 metaclass 关键字参数，或是通过继承一个包含此参数的现有类来进行定制。在以下示例中，MyClass 和 MySubclass 都是 Meta 的实例:

class Meta(type):
    pass

class MyClass(metaclass=Meta):
    pass

class MySubclass(MyClass):
    pass

在类定义内指定的任何其他关键字参数都会在下面所描述的所有元类操作中进行传递。

当一个类定义被执行时，将发生以下步骤:

the appropriate metaclass is determined

the class namespace is prepared

the class body is executed

the class object is created

3.3.3.2. 确定适当的元类

为一个类定义确定适当的元类是根据以下规则:

if no bases and no explicit metaclass are given, then type() is used

if an explicit metaclass is given and it is not an instance of type(), then it is used directly as the metaclass

if an instance of type() is given as the explicit metaclass, or bases are defined, then the most derived metaclass is used

最近派生的元类会从显式指定的元类（如果有）以及所有指定的基类的元类（即 type(cls)）中选取。最近派生的元类应为 所有 这些候选元类的一个子类型。如果没有一个候选元类符合该条件，则类定义将失败并抛出 TypeError。

3.3.3.3. 准备类命名空间

Once the appropriate metaclass has been identified, then the class namespace is prepared. If the metaclass has a __prepare__ attribute, it is called as namespace = metaclass.__prepare__(name, bases, **kwds) (where the additional keyword arguments, if any, come from the class definition).

如果元类没有 __prepare__ 属性，则类命名空间将初始化为一个空的有序映射。

参见

PEP 3115 [https://www.python.org/dev/peps/pep-3115] - Python 3000 中的元类

引入 __prepare__ 命名空间钩子

3.3.3.4. 执行类主体

类主体会以（类似于） exec(body, globals(), namespace) 的形式被执行。普通调用与 exec() 的关键区别在于当类定义发生于函数内部时，词法作用域允许类主体（包括任何方法）引用来自当前和外部作用域的名称。

但是，即使当类定义发生于函数内部时，在类内部定义的方法仍然无法看到在类作用域层次上定义的名称。类变量必须通过实例的第一个形参或类方法来访问，或者是通过下一节中描述的隐式词法作用域的 __class__ 引用。

3.3.3.5. 创建类对象

一旦执行类主体完成填充类命名空间，将通过调用 metaclass(name, bases, namespace, **kwds) 创建类对象（此处的附加关键字参数与传入 __prepare__ 的相同）。

如果类主体中有任何方法引用了 __class__ 或 super，这个类对象会通过零参数形式的 super(). __class__ 所引用，这是由编译器所创建的隐式闭包引用。这使用零参数形式的 super() 能够正确标识正在基于词法作用域来定义的类，而被用于进行当前调用的类或实例则是基于传递给方法的第一个参数来标识的。

CPython implementation detail: 在 CPython 3.6 及之后的版本中，__class__ 单元会被作为类命名空间中的 __classcell__ 一项传递给元类。如果存在，这必须被向上传播给 type.__new__ 调用，以便能正确地初始化该类。如果不这样做，在 Python 3.6 中将导致 DeprecationWarning，而在 Python 3.8 中将引发 RuntimeError。

当使用默认的元类 type 或者任何最终会调用 type.__new__ 的元类时，以下额外的自定义步骤将在创建类对象之后被发起调用:

首先，type.__new__ 将收集类命名空间中所有定义了 __set_name__() 方法的描述器；

second, all of these __set_name__ methods are called with the class being defined and the assigned name of that particular descriptor; and

最后，将在新类根据方法解析顺序所确定的直接父类上调用 __init_subclass__() 钩子。

在类对象创建之后，它会被传给包含在类定义中的类装饰器（如果有的话），得到的对象将作为已定义的类绑定到局部命名空间。

当通过 type.__new__ 创建一个新类时，提供以作为命名空间形参的对象会被复制到一个新的有序映射并丢弃原对象。这个新副本包装于一个只读代理中，后者则成为类对象的 __dict__ 属性。

参见

PEP 3135 [https://www.python.org/dev/peps/pep-3135] - 新的超类型

描述隐式的 __class__ 闭包引用

3.3.3.6. 元类的作用

元类的潜在作用非常广泛。已经过尝试的设想包括枚举、日志、接口检查、自动委托、自动特征属性创建、代理、框架以及自动资源锁定/同步等等。

3.3.4. 自定义实例及子类检查

以下方法被用来重载 isinstance() 和 issubclass() 内置函数的默认行为。

特别地，元类 abc.ABCMeta 实现了这些方法以便允许将抽象基类（ABC）作为“虚拟基类”添加到任何类或类型（包括内置类型），包括其他 ABC 之中。

class.__instancecheck__(self, instance)

如果 instance 应被视为 class 的一个（直接或间接）实例则返回真值。如果定义了此方法，则会被调用以实现 isinstance(instance, class)。

class.__subclasscheck__(self, subclass)

Return true 如果 subclass 应被视为 class 的一个（直接或间接）子类则返回真值。如果定义了此方法，则会被调用以实现 issubclass(subclass, class)。

请注意这些方法的查找是基于类的类型（元类）。它们不能作为类方法在实际的类中被定义。这与基于实例被调用的特殊方法的查找是一致的，只有在此情况下实例本身被当作是类。

参见

PEP 3119 [https://www.python.org/dev/peps/pep-3119] - 引入抽象基类

新增功能描述，通过 __instancecheck__() 和 __subclasscheck__() 来定制 isinstance() 和 issubclass() 行为，加入此功能的动机是出于向该语言添加抽象基类的内容（参见 abc 模块）。

3.3.5. 模拟可调用对象

object.__call__(self[, args...])

此方法会在实例作为一个函数被“调用”时被调用；如果定义了此方法，则 x(arg1, arg2, ...) 就相当于 x.__call__(arg1, arg2, ...) 的快捷方式。

3.3.6. 模拟容器类型

The following methods can be defined to implement container objects. Containers usually are sequences (such as lists or tuples) or mappings (like dictionaries), but can represent other containers as well. The first set of methods is used either to emulate a sequence or to emulate a mapping; the difference is that for a sequence, the allowable keys should be the integers k for which 0 <= k < N where N is the length of the sequence, or slice objects, which define a range of items. It is also recommended that mappings provide the methods keys(), values(), items(), get(), clear(), setdefault(), pop(), popitem(), copy(), and update() behaving similar to those for Python’s standard dictionary objects. The collections module provides a MutableMapping abstract base class to help create those methods from a base set of __getitem__(), __setitem__(), __delitem__(), and keys(). Mutable sequences should provide methods append(), count(), index(), extend(), insert(), pop(), remove(), reverse() and sort(), like Python standard list objects. Finally, sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods __add__(), __radd__(), __iadd__(), __mul__(), __rmul__() and __imul__() described below; they should not define other numerical operators. It is recommended that both mappings and sequences implement the __contains__() method to allow efficient use of the in operator; for mappings, in should search the mapping’s keys; for sequences, it should search through the values. It is further recommended that both mappings and sequences implement the __iter__() method to allow efficient iteration through the container; for mappings, __iter__() should be the same as keys(); for sequences, it should iterate through the values.

object.__len__(self)

调用此方法以实现内置函数 len()。应该返回对象的长度，以一个 >= 0 的整数表示。此外，如果一个对象未定义 __bool__() 方法而其 __len__() 方法返回值为零，则在布尔运算中会被视为假值。

CPython implementation detail: 在 CPython 中，要求长度最大为 sys.maxsize。如果长度大于 sys.maxsize 则某些特性 (例如 len()) 可能会引发 OverflowError。要通过真值检测来防止引发 OverflowError，对象必须定义 __bool__() 方法。

object.__length_hint__(self)

Called to implement operator.length_hint(). Should return an estimated length for the object (which may be greater or less than the actual length). The length must be an integer >= 0. This method is purely an optimization and is never required for correctness.

3.4 新版功能.

注解

切片是通过下述三个专门方法完成的。以下形式的调用

a[1:2] = b

会为转写为

a[slice(1, 2, None)] = b

其他形式以此类推。略去的切片项总是以 None 补全。

object.__getitem__(self, key)

调用此方法以实现 self[key] 的求值。对于序列类型，接受的键应为整数和切片对象。请注意负数索引（如果类想要模拟序列类型）的特殊解读是取决于 __getitem__() 方法。如果 key 的类型不正确则会引发 TypeError 异常；如果为序列索引集范围以外的值（在进行任何负数索引的特殊解读之后）则应引发 IndexError 异常。对于映射类型，如果 key 找不到（不在容器中）则应引发 KeyError 异常。

注解

for 循环在有不合法索引时会期待捕获 IndexError 以便正确地检测到序列的结束。

object.__setitem__(self, key, value)

调用此方法以实现向 self[key] 赋值。注意事项与 __getitem__() 相同。为对象实现此方法应该仅限于需要映射允许基于键修改值或添加键，或是序列允许元素被替换时。不正确的 key 值所引发的异常应与 __getitem__() 方法的情况相同。

object.__delitem__(self, key)

调用此方法以实现 self[key] 的删除。注意事项与 __getitem__() 相同。为对象实现此方法应该权限于需要映射允许移除键，或是序列允许移除元素时。不正确的 key 值所引发的异常应与 __getitem__() 方法的情况相同。

object.__missing__(self, key)

此方法由 dict.__getitem__() 在找不到字典中的键时调用以实现 dict 子类的 self[key]。

object.__iter__(self)

此方法在需要为容器创建迭代器时被调用。此方法应该返回一个新的迭代器对象，它能够逐个迭代容器中的所有对象。对于映射，它应该逐个迭代容器中的键。

迭代器对象也需要实现此方法；它们需要返回对象自身。有关迭代器对象的详情请参看 迭代器类型 一节。

object.__reversed__(self)

此方法（如果存在）会被 reversed() 内置函数调用以实现逆向迭代。它应当返回一个新的以逆序逐个迭代容器内所有对象的迭代器对象。

如果未提供 __reversed__() 方法，则 reversed() 内置函数将回退到使用序列协议 (__len__() 和 __getitem__())。支持序列协议的对象应当仅在能够提供比 reversed() 所提供的实现更高效的实现时才提供 __reversed__() 方法。

The membership test operators (in and not in) are normally implemented as an iteration through a sequence. However, container objects can supply the following special method with a more efficient implementation, which also does not require the object be a sequence.

object.__contains__(self, item)

调用此方法以实现成员检测运算符。如果 item 是 self 的成员则应返回真，否则返回假。对于映射类型，此检测应基于映射的键而不是值或者键值对。

对于未定义 __contains__() 的对象，成员检测将首先尝试通过 __iter__() 进行迭代，然后再使用 __getitem__() 的旧式序列迭代协议，参看 语言参考中的相应部分。

3.3.7. 模拟数字类型

定义以下方法即可模拟数字类型。特定种类的数字不支持的运算（例如非整数不能进行位运算）所对应的方法应当保持未定义状态。

object.__add__(self, other)

object.__sub__(self, other)

object.__mul__(self, other)

object.__matmul__(self, other)

object.__truediv__(self, other)

object.__floordiv__(self, other)

object.__mod__(self, other)

object.__divmod__(self, other)

object.__pow__(self, other[, modulo])

object.__lshift__(self, other)

object.__rshift__(self, other)

object.__and__(self, other)

object.__xor__(self, other)

object.__or__(self, other)

调用这些方法来实现二进制算术运算 (+, -, *, @, /, //, %, divmod(), pow(), **, <<, >>, &, ^, |)。例如，求表达式 x + y 的值，其中 x 是具有 __add__() 方法的类的一个实例，则会调用 x.__add__(y)。__divmod__() 方法应该等价于使用 __floordiv__() 和 __mod__()，它不应该被关联到 __truediv__()。请注意如果要支持三元版本的内置 pow() 函数，则 __pow__() 的定义应该接受可选的第三个参数。

如果这些方法中的某一个不支持与所提供参数进行运算，它应该返回 NotImplemented。

object.__radd__(self, other)

object.__rsub__(self, other)

object.__rmul__(self, other)

object.__rmatmul__(self, other)

object.__rtruediv__(self, other)

object.__rfloordiv__(self, other)

object.__rmod__(self, other)

object.__rdivmod__(self, other)

object.__rpow__(self, other)

object.__rlshift__(self, other)

object.__rrshift__(self, other)

object.__rand__(self, other)

object.__rxor__(self, other)

object.__ror__(self, other)

调用这些方法来实现具有反射（交换）操作数的二进制算术运算 (+, -, *, @, /, //, %, divmod(), pow(), **, <<, >>, &, ^, |)。这些成员函数仅会在左操作数不支持相应运算 3 且两个操作数类型不同时被调用。4 例如，求表达式 x - y 的值，其中 y 是具有 __rsub__() 方法的类的一个实例，则当 x.__sub__(y) 返回 NotImplemented 时会调用 y.__rsub__(x)。

请注意三元版的 pow() 并不会尝试调用 __rpow__() (因为强制转换规则会太过复杂)。

注解

如果右操作数类型为左操作数类型的一个子类，且该子类提供了指定运算的反射方法，则此方法会先于左操作数的非反射方法被调用。此行为可允许子类重载其祖先类的运算符。

object.__iadd__(self, other)

object.__isub__(self, other)

object.__imul__(self, other)

object.__imatmul__(self, other)

object.__itruediv__(self, other)

object.__ifloordiv__(self, other)

object.__imod__(self, other)

object.__ipow__(self, other[, modulo])

object.__ilshift__(self, other)

object.__irshift__(self, other)

object.__iand__(self, other)

object.__ixor__(self, other)

object.__ior__(self, other)

调用这些方法来实现扩展算术赋值 (+=, -=, *=, @=, /=, //=, %=, **=, <<=, >>=, &=, ^=, |=)。这些方法应该尝试进行自身操作 (修改 self) 并返回结果 (结果应该但并非必须为 self)。如果某个方法未被定义，相应的扩展算术赋值将回退到普通方法。例如，如果 x 是具有 __iadd__() 方法的类的一个实例，则 x += y 就等价于 x = x.__iadd__(y)。否则就如 x + y 的求值一样选择 x.__add__(y) 和 y.__radd__(x)。在某些情况下，扩展赋值可导致未预期的错误 (参见 为什么 a_tuple[i] += [‘item’] 会在执行加法时引发异常？)，但此行为实际上是数据模型的一个组成部分。

object.__neg__(self)

object.__pos__(self)

object.__abs__(self)

object.__invert__(self)

调用此方法以实现一元算术运算 (-, +, abs() 和 ~)。

object.__complex__(self)

object.__int__(self)

object.__float__(self)

调用这些方法以实现内置函数 complex(), int() 和 float()。应当返回一个相应类型的值。

object.__index__(self)

调用此方法以实现 operator.index() 以及 Python 需要无损地将数字对象转换为整数对象的场合（例如切片或是内置的 bin(), hex() 和 oct() 函数)。 存在此方法表明数字对象属于整数类型。 必须返回一个整数。

注解

为了具有一致的整数类型类，当定义了 __index__() 的时候也应当定义 __int__()，两者应当返回相同的值。

object.__round__(self[, ndigits])

object.__trunc__(self)

object.__floor__(self)

object.__ceil__(self)

调用这些方法以实现内置函数 round() 以及 math 函数 trunc(), floor() 和 ceil()。 除了将 ndigits 传给 __round__() 的情况之外这些方法的返回值都应当是原对象截断为 Integral (通常为 int)。

如果未定义 __int__() 则内置函数 int() 会回退到 __trunc__()。

3.3.8. with 语句上下文管理器

A context manager is an object that defines the runtime context to be established when executing a with statement. The context manager handles the entry into, and the exit from, the desired runtime context for the execution of the block of code. Context managers are normally invoked using the with statement (described in section The with statement), but can also be used by directly invoking their methods.

上下文管理器的典型用法包括保存和恢复各种全局状态，锁定和解锁资源，关闭打开的文件等等。

要了解上下文管理器的更多信息，请参阅 上下文管理器类型。

object.__enter__(self)

Enter the runtime context related to this object. The with statement will bind this method’s return value to the target(s) specified in the as clause of the statement, if any.

object.__exit__(self, exc_type, exc_value, traceback)

退出关联到此对象的运行时上下文。 各个参数描述了导致上下文退出的异常。 如果上下文是无异常地退出的，三个参数都将为 None。

如果提供了异常，并且希望方法屏蔽此异常（即避免其被传播），则应当返回真值。 否则的话，异常将在退出此方法时按正常流程处理。

请注意 __exit__() 方法不应该重新引发被传入的异常，这是调用者的责任。

参见

PEP 343 [https://www.python.org/dev/peps/pep-0343] - “with” 语句

Python with 语句的规范描述、背景和示例。

3.3.9. 特殊方法查找

对于自定义类来说，特殊方法的隐式发起调用仅保证在其定义于对象类型中时能正确地发挥作用，而不能定义在对象实例字典中。 该行为就是以下代码会引发异常的原因。:

>>> class C:
...     pass
...
>>> c = C()
>>> c.__len__ = lambda: 5
>>> len(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'C' has no len()

此行为背后的原理在于包括类型对象在内的所有对象都会实现的几个特殊方法，例如 __hash__() 和 __repr__()。 如果这些方法的隐式查找使用了传统的查找过程，它们会在对类型对象本身发起调用时失败:

>>> 1 .__hash__() == hash(1)
True
>>> int.__hash__() == hash(int)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: descriptor '__hash__' of 'int' object needs an argument

以这种方式不正确地尝试发起调用一个类的未绑定方法有时被称为‘元类混淆’，可以通过在查找特殊方法时绕过实例的方式来避免:

>>> type(1).__hash__(1) == hash(1)
True
>>> type(int).__hash__(int) == hash(int)
True

除了为了正确性而绕过任何实例属性之外，隐式特殊方法查找通常也会绕过 __getattribute__() 方法，甚至包括对象的元类:

>>> class Meta(type):
...     def __getattribute__(*args):
...         print("Metaclass getattribute invoked")
...         return type.__getattribute__(*args)
...
>>> class C(object, metaclass=Meta):
...     def __len__(self):
...         return 10
...     def __getattribute__(*args):
...         print("Class getattribute invoked")
...         return object.__getattribute__(*args)
...
>>> c = C()
>>> c.__len__()                 # Explicit lookup via instance
Class getattribute invoked
10
>>> type(c).__len__(c)          # Explicit lookup via type
Metaclass getattribute invoked
10
>>> len(c)                      # Implicit lookup
10

以这种方式绕过 __getattribute__() 机制为解析器内部的速度优化提供了显著的空间，其代价则是牺牲了处理特殊方法时的一些灵活性（特殊方法 必须 设置在类对象本身上以便始终一致地由解释器发起调用）。

3.4. 协程

3.4.1. 可等待对象

awaitable 对象主要实现了 __await__() 方法。 从 async def 函数返回的 Coroutine 对象即属于可等待对象。

注解

从带有 types.coroutine() 或 asyncio.coroutine() 装饰器的生成器返回的 generator iterator 对象也属于可等待对象，但它们并未实现 __await__()。

object.__await__(self)

必须返回一个 iterator。 应当被用来实现 awaitable 对象。 例如，asyncio.Future 实现了此方法以与 await 表达式相兼容。

3.5 新版功能.

参见

PEP 492 [https://www.python.org/dev/peps/pep-0492] 了解有关可等待对象的详细信息。

3.4.2. 协程对象

Coroutine 对象属于 awaitable 对象。 协程的执行可通过调用 __await__() 并迭代其结果来进行控制。 当协程结束执行并返回时，迭代器会引发 StopIteration，该异常的 value 属性将指向返回值。 如果协程引发了异常，它会被迭代器所传播。 协程不应该直接引发未处理的 StopIteration 异常。

协程也具有下面列出的方法，它们类似于生成器的对应方法 (参见 生成器-迭代器的方法)。 但是，与生成器不同，协程并不直接支持迭代。

在 3.5.2 版更改: 等待一个协程超过一次将引发 RuntimeError。

coroutine.send(value)

开始或恢复协程的执行。 如果 value 为 None，则这相当于前往 __await__() 所返回迭代器的下一项。 如果 value 不为 None，此方法将委托给导致协程挂起的迭代器的 send() 方法。 其结果（返回值，StopIteration 或是其他异常）将与上述对 __await__() 返回值进行迭代的结果相同。

coroutine.throw(type[, value[, traceback]])

在协程内引发指定的异常。 此方法将委托给导致协程挂起的迭代器的 throw() 方法，如果存在该方法。 否则的话，异常会在挂起点被引发。 其结果（返回值，StopIteration 或是其他异常）将与上述对 __await__() 返回值进行迭代的结果相同。 如果异常未在协程内被捕获，则将回传给调用者。

coroutine.close()

此方法会使得协程清理自身并退出。 如果协程被挂起，此方法会先委托给导致协程挂起的迭代器的 close() 方法，如果存在该方法。 然后它会在挂起点引发 GeneratorExit，使得协程立即清理自身。 最后，协程会被标记为已结束执行，即使它根本未被启动。

当协程对象将要被销毁时，会使用以上处理过程来自动关闭。

3.4.3. 异步迭代器

An asynchronous iterable is able to call asynchronous code in its __aiter__ implementation, and an asynchronous iterator can call asynchronous code in its __anext__ method.

异步迭代器可在 async for 语句中使用。

object.__aiter__(self)

必须返回一个 异步迭代器 对象。

object.__anext__(self)

必须返回一个 可迭代对象 输出迭代器的下一结果值。 当迭代结束时应该引发 StopAsyncIteration 错误。

异步可迭代对象的一个示例:

class Reader:
    async def readline(self):
        ...

    def __aiter__(self):
        return self

    async def __anext__(self):
        val = await self.readline()
        if val == b'':
            raise StopAsyncIteration
        return val

3.5 新版功能.

注解

在 3.5.2 版更改: Starting with CPython 3.5.2, __aiter__ can directly return asynchronous iterators. Returning an awaitable object will result in a PendingDeprecationWarning.

The recommended way of writing backwards compatible code in CPython 3.5.x is to continue returning awaitables from __aiter__. If you want to avoid the PendingDeprecationWarning and keep the code backwards compatible, the following decorator can be used:

import functools
import sys

if sys.version_info < (3, 5, 2):
    def aiter_compat(func):
        @functools.wraps(func)
        async def wrapper(self):
            return func(self)
        return wrapper
else:
    def aiter_compat(func):
        return func

Example:

class AsyncIterator:

    @aiter_compat
    def __aiter__(self):
        return self

    async def __anext__(self):
        ...

Starting with CPython 3.6, the PendingDeprecationWarning will be replaced with the DeprecationWarning. In CPython 3.7, returning an awaitable from __aiter__ will result in a RuntimeError.

3.4.4. 异步上下文管理器

异步上下文管理器 是 上下文管理器 的一种，它能够在其 __aenter__ 和 __aexit__ 方法中暂停执行。

异步上下文管理器可在 async with 语句中使用。

object.__aenter__(self)

此方法在语义上类似于 __enter__()，仅有的区别是它必须返回一个 可等待对象。

object.__aexit__(self, exc_type, exc_value, traceback)

此方法在语义上类似于 __exit__()，仅有的区别是它必须返回一个 可等待对象。

异步上下文管理器类的一个示例:

class AsyncContextManager:
    async def __aenter__(self):
        await log('entering context')

    async def __aexit__(self, exc_type, exc, tb):
        await log('exiting context')

3.5 新版功能.

备注

1

在某些情况下 有可能 基于可控的条件改变一个对象的类型。 但这通常不是个好主意，因为如果处理不当会导致一些非常怪异的行为。

2

__hash__(), __iter__(), __reversed__() 以及 __contains__() 方法对此有特殊处理；其他方法仍会引发 TypeError，但可能依靠 None 属于不可调用对象的行为来做到这一点。

3

这里的“不支持”是指该类无此方法，或方法返回 NotImplemented。 如果你想强制回退到右操作数的反射方法，请不要设置方法为 None — 那会造成显式地 阻塞 此种回退的相反效果。

4

对于相同类型的操作数，如果非反射方法 (例如 __add__()) 失败则会认为相应运算不被支持，这就是反射方法未被调用的原因。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

4. 执行模型

4.1. 程序的结构

Python 程序是由代码块构成的。 代码块 是被作为一个单元来执行的一段 Python 程序文本。 以下几个都是代码块：模块、函数体和类定义。 交互式输入的每条命令都是一个代码块。 一个脚本文件（作为标准输入发送给解释器或是作为命令行参数发送给解释器的文件）也是一个代码块。 一条脚本命令（通过 -c 选项在解释器命令行中指定的命令）也是一个代码块。 传递给内置函数 eval() 和 exec() 的字符串参数也是代码块。

代码块在 执行帧 中被执行。 一个帧会包含某些管理信息（用于调试）并决定代码块执行完成后应前往何处以及如何继续执行。

4.2. 命名与绑定

4.2.1. 名称的绑定

名称 用于指代对象。 名称是通过名称绑定操作来引入的。

The following constructs bind names: formal parameters to functions, import statements, class and function definitions (these bind the class or function name in the defining block), and targets that are identifiers if occurring in an assignment, for loop header, or after as in a with statement or except clause. The import statement of the form from ... import * binds all names defined in the imported module, except those beginning with an underscore. This form may only be used at the module level.

del 语句的目标也被视作一种绑定（虽然其实际语义为解除名称绑定）。

每条赋值或导入语句均发生于类或函数内部定义的代码块中，或是发生于模块层级（即最高层级的代码块）。

如果名称绑定在一个代码块中，则为该代码块的局部变量，除非声明为 nonlocal 或 global。 如果名称绑定在模块层级，则为全局变量。 (模块代码块的变量既为局部变量又为全局变量。) 如果变量在一个代码块中被使用但不是在其中定义，则为 自由变量。

每个在程序文本中出现的名称是指由以下名称解析规则所建立的对该名称的 绑定。

4.2.2. 名称的解析

作用域 定义了一个代码块中名称的可见性。 如果代码块中定义了一个局部变量，则其作用域包含该代码块。 如果定义发生于函数代码块中，则其作用域会扩展到该函数所包含的任何代码块，除非有某个被包含代码块引入了对该名称的不同绑定。

当一个名称在代码块中被使用时，会由包含它的最近作用域来解析。 对一个代码块可见的所有这种作用域的集合称为该代码块的 环境。

当一个名称完全找不到时，将会引发 NameError 异常。 如果当前作用域为函数作用域，且该名称指向一个局部变量，而此变量在该名称被使用的时候尚未绑定到特定值，将会引发 UnboundLocalError 异常。 UnboundLocalError 为 NameError 的一个子类。

如果一个代码块内的任何位置发生名称绑定操作，则代码块内所有对该名称的使用会被认为是对当前代码块的引用。 当一个名称在其被绑定前就在代码块内被使用时则会导致错误。 这个一个很微妙的规则。 Python 缺少声明语法，并允许名称绑定操作发生于代码块内的任何位置。 一个代码块的局部变量可通过在整个代码块文本中扫描名称绑定操作来确定。

If the global statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace. Names are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace, the namespace of the module builtins. The global namespace is searched first. If the name is not found there, the builtins namespace is searched. The global statement must precede all uses of the name.

global 语句与同一代码块中名称绑定具有相同的作用域。 如果一个自由变量的最近包含作用域中有一条 global 语句，则该自由变量也会被当作是全局变量。

nonlocal 语句会使得相应的名称指向之前在最近包含函数作用域中绑定的变量。 如果指定名称不存在于任何包含函数作用域中则将在编译时引发 SyntaxError。

模块的作用域会在模块第一次被导入时自动创建。 一个脚本的主模块总是被命名为 __main__。

类定义代码块以及传给 exec() 和 eval() 的参数是名称解析上下文中的特殊情况。 类定义是可能使用并定义名称的可执行语句。 这些引用遵循正常的名称解析规则，例外之处在于未绑定的局部变量将会在全局命名空间中查找。 类定义的命名空间会成为该类的属性字典。 在类代码块中定义的名称的作用域会被限制在类代码块中；它不会扩展到方法的代码块中 – 这也包括推导式和生成器表达式，因为它们都是使用函数作用域实现的。 这意味着以下代码将会失败:

class A:
    a = 42
    b = list(a + i for i in range(10))

4.2.3. 内置命名空间和受限的执行

CPython implementation detail: 用户不应该接触 __builtins__，严格说来它属于实现细节。 用户如果要重载内置命名空间中的值则应该 import builtins 并相应地修改该模块中的属性。

与一个代码块的执行相关联的内置命名空间实际上是通过在其全局命名空间中搜索名称 __builtins__ 来找到的；这应该是一个字典或一个模块（在后一种情况下会使用该模块的字典）。 默认情况下，当在 __main__ 模块中时，__builtins__ 就是内置模块 builtins；当在任何其他模块中时，__builtins__ 则是 builtins 模块自身的字典的一个别名。

4.2.4. 与动态特性的交互

自由变量的名称解析发生于运行时而不是编译时。 这意味着以下代码将打印出 42:

i = 10
def f():
    print(i)
i = 42
f()

eval() 和 exec() 函数没有对完整环境的访问权限来解析名称。 名称可以在调用者的局部和全局命名空间中被解析。 自由变量的解析不是在最近包含命名空间中，而是在全局命名空间中。 1 exec() 和 eval() 函数有可选参数用来重载全局和局部命名空间。 如果只指定一个命名空间，则它会同时作用于两者。

4.3. 异常

异常是中断代码块的正常控制流程以便处理错误或其他异常条件的一种方式。 异常会在错误被检测到的位置 引发，它可以被当前包围代码块或是任何直接或间接发起调用发生错误的代码块的其他代码块所 处理。

Python 解析器会在检测到运行时错误（例如零作为被除数）的时候引发异常。 Python 程序也可以通过 raise 语句显式地引发异常。 异常处理是通过 try … except 语句来指定的。 该语句的 finally 子句可被用来指定清理代码，它并不处理异常，而是无论之前的代码是否发生异常都会被执行。

Python 的错误处理采用的是“终止”模型：异常处理器可以找出发生了什么问题，并在外层继续执行，但它不能修复错误的根源并重试失败的操作（除非通过从顶层重新进入出错的代码片段）。

When an exception is not handled at all, the interpreter terminates execution of the program, or returns to its interactive main loop. In either case, it prints a stack backtrace, except when the exception is SystemExit.

异常是通过类实例来标识的。 except 子句会依据实例的类来选择：它必须引用实例的类或是其所属的基类。 实例可通过处理器被接收，并可携带有关异常条件的附加信息。

注解

异常消息不是 Python API 的组成部分。 其内容可能在 Python 升级到新版本时不经警告地发生改变，不应该被需要在多版本解释器中运行的代码所依赖。

另请参看 The try statement 小节中对 try 语句的描述以及 The raise statement 小节中对 raise 语句的描述。

备注

1

出现这样的限制是由于通过这些操作执行的代码在模块被编译的时候并不可用。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

5. 导入系统

一个 module 内的 Python 代码通过 importing 操作就能够访问另一个模块内的代码。 import 语句是发起调用导入机制的最常用方式，但不是唯一的方式。 importlib.import_module() 以及内置的 __import__() 等函数也可以被用来发起调用导入机制。

The import statement combines two operations; it searches for the named module, then it binds the results of that search to a name in the local scope. The search operation of the import statement is defined as a call to the __import__() function, with the appropriate arguments. The return value of __import__() is used to perform the name binding operation of the import statement. See the import statement for the exact details of that name binding operation.

对 __import__() 的直接调用将仅执行模块搜索以及在找到时的模块创建操作。 不过也可能产生某些副作用，例如导入父包和更新各种缓存 (包括 sys.modules)，只有 import 语句会执行名称绑定操作。

When calling __import__() as part of an import statement, the standard builtin __import__() is called. Other mechanisms for invoking the import system (such as importlib.import_module()) may choose to subvert __import__() and use its own solution to implement import semantics.

当一个模块首次被导入时，Python 会搜索该模块，如果找到就创建一个 module 对象 1 并初始化它。 如果指定名称的模块未找到，则会引发 ModuleNotFoundError。 当发起调用导入机制时，Python 会实现多种策略来搜索指定名称的模块。 这些策略可以通过使用使用下文所描述的多种钩子来加以修改和扩展。

在 3.3 版更改: 导入系统已被更新以完全实现 PEP 302 [https://www.python.org/dev/peps/pep-0302] 中的第二阶段要求。 不会再有任何隐式的导入机制 —— 整个导入系统都通过 sys.meta_path 暴露出来。 此外，对原生命名空间包的支持也已被实现 (参见 PEP 420 [https://www.python.org/dev/peps/pep-0420])。

5.1. importlib

importlib 模块提供了一个丰富的 API 用来与导入系统进行交互。 例如 importlib.import_module() 提供了相比内置的 __import__() 更推荐、更简单的 API 用来发起调用导入机制。 更多细节请参看 importlib 库文档。

5.2. 包

Python 只有一种模块对象类型，所有模块都属于该类型，无论模块是用 Python、C 还是别的语言实现。 为了帮助组织模块并提供名称层次结构，Python 还引入了 包 的概念。

你可以把包看成是文件系统中的目录，并把模块看成是目录中的文件，但请不要对这个类似做过于字面的理解，因为包和模块不是必须来自于文件系统。 为了方便理解本文档，我们将继续使用这种目录和文件的类比。 与文件系统一样，包通过层次结构进行组织，在包之内除了一般的模块，还可以有子包。

要注意的一个重点概念是所有包都是模块，但并非所有模块都是包。 或者换句话说，包只是一种特殊的模块。 特别地，任何具有 __path__ 属性的模块都会被当作是包。

所有模块都有自己的名字。 子包名与其父包名以点号分隔，与 Python 的标准属性访问语法一致。 例如你可能看到一个名为 sys 的模块，以及一个名为 email 的包，这个包又有一个名为 email.mime 的子包和该子包中的名为 email.mime.text 的子包。

5.2.1. 常规包

Python 定义了两种类型的包，常规包 和 命名空间包。 常规包是传统的包类型，它们在 Python 3.2 及之前就已存在。 常规包通常以一个包含 __init__.py 文件的目录形式实现。 当一个常规包被导入时，这个 __init__.py 文件会隐式地被执行，它所定义的对象会被绑定到该包命名空间中的名称。__init__.py 文件可以包含与任何其他模块中所包含的 Python 代码相似的代码，Python 将在模块被导入时为其添加额外的属性。

例如，以下文件系统布局定义了一个最高层级的 parent 包和三个子包:

parent/
    __init__.py
    one/
        __init__.py
    two/
        __init__.py
    three/
        __init__.py

导入 parent.one 将隐式地执行 parent/__init__.py 和 parent/one/__init__.py。 后续导入 parent.two 或 parent.three 则将分别执行 parent/two/__init__.py 和 parent/three/__init__.py。

5.2.2. 命名空间包

命名空间包是由多个 部分 构成的，每个部分为父包增加一个子包。 各个部分可能处于文件系统的不同位置。 部分也可能处于 zip 文件中、网络上，或者 Python 在导入期间可以搜索的其他地方。 命名空间包并不一定会直接对应到文件系统中的对象；它们有可能是无实体表示的虚拟模块。

命名空间包的 __path__ 属性不使用普通的列表。 而是使用定制的可迭代类型，如果其父包的路径 (或者最高层级包的 sys.path) 发生改变，这种对象会在该包内的下一次导入尝试时自动执行新的对包部分的搜索。

命名空间包没有 parent/__init__.py 文件。 实际上，在导入搜索期间可能找到多个 parent 目录，每个都由不同的部分所提供。 因此 parent/one 的物理位置不一定与 parent/two 相邻。 在这种情况下，Python 将为顶级的 parent 包创建一个命名空间包，无论是它本身还是它的某个子包被导入。

另请参阅 PEP 420 [https://www.python.org/dev/peps/pep-0420] 了解对命名空间包的规格描述。

5.3. 搜索

为了开始搜索，Python 需要被导入模块（或者包，对于当前讨论来说两者没有差别）的完整 限定名称。 此名称可以来自 import 语句所带的各种参数，或者来自传给 importlib.import_module() 或 __import__() 函数的形参。

此名称会在导入搜索的各个阶段被使用，它也可以是指向一个子模块的带点号路径，例如 foo.bar.baz。 在这种情况下，Python 会先尝试导入 foo，然后是 foo.bar，最后是 foo.bar.baz。 如果这些导入中的任何一个失败，都会引发 ModuleNotFoundError。

5.3.1. 模块缓存

在导入搜索期间首先会被检查的地方是 sys.modules。 这个映射起到缓存之前导入的所有模块的作用（包括其中间路径）。 因此如果之前导入过 foo.bar.baz，则 sys.modules 将包含 foo, foo.bar 和 foo.bar.baz 条目。 每个键的值就是相应的模块对象。

在导入期间，会在 sys.modules 查找模块名称，如存在则其关联的值就是需要导入的模块，导入过程完成。 然而，如果值为 None，则会引发 ModuleNotFoundError。 如果找不到指定模块名称，Python 将继续搜索该模块。

sys.modules 是可写的。删除键可能不会破坏关联的模块（因为其他模块可能会保留对它的引用），但它会使命名模块的缓存条目无效，导致 Python 在下次导入时重新搜索命名模块。键也可以赋值为 None ，强制下一次导入模块导致 ModuleNotFoundError 。

但是要小心，因为如果你还保有对某个模块对象的引用，同时停用其在 sys.modules 中的缓存条目，然后又再次导入该名称的模块，则前后两个模块对象将 不是 同一个。 相反地，importlib.reload() 将重用 同一个 模块对象，并简单地通过重新运行模块的代码来重新初始化模块内容。

5.3.2. 查找器和加载器

如果指定名称的模块在 sys.modules 找不到，则将发起调用 Python 的导入协议以查找和加载该模块。 此协议由两个概念性模块构成，即 查找器 和 加载器。 查找器的任务是确定是否能使用其所知的策略找到该名称的模块。 同时实现这两种接口的对象称为 导入器 —— 它们在确定能加载所需的模块时会返回其自身。

Python 包含了多个默认查找器和导入器。 第一个知道如何定位内置模块，第二个知道如何定位冻结模块。 第三个默认查找器会在 import path 中搜索模块。 import path 是一个由文件系统路径或 zip 文件组成的位置列表。 它还可以扩展为搜索任意可定位资源，例如由 URL 指定的资源。

导入机制是可扩展的，因此可以加入新的查找器以扩展模块搜索的范围和作用域。

查找器并不真正加载模块。 如果它们能找到指定名称的模块，会返回一个 模块规格说明，这是对模块导入相关信息的封装，供后续导入机制用于在加载模块时使用。

以下各节描述了有关查找器和加载器协议的更多细节，包括你应该如何创建并注册新的此类对象来扩展导入机制。

在 3.4 版更改: 在之前的 Python 版本中，查找器会直接返回 加载器，现在它们则返回模块规格说明，其中 包含 加载器。 加载器仍然在导入期间被使用，但负担的任务有所减少。

5.3.3. 导入钩子

导入机制被设计为可扩展；其中的基本机制是 导入钩子。 导入钩子有两种类型: 元钩子 和 导入路径钩子。

元钩子在导入过程开始时被调用，此时任何其他导入过程尚未发生，但 sys.modules 缓存查找除外。 这允许元钩子重载 sys.path 过程、冻结模块甚至内置模块。 元钩子的注册是通过向 sys.meta_path 添加新的查找器对象，具体如下所述。

导入路径钩子是作为 sys.path (或 package.__path__) 过程的一部分，在遇到它们所关联的路径项的时候被调用。 导入路径钩子的注册是通过向 sys.path_hooks 添加新的可调用对象，具体如下所述。

5.3.4. 元路径

当指定名称的模块在 sys.modules 中找不到时，Python 会接着搜索 sys.meta_path，其中包含元路径查找器对象列表。 这些查找器按顺序被查询以确定它们是否知道如何处理该名称的模块。 元路径查找器必须实现名为 find_spec() 的方法，该方法接受三个参数：名称、导入路径和目标模块（可选）。 元路径查找器可使用任何策略来确定它是否能处理指定名称的模块。

如果元路径查找器知道如何处理指定名称的模块，它将返回一个说明对象。 如果它不能处理该名称的模块，则会返回 None。 如果 sys.meta_path 处理过程到达列表末尾仍未返回说明对象，则将引发 ModuleNotFoundError。 任何其他被引发异常将直接向上传播，并放弃导入过程。

元路径查找器的 find_spec() 方法调用带有两到三个参数。 第一个是被导入模块的完整限定名称，例如 foo.bar.baz。 第二个参数是供模块搜索使用的路径条目。 对于最高层级模块，第二个参数为 None，但对于子模块或子包，第二个参数为父包 __path__ 属性的值。 如果相应的 __path__ 属性无法访问，将引发 ModuleNotFoundError。 第三个参数是一个将被作为稍后加载目标的现有模块对象。 导入系统仅会在重加载期间传入一个目标模块。

对于单个导入请求可以多次遍历元路径。 例如，假设所涉及的模块都尚未被缓存，则导入 foo.bar.baz 将首先执行顶级的导入，在每个元路径查找器 (mpf) 上调用 mpf.find_spec("foo", None, None)。 在导入 foo 之后，foo.bar 将通过第二次遍历元路径来导入，调用 mpf.find_spec("foo.bar", foo.__path__, None)。 一旦 foo.bar 完成导入，最后一次遍历将调用 mpf.find_spec("foo.bar.baz", foo.bar.__path__, None)。

有些元路径查找器只支持顶级导入。 当把 None 以外的对象作为第三个参数传入时，这些导入器将总是返回 None。

Python 的默认 sys.meta_path 具有三种元路径查找器，一种知道如何导入内置模块，一种知道如何导入冻结模块，还有一种知道如何导入来自 import path 的模块 (即 path based finder)。

在 3.4 版更改: 元路径查找器的 find_spec() 方法替代了 find_module()，后者现已弃用，它将继续可用但不会再做改变，导入机制仅会在查找器未实现 find_spec() 时尝试使用它。

5.4. 加载

当一个模块说明被找到时，导入机制将在加载该模块时使用它（及其所包含的加载器）。 下面是导入的加载部分所发生过程的简要说明:

module = None
if spec.loader is not None and hasattr(spec.loader, 'create_module'):
    # It is assumed 'exec_module' will also be defined on the loader.
    module = spec.loader.create_module(spec)
if module is None:
    module = ModuleType(spec.name)
# The import-related module attributes get set here:
_init_module_attrs(spec, module)

if spec.loader is None:
    if spec.submodule_search_locations is not None:
        # namespace package
        sys.modules[spec.name] = module
    else:
        # unsupported
        raise ImportError
elif not hasattr(spec.loader, 'exec_module'):
    module = spec.loader.load_module(spec.name)
    # Set __loader__ and __package__ if missing.
else:
    sys.modules[spec.name] = module
    try:
        spec.loader.exec_module(module)
    except BaseException:
        try:
            del sys.modules[spec.name]
        except KeyError:
            pass
        raise
return sys.modules[spec.name]

请注意以下细节:

如果在 sys.modules 中存在指定名称的模块对象，导入操作会已经将其返回。

在加载器执行模块代码之前，该模块将存在于 sys.modules 中。 这一点很关键，因为该模块代码可能（直接或间接地）导入其自身；预先将其添加到 sys.modules 可防止在最坏情况下的无限递归和最好情况下的多次加载。

如果加载失败，则该模块 – 只限加载失败的模块 – 将从 sys.modules 中移除。 任何已存在于 sys.modules 缓存的模块，以及任何作为附带影响被成功加载的模块仍会保留在缓存中。 这与重新加载不同，后者会把即使加载失败的模块也保留在 sys.modules 中。

在模块创建完成但还未执行之前，导入机制会设置导入相关模块属性（在上面的示例伪代码中为 “_init_module_attrs”），详情参见 后续部分。

模块执行是加载的关键时刻，在此期间将填充模块的命名空间。 执行会完全委托给加载器，由加载器决定要填充的内容和方式。

在加载过程中创建并传递给 exec_module() 的模块并不一定就是在导入结束时返回的模块 2。

在 3.4 版更改: 导入系统已经接管了加载器建立样板的责任。 这些在以前是由 importlib.abc.Loader.load_module() 方法来执行的。

5.4.1. 加载器

模块加载器提供关键的加载功能：模块执行。 导入机制调用 importlib.abc.Loader.exec_module() 方法并传入一个参数来执行模块对象。 从 exec_module() 返回的任何值都将被忽略。

加载器必须满足下列要求:

如果模块是一个 Python 模块（而非内置模块或动态加载的扩展），加载器应该在模块的全局命名空间 (module.__dict__) 中执行模块的代码。

如果加载器无法执行指定模块，它应该引发 ImportError，不过在 exec_module() 期间引发的任何其他异常也会被传播。

在许多情况下，查找器和加载器可以是同一对象；在此情况下 find_spec() 方法将返回一个规格说明，其中加载器会被设为 self。

模块加载器可以选择通过实现 create_module() 方法在加载期间创建模块对象。 它接受一个参数，即模块规格说明，并返回新的模块对象供加载期间使用。 create_module() 不需要在模块对象上设置任何属性。 如果模块返回 None，导入机制将自行创建新模块。

3.4 新版功能: 加载器的 create_module() 方法。

在 3.4 版更改: load_module() 方法被 exec_module() 所替代，导入机制会对加载的所有样板责任作出假定。

为了与现有的加载器兼容，导入机制会使用导入器的 load_module() 方法，如果它存在且导入器也未实现 exec_module()。 但是，load_module() 现已弃用，加载器应该转而实现 exec_module()。

除了执行模块之外，load_module() 方法必须实现上文描述的所有样板加载功能。 所有相同的限制仍然适用，并带有一些附加规定:

如果 sys.modules 中存在指定名称的模块对象，加载器必须使用已存在的模块。 （否则 importlib.reload() 将无法正确工作。） 如果该名称模块不存在于 sys.modules 中，加载器必须创建一个新的模块对象并将其加入 sys.modules。

在加载器执行模块代码之前，模块 必须 存在于 sys.modules 之中，以防止无限递归或多次加载。

如果加载失败，加载器必须移除任何它已加入到 sys.modules 中的模块，但它必须 仅限 移除加载失败的模块，且所移除的模块应为加载器自身显式加载的。

在 3.5 版更改: 当 exec_module() 已定义但 create_module() 未定义时将引发 DeprecationWarning。

在 3.6 版更改: 当 exec_module() 已定义但 create_module() 未定义时将引发 ImportError。

5.4.2. 子模块

当使用任意机制 (例如 importlib API, import 及 import-from 语句或者内置的 __import__()) 加载一个子模块时，父模块的命名空间中会添加一个对子模块对象的绑定。 例如，如果包 spam 有一个子模块 foo，则在导入 spam.foo 之后，spam 将具有一个 绑定到相应子模块的 foo 属性。 假如现在有如下的目录结构:

spam/
    __init__.py
    foo.py
    bar.py

并且 spam/__init__.py 中有如下几行内容:

from .foo import Foo
from .bar import Bar

则执行如下代码将在 spam 模块中添加对 foo 和 bar 的名称绑定:

>>> import spam
>>> spam.foo
<module 'spam.foo' from '/tmp/imports/spam/foo.py'>
>>> spam.bar
<module 'spam.bar' from '/tmp/imports/spam/bar.py'>

按照通常的 Python 名称绑定规则，这看起来可能会令人惊讶，但它实际上是导入系统的一个基本特性。 保持不变的一点是如果你有 sys.modules['spam'] 和 sys.modules['spam.foo'] (例如在上述导入之后就是如此)，则后者必须显示为前者的 foo 属性。

5.4.3. 模块规格说明

导入机制在导入期间会使用有关每个模块的多种信息，特别是加载之前。 大多数信息都是所有模块通用的。 模块规格说明的目的是基于每个模块来封装这些导入相关信息。

在导入期间使用规格说明可允许状态在导入系统各组件之间传递，例如在创建模块规格说明的查找器和执行模块的加载器之间。 最重要的一点是，它允许导入机制执行加载的样板操作，在没有模块规格说明的情况下这是加载器的责任。

模块的规格说明会作为模块对象的 __spec__ 属性对外公开。 有关模块规格的详细内容请参阅 ModuleSpec。

3.4 新版功能.

5.4.4. 导入相关的模块属性

导入机制会在加载期间会根据模块的规格说明填充每个模块对象的这些属性，并在加载器执行模块之前完成。

__name__

__name__ 属性必须被设为模块的完整限定名称。 此名称被用来在导入系统中唯一地标识模块。

__loader__

__loader__ 属性必须被设为导入系统在加载模块时使用的加载器对象。 这主要是用于内省，但也可用于额外的加载器专用功能，例如获取关联到加载器的数据。

__package__

模块的 __package__ 属性必须设定。 其取值必须为一个字符串，但可以与 __name__ 取相同的值。 当模块是包时，其 __package__ 值应该设为其 __name__ 值。 当模块不是包时，对于最高层级模块 __package__ 应该设为空字符串，对于子模块则应该设为其父包名。 更多详情可参阅 PEP 366 [https://www.python.org/dev/peps/pep-0366]。

该属性取代 __name__ 被用来为主模块计算显式相对导入，相关定义见 PEP 366 [https://www.python.org/dev/peps/pep-0366]。 预期它与 __spec__.parent 具有相同的值。

在 3.6 版更改: __package__ 预期与 __spec__.parent 具有相同的值。

__spec__

__spec__ 属性必须设为在导入模块时要使用的模块规格说明。 对 __spec__ 的正确设定将同时作用于 解释器启动期间初始化的模块。 唯一的例外是 __main__，其中的 __spec__ 会 在某些情况下设为 None.

当 __package__ 未定义时， __spec__.parent 会被用作回退项。

3.4 新版功能.

在 3.6 版更改: 当 __package__ 未定义时，__spec__.parent 会被用作回退项。

__path__

如果模块为包（不论是正规包还是命名空间包），则必须设置模块对象的 __path__ 属性。 属性值必须为可迭代对象，但如果 __path__ 没有进一步的用处则可以为空。 如果 __path__ 不为空，则在迭代时它应该产生字符串。 有关 __path__ 语义的更多细节将在 下文 中给出。

不是包的模块不应该具有 __path__ 属性。

__file__

__cached__

__file__ 是可选项。 如果设置，此属性的值必须为字符串。 导入系统可以选择在其没有语法意义时不设置 __file__ (例如从数据库加载的模块)。

如果设置了 __file__，则也可以再设置 __cached__ 属性，后者取值为编译版本代码（例如字节码文件）所在的路径。 设置此属性不要求文件已存在；该路径可以简单地指向应该存放编译文件的位置 (参见 PEP 3147 [https://www.python.org/dev/peps/pep-3147])。

当未设置 __file__ 时也可以设置 __cached__。 但是，那样的场景很不典型。 最终，加载器会使用 __file__ 和/或 __cached__。 因此如果一个加载器可以从缓存加载模块但是不能从文件加载，那种非典型场景就是适当的。

5.4.5. module.__path__

根据定义，如果一个模块具有 __path__ 属性，它就是包。

包的 __path__ 属性会在导入其子包期间被使用。 在导入机制内部，它的功能与 sys.path 基本相同，即在导入期间提供一个模块搜索位置列表。 但是，__path__ 通常会比 sys.path 受到更多限制。

__path__ 必须是由字符串组成的可迭代对象，但它也可以为空。 作用于 sys.path 的规则同样适用于包的 __path__，并且 sys.path_hooks (见下文) 会在遍历包的 __path__ 时被查询。

包的 __init__.py 文件可以设置或更改包的 __path__ 属性，而且这是在 PEP 420 [https://www.python.org/dev/peps/pep-0420] 之前实现命名空间包的典型方式。 随着 PEP 420 [https://www.python.org/dev/peps/pep-0420] 的引入，命名空间包不再需要提供仅包含 __path__ 操控代码的 __init__.py 文件；导入机制会自动为命名空间包正确地设置 __path__。

5.4.6. 模块的 repr

默认情况下，全部模块都具有一个可用的 repr，但是你可以依据上述的属性设置，在模块的规格说明中更为显式地控制模块对象的 repr。

如果模块具有 spec (__spec__)，导入机制将尝试用它来生成一个 repr。 如果生成失败或找不到 spec，导入系统将使用模块中的各种可用信息来制作一个默认 repr。 它将尝试使用 module.__name__, module.__file__ 以及 module.__loader__ 作为 repr 的输入，并将任何丢失的信息赋为默认值。

以下是所使用的确切规则:

如果模块具有 __spec__ 属性，其中的规格信息会被用来生成 repr。 被查询的属性有 “name”, “loader”, “origin” 和 “has_location” 等等。

如果模块具有 __file__ 属性，这会被用作模块 repr 的一部分。

如果模块没有 __file__ 但是有 __loader__ 且取值不为 None，则加载器的 repr 会被用作模块 repr 的一部分。

对于其他情况，仅在 repr 中使用模块的 __name__。

在 3.4 版更改: loader.module_repr() 已弃用，导入机制现在使用模块规格说明来生成模块 repr。

为了向后兼容 Python 3.3，如果加载器定义了 module_repr() 方法，则会在尝试上述两种方式之前先调用该方法来生成模块 repr。 但请注意此方法已弃用。

5.5. 基于路径的查找器

在之前已经提及，Python 带有几种默认的元路径查找器。 其中之一是 path based finder (PathFinder)，它会搜索包含一个 路径条目 列表的 import path。 每个路径条目指定一个用于搜索模块的位置。

基于路径的查找器自身并不知道如何进行导入。 它只是遍历单独的路径条目，将它们各自关联到某个知道如何处理特定类型路径的路径条目查找器。

默认的路径条目查找器集合实现了在文件系统中查找模块的所有语义，可处理多种特殊文件类型例如 Python 源码 (.py 文件)，Python 字节码 (.pyc 文件) 以及共享库 (例如 .so 文件)。 在标准库中 zipimport 模块的支持下，默认路径条目查找器还能处理所有来自 zip 文件的上述文件类型。

路径条目不必仅限于文件系统位置。 它们可以指向 URL、数据库查询或可以用字符串指定的任何其他位置。

基于路径的查找器还提供了额外的钩子和协议以便能扩展和定制可搜索路径条目的类型。 例如，如果你想要支持网络 URL 形式的路径条目，你可以编写一个实现 HTTP 语义在网络上查找模块的钩子。 这个钩子（可调用对象）应当返回一个支持下述协议的 path entry finder，以被用来获取一个专门针对来自网络的模块的加载器。

预先的警告：本节和上节都使用了 查找器 这一术语，并通过 meta path finder 和 path entry finder 两个术语来明确区分它们。 这两种类型的查找器非常相似，支持相似的协议，且在导入过程中以相似的方式运作，但关键的一点是要记住它们是有微妙差异的。 特别地，元路径查找器作用于导入过程的开始，主要是启动 sys.meta_path 遍历。

相比之下，路径条目查找器在某种意义上说是基于路径的查找器的实现细节，实际上，如果需要从 sys.meta_path 移除基于路径的查找器，并不会有任何路径条目查找器被发起调用。

5.5.1. 路径条目查找器

path based finder 会负责查找和加载通过 path entry 字符串来指定位置的 Python 模块和包。 多数路径条目所指定的是文件系统中的位置，但它们并不必受限于此。

作为一种元路径查找器，path based finder 实现了上文描述的 find_spec() 协议，但是它还对外公开了一些附加钩子，可被用来定制模块如何从 import path 查找和加载。

有三个变量由 path based finder, sys.path, sys.path_hooks 和 sys.path_importer_cache 所使用。 包对象的 __path__ 属性也会被使用。 它们提供了可用于定制导入机制的额外方式。

sys.path 包含一个提供模块和包搜索位置的字符串列表。 它初始化自 PYTHONPATH 环境变量以及多种其他特定安装和实现的默认设置。 sys.path 条目可指定的名称有文件系统中的目录、zip 文件和其他可用于搜索模块的潜在“位置”（参见 site 模块），例如 URL 或数据库查询等。 在 sys.path 中只能出现字符串和字节串；所有其他数据类型都会被忽略。 字节串条目使用的编码由单独的 路径条目查找器 来确定。

path based finder 是一种 meta path finder，因此导入机制会通过调用上文描述的基于路径的查找器的 find_spec() 方法来启动 import path 搜索。 当要向 find_spec() 传入 path 参数时，它将是一个可遍历的字符串列表 —— 通常为用来在其内部进行导入的包的 __path__ 属性。 如果 path 参数为 None，这表示最高层级的导入，将会使用 sys.path。

基于路径的查找器会迭代搜索路径中的每个条目，并且每次都查找与路径条目对应的 path entry finder (PathEntryFinder)。 因为这种操作可能很耗费资源（例如搜索会有 stat() 调用的开销），基于路径的查找器会维持一个缓存来将路径条目映射到路径条目查找器。 这个缓存放于 sys.path_importer_cache (尽管如此命名，但这个缓存实际存放的是查找器对象而非仅限于 importer 对象)。 通过这种方式，对特定 path entry 位置的 path entry finder 的高耗费搜索只需进行一次。 用户代码可以自由地从 sys.path_importer_cache 移除缓存条目，以强制基于路径的查找器再次执行路径条目搜索 3。

如果路径条目不存在于缓存中，基于路径的查找器会迭代 sys.path_hooks 中的每个可调用对象。 对此列表中的每个 路径条目钩子 的调用会带有一个参数，即要搜索的路径条目。 每个可调用对象或是返回可处理路径条目的 path entry finder，或是引发 ImportError。 基于路径的查找器使用 ImportError 来表示钩子无法找到与 path entry 相对应的 path entry finder。 该异常会被忽略并继续进行 import path 的迭代。 每个钩子应该期待接收一个字符串或字节串对象；字节串对象的编码由钩子决定（例如可以是文件系统使用的编码 UTF-8 或其它编码），如果钩子无法解码参数，它应该引发 ImportError。

如果 sys.path_hooks 迭代结束时没有返回 path entry finder，则基于路径的查找器 find_spec() 方法将在 sys.path_importer_cache 中存入 None (表示此路径条目没有对应的查找器) 并返回 None，表示此 meta path finder 无法找到该模块。

如果 sys.path_hooks 中的某个 path entry hook 可调用对象的返回值 是 一个 path entry finder，则以下协议会被用来向查找器请求一个模块的规格说明，并在加载该模块时被使用。

当前工作目录 – 由一个空字符串表示 – 的处理方式与 sys.path 中的其他条目略有不同。 首先，如果发现当前工作目录不存在，则 sys.path_importer_cache 中不会存放任何值。 其次，每个模块查找会对当前工作目录的值进行全新查找。 第三，由 sys.path_importer_cache 所使用并由 importlib.machinery.PathFinder.find_spec() 所返回的路径将是实际的当前工作目录而非空字符串。

5.5.2. 路径条目查找器协议

为了支持模块和已初始化包的导入，也为了给命名空间包提供组成部分，路径条目查找器必须实现 find_spec() 方法。

find_spec() takes two argument, the fully qualified name of the module being imported, and the (optional) target module. find_spec() returns a fully populated spec for the module. This spec will always have “loader” set (with one exception).

To indicate to the import machinery that the spec represents a namespace portion. the path entry finder sets “loader” on the spec to None and “submodule_search_locations” to a list containing the portion.

在 3.4 版更改: find_spec() 替代了 find_loader() 和 find_module()，后两者现在都已弃用，但会在 find_spec() 未定义时被使用。

较旧的路径条目查找器可能会实现这两个已弃用的方法中的一个而没有实现 find_spec()。 为保持向后兼容，这两个方法仍会被接受。 但是，如果在路径条目查找器上实现了 find_spec()，这两个遗留方法就会被忽略。

find_loader() 接受一个参数，即被导入模块的完整限定名称。 find_loader() 会返回一个二元组，其中第一项为加载器，第二项为一个命名空间 portion。 当第一项（即加载器）为 None 时，这意味着路径条目查找器虽然没有指定名称模块的加载器，但它知道该路径条目为指定名称模块提供了一个命名空间部分。 这几乎总是表明一种情况，即 Python 被要求导入一个并不以文件系统中的实体形式存在的命名空间包。 当一个路径条目查找器返回的加载器为 None 时，该二元组返回值的第二项必须为一个序列，不过它也可以为空。

如果 find_loader() 所返回加载器的值不为 None，则该部分会被忽略，而该加载器会自基于路径的查找器返回，终止对路径条目的搜索。

为了向后兼容其他导入协议的实现，许多路径条目查找器也同样支持元路径查找器所支持的传统 find_module() 方法。 但是路径条目查找器 find_module() 方法的调用绝不会带有 path 参数（它们被期望记录来自对路径钩子初始调用的恰当路径信息）。

路径条目查找器的 find_module() 方法已弃用，因为它不允许路径条目查找器为命名空间包提供部分。 如果 find_loader() 和 find_module() 同时存在于一个路径条目查找器中，导入系统将总是调用 find_loader() 而不选择 find_module()。

5.6. 替换标准导入系统

替换整个导入系统的最可靠机制是移除 sys.meta_path 的默认内容，,将其完全替换为自定义的元路径钩子。

一个可行的方式是仅改变导入语句的行为而不影响访问导入系统的其他 API，那么替换内置的 __import__() 函数可能就够了。 这种技巧也可以在模块层级上运用，即只在某个模块内部改变导入语句的行为。

To selectively prevent import of some modules from a hook early on the meta path (rather than disabling the standard import system entirely), it is sufficient to raise ModuleNotFoundError directly from find_spec() instead of returning None. The latter indicates that the meta path search should continue, while raising an exception terminates it immediately.

5.7. 有关 __main__ 的特殊事项

对于 Python 的导入系统来说 __main__ 模块是一个特殊情况。 正如在 另一节 中所述，__main__ 模块是在解释器启动时直接初始化的，与 sys 和 builtins 很类似。 但是，与那两者不同，它并不被严格归类为内置模块。 这是因为 __main__ 被初始化的方式依赖于发起调用解释器所附带的旗标和其他选项。

5.7.1. __main__.__spec__

根据 __main__ 被初始化的方式，__main__.__spec__ 会被设置相应值或是 None。

当 Python 附加 -m 选项启动时，__spec__ 会被设为相应模块或包的模块规格说明。 __spec__ 也会在 __main__ 模块作为执行某个目录，zip 文件或其它 sys.path 条目的一部分加载时被填充。

在 其余的情况 下 __main__.__spec__ 会被设为 None，因为用于填充 __main__ 的代码不直接与可导入的模块相对应:

交互型提示

-c 选项

从 stdin 运行

直接从源码或字节码文件运行

请注意在最后一种情况中 __main__.__spec__ 总是为 None，即使 文件从技术上说可以作为一个模块被导入。 如果想要让 __main__ 中的元数据生效，请使用 -m 开关。

还要注意即使是在 __main__ 对应于一个可导入模块且 __main__.__spec__ 被相应地设定时，它们仍会被视为 不同的 模块。 这是由于以下事实：使用 if __name__ == "__main__": 检测来保护的代码块仅会在模块被用来填充 __main__ 命名空间时而非普通的导入时被执行。

5.8. 开放问题项

XXX 最好是能增加一个图表。

XXX * (import_machinery.rst) 是否要专门增加一节来说明模块和包的属性，也许可以扩展或移植数据模型参考页中的相关条目？

XXX 库手册中的 runpy 和 pkgutil 等等应该都在页面顶端增加指向新的导入系统章节的“另请参阅”链接。

XXX 是否要增加关于初始化 __main__ 的不同方式的更多解释？

XXX 增加更多有关 __main__ 怪异/坑人特性的信息 (例如直接从 PEP 395 [https://www.python.org/dev/peps/pep-0395] 复制)。

5.9. 参考文献

导入机制自 Python 诞生之初至今已发生了很大的变化。 原始的 包规格说明 [https://www.python.org/doc/essays/packages/] 仍然可以查阅，但在撰写该文档之后许多相关细节已被修改。

原始的 sys.meta_path 规格说明见 PEP 302 [https://www.python.org/dev/peps/pep-0302]，后续的扩展说明见 PEP 420 [https://www.python.org/dev/peps/pep-0420]。

PEP 420 [https://www.python.org/dev/peps/pep-0420] 为 Python 3.3 引入了 命名空间包。 PEP 420 [https://www.python.org/dev/peps/pep-0420] 还引入了 find_loader() 协议作为 find_module() 的替代。

PEP 366 [https://www.python.org/dev/peps/pep-0366] 描述了新增的 __package__ 属性，用于在模块中的显式相对导入。

PEP 328 [https://www.python.org/dev/peps/pep-0328] 引入了绝对和显式相对导入，并初次提出了 __name__ 语义，最终由 PEP 366 [https://www.python.org/dev/peps/pep-0366] 为 __package__ 加入规范描述。

PEP 338 [https://www.python.org/dev/peps/pep-0338] 定义了将模块作为脚本执行。

PEP 451 [https://www.python.org/dev/peps/pep-0451] 在 spec 对象中增加了对每个模块导入状态的封装。 它还将加载器的大部分样板责任移交回导入机制中。 这些改变允许弃用导入系统中的一些 API 并为查找器和加载器增加一些新的方法。

备注

1

参见 types.ModuleType。

2

importlib 实现避免直接使用返回值。 而是通过在 sys.modules 中查找模块名称来获取模块对象。 这种方式的间接影响是被导入的模块可能在 sys.modules 中替换其自身。 这属于具体实现的特定行为，不保证能在其他 Python 实现中起作用。

3

在遗留代码中，有可能在 sys.path_importer_cache 中找到 imp.NullImporter 的实例。 建议将这些代码修改为使用 None 代替。 详情参见 Porting Python code。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

6. 表达式

本章将解释 Python 中组成表达式的各种元素的的含义。

语法注释: 在本章和后续章节中，会使用扩展 BNF 标注来描述语法而不是词法分析。 当（某种替代的）语法规则具有如下形式

name ::=  othername

并且没有给出语义，则这种形式的 name 在语法上与 othername 相同。

6.1. 算术转换

When a description of an arithmetic operator below uses the phrase “the numeric arguments are converted to a common type,” this means that the operator implementation for built-in types works as follows:

如果任一参数为复数，另一参数会被转换为复数；

否则，如果任一参数为浮点数，另一参数会被转换为浮点数；

否则，两者应该都为整数，不需要进行转换。

某些附加规则会作用于特定运算符（例如，字符串作为 ‘%’ 运算符的左运算参数）。 扩展必须定义它们自己的转换行为。

6.2. 原子

“原子”指表达式的最基本构成元素。 最简单的原子是标识符和字面值。 以圆括号、方括号或花括号包括的形式在语法上也被归类为原子。 原子的句法为:

atom      ::=  identifier | literal | enclosure
enclosure ::=  parenth_form | list_display | dict_display | set_display
               | generator_expression | yield_atom

6.2.1. 标识符（名称）

作为原子出现的标识符叫做名称。 请参看 标识符和关键字 一节了解其词法定义，以及 命名与绑定 获取有关命名与绑定的文档。

当名称被绑定到一个对象时，对该原子求值将返回相应对象。 当名称未被绑定时，尝试对其求值将引发 NameError 异常。

私有名称转换: 当以文本形式出现在类定义中的一个标识符以两个或更多下划线开头并且不以两个或更多下划线结尾，它会被视为该类的 私有名称。 私有名称会在为其生成代码之前被转换为一种更长的形式。 转换时会插入类名，移除打头的下划线再在名称前增加一个下划线。 例如，出现在一个名为 Ham 的类中的标识符 __spam 会被转换为 _Ham__spam。 这种转换独立于标识符所使用的相关句法。 如果转换后的名称太长（超过 255 个字符），可能发生由具体实现定义的截断。 如果类名仅由下划线组成，则不会进行转换。

6.2.2. 字面值

Python 支持字符串和字节串字面值，以及几种数字字面值:

literal ::=  stringliteral | bytesliteral
             | integer | floatnumber | imagnumber

对字面值求值将返回一个该值所对应类型的对象（字符串、字节串、整数、浮点数、复数）。 对于浮点数和虚数（复数）的情况，该值可能为近似值。 详情参见 字面值。

所有字面值都对应与不可变数据类型，因此对象标识的重要性不如其实际值。 多次对具有相同值的字面值求值（不论是发生在程序文本的相同位置还是不同位置）可能得到相同对象或是具有相同值的不同对象。

6.2.3. 带圆括号的形式

带圆括号的形式是包含在圆括号中的可选表达式列表。

parenth_form ::=  "(" [starred_expression] ")"

带圆括号的表达式列表将返回该表达式列表所产生的任何东西：如果该列表包含至少一个逗号，它会产生一个元组；否则，它会产生该表达式列表所对应的单一表达式。

An empty pair of parentheses yields an empty tuple object. Since tuples are immutable, the rules for literals apply (i.e., two occurrences of the empty tuple may or may not yield the same object).

请注意元组并不是由圆括号构建，实际起作用的是逗号操作符。 例外情况是空元组，这时圆括号 才是 必须的 — 允许在表达式中使用不带圆括号的 “空” 会导致歧义，并会造成常见输入错误无法被捕获。

6.2.4. 列表、集合与字典的显示

为了构建列表、集合或字典，Python 提供了名为“显示”的特殊句法，每个类型各有两种形式:

第一种是显式地列出容器内容

第二种是通过一组循环和筛选指令计算出来，称为 推导式。

推导式的常用句法元素为:

comprehension ::=  expression comp_for
comp_for      ::=  [ASYNC] "for" target_list "in" or_test [comp_iter]
comp_iter     ::=  comp_for | comp_if
comp_if       ::=  "if" expression_nocond [comp_iter]

The comprehension consists of a single expression followed by at least one for clause and zero or more for or if clauses. In this case, the elements of the new container are those that would be produced by considering each of the for or if clauses a block, nesting from left to right, and evaluating the expression to produce an element each time the innermost block is reached.

Note that the comprehension is executed in a separate scope, so names assigned to in the target list don’t “leak” into the enclosing scope.

Since Python 3.6, in an async def function, an async for clause may be used to iterate over a asynchronous iterator. A comprehension in an async def function may consist of either a for or async for clause following the leading expression, may contain additional for or async for clauses, and may also use await expressions. If a comprehension contains either async for clauses or await expressions it is called an asynchronous comprehension. An asynchronous comprehension may suspend the execution of the coroutine function in which it appears. See also PEP 530 [https://www.python.org/dev/peps/pep-0530].

6.2.5. 列表显示

列表显示是一个用方括号括起来的可能为空的表达式系列:

list_display ::=  "[" [starred_list | comprehension] "]"

列表显示会产生一个新的列表对象，其内容通过一系列表达式或一个推导式来指定。 当提供由逗号分隔的一系列表达式时，其元素会从左至右被求值并按此顺序放入列表对象。 当提供一个推导式时，列表会根据推导式所产生的结果元素进行构建。

6.2.6. 集合显示

集合显示是用花括号标明的，与字典显示的区别在于没有冒号分隔的键和值:

set_display ::=  "{" (starred_list | comprehension) "}"

集合显示会产生一个新的可变集合对象，其内容通过一系列表达式或一个推导式来指定。 当提供由逗号分隔的一系列表达式时，其元素会从左至右被求值并加入到集合对象。 当提供一个推导式时，集合会根据推导式所产生的结果元素进行构建。

空集合不能用 {} 来构建；该字面值所构建的是一个空字典。

6.2.7. 字典显示

字典显示是一个用花括号括起来的可能为空的键/数据对系列:

dict_display       ::=  "{" [key_datum_list | dict_comprehension] "}"
key_datum_list     ::=  key_datum ("," key_datum)* [","]
key_datum          ::=  expression ":" expression | "**" or_expr
dict_comprehension ::=  expression ":" expression comp_for

字典显示会产生一个新的字典对象。

如果给出一个由逗号分隔的键/数据对序列，它们会从左至右被求值以定义字典的条目：每个键对象会被用作在字典中存放相应数据的键。 这意味着你可以在键/数据对序列中多次指定相同的键，最终字典的值将由最后一次给出的键决定。

双星号 ** 表示 字典拆包。 它的操作数必须是一个 mapping。 每个映射项被会加入新的字典。 后续的值会替代先前的键/数据对和先前的字典拆包所设置的值。

3.5 新版功能: 拆包到字典显示，最初由 PEP 448 [https://www.python.org/dev/peps/pep-0448] 提出。

字典推导式与列表和集合推导式有所不同，它需要以冒号分隔的两个表达式，后面带上标准的 “for” 和 “if” 子句。 当推导式被执行时，作为结果的键和值元素会按它们的产生顺序被加入新的字典。

对键取值类型的限制已列在之前的 标准类型层级结构 一节中。 (总的说来，键的类型应该为 hashable，这就把所有可变对象都排除在外。) 重复键之间的冲突不会被检测；指定键所保存的最后一个数据 (即在显示中排最右边的文本) 为最终有效数据。

6.2.8. 生成器表达式

生成器表达式是用圆括号括起来的紧凑形式生成器标注。

generator_expression ::=  "(" expression comp_for ")"

生成器表达式会产生一个新的生成器对象。 其句法与推导式相同，区别在于它是用圆括号而不是用方括号或花括号括起来的。

Variables used in the generator expression are evaluated lazily when the __next__() method is called for the generator object (in the same fashion as normal generators). However, the leftmost for clause is immediately evaluated, so that an error produced by it can be seen before any other possible error in the code that handles the generator expression. Subsequent for clauses cannot be evaluated immediately since they may depend on the previous for loop. For example: (x*y for x in range(10) for y in bar(x)).

圆括号在只附带一个参数的调用中可以被省略。 详情参见 调用 一节。

Since Python 3.6, if the generator appears in an async def function, then async for clauses and await expressions are permitted as with an asynchronous comprehension. If a generator expression contains either async for clauses or await expressions it is called an asynchronous generator expression. An asynchronous generator expression yields a new asynchronous generator object, which is an asynchronous iterator (see 异步迭代器).

6.2.9. yield 表达式

yield_atom       ::=  "(" yield_expression ")"
yield_expression ::=  "yield" [expression_list | "from" expression]

yield 表达式在定义 generator 函数或是 asynchronous generator 的时候才会用到。 因此只能在函数定义的内部使用yield表达式。 在一个函数体内使用 yield 表达式会使这个函数变成一个生成器，并且在一个 async def 定义的函数体内使用 yield 表达式会让协程函数变成异步的生成器。 比如说:

def gen():  # defines a generator function
    yield 123

async def agen(): # defines an asynchronous generator function
    yield 123

下面是对生成器函数的描述，异步生成器函数会在 异步生成器函数 一节中单独介绍。

当一个生成器函数被调用的时候，它返回一个迭代器，称为生成器。然后这个生成器来控制生成器函数的执行。当这个生成器的某一个方法被调用的时候，生成器函数开始执行。这时会一直执行到第一个 yield 表达式，在此执行再次被挂起，给生成器的调用者返回 expression_list 的值。挂起后，我们说所有局部状态都被保留下来，包括局部变量的当前绑定，指令指针，内部求值栈和任何异常处理的状态。通过调用生成器的某一个方法，生成器函数继续执行。此时函数的运行就和 yield 表达式只是一个外部函数调用的情况完全一致。恢复后 yield 表达式的值取决于调用的哪个方法来恢复执行。 如果用的是 __next__() (通常通过语言内置的 for 或是 next() 来调用) 那么结果就是 None. 否则，如果用 send(), 那么结果就是传递给send方法的值。

所有这些使生成器函数与协程非常相似；它们 yield 多次，它们具有多个入口点，并且它们的执行可以被挂起。唯一的区别是生成器函数不能控制在它在 yield 后交给哪里继续执行；控制权总是转移到生成器的调用者。

在 try 结构中的任何位置都允许yield表达式。如果生成器在(因为引用计数到零或是因为被垃圾回收)销毁之前没有恢复执行，将调用生成器-迭代器的 close() 方法. close 方法允许任何挂起的 finally 子句执行。

当使用 yield from <expr> 时，它会将所提供的表达式视为一个子迭代器。 这个子迭代器产生的所有值都直接被传递给当前生成器方法的调用者。 通过 send() 传入的任何值以及通过 throw() 传入的任何异常如果有适当的方法则会被传给下层迭代器。 如果不是这种情况，那么 send() 将引发 AttributeError 或 TypeError，而 throw() 将立即引发所传入的异常。

When the underlying iterator is complete, the value attribute of the raised StopIteration instance becomes the value of the yield expression. It can be either set explicitly when raising StopIteration, or automatically when the sub-iterator is a generator (by returning a value from the sub-generator).

在 3.3 版更改: 添加 yield from <expr> 以委托控制流给一个子迭代器。

当yield表达式是赋值语句右侧的唯一表达式时，括号可以省略。

参见

PEP 255 [https://www.python.org/dev/peps/pep-0255] - 简单生成器

在 Python 中加入生成器和 yield 语句的提议。

PEP 342 [https://www.python.org/dev/peps/pep-0342] - 通过增强型生成器实现协程

增强生成器 API 和语法的提议，使其可以被用作简单的协程。

PEP 380 [https://www.python.org/dev/peps/pep-0380] - 委托给子生成器的语法

The proposal to introduce the yield_from syntax, making delegation to sub-generators easy.

PEP 525 [https://www.python.org/dev/peps/pep-0525] - 异步生成器

通过给协程函数加入生成器功能对 PEP 492 [https://www.python.org/dev/peps/pep-0492] 进行扩展的提议。

6.2.9.1. 生成器-迭代器的方法

这个子小节描述了生成器迭代器的方法。 它们可被用于控制生成器函数的执行。

请注意在生成器已经在执行时调用以下任何方法都会引发 ValueError 异常。

generator.__next__()

开始一个生成器函数的执行或是从上次执行的 yield 表达式位置恢复执行。 当一个生成器函数通过 __next__() 方法恢复执行时，当前的 yield 表达式总是取值为 None。 随后会继续执行到下一个 yield 表达式，其 expression_list 的值会返回给 __next__() 的调用者。 如果生成器没有产生下一个值就退出，则将引发 StopIteration 异常。

此方法通常是隐式地调用，例如通过 for 循环或是内置的 next() 函数。

generator.send(value)

恢复执行并向生成器函数“发送”一个值。 value 参数将成为当前 yield 表达式的结果。 send() 方法会返回生成器所产生的下一个值，或者如果生成器没有产生下一个值就退出则会引发 StopIteration。 当调用 send() 来启动生成器时，它必须以 None 作为调用参数，因为这时没有可以接收值的 yield 表达式。

generator.throw(type[, value[, traceback]])

在生成器暂停的位置引发 type 类型的异常，并返回该生成器函数所产生的下一个值。 如果生成器没有产生下一个值就退出，则将引发 StopIteration 异常。 如果生成器函数没有捕获传入的异常，或引发了另一个异常，则该异常会被传播给调用者。

generator.close()

在生成器函数暂停的位置引发 GeneratorExit。 如果之后生成器函数正常退出、关闭或引发 GeneratorExit (由于未捕获该异常) 则关闭并返回其调用者。 如果生成器产生了一个值，关闭会引发 RuntimeError。 如果生成器引发任何其他异常，它会被传播给调用者。 如果生成器已经由于异常或正常退出则 close() 不会做任何事。

6.2.9.2. 例子

这里是一个简单的例子，演示了生成器和生成器函数的行为:

>>> def echo(value=None):
...     print("Execution starts when 'next()' is called for the first time.")
...     try:
...         while True:
...             try:
...                 value = (yield value)
...             except Exception as e:
...                 value = e
...     finally:
...         print("Don't forget to clean up when 'close()' is called.")
...
>>> generator = echo(1)
>>> print(next(generator))
Execution starts when 'next()' is called for the first time.
1
>>> print(next(generator))
None
>>> print(generator.send(2))
2
>>> generator.throw(TypeError, "spam")
TypeError('spam',)
>>> generator.close()
Don't forget to clean up when 'close()' is called.

对于 yield from 的例子, 参见“Python 有什么新变化”中的 PEP 380: 委托给子生成器的语法。

6.2.9.3. 异步生成器函数

The presence of a yield expression in a function or method defined using async def further defines the function as a asynchronous generator function.

当一个异步生成器函数被调用时，它会返回一个名为异步生成器对象的异步迭代器。 此对象将在之后控制该生成器函数的执行。 异步生成器对象通常被用在协程函数的 async for 语句中，类似于在 for 语句中使用生成器对象。

调用异步生成器的方法之一将返回 awaitable 对象，执行会在此对象被等待时启动。 到那时，执行将前往第一个 yield 表达式，在那里它会再次暂停，将 expression_list 的值返回给等待中的协程。 与生成器一样，挂起意味着局部的所有状态会被保留，包括局部变量的当前绑定、指令的指针、内部求值的堆栈以及任何异常处理的状态。 当执行在等待异步生成器的方法返回下一个对象后恢复时，该函数可以从原状态继续进行，就仿佛 yield 表达式只是另一个外部调用。 恢复执行之后 yield 表达式的值取决于恢复执行所用的方法。 如果使用 __anext__() 则结果为 None。 否则的话，如果使用 asend() 则结果将是传递给该方法的值。

In an asynchronous generator function, yield expressions are allowed anywhere in a try construct. However, if an asynchronous generator is not resumed before it is finalized (by reaching a zero reference count or by being garbage collected), then a yield expression within a try construct could result in a failure to execute pending finally clauses. In this case, it is the responsibility of the event loop or scheduler running the asynchronous generator to call the asynchronous generator-iterator’s aclose() method and run the resulting coroutine object, thus allowing any pending finally clauses to execute.

为了能处理最终化，事件循环应该定义一个 终结器 函数，它接受一个异步生成器-迭代器且可能调用 aclose() 并执行协程。 这个 终结器 可能通过调用 sys.set_asyncgen_hooks() 来注册。 当首次迭代时，异步生成器-迭代器将保存已注册的 终结器 以便在最终化时调用。 有关For a reference example of a 终结器 方法的参考示例请查看 Lib/asyncio/base_events.py [https://github.com/python/cpython/tree/3.6/Lib/asyncio/base_events.py] 中实现的 asyncio.Loop.shutdown_asyncgens。

yield from <expr> 表达式如果在异步生成器函数中使用会引发语法错误。

6.2.9.4. 异步生成器-迭代器方法

这个子小节描述了异步生成器迭代器的方法，它们可被用于控制生成器函数的执行。

coroutine agen.__anext__()

Returns an awaitable which when run starts to execute the asynchronous generator or resumes it at the last executed yield expression. When an asynchronous generator function is resumed with a __anext__() method, the current yield expression always evaluates to None in the returned awaitable, which when run will continue to the next yield expression. The value of the expression_list of the yield expression is the value of the StopIteration exception raised by the completing coroutine. If the asynchronous generator exits without yielding another value, the awaitable instead raises an StopAsyncIteration exception, signalling that the asynchronous iteration has completed.

此方法通常是通过 async for 循环隐式地调用。

coroutine agen.asend(value)

返回一个可等待对象，它在运行时会恢复该异步生成器的执行。 与生成器的 send() 方法一样，此方法会“发送”一个值给异步生成器函数，其 value 参数会成为当前 yield 表达式的结果值。 asend() 方法所返回的可等待对象将返回生成器产生的下一个值，其值为所引发的 StopIteration，或者如果异步生成器没有产生下一个值就退出则引发 StopAsyncIteration。 当调用 asend() 来启动异步生成器时，它必须以 None 作为调用参数，因为这时没有可以接收值的 yield 表达式。

coroutine agen.athrow(type[, value[, traceback]])

Returns an awaitable that raises an exception of type type at the point where the asynchronous generator was paused, and returns the next value yielded by the generator function as the value of the raised StopIteration exception. If the asynchronous generator exits without yielding another value, an StopAsyncIteration exception is raised by the awaitable. If the generator function does not catch the passed-in exception, or raises a different exception, then when the awaitable is run that exception propagates to the caller of the awaitable.

coroutine agen.aclose()

返回一个可等待对象，它会在运行时向异步生成器函数暂停的位置抛入一个 GeneratorExit。 如果该异步生成器函数正常退出、关闭或引发 GeneratorExit (由于未捕获该异常) 则返回的可等待对象将引发 StopIteration 异常。 后续调用异步生成器所返回的任何其他可等待对象将引发 StopAsyncIteration 异常。 如果异步生成器产生了一个值，该可等待对象会引发 RuntimeError。 如果异步生成器引发任何其他异常，它会被传播给可等待对象的调用者。 如果异步生成器已经由于异常或正常退出则后续调用 aclose() 将返回一个不会做任何事的可等待对象。

6.3. 原型

原型代表编程语言中最紧密绑定的操作。 它们的句法如下:

primary ::=  atom | attributeref | subscription | slicing | call

6.3.1. 属性引用

属性引用是后面带有一个句点加一个名称的原型:

attributeref ::=  primary "." identifier

此原型必须求值为一个支持属性引用的类型的对象，多数对象都支持属性引用。 随后该对象会被要求产生以指定标识符为名称的属性。 这个产生过程可通过重载 __getattr__() 方法来自定义。 如果这个属性不可用，则将引发 AttributeError 异常。 否则的话，所产生对象的类型和值会根据该对象来确定。 对同一属性引用的多次求值可能产生不同的对象。

6.3.2. 抽取

抽取就是在序列（字符串、元组或列表）或映射（字典）对象中选择一项:

subscription ::=  primary "[" expression_list "]"

此原型必须求值为一个支持抽取操作的对象（例如列表或字典）。 用户定义的对象可通过定义 __getitem__() 方法来支持抽取操作。

对于内置对象，有两种类型的对象支持抽取操作:

如果原型为映射，表达式列表必须求值为一个以该映射的键为值的对象，抽取操作会在映射中选出该键所对应的值。（表达式列表为一个元组，除非其中只有一项。）

如果原型为序列，表达式列表必须求值为一个整数或一个切片（详情见下节）。

正式句法规则并没有在序列中设置负标号的特殊保留条款；但是，内置序列所提供的 __getitem__() 方法都可通过在索引中添加序列长度来解析负标号 (这样 x[-1] 会选出 x 中的最后一项)。 结果值必须为一个小于序列中项数的非负整数，抽取操作会选出标号为该值的项（从零开始数）。 由于对负标号和切片的支持存在于对象的 __getitem__() 方法，重载此方法的子类需要显式地添加这种支持。

字符串的项是字符。 字符不是单独的数据类型而是仅有一个字符的字符串。

6.3.3. 切片

切片就是在序列对象（字符串、元组或列表）中选择某个范围内的项。 切片可被用作表达式以及赋值或 del 语句的目标。 切片的句法如下:

slicing      ::=  primary "[" slice_list "]"
slice_list   ::=  slice_item ("," slice_item)* [","]
slice_item   ::=  expression | proper_slice
proper_slice ::=  [lower_bound] ":" [upper_bound] [ ":" [stride] ]
lower_bound  ::=  expression
upper_bound  ::=  expression
stride       ::=  expression

此处的正式句法中存在一点歧义：任何形似表达式列表的东西同样也会形似切片列表，因此任何抽取操作也可以被解析为切片。 为了不使句法更加复杂，于是通过定义将此情况解析为抽取优先于解析为切片来消除这种歧义（切片列表未包含正确的切片就属于此情况）。

切片的语义如下所述。 元型通过一个根据下面的切片列表来构造的键进行索引（与普通抽取一样使用 __getitem__() 方法）。 如果切片列表包含至少一个逗号，则键将是一个包含切片项转换的元组；否则的话，键将是单个切片项的转换。 切片项如为一个表达式，则其转换就是该表达式。 一个正确切片的转换就是一个切片对象（参见 标准类型层级结构 一节），该对象的 start, stop 和 step 属性将分别为表达式所给出的下界、上界和步长值，省略的表达式将用 None 来替换。

6.3.4. 调用

所谓调用就是附带可能为空的一系列 参数 来执行一个可调用对象 (例如 function):

call                 ::=  primary "(" [argument_list [","] | comprehension] ")"
argument_list        ::=  positional_arguments ["," starred_and_keywords]
                            ["," keywords_arguments]
                          | starred_and_keywords ["," keywords_arguments]
                          | keywords_arguments
positional_arguments ::=  ["*"] expression ("," ["*"] expression)*
starred_and_keywords ::=  ("*" expression | keyword_item)
                          ("," "*" expression | "," keyword_item)*
keywords_arguments   ::=  (keyword_item | "**" expression)
                          ("," keyword_item | "," "**" expression)*
keyword_item         ::=  identifier "=" expression

一个可选项为在位置和关键字参数后加上逗号而不影响语义。

此原型必须求值为一个可调用对象（用户定义的函数，内置函数，内置对象的方法，类对象，类实例的方法以及任何具有 __call__() 方法的对象都是可调用对象）。 所有参数表达式将在尝试调用前被求值。 请参阅 函数定义 一节了解正式的 parameter 列表句法。

如果存在关键字参数，它们会先通过以下操作被转换为位置参数。 首先，为正式参数创建一个未填充空位的列表. 如果有 N 个位置参数，则将它们放入前 N 个空位。 然后，对于每个关键字参数，使用标识符来确定其对应的空位（如果标识符与第一个正式参数名相同则使用第一个个空位，依此类推）。 如果空位已被填充，则会引发 TypeError 异常。 否则，将参数值放入空位进行填充（即使表达式为 None 也会填充空位）。 当所有参数处理完毕时，尚未填充的空位将用来自函数定义的相应默认值来填充。 （函数一旦定义其参数默认值就会被计算；因此，当列表或字典这类可变对象被用作默认值时，将会被所有未指定相应空位参数值的调用所共享；这种情况通常应当避免。） 如果任何一个未填充空位没有指定默认值，则会引发 TypeError 异常。 否则的话，已填充空位的列表会被作为调用的参数列表。

CPython implementation detail: 某些实现可能提供位置参数没有名称的内置函数，即使它们在文档说明的场合下有“命名”，因此不能以关键字形式提供参数。 在 CPython 中，以 C 编写并使用 PyArg_ParseTuple() 来解析其参数的函数实现就属于这种情况。

如果存在比正式参数空位多的位置参数，将会引发 TypeError 异常，除非有一个正式参数使用了 *identifier 句法；在此情况下，该正式参数将接受一个包含了多余位置参数的元组（如果没有多余位置参数则为一个空元组）。

如果任何关键字参数没有与之对应的正式参数名称，将会引发 TypeError 异常，除非有一个正式参数使用了 **identifier 句法，该正式参数将接受一个包含了多余关键字参数的字典（使用关键字作为键而参数值作为与键对应的值），如果没有多余关键字参数则为一个（新的）空字典。

如果函数调用中出现了 *expression 句法，expression 必须求值为一个 iterable。 来自该可迭代对象的元素会被当作是额外的位置参数。 对于 f(x1, x2, *y, x3, x4) 调用，如果 y 求值为一个序列 y1, …, yM，则它就等价于一个带有 M+4 个位置参数 x1, x2, y1, …, yM, x3, x4 的调用。

这样做的一个后果是虽然 *expression 句法可能出现于显式的关键字参数 之后，但它会在关键字参数（以及任何 **expression 参数 – 见下文） 之前 被处理。 因此:

>>> def f(a, b):
...     print(a, b)
...
>>> f(b=1, *(2,))
2 1
>>> f(a=1, *(2,))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() got multiple values for keyword argument 'a'
>>> f(1, *(2,))
1 2

在同一个调用中同时使用关键字参数和 *expression 句法并不常见，因此实际上这样的混淆不会发生。

如果函数调用中出现了 **expression 句法，expression 必须求值为一个 mapping，其内容会被当作是额外的关键字参数。 如果一个关键字已存在（作为显式关键字参数，或来自另一个拆包），则将引发 TypeError 异常。

使用 *identifier 或 **identifier 句法的正式参数不能被用作位置参数空位或关键字参数名称。

在 3.5 版更改: 函数调用接受任意数量的 * 和 ** 拆包，位置参数可能跟在可迭代对象拆包 (*) 之后，而关键字参数可能跟在字典拆包 (**) 之后。 由 PEP 448 [https://www.python.org/dev/peps/pep-0448] 发起最初提议。

除非引发了异常，调用总是会有返回值，返回值也可能为 None。 返回值的计算方式取决于可调用对象的类型。

如果类型为—

用户自定义函数:

函数的代码块会被执行，并向其传入参数列表。 代码块所做的第一件事是将正式形参绑定到对应参数；相关描述参见 函数定义 一节。 当代码块执行 return 语句时，由其指定函数调用的返回值。

内置函数或方法:

具体结果依赖于解释器；有关内置函数和方法的描述参见 内置函数。

类对象:

返回该类的一个新实例。

类实例方法:

调用相应的用户自定义函数，向其传入的参数列表会比调用的参数列表多一项：该实例将成为第一个参数。

类实例:

该类必须定义有 __call__() 方法；作用效果将等价于调用该方法。

6.4. await 表达式

挂起 coroutine 的执行以等待一个 awaitable 对象。 只能在 coroutine function 内部使用。

await_expr ::=  "await" primary

3.5 新版功能.

6.5. 幂运算符

幂运算符的绑定比在其左侧的一元运算符更紧密；但绑定紧密程度不及在其右侧的一元运算符。 句法如下:

power ::=  (await_expr | primary) ["**" u_expr]

因此，在一个未加圆括号的幂运算符和单目运算符序列中，运算符将从右向左求值（这不会限制操作数的求值顺序）: -1**2 结果将为 -1。

幂运算符与附带两个参数调用内置 pow() 函数具有相同的语义：结果为对其左参数进行其右参数所指定幂次的乘方运算。 数值参数会先转换为相同类型，结果也为转换后的类型。

对于 int 类型的操作数，结果将具有与操作数相同的类型，除非第二个参数为负数；在那种情况下，所有参数会被转换为 float 类型并输出 float 类型的结果。 例如，10**2 返回 100，而 10**-2 返回 0.01。

对 0.0 进行负数幂次运算将导致 ZeroDivisionError。 对负数进行分数幂次运算将返回 complex 数值。 （在早期版本中这将引发 ValueError。）

6.6. 一元算术和位运算

所有算术和位运算具有相同的优先级:

u_expr ::=  power | "-" u_expr | "+" u_expr | "~" u_expr

一元运算符 - (负) 会产生其数值参数的负值。

一元运算符 + (正) 会产生与其数值参数相同的值。

一元运算符 ~ (取反) 的结果是对其整数参数按位取反。 x 的按位取反被定义为 -(x+1)。 它只作用于整数。

在所有三种情况下，如果参数的类型不正确，将引发 TypeError 异常。

6.7. 二元算术运算符

二元算术运算符遵循传统的优先级。 请注意某些此类运算符也作用于特定的非数字类型。 除幂运算符以外只有两个优先级别，一个作用于乘法型运算符，另一个作用于加法型运算符:

m_expr ::=  u_expr | m_expr "*" u_expr | m_expr "@" m_expr |
            m_expr "//" u_expr | m_expr "/" u_expr |
            m_expr "%" u_expr
a_expr ::=  m_expr | a_expr "+" m_expr | a_expr "-" m_expr

运算符 * (乘) 将输出其参数的乘积。 两个参数或者必须都为数字，或者一个参数必须为整数而另一个参数必须为序列。 在前一种情况下，两个数字将被转换为相同类型然后相乘。 在后一种情况下，将执行序列的重复；重复因子为负数将输出空序列。

运算符 @ (at) 的目标是用于矩阵乘法。 没有内置 Python 类型实现此运算符。

3.5 新版功能.

运算符 / (除) 和 // (整除) 将输出其参数的商。 两个数字参数将先被转换为相同类型。 整数相除会输出一个 float 值，整数相整除的结果仍是整数；整除的结果就是使用 ‘floor’ 函数进行算术除法的结果。 除以零的运算将引发 ZeroDivisionError 异常。

运算符 % (模) 将输出第一个参数除以第二个参数的余数。 两个数字参数将先被转换为相同类型。 右参数为零将引发 ZeroDivisionError 异常。 参数可以为浮点数，例如 3.14%0.7 等于 0.34 (因为 3.14 等于 4*0.7 + 0.34)。 模运算符的结果的正负总是与第二个操作数一致（或是为零）；结果的绝对值一定小于第二个操作数的绝对值 1。

整除与模运算符的联系可通过以下等式说明: x == (x//y)*y + (x%y)。 此外整除与模也可通过内置函数 divmod() 来同时进行: divmod(x, y) == (x//y, x%y)。 2。

除了对数字执行模运算，运算符 % 还被字符串对象重载用于执行旧式的字符串格式化（又称插值）。 字符串格式化句法的描述参见 Python 库参考的 printf 风格的字符串格式化 一节。

整除运算符，模运算符和 divmod() 函数未被定义用于复数。 如果有必要可以使用 abs() 函数将其转换为浮点数。

运算符 + (addition) 将输出其参数的和。 两个参数或者必须都为数字，或者都为相同类型的序列。 在前一种情况下，两个数字将被转换为相同类型然后相加。 在后一种情况下，将执行序列拼接操作。

运算符 - (减) 将输出其参数的差。 两个数字参数将先被转换为相同类型。

6.8. 移位运算

移位运算的优先级低于算术运算:

shift_expr ::=  a_expr | shift_expr ("<<" | ">>") a_expr

这些运算符接受整数参数。 它们会将第一个参数左移或右移第二个参数所指定的比特位数。

右移 n 位被定义为被 pow(2,n) 整除。 左移 n 位被定义为乘以 pow(2,n)。

注解

In the current implementation, the right-hand operand is required to be at most sys.maxsize. If the right-hand operand is larger than sys.maxsize an OverflowError exception is raised.

6.9. 二元位运算

三种位运算具有各不相同的优先级:

and_expr ::=  shift_expr | and_expr "&" shift_expr
xor_expr ::=  and_expr | xor_expr "^" and_expr
or_expr  ::=  xor_expr | or_expr "|" xor_expr

运算符 & 对两个参数进行按位 AND (与) 运算，两个参数必须为整数。

运算符 ^ 对两个参数进行按位 XOR (异或) 运算，两个参数必须为整数。

运算符 | 对两个参数进行按位 OR (或) 运算，两个参数必须为整数。

6.10. 比较运算

与 C 不同，Python 中所有比较运算的优先级相同，低于任何算术、移位或位运算。 另一个与 C 不同之处在于 a < b < c 这样的表达式会按传统算术法则来解读:

comparison    ::=  or_expr (comp_operator or_expr)*
comp_operator ::=  "<" | ">" | "==" | ">=" | "<=" | "!="
                   | "is" ["not"] | ["not"] "in"

比较运算将输出布尔值: True 或 False。

比较运算可以任意串连，例如 x < y <= z 等价于 x < y and y <= z，除了 y 只被求值一次（但在两种写法下当 x < y 值为假时 z 都不会被求值）。

正式的说法是这样：如果 a, b, c, …, y, z 为表达式而 op1, op2, …, opN 为比较运算符，则 a op1 b op2 c ... y opN z 就等价于 a op1 b and b op2 c and ... y opN z，后者的不同之处只是每个表达式最多只被求值一次。

请注意 a op1 b op2 c 不意味着在 a 和 c 之间进行任何比较，因此，如 x < y > z 这样的写法是完全合法的（虽然也许不太好看）。

6.10.1. 值比较

运算符 <, >, ==, >=, <= 和 != 将比较两个对象的值。 两个对象不要求为相同类型。

对象、值与类型 一章已说明对象都有相应的值（还有类型和标识号）。 对象值在 Python 中是一个相当抽象的概念：例如，对象值并没有一个规范的访问方法。 而且，对象值并不要求具有特定的构建方式，例如由其全部数据属性组成等。 比较运算符实现了一个特定的对象值概念。 人们可以认为这是通过实现对象比较间接地定义了对象值。

由于所有类型都是 object 的（直接或间接）子类型，它们都从 object 继承了默认的比较行为。 类型可以通过实现 丰富比较方法 例如 __lt__() 来定义自己的比较行为，详情参见 基本定制。

默认的一致性比较 (== 和 !=) 是基于对象的标识号。 因此，具有相同标识号的实例一致性比较结果为相等，具有不同标识号的实例一致性比较结果为不等。 规定这种默认行为的动机是希望所有对象都应该是自反射的 (即 x is y 就意味着 x == y)。

次序比较 (<, >, <= 和 >=) 默认没有提供；如果尝试比较会引发 TypeError。 规定这种默认行为的原因是缺少与一致性比较类似的固定值。

按照默认的一致性比较行为，具有不同标识号的实例总是不相等，这可能不适合某些对象值需要有合理定义并有基于值的一致性的类型。 这样的类型需要定制自己的比较行为，实际上，许多内置类型都是这样做的。

以下列表描述了最主要内置类型的比较行为。

内置数值类型 (数字类型 — int, float, complex) 以及标准库类型 fractions.Fraction 和 decimal.Decimal 可进行类型内部和跨类型的比较，例外限制是复数不支持次序比较。 在类型相关的限制以内，它们会按数学（算法）规则正确进行比较且不会有精度损失。

The not-a-number values float('NaN') and Decimal('NaN') are special. They are identical to themselves (x is x is true) but are not equal to themselves (x == x is false). Additionally, comparing any number to a not-a-number value will return False. For example, both 3 < float('NaN') and float('NaN') < 3 will return False.

二进制码序列 (bytes 或 bytearray 的实例) 可进行类型内部和跨类型的比较。 它们使用其元素的数字值按字典顺序进行比较。

字符串 (str 的实例) 使用其字符的 Unicode 码位数字值 (内置函数 ord() 的结果) 按字典顺序进行比较。 3

字符串和二进制码序列不能直接比较。

序列 (tuple, list 或 range 的实例) 只可进行类型内部的比较，range 还有一个限制是不支持次序比较。 以上对象的跨类型一致性比较结果将是不相等，跨类型次序比较将引发 TypeError。

序列通过相应元素的比较进行字典列比较，并强制规定元素自反射性。

由于强制元素自反射性，多项集的比较将假定对于一个多项集元素 x, x == x 总是为真。 基于该假设，将首先比较元素标识号，并且仅会对不同元素执行元素比较。 如果元素是自反射的，这种方式会产生与严格元素比较相同的结果。 对于非自反射的元素，结果将不同于严格元素比较，并且可能会令人惊讶：例如当在列表中使用非自反射的非数字值时，将导致以下比较行为:

>>> nan = float('NaN')
>>> nan is nan
True
>>> nan == nan
False                 <-- the defined non-reflexive behavior of NaN
>>> [nan] == [nan]
True                  <-- list enforces reflexivity and tests identity first

内置多项集间的字典序比较规则如下:

两个多项集若要相等，它们必须为相同类型、相同长度，并且每对相应的元素都必须相等（例如，[1,2] == (1,2) 为假值，因为类型不同）。

对于支持次序比较的多项集，排序与其第一个不相等元素的排序相同（例如 [1,2,x] <= [1,2,y] 的值与``x <= y`` 相同）。 如果对应元素不存在，较短的多项集排序在前（例如 [1,2] < [1,2,3] 为真值）。

两个映射 (dict 的实例) 若要相等，必须当且仅当它们具有相同的 (键, 值) 对。 键和值的一致性比较强制规定自反射性。

次序比较 (<, >, <= 和 >=) 将引发 TypeError。

集合 (set 或 frozenset 的实例) 可进行类型内部和跨类型的比较。

它们将比较运算符定义为子集和超集检测。 这类关系没有定义完全排序（例如 {1,2} 和 {2,3} 两个集合不相等，即不为彼此的子集，也不为彼此的超集。 相应地，集合不适宜作为依赖于完全排序的函数的参数（例如如果给出一个集合列表作为 min(), max() 和 sorted() 的输入将产生未定义的结果）。

集合的比较强制规定其元素的自反射性。

大多数其他内置类型没有实现比较方法，因此它们会继承默认的比较行为。

在可能的情况下，用户定义类在定制其比较行为时应当遵循一些一致性规则:

相等比较应该是自反射的。 换句话说，相同的对象比较时应该相等:

x is y 意味着 x == y

比较应该是对称的。 换句话说，下列表达式应该有相同的结果:

x == y 和 y == x

x != y 和 y != x

x < y 和 y > x

x <= y 和 y >= x

比较应该是可传递的。 下列（简要的）例子显示了这一点:

x > y and y > z 意味着 x > z

x < y and y <= z 意味着 x < z

反向比较应该导致布尔值取反。 换句话说，下列表达式应该有相同的结果:

x == y 和 not x != y

x < y 和 not x >= y (对于完全排序)

x > y 和 not x <= y (对于完全排序)

最后两个表达式适用于完全排序的多项集（即序列而非集合或映射）。 另请参阅 total_ordering() 装饰器。

hash() 的结果应该与是否相等一致。 相等的对象应该或者具有相同的哈希值，或者标记为不可哈希。

Python 并不强制要求这些一致性规则。 实际上，非数字值就是一个不遵循这些规则的例子。

6.10.2. 成员检测运算

The operators in and not in test for membership. x in s evaluates to True if x is a member of s, and False otherwise. x not in s returns the negation of x in s. All built-in sequences and set types support this as well as dictionary, for which in tests whether the dictionary has a given key. For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression x in y is equivalent to any(x is e or x == e for e in y).

对于字符串和字节串类型来说，当且仅当 x 是 y 的子串时 x in y 为 True。 一个等价的检测是 y.find(x) != -1。 空字符串总是被视为任何其他字符串的子串，因此 "" in "abc" 将返回 True。

对于定义了 __contains__() 方法的用户自定义类来说，如果 y.__contains__(x) 返回真值则 x in y 返回 True，否则返回 False。

For user-defined classes which do not define __contains__() but do define __iter__(), x in y is True if some value z with x == z is produced while iterating over y. If an exception is raised during the iteration, it is as if in raised that exception.

Lastly, the old-style iteration protocol is tried: if a class defines __getitem__(), x in y is True if and only if there is a non-negative integer index i such that x == y[i], and all lower integer indices do not raise IndexError exception. (If any other exception is raised, it is as if in raised that exception).

The operator not in is defined to have the inverse true value of in.

6.10.3. 标识号比较

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. Object identity is determined using the id() function. x is not y yields the inverse truth value. 4

6.11. 布尔运算

or_test  ::=  and_test | or_test "or" and_test
and_test ::=  not_test | and_test "and" not_test
not_test ::=  comparison | "not" not_test

在执行布尔运算的情况下，或是当表达式被用于流程控制语句时，以下值会被解析为假值: False, None, 所有类型的数字零，以及空字符串和空容器（包括字符串、元组、列表、字典、集合与冻结集合）。 所有其他值都会被解析为真值。 用户自定义对象可通过提供 __bool__() 方法来定制其逻辑值。

运算符 not 将在其参数为假值时产生 True，否则产生 False。

表达式 x and y 首先对 x 求值；如果 x 为假则返回该值；否则对 y 求值并返回其结果值。

表达式 x or y 首先对 x 求值；如果 x 为真则返回该值；否则对 y 求值并返回其结果值。

请注意 and 和 or 都不限制其返回的值和类型必须为 False 和 True，而是返回最终求值的参数。 此行为是有必要的，例如假设 s 为一个当其为空时应被替换为某个默认值的字符串，表达式 s or 'foo' 将产生希望的值。 由于 not 必须创建一个新值，不论其参数为何种类型它都会返回一个布尔值（例如，not 'foo' 结果为 False 而非 ''。）

6.12. 条件表达式

conditional_expression ::=  or_test ["if" or_test "else" expression]
expression             ::=  conditional_expression | lambda_expr
expression_nocond      ::=  or_test | lambda_expr_nocond

条件表达式（有时称为“三元运算符”）在所有 Python 运算中具有最低的优先级。

表达式 x if C else y 首先是对条件 C 而非 x 求值。 如果 C 为真，x 将被求值并返回其值；否则将对 y 求值并返回其值。

请参阅 PEP 308 [https://www.python.org/dev/peps/pep-0308] 了解有关条件表达式的详情。

6.13. lambda 表达式

lambda_expr        ::=  "lambda" [parameter_list] ":" expression
lambda_expr_nocond ::=  "lambda" [parameter_list] ":" expression_nocond

lambda 表达式（有时称为 lambda 构型）被用于创建匿名函数。 表达式 lambda parameters: expression 会产生一个函数对象 。 该未命名对象的行为类似于用以下方式定义的函数:

def <lambda>(parameters):
    return expression

请参阅 函数定义 了解有关参数列表的句法。 请注意通过 lambda 表达式创建的函数不能包含语句或标注。

6.14. 表达式列表

expression_list    ::=  expression ("," expression)* [","]
starred_list       ::=  starred_item ("," starred_item)* [","]
starred_expression ::=  expression | (starred_item ",")* [starred_item]
starred_item       ::=  expression | "*" or_expr

除了作为列表或集合显示的一部分，包含至少一个逗号的表达式列表将生成一个元组。 元组的长度就是列表中表达式的数量。 表达式将从左至右被求值。

一个星号 * 表示 可迭代拆包。 其操作数必须为一个 iterable。 该可迭代对象将被拆解为迭代项的序列，并被包含于在拆包位置上新建的元组、列表或集合之中。

3.5 新版功能: 表达式列表中的可迭代对象拆包，最初由 PEP 448 [https://www.python.org/dev/peps/pep-0448] 提出。

末尾的逗号仅在创建单独元组 (或称 单例) 时需要；在所有其他情况下都是可选项。 没有末尾逗号的单独表达式不会创建一个元组，而是产生该表达式的值。 （要创建一个空元组，应使用一对内容为空的圆括号: ()。）

6.15. 求值顺序

Python 按从左至右的顺序对表达式求值。 但注意在对赋值操作求值时，右侧会先于左侧被求值。

在以下几行中，表达式将按其后缀的算术优先顺序被求值。:

expr1, expr2, expr3, expr4
(expr1, expr2, expr3, expr4)
{expr1: expr2, expr3: expr4}
expr1 + expr2 * (expr3 - expr4)
expr1(expr2, expr3, *expr4, **expr5)
expr3, expr4 = expr1, expr2

6.16. 运算符优先级

下表对 Python 中运算符的优先顺序进行了总结，从最低优先级（最后绑定）到最高优先级（最先绑定）。 相同单元格内的运算符具有相同优先级。 除非句法显式地给出，否则运算符均指二元运算。 相同单元格内的运算符均从左至右分组（除了幂运算是从右至左分组）。

请注意比较、成员检测和标识号检测均为相同优先级，并具有如 比较运算 一节所描述的从左至右串连特性。

运算符

描述

lambda

lambda 表达式

if – else

条件表达式

or

布尔逻辑或 OR

and

布尔逻辑与 AND

not x

布尔逻辑非 NOT

in, not in, is, is not, <, <=, >, >=, !=, ==

比较运算，包括成员检测和标识号检测

|

按位或 OR

^

按位异或 XOR

&

按位与 AND

<<, >>

移位

+, -

加和减

*, @, /, //, %

乘，矩阵乘，除，整除，取余 5

+x, -x, ~x

正，负，按位非 NOT

**

乘方 6

await x

await 表达式

x[index], x[index:index], x(arguments...), x.attribute

抽取，切片，调用，属性引用

(expressions...), [expressions...], {key: value...}, {expressions...}

Binding or tuple display, list display, dictionary display, set display

备注

1

虽然 abs(x%y) < abs(y) 在数学中必为真，但对于浮点数而言，由于舍入的存在，其在数值上未必为真。 例如，假设在某个平台上的 Python 浮点数为一个 IEEE 754 双精度数值，为了使 -1e-100 % 1e100 具有与 1e100 相同的正负性，计算结果将是 -1e-100 + 1e100，这在数值上正好等于 1e100。 函数 math.fmod() 返回的结果则会具有与第一个参数相同的正负性，因此在这种情况下将返回 -1e-100。 何种方式更适宜取决于具体的应用。

2

如果 x 恰好非常接近于 y 的整数倍，则由于舍入的存在 x//y 可能会比 (x-x%y)//y 大。 在这种情况下，Python 会返回后一个结果，以便保持令 divmod(x,y)[0] * y + x % y 尽量接近 x.

3

Unicode 标准明确区分 码位 (例如 U+0041) 和 抽象字符 (例如 “大写拉丁字母 A”)。 虽然 Unicode 中的大多数抽象字符都只用一个码位来代表，但也存在一些抽象字符可使用由多个码位组成的序列来表示。 例如，抽象字符 “带有下加符的大写拉丁字母 C” 可以用 U+00C7 码位上的单个 预设字符 来表示，也可以用一个 U+0043 码位上的 基础字符 (大写拉丁字母 C) 加上一个 U+0327 码位上的 组合字符 (组合下加符) 组成的序列来表示。

对于字符串，比较运算符会按 Unicode 码位级别进行比较。 这可能会违反人类的直觉。 例如，"\u00C7" == "\u0043\u0327" 为 False，虽然两个字符串都代表同一个抽象字符 “带有下加符的大写拉丁字母 C”。

要按抽象字符级别（即对人类来说更直观的方式）对字符串进行比较，应使用 unicodedata.normalize()。

4

由于存在自动垃圾收集、空闲列表以及描述器的动态特性，你可能会注意到在特定情况下使用 is 运算符会出现看似不正常的行为，例如涉及到实例方法或常量之间的比较时就是如此。 更多信息请查看有关它们的文档。

5

% 运算符也被用于字符串格式化；在此场合下会使用同样的优先级。

6

幂运算符 ** 绑定的紧密程度低于在其右侧的算术或按位一元运算符，也就是说 2**-1 为 0.5。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

7. 简单语句

简单语句由一个单独的逻辑行构成。 多条简单语句可以存在于同一行内并以分号分隔。 简单语句的句法为:

simple_stmt ::=  expression_stmt
                 | assert_stmt
                 | assignment_stmt
                 | augmented_assignment_stmt
                 | annotated_assignment_stmt
                 | pass_stmt
                 | del_stmt
                 | return_stmt
                 | yield_stmt
                 | raise_stmt
                 | break_stmt
                 | continue_stmt
                 | import_stmt
                 | future_stmt
                 | global_stmt
                 | nonlocal_stmt

7.1. 表达式语句

表达式语句用于计算和写入值（大多是在交互模式下），或者（通常情况）调用一个过程 (过程就是不返回有意义结果的函数；在 Python 中，过程的返回值为 None)。 表达式语句的其他使用方式也是允许且有特定用处的。 表达式语句的句法为:

expression_stmt ::=  starred_expression

表达式语句会对指定的表达式列表（也可能为单一表达式）进行求值。

在交互模式下，如果结果值不为 None，它会通过内置的 repr() 函数转换为一个字符串，该结果字符串将以单独一行的形式写入标准输出（例外情况是如果结果为 None，则该过程调用不产生任何输出。）

7.2. 赋值语句

赋值语句用于将名称（重）绑定到特定值，以及修改属性或可变对象的成员项:

assignment_stmt ::=  (target_list "=")+ (starred_expression | yield_expression)
target_list     ::=  target ("," target)* [","]
target          ::=  identifier
                     | "(" [target_list] ")"
                     | "[" [target_list] "]"
                     | attributeref
                     | subscription
                     | slicing
                     | "*" target

(请参阅 原型 一节了解 属性引用, 抽取 和 切片 的句法定义。)

赋值语句会对指定的表达式列表进行求值（注意这可能为单一表达式或是由逗号分隔的列表，后者将产生一个元组）并将单一结果对象从左至右逐个赋值给目标列表。

赋值是根据目标（列表）的格式递归地定义的。 当目标为一个可变对象（属性引用、抽取或切片）的组成部分时，该可变对象必须最终执行赋值并决定其有效性，如果赋值操作不可接受也可能引发异常。 各种类型可用的规则和引发的异常通过对象类型的定义给出（参见 标准类型层级结构 一节）。

对象赋值的目标对象可以包含于圆括号或方括号内，具体操作按以下方式递归地定义。

如果目标列表为后面不带逗号、可以包含于圆括号内的单一目标，则将对象赋值给该目标。

否则：该对象必须为具有与目标列表相同项数的可迭代对象，这些项将按从左至右的顺序被赋值给对应的目标。

如果目标列表包含一个带有星号前缀的目标，这称为“加星”目标：则该对象至少必须为与目标列表项数减一相同项数的可迭代对象。 该可迭代对象前面的项将按从左至右的顺序被赋值给加星目标之前的目标。 该可迭代对象末尾的项将被赋值给加星目标之后的目标。 然后该可迭代对象中剩余项的列表将被赋值给加星目标（该列表可以为空）。

否则：该对象必须为具有与目标列表相同项数的可迭代对象，这些项将按从左至右的顺序被赋值给对应的目标。

对象赋值给单个目标的操作按以下方式递归地定义。

如果目标为标识符（名称）:

如果该名称未出现于当前代码块的 global 或 nonlocal 语句中：该名称将被绑定到当前局部命名空间的对象。

否则：该名称将被分别绑定到全局命名空间或由 nonlocal 所确定的外层命名空间的对象。

如果该名称已经被绑定则将被重新绑定。 这可能导致之前被绑定到该名称的对象的引用计数变为零，造成该对象进入释放过程并调用其析构器（如果存在）。

如果该对象为属性引用：引用中的原型表达式会被求值。 它应该产生一个具有可赋值属性的对象；否则将引发 TypeError。 该对象会被要求将可赋值对象赋值给指定的属性；如果它无法执行赋值，则会引发异常 (通常应为 AttributeError 但并不强制要求)。

注意：如果该对象为类实例并且属性引用在赋值运算符的两侧都出现，则右侧表达式 a.x 可以访问实例属性或（如果实例属性不存在）类属性。 左侧目标 a.x 将总是设定为实例属性，并在必要时创建该实例属性。 因此，a.x 的两次出现不一定指向相同的属性：如果右侧表达式指向一个类属性，则左侧表达式会创建一个新的实例属性作为赋值的目标:

class Cls:
    x = 3             # class variable
inst = Cls()
inst.x = inst.x + 1   # writes inst.x as 4 leaving Cls.x as 3

此描述不一定作用于描述器属性，例如通过 property() 创建的特征属性。

如果目标为一个抽取项：引用中的原型表达式会被求值。 它应当产生一个可变序列对象（例如列表）或一个映射对象（例如字典）。 接下来，该抽取表达式会被求值。

如果原型为一个可变序列对象（例如列表），抽取应产生一个整数。 如其为负值，则再加上序列长度。 结果值必须为一个小于序列长度的非负整数，序列将把被赋值对象赋值给该整数指定索引号的项。 如果索引超出范围，将会引发 IndexError (给被抽取序列赋值不能向列表添加新项)。

如果原型为一个映射对象（例如字典），抽取必须具有与该映射的键类型相兼容的类型，然后映射中会创建一个将抽取映射到被赋值对象的键/值对。 这可以是替换一个现有键/值对并保持相同键值，也可以是插入一个新键/值对（如果具有相同值的键不存在）。

对于用户定义对象，会调用 __setitem__() 方法并附带适当的参数。

如果目标为一个切片：引用中的原型表达式会被求值。 它应当产生一个可变序列对象（例如列表）。 被赋值对象应当是一个相同类型的序列对象。 接下来，下界与上界表达式如果存在的话将被求值；默认值分别为零和序列长度。 上下边界值应当为整数。 如果某一边界为负值，则会加上序列长度。 求出的边界会被裁剪至介于零和序列长度的开区间中。 最后，将要求序列对象以被赋值序列的项替换该切片。 切片的长度可能与被赋值序列的长度不同，这会在目标序列允许的情况下改变目标序列的长度。

CPython implementation detail: 在当前实现中，目标的句法被当作与表达式的句法相同，无效的句法会在代码生成阶段被拒绝，导致不太详细的错误信息。

虽然赋值的定义意味着左手边与右手边的重叠是“同时”进行的（例如 a, b = b, a 会交换两个变量的值），但在赋值给变量的多项集 之内 的重叠是从左至右进行的，这有时会令人混淆。 例如，以下程序将会打印出 [0, 2]:

x = [0, 1]
i = 0
i, x[i] = 1, 2         # i is updated, then x[i] is updated
print(x)

参见

PEP 3132 [https://www.python.org/dev/peps/pep-3132] - 扩展的可迭代对象拆包

对 *target 特性的规范说明。

7.2.1. 增强赋值语句

增强赋值语句就是在单个语句中将二元运算和赋值语句合为一体:

augmented_assignment_stmt ::=  augtarget augop (expression_list | yield_expression)
augtarget                 ::=  identifier | attributeref | subscription | slicing
augop                     ::=  "+=" | "-=" | "*=" | "@=" | "/=" | "//=" | "%=" | "**="
                               | ">>=" | "<<=" | "&=" | "^=" | "|="

（请参阅 原型 一节了解最后三种符号的句法定义。）

增强赋值语句将对目标和表达式列表求值（与普通赋值语句不同的是，前者不能为可迭代对象拆包），对两个操作数相应类型的赋值执行指定的二元运算，并将结果赋值给原始目标。 目标仅会被求值一次。

增强赋值语句例如 x += 1 可以改写为 x = x + 1 获得类似但并非完全等价的效果。 在增强赋值的版本中，x 仅会被求值一次。 而且，在可能的情况下，实际的运算是 原地 执行的，也就是说并不是创建一个新对象并将其赋值给目标，而是直接修改原对象。

不同于普通赋值，增强赋值会在对右手边求值 之前 对左手边求值。 例如，a[i] += f(x) 首先查找 a[i]，然后对 f(x) 求值并执行加法操作，最后将结果写回到 a[i]。

除了在单个语句中赋值给元组和多个目标的例外情况，增强赋值语句的赋值操作处理方式与普通赋值相同。 类似地，除了可能存在 原地 操作行为的例外情况，增强赋值语句执行的二元运算也与普通二元运算相同。

对于属性引用类目标，针对常规赋值的 关于类和实例属性的警告 也同样适用。

7.2.2. 带标注的赋值语句

Annotation assignment is the combination, in a single statement, of a variable or attribute annotation and an optional assignment statement:

annotated_assignment_stmt ::=  augtarget ":" expression ["=" expression]

与普通 赋值语句 的差别在于仅有单个目标且仅有单个右手边的值才被允许。

对于将简单名称作为赋值目标的情况，如果是在类或模块作用域中，标注会被求值并存入一个特殊的类或模块属性 __annotations__ 中，这是一个将变量名称（如为私有会被移除）映射到被求值标注的字典。 此属性为可写并且在类或模块体开始执行时如果静态地发现标注就会自动创建。

对于将表达式作为赋值目标的情况，如果是在类或模块作用域中，标注会被求值，但不会保存。

如果一个名称在函数作用域内被标注，则该名称为该作用域的局部变量。 标注绝不会在函数作用域内被求值和保存。

如果存在右手边，带标注的赋值会在对标注求值之前（如果适用）执行实际的赋值。 如果用作表达式目标的右手边不存在，则解释器会对目标求值，但最后的 __setitem__() 或 __setattr__() 调用除外。

参见

PEP 526 [https://www.python.org/dev/peps/pep-0526] - 变量标注的语法

该提议增加了标注变量（也包括类变量和实例变量）类型的语法，而不再是通过注释来进行表达。

PEP 484 [https://www.python.org/dev/peps/pep-0484] - 类型提示

该提议增加了 typing 模块以便为类型标注提供标准句法，可被静态分析工具和 IDE 所使用。

7.3. The assert statement

assert 语句是在程序中插入调试性断言的简便方式:

assert_stmt ::=  "assert" expression ["," expression]

简单形式 assert expression 等价于

if __debug__:
    if not expression: raise AssertionError

扩展形式 assert expression1, expression2 等价于

if __debug__:
    if not expression1: raise AssertionError(expression2)

以上等价形式假定 __debug__ 和 AssertionError 指向具有指定名称的内置变量。 在当前实现中，内置变量 __debug__ 在正常情况下为 True，在请求优化时为 False (对应命令行选项为 -O)。 如果在编译时请求优化，当前代码生成器不会为 assert 语句发出任何代码。 请注意不必在错误信息中包含失败表达式的源代码；它会被作为栈追踪的一部分被显示。

赋值给 __debug__ 是非法的。 该内置变量的值会在解释器启动时确定。

7.4. The pass statement

pass_stmt ::=  "pass"

pass 是一个空操作 — 当它被执行时，什么都不发生。 它适合当语法上需要一条语句但并不需要执行任何代码时用来临时占位，例如:

def f(arg): pass    # a function that does nothing (yet)

class C: pass       # a class with no methods (yet)

7.5. The del statement

del_stmt ::=  "del" target_list

删除是递归定义的，与赋值的定义方式非常类似。 此处不再详细说明，只给出一些提示。

目标列表的删除将从左至右递归地删除每一个目标。

名称的删除将从局部或全局命名空间中移除该名称的绑定，具体作用域的确定是看该名称是否有在同一代码块的 global 语句中出现。 如果该名称未被绑定，将会引发 NameError。

属性引用、抽取和切片的删除会被传递给相应的原型对象；删除一个切片基本等价于赋值为一个右侧类型的空切片（但即便这一点也是由切片对象决定的）。

在 3.2 版更改: 在之前版本中，如果一个名称作为被嵌套代码块中的自由变量出现，则将其从局部命名空间中删除是非法的。

7.6. The return statement

return_stmt ::=  "return" [expression_list]

return 在语法上只会出现于函数定义所嵌套的代码，不会出现于类定义所嵌套的代码。

如果提供了表达式列表，它将被求值，否则以 None 替代。

return 会离开当前函数调用，并以表达式列表 (或 None) 作为返回值。

When return passes control out of a try statement with a finally clause, that finally clause is executed before really leaving the function.

在一个生成器函数中，return 语句表示生成器已完成并将导致 StopIteration 被引发。 返回值（如果有的话）会被当作一个参数用来构建 StopIteration 并成为 StopIteration.value 属性。

In an asynchronous generator function, an empty return statement indicates that the asynchronous generator is done and will cause StopAsyncIteration to be raised. A non-empty return statement is a syntax error in an asynchronous generator function.

7.7. The yield statement

yield_stmt ::=  yield_expression

yield 语句在语义上等同于 yield 表达式。 yield 语句可用来省略在使用等效的 yield 表达式语句时所必须的圆括号。 例如，以下 yield 语句

yield <expr>
yield from <expr>

等同于以下 yield 表达式语句

(yield <expr>)
(yield from <expr>)

yield 表达式和语句仅在定义 generator 函数时使用，并且仅被用于生成器函数的函数体内部。 在函数定义中使用 yield 就足以使得该定义创建的是生成器函数而非普通函数。

有关 yield 语义的完整细节请参看 yield 表达式 一节。

7.8. The raise statement

raise_stmt ::=  "raise" [expression ["from" expression]]

如果不带表达式，raise 会重新引发当前作用域内最后一个激活的异常。 如果当前作用域内没有激活的异常，将会引发 RuntimeError 来提示错误。

否则的话，raise 会将第一个表达式求值为异常对象。 它必须为 BaseException 的子类或实例。 如果它是一个类，当需要时会通过不带参数地实例化该类来获得异常的实例。

异常的 类型 为异常实例的类，值 为实例本身。

当异常被引发时通常会自动创建一个回溯对象并将其关联到可写的 __traceback__ 属性。 你可以创建一个异常并同时使用 with_traceback() 异常方法（该方法将返回同一异常实例，并将回溯对象设为其参数）设置自己的回溯，就像这样:

raise Exception("foo occurred").with_traceback(tracebackobj)

from 子句用于异常串连：如果有该子句，则第二个 表达式 必须为另一个异常或实例，它将作为可写的 __cause__ 属性被关联到所引发的异常。 如果引发的异常未被处理，两个异常都将被打印出来:

>>> try:
...     print(1 / 0)
... except Exception as exc:
...     raise RuntimeError("Something bad happened") from exc
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
RuntimeError: Something bad happened

如果一个异常在异常处理器或 finally clause: 中被引发，类似的机制会隐式地发挥作用，之前的异常将被关联到新异常的 __context__ 属性:

>>> try:
...     print(1 / 0)
... except:
...     raise RuntimeError("Something bad happened")
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
RuntimeError: Something bad happened

异常串连可通过在 from 子句中指定 None 来显式地加以抑制:

>>> try:
...     print(1 / 0)
... except:
...     raise RuntimeError("Something bad happened") from None
...
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
RuntimeError: Something bad happened

有关异常的更多信息可在 异常 一节查看，有关处理异常的信息可在 The try statement 一节查看。

在 3.3 版更改: None 现在允许被用作 raise X from Y 中的 Y。

3.3 新版功能: 使用 __suppress_context__ 属性来抑制异常上下文的自动显示。

7.9. The break statement

break_stmt ::=  "break"

break 在语法上只会出现于 for 或 while 循环所嵌套的代码，但不会出现于该循环内部的函数或类定义所嵌套的代码。

It terminates the nearest enclosing loop, skipping the optional else clause if the loop has one.

如果一个 for 循环被 break 所终结，该循环的控制目标会保持其当前值。

When break passes control out of a try statement with a finally clause, that finally clause is executed before really leaving the loop.

7.10. The continue statement

continue_stmt ::=  "continue"

continue 在语法上只会出现于 for 或 while 循环所嵌套的代码，但不会出现于该循环内部的函数或类定义或者 finally 子句所嵌套的代码。 它会继续执行最近的外层循环的下一个轮次。

When continue passes control out of a try statement with a finally clause, that finally clause is executed before really starting the next loop cycle.

7.11. The import statement

import_stmt     ::=  "import" module ["as" identifier] ("," module ["as" identifier])*
                     | "from" relative_module "import" identifier ["as" identifier]
                     ("," identifier ["as" identifier])*
                     | "from" relative_module "import" "(" identifier ["as" identifier]
                     ("," identifier ["as" identifier])* [","] ")"
                     | "from" module "import" "*"
module          ::=  (identifier ".")* identifier
relative_module ::=  "."* module | "."+

基本的 import 语句（不带 from 子句）会分两步执行:

查找一个模块，如果有必要还会加载并初始化模块。

在局部命名空间中为 import 语句发生位置所处的作用域定义一个或多个名称。

当语句包含多个子句（由逗号分隔）时这两个步骤将对每个子句分别执行，如同这些子句被分成独立的 import 语句一样。

第一个步骤即查找和加载模块的详情 导入系统 一节中有更详细的描述，其中也描述了可被导入的多种类型的包和模块，以及可用于定制导入系统的所有钩子对象。 请注意这一步如果失败，则可能说明模块无法找到，或者 是在初始化模块，包括执行模块代码期间发生了错误。

如果成功获取到请求的模块，则可以通过以下三种方式一之在局部命名空间中使用它:

If the module name is followed by as, then the name following as is bound directly to the imported module.

如果没有指定其他名称，且被导入的模块为最高层级模块，则模块的名称将被绑定到局部命名空间作为对所导入模块的引用。

如果被导入的模块 不是 最高层级模块，则包含该模块的最高层级包的名称将被绑定到局部命名空间作为对该最高层级包的引用。 所导入的模块必须使用其完整限定名称来访问而不能直接访问。

from 形式使用的过程略微繁复一些:

查找 from 子句中指定的模块，如有必要还会加载并初始化模块；

对于 import 子句中指定的每个标识符：

检查被导入模块是否有该名称的属性

如果没有，尝试导入具有该名称的子模块，然后再次检查被导入模块是否有该属性

如果未找到该属性，则引发 ImportError。

otherwise, a reference to that value is stored in the local namespace, using the name in the as clause if it is present, otherwise using the attribute name

示例:

import foo                 # foo imported and bound locally
import foo.bar.baz         # foo.bar.baz imported, foo bound locally
import foo.bar.baz as fbb  # foo.bar.baz imported and bound as fbb
from foo.bar import baz    # foo.bar.baz imported and bound as baz
from foo import attr       # foo imported and foo.attr bound as attr

如果标识符列表改为一个星号 ('*')，则在模块中定义的全部公有名称都将按 import 语句所在的作用域被绑定到局部命名空间。

一个模块所定义的 公有名称 是由在模块的命名空间中检测一个名为 __all__ 的变量来确定的；如果有定义，它必须是一个字符串列表，其中的项为该模块所定义或导入的名称。 在 __all__ 中所给出的名称都会被视为公有并且应当存在。 如果 __all__ 没有被定义，则公有名称的集合将包含在模块的命名空间中找到的所有不以下划线字符 ('_') 打头的名称。 __all__ 应当包括整个公有 API。 它的目标是避免意外地导出不属于 API 的一部分的项（例如在模块内部被导入和使用的库模块）。

通配符形式的导入 — from module import * — 仅在模块层级上被允许。 尝试在类或函数定义中使用它将引发 SyntaxError。

When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after from you can specify how high to traverse up the current package hierarchy without specifying exact names. One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc. So if you execute from . import mod from a module in the pkg package then you will end up importing pkg.mod. If you execute from ..subpkg2 import mod from within pkg.subpkg1 you will import pkg.subpkg2.mod. The specification for relative imports is contained within PEP 328 [https://www.python.org/dev/peps/pep-0328].

importlib.import_module() 被提供用来为动态地确定要导入模块的应用提供支持。

7.11.1. future 语句

future 语句 是一种针对编译器的指令，指明某个特定模块应当使用在特定的未来某个 Python 发行版中成为标准特性的语法或语义。

future 语句的目的是使得向在语言中引入了不兼容改变的 Python 未来版本的迁移更为容易。 它允许基于每个模块在某种新特性成为标准之前的发行版中使用该特性。

future_stmt ::=  "from" "__future__" "import" feature ["as" identifier]
                 ("," feature ["as" identifier])*
                 | "from" "__future__" "import" "(" feature ["as" identifier]
                 ("," feature ["as" identifier])* [","] ")"
feature     ::=  identifier

future 语句必须在靠近模块开头的位置出现。 可以出现在 future 语句之前行只有:

模块的文档字符串（如果存在），

注释，

空行，以及

其他 future 语句。

The features recognized by Python 3.0 are absolute_import, division, generators, unicode_literals, print_function, nested_scopes and with_statement. They are all redundant because they are always enabled, and only kept for backwards compatibility.

future 语句在编译时会被识别并做特殊对待：对核心构造语义的改变常常是通过生成不同的代码来实现。 新的特性甚至可能会引入新的不兼容语法（例如新的保留字），在这种情况下编译器可能需要以不同的方式来解析模块。 这样的决定不能推迟到运行时方才作出。

对于任何给定的发布版本，编译器要知道哪些特性名称已被定义，如果某个 future 语句包含未知的特性则会引发编译时错误。

直接运行时的语义与任何 import 语句相同：存在一个后文将详细说明的标准模块 __future__，它会在执行 future 语句时以通常的方式被导入。

相应的运行时语义取决于 future 语句所启用的指定特性。

请注意以下语句没有任何特别之处:

import __future__ [as name]

这并非 future 语句；它只是一条没有特殊语义或语法限制的普通 import 语句。

在默认情况下，通过对Code compiled by calls to the 内置函数 exec() 和 compile() 的调用所编译的代码如果出现于一个包含有 future 语句的模块 M 之中，就会使用 future 语句所关联的语法和语义。 此行为可以通过 compile() 的可选参数加以控制 — 请参阅该函数的文档以了解详情。

在交互式解释器提示符中键入的 future 语句将在解释器会话此后的交互中有效。 如果一个解释器的启动使用了 -i 选项启动，并传入了一个脚本名称来执行，且该脚本包含 future 语句，它将在交互式会话开始执行脚本之后保持有效。

参见

PEP 236 [https://www.python.org/dev/peps/pep-0236] - 回到 __future__

有关 __future__ 机制的最初提议。

7.12. The global statement

global_stmt ::=  "global" identifier ("," identifier)*

The global statement is a declaration which holds for the entire current code block. It means that the listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without global, although free variables may refer to globals without being declared global.

Names listed in a global statement must not be used in the same code block textually preceding that global statement.

在 global 语句中列出的名称不得被定义为正式形参，不也得出现于 for 循环的控制目标、class 定义、函数定义、import 语句或变量标注之中。

CPython implementation detail: 当前的实现并未强制要求所有的上述限制，但程序不应当滥用这样的自由，因为未来的实现可能会改为强制要求，并静默地改变程序的含义。

Programmer’s note: global is a directive to the parser. It applies only to code parsed at the same time as the global statement. In particular, a global statement contained in a string or code object supplied to the built-in exec() function does not affect the code block containing the function call, and code contained in such a string is unaffected by global statements in the code containing the function call. The same applies to the eval() and compile() functions.

7.13. The nonlocal statement

nonlocal_stmt ::=  "nonlocal" identifier ("," identifier)*

nonlocal 语句会使得所列出的名称指向之前在最近的包含作用域中绑定的除全局变量以外的变量。 这种功能很重要，因为绑定的默认行为是先搜索局部命名空间。 这个语句允许被封装的代码重新绑定局部作用域以外且非全局（模块）作用域当中的变量。

与 global 语句中列出的名称不同，nonlocal 语句中列出的名称必须指向之前存在于包含作用域之中的绑定（在这个应当用来创建新绑定的作用域不能被无歧义地确定）。

nonlocal 语句中列出的名称不得与之前存在于局部作用域中的绑定相冲突。

参见

PEP 3104 [https://www.python.org/dev/peps/pep-3104] - 访问外层作用域中的名称

有关 nonlocal 语句的规范说明。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

8. 复合语句

复合语句是包含其它语句（语句组）的语句；它们会以某种方式影响或控制所包含其它语句的执行。 通常，复合语句会跨越多行，虽然在某些简单形式下整个复合语句也可能包含于一行之内。

if, while 和 for 语句用来实现传统的控制流程构造。 try 语句为一组语句指定异常处理和/和清理代码，而 with 语句允许在一个代码块周围执行初始化和终结化代码。 函数和类定义在语法上也属于复合语句。

一条复合语句由一个或多个‘子句’组成。 一个子句则包含一个句头和一个‘句体’。 特定复合语句的子句头都处于相同的缩进层级。 每个子句头以一个作为唯一标识的关键字开始并以一个冒号结束。 子句体是由一个子句控制的一组语句。 子句体可以是在子句头的冒号之后与其同处一行的一条或由分号分隔的多条简单语句，或者也可以是在其之后缩进的一行或多行语句。 只有后一种形式的子句体才能包含嵌套的复合语句；以下形式是不合法的，这主要是因为无法分清某个后续的 else 子句应该属于哪个 if 子句:

if test1: if test2: print(x)

还要注意的是在这种情形下分号的绑定比冒号更紧密，因此在以下示例中，所有 print() 调用或者都不执行，或者都执行:

if x < y < z: print(x); print(y); print(z)

总结:

compound_stmt ::=  if_stmt
                   | while_stmt
                   | for_stmt
                   | try_stmt
                   | with_stmt
                   | funcdef
                   | classdef
                   | async_with_stmt
                   | async_for_stmt
                   | async_funcdef
suite         ::=  stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement     ::=  stmt_list NEWLINE | compound_stmt
stmt_list     ::=  simple_stmt (";" simple_stmt)* [";"]

请注意语句总是以 NEWLINE 结束，之后可能跟随一个 DEDENT。 还要注意可选的后续子句总是以一个不能作为语句开头的关键字作为开头，因此不会产生歧义（‘悬空的 else’问题在 Python 中是通过要求嵌套的 if 语句必须缩进来解决的)。

为了保证清晰，以下各节中语法规则采用将每个子句都放在单独行中的格式。

8.1. The if statement

if 语句用于有条件的执行:

if_stmt ::=  "if" expression ":" suite
             ("elif" expression ":" suite)*
             ["else" ":" suite]

它通过对表达式逐个求值直至找到一个真值（请参阅 布尔运算 了解真值与假值的定义）在子句体中选择唯一匹配的一个；然后执行该子句体（而且 if 语句的其他部分不会被执行或求值）。 如果所有表达式均为假值，则如果 else 子句体如果存在就会被执行。

8.2. The while statement

while 语句用于在表达式保持为真的情况下重复地执行:

while_stmt ::=  "while" expression ":" suite
                ["else" ":" suite]

This repeatedly tests the expression and, if it is true, executes the first suite; if the expression is false (which may be the first time it is tested) the suite of the else clause, if present, is executed and the loop terminates.

A break statement executed in the first suite terminates the loop without executing the else clause’s suite. A continue statement executed in the first suite skips the rest of the suite and goes back to testing the expression.

8.3. The for statement

for 语句用于对序列（例如字符串、元组或列表）或其他可迭代对象中的元素进行迭代:

for_stmt ::=  "for" target_list "in" expression_list ":" suite
              ["else" ":" suite]

The expression list is evaluated once; it should yield an iterable object. An iterator is created for the result of the expression_list. The suite is then executed once for each item provided by the iterator, in the order returned by the iterator. Each item in turn is assigned to the target list using the standard rules for assignments (see 赋值语句), and then the suite is executed. When the items are exhausted (which is immediately when the sequence is empty or an iterator raises a StopIteration exception), the suite in the else clause, if present, is executed, and the loop terminates.

A break statement executed in the first suite terminates the loop without executing the else clause’s suite. A continue statement executed in the first suite skips the rest of the suite and continues with the next item, or with the else clause if there is no next item.

The for-loop makes assignments to the variables(s) in the target list. This overwrites all previous assignments to those variables including those made in the suite of the for-loop:

for i in range(10):
    print(i)
    i = 5             # this will not affect the for-loop
                      # because i will be overwritten with the next
                      # index in the range

目标列表中的名称在循环结束时不会被删除，但如果序列为空，则它们根本不会被循环所赋值。 提示：内置函数 range() 会返回一个可迭代的整数序列，适用于模拟 Pascal 中的 for i := a to b do 这种效果；例如 list(range(3)) 会返回列表 [0, 1, 2]。

注解

当序列在循环中被修改时会有一个微妙的问题（这只可能发生于可变序列例如列表中）。 会有一个内部计数器被用来跟踪下一个要使用的项，每次迭代都会使计数器递增。 当计数器值达到序列长度时循环就会终止。 这意味着如果语句体从序列中删除了当前（或之前）的一项，下一项就会被跳过（因为其标号将变成已被处理的当前项的标号）。 类似地，如果语句体在序列当前项的前面插入一个新项，当前项会在循环的下一轮中再次被处理。 这会导致麻烦的程序错误，避免此问题的办法是对整个序列使用切片来创建一个临时副本，例如

for x in a[:]:
    if x < 0: a.remove(x)

8.4. The try statement

try 语句可为一组语句指定异常处理器和/或清理代码:

try_stmt  ::=  try1_stmt | try2_stmt
try1_stmt ::=  "try" ":" suite
               ("except" [expression ["as" identifier]] ":" suite)+
               ["else" ":" suite]
               ["finally" ":" suite]
try2_stmt ::=  "try" ":" suite
               "finally" ":" suite

The except clause(s) specify one or more exception handlers. When no exception occurs in the try clause, no exception handler is executed. When an exception occurs in the try suite, a search for an exception handler is started. This search inspects the except clauses in turn until one is found that matches the exception. An expression-less except clause, if present, must be last; it matches any exception. For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is “compatible” with the exception. An object is compatible with an exception if it is the class or a base class of the exception object or a tuple containing an item compatible with the exception.

如果没有 except 子句与异常相匹配，则会在周边代码和发起调用栈上继续搜索异常处理器。 1

如果在对 except 子句头中的表达式求值时引发了异常，则原来对处理器的搜索会被取消，并在周边代码和调用栈上启动对新异常的搜索（它会被视作是整个 try 语句所引发的异常）。

When a matching except clause is found, the exception is assigned to the target specified after the as keyword in that except clause, if present, and the except clause’s suite is executed. All except clauses must have an executable block. When the end of this block is reached, execution continues normally after the entire try statement. (This means that if two nested handlers exist for the same exception, and the exception occurs in the try clause of the inner handler, the outer handler will not handle the exception.)

当使用 as 将目标赋值为一个异常时，它将在 except 子句结束时被清除。 这就相当于

except E as N:
    foo

被转写为

except E as N:
    try:
        foo
    finally:
        del N

这意味着异常必须赋值给一个不同的名称才能在 except 子句之后引用它。 异常会被清除是因为在附加了回溯信息的情况下，它们会形成堆栈帧的循环引用，使得所有局部变量保持存活直到发生下一次垃圾回收。

在一个 except 子句体被执行之前，有关异常的详细信息存放在 sys 模块中，可通过 sys.exc_info() 来访问。 sys.exc_info() 返回一个 3 元组，由异常类、异常实例和回溯对象组成（参见 标准类型层级结构 一节），用于在程序中标识异常发生点。 当从处理异常的函数返回时 sys.exc_info() 的值会恢复为（调用前的）原值。

The optional else clause is executed if the control flow leaves the try suite, no exception was raised, and no return, continue, or break statement was executed. Exceptions in the else clause are not handled by the preceding except clauses.

If finally is present, it specifies a ‘cleanup’ handler. The try clause is executed, including any except and else clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The finally clause is executed. If there is a saved exception it is re-raised at the end of the finally clause. If the finally clause raises another exception, the saved exception is set as the context of the new exception. If the finally clause executes a return or break statement, the saved exception is discarded:

>>> def f():
...     try:
...         1/0
...     finally:
...         return 42
...
>>> f()
42

在 finally 子句执行期间，程序不能获取异常信息。

When a return, break or continue statement is executed in the try suite of a try…finally statement, the finally clause is also executed ‘on the way out.’ A continue statement is illegal in the finally clause. (The reason is a problem with the current implementation — this restriction may be lifted in the future).

The return value of a function is determined by the last return statement executed. Since the finally clause always executes, a return statement executed in the finally clause will always be the last one executed:

>>> def foo():
...     try:
...         return 'try'
...     finally:
...         return 'finally'
...
>>> foo()
'finally'

有关异常的更多信息可以在 异常 一节找到，有关使用 raise 语句生成异常的信息可以在 The raise statement 一节找到。

8.5. The with statement

with 语句用于包装带有使用上下文管理器 (参见 with 语句上下文管理器 一节) 定义的方法的代码块的执行。 这允许对普通的 try…except…finally 使用模式进行封装以方便地重用。

with_stmt ::=  "with" with_item ("," with_item)* ":" suite
with_item ::=  expression ["as" target]

带有一个“项目”的 with 语句的执行过程如下:

对上下文表达式 (在 with_item 中给出的表达式) 求值以获得一个上下文管理器。

载入上下文管理器的 __exit__() 以便后续使用。

发起调用上下文管理器的 __enter__() 方法。

如果 with 语句中包含一个目标，来自 __enter__() 的返回值将被赋值给它。

注解

with 语句会保证如果 __enter__() 方法返回时未发生错误，则 __exit__() 将总是被调用。 因此，如果在对目标列表赋值期间发生错误，则会将其视为在语句体内部发生的错误。 参见下面的第 6 步。

执行语句体。

发起调用上下文管理器的 __exit__() 方法。 如果语句体的退出是由异常导致的，则其类型、值和回溯信息将被作为参数传递给 __exit__()。 否则的话，将提供三个 None 参数。

如果语句体的退出是由异常导致的，并且来自 __exit__() 方法的返回值为假，则该异常会被重新引发。 如果返回值为真，则该异常会被抑制，并会继续执行 with 语句之后的语句。

如果语句体由于异常以外的任何原因退出，则来自 __exit__() 的返回值会被忽略，并会在该类退出正常的发生位置继续执行。

如果有多个项目，则会视作存在多个 with 语句嵌套来处理多个上下文管理器:

with A() as a, B() as b:
    suite

等价于

with A() as a:
    with B() as b:
        suite

在 3.1 版更改: 支持多个上下文表达式。

参见

PEP 343 [https://www.python.org/dev/peps/pep-0343] - “with” 语句

Python with 语句的规范描述、背景和示例。

8.6. 函数定义

函数定义就是对用户自定义函数的定义（参见 标准类型层级结构 一节）:

funcdef                 ::=  [decorators] "def" funcname "(" [parameter_list] ")"
                             ["->" expression] ":" suite
decorators              ::=  decorator+
decorator               ::=  "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE
dotted_name             ::=  identifier ("." identifier)*
parameter_list          ::=  defparameter ("," defparameter)* ["," [parameter_list_starargs]]
                             | parameter_list_starargs
parameter_list_starargs ::=  "*" [parameter] ("," defparameter)* ["," ["**" parameter [","]]]
                             | "**" parameter [","]
parameter               ::=  identifier [":" expression]
defparameter            ::=  parameter ["=" expression]
funcname                ::=  identifier

函数定义是一条可执行语句。 它执行时会在当前局部命名空间中将函数名称绑定到一个函数对象（函数可执行代码的包装器）。 这个函数对象包含对当前全局命名空间的引用，作为函数被调用时所使用的全局命名空间。

函数定义并不会执行函数体；只有当函数被调用时才会执行此操作。 2

一个函数定义可以被一个或多个 decorator 表达式所包装。 当函数被定义时将在包含该函数定义的作用域中对装饰器表达式求值。 求值结果必须是一个可调用对象，它会以该函数对象作为唯一参数被发起调用。 其返回值将被绑定到函数名称而非函数对象。 多个装饰器会以嵌套方式被应用。 例如以下代码

@f1(arg)
@f2
def func(): pass

大致等价于

def func(): pass
func = f1(arg)(f2(func))

不同之处在于原始函数并不会被临时绑定到名称 func。

当一个或多个 形参 具有 形参 = 表达式 这样的形式时，该函数就被称为具有“默认形参值”。 对于一个具有默认值的形参，其对应的 argument 可以在调用中被省略，在此情况下会用形参的默认值来替代。 如果一个形参具有默认值，后续所有在 “*” 之前的形参也必须具有默认值 — 这个句法限制并未在语法中明确表达。

默认形参值会在执行函数定义时按从左至右的顺序被求值。 这意味着当函数被定义时将对表达式求值一次，相同的“预计算”值将在每次调用时被使用。 这一点在默认形参为可变对象，例如列表或字典的时候尤其需要重点理解：如果函数修改了该对象（例如向列表添加了一项），则实际上默认值也会被修改。 这通常不是人们所预期的。 绕过此问题的一个方法是使用 None 作为默认值，并在函数体中显式地对其进行测试，例如:

def whats_on_the_telly(penguin=None):
    if penguin is None:
        penguin = []
    penguin.append("property of the zoo")
    return penguin

函数调用的语义在 调用 一节中有更详细的描述。 函数调用总是会给形参列表中列出的所有形参赋值，或用位置参数，或用关键字参数，或用默认值。 如果存在 “*identifier” 这样的形式，它会被初始化为一个元组来接收任何额外的位置参数，默认为空元组。 如果存在 “**identifier” 这样的形式，它会被初始化为一个新的有序映射来接收任何额外的关键字参数，默认为一个相同类型的空映射。 在 “*” 或 “*identifier” 之后的形参都是仅关键字形参，只能通过关键字参数传入值。

Parameters may have annotations of the form “: expression” following the parameter name. Any parameter may have an annotation even those of the form *identifier or **identifier. Functions may have “return” annotation of the form “-> expression” after the parameter list. These annotations can be any valid Python expression and are evaluated when the function definition is executed. Annotations may be evaluated in a different order than they appear in the source code. The presence of annotations does not change the semantics of a function. The annotation values are available as values of a dictionary keyed by the parameters’ names in the __annotations__ attribute of the function object.

It is also possible to create anonymous functions (functions not bound to a name), for immediate use in expressions. This uses lambda expressions, described in section lambda 表达式. Note that the lambda expression is merely a shorthand for a simplified function definition; a function defined in a “def” statement can be passed around or assigned to another name just like a function defined by a lambda expression. The “def” form is actually more powerful since it allows the execution of multiple statements and annotations.

程序员注意事项: 函数属于一类对象。 在一个函数内部执行的 “def” 语句会定义一个局部函数并可被返回或传递。 在嵌套函数中使用的自由变量可以访问包含该 def 语句的函数的局部变量。 详情参见 命名与绑定 一节。

参见

PEP 3107 [https://www.python.org/dev/peps/pep-3107] - 函数标注

最初的函数标注规范说明。

8.7. 类定义

类定义就是对类对象的定义 (参见 标准类型层级结构 一节):

classdef    ::=  [decorators] "class" classname [inheritance] ":" suite
inheritance ::=  "(" [argument_list] ")"
classname   ::=  identifier

类定义是一条可执行语句。 其中继承列表通常给出基类的列表 (进阶用法请参见 元类)，列表中的每一项都应当被求值为一个允许子类的类对象。 没有继承列表的类默认继承自基类 object；因此，:

class Foo:
    pass

等价于

class Foo(object):
    pass

随后类体将在一个新的执行帧 (参见 命名与绑定) 中被执行，使用新创建的局部命名空间和原有的全局命名空间。 （通常，类体主要包含函数定义。） 当类体结束执行时，其执行帧将被丢弃而其局部命名空间会被保存。 3 一个类对象随后会被创建，其基类使用给定的继承列表，属性字典使用保存的局部命名空间。 类名称将在原有的全局命名空间中绑定到该类对象。

在类体内定义的属性的顺序保存在新类的 __dict__ 中。 请注意此顺序的可靠性只限于类刚被创建时，并且只适用于使用定义语法所定义的类。

类的创建可使用 元类 进行重度定制。

类也可以被装饰：就像装饰函数一样，:

@f1(arg)
@f2
class Foo: pass

大致等价于

class Foo: pass
Foo = f1(arg)(f2(Foo))

装饰器表达式的求值规则与函数装饰器相同。 结果随后会被绑定到类名称。

程序员注意事项: 在类定义内定义的变量是类属性；它们将被类实例所共享。 实例属性可通过 self.name = value 在方法中设定。 类和实例属性均可通过 “self.name” 表示法来访问，当通过此方式访问时实例属性会隐藏同名的类属性。 类属性可被用作实例属性的默认值，但在此场景下使用可变值可能导致未预期的结果。 可以使用 描述器 来创建具有不同实现细节的实例变量。

参见

PEP 3115 [https://www.python.org/dev/peps/pep-3115] - Python 3000 中的元类

将元类声明修改为当前语法的提议，以及关于如何构建带有元类的类的语义描述。

PEP 3129 [https://www.python.org/dev/peps/pep-3129] - 类装饰器

增加类装饰器的提议。 函数和方法装饰器是在 PEP 318 [https://www.python.org/dev/peps/pep-0318] 中被引入的。

8.8. 协程

3.5 新版功能.

8.8.1. 协程函数定义

async_funcdef ::=  [decorators] "async" "def" funcname "(" [parameter_list] ")"
                   ["->" expression] ":" suite

Execution of Python coroutines can be suspended and resumed at many points (see coroutine). In the body of a coroutine, any await and async identifiers become reserved keywords; await expressions, async for and async with can only be used in coroutine bodies.

使用 async def 语法定义的函数总是为协程函数，即使它们不包含 await 或 async 关键字。

It is a SyntaxError to use yield from expressions in async def coroutines.

协程函数的例子:

async def func(param1, param2):
    do_stuff()
    await some_coroutine()

8.8.2. The async for statement

async_for_stmt ::=  "async" for_stmt

asynchronous iterable 能够在其 iter 实现中调用异步代码，而 asynchronous iterator 可以在其 next 方法中调用异步代码。

async for 语句允许方便地对异步迭代器进行迭代。

以下代码:

async for TARGET in ITER:
    BLOCK
else:
    BLOCK2

在语义上等价于:

iter = (ITER)
iter = type(iter).__aiter__(iter)
running = True
while running:
    try:
        TARGET = await type(iter).__anext__(iter)
    except StopAsyncIteration:
        running = False
    else:
        BLOCK
else:
    BLOCK2

另请参阅 __aiter__() 和 __anext__() 了解详情。

It is a SyntaxError to use async for statement outside of an async def function.

8.8.3. The async with statement

async_with_stmt ::=  "async" with_stmt

asynchronous context manager 是一种 context manager，能够在其 enter 和 exit 方法中暂停执行。

以下代码:

async with EXPR as VAR:
    BLOCK

在语义上等价于:

mgr = (EXPR)
aexit = type(mgr).__aexit__
aenter = type(mgr).__aenter__(mgr)

VAR = await aenter
try:
    BLOCK
except:
    if not await aexit(mgr, *sys.exc_info()):
        raise
else:
    await aexit(mgr, None, None, None)

另请参阅 __aenter__() 和 __aexit__() 了解详情。

It is a SyntaxError to use async with statement outside of an async def function.

参见

PEP 492 [https://www.python.org/dev/peps/pep-0492] - 使用 async 和 await 语法实现协程

将协程作为 Python 中的一个正式的单独概念，并增加相应的支持语法。

备注

1

异常会被传播给发起调用栈，除非存在一个 finally 子句正好引发了另一个异常。 新引发的异常将导致旧异常的丢失。

2

作为函数体的第一条语句出现的字符串字面值会被转换为函数的 __doc__ 属性，也就是该函数的 docstring。

3

作为类体的第一条语句出现的字符串字面值会被转换为命名空间的 __doc__ 条目，也就是该类的 docstring。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

9. 最高层级组件

Python 解释器可以从多种源获得输入：作为标准输入或程序参数传入的脚本，以交互方式键入的语句，导入的模块源文件等等。 这一章将给出在这些情况下所用的语法。

9.1. 完整的 Python 程序

虽然语言规范描述不必规定如何发起调用语言解释器，但对完整的 Python 程序加以说明还是很有用的。 一个完整的 Python 程序会在最小初始化环境中被执行：所有内置和标准模块均为可用，但均处于未初始化状态，只有 sys (各种系统服务), builtins (内置函数、异常以及 None) 和 __main__ 除外。 最后一个模块用于为完整程序的执行提供局部和全局命名空间。

适用于一个完整 Python 程序的语法即下节所描述的文件输入。

解释器也可以通过交互模式被发起调用；在此情况下，它并不读取和执行一个完整程序，而是每次读取和执行一条语句（可能为复合语句）。 此时的初始环境与一个完整程序的相同；每条语句会在 __main__ 的命名空间中被执行。

一个完整程序可通过三种形式被传递给解释器：使用 -c 字符串 命令行选项，使用一个文件作为第一个命令行参数，或者使用标准输入。 如果文件或标准输入是一个 tty 设置，解释器会进入交互模式；否则的话，它会将文件当作一个完整程序来执行。

9.2. 文件输入

所有从非交互式文件读取的输入都具有相同的形式:

file_input ::=  (NEWLINE | statement)*

此语法用于下列几种情况:

解析一个完整 Python 程序时（从文件或字符串）；

解析一个模块时；

解析一个传递给 exec() 函数的字符串时；

9.3. 交互式输入

交互模式下的输入使用以下语法进行解析:

interactive_input ::=  [stmt_list] NEWLINE | compound_stmt NEWLINE

请注意在交互模式下一条（最高层级）复合语句必须带有一个空行；这对于帮助解析器确定输入的结束是必须的。

9.4. 表达式输入

eval() 被用于表达式输入。 它会忽略开头的空白。 传递给 eval() 的字符串参数必须具有以下形式:

eval_input ::=  expression_list NEWLINE*

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

10. 完整的语法规范

这是完整的Python语法，它被送入解析器生成器，以生成解析Python源文件的解析器：

# Grammar for Python

# NOTE WELL: You should also follow all the steps listed at
# https://devguide.python.org/grammar/

# Start symbols for the grammar:
#       single_input is a single interactive statement;
#       file_input is a module or sequence of commands read from an input file;
#       eval_input is the input for the eval() functions.
# NB: compound_stmt in single_input is followed by extra NEWLINE!
single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
file_input: (NEWLINE | stmt)* ENDMARKER
eval_input: testlist NEWLINE* ENDMARKER

decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
decorators: decorator+
decorated: decorators (classdef | funcdef | async_funcdef)

async_funcdef: ASYNC funcdef
funcdef: 'def' NAME parameters ['->' test] ':' suite

parameters: '(' [typedargslist] ')'
typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
        '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
      | '**' tfpdef [',']]]
  | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
  | '**' tfpdef [','])
tfpdef: NAME [':' test]
varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
        '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
      | '**' vfpdef [',']]]
  | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
  | '**' vfpdef [',']
)
vfpdef: NAME

stmt: simple_stmt | compound_stmt
simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
             import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
                     ('=' (yield_expr|testlist_star_expr))*)
annassign: ':' test ['=' test]
testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
            '<<=' | '>>=' | '**=' | '//=')
# For normal and annotated assignments, additional restrictions enforced by the interpreter
del_stmt: 'del' exprlist
pass_stmt: 'pass'
flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | yield_stmt
break_stmt: 'break'
continue_stmt: 'continue'
return_stmt: 'return' [testlist]
yield_stmt: yield_expr
raise_stmt: 'raise' [test ['from' test]]
import_stmt: import_name | import_from
import_name: 'import' dotted_as_names
# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
import_from: ('from' (('.' | '...')* dotted_name | ('.' | '...')+)
              'import' ('*' | '(' import_as_names ')' | import_as_names))
import_as_name: NAME ['as' NAME]
dotted_as_name: dotted_name ['as' NAME]
import_as_names: import_as_name (',' import_as_name)* [',']
dotted_as_names: dotted_as_name (',' dotted_as_name)*
dotted_name: NAME ('.' NAME)*
global_stmt: 'global' NAME (',' NAME)*
nonlocal_stmt: 'nonlocal' NAME (',' NAME)*
assert_stmt: 'assert' test [',' test]

compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
async_stmt: ASYNC (funcdef | with_stmt | for_stmt)
if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
while_stmt: 'while' test ':' suite ['else' ':' suite]
for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]
try_stmt: ('try' ':' suite
           ((except_clause ':' suite)+
            ['else' ':' suite]
            ['finally' ':' suite] |
           'finally' ':' suite))
with_stmt: 'with' with_item (',' with_item)*  ':' suite
with_item: test ['as' expr]
# NB compile.c makes sure that the default except clause is last
except_clause: 'except' [test ['as' NAME]]
suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT

test: or_test ['if' or_test 'else' test] | lambdef
test_nocond: or_test | lambdef_nocond
lambdef: 'lambda' [varargslist] ':' test
lambdef_nocond: 'lambda' [varargslist] ':' test_nocond
or_test: and_test ('or' and_test)*
and_test: not_test ('and' not_test)*
not_test: 'not' not_test | comparison
comparison: expr (comp_op expr)*
# <> isn't actually a valid comparison operator in Python. It's here for the
# sake of a __future__ import described in PEP 401 (which really works :-)
comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
star_expr: '*' expr
expr: xor_expr ('|' xor_expr)*
xor_expr: and_expr ('^' and_expr)*
and_expr: shift_expr ('&' shift_expr)*
shift_expr: arith_expr (('<<'|'>>') arith_expr)*
arith_expr: term (('+'|'-') term)*
term: factor (('*'|'@'|'/'|'%'|'//') factor)*
factor: ('+'|'-'|'~') factor | power
power: atom_expr ['**' factor]
atom_expr: [AWAIT] atom trailer*
atom: ('(' [yield_expr|testlist_comp] ')' |
       '[' [testlist_comp] ']' |
       '{' [dictorsetmaker] '}' |
       NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')
testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
subscriptlist: subscript (',' subscript)* [',']
subscript: test | [test] ':' [test] [sliceop]
sliceop: ':' [test]
exprlist: (expr|star_expr) (',' (expr|star_expr))* [',']
testlist: test (',' test)* [',']
dictorsetmaker: ( ((test ':' test | '**' expr)
                   (comp_for | (',' (test ':' test | '**' expr))* [','])) |
                  ((test | star_expr)
                   (comp_for | (',' (test | star_expr))* [','])) )

classdef: 'class' NAME ['(' [arglist] ')'] ':' suite

arglist: argument (',' argument)*  [',']

# The reason that keywords are test nodes instead of NAME is that using NAME
# results in an ambiguity. ast.c makes sure it's a NAME.
# "test '=' test" is really "keyword '=' test", but we have no such token.
# These need to be in a single rule to avoid grammar that is ambiguous
# to our LL(1) parser. Even though 'test' includes '*expr' in star_expr,
# we explicitly match '*' here, too, to give it proper precedence.
# Illegal combinations and orderings are blocked in ast.c:
# multiple (test comp_for) arguments are blocked; keyword unpackings
# that precede iterable unpackings are blocked; etc.
argument: ( test [comp_for] |
            test '=' test |
            '**' test |
            '*' test )

comp_iter: comp_for | comp_if
comp_for: [ASYNC] 'for' exprlist 'in' or_test [comp_iter]
comp_if: 'if' test_nocond [comp_iter]

# not used in grammar, but may appear in "node" passed from Parser to Compiler
encoding_decl: NAME

yield_expr: 'yield' [yield_arg]
yield_arg: 'from' test | testlist

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python语言参考 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

Python 标准库

Python语言参考 描述了 Python 语言的具体语法和语义，这份库参考则介绍了与 Python 一同发行的标准库。它还描述了通常包含在 Python 发行版中的一些可选组件。

Python 标准库非常庞大，所提供的组件涉及范围十分广泛，正如以下内容目录所显示的。这个库包含了多个内置模块 (以 C 编写)，Python 程序员必须依靠它们来实现系统级功能，例如文件 I/O，此外还有大量以 Python 编写的模块，提供了日常编程中许多问题的标准解决方案。其中有些模块经过专门设计，通过将特定平台功能抽象化为平台中立的 API 来鼓励和加强 Python 程序的可移植性。

Windows 版本的 Python 安装程序通常包含整个标准库，往往还包含许多额外组件。对于类 Unix 操作系统，Python 通常会分成一系列的软件包，因此可能需要使用操作系统所提供的包管理工具来获取部分或全部可选组件。

在这个标准库以外还存在成千上万并且不断增加的其他组件 (从单独的程序、模块、软件包直到完整的应用开发框架)，访问 Python 包索引 [https://pypi.org] 即可获取这些第三方包。

1. 概述

2. 内置函数

3. 内置常量

3.1. 由 site 模块添加的常量

4. 内置类型

4.1. 逻辑值检测

4.2. Boolean Operations — and, or, not

4.3. 比较运算

4.4. 数字类型 — int, float, complex

4.5. 迭代器类型

4.6. 序列类型 — list, tuple, range

4.7. 文本序列类型 — str

4.8. 二进制序列类型 — bytes, bytearray, memoryview

4.9. 集合类型 — set, frozenset

4.10. 映射类型 — dict

4.11. 上下文管理器类型

4.12. 其他内置类型

4.13. 特殊属性

5. 内置异常

5.1. 基类

5.2. 具体异常

5.3. 警告

5.4. 异常层次结构

6. 文本处理服务

6.1. string — 常见的字符串操作

6.2. re — 正则表达式操作

6.3. difflib — 计算差异的辅助工具

6.4. textwrap — 文本自动换行与填充

6.5. unicodedata — Unicode 数据库

6.6. stringprep — 因特网字符串预备

6.7. readline — GNU readline 接口

6.8. rlcompleter — GNU readline 的补全函数

7. 二进制数据服务

7.1. struct — 将字节串解读为打包的二进制数据

7.2. codecs — 编解码器注册和相关基类

8. 数据类型

8.1. datetime — 基本的日期和时间类型

8.2. calendar — 日历相关函数

8.3. collections — 容器数据类型

8.4. collections.abc — 容器的抽象基类

8.5. heapq — 堆队列算法

8.6. bisect — 数组二分查找算法

8.7. array — 高效的数值数组

8.8. weakref — 弱引用

8.9. types — 动态类型创建和内置类型名称

8.10. copy — 浅层 (shallow) 和深层 (deep) 复制操作

8.11. pprint — 数据美化输出

8.12. reprlib — 另一种 repr() 实现

8.13. enum — 枚举类型支持

9. 数字和数学模块

9.1. numbers — 数字的抽象基类

9.2. math — 数学函数

9.3. cmath ——关于复数的数学函数

9.4. decimal — 十进制定点和浮点运算

9.5. fractions — 分数

9.6. random — 生成伪随机数

9.7. statistics — 数学统计函数

10. 函数式编程模块

10.1. itertools — 为高效循环而创建迭代器的函数

10.2. functools — 高阶函数和可调用对象上的操作

10.3. operator — 标准运算符替代函数

11. 文件和目录访问

11.1. pathlib — 面向对象的文件系统路径

11.2. os.path — 常见路径操作

11.3. fileinput — 迭代来自多个输入流的行

11.4. stat — 解析 stat() 结果

11.5. filecmp — 文件及目录的比较

11.6. tempfile — 生成临时文件和目录

11.7. glob — Unix 风格路径名模式扩展

11.8. fnmatch — Unix 文件名模式匹配

11.9. linecache — 随机读写文本行

11.10. shutil — 高阶文件操作

11.11. macpath — Mac OS 9 路径操作函数

12. 数据持久化

12.1. pickle —— Python 对象序列化

12.2. copyreg — 注意 pickle 支持函数

12.3. shelve — Python 对象持久化

12.4. marshal — 内部 Python 对象序列化

12.5. dbm — Unix “数据库” 接口

12.6. sqlite3 — SQLite 数据库 DB-API 2.0 接口模块

13. 数据压缩和存档

13.1. zlib — 与 gzip 兼容的压缩

13.2. gzip — 对 gzip 格式的支持

13.3. bz2 — 对 bzip2 压缩算法的支持

13.4. lzma — 用 LZMA 算法压缩

13.5. zipfile — 使用ZIP存档

13.6. tarfile — 读写tar归档文件

14. 文件格式

14.1. csv — CSV 文件读写

14.2. configparser — 配置文件解析器

14.3. netrc — netrc 文件处理

14.4. xdrlib — 编码与解码 XDR 数据

14.5. plistlib — 生成与解析 Mac OS X .plist 文件

15. 加密服务

15.1. hashlib — 安全哈希与消息摘要

15.2. hmac — 基于密钥的消息验证

15.3. secrets — 生成安全随机数字用于管理密码

16. 通用操作系统服务

16.1. os — 操作系统接口模块

16.2. io — 处理流的核心工具

16.3. time — 时间的访问和转换

16.4. argparse — 命令行选项、参数和子命令解析器

16.5. getopt — C 风格的命令行选项解析器

16.6. 模块 logging — Python 的日志记录工具

16.7. logging.config — 日志记录配置

16.8. logging.handlers — 日志处理

16.9. getpass — 便携式密码输入工具

16.10. curses — 终端字符单元显示的处理

16.11. curses.textpad — 用于 curses 程序的文本输入控件

16.12. curses.ascii — 用于 ASCII 字符的工具

16.13. curses.panel — curses 的 panel 栈扩展

16.14. platform — 获取底层平台的标识数据

16.15. errno — 标准 errno 系统符号

16.16. ctypes — Python 的外部函数库

17. 并发执行

17.1. threading — 基于线程的并行

17.2. multiprocessing — 基于进程的并行

17.3. concurrent 包

17.4. concurrent.futures — 启动并行任务

17.5. subprocess — 子进程管理

17.6. sched — 事件调度器

17.7. queue — 一个同步的队列类

17.8. dummy_threading — 可直接替代 threading 模块。

17.9. _thread — 底层多线程 API

17.10. _dummy_thread — _thread 的替代模块

18. Interprocess Communication and Networking

18.1. socket — 底层网络接口

18.2. ssl — 套接字对象的TLS/SSL封装

18.3. select — Waiting for I/O 完成

18.4. selectors — 高级 I/O 复用库

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks

18.6. asyncore — 异步socket处理器

18.7. asynchat — 异步 socket 指令/响应 处理器

18.8. signal — 设置异步事件处理程序

18.9. mmap — 内存映射文件支持

19. 互联网数据处理

19.1. email — 电子邮件与 MIME 处理包

19.2. json — JSON 编码和解码器

19.3. mailcap — Mailcap 文件处理

19.4. mailbox — Manipulate mailboxes in various formats

19.5. mimetypes — Map filenames to MIME types

19.6. base64 — Base16, Base32, Base64, Base85 数据编码

19.7. binhex — 对binhex4文件进行编码和解码

19.8. binascii — 二进制和 ASCII 码互转

19.9. quopri — 编码与解码经过 MIME 转码的可打印数据

19.10. uu — 对 uuencode 文件进行编码与解码

20. 结构化标记处理工具

20.1. html — 超文本标记语言支持

20.2. html.parser — 简单的 HTML 和 XHTML 解析器

20.3. html.entities — HTML 一般实体的定义

20.4. XML处理模块

20.5. xml.etree.ElementTree — ElementTree XML API

20.6. xml.dom — The Document Object Model API

20.7. xml.dom.minidom — Minimal DOM implementation

20.8. xml.dom.pulldom — Support for building partial DOM trees

20.9. xml.sax — Support for SAX2 parsers

20.10. xml.sax.handler — Base classes for SAX handlers

20.11. xml.sax.saxutils — SAX 工具集

20.12. xml.sax.xmlreader — Interface for XML parsers

20.13. xml.parsers.expat — Fast XML parsing using Expat

21. 互联网协议和支持

21.1. webbrowser — 方便的Web浏览器控制器

21.2. cgi — Common Gateway Interface support

21.3. cgitb — 用于 CGI 脚本的回溯管理器

21.4. wsgiref — WSGI Utilities and Reference Implementation

21.5. urllib — URL 处理模块

21.6. urllib.request — 用于打开 URL 的可扩展库

21.7. urllib.response — urllib 使用的 Response 类

21.8. urllib.parse — Parse URLs into components

21.9. urllib.error — urllib.request 引发的异常类

21.10. urllib.robotparser — robots.txt 语法分析程序

21.11. http — HTTP 模块

21.12. http.client — HTTP 协议客户端

21.13. ftplib — FTP 协议客户端

21.14. poplib — POP3 协议客户端

21.15. imaplib — IMAP4 协议客户端

21.16. nntplib — NNTP protocol client

21.17. smtplib —SMTP协议客户端

21.18. smtpd — SMTP 服务器

21.19. telnetlib — Telnet client

21.20. uuid — UUID objects according to RFC 4122

21.21. socketserver — A framework for network servers

21.22. http.server — HTTP 服务器

21.23. http.cookies — HTTP状态管理

21.24. http.cookiejar —— HTTP 客户端的 Cookie 处理

21.25. xmlrpc — XMLRPC 服务端与客户端模块

21.26. xmlrpc.client — XML-RPC client access

21.27. xmlrpc.server — Basic XML-RPC servers

21.28. ipaddress — IPv4/IPv6 操作库

22. 多媒体服务

22.1. audioop — 处理原始音频数据

22.2. aifc — 读写 AIFF 和 AIFC 文件

22.3. sunau — 读写 Sun AU 文件

22.4. wave — 读写WAV格式文件

22.5. chunk — 读取 IFF 分块数据

22.6. colorsys — 颜色系统间的转换

22.7. imghdr — 推测图像类型

22.8. sndhdr — 推测声音文件的类型

22.9. ossaudiodev — Access to OSS-compatible audio devices

23. 国际化

23.1. gettext — 多语种国际化服务

23.2. locale — 国际化服务

24. 程序框架

24.1. turtle — 海龟绘图

24.2. cmd — 支持面向行的命令解释器

24.3. shlex — Simple lexical analysis

25. Tk图形用户界面(GUI)

25.1. tkinter — Tcl/Tk的Python接口

25.2. tkinter.ttk — Tk主题小部件

25.3. tkinter.tix — Extension widgets for Tk

25.4. tkinter.scrolledtext — 滚动文字控件

25.5. IDLE

25.6. 其他图形用户界面（GUI）包

26. 开发工具

26.1. typing — 类型标注支持

26.2. pydoc — 文档生成器和在线帮助系统

26.3. doctest — 测试交互性的Python示例

26.4. unittest — 单元测试框架

26.5. unittest.mock — 模拟对象库

26.6. unittest.mock 上手指南

26.7. 2to3 - 自动将 Python 2 代码转为 Python 3 代码

26.8. test — Python回归测试包

26.9. test.support — Utilities for the Python test suite

27. 调试和分析

27.1. bdb — Debugger framework

27.2. faulthandler — Dump the Python traceback

27.3. pdb — Python的调试器

27.4. Python 分析器

27.5. timeit — 测量小代码片段的执行时间

27.6. trace — 跟踪Python语句执行

27.7. tracemalloc — 跟踪内存分配

28. 软件打包和分发

28.1. distutils — 构建和安装 Python 模块

28.2. ensurepip — Bootstrapping the pip installer

28.3. venv — 创建虚拟环境

28.4. zipapp — Manage executable python zip archives

29. Python运行时服务

29.1. sys — 系统相关的参数和函数

29.2. sysconfig — Provide access to Python’s configuration information

29.3. builtins — 内建对象

29.4. __main__ — 顶层脚本环境

29.5. warnings — Warning control

29.6. contextlib — Utilities for with-statement contexts

29.7. abc — 抽象基类

29.8. atexit — 退出处理器

29.9. traceback — 打印或检索堆栈回溯

29.10. __future__ — Future 语句定义

29.11. gc — 垃圾回收器接口

29.12. inspect — 检查对象

29.13. site —— 指定 Site 的配置钩子

29.14. fpectl — Floating point exception control

30. 自定义 Python 解释器

30.1. code — 解释器基础类

30.2. codeop — 编译Python代码

31. 导入模块

31.1. zipimport — 从 Zip 存档中导入模块

31.2. pkgutil — 包扩展模块工具

31.3. modulefinder — 查找脚本使用的模块

31.4. runpy — Locating and executing Python modules

31.5. importlib — The implementation of import

32. Python 语言服务

32.1. parser — 访问 Python 解析树

32.2. ast — 抽象语法树

32.3. symtable — Access to the compiler’s symbol tables

32.4. symbol — 与 Python 解析树一起使用的常量

32.5. token — 与Python解析树一起使用的常量

32.6. keyword — 检验Python关键字

32.7. tokenize – 对 Python 代码使用的标记解析器

32.8. tabnanny — 模糊缩进检测

32.9. pyclbr — Python class browser support

32.10. py_compile — 编译 Python 源文件

32.11. compileall — Byte-compile Python libraries

32.12. dis — Python 字节码反汇编器

32.13. pickletools — pickle 开发者工具集

33. 杂项服务

33.1. formatter — 通用格式化输出

34. Windows系统相关模块

34.1. msilib — Read and write Microsoft Installer files

34.2. msvcrt — 来自 MS VC++ 运行时的有用例程

34.3. winreg — Windows 注册表访问

34.4. winsound — Sound-playing interface for Windows

35. Unix 专有服务

35.1. posix — 最常见的 POSIX 系统调用

35.2. pwd — 用户密码数据库

35.3. spwd — The shadow password database

35.4. grp — 组数据库

35.5. crypt — Function to check Unix passwords

35.6. termios — POSIX 风格的 tty 控制

35.7. tty — 终端控制功能

35.8. pty — 伪终端工具

35.9. fcntl — The fcntl and ioctl system calls

35.10. pipes — 终端管道接口

35.11. resource — Resource usage information

35.12. nis — Sun 的 NIS (黄页) 接口

35.13. Unix syslog 库例程

36. 被取代的模块

36.1. optparse — 解析器的命令行选项

36.2. imp — Access the import internals

37. 未创建文档的模块

37.1. 平台特定模块

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

1. 概述

“Python 库”中包含了几种不同的组件。

它包含通常被视为语言“核心”中的一部分的数据类型，例如数字和列表。对于这些类型，Python语言核心定义了文字的形式，并对它们的语义设置了一些约束，但没有完全定义语义。（另一方面，语言核心确实定义了语法属性，如操作符的拼写和优先级。）

这个库也包含了内置函数和异常 — 不需要 import 语句就可以在所有Python代码中使用的对象。有一些是由语言核心定义的，但是许多对于核心语义不是必需的，并且仅在这里描述。

不过这个库主要是由一系列的模块组成。这些模块集可以不同方式分类。有些模块是用 C 编写并内置于 Python 解释器中；另一些模块则是用 Python 编写并以源码形式导入。有些模块提供专用于 Python 的接口，例如打印栈追踪信息；有些模块提供专用于特定操作系统的接口，例如操作特定的硬件；另一些模块则提供针对特定应用领域的接口，例如万维网。有些模块在所有更新和移植版本的 Python 中可用；另一些模块仅在底层系统支持或要求时可用；还有些模块则仅当编译和安装 Python 时选择了特定配置选项时才可用。

本手册以 “从内到外” 的顺序组织：首先描述内置函数、数据类型和异常，最后是根据相关性进行分组的各种模块。

这意味着如果你从头开始阅读本手册，并在感到厌烦时跳到下一章，你仍能对 Python 库的可用模块和所支持的应用领域有个大致了解。当然，你并非 必须 如同读小说一样从头读到尾 — 你也可以先浏览内容目录 (在手册开头)，或在索引 (在手册末尾) 中查找某个特定函数、模块或条目。最后，如果你喜欢随意学习某个主题，你可以选择一个随机页码 (参见 random 模块) 并读上一两小节。无论你想以怎样的顺序阅读本手册，还是建议先从 内置函数 这一章开始，因为本手册的其余内容都需要你熟悉其中的基本概念。

让我们开始吧！

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

2. 内置函数

Python 解释器内置了很多函数和类型，您可以在任何时候使用它们。以下按字母表顺序列出它们。

内置函数

abs()

dict()

help()

min()

setattr()

all()

dir()

hex()

next()

slice()

any()

divmod()

id()

object()

sorted()

ascii()

enumerate()

input()

oct()

staticmethod()

bin()

eval()

int()

open()

str()

bool()

exec()

isinstance()

ord()

sum()

bytearray()

filter()

issubclass()

pow()

super()

bytes()

float()

iter()

print()

tuple()

callable()

format()

len()

property()

type()

chr()

frozenset()

list()

range()

vars()

classmethod()

getattr()

locals()

repr()

zip()

compile()

globals()

map()

reversed()

__import__()

complex()

hasattr()

max()

round()

delattr()

hash()

memoryview()

set()

abs(x)

返回一个数的绝对值。实参可以是整数或浮点数。如果实参是一个复数，返回它的模。

all(iterable)

如果 iterable 的所有元素为真（或迭代器为空），返回 True 。等价于:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

any(iterable)

如果 iterable 的任一元素为真则返回 True。 如果迭代器为空，返回 False。 等价于:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

ascii(object)

就像函数 repr()，返回一个对象可打印的字符串，但是 repr() 返回的字符串中非 ASCII 编码的字符，会使用 \x、\u 和 \U 来转义。生成的字符串和 Python 2 的 repr() 返回的结果相似。

bin(x)

将一个整数转变为一个前缀为“0b”的二进制字符串。结果是一个合法的 Python 表达式。如果 x 不是 Python 的 int 对象，那它需要定义 __index__() 方法返回一个整数。一些例子：

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'

如果不一定需要前缀“0b”，还可以使用如下的方法。

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')

另见 format() 获取更多信息。

class bool([x])

返回一个布尔值，True 或者 False。 x 使用标准的 真值测试过程 来转换。如果 x 是假的或者被省略，返回 False；其他情况返回 True。bool 类是 int 的子类（参见 数字类型 — int, float, complex）。其他类不能继承自它。它只有 False 和 True 两个实例（参见 布尔值）。

class bytearray([source[, encoding[, errors]]])

返回一个新的 bytes 数组。 bytearray 类是一个可变序列，包含范围为 0 <= x < 256 的整数。它有可变序列大部分常见的方法，见 可变序列类型 的描述；同时有 bytes 类型的大部分方法，参见 bytes 和 bytearray 操作。

可选形参 source 可以用不同的方式来初始化数组：

如果是一个 string，您必须提供 encoding 参数（errors 参数仍是可选的）；bytearray() 会使用 str.encode() 方法来将 string 转变成 bytes。

如果是一个 integer，会初始化大小为该数字的数组，并使用 null 字节填充。

如果是一个符合 buffer 接口的对象，该对象的只读 buffer 会用来初始化字节数组。

如果是一个 iterable 可迭代对象，它的元素的范围必须是 0 <= x < 256 的整数，它会被用作数组的初始内容。

如果没有实参，则创建大小为 0 的数组。

另见 二进制序列类型 — bytes, bytearray, memoryview 和 bytearray 对象。

class bytes([source[, encoding[, errors]]])

返回一个新的“bytes”对象， 是一个不可变序列，包含范围为 0 <= x < 256 的整数。bytes 是 bytearray 的不可变版本 - 它有其中不改变序列的方法和相同的索引、切片操作。

因此，构造函数的实参和 bytearray() 相同。

字节对象还可以用字面值创建，参见 字符串和字节串字面值。

另见 二进制序列类型 — bytes, bytearray, memoryview，bytes 对象 和 bytes 和 bytearray 操作。

callable(object)

Return True if the object argument appears callable, False if not. If this returns true, it is still possible that a call fails, but if it is false, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); instances are callable if their class has a __call__() method.

3.2 新版功能: 这个函数一开始在 Python 3.0 被移除了，但在 Python 3.2 被重新加入。

chr(i)

返回 Unicode 码位为整数 i 的字符的字符串格式。例如，chr(97) 返回字符串 'a'，chr(8364) 返回字符串 '€'。这是 ord() 的逆函数。

实参的合法范围是 0 到 1,114,111（16 进制表示是 0x10FFFF）。如果 i 超过这个范围，会触发 ValueError 异常。

@classmethod

把一个方法封装成类方法。

一个类方法把类自己作为第一个实参，就像一个实例方法把实例自己作为第一个实参。请用以下习惯来声明类方法:

class C:
    @classmethod
    def f(cls, arg1, arg2, ...): ...

The @classmethod form is a function decorator – see the description of function definitions in 函数定义 for details.

It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.

Class methods are different than C++ or Java static methods. If you want those, see staticmethod() in this section.

For more information on class methods, consult the documentation on the standard type hierarchy in 标准类型层级结构.

compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)

将 source 编译成代码或 AST 对象。代码对象可以被 exec() 或 eval() 执行。source 可以是常规的字符串、字节字符串，或者 AST 对象。参见 ast 模块的文档了解如何使用 AST 对象。

filename 实参需要是代码读取的文件名；如果代码不需要从文件中读取，可以传入一些可辨识的值（经常会使用 '<string>'）。

mode 实参指定了编译代码必须用的模式。如果 source 是语句序列，可以是 'exec'；如果是单一表达式，可以是 'eval'；如果是单个交互式语句，可以是 'single'。（在最后一种情况下，如果表达式执行结果不是 None 将会被打印出来。）

可选参数 flags 和 dont_inherit 控制在编译 source 时要用到哪个 future 语句。 如果两者都未提供（或都为零）则会使用调用 compile() 的代码中有效的 future 语句来编译代码。 如果给出了 flags 参数但没有 dont_inherit (或是为零) 则 flags 参数所指定的 以及那些无论如何都有效的 future 语句会被使用。 如果 dont_inherit 为一个非零整数，则只使用 flags 参数 – 在调用外围有效的 future 语句将被忽略。

Future 语句使用比特位来指定，多个语句可以通过按位或来指定。具体特性的比特位可以通过 __future__ 模块中的 _Feature 类的实例的 compiler_flag 属性来获得。

optimize 实参指定编译器的优化级别；默认值 -1 选择与解释器的 -O 选项相同的优化级别。显式级别为 0 （没有优化；__debug__ 为真）、1 （断言被删除， __debug__ 为假）或 2 （文档字符串也被删除）。

如果编译的源码不合法，此函数会触发 SyntaxError 异常；如果源码包含 null 字节，则会触发 ValueError 异常。

如果您想分析 Python 代码的 AST 表示，请参阅 ast.parse()。

注解

在 'single' 或 'eval' 模式编译多行代码字符串时，输入必须以至少一个换行符结尾。 这使 code 模块更容易检测语句的完整性。

警告

在将足够大或者足够复杂的字符串编译成 AST 对象时，Python 解释器有可以因为 Python AST 编译器的栈深度限制而崩溃。

在 3.2 版更改: 允许使用 Windows 和 Mac 的换行符。在 'exec' 模式不再需要以换行符结尾。增加了 optimize 形参。

在 3.5 版更改: 之前 source 中包含 null 字节的话会触发 TypeError 异常。

class complex([real[, imag]])

返回值为 real + imag*1j 的复数，或将字符串或数字转换为复数。如果第一个形参是字符串，则它被解释为一个复数，并且函数调用时必须没有第二个形参。第二个形参不能是字符串。每个实参都可以是任意的数值类型（包括复数）。如果省略了 imag，则默认值为零，构造函数会像 int 和 float 一样进行数值转换。如果两个实参都省略，则返回 0j。

注解

当从字符串转换时，字符串在 + 或 - 的周围必须不能有空格。例如 complex('1+2j') 是合法的，但 complex('1 + 2j') 会触发 ValueError 异常。

数字类型 — int, float, complex 描述了复数类型。

在 3.6 版更改: 您可以使用下划线将代码文字中的数字进行分组。

delattr(object, name)

setattr() 相关的函数。实参是一个对象和一个字符串。该字符串必须是对象的某个属性。如果对象允许，该函数将删除指定的属性。例如 delattr(x, 'foobar') 等价于 del x.foobar 。

class dict(**kwarg)

class dict(mapping, **kwarg)

class dict(iterable, **kwarg)

创建一个新的字典。dict 对象是一个字典类。参见 dict 和 映射类型 — dict 了解这个类。

其他容器类型，请参见内置的 list、set 和 tuple 类，以及 collections 模块。

dir([object])

如果没有实参，则返回当前本地作用域中的名称列表。如果有实参，它会尝试返回该对象的有效属性列表。

如果对象有一个名为 __dir__() 的方法，那么该方法将被调用，并且必须返回一个属性列表。这允许实现自定义 __getattr__() 或 __getattribute__() 函数的对象能够自定义 dir() 来报告它们的属性。

如果对象不提供 __dir__()，这个函数会尝试从对象已定义的 __dict__ 属性和类型对象收集信息。结果列表并不总是完整的，如果对象有自定义 __getattr__()，那结果可能不准确。

默认的 dir() 机制对不同类型的对象行为不同，它会试图返回最相关而不是最全的信息：

如果对象是模块对象，则列表包含模块的属性名称。

如果对象是类型或类对象，则列表包含它们的属性名称，并且递归查找所有基类的属性。

否则，列表包含对象的属性名称，它的类属性名称，并且递归查找它的类的所有基类的属性。

返回的列表按字母表排序。例如：

>>> import struct
>>> dir()   # show the names in the module namespace
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']

注解

因为 dir() 主要是为了便于在交互式时使用，所以它会试图返回人们感兴趣的名字集合，而不是试图保证结果的严格性或一致性，它具体的行为也可能在不同版本之间改变。例如，当实参是一个类时，metaclass 的属性不包含在结果列表中。

divmod(a, b)

它将两个（非复数）数字作为实参，并在执行整数除法时返回一对商和余数。对于混合操作数类型，适用双目算术运算符的规则。对于整数，结果和 (a // b, a % b) 一致。对于浮点数，结果是 (q, a % b) ，q 通常是 math.floor(a / b) 但可能会比 1 小。在任何情况下， q * b + a % b 和 a 基本相等；如果 a % b 非零，它的符号和 b 一样，并且 0 <= abs(a % b) < abs(b) 。

enumerate(iterable, start=0)

返回一个枚举对象。iterable 必须是一个序列，或 iterator，或其他支持迭代的对象。 enumerate() 返回的迭代器的 __next__() 方法返回一个元组，里面包含一个计数值（从 start 开始，默认为 0）和通过迭代 iterable 获得的值。

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

等价于:

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1

eval(expression, globals=None, locals=None)

实参是一个字符串，以及可选的 globals 和 locals。globals 实参必须是一个字典。locals 可以是任何映射对象。

expression 参数会作为一个 Python 表达式（从技术上说是一个条件列表）被解析并求值，使用 globals 和 locals 字典作为全局和局部命名空间。 如果 globals 字典存在且不包含以 __builtins__ 为键的值，则会在解析 expression 之前插入以此为键的对内置模块 builtins 的字典的引用。 这意味着 expression 通常具有对标准 builtins 模块的完全访问权限且受限的环境会被传播。 如果省略 locals 字典则其默认值为 globals 字典。 如果两个字典同时省略，表达式会在 eval() 被调用的环境中执行。 返回值为表达式求值的结果。 语法错误将作为异常被报告。 例如:

>>> x = 1
>>> eval('x+1')
2

这个函数也可以用来执行任何代码对象（如 compile() 创建的）。这种情况下，参数是代码对象，而不是字符串。如果编译该对象时的 mode 实参是 'exec' 那么 eval() 返回值为 None 。

提示： exec() 函数支持动态执行语句。 globals() 和 locals() 函数各自返回当前的全局和本地字典，因此您可以将它们传递给 eval() 或 exec() 来使用。

另外可以参阅 ast.literal_eval()，该函数可以安全执行仅包含文字的表达式字符串。

exec(object[, globals[, locals]])

这个函数支持动态执行 Python 代码。object 必须是字符串或者代码对象。如果是字符串，那么该字符串将被解析为一系列 Python 语句并执行（除非发生语法错误）。1 如果是代码对象，它将被直接执行。在任何情况下，被执行的代码都需要和文件输入一样是有效的（见参考手册中关于文件输入的章节）。请注意即使在传递给 exec() 函数的代码的上下文中，return 和 yield 语句也不能在函数定义之外使用。该函数返回值是 None 。

无论哪种情况，如果省略了可选参数，代码将在当前范围内执行。如果提供了 globals 参数，就必须是字典类型，而且会被用作全局和本地变量。如果同时提供了 globals 和 locals 参数，它们分别被用作全局和本地变量。如果提供了 locals 参数，则它可以是任何映射型的对象。请记住在模块层级，全局和本地变量是相同的字典。如果 exec 有两个不同的 globals 和 locals 对象，代码就像嵌入在类定义中一样执行。

如果 globals 字典不包含 __builtins__ 键值，则将为该键插入对内建 builtins 模块字典的引用。因此，在将执行的代码传递给 exec() 之前，可以通过将自己的 __builtins__ 字典插入到 globals 中来控制可以使用哪些内置代码。

注解

内置 globals() 和 locals() 函数各自返回当前的全局和本地字典，因此可以将它们传递给 exec() 的第二个和第三个实参。

注解

默认情况下，locals 的行为如下面 locals() 函数描述的一样：不要试图改变默认的 locals 字典。如果您想在 exec() 函数返回时知道代码对 locals 的变动，请明确地传递 locals 字典。

filter(function, iterable)

用 iterable 中函数 function 返回真的那些元素，构建一个新的迭代器。iterable 可以是一个序列，一个支持迭代的容器，或一个迭代器。如果 function 是 None ，则会假设它是一个身份函数，即 iterable 中所有返回假的元素会被移除。

请注意， filter(function, iterable) 相当于一个生成器表达式，当 function 不是 None 的时候为 (item for item in iterable if function(item))；function 是 None 的时候为 (item for item in iterable if item) 。

请参阅 itertools.filterfalse() 了解，只有 function 返回 false 时才选取 iterable 中元素的补充函数。

class float([x])

返回从数字或字符串 x 生成的浮点数。

如果实参是字符串，则它必须是包含十进制数字的字符串，字符串前面可以有符号，之前也可以有空格。可选的符号有 '+' 和 '-' ； '+' 对创建的值没有影响。实参也可以是 NaN（非数字）、正负无穷大的字符串。确切地说，除去首尾的空格后，输入必须遵循以下语法：

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value

这里， floatnumber 是 Python 浮点数的字符串形式，详见 浮点数字面值。字母大小写都可以，例如，“inf”、“Inf”、“INFINITY”、“iNfINity” 都可以表示正无穷大。

另一方面，如果实参是整数或浮点数，则返回具有相同值（在 Python 浮点精度范围内）的浮点数。如果实参在 Python 浮点精度范围外，则会触发 OverflowError。

对于一般的 Python 对象 x ， float(x) 指派给 x.__float__() 。

如果没有实参，则返回 0.0 。

示例:

>>> float('+1.23')
1.23
>>> float('   -12345\n')
-12345.0
>>> float('1e-003')
0.001
>>> float('+1E6')
1000000.0
>>> float('-Infinity')
-inf

数字类型 — int, float, complex 描述了浮点类型。

在 3.6 版更改: 您可以使用下划线将代码文字中的数字进行分组。

format(value[, format_spec])

将 value 转换为 format_spec 控制的“格式化”表示。format_spec 的解释取决于 value 实参的类型，但是大多数内置类型使用标准格式化语法：格式规格迷你语言。

默认的 format_spec 是一个空字符串，它通常和调用 str(value) 的结果相同。

调用 format(value, format_spec) 会转换成 type(value).__format__(value, format_spec) ，所以实例字典中的 __format__() 方法将不会调用。如果搜索到 object 有这个方法但 format_spec 不为空，format_spec 或返回值不是字符串，会触发 TypeError 异常。

在 3.4 版更改: 当 format_spec 不是空字符串时， object().__format__(format_spec) 会触发 TypeError。

class frozenset([iterable])

返回一个新的 frozenset 对象，它包含可选参数 iterable 中的元素。 frozenset 是一个内置的类。有关此类的文档，请参阅 frozenset 和 集合类型 — set, frozenset。

请参阅内建的 set、list、tuple 和 dict 类，以及 collections 模块来了解其它的容器。

getattr(object, name[, default])

返回对象命名属性的值。name 必须是字符串。如果该字符串是对象的属性之一，则返回该属性的值。例如， getattr(x, 'foobar') 等同于 x.foobar。如果指定的属性不存在，且提供了 default 值，则返回它，否则触发 AttributeError。

globals()

返回表示当前全局符号表的字典。这总是当前模块的字典（在函数或方法中，不是调用它的模块，而是定义它的模块）。

hasattr(object, name)

该实参是一个对象和一个字符串。如果字符串是对象的属性之一的名称，则返回 True，否则返回 False。（此功能是通过调用 getattr(object, name) 看是否有 AttributeError 异常来实现的。）

hash(object)

返回该对象的哈希值（如果它有的话）。哈希值是整数。它们在字典查找元素时用来快速比较字典的键。相同大小的数字变量有相同的哈希值（即使它们类型不同，如 1 和 1.0）。

注解

如果对象实现了自己的 __hash__() 方法，请注意，hash() 根据机器的字长来截断返回值。另请参阅 __hash__()。

help([object])

启动内置的帮助系统（此函数主要在交互式中使用）。如果没有实参，解释器控制台里会启动交互式帮助系统。如果实参是一个字符串，则在模块、函数、类、方法、关键字或文档主题中搜索该字符串，并在控制台上打印帮助信息。如果实参是其他任意对象，则会生成该对象的帮助页。

该函数通过 site 模块加入到内置命名空间。

在 3.4 版更改: pydoc 和 inspect 的变更使得可调用对象的签名信息更加全面和一致。

hex(x)

将整数转换为以“0x”为前缀的小写十六进制字符串。如果 x 不是 Python int 对象，则必须定义返回整数的 __index__() 方法。一些例子：

>>> hex(255)
'0xff'
>>> hex(-42)
'-0x2a'

如果要将整数转换为大写或小写的十六进制字符串，并可选择有无“0x”前缀，则可以使用如下方法：

>>> '%#x' % 255, '%x' % 255, '%X' % 255
('0xff', 'ff', 'FF')
>>> format(255, '#x'), format(255, 'x'), format(255, 'X')
('0xff', 'ff', 'FF')
>>> f'{255:#x}', f'{255:x}', f'{255:X}'
('0xff', 'ff', 'FF')

另见 format() 获取更多信息。

另请参阅 int() 将十六进制字符串转换为以 16 为基数的整数。

注解

如果要获取浮点数的十六进制字符串形式，请使用 float.hex() 方法。

id(object)

返回对象的“标识值”。该值是一个整数，在此对象的生命周期中保证是唯一且恒定的。两个生命期不重叠的对象可能具有相同的 id() 值。

CPython implementation detail: This is the address of the object in memory.

input([prompt])

如果存在 prompt 实参，则将其写入标准输出，末尾不带换行符。接下来，该函数从输入中读取一行，将其转换为字符串（除了末尾的换行符）并返回。当读取到 EOF 时，则触发 EOFError。例如:

>>> s = input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"

如果加载了 readline 模块，input() 将使用它来提供复杂的行编辑和历史记录功能。

class int(x=0)

class int(x, base=10)

返回一个使用数字或字符串 x 生成的整数对象，或者没有实参的时候返回 0 。如果 x 定义了 __int__()，int(x) 返回 x.__int__() 。如果 x 定义了 __trunc__()，它返回 x.__trunc__() 。对于浮点数，它向零舍入。

如果 x 不是数字，或者有 base 参数，x 必须是字符串、bytes、表示进制为 base 的 整数字面值 的 bytearray 实例。该文字前可以有 + 或 - （中间不能有空格），前后可以有空格。一个进制为 n 的数字包含 0 到 n-1 的数，其中 a 到 z （或 A 到 Z ）表示 10 到 35。默认的 base 为 10 ，允许的进制有 0、2-36。2、8、16 进制的数字可以在代码中用 0b/0B 、 0o/0O 、 0x/0X 前缀来表示。进制为 0 将安照代码的字面量来精确解释，最后的结果会是 2、8、10、16 进制中的一个。所以 int('010', 0) 是非法的，但 int('010') 和 int('010', 8) 是合法的。

整数类型定义请参阅 数字类型 — int, float, complex 。

在 3.4 版更改: 如果 base 不是 int 的实例，但 base 对象有 base.__index__ 方法，则会调用该方法来获取进制数。以前的版本使用 base.__int__ 而不是 base.__index__。

在 3.6 版更改: 您可以使用下划线将代码文字中的数字进行分组。

isinstance(object, classinfo)

Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. If object is not an object of the given type, the function always returns false. If classinfo is a tuple of type objects (or recursively, other such tuples), return true if object is an instance of any of the types. If classinfo is not a type or tuple of types and such tuples, a TypeError exception is raised.

issubclass(class, classinfo)

Return true if class is a subclass (direct, indirect or virtual) of classinfo. A class is considered a subclass of itself. classinfo may be a tuple of class objects, in which case every entry in classinfo will be checked. In any other case, a TypeError exception is raised.

iter(object[, sentinel])

返回一个 iterator 对象。根据是否存在第二个实参，第一个实参的解释是非常不同的。如果没有第二个实参，object 必须是支持迭代协议（有 __iter__() 方法）的集合对象，或必须支持序列协议（有 __getitem__() 方法，且数字参数从 0 开始）。如果它不支持这些协议，会触发 TypeError。如果有第二个实参 sentinel，那么 object 必须是可调用的对象。这种情况下生成的迭代器，每次迭代调用它的 __next__() 方法时都会不带实参地调用 object；如果返回的结果是 sentinel 则触发 StopIteration，否则返回调用结果。

另请参阅 迭代器类型。

One useful application of the second form of iter() is to read lines of a file until a certain line is reached. The following example reads a file until the readline() method returns an empty string:

with open('mydata.txt') as fp:
    for line in iter(fp.readline, ''):
        process_line(line)

len(s)

返回对象的长度（元素个数）。实参可以是序列（如 string、bytes、tuple、list 或 range 等）或集合（如 dictionary、set 或 frozen set 等）。

class list([iterable])

虽然被称为函数，list 实际上是一种可变序列类型，详情请参阅 列表 和 序列类型 — list, tuple, range。

locals()

Update and return a dictionary representing the current local symbol table. Free variables are returned by locals() when it is called in function blocks, but not in class blocks.

注解

不要更改此字典的内容；更改不会影响解释器使用的局部变量或自由变量的值。

map(function, iterable, ...)

返回一个将 function 应用于 iterable 中每一项并输出其结果的迭代器。 如果传入了额外的 iterable 参数，function 必须接受相同个数的实参并被应用于从所有可迭代对象中并行获取的项。 当有多个可迭代对象时，最短的可迭代对象耗尽则整个迭代就将结束。 对于函数的输入已经是参数元组的情况，请参阅 itertools.starmap()。

max(iterable, *[, key, default])

max(arg1, arg2, *args[, key])

返回可迭代对象中最大的元素，或者返回两个及以上实参中最大的。

如果只提供了一个位置参数，它必须是非空 iterable，返回可迭代对象中最大的元素；如果提供了两个及以上的位置参数，则返回最大的位置参数。

有两个可选只能用关键字的实参。key 实参指定排序函数用的参数，如传给 list.sort() 的。default 实参是当可迭代对象为空时返回的值。如果可迭代对象为空，并且没有给 default ，则会触发 ValueError。

如果有多个最大元素，则此函数将返回第一个找到的。这和其他稳定排序工具如 sorted(iterable, key=keyfunc, reverse=True)[0] 和 heapq.nlargest(1, iterable, key=keyfunc) 保持一致。

3.4 新版功能: keyword-only 实参 default 。

memoryview(obj)

返回由给定实参创建的“内存视图”对象。有关详细信息，请参阅 内存视图。

min(iterable, *[, key, default])

min(arg1, arg2, *args[, key])

返回可迭代对象中最小的元素，或者返回两个及以上实参中最小的。

如果只提供了一个位置参数，它必须是 iterable，返回可迭代对象中最小的元素；如果提供了两个及以上的位置参数，则返回最小的位置参数。

有两个可选只能用关键字的实参。key 实参指定排序函数用的参数，如传给 list.sort() 的。default 实参是当可迭代对象为空时返回的值。如果可迭代对象为空，并且没有给 default ，则会触发 ValueError。

如果有多个最小元素，则此函数将返回第一个找到的。这和其他稳定排序工具如 sorted(iterable, key=keyfunc)[0] 和 heapq.nsmallest(1, iterable, key=keyfunc) 保持一致。

3.4 新版功能: keyword-only 实参 default 。

next(iterator[, default])

通过调用 iterator 的 __next__() 方法获取下一个元素。如果迭代器耗尽，则返回给定的 default，如果没有默认值则触发 StopIteration。

class object

返回一个没有特征的新对象。object 是所有类的基类。它具有所有 Python 类实例的通用方法。这个函数不接受任何实参。

注解

由于 object 没有 __dict__，因此无法将任意属性赋给 object 的实例。

oct(x)

将一个整数转变为一个前缀为“0o”的八进制字符串。结果是一个合法的 Python 表达式。如果 x 不是 Python 的 int 对象，那它需要定义 __index__() 方法返回一个整数。一些例子：

>>> oct(8)
'0o10'
>>> oct(-56)
'-0o70'

如果要将整数转换为八进制字符串，并可选择有无“0o”前缀，则可以使用如下方法：

>>> '%#o' % 10, '%o' % 10
('0o12', '12')
>>> format(10, '#o'), format(10, 'o')
('0o12', '12')
>>> f'{10:#o}', f'{10:o}'
('0o12', '12')

另见 format() 获取更多信息。

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

打开 file 并返回对应的 file object。如果该文件不能打开，则触发 OSError。

file 是一个 path-like object，表示将要打开的文件的路径（绝对路径或者当前工作目录的相对路径），也可以是要被封装的整数类型文件描述符。（如果是文件描述符，它会随着返回的 I/O 对象关闭而关闭，除非 closefd 被设为 False 。）

mode 是一个可选字符串，用于指定打开文件的模式。默认值是 'r' ，这意味着它以文本模式打开并读取。其他常见模式有：写入 'w' （截断已经存在的文件）；排它性创建 'x' ；追加写 'a' （在 一些 Unix 系统上，无论当前的文件指针在什么位置，所有 写入都会追加到文件末尾）。在文本模式，如果 encoding 没有指定，则根据平台来决定使用的编码：使用 locale.getpreferredencoding(False) 来获取本地编码。（要读取和写入原始字节，请使用二进制模式并不要指定 encoding。）可用的模式有：

字符

含义

'r'

读取（默认）

'w'

写入，并先截断文件

'x'

排它性创建，如果文件已存在则失败

'a'

写入，如果文件存在则在末尾追加

'b'

二进制模式

't'

文本模式（默认）

'+'

更新磁盘文件（读取并写入）

'U'

universal newlines mode (deprecated)

默认的模式是 'r' （打开并读取文本，同 'rt' ）。对于二进制写入， 'w+b' 模式打开并把文件截断成 0 字节； 'r+b' 则不会截断。

正如在 概述 中提到的，Python区分二进制和文本I/O。以二进制模式打开的文件（包括 mode 参数中的 'b' ）返回的内容为 bytes`对象，不进行任何解码。在文本模式下（默认情况下，或者在 *mode* 参数中包含 `’t’` ）时，文件内容返回为 str ，首先使用指定的 encoding （如果给定）或者使用平台默认的的字节编码解码。

注解

Python不依赖于底层操作系统的文本文件概念;所有处理都由Python本身完成，因此与平台无关。

buffering 是一个可选的整数，用于设置缓冲策略。传递0以切换缓冲关闭（仅允许在二进制模式下），1选择行缓冲（仅在文本模式下可用），并且>1的整数以指示固定大小的块缓冲区的大小（以字节为单位）。如果没有给出 buffering 参数，则默认缓冲策略的工作方式如下:

二进制文件以固定大小的块进行缓冲；使用启发式方法选择缓冲区的大小，尝试确定底层设备的“块大小”或使用 io.DEFAULT_BUFFER_SIZE。在许多系统上，缓冲区的长度通常为4096或8192字节。

“交互式”文本文件（ isatty() 返回 True 的文件）使用行缓冲。其他文本文件使用上述策略用于二进制文件。

encoding 是用于解码或编码文件的编码的名称。这应该只在文本模式下使用。默认编码是依赖于平台的（不 管 locale.getpreferredencoding() 返回何值），但可以使用任何Python支持的 text encoding 。有关支持的编码列表，请参阅 codecs 模块。

errors 是一个可选的字符串参数，用于指定如何处理编码和解码错误 - 这不能在二进制模式下使用。可以使用各种标准错误处理程序（列在 错误处理方案 ），但是使用 codecs.register_error() 注册的任何错误处理名称也是有效的。标准名称包括:

如果存在编码错误，'strict' 会引发 ValueError 异常。 默认值 None 具有相同的效果。

'ignore' 忽略错误。请注意，忽略编码错误可能会导致数据丢失。

'replace' 会将替换标记（例如 '?' ）插入有错误数据的地方。

'surrogateescape' 将表示任何不正确的字节作为Unicode专用区中的代码点，范围从U+DC80到U+DCFF。当在写入数据时使用 surrogateescape 错误处理程序时，这些私有代码点将被转回到相同的字节中。这对于处理未知编码的文件很有用。

只有在写入文件时才支持 'xmlcharrefreplace'。编码不支持的字符将替换为相应的XML字符引用 &#nnn;。

'backslashreplace' 用Python的反向转义序列替换格式错误的数据。

'namereplace' （也只在编写时支持）用 \N{...} 转义序列替换不支持的字符。

newline 控制 universal newlines 模式如何生效（它仅适用于文本模式）。它可以是 None，''，'\n'，'\r' 和 '\r\n'。它的工作原理:

从流中读取输入时，如果 newline 为 None，则启用通用换行模式。输入中的行可以以 '\n'，'\r' 或 '\r\n' 结尾，这些行被翻译成 '\n' 在返回呼叫者之前。如果它是 ''，则启用通用换行模式，但行结尾将返回给调用者未翻译。如果它具有任何其他合法值，则输入行仅由给定字符串终止，并且行结尾将返回给未调用的调用者。

将输出写入流时，如果 newline 为 None，则写入的任何 '\n' 字符都将转换为系统默认行分隔符 os.linesep。如果 newline 是 '' 或 '\n'，则不进行翻译。如果 newline 是任何其他合法值，则写入的任何 '\n' 字符将被转换为给定的字符串。

如果 closefd 是 False 并且给出了文件描述符而不是文件名，那么当文件关闭时，底层文件描述符将保持打开状态。如果给出文件名则 closefd 必须为 True （默认值），否则将引发错误。

可以通过传递可调用的 opener 来使用自定义开启器。然后通过使用参数（ file，flags ）调用 opener 获得文件对象的基础文件描述符。 opener 必须返回一个打开的文件描述符（使用 os.open as opener 时与传递 None 的效果相同）。

新创建的文件是 不可继承的。

下面的示例使用 os.open() 函数的 dir_fd 的形参，从给定的目录中用相对路径打开文件:

>>> import os
>>> dir_fd = os.open('somedir', os.O_RDONLY)
>>> def opener(path, flags):
...     return os.open(path, flags, dir_fd=dir_fd)
...
>>> with open('spamspam.txt', 'w', opener=opener) as f:
...     print('This will be written to somedir/spamspam.txt', file=f)
...
>>> os.close(dir_fd)  # don't leak a file descriptor

open() 函数所返回的 file object 类型取决于所用模式。 当使用 open() 以文本模式 ('w', 'r', 'wt', 'rt' 等) 打开文件时，它将返回 io.TextIOBase (特别是 io.TextIOWrapper) 的一个子类。 当使用缓冲以二进制模式打开文件时，返回的类是 io.BufferedIOBase 的一个子类。 具体的类会有多种：在只读的二进制模式下，它将返回 io.BufferedReader；在写入二进制和追加二进制模式下，它将返回 io.BufferedWriter，而在读/写模式下，它将返回 io.BufferedRandom。 当禁用缓冲时，则会返回原始流，即 io.RawIOBase 的一个子类 io.FileIO。

另请参阅文件操作模块，例如 fileinput、io （声明了 open()）、os、os.path、tempfile 和 shutil。

在 3.3 版更改:

增加了 opener 形参。

增加了 'x' 模式。

过去触发的 IOError，现在是 OSError 的别名。

如果文件已存在但使用了排它性创建模式（ 'x' ），现在会触发 FileExistsError。

在 3.4 版更改:

文件现在禁止继承。

Deprecated since version 3.4, will be removed in version 4.0: 'U' 模式。

在 3.5 版更改:

如果系统调用被中断，但信号处理程序没有触发异常，此函数现在会重试系统调用，而不是触发 InterruptedError 异常 (原因详见 PEP 475 [https://www.python.org/dev/peps/pep-0475])。

增加了 'namereplace' 错误处理接口。

在 3.6 版更改:

增加对实现了 os.PathLike 对象的支持。

在 Windows 上，打开一个控制台缓冲区将返回 io.RawIOBase 的子类，而不是 io.FileIO。

ord(c)

对表示单个 Unicode 字符的字符串，返回代表它 Unicode 码点的整数。例如 ord('a') 返回整数 97， ord('€') （欧元符号）返回 8364 。这是 chr() 的逆函数。

pow(x, y[, z])

返回 x 的 y 次幂；如果 z 存在，则对 z 取余（比直接 pow(x, y) % z 计算更高效）。两个参数形式的 pow(x, y) 等价于幂运算符： x**y。

参数必须为数值类型。 对于混用的操作数类型，则适用二元算术运算符的类型强制转换规则。 对于 int 操作数，结果具有与操作数相同的类型（转换后），除非第二个参数为负值；在这种情况下，所有参数将被转换为浮点数并输出浮点数结果。 例如，10**2 返回 100，但 10**-2 返回 0.01。 如果第二个参数为负值，则第三个参数必须省略。 如果存在 z，则 x 和 y 必须为整数类型，且 y 必须为非负数。

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

将 objects 打印到 file 指定的文本流，以 sep 分隔并在末尾加上 end。 sep, end, file 和 flush 如果存在，它们必须以关键字参数的形式给出。

所有非关键字参数都会被转换为字符串，就像是执行了 str() 一样，并会被写入到流，以 sep 且在末尾加上 end。 sep 和 end 都必须为字符串；它们也可以为 None，这意味着使用默认值。 如果没有给出 objects，则 print() 将只写入 end。

file 参数必须是一个具有 write(string) 方法的对象；如果参数不存在或为 None，则将使用 sys.stdout。 由于要打印的参数会被转换为文本字符串，因此 print() 不能用于二进制模式的文件对象。 对于这些对象，应改用 file.write(...)。

输出是否被缓存通常决定于 file，但如果 flush 关键字参数为真值，流会被强制刷新。

在 3.3 版更改: 增加了 flush 关键字参数。

class property(fget=None, fset=None, fdel=None, doc=None)

返回 property 属性。

fget 是获取属性值的函数。 fset 是用于设置属性值的函数。 fdel 是用于删除属性值的函数。并且 doc 为属性对象创建文档字符串。

一个典型的用法是定义一个托管属性 x:

class C:
    def __init__(self):
        self._x = None

    def getx(self):
        return self._x

    def setx(self, value):
        self._x = value

    def delx(self):
        del self._x

    x = property(getx, setx, delx, "I'm the 'x' property.")

如果 c 是 C 的实例，c.x 将调用getter，c.x = value 将调用setter， del c.x 将调用deleter。

如果给出，doc 将成为该 property 属性的文档字符串。 否则该 property 将拷贝 fget 的文档字符串（如果存在）。 这令使用 property() 作为 decorator 来创建只读的特征属性可以很容易地实现:

class Parrot:
    def __init__(self):
        self._voltage = 100000

    @property
    def voltage(self):
        """Get the current voltage."""
        return self._voltage

以上 @property 装饰器会将 voltage() 方法转化为一个具有相同名称的只读属性的 “getter”，并将 voltage 的文档字符串设置为 “Get the current voltage.”

特征属性对象具有 getter, setter 以及 deleter 方法，它们可用作装饰器来创建该特征属性的副本，并将相应的访问函数设为所装饰的函数。 这最好是用一个例子来解释:

class C:
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I'm the 'x' property."""
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

    @x.deleter
    def x(self):
        del self._x

上述代码与第一个例子完全等价。 注意一定要给附加函数与原始的特征属性相同的名称 (在本例中为 x。)

返回的特征属性对象同样具有与构造器参数相对应的属性 fget, fset 和 fdel。

在 3.5 版更改: 特征属性对象的文档字符串现在是可写的。

range(stop)

range(start, stop[, step])

虽然被称为函数，但 range 实际上是一个不可变的序列类型，参见在 range 对象 与 序列类型 — list, tuple, range 中的文档说明。

repr(object)

返回包含一个对象的可打印表示形式的字符串。 对于许多类型来说，该函数会尝试返回的字符串将会与该对象被传递给 eval() 时所生成的对象具有相同的值，在其他情况下表示形式会是一个括在尖括号中的字符串，其中包含对象类型的名称与通常包括对象名称和地址的附加信息。 类可以通过定义 __repr__() 方法来控制此函数为它的实例所返回的内容。

reversed(seq)

返回一个反向的 iterator。 seq 必须是一个具有 __reversed__() 方法的对象或者是支持该序列协议（具有从 0 开始的整数类型参数的 __len__() 方法和 __getitem__() 方法）。

round(number[, ndigits])

返回 number 舍入到小数点后 ndigits 位精度的值。 如果 ndigits 被省略或为 None，则返回最接近输入值的整数。

对于支持 round() 的内置类型，值会被舍入到最接近的 10 的负 ndigits 次幂的倍数；如果与两个倍数的距离相等，则选择偶数 (因此，round(0.5) 和 round(-0.5) 均为 0 而 round(1.5) 为 2)。 任何整数值都可作为有效的 ndigits (正数、零或负数)。 如果 ndigits 被省略或为 None 则返回值将为整数。 否则返回值与 number 的类型相同。

对于一般的 Python 对象 number, round 将委托给 number.__round__。

注解

对浮点数执行 round() 的行为可能会令人惊讶：例如，round(2.675, 2) 将给出 2.67 而不是期望的 2.68。 这不算是程序错误：这一结果是由于大多数十进制小数实际上都不能以浮点数精确地表示。 请参阅 浮点算术：争议和限制 了解更多信息。

class set([iterable])

返回一个新的 set 对象，可以选择带有从 iterable 获取的元素。 set 是一个内置类型。 请查看 set 和 集合类型 — set, frozenset 获取关于这个类的文档。

有关其他容器请参看内置的 frozenset, list, tuple 和 dict 类，以及 collections 模块。

setattr(object, name, value)

此函数与 getattr() 两相对应。 其参数为一个对象、一个字符串和一个任意值。 字符串指定一个现有属性或者新增属性。 函数会将值赋给该属性，只要对象允许这种操作。 例如，setattr(x, 'foobar', 123) 等价于 x.foobar = 123。

class slice(stop)

class slice(start, stop[, step])

返回一个表示由 range(start, stop, step) 所指定索引集的 slice 对象。 其中 start 和 step 参数默认为 None。 切片对象具有仅会返回对应参数值（或其默认值）的只读数据属性 start, stop 和 step。 它们没有其他的显式功能；不过它们会被 NumPy 以及其他第三方扩展所使用。 切片对象也会在使用扩展索引语法时被生成。 例如: a[start:stop:step] 或 a[start:stop, i]。 请参阅 itertools.islice() 了解返回迭代器的一种替代版本。

sorted(iterable, *, key=None, reverse=False)

根据 iterable 中的项返回一个新的已排序列表。

具有两个可选参数，它们都必须指定为关键字参数。

key 指定带有单个参数的函数，用于从 iterable 的每个元素中提取用于比较的键 (例如 key=str.lower)。 默认值为 None (直接比较元素)。

reverse 为一个布尔值。 如果设为 True，则每个列表元素将按反向顺序比较进行排序。

使用 functools.cmp_to_key() 可将老式的 cmp 函数转换为 key 函数。

内置的 sorted() 确保是稳定的。 如果一个排序确保不会改变比较结果相等的元素的相对顺序就称其为稳定的 — 这有利于进行多重排序（例如先按部门、再按薪级排序）。

有关排序示例和简要排序教程，请参阅 排序指南 。

@staticmethod

将方法转换为静态方法。

静态方法不会接收隐式的第一个参数。要声明一个静态方法，请使用此语法

class C:
    @staticmethod
    def f(arg1, arg2, ...): ...

The @staticmethod form is a function decorator – see the description of function definitions in 函数定义 for details.

It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.

Python中的静态方法与Java或C ++中的静态方法类似。另请参阅 classmethod() ，用于创建备用类构造函数的变体。

像所有装饰器一样，也可以像常规函数一样调用 staticmethod ，并对其结果执行某些操作。比如某些情况下需要从类主体引用函数并且您希望避免自动转换为实例方法。对于这些情况，请使用此语法:

class C:
    builtin_open = staticmethod(open)

For more information on static methods, consult the documentation on the standard type hierarchy in 标准类型层级结构.

class str(object='')

class str(object=b'', encoding='utf-8', errors='strict')

返回一个 str 版本的 object 。有关详细信息，请参阅 str() 。

str 是内置字符串 class 。更多关于字符串的信息查看 文本序列类型 — str。

sum(iterable[, start])

从 start 开始自左向右对 iterable 中的项求和并返回总计值。 start 默认为 0。 iterable 的项通常为数字，开始值则不允许为字符串。

对某些用例来说，存在 sum() 的更好替代。 拼接字符串序列的更好更快方式是调用 ''.join(sequence)。 要以扩展精度对浮点值求和，请参阅 math.fsum()。 要拼接一系列可迭代对象，请考虑使用 itertools.chain()。

super([type[, object-or-type]])

返回一个代理对象，它会将方法调用委托给 type 指定的父类或兄弟类。 这对于访问已在类中被重载的继承方法很有用。 搜索顺序与 getattr() 所使用的相同，只是 type 指定的类型本身会被跳过。

type 的 __mro__ 属性列出了 getattr() 和 super() 所使用的方法解析顺序。 该属性是动态的，可以在继承层级结构更新的时候任意改变。

如果省略第二个参数，则返回的超类对象是未绑定的。 如果第二个参数为一个对象，则 isinstance(obj, type) 必须为真值。 如果第二个参数为一个类型，则 issubclass(type2, type) 必须为真值（这适用于类方法）。

super 有两个典型用例。 在具有单继承的类层级结构中，super 可用来引用父类而不必显式地指定它们的名称，从而令代码更易维护。 这种用法与其他编程语言中 super 的用法非常相似。

第二个用例是在动态执行环境中支持协作多重继承。 此用例为 Python 所独有，在静态编译语言或仅支持单继承的语言中是不存在的。 这使得实现“菱形图”成为可能，在这时会有多个基类实现相同的方法。 好的设计强制要求这种方法在每个情况下具有相同的调用签名（因为调用顺序是在运行时确定的，也因为该顺序要适应类层级结构的更改，还因为该顺序可能包含在运行时之前未知的兄弟类）。

对于以上两个用例，典型的超类调用看起来是这样的:

class C(B):
    def method(self, arg):
        super().method(arg)    # This does the same thing as:
                               # super(C, self).method(arg)

请注意 super() 是作为显式加点属性查找的绑定过程的一部分来实现的，例如 super().__getitem__(name)。 它做到这一点是通过实现自己的 __getattribute__() 方法，这样就能以可预测的顺序搜索类，并且支持协作多重继承。 对应地，super() 在像 super()[name] 这样使用语句或操作符进行隐式查找时则未被定义。

还要注意的是，除了零个参数的形式以外，super() 并不限于在方法内部使用。 两个参数的形式明确指定参数并进行相应的引用。 零个参数的形式仅适用于类定义内部，因为编译器需要填入必要的细节以正确地检索到被定义的类，还需要让普通方法访问当前实例。

对于有关如何使用 super() 来如何设计协作类的实用建议，请参阅 使用 super() 的指南 [https://rhettinger.wordpress.com/2011/05/26/super-considered-super/]。

tuple([iterable])

虽然被称为函数，但 tuple 实际上是一个不可变的序列类型，参见在 元组 与 序列类型 — list, tuple, range 中的文档说明。

class type(object)

class type(name, bases, dict)

传入一个参数时，返回 object 的类型。 返回值是一个 type 对象，通常与 object.__class__ 所返回的对象相同。

推荐使用 isinstance() 内置函数来检测对象的类型，因为它会考虑子类的情况。

传入三个参数时，返回一个新的 type 对象。 这在本质上是 class 语句的一种动态形式。 name 字符串即类名并且会成为 __name__ 属性；bases 元组列出基类并且会成为 __bases__ 属性；而 dict 字典为包含类主体定义的命名空间并且会被复制到一个标准字典成为 __dict__ 属性。 例如，下面两条语句会创建相同的 type 对象:

>>> class X:
...     a = 1
...
>>> X = type('X', (object,), dict(a=1))

另请参阅 类型对象。

在 3.6 版更改: type 的子类如果未重载 type.__new__，将不再能使用一个参数的形式来获取对象的类型。

vars([object])

返回模块、类、实例或任何其它具有 __dict__ 属性的对象的 __dict__ 属性。

模块和实例这样的对象具有可更新的 __dict__ 属性；但是，其它对象的 __dict__ 属性可能会设为限制写入（例如，类会使用 types.MappingProxyType 来防止直接更新字典）。

不带参数时，vars() 的行为类似 locals()。 请注意，locals 字典仅对于读取起作用，因为对 locals 字典的更新会被忽略。

zip(*iterables)

创建一个聚合了来自每个可迭代对象中的元素的迭代器。

返回一个元组的迭代器，其中的第 i 个元组包含来自每个参数序列或可迭代对象的第 i 个元素。 当所输入可迭代对象中最短的一个被耗尽时，迭代器将停止迭代。 当只有一个可迭代对象参数时，它将返回一个单元组的迭代器。 不带参数时，它将返回一个空迭代器。 相当于:

def zip(*iterables):
    # zip('ABCD', 'xy') --> Ax By
    sentinel = object()
    iterators = [iter(it) for it in iterables]
    while iterators:
        result = []
        for it in iterators:
            elem = next(it, sentinel)
            if elem is sentinel:
                return
            result.append(elem)
        yield tuple(result)

函数会保证可迭代对象按从左至右的顺序被求值。 使得可以通过 zip(*[iter(s)]*n) 这样的惯用形式将一系列数据聚类为长度为 n 的分组。 这将重复 同样的 迭代器 n 次，以便每个输出的元组具有第 n 次调用该迭代器的结果。 它的作用效果就是将输入拆分为长度为 n 的数据块。

当你不用关心较长可迭代对象末尾不匹配的值时，则 zip() 只须使用长度不相等的输入即可。 如果那些值很重要，则应改用 itertools.zip_longest()。

zip() 与 * 运算符相结合可以用来拆解一个列表:

>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zip(x, y))
>>> x == list(x2) and y == list(y2)
True

__import__(name, globals=None, locals=None, fromlist=(), level=0)

注解

与 importlib.import_module() 不同，这是一个日常 Python 编程中不需要用到的高级函数。

This function is invoked by the import statement. It can be replaced (by importing the builtins module and assigning to builtins.__import__) in order to change semantics of the import statement, but doing so is strongly discouraged as it is usually simpler to use import hooks (see PEP 302 [https://www.python.org/dev/peps/pep-0302]) to attain the same goals and does not cause issues with code which assumes the default import implementation is in use. Direct use of __import__() is also discouraged in favor of importlib.import_module().

该函数会导入 name 模块，有可能使用给定的 globals 和 locals 来确定如何在包的上下文中解读名称。 fromlist 给出了应该从由 name 指定的模块导入对象或子模块的名称。 标准实现完全不使用其 locals 参数，而仅使用 globals 参数来确定 import 语句的包上下文。

level 指定是使用绝对还是相对导入。 0 (默认值) 意味着仅执行绝对导入。 level 为正数值表示相对于模块调用 __import__() 的目录，将要搜索的父目录层数 (详情参见 PEP 328 [https://www.python.org/dev/peps/pep-0328])。

当 name 变量的形式为 package.module 时，通常将会返回最高层级的包（第一个点号之前的名称），而 不是 以 name 命名的模块。 但是，当给出了非空的 fromlist 参数时，则将返回以 name 命名的模块。

例如，语句 import spam 的结果将为与以下代码作用相同的字节码:

spam = __import__('spam', globals(), locals(), [], 0)

语句 import spam.ham 的结果将为以下调用:

spam = __import__('spam.ham', globals(), locals(), [], 0)

请注意在这里 __import__() 是如何返回顶层模块的，因为这是通过 import 语句被绑定到特定名称的对象。

另一方面，语句 from spam.ham import eggs, sausage as saus 的结果将为

_temp = __import__('spam.ham', globals(), locals(), ['eggs', 'sausage'], 0)
eggs = _temp.eggs
saus = _temp.sausage

在这里， spam.ham 模块会由 __import__() 返回。 要导入的对象将从此对象中提取并赋值给它们对应的名称。

如果您只想按名称导入模块（可能在包中），请使用 importlib.import_module()

在 3.3 版更改: Negative values for level are no longer supported (which also changes the default value to 0).

备注

1

解析器只接受 Unix 风格的行结束符。如果您从文件中读取代码，请确保用换行符转换模式转换 Windows 或 Mac 风格的换行符。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

3. 内置常量

有少数的常量存在于内置命名空间中。 它们是：

False

bool 类型的假值。 给 False 赋值是非法的并会引发 SyntaxError。

True

bool 类型的真值。 给 True 赋值是非法的并会引发 SyntaxError。

None

NoneType 类型的唯一值。 None 经常用于表示缺少值，当因为默认参数未传递给函数时。 给 None 赋值是非法的并会引发 SyntaxError。

NotImplemented

二进制特殊方法应返回的特殊值（例如，__eq__()、__lt__()、__add __()、__rsub__() 等）表示操作没有针对其他类型实现；为了相同的目的，可以通过就地二进制特殊方法（例如，__imul __()、__ rightnd__() 等）返回。 它的逻辑值为真。

注解

当二进制（或就地）方法返回``NotImplemented``时，解释器将尝试对另一种类型（或其他一些回滚操作，取决于运算符）的反射操作。 如果所有尝试都返回``NotImplemented``，则解释器将引发适当的异常。 错误返回的``NotImplemented``将导致误导性错误消息或返回到Python代码中的``NotImplemented``值。

参见 实现算术运算 为例。

注解

NotImplementedError 和 NotImplemented 不可互换，即使它们有相似的名称和用途。 有关何时使用它的详细信息，请参阅 NotImplementedError。

Ellipsis

与省略号文字字面 “...” 相同。 特殊值主要与用户定义的容器数据类型的扩展切片语法结合使用。

__debug__

如果 Python 没有以 -O 选项启动，则此常量为真值。 另请参见 assert 语句。

注解

变量名 None，False，True 和 __ debug__ 无法重新赋值（赋值给它们，即使是属性名，将引发 SyntaxError ），所以它们可以被认为是“真正的”常数。

3.1. 由 site 模块添加的常量

site 模块（在启动期间自动导入，除非给出 -S 命令行选项）将几个常量添加到内置命名空间。 它们对交互式解释器 shell 很有用，并且不应在程序中使用。

quit(code=None)

exit(code=None)

当打印此对象时，会打印出一条消息，例如“Use quit() or Ctrl-D (i.e. EOF) to exit”，当调用此对象时，将使用指定的退出代码来引发 SystemExit。

copyright

credits

打印或调用的对象分别打印版权或作者的文本。

license

当打印此对象时，会打印出一条消息“Type license() to see the full license text”，当调用此对象时，将以分页形式显示完整的许可证文本（每次显示一屏）。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

4. 内置类型

以下部分描述了解释器中内置的标准类型。

主要内置类型有数字、序列、映射、类、实例和异常。

有些多项集类是可变的。 它们用于添加、移除或重排其成员的方法将原地执行，并不返回特定的项，绝对不会返回多项集实例自身而是返回 None。

有些操作受多种对象类型的支持；特别地，实际上所有对象都可以被比较、检测逻辑值，以及转换为字符串（使用 repr() 函数或略有差异的 str() 函数）。 后一个函数是在对象由 print() 函数输出时被隐式地调用的。

4.1. 逻辑值检测

任何对象都可以进行逻辑值的检测，以便在 if 或 while 作为条件或是作为下文所述布尔运算的操作数来使用。

一个对象在默认情况下均被视为真值，除非当该对象被调用时其所属类定义了 __bool__() 方法且返回 False 或是定义了 __len__() 方法且返回零。 1 下面基本完整地列出了会被视为假值的内置对象:

被定义为假值的常量: None 和 False。

任何数值类型的零: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)

空的序列和多项集: '', (), [], {}, set(), range(0)

产生布尔值结果的运算和内置函数总是返回 0 或 False 作为假值，1 或 True 作为真值，除非另行说明。 （重要例外：布尔运算 or 和 and 总是返回其中一个操作数。）

4.2. Boolean Operations — and, or, not

这些属于布尔运算，按优先级升序排列:

运算

结果：

注释

x or y

if x is false, then y, else x

(1)

x and y

if x is false, then x, else y

(2)

not x

if x is false, then True, else False

(3)

注释:

这是个短路运算符，因此只有在第一个参数为假值时才会对第二个参数求值。

这是个短路运算符，因此只有在第一个参数为真值时才会对第二个参数求值。

not 的优先级比非布尔运算符低，因此 not a == b 会被解读为 not (a == b) 而 a == not b 会引发语法错误。

4.3. 比较运算

在 Python 中有八种比较运算符。 它们的优先级相同（比布尔运算的优先级高）。 比较运算可以任意串连；例如，x < y <= z 等价于 x < y and y <= z，前者的不同之处在于 y 只被求值一次（但在两种情况下当 x < y 结果为假值时 z 都不会被求值）。

此表格汇总了比较运算:

运算

含义

<

严格小于

<=

小于或等于

>

严格大于

>=

大于或等于

==

等于

!=

不等于

is

对象标识

is not

否定的对象标识

除了不同数字类型以外，不同类型的对象比较时绝对不会相等。 而且，某些类型（例如函数对象）仅支持简化比较形式，即任何两个该种类型的对象必定不相等。 <, <=, > 和 >= 运算符在以下情况中将引发 TypeError 异常：当比较复数与另一个内置数字类型时，当两个对象具有无法被比较的不同类型时，或在未定义次序的其他情况时。

具有不同标识的类的实例比较结果通常为不相等，除非类定义了 __eq__() 方法。

一个类实例不能与相同类或的其他实例或其他类型的对象进行排序，除非该类定义了足够多的方法，包括 __lt__(), __le__(), __gt__() 以及 __ge__() (而如果你想实现常规意义上的比较操作，通常只要有 __lt__() 和 __eq__() 就可以了)。

is 和 is not 运算符无法自定义；并且它们可以被应用于任意两个对象而不会引发异常。

还有两种具有相同语法优先级的运算 in 和 not in，它们被 iterable 或实现了 __contains__() 方法的类型所支持。

4.4. 数字类型 — int, float, complex

存在三种不同的数字类型: 整数, 浮点数 和 复数。 此外，布尔值属于整数的子类型。 整数具有无限的精度。 浮点数通常使用 C 中的 double 来实现；有关你的程序运行所在机器上浮点数的精度和内部表示法可在 sys.float_info 中查看。 复数包含实部和虚部，分别以一个浮点数表示。 要从一个复数 z 中提取这两个部分，可使用 z.real 和 z.imag。 （标准库包含附加的数字类型，如表示有理数的 fractions 以及以用户定制精度表示浮点数的 decimal。）

数字是由数字字面值或内置函数与运算符的结果来创建的。 不带修饰的整数字面值（包括十六进制、八进制和二进制数）会生成整数。 包含小数点或幂运算符的数字字面值会生成浮点数。 在数字字面值末尾加上 'j' 或 'J' 会生成虚数（实部为零的复数），你可以将其与整数或浮点数相加来得到具有实部和虚部的复数。

Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the “narrower” type is widened to that of the other, where integer is narrower than floating point, which is narrower than complex. Comparisons between numbers of mixed type use the same rule. 2 The constructors int(), float(), and complex() can be used to produce numbers of a specific type.

All numeric types (except complex) support the following operations, sorted by ascending priority (all numeric operations have a higher priority than comparison operations):

运算

结果：

注释

完整文档

x + y

x 和 y 的和

x - y

x 和 y 的差

x * y

x 和 y 的乘积

x / y

x 和 y 的商

x // y

x 和 y 的商数

(1)

x % y

x / y 的余数

(2)

-x

x 取反

+x

x 不变

abs(x)

x 的绝对值或大小

abs()

int(x)

将 x 转换为整数

(3)(6)

int()

float(x)

将 x 转换为浮点数

(4)(6)

float()

complex(re, im)

一个带有实部 re 和虚部 im 的复数。im 默认为0。

(6)

complex()

c.conjugate()

复数 c 的共轭

divmod(x, y)

(x // y, x % y)

(2)

divmod()

pow(x, y)

x 的 y 次幂

(5)

pow()

x ** y

x 的 y 次幂

(5)

注释:

也称为整数除法。 结果值是一个整数，但结果的类型不一定是 int。 运算结果总是向负无穷的方向舍入: 1//2 为 0, (-1)//2 为 -1, 1//(-2) 为 -1 而 (-1)//(-2) 为 0。

不可用于复数。 而应在适当条件下使用 abs() 转换为浮点数。

从浮点数转换为整数会被舍入或是像在 C 语言中一样被截断；请参阅 math.floor() 和 math.ceil() 函数查看转换的完整定义。

float 也接受字符串 “nan” 和附带可选前缀 “+” 或 “-” 的 “inf” 分别表示非数字 (NaN) 以及正或负无穷。

Python 将 pow(0, 0) 和 0 ** 0 定义为 1，这是编程语言的普遍做法。

接受的数字字面值包括数码 0 到 9 或任何等效的 Unicode 字符（具有 Nd 特征属性的代码点）。

See http://www.unicode.org/Public/9.0.0/ucd/extracted/DerivedNumericType.txt for a complete list of code points with the Nd property.

所有 numbers.Real 类型 (int 和 float) 还包括下列运算:

运算

结果：

math.trunc(x)

x 截断为 Integral

round(x[, n])

x 舍入到 n 位小数，半数值会舍入到偶数。 如果省略 n，则默认为 0。

math.floor(x)

<= x 的最大 Integral

math.ceil(x)

>= x 的最小 Integral

有关更多的数字运算请参阅 math 和 cmath 模块。

4.4.1. 整数类型的按位运算

按位运算只对整数有意义。 计算按位运算的结果，就相当于使用无穷多个二进制符号位对二的补码执行操作。

二进制按位运算的优先级全都低于数字运算，但又高于比较运算；一元运算 ~ 具有与其他一元算术运算 (+ and -) 相同的优先级。

此表格是以优先级升序排序的按位运算列表:

运算

结果：

注释

x | y

x 和 y 按位 或

(4)

x ^ y

x 和 y 按位 异或

(4)

x & y

x 和 y 按位 与

(4)

x << n

x 左移 n 位

(1)(2)

x >> n

x 右移 n 位

(1)(3)

~x

x 逐位取反

注释:

负的移位数是非法的，会导致引发 ValueError。

A left shift by n bits is equivalent to multiplication by pow(2, n) without overflow check.

A right shift by n bits is equivalent to division by pow(2, n) without overflow check.

使用带有至少一个额外符号扩展位的有限个二进制补码表示（有效位宽度为 1 + max(x.bit_length(), y.bit_length()) 或以上）执行这些计算就足以获得相当于有无数个符号位时的同样结果。

4.4.2. 整数类型的附加方法

int 类型实现了 numbers.Integral abstract base class。 此外，它还提供了其他几个方法:

int.bit_length()

返回以二进制表示一个整数所需要的位数，不包括符号位和前面的零:

>>> n = -37
>>> bin(n)
'-0b100101'
>>> n.bit_length()
6

更准确地说，如果 x 非零，则 x.bit_length() 是使得 2**(k-1) <= abs(x) < 2**k 的唯一正整数 k。 同样地，当 abs(x) 小到足以具有正确的舍入对数时，则 k = 1 + int(log(abs(x), 2))。 如果 x 为零，则 x.bit_length() 返回 0。

等价于:

def bit_length(self):
    s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'
    s = s.lstrip('-0b') # remove leading zeros and minus sign
    return len(s)       # len('100101') --> 6

3.1 新版功能.

int.to_bytes(length, byteorder, *, signed=False)

返回表示一个整数的字节数组。

>>> (1024).to_bytes(2, byteorder='big')
b'\x04\x00'
>>> (1024).to_bytes(10, byteorder='big')
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> (-1024).to_bytes(10, byteorder='big', signed=True)
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes((x.bit_length() + 7) // 8, byteorder='little')
b'\xe8\x03'

整数会使用 length 个字节来表示。 如果整数不能用给定的字节数来表示则会引发 OverflowError。

byteorder 参数确定用于表示整数的字节顺序。 如果 byteorder 为 "big"，则最高位字节放在字节数组的开头。 如果 byteorder 为 "little"，则最高位字节放在字节数组的末尾。 要请求主机系统上的原生字节顺序，请使用 sys.byteorder 作为字节顺序值。

signed 参数确定是否使用二的补码来表示整数。 如果 signed 为 False 并且给出的是负整数，则会引发 OverflowError。 signed 的默认值为 False。

3.2 新版功能.

classmethod int.from_bytes(bytes, byteorder, *, signed=False)

返回由给定字节数组所表示的整数。

>>> int.from_bytes(b'\x00\x10', byteorder='big')
16
>>> int.from_bytes(b'\x00\x10', byteorder='little')
4096
>>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=True)
-1024
>>> int.from_bytes(b'\xfc\x00', byteorder='big', signed=False)
64512
>>> int.from_bytes([255, 0, 0], byteorder='big')
16711680

bytes 参数必须为一个 bytes-like object 或是生成字节的可迭代对象。

byteorder 参数确定用于表示整数的字节顺序。 如果 byteorder 为 "big"，则最高位字节放在字节数组的开头。 如果 byteorder 为 "little"，则最高位字节放在字节数组的末尾。 要请求主机系统上的原生字节顺序，请使用 sys.byteorder 作为字节顺序值。

signed 参数指明是否使用二的补码来表示整数。

3.2 新版功能.

4.4.3. 浮点类型的附加方法

float 类型实现了 numbers.Real abstract base class。 float 还具有以下附加方法。

float.as_integer_ratio()

返回一对整数，其比率正好等于原浮点数并且分母为正数。 无穷大会引发 OverflowError 而 NaN 则会引发 ValueError。

float.is_integer()

如果 float 实例可用有限位整数表示则返回 True，否则返回 False:

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False

两个方法均支持与十六进制数字符串之间的转换。 由于 Python 浮点数在内部存储为二进制数，因此浮点数与 十进制数 字符串之间的转换往往会导致微小的舍入错误。 而十六进制数字符串却允许精确地表示和描述浮点数。 这在进行调试和数值工作时非常有用。

float.hex()

以十六进制字符串的形式返回一个浮点数表示。 对于有限浮点数，这种表示法将总是包含前导的 0x 和尾随的 p 加指数。

classmethod float.fromhex(s)

返回以十六进制字符串 s 表示的浮点数的类方法。 字符串 s 可以带有前导和尾随的空格。

请注意 float.hex() 是实例方法，而 float.fromhex() 是类方法。

十六进制字符串采用的形式为:

[sign] ['0x'] integer ['.' fraction] ['p' exponent]

可选的 sign 可以是 + 或 -，integer 和 fraction 是十六进制数码组成的字符串，exponent 是带有可选前导符的十进制整数。 大小写没有影响，在 integer 或 fraction 中必须至少有一个十六进制数码。 此语法类似于 C99 标准的 6.4.4.2 小节中所描述的语法，也是 Java 1.5 以上所使用的语法。 特别地，float.hex() 的输出可以用作 C 或 Java 代码中的十六进制浮点数字面值，而由 C 的 %a 格式字符或 Java 的 Double.toHexString 所生成的十六进制数字符串由为 float.fromhex() 所接受。

请注意 exponent 是十进制数而非十六进制数，它给出要与系数相乘的 2 的幂次。 例如，十六进制数字符串 0x3.a7p10 表示浮点数 (3 + 10./16 + 7./16**2) * 2.0**10 即 3740.0:

>>> float.fromhex('0x3.a7p10')
3740.0

对 3740.0 应用反向转换会得到另一个代表相同数值的十六进制数字符串:

>>> float.hex(3740.0)
'0x1.d380000000000p+11'

4.4.4. 数字类型的哈希运算

对于可能为不同类型的数字 x 和 y，要求 x == y 时必定 hash(x) == hash(y) (详情参见 __hash__() 方法的文档)。 为了便于在各种数字类型 (包括 int, float, decimal.Decimal 和 fractions.Fraction) 上实现并保证效率，Python 对数字类型的哈希运算是基于为任意有理数定义统一的数学函数，因此该运算对 int 和 fractions.Fraction 的全部实例，以及 float 和 decimal.Decimal 的全部有限实例均可用。 从本质上说，此函数是通过以一个固定质数 P 进行 P 降模给出的。 P 的值在 Python 中可以 sys.hash_info 的 modulus 属性的形式被访问。

CPython implementation detail: 目前所用的质数设定，在 C long 为 32 位的机器上 P = 2**31 - 1 而在 C long 为 64 位的机器上 P = 2**61 - 1。

详细规则如下所述:

如果 x = m / n 是一个非负的有理数且 n 不可被 P 整除，则定义 hash(x) 为 m * invmod(n, P) % P，其中 invmod(n, P) 是对 n 模 P 取反。

如果 x = m / n 是一个非负的有理数且 n 可被 P 整除（但 m 不能）则 n 不能对 P 降模，以上规则不适用；在此情况下则定义 hash(x) 为常数值 sys.hash_info.inf。

如果 x = m / n 是一个负的有理数则定义 hash(x) 为 -hash(-x)。 如果结果哈希值为 -1 则将其替换为 -2。

特定值 sys.hash_info.inf, -sys.hash_info.inf 和 sys.hash_info.nan 被用作正无穷、负无穷和空值（所分别对应的）哈希值。 （所有可哈希的空值都具有相同的哈希值。）

对于一个 complex 值 z，会通过计算 hash(z.real) + sys.hash_info.imag * hash(z.imag) 将实部和虚部的哈希值结合起来，并进行降模 2**sys.hash_info.width 以使其处于 range(-2**(sys.hash_info.width - 1), 2**(sys.hash_info.width - 1)) 范围之内。 同样地，如果结果为 -1 则将其替换为 -2。

为了阐明上述规则，这里有一些等价于内置哈希算法的 Python 代码示例，可用于计算有理数、float 或 complex 的哈希值:

import sys, math

def hash_fraction(m, n):
    """Compute the hash of a rational number m / n.

    Assumes m and n are integers, with n positive.
    Equivalent to hash(fractions.Fraction(m, n)).

    """
    P = sys.hash_info.modulus
    # Remove common factors of P.  (Unnecessary if m and n already coprime.)
    while m % P == n % P == 0:
        m, n = m // P, n // P

    if n % P == 0:
        hash_value = sys.hash_info.inf
    else:
        # Fermat's Little Theorem: pow(n, P-1, P) is 1, so
        # pow(n, P-2, P) gives the inverse of n modulo P.
        hash_value = (abs(m) % P) * pow(n, P - 2, P) % P
    if m < 0:
        hash_value = -hash_value
    if hash_value == -1:
        hash_value = -2
    return hash_value

def hash_float(x):
    """Compute the hash of a float x."""

    if math.isnan(x):
        return sys.hash_info.nan
    elif math.isinf(x):
        return sys.hash_info.inf if x > 0 else -sys.hash_info.inf
    else:
        return hash_fraction(*x.as_integer_ratio())

def hash_complex(z):
    """Compute the hash of a complex number z."""

    hash_value = hash_float(z.real) + sys.hash_info.imag * hash_float(z.imag)
    # do a signed reduction modulo 2**sys.hash_info.width
    M = 2**(sys.hash_info.width - 1)
    hash_value = (hash_value & (M - 1)) - (hash_value & M)
    if hash_value == -1:
        hash_value = -2
    return hash_value

4.5. 迭代器类型

Python 支持在容器中进行迭代的概念。 这是通过使用两个单独方法来实现的；它们被用于允许用户自定义类对迭代的支持。 将在下文中详细描述的序列总是支持迭代方法。

容器对象要提供迭代支持，必须定义一个方法:

container.__iter__()

返回一个迭代器对象。 该对象需要支持下文所述的迭代器协议。 如果容器支持不同的迭代类型，则可以提供额外的方法来专门地请求不同迭代类型的迭代器。 （支持多种迭代形式的对象的例子有同时支持广度优先和深度优先遍历的树结构。） 此方法对应于 Python/C API 中 Python 对象类型结构体的 tp_iter 槽位。

迭代器对象自身需要支持以下两个方法，它们共同组成了 迭代器协议:

iterator.__iter__()

返回迭代器对象本身。 这是同时允许容器和迭代器配合 for 和 in 语句使用所必须的。 此方法对应于 Python/C API 中 Python 对象类型结构体的 tp_iter 槽位。

iterator.__next__()

从容器中返回下一项。 如果已经没有项可返回，则会引发 StopIteration 异常。 此方法对应于 Python/C API 中 Python 对象类型结构体的 tp_iternext 槽位。

Python 定义了几种迭代器对象以支持对一般和特定序列类型、字典和其他更特别的形式进行迭代。 除了迭代器协议的实现，特定类型的其他性质对迭代操作来说都不重要。

一旦迭代器的 __next__() 方法引发了 StopIteration，它必须一直对后续调用引发同样的异常。 不遵循此行为特性的实现将无法正常使用。

4.5.1. 生成器类型

Python 的 generator 提供了一种实现迭代器协议的便捷方式。 如果容器对象 __iter__() 方法被实现为一个生成器，它将自动返回一个迭代器对象（从技术上说是一个生成器对象），该对象提供 __iter__() 和 __next__() 方法。 有关生成器的更多信息可以参阅 yield 表达式的文档。

4.6. 序列类型 — list, tuple, range

有三种基本序列类型：list, tuple 和 range 对象。 为处理 二进制数据 和 文本字符串 而特别定制的附加序列类型会在专门的小节中描述。

4.6.1. 通用序列操作

大多数序列类型，包括可变类型和不可变类型都支持下表中的操作。 collections.abc.Sequence ABC 被提供用来更容易地在自定义序列类型上正确地实现这些操作。

此表按优先级升序列出了序列操作。 在表格中，s 和 t 是具有相同类型的序列，n, i, j 和 k 是整数而 x 是任何满足 s 所规定的类型和值限制的任意对象。

in 和 not in 操作具有与比较操作相同的优先级。 + (拼接) 和 * (重复) 操作具有与对应数值运算相同的优先级。 3

运算

结果：

注释

x in s

如果 s 中的某项等于 x 则结果为 True，否则为 False

(1)

x not in s

如果 s 中的某项等于 x 则结果为 False，否则为 True

(1)

s + t

s 与 t 相拼接

(6)(7)

s * n 或 n * s

相当于 s 与自身进行 n 次拼接

(2)(7)

s[i]

s 的第 i 项，起始为 0

(3)

s[i:j]

s 从 i 到 j 的切片

(3)(4)

s[i:j:k]

s 从 i 到 j 步长为 k 的切片

(3)(5)

len(s)

s 的长度

min(s)

s 的最小项

max(s)

s 的最大项

s.index(x[, i[, j]])

x 在 s 中首次出现项的索引号（索引号在 i 或其后且在 j 之前）

(8)

s.count(x)

x 在 s 中出现的总次数

相同类型的序列也支持比较。 特别地，tuple 和 list 的比较是通过比较对应元素的字典顺序。 这意味着想要比较结果相等，则每个元素比较结果都必须相等，并且两个序列长度必须相同。 （完整细节请参阅语言参考的 比较运算 部分。）

注释:

虽然 in 和 not in 操作在通常情况下仅被用于简单的成员检测，某些专门化序列 (例如 str, bytes 和 bytearray) 也使用它们进行子序列检测:

>>> "gg" in "eggs"
True

小于 0 的 n 值会被当作 0 来处理 (生成一个与 s 同类型的空序列)。 请注意序列 s 中的项并不会被拷贝；它们会被多次引用。 这一点经常会令 Python 编程新手感到困扰；例如:

>>> lists = [[]] * 3
>>> lists
[[], [], []]
>>> lists[0].append(3)
>>> lists
[[3], [3], [3]]

具体的原因在于 [[]] 是一个包含了一个空列表的单元素列表，所以 [[]] * 3 结果中的三个元素都是对这一个空列表的引用。 修改 lists 中的任何一个元素实际上都是对这一个空列表的修改。 你可以用以下方式创建以不同列表为元素的列表:

>>> lists = [[] for i in range(3)]
>>> lists[0].append(3)
>>> lists[1].append(5)
>>> lists[2].append(7)
>>> lists
[[3], [5], [7]]

进一步的解释可以在 FAQ 条目 如何创建多维列表？ 中查看。

如果 i 或 j 为负值，则索引顺序是相对于序列 s 的末尾: 索引号会被替换为 len(s) + i 或 len(s) + j。 但要注意 -0 仍然为 0。

s 从 i 到 j 的切片被定义为所有满足 i <= k < j 的索引号 k 的项组成的序列。 如果 i 或 j 大于 len(s)，则使用 len(s)。 如果 i 被省略或为 None，则使用 0。 如果 j 被省略或为 None，则使用 len(s)。 如果 i 大于等于 j，则切片为空。

s 从 i 到 j 步长为 k 的切片被定义为所有满足 0 <= n < (j-i)/k 的索引号 x = i + n*k 的项组成的序列。 换句话说，索引号为 i, i+k, i+2*k, i+3*k，以此类推，当达到 j 时停止 (但一定不包括 j)。 当 k 为正值时，i 和 j 会被减至不大于 len(s)。 当 k 为负值时，i 和 j 会被减至不大于 len(s) - 1。 如果 i 或 j 被省略或为 None，它们会成为“终止”值 (是哪一端的终止值则取决于 k 的符号)。 请注意，k 不可为零。 如果 k 为 None，则当作 1 处理。

拼接不可变序列总是会生成新的对象。 这意味着通过重复拼接来构建序列的运行时开销将会基于序列总长度的乘方。 想要获得线性的运行时开销，你必须改用下列替代方案之一：

如果拼接 str 对象，你可以构建一个列表并在最后使用 str.join() 或是写入一个 io.StringIO 实例并在结束时获取它的值

如果拼接 bytes 对象，你可以类似地使用 bytes.join() 或 io.BytesIO，或者你也可以使用 bytearray 对象进行原地拼接。 bytearray 对象是可变的，并且具有高效的重分配机制

如果拼接 tuple 对象，请改为扩展 list 类

对于其它类型，请查看相应的文档

某些序列类型 (例如 range) 仅支持遵循特定模式的项序列，因此并不支持序列拼接或重复。

当 x 在 s 中找不到时 index 会引发 ValueError。 不是所有实现都支持传入额外参数 i 和 j。 这两个参数允许高效地搜索序列的子序列。 传入这两个额外参数大致相当于使用 s[i:j].index(x)，但是不会复制任何数据，并且返回的索引是相对于序列的开头而非切片的开头。

4.6.2. 不可变序列类型

不可变序列类型普遍实现而可变序列类型未实现的唯一操作就是对 hash() 内置函数的支持。

这种支持允许不可变类型，例如 tuple 实例被用作 dict 键，以及存储在 set 和 frozenset 实例中。

尝试对包含有不可哈希值的不可变序列进行哈希运算将会导致 TypeError。

4.6.3. 可变序列类型

以下表格中的操作是在可变序列类型上定义的。 collections.abc.MutableSequence ABC 被提供用来更容易地在自定义序列类型上正确实现这些操作。

表格中的 s 是可变序列类型的实例，t 是任意可迭代对象，而 x 是符合对 s 所规定类型与值限制的任何对象 (例如，bytearray 仅接受满足 0 <= x <= 255 值限制的整数)。

运算

结果：

注释

s[i] = x

将 s 的第 i 项替换为 x

s[i:j] = t

将 s 从 i 到 j 的切片替换为可迭代对象 t 的内容

del s[i:j]

等同于 s[i:j] = []

s[i:j:k] = t

将 s[i:j:k] 的元素替换为 t 的元素

(1)

del s[i:j:k]

从列表中移除 s[i:j:k] 的元素

s.append(x)

将 x 添加到序列的末尾 (等同于 s[len(s):len(s)] = [x])

s.clear()

从 s 中移除所有项 (等同于 del s[:])

(5)

s.copy()

创建 s 的浅拷贝 (等同于 s[:])

(5)

s.extend(t) 或 s += t

用 t 的内容扩展 s (基本上等同于 s[len(s):len(s)] = t)

s *= n

使用 s 的内容重复 n 次来对其进行更新

(6)

s.insert(i, x)

在由 i 给出的索引位置将 x 插入 s (等同于 s[i:i] = [x])

s.pop([i])

提取在 i 位置上的项，并将其从 s 中移除

(2)

s.remove(x)

remove the first item from s where s[i] == x

(3)

s.reverse()

就地将列表中的元素逆序。

(4)

注释:

t 必须与它所替换的切片具有相同的长度。

可选参数 i 默认为 -1，因此在默认情况下会移除并返回最后一项。

当在 s 中找不到 x 时 remove 操作会引发 ValueError。

当反转大尺寸序列时 reverse() 方法会原地修改该序列以保证空间经济性。 为提醒用户此操作是通过间接影响进行的，它并不会返回反转后的序列。

包括 clear() 和 copy() 是为了与不支持切片操作的可变容器 (例如 dict 和 set) 的接口保持一致

3.3 新版功能: clear() 和 copy() 方法。

n 值为一个整数，或是一个实现了 __index__() 的对象。 n 值为零或负数将清空序列。 序列中的项不会被拷贝；它们会被多次引用，正如 通用序列操作 中有关 s * n 的说明。

4.6.4. 列表

列表是可变序列，通常用于存放同类项目的集合（其中精确的相似程度将根据应用而变化）。

class list([iterable])

可以用多种方式构建列表：

使用一对方括号来表示空列表: []

使用方括号，其中的项以逗号分隔: [a], [a, b, c]

使用列表推导式: [x for x in iterable]

使用类型的构造器: list() 或 list(iterable)

构造器将构造一个列表，其中的项与 iterable 中的项具有相同的的值与顺序。 iterable 可以是序列、支持迭代的容器或其它可迭代对象。 如果 iterable 已经是一个列表，将创建并返回其副本，类似于 iterable[:]。 例如，list('abc') 返回 ['a', 'b', 'c'] 而 list( (1, 2, 3) ) 返回 [1, 2, 3]。 如果没有给出参数，构造器将创建一个空列表 []。

其它许多操作也会产生列表，包括 sorted() 内置函数。

列表实现了所有 一般 和 可变 序列的操作。 列表还额外提供了以下方法：

sort(*, key=None, reverse=False)

此方法会对列表进行原地排序，只使用 < 来进行各项间比较。 异常不会被屏蔽 —— 如果有任何比较操作失败，整个排序操作将失败（而列表可能会处于被部分修改的状态）。

sort() 接受两个仅限以关键字形式传入的参数 (仅限关键字参数):

key 指定带有一个参数的函数，用于从每个列表元素中提取比较键 (例如 key=str.lower)。 对应于列表中每一项的键会被计算一次，然后在整个排序过程中使用。 默认值 None 表示直接对列表项排序而不计算一个单独的键值。

可以使用 functools.cmp_to_key() 将 2.x 风格的 cmp 函数转换为 key 函数。

reverse 为一个布尔值。 如果设为 True，则每个列表元素将按反向顺序比较进行排序。

当顺序大尺寸序列时此方法会原地修改该序列以保证空间经济性。 为提醒用户此操作是通过间接影响进行的，它并不会返回排序后的序列（请使用 sorted() 显示地请求一个新的已排序列表实例）。

sort() 方法确保是稳定的。 如果一个排序确保不会改变比较结果相等的元素的相对顺序就称其为稳定的 — 这有利于进行多重排序（例如先按部门、再接薪级排序）。

CPython implementation detail: 在一个列表被排序期间，尝试改变甚至进行检测也会造成未定义的影响。 Python 的 C 实现会在排序期间将列表显示为空，如果发现列表在排序期间被改变将会引发 ValueError。

4.6.5. 元组

元组是不可变序列，通常用于储存异构数据的多项集（例如由 enumerate() 内置函数所产生的二元组）。 元组也被用于需要同构数据的不可变序列的情况（例如允许存储到 set 或 dict 的实例）。

class tuple([iterable])

可以用多种方式构建元组：

使用一对圆括号来表示空元组: ()

使用一个后缀的逗号来表示单元组: a, 或 (a,)

使用以逗号分隔的多个项: a, b, c or (a, b, c)

使用内置的 tuple(): tuple() 或 tuple(iterable)

构造器将构造一个元组，其中的项与 iterable 中的项具有相同的值与顺序。 iterable 可以是序列、支持迭代的容器或其他可迭代对象。 如果 iterable 已经是一个元组，会不加改变地将其返回。 例如，tuple('abc') 返回 ('a', 'b', 'c') 而 tuple( [1, 2, 3] ) 返回 (1, 2, 3)。 如果没有给出参数，构造器将创建一个空元组 ()。

请注意决定生成元组的其实是逗号而不是圆括号。 圆括号只是可选的，生成空元组或需要避免语法歧义的情况除外。 例如，f(a, b, c) 是在调用函数时附带三个参数，而 f((a, b, c)) 则是在调用函数时附带一个三元组。

元组实现了所有 一般 序列的操作。

对于通过名称访问相比通过索引访问更清晰的异构数据多项集，collections.namedtuple() 可能是比简单元组对象更为合适的选择。

4.6.6. range 对象

range 类型表示不可变的数字序列，通常用于在 for 循环中循环指定的次数。

class range(stop)

class range(start, stop[, step])

range 构造器的参数必须为整数（可以是内置的 int 或任何实现了 __index__ 特殊方法的对象）。 如果省略 step 参数，其默认值为 1。 如果省略 start 参数，其默认值为 0，如果 step 为零则会引发 ValueError。

如果 step 为正值，确定 range r 内容的公式为 r[i] = start + step*i 其中 i >= 0 且 r[i] < stop。

如果 step 为负值，确定 range 内容的公式仍然为 r[i] = start + step*i，但限制条件改为 i >= 0 且 r[i] > stop.

如果 r[0] 不符合值的限制条件，则该 range 对象为空。 range 对象确实支持负索引，但是会将其解读为从正索引所确定的序列的末尾开始索引。

元素绝对值大于 sys.maxsize 的 range 对象是被允许的，但某些特性 (例如 len()) 可能引发 OverflowError。

一些 range 对象的例子:

>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(0))
[]
>>> list(range(1, 0))
[]

range 对象实现了 一般 序列的所有操作，但拼接和重复除外（这是由于 range 对象只能表示符合严格模式的序列，而重复和拼接通常都会违反这样的模式）。

start

start 形参的值 (如果该形参未提供则为 0)

stop

stop 形参的值

step

step 形参的值 (如果该形参未提供则为 1)

range 类型相比常规 list 或 tuple 的优势在于一个 range 对象总是占用固定数量的（较小）内存，不论其所表示的范围有多大（因为它只保存了 start, stop 和 step 值，并会根据需要计算具体单项或子范围的值）。

range 对象实现了 collections.abc.Sequence ABC，提供如包含检测、元素索引查找、切片等特性，并支持负索引 (参见 序列类型 — list, tuple, range):

>>> r = range(0, 20, 2)
>>> r
range(0, 20, 2)
>>> 11 in r
False
>>> 10 in r
True
>>> r.index(10)
5
>>> r[5]
10
>>> r[:5]
range(0, 10, 2)
>>> r[-1]
18

使用 == 和 != 检测 range 对象是否相等是将其作为序列来比较。 也就是说，如果两个 range 对象表示相同的值序列就认为它们是相等的。 （请注意比较结果相等的两个 range 对象可能会具有不同的 start, stop 和 step 属性，例如 range(0) == range(2, 1, 3) 而 range(0, 3, 2) == range(0, 4, 2)。）

在 3.2 版更改: 实现 Sequence ABC。 支持切片和负数索引。 使用 int 对象在固定时间内进行成员检测，而不是逐一迭代所有项。

在 3.3 版更改: 定义 ‘==’ 和 ‘!=’ 以根据 range 对象所定义的值序列来进行比较（而不是根据对象的标识）。

3.3 新版功能: start, stop 和 step 属性。

参见

linspace recipe [http://code.activestate.com/recipes/579000/] 演示了如何实现一个延迟求值版本的适合浮点数应用的 range 对象。

4.7. 文本序列类型 — str

在 Python 中处理文本数据是使用 str 对象，也称为 字符串。 字符串是由 Unicode 码位构成的不可变 序列。 字符串字面值有多种不同的写法：

单引号: '允许包含有 "双" 引号'

双引号: "允许包含有 '单' 引号"。

三重引号: '''三重单引号''', """三重双引号"""

使用三重引号的字符串可以跨越多行 —— 其中所有的空白字符都将包含在该字符串字面值中。

作为单一表达式组成部分，之间只由空格分隔的多个字符串字面值会被隐式地转换为单个字符串字面值。 也就是说，("spam " "eggs") == "spam eggs"。

请参阅 字符串和字节串字面值 有解有关不同字符串字面值的更多信息，包括所支持的转义序列，以及使用 r (“raw”) 前缀来禁用大多数转义序列的处理。

字符串也可以通过使用 str 构造器从其他对象创建。

由于不存在单独的“字符”类型，对字符串做索引操作将产生一个长度为 1 的字符串。 也就是说，对于一个非空字符串 s, s[0] == s[0:1]。

不存在可变的字符串类型，但是 str.join() 或 io.StringIO 可以被被用来根据多个片段高效率地构建字符串。

在 3.3 版更改: 为了与 Python 2 系列的向下兼容，再次允许字符串字面值使用 u 前缀。 它对字符串字面值的含义没有影响，并且不能与 r 前缀同时出现。

class str(object='')

class str(object=b'', encoding='utf-8', errors='strict')

返回 object 的 字符串 版本。 如果未提供 object 则返回空字符串。 在其他情况下 str() 的行为取决于 encoding 或 errors 是否有给出，具体见下。

如果 encoding 或 errors 均未给出，str(object) 返回 object.__str__()，这是 object 的“非正式”或格式良好的字符串表示。 对于字符串对象，这是该字符串本身。 如果 object 没有 __str__() 方法，则 str() 将回退为返回 repr(object)。

如果 encoding 或 errors 至少给出其中之一，则 object 应该是一个 bytes-like object (例如 bytes 或 bytearray)。 在此情况下，如果 object 是一个 bytes (或 bytearray) 对象，则 str(bytes, encoding, errors) 等价于 bytes.decode(encoding, errors)。 否则的话，会在调用 bytes.decode() 之前获取缓冲区对象下层的 bytes 对象。 请参阅 二进制序列类型 — bytes, bytearray, memoryview 与 缓冲协议 了解有关缓冲区对象的信息。

将一个 bytes 对象传入 str() 而不给出 encoding 或 errors 参数的操作属于第一种情况， 将返回非正式的字符串表示（另请参阅 Python 的 -b 命令行选项）。 例如:

>>> str(b'Zoot!')
"b'Zoot!'"

有关 str 类及其方法的更多信息，请参阅下面的 文本序列类型 — str 和 字符串的方法 小节。 要输出格式化字符串，请参阅 格式化字符串字面值 和 格式字符串语法 小节。 此外还可以参阅 文本处理服务 小节。

4.7.1. 字符串的方法

字符串实现了所有 一般 序列的操作，还额外提供了以下列出的一些附加方法。

字符串还支持两种字符串格式化样式，一种提供了很大程度的灵活性和可定制性 (参阅 str.format(), 格式字符串语法 和 自定义字符串格式化) 而另一种是基于 C printf 样式的格式化，它可处理的类型范围较窄，并且更难以正确使用，但对于它可处理的情况往往会更为快速 (printf 风格的字符串格式化)。

标准库的 文本处理服务 部分涵盖了许多其他模块，提供各种文本相关工具（例如包含于 re 模块中的正则表达式支持）。

str.capitalize()

返回原字符串的副本，其首个字符大写，其余为小写。

str.casefold()

返回原字符串消除大小写的副本。 消除大小写的字符串可用于忽略大小写的匹配。

消除大小写类似于转为小写，但是更加彻底一些，因为它会移除字符串中的所有大小写变化形式。 例如，德语小写字母 'ß' 相当于 "ss"。 由于它已经是小写了，lower() 不会对 'ß' 做任何改变；而 casefold() 则会将其转换为 "ss"。

消除大小写算法的描述请参见 Unicode 标准的 3.13 节。

3.3 新版功能.

str.center(width[, fillchar])

返回长度为 width 的字符串，原字符串在其正中。 使用指定的 fillchar 填充两边的空位（默认使用 ASCII 空格符）。 如果 width 小于等于 len(s) 则返回原字符串的副本。

str.count(sub[, start[, end]])

返回子字符串 sub 在 [start, end] 范围内非重叠出现的次数。 可选参数 start 与 end 会被解读为切片表示法。

str.encode(encoding="utf-8", errors="strict")

返回原字符串编码为字节串对象的版本。 默认编码为 'utf-8'。 可以给出 errors 来设置不同的错误处理方案。 errors 的默认值为 'strict'，表示编码错误会引发 UnicodeError。 其他可用的值为 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及任何其他通过 codecs.register_error() 注册的值，请参阅 错误处理方案 小节。 要查看可用的编码列表，请参阅 标准编码 小节。

在 3.1 版更改: 加入了对关键字参数的支持。

str.endswith(suffix[, start[, end]])

如果字符串以指定的 suffix 结束返回 True，否则返回 False。 suffix 也可以为由多个供查找的后缀构成的元组。 如果有可选项 start，将从所指定位置开始检查。 如果有可选项 end，将在所指定位置停止比较。

str.expandtabs(tabsize=8)

返回字符串的副本，其中所有的制表符会由一个或多个空格替换，具体取决于当前列位置和给定的制表符宽度。 每 tabsize 个字符设为一个制表位（默认值 8 时设定的制表位在列 0, 8, 16 依次类推）。 要展开字符串，当前列将被设为零并逐一检查字符串中的每个字符。 如果字符为制表符 (\t)，则会在结果中插入一个或多个空格符，直到当前列等于下一个制表位。 （制表符本身不会被复制。） 如果字符为换行符 (\n) 或回车符 (\r)，它会被复制并将当前列重设为零。 任何其他字符会被不加修改地复制并将当前列加一，不论该字符在被打印时会如何显示。

>>> '01\t012\t0123\t01234'.expandtabs()
'01      012     0123    01234'
>>> '01\t012\t0123\t01234'.expandtabs(4)
'01  012 0123    01234'

str.find(sub[, start[, end]])

返回子字符串 sub 在 s[start:end] 切片内被找到的最小索引。 可选参数 start 与 end 会被解读为切片表示法。 如果 sub 未被找到则返回 -1。

注解

find() 方法应该只在你需要知道 sub 所在位置时使用。 要检查 sub 是否为子字符串，请使用 in 操作符:

>>> 'Py' in 'Python'
True

str.format(*args, **kwargs)

执行字符串格式化操作。 调用此方法的字符串可以包含字符串字面值或者以花括号 {} 括起来的替换域。 每个替换域可以包含一个位置参数的数字索引，或者一个关键字参数的名称。 返回的字符串副本中每个替换域都会被替换为对应参数的字符串值。

>>> "The sum of 1 + 2 is {0}".format(1+2)
'The sum of 1 + 2 is 3'

请参阅 格式字符串语法 了解有关可以在格式字符串中指定的各种格式选项的说明。

注解

当使用 n 类型 (例如: '{:n}'.format(1234)) 来格式化数字 (int, float, complex, decimal.Decimal 及其子类) 的时候，该函数会临时性地将 LC_CTYPE 区域设置为 LC_NUMERIC 区域以解码 localeconv() 的 decimal_point 和 thousands_sep 字段，如果它们是非 ASCII 字符或长度超过 1 字节的话，并且 LC_NUMERIC 区域会与 LC_CTYPE 区域不一致。 这个临时更改会影响其他线程。

在 3.6.5 版更改: 当使用 n 类型格式化数字时，该函数在某些情况下会临时性地将 LC_CTYPE 区域设置为 LC_NUMERIC 区域。

str.format_map(mapping)

类似于 str.format(**mapping)，不同之处在于 mapping 会被直接使用而不是复制到一个 dict。 适宜使用此方法的一个例子是当 mapping 为 dict 的子类的情况：

>>> class Default(dict):
...     def __missing__(self, key):
...         return key
...
>>> '{name} was born in {country}'.format_map(Default(name='Guido'))
'Guido was born in country'

3.2 新版功能.

str.index(sub[, start[, end]])

类似于 find()，但在找不到子类时会引发 ValueError。

str.isalnum()

Return true if all characters in the string are alphanumeric and there is at least one character, false otherwise. A character c is alphanumeric if one of the following returns True: c.isalpha(), c.isdecimal(), c.isdigit(), or c.isnumeric().

str.isalpha()

Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”. Note that this is different from the “Alphabetic” property defined in the Unicode Standard.

str.isdecimal()

Return true if all characters in the string are decimal characters and there is at least one character, false otherwise. Decimal characters are those that can be used to form numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in the Unicode General Category “Nd”.

str.isdigit()

Return true if all characters in the string are digits and there is at least one character, false otherwise. Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.

str.isidentifier()

Return true if the string is a valid identifier according to the language definition, section 标识符和关键字.

请使用 keyword.iskeyword() 来检测保留标识符，例如 def 和 class。

str.islower()

Return true if all cased characters 4 in the string are lowercase and there is at least one cased character, false otherwise.

str.isnumeric()

Return true if all characters in the string are numeric characters, and there is at least one character, false otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.

str.isprintable()

Return true if all characters in the string are printable or the string is empty, false otherwise. Nonprintable characters are those characters defined in the Unicode character database as “Other” or “Separator”, excepting the ASCII space (0x20) which is considered printable. (Note that printable characters in this context are those which should not be escaped when repr() is invoked on a string. It has no bearing on the handling of strings written to sys.stdout or sys.stderr.)

str.isspace()

Return true if there are only whitespace characters in the string and there is at least one character, false otherwise. Whitespace characters are those characters defined in the Unicode character database as “Other” or “Separator” and those with bidirectional property being one of “WS”, “B”, or “S”.

str.istitle()

Return true if the string is a titlecased string and there is at least one character, for example uppercase characters may only follow uncased characters and lowercase characters only cased ones. Return false otherwise.

str.isupper()

Return true if all cased characters 4 in the string are uppercase and there is at least one cased character, false otherwise.

str.join(iterable)

返回一个由 iterable 中的字符串拼接而成的字符串。 如果 iterable 中存在任何非字符串值包括 bytes 对象则会引发 TypeError。 调用该方法的字符串将作为元素之间的分隔。

str.ljust(width[, fillchar])

返回长度为 width 的字符串，原字符串在其中靠左对齐。 使用指定的 fillchar 填充空位 (默认使用 ASCII 空格符)。 如果 width 小于等于 len(s) 则返回原字符串的副本。

str.lower()

返回原字符串的副本，其所有区分大小写的字符 4 均转换为小写。

所用转换小写算法的描述请参见 Unicode 标准的 3.13 节。

str.lstrip([chars])

返回原字符串的副本，移除其中的前导字符。 chars 参数为指定要移除字符的字符串。 如果省略或为 None，则 chars 参数默认移除空白符。 实际上 chars 参数并非指定单个前缀；而是会移除参数值的所有组合:

>>> '   spacious   '.lstrip()
'spacious   '
>>> 'www.example.com'.lstrip('cmowz.')
'example.com'

static str.maketrans(x[, y[, z]])

此静态方法返回一个可供 str.translate() 使用的转换对照表。

如果只有一个参数，则它必须是一个将 Unicode 码位序号（整数）或字符（长度为 1 的字符串）映射到 Unicode 码位序号、（任意长度的）字符串或 None 的字典。 字符键将会被转换为码位序号。

如果有两个参数，则它们必须是两个长度相等的字符串，并且在结果字典中，x 中每个字符将被映射到 y 中相同位置的字符。 如果有第三个参数，它必须是一个字符串，其中的字符将在结果中被映射到 None。

str.partition(sep)

在 sep 首次出现的位置拆分字符串，返回一个 3 元组，其中包含分隔符之前的部分、分隔符本身，以及分隔符之后的部分。 如果分隔符未找到，则返回的 3 元组中包含字符本身以及两个空字符串。

str.replace(old, new[, count])

返回字符串的副本，其中出现的所有子字符串 old 都将被替换为 new。 如果给出了可选参数 count，则只替换前 count 次出现。

str.rfind(sub[, start[, end]])

返回子字符串 sub 在字符串内被找到的最大（最右）索引，这样 sub 将包含在 s[start:end] 当中。 可选参数 start 与 end 会被解读为切片表示法。 如果未找到则返回 -1。

str.rindex(sub[, start[, end]])

类似于 rfind()，但在子字符串 sub 未找到时会引发 ValueError。

str.rjust(width[, fillchar])

返回长度为 width 的字符串，原字符串在其中靠右对齐。 使用指定的 fillchar 填充空位 (默认使用 ASCII 空格符)。 如果 width 小于等于 len(s) 则返回原字符串的副本。

str.rpartition(sep)

在 sep 最后一次出现的位置拆分字符串，返回一个 3 元组，其中包含分隔符之前的部分、分隔符本身，以及分隔符之后的部分。 如果分隔符未找到，则返回的 3 元组中包含两个空字符串以及字符串本身。

str.rsplit(sep=None, maxsplit=-1)

返回一个由字符串内单词组成的列表，使用 sep 作为分隔字符串。 如果给出了 maxsplit，则最多进行 maxsplit 次拆分，从 最右边 开始。 如果 sep 未指定或为 None，任何空白字符串都会被作为分隔符。 除了从右边开始拆分，rsplit() 的其他行为都类似于下文所述的 split()。

str.rstrip([chars])

返回原字符串的副本，移除其中的末尾字符。 chars 参数为指定要移除字符的字符串。 如果省略或为 None，则 chars 参数默认移除空白符。 实际上 chars 参数并非指定单个后缀；而是会移除参数值的所有组合:

>>> '   spacious   '.rstrip()
'   spacious'
>>> 'mississippi'.rstrip('ipz')
'mississ'

str.split(sep=None, maxsplit=-1)

返回一个由字符串内单词组成的列表，使用 sep 作为分隔字符串。 如果给出了 maxsplit，则最多进行 maxsplit 次拆分（因此，列表最多会有 maxsplit+1 个元素）。 如果 maxsplit 未指定或为 -1，则不限制拆分次数（进行所有可能的拆分）。

如果给出了 sep，则连续的分隔符不会被组合在一起而是被视为分隔空字符串 (例如 '1,,2'.split(',') 将返回 ['1', '', '2'])。 sep 参数可能由多个字符组成 (例如 '1<>2<>3'.split('<>') 将返回 ['1', '2', '3'])。 使用指定的分隔符拆分空字符串将返回 ['']。

例如

>>> '1,2,3'.split(',')
['1', '2', '3']
>>> '1,2,3'.split(',', maxsplit=1)
['1', '2,3']
>>> '1,2,,3,'.split(',')
['1', '2', '', '3', '']

如果 sep 未指定或为 None，则会应用另一种拆分算法：连续的空格会被视为单个分隔符，其结果将不包含开头或末尾的空字符串，如果字符串包含前缀或后缀空格的话。 因此，使用 None 拆分空字符串或仅包含空格的字符串将返回 []。

例如

>>> '1 2 3'.split()
['1', '2', '3']
>>> '1 2 3'.split(maxsplit=1)
['1', '2 3']
>>> '   1   2   3   '.split()
['1', '2', '3']

str.splitlines([keepends])

返回由原字符串中各行组成的列表，在行边界的位置拆分。 结果列表中不包含行边界，除非给出了 keepends 且为真值。

此方法会以下列行边界进行拆分。 特别地，行边界是 universal newlines 的一个超集。

表示符

描述

\n

换行

\r

回车

\r\n

回车 + 换行

\v 或 \x0b

行制表符

\f 或 \x0c

换表单

\x1c

文件分隔符

\x1d

组分隔符

\x1e

记录分隔符

\x85

下一行 (C1 控制码)

\u2028

行分隔符

\u2029

段分隔符

在 3.2 版更改: \v 和 \f 被添加到行边界列表

例如

>>> 'ab c\n\nde fg\rkl\r\n'.splitlines()
['ab c', '', 'de fg', 'kl']
>>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
['ab c\n', '\n', 'de fg\r', 'kl\r\n']

不同于 split()，当给出了分隔字符串 sep 时，对于空字符串此方法将返回一个空列表，而末尾的换行不会令结果中增加额外的行:

>>> "".splitlines()
[]
>>> "One line\n".splitlines()
['One line']

作为比较，split('\n') 的结果为:

>>> ''.split('\n')
['']
>>> 'Two lines\n'.split('\n')
['Two lines', '']

str.startswith(prefix[, start[, end]])

如果字符串以指定的 prefix 开始则返回 True，否则返回 False。 prefix 也可以为由多个供查找的前缀构成的元组。 如果有可选项 start，将从所指定位置开始检查。 如果有可选项 end，将在所指定位置停止比较。

str.strip([chars])

返回原字符串的副本，移除其中的前导和末尾字符。 chars 参数为指定要移除字符的字符串。 如果省略或为 None，则 chars 参数默认移除空白符。 实际上 chars 参数并非指定单个前缀或后缀；而是会移除参数值的所有组合:

>>> '   spacious   '.strip()
'spacious'
>>> 'www.example.com'.strip('cmowz.')
'example'

最外侧的前导和末尾 chars 参数值将从字符串中移除。 开头端的字符的移除将在遇到一个未包含于 chars 所指定字符集的字符时停止。 类似的操作也将在结尾端发生。 例如:

>>> comment_string = '#....... Section 3.2.1 Issue #32 .......'
>>> comment_string.strip('.#! ')
'Section 3.2.1 Issue #32'

str.swapcase()

返回原字符串的副本，其中大写字符转换为小写，反之亦然。 请注意 s.swapcase().swapcase() == s 并不一定为真值。

str.title()

返回原字符串的标题版本，其中每个单词第一个字母为大写，其余字母为小写。

例如

>>> 'Hello world'.title()
'Hello World'

该算法使用一种简单的与语言无关的定义，将连续的字母组合视为单词。 该定义在多数情况下都很有效，但它也意味着代表缩写形式与所有格的撇号也会成为单词边界，这可能导致不希望的结果:

>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"

可以使用正则表达式来构建针对撇号的特别处理:

>>> import re
>>> def titlecase(s):
...     return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
...                   lambda mo: mo.group(0)[0].upper() +
...                              mo.group(0)[1:].lower(),
...                   s)
...
>>> titlecase("they're bill's friends.")
"They're Bill's Friends."

str.translate(table)

返回原字符串的副本，其中每个字符按给定的转换表进行映射。 转换表必须是一个使用 __getitem__() 来实现索引操作的对象，通常为 mapping 或 sequence。 当以 Unicode 码位序号（整数）为索引时，转换表对象可以做以下任何一种操作：返回 Unicode 序号或字符串，将字符映射为一个或多个字符；返回 None，将字符从结果字符串中删除；或引发 LookupError 异常，将字符映射为其自身。

你可以使用 str.maketrans() 基于不同格式的字符到字符映射来创建一个转换映射表。

另请参阅 codecs 模块以了解定制字符映射的更灵活方式。

str.upper()

返回原字符串的副本，其中所有区分大小写的字符 4 均转换为大写。 请注意如果 s 包含不区分大小写的字符或者如果结果字符的 Unicode 类别不是 “Lu” (Letter, uppercase) 而是 “Lt” (Letter, titlecase) 则 s.upper().isupper() 有可能为 False。

所用转换大写算法的描述请参见 Unicode 标准的 3.13 节。

str.zfill(width)

返回原字符串的副本，在左边填充 ASCII '0' 数码使其长度变为 width。 正负值前缀 ('+'/'-') 的处理方式是在正负符号 之后 填充而非在之前。 如果 width 小于等于 len(s) 则返回原字符串的副本。

例如

>>> "42".zfill(5)
'00042'
>>> "-42".zfill(5)
'-0042'

4.7.2. printf 风格的字符串格式化

注解

The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals or the str.format() interface helps avoid these errors. These alternatives also provide more powerful, flexible and extensible approaches to formatting text.

字符串具有一种特殊的内置操作：使用 % (取模) 运算符。 这也被称为字符串的 格式化 或 插值 运算符。 对于 format % values (其中 format 为一个字符串)，在 format 中的 % 转换标记符将被替换为零个或多个 values 条目。 其效果类似于在 C 语言中使用 sprintf()。

如果 format 要求一个单独参数，则 values 可以为一个非元组对象。 5 否则的话，values 必须或者是一个包含项数与格式字符串中指定的转换符项数相同的元组，或者是一个单独映射对象（例如字典）。

转换标记符包含两个或更多字符并具有以下组成，且必须遵循此处规定的顺序：

'%' 字符，用于标记转换符的起始。

映射键（可选），由加圆括号的字符序列组成 (例如 (somename))。

转换旗标（可选），用于影响某些转换类型的结果。

最小字段宽度（可选）。 如果指定为 '*' (星号)，则实际宽度会从 values 元组的下一元素中读取，要转换的对象则为最小字段宽度和可选的精度之后的元素。

精度（可选），以在 '.' (点号) 之后加精度值的形式给出。 如果指定为 '*' (星号)，则实际精度会从 values 元组的下一元素中读取，要转换的对象则为精度之后的元素。

长度修饰符（可选）。

转换类型。

当右边的参数为一个字典（或其他映射类型）时，字符串中的格式 必须 包含加圆括号的映射键，对应 '%' 字符之后字典中的每一项。 映射键将从映射中选取要格式化的值。 例如：

>>> print('%(language)s has %(number)03d quote types.' %
...       {'language': "Python", "number": 2})
Python has 002 quote types.

在此情况下格式中不能出现 * 标记符（因其需要一个序列类的参数列表）。

转换旗标为：

标志

含义

'#'

值的转换将使用“替代形式”（具体定义见下文）。

'0'

转换将为数字值填充零字符。

'-'

转换值将靠左对齐（如果同时给出 '0' 转换，则会覆盖后者）。

' '

(空格) 符号位转换产生的正数（或空字符串）前将留出一个空格。

'+'

符号字符 ('+' 或 '-') 将显示于转换结果的开头（会覆盖 “空格” 旗标）。

可以给出长度修饰符 (h, l 或 L)，但会被忽略，因为对 Python 来说没有必要 – 所以 %ld 等价于 %d。

转换类型为：

转换符

含义

注释

'd'

有符号十进制整数。

'i'

有符号十进制整数。

'o'

有符号八进制数。

(1)

'u'

过时类型 – 等价于 'd'。

(6)

'x'

有符号十六进制数（小写）。

(2)

'X'

有符号十六进制数（大写）。

(2)

'e'

浮点指数格式（小写）。

(3)

'E'

浮点指数格式（大写）。

(3)

'f'

浮点十进制格式。

(3)

'F'

浮点十进制格式。

(3)

'g'

浮点格式。 如果指数小于 -4 或不小于精度则使用小写指数格式，否则使用十进制格式。

(4)

'G'

浮点格式。 如果指数小于 -4 或不小于精度则使用大写指数格式，否则使用十进制格式。

(4)

'c'

单个字符（接受整数或单个字符的字符串）。

'r'

字符串（使用 repr() 转换任何 Python 对象）。

(5)

's'

字符串（使用 str() 转换任何 Python 对象）。

(5)

'a'

字符串（使用 ascii() 转换任何 Python 对象）。

(5)

'%'

不转换参数，在结果中输出一个 '%' 字符。

注释:

此替代形式会在第一个数码之前插入标示八进制数的前缀 ('0o')。

此替代形式会在第一个数码之前插入 '0x' 或 '0X' 前缀（取决于是使用 'x' 还是 'X' 格式）。

此替代形式总是会在结果中包含一个小数点，即使其后并没有数码。

小数点后的数码位数由精度决定，默认为 6。

此替代形式总是会在结果中包含一个小数点，末尾各位的零不会如其他情况下那样被移除。

小数点前后的有效数码位数由精度决定，默认为 6。

如果精度为 N，输出将截短为 N 个字符。

参见 PEP 237 [https://www.python.org/dev/peps/pep-0237]。

由于 Python 字符串显式指明长度，%s 转换不会将 '\0' 视为字符串的结束。

在 3.1 版更改: 绝对值超过 1e50 的 %f 转换不会再被替换为 %g 转换。

4.8. 二进制序列类型 — bytes, bytearray, memoryview

操作二进制数据的核心内置类型是 bytes 和 bytearray。 它们由 memoryview 提供支持，该对象使用 缓冲区协议 来访问其他二进制对象所在内存，不需要创建对象的副本。

array 模块支持高效地存储基本数据类型，例如 32 位整数和 IEEE754 双精度浮点值。

4.8.1. bytes 对象

bytes 对象是由单个字节构成的不可变序列。 由于许多主要二进制协议都基于 ASCII 文本编码，因此 bytes 对象提供了一些仅在处理 ASCII 兼容数据时可用，并且在许多特性上与字符串对象紧密相关的方法。

class bytes([source[, encoding[, errors]]])

首先，表示 bytes 字面值的语法与字符串字面值的大致相同，只是添加了一个 b 前缀：

单引号: b'同样允许嵌入 "双" 引号'。

双引号: b"同样允许嵌入 '单' 引号"。

三重引号: b'''三重单引号''', b"""三重双引号"""

bytes 字面值中只允许 ASCII 字符（无论源代码声明的编码为何）。 任何超出 127 的二进制值必须使用相应的转义序列形式加入 bytes 字面值。

像字符串字面值一样，bytes 字面值也可以使用 r 前缀来禁用转义序列处理。 请参阅 字符串和字节串字面值 了解有关各种 bytes 字面值形式的详情，包括所支持的转义序列。

虽然 bytes 字面值和表示法是基于 ASCII 文本的，但 bytes 对象的行为实际上更像是不可变的整数序列，序列中的每个值的大小被限制为 0 <= x < 256 (如果违反此限制将引发 ValueError)。 这种限制是有意设计用以强调以下事实，虽然许多二进制格式都包含基于 ASCII 的元素，可以通过某些面向文本的算法进行有用的操作，但情况对于任意二进制数据来说通常却并非如此（盲目地将文本处理算法应用于不兼容 ASCII 的二进制数据格式往往将导致数据损坏）。

除了字面值形式，bytes 对象还可以通过其他几种方式来创建：

指定长度的以零值填充的 bytes 对象: bytes(10)

通过由整数组成的可迭代对象: bytes(range(20))

通过缓冲区协议复制现有的二进制数据: bytes(obj)

另请参阅 bytes 内置类型。

由于两个十六进制数码精确对应一个字节，因此十六进制数是描述二进制数据的常用格式。 相应地，bytes 类型具有从此种格式读取数据的附加类方法：

classmethod fromhex(string)

此 bytes 类方法返回一个解码给定字符串的 bytes 对象。 字符串必须由表示每个字节的两个十六进制数码构成，其中的 ASCII 空白符会被忽略。

>>> bytes.fromhex('2Ef0 F1f2  ')
b'.\xf0\xf1\xf2'

存在一个反向转换函数，可以将 bytes 对象转换为对应的十六进制表示。

hex()

返回一个字符串对象，该对象包含实例中每个字节的两个十六进制数字。

>>> b'\xf0\xf1\xf2'.hex()
'f0f1f2'

3.5 新版功能.

由于 bytes 对象是由整数构成的序列（类似于元组），因此对于一个 bytes 对象 b，b[0] 将为一个整数，而 b[0:1] 将为一个长度为 1 的 bytes 对象。 （这与文本字符串不同，索引和切片所产生的将都是一个长度为 1 的字符串）。

bytes 对象的表示使用字面值格式 (b'...')，因为它通常都要比像 bytes([46, 46, 46]) 这样的格式更好用。 你总是可以使用 list(b) 将 bytes 对象转换为一个由整数构成的列表。

注解

针对 Python 2.x 用户的说明：在 Python 2.x 系列中，允许 8 位字符串（ 2.x 所提供的最接近内置二进制数据类型的对象）与 Unicode 字符串进行各种隐式转换。 这是为了实现向下兼容的变通做法，以适应 Python 最初只支持 8 位文本而 Unicode 文本是后来才被加入这一事实。 在 Python 3.x 中，这些隐式转换已被取消 —— 8 位二进制数据与 Unicode 文本间的转换必须显式地进行，bytes 与字符串对象的比较结果将总是不相等。

4.8.2. bytearray 对象

bytearray 对象是 bytes 对象的可变对应物。

class bytearray([source[, encoding[, errors]]])

bytearray 对象没有专属的字面值语法，它们总是通过调用构造器来创建：

创建一个空实例: bytearray()

创建一个指定长度的以零值填充的实例: bytearray(10)

通过由整数组成的可迭代对象: bytearray(range(20))

通过缓冲区协议复制现有的二进制数据: bytearray(b'Hi!')

由于 bytearray 对象是可变的，该对象除了 bytes 和 bytearray 操作 中所描述的 bytes 和 bytearray 共有操作之外，还支持 可变 序列操作。

另请参见 bytearray 内置类型。

由于两个十六进制数码精确对应一个字节，因此十六进制数是描述二进制数据的常用格式。 相应地，bytearray 类型具有从此种格式读取数据的附加类方法：

classmethod fromhex(string)

bytearray 类方法返回一个解码给定字符串的 bytearray 对象。 字符串必须由表示每个字节的两个十六进制数码构成，其中的 ASCII 空白符会被忽略。

>>> bytearray.fromhex('2Ef0 F1f2  ')
bytearray(b'.\xf0\xf1\xf2')

存在一个反向转换函数，可以将 bytearray 对象转换为对应的十六进制表示。

hex()

返回一个字符串对象，该对象包含实例中每个字节的两个十六进制数字。

>>> bytearray(b'\xf0\xf1\xf2').hex()
'f0f1f2'

3.5 新版功能.

由于 bytearray 对象是由整数构成的序列（类似于列表），因此对于一个 bytearray 对象 b，b[0] 将为一个整数，而 b[0:1] 将为一个长度为 1 的 bytearray 对象。 （这与文本字符串不同，索引和切片所产生的将都是一个长度为 1 的字符串）。

bytearray 对象的表示使用 bytes 对象字面值格式 (bytearray(b'...'))，因为它通常都要比 bytearray([46, 46, 46]) 这样的格式更好用。 你总是可以使用 list(b) 将 bytearray 对象转换为一个由整数构成的列表。

4.8.3. bytes 和 bytearray 操作

bytes 和 bytearray 对象都支持 通用 序列操作。 它们不仅能与相同类型的操作数，也能与任何 bytes-like object 进行互操作。 由于这样的灵活性，它们可以在操作中自由地混合而不会导致错误。 但是，操作结果的返回值类型可能取决于操作数的顺序。

注解

bytes 和 bytearray 对象的方法不接受字符串作为其参数，就像字符串的方法不接受 bytes 对象作为其参数一样。 例如，你必须使用以下写法:

a = "abc"
b = a.replace("a", "f")

和:

a = b"abc"
b = a.replace(b"a", b"f")

某些 bytes 和 bytearray 操作假定使用兼容 ASCII 的二进制格式，因此在处理任意二进数数据时应当避免使用。 这些限制会在下文中说明。

注解

使用这些基于 ASCII 的操作来处理未以基于 ASCII 的格式存储的二进制数据可能会导致数据损坏。

bytes 和 bytearray 对象的下列方法可以用于任意二进制数据。

bytes.count(sub[, start[, end]])

bytearray.count(sub[, start[, end]])

返回子序列 sub 在 [start, end] 范围内非重叠出现的次数。 可选参数 start 与 end 会被解读为切片表示法。

要搜索的子序列可以是任意 bytes-like object 或是 0 至 255 范围内的整数。

在 3.3 版更改: 也接受 0 至 255 范围内的整数作为子序列。

bytes.decode(encoding="utf-8", errors="strict")

bytearray.decode(encoding="utf-8", errors="strict")

返回从给定 bytes 解码出来的字符串。 默认编码为 'utf-8'。 可以给出 errors 来设置不同的错误处理方案。 errors 的默认值为 'strict'，表示编码错误会引发 UnicodeError。 其他可用的值为 'ignore', 'replace' 以及任何其他通过 codecs.register_error() 注册的名称，请参阅 错误处理方案 小节。 要查看可用的编码列表，请参阅 标准编码 小节。

注解

将 encoding 参数传给 str 允许直接解码任何 bytes-like object，无须创建临时的 bytes 或 bytearray 对象。

在 3.1 版更改: 加入了对关键字参数的支持。

bytes.endswith(suffix[, start[, end]])

bytearray.endswith(suffix[, start[, end]])

如果二进制数据以指定的 suffix 结束则返回 True，否则返回 False。 suffix 也可以为由多个供查找的后缀构成的元组。 如果有可选项 start，将从所指定位置开始检查。 如果有可选项 end，将在所指定位置停止比较。

要搜索的后缀可以是任意 bytes-like object。

bytes.find(sub[, start[, end]])

bytearray.find(sub[, start[, end]])

返回子序列 sub 在数据中被找到的最小索引，sub 包含于切片 s[start:end] 之内。 可选参数 start 与 end 会被解读为切片表示法。 如果 sub 未被找到则返回 -1。

要搜索的子序列可以是任意 bytes-like object 或是 0 至 255 范围内的整数。

注解

find() 方法应该只在你需要知道 sub 所在位置时使用。 要检查 sub 是否为子串，请使用 in 操作符:

>>> b'Py' in b'Python'
True

在 3.3 版更改: 也接受 0 至 255 范围内的整数作为子序列。

bytes.index(sub[, start[, end]])

bytearray.index(sub[, start[, end]])

类似于 find()，但在找不到子序列时会引发 ValueError。

要搜索的子序列可以是任意 bytes-like object 或是 0 至 255 范围内的整数。

在 3.3 版更改: 也接受 0 至 255 范围内的整数作为子序列。

bytes.join(iterable)

bytearray.join(iterable)

返回一个由 iterable 中的二进制数据序列拼接而成的 bytes 或 bytearray 对象。 如果 iterable 中存在任何非 字节类对象 包括存在 str 对象值则会引发 TypeError。 提供该方法的 bytes 或 bytearray 对象的内容将作为元素之间的分隔。

static bytes.maketrans(from, to)

static bytearray.maketrans(from, to)

此静态方法返回一个可用于 bytes.translate() 的转换对照表，它将把 from 中的每个字符映射为 to 中相同位置上的字符；from 与 to 必须都是 字节类对象 并且具有相同的长度。

3.1 新版功能.

bytes.partition(sep)

bytearray.partition(sep)

在 sep 首次出现的位置拆分序列，返回一个 3 元组，其中包含分隔符之前的部分、分隔符本身或其 bytearray 副本，以及分隔符之后的部分。 如果分隔符未找到，则返回的 3 元组中包含原序列以及两个空的 bytes 或 bytearray 对象。

要搜索的分隔符可以是任意 bytes-like object。

bytes.replace(old, new[, count])

bytearray.replace(old, new[, count])

返回序列的副本，其中出现的所有子序列 old 都将被替换为 new。 如果给出了可选参数 count，则只替换前 count 次出现。

要搜索的子序列及其替换序列可以是任意 bytes-like object。

注解

此方法的 bytearray 版本 并非 原地操作 —— 它总是产生一个新对象，即便没有做任何改变。

bytes.rfind(sub[, start[, end]])

bytearray.rfind(sub[, start[, end]])

返回子序列 sub 在序列内被找到的最大（最右）索引，这样 sub 将包含在 s[start:end] 当中。 可选参数 start 与 end 会被解读为切片表示法。 如果未找到则返回 -1。

要搜索的子序列可以是任意 bytes-like object 或是 0 至 255 范围内的整数。

在 3.3 版更改: 也接受 0 至 255 范围内的整数作为子序列。

bytes.rindex(sub[, start[, end]])

bytearray.rindex(sub[, start[, end]])

类似于 rfind()，但在子序列 sub 未找到时会引发 ValueError。

要搜索的子序列可以是任意 bytes-like object 或是 0 至 255 范围内的整数。

在 3.3 版更改: 也接受 0 至 255 范围内的整数作为子序列。

bytes.rpartition(sep)

bytearray.rpartition(sep)

Split the sequence at the last occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself or its bytearray copy, and the part after the separator. If the separator is not found, return a 3-tuple containing a copy of the original sequence, followed by two empty bytes or bytearray objects.

要搜索的分隔符可以是任意 bytes-like object。

bytes.startswith(prefix[, start[, end]])

bytearray.startswith(prefix[, start[, end]])

如果二进制数据以指定的 prefix 开头则返回 True，否则返回 False。 prefix 也可以为由多个供查找的前缀构成的元组。 如果有可选项 start，将从所指定位置开始检查。 如果有可选项 end，将在所指定位置停止比较。

要搜索的前缀可以是任意 bytes-like object。

bytes.translate(table, delete=b'')

bytearray.translate(table, delete=b'')

返回原 bytes 或 bytearray 对象的副本，移除其中所有在可选参数 delete 中出现的 bytes，其余 bytes 将通过给定的转换表进行映射，该转换表必须是长度为 256 的 bytes 对象。

你可以使用 bytes.maketrans() 方法来创建转换表。

对于仅需移除字符的转换，请将 table 参数设为 None:

>>> b'read this short text'.translate(None, b'aeiou')
b'rd ths shrt txt'

在 3.6 版更改: 现在支持将 delete 作为关键字参数。

以下 bytes 和 bytearray 对象的方法的默认行为会假定使用兼容 ASCII 的二进制格式，但通过传入适当的参数仍然可用于任意二进制数据。 请注意本小节中所有的 bytearray 方法都 不是 原地执行操作，而是会产生新的对象。

bytes.center(width[, fillbyte])

bytearray.center(width[, fillbyte])

返回原对象的副本，在长度为 width 的序列内居中，使用指定的 fillbyte 填充两边的空位（默认使用 ASCII 空格符）。 对于 bytes 对象，如果 width 小于等于 len(s) 则返回原序列的副本。

注解

此方法的 bytearray 版本 并非 原地操作 —— 它总是产生一个新对象，即便没有做任何改变。

bytes.ljust(width[, fillbyte])

bytearray.ljust(width[, fillbyte])

返回原对象的副本，在长度为 width 的序列中靠左对齐。 使用指定的 fillbyte 填充空位（默认使用 ASCII 空格符）。 对于 bytes 对象，如果 width 小于等于 len(s) 则返回原序列的副本。

注解

此方法的 bytearray 版本 并非 原地操作 —— 它总是产生一个新对象，即便没有做任何改变。

bytes.lstrip([chars])

bytearray.lstrip([chars])

返回原序列的副本，移除指定的前导字节。 chars 参数为指定要移除字节值集合的二进制序列 —— 这个名称表明此方法通常是用于 ASCII 字符。 如果省略或为 None，则 chars 参数默认移除 ASCII 空白符。 chars 参数并非指定单个前缀；而是会移除参数值的所有组合:

>>> b'   spacious   '.lstrip()
b'spacious   '
>>> b'www.example.com'.lstrip(b'cmowz.')
b'example.com'

要移除的字节值二进制序列可以是任意 bytes-like object。

注解

此方法的 bytearray 版本 并非 原地操作 —— 它总是产生一个新对象，即便没有做任何改变。

bytes.rjust(width[, fillbyte])

bytearray.rjust(width[, fillbyte])

返回原对象的副本，在长度为 width 的序列中靠右对齐。 使用指定的 fillbyte 填充空位（默认使用 ASCII 空格符）。 对于 bytes 对象，如果 width 小于等于 len(s) 则返回原序列的副本。

注解

此方法的 bytearray 版本 并非 原地操作 —— 它总是产生一个新对象，即便没有做任何改变。

bytes.rsplit(sep=None, maxsplit=-1)

bytearray.rsplit(sep=None, maxsplit=-1)

将二进制序列拆分为相同类型的子序列，使用 sep 作为分隔符。 如果给出了 maxsplit，则最多进行 maxsplit 次拆分，从 最右边 开始。 如果 sep 未指定或为 None，任何只包含 ASCII 空白符的子序列都会被作为分隔符。 除了从右边开始拆分，rsplit() 的其他行为都类似于下文所述的 split()。

bytes.rstrip([chars])

bytearray.rstrip([chars])

返回原序列的副本，移除指定的末尾字节。 chars 参数为指定要移除字节值集合的二进制序列 —— 这个名称表明此方法通常是用于 ASCII 字符。 如果省略或为 None，则 chars 参数默认移除 ASCII 空白符。 chars 参数并非指定单个后缀；而是会移除参数值的所有组合:

>>> b'   spacious   '.rstrip()
b'   spacious'
>>> b'mississippi'.rstrip(b'ipz')
b'mississ'

要移除的字节值二进制序列可以是任意 bytes-like object。

注解

此方法的 bytearray 版本 并非 原地操作 —— 它总是产生一个新对象，即便没有做任何改变。

bytes.split(sep=None, maxsplit=-1)

bytearray.split(sep=None, maxsplit=-1)

将二进制序列拆分为相同类型的子序列，使用 sep 作为分隔符。 如果给出了 maxsplit 且非负值，则最多进行 maxsplit 次拆分（因此，列表最多会有 maxsplit+1 个元素）。 如果 maxsplit 未指定或为 -1，则不限制拆分次数（进行所有可能的拆分）。

如果给出了 sep，则连续的分隔符不会被组合在一起而是被视为分隔空子序列 (例如 b'1,,2'.split(b',') 将返回 [b'1', b'', b'2'])。 sep 参数可能为一个多字节序列 (例如 b'1<>2<>3'.split(b'<>') 将返回 [b'1', b'2', b'3'])。 使用指定的分隔符拆分空序列将返回 [b''] 或 [bytearray(b'')]，具体取决于被拆分对象的类型。 sep 参数可以是任意 bytes-like object。

例如

>>> b'1,2,3'.split(b',')
[b'1', b'2', b'3']
>>> b'1,2,3'.split(b',', maxsplit=1)
[b'1', b'2,3']
>>> b'1,2,,3,'.split(b',')
[b'1', b'2', b'', b'3', b'']

如果 sep 未指定或为 None，则会应用另一种拆分算法：连续的 ASCII 空白符会被视为单个分隔符，其结果将不包含序列开头或末尾的空白符。 因此，在不指定分隔符的情况下对空序列或仅包含 ASCII 空白符的序列进行拆分将返回 []。

例如

>>> b'1 2 3'.split()
[b'1', b'2', b'3']
>>> b'1 2 3'.split(maxsplit=1)
[b'1', b'2 3']
>>> b'   1   2   3   '.split()
[b'1', b'2', b'3']

bytes.strip([chars])

bytearray.strip([chars])

返回原序列的副本，移除指定的开头和末尾字节。 chars 参数为指定要移除字节值集合的二进制序列 —— 这个名称表明此方法通常是用于 ASCII 字符。 如果省略或为 None，则 chars 参数默认移除 ASCII 空白符。 chars 参数并非指定单个前缀或后缀；而是会移除参数值的所有组合:

>>> b'   spacious   '.strip()
b'spacious'
>>> b'www.example.com'.strip(b'cmowz.')
b'example'

要移除的字节值二进制序列可以是任意 bytes-like object。

注解

此方法的 bytearray 版本 并非 原地操作 —— 它总是产生一个新对象，即便没有做任何改变。

以下 bytes 和 bytearray 对象的方法会假定使用兼容 ASCII 的二进制格式，不应当被应用于任意二进制数据。 请注意本小节中所有的 bytearray 方法都 不是 原地执行操作，而是会产生新的对象。

bytes.capitalize()

bytearray.capitalize()

返回原序列的副本，其中每个字节将都将被解读为一个 ASCII 字符，并且第一个字节的字符大写而其余的小写。 非 ASCII 字节值将保持原样不变。

注解

此方法的 bytearray 版本 并非 原地操作 —— 它总是产生一个新对象，即便没有做任何改变。

bytes.expandtabs(tabsize=8)

bytearray.expandtabs(tabsize=8)

返回序列的副本，其中所有的 ASCII 制表符会由一个或多个 ASCII 空格替换，具体取决于当前列位置和给定的制表符宽度。 每 tabsize 个字节设为一个制表位（默认值 8 时设定的制表位在列 0, 8, 16 依次类推）。 要展开序列，当前列位置将被设为零并逐一检查序列中的每个字节。 如果字节为 ASCII 制表符 (b'\t')，则并在结果中插入一个或多个空格符，直到当前列等于下一个制表位。 （制表符本身不会被复制。） 如果当前字节为 ASCII 换行符 (b'\n') 或回车符 (b'\r')，它会被复制并将当前列重设为零。 任何其他字节会被不加修改地复制并将当前列加一，不论该字节值在被打印时会如何显示:

>>> b'01\t012\t0123\t01234'.expandtabs()
b'01      012     0123    01234'
>>> b'01\t012\t0123\t01234'.expandtabs(4)
b'01  012 0123    01234'

注解

此方法的 bytearray 版本 并非 原地操作 —— 它总是产生一个新对象，即便没有做任何改变。

bytes.isalnum()

bytearray.isalnum()

Return true if all bytes in the sequence are alphabetical ASCII characters or ASCII decimal digits and the sequence is not empty, false otherwise. Alphabetic ASCII characters are those byte values in the sequence b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'. ASCII decimal digits are those byte values in the sequence b'0123456789'.

例如

>>> b'ABCabc1'.isalnum()
True
>>> b'ABC abc1'.isalnum()
False

bytes.isalpha()

bytearray.isalpha()

Return true if all bytes in the sequence are alphabetic ASCII characters and the sequence is not empty, false otherwise. Alphabetic ASCII characters are those byte values in the sequence b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.

例如

>>> b'ABCabc'.isalpha()
True
>>> b'ABCabc1'.isalpha()
False

bytes.isdigit()

bytearray.isdigit()

Return true if all bytes in the sequence are ASCII decimal digits and the sequence is not empty, false otherwise. ASCII decimal digits are those byte values in the sequence b'0123456789'.

例如

>>> b'1234'.isdigit()
True
>>> b'1.23'.isdigit()
False

bytes.islower()

bytearray.islower()

Return true if there is at least one lowercase ASCII character in the sequence and no uppercase ASCII characters, false otherwise.

例如

>>> b'hello world'.islower()
True
>>> b'Hello world'.islower()
False

小写 ASCII 字符就是字节值包含在序列 b'abcdefghijklmnopqrstuvwxyz' 中的字符。 大写 ASCII 字符就是字节值包含在序列 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 中的字符。

bytes.isspace()

bytearray.isspace()

Return true if all bytes in the sequence are ASCII whitespace and the sequence is not empty, false otherwise. ASCII whitespace characters are those byte values in the sequence b' \t\n\r\x0b\f' (space, tab, newline, carriage return, vertical tab, form feed).

bytes.istitle()

bytearray.istitle()

Return true if the sequence is ASCII titlecase and the sequence is not empty, false otherwise. See bytes.title() for more details on the definition of “titlecase”.

例如

>>> b'Hello World'.istitle()
True
>>> b'Hello world'.istitle()
False

bytes.isupper()

bytearray.isupper()

Return true if there is at least one uppercase alphabetic ASCII character in the sequence and no lowercase ASCII characters, false otherwise.

例如

>>> b'HELLO WORLD'.isupper()
True
>>> b'Hello world'.isupper()
False

小写 ASCII 字符就是字节值包含在序列 b'abcdefghijklmnopqrstuvwxyz' 中的字符。 大写 ASCII 字符就是字节值包含在序列 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 中的字符。

bytes.lower()

bytearray.lower()

返回原序列的副本，其所有大写 ASCII 字符均转换为对应的小写形式。

例如

>>> b'Hello World'.lower()
b'hello world'

小写 ASCII 字符就是字节值包含在序列 b'abcdefghijklmnopqrstuvwxyz' 中的字符。 大写 ASCII 字符就是字节值包含在序列 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 中的字符。

注解

此方法的 bytearray 版本 并非 原地操作 —— 它总是产生一个新对象，即便没有做任何改变。

bytes.splitlines(keepends=False)

bytearray.splitlines(keepends=False)

返回由原二进制序列中各行组成的列表，在 ASCII 行边界符的位置拆分。 此方法使用 universal newlines 方式来分行。 结果列表中不包含换行符，除非给出了 keepends 且为真值。

例如

>>> b'ab c\n\nde fg\rkl\r\n'.splitlines()
[b'ab c', b'', b'de fg', b'kl']
>>> b'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
[b'ab c\n', b'\n', b'de fg\r', b'kl\r\n']

不同于 split()，当给出了分隔符 sep 时，对于空字符串此方法将返回一个空列表，而末尾的换行不会令结果中增加额外的行:

>>> b"".split(b'\n'), b"Two lines\n".split(b'\n')
([b''], [b'Two lines', b''])
>>> b"".splitlines(), b"One line\n".splitlines()
([], [b'One line'])

bytes.swapcase()

bytearray.swapcase()

返回原序列的副本，其所有小写 ASCII 字符均转换为对应的大写形式，反之亦反。

例如

>>> b'Hello World'.swapcase()
b'hELLO wORLD'

小写 ASCII 字符就是字节值包含在序列 b'abcdefghijklmnopqrstuvwxyz' 中的字符。 大写 ASCII 字符就是字节值包含在序列 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 中的字符。

不同于 str.swapcase()，在些二进制版本下 bin.swapcase().swapcase() == bin 总是成立。 大小写转换在 ASCII 中是对称的，即使其对于任意 Unicode 码位来说并不总是成立。

注解

此方法的 bytearray 版本 并非 原地操作 —— 它总是产生一个新对象，即便没有做任何改变。

bytes.title()

bytearray.title()

返回原二进制序列的标题版本，其中每个单词以一个大写 ASCII 字符为开头，其余字母为小写。 不区别大小写的字节值将保持原样不变。

例如

>>> b'Hello world'.title()
b'Hello World'

小写 ASCII 字符就是字节值包含在序列 b'abcdefghijklmnopqrstuvwxyz' 中的字符。 大写 ASCII 字符就是字节值包含在序列 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 中的字符。 所有其他字节值都不区分大小写。

该算法使用一种简单的与语言无关的定义，将连续的字母组合视为单词。 该定义在多数情况下都很有效，但它也意味着代表缩写形式与所有格的撇号也会成为单词边界，这可能导致不希望的结果:

>>> b"they're bill's friends from the UK".title()
b"They'Re Bill'S Friends From The Uk"

可以使用正则表达式来构建针对撇号的特别处理:

>>> import re
>>> def titlecase(s):
...     return re.sub(rb"[A-Za-z]+('[A-Za-z]+)?",
...                   lambda mo: mo.group(0)[0:1].upper() +
...                              mo.group(0)[1:].lower(),
...                   s)
...
>>> titlecase(b"they're bill's friends.")
b"They're Bill's Friends."

注解

此方法的 bytearray 版本 并非 原地操作 —— 它总是产生一个新对象，即便没有做任何改变。

bytes.upper()

bytearray.upper()

返回原序列的副本，其所有小写 ASCII 字符均转换为对应的大写形式。

例如

>>> b'Hello World'.upper()
b'HELLO WORLD'

小写 ASCII 字符就是字节值包含在序列 b'abcdefghijklmnopqrstuvwxyz' 中的字符。 大写 ASCII 字符就是字节值包含在序列 b'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 中的字符。

注解

此方法的 bytearray 版本 并非 原地操作 —— 它总是产生一个新对象，即便没有做任何改变。

bytes.zfill(width)

bytearray.zfill(width)

返回原序列的副本，在左边填充 b'0' 数码使序列长度为 width。 正负值前缀 (b'+'/ b'-') 的处理方式是在正负符号 之后 填充而非在之前。 对于 bytes 对象，如果 width 小于等于 len(seq) 则返回原序列。

例如

>>> b"42".zfill(5)
b'00042'
>>> b"-42".zfill(5)
b'-0042'

注解

此方法的 bytearray 版本 并非 原地操作 —— 它总是产生一个新对象，即便没有做任何改变。

4.8.4. printf 风格的字节串格式化

注解

此处介绍的格式化操作具有多种怪异特性，可能导致许多常见错误（例如无法正确显示元组和字典）。 如果要打印的值可能为元组或字典，请将其放入一个元组中。

字节串对象 (bytes/bytearray) 具有一种特殊的内置操作：使用 % (取模) 运算符。 这也被称为字节串的 格式化 或 插值 运算符。 对于 format % values (其中 format 为一个字节串对象)，在 format 中的 % 转换标记符将被替换为零个或多个 values 条目。 其效果类似于在 C 语言中使用 sprintf()。

如果 format 要求一个单独参数，则 values 可以为一个非元组对象。 5 否则的话，values 必须或是是一个包含项数与格式字节串对象中指定的转换符项数相同的元组，或者是一个单独的映射对象（例如元组）。

转换标记符包含两个或更多字符并具有以下组成，且必须遵循此处规定的顺序：

'%' 字符，用于标记转换符的起始。

映射键（可选），由加圆括号的字符序列组成 (例如 (somename))。

转换旗标（可选），用于影响某些转换类型的结果。

最小字段宽度（可选）。 如果指定为 '*' (星号)，则实际宽度会从 values 元组的下一元素中读取，要转换的对象则为最小字段宽度和可选的精度之后的元素。

精度（可选），以在 '.' (点号) 之后加精度值的形式给出。 如果指定为 '*' (星号)，则实际精度会从 values 元组的下一元素中读取，要转换的对象则为精度之后的元素。

长度修饰符（可选）。

转换类型。

当右边的参数为一个字典（或其他映射类型）时，字节串对象中的格式 必须 包含加圆括号的映射键，对应 '%' 字符之后字典中的每一项。 映射键将从映射中选取要格式化的值。 例如：

>>> print(b'%(language)s has %(number)03d quote types.' %
...       {b'language': b"Python", b"number": 2})
b'Python has 002 quote types.'

在此情况下格式中不能出现 * 标记符（因其需要一个序列类的参数列表）。

转换旗标为：

标志

含义

'#'

值的转换将使用“替代形式”（具体定义见下文）。

'0'

转换将为数字值填充零字符。

'-'

转换值将靠左对齐（如果同时给出 '0' 转换，则会覆盖后者）。

' '

(空格) 符号位转换产生的正数（或空字符串）前将留出一个空格。

'+'

符号字符 ('+' 或 '-') 将显示于转换结果的开头（会覆盖 “空格” 旗标）。

可以给出长度修饰符 (h, l 或 L)，但会被忽略，因为对 Python 来说没有必要 – 所以 %ld 等价于 %d。

转换类型为：

转换符

含义

注释

'd'

有符号十进制整数。

'i'

有符号十进制整数。

'o'

有符号八进制数。

(1)

'u'

过时类型 – 等价于 'd'。

(8)

'x'

有符号十六进制数（小写）。

(2)

'X'

有符号十六进制数（大写）。

(2)

'e'

浮点指数格式（小写）。

(3)

'E'

浮点指数格式（大写）。

(3)

'f'

浮点十进制格式。

(3)

'F'

浮点十进制格式。

(3)

'g'

浮点格式。 如果指数小于 -4 或不小于精度则使用小写指数格式，否则使用十进制格式。

(4)

'G'

浮点格式。 如果指数小于 -4 或不小于精度则使用大写指数格式，否则使用十进制格式。

(4)

'c'

单个字节（接受整数或单个字节对象）。

'b'

字节串（任何遵循 缓冲区协议 或是具有 __bytes__() 的对象）。

(5)

's'

's' 是 'b' 的一个别名，只应当在基于 Python2/3 的代码中使用。

(6)

'a'

字节串（使用 repr(obj).encode('ascii','backslashreplace) 转换任何 Python 对象）。

(5)

'r'

'r' 是 'a' 的一个别名，只应当在基于 Python2/3 的代码中使用。

(7)

'%'

不转换参数，在结果中输出一个 '%' 字符。

注释:

此替代形式会在第一个数码之前插入标示八进制数的前缀 ('0o')。

此替代形式会在第一个数码之前插入 '0x' 或 '0X' 前缀（取决于是使用 'x' 还是 'X' 格式）。

此替代形式总是会在结果中包含一个小数点，即使其后并没有数码。

小数点后的数码位数由精度决定，默认为 6。

此替代形式总是会在结果中包含一个小数点，末尾各位的零不会如其他情况下那样被移除。

小数点前后的有效数码位数由精度决定，默认为 6。

如果精度为 N，输出将截短为 N 个字符。

b'%s' 已弃用，但在 3.x 系列中将不会被移除。

b'%r' 已弃用，但在 3.x 系列中将不会被移除。

参见 PEP 237 [https://www.python.org/dev/peps/pep-0237]。

注解

此方法的 bytearray 版本 并非 原地操作 —— 它总是产生一个新对象，即便没有做任何改变。

参见

PEP 461 [https://www.python.org/dev/peps/pep-0461] - 为 bytes 和 bytearray 添加 % 格式化

3.5 新版功能.

4.8.5. 内存视图

memoryview 对象允许 Python 代码访问一个对象的内部数据，只要该对象支持 缓冲区协议 而无需进行拷贝。

class memoryview(obj)

创建一个引用 obj 的 memoryview。 obj 必须支持缓冲区协议。 支持缓冲区协议的内置对象包括 bytes 和 bytearray。

memoryview 具有 元素 的概念，即由原始对象 obj 所处理的基本内存单元。 对于许多简单类型例如 bytes 和 bytearray 来说，一个元素就是一个字节，但是其他的类型例如 array.array 可能有更大的元素。

len(view) 与 tolist 的长度相等。 如果 view.ndim = 0，则其长度为 1。 如果 view.ndim = 1，则其长度等于 view 中元素的数量。 对于更高的维度，其长度等于表示 view 的嵌套列表的长度。 itemsize 属性可向你给出单个元素所占的字节数。

memoryview 支持通过切片和索引访问其元素。 一维切片的结果将是一个子视图:

>>> v = memoryview(b'abcefg')
>>> v[1]
98
>>> v[-1]
103
>>> v[1:4]
<memory at 0x7f3ddc9f4350>
>>> bytes(v[1:4])
b'bce'

如果 format 是一个来自于 struct 模块的原生格式说明符，则也支持使用整数或由整数构成的元组进行索引，并返回具有正确类型的单个 元素。 一维内存视图可以使用一个整数或由一个整数构成的元组进行索引。 多维内存视图可以使用由恰好 ndim 个整数构成的元素进行索引，ndim 即其维度。 零维内存视图可以使用空元组进行索引。

这里是一个使用非字节格式的例子:

>>> import array
>>> a = array.array('l', [-11111111, 22222222, -33333333, 44444444])
>>> m = memoryview(a)
>>> m[0]
-11111111
>>> m[-1]
44444444
>>> m[::2].tolist()
[-11111111, -33333333]

如果下层对象是可写的，则内存视图支持一维切片赋值。 改变大小则不被允许:

>>> data = bytearray(b'abcefg')
>>> v = memoryview(data)
>>> v.readonly
False
>>> v[0] = ord(b'z')
>>> data
bytearray(b'zbcefg')
>>> v[1:4] = b'123'
>>> data
bytearray(b'z123fg')
>>> v[2:3] = b'spam'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: memoryview assignment: lvalue and rvalue have different structures
>>> v[2:6] = b'spam'
>>> data
bytearray(b'z1spam')

由带有格式符号 ‘B’, ‘b’ 或 ‘c’ 的可哈希（只读）类型构成的一维内存视图同样是可哈希的。 哈希定义为 hash(m) == hash(m.tobytes()):

>>> v = memoryview(b'abcefg')
>>> hash(v) == hash(b'abcefg')
True
>>> hash(v[2:4]) == hash(b'ce')
True
>>> hash(v[::-2]) == hash(b'abcefg'[::-2])
True

在 3.3 版更改: 一维内存视图现在可以被切片。 带有格式符号 ‘B’, ‘b’ 或 ‘c’ 的一维内存视图现在是可哈希的。

在 3.4 版更改: 内存视图现在会自动注册为 collections.abc.Sequence

在 3.5 版更改: 内存视图现在可使用整数元组进行索引。

memoryview 具有以下一些方法：

__eq__(exporter)

memoryview 与 PEP 3118 [https://www.python.org/dev/peps/pep-3118] 中的导出器这两者如果形状相同，并且如果当使用 struct 语法解读操作数的相应格式代码时所有对应值都相同，则它们就是等价的。

对于 tolist() 当前所支持的 struct 格式字符串子集，如果 v.tolist() == w.tolist() 则 v 和 w 相等:

>>> import array
>>> a = array.array('I', [1, 2, 3, 4, 5])
>>> b = array.array('d', [1.0, 2.0, 3.0, 4.0, 5.0])
>>> c = array.array('b', [5, 3, 1])
>>> x = memoryview(a)
>>> y = memoryview(b)
>>> x == a == y == b
True
>>> x.tolist() == a.tolist() == y.tolist() == b.tolist()
True
>>> z = y[::-2]
>>> z == c
True
>>> z.tolist() == c.tolist()
True

如果两边的格式字符串都不被 struct 模块所支持，则两对象比较结果总是不相等（即使格式字符串和缓冲区内容相同）:

>>> from ctypes import BigEndianStructure, c_long
>>> class BEPoint(BigEndianStructure):
...     _fields_ = [("x", c_long), ("y", c_long)]
...
>>> point = BEPoint(100, 200)
>>> a = memoryview(point)
>>> b = memoryview(point)
>>> a == point
False
>>> a == b
False

请注意，与浮点数的情况一样，对于内存视图对象来说，v is w 也 并不 意味着 v == w。

在 3.3 版更改: 之前的版本比较原始内存时会忽略条目的格式与逻辑数组结构。

tobytes()

将缓冲区中的数据作为字节串返回。 这相当于在内存视图上调用 bytes 构造器。

>>> m = memoryview(b"abc")
>>> m.tobytes()
b'abc'
>>> bytes(m)
b'abc'

对于非连续数组，结果等于平面化表示的列表，其中所有元素都转换为字节串。 tobytes() 支持所有格式字符串，不符合 struct 模块语法的那些也包括在内。

hex()

返回一个字符串对象，其中分别以两个十六进制数码表示缓冲区里的每个字节。

>>> m = memoryview(b"abc")
>>> m.hex()
'616263'

3.5 新版功能.

tolist()

将缓冲区内的数据以一个元素列表的形式返回。

>>> memoryview(b'abc').tolist()
[97, 98, 99]
>>> import array
>>> a = array.array('d', [1.1, 2.2, 3.3])
>>> m = memoryview(a)
>>> m.tolist()
[1.1, 2.2, 3.3]

在 3.3 版更改: tolist() 现在支持 struct 模块语法中的所有单字符原生格式以及多维表示形式。

release()

释放由内存视图对象所公开的底层缓冲区。 许多对象在被视图所获取时都会采取特殊动作（例如，bytearray 将会暂时禁止调整大小）；因此，调用 release() 可以方便地尽早去除这些限制（并释放任何多余的资源）。

在此方法被调用后，任何对视图的进一步操作将引发 ValueError (release() 本身除外，它可以被多次调用):

>>> m = memoryview(b'abc')
>>> m.release()
>>> m[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operation forbidden on released memoryview object

使用 with 语句，可以通过上下文管理协议达到类似的效果:

>>> with memoryview(b'abc') as m:
...     m[0]
...
97
>>> m[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operation forbidden on released memoryview object

3.2 新版功能.

cast(format[, shape])

将内存视图转化为新的格式或形状。 shape 默认为 [byte_length//new_itemsize]，这意味着结果视图将是一维的。 返回值是一个新的内存视图，但缓冲区本身不会被复制。 支持的转化有 1D -> C-contiguous 和 C-contiguous -> 1D。

目标格式仅限于 struct 语法中的单一元素原生格式。 其中一种格式必须为字节格式 (‘B’, ‘b’ 或 ‘c’)。 结果的字节长度必须与原始长度相同。

将 1D/long 转换为 1D/unsigned bytes:

>>> import array
>>> a = array.array('l', [1,2,3])
>>> x = memoryview(a)
>>> x.format
'l'
>>> x.itemsize
8
>>> len(x)
3
>>> x.nbytes
24
>>> y = x.cast('B')
>>> y.format
'B'
>>> y.itemsize
1
>>> len(y)
24
>>> y.nbytes
24

将 1D/unsigned bytes 转换为 1D/char:

>>> b = bytearray(b'zyz')
>>> x = memoryview(b)
>>> x[0] = b'a'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: memoryview: invalid value for format "B"
>>> y = x.cast('c')
>>> y[0] = b'a'
>>> b
bytearray(b'ayz')

将 1D/bytes 转换为 3D/ints 再转换为 1D/signed char:

>>> import struct
>>> buf = struct.pack("i"*12, *list(range(12)))
>>> x = memoryview(buf)
>>> y = x.cast('i', shape=[2,2,3])
>>> y.tolist()
[[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]
>>> y.format
'i'
>>> y.itemsize
4
>>> len(y)
2
>>> y.nbytes
48
>>> z = y.cast('b')
>>> z.format
'b'
>>> z.itemsize
1
>>> len(z)
48
>>> z.nbytes
48

Cast 1D/unsigned char to 2D/unsigned long:

>>> buf = struct.pack("L"*6, *list(range(6)))
>>> x = memoryview(buf)
>>> y = x.cast('L', shape=[2,3])
>>> len(y)
2
>>> y.nbytes
48
>>> y.tolist()
[[0, 1, 2], [3, 4, 5]]

3.3 新版功能.

在 3.5 版更改: 当转换为字节视图时，源格式将不再受限。

还存在一些可用的只读属性：

obj

内存视图的下层对象:

>>> b  = bytearray(b'xyz')
>>> m = memoryview(b)
>>> m.obj is b
True

3.3 新版功能.

nbytes

nbytes == product(shape) * itemsize == len(m.tobytes())。 这是数组在连续表示时将会占用的空间总字节数。 它不一定等于 len(m):

>>> import array
>>> a = array.array('i', [1,2,3,4,5])
>>> m = memoryview(a)
>>> len(m)
5
>>> m.nbytes
20
>>> y = m[::2]
>>> len(y)
3
>>> y.nbytes
12
>>> len(y.tobytes())
12

多维数组:

>>> import struct
>>> buf = struct.pack("d"*12, *[1.5*x for x in range(12)])
>>> x = memoryview(buf)
>>> y = x.cast('d', shape=[3,4])
>>> y.tolist()
[[0.0, 1.5, 3.0, 4.5], [6.0, 7.5, 9.0, 10.5], [12.0, 13.5, 15.0, 16.5]]
>>> len(y)
3
>>> y.nbytes
96

3.3 新版功能.

readonly

一个表明内存是否只读的布尔值。

format

一个字符串，包含视图中每个元素的格式（表示为 struct 模块样式）。 内存视图可以从具有任意格式字符串的导出器创建，但某些方法 (例如 tolist()) 仅限于原生的单元素格式。

在 3.3 版更改: 格式 'B' 现在会按照 struct 模块语法来处理。 这意味着 memoryview(b'abc')[0] == b'abc'[0] == 97。

itemsize

memoryview 中每个元素以字节表示的大小:

>>> import array, struct
>>> m = memoryview(array.array('H', [32000, 32001, 32002]))
>>> m.itemsize
2
>>> m[0]
32000
>>> struct.calcsize('H') == m.itemsize
True

ndim

一个整数，表示内存所代表的多维数组具有多少个维度。

shape

一个整数元组，通过 ndim 的长度值给出内存所代表的 N 维数组的形状。

在 3.3 版更改: 当 ndim = 0 时值为空元组而不再为 None。

strides

一个整数元组，通过 ndim 的长度给出以字节表示的大小，以便访问数组中每个维度上的每个元素。

在 3.3 版更改: 当 ndim = 0 时值为空元组而不再为 None。

suboffsets

供 PIL 风格的数组内部使用。 该值仅作为参考信息。

c_contiguous

一个表明内存是否为 C-contiguous 的布尔值。

3.3 新版功能.

f_contiguous

一个表明内存是否为 Fortran contiguous 的布尔值。

3.3 新版功能.

contiguous

一个表明内存是否为 contiguous 的布尔值。

3.3 新版功能.

4.9. 集合类型 — set, frozenset

set 对象是由具有唯一性的 hashable 对象所组成的无序多项集。 常见的用途包括成员检测、从序列中去除重复项以及数学中的集合类计算，例如交集、并集、差集与对称差集等等。 （关于其他容器对象请参看 dict, list 与 tuple 等内置类，以及 collections 模块。）

与其他多项集一样，集合也支持 x in set, len(set) 和 for x in set。 作为一种无序的多项集，集合并不记录元素位置或插入顺序。 相应地，集合不支持索引、切片或其他序列类的操作。

目前有两种内置集合类型，set 和 frozenset。 set 类型是可变的 — 其内容可以使用 add() 和 remove() 这样的方法来改变。 由于是可变类型，它没有哈希值，且不能被用作字典的键或其他集合的元素。 frozenset 类型是不可变并且为 hashable — 其内容在被创建后不能再改变；因此它可以被用作字典的键或其他集合的元素。

除了可以使用 set 构造器，非空的 set (不是 frozenset) 还可以通过将以逗号分隔的元素列表包含于花括号之内来创建，例如: {'jack', 'sjoerd'}。

两个类的构造器具有相同的作用方式：

class set([iterable])

class frozenset([iterable])

返回一个新的 set 或 frozenset 对象，其元素来自于 iterable。 集合的元素必须为 hashable。 要表示由集合对象构成的集合，所有的内层集合必须为 frozenset 对象。 如果未指定 iterable，则将返回一个新的空集合。

set 和 frozenset 的实例提供以下操作：

len(s)

返回集合 s 中的元素数量（即 s 的基数）。

x in s

检测 x 是否为 s 中的成员。

x not in s

检测 x 是否非 s 中的成员。

isdisjoint(other)

如果集合中没有与 other 共有的元素则返回 True。 当且仅当两个集合的交集为空集合时，两者为不相交集合。

issubset(other)

set <= other

检测是否集合中的每个元素都在 other 之中。

set < other

检测集合是否为 other 的真子集，即 set <= other and set != other。

issuperset(other)

set >= other

检测是否 other 中的每个元素都在集合之中。

set > other

检测集合是否为 other 的真超集，即 set >= other and set != other。

union(*others)

set | other | ...

返回一个新集合，其中包含来自原集合以及 others 指定的所有集合中的元素。

intersection(*others)

set & other & ...

返回一个新集合，其中包含原集合以及 others 指定的所有集合中共有的元素。

difference(*others)

set - other - ...

返回一个新集合，其中包含原集合中在 others 指定的其他集合中不存在的元素。

symmetric_difference(other)

set ^ other

返回一个新集合，其中的元素或属于原集合或属于 other 指定的其他集合，但不能同时属于两者。

copy()

Return a new set with a shallow copy of s.

请注意，非运算符版本的 union(), intersection(), difference()，以及 symmetric_difference(), issubset() 和 issuperset() 方法会接受任意可迭代对象作为参数。 相比之下，它们所对应的运算符版本则要求其参数为集合。 这就排除了容易出错的构造形式例如 set('abc') & 'cbs'，而推荐可读性更强的 set('abc').intersection('cbs')。

set 和 frozenset 均支持集合与集合的比较。 两个集合当且仅当每个集合中的每个元素均包含于另一个集合之内（即各为对方的子集）时则相等。 一个集合当且仅当其为另一个集合的真子集（即为后者的子集但两者不相等）时则小于另一个集合。 一个集合当且仅当其为另一个集合的真超集（即为后者的超集但两者不相等）时则大于另一个集合。

set 的实例与 frozenset 的实例之间基于它们的成员进行比较。 例如 set('abc') == frozenset('abc') 返回 True，set('abc') in set([frozenset('abc')]) 也一样。

子集与相等比较并不能推广为完全排序函数。 例如，任意两个非空且不相交的集合不相等且互不为对方的子集，因此以下 所有 比较均返回 False: a<b, a==b, or a>b。

由于集合仅定义了部分排序（子集关系），因此由集合构成的列表 list.sort() 方法的输出并无定义。

集合的元素，与字典的键类似，必须为 hashable。

混合了 set 实例与 frozenset 的二进制位运算将返回与第一个操作数相同的类型。例如: frozenset('ab') | set('bc') 将返回 frozenset 的实例。

下表列出了可用于 set 而不能用于不可变的 frozenset 实例的操作：

update(*others)

set |= other | ...

更新集合，添加来自 others 中的所有元素。

intersection_update(*others)

set &= other & ...

更新集合，只保留其中在所有 others 中也存在的元素。

difference_update(*others)

set -= other | ...

更新集合，移除其中也存在于 others 中的元素。

symmetric_difference_update(other)

set ^= other

更新集合，只保留存在于集合的一方而非共同存在的元素。

add(elem)

将元素 elem 添加到集合中。

remove(elem)

从集合中移除元素 elem。 如果 elem 不存在于集合中则会引发 KeyError。

discard(elem)

如果元素 elem 存在于集合中则将其移除。

pop()

从集合中移除并返回任意一个元素。 如果集合为空则会引发 KeyError。

clear()

从集合中移除所有元素。

请注意，非运算符版本的 update(), intersection_update(), difference_update() 和 symmetric_difference_update() 方法将接受任意可迭代对象作为参数。

请注意，__contains__(), remove() 和 discard() 方法的 elem 参数可能是一个 set。 为支持对一个等价的 frozenset 进行搜索，会根据 elem 临时创建一个该类型对象。

4.10. 映射类型 — dict

mapping 对象会将 hashable 值映射到任意对象。 映射属于可变对象。 目前仅有一种标准映射类型 字典。 （关于其他容器对象请参看 list, set 与 tuple 等内置类，以及 collections 模块。）

字典的键 几乎 可以是任何值。 非 hashable 的值，即包含列表、字典或其他可变类型的值（此类对象基于值而非对象标识进行比较）不可用作键。 数字类型用作键时遵循数字比较的一般规则：如果两个数值相等 (例如 1 和 1.0) 则两者可以被用来索引同一字典条目。 （但是请注意，由于计算机对于浮点数存储的只是近似值，因此将其用作字典键是不明智的。）

字典可以通过将以逗号分隔的 键: 值 对列表包含于花括号之内来创建，例如: {'jack': 4098, 'sjoerd': 4127} 或 {4098: 'jack', 4127: 'sjoerd'}，也可以通过 dict 构造器来创建。

class dict(**kwarg)

class dict(mapping, **kwarg)

class dict(iterable, **kwarg)

返回一个新的字典，基于可选的位置参数和可能为空的关键字参数集来初始化。

如果没有给出位置参数，将创建一个空字典。 如果给出一个位置参数并且其属于映射对象，将创建一个具有与映射对象相同键值对的字典。 否则的话，位置参数必须为一个 iterable 对象。 该可迭代对象中的每一项本身必须为一个刚好包含两个元素的可迭代对象。 每一项中的第一个对象将成为新字典的一个键，第二个对象将成为其对应的值。 如果一个键出现一次以上，该键的最后一个值将成为其在新字典中对应的值。

如果给出了关键字参数，则关键字参数及其值会被加入到基于位置参数创建的字典。 如果要加入的键已存在，来自关键字参数的值将替代来自位置参数的值。

作为演示，以下示例返回的字典均等于 {"one": 1, "two": 2, "three": 3}:

>>> a = dict(one=1, two=2, three=3)
>>> b = {'one': 1, 'two': 2, 'three': 3}
>>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d = dict([('two', 2), ('one', 1), ('three', 3)])
>>> e = dict({'three': 3, 'one': 1, 'two': 2})
>>> a == b == c == d == e
True

像第一个例子那样提供关键字参数的方式只能使用有效的 Python 标识符作为键。 其他方式则可使用任何有效的键。

这些是字典所支持的操作（因而自定义的映射类型也应当支持）：

len(d)

返回字典 d 中的项数。

d[key]

返回 d 中以 key 为键的项。 如果映射中不存在 key 则会引发 KeyError。

如果字典的子类定义了方法 __missing__() 并且 key 不存在，则 d[key] 操作将调用该方法并附带键 key 作为参数。 d[key] 随后将返回或引发 __missing__(key) 调用所返回或引发的任何对象或异常。 没有其他操作或方法会发起调用 __missing__()。 如果未定义 __missing__()，则会引发 KeyError。 __missing__() 必须是一个方法；它不能是一个实例变量:

>>> class Counter(dict):
...     def __missing__(self, key):
...         return 0
>>> c = Counter()
>>> c['red']
0
>>> c['red'] += 1
>>> c['red']
1

上面的例子显示了 collections.Counter 实现的部分代码。 还有另一个不同的 __missing__ 方法是由 collections.defaultdict 所使用的。

d[key] = value

将 d[key] 设为 value。

del d[key]

将 d[key] 从 d 中移除。 如果映射中不存在 key 则会引发 KeyError。

key in d

如果 d 中存在键 key 则返回 True，否则返回 False。

key not in d

等价于 not key in d。

iter(d)

返回以字典的键为元素的迭代器。 这是 iter(d.keys()) 的快捷方式。

clear()

移除字典中的所有元素。

copy()

返回原字典的浅拷贝。

classmethod fromkeys(seq[, value])

Create a new dictionary with keys from seq and values set to value.

fromkeys() 属于类方法，会返回一个新字典。 value 默认为 None。

get(key[, default])

如果 key 存在于字典中则返回 key 的值，否则返回 default。 如果 default 未给出则默认为 None，因而此方法绝不会引发 KeyError。

items()

返回由字典项 ((键, 值) 对) 组成的一个新视图。 参见 视图对象文档。

keys()

返回由字典键组成的一个新视图。 参见 视图对象文档。

pop(key[, default])

如果 key 存在于字典中则将其移除并返回其值，否则返回 default。 如果 default 未给出且 key 不存在于字典中，则会引发 KeyError。

popitem()

Remove and return an arbitrary (key, value) pair from the dictionary.

popitem() 适用于对字典进行消耗性的迭代，这在集合算法中经常被使用。 如果字典为空，调用 popitem() 将引发 KeyError。

setdefault(key[, default])

如果字典存在键 key ，返回它的值。如果不存在，插入值为 default 的键 key ，并返回 default 。 default 默认为 None。

update([other])

使用来自 other 的键/值对更新字典，覆盖原有的键。 返回 None。

update() 接受另一个字典对象，或者一个包含键/值对（以长度为二的元组或其他可迭代对象表示）的可迭代对象。 如果给出了关键字参数，则会以其所指定的键/值对更新字典: d.update(red=1, blue=2)。

values()

返回由字典值组成的一个新视图。 参见 视图对象文档。

Dictionaries compare equal if and only if they have the same (key, value) pairs. Order comparisons (‘<’, ‘<=’, ‘>=’, ‘>’) raise TypeError.

参见

types.MappingProxyType 可被用来创建一个 dict 的只读视图。

4.10.1. 字典视图对象

由 dict.keys(), dict.values() 和 dict.items() 所返回的对象是 视图对象。 该对象提供字典条目的一个动态视图，这意味着当字典改变时，视图也会相应改变。

字典视图可以被迭代以产生与其对应的数据，并支持成员检测：

len(dictview)

返回字典中的条目数。

iter(dictview)

返回字典中的键、值或项（以 (键, 值) 为元素的元组表示）的迭代器。

Keys and values are iterated over in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. If keys, values and items views are iterated over with no intervening modifications to the dictionary, the order of items will directly correspond. This allows the creation of (value, key) pairs using zip(): pairs = zip(d.values(), d.keys()). Another way to create the same list is pairs = [(v, k) for (k, v) in d.items()].

在添加或删除字典中的条目期间对视图进行迭代可能引发 RuntimeError 或者无法完全迭代所有条目。

x in dictview

如果 x 是对应字典中存在的键、值或项（在最后一种情况下 x 应为一个 (键, 值) 元组） 则返回 True。

键视图类似于集合，因为其条目不重复且可哈希。 如果所有值都是可哈希的，即 (键, 值) 对也是不重复且可哈希的，那么条目视图也会类似于集合。 （值视图则不被视为类似于集合，因其条目通常都是有重复的。） 对于类似于集合的视图，为抽象基类 collections.abc.Set 所定义的全部操作都是有效的 (例如 ==, < 或 ^)。

一个使用字典视图的示例:

>>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500}
>>> keys = dishes.keys()
>>> values = dishes.values()

>>> # iteration
>>> n = 0
>>> for val in values:
...     n += val
>>> print(n)
504

>>> # keys and values are iterated over in the same order
>>> list(keys)
['eggs', 'bacon', 'sausage', 'spam']
>>> list(values)
[2, 1, 1, 500]

>>> # view objects are dynamic and reflect dict changes
>>> del dishes['eggs']
>>> del dishes['sausage']
>>> list(keys)
['spam', 'bacon']

>>> # set operations
>>> keys & {'eggs', 'bacon', 'salad'}
{'bacon'}
>>> keys ^ {'sausage', 'juice'}
{'juice', 'sausage', 'bacon', 'spam'}

4.11. 上下文管理器类型

Python 的 with 语句支持通过上下文管理器所定义的运行时上下文这一概念。 此对象的实现使用了一对专门方法，允许用户自定义类来定义运行时上下文，在语句体被执行前进入该上下文，并在语句执行完毕时退出该上下文：

contextmanager.__enter__()

Enter the runtime context and return either this object or another object related to the runtime context. The value returned by this method is bound to the identifier in the as clause of with statements using this context manager.

一个返回其自身的上下文管理器的例子是 file object。 文件对象会从 __enter__() 返回其自身，以允许 open() 被用作 with 语句中的上下文表达式。

An example of a context manager that returns a related object is the one returned by decimal.localcontext(). These managers set the active decimal context to a copy of the original decimal context and then return the copy. This allows changes to be made to the current decimal context in the body of the with statement without affecting code outside the with statement.

contextmanager.__exit__(exc_type, exc_val, exc_tb)

退出运行时上下文并返回一个布尔值旗标来表明所发生的任何异常是否应当被屏蔽。 如果在执行 with 语句的语句体期间发生了异常，则参数会包含异常的类型、值以及回溯信息。 在其他情况下三个参数均为 None。

Returning a true value from this method will cause the with statement to suppress the exception and continue execution with the statement immediately following the with statement. Otherwise the exception continues propagating after this method has finished executing. Exceptions that occur during execution of this method will replace any exception that occurred in the body of the with statement.

传入的异常绝对不应当被显式地重新引发 —— 相反地，此方法应当返回一个假值以表明方法已成功完成并且不希望屏蔽被引发的异常。 这允许上下文管理代码方便地检测 __exit__() 方法是否确实已失败。

Python 定义了一些上下文管理器来支持简易的线程同步、文件或其他对象的快速关闭，以及更方便地操作活动的十进制算术上下文。 除了实现上下文管理协议以外，不同类型不会被特殊处理。 请参阅 contextlib 模块查看相关的示例。

Python 的 generator 和 contextlib.contextmanager 装饰器提供了实现这些协议的便捷方式。 如果使用 contextlib.contextmanager 装饰器来装饰一个生成器函数，它将返回一个实现了必要的 __enter__() and __exit__() 方法的上下文管理器，而不再是由未经装饰的生成器函数所产生的迭代器。

请注意，Python/C API 中 Python 对象的类型结构中并没有针对这些方法的专门槽位。 想要定义这些方法的扩展类型必须将它们作为普通的 Python 可访问方法来提供。 与设置运行时上下文的开销相比，单个类字典查找的开销可以忽略不计。

4.12. 其他内置类型

解释器支持一些其他种类的对象。 这些对象大都仅支持一两种操作。

4.12.1. 模块

模块唯一的特殊操作是属性访问: m.name，这里 m 为一个模块而 name 访问定义在 m 的符号表中的一个名称。 模块属性可以被赋值。 （请注意 import 语句严格来说也是对模块对象的一种操作；import foo 不要求存在一个名为 foo 的模块对象，而是要求存在一个对于名为 foo 的模块的 (永久性) 定义。）

每个模块都有一个特殊属性 __dict__。 这是包含模块的符号表的字典。 修改此字典将实际改变模块的符号表，但是无法直接对 __dict__ 赋值 (你可以写 m.__dict__['a'] = 1，这会将 m.a 定义为 1，但是你不能写 m.__dict__ = {})。 不建议直接修改 __dict__。

内置于解释器中的模块会写成这样: <module 'sys' (built-in)>。 如果是从一个文件加载，则会写成 <module 'os' from '/usr/local/lib/pythonX.Y/os.pyc'>。

4.12.2. 类与类实例

关于这些类型请参阅 对象、值与类型 和 类定义。

4.12.3. 函数

函数对象是通过函数定义创建的。 对函数对象的唯一操作是调用它: func(argument-list)。

实际上存在两种不同的函数对象：内置函数和用户自定义函数。 两者支持同样的操作（调用函数），但实现方式不同，因此对象类型也不同。

更多信息请参阅 函数定义。

4.12.4. 方法

方法是使用属性表示法来调用的函数。 存在两种形式：内置方法（例如列表的 append() 方法）和类实例方法。 内置方法由支持它们的类型来描述。

如果你通过一个实例来访问方法（即定义在类命名空间内的函数），你会得到一个特殊对象: 绑定方法 (或称 实例方法) 对象。 当被调用时，它会将 self 参数添加到参数列表。 绑定方法具有两个特殊的只读属性: m.__self__ 操作该方法的对象，而 m.__func__ 是实现该方法的函数。 调用 m(arg-1, arg-2, ..., arg-n) 完全等价于调用 m.__func__(m.__self__, arg-1, arg-2, ..., arg-n)。

与函数对象类似，绑定方法对象也支持获取任意属性。 但是，由于方法属性实际上保存于下层的函数对象中 (meth.__func__)，因此不允许设置绑定方法的方法属性。 尝试设置方法的属性将会导致引发 AttributeError。 想要设置方法属性，你必须在下层的函数对象中显式地对其进行设置:

>>> class C:
...     def method(self):
...         pass
...
>>> c = C()
>>> c.method.whoami = 'my name is method'  # can't set on the method
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'method' object has no attribute 'whoami'
>>> c.method.__func__.whoami = 'my name is method'
>>> c.method.whoami
'my name is method'

更多信息请参阅 标准类型层级结构。

4.12.5. 代码对象

代码对象被具体实现用来表示“伪编译”的可执行 Python 代码，例如一个函数体。 它们不同于函数对象，因为它们不包含对其全局执行环境的引用。 代码对象由内置的 compile() 函数返回，并可通过从函数对象的 __code__ 属性从中提取。 另请参阅 code 模块。

可以通过将代码对象（而非源码字符串）传给 exec() 或 eval() 内置函数来执行或求值。

更多信息请参阅 标准类型层级结构。

4.12.6. 类型对象

类型对象表示各种对象类型。 对象的类型可通过内置函数 type() 来获取。 类型没有特殊的操作。 标准库模块 types 定义了所有标准内置类型的名称。

类型以这样的写法来表示: <class 'int'>。

4.12.7. 空对象

此对象会由不显式地返回值的函数所返回。 它不支持任何特殊的操作。 空对象只有一种值 None (这是个内置名称)。 type(None)() 会生成同一个单例。

该对象的写法为 None。

4.12.8. 省略符对象

此对象常被用于切片 (参见 切片)。 它不支持任何特殊的操作。 省略符对象只有一种值 Ellipsis (这是个内置名称)。 type(Ellipsis)() 会生成 Ellipsis 单例。

该对象的写法为 Ellipsis 或 ...。

4.12.9. 未实现对象

此对象会被作为比较和二元运算被应用于它们所不支持的类型时的返回值。 请参阅 比较运算 了解更多信息。 未实现对象只有一种值 NotImplemented。 type(NotImplemented)() 会生成这个单例。

该对象的写法为 NotImplemented。

4.12.10. 布尔值

布尔值是两个常量对象 False 和 True。 它们被用来表示逻辑上的真假（不过其他值也可被当作真值或假值）。 在数字类的上下文中（例如被用作算术运算符的参数时），它们的行为分别类似于整数 0 和 1。 内置函数 bool() 可被用来将任意值转换为布尔值，只要该值可被解析为一个逻辑值（参见之前的 逻辑值检测 部分）。

该对象的写法分别为 False 和 True。

4.12.11. 内部对象

有关此对象的信息请参阅 标准类型层级结构。 其中描述了栈帧对象、回溯对象以及切片对象等等。

4.13. 特殊属性

语言实现为部分对象类型添加了一些特殊的只读属性，它们具有各自的作用。 其中一些并不会被 dir() 内置函数所列出。

object.__dict__

一个字典或其他类型的映射对象，用于存储对象的（可写）属性。

instance.__class__

类实例所属的类。

class.__bases__

由类对象的基类所组成的元组。

definition.__name__

类、函数、方法、描述器或生成器实例的名称。

definition.__qualname__

类、函数、方法、描述器或生成器实例的 qualified name。

3.3 新版功能.

class.__mro__

此属性是由类组成的元组，在方法解析期间会基于它来查找基类。

class.mro()

此方法可被一个元类来重载，以为其实例定制方法解析顺序。 它会在类实例化时被调用，其结果存储于 __mro__ 之中。

class.__subclasses__()

每个类会保存由对其直接子类的弱引用组成的列表。 此方法将返回一个由仍然存在的所有此类引用组成的列表。 例如:

>>> int.__subclasses__()
[<class 'bool'>]

备注

1

有关这些特殊方法的额外信息可参看 Python 参考指南 (基本定制)。

2

作为结果，列表 [1, 2] 与 [1.0, 2.0] 是相等的，元组的情况也类似。

3

它们必须如此，因为解析器无法区分这些操作数的类型。

4(1,2,3,4)

区分大小写的字符是指所属一般类别属性为 “Lu” (Letter, uppercase), “Ll” (Letter, lowercase) 或 “Lt” (Letter, titlecase) 之一的字符。

5(1,2)

要格式化单独一个元组，那么你应当提供一个单例元组，其唯一的元素就是要被格式化的元组。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

5. 内置异常

在 Python 中，所有异常必须为一个派生自 BaseException 的类的实例。 在带有提及一个特定类的 except 子句的 try 语句中，该子句也会处理任何派生自该类的异常类（但不处理 它 所派生出的异常类）。 通过子类化创建的两个不相关异常类永远是不等效的，既使它们具有相同的名称。

下面列出的内置异常可通过解释器或内置函数来生成。除非另有说明，它们都会具有一个提示导致错误详细原因的“关联值”。 这可以是一个字符串或由多个信息项（例如一个错误码和一个解释错误的字符串）组成的元组。 关联值通常会作为参数被传递给异常类的构造器。

用户代码可以引发内置异常。 这可被用于测试异常处理程序或报告错误条件，“就像” 在解释器引发了相同异常的情况时一样；但是请注意，没有任何机制能防止用户代码引发不适当的错误。

内置异常类可以被子类化以定义新的异常；鼓励程序员从 Exception 类或它的某个子类而不是从 BaseException 来派生新的异常。 关于定义异常的更多信息可以在 Python 教程的 用户自定义异常 部分查看。

当在 except 或 finally 子句中引发（或重新引发）异常时，__context__ 会被自动设为所捕获的最后一个异常；如果新的异常未被处理，则最终显示的回溯信息将包括原始的异常和最后的异常。

当引发一个新的异常（而不是简单地使用 raise 来重新引发当前在处理的异常）时，隐式的异常上下文可以通过使用带有 raise 的 from 来补充一个显式的原因:

raise new_exc from original_exc

跟在 from 之后的表达式必须为一个异常或 None。 它将在所引发的异常上被设置为 __cause__。 设置 __cause__ 还会隐式地将 __suppress_context__ 属性设为 True，这样使用 raise new_exc from None 可以有效地将旧异常替换为新异常来显示其目的 (例如将 KeyError 转换为 AttributeError)，同时让旧异常在 __context__ 中保持可用状态以便在调试时进行内省。

除了异常本身的回溯以外，默认的回溯还会显示这些串连的异常。 __cause__ 中的显式串连异常如果存在将总是显示。 __context__ 中的隐式串连异常仅在 __cause__ 为 None 并且 __suppress_context__ 为假值时显示。

不论在哪种情况下，异常本身总会在任何串连异常之后显示，以便回溯的最后一行总是显示所引发的最后一个异常。

5.1. 基类

下列异常主要被用作其他异常的基类。

exception BaseException

所有内置异常的基类。 它不应该被用户自定义类直接继承 (这种情况请使用 Exception)。 如果在此类的实例上调用 str()，则会返回实例的参数表示，或者当没有参数时返回空字符串。

args

传给异常构造器的参数元组。 某些内置异常 (例如 OSError) 接受特定数量的参数并赋予此元组中的元素特殊的含义，而其他异常通常只接受一个给出错误信息的单独字符串。

with_traceback(tb)

此方法将 tb 设为异常的新回溯信息并返回该异常对象。 它通常以如下的形式在异常处理程序中使用:

try:
    ...
except SomeException:
    tb = sys.exc_info()[2]
    raise OtherException(...).with_traceback(tb)

exception Exception

所有内置的非系统退出类异常都派生自此类。 所有用户自定义异常也应当派生自此类。

exception ArithmeticError

此基类用于派生针对各种算术类错误而引发的内置异常: OverflowError, ZeroDivisionError, FloatingPointError。

exception BufferError

当与 缓冲区 相关的操作无法执行时将被引发。

exception LookupError

此基类用于派生当映射或序列所使用的键或索引无效时引发的异常: IndexError, KeyError。 这可以通过 codecs.lookup() 来直接引发。

5.2. 具体异常

以下异常属于经常被引发的异常。

exception AssertionError

当 assert 语句失败时将被引发。

exception AttributeError

当属性引用 (参见 属性引用) 或赋值失败时将被引发。 （当一个对象根本不支持属性引用或属性赋值时则将引发 TypeError。）

exception EOFError

当 input() 函数未读取任何数据即达到文件结束条件 (EOF) 时将被引发。 （另外，io.IOBase.read() 和 io.IOBase.readline() 方法在遇到 EOF 则将返回一个空字符串。）

exception FloatingPointError

Raised when a floating point operation fails. This exception is always defined, but can only be raised when Python is configured with the --with-fpectl option, or the WANT_SIGFPE_HANDLER symbol is defined in the pyconfig.h file.

exception GeneratorExit

当一个 generator 或 coroutine 被关闭时将被引发；参见 generator.close() 和 coroutine.close()。 它直接继承自 BaseException 而不是 Exception，因为从技术上来说它并不是一个错误。

exception ImportError

当 import 语句尝试加载模块遇到麻烦时将被引发。 并且当 from ... import 中的 “from list” 存在无法找到的名称时也会被引发。

name 与 path 属性可通过对构造器使用仅关键字参数来设定。 设定后它们将分别表示被尝试导入的模块名称与触发异常的任意文件所在路径。

在 3.3 版更改: 添加了 name 与 path 属性。

exception ModuleNotFoundError

ImportError 的子类，当一个模块无法被定位时将由 import 引发。 当在 sys.modules 中找到 None 时也会被引发。

3.6 新版功能.

exception IndexError

当序列抽取超出范围时将被引发。 （切片索引会被静默截短到允许的范围；如果指定索引不是整数则 TypeError 会被引发。）

exception KeyError

当在现有键集合中找不到指定的映射（字典）键时将被引发。

exception KeyboardInterrupt

当用户按下中断键 (通常为 Control-C 或 Delete) 时将被引发。 在执行期间，会定期检测中断信号。 该异常继承自 BaseException 以确保不会被处理 Exception 的代码意外捕获，这样可以避免退出解释器。

exception MemoryError

当一个操作耗尽内存但情况仍可（通过删除一些对象）进行挽救时将被引发。 关联的值是一个字符串，指明是哪种（内部）操作耗尽了内存。 请注意由于底层的内存管理架构（C 的 malloc() 函数），解释器也许并不总是能够从这种情况下完全恢复；但它毕竟可以引发一个异常，这样就能打印出栈回溯信息，以便找出导致问题的失控程序。

exception NameError

当某个局部或全局名称未找到时将被引发。 此异常仅用于非限定名称。 关联的值是一条错误信息，其中包含未找到的名称。

exception NotImplementedError

此异常派生自 RuntimeError。 在用户自定义的基类中，抽象方法应当在其要求所派生类重载该方法，或是在其要求所开发的类提示具体实现尚待添加时引发此异常。

注解

它不应当用来表示一个运算符或方法根本不能被支持 – 在此情况下应当让特定运算符 / 方法保持未定义，或者在子类中将其设为 None。

注解

NotImplementedError 和 NotImplemented 不可互换，即使它们有相似的名称和用途。 请参阅 NotImplemented 了解有关何时使用它们的详细说明。

exception OSError([arg])

exception OSError(errno, strerror[, filename[, winerror[, filename2]]])

此异常在一个系统函数返回系统相关的错误时将被引发，此类错误包括 I/O 操作失败例如 “文件未找到” 或 “磁盘已满” 等（不包括非法参数类型或其他偶然性错误）。

构造器的第二种形式可设置如下所述的相应属性。 如果未指定这些属性则默认为 None。 为了能向下兼容，如果传入了三个参数，则 args 属性将仅包含由前两个构造器参数组成的 2 元组。

构造器实际返回的往往是 OSError 的某个子类，如下文 OS exceptions 中所描述的。 具体的子类取决于最终的 errno 值。 此行为仅在直接或通过别名来构造 OSError 时发生，并且在子类化时不会被继承。

errno

来自于 C 变量 errno 的数字错误码。

winerror

在 Windows 下，此参数将给出原生的 Windows 错误码。 而 errno 属性将是该原生错误码在 POSIX 平台下的近似转换形式。

在 Windows 下，如果 winerror 构造器参数是一个整数，则 errno 属性会根据 Windows 错误码来确定，而 errno 参数会被忽略。 在其他平台上，winerror 参数会被忽略，并且 winerror 属性将不存在。

strerror

操作系统所提供的相应错误信息。 它在 POSIX 平台中由 C 函数 perror() 来格式化，在 Windows 中则是由 FormatMessage()。

filename

filename2

对于与文件系统路径有关 (例如 open() 或 os.unlink()) 的异常，filename 是传给函数的文件名。 对于涉及两个文件系统路径的函数 (例如 os.rename())，filename2 将是传给函数的第二个文件名。

在 3.3 版更改: EnvironmentError, IOError, WindowsError, socket.error, select.error 与 mmap.error 已被合并到 OSError，构造器可能返回其中一个子类。

在 3.4 版更改: filename 属性现在将是传给函数的原始文件名，而不是经过编码或基于文件系统编码进行解码之后的名称。 此外还添加了 filename2 构造器参数和属性。

exception OverflowError

当算术运算的结果大到无法表示时将被引发。 这对整数来说不可能发生（宁可引发 MemoryError 也不会放弃尝试）。 但是出于历史原因，有时也会在整数超出要求范围的情况下引发 OverflowError。 因为在 C 中缺少对浮点异常处理的标准化，大多数浮点运算都不会做检查。

exception RecursionError

此异常派生自 RuntimeError。 它会在解释器检测发现超过最大递归深度 (参见 sys.getrecursionlimit()) 时被引发。

3.5 新版功能: 在此之前将只引发 RuntimeError。

exception ReferenceError

此异常将在使用 weakref.proxy() 函数所创建的弱引用来访问该引用的某个已被作为垃圾回收的属性时被引发。 有关弱引用的更多信息请参阅 weakref 模块。

exception RuntimeError

当检测到一个不归属于任何其他类别的错误时将被引发。 关联的值是一个指明究竟发生了什么问题的字符串。

exception StopIteration

由内置函数 next() 和 iterator 的 __next__() 方法所引发，用来表示该迭代器不能产生下一项。

该异常对象只有一个属性 value，它在构造该异常时作为参数给出，默认值为 None。

当一个 generator 或 coroutine 函数返回时，将引发一个新的 StopIteration 实例，函数返回的值将被用作异常构造器的 value 形参。

If a generator function defined in the presence of a from __future__ import generator_stop directive raises StopIteration, it will be converted into a RuntimeError (retaining the StopIteration as the new exception’s cause).

在 3.3 版更改: 添加了 value 属性及其被生成器函数用作返回值的功能。

在 3.5 版更改: Introduced the RuntimeError transformation.

exception StopAsyncIteration

必须由一个 asynchronous iterator 对象的 __anext__() 方法来引发以停止迭代操作。

3.5 新版功能.

exception SyntaxError

当解析器遇到语法错误时将被引发。 这可以发生在 import 语句，对内置函数 exec() 或 eval() 的调用，或者读取原始脚本或标准输入（也包括交互模式）的时候。

该类的实例包含有属性 filename, lineno, offset 和 text 用于方便地访问相应的详细信息。 异常实例的 str() 仅返回消息文本。

exception IndentationError

与不正确的缩进相关的语法错误的基类。 这是 SyntaxError 的一个子类。

exception TabError

当缩进包含对制表符和空格符不一致的使用时将被引发。 这是 IndentationError 的一个子类。

exception SystemError

当解释器发现内部错误，但情况看起来尚未严重到要放弃所有希望时将被引发。 关联的值是一个指明发生了什么问题的字符串（表示为低层级的符号）。

你应当将此问题报告给你所用 Python 解释器的作者或维护人员。 请确认报告 Python 解释器的版本号 (sys.version; 它也会在交互式 Python 会话开始时被打印出来)，具体的错误消息（异常所关联的值）以及可能触发该错误的程序源码。

exception SystemExit

此异常由 sys.exit() 函数引发。 它继承自 BaseException 而不是 Exception 以确保不会被处理 Exception 的代码意外捕获。 这允许此异常正确地向上传播并导致解释器退出。 如果它未被处理，则 Python 解释器就将退出；不会打印任何栈回溯信息。 构造器接受的可选参数与传递给 sys.exit() 的相同。 如果该值为一个整数，则它指明系统退出状态码（会传递给 C 的 exit() 函数）；如果该值为 None，则退出状态码为零；如果该值为其他类型（例如字符串），则会打印对象的值并将退出状态码设为一。

对 sys.exit() 的调用会被转换为一个异常以便能执行清理处理程序 (try 语句的 finally 子句)，并且使得调试器可以执行一段脚本而不必冒失去控制的风险。 如果绝对确实地需要立即退出（例如在调用 os.fork() 之后的子进程中）则可使用 os._exit().

code

传给构造器的退出状态码或错误信息（默认为 None。）

exception TypeError

当一个操作或函数被应用于类型不适当的对象时将被引发。 关联的值是一个字符串，给出有关类型不匹配的详情。

此异常可以由用户代码引发，以表明尝试对某个对象进行的操作不受支持也不应当受支持。 如果某个对象应当支持给定的操作但尚未提供相应的实现，所要引发的适当异常应为 NotImplementedError。

传入参数的类型错误 (例如在要求 int 时却传入了 list) 应当导致 TypeError，但传入参数的值错误 (例如传入要求范围之外的数值) 则应当导致 ValueError。

exception UnboundLocalError

当在函数或方法中对某个局部变量进行引用，但该变量并未绑定任何值时将被引发。 此异常是 NameError 的一个子类。

exception UnicodeError

当发生与 Unicode 相关的编码或解码错误时将被引发。 此异常是 ValueError 的一个子类。

UnicodeError 具有一些描述编码或解码错误的属性。 例如 err.object[err.start:err.end] 会给出导致编解码器失败的特定无效输入。

encoding

引发错误的编码名称。

reason

描述特定编解码器错误的字符串。

object

编解码器试图要编码或解码的对象。

start

object 中无效数据的开始位置索引。

end

object 中无效数据的末尾位置索引（不含）。

exception UnicodeEncodeError

当在编码过程中发生与 Unicode 相关的错误时将被引发。 此异常是 UnicodeError 的一个子类。

exception UnicodeDecodeError

当在解码过程中发生与 Unicode 相关的错误时将被引发。 此异常是 UnicodeError 的一个子类。

exception UnicodeTranslateError

在转写过程中发生与 Unicode 相关的错误时将被引发。 此异常是 UnicodeError 的一个子类。

exception ValueError

当操作或函数接收到具有正确类型但值不适合的参数，并且情况不能用更精确的异常例如 IndexError 来描述时将被引发。

exception ZeroDivisionError

当除法或取余运算的第二个参数为零时将被引发。 关联的值是一个字符串，指明操作数和运算的类型。

下列异常被保留以与之前的版本相兼容；从 Python 3.3 开始，它们都是 OSError 的别名。

exception EnvironmentError

exception IOError

exception WindowsError

限在 Windows 中可用。

5.2.1. OS 异常

下列异常均为 OSError 的子类，它们将根据系统错误代码被引发。

exception BlockingIOError

当一个操作会被某个设置为非阻塞操作的对象（例如套接字）所阻塞时将被引发。 对应于 errno EAGAIN, EALREADY, EWOULDBLOCK 和 EINPROGRESS。

除了 OSError 已有的属性，BlockingIOError 还有一个额外属性：

characters_written

一个整数，表示在被阻塞前已写入到流的字符数。 当使用来自 io 模块的带缓冲 I/O 类时此属性可用。

exception ChildProcessError

当一个子进程上的操作失败时将被引发。 对应于 errno ECHILD。

exception ConnectionError

与连接相关问题的基类。

其子类有 BrokenPipeError, ConnectionAbortedError, ConnectionRefusedError 和 ConnectionResetError。

exception BrokenPipeError

ConnectionError 的子类，当试图写入另一端已被关闭的管道，或是试图写入已关闭写入的套接字时将被引发。 对应于 errno EPIPE 和 ESHUTDOWN。

exception ConnectionAbortedError

ConnectionError 的子类，当连接尝试被对端中止时将被引发。 对应于 errno ECONNABORTED。

exception ConnectionRefusedError

ConnectionError 的子类，当连接尝试被对端拒绝时将被引发。 对应于 errno ECONNREFUSED。

exception ConnectionResetError

ConnectionError 的子类，当连接被对端重置时将被引发。 对应于 errno ECONNRESET。

exception FileExistsError

当试图创建一个已存在的文件或目录时将被引发。 对应于 errno EEXIST。

exception FileNotFoundError

当所请求的文件或目录不存在时将被引发。 对应于 errno ENOENT。

exception InterruptedError

当系统调用被输入信号中断时将被引发。 对应于 errno EINTR。

在 3.5 版更改: 当系统调用被某个信号中断时，Python 现在会重试系统调用，除非该信号的处理程序引发了其它异常 (原理参见 PEP 475 [https://www.python.org/dev/peps/pep-0475]) 而不是引发 InterruptedError。

exception IsADirectoryError

当请求对一个目录执行文件操作 (例如 os.remove()) 将被引发。 对应于 errno EISDIR。

exception NotADirectoryError

当请求对一个非目录对象执行目录操作 (例如 os.listdir()) 时将被引发。 对应于 errno ENOTDIR。

exception PermissionError

当在没有足够操作权限的情况下试图执行某个操作时将被引发 —— 例如缺少文件系统权限。 对应于 errno EACCES 和 EPERM。

exception ProcessLookupError

当给定的进程不存在时将被引发。 对应于 errno ESRCH。

exception TimeoutError

当一个系统函数发生系统级超时的情况下将被引发。 对应于 errno ETIMEDOUT。

3.3 新版功能: 添加了以上所有 OSError 的子类。

参见

PEP 3151 [https://www.python.org/dev/peps/pep-3151] - 重写 OS 和 IO 异常的层次结构

5.3. 警告

The following exceptions are used as warning categories; see the warnings module for more information.

exception Warning

警告类别的基类。

exception UserWarning

用户代码所产生警告的基类。

exception DeprecationWarning

Base class for warnings about deprecated features.

exception PendingDeprecationWarning

Base class for warnings about features which will be deprecated in the future.

exception SyntaxWarning

与模糊的语法相关的警告的基类。

exception RuntimeWarning

与模糊的运行时行为相关的警告的基类。

exception FutureWarning

Base class for warnings about constructs that will change semantically in the future.

exception ImportWarning

与在模块导入中可能的错误相关的警告的基类。

exception UnicodeWarning

与 Unicode 相关的警告的基类。

exception BytesWarning

与 bytes 和 bytearray 相关的警告的基类。

exception ResourceWarning

Base class for warnings related to resource usage.

3.2 新版功能.

5.4. 异常层次结构

内置异常的类层级结构如下：

BaseException
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- GeneratorExit
 +-- Exception
      +-- StopIteration
      +-- StopAsyncIteration
      +-- ArithmeticError
      |    +-- FloatingPointError
      |    +-- OverflowError
      |    +-- ZeroDivisionError
      +-- AssertionError
      +-- AttributeError
      +-- BufferError
      +-- EOFError
      +-- ImportError
      |    +-- ModuleNotFoundError
      +-- LookupError
      |    +-- IndexError
      |    +-- KeyError
      +-- MemoryError
      +-- NameError
      |    +-- UnboundLocalError
      +-- OSError
      |    +-- BlockingIOError
      |    +-- ChildProcessError
      |    +-- ConnectionError
      |    |    +-- BrokenPipeError
      |    |    +-- ConnectionAbortedError
      |    |    +-- ConnectionRefusedError
      |    |    +-- ConnectionResetError
      |    +-- FileExistsError
      |    +-- FileNotFoundError
      |    +-- InterruptedError
      |    +-- IsADirectoryError
      |    +-- NotADirectoryError
      |    +-- PermissionError
      |    +-- ProcessLookupError
      |    +-- TimeoutError
      +-- ReferenceError
      +-- RuntimeError
      |    +-- NotImplementedError
      |    +-- RecursionError
      +-- SyntaxError
      |    +-- IndentationError
      |         +-- TabError
      +-- SystemError
      +-- TypeError
      +-- ValueError
      |    +-- UnicodeError
      |         +-- UnicodeDecodeError
      |         +-- UnicodeEncodeError
      |         +-- UnicodeTranslateError
      +-- Warning
           +-- DeprecationWarning
           +-- PendingDeprecationWarning
           +-- RuntimeWarning
           +-- SyntaxWarning
           +-- UserWarning
           +-- FutureWarning
           +-- ImportWarning
           +-- UnicodeWarning
           +-- BytesWarning
           +-- ResourceWarning

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

6. 文本处理服务

本章介绍的模块提供了广泛的字符串操作和其他文本处理服务。

在 二进制数据服务 之下描述的 codecs 模块也与文本处理高度相关。 此外也请参阅 Python 内置字符串类型的文档 文本序列类型 — str。

6.1. string — 常见的字符串操作

6.1.1. 字符串常量

6.1.2. 自定义字符串格式化

6.1.3. 格式字符串语法

6.1.3.1. 格式规格迷你语言

6.1.3.2. 格式示例

6.1.4. 模板字符串

6.1.5. 辅助函数

6.2. re — 正则表达式操作

6.2.1. 正则表达式语法

6.2.2. 模块内容

6.2.3. 正则表达式对象 （正则对象）

6.2.4. 匹配对象

6.2.5. 正则表达式例子

6.2.5.1. 检查对子

6.2.5.2. 模拟 scanf()

6.2.5.3. search() vs. match()

6.2.5.4. 建立一个电话本

6.2.5.5. 文字整理

6.2.5.6. 找到所有副词

6.2.5.7. 找到所有副词和位置

6.2.5.8. 原始字符记法

6.2.5.9. 写一个词法分析器

6.3. difflib — 计算差异的辅助工具

6.3.1. SequenceMatcher 对象

6.3.2. SequenceMatcher 的示例

6.3.3. Differ 对象

6.3.4. Differ 示例

6.3.5. difflib 的命令行接口

6.4. textwrap — 文本自动换行与填充

6.5. unicodedata — Unicode 数据库

6.6. stringprep — 因特网字符串预备

6.7. readline — GNU readline 接口

6.7.1. 初始化文件

6.7.2. 行缓冲区

6.7.3. 历史文件

6.7.4. 历史列表

6.7.5. 启动钩子

6.7.6. Completion

6.7.7. 示例

6.8. rlcompleter — GNU readline 的补全函数

6.8.1. Completer 对象

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

6.1. string — 常见的字符串操作

源代码： Lib/string.py [https://github.com/python/cpython/tree/3.6/Lib/string.py]

参见

文本序列类型 — str

字符串的方法

6.1.1. 字符串常量

此模块中定义的常量为：

string.ascii_letters

下文所述 ascii_lowercase 和 ascii_uppercase 常量的拼连。 该值不依赖于语言区域。

string.ascii_lowercase

小写字母 'abcdefghijklmnopqrstuvwxyz'。 该值不依赖于语言区域，不会发生改变。

string.ascii_uppercase

大写字母 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'。 该值不依赖于语言区域，不会发生改变。

string.digits

字符串 '0123456789'。

string.hexdigits

字符串 '0123456789abcdefABCDEF'。

string.octdigits

字符串 '01234567'。

string.punctuation

由在 C 语言区域中被视为标点符号的 ASCII 字符组成的字符串。

string.printable

由被视为可打印符号的 ASCII 字符组成的字符串。 这是 digits, ascii_letters, punctuation 和 whitespace 的总和。

string.whitespace

由被视为空白符号的 ASCII 字符组成的字符串。 其中包括空格、制表、换行、回车、进纸和纵向制表符。

6.1.2. 自定义字符串格式化

内置的字符串类提供了通过使用 PEP 3101 [https://www.python.org/dev/peps/pep-3101] 所描述的 format() 方法进行复杂变量替换和值格式化的能力。 string 模块中的 Formatter 类允许你使用与内置 format() 方法相同的实现来创建并定制你自己的字符串格式化行为。

class string.Formatter

Formatter 类包含下列公有方法：

format(format_string, *args, **kwargs)

首要的 API 方法。 它接受一个格式字符串和任意一组位置和关键字参数。 它只是一个调用 vformat() 的包装器。

3.5 版后已移除: Passing a format string as keyword argument format_string has been deprecated.

vformat(format_string, args, kwargs)

此函数执行实际的格式化操作。 它被公开为一个单独的函数，用于需要传入一个预定义字母作为参数，而不是使用 *args 和 **kwargs 语法将字典解包为多个单独参数并重打包的情况。 vformat() 完成将格式字符串分解为字符数据和替换字段的工作。 它会调用下文所述的几种不同方法。

此外，Formatter 还定义了一些旨在被子类替换的方法：

parse(format_string)

循环遍历 format_string 并返回一个由可迭代对象组成的元组 (literal_text, field_name, format_spec, conversion)。 它会被 vformat() 用来将字符串分解为文本字面值或替换字段。

元组中的值在概念上表示一段字面文本加上一个替换字段。 如果没有字面文本（如果连续出现两个替换字段就会发生这种情况），则 literal_text 将是一个长度为零的字符串。 如果没有替换字段，则 field_name, format_spec 和 conversion 的值将为 None。

get_field(field_name, args, kwargs)

给定 field_name 作为 parse() (见上文) 的返回值，将其转换为要格式化的对象。 返回一个元组 (obj, used_key)。 默认版本接受在 PEP 3101 [https://www.python.org/dev/peps/pep-3101] 所定义形式的字符串，例如 “0[name]” 或 “label.title”。 args 和 kwargs 与传给 vformat() 的一样。 返回值 used_key 与 get_value() 的 key 形参具有相同的含义。

get_value(key, args, kwargs)

提取给定的字段值。 key 参数将为整数或字符串。 如果是整数，它表示 args 中位置参数的索引；如果是字符串，它表示 kwargs 中的关键字参数名。

args 形参会被设为 vformat() 的位置参数列表，而 kwargs 形参会被设为由关键字参数组成的字典。

对于复合字段名称，仅会为字段名称的第一个组件调用这些函数；后续组件会通过普通属性和索引操作来进行处理。

因此举例来说，字段表达式 ‘0.name’ 将导致调用 get_value() 时附带 key 参数值 0。 在 get_value() 通过调用内置的 getattr() 函数返回后将会查找 name 属性。

如果索引或关键字引用了一个不存在的项，则将引发 IndexError 或 KeyError。

check_unused_args(used_args, args, kwargs)

在必要时实现对未使用参数进行检测。 此函数的参数是是格式字符串中实际引用的所有参数键的集合（整数表示位置参数，字符串表示名称参数），以及被传给 vformat 的 args 和 kwargs 的引用。 未使用参数的集合可以根据这些形参计算出来。 如果检测失败则 check_unused_args() 应会引发一个异常。

format_field(value, format_spec)

format_field() 会简单地调用内置全局函数 format()。 提供该方法是为了让子类能够重载它。

convert_field(value, conversion)

使用给定的转换类型（来自 parse() 方法所返回的元组）来转换（由 get_field() 所返回的）值。 默认版本支持 ‘s’ (str), ‘r’ (repr) 和 ‘a’ (ascii) 等转换类型。

6.1.3. 格式字符串语法

str.format() 方法和 Formatter 类共享相同的格式字符串语法（虽然对于 Formatter 来说，其子类可以定义它们自己的格式字符串语法）。 具体语法与 格式化字符串字面值 相似，但也存在区别。

格式字符串包含有以花括号 {} 括起来的“替换字段”。 不在花括号之内的内容被视为字面文本，会不加修改地复制到输出中。 如果你需要在字面文本中包含花括号字符，可以通过重复来转义: {{ and }}。

替换字段的语法如下：

replacement_field ::=  "{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name        ::=  arg_name ("." attribute_name | "[" element_index "]")*
arg_name          ::=  [identifier | digit+]
attribute_name    ::=  identifier
element_index     ::=  digit+ | index_string
index_string      ::=  <any source character except "]"> +
conversion        ::=  "r" | "s" | "a"
format_spec       ::=  <described in the next section>

用不太正式的术语来描述，替换字段开头可以用一个 field_name 指定要对值进行格式化并取代替换字符被插入到输出结果的对象。 field_name 之后有可选的 conversion 字段，它是一个感叹号 '!' 加一个 format_spec，并以一个冒号 ':' 打头。 这些指明了替换值的非默认格式。

另请参阅 格式规格迷你语言 一节。

field_name 本身以一个数字或关键字 arg_name 打头。 如果为数字，则它指向一个位置参数，而如果为关键字，则它指向一个命名关键字参数。 如果格式字符串中的数字 arg_names 为 0, 1, 2, … 的序列，它们可以全部省略（而非部分省略），数字 0, 1, 2, … 将会按顺序自动插入。 由于 arg_name 不使用引号分隔，因此无法在格式字符串中指定任意的字典键 (例如字符串 '10' 或 ':-]')。 arg_name 之后可以带上任意数量的索引或属性表达式。 '.name' 形式的表达式会使用 getattr() 选择命名属性，而 '[index]' 形式的表达式会使用 __getitem__() 执行索引查找。

在 3.1 版更改: 位置参数说明符对于 str.format() 可以省略，因此 '{} {}'.format(a, b) 等价于 '{0} {1}'.format(a, b)。

在 3.4 版更改: 位置参数说明符对于 Formatter 可以省略。

一些简单的格式字符串示例

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.

使用 conversion 字段在格式化之前进行类型强制转换。 通常，格式化值的工作由值本身的 __format__() 方法来完成。 但是，在某些情况下最好强制将类型格式化为一个字符串，覆盖其本身的格式化定义。 通过在调用 __format__() 之前将值转换为字符串，可以绕过正常的格式化逻辑。

目前支持的转换旗标有三种: '!s' 会对值调用 str()，'!r' 调用 repr() 而 '!a' 则调用 ascii()。

几个例子:

"Harold's a clever {0!s}"        # Calls str() on the argument first
"Bring out the holy {name!r}"    # Calls repr() on the argument first
"More {!a}"                      # Calls ascii() on the argument first

format_spec 字段包含值应如何呈现的规格描述，例如字段宽度、对齐、填充、小数精度等细节信息。 每种值类型可以定义自己的“格式化迷你语言”或对 format_spec 的解读方式。

大多数内置类型都支持同样的格式化迷你语言，具体描述见下一节。

format_spec 字段还可以在其内部包含嵌套的替换字段。 这些嵌套的替换字段可能包括字段名称、转换旗标和格式规格描述，但是不再允许更深层的嵌套。 format_spec 内部的替换字段会在解读 format_spec 字符串之前先被解读。 这将允许动态地指定特定值的格式。

请参阅 格式示例 一节查看相关示例。

6.1.3.1. 格式规格迷你语言

“格式规格”在格式字符串所包含的替换字段内部使用，用于定义单个值应如何呈现 (参见 格式字符串语法 和 格式化字符串字面值)。 它们也可以被直接传给内置的 format() 函数。 每种可格式化的类型都可以自行定义如何对格式规格进行解读。

大多数内置类型都为格式规格实现了下列选项，不过某些格式化选项只被数值类型所支持。

A general convention is that an empty format string ("") produces the same result as if you had called str() on the value. A non-empty format string typically modifies the result.

标准格式说明符 的一般形式如下：

format_spec     ::=  [[fill]align][sign][#][0][width][grouping_option][.precision][type]
fill            ::=  <any character>
align           ::=  "<" | ">" | "=" | "^"
sign            ::=  "+" | "-" | " "
width           ::=  digit+
grouping_option ::=  "_" | ","
precision       ::=  digit+
type            ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"

如果指定了一个有效的 align 值，则可以在该值前面加一个 fill 字符，它可以为任意字符，如果省略则默认为空格符。 在 格式化字符串字面值 或在使用 str.format() 方法时是无法使用花括号字面值 (“{” or “}”) 作为 fill 字符的。 但是，通过嵌套替换字段插入花括号则是可以的。 这个限制不会影响 format() 函数。

各种对齐选项的含义如下：

选项

含义

'<'

强制字段在可用空间内左对齐（这是大多数对象的默认值）。

'>'

强制字段在可用空间内右对齐（这是数字的默认值）。

'='

强制将填充放置在符号（如果有）之后但在数字之前。这用于以“+000000120”形式打印字段。此对齐选项仅对数字类型有效。当’0’紧接在字段宽度之前时，它成为默认值。

'^'

强制字段在可用空间内居中。

请注意，除非定义了最小字段宽度，否则字段宽度将始终与填充它的数据大小相同，因此在这种情况下，对齐选项没有意义。

sign 选项仅对数字类型有效，可以是以下之一：

选项

含义

'+'

表示标志应该用于正数和负数。

'-'

表示标志应仅用于负数（这是默认行为）。

space

表示应在正数上使用前导空格，在负数上使用减号。

'#' 选项可以让“替代形式”被用于转换。 替代形式可针对不同类型分别定义。 此选项仅对整数、浮点、复数和 Decimal 类型有效。 对于整数类型，当使用二进制、八进制或十六进制输出时，此选项会为输出值添加相应的 '0b', '0o' 或 '0x' 前缀。 对于浮点数、复数和 Decimal 类型，替代形式会使得转换结果总是包含小数点符号，即使其不带小数。 通常只有在带有小数的情况下，此类转换的结果中才会出现小数点符号。 此外，对于 'g' 和 'G' 转换，末尾的零不会从结果中被移除。

',' 选项表示使用逗号作为千位分隔符。 对于感应区域设置的分隔符，请改用 'n' 整数表示类型。

在 3.1 版更改: 添加了 ',' 选项 (另请参阅 PEP 378 [https://www.python.org/dev/peps/pep-0378])。

'_' 选项表示对浮点表示类型和整数表示类型 'd' 使用下划线作为千位分隔符。 对于整数表示类型 'b', 'o', 'x' 和 'X'，将为每 4 个数位插入一个下划线。 对于其他表示类型指定此选项则将导致错误。

在 3.6 版更改: 添加了 '_' 选项 (另请参阅 PEP 515 [https://www.python.org/dev/peps/pep-0515])。

width is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content.

当未显式给出对齐方式时，在 width 字段前加一个零 ('0') 字段将为数字类型启用感知正负号的零填充。 这相当于设置 fill 字符为 '0' 且 alignment 类型为 '='。

precision 是一个十进制数字，表示对于以 'f' and 'F' 格式化的浮点数值要在小数点后显示多少个数位，或者对于以 'g' 或 'G' 格式化的浮点数值要在小数点前后共显示多少个数位。 对于非数字类型，该字段表示最大字段大小 —— 换句话说就是要使用多少个来自字段内容的字符。 对于整数值则不允许使用 precision。

最后，type 确定了数据应如何呈现。

可用的字符串表示类型是：

类型

含义

's'

字符串格式。这是字符串的默认类型，可以省略。

None

和 's' 一样。

可用的整数表示类型是：

类型

含义

'b'

二进制格式。 输出以 2 为基数的数字。

'c'

字符。在打印之前将整数转换为相应的unicode字符。

'd'

十进制整数。 输出以 10 为基数的数字。

'o'

八进制格式。 输出以 8 为基数的数字。

'x'

十六进制格式。 输出以 16 为基数的数字，使用小写字母表示 9 以上的数码。

'X'

十六进制格式。 输出以 16 为基数的数字，使用大写字母表示 9 以上的数码。

'n'

数字。 这与 'd' 相似，不同之处在于它会使用当前区域设置来插入适当的数字分隔字符。

None

和 'd' 相同。

在上述的表示类型之外，整数还可以通过下列的浮点表示类型来格式化 (除了 'n' 和 None)。 当这样做时，会在格式化之前使用 float() 将整数转换为浮点数。

浮点数和小数值可用的表示类型有：

类型

含义

'e'

指数表示。 以使用字母 ‘e’ 来标示指数的科学计数法打印数字。 默认的精度为 6。

'E'

指数表示。 与 'e' 相似，不同之处在于它使用大写字母 ‘E’ 作为分隔字符。

'f'

定点表示。 将数字显示为一个定点数。 默认的精确度为 6。

'F'

定点表示。 与 'f' 相似，但会将 nan 转为 NAN 并将 inf 转为 INF。

'g'

常规格式。 对于给定的精度 p >= 1，这会将数值舍入到 p 位有效数字，再将结果以定点格式或科学计数法进行格式化，具体取决于其值的大小。

The precise rules are as follows: suppose that the result formatted with presentation type 'e' and precision p-1 would have exponent exp. Then if -4 <= exp < p, the number is formatted with presentation type 'f' and precision p-1-exp. Otherwise, the number is formatted with presentation type 'e' and precision p-1. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it.

正负无穷，正负零和 nan 会分别被格式化为 inf, -inf, 0, -0 和 nan，无论精度如何设定。

精度 0 会被视为等同于精度 1。 默认精度为 6。

'G'

常规格式。 类似于 'g'，不同之处在于当数值非常大时会切换为 'E'。 无穷与 NaN 也会表示为大写形式。

'n'

数字。 这与 'g' 相似，不同之处在于它会使用当前区域设置来插入适当的数字分隔字符。

'%'

百分比。 将数字乘以 100 并显示为定点 ('f') 格式，后面带一个百分号。

None

类似于 'g'，不同之处在于当使用定点表示法时，小数点后将至少显示一位。 默认精度与表示给定值所需的精度一样。 整体效果为与其他格式修饰符所调整的 str() 输出保持一致。

6.1.3.2. 格式示例

本节包含 str.format() 语法的示例以及与旧式 % 格式化的比较。

该语法在大多数情况下与旧式的 % 格式化类似，只是增加了 {} 和 : 来取代 %。 例如，，'%03.2f' 可以被改写为 '{:03.2f}'。

新的格式语法还支持新增的不同选项，将在以下示例中说明。

按位置访问参数:

>>> '{0}, {1}, {2}'.format('a', 'b', 'c')
'a, b, c'
>>> '{}, {}, {}'.format('a', 'b', 'c')  # 3.1+ only
'a, b, c'
>>> '{2}, {1}, {0}'.format('a', 'b', 'c')
'c, b, a'
>>> '{2}, {1}, {0}'.format(*'abc')      # unpacking argument sequence
'c, b, a'
>>> '{0}{1}{0}'.format('abra', 'cad')   # arguments' indices can be repeated
'abracadabra'

按名称访问参数:

>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
'Coordinates: 37.24N, -115.81W'
>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
>>> 'Coordinates: {latitude}, {longitude}'.format(**coord)
'Coordinates: 37.24N, -115.81W'

访问参数的属性:

>>> c = 3-5j
>>> ('The complex number {0} is formed from the real part {0.real} '
...  'and the imaginary part {0.imag}.').format(c)
'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.'
>>> class Point:
...     def __init__(self, x, y):
...         self.x, self.y = x, y
...     def __str__(self):
...         return 'Point({self.x}, {self.y})'.format(self=self)
...
>>> str(Point(4, 2))
'Point(4, 2)'

访问参数的项:

>>> coord = (3, 5)
>>> 'X: {0[0]};  Y: {0[1]}'.format(coord)
'X: 3;  Y: 5'

替代 %s 和 %r:

>>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')
"repr() shows quotes: 'test1'; str() doesn't: test2"

对齐文本以及指定宽度:

>>> '{:<30}'.format('left aligned')
'left aligned                  '
>>> '{:>30}'.format('right aligned')
'                 right aligned'
>>> '{:^30}'.format('centered')
'           centered           '
>>> '{:*^30}'.format('centered')  # use '*' as a fill char
'***********centered***********'

替代 %+f, %-f 和 % f 以及指定正负号:

>>> '{:+f}; {:+f}'.format(3.14, -3.14)  # show it always
'+3.140000; -3.140000'
>>> '{: f}; {: f}'.format(3.14, -3.14)  # show a space for positive numbers
' 3.140000; -3.140000'
>>> '{:-f}; {:-f}'.format(3.14, -3.14)  # show only the minus -- same as '{:f}; {:f}'
'3.140000; -3.140000'

替代 %x 和 %o 以及转换基于不同进位制的值:

>>> # format also supports binary numbers
>>> "int: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(42)
'int: 42;  hex: 2a;  oct: 52;  bin: 101010'
>>> # with 0x, 0o, or 0b as prefix:
>>> "int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42)
'int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010'

使用逗号作为千位分隔符:

>>> '{:,}'.format(1234567890)
'1,234,567,890'

表示为百分数:

>>> points = 19
>>> total = 22
>>> 'Correct answers: {:.2%}'.format(points/total)
'Correct answers: 86.36%'

使用特定类型的专属格式化:

>>> import datetime
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
'2010-07-04 12:15:58'

嵌套参数以及更复杂的示例:

>>> for align, text in zip('<^>', ['left', 'center', 'right']):
...     '{0:{fill}{align}16}'.format(text, fill=align, align=align)
...
'left<<<<<<<<<<<<'
'^^^^^center^^^^^'
'>>>>>>>>>>>right'
>>>
>>> octets = [192, 168, 0, 1]
>>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)
'C0A80001'
>>> int(_, 16)
3232235521
>>>
>>> width = 5
>>> for num in range(5,12):
...     for base in 'dXob':
...         print('{0:{width}{base}}'.format(num, base=base, width=width), end=' ')
...     print()
...
    5     5     5   101
    6     6     6   110
    7     7     7   111
    8     8    10  1000
    9     9    11  1001
   10     A    12  1010
   11     B    13  1011

6.1.4. 模板字符串

模板字符串提供了由 PEP 292 [https://www.python.org/dev/peps/pep-0292] 所描述的更简便的字符串替换方式。 模板字符串的一个主要用例是文本国际化 (i18n)，因为在此场景下，更简单的语法和功能使得文本翻译过程比使用 Python 的其他内置字符串格式化工具更为方便。 作为基于模板字符串构建以实现 i18n 的库的一个示例，请参看 flufl.i18n [http://flufli18n.readthedocs.io/en/latest/] 包。

模板字符串支持基于 $ 的替换，使用以下规则：

$$ 为转义符号；它会被替换为单个的 $。

$identifier 为替换占位符，它会匹配一个名为 "identifier" 的映射键。 在默认情况下，"identifier" 限制为任意 ASCII 字母数字（包括下划线）组成的字符串，不区分大小写，以下划线或 ASCII 字母开头。 在 $ 字符之后的第一个非标识符字符将表明占位符的终结。

${identifier} 等价于 $identifier。 当占位符之后紧跟着有效的但又不是占位符一部分的标识符字符时需要使用，例如 "${noun}ification"。

在字符串的其他位置出现 $ 将导致引发 ValueError。

string 模块提供了实现这些规则的 Template 类。 Template 有下列方法：

class string.Template(template)

该构造器接受一个参数作为模板字符串。

substitute(mapping, **kwds)

执行模板替换，返回一个新字符串。 mapping 为任意字典类对象，其中的键将匹配模板中的占位符。 或者你也可以提供一组关键字参数，其中的关键字即对应占位符。 当同时给出 mapping 和 kwds 并且存在重复时，则以 kwds 中的占位符为优先。

safe_substitute(mapping, **kwds)

类似于 substitute()，不同之处是如果有占位符未在 mapping 和 kwds 中找到，不是引发 KeyError 异常，而是将原始占位符不加修改地显示在结果字符串中。 另一个与 substitute() 的差异是任何在其他情况下出现的 $ 将简单地返回 $ 而不是引发 ValueError。

此方法被认为“安全”，因为虽然仍有可能发生其他异常，但它总是尝试返回可用的字符串而不是引发一个异常。 从另一方面来说，safe_substitute() 也可能根本算不上安全，因为它将静默地忽略错误格式的模板，例如包含多余的分隔符、不成对的花括号或不是合法 Python 标识符的占位符等等。

Template 的实例还提供一个公有数据属性：

template

这是作为构造器的 template 参数被传入的对象。 一般来说，你不应该修改它，但并不强制要求只读访问。

以下是一个如何使用模版的示例：

>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
>>> d = dict(who='tim')
>>> Template('Give $who $100').substitute(d)
Traceback (most recent call last):
...
ValueError: Invalid placeholder in string: line 1, col 11
>>> Template('$who likes $what').substitute(d)
Traceback (most recent call last):
...
KeyError: 'what'
>>> Template('$who likes $what').safe_substitute(d)
'tim likes $what'

进阶用法：你可以派生 Template 的子类来自定义占位符语法、分隔符，或用于解析模板字符串的整个正则表达式。 为此目的，你可以重载这些类属性：

delimiter – This is the literal string describing a placeholder introducing delimiter. The default value is $. Note that this should not be a regular expression, as the implementation will call re.escape() on this string as needed.

idpattern – This is the regular expression describing the pattern for non-braced placeholders (the braces will be added automatically as appropriate). The default value is the regular expression (?-i:[_a-zA-Z][_a-zA-Z0-9]*).

注解

Since default flags is re.IGNORECASE, pattern [a-z] can match with some non-ASCII characters. That’s why we use local -i flag here.

While flags is kept to re.IGNORECASE for backward compatibility, you can override it to 0 or re.IGNORECASE | re.ASCII when subclassing.

flags – 将在编译用于识别替换内容的正则表达式被应用的正则表达式旗标。 默认值为 re.IGNORECASE。 请注意 re.VERBOSE 总是会被加为旗标，因此自定义的 idpattern 必须遵循详细正则表达式的约定。

3.2 新版功能.

作为另一种选项，你可以通过重载类属性 pattern 来提供整个正则表达式模式。 如果你这样做，该值必须为一个具有四个命名捕获组的正则表达式对象。 这些捕获组对应于上面已经给出的规则，以及无效占位符的规则：

escaped – 这个组匹配转义序列，在默认模式中即 $$。

named – 这个组匹配不带花括号的占位符名称；它不应当包含捕获组中的分隔符。

braced – 这个组匹配带有花括号的占位符名称；它不应当包含捕获组中的分隔符或者花括号。

invalid – 这个组匹配任何其他分隔符模式（通常为单个分隔符），并且它应当出现在正则表达式的末尾。

6.1.5. 辅助函数

string.capwords(s, sep=None)

使用 str.split() 将参数拆分为单词，使用 str.capitalize() 将单词转为大写形式，使用 str.join() 将大写的单词进行拼接。 如果可选的第二个参数 sep 被省略或为 None，则连续的空白字符会被替换为单个空格符并且开头和末尾的空白字符会被移除，否则 sep 会被用来拆分和拼接单词。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

6.2. re — 正则表达式操作

源代码: Lib/re.py [https://github.com/python/cpython/tree/3.6/Lib/re.py]

这个模块提供了与 Perl 语言类似的正则表达式匹配操作。

模式和被搜索的字符串既可以是 Unicode 字符串 (str) ，也可以是8位字节串 (bytes)。 但是，Unicode 字符串与8位字节串不能混用：也就是说，你不能用一个字节串模式去匹配 Unicode 字符串，反之亦然；类似地，当进行替换操作时，替换字符串的类型也必须与所用的模式和搜索字符串的类型一致。

正则表达式使用反斜杠（'\'）来表示特殊形式，或者把特殊字符转义成普通字符。 而反斜杠在普通的 Python 字符串里也有相同的作用，所以就产生了冲突。比如说，要匹配一个字面上的反斜杠，正则表达式模式不得不写成 '\\\\'，因为正则表达式里匹配一个反斜杠必须是 \\ ，而每个反斜杠在普通的 Python 字符串里都要写成 \\ 。

解决办法是对于正则表达式样式使用 Python 的原始字符串表示法；在带有 'r' 前缀的字符串字面值中，反斜杠不必做任何特殊处理。 因此 r"\n" 表示包含 '\' 和 'n' 两个字符的字符串，而 "\n" 则表示只包含一个换行符的字符串。 样式在 Python 代码中通常都会使用这种原始字符串表示法来表示。

绝大部分正则表达式操作都提供为模块函数和方法，在 编译正则表达式. 这些函数是一个捷径，不需要先编译一个正则对象，但是损失了一些优化参数。

参见

第三方模块 regex [https://pypi.org/project/regex/] , 提供了与标准库 re 模块兼容的API接口, 同时还提供了额外的功能和更全面的Unicode支持。

6.2.1. 正则表达式语法

一个正则表达式（或RE）指定了一集与之匹配的字符串；模块内的函数可以让你检查某个字符串是否跟给定的正则表达式匹配（或者一个正则表达式是否匹配到一个字符串，这两种说法含义相同）。

正则表达式可以拼接； 如果 A 和 B 都是正则表达式， 那么 AB 也是正则表达式。 通常， 如果字符串 p 匹配 A 并且另一个字符串 q 匹配 B, 那么 pq 可以匹配 AB。除非 A 或者 B 包含低优先级操作，A 和 B 存在边界条件；或者命名组引用。所以，复杂表达式可以很容易的从这里描述的简单源语表达式构建。 了解更多正则表达式理论和实现，参考the Friedl book [Frie09] ，或者其他编译器构建的书籍。

以下是正则表达式格式的简要说明。更详细的信息和演示，参考 正则表达式HOWTO。

正则表达式可以包含普通或者特殊字符。绝大部分普通字符，比如 'A', 'a', 或者 '0'，都是最简单的正则表达式。它们就匹配自身。你可以拼接普通字符，所以 last 匹配字符串 'last'. （在这一节的其他部分，我们将用 this special style 这种方式表示正则表达式，通常不带引号，要匹配的字符串用 'in single quotes' ，单引号形式。）

有些字符，比如 '|' 或者 '('，属于特殊字符。 特殊字符既可以表示它的普通含义， 也可以影响它旁边的正则表达式的解释。

重复修饰符 (*, +, ?, {m,n}, 等) 不能直接嵌套。这样避免了非贪婪后缀 ? 修饰符，和其他实现中的修饰符产生的多义性。要应用一个内层重复嵌套，可以使用括号。 比如，表达式 (?:a{6})* 匹配6个 'a' 字符重复任意次数。

特殊字符是：

.

(点) 在默认模式，匹配除了换行的任意字符。如果指定了标签 DOTALL ，它将匹配包括换行符的任意字符。

^

(插入符号) 匹配字符串的开头， 并且在 MULTILINE 模式也匹配换行后的首个符号。

$

匹配字符串尾或者在字符串尾的换行符的前一个字符，在 MULTILINE 模式下也会匹配换行符之前的文本。 foo 匹配 ‘foo’ 和 ‘foobar’，但正则表达式 foo$ 只匹配 ‘foo’。 更有趣的是，在 'foo1\nfoo2\n' 中搜索 foo.$，通常匹配 ‘foo2’，但在 MULTILINE 模式下可以匹配到 ‘foo1’；在 'foo\n' 中搜索 $ 会找到两个（空的）匹配：一个在换行符之前，一个在字符串的末尾。

*

对它前面的正则式匹配0到任意次重复， 尽量多的匹配字符串。 ab* 会匹配 'a'，'ab'，或者 'a' 后面跟随任意个 'b'。

+

对它前面的正则式匹配1到任意次重复。 ab+ 会匹配 'a' 后面跟随1个以上到任意个 'b'，它不会匹配 'a'。

?

对它前面的正则式匹配0到1次重复。 ab? 会匹配 'a' 或者 'ab'。

*?, +?, ??

'*', '+'，和 '?' 修饰符都是 贪婪的；它们在字符串进行尽可能多的匹配。有时候并不需要这种行为。如果正则式 <.*> 希望找到 '<a> b <c>'，它将会匹配整个字符串，而不仅是 '<a>'。在修饰符之后添加 ? 将使样式以 非贪婪`方式或者 :dfn:`最小 方式进行匹配； 尽量 少 的字符将会被匹配。 使用正则式 <.*?> 将会仅仅匹配 '<a>'。

“{m}”

对其之前的正则式指定匹配 m 个重复；少于 m 的话就会导致匹配失败。比如， a{6} 将匹配6个 'a' , 但是不能是5个。

“{m, n}”

对正则式进行 m 到 n 次匹配，在 m 和 n 之间取尽量多。 比如，a{3,5} 将匹配 3 到 5个 'a'。忽略 m 意为指定下界为0，忽略 n 指定上界为无限次。 比如 a{4,}b 将匹配 'aaaab' 或者1000个 'a' 尾随一个 'b'，但不能匹配 'aaab'。逗号不能省略，否则无法辨别修饰符应该忽略哪个边界。

{m,n}?

前一个修饰符的非贪婪模式，只匹配尽量少的字符次数。比如，对于 'aaaaaa'， a{3,5} 匹配 5个 'a' ，而 a{3,5}? 只匹配3个 'a'。

\

转义特殊字符（允许你匹配 '*', '?', 或者此类其他），或者表示一个特殊序列；特殊序列之后进行讨论。

如果你没有使用原始字符串（ r'raw' ）来表达样式，要牢记Python也使用反斜杠作为转义序列；如果转义序列不被Python的分析器识别，反斜杠和字符才能出现在字符串中。如果Python可以识别这个序列，那么反斜杠就应该重复两次。这将导致理解障碍，所以高度推荐，就算是最简单的表达式，也要使用原始字符串。

[]

用于表示一个字符集合。在一个集合中：

字符可以单独列出，比如 [amk] 匹配 'a'， 'm'， 或者 'k'。

可以表示字符范围，通过用 '-' 将两个字符连起来。比如 [a-z] 将匹配任何小写ASCII字符， [0-5][0-9] 将匹配从 00 到 59 的两位数字， [0-9A-Fa-f] 将匹配任何十六进制数位。 如果 - 进行了转义 （比如 [a\-z]）或者它的位置在首位或者末尾（如 [-a] 或 [a-]），它就只表示普通字符 '-'。

特殊字符在集合中，失去它的特殊含义。比如 [(+*)] 只会匹配这几个文法字符 '(', '+', '*', or ')'。

字符类如 \w 或者 \S (如下定义) 在集合内可以接受，它们可以匹配的字符由 ASCII 或者 LOCALE 模式决定。

不在集合范围内的字符可以通过 取反 来进行匹配。如果集合首字符是 '^' ，所有 不 在集合内的字符将会被匹配，比如 [^5] 将匹配所有字符，除了 '5'， [^^] 将匹配所有字符，除了 '^'. ^ 如果不在集合首位，就没有特殊含义。

在集合内要匹配一个字符 ']'，有两种方法，要么就在它之前加上反斜杠，要么就把它放到集合首位。比如， [()[\]{}] 和 []()[{}] 都可以匹配括号。

|

A|B， A 和 B 可以是任意正则表达式，创建一个正则表达式，匹配 A 或者 B. 任意个正则表达式可以用 '|' 连接。它也可以在组合（见下列）内使用。扫描目标字符串时， '|' 分隔开的正则样式从左到右进行匹配。当一个样式完全匹配时，这个分支就被接受。意思就是，一旦 A 匹配成功， B 就不再进行匹配，即便它能产生一个更好的匹配。或者说，'|' 操作符绝不贪婪。 如果要匹配 '|' 字符，使用 \|， 或者把它包含在字符集里，比如 [|].

(...)

（组合），匹配括号内的任意正则表达式，并标识出组合的开始和结尾。匹配完成后，组合的内容可以被获取，并可以在之后用 \number 转义序列进行再次匹配，之后进行详细说明。要匹配字符 '(' 或者 ')', 用 \( 或 \), 或者把它们包含在字符集合里: [(], [)].

(?…)

这是个扩展标记法 （一个 '?' 跟随 '(' 并无含义）。 '?' 后面的第一个字符决定了这个构建采用什么样的语法。这种扩展通常并不创建新的组合； (?P<name>...) 是唯一的例外。 以下是目前支持的扩展。

(?aiLmsux)

( 'a', 'i', 'L', 'm', 's', 'u', 'x' 中的一个或多个) 这个组合匹配一个空字符串；这些字符对正则表达式设置以下标记 re.A (只匹配ASCII字符), re.I (忽略大小写), re.L (语言依赖), re.M (多行模式), re.S (点dot匹配全部字符), re.U (Unicode匹配), and re.X (冗长模式)。 (这些标记在 模块内容 中描述) 如果你想将这些标记包含在正则表达式中，这个方法就很有用，免去了在 re.compile() 中传递 flag 参数。标记应该在表达式字符串首位表示。

(?:…)

正则括号的非捕获版本。 匹配在括号内的任何正则表达式，但该分组所匹配的子字符串 不能 在执行匹配后被获取或是之后在模式中被引用。

(?imsx-imsx:...)

(Zero or more letters from the set 'i', 'm', 's', 'x', optionally followed by '-' followed by one or more letters from the same set.) The letters set or removes the corresponding flags: re.I (ignore case), re.M (multi-line), re.S (dot matches all), and re.X (verbose), for the part of the expression. (The flags are described in 模块内容.)

3.6 新版功能.

(?P<name>…)

（命名组合）类似正则组合，但是匹配到的子串组在外部是通过定义的 name 来获取的。组合名必须是有效的Python标识符，并且每个组合名只能用一个正则表达式定义，只能定义一次。一个符号组合同样是一个数字组合，就像这个组合没有被命名一样。

命名组合可以在三种上下文中引用。如果样式是 (?P<quote>['"]).*?(?P=quote) （也就是说，匹配单引号或者双引号括起来的字符串)：

引用组合 “quote” 的上下文

引用方法

在正则式自身内

(?P=quote) (如示)

\1

处理匹配对象 m

m.group('quote')

m.end('quote') (等)

传递到 re.sub() 里的 repl 参数中

\g<quote>

\g<1>

\1

(?P=name)

反向引用一个命名组合；它匹配前面那个叫 name 的命名组中匹配到的串同样的字串。

(?#…)

注释；里面的内容会被忽略。

(?=…)

匹配 … 的内容，但是并不消费样式的内容。这个叫做 lookahead assertion。比如， Isaac (?=Asimov) 匹配 'Isaac ' 只有在后面是 'Asimov' 的时候。

(?!…)

匹配 … 不符合的情况。这个叫 negative lookahead assertion （前视取反）。比如说， Isaac (?!Asimov) 只有后面 不 是 'Asimov' 的时候才匹配 'Isaac ' 。

(?<=…)

匹配字符串的当前位置，它的前面匹配 … 的内容到当前位置。这叫:dfn:positive lookbehind assertion （正向后视断定）。 (?<=abc)def 会在 'abcdef' 中找到一个匹配，因为后视会往后看3个字符并检查是否包含匹配的样式。包含的匹配样式必须是定长的，意思就是 abc 或 a|b 是允许的，但是 a* 和 a{3,4} 不可以。注意以 positive lookbehind assertions 开始的样式，如 (?<=abc)def ，并不是从 a 开始搜索，而是从 d 往回看的。你可能更加愿意使用 search() 函数，而不是 match() 函数：

>>> import re
>>> m = re.search('(?<=abc)def', 'abcdef')
>>> m.group(0)
'def'

这个例子搜索一个跟随在连字符后的单词：

>>> m = re.search(r'(?<=-)\w+', 'spam-egg')
>>> m.group(0)
'egg'

在 3.5 版更改: 添加定长组合引用的支持。

(?<!…)

匹配当前位置之前不是 … 的样式。这个叫:dfn:negative lookbehind assertion （后视断定取非）。类似正向后视断定，包含的样式匹配必须是定长的。由 negative lookbehind assertion 开始的样式可以从字符串搜索开始的位置进行匹配。

(?(id/name)yes-pattern|no-pattern)

如果给定的 id 或 name 存在，将会尝试匹配 yes-pattern ，否则就尝试匹配 no-pattern，no-pattern 可选，也可以被忽略。比如， (<)?(\w+@\w+(?:\.\w+)+)(?(1)>|$) 是一个email样式匹配，将匹配 '<user@host.com>' 或 'user@host.com' ，但不会匹配 '<user@host.com' ，也不会匹配 'user@host.com>'。

由 '\' 和一个字符组成的特殊序列在以下列出。 如果普通字符不是ASCII数位或者ASCII字母，那么正则样式将匹配第二个字符。比如，\$ 匹配字符 '$'.

\number

匹配数字代表的组合。每个括号是一个组合，组合从1开始编号。比如 (.+) \1 匹配 'the the' 或者 '55 55', 但不会匹配 'thethe' (注意组合后面的空格)。这个特殊序列只能用于匹配前面99个组合。如果 number 的第一个数位是0， 或者 number 是三个八进制数，它将不会被看作是一个组合，而是八进制的数字值。在 '[' 和 ']' 字符集合内，任何数字转义都被看作是字符。

\A

只匹配字符串开始。

\b

匹配空字符串，但只在单词开始或结尾的位置。一个单词被定义为一个单词字符的序列。注意，通常 \b 定义为 \w 和 \W 字符之间，或者 \w 和字符串开始/结尾的边界， 意思就是 r'\bfoo\b' 匹配 'foo', 'foo.', '(foo)', 'bar foo baz' 但不匹配 'foobar' 或者 'foo3'。

默认情况下，Unicode字母和数字是在Unicode样式中使用的，但是可以用 ASCII 标记来更改。如果 LOCALE 标记被设置的话，词的边界是由当前语言区域设置决定的，\b 表示退格字符，以便与Python字符串文本兼容。

\B

匹配空字符串，但 不 能在词的开头或者结尾。意思就是 r'py\B' 匹配 'python', 'py3', 'py2', 但不匹配 'py', 'py.', 或者 'py!'. \B 是 \b 的取非，所以Unicode样式的词语是由Unicode字母，数字或下划线构成的，虽然可以用 ASCII 标志来改变。如果使用了 LOCALE 标志，则词的边界由当前语言区域设置。

\d

对于 Unicode (str) 样式：

Matches any Unicode decimal digit (that is, any character in Unicode character category [Nd]). This includes [0-9], and also many other digit characters. If the ASCII flag is used only [0-9] is matched (but the flag affects the entire regular expression, so in such cases using an explicit [0-9] may be a better choice).

对于8位(bytes)样式：

匹配任何十进制数，就是 [0-9]。

\D

Matches any character which is not a decimal digit. This is the opposite of \d. If the ASCII flag is used this becomes the equivalent of [^0-9] (but the flag affects the entire regular expression, so in such cases using an explicit [^0-9] may be a better choice).

\s

对于 Unicode (str) 样式：

Matches Unicode whitespace characters (which includes [ \t\n\r\f\v], and also many other characters, for example the non-breaking spaces mandated by typography rules in many languages). If the ASCII flag is used, only [ \t\n\r\f\v] is matched (but the flag affects the entire regular expression, so in such cases using an explicit [ \t\n\r\f\v] may be a better choice).

对于8位(bytes)样式：

匹配ASCII中的空白字符，就是 [ \t\n\r\f\v] 。

\S

Matches any character which is not a whitespace character. This is the opposite of \s. If the ASCII flag is used this becomes the equivalent of [^ \t\n\r\f\v] (but the flag affects the entire regular expression, so in such cases using an explicit [^ \t\n\r\f\v] may be a better choice).

\w

对于 Unicode (str) 样式：

Matches Unicode word characters; this includes most characters that can be part of a word in any language, as well as numbers and the underscore. If the ASCII flag is used, only [a-zA-Z0-9_] is matched (but the flag affects the entire regular expression, so in such cases using an explicit [a-zA-Z0-9_] may be a better choice).

对于8位(bytes)样式：

匹配ASCII字符中的数字和字母和下划线，就是 [a-zA-Z0-9_] 。如果设置了 LOCALE 标记，就匹配当前语言区域的数字和字母和下划线。

\W

Matches any character which is not a word character. This is the opposite of \w. If the ASCII flag is used this becomes the equivalent of [^a-zA-Z0-9_] (but the flag affects the entire regular expression, so in such cases using an explicit [^a-zA-Z0-9_] may be a better choice). If the LOCALE flag is used, matches characters considered alphanumeric in the current locale and the underscore.

\Z

只匹配字符串尾。

绝大部分Python的标准转义字符也被正则表达式分析器支持。:

\a      \b      \f      \n
\r      \t      \u      \U
\v      \x      \\

（注意 \b 被用于表示词语的边界，它只在字符集合内表示退格，比如 [\b] 。）

'\u' and '\U' escape sequences are only recognized in Unicode patterns. In bytes patterns they are errors.

八进制转义包含为一个有限形式。如果首位数字是 0， 或者有三个八进制数位，那么就认为它是八进制转义。其他的情况，就看作是组引用。对于字符串文本，八进制转义最多有三个数位长。

在 3.3 版更改: 增加了 '\u' 和 '\U' 转义序列。

在 3.6 版更改: 由 '\' 和一个ASCII字符组成的未知转义会被看成错误。

6.2.2. 模块内容

模块定义了几个函数，常量，和一个例外。有些函数是编译后的正则表达式方法的简化版本（少了一些特性）。绝大部分重要的应用，总是会先将正则表达式编译，之后在进行操作。

在 3.6 版更改: 标志常量现在是 RegexFlag 类的实例，这个类是 enum.IntFlag 的子类。

re.compile(pattern, flags=0)

Compile a regular expression pattern into a regular expression object, which can be used for matching using its match(), search() and other methods, described below.

这个表达式的行为可以通过指定 标记 的值来改变。值可以是以下任意变量，可以通过位的OR操作来结合（ | 操作符）。

序列

prog = re.compile(pattern)
result = prog.match(string)

等价于

result = re.match(pattern, string)

如果需要多次使用这个正则表达式的话，使用 re.compile() 和保存这个正则对象以便复用，可以让程序更加高效。

注解

通过 re.compile() 编译后的样式，和模块级的函数会被缓存， 所以少数的正则表达式使用无需考虑编译的问题。

re.A

re.ASCII

让 \w, \W, \b, \B, \d, \D, \s 和 \S 只匹配ASCII，而不是Unicode。这只对Unicode样式有效，会被byte样式忽略。相当于前面语法中的内联标志 (?a) 。

注意，为了保持向后兼容， re.U 标记依然存在（还有他的同义 re.UNICODE 和嵌入形式 (?u) ) ， 但是这些在 Python 3 是冗余的，因为默认字符串已经是Unicode了（并且Unicode匹配不允许byte出现)。

re.DEBUG

显示编译时的debug信息，没有内联标记。

re.I

re.IGNORECASE

进行忽略大小写匹配；表达式如 [A-Z] 也会匹配小写字符。Unicode匹配（比如 Ü 匹配 ü）同样有用，除非设置了 re.ASCII 标记来禁用非ASCII匹配。当前语言区域不会改变这个标记，除非设置了 re.LOCALE 标记。这个相当于内联标记 (?i) 。

Note that when the Unicode patterns [a-z] or [A-Z] are used in combination with the IGNORECASE flag, they will match the 52 ASCII letters and 4 additional non-ASCII letters: ‘İ’ (U+0130, Latin capital letter I with dot above), ‘ı’ (U+0131, Latin small letter dotless i), ‘ſ’ (U+017F, Latin small letter long s) and ‘K’ (U+212A, Kelvin sign). If the ASCII flag is used, only letters ‘a’ to ‘z’ and ‘A’ to ‘Z’ are matched (but the flag affects the entire regular expression, so in such cases using an explicit (?-i:[a-zA-Z]) may be a better choice).

re.L

re.LOCALE

由当前语言区域决定 \w, \W, \b, \B 和大小写敏感匹配。这个标记只能对byte样式有效。这个标记不推荐使用，因为语言区域机制很不可靠，它一次只能处理一个 “习惯”，而且只对8位字节有效。Unicode匹配在Python 3 里默认启用，并可以处理不同语言。 这个对应内联标记 (?L) 。

在 3.6 版更改: re.LOCALE 只能用于byte样式，而且不能和 re.ASCII 一起用。

re.M

re.MULTILINE

设置以后，样式字符 '^' 匹配字符串的开始，和每一行的开始（换行符后面紧跟的符号）；样式字符 '$' 匹配字符串尾，和每一行的结尾（换行符前面那个符号）。默认情况下，’^’ 匹配字符串头，'$' 匹配字符串尾。对应内联标记 (?m) 。

re.S

re.DOTALL

让 '.' 特殊字符匹配任何字符，包括换行符；如果没有这个标记，'.' 就匹配 除了 换行符的其他任意字符。对应内联标记 (?s) 。

re.X

re.VERBOSE

这个标记允许你编写更具可读性更友好的正则表达式。通过分段和添加注释。空白符号会被忽略，除非在一个字符集合当中或者由反斜杠转义，或者在 *?, (?: or (?P<…> 分组之内。当一个行内有 # 不在字符集和转义序列，那么它之后的所有字符都是注释。

意思就是下面两个正则表达式等价地匹配一个十进制数字：

a = re.compile(r"""\d +  # the integral part
                   \.    # the decimal point
                   \d *  # some fractional digits""", re.X)
b = re.compile(r"\d+\.\d*")

对应内联标记 (?x) 。

re.search(pattern, string, flags=0)

扫描整个 字符串 找到匹配样式的第一个位置，并返回一个相应的 匹配对象。如果没有匹配，就返回一个 None ； 注意这和找到一个零长度匹配是不同的。

re.match(pattern, string, flags=0)

如果 string 开始的0或者多个字符匹配到了正则表达式样式，就返回一个相应的 匹配对象 。 如果没有匹配，就返回 None ；注意它跟零长度匹配是不同的。

注意即便是 MULTILINE 多行模式， re.match() 也只匹配字符串的开始位置，而不匹配每行开始。

如果你想定位 string 的任何位置，使用 search() 来替代（也可参考 search() vs. match() ）

re.fullmatch(pattern, string, flags=0)

如果整个 string 匹配到正则表达式样式，就返回一个相应的 匹配对象 。 否则就返回一个 None ；注意这跟零长度匹配是不同的。

3.4 新版功能.

re.split(pattern, string, maxsplit=0, flags=0)

用 pattern 分开 string 。 如果在 pattern 中捕获到括号，那么所有的组里的文字也会包含在列表里。如果 maxsplit 非零， 最多进行 maxsplit 次分隔， 剩下的字符全部返回到列表的最后一个元素。

>>> re.split(r'\W+', 'Words, words, words.')
['Words', 'words', 'words', '']
>>> re.split(r'(\W+)', 'Words, words, words.')
['Words', ', ', 'words', ', ', 'words', '.', '']
>>> re.split(r'\W+', 'Words, words, words.', 1)
['Words', 'words, words.']
>>> re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE)
['0', '3', '9']

如果分隔符里有捕获组合，并且匹配到字符串的开始，那么结果将会以一个空字符串开始。对于结尾也是一样

>>> re.split(r'(\W+)', '...words, words...')
['', '...', 'words', ', ', 'words', '...', '']

这样的话，分隔组将会出现在结果列表中同样的位置。

注解

split() doesn’t currently split a string on an empty pattern match. For example:

>>> re.split('x*', 'axbc')
['a', 'bc']

Even though 'x*' also matches 0 ‘x’ before ‘a’, between ‘b’ and ‘c’, and after ‘c’, currently these matches are ignored. The correct behavior (i.e. splitting on empty matches too and returning ['', 'a', 'b', 'c', '']) will be implemented in future versions of Python, but since this is a backward incompatible change, a FutureWarning will be raised in the meanwhile.

Patterns that can only match empty strings currently never split the string. Since this doesn’t match the expected behavior, a ValueError will be raised starting from Python 3.5:

>>> re.split("^$", "foo\n\nbar\n", flags=re.M)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  ...
ValueError: split() requires a non-empty pattern match.

在 3.1 版更改: 增加了可选标记参数。

在 3.5 版更改: Splitting on a pattern that could match an empty string now raises a warning. Patterns that can only match empty strings are now rejected.

re.findall(pattern, string, flags=0)

对 string 返回一个不重复的 pattern 的匹配列表， string 从左到右进行扫描，匹配按找到的顺序返回。如果样式里存在一到多个组，就返回一个组合列表；就是一个元组的列表（如果样式里有超过一个组合的话）。空匹配也会包含在结果里。

注解

Due to the limitation of the current implementation the character following an empty match is not included in a next match, so findall(r'^|\w+', 'two words') returns ['', 'wo', 'words'] (note missed “t”). This is changed in Python 3.7.

re.finditer(pattern, string, flags=0)

Return an iterator yielding match objects over all non-overlapping matches for the RE pattern in string. The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result. See also the note about findall().

re.sub(pattern, repl, string, count=0, flags=0)

Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl. If the pattern isn’t found, string is returned unchanged. repl can be a string or a function; if it is a string, any backslash escapes in it are processed. That is, \n is converted to a single newline character, \r is converted to a carriage return, and so forth. Unknown escapes such as \& are left alone. Backreferences, such as \6, are replaced with the substring matched by group 6 in the pattern. For example:

>>> re.sub(r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):',
...        r'static PyObject*\npy_\1(void)\n{',
...        'def myfunc():')
'static PyObject*\npy_myfunc(void)\n{'

如果 repl 是一个函数，那它会对每个非重复的 pattern 的情况调用。这个函数只能有一个 匹配对象 参数，并返回一个替换后的字符串。比如

>>> def dashrepl(matchobj):
...     if matchobj.group(0) == '-': return ' '
...     else: return '-'
>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
'pro--gram files'
>>> re.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)
'Baked Beans & Spam'

样式可以是一个字符串或者一个 样式对象 。

The optional argument count is the maximum number of pattern occurrences to be replaced; count must be a non-negative integer. If omitted or zero, all occurrences will be replaced. Empty matches for the pattern are replaced only when not adjacent to a previous match, so sub('x*', '-', 'abc') returns '-a-b-c-'.

在字符串类型的 repl 参数里，如上所述的转义和向后引用中，\g<name> 会使用命名组合 name，（在 (?P<name>…) 语法中定义） \g<number> 会使用数字组；\g<2> 就是 \2，但它避免了二义性，如 \g<2>0。 \20 就会被解释为组20，而不是组2后面跟随一个字符 '0'。向后引用 \g<0> 把 pattern 作为一整个组进行引用。

在 3.1 版更改: 增加了可选标记参数。

在 3.5 版更改: 不匹配的组合替换为空字符串。

在 3.6 版更改: pattern 中的未知转义（由 '\' 和一个 ASCII 字符组成）被视为错误。

Deprecated since version 3.5, will be removed in version 3.7: Unknown escapes in repl consisting of '\' and an ASCII letter now raise a deprecation warning and will be forbidden in Python 3.7.

re.subn(pattern, repl, string, count=0, flags=0)

行为与 sub() 相同，但是返回一个元组 (字符串, 替换次数).

在 3.1 版更改: 增加了可选标记参数。

在 3.5 版更改: 不匹配的组合替换为空字符串。

re.escape(pattern)

Escape all the characters in pattern except ASCII letters, numbers and '_'. This is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it. For example:

>>> print(re.escape('python.exe'))
python\.exe

>>> legal_chars = string.ascii_lowercase + string.digits + "!#$%&'*+-.^_`|~:"
>>> print('[%s]+' % re.escape(legal_chars))
[abcdefghijklmnopqrstuvwxyz0123456789\!\#\$\%\&\'\*\+\-\.\^_\`\|\~\:]+

>>> operators = ['+', '-', '*', '/', '**']
>>> print('|'.join(map(re.escape, sorted(operators, reverse=True))))
\/|\-|\+|\*\*|\*

This functions must not be used for the replacement string in sub() and subn(), only backslashes should be escaped. For example:

>>> digits_re = r'\d+'
>>> sample = '/usr/sbin/sendmail - 0 errors, 12 warnings'
>>> print(re.sub(digits_re, digits_re.replace('\\', r'\\'), sample))
/usr/sbin/sendmail - \d+ errors, \d+ warnings

在 3.3 版更改: '_' 不再被转义。

re.purge()

清除正则表达式缓存。

exception re.error(msg, pattern=None, pos=None)

raise 一个例外。当传递到函数的字符串不是一个有效正则表达式的时候（比如，包含一个不匹配的括号）或者其他错误在编译时或匹配时产生。如果字符串不包含样式匹配，是不会被视为错误的。错误实例有以下附加属性：

msg

未格式化的错误消息。

pattern

正则表达式样式。

pos

编译失败的 pattern 的位置索引（可以是 None ）。

lineno

对应 pos (可以是 None) 的行号。

colno

对应 pos (可以是 None) 的列号。

在 3.5 版更改: 添加了附加属性。

6.2.3. 正则表达式对象 （正则对象）

编译后的正则表达式对象支持以下方法和属性：

regex.search(string[, pos[, endpos]])

扫描整个 string 寻找第一个匹配的位置， 并返回一个相应的 匹配对象。如果没有匹配，就返回 None ；注意它和零长度匹配是不同的。

可选的第二个参数 pos 给出了字符串中开始搜索的位置索引；默认为 0，它不完全等价于字符串切片； '^' 样式字符匹配字符串真正的开头，和换行符后面的第一个字符，但不会匹配索引规定开始的位置。

可选参数 endpos 限定了字符串搜索的结束；它假定字符串长度到 endpos ， 所以只有从 pos 到 endpos - 1 的字符会被匹配。如果 endpos 小于 pos，就不会有匹配产生；另外，如果 rx 是一个编译后的正则对象， rx.search(string, 0, 50) 等价于 rx.search(string[:50], 0)。

>>> pattern = re.compile("d")
>>> pattern.search("dog")     # Match at index 0
<_sre.SRE_Match object; span=(0, 1), match='d'>
>>> pattern.search("dog", 1)  # No match; search doesn't include the "d"

regex.match(string[, pos[, endpos]])

如果 string 的 开始位置 能够找到这个正则样式的任意个匹配，就返回一个相应的 匹配对象。如果不匹配，就返回 None ；注意它与零长度匹配是不同的。

The optional pos and endpos parameters have the same meaning as for the search() method.

>>> pattern = re.compile("o")
>>> pattern.match("dog")      # No match as "o" is not at the start of "dog".
>>> pattern.match("dog", 1)   # Match as "o" is the 2nd character of "dog".
<_sre.SRE_Match object; span=(1, 2), match='o'>

If you want to locate a match anywhere in string, use search() instead (see also search() vs. match()).

regex.fullmatch(string[, pos[, endpos]])

如果整个 string 匹配这个正则表达式，就返回一个相应的 匹配对象 。 否则就返回 None ； 注意跟零长度匹配是不同的。

The optional pos and endpos parameters have the same meaning as for the search() method.

>>> pattern = re.compile("o[gh]")
>>> pattern.fullmatch("dog")      # No match as "o" is not at the start of "dog".
>>> pattern.fullmatch("ogre")     # No match as not the full string matches.
>>> pattern.fullmatch("doggie", 1, 3)   # Matches within given limits.
<_sre.SRE_Match object; span=(1, 3), match='og'>

3.4 新版功能.

regex.split(string, maxsplit=0)

等价于 split() 函数，使用了编译后的样式。

regex.findall(string[, pos[, endpos]])

类似函数 findall() ， 使用了编译后样式，但也可以接收可选参数 pos 和 endpos ，限制搜索范围，就像 search()。

regex.finditer(string[, pos[, endpos]])

类似函数 finiter() ， 使用了编译后样式，但也可以接收可选参数 pos 和 endpos ，限制搜索范围，就像 search()。

regex.sub(repl, string, count=0)

等价于 sub() 函数，使用了编译后的样式。

regex.subn(repl, string, count=0)

等价于 subn() 函数，使用了编译后的样式。

regex.flags

正则匹配标记。这是可以传递给 compile() 的参数，任何 (?…) 内联标记，隐性标记比如 UNICODE 的结合。

regex.groups

捕获组合的数量。

regex.groupindex

映射由 (?P<id>) 定义的命名符号组合和数字组合的字典。如果没有符号组，那字典就是空的。

regex.pattern

The pattern string from which the RE object was compiled.

6.2.4. 匹配对象

Match objects always have a boolean value of True. Since match() and search() return None when there is no match, you can test whether there was a match with a simple if statement:

match = re.search(pattern, string)
if match:
    process(match)

匹配对象支持以下方法和属性：

match.expand(template)

Return the string obtained by doing backslash substitution on the template string template, as done by the sub() method. Escapes such as \n are converted to the appropriate characters, and numeric backreferences (\1, \2) and named backreferences (\g<1>, \g<name>) are replaced by the contents of the corresponding group.

在 3.5 版更改: 不匹配的组合替换为空字符串。

match.group([group1, ...])

返回一个或者多个匹配的子组。如果只有一个参数，结果就是一个字符串，如果有多个参数，结果就是一个元组（每个参数对应一个项），如果没有参数，组1默认到0（整个匹配都被返回）。 如果一个组N 参数值为 0，相应的返回值就是整个匹配字符串；如果它是一个范围 [1..99]，结果就是相应的括号组字符串。如果一个组号是负数，或者大于样式中定义的组数，一个 IndexError 索引错误就 raise。如果一个组包含在样式的一部分，并被匹配多次，就返回最后一个匹配。:

>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m.group(0)       # The entire match
'Isaac Newton'
>>> m.group(1)       # The first parenthesized subgroup.
'Isaac'
>>> m.group(2)       # The second parenthesized subgroup.
'Newton'
>>> m.group(1, 2)    # Multiple arguments give us a tuple.
('Isaac', 'Newton')

如果正则表达式使用了 (?P<name>…) 语法， groupN 参数就也可能是命名组合的名字。如果一个字符串参数在样式中未定义为组合名，一个 IndexError 就 raise。

一个相对复杂的例子

>>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
>>> m.group('first_name')
'Malcolm'
>>> m.group('last_name')
'Reynolds'

命名组合同样可以通过索引值引用

>>> m.group(1)
'Malcolm'
>>> m.group(2)
'Reynolds'

如果一个组匹配成功多次，就只返回最后一个匹配

>>> m = re.match(r"(..)+", "a1b2c3")  # Matches 3 times.
>>> m.group(1)                        # Returns only the last match.
'c3'

match.__getitem__(g)

这个等价于 m.group(g)。这允许更方便的引用一个匹配

>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m[0]       # The entire match
'Isaac Newton'
>>> m[1]       # The first parenthesized subgroup.
'Isaac'
>>> m[2]       # The second parenthesized subgroup.
'Newton'

3.6 新版功能.

match.groups(default=None)

返回一个元组，包含所有匹配的子组，在样式中出现的从1到任意多的组合。 default 参数用于不参与匹配的情况，默认为 None。

例如

>>> m = re.match(r"(\d+)\.(\d+)", "24.1632")
>>> m.groups()
('24', '1632')

如果我们使小数点可选，那么不是所有的组都会参与到匹配当中。这些组合默认会返回一个 None ，除非指定了 default 参数。

>>> m = re.match(r"(\d+)\.?(\d+)?", "24")
>>> m.groups()      # Second group defaults to None.
('24', None)
>>> m.groups('0')   # Now, the second group defaults to '0'.
('24', '0')

match.groupdict(default=None)

返回一个字典，包含了所有的 命名 子组。key就是组名。 default 参数用于不参与匹配的组合；默认为 None。 例如

>>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
>>> m.groupdict()
{'first_name': 'Malcolm', 'last_name': 'Reynolds'}

match.start([group])

match.end([group])

返回 group 匹配到的字串的开始和结束标号。group 默认为0（意思是整个匹配的子串）。如果 group 存在，但未产生匹配，就返回 -1 。对于一个匹配对象 m， 和一个未参与匹配的组 g ，组 g (等价于 m.group(g))产生的匹配是

m.string[m.start(g):m.end(g)]

注意 m.start(group) 将会等于 m.end(group) ，如果 group 匹配一个空字符串的话。比如，在 m = re.search('b(c?)', 'cba') 之后，m.start(0) 为 1, m.end(0) 为 2, m.start(1) 和 m.end(1) 都是 2, m.start(2) raise 一个 IndexError 例外。

这个例子会从email地址中移除掉 remove_this

>>> email = "tony@tiremove_thisger.net"
>>> m = re.search("remove_this", email)
>>> email[:m.start()] + email[m.end():]
'tony@tiger.net'

match.span([group])

对于一个匹配 m ， 返回一个二元组 (m.start(group), m.end(group)) 。 注意如果 group 没有在这个匹配中，就返回 (-1, -1) 。group 默认为0，就是整个匹配。

match.pos

The value of pos which was passed to the search() or match() method of a regex object. This is the index into the string at which the RE engine started looking for a match.

match.endpos

The value of endpos which was passed to the search() or match() method of a regex object. This is the index into the string beyond which the RE engine will not go.

match.lastindex

捕获组的最后一个匹配的整数索引值，或者 None 如果没有匹配产生的话。比如，对于字符串 'ab'，表达式 (a)b, ((a)(b)), 和 ((ab)) 将得到 lastindex == 1 ， 而 (a)(b) 会得到 lastindex == 2 。

match.lastgroup

最后一个匹配的命名组名字，或者 None 如果没有产生匹配的话。

match.re

The regular expression object whose match() or search() method produced this match instance.

match.string

The string passed to match() or search().

6.2.5. 正则表达式例子

6.2.5.1. 检查对子

在这个例子里，我们使用以下辅助函数来更好的显示匹配对象：

def displaymatch(match):
    if match is None:
        return None
    return '<Match: %r, groups=%r>' % (match.group(), match.groups())

假设你在写一个扑克程序，一个玩家的一手牌为五个字符的串，每个字符表示一张牌，”a” 就是 A, “k” K， “q” Q, “j” J, “t” 为 10, “2” 到 “9” 表示2 到 9。

要看给定的字符串是否有效，我们可以按照以下步骤

>>> valid = re.compile(r"^[a2-9tjqk]{5}$")
>>> displaymatch(valid.match("akt5q"))  # Valid.
"<Match: 'akt5q', groups=()>"
>>> displaymatch(valid.match("akt5e"))  # Invalid.
>>> displaymatch(valid.match("akt"))    # Invalid.
>>> displaymatch(valid.match("727ak"))  # Valid.
"<Match: '727ak', groups=()>"

最后一手牌，"727ak" ，包含了一个对子，或者两张同样数值的牌。要用正则表达式匹配它，应该使用向后引用如下

>>> pair = re.compile(r".*(.).*\1")
>>> displaymatch(pair.match("717ak"))     # Pair of 7s.
"<Match: '717', groups=('7',)>"
>>> displaymatch(pair.match("718ak"))     # No pairs.
>>> displaymatch(pair.match("354aa"))     # Pair of aces.
"<Match: '354aa', groups=('a',)>"

To find out what card the pair consists of, one could use the group() method of the match object in the following manner:

>>> pair.match("717ak").group(1)
'7'

# Error because re.match() returns None, which doesn't have a group() method:
>>> pair.match("718ak").group(1)
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    re.match(r".*(.).*\1", "718ak").group(1)
AttributeError: 'NoneType' object has no attribute 'group'

>>> pair.match("354aa").group(1)
'a'

6.2.5.2. 模拟 scanf()

Python 目前没有一个类似c函数 scanf() 的替代品。正则表达式通常比 scanf() 格式字符串要更强大一些，但也带来更多复杂性。下面的表格提供了 scanf() 格式符和正则表达式大致相同的映射。

scanf() 格式符

正则表达式

%c

.

%5c

.{5}

%d

[-+]?\d+

%e, %E, %f, %g

[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?

%i

[-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)

%o

[-+]?[0-7]+

%s

\S+

%u

\d+

%x, %X

[-+]?(0[xX])?[\dA-Fa-f]+

从文件名和数字提取字符串

/usr/sbin/sendmail - 0 errors, 4 warnings

你可以使用 scanf() 格式化

%s - %d errors, %d warnings

等价的正则表达式是：

(\S+) - (\d+) errors, (\d+) warnings

6.2.5.3. search() vs. match()

Python 提供了两种不同的操作：基于 re.match() 检查字符串开头，或者 re.search() 检查字符串的任意位置（默认Perl中的行为）。

例如

>>> re.match("c", "abcdef")    # No match
>>> re.search("c", "abcdef")   # Match
<_sre.SRE_Match object; span=(2, 3), match='c'>

在 search() 中，可以用 '^' 作为开始来限制匹配到字符串的首位

>>> re.match("c", "abcdef")    # No match
>>> re.search("^c", "abcdef")  # No match
>>> re.search("^a", "abcdef")  # Match
<_sre.SRE_Match object; span=(0, 1), match='a'>

注意 MULTILINE 多行模式中函数 match() 只匹配字符串的开始，但使用 search() 和以 '^' 开始的正则表达式会匹配每行的开始

>>> re.match('X', 'A\nB\nX', re.MULTILINE)  # No match
>>> re.search('^X', 'A\nB\nX', re.MULTILINE)  # Match
<_sre.SRE_Match object; span=(4, 5), match='X'>

6.2.5.4. 建立一个电话本

split() 将字符串用参数传递的样式分隔开。这个方法对于转换文本数据到易读而且容易修改的数据结构，是很有用的，如下面的例子证明。

首先，这里是输入。通常是一个文件，这里我们用三引号字符串语法

>>> text = """Ross McFluff: 834.345.1254 155 Elm Street
...
... Ronald Heathmore: 892.345.3428 436 Finley Avenue
... Frank Burger: 925.541.7625 662 South Dogwood Way
...
...
... Heather Albrecht: 548.326.4584 919 Park Place"""

条目用一个或者多个换行符分开。现在我们将字符串转换为一个列表，每个非空行都有一个条目:

>>> entries = re.split("\n+", text)
>>> entries
['Ross McFluff: 834.345.1254 155 Elm Street',
'Ronald Heathmore: 892.345.3428 436 Finley Avenue',
'Frank Burger: 925.541.7625 662 South Dogwood Way',
'Heather Albrecht: 548.326.4584 919 Park Place']

最终，将每个条目分割为一个由名字、姓氏、电话号码和地址组成的列表。我们为 split() 使用了 maxsplit 形参，因为地址中包含有被我们作为分割模式的空格符:

>>> [re.split(":? ", entry, 3) for entry in entries]
[['Ross', 'McFluff', '834.345.1254', '155 Elm Street'],
['Ronald', 'Heathmore', '892.345.3428', '436 Finley Avenue'],
['Frank', 'Burger', '925.541.7625', '662 South Dogwood Way'],
['Heather', 'Albrecht', '548.326.4584', '919 Park Place']]

:? 样式匹配姓后面的冒号，因此它不出现在结果列表中。如果 maxsplit 设置为 4 ，我们还可以从地址中获取到房间号:

>>> [re.split(":? ", entry, 4) for entry in entries]
[['Ross', 'McFluff', '834.345.1254', '155', 'Elm Street'],
['Ronald', 'Heathmore', '892.345.3428', '436', 'Finley Avenue'],
['Frank', 'Burger', '925.541.7625', '662', 'South Dogwood Way'],
['Heather', 'Albrecht', '548.326.4584', '919', 'Park Place']]

6.2.5.5. 文字整理

sub() 替换字符串中出现的样式的每一个实例。这个例子证明了使用 sub() 来整理文字，或者随机化每个字符的位置，除了首位和末尾字符

>>> def repl(m):
...     inner_word = list(m.group(2))
...     random.shuffle(inner_word)
...     return m.group(1) + "".join(inner_word) + m.group(3)
>>> text = "Professor Abdolmalek, please report your absences promptly."
>>> re.sub(r"(\w)(\w+)(\w)", repl, text)
'Poefsrosr Aealmlobdk, pslaee reorpt your abnseces plmrptoy.'
>>> re.sub(r"(\w)(\w+)(\w)", repl, text)
'Pofsroser Aodlambelk, plasee reoprt yuor asnebces potlmrpy.'

6.2.5.6. 找到所有副词

findall() 匹配样式 所有 的出现，不仅是像 search() 中的第一个匹配。比如，如果一个作者希望找到文字中的所有副词，他可能会按照以下方法用 findall()

>>> text = "He was carefully disguised but captured quickly by police."
>>> re.findall(r"\w+ly", text)
['carefully', 'quickly']

6.2.5.7. 找到所有副词和位置

如果需要匹配样式的更多信息， finditer() 可以起到作用，它提供了 匹配对象 作为返回值，而不是字符串。继续上面的例子，如果一个作者希望找到所有副词和它的位置，可以按照下面方法使用 finditer()

>>> text = "He was carefully disguised but captured quickly by police."
>>> for m in re.finditer(r"\w+ly", text):
...     print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0)))
07-16: carefully
40-47: quickly

6.2.5.8. 原始字符记法

原始字符串记法 (r"text") 保持正则表达式正常。否则，每个正则式里的反斜杠('\') 都必须前缀一个反斜杠来转义。比如，下面两行代码功能就是完全一致的

>>> re.match(r"\W(.)\1\W", " ff ")
<_sre.SRE_Match object; span=(0, 4), match=' ff '>
>>> re.match("\\W(.)\\1\\W", " ff ")
<_sre.SRE_Match object; span=(0, 4), match=' ff '>

当需要匹配一个字符反斜杠，它必须在正则表达式中转义。在原始字符串记法，就是 r"\\"。否则就必须用 "\\\\"，来表示同样的意思

>>> re.match(r"\\", r"\\")
<_sre.SRE_Match object; span=(0, 1), match='\\'>
>>> re.match("\\\\", r"\\")
<_sre.SRE_Match object; span=(0, 1), match='\\'>

6.2.5.9. 写一个词法分析器

一个 词法器或词法分析器 [https://en.wikipedia.org/wiki/Lexical_analysis] 分析字符串，并分类成目录组。 这是写一个编译器或解释器的第一步。

文字目录是由正则表达式指定的。这个技术是通过将这些样式合并为一个主正则式，并且循环匹配来实现的

import collections
import re

Token = collections.namedtuple('Token', ['type', 'value', 'line', 'column'])

def tokenize(code):
    keywords = {'IF', 'THEN', 'ENDIF', 'FOR', 'NEXT', 'GOSUB', 'RETURN'}
    token_specification = [
        ('NUMBER',   r'\d+(\.\d*)?'),  # Integer or decimal number
        ('ASSIGN',   r':='),           # Assignment operator
        ('END',      r';'),            # Statement terminator
        ('ID',       r'[A-Za-z]+'),    # Identifiers
        ('OP',       r'[+\-*/]'),      # Arithmetic operators
        ('NEWLINE',  r'\n'),           # Line endings
        ('SKIP',     r'[ \t]+'),       # Skip over spaces and tabs
        ('MISMATCH', r'.'),            # Any other character
    ]
    tok_regex = '|'.join('(?P<%s>%s)' % pair for pair in token_specification)
    line_num = 1
    line_start = 0
    for mo in re.finditer(tok_regex, code):
        kind = mo.lastgroup
        value = mo.group()
        column = mo.start() - line_start
        if kind == 'NUMBER':
            value = float(value) if '.' in value else int(value)
        elif kind == 'ID' and value in keywords:
            kind = value
        elif kind == 'NEWLINE':
            line_start = mo.end()
            line_num += 1
            continue
        elif kind == 'SKIP':
            continue
        elif kind == 'MISMATCH':
            raise RuntimeError(f'{value!r} unexpected on line {line_num}')
        yield Token(kind, value, line_num, column)

statements = '''
    IF quantity THEN
        total := total + price * quantity;
        tax := price * 0.05;
    ENDIF;
'''

for token in tokenize(statements):
    print(token)

这个词法器产生以下输出

Token(type='IF', value='IF', line=2, column=4)
Token(type='ID', value='quantity', line=2, column=7)
Token(type='THEN', value='THEN', line=2, column=16)
Token(type='ID', value='total', line=3, column=8)
Token(type='ASSIGN', value=':=', line=3, column=14)
Token(type='ID', value='total', line=3, column=17)
Token(type='OP', value='+', line=3, column=23)
Token(type='ID', value='price', line=3, column=25)
Token(type='OP', value='*', line=3, column=31)
Token(type='ID', value='quantity', line=3, column=33)
Token(type='END', value=';', line=3, column=41)
Token(type='ID', value='tax', line=4, column=8)
Token(type='ASSIGN', value=':=', line=4, column=12)
Token(type='ID', value='price', line=4, column=15)
Token(type='OP', value='*', line=4, column=21)
Token(type='NUMBER', value=0.05, line=4, column=23)
Token(type='END', value=';', line=4, column=27)
Token(type='ENDIF', value='ENDIF', line=5, column=4)
Token(type='END', value=';', line=5, column=9)

Frie09

Friedl, Jeffrey. Mastering Regular Expressions. 第三版, O’Reilly Media, 2009. 第三版不再使用Python, 但第一版提供了编写正则表达式的良好细节。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

6.3. difflib — 计算差异的辅助工具

源代码: Lib/difflib.py [https://github.com/python/cpython/tree/3.6/Lib/difflib.py]

此模块提供用于比较序列的类和函数。 例如，它可以用于比较文件，并可以产生各种格式的不同信息，包括 HTML 和上下文以及统一格式的差异点。 有关目录和文件的比较，请参见 filecmp 模块。

class difflib.SequenceMatcher

这是一个灵活的类，可用于比较任何类型的序列对，只要序列元素为 hashable 对象。 其基本算法要早于由 Ratcliff 和 Obershelp 于 1980 年代末期发表并以“格式塔模式匹配”的夸张名称命名的算法，并且更加有趣一些。 其思路是找到不包含“垃圾”元素的最长连续匹配子序列；所谓“垃圾”元素是指其在某种意义上没有价值，例如空白行或空白符。 （处理垃圾元素是对 Ratcliff 和 Obershelp 算法的一个扩展。） 然后同样的思路将递归地应用于匹配序列的左右序列片段。 这并不能产生最小编辑序列，但确实能产生在人们看来“正确”的匹配。

耗时: 基本 Ratcliff-Obershelp 算法在最坏情况下为立方时间而在一般情况下为平方时间。 SequenceMatcher 在最坏情况下为平方时间而在一般情况下的行为受到序列中有多少相同元素这一因素的微妙影响；在最佳情况下则为线性时间。

自动垃圾启发式计算: SequenceMatcher 支持使用启发式计算来自动将特定序列项视为垃圾。 这种启发式计算会统计每个单独项在序列中出现的次数。 如果某一项（在第一项之后）的重复次数超过序列长度的 1% 并且序列长度至少有 200 项，该项会被标记为“热门”并被视为序列匹配中的垃圾。 这种启发式计算可以通过在创建 SequenceMatcher 时将 autojunk 参数设为 False 来关闭。

3.2 新版功能: autojunk 形参。

class difflib.Differ

这个类的作用是比较由文本行组成的序列，并产生可供人阅读的差异或增量信息。 Differ 统一使用 SequenceMatcher 来完成行序列的比较以及相似（接近匹配）行内部字符序列的比较。

Differ 增量的每一行均以双字母代码打头：

双字母代码

含义

'- '

行为序列 1 所独有

'+ '

行为序列 2 所独有

'  '

行在两序列中相同

'? '

行不存在于任一输入序列

以 ‘?’ 打头的行尝试将视线引至行以外而不存在于任一输入序列的差异。 如果序列包含制表符则这些行可能会令人感到迷惑。

class difflib.HtmlDiff

这个类可用于创建 HTML 表格（或包含表格的完整 HTML 文件）以并排地逐行显示文本比较，行间与行外的更改将突出显示。 此表格可以基于完全或上下文差异模式来生成。

这个类的构造函数：

__init__(tabsize=8, wrapcolumn=None, linejunk=None, charjunk=IS_CHARACTER_JUNK)

初始化 HtmlDiff 的实例。

tabsize 是一个可选关键字参数，指定制表位的间隔，默认值为 8。

wrapcolumn 是一个可选关键字参数，指定行文本自动打断并换行的列位置，默认值为 None 表示不自动换行。

linejunk 和 charjunk 均是可选关键字参数，会传入 ndiff() (被 HtmlDiff 用来生成并排显示的 HTML 差异)。 请参阅 ndiff() 文档了解参数默认值及其说明。

下列是公开的方法

make_file(fromlines, tolines, fromdesc='', todesc='', context=False, numlines=5, *, charset='utf-8')

比较 fromlines 和 tolines (字符串列表) 并返回一个字符串，表示一个完整 HTML 文件，其中包含各行差异的表格，行间与行外的更改将突出显示。

fromdesc 和 todesc 均是可选关键字参数，指定来源/目标文件的列标题字符串（默认均为空白字符串）。

context 和 numlines 均是可选关键字参数。 当只要显示上下文差异时就将 context 设为 True，否则默认值 False 为显示完整文件。 numlines 默认为 5。 当 context 为 True 时 numlines 将控制围绕突出显示差异部分的上下文行数。 当 context 为 False 时 numlines 将控制在使用 “next” 超链接时突出显示差异部分之前所显示的行数（设为零则会导致 “next” 超链接将下一个突出显示差异部分放在浏览器顶端，不添加任何前导上下文）。

在 3.5 版更改: 增加了 charset 关键字参数。 HTML 文档的默认字符集从 'ISO-8859-1' 更改为 'utf-8'。

make_table(fromlines, tolines, fromdesc='', todesc='', context=False, numlines=5)

比较 fromlines 和 tolines (字符串列表) 并返回一个字符串，表示一个包含各行差异的完整 HTML 表格，行间与行外的更改将突出显示。

此方法的参数与 make_file() 方法的相同。

Tools/scripts/diff.py 是这个类的命令行前端，其中包含一个很好的使用示例。

difflib.context_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n')

比较 a 和 b (字符串列表)；返回上下文差异格式的增量信息 (一个产生增量行的 generator)。

所谓上下文差异是一种只显示有更改的行再加几个上下文行的紧凑形式。 更改被显示为之前/之后的样式。 上下文行数由 n 设定，默认为三行。

默认情况下，差异控制行（以 *** or --- 表示）是通过末尾换行符来创建的。 这样做的好处是从 io.IOBase.readlines() 创建的输入将得到适用于 io.IOBase.writelines() 的差异信息，因为输入和输出都带有末尾换行符。

对于没有末尾换行符的输入，应将 lineterm 参数设为 ""，这样输出内容将统一不带换行符。

上下文差异格式通常带有一个记录文件名和修改时间的标头。 这些信息的部分或全部可以使用字符串 fromfile, tofile, fromfiledate 和 tofiledate 来指定。 修改时间通常以 ISO 8601 格式表示。 如果未指定，这些字符串默认为空。

>>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
>>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
>>> sys.stdout.writelines(context_diff(s1, s2, fromfile='before.py', tofile='after.py'))
*** before.py
--- after.py
***************
*** 1,4 ****
! bacon
! eggs
! ham
  guido
--- 1,4 ----
! python
! eggy
! hamster
  guido

请参阅 difflib 的命令行接口 获取更详细的示例。

difflib.get_close_matches(word, possibilities, n=3, cutoff=0.6)

返回由最佳“近似”匹配构成的列表。 word 为一个指定目标近似匹配的序列（通常为字符串），possibilities 为一个由用于匹配 word 的序列构成的列表（通常为字符串列表）。

可选参数 n (默认为 3) 指定最多返回多少个近似匹配； n 必须大于 0.

可选参数 cutoff (默认为 0.6) 是一个 [0, 1] 范围内的浮点数。 与 word 相似度得分未达到该值的候选匹配将被忽略。

候选匹配中（不超过 n 个）的最佳匹配将以列表形式返回，按相似度得分排序，最相似的排在最前面。

>>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
['apple', 'ape']
>>> import keyword
>>> get_close_matches('wheel', keyword.kwlist)
['while']
>>> get_close_matches('pineapple', keyword.kwlist)
[]
>>> get_close_matches('accept', keyword.kwlist)
['except']

difflib.ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK)

比较 a 和 b (字符串列表)；返回 Differ 形式的增量信息 (一个产生增量行的 generator)。

可选关键字形参 linejunk 和 charjunk 均为过滤函数 (或为 None)：

linejunk: 此函数接受单个字符串参数，如果其为垃圾字符串则返回真值，否则返回假值。 默认为 None。 此外还有一个模块层级的函数 IS_LINE_JUNK()，它会过滤掉没有可见字符的行，除非该行添加了至多一个井号符 ('#') – 但是下层的 SequenceMatcher 类会动态分析哪些行的重复频繁到足以形成噪音，这通常会比使用此函数的效果更好。

charjunk: 此函数接受一个字符（长度为 1 的字符串)，如果其为垃圾字符则返回真值，否则返回假值。 默认为模块层级的函数 IS_CHARACTER_JUNK()，它会过滤掉空白字符（空格符或制表符；但包含换行符可不是个好主意！）。

Tools/scripts/ndiff.py 是这个函数的命令行前端。

>>> diff = ndiff('one\ntwo\nthree\n'.splitlines(keepends=True),
...              'ore\ntree\nemu\n'.splitlines(keepends=True))
>>> print(''.join(diff), end="")
- one
?  ^
+ ore
?  ^
- two
- three
?  -
+ tree
+ emu

difflib.restore(sequence, which)

返回两个序列中产生增量的那一个。

给出一个由 Differ.compare() 或 ndiff() 产生的 序列，提取出来自文件 1 或 2 (which 形参) 的行，去除行前缀。

示例:

>>> diff = ndiff('one\ntwo\nthree\n'.splitlines(keepends=True),
...              'ore\ntree\nemu\n'.splitlines(keepends=True))
>>> diff = list(diff) # materialize the generated delta into a list
>>> print(''.join(restore(diff, 1)), end="")
one
two
three
>>> print(''.join(restore(diff, 2)), end="")
ore
tree
emu

difflib.unified_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\n')

比较 a 和 b (字符串列表)；返回统一差异格式的增量信息 (一个产生增量行的 generator)。

所以统一差异是一种只显示有更改的行再加几个上下文行的紧凑形式。 更改被显示为内联的样式（而不是分开的之前/之后文本块）。 上下文行数由 n 设定，默认为三行。

默认情况下，差异控制行 (以 ---, +++ 或 @@ 表示) 是通过末尾换行符来创建的。 这样做的好处是从 io.IOBase.readlines() 创建的输入将得到适用于 io.IOBase.writelines() 的差异信息，因为输入和输出都带有末尾换行符。

对于没有末尾换行符的输入，应将 lineterm 参数设为 ""，这样输出内容将统一不带换行符。

上下文差异格式通常带有一个记录文件名和修改时间的标头。 这些信息的部分或全部可以使用字符串 fromfile, tofile, fromfiledate 和 tofiledate 来指定。 修改时间通常以 ISO 8601 格式表示。 如果未指定，这些字符串默认为空。

>>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
>>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
>>> sys.stdout.writelines(unified_diff(s1, s2, fromfile='before.py', tofile='after.py'))
--- before.py
+++ after.py
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
 guido

请参阅 difflib 的命令行接口 获取更详细的示例。

difflib.diff_bytes(dfunc, a, b, fromfile=b'', tofile=b'', fromfiledate=b'', tofiledate=b'', n=3, lineterm=b'\n')

使用 dfunc 比较 a 和 b (字节串对象列表)；产生以 dfunc 所返回格式表示的差异行列表（也是字节串）。 dfunc 必须是可调用对象，通常为 unified_diff() 或 context_diff()。

允许你比较编码未知或不一致的数据。 除 n 之外的所有输入都必须为字节串对象而非字符串。 作用方式为无损地将所有输入 (除 n 之外) 转换为字符串，并调用 dfunc(a, b, fromfile, tofile, fromfiledate, tofiledate, n, lineterm)。 dfunc 的输出会被随即转换回字节串，这样你所得到的增量行将具有与 a 和 b 相同的未知/不一致编码。

3.5 新版功能.

difflib.IS_LINE_JUNK(line)

Return true for ignorable lines. The line line is ignorable if line is blank or contains a single '#', otherwise it is not ignorable. Used as a default for parameter linejunk in ndiff() in older versions.

difflib.IS_CHARACTER_JUNK(ch)

Return true for ignorable characters. The character ch is ignorable if ch is a space or tab, otherwise it is not ignorable. Used as a default for parameter charjunk in ndiff().

参见

模式匹配：格式塔方法 [http://www.drdobbs.com/database/pattern-matching-the-gestalt-approach/184407970]

John W. Ratcliff 和 D. E. Metzener 对于一种类似算法的讨论。 此文于 1988 年 7 月发表于 Dr. Dobb’s Journal [http://www.drdobbs.com/]。

6.3.1. SequenceMatcher 对象

SequenceMatcher 类具有这样的构造器：

class difflib.SequenceMatcher(isjunk=None, a='', b='', autojunk=True)

Optional argument isjunk must be None (the default) or a one-argument function that takes a sequence element and returns true if and only if the element is “junk” and should be ignored. Passing None for isjunk is equivalent to passing lambda x: 0; in other words, no elements are ignored. For example, pass:

lambda x: x in " \t"

如果你以字符序列的形式对行进行比较，并且不希望区分空格符或硬制表符。

可选参数 a 和 b 为要比较的序列；两者默认为空字符串。 两个序列的元素都必须为 hashable。

可选参数 autojunk 可用于启用自动垃圾启发式计算。

3.2 新版功能: autojunk 形参。

SequenceMatcher 对象接受三个数据属性: bjunk 是 b 当中 isjunk 为 True 的元素集合；bpopular 是被启发式计算（如果其未被禁用）视为热门候选的非垃圾元素集合；b2j 是将 b 当中剩余元素映射到一个它们出现位置列表的字典。 所有三个数据属性将在 b 通过 set_seqs() 或 set_seq2() 重置时被重置。

3.2 新版功能: bjunk 和 bpopular 属性。

SequenceMatcher 对象具有以下方法：

set_seqs(a, b)

设置要比较的两个序列。

SequenceMatcher 计算并缓存有关第二个序列的详细信息，这样如果你想要将一个序列与多个序列进行比较，可使用 set_seq2() 一次性地设置该常用序列并重复地对每个其他序列各调用一次 set_seq1()。

set_seq1(a)

设置要比较的第一个序列。 要比较的第二个序列不会改变。

set_seq2(b)

设置要比较的第二个序列。 要比较的第一个序列不会改变。

find_longest_match(alo, ahi, blo, bhi)

找出 a[alo:ahi] 和 b[blo:bhi] 中的最长匹配块。

如果 isjunk 被省略或为 None，find_longest_match() 将返回 (i, j, k) 使得 a[i:i+k] 等于 b[j:j+k]，其中 alo <= i <= i+k <= ahi 并且 blo <= j <= j+k <= bhi。 对于所有满足这些条件的 (i', j', k')，如果 i == i', j <= j' 也被满足，则附加条件 k >= k', i <= i'。 换句话说，对于所有最长匹配块，返回在 a 当中最先出现的一个，而对于在 a 当中最先出现的所有最长匹配块，则返回在 b 当中最先出现的一个。

>>> s = SequenceMatcher(None, " abcd", "abcd abcd")
>>> s.find_longest_match(0, 5, 0, 9)
Match(a=0, b=4, size=5)

如果提供了 isjunk，将按上述规则确定第一个最长匹配块，但额外附加不允许块内出现垃圾元素的限制。 然后将通过（仅）匹配两边的垃圾元素来尽可能地扩展该块。 这样结果块绝对不会匹配垃圾元素，除非同样的垃圾元素正好与有意义的匹配相邻。

这是与之前相同的例子，但是将空格符视为垃圾。 这将防止 ' abcd' 直接与第二个序列末尾的 ' abcd' 相匹配。 而只可以匹配 'abcd'，并且是匹配第二个序列最左边的 'abcd'：

>>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")
>>> s.find_longest_match(0, 5, 0, 9)
Match(a=1, b=0, size=4)

如果未找到匹配块，此方法将返回 (alo, blo, 0)。

此方法将返回一个 named tuple Match(a, b, size)。

get_matching_blocks()

返回描述非重叠匹配子序列的三元组列表。 每个三元组的形式为 (i, j, n)，其含义为 a[i:i+n] == b[j:j+n]。 这些三元组按 i 和 j 单调递增排列。

最后一个三元组用于占位，其值为 (len(a), len(b), 0)。 它是唯一 n == 0 的三元组。 如果 (i, j, n) 和 (i', j', n') 是在列表中相邻的三元组，且后者不是列表中的最后一个三元组，则 i+n < i' 或 j+n < j'；换句话说，相邻的三元组总是描述非相邻的相等块。

>>> s = SequenceMatcher(None, "abxcd", "abcd")
>>> s.get_matching_blocks()
[Match(a=0, b=0, size=2), Match(a=3, b=2, size=2), Match(a=5, b=4, size=0)]

get_opcodes()

返回描述如何将 a 变为 b 的 5 元组列表，每个元组的形式为 (tag, i1, i2, j1, j2)。 在第一个元组中 i1 == j1 == 0，而在其余的元组中 i1 等于前一个元组的 i2，并且 j1 也等于前一个元组的 j2。

tag 值为字符串，其含义如下：

值

含义

'replace'

a[i1:i2] 应由 b[j1:j2] 替换。

'delete'

a[i1:i2] 应被删除。 请注意在此情况下 j1 == j2。

'insert'

b[j1:j2] 应插入到 a[i1:i1]。 请注意在此情况下 i1 == i2。

'equal'

a[i1:i2] == b[j1:j2] (两个子序列相同)。

例如

>>> a = "qabxcd"
>>> b = "abycdf"
>>> s = SequenceMatcher(None, a, b)
>>> for tag, i1, i2, j1, j2 in s.get_opcodes():
...     print('{:7}   a[{}:{}] --> b[{}:{}] {!r:>8} --> {!r}'.format(
...         tag, i1, i2, j1, j2, a[i1:i2], b[j1:j2]))
delete    a[0:1] --> b[0:0]      'q' --> ''
equal     a[1:3] --> b[0:2]     'ab' --> 'ab'
replace   a[3:4] --> b[2:3]      'x' --> 'y'
equal     a[4:6] --> b[3:5]     'cd' --> 'cd'
insert    a[6:6] --> b[5:6]       '' --> 'f'

get_grouped_opcodes(n=3)

返回一个带有最多 n 行上下文的分组的 generator。

从 get_opcodes() 所返回的组开始，此方法会拆分出较小的更改簇并消除没有更改的间隔区域。

这些分组以与 get_opcodes() 相同的格式返回。

ratio()

返回一个取值范围 [0, 1] 的浮点数作为序列相似性度量。

其中 T 是两个序列中元素的总数量，M 是匹配的数量，即 2.0*M / T。 请注意如果两个序列完全相同则该值为 1.0，如果两者完全不同则为 0.0。

如果 get_matching_blocks() 或 get_opcodes() 尚未被调用则此方法运算消耗较大，在此情况下你可能需要先调用 quick_ratio() 或 real_quick_ratio() 来获取一个上界。

quick_ratio()

相对快速地返回一个 ratio() 的上界。

real_quick_ratio()

非常快速地返回一个 ratio() 的上界。

这三个返回匹配部分占字符总数的比率的方法可能由于不同的近似级别而给出不一样的结果，但是 quick_ratio() 和 real_quick_ratio() 总是会至少与 ratio() 一样大：

>>> s = SequenceMatcher(None, "abcd", "bcde")
>>> s.ratio()
0.75
>>> s.quick_ratio()
0.75
>>> s.real_quick_ratio()
1.0

6.3.2. SequenceMatcher 的示例

以下示例比较两个字符串，并将空格视为“垃圾”：

>>> s = SequenceMatcher(lambda x: x == " ",
...                     "private Thread currentThread;",
...                     "private volatile Thread currentThread;")

ratio() 返回一个 [0, 1] 范围内的整数作为两个序列相似性的度量。 根据经验，ratio() 值超过 0.6 就意味着两个序列是近似匹配的：

>>> print(round(s.ratio(), 3))
0.866

如果你只对两个序列相匹配的位置感兴趣，则 get_matching_blocks() 就很方便：

>>> for block in s.get_matching_blocks():
...     print("a[%d] and b[%d] match for %d elements" % block)
a[0] and b[0] match for 8 elements
a[8] and b[17] match for 21 elements
a[29] and b[38] match for 0 elements

请注意 get_matching_blocks() 返回的最后一个元组总是只用于占位的 (len(a), len(b), 0)，这也是元组末尾元素（匹配的元素数量）为 0 的唯一情况。

如果你想要知道如何将第一个序列转成第二个序列，可以使用 get_opcodes():

>>> for opcode in s.get_opcodes():
...     print("%6s a[%d:%d] b[%d:%d]" % opcode)
 equal a[0:8] b[0:8]
insert a[8:8] b[8:17]
 equal a[8:29] b[17:38]

参见

此模块中的 get_close_matches() 函数显示了如何基于 SequenceMatcher 构建简单的代码来执行有用的功能。

使用 SequenceMatcher 构建小型应用的 简易版本控制方案 [https://code.activestate.com/recipes/576729/]。

6.3.3. Differ 对象

请注意 Differ 所生成的增量并不保证是 最小 差异。 相反，最小差异往往是违反直觉的，因为它们会同步任何可能的地方，有时甚至意外产生相距 100 页的匹配。 将同步点限制为连续匹配保留了一些局部性概念，这偶尔会带来产生更长差异的代价。

Differ 类具有这样的构造器：

class difflib.Differ(linejunk=None, charjunk=None)

可选关键字形参 linejunk 和 charjunk 均为过滤函数 (或为 None)：

linejunk: 接受单个字符串作为参数的函数，如果其为垃圾字符串则返回真值。 默认值为 None，意味着没有任何行会被视为垃圾行。

charjunk: 接受单个字符（长度为 1 的字符串）作为参数的函数，如果其为垃圾字符则返回真值。 默认值为 None，意味着没有任何字符会被视为垃圾字符。

这些垃圾过滤函数可加快查找差异的匹配速度，并且不会导致任何差异行或字符被忽略。 请阅读 find_longest_match() 方法的 isjunk 形参的描述了解详情。

Differ 对象是通过一个单独方法来使用（生成增量）的：

compare(a, b)

比较两个由行组成的序列，并生成增量（一个由行组成的序列）。

每个序列必须包含一个以换行符结尾的单行字符串。 这样的序列可以通过文件类对象的 readlines() 方法来获取。 所生成的增量同样由以换行符结尾的字符串构成，可以通过文件类对象的 writelines() 方法原样打印出来。

6.3.4. Differ 示例

此示例比较两段文本。 首先我们设置文本为以换行符结尾的单行字符串构成的序列（这样的序列也可以通过文件类对象的 readlines() 方法来获取）：

>>> text1 = '''  1. Beautiful is better than ugly.
...   2. Explicit is better than implicit.
...   3. Simple is better than complex.
...   4. Complex is better than complicated.
... '''.splitlines(keepends=True)
>>> len(text1)
4
>>> text1[0][-1]
'\n'
>>> text2 = '''  1. Beautiful is better than ugly.
...   3.   Simple is better than complex.
...   4. Complicated is better than complex.
...   5. Flat is better than nested.
... '''.splitlines(keepends=True)

接下来我们实例化一个 Differ 对象：

>>> d = Differ()

请注意在实例化 Differ 对象时我们可以传入函数来过滤掉“垃圾”行和字符。 详情参见 Differ() 构造器说明。

最后，我们比较两个序列：

>>> result = list(d.compare(text1, text2))

result 是一个字符串列表，让我们将其美化打印出来：

>>> from pprint import pprint
>>> pprint(result)
['    1. Beautiful is better than ugly.\n',
 '-   2. Explicit is better than implicit.\n',
 '-   3. Simple is better than complex.\n',
 '+   3.   Simple is better than complex.\n',
 '?     ++\n',
 '-   4. Complex is better than complicated.\n',
 '?            ^                     ---- ^\n',
 '+   4. Complicated is better than complex.\n',
 '?           ++++ ^                      ^\n',
 '+   5. Flat is better than nested.\n']

作为单独的多行字符串显示出来则是这样：

>>> import sys
>>> sys.stdout.writelines(result)
    1. Beautiful is better than ugly.
-   2. Explicit is better than implicit.
-   3. Simple is better than complex.
+   3.   Simple is better than complex.
?     ++
-   4. Complex is better than complicated.
?            ^                     ---- ^
+   4. Complicated is better than complex.
?           ++++ ^                      ^
+   5. Flat is better than nested.

6.3.5. difflib 的命令行接口

这个实例演示了如何使用 difflib 来创建一个类似于 diff 的工具。 它同样包含在 Python 源码发布包中，文件名为 Tools/scripts/diff.py。

#!/usr/bin/env python3
""" Command line interface to difflib.py providing diffs in four formats:

* ndiff:    lists every line and highlights interline changes.
* context:  highlights clusters of changes in a before/after format.
* unified:  highlights clusters of changes in an inline format.
* html:     generates side by side comparison with change highlights.

"""

import sys, os, difflib, argparse
from datetime import datetime, timezone

def file_mtime(path):
    t = datetime.fromtimestamp(os.stat(path).st_mtime,
                               timezone.utc)
    return t.astimezone().isoformat()

def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('-c', action='store_true', default=False,
                        help='Produce a context format diff (default)')
    parser.add_argument('-u', action='store_true', default=False,
                        help='Produce a unified format diff')
    parser.add_argument('-m', action='store_true', default=False,
                        help='Produce HTML side by side diff '
                             '(can use -c and -l in conjunction)')
    parser.add_argument('-n', action='store_true', default=False,
                        help='Produce a ndiff format diff')
    parser.add_argument('-l', '--lines', type=int, default=3,
                        help='Set number of context lines (default 3)')
    parser.add_argument('fromfile')
    parser.add_argument('tofile')
    options = parser.parse_args()

    n = options.lines
    fromfile = options.fromfile
    tofile = options.tofile

    fromdate = file_mtime(fromfile)
    todate = file_mtime(tofile)
    with open(fromfile) as ff:
        fromlines = ff.readlines()
    with open(tofile) as tf:
        tolines = tf.readlines()

    if options.u:
        diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
    elif options.n:
        diff = difflib.ndiff(fromlines, tolines)
    elif options.m:
        diff = difflib.HtmlDiff().make_file(fromlines,tolines,fromfile,tofile,context=options.c,numlines=n)
    else:
        diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)

    sys.stdout.writelines(diff)

if __name__ == '__main__':
    main()

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

6.4. textwrap — 文本自动换行与填充

源代码: Lib/textwrap.py [https://github.com/python/cpython/tree/3.6/Lib/textwrap.py]

textwrap 模块提供了一些快捷函数，以及可以完成所有工作的类 TextWrapper。 如果你只是要对一两个文本字符串进行自动换行或填充，快捷函数应该就够用了；否则的话，你应该使用 TextWrapper 的实例来提高效率。

textwrap.wrap(text, width=70, **kwargs)

对 text (字符串) 中的单独段落自动换行以使每行长度最多为 width 个字符。 返回由输出行组成的列表，行尾不带换行符。

可选的关键字参数对应于 TextWrapper 的实例属性，具体文档见下。 width 默认为 70。

请参阅 TextWrapper.wrap() 方法了解有关 wrap() 行为的详细信息。

textwrap.fill(text, width=70, **kwargs)

对 text 中的单独段落自动换行，并返回一个包含被自动换行段落的单独字符串。 fill() 是以下语句的快捷方式

"\n".join(wrap(text, ...))

特别要说明的是，fill() 接受与 wrap() 完全相同的关键字参数。

textwrap.shorten(text, width, **kwargs)

折叠并截短给定的 text 以符合给定的 width。

首先将折叠 text 中的空格（所有连续空格替换为单个空格）。 如果结果能适合 width 则将其返回。 否则将丢弃足够数量的末尾单词以使得剩余单词加 placeholder 能适合 width:

>>> textwrap.shorten("Hello  world!", width=12)
'Hello world!'
>>> textwrap.shorten("Hello  world!", width=11)
'Hello [...]'
>>> textwrap.shorten("Hello world", width=10, placeholder="...")
'Hello...'

可选的关键字参数对应于 TextWrapper 的实际属性，具体见下文。 请注意文本在被传入 TextWrapper 的 fill() 函数之前会被折叠，因此改变 tabsize, expand_tabs, drop_whitespace 和 replace_whitespace 的值将没有任何效果。

3.4 新版功能.

textwrap.dedent(text)

移除 text 中每一行的任何相同前缀空白符。

这可以用来清除三重引号字符串行左侧空格，而仍然在源码中显示为缩进格式。

请注意制表符和空格符都被视为是空白符，但它们并不相等：以下两行 "  hello" 和 "\thello" 不会被视为具有相同的前缀空白符。

例如

def test():
    # end first line with \ to avoid the empty line!
    s = '''\
    hello
      world
    '''
    print(repr(s))          # prints '    hello\n      world\n    '
    print(repr(dedent(s)))  # prints 'hello\n  world\n'

textwrap.indent(text, prefix, predicate=None)

将 prefix 添加到 text 中选定行的开头。

通过调用 text.splitlines(True) 来对行进行拆分。

默认情况下，prefix 会被添加到所有不是只由空白符（包括任何行结束符）组成的行。

例如

>>> s = 'hello\n\n \nworld'
>>> indent(s, '  ')
'  hello\n\n \n  world'

可选的 predicate 参数可用来控制哪些行要缩进。 例如，可以很容易地为空行或只有空白符的行添加 prefix:

>>> print(indent(s, '+ ', lambda line: True))
+ hello
+
+
+ world

3.3 新版功能.

wrap(), fill() 和 shorten() 的作用方式为创建一个 TextWrapper 实例并在其上调用单个方法。 该实例不会被重用，因此对于要使用 wrap() 和/或 fill() 来处理许多文本字符串的应用来说，创建你自己的 TextWrapper 对象可能会更有效率。

文本最好在空白符位置自动换行，包括带连字符单词的连字符之后；长单词仅在必要时会被拆分，除非 TextWrapper.break_long_words 被设为假值。

class textwrap.TextWrapper(**kwargs)

TextWrapper 构造器接受多个可选的关键字参数。 每个关键字参数对应一个实例属性，比如说

wrapper = TextWrapper(initial_indent="* ")

就相当于

wrapper = TextWrapper()
wrapper.initial_indent = "* "

你可以多次重用相同的 TextWrapper 对象，并且你也可以在使用期间通过直接向实例属性赋值来修改它的任何选项。

TextWrapper 的实例属性（以及构造器的关键字参数）如下所示：

width

(默认: 70) 自动换行的最大行长度。 只要输入文本中没有长于 width 的单个单词，TextWrapper 就能保证没有长于 width 个字符的输出行。

expand_tabs

(默认: True) 如果为真值，则 text 中所有的制表符将使用 text 的 expandtabs() 方法扩展为空格符。

tabsize

(默认: 8) 如果 expand_tabs 为真值，则 text 中所有的制表符将扩展为零个或多个空格，具体取决于当前列位置和给定的制表宽度。

3.3 新版功能.

replace_whitespace

(default: True) 如果为真值，在制表符扩展之后、自动换行之前，wrap() 方法将把每个空白字符都替换为单个空格。 会被替换的空白字符如下：制表，换行，垂直制表，进纸和回车 ('\t\n\v\f\r')。

注解

如果 expand_tabs 为假值且 replace_whitespace 为真值，每个制表符将被替换为单个空格，这与制表符扩展是 不 一样的。

注解

如果 replace_whitespace 为假值，在一行的中间有可能出现换行符并导致怪异的输出。 因此，文本应当（使用 str.splitlines() 或类似方法）拆分为段落并分别进行自动换行。

drop_whitespace

(默认: True) 如果为真值，每一行开头和末尾的空白字符（在包装之后、缩进之前）会被丢弃。 但是段落开头的空白字符如果后面不带任何非空白字符则不会被丢弃。 如果被丢弃的空白字符占据了一个整行，则该整行将被丢弃。

initial_indent

(默认: '') 将被添加到被自动换行输出内容的第一行的字符串。 其长度会被计入第一行的长度。 空字符串不会被缩进。

subsequent_indent

(default: '') 将被添加到被自动换行输出内容除第一行外的所有行的字符串。 其长度会被计入除行一行外的所有行的长度。

fix_sentence_endings

(默认: False) 如果为真值，TextWrapper 将尝试检测句子结尾并确保句子间总是以恰好两个空格符分隔。 对于使用等宽字体的文本来说通常都需要这样。 但是，句子检测算法并不完美：它假定句子结尾是一个小写字母加字符 '.', '!' 或 '?' 中的一个，并可能带有字符 '"' 或 "'"，最后以一个空格结束。 此算法的问题之一是它无法区分以下文本中的 “Dr.”

[...] Dr. Frankenstein's monster [...]

和以下文本中的 “Spot.”

[...] See Spot. See Spot run [...]

fix_sentence_endings 默认为假值。

Since the sentence detection algorithm relies on string.lowercase for the definition of “lowercase letter,” and a convention of using two spaces after a period to separate sentences on the same line, it is specific to English-language texts.

break_long_words

(默认: True) 如果为真值，则长度超过 width 的单词将被分开以保证行的长度不会超过 width。 如果为假值，超长单词不会被分开，因而某些行的长度可能会超过 width。 （超长单词将被单独作为一行，以尽量减少超出 width 的情况。）

break_on_hyphens

(默认: True) 如果为真值，将根据英语的惯例首选在空白符和复合词的连字符之后自动换行。 如果为假值，则只有空白符会被视为合适的潜在断行位置，但如果你确实不希望出现分开的单词则你必须将 break_long_words 设为假值。 之前版本的默认行为总是允许分开带有连字符的单词。

max_lines

(默认: None) 如果不为 None，则输出内容将最多包含 max_lines 行，并使 placeholder 出现在输出内容的末尾。

3.4 新版功能.

placeholder

(默认: ' [...]') 该文本将在输出文本被截短时出现在文本末尾。

3.4 新版功能.

TextWrapper 还提供了一些公有方法，类似于模块层级的便捷函数：

wrap(text)

对 text (字符串) 中的单独段落自动换行以使每行长度最多为 width 个字符。 所有自动换行选项均获取自 TextWrapper 实例的实例属性。 返回由输出行组成的列表，行尾不带换行符。 如果自动换行输出结果没有任何内容，则返回空列表。

fill(text)

对 text 中的单独段落自动换行并返回包含被自动换行段落的单独字符串。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

6.5. unicodedata — Unicode 数据库

This module provides access to the Unicode Character Database (UCD) which defines character properties for all Unicode characters. The data contained in this database is compiled from the UCD version 9.0.0 [http://www.unicode.org/Public/9.0.0/ucd].

The module uses the same names and symbols as defined by Unicode Standard Annex #44, “Unicode Character Database” [http://www.unicode.org/reports/tr44/tr44-6.html]. It defines the following functions:

unicodedata.lookup(name)

按名称查找字符。如果找到具有给定名称的字符，则返回相应的字符。 如果没有找到，则 KeyError 被引发。

在 3.3 版更改: 已添加对名称别名 1 和命名序列 2 的支持。

unicodedata.name(chr[, default])

返回分配给字符 chr 的名称作为字符串。如果没有定义名称，则返回 default ，如果没有给出，则 ValueError 被引发。

unicodedata.decimal(chr[, default])

返回分配给字符 chr 的十进制值作为整数。 如果没有定义这样的值，则返回 default ，如果没有给出，则 ValueError 被引发。

unicodedata.digit(chr[, default])

返回分配给字符 chr 的数字值作为整数。 如果没有定义这样的值，则返回 default ，如果没有给出，则 ValueError 被引发。

unicodedata.numeric(chr[, default])

返回分配给字符 chr 的数值作为浮点数。 如果没有定义这样的值，则返回 default ，如果没有给出，则 ValueError 被引发。

unicodedata.category(chr)

返回分配给字符 chr 的常规类别为字符串。

unicodedata.bidirectional(chr)

返回分配给字符 chr 的双向类作为字符串。如果未定义此类值，则返回空字符串。

unicodedata.combining(chr)

返回分配给字符 chr 的规范组合类作为整数。如果没有定义组合类，则返回 0 。

unicodedata.east_asian_width(chr)

返回分配给字符 chr 的东亚宽度作为字符串。

unicodedata.mirrored(chr)

返回分配给字符 chr 的镜像属性为整数。如果字符在双向文本中被识别为“镜像”字符，则返回 1 ，否则返回 0 。

unicodedata.decomposition(chr)

返回分配给字符 chr 的字符分解映射作为字符串。如果未定义此类映射，则返回空字符串。

unicodedata.normalize(form, unistr)

返回 Unicode 字符串 unistr 的正常形式 form 。 form 的有效值为 ‘NFC’ 、 ‘NFKC’ 、 ‘NFD’ 和 ‘NFKD’ 。

Unicode 标准基于规范等价和兼容性等效的定义定义了 Unicode 字符串的各种规范化形式。在 Unicode 中，可以以各种方式表示多个字符。 例如，字符 U+00C7 （带有 CEDILLA 的 LATIN CAPITAL LETTER C ）也可以表示为序列 U+0043（ LATIN CAPITAL LETTER C ）U+0327（ COMBINING CEDILLA ）。

对于每个字符，有两种正规形式：正规形式 C 和正规形式 D 。正规形式D（NFD）也称为规范分解，并将每个字符转换为其分解形式。 正规形式C（NFC）首先应用规范分解，然后再次组合预组合字符。

除了这两种形式之外，还有两种基于兼容性等效的其他常规形式。 在 Unicode 中，支持某些字符，这些字符通常与其他字符统一。 例如， U+2160（ROMAN NUMERAL ONE）与 U+0049（LATIN CAPITAL LETTER I）完全相同。 但是， Unicode 支持它与现有字符集（例如 gb2312 ）的兼容性。

正规形式KD（NFKD）将应用兼容性分解，即用其等价项替换所有兼容性字符。 正规形式KC（NFKC）首先应用兼容性分解，然后是规范组合。

即使两个 unicode 字符串被规范化并且人类读者看起来相同，如果一个具有组合字符而另一个没有，则它们可能无法相等。

此外，该模块暴露了以下常量：

unicodedata.unidata_version

此模块中使用的 Unicode 数据库的版本。

unicodedata.ucd_3_2_0

这是一个与整个模块具有相同方法的对象，但对于需要此特定版本的 Unicode 数据库（如 IDNA ）的应用程序，则使用 Unicode 数据库版本 3.2 。

示例：

>>> import unicodedata
>>> unicodedata.lookup('LEFT CURLY BRACKET')
'{'
>>> unicodedata.name('/')
'SOLIDUS'
>>> unicodedata.decimal('9')
9
>>> unicodedata.decimal('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not a decimal
>>> unicodedata.category('A')  # 'L'etter, 'u'ppercase
'Lu'
>>> unicodedata.bidirectional('\u0660') # 'A'rabic, 'N'umber
'AN'

备注

1

http://www.unicode.org/Public/9.0.0/ucd/NameAliases.txt

2

http://www.unicode.org/Public/9.0.0/ucd/NamedSequences.txt

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

6.6. stringprep — 因特网字符串预备

源代码: Lib/stringprep.py [https://github.com/python/cpython/tree/3.6/Lib/stringprep.py]

在标识因特网上的事物（例如主机名），经常需要比较这些标识是否（相等）。 这种比较的具体执行可能会取决于应用域的不同，例如是否要区分大小写等等。 有时也可能需要限制允许的标识为仅由“可打印”字符组成。

RFC 3454 [https://tools.ietf.org/html/rfc3454.html] 定义了在因特网协议中 Unicode 字符串的“预备”过程。 在将字符串连线传输之前，它们会先使用预备过程进行处理，之后它们将具有特定的标准形式。 该 RFC 定义了一系列表格，它们可以被组合为选项配置。 每个配置必须定义所使用的表格，stringprep 过程的其他可选项也是配置的组成部分。 stringprep 配置的一个例子是 nameprep，它被用于国际化域名。

模块 stringprep 仅公开了来自 RFC 3454 [https://tools.ietf.org/html/rfc3454.html] 的表格。 由于这些如果表格如果表示为字典或列表将会非常庞大，该模块在内部使用 Unicode 字符数据库。 该模块本身的源代码是使用 mkstringprep.py 工具生成的。

As a result, these tables are exposed as functions, not as data structures. There are two kinds of tables in the RFC: sets and mappings. For a set, stringprep provides the “characteristic function”, i.e. a function that returns true if the parameter is part of the set. For mappings, it provides the mapping function: given the key, it returns the associated value. Below is a list of all functions available in the module.

stringprep.in_table_a1(code)

确定 code 是否属于 tableA.1 (Unicode 3.2 中的未分配码位)。

stringprep.in_table_b1(code)

确定 code 是否属于 tableB.1 (通常映射为空值)。

stringprep.map_table_b2(code)

返回 code 依据 tableB.2 (配合 NFKC 使用的大小写转换映射) 所映射的值。

stringprep.map_table_b3(code)

返回 code 依据 tableB.3 (不附带正规化的大小写折叠映射) 所映射的值。

stringprep.in_table_c11(code)

确定 code 是否属于 tableC.1.1 (ASCII 空白字符)。

stringprep.in_table_c12(code)

确定 code 是否属于 tableC.1.2 (非 ASCII 空白字符)。

stringprep.in_table_c11_c12(code)

确定 code 是否属于 tableC.1 (空白字符，C.1.1 和 C.1.2 的并集)。

stringprep.in_table_c21(code)

确定 code 是否属于 tableC.2.1 (ASCII 控制字符)。

stringprep.in_table_c22(code)

确定 code 是否属于 tableC.2.2 (非 ASCII 控制字符)。

stringprep.in_table_c21_c22(code)

确定 code 是否属于 tableC.2 (控制字符，C.2.1 和 C.2.2 的并集)。

stringprep.in_table_c3(code)

确定 code 是否属于 tableC.3 (私有使用)。

stringprep.in_table_c4(code)

确定 code 是否属于 tableC.4 (非字符码位)。

stringprep.in_table_c5(code)

确定 code 是否属于 tableC.5 (替代码)。

stringprep.in_table_c6(code)

确定 code 是否属于 tableC.6 (不适用于纯文本)。

stringprep.in_table_c7(code)

确定 code 是否属于 tableC.7 (不适用于规范表示)。

stringprep.in_table_c8(code)

确定 code 是否属于 tableC.8 (改变显示属性或已弃用)。

stringprep.in_table_c9(code)

确定 code 是否属于 tableC.9 (标记字符)。

stringprep.in_table_d1(code)

确定 code 是否属于 tableD.1 (带有双向属性 “R” 或 “AL” 的字符)。

stringprep.in_table_d2(code)

确定 code 是否属于 tableD.2 (带有双向属性 “L” 的字符)。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

6.7. readline — GNU readline 接口

readline 模块定义了许多方便从 Python 解释器完成和读取/写入历史文件的函数。 此模块可以直接使用，或通过支持在交互提示符下完成 Python 标识符的 rlcompleter 模块使用。 使用此模块进行的设置会同时影响解释器的交互提示符以及内置 input() 函数提供的提示符。

注解

The underlying Readline library API may be implemented by the libedit library instead of GNU readline. On MacOS X the readline module detects which library is being used at run time.

libedit 所用的配置文件与 GNU readline 的不同。 如果你要在程序中载入配置字符串你可以在 readline.__doc__ 中检测文本 “libedit” 来区分 GNU readline 和 libedit。

Readline keybindings may be configured via an initialization file, typically .inputrc in your home directory. See Readline Init File [https://cnswww.cns.cwru.edu/php/chet/readline/rluserman.html#SEC9] in the GNU Readline manual for information about the format and allowable constructs of that file, and the capabilities of the Readline library in general.

6.7.1. 初始化文件

下列函数与初始化文件和用户配置有关：

readline.parse_and_bind(string)

执行在 string 参数中提供的初始化行。 此函数会调用底层库中的 rl_parse_and_bind()。

readline.read_init_file([filename])

执行一个 readline 初始化文件。 默认文件名为最近所使用的文件名。 此函数会调用底层库中的 rl_read_init_file()。

6.7.2. 行缓冲区

下列函数会在行缓冲区上操作。

readline.get_line_buffer()

返回行缓冲区的当前内容 (底层库中的 rl_line_buffer)。

readline.insert_text(string)

将文本插入行缓冲区的当前游标位置。 该函数会调用底层库中的 rl_insert_text()，但会忽略其返回值。

readline.redisplay()

改变屏幕的显示以反映行缓冲区的当前内容。 该函数会调用底层库中的 rl_redisplay()。

6.7.3. 历史文件

下列函数会在历史文件上操作：

readline.read_history_file([filename])

载入一个 readline 历史文件，并将其添加到历史列表。 默认文件名为 ~/.history。 此函数会调用底层库中的 read_history()。

readline.write_history_file([filename])

将历史列表保存为 readline 历史文件，覆盖任何现有文件。 默认文件名为 ~/.history。 此函数会调用底层库中的 write_history()。

readline.append_history_file(nelements[, filename])

将历史列表的最后 nelements 项添加到历史文件。 默认文件名为 ~/.history。 文件必须已存在。 此函数会调用底层库中的 append_history()。 此函数仅当 Python 编译包带有支持此功能的库版本时才会存在。

3.5 新版功能.

readline.get_history_length()

readline.set_history_length(length)

设置或返回需要保存到历史文件的行数。 write_history_file() 函数会通过调用底层库中的 history_truncate_file() 以使用该值来截取历史文件。 负值意味着不限制历史文件的大小。

6.7.4. 历史列表

以下函数会在全局历史列表上操作：

readline.clear_history()

清除当前历史。 此函数会调用底层库的 clear_history()。 此 Python 函数仅当 Python 编译包带有支持此功能的库版本时才会存在。

readline.get_current_history_length()

返回历史列表的当前项数。 （此函数不同于 get_history_length()，后者是返回将被写入历史文件的最大行数。）

readline.get_history_item(index)

返回序号为 index 的历史条目的当前内容。 条目序号从一开始。 此函数会调用底层库中的 history_get()。

readline.remove_history_item(pos)

从历史列表中移除指定位置上的历史条目。 条目位置从零开始。 此函数会调用底层库中的 remove_history()。

readline.replace_history_item(pos, line)

将指定位置上的历史条目替换为 line。 条目位置从零开始。 此函数会调用底层库中的 replace_history_entry()。

readline.add_history(line)

将 line 添加到历史缓冲区，相当于是最近输入的一行。 此函数会调用底层库中的 add_history()。

readline.set_auto_history(enabled)

启用或禁用当通过 readline 读取输入时自动调用 add_history()。 enabled 参数应为一个布尔值，当其为真值时启用自动历史，当其为假值时禁用自动历史。

3.6 新版功能.

CPython implementation detail: Auto history is enabled by default, and changes to this do not persist across multiple sessions.

6.7.5. 启动钩子

readline.set_startup_hook([function])

设置或移除底层库的 rl_startup_hook 回调所发起调用的函数。 如果指定了 function，它将被用作新的钩子函数；如果省略或为 None，任何已安装的函数将被移除。 钩子函数将在 readline 打印第一个提示信息之前不带参数地被调用。

readline.set_pre_input_hook([function])

设置或移除底层库的 rl_pre_input_hook 回调所发起调用的函数。 如果指定了 function，它将被用作新的钩子函数；如果省略或为 None，任何已安装的函数将被移除。 钩子函数将在打印第一个提示信息之后、readline 开始读取输入字符之前不带参数地被调用。 此函数仅当 Python 编译包带有支持此功能的库版本时才会存在。

6.7.6. Completion

以下函数与自定义单词补全函数的实现有关。 这通常使用 Tab 键进行操作，能够提示并自动补全正在输入的单词。 默认情况下，Readline 设置为由 rlcompleter 来补全交互模式解释器的 Python 标识符。 如果 readline 模块要配合自定义的补全函数来使用，则需要设置不同的单词分隔符。

readline.set_completer([function])

设置或移除补全函数。 如果指定了 function，它将被用作新的补全函数；如果省略或为 None，任何已安装的补全函数将被移除。 补全函数的调用形式为 function(text, state)，其中 state 为 0, 1, 2, …, 直至其返回一个非字符串值。 它应当返回下一个以 text 开头的候选补全内容。

已安装的补全函数将由传递给底层库中 rl_completion_matches() 的 entry_func 回调函数来发起调用。 text 字符串来自于底层库中 rl_attempted_completion_function 回调函数的第一个形参。

readline.get_completer()

获取补全函数，如果没有设置补全函数则返回 None。

readline.get_completion_type()

获取正在尝试的补全类型。 此函数会将底层库中的 rl_completion_type 变量作为一个整数返回。

readline.get_begidx()

readline.get_endidx()

获取补全域的开始和结束序号。 这些序号就是传给底层库中 rl_attempted_completion_function 回调函数的 start 和 end 参数。

readline.set_completer_delims(string)

readline.get_completer_delims()

设置或获取补全的单词分隔符。 此分隔符确定了要考虑补全的单词的开始和结束位置（补全域）。 这些函数会访问底层库的 rl_completer_word_break_characters 变量。

readline.set_completion_display_matches_hook([function])

设置或移除补全显示函数。 如果指定了 function，它将被用作新的补全显示函数；如果省略或为 None，任何已安装的补全显示函数将被移除。 此函数会设置或清除底层库的 rl_completion_display_matches_hook 回调函数。 补全显示函数会在每次需要显示匹配项时以 function(substitution, [matches], longest_match_length) 的形式被调用。

6.7.7. 示例

以下示例演示了如何使用 readline 模块的历史读取或写入函数来自动加载和保存用户主目录下名为 .python_history 的历史文件。 以下代码通常应当在交互会话期间从用户的 PYTHONSTARTUP 文件自动执行。

import atexit
import os
import readline

histfile = os.path.join(os.path.expanduser("~"), ".python_history")
try:
    readline.read_history_file(histfile)
    # default history len is -1 (infinite), which may grow unruly
    readline.set_history_length(1000)
except FileNotFoundError:
    pass

atexit.register(readline.write_history_file, histfile)

此代码实际上会在 Python 运行于 交互模式 时自动运行 (参见 Readline configuration)。

以下示例实现了同样的目标，但是通过只添加新历史的方式来支持并发的交互会话。

import atexit
import os
import readline
histfile = os.path.join(os.path.expanduser("~"), ".python_history")

try:
    readline.read_history_file(histfile)
    h_len = readline.get_current_history_length()
except FileNotFoundError:
    open(histfile, 'wb').close()
    h_len = 0

def save(prev_h_len, histfile):
    new_h_len = readline.get_current_history_length()
    readline.set_history_length(1000)
    readline.append_history_file(new_h_len - prev_h_len, histfile)
atexit.register(save, h_len, histfile)

以下示例扩展了 code.InteractiveConsole 类以支持历史保存/恢复。

import atexit
import code
import os
import readline

class HistoryConsole(code.InteractiveConsole):
    def __init__(self, locals=None, filename="<console>",
                 histfile=os.path.expanduser("~/.console-history")):
        code.InteractiveConsole.__init__(self, locals, filename)
        self.init_history(histfile)

    def init_history(self, histfile):
        readline.parse_and_bind("tab: complete")
        if hasattr(readline, "read_history_file"):
            try:
                readline.read_history_file(histfile)
            except FileNotFoundError:
                pass
            atexit.register(self.save_history, histfile)

    def save_history(self, histfile):
        readline.set_history_length(1000)
        readline.write_history_file(histfile)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

6.8. rlcompleter — GNU readline 的补全函数

源代码: Lib/rlcompleter.py [https://github.com/python/cpython/tree/3.6/Lib/rlcompleter.py]

rlcompeleter 通过补全有效的 Python 标识符和关键字定义了一个适用于 readline 模块的补全函数。

当此模块在具有可用的 readline 模块的 Unix 平台被导入, 一个 Completer 实例将被自动创建并且它的 complete() 方法将设置为 readline 的补全器.

示例:

>>> import rlcompleter
>>> import readline
>>> readline.parse_and_bind("tab: complete")
>>> readline. <TAB PRESSED>
readline.__doc__          readline.get_line_buffer(  readline.read_init_file(
readline.__file__         readline.insert_text(      readline.set_completer(
readline.__name__         readline.parse_and_bind(
>>> readline.

rlcompleter 模块是为了使用 Python 的 交互模式 而设计的。 除非 Python 是通过 -S 选项运行, 这个模块总是自动地被导入且配置 (参见 Readline configuration)。

在没有 readline 的平台, 此模块定义的 Completer 类仍然可以用于自定义行为.

6.8.1. Completer 对象

Completer 对象具有以下方法：

Completer.complete(text, state)

为 text 返回第 state 项补全。

如果指定的 text 不包含句点字符 ('.')，它将根据当前 __main__, builtins 和保留关键字（定义于 keyword 模块）所定义的名称进行补全。

如果为带有句点的名称执行调用，它将尝试尽量求值直到最后一部分为止而不产生附带影响（函数不会被求值，但它可以生成对 __getattr__() 的调用），并通过 dir() 函数来匹配剩余部分。 在对表达式求值期间引发的任何异常都会被捕获、静默处理并返回 None。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

6. 文本处理服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

7. 二进制数据服务

本章介绍的模块提供了一些操作二进制数据的基本服务操作。 有关二进制数据的其他操作，特别是与文件格式和网络协议有关的操作，将在相关章节中介绍。

下面描述的一些库 文本处理服务 也可以使用 ASCII 兼容的二进制格式（例如 re ）或所有二进制数据（例如 difflib ）。

另外，请参阅 Python 的内置二进制数据类型的文档 二进制序列类型 — bytes, bytearray, memoryview 。

7.1. struct — 将字节串解读为打包的二进制数据

7.1.1. 函数和异常

7.1.2. 格式字符串

7.1.2.1. 字节顺序，大小和对齐方式

7.1.2.2. 格式字符

7.1.2.3. 例子

7.1.3. 类

7.2. codecs — 编解码器注册和相关基类

7.2.1. 编解码器基类

7.2.1.1. 错误处理方案

7.2.1.2. 无状态的编码和解码

7.2.1.3. 增量式的编码和解码

7.2.1.3.1. IncrementalEncoder 对象

7.2.1.3.2. IncrementalDecoder 对象

7.2.1.4. 流式的编码和解码

7.2.1.4.1. StreamWriter 对象

7.2.1.4.2. StreamReader 对象

7.2.1.4.3. StreamReaderWriter 对象

7.2.1.4.4. StreamRecoder 对象

7.2.2. 编码格式与 Unicode

7.2.3. 标准编码

7.2.4. Python 专属的编码格式

7.2.4.1. 文字编码

7.2.4.2. 二进制转换

7.2.4.3. 文字转换

7.2.5. encodings.idna — 应用程序中的国际化域名

7.2.6. encodings.mbcs — Windows ANSI代码页

7.2.7. encodings.utf_8_sig — 带BOM签名的UTF-8编解码器

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

7. 二进制数据服务 »

|

7.1. struct — 将字节串解读为打包的二进制数据

源代码： Lib/struct.py [https://github.com/python/cpython/tree/3.6/Lib/struct.py]

此模块可以执行 Python 值和以 Python bytes 对象表示的 C 结构之间的转换。 这可以被用来处理存储在文件中或是从网络连接等其他来源获取的二进制数据。 它使用 格式字符串 作为 C 结构布局的精简描述以及与 Python 值的双向转换。

注解

默认情况下，打包给定 C 结构的结果会包含填充字节以使得所涉及的 C 类型保持正确的对齐；类似地，对齐在解包时也会被纳入考虑。 选择此种行为的目的是使得被打包结构的字节能与相应 C 结构在内存中的布局完全一致。 要处理平台独立的数据格式或省略隐式的填充字节，请使用 standard 大小和对齐而不是 native 大小和对齐：详情参见 字节顺序，大小和对齐方式。

某些 struct 的函数（以及 Struct 的方法）接受一个 buffer 参数。 这将指向实现了 缓冲协议 并提供只读或是可读写缓冲的对象。 用于此目的的最常见类型为 bytes 和 bytearray，但许多其他可被视为字节数组的类型也实现了缓冲协议，因此它们无需额外从 bytes 对象复制即可被读取或填充。

7.1.1. 函数和异常

此模块定义了下列异常和函数：

exception struct.error

会在多种场合下被引发的异常；其参数为一个描述错误信息的字符串。

struct.pack(fmt, v1, v2, ...)

Return a bytes object containing the values v1, v2, … packed according to the format string fmt. The arguments must match the values required by the format exactly.

struct.pack_into(fmt, buffer, offset, v1, v2, ...)

Pack the values v1, v2, … according to the format string fmt and write the packed bytes into the writable buffer buffer starting at position offset. Note that offset is a required argument.

struct.unpack(fmt, buffer)

Unpack from the buffer buffer (presumably packed by pack(fmt, ...)) according to the format string fmt. The result is a tuple even if it contains exactly one item. The buffer’s size in bytes must match the size required by the format, as reflected by calcsize().

struct.unpack_from(fmt, buffer, offset=0)

Unpack from buffer starting at position offset, according to the format string fmt. The result is a tuple even if it contains exactly one item. The buffer’s size in bytes, minus offset, must be at least the size required by the format, as reflected by calcsize().

struct.iter_unpack(fmt, buffer)

Iteratively unpack from the buffer buffer according to the format string fmt. This function returns an iterator which will read equally-sized chunks from the buffer until all its contents have been consumed. The buffer’s size in bytes must be a multiple of the size required by the format, as reflected by calcsize().

每次迭代将产生一个如格式字符串所指定的元组。

3.4 新版功能.

struct.calcsize(fmt)

Return the size of the struct (and hence of the bytes object produced by pack(fmt, ...)) corresponding to the format string fmt.

7.1.2. 格式字符串

格式字符串是用来在打包和解包数据时指定预期布局的机制。 它们使用指定被打包/解包数据类型的 格式字符 进行构建。 此外，还有一些特殊字符用来控制 字节顺序，大小和对齐方式。

7.1.2.1. 字节顺序，大小和对齐方式

默认情况下，C类型以机器的本机格式和字节顺序表示，并在必要时通过跳过填充字节进行正确对齐（根据C编译器使用的规则）。

或者，根据下表，格式字符串的第一个字符可用于指示打包数据的字节顺序，大小和对齐方式：

字符

字节顺序

大小

对齐方式

@

按原字节

按原字节

按原字节

=

按原字节

标准

无

<

小端

标准

无

>

大端

标准

无

!

网络（=大端）

标准

无

如果第一个字符不是其中之一，则假定为 '@' 。

本机字节顺序可能为大端或是小端，取决于主机系统的不同。 例如， Intel x86 和 AMD64 (x86-64) 是小端的；Motorola 68000 和 PowerPC G5 是大端的；ARM 和 Intel Itanium 具有可切换的字节顺序（双端）。 请使用 sys.byteorder 来检查你的系统字节顺序。

本机大小和对齐方式是使用 C 编译器的 sizeof 表达式来确定的。 这总是会与本机字节顺序相绑定。

标准大小仅取决于格式字符；请参阅 格式字符 部分中的表格。

请注意 '@' 和 '=' 之间的区别：两个都使用本机字节顺序，但后者的大小和对齐方式是标准化的。

格式 '!' 适合给那些宣称他们记不得网络字节顺序是大端还是小端的可怜人使用。

没有什么方式能指定非本机字节顺序（强制字节对调）；请正确选择使用 '<' 或 '>'。

注释:

填充只会在连续结构成员之间自动添加。 填充不会添加到已编码结构的开头和末尾。

当使用非本机大小和对齐方式即 ‘<’, ‘>’, ‘=’, and ‘!’ 时不会添加任何填充。

要将结构的末尾对齐到符合特定类型的对齐要求，请以该类型代码加重复计数的零作为格式结束。 参见 例子。

7.1.2.2. 格式字符

格式字符具有以下含义；C 和 Python 值之间的按其指定类型的转换应当是相当明显的。 ‘标准大小’列是指当使用标准大小时以字节表示的已打包值大小；也就是当格式字符串以 '<', '>', '!' 或 '=' 之一开头的情况。 当使用本机大小时，已打包值的大小取决于具体的平台。

格式

C 类型

Python 类型

标准大小

注释

x

填充字节

无

c

char

长度为 1 的字节串

1

b

signed char

整数

1

(1),(3)

B

unsigned char

整数

1

(3)

?

_Bool

bool

1

(1)

h

short

整数

2

(3)

H

unsigned short

整数

2

(3)

i

int

整数

4

(3)

I

unsigned int

整数

4

(3)

l

long

整数

4

(3)

L

unsigned long

整数

4

(3)

q

long long

整数

8

(2), (3)

Q

unsigned long long

整数

8

(2), (3)

n

ssize_t

整数

(4)

N

size_t

整数

(4)

e

(7)

浮点数

2

(5)

f

float

浮点数

4

(5)

d

double

浮点数

8

(5)

s

char[]

字节串

p

char[]

字节串

P

void *

整数

(6)

在 3.3 版更改: 增加了对 'n' 和 'N' 格式的支持

在 3.6 版更改: 添加了对 'e' 格式的支持。

注释:

'?' 转换码对应于 C99 定义的 _Bool 类型。 如果此类型不可用，则使用 char 来模拟。 在标准模式下，它总是以一个字节表示。

The 'q' and 'Q' conversion codes are available in native mode only if the platform C compiler supports C long long, or, on Windows, __int64. They are always available in standard modes.

当尝试使用任何整数转换码打包一个非整数时，如果该非整数具有 __index__() 方法，则会在打包之前调用该方法将参数转换为一个整数。

在 3.2 版更改: 为非整数使用 __index__() 方法是 3.2 版的新增特性。

'n' 和 'N' 转换码仅对本机大小可用（选择为默认或使用 '@' 字节顺序字符）。 对于标准大小，你可以使用适合你的应用的任何其他整数格式。

对于 'f', 'd' 和 'e' 转换码，打包表示形式将使用 IEEE 754 binary32, binary64 或 binary16 格式 (分别对应于 'f', 'd' 或 'e')，无论平台使用何种浮点格式。

'P' 格式字符仅对本机字节顺序可用（选择为默认或使用 '@' 字节顺序字符）。 字节顺序字符 '=' 选择使用基于主机系统的小端或大端排序。 struct 模块不会将其解读为本机排序，因此 'P' 格式将不可用。

IEEE 754 binary16 “半精度” 类型是在 IEEE 754 标准 [https://en.wikipedia.org/wiki/IEEE_floating_point#IEEE_754-2008] 的 2008 修订版中引入的。 它包含一个符号位，5 个指数位和 11 个精度位（明确存储 10 位），可以完全精确地表示大致范围在 6.1e-05 和 6.5e+04 之间的数字。 此类型并不被 C 编译器广泛支持：在一台典型的机器上，可以使用 unsigned short 进行存储，但不会被用于数学运算。 请参阅维基百科页面 half-precision floating-point format [https://en.wikipedia.org/wiki/Half-precision_floating-point_format] 了解详情。

格式字符之前可以带有整数重复计数。 例如，格式字符串 '4h' 的含义与 'hhhh' 完全相同。

格式之间的空白字符会被忽略；但是计数及其格式字符中不可有空白字符。

对于 's' 格式字符，计数会被解析为字节的长度，而不是像其他格式字符那样的重复计数；例如，'10s' 表示一个 10 字节的字节串，而 '10c' 表示 10 个字符。 如果未给出计数，则默认值为 1。 对于打包操作，字节串会被适当地截断或填充空字节以符合要求。 对于解包操作，结果字节对象总是恰好具有指定数量的字节。 作为特殊情况，'0s' 表示一个空字符串（而 '0c' 表示 0 个字符）。

当使用某一种整数格式 ('b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q') 打包值 x 时，如果 x 在该格式的有效范围之外则将引发 struct.error。

在 3.1 版更改: 在 3.0 中，某些包装了超范围值的整数格式会引发 DeprecationWarning 而不是 struct.error。

'p' 格式字符用于编码“Pascal 字符串”，即存储在由计数指定的 固定长度字节 中的可变长度短字符串。 所存储的第一个字节为字符串长度或 255 中的较小值。 之后是字符串对应的字节。 如果传入 pack() 的字符串过长（超过计数值减 1），则只有字符串前 count-1 个字节会被存储。 如果字符串短于 count-1，则会填充空字节以使得恰好使用了 count 个字节。 请注意对于 unpack()，'p' 格式字符会消耗 count 个字节，但返回的字符串永远不会包含超过 255 个字节。

对于 '?' 格式字符，返回值为 True 或 False。 在打包时将会使用参数对象的逻辑值。 以本机或标准 bool 类型表示的 0 或 1 将被打包，任何非零值在解包时将为 True。

7.1.2.3. 例子

注解

所有示例都假定使用一台大端机器的本机字节顺序、大小和对齐方式。

打包/解包三个整数的基础示例:

>>> from struct import *
>>> pack('hhl', 1, 2, 3)
b'\x00\x01\x00\x02\x00\x00\x00\x03'
>>> unpack('hhl', b'\x00\x01\x00\x02\x00\x00\x00\x03')
(1, 2, 3)
>>> calcsize('hhl')
8

解包的字段可通过将它们赋值给变量或将结果包装为一个具名元组来命名:

>>> record = b'raymond   \x32\x12\x08\x01\x08'
>>> name, serialnum, school, gradelevel = unpack('<10sHHb', record)

>>> from collections import namedtuple
>>> Student = namedtuple('Student', 'name serialnum school gradelevel')
>>> Student._make(unpack('<10sHHb', record))
Student(name=b'raymond   ', serialnum=4658, school=264, gradelevel=8)

格式字符的顺序可能对大小产生影响，因为满足对齐要求所需的填充是不同的:

>>> pack('ci', b'*', 0x12131415)
b'*\x00\x00\x00\x12\x13\x14\x15'
>>> pack('ic', 0x12131415, b'*')
b'\x12\x13\x14\x15*'
>>> calcsize('ci')
8
>>> calcsize('ic')
5

以下格式 'llh0l' 指定在末尾有两个填充字节，假定 long 类型按 4 个字节的边界对齐:

>>> pack('llh0l', 1, 2, 3)
b'\x00\x00\x00\x01\x00\x00\x00\x02\x00\x03\x00\x00'

这仅当本机大小和对齐方式生效时才会起作用；标准大小和对齐方式并不会强制进行任何对齐。

参见

模块 array

被打包为二进制存储的同质数据。

模块 xdrlib

打包和解包 XDR 数据。

7.1.3. 类

struct 模块还定义了以下类型：

class struct.Struct(format)

返回一个新的 Struct 对象，它会根据格式字符串 format 来写入和读取二进制数据。 一次性地创建 Struct 对象并调用其方法相比使用同样的格式调用 struct 函数更为高效，因为这样格式字符串只需被编译一次。

已编译的 Struct 对象支持以下方法和属性：

pack(v1, v2, ...)

等价于 pack() 函数，使用了已编译的格式。 (len(result) 将等于 size。)

pack_into(buffer, offset, v1, v2, ...)

等价于 pack_into() 函数，使用了已编译的格式。

unpack(buffer)

等价于 unpack() 函数，使用了已编译的格式。 缓冲区的字节大小必须等于 size。

unpack_from(buffer, offset=0)

等价于 unpack_from() 函数，使用了已编译的格式。 缓冲区的字节大小减去 offset 必须至少为 size。

iter_unpack(buffer)

等价于 iter_unpack() 函数，使用了已编译的格式。 缓冲区的大小必须为 size 的整数倍。

3.4 新版功能.

format

用于构造此 Struct 对象的格式字符串。

size

计算出对应于 format 的结构大小（亦即 pack() 方法所产生的字节串对象的大小）。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

7. 二进制数据服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

7. 二进制数据服务 »

|

7.2. codecs — 编解码器注册和相关基类

源代码： Lib/codecs.py [https://github.com/python/cpython/tree/3.6/Lib/codecs.py]

这个模块定义了标准 Python 编解码器（编码器和解码器）的基类，并提供接口用来访问内部的 Python 编解码器注册表，该注册表负责管理编解码器和错误处理的查找过程。 大多数标准编解码器都属于 文本编码，它们可将文本编码为字节串，但也提供了一些编解码器可将文本编码为文本，以及字节串编码为字节串。 自定义编解码器可以在任意类型间进行编码和解码，但某些模块特性仅适用于 文本编码 或将数据编码为 字节串 的编解码器。

该模块定义了以下用于使用任何编解码器进行编码和解码的函数:

codecs.encode(obj, encoding='utf-8', errors='strict')

使用为 encoding 注册的编解码器对 obj 进行编码。

可以给定 Errors 以设置所需要的错误处理方案。 默认的错误处理方案 'strict' 表示编码错误将引发 ValueError (或更特定编解码器相关的子类，例如 UnicodeEncodeError)。 请参阅 编解码器基类 了解有关编解码器错误处理的更多信息。

codecs.decode(obj, encoding='utf-8', errors='strict')

使用为 encoding 注册的编解码器对 obj 进行解码。

可以给定 Errors 以设置所需要的错误处理方案。 默认的错误处理方案 'strict' 表示编码错误将引发 ValueError (或更特定编解码器相关的子类，例如 UnicodeDecodeError)。 请参阅 编解码器基类 了解有关编解码器错误处理的更多信息。

每种编解码器的完整细节也可以直接查找获取：

codecs.lookup(encoding)

在 Python 编解码器注册表中查找编解码器信息，并返回一个 CodecInfo 对象，其定义见下文。

首先将会在注册表缓存中查找编码，如果未找到，则会扫描注册的搜索函数列表。 如果没有找到 CodecInfo 对象，则将引发 LookupError。 否则，CodecInfo 对象将被存入缓存并返回给调用者。

class codecs.CodecInfo(encode, decode, streamreader=None, streamwriter=None, incrementalencoder=None, incrementaldecoder=None, name=None)

查找编解码器注册表所得到的编解码器细节信息。 构造器参数将保存为同名的属性：

name

编码名称

encode

decode

无状态的编码和解码函数。 它们必须是具有与 Codec 的 encode() 和 decode() 方法相同接口的函数或方法 (参见 Codec 接口)。 这些函数或方法应当工作于无状态的模式。

incrementalencoder

incrementaldecoder

增量式的编码器和解码器类或工厂函数。 这些函数必须分别提供由基类 IncrementalEncoder 和 IncrementalDecoder 所定义的接口。 增量式编解码器可以保持状态。

streamwriter

streamreader

流式写入器和读取器类或工厂函数。 这些函数必须分别提供由基类 StreamWriter 和 StreamReader 所定义的接口。 流式编解码器可以保持状态。

为了简化对各种编解码器组件的访问，本模块提供了以下附加函数，它们使用 lookup() 来执行编解码器查找：

codecs.getencoder(encoding)

查找给定编码的编解码器并返回其编码器函数。

在编码无法找到时将引发 LookupError。

codecs.getdecoder(encoding)

查找给定编码的编解码器并返回其解码器函数。

在编码无法找到时将引发 LookupError。

codecs.getincrementalencoder(encoding)

查找给定编码的编解码器并返回其增量式编码器类或工厂函数。

在编码无法找到或编解码器不支持增量式编码器时将引发 LookupError。

codecs.getincrementaldecoder(encoding)

查找给定编码的编解码器并返回其增量式解码器类或工厂函数。

在编码无法找到或编解码器不支持增量式解码器时将引发 LookupError。

codecs.getreader(encoding)

查找给定编码的编解码器并返回其 StreamReader 类或工厂函数。

在编码无法找到时将引发 LookupError。

codecs.getwriter(encoding)

查找给定编码的编解码器并返回其 StreamWriter 类或工厂函数。

在编码无法找到时将引发 LookupError。

自定义编解码器的启用是通过注册适当的编解码器搜索函数：

codecs.register(search_function)

注册一个编解码器搜索函数。 搜索函数预期接收一个参数，即全部以小写字母表示的编码名称，并返回一个 CodecInfo 对象。 在搜索函数无法找到给定编码的情况下，它应当返回 None。

注解

搜索函数的注册目前是不可逆的，这在某些情况下可能导致问题，例如单元测试或模块重载等。

虽然内置的 open() 和相关联的 io 模块是操作已编码文本文件的推荐方式，但本模块也提供了额外的工具函数和类，允许在操作二进制文件时使用更多各类的编解码器：

codecs.open(filename, mode='r', encoding=None, errors='strict', buffering=1)

使用给定的 mode 打开已编码的文件并返回一个 StreamReaderWriter 的实例，提供透明的编码/解码。 默认的文件模式为 'r'，表示以读取模式打开文件。

注解

下层的已编码文件总是以二进制模式打开。 在读取和写入时不会自动执行 '\n' 的转换。 mode 参数可以是内置 open() 函数所接受的任意二进制模式；'b' 会被自动添加。

encoding 指定文件所要使用的编码格式。 允许任何编码为字节串或从字节串解码的编码格式，而文件方法所支持的数据类型则取决于所使用的编解码器。

可以指定 errors 来定义错误处理方案。 默认值 'strict' 表示在出现编码错误时引发 ValueError。

buffering 的含义与内置 open() 函数中的相同。 默认为行缓冲。

codecs.EncodedFile(file, data_encoding, file_encoding=None, errors='strict')

返回一个 StreamRecoder 实例，它提供了 file 的透明转码包装版本。 当包装版本被关闭时原始文件也会被关闭。

写入已包装文件的数据会根据给定的 data_encoding 解码，然后以使用 file_encoding 的字节形式写入原始文件。 从原始文件读取的字节串将根据 file_encoding 解码，其结果将使用 data_encoding 进行编码。

如果 file_encoding 未给定，则默认为 data_encoding。

可以指定 errors 来定义错误处理方案。 默认值 'strict' 表示在出现编码错误时引发 ValueError。

codecs.iterencode(iterator, encoding, errors='strict', **kwargs)

使用增量式编码器通过迭代来编码由 iterator 所提供的输入。 此函数属于 generator。 errors 参数（以及任何其他关键字参数）会被传递给增量式编码器。

此函数要求编解码器接受 str 对象形式的文本进行编码。 因此它不支持字节到字节的编码器，例如 base64_codec。

codecs.iterdecode(iterator, encoding, errors='strict', **kwargs)

使用增量式解码器通过迭代来解码由 iterator 所提供的输入。 此函数属于 generator。 errors 参数（以及任何其他关键字参数）会被传递给增量式解码器。

此函数要求编解码器接受 bytes 对象进行解码。 因此它不支持文本到文本的编码器，例如 rot_13，但是 rot_13 可以通过同样效果的 iterencode() 来使用。

本模块还提供了以下常量，适用于读取和写入依赖于平台的文件：

codecs.BOM

codecs.BOM_BE

codecs.BOM_LE

codecs.BOM_UTF8

codecs.BOM_UTF16

codecs.BOM_UTF16_BE

codecs.BOM_UTF16_LE

codecs.BOM_UTF32

codecs.BOM_UTF32_BE

codecs.BOM_UTF32_LE

这些常量定义了多种字节序列，即一些编码格式的 Unicode 字节顺序标记（BOM）。 它们在 UTF-16 和 UTF-32 数据流中被用以指明所使用的字节顺序，并在 UTF-8 中被用作 Unicode 签名。 BOM_UTF16 是 BOM_UTF16_BE 或 BOM_UTF16_LE，具体取决于平台的本机字节顺序，BOM 是 BOM_UTF16 的别名, BOM_LE 是 BOM_UTF16_LE 的别名，BOM_BE 是 BOM_UTF16_BE 的别名。 其他序列则表示 UTF-8 和 UTF-32 编码格式中的 BOM。

7.2.1. 编解码器基类

codecs 模块定义了一系列基类用来定义配合编解码器对象进行工作的接口，并且也可用作定制编解码器实现的基础。

每种编解码器必须定义四个接口以便用作 Python 中的编解码器：无状态编码器、无状态解码器、流读取器和流写入器。 流读取器和写入器通常会重用无状态编码器/解码器来实现文件协议。 编解码器作者还需要定义编解码器将如何处理编码和解码错误。

7.2.1.1. 错误处理方案

To simplify and standardize error handling, codecs may implement different error handling schemes by accepting the errors string argument. The following string values are defined and implemented by all standard Python codecs:

值

含义

'strict'

Raise UnicodeError (or a subclass); this is the default. Implemented in strict_errors().

'ignore'

Ignore the malformed data and continue without further notice. Implemented in ignore_errors().

以下错误处理方案仅适用于 文本编码:

值

含义

'replace'

Replace with a suitable replacement marker; Python will use the official U+FFFD REPLACEMENT CHARACTER for the built-in codecs on decoding, and ‘?’ on encoding. Implemented in replace_errors().

'xmlcharrefreplace'

Replace with the appropriate XML character reference (only for encoding). Implemented in xmlcharrefreplace_errors().

'backslashreplace'

使用带反斜杠的转义序列进行替换。 在 backslashreplace_errors() 中实现。

'namereplace'

Replace with \N{...} escape sequences (only for encoding). Implemented in namereplace_errors().

'surrogateescape'

On decoding, replace byte with individual surrogate code ranging from U+DC80 to U+DCFF. This code will then be turned back into the same byte when the 'surrogateescape' error handler is used when encoding the data. (See PEP 383 [https://www.python.org/dev/peps/pep-0383] for more.)

此外，以下错误处理方案被专门用于指定的编解码器：

值

编解码器

含义

'surrogatepass'

utf-8, utf-16, utf-32, utf-16-be, utf-16-le, utf-32-be, utf-32-le

Allow encoding and decoding of surrogate codes. These codecs normally treat the presence of surrogates as an error.

3.1 新版功能: 'surrogateescape' 和 'surrogatepass' 错误处理方案。

在 3.4 版更改: 'surrogatepass' 错误处理方案现在适用于 utf-16* 和 utf-32* 编解码器。

3.5 新版功能: 'namereplace' 错误处理方案。

在 3.5 版更改: 'backslashreplace' 错误处理方案现在适用于解码和转换。

允许的值集合可以通过注册新命名的错误处理方案来扩展：

codecs.register_error(name, error_handler)

在名称 name 之下注册错误处理函数 error_handler。 当 name 被指定为错误形参时，error_handler 参数所指定的对象将在编码和解码期间发生错误的情况下被调用，

For encoding, error_handler will be called with a UnicodeEncodeError instance, which contains information about the location of the error. The error handler must either raise this or a different exception, or return a tuple with a replacement for the unencodable part of the input and a position where encoding should continue. The replacement may be either str or bytes. If the replacement is bytes, the encoder will simply copy them into the output buffer. If the replacement is a string, the encoder will encode the replacement. Encoding continues on original input at the specified position. Negative position values will be treated as being relative to the end of the input string. If the resulting position is out of bound an IndexError will be raised.

解码和转换的做法很相似，不同之处在于将把 UnicodeDecodeError 或 UnicodeTranslateError 传给处理程序，并且来自错误处理程序的替换对象将被直接放入输出。

之前注册的错误处理方案（包括标准错误处理方案）可通过名称进行查找：

codecs.lookup_error(name)

返回之前在名称 name 之下注册的错误处理方案。

在处理方案无法找到时将引发 LookupError。

以下标准错误处理方案也可通过模块层级函数的方式来使用：

codecs.strict_errors(exception)

实现 'strict' 错误处理方案：每个编码或解码错误都会引发 UnicodeError。

codecs.replace_errors(exception)

实现 'replace' 错误处理方案 (仅用于 文本编码)：编码错误替换为 '?' (并由编解码器编码)，解码错误替换为 '\ufffd' (Unicode 替换字符)。

codecs.ignore_errors(exception)

实现 'ignore' 错误处理方案：忽略错误格式的数据并且不加进一步通知就继续执行。

codecs.xmlcharrefreplace_errors(exception)

实现 'xmlcharrefreplace' 错误处理方案 (仅用于 文本编码 的编码过程)：不可编码的字符将以适当的 XML 字符引用进行替换。

codecs.backslashreplace_errors(exception)

实现 'backslashreplace' 错误处理方案 (仅用于 文本编码)：错误格式的数据将以带反斜杠的转义序列进行替换。

codecs.namereplace_errors(exception)

实现 'namereplace' 错误处理方案 (仅用于 文本编码 的编码过程)：不可编码的字符将以 \N{...} 转义序列进行替换。

3.5 新版功能.

7.2.1.2. 无状态的编码和解码

基本 Codec 类定义了这些方法，同时还定义了无状态编码器和解码器的函数接口：

Codec.encode(input[, errors])

编码 input 对象并返回一个元组 (输出对象, 消耗长度)。 例如，text encoding 会使用特定的字符集编码格式 (例如 cp1252 或 iso-8859-1) 将字符串转换为字节串对象。

errors 参数定义了要应用的错误处理方案。 默认为 'strict' 处理方案。

此方法不一定会在 Codec 实例中保存状态。 可使用必须保存状态的 StreamWriter 作为编解码器以便高效地进行编码。

编码器必须能够处理零长度的输入并在此情况下返回输出对象类型的空对象。

Codec.decode(input[, errors])

Decodes the object input and returns a tuple (output object, length consumed). For instance, for a text encoding, decoding converts a bytes object encoded using a particular character set encoding to a string object.

对于文本编码格式和字节到字节编解码器，input 必须为一个字节串对象或提供了只读缓冲区接口的对象 – 例如，缓冲区对象和映射到内存的文件。

errors 参数定义了要应用的错误处理方案。 默认为 'strict' 处理方案。

此方法不一定会在 Codec 实例中保存状态。 可使用必须保存状态的 StreamReader 作为编解码器以便高效地进行解码。

解码器必须能够处理零长度的输入并在此情况下返回输出对象类型的空对象。

7.2.1.3. 增量式的编码和解码

IncrementalEncoder 和 IncrementalDecoder 类提供了增量式编码和解码的基本接口。 对输入的编码/解码不是通过对无状态编码器/解码器的一次调用，而是通过对增量式编码器/解码器的 encode()/decode() 方法的多次调用。 增量式编码器/解码器会在方法调用期间跟踪编码/解码过程。

调用 encode()/decode() 方法后的全部输出相当于将所有通过无状态编码器/解码器进行编码/解码的单个输入连接在一起所得到的输出。

7.2.1.3.1. IncrementalEncoder 对象

IncrementalEncoder 类用来对一个输入进行分步编码。 它定义了以下方法，每个增量式编码器都必须定义这些方法以便与 Python 编解码器注册表相兼容。

class codecs.IncrementalEncoder(errors='strict')

IncrementalEncoder 实例的构造器。

所有增量式编码器必须提供此构造器接口。 它们可以自由地添加额外的关键字参数，但只有在这里定义的参数才会被 Python 编解码器注册表所使用。

IncrementalEncoder 可以通过提供 errors 关键字参数来实现不同的错误处理方案。 可用的值请参阅 错误处理方案。

errors 参数将被赋值给一个同名的属性。 通过对此属性赋值就可以在 IncrementalEncoder 对象的生命期内在不同的错误处理策略之间进行切换。

encode(object[, final])

编码 object (会将编码器的当前状态纳入考虑) 并返回已编码的结果对象。 如果这是对 encode() 的最终调用则 final 必须为真值（默认为假值）。

reset()

将编码器重置为初始状态。 输出将被丢弃：调用 .encode(object, final=True)，在必要时传入一个空字节串或字符串，重置编码器并得到输出。

getstate()

Return the current state of the encoder which must be an integer. The implementation should make sure that 0 is the most common state. (States that are more complicated than integers can be converted into an integer by marshaling/pickling the state and encoding the bytes of the resulting string into an integer).

setstate(state)

将编码器的状态设为 state。 state 必须为 getstate() 所返回的一个编码器状态。

7.2.1.3.2. IncrementalDecoder 对象

IncrementalDecoder 类用来对一个输入进行分步解码。 它定义了以下方法，每个增量式解码器都必须定义这些方法以便与 Python 编解码器注册表相兼容。

class codecs.IncrementalDecoder(errors='strict')

IncrementalDecoder 实例的构造器。

所有增量式解码器必须提供此构造器接口。 它们可以自由地添加额外的关键字参数，但只有在这里定义的参数才会被 Python 编解码器注册表所使用。

IncrementalDecoder 可以通过提供 errors 关键字参数来实现不同的错误处理方案。 可用的值请参阅 错误处理方案。

errors 参数将被赋值给一个同名的属性。 通过对此属性赋值就可以在 IncrementalDecoder 对象的生命期内在不同的错误处理策略之间进行切换。

decode(object[, final])

解码 object (会将解码器的当前状态纳入考虑) 并返回已解码的结果对象。 如果这是对 decode() 的最终调用则 final 必须为真值（默认为假值）。 如果 final 为真值则解码器必须对输入进行完全解码并且必须 刷新所有缓冲区。 如果这无法做到（例如由于在输入结束时字节串序列不完整）则它必须像在无状态的情况下那样初始化错误处理（这可能引发一个异常）。

reset()

将解码器重置为初始状态。

getstate()

返回解码器的当前状态。 这必须为一个二元组，第一项必须是包含尚未解码的输入的缓冲区。 第二项必须为一个整数，可以表示附加状态信息。 （实现应当确保 0 是最常见的附加状态信息。） 如果此附加状态信息为 0 则必须可以将解码器设为没有已缓冲输入并且以 0 作为附加状态信息，以便将先前已缓冲的输入馈送到解码器使其返回到先前的状态而不产生任何输出。 （比整数更复杂的附加状态信息可以通过编组/选择状态信息并将结果字符串的字节数据编码为整数来转换为一个整数值。）

setstate(state)

Set the state of the encoder to state. state must be a decoder state returned by getstate().

7.2.1.4. 流式的编码和解码

StreamWriter 和 StreamReader 类提供了一些泛用工作接口，可被用来非常方便地实现新的编码格式子模块。 请参阅 encodings.utf_8 中的示例了解如何做到这一点。

7.2.1.4.1. StreamWriter 对象

StreamWriter 类是 Codec 的子类，它定义了以下方法，每个流式写入器都必须定义这些方法以便与 Python 编解码器注册表相兼容。

class codecs.StreamWriter(stream, errors='strict')

StreamWriter 实例的构造器。

所有流式写入器必须提供此构造器接口。 它们可以自由地添加额外的关键字参数，但只有在这里定义的参数才会被 Python 编解码器注册表所使用。

stream 参数必须为一个基于特定编解码器打开用于写入文本或二进制数据的文件类对象。

StreamWriter 可以通过提供 errors 关键字参数来实现不同的错误处理方案。 请参阅 错误处理方案 了解下层的流式编解码器可支持的标准错误处理方案。

errors 参数将被赋值给一个同名的属性。 通过对此属性赋值就可以在 StreamWriter 对象的生命期内在不同的错误处理策略之间进行切换。

write(object)

将编码后的对象内容写入到流。

writelines(list)

将拼接后的字符串列表写入到流（可能通过重用 write() 方法）。 标准的字节到字节编解码器不支持此方法。

reset()

刷新并重置用于保持状态的编解码器缓冲区。

调用此方法应当确保在干净的状态下放入输出数据，以允许直接添加新的干净数据而无须重新扫描整个流来恢复状态。

除了上述的方法，StreamWriter 还必须继承来自下层流的所有其他方法和属性。

7.2.1.4.2. StreamReader 对象

StreamReader 类是 Codec 的子类，它定义了以下方法，每个流式读取器都必须定义这些方法以便与 Python 编解码器注册表相兼容。

class codecs.StreamReader(stream, errors='strict')

StreamReader 实例的构造器。

所有流式读取器必须提供此构造器接口。 它们可以自由地添加额外的关键字参数，但只有在这里定义的参数才会被 Python 编解码器注册表所使用。

stream 参数必须为一个基于特定编解码器打开用于读取文本或二进制数据的文件类对象。

StreamReader 可以通过提供 errors 关键字参数来实现不同的错误处理方案。 请参阅 错误处理方案 了解下层的流式编解码器可支持的标准错误处理方案。

errors 参数将被赋值给一个同名的属性。 通过对此属性赋值就可以在 StreamReader 对象的生命期内在不同的错误处理策略之间进行切换。

errors 参数所允许的值集合可以使用 register_error() 来扩展。

read([size[, chars[, firstline]]])

解码来自流的数据并返回结果对象。

chars 参数指明要返回的解码后码位或字节数量。 read() 方法绝不会返回超出请求数量的数据，但如果可用数量不足，它可能返回少于请求数量的数据。

The size argument indicates the approximate maximum number of encoded bytes or code points to read for decoding. The decoder can modify this setting as appropriate. The default value -1 indicates to read and decode as much as possible. This parameter is intended to prevent having to decode huge files in one step.

firstline 旗标指明如果在后续行发生解码错误，则仅返回第一行就足够了。

此方法应当使用“贪婪”读取策略，这意味着它应当在编码格式定义和给定大小所允许的情况下尽可能多地读取数据，例如，如果在流上存在可选的编码结束或状态标记，这些内容也应当被读取。

readline([size[, keepends]])

从输入流读取一行并返回解码后的数据。

如果给定了 size，则将其作为 size 参数传递给流的 read() 方法。

如果 keepends 为假值，则行结束符将从返回的行中去除。

readlines([sizehint[, keepends]])

从输入流读取所有行并将其作为一个行列表返回。

Line-endings are implemented using the codec’s decoder method and are included in the list entries if keepends is true.

如果给定了 sizehint，则将其作为 size 参数传递给流的 read() 方法。

reset()

重置用于保持状态的编解码器缓冲区。

Note that no stream repositioning should take place. This method is primarily intended to be able to recover from decoding errors.

除了上述的方法，StreamReader 还必须继承来自下层流的所有其他方法和属性。

7.2.1.4.3. StreamReaderWriter 对象

StreamReaderWriter 是一个方便的类，允许对同时工作于读取和写入模式的流进行包装。

其设计使得开发者可以使用 lookup() 函数所返回的工厂函数来构造实例。

class codecs.StreamReaderWriter(stream, Reader, Writer, errors='strict')

创建一个 StreamReaderWriter 实例。 stream 必须为一个文件类对象。 Reader 和 Writer 必须为分别提供了 StreamReader 和 StreamWriter 接口的工厂函数或类。 错误处理通过与流式读取器和写入器所定义的相同方式来完成。

StreamReaderWriter 实例定义了 StreamReader 和 StreamWriter 类的组合接口。 它们还继承了来自下层流的所有其他方法和属性。

7.2.1.4.4. StreamRecoder 对象

StreamRecoder 将数据从一种编码格式转换为另一种，这对于处理不同编码环境的情况有时会很有用。

其设计使得开发者可以使用 lookup() 函数所返回的工厂函数来构造实例。

class codecs.StreamRecoder(stream, encode, decode, Reader, Writer, errors='strict')

创建一个实现了双向转换的 StreamRecoder 实例: encode 和 decode 工作于前端 — 对代码可见的数据调用 read() 和 write()，而 Reader 和 Writer 工作于后端 — stream 中的数据。

You can use these objects to do transparent transcodings from e.g. Latin-1 to UTF-8 and back.

stream 参数必须为一个文件类对象。

encode 和 decode 参数必须遵循 Codec 接口。 Reader 和 Writer 必须为分别提供了 StreamReader 和 StreamWriter 接口对象的工厂函数或类。

错误处理通过与流式读取器和写入器所定义的相同方式来完成。

StreamRecoder 实例定义了 StreamReader 和 StreamWriter 类的组合接口。 它们还继承了来自下层流的所有其他方法和属性。

7.2.2. 编码格式与 Unicode

Strings are stored internally as sequences of code points in range 0x0–0x10FFFF. (See PEP 393 [https://www.python.org/dev/peps/pep-0393] for more details about the implementation.) Once a string object is used outside of CPU and memory, endianness and how these arrays are stored as bytes become an issue. As with other codecs, serialising a string into a sequence of bytes is known as encoding, and recreating the string from the sequence of bytes is known as decoding. There are a variety of different text serialisation codecs, which are collectivity referred to as text encodings.

最简单的文本编码格式 (称为 'latin-1' 或 'iso-8859-1') 将码位 0–255 映射为字节值 0x0–0xff，这意味着包含 U+00FF 以上码位的字符串对象无法使用此编解码器进行编码。 这样做将引发 UnicodeEncodeError，其形式类似下面这样（不过详细的错误信息可能会有所不同）: UnicodeEncodeError: 'latin-1' codec can't encode character '\u1234' in position 3: ordinal not in range(256)。

还有另外一组编码格式（所谓的字符映射编码）会选择全部 Unicode 码位的不同子集并设定如何将这些码位映射为字节值 0x0–0xff。 要查看这是如何实现的，只需简单地打开相应源码例如 encodings/cp1252.py (这是一个主要在 Windows 上使用的编码格式)。 其中会有一个包含 256 个字符的字符串常量，指明每个字符所映射的字节值。

所有这些编码格式只能对 Unicode 所定义的 1114112 个码位中的 256 个进行编码。 一种能够存储每个 Unicode 码位的简单而直接的办法就是将每个码位存储为四个连续的字节。 存在两种不同的可能性：以大端序存储或以小端序存储。 这两种编码格式分别被称为 UTF-32-BE 和 UTF-32-LE。 它们的缺点可以举例说明：如果你在一台小端序的机器上使用 UTF-32-BE 则你将必须在编码和解码时翻转字节。 UTF-32 避免了这个问题：字节的排列将总是使用自然顺序。 当这些字节被具有不同字节顺序的 CPU 读取时，则必须进行字节翻转。 为了能够检测 UTF-16 或 UTF-32 字节序列的大小端序，可以使用所谓的 BOM (“字节顺序标记”)。 这对应于 Unicode 字符 U+FEFF。 此字符可添加到每个 UTF-16 或 UTF-32 字节序列的开头。 此字符的字节翻转版本 (0xFFFE) 是一个不可出现于 Unicode 文本中的非法字符。 因此当发现一个 UTF-16 或 UTF-32 字节序列的首个字符是 U+FFFE 时，就必须在解码时进行字节翻转。 不幸的是字符 U+FEFF 还有第二个含义 ZERO WIDTH NO-BREAK SPACE: 即宽度为零并且不允许用来拆分单词的字符。 它可以被用来为语言分析算法提供提示。 在 Unicode 4.0 中用 U+FEFF 表示 ZERO WIDTH NO-BREAK SPACE 已被弃用（改用 U+2060 (WORD JOINER) 负责此任务）。 然而 Unicode 软件仍然必须能够处理 U+FEFF 的两个含义：作为 BOM 它被用来确定已编码字节的存储布局，并在字节序列被解码为字符串后将其去除；作为 ZERO WIDTH NO-BREAK SPACE 它是一个普通字符，将像其他字符一样被解码。

还有另一种编码格式能够对所有的 Unicode 字符进行编码：UTF-8。 UTF-8 是一种 8 位编码，这意味着在 UTF-8 中没有字节顺序问题。 UTF-8 字节序列中的每个字节由两部分组成：标志位（最重要的位）和内容位。 标志位是由零至四个值为 1 的二进制位加一个值为 0 的二进制位构成的序列。 Unicode 字符会按以下形式进行编码（其中 x 为内容位，当拼接为一体时将给出对应的 Unicode 字符）：

范围

编码

U-00000000 … U-0000007F

0xxxxxxx

U-00000080 … U-000007FF

110xxxxx 10xxxxxx

U-00000800 … U-0000FFFF

1110xxxx 10xxxxxx 10xxxxxx

U-00010000 … U-0010FFFF

11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

Unicode 字符最不重要的一个位就是最右侧的二进制位 x。

由于 UTF-8 是一种 8 位编码格式，因此 BOM 是不必要的，并且已编码字符串中的任何 U+FEFF 字符（即使是作为第一个字符）都会被视为是 ZERO WIDTH NO-BREAK SPACE。

在没有外部信息的情况下，就不可能毫无疑义地确定一个字符串使用了何种编码格式。 每种字符映射编码格式都可以解码任意的随机字节序列。 然而对 UTF-8 来说这却是不可能的，因为 UTF-8 字节序列具有不允许任意字节序列的特别结构。 为了提升 UTF-8 编码检测的可靠性，Microsoft 发明了一种 UTF-8 变体形式 (Python 2.5 称之为 "utf-8-sig") 专门用于其 Notepad 程序：在任何 Unicode 字符在被写入文件之前，会先写入一个 UTF-8 编码的 BOM (它看起来是这样一个字节序列: 0xef, 0xbb, 0xbf)。 由于任何字符映射编码后的文件都不大可能以这些字节值开头（例如它们会映射为

LATIN SMALL LETTER I WITH DIAERESIS

RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK

INVERTED QUESTION MARK

in iso-8859-1), this increases the probability that a utf-8-sig encoding can be correctly guessed from the byte sequence. So here the BOM is not used to be able to determine the byte order used for generating the byte sequence, but as a signature that helps in guessing the encoding. On encoding the utf-8-sig codec will write 0xef, 0xbb, 0xbf as the first three bytes to the file. On decoding utf-8-sig will skip those three bytes if they appear as the first three bytes in the file. In UTF-8, the use of the BOM is discouraged and should generally be avoided.

7.2.3. 标准编码

Python 自带了许多内置的编解码器，它们的实现或者是通过 C 函数，或者是通过映射表。 以下表格是按名称排序的编解码器列表，并提供了一些常见别名以及编码格式通常针对的语言。 别名和语言列表都不是详尽无遗的。 请注意仅有大小写区别或使用连字符替代下划线的拼写形式也都是有效的别名；因此，'utf-8' 是 'utf_8' 编解码器的有效别名。

CPython implementation detail: Some common encodings can bypass the codecs lookup machinery to improve performance. These optimization opportunities are only recognized by CPython for a limited set of (case insensitive) aliases: utf-8, utf8, latin-1, latin1, iso-8859-1, iso8859-1, mbcs (Windows only), ascii, us-ascii, utf-16, utf16, utf-32, utf32, and the same using underscores instead of dashes. Using alternative aliases for these encodings may result in slower execution.

在 3.6 版更改: 可识别针对 us-ascii 的优化机会。

许多字符集都支持相同的语言。 它们在个别字符（例如是否支持 EURO SIGN 等）以及给字符所分配的码位方面存在差异。 特别是对于欧洲语言来说，通常存在以下几种变体：

某个 ISO 8859 编码集

某个 Microsoft Windows 编码页，通常是派生自某个 8859 编码集，但会用附加的图形字符来替换控制字符。

某个 IBM EBCDIC 编码页

某个 IBM PC 编码页，通常会兼容 ASCII

编码

别名

语言

ascii

646, us-ascii

英语

big5

big5-tw, csbig5

繁体中文

big5hkscs

big5-hkscs, hkscs

繁体中文

cp037

IBM037, IBM039

英语

cp273

273, IBM273, csIBM273

德语

3.4 新版功能.

cp424

EBCDIC-CP-HE, IBM424

希伯来语

cp437

437, IBM437

英语

cp500

EBCDIC-CP-BE, EBCDIC-CP-CH, IBM500

西欧

cp720

阿拉伯语

cp737

希腊语

cp775

IBM775

波罗的海语言

cp850

850, IBM850

西欧

cp852

852, IBM852

中欧和东欧

cp855

855, IBM855

保加利亚语，白俄罗斯语，马其顿语，俄语，塞尔维亚语

cp856

希伯来语

cp857

857, IBM857

土耳其语

cp858

858, IBM858

西欧

cp860

860, IBM860

葡萄牙语

cp861

861, CP-IS, IBM861

冰岛语

cp862

862, IBM862

希伯来语

cp863

863, IBM863

加拿大语

cp864

IBM864

阿拉伯语

cp865

865, IBM865

丹麦语/挪威语

cp866

866, IBM866

俄语

cp869

869, CP-GR, IBM869

希腊语

cp874

泰语

cp875

希腊语

cp932

932, ms932, mskanji, ms-kanji

日语

cp949

949, ms949, uhc

韩语

cp950

950, ms950

繁体中文

cp1006

乌尔都语

cp1026

ibm1026

土耳其语

cp1125

1125, ibm1125, cp866u, ruscii

乌克兰语

3.4 新版功能.

cp1140

ibm1140

西欧

cp1250

windows-1250

中欧和东欧

cp1251

windows-1251

保加利亚语，白俄罗斯语，马其顿语，俄语，塞尔维亚语

cp1252

windows-1252

西欧

cp1253

windows-1253

希腊语

cp1254

windows-1254

土耳其语

cp1255

windows-1255

希伯来语

cp1256

windows-1256

阿拉伯语

cp1257

windows-1257

波罗的海语言

cp1258

windows-1258

越南语

cp65001

仅Windows: Windows UTF-8 (CP_UTF8)

3.3 新版功能.

euc_jp

eucjp, ujis, u-jis

日语

euc_jis_2004

jisx0213, eucjis2004

日语

euc_jisx0213

eucjisx0213

日语

euc_kr

euckr, korean, ksc5601, ks_c-5601, ks_c-5601-1987, ksx1001, ks_x-1001

韩语

gb2312

chinese, csiso58gb231280, euc-cn, euccn, eucgb2312-cn, gb2312-1980, gb2312-80, iso-ir-58

简体中文

gbk

936, cp936, ms936

统一汉语

gb18030

gb18030-2000

统一汉语

hz

hzgb, hz-gb, hz-gb-2312

简体中文

iso2022_jp

csiso2022jp, iso2022jp, iso-2022-jp

日语

iso2022_jp_1

iso2022jp-1, iso-2022-jp-1

日语

iso2022_jp_2

iso2022jp-2, iso-2022-jp-2

日语，韩语，简体中文，西欧，希腊语

iso2022_jp_2004

iso2022jp-2004, iso-2022-jp-2004

日语

iso2022_jp_3

iso2022jp-3, iso-2022-jp-3

日语

iso2022_jp_ext

iso2022jp-ext, iso-2022-jp-ext

日语

iso2022_kr

csiso2022kr, iso2022kr, iso-2022-kr

韩语

latin_1

iso-8859-1, iso8859-1, 8859, cp819, latin, latin1, L1

West Europe

iso8859_2

iso-8859-2, latin2, L2

中欧和东欧

iso8859_3

iso-8859-3, latin3, L3

世界语，马耳他语

iso8859_4

iso-8859-4, latin4, L4

波罗的海语言

iso8859_5

iso-8859-5, cyrillic

保加利亚语，白俄罗斯语，马其顿语，俄语，塞尔维亚语

iso8859_6

iso-8859-6, arabic

阿拉伯语

iso8859_7

iso-8859-7, greek, greek8

希腊语

iso8859_8

iso-8859-8, hebrew

希伯来语

iso8859_9

iso-8859-9, latin5, L5

土耳其语

iso8859_10

iso-8859-10, latin6, L6

北欧语言

iso8859_11

iso-8859-11, thai

泰语

iso8859_13

iso-8859-13, latin7, L7

波罗的海语言

iso8859_14

iso-8859-14, latin8, L8

凯尔特语

iso8859_15

iso-8859-15, latin9, L9

西欧

iso8859_16

iso-8859-16, latin10, L10

东南欧

johab

cp1361, ms1361

韩语

koi8_r

俄语

koi8_t

塔吉克

3.5 新版功能.

koi8_u

乌克兰语

kz1048

kz_1048, strk1048_2002, rk1048

哈萨克语

3.5 新版功能.

mac_cyrillic

maccyrillic

保加利亚语，白俄罗斯语，马其顿语，俄语，塞尔维亚语

mac_greek

macgreek

希腊语

mac_iceland

maciceland

冰岛语

mac_latin2

maclatin2, maccentraleurope

中欧和东欧

mac_roman

macroman, macintosh

西欧

mac_turkish

macturkish

土耳其语

ptcp154

csptcp154, pt154, cp154, cyrillic-asian

哈萨克语

shift_jis

csshiftjis, shiftjis, sjis, s_jis

日语

shift_jis_2004

shiftjis2004, sjis_2004, sjis2004

日语

shift_jisx0213

shiftjisx0213, sjisx0213, s_jisx0213

日语

utf_32

U32, utf32

所有语言

utf_32_be

UTF-32BE

所有语言

utf_32_le

UTF-32LE

所有语言

utf_16

U16, utf16

所有语言

utf_16_be

UTF-16BE

所有语言

utf_16_le

UTF-16LE

所有语言

utf_7

U7, unicode-1-1-utf-7

所有语言

utf_8

U8, UTF, utf8

所有语言

utf_8_sig

所有语言

在 3.4 版更改: utf-16* and utf-32* 编码器将不再允许编码代理码位 (U+D800–U+DFFF)。 utf-32* 解码器将不再解码与代理码位相对应的字节序列。

7.2.4. Python 专属的编码格式

A number of predefined codecs are specific to Python, so their codec names have no meaning outside Python. These are listed in the tables below based on the expected input and output types (note that while text encodings are the most common use case for codecs, the underlying codec infrastructure supports arbitrary data transforms rather than just text encodings). For asymmetric codecs, the stated purpose describes the encoding direction.

7.2.4.1. 文字编码

以下编解码器提供了 str 到 bytes 的编码和 bytes-like object 到 str 的解码，类似于 Unicode 文本编码。

编码

别名

Purpose

idna

Implements RFC 3490 [https://tools.ietf.org/html/rfc3490.html], see also encodings.idna. Only errors='strict' is supported.

mbcs

ansi, dbcs

Windows only: Encode operand according to the ANSI codepage (CP_ACP)

oem

Windows only: Encode operand according to the OEM codepage (CP_OEMCP)

3.6 新版功能.

palmos

Encoding of PalmOS 3.5

punycode

Implements RFC 3492 [https://tools.ietf.org/html/rfc3492.html]. Stateful codecs are not supported.

raw_unicode_escape

Latin-1 编码格式附带对其他码位以 \uXXXX 和 \UXXXXXXXX 进行编码。 现有反斜杠不会以任何方式转义。 它被用于 Python 的 pickle 协议。

undefined

所有转换都将引发异常，甚至对空字符串也不例外。 错误处理方案会被忽略。

unicode_escape

Encoding suitable as the contents of a Unicode literal in ASCII-encoded Python source code, except that quotes are not escaped. Decodes from Latin-1 source code. Beware that Python source code actually uses UTF-8 by default.

unicode_internal

返回操作数的内部表示。 不支持有状态的编解码器。

3.3 版后已移除: 此表示已被 PEP 393 [https://www.python.org/dev/peps/pep-0393] 所废弃。

7.2.4.2. 二进制转换

The following codecs provide binary transforms: bytes-like object to bytes mappings. They are not supported by bytes.decode() (which only produces str output).

编码

别名

Purpose

编码器/解码器

base64_codec 1

base64, base_64

Convert operand to multiline MIME base64 (the result always includes a trailing '\n')

在 3.4 版更改: 接受任意 bytes-like object 作为输入用于编码和解码

base64.encodebytes() / base64.decodebytes()

bz2_codec

bz2

Compress the operand using bz2

bz2.compress() / bz2.decompress()

hex_codec

hex

Convert operand to hexadecimal representation, with two digits per byte

binascii.b2a_hex() / binascii.a2b_hex()

quopri_codec

quopri, quotedprintable, quoted_printable

Convert operand to MIME quoted printable

quopri.encode() 且 quotetabs=True / quopri.decode()

uu_codec

uu

Convert the operand using uuencode

uu.encode() / uu.decode()

zlib_codec

zip, zlib

Compress the operand using gzip

zlib.compress() / zlib.decompress()

1

除了 字节类对象，'base64_codec' 也接受仅包含 ASCII 的 str 实例用于解码

3.2 新版功能: 恢复二进制转换。

在 3.4 版更改: 恢复二进制转换的别名。

7.2.4.3. 文字转换

The following codec provides a text transform: a str to str mapping. It is not supported by str.encode() (which only produces bytes output).

编码

别名

Purpose

rot_13

rot13

Returns the Caesar-cypher encryption of the operand

3.2 新版功能: 恢复 rot_13 文本转换。

在 3.4 版更改: 恢复 rot13 别名。

7.2.5. encodings.idna — 应用程序中的国际化域名

此模块实现了 RFC 3490 [https://tools.ietf.org/html/rfc3490.html] (应用程序中的国际化域名) 和 RFC 3492 [https://tools.ietf.org/html/rfc3492.html] (Nameprep: 用于国际化域名 (IDN) 的 Stringprep 配置文件)。 它是在 punycode 编码格式和 stringprep 的基础上构建的。

这些 RFC 共同定义了一个在域名中支持非 ASCII 字符的协议。 一个包含非 ASCII 字符的域名 (例如 www.Alliancefrançaise.nu) 会被转换为兼容 ASCII 的编码格式 (简称 ACE，例如 www.xn--alliancefranaise-npb.nu)。 随后此域名的 ACE 形式可以用于所有由于特定协议而不允许使用任意字符的场合，例如 DNS 查询，HTTP Host 字段等等。 此转换是在应用中进行的；如有可能将对用户可见：应用应当透明地将 Unicode 域名标签转换为线上的 IDNA，并在 ACE 标签被呈现给用户之前将其转换回 Unicode。

Python supports this conversion in several ways: the idna codec performs conversion between Unicode and ACE, separating an input string into labels based on the separator characters defined in section 3.1 of RFC 3490 [https://tools.ietf.org/html/rfc3490.html#section-3.1] and converting each label to ACE as required, and conversely separating an input byte string into labels based on the . separator and converting any ACE labels found into unicode. Furthermore, the socket module transparently converts Unicode host names to ACE, so that applications need not be concerned about converting host names themselves when they pass them to the socket module. On top of that, modules that have host names as function parameters, such as http.client and ftplib, accept Unicode host names (http.client then also transparently sends an IDNA hostname in the Host field if it sends that field at all).

When receiving host names from the wire (such as in reverse name lookup), no automatic conversion to Unicode is performed: Applications wishing to present such host names to the user should decode them to Unicode.

encodings.idna 模块还实现了 nameprep 过程，该过程会对主机名执行特定的规范化操作，以实现国际域名的大小写不敏感特性与合并相似的字符。 如果有需要可以直接使用 nameprep 函数。

encodings.idna.nameprep(label)

返回 label 经过名称处理操作的版本。 该实现目前基于查询字符串，因此 AllowUnassigned 为真值。

encodings.idna.ToASCII(label)

将标签转换为 ASCII，规则定义见 RFC 3490 [https://tools.ietf.org/html/rfc3490.html]。 UseSTD3ASCIIRules 预设为假值。

encodings.idna.ToUnicode(label)

将标签转换为 Unicode，规则定义见 RFC 3490 [https://tools.ietf.org/html/rfc3490.html]。

7.2.6. encodings.mbcs — Windows ANSI代码页

Encode operand according to the ANSI codepage (CP_ACP).

Availability: Windows only.

在 3.3 版更改: 支持任何错误处理

在 3.2 版更改: 在 3.2 版之前， errors 参数会被忽略；总是会使用 'replace' 进行编码，并使用 'ignore' 进行解码。

7.2.7. encodings.utf_8_sig — 带BOM签名的UTF-8编解码器

This module implements a variant of the UTF-8 codec: On encoding a UTF-8 encoded BOM will be prepended to the UTF-8 encoded bytes. For the stateful encoder this is only done once (on the first write to the byte stream). For decoding an optional UTF-8 encoded BOM at the start of the data will be skipped.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

7. 二进制数据服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

8. 数据类型

The modules described in this chapter provide a variety of specialized data types such as dates and times, fixed-type arrays, heap queues, synchronized queues, and sets.

Python also provides some built-in data types, in particular, dict, list, set and frozenset, and tuple. The str class is used to hold Unicode strings, and the bytes class is used to hold binary data.

本章包含以下模块的文档：

8.1. datetime — 基本的日期和时间类型

8.1.1. 有效的类型

8.1.2. timedelta 类对象

8.1.3. date 对象

8.1.4. datetime 对象

8.1.5. time 对象

8.1.6. tzinfo 对象

8.1.7. timezone 对象

8.1.8. strftime() 和 strptime() 的行为

8.2. calendar — 日历相关函数

8.3. collections — 容器数据类型

8.3.1. ChainMap 对象

8.3.1.1. ChainMap 例子和方法

8.3.2. Counter 对象

8.3.3. deque 对象

8.3.3.1. deque 用法

8.3.4. defaultdict 对象

8.3.4.1. defaultdict 例子

8.3.5. namedtuple() 命名元组的工厂函数

8.3.6. OrderedDict 对象

8.3.6.1. OrderedDict 例子和用法

8.3.7. UserDict 对象

8.3.8. UserList 对象

8.3.9. UserString 对象

8.4. collections.abc — 容器的抽象基类

8.4.1. 容器抽象基类

8.5. heapq — 堆队列算法

8.5.1. 基本示例

8.5.2. 优先队列实现说明

8.5.3. 理论

8.6. bisect — 数组二分查找算法

8.6.1. 搜索有序列表

8.6.2. 其他示例

8.7. array — 高效的数值数组

8.8. weakref — 弱引用

8.8.1. 弱引用对象

8.8.2. 示例

8.8.3. 终结器对象

8.8.4. 比较终结器与 __del__() 方法

8.9. types — 动态类型创建和内置类型名称

8.9.1. 动态类型创建

8.9.2. 标准解释器类型

8.9.3. 附加工具类和函数

8.9.4. 协程工具函数

8.10. copy — 浅层 (shallow) 和深层 (deep) 复制操作

8.11. pprint — 数据美化输出

8.11.1. PrettyPrinter 对象

8.11.2. 示例

8.12. reprlib — 另一种 repr() 实现

8.12.1. Repr 对象

8.12.2. 子类化 Repr 对象

8.13. enum — 枚举类型支持

8.13.1. 模块内容

8.13.2. 创建一个 Enum

8.13.3. 对枚举成员及其属性的程序化访问

8.13.4. 复制枚举成员和值

8.13.5. 确保唯一的枚举值

8.13.6. 使用自动设定的值

8.13.7. 迭代

8.13.8. 比较运算

8.13.9. 允许的枚举成员和属性

8.13.10. Restricted subclassing of enumerations

8.13.11. 封存

8.13.12. 可用 API

8.13.13. 派生的枚举

8.13.13.1. IntEnum

8.13.13.2. IntFlag

8.13.13.3. 标志

8.13.13.4. 其他事项

8.13.14. 有趣的示例

8.13.14.1. 省略值

8.13.14.1.1. 使用 auto

8.13.14.1.2. 使用 object

8.13.14.1.3. 使用描述性字符串

8.13.14.1.4. 使用自定义的 __new__()

8.13.14.2. OrderedEnum

8.13.14.3. DuplicateFreeEnum

8.13.14.4. Planet

8.13.15. 各种枚举有何区别？

8.13.15.1. 枚举类

8.13.15.2. 枚举成员（即实例）

8.13.15.3. 细节要点

8.13.15.3.1. 支持 __dunder__ 名称

8.13.15.3.2. 支持的 _sunder_ 名称

8.13.15.3.3. Enum 成员类型

8.13.15.3.4. Enum 类和成员的布尔值

8.13.15.3.5. 带有方法的 Enum 类

8.13.15.3.6. 组合 Flag 的成员

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

8.1. datetime — 基本的日期和时间类型

源代码： Lib/datetime.py [https://github.com/python/cpython/tree/3.6/Lib/datetime.py]

datetime 模块提供了可以通过多种方式操作日期和时间的类。在支持日期时间数学运算的同时，实现的关注点更着重于如何能够更有效地解析其属性用于格式化输出和数据操作。相关功能可以参阅 time 和 calendar 模块。

有两种日期和时间的对象：“简单型“和”感知型“。

感知型对象有着用足以支持一些应用层面算法和国家层面时间调整的信息，例如时区和夏令时，来让自己和其他的感知型对象区别开来。感知型对象是用来表达不对解释器开放的特定时间信息 1。

简单型对象没包含足够多的信息来明确定位与之相关的 date/time 对象。 简单型对象所代表的是世界标准时间(UTC)、当地时间或者是其它时区的时间完全取决于程序，就像一个数字是代表的是米、英里或者质量完全取决于程序一样。 简单型对象以忽略了一些现实情况的为代价使得它容易理解和使用。

对于需要感知型对象的应用， datetime 对象和 time 对象有一个可选的时区信息属性 tzinfo ，这个属性可以设置给 tzinfo 类的子类实例。这些 tzinfo 对象捕获关于相对于世界标准时间（UTC）偏移、时区名字和夏令时是否有效等信息。需要注意的是，只有一个具体的 tzinfo 类，即由 datetime 模块提供的 timezone 类。 timezone 类可以代表相对于世界标准时间（UTC）固定偏移的简单时区，比如世界标准时间（UTC）自己或者北美东部时间或者东部夏令时。支持时区的详细程度取决于应用。世界各地的时间调整规则相比理性更加政治性，经常会变更。也没有一个基于世界标准时间（UTC）的标准套件适合用于所有应用。

The datetime module exports the following constants:

datetime.MINYEAR

date 或者 datetime 对象允许的最小年份。 常量 MINYEAR 是 1 。

datetime.MAXYEAR

date 或 datetime 对象允许最大的年份。常量 MAXYEAR 是 9999 。

参见

模块 calendar

日历相关函数

模块 time

时间的访问和转换

8.1.1. 有效的类型

class datetime.date

一个理想化的简单型日期，它假设当今的公历在过去和未来永远有效。 属性: year, month, and day。

class datetime.time

一个理想化的时间，它独立于任何特定的日期，假设每天一共有 24*60*60 秒（这里没有”闰秒”的概念）。 属性： hour, minute, second, microsecond, 和 tzinfo。

class datetime.datetime

日期和时间的结合。属性：year, month, day, hour, minute, second, microsecond, and tzinfo.

class datetime.timedelta

表示两个 date 对象或者 time 对象,或者 datetime 对象之间的时间间隔，精确到微秒。

class datetime.tzinfo

一个描述时区信息的抽象基类。用于给 datetime 类和 time 类提供自定义的时间调整概念（例如，负责时区或者夏令时）。

class datetime.timezone

一个实现了 tzinfo 抽象基类的子类，用于表示相对于 世界标准时间（UTC）的偏移量。

3.2 新版功能.

这些类型的对象都是不可变的。

date 类型的对象都是简单型的。

An object of type time or datetime may be naive or aware. A datetime object d is aware if d.tzinfo is not None and d.tzinfo.utcoffset(d) does not return None. If d.tzinfo is None, or if d.tzinfo is not None but d.tzinfo.utcoffset(d) returns None, d is naive. A time object t is aware if t.tzinfo is not None and t.tzinfo.utcoffset(None) does not return None. Otherwise, t is naive.

简单型和感知型之间的差别不适用于 timedelta 对象。

子类关系

object
    timedelta
    tzinfo
        timezone
    time
    date
        datetime

8.1.2. timedelta 类对象

timedelta 对象表示两个 date 或者 time 的时间间隔。

class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

所有的参数都是可选的并且默认为 0。这些参数可以是整数或者浮点数，也可以是正数或者负数。

只有 days,*seconds* 和 microseconds 会存储在内部，即python内部以 days,*seconds* 和 microseconds 三个单位作为存储的基本单位。参数单位转换规则如下：

1毫秒会转换成1000微秒。

1分钟会转换成60秒。

1小时会转换成3600秒。

1星期会转换成7天。

days, seconds, microseconds 本身也是标准化的，以保证表达方式的唯一性，例：

0 <= microseconds < 1000000

0 <= seconds < 3600*24 (一天的秒数)

-999999999 <= days <= 999999999

在有任何浮点型参数或者微秒为小数的情况下，所有小数均会按照前面的换算规则叠加到下一级，并使用round-half-to-even的方法对微秒进行取舍。没有浮点型参数情况下，转换的过程就是精确的（没有信息丢失）。

如果标准化后的 days 数值超过了指定范围，将会抛出 OverflowError 异常。

需要注意的是，负数被标准化后的结果会让你大吃一惊。例如，

>>> from datetime import timedelta
>>> d = timedelta(microseconds=-1)
>>> (d.days, d.seconds, d.microseconds)
(-1, 86399, 999999)

类属性：

timedelta.min

The most negative timedelta object, timedelta(-999999999).

timedelta.max

The most positive timedelta object, timedelta(days=999999999, hours=23, minutes=59, seconds=59, microseconds=999999).

timedelta.resolution

两个不相等的 timedelta 类对象最小的间隔为 timedelta(microseconds=1)。

需要注意的是，因为标准化的缘故，timedelta.max > -timedelta.min，-timedelta.max 不可以表示一个 timedelta 类对象。

实例属性（只读）：

属性

值

days

-999999999 至 999999999 ，含999999999

seconds

0 至 86399，包含86399

microseconds

0 至 999999，包含999999

支持的运算：

运算

结果：

t1 = t2 + t3

t2 和 t3 的和。 运算后 t1-t2 == t3 and t1-t3 == t2 必为真值。(1)

t1 = t2 - t3

t2 减 t3 的差。 运算后 t1 == t2 - t3 and t2 == t1 + t3 必为真值。 (1)(6)

t1 = t2 * i or t1 = i * t2

乘以一个整数。运算后假如 i != 0 则 t1 // i == t2 必为真值。

In general, t1 * i == t1 * (i-1) + t1 is true. (1)

t1 = t2 * f or t1 = f * t2

乘以一个浮点数，结果会被舍入到 timedelta 最接近的整数倍。 精度使用四舍五偶入奇不入规则。

f = t2 / t3

Division (3) of t2 by t3. Returns a float object.

t1 = t2 / f or t1 = t2 / i

除以一个浮点数或整数。 结果会被舍入到 timedelta 最接近的整数倍。 精度使用四舍五偶入奇不入规则。

t1 = t2 // i or t1 = t2 // t3

取整除，余数部分(如果有的话)将被丢弃。在第二种情况下，返回一个整数。(3)

t1 = t2 % t3

余数为一个 timedelta 对象。(3)

q, r = divmod(t1, t2)

通过 : q = t1 // t2 (3) and r = t1 % t2 计算出商和余数。q是一个整数，r是一个 timedelta 对象。

+t1

返回一个相同数值的 timedelta 对象。

-t1

等价于 timedelta(-t1.days, -t1.seconds, -t1.microseconds), 和 t1* -1. (1)(4)

abs(t)

当 t.days >= 0``时等于 +\ *t*, 当 ``t.days < 0 时 -t 。 (2)

str(t)

返回一个形如 [D day[s], ][H]H:MM:SS[.UUUUUU] 的字符串，当 t 为负数的时候， D 也为负数。 (5)

repr(t)

Returns a string in the form datetime.timedelta(D[, S[, U]]), where D is negative for negative t. (5)

注释:

精确但可能会溢出。

精确且不会溢出。

除以0将会抛出异常 ZeroDivisionError 。

-timedelta.max 不是一个 timedelta 类对象。

String representations of timedelta objects are normalized similarly to their internal representation. This leads to somewhat unusual results for negative timedeltas. For example:

>>> timedelta(hours=-5)
datetime.timedelta(-1, 68400)
>>> print(_)
-1 day, 19:00:00

表达式 t2 - t3 通常与 t2 + (-t3) 是等价的，除非 t3 等于 timedelta.max; 在这种情况下前者会返回结果，而后者则会溢出。

除了上面列举的操作以外 timedelta 对象还支持与 date 和 datetime 对象进行特定的相加和相减运算（见下文）。

在 3.2 版更改: 现在已支持 timedelta 对象与另一个 timedelta 对象相整除或相除，包括求余运算和 divmod() 函数。 现在也支持 timedelta 对象被 float 对象除或乘。

timedelta 对象与 timedelta 对象比较的支持是通过将表示较小时间差的 timedelta 对象视为较小值。 为了防止将混合类型比较回退为基于对象地址的默认比较，当 timedelta 对象与不同类型的对象比较时，将会引发 TypeError，除非比较运算符是 == 或 !=。 在后一种情况下将分别返回 False 或 True。

timedelta 对象是 hashable 类型的(可以作为字典关键字), 支持高效获取, 在布尔上下文中， timedelta 对象大多数情况下都被视为真，仅在不等于 timedelta(0) 时。

实例方法：

timedelta.total_seconds()

Return the total number of seconds contained in the duration. Equivalent to td / timedelta(seconds=1).

需要注意的是，时间间隔较大时，这个方法的结果中的微秒将会失真（大多数平台上大于270年视为一个较大的时间间隔）。

3.2 新版功能.

用法示例：

>>> from datetime import timedelta
>>> year = timedelta(days=365)
>>> another_year = timedelta(weeks=40, days=84, hours=23,
...                          minutes=50, seconds=600)  # adds up to 365 days
>>> year.total_seconds()
31536000.0
>>> year == another_year
True
>>> ten_years = 10 * year
>>> ten_years, ten_years.days // 365
(datetime.timedelta(3650), 10)
>>> nine_years = ten_years - year
>>> nine_years, nine_years.days // 365
(datetime.timedelta(3285), 9)
>>> three_years = nine_years // 3;
>>> three_years, three_years.days // 365
(datetime.timedelta(1095), 3)
>>> abs(three_years - ten_years) == 2 * three_years + year
True

8.1.3. date 对象

date 对象代表一个理想化历法中的日期（年、月和日），即当今的格列高利历向前后两个方向无限延伸。公元 1 年 1 月 1 日是第 1 日，公元 1 年 1 月 2 日是第 2 日， 依此类推。 这与 Dershowitz 与 Reingold 所著 Calendrical Calculations 中“预期格列高利”历法的定义一致，它是适用于该书中所有运算的基础历法。 请参阅该书了解在预期格列高利历序列与许多其他历法系统之间进行转换的算法。

class datetime.date(year, month, day)

All arguments are required. Arguments may be integers, in the following ranges:

MINYEAR <= year <= MAXYEAR

1 <= month <= 12

1 <= 日期 <= 给定年月对应的天数

如果参数不在这些范围内，则抛出 ValueError 异常。

其它构造器，所有的类方法：

classmethod date.today()

返回当地的当前日期。与``date.fromtimestamp(time.time())``等价。

classmethod date.fromtimestamp(timestamp)

返回对应于 POSIX 时间戳例如 time.time() 返回值的本地日期。 这可能引发 OverflowError，如果时间戳数值超出所在平台 C localtime() 函数的支持范围的话，并会在 localtime() 出错时引发 OSError。 通常该数值会被限制在 1970 年至 2038 年之间。 请注意在时间戳概念包含闰秒的非 POSIX 系统上，闰秒会被 fromtimestamp() 所忽略。

在 3.3 版更改: 引发 OverflowError 而不是 ValueError，如果时间戳数值超出所在平台 C localtime() 函数的支持范围的话，并会在 localtime() 出错时引发 OSError 而不是 ValueError。

classmethod date.fromordinal(ordinal)

返回对应于预期格列高利历序号的日期，其中公元 1 年 1 月 1 日的序号为 1。 除非 1 <= 序号 <= date.max.toordinal() 否则会引发 ValueError。 对于任意日期 d， date.fromordinal(d.toordinal()) == d。

类属性：

date.min

最小的日期 date(MINYEAR, 1, 1) 。

date.max

最大的日期 ，date(MAXYEAR, 12, 31)。

date.resolution

两个日期对象的最小间隔，timedelta(days=1)。

实例属性（只读）：

date.year

在 MINYEAR 和 MAXYEAR 之间，包含边界。

date.month

1 至 12（含）

date.day

返回1到指定年月的天数间的数字。

支持的运算：

运算

结果：

date2 = date1 + timedelta

date2 等于从 date1 减去 timedelta.days 天。 (1)

date2 = date1 - timedelta

计算 date2 的值使得 date2 + timedelta == date1。 (2)

timedelta = date1 - date2

(3)

date1 < date2

如果 date1 的时间在 date2 之前则认为 date1 小于 date2 。 (4)

注释:

如果 timedelta.days > 0 则 date2 在时间线上前进，如果 timedelta.days < 0 则后退。 操作完成后 date2 - date1 == timedelta.days。 timedelta.seconds 和 timedelta.microseconds 会被忽略。如果 date2.year 将小于 MINYEAR 或大于 MAXYEAR 则会引发 OverflowError。.

timedelta.seconds 和 timedelta.microseconds 会被忽略。

精确且不会溢出。 操作完成后 timedelta.seconds 和 timedelta.microseconds 均为0， 并且 date2 + timedelta == date1。

换句话说，当且仅当 date1.toordinal() < date2.toordinal() 时 date1 < date2。 日期比较会引发 TypeError，如果比较目标不为 date 对象的话。 不过也可能会返回 NotImplemented，如果比较目标具有 timetuple() 属性的话。 这个钩子给予其他日期对象类型实现混合类型比较的机会。 否则，当 date 对象与不同类型的对象比较时将会引发 TypeError，除非 == 或 != 比较。 后两种情况将分别返回 False 或 True。

日期可以作为字典的关键字。在布尔上下文中，所有的 date 对象都视为真。

实例方法：

date.replace(year=self.year, month=self.month, day=self.day)

返回一个具有同样值的日期，除非通过任何关键字参数给出了某些形参的新值。 例如，如果 d == date(2002, 12, 31)，则 d.replace(day=26) == date(2002, 12, 26)。

date.timetuple()

返回一个 time.struct_time，即与 time.localtime() 的返回类型相同。 hours, minutes 和 seconds 值为 0, 且 DST 标志值为 -1。 d.timetuple() 等价于 time.struct_time((d.year, d.month, d.day, 0, 0, 0, d.weekday(), yday, -1))，其中 yday = d.toordinal() - date(d.year, 1, 1).toordinal() + 1 是日期在当前年份中的序号，起始序号 1 表示 1 月 1 日。

date.toordinal()

返回日期的预期格列高利历序号，其中公元 1 年 1 月 1 日的序号为 1。 对于任意 date 对象 d，date.fromordinal(d.toordinal()) == d。

date.weekday()

返回一个整数代表星期几，星期一为0，星期天为6。例如， date(2002, 12, 4).weekday() == 2，表示的是星期三。参阅 isoweekday()。

date.isoweekday()

返回一个整数代表星期几，星期一为1，星期天为7。例如：date(2002, 12, 4).isoweekday() == 3,表示星期三。参见 weekday(), isocalendar()。

date.isocalendar()

返回一个三元元组，(ISO year, ISO week number, ISO weekday) 。

ISO日历是一个被广泛使用的公历。可以从 https://www.staff.science.uu.nl/~gent0113/calendar/isocalendar.htm 上查看更完整的说明。

ISO 年由 52 或 53 个完整星期构成，每个星期开始于星期一结束于星期日。 一个 ISO 年的第一个星期就是（格列高利）历法的的一年中第一个包含星期四的星期。 这被称为 1 号星期，其中星期四所在的 ISO 年与其所在的格列高利年相同。

例如，2004 年的第一天是一个星期四，因此 ISO 2004 年的第一个星期开始于 2003 年 12 月 29 日星期一，结束于 2004 年 1 月 4 日星期日，因此 date(2003, 12, 29).isocalendar() == (2004, 1, 1) and date(2004, 1, 4).isocalendar() == (2004, 1, 7)。

date.isoformat()

返回一个 ISO 8601 格式的字符串， ‘YYYY-MM-DD’。例如 date(2002, 12, 4).isoformat() == '2002-12-04'。

date.__str__()

对于日期对象 d, str(d) 等价于 d.isoformat() 。

date.ctime()

返回一个代表日期的字符串，例如 date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'。 在原生 C ctime() 函数 (time.ctime() 会发起调用该函数，但 date.ctime() 则不会) 遵循 C 标准的平台上，d.ctime() 等价于 time.ctime(time.mktime(d.timetuple()))。

date.strftime(format)

返回一个由显式格式字符串所指明的代表日期的字符串。 表示时、分或秒的格式代码值将为 0。 要获取格式指令的完整列表请参阅 strftime() 和 strptime() 的行为。

date.__format__(format)

与 date.strftime() 相同。 此方法使得为 date 对象指定以 格式化字符串字面值 表示的格式化字符串以及使用 str.format() 进行格式化成为可能。 要获取格式指令的完整列表，请参阅 strftime() 和 strptime() 的行为。

计算距离特定事件天数的例子:

>>> import time
>>> from datetime import date
>>> today = date.today()
>>> today
datetime.date(2007, 12, 5)
>>> today == date.fromtimestamp(time.time())
True
>>> my_birthday = date(today.year, 6, 24)
>>> if my_birthday < today:
...     my_birthday = my_birthday.replace(year=today.year + 1)
>>> my_birthday
datetime.date(2008, 6, 24)
>>> time_to_birthday = abs(my_birthday - today)
>>> time_to_birthday.days
202

使用 date 的例子:

>>> from datetime import date
>>> d = date.fromordinal(730920) # 730920th day after 1. 1. 0001
>>> d
datetime.date(2002, 3, 11)
>>> t = d.timetuple()
>>> for i in t:
...     print(i)
2002                # year
3                   # month
11                  # day
0
0
0
0                   # weekday (0 = Monday)
70                  # 70th day in the year
-1
>>> ic = d.isocalendar()
>>> for i in ic:
...     print(i)
2002                # ISO year
11                  # ISO week number
1                   # ISO day number ( 1 = Monday )
>>> d.isoformat()
'2002-03-11'
>>> d.strftime("%d/%m/%y")
'11/03/02'
>>> d.strftime("%A %d. %B %Y")
'Monday 11. March 2002'
>>> 'The {1} is {0:%d}, the {2} is {0:%B}.'.format(d, "day", "month")
'The day is 11, the month is March.'

8.1.4. datetime 对象

datetime 对象是一个包含了来自 date 对象和 time 对象所有信息的单一对象。 与 date 对象一样，datetime 假定当今的格列高利历向前后两个方向无限延伸；与 time 对象一样，datetime 假定每一天恰好有 3600*24 秒。

构造器 ：

class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)

The year, month and day arguments are required. tzinfo may be None, or an instance of a tzinfo subclass. The remaining arguments may be integers, in the following ranges:

MINYEAR <= year <= MAXYEAR,

1 <= month <= 12,

1 <= day <= 指定年月的天数,

0 <= hour < 24,

0 <= minute < 60,

0 <= second < 60,

0 <= microsecond < 1000000,

fold in [0, 1].

如果参数不在这些范围内，则抛出 ValueError 异常。

3.6 新版功能: 增加了 fold 参数。

其它构造器，所有的类方法：

classmethod datetime.today()

返回当前的本地 datetime，tzinfo 值为 None。 这等价于 datetime.fromtimestamp(time.time())。 另请参阅 now(), fromtimestamp()。

classmethod datetime.now(tz=None)

返回当前的本地 date 和 time。 如果可选参数 tz 为 None 或未指定，这就类似于 today()，但该方法会在可能的情况下提供比通过 time.time() 时间戳所获时间值更高的精度（例如，在提供了 C gettimeofday() 函数的平台上就可能做到）。

如果 tz 不为 None，它必须是 tzinfo 的子类的一个实例，并且当前日期和时间将转换为 tz 时区的日期和时间。 在这种情况下结果等价于 tz.fromutc(datetime.utcnow().replace(tzinfo=tz))。 另请参阅 today(), utcnow()。

classmethod datetime.utcnow()

返回当前 UTC 日期和时间，其中 tzinfo 为 None。 这类似于 now()，但返回的当前 UTC 日期和时间是一个本机 datetime 对象。 自动感知的当前 UTC 日期时间可通过调用 datetime.now(timezone.utc) 来获得。 另请参阅 now()。

classmethod datetime.fromtimestamp(timestamp, tz=None)

返回对应于 POSIX 时间戳例如 time.time() 的返回值的本地日期和时间。 如果可选参数 tz 为 None 或未指定，时间戳会被转换为所在平台的本地日期和时间，返回的 datetime 对象将为天真型。

如果 tz 不为 None，它必须是 tzinfo 子类的一个实例，并且时间戳将被转换到 tz 指定的时区。 在这种情况下结果等价于 tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz))。

fromtimestamp() 可能引发 OverflowError，如果时间戳数值超出所在平台 C localtime() 或 gmtime() 函数的支持范围的话，并会在 localtime() 或 gmtime() 出错时引发 OSError。 通常该数值会被限制在 1970 年至 2038 年之间。 请注意在时间戳概念包含闰秒的非 POSIX 系统上，闰秒会被 fromtimestamp() 所忽略，结果可能导致两个相差一秒的时间戳产生相同的 datetime 对象。 另请参阅 utcfromtimestamp()。

在 3.3 版更改: 引发 OverflowError 而不是 ValueError，如果时间戳数值超出所在平台 C localtime() 或 gmtime() 函数的支持范围的话。 并会在 localtime() 或 gmtime() 出错时引发 OSError 而不是 ValueError。

在 3.6 版更改: fromtimestamp() 可能返回 fold 值设为 1 的实例。

classmethod datetime.utcfromtimestamp(timestamp)

返回对应于 POSIX 时间戳的 UTC datetime，其中 tzinfo 为 None。 这可能引发 OverflowError，如果时间戳数值超出所在平台 C gmtime() 函数的支持范围的话，并会在 gmtime() 出错时引发 OSError。 通常该数值并会限制在 1970 年至 2038 年之间。

要得到一个感知型 datetime 对象，应调用 fromtimestamp():

datetime.fromtimestamp(timestamp, timezone.utc)

在 POSIX 兼容的平台上，它等价于以下表达式:

datetime(1970, 1, 1, tzinfo=timezone.utc) + timedelta(seconds=timestamp)

不同之处在于后一种形式总是支持完整年份范围：从 MINYEAR 到 MAXYEAR 的开区间。

在 3.3 版更改: 引发 OverflowError 而不是 ValueError，如果时间戳数值超出所在平台 C gmtime() 函数的支持范围的话。 并会在 gmtime() 出错时引发 OSError 而不是 ValueError。

classmethod datetime.fromordinal(ordinal)

返回对应于预期格列高利历序号的 datetime，其中公元 1 年 1 月 1 日的序号为 1。 除非 1 <= ordinal <= datetime.max.toordinal() 否则会引发 ValueError。 结果的hour, minute, second 和 microsecond 值均为 0，并且 tzinfo 值为 None。

classmethod datetime.combine(date, time, tzinfo=self.tzinfo)

返回一个新的 datetime 对象，对象的日期数值等于给定的 date 对象的数值，时间数值等于给定的 time 对象的数值。 如果提供 tzinfo 参数，其值会被用来设置结果的 tzinfo 属性，否则将会使用 time 参数的 tzinfo 属性。

对于任意 datetime 对象 d，d == datetime.combine(d.date(), d.time(), d.tzinfo)。 如果 date 是一个 datetime 对象，它的时间数值和 tzinfo 属性会被忽略。

在 3.6 版更改: 增加了 tzinfo 参数。

classmethod datetime.strptime(date_string, format)

返回根据 format 解析与 date_string 相对应的 datetime，这等价于 datetime(*(time.strptime(date_string, format)[0:6]))，如果 date_string 和 format 无法被 time.strptime() 解析或它返回一个不是时间元组的值，则将引发 ValueError。 要获取格式化指令的完整列表请参阅 strftime() 和 strptime() 的行为。

类属性：

datetime.min

最早的可表示 datetime，datetime(MINYEAR, 1, 1, tzinfo=None)。

datetime.max

最晚的可表示 datetime，datetime(MAXYEAR, 12, 31, 23, 59, 59, 999999, tzinfo=None)。

datetime.resolution

两个不相等的 datetime 对象之间可能的最小间隔，timedelta(microseconds=1)。

实例属性（只读）：

datetime.year

在 MINYEAR 和 MAXYEAR 之间，包含边界。

datetime.month

1 至 12（含）

datetime.day

返回1到指定年月的天数间的数字。

datetime.hour

取值范围是 range(24)。

datetime.minute

取值范围是 range(60)。

datetime.second

取值范围是 range(60)。

datetime.microsecond

取值范围是 range(1000000)。

datetime.tzinfo

作为 tzinfo 参数被传给 datetime 构造器的对象，如果没有传入值则为 None。

datetime.fold

取值范围是 [0, 1]。 用于在重复的时间段中消除边界时间歧义。 （当夏令时结束时回调时钟或由于政治原因导致当前时区的 UTC 时差减少就会出现重复的时间段。） 取值 0 (1) 表示两个时刻早于（晚于）所代表的同一边界时间。

3.6 新版功能.

支持的运算：

运算

结果：

datetime2 = datetime1 + timedelta

(1)

datetime2 = datetime1 - timedelta

(2)

timedelta = datetime1 - datetime2

(3)

datetime1 < datetime2

比较 datetime 与 datetime。 (4)

datetime2 是从中去掉的一段 timedelta 的结果，如果 timedelta.days > 0 则是在时间线上前进，如果 timedelta.days < 0 则后退。 结果具有与输入的 datetime 相同的 tzinfo 属性，并且操作完成后 datetime2 - datetime1 == timedelta。 如果 datetime2.year 将小于 MINYEAR 或大于 MAXYEAR 则会引发 OverflowError。 请注意即使输入的是一个感知型对象，该方法也不会进行时区调整。

计算 datetime2 使得 datetime2 + timedelta == datetime1。 与相加操作一样，结果具有与输入的 datetime 相同的 tzinfo 属性，即使输入的是一个感知型对象，该方法也不会进行时区调整。

从一个 datetime 减去一个 datetime 仅对两个操作数均为简单型或均为感知型时有定义。 如果一个是感知型而另一个是简单型，则会引发 TypeError。

如果两个操作数都是简单型，或都是感知型且具有相同的 tzinfo 属性，tzinfo 属性会被忽略，结果是一个使得 datetime2 + t == datetime1 的 timedelta 对象 t。 在此情况下不会进行时区调整。

如果两个操作数都是感知型且具有不同的 tzinfo 属性，a-b 操作的行为就如同 a 和 b 被首先转换为简单型 UTC 日期时间。 结果将是 (a.replace(tzinfo=None) - a.utcoffset()) - (b.replace(tzinfo=None) - b.utcoffset()) 除非具体实现绝对不溢出。

当 datetime1 的时间在 datetime2 之前则认为 datetime1 小于 datetime2。

如果比较的一方是简单型而另一方是感知型，则如果尝试进行顺序比较将引发 TypeError。 对于相等比较，简单型实例将永远不等于感知型实例。

如果两个比较方都是感知型，且具有相同的 tzinfo 属性，相同的 tzinfo 属性会被忽略并对基本日期时间值进行比较。 如果两个比较方都是感知型且具有不同的 tzinfo 属性，两个比较方将首先通过减去它们的 UTC 差值（使用 self.utcoffset() 获取）来进行调整。

在 3.3 版更改: 简单型和感知型 datetime 实例之间的相等比较不会引发 TypeError。

注解

为了防止比较操作回退为默认的对象地址比较方案，datetime 比较通常会引发 TypeError，如果比较目标不同样为 datetime 对象的话。 不过也可能会返回 NotImplemented 如果比较目标具有 timetuple() 属性的话。 这个钩子给予其他日期对象类型实现混合类型比较的机会。 否则，当 datetime 对象与不同类型的对象比较时将会引发 TypeError，除非 == 或 != 比较。 后两种情况将分别返回 False 或 True。

datetime 对象可以用作字典的键。 在布尔运算时，所有 datetime 对象都被视为真值。

实例方法：

datetime.date()

返回具有同样 year, month 和 day 值的 date 对象。

datetime.time()

返回具有同样 hour, minute, second, microsecond 和 fold 值的 time 对象。 tzinfo 为 None。 另请参见方法 timetz()。

在 3.6 版更改: fold 值会被复制给返回的 time 对象。

datetime.timetz()

返回具有同样 hour, minute, second, microsecond, fold 和 tzinfo 属性值的 time 对象。 另请参见方法 time()。

在 3.6 版更改: fold 值会被复制给返回的 time 对象。

datetime.replace(year=self.year, month=self.month, day=self.day, hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, tzinfo=self.tzinfo, * fold=0)

返回一个具有同样属性值的 datetime，除非通过任何关键字参数指定了某些属性值。 请注意可以通过指定 tzinfo=None 从一个感知型 datetime 创建一个简单型 datetime 而不必转换日期和时间值。

3.6 新版功能: 增加了 fold 参数。

datetime.astimezone(tz=None)

返回一个具有新的 tzinfo 属性 tz 的 datetime 对象，并会调整日期和时间数据使得结果对应的 UTC 时间与 self 相同，但为 tz 时区的本地时间。

tz 如果给出则必须是一个 tzinfo 子类的实例，并且其 utcoffset() 和 dst() 方法不可返回 None。 如果 self 为简单型，它会被假定为基于系统时区表示的时间。

如果调用时不传入参数（或传入 tz=None) 则将假定目标时区为系统的本地时区。 转换后 datetime 实例的 .tzinfo 属性将被设为一个 timezone 实例，时区名称和时差值将从系统获取。

如果 self.tzinfo 为 tz，self.astimezone(tz) 等于 self: 不会对日期或时间数据进行调整。 否则结果为 tz 时区的本地时间，代表的 UTC 时间与 self 相同：在 astz = dt.astimezone(tz) 之后，astz - astz.utcoffset() 将具有与 dt - dt.utcoffset() 相同的日期和时间数据。

如果你只想附加一个时区对象 tz 给一个 datetime 对象 dt 而不调整日期和时间数据，请使用 dt.replace(tzinfo=tz)。 如果你只想从一个感知型 datetime 对象 dt 移除时区对象则不转换日期和时间数据，请使用 dt.replace(tzinfo=None)。

请注意默认的 tzinfo.fromutc() 方法在 tzinfo 的子类中可以被重载，从而影响 astimezone() 的返回结果。 如果忽略出错的情况，astimezone() 的行为就类似于:

def astimezone(self, tz):
    if self.tzinfo is tz:
        return self
    # Convert self to UTC, and attach the new time zone object.
    utc = (self - self.utcoffset()).replace(tzinfo=tz)
    # Convert from UTC to tz's local time.
    return tz.fromutc(utc)

在 3.3 版更改: tz 现在可以被省略。

在 3.6 版更改: astimezone() 方法可以由简单型实例调用，这将假定其表示本地时间。

datetime.utcoffset()

If tzinfo is None, returns None, else returns self.tzinfo.utcoffset(self), and raises an exception if the latter doesn’t return None, or a timedelta object representing a whole number of minutes with magnitude less than one day.

datetime.dst()

If tzinfo is None, returns None, else returns self.tzinfo.dst(self), and raises an exception if the latter doesn’t return None, or a timedelta object representing a whole number of minutes with magnitude less than one day.

datetime.tzname()

如果 tzinfo 为 None，则返回 None，否则返回 self.tzinfo.tzname(self)，如果后者不返回 None 或者一个字符串对象则将引发异常。

datetime.timetuple()

返回一个 time.struct_time，即与 time.localtime() 的返回类型相同。 d.timetuple() 等价于 time.struct_time((d.year, d.month, d.day, d.hour, d.minute, d.second, d.weekday(), yday, dst))，其中 yday = d.toordinal() - date(d.year, 1, 1).toordinal() + 1 是日期在当前年份中的序号，起始序号 1 表示 1 月 1 日。 结果的 tm_isdst 旗标的设定会依据 dst() 方法：如果 tzinfo 为 None 或 dst() 返回 None，则 tm_isdst 将设为 -1；否则如果 dst() 返回一个非零值，则 tm_isdst 将设为 1；否则 tm_isdst 将设为 0。

datetime.utctimetuple()

如果 datetime 实例 d 为简单型，这类似于 d.timetuple()，不同之处为 tm_isdst 会强设为 0，无论 d.dst() 返回什么结果。 DST 对于 UTC 时间永远无效。

如果 d 为感知型，d 会通过减去 d.utcoffset() 被标准化为 UTC 时间，并返回标准化时间对应的 time.struct_time。 tm_isdst 会强设为 0。 请注意如果 d.year 为 MINYEAR 或 MAXYEAR 并且 UTC 调整超出一年的边界则可能引发 OverflowError。

datetime.toordinal()

返回日期的预期格列高利历序号。 与 self.date().toordinal() 相同。

datetime.timestamp()

返回对应于 datetime 实例的 POSIX 时间戳。 返回值是与 time.time() 类似的 float。

简单型 datetime 实例会假定为代表本地时间，并且此方法依赖于平台的 C mktime() 函数来执行转换。 由于在许多平台上 datetime 支持的值范围比 mktime() 更广，对于极其遥远的过去或末来时间此方法可能引发 OverflowError。

对于感知型 datetime 实例，返回值的计算方式为:

(dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()

3.3 新版功能.

在 3.6 版更改: timestamp() 方法使用 fold 属性来消除重复间隔中的时间歧义。

注解

没有一个方法能直接从简单型 datetime 实例获取 POSIX 时间戳来代表 UTC 时间。 如果你的应用使用此惯例方式并且你的系统时区不是设为 UTC，你可以通过提供 tzinfo=timezone.utc 来获取 POSIX 时间戳:

timestamp = dt.replace(tzinfo=timezone.utc).timestamp()

或者通过直接计算时间戳:

timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1)

datetime.weekday()

返回一个整数代表星期几，星期一为 0，星期天为 6。 相当于 self.date().weekday()。 另请参阅 isoweekday()。

datetime.isoweekday()

返回一个整数代表星期几，星期一为 1，星期天为 7。 相当于 self.date().isoweekday()。 另请参阅 weekday(), isocalendar()。

datetime.isocalendar()

返回一个 3 元组 (ISO 年份, ISO 周序号, ISO 周日期)。 相当于 self.date().isocalendar()。

datetime.isoformat(sep='T', timespec='auto')

Return a string representing the date and time in ISO 8601 format, YYYY-MM-DDTHH:MM:SS.mmmmmm or, if microsecond is 0, YYYY-MM-DDTHH:MM:SS

If utcoffset() does not return None, a 6-character string is appended, giving the UTC offset in (signed) hours and minutes: YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM or, if microsecond is 0 YYYY-MM-DDTHH:MM:SS+HH:MM

可选参数 sep (默认为 'T') 为单个分隔字符，会被放在结果的日期和时间两部分之间。例如

>>> from datetime import tzinfo, timedelta, datetime
>>> class TZ(tzinfo):
...     def utcoffset(self, dt): return timedelta(minutes=-399)
...
>>> datetime(2002, 12, 25, tzinfo=TZ()).isoformat(' ')
'2002-12-25 00:00:00-06:39'

可选参数 timespec 要包含的额外时间组件值 (默认为 'auto')。它可以是以下值之一：

'auto': 如果 microsecond 为 0 则与 'seconds' 相同，否则与 'microseconds' 相同。

'hours': 以两个数码的 HH 格式包含 hour。

'minutes': 以 HH:MM 格式包含 hour 和 minute。

'seconds': 以 HH:MM:SS 格式包含 hour, minute 和 second。

'milliseconds': 包含完整时间，但将秒值的小数部分截断至微秒。 格式为 HH:MM:SS.sss。

'microseconds': Include full time in HH:MM:SS.mmmmmm format.

注解

排除掉的时间部分将被截断，而不是被舍入。

对于无效的 timespec 参数将引发 ValueError。

>>> from datetime import datetime
>>> datetime.now().isoformat(timespec='minutes')
'2002-12-25T00:00'
>>> dt = datetime(2015, 1, 1, 12, 30, 59, 0)
>>> dt.isoformat(timespec='microseconds')
'2015-01-01T12:30:59.000000'

3.6 新版功能: 增加了 timespec 参数。

datetime.__str__()

对于 datetime 实例 d，str(d) 等价于 d.isoformat(' ')。

datetime.ctime()

返回一个代表日期和时间的字符串，例如 datetime(2002, 12, 4, 20, 30, 40).ctime() == 'Wed Dec  4 20:30:40 2002'。 在原生 C ctime() 函数 (time.ctime() 会发起调用该函数，但 datetime.ctime() 则不会) 遵循 C 标准的平台上，d.ctime() 等价于 time.ctime(time.mktime(d.timetuple()))。

datetime.strftime(format)

返回一个由显式格式字符串所指明的代表日期和时间的字符串， 要获取格式指令的完整列表请参阅 strftime() 和 strptime() 的行为。

datetime.__format__(format)

与 datetime.strftime() 相同。 此方法使得为 datetime 对象指定以 格式化字符串字面值 表示的格式化字符串以及使用 str.format() 进行格式化成为可能。 要获取格式指令的完整列表，请参阅 strftime() 和 strptime() 的行为。

使用 datetime 对象的例子:

>>> from datetime import datetime, date, time
>>> # Using datetime.combine()
>>> d = date(2005, 7, 14)
>>> t = time(12, 30)
>>> datetime.combine(d, t)
datetime.datetime(2005, 7, 14, 12, 30)
>>> # Using datetime.now() or datetime.utcnow()
>>> datetime.now()
datetime.datetime(2007, 12, 6, 16, 29, 43, 79043)   # GMT +1
>>> datetime.utcnow()
datetime.datetime(2007, 12, 6, 15, 29, 43, 79060)
>>> # Using datetime.strptime()
>>> dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
>>> dt
datetime.datetime(2006, 11, 21, 16, 30)
>>> # Using datetime.timetuple() to get tuple of all attributes
>>> tt = dt.timetuple()
>>> for it in tt:
...     print(it)
...
2006    # year
11      # month
21      # day
16      # hour
30      # minute
0       # second
1       # weekday (0 = Monday)
325     # number of days since 1st January
-1      # dst - method tzinfo.dst() returned None
>>> # Date in ISO format
>>> ic = dt.isocalendar()
>>> for it in ic:
...     print(it)
...
2006    # ISO year
47      # ISO week
2       # ISO weekday
>>> # Formatting datetime
>>> dt.strftime("%A, %d. %B %Y %I:%M%p")
'Tuesday, 21. November 2006 04:30PM'
>>> 'The {1} is {0:%d}, the {2} is {0:%B}, the {3} is {0:%I:%M%p}.'.format(dt, "day", "month", "time")
'The day is 21, the month is November, the time is 04:30PM.'

使用 datetime 并附带 tzinfo:

>>> from datetime import timedelta, datetime, tzinfo
>>> class GMT1(tzinfo):
...     def utcoffset(self, dt):
...         return timedelta(hours=1) + self.dst(dt)
...     def dst(self, dt):
...         # DST starts last Sunday in March
...         d = datetime(dt.year, 4, 1)   # ends last Sunday in October
...         self.dston = d - timedelta(days=d.weekday() + 1)
...         d = datetime(dt.year, 11, 1)
...         self.dstoff = d - timedelta(days=d.weekday() + 1)
...         if self.dston <=  dt.replace(tzinfo=None) < self.dstoff:
...             return timedelta(hours=1)
...         else:
...             return timedelta(0)
...     def tzname(self,dt):
...          return "GMT +1"
...
>>> class GMT2(tzinfo):
...     def utcoffset(self, dt):
...         return timedelta(hours=2) + self.dst(dt)
...     def dst(self, dt):
...         d = datetime(dt.year, 4, 1)
...         self.dston = d - timedelta(days=d.weekday() + 1)
...         d = datetime(dt.year, 11, 1)
...         self.dstoff = d - timedelta(days=d.weekday() + 1)
...         if self.dston <=  dt.replace(tzinfo=None) < self.dstoff:
...             return timedelta(hours=1)
...         else:
...             return timedelta(0)
...     def tzname(self,dt):
...         return "GMT +2"
...
>>> gmt1 = GMT1()
>>> # Daylight Saving Time
>>> dt1 = datetime(2006, 11, 21, 16, 30, tzinfo=gmt1)
>>> dt1.dst()
datetime.timedelta(0)
>>> dt1.utcoffset()
datetime.timedelta(0, 3600)
>>> dt2 = datetime(2006, 6, 14, 13, 0, tzinfo=gmt1)
>>> dt2.dst()
datetime.timedelta(0, 3600)
>>> dt2.utcoffset()
datetime.timedelta(0, 7200)
>>> # Convert datetime to another time zone
>>> dt3 = dt2.astimezone(GMT2())
>>> dt3
datetime.datetime(2006, 6, 14, 14, 0, tzinfo=<GMT2 object at 0x...>)
>>> dt2
datetime.datetime(2006, 6, 14, 13, 0, tzinfo=<GMT1 object at 0x...>)
>>> dt2.utctimetuple() == dt3.utctimetuple()
True

8.1.5. time 对象

一个 time 对象代表某个日期内的（本地）时间，它独立于任何特定日期，并可通过 tzinfo 对象来调整。

class datetime.time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)

All arguments are optional. tzinfo may be None, or an instance of a tzinfo subclass. The remaining arguments may be integers, in the following ranges:

0 <= hour < 24,

0 <= minute < 60,

0 <= second < 60,

0 <= microsecond < 1000000,

fold in [0, 1].

如果给出一个此范围以外的参数，则会引发 ValueError。 所有参数值默认为 0，除了 tzinfo 默认为 None。

类属性：

time.min

早最的可表示 time, time(0, 0, 0, 0)。

time.max

最晚的可表示 time, time(23, 59, 59, 999999)。

time.resolution

两个不相等的 time 对象之间可能的最小间隔，timedelta(microseconds=1)，但是请注意 time 对象并不支持算术运算。

实例属性（只读）：

time.hour

取值范围是 range(24)。

time.minute

取值范围是 range(60)。

time.second

取值范围是 range(60)。

time.microsecond

取值范围是 range(1000000)。

time.tzinfo

作为 tzinfo 参数被传给 time 构造器的对象，如果没有传入值则为 None。

time.fold

取值范围是 [0, 1]。 用于在重复的时间段中消除边界时间歧义。 （当夏令时结束时回调时钟或由于政治原因导致当前时区的 UTC 时差减少就会出现重复的时间段。） 取值 0 (1) 表示两个时刻早于（晚于）所代表的同一边界时间。

3.6 新版功能.

支持的运算：

比较 time 和另一个 time，当 a 的时间在 b 之前时，则认为 a 小于 b。 如果比较的一方是简单型而另一方是感知型，则如果尝试进行顺序比较将引发 TypeError。 对于相等比较，简单型实例将永远不等于感知型实例。

如果两个比较方都是感知型，且具有相同的 tzinfo 属性，相同的 tzinfo 属性会被忽略并对基本时间值进行比较。 如果两个比较方都是感知型且具有不同的 tzinfo 属性，两个比较方将首先通过减去它们的 UTC 差值（使用 self.utcoffset() 获取）来进行调整。 为了防止将混合类型比较回退为基于对象地址的默认比较，当 time 对象与不同类型的对象比较时，将会引发 TypeError，除非比较运算符是 == 或 !=。 在后一种情况下将分别返回 False 或 True。

在 3.3 版更改: 简单型和感知型 time 实例之前的相等比较不会引发 TypeError。

哈希，以便用作字典的键

高效的封存

在布尔运算时，time 对象总是被视为真值。

在 3.5 版更改: 在 Python 3.5 之前，如果一个 time 对象代表 UTC 午夜零时则会被视为假值。 此行为被认为容易引发困惑和错误，因此从 Python 3.5 起已被去除。 详情参见 bpo-13936 [https://bugs.python.org/issue13936]。

实例方法：

time.replace(hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, tzinfo=self.tzinfo, * fold=0)

返回一个具有同样属性值的 time，除非通过任何关键字参数指定了某些属性值。 请注意可以通过指定 tzinfo=None 从一个感知型 time 创建一个简单型 time 而不必转换时间值。

3.6 新版功能: 增加了 fold 参数。

time.isoformat(timespec='auto')

Return a string representing the time in ISO 8601 format, HH:MM:SS.mmmmmm or, if microsecond is 0, HH:MM:SS If utcoffset() does not return None, a 6-character string is appended, giving the UTC offset in (signed) hours and minutes: HH:MM:SS.mmmmmm+HH:MM or, if self.microsecond is 0, HH:MM:SS+HH:MM

可选参数 timespec 要包含的额外时间组件值 (默认为 'auto')。它可以是以下值之一：

'auto': 如果 microsecond 为 0 则与 'seconds' 相同，否则与 'microseconds' 相同。

'hours': 以两个数码的 HH 格式包含 hour。

'minutes': 以 HH:MM 格式包含 hour 和 minute。

'seconds': 以 HH:MM:SS 格式包含 hour, minute 和 second。

'milliseconds': 包含完整时间，但将秒值的小数部分截断至微秒。 格式为 HH:MM:SS.sss。

'microseconds': Include full time in HH:MM:SS.mmmmmm format.

注解

排除掉的时间部分将被截断，而不是被舍入。

对于无效的 timespec 参数将引发 ValueError。

>>> from datetime import time
>>> time(hour=12, minute=34, second=56, microsecond=123456).isoformat(timespec='minutes')
'12:34'
>>> dt = time(hour=12, minute=34, second=56, microsecond=0)
>>> dt.isoformat(timespec='microseconds')
'12:34:56.000000'
>>> dt.isoformat(timespec='auto')
'12:34:56'

3.6 新版功能: 增加了 timespec 参数。

time.__str__()

对于时间对象 t, str(t) 等价于 t.isoformat()。

time.strftime(format)

返回一个由显式格式字符串所指明的代表时间的字符串。 要获取格式指令的完整列表请参阅 strftime() 和 strptime() 的行为。

time.__format__(format)

与 time.strftime() 相同。 此方法使得为 time 对象指定以 格式化字符串字面值 表示的格式化字符串以及使用 str.format() 进行格式化成为可能。 要获取格式指令的完整列表，请参阅 strftime() 和 strptime() 的行为。

time.utcoffset()

If tzinfo is None, returns None, else returns self.tzinfo.utcoffset(None), and raises an exception if the latter doesn’t return None or a timedelta object representing a whole number of minutes with magnitude less than one day.

time.dst()

If tzinfo is None, returns None, else returns self.tzinfo.dst(None), and raises an exception if the latter doesn’t return None, or a timedelta object representing a whole number of minutes with magnitude less than one day.

time.tzname()

如果 tzinfo 为 None，则返回 None，否则返回 self.tzinfo.tzname(None)，如果后者不返回 None 或者一个字符串对象则将引发异常。

示例:

>>> from datetime import time, tzinfo, timedelta
>>> class GMT1(tzinfo):
...     def utcoffset(self, dt):
...         return timedelta(hours=1)
...     def dst(self, dt):
...         return timedelta(0)
...     def tzname(self,dt):
...         return "Europe/Prague"
...
>>> t = time(12, 10, 30, tzinfo=GMT1())
>>> t
datetime.time(12, 10, 30, tzinfo=<GMT1 object at 0x...>)
>>> gmt = GMT1()
>>> t.isoformat()
'12:10:30+01:00'
>>> t.dst()
datetime.timedelta(0)
>>> t.tzname()
'Europe/Prague'
>>> t.strftime("%H:%M:%S %Z")
'12:10:30 Europe/Prague'
>>> 'The {} is {:%H:%M}.'.format("time", t)
'The time is 12:10.'

8.1.6. tzinfo 对象

class datetime.tzinfo

这是一个抽象基类，即这个类不可直接被实例化。 你必须从该类派生一个实体子类，并且（至少）提供你使用 datetime 需要的标准 tzinfo 方法的实现。 datetime 模块提供了 tzinfo 的一个简单实体子类，timezone，它能以与 UTC 的固定差值来表示不同的时区，例如 UTC 本身或北美的 EST 和 EDT。

tzinfo 的（某个实体子类）的实例可以被传给 datetime 和 time 对象的构造器。 这些对象会将它们的属性视为对应于本地时间，并且 tzinfo 对象支持展示本地时间与 UTC 的差值、时区名称以及 DST 差值的方法，都是与传给它们的日期或时间对象的相对值。

对于封存操作的特殊要求：一个 tzinfo 子类必须具有可不带参数调用的 __init__() 方法，否则它虽然可以被封存，但可能无法再次解封。 这是个技术性要求，在未来可能会被取消。

一个 tzinfo 的实体子类可能需要实现以下方法。 具体需要实现的方法取决于感知型 datetime 对象如何使用它。 如果有疑问，可以简单地全都实现。

tzinfo.utcoffset(dt)

Return offset of local time from UTC, in minutes east of UTC. If local time is west of UTC, this should be negative. Note that this is intended to be the total offset from UTC; for example, if a tzinfo object represents both time zone and DST adjustments, utcoffset() should return their sum. If the UTC offset isn’t known, return None. Else the value returned must be a timedelta object specifying a whole number of minutes in the range -1439 to 1439 inclusive (1440 = 24*60; the magnitude of the offset must be less than one day). Most implementations of utcoffset() will probably look like one of these two:

return CONSTANT                 # fixed-offset class
return CONSTANT + self.dst(dt)  # daylight-aware class

如果 utcoffset() 返回值不为 None，则 dst() 也不应返回 None。

默认的 utcoffset() 实现会引发 NotImplementedError。

tzinfo.dst(dt)

Return the daylight saving time (DST) adjustment, in minutes east of UTC, or None if DST information isn’t known. Return timedelta(0) if DST is not in effect. If DST is in effect, return the offset as a timedelta object (see utcoffset() for details). Note that DST offset, if applicable, has already been added to the UTC offset returned by utcoffset(), so there’s no need to consult dst() unless you’re interested in obtaining DST info separately. For example, datetime.timetuple() calls its tzinfo attribute’s dst() method to determine how the tm_isdst flag should be set, and tzinfo.fromutc() calls dst() to account for DST changes when crossing time zones.

一个可以同时处理标准时和夏令时的 tzinfo 子类的实例 tz 必须在此情形中保持一致：

tz.utcoffset(dt) - tz.dst(dt)

必须为具有同样的 tzinfo 子类实例 dt.tzinfo == tz 的每个 datetime 对象 dt 返回同样的结果，此表达式会产生时区的“标准时差”，它不应取决于具体日期或时间，只取决于地理位置。 datetime.astimezone() 的实现依赖此方法，但无法检测违反规则的情况；确保符合规则是程序员的责任。 如果一个 tzinfo 子类不能保证这一点，也许应该重载 tzinfo.fromutc() 的默认实现以便在任何情况下与 astimezone() 配合正常。

大多数 dst() 的实现可能会如以下两者之一:

def dst(self, dt):
    # a fixed-offset class:  doesn't account for DST
    return timedelta(0)

或者

def dst(self, dt):
    # Code to set dston and dstoff to the time zone's DST
    # transition times based on the input dt.year, and expressed
    # in standard local time.  Then

    if dston <= dt.replace(tzinfo=None) < dstoff:
        return timedelta(hours=1)
    else:
        return timedelta(0)

默认的 dst() 实现会引发 NotImplementedError。

tzinfo.tzname(dt)

将对应于 datetime 对象 dt 的时区名称作为字符串返回。 datetime 模块没有定义任何字符串名称相关内容，也不要求名称有任何特定含义。 例如，”GMT”, “UTC”, “-500”, “-5:00”, “EDT”, “US/Eastern”, “America/New York” 都是有效的返回值。 如果字符串名称未知则返回 None。 请注意这是一个方法而不是一个固定的字符串，这主要是因为某些 tzinfo 子类可能需要根据所传入的特定 dt 值返回不同的名称，特别是当 tzinfo 类要负责处理夏令时的时候。

默认的 tzname() 实现会引发 NotImplementedError。

这些方法会被 datetime 或 time 对象调用，用来对应它们的同名方法。 datetime 对象会将自身作为传入参数，而 time 对象会将 None 作为传入参数。 这样 tzinfo 子类的方法应当准备好接受 dt 参数值为 None 或是 datetime 类的实例。

当传入 None 时，应当由类的设计者来决定最佳回应方式。 例如，返回 None 适用于希望该类提示时间对象不参与 tzinfo 协议处理。 让 utcoffset(None) 返回标准 UTC 时差也许会更有用处，如果没有其他用于发现标准时差的约定。

当传入一个 datetime 对象来回应 datetime 方法时，dt.tzinfo 与 self 是同一对象。 tzinfo 方法可以依赖这一点，除非用户代码直接调用了 tzinfo 方法。 此行为的目的是使得 tzinfo 方法将 dt 解读为本地时间，而不需要担心其他时区的相关对象。

还有一个额外的 tzinfo 方法，某个子类可能会希望重载它：

tzinfo.fromutc(dt)

此方法会由默认的 datetime.astimezone() 实现来调用。 当被调用时，dt.tzinfo 为 self，并且 dt 的日期和时间数据会被视为代表 UTC 时间。 fromutc() 的目标是调整日期和时间数据，返回一个等价的 datetime 来表示 self 的本地时间。

大多数 tzinfo 子类应该能够毫无问题地继承默认的 fromutc() 实现。 它的健壮性足以处理固定差值的时区以及同时负责标准时和夏令时的时区，对于后者甚至还能处理 DST 转换时间在各个年份有变化的情况。 一个默认 fromutc() 实现可能无法在所有情况下正确处理的例子是（与 UTC 的）标准差值取决于所经过的特定日期和时间，这种情况可能由于政治原因而出现。 默认的 astimezone() 和 fromutc() 实现可能无法生成你希望的结果，如果这个结果恰好是跨越了标准差值发生改变的时刻当中的某个小时值的话。

忽略针对错误情况的代码，默认 fromutc() 实现的行为方式如下:

def fromutc(self, dt):
    # raise ValueError error if dt.tzinfo is not self
    dtoff = dt.utcoffset()
    dtdst = dt.dst()
    # raise ValueError if dtoff is None or dtdst is None
    delta = dtoff - dtdst  # this is self's standard offset
    if delta:
        dt += delta   # convert to standard local time
        dtdst = dt.dst()
        # raise ValueError if dtdst is None
    if dtdst:
        return dt + dtdst
    else:
        return dt

Example tzinfo classes:

from datetime import tzinfo, timedelta, datetime, timezone

ZERO = timedelta(0)
HOUR = timedelta(hours=1)
SECOND = timedelta(seconds=1)

# A class capturing the platform's idea of local time.
# (May result in wrong values on historical times in
#  timezones where UTC offset and/or the DST rules had
#  changed in the past.)
import time as _time

STDOFFSET = timedelta(seconds = -_time.timezone)
if _time.daylight:
    DSTOFFSET = timedelta(seconds = -_time.altzone)
else:
    DSTOFFSET = STDOFFSET

DSTDIFF = DSTOFFSET - STDOFFSET

class LocalTimezone(tzinfo):

    def fromutc(self, dt):
        assert dt.tzinfo is self
        stamp = (dt - datetime(1970, 1, 1, tzinfo=self)) // SECOND
        args = _time.localtime(stamp)[:6]
        dst_diff = DSTDIFF // SECOND
        # Detect fold
        fold = (args == _time.localtime(stamp - dst_diff))
        return datetime(*args, microsecond=dt.microsecond,
                        tzinfo=self, fold=fold)

    def utcoffset(self, dt):
        if self._isdst(dt):
            return DSTOFFSET
        else:
            return STDOFFSET

    def dst(self, dt):
        if self._isdst(dt):
            return DSTDIFF
        else:
            return ZERO

    def tzname(self, dt):
        return _time.tzname[self._isdst(dt)]

    def _isdst(self, dt):
        tt = (dt.year, dt.month, dt.day,
              dt.hour, dt.minute, dt.second,
              dt.weekday(), 0, 0)
        stamp = _time.mktime(tt)
        tt = _time.localtime(stamp)
        return tt.tm_isdst > 0

Local = LocalTimezone()

# A complete implementation of current DST rules for major US time zones.

def first_sunday_on_or_after(dt):
    days_to_go = 6 - dt.weekday()
    if days_to_go:
        dt += timedelta(days_to_go)
    return dt

# US DST Rules
#
# This is a simplified (i.e., wrong for a few cases) set of rules for US
# DST start and end times. For a complete and up-to-date set of DST rules
# and timezone definitions, visit the Olson Database (or try pytz):
# http://www.twinsun.com/tz/tz-link.htm
# http://sourceforge.net/projects/pytz/ (might not be up-to-date)
#
# In the US, since 2007, DST starts at 2am (standard time) on the second
# Sunday in March, which is the first Sunday on or after Mar 8.
DSTSTART_2007 = datetime(1, 3, 8, 2)
# and ends at 2am (DST time) on the first Sunday of Nov.
DSTEND_2007 = datetime(1, 11, 1, 2)
# From 1987 to 2006, DST used to start at 2am (standard time) on the first
# Sunday in April and to end at 2am (DST time) on the last
# Sunday of October, which is the first Sunday on or after Oct 25.
DSTSTART_1987_2006 = datetime(1, 4, 1, 2)
DSTEND_1987_2006 = datetime(1, 10, 25, 2)
# From 1967 to 1986, DST used to start at 2am (standard time) on the last
# Sunday in April (the one on or after April 24) and to end at 2am (DST time)
# on the last Sunday of October, which is the first Sunday
# on or after Oct 25.
DSTSTART_1967_1986 = datetime(1, 4, 24, 2)
DSTEND_1967_1986 = DSTEND_1987_2006

def us_dst_range(year):
    # Find start and end times for US DST. For years before 1967, return
    # start = end for no DST.
    if 2006 < year:
        dststart, dstend = DSTSTART_2007, DSTEND_2007
    elif 1986 < year < 2007:
        dststart, dstend = DSTSTART_1987_2006, DSTEND_1987_2006
    elif 1966 < year < 1987:
        dststart, dstend = DSTSTART_1967_1986, DSTEND_1967_1986
    else:
        return (datetime(year, 1, 1), ) * 2

    start = first_sunday_on_or_after(dststart.replace(year=year))
    end = first_sunday_on_or_after(dstend.replace(year=year))
    return start, end

class USTimeZone(tzinfo):

    def __init__(self, hours, reprname, stdname, dstname):
        self.stdoffset = timedelta(hours=hours)
        self.reprname = reprname
        self.stdname = stdname
        self.dstname = dstname

    def __repr__(self):
        return self.reprname

    def tzname(self, dt):
        if self.dst(dt):
            return self.dstname
        else:
            return self.stdname

    def utcoffset(self, dt):
        return self.stdoffset + self.dst(dt)

    def dst(self, dt):
        if dt is None or dt.tzinfo is None:
            # An exception may be sensible here, in one or both cases.
            # It depends on how you want to treat them.  The default
            # fromutc() implementation (called by the default astimezone()
            # implementation) passes a datetime with dt.tzinfo is self.
            return ZERO
        assert dt.tzinfo is self
        start, end = us_dst_range(dt.year)
        # Can't compare naive to aware objects, so strip the timezone from
        # dt first.
        dt = dt.replace(tzinfo=None)
        if start + HOUR <= dt < end - HOUR:
            # DST is in effect.
            return HOUR
        if end - HOUR <= dt < end:
            # Fold (an ambiguous hour): use dt.fold to disambiguate.
            return ZERO if dt.fold else HOUR
        if start <= dt < start + HOUR:
            # Gap (a non-existent hour): reverse the fold rule.
            return HOUR if dt.fold else ZERO
        # DST is off.
        return ZERO

    def fromutc(self, dt):
        assert dt.tzinfo is self
        start, end = us_dst_range(dt.year)
        start = start.replace(tzinfo=self)
        end = end.replace(tzinfo=self)
        std_time = dt + self.stdoffset
        dst_time = std_time + HOUR
        if end <= dst_time < end + HOUR:
            # Repeated hour
            return std_time.replace(fold=1)
        if std_time < start or dst_time >= end:
            # Standard time
            return std_time
        if start <= std_time < end - HOUR:
            # Daylight saving time
            return dst_time

Eastern  = USTimeZone(-5, "Eastern",  "EST", "EDT")
Central  = USTimeZone(-6, "Central",  "CST", "CDT")
Mountain = USTimeZone(-7, "Mountain", "MST", "MDT")
Pacific  = USTimeZone(-8, "Pacific",  "PST", "PDT")

请注意同时负责标准时和夏令时的 tzinfo 子类在每年两次的 DST 转换点上会出现不可避免的微妙问题。 具体而言，考虑美国东部时区 (UTC -0500)，它的 EDT 从三月的第二个星期天 1:59 (EST) 之后一分钟开始，并在十一月的第一个星期天 1:59 (EDT) 之后一分钟结束:

  UTC   3:MM  4:MM  5:MM  6:MM  7:MM  8:MM
  EST  22:MM 23:MM  0:MM  1:MM  2:MM  3:MM
  EDT  23:MM  0:MM  1:MM  2:MM  3:MM  4:MM

start  22:MM 23:MM  0:MM  1:MM  3:MM  4:MM

  end  23:MM  0:MM  1:MM  1:MM  2:MM  3:MM

当 DST 开始时（见 “start” 行），本地时钟从 1:59 跳到 3:00。 形式为 2:MM 的时间值在那一天是没有意义的，因此在 DST 开始的那一天 astimezone(Eastern) 不会输出包含 hour == 2 的结果。 例如，在 2016 年春季时钟向前调整时，我们得到

>>> u0 = datetime(2016, 3, 13, 5, tzinfo=timezone.utc)
>>> for i in range(4):
...     u = u0 + i*HOUR
...     t = u.astimezone(Eastern)
...     print(u.time(), 'UTC =', t.time(), t.tzname())
...
05:00:00 UTC = 00:00:00 EST
06:00:00 UTC = 01:00:00 EST
07:00:00 UTC = 03:00:00 EDT
08:00:00 UTC = 04:00:00 EDT

当 DST 结束时（见 “end” 行），会有更糟糕的潜在问题：本地时间值中有一个小时是不可能没有歧义的：夏令时的最后一小时。 即以美国东部时间表示当天夏令时结束时的形式为 5:MM UTC 的时间。 本地时间从 1:59 (夏令时) 再次跳回到 1:00 (标准时)。 形式为 1:MM 的本地时间就是有歧义的。 此时 astimezone() 是通过将两个相邻的 UTC 小时映射到两个相同的本地小时来模仿本地时钟的行为。 在这个美国东部时间的示例中，形式为 5:MM 和 6:MM 的 UTC 时间在转换为美国东部时间时都将被映射到 1:MM，但前一个时间会将 fold 属性设为 0 而后一个时间会将其设为 1。 例如，在 2016 年秋季时钟往回调整时，我们得到

>>> u0 = datetime(2016, 11, 6, 4, tzinfo=timezone.utc)
>>> for i in range(4):
...     u = u0 + i*HOUR
...     t = u.astimezone(Eastern)
...     print(u.time(), 'UTC =', t.time(), t.tzname(), t.fold)
...
04:00:00 UTC = 00:00:00 EDT 0
05:00:00 UTC = 01:00:00 EDT 0
06:00:00 UTC = 01:00:00 EST 1
07:00:00 UTC = 02:00:00 EST 0

请注意不同的 datetime 实例仅通过 fold 属性值来加以区分，它们在比较时会被视为相等。

不允许时间显示存在歧义的应用需要显式地检查 fold 属性的值，或者避免使用混合式的 tzinfo 子类；当使用 timezone 或者任何其他固定差值的 tzinfo 子类例如仅表示 EST（固定差值 -5 小时）或仅表示 EDT（固定差值 -4 小时）的类时是不会有歧义的。

参见

dateutil.tz [https://dateutil.readthedocs.io/en/stable/tz.html]

该标准库具有 timezone 类用来将相对于 UTC 和 timezone.utc 的任意固定差值处理为 UTC 时区实例。

dateutil.tz 库将 IANA 时区数据库 (又名 Olson 数据库) 引入 Python 并推荐使用。

IANA 时区数据库 [https://www.iana.org/time-zones]

该时区数据库 (通常称为 tz, tzdata 或 zoneinfo) 包含大量代码和数据用来表示全球许多有代表性的地点的本地时间的历史信息。 它会定期进行更新以反映各政治实体对时区边界、UTC 差值和夏令时规则的更改。

8.1.7. timezone 对象

timezone 类是 tzinfo 的一个子类，它的每个实例代表一个由与 UTC 的固定差值所定义的时区。 请注意该类的对象不可被用于代表某些特殊地点的时区信息，例如在一年的不同日期使用不同差值，或是在历史上对民用时间进行过调整的地点。

class datetime.timezone(offset, name=None)

The offset argument must be specified as a timedelta object representing the difference between the local time and UTC. It must be strictly between -timedelta(hours=24) and timedelta(hours=24) and represent a whole number of minutes, otherwise ValueError is raised.

name 参数是可选的。 如果指定则必须为一个字符串，它将被用作 datetime.tzname() 方法的返回值。

3.2 新版功能.

timezone.utcoffset(dt)

返回在构造 timezone 实例时指定的固定值。 dt 参数会被忽略。 返回值是一个 timedelta 实例，其值等于本地时间与 UTC 之间的差值。

timezone.tzname(dt)

返回在构造 timezone 实例时指定的固定值。 如果没有为构造器提供 name，则 tzname(dt) 所返回的名称将根据 offset 值按以下规则生成。 如果 offset 为 timedelta(0)，则名称为 “UTC”，否则为字符串 ‘UTC±HH:MM’，其中 ± 为 offset 值的正负，HH 和 MM 分别为表示 offset.hours 和 offset.minutes 的两个数码。

在 3.6 版更改: 由 offset=timedelta(0) 生成的名称现在改为简单的 ‘UTC’ 而不再是 ‘UTC+00:00’。

timezone.dst(dt)

总是返回 None。

timezone.fromutc(dt)

返回 dt + offset。 dt 参数必须为一个感知型 datetime 实例，其中 tzinfo 值设为 self。

类属性：

timezone.utc

UTC 时区，timezone(timedelta(0))。

8.1.8. strftime() 和 strptime() 的行为

date, datetime 和 time 对象都支持 strftime(format) 方法，可用来创建一个由指定格式字符串所控制的表示时间的字符串。 总体而言，d.strftime(fmt) 类似于 time 模块的 time.strftime(fmt, d.timetuple()) 但是并非所有对象都支持 timetuple() 方法。

相反地，datetime.strptime() 类方法可根据一个表示时间的字符串和对应的格式字符串创建来一个 datetime 对象。 datetime.strptime(date_string, format) 等价于 datetime(*(time.strptime(date_string, format)[0:6]))，差别在于当 format 包含小于秒的部分或者时区差值信息的时候，这些信息被 datetime.strptime 所支持但会被 time.strptime 所丢弃。

对于 time 对象，年、月、日的格式代码不应被使用，因为 time 对象没有这些值。 如果它们被使用，年份将被替换为 1900 而月和日将被替换为 1。

对于 date 对象，时、分、秒和微秒的格式代码不应被使用，因为 date 对象没有这些值。 如果它们被使用，它们都将被替换为 0。

对完整格式代码集的支持在不同平台上有所差异，因为 Python 要调用所在平台 C 库的 strftime() 函数，而不同平台的差异是很常见的。 要查看你所用平台所支持的完整格式代码集，请参阅 strftime(3) 文档。

以下列表显示了 C 标准（1989 版）所要求的全部格式代码，它们在带有标准 C 实现的所有平台上均可用。 请注意 1999 版 C 标准又添加了额外的格式代码。

指令

含义

示例

注释

%a

当地工作日的缩写。

Sun, Mon, …, Sat (美国);

So, Mo, …, Sa (德国)

(1)

%A

当地工作日的全名。

Sunday, Monday, …, Saturday (美国);

Sonntag, Montag, …, Samstag (德国)

(1)

%w

以十进制数显示的工作日，其中0表示星期日，6表示星期六。

0, 1, …, 6

%d

补零后，以十进制数显示的月份中的一天。

01, 02, …, 31

%b

当地月份的缩写。

Jan, Feb, …, Dec (美国);

Jan, Feb, …, Dez (德国)

(1)

%B

当地月份的全名。

January, February, …, December (美国);

Januar, Februar, …, Dezember (德国)

(1)

%m

补零后，以十进制数显示的月份。

01, 02, …, 12

%y

补零后，以十进制数表示的，不带世纪的年份。

00, 01, …, 99

%Y

十进制数表示的带世纪的年份。

0001, 0002, …, 2013, 2014, …, 9998, 9999

(2)

%H

以补零后的十进制数表示的小时（24 小时制）。

00, 01, …, 23

%I

以补零后的十进制数表示的小时（12 小时制）。

01, 02, …, 12

%p

本地化的 AM 或 PM 。

AM, PM (美国);

am, pm (德国)

(1), (3)

%M

补零后，以十进制数显示的分钟。

00, 01, …, 59

%S

补零后，以十进制数显示的秒。

00, 01, …, 59

(4)

%f

以十进制数表示的微秒，在左侧补零。

000000, 000001, …, 999999

(5)

%z

UTC offset in the form +HHMM or -HHMM (empty string if the object is naive).

(empty), +0000, -0400, +1030

(6)

%Z

时区名称（如果对象为简单型则为空字符串）。

(空), UTC, EST, CST

%j

以补零后的十进制数表示的一年中的日序号。

001, 002, …, 366

%U

以补零后的十进制数表示的一年中的周序号（星期日作为每周的第一天）。 在新的一年中第一个星期日之前的所有日子都被视为是在第 0 周。

00, 01, …, 53

(7)

%W

以十进制数表示的一年中的周序号（星期一作为每周的第一天）。 在新的一年中第一个第期一之前的所有日子都被视为是在第 0 周。

00, 01, …, 53

(7)

%c

本地化的适当日期和时间表示。

Tue Aug 16 21:30:00 1988 (美国);

Di 16 Aug 21:30:00 1988 (德国)

(1)

%x

本地化的适当日期表示。

08/16/88 (None);

08/16/1988 (en_US);

16.08.1988 (de_DE)

(1)

%X

本地化的适当时间表示。

21:30:00 (en_US);

21:30:00 (de_DE)

(1)

%%

字面的 '%' 字符。

%

有一些并非 C89 标准所要求的额外指令是为使用方便而加入的。 这些形参都与 ISO 8601 日期值相对应。 它们可能不是在所有平台上都可通过 strftime() 方法使用。 ISO 8601 年份和 ISO 8601 星期指令并不能与上面的年份和星期序号指令相互替代。 调用 strptime() 时传入不完整或有歧义的 ISO 8601 指令将引发 ValueError。

指令

含义

示例

注释

%G

带有世纪的 ISO 8601 年份，表示包含大部分 ISO 星期 (%V) 的年份。

0001, 0002, …, 2013, 2014, …, 9998, 9999

(8)

%u

以十进制数显示的 ISO 8601 星期中的日序号，其中 1 表示星期一。

1, 2, …, 7

%V

以十进制数显示的 ISO 8601 星期，以星期一作为每周的第一天。 第 01 周为包含 1 月 4 日的星期。

01, 02, …, 53

(8)

3.6 新版功能: 增加了 %G, %u 和 %V。

注释:

由于此格式依赖于当前区域设置，因此对具体输出值应当保持谨慎预期。 字段顺序会发生改变（例如 “month/day/year” 与 “day/month/year”），并且输出可能包含使用区域设置所指定的默认编码格式的 Unicode 字符（例如如果当前区域为 ja_JP，则默认编码格式可能为 eucJP, SJIS 或 utf-8 中的一个；使用 locale.getlocale() 可确定当前区域设置的编码格式）。

strptime() 方法能够解析整个 [1, 9999] 范围内的年份，但 < 1000 的年份必须加零填充为 4 位数字宽度。

在 3.2 版更改: 在之前的版本中，strftime() 方法只限于 >= 1900 的年份。

在 3.3 版更改: 在版本3.2中，strftime() 方法只限于 years >= 1000。

当与 strptime() 方法一起使用时，如果使用 %I 指令来解析小时，%p 指令只影响输出小时字段。

与 time 模块不同的是， datetime 模块不支持闰秒。

当与 strptime() 方法一起使用时，%f 指令可接受一至六个数码及左边的零填充。 %f 是对 C 标准中格式字符集的扩展（但单独在 datetime 对象中实现，因此它总是可用）。

对于简单型对象，%z and %Z 格式代码会被替换为空字符串。

对于一个觉悟型对象而言：

%z

utcoffset() is transformed into a 5-character string of the form +HHMM or -HHMM, where HH is a 2-digit string giving the number of UTC offset hours, and MM is a 2-digit string giving the number of UTC offset minutes. For example, if utcoffset() returns timedelta(hours=-3, minutes=-30), %z is replaced with the string '-0330'.

%Z

如果 tzname() 返回 None，%Z 会被替换为一个空字符串。 在其他情况下 %Z 会被替换为返回值，这必须为一个字符串。

在 3.2 版更改: 当提供 %z 指令给 strptime() 方法时，将产生一个感知型 datetime 对象。 结果的 tzinfo 将被设为一个 timezone 实例。

当与 strptime() 方法一起使用时，%U 和 %W 仅用于指定星期几和日历年份 (%Y) 的计算。

类似于 %U 和 %W，%V 仅用于在 strptime() 格式字符串中指定星期几和 ISO 年份 (%G) 的计算。 还要注意 %G 和 %Y 是不可交换的。

备注

1

就是说如果我们忽略相对论效应的话。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

8.2. calendar — 日历相关函数

源代码： Lib/calendar.py [https://github.com/python/cpython/tree/3.6/Lib/calendar.py]

这个模块让你可以输出像 Unix cal 那样的日历，它还提供了其它与日历相关的实用函数。 默认情况下，这些日历把星期一当作一周的第一天，星期天为一周的最后一天（按照欧洲惯例）。 可以使用 setfirstweekday() 方法设置一周的第一天为星期天 (6) 或者其它任意一天。 使用整数作为指定日期的参数。 更多相关的函数，参见 datetime 和 time 模块。

Most of these functions and classes rely on the datetime module which uses an idealized calendar, the current Gregorian calendar extended in both directions. This matches the definition of the “proleptic Gregorian” calendar in Dershowitz and Reingold’s book “Calendrical Calculations”, where it’s the base calendar for all computations.

class calendar.Calendar(firstweekday=0)

创建一个 Calendar 对象。 firstweekday 是一个整数，用于指定一周的第一天。 0 是星期一（默认值），6 是星期天。

Calendar 对象提供了一些可被用于准备日历数据格式化的方法。 这个类本身不执行任何格式化操作。 这部分任务应由子类来完成。

Calendar 类的实例有下列方法：

iterweekdays()

返回一个迭代器，迭代器的内容为一星期的数字。迭代器的第一个值与 firstweekday 属性的值一至。

itermonthdates(year, month)

返回一个迭代器，迭代器的内容为 year 年 month 月(1-12)的日期。这个迭代器返回当月的所有日期 ( datetime.date 对象)，日期包含了本月头尾用于组成完整一周的日期。

itermonthdays2(year, month)

Return an iterator for the month month in the year year similar to itermonthdates(). Days returned will be tuples consisting of a day number and a week day number.

itermonthdays(year, month)

Return an iterator for the month month in the year year similar to itermonthdates(). Days returned will simply be day numbers.

monthdatescalendar(year, month)

返回一个表示指定年月的周列表。周列表由七个 datetime.date 对象组成。

monthdays2calendar(year, month)

返回一个表示指定年月的周列表。周列表由七个代表日期的数字和代表周几的数字组成的二元元组。

monthdayscalendar(year, month)

返回一个表示指定年月的周列表。周列表由七个代表日期的数字组成。

yeardatescalendar(year, width=3)

返回可以用来格式化的指定年月的数据。返回的值是一个列表，列表是月份组成的行。每一行包含了最多 width 个月(默认为3)。每个月包含了4到6周，每周包含1–7天。每一天使用 datetime.date 对象。

yeardays2calendar(year, width=3)

返回可以用来模式化的指定年月的数据(与 yeardatescalendar() 类似)。周列表的元素是由表示日期的数字和表示星期几的数字组成的元组。不在这个月的日子为0。

yeardayscalendar(year, width=3)

返回可以用来模式化的指定年月的数据(与 yeardatescalendar() 类似)。周列表的元素是表示日期的数字。不在这个月的日子为0。

class calendar.TextCalendar(firstweekday=0)

可以使用这个类生成纯文本日历。

TextCalendar 实例有以下方法：

formatmonth(theyear, themonth, w=0, l=0)

返回一个多行字符串来表示指定年月的日历。w 为日期的宽度，但始终保持日期居中。l 指定了每星期占用的行数。以上这些还依赖于构造器或者 setfirstweekday() 方法指定的周的第一天是哪一天。

prmonth(theyear, themonth, w=0, l=0)

与 formatmonth() 方法一样，返回一个月的日历。

formatyear(theyear, w=2, l=1, c=6, m=3)

返回一个多行字符串，这个字符串为一个 m 列日历。可选参数 w, l, 和 c 分别表示日期列数， 周的行数， 和月之间的间隔。同样，以上这些还依赖于构造器或者 setfirstweekday() 指定哪一天为一周的第一天。日历的第一年由平台依赖于使用的平台。

pryear(theyear, w=2, l=1, c=6, m=3)

与 formatyear() 方法一样，返回一整年的日历。

class calendar.HTMLCalendar(firstweekday=0)

可以使用这个类生成 HTML 日历。

HTMLCalendar instances have the following methods:

formatmonth(theyear, themonth, withyear=True)

返回一个 HTML 表格作为指定年月的日历。 withyear 为真，则年份将会包含在表头，否则只显示月份。

formatyear(theyear, width=3)

返回一个 HTML 表格作为指定年份的日历。 width (默认为3) 用于规定每一行显示月份的数量。

formatyearpage(theyear, width=3, css='calendar.css', encoding=None)

返回一个完整的 HTML 页面作为指定年份的日历。 width*(默认为3) 用于规定每一行显示的月份数量。 *css 为层叠样式表的名字。如果不使用任何层叠样式表，可以使用 None 。 encoding 为输出页面的编码 (默认为系统的默认编码)。

class calendar.LocaleTextCalendar(firstweekday=0, locale=None)

这个子类 TextCalendar 可以在构造函数中传递一个语言环境名称，并且返回月份和星期几的名称在特定语言环境中。如果此语言环境包含编码，则包含月份和工作日名称的所有字符串将作为 unicode 返回。

class calendar.LocaleHTMLCalendar(firstweekday=0, locale=None)

This subclass of HTMLCalendar can be passed a locale name in the constructor and will return month and weekday names in the specified locale. If this locale includes an encoding all strings containing month and weekday names will be returned as unicode.

注解

这两个类的 formatweekday() 和 formatmonthname() 方法临时更改dang当前区域至给定 locale 。由于当前的区域设置是进程范围的设置，因此它们不是线程安全的。

对于简单的文本日历，这个模块提供了以下方法。

calendar.setfirstweekday(weekday)

设置每一周的开始(0 表示星期一，6 表示星期天)。calendar还提供了 MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY 和 SUNDAY 几个常量方便使用。例如，设置每周的第一天为星期天

import calendar
calendar.setfirstweekday(calendar.SUNDAY)

calendar.firstweekday()

返回当前设置的每星期的第一天的数值。

calendar.isleap(year)

如果 year 是闰年则返回 True ,否则返回 False。

calendar.leapdays(y1, y2)

返回在范围 y1 至 y2 （包含 y1 和 y2 ）之间的闰年的年数，其中 y1 和 y2 是年份。

此函数适用于跨越一个世纪变化的范围。

calendar.weekday(year, month, day)

返回一周中的某一天 （ 0 是周一） 以年（ 1970 – …），月（ 1 – 12 ），日（ 1 – 31 ）的格式。

calendar.weekheader(n)

返回一个包含星期几的缩写名的头。 n 指定星期几缩写的字符宽度。

calendar.monthrange(year, month)

返回指定年份的指定月份第一天是星期几和这个月的天数。

calendar.monthcalendar(year, month)

返回表示一个月的日历的矩阵。 每一行代表一周；此月份外的日子由零表示。 每周从周一开始，除非使用 setfirstweekday() 改变设置。

calendar.prmonth(theyear, themonth, w=0, l=0)

打印由 month() 返回的一个月的日历。

calendar.month(theyear, themonth, w=0, l=0)

使用 TextCalendar 类的 formatmonth() 以多行字符串形式返回月份日历。

calendar.prcal(year, w=0, l=0, c=6, m=3)

打印由 calendar() 返回的整年的日历。

calendar.calendar(year, w=2, l=1, c=6, m=3)

使用 TextCalendar 类的 formatyear() 返回整年的三列的日历以多行字符串的形式。

calendar.timegm(tuple)

一个不相关但很好用的函数，它接受一个时间元组例如 time 模块中的 gmtime() 函数的返回并返回相应的 Unix 时间戳，假定 1970 年开始计数， POSIX 编码。实际上， time.gmtime() 和 timegm() 是彼此相反的。

calendar 模块导出以下数据属性：

calendar.day_name

在当前的语言环境下表示星期几。

calendar.day_abbr

在当前语言环境下表示星期几缩写的数组。

calendar.month_name

在当前语言环境下表示一年中月份的数组。这遵循一月的月号为 1 的通常惯例，所以它的长度为 13 且 month_name[0] 是空字符串。

calendar.month_abbr

在当前语言环境下表示月份简写的数组。这遵循一月的月号为 1 的通常惯例，所以它的长度为 13 且 month_abbr[0] 是空字符串。

参见

模块 datetime

为日期和时间提供与 time 模块相似功能的面向对象接口。

模块 time

底层时间相关函数。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

8.3. collections — 容器数据类型

Source code: Lib/collections/__init__.py [https://github.com/python/cpython/tree/3.6/Lib/collections/__init__.py]

这个模块实现了特定目标的容器，以提供Python标准内建容器 dict , list , set , 和 tuple 的替代选择。

namedtuple()

创建命名元组子类的工厂函数

deque

类似列表(list)的容器，实现了在两端快速添加(append)和弹出(pop)

ChainMap

类似字典(dict)的容器类，将多个映射集合到一个视图里面

Counter

字典的子类，提供了可哈希对象的计数功能

OrderedDict

字典的子类，保存了他们被添加的顺序

defaultdict

字典的子类，提供了一个工厂函数，为字典查询提供一个默认值

UserDict

封装了字典对象，简化了字典子类化

UserList

封装了列表对象，简化了列表子类化

UserString

封装了列表对象，简化了字符串子类化

在 3.3 版更改: Moved 容器抽象基类 to the collections.abc module. For backwards compatibility, they continue to be visible in this module as well.

8.3.1. ChainMap 对象

3.3 新版功能.

一个 ChainMap 类是为了将多个映射快速的链接到一起，这样它们就可以作为一个单元处理。它通常比创建一个新字典和多次调用 update() 要快很多。

这个类可以用于模拟嵌套作用域，并且在模版化的时候比较有用。

class collections.ChainMap(*maps)

一个 ChainMap 将多个字典或者其他映射组合在一起，创建一个单独的可更新的视图。 如果没有 maps 被指定，就提供一个默认的空字典，这样一个新链至少有一个映射。

底层映射被存储在一个列表中。这个列表是公开的，可以通过 maps 属性存取和更新。没有其他的状态。

搜索查询底层映射，直到一个键被找到。不同的是，写，更新和删除只操作第一个映射。

一个 ChainMap 通过引用合并底层映射。 所以，如果一个底层映射更新了，这些更改会反映到 ChainMap 。

支持所有常用字典方法。另外还有一个 maps 属性(attribute)，一个创建子上下文的方法(method)， 一个存取它们首个映射的属性(property):

maps

一个可以更新的映射列表。这个列表是按照第一次搜索到最后一次搜索的顺序组织的。它是仅有的存储状态，可以被修改。列表最少包含一个映射。

new_child(m=None)

返回一个新的 ChainMap 类，包含了一个新映射(map)，后面跟随当前实例的全部映射(map)。如果 m 被指定，它就成为不同新的实例，就是在所有映射前加上 m，如果没有指定，就加上一个空字典，这样的话一个 d.new_child() 调用等价于 ChainMap({}, *d.maps) 。这个方法用于创建子上下文，不改变任何父映射的值。

在 3.4 版更改: 添加了 m 可选参数。

parents

属性返回一个新的 ChainMap 包含所有的当前实例的映射，除了第一个。这样可以在搜索的时候跳过第一个映射。 使用的场景类似在 nested scopes 嵌套作用域中使用 nonlocal 关键词。用例也可以类比内建函数 super() 。一个 d.parents 的引用等价于 ChainMap(*d.maps[1:]) 。

参见

MultiContext class [https://github.com/enthought/codetools/blob/4.0.0/codetools/contexts/multi_context.py] 在 Enthought CodeTools package [https://github.com/enthought/codetools] 有支持写映射的选项。

Django’s Context class [https://github.com/django/django/blob/master/django/template/context.py] for templating is a read-only chain of mappings. It also features pushing and popping of contexts similar to the new_child() method and the parents() property.

Nested Contexts recipe [https://code.activestate.com/recipes/577434/] 提供了是否对第一个映射或其他映射进行写和其他修改的选项。

一个 极简的只读版 Chainmap [https://code.activestate.com/recipes/305268/].

8.3.1.1. ChainMap 例子和方法

这一节提供了多个使用链映射的案例。

模拟Python内部lookup链的例子

import builtins
pylookup = ChainMap(locals(), globals(), vars(builtins))

让用户指定的命令行参数优先于环境变量，优先于默认值的例子

import os, argparse

defaults = {'color': 'red', 'user': 'guest'}

parser = argparse.ArgumentParser()
parser.add_argument('-u', '--user')
parser.add_argument('-c', '--color')
namespace = parser.parse_args()
command_line_args = {k:v for k, v in vars(namespace).items() if v}

combined = ChainMap(command_line_args, os.environ, defaults)
print(combined['color'])
print(combined['user'])

用 ChainMap 类模拟嵌套上下文的例子

c = ChainMap()        # Create root context
d = c.new_child()     # Create nested child context
e = c.new_child()     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals()
e.maps[-1]            # Root context -- like Python's globals()
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x']                # Get first key in the chain of contexts
d['x'] = 1            # Set value in current context
del d['x']            # Delete from current context
list(d)               # All nested values
k in d                # Check all nested values
len(d)                # Number of nested values
d.items()             # All nested items
dict(d)               # Flatten into a regular dictionary

ChainMap 类只更新链中的第一个映射，但lookup会搜索整个链。 然而，如果需要深度写和删除，也可以很容易的通过定义一个子类来实现它

class DeepChainMap(ChainMap):
    'Variant of ChainMap that allows direct updates to inner scopes'

    def __setitem__(self, key, value):
        for mapping in self.maps:
            if key in mapping:
                mapping[key] = value
                return
        self.maps[0][key] = value

    def __delitem__(self, key):
        for mapping in self.maps:
            if key in mapping:
                del mapping[key]
                return
        raise KeyError(key)

>>> d = DeepChainMap({'zebra': 'black'}, {'elephant': 'blue'}, {'lion': 'yellow'})
>>> d['lion'] = 'orange'         # update an existing key two levels down
>>> d['snake'] = 'red'           # new keys get added to the topmost dict
>>> del d['elephant']            # remove an existing key one level down
>>> d                            # display result
DeepChainMap({'zebra': 'black', 'snake': 'red'}, {}, {'lion': 'orange'})

8.3.2. Counter 对象

一个计数器工具提供快速和方便的计数。比如

>>> # Tally occurrences of words in a list
>>> cnt = Counter()
>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
...     cnt[word] += 1
>>> cnt
Counter({'blue': 3, 'red': 2, 'green': 1})

>>> # Find the ten most common words in Hamlet
>>> import re
>>> words = re.findall(r'\w+', open('hamlet.txt').read().lower())
>>> Counter(words).most_common(10)
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
 ('you', 554),  ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]

class collections.Counter([iterable-or-mapping])

A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.

元素从一个 iterable 被计数或从其他的 mapping (or counter)初始化：

>>> c = Counter()                           # a new, empty counter
>>> c = Counter('gallahad')                 # a new counter from an iterable
>>> c = Counter({'red': 4, 'blue': 2})      # a new counter from a mapping
>>> c = Counter(cats=4, dogs=8)             # a new counter from keyword args

Counter对象有一个字典接口，如果引用的键没有任何记录，就返回一个0，而不是弹出一个 KeyError :

>>> c = Counter(['eggs', 'ham'])
>>> c['bacon']                              # count of a missing element is zero
0

设置一个计数为0不会从计数器中移去一个元素。使用 del 来删除它:

>>> c['sausage'] = 0                        # counter entry with a zero count
>>> del c['sausage']                        # del actually removes the entry

3.1 新版功能.

计数器对象除了字典方法以外，还提供了三个其他的方法：

elements()

返回一个迭代器，每个元素重复计数的个数。元素顺序是任意的。如果一个元素的计数小于1， elements() 就会忽略它。

>>> c = Counter(a=4, b=2, c=0, d=-2)
>>> sorted(c.elements())
['a', 'a', 'a', 'a', 'b', 'b']

most_common([n])

Return a list of the n most common elements and their counts from the most common to the least. If n is omitted or None, most_common() returns all elements in the counter. Elements with equal counts are ordered arbitrarily:

>>> Counter('abracadabra').most_common(3)
[('a', 5), ('r', 2), ('b', 2)]

subtract([iterable-or-mapping])

从 迭代对象 或 映射对象 减去元素。像 dict.update() 但是是减去，而不是替换。输入和输出都可以是0或者负数。

>>> c = Counter(a=4, b=2, c=0, d=-2)
>>> d = Counter(a=1, b=2, c=3, d=4)
>>> c.subtract(d)
>>> c
Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})

3.2 新版功能.

通常字典方法都可用于 Counter 对象，除了有两个方法工作方式与字典并不相同。

fromkeys(iterable)

这个类方法没有在 Counter 中实现。

update([iterable-or-mapping])

从 迭代对象 计数元素或者 从另一个 映射对象 (或计数器) 添加。 像 dict.update() 但是是加上，而不是替换。另外，迭代对象 应该是序列元素，而不是一个 (key, value) 对。

Counter 对象的常用案例

sum(c.values())                 # total of all counts
c.clear()                       # reset all counts
list(c)                         # list unique elements
set(c)                          # convert to a set
dict(c)                         # convert to a regular dictionary
c.items()                       # convert to a list of (elem, cnt) pairs
Counter(dict(list_of_pairs))    # convert from a list of (elem, cnt) pairs
c.most_common()[:-n-1:-1]       # n least common elements
+c                              # remove zero and negative counts

提供了几个数学操作，可以结合 Counter 对象，以生产 multisets (计数器中大于0的元素）。 加和减，结合计数器，通过加上或者减去元素的相应计数。交集和并集返回相应计数的最小或最大值。每种操作都可以接受带符号的计数，但是输出会忽略掉结果为零或者小于零的计数。

>>> c = Counter(a=3, b=1)
>>> d = Counter(a=1, b=2)
>>> c + d                       # add two counters together:  c[x] + d[x]
Counter({'a': 4, 'b': 3})
>>> c - d                       # subtract (keeping only positive counts)
Counter({'a': 2})
>>> c & d                       # intersection:  min(c[x], d[x])
Counter({'a': 1, 'b': 1})
>>> c | d                       # union:  max(c[x], d[x])
Counter({'a': 3, 'b': 2})

单目加和减（一元操作符）意思是从空计数器加或者减去。

>>> c = Counter(a=2, b=-4)
>>> +c
Counter({'a': 2})
>>> -c
Counter({'b': 4})

3.3 新版功能: 添加了对一元加，一元减和位置集合操作的支持。

注解

计数器主要是为了表达运行的正的计数而设计；但是，小心不要预先排除负数或者其他类型。为了帮助这些用例，这一节记录了最小范围和类型限制。

Counter 类是一个字典的子类，不限制键和值。值用于表示计数，但你实际上 可以 存储任何其他值。

The most_common() method requires only that the values be orderable.

For in-place operations such as c[key] += 1, the value type need only support addition and subtraction. So fractions, floats, and decimals would work and negative values are supported. The same is also true for update() and subtract() which allow negative and zero values for both inputs and outputs.

Multiset多集合方法只为正值的使用情况设计。输入可以是负数或者0，但只输出计数为正的值。没有类型限制，但值类型需要支持加，减和比较操作。

The elements() method requires integer counts. It ignores zero and negative counts.

参见

Bag class [https://www.gnu.org/software/smalltalk/manual-base/html_node/Bag.html] 在 Smalltalk。

Wikipedia 链接 Multisets [https://en.wikipedia.org/wiki/Multiset].

C++ multisets [http://www.java2s.com/Tutorial/Cpp/0380__set-multiset/Catalog0380__set-multiset.htm] 教程和例子。

数学操作和多集合用例，参考 Knuth, Donald. The Art of Computer Programming Volume II, Section 4.6.3, Exercise 19 。

To enumerate all distinct multisets of a given size over a given set of elements, see itertools.combinations_with_replacement():

map(Counter, combinations_with_replacement(‘ABC’, 2)) –> AA AB AC BB BC CC

8.3.3. deque 对象

class collections.deque([iterable[, maxlen]])

返回一个新的双向队列对象，从左到右初始化(用方法 append()) ，从 iterable （迭代对象) 数据创建。如果 iterable 没有指定，新队列为空。

Deque队列是由栈或者queue队列生成的（发音是 “deck”，”double-ended queue”的简称）。Deque 支持线程安全，内存高效添加(append)和弹出(pop)，从两端都可以，两个方向的大概开销都是 O(1) 复杂度。

虽然 list 对象也支持类似操作，不过这里优化了定长操作和 pop(0) 和 insert(0, v) 的开销。它们引起 O(n) 内存移动的操作，改变底层数据表达的大小和位置。

如果 maxlen 没有指定或者是 None ，deques 可以增长到任意长度。否则，deque就限定到指定最大长度。一旦限定长度的deque满了，当新项加入时，同样数量的项就从另一端弹出。限定长度deque提供类似Unix filter tail 的功能。它们同样可以用与追踪最近的交换和其他数据池活动。

双向队列(deque)对象支持以下方法：

append(x)

添加 x 到右端。

appendleft(x)

添加 x 到左端。

clear()

移除所有元素，使其长度为0.

copy()

创建一份浅拷贝。

3.5 新版功能.

count(x)

计算 deque 中元素等于 x 的个数。

3.2 新版功能.

extend(iterable)

扩展deque的右侧，通过添加iterable参数中的元素。

extendleft(iterable)

扩展deque的左侧，通过添加iterable参数中的元素。注意，左添加时，在结果中iterable参数中的顺序将被反过来添加。

index(x[, start[, stop]])

返回 x 在 deque 中的位置（在索引 start 之后，索引 stop 之前）。 返回第一个匹配项，如果未找到则引发 ValueError。

3.5 新版功能.

insert(i, x)

在位置 i 插入 x 。

如果插入会导致一个限长 deque 超出长度 maxlen 的话，就引发一个 IndexError。

3.5 新版功能.

pop()

移去并且返回一个元素，deque 最右侧的那一个。 如果没有元素的话，就引发一个 IndexError。

popleft()

移去并且返回一个元素，deque 最左侧的那一个。 如果没有元素的话，就引发 IndexError。

remove(value)

移除找到的第一个 value。 如果没有的话就引发 ValueError。

reverse()

将deque逆序排列。返回 None 。

3.2 新版功能.

rotate(n=1)

向右循环移动 n 步。 如果 n 是负数，就向左循环。

如果deque不是空的，向右循环移动一步就等价于 d.appendleft(d.pop()) ， 向左循环一步就等价于 d.append(d.popleft()) 。

Deque对象同样提供了一个只读属性:

maxlen

Deque的最大尺寸，如果没有限定的话就是 None 。

3.1 新版功能.

除了以上，deque还支持迭代，清洗，len(d), reversed(d), copy.copy(d), copy.deepcopy(d), 成员测试 in 操作符，和下标引用 d[-1] 。索引存取在两端的复杂度是 O(1)， 在中间的复杂度比 O(n) 略低。要快速存取，使用list来替代。

Deque从版本3.5开始支持 __add__(), __mul__(), 和 __imul__() 。

示例:

>>> from collections import deque
>>> d = deque('ghi')                 # make a new deque with three items
>>> for elem in d:                   # iterate over the deque's elements
...     print(elem.upper())
G
H
I

>>> d.append('j')                    # add a new entry to the right side
>>> d.appendleft('f')                # add a new entry to the left side
>>> d                                # show the representation of the deque
deque(['f', 'g', 'h', 'i', 'j'])

>>> d.pop()                          # return and remove the rightmost item
'j'
>>> d.popleft()                      # return and remove the leftmost item
'f'
>>> list(d)                          # list the contents of the deque
['g', 'h', 'i']
>>> d[0]                             # peek at leftmost item
'g'
>>> d[-1]                            # peek at rightmost item
'i'

>>> list(reversed(d))                # list the contents of a deque in reverse
['i', 'h', 'g']
>>> 'h' in d                         # search the deque
True
>>> d.extend('jkl')                  # add multiple elements at once
>>> d
deque(['g', 'h', 'i', 'j', 'k', 'l'])
>>> d.rotate(1)                      # right rotation
>>> d
deque(['l', 'g', 'h', 'i', 'j', 'k'])
>>> d.rotate(-1)                     # left rotation
>>> d
deque(['g', 'h', 'i', 'j', 'k', 'l'])

>>> deque(reversed(d))               # make a new deque in reverse order
deque(['l', 'k', 'j', 'i', 'h', 'g'])
>>> d.clear()                        # empty the deque
>>> d.pop()                          # cannot pop from an empty deque
Traceback (most recent call last):
    File "<pyshell#6>", line 1, in -toplevel-
        d.pop()
IndexError: pop from an empty deque

>>> d.extendleft('abc')              # extendleft() reverses the input order
>>> d
deque(['c', 'b', 'a'])

8.3.3.1. deque 用法

这一节展示了deque的多种用法。

限长deque提供了类似Unix tail 过滤功能

def tail(filename, n=10):
    'Return the last n lines of a file'
    with open(filename) as f:
        return deque(f, n)

另一个用法是维护一个近期添加元素的序列，通过从右边添加和从左边弹出

def moving_average(iterable, n=3):
    # moving_average([40, 30, 50, 46, 39, 44]) --> 40.0 42.0 45.0 43.0
    # http://en.wikipedia.org/wiki/Moving_average
    it = iter(iterable)
    d = deque(itertools.islice(it, n-1))
    d.appendleft(0)
    s = sum(d)
    for elem in it:
        s += elem - d.popleft()
        d.append(elem)
        yield s / n

The rotate() method provides a way to implement deque slicing and deletion. For example, a pure Python implementation of del d[n] relies on the rotate() method to position elements to be popped:

def delete_nth(d, n):
    d.rotate(-n)
    d.popleft()
    d.rotate(n)

To implement deque slicing, use a similar approach applying rotate() to bring a target element to the left side of the deque. Remove old entries with popleft(), add new entries with extend(), and then reverse the rotation. With minor variations on that approach, it is easy to implement Forth style stack manipulations such as dup, drop, swap, over, pick, rot, and roll.

8.3.4. defaultdict 对象

class collections.defaultdict([default_factory[, ...]])

返回一个新的类似字典的对象。 defaultdict 是内置 dict 类的子类。它重载了一个方法并添加了一个可写的实例变量。其余的功能与 dict 类相同，此处不再重复说明。

第一个参数 default_factory 提供了一个初始值。它默认为 None 。所有的其他参数都等同与 dict 构建器中的参数对待，包括关键词参数。

defaultdict 对象除了支持 dict 的操作，还支持下面的方法作为扩展:

__missing__(key)

如果 default_factory 属性为 None，则调用本方法会抛出 KeyError 异常，附带参数 key。

如果 default_factory 不为 None ， 它就会会被调用，不带参数，为 key 提供一个默认值， 这个值和 key 作为一个对被插入到字典中，并返回。

如果调用 default_factory 时抛出了异常，这个异常会原封不动地向外层传递。

在无法找到所需键值时，本方法会被 dict 中的 __getitem__() 方法调用。无论本方法返回了值还是抛出了异常，都会被 __getitem__() 传递。

注意 __missing__() 不会 被 __getitem__() 以外的其他方法调用。意思就是 get() 会向正常的dict那样返回 None ，而不是使用 default_factory 。

defaultdict 支持以下实例变量:

default_factory

这个属性被 __missing__() 方法使用；它从构建器的第一个参数初始化，如果提供了的话，否则就是 None 。

8.3.4.1. defaultdict 例子

Using list as the default_factory, it is easy to group a sequence of key-value pairs into a dictionary of lists:

>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
...     d[k].append(v)
...
>>> sorted(d.items())
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]

When each key is encountered for the first time, it is not already in the mapping; so an entry is automatically created using the default_factory function which returns an empty list. The list.append() operation then attaches the value to the new list. When keys are encountered again, the look-up proceeds normally (returning the list for that key) and the list.append() operation adds another value to the list. This technique is simpler and faster than an equivalent technique using dict.setdefault():

>>> d = {}
>>> for k, v in s:
...     d.setdefault(k, []).append(v)
...
>>> sorted(d.items())
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]

Setting the default_factory to int makes the defaultdict useful for counting (like a bag or multiset in other languages):

>>> s = 'mississippi'
>>> d = defaultdict(int)
>>> for k in s:
...     d[k] += 1
...
>>> sorted(d.items())
[('i', 4), ('m', 1), ('p', 2), ('s', 4)]

When a letter is first encountered, it is missing from the mapping, so the default_factory function calls int() to supply a default count of zero. The increment operation then builds up the count for each letter.

函数 int() 总是返回0，是常数函数的特殊情况。一个更快和灵活的方法是使用lambda函数，可以提供任何常量值(不只是0):

>>> def constant_factory(value):
...     return lambda: value
>>> d = defaultdict(constant_factory('<missing>'))
>>> d.update(name='John', action='ran')
>>> '%(name)s %(action)s to %(object)s' % d
'John ran to <missing>'

Setting the default_factory to set makes the defaultdict useful for building a dictionary of sets:

>>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
>>> d = defaultdict(set)
>>> for k, v in s:
...     d[k].add(v)
...
>>> sorted(d.items())
[('blue', {2, 4}), ('red', {1, 3})]

8.3.5. namedtuple() 命名元组的工厂函数

命名元组赋予每个位置一个含义，提供可读性和自文档性。它们可以用于任何普通元组，并添加了通过名字获取值的能力，通过索引值也是可以的。

collections.namedtuple(typename, field_names, *, verbose=False, rename=False, module=None)

返回一个新的元组子类，名为 typename 。这个新的子类用于创建类元组的对象，可以通过域名来获取属性值，同样也可以通过索引和迭代获取值。子类实例同样有文档字符串（类名和域名）另外一个有用的 __repr__() 方法，以 name=value 格式列明了元组内容。

field_names 是一个像 [‘x’, ‘y’] 一样的字符串序列。另外 field_names 可以是一个纯字符串，用空白或逗号分隔开元素名，比如 'x y' 或者 'x, y' 。

任何有效的Python 标识符都可以作为域名，除了下划线开头的那些。有效标识符由字母，数字，下划线组成，但首字母不能是数字或下划线，另外不能是关键词 keyword 比如 class, for, return, global, pass, 或 raise 。

如果 rename 为真， 无效域名会自动转换成位置名。比如 ['abc', 'def', 'ghi', 'abc'] 转换成 ['abc', '_1', 'ghi', '_3'] ， 消除关键词 def 和重复域名 abc 。

If verbose is true, the class definition is printed after it is built. This option is outdated; instead, it is simpler to print the _source attribute.

如果 module 值有定义，命名元组的 __module__ 属性值就被设置。

命名元组实例没有字典，所以它们要更轻量，并且占用更小内存。

在 3.1 版更改: 添加了对 rename 的支持。

在 3.6 版更改: verbose 和 rename 参数成为 仅限关键字参数.

在 3.6 版更改: 添加了 module 参数。

>>> # Basic example
>>> Point = namedtuple('Point', ['x', 'y'])
>>> p = Point(11, y=22)     # instantiate with positional or keyword arguments
>>> p[0] + p[1]             # indexable like the plain tuple (11, 22)
33
>>> x, y = p                # unpack like a regular tuple
>>> x, y
(11, 22)
>>> p.x + p.y               # fields also accessible by name
33
>>> p                       # readable __repr__ with a name=value style
Point(x=11, y=22)

命名元组尤其有用于赋值 csv sqlite3 模块返回的元组

EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')

import csv
for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))):
    print(emp.name, emp.title)

import sqlite3
conn = sqlite3.connect('/companydata')
cursor = conn.cursor()
cursor.execute('SELECT name, age, title, department, paygrade FROM employees')
for emp in map(EmployeeRecord._make, cursor.fetchall()):
    print(emp.name, emp.title)

除了继承元组的方法，命名元组还支持三个额外的方法和两个属性。为了防止域名冲突，方法和属性以下划线开始。

classmethod somenamedtuple._make(iterable)

类方法从存在的序列或迭代实例创建一个新实例。

>>> t = [11, 22]
>>> Point._make(t)
Point(x=11, y=22)

somenamedtuple._asdict()

Return a new OrderedDict which maps field names to their corresponding values:

>>> p = Point(x=11, y=22)
>>> p._asdict()
OrderedDict([('x', 11), ('y', 22)])

在 3.1 版更改: 返回一个 OrderedDict 而不是 dict 。

somenamedtuple._replace(**kwargs)

返回一个新的命名元组实例，并将指定域替换为新的值

>>> p = Point(x=11, y=22)
>>> p._replace(x=33)
Point(x=33, y=22)

>>> for partnum, record in inventory.items():
...     inventory[partnum] = record._replace(price=newprices[partnum], timestamp=time.now())

somenamedtuple._source

A string with the pure Python source code used to create the named tuple class. The source makes the named tuple self-documenting. It can be printed, executed using exec(), or saved to a file and imported.

3.3 新版功能.

somenamedtuple._fields

字符串元组列出了域名。用于提醒和从现有元组创建一个新的命名元组类型。

>>> p._fields            # view the field names
('x', 'y')

>>> Color = namedtuple('Color', 'red green blue')
>>> Pixel = namedtuple('Pixel', Point._fields + Color._fields)
>>> Pixel(11, 22, 128, 255, 0)
Pixel(x=11, y=22, red=128, green=255, blue=0)

要获取这个名字域的值，使用 getattr() 函数 :

>>> getattr(p, 'x')
11

To convert a dictionary to a named tuple, use the double-star-operator (as described in 解包参数列表):

>>> d = {'x': 11, 'y': 22}
>>> Point(**d)
Point(x=11, y=22)

因为一个命名元组是一个正常的Python类，它可以很容易的通过子类更改功能。这里是如何添加一个计算域和定宽输出打印格式:

>>> class Point(namedtuple('Point', ['x', 'y'])):
...     __slots__ = ()
...     @property
...     def hypot(self):
...         return (self.x ** 2 + self.y ** 2) ** 0.5
...     def __str__(self):
...         return 'Point: x=%6.3f  y=%6.3f  hypot=%6.3f' % (self.x, self.y, self.hypot)

>>> for p in Point(3, 4), Point(14, 5/7):
...     print(p)
Point: x= 3.000  y= 4.000  hypot= 5.000
Point: x=14.000  y= 0.714  hypot=14.018

上面的子类设置 __slots__ 为一个空元组。通过阻止创建实例字典保持了较低的内存开销。

Subclassing is not useful for adding new, stored fields. Instead, simply create a new named tuple type from the _fields attribute:

>>> Point3D = namedtuple('Point3D', Point._fields + ('z',))

文档字符串可以自定义，通过直接赋值给 __doc__ 属性:

>>> Book = namedtuple('Book', ['id', 'title', 'authors'])
>>> Book.__doc__ += ': Hardcover book in active collection'
>>> Book.id.__doc__ = '13-digit ISBN'
>>> Book.title.__doc__ = 'Title of first printing'
>>> Book.authors.__doc__ = 'List of authors sorted by last name'

在 3.5 版更改: 文档字符串属性变成可写。

Default values can be implemented by using _replace() to customize a prototype instance:

>>> Account = namedtuple('Account', 'owner balance transaction_count')
>>> default_account = Account('<owner name>', 0.0, 0)
>>> johns_account = default_account._replace(owner='John')
>>> janes_account = default_account._replace(owner='Jane')

参见

Recipe for named tuple abstract base class with a metaclass mix-in [https://code.activestate.com/recipes/577629-namedtupleabc-abstract-base-class-mix-in-for-named/] by Jan Kaliszewski. Besides providing an abstract base class for named tuples, it also supports an alternate metaclass-based constructor that is convenient for use cases where named tuples are being subclassed.

对于以字典为底层的可变域名， 参考 types.SimpleNamespace() 。

See typing.NamedTuple() for a way to add type hints for named tuples.

8.3.6. OrderedDict 对象

Ordered dictionaries are just like regular dictionaries but they remember the order that items were inserted. When iterating over an ordered dictionary, the items are returned in the order their keys were first added.

class collections.OrderedDict([items])

Return an instance of a dict subclass, supporting the usual dict methods. An OrderedDict is a dict that remembers the order that keys were first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.

3.1 新版功能.

popitem(last=True)

有序字典的 popitem() 方法移除并返回一个 (key, value) 键值对。 如果 last 值为真，则按 LIFO 后进先出的顺序返回键值对，否则就按 FIFO 先进先出的顺序返回键值对。

move_to_end(key, last=True)

将现有 key 移动到有序字典的任一端。 如果 last 为真值（默认）则将元素移至末尾；如果 last 为假值则将元素移至开头。如果 key 不存在则会触发 KeyError:

>>> d = OrderedDict.fromkeys('abcde')
>>> d.move_to_end('b')
>>> ''.join(d.keys())
'acdeb'
>>> d.move_to_end('b', last=False)
>>> ''.join(d.keys())
'bacde'

3.2 新版功能.

相对于通常的映射方法，有序字典还另外提供了逆序迭代的支持，通过 reversed() 。

OrderedDict 之间的相等测试是顺序敏感的，实现为 list(od1.items())==list(od2.items()) 。 OrderedDict 对象和其他的 Mapping 的相等测试，是顺序敏感的字典测试。这允许 OrderedDict 替换为任何字典可以使用的场所。

在 3.5 版更改: OrderedDict 的项(item)，键(key)和值(value) 视图 现在支持逆序迭代，通过 reversed() 。

在 3.6 版更改: PEP 468 [https://www.python.org/dev/peps/pep-0468] 赞成将关键词参数的顺序保留， 通过传递给 OrderedDict 构造器和它的 update() 方法。

8.3.6.1. OrderedDict 例子和用法

Since an ordered dictionary remembers its insertion order, it can be used in conjunction with sorting to make a sorted dictionary:

>>> # regular unsorted dictionary
>>> d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}

>>> # dictionary sorted by key
>>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))
OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])

>>> # dictionary sorted by value
>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))
OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])

>>> # dictionary sorted by length of the key string
>>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])

The new sorted dictionaries maintain their sort order when entries are deleted. But when new keys are added, the keys are appended to the end and the sort is not maintained.

It is also straight-forward to create an ordered dictionary variant that remembers the order the keys were last inserted. If a new entry overwrites an existing entry, the original insertion position is changed and moved to the end:

class LastUpdatedOrderedDict(OrderedDict):
    'Store items in the order the keys were last added'

    def __setitem__(self, key, value):
        if key in self:
            del self[key]
        OrderedDict.__setitem__(self, key, value)

An ordered dictionary can be combined with the Counter class so that the counter remembers the order elements are first encountered:

class OrderedCounter(Counter, OrderedDict):
    'Counter that remembers the order elements are first encountered'

    def __repr__(self):
        return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))

    def __reduce__(self):
        return self.__class__, (OrderedDict(self),)

8.3.7. UserDict 对象

UserDict 类是用作字典对象的外包装。对这个类的需求已部分由直接创建 dict 的子类的功能所替代；不过，这个类处理起来更容易，因为底层的字典可以作为属性来访问。

class collections.UserDict([initialdata])

模拟一个字典类。这个实例的内容保存为一个正常字典， 可以通过 UserDict 实例的 data 属性存取。如果提供了 initialdata 值， data 就被初始化为它的内容；注意一个 initialdata 的引用不会被保留作为其他用途。

UserDict 实例提供了以下属性作为扩展方法和操作的支持:

data

一个真实的字典，用于保存 UserDict 类的内容。

8.3.8. UserList 对象

这个类封装了列表对象。它是一个有用的基础类，对于你想自定义的类似列表的类，可以继承和覆盖现有的方法，也可以添加新的方法。这样我们可以对列表添加新的行为。

对这个类的需求已部分由直接创建 list 的子类的功能所替代；不过，这个类处理起来更容易，因为底层的列表可以作为属性来访问。

class collections.UserList([list])

模拟一个列表。这个实例的内容被保存为一个正常列表，通过 UserList 的 data 属性存取。实例内容被初始化为一个 list 的copy，默认为 [] 空列表。 list 可以是迭代对象，比如一个Python列表，或者一个 UserList 对象。

UserList 提供了以下属性作为可变序列的方法和操作的扩展:

data

一个 list 对象用于存储 UserList 的内容。

子类化的要求: UserList 的子类需要提供一个构造器，可以无参数调用，或者一个参数调用。返回一个新序列的列表操作需要创建一个实现类的实例。它假定了构造器可以以一个参数进行调用，这个参数是一个序列对象，作为数据源。

如果一个分离的类不希望依照这个需求，所有的特殊方法就必须重写；请参照源代码进行修改。

8.3.9. UserString 对象

UserString 类是用作字符串对象的外包装。对这个类的需求已部分由直接创建 str 的子类的功能所替代；不过，这个类处理起来更容易，因为底层的字符串可以作为属性来访问。

class collections.UserString([sequence])

Class that simulates a string or a Unicode string object. The instance’s content is kept in a regular string object, which is accessible via the data attribute of UserString instances. The instance’s contents are initially set to a copy of sequence. The sequence can be an instance of bytes, str, UserString (or a subclass) or an arbitrary sequence which can be converted into a string using the built-in str() function.

在 3.5 版更改: 新方法 __getnewargs__, __rmod__, casefold, format_map, isprintable, 和 maketrans。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

8.4. collections.abc — 容器的抽象基类

3.3 新版功能: 该模块曾是 collections 模块的组成部分。

源代码： Lib/_collections_abc.py [https://github.com/python/cpython/tree/3.6/Lib/_collections_abc.py]

该模块定义了一些 抽象基类，它们可用于判断一个具体类是否具有某一特定的接口；例如，这个类是否可哈希，或其是否为映射类。

8.4.1. 容器抽象基类

这个容器模块提供了以下 ABCs:

抽象基类

继承自

抽象方法

Mixin 方法

Container

__contains__

Hashable

__hash__

Iterable

__iter__

Iterator

Iterable

__next__

__iter__

Reversible

Iterable

__reversed__

Generator

Iterator

send, throw

close, __iter__, __next__

Sized

__len__

Callable

__call__

Collection

Sized, Iterable, Container

__contains__, __iter__, __len__

Sequence

Reversible, Collection

__getitem__, __len__

__contains__, __iter__, __reversed__, index, and count

MutableSequence

Sequence

__getitem__, __setitem__, __delitem__, __len__, insert

继承自 Sequence 的方法，以及 append, reverse, extend, pop, remove，和 __iadd__

ByteString

Sequence

__getitem__, __len__

继承自 Sequence 的方法

Set

Collection

__contains__, __iter__, __len__

__le__, __lt__, __eq__, __ne__, __gt__, __ge__, __and__, __or__, __sub__, __xor__, and isdisjoint

MutableSet

Set

__contains__, __iter__, __len__, add, discard

继承自 Set 的方法以及 clear, pop, remove, __ior__, __iand__, __ixor__，和 __isub__

Mapping

Collection

__getitem__, __iter__, __len__

__contains__, keys, items, values, get, __eq__, and __ne__

MutableMapping

Mapping

__getitem__, __setitem__, __delitem__, __iter__, __len__

继承自 Mapping 的方法以及 pop, popitem, clear, update，和 setdefault

MappingView

Sized

__len__

ItemsView

MappingView, Set

__contains__, __iter__

KeysView

MappingView, Set

__contains__, __iter__

ValuesView

MappingView

__contains__, __iter__

Awaitable

__await__

Coroutine

Awaitable

send, throw

close

AsyncIterable

__aiter__

AsyncIterator

AsyncIterable

__anext__

__aiter__

AsyncGenerator

AsyncIterator

asend, athrow

aclose, __aiter__, __anext__

class collections.abc.Container

class collections.abc.Hashable

class collections.abc.Sized

class collections.abc.Callable

分别提供了 __contains__(), __hash__(), __len__() 和 __call__() 方法的抽象基类。

class collections.abc.Iterable

提供了 __iter__() 方法的抽象基类。

使用 isinstance(obj, Iterable) 可以检测一个类是否已经注册到了 Iterable 或者实现了 __iter__() 函数，但是无法检测这个类是否能够使用 __getitem__() 方法进行迭代。检测一个对象是否是 iterable 的唯一可信赖的方法是调用 iter(obj)。

class collections.abc.Collection

集合了 Sized 和 Iterable 类的抽象基类。

3.6 新版功能.

class collections.abc.Iterator

提供了 __iter__() 和 __next__() 方法的抽象基类。参见 iterator 的定义。

class collections.abc.Reversible

为可迭代类提供了 __reversed__() 方法的抽象基类。

3.6 新版功能.

class collections.abc.Generator

生成器类，实现了 PEP 342 [https://www.python.org/dev/peps/pep-0342] 中定义的协议，继承并扩展了迭代器，提供了 send(), throw() 和 close() 方法。参见 generator 的定义。

3.5 新版功能.

class collections.abc.Sequence

class collections.abc.MutableSequence

class collections.abc.ByteString

只读且可变的序列 sequences 的抽象基类。

实现笔记：一些混入（Maxin）方法比如 __iter__(), __reversed__() 和 index() 会重复调用底层的 __getitem__() 方法。因此，如果实现的 __getitem__() 是常数级访问速度，那么相应的混入方法会有一个线性的表现；然而，如果底层方法是线性实现（例如链表），那么混入方法将会是平方级的表现，这也许就需要被重构了。

在 3.5 版更改: index() 方法支持 stop 和 start 参数。

class collections.abc.Set

class collections.abc.MutableSet

只读且可变的集合的抽象基类。

class collections.abc.Mapping

class collections.abc.MutableMapping

只读且可变的映射 mappings 的抽象基类。

class collections.abc.MappingView

class collections.abc.ItemsView

class collections.abc.KeysView

class collections.abc.ValuesView

映射及其键和值的视图 views 的抽象基类。

class collections.abc.Awaitable

为可等待对象 awaitable 提供的类，可以被用于 await 表达式中。习惯上必须实现 __await__() 方法。

协程对象 Coroutine 和 Coroutine 抽象基类的实例都是这个抽象基类的实例。

注解

在 CPython 里，基于生成器的协程（使用 types.coroutine() 或 asyncio.coroutine() 包装的生成器）都是 可等待对象，即使他们不含有 __await__() 方法。使用 isinstance(gencoro, Awaitable) 来检测他们会返回 False。要使用 inspect.isawaitable() 来检测他们。

3.5 新版功能.

class collections.abc.Coroutine

用于协程兼容类的抽象基类。实现了如下定义在 协程对象: 里的方法： send()，throw() 和 close()。通常的实现里还需要实现 __await__() 方法。所有的 Coroutine 实例都必须是 Awaitable 实例。参见 coroutine 的定义。

注解

在 CPython 里，基于生成器的协程（使用 types.coroutine() 或 asyncio.coroutine() 包装的生成器）都是 可等待对象，即使他们不含有 __await__() 方法。使用 isinstance(gencoro, Coroutine) 来检测他们会返回 False。要使用 inspect.isawaitable() 来检测他们。

3.5 新版功能.

class collections.abc.AsyncIterable

提供了 __aiter__ 方法的抽象基类。参见 asynchronous iterable 的定义。

3.5 新版功能.

class collections.abc.AsyncIterator

提供了 __aiter__ 和 __anext__ 方法的抽象基类。参见 asynchronous iterator 的定义。

3.5 新版功能.

class collections.abc.AsyncGenerator

为异步生成器类提供的抽象基类，这些类实现了定义在 PEP 525 [https://www.python.org/dev/peps/pep-0525] 和 PEP 492 [https://www.python.org/dev/peps/pep-0492] 里的协议。

3.6 新版功能.

这些抽象基类让我们可以确定类和示例拥有某些特定的函数，例如：

size = None
if isinstance(myvar, collections.abc.Sized):
    size = len(myvar)

有些抽象基类也可以用作混入类（mixin），这可以更容易地开发支持容器 API 的类。例如，要写一个支持完整 Set API 的类，只需要提供下面这三个方法： __contains__(), __iter__() 和 __len__()。抽象基类会补充上其余的方法，比如 __and__() 和 isdisjoint():

class ListBasedSet(collections.abc.Set):
    ''' Alternate set implementation favoring space over speed
        and not requiring the set elements to be hashable. '''
    def __init__(self, iterable):
        self.elements = lst = []
        for value in iterable:
            if value not in lst:
                lst.append(value)

    def __iter__(self):
        return iter(self.elements)

    def __contains__(self, value):
        return value in self.elements

    def __len__(self):
        return len(self.elements)

s1 = ListBasedSet('abcdef')
s2 = ListBasedSet('defghi')
overlap = s1 & s2            # The __and__() method is supported automatically

当把 Set 和 MutableSet 用作混入类时需注意：

由于某些集合操作会创建新集合，默认的混入方法需要一种从可迭代对象里创建新实例的方法。假如其类构造函数签名形如 ClassName(iterable) ，则其会调用一个内部的类方法 _from_iterable()，其中调用了 cls(iterable) 来生成一个新集合。如果这个 Set 混入类在类中被使用，但其构造函数的签名却是不同的形式，那么你就需要重载 _from_iterable() 方法，将其编写成一个类方法，并且它能够从可迭代对象参数中构造一个新实例。

重载比较符时时（想必是为了速度，因为其语义都是固定的），只需要重定义 __le__() 和 __ge__() 函数，然后其他的操作会自动跟进。

混入集合类 Set 提供了一个 _hash() 方法为集合计算哈希值，然而， __hash__() 函数却没有被定义，因为并不是所有集合都是可哈希并且不可变的。为了使用混入类为集合添加哈希能力，可以同时继承 Set() 和 Hashable() 类，然后定义 __hash__ = Set._hash。

参见

OrderedSet recipe [https://code.activestate.com/recipes/576694/] 是基于 MutableSet 构建的一个示例。

对于抽象基类，参见 abc 模块和 PEP 3119 [https://www.python.org/dev/peps/pep-3119]。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

8.5. heapq — 堆队列算法

源码：Lib/heapq.py [https://github.com/python/cpython/tree/3.6/Lib/heapq.py]

这个模块提供了堆队列算法的实现，也称为优先队列算法。

堆是一个二叉树，它的每个父节点的值都只会小于或等于所有孩子节点（的值）。 它使用了数组来实现：从零开始计数，对于所有的 k ，都有 heap[k] <= heap[2*k+1] 和 heap[k] <= heap[2*k+2]。 为了便于比较，不存在的元素被认为是无限大。 堆最有趣的特性在于最小的元素总是在根结点：heap[0]。

这个API与教材中堆算法的实现不太一样，在于两方面：（a）我们使用了基于零开始的索引。这使得节点和其孩子节点之间的索引关系不太直观，但是由于Python使用了从零开始的索引，所以这样做更加合适。（b）我们的 pop 方法返回了最小的元素，而不是最大的（这在教材中叫做 “最小堆”；而“最大堆”在课本中更加常见，因为它更加适用于原地排序）。

基于这两方面，把堆看作原生的Python list也没什么奇怪的： heap[0] 表示最小的元素，同时 heap.sort() 维护了堆的不变性！

要创建一个堆，可以使用list来初始化为 [] ，或者你可以通过一个函数 heapify() ，来把一个list转换成堆。

定义了以下函数：

heapq.heappush(heap, item)

将 item 的值加入 heap 中，保持堆的不变性。

heapq.heappop(heap)

弹出并返回 heap 的最小的元素，保持堆的不变性。如果堆为空，抛出 IndexError 。使用 heap[0] ，可以只访问最小的元素而不弹出它。

heapq.heappushpop(heap, item)

将 item 放入堆中，然后弹出并返回 heap 的最小元素。该组合操作比先调用 heappush() 再调用 heappop() 运行起来更有效率。

heapq.heapify(x)

将list x 转换成堆，原地，线性时间内。

heapq.heapreplace(heap, item)

弹出并返回 heap 中最小的一项，同时推入新的 item。 堆的大小不变。 如果堆为空则引发 IndexError。

这个单步骤操作比 heappop() 加 heappush() 更高效，并且在使用固定大小的堆时更为适宜。 pop/push 组合总是会从堆中返回一个元素并将其替换为 item。

返回的值可能会比添加的 item 更大。 如果不希望如此，可考虑改用 heappushpop()。 它的 push/pop 组合会返回两个值中较小的一个，将较大的值留在堆中。

该模块还提供了三个基于堆的通用功能函数。

heapq.merge(*iterables, key=None, reverse=False)

将多个已排序的输入合并为一个已排序的输出（例如，合并来自多个日志文件的带时间戳的条目）。 返回已排序值的 iterator。

类似于 sorted(itertools.chain(*iterables)) 但返回一个可迭代对象，不会一次性地将数据全部放入内存，并假定每个输入流都是已排序的（从小到大）。

具有两个可选参数，它们都必须指定为关键字参数。

key 指定带有单个参数的 key function，用于从每个输入元素中提取比较键。 默认值为 None (直接比较元素)。

reverse is a boolean value. If set to True, then the input elements are merged as if each comparison were reversed.

在 3.5 版更改: 添加了可选的 key 和 reverse 形参。

heapq.nlargest(n, iterable, key=None)

从 iterable 所定义的数据集中返回前 n 个最大的元素。 如果提供了 key 则其应指定一个单参数的函数，用于从 that is used to extract a comparison key from each element in iterable 的每个元素中提取比较键 (例如 key=str.lower)。 等价于: sorted(iterable, key=key, reverse=True)[:n]。

heapq.nsmallest(n, iterable, key=None)

从 iterable 所定义的数据集中返回前 n 个最小元素组成的列表。 如果提供了 key 则其应指定一个单参数的函数，用于从 iterable 的每个元素中提取比较键 (例如 key=str.lower)。 等价于: sorted(iterable, key=key)[:n]。

后两个函数在 n 值较小时性能最好。 对于更大的值，使用 sorted() 函数会更有效率。 此外，当 n==1 时，使用内置的 min() 和 max() 函数会更有效率。 如果需要重复使用这些函数，请考虑将可迭代对象转为真正的堆。

8.5.1. 基本示例

堆排序 [https://en.wikipedia.org/wiki/Heapsort] 可以通过将所有值推入堆中然后每次弹出一个最小值项来实现。

>>> def heapsort(iterable):
...     h = []
...     for value in iterable:
...         heappush(h, value)
...     return [heappop(h) for i in range(len(h))]
...
>>> heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

这类似于 sorted(iterable)，但与 sorted() 不同的是这个实现是不稳定的。

堆元素可以为元组。 这适用于将比较值（例如任务优先级）与跟踪的主记录进行赋值的场合:

>>> h = []
>>> heappush(h, (5, 'write code'))
>>> heappush(h, (7, 'release product'))
>>> heappush(h, (1, 'write spec'))
>>> heappush(h, (3, 'create tests'))
>>> heappop(h)
(1, 'write spec')

8.5.2. 优先队列实现说明

优先队列 [https://en.wikipedia.org/wiki/Priority_queue] 是堆的常用场合，并且它的实现包含了多个挑战：

排序稳定性：你该如何令相同优先级的两个任务按它们最初被加入时的顺序返回？

如果优先级相同且任务没有默认比较顺序，则 (priority, task) 对的元组比较将会中断。

如果任务优先级发生改变，你该如何将其移至堆中的新位置？

或者如果一个挂起的任务需要被删除，你该如何找到它并将其移出队列？

针对前两项挑战的一种解决方案是将条目保存为包含优先级、条目计数和任务对象 3 个元素的列表。 条目计数可用来打破平局，这样具有相同优先级的任务将按它们的添加顺序返回。 并且由于没有哪两个条目计数是相同的，元组比较将永远不会直接比较两个任务。

其余的挑战主要包括找到挂起的任务并修改其优先级或将其完全移除。 找到一个任务可使用一个指向队列中条目的字典来实现。

移除条目或改变其优先级的操作实现起来更为困难，因为它会破坏堆结构不变量。 因此，一种可能的解决方案是将条目标记为已移除，再添加一个改变了优先级的新条目:

pq = []                         # list of entries arranged in a heap
entry_finder = {}               # mapping of tasks to entries
REMOVED = '<removed-task>'      # placeholder for a removed task
counter = itertools.count()     # unique sequence count

def add_task(task, priority=0):
    'Add a new task or update the priority of an existing task'
    if task in entry_finder:
        remove_task(task)
    count = next(counter)
    entry = [priority, count, task]
    entry_finder[task] = entry
    heappush(pq, entry)

def remove_task(task):
    'Mark an existing task as REMOVED.  Raise KeyError if not found.'
    entry = entry_finder.pop(task)
    entry[-1] = REMOVED

def pop_task():
    'Remove and return the lowest priority task. Raise KeyError if empty.'
    while pq:
        priority, count, task = heappop(pq)
        if task is not REMOVED:
            del entry_finder[task]
            return task
    raise KeyError('pop from an empty priority queue')

8.5.3. 理论

堆是通过数组来实现的，其中的元素从 0 开始计数，对于所有的 k 都有 a[k] <= a[2*k+1] 且 a[k] <= a[2*k+2]。 为了便于比较，不存在的元素被视为无穷大。 堆最有趣的特性在于 a[0] 总是其中最小的元素。

上面的特殊不变量是用来作为一场锦标赛的高效内存表示。 下面的数字是 k 而不是 a[k]:

                               0

              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 26   27 28   29 30

在上面的树中，每个 k 单元都位于 2*k+1 和 2*k+2 之上。 体育运动中我们经常见到二元锦标赛模式，每个胜者单元都位于另两个单元之上，并且我们可以沿着树形图向下追溯胜者所遇到的所有对手。 但是，在许多采用这种锦标赛模式的计算机应用程序中，我们并不需要追溯胜者的历史。 为了获得更高的内存利用效率，当一个胜者晋级时，我们会用较低层级的另一条目来替代它，因此规则变为一个单元和它之下的两个单元包含三个不同条目，上方单元“胜过”了两个下方单元。

如果此堆的不变量始终受到保护，则序号 0 显然是最后的赢家。 删除它并找出“下一个”赢家的最简单算法方式是家某个输家（让我们假定是上图中的 30 号单元）移至 0 号位置，然后将这个新的 0 号沿树下行，不断进行值的交换，直到不变量重新建立。 这显然会是树中条目总数的对数。 通过迭代所有条目，你将得到一个 O(n log n) 复杂度的排序。

此排序有一个很好的特性就是你可以在排序进行期间高效地插入新条目，前提是插入的条目不比你最近取出的 0 号元素“更好”。 这在模拟上下文时特别有用，在这种情况下树保存的是所有传入事件，“胜出”条件是最小调度时间。 当一个事件将其他事件排入执行计划时，它们的调试时间向未来方向延长，这样它们可方便地入堆。 因此，堆结构很适宜用来实现调度器，我的 MIDI 音序器就是用的这个 :-)。

用于实现调度器的各种结构都得到了充分的研究，堆是非常适宜的一种，因为它们的速度相当快，并且几乎是恒定的，最坏的情况与平均情况没有太大差别。 虽然还存在其他总体而言更高效的实现方式，但其最坏的情况却可能非常糟糕。

堆在大磁盘排序中也非常有用。 你应该已经了解大规模排序会有多个“运行轮次”（即预排序的序列，其大小通常与 CPU 内存容量相关），随后这些轮次会进入合并通道，轮次合并的组织往往非常巧妙 1。 非常重要的一点是初始排序应产生尽可能长的运行轮次。 锦标赛模式是达成此目标的好办法。 如果你使用全部有用内存来进行锦标赛，替换和安排恰好适合当前运行轮次的条目，你将可以对于随机输入生成两倍于内存大小的运行轮次，对于模糊排序的输入还会有更好的效果。

另外，如果你输出磁盘上的第 0 个条目并获得一个可能不适合当前锦标赛的输入（因为其值要“胜过”上一个输出值），它无法被放入堆中，因此堆的尺寸将缩小。 被释放的内存可以被巧妙地立即重用以逐步构建第二个堆，其增长速度与第一个堆的缩减速度正好相同。 当第一个堆完全消失时，你可以切换新堆并启动新的运行轮次。 这样做既聪明又高效！

总之，堆是值得了解的有用内存结构。 我在一些应用中用到了它们，并且认为保留一个 ‘heap’ 模块是很有意义的。 :-)

备注

1

当前时代的磁盘平衡算法与其说是巧妙，不如说是麻烦，这是由磁盘的寻址能力导致的结果。 在无法寻址的设备例如大型磁带机上，情况则相当不同，开发者必须非常聪明地（极为提前地）确保每次磁带转动都尽可能地高效（就是说能够最好地加入到合并“进程”中）。 有些磁带甚至能够反向读取，这也被用来避免倒带的耗时。 请相信我，真正优秀的磁带机排序看起来是极其壮观的，排序一向都是一门伟大的艺术！ :-)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

8.6. bisect — 数组二分查找算法

源代码： Lib/bisect.py [https://github.com/python/cpython/tree/3.6/Lib/bisect.py]

这个模块对有序列表提供了支持，使得他们可以在插入新数据仍然保持有序。对于长列表，如果其包含元素的比较操作十分昂贵的话，这可以是对更常见方法的改进。这个模块叫做 bisect 因为其使用了基本的二分（bisection）算法。源代码也可以作为很棒的算法示例（边界判断也做好啦！）

定义了以下函数：

bisect.bisect_left(a, x, lo=0, hi=len(a))

在 a 中找到 x 合适的插入点以维持有序。参数 lo 和 hi 可以被用于确定需要考虑的子集；默认情况下整个列表都会被使用。如果 x 已经在 a 里存在，那么插入点会在已存在元素之前（也就是左边）。如果 a 是列表（list）的话，返回值是可以被放在 list.insert() 的第一个参数的。

返回的插入点 i 可以将数组 a 分成两部分。左侧是 all(val < x for val in a[lo:i]) ，右侧是 all(val >= x for val in a[i:hi]) 。

bisect.bisect_right(a, x, lo=0, hi=len(a))

bisect.bisect(a, x, lo=0, hi=len(a))

类似于 bisect_left()，但是返回的插入点是 a 中已存在元素 x 的右侧。

返回的插入点 i 可以将数组 a 分成两部分。左侧是 all(val <= x for val in a[lo:i])，右侧是 all(val > x for val in a[i:hi]) for the right side。

bisect.insort_left(a, x, lo=0, hi=len(a))

将 x 插入到一个有序序列 a 里，并维持其有序。如果 a 有序的话，这相当于 a.insert(bisect.bisect_left(a, x, lo, hi), x)。要注意搜索是 O(log n) 的，插入却是 O(n) 的。

bisect.insort_right(a, x, lo=0, hi=len(a))

bisect.insort(a, x, lo=0, hi=len(a))

类似于 insort_left()，但是把 x 插入到 a 中已存在元素 x 的右侧。

参见

SortedCollection recipe [https://code.activestate.com/recipes/577197-sortedcollection/] 使用 bisect 构造了一个功能完整的集合类，提供了直接的搜索方法和对用于搜索的 key 方法的支持。所有用于搜索的键都是预先计算的，以避免在搜索时对 key 方法的不必要调用。

8.6.1. 搜索有序列表

上面的 bisect() 函数对于找到插入点是有用的，但在一般的搜索任务中可能会有点尴尬。下面 5 个函数展示了如何将其转变成有序列表中的标准查找函数

def index(a, x):
    'Locate the leftmost value exactly equal to x'
    i = bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    raise ValueError

def find_lt(a, x):
    'Find rightmost value less than x'
    i = bisect_left(a, x)
    if i:
        return a[i-1]
    raise ValueError

def find_le(a, x):
    'Find rightmost value less than or equal to x'
    i = bisect_right(a, x)
    if i:
        return a[i-1]
    raise ValueError

def find_gt(a, x):
    'Find leftmost value greater than x'
    i = bisect_right(a, x)
    if i != len(a):
        return a[i]
    raise ValueError

def find_ge(a, x):
    'Find leftmost item greater than or equal to x'
    i = bisect_left(a, x)
    if i != len(a):
        return a[i]
    raise ValueError

8.6.2. 其他示例

函数 bisect() 还可以用于数字表查询。这个例子是使用 bisect() 从一个给定的考试成绩集合里，通过一个有序数字表，查出其对应的字母等级：90 分及以上是 ‘A’，80 到 89 是 ‘B’，以此类推

>>> def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
...     i = bisect(breakpoints, score)
...     return grades[i]
...
>>> [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
['F', 'A', 'C', 'C', 'B', 'A', 'A']

与 sorted() 函数不同，对于 bisect() 函数来说，key 或者 reversed 参数并没有什么意义。因为这会导致设计效率低下（连续调用 bisect 函数时，是不会 “记住” 过去查找过的键的）。

正相反，最好去搜索预先计算好的键列表，来查找相关记录的索引。

>>> data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)]
>>> data.sort(key=lambda r: r[1])
>>> keys = [r[1] for r in data]         # precomputed list of keys
>>> data[bisect_left(keys, 0)]
('black', 0)
>>> data[bisect_left(keys, 1)]
('blue', 1)
>>> data[bisect_left(keys, 5)]
('red', 5)
>>> data[bisect_left(keys, 8)]
('yellow', 8)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

8.7. array — 高效的数值数组

此模块定义了一种对象类型，可以紧凑地表示基本类型值的数组：字符、整数、浮点数等。 数组属于序列类型，其行为与列表非常相似，不同之处在于其中存储的对象类型是受限的。 类型在对象创建时使用单个字符的 类型码 来指定。 已定义的类型码如下：

类型码

C 类型

Python 类型

以字节表示的最小尺寸

注释

'b'

signed char

int

1

'B'

unsigned char

int

1

'u'

Py_UNICODE

Unicode 字符

2

(1)

'h'

signed short

int

2

'H'

unsigned short

int

2

'i'

signed int

int

2

'I'

unsigned int

int

2

'l'

signed long

int

4

'L'

unsigned long

int

4

'q'

signed long long

int

8

(2)

'Q'

unsigned long long

int

8

(2)

'f'

float

float

4

'd'

double

float

8

注释:

'u' 类型码对应于 Python 中已过时的 unicode 字符 (Py_UNICODE 即 wchar_t)。 根据系统平台的不同，它可能是 16 位或 32 位。

'u' 将与其它的 Py_UNICODE API 一起被移除。

Deprecated since version 3.3, will be removed in version 4.0.

The 'q' and 'Q' type codes are available only if the platform C compiler used to build Python supports C long long, or, on Windows, __int64.

3.3 新版功能.

值的实际表示会由机器的架构决定（严格地说是由 C 实现决定）。 实际大小可通过 itemsize 属性来获取。

这个模块定义了以下类型：

class array.array(typecode[, initializer])

一个包含由 typecode 限制类型的条目的新数组，并由可选的 initializer 值进行初始化，该值必须为一个列表、bytes-like object 或包含正确类型元素的可迭代对象。

如果给定一个列表或字符串，该 initializer 会被传给新数组的 fromlist(), frombytes() 或 fromunicode() 方法（见下文）以将初始条目添加到数组中。 否则会将可迭代对象作为 initializer 传给 extend() 方法。

array.typecodes

一个包含所有可用类型码的字符串。

数组对象支持普通的序列操作如索引、切片、拼接和重复等。 当使用切片赋值时，所赋的值必须为具有相同类型码的数组对象；所有其他情况都将引发 TypeError。 数组对象也实现了缓冲区接口，可以用于所有支持 字节类对象 的场合。

以下数据项和方法也受到支持：

array.typecode

用于创建数组的类型码字符。

array.itemsize

在内部表示中一个数组项的字节长度。

array.append(x)

添加一个值为 x 的新项到数组末尾。

array.buffer_info()

返回一个元组 (address, length) 以给出用于存放数组内容的缓冲区元素的当前内存地址和长度。 以字节表示的内存缓冲区大小可通过 array.buffer_info()[1] * array.itemsize 来计算。 这在使用需要内存地址的低层级（因此不够安全） I/O 接口时会很有用，例如某些 ioctl() 操作。 只要数组存在并且没有应用改变长度的操作，返回数值就是有效的。

注解

当在 C 或 C++ 编写的代码中使用数组对象时（这是有效使用此类信息的唯一方式），使用数组对象所支持的缓冲区接口更为适宜。 此方法仅保留用作向下兼容，应避免在新代码中使用。 缓冲区接口的文档参见 缓冲协议。

array.byteswap()

“字节对调”所有数组项。 此方法只支持大小为 1, 2, 4 或 8 字节的值；对于其他值类型将引发 RuntimeError。 它适用于从不同字节序机器所生成的文件中读取数据的情况。

array.count(x)

返回 x 在数组中的出现次数。

array.extend(iterable)

将来自 iterable 的项添加到数组末尾。 如果 iterable 是另一个数组，它必须具有 完全 相同的类型码；否则将引发 TypeError。 如果 iterable 不是一个数组，则它必须为可迭代对象并且其元素必须为可添加到数组的适当类型。

array.frombytes(s)

添加来自字符串的项，将字符串解读为机器值的数组（相当于使用 fromfile() 方法从文件中读取数据）。

3.2 新版功能: fromstring() 重命名为 frombytes() 以使其含义更清晰。

array.fromfile(f, n)

从 file object f 中读取 n 项（解读为机器值）并将它们添加到数组末尾。 如果可读取数据少于 n 项则将引发 EOFError，但有效的项仍然会被插入数组。 f 必须为一个真实的内置文件对象；不支持带有 read() 方法的其它对象。

array.fromlist(list)

添加来自 list 的项。 这等价于 for x in list: a.append(x)，区别在于如果发生类型错误，数组将不会被改变。

array.fromstring()

已弃用的 frombytes() 的别名。

array.fromunicode(s)

使用来自给定 Unicode 字符串的数组扩展数组。 数组必须是类型为 'u' 的数组；否则将引发 ValueError。 请使用 array.frombytes(unicodestring.encode(enc)) 来将 Unicode 数据添加到其他类型的数组。

array.index(x)

返回最小的 i 使得 i 为 x 在数组中首次出现的序号。

array.insert(i, x)

将值 x 作为新项插入数组的 i 位置之前。 负值将被视为相对于数组末尾的位置。

array.pop([i])

从数组中移除序号为 i 的项并将其返回。 可选参数值默认为 -1，因此默认将移除并返回末尾项。

array.remove(x)

从数组中移除首次出现的 x。

array.reverse()

反转数组中各项的顺序。

array.tobytes()

将数组转换为一个机器值数组并返回其字节表示（即相当与通过 tofile() 方法写入到文件的字节序列。）

3.2 新版功能: tostring() 被重命名为 tobytes() 以使其含义更清晰。

array.tofile(f)

将所有项（作为机器值）写入到 file object f。

array.tolist()

将数组转换为包含相同项的普通列表。

array.tostring()

tobytes() 的已弃用别名。

array.tounicode()

将数组转换为一个 Unicode 字符串。 数组必须是类型为 'u' 的数组；否则将引发 ValueError。 请使用 array.tobytes().decode(enc) 来从其他类型的数组生成 Unicode 字符串。

当一个数组对象被打印或转换为字符串时，它会表示为 array(typecode, initializer)。 如果数组为空则 initializer 会被省略，否则如果 typecode 为 'u' 则它是一个字符串，否则它是一个数字列表。 使用 eval() 保证能将字符串转换回具有相同类型和值的数组，只要 array 类已通过 from array import array 被引入。 例如:

array('l')
array('u', 'hello \u2641')
array('l', [1, 2, 3, 4, 5])
array('d', [1.0, 2.0, 3.14])

参见

模块 struct

打包和解包异构二进制数据。

模块 xdrlib

打包和解包用于某些远程过程调用系统的 External Data Representation (XDR) 数据。

The Numerical Python Documentation [https://docs.scipy.org/doc/]

Numeric Python 扩展 (NumPy) 定义了另一种数组类型；请访问 http://www.numpy.org/ 了解有关 Numerical Python 的更多信息。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

8.8. weakref — 弱引用

源码： Lib/weakref.py [https://github.com/python/cpython/tree/3.6/Lib/weakref.py]

weakref 模块允许Python程序员创建对象的 weak references 。

在下文中，术语 referent 表示由弱引用引用的对象。

对对象的弱引用不能保证对象存活：当对像的引用只剩弱引用时， garbage collection 可以销毁引用并将其内存重用于其他内容。但是，在实际销毁对象之前，即使没有强引用，弱引用也一直能返回该对象。

弱引用的主要用途是实现保存大对象的高速缓存或映射，但又不希望大对象仅仅因为它出现在高速缓存或映射中而保持存活。

例如，如果您有许多大型二进制图像对象，则可能希望将名称与每个对象关联起来。如果您使用Python字典将名称映射到图像，或将图像映射到名称，则图像对象将保持活动状态，因为它们在字典中显示为值或键。 weakref 模块提供的 WeakKeyDictionary 和 WeakValueDictionary 类可以替代Python字典，使用弱引用来构造映射，这些映射不会仅仅因为它们出现在映射对象中而使对象保持存活。例如，如果一个图像对象是 WeakValueDictionary 中的值，那么当对该图像对象的剩余引用是弱映射对象所持有的弱引用时，垃圾回收可以回收该对象并将其在弱映射对象中相应的条目删除。

WeakKeyDictionary 和 WeakValueDictionary 在它们的实现中使用弱引用，在弱引用上设置回调函数，当键或值被垃圾回收回收时通知弱字典。 WeakSet 实现了 set 接口，但像 WeakKeyDictionary 一样，只持有其元素的弱引用。

finalize 提供了注册一个对象被垃圾收集时要调用的清理函数的方式。这比在原始弱引用上设置回调函数更简单，因为模块会自动确保对象被回收前终结器一直保持存活。

这些弱容器类型之一或者 finalize 就是大多数程序所需要的 - 通常不需要直接创建自己的弱引用。weakref 模块暴露了低级机制，以便于高级用途。

Not all objects can be weakly referenced; those objects which can include class instances, functions written in Python (but not in C), instance methods, sets, frozensets, some file objects, generators, type objects, sockets, arrays, deques, regular expression pattern objects, and code objects.

在 3.2 版更改: 添加了对thread.lock，threading.Lock和代码对象的支持。

几个内建类型如 list 和 dict 不直接支持弱引用，但可以通过子类化添加支持:

class Dict(dict):
    pass

obj = Dict(red=1, green=2, blue=3)   # this object is weak referenceable

Other built-in types such as tuple and int do not support weak references even when subclassed (This is an implementation detail and may be different across various Python implementations.).

Extension types can easily be made to support weak references; see Weak Reference Support.

class weakref.ref(object[, callback])

返回对 对象 的弱引用。如果原始对象仍然存活，则可以通过调用引用对象来检索原始对象；如果引用的原始对象不再存在，则调用引用对象将得到 None 。如果提供了 回调 而且值不是 None ，并且返回的弱引用对象仍然存活，则在对象即将终结时将调用回调;弱引用对象将作为回调的唯一参数传递；指示物将不再可用。

许多弱引用也允许针对相同对象来构建。 为每个弱引用注册的回调将按从最近注册的回调到最早注册的回调的顺序被调用。

回调所引发的异常将记录于标准错误输出，但无法被传播；它们会按与对象的 __del__() 方法所引发的异常相同的方式被处理。

如果 object 可哈希，则弱引用也为 hashable。 即使在 object 被删除之后它们仍将保持其哈希值。 如果 hash() 在 object 被删除之后才首次被调用，则该调用将引发 TypeError。

弱引用支持相等检测，但不支持排序比较。 如果被引用对象仍然存在，两个引用具有与它们的被引用对象一致的相等关系（无论 callback 是否相同）。 如果删除了任一被引用对象，则仅在两个引用对象为同一对象时两者才相等。

这是一个可子类化的类型而非一个工厂函数。

__callback__

这个只读属性会返回当前关联到弱引用的回调。 如果回调不存在或弱引用的被引用对象已不存在，则此属性的值为 None。

在 3.4 版更改: 添加了 __callback__ 属性。

weakref.proxy(object[, callback])

返回 object 的一个使用弱引用的代理。 此函数支持在大多数上下文中使用代理，而不要求显式地对所使用的弱引用对象解除引用。 返回的对象类型将为 ProxyType 或 CallableProxyType，具体取决于 object 是否可调用。 Proxy 对象不是 hashable 对象，无论被引用对象是否可哈希；这可避免与它们的基本可变性质相关的多种问题，并可防止它们被用作字典键。 callback 与 ref() 函数的同名形参含义相同。

weakref.getweakrefcount(object)

返回指向 object 的弱引用和代理的数量。

weakref.getweakrefs(object)

返回由指向 object 的所有弱引用和代理构成的列表。

class weakref.WeakKeyDictionary([dict])

弱引用键的映射类。 当不再有对键的强引用时字典中的条目将被丢弃。 这可被用来将额外数据关联到一个应用中其他部分所拥有的对象而无需在那些对象中添加属性。 这对于重载了属性访问的对象来说特别有用。

注解

Caution: Because a WeakKeyDictionary is built on top of a Python dictionary, it must not change size when iterating over it. This can be difficult to ensure for a WeakKeyDictionary because actions performed by the program during iteration may cause items in the dictionary to vanish “by magic” (as a side effect of garbage collection).

WeakKeyDictionary 对象具有一个额外方法可以直接公开内部引用。 这些引用不保证在它们被使用时仍然保持“存活”，因此这些引用的调用结果需要在使用前进行检测。 此方法可用于避免创建会导致垃圾回收器将保留键超出实际需要时长的引用。

WeakKeyDictionary.keyrefs()

返回包含对键的弱引用的可迭代对象。

class weakref.WeakValueDictionary([dict])

弱引用值的映射类。 当不再有对键的强引用时字典中的条目将被丢弃。

注解

Caution: Because a WeakValueDictionary is built on top of a Python dictionary, it must not change size when iterating over it. This can be difficult to ensure for a WeakValueDictionary because actions performed by the program during iteration may cause items in the dictionary to vanish “by magic” (as a side effect of garbage collection).

WeakValueDictionary 对象具有一个额外方法，此方法存在与 WeakKeyDictionary 对象的 keyrefs() 方法相同的问题。

WeakValueDictionary.valuerefs()

返回包含对值的弱引用的可迭代对象。

class weakref.WeakSet([elements])

保持对其元素弱引用的集合类。 当不再有对某个元素的强引用时元素将被丢弃。

class weakref.WeakMethod(method)

一个模拟对绑定方法（即在类中定义并在实例中查找的方法）进行弱引用的自定义 ref 子类。 由于绑定方法是临时性的，标准弱引用无法保持它。 WeakMethod 包含特别代码用来重新创建绑定方法，直到对象或初始函数被销毁:

>>> class C:
...     def method(self):
...         print("method called!")
...
>>> c = C()
>>> r = weakref.ref(c.method)
>>> r()
>>> r = weakref.WeakMethod(c.method)
>>> r()
<bound method C.method of <__main__.C object at 0x7fc859830220>>
>>> r()()
method called!
>>> del c
>>> gc.collect()
0
>>> r()
>>>

3.4 新版功能.

class weakref.finalize(obj, func, *args, **kwargs)

返回一个可调用的终结器对象，该对象将在 obj 作为垃圾回收时被调用。 与普通的弱引用不同，终结器将总是存活，直到引用对象被回收，这极大地简化了生存期管理。

终结器总是被视为 存活 直到它被调用（显式调用或在垃圾回收时隐式调用），调用之后它将 死亡。 调用存活的终结器将返回 func(*arg, **kwargs) 的求值结果，而调用死亡的终结器将返回 None。

在垃圾收集期间由终结器回调所引发异常将显示于标准错误输出，但无法被传播。 它们会按与对象的 __del__() 方法或弱引用的回调所引发异常相同的方式被处理。

当程序退出时，剩余的存活终结器会被调用，除非它们的 atexit 属性已被设为假值。 它们会按与创建时相反的顺序被调用。

终结器在 interpreter shutdown 的后期绝不会发起调用其回调函数，此时模块全局变量很可能已被替换为 None。

__call__()

如果 self 为存活状态则将其标记为已死亡，并返回调用 func(*args, **kwargs) 的结果。 如果 self 已死亡则返回 None。

detach()

如果 self 为存活状态则将其标记为已死亡，并返回元组 (obj, func, args, kwargs)。 如果 self 已死亡则返 None。

peek()

如果 self 为存活状态则返回元组 (obj, func, args, kwargs)。 如果 self 已死亡则返回 None。

alive

如果终结器为存活状态则该特征属性为真值，否则为假值。

atexit

一个可写的布尔型特征属性，默认为真值。 当程序退出时，它会调用所有 atexit 为真值的剩余存活终结器。 它们会按与创建时相反的顺序被调用。

注解

很重要的一点是确保 func, args 和 kwargs 不拥有任何对 obj 的引用，无论是直接的或是间接的，否则的话 obj 将永远不会被作为垃圾回收。 特别地，func 不应当是 obj 的一个绑定方法。

3.4 新版功能.

weakref.ReferenceType

弱引用对象的类型对象。

weakref.ProxyType

不可调用的对象的代理的类型对象。

weakref.CallableProxyType

可调用对象的代理的类型对象。

weakref.ProxyTypes

包含所有代理的类型对象的序列。 这可以用于更方便地检测一个对象是否是代理，而不必依赖于两种代理对象的名称。

exception weakref.ReferenceError

Exception raised when a proxy object is used but the underlying object has been collected. This is the same as the standard ReferenceError exception.

参见

PEP 205 [https://www.python.org/dev/peps/pep-0205] - 弱引用

此特性的提议和理由，包括早期实现的链接和其他语言中类似特性的相关信息。

8.8.1. 弱引用对象

弱引用对象没有 ref.__callback__ 以外的方法和属性。 一个弱引用对象如果存在，就允许通过调用它来获取引用：

>>> import weakref
>>> class Object:
...     pass
...
>>> o = Object()
>>> r = weakref.ref(o)
>>> o2 = r()
>>> o is o2
True

如果引用已不存在，则调用引用对象将返回 None:

>>> del o, o2
>>> print(r())
None

检测一个弱引用对象是否仍然存在应该使用表达式 ref() is not None。 通常，需要使用引用对象的应用代码应当遵循这样的模式:

# r is a weak reference object
o = r()
if o is None:
    # referent has been garbage collected
    print("Object has been deallocated; can't frobnicate.")
else:
    print("Object is still live!")
    o.do_something_useful()

使用单独的“存活”测试会在多线程应用中制造竞争条件；其他线程可能导致某个弱引用在该弱引用被调用前就失效；上述的写法在多线程应用和单线程应用中都是安全的。

特别版本的 ref 对象可以通过子类化来创建。 在 WeakValueDictionary 的实现中就使用了这种方式来减少映射中每个条目的内存开销。 这对于将附加信息关联到引用的情况最为适用，但也可以被用于在调用中插入额外处理来提取引用。

这个例子演示了如何将 ref 的一个子类用于存储有关对象的附加信息并在引用被访问时影响其所返回的值:

import weakref

class ExtendedRef(weakref.ref):
    def __init__(self, ob, callback=None, **annotations):
        super(ExtendedRef, self).__init__(ob, callback)
        self.__counter = 0
        for k, v in annotations.items():
            setattr(self, k, v)

    def __call__(self):
        """Return a pair containing the referent and the number of
        times the reference has been called.
        """
        ob = super(ExtendedRef, self).__call__()
        if ob is not None:
            self.__counter += 1
            ob = (ob, self.__counter)
        return ob

8.8.2. 示例

这个简单的例子演示了一个应用如何使用对象 ID 来提取之前出现过的对象。 然后对象的 ID 可以在其它数据结构中使用，而无须强制对象保持存活，但处于存活状态的对象也仍然可以通过 ID 来提取。

import weakref

_id2obj_dict = weakref.WeakValueDictionary()

def remember(obj):
    oid = id(obj)
    _id2obj_dict[oid] = obj
    return oid

def id2obj(oid):
    return _id2obj_dict[oid]

8.8.3. 终结器对象

使用 finalize 的主要好处在于它能更简便地注册回调函数，而无须保留所返回的终结器对象。 例如

>>> import weakref
>>> class Object:
...     pass
...
>>> kenny = Object()
>>> weakref.finalize(kenny, print, "You killed Kenny!")
<finalize object at ...; for 'Object' at ...>
>>> del kenny
You killed Kenny!

终结器也可以被直接调用。 但是终结器最多只能对回调函数发起一次调用。

>>> def callback(x, y, z):
...     print("CALLBACK")
...     return x + y + z
...
>>> obj = Object()
>>> f = weakref.finalize(obj, callback, 1, 2, z=3)
>>> assert f.alive
>>> assert f() == 6
CALLBACK
>>> assert not f.alive
>>> f()                     # callback not called because finalizer dead
>>> del obj                 # callback not called because finalizer dead

你可以使用 detach() 方法来注销一个终结器。 该方法将销毁终结器并返回其被创建时传给构造器的参数。

>>> obj = Object()
>>> f = weakref.finalize(obj, callback, 1, 2, z=3)
>>> f.detach()
(<__main__.Object object ...>, <function callback ...>, (1, 2), {'z': 3})
>>> newobj, func, args, kwargs = _
>>> assert not f.alive
>>> assert newobj is obj
>>> assert func(*args, **kwargs) == 6
CALLBACK

除非你将 atexit 属性设为 False，否则终结器在程序退出时如果仍然存活就将被调用。 例如

>>> obj = Object()
>>> weakref.finalize(obj, print, "obj dead or exiting")
<finalize object at ...; for 'Object' at ...>
>>> exit()
obj dead or exiting

8.8.4. 比较终结器与 __del__() 方法

假设我们想创建一个类，用它的实例来代表临时目录。 当以下事件中的某一个发生时，这个目录应当与其内容一起被删除：

对象被作为垃圾回收，

对象的 remove() 方法被调用，或

程序退出。

我们可以尝试使用 __del__() 方法来实现这个类，如下所示:

class TempDir:
    def __init__(self):
        self.name = tempfile.mkdtemp()

    def remove(self):
        if self.name is not None:
            shutil.rmtree(self.name)
            self.name = None

    @property
    def removed(self):
        return self.name is None

    def __del__(self):
        self.remove()

从 Python 3.4 开始，__del__() 方法不会再阻止循环引用被作为垃圾回收，并且模块全局变量在 interpreter shutdown 期间不会被强制设为 None。 因此这段代码在 CPython 上应该会正常运行而不会出现任何问题。

然而，__del__() 方法的处理会严重地受到具体实现的影响，因为它依赖于解释器垃圾回收实现方式的内部细节。

更健壮的替代方式可以是定义一个终结器，只引用它所需要的特定函数和对象，而不是获取对整个对象状态的访问权:

class TempDir:
    def __init__(self):
        self.name = tempfile.mkdtemp()
        self._finalizer = weakref.finalize(self, shutil.rmtree, self.name)

    def remove(self):
        self._finalizer()

    @property
    def removed(self):
        return not self._finalizer.alive

像这样定义后，我们的终结器将只接受一个对其完成正确清理目录任务所需细节的引用。 如果对象一直未被作为垃圾回收，终结器仍会在退出时被调用。

基于弱引用的终结器还具有另一项优势，就是它们可被用来为定义由第三方控制的类注册终结器，例如当一个模块被卸载时运行特定代码:

import weakref, sys
def unloading_module():
    # implicit reference to the module globals from the function body
weakref.finalize(sys.modules[__name__], unloading_module)

注解

如果当程序退出时你恰好在守护线程中创建终结器对象，则有可能该终结器不会在退出时被调用。 但是，在一个守护线程中 atexit.register(), try: ... finally: ... 和 with: ... 同样不能保证执行清理。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

8.9. types — 动态类型创建和内置类型名称

源代码: Lib/types.py [https://github.com/python/cpython/tree/3.6/Lib/types.py]

This module defines utility function to assist in dynamic creation of new types.

它还为某些对象类型定义了名称，这些名称由标准 Python 解释器所使用，但并不像内置的 int 或 str 那样对外公开。

最后，它还额外提供了一些类型相关但重要程度不足以作为内置对象的工具类和函数。

8.9.1. 动态类型创建

types.new_class(name, bases=(), kwds=None, exec_body=None)

使用适当的元类动态地创建一个类对象。

前三个参数是组成类定义头的部件：类名称，基类 (有序排列)，关键字参数 (例如 metaclass)。

exec_body 参数是一个回调函数，用于填充新创建类的命名空间。 它应当接受类命名空间作为其唯一的参数并使用类内容直接更新命名空间。 如果未提供回调函数，则它就等效于传入 lambda ns: ns。

3.3 新版功能.

types.prepare_class(name, bases=(), kwds=None)

计算适当的元类并创建类命名空间。

参数是组成类定义头的部件：类名称，基类 (有序排列) 以及关键字参数 (例如 metaclass)。

返回值是一个 3 元组: metaclass, namespace, kwds

metaclass 是适当的元类，namespace 是预备好的类命名空间而 kwds 是所传入 kwds 参数移除每个 'metaclass' 条目后的已更新副本。 如果未传入 kwds 参数，这将为一个空字典。

3.3 新版功能.

在 3.6 版更改: The default value for the namespace element of the returned tuple has changed. Now an insertion-order-preserving mapping is used when the metaclass does not have a __prepare__ method,

参见

元类

这些函数所支持的类创建过程的完整细节

PEP 3115 [https://www.python.org/dev/peps/pep-3115] - Python 3000 中的元类

引入 __prepare__ 命名空间钩子

8.9.2. 标准解释器类型

此模块为许多类型提供了实现 Python 解释器所要求的名称。 它刻意地避免了包含某些仅在处理过程中偶然出现的类型，例如 listiterator 类型。

此种名称的典型应用如 isinstance() 或 issubclass() 检测。

以下类型有相应的标准名称定义：

types.FunctionType

types.LambdaType

用户自定义函数以及由 lambda 表达式所创建函数的类型。

types.GeneratorType

generator 迭代器对象的类型，由生成器函数创建。

types.CoroutineType

coroutine 对象的类型，由 async def 函数创建。

3.5 新版功能.

types.AsyncGeneratorType

asynchronous generator 迭代器对象的类型，由异步生成器函数创建。

3.6 新版功能.

types.CodeType

代码对象的类型，例如 compile() 的返回值。

types.MethodType

用户自定义类实例方法的类型。

types.BuiltinFunctionType

types.BuiltinMethodType

内置函数例如 len() 或 sys.exit() 以及内置类方法的类型。 （这里所说的“内置”是指“以 C 语言编写”。）

class types.ModuleType(name, doc=None)

模块 的类型。 构造器接受待创建模块的名称及其作为可选项 docstring。

注解

如果你希望设置各种由导入控制的属性，请使用 importlib.util.module_from_spec() 来创建一个新模块。

__doc__

模块的 docstring。 默认为 None。

__loader__

用于加载模块的 loader。 默认为 None。

在 3.4 版更改: 默认为 None。 之前该属性为可选项。

__name__

模块的名字

__package__

一个模块所属的 package。 如果模块为最高层级的（即不是任何特定包的组成部分）则该属性应设为 ''，否则它应设为特定包的名称 (如果模块本身也是一个包则名称可以为 __name__)。 默认为 None。

在 3.4 版更改: 默认为 None。 之前该属性为可选项。

types.TracebackType

回溯对象的类型，例如 sys.exc_info()[2] 中的对象。

types.FrameType

帧对象的类型，例如 tb.tb_frame 中的对象，其中 tb 是一个回溯对象。

types.GetSetDescriptorType

使用 PyGetSetDef 在扩展模块中定义的对象的类型，例如 FrameType.f_locals 或 array.array.typecode。 此类型被用作对象属性的描述器；它的目的与 property 类型相同，但专门针对在扩展模块中定义的类。

types.MemberDescriptorType

使用 PyMemberDef 在扩展模块中定义的对象的类型，例如 datetime.timedelta.days。 此类型被用作使用标准转换函数的简单 C 数据成员的描述器；它的目的与 property 类型相同，但专门针对在扩展模块中定义的类。

CPython implementation detail: 在 Python 的其它实现中，此类型可能与 GetSetDescriptorType 完全相同。

class types.MappingProxyType(mapping)

一个映射的只读代理。 它提供了对映射条目的动态视图，这意味着当映射发生改变时，视图会反映这些改变。

3.3 新版功能.

key in proxy

如果下层的映射中存在键 key 则返回 True，否则返回 False。

proxy[key]

返回下层的映射中以 key 为键的项。 如果下层的映射中不存在键 key 则引发 KeyError。

iter(proxy)

返回由下层映射的键为元素的迭代器。 这是 iter(proxy.keys()) 的快捷方式。

len(proxy)

返回下层映射中的项数。

copy()

返回下层映射的浅拷贝。

get(key[, default])

如果 key 存在于下层映射中则返回 key 的值，否则返回 default。 如果 default 未给出则默认为 None，因而此方法绝不会引发 KeyError。

items()

返回由下层映射项 ((键, 值) 对) 组成的一个新视图。

keys()

返回由下层映射的键组成的一个新视图。

values()

返回由下层映射的值组成的一个新视图。

8.9.3. 附加工具类和函数

class types.SimpleNamespace

一个简单的 object 子类，提供了访问其命名空间的属性，以及一个有意义的 repr。

不同于 object，对于 SimpleNamespace 你可以添加和移除属性。 如果一个 SimpleNamespace 对象使用关键字参数进行初始化，这些参数会被直接加入下层命名空间。

此类型大致等价于以下代码:

class SimpleNamespace:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __repr__(self):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

SimpleNamespace 可被用于替代 class NS: pass。 但是，对于结构化记录类型则应改用 namedtuple()。

3.3 新版功能.

types.DynamicClassAttribute(fget=None, fset=None, fdel=None, doc=None)

在类上访问 __getattr__ 的路由属性。

这是一个描述器，用于定义通过实例与通过类访问时具有不同行为的属性。 当实例访问时保持正常行为，但当类访问属性时将被路由至类的 __getattr__ 方法；这是通过引发 AttributeError 来完成的。

这样就允许有在实例上激活的特征属性，同时又有在类上的同名虚拟属性（一个这样的例子是 Enum）。

3.4 新版功能.

8.9.4. 协程工具函数

types.coroutine(gen_func)

此函数可将 generator 函数转换为返回基于生成器的协程的 coroutine function。 基于生成器的协程仍然属于 generator iterator，但同时又可被视为 coroutine 对象兼 awaitable。 不过，它没有必要实现 __await__() 方法。

如果 gen_func 是一个生成器函数，它将被原地修改。

如果 gen_func 不是一个生成器函数，则它会被包装。 如果它返回一个 collections.abc.Generator 的实例，该实例将被包装在一个 awaitable 代理对象中。 所有其他对象类型将被原样返回。

3.5 新版功能.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

8.10. copy — 浅层 (shallow) 和深层 (deep) 复制操作

源代码: Lib/copy.py [https://github.com/python/cpython/tree/3.6/Lib/copy.py]

Python 中赋值语句不复制对象，而是在目标和对象之间创建绑定 (bindings) 关系。对于自身可变或者包含可变项的集合对象，开发者有时会需要生成其副本用于改变操作，进而避免改变原对象。本模块提供了通用的浅层复制和深层复制操作（如下所述）。

接口摘要：

copy.copy(x)

返回 x 的浅层复制。

copy.deepcopy(x[, memo])

返回 x 的深层复制。

exception copy.error

针对模块特定错误引发。

浅层复制和深层复制之间的区别仅与复合对象 (即包含其他对象的对象，如列表或类的实例) 相关:

一个 浅层复制 会构造一个新的复合对象，然后（在可能的范围内）将原对象中找到的 引用 插入其中。

一个 深层复制 会构造一个新的复合对象，然后递归地将原始对象中所找到的对象的 副本 插入。

深度复制操作通常存在两个问题, 而浅层复制操作并不存在这些问题：

递归对象 (直接或间接包含对自身引用的复合对象) 可能会导致递归循环。

由于深层复制会复制所有内容，因此可能会过多复制（例如本应该在副本之间共享的数据）。

The deepcopy() function avoids these problems by:

保留在当前复制过程中已复制的对象的 “备忘录” （memo） 字典；以及

允许用户定义的类重载复制操作或复制的组件集合。

该模块不复制模块、方法、栈追踪（stack trace）、栈帧（stack frame）、文件、套接字、窗口、数组以及任何类似的类型。它通过不改变地返回原始对象来（浅层或深层地）“复制”函数和类；这与 pickle 模块处理这类问题的方式是相似的。

制作字典的浅层复制可以使用 dict.copy() 方法，而制作列表的浅层复制可以通过赋值整个列表的切片完成，例如，copied_list = original_list[:]。

类可以使用与控制序列化（pickling）操作相同的接口来控制复制操作，关于这些方法的描述信息请参考 pickle 模块。实际上，copy 模块使用的正是从 copyreg 模块中注册的 pickle 函数。

想要给一个类定义它自己的拷贝操作实现，可以通过定义特殊方法 __copy__() 和 __deepcopy__()。 调用前者以实现浅层拷贝操作，该方法不用传入额外参数。 调用后者以实现深层拷贝操作；它应传入一个参数即 memo 字典。 如果 __deepcopy__() 实现需要创建一个组件的深层拷贝，它应当调用 deepcopy() 函数并以该组件作为第一个参数，而将 memo 字典作为第二个参数。

参见

模块 pickle

讨论了支持对象状态检索和恢复的特殊方法。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

8.11. pprint — 数据美化输出

源代码： Lib/pprint.py [https://github.com/python/cpython/tree/3.6/Lib/pprint.py]

pprint 模块提供了“美化打印”任意 Python 数据结构的功能，这种美化形式可用作对解释器的输入。 如果经格式化的结构包含非基本 Python 类型的对象，则其美化形式可能无法被加载。 包含文件、套接字或类对象，以及许多其他不能用 Python 字面值来表示的对象都有可能导致这样的结果。

格式化后的形式会在可能的情况下以单行来表示对象，并在无法在允许宽度内容纳对象的情况下将其分为多行。 如果你需要调整宽度限制则应显式地构造 PrettyPrinter 对象。

Dictionaries are sorted by key before the display is computed. 字典在被计算前通过键值来排序。

pprint 模块定义了一个类：

class pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False)

构造一个 PrettyPrinter 实例。 此构造器接受几个关键字形参。 使用 stream 关键字可设置输出流；在流对象上使用的唯一方法是文件协议的 write() 方法。 如果未指定此关键字，则 PrettyPrinter 会选择 sys.stdout。 每个递归层级的缩进量由 indent 指定；默认值为一。 其他值可导致输出看起来有些怪异，但可使得嵌套结构更易区分。 可被打印的层级数量由 depth 控制；如果数据结构的层级被打印得过深，其所包含的下一层级会被替换为 ...。 在默认情况下，对被格式化对象的层级深度没有限制。 希望的输出宽度可使用 width 形参来限制；默认值为 80 个字符。 如果一个结构无法在限定宽度内被格式化，则将做到尽可能接近。 如果 compact 为假值（默认）则长序列的每一项将被格式化为单独的行。 如果 compact 为真值，则格式化将在 width 可容纳的情况下把尽可能多的项放入每个输出行。

在 3.4 版更改: 加入*compact* 参数。

>>> import pprint
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff[:])
>>> pp = pprint.PrettyPrinter(indent=4)
>>> pp.pprint(stuff)
[   ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
    'spam',
    'eggs',
    'lumberjack',
    'knights',
    'ni']
>>> pp = pprint.PrettyPrinter(width=41, compact=True)
>>> pp.pprint(stuff)
[['spam', 'eggs', 'lumberjack',
  'knights', 'ni'],
 'spam', 'eggs', 'lumberjack', 'knights',
 'ni']
>>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
... ('parrot', ('fresh fruit',))))))))
>>> pp = pprint.PrettyPrinter(depth=6)
>>> pp.pprint(tup)
('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))

pprint 模块还提供了一些快捷函数：

pprint.pformat(object, indent=1, width=80, depth=None, *, compact=False)

将 object 格式化表示作为字符串返回。 indent, width, depth 和 compact 将作为格式化形参被传入 PrettyPrinter 构造器。

在 3.4 版更改: 加入*compact* 参数。

pprint.pprint(object, stream=None, indent=1, width=80, depth=None, *, compact=False)

在 stream 上打印 object 的格式化表示，并附带一个换行符。 如果 stream 为 None，则使用 sys.stdout。 这可以替换 print() 函数在交互式解释器中使用以查看值（你甚至可以执行重新赋值 print = pprint.pprint 以在特定作用域中使用）。 indent, width, depth 和 compact 将作为格式化形参被传给 PrettyPrinter 构造器。

在 3.4 版更改: 加入*compact* 参数。

>>> import pprint
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff)
>>> pprint.pprint(stuff)
[<Recursion on list with id=...>,
 'spam',
 'eggs',
 'lumberjack',
 'knights',
 'ni']

pprint.isreadable(object)

Determine if the formatted representation of object is “readable,” or can be used to reconstruct the value using eval(). This always returns False for recursive objects.

>>> pprint.isreadable(stuff)
False

pprint.isrecursive(object)

确定 object 是否需要递归表示。

此外还定义了一个支持函数：

pprint.saferepr(object)

返回 object 的字符串表示，并为递归数据结构提供保护。 如果 object 的表示形式公开了一个递归条目，该递归引用会被表示为 <Recursion on typename with id=number>。 该表示因而不会进行其它格式化。

>>> pprint.saferepr(stuff)
"[<Recursion on list with id=...>, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"

8.11.1. PrettyPrinter 对象

PrettyPrinter 的实例具有下列方法：

PrettyPrinter.pformat(object)

返回 object 格式化表示。 这会将传给 PrettyPrinter 构造器的选项纳入考虑。

PrettyPrinter.pprint(object)

在所配置的流上打印 object 的格式化表示，并附加一个换行符。

下列方法提供了与同名函数相对应的实现。 在实例上使用这些方法效率会更高一些，因为不需要创建新的 PrettyPrinter 对象。

PrettyPrinter.isreadable(object)

确定对象的格式化表示是否“可读”，或者是否可使用 eval() 重建对象值。 请注意此方法对于递归对象将返回 False。 如果设置了 PrettyPrinter 的 depth 形参并且对象深度超出允许范围，此方法将返回 False。

PrettyPrinter.isrecursive(object)

确定对象是否需要递归表示。

此方法作为一个钩子提供，允许子类修改将对象转换为字符串的方式。 默认实现使用 saferepr() 实现的内部方式。

PrettyPrinter.format(object, context, maxlevels, level)

返回三个值：字符串形式的 object 已格式化版本，指明结果是否可读的旗标，以及指明是否检测到递归的旗标。 第一个参数是要表示的对象。 第二个是以对象 id() 为键的字典，这些对象是当前表示上下文的一部分（影响 object 表示的直接和间接容器）；如果需要呈现一个已经在 context 中表示的对象，则第三个返回值应当为 True。 对 format() 方法的递归调用应当将容器的附加条目添加到此字典中。 第三个参数 maxlevels 给出了对递归的请求限制；如果没有请求限制则其值将为 0。 此参数应当不加修改地传给递归调用。 第四个参数 level 给出于当前层级；传给递归调用的参数值应当小于当前调用的值。

8.11.2. 示例

为了演示 pprint() 函数及其形参的几种用法，让我们从 PyPI [https://pypi.org] 获取关于某个项目的信息:

>>> import json
>>> import pprint
>>> from urllib.request import urlopen
>>> with urlopen('https://pypi.org/pypi/sampleproject/json') as resp:
...     project_info = json.load(resp)['info']

pprint() 以其基本形式显示了整个对象:

>>> pprint.pprint(project_info)
{'author': 'The Python Packaging Authority',
 'author_email': 'pypa-dev@googlegroups.com',
 'bugtrack_url': None,
 'classifiers': ['Development Status :: 3 - Alpha',
                 'Intended Audience :: Developers',
                 'License :: OSI Approved :: MIT License',
                 'Programming Language :: Python :: 2',
                 'Programming Language :: Python :: 2.6',
                 'Programming Language :: Python :: 2.7',
                 'Programming Language :: Python :: 3',
                 'Programming Language :: Python :: 3.2',
                 'Programming Language :: Python :: 3.3',
                 'Programming Language :: Python :: 3.4',
                 'Topic :: Software Development :: Build Tools'],
 'description': 'A sample Python project\n'
                '=======================\n'
                '\n'
                'This is the description file for the project.\n'
                '\n'
                'The file should use UTF-8 encoding and be written using '
                'ReStructured Text. It\n'
                'will be used to generate the project webpage on PyPI, and '
                'should be written for\n'
                'that purpose.\n'
                '\n'
                'Typical contents for this file would include an overview of '
                'the project, basic\n'
                'usage examples, etc. Generally, including the project '
                'changelog in here is not\n'
                'a good idea, although a simple "What\'s New" section for the '
                'most recent version\n'
                'may be appropriate.',
 'description_content_type': None,
 'docs_url': None,
 'download_url': 'UNKNOWN',
 'downloads': {'last_day': -1, 'last_month': -1, 'last_week': -1},
 'home_page': 'https://github.com/pypa/sampleproject',
 'keywords': 'sample setuptools development',
 'license': 'MIT',
 'maintainer': None,
 'maintainer_email': None,
 'name': 'sampleproject',
 'package_url': 'https://pypi.org/project/sampleproject/',
 'platform': 'UNKNOWN',
 'project_url': 'https://pypi.org/project/sampleproject/',
 'project_urls': {'Download': 'UNKNOWN',
                  'Homepage': 'https://github.com/pypa/sampleproject'},
 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
 'requires_dist': None,
 'requires_python': None,
 'summary': 'A sample Python project',
 'version': '1.2.0'}

结果可以被限制到特定的 depth (更深层的内容将使用省略号):

>>> pprint.pprint(project_info, depth=1)
{'author': 'The Python Packaging Authority',
 'author_email': 'pypa-dev@googlegroups.com',
 'bugtrack_url': None,
 'classifiers': [...],
 'description': 'A sample Python project\n'
                '=======================\n'
                '\n'
                'This is the description file for the project.\n'
                '\n'
                'The file should use UTF-8 encoding and be written using '
                'ReStructured Text. It\n'
                'will be used to generate the project webpage on PyPI, and '
                'should be written for\n'
                'that purpose.\n'
                '\n'
                'Typical contents for this file would include an overview of '
                'the project, basic\n'
                'usage examples, etc. Generally, including the project '
                'changelog in here is not\n'
                'a good idea, although a simple "What\'s New" section for the '
                'most recent version\n'
                'may be appropriate.',
 'description_content_type': None,
 'docs_url': None,
 'download_url': 'UNKNOWN',
 'downloads': {...},
 'home_page': 'https://github.com/pypa/sampleproject',
 'keywords': 'sample setuptools development',
 'license': 'MIT',
 'maintainer': None,
 'maintainer_email': None,
 'name': 'sampleproject',
 'package_url': 'https://pypi.org/project/sampleproject/',
 'platform': 'UNKNOWN',
 'project_url': 'https://pypi.org/project/sampleproject/',
 'project_urls': {...},
 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
 'requires_dist': None,
 'requires_python': None,
 'summary': 'A sample Python project',
 'version': '1.2.0'}

此外，还可以设置建议的最大字符 width。 如果一个对象无法被拆分，则将超出指定宽度:

>>> pprint.pprint(project_info, depth=1, width=60)
{'author': 'The Python Packaging Authority',
 'author_email': 'pypa-dev@googlegroups.com',
 'bugtrack_url': None,
 'classifiers': [...],
 'description': 'A sample Python project\n'
                '=======================\n'
                '\n'
                'This is the description file for the '
                'project.\n'
                '\n'
                'The file should use UTF-8 encoding and be '
                'written using ReStructured Text. It\n'
                'will be used to generate the project '
                'webpage on PyPI, and should be written '
                'for\n'
                'that purpose.\n'
                '\n'
                'Typical contents for this file would '
                'include an overview of the project, '
                'basic\n'
                'usage examples, etc. Generally, including '
                'the project changelog in here is not\n'
                'a good idea, although a simple "What\'s '
                'New" section for the most recent version\n'
                'may be appropriate.',
 'description_content_type': None,
 'docs_url': None,
 'download_url': 'UNKNOWN',
 'downloads': {...},
 'home_page': 'https://github.com/pypa/sampleproject',
 'keywords': 'sample setuptools development',
 'license': 'MIT',
 'maintainer': None,
 'maintainer_email': None,
 'name': 'sampleproject',
 'package_url': 'https://pypi.org/project/sampleproject/',
 'platform': 'UNKNOWN',
 'project_url': 'https://pypi.org/project/sampleproject/',
 'project_urls': {...},
 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
 'requires_dist': None,
 'requires_python': None,
 'summary': 'A sample Python project',
 'version': '1.2.0'}

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

8.12. reprlib — 另一种 repr() 实现

源代码: Lib/reprlib.py [https://github.com/python/cpython/tree/3.6/Lib/reprlib.py]

reprlib 模块提供了一种对象表示的产生方式，它会对结果字符串的大小进行限制。 该方式被用于 Python 调试器，也适用于某些其他场景。

此模块提供了一个类、一个实例和一个函数：

class reprlib.Repr

该类提供了格式化服务 适用于实现与内置 repr() 相似的方法；其中附加了针对不同对象类型的大小限制，以避免生成超长的表示。

reprlib.aRepr

这是 Repr 的一个实例，用于提供如下所述的 repr() 函数。 改变此对象的属性将会影响 repr() 和 Python 调试器所使用的大小限制。

reprlib.repr(obj)

这是 aRepr 的 repr() 方法。 它会返回与同名内置函数所返回字符串相似的字符串，区别在于附带了对多数类型的大小限制。

在大小限制工具以外，此模块还提供了一个装饰器，用于检测对 __repr__() 的递归调用并改用一个占位符来替换。

@reprlib.recursive_repr(fillvalue="...")

用于为 __repr__() 方法检测同一线程内部递归调用的装饰器。 如果执行了递归调用，则会返回 fillvalue，否则执行正常的 __repr__() 调用。 例如：

>>> class MyList(list):
...     @recursive_repr()
...     def __repr__(self):
...         return '<' + '|'.join(map(repr, self)) + '>'
...
>>> m = MyList('abc')
>>> m.append(m)
>>> m.append('x')
>>> print(m)
<'a'|'b'|'c'|...|'x'>

3.2 新版功能.

8.12.1. Repr 对象

Repr 实例对象包含一些属性可以用于为不同对象类型的表示提供大小限制，还包含一些方法可以格式化特定的对象类型。

Repr.maxlevel

创建递归表示形式的深度限制。 默认为 6。

Repr.maxdict

Repr.maxlist

Repr.maxtuple

Repr.maxset

Repr.maxfrozenset

Repr.maxdeque

Repr.maxarray

表示命名对象类型的条目数量限制。 对于 maxdict 的默认值为 4，对于 maxarray 为 5，对于其他则为 6。

Repr.maxlong

表示整数的最大字符数量。 数码会从中间被丢弃。 默认值为 40。

Repr.maxstring

表示字符串的字符数量限制。 请注意字符源会使用字符串的“正常”表示形式：如果表示中需要用到转义序列，在缩短表示时它们可能会被破坏。 默认值为 30。

Repr.maxother

此限制用于控制在 Repr 对象上没有特定的格式化方法可用的对象类型的大小。 它会以类似 maxstring 的方式被应用。 默认值为 20。

Repr.repr(obj)

内置 repr() 的等价形式，它使用实例专属的格式化。

Repr.repr1(obj, level)

供 repr() 使用的递归实现。 此方法使用 obj 的类型来确定要调用哪个格式化方法，并传入 obj 和 level。 类型专属的方法应当调用 repr1() 来执行递归格式化，在递归调用中使用 level - 1 作为 level 的值。

Repr.repr_TYPE(obj, level)

特定类型的格式化方法会被实现为基于类型名称来命名的方法。 在方法名称中，TYPE 会被替换为 '_'.join(type(obj).__name__.split())。 对这些方法的分派会由 repr1() 来处理。 需要对值进行递归格式化的类型专属方法应当调用 self.repr1(subobj, level - 1)。

8.12.2. 子类化 Repr 对象

通过 Repr.repr1() 使用动态分派允许 Repr 的子类添加对额外内置对象类型的支持，或是修改对已支持类型的处理。 这个例子演示了如何添加对文件对象的特殊支持:

import reprlib
import sys

class MyRepr(reprlib.Repr):

    def repr_TextIOWrapper(self, obj, level):
        if obj.name in {'<stdin>', '<stdout>', '<stderr>'}:
            return obj.name
        return repr(obj)

aRepr = MyRepr()
print(aRepr.repr(sys.stdin))         # prints '<stdin>'

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

8.13. enum — 枚举类型支持

3.4 新版功能.

源代码： Lib/enum.py [https://github.com/python/cpython/tree/3.6/Lib/enum.py]

枚举是一组符号名称（枚举成员）的集合，枚举成员应该是唯一的、不可变的。在枚举中，可以对成员进行恒等比较，并且枚举本身是可迭代的。

8.13.1. 模块内容

此模块定义了四个枚举类，它们可被用来定义名称和值的不重复集合: Enum, IntEnum, Flag 和 IntFlag。 此外还定义了一个装饰器 unique() 和一个辅助类 auto。

class enum.Enum

用于创建枚举型常数的基类。 请参阅 Functional API 小节了解另一种替代性的构建语法。

class enum.IntEnum

用于创建同时也是 int 的子类的枚举型常数的基类。

class enum.IntFlag

此基类用于创建可使用按位运算符进行组合而不会丢失其 IntFlag 成员资格的枚举常量。 IntFlag 成员同样也是 int 的子类。

class enum.Flag

此基类用于创建枚举常量 可使用按位运算符进行组合而不会丢失其 Flag 成员资格的枚举常量。

enum.unique()

此 Enum 类装饰器可确保只将一个名称绑定到任意一个值。

class enum.auto

Instances are replaced with an appropriate value for Enum members.

3.6 新版功能: Flag, IntFlag, auto

8.13.2. 创建一个 Enum

枚举是使用 class 语法来创建的，这使得它们易于读写。 另一种替代创建方法的描述见 Functional API。 要定义一个枚举，可以对 Enum 进行如下的子类化:

>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...

注解

Enum 的成员值

成员值可以为任意类型: int, str 等等。 如果具体的值不重要，你可以使用 auto 实例，将为你选择适当的值。 但如果你混用 auto 与其他值则需要小心谨慎。

注解

命名法

类 Color 是一个 enumeration (或称 enum)

属性 Color.RED, Color.GREEN 等等是 枚举成员 (或称 enum 成员) 并且被用作常量。

枚举成员具有 名称 和 值 (例如 Color.RED 的名称为 RED，Color.BLUE 的值为 3 等等)

注解

虽然我们使用 class 语法来创建 Enum，但 Enum 并不是普通的 Python 类。 更多细节请参阅 How are Enums different?。

枚举成员具有适合人类阅读的表示形式:

>>> print(Color.RED)
Color.RED

…而它们的 repr 包含更多信息:

>>> print(repr(Color.RED))
<Color.RED: 1>

一个枚举成员的 type 就是它所从属的枚举:

>>> type(Color.RED)
<enum 'Color'>
>>> isinstance(Color.GREEN, Color)
True
>>>

Enum 的成员还有一个包含其条目名称的特征属性:

>>> print(Color.RED.name)
RED

枚举支持按照定义顺序进行迭代:

>>> class Shake(Enum):
...     VANILLA = 7
...     CHOCOLATE = 4
...     COOKIES = 9
...     MINT = 3
...
>>> for shake in Shake:
...     print(shake)
...
Shake.VANILLA
Shake.CHOCOLATE
Shake.COOKIES
Shake.MINT

枚举成员是可哈希的，因此它们可在字典和集合中可用:

>>> apples = {}
>>> apples[Color.RED] = 'red delicious'
>>> apples[Color.GREEN] = 'granny smith'
>>> apples == {Color.RED: 'red delicious', Color.GREEN: 'granny smith'}
True

8.13.3. 对枚举成员及其属性的程序化访问

有时对枚举中的成员进行程序化访问是很有用的（例如在某些场合不能使用 Color.RED 因为在编程时并不知道要指定的确切颜色）。 Enum 允许这样的访问:

>>> Color(1)
<Color.RED: 1>
>>> Color(3)
<Color.BLUE: 3>

如果你希望通过 name 来访问枚举成员，可使用条目访问:

>>> Color['RED']
<Color.RED: 1>
>>> Color['GREEN']
<Color.GREEN: 2>

如果你有一个枚举成员并且需要它的 name 或 value:

>>> member = Color.RED
>>> member.name
'RED'
>>> member.value
1

8.13.4. 复制枚举成员和值

不允许有同名的枚举成员:

>>> class Shape(Enum):
...     SQUARE = 2
...     SQUARE = 3
...
Traceback (most recent call last):
...
TypeError: Attempted to reuse key: 'SQUARE'

但是，允许两个枚举成员有相同的值。 假定两个成员 A 和 B 有相同的值（且 A 先被定义），则 B 就是 A 的一个别名。 按值查找 A 和 B 的值将返回 A。 按名称查找 B 也将返回 A:

>>> class Shape(Enum):
...     SQUARE = 2
...     DIAMOND = 1
...     CIRCLE = 3
...     ALIAS_FOR_SQUARE = 2
...
>>> Shape.SQUARE
<Shape.SQUARE: 2>
>>> Shape.ALIAS_FOR_SQUARE
<Shape.SQUARE: 2>
>>> Shape(2)
<Shape.SQUARE: 2>

注解

试图创建具有与某个已定义的属性（另一个成员或方法等）相同名称的成员或者试图创建具有相同名称的属性也是不允许的。

8.13.5. 确保唯一的枚举值

默认情况下，枚举允许有多个名称作为某个相同值的别名。 如果不想要这样的行为，可以使用以下装饰器来确保每个值在枚举中只被使用一次:

@enum.unique

专用于枚举的 class 装饰器。 它会搜索一个枚举的 __members__ 并收集所找到的任何别名；只要找到任何别名就会引发 ValueError 并附带相关细节信息:

>>> from enum import Enum, unique
>>> @unique
... class Mistake(Enum):
...     ONE = 1
...     TWO = 2
...     THREE = 3
...     FOUR = 3
...
Traceback (most recent call last):
...
ValueError: duplicate values found in <enum 'Mistake'>: FOUR -> THREE

8.13.6. 使用自动设定的值

如果确切的值不重要，你可以使用 auto:

>>> from enum import Enum, auto
>>> class Color(Enum):
...     RED = auto()
...     BLUE = auto()
...     GREEN = auto()
...
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

值将由 _generate_next_value_() 来选择，该函数可以被重载:

>>> class AutoName(Enum):
...     def _generate_next_value_(name, start, count, last_values):
...         return name
...
>>> class Ordinal(AutoName):
...     NORTH = auto()
...     SOUTH = auto()
...     EAST = auto()
...     WEST = auto()
...
>>> list(Ordinal)
[<Ordinal.NORTH: 'NORTH'>, <Ordinal.SOUTH: 'SOUTH'>, <Ordinal.EAST: 'EAST'>, <Ordinal.WEST: 'WEST'>]

注解

默认 _generate_next_value_() 方法的目标是提供所给出的最后一个 int 所在序列的下一个 int，但这种行为方式属于实现细节并且可能发生改变。

8.13.7. 迭代

对枚举成员的迭代不会给出别名:

>>> list(Shape)
[<Shape.SQUARE: 2>, <Shape.DIAMOND: 1>, <Shape.CIRCLE: 3>]

特殊属性 __members__ 是一个将名称映射到成员的有序字典。 它包含枚举中定义的所有名称，包括别名:

>>> for name, member in Shape.__members__.items():
...     name, member
...
('SQUARE', <Shape.SQUARE: 2>)
('DIAMOND', <Shape.DIAMOND: 1>)
('CIRCLE', <Shape.CIRCLE: 3>)
('ALIAS_FOR_SQUARE', <Shape.SQUARE: 2>)

__members__ 属性可被用于对枚举成员进行详细的程序化访问。 例如，找出所有别名:

>>> [name for name, member in Shape.__members__.items() if member.name != name]
['ALIAS_FOR_SQUARE']

8.13.8. 比较运算

枚举成员是按标识号进行比较的:

>>> Color.RED is Color.RED
True
>>> Color.RED is Color.BLUE
False
>>> Color.RED is not Color.BLUE
True

枚举值之间的排序比较 不被 支持。 Enum 成员不属于整数 (另请参阅下文的 IntEnum):

>>> Color.RED < Color.BLUE
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'Color' and 'Color'

相等比较的定义如下:

>>> Color.BLUE == Color.RED
False
>>> Color.BLUE != Color.RED
True
>>> Color.BLUE == Color.BLUE
True

与非枚举值的比较将总是不相等（同样地，IntEnum 被显式设计成不同的行为，参见下文）:

>>> Color.BLUE == 2
False

8.13.9. 允许的枚举成员和属性

以上示例使用整数作为枚举值。 使用整数相当简洁方便（并由 Functional API 默认提供），但并不强制要求使用。 在大部分用例中，开发者都关心枚举的实际值是什么。 但如果值 确实 重要，则枚举可以使用任意的值。

枚举属于 Python 的类，并可具有普通方法和特殊方法。 如果我们有这样一个枚举:

>>> class Mood(Enum):
...     FUNKY = 1
...     HAPPY = 3
...
...     def describe(self):
...         # self is the member here
...         return self.name, self.value
...
...     def __str__(self):
...         return 'my custom str! {0}'.format(self.value)
...
...     @classmethod
...     def favorite_mood(cls):
...         # cls here is the enumeration
...         return cls.HAPPY
...

那么:

>>> Mood.favorite_mood()
<Mood.HAPPY: 3>
>>> Mood.HAPPY.describe()
('HAPPY', 3)
>>> str(Mood.FUNKY)
'my custom str! 1'

The rules for what is allowed are as follows: names that start and end with a single underscore are reserved by enum and cannot be used; all other attributes defined within an enumeration will become members of this enumeration, with the exception of special methods (__str__(), __add__(), etc.) and descriptors (methods are also descriptors).

注意：如果你的枚举定义了 __new__() 和/或 __init__() 那么指定给枚举成员的任何值都会被传入这些方法。 请参阅示例 Planet。

8.13.10. Restricted subclassing of enumerations

Subclassing an enumeration is allowed only if the enumeration does not define any members. So this is forbidden:

>>> class MoreColor(Color):
...     PINK = 17
...
Traceback (most recent call last):
...
TypeError: Cannot extend enumerations

但是允许这样的写法:

>>> class Foo(Enum):
...     def some_behavior(self):
...         pass
...
>>> class Bar(Foo):
...     HAPPY = 1
...     SAD = 2
...

允许子类化定义了成员的枚举将会导致违反类型与实例的某些重要的不可变规则。 在另一方面，允许在一组枚举之间共享某些通用行为也是有意义的。 （请参阅示例 OrderedEnum 。）

8.13.11. 封存

枚举可以被封存与解封:

>>> from test.test_enum import Fruit
>>> from pickle import dumps, loads
>>> Fruit.TOMATO is loads(dumps(Fruit.TOMATO))
True

封存的常规限制同样适用：可封存枚举必须在模块的最高层级中定义，因为解封操作要求它们可以从该模块导入。

注解

使用 pickle 协议版本 4 可以方便地封存嵌套在其他类中的枚举。

通过在枚举类中定义 __reduce_ex__() 可以对 Enum 成员的封存/解封方式进行修改。

8.13.12. 可用 API

Enum 类属于可调用对象，它提供了以下可用的 API:

>>> Animal = Enum('Animal', 'ANT BEE CAT DOG')
>>> Animal
<enum 'Animal'>
>>> Animal.ANT
<Animal.ANT: 1>
>>> Animal.ANT.value
1
>>> list(Animal)
[<Animal.ANT: 1>, <Animal.BEE: 2>, <Animal.CAT: 3>, <Animal.DOG: 4>]

该 API 的主义类似于 namedtuple。 调用 Enum 的第一个参数是枚举的名称。

第二个参数是枚举成员名称的 来源。 它可以是一个用空格分隔的名称字符串、名称序列、键/值对 2 元组的序列，或者名称到值的映射（例如字典）。 最后两种选项使得可以为枚举任意赋值；其他选项会自动以从 1 开始递增的整数赋值（使用 start 形参可指定不同的起始值）。 返回值是一个派生自 Enum 的新类。 换句话说，以上对 Animal 的赋值就等价于:

>>> class Animal(Enum):
...     ANT = 1
...     BEE = 2
...     CAT = 3
...     DOG = 4
...

默认以 1 而以 0 作为起始数值的原因在于 0 的布尔值为 False，但所有枚举成员都应被求值为 True。

封存通过功能性 API 创建的枚举可能会有点麻烦，因为要使用帧堆栈的实现细节来尝试并找出枚举是在哪个模块中创建的（例如，当你使用不同模块的工具函数时可能会失败，在 IronPython 或 Jython 上也可能会没有效果）。 解决办法是显式地指定模块名称，如下所示:

>>> Animal = Enum('Animal', 'ANT BEE CAT DOG', module=__name__)

警告

如果未提供 module，且 Enum 无法确定是哪个模块，新的 Enum 成员将不可被解封；为了让错误尽量靠近源头，封存将被禁用。

新的 pickle 协议版本 4 在某些情况下同样依赖于 __qualname__ 被设为特定位置以便 pickle 能够找到相应的类。 例如，类是否存在于全局作用域的 SomeData 类中:

>>> Animal = Enum('Animal', 'ANT BEE CAT DOG', qualname='SomeData.Animal')

完整的签名为:

Enum(value='NewEnumName', names=<...>, *, module='...', qualname='...', type=<mixed-in class>, start=1)

值

将被新 Enum 类将记录为其名称的数据。

names

Enum 的成员。 这可以是一个空格或逗号分隔的字符串 (起始值将为 1，除非另行指定):

'RED GREEN BLUE' | 'RED,GREEN,BLUE' | 'RED, GREEN, BLUE'

或是一个名称的迭代器:

['RED', 'GREEN', 'BLUE']

或是一个 (名称, 值) 对的迭代器:

[('CYAN', 4), ('MAGENTA', 5), ('YELLOW', 6)]

或是一个映射:

{'CHARTREUSE': 7, 'SEA_GREEN': 11, 'ROSEMARY': 42}

module

新 Enum 类所在模块的名称。

qualname

新 Enum 类在模块中的具体位置。

type

要加入新 Enum 类的类型。

start

当只传入名称时要使用的起始数值。

在 3.5 版更改: 增加了 start 形参。

8.13.13. 派生的枚举

8.13.13.1. IntEnum

所提供的第一个变种 Enum 同时也是 int 的一个子类。 IntEnum 的成员可与整数进行比较；通过扩展，不同类型的整数枚举也可以相互进行比较:

>>> from enum import IntEnum
>>> class Shape(IntEnum):
...     CIRCLE = 1
...     SQUARE = 2
...
>>> class Request(IntEnum):
...     POST = 1
...     GET = 2
...
>>> Shape == 1
False
>>> Shape.CIRCLE == 1
True
>>> Shape.CIRCLE == Request.POST
True

不过，它们仍然不可与标准 Enum 枚举进行比较:

>>> class Shape(IntEnum):
...     CIRCLE = 1
...     SQUARE = 2
...
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...
>>> Shape.CIRCLE == Color.RED
False

IntEnum 值在其他方面的行为都如你预期的一样类似于整数:

>>> int(Shape.CIRCLE)
1
>>> ['a', 'b', 'c'][Shape.CIRCLE]
'b'
>>> [i for i in range(Shape.SQUARE)]
[0, 1]

8.13.13.2. IntFlag

所提供的下一个 Enum 的变种 IntFlag 同样是基于 int 的，不同之处在于 IntFlag 成员可使用按位运算符 (&, |, ^, ~) 进行合并且结果仍然为 IntFlag 成员。 如果，正如名称所表明的，IntFlag 成员同时也是 int 的子类，并能在任何使用 int 的场合被使用。 IntFlag 成员进行除按位运算以外的其他运算都将导致失去 IntFlag 成员资格。

3.6 新版功能.

示例 IntFlag 类:

>>> from enum import IntFlag
>>> class Perm(IntFlag):
...     R = 4
...     W = 2
...     X = 1
...
>>> Perm.R | Perm.W
<Perm.R|W: 6>
>>> Perm.R + Perm.W
6
>>> RW = Perm.R | Perm.W
>>> Perm.R in RW
True

对于组合同样可以进行命名:

>>> class Perm(IntFlag):
...     R = 4
...     W = 2
...     X = 1
...     RWX = 7
>>> Perm.RWX
<Perm.RWX: 7>
>>> ~Perm.RWX
<Perm.-8: -8>

IntFlag 和 Enum 的另一个重要区别在于如果没有设置任何旗标（值为 0），则其布尔值为 False:

>>> Perm.R & Perm.X
<Perm.0: 0>
>>> bool(Perm.R & Perm.X)
False

由于 IntFlag 成员同时也是 int 的子类，因此它们可以相互组合:

>>> Perm.X | 8
<Perm.8|X: 9>

8.13.13.3. 标志

最后一个变种是 Flag。 与 IntFlag 类似，Flag 成员可使用按位运算符 (&, |, ^, ~) 进行组合，与 IntFlag 不同的是它们不可与任何其它 Flag 枚举或 int 进行组合或比较。 虽然可以直接指定值，但推荐使用 auto 作为值以便让 Flag 选择适当的值。

3.6 新版功能.

与 IntFlag 类似，如果 Flag 成员的某种组合导致没有设置任何旗标，则其布尔值为 False:

>>> from enum import Flag, auto
>>> class Color(Flag):
...     RED = auto()
...     BLUE = auto()
...     GREEN = auto()
...
>>> Color.RED & Color.GREEN
<Color.0: 0>
>>> bool(Color.RED & Color.GREEN)
False

单个旗标的值应当为二的乘方 (1, 2, 4, 8, …)，旗标的组合则无此限制:

>>> class Color(Flag):
...     RED = auto()
...     BLUE = auto()
...     GREEN = auto()
...     WHITE = RED | BLUE | GREEN
...
>>> Color.WHITE
<Color.WHITE: 7>

对 “no flags set” 条件指定一个名称并不会改变其布尔值:

>>> class Color(Flag):
...     BLACK = 0
...     RED = auto()
...     BLUE = auto()
...     GREEN = auto()
...
>>> Color.BLACK
<Color.BLACK: 0>
>>> bool(Color.BLACK)
False

注解

对于大多数新代码，强烈推荐使用 Enum 和 Flag，因为 IntEnum 和 IntFlag 打破了枚举的某些语义约定（例如可以同整数进行比较，并因而导致此行为被传递给其他无关的枚举）。 IntEnum 和 IntFlag 的使用应当仅限于 Enum 和 Flag 无法使用的场合；例如，当使用枚举替代整数常量时，或是与其他系统进行交互操作时。

8.13.13.4. 其他事项

虽然 IntEnum 是 enum 模块的一部分，但要独立实现也应该相当容易:

class IntEnum(int, Enum):
    pass

这里演示了如何定义类似的派生枚举；例如一个混合了 str 而不是 int 的 StrEnum。

几条规则：

当子类化 Enum 时，在基类序列中的混合类型必须出现于 Enum 本身之前，如以上 IntEnum 的例子所示。

虽然 Enum 可以拥有任意类型的成员，不过一旦你混合了附加类型，则所有成员必须为相应类型的值，如在上面的例子中即为 int。 此限制不适用于仅添加方法而未指定另一数据类型如 int 或 str 的混合类。

当混合了另一数据类型时，value 属性会 不同于 枚举成员自身，但它们仍保持等价且比较结果也相等。

%-style formatting: %s 和 %r 会分别调用 Enum 类的 __str__() 和 __repr__()；其他代码 (例如表示 IntEnum 的 %i 或 %h) 会将枚举成员视为对应的混合类型。

格式化字符串字面值, str.format() 和 format() 将使用混合类型的 __format__()。 如果需要 Enum 类的 str() 或 repr()，请使用 !s 或 !r 格式代码。

8.13.14. 有趣的示例

虽然 Enum, IntEnum, IntFlag 和 Flag 预期可覆盖大多数应用场景，但它们无法覆盖全部。 这里有一些不同类型枚举的方案，它们可以被直接使用，或是作为自行创建的参考示例。

8.13.14.1. 省略值

在许多应用场景中人们都不关心枚举的实际值是什么。 有几个方式可以定义此种类型的简单枚举：

使用 auto 的实例作为值

使用 object 的实例作为值

使用描述性的字符串作为值

使用元组作为值并用自定义的 __new__() 以一个 int 值来替代该元组

使用以上任何一种方法均可向用户指明值并不重要，并且使人能够添加、移除或重排序成员而不必改变其余成员的数值。

无论你选择何种方法，你都应当提供一个 repr() 并且它也需要隐藏（不重要的）值:

>>> class NoValue(Enum):
...     def __repr__(self):
...         return '<%s.%s>' % (self.__class__.__name__, self.name)
...

8.13.14.1.1. 使用 auto

使用 auto 看起来是这样:

>>> class Color(NoValue):
...     RED = auto()
...     BLUE = auto()
...     GREEN = auto()
...
>>> Color.GREEN
<Color.GREEN>

8.13.14.1.2. 使用 object

使用 object 的形式如下:

>>> class Color(NoValue):
...     RED = object()
...     GREEN = object()
...     BLUE = object()
...
>>> Color.GREEN
<Color.GREEN>

8.13.14.1.3. 使用描述性字符串

使用字符串作为值的形式如下:

>>> class Color(NoValue):
...     RED = 'stop'
...     GREEN = 'go'
...     BLUE = 'too fast!'
...
>>> Color.GREEN
<Color.GREEN>
>>> Color.GREEN.value
'go'

8.13.14.1.4. 使用自定义的 __new__()

使用自动编号 __new__() 的形式如下:

>>> class AutoNumber(NoValue):
...     def __new__(cls):
...         value = len(cls.__members__) + 1
...         obj = object.__new__(cls)
...         obj._value_ = value
...         return obj
...
>>> class Color(AutoNumber):
...     RED = ()
...     GREEN = ()
...     BLUE = ()
...
>>> Color.GREEN
<Color.GREEN>
>>> Color.GREEN.value
2

注解

如果定义了 __new__() 则它会在创建 Enum 成员期间被使用；随后它将被 Enum 的 __new__() 所替换，该方法会在类创建后被用来查找现有成员。

8.13.14.2. OrderedEnum

一个有序枚举，它不是基于 IntEnum，因此保持了正常的 Enum 不变特性（例如不可与其他枚举进行比较）:

>>> class OrderedEnum(Enum):
...     def __ge__(self, other):
...         if self.__class__ is other.__class__:
...             return self.value >= other.value
...         return NotImplemented
...     def __gt__(self, other):
...         if self.__class__ is other.__class__:
...             return self.value > other.value
...         return NotImplemented
...     def __le__(self, other):
...         if self.__class__ is other.__class__:
...             return self.value <= other.value
...         return NotImplemented
...     def __lt__(self, other):
...         if self.__class__ is other.__class__:
...             return self.value < other.value
...         return NotImplemented
...
>>> class Grade(OrderedEnum):
...     A = 5
...     B = 4
...     C = 3
...     D = 2
...     F = 1
...
>>> Grade.C < Grade.A
True

8.13.14.3. DuplicateFreeEnum

如果发现重复的成员名称则将引发错误而不是创建别名:

>>> class DuplicateFreeEnum(Enum):
...     def __init__(self, *args):
...         cls = self.__class__
...         if any(self.value == e.value for e in cls):
...             a = self.name
...             e = cls(self.value).name
...             raise ValueError(
...                 "aliases not allowed in DuplicateFreeEnum:  %r --> %r"
...                 % (a, e))
...
>>> class Color(DuplicateFreeEnum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...     GRENE = 2
...
Traceback (most recent call last):
...
ValueError: aliases not allowed in DuplicateFreeEnum:  'GRENE' --> 'GREEN'

注解

这个例子适用于子类化 Enum 来添加或改变禁用别名以及其他行为。 如果需要的改变只是禁用别名，也可以选择使用 unique() 装饰器。

8.13.14.4. Planet

如果定义了 __new__() 或 __init__() 则枚举成员的值将被传给这些方法:

>>> class Planet(Enum):
...     MERCURY = (3.303e+23, 2.4397e6)
...     VENUS   = (4.869e+24, 6.0518e6)
...     EARTH   = (5.976e+24, 6.37814e6)
...     MARS    = (6.421e+23, 3.3972e6)
...     JUPITER = (1.9e+27,   7.1492e7)
...     SATURN  = (5.688e+26, 6.0268e7)
...     URANUS  = (8.686e+25, 2.5559e7)
...     NEPTUNE = (1.024e+26, 2.4746e7)
...     def __init__(self, mass, radius):
...         self.mass = mass       # in kilograms
...         self.radius = radius   # in meters
...     @property
...     def surface_gravity(self):
...         # universal gravitational constant  (m3 kg-1 s-2)
...         G = 6.67300E-11
...         return G * self.mass / (self.radius * self.radius)
...
>>> Planet.EARTH.value
(5.976e+24, 6378140.0)
>>> Planet.EARTH.surface_gravity
9.802652743337129

8.13.15. 各种枚举有何区别？

枚举具有自定义的元类，它会影响所派生枚举类及其实例（成员）的各个方面。

8.13.15.1. 枚举类

The EnumMeta metaclass is responsible for providing the __contains__(), __dir__(), __iter__() and other methods that allow one to do things with an Enum class that fail on a typical class, such as list(Color) or some_var in Color. EnumMeta is responsible for ensuring that various other methods on the final Enum class are correct (such as __new__(), __getnewargs__(), __str__() and __repr__()).

8.13.15.2. 枚举成员（即实例）

有关枚举成员最有趣的特点是它们都是单例对象。 EnumMeta 会在创建 Enum 类本身时将它们全部创建完成，然后准备好一个自定义的 __new__()，通过只返回现有的成员实例来确保不会再实例化新的对象。

8.13.15.3. 细节要点

8.13.15.3.1. 支持 __dunder__ 名称

__members__ 是一个 OrderedDict，由 member_name:member 条目组成。 它只在类上可用。

如果指定了 __new__()，它必须创建并返回枚举成员；相应地设定成员的 _value_ 也是一个很好的主意。 一旦所有成员都创建完成它就不会再被使用。

8.13.15.3.2. 支持的 _sunder_ 名称

_name_ – 成员的名称

_value_ – 成员的值；可以在 __new__ 中设置 / 修改

_missing_ – 当未发现某个值时所使用的查找函数；可被重载

_order_ – 用于 Python 2/3 代码以确保成员顺序一致（类属性，在类创建期间会被移除）

_generate_next_value_ – 用于 Functional API 并通过 auto 为枚举成员获取适当的值；可被重载

3.6 新版功能: _missing_, _order_, _generate_next_value_

用来帮助 Python 2 / Python 3 代码保持同步提供 _order_ 属性。 它将与枚举的实际顺序进行对照检查，如果两者不匹配则会引发错误:

>>> class Color(Enum):
...     _order_ = 'RED GREEN BLUE'
...     RED = 1
...     BLUE = 3
...     GREEN = 2
...
Traceback (most recent call last):
...
TypeError: member order does not match _order_

注解

在 Python 2 代码中 _order_ 属性是必须的，因为定义顺序在被记录之前就会丢失。

8.13.15.3.3. Enum 成员类型

Enum 成员是其 Enum 类的实例，一般通过 EnumClass.member 的形式来访问。 在特定情况下它们也可通过 EnumClass.member.member 的形式来访问，但你绝对不应这样做，因为查找可能失败，或者更糟糕地返回你所查找的 Enum 成员以外的对象（这也是成员应使用全大写名称的另一个好理由）:

>>> class FieldTypes(Enum):
...     name = 0
...     value = 1
...     size = 2
...
>>> FieldTypes.value.size
<FieldTypes.size: 2>
>>> FieldTypes.size.value
2

在 3.5 版更改.

8.13.15.3.4. Enum 类和成员的布尔值

混合了非 Enum 类型（例如 int, str 等）的 Enum 成员会按所混合类型的规则被求值；在其他情况下，所有成员都将被求值为 True。 要使你的自定义 Enum 的布尔值取决于成员的值，请在你的类中添加以下代码:

def __bool__(self):
    return bool(self.value)

Enum 类总是会被求值为 True。

8.13.15.3.5. 带有方法的 Enum 类

如果你为你的 Enum 子类添加了额外的方法，如同上述的 Planet 类一样，这些方法将在对成员执行 dir() 时显示出来，但对类执行时则不会显示:

>>> dir(Planet)
['EARTH', 'JUPITER', 'MARS', 'MERCURY', 'NEPTUNE', 'SATURN', 'URANUS', 'VENUS', '__class__', '__doc__', '__members__', '__module__']
>>> dir(Planet.EARTH)
['__class__', '__doc__', '__module__', 'name', 'surface_gravity', 'value']

8.13.15.3.6. 组合 Flag 的成员

如果 Flag 成员的某种组合未被命名，则 repr() 将包含所有已命名的旗标和值中所有已命名的旗标组合:

>>> class Color(Flag):
...     RED = auto()
...     GREEN = auto()
...     BLUE = auto()
...     MAGENTA = RED | BLUE
...     YELLOW = RED | GREEN
...     CYAN = GREEN | BLUE
...
>>> Color(3)  # named combination
<Color.YELLOW: 3>
>>> Color(7)      # not named combination
<Color.CYAN|MAGENTA|BLUE|YELLOW|GREEN|RED: 7>

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

8. 数据类型 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

9. 数字和数学模块

本章介绍的模块提供与数字和数学相关的函数和数据类型。 numbers 模块定义了数字类型的抽象层次结构。 math 和 cmath 模块包含浮点数和复数的各种数学函数。 decimal 模块支持使用任意精度算术的十进制数的精确表示。

本章包含以下模块的文档：

9.1. numbers — 数字的抽象基类

9.1.1. 数字的层次

9.1.2. 类型接口注释。

9.1.2.1. 加入更多数字的ABC

9.1.2.2. 实现算术运算

9.2. math — 数学函数

9.2.1. 数论与表示函数

9.2.2. 幂函数与对数函数

9.2.3. 三角函数

9.2.4. 角度转换

9.2.5. 双曲函数

9.2.6. 特殊函数

9.2.7. 常量

9.3. cmath ——关于复数的数学函数

9.3.1. 到极坐标和从极坐标的转换

9.3.2. 幂函数与对数函数

9.3.3. 三角函数

9.3.4. 双曲函数

9.3.5. 分类函数

9.3.6. 常量

9.4. decimal — 十进制定点和浮点运算

9.4.1. 快速入门教程

9.4.2. Decimal 对象

9.4.2.1. 逻辑操作数

9.4.3. Context 对象

9.4.4. 常量

9.4.5. 舍入模式

9.4.6. 信号

9.4.7. 浮点数说明

9.4.7.1. 通过提升精度来缓解舍入误差

9.4.7.2. 特殊的值

9.4.8. 使用线程

9.4.9. 例程

9.4.10. Decimal FAQ

9.5. fractions — 分数

9.6. random — 生成伪随机数

9.6.1. 簿记功能

9.6.2. 整数用函数

9.6.3. 序列用函数

9.6.4. 实值分布

9.6.5. 替代生成器

9.6.6. 关于再现性的说明

9.6.7. 例子和配方

9.7. statistics — 数学统计函数

9.7.1. 平均值以及对中心位置的评估

9.7.2. 对分散程度的评估

9.7.3. 函数细节

9.7.4. 异常

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

9. 数字和数学模块 »

|

9.1. numbers — 数字的抽象基类

源代码： Lib/numbers.py [https://github.com/python/cpython/tree/3.6/Lib/numbers.py]

numbers 模块 (PEP 3141 [https://www.python.org/dev/peps/pep-3141]) 定义了数字 抽象基类 的层次结构，其中逐级定义了更多操作。 此模块中所定义的类型都不可被实例化。

class numbers.Number

数字的层次结构的基础。 如果你只想确认参数 x 是不是数字而不关心其类型，则使用 isinstance(x, Number)。

9.1.1. 数字的层次

class numbers.Complex

内置在类型 complex 里的子类描述了复数和它的运算操作。这些操作有：转化至 complex 和 bool， real、 imag、+、-、*、/、 abs()、 conjugate()、 == 和 !=。 所有的异常，- 和 != ，都是抽象的。

real

抽象的。得到该数字的实数部分。

imag

抽象的。得到该数字的虚数部分。

abstractmethod conjugate()

抽象的。返回共轭复数。例如 (1+3j).conjugate() == (1-3j)。

class numbers.Real

相对于 Complex，Real 加入了只有实数才能进行的操作。

简单的说，它们是：转化至 float，math.trunc()、 round()、 math.floor()、 math.ceil()、 divmod()、 //、 %、 <、 <=、 >、 和 >=。

实数同样默认支持 complex()、 real、 imag 和 conjugate()。

class numbers.Rational

子类型 Real 并加入 numerator 和 denominator 两种属性，这两种属性应该属于最低的级别。加入后，这默认支持 float()。

numerator

抽象的。

denominator

抽象的。

class numbers.Integral

子类型 Rational 加上转化至 int。 默认支持 float()、 numerator 和 denominator。 在 ** 中加入抽象方法和比特字符串的操作： <<、 >>、 &、 ^、 |、 ~。

9.1.2. 类型接口注释。

实现者需要注意使相等的数字相等并拥有同样的值。当这两个数使用不同的扩展模块时，这其中的差异是很微妙的。例如，用 fractions.Fraction 实现 hash() 如下:

def __hash__(self):
    if self.denominator == 1:
        # Get integers right.
        return hash(self.numerator)
    # Expensive check, but definitely correct.
    if self == float(self):
        return hash(float(self))
    else:
        # Use tuple's hash to avoid a high collision rate on
        # simple fractions.
        return hash((self.numerator, self.denominator))

9.1.2.1. 加入更多数字的ABC

当然，这里有更多支持数字的ABC，如果不加入这些，就将缺少层次感。你可以用如下方法在 Complex 和 Real 中加入 MyFoo:

class MyFoo(Complex): ...
MyFoo.register(Real)

9.1.2.2. 实现算术运算

我们希望实现计算，因此，混合模式操作要么调用一个作者知道参数类型的实现，要么转变成为最接近的内置类型并对这个执行操作。对于子类 Integral，这意味着 __add__() 和 __radd__() 必须用如下方式定义：

class MyIntegral(Integral):

    def __add__(self, other):
        if isinstance(other, MyIntegral):
            return do_my_adding_stuff(self, other)
        elif isinstance(other, OtherTypeIKnowAbout):
            return do_my_other_adding_stuff(self, other)
        else:
            return NotImplemented

    def __radd__(self, other):
        if isinstance(other, MyIntegral):
            return do_my_adding_stuff(other, self)
        elif isinstance(other, OtherTypeIKnowAbout):
            return do_my_other_adding_stuff(other, self)
        elif isinstance(other, Integral):
            return int(other) + int(self)
        elif isinstance(other, Real):
            return float(other) + float(self)
        elif isinstance(other, Complex):
            return complex(other) + complex(self)
        else:
            return NotImplemented

Complex 有 5 种不同的混合类型的操作。 我将上面提到的所有代码作为“模板”称作 MyIntegral 和 OtherTypeIKnowAbout。 a 是 Complex 的子类型 A 的实例 (a : A <: Complex)，同时 b : B <: Complex。 我将要计算 a + b:

如果 A 被定义成一个承认 b 的 __add__()，一切都没有问题。

如果 A 转回成“模板”失败，它将返回一个属于 __add__() 的值，我们需要避免 B 定义了一个更加智能的 __radd__()，因此模板需要返回一个属于 __add__() 的 NotImplemented 。（或者 A 可能完全不实现 __add__() 。）

接着看 B 的 __radd__() 。如果它承认 a ，一切都没有问题。

如果没有成功回退到模板，就没有更多的方法可以去尝试，因此这里将使用默认的实现。

如果 B <: A ， Python 在 A.__add__ 之前尝试 B.__radd__ 。 这是可行的，是通过对 A 的认识实现的，因此这可以在交给 Complex 处理之前处理这些实例。

如果 A <: Complex 和 B <: Real 没有共享任何资源，那么适当的共享操作涉及内置的 complex ，并且分别获得 __radd__() ，因此 a+b == b+a。

由于对任何一直类型的大部分操作是十分相似的，可以定义一个帮助函数，即一个生成后续或相反的实例的生成器。例如，使用 fractions.Fraction 如下：

def _operator_fallbacks(monomorphic_operator, fallback_operator):
    def forward(a, b):
        if isinstance(b, (int, Fraction)):
            return monomorphic_operator(a, b)
        elif isinstance(b, float):
            return fallback_operator(float(a), b)
        elif isinstance(b, complex):
            return fallback_operator(complex(a), b)
        else:
            return NotImplemented
    forward.__name__ = '__' + fallback_operator.__name__ + '__'
    forward.__doc__ = monomorphic_operator.__doc__

    def reverse(b, a):
        if isinstance(a, Rational):
            # Includes ints.
            return monomorphic_operator(a, b)
        elif isinstance(a, numbers.Real):
            return fallback_operator(float(a), float(b))
        elif isinstance(a, numbers.Complex):
            return fallback_operator(complex(a), complex(b))
        else:
            return NotImplemented
    reverse.__name__ = '__r' + fallback_operator.__name__ + '__'
    reverse.__doc__ = monomorphic_operator.__doc__

    return forward, reverse

def _add(a, b):
    """a + b"""
    return Fraction(a.numerator * b.denominator +
                    b.numerator * a.denominator,
                    a.denominator * b.denominator)

__add__, __radd__ = _operator_fallbacks(_add, operator.add)

# ...

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

9. 数字和数学模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

9. 数字和数学模块 »

|

9.2. math — 数学函数

This module is always available. It provides access to the mathematical functions defined by the C standard.

这些函数不适用于复数；如果你需要计算复数，请使用 cmath 模块中的同名函数。将支持计算复数的函数区分开的目的，来自于大多数开发者并不愿意像数学家一样需要学习复数的概念。得到一个异常而不是一个复数结果使得开发者能够更早地监测到传递给这些函数的参数中包含复数，进而调查其产生的原因。

该模块提供了以下函数。除非另有明确说明，否则所有返回值均为浮点数。

9.2.1. 数论与表示函数

math.ceil(x)

返回 x 的上限，即大于或者等于 x 的最小整数。如果 x 不是一个浮点数，则委托 x.__ceil__(), 返回一个 Integral 类的值。

math.copysign(x, y)

返回一个基于 x 的绝对值和 y 的符号的浮点数。在支持带符号零的平台上，copysign(1.0, -0.0) 返回 -1.0.

math.fabs(x)

返回 x 的绝对值。

math.factorial(x)

Return x factorial. Raises ValueError if x is not integral or is negative.

math.floor(x)

返回 x 的向下取整，小于或等于 x 的最大整数。如果 x 不是浮点数，则委托 x.__floor__() ，它应返回 Integral 值。

math.fmod(x, y)

返回 fmod(x, y) ，由平台C库定义。请注意，Python表达式 x % y 可能不会返回相同的结果。C标准的目的是 fmod(x, y) 完全（数学上；到无限精度）等于 x - n*y 对于某个整数 n ，使得结果具有 与 x 相同的符号和小于 abs(y) 的幅度。Python的 x % y 返回带有 y 符号的结果，并且可能不能完全计算浮点参数。 例如， fmod(-1e-100, 1e100) 是 -1e-100 ，但Python的 -1e-100 % 1e100 的结果是 1e100-1e-100 ，它不能完全表示为浮点数，并且取整为令人惊讶的 1e100 。 出于这个原因，函数 fmod() 在使用浮点数时通常是首选，而Python的 x % y 在使用整数时是首选。

math.frexp(x)

以 (m, e) 对的形式返回 x 的尾数和指数。 m 是一个浮点数， e 是一个整数，正好是 x == m * 2**e 。 如果 x 为零，则返回 (0.0, 0) ，否则返回 0.5 <= abs(m) < 1 。这用于以可移植方式“分离”浮点数的内部表示。

math.fsum(iterable)

返回迭代中的精确浮点值。通过跟踪多个中间部分和来避免精度损失:

>>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
0.9999999999999999
>>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
1.0

该算法的准确性取决于IEEE-754算术保证和舍入模式为半偶的典型情况。在某些非Windows版本中，底层C库使用扩展精度添加，并且有时可能会使中间和加倍，导致它在最低有效位中关闭。

有关待进一步讨论和两种替代方法，参见 ASPN cookbook recipes for accurate floating point summation [https://code.activestate.com/recipes/393090/]。

math.gcd(a, b)

返回整数 a 和 b 的最大公约数。如果 a 或 b 之一非零，则 gcd(a, b) 的值是能同时整除 a 和 b 的最大正整数。gcd(0, 0) 返回 0。

3.5 新版功能.

math.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)

若 a 和 b 的值比较接近则返回 True，否则返回 False。

根据给定的绝对和相对容差确定两个值是否被认为是接近的。

rel_tol 是相对容差 —— 它是 a 和 b 之间允许的最大差值，相对于 a 或 b 的较大绝对值。例如，要设置5％的容差，请传递 rel_tol=0.05 。默认容差为 1e-09，确保两个值在大约9位十进制数字内相同。 rel_tol 必须大于零。

abs_tol 是最小绝对容差 —— 对于接近零的比较很有用。 abs_tol 必须至少为零。

如果没有错误发生，结果将是： abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol) 。

IEEE 754特殊值 NaN ， inf 和 -inf 将根据IEEE规则处理。具体来说， NaN 不被认为接近任何其他值，包括 NaN 。 inf 和 -inf 只被认为接近自己。

3.5 新版功能.

参见

PEP 485 [https://www.python.org/dev/peps/pep-0485] —— 用于测试近似相等的函数

math.isfinite(x)

如果 x 既不是无穷大也不是NaN，则返回 True ，否则返回 False 。 （注意 0.0 被认为 是 有限的。）

3.2 新版功能.

math.isinf(x)

如果 x 是正或负无穷大，则返回 True ，否则返回 False 。

math.isnan(x)

如果 x 是 NaN（不是数字），则返回 True ，否则返回 False 。

math.ldexp(x, i)

返回 x * (2**i) 。 这基本上是函数 frexp() 的反函数。

math.modf(x)

返回 x 的小数和整数部分。两个结果都带有 x 的符号并且是浮点数。

math.trunc(x)

返回 Real 值 x 截断为 Integral （通常是整数）。 委托给 x.__trunc__()。

注意 frexp() 和 modf() 具有与它们的C等价函数不同的调用/返回模式：它们采用单个参数并返回一对值，而不是通过 ‘输出形参’ 返回它们的第二个返回参数（Python中没有这样的东西）。

对于 ceil() ， floor() 和 modf() 函数，请注意 所有 足够大的浮点数都是精确整数。Python浮点数通常不超过53位的精度（与平台C double类型相同），在这种情况下，任何浮点 x 与 abs(x) >= 2**52 必然没有小数位。

9.2.2. 幂函数与对数函数

math.exp(x)

Return e**x.

math.expm1(x)

Return e**x - 1. For small floats x, the subtraction in exp(x) - 1 can result in a significant loss of precision [https://en.wikipedia.org/wiki/Loss_of_significance]; the expm1() function provides a way to compute this quantity to full precision:

>>> from math import exp, expm1
>>> exp(1e-5) - 1  # gives result accurate to 11 places
1.0000050000069649e-05
>>> expm1(1e-5)    # result accurate to full precision
1.0000050000166668e-05

3.2 新版功能.

math.log(x[, base])

使用一个参数，返回 x 的自然对数（底为 e ）。

使用两个参数，返回给定的 base 的对数 x ，计算为 log(x)/log(base) 。

math.log1p(x)

返回 1+x (base e) 的自然对数。以对于接近零的 x 精确的方式计算结果。

math.log2(x)

返回 x 以2为底的对数。这通常比 log(x, 2) 更准确。

3.3 新版功能.

参见

int.bit_length() 返回表示二进制整数所需的位数，不包括符号和前导零。

math.log10(x)

返回 x 底为10的对数。这通常比 log(x, 10) 更准确。

math.pow(x, y)

将返回 x 的 y 次幂。特殊情况尽可能遵循C99标准的附录’F’。特别是， pow(1.0, x) 和 pow(x, 0.0) 总是返回 1.0 ，即使 x 是零或NaN。 如果 x 和 y 都是有限的， x 是负数， y 不是整数那么 pow(x, y) 是未定义的，并且引发 ValueError 。

与内置的 ** 运算符不同， math.pow() 将其参数转换为 float 类型。使用 ** 或内置的 pow() 函数来计算精确的整数幂。

math.sqrt(x)

返回 x 的平方根。

9.2.3. 三角函数

math.acos(x)

以弧度为单位返回 x 的反余弦值。

math.asin(x)

以弧度为单位返回 x 的反正弦值。

math.atan(x)

以弧度为单位返回 x 的反正切值。

math.atan2(y, x)

以弧度为单位返回 atan(y / x) 。结果是在 -pi 和 pi 之间。从原点到点 (x, y) 的平面矢量使该角度与正X轴成正比。 atan2() 的点的两个输入的符号都是已知的，因此它可以计算角度的正确象限。 例如， atan(1) 和 atan2(1, 1) 都是 pi/4 ，但 atan2(-1, -1) 是 -3*pi/4 。

math.cos(x)

返回 x 弧度的余弦值。

math.hypot(x, y)

返回欧几里德范数， sqrt(x*x + y*y) 。 这是从原点到点 (x, y) 的向量长度。

math.sin(x)

返回 x 弧度的正弦值。

math.tan(x)

返回 x 弧度的正切值。

9.2.4. 角度转换

math.degrees(x)

将角度 x 从弧度转换为度数。

math.radians(x)

将角度 x 从度数转换为弧度。

9.2.5. 双曲函数

双曲函数 [https://en.wikipedia.org/wiki/Hyperbolic_function] 是基于双曲线而非圆来对三角函数进行模拟。

math.acosh(x)

返回 x 的反双曲余弦值。

math.asinh(x)

返回 x 的反双曲正弦值。

math.atanh(x)

返回 x 的反双曲正切值。

math.cosh(x)

返回 x 的双曲余弦值。

math.sinh(x)

返回 x 的双曲正弦值。

math.tanh(x)

返回 x 的双曲正切值。

9.2.6. 特殊函数

math.erf(x)

返回 x 处的 error function [https://en.wikipedia.org/wiki/Error_function] 。

erf() 函数可用于计算传统的统计函数，如 累积标准正态分布 [https://en.wikipedia.org/wiki/Normal_distribution#Cumulative_distribution_function]

def phi(x):
    'Cumulative distribution function for the standard normal distribution'
    return (1.0 + erf(x / sqrt(2.0))) / 2.0

3.2 新版功能.

math.erfc(x)

返回 x 处的互补误差函数。 互补错误函数 [https://en.wikipedia.org/wiki/Error_function] 定义为 1.0 - erf(x)。 它用于 x 的大值，从其中减去一个会导致 有效位数损失 [https://en.wikipedia.org/wiki/Loss_of_significance]。

3.2 新版功能.

math.gamma(x)

返回 x 处的 伽马函数 [https://en.wikipedia.org/wiki/Gamma_function] 值。

3.2 新版功能.

math.lgamma(x)

返回Gamma函数在 x 绝对值的自然对数。

3.2 新版功能.

9.2.7. 常量

math.pi

The mathematical constant π = 3.141592…, to available precision.

math.e

The mathematical constant e = 2.718281…, to available precision.

math.tau

The mathematical constant τ = 6.283185…, to available precision. Tau is a circle constant equal to 2π, the ratio of a circle’s circumference to its radius. To learn more about Tau, check out Vi Hart’s video Pi is (still) Wrong [https://www.youtube.com/watch?v=jG7vhMMXagQ], and start celebrating Tau day [http://tauday.com/] by eating twice as much pie!

3.6 新版功能.

math.inf

浮点正无穷大。 （对于负无穷大，使用 -math.inf 。）相当于``float(‘inf’)`` 的输出。

3.5 新版功能.

math.nan

浮点“非数字”（NaN）值。 相当于 float('nan') 的输出。

3.5 新版功能.

CPython implementation detail: math 模块主要包含围绕平台C数学库函数的简单包装器。特殊情况下的行为在适当情况下遵循C99标准的附录F。当前的实现将引发 ValueError 用于无效操作，如 sqrt(-1.0) 或 log(0.0) （其中C99附件F建议发出无效操作信号或被零除）， 和 OverflowError 用于溢出的结果（例如， exp(1000.0) ）。除非一个或多个输入参数是NaN，否则不会从上述任何函数返回NaN；在这种情况下，大多数函数将返回一个NaN，但是（再次遵循C99附件F）这个规则有一些例外，例如 pow(float('nan'), 0.0) 或 hypot(float('nan'), float('inf')) 。

请注意，Python不会将显式NaN与静默NaN区分开来，并且显式NaN的行为仍未明确。典型的行为是将所有NaN视为静默的。

参见

cmath 模块

这里很多函数的复数版本。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

9. 数字和数学模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

9. 数字和数学模块 »

|

9.3. cmath ——关于复数的数学函数

This module is always available. It provides access to mathematical functions for complex numbers. The functions in this module accept integers, floating-point numbers or complex numbers as arguments. They will also accept any Python object that has either a __complex__() or a __float__() method: these methods are used to convert the object to a complex or floating-point number, respectively, and the function is then applied to the result of the conversion.

注解

在具有对于有符号零的硬件和系统级支持的平台上，涉及支割线的函数在支割线的 两侧 都是连续的：零的符号可用来区别支割线的一侧和另一侧。 在不支持有符号零的平台上，连续性的规则见下文。

9.3.1. 到极坐标和从极坐标的转换

使用 矩形坐标 或 笛卡尔坐标 在内部存储 Python 复数 z。 这完全取决于它的 实部 z.real 和 虚部 z.imag。 换句话说:

z == z.real + z.imag*1j

极坐标 提供了另一种复数的表示方法。在极坐标中，一个复数 z 由模量 r 和相位角 phi 来定义。模量 r 是从 z 到坐标原点的距离，而相位角 phi 是以弧度为单位的，逆时针的，从正X轴到连接原点和 z 的线段间夹角的角度。

下面的函数可用于原生直角坐标与极坐标的相互转换。

cmath.phase(x)

Return the phase of x (also known as the argument of x), as a float. phase(x) is equivalent to math.atan2(x.imag, x.real). The result lies in the range [-π, π], and the branch cut for this operation lies along the negative real axis, continuous from above. On systems with support for signed zeros (which includes most systems in current use), this means that the sign of the result is the same as the sign of x.imag, even when x.imag is zero:

>>> phase(complex(-1.0, 0.0))
3.141592653589793
>>> phase(complex(-1.0, -0.0))
-3.141592653589793

注解

一个复数 x 的模数（绝对值）可以通过内置函数 abs() 计算。没有单独的 cmath 模块函数用于这个操作。

cmath.polar(x)

在极坐标中返回 x 的表达方式。返回一个数对 (r, phi)，r 是 x 的模数，phi 是 x 的相位角。 polar(x) 相当于 (abs(x), phase(x))。

cmath.rect(r, phi)

通过极坐标的 r 和 phi 返回复数 x。相当于 r * (math.cos(phi) + math.sin(phi)*1j)。

9.3.2. 幂函数与对数函数

cmath.exp(x)

Return the exponential value e**x.

cmath.log(x[, base])

返回给定 base 的 x 的对数。如果没有给定 base，返回 x 的自然对数。 从 0 到 -∞ 存在一条支割线，沿负实轴之上连续。

cmath.log10(x)

返回底数为 10 的 x 的对数。它具有与 log() 相同的支割线。

cmath.sqrt(x)

返回 x 的平方根。 它具有与 log() 相同的支割线。

9.3.3. 三角函数

cmath.acos(x)

返回 x 的反余弦。这里有两条支割线：一条沿着实轴从 1 向右延伸到 ∞，从下面连续延伸。另外一条沿着实轴从 -1 向左延伸到 -∞，从上面连续延伸。

cmath.asin(x)

返回 x 的反正弦。它与 acos() 有相同的支割线。

cmath.atan(x)

返回 x 的反正切。它具有两条支割线：一条沿着虚轴从 1j 延伸到 ∞j，向右持续延伸。另一条是沿着虚轴从 -1j 延伸到 -∞j ，向左持续延伸。

cmath.cos(x)

返回 x 的余弦。

cmath.sin(x)

返回 x 的正弦。

cmath.tan(x)

返回 x 的正切。

9.3.4. 双曲函数

cmath.acosh(x)

返回 x 的反双曲余弦。它有一条支割线沿着实轴从 1 到 -∞ 向左延伸，从上方持续延伸。

cmath.asinh(x)

返回 x 的反双曲正弦。它有两条支割线：一条沿着虚轴从 1j 向右持续延伸到 ∞j。另一条是沿着虚轴从 -1j 向左持续延伸到 -∞j。

cmath.atanh(x)

返回 x 的反双曲正切。它有两条支割线：一条是沿着实轴从 1 延展到 ∞，从下面持续延展。另一条是沿着实轴从 -1 延展到 -∞，从上面持续延展。

cmath.cosh(x)

返回 x 的双曲余弦值。

cmath.sinh(x)

返回 x 的双曲正弦值。

cmath.tanh(x)

返回 x 的双曲正切值。

9.3.5. 分类函数

cmath.isfinite(x)

如果 x 的实部和虚部都是有限的，则返回 True，否则返回 False。

3.2 新版功能.

cmath.isinf(x)

如果 x 的实部或者虚部是无穷大的，则返回 True，否则返回 False。

cmath.isnan(x)

如果 x 的实部或者虚部是 NaN，则返回 True ，否则返回 False。

cmath.isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)

若 a 和 b 的值比较接近则返回 True，否则返回 False。

根据给定的绝对和相对容差确定两个值是否被认为是接近的。

rel_tol 是相对容差 —— 它是 a 和 b 之间允许的最大差值，相对于 a 或 b 的较大绝对值。例如，要设置5％的容差，请传递 rel_tol=0.05 。默认容差为 1e-09，确保两个值在大约9位十进制数字内相同。 rel_tol 必须大于零。

abs_tol 是最小绝对容差 —— 对于接近零的比较很有用。 abs_tol 必须至少为零。

如果没有错误发生，结果将是： abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol) 。

IEEE 754特殊值 NaN ， inf 和 -inf 将根据IEEE规则处理。具体来说， NaN 不被认为接近任何其他值，包括 NaN 。 inf 和 -inf 只被认为接近自己。

3.5 新版功能.

参见

PEP 485 [https://www.python.org/dev/peps/pep-0485] —— 用于测试近似相等的函数

9.3.6. 常量

cmath.pi

数学常数 π ，作为一个浮点数。

cmath.e

数学常数 e ，作为一个浮点数。

cmath.tau

数学常数 τ ，作为一个浮点数。

3.6 新版功能.

cmath.inf

浮点正无穷大。相当于 float('inf')。

3.6 新版功能.

cmath.infj

具有零实部和正无穷虚部的复数。相当于 complex(0.0, float('inf'))。

3.6 新版功能.

cmath.nan

浮点“非数字”（NaN）值。相当于 float('nan')。

3.6 新版功能.

cmath.nanj

具有零实部和 NaN 虚部的复数。相当于 complex(0.0, float('nan'))。

3.6 新版功能.

请注意，函数的选择与模块 math 中的函数选择相似，但不完全相同。 拥有两个模块的原因是因为有些用户对复数不感兴趣，甚至根本不知道它们是什么。它们宁愿 math.sqrt(-1) 引发异常，也不想返回一个复数。 另请注意，被 cmath 定义的函数始终会返回一个复数，尽管答案可以表示为一个实数（在这种情况下，复数的虚数部分为零）。

关于支割线的注释：它们是沿着给定函数无法连续的曲线。它们是许多复变函数的必要特征。 假设您需要使用复变函数进行计算，您将会了解支割线的概念。 请参阅几乎所有关于复变函数的（不太基本）的书来获得启发。 对于如何正确地基于数值目的来选择支割线的相关信息，一个良好的参考如下：

参见

Kahan, W: Branch cuts for complex elementary functions; or, Much ado about nothing’s sign bit. In Iserles, A., and Powell, M. (eds.), The state of the art in numerical analysis. Clarendon Press (1987) pp165–211.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

9. 数字和数学模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

9. 数字和数学模块 »

|

9.4. decimal — 十进制定点和浮点运算

源码： Lib/decimal.py [https://github.com/python/cpython/tree/3.6/Lib/decimal.py]

decimal 模块为快速正确舍入的十进制浮点运算提供支持。 它提供了 float 数据类型以外的几个优点：

Decimal 类型的“设计是基于考虑人类习惯的浮点数模型，并且因此具有以下最高指导原则 —— 计算机必须提供与人们在学校所学习的算术相一致的算术。” —— 摘自 decimal 算术规范描述。

Decimal 数字的表示是精确的。 相比之下，1.1 和 2.2 这样则是不精确的二进制浮点数表示。 最终用户通常不希望 1.1 + 2.2 的结果会如采用二进制浮点数时那样显示为 3.3000000000000003。

精确性会延续到算术类操作中。 对于 decimal 浮点数，0.1 + 0.1 + 0.1 - 0.3 会精确地等于零。 而对于二进制浮点数，结果则为 5.5511151231257827e-017 。 虽然接近于零，但其中的误差将妨碍可靠的相等性检验，并且误差还会不断累积。 因此，decimal 更适合具有严格相等不变性要求的会计类应用。

decimal 模块包含了有效位的概念，使得 1.30 + 1.20 是 2.50 。 保留尾随零以表示有效位。 这是货币类应用的习惯表示法。 对于乘法，“教科书”方式使用被乘数中的所有数位。 例如， 1.3 * 1.2 给出 1.56 而 1.30 * 1.20 给出 1.5600 。

与基于硬件的二进制浮点数不同，decimal 模块具有用户可更改的精度（默认为28位），可以与给定问题所需的一样大：

>>> from decimal import *
>>> getcontext().prec = 6
>>> Decimal(1) / Decimal(7)
Decimal('0.142857')
>>> getcontext().prec = 28
>>> Decimal(1) / Decimal(7)
Decimal('0.1428571428571428571428571429')

二进制和 decimal 浮点数都是根据已发布的标准实现的。 虽然内置浮点类型只公开其功能的一小部分，但 decimal 模块公开了标准的所有必需部分。 在需要时，程序员可以完全控制舍入和信号处理。 这包括通过使用异常来阻止任何不精确操作来强制执行精确算术的选项。

decimal 模块旨在支持“无偏差，精确无舍入的十进制算术（有时称为定点数算术）和有舍入的浮点数算术”。 —— 摘自 decimal 算术规范说明。

该模块的设计以三个概念为中心：decimal 数值，算术上下文和信号。

decimal 数值是不可变对象。 它由符号，系数和指数位组成。 为了保持有效位，系数位不会截去末尾零。 decimal 数值也包括特殊值例如 Infinity ，-Infinity 和 NaN 。 该标准还区分 -0 和 +0 。

算术的上下文是指定精度、舍入规则、指数限制、指示操作结果的标志以及确定符号是否被视为异常的陷阱启用器的环境。 舍入选项包括 ROUND_CEILING 、 ROUND_DOWN 、 ROUND_FLOOR 、 ROUND_HALF_DOWN, ROUND_HALF_EVEN 、 ROUND_HALF_UP 、 ROUND_UP 以及 ROUND_05UP.

信号是在计算过程中出现的异常条件组。 根据应用程序的需要，信号可能会被忽略，被视为信息，或被视为异常。 十进制模块中的信号有：Clamped 、 InvalidOperation 、 DivisionByZero 、 Inexact 、 Rounded 、 Subnormal 、 Overflow 、 Underflow 以及 FloatOperation 。

对于每个信号，都有一个标志和一个陷阱启动器。 遇到信号时，其标志设置为 1 ，然后，如果陷阱启用器设置为 1 ，则引发异常。 标志是粘性的，因此用户需要在监控计算之前重置它们。

参见

IBM 的通用十进制算术规范描述 The General Decimal Arithmetic Specification [http://speleotrove.com/decimal/decarith.html]。

9.4.1. 快速入门教程

通常使用 decimal 的方式是先导入该模块，通过 getcontext() 查看当前上下文，并在必要时为精度、舍入或启用的陷阱设置新值:

>>> from decimal import *
>>> getcontext()
Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999,
        capitals=1, clamp=0, flags=[], traps=[Overflow, DivisionByZero,
        InvalidOperation])

>>> getcontext().prec = 7       # Set a new precision

可以基于整数、字符串、浮点数或元组构造 Decimal 实例。 基于整数或浮点数构造将执行该整数或浮点值的精确转换。 Decimal 数字包括特殊值例如 NaN 表示“非数字”，正的和负的 Infinity 和 -0

>>> getcontext().prec = 28
>>> Decimal(10)
Decimal('10')
>>> Decimal('3.14')
Decimal('3.14')
>>> Decimal(3.14)
Decimal('3.140000000000000124344978758017532527446746826171875')
>>> Decimal((0, (3, 1, 4), -2))
Decimal('3.14')
>>> Decimal(str(2.0 ** 0.5))
Decimal('1.4142135623730951')
>>> Decimal(2) ** Decimal('0.5')
Decimal('1.414213562373095048801688724')
>>> Decimal('NaN')
Decimal('NaN')
>>> Decimal('-Infinity')
Decimal('-Infinity')

如果 FloatOperation 信号被捕获，构造函数中的小数和浮点数的意外混合或排序比较会引发异常

>>> c = getcontext()
>>> c.traps[FloatOperation] = True
>>> Decimal(3.14)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
decimal.FloatOperation: [<class 'decimal.FloatOperation'>]
>>> Decimal('3.5') < 3.7
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
decimal.FloatOperation: [<class 'decimal.FloatOperation'>]
>>> Decimal('3.5') == 3.5
True

3.3 新版功能.

新 Decimal 的重要性仅由输入的位数决定。 上下文精度和舍入仅在算术运算期间发挥作用。

>>> getcontext().prec = 6
>>> Decimal('3.0')
Decimal('3.0')
>>> Decimal('3.1415926535')
Decimal('3.1415926535')
>>> Decimal('3.1415926535') + Decimal('2.7182818285')
Decimal('5.85987')
>>> getcontext().rounding = ROUND_UP
>>> Decimal('3.1415926535') + Decimal('2.7182818285')
Decimal('5.85988')

如果超出了 C 版本的内部限制，则构造一个 decimal 将引发 InvalidOperation

>>> Decimal("1e9999999999999999999")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]

在 3.3 版更改.

Decimal 数字能很好地与 Python 的其余部分交互。 以下是一个小小的 decimal 浮点数飞行马戏团：

>>> data = list(map(Decimal, '1.34 1.87 3.45 2.35 1.00 0.03 9.25'.split()))
>>> max(data)
Decimal('9.25')
>>> min(data)
Decimal('0.03')
>>> sorted(data)
[Decimal('0.03'), Decimal('1.00'), Decimal('1.34'), Decimal('1.87'),
 Decimal('2.35'), Decimal('3.45'), Decimal('9.25')]
>>> sum(data)
Decimal('19.29')
>>> a,b,c = data[:3]
>>> str(a)
'1.34'
>>> float(a)
1.34
>>> round(a, 1)
Decimal('1.3')
>>> int(a)
1
>>> a * 5
Decimal('6.70')
>>> a * b
Decimal('2.5058')
>>> c % a
Decimal('0.77')

Decimal 也可以使用一些数学函数：

>>> getcontext().prec = 28
>>> Decimal(2).sqrt()
Decimal('1.414213562373095048801688724')
>>> Decimal(1).exp()
Decimal('2.718281828459045235360287471')
>>> Decimal('10').ln()
Decimal('2.302585092994045684017991455')
>>> Decimal('10').log10()
Decimal('1')

quantize() 方法将数字四舍五入为固定指数。 此方法对于将结果舍入到固定的位置的货币应用程序非常有用：

>>> Decimal('7.325').quantize(Decimal('.01'), rounding=ROUND_DOWN)
Decimal('7.32')
>>> Decimal('7.325').quantize(Decimal('1.'), rounding=ROUND_UP)
Decimal('8')

如上所示，getcontext() 函数访问当前上下文并允许更改设置。 这种方法满足大多数应用程序的需求。

对于更高级的工作，使用 Context() 构造函数创建备用上下文可能很有用。 要使用备用活动，请使用 setcontext() 函数。

根据标准，decimal 模块提供了两个现成的标准上下文 BasicContext 和 ExtendedContext 。 前者对调试特别有用，因为许多陷阱都已启用：

>>> myothercontext = Context(prec=60, rounding=ROUND_HALF_DOWN)
>>> setcontext(myothercontext)
>>> Decimal(1) / Decimal(7)
Decimal('0.142857142857142857142857142857142857142857142857142857142857')

>>> ExtendedContext
Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999,
        capitals=1, clamp=0, flags=[], traps=[])
>>> setcontext(ExtendedContext)
>>> Decimal(1) / Decimal(7)
Decimal('0.142857143')
>>> Decimal(42) / Decimal(0)
Decimal('Infinity')

>>> setcontext(BasicContext)
>>> Decimal(42) / Decimal(0)
Traceback (most recent call last):
  File "<pyshell#143>", line 1, in -toplevel-
    Decimal(42) / Decimal(0)
DivisionByZero: x / 0

上下文还具有用于监视计算期间遇到的异常情况的信号标志。 标志保持设置直到明确清除，因此最好通过使用 clear_flags() 方法清除每组受监控计算之前的标志。:

>>> setcontext(ExtendedContext)
>>> getcontext().clear_flags()
>>> Decimal(355) / Decimal(113)
Decimal('3.14159292')
>>> getcontext()
Context(prec=9, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999,
        capitals=1, clamp=0, flags=[Inexact, Rounded], traps=[])

flags 条目显示对 Pi 的有理逼近被舍入（超出上下文精度的数字被抛弃）并且结果是不精确的（一些丢弃的数字不为零）。

使用上下文的 traps 字段中的字典设置单个陷阱：

>>> setcontext(ExtendedContext)
>>> Decimal(1) / Decimal(0)
Decimal('Infinity')
>>> getcontext().traps[DivisionByZero] = 1
>>> Decimal(1) / Decimal(0)
Traceback (most recent call last):
  File "<pyshell#112>", line 1, in -toplevel-
    Decimal(1) / Decimal(0)
DivisionByZero: x / 0

大多数程序仅在程序开始时调整当前上下文一次。 并且，在许多应用程序中，数据在循环内单个强制转换为 Decimal 。 通过创建上下文集和小数，程序的大部分操作数据与其他 Python 数字类型没有区别。

9.4.2. Decimal 对象

class decimal.Decimal(value="0", context=None)

根据 value 构造一个新的 Decimal 对象。

value 可以是整数，字符串，元组，float ，或另一个 Decimal 对象。 如果没有给出 value，则返回 Decimal('0')。 如果 value 是一个字符串，它应该在前导和尾随空格字符以及下划线被删除之后符合十进制数字字符串语法:

sign           ::=  '+' | '-'
digit          ::=  '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
indicator      ::=  'e' | 'E'
digits         ::=  digit [digit]...
decimal-part   ::=  digits '.' [digits] | ['.'] digits
exponent-part  ::=  indicator [sign] digits
infinity       ::=  'Infinity' | 'Inf'
nan            ::=  'NaN' [digits] | 'sNaN' [digits]
numeric-value  ::=  decimal-part [exponent-part] | infinity
numeric-string ::=  [sign] numeric-value | [sign] nan

当上面出现 digit 时也允许其他十进制数码。 其中包括来自各种其他语言系统的十进制数码（例如阿拉伯-印地语和天城文的数码）以及全宽数码 '\uff10' 到 '\uff19'。

如果 value 是一个 tuple ，它应该有三个组件，一个符号（ 0 表示正数或 1 表示负数），一个数字的 tuple 和整数指数。 例如， Decimal((0, (1, 4, 1, 4), -3)) 返回 Decimal('1.414')。

如果 value 是 float ，则二进制浮点值无损地转换为其精确的十进制等效值。 此转换通常需要53位或更多位数的精度。 例如， Decimal(float('1.1')) 转换为``Decimal(‘1.100000000000000088817841970012523233890533447265625’)``。

context 精度不会影响存储的位数。 这完全由 value 中的位数决定。 例如，Decimal('3.00000') 记录所有五个零，即使上下文精度只有三。

context 参数的目的是确定 value 是格式错误的字符串时该怎么做。 如果上下文陷阱 InvalidOperation，则引发异常；否则，构造函数返回一个新的 Decimal，其值为 NaN。

构造完成后， Decimal 对象是不可变的。

在 3.2 版更改: 现在允许构造函数的参数为 float 实例。

在 3.3 版更改: float 参数在设置 FloatOperation 陷阱时引发异常。 默认情况下，陷阱已关闭。

在 3.6 版更改: 允许下划线进行分组，就像代码中的整数和浮点文字一样。

十进制浮点对象与其他内置数值类型共享许多属性，例如 float 和 int 。 所有常用的数学运算和特殊方法都适用。 同样，十进制对象可以复制、pickle、打印、用作字典键、用作集合元素、比较、排序和强制转换为另一种类型（例如 float 或 int ）。

算术对十进制对象和算术对整数和浮点数有一些小的差别。 当余数运算符 % 应用于Decimal对象时，结果的符号是 被除数 的符号，而不是除数的符号:

>>> (-7) % 4
1
>>> Decimal(-7) % Decimal(4)
Decimal('-3')

整数除法运算符 // 的行为类似，返回真商的整数部分（截断为零）而不是它的向下取整，以便保留通常的标识 x == (x // y) * y + x % y:

>>> -7 // 4
-2
>>> Decimal(-7) // Decimal(4)
Decimal('-1')

% 和 // 运算符实现了 remainder 和 divide-integer 操作（分别），如规范中所述。

十进制对象通常不能与浮点数或 fractions.Fraction 实例在算术运算中结合使用：例如,尝试将 Decimal 加到 float ，将引发 TypeError。 但是，可以使用 Python 的比较运算符来比较 Decimal 实例 x 和另一个数字 y 。 这样可以避免在对不同类型的数字进行相等比较时混淆结果。

在 3.2 版更改: 现在完全支持 Decimal 实例和其他数字类型之间的混合类型比较。

除了标准的数字属性，十进制浮点对象还有许多专门的方法：

adjusted()

在移出系数最右边的数字之后返回调整后的指数，直到只剩下前导数字：Decimal('321e+5').adjusted() 返回 7 。 用于确定最高有效位相对于小数点的位置。

as_integer_ratio()

返回一对 (n, d) 整数，表示给定的 Decimal 实例作为分数、最简形式项并带有正分母:

>>> Decimal('-3.14').as_integer_ratio()
(-157, 50)

转换是精确的。 在 Infinity 上引发 OverflowError ，在 NaN 上引起 ValueError 。

3.6 新版功能.

as_tuple()

返回一个 named tuple 表示的数字： DecimalTuple(sign, digits, exponent)。

canonical()

返回参数的规范编码。 目前，一个 Decimal 实例的编码始终是规范的，因此该操作返回其参数不变。

compare(other, context=None)

比较两个 Decimal 实例的值。 compare() 返回一个 Decimal 实例，如果任一操作数是 NaN ，那么结果是 NaN

a or b is a NaN  ==> Decimal('NaN')
a < b            ==> Decimal('-1')
a == b           ==> Decimal('0')
a > b            ==> Decimal('1')

compare_signal(other, context=None)

除了所有 NaN 信号之外，此操作与 compare() 方法相同。 也就是说，如果两个操作数都不是信令NaN，那么任何静默的 NaN 操作数都被视为信令NaN。

compare_total(other, context=None)

使用它们的抽象表示而不是它们的数值来比较两个操作数。 类似于 compare() 方法，但结果给出了一个总排序 Decimal 实例。 两个 Decimal 实例具有相同的数值但不同的表示形式在此排序中比较不相等：

>>> Decimal('12.0').compare_total(Decimal('12'))
Decimal('-1')

静默和发出信号的 NaN 也包括在总排序中。 这个函数的结果是 Decimal('0') 如果两个操作数具有相同的表示，或是 Decimal('-1') 如果第一个操作数的总顺序低于第二个操作数，或是 Decimal('1') 如果第一个操作数在总顺序中高于第二个操作数。 有关总排序的详细信息，请参阅规范。

此操作不受上下文影响且静默：不更改任何标志且不执行舍入。 作为例外，如果无法准确转换第二个操作数，则C版本可能会引发InvalidOperation。

compare_total_mag(other, context=None)

比较两个操作数使用它们的抽象表示而不是它们的值，如 compare_total()，但忽略每个操作数的符号。 x.compare_total_mag(y) 相当于 x.copy_abs().compare_total(y.copy_abs())。

此操作不受上下文影响且静默：不更改任何标志且不执行舍入。 作为例外，如果无法准确转换第二个操作数，则C版本可能会引发InvalidOperation。

conjugate()

只返回self，这种方法只符合 Decimal 规范。

copy_abs()

返回参数的绝对值。 此操作不受上下文影响并且是静默的：没有更改标志且不执行舍入。

copy_negate()

回到参数的否定。 此操作不受上下文影响并且是静默的：没有标志更改且不执行舍入。

copy_sign(other, context=None)

返回第一个操作数的副本，其符号设置为与第二个操作数的符号相同。 例如：

>>> Decimal('2.3').copy_sign(Decimal('-1.5'))
Decimal('-2.3')

此操作不受上下文影响且静默：不更改任何标志且不执行舍入。 作为例外，如果无法准确转换第二个操作数，则C版本可能会引发InvalidOperation。

exp(context=None)

返回给定数字的（自然）指数函数``e**x``的值。结果使用 ROUND_HALF_EVEN 舍入模式正确舍入。

>>> Decimal(1).exp()
Decimal('2.718281828459045235360287471')
>>> Decimal(321).exp()
Decimal('2.561702493119680037517373933E+139')

from_float(f)

将浮点数转换为十进制数的类方法。

注意， Decimal.from_float(0.1) 与 Decimal(‘0.1’) 不同。 由于 0.1 在二进制浮点中不能精确表示，因此该值存储为最接近的可表示值，即 0x1.999999999999ap-4 。 十进制的等效值是`0.1000000000000000055511151231257827021181583404541015625`。

注解

从 Python 3.2 开始，Decimal 实例也可以直接从 float 构造。

>>> Decimal.from_float(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
>>> Decimal.from_float(float('nan'))
Decimal('NaN')
>>> Decimal.from_float(float('inf'))
Decimal('Infinity')
>>> Decimal.from_float(float('-inf'))
Decimal('-Infinity')

3.1 新版功能.

fma(other, third, context=None)

混合乘法加法。 返回 self*other+third ，中间乘积 self*other 没有四舍五入。

>>> Decimal(2).fma(3, 5)
Decimal('11')

is_canonical()

如果参数是规范的，则为返回 True，否则为 False 。 目前，Decimal 实例总是规范的，所以这个操作总是返回 True 。

is_finite()

如果参数是一个有限的数，则返回为 True ；如果参数为无穷大或 NaN ，则返回为 False。

is_infinite()

如果参数为正负无穷大，则返回为 True ，否则为 False 。

is_nan()

如果参数为 NaN （无论是否静默），则返回为 True ，否则为 False 。

is_normal(context=None)

如果参数是一个有限正规数，返回 True，如果参数是0、次正规数、无穷大或是NaN，返回 False。

is_qnan()

如果参数为静默 NaN，返回 True，否则返回 False。

is_signed()

如果参数带有负号，则返回为 True，否则返回 False。注意，0 和 NaN 都可带有符号。

is_snan()

如果参数为显式 NaN，则返回 True，否则返回 False。

is_subnormal(context=None)

如果参数为次正规数，则返回 True，否则返回 False。

is_zero()

如果参数是0（正负皆可），则返回 True，否则返回 False。

ln(context=None)

返回操作数的自然对数（以 e 为底）。结果是使用 ROUND_HALF_EVEN 舍入模式正确四舍五入的。

log10(context=None)

返回操作数的以十为底的对数。结果是使用 ROUND_HALF_EVEN 舍入模式正确四舍五入的。

logb(context=None)

对于一个非零数，返回其运算数的调整后指数作为一个 Decimal 实例。 如果运算数为零将返回 Decimal('-Infinity') 并且产生 the DivisionByZero 标志。如果运算数是无限大则返回 Decimal('Infinity') 。

logical_and(other, context=None)

logical_and() 是需要两个 逻辑运算数 的逻辑运算（参考 逻辑操作数 ）。结果是按位输出的两运算数的 “和”。

logical_invert(context=None)

logical_invert() 是一个逻辑运算。 结果是按位的倒转的运算数。

logical_or(other, context=None)

logical_or() 是需要两个 logical operands 的逻辑运算（请参阅 逻辑操作数 ）。结果是两个运算数的按位的 or 。

logical_xor(other, context=None)

logical_xor() 是需要两个 逻辑运算数 的逻辑运算（参考 逻辑操作数 ）。结果是按位输出的两运算数的异或运算。

max(other, context=None)

像 max(self, other) 一样，除了在返回之前应用上下文舍入规则并且用信号通知或忽略 NaN 值（取决于上下文以及它们是发信号还是安静）。

max_mag(other, context=None)

与 max() 方法相似，但是操作数使用绝对值进行比较。

min(other, context=None)

像 min(self, other) 一样，除了在返回之前应用上下文舍入规则并且用信号通知或忽略 NaN 值（取决于上下文以及它们是发信号还是安静）。

min_mag(other, context=None)

与 min() 方法相似，但是操作数使用绝对值进行比较。

next_minus(context=None)

返回小于给定操作数的上下文中可表示的最大数字（或者当前线程的上下文中的可表示的最大数字如果没有给定上下文）。

next_plus(context=None)

返回大于给定操作数的上下文中可表示的最小数字（或者当前线程的上下文中的可表示的最小数字如果没有给定上下文）。

next_toward(other, context=None)

如果两运算数不相等，返回在第二个操作数的方向上最接近第一个操作数的数。如果两操作数数值上相等，返回将符号设置为与第二个运算数相同的第一个运算数的拷贝。

normalize(context=None)

通过去除尾随的零并将所有结果等于 Decimal('0') 的转化为 Decimal('0e0') 来标准化数字。用于为等效类的属性生成规范值。比如， Decimal('32.100') 和 Decimal('0.321000e+2') 都被标准化为相同的值 Decimal('32.1')。

number_class(context=None)

返回一个字符串描述运算数的 class 。返回值是以下十个字符串中的一个。

"-Infinity" ，指示操作数为负无穷大。

"-Normal" ，指示该操作数是负正常数字。

"-Subnormal" ，指示该操作数是负的次正规数。

"-Zero" ，指示该操作数是负零。

"-Zero" ，指示该操作数是正零。

"+Subnormal" ，指示该操作数是正的次正规数。

"+Normal" ，指示该操作数是正的正规数。

"+Infinity" ，指示该运算数是正无穷。

"NaN" ，指示该运算数是沉寂的 NaN （非数字）。

"sNaN" ，指示该运算数是信号 NaN 。

quantize(exp, rounding=None, context=None)

返回的值等于四舍五入的第一个运算数并且具有第二个操作数的指数。

>>> Decimal('1.41421356').quantize(Decimal('1.000'))
Decimal('1.414')

与其他运算不同，如果量化运算后的系数长度大于精度，那么会发出一个 InvalidOperation 信号。这保证了除非有一个错误情况，量化指数恒等于右手运算数的指数。

与其他运算不同，量化永不信号下溢，即使结果不正常且不精确。

如果第二个运算数的指数大于第一个运算数的指数那或许需要四舍五入。在这种情况下，舍入模式由给定 rounding 参数决定，其余的由给定 context 参数决定；如果参数都未给定，使用当前线程上下文的舍入模式。

每当结果的指数大于 Emax 或小于 Etiny 就会返回错误。

radix()

返回 Decimal(10)，即 Decimal 类进行所有算术运算所用的数制（基数）。 这是为保持与规范描述的兼容性而加入的。

remainder_near(other, context=None)

返回 self 除以 other 的余数。 这与 self % other 的区别在于所选择的余数要使其绝对值最小化。 更准确地说，返回值为 self - n * other 其中 n 是最接近 self / other 的实际值的整数，并且如果两个整数与实际值的差相等则会选择其中的偶数。

如果结果为零则其符号将为 self 的符号。

>>> Decimal(18).remainder_near(Decimal(10))
Decimal('-2')
>>> Decimal(25).remainder_near(Decimal(10))
Decimal('5')
>>> Decimal(35).remainder_near(Decimal(10))
Decimal('-5')

rotate(other, context=None)

返回对第一个操作数的数码按第二个操作数所指定的数量进行轮转的结果。 第二个操作数必须为 -precision 至 precision 精度范围内的整数。 第二个操作数的绝对值给出要轮转的位数。 如果第二个操作数为正值则向左轮转；否则向右轮转。 如有必要第一个操作数的系数会在左侧填充零以达到 precision 所指定的长度。 第一个操作数的符号和指数保持不变。

same_quantum(other, context=None)

检测自身与 other 是否具有相同的指数或是否均为 NaN。

此操作不受上下文影响且静默：不更改任何标志且不执行舍入。 作为例外，如果无法准确转换第二个操作数，则C版本可能会引发InvalidOperation。

scaleb(other, context=None)

返回第一个操作数使用第二个操作数对指数进行调整的结果。 等价于返回第一个操作数乘以 10**other 的结果。 第二个操作数必须为整数。

shift(other, context=None)

返回第一个操作数的数码按第二个操作数所指定的数量进行移位的结果。 第二个操作数必须为 -precision 至 precision 范围内的整数。 第二个操作数的绝对值给出要移动的位数。 如果第二个操作数为正值则向左移位；否则向右移位。 移入系数的数码为零。 第一个操作数的符号和指数保持不变。

sqrt(context=None)

返回参数的平方根精确到完整精度。

to_eng_string(context=None)

转换为字符串，如果需要指数则会使用工程标注法。

工程标注法的指数是 3 的倍数。 这会在十进制位的左边保留至多 3 个数码，并可能要求添加一至两个末尾零。

例如，此方法会将 Decimal('123E+1') 转换为 Decimal('1.23E+3')。

to_integral(rounding=None, context=None)

与 to_integral_value() 方法相同。 保留 to_integral 名称是为了与旧版本兼容。

to_integral_exact(rounding=None, context=None)

舍入到最接近的整数，发出信号 Inexact 或者如果发生舍入则相应地发出信号 Rounded。 如果给出 rounding 形参则由其确定舍入模式，否则由给定的 context 来确定。 如果没有给定任何形参则会使用当前上下文的舍入模式。

to_integral_value(rounding=None, context=None)

舍入到最接近的整数而不发出 Inexact 或 Rounded 信号。 如果给出 rounding 则会应用其所指定的舍入模式；否则使用所提供的 context 或当前上下文的舍入方法。

9.4.2.1. 逻辑操作数

logical_and(), logical_invert(), logical_or() 和 logical_xor() 方法期望其参数为 逻辑操作数。 逻辑操作数 是指数位与符号位均为零的 Decimal 实例，并且其数字位均为 0 或 1。

9.4.3. Context 对象

上下文是算术运算所在的环境。 它们管理精度、设置舍入规则、确定将哪些信号视为异常，并限制指数的范围。

每个线程都有自己的当前上下文，可使用 getcontext() 和 setcontext() 函数来读取或修改：

decimal.getcontext()

返回活动线程的当前上下文。

decimal.setcontext(c)

将活动线程的当前上下文设为 c。

你也可以使用 with 语句和 localcontext() 函数来临时改变活动上下文。

decimal.localcontext(ctx=None)

返回一个上下文管理器，它将在进入 with 语句时将活动线程的当前上下文设为 ctx 的一个副本并在退出 with 语句时恢复之前的上下文。 如果未指定上下文，则会使用当前上下文的一个副本。

例如，以下代码会将当前 decimal 精度设为 42 位，执行一个运算，然后自动恢复之前的上下文:

from decimal import localcontext

with localcontext() as ctx:
    ctx.prec = 42   # Perform a high precision calculation
    s = calculate_something()
s = +s  # Round the final result back to the default precision

新的上下文也可使用下述的 Context 构造器来创建。 此外，模块还提供了三种预设的上下文:

class decimal.BasicContext

这是由通用十进制算术规范描述所定义的标准上下文。 精度设为九。 舍入设为 ROUND_HALF_UP。 清除所有旗标。 启用所有陷阱（视为异常），但 Inexact, Rounded 和 Subnormal 除外。

由于启用了许多陷阱，此上下文适用于进行调试。

class decimal.ExtendedContext

这是由通用十进制算术规范描述所定义的标准上下文。 精度设为九。 舍入设为 ROUND_HALF_EVEN。 清除所有旗标。 不启用任何陷阱（因此在计算期间不会引发异常）。

由于禁用了陷阱，此上下文适用于希望结果值为 NaN 或 Infinity 而不是引发异常的应用。 这允许应用在出现当其他情况下会中止程序的条件时仍能完成运行。

class decimal.DefaultContext

此上下文被 Context 构造器用作新上下文的原型。 改变一个字段（例如精度）的效果将是改变 Context 构造器所创建的新上下文的默认值。

此上下文最适用于多线程环境。 在线程开始前改变一个字段具有设置全系统默认值的效果。 不推荐在线程开始后改变字段，因为这会要求线程同步避免竞争条件。

在单线程环境中，最好完全不使用此上下文。 而是简单地电显式创建上下文，具体如下所述。

默认值为 prec=28, rounding=ROUND_HALF_EVEN，并为 Overflow, InvalidOperation 和 DivisionByZero 启用陷阱。

在已提供的三种上下文之外，还可以使用 Context 构造器创建新的上下文。

class decimal.Context(prec=None, rounding=None, Emin=None, Emax=None, capitals=None, clamp=None, flags=None, traps=None)

创建一个新上下文。 如果某个字段未指定或为 None，则从 DefaultContext 拷贝默认值。 如果 flags 字段未指定或为 None，则清空所有旗标。

prec 为一个 [1, MAX_PREC] 范围内的整数，用于设置该上下文中算术运算的精度。

rounding 选项应为 Rounding Modes 小节中列出的常量之一。

traps 和 flags 字段列出要设置的任何信号。 通常，新上下文应当只设置 traps 而让 flags 为空。

Emin 和 Emax 字段给定指数所允许的外部上限。 Emin 必须在 [MIN_EMIN, 0] 范围内，Emax 在 [0, MAX_EMAX] 范围内。

capitals 字段为 0 或 1 (默认值)。 如果设为 1，指数将附带打印大写的 E；其他情况则将使用小写的 e: Decimal('6.02e+23')。

clamp 字段为 0 (默认值) 或 1。 如果设为 1，则 Decimal 实例的指数 e 的表示范围在此上下文中将严格限制为 Emin - prec + 1 <= e <= Emax - prec + 1。 如果 clamp 为 0 则将适用较弱的条件: Decimal 实例调整后的指数最大值为 Emax。 当 clamp 为 1 时，一个较大的普通数值将在可能的情况下减小其指数并为其系统添加相应数量的零，以便符合指数值限制；这可以保持数字值但会丢失有效末尾零的信息。 例如:

>>> Context(prec=6, Emax=999, clamp=1).create_decimal('1.23e999')
Decimal('1.23000E+999')

clamp 值为 1 时即允许与在 IEEE 754 中描述的固定宽度十进制交换格式相兼容。

Context 类定义了几种通用方法以及大量直接在给定上下文中进行算术运算的方法。 此外，对于上述的每种 Decimal 方法（不包括 adjusted() 和 as_tuple() 方法）都有一个相应的 Context 方法。 例如，对于一个 Context 的实例 C 和 Decimal 的实例 x，C.exp(x) 就等价于 x.exp(context=C)。 每个 Context 方法都接受一个 Python 整数（即 int 的实例）在任何接受 Decimal 的实例的地方使用。

clear_flags()

将所有旗标重置为 0。

clear_traps()

将所有陷阱重置为零 0。

3.3 新版功能.

copy()

返回上下文的一个副本。

copy_decimal(num)

返回 Decimal 实例 num 的一个副本。

create_decimal(num)

基于 num 创建一个新 Decimal 实例但使用 self 作为上下文。 与 Decimal 构造器不同，该上下文的精度、舍入方法、旗标和陷阱会被应用于转换过程。

此方法很有用处，因为常量往往被给予高于应用所需的精度。 另一个好处在于立即执行舍入可以消除超出当前精度的数位所导致的意外效果。 在下面的示例中，使用未舍入的输入意味着在总和中添加零会改变结果：

>>> getcontext().prec = 3
>>> Decimal('3.4445') + Decimal('1.0023')
Decimal('4.45')
>>> Decimal('3.4445') + Decimal(0) + Decimal('1.0023')
Decimal('4.44')

此方法实现了 IBM 规格描述中的转换为数字操作。 如果参数为字符串，则不允许有开头或末尾的空格或下划线。

create_decimal_from_float(f)

基于浮点数 f 创建一个新的 Decimal 实例，但会使用 self 作为上下文来执行舍入。 与 Decimal.from_float() 类方法不同，上下文的精度、舍入方法、旗标和陷阱会应用到转换中。

>>> context = Context(prec=5, rounding=ROUND_DOWN)
>>> context.create_decimal_from_float(math.pi)
Decimal('3.1415')
>>> context = Context(prec=5, traps=[Inexact])
>>> context.create_decimal_from_float(math.pi)
Traceback (most recent call last):
    ...
decimal.Inexact: None

3.1 新版功能.

Etiny()

返回一个等于 Emin - prec + 1 的值即次正规化结果中的最小指数值。 当发生向下溢出时，指数会设为 Etiny。

Etop()

返回一个等于 Emax - prec + 1 的值。

使用 decimal 的通常方式是创建 Decimal 实例然后对其应用算术运算,这些运算发生在活动线程的当前上下文中。 一种替代方式则是使用上下文的方法在特定上下文中进行计算。 这些方法类似于 Decimal 类的方法，在此仅简单地重新列出。

abs(x)

返回 x 的绝对值。

add(x, y)

返回 x 与 y 的和。

canonical(x)

返回相同的 Decimal 对象 x。

compare(x, y)

对 x 与 y 进行数值比较。

compare_signal(x, y)

对两个操作数进行数值比较。

compare_total(x, y)

对两个操作数使用其抽象表示进行比较。

compare_total_mag(x, y)

对两个操作数使用其抽象表示进行比较，忽略符号。

copy_abs(x)

返回 x 的副本，符号设为 0。

copy_negate(x)

返回 x 的副本，符号取反。

copy_sign(x, y)

从 y 拷贝符号至 x。

divide(x, y)

返回 x 除以 y 的结果。

divide_int(x, y)

返回 x 除以 y 的结果，截短为整数。

divmod(x, y)

两个数字相除并返回结果的整数部分。

exp(x)

返回 e ** x。

fma(x, y, z)

返回 x 乘以 y 再加 z 的结果。

is_canonical(x)

如果 x 是规范的则返回 True；否则返回 False。

is_finite(x)

如果 x 为有限数则返回``True``；否则返回 False。

is_infinite(x)

如果 x 是无限的则返回 True；否则返回 False。

is_nan(x)

如果 x 是 qNaN 或 sNaN 则返回 True；否则返回 False。

is_normal(x)

如果 x 是正规数则返回 True；否则返回 False。

is_qnan(x)

如果 x 是静默 NaN 则返回 True；否则返回 False。

is_signed(x)

x 是负数则返回 True；否则返回 False。

is_snan(x)

如果 x 是显式 NaN 则返回 True；否则返回 False。

is_subnormal(x)

如果 x 是次标准数则返回 True；否则返回 False。

is_zero(x)

如果 x 为零则返回 True；否则返回 False。

ln(x)

返回 x 的自然对数（以 e 为底）。

log10(x)

返回 x 的以 10 为底的对数。

logb(x)

返回操作数的 MSD 等级的指数。

logical_and(x, y)

在操作数的每个数码间应用逻辑运算 and。

logical_invert(x)

反转 x 中的所有数位。

logical_or(x, y)

在操作数的每个数位间应用逻辑运算 or。

logical_xor(x, y)

在操作数的每个数位间应用逻辑运算 xor。

max(x, y)

对两个值执行数字比较并返回其中的最大值。

max_mag(x, y)

对两个值执行忽略正负号的数字比较。

min(x, y)

对两个值执行数字比较并返回其中的最小值。

min_mag(x, y)

对两个值执行忽略正负号的数字比较。

minus(x)

对应于 Python 中的单目取负运算符执行取负操作。

multiply(x, y)

返回 x 和 y 的积。

next_minus(x)

返回小于 x 的最大数字表示形式。

next_plus(x)

返回大于 x 的最小数字表示形式。

next_toward(x, y)

返回 x 趋向于 y 的最接近的数字。

normalize(x)

将 x 改写为最简形式。

number_class(x)

返回 x 的类的表示。

plus(x)

对应于 Python 中的单目前缀取正运算符执行取正操作。 此操作将应用上下文精度和舍入，因此它 不是 标识运算。

power(x, y, modulo=None)

返回 x 的 y 次方，如果给出了模数 modulo 则取其余数。

如为两个参数则计算 x**y。 如果 x 为负值则 y 必须为整数。 除非 y 为整数且结果为有限值并可在 ‘precision’ 位内精确表示否则结果将是不精确的。 上下文的舍入模式将被使用。 结果在 Python 版中总是会被正确地舍入。

在 3.3 版更改: C 模块计算 power() 时会使用已正确舍入的 exp() 和 ln() 函数。 结果是经过良好定义的，但仅限于“几乎总是正确地舍入”。

带有三个参数时，计算 (x**y) % modulo。 对于三个参数的形式，参数将会应用以下限制：

三个参数必须都是整数

y 必须是非负数

x 或 y 至少有一个不为零

modulo 必须不为零且至多有 ‘precision’ 位

来自 Context.power(x, y, modulo) 的结果值等于使用无限精度计算 (x**y) % modulo 所得到的值，但其计算过程更高效。 结果的指数为零，无论 x, y 和 modulo 的指数是多少。 结果值总是完全精确的。

quantize(x, y)

返回的值等于 x (舍入后)，并且指数为 y。

radix()

恰好返回 10，因为这是 Decimal 对象 :)

remainder(x, y)

返回整除所得到的余数。

结果的符号，如果不为零，则与原始除数的符号相同。

remainder_near(x, y)

返回 x - y * n，其中 n 为最接近 x / y 实际值的整数（如结果为 0 则其符号将与 x 的符号相同）。

rotate(x, y)

返回 x 翻转 y 次的副本。

same_quantum(x, y)

如果两个操作数具有相同的指数则返回 True。

scaleb(x, y)

返回第一个操作数对第二个值添加其指数后的结果。

shift(x, y)

返回 x 变换 y 次的副本。

sqrt(x)

非负数基于上下文精度的平方根。

subtract(x, y)

返回 x 和 y 的差。

to_eng_string(x)

转换为字符串，如果需要指数则会使用工程标注法。

工程标注法的指数是 3 的倍数。 这会在十进制位的左边保留至多 3 个数码，并可能要求添加一至两个末尾零。

to_integral_exact(x)

舍入到一个整数。

to_sci_string(x)

使用科学计数法将一个数字转换为字符串。

9.4.4. 常量

本节中的常量仅与 C 模块相关。 它们也被包含在纯 Python 版本以保持兼容性。

32位

64位

decimal.MAX_PREC

425000000

999999999999999999

decimal.MAX_EMAX

425000000

999999999999999999

decimal.MIN_EMIN

-425000000

-999999999999999999

decimal.MIN_ETINY

-849999999

-1999999999999999997

decimal.HAVE_THREADS

The default value is True. If Python is compiled without threads, the C version automatically disables the expensive thread local context machinery. In this case, the value is False.

9.4.5. 舍入模式

decimal.ROUND_CEILING

舍入方向 Infinity。

decimal.ROUND_DOWN

舍入方向为零。

decimal.ROUND_FLOOR

舍入方向为 -Infinity。

decimal.ROUND_HALF_DOWN

舍入到最接近的数，同样接近则舍入方向为零。

decimal.ROUND_HALF_EVEN

舍入到最接近的数，同样接近则舍入到最接近的偶数。

decimal.ROUND_HALF_UP

舍入到最接近的数，同样接近则舍入到零的反方向。

decimal.ROUND_UP

舍入到零的反方向。

decimal.ROUND_05UP

如果最后一位朝零的方向舍入后为 0 或 5 则舍入到零的反方向；否则舍入方向为零。

9.4.6. 信号

信号代表在计算期间引发的条件。 每个信号对应于一个上下文旗标和一个上下文陷阱启用器。

上下文旗标将在遇到特定条件时被设定。 在完成计算之后，将为了获得信息而检测旗标（例如确定计算是否精确）。 在检测旗标后，请确保在开始下一次计算之前清除所有旗标。

如果为信号设定了上下文的陷阱启用器，则条件会导致特定的 Python 异常被引发。 举例来说，如果设定了 DivisionByZero 陷阱，则当遇到此条件时就将引发 DivisionByZero 异常。

class decimal.Clamped

修改一个指数以符合表示限制。

通常，限位将在一个指数超出上下文的 Emin 和 Emax 限制时发生。 在可能的情况下，会通过给系数添加零来将指数缩减至符合限制。

class decimal.DecimalException

其他信号的基类，并且也是 ArithmeticError 的一个子类。

class decimal.DivisionByZero

非无限数被零除的信号。

可在除法、取余队法或对一个数求负数次幂时发生。 如果此信号未被陷阱捕获，则返回 Infinity 或 -Infinity 并且由对计算的输入来确定正负符号。

class decimal.Inexact

表明发生了舍入且结果是不精确的。

有非零数位在舍入期间被丢弃的信号。 舍入结果将被返回。 此信号旗标或陷阱被用于检测结果不精确的情况。

class decimal.InvalidOperation

执行了一个无效的操作。

表明请求了一个无意义的操作。 如未被陷阱捕获则返回 NaN。 可能的原因包括:

Infinity - Infinity
0 * Infinity
Infinity / Infinity
x % 0
Infinity % x
sqrt(-x) and x > 0
0 ** 0
x ** (non-integer)
x ** Infinity

class decimal.Overflow

数值的溢出。

表明在发生舍入之后的指数大于 Emax。 如果未被陷阱捕获，则结果将取决于舍入模式，或者向下舍入为最大的可表示有限数，或者向上舍入为 Infinity。 无论哪种情况，都将引发 Inexact 和 Rounded 信号。

class decimal.Rounded

发生了舍入，但或许并没有信息丢失。

一旦舍入丢弃了数位就会发出此信号；即使被丢弃的数位是零 (例如将 5.00 舍入为 5.0)。 如果未被陷阱捕获，则不经修改地返回结果。 此信号用于检测有效位数的丢弃。

class decimal.Subnormal

在舍入之前指数低于 Emin。

当操作结果是次标准数（即指数过小）时就会发出此信号。 如果未被陷阱捕获，则不经修改过返回结果。

class decimal.Underflow

数字向下溢出导致结果舍入到零。

当一个次标准数结果通过舍入转为零时就会发出此信号。 同时还将引发 Inexact 和 Subnormal 信号。

class decimal.FloatOperation

为 float 和 Decimal 的混合启用更严格的语义。

如果信号未被捕获（默认），则在 Decimal 构造器、create_decimal() 和所有比较运算中允许 float 和 Decimal 的混合。 转换和比较都是完全精确的。 发生的任何混合运算都将通过在上下文旗标中设置 FloatOperation 来静默地记录。 通过 from_float() 或 create_decimal_from_float() 进行显式转换则不会设置旗标。

在其他情况下（即信号被捕获），则只静默执行相等性比较和显式转换。 所有其他混合运算都将引发 FloatOperation。

以下表格总结了信号的层级结构:

exceptions.ArithmeticError(exceptions.Exception)
    DecimalException
        Clamped
        DivisionByZero(DecimalException, exceptions.ZeroDivisionError)
        Inexact
            Overflow(Inexact, Rounded)
            Underflow(Inexact, Rounded, Subnormal)
        InvalidOperation
        Rounded
        Subnormal
        FloatOperation(DecimalException, exceptions.TypeError)

9.4.7. 浮点数说明

9.4.7.1. 通过提升精度来缓解舍入误差

使用十进制浮点数可以消除十进制表示错误（即能够完全精确地表示 0.1 这样的数）；然而，某些运算在非零数位超出给定的精度时仍然可能导致舍入错误。

舍入错误的影响可能因接近相互抵销的加减运算被放大从而导致损失有效位。 Knuth 提供了两个指导性示例，其中出现了精度不足的浮点算术舍入，导致加法的交换律和分配律被打破：

# Examples from Seminumerical Algorithms, Section 4.2.2.
>>> from decimal import Decimal, getcontext
>>> getcontext().prec = 8

>>> u, v, w = Decimal(11111113), Decimal(-11111111), Decimal('7.51111111')
>>> (u + v) + w
Decimal('9.5111111')
>>> u + (v + w)
Decimal('10')

>>> u, v, w = Decimal(20000), Decimal(-6), Decimal('6.0000003')
>>> (u*v) + (u*w)
Decimal('0.01')
>>> u * (v+w)
Decimal('0.0060000')

decimal 模块则可以通过充分地扩展精度来避免有效位的丢失：

>>> getcontext().prec = 20
>>> u, v, w = Decimal(11111113), Decimal(-11111111), Decimal('7.51111111')
>>> (u + v) + w
Decimal('9.51111111')
>>> u + (v + w)
Decimal('9.51111111')
>>>
>>> u, v, w = Decimal(20000), Decimal(-6), Decimal('6.0000003')
>>> (u*v) + (u*w)
Decimal('0.0060000')
>>> u * (v+w)
Decimal('0.0060000')

9.4.7.2. 特殊的值

decimal 模块的数字系统提供了一些特殊的值，包括 NaN, sNaN, -Infinity, Infinity 以及两种零值 +0 和 -0。

无穷大可以使用 Decimal('Infinity') 来构建。 它们也可以在不捕获 DivisionByZero 信号捕获时通过除以零来产生。 类似地，当不捕获 Overflow 信号时，也可以通过舍入到超出最大可表示数字限制的方式产生无穷大的结果。

无穷大是有符号的（仿射）并可用于算术运算，它们会被当作极其巨大的不确定数字来处理。 例如，无穷大加一个常量结果也将为无穷大。

某些不存在有效结果的运算将会返回 NaN，或者如果捕获了 InvalidOperation 信号则会引发一个异常。 例如，0/0 会返回 NaN 表示结果“不是一个数字”。 这样的 NaN 是静默产生的，并且在产生之后参与其它计算时总是会得到 NaN 的结果。 这种行为对于偶而缺少输入的各类计算都很有用处 — 它允许在将特定结果标记为无效的同时让计算继续运行。

另一种变体形式是 sNaN，它在每次运算后会发出信号而不是保持静默。 当对于无效结果需要中断计算进行特别处理时，这是一个很有用的返回值。

Python 中比较运算符的行为在涉及 NaN 时可能会令人有点惊讶。 相等性检测在操作数中有静默型或信号型 NaN 时总是会返回 False (即使是执行 Decimal('NaN')==Decimal('NaN'))，而不等性检测总是会返回 True。 当尝试使用 <, <=, > 或 >= 运算符中的任何一个来比较两个 Decimal 值时，如果运算数中有 NaN 则将引发 InvalidOperation 信号，如果此信号未被捕获则将返回 False。 请注意通用十进制算术规范并未规定直接比较行为；这些涉及 NaN 的比较规则来自于 IEEE 854 标准 (见第 5.7 节表 3)。 要确保严格符合标准，请改用 compare() 和 compare-signal() 方法。

有符号零值可以由向下溢出的运算产生。 它们保留符号是为了让运算结果能以更高的精度传递。 由于它们的大小为零，正零和负零会被视为相等，且它们的符号具有信息。

在这两个不相同但却相等的有符号零之外，还存在几种零的不同表示形式，它们的精度不同但值也都相等。 这需要一些时间来逐渐适应。 对于习惯了标准浮点表示形式的眼睛来说，以下运算返回等于零的值并不是显而易见的：

>>> 1 / Decimal('Infinity')
Decimal('0E-1000026')

9.4.8. 使用线程

getcontext() 函数会为每个线程访问不同的 Context 对象。 具有单独线程上下文意味着线程可以修改上下文 (例如 getcontext().prec=10) 而不影响其他线程。

类似的 setcontext() 会为当前上下文的目标自动赋值。

如果在调用 setcontext() 之前调用了 getcontext()，则 getcontext() 将自动创建一个新的上下文在当前线程中使用。

新的上下文拷贝自一个名为 DefaultContext 的原型上下文。 要控制默认值以便每个线程在应用运行期间都使用相同的值，可以直接修改 DefaultContext 对象。 这应当在任何线程启动 之前 完成以使得调用 getcontext() 的线程之间不会产生竞争条件。 例如:

# Set applicationwide defaults for all threads about to be launched
DefaultContext.prec = 12
DefaultContext.rounding = ROUND_DOWN
DefaultContext.traps = ExtendedContext.traps.copy()
DefaultContext.traps[InvalidOperation] = 1
setcontext(DefaultContext)

# Afterwards, the threads can be started
t1.start()
t2.start()
t3.start()
 . . .

9.4.9. 例程

以下是一些用作工具函数的例程，它们演示了使用 Decimal 类的各种方式:

def moneyfmt(value, places=2, curr='', sep=',', dp='.',
             pos='', neg='-', trailneg=''):
    """Convert Decimal to a money formatted string.

    places:  required number of places after the decimal point
    curr:    optional currency symbol before the sign (may be blank)
    sep:     optional grouping separator (comma, period, space, or blank)
    dp:      decimal point indicator (comma or period)
             only specify as blank when places is zero
    pos:     optional sign for positive numbers: '+', space or blank
    neg:     optional sign for negative numbers: '-', '(', space or blank
    trailneg:optional trailing minus indicator:  '-', ')', space or blank

    >>> d = Decimal('-1234567.8901')
    >>> moneyfmt(d, curr='$')
    '-$1,234,567.89'
    >>> moneyfmt(d, places=0, sep='.', dp='', neg='', trailneg='-')
    '1.234.568-'
    >>> moneyfmt(d, curr='$', neg='(', trailneg=')')
    '($1,234,567.89)'
    >>> moneyfmt(Decimal(123456789), sep=' ')
    '123 456 789.00'
    >>> moneyfmt(Decimal('-0.02'), neg='<', trailneg='>')
    '<0.02>'

    """
    q = Decimal(10) ** -places      # 2 places --> '0.01'
    sign, digits, exp = value.quantize(q).as_tuple()
    result = []
    digits = list(map(str, digits))
    build, next = result.append, digits.pop
    if sign:
        build(trailneg)
    for i in range(places):
        build(next() if digits else '0')
    if places:
        build(dp)
    if not digits:
        build('0')
    i = 0
    while digits:
        build(next())
        i += 1
        if i == 3 and digits:
            i = 0
            build(sep)
    build(curr)
    build(neg if sign else pos)
    return ''.join(reversed(result))

def pi():
    """Compute Pi to the current precision.

    >>> print(pi())
    3.141592653589793238462643383

    """
    getcontext().prec += 2  # extra digits for intermediate steps
    three = Decimal(3)      # substitute "three=3.0" for regular floats
    lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24
    while s != lasts:
        lasts = s
        n, na = n+na, na+8
        d, da = d+da, da+32
        t = (t * n) / d
        s += t
    getcontext().prec -= 2
    return +s               # unary plus applies the new precision

def exp(x):
    """Return e raised to the power of x.  Result type matches input type.

    >>> print(exp(Decimal(1)))
    2.718281828459045235360287471
    >>> print(exp(Decimal(2)))
    7.389056098930650227230427461
    >>> print(exp(2.0))
    7.38905609893
    >>> print(exp(2+0j))
    (7.38905609893+0j)

    """
    getcontext().prec += 2
    i, lasts, s, fact, num = 0, 0, 1, 1, 1
    while s != lasts:
        lasts = s
        i += 1
        fact *= i
        num *= x
        s += num / fact
    getcontext().prec -= 2
    return +s

def cos(x):
    """Return the cosine of x as measured in radians.

    The Taylor series approximation works best for a small value of x.
    For larger values, first compute x = x % (2 * pi).

    >>> print(cos(Decimal('0.5')))
    0.8775825618903727161162815826
    >>> print(cos(0.5))
    0.87758256189
    >>> print(cos(0.5+0j))
    (0.87758256189+0j)

    """
    getcontext().prec += 2
    i, lasts, s, fact, num, sign = 0, 0, 1, 1, 1, 1
    while s != lasts:
        lasts = s
        i += 2
        fact *= i * (i-1)
        num *= x * x
        sign *= -1
        s += num / fact * sign
    getcontext().prec -= 2
    return +s

def sin(x):
    """Return the sine of x as measured in radians.

    The Taylor series approximation works best for a small value of x.
    For larger values, first compute x = x % (2 * pi).

    >>> print(sin(Decimal('0.5')))
    0.4794255386042030002732879352
    >>> print(sin(0.5))
    0.479425538604
    >>> print(sin(0.5+0j))
    (0.479425538604+0j)

    """
    getcontext().prec += 2
    i, lasts, s, fact, num, sign = 1, 0, x, 1, x, 1
    while s != lasts:
        lasts = s
        i += 2
        fact *= i * (i-1)
        num *= x * x
        sign *= -1
        s += num / fact * sign
    getcontext().prec -= 2
    return +s

9.4.10. Decimal FAQ

Q. 总是输入 decimal.Decimal('1234.5') 是否过于笨拙。 在使用交互解释器时有没有最小化输入量的方式？

A. 有些用户会将构造器简写为一个字母：

>>> D = decimal.Decimal
>>> D('1.23') + D('3.45')
Decimal('4.68')

Q. 在带有两个十进制位的定点数应用中，有些输入值具有许多位，需要被舍入。 另一些数则不应具有多余位，需要验证有效性。 这种情况应该用什么方法？

A. 用 quantize() 方法舍入到固定数量的十进制位。 如果设置了 Inexact 陷阱，它也适用于验证有效性：

>>> TWOPLACES = Decimal(10) ** -2       # same as Decimal('0.01')

>>> # Round to two places
>>> Decimal('3.214').quantize(TWOPLACES)
Decimal('3.21')

>>> # Validate that a number does not exceed two places
>>> Decimal('3.21').quantize(TWOPLACES, context=Context(traps=[Inexact]))
Decimal('3.21')

>>> Decimal('3.214').quantize(TWOPLACES, context=Context(traps=[Inexact]))
Traceback (most recent call last):
   ...
Inexact: None

Q. 当我使用两个有效位的输入时，我要如何在一个应用中保持有效位不变？

A. 某些运算例如与整数相加、相减和相乘将会自动保留固定的小数位数。 其他运算，例如相除和非整数相乘则将会改变小数位数，需要再加上 quantize() 处理步骤：

>>> a = Decimal('102.72')           # Initial fixed-point values
>>> b = Decimal('3.17')
>>> a + b                           # Addition preserves fixed-point
Decimal('105.89')
>>> a - b
Decimal('99.55')
>>> a * 42                          # So does integer multiplication
Decimal('4314.24')
>>> (a * b).quantize(TWOPLACES)     # Must quantize non-integer multiplication
Decimal('325.62')
>>> (b / a).quantize(TWOPLACES)     # And quantize division
Decimal('0.03')

在开发定点数应用时，更方便的做法是定义处理 quantize() 步骤的函数：

>>> def mul(x, y, fp=TWOPLACES):
...     return (x * y).quantize(fp)
>>> def div(x, y, fp=TWOPLACES):
...     return (x / y).quantize(fp)

>>> mul(a, b)                       # Automatically preserve fixed-point
Decimal('325.62')
>>> div(b, a)
Decimal('0.03')

Q. 表示同一个值有许多方式。 数字 200, 200.000, 2E2 和 02E+4 的值都相同但有精度不同。 是否有办法将它们转换为一个可识别的规范值？

A. normalize() 方法可将所有相同的值映射为统一表示形式：

>>> values = map(Decimal, '200 200.000 2E2 .02E+4'.split())
>>> [v.normalize() for v in values]
[Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2'), Decimal('2E+2')]

Q. 有些十进制值总是被打印为指数表示形式。 是否有办法得到一个非指数表示形式？

A. 对于某些值来说，指数表示形式是表示系数中有效位的唯一办法。 例如，将 5.0E+3 表示为 5000 可以让值保持恒定，但是无法显示原本的两位有效数字。

如果一个应用不必关心追踪有效位，则可以很容易地移除指数和末尾的零，丢弃有效位但让值保持不变：

>>> def remove_exponent(d):
...     return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize()

>>> remove_exponent(Decimal('5E+3'))
Decimal('5000')

Q. 是否有办法将一个普通浮点数转换为 Decimal？

A. 是的，任何二进制浮点数都可以精确地表示为 Decimal 值，但精确的转换可能需要比直觉设想更高的精度：

>>> Decimal(math.pi)
Decimal('3.141592653589793115997963468544185161590576171875')

Q. 在一个复杂的计算中，我怎样才能保证不会得到由精度不足和舍入异常所导致的虚假结果。

A. 使用 decimal 模块可以很容易地检测结果。 最好的做法是使用更高的精度和不同的舍入模式重新进行计算。 明显不同的结果表明存在精度不足、舍入模式问题、不符合条件的输入或是结果不稳定的算法。

Q. 我发现上下文精度的应用只针对运算结果而不针对输入。在混合使用不同精度的值时有什么需要注意的吗？

A. 是的。 原则上所有值都会被视为精确值，在这些值上进行的算术运算也是如此。 只有结果会被舍入。 对于输入来说其好处是“所输入即所得”。 而其缺点则是如果你忘记了输入没有被舍入，结果看起来可能会很奇怪：

>>> getcontext().prec = 3
>>> Decimal('3.104') + Decimal('2.104')
Decimal('5.21')
>>> Decimal('3.104') + Decimal('0.000') + Decimal('2.104')
Decimal('5.20')

解决办法是提高精度或使用单目加法运算对输入执行强制舍入：

>>> getcontext().prec = 3
>>> +Decimal('1.23456789')      # unary plus triggers rounding
Decimal('1.23')

此外，还可以使用 Context.create_decimal() 方法在创建输入时执行舍入：

>>> Context(prec=5, rounding=ROUND_DOWN).create_decimal('1.2345678')
Decimal('1.2345')

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

9. 数字和数学模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

9. 数字和数学模块 »

|

9.5. fractions — 分数

源代码 Lib/fractions.py [https://github.com/python/cpython/tree/3.6/Lib/fractions.py]

fractions 模块支持分数运算。

分数实例可以由一对整数，一个分数，或者一个字符串构建而成。

class fractions.Fraction(numerator=0, denominator=1)

class fractions.Fraction(other_fraction)

class fractions.Fraction(float)

class fractions.Fraction(decimal)

class fractions.Fraction(string)

第一个版本要求 numerator 和 denominator 是 numbers.Rational 的实例，并返回一个新的 Fraction 实例，其值为 numerator/denominator。 如果 denominator 为 0 将会引发 ZeroDivisionError。 第二个版本要求 other_fraction 是 numbers.Rational 的实例，并返回一个 Fraction 实例且与传入值相等。 下两个版本接受 float 或 decimal.Decimal 的实例，并返回一个 Fraction 实例且与传入值完全相等。 请注意由于二进制浮点数通常存在的问题 (参见 浮点算术：争议和限制)，Fraction(1.1) 的参数并不会精确等于 11/10，因此 Fraction(1.1) 也 不会 返回用户所期望的 Fraction(11, 10)。 （请参阅下文中 limit_denominator() 方法的文档。） 构造器的最后一个版本接受一个字符串或 unicode 实例。 此实例的通常形式为:

[sign] numerator ['/' denominator]

其中的可选项 sign 可以为 ‘+’ 或 ‘-‘ 并且 numerator 和 denominator (如果存在) 是十进制数码的字符串。 此外，float 构造器所接受的任何表示一个有限值的字符串也都为 Fraction 构造器所接受。 不论哪种形式的输入字符串也都可以带有前缀和/或后缀的空格符。 这里是一些示例:

>>> from fractions import Fraction
>>> Fraction(16, -10)
Fraction(-8, 5)
>>> Fraction(123)
Fraction(123, 1)
>>> Fraction()
Fraction(0, 1)
>>> Fraction('3/7')
Fraction(3, 7)
>>> Fraction(' -3/7 ')
Fraction(-3, 7)
>>> Fraction('1.414213 \t\n')
Fraction(1414213, 1000000)
>>> Fraction('-.125')
Fraction(-1, 8)
>>> Fraction('7e-6')
Fraction(7, 1000000)
>>> Fraction(2.25)
Fraction(9, 4)
>>> Fraction(1.1)
Fraction(2476979795053773, 2251799813685248)
>>> from decimal import Decimal
>>> Fraction(Decimal('1.1'))
Fraction(11, 10)

Fraction 类继承自抽象基类 numbers.Rational，并实现了该类的所有方法和操作。 Fraction 实例是可哈希的，并应当被视为不可变对象。 此外，Fraction 还具有以下属性和方法：

在 3.2 版更改: Fraction 构造器现在接受 float 和 decimal.Decimal 实例。

numerator

最简分数形式的分子。

denominator

最简分数形式的分母。

from_float(flt)

此类方法可构造一个 Fraction 来表示 flt 的精确值，该参数必须是一个 float。 请注意 Fraction.from_float(0.3) 的值并不等于 Fraction(3, 10)。

注解

从 Python 3.2 开始，在构造 Fraction 实例时可以直接使用 float。

from_decimal(dec)

此类方法可构造一个 Fraction 来表示 dec 的精确值，该参数必须是一个 decimal.Decimal 实例。

注解

从 Python 3.2 开始，在构造 Fraction 实例时可以直接使用 decimal.Decimal 实例。

limit_denominator(max_denominator=1000000)

找到并返回一个 Fraction 使得其值最接近 self 并且分母不大于 max_denominator。 此方法适用于找出给定浮点数的有理数近似值：

>>> from fractions import Fraction
>>> Fraction('3.1415926535897932').limit_denominator(1000)
Fraction(355, 113)

或是用来恢复被表示为一个浮点数的有理数：

>>> from math import pi, cos
>>> Fraction(cos(pi/3))
Fraction(4503599627370497, 9007199254740992)
>>> Fraction(cos(pi/3)).limit_denominator()
Fraction(1, 2)
>>> Fraction(1.1).limit_denominator()
Fraction(11, 10)

__floor__()

返回最大的 int <= self。 此方法也可通过 math.floor() 函数来访问：

>>> from math import floor
>>> floor(Fraction(355, 113))
3

__ceil__()

返回最小的 int >= self。 此方法也可通过 math.ceil() 函数来访问。

__round__()

__round__(ndigits)

第一个版本返回一个 int 使得其值最接近 self，位值为二分之一时只对偶数舍入。第二个版本会将 self 舍入到最接近 Fraction(1, 10**ndigits) 的倍数（如果 ndigits 为负值则为逻辑运算），位值为二分之一时同样只对偶数舍入。 此方法也可通过 round() 函数来访问。

fractions.gcd(a, b)

返回整数 a 和 b 的最大公约数。如果 a 或 b 之一非零，则 gcd(a, b) 的绝对值是能同时整除 a 和 b 的最大整数。若 b 非零，则 gcd(a,b) 与 b 同号；否则返回值与 a 同号。gcd(0, 0) 返回 0。

3.5 版后已移除: 由 math.gcd() 取代.

参见

numbers 模块

构成数字塔的所有抽象基类。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

9. 数字和数学模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

9. 数字和数学模块 »

|

9.6. random — 生成伪随机数

源码： Lib/random.py [https://github.com/python/cpython/tree/3.6/Lib/random.py]

该模块实现了各种分布的伪随机数生成器。

对于整数，从范围中有统一的选择。 对于序列，存在随机元素的统一选择、用于生成列表的随机排列的函数、以及用于随机抽样而无需替换的函数。

在实数轴上，有计算均匀、正态（高斯）、对数正态、负指数、伽马和贝塔分布的函数。 为了生成角度分布，可以使用 von Mises 分布。

几乎所有模块函数都依赖于基本函数 random() ，它在半开放区间 [0.0,1.0) 内均匀生成随机浮点数。 Python 使用 Mersenne Twister 作为核心生成器。 它产生 53 位精度浮点数，周期为 2**19937-1 ，其在 C 中的底层实现既快又线程安全。 Mersenne Twister 是现存最广泛测试的随机数发生器之一。 但是，因为完全确定性，它不适用于所有目的，并且完全不适合加密目的。

这个模块提供的函数实际上是 random.Random 类的隐藏实例的绑定方法。 你可以实例化自己的 Random 类实例以获取不共享状态的生成器。

如果你想使用自己设计的不同基础生成器，类 Random 也可以作为子类：在这种情况下，重载 random() 、 seed() 、 getstate() 以及 setstate() 方法。可选地，新生成器可以提供 getrandbits() 方法——这允许 randrange() 在任意大的范围内产生选择。

random 模块还提供 SystemRandom 类，它使用系统函数 os.urandom() 从操作系统提供的源生成随机数。

警告

不应将此模块的伪随机生成器用于安全目的。 有关安全性或加密用途，请参阅 secrets 模块。

参见

M. Matsumoto and T. Nishimura, “Mersenne Twister: A 623-dimensionally equidistributed uniform pseudorandom number generator”, ACM Transactions on Modeling and Computer Simulation Vol. 8, No. 1, January pp.3–30 1998.

Complementary-Multiply-with-Carry recipe [https://code.activestate.com/recipes/576707/] 用于兼容的替代随机数发生器，具有长周期和相对简单的更新操作。

9.6.1. 簿记功能

random.seed(a=None, version=2)

初始化随机数生成器。

如果 a 被省略或为 None ，则使用当前系统时间。 如果操作系统提供随机源，则使用它们而不是系统时间（有关可用性的详细信息，请参阅 os.urandom() 函数）。

如果 a 是 int 类型，则直接使用。

对于版本2（默认的），str 、 bytes 或 bytearray 对象转换为 int 并使用它的所有位。

对于版本1（用于从旧版本的Python再现随机序列），用于 str 和 bytes 的算法生成更窄的种子范围。

在 3.2 版更改: 已移至版本2方案，该方案使用字符串种子中的所有位。

random.getstate()

返回捕获生成器当前内部状态的对象。 这个对象可以传递给 setstate() 来恢复状态。

random.setstate(state)

state 应该是从之前调用 getstate() 获得的，并且 setstate() 将生成器的内部状态恢复到 getstate() 被调用时的状态。

random.getrandbits(k)

返回带有 k 位随机的Python整数。 此方法随 MersenneTwister 生成器一起提供，其他一些生成器也可以将其作为API的可选部分提供。 如果可用，getrandbits() 启用 randrange() 来处理任意大范围。

9.6.2. 整数用函数

random.randrange(stop)

random.randrange(start, stop[, step])

从 range(start, stop, step) 返回一个随机选择的元素。 这相当于 choice(range(start, stop, step)) ，但实际上并没有构建一个 range 对象。

位置参数模式匹配 range() 。不应使用关键字参数，因为该函数可能以意外的方式使用它们。

在 3.2 版更改: randrange() 在生成均匀分布的值方面更为复杂。 以前它使用了像``int(random()*n)``这样的形式，它可以产生稍微不均匀的分布。

random.randint(a, b)

返回随机整数 N 满足 a <= N <= b。相当于 randrange(a, b+1)。

9.6.3. 序列用函数

random.choice(seq)

从非空序列 seq 返回一个随机元素。 如果 seq 为空，则引发 IndexError。

random.choices(population, weights=None, *, cum_weights=None, k=1)

从*population*中选择替换，返回大小为 k 的元素列表。 如果 population 为空，则引发 IndexError。

如果指定了 weight 序列，则根据相对权重进行选择。 或者，如果给出 cum_weights 序列，则根据累积权重（可能使用 itertools.accumulate() 计算）进行选择。 例如，相对权重``[10, 5, 30, 5]``相当于累积权重``[10, 15, 45, 50]``。 在内部，相对权重在进行选择之前会转换为累积权重，因此提供累积权重可以节省工作量。

如果既未指定 weight 也未指定 cum_weights ，则以相等的概率进行选择。 如果提供了权重序列，则它必须与 population 序列的长度相同。 一个 TypeError 指定了 weights 和*cum_weights*。

weights 或 cum_weights 可以使用任何与 random() 返回的 float 值互操作的数值类型（包括整数，浮点数和分数但不包括十进制小数）。

3.6 新版功能.

random.shuffle(x[, random])

将序列 x 随机打乱位置。

可选参数 random 是一个0参数函数，在 [0.0, 1.0) 中返回随机浮点数；默认情况下，这是函数 random() 。

要改变一个不可变的序列并返回一个新的打乱列表，请使用``sample(x, k=len(x))``。

请注意，即使对于小的 len(x)，x 的排列总数也可以快速增长，大于大多数随机数生成器的周期。 这意味着长序列的大多数排列永远不会产生。 例如，长度为2080的序列是可以在 Mersenne Twister 随机数生成器的周期内拟合的最大序列。

random.sample(population, k)

返回从总体序列或集合中选择的唯一元素的 k 长度列表。 用于无重复的随机抽样。

返回包含来自总体的元素的新列表，同时保持原始总体不变。 结果列表按选择顺序排列，因此所有子切片也将是有效的随机样本。 这允许抽奖获奖者（样本）被划分为大奖和第二名获胜者（子切片）。

总体成员不必是 hashable 或 unique 。 如果总体包含重复，则每次出现都是样本中可能的选择。

要从一系列整数中选择样本，请使用 range() 对象作为参数。 对于从大量人群中采样，这种方法特别快速且节省空间：sample(range(10000000), k=60) 。

如果样本大小大于总体大小，则引发 ValueError 。

9.6.4. 实值分布

以下函数生成特定的实值分布。如常用数学实践中所使用的那样, 函数参数以分布方程中的相应变量命名;大多数这些方程都可以在任何统计学教材中找到。

random.random()

返回 [0.0, 1.0) 范围内的下一个随机浮点数。

random.uniform(a, b)

返回一个随机浮点数 N ，当 a <= b 时 a <= N <= b ，当 b < a 时 b <= N <= a 。

取决于等式 a + (b-a) * random() 中的浮点舍入，终点 b 可以包括或不包括在该范围内。

random.triangular(low, high, mode)

返回一个随机浮点数 N ，使得 low <= N <= high 并在这些边界之间使用指定的 mode 。 low 和 high 边界默认为零和一。 mode 参数默认为边界之间的中点，给出对称分布。

random.betavariate(alpha, beta)

Beta 分布。 参数的条件是 alpha > 0 和 beta > 0。 返回值的范围介于 0 和 1 之间。

random.expovariate(lambd)

指数分布。 lambd 是 1.0 除以所需的平均值，它应该是非零的。 （该参数本应命名为 “lambda” ，但这是 Python 中的保留字。）如果 lambd 为正，则返回值的范围为 0 到正无穷大；如果 lambd 为负，则返回值从负无穷大到 0。

random.gammavariate(alpha, beta)

Gamma 分布。 （ 不是 gamma 函数！ ） 参数的条件是 alpha > 0 和 beta > 0。

概率分布函数是:

          x ** (alpha - 1) * math.exp(-x / beta)
pdf(x) =  --------------------------------------
            math.gamma(alpha) * beta ** alpha

random.gauss(mu, sigma)

高斯分布。 mu 是平均值，sigma 是标准差。 这比下面定义的 normalvariate() 函数略快。

random.lognormvariate(mu, sigma)

对数正态分布。 如果你采用这个分布的自然对数，你将得到一个正态分布，平均值为 mu 和标准差为 sigma 。 mu 可以是任何值，sigma 必须大于零。

random.normalvariate(mu, sigma)

正态分布。 mu 是平均值，sigma 是标准差。

random.vonmisesvariate(mu, kappa)

冯·米塞斯（von Mises）分布。 mu 是平均角度，以弧度表示，介于0和 2*pi 之间，kappa 是浓度参数，必须大于或等于零。 如果 kappa 等于零，则该分布在 0 到 2*pi 的范围内减小到均匀的随机角度。

random.paretovariate(alpha)

帕累托分布。 alpha 是形状参数。

random.weibullvariate(alpha, beta)

威布尔分布。 alpha 是比例参数，beta 是形状参数。

9.6.5. 替代生成器

class random.SystemRandom([seed])

使用 os.urandom() 函数的类，用从操作系统提供的源生成随机数。 这并非适用于所有系统。 也不依赖于软件状态，序列不可重现。 因此，seed() 方法没有效果而被忽略。 getstate() 和 setstate() 方法如果被调用则引发 NotImplementedError。

9.6.6. 关于再现性的说明

有时能够重现伪随机数生成器给出的序列是有用的。 通过重新使用种子值，只要多个线程没有运行，相同的序列就可以在两次不同运行之间重现。

大多数随机模块的算法和种子函数都会在 Python 版本中发生变化，但保证两个方面不会改变：

如果添加了新的播种方法，则将提供向后兼容的播种机。

当兼容的播种机被赋予相同的种子时，生成器的 random() 方法将继续产生相同的序列。

9.6.7. 例子和配方

基本示例:

>>> random()                             # Random float:  0.0 <= x < 1.0
0.37444887175646646

>>> uniform(2.5, 10.0)                   # Random float:  2.5 <= x < 10.0
3.1800146073117523

>>> expovariate(1 / 5)                   # Interval between arrivals averaging 5 seconds
5.148957571865031

>>> randrange(10)                        # Integer from 0 to 9 inclusive
7

>>> randrange(0, 101, 2)                 # Even integer from 0 to 100 inclusive
26

>>> choice(['win', 'lose', 'draw'])      # Single random element from a sequence
'draw'

>>> deck = 'ace two three four'.split()
>>> shuffle(deck)                        # Shuffle a list
>>> deck
['four', 'two', 'ace', 'three']

>>> sample([10, 20, 30, 40, 50], k=4)    # Four samples without replacement
[40, 10, 50, 30]

模拟:

>>> # Six roulette wheel spins (weighted sampling with replacement)
>>> choices(['red', 'black', 'green'], [18, 18, 2], k=6)
['red', 'green', 'black', 'black', 'red', 'black']

>>> # Deal 20 cards without replacement from a deck of 52 playing cards
>>> # and determine the proportion of cards with a ten-value
>>> # (a ten, jack, queen, or king).
>>> deck = collections.Counter(tens=16, low_cards=36)
>>> seen = sample(list(deck.elements()), k=20)
>>> seen.count('tens') / 20
0.15

>>> # Estimate the probability of getting 5 or more heads from 7 spins
>>> # of a biased coin that settles on heads 60% of the time.
>>> trial = lambda: choices('HT', cum_weights=(0.60, 1.00), k=7).count('H') >= 5
>>> sum(trial() for i in range(10000)) / 10000
0.4169

>>> # Probability of the median of 5 samples being in middle two quartiles
>>> trial = lambda : 2500 <= sorted(choices(range(10000), k=5))[2]  < 7500
>>> sum(trial() for i in range(10000)) / 10000
0.7958

statistical bootstrapping [https://en.wikipedia.org/wiki/Bootstrapping_(statistics)] 使用重采样和替换来估计大小为五的样本的均值的置信区间的示例:

# http://statistics.about.com/od/Applications/a/Example-Of-Bootstrapping.htm
from statistics import mean
from random import choices

data = 1, 2, 4, 4, 10
means = sorted(mean(choices(data, k=5)) for i in range(20))
print(f'The sample mean of {mean(data):.1f} has a 90% confidence '
      f'interval from {means[1]:.1f} to {means[-2]:.1f}')

使用 重新采样排列测试 [https://en.wikipedia.org/wiki/Resampling_(statistics)#Permutation_tests] 来确定统计学显著性或者使用 p-值 [https://en.wikipedia.org/wiki/P-value] 来观察药物与安慰剂的作用之间差异的示例:

# Example from "Statistics is Easy" by Dennis Shasha and Manda Wilson
from statistics import mean
from random import shuffle

drug = [54, 73, 53, 70, 73, 68, 52, 65, 65]
placebo = [54, 51, 58, 44, 55, 52, 42, 47, 58, 46]
observed_diff = mean(drug) - mean(placebo)

n = 10000
count = 0
combined = drug + placebo
for i in range(n):
    shuffle(combined)
    new_diff = mean(combined[:len(drug)]) - mean(combined[len(drug):])
    count += (new_diff >= observed_diff)

print(f'{n} label reshufflings produced only {count} instances with a difference')
print(f'at least as extreme as the observed difference of {observed_diff:.1f}.')
print(f'The one-sided p-value of {count / n:.4f} leads us to reject the null')
print(f'hypothesis that there is no difference between the drug and the placebo.')

模拟单个服务器队列中的到达时间和服务交付:

from random import expovariate, gauss
from statistics import mean, median, stdev

average_arrival_interval = 5.6
average_service_time = 5.0
stdev_service_time = 0.5

num_waiting = 0
arrivals = []
starts = []
arrival = service_end = 0.0
for i in range(20000):
    if arrival <= service_end:
        num_waiting += 1
        arrival += expovariate(1.0 / average_arrival_interval)
        arrivals.append(arrival)
    else:
        num_waiting -= 1
        service_start = service_end if num_waiting else arrival
        service_time = gauss(average_service_time, stdev_service_time)
        service_end = service_start + service_time
        starts.append(service_start)

waits = [start - arrival for arrival, start in zip(arrivals, starts)]
print(f'Mean wait: {mean(waits):.1f}.  Stdev wait: {stdev(waits):.1f}.')
print(f'Median wait: {median(waits):.1f}.  Max wait: {max(waits):.1f}.')

参见

Statistics for Hackers [https://www.youtube.com/watch?v=Iq9DzN6mvYA] Jake Vanderplas [https://us.pycon.org/2016/speaker/profile/295/] 撰写的视频教程，使用一些基本概念进行统计分析，包括模拟、抽样、改组和交叉验证。

Economics Simulation [http://nbviewer.jupyter.org/url/norvig.com/ipython/Economics.ipynb] Peter Norvig [http://norvig.com/bio.html] 编写的市场模拟，显示了该模块提供的许多工具和分布的有效使用（高斯、均匀、样本、beta变量、选择、三角和随机范围等）。

A Concrete Introduction to Probability (using Python) [http://nbviewer.jupyter.org/url/norvig.com/ipython/Probability.ipynb] Peter Norvig [http://norvig.com/bio.html] 撰写的教程，涵盖了概率论基础知识，如何编写模拟，以及如何使用 Python 进行数据分析。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

9. 数字和数学模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

9. 数字和数学模块 »

|

9.7. statistics — 数学统计函数

3.4 新版功能.

源代码: Lib/statistics.py [https://github.com/python/cpython/tree/3.6/Lib/statistics.py]

该模块提供了用于计算数字 (Real 值) 数据的数理统计量的函数。

注解

除非明确注释，这些函数支持 int, float, decimal.Decimal 和 fractions.Fraction。 当前不支持同其他类型（不论是否在数字塔中）的行为。 混合类型也是未定义且取决于具体实现的。 如果你输入由混合类型组成的数据，你应该能够使用 map() 来确保得到一致的结果，例如 map(float, input_data)。

9.7.1. 平均值以及对中心位置的评估

这些函数用于计算一个总体或样本的平均值或者典型值。

mean()

数据的算术平均数（“平均数”）。

harmonic_mean()

数据的调和均值

median()

数据的中位数（中间值）

median_low()

数据的低中位数

median_high()

数据的高中位数

median_grouped()

分组数据的中位数，即第50个百分点。

mode()

Mode (most common value) of discrete data.

9.7.2. 对分散程度的评估

这些函数用于计算总体或样本与典型值或平均值的偏离程度。

pstdev()

数据的总体标准差

pvariance()

数据的总体方差

stdev()

数据的样本标准差

variance()

数据的样本方差

9.7.3. 函数细节

注释：这些函数不需要对提供给它们的数据进行排序。但是，为了方便阅读，大多数例子展示的是已排序的序列。

statistics.mean(data)

返回 data 的样本算术平均数，数据可是是一个序列或迭代器。

算术平均数是数据之和与数据点个数的商。通常称作“平均数”，尽管它指示诸多数学平均数之一。它是数据中心位置的度量。

若 data 为空，将会引发 StatisticsError。

一些用法示例：

>>> mean([1, 2, 3, 4, 4])
2.8
>>> mean([-1.0, 2.5, 3.25, 5.75])
2.625

>>> from fractions import Fraction as F
>>> mean([F(3, 7), F(1, 21), F(5, 3), F(1, 3)])
Fraction(13, 21)

>>> from decimal import Decimal as D
>>> mean([D("0.5"), D("0.75"), D("0.625"), D("0.375")])
Decimal('0.5625')

注解

The mean is strongly affected by outliers and is not a robust estimator for central location: the mean is not necessarily a typical example of the data points. For more robust, although less efficient, measures of central location, see median() and mode(). (In this case, “efficient” refers to statistical efficiency rather than computational efficiency.)

The sample mean gives an unbiased estimate of the true population mean, which means that, taken on average over all the possible samples, mean(sample) converges on the true mean of the entire population. If data represents the entire population rather than a sample, then mean(data) is equivalent to calculating the true population mean μ.

statistics.harmonic_mean(data)

返回 data 的调和均值，数据可以是序列或实数值的迭代器。

调和均值,也叫次相反均值，所有数据的倒数的算术平均数 mean() 的倒数。比如说，数据 a ， b ， c 的调和均值等于 3/(1/a + 1/b + 1/c) 。

The harmonic mean is a type of average, a measure of the central location of the data. It is often appropriate when averaging quantities which are rates or ratios, for example speeds. For example:

假设一名投资者在三家公司各购买了等价值的股票，以 2.5， 3 ， 10 的 P/E (投资/回报) 率。投资者投资组合的平均市盈率是多少？

>>> harmonic_mean([2.5, 3, 10])  # For an equal investment portfolio.
3.6

Using the arithmetic mean would give an average of about 5.167, which is too high.

如果 data 为空或者 任何一个元素的值小于零，会引发 StatisticsError 。

3.6 新版功能.

statistics.median(data)

使用常见的“取中间两数平均值”方法，返回数字数据的中位数（中间值）。如果 data 为空，则引发 StatisticsError 。 data  可以是序列或迭代器。

The median is a robust measure of central location, and is less affected by the presence of outliers in your data. When the number of data points is odd, the middle data point is returned:

>>> median([1, 3, 5])
3

当数据点的总数为偶数时，中位数将通过对两个中间值求平均进行插值得出：

>>> median([1, 3, 5, 7])
4.0

这适用于当你的数据是离散的，并且你不介意中位数不是实际数据点的情况。

If your data is ordinal (supports order operations) but not numeric (doesn’t support addition), you should use median_low() or median_high() instead.

参见

median_low(), median_high(), median_grouped()

statistics.median_low(data)

Return the low median of numeric data. If data is empty, StatisticsError is raised. data can be a sequence or iterator.

低中位数一定是数据集的成员。 当数据点总数为奇数时，将返回中间值。 当其为偶数时，将返回两个中间值中较小的那个。

>>> median_low([1, 3, 5])
3
>>> median_low([1, 3, 5, 7])
3

当你的数据是离散的，并且你希望中位数是一个实际数据点而非插值结果时可以使用低中位数。

statistics.median_high(data)

Return the high median of data. If data is empty, StatisticsError is raised. data can be a sequence or iterator.

高中位数一定是数据集的成员。 当数据点总数为奇数时，将返回中间值。 当其为偶数时，将返回两个中间值中较大的那个。

>>> median_high([1, 3, 5])
3
>>> median_high([1, 3, 5, 7])
5

当你的数据是离散的，并且你希望中位数是一个实际数据点而非插值结果时可以使用高中位数。

statistics.median_grouped(data, interval=1)

Return the median of grouped continuous data, calculated as the 50th percentile, using interpolation. If data is empty, StatisticsError is raised. data can be a sequence or iterator.

>>> median_grouped([52, 52, 53, 54])
52.5

在下面的示例中，数据已经过舍入，这样每个值都代表数据分类的中间点，例如 1 是 0.5–1.5 分类的中间点，2 是 1.5–2.5 分类的中间点，3 是 2.5–3.5 的中间点等待。 根据给定的数据，中间值应落在 3.5–4.5 分类之内，并可使用插值法来进行估算：

>>> median_grouped([1, 2, 2, 3, 4, 4, 4, 4, 4, 5])
3.7

可选参数 interval 表示分类间隔，默认值为 1。 改变分类间隔自然会改变插件结果：

>>> median_grouped([1, 3, 3, 5, 7], interval=1)
3.25
>>> median_grouped([1, 3, 3, 5, 7], interval=2)
3.5

此函数不会检查数据点之间是否至少相隔 interval 的距离。

CPython implementation detail: 在某些情况下，median_grouped() 可以会将数据点强制转换为浮点数。 此行为在未来有可能会发生改变。

参见

“Statistics for the Behavioral Sciences”, Frederick J Gravetter and Larry B Wallnau (8th Edition).

Calculating the median [https://www.ualberta.ca/~opscan/median.html].

Gnome Gnumeric 电子表格中的 SSMEDIAN [https://help.gnome.org/users/gnumeric/stable/gnumeric.html#gnumeric-function-SSMEDIAN] 函数，包括 这篇讨论 [https://mail.gnome.org/archives/gnumeric-list/2011-April/msg00018.html]。

statistics.mode(data)

Return the most common data point from discrete or nominal data. The mode (when it exists) is the most typical value, and is a robust measure of central location.

If data is empty, or if there is not exactly one most common value, StatisticsError is raised.

mode assumes discrete data, and returns a single value. This is the standard treatment of the mode as commonly taught in schools:

>>> mode([1, 1, 2, 3, 3, 3, 3, 4])
3

The mode is unique in that it is the only statistic which also applies to nominal (non-numeric) data:

>>> mode(["red", "blue", "blue", "red", "green", "red", "red"])
'red'

statistics.pstdev(data, mu=None)

返回总体标准差（总体方差的平方根）。 请参阅 pvariance() 了解参数和其他细节。

>>> pstdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])
0.986893273527251

statistics.pvariance(data, mu=None)

Return the population variance of data, a non-empty iterable of real-valued numbers. Variance, or second moment about the mean, is a measure of the variability (spread or dispersion) of data. A large variance indicates that the data is spread out; a small variance indicates it is clustered closely around the mean.

If the optional second argument mu is given, it should be the mean of data. If it is missing or None (the default), the mean is automatically calculated.

使用此函数可根据所有数值来计算方差。 要根据一个样本来估算方差，通常 variance() 函数是更好的选择。

如果 data 为空则会引发 StatisticsError。

示例：

>>> data = [0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]
>>> pvariance(data)
1.25

如果你已经计算过数据的平均值，你可以将其作为可选的第二个参数 mu 传入以避免重复计算：

>>> mu = mean(data)
>>> pvariance(data, mu)
1.25

This function does not attempt to verify that you have passed the actual mean as mu. Using arbitrary values for mu may lead to invalid or impossible results.

同样也支持使用 Decimal 和 Fraction 值：

>>> from decimal import Decimal as D
>>> pvariance([D("27.5"), D("30.25"), D("30.25"), D("34.5"), D("41.75")])
Decimal('24.815')

>>> from fractions import Fraction as F
>>> pvariance([F(1, 4), F(5, 4), F(1, 2)])
Fraction(13, 72)

注解

当调用时附带完整的总体数据时，这将给出总体方差 σ²。 而当调用时只附带一个样本时，这将给出偏置样本方差 s²，也被称为带有 N 个自由度的方差。

If you somehow know the true population mean μ, you may use this function to calculate the variance of a sample, giving the known population mean as the second argument. Provided the data points are representative (e.g. independent and identically distributed), the result will be an unbiased estimate of the population variance.

statistics.stdev(data, xbar=None)

返回样本标准差（样本方差的平方根）。 请参阅 variance() 了解参数和其他细节。

>>> stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])
1.0810874155219827

statistics.variance(data, xbar=None)

返回包含至少两个实数值的可迭代对象 data 的样本方差。 方差或称相对于均值的二阶矩，是对数据变化幅度（延展度或分散度）的度量。 方差值较大表明数据的散布范围较大；方差值较小表明它紧密聚集于均值附近。

如果给出了可选的第二个参数 xbar，它应当是 data 的均值。 如果该参数省略或为 None (默认值)，则会自动进行均值的计算。

当你的数据是总体数据的样本时请使用此函数。 要根据整个总体数据来计算方差，请参见 pvariance()。

如果 data 包含的值少于两个则会引发 StatisticsError。

示例：

>>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
>>> variance(data)
1.3720238095238095

如果你已经计算过数据的平均值，你可以将其作为可选的第二个参数 xbar 传入以避免重复计算：

>>> m = mean(data)
>>> variance(data, m)
1.3720238095238095

此函数不会试图检查你所传入的 xbar 是否为真实的平均值。 使用任意值作为 xbar 可能导致无效或不可能的结果。

同样也支持使用 Decimal 和 Fraction 值：

>>> from decimal import Decimal as D
>>> variance([D("27.5"), D("30.25"), D("30.25"), D("34.5"), D("41.75")])
Decimal('31.01875')

>>> from fractions import Fraction as F
>>> variance([F(1, 6), F(1, 2), F(5, 3)])
Fraction(67, 108)

注解

这是附带贝塞尔校正的样本方差 s²，也称为具有 N-1 自由度的方差。 假设数据点具有代表性（即为独立且均匀的分布），则结果应当是对总体方差的无偏估计。

如果你通过某种方式知道了真实的总体平均值 μ 则应当调用 pvariance() 函数并将该值作为 mu 形参传入以得到一个样本的方差。

9.7.4. 异常

只定义了一个异常：

exception statistics.StatisticsError

ValueError 的子类，表示统计相关的异常。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

9. 数字和数学模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

10. 函数式编程模块

本章里描述的模块提供了函数和类，以支持函数式编程风格和在可调用对象上的通用操作。

本章包含以下模块的文档：

10.1. itertools — 为高效循环而创建迭代器的函数

10.1.1. Itertool函数

10.1.2. Itertools食谱

10.2. functools — 高阶函数和可调用对象上的操作

10.2.1. partial 对象

10.3. operator — 标准运算符替代函数

10.3.1. 将运算符映射到函数

10.3.2. Inplace Operators

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

10. 函数式编程模块 »

|

10.1. itertools — 为高效循环而创建迭代器的函数

本模块实现一系列 iterator ，这些迭代器受到APL，Haskell和SML的启发。为了适用于Python，它们都被重新写过。

本模块标准化了一个快速、高效利用内存的核心工具集，这些工具本身或组合都很有用。它们一起形成了“迭代器代数”，这使得在纯Python中有可能创建简洁又高效的专用工具。

例如，SML有一个制表工具： tabulate(f)，它可产生一个序列 f(0), f(1), ...。在Python中可以组合 map() 和 count() 实现： map(f, count())。

这些内置工具同时也能很好地与 operator 模块中的高效函数配合使用。例如，我们可以将两个向量的点积映射到乘法运算符： sum(map(operator.mul, vector1, vector2)) 。

无穷迭代器：

迭代器

实参

结果

示例

count()

start, [step]

start, start+step, start+2*step, …

count(10) --> 10 11 12 13 14 ...

cycle()

p

p0, p1, … plast, p0, p1, …

cycle('ABCD') --> A B C D A B C D ...

repeat()

elem [,n]

elem, elem, elem, … 重复无限次或n次

repeat(10, 3) --> 10 10 10

根据最短输入序列长度停止的迭代器：

迭代器

实参

结果

示例

accumulate()

p [,func]

p0, p0+p1, p0+p1+p2, …

accumulate([1,2,3,4,5]) --> 1 3 6 10 15

chain()

p, q, …

p0, p1, … plast, q0, q1, …

chain('ABC', 'DEF') --> A B C D E F

chain.from_iterable()

iterable – 可迭代对象

p0, p1, … plast, q0, q1, …

chain.from_iterable(['ABC', 'DEF']) --> A B C D E F

compress()

data, selectors

(d[0] if s[0]), (d[1] if s[1]), …

compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F

dropwhile()

pred, seq

seq[n], seq[n+1], … 从pred首次真值测试失败开始

dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1

filterfalse()

pred, seq

seq中pred(x)为假值的元素，x是seq中的元素。

filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8

groupby()

iterable[, key]

根据key(v)值分组的迭代器

islice()

seq, [start,] stop [, step]

seq[start:stop:step]中的元素

islice('ABCDEFG', 2, None) --> C D E F G

starmap()

func, seq

func(*seq[0]), func(*seq[1]), …

starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000

takewhile()

pred, seq

seq[0], seq[1], …, 直到pred真值测试失败

takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4

tee()

it, n

it1, it2, … itn 将一个迭代器拆分为n个迭代器

zip_longest()

p, q, …

(p[0], q[0]), (p[1], q[1]), …

zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-

排列组合迭代器：

迭代器

实参

结果

product()

p, q, … [repeat=1]

笛卡尔积，相当于嵌套的for循环

permutations()

p[, r]

长度r元组，所有可能的排列，无重复元素

combinations()

p, r

长度r元组，有序，无重复元素

combinations_with_replacement()

p, r

长度r元组，有序，元素可重复

product('ABCD', repeat=2)

AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD

permutations('ABCD', 2)

AB AC AD BA BC BD CA CB CD DA DB DC

combinations('ABCD', 2)

AB AC AD BC BD CD

combinations_with_replacement('ABCD', 2)

AA AB AC AD BB BC BD CC CD DD

10.1.1. Itertool函数

下列模块函数均创建并返回迭代器。有些迭代器不限制输出流长度，所以它们只应在能截断输出流的函数或循环中使用。

itertools.accumulate(iterable[, func])

创建一个迭代器，返回累加和或其他二元函数的累加结果（通过可选参数 func 指定）。如果提供了 func ，它应是2个参数的函数。输入 iterable 元素类型应是 func 能支持的任意类型。（例如，对于默认的加法操作，元素可以是任一支持加法的类型，包括 Decimal 或 Fraction ）。如果可迭代对象的输入为空，输出也为空。

大致相当于：

def accumulate(iterable, func=operator.add):
    'Return running totals'
    # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
    # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
    it = iter(iterable)
    try:
        total = next(it)
    except StopIteration:
        return
    yield total
    for element in it:
        total = func(total, element)
        yield total

func 参数有几种用法。它可以被设为 min() 最终得到一个最小值，或者设为 max() 最终得到一个最大值，或设为 operator.mul() 最终得到一个乘积。摊销表可通过累加利息和支付款项得到。给iterable设置初始值并只将参数 func 设为累加总数可以对一阶 递归关系 [https://en.wikipedia.org/wiki/Recurrence_relation] 建模。

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list(accumulate(data, operator.mul))     # running product
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list(accumulate(data, max))              # running maximum
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# Amortize a 5% loan of 1000 with 4 annual payments of 90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list(accumulate(cashflows, lambda bal, pmt: bal*1.05 + pmt))
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]

# Chaotic recurrence relation https://en.wikipedia.org/wiki/Logistic_map
>>> logistic_map = lambda x, _:  r * x * (1 - x)
>>> r = 3.8
>>> x0 = 0.4
>>> inputs = repeat(x0, 36)     # only the initial value is used
>>> [format(x, '.2f') for x in accumulate(inputs, logistic_map)]
['0.40', '0.91', '0.30', '0.81', '0.60', '0.92', '0.29', '0.79', '0.63',
 '0.88', '0.39', '0.90', '0.33', '0.84', '0.52', '0.95', '0.18', '0.57',
 '0.93', '0.25', '0.71', '0.79', '0.63', '0.88', '0.39', '0.91', '0.32',
 '0.83', '0.54', '0.95', '0.20', '0.60', '0.91', '0.30', '0.80', '0.60']

参考一个类似函数 functools.reduce() ，它只返回一个最终累积值。

3.2 新版功能.

在 3.3 版更改: 增加可选参数 func 。

itertools.chain(*iterables)

创建一个迭代器，它首先返回第一个可迭代对象中所有元素，接着返回下一个可迭代对象中所有元素，直到耗尽所有可迭代对象中的元素。可将多个序列处理为单个序列。大致相当于：

def chain(*iterables):
    # chain('ABC', 'DEF') --> A B C D E F
    for it in iterables:
        for element in it:
            yield element

classmethod chain.from_iterable(iterable)

构建类似 chain() 迭代器的另一个选择。从一个单独的可迭代参数中得到链式输入，该参数是延迟计算的。大致相当于：

def from_iterable(iterables):
    # chain.from_iterable(['ABC', 'DEF']) --> A B C D E F
    for it in iterables:
        for element in it:
            yield element

itertools.combinations(iterable, r)

返回由输入 iterable 中元素组成长度为 r 的子序列。

组合按照字典序返回。所以如果输入 iterable 是有序的，生成的组合元组也是有序的。

即使元素的值相同，不同位置的元素也被认为是不同的。如果元素各自不同，那么每个组合中没有重复元素。

大致相当于：

def combinations(iterable, r):
    # combinations('ABCD', 2) --> AB AC AD BC BD CD
    # combinations(range(4), 3) --> 012 013 023 123
    pool = tuple(iterable)
    n = len(pool)
    if r > n:
        return
    indices = list(range(r))
    yield tuple(pool[i] for i in indices)
    while True:
        for i in reversed(range(r)):
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range(i+1, r):
            indices[j] = indices[j-1] + 1
        yield tuple(pool[i] for i in indices)

combinations() 的代码可被改写为 permutations() 过滤后的子序列，（相对于元素在输入中的位置）元素不是有序的。

def combinations(iterable, r):
    pool = tuple(iterable)
    n = len(pool)
    for indices in permutations(range(n), r):
        if sorted(indices) == list(indices):
            yield tuple(pool[i] for i in indices)

当 0 <= r <= n 时，返回项的个数是 n! / r! / (n-r)!；当 r > n 时，返回项个数为0。

itertools.combinations_with_replacement(iterable, r)

返回由输入 iterable 中元素组成的长度为 r 的子序列，允许每个元素可重复出现。

组合按照字典序返回。所以如果输入 iterable 是有序的，生成的组合元组也是有序的。

不同位置的元素是不同的，即使它们的值相同。因此如果输入中的元素都是不同的话，返回的组合中元素也都会不同。

大致相当于：

def combinations_with_replacement(iterable, r):
    # combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC
    pool = tuple(iterable)
    n = len(pool)
    if not n and r:
        return
    indices = [0] * r
    yield tuple(pool[i] for i in indices)
    while True:
        for i in reversed(range(r)):
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * (r - i)
        yield tuple(pool[i] for i in indices)

combinations_with_replacement() 的代码可被改写为 production() 过滤后的子序列，（相对于元素在输入中的位置）元素不是有序的。

def combinations_with_replacement(iterable, r):
    pool = tuple(iterable)
    n = len(pool)
    for indices in product(range(n), repeat=r):
        if sorted(indices) == list(indices):
            yield tuple(pool[i] for i in indices)

当 n > 0 时，返回项个数为 (n+r-1)! / r! / (n-1)!.

3.1 新版功能.

itertools.compress(data, selectors)

创建一个迭代器，它返回 data 中经 selectors 真值测试为 True 的元素。迭代器在两者较短的长度处停止。大致相当于：

def compress(data, selectors):
    # compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
    return (d for d, s in zip(data, selectors) if s)

3.1 新版功能.

itertools.count(start=0, step=1)

创建一个迭代器，它从 start 值开始，返回均匀间隔的值。常用于 map() 中的实参来生成连续的数据点。此外，还用于 zip() 来添加序列号。大致相当于：

def count(start=0, step=1):
    # count(10) --> 10 11 12 13 14 ...
    # count(2.5, 0.5) -> 2.5 3.0 3.5 ...
    n = start
    while True:
        yield n
        n += step

当对浮点数计数时，替换为乘法代码有时精度会更好，例如： (start + step * i for i in count()) 。

在 3.1 版更改: 增加参数 step ，允许非整型。

itertools.cycle(iterable)

创建一个迭代器，返回 iterable 中所有元素并保存一个副本。当取完 iterable 中所有元素，返回副本中的所有元素。无限重复。大致相当于：

def cycle(iterable):
    # cycle('ABCD') --> A B C D A B C D A B C D ...
    saved = []
    for element in iterable:
        yield element
        saved.append(element)
    while saved:
        for element in saved:
              yield element

注意，该函数可能需要相当大的辅助空间（取决于 iterable 的长度）。

itertools.dropwhile(predicate, iterable)

创建一个迭代器，如果 predicate 为true，迭代器丢弃这些元素，然后返回其他元素。注意，迭代器在 predicate 首次为false之前不会产生任何输出，所以可能需要一定长度的启动时间。大致相当于：

def dropwhile(predicate, iterable):
    # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
    iterable = iter(iterable)
    for x in iterable:
        if not predicate(x):
            yield x
            break
    for x in iterable:
        yield x

itertools.filterfalse(predicate, iterable)

创建一个迭代器，只返回 iterable 中 predicate 为 False 的元素。如果 predicate 是 None，返回真值测试为false的元素。大致相当于：

def filterfalse(predicate, iterable):
    # filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
    if predicate is None:
        predicate = bool
    for x in iterable:
        if not predicate(x):
            yield x

itertools.groupby(iterable, key=None)

创建一个迭代器，返回 iterable 中连续的键和组。key 是一个计算元素键值函数。如果未指定或为 None，key 缺省为恒等函数（identity function），返回元素不变。一般来说，iterable 需用同一个键值函数预先排序。

groupby() 操作类似于Unix中的 uniq。当每次 key 函数产生的键值改变时，迭代器会分组或生成一个新组（这就是为什么通常需要使用同一个键值函数先对数据进行排序）。这种行为与SQL的GROUP BY操作不同，SQL的操作会忽略输入的顺序将相同键值的元素分在同组中。

返回的组本身也是一个迭代器，它与 groupby() 共享底层的可迭代对象。因为源是共享的，当 groupby() 对象向后迭代时，前一个组将消失。因此如果稍后还需要返回结果，可保存为列表：

groups = []
uniquekeys = []
data = sorted(data, key=keyfunc)
for k, g in groupby(data, keyfunc):
    groups.append(list(g))      # Store group iterator as a list
    uniquekeys.append(k)

groupby() 大致相当于：

class groupby:
    # [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
    # [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
    def __init__(self, iterable, key=None):
        if key is None:
            key = lambda x: x
        self.keyfunc = key
        self.it = iter(iterable)
        self.tgtkey = self.currkey = self.currvalue = object()
    def __iter__(self):
        return self
    def __next__(self):
        while self.currkey == self.tgtkey:
            self.currvalue = next(self.it)    # Exit on StopIteration
            self.currkey = self.keyfunc(self.currvalue)
        self.tgtkey = self.currkey
        return (self.currkey, self._grouper(self.tgtkey))
    def _grouper(self, tgtkey):
        while self.currkey == tgtkey:
            yield self.currvalue
            try:
                self.currvalue = next(self.it)
            except StopIteration:
                return
            self.currkey = self.keyfunc(self.currvalue)

itertools.islice(iterable, stop)

itertools.islice(iterable, start, stop[, step])

创建一个迭代器，返回从 iterable 里选中的元素。如果 start 不是0，跳过 iterable 中的元素，直到到达 start 这个位置。之后迭代器连续返回元素，除非 step 设置的值很高导致被跳过。如果 stop 为 None，迭代器耗光为止；否则，在指定的位置停止。与普通的切片不同，islice() 不支持将 start ， stop ，或 step 设为负值。可用来从内部数据结构被压平的数据中提取相关字段（例如一个多行报告，它的名称字段出现在每三行上）。大致相当于：

def islice(iterable, *args):
    # islice('ABCDEFG', 2) --> A B
    # islice('ABCDEFG', 2, 4) --> C D
    # islice('ABCDEFG', 2, None) --> C D E F G
    # islice('ABCDEFG', 0, None, 2) --> A C E G
    s = slice(*args)
    start, stop, step = s.start or 0, s.stop or sys.maxsize, s.step or 1
    it = iter(range(start, stop, step))
    try:
        nexti = next(it)
    except StopIteration:
        # Consume *iterable* up to the *start* position.
        for i, element in zip(range(start), iterable):
            pass
        return
    try:
        for i, element in enumerate(iterable):
            if i == nexti:
                yield element
                nexti = next(it)
    except StopIteration:
        # Consume to *stop*.
        for i, element in zip(range(i + 1, stop), iterable):
            pass

如果 start 为 None，迭代从0开始。如果 step 为 None ，步长缺省为1。

itertools.permutations(iterable, r=None)

连续返回由 iterable 元素生成长度为 r 的排列。

如果 r 未指定或为 None ，r 默认设置为 iterable 的长度，这种情况下，生成所有全长排列。

排列依字典序发出。因此，如果 iterable 是已排序的，排列元组将有序地产出。

即使元素的值相同，不同位置的元素也被认为是不同的。如果元素值都不同，每个排列中的元素值不会重复。

大致相当于：

def permutations(iterable, r=None):
    # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
    # permutations(range(3)) --> 012 021 102 120 201 210
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    if r > n:
        return
    indices = list(range(n))
    cycles = list(range(n, n-r, -1))
    yield tuple(pool[i] for i in indices[:r])
    while n:
        for i in reversed(range(r)):
            cycles[i] -= 1
            if cycles[i] == 0:
                indices[i:] = indices[i+1:] + indices[i:i+1]
                cycles[i] = n - i
            else:
                j = cycles[i]
                indices[i], indices[-j] = indices[-j], indices[i]
                yield tuple(pool[i] for i in indices[:r])
                break
        else:
            return

permutations() 的代码也可被改写为 product() 的子序列，只要将含有重复元素（来自输入中同一位置的）的项排除。

def permutations(iterable, r=None):
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    for indices in product(range(n), repeat=r):
        if len(set(indices)) == r:
            yield tuple(pool[i] for i in indices)

当 0 <= r <= n ，返回项个数为 n! / (n-r)! ；当 r > n ，返回项个数为0。

itertools.product(*iterables, repeat=1)

可迭代对象输入的笛卡儿积。

大致相当于生成器表达式中的嵌套循环。例如， product(A, B) 和 ((x,y) for x in A for y in B) 返回结果一样。

嵌套循环像里程表那样循环变动，每次迭代时将最右侧的元素向后迭代。这种模式形成了一种字典序，因此如果输入的可迭代对象是已排序的，笛卡尔积元组依次序发出。

要计算可迭代对象自身的笛卡尔积，将可选参数 repeat 设定为要重复的次数。例如，product(A, repeat=4) 和 product(A, A, A, A) 是一样的。

该函数大致相当于下面的代码，只不过实际实现方案不会在内存中创建中间结果。

def product(*args, repeat=1):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = [tuple(pool) for pool in args] * repeat
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

itertools.repeat(object[, times])

创建一个迭代器，不断重复 object 。除非设定参数 times ，否则将无限重复。可用于 map() 函数中的参数，被调用函数可得到一个不变参数。也可用于 zip() 的参数以在元组记录中创建一个不变的部分。

大致相当于：

def repeat(object, times=None):
    # repeat(10, 3) --> 10 10 10
    if times is None:
        while True:
            yield object
    else:
        for i in range(times):
            yield object

repeat 最常见的用途就是在 map 或 zip 提供一个常量流：

>>> list(map(pow, range(10), repeat(2)))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

itertools.starmap(function, iterable)

创建一个迭代器，使用从可迭代对象中获取的参数来计算该函数。当参数对应的形参已从一个单独可迭代对象组合为元组时（数据已被“预组对”）可用此函数代替 map()。map() 与 starmap() 之间的区别可以类比 function(a,b) 与 function(*c) 的区别。大致相当于:

def starmap(function, iterable):
    # starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
    for args in iterable:
        yield function(*args)

itertools.takewhile(predicate, iterable)

创建一个迭代器，只要 predicate 为真就从可迭代对象中返回元素。大致相当于:

def takewhile(predicate, iterable):
    # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
    for x in iterable:
        if predicate(x):
            yield x
        else:
            break

itertools.tee(iterable, n=2)

从一个可迭代对象中返回 n 个独立的迭代器。

下面的Python代码能帮助解释 tee 做了什么（尽管实际的实现更复杂，而且仅使用了一个底层的 FIFO 队列）。

大致相当于：

def tee(iterable, n=2):
    it = iter(iterable)
    deques = [collections.deque() for i in range(n)]
    def gen(mydeque):
        while True:
            if not mydeque:             # when the local deque is empty
                try:
                    newval = next(it)   # fetch a new value and
                except StopIteration:
                    return
                for d in deques:        # load it to all the deques
                    d.append(newval)
            yield mydeque.popleft()
    return tuple(gen(d) for d in deques)

一旦 tee() 实施了一次分裂，原有的 iterable 不应再被使用；否则tee对象无法得知 iterable 可能已向后迭代。

该迭代工具可能需要相当大的辅助存储空间（这取决于要保存多少临时数据）。通常，如果一个迭代器在另一个迭代器开始之前就要使用大部份或全部数据，使用 list() 会比 tee() 更快。

itertools.zip_longest(*iterables, fillvalue=None)

创建一个迭代器，从每个可迭代对象中收集元素。如果可迭代对象的长度未对齐，将根据 fillvalue 填充缺失值。迭代持续到耗光最长的可迭代对象。大致相当于：

class ZipExhausted(Exception):
    pass

def zip_longest(*args, **kwds):
    # zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
    fillvalue = kwds.get('fillvalue')
    counter = len(args) - 1
    def sentinel():
        nonlocal counter
        if not counter:
            raise ZipExhausted
        counter -= 1
        yield fillvalue
    fillers = repeat(fillvalue)
    iterators = [chain(it, sentinel(), fillers) for it in args]
    try:
        while iterators:
            yield tuple(map(next, iterators))
    except ZipExhausted:
        pass

如果其中一个可迭代对象有无限长度，zip_longest() 函数应封装在限制调用次数的场景中（例如 islice() 或 takewhile()）。除非指定， fillvalue 默认为 None 。

10.1.2. Itertools食谱

本节将展示如何使用现有的itertools作为基础构件来创建扩展的工具集。

扩展的工具提供了与底层工具集相同的高性能。保持了超棒的内存利用率，因为一次只处理一个元素，而不是将整个可迭代对象加载到内存。代码量保持得很小，以函数式风格将这些工具连接在一起，有助于消除临时变量。速度依然很快，因为倾向于使用“矢量化”构件来取代解释器开销大的 for 循环和 generator 。

def take(n, iterable):
    "Return first n items of the iterable as a list"
    return list(islice(iterable, n))

def prepend(value, iterator):
    "Prepend a single value in front of an iterator"
    # prepend(1, [2, 3, 4]) -> 1 2 3 4
    return chain([value], iterator)

def tabulate(function, start=0):
    "Return function(0), function(1), ..."
    return map(function, count(start))

def tail(n, iterable):
    "Return an iterator over the last n items"
    # tail(3, 'ABCDEFG') --> E F G
    return iter(collections.deque(iterable, maxlen=n))

def consume(iterator, n=None):
    "Advance the iterator n-steps ahead. If n is None, consume entirely."
    # Use functions that consume iterators at C speed.
    if n is None:
        # feed the entire iterator into a zero-length deque
        collections.deque(iterator, maxlen=0)
    else:
        # advance to the empty slice starting at position n
        next(islice(iterator, n, n), None)

def nth(iterable, n, default=None):
    "Returns the nth item or a default value"
    return next(islice(iterable, n, None), default)

def all_equal(iterable):
    "Returns True if all the elements are equal to each other"
    g = groupby(iterable)
    return next(g, True) and not next(g, False)

def quantify(iterable, pred=bool):
    "Count how many times the predicate is true"
    return sum(map(pred, iterable))

def padnone(iterable):
    """Returns the sequence elements and then returns None indefinitely.

    Useful for emulating the behavior of the built-in map() function.
    """
    return chain(iterable, repeat(None))

def ncycles(iterable, n):
    "Returns the sequence elements n times"
    return chain.from_iterable(repeat(tuple(iterable), n))

def dotproduct(vec1, vec2):
    return sum(map(operator.mul, vec1, vec2))

def flatten(listOfLists):
    "Flatten one level of nesting"
    return chain.from_iterable(listOfLists)

def repeatfunc(func, times=None, *args):
    """Repeat calls to func with specified arguments.

    Example:  repeatfunc(random.random)
    """
    if times is None:
        return starmap(func, repeat(args))
    return starmap(func, repeat(args, times))

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)

def roundrobin(*iterables):
    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
    # Recipe credited to George Sakkis
    num_active = len(iterables)
    nexts = cycle(iter(it).__next__ for it in iterables)
    while num_active:
        try:
            for next in nexts:
                yield next()
        except StopIteration:
            # Remove the iterator we just exhausted from the cycle.
            num_active -= 1
            nexts = cycle(islice(nexts, num_active))

def partition(pred, iterable):
    'Use a predicate to partition entries into false entries and true entries'
    # partition(is_odd, range(10)) --> 0 2 4 6 8   and  1 3 5 7 9
    t1, t2 = tee(iterable)
    return filterfalse(pred, t1), filter(pred, t2)

def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

def unique_everseen(iterable, key=None):
    "List unique elements, preserving order. Remember all elements ever seen."
    # unique_everseen('AAAABBBCCDAABBB') --> A B C D
    # unique_everseen('ABBCcAD', str.lower) --> A B C D
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in filterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element

def unique_justseen(iterable, key=None):
    "List unique elements, preserving order. Remember only the element just seen."
    # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
    # unique_justseen('ABBCcAD', str.lower) --> A B C A D
    return map(next, map(itemgetter(1), groupby(iterable, key)))

def iter_except(func, exception, first=None):
    """ Call a function repeatedly until an exception is raised.

    Converts a call-until-exception interface to an iterator interface.
    Like builtins.iter(func, sentinel) but uses an exception instead
    of a sentinel to end the loop.

    Examples:
        iter_except(functools.partial(heappop, h), IndexError)   # priority queue iterator
        iter_except(d.popitem, KeyError)                         # non-blocking dict iterator
        iter_except(d.popleft, IndexError)                       # non-blocking deque iterator
        iter_except(q.get_nowait, Queue.Empty)                   # loop over a producer Queue
        iter_except(s.pop, KeyError)                             # non-blocking set iterator

    """
    try:
        if first is not None:
            yield first()            # For database APIs needing an initial cast to db.first()
        while True:
            yield func()
    except exception:
        pass

def first_true(iterable, default=False, pred=None):
    """Returns the first true value in the iterable.

    If no true value is found, returns *default*

    If *pred* is not None, returns the first item
    for which pred(item) is true.

    """
    # first_true([a,b,c], x) --> a or b or c or x
    # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
    return next(filter(pred, iterable), default)

def random_product(*args, repeat=1):
    "Random selection from itertools.product(*args, **kwds)"
    pools = [tuple(pool) for pool in args] * repeat
    return tuple(random.choice(pool) for pool in pools)

def random_permutation(iterable, r=None):
    "Random selection from itertools.permutations(iterable, r)"
    pool = tuple(iterable)
    r = len(pool) if r is None else r
    return tuple(random.sample(pool, r))

def random_combination(iterable, r):
    "Random selection from itertools.combinations(iterable, r)"
    pool = tuple(iterable)
    n = len(pool)
    indices = sorted(random.sample(range(n), r))
    return tuple(pool[i] for i in indices)

def random_combination_with_replacement(iterable, r):
    "Random selection from itertools.combinations_with_replacement(iterable, r)"
    pool = tuple(iterable)
    n = len(pool)
    indices = sorted(random.randrange(n) for i in range(r))
    return tuple(pool[i] for i in indices)

def nth_combination(iterable, r, index):
    'Equivalent to list(combinations(iterable, r))[index]'
    pool = tuple(iterable)
    n = len(pool)
    if r < 0 or r > n:
        raise ValueError
    c = 1
    k = min(r, n-r)
    for i in range(1, k+1):
        c = c * (n - k + i) // i
    if index < 0:
        index += c
    if index < 0 or index >= c:
        raise IndexError
    result = []
    while r:
        c, n, r = c*r//n, n-1, r-1
        while index >= c:
            index -= c
            c, n = c*(n-r)//n, n-1
        result.append(pool[-1-n])
    return tuple(result)

注意，通过将全局查找替换为局部变量的缺省值，上述配方中有很多可以这样优化。例如， dotproduct 配方可以这样写：

def dotproduct(vec1, vec2, sum=sum, map=map, mul=operator.mul):
    return sum(map(mul, vec1, vec2))

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

10. 函数式编程模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

10. 函数式编程模块 »

|

10.2. functools — 高阶函数和可调用对象上的操作

源代码: Lib/functools.py [https://github.com/python/cpython/tree/3.6/Lib/functools.py]

functools 模块应用于高阶函数，即——参数或（和）返回值为其他函数的函数。通常来说，此模块的功能适用于所有可调用对象。

functools 模块定义了以下函数:

functools.cmp_to_key(func)

将(旧式的)比较函数转换为新式的 key function . 在类似于 sorted() ， min() ， max() ， heapq.nlargest() ， heapq.nsmallest() ， itertools.groupby() 等函数的 key 参数中使用。此函数主要用作将 Python 2 程序转换至新版的转换工具，以保持对比较函数的兼容。

比较函数意为一个可调用对象，该对象接受两个参数并比较它们，结果为小于则返回一个负数，相等则返回零，大于则返回一个正数。key function则是一个接受一个参数，并返回另一个用以排序的值的可调用对象。

示例:

sorted(iterable, key=cmp_to_key(locale.strcoll))  # locale-aware sort order

有关排序示例和简要排序教程，请参阅 排序指南 。

3.2 新版功能.

@functools.lru_cache(maxsize=128, typed=False)

一个为函数提供缓存功能的装饰器，缓存 maxsize 组传入参数，在下次以相同参数调用时直接返回上一次的结果。用以节约高开销或I/O函数的调用时间。

由于使用了字典存储缓存，所以该函数的固定参数和关键字参数必须是可哈希的。

如果 maxsize 设置为 None ，LRU功能将被禁用且缓存数量无上限。 maxsize 设置为2的幂时可获得最佳性能。

如果 typed 设置为true，不同类型的函数参数将被分别缓存。例如， f(3) 和 f(3.0) 将被视为不同而分别缓存。

为了衡量缓存的有效性以便调整 maxsize 形参，被装饰的函数带有一个 cache_info() 函数。当调用 cache_info() 函数时，返回一个具名元组，包含命中次数 hits，未命中次数 misses ，最大缓存数量 maxsize 和 当前缓存大小 currsize。在多线程环境中，命中数与未命中数是不完全准确的。

该装饰器也提供了一个用于清理/使缓存失效的函数 cache_clear() 。

原始的未经装饰的函数可以通过 __wrapped__ 属性访问。它可以用于检查、绕过缓存，或使用不同的缓存再次装饰原始函数。

“最久未使用算法”（LRU）缓存 [https://en.wikipedia.org/wiki/Cache_algorithms#Examples] 在“最近的调用是即将到来的调用的最佳预测因子”时性能最好（比如，新闻服务器上最受欢迎的文章倾向于每天更改）。 “缓存大小限制”参数保证缓存不会在长时间运行的进程比如说网站服务器上无限制的增加自身的大小。

一般来说，LRU缓存只在当你想要重用之前计算的结果时使用。因此，用它缓存具有副作用的函数、需要在每次调用时创建不同、易变的对象的函数或者诸如time（）或random（）之类的不纯函数是没有意义的。

静态 Web 内容的 LRU 缓存示例:

@lru_cache(maxsize=32)
def get_pep(num):
    'Retrieve text of a Python Enhancement Proposal'
    resource = 'http://www.python.org/dev/peps/pep-%04d/' % num
    try:
        with urllib.request.urlopen(resource) as s:
            return s.read()
    except urllib.error.HTTPError:
        return 'Not Found'

>>> for n in 8, 290, 308, 320, 8, 218, 320, 279, 289, 320, 9991:
...     pep = get_pep(n)
...     print(n, len(pep))

>>> get_pep.cache_info()
CacheInfo(hits=3, misses=8, maxsize=32, currsize=8)

以下是使用缓存通过 动态规划 [https://zh.wikipedia.org/wiki/动态规划] 计算 斐波那契数列 [https://zh.wikipedia.org/wiki/斐波那契数列] 的例子。

@lru_cache(maxsize=None)
def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)

>>> [fib(n) for n in range(16)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]

>>> fib.cache_info()
CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)

3.2 新版功能.

在 3.3 版更改: 添加 typed 选项。

@functools.total_ordering

给定一个声明一个或多个全比较排序方法的类，这个类装饰器实现剩余的方法。这减轻了指定所有可能的全比较操作的工作。

此类必须包含以下方法之一：__lt__() 、__le__()、__gt__() 或 __ge__()。另外，此类必须支持 __eq__() 方法。

例如

@total_ordering
class Student:
    def _is_valid_operand(self, other):
        return (hasattr(other, "lastname") and
                hasattr(other, "firstname"))
    def __eq__(self, other):
        if not self._is_valid_operand(other):
            return NotImplemented
        return ((self.lastname.lower(), self.firstname.lower()) ==
                (other.lastname.lower(), other.firstname.lower()))
    def __lt__(self, other):
        if not self._is_valid_operand(other):
            return NotImplemented
        return ((self.lastname.lower(), self.firstname.lower()) <
                (other.lastname.lower(), other.firstname.lower()))

注解

虽然此装饰器使得创建具有良好行为的完全有序类型变得非常容易，但它 确实 是以执行速度更缓慢和派生比较方法的堆栈回溯更复杂为代价的。 如果性能基准测试表明这是特定应用的瓶颈所在，则改为实现全部六个富比较方法应该会轻松提升速度。

3.2 新版功能.

在 3.4 版更改: 现在已支持从未识别类型的下层比较函数返回 NotImplemented 异常。

functools.partial(func, *args, **keywords)

返回一个新的 部分对象，当被调用时其行为类似于 func 附带位置参数 args 和关键字参数 keywords 被调用。 如果为调用提供了更多的参数，它们会被附加到 args。 如果提供了额外的关键字参数，它们会扩展并重载 keywords。 大致等价于:

def partial(func, *args, **keywords):
    def newfunc(*fargs, **fkeywords):
        newkeywords = keywords.copy()
        newkeywords.update(fkeywords)
        return func(*args, *fargs, **newkeywords)
    newfunc.func = func
    newfunc.args = args
    newfunc.keywords = keywords
    return newfunc

partial() 会被“冻结了”一部分函数参数和/或关键字的部分函数应用所使用，从而得到一个具有简化签名的新对象。 例如，partial() 可用来创建一个行为类似于 int() 函数的可调用对象，其中 base 参数默认为二：

>>> from functools import partial
>>> basetwo = partial(int, base=2)
>>> basetwo.__doc__ = 'Convert base 2 string to an int.'
>>> basetwo('10010')
18

class functools.partialmethod(func, *args, **keywords)

返回一个新的 partialmethod 描述器，其行为类似 partial 但它被设计用作方法定义而非直接用作可调用对象。

func 必须是一个 descriptor 或可调用对象（同属两者的对象例如普通函数会被当作描述器来处理）。

当 func 是一个描述器（例如普通 Python 函数, classmethod(), staticmethod(), abstractmethod() 或其他 partialmethod 的实例）时, 对 __get__ 的调用会被委托给底层的描述器，并会返回一个适当的 部分对象 作为结果。

当 func 是一个非描述器类可调用对象时，则会动态创建一个适当的绑定方法。 当用作方法时其行为类似普通 Python 函数：将会插入 self 参数作为第一个位置参数，其位置甚至会处于提供给 partialmethod 构造器的 args 和 keywords 之前。

示例:

>>> class Cell(object):
...     def __init__(self):
...         self._alive = False
...     @property
...     def alive(self):
...         return self._alive
...     def set_state(self, state):
...         self._alive = bool(state)
...     set_alive = partialmethod(set_state, True)
...     set_dead = partialmethod(set_state, False)
...
>>> c = Cell()
>>> c.alive
False
>>> c.set_alive()
>>> c.alive
True

3.4 新版功能.

functools.reduce(function, iterable[, initializer])

将两个参数的 function 从左至右累积地应用到 sequence 的条目，以便将该序列缩减为单一值。 例如，reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) 是计算 ((((1+2)+3)+4)+5) 的值。 左边的参数 x 是累积值而右边的参数 y 则是来自 sequence 的更新值。 如果存在可选项 initializer，它会被放在参与计算的序列的条目之前，并在序列对象为空时作为默认值。 如果没有给出 initializer 并且 sequence 仅包含一个条目，则将返回第一项。

大致相当于：

def reduce(function, iterable, initializer=None):
    it = iter(iterable)
    if initializer is None:
        value = next(it)
    else:
        value = initializer
    for element in it:
        value = function(value, element)
    return value

@functools.singledispatch

将一个函数转换为 单分派 generic function。

要定义一个泛型函数，应使用 @singledispatch 装饰器进行装饰。 请注意分派是作用于第一个参数的类型，要相应地创建你的函数:

>>> from functools import singledispatch
>>> @singledispatch
... def fun(arg, verbose=False):
...     if verbose:
...         print("Let me just say,", end=" ")
...     print(arg)

To add overloaded implementations to the function, use the register() attribute of the generic function. It is a decorator, taking a type parameter and decorating a function implementing the operation for that type:

>>> @fun.register(int)
... def _(arg, verbose=False):
...     if verbose:
...         print("Strength in numbers, eh?", end=" ")
...     print(arg)
...
>>> @fun.register(list)
... def _(arg, verbose=False):
...     if verbose:
...         print("Enumerate this:")
...     for i, elem in enumerate(arg):
...         print(i, elem)

要启用注册 lambda 和现有函数，可以使用函数形式的 register() 属性:

>>> def nothing(arg, verbose=False):
...     print("Nothing.")
...
>>> fun.register(type(None), nothing)

register() 属性将返回启用了装饰器堆栈、封存的未装饰函数，并会为每个变量单独创建单元测试:

>>> @fun.register(float)
... @fun.register(Decimal)
... def fun_num(arg, verbose=False):
...     if verbose:
...         print("Half of your number:", end=" ")
...     print(arg / 2)
...
>>> fun_num is fun
False

在调用时，泛型函数会根据第一个参数的类型进行分派:

>>> fun("Hello, world.")
Hello, world.
>>> fun("test.", verbose=True)
Let me just say, test.
>>> fun(42, verbose=True)
Strength in numbers, eh? 42
>>> fun(['spam', 'spam', 'eggs', 'spam'], verbose=True)
Enumerate this:
0 spam
1 spam
2 eggs
3 spam
>>> fun(None)
Nothing.
>>> fun(1.23)
0.615

在没有用于特定类型的已注册实现的情况下，则会使用其方法解析顺序来查找更通用的实现。 以 @singledispatch 装饰的原始函数将为最基本的 object 类型进行注册，这意味着它将在找不到更好的实现时被使用。

要检查泛型函数将为给定类型选择哪个实现，请使用 dispatch() 属性:

>>> fun.dispatch(float)
<function fun_num at 0x1035a2840>
>>> fun.dispatch(dict)    # note: default implementation
<function fun at 0x103fe0000>

要访问所有忆注册实现，请使用只读的 registry 属性:

>>> fun.registry.keys()
dict_keys([<class 'NoneType'>, <class 'int'>, <class 'object'>,
          <class 'decimal.Decimal'>, <class 'list'>,
          <class 'float'>])
>>> fun.registry[float]
<function fun_num at 0x1035a2840>
>>> fun.registry[object]
<function fun at 0x103fe0000>

3.4 新版功能.

functools.update_wrapper(wrapper, wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)

更新一个 wrapper 函数以使其类似于 wrapped 函数。 可选参数为指明原函数的哪些属性要直接被赋值给 wrapper 函数的匹配属性的元组，并且这些 wrapper 函数的属性将使用原函数的对应属性来更新。 这些参数的默认值是模块级常量 WRAPPER_ASSIGNMENTS (它将被赋值给 wrapper 函数的 __module__, __name__, __qualname__, __annotations__ 和 __doc__ 即文档字符串) 以及 WRAPPER_UPDATES (它将更新 wrapper 函数的 __dict__ 即实例字典)。

为了允许出于内省和其他目的访问原始函数（例如绕过 lru_cache() 之类的缓存装饰器），此函数会自动为 wrapper 添加一个指向被包装函数的 __wrapped__ 属性。

此函数的主要目的是在 decorator 函数中用来包装被装饰的函数并返回包装器。 如果包装器函数未被更新，则被返回函数的元数据将反映包装器定义而不是原始函数定义，这通常没有什么用处。

update_wrapper() 可以与函数之外的可调用对象一同使用。 在 assigned 或 updated 中命名的任何属性如果不存在于被包装对象则会被忽略（即该函数将不会尝试在包装器函数上设置它们）。 如果包装器函数自身缺少在 updated 中命名的任何属性则仍将引发 AttributeError。

3.2 新版功能: 自动添加 __wrapped__ 属性。

3.2 新版功能: 默认拷贝 __annotations__ 属性。

在 3.2 版更改: 不存在的属性将不再触发 AttributeError。

在 3.4 版更改: __wrapped__ 属性现在总是指向被包装的函数，即使该函数定义了 __wrapped__ 属性。 (参见 bpo-17482 [https://bugs.python.org/issue17482])

@functools.wraps(wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)

这是一个便捷函数，用于在定义包装器函数时发起调用 update_wrapper() 作为函数装饰器。 它等价于 partial(update_wrapper, wrapped=wrapped, assigned=assigned, updated=updated)。 例如:

>>> from functools import wraps
>>> def my_decorator(f):
...     @wraps(f)
...     def wrapper(*args, **kwds):
...         print('Calling decorated function')
...         return f(*args, **kwds)
...     return wrapper
...
>>> @my_decorator
... def example():
...     """Docstring"""
...     print('Called example function')
...
>>> example()
Calling decorated function
Called example function
>>> example.__name__
'example'
>>> example.__doc__
'Docstring'

如果不使用这个装饰器工厂函数，则 example 函数的名称将变为 'wrapper'，并且 example() 原本的文档字符串将会丢失。

10.2.1. partial 对象

partial 对象是由 partial() 创建的可调用对象。 它们具有三个只读属性：

partial.func

一个可调用对象或函数。 对 partial 对象的调用将被转发给 func 并附带新的参数和关键字。

partial.args

最左边的位置参数将放置在提供给 partial 对象调用的位置参数之前。

partial.keywords

当调用 partial 对象时将要提供的关键字参数。

partial 对象与 function 对象的类似之处在于它们都是可调用、可弱引用的对象并可拥有属性。 但两者也存在一些重要的区别。 例如前者不会自动创建 __name__ 和 __doc__ 属性。 而且，在类中定义的 partial 对象的行为类似于静态方法，并且不会在实例属性查找期间转换为绑定方法。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

10. 函数式编程模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

10. 函数式编程模块 »

|

10.3. operator — 标准运算符替代函数

源代码: Lib/operator.py [https://github.com/python/cpython/tree/3.6/Lib/operator.py]

operator 模块提供了一套与Python的内置运算符对应的高效率函数。例如，operator.add(x, y) 与表达式 x+y 相同。 许多函数名与特殊方法名相同，只是没有双下划线。为了向后兼容性，也保留了许多包含双下划线的函数。为了表述清楚，建议使用没有双下划线的函数。

函数包含的种类有：对象的比较运算、逻辑运算、数学运算以及序列运算。

对象比较函数适用于所有的对象，函数名根据它们对应的比较运算符命名。

operator.lt(a, b)

operator.le(a, b)

operator.eq(a, b)

operator.ne(a, b)

operator.ge(a, b)

operator.gt(a, b)

operator.__lt__(a, b)

operator.__le__(a, b)

operator.__eq__(a, b)

operator.__ne__(a, b)

operator.__ge__(a, b)

operator.__gt__(a, b)

在 a 和 b 之间进行全比较。具体的，lt(a, b) 与 a < b 相同， le(a, b) 与 a <= b 相同，eq(a, b) 与 a == b 相同，ne(a, b) 与 a != b 相同，gt(a, b) 与 a > b 相同，ge(a, b)``与 ``a >= b 相同。注意这些函数可以返回任何值，无论它是否可当作布尔值。关于全比较的更多信息请参考 比较运算 。

逻辑运算通常也适用于所有对象，并且支持真值检测、标识检测和布尔运算：

operator.not_(obj)

operator.__not__(obj)

返回 not obj 的结果。 （请注意对象实例并没有 __not__() 方法；只有解释器核心可定义此操作。 结果会受 __bool__() 和 __len__() 方法影响。）

operator.truth(obj)

如果 obj 为真值则返回 True，否则返回 False。 这等价于使用 bool 构造器。

operator.is_(a, b)

返回 a is b. 测试对象标识。

operator.is_not(a, b)

返回 a is not b. 测试对象标识。

数学和按位运算的种类是最多的：

operator.abs(obj)

operator.__abs__(obj)

返回 obj 的绝对值。

operator.add(a, b)

operator.__add__(a, b)

对于数字 a 和 b，返回 a + b。

operator.and_(a, b)

operator.__and__(a, b)

返回 x 和 y 按位与

operator.floordiv(a, b)

operator.__floordiv__(a, b)

返回 a // b.

operator.index(a)

operator.__index__(a)

返回 a 转换为整数的结果。 等价于 a.__index__()。

operator.inv(obj)

operator.invert(obj)

operator.__inv__(obj)

operator.__invert__(obj)

返回数字 obj 按位取反的结果。 这等价于 ~obj。

operator.lshift(a, b)

operator.__lshift__(a, b)

返回 a 左移 b 位的结果。

operator.mod(a, b)

operator.__mod__(a, b)

返回 a % b.

operator.mul(a, b)

operator.__mul__(a, b)

对于数字 a 和 b，返回 a * b。

operator.matmul(a, b)

operator.__matmul__(a, b)

返回 a @ b.

3.5 新版功能.

operator.neg(obj)

operator.__neg__(obj)

返回 obj 的负值 (-obj)。

operator.or_(a, b)

operator.__or__(a, b)

返回 a 和 b 按位或的结果。

operator.pos(obj)

operator.__pos__(obj)

返回 obj 取正的结果 (+obj)。

operator.pow(a, b)

operator.__pow__(a, b)

对于数字 a 和 b，返回 a ** b。

operator.rshift(a, b)

operator.__rshift__(a, b)

返回 a 右移 b 位的结果。

operator.sub(a, b)

operator.__sub__(a, b)

返回 a - b.

operator.truediv(a, b)

operator.__truediv__(a, b)

返回 a / b 例如 2/3 将等于 .66 而不是 0。 这也被称为“真”除法。

operator.xor(a, b)

operator.__xor__(a, b)

返回 a 和 b 按位异或的结果。

适用于序列的操作（其中一些也适用于映射）包括：

operator.concat(a, b)

operator.__concat__(a, b)

对于序列 a 和 b，返回 a + b。

operator.contains(a, b)

operator.__contains__(a, b)

返回 b in a 检测的结果。 请注意操作数是反序的。

operator.countOf(a, b)

返回 b 在 a 中的出现次数。

operator.delitem(a, b)

operator.__delitem__(a, b)

移除索引号 b 上的值 a。

operator.getitem(a, b)

operator.__getitem__(a, b)

返回索引号 b 上的值 a。

operator.indexOf(a, b)

返回 b 在 a 中首次出现所在的索引号。

operator.setitem(a, b, c)

operator.__setitem__(a, b, c)

将索引号 b 上的值 a 设为 c。

operator.length_hint(obj, default=0)

返回对象 o 的估计长度。 首先尝试返回其实际长度，再使用 object.__length_hint__() 得出估计值，最后返回默认值。

3.4 新版功能.

operator 模块还定义了一些用于常规属性和条目查找的工具。 这些工具适合用来编写快速字段提取器作为 map(), sorted(), itertools.groupby() 或其他需要相应函数参数的函数的参数。

operator.attrgetter(attr)

operator.attrgetter(*attrs)

返回一个可从操作数中获取 attr 的可调用对象。 如果请求了一个以上的属性，则返回一个属性元组。 属性名称还可包含点号。 例如：

在 f = attrgetter('name') 之后，调用 f(b) 将返回 b.name。

在 f = attrgetter('name', 'date') 之后，调用 f(b) 将返回 (b.name, b.date)。

在 f = attrgetter('name.first', 'name.last') 之后，调用 f(b) 将返回 (b.name.first, b.name.last)。

等价于:

def attrgetter(*items):
    if any(not isinstance(item, str) for item in items):
        raise TypeError('attribute name must be a string')
    if len(items) == 1:
        attr = items[0]
        def g(obj):
            return resolve_attr(obj, attr)
    else:
        def g(obj):
            return tuple(resolve_attr(obj, attr) for attr in items)
    return g

def resolve_attr(obj, attr):
    for name in attr.split("."):
        obj = getattr(obj, name)
    return obj

operator.itemgetter(item)

operator.itemgetter(*items)

返回一个使用操作数的 __getitem__() 方法从操作数中获取 item 的可调用对象。 如果指定了多个条目，则返回一个查找值的元组。 例如：

在 f = itemgetter(2) 之后，调用 f(r) 将返回 r[2]。

在 g = itemgetter(2, 5, 3) 之后，调用 g(r) 将返回 (r[2], r[5], r[3])。

等价于:

def itemgetter(*items):
    if len(items) == 1:
        item = items[0]
        def g(obj):
            return obj[item]
    else:
        def g(obj):
            return tuple(obj[item] for item in items)
    return g

传入的条目可以为操作数的 __getitem__() 所接受的任何类型。 字典接受任意可哈希的值。 列表、元组和字符串接受 index 或 slice 对象：

>>> itemgetter(1)('ABCDEFG')
'B'
>>> itemgetter(1,3,5)('ABCDEFG')
('B', 'D', 'F')
>>> itemgetter(slice(2,None))('ABCDEFG')
'CDEFG'

使用 itemgetter() 从元组的记录中提取特定字段的例子：

>>> inventory = [('apple', 3), ('banana', 2), ('pear', 5), ('orange', 1)]
>>> getcount = itemgetter(1)
>>> list(map(getcount, inventory))
[3, 2, 5, 1]
>>> sorted(inventory, key=getcount)
[('orange', 1), ('banana', 2), ('apple', 3), ('pear', 5)]

operator.methodcaller(name[, args...])

返回一个在操作数上调用 name 方法的可调用对象。 如果给出额外的参数和/或关键字参数，它们也将被传给该方法。 例如：

在 f = methodcaller('name') 之后，调用 f(b) 将返回 b.name()。

在 f = methodcaller('name', 'foo', bar=1) 之后，调用 f(b) 将返回 b.name('foo', bar=1)。

等价于:

def methodcaller(name, *args, **kwargs):
    def caller(obj):
        return getattr(obj, name)(*args, **kwargs)
    return caller

10.3.1. 将运算符映射到函数

以下表格显示了抽象运算是如何对应于 Python 语法中的运算符和 operator 模块中的函数的。

运算

语法

函数

加法

a + b

add(a, b)

字符串拼接

seq1 + seq2

concat(seq1, seq2)

包含测试

obj in seq

contains(seq, obj)

除法

a / b

truediv(a, b)

除法

a // b

floordiv(a, b)

按位与

a & b

and_(a, b)

按位异或

a ^ b

xor(a, b)

按位取反

~ a

invert(a)

按位或

a | b

or_(a, b)

取幂

a ** b

pow(a, b)

一致

a is b

is_(a, b)

一致

a is not b

is_not(a, b)

索引赋值

obj[k] = v

setitem(obj, k, v)

索引删除

del obj[k]

delitem(obj, k)

索引取值

obj[k]

getitem(obj, k)

左移

a << b

lshift(a, b)

取模

a % b

mod(a, b)

乘法

a * b

mul(a, b)

矩阵乘法

a @ b

matmul(a, b)

否定（算术）

- a

neg(a)

否定（逻辑）

not a

not_(a)

正数

+ a

pos(a)

右移

a >> b

rshift(a, b)

切片赋值

seq[i:j] = values

setitem(seq, slice(i, j), values)

切片删除

del seq[i:j]

delitem(seq, slice(i, j))

切片取值

seq[i:j]

getitem(seq, slice(i, j))

字符串格式化

s % obj

mod(s, obj)

减法

a - b

sub(a, b)

真值测试

obj

truth(obj)

比较

a < b

lt(a, b)

比较

a <= b

le(a, b)

相等

a == b

eq(a, b)

不等

a != b

ne(a, b)

比较

a >= b

ge(a, b)

比较

a > b

gt(a, b)

10.3.2. Inplace Operators

许多运算都有“原地”版本。 以下列出的是提供对原地运算符相比通常语法更底层访问的函数，例如 statement x += y 相当于 x = operator.iadd(x, y)。 换一种方式来讲就是 z = operator.iadd(x, y) 等价于语句块 z = x; z += y。

在这些例子中，请注意当调用一个原地方法时，运算和赋值是分成两个步骤来执行的。 下面列出的原地函数只执行第一步即调用原地方法。 第二步赋值则不加处理。

对于不可变的目标例如字符串、数字和元组，更新的值会被计算，但不会被再被赋值给输入变量：

>>> a = 'hello'
>>> iadd(a, ' world')
'hello world'
>>> a
'hello'

For mutable targets such as lists and dictionaries, the inplace method will perform the update, so no subsequent assignment is necessary:

>>> s = ['h', 'e', 'l', 'l', 'o']
>>> iadd(s, [' ', 'w', 'o', 'r', 'l', 'd'])
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
>>> s
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

operator.iadd(a, b)

operator.__iadd__(a, b)

a = iadd(a, b) 等价于 a += b。

operator.iand(a, b)

operator.__iand__(a, b)

a = iand(a, b) 等价于 a &= b。

operator.iconcat(a, b)

operator.__iconcat__(a, b)

a = iconcat(a, b) 等价于 a += b 其中 a 和 b 为序列。

operator.ifloordiv(a, b)

operator.__ifloordiv__(a, b)

a = ifloordiv(a, b) 等价于 a //= b.

operator.ilshift(a, b)

operator.__ilshift__(a, b)

a = ilshift(a, b) 等价于 a <<= b。

operator.imod(a, b)

operator.__imod__(a, b)

a = imod(a, b) 等价于 a %= b。

operator.imul(a, b)

operator.__imul__(a, b)

a = imul(a, b) 等价于 a *= b。

operator.imatmul(a, b)

operator.__imatmul__(a, b)

a = imatmul(a, b) 等价于 a @= b。

3.5 新版功能.

operator.ior(a, b)

operator.__ior__(a, b)

a = ior(a, b) 等价于 a |= b。

operator.ipow(a, b)

operator.__ipow__(a, b)

a = ipow(a, b) 等价于 a **= b。

operator.irshift(a, b)

operator.__irshift__(a, b)

a = irshift(a, b) 等价于 a >>= b。

operator.isub(a, b)

operator.__isub__(a, b)

a = isub(a, b) 等价于 a -= b。

operator.itruediv(a, b)

operator.__itruediv__(a, b)

a = itruediv(a, b) 等价于 a /= b。

operator.ixor(a, b)

operator.__ixor__(a, b)

a = ixor(a, b) 等价于 a ^= b。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

10. 函数式编程模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

11. 文件和目录访问

本章中描述的模块处理磁盘文件和目录。 例如，有一些模块用于读取文件的属性，以可移植的方式操作路径以及创建临时文件。 本章的完整模块列表如下：

11.1. pathlib — 面向对象的文件系统路径

11.1.1. 基础使用

11.1.2. 纯路径

11.1.2.1. 通用性质

11.1.2.2. 运算符

11.1.2.3. 访问个别部分

11.1.2.4. 方法和特征属性

11.1.3. 具体路径

11.1.3.1. 方法

11.2. os.path — 常见路径操作

11.3. fileinput — 迭代来自多个输入流的行

11.4. stat — 解析 stat() 结果

11.5. filecmp — 文件及目录的比较

11.5.1. dircmp 类

11.6. tempfile — 生成临时文件和目录

11.6.1. 例子

11.6.2. 已弃用的函数和变量

11.7. glob — Unix 风格路径名模式扩展

11.8. fnmatch — Unix 文件名模式匹配

11.9. linecache — 随机读写文本行

11.10. shutil — 高阶文件操作

11.10.1. 目录和文件操作

11.10.1.1. copytree 示例

11.10.1.2. rmtree 示例

11.10.2. 归档操作

11.10.2.1. 归档程序示例

11.10.3. 查询输出终端的尺寸

11.11. macpath — Mac OS 9 路径操作函数

参见

模块 os

操作系统接口，包括处理比 Python 文件对象 更低级别文件的功能。

模块 io

Python的内置 I/O 库，包括抽象类和一些具体的类，如文件 I/O 。

内置函数 open()

使用 Python 打开文件进行读写的标准方法。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

11.1. pathlib — 面向对象的文件系统路径

3.4 新版功能.

源代码 Lib/pathlib.py [https://github.com/python/cpython/tree/3.6/Lib/pathlib.py]

该模块提供表示文件系统路径的类，其语义适用于不同的操作系统。路径类被分为提供纯计算操作而没有 I/O 的 纯路径，以及从纯路径继承而来但提供 I/O 操作的 具体路径。

如果你以前从未使用过此模块或者不确定在项目中使用哪一个类是正确的，则 Path 总是你需要的。它在运行代码的平台上实例化为一个 具体路径。

在一些用例中纯路径很有用，例如：

如果你想要在 Unix 设备上操作 Windows 路径（或者相反）。你不应在 Unix 上实例化一个 WindowsPath，但是你可以实例化 PureWindowsPath。

你只想操作路径但不想实际访问操作系统。在这种情况下，实例化一个纯路径是有用的，因为它们没有任何访问操作系统的操作。

参见

PEP 428 [https://www.python.org/dev/peps/pep-0428]：pathlib 模块 – 面向对象的的文件系统路径。

参见

对于底层的路径字符串操作，你也可以使用 os.path 模块。

11.1.1. 基础使用

导入主类:

>>> from pathlib import Path

列出子目录:

>>> p = Path('.')
>>> [x for x in p.iterdir() if x.is_dir()]
[PosixPath('.hg'), PosixPath('docs'), PosixPath('dist'),
 PosixPath('__pycache__'), PosixPath('build')]

列出当前目录树下的所有 Python 源代码文件:

>>> list(p.glob('**/*.py'))
[PosixPath('test_pathlib.py'), PosixPath('setup.py'),
 PosixPath('pathlib.py'), PosixPath('docs/conf.py'),
 PosixPath('build/lib/pathlib.py')]

在目录树中移动:

>>> p = Path('/etc')
>>> q = p / 'init.d' / 'reboot'
>>> q
PosixPath('/etc/init.d/reboot')
>>> q.resolve()
PosixPath('/etc/rc.d/init.d/halt')

查询路径的属性:

>>> q.exists()
True
>>> q.is_dir()
False

打开一个文件:

>>> with q.open() as f: f.readline()
...
'#!/bin/bash\n'

11.1.2. 纯路径

纯路径对象提供了不实际访问文件系统的路径处理操作。有三种方式来访问这些类，也是不同的风格：

class pathlib.PurePath(*pathsegments)

一个通用的类，代表当前系统的路径风格（实例化为 PurePosixPath 或者 PureWindowsPath）:

>>> PurePath('setup.py')      # Running on a Unix machine
PurePosixPath('setup.py')

每一个 pathsegments 的元素可能是一个代表路径片段的字符串，一个返回字符串的实现了 os.PathLike 接口的对象，或者另一个路径对象:

>>> PurePath('foo', 'some/path', 'bar')
PurePosixPath('foo/some/path/bar')
>>> PurePath(Path('foo'), Path('bar'))
PurePosixPath('foo/bar')

当 pathsegments 为空的时候，假定为当前目录:

>>> PurePath()
PurePosixPath('.')

当给出一些绝对路径，最后一位将被当作锚（模仿 os.path.join() 的行为）:

>>> PurePath('/etc', '/usr', 'lib64')
PurePosixPath('/usr/lib64')
>>> PureWindowsPath('c:/Windows', 'd:bar')
PureWindowsPath('d:bar')

但是，在 Windows 路径中，改变本地根目录并不会丢弃之前盘符的设置:

>>> PureWindowsPath('c:/Windows', '/Program Files')
PureWindowsPath('c:/Program Files')

假斜线和单独的点都会被消除，但是双点 （‘..’） 不会，以防改变符号链接的含义。

>>> PurePath('foo//bar')
PurePosixPath('foo/bar')
>>> PurePath('foo/./bar')
PurePosixPath('foo/bar')
>>> PurePath('foo/../bar')
PurePosixPath('foo/../bar')

（如果你想让 PurePosixPath('foo/../bar') 等同于 PurePosixPath('bar')，那么 you are too young, too simple, sometimes naive! 如果 foo 是一个指向其他其他目录的符号链接，那就出毛病啦。）

纯路径对象实现了 os.PathLike 接口，允许它们在任何接受此接口的地方使用。

在 3.6 版更改: 添加了 os.PathLike 接口支持。

class pathlib.PurePosixPath(*pathsegments)

一个 PurePath 的子类，路径风格不同于 Windows 文件系统:

>>> PurePosixPath('/etc')
PurePosixPath('/etc')

pathsegments 参数的指定和 PurePath 相同。

class pathlib.PureWindowsPath(*pathsegments)

PurePath 的一个子类，路径风格为 Windows 文件系统路径:

>>> PureWindowsPath('c:/Program Files/')
PureWindowsPath('c:/Program Files')

pathsegments 参数的指定和 PurePath 相同。

无论你正运行什么系统，你都可以实例化这些类，因为它们提供的操作不做任何系统调用。

11.1.2.1. 通用性质

路径是不可变并可哈希的。相同风格的路径可以排序与比较。这些性质尊重对应风格的大小写转换语义:

>>> PurePosixPath('foo') == PurePosixPath('FOO')
False
>>> PureWindowsPath('foo') == PureWindowsPath('FOO')
True
>>> PureWindowsPath('FOO') in { PureWindowsPath('foo') }
True
>>> PureWindowsPath('C:') < PureWindowsPath('d:')
True

不同风格的路径比较得到不等的结果并且无法被排序:

>>> PureWindowsPath('foo') == PurePosixPath('foo')
False
>>> PureWindowsPath('foo') < PurePosixPath('foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'PureWindowsPath' and 'PurePosixPath'

11.1.2.2. 运算符

斜杠 / 操作符有助于创建子路径，就像 os.path.join() 一样:

>>> p = PurePath('/etc')
>>> p
PurePosixPath('/etc')
>>> p / 'init.d' / 'apache2'
PurePosixPath('/etc/init.d/apache2')
>>> q = PurePath('bin')
>>> '/usr' / q
PurePosixPath('/usr/bin')

文件对象可用于任何接受 os.PathLike 接口实现的地方。

>>> import os
>>> p = PurePath('/etc')
>>> os.fspath(p)
'/etc'

路径的字符串表示法为它自己原始的文件系统路径（以原生形式，例如在 Windows 下使用反斜杠）。你可以传递给任何需要字符串形式路径的函数。

>>> p = PurePath('/etc')
>>> str(p)
'/etc'
>>> p = PureWindowsPath('c:/Program Files')
>>> str(p)
'c:\\Program Files'

类似地，在路径上调用 bytes 将原始文件系统路径作为字节对象给出，就像被 os.fsencode() 编码一样:

>>> bytes(p)
b'/etc'

注解

只推荐在 Unix 下调用 bytes。在 Windows， unicode 形式是文件系统路径的规范表示法。

11.1.2.3. 访问个别部分

为了访问路径独立的部分 （组件），使用以下特征属性：

PurePath.parts

一个元组，可以访问路径的多个组件:

>>> p = PurePath('/usr/bin/python3')
>>> p.parts
('/', 'usr', 'bin', 'python3')

>>> p = PureWindowsPath('c:/Program Files/PSF')
>>> p.parts
('c:\\', 'Program Files', 'PSF')

（注意盘符和本地根目录是如何重组的）

11.1.2.4. 方法和特征属性

纯路径提供以下方法和特征属性：

PurePath.drive

一个表示驱动器盘符或命名的字符串，如果存在:

>>> PureWindowsPath('c:/Program Files/').drive
'c:'
>>> PureWindowsPath('/Program Files/').drive
''
>>> PurePosixPath('/etc').drive
''

UNC 分享也被认作驱动器:

>>> PureWindowsPath('//host/share/foo.txt').drive
'\\\\host\\share'

PurePath.root

一个表示（本地或全局）根的字符串，如果存在:

>>> PureWindowsPath('c:/Program Files/').root
'\\'
>>> PureWindowsPath('c:Program Files/').root
''
>>> PurePosixPath('/etc').root
'/'

UNC 分享一样拥有根:

>>> PureWindowsPath('//host/share').root
'\\'

PurePath.anchor

驱动器和根的联合:

>>> PureWindowsPath('c:/Program Files/').anchor
'c:\\'
>>> PureWindowsPath('c:Program Files/').anchor
'c:'
>>> PurePosixPath('/etc').anchor
'/'
>>> PureWindowsPath('//host/share').anchor
'\\\\host\\share\\'

PurePath.parents

An immutable sequence providing access to the logical ancestors of the path:

>>> p = PureWindowsPath('c:/foo/bar/setup.py')
>>> p.parents[0]
PureWindowsPath('c:/foo/bar')
>>> p.parents[1]
PureWindowsPath('c:/foo')
>>> p.parents[2]
PureWindowsPath('c:/')

PurePath.parent

此路径的逻辑父路径:

>>> p = PurePosixPath('/a/b/c/d')
>>> p.parent
PurePosixPath('/a/b/c')

你不能超过一个 anchor 或空路径:

>>> p = PurePosixPath('/')
>>> p.parent
PurePosixPath('/')
>>> p = PurePosixPath('.')
>>> p.parent
PurePosixPath('.')

注解

这是一个单纯的词法操作，因此有以下行为:

>>> p = PurePosixPath('foo/..')
>>> p.parent
PurePosixPath('foo')

如果你想要向上移动任意文件系统路径，推荐先使用 Path.resolve() 来解析符号链接以及消除 ".." 组件。

PurePath.name

一个表示最后路径组件的字符串，排除了驱动器与根目录，如果存在的话:

>>> PurePosixPath('my/library/setup.py').name
'setup.py'

UNC 驱动器名不被考虑:

>>> PureWindowsPath('//some/share/setup.py').name
'setup.py'
>>> PureWindowsPath('//some/share').name
''

PurePath.suffix

最后一个组件的文件扩展名，如果存在:

>>> PurePosixPath('my/library/setup.py').suffix
'.py'
>>> PurePosixPath('my/library.tar.gz').suffix
'.gz'
>>> PurePosixPath('my/library').suffix
''

PurePath.suffixes

路径的文件扩展名列表:

>>> PurePosixPath('my/library.tar.gar').suffixes
['.tar', '.gar']
>>> PurePosixPath('my/library.tar.gz').suffixes
['.tar', '.gz']
>>> PurePosixPath('my/library').suffixes
[]

PurePath.stem

最后一个路径组件，除去后缀:

>>> PurePosixPath('my/library.tar.gz').stem
'library.tar'
>>> PurePosixPath('my/library.tar').stem
'library'
>>> PurePosixPath('my/library').stem
'library'

PurePath.as_posix()

返回使用正斜杠（/）的路径字符串:

>>> p = PureWindowsPath('c:\\windows')
>>> str(p)
'c:\\windows'
>>> p.as_posix()
'c:/windows'

PurePath.as_uri()

将路径表示为 file URL。如果并非绝对路径，抛出 ValueError。

>>> p = PurePosixPath('/etc/passwd')
>>> p.as_uri()
'file:///etc/passwd'
>>> p = PureWindowsPath('c:/Windows')
>>> p.as_uri()
'file:///c:/Windows'

PurePath.is_absolute()

返回此路径是否为绝对路径。如果路径同时拥有驱动器符与根路径（如果风格允许）则将被认作绝对路径。

>>> PurePosixPath('/a/b').is_absolute()
True
>>> PurePosixPath('a/b').is_absolute()
False

>>> PureWindowsPath('c:/a/b').is_absolute()
True
>>> PureWindowsPath('/a/b').is_absolute()
False
>>> PureWindowsPath('c:').is_absolute()
False
>>> PureWindowsPath('//some/share').is_absolute()
True

PurePath.is_reserved()

在 PureWindowsPath，如果路径是被 Windows 保留的则返回 True，否则 False。在 PurePosixPath，总是返回 False。

>>> PureWindowsPath('nul').is_reserved()
True
>>> PurePosixPath('nul').is_reserved()
False

当保留路径上的文件系统被调用，则可能出现玄学失败或者意料之外的效应。

PurePath.joinpath(*other)

调用此方法等同于将每个 other 参数中的项目连接在一起:

>>> PurePosixPath('/etc').joinpath('passwd')
PurePosixPath('/etc/passwd')
>>> PurePosixPath('/etc').joinpath(PurePosixPath('passwd'))
PurePosixPath('/etc/passwd')
>>> PurePosixPath('/etc').joinpath('init.d', 'apache2')
PurePosixPath('/etc/init.d/apache2')
>>> PureWindowsPath('c:').joinpath('/Program Files')
PureWindowsPath('c:/Program Files')

PurePath.match(pattern)

将此路径与提供的通配符风格的模式匹配。如果匹配成功则返回 True，否则返回 False。

如果 pattern 是相对的，则路径可以是相对路径或绝对路径，并且匹配是从右侧完成的：

>>> PurePath('a/b.py').match('*.py')
True
>>> PurePath('/a/b/c.py').match('b/*.py')
True
>>> PurePath('/a/b/c.py').match('a/*.py')
False

如果 pattern 是绝对的，则路径必须是绝对的，并且路径必须完全匹配:

>>> PurePath('/a.py').match('/*.py')
True
>>> PurePath('a/b.py').match('/*.py')
False

As with other methods, case-sensitivity is observed:

>>> PureWindowsPath('b.py').match('*.PY')
True

PurePath.relative_to(*other)

计算此路径相对 other 表示路径的版本。如果不可计算，则抛出 ValueError:

>>> p = PurePosixPath('/etc/passwd')
>>> p.relative_to('/')
PurePosixPath('etc/passwd')
>>> p.relative_to('/etc')
PurePosixPath('passwd')
>>> p.relative_to('/usr')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pathlib.py", line 694, in relative_to
    .format(str(self), str(formatted)))
ValueError: '/etc/passwd' does not start with '/usr'

PurePath.with_name(name)

返回一个新的路径并修改 name。如果原本路径没有 name，ValueError 被抛出:

>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
>>> p.with_name('setup.py')
PureWindowsPath('c:/Downloads/setup.py')
>>> p = PureWindowsPath('c:/')
>>> p.with_name('setup.py')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/antoine/cpython/default/Lib/pathlib.py", line 751, in with_name
    raise ValueError("%r has an empty name" % (self,))
ValueError: PureWindowsPath('c:/') has an empty name

PurePath.with_suffix(suffix)

返回一个新的路径并修改 suffix。如果原本的路径没有后缀，新的 suffix 则被追加以代替。如果 suffix 是空字符串，则原本的后缀被移除:

>>> p = PureWindowsPath('c:/Downloads/pathlib.tar.gz')
>>> p.with_suffix('.bz2')
PureWindowsPath('c:/Downloads/pathlib.tar.bz2')
>>> p = PureWindowsPath('README')
>>> p.with_suffix('.txt')
PureWindowsPath('README.txt')
>>> p = PureWindowsPath('README.txt')
>>> p.with_suffix('')
PureWindowsPath('README')

11.1.3. 具体路径

具体路径是纯路径的子类。除了后者提供的操作之外，它们还提供了对路径对象进行系统调用的方法。有三种方法可以实例化具体路径:

class pathlib.Path(*pathsegments)

一个 PurePath 的子类，此类以当前系统的路径风格表示路径（实例化为 PosixPath 或 WindowsPath）:

>>> Path('setup.py')
PosixPath('setup.py')

pathsegments 参数的指定和 PurePath 相同。

class pathlib.PosixPath(*pathsegments)

一个 Path 和 PurePosixPath 的子类，此类表示一个非 Windows 文件系统的具体路径:

>>> PosixPath('/etc')
PosixPath('/etc')

pathsegments 参数的指定和 PurePath 相同。

class pathlib.WindowsPath(*pathsegments)

Path 和 PureWindowsPath 的子类，从类表示一个 Windows 文件系统的具体路径:

>>> WindowsPath('c:/Program Files/')
WindowsPath('c:/Program Files')

pathsegments 参数的指定和 PurePath 相同。

你只能实例化与当前系统风格相同的类（允许系统调用作用于不兼容的路径风格可能在应用程序中导致缺陷或失败）:

>>> import os
>>> os.name
'posix'
>>> Path('setup.py')
PosixPath('setup.py')
>>> PosixPath('setup.py')
PosixPath('setup.py')
>>> WindowsPath('setup.py')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pathlib.py", line 798, in __new__
    % (cls.__name__,))
NotImplementedError: cannot instantiate 'WindowsPath' on your system

11.1.3.1. 方法

除纯路径方法外，具体路径还提供以下方法。如果系统调用失败（例如因为路径不存在），其中许多方法都会引发 OSError：

classmethod Path.cwd()

返回一个新的表示当前目录的路径对象（和 os.getcwd() 返回的相同）:

>>> Path.cwd()
PosixPath('/home/antoine/pathlib')

classmethod Path.home()

返回一个表示当前用户家目录的新路径对象（和 os.path.expanduser() 构造含 ~ 路径返回的相同）:

>>> Path.home()
PosixPath('/home/antoine')

3.5 新版功能.

Path.stat()

Return information about this path (similarly to os.stat()). The result is looked up at each call to this method.

>>> p = Path('setup.py')
>>> p.stat().st_size
956
>>> p.stat().st_mtime
1327883547.852554

Path.chmod(mode)

改变文件的模式和权限，和 os.chmod() 一样:

>>> p = Path('setup.py')
>>> p.stat().st_mode
33277
>>> p.chmod(0o444)
>>> p.stat().st_mode
33060

Path.exists()

此路径是否指向一个已存在的文件或目录:

>>> Path('.').exists()
True
>>> Path('setup.py').exists()
True
>>> Path('/etc').exists()
True
>>> Path('nonexistentfile').exists()
False

注解

如果路径指向一个符号链接， exists() 返回此符号链接是否指向存在的文件或目录。

Path.expanduser()

返回展开了包含 ~ 和 ~user 的构造，就和 os.path.expanduser() 一样:

>>> p = PosixPath('~/films/Monty Python')
>>> p.expanduser()
PosixPath('/home/eric/films/Monty Python')

3.5 新版功能.

Path.glob(pattern)

Glob the given pattern in the directory represented by this path, yielding all matching files (of any kind):

>>> sorted(Path('.').glob('*.py'))
[PosixPath('pathlib.py'), PosixPath('setup.py'), PosixPath('test_pathlib.py')]
>>> sorted(Path('.').glob('*/*.py'))
[PosixPath('docs/conf.py')]

“**” 模式表示 “此目录以及所有子目录，递归”。换句话说，它启用递归通配:

>>> sorted(Path('.').glob('**/*.py'))
[PosixPath('build/lib/pathlib.py'),
 PosixPath('docs/conf.py'),
 PosixPath('pathlib.py'),
 PosixPath('setup.py'),
 PosixPath('test_pathlib.py')]

注解

在一个较大的目录树中使用 “**” 模式可能会消耗非常多的时间。

Path.group()

返回拥有此文件的用户组。如果文件的 GID 无法在系统数据库中找到，将抛出 KeyError 。

Path.is_dir()

如果路径指向一个目录（或者一个指向目录的符号链接）则返回 True，如果指向其他类型的文件则返回 False。

当路径不存在或者是一个破损的符号链接时也会返回 False；其他错误（例如权限错误）被传播。

Path.is_file()

如果路径指向一个正常的文件（或者一个指向正常文件的符号链接）则返回 True，如果指向其他类型的文件则返回 False。

当路径不存在或者是一个破损的符号链接时也会返回 False；其他错误（例如权限错误）被传播。

Path.is_symlink()

如果路径指向符号链接则返回 True， 否则 False。

如果路径不存在也返回 False；其他错误（例如权限错误）被传播。

Path.is_socket()

如果路径指向一个 Unix socket 文件（或者指向 Unix socket 文件的符号链接）则返回 True，如果指向其他类型的文件则返回 False。

当路径不存在或者是一个破损的符号链接时也会返回 False；其他错误（例如权限错误）被传播。

Path.is_fifo()

如果路径指向一个先进先出存储（或者指向先进先出存储的符号链接）则返回 True ，指向其他类型的文件则返回 False。

当路径不存在或者是一个破损的符号链接时也会返回 False；其他错误（例如权限错误）被传播。

Path.is_block_device()

如果文件指向一个块设备（或者指向块设备的符号链接）则返回 True，指向其他类型的文件则返回 False。

当路径不存在或者是一个破损的符号链接时也会返回 False；其他错误（例如权限错误）被传播。

Path.is_char_device()

如果路径指向一个字符设备（或指向字符设备的符号链接）则返回 True，指向其他类型的文件则返回 False。

当路径不存在或者是一个破损的符号链接时也会返回 False；其他错误（例如权限错误）被传播。

Path.iterdir()

当路径指向一个目录时，产生该路径下的对象的路径:

>>> p = Path('docs')
>>> for child in p.iterdir(): child
...
PosixPath('docs/conf.py')
PosixPath('docs/_templates')
PosixPath('docs/make.bat')
PosixPath('docs/index.rst')
PosixPath('docs/_build')
PosixPath('docs/_static')
PosixPath('docs/Makefile')

Path.lchmod(mode)

就像 Path.chmod() 但是如果路径指向符号链接则是修改符号链接的模式，而不是修改符号链接的目标。

Path.lstat()

就和 Path.stat() 一样，但是如果路径指向符号链接，则是返回符号链接而不是目标的信息。

Path.mkdir(mode=0o777, parents=False, exist_ok=False)

新建给定路径的目录。如果给出了 mode ，它将与当前进程的 umask 值合并来决定文件模式和访问标志。如果路径已经存在，则抛出 FileExistsError。

如果 parents 为 true，任何找不到的父目录都会伴随着此路径被创建；它们会以默认权限被创建，而不考虑 mode 设置（模仿 POSIX 的 mkdir -p 命令）。

如果 parents 为 false（默认），则找不到的父级目录会导致 FileNotFoundError 被抛出。

如果 exist_ok 为 false（默认），则在目标已存在的情况下抛出 FileExistsError。

如果 exist_ok 为 true， 则 FileExistsError 异常将被忽略（和 POSIX mkdir -p 命令行为相同），但是只有在最后一个路径组件不是现存的非目录文件时才生效。

在 3.5 版更改: exist_ok 形参被加入。

Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None)

打开路径指向的文件，就像内置的 open() 函数所做的一样:

>>> p = Path('setup.py')
>>> with p.open() as f:
...     f.readline()
...
'#!/usr/bin/env python3\n'

Path.owner()

返回拥有此文件的用户名。如果文件的 UID 无法在系统数据库中找到，则抛出 KeyError。

Path.read_bytes()

以字节对象的形式返回路径指向的文件的二进制内容:

>>> p = Path('my_binary_file')
>>> p.write_bytes(b'Binary file contents')
20
>>> p.read_bytes()
b'Binary file contents'

3.5 新版功能.

Path.read_text(encoding=None, errors=None)

以字符串形式返回路径指向的文件的解码后文本内容。

>>> p = Path('my_text_file')
>>> p.write_text('Text file contents')
18
>>> p.read_text()
'Text file contents'

文件先被打开然后关闭。有和 open() 一样的可选形参。

3.5 新版功能.

Path.rename(target)

使用给定的 target 将文件重命名。在 Unix 上，如果 target 已经存在并且为文件，则只要用户拥有权限，其将被静默地被覆盖。 target 可以是一个字符串或者另一个路径对象:

>>> p = Path('foo')
>>> p.open('w').write('some text')
9
>>> target = Path('bar')
>>> p.rename(target)
>>> target.open().read()
'some text'

Path.replace(target)

使用给定的 target 重命名文件或目录。如果 target 指向现存的文件或目录，则将被无条件覆盖。

Path.resolve(strict=False)

将路径绝对化，解析任何符号链接。返回新的路径对象:

>>> p = Path()
>>> p
PosixPath('.')
>>> p.resolve()
PosixPath('/home/antoine/pathlib')

“..” 组件也将被消除（只有这一种方法这么做）:

>>> p = Path('docs/../setup.py')
>>> p.resolve()
PosixPath('/home/antoine/pathlib/setup.py')

如果路径不存在并且 strict 设为 True，则抛出 FileNotFoundError。如果 strict 为 False，则路径将被尽可能地解析并且任何剩余部分都会被不检查是否存在地追加。如果在解析路径上发生无限循环，则抛出 RuntimeError。

3.6 新版功能: The strict argument.

Path.rglob(pattern)

This is like calling Path.glob() with “**” added in front of the given pattern:

>>> sorted(Path().rglob("*.py"))
[PosixPath('build/lib/pathlib.py'),
 PosixPath('docs/conf.py'),
 PosixPath('pathlib.py'),
 PosixPath('setup.py'),
 PosixPath('test_pathlib.py')]

Path.rmdir()

移除此目录。此目录必须为空的。

Path.samefile(other_path)

返回此目录是否指向与可能是字符串或者另一个路径对象的 other_path 相同的文件。语义类似于 os.path.samefile() 与 os.path.samestat()。

如果两者都以同一原因无法访问，则抛出 OSError。

>>> p = Path('spam')
>>> q = Path('eggs')
>>> p.samefile(q)
False
>>> p.samefile('spam')
True

3.5 新版功能.

Path.symlink_to(target, target_is_directory=False)

将此路径创建为指向 target 的符号链接。在 Windows 下，如果链接的目标是一个目录则 target_is_directory 必须为 true （默认为 False）。在 POSIX 下， target_is_directory 的值将被忽略。

>>> p = Path('mylink')
>>> p.symlink_to('setup.py')
>>> p.resolve()
PosixPath('/home/antoine/pathlib/setup.py')
>>> p.stat().st_size
956
>>> p.lstat().st_size
8

注解

参数的顺序（link, target) 和 os.symlink() 是相反的。

Path.touch(mode=0o666, exist_ok=True)

将给定的路径创建为文件。如果给出了 mode 它将与当前进程的 umask 值合并以确定文件的模式和访问标志。如果文件已经存在，则当 exist_ok 为 true 则函数仍会成功（并且将它的修改事件更新为当前事件），否则抛出 FileExistsError。

Path.unlink()

移除此文件或符号链接。如果路径指向目录，则用 Path.rmdir() 代替。

Path.write_bytes(data)

将文件以二进制模式打开，写入 data 并关闭:

>>> p = Path('my_binary_file')
>>> p.write_bytes(b'Binary file contents')
20
>>> p.read_bytes()
b'Binary file contents'

一个同名的现存文件将被覆盖。

3.5 新版功能.

Path.write_text(data, encoding=None, errors=None)

将文件以文本模式打开，写入 data 并关闭:

>>> p = Path('my_text_file')
>>> p.write_text('Text file contents')
18
>>> p.read_text()
'Text file contents'

3.5 新版功能.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

11.2. os.path — 常见路径操作

源代码: Lib/posixpath.py [https://github.com/python/cpython/tree/3.6/Lib/posixpath.py] (适用 POSIX), Lib/ntpath.py [https://github.com/python/cpython/tree/3.6/Lib/ntpath.py] (适用 Windows NT), and Lib/macpath.py [https://github.com/python/cpython/tree/3.6/Lib/macpath.py] (适用 Macintosh)

该模块在路径名上实现了一些有用的功能：如需读取或写入文件，请参见 open() ；有关访问文件系统的信息，请参见 os 模块。路径参数可以字符串或字节形式传递。我们鼓励应用程序将文件名表示为（Unicode）字符串。不幸的是，某些文件名在Unix上可能无法用字符串表示，因此在Unix上平台上需要支持任意文件名的应用程序，应使用字节对象来表示路径名。反之亦然，在Windows平台上仅使用字节对象，不能表示的所有文件名（以标准 mbcs 编码），因此Windows应用程序应使用字符串对象来访问所有文件。

与unix shell不同，Python不执行任何 自动 路径扩展。当应用程序需要类似shell的路径扩展时，可以显式调用诸如 expanduser() 和 expandvars() 之类的函数。 （另请参见 glob 模块。）

参见

pathlib 模块提供高级路径对象。

注解

所有这些函数都仅接受字节或字符串对象作为其参数。如果返回路径或文件名，则结果是相同类型的对象。

注解

由于不同的操作系统具有不同的路径名称约定，因此标准库中有此模块的几个版本。 os.path 模块始终是适合Python运行的操作系统的路径模块，因此可用于本地路径。但是，如果操作的路径 总是 以一种不同的格式显示，那么也可以分别导入和使用各个模块。它们都具有相同的界面：

posixpath 用于Unix 样式的路径

ntpath 用于 Windows 路径

macpath 用于旧 MacOS 样式的路径

os.path.abspath(path)

返回路径 path 的绝对路径（标准化的）。在大多数平台上，这等同于用 normpath(join(os.getcwd(), path)) 的方式调用 normpath() 函数。

在 3.6 版更改: 接受一个 path-like object。

os.path.basename(path)

返回路径 path 的基本名称。这是将 path 传入函数 split() 之后，返回的一对值中的第二个元素。请注意，此函数的结果与Unix basename 程序不同。basename 在 '/foo/bar/' 上返回 'bar'，而 basename() 函数返回一个空字符串 ('')。

在 3.6 版更改: 接受一个 path-like object。

os.path.commonpath(paths)

Return the longest common sub-path of each pathname in the sequence paths. Raise ValueError if paths contains both absolute and relative pathnames, or if paths is empty. Unlike commonprefix(), this returns a valid path.

Availability: Unix, Windows

3.5 新版功能.

在 3.6 版更改: 接受一个 类路径对象 序列。

os.path.commonprefix(list)

接受包含多个路径的 列表，返回所有路径的最长公共前缀（逐字符比较）。如果 列表 为空，则返回空字符串 ('')。

注解

此函数是逐字符比较，因此可能返回无效路径。要获取有效路径，参见 commonpath()。

>>> os.path.commonprefix(['/usr/lib', '/usr/local/lib'])
'/usr/l'

>>> os.path.commonpath(['/usr/lib', '/usr/local/lib'])
'/usr'

在 3.6 版更改: 接受一个 path-like object。

os.path.dirname(path)

返回路径 path 的目录名称。这是将 path 传入函数 split() 之后，返回的一对值中的第一个元素。

在 3.6 版更改: 接受一个 path-like object。

os.path.exists(path)

如果 path 指向一个已存在的路径或已打开的文件描述符，返回 True。对于失效的符号链接，返回 False。在某些平台上，如果使用 os.stat() 查询到目标文件没有执行权限，即使 path 确实存在，本函数也可能返回 False。

在 3.3 版更改: path 现在可以是一个整数：如果该整数是一个已打开的文件描述符，返回 True，否则返回 False。

在 3.6 版更改: 接受一个 path-like object。

os.path.lexists(path)

如果 path 指向一个已存在的路径，返回 True。对于失效的符号链接，返回 False。在缺失 os.lstat() 的平台上等同于 exists()。

在 3.6 版更改: 接受一个 path-like object。

os.path.expanduser(path)

在 Unix 和 Windows 上，将参数中开头部分的 ~ 或 ~user 替换为当前 用户 的家目录并返回。

在 Unix 上，开头的 ~ 会被环境变量 HOME 代替，如果变量未设置，则通过内置模块 pwd 在 password 目录中查找当前用户的主目录。以 ~user 开头则直接在 password 目录中查找。

在 Windows 上，如果设置了 HOME 和 USERPROFILE 则将使用它们，否则将使用 HOMEPATH 和 HOMEDRIVE 的组合。 原本的 ~user 处理方式为从上述方法所生成的用户路径中截去最后一级目录。

如果展开路径失败，或者路径不是以波浪号开头，则路径将保持不变。

在 3.6 版更改: 接受一个 path-like object。

os.path.expandvars(path)

输入带有环境变量的路径作为参数，返回展开变量以后的路径。$name 或 ${name} 形式的子字符串被环境变量 name 的值替换。格式错误的变量名称和对不存在变量的引用保持不变。

在 Windows 上，除了 $name 和 ${name} 外，还可以展开 %name%。

在 3.6 版更改: 接受一个 path-like object。

os.path.getatime(path)

Return the time of last access of path. The return value is a number giving the number of seconds since the epoch (see the time module). Raise OSError if the file does not exist or is inaccessible.

If os.stat_float_times() returns True, the result is a floating point number.

os.path.getmtime(path)

Return the time of last modification of path. The return value is a number giving the number of seconds since the epoch (see the time module). Raise OSError if the file does not exist or is inaccessible.

If os.stat_float_times() returns True, the result is a floating point number.

在 3.6 版更改: 接受一个 path-like object。

os.path.getctime(path)

返回 path 在系统中的 ctime，在有些系统（比如 Unix）上，它是元数据的最后修改时间，其他系统（比如 Windows）上，它是 path 的创建时间。返回值是一个数，为纪元秒数（参见 time 模块）。如果该文件不存在或不可访问，则抛出 OSError 异常。

在 3.6 版更改: 接受一个 path-like object。

os.path.getsize(path)

返回 path 的大小，以字节为单位。如果该文件不存在或不可访问，则抛出 OSError 异常。

在 3.6 版更改: 接受一个 path-like object。

os.path.isabs(path)

如果 path 是一个绝对路径，则返回 True。在 Unix 上，它就是以斜杠开头，而在 Windows 上，它可以是去掉驱动器号后以斜杠（或反斜杠）开头。

在 3.6 版更改: 接受一个 path-like object。

os.path.isfile(path)

如果 path 是 现有的 常规文件，则返回 True。本方法会跟踪符号链接，因此，对于同一路径，islink() 和 isfile() 都可能为 True。

在 3.6 版更改: 接受一个 path-like object。

os.path.isdir(path)

如果 path 是 现有的 目录，则返回 True。本方法会跟踪符号链接，因此，对于同一路径，islink() 和 isdir() 都可能为 True。

在 3.6 版更改: 接受一个 path-like object。

os.path.islink(path)

如果 path 指向的 现有 目录条目是一个符号链接，则返回 True。如果 Python 运行时不支持符号链接，则总是返回 False。

在 3.6 版更改: 接受一个 path-like object。

os.path.ismount(path)

Return True if pathname path is a mount point: a point in a file system where a different file system has been mounted. On POSIX, the function checks whether path’s parent, path/.., is on a different device than path, or whether path/.. and path point to the same i-node on the same device — this should detect mount points for all Unix and POSIX variants. On Windows, a drive letter root and a share UNC are always mount points, and for any other path GetVolumePathName is called to see if it is different from the input path.

3.4 新版功能: 支持在Windows上检测非根安装点。

在 3.6 版更改: 接受一个 path-like object。

os.path.join(path, *paths)

合理地拼接一个或多个路径部分。返回值是 path 和 *paths 所有值的连接，每个非空部分后面都紧跟一个目录分隔符 (os.sep)，除了最后一部分。这意味着如果最后一部分为空，则结果将以分隔符结尾。如果参数中某个部分是绝对路径，则绝对路径前的路径都将被丢弃，并从绝对路径部分开始连接。

在 Windows 上，遇到绝对路径部分（例如 r'\foo'）时，不会重置盘符。如果某部分路径包含盘符，则会丢弃所有先前的部分，并重置盘符。请注意，由于每个驱动器都有一个“当前目录”，所以 os.path.join("c:", "foo") 表示驱动器 C: 上当前目录的相对路径 (c:foo)，而不是 c:\foo。

在 3.6 版更改: 接受一个 类路径对象 用于 path 和 paths 。

os.path.normcase(path)

Normalize the case of a pathname. On Unix and Mac OS X, this returns the path unchanged; on case-insensitive filesystems, it converts the path to lowercase. On Windows, it also converts forward slashes to backward slashes. Raise a TypeError if the type of path is not str or bytes (directly or indirectly through the os.PathLike interface).

在 3.6 版更改: 接受一个 path-like object。

os.path.normpath(path)

通过折叠多余的分隔符和对上级目录的引用来标准化路径名，所以 A//B、A/B/、A/./B 和 A/foo/../B 都会转换成 A/B。这个字符串操作可能会改变带有符号链接的路径的含义。在 Windows 上，本方法将正斜杠转换为反斜杠。要规范大小写，请使用 normcase()。

在 3.6 版更改: 接受一个 path-like object。

os.path.realpath(path)

返回指定文件的规范路径，消除路径中存在的任何符号链接（如果操作系统支持）。

在 3.6 版更改: 接受一个 path-like object。

os.path.relpath(path, start=os.curdir)

返回从当前目录或 start 目录（可选）到达 path 之间要经过的相对路径。这仅仅是对路径的计算，不会访问文件系统来确认 path 或 start 的存在性或属性。

开始 默认为 os.curdir

Availability: Unix, Windows.

在 3.6 版更改: 接受一个 path-like object。

os.path.samefile(path1, path2)

如果两个路径都指向相同的文件或目录，则返回 True。这由设备号和 inode 号确定，在任一路径上调用 os.stat() 失败则抛出异常。

Availability: Unix, Windows.

在 3.2 版更改: 添加了Windows 支持

在 3.4 版更改: Windows现在使用与其他所有平台相同的实现。

在 3.6 版更改: 接受一个 path-like object。

os.path.sameopenfile(fp1, fp2)

如果文件描述符 fp1 和 fp2 指向相同文件，则返回 True。

Availability: Unix, Windows.

在 3.2 版更改: 添加了Windows 支持

在 3.6 版更改: 接受一个 path-like object。

os.path.samestat(stat1, stat2)

如果 stat 元组 stat1 和 stat2 指向相同文件，则返回 True。这些 stat 元组可能是由 os.fstat()、os.lstat() 或 os.stat() 返回的。本函数实现了 samefile() 和 sameopenfile() 底层所使用的比较过程。

Availability: Unix, Windows.

在 3.4 版更改: 添加了Windows 支持

在 3.6 版更改: 接受一个 path-like object。

os.path.split(path)

将路径 path 拆分为一对，即 (head, tail)，其中，tail 是路径的最后一部分，而 head 里是除最后部分外的所有内容。tail 部分不会包含斜杠，如果 path 以斜杠结尾，则 tail 将为空。如果 path 中没有斜杠，head 将为空。如果 path 为空，则 head 和 tail 均为空。head 末尾的斜杠会被去掉，除非它是根目录（即它仅包含一个或多个斜杠）。在所有情况下，join(head, tail) 指向的位置都与 path 相同（但字符串可能不同）。另请参见函数 dirname() 和 basename()。

在 3.6 版更改: 接受一个 path-like object。

os.path.splitdrive(path)

将路径 path 拆分为一对，即 (drive, tail)，其中 drive 是挂载点或空字符串。在没有驱动器概念的系统上，drive 将始终为空字符串。在所有情况下，drive + tail 都与 path 相同。

在 Windows 上，本方法将路径拆分为驱动器/UNC 根节点和相对路径。

如果路径 path 包含盘符，则 drive 将包含冒号及冒号前面的所有内容。例如 splitdrive("c:/dir") 返回 ("c:", "/dir")。

如果 path 是一个 UNC 路径，则 drive 将包含主机名和共享点，但不包括第四个分隔符。例如 splitdrive("//host/computer/dir") 返回 ("//host/computer", "/dir")。

在 3.6 版更改: 接受一个 path-like object。

os.path.splitext(path)

将路径 path 拆分为一对，即 (root, ext)，使 root + ext == path，其中 ext 为空或以英文句点开头，且最多包含一个句点。路径前的句点将被忽略，例如 splitext('.cshrc') 返回 ('.cshrc', '')。

在 3.6 版更改: 接受一个 path-like object。

os.path.splitunc(path)

3.1 版后已移除: Use splitdrive instead.

Split the pathname path into a pair (unc, rest) so that unc is the UNC mount point (such as r'\\host\mount'), if present, and rest the rest of the path (such as r'\path\file.ext'). For paths containing drive letters, unc will always be the empty string.

Availability: Windows.

os.path.supports_unicode_filenames

如果（在文件系统限制下）允许将任意 Unicode 字符串用作文件名，则为 True。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

11.3. fileinput — 迭代来自多个输入流的行

源代码: Lib/fileinput.py [https://github.com/python/cpython/tree/3.6/Lib/fileinput.py]

此模块实现了一个辅助类和一些函数用来快速编写访问标准输入或文件列表的循环。 如果你只想要读写一个文件请参阅 open()。

典型用法为:

import fileinput
for line in fileinput.input():
    process(line)

This iterates over the lines of all files listed in sys.argv[1:], defaulting to sys.stdin if the list is empty. If a filename is '-', it is also replaced by sys.stdin. To specify an alternative list of filenames, pass it as the first argument to input(). A single file name is also allowed.

所有文件都默认以文本模式打开，但你可以通过在调用 input() 或 FileInput 时指定 mode 形参来重载此行为。 如果在打开或读取文件时发生了 I/O 错误，将会引发 OSError。

在 3.3 版更改: 原来会引发 IOError；现在它是 OSError 的别名。

如果 sys.stdin 被使用超过一次，则第二次之后的使用将不返回任何行，除非是被交互式的使用，或都是被显式地重置 (例如使用 sys.stdin.seek(0))。

空文件打开后将立即被关闭；它们在文件列表中会被注意到的唯一情况只有当最后打开的文件为空的时候。

反回的行不会对换行符做任何处理，这意味着文件中的最后一行可能不带换行符。

想要控制文件的打开方式，你可以通过将 openhook 形参传给 fileinput.input() 或 FileInput() 来提供一个打开钩子。 此钩子必须为一个函数，它接受两个参数，filename 和 mode，并返回一个以相应模式打开的文件类对象。 此模块已经提供了两个有用的钩子。

以下函数是此模块的初始接口：

fileinput.input(files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None)

创建一个 FileInput 类的实例。 该实例将被用作此模块中函数的全局状态，并且还将在迭代期间被返回使用。 此函数的形参将被继续传递给 FileInput 类的构造器。

The FileInput instance can be used as a context manager in the with statement. In this example, input is closed after the with statement is exited, even if an exception occurs:

with fileinput.input(files=('spam.txt', 'eggs.txt')) as f:
    for line in f:
        process(line)

在 3.2 版更改: 可以被用作上下文管理器。

Deprecated since version 3.6, will be removed in version 3.8: bufsize 形参。

下列函数会使用 fileinput.input() 所创建的全局状态；如果没有活动的状态，则会引发 RuntimeError。

fileinput.filename()

返回当前被读取的文件名。 在第一行被读取之前，返回 None。

fileinput.fileno()

返回以整数表示的当前文件“文件描述符”。 当未打开文件时（处在第一行和文件之间），返回 -1。

fileinput.lineno()

返回已被读取的累计行号。 在第一行被读取之前，返回 0。 在最后一个文件的最后一行被读取之后，返回该行的行号。

fileinput.filelineno()

返回当前文件中的行号。 在第一行被读取之前，返回 0。 在最后一个文件的最后一行被读取之后，返回此文件中该行的行号。

fileinput.isfirstline()

Returns true if the line just read is the first line of its file, otherwise returns false.

fileinput.isstdin()

Returns true if the last line was read from sys.stdin, otherwise returns false.

fileinput.nextfile()

关闭当前文件以使下次迭代将从下一个文件（如果存在）读取第一行；不是从该文件读取的行将不会被计入累计行数。 直到下一个文件的第一行被读取之后文件名才会改变。 在第一行被读取之前，此函数将不会生效；它不能被用来跳过第一个文件。 在最后一个文件的最后一行被读取之后，此函数将不再生效。

fileinput.close()

关闭序列。

此模块所提供的实现了序列行为的类同样也可用于子类化：

class fileinput.FileInput(files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None)

类 FileInput 是一个实现；它的方法 filename(), fileno(), lineno(), filelineno(), isfirstline(), isstdin(), nextfile() 和 close() 对应于此模块中具有相同名称的函数。 此外它还有一个 readline() 方法可返回下一个输入行，以及一个 __getitem__() 方法，该方法实现了序列行为。 这种序列必须以严格的序列顺序来读写；随机读写和 readline() 不可以被混用。

通过 mode 你可以指定要传给 open() 的文件模式。 它必须为 'r', 'rU', 'U' 和 'rb' 中的一个。

openhook 如果给出则必须为一个函数，它接受两个参数 filename 和 mode，并相应地返回一个打开的文件类对象。 你不能同时使用 inplace 和 openhook。

A FileInput instance can be used as a context manager in the with statement. In this example, input is closed after the with statement is exited, even if an exception occurs:

with FileInput(files=('spam.txt', 'eggs.txt')) as input:
    process(input)

在 3.2 版更改: 可以被用作上下文管理器。

3.4 版后已移除: 'rU' 和 'U' 模式。

Deprecated since version 3.6, will be removed in version 3.8: bufsize 形参。

可选的原地过滤: 如果传递了关键字参数 inplace=True 给 fileinput.input() 或 FileInput 构造器，则文件会被移至备份文件并将标准输出定向到输入文件（如果已存在与备份文件同名的文件，它将被静默地替换）。 这使得编写一个能够原地重写其输入文件的过滤器成为可能。 如果给出了 backup 形参 (通常形式为 backup='.<some extension>')，它将指定备份文件的扩展名，并且备份文件会被保留；默认情况下扩展名为 '.bak' 并且它会在输出文件关闭时被删除。 在读取标准输入时原地过滤会被禁用。

此模块提供了以下两种打开文件钩子：

fileinput.hook_compressed(filename, mode)

使用 gzip 和 bz2 模块透明地打开 gzip 和 bzip2 压缩的文件（通过扩展名 '.gz' 和 '.bz2' 来识别）。 如果文件扩展名不是 '.gz' 或 '.bz2'，文件会以正常方式打开（即使用 open() 并且不带任何解压操作）。

使用示例: fi = fileinput.FileInput(openhook=fileinput.hook_compressed)

fileinput.hook_encoded(encoding, errors=None)

返回一个通过 open() 打开每个文件的钩子，使用给定的 encoding 和 errors 来读取文件。

使用示例: fi = fileinput.FileInput(openhook=fileinput.hook_encoded("utf-8", "surrogateescape"))

在 3.6 版更改: 添加了可选的 errors 形参。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

11.4. stat — 解析 stat() 结果

源代码： Lib/stat.py [https://github.com/python/cpython/tree/3.6/Lib/stat.py]

stat 模块定义了一些用于解析 os.stat(), os.fstat() 和 os.lstat() (如果它们存在) 输出结果的常量和函数。 有关 stat(), fstat() 和 lstat() 调用的完整细节，请参阅你的系统文档。

在 3.4 版更改: stat 模块是通过 C 实现来支持的。

stat 模块定义了以下函数来检测特定文件类型：

stat.S_ISDIR(mode)

如果 mode 来自一个目录则返回非零值。

stat.S_ISCHR(mode)

如果 mode 来自一个字符专属的设备文件则返回非零值。

stat.S_ISBLK(mode)

如果 mode 来自一个块特殊设备文件则返回非零值。

stat.S_ISREG(mode)

如果 mode 来自一个常规文件则返回非零值。

stat.S_ISFIFO(mode)

如果 mode 来自一个 FIFO (命名管道) 则返回非零值。

stat.S_ISLNK(mode)

如果 mode 来自一个符号链接则返回非零值。

stat.S_ISSOCK(mode)

如果 mode 来自一个套接字则返回非零值。

stat.S_ISDOOR(mode)

如果 mode 来自一个门则返回非零值。

3.4 新版功能.

stat.S_ISPORT(mode)

如果 mode 来自一个事件端口则返回非零值。

3.4 新版功能.

stat.S_ISWHT(mode)

如果 mode 来自一个白输出则返回非零值。

3.4 新版功能.

定义了两个附加函数用于对文件模式进行更一般化的操作：

stat.S_IMODE(mode)

返回文件模式中可由 os.chmod() 进行设置的部分 — 即文件的 permission 位，加上 sticky 位、set-group-id 以及 set-user-id 位（在支持这些部分的系统上）。

stat.S_IFMT(mode)

返回文件模式中描述文件类型的部分（供上面的 S_IS*() 函数使用）。

通常，你应当使用 os.path.is*() 函数来检测文件的类型；这里提供的函数则适用于当你要对同一文件执行多项检测并且希望避免每项检测的 stat() 系统调用开销的情况。 这些函数也适用于检测有关未被 os.path 处理的信息，例如检测块和字符设备等。

示例:

import os, sys
from stat import *

def walktree(top, callback):
    '''recursively descend the directory tree rooted at top,
       calling the callback function for each regular file'''

    for f in os.listdir(top):
        pathname = os.path.join(top, f)
        mode = os.stat(pathname).st_mode
        if S_ISDIR(mode):
            # It's a directory, recurse into it
            walktree(pathname, callback)
        elif S_ISREG(mode):
            # It's a file, call the callback function
            callback(pathname)
        else:
            # Unknown file type, print a message
            print('Skipping %s' % pathname)

def visitfile(file):
    print('visiting', file)

if __name__ == '__main__':
    walktree(sys.argv[1], visitfile)

另外还提供了一个附加的辅助函数用来将文件模式转换为人类易读的字符串：

stat.filemode(mode)

将文件模式转换为 ‘-rwxrwxrwx’ 形式的字符串。

3.3 新版功能.

在 3.4 版更改: 此函数支持 S_IFDOOR, S_IFPORT and S_IFWHT。

以下所有变量是一些简单的符号索引，用于访问 os.stat(), os.fstat() 或 os.lstat() 所返回的 10 条目元组。

stat.ST_MODE

inode保护模式。

stat.ST_INO

Inode 号

stat.ST_DEV

Inode 所在的设备。

stat.ST_NLINK

Inode 拥有的链接数量。

stat.ST_UID

所有者的用户ID。

stat.ST_GID

所有者的用户组ID。

stat.ST_SIZE

以字节为单位的普通文件大小；对于某些特殊文件的预期数据量。

stat.ST_ATIME

上次访问的时间。

stat.ST_MTIME

上次修改的时间。

stat.ST_CTIME

操作系统所报告的 “ctime”。 在某些系统上（例如 Unix）是元数据的最后修改时间，而在其他系统上（例如 Windows）则是创建时间（请参阅系统平台的文档了解相关细节）。

对于“文件大小”的解析可因文件类型的不同而变化。 对于普通文件就是文件的字节数。 对于大部分种类的 Unix（特别包括 Linux）的 FIFO 和套接字来说，“大小”则是指在调用 os.stat(), os.fstat() 或 os.lstat() 时等待读取的字节数；这在某些时候很有用处，特别是在一个非阻塞的打开后轮询这些特殊文件中的一个时。 其他字符和块设备的文件大小字段的含义还会有更多变化，具体取决于底层系统调用的实现方式。

以下变量定义了在 ST_MODE 字段中使用的旗标。

使用上面的函数会比使用第一组旗标更容易移植：

stat.S_IFSOCK

套接字

stat.S_IFLNK

符号链接。

stat.S_IFREG

普通文件。

stat.S_IFBLK

块设备

stat.S_IFDIR

目录

stat.S_IFCHR

字符设备。

stat.S_IFIFO

先进先出

stat.S_IFDOOR

门

3.4 新版功能.

stat.S_IFPORT

事件端口。

3.4 新版功能.

stat.S_IFWHT

白输出。

3.4 新版功能.

注解

S_IFDOOR, S_IFPORT or S_IFWHT 等文件类型在不受系统平台支持时会被定义为 0。

以下旗标还可以 os.chmod() 的在 mode 参数中使用：

stat.S_ISUID

设置UID位。

stat.S_ISGID

设置分组 ID 位。 这个位有几种特殊用途。 对于目录它表示该目录将使用 BSD 语义：在其中创建的文件将从目录继承其分组 ID，而不是从创建进程的有效分组 ID 继承，并且在其中创建的目录也将设置 S_ISGID 位。 对于没有设置分组执行位 (S_IXGRP) 的文件，设置分组 ID 位表示强制性文件/记录锁定 (另请参见 S_ENFMT)。

stat.S_ISVTX

固定位。 当对目录设置该位时则意味着此目录中的文件只能由文件所有者、目录所有者或特权进程来重命名或删除。

stat.S_IRWXU

文件所有者权限的掩码。

stat.S_IRUSR

所有者具有读取权限。

stat.S_IWUSR

所有者具有写入权限。

stat.S_IXUSR

所有者具有执行权限。

stat.S_IRWXG

组权限的掩码。

stat.S_IRGRP

组具有读取权限。

stat.S_IWGRP

组具有写入权限。

stat.S_IXGRP

组具有执行权限。

stat.S_IRWXO

其他人（不在组中）的权限掩码。

stat.S_IROTH

其他人具有读取权限。

stat.S_IWOTH

其他人具有写入权限。

stat.S_IXOTH

其他人具有执行权限。

stat.S_ENFMT

System V 执行文件锁定。 此旗标是与 S_ISGID 共享的：文件/记录锁定会针对未设置分组执行位 (S_IXGRP) 的文件强制执行。

stat.S_IREAD

Unix V7 中 S_IRUSR 的同义词。

stat.S_IWRITE

Unix V7 中 S_IWUSR 的同义词。

stat.S_IEXEC

Unix V7 中 S_IXUSR 的同义词。

以下旗标可以在 os.chflags() 的 flags 参数中使用：

stat.UF_NODUMP

不要转储文件。

stat.UF_IMMUTABLE

文件不能更改。

stat.UF_APPEND

文件只能附加到。

stat.UF_OPAQUE

当通过联合堆栈查看时，目录是不透明的。

stat.UF_NOUNLINK

文件不能重命名或删除。

stat.UF_COMPRESSED

文件是压缩存储的（Mac OS X 10.6+）。

stat.UF_HIDDEN

文件不能显示在GUI 中（Mac OS X 10.5+）。

stat.SF_ARCHIVED

文件可能已存档。

stat.SF_IMMUTABLE

文件不能更改。

stat.SF_APPEND

文件只能附加到。

stat.SF_NOUNLINK

文件不能重命名或删除。

stat.SF_SNAPSHOT

文件有一个快照文件

请参阅 *BSD 或 Mac OS 系统的指南页 chflags(2) 了解详情。

在 Windows 上，以下文件属性常量可用于检测 os.stat() 所返回的 st_file_attributes 成员中的位。 请参阅 Windows API 文档 [https://msdn.microsoft.com/en-us/library/windows/desktop/gg258117.aspx] 了解有关这些常量含义的详情。

stat.FILE_ATTRIBUTE_ARCHIVE

stat.FILE_ATTRIBUTE_COMPRESSED

stat.FILE_ATTRIBUTE_DEVICE

stat.FILE_ATTRIBUTE_DIRECTORY

stat.FILE_ATTRIBUTE_ENCRYPTED

stat.FILE_ATTRIBUTE_HIDDEN

stat.FILE_ATTRIBUTE_INTEGRITY_STREAM

stat.FILE_ATTRIBUTE_NORMAL

stat.FILE_ATTRIBUTE_NOT_CONTENT_INDEXED

stat.FILE_ATTRIBUTE_NO_SCRUB_DATA

stat.FILE_ATTRIBUTE_OFFLINE

stat.FILE_ATTRIBUTE_READONLY

stat.FILE_ATTRIBUTE_REPARSE_POINT

stat.FILE_ATTRIBUTE_SPARSE_FILE

stat.FILE_ATTRIBUTE_SYSTEM

stat.FILE_ATTRIBUTE_TEMPORARY

stat.FILE_ATTRIBUTE_VIRTUAL

3.5 新版功能.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

11.5. filecmp — 文件及目录的比较

源代码: Lib/filecmp.py [https://github.com/python/cpython/tree/3.6/Lib/filecmp.py]

filecmp 模块定义了用于比较文件及目录的函数，并且可以选取多种关于时间和准确性的折衷方案。对于文件的比较，另见 difflib 模块。

filecmp 模块定义了如下函数：

filecmp.cmp(f1, f2, shallow=True)

比较名为 f1 和 f2 的文件，如果它们似乎相等则返回 True ，否则返回 False 。

如果 shallow 为真，那么具有相同 os.stat() 签名的文件将会被认为是相等的。否则，将比较文件的内容。

需要注意，没有外部程序被该函数调用，这赋予了该函数可移植性与效率。

该函数会缓存过去的比较及其结果，且在文件的 os.stat() 信息变化后缓存条目失效。所有的缓存可以通过使用 clear_cache() 来清除。

filecmp.cmpfiles(dir1, dir2, common, shallow=True)

比较在两个目录 dir1 和 dir2 中，由 common 所确定名称的文件。

返回三组文件名列表： match, mismatch, errors 。 match 含有相匹配的文件， mismatch 含有那些不匹配的，然后 errors 列出那些未被比较文件的名称。如果文件不存在于两目录中的任一个，或者用户缺少读取它们的权限，又或者因为其他的一些原因而无法比较，那么这些文件将会被列在 errors 中。

参数 shallow 具有同 filecmp.cmp() 一致的含义与默认值。

例如， cmpfiles('a', 'b', ['c', 'd/e']) 将会比较 a/c 与 b/c 以及 a/d/e 与 b/d/e 。 'c' 和 'd/e' 将会各自出现在返回的三个列表里的某一个列表中。

filecmp.clear_cache()

清除 filecmp 缓存。如果一个文件过快地修改，以至于超过底层文件系统记录修改时间的精度，那么该函数可能有助于比较该类文件。

3.4 新版功能.

11.5.1. dircmp 类

class filecmp.dircmp(a, b, ignore=None, hide=None)

创建一个用于比较目录 a 和 b 的新的目录比较对象。 ignore 是需要忽略的文件名列表，且默认为 filecmp.DEFAULT_IGNORES 。 hide 是需要隐藏的文件名列表，且默认为 [os.curdir, os.pardir] 。

dircmp 类如 filecmp.cmp() 中所描述的那样对文件进行 shallow 比较。

dircmp 类提供以下方法：

report()

将 a 与 b 之间的比较结果打印（到 sys.stdout ）。

report_partial_closure()

打印 a 与 b 及共同直接子目录的比较结果。

report_full_closure()

打印 a 与 b 及共同子目录比较结果（递归地）。

dircmp 类提供了一些有趣的属性，用以得到关于参与比较的目录树的各种信息。

需要注意，通过 __getattr__() 钩子，所有的属性将会惰性求值，因此如果只使用那些计算简便的属性，将不会有速度损失。

left

目录 a 。

right

目录 b 。

left_list

经 hide 和 ignore 过滤，目录 a 中的文件与子目录。

right_list

经 hide 和 ignore 过滤，目录 b 中的文件与子目录。

common

同时存在于目录 a 和 b 中的文件和子目录。

left_only

仅在目录 a 中的文件和子目录。

right_only

仅在目录 b 中的文件和子目录。

common_dirs

同时存在于目录 a 和 b 中的子目录。

common_files

同时存在于目录 a 和 b 中的文件。

common_funny

在目录 a 和 b 中类型不同的名字，或者那些 os.stat() 报告错误的名字。

same_files

在目录 a 和 b 中，使用类的文件比较操作符判定相等的文件。

diff_files

在目录 a 和 b 中，根据类的文件比较操作符判定内容不等的文件。

funny_files

在目录 a 和 b 中无法比较的文件。

subdirs

一个将 common_dirs 中名称映射为 dircmp 对象的字典。

filecmp.DEFAULT_IGNORES

3.4 新版功能.

默认被 dircmp 忽略的目录列表。

下面是一个简单的例子，使用 subdirs 属性递归搜索两个目录以显示公共差异文件：

>>> from filecmp import dircmp
>>> def print_diff_files(dcmp):
...     for name in dcmp.diff_files:
...         print("diff_file %s found in %s and %s" % (name, dcmp.left,
...               dcmp.right))
...     for sub_dcmp in dcmp.subdirs.values():
...         print_diff_files(sub_dcmp)
...
>>> dcmp = dircmp('dir1', 'dir2')
>>> print_diff_files(dcmp)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

11.6. tempfile — 生成临时文件和目录

源代码： Lib/tempfile.py [https://github.com/python/cpython/tree/3.6/Lib/tempfile.py]

该模块用于创建临时文件和目录，它可以跨平台使用。TemporaryFile、NamedTemporaryFile、TemporaryDirectory 和 SpooledTemporaryFile 是带有自动清理功能的高级接口，可用作上下文管理器。mkstemp() 和 mkdtemp() 是低级函数，使用完毕需手动清理。

所有由用户调用的函数和构造函数都带有参数，这些参数可以设置临时文件和临时目录的路径和名称。该模块生成的文件名包括一串随机字符，在公共的临时目录中，这些字符可以让创建文件更加安全。为了保持向后兼容性，参数的顺序有些奇怪。所以为了代码清晰，建议使用关键字参数。

这个模块定义了以下内容供用户调用：

tempfile.TemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None)

返回一个 file-like object 作为临时存储区域。创建该文件使用了与 mkstemp() 相同的安全规则。它将在关闭后立即销毁（包括垃圾回收机制关闭该对象时）。在 Unix 下，该文件在目录中的条目根本不创建，或者创建文件后立即就被删除了，其他平台不支持此功能。您的代码不应依赖使用此功能创建的临时文件名称，因为它在文件系统中的名称可能是可见的，也可能是不可见的。

生成的对象可以用作上下文管理器（参见 例子）。完成文件对象的上下文或销毁后，临时文件将从文件系统中删除。

mode 参数默认值为 'w+b' 因此创建的文件可以读取或写入而不用关闭。 因为是使用二进制模式，所以它在所有平台上的行为都保持一致而不用关心所存储的是什么数据。 buffering, encoding 和 newline 的解读方式与 open() 相同。

参数 dir、prefix 和 suffix 的含义和默认值都与它们在 mkstemp() 中的相同。

在 POSIX 平台上，它返回的对象是真实的文件对象。在其他平台上，它是一个文件类对象 (file-like object)，它的 file 属性是底层的真实文件对象。

如果可用，则使用 os.O_TMPFILE 标志（仅限于 Linux，需要 3.11 及更高版本的内核）。

在 3.5 版更改: 如果可用，现在用的是 os.O_TMPFILE 标志。

tempfile.NamedTemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True)

此函数执行的操作与 TemporaryFile() 完全相同，但确保了该临时文件在文件系统中具有可见的名称（在 Unix 上表现为目录条目不取消链接）。从返回的文件类对象的 name 属性中可以检索到文件名。在临时文件仍打开时，是否允许用文件名第二次打开文件，在各个平台上是不同的（在 Unix 上可以，但在 Windows NT 或更高版本上不行）。如果 delete 为 true（默认值），则文件会在关闭后立即被删除。该函数返回的对象始终是文件类对象 (file-like object)，它的 file 属性是底层的真实文件对象。文件类对象可以像普通文件一样在 with 语句中使用。

tempfile.SpooledTemporaryFile(max_size=0, mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None)

此函数执行的操作与 TemporaryFile() 完全相同，但会将数据缓存在内存中，直到文件大小超过 max_size，或调用文件的 fileno() 方法为止，此时数据会被写入磁盘，并且写入操作与 TemporaryFile() 相同。

此函数生成的文件对象有一个额外的方法——rollover()，可以忽略文件大小，让文件立即写入磁盘。

The returned object is a file-like object whose _file attribute is either an io.BytesIO or io.StringIO object (depending on whether binary or text mode was specified) or a true file object, depending on whether rollover() has been called. This file-like object can be used in a with statement, just like a normal file.

在 3.3 版更改: 现在，文件的 truncate 方法可接受一个 size 参数。

tempfile.TemporaryDirectory(suffix=None, prefix=None, dir=None)

此函数会安全地创建一个临时目录，且使用与 mkdtemp() 相同的规则。此函数返回的对象可用作上下文管理器（参见 例子）。完成上下文或销毁临时目录对象后，新创建的临时目录及其所有内容将从文件系统中删除。

The directory name can be retrieved from the name attribute of the returned object. When the returned object is used as a context manager, the name will be assigned to the target of the as clause in the with statement, if there is one.

可以调用 cleanup() 方法来手动清理目录。

3.2 新版功能.

tempfile.mkstemp(suffix=None, prefix=None, dir=None, text=False)

以最安全的方式创建一个临时文件。假设所在平台正确实现了 os.open() 的 os.O_EXCL 标志，则创建文件时不会有竞争的情况。该文件只能由创建者读写，如果所在平台用权限位来标记文件是否可执行，那么没有人有执行权。文件描述符不会过继给子进程。

与 TemporaryFile() 不同，mkstemp() 用户用完临时文件后需要自行将其删除。

如果 suffix 不是 None 则文件名将以该后缀结尾，是 None 则没有后缀。mkstemp() 不会在文件名和后缀之间加点，如果需要加一个点号，请将其放在 suffix 的开头。

如果 prefix 不是 None，则文件名将以该前缀开头，是 None 则使用默认前缀。默认前缀是 gettempprefix() 或 gettempprefixb() 函数的返回值（自动调用合适的函数）。

如果 dir 不为 None，则在指定的目录创建文件，是 None 则使用默认目录。默认目录是从一个列表中选择出来的，这个列表不同平台不一样，但是用户可以设置 TMPDIR、TEMP 或 TMP 环境变量来设置目录的位置。因此，不能保证生成的临时文件路径很规范，比如，通过 os.popen() 将路径传递给外部命令时仍需要加引号。

如果 suffix、prefix 和 dir 中的任何一个不是 None，就要保证它们是同一数据类型。如果它们是 bytes，则返回的名称的类型就是 bytes 而不是 str。如果确实要用默认参数，但又想要返回值是 bytes 类型，请传入 suffix=b''。

如果指定了 text 参数，它表示的是以二进制模式（默认）还是文本模式打开文件。在某些平台上，两种模式没有区别。

mkstemp() 返回一个元组，元组中第一个元素是句柄，它是一个系统级句柄，指向一个打开的文件（等同于 os.open() 的返回值），第二元素是该文件的绝对路径。

在 3.5 版更改: 现在，suffix、prefix 和 dir 可以以 bytes 类型按顺序提供，以获得 bytes 类型的返回值。之前只允许使用 str。suffix 和 prefix 现在可以接受 None，并且默认为 None 以使用合适的默认值。

tempfile.mkdtemp(suffix=None, prefix=None, dir=None)

以最安全的方式创建一个临时目录，创建该目录时不会有竞争的情况。该目录只能由创建者读取、写入和搜索。

mkdtemp() 用户用完临时目录后需要自行将其删除。

prefix、suffix 和 dir 的含义与它们在 mkstemp() 中的相同。

mkdtemp() 返回新目录的绝对路径名。

在 3.5 版更改: 现在，suffix、prefix 和 dir 可以以 bytes 类型按顺序提供，以获得 bytes 类型的返回值。之前只允许使用 str。suffix 和 prefix 现在可以接受 None，并且默认为 None 以使用合适的默认值。

tempfile.gettempdir()

返回放置临时文件的目录的名称。这个方法的返回值就是本模块所有函数的 dir 参数的默认值。

Python 搜索标准目录列表，以找到调用者可以在其中创建文件的目录。这个列表是：

TMPDIR 环境变量指向的目录。

TEMP 环境变量指向的目录。

TMP 环境变量指向的目录。

与平台相关的位置：

在 Windows 上，依次为 C:\TEMP、C:\TMP、\TEMP 和 \TMP。

在所有其他平台上，依次为 /tmp、/var/tmp 和 /usr/tmp。

不得已时，使用当前工作目录。

搜索的结果会缓存起来，参见下面 tempdir 的描述。

tempfile.gettempdirb()

与 gettempdir() 相同，但返回值为字节类型。

3.5 新版功能.

tempfile.gettempprefix()

返回用于创建临时文件的文件名前缀，它不包含目录部分。

tempfile.gettempprefixb()

与 gettempprefix() 相同，但返回值为字节类型。

3.5 新版功能.

本模块使用一个全局变量来存储由 gettempdir() 返回的临时文件目录路径。可以直接给它赋值，这样可以覆盖自动选择的路径，但是不建议这样做。本模块中的所有函数都带有一个 dir 参数，该参数可用于指定目录，这是推荐的方法。

tempfile.tempdir

当设置为 None 以外的其他值时，此变量将决定本模块所有函数的 dir 参数的默认值。

如果在调用除 gettempprefix() 外的上述任何函数时 tempdir 为 None (默认值) 则它会按照 gettempdir() 中所描述的算法来初始化。

11.6.1. 例子

以下是 tempfile 模块典型用法的一些示例:

>>> import tempfile

# create a temporary file and write some data to it
>>> fp = tempfile.TemporaryFile()
>>> fp.write(b'Hello world!')
# read data from file
>>> fp.seek(0)
>>> fp.read()
b'Hello world!'
# close the file, it will be removed
>>> fp.close()

# create a temporary file using a context manager
>>> with tempfile.TemporaryFile() as fp:
...     fp.write(b'Hello world!')
...     fp.seek(0)
...     fp.read()
b'Hello world!'
>>>
# file is now closed and removed

# create a temporary directory using the context manager
>>> with tempfile.TemporaryDirectory() as tmpdirname:
...     print('created temporary directory', tmpdirname)
>>>
# directory and contents have been removed

11.6.2. 已弃用的函数和变量

创建临时文件有一种历史方法，首先使用 mktemp() 函数生成一个文件名，然后使用该文件名创建文件。不幸的是，这是不安全的，因为在调用 mktemp() 与随后尝试创建文件的进程之间的时间里，其他进程可能会使用该名称创建文件。解决方案是将两个步骤结合起来，立即创建文件。这个方案目前被 mkstemp() 和上述其他函数所采用。

tempfile.mktemp(suffix='', prefix='tmp', dir=None)

2.3 版后已移除: 使用 mkstemp() 来代替。

返回一个绝对路径，这个路径指向的文件在调用本方法时不存在。prefix、suffix 和 dir 参数与 mkstemp() 中的同名参数类似，不同之处在于不支持字节类型的文件名，不支持 suffix=None 和 prefix=None。

警告

使用此功能可能会在程序中引入安全漏洞。当你开始使用本方法返回的文件执行任何操作时，可能有人已经捷足先登了。mktemp() 的功能可以很轻松地用 NamedTemporaryFile() 代替，当然需要传递 delete=False 参数:

>>> f = NamedTemporaryFile(delete=False)
>>> f.name
'/tmp/tmptjujjt'
>>> f.write(b"Hello World!\n")
13
>>> f.close()
>>> os.unlink(f.name)
>>> os.path.exists(f.name)
False

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

11.7. glob — Unix 风格路径名模式扩展

源代码: Lib/glob.py [https://github.com/python/cpython/tree/3.6/Lib/glob.py]

glob 模块可根据 Unix 终端所用规则找出所有匹配特定模式的路径名，但会按不确定的顺序返回结果。 波浪号扩展不会生效，但 *, ? 以及表示为 [] 的字符范围将被正确地匹配。 这是通过配合使用 os.scandir() 和 fnmatch.fnmatch() 函数来实现的，而不是通过实际发起调用子终端。 请注意不同于 fnmatch.fnmatch()，glob 会将以点号 (.) 开头的文件名作为特殊情况来处理。 （对于波浪号和终端变量扩展，请使用 os.path.expanduser() 和 os.path.expandvars()。）

对于字面值匹配，请将原字符用方括号括起来。 例如，'[?]' 将匹配字符 '?'。

参见

pathlib 模块提供高级路径对象。

glob.glob(pathname, *, recursive=False)

返回匹配 pathname 的可能为空的路径名列表，路径名必须为包含一个路径描述的字符串。 pathname 可以是绝对路径 (如 /usr/src/Python-1.5/Makefile) 或相对路径 (如 ../../Tools/*/*.gif)，并且可包含 shell 风格的通配符。 无效的符号链接可以包含在结果中 (与在 shell 中一样)。

If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is followed by an os.sep, only directories and subdirectories match.

注解

在一个较大的目录树中使用 “**” 模式可能会消耗非常多的时间。

在 3.5 版更改: 支持使用 “**” 的递归 glob.

glob.iglob(pathname, *, recursive=False)

返回一个 iterator，它会产生与 glob() 相同的结果，但不会实际地同时保存它们。

glob.escape(pathname)

转义所有特殊字符 ('?', '*' 和 '[')。 这适用于当你想要匹配可能带有特殊字符的任意字符串字面值的情况。 在 drive/UNC 共享点中的特殊字符不会被转义，例如在 Windows 上 escape('//?/c:/Quo vadis?.txt') 将返回 '//?/c:/Quo vadis[?].txt'。

3.4 新版功能.

例如，考虑一个包含以下内容的目录：文件 1.gif, 2.txt, card.gif 以及一个子目录 sub 其中只包含一个文件 3.txt. glob() 将产生如下结果。 请注意路径的任何开头部分都将被保留。:

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
>>> glob.glob('**/*.txt', recursive=True)
['2.txt', 'sub/3.txt']
>>> glob.glob('./**/', recursive=True)
['./', './sub/']

如果目录包含以 . 打头的文件，它们默认将不会被匹配。 例如，考虑一个包含 card.gif 和 .card.gif 的目录:

>>> import glob
>>> glob.glob('*.gif')
['card.gif']
>>> glob.glob('.c*')
['.card.gif']

参见

模块 fnmatch

Shell 风格文件名（而非路径）扩展

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

11.8. fnmatch — Unix 文件名模式匹配

源代码: Lib/fnmatch.py [https://github.com/python/cpython/tree/3.6/Lib/fnmatch.py]

此模块提供了 Unix shell 风格的通配符，它们 并不 等同于正则表达式（关于后者的文档参见 re 模块）。 shell 风格通配符所使用的特殊字符如下：

模式

含义

*

匹配所有

?

匹配任何单个字符

[seq]

匹配 seq 中的任何字符

[!seq]

匹配任何不在 seq 中的字符

对于字面值匹配，请将原字符用方括号括起来。 例如，'[?]' 将匹配字符 '?'。

注意文件名分隔符 (Unix 上为 '/') 不是 此模块所特有的。 请参见 glob 模块了解文件名扩展 (glob 使用 filter() 来匹配文件名的各个部分)。 类似地，以一个句点打头的文件名也不是此模块所特有的，可以通过 * 和 ? 模式来匹配。

fnmatch.fnmatch(filename, pattern)

检测 filename 字符串是否匹配 pattern 字符串，返回 True 或 False。 两个形参都会使用 os.path.normcase() 进行大小写正规化。 fnmatchcase() 可被用于执行大小写敏感的比较，无论这是否为所在操作系统的标准。

这个例子将打印当前目录下带有扩展名 .txt 的所有文件名:

import fnmatch
import os

for file in os.listdir('.'):
    if fnmatch.fnmatch(file, '*.txt'):
        print(file)

fnmatch.fnmatchcase(filename, pattern)

检测 filename 是否匹配 pattern，返回 True 或 False；此比较是大小写敏感的，并且不会应用 os.path.normcase()。

fnmatch.filter(names, pattern)

返回 names 列表中匹配 pattern 的子集。 它等价于 [n for n in names if fnmatch(n, pattern)]，但其实现更为高效。

fnmatch.translate(pattern)

返回 shell 风格 pattern 转换成的正则表达式以便用于 re.match()。

示例:

>>> import fnmatch, re
>>>
>>> regex = fnmatch.translate('*.txt')
>>> regex
'(?s:.*\\.txt)\\Z'
>>> reobj = re.compile(regex)
>>> reobj.match('foobar.txt')
<_sre.SRE_Match object; span=(0, 10), match='foobar.txt'>

参见

模块 glob

Unix shell 风格路径扩展。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

11.9. linecache — 随机读写文本行

源代码: Lib/linecache.py [https://github.com/python/cpython/tree/3.6/Lib/linecache.py]

linecache 模块允许从一个 Python 源文件中获取任意的行，并会尝试使用缓存进行内部优化，常应用于从单个文件读取多行的场合。 此模块被 traceback 模块用来提取源码行以便包含在格式化的回溯中。

tokenize.open() 函数被用于打开文件。 此函数使用 tokenize.detect_encoding() 来获取文件的编码格式；如果未指明编码格式，则默认编码为 UTF-8。

linecache 模块定义了下列函数：

linecache.getline(filename, lineno, module_globals=None)

从名为 filename 的文件中获取 lineno 行，此函数绝不会引发异常 — 出现错误时它将返回 '' (所有找到的行都将包含换行符作为结束)。

如果名为 filename 的文件未找到，该函数将在模块搜索路径 sys.path 中查找它，在此之前会先在 module_globals 中检查 PEP 302 [https://www.python.org/dev/peps/pep-0302] __loader__，以涵盖模块是从 zip 文件或其他非文件系统导入源导入的情况。

linecache.clearcache()

清空缓存。 如果你不再需要之前使用 getline() 从文件读取的行即可使用此函数。

linecache.checkcache(filename=None)

检查缓存有效性。 如果缓存中的文件在磁盘上发生了改变，而你需要更新后的版本即可使用此函数。 如果省略了 filename，它会检查缓存中的所有条目。

linecache.lazycache(filename, module_globals)

捕获有关某个非基于文件的模块的足够细节信息，以允许稍后再通过 getline() 来获取其中的行，即使当稍后调用时 module_globals 为 None。 这可以避免在实际需要读取行之前执行 I/O，也不必始终保持模块全局变量。

3.5 新版功能.

示例:

>>> import linecache
>>> linecache.getline(linecache.__file__, 8)
'import sys\n'

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

11.10. shutil — 高阶文件操作

源代码： Lib/shutil.py [https://github.com/python/cpython/tree/3.6/Lib/shutil.py]

shutil 模块提供了一系列对文件和文件集合的高阶操作。 特别是提供了一些支持文件拷贝和删除的函数。 对于单个文件的操作，请参阅 os 模块。

警告

即便是高阶文件拷贝函数 (shutil.copy(), shutil.copy2()) 也无法拷贝所有的文件元数据。

在 POSIX 平台上，这意味着将丢失文件所有者和组以及 ACL 数据。 在 Mac OS 上，资源钩子和其他元数据不被使用。 这意味着将丢失这些资源并且文件类型和创建者代码将不正确。 在 Windows 上，将不会拷贝文件所有者、ACL 和替代数据流。

11.10.1. 目录和文件操作

shutil.copyfileobj(fsrc, fdst[, length])

将文件类对象 fsrc 的内容拷贝到文件类对象 fdst。 整数值 length 如果给出则为缓冲区大小。 特别地， length 为负值表示拷贝数据时不对源数据进行分块循环处理；默认情况下会分块读取数据以避免不受控制的内存消耗。 请注意如果 fsrc 对象的当前文件位置不为 0，则只有从当前文件位置到文件末尾的内容会被拷贝。

shutil.copyfile(src, dst, *, follow_symlinks=True)

将 src 文件的内容（不含元数据）拷贝到 dst 文件并返回 dst。 src 和 dst 是字符串形式的路径。 dst 必须是完整的目标文件名；对于接受一个目标目录路径的拷贝请参见 shutil.copy()。 如果 src 和 dst 指定了同一文件，则将引发 SameFileError。

目标位置必须是可写的；否则将引发 OSError 异常。 如果 dst 已经存在，它将被替换。 特殊文件如字符或块设备以及管道无法用此函数来拷贝。

如果 follow_symlinks 为假值且 src 为符号链接，则将创建一个新的符号链接而不是拷贝 src 所指向的文件。

在 3.3 版更改: 曾经是引发 IOError 而不是 OSError。 增加了 follow_symlinks 参数。 现在是返回 dst。

在 3.4 版更改: 引发 SameFileError 而不是 Error。 由于前者是后者的子类，此改变是向后兼容的。

exception shutil.SameFileError

此异常会在 copyfile() 中的源和目标为同一文件时被引发。

3.4 新版功能.

shutil.copymode(src, dst, *, follow_symlinks=True)

从 src 拷贝权限位到 dst。 文件的内容、所有者和分组将不受影响。 src 和 dst 均为字符串形式的路径名。 如果 follow_symlinks 为假值，并且 src 和 dst 均为符号链接，copymode() 将尝试修改 dst 本身的模式（而非它所指向的文件）。 此功能并不是在所有平台上均可用；请参阅 copystat() 了解详情。 如果 copymode() 无法修改本机平台上的符号链接，而它被要求这样做，它将不做任何操作即返回。

在 3.3 版更改: 加入 follow_symlinks 参数。

shutil.copystat(src, dst, *, follow_symlinks=True)

从 src 拷贝权限位、最近访问时间、最近修改时间以及旗标到 dst。 在 Linux 上，copystat() 还会在可能的情况下拷贝“扩展属性”。 文件内容、所有者和分组将不受影响。 src 和 dst 均为字符串形式的路径名。

如果 follow_symlinks 为假值，并且 src 和 dst 均指向符号链接，copystat() 将作用于符号链接本身而非该符号链接所指向的文件 — 从 src 符号链接读取信息，并将信息写入 dst 符号链接。

注解

并非所有平台者提供检查和修改符号链接的功能。 Python 本身可以告诉你哪些功能是在本机上可用的。

如果 os.chmod in os.supports_follow_symlinks 为 True，则 copystat() 可以修改符号链接的权限位。

如果 os.utime in os.supports_follow_symlinks 为 True，则 copystat() 可以修改符号链接的最近访问和修改时间。

如果 os.chflags in os.supports_follow_symlinks 为 True，则 copystat() 可以修改符号链接的旗标。 (os.chflags 不是在所有平台上均可用。)

在此功能部分或全部不可用的平台上，当被要求修改一个符号链接时，copystat() 将尽量拷贝所有内容。 copystat() 一定不会返回失败信息。

更多信息请参阅 os.supports_follow_symlinks。

在 3.3 版更改: 添加了 follow_symlinks 参数并且支持 Linux 扩展属性。

shutil.copy(src, dst, *, follow_symlinks=True)

将文件 src 拷贝到文件或目录 dst。 src 和 dst 应为字符串。 如果 dst 指定了一个目录，文件将使用 src 中的基准文件名拷贝到 dst。 返回新创建文件所对应的路径。

如果 follow_symlinks 为假值且 src 为符号链接，则 dst 也将被创建为符号链接。 如果 follow_symlinks 为真值且 src 为符号链接，dst 将成为 src 所指向的文件的一个副本。

copy() 会拷贝文件数据和文件的权限模式 (参见 os.chmod())。 其他元数据，例如文件的创建和修改时间不会被保留。 要保留所有原有的元数据，请改用 copy2() 。

在 3.3 版更改: 添加了 follow_symlinks 参数。 现在会返回新创建文件的路径。

shutil.copy2(src, dst, *, follow_symlinks=True)

类似于 copy()，区别在于 copy2() 还会尝试保留文件的元数据。

当 follow_symlinks 为假值且 src 为符号链接时，copy2() 会尝试将来自 src 符号链接的所有元数据拷贝到新创建的 dst 符号链接。 但是，此功能不是在所有平台上均可用。 在此功能部分或全部不可用的平台上，copy2() 将尽量保留所有元数据；copy2() 一定不会返回失败的结果。

copy2() 会使用 copystat() 来拷贝文件元数据。 请参阅 copystat() 了解有关修改符号链接元数据的平台支持的更多信息。

在 3.3 版更改: 添加了 follow_symlinks 参数，还会尝试拷贝扩展文件系统属性（目前仅限 Linux）。 现在会返回新创建文件的路径。

shutil.ignore_patterns(*patterns)

这个工厂函数会创建一个函数，它可被用作 copytree() 的 ignore 可调用对象参数，以忽略那些匹配所提供的 glob 风格的 patterns 之一的文件和目录。 参见以下示例。

shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)

递归地拷贝以 src 为根路径的整个目录树，返回目标目录。 名为 dst 的目标目录不必已存在；它本身和还不存在的父目录都将被自动创建。 目录的权限和时间信息将通过 copystat() 来拷贝，单独的文件将使用 shutil.copy2() 来拷贝。

如果 symlinks 为真值，源目录树中的符号链接会在新目录树中表示为符号链接，并且原链接的元数据在平台允许的情况下也会被拷贝；如果为假值或省略，则会将被链接文件的内容和元数据拷贝到新目录树。

当 symlinks 为假值时，如果符号链接所指向的文件不存在，则会在拷贝进程的末尾将一个异常添加到 Error 异常中的错误列表。 如果你希望屏蔽此异常那就将可选的 ignore_dangling_symlinks 旗标设为真值。 请注意此选项在不支持 os.symlink() 的平台上将不起作用。

如果给出了 ignore，它必须是一个可调用对象，该对象将接受 copytree() 所访问的目录以及 os.listdir() 所返回的目录内容列表作为其参数。 由于 copytree() 是递归地被调用的，ignore 可调用对象对于每个被拷贝目录都将被调用一次。 该可调用对象必须返回一个相对于当前目录的目录和文件名序列（即其第二个参数的子集）；随后这些名称将在拷贝进程中被忽略。 ignore_patterns() 可被用于创建这种基于 glob 风格模式来忽略特定名称的可调用对象。

如果发生了异常，将引发一个附带原因列表的 Error。

如果给出了 copy_function，它必须是一个将被用来拷贝每个文件的可调用对象。 它在被调用时会将源路径和目标路径作为参数传入。 默认情况下，shutil.copy2() 将被使用，但任何支持同样签名（与 shutil.copy() 一致）的函数都可以使用。

在 3.3 版更改: 当 symlinks 为假值时拷贝元数据。 现在会返回 dst。

在 3.2 版更改: 添加了 copy_function 参数以允许提供定制的拷贝函数。 添加了 ignore_dangling_symlinks 参数以便在 symlinks 为假值时屏蔽符号链接错误。

shutil.rmtree(path, ignore_errors=False, onerror=None)

删除一个完整的目录树；path 必须指向一个目录（但不能是一个目录的符号链接）。 如果 ignore_errors 为真值，删除失败导致的错误将被忽略；如果为假值或是省略，此类错误将通过调用由 onerror 所指定的处理程序来处理，或者如果此参数被省略则将引发一个异常。

注解

在支持必要的基于 fd 的函数的平台上，默认会使用 rmtree() 的可防御符号链接攻击的版本。 在其他平台上，rmtree() 较易遭受符号链接攻击：给定适当的时间和环境，攻击者可以操纵文件系统中的符号链接来删除他们在其他情况下无法访问的文件。 应用程序可以使用 rmtree.avoids_symlink_attacks 函数属性来确定此类情况具体是哪一些。

如果提供了 onerror，它必须为接受三个形参的可调用对象: function, path 和 excinfo。

第一个形参 function 是引发异常的函数；它依赖于具体的平台和实现。 第二个形参 path 将是传递给 function 的路径名。 第三个形参 excinfo 将是由 sys.exc_info() 所返回的异常信息。 由 onerror 所引发的异常将不会被捕获。

在 3.3 版更改: 添加了一个防御符号链接攻击的版本，如果平台支持基于 fd 的函数就会被使用。

rmtree.avoids_symlink_attacks

指明当前平台和实现是否提供防御符号链接攻击的 rmtree() 版本。 目前它仅在平台支持基于 fd 的目录访问函数时才返回真值。

3.3 新版功能.

shutil.move(src, dst, copy_function=copy2)

递归地将一个文件或目录 (src) 移至另一位置 (dst) 并返回目标位置。

如果目标是已存在的目录，则 src 会被移至该目录下。 如果目标已存在但不是目录，它可能会被覆盖，具体取决于 os.rename() 的语义。

如果目标是在当前文件系统中，则会使用 os.rename()。 在其他情况下，src 将被拷贝至 dst，使用的函数为 copy_function，然后目标会被移除。 对于符号链接，则将在 dst 之下或以其本身为名称创建一个指向 src 目标的新符号链接，并且 src 将被移除。

如果给出了 copy_function，则它必须为接受两个参数 src 和 dst 的可调用对象，并会在 os.rename() 无法使用时被用来将 src 拷贝到 dest。 如果源位置是一个目录，则会调用 copytree()，并向它传入 copy_function()。 默认的 copy_function 是 copy2()。 使用 copy() 作为 copy_function 允许在无法附带拷贝元数据时让移动操作成功执行，但其代价是不拷贝任何元数据。

在 3.3 版更改: 为异类文件系统添加了显式的符号链接处理，以便使它适应 GNU 的 mv 的行为。 现在会返回 dst。

在 3.5 版更改: 增加了 copy_function 关键字参数。

shutil.disk_usage(path)

返回给定路径的磁盘使用统计数据，形式为一个 named tuple，其中包含 total, used 和 free 属性，分别表示总计、已使用和未使用空间的字节数。 在 Windows 上，path 必须是一个目录；在 Unix 上，它可以是一个文件或一个目录。

3.3 新版功能.

Availability: Unix, Windows.

shutil.chown(path, user=None, group=None)

修改给定 path 的所有者 user 和/或 group。

user 可以是一个系统用户名或 uid；group 同样如此。 要求至少有一个参数。

另请参阅下层的函数 os.chown()。

Availability: Unix.

3.3 新版功能.

shutil.which(cmd, mode=os.F_OK | os.X_OK, path=None)

返回当给定的 cmd 被调用时将要运行的可执行文件的路径。 如果没有 cmd 会被调用则返回 None。

mode 是一个传递给 os.access() 的权限掩码，在默认情况下将确定文件是否存在并且为可执行文件。

当未指定 path 时，将会使用 os.environ() 的结果，返回 “PATH” 的值或回退为 os.defpath。

在 Windows 上当前目录总是会被添加为 path 的第一项，无论你是否使用默认值或提供你自己的路径，这是命令行终端在查找可执行文件时所采用的行为方式。 此外，当在 path 中查找 cmd 时，还会检查 PATHEXT 环境变量。 例如，如果你调用 shutil.which("python")，which() 将搜索 PATHEXT 来确定它要在 path 目录中查找 python.exe。 例如，在 Windows 上:

>>> shutil.which("python")
'C:\\Python33\\python.EXE'

3.3 新版功能.

exception shutil.Error

此异常会收集在多文件操作期间所引发的异常。 对于 copytree()，此异常参数将是一个由三元组 (srcname, dstname, exception) 构成的列表。

11.10.1.1. copytree 示例

这个示例就是上面所描述的 copytree() 函数的实现，其中省略了文档字符串。 它还展示了此模块所提供的许多其他函数。

def copytree(src, dst, symlinks=False):
    names = os.listdir(src)
    os.makedirs(dst)
    errors = []
    for name in names:
        srcname = os.path.join(src, name)
        dstname = os.path.join(dst, name)
        try:
            if symlinks and os.path.islink(srcname):
                linkto = os.readlink(srcname)
                os.symlink(linkto, dstname)
            elif os.path.isdir(srcname):
                copytree(srcname, dstname, symlinks)
            else:
                copy2(srcname, dstname)
            # XXX What about devices, sockets etc.?
        except OSError as why:
            errors.append((srcname, dstname, str(why)))
        # catch the Error from the recursive copytree so that we can
        # continue with other files
        except Error as err:
            errors.extend(err.args[0])
    try:
        copystat(src, dst)
    except OSError as why:
        # can't copy file access times on Windows
        if why.winerror is None:
            errors.extend((src, dst, str(why)))
    if errors:
        raise Error(errors)

另一个使用 ignore_patterns() 辅助函数的例子:

from shutil import copytree, ignore_patterns

copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))

这将会拷贝除 .pyc 文件和以 tmp 打头的文件或目录以外的所有条目.

另一个使用 ignore 参数来添加记录调用的例子:

from shutil import copytree
import logging

def _logpath(path, names):
    logging.info('Working in %s', path)
    return []   # nothing will be ignored

copytree(source, destination, ignore=_logpath)

11.10.1.2. rmtree 示例

这个例子演示了如何在 Windows 上删除一个目录树，其中部分文件设置了只读属性位。 它会使用 onerror 回调函数来清除只读属性位并再次尝试删除。 任何后续的失败都将被传播。

import os, stat
import shutil

def remove_readonly(func, path, _):
    "Clear the readonly bit and reattempt the removal"
    os.chmod(path, stat.S_IWRITE)
    func(path)

shutil.rmtree(directory, onerror=remove_readonly)

11.10.2. 归档操作

3.2 新版功能.

在 3.5 版更改: 添加了对 xztar 格式的支持。

本模块也提供了用于创建和读取压缩和归档文件的高层级工具。 它们依赖于 zipfile 和 tarfile 模块。

shutil.make_archive(base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]]]]]]])

创建一个归档文件（例如 zip 或 tar）并返回其名称。

base_name 是要创建的文件名称，包括路径，去除任何特定格式的扩展名。 format 是归档格式：为 “zip” (如果 zlib 模块可用), “tar”, “gztar” (如果 zlib 模块可用), “bztar” (如果 bz2 模块可用) 或 “xztar” (如果 lzma 模块可用) 中的一个。

root_dir is a directory that will be the root directory of the archive; for example, we typically chdir into root_dir before creating the archive.

base_dir is the directory where we start archiving from; i.e. base_dir will be the common prefix of all files and directories in the archive.

root_dir 和 base_dir 默认均为当前目录。

如果 dry_run 为真值，则不会创建归档文件，但将要被执行的操作会被记录到 logger。

owner 和 group 将在创建 tar 归档文件时被使用。 默认会使用当前的所有者和分组。

logger 必须是一个兼容 PEP 282 [https://www.python.org/dev/peps/pep-0282] 的对象，通常为 logging.Logger 的实例。

verbose 参数已不再使用并进入弃用状态。

shutil.get_archive_formats()

返回支持的归档格式列表。 所返回序列中的每个元素为一个元组 (name, description)。

默认情况下 shutil 提供以下格式:

zip: ZIP 文件（如果 zlib 模块可用）。

tar: 未压缩的 tar 文件。

gztar: gzip 压缩的 tar 文件（如果 zlib 模块可用）。

bztar: bzip2 压缩的 tar 文件（如果 bz2 模块可用）。

xztar: xz 压缩的 tar 文件（如果 lzma 模块可用）。

你可以通过使用 register_archive_format() 注册新的格式或为任何现有格式提供你自己的归档程序。

shutil.register_archive_format(name, function[, extra_args[, description]])

为 name 格式注册一个归档程序。

function 是将被用来解包归档文件的可调用对象。 该可调用对象将接收要创建文件的 base_name，再加上要归档内容的 base_dir (其默认值为 os.curdir)。 更多参数会被作为关键字参数传入: owner, group, dry_run 和 logger (与向 make_archive() 传入的参数一致)。

如果给出了 extra_args，则其应为一个 (name, value) 对的序列，将在归档器可调用对象被使用时作为附加的关键字参数。

description 由 get_archive_formats() 使用，它将返回归档器的列表。 默认值为一个空字符串。

shutil.unregister_archive_format(name)

从支持的格式中移除归档格式 name。

shutil.unpack_archive(filename[, extract_dir[, format]])

解包一个归档文件。 filename 是归档文件的完整路径。

extract_dir 是归档文件解包的目标目录名称。 如果未提供，则将使用当前工作目录。

format 是归档格式：应为 “zip”, “tar”, “gztar”, “bztar” 或 “xztar” 之一。 或者任何通过 register_unpack_format() 注册的其他格式。 如果未提供，unpack_archive() 将使用归档文件的后缀来检查是否注册了对应于该后缀的解包器。 在未找到任何解包器的情况下，将引发 ValueError。

shutil.register_unpack_format(name, extensions, function[, extra_args[, description]])

注册一个解包格式。 name 为格式名称而 extensions 为对应于该格式的扩展名列表，例如 Zip 文件的扩展名为 .zip。

function 是将被用来解包归档文件的可调用对象。 该可调用对象将接受归档文件的路径，加上该归档文件要被解包的目标目录。

如果提供了 extra_args，则其应为一个 (name, value) 元组的序列，将被作为关键字参数传递给该可调用对象。

可以提供 description 来描述该格式，它将被 get_unpack_formats() 返回。

shutil.unregister_unpack_format(name)

撤销注册一个解包格式。 name 为格式的名称。

shutil.get_unpack_formats()

返回所有已注册的解包格式列表。 所返回序列中的每个元素为一个元组 (name, extensions, description)。

默认情况下 shutil 提供以下格式:

zip: ZIP 文件（只有在相应模块可用时才能解包压缩文件）。

tar: 未压缩的 tar 文件。

gztar: gzip 压缩的 tar 文件（如果 zlib 模块可用）。

bztar: bzip2 压缩的 tar 文件（如果 bz2 模块可用）。

xztar: xz 压缩的 tar 文件（如果 lzma 模块可用）。

你可以通过使用 register_unpack_format() 注册新的格式或为任何现有格式提供你自己的解包器。

11.10.2.1. 归档程序示例

在这个示例中，我们创建了一个 gzip 压缩的 tar 归档文件，其中包含用户的 .ssh 目录下的所有文件:

>>> from shutil import make_archive
>>> import os
>>> archive_name = os.path.expanduser(os.path.join('~', 'myarchive'))
>>> root_dir = os.path.expanduser(os.path.join('~', '.ssh'))
>>> make_archive(archive_name, 'gztar', root_dir)
'/Users/tarek/myarchive.tar.gz'

结果归档文件中包含有:

$ tar -tzvf /Users/tarek/myarchive.tar.gz
drwx------ tarek/staff       0 2010-02-01 16:23:40 ./
-rw-r--r-- tarek/staff     609 2008-06-09 13:26:54 ./authorized_keys
-rwxr-xr-x tarek/staff      65 2008-06-09 13:26:54 ./config
-rwx------ tarek/staff     668 2008-06-09 13:26:54 ./id_dsa
-rwxr-xr-x tarek/staff     609 2008-06-09 13:26:54 ./id_dsa.pub
-rw------- tarek/staff    1675 2008-06-09 13:26:54 ./id_rsa
-rw-r--r-- tarek/staff     397 2008-06-09 13:26:54 ./id_rsa.pub
-rw-r--r-- tarek/staff   37192 2010-02-06 18:23:10 ./known_hosts

11.10.3. 查询输出终端的尺寸

shutil.get_terminal_size(fallback=(columns, lines))

获取终端窗口的尺寸。

对于两个维度中的每一个，会分别检查环境变量 COLUMNS 和 LINES。 如果定义了这些变量并且其值为正整数，则将使用这些值。

如果未定义 COLUMNS 或 LINES，这是通常的情况，则连接到 sys.__stdout__ 的终端将通过发起调用 os.get_terminal_size() 被查询。

如果由于系统不支持查询，或是由于我们未连接到某个终端而导致查询终端尺寸不成功，则会使用在 fallback 形参中给出的值。 fallback 默认为 (80, 24)，这是许多终端模拟器所使用的默认尺寸。

返回的值是一个 os.terminal_size 类型的具名元组。

另请参阅: The Single UNIX Specification, Version 2, Other Environment Variables [http://pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html#tag_002_003].

3.3 新版功能.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

11.11. macpath — Mac OS 9 路径操作函数

源码： Lib/macpath.py [https://github.com/python/cpython/tree/3.6/Lib/macpath.py]

该模块是 os.path 模块的 Mac OS 9（及更早版本）实现。 它可用于在 Mac OS X（或任何其他平台）上操作旧式Macintosh路径名。

该模块中提供了以下函数：normcase() 、 normpath() 、 isabs() 、 join() 、 split() 、 isdir() 、 isfile() 、 walk() 、 exists()。 对于其他可用的函数 os.path 虚拟对应可用。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

11. 文件和目录访问 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

12. 数据持久化

本章中描述的模块支持在磁盘上以持久形式存储 Python 数据。 pickle 和 marshal 模块可以将许多 Python 数据类型转换为字节流，然后从字节中重新创建对象。 各种与 DBM 相关的模块支持一系列基于散列的文件格式，这些格式存储字符串到其他字符串的映射。

本章中描述的模块列表是：

12.1. pickle —— Python 对象序列化

12.1.1. 与其他 Python 模块间的关系

12.1.1.1. 与 marshal 间的关系

12.1.1.2. 与 json 模块的比较

12.1.2. 数据流格式

12.1.3. 模块接口

12.1.4. 可以被打包/解包的对象

12.1.5. 打包类实例

12.1.5.1. 持久化外部对象

12.1.5.2. Dispatch 表

12.1.5.3. 处理有状态的对象

12.1.6. 限制全局变量

12.1.7. 性能

12.1.8. 例子

12.2. copyreg — 注意 pickle 支持函数

12.2.1. 示例

12.3. shelve — Python 对象持久化

12.3.1. 限制

12.3.2. 示例

12.4. marshal — 内部 Python 对象序列化

12.5. dbm — Unix “数据库” 接口

12.5.1. dbm.gnu — GNU 对 dbm 的重解析

12.5.2. dbm.ndbm — 基于 ndbm 的接口

12.5.3. dbm.dumb — 便携式 DBM 实现

12.6. sqlite3 — SQLite 数据库 DB-API 2.0 接口模块

12.6.1. 模块函数和常量

12.6.2. 连接对象（Connection）

12.6.3. Cursor 对象

12.6.4. 行对象*Row*

12.6.5. 异常

12.6.6. SQLite 与 Python 类型

12.6.6.1. 概述

12.6.6.2. 使用适配器将额外的 Python 类型保存在 SQLite 数据库中。

12.6.6.2.1. 让对象自行调整

12.6.6.2.2. 注册可调用的适配器

12.6.6.3. 将SQLite 值转换为自定义Python 类型

12.6.6.4. 默认适配器和转换器

12.6.7. 控制事务

12.6.8. 有效使用 sqlite3

12.6.8.1. 使用快捷方式

12.6.8.2. 通过名称而不是索引访问索引

12.6.8.3. 使用连接作为上下文管理器

12.6.9. 常见问题

12.6.9.1. 多线程

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

12. 数据持久化 »

|

12.1. pickle —— Python 对象序列化

源代码： Lib/pickle.py [https://github.com/python/cpython/tree/3.6/Lib/pickle.py]

模块 pickle 实现了对一个 Python 对象结构的二进制序列化和反序列化。 “Pickling” 是将 Python 对象及其所拥有的层次结构转化为一个字节流的过程，而 “unpickling” 是相反的操作，会将（来自一个 binary file 或者 bytes-like object 的）字节流转化回一个对象层次结构。Pickling（和 unpickling）也被称为“序列化”, “编组” 1 或者 “平面化”。而为了避免混乱，此处采用术语 “pickling” 和 “unpickling”。

警告

pickle 模块在接受被错误地构造或者被恶意地构造的数据时不安全。永远不要 unpickle 来自于不受信任的或者未经验证的来源的数据。

12.1.1. 与其他 Python 模块间的关系

12.1.1.1. 与 marshal 间的关系

Python 有一个更原始的序列化模块称为 marshal，但一般地 pickle 应该是序列化 Python 对象时的首选。marshal 存在主要是为了支持 Python 的 .pyc 文件.

pickle 模块与 marshal 在如下几方面显著地不同：

pickle 模块会跟踪已被序列化的对象，所以该对象之后再次被引用时不会再次被序列化。marshal 不会这么做。

这隐含了递归对象和共享对象。递归对象指包含对自己的引用的对象。这种对象并不会被 marshal 接受，并且实际上尝试 marshal 递归对象会让你的 Python 解释器崩溃。对象共享发生在对象层级中存在多处引用同一对象时。pickle 只会存储这些对象一次，并确保其他的引用指向同一个主副本。共享对象将保持共享，这可能对可变对象非常重要。

marshal 不能被用于序列化用户定义类及其实例。pickle 能够透明地存储并保存类实例，然而此时类定义必须能够从与被存储时相同的模块被引入。

The marshal serialization format is not guaranteed to be portable across Python versions. Because its primary job in life is to support .pyc files, the Python implementers reserve the right to change the serialization format in non-backwards compatible ways should the need arise. The pickle serialization format is guaranteed to be backwards compatible across Python releases.

12.1.1.2. 与 json 模块的比较

Pickle 协议和 JSON (JavaScript Object Notation) [http://json.org] 间有着本质的不同：

JSON 是一个文本序列化格式（它输出 unicode 文本，尽管在大多数时候它会接着以 utf-8 编码），而 pickle 是一个二进制序列化格式；

JSON 是我们可以直观阅读的，而 pickle 不是；

JSON是可互操作的，在Python系统之外广泛使用，而pickle则是Python专用的；

默认情况下，JSON 只能表示 Python 内置类型的子集，不能表示自定义的类；但 pickle 可以表示大量的 Python 数据类型（可以合理使用 Python 的对象内省功能自动地表示大多数类型，复杂情况可以通过实现 specific object APIs 来解决）。

参见

json 模块:一个允许JSON序列化和反序列化的标准库模块

12.1.2. 数据流格式

pickle 所使用的数据格式仅可用于 Python。这样做的好处是没有外部标准给该格式强加限制，比如 JSON 或 XDR（不能表示共享指针）标准；但这也意味着非 Python 程序可能无法重新读取 pickle 打包的 Python 对象。

默认情况下，pickle 格式使用相对紧凑的二进制来存储。如果需要让文件更小，可以高效地 压缩 由 pickle 打包的数据。

pickletools 模块包含了相应的工具用于分析 pickle 生成的数据流。pickletools 源码中包含了对 pickle 协议使用的操作码的大量注释。

当前用于 pickling 的协议共有 5 种。使用的协议版本越高，读取生成的 pickle 所需的 Python 版本就要越新。

v0 版协议是原始的“人类可读”协议，并且向后兼容早期版本的 Python。

v1 版协议是较早的二进制格式，它也与早期版本的 Python 兼容。

v2 版协议是在 Python 2.3 中引入的。它为存储 new-style class 提供了更高效的机制。欲了解有关第 2 版协议带来的改进，请参阅 PEP 307 [https://www.python.org/dev/peps/pep-0307]。

v3 版协议添加于 Python 3.0。它具有对 bytes 对象的显式支持，且无法被 Python 2.x 打开。这是目前默认使用的协议，也是在要求与其他 Python 3 版本兼容时的推荐协议。

v4 版协议添加于 Python 3.4。它支持存储非常大的对象，能存储更多种类的对象，还包括一些针对数据格式的优化。有关第 4 版协议带来改进的信息，请参阅 PEP 3154 [https://www.python.org/dev/peps/pep-3154]。

注解

序列化是一种比持久化更底层的概念，虽然 pickle 读取和写入的是文件对象，但它不处理持久对象的命名问题，也不处理对持久对象的并发访问（甚至更复杂）的问题。pickle 模块可以将复杂对象转换为字节流，也可以将字节流转换为具有相同内部结构的对象。处理这些字节流最常见的做法是将它们写入文件，但它们也可以通过网络发送或存储在数据库中。shelve 模块提供了一个简单的接口，用于在 DBM 类型的数据库文件上 pickle 和 unpickle 对象。

12.1.3. 模块接口

要序列化某个包含层次结构的对象，只需调用 dumps() 函数即可。同样，要反序列化数据流，可以调用 loads() 函数。但是，如果要对序列化和反序列化加以更多的控制，可以分别创建 Pickler 或 Unpickler 对象。

pickle 模块包含了以下常量：

pickle.HIGHEST_PROTOCOL

整数，可用的最高 协议版本。此值可以作为 协议 值传递给 dump() 和 dumps() 函数，以及 Pickler 的构造函数。

pickle.DEFAULT_PROTOCOL

一个整数，表示封存操作使用的 协议版本。 它可能小于 HIGHEST_PROTOCOL。 当前默认协议版本为 3，它是一个为 Python 3 设计的新协议。

pickle 模块提供了以下方法，让打包过程更加方便。

pickle.dump(obj, file, protocol=None, *, fix_imports=True)

Write a pickled representation of obj to the open file object file. This is equivalent to Pickler(file, protocol).dump(obj).

可选参数 protocol 是一个整数，告知 pickler 使用指定的协议，可选择的协议范围从 0 到 HIGHEST_PROTOCOL。如果没有指定，这一参数默认值为 DEFAULT_PROTOCOL。指定一个负数就相当于指定 HIGHEST_PROTOCOL。

参数 file 必须有一个 write() 方法，该 write() 方法要能接收字节作为其唯一参数。因此，它可以是一个打开的磁盘文件（用于写入二进制内容），也可以是一个 io.BytesIO 实例，也可以是满足这一接口的其他任何自定义对象。

如果 fix_imports 为 True 且 protocol 小于 3，pickle 将尝试将 Python 3 中的新名称映射到 Python 2 中的旧模块名称，因此 Python 2 也可以读取打包出的数据流。

pickle.dumps(obj, protocol=None, *, fix_imports=True)

Return the pickled representation of the object as a bytes object, instead of writing it to a file.

参数 protocol 和 fix_imports 的含义与它们在 dump() 中的含义相同。

pickle.load(file, *, fix_imports=True, encoding="ASCII", errors="strict")

Read a pickled object representation from the open file object file and return the reconstituted object hierarchy specified therein. This is equivalent to Unpickler(file).load().

The protocol version of the pickle is detected automatically, so no protocol argument is needed. Bytes past the pickled object’s representation are ignored.

参数 file 必须有两个方法，其中 read() 方法接受一个整数参数，而 readline() 方法不需要参数。 两个方法都应返回字节串。 因此 file 可以是一个打开用于二进制读取的磁盘文件、一个 io.BytesIO 对象，或者任何满足此接口要求的其他自定义对象。

可选的关键字参数是 fix_imports, encoding 和 errors，用于控制由Python 2 生成的 pickle 流的兼容性。如果 fix_imports 为 true，则 pickle 将尝试将旧的 Python 2 名称映射到 Python 3 中对应的新名称。encoding 和 errors 参数告诉 pickle 如何解码 Python 2 存储的 8 位字符串实例；这两个参数默认分别为 ‘ASCII’ 和 ‘strict’。 encoding 参数可置为 ‘bytes’ 来将这些 8 位字符串实例读取为字节对象。读取 NumPy array 和 Python 2 存储的 datetime、date 和 time 实例时，请使用 encoding='latin1'。

pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict")

Read a pickled object hierarchy from a bytes object and return the reconstituted object hierarchy specified therein.

The protocol version of the pickle is detected automatically, so no protocol argument is needed. Bytes past the pickled object’s representation are ignored.

可选的关键字参数是 fix_imports, encoding 和 errors，用于控制由Python 2 生成的 pickle 流的兼容性。如果 fix_imports 为 true，则 pickle 将尝试将旧的 Python 2 名称映射到 Python 3 中对应的新名称。encoding 和 errors 参数告诉 pickle 如何解码 Python 2 存储的 8 位字符串实例；这两个参数默认分别为 ‘ASCII’ 和 ‘strict’。 encoding 参数可置为 ‘bytes’ 来将这些 8 位字符串实例读取为字节对象。读取 NumPy array 和 Python 2 存储的 datetime、date 和 time 实例时，请使用 encoding='latin1'。

pickle 模块定义了以下 3 个异常：

exception pickle.PickleError

其他 pickle 异常的基类。它是 Exception 的一个子类。

exception pickle.PicklingError

当 Pickler 遇到无法解包的对象时抛出此错误。它是 PickleError 的子类。

参考 可以被打包/解包的对象 来了解哪些对象可以被打包。

exception pickle.UnpicklingError

当解包出错时抛出此异常，例如数据损坏或对象不安全。它是 PickleError 的子类。

注意，解包时可能还会抛出其他异常，包括（但不限于） AttributeError、EOFError、ImportError 和 IndexError。

pickle 模块可导出两个类，Pickler 和 Unpickler:

class pickle.Pickler(file, protocol=None, *, fix_imports=True)

它接受一个二进制文件用于写入 pickle 数据流。

可选参数 protocol 是一个整数，告知 pickler 使用指定的协议，可选择的协议范围从 0 到 HIGHEST_PROTOCOL。如果没有指定，这一参数默认值为 DEFAULT_PROTOCOL。指定一个负数就相当于指定 HIGHEST_PROTOCOL。

参数 file 必须有一个 write() 方法，该 write() 方法要能接收字节作为其唯一参数。因此，它可以是一个打开的磁盘文件（用于写入二进制内容），也可以是一个 io.BytesIO 实例，也可以是满足这一接口的其他任何自定义对象。

如果 fix_imports 为 True 且 protocol 小于 3，pickle 将尝试将 Python 3 中的新名称映射到 Python 2 中的旧模块名称，因此 Python 2 也可以读取打包出的数据流。

dump(obj)

Write a pickled representation of obj to the open file object given in the constructor.

persistent_id(obj)

默认什么也不做。它存在是为了让子类可以重载它。

如果 persistent_id() 返回 None，obj 会被照常 pickle。如果返回其他值，Pickler 会将这个函数的返回值作为 obj 的持久化 ID（Pickler 本应得到序列化数据流并将其写入文件，若此函数有返回值，则得到此函数的返回值并写入文件）。这个持久化 ID 的解释应当定义在 Unpickler.persistent_load() 中（该方法定义还原对象的过程，并返回得到的对象）。注意，persistent_id() 的返回值本身不能拥有持久化 ID。

参阅 持久化外部对象 获取详情和使用示例。

dispatch_table

Pickler 对象的 dispatch 表是 copyreg.pickle() 中用到的 reduction 函数 的注册。dispatch 表本身是一个 class 到其 reduction 函数的映射键值对。一个 reduction 函数只接受一个参数，就是其关联的 class，函数行为应当遵守 __reduce__() 接口规范。

Pickler 对象默认并没有 dispatch_table 属性，该对象默认使用 copyreg 模块中定义的全局 dispatch 表。如果要为特定 Pickler 对象自定义序列化过程，可以将 dispatch_table 属性设置为类字典对象（dict-like object）。另外，如果 Pickler 的子类设置了 dispatch_table 属性，则该子类的实例会使用这个表作为默认的 dispatch 表。

参阅 Dispatch 表 获取使用示例。

3.3 新版功能.

fast

已弃用。设为 True 则启用快速模式。快速模式禁用了“备忘录” (memo) 的使用，即不生成多余的 PUT 操作码来加快打包过程。不应将其与自指 (self-referential) 对象一起使用，否则将导致 Pickler 无限递归。

如果需要进一步提高 pickle 的压缩率，请使用 pickletools.optimize()。

class pickle.Unpickler(file, *, fix_imports=True, encoding="ASCII", errors="strict")

它接受一个二进制文件用于读取 pickle 数据流。

Pickle 协议版本是自动检测出来的，所以不需要参数来指定协议。

参数 file 必须有两个方法，其中 read() 方法接受一个整数参数，而 readline() 方法不需要参数。 两个方法都应返回字节串。 因此 file 可以是一个打开用于二进制读取的磁盘文件对象、一个 io.BytesIO 对象，或者任何满足此接口要求的其他自定义对象。

可选的关键字参数有 fix_imports, encoding 和 errors，它们用于控制由 Python 2 所生成 pickle 流的兼容性支持。 如果 fix_imports 为真值，则 pickle 将尝试把旧的 Python 2 名称映射到 Python 3 所使用的新名称。 encoding 和 errors 将告知 pickle 如何解码由 Python 2 所封存的 8 位字符串实例；这两个参数的默认值分别为 ‘ASCII’ 和 ‘strict’。 encoding 可设为 ‘bytes’ 以将这些 8 位字符串实例作为字节对象来读取。

load()

Read a pickled object representation from the open file object given in the constructor, and return the reconstituted object hierarchy specified therein. Bytes past the pickled object’s representation are ignored.

persistent_load(pid)

默认抛出 UnpicklingError 异常。

如果定义了此方法，persistent_load() 应当返回持久化 ID pid 所指定的对象。 如果遇到无效的持久化 ID，则应当引发 UnpicklingError。

参阅 持久化外部对象 获取详情和使用示例。

find_class(module, name)

如有必要，导入 module 模块并返回其中名叫 name 的对象，其中 module 和 name 参数都是 str 对象。注意，不要被这个函数的名字迷惑，find_class() 同样可以用来导入函数。

子类可以重载此方法，来控制加载对象的类型和加载对象的方式，从而尽可能降低安全风险。参阅 限制全局变量 获取更详细的信息。

12.1.4. 可以被打包/解包的对象

下列类型可以被打包：

None、True 和 False

整数、浮点数、复数

str、byte、bytearray

只包含可打包对象的集合，包括 tuple、list、set 和 dict

定义在模块顶层的函数（使用 def 定义，lambda 函数则不可以）

定义在模块顶层的内置函数

定义在模块顶层的类

某些类实例，这些类的 __dict__ 属性值或 __getstate__() 函数的返回值可以被打包（详情参阅 打包类实例 这一段）。

尝试打包不能被打包的对象会抛出 PicklingError 异常，异常发生时，可能有部分字节已经被写入指定文件中。尝试打包递归层级很深的对象时，可能会超出最大递归层级限制，此时会抛出 RecursionError 异常，可以通过 sys.setrecursionlimit() 调整递归层级，不过请谨慎使用这个函数，因为可能会导致解释器崩溃。

注意，函数（内建函数或用户自定义函数）在被打包时，引用的是函数全名。2 这意味着只有函数所在的模块名，与函数名会被打包，函数体及其属性不会被打包。因此，在解包的环境中，函数所属的模块必须是可以被导入的，而且模块必须包含这个函数被打包时的名称，否则会抛出异常。3

同样的，类也只打包名称，所以在解包环境中也有和函数相同的限制。注意，类体及其数据不会被打包，所以在下面的例子中类属性 attr 不会存在于解包后的环境中：

class Foo:
    attr = 'A class attribute'

picklestring = pickle.dumps(Foo)

这些限制决定了为什么必须在一个模块的顶层定义可打包的函数和类。

类似的，在打包类的实例时，其类体和类数据不会跟着实例一起被打包，只有实例数据会被打包。这样设计是有目的的，在将来修复类中的错误、给类增加方法之后，仍然可以载入原来版本类实例的打包数据来还原该实例。如果你准备长期使用一个对象，可能会同时存在较多版本的类体，可以为对象添加版本号，这样就可以通过类的 __setstate__() 方法将老版本转换成新版本。

12.1.5. 打包类实例

在本节中，我们描述了可用于定义、自定义和控制如何打包和解包类实例的通用流程。

通常，使一个实例可被打包不需要附加任何代码。Pickle 默认会通过 Python 的内省机制获得实例的类及属性。而当实例解包时，它的 __init__() 方法通常 不会 被调用。其默认动作是：先创建一个未初始化的实例，然后还原其属性，下面的代码展示了这种行为的实现机制：

def save(obj):
    return (obj.__class__, obj.__dict__)

def load(cls, attributes):
    obj = cls.__new__(cls)
    obj.__dict__.update(attributes)
    return obj

类可以改变默认行为，只需定义以下一种或几种特殊方法：

object.__getnewargs_ex__()

对于使用第 2 版或更高版协议的 pickle，实现了 __getnewargs_ex__() 方法的类可以控制在解包时传给 __new__() 方法的参数。本方法必须返回一对 (args, kwargs) 用于构建对象，其中 args 是表示位置参数的 tuple，而 kwargs 是表示命名参数的 dict。它们会在解包时传递给 __new__() 方法。

如果类的 __new__() 方法只接受关键字参数，则应当实现这个方法。否则，为了兼容性，更推荐实现 __getnewargs__() 方法。

在 3.6 版更改: __getnewargs_ex__() 现在可用于第 2 和第 3 版协议。

object.__getnewargs__()

这个方法与上一个 __getnewargs_ex__() 方法类似，但仅支持位置参数。它要求返回一个 tuple 类型的 args，用于解包时传递给 __new__() 方法。

如果定义了 __getnewargs_ex__()，那么 __getnewargs__() 就不会被调用。

在 3.6 版更改: 在 Python 3.6 前，第 2、3 版协议会调用 __getnewargs__()，更高版本协议会调用 __getnewargs_ex__()。

object.__getstate__()

类还可以进一步控制其实例的打包过程。如果类定义了 __getstate__()，它就会被调用，其返回的对象是被当做实例内容来打包的，否则打包的是实例的 __dict__。如果 __getstate__() 未定义，实例的 __dict__ 会被照常打包。

object.__setstate__(state)

当解包时，如果类定义了 __setstate__()，就会在已解包状态下调用它。此时不要求实例的 state 对象必须是 dict。没有定义此方法的话，先前打包的 state 对象必须是 dict，且该 dict 内容会在解包时赋给新实例的 __dict__。

注解

如果 __getstate__() 返回 False，那么在解包时就不会调用 __setstate__() 方法。

参考 处理有状态的对象 一段获取如何使用 __getstate__() 和 __setstate__() 方法的更多信息。

注解

At unpickling time, some methods like __getattr__(), __getattribute__(), or __setattr__() may be called upon the instance. In case those methods rely on some internal invariant being true, the type should implement __getnewargs__() or __getnewargs_ex__() to establish such an invariant; otherwise, neither __new__() nor __init__() will be called.

可以看出，其实 pickle 并不直接调用上面的几个函数。事实上，这几个函数是复制协议的一部分，它们实现了 __reduce__() 这一特殊接口。复制协议提供了统一的接口，用于在打包或复制对象的过程中取得所需数据。4

尽管这个协议功能很强，但是直接在类中实现 __reduce__() 接口容易产生错误。因此，设计类时应当尽可能的使用高级接口（比如 __getnewargs_ex__()、__getstate__() 和 __setstate__()）。后面仍然可以看到直接实现 __reduce__() 接口的状况，可能别无他法，可能为了获得更好的性能，或者两者皆有之。

object.__reduce__()

该接口当前定义如下。__reduce__() 方法不带任何参数，并且应返回字符串或最好返回一个元组（返回的对象通常称为“reduce 值”）。

如果返回字符串，该字符串会被当做一个全局变量的名称。它应该是对象相对于其模块的本地名称，pickle 模块会搜索模块命名空间来确定对象所属的模块。这种行为常在单例模式使用。

当返回的是一个元组时，它的长度必须在二至五项之间。 可选项可以被省略或将值设为 None。 每项的语义分别如下所示：

一个可调用对象，该对象会在创建对象的最初版本时调用。

可调用对象的参数，是一个元组。如果可调用对象不接受参数，必须提供一个空元组。

可选元素，用于表示对象的状态，将被传给前述的 __setstate__() 方法。 如果对象没有此方法，则这个元素必须是字典类型，并会被添加至 __dict__ 属性中。

可选元素，一个返回连续项的迭代器（而不是序列）。这些项会被 obj.append(item) 逐个加入对象，或被 obj.extend(list_of_items) 批量加入对象。这个元素主要用于 list 的子类，也可以用于那些正确实现了 append() 和 extend() 方法的类。（具体是使用 append() 还是 extend() 取决于 pickle 协议版本以及待插入元素的项数，所以这两个方法必须同时被类支持。）

可选元素，一个返回连续键值对的迭代器（而不是序列）。这些键值对将会以 obj[key] = value 的方式存储于对象中。该元素主要用于 dict 子类，也可以用于那些实现了 __setitem__() 的类。

object.__reduce_ex__(protocol)

作为替代选项，也可以实现 __reduce_ex__() 方法。 此方法的唯一不同之处在于它应接受一个整型参数用于指定协议版本。 如果定义了这个函数，则会覆盖 __reduce__() 的行为。 此外，__reduce__() 方法会自动成为扩展版方法的同义词。 这个函数主要用于为以前的 Python 版本提供向后兼容的 reduce 值。

12.1.5.1. 持久化外部对象

为了获取对象持久化的利益， pickle 模块支持引用已封存数据流之外的对象。 这样的对象是通过一个持久化 ID 来引用的，它应当是一个由字母数字类字符组成的字符串 (对于第 0 版协议) 5 或是一个任意对象 (用于任意新版协议)。

The resolution of such persistent IDs is not defined by the pickle module; it will delegate this resolution to the user defined methods on the pickler and unpickler, persistent_id() and persistent_load() respectively.

To pickle objects that have an external persistent id, the pickler must have a custom persistent_id() method that takes an object as an argument and returns either None or the persistent id for that object. When None is returned, the pickler simply pickles the object as normal. When a persistent ID string is returned, the pickler will pickle that object, along with a marker so that the unpickler will recognize it as a persistent ID.

要解封外部对象，Unpickler 必须实现 persistent_load() 方法，接受一个持久化 ID 对象作为参数并返回一个引用的对象。

下面是一个全面的例子，展示了如何使用持久化 ID 来封存外部对象。

# Simple example presenting how persistent ID can be used to pickle
# external objects by reference.

import pickle
import sqlite3
from collections import namedtuple

# Simple class representing a record in our database.
MemoRecord = namedtuple("MemoRecord", "key, task")

class DBPickler(pickle.Pickler):

    def persistent_id(self, obj):
        # Instead of pickling MemoRecord as a regular class instance, we emit a
        # persistent ID.
        if isinstance(obj, MemoRecord):
            # Here, our persistent ID is simply a tuple, containing a tag and a
            # key, which refers to a specific record in the database.
            return ("MemoRecord", obj.key)
        else:
            # If obj does not have a persistent ID, return None. This means obj
            # needs to be pickled as usual.
            return None

class DBUnpickler(pickle.Unpickler):

    def __init__(self, file, connection):
        super().__init__(file)
        self.connection = connection

    def persistent_load(self, pid):
        # This method is invoked whenever a persistent ID is encountered.
        # Here, pid is the tuple returned by DBPickler.
        cursor = self.connection.cursor()
        type_tag, key_id = pid
        if type_tag == "MemoRecord":
            # Fetch the referenced record from the database and return it.
            cursor.execute("SELECT * FROM memos WHERE key=?", (str(key_id),))
            key, task = cursor.fetchone()
            return MemoRecord(key, task)
        else:
            # Always raises an error if you cannot return the correct object.
            # Otherwise, the unpickler will think None is the object referenced
            # by the persistent ID.
            raise pickle.UnpicklingError("unsupported persistent object")

def main():
    import io
    import pprint

    # Initialize and populate our database.
    conn = sqlite3.connect(":memory:")
    cursor = conn.cursor()
    cursor.execute("CREATE TABLE memos(key INTEGER PRIMARY KEY, task TEXT)")
    tasks = (
        'give food to fish',
        'prepare group meeting',
        'fight with a zebra',
        )
    for task in tasks:
        cursor.execute("INSERT INTO memos VALUES(NULL, ?)", (task,))

    # Fetch the records to be pickled.
    cursor.execute("SELECT * FROM memos")
    memos = [MemoRecord(key, task) for key, task in cursor]
    # Save the records using our custom DBPickler.
    file = io.BytesIO()
    DBPickler(file).dump(memos)

    print("Pickled records:")
    pprint.pprint(memos)

    # Update a record, just for good measure.
    cursor.execute("UPDATE memos SET task='learn italian' WHERE key=1")

    # Load the records from the pickle data stream.
    file.seek(0)
    memos = DBUnpickler(file, conn).load()

    print("Unpickled records:")
    pprint.pprint(memos)

if __name__ == '__main__':
    main()

12.1.5.2. Dispatch 表

如果想对某些类进行自定义封存，而又不想在类中增加用于封存的代码，就可以创建带有特殊 dispatch 表的 pickler。

在 copyreg 模块的 copyreg.dispatch_table 中定义了全局 dispatch 表。因此，可以使用 copyreg.dispatch_table 修改后的副本作为自有 dispatch 表。

例如：

f = io.BytesIO()
p = pickle.Pickler(f)
p.dispatch_table = copyreg.dispatch_table.copy()
p.dispatch_table[SomeClass] = reduce_SomeClass

创建了一个带有自有 dispatch 表的 pickle.Pickler 实例，它可以对 SomeClass 类进行特殊处理。另外，下列代码：

class MyPickler(pickle.Pickler):
    dispatch_table = copyreg.dispatch_table.copy()
    dispatch_table[SomeClass] = reduce_SomeClass
f = io.BytesIO()
p = MyPickler(f)

完成了相同的操作，但所有 MyPickler 的实例都会共用同一份 dispatch 表。使用 copyreg 模块实现的等效代码是：

copyreg.pickle(SomeClass, reduce_SomeClass)
f = io.BytesIO()
p = pickle.Pickler(f)

12.1.5.3. 处理有状态的对象

下面的示例展示了如何修改类在封存时的行为。其中 TextReader 类打开了一个文本文件，每次调用其 readline() 方法则返回行号和该行的字符。 在封存这个 TextReader 的实例时，除了 文件对象，其他属性都会被保存。 当解封实例时，需要重新打开文件，然后从上次的位置开始继续读取。实现这些功能需要实现 __setstate__() 和 __getstate__() 方法。

class TextReader:
    """Print and number lines in a text file."""

    def __init__(self, filename):
        self.filename = filename
        self.file = open(filename)
        self.lineno = 0

    def readline(self):
        self.lineno += 1
        line = self.file.readline()
        if not line:
            return None
        if line.endswith('\n'):
            line = line[:-1]
        return "%i: %s" % (self.lineno, line)

    def __getstate__(self):
        # Copy the object's state from self.__dict__ which contains
        # all our instance attributes. Always use the dict.copy()
        # method to avoid modifying the original state.
        state = self.__dict__.copy()
        # Remove the unpicklable entries.
        del state['file']
        return state

    def __setstate__(self, state):
        # Restore instance attributes (i.e., filename and lineno).
        self.__dict__.update(state)
        # Restore the previously opened file's state. To do so, we need to
        # reopen it and read from it until the line count is restored.
        file = open(self.filename)
        for _ in range(self.lineno):
            file.readline()
        # Finally, save the file.
        self.file = file

使用方法如下所示：

>>> reader = TextReader("hello.txt")
>>> reader.readline()
'1: Hello world!'
>>> reader.readline()
'2: I am line number two.'
>>> new_reader = pickle.loads(pickle.dumps(reader))
>>> new_reader.readline()
'3: Goodbye!'

12.1.6. 限制全局变量

默认情况下，解封将会导入在 pickle 数据中找到的任何类或函数。 对于许多应用来说，此行为是不可接受的，因为它会允许解封器导入并发起调用任意代码。 只须考虑当这个手工构建的 pickle 数据流被加载时会做什么:

>>> import pickle
>>> pickle.loads(b"cos\nsystem\n(S'echo hello world'\ntR.")
hello world
0

在这个例子里，解封器导入 os.system() 函数然后应用字符串参数 “echo hello world”。 虽然这个例子不具攻击性，但是不难想象别人能够通过此方式对你的系统造成损害。

出于这样的理由，你可能会希望通过定制 Unpickler.find_class() 来控制要解封的对象。 与其名称所提示的不同，Unpickler.find_class() 会在执行对任何全局对象（例如一个类或一个函数）的请求时被调用。 因此可以完全禁止全局对象或是将它们限制在一个安全的子集中。

下面的例子是一个解封器，它只允许某一些安全的来自 builtins 模块的类被加载:

import builtins
import io
import pickle

safe_builtins = {
    'range',
    'complex',
    'set',
    'frozenset',
    'slice',
}

class RestrictedUnpickler(pickle.Unpickler):

    def find_class(self, module, name):
        # Only allow safe classes from builtins.
        if module == "builtins" and name in safe_builtins:
            return getattr(builtins, name)
        # Forbid everything else.
        raise pickle.UnpicklingError("global '%s.%s' is forbidden" %
                                     (module, name))

def restricted_loads(s):
    """Helper function analogous to pickle.loads()."""
    return RestrictedUnpickler(io.BytesIO(s)).load()

我们这个解封器的一个示例用法所达成的目标:

>>> restricted_loads(pickle.dumps([1, 2, range(15)]))
[1, 2, range(0, 15)]
>>> restricted_loads(b"cos\nsystem\n(S'echo hello world'\ntR.")
Traceback (most recent call last):
  ...
pickle.UnpicklingError: global 'os.system' is forbidden
>>> restricted_loads(b'cbuiltins\neval\n'
...                  b'(S\'getattr(__import__("os"), "system")'
...                  b'("echo hello world")\'\ntR.')
Traceback (most recent call last):
  ...
pickle.UnpicklingError: global 'builtins.eval' is forbidden

正如我们这个例子所显示的，对于允许解封的对象你必须要保持谨慎。 因此如果要保证安全，你可以考虑其他选择例如 xmlrpc.client 中的编组 API 或是第三方解决方案。

12.1.7. 性能

较新版本的 pickle 协议（第 2 版或更高）具有针对某些常见特性和内置类型的高效二进制编码格式。 此外，pickle 模块还拥有一个以 C 编写的透明优化器。

12.1.8. 例子

对于最简单的代码，请使用 dump() 和 load() 函数。

import pickle

# An arbitrary collection of objects supported by pickle.
data = {
    'a': [1, 2.0, 3, 4+6j],
    'b': ("character string", b"byte string"),
    'c': {None, True, False}
}

with open('data.pickle', 'wb') as f:
    # Pickle the 'data' dictionary using the highest protocol available.
    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)

以下示例读取之前封存的数据。

import pickle

with open('data.pickle', 'rb') as f:
    # The protocol version used is detected automatically, so we do not
    # have to specify it.
    data = pickle.load(f)

参见

模块 copyreg

为扩展类型提供 pickle 接口所需的构造函数。

模块 pickletools

用于处理和分析已打包数据的工具。

模块 shelve

带索引的数据库，用于存放对象，使用了 pickle 模块。

模块 copy

浅层 (shallow) 和深层 (deep) 复制对象操作

模块 marshal

高效地序列化内置类型的数据。

备注

1

不要把它与 marshal 模块混淆。

2

This is why lambda functions cannot be pickled: all lambda functions share the same name: <lambda>.

3

抛出的异常有可能是 ImportError 或 AttributeError，也可能是其他异常。

4

copy 模块使用这一协议实现浅层 (shallow) 和深层 (deep) 复制操作。

5

对字母数字类字符的限制是由于持久化 ID 在协议版本 0 中是由分行符来分隔的。 因此如果持久化 ID 中出现任何形式的分行符，封存结果就将变得无法读取。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

12. 数据持久化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

12. 数据持久化 »

|

12.2. copyreg — 注意 pickle 支持函数

源代码: Lib/copyreg.py [https://github.com/python/cpython/tree/3.6/Lib/copyreg.py]

copyreg 模块提供了可在封存特定对象时使用的一种定义函数方式。 pickle 和 copy 模块会在封存/拷贝特定对象时使用这些函数。 此模块提供了非类对象构造器的相关配置信息。 这样的构造器可以是工厂函数或类实例。

copyreg.constructor(object)

将 object 声明为一个有效的构造器。 如果 object 是不可调用的（因而不是一个有效的构造器）则会引发 TypeError。

copyreg.pickle(type, function, constructor=None)

声明该 function 应当被用作 type 类型对象的“归约函数”。 function 应当返回字符串或包含两到三个元素的元组。

如果提供了可选的 constructor 形参，它应当是一个可用来重建相应对象的可调用对象，在调用该对象时应传入由 function 所返回的参数元组。 如果 object 是一个类或 constructor 是不可调用的则将引发 TypeError。

请查看 pickle 模块了解 function 和 constructor 所要求的接口的详情。 请注意一个 pickler 对象或 pickle.Pickler 的子类的 dispatch_table 属性也可以被用来声明归约函数。

12.2.1. 示例

以下示例将会显示如何注册一个封存函数，以及如何来使用它：

>>> import copyreg, copy, pickle
>>> class C(object):
...     def __init__(self, a):
...         self.a = a
...
>>> def pickle_c(c):
...     print("pickling a C instance...")
...     return C, (c.a,)
...
>>> copyreg.pickle(C, pickle_c)
>>> c = C(1)
>>> d = copy.copy(c)
pickling a C instance...
>>> p = pickle.dumps(c)
pickling a C instance...

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

12. 数据持久化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

12. 数据持久化 »

|

12.3. shelve — Python 对象持久化

源代码: Lib/shelve.py [https://github.com/python/cpython/tree/3.6/Lib/shelve.py]

“shelf” 是一种持久化的类似字典的对象。 与 “dbm” 数据库的区别在于 shelf 中的值（不是键！）实际上可以为任意 Python 对象 — 即 pickle 模块能够处理的任何东西。 这包括大部分类实例、递归数据类型，以及包含大量共享子对象的对象。 键则为普通的字符串。

shelve.open(filename, flag='c', protocol=None, writeback=False)

打开一个持久化字典。 filename 指定下层数据库的基准文件名。 作为附带效果，会为 filename 添加一个扩展名并且可能创建更多的文件。 默认情况下，下层数据库会以读写模式打开。 可选的 flag 形参具有与 dbm.open() flag 形参相同的含义。

默认会使用第 3 版 pickle 协议来序列化值。 pickle 协议版本可通过 protocol 形参来指定。

由于 Python 语义的限制，shelf 无法确定一个可变的持久化字典条目在何时被修改。 默认情况下 只有 在被修改对象再赋值给 shelf 时才会写入该对象 (参见 示例)。 如果可选的 writeback 形参设为 True，则所有被访问的条目都将在内存中被缓存，并会在 sync() 和 close() 时被写入；这可以使得对持久化字典中可变条目的修改更方便，但是如果访问的条目很多，这会消耗大量内存作为缓存，并会使得关闭操作变得非常缓慢，因为所有被访问的条目都需要写回到字典（无法确定被访问的条目中哪个是可变的，也无法确定哪个被实际修改了）。

注解

请不要依赖于 shelf 的自动关闭功能；当你不再需要时应当总是显式地调用 close()，或者使用 shelve.open() 作为上下文管理器:

with shelve.open('spam') as db:
    db['eggs'] = 'eggs'

警告

由于 shelve 模块需要 pickle 的支持，因此从不可靠的来源载入 shelf 是不安全的。 与 pickle 一样，载入 shelf 时可以执行任意代码。

字典所支持的所有方法都被 shelf 对象所支持。 因此很容易将基于字典的代码转换为需要持久化存储的代码。

额外支持的两个方法：

Shelf.sync()

如果 shelf 打开时将 writeback 设为 True 则写回缓存中的所有条目。 如果可行还会清空缓存并将持久化字典同步到磁盘。 此方法会在使用 close() 关闭 shelf 时自动被调用。

Shelf.close()

同步并关闭持久化 dict 对象。 对已关闭 shelf 的操作将失败并引发 ValueError。

参见

持久化字典方案 [https://code.activestate.com/recipes/576642/]，使用了广泛支持的存储格式并具有原生字典的速度。

12.3.1. 限制

可选择使用哪种数据库包 (例如 dbm.ndbm 或 dbm.gnu) 取决于支持哪种接口。 因此使用 dbm 直接打开数据库是不安全的。 如果使用了 dbm，数据库同样会（不幸地）受限于它 — 这意味着存储在数据库中的（封存形式的）对象尺寸应当较小，并且在少数情况下键冲突有可能导致数据库拒绝更新。

shelve 模块不支持对 shelve 对象的 并发 读/写访问。 （多个同时读取访问则是安全的。） 当一个程序打开一个 shelve 对象来写入时，不应再有其他程序同时打开它来读取或写入。 Unix 文件锁定可被用来解决此问题，但这在不同 Unix 版本上会存在差异，并且需要有关所用数据库实现的细节知识。

class shelve.Shelf(dict, protocol=None, writeback=False, keyencoding='utf-8')

collections.abc.MutableMapping 的一个子类，它会将封存的值保存在 dict 对象中。

默认会使用第 3 版 pickle 协议来序列化值。 pickle 协议版本可通过 protocol 形参来指定。 请参阅 pickle 文档来查看 pickle 协议的相关讨论。

如果 writeback 形参为 True，对象将为所有访问过的条目保留缓存并在同步和关闭时将它们写回到 dict。 这允许对可变的条目执行自然操作，但是会消耗更多内存并让同步和关闭花费更长时间。

keyencoding 形参是在下层字典被使用之前用于编码键的编码格式。

Shelf 对象还可以被用作上下文管理器，在这种情况下它将在 with 语句块结束时自动被关闭。

在 3.2 版更改: 添加了 keyencoding 形参；之前，键总是使用 UTF-8 编码。

在 3.4 版更改: 添加了上下文管理器支持。

class shelve.BsdDbShelf(dict, protocol=None, writeback=False, keyencoding='utf-8')

Shelf 的一个子类，将 first(), next(), previous(), last() 和 set_location() 对外公开，在来自 pybsddb [https://www.jcea.es/programacion/pybsddb.htm] 的第三方 bsddb 模块中可用，但在其他数据库模块中不可用。 传给构造器的 dict 对象必须支持这些方法。 这通常是通过调用 bsddb.hashopen(), bsddb.btopen() 或 bsddb.rnopen() 之一来完成的。 可选的 protocol, writeback 和 keyencoding 形参具有与 Shelf 类相同的含义。

class shelve.DbfilenameShelf(filename, flag='c', protocol=None, writeback=False)

Shelf 的一个子类，它接受一个 filename 而非字典类对象。 下层文件将使用 dbm.open() 来打开。 默认情况下，文件将以读写模式打开。 可选的 flag 形参具有与 open() 函数相同的含义。 可选的 protocol 和 writeback 形参具有与 Shelf 类相同的含义。

12.3.2. 示例

对接口的总结如下 (key 为字符串，data 为任意对象):

import shelve

d = shelve.open(filename)  # open -- file may get suffix added by low-level
                           # library

d[key] = data              # store data at key (overwrites old data if
                           # using an existing key)
data = d[key]              # retrieve a COPY of data at key (raise KeyError
                           # if no such key)
del d[key]                 # delete data stored at key (raises KeyError
                           # if no such key)

flag = key in d            # true if the key exists
klist = list(d.keys())     # a list of all existing keys (slow!)

# as d was opened WITHOUT writeback=True, beware:
d['xx'] = [0, 1, 2]        # this works as expected, but...
d['xx'].append(3)          # *this doesn't!* -- d['xx'] is STILL [0, 1, 2]!

# having opened d without writeback=True, you need to code carefully:
temp = d['xx']             # extracts the copy
temp.append(5)             # mutates the copy
d['xx'] = temp             # stores the copy right back, to persist it

# or, d=shelve.open(filename,writeback=True) would let you just code
# d['xx'].append(5) and have it work as expected, BUT it would also
# consume more memory and make the d.close() operation slower.

d.close()                  # close it

参见

模块 dbm

dbm 风格数据库的泛型接口。

模块 pickle

shelve 所使用的对象序列化。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

12. 数据持久化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

12. 数据持久化 »

|

12.4. marshal — 内部 Python 对象序列化

此模块包含一此能以二进制格式来读写 Python 值的函数。 这种格式是 Python 专属的，但是独立于特定的机器架构（即你可以在一台 PC 上写入某个 Python 值，将文件传到一台 Sun 上并在那里读取它）。 这种格式的细节有意不带文档说明；它可能在不同 Python 版本中发生改变（但这种情况极少发生）。 1

这不是一个通用的“持久化”模块。 对于通用的持久化以及通过 RPC 调用传递 Python 对象，请参阅 pickle 和 shelve 等模块。 marshal 模块主要是为了支持读写 .pyc 文件形式“伪编译”代码的 Python 模块。 因此，Python 维护者保留在必要时以不向下兼容的方式修改 marshal 格式的权利。 如果你要序列化和反序列化 Python 对象，请改用 pickle 模块 – 其执行效率相当，版本独立性有保证，并且 pickle 还支持比 marshal 更多样的对象类型。

警告

marshal 模块对于错误或恶意构建的数据来说是不安全的。 永远不要 unmarshal 来自不受信任的或未经验证的来源的数据。

不是所有 Python 对象类型都受支持；一般来说，此模块只能写入和读取不依赖于特定 Python 调用的对象。 下列类型是受支持的：布尔值、整数、浮点数、复数、字符串、字节串、字节数组、元组、列表、集合、冻结集合、字典和代码对象，需要了解的一点是元组、列表、集合、冻结集合和字典只在其所包含的值也是这些值时才受支持。 单例对象 None, Ellipsis and StopIteration 也可以被 marshal 和 unmarshal。 对于 version 低于 3 的格式，递归列表、集合和字典无法被写入（见下文）。

有些函数可以读/写文件，还有些函数可以操作字节类对象。

这个模块定义了以下函数：

marshal.dump(value, file[, version])

向打开的文件写入值。 值必须为受支持的类型。 文件必须为可写的 binary file。

如果值具有（或所包含的对象具有）不受支持的类型，则会引发 ValueError — 但是将向文件写入垃圾数据。 对象也将不能正确地通过 load() 重新读取。

version 参数指明 dump 应当使用的数据格式（见下文）。

marshal.load(file)

从打开的文件读取一个值并返回。 如果读不到有效的值（例如由于数据为不同 Python 版本的不兼容 marshal 格式），则会引发 EOFError, ValueError 或 TypeError。 文件必须为可读的 binary file。

注解

如果通过 dump() marshal 了一个包含不受支持类型的对象，load() 将为不可 marshal 的类型替换 None。

marshal.dumps(value[, version])

返回将通过 dump(value, file) 被写入一个文件的字节串对象。 值必须属于受支持的类型。 如果值属于（或包含的对象属于）不受支持的类型则会引发 ValueError。

version 参数指明 dumps 应当使用的数据类型（见下文）。

marshal.loads(bytes)

将 bytes-like object 转换为一个值。 如果找不到有效的值，则会引发 EOFError, ValueError 或 TypeError。 输入的额外字节串会被忽略。

此外，还定义了以下常量：

marshal.version

指明模块所使用的格式。 第 0 版为历史格式，第 1 版为共享固化的字符串，第 2 版对浮点数使用二进制格式。 第 3 版添加了对于对象实例化和递归的支持。 目前使用的为第 4 版。

备注

1

此模块的名称来源于 Modula-3 (及其他语言) 的设计者所使用的术语，他们使用术语 “marshal” 来表示以自包含的形式传输数据。 严格地说，将数据从内部形式转换为外部形式 (例如用于 RPC 缓冲区) 称为 “marshal” 而其逆过程则称为 “unmarshal”。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

12. 数据持久化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

12. 数据持久化 »

|

12.5. dbm — Unix “数据库” 接口

源代码: Lib/dbm/__init__.py [https://github.com/python/cpython/tree/3.6/Lib/dbm/__init__.py]

dbm 是一种泛用接口，针对各种 DBM 数据库 — 包括 dbm.gnu 或 dbm.ndbm。 如果未安装这些模块中的任何一种，则将使用 dbm.dumb 模块中慢速但简单的实现。 还有一个适用于 Oracle Berkeley DB 的 第三方接口 [https://www.jcea.es/programacion/pybsddb.htm]。

exception dbm.error

一个元组，其中包含每个受支持的模块可引发的异常，另外还有一个名为 dbm.error 的特殊异常作为第一项 — 后者最在引发 dbm.error 时被使用。

dbm.whichdb(filename)

此函数会猜测各种简单数据库模块中的哪一个是可用的 — dbm.gnu, dbm.ndbm 还是 dbm.dumb — 应该被用来打开给定的文件。

返回下列值中的一个：如果文件由于不可读或不存在而无法打开则返回 None；如果文件的格式无法猜测则返回空字符串 ('')；或是包含所需模块名称的字符串，例如 'dbm.ndbm' 或 'dbm.gnu'。

dbm.open(file, flag='r', mode=0o666)

打开数据库文件 file 并返回一个相应的对象。

如果数据库文件已存在，则使用 whichdb() 函数来确定其类型和要使用的适当模块；如果文件不存在，则会使用上述可导入模块中的第一个。

可选的 flag 参数可以是：

值

含义

'r'

以只读方式打开现有数据库（默认）

'w'

以读写方式打开现有数据库

'c'

以读写方式打开数据库，如果不存在则创建它

'n'

始终创建一个新的空数据库，以读写方式打开

可选的 mode 参数是文件的 Unix 模式，仅在要创建数据库时才会被使用。 其默认值为八进制数 0o666 (并将被当前的 umask 所修改)。

open() 所返回的对象支持与字典相同的基本功能；可以存储、获取和删除键及其对应的值，并可使用 in 运算符和 keys() 方法，以及 get() 和 setdefault()。

在 3.2 版更改: 现在 get() 和 setdefault() 在所有数据库模块中均可用。

键和值总是被存储为字节串。 这意味着当使用字符串时它们会在被存储之前隐式地转换至默认编码格式。

这些对象也支持在 with 语句中使用，当语句结束时将自动关闭它们。

在 3.4 版更改: 向 open() 所返回的对象添加了上下文管理协议的原生支持。

以下示例记录了一些主机名和对应的标题，随后将数据库的内容打印出来。:

import dbm

# Open database, creating it if necessary.
with dbm.open('cache', 'c') as db:

    # Record some values
    db[b'hello'] = b'there'
    db['www.python.org'] = 'Python Website'
    db['www.cnn.com'] = 'Cable News Network'

    # Note that the keys are considered bytes now.
    assert db[b'www.python.org'] == b'Python Website'
    # Notice how the value is now in bytes.
    assert db['www.cnn.com'] == b'Cable News Network'

    # Often-used methods of the dict interface work too.
    print(db.get('python.org', b'not present'))

    # Storing a non-string key or value will raise an exception (most
    # likely a TypeError).
    db['www.yahoo.com'] = 4

# db is automatically closed when leaving the with statement.

参见

模块 shelve

存储非字符串数据的持久化模块。

以下部分描述了各个单独的子模块。

12.5.1. dbm.gnu — GNU 对 dbm 的重解析

源代码: Lib/dbm/gnu.py [https://github.com/python/cpython/tree/3.6/Lib/dbm/gnu.py]

此模块与 dbm 模块很相似，但是改用 GNU 库 gdbm 来提供某些附加功能。 请注意由 dbm.gnu 与 dbm.ndbm 所创建的文件格式是不兼容的。

dbm.gnu 模块提供了对 GNU DBM 库的接口。 dbm.gnu.gdbm 对象的行为类似于映射（字典），区别在于其键和值总是会在存储之前被转换为字节串。 打印 gdbm 对象不会打印出键和值，并且 items() 和 values() 等方法也不受支持。

exception dbm.gnu.error

针对 dbm.gnu 专属错误例如 I/O 错误引发。 KeyError 的引发则针对一般映射错误例如指定了不正确的键。

dbm.gnu.open(filename[, flag[, mode]])

打开一个 gdbm 数据库并返回 gdbm 对象。 filename 参数为数据库文件名称。

可选的 flag 参数可以是：

值

含义

'r'

以只读方式打开现有数据库（默认）

'w'

以读写方式打开现有数据库

'c'

以读写方式打开数据库，如果不存在则创建它

'n'

始终创建一个新的空数据库，以读写方式打开

下列附加字符可被添加至旗标以控制数据库的打开方式：

值

含义

'f'

以快速模式打开数据库。写入数据库将不会同步。

's'

同步模式。这将导致数据库的更改立即写入文件。

'u'

不要锁定数据库。

不是所有旗标都可用于所有版本的 gdbm。 模块常量 open_flags 为包含受支持旗标字符的字符串。 如果指定了无效的旗标则会引发 error。

可选的 mode 参数是文件的 Unix 模式，仅在要创建数据库时才会被使用。 其默认值为八进制数 0o666。

除了与字典类似的方法，gdbm 对象还有以下方法：

gdbm.firstkey()

使用此方法和 nextkey() 方法可以循环遍历数据库中的每个键。 遍历的顺序是按照 gdbm 的内部哈希值，而不会根据键的值排序。 此方法将返回起始键。

gdbm.nextkey(key)

在遍历中返回 key 之后的的下一个键。 以下代码将打印数据库 db 中的每个键，而不会在内存中创建一个包含所有键的列表:

k = db.firstkey()
while k != None:
    print(k)
    k = db.nextkey(k)

gdbm.reorganize()

如果你进行了大量删除操作并且想要缩减 gdbm 文件所使用的空间，此例程将可重新组织数据库。 除非使用此重组功能否则 gdbm 对象不会缩减数据库文件大小；在其他情况下，被删除的文件空间将会保留并在添加新的 (键, 值) 对时被重用。

gdbm.sync()

当以快速模式打开数据库时，此方法会将任何未写入数据强制写入磁盘。

gdbm.close()

关闭 gdbm 数据库。

12.5.2. dbm.ndbm — 基于 ndbm 的接口

源代码: Lib/dbm/ndbm.py [https://github.com/python/cpython/tree/3.6/Lib/dbm/ndbm.py]

dbm.ndbm 模块提供了对 Unix “(n)dbm” 库的接口。 Dbm 对象的行为类似于映射（字典），区别在于其键和值总是被存储为字节串。 打印 dbm 对象不会打印出键和值，并且 items() 和 values() 等方法也不受支持。

此模块可与 “经典classic” ndbm 接口或 GNU GDBM 兼容接口一同使用。 在 Unix 上，configure 脚本将尝试定位适当的头文件来简化此模块的构建。

exception dbm.ndbm.error

针对 dbm.ndbm 专属错误例如 I/O 错误引发。 KeyError 的引发则针对一般映射错误例如指定了不正确的键。

dbm.ndbm.library

所使用的 ndbm 实现库的名称。

dbm.ndbm.open(filename[, flag[, mode]])

打开一个 dbm 数据库并返回 ndbm 对象。 filename 参数为数据库文件名称（不带 .dir 或 .pag 扩展名）。

可选的 flag 参数必须是下列值之一：

值

含义

'r'

以只读方式打开现有数据库（默认）

'w'

以读写方式打开现有数据库

'c'

以读写方式打开数据库，如果不存在则创建它

'n'

始终创建一个新的空数据库，以读写方式打开

可选的 mode 参数是文件的 Unix 模式，仅在要创建数据库时才会被使用。 其默认值为八进制数 0o666 (并将被当前的 umask 所修改)。

除了与字典类似的方法，ndbm 对象还有以下方法：

ndbm.close()

关闭 ndbm 数据库。

12.5.3. dbm.dumb — 便携式 DBM 实现

源代码: Lib/dbm/dumb.py [https://github.com/python/cpython/tree/3.6/Lib/dbm/dumb.py]

注解

dbm.dumb 模块的目的是在更健壮的模块不可用时作为 dbm 模块的最终回退项。 dbm.dumb 不是为高速运行而编写的，也不像其他数据库模块一样被经常使用。

dbm.dumb 模块提供了一个完全以 Python 编写的持久化字典类接口。 不同于 dbm.gnu 等其他模块，它不需要外部库。 与其他持久化映射一样，它的键和值也总是被存储为字节串。

该模块定义以下内容：

exception dbm.dumb.error

针对 dbm.dumb 专属错误例如 I/O 错误引发。 KeyError 的引发则针对一般映射例如指定了不正确的键。

dbm.dumb.open(filename[, flag[, mode]])

打开一个 dumbdbm 数据库并返回 dumbdbm 对象。 filename 参数为数据库文件的主名称（不带任何特定扩展名）。 创建一个 dumbdbm 数据库时将创建多个带有 .dat 和 .dir 扩展名的文件。

可选的 flag 参数仅支持 'c' 和 'n' 这两个值的语义。 其他值将默认设为数据库总是打开为可更新，并且在数据库不存在时将被创建。

可选的 mode 参数是文件的 Unix 模式，仅在要创建数据库时才会被使用。 其默认值为八进制数 0o666 (并将被当前的 umask 所修改)。

在 3.5 版更改: open() 在 flag 值为 'n' 时将总是创建一个新的数据库。

Deprecated since version 3.6, will be removed in version 3.8: 创建数据库使用 'r' 和 'w' 模式。 修改数据库使用 'r' 模式。

除了 collections.abc.MutableMapping 类所提供的方法，dumbdbm 对象还提供了以下方法：

dumbdbm.sync()

同步磁盘上的目录和数据文件。 此方法会由 Shelve.sync() 方法来调用。

dumbdbm.close()

关闭 dumbdbm 数据库。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

12. 数据持久化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

12. 数据持久化 »

|

12.6. sqlite3 — SQLite 数据库 DB-API 2.0 接口模块

源代码： Lib/sqlite3/ [https://github.com/python/cpython/tree/3.6/Lib/sqlite3/]

SQLite 是一个C语言库，它可以提供一种轻量级的基于磁盘的数据库，这种数据库不需要独立的服务器进程，也允许需要使用一种非标准的 SQL 查询语言来访问它。一些应用程序可以使用 SQLite 作为内部数据存储。可以用它来创建一个应用程序原型，然后再迁移到更大的数据库，比如 PostgreSQL 或 Oracle。

sqlite3 模块由 Gerhard Häring 编写。它提供了符合 DB-API 2.0 规范的接口，这个规范是 PEP 249 [https://www.python.org/dev/peps/pep-0249]。

要使用这个模块，必须先创建一个 Connection 对象，它代表数据库。下面例子中，数据将存储在 example.db 文件中：

import sqlite3
conn = sqlite3.connect('example.db')

你也可以使用 :memory: 来创建一个内存中的数据库

当有了 Connection 对象后，你可以创建一个 Cursor 游标对象，然后调用它的 execute() 方法来执行 SQL 语句：

c = conn.cursor()

# Create table
c.execute('''CREATE TABLE stocks
             (date text, trans text, symbol text, qty real, price real)''')

# Insert a row of data
c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")

# Save (commit) the changes
conn.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()

这些数据被持久化保存了，而且可以在之后的会话中使用它们：

import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()

通常你的 SQL 操作需要使用一些 Python 变量的值。你不应该使用 Python 的字符串操作来创建你的查询语句，因为那样做不安全；它会使你的程序容易受到 SQL 注入攻击（在 https://xkcd.com/327/ 上有一个搞笑的例子，看看有什么后果）

推荐另外一种方法：使用 DB-API 的参数替换。在你的 SQL 语句中，使用 ? 占位符来代替值，然后把对应的值组成的元组做为 execute() 方法的第二个参数。（其他数据库可能会使用不同的占位符，比如 %s 或者 :1）例如：

# Never do this -- insecure!
symbol = 'RHAT'
c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)

# Do this instead
t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
print(c.fetchone())

# Larger example that inserts many records at a time
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
             ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
             ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
            ]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)

要在执行 SELECT 语句后获取数据，你可以把游标作为 iterator，然后调用它的 fetchone() 方法来获取一条匹配的行，也可以调用 fetchall() 来得到包含多个匹配行的列表。

下面是一个使用迭代器形式的例子：

>>> for row in c.execute('SELECT * FROM stocks ORDER BY price'):
        print(row)

('2006-01-05', 'BUY', 'RHAT', 100, 35.14)
('2006-03-28', 'BUY', 'IBM', 1000, 45.0)
('2006-04-06', 'SELL', 'IBM', 500, 53.0)
('2006-04-05', 'BUY', 'MSFT', 1000, 72.0)

参见

https://github.com/ghaering/pysqlite

pysqlite的主页 – sqlite3 在外部使用 “pysqlite” 名字进行开发。

https://www.sqlite.org

SQLite的主页；它的文档详细描述了它所支持的 SQL 方言的语法和可用的数据类型。

http://www.w3schools.com/sql/

学习 SQL 语法的教程、参考和例子。

PEP 249 [https://www.python.org/dev/peps/pep-0249] - DB-API 2.0 规范

Marc-André Lemburg 写的 PEP。

12.6.1. 模块函数和常量

sqlite3.version

这个模块的版本号，是一个字符串。不是 SQLite 库的版本号。

sqlite3.version_info

这个模块的版本号，是一个由整数组成的元组。不是 SQLite 库的版本号。

sqlite3.sqlite_version

使用中的 SQLite 库的版本号，是一个字符串。

sqlite3.sqlite_version_info

使用中的 SQLite 库的版本号，是一个整数组成的元组。

sqlite3.PARSE_DECLTYPES

这个常量可以作为 connect() 函数的 detect_types 参数。

设置这个参数后，sqlite3 模块将解析它返回的每一列申明的类型。它会申明的类型的第一个单词，比如“integer primary key”，它会解析出“integer”，再比如“number(10)”，它会解析出“number”。然后，它会在转换器字典里查找那个类型注册的转换器函数，并调用它。

sqlite3.PARSE_COLNAMES

这个常量可以作为 connect() 函数的 detect_types 参数。

Setting this makes the SQLite interface parse the column name for each column it returns. It will look for a string formed [mytype] in there, and then decide that ‘mytype’ is the type of the column. It will try to find an entry of ‘mytype’ in the converters dictionary and then use the converter function found there to return the value. The column name found in Cursor.description is only the first word of the column name, i. e. if you use something like 'as "x [datetime]"' in your SQL, then we will parse out everything until the first blank for the column name: the column name would simply be “x”.

sqlite3.connect(database[, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements, uri])

Opens a connection to the SQLite database file database. You can use ":memory:" to open a database connection to a database that resides in RAM instead of on disk.

当一个数据库被多个连接访问的时候，如果其中一个进程修改这个数据库，在这个事务提交之前，这个 SQLite 数据库将会被一直锁定。timeout 参数指定了这个连接等待锁释放的超时时间，超时之后会引发一个异常。这个超时时间默认是 5.0（5秒）。

isolation_level 参数，请查看 Connection 对象的 isolation_level 属性。

SQLite 原生只支持5种类型：TEXT，INTEGER，REAL，BLOB 和 NULL。如果你想用其它类型，你必须自己添加相应的支持。使用 detect_types 参数和模块级别的 register_converter() 函数注册**转换器** 可以简单的实现。

detect_types 默认为0（即关闭，没有类型检测）。你也可以组合 PARSE_DECLTYPES 和 PARSE_COLNAMES 来开启类型检测。

默认情况下，check_same_thread 为 True，只有当前的线程可以使用该连接。 如果设置为 False，则多个线程可以共享返回的连接。 当多个线程使用同一个连接的时候，用户应该把写操作进行序列化，以避免数据损坏。

默认情况下，当调用 connect 方法的时候，sqlite3 模块使用了它的 Connection 类。当然，你也可以创建 Connection 类的子类，然后创建提供了 factory 参数的 connect() 方法。

详情请查阅当前手册的 SQLite 与 Python 类型 部分。

sqlite3 模块在内部使用语句缓存来避免 SQL 解析开销。 如果要显式设置当前连接可以缓存的语句数，可以设置 cached_statements 参数。 当前实现的默认值是缓存100条语句。

如果 uri 为真，则 database 被解释为 URI。 它允许您指定选项。 例如，以只读模式打开数据库：

db = sqlite3.connect('file:path/to/database?mode=ro', uri=True)

有关此功能的更多信息，包括已知选项的列表，可以在 ` SQLite URI 文档 <https://www.sqlite.org/uri.html>`_ 中找到。

在 3.4 版更改: 增加了 uri 参数。

sqlite3.register_converter(typename, callable)

注册一个回调对象 callable, 用来转换数据库中的字节串为自定的 Python 类型。所有类型为 typename 的数据库的值在转换时，都会调用这个回调对象。通过指定 connect() 函数的 detect-types 参数来设置类型检测的方式。注意，typename 与查询语句中的类型名进行匹配时不区分大小写。

sqlite3.register_adapter(type, callable)

注册一个回调对象 callable，用来转换自定义Python类型为一个 SQLite 支持的类型。 这个回调对象 callable 仅接受一个 Python 值作为参数，而且必须返回以下某个类型的值：int，float，str 或 bytes。

sqlite3.complete_statement(sql)

如果字符串 sql 包含一个或多个完整的 SQL 语句（以分号结束）则返回 True。它不会验证 SQL 语法是否正确，仅会验证字符串字面上是否完整，以及是否以分号结束。

它可以用来构建一个 SQLite shell，下面是一个例子：

# A minimal SQLite shell for experiments

import sqlite3

con = sqlite3.connect(":memory:")
con.isolation_level = None
cur = con.cursor()

buffer = ""

print("Enter your SQL commands to execute in sqlite3.")
print("Enter a blank line to exit.")

while True:
    line = input()
    if line == "":
        break
    buffer += line
    if sqlite3.complete_statement(buffer):
        try:
            buffer = buffer.strip()
            cur.execute(buffer)

            if buffer.lstrip().upper().startswith("SELECT"):
                print(cur.fetchall())
        except sqlite3.Error as e:
            print("An error occurred:", e.args[0])
        buffer = ""

con.close()

sqlite3.enable_callback_tracebacks(flag)

默认情况下，您不会获得任何用户定义函数中的回溯消息，比如聚合，转换器，授权器回调等。如果要调试它们，可以设置 flag 参数为 True 并调用此函数。 之后，回调中的回溯信息将会输出到 sys.stderr。 再次使用 False 来禁用该功能。

12.6.2. 连接对象（Connection）

class sqlite3.Connection

SQLite 数据库连接对象有如下的属性和方法：

isolation_level

获取或设置当前默认的隔离级别。 表示自动提交模式的 None 以及 “DEFERRED”, “IMMEDIATE” 或 “EXCLUSIVE” 其中之一。 详细描述请参阅 控制事务。

in_transaction

如果是在活动事务中（还没有提交改变），返回 True，否则，返回 False。它是一个只读属性。

3.2 新版功能.

cursor(factory=Cursor)

这个方法接受一个可选参数 factory，如果要指定这个参数，它必须是一个可调用对象，而且必须返回 Cursor 类的一个实例或者子类。

commit()

这个方法提交当前事务。如果没有调用这个方法，那么从上一次提交 commit() 以来所有的变化在其他数据库连接上都是不可见的。如果你往数据库里写了数据，但是又查询不到，请检查是否忘记了调用这个方法。

rollback()

这个方法回滚从上一次调用 commit() 以来所有数据库的改变。

close()

关闭数据库连接。注意，它不会自动调用 commit() 方法。如果在关闭数据库连接之前没有调用 commit()，那么你的修改将会丢失！

execute(sql[, parameters])

这是一个非标准的快捷方法，它会调用 cursor() 方法来创建一个游标对象，并使用给定的 parameters 参数来调用游标对象的 execute() 方法，最后返回这个游标对象。

executemany(sql[, parameters])

这是一个非标准的快捷方法，它会调用 cursor() 方法来创建一个游标对象，并使用给定的 parameters 参数来调用游标对象的 executemany() 方法，最后返回这个游标对象。

executescript(sql_script)

这是一个非标准的快捷方法，它会调用 cursor() 方法来创建一个游标对象，并使用给定的 sql_script 参数来调用游标对象的 executescript() 方法，最后返回这个游标对象。

create_function(name, num_params, func)

创建一个可以在 SQL 语句中使用的自定义函数，其中参数 name 为 SQL 语句中使用的函数名，num_params 是这个函数接受的参数个数（如果 num_params 为 -1，那这个函数可以接受任意数量的参数），最后一个参数 func 是作为 SQL 函数调用的一个 Python 可调用对象。

此函数可返回任何 SQLite 所支持的类型: bytes, str, int, float 和 None。

示例:

import sqlite3
import hashlib

def md5sum(t):
    return hashlib.md5(t).hexdigest()

con = sqlite3.connect(":memory:")
con.create_function("md5", 1, md5sum)
cur = con.cursor()
cur.execute("select md5(?)", (b"foo",))
print(cur.fetchone()[0])

create_aggregate(name, num_params, aggregate_class)

创建一个自定义的聚合函数。

参数中 aggregate_class 类必须实现两个方法：step 和 finalize。step 方法接受 num_params 个参数（如果 num_params 为 -1，那么这个函数可以接受任意数量的参数）；finalize 方法返回最终的聚合结果。

finalize 方法可以返回任何 SQLite 支持的类型：bytes，str，int，float 和 None。

示例:

import sqlite3

class MySum:
    def __init__(self):
        self.count = 0

    def step(self, value):
        self.count += value

    def finalize(self):
        return self.count

con = sqlite3.connect(":memory:")
con.create_aggregate("mysum", 1, MySum)
cur = con.cursor()
cur.execute("create table test(i)")
cur.execute("insert into test(i) values (1)")
cur.execute("insert into test(i) values (2)")
cur.execute("select mysum(i) from test")
print(cur.fetchone()[0])

create_collation(name, callable)

使用 name 和 callable 创建排序规则。这个 callable 接受两个字符串对象，如果第一个小于第二个则返回 -1， 如果两个相等则返回 0，如果第一个大于第二个则返回 1。注意，这是用来控制排序的（SQL 中的 ORDER BY），所以它不会影响其它的 SQL 操作。

注意，这个 callable 可调用对象会把它的参数作为 Python 字节串，通常会以 UTF-8 编码格式对它进行编码。

以下示例显示了使用“错误方式”进行排序的自定义排序规则：

import sqlite3

def collate_reverse(string1, string2):
    if string1 == string2:
        return 0
    elif string1 < string2:
        return 1
    else:
        return -1

con = sqlite3.connect(":memory:")
con.create_collation("reverse", collate_reverse)

cur = con.cursor()
cur.execute("create table test(x)")
cur.executemany("insert into test(x) values (?)", [("a",), ("b",)])
cur.execute("select x from test order by x collate reverse")
for row in cur:
    print(row)
con.close()

要移除一个排序规则，需要调用 create_collation 并设置 callable 参数为 None。

con.create_collation("reverse", None)

interrupt()

可以从不同的线程调用这个方法来终止所有查询操作，这些查询操作可能正在连接上执行。此方法调用之后， 查询将会终止，而且查询的调用者会获得一个异常。

set_authorizer(authorizer_callback)

此方法注册一个授权回调对象。每次在访问数据库中某个表的某一列的时候，这个回调对象将会被调用。如果要允许访问，则返回 SQLITE_OK，如果要终止整个 SQL 语句，则返回 SQLITE_DENY，如果这一列需要当做 NULL 值处理，则返回 SQLITE_IGNORE。这些常量可以在 sqlite3 模块中找到。

回调的第一个参数表示要授权的操作类型。 第二个和第三个参数将是参数或 None，具体取决于第一个参数的值。 第 4 个参数是数据库的名称（“main”，“temp”等），如果需要的话。 第 5 个参数是负责访问尝试的最内层触发器或视图的名称，或者如果此访问尝试直接来自输入 SQL 代码，则为 None。

请参阅 SQLite 文档，了解第一个参数的可能值以及第二个和第三个参数的含义，具体取决于第一个参数。 所有必需的常量都可以在 sqlite3 模块中找到。

set_progress_handler(handler, n)

此例程注册回调。 对SQLite虚拟机的每个多指令调用回调。 如果要在长时间运行的操作期间从SQLite调用（例如更新用户界面），这非常有用。

如果要清除以前安装的任何进度处理程序，调用该方法时请将 handler 参数设置为 None。

从处理函数返回非零值将终止当前正在执行的查询并导致它引发 OperationalError 异常。

set_trace_callback(trace_callback)

为每个 SQLite 后端实际执行的 SQL 语句注册要调用的 trace_callback。

传递给回调的唯一参数是正在执行的语句（作为字符串）。 回调的返回值将被忽略。 请注意，后端不仅运行传递给 Cursor.execute() 方法的语句。 其他来源包括 Python 模块的事务管理和当前数据库中定义的触发器的执行。

将传入的 trace_callback 设为 None 将禁用跟踪回调。

3.3 新版功能.

enable_load_extension(enabled)

此例程允许/禁止SQLite引擎从共享库加载SQLite扩展。 SQLite扩展可以定义新功能，聚合或全新的虚拟表实现。 一个众所周知的扩展是与SQLite一起分发的全文搜索扩展。

默认情况下禁用可加载扩展。 见 1.

3.2 新版功能.

import sqlite3

con = sqlite3.connect(":memory:")

# enable extension loading
con.enable_load_extension(True)

# Load the fulltext search extension
con.execute("select load_extension('./fts3.so')")

# alternatively you can load the extension using an API call:
# con.load_extension("./fts3.so")

# disable extension loading again
con.enable_load_extension(False)

# example from SQLite wiki
con.execute("create virtual table recipe using fts3(name, ingredients)")
con.executescript("""
    insert into recipe (name, ingredients) values ('broccoli stew', 'broccoli peppers cheese tomatoes');
    insert into recipe (name, ingredients) values ('pumpkin stew', 'pumpkin onions garlic celery');
    insert into recipe (name, ingredients) values ('broccoli pie', 'broccoli cheese onions flour');
    insert into recipe (name, ingredients) values ('pumpkin pie', 'pumpkin sugar flour butter');
    """)
for row in con.execute("select rowid, name, ingredients from recipe where name match 'pie'"):
    print(row)

load_extension(path)

此例程从共享库加载SQLite扩展。 在使用此例程之前，必须使用 enable_load_extension() 启用扩展加载。

默认情况下禁用可加载扩展。 见 1.

3.2 新版功能.

row_factory

您可以将此属性更改为可接受游标和原始行作为元组的可调用对象，并将返回实际结果行。 这样，您可以实现更高级的返回结果的方法，例如返回一个可以按名称访问列的对象。

示例:

import sqlite3

def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d

con = sqlite3.connect(":memory:")
con.row_factory = dict_factory
cur = con.cursor()
cur.execute("select 1 as a")
print(cur.fetchone()["a"])

如果返回一个元组是不够的，并且你想要对列进行基于名称的访问，你应该考虑将 row_factory 设置为高度优化的 sqlite3.Row 类型。 Row 提供基于索引和不区分大小写的基于名称的访问，几乎没有内存开销。 它可能比您自己的基于字典的自定义方法甚至基于 db_row 的解决方案更好。

text_factory

使用此属性可以控制为 TEXT 数据类型返回的对象。 默认情况下，此属性设置为 str 和 sqlite3 模块将返回 TEXT 的 Unicode 对象。 如果要返回字节串，可以将其设置为 bytes。

您还可以将其设置为接受单个 bytestring 参数的任何其他可调用对象，并返回结果对象。

请参阅以下示例代码以进行说明：

import sqlite3

con = sqlite3.connect(":memory:")
cur = con.cursor()

AUSTRIA = "\xd6sterreich"

# by default, rows are returned as Unicode
cur.execute("select ?", (AUSTRIA,))
row = cur.fetchone()
assert row[0] == AUSTRIA

# but we can make sqlite3 always return bytestrings ...
con.text_factory = bytes
cur.execute("select ?", (AUSTRIA,))
row = cur.fetchone()
assert type(row[0]) is bytes
# the bytestrings will be encoded in UTF-8, unless you stored garbage in the
# database ...
assert row[0] == AUSTRIA.encode("utf-8")

# we can also implement a custom text_factory ...
# here we implement one that appends "foo" to all strings
con.text_factory = lambda x: x.decode("utf-8") + "foo"
cur.execute("select ?", ("bar",))
row = cur.fetchone()
assert row[0] == "barfoo"

total_changes

返回自打开数据库连接以来已修改，插入或删除的数据库行的总数。

iterdump()

返回以SQL文本格式转储数据库的迭代器。 保存内存数据库以便以后恢复时很有用。 此函数提供与 sqlite3 shell 中的 .dump 命令相同的功能。

示例:

# Convert file existing_db.db to SQL dump file dump.sql
import sqlite3

con = sqlite3.connect('existing_db.db')
with open('dump.sql', 'w') as f:
    for line in con.iterdump():
        f.write('%s\n' % line)

12.6.3. Cursor 对象

class sqlite3.Cursor

Cursor 游标实例具有以下属性和方法。

execute(sql[, parameters])

执行SQL语句。 可以是参数化 SQL 语句（即，在 SQL 语句中使用占位符）。sqlite3 模块支持两种占位符：问号（qmark风格）和命名占位符（命名风格）。

以下是两种风格的示例：

import sqlite3

con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("create table people (name_last, age)")

who = "Yeltsin"
age = 72

# This is the qmark style:
cur.execute("insert into people values (?, ?)", (who, age))

# And this is the named style:
cur.execute("select * from people where name_last=:who and age=:age", {"who": who, "age": age})

print(cur.fetchone())

execute() 将只执行一条单独的 SQL 语句。 如果你尝试用它执行超过一条语句，将会引发 Warning。 如果你想要用一次调用执行多条 SQL 语句请使用 executescript()。

executemany(sql, seq_of_parameters)

基于在序列 seq_of_parameters 中找到的所有形参序列或映射执行一条 SQL 命令。 sqlite3 模块还允许使用 iterator 代替序列来产生形参。

import sqlite3

class IterChars:
    def __init__(self):
        self.count = ord('a')

    def __iter__(self):
        return self

    def __next__(self):
        if self.count > ord('z'):
            raise StopIteration
        self.count += 1
        return (chr(self.count - 1),) # this is a 1-tuple

con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("create table characters(c)")

theIter = IterChars()
cur.executemany("insert into characters(c) values (?)", theIter)

cur.execute("select c from characters")
print(cur.fetchall())

这是一个使用生成器 generator 的简短示例：

import sqlite3
import string

def char_generator():
    for c in string.ascii_lowercase:
        yield (c,)

con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("create table characters(c)")

cur.executemany("insert into characters(c) values (?)", char_generator())

cur.execute("select c from characters")
print(cur.fetchall())

executescript(sql_script)

这是一个非标准的便捷方法，可用于一次执行多条 SQL 语句。 它会首先执行一条 COMMIT 语句，再执行以形参方式获取的 SQL 脚本。

sql_script 可以是一个 str 类的实例。

示例:

import sqlite3

con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.executescript("""
    create table person(
        firstname,
        lastname,
        age
    );

    create table book(
        title,
        author,
        published
    );

    insert into book(title, author, published)
    values (
        'Dirk Gently''s Holistic Detective Agency',
        'Douglas Adams',
        1987
    );
    """)

fetchone()

获取一个查询结果集的下一行，返回一个单独序列，或是在没有更多可用数据时返回 None。

fetchmany(size=cursor.arraysize)

获取下一个多行查询结果集，返回一个列表。 当没有更多可用行时将返回一个空列表。

每次调用获取的行数由 size 形参指定。 如果没有给出该形参，则由 cursor 的 arraysize 决定要获取的行数。 此方法将基于 size 形参值尝试获取指定数量的行。 如果获取不到指定的行数，则可能返回较少的行。

请注意 size 形参会涉及到性能方面的考虑。为了获得优化的性能，通常最好是使用 arraysize 属性。 如果使用 size 形参，则最好在从一个 fetchmany() 调用到下一个调用之间保持相同的值。

fetchall()

获取一个查询结果的所有（剩余）行，返回一个列表。 请注意 cursor 的 arraysize 属性会影响此操作的执行效率。 当没有可用行时将返回一个空列表。

close()

立即关闭 cursor（而不是在当 __del__ 被调用的时候）。

从这一时刻起该 cursor 将不再可用，如果再尝试用该 cursor 执行任何操作将引发 ProgrammingError 异常。

rowcount

虽然 sqlite3 模块的 Cursor 类实现了此属性，但数据库引擎本身对于确定 “受影响行”/”已选择行” 的支持并不完善。

对于 executemany() 语句，修改行数会被汇总至 rowcount。

根据 Python DB API 规格描述的要求，rowcount 属性 “当未在 cursor 上执行 executeXX() 或者上一次操作的 rowcount 不是由接口确定时为 -1”。 这包括 SELECT 语句，因为我们无法确定一次查询将产生的行计数，而要等获取了所有行时才会知道。。

在 SQLite 的 3.6.5 版之前，如果你执行 DELETE FROM table 时不附带任何条件，则 rowcount 将被设为 0。

lastrowid

这个只读属性会提供最近修改行的 rowid。 它只在你使用 execute() 方法执行 INSERT 或 REPLACE 语句时会被设置。 对于 INSERT 或 REPLACE 以外的操作或者当 executemany() 被调用时，lastrowid 会被设为 None。

如果 INSERT 或 REPLACE 语句操作失败则将返回上一次成功操作的 rowid。

在 3.6 版更改: 增加了 REPLACE 语句的支持。

arraysize

用于控制 fetchmany() 返回行数的可读取/写入属性。 该属性的默认值为 1，表示每次调用将获取单独一行。

description

这个只读属性将提供上一次查询的列名称。 为了与 Python DB API 保持兼容，它会为每个列返回一个 7 元组，每个元组的最后六个条目均为 None。

对于没有任何匹配行的 SELECT 语句同样会设置该属性。

connection

这个只读属性将提供 Cursor 对象所使用的 SQLite 数据库 Connection。 通过调用 con.cursor() 创建的 Cursor 对象所包含的 connection 属性将指向 con:

>>> con = sqlite3.connect(":memory:")
>>> cur = con.cursor()
>>> cur.connection == con
True

12.6.4. 行对象*Row*

class sqlite3.Row

一个 Row 实例，该实例将作为用于 Connection 对象的高度优化的 row_factory。 它的大部分行为都会模仿元组的特性。

它支持使用列名称的映射访问以及索引、迭代、文本表示、相等检测和 len() 等操作。

如果两个 Row 对象具有完全相同的列并且其成员均相等，则它们的比较结果为相等。

keys()

此方法会在一次查询之后立即返回一个列名称的列表，它是 Cursor.description 中每个元组的第一个成员。

在 3.5 版更改: 添加了对切片操作的支持。

让我们假设我们如上面的例子所示初始化一个表:

conn = sqlite3.connect(":memory:")
c = conn.cursor()
c.execute('''create table stocks
(date text, trans text, symbol text,
 qty real, price real)''')
c.execute("""insert into stocks
          values ('2006-01-05','BUY','RHAT',100,35.14)""")
conn.commit()
c.close()

现在我们将 Row 插入:

>>> conn.row_factory = sqlite3.Row
>>> c = conn.cursor()
>>> c.execute('select * from stocks')
<sqlite3.Cursor object at 0x7f4e7dd8fa80>
>>> r = c.fetchone()
>>> type(r)
<class 'sqlite3.Row'>
>>> tuple(r)
('2006-01-05', 'BUY', 'RHAT', 100.0, 35.14)
>>> len(r)
5
>>> r[2]
'RHAT'
>>> r.keys()
['date', 'trans', 'symbol', 'qty', 'price']
>>> r['qty']
100.0
>>> for member in r:
...     print(member)
...
2006-01-05
BUY
RHAT
100.0
35.14

12.6.5. 异常

exception sqlite3.Warning

Exception 的一个子类。

exception sqlite3.Error

此模块中其他异常的基类。 它是 Exception 的一个子类。

exception sqlite3.DatabaseError

针对数据库相关错误引发的异常。

exception sqlite3.IntegrityError

当数据库的关系一致性受到影响时引发的异常。 例如外键检查失败等。 它是 DatabaseError 的子类。

exception sqlite3.ProgrammingError

编程错误引发的异常，例如表未找到或已存在，SQL 语句存在语法错误，指定的形参数量错误等。 它是 DatabaseError 的子类。

exception sqlite3.OperationalError

与数据库操作相关而不一定能受程序员掌控的错误引发的异常，例如发生非预期的连接中断，数据源名称未找到，事务无法被执行等。 它是 DatabaseError 的子类。

exception sqlite3.NotSupportedError

在使用了某个数据库不支持的方法或数据库 API 时引发的异常，例如在一个不支持事务或禁用了事务的连接上调用 rollback() 方法等。 它是 DatabaseError 的子类。

12.6.6. SQLite 与 Python 类型

12.6.6.1. 概述

SQLite 原生支持如下的类型： NULL，INTEGER，REAL，TEXT，BLOB。

因此可以将以下Python类型发送到SQLite而不会出现任何问题：

Python 类型

SQLite 类型

None

NULL

int

INTEGER

float

REAL

str

TEXT

bytes

BLOB

这是SQLite类型默认转换为Python类型的方式：

SQLite 类型

Python 类型

NULL

None

INTEGER

int

REAL

float

TEXT

取决于 text_factory , 默认为 str

BLOB

bytes

sqlite3 模块的类型系统可通过两种方式来扩展：你可以通过对象适配将额外的 Python 类型保存在 SQLite 数据库中，你也可以让 sqlite3 模块通过转换器将 SQLite 类型转换为不同的 Python 类型。

12.6.6.2. 使用适配器将额外的 Python 类型保存在 SQLite 数据库中。

如上文所述，SQLite 只包含对有限类型集的原生支持。 要让 SQLite 能使用其他 Python 类型，你必须将它们 适配 至 sqlite3 模块所支持的 SQLite 类型中的一种：NoneType, int, float, str, bytes。

有两种方式能让 sqlite3 模块将某个定制的 Python 类型适配为受支持的类型。

12.6.6.2.1. 让对象自行调整

如果自己编写类，这是一种很好的方法。假设有这样的类：：

class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y

现在你想将这种点对象保存在一个 SQLite 列中。 首先你必须选择一种受支持的类型用来表示点对象。 让我们就用 str 并使用一个分号来分隔坐标值。 然后你需要给你的类加一个方法 __conform__(self, protocol)，它必须返回转换后的值。 形参 protocol 将为 PrepareProtocol。

import sqlite3

class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y

    def __conform__(self, protocol):
        if protocol is sqlite3.PrepareProtocol:
            return "%f;%f" % (self.x, self.y)

con = sqlite3.connect(":memory:")
cur = con.cursor()

p = Point(4.0, -3.2)
cur.execute("select ?", (p,))
print(cur.fetchone()[0])

12.6.6.2.2. 注册可调用的适配器

另一种可能的做法是创建一个将该类型转换为字符串表示的函数并使用 register_adapter() 注册该函数。

import sqlite3

class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y

def adapt_point(point):
    return "%f;%f" % (point.x, point.y)

sqlite3.register_adapter(Point, adapt_point)

con = sqlite3.connect(":memory:")
cur = con.cursor()

p = Point(4.0, -3.2)
cur.execute("select ?", (p,))
print(cur.fetchone()[0])

sqlite3 模块有两个适配器可用于 Python 的内置 datetime.date 和 datetime.datetime 类型。 现在假设我们想要存储 datetime.datetime 对象，但不是表示为 ISO 格式，而是表示为 Unix 时间戳。

import sqlite3
import datetime
import time

def adapt_datetime(ts):
    return time.mktime(ts.timetuple())

sqlite3.register_adapter(datetime.datetime, adapt_datetime)

con = sqlite3.connect(":memory:")
cur = con.cursor()

now = datetime.datetime.now()
cur.execute("select ?", (now,))
print(cur.fetchone()[0])

12.6.6.3. 将SQLite 值转换为自定义Python 类型

编写适配器让你可以将定制的 Python 类型发送给 SQLite。 但要令它真正有用，我们需要实现从 Python 到 SQLite 再回到 Python 的双向转换。

输入转换器。

让我们回到 Point 类。 我们以字符串形式在 SQLite 中存储了 x 和 y 坐标值。

首先，我们将定义一个转换器函数，它接受这样的字符串作为形参并根据该参数构造一个 Point 对象。

注解

转换器函数在调用时 总是 会附带一个 bytes 对象，无论你将何种数据类型的值发给 SQLite。

def convert_point(s):
    x, y = map(float, s.split(b";"))
    return Point(x, y)

现在你需要让 sqlite3 模块知道你从数据库中选取的其实是一个点对象。 有两种方式都可以做到这件事：

隐式的声明类型

显式的通过列名

这两种方式会在 模块函数和常量 一节中描述，相应条目为 PARSE_DECLTYPES 和 PARSE_COLNAMES 常量。

下面的示例说明了这两种方法。

import sqlite3

class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y

    def __repr__(self):
        return "(%f;%f)" % (self.x, self.y)

def adapt_point(point):
    return ("%f;%f" % (point.x, point.y)).encode('ascii')

def convert_point(s):
    x, y = list(map(float, s.split(b";")))
    return Point(x, y)

# Register the adapter
sqlite3.register_adapter(Point, adapt_point)

# Register the converter
sqlite3.register_converter("point", convert_point)

p = Point(4.0, -3.2)

#########################
# 1) Using declared types
con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
cur = con.cursor()
cur.execute("create table test(p point)")

cur.execute("insert into test(p) values (?)", (p,))
cur.execute("select p from test")
print("with declared types:", cur.fetchone()[0])
cur.close()
con.close()

#######################
# 1) Using column names
con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_COLNAMES)
cur = con.cursor()
cur.execute("create table test(p)")

cur.execute("insert into test(p) values (?)", (p,))
cur.execute('select p as "p [point]" from test')
print("with column names:", cur.fetchone()[0])
cur.close()
con.close()

12.6.6.4. 默认适配器和转换器

对于 datetime 模块中的 date 和 datetime 类型已提供了默认的适配器。 它们将会以 ISO 日期/ISO 时间戳的形式发给 SQLite。

默认转换器使用的注册名称是针对 datetime.date 的 “date” 和针对 datetime.datetime 的 “timestamp”。

通过这种方式，你可以在大多数情况下使用 Python 的 date/timestamp 对象而无须任何额外处理。 适配器的格式还与实验性的 SQLite date/time 函数兼容。

下面的示例演示了这一点。

import sqlite3
import datetime

con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
cur = con.cursor()
cur.execute("create table test(d date, ts timestamp)")

today = datetime.date.today()
now = datetime.datetime.now()

cur.execute("insert into test(d, ts) values (?, ?)", (today, now))
cur.execute("select d, ts from test")
row = cur.fetchone()
print(today, "=>", row[0], type(row[0]))
print(now, "=>", row[1], type(row[1]))

cur.execute('select current_date as "d [date]", current_timestamp as "ts [timestamp]"')
row = cur.fetchone()
print("current_date", row[0], type(row[0]))
print("current_timestamp", row[1], type(row[1]))

如果存储在 SQLite 中的时间戳的小数位多于 6 个数字，则时间戳转换器会将该值截断至微秒精度。

12.6.7. 控制事务

底层的 sqlite3 库默认会以 autocommit 模式运行，但 Python 的 sqlite3 模块默认则不使用此模式。

autocommit 模式意味着修改数据库的操作会立即生效。 BEGIN 或 SAVEPOINT 语句会禁用 autocommit 模式，而用于结束外层事务的 COMMIT, ROLLBACK 或 RELEASE 则会恢复 autocommit 模式。

Python 的 sqlite3 模块默认会在数据修改语言 (DML) 类语句 (即 INSERT/UPDATE/DELETE/REPLACE) 之前隐式地执行一条 BEGIN 语句。

你可以控制 sqlite3 隐式执行的 BEGIN 语句的种类，具体做法是通过将 isolation_level 形参传给 connect() 调用，或者通过指定连接的 isolation_level 属性。 如果你没有指定 isolation_level，将使用基本的 BEGIN，它等价于指定 DEFERRED。 其他可能的值为 IMMEDIATE 和 EXCLUSIVE。

你可以禁用 sqlite3 模块的隐式事务管理，具体做法是将 isolation_level 设为 None。 这将使得下层的 sqlite3 库采用 autocommit 模式。 随后你可以通过在代码中显式地使用 BEGIN, ROLLBACK, SAVEPOINT 和 RELEASE 语句来完全控制事务状态。

在 3.6 版更改: 以前 sqlite3 会在 DDL 语句之前隐式地提交未完成事务。 现在则不会再这样做。

12.6.8. 有效使用 sqlite3

12.6.8.1. 使用快捷方式

使用 Connection 对象的非标准 execute(), executemany() 和 executescript() 方法，可以更简洁地编写代码，因为不必显式创建（通常是多余的） Cursor 对象。相反， Cursor 对象是隐式创建的，这些快捷方法返回游标对象。这样，只需对 Connection 对象调用一次，就能直接执行 SELECT 语句并遍历对象。

import sqlite3

persons = [
    ("Hugo", "Boss"),
    ("Calvin", "Klein")
    ]

con = sqlite3.connect(":memory:")

# Create the table
con.execute("create table person(firstname, lastname)")

# Fill the table
con.executemany("insert into person(firstname, lastname) values (?, ?)", persons)

# Print the table contents
for row in con.execute("select firstname, lastname from person"):
    print(row)

print("I just deleted", con.execute("delete from person").rowcount, "rows")

12.6.8.2. 通过名称而不是索引访问索引

sqlite3 模块的一个有用功能是内置的 sqlite3.Row 类，它被设计用作行对象的工厂。

该类的行装饰器可以用索引（如元组）和不区分大小写的名称访问：

import sqlite3

con = sqlite3.connect(":memory:")
con.row_factory = sqlite3.Row

cur = con.cursor()
cur.execute("select 'John' as name, 42 as age")
for row in cur:
    assert row[0] == row["name"]
    assert row["name"] == row["nAmE"]
    assert row[1] == row["age"]
    assert row[1] == row["AgE"]

12.6.8.3. 使用连接作为上下文管理器

连接对象可以用来作为上下文管理器，它可以自动提交或者回滚事务。如果出现异常，事务会被回滚；否则，事务会被提交。

import sqlite3

con = sqlite3.connect(":memory:")
con.execute("create table person (id integer primary key, firstname varchar unique)")

# Successful, con.commit() is called automatically afterwards
with con:
    con.execute("insert into person(firstname) values (?)", ("Joe",))

# con.rollback() is called after the with block finishes with an exception, the
# exception is still raised and must be caught
try:
    with con:
        con.execute("insert into person(firstname) values (?)", ("Joe",))
except sqlite3.IntegrityError:
    print("couldn't add Joe twice")

12.6.9. 常见问题

12.6.9.1. 多线程

较老版本的 SQLite 在共享线程之间存在连接问题。这就是Python模块不允许线程之间共享连接和游标的原因。如果仍然尝试这样做，则在运行时会出现异常。

唯一的例外是调用 interrupt() 方法，该方法仅在从其他线程进行调用时才有意义。

备注

1(1,2)

sqlite3 模块默认没有构建可加载扩展支持，因为有一些平台带有不支持这个特性的 SQLite 库（特别是 Mac OS X）。要获得可加载扩展的支持，那么在编译配置的时候必须指定 –enable-loadable-sqlite-extensions 选项。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

12. 数据持久化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

13. 数据压缩和存档

本章中描述的模块支持 zlib、gzip、bzip2 和 lzma 数据压缩算法，以及创建 ZIP 和 tar 格式的归档文件。参见由 shutil 模块提供的 归档操作 。

13.1. zlib — 与 gzip 兼容的压缩

13.2. gzip — 对 gzip 格式的支持

13.2.1. 用法示例

13.3. bz2 — 对 bzip2 压缩算法的支持

13.3.1. 文件压缩和解压

13.3.2. 增量压缩和解压

13.3.3. 一次性压缩或解压

13.4. lzma — 用 LZMA 算法压缩

13.4.1. 读写压缩文件

13.4.2. 在内存中压缩和解压缩数据

13.4.3. 杂项

13.4.4. 指定自定义的过滤器链

13.4.5. 例子

13.5. zipfile — 使用ZIP存档

13.5.1. ZipFile 对象

13.5.2. PyZipFile 对象

13.5.3. ZipInfo 对象

13.5.4. 命令行界面

13.5.4.1. 命令行选项

13.6. tarfile — 读写tar归档文件

13.6.1. TarFile 对象

13.6.2. TarInfo 对象

13.6.3. 命令行界面

13.6.3.1. 命令行选项

13.6.4. 例子

13.6.5. 受支持的 tar 格式

13.6.6. Unicode 问题

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

13. 数据压缩和存档 »

|

13.1. zlib — 与 gzip 兼容的压缩

此模块为需要数据压缩的程序提供了一系列函数，用于压缩和解压缩。这些函数使用了 zlib 库。zlib 库的项目主页是 http://www.zlib.net. 版本低于 1.1.3 的 zlib 与此 Python 模块之间存在已知的不兼容。1.1.3 版本的 zlib 存在一个安全漏洞，我们推荐使用 1.1.4 或更新的版本。

zlib 的函数有很多选项，一般需要按特定顺序使用。本文档没有覆盖全部的用法。更多详细信息请于 http://www.zlib.net/manual.html 参阅官方手册。

要读写 .gz 格式的文件，请参考 gzip 模块。

此模块中可用的异常和函数如下：

exception zlib.error

在压缩或解压缩过程中发生错误时的异常。

zlib.adler32(data[, value])

计算 data 的 Adler-32 校验值。(Adler-32 校验的可靠性与 CRC32 基本相当，但比计算 CRC32 更高效。) 计算的结果是一个 32 位的整数。参数 value 是校验时的起始值，其默认值为 1。借助参数 value 可为分段的输入计算校验值。此算法没有加密强度，不应用于身份验证和数字签名。此算法的目的仅为验证数据的正确性，不适合作为通用散列算法。

在 3.0 版更改: 返回值永远是无符号数。要在所有的 Python 版本和平台上获得相同的值，请使用 adler32(data) & 0xffffffff。

zlib.compress(data, level=-1)

压缩 data 中的字节，返回含有已压缩内容的 bytes 对象。参数 level 为整数，可取值为 0 到 9 或 -1，用于指定压缩等级。1 (Z_BEST_SPEED) 表示最快速度和最低压缩率，9 (Z_BEST_COMPRESSION) 表示最慢速度和最高压缩率。0 (Z_NO_COMPRESSION) 表示不压缩。参数默认值为 -1 (Z_DEFAULT_COMPRESSION)。Z_DEFAULT_COMPRESSION 是速度和压缩率之间的平衡 (一般相当于设压缩等级为 6)。函数发生错误时抛出 error 异常。

在 3.6 版更改: 现在，level 可作为关键字参数。

zlib.compressobj(level=-1, method=DEFLATED, wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=Z_DEFAULT_STRATEGY[, zdict])

返回一个 压缩对象，用来压缩内存中难以容下的数据流。

参数 level 为压缩等级，是整数，可取值为 0 到 9 或 -1。1 (Z_BEST_SPEED) 表示最快速度和最低压缩率，9 (Z_BEST_COMPRESSION) 表示最慢速度和最高压缩率。0 (Z_NO_COMPRESSION) 表示不压缩。参数默认值为 -1 (Z_DEFAULT_COMPRESSION)。Z_DEFAULT_COMPRESSION 是速度和压缩率之间的平衡 (一般相当于设压缩等级为 6)。

method 表示压缩算法。现在只支持 DEFLATED 这个算法。

参数 wbits 指定压缩数据时所使用的历史缓冲区的大小 (窗口大小)，并指定压缩输出是否包含头部或尾部。参数的默认值是 15 (MAX_WBITS)。参数的值分为几个范围：

+9 到 +15：窗口大小以 2 为底的对数。即这些值对应着 512 到 32768 的窗口大小。更大的值会提供更好的压缩，同时内存开销也会更大。压缩输出会包含 zlib 特定格式的头部和尾部。

−9 到 −15：绝对值为窗口大小以 2 为底的对数。压缩输出仅包含压缩数据，没有头部和尾部。

+25 到 +31 = 16 + (9 到 15)：后 4 个比特位为窗口大小以 2 为底的对数。压缩输出包含一个基本的 gzip 头部，并以校验和为尾部。

参数 memLevel 指定内部压缩操作时所占用内存大小。参数取 1 到 9。更大的值占用更多的内存，同时速度也更快输出也更小。

参数 strategy 用于调节压缩算法。可取值为 Z_DEFAULT_STRATEGY、Z_FILTERED、Z_HUFFMAN_ONLY、Z_RLE (zlib 1.2.0.1) 或 Z_FIXED (zlib 1.2.2.2)。

参数 zdict 指定预定义的压缩字典。它是一个字节序列 (如 bytes 对象)，其中包含用户认为要压缩的数据中可能频繁出现的子序列。频率高的子序列应当放在字典的尾部。

在 3.3 版更改: 添加关键字参数 zdict。

zlib.crc32(data[, value])

计算 data 的 CRC (循环冗余校验) 值。计算的结果是一个 32 位的整数。参数 value 是校验时的起始值，其默认值为 0。借助参数 value 可为分段的输入计算校验值。此算法没有加密强度，不应用于身份验证和数字签名。此算法的目的仅为验证数据的正确性，不适合作为通用散列算法。

在 3.0 版更改: 返回值永远是无符号数。要在所有的 Python 版本和平台上获得相同的值，请使用 crc32(data) & 0xffffffff。

zlib.decompress(data, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE)

解压 data 中的字节，返回含有已解压内容的 bytes 对象。参数 wbits 取决于 data 的格式，具体参见下边的说明。bufsize 为输出缓冲区的起始大小。函数发生错误时抛出 error 异常。

wbits 形参控制历史缓冲区（或称“窗口尺寸”）的大小以及所期望的头部和尾部格式。 它类似于 compressobj() 的形参，但可接受更大范围的值：

+8 至 +15：窗口尺寸以二为底的对数。 输入必须包含 zlib 头部和尾部。

0：根据 zlib 头部自动确定窗口大小。 只从 zlib 1.2.3.5 版起受支持。

−8 to −15：使用 wbits 的绝对值作为窗口大小以二为底的对数。 输入必须为原始数据流，没有头部和尾部。

+24 至 +31 = 16 + (8 至 15)：使用后 4 个比特位作为窗口大小以二为底的对数。 输入必须包括 gzip 头部和尾部。

+40 至 +47 = 32 + (8 至 15)：使用后 4 个比特位作为窗口大小以二为底的对数，并且自动接受 zlib 或 gzip 格式。

当解压缩一个数据流时，窗口大小必须不小于用于压缩数据流的原始窗口大小；使用太小的值可能导致 error 异常。 默认 wbits 值对应于最大的窗口大小并且要求包括 zlib 头部和尾部。

bufsize 是用于存放解压数据的缓冲区初始大小。 如果需要更大空间，缓冲区大小将按需增加，因此你不需要让这个值完全精确；对其进行调整仅会节省一点对 malloc() 的调用次数。

在 3.6 版更改: wbits 和 bufsize 可用作关键字参数。

zlib.decompressobj(wbits=MAX_WBITS[, zdict])

返回一个解压对象，用来解压无法被一次性放入内存的数据流。

wbits 形参控制历史缓冲区的大小（或称“窗口大小”）以及所期望的头部和尾部格式。 它的含义与 对 decompress() 的描述 相同。

zdict 形参指定指定一个预定义的压缩字典。 如果提供了此形参，它必须与产生将解压数据的压缩器所使用的字典相同。

注解

如果 zdict 是一个可变对象 (例如 bytearray)，则你不可在对 decompressobj() 的调用和对解压器的 decompress() 方法的调用之间修改其内容。

在 3.3 版更改: 增加了 zdict 形参。

压缩对象支持以下方法：

Compress.compress(data)

压缩 data 并返回 bytes 对象，这个对象含有 data 的部分或全部内容的已压缩数据。所得的对象必须拼接在上一次调用 compress() 方法所得数据的后面。缓冲区中可能留存部分输入以供下一次调用。

Compress.flush([mode])

压缩所有缓冲区的数据并返回已压缩的数据。参数 mode 可以传入的常量为：Z_NO_FLUSH、Z_PARTIAL_FLUSH、Z_SYNC_FLUSH、Z_FULL_FLUSH、Z_BLOCK (zlib 1.2.3.4) 或 Z_FINISH。默认值为 Z_FINISH。Z_FINISH 关闭已压缩数据流并不允许再压缩其他数据，Z_FINISH 以外的值皆允许这个对象继续压缩数据。调用 flush() 方法并将 mode 设为 Z_FINISH 后会无法再次调用 compress()，此时只能删除这个对象。

Compress.copy()

返回此压缩对象的一个拷贝。它可以用来高效压缩一系列拥有相同前缀的数据。

解压缩对象支持以下方法：

Decompress.unused_data

一个 bytes 对象，其中包含压缩数据结束之后的任何字节数据。 也就是说，它将为 b"" 直到包含压缩数据的末尾字节可用。 如果整个结果字节串都包含压缩数据，它将为一个空的 bytes 对象 b""。

Decompress.unconsumed_tail

一个 bytes 对象，其中包含未被上一次 decompress() 调用所消耗的任何数据。 此数据不能被 zlib 机制看到，因此你必须将其送回（可能要附带额外的数据拼接）到后续的 decompress() 方法调用以获得正确的输出。

Decompress.eof

一个布尔值，指明是否已到达压缩数据流的末尾。

这使得区分正确构造的压缩数据流和不完整或被截断的压缩数据流成为可能。

3.3 新版功能.

Decompress.decompress(data, max_length=0)

解压缩 data 并返回 bytes 对象，其中包含对应于 string 中至少一部分数据的解压缩数据。 此数据应当被拼接到之前任何对 decompress() 方法的调用所产生的输出。 部分输入数据可能会被保留在内部缓冲区以供后续处理。

如果可选的形参 max_length 非零则返回值将不会长于 max_length。 这可能意味着不是所有已压缩输入都能被处理；并且未被消耗的数据将被保存在 unconsumed_tail 属性中。 如果要继续解压缩则这个字节串必须被传给对 decompress() 的后续调用。 如果 max_length 为零则整个输入都会被解压缩，并且 unconsumed_tail 将为空。

在 3.6 版更改: max_length 可用作关键字参数。

Decompress.flush([length])

所有挂起的输入会被处理，并且返回包含剩余未压缩输出的 bytes 对象。 在调用 flush() 之后，decompress() 方法将无法被再次调用；唯一可行的操作是删除该对象。

可选的形参 length 设置输出缓冲区的初始大小。

Decompress.copy()

返回解压缩对象的一个拷贝。 它可以用来在数据流的中途保存解压缩器的状态以便加快随机查找数据流后续位置的速度。

通过下列常量可获取模块所使用的 zlib 库的版本信息：

zlib.ZLIB_VERSION

构建此模块时所用的 zlib 库的版本字符串。它的值可能与运行时所加载的 zlib 不同。运行时加载的 zlib 库的版本字符串为 ZLIB_RUNTIME_VERSION。

zlib.ZLIB_RUNTIME_VERSION

解释器所加载的 zlib 库的版本字符串。

3.3 新版功能.

参见

模块 gzip

读写 gzip 格式的文件。

http://www.zlib.net

zlib 库项目主页。

http://www.zlib.net/manual.html

zlib 库用户手册。提供了库的许多功能的解释和用法。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

13. 数据压缩和存档 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

13. 数据压缩和存档 »

|

13.2. gzip — 对 gzip 格式的支持

源代码： Lib/gzip.py [https://github.com/python/cpython/tree/3.6/Lib/gzip.py]

此模块提供的简单接口帮助用户压缩和解压缩文件，功能类似于 GNU 应用程序 gzip 和 gunzip。

数据压缩由 zlib 模块提供。

gzip 模块提供 GzipFile 类和 open()、compress()、decompress() 几个便利的函数。GzipFile 类可以读写 gzip 格式的文件，还能自动压缩和解压缩数据，这让操作压缩文件如同操作普通的 file object 一样方便。

注意，此模块不支持部分可以被 gzip 和 gunzip 解压的格式，如利用 compress 或 pack 压缩所得的文件。

这个模块定义了以下内容：

gzip.open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)

以二进制方式或者文本方式打开一个 gzip 格式的压缩文件，返回一个 file object。

filename 参数可以是一个实际的文件名(一个a str 对象或者 bytes 对象), 或者是一个用来读写的已存在的文件对象。

mode 参数可以是二进制模式： 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x' or 'xb' , 或者是文本模式 'rt', 'at', 'wt', or 'xt'。默认值是 'rb'。

The compresslevel argument is an integer from 0 to 9, as for the GzipFile constructor.

对于二进制模式，这个函数等价于 GzipFile 构造器：GzipFile(filename, mode, compresslevel)。在这个例子中，encoding, errors 和 newline 三个参数一定不要设置。

对于文本模式，将会创建一个 GzipFile 对象，并将它封装到一个 io.TextIOWrapper 实例中， 这个实例默认了指定编码，错误抓获行为和行。

在 3.3 版更改: 支持 filename 为一个文件对象，支持文本模式和 encoding, errors 和 newline 参数。

在 3.4 版更改: 支持 'x', 'xb' 和``’xt’`` 三种模式。

在 3.6 版更改: 接受一个 path-like object。

class gzip.GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)

GzipFile 类的构造器支持 truncate() 的异常，与 file object 的大多数方法非常相似。fileobj 和 filename 至少有一个不为空。

新的实例基于 fileobj，它可以是一个普通文件，一个 io.BytesIO 对象，或者任何一个与文件相似的对象。当 filename 是一个文件对象时，它的默认值是 None。

当 fileobj 为 None 时， filename 参数只用于 gzip 文件头中，这个文件有可能包含未压缩文件的源文件名。如果文件可以被识别，默认 fileobj 的文件名；否则默认为空字符串，在这种情况下文件头将不包含源文件名。

The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x', or 'xb', depending on whether the file will be read or written. The default is the mode of fileobj if discernible; otherwise, the default is 'rb'.

需要注意的是，文件默认使用二进制模式打开。如果要以文本模式打开文件一个压缩文件，请使用 open() 方法(或者使用 io.TextIOWrapper 包装 GzipFile )。

compresslevel 参数是一个从 0 to 9 的整数，用于控制压缩等级；1 最快但压缩比例最小，9 最慢但压缩比例最大。 0 不压缩。默认为 9。

The mtime argument is an optional numeric timestamp to be written to the last modification time field in the stream when compressing. It should only be provided in compression mode. If omitted or None, the current time is used. See the mtime attribute for more details.

调用 GzipFile 的 close() 方法不会关闭 fileobj，因为你可以希望增加其它内容到已经压缩的数中。你可以将一个 io.BytesIO 对象作为 fileobj，也可以使用 io.BytesIO 的 getvalue() 方法从内存缓存中恢复数据。

GzipFile 支持 io.BufferedIOBase 类的接口, 包括迭代和 with 语句。只有 truncate() 方法没有实现。

GzipFile 还提供了以下的方法和属性:

peek(n)

在不移动文件指针的情况下读取 n 个未压缩字节。最多只有一个单独的读取流来服务这个方法调用。返回的字节数不一定刚好等于要求的数量。

注解

调用 peek() 并没有改变 GzipFile 的文件指针，它可能改变潜在文件对象(例如： GzipFile 使用 fileobj 参数进行初始化)。

3.2 新版功能.

mtime

在解压的过程中，最后修改时间字段的值可能来自于这个属性，以整数的形式出现。在读取任何文件头信息前，初始值为 None。

所有 gzip 东方压缩流中必须包含时间戳这个字段。以便于像 gunzip这样的程序可以使用时间戳。格式与 time.time() 的返回值和 os.stat() 对象的 st_mtime 属性值一样。

在 3.1 版更改: 支持 with 语句，构造器参数 mtime 和 mtime 属性。

在 3.2 版更改: 添加了对零填充和不可搜索文件的支持。

在 3.3 版更改: 实现 io.BufferedIOBase.read1() 方法。

在 3.4 版更改: 支持 'x' and 'xb' 两种模式。

在 3.5 版更改: 支持写入任意 bytes-like objects。read() 方法可以接受``None``为参数。

在 3.6 版更改: 接受一个 path-like object。

gzip.compress(data, compresslevel=9)

压缩 data 返回一个包含压缩数据的 bytes 对象。 compresslevel 的意义与上面提到的 GzipFile 构造器一至。

3.2 新版功能.

gzip.decompress(data)

解压数据，返回一个 bytes 包含未解压数据的对象。

3.2 新版功能.

13.2.1. 用法示例

读取压缩文件示例：

import gzip
with gzip.open('/home/joe/file.txt.gz', 'rb') as f:
    file_content = f.read()

创建GZIP 文件示例：

import gzip
content = b"Lots of content here"
with gzip.open('/home/joe/file.txt.gz', 'wb') as f:
    f.write(content)

使用 GZIP 压缩已有的文件示例：

import gzip
import shutil
with open('/home/joe/file.txt', 'rb') as f_in:
    with gzip.open('/home/joe/file.txt.gz', 'wb') as f_out:
        shutil.copyfileobj(f_in, f_out)

使用 GZIP 压缩二进制字符串示例：

import gzip
s_in = b"Lots of content here"
s_out = gzip.compress(s_in)

参见

模块 zlib

支持 gzip 格式所需要的基本压缩模块。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

13. 数据压缩和存档 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

13. 数据压缩和存档 »

|

13.3. bz2 — 对 bzip2 压缩算法的支持

源代码： Lib/bz2.py [https://github.com/python/cpython/tree/3.6/Lib/bz2.py]

此模块提供了使用 bzip2 压缩算法压缩和解压数据的一套完整的接口。

bz2 模块包含：

用于读写压缩文件的 open() 函数和 BZ2File 类。

用于增量压缩和解压的 BZ2Compressor 和 BZ2Decompressor 类。

用于一次性压缩和解压的 compress() 和 decompress() 函数。

此模块中的所有类都能安全地从多个线程访问。

13.3.1. 文件压缩和解压

bz2.open(filename, mode='r', compresslevel=9, encoding=None, errors=None, newline=None)

以二进制或文本模式打开 bzip2 压缩文件，返回一个 file object。

和 BZ2File 的构造函数类似，filename 参数可以是一个实际的文件名（str 或 bytes 对象），或是已有的可供读取或写入的文件对象。

mode 参数可设为二进制模式的 'r'、'rb'、'w'、'wb'、'x'、'xb'、'a' 或 'ab'，或者文本模式的 'rt'、'wt'、'xt' 或 'at'。默认是 'rb'。

compresslevel 参数是 1 到 9 的整数，和 BZ2File 的构造函数一样。

对于二进制模式，这个函数等价于 BZ2File 构造器: BZ2File(filename, mode, compresslevel=compresslevel)。 在这种情况下，不可提供 encoding, errors 和 newline 参数。

对于文本模式，将会创建一个 BZ2File 对象，并将它包装到一个 io.TextIOWrapper 实例中，此实例带有指定的编码格式、错误处理行为和行结束符。

3.3 新版功能.

在 3.4 版更改: 添加了 'x' (仅创建) 模式。

在 3.6 版更改: 接受一个 path-like object。

class bz2.BZ2File(filename, mode='r', buffering=None, compresslevel=9)

用二进制模式打开 bzip2 压缩文件。

如果 filename 是一个 str 或 bytes 对象，则打开名称对应的文件目录。 否则的话，filename 应当是一个 file object，它将被用来读取或写入压缩数据。

mode 参数可以是表示读取的 'r' (默认值)，表示覆写的 'w'，表示单独创建的 'x'，或表示添加的 'a'。 这些模式还可别以 'rb', 'wb', 'xb' 和 'ab' 的等价形式给出。

如果 filename 是一个文件对象（而不是实际的文件名），则 'w' 模式并不会截断文件，而是会等价于 'a'。

buffering 参数会被忽略。 它已经被弃用。

If mode is 'w' or 'a', compresslevel can be a number between 1 and 9 specifying the level of compression: 1 produces the least compression, and 9 (default) produces the most compression.

如果 mode 为 'r'，则输入文件可以为多个压缩流的拼接。

BZ2File 提供了 io.BufferedIOBase 所指定的所有成员，但 detach() 和 truncate() 除外。 并支持迭代和 with 语句。

BZ2File 还提供了以下方法：

peek([n])

返回缓冲的数据而不前移文件位置。 至少将返回一个字节的数据（除非为 EOF）。 实际返回的字节数不确定。

注解

虽然调用 peek() 不会改变 BZ2File 的文件位置，但它可能改变下层文件对象的位置（举例来说如果 BZ2File 是通过传为一个文件对象作为 filename 的话）。

3.3 新版功能.

在 3.1 版更改: 支持了 with 语句。

在 3.3 版更改: 添加了 fileno(), readable(), seekable(), writable(), read1() 和 readinto() 方法。

在 3.3 版更改: 添加了对 filename 使用 file object 而非实际文件名的支持。

在 3.3 版更改: 添加了 'a' (append) 模式，以及对读取多数据流文件的支持。

在 3.4 版更改: 添加了 'x' (仅创建) 模式。

在 3.5 版更改: read() 方法现在接受 None 作为参数。

在 3.6 版更改: 接受一个 path-like object。

13.3.2. 增量压缩和解压

class bz2.BZ2Compressor(compresslevel=9)

创建一个新的压缩器对象。 此对象可被用来执行增量数据压缩。 对于一次性压缩，请改用 compress() 函数。

compresslevel, if given, must be a number between 1 and 9. The default is 9.

compress(data)

向压缩器对象提供数据。 在可能的情况下返回一段已压缩数据，否则返回空字节串。

当你已结束向压缩器提供数据时，请调用 flush() 方法来完成压缩进程。

flush()

结束压缩进程，返回内部缓冲中剩余的压缩完成的数据。

调用此方法之后压缩器对象将不可再被使用。

class bz2.BZ2Decompressor

创建一个新的解压缩器对象。 此对象可被用来执行增量数据解压缩。 对于一次性解压缩，请改用 decompress() 函数。

注解

这个类不会透明地处理包含多个已压缩数据流的输入，这不同于 decompress() 和 BZ2File。 如果你需要通过 BZ2Decompressor 来解压缩多个数据流输入，你必须为每个数据流都使用新的解压缩器。

decompress(data, max_length=-1)

解压缩 data (一个 bytes-like object)，返回字节串形式的解压缩数据。 某些 data 可以在内部被缓冲，以便用于后续的 decompress() 调用。 返回的数据应当与之前任何 decompress() 调用的输出进行拼接。

如果 max_length 为非负数，将返回至多 max_length 个字节的解压缩数据。 如果达到此限制并且可以产生后续输出，则 needs_input 属性将被设为 False。 在这种情况下，下一次 decompress() 调用提供的 data 可以为 b'' 以获取更多的输出。

如果所有输入数据都已被解压缩并返回（或是因为它少于 max_length 个字节，或是因为 max_length 为负数），则 needs_input 属性将被设为 True。

在到达数据流末尾之后再尝试解压缩数据会引发 EOFError。 在数据流末尾之后获取的任何数据都会被忽略并存储至 unused_data 属性。

在 3.5 版更改: 添加了 max_length 形参。

eof

若达到了数据流末尾标识符则为 True。

3.3 新版功能.

unused_data

压缩数据流的末尾还有数据。

如果在达到数据流末尾之前访问此属性，其值将为 b''。

needs_input

如果在要求新的未解压缩输入之前 decompress() 方法可以提供更多的解压缩数据则为 False。

3.5 新版功能.

13.3.3. 一次性压缩或解压

bz2.compress(data, compresslevel=9)

Compress data.

compresslevel, if given, must be a number between 1 and 9. The default is 9.

对于增量压缩，请改用 BZ2Compressor。

bz2.decompress(data)

Decompress data.

如果 data 是多个压缩数据流的拼接，则解压缩所有数据流。

对于增量解压缩，请改用 BZ2Decompressor。

在 3.3 版更改: 支持了多数据流的输入。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

13. 数据压缩和存档 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

13. 数据压缩和存档 »

|

13.4. lzma — 用 LZMA 算法压缩

3.3 新版功能.

源代码： Lib/lzma.py [https://github.com/python/cpython/tree/3.6/Lib/lzma.py]

此模块提供了可以压缩和解压缩使用 LZMA 压缩算法的数据的类和便携函数。 其中还包含支持 xz 工具所使用的 .xz 和旧式 .lzma 文件格式的文件接口，以及相应的原始压缩数据流。

此模块所提供的接口与 bz2 模块的非常类似。 但是，请注意 LZMAFile 不是 线程安全的，这与 bz2.BZ2File 不同，因此如果你需要在多个线程中使用单个 LZMAFile 实例，则需要通过锁来保护它。

exception lzma.LZMAError

当在压缩或解压缩期间或是在初始化压缩器/解压缩器的状态期间发生错误时此异常会被引发。

13.4.1. 读写压缩文件

lzma.open(filename, mode="rb", *, format=None, check=-1, preset=None, filters=None, encoding=None, errors=None, newline=None)

以二进制或文本模式打开 LZMA 压缩文件，返回一个 file object。

filename 参数可以是一个实际的文件名（以 str, bytes 或 路径类 对象的形式给出），在此情况下会打开指定名称的文件，或者可以是一个用于读写的现有文件对象。

mode 参数可以是二进制模式的 "r", "rb", "w", "wb", "x", "xb", "a" 或 "ab"，或者文本模式的 "rt", "wt", "xt" 或 "at"。 默认值为 "rb"。

当打开一个文件用于读取时，format 和 filters 参数具有与 LZMADecompressor 的参数相同的含义。 在此情况下，check 和 preset 参数不应被使用。

当打开一个文件用于写入的，format, check, preset 和 filters 参数具有与 LZMACompressor 的参数相同的含义。

对于二进制模式，这个函数等价于 LZMAFile 构造器: LZMAFile(filename, mode, ...)。 在这种情况下，不可提供 encoding, errors 和 newline 参数。

对于文本模式，将会创建一个 LZMAFile 对象，并将它包装到一个 io.TextIOWrapper 实例中，此实例带有指定的编码格式、错误处理行为和行结束符。

在 3.4 版更改: 增加了对 "x", "xb" 和 "xt" 模式的支持。

在 3.6 版更改: 接受一个 path-like object。

class lzma.LZMAFile(filename=None, mode="r", *, format=None, check=-1, preset=None, filters=None)

以二进制模式打开一个 LZMA 压缩文件。

LZMAFile 可以包装在一个已打开的 file object 中，或者是在给定名称的文件上直接操作。 filename 参数指定所包装的文件对象，或是要打开的文件名称（类型为 str, bytes 或 路径类 对象）。 如果是包装现有的文件对象，被包装的文件在 LZMAFile 被关闭时将不会被关闭。

mode 参数可以是表示读取的 "r" (默认值)，表示覆写的 "w"，表示单独创建的 "x"，或表示添加的 "a"。 这些模式还可以分别以 "rb", "wb", "xb" 和 "ab" 的等价形式给出。

如果 filename 是一个文件对象（而不是实际的文件名），则 "w" 模式并不会截断文件，而会等价于 "a"。

当打开一个文件用于读取时，输入文件可以为多个独立压缩流的拼接。 它们会被作为单个逻辑流被透明地解码。

当打开一个文件用于读取时，format 和 filters 参数具有与 LZMADecompressor 的参数相同的含义。 在此情况下，check 和 preset 参数不应被使用。

当打开一个文件用于写入的，format, check, preset 和 filters 参数具有与 LZMACompressor 的参数相同的含义。

LZMAFile 支持 io.BufferedIOBase 所指定的所有成员，但 detach() 和 truncate() 除外。 并支持迭代和 with 语句。

也提供以下方法：

peek(size=-1)

返回缓冲的数据而不前移文件位置。 至少将返回一个字节的数据，除非已经到达 EOF。 实际返回的字节数不确定（会忽略 size 参数）。

注解

虽然调用 peek() 不会改变 LZMAFile 的文件位置，但它可能改变下层文件对象的位置（举例来说如果 LZMAFile 是通过传入一个文件对象作为 filename 的话）。

在 3.4 版更改: 增加了对 "x" 和 "xb" 模式的支持。

在 3.5 版更改: read() 方法现在接受 None 作为参数。

在 3.6 版更改: 接受一个 path-like object。

13.4.2. 在内存中压缩和解压缩数据

class lzma.LZMACompressor(format=FORMAT_XZ, check=-1, preset=None, filters=None)

创建一个压缩器对象，此对象可被用来执行增量压缩。

压缩单个数据块的更便捷方式请参阅 compress()。

format 参数指定应当使用哪种压缩格式。 可能的值有：

FORMAT_XZ: .xz 容器格式。

这是默认格式。

FORMAT_ALONE: 传统的 .lzma 容器格式。

这种格式相比 .xz 更为受限 – 它不支持一致性检查或多重过滤器。

FORMAT_RAW: 原始数据流，不使用任何容器格式。

这个格式描述器不支持一致性检查，并且要求你必须指定一个自定义的过滤器链（用于压缩和解压缩）。 此外，以这种方式压缩的数据不可使用 FORMAT_AUTO 来解压缩 (参见 LZMADecompressor)。

check 参数指定要包含在压缩数据中的一致性检查类型。 这种检查在解压缩时使用，以确保数据没有被破坏。 可能的值是：

CHECK_NONE: 没有一致性检查。 这是 FORMAT_ALONE 和 FORMAT_RAW 的默认值（也是唯一可接受的值）。

CHECK_CRC32: 32 位循环冗余检查。

CHECK_CRC64: 64 位循环冗余检查。 这是 FORMAT_XZ 的默认值。

CHECK_SHA256: 256 位安全哈希算法。

如果指定的检查不受支持，则会引发 LZMAError。

压缩设置可被指定为一个预设的压缩等级（通过 preset 参数）或以自定义过滤器链来详细设置（通过 filters 参数）。

preset 参数（如果提供）应当为一个 0 到 9 (包括边界) 之间的整数，可以选择与常数 PRESET_EXTREME 进行 OR 运算。 如果 preset 和 filters 均未给出，则默认行为是使用 PRESET_DEFAULT (预设等级 6)。 更高的预设等级会产生更小的输出，但会使得压缩过程更缓慢。

注解

除了更加 CPU 密集，使用更高的预设等级来压缩还需要更多的内存（并产生需要更多内存来解压缩的输出）。 例如使用预设等级 9 时，一个 LZMACompressor 对象的开销可以高达 800 MiB。 出于这样的原因，通常最好是保持使用默认预设等级。

filters 参数（如果提供）应当指定一个过滤器链。 详情参见 指定自定义的过滤器链。

compress(data)

压缩 data (一个 bytes object)，返回包含针对输入的至少一部分已压缩数据的 bytes 对象。 一部 data 可能会被放入内部缓冲区，以便用于后续的 compress() 和 flush() 调用。 返回的数据应当与之前任何 compress() 调用的输出进行拼接。

flush()

结束压缩进程，返回包含保存在压缩器的内部缓冲区中的任意数据的 bytes 对象。

调用此方法之后压缩器将不可再被使用。

class lzma.LZMADecompressor(format=FORMAT_AUTO, memlimit=None, filters=None)

创建一个压缩器对象，此对象可被用来执行增量解压缩。

一次性解压缩整个压缩数据流的更便捷方式请参阅 decompress()。

format 参数指定应当被使用的容器格式。 默认值为 FORMAT_AUTO，它可以解压缩 .xz 和 .lzma 文件。 其他可能的值为 FORMAT_XZ, FORMAT_ALONE 和 FORMAT_RAW。

memlimit 参数指定解压缩器可以使用的内存上限（字节数）。 当使用此参数时，如果不可能在给定内存上限之内解压缩输入数据则解压缩将失败并引发 LZMAError。

filters 参数指定用于创建被解压缩数据流的过滤器链。 此参数在 format 为 FORMAT_RAW 时要求提供，但对于其他格式不应使用。 有关过滤器链的更多信息请参阅 指定自定义的过滤器链。

注解

这个类不会透明地处理包含多个已压缩数据流的输入，这不同于 decompress() 和 LZMAFile。 要通过 LZMADecompressor 来解压缩多个数据流输入，你必须为每个数据流都创建一个新的解压缩器。

decompress(data, max_length=-1)

解压缩 data (一个 bytes-like object)，返回字节串形式的解压缩数据。 某些 data 可以在内部被缓冲，以便用于后续的 decompress() 调用。 返回的数据应当与之前任何 decompress() 调用的输出进行拼接。

如果 max_length 为非负数，将返回至多 max_length 个字节的解压缩数据。 如果达到此限制并且可以产生后续输出，则 needs_input 属性将被设为 False。 在这种情况下，下一次 decompress() 调用提供的 data 可以为 b'' 以获取更多的输出。

如果所有输入数据都已被解压缩并返回（或是因为它少于 max_length 个字节，或是因为 max_length 为负数），则 needs_input 属性将被设为 True。

在到达数据流末尾之后再尝试解压缩数据会引发 EOFError。 在数据流末尾之后获取的任何数据都会被忽略并存储至 unused_data 属性。

在 3.5 版更改: 添加了 max_length 形参。

check

输入流使用的一致性检查的 ID。 这可能为 CHECK_UNKNOWN 直到已解压了足够的输入数据来确定它所使用的一致性检查。

eof

若达到了数据流末尾标识符则为 True。

unused_data

压缩数据流的末尾还有数据。

在达到数据流末尾之前，这个值将为 b""。

needs_input

如果在要求新的未解压缩输入之前 decompress() 方法可以提供更多的解压缩数据则为 False。

3.5 新版功能.

lzma.compress(data, format=FORMAT_XZ, check=-1, preset=None, filters=None)

压缩 data (一个 bytes 对象)，返回包含压缩数据的 bytes 对象。

参见上文的 LZMACompressor 了解有关 format, check, preset 和 filters 参数的说明。

lzma.decompress(data, format=FORMAT_AUTO, memlimit=None, filters=None)

解压缩 data (一个 bytes 对象)，返回包含解压缩数据的 bytes 对象。

如果 data 是多个单独压缩数据流的拼接，则解压缩所有相应数据流，并返回结果的拼接。

参见上文的 LZMADecompressor 了解有关 format, memlimit 和 filters 参数的说明。

13.4.3. 杂项

lzma.is_check_supported(check)

Returns true if the given integrity check is supported on this system.

CHECK_NONE 和 CHECK_CRC32 总是受支持。 CHECK_CRC64 和 CHECK_SHA256 或许不可用，如果你正在使用基于受限制特性集编译的 liblzma 版本的话。

13.4.4. 指定自定义的过滤器链

过滤器链描述符是由字典组成的序列，其中每个字典包含单个过滤器的 ID 和选项。 每个字典必须包含键 "id"，并可能包含额外的键用来指定基于过滤器的选项。 有效的过滤器 ID 如下：

压缩过滤器：

FILTER_LZMA1 (配合 FORMAT_ALONE 使用)

FILTER_LZMA2 (配合 FORMAT_XZ 和 FORMAT_RAW 使用)

增量过滤器：

FILTER_DELTA

Branch-Call-Jump (BCJ) 过滤器：

FILTER_X86

FILTER_IA64

FILTER_ARM

FILTER_ARMTHUMB

FILTER_POWERPC

FILTER_SPARC

一个过滤器链最多可由 4 个过滤器组成，并且不能为空。 过滤器链中的最后一个过滤器必须为压缩过滤器，其他过滤器必须为 Delta 或 BCJ 过滤器。

压缩过滤器支持下列选项（指定为表示过滤器的字典中的附加条目）：

preset: 压缩预设选项，用于作为未显式指定的选项的默认值的来源。

dict_size: 以字节表示的字典大小。 这应当在 4 KiB 和 1.5 GiB 之间（包含边界）。

lc: 字面值上下文的位数。

lp: 字面值位置的比特数。 总计值 lc + lp 必须不大于 4。

pb: 位置的比特数；必须不大于 4。

mode: MODE_FAST 或 MODE_NORMAL。

nice_len: 对于一个匹配应当被视为“适宜长度”的值。 这应当小于或等于 273。

mf: 要使用的匹配查找器 – MF_HC3, MF_HC4, MF_BT2, MF_BT3 或 MF_BT4。

depth: 匹配查找器使用的最大查找深度。 0 (默认值) 表示基于其他过滤器选项自动选择。

Delta 过滤器保存字节数据之间的差值，在特定环境下可产生更具重复性的输入。 它支持一个 dist 选项，指明要减去的字节之间的差值大小。 默认值为 1，即相邻字节之间的差值。

BCJ 过滤器主要作用于机器码。 它们会转换机器码内的相对分支、调用和跳转以使用绝对寻址，其目标是提升冗余度以供压缩器利用。 这些过滤器支持一个 start_offset 选项，指明应当被映射到输入数据开头的地址。 默认值为 0。

13.4.5. 例子

在已压缩的数据中读取:

import lzma
with lzma.open("file.xz") as f:
    file_content = f.read()

创建一个压缩文件:

import lzma
data = b"Insert Data Here"
with lzma.open("file.xz", "w") as f:
    f.write(data)

在内存中压缩文件:

import lzma
data_in = b"Insert Data Here"
data_out = lzma.compress(data_in)

增量压缩:

import lzma
lzc = lzma.LZMACompressor()
out1 = lzc.compress(b"Some data\n")
out2 = lzc.compress(b"Another piece of data\n")
out3 = lzc.compress(b"Even more data\n")
out4 = lzc.flush()
# Concatenate all the partial results:
result = b"".join([out1, out2, out3, out4])

写入已压缩数据到已打开的文件:

import lzma
with open("file.xz", "wb") as f:
    f.write(b"This data will not be compressed\n")
    with lzma.open(f, "w") as lzf:
        lzf.write(b"This *will* be compressed\n")
    f.write(b"Not compressed\n")

使用自定义过滤器链创建一个已压缩文件:

import lzma
my_filters = [
    {"id": lzma.FILTER_DELTA, "dist": 5},
    {"id": lzma.FILTER_LZMA2, "preset": 7 | lzma.PRESET_EXTREME},
]
with lzma.open("file.xz", "w", filters=my_filters) as f:
    f.write(b"blah blah blah")

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

13. 数据压缩和存档 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

13. 数据压缩和存档 »

|

13.5. zipfile — 使用ZIP存档

源代码： Lib/zipfile.py [https://github.com/python/cpython/tree/3.6/Lib/zipfile.py]

ZIP 文件格式是一个常用的归档与压缩标准。 这个模块提供了创建、读取、写入、添加及列出 ZIP 文件的工具。 任何对此模块的进阶使用都将需要理解此格式，其定义参见 PKZIP 应用程序笔记 [https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT]。

此模块目前不能处理分卷 ZIP 文件。它可以处理使用 ZIP64 扩展（超过 4 GB 的 ZIP 文件）的 ZIP 文件。它支持解密 ZIP 归档中的加密文件，但是目前不能创建一个加密的文件。解密非常慢，因为它是使用原生 Python 而不是 C 实现的。

这个模块定义了以下内容：

exception zipfile.BadZipFile

为损坏的 ZIP 文件抛出的错误。

3.2 新版功能.

exception zipfile.BadZipfile

BadZipFile 的别名，与旧版本 Python 保持兼容性。

3.2 版后已移除.

exception zipfile.LargeZipFile

当 ZIP 文件需要 ZIP64 功能但是未启用时会抛出此错误。

class zipfile.ZipFile

用于读写 ZIP 文件的类。 欲了解构造函数的描述，参阅段落 ZipFile 对象。

class zipfile.PyZipFile

用于创建包含 Python 库的 ZIP 归档的类。

class zipfile.ZipInfo(filename='NoName', date_time=(1980, 1, 1, 0, 0, 0))

用于表示档案内一个成员信息的类。 此类的实例会由 ZipFile 对象的 getinfo() 和 infolist() 方法返回。 大多数 zipfile 模块的用户都不必创建它们，只需使用此模块所创建的实例。 filename 应当是档案成员的全名，date_time 应当是包含六个字段的描述最近修改时间的元组；这些字段的描述请参阅 ZipInfo 对象。

zipfile.is_zipfile(filename)

根据文件的 Magic Number，如果 filename 是一个有效的 ZIP 文件则返回 True，否则返回 False。 filename 也可能是一个文件或类文件对象。

在 3.1 版更改: 支持文件或类文件对象。

zipfile.ZIP_STORED

未被压缩的归档成员的数字常数。

zipfile.ZIP_DEFLATED

常用的 ZIP 压缩方法的数字常数。需要 zlib 模块。

zipfile.ZIP_BZIP2

BZIP2 压缩方法的数字常数。需要 bz2 模块。

3.3 新版功能.

zipfile.ZIP_LZMA

LZMA 压缩方法的数字常数。需要 lzma 模块。

3.3 新版功能.

注解

ZIP 文件格式规范包括自 2001 年以来对 bzip2 压缩的支持，以及自 2006 年以来对 LZMA 压缩的支持。但是，一些工具（包括较旧的 Python 版本）不支持这些压缩方法，并且可能拒绝完全处理 ZIP 文件，或者无法提取单个文件。

参见

PKZIP 应用程序笔记 [https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT]

Phil Katz 编写的 ZIP 文件格式文档，此格式和使用的算法的创建者。

Info-ZIP 主页 [http://www.info-zip.org/]

有关 Info-ZIP 项目的 ZIP 存档程序和开发库的信息。

13.5.1. ZipFile 对象

class zipfile.ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True)

Open a ZIP file, where file can be a path to a file (a string), a file-like object or a path-like object. The mode parameter should be 'r' to read an existing file, 'w' to truncate and write a new file, 'a' to append to an existing file, or 'x' to exclusively create and write a new file. If mode is 'x' and file refers to an existing file, a FileExistsError will be raised. If mode is 'a' and file refers to an existing ZIP file, then additional files are added to it. If file does not refer to a ZIP file, then a new ZIP archive is appended to the file. This is meant for adding a ZIP archive to another file (such as python.exe). If mode is 'a' and the file does not exist at all, it is created. If mode is 'r' or 'a', the file should be seekable. compression is the ZIP compression method to use when writing the archive, and should be ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2 or ZIP_LZMA; unrecognized values will cause NotImplementedError to be raised. If ZIP_DEFLATED, ZIP_BZIP2 or ZIP_LZMA is specified but the corresponding module (zlib, bz2 or lzma) is not available, RuntimeError is raised. The default is ZIP_STORED. If allowZip64 is True (the default) zipfile will create ZIP files that use the ZIP64 extensions when the zipfile is larger than 4 GiB. If it is false zipfile will raise an exception when the ZIP file would require ZIP64 extensions.

如果创建文件时使用 'w', 'x' 或 'a' 模式并且未向归档添加任何文件就执行了 closed，则会将适当的空归档 ZIP 结构写入文件。

ZipFile is also a context manager and therefore supports the with statement. In the example, myzip is closed after the with statement’s suite is finished—even if an exception occurs:

with ZipFile('spam.zip', 'w') as myzip:
    myzip.write('eggs.txt')

3.2 新版功能: 添加了将 ZipFile 用作上下文管理员的功能。

在 3.3 版更改: 添加了对 bzip2 和 lzma 压缩的支持。

在 3.4 版更改: 默认启用 ZIP64 扩展。

在 3.5 版更改: 添加了对不可查找数据流的支持。 并添加了对 'x' 模式的支持。

在 3.6 版更改: 在此之前，对于不可识别的压缩值将引发普通的 RuntimeError。

在 3.6.2 版更改: file 形参接受一个 path-like object。

ZipFile.close()

关闭归档文件。 你必须在退出程序之前调用 close() 否则将不会写入关键记录数据。

ZipFile.getinfo(name)

返回一个 ZipInfo 对象，其中包含有关归档成员 name 的信息。 针对一个目前并不包含于归档中的名称调用 getinfo() 将会引发 KeyError。

ZipFile.infolist()

返回一个列表，其中包含每个归档成员的 ZipInfo 对象。 如果是打开一个现有归档则这些对象的排列顺序与它们对应条目在磁盘上的实际 ZIP 文件中的顺序一致。

ZipFile.namelist()

返回按名称排序的归档成员列表。

ZipFile.open(name, mode='r', pwd=None, *, force_zip64=False)

以二进制文件类对象的形式访问一个归档成员。 name 可以是归档内某个文件的名称也可以是某个 ZipInfo 对象。 如果包含了 mode 形参，则它必须为 'r' (默认值) 或 'w'。 pwd 为用于解密已加密 ZIP 文件的密码。

open() 也是一个上下文管理器，因此支持 with 语句:

with ZipFile('spam.zip') as myzip:
    with myzip.open('eggs.txt') as myfile:
        print(myfile.read())

With mode 'r' the file-like object (ZipExtFile) is read-only and provides the following methods: read(), readline(), readlines(), __iter__(), __next__(). These objects can operate independently of the ZipFile.

如果 mode='w' 则返回一个可写入的文件句柄，它将支持 write() 方法。 当一个可写入的文件句柄被打开时，尝试读写 ZIP 文件中的其他文件将会引发 ValueError。

当写入一个文件时，如果文件大小不能预先确定但是可能超过 2 GiB，可传入 force_zip64=True 以确保标头格式能够支持超大文件。 如果文件大小可以预先确定，则在构造 ZipInfo 对象时应设置 file_size，并将其用作 name 形参。

注解

open(), read() 和 extract() 方法可接受文件名或 ZipInfo 对象。 当尝试读取一个包含重复名称成员的 ZIP 文件时你将发现此功能很有好处。

在 3.6 版更改: 移除了对 mode='U' 的支持。 请使用 io.TextIOWrapper 以在 universal newlines 模式中读取已压缩的文本文件。

在 3.6 版更改: open() 现在可以被用来配合 mode='w' 选项来将文件写入归档。

在 3.6 版更改: 在已关闭的 ZipFile 上调用 open() 将引发 ValueError。 在之前的版本中则会引发 RuntimeError。

ZipFile.extract(member, path=None, pwd=None)

从归档中提取出一个成员放入当前工作目录；member 必须为成员的完整名称或 ZipInfo 对象。 成员的文件信息会尽可能精确地被提取。 path 指定一个要提取到的不同目录。 member 可以是一个文件名或 ZipInfo 对象。 pwd 是用于解密文件的密码。

返回所创建的经正规化的路径（对应于目录或新文件）。

注解

如果一个成员文件名为绝对路径，则将去掉驱动器/UNC共享点和前导的（反）斜杠，例如: ///foo/bar 在 Unix 上将变为 foo/bar，而 C:\foo\bar 在 Windows 上将变为 foo\bar。 并且一个成员文件名中的所有 ".." 都将被移除，例如: ../../foo../../ba..r 将变为 foo../ba..r。 在 Windows 上非法字符 (:, <, >, |, ", ?, and *) 会被替换为下划线 (_)。

在 3.6 版更改: 在已关闭的 ZipFile 上调用 extract() 将引发 ValueError。 在之前的版本中则将引发 RuntimeError。

在 3.6.2 版更改: path 形参接受一个 path-like object。

ZipFile.extractall(path=None, members=None, pwd=None)

从归档中提取出所有成员放入当前工作目录。 path 指定一个要提取到的不同目录。 members 为可选项且必须为 namelist() 所返回列表的一个子集。 pwd 是用于解密文件的密码。

警告

绝不要未经预先检验就从不可靠的源中提取归档文件。 这样有可能在 path 之外创建文件，例如某些成员具有以 "/" 开始的文件名或带有两个点号 ".." 的文件名。 此模块会尝试防止这种情况。 参见 extract() 的注释。

在 3.6 版更改: 在已关闭的 ZipFile 上调用 extractall() 将引发 ValueError。 在之前的版本中则将引发 RuntimeError。

在 3.6.2 版更改: path 形参接受一个 path-like object。

ZipFile.printdir()

将归档的目录表打印到 sys.stdout。

ZipFile.setpassword(pwd)

设置 pwd 为用于提取已加密文件的默认密码。

ZipFile.read(name, pwd=None)

返回归档中文件 name 的字节数据。 name 是归档中文件的名称，或是一个 ZipInfo 对象。 归档必须以读取或追加方式打开。 pwd 为用于已加密文件的密码，并且如果指定该参数则它将覆盖通过 setpassword() 设置的默认密码。 on a ZipFile that uses a compression method 在使用 ZIP_STORED , ZIP_DEFLATED, ZIP_BZIP2 或 ZIP_LZMA 以外的压缩方法的 ZipFile 上调用 read() 将引发 NotImplementedError。 如果相应的压缩模块不可用也会引发错误。

在 3.6 版更改: 在已关闭的 ZipFile 上调用 read() 将引发 ValueError。 在之前的版本中则会引发 RuntimeError.

ZipFile.testzip()

读取归档中的所有文件并检查它们的 CRC 和文件头。 返回第一个已损坏文件的名称，在其他情况下则返回 None。

在 3.6 版更改: 在已关闭的 ZipFile 上调用 testzip() 将引发 ValueError。 在之前的版本中则将引发 RuntimeError。

ZipFile.write(filename, arcname=None, compress_type=None)

Write the file named filename to the archive, giving it the archive name arcname (by default, this will be the same as filename, but without a drive letter and with leading path separators removed). If given, compress_type overrides the value given for the compression parameter to the constructor for the new entry. The archive must be open with mode 'w', 'x' or 'a'.

注解

归档名称应当是基于归档根目录的相对路径，也就是说，它们不应以路径分隔符开头。

注解

如果 arcname (或 filename，如果 arcname 未给出) 包含一个空字节，则归档中该文件的名称将在空字节位置被截断。

在 3.6 版更改: 在使用 'r' 模式创建的 ZipFile 或已关闭的 ZipFile 上调用 write() 将引发 ValueError。 在之前的版本中则会引发 RuntimeError。

ZipFile.writestr(zinfo_or_arcname, data[, compress_type])

将一个文件写入归档。 内容为 data，它可以是一个 str 或 bytes 的实例；如果是 str，则会先使用 UTF-8 进行编码。 zinfo_or_arcname 可以是它在归档中将被给予的名称，或者是 ZipInfo 的实例。 如果它是一个实例，则至少必须给定文件名、日期和时间。 如果它是一个名称，则日期和时间会被设为当前日期和时间。 归档必须以 'w', 'x' 或 'a' 模式打开。

If given, compress_type overrides the value given for the compression parameter to the constructor for the new entry, or in the zinfo_or_arcname (if that is a ZipInfo instance).

注解

当传入一个 ZipInfo 实例作为 zinfo_or_arcname 形参时，所使用的压缩方法将为在给定的 ZipInfo 实例的 compress_type 成员中指定的方法。 默认情况下，ZipInfo 构造器将将此成员设为 ZIP_STORED。

在 3.2 版更改: compress_type 参数。

在 3.6 版更改: 在使用 'r' 模式创建的 ZipFile 或已关闭的 ZipFile 上调用 writestr() 将引发 ValueError。 在之前的版本中则会引发 RuntimeError。

以下数据属性也是可用的:

ZipFile.filename

ZIP 文件的名称。

ZipFile.debug

要使用的调试输出等级。 这可以设为从 0 (默认无输出) 到 3 (最多输出) 的值。 调试信息会被写入 sys.stdout。

ZipFile.comment

关联到 ZIP 文件的 bytes 对象形式的说明。 如果将说明赋给以 'w', 'x' 或 'a' 模式创建的 ZipFile 实例，它的长度不应超过 65535 字节。 超过此长度的说明将被截断。

13.5.2. PyZipFile 对象

PyZipFile 构造器接受与 ZipFile 构造器相同的形参，以及一个额外的形参 optimize。

class zipfile.PyZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, optimize=-1)

3.2 新版功能: optimize 形参。

在 3.4 版更改: 默认启用 ZIP64 扩展。

实例在 ZipFile 对象所具有的方法以外还附加了一个方法:

writepy(pathname, basename='', filterfunc=None)

查找 *.py 文件并将相应的文件添加到归档。

如果 PyZipFile 的 optimize 形参未给定或为 -1，则相应的文件为 *.pyc 文件，并在必要时进行编译。

如果 PyZipFile 的 optimize 形参为 0, 1 或 2，则限具有相应优化级别 (参见 compile()) 的文件会被添加到归档，并在必要时进行编译。

If pathname is a file, the filename must end with .py, and just the (corresponding *.pyc) file is added at the top level (no path information). If pathname is a file that does not end with .py, a RuntimeError will be raised. If it is a directory, and the directory is not a package directory, then all the files *.pyc are added at the top level. If the directory is a package directory, then all *.pyc are added under the package name as a file path, and if any subdirectories are package directories, all of these are added recursively.

basename 仅限在内部使用。

如果给定 filterfunc，则它必须是一个接受单个字符串参数的函数。 在将其添加到归档之前它将被传入每个路径（包括每个单独的完整路径）。 如果 filterfunc 返回假值，则路径将不会被添加，而如果它是一个目录则其内容将被忽略。 例如，如果我们的测试文件全都位于 test 目录或以字符串 test_ 打头，则我们可以使用一个 filterfunc 来排除它们:

>>> zf = PyZipFile('myprog.zip')
>>> def notests(s):
...     fn = os.path.basename(s)
...     return (not (fn == 'test' or fn.startswith('test_')))
>>> zf.writepy('myprog', filterfunc=notests)

writepy() 方法会产生带有这样一些文件名的归档:

string.pyc                   # Top level name
test/__init__.pyc            # Package directory
test/testall.pyc             # Module test.testall
test/bogus/__init__.pyc      # Subpackage directory
test/bogus/myfile.pyc        # Submodule test.bogus.myfile

3.4 新版功能: filterfunc 形参。

在 3.6.2 版更改: pathname 形参接受一个 path-like object。

13.5.3. ZipInfo 对象

ZipInfo 类的实例会通过 getinfo() 和 ZipFile 对象的 infolist() 方法返回。 每个对象将存储关于 ZIP 归档的一个成员的信息。

有一个类方法可以为文件系统文件创建 ZipInfo 实例:

classmethod ZipInfo.from_file(filename, arcname=None)

为文件系统中的文件构造一个 ZipInfo 实例，并准备将其添加到一个 zip 文件。

filename 应为文件系统中某个文件或目录的路径。

如果指定了 arcname，它会被用作归档中的名称。 如果未指定 arcname，则所用名称与 filename 相同，但将去除任何驱动器盘符和打头的路径分隔符。

3.6 新版功能.

在 3.6.2 版更改: filename 形参接受一个 path-like object。

实例具有下列方法和属性:

ZipInfo.is_dir()

如果此归档成员是一个目录则返回 True。

这会使用条目的名称：目录应当总是以 / 结尾。

3.6 新版功能.

ZipInfo.filename

归档中的文件名称。

ZipInfo.date_time

上次修改存档成员的时间和日期。这是六个值的元组：

索引

值

0

Year (>= 1980)

1

月（1为基数）

2

月份中的日期（1为基数）

3

小时（0为基数）

4

分钟（0为基数）

5

秒（0为基数）

注解

ZIP文件格式不支持1980年以前的时间戳。

ZipInfo.compress_type

归档成员的压缩类型。

ZipInfo.comment

bytes 对象形式的单个归档成员的注释。

ZipInfo.extra

扩展字段数据。 PKZIP Application Note [https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT] 包含一些保存于该 bytes 对象中的内部结构的注释。

ZipInfo.create_system

创建 ZIP 归档所用的系统。

ZipInfo.create_version

创建 ZIP 归档所用的 PKZIP 版本。

ZipInfo.extract_version

需要用来提取归档的 PKZIP 版本。

ZipInfo.reserved

必须为零。

ZipInfo.flag_bits

ZIP 标志位。

ZipInfo.volume

文件中的分卷号。

ZipInfo.internal_attr

内部属性。

ZipInfo.external_attr

外部文件属性。

ZipInfo.header_offset

文件头的字节偏移量。

ZipInfo.CRC

未压缩文件的 CRC-32。

ZipInfo.compress_size

已压缩数据的大小。

ZipInfo.file_size

未压缩文件的大小。

13.5.4. 命令行界面

zipfile 模块提供了简单的命令行接口用于与 ZIP 归档的交互。

如果你想要创建一个新的 ZIP 归档，请在 -c 选项后指定其名称然后列出应当被包含的文件名:

$ python -m zipfile -c monty.zip spam.txt eggs.txt

传入一个字典也是可接受的:

$ python -m zipfile -c monty.zip life-of-brian_1979/

如果你想要将一个 ZIP 归档提取到指定的目录，请使用 -e 选项:

$ python -m zipfile -e monty.zip target-dir/

对于一个 ZIP 归档中的文件列表，请使用 -l 选项:

$ python -m zipfile -l monty.zip

13.5.4.1. 命令行选项

-l <zipfile>

列出一个 zipfile 中的文件名。

-c <zipfile> <source1> ... <sourceN>

基于源文件创建 zipfile。

-e <zipfile> <output_dir>

将 zipfile 提取到目标目录中。

-t <zipfile>

检测 zipfile 是否有效。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

13. 数据压缩和存档 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

13. 数据压缩和存档 »

|

13.6. tarfile — 读写tar归档文件

源代码: Lib/tarfile.py [https://github.com/python/cpython/tree/3.6/Lib/tarfile.py]

tarfile 模块可以用来读写 tar 归档，包括使用 gzip, bz2 和 lzma 压缩的归档。 请使用 zipfile 模块来读写 .zip 文件，或者使用 shutil 的高层级函数。

一些事实和数字:

读写 gzip, bz2 和 lzma 解压的归档要求相应的模块可用。

支持读取 / 写入 POSIX.1-1988 (ustar) 格式。

对 GNU tar 格式的读/写支持，包括 longname 和 longlink 扩展，对所有种类 sparse 扩展的只读支持，包括 sparse 文件的恢复。

对 POSIX.1-2001 (pax) 格式的读/写支持。

处理目录、正常文件、硬链接、符号链接、fifo 管道、字符设备和块设备，并且能够获取和恢复文件信息例如时间戳、访问权限和所有者等。

在 3.3 版更改: 添加了对 lzma 压缩的支持。

tarfile.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)

针对路径名 name 返回 TarFile 对象。 有关 TarFile 对象以及所允许的关键字参数的详细信息请参阅 TarFile 对象。

mode 必须是 'filemode[:compression]' 形式的字符串，其默认值为 'r'。 以下是模式组合的完整列表:

模式

action

'r' or 'r:*'

打开和读取使用透明压缩（推荐）。

'r:'

打开和读取不使用压缩。

'r:gz'

打开和读取使用gzip 压缩。

'r:bz2'

打开和读取使用bzip2 压缩。

'r:xz'

打开和读取使用lzma 压缩。

'x' or 'x:'

创建tarfile不进行压缩。如果文件已经存在，则抛出 FileExistsError 异常。

'x:gz'

使用gzip压缩创建tarfile。如果文件已经存在，则抛出 FileExistsError 异常。

'x:bz2'

使用bzip2 压缩创建tarfile。如果文件已经存在，则抛出 FileExistsError 异常。

'x:xz'

使用lzma 压缩创建tarfile。如果文件已经存在，则抛出 FileExistsError 异常。

'a' or 'a:'

打开以便在没有压缩的情况下追加。如果文件不存在，则创建该文件。

'w' or 'w:'

打开用于未压缩的写入。

'w:gz'

打开用于 gzip 压缩的写入。

'w:bz2'

打开用于 bzip2 压缩的写入。

'w:xz'

打开用于 lzma 压缩的写入。

请注意 'a:gz', 'a:bz2' 或 'a:xz' 是不可能的组合。 如果 mode 不适用于打开特定（压缩的）文件用于读取，则会引发 ReadError。 请使用 mode 'r' 来避免这种情况。 如果某种压缩方法不受支持，则会引发 CompressionError。

如果指定了 fileobj，它会被用作对应于 name 的以二进制模式打开的 file object 的替代。 它会被设定为处在位置 0。

对于 'w:gz', 'r:gz', 'w:bz2', 'r:bz2', 'x:gz', 'x:bz2' 等模式，tarfile.open() 接受关键字参数 compresslevel (默认值为 9) 来指定文件的压缩等级。

针对特殊的目的，还存在第二种 mode 格式: 'filemode|[compression]'。 tarfile.open() 将返回一个将其数据作为数据块流来处理的 TarFile 对象。 对此文件将不能执行随机查找。 如果给定了 fileobj，它可以是任何具有 read() 或 write() 方法 (由 mode 确定) 的对象。 bufsize 指定块大小，默认值为 20 * 512 字节。 可与此格式组合使用的有 sys.stdin, 套接字 file object 或磁带设备等。 但是，对于这样的 TarFile 对象存在不允许随机访问的限制，参见 例子。 目前可用的模式如下:

模式

动作

'r|*'

打开 tar 块的 流 以进行透明压缩读取。

'r|'

打开一个未压缩的 tar 块的 stream 用于读取。

'r|gz'

打开一个 gzip 压缩的 stream 用于读取。

'r|bz2'

打开一个 bzip2 压缩的 stream 用于读取。

'r|xz'

打开一个 lzma 压缩 stream 用于读取。

'w|'

打开一个未压缩的 stream 用于写入。

'w|gz'

打开一个 gzip 压缩的 stream 用于写入。

'w|bz2'

打开一个 bzip2 压缩的 stream 用于写入。

'w|xz'

打开一个 lzma 压缩的 stream 用于写入。

在 3.5 版更改: 添加了 'x' (仅创建) 模式。

在 3.6 版更改: name 形参接受一个 path-like object。

class tarfile.TarFile

用于读取和写入 tar 归档的类。 请不要直接使用这个类：而要使用 tarfile.open()。 参见 TarFile 对象。

tarfile.is_tarfile(name)

如果 name 是一个 tarfile 模块能读取的 tar 归档文件则返回 True。

tarfile 模块定义了以下异常:

exception tarfile.TarError

所有 tarfile 异常的基类。

exception tarfile.ReadError

当一个不能被 tarfile 模块处理或者因某种原因而无效的 tar 归档被打开时将被引发。

exception tarfile.CompressionError

当一个压缩方法不受支持或者当数据无法被正确解码时将被引发。

exception tarfile.StreamError

当达到流式 TarFile 对象的典型限制时将被引发。

exception tarfile.ExtractError

当使用 TarFile.extract() 时针对 non-fatal 所引发的异常，但是仅限 TarFile.errorlevel== 2。

exception tarfile.HeaderError

如果获取的缓冲区无效则会由 TarInfo.frombuf() 引发的异常。

以下常量在模块层级上可用:

tarfile.ENCODING

默认的字符编码格式：在 Windows 上为 'utf-8'，其他系统上则为 sys.getfilesystemencoding() 所返回的值。

以下常量各自定义了一个 tarfile 模块能够创建的 tar 归档格式。 相关细节请参阅 受支持的 tar 格式 小节。

tarfile.USTAR_FORMAT

POSIX.1-1988 (ustar) 格式。

tarfile.GNU_FORMAT

GNU tar 格式。

tarfile.PAX_FORMAT

POSIX.1-2001 (pax) 格式。

tarfile.DEFAULT_FORMAT

用于创建归档的默认格式。 目前为 GNU_FORMAT。

参见

模块 zipfile

zipfile 标准模块的文档。

归档操作

标准 shutil 模块所提供的高层级归档工具的文档。

GNU tar manual, Basic Tar Format [https://www.gnu.org/software/tar/manual/html_node/Standard.html]

针对 tar 归档文件的文档，包含 GNU tar 扩展。

13.6.1. TarFile 对象

TarFile 对象提供了一个 tar 归档的接口。 一个 tar 归档就是数据块的序列。 一个归档成员（被保存文件）是由一个标头块加多个数据块组成的。 一个文件可以在一个 tar 归档中多次被保存。 每个归档成员都由一个 TarInfo 对象来代表，详情参见 TarInfo 对象。

TarFile 对象可在 with 语句中作为上下文管理器使用。 当语句块结束时它将自动被关闭。 请注意在发生异常事件时被打开用于写入的归档将不会被终结；只有内部使用的文件对象将被关闭。 相关用例请参见 例子。

3.2 新版功能: 添加了对上下文管理器协议的支持。

class tarfile.TarFile(name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, ignore_zeros=False, encoding=ENCODING, errors='surrogateescape', pax_headers=None, debug=0, errorlevel=0)

下列所有参数都是可选项并且也可作为实例属性来访问。

name 是归档的路径名称。 name 可以是一个 path-like object。 如果给定了 fileobj 则它可以被省略。 在此情况下，如果对象的 name 属性存在则它会被使用。

mode 可以为 'r' 表示从现有归档读取，'a' 表示将数据追加到现有文件，'w' 表示创建新文件覆盖现有文件，或者 'x' 表示仅在文件不存在时创建新文件。

如果给定了 fileobj，它会被用于读取或写入数据。 如果可以被确定，则 mode 会被 fileobj 的模式所覆盖。 fileobj 的使用将从位置 0 开始。

注解

当 TarFile 被关闭时，fileobj 不会被关闭。

format 控制归档格式。 它必须为在模块层级定义的常量 USTAR_FORMAT, GNU_FORMAT 或 PAX_FORMAT 中的一个。

tarinfo 参数可以被用来将默认的 TarInfo 类替换为另一个。

如果 dereference 为 False，则会将符号链接和硬链接添加到归档中。 如果为 True，则会将目标文件的内容添加到归档中。 在不支持符号链接的系统上参数将不起作用。

如果 ignore_zeros 为 False，则会将空的数据块当作归档的末尾来处理。 如果为 True，则会跳过空的（和无效的）数据块并尝试获取尽可能多的成员。 此参数仅适用于读取拼接的或损坏的归档。

debug 可设为从 0 (无调试消息) 到 3 (全部调试消息)。 消息会被写入到 sys.stderr。

如果 errorlevel 为 0，则当使用 TarFile.extract() 时会忽略所有错误。 无论何种情况，当启用调试时它们都将被显示为调试输出的错误消息。 如果为 1，则所有 fatal 错误会被作为 OSError 异常被引发。 如果为 2，则所有 non-fatal 错误也会被作为 TarError 异常被引发。

encoding 和 errors 参数定义了读取或写入归档所使用的字符编码格式以及要如何处理转换错误。 默认设置将适用于大多数用户。 要深入了解详情可参阅 Unicode 问题 小节。

可选的 pax_headers 参数是字符串的字典，如果 format 为 PAX_FORMAT 它将被作为 pax 全局标头被添加。

在 3.2 版更改: 使用 'surrogateescape' 作为 errors 参数的默认值。

在 3.5 版更改: 添加了 'x' (仅创建) 模式。

在 3.6 版更改: name 形参接受一个 path-like object。

classmethod TarFile.open(...)

作为替代的构造器。 tarfile.open() 函数实际上是这个类方法的快捷方式。

TarFile.getmember(name)

返回成员 name 的 TarInfo 对象。 如果 name 在归档中找不到，则会引发 KeyError。

注解

如果一个成员在归档中出现超过一次，它的最后一次出现会被视为是最新的版本。

TarFile.getmembers()

以 TarInfo 对象列表的形式返回归档的成员。 列表的顺序与归档中成员的顺序一致。

TarFile.getnames()

以名称列表的形式返回成员。 它的顺序与 getmembers() 所返回列表的顺序一致。

TarFile.list(verbose=True, *, members=None)

将内容清单打印到 sys.stdout。 如果 verbose 为 False，则将只打印成员名称。 如果为 True，则输出将类似于 ls -l 的输出效果。 如果给定了可选的 members，它必须为 getmembers() 所返回的列表的一个子集。

在 3.5 版更改: 添加了 members 形参。

TarFile.next()

当 TarFile 被打开用于读取时，以 TarInfo 对象的形式返回归档的下一个成员。 如果不再有可用对象则返回 None。

TarFile.extractall(path=".", members=None, *, numeric_owner=False)

将归档中的所有成员提取到当前工作目录或 path 目录。 如果给定了可选的 members，则它必须为 getmembers() 所返回的列表的一个子集。 字典信息例如所有者、修改时间和权限会在所有成员提取完毕后被设置。 这样做是为了避免两个问题：目录的修改时间会在每当在其中创建文件时被重置。 并且如果目录的权限不允许写入，提取文件到目录的操作将失败。

如果 numeric_owner 为 True，则将使用来自 tarfile 的 uid 和 gid 数值来设置被提取文件的所有者/用户组。 在其他情况下，则会使用来自 tarfile 的名称值。

警告

绝不要未经预先检验就从不可靠的源中提取归档文件。 这样有可能在 path 之外创建文件，例如某些成员具有以 "/" 开始的绝对路径文件名或带有两个点号 ".." 的文件名。

在 3.5 版更改: 添加了 numeric_owner 形参。

在 3.6 版更改: path 形参接受一个 path-like object。

TarFile.extract(member, path="", set_attrs=True, *, numeric_owner=False)

从归档中提取出一个成员放入当前工作目录，将使用其完整名称。 成员的文件信息会尽可能精确地被提取。 member 可以是一个文件名或 TarInfo 对象。 你可以使用 path 指定一个不同的目录。 path 可以是一个 path-like object。 将会设置文件属性 (owner, mtime, mode) 除非 set_attrs 为假值。

如果 numeric_owner 为 True，则将使用来自 tarfile 的 uid 和 gid 数值来设置被提取文件的所有者/用户组。 在其他情况下，则会使用来自 tarfile 的名称值。

注解

extract() 方法不会处理某些提取问题。 在大多数情况下你应当考虑使用 extractall() 方法。

警告

参看 extractall() 的警告信息。

在 3.2 版更改: 添加了 set_attrs 形参。

在 3.5 版更改: 添加了 numeric_owner 形参。

在 3.6 版更改: path 形参接受一个 path-like object。

TarFile.extractfile(member)

将归档中的一个成员提取为文件对象。 member 可以是一个文件名或 TarInfo 对象。 如果 member 是一个常规文件或链接，则会返回一个 io.BufferedReader 对象。 在其他情况下将返回 None。

在 3.3 版更改: 返回一个 io.BufferedReader 对象。

TarFile.add(name, arcname=None, recursive=True, exclude=None, *, filter=None)

Add the file name to the archive. name may be any type of file (directory, fifo, symbolic link, etc.). If given, arcname specifies an alternative name for the file in the archive. Directories are added recursively by default. This can be avoided by setting recursive to False. If exclude is given, it must be a function that takes one filename argument and returns a boolean value. Depending on this value the respective file is either excluded (True) or added (False). If filter is specified it must be a keyword argument. It should be a function that takes a TarInfo object argument and returns the changed TarInfo object. If it instead returns None the TarInfo object will be excluded from the archive. See 例子 for an example.

在 3.2 版更改: 添加了 filter 形参。

3.2 版后已移除: The exclude parameter is deprecated, please use the filter parameter instead.

TarFile.addfile(tarinfo, fileobj=None)

将 TarInfo 对象 tarinfo 添加到归档。 如果给定了 fileobj，它应当是一个 binary file，并会从中读取 tarinfo.size 个字节添加到归档。 你可以直接创建 TarInfo 对象，或是通过使用 gettarinfo()。

TarFile.gettarinfo(name=None, arcname=None, fileobj=None)

基于 os.stat() 的结果或者现有文件的相同数据创建一个 TarInfo。 文件或者是命名为 name，或者是使用文件描述符指定为一个 file object fileobj。 name 可以是一个 path-like object。 如果给定了 arcname，则它将为归档中的文件指定一个替代名称，在其他情况下，名称将从 fileobj 的 name 属性或 name 参数获取。 名称应当是一个文本字符串。

你可以在使用 addfile() 添加 TarInfo 的某些属性之前修改它们。 如果文件对象不是从文件开头进行定位的普通文件对象，size 之类的属性就可能需要修改。 例如 GzipFile 之类的文件就属于这种情况。 name 也可以被修改，在这种情况下 arcname 可以是一个占位字符串。

在 3.6 版更改: name 形参接受一个 path-like object。

TarFile.close()

关闭 TarFile。 在写入模式下，会向归档添加两个表示结束的零数据块。

TarFile.pax_headers

一个包含 pax 全局标头的键值对的字典。

13.6.2. TarInfo 对象

TarInfo 对象代表 TarFile 中的一个文件。 除了会存储所有必要的文件属性（例如文件类型、大小、时间、权限、所有者等），它还提供了一些确定文件类型的有用方法。 此对象 并不 包含文件数据本身。

TarInfo 对象可通过 TarFile 的方法 getmember(), getmembers() 和 gettarinfo() 返回。

class tarfile.TarInfo(name="")

创建一个 TarInfo 对象。

classmethod TarInfo.frombuf(buf, encoding, errors)

基于字符串缓冲区 buf 创建并返回一个 TarInfo 对象。

如果缓冲区无效则会引发 HeaderError。

classmethod TarInfo.fromtarfile(tarfile)

从 TarFile 对象 tarfile 读取下一个成员并将其作为 TarInfo 对象返回。

TarInfo.tobuf(format=DEFAULT_FORMAT, encoding=ENCODING, errors='surrogateescape')

基于 TarInfo 对象创建一个字符串缓冲区。 有关参数的信息请参见 TarFile 类的构造器。

在 3.2 版更改: 使用 'surrogateescape' 作为 errors 参数的默认值。

TarInfo 对象具有以下公有数据属性:

TarInfo.name

归档成员的名称。

TarInfo.size

以字节表示的大小。

TarInfo.mtime

上次修改的时间。

TarInfo.mode

权限位。

TarInfo.type

文件类型。 type 通常为以下常量之一: REGTYPE, AREGTYPE, LNKTYPE, SYMTYPE, DIRTYPE, FIFOTYPE, CONTTYPE, CHRTYPE, BLKTYPE, GNUTYPE_SPARSE。 要更方便地确定一个 TarInfo 对象的类型，请使用下述的 is*() 方法。

TarInfo.linkname

目标文件名的名称，该属性仅在类型为 LNKTYPE 和 SYMTYPE 的 TarInfo 对象中存在。

TarInfo.uid

最初保存该成员的用户的用户 ID。

TarInfo.gid

最初保存该成员的用户的分组 ID。

TarInfo.uname

用户名。

TarInfo.gname

分组名。

TarInfo.pax_headers

一个包含所关联的 pax 扩展标头的键值对的字典。

TarInfo 对象还提供了一些便捷查询方法:

TarInfo.isfile()

如果 Tarinfo 对象为普通文件则返回 True。

TarInfo.isreg()

与 isfile() 相同。

TarInfo.isdir()

如果为目录则返回 True。

TarInfo.issym()

如果为符号链接则返回 True。

TarInfo.islnk()

如果为硬链接则返回 True。

TarInfo.ischr()

如果为字符设备则返回 True。

TarInfo.isblk()

如果为块设备则返回 True。

TarInfo.isfifo()

如果为 FIFO 则返回 True。.

TarInfo.isdev()

如果为字符设备、块设备或 FIFO 之一则返回 True。

13.6.3. 命令行界面

3.4 新版功能.

tarfile 模块提供了简单的命令行接口以便与 tar 归档进行交互。

如果你想要创建一个新的 tar 归档，请在 -c 选项后指定其名称然后列出应当被包含的文件名:

$ python -m tarfile -c monty.tar  spam.txt eggs.txt

传入一个字典也是可接受的:

$ python -m tarfile -c monty.tar life-of-brian_1979/

如果你想要将一个 tar 归档提取到指定的目录，请使用 -e 选项:

$ python -m tarfile -e monty.tar

你也可以通过传入目录名称将一个 tar 归档提取到不同的目录:

$ python -m tarfile -e monty.tar  other-dir/

要获取一个 tar 归档中文件的列表，请使用 -l 选项:

$ python -m tarfile -l monty.tar

13.6.3.1. 命令行选项

-l <tarfile>

--list <tarfile>

列出一个 tarfile 中的文件名。

-c <tarfile> <source1> ... <sourceN>

--create <tarfile> <source1> ... <sourceN>

基于源文件创建 tarfile。

-e <tarfile> [<output_dir>]

--extract <tarfile> [<output_dir>]

如果未指定 output_dir 则会将 tarfile 提取到当前目录。

-t <tarfile>

--test <tarfile>

检测 tarfile 是否有效。

-v, --verbose

更详细地输出结果。

13.6.4. 例子

如何将整个 tar 归档提取到当前工作目录:

import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()

如何通过 TarFile.extractall() 使用生成器函数而非列表来提取一个 tar 归档的子集:

import os
import tarfile

def py_files(members):
    for tarinfo in members:
        if os.path.splitext(tarinfo.name)[1] == ".py":
            yield tarinfo

tar = tarfile.open("sample.tar.gz")
tar.extractall(members=py_files(tar))
tar.close()

如何基于一个文件名列表创建未压缩的 tar 归档:

import tarfile
tar = tarfile.open("sample.tar", "w")
for name in ["foo", "bar", "quux"]:
    tar.add(name)
tar.close()

使用 with 语句的同一个示例:

import tarfile
with tarfile.open("sample.tar", "w") as tar:
    for name in ["foo", "bar", "quux"]:
        tar.add(name)

如何读取一个 gzip 压缩的 tar 归档并显示一些成员信息:

import tarfile
tar = tarfile.open("sample.tar.gz", "r:gz")
for tarinfo in tar:
    print(tarinfo.name, "is", tarinfo.size, "bytes in size and is", end="")
    if tarinfo.isreg():
        print("a regular file.")
    elif tarinfo.isdir():
        print("a directory.")
    else:
        print("something else.")
tar.close()

如何创建一个归档并使用 TarFile.add() 中的 filter 形参来重置用户信息:

import tarfile
def reset(tarinfo):
    tarinfo.uid = tarinfo.gid = 0
    tarinfo.uname = tarinfo.gname = "root"
    return tarinfo
tar = tarfile.open("sample.tar.gz", "w:gz")
tar.add("foo", filter=reset)
tar.close()

13.6.5. 受支持的 tar 格式

通过 tarfile 模块可以创建三种 tar 格式:

The POSIX.1-1988 ustar 格式 (USTAR_FORMAT)。 它支持最多 256 个字符的文件名长度和最多 100 个字符的链接名长度。 文件大小上限为 8 GiB。 这是一种老旧但广受支持的格式。

GNU tar 格式 (GNU_FORMAT)。 它支持长文件名和链接名、大于 8 GiB 的文件以及稀疏文件。 它是 GNU/Linux 系统上的事实标准。 tarfile 完全支持针对长名称的 GNU tar 扩展，稀疏文件支持则限制为只读。

POSIX.1-2001 pax 格式 (PAX_FORMAT)。 它是几乎无限制的最灵活格式。 它支持长文件名和链接名，大文件以及使用便捷方式存储路径名。 但是并非所有现今的 tar 实现都能够正确地处理 pax 归档。

pax 格式是对现有 ustar 格式的扩展。 它会对无法以其他方式存储的信息使用附加标头。 存在两种形式的 pax 标头：扩展标头只影响后续的文件标头，全局标头则适用于完整归档并会影响所有后续的文件。 为了便于移植，在 pax 标头中的所有数据均以 UTF-8 编码。

还有一些 tar 格式的其他变种，它们可以被读取但不能被创建:

古老的 V7 格式。 这是来自 Unix 第七版的第一个 tar 格式，它只存储常规文件和目录。 名称长度不能超过 100 个字符，并且没有用户/分组名信息。 某些归档在带有非 ASCII 字符字段的情况下会产生计算错误的标头校验和。

SunOS tar 扩展格式。 此格式是 POSIX.1-2001 pax 格式的一个变种，但并不保持兼容。

13.6.6. Unicode 问题

最初 tar 格式被设计用来在磁带机上生成备份，主要关注于保存文件系统信息。 现在 tar 归档通常用于文件分发和在网络上交换归档。 最初格式（它是所有其他格式的基础）的一个问题是它没有支持不同字符编码格式的概念。 例如，一个在 UTF-8 系统上创建的普通 tar 归档如果包含非 ASCII 字符则将无法在 Latin-1 系统上被正确读取。 文本元数据（例如文件名，链接名，用户/分组名）将变为损坏状态。 不幸的是，没有什么办法能够自动检测一个归档的编码格式。 pax 格式被设计用来解决这个问题。 它使用通用字符编码格式 UTF-8 来存储非 ASCII 元数据。

在 tarfile 中字符转换的细节由 TarFile 类的 encoding 和 errors 关键字参数控制。

encoding 定义了用于归档中元数据的字符编码格式。 默认值为 sys.getfilesystemencoding() 或是回退选项 'ascii'。 根据归档是被读取还是被写入，元数据必须被解码或编码。 如果没有正确设置 encoding，转换可能会失败。

errors 参数定义了不能被转换的字符将如何处理。 可能的取值在 错误处理方案 小节列出。 默认方案为 'surrogateescape'，它也被 Python 用于文件系统调用，参见 文件名，命令行参数，以及环境变量。。

对于 PAX_FORMAT 归档，encoding 通常是不必要的，因为所有元数据都使用 UTF-8 来存储。 encoding 仅在解码二进制 pax 标头或存储带有替代字符的字符串等少数场景下会被使用。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

13. 数据压缩和存档 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

14. 文件格式

本章中描述的模块解析各种不是标记语言且与电子邮件无关的杂项文件格式。

14.1. csv — CSV 文件读写

14.1.1. 模块内容

14.1.2. 变种与格式参数

14.1.3. Reader 对象

14.1.4. Writer 对象

14.1.5. 例子

14.2. configparser — 配置文件解析器

14.2.1. 快速起步

14.2.2. 支持的数据类型

14.2.3. 回退值

14.2.4. 受支持的 INI 文件结构

14.2.5. 值的插入

14.2.6. 映射协议访问

14.2.7. 定制解析器行为

14.2.8. 旧式 API 示例

14.2.9. ConfigParser 对象

14.2.10. RawConfigParser 对象

14.2.11. 异常

14.3. netrc — netrc 文件处理

14.3.1. netrc 对象

14.4. xdrlib — 编码与解码 XDR 数据

14.4.1. Packer 对象

14.4.2. Unpacker 对象

14.4.3. 异常

14.5. plistlib — 生成与解析 Mac OS X .plist 文件

14.5.1. 例子

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

14. 文件格式 »

|

14.1. csv — CSV 文件读写

源代码： Lib/csv.py [https://github.com/python/cpython/tree/3.6/Lib/csv.py]

CSV (Comma Separated Values) 格式是电子表格和数据库中最常见的输入、输出文件格式。在 RFC 4180 [https://tools.ietf.org/html/rfc4180.html] 规范推出的很多年前，CSV 格式就已经被开始使用了，由于当时并没有合理的标准，不同应用程序读写的数据会存在细微的差别。这种差别让处理多个来源的 CSV 文件变得困难。但尽管分隔符会变化，此类文件的大致格式是相似的，所以编写一个单独的模块以高效处理此类数据，将程序员从读写数据的繁琐细节中解放出来是有可能的。

The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the format preferred by Excel,” or “read data from this file which was generated by Excel,” without knowing the precise details of the CSV format used by Excel. Programmers can also describe the CSV formats understood by other applications or define their own special-purpose CSV formats.

csv 模块中的 reader 类和 writer 类可用于读写序列化的数据。也可使用 DictReader 类和 DictWriter 类以字典的形式读写数据。

参见

该实现在“Python 增强提议” - PEP 305 (CSV 文件 API) 中被提出

《Python 增强提议》提出了对 Python 的这一补充。

14.1.1. 模块内容

csv 模块定义了以下函数：

csv.reader(csvfile, dialect='excel', **fmtparams)

返回一个 reader 对象，该对象将逐行遍历 csvfile。csvfile 可以是任何对象，只要这个对象支持 iterator 协议并在每次调用 __next__() 方法时都返回字符串，文件对象 和列表对象均适用。如果 csvfile 是文件对象，则打开它时应使用 newline=''。 1 可选参数 dialect 是用于不同的 CSV 方言的特定参数组。它可以是 Dialect 类的子类的实例，也可以是 list_dialects() 函数返回的字符串之一。另一个可选关键字参数 fmtparams 可以覆写当前方言格式中的单个格式设置。有关方言和格式设置参数的完整详细信息，请参见 变种与格式参数 部分。

csv 文件的每一行都读取为一个由字符串组成的列表。除非指定了 QUOTE_NONNUMERIC 格式选项（在这种情况下，未引用的字段会转换为浮点数），否则不会执行自动数据类型转换。

一个简短的用法示例:

>>> import csv
>>> with open('eggs.csv', newline='') as csvfile:
...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
...     for row in spamreader:
...         print(', '.join(row))
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam

csv.writer(csvfile, dialect='excel', **fmtparams)

返回一个 writer 对象，该对象负责将用户的数据在给定的文件类对象上转换为带分隔符的字符串。csvfile 可以是具有 write() 方法的任何对象。如果 csvfile 是文件对象，则打开它时应使用 newline=''。 1 可选参数 dialect 是用于不同的 CSV 方言的特定参数组。它可以是 Dialect 类的子类的实例，也可以是 list_dialects() 函数返回的字符串之一。另一个可选关键字参数 fmtparams 可以覆写当前方言格式中的单个格式设置。有关方言和格式设置参数的完整详细信息，请参见 变种与格式参数 部分。为了尽量简化与数据库 API 模块之间的对接，None 值会写入为空字符串。虽然这个转换是不可逆的，但它让 SQL 空数据值转储到 CSV 文件更容易，而无需预处理从 cursor.fetch* 调用返回的数据。写入前，所有非字符串数据都先用 str() 转化为字符串再写入。

一个简短的用法示例:

import csv
with open('eggs.csv', 'w', newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

csv.register_dialect(name[, dialect[, **fmtparams]])

将 name 与 dialect 关联起来。name 必须是字符串。要指定变种 (dialect)，可以给出 Dialect 的子类，或给出 fmtparams 关键字参数，或两者都给出，此时关键字参数会覆盖 dialect 参数。 有关变种和格式设置参数的完整详细信息，请参见 变种与格式参数 部分。

csv.unregister_dialect(name)

从变种注册表中删除 name 对应的变种。如果 name 不是已注册的变种名称，则抛出 Error 异常。

csv.get_dialect(name)

返回 name 对应的变种。如果 name 不是已注册的变种名称，则抛出 Error 异常。该函数返回的是不可变的 Dialect 对象。

csv.list_dialects()

返回所有已注册变种的名称。

csv.field_size_limit([new_limit])

返回解析器当前允许的最大字段大小。如果指定了 new_limit，则它将成为新的最大字段大小。

csv 模块定义了以下类：

class csv.DictReader(f, fieldnames=None, restkey=None, restval=None, dialect='excel', *args, **kwds)

创建一个对象，其操作类似于常规 reader 但会将每行中的信息映射到一个 OrderedDict，其中的键由可选的 fieldnames 形参给出。

fieldnames 形参是一个 sequence。 如果省略 fieldnames，则文件 f 第一行中的值将被用作字段名。 无论字段名是如何确定的，有序字典都将保留其原始顺序。

If a row has more fields than fieldnames, the remaining data is put in a list and stored with the fieldname specified by restkey (which defaults to None). If a non-blank row has fewer fields than fieldnames, the missing values are filled-in with None.

所有其他可选或关键字参数都传递给底层的 reader 实例。

在 3.6 版更改: 返回的行现在的类型是 OrderedDict。

一个简短的用法示例:

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese

>>> print(row)
OrderedDict([('first_name', 'John'), ('last_name', 'Cleese')])

class csv.DictWriter(f, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)

创建一个对象，该对象在操作上类似常规 writer，但能将字典映射到输出行。fieldnames 参数是由 key（键）组成的 序列，用于指定字典中的 value （值）的顺序，这些值会按指定顺序传递给 writerow() 方法并写入 f 文件。如果字典缺少 fieldnames 中的键，则可选参数 restval 用于指定要写入的值。如果传递给 writerow() 方法的字典的某些键在 fieldnames 中找不到，则可选参数 extrasaction 用于指定要执行的操作。如果将其设置为 'raise' (默认值)，则会引发 ValueError。 如果将其设置为 'ignore'，则字典中的其他键值将被忽略。 所有其他可选或关键字参数都传递给底层的 writer 实例。

Note that unlike the DictReader class, the fieldnames parameter of the DictWriter is not optional. Since Python’s dict objects are not ordered, there is not enough information available to deduce the order in which the row should be written to file f.

一个简短的用法示例:

import csv

with open('names.csv', 'w', newline='') as csvfile:
    fieldnames = ['first_name', 'last_name']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
    writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
    writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})

class csv.Dialect

Dialect 类是主要依赖于其属性的容器类，用于将定义好的参数传递给特定的 reader 或 writer 实例。

class csv.excel

excel 类定义了 Excel 生成的 CSV 文件的常规属性。它在变种注册表中的名称是 'excel'。

class csv.excel_tab

excel_tab 类定义了 Excel 生成的、制表符分隔的 CSV 文件的常规属性。它在变种注册表中的名称是 'excel-tab'。

class csv.unix_dialect

unix_dialect 类定义了在 UNIX 系统上生成的 CSV 文件的常规属性，即使用 '\n' 作为换行符，且所有字段都有引号包围。它在变种注册表中的名称是 'unix'。

3.2 新版功能.

class csv.Sniffer

Sniffer 类用于推断 CSV 文件的格式。

Sniffer 类提供了两个方法：

sniff(sample, delimiters=None)

分析给定的 sample 并返回一个 Dialect 子类，该子类中包含了分析出的格式参数。如果给出可选的 delimiters 参数，则该参数会被解释为字符串，该字符串包含了可能的有效分隔符。

has_header(sample)

分析示例文本（假定为 CSV 格式），如果第一行很可能是一系列列标题，则返回 True。

使用 Sniffer 的示例：

with open('example.csv', newline='') as csvfile:
    dialect = csv.Sniffer().sniff(csvfile.read(1024))
    csvfile.seek(0)
    reader = csv.reader(csvfile, dialect)
    # ... process CSV file contents here ...

csv 模块定义了以下常量：

csv.QUOTE_ALL

指示 writer 对象给所有字段加上引号。

csv.QUOTE_MINIMAL

指示 writer 对象仅为包含特殊字符（例如 定界符、引号字符 或 行结束符 中的任何字符）的字段加上引号。

csv.QUOTE_NONNUMERIC

指示 writer 对象为所有非数字字段加上引号。

指示 reader 将所有未用引号引出的字段转换为 float 类型。

csv.QUOTE_NONE

指示 writer 对象不使用引号引出字段。当 定界符 出现在输出数据中时，其前面应该有 转义符。如果未设置 转义符，则遇到任何需要转义的字符时，writer 都会抛出 Error 异常。

指示 reader 不对引号字符进行特殊处理。

csv 模块定义了以下异常：

exception csv.Error

该异常可能由任何发生错误的函数抛出。

14.1.2. 变种与格式参数

为了更容易指定输入和输出记录的格式，特定的一组格式参数组合为一个 dialect（变种）。一个 dialect 是一个 Dialect 类的子类，它具有一组特定的方法和一个 validate() 方法。创建 reader 或 writer 对象时，程序员可以将某个字符串或 Dialect 类的子类指定为 dialect 参数。要想补充或覆盖 dialect 参数，程序员还可以单独指定某些格式参数，这些参数的名称与下面 Dialect 类定义的属性相同。

Dialect 类支持以下属性：

Dialect.delimiter

一个用于分隔字段的单字符，默认为 ','。

Dialect.doublequote

控制出现在字段中的 引号字符 本身应如何被引出。当该属性为 True 时，双写引号字符。如果该属性为 False，则在 引号字符 的前面放置 转义符。默认值为 True。

在输出时，如果 doublequote 是 False，且 转义符 未指定，且在字段中发现 引号字符 时，会抛出 Error 异常。

Dialect.escapechar

一个用于 writer 的单字符，用来在 quoting 设置为 QUOTE_NONE 的情况下转义 定界符，在 doublequote 设置为 False 的情况下转义 引号字符。在读取时，escapechar 去除了其后所跟字符的任何特殊含义。该属性默认为 None，表示禁用转义。

Dialect.lineterminator

放在 writer 产生的行的结尾，默认为 '\r\n'。

注解

reader 经过硬编码，会识别 '\r' 或 '\n' 作为行尾，并忽略 lineterminator。未来可能会更改这一行为。

Dialect.quotechar

一个单字符，用于包住含有特殊字符的字段，特殊字符如 定界符 或 引号字符 或换行符。默认为 '"'。

Dialect.quoting

控制 writer 何时生成引号，以及 reader 何时识别引号。该属性可以等于任何 QUOTE_* 常量（参见 模块内容 段落），默认为 QUOTE_MINIMAL。

Dialect.skipinitialspace

如果为 True，则忽略 定界符 之后的空格。默认值为 False。

Dialect.strict

如果为 True，则在输入错误的 CSV 时抛出 Error 异常。默认值为 False。

14.1.3. Reader 对象

Reader 对象（DictReader 实例和 reader() 函数返回的对象）具有以下公开方法：

csvreader.__next__()

返回 reader 的可迭代对象的下一行，返回值可能是列表（由 reader() 返回的对象）或字典（由 DictReader 返回的对象），解析是根据当前设置的变种进行的。通常应该这样调用它：next(reader)。

Reader 对象具有以下公开属性：

csvreader.dialect

变种描述，只读，供解析器使用。

csvreader.line_num

源迭代器已经读取了的行数。它与返回的记录数不同，因为记录可能跨越多行。

DictReader 对象具有以下公开属性：

csvreader.fieldnames

字段名称。如果在创建对象时未传入字段名称，则首次访问时或从文件中读取第一条记录时会初始化此属性。

14.1.4. Writer 对象

Writer 对象（DictWriter 实例和 writer() 函数返回的对象）具有下面的公开方法。对于 Writer 对象，行 必须是（一组可迭代的）字符串或数字。对于 DictWriter 对象，行 必须是一个字典，这个字典将字段名映射为字符串或数字（数字要先经过 str() 转换类型）。请注意，输出的复数会有括号包围。这样其他程序读取 CSV 文件时可能会有一些问题（假设它们完全支持复数）。

csvwriter.writerow(row)

将 row 形参写入 writer 的文件对象，并按照当前设定形式进行格式化。

在 3.5 版更改: 开始支持任意类型的迭代器。

csvwriter.writerows(rows)

将 rows*（即能迭代出多个上述 *row 对象的迭代器）中的所有元素写入 writer 的文件对象，并根据当前设置的变种进行格式化。

Writer 对象具有以下公开属性：

csvwriter.dialect

变种描述，只读，供 writer 使用。

DictWriter 对象具有以下公开方法：

DictWriter.writeheader()

使用（构造器所规定的）字段名写入一行。

3.2 新版功能.

14.1.5. 例子

读取 CSV 文件最简单的一个例子:

import csv
with open('some.csv', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)

读取其他格式的文件:

import csv
with open('passwd', newline='') as f:
    reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
    for row in reader:
        print(row)

相应最简单的写入示例是:

import csv
with open('some.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(someiterable)

由于使用 open() 来读取 CSV 文件，因此默认情况下，将使用系统默认编码来解码文件并转换为 unicode（请参阅 locale.getpreferredencoding()）。要使用其他编码来解码文件，请使用 open 的 encoding 参数:

import csv
with open('some.csv', newline='', encoding='utf-8') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)

这同样适用于写入非系统默认编码的内容：打开输出文件时，指定 encoding 参数。

注册一个新的变种:

import csv
csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
with open('passwd', newline='') as f:
    reader = csv.reader(f, 'unixpwd')

Reader 的更高级用法——捕获并报告错误:

import csv, sys
filename = 'some.csv'
with open(filename, newline='') as f:
    reader = csv.reader(f)
    try:
        for row in reader:
            print(row)
    except csv.Error as e:
        sys.exit('file {}, line {}: {}'.format(filename, reader.line_num, e))

尽管该模块不直接支持解析字符串，但仍可如下轻松完成:

import csv
for row in csv.reader(['one,two,three']):
    print(row)

备注

1(1,2)

如果没有指定 newline=''，则嵌入引号中的换行符将无法正确解析，并且在写入时，使用 \r\n 换行的平台会有多余的 \r 写入。由于 csv 模块会执行自己的（通用）换行符处理，因此指定 newline='' 应该总是安全的。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

14. 文件格式 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

14. 文件格式 »

|

14.2. configparser — 配置文件解析器

源代码: Lib/configparser.py [https://github.com/python/cpython/tree/3.6/Lib/configparser.py]

此模块提供了它实现一种基本配置语言 ConfigParser 类，这种语言所提供的结构与 Microsoft Windows INI 文件的类似。 你可以使用这种语言来编写能够由最终用户来自定义的 Python 程序。

注解

这个库 并不 能够解析或写入在 Windows Registry 扩展版本 INI 语法中所使用的值-类型前缀。

参见

模块 shlex

支持创建可被用作应用配置文件的替代的 Unix 终端式微语言。

模块 json

json 模块实现了一个 JavaScript 语法的子集，它也可被用于这种目的。

14.2.1. 快速起步

让我们准备一个非常基本的配置文件，它看起来是这样的:

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardX11 = no

INI 文件的结构描述见 以下章节。 总的来说，这种文件由多个节组成，每个节包含多个带有值的键。 configparser 类可以读取和写入这种文件。 让我们先通过程序方式来创建上述的配置文件。

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config['DEFAULT'] = {'ServerAliveInterval': '45',
...                      'Compression': 'yes',
...                      'CompressionLevel': '9'}
>>> config['bitbucket.org'] = {}
>>> config['bitbucket.org']['User'] = 'hg'
>>> config['topsecret.server.com'] = {}
>>> topsecret = config['topsecret.server.com']
>>> topsecret['Port'] = '50022'     # mutates the parser
>>> topsecret['ForwardX11'] = 'no'  # same here
>>> config['DEFAULT']['ForwardX11'] = 'yes'
>>> with open('example.ini', 'w') as configfile:
...   config.write(configfile)
...

如你所见，我们可以把配置解析器当作一个字典来处理。 两者确实存在差异，将在后文说明，但是其行为非常接近于字典所具有一般行为。

现在我们已经创建并保存了一个配置文件，让我们再将它读取出来并探究其中包含的数据。

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config.read('example.ini')
['example.ini']
>>> config.sections()
['bitbucket.org', 'topsecret.server.com']
>>> 'bitbucket.org' in config
True
>>> 'bytebong.com' in config
False
>>> config['bitbucket.org']['User']
'hg'
>>> config['DEFAULT']['Compression']
'yes'
>>> topsecret = config['topsecret.server.com']
>>> topsecret['ForwardX11']
'no'
>>> topsecret['Port']
'50022'
>>> for key in config['bitbucket.org']: print(key)
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config['bitbucket.org']['ForwardX11']
'yes'

正如我们在上面所看到的，相关的 API 相当直观。 唯一有些神奇的地方是 DEFAULT 小节，它为所有其他小节提供了默认值 1。 还要注意小节中的键大小写不敏感并且会存储为小写形式 1。

14.2.2. 支持的数据类型

配置解析器并不会猜测配置文件中值的类型，而总是将它们在内部存储为字符串。 这意味着如果你需要其他数据类型，你应当自己来转换:

>>> int(topsecret['Port'])
50022
>>> float(topsecret['CompressionLevel'])
9.0

由于这种任务十分常用，配置解析器提供了一系列便捷的获取方法来处理整数、浮点数和布尔值。 最后一个类型的处理最为有趣，因为简单地将值传给 bool() 是没有用的，bool('False') 仍然会是 True。 为解决这个问题配置解析器还提供了 getboolean()。 这个方法对大小写不敏感并可识别 'yes'/'no', 'on'/'off', 'true'/'false' 和 '1'/'0' 1 等布尔值。 例如:

>>> topsecret.getboolean('ForwardX11')
False
>>> config['bitbucket.org'].getboolean('ForwardX11')
True
>>> config.getboolean('bitbucket.org', 'Compression')
True

除了 getboolean()，配置解析器还提供了同类的 getint() 和 getfloat() 方法。 你可以注册你自己的转换器并或是定制已提供的转换器。 1

14.2.3. 回退值

与字典类似，你可以使用某个小节的 get() 方法来提供回退值:

>>> topsecret.get('Port')
'50022'
>>> topsecret.get('CompressionLevel')
'9'
>>> topsecret.get('Cipher')
>>> topsecret.get('Cipher', '3des-cbc')
'3des-cbc'

请注意默认值会优先于回退值。 例如，在我们的示例中 'CompressionLevel' 键仅在 'DEFAULT' 小节被指定。 如果你尝试在 'topsecret.server.com' 小节获取它，我们将总是获取到默认值，即使我们指定了一个回退值:

>>> topsecret.get('CompressionLevel', '3')
'9'

还需要注意的一点是解析器层级的 get() 方法提供了自定义的更复杂接口，它被维护用于向下兼容。 当使用此方法时，回退值可以通过 fallback 仅限关键字参数来提供:

>>> config.get('bitbucket.org', 'monster',
...            fallback='No such things as monsters')
'No such things as monsters'

同样的 fallback 参数也可在 getint(), getfloat() 和 getboolean() 方法中使用，例如:

>>> 'BatchMode' in topsecret
False
>>> topsecret.getboolean('BatchMode', fallback=True)
True
>>> config['DEFAULT']['BatchMode'] = 'no'
>>> topsecret.getboolean('BatchMode', fallback=True)
False

14.2.4. 受支持的 INI 文件结构

配置文件是由小节组成的，每个小节都有一个 [section] 标头，加上多个由特定字符串 (默认为 = 或 : 1) 分隔的键/值条目。 默认情况下小节名对大小写敏感而键对大小写不敏感 1。 键和值开头和末尾的空格会被移除。 值可以被省略，在此情况下键/值分隔符也可以被省略。 值还可以跨越多行，只要其他行带有比值的第一行更深的缩进。 依据解析器的具体模式，空白行可能被视为多行值的组成部分也可能被忽略。

配置文件可以包含注释，要带有指定字符前缀 (默认为 # 和 ; 1)。 注释可以单独出现于原本的空白行，并可使用缩进。 1

例如:

[Simple Values]
key=value
spaces in keys=allowed
spaces in values=allowed as well
spaces around the delimiter = obviously
you can also use : to delimit keys from values

[All Values Are Strings]
values like this: 1000000
or this: 3.14159265359
are they treated as numbers? : no
integers, floats and booleans are held as: strings
can use the API to get converted values directly: true

[Multiline Values]
chorus: I'm a lumberjack, and I'm okay
    I sleep all night and I work all day

[No Values]
key_without_value
empty string value here =

[You can use comments]
# like this
; or this

# By default only in an empty line.
# Inline comments can be harmful because they prevent users
# from using the delimiting characters as parts of values.
# That being said, this can be customized.

    [Sections Can Be Indented]
        can_values_be_as_well = True
        does_that_mean_anything_special = False
        purpose = formatting for readability
        multiline_values = are
            handled just fine as
            long as they are indented
            deeper than the first line
            of a value
        # Did I mention we can indent comments, too?

14.2.5. 值的插入

在核心功能之上，ConfigParser 还支持插值。 这意味着值可以在被 get() 调用返回之前进行预处理。

class configparser.BasicInterpolation

默认实现由 ConfigParser 来使用。 它允许值包含引用了相同小节中其他值或者特殊的默认小节中的值的格式字符串 1。 额外的默认值可以在初始化时提供。

例如:

[Paths]
home_dir: /Users
my_dir: %(home_dir)s/lumberjack
my_pictures: %(my_dir)s/Pictures

在上面的例子里，ConfigParser 的 interpolation 设为 BasicInterpolation()，这会将 %(home_dir)s 求解为 home_dir 的值 (在这里是 /Users)。 %(my_dir)s 的将被实际求解为 /Users/lumberjack。 所有插值都是按需进行的，这样引用链中使用的键不必以任何特定顺序在配置文件中指明。

当 interpolation 设为 None 时，解析器会简单地返回 %(my_dir)s/Pictures 作为 my_pictures 的值，并返回 %(home_dir)s/lumberjack 作为 my_dir 的值。

class configparser.ExtendedInterpolation

一个用于插值的替代处理程序实现了更高级的语法，它被用于 zc.buildout 中的实例。 扩展插值使用 ${section:option} 来表示来自外部小节的值。 插值可以跨越多个层级。 为了方便使用，section: 部分可被省略，插值会默认作用于当前小节（可能会从特殊小节获取默认值）。

例如，上面使用基本插值描述的配置，使用扩展插值将是这个样子:

[Paths]
home_dir: /Users
my_dir: ${home_dir}/lumberjack
my_pictures: ${my_dir}/Pictures

来自其他小节的值也可以被获取:

[Common]
home_dir: /Users
library_dir: /Library
system_dir: /System
macports_dir: /opt/local

[Frameworks]
Python: 3.2
path: ${Common:system_dir}/Library/Frameworks/

[Arthur]
nickname: Two Sheds
last_name: Jackson
my_dir: ${Common:home_dir}/twosheds
my_pictures: ${my_dir}/Pictures
python_dir: ${Frameworks:path}/Python/Versions/${Frameworks:Python}

14.2.6. 映射协议访问

3.2 新版功能.

映射协议访问这个通用名称是指允许以字典的方式来使用自定义对象的功能。 在 configparser 中，映射接口的实现使用了 parser['section']['option'] 标记法。

parser['section'] 专门为解析器中的小节数据返回一个代理。 这意味着其中的值不会被拷贝，而是在需要时从原始解析器中获取。 更为重要的是，当值在小节代理上被修改时，它们其实是在原始解析器中发生了改变。

configparser 对象的行为会尽可能地接近真正的字典。 映射接口是完整而且遵循 MutableMapping ABC 规范的。 但是，还是有一些差异应当被纳入考虑:

默认情况下，小节中的所有键是以大小写不敏感的方式来访问的 1。 例如 for option in parser["section"] 只会产生 optionxform 形式的选项键名称。 也就是说默认使用小写字母键名。 与此同时，对于一个包含键 'a' 的小节，以下两个表达式均将返回 True:

"a" in parser["section"]
"A" in parser["section"]

All sections include DEFAULTSECT values as well which means that .clear() on a section may not leave the section visibly empty. This is because default values cannot be deleted from the section (because technically they are not there). If they are overridden in the section, deleting causes the default value to be visible again. Trying to delete a default value causes a KeyError.

DEFAULTSECT 无法从解析器中被移除:

trying to delete it raises ValueError,

parser.clear() 会保留其原状，

parser.popitem() 绝不会将其返回。

parser.get(section, option, **kwargs) - 第二个参数 并非 回退值。 但是请注意小节层级的 get() 方法可同时兼容映射协议和经典配置解析器 API。

parser.items() 兼容映射协议（返回 section_name, section_proxy 对的列表，包括 DEFAULTSECT）。 但是，此方法也可以带参数发起调用: parser.items(section, raw, vars)。 这种调用形式返回指定 section 的 option, value 对的列表，将展开所有插值（除非提供了 raw=True 选项）。

映射协议是在现有的传统 API 之上实现的，以便重载原始接口的子类仍然具有符合预期的有效映射。

14.2.7. 定制解析器行为

INI 格式的变种数量几乎和使用此格式的应用一样多。 configparser 花费了很大力气来为尽量大范围的可用 INI 样式提供支持。 默认的可用功能主要由历史状况来确定，你很可能会想要定制某些特性。

改变特定配置解析器行为的最常见方式是使用 __init__() 选项:

defaults，默认值: None

此选项接受一个键值对的字典，它将被首先放入 DEFAULT 小节。 这实现了一种优雅的方式来支持简洁的配置文件，它不必指定与已记录的默认值相同的值。

提示：如果你想要为特定的小节指定默认的值，请在读取实际文件之前使用 read_dict()。

dict_type，默认值: collections.OrderedDict

此选项主要影响映射协议的行为和写入配置文件的外观。 使用默认的有序字典时，每个小节是按照它们被加入解析器的顺序保存的。 在小节内的选项也是如此。

还有其他替换的字典类型可用，例如在写回数据时对小节和选项进行排序。 你也可以出于性能原因而使用普通字典。

Please note: there are ways to add a set of key-value pairs in a single operation. When you use a regular dictionary in those operations, the order of the keys may be random. For example:

>>> parser = configparser.ConfigParser()
>>> parser.read_dict({'section1': {'key1': 'value1',
...                                'key2': 'value2',
...                                'key3': 'value3'},
...                   'section2': {'keyA': 'valueA',
...                                'keyB': 'valueB',
...                                'keyC': 'valueC'},
...                   'section3': {'foo': 'x',
...                                'bar': 'y',
...                                'baz': 'z'}
... })
>>> parser.sections()
['section3', 'section2', 'section1']
>>> [option for option in parser['section3']]
['baz', 'foo', 'bar']

In these operations you need to use an ordered dictionary as well:

>>> from collections import OrderedDict
>>> parser = configparser.ConfigParser()
>>> parser.read_dict(
...   OrderedDict((
...     ('s1',
...      OrderedDict((
...        ('1', '2'),
...        ('3', '4'),
...        ('5', '6'),
...      ))
...     ),
...     ('s2',
...      OrderedDict((
...        ('a', 'b'),
...        ('c', 'd'),
...        ('e', 'f'),
...      ))
...     ),
...   ))
... )
>>> parser.sections()
['s1', 's2']
>>> [option for option in parser['s1']]
['1', '3', '5']
>>> [option for option in parser['s2'].values()]
['b', 'd', 'f']

allow_no_value，默认值: False

已知某些配置文件会包括不带值的设置，但其在其他方面均符合 configparser 所支持的语法。 构造器的 allow_no_value 形参可用于指明应当接受这样的值:

>>> import configparser

>>> sample_config = """
... [mysqld]
...   user = mysql
...   pid-file = /var/run/mysqld/mysqld.pid
...   skip-external-locking
...   old_passwords = 1
...   skip-bdb
...   # we don't need ACID today
...   skip-innodb
... """
>>> config = configparser.ConfigParser(allow_no_value=True)
>>> config.read_string(sample_config)

>>> # Settings with values are treated as before:
>>> config["mysqld"]["user"]
'mysql'

>>> # Settings without values provide None:
>>> config["mysqld"]["skip-bdb"]

>>> # Settings which aren't specified still raise an error:
>>> config["mysqld"]["does-not-exist"]
Traceback (most recent call last):
  ...
KeyError: 'does-not-exist'

delimiters，默认值: ('=', ':')

分隔符是用于在小节内分隔键和值的子字符串。 在一行中首次出现的分隔子字符串会被视为一个分隔符。 这意味着值可以包含分隔符（但键不可以）。

另请参见 ConfigParser.write() 的 space_around_delimiters 参数。

comment_prefixes，默认值: ('#', ';')

inline_comment_prefixes，默认值: None

注释前缀是配置文件中用于标示一条有效注释的开头的字符串。 comment_prefixes 仅用在被视为空白的行（可以缩进）之前而 inline_comment_prefixes 可用在每个有效值之后（例如小节名称、选项以及空白的行）。 默认情况下禁用行内注释，并且 '#' 和 ';' 都被用作完整行注释的前缀。

在 3.2 版更改: 在之前的 configparser 版本中行为匹配 comment_prefixes=('#',';') 和 inline_comment_prefixes=(';',)。

请注意配置解析器不支持对注释前缀的转义，因此使用 inline_comment_prefixes 可能妨碍用户将被用作注释前缀的字符指定为可选值。 当有疑问时，请避免设置 inline_comment_prefixes。 在许多情况下，在多行值的一行开头存储注释前缀字符的唯一方式是进行前缀插值，例如:

>>> from configparser import ConfigParser, ExtendedInterpolation
>>> parser = ConfigParser(interpolation=ExtendedInterpolation())
>>> # the default BasicInterpolation could be used as well
>>> parser.read_string("""
... [DEFAULT]
... hash = #
...
... [hashes]
... shebang =
...   ${hash}!/usr/bin/env python
...   ${hash} -*- coding: utf-8 -*-
...
... extensions =
...   enabled_extension
...   another_extension
...   #disabled_by_comment
...   yet_another_extension
...
... interpolation not necessary = if # is not at line start
... even in multiline values = line #1
...   line #2
...   line #3
... """)
>>> print(parser['hashes']['shebang'])

#!/usr/bin/env python
# -*- coding: utf-8 -*-
>>> print(parser['hashes']['extensions'])

enabled_extension
another_extension
yet_another_extension
>>> print(parser['hashes']['interpolation not necessary'])
if # is not at line start
>>> print(parser['hashes']['even in multiline values'])
line #1
line #2
line #3

strict，默认值: True

当设为 True 时，解析器在从单一源读取 (使用 read_file(), read_string() 或 read_dict()) 期间将不允许任何小节或选项出现重复。 推荐在新的应用中使用严格解析器。

在 3.2 版更改: 在之前的 configparser 版本中行为匹配 strict=False。

empty_lines_in_values，默认值: True

在配置解析器中，值可以包含多行，只要它们的缩进级别低于它们所对应的键。 默认情况下解析器还会将空行视为值的一部分。 于此同时，键本身也可以任意缩进以提升可读性。 因此，当配置文件变得非常庞大而复杂时，用户很容易失去对文件结构的掌控。 例如:

[Section]
key = multiline
  value with a gotcha

 this = is still a part of the multiline value of 'key'

在用户查看时这可能会特别有问题，如果她是使用比例字体来编辑文件的话。 这就是为什么当你的应用不需要带有空行的值时，你应该考虑禁用它们。 这将使得空行每次都会作为键之间的分隔。 在上面的示例中，空行产生了两个键，key 和 this。

default_section，默认值: configparser.DEFAULTSECT (即: "DEFAULT")

允许设置一个保存默认值的特殊节在其他节或插值等目的中使用的惯例是这个库所拥有的一个强大概念，使得用户能够创建复杂的声明性配置。 这种特殊节通常称为 "DEFAULT" 但也可以被定制为指向任何其他有效的节名称。 一些典型的值包括: "general" 或 "common"。 所提供的名称在从任意节读取的时候被用于识别默认的节，而且也会在将配置写回文件时被使用。 它的当前值可以使用 parser_instance.default_section 属性来获取，并且可以在运行时被修改（即将文件从一种格式转换为另一种格式）。

interpolation，默认值: configparser.BasicInterpolation

插值行为可以用通过提供 interpolation 参数提供自定义处理程序的方式来定制。 None 可用来完全禁用插值，ExtendedInterpolation() 提供了一种更高级的变体形式，它的设计受到了 zc.buildout 的启发。 有关该主题的更多信息请参见 专门的文档章节。 RawConfigParser 具有默认的值 None。

converters，默认值: 不设置

配置解析器提供了可选的值获取方法用来执行类型转换。 默认实现包括 getint(), getfloat() 以及 getboolean()。 如果还需要其他获取方法，用户可以在子类中定义它们，或者传入一个字典，其中每个键都是一个转换器的名称而每个值都是一个实现了特定转换的可调用对象。 例如，传入 {'decimal': decimal.Decimal} 将对解释器对象和所有节代理添加 getdecimal()。 换句话说，可以同时编写 parser_instance.getdecimal('section', 'key', fallback=0) 和 parser_instance['section'].getdecimal('key', 0)。

如果转换器需要访问解析器的状态，可以在配置解析器子类上作为一个方法来实现。 如果该方法的名称是以 get 打头的，它将在所有节代理上以兼容字典的形式提供（参见上面的 getdecimal() 示例）。

更多高级定制选项可通过重载这些解析器属性的默认值来达成。 默认值是在类中定义的，因此它们可以通过子类或属性赋值来重载。

ConfigParser.BOOLEAN_STATES

默认情况下当使用 getboolean() 时，配置解析器会将下列值视为 True: '1', 'yes', 'true', 'on' 而将下列值视为 False: '0', 'no', 'false', 'off'。 你可以通过指定一个自定义的字符串键及其对应的布尔值字典来重载此行为。 例如:

>>> custom = configparser.ConfigParser()
>>> custom['section1'] = {'funky': 'nope'}
>>> custom['section1'].getboolean('funky')
Traceback (most recent call last):
...
ValueError: Not a boolean: nope
>>> custom.BOOLEAN_STATES = {'sure': True, 'nope': False}
>>> custom['section1'].getboolean('funky')
False

其他典型的布尔值对包括 accept/reject 或 enabled/disabled。

ConfigParser.optionxform(option)

这个方法会转换每次 read, get, 或 set 操作的选项名称。 默认会将名称转换为小写形式。 这也意味着当一个配置文件被写入时，所有键都将为小写形式。 如果此行为不合适则要重载此方法。 例如:

>>> config = """
... [Section1]
... Key = Value
...
... [Section2]
... AnotherKey = Value
... """
>>> typical = configparser.ConfigParser()
>>> typical.read_string(config)
>>> list(typical['Section1'].keys())
['key']
>>> list(typical['Section2'].keys())
['anotherkey']
>>> custom = configparser.RawConfigParser()
>>> custom.optionxform = lambda option: option
>>> custom.read_string(config)
>>> list(custom['Section1'].keys())
['Key']
>>> list(custom['Section2'].keys())
['AnotherKey']

ConfigParser.SECTCRE

一个已编译正则表达式会被用来解析节标头。 默认将 [section] 匹配到名称 "section"。 空格会被视为节名称的一部分，因此 [  larch  ] 将被读取为一个名称为 "  larch  " 的节。 如果此行为不合适则要重载此属性。 例如:

>>> config = """
... [Section 1]
... option = value
...
... [  Section 2  ]
... another = val
... """
>>> typical = ConfigParser()
>>> typical.read_string(config)
>>> typical.sections()
['Section 1', '  Section 2  ']
>>> custom = ConfigParser()
>>> custom.SECTCRE = re.compile(r"\[ *(?P<header>[^]]+?) *\]")
>>> custom.read_string(config)
>>> custom.sections()
['Section 1', 'Section 2']

注解

虽然 ConfigParser 对象也使用 OPTCRE 属性来识别选项行，但并不推荐重载它，因为这会与构造器选项 allow_no_value 和 delimiters 产生冲突。

14.2.8. 旧式 API 示例

主要出于向下兼容性的考虑，configparser 还提供了一种采用显式 get/set 方法的旧式 API。 虽然以下介绍的方法存在有效的用例，但对于新项目仍建议采用映射协议访问。 旧式 API 在多数时候都更复杂、更底层并且完全违反直觉。

一个写入配置文件的示例:

import configparser

config = configparser.RawConfigParser()

# Please note that using RawConfigParser's set functions, you can assign
# non-string values to keys internally, but will receive an error when
# attempting to write to a file or when you get it in non-raw mode. Setting
# values using the mapping protocol or ConfigParser's set() does not allow
# such assignments to take place.
config.add_section('Section1')
config.set('Section1', 'an_int', '15')
config.set('Section1', 'a_bool', 'true')
config.set('Section1', 'a_float', '3.1415')
config.set('Section1', 'baz', 'fun')
config.set('Section1', 'bar', 'Python')
config.set('Section1', 'foo', '%(bar)s is %(baz)s!')

# Writing our configuration file to 'example.cfg'
with open('example.cfg', 'w') as configfile:
    config.write(configfile)

一个再次读取配置文件的示例:

import configparser

config = configparser.RawConfigParser()
config.read('example.cfg')

# getfloat() raises an exception if the value is not a float
# getint() and getboolean() also do this for their respective types
a_float = config.getfloat('Section1', 'a_float')
an_int = config.getint('Section1', 'an_int')
print(a_float + an_int)

# Notice that the next output does not interpolate '%(bar)s' or '%(baz)s'.
# This is because we are using a RawConfigParser().
if config.getboolean('Section1', 'a_bool'):
    print(config.get('Section1', 'foo'))

要获取插值，请使用 ConfigParser:

import configparser

cfg = configparser.ConfigParser()
cfg.read('example.cfg')

# Set the optional *raw* argument of get() to True if you wish to disable
# interpolation in a single get operation.
print(cfg.get('Section1', 'foo', raw=False))  # -> "Python is fun!"
print(cfg.get('Section1', 'foo', raw=True))   # -> "%(bar)s is %(baz)s!"

# The optional *vars* argument is a dict with members that will take
# precedence in interpolation.
print(cfg.get('Section1', 'foo', vars={'bar': 'Documentation',
                                       'baz': 'evil'}))

# The optional *fallback* argument can be used to provide a fallback value
print(cfg.get('Section1', 'foo'))
      # -> "Python is fun!"

print(cfg.get('Section1', 'foo', fallback='Monty is not.'))
      # -> "Python is fun!"

print(cfg.get('Section1', 'monster', fallback='No such things as monsters.'))
      # -> "No such things as monsters."

# A bare print(cfg.get('Section1', 'monster')) would raise NoOptionError
# but we can also use:

print(cfg.get('Section1', 'monster', fallback=None))
      # -> None

默认值在两种类型的 ConfigParser 中均可用。 它们将在当某个选项未在别处定义时被用于插值。

import configparser

# New instance with 'bar' and 'baz' defaulting to 'Life' and 'hard' each
config = configparser.ConfigParser({'bar': 'Life', 'baz': 'hard'})
config.read('example.cfg')

print(config.get('Section1', 'foo'))     # -> "Python is fun!"
config.remove_option('Section1', 'bar')
config.remove_option('Section1', 'baz')
print(config.get('Section1', 'foo'))     # -> "Life is hard!"

14.2.9. ConfigParser 对象

class configparser.ConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=configparser.DEFAULTSECT, interpolation=BasicInterpolation(), converters={})

主配置解析器。 当给定 defaults 时，它会被初始化为包含固有默认值的字典。 当给定 dict_type 时，它将被用来创建包含节、节中的选项以及默认值的字典。

当给定 delimiters 时，它会被用作分隔键与值的子字符串的集合。 当给定 comment_prefixes 时，它将被用作在否则为空行的注释的前缀子字符串的集合。 注释可以被缩进。 当给定 inline_comment_prefixes 时，它将被用作非空行的注释的前缀子字符串的集合。

当 strict 为 True (默认值) 时，解析器在从单个源（文件、字符串或字典）读取时将不允许任何节或选项出现重复，否则会引发 DuplicateSectionError 或 DuplicateOptionError。 当 empty_lines_in_values 为 False (默认值: True) 时，每个空行均表示一个选项的结束。 在其他情况下，一个多行选项内部的空行会被保留为值的一部分。 当 allow_no_value 为 True (默认值: False) 时，将接受没有值的选项；此种选项的值将为 None 并且它们会以不带末尾分隔符的形式被序列化。

当给定 default_section 时，它将指定存放其他节的默认值和用于插值的特殊节的名称 (通常命名为 "DEFAULT")。 该值可通过使用 default_section 实例属性在运行时被读取或修改。

插值行为可通过给出 interpolation 参数提供自定义处理程序的方式来定制。 None 可用来完全禁用插值，ExtendedInterpolation() 提供了一种更高级的变体形式，它的设计受到了 zc.buildout 的启发。 有关该主题的更多信息请参见 专门的文档章节。

插值中使用的所有选项名称将像任何其他选项名称引用一样通过 optionxform() 方法来传递。 例如，使用 optionxform() 的默认实现（它会将选项名称转换为小写形式）时，值 foo %(bar)s 和 foo %(BAR)s 是等价的。

当给定 converters 时，它应当为一个字典，其中每个键代表一个类型转换器的名称而每个值则为实现从字符串到目标数据类型的转换的可调用对象。 每个转换器会获得其在解析器对象和节代理上对应的 get*() 方法。

在 3.1 版更改: 默认的 dict_type 为 collections.OrderedDict。

在 3.2 版更改: 添加了 allow_no_value, delimiters, comment_prefixes, strict, empty_lines_in_values, default_section 以及 interpolation。

在 3.5 版更改: 添加了 converters 参数。

defaults()

返回包含实例范围内默认值的字典。

sections()

返回可用节的列表；default section 不包括在该列表中。

add_section(section)

向实例添加一个名为 section 的节。 如果给定名称的节已存在，将会引发 DuplicateSectionError。 如果转入了 default section 名称，则会引发 ValueError。 节名称必须为字符串；如果不是则会引发 TypeError。

在 3.2 版更改: 非字符串的节名称将引发 TypeError。

has_section(section)

指明相应名称的 section 是否存在于配置中。 default section 不包含在内。

options(section)

返回指定 section 中可用选项的列表。

has_option(section, option)

如果给定的 section 存在并且包含给定的 option 则返回 True；否则返回 False。 如果指定的 section 为 None 或空字符串，则会使用 DEFAULT。

read(filenames, encoding=None)

尝试读取并解析一个包含文件名的可迭代对象，返回一个被成功解析的文件名列表。

If filenames is a string or path-like object, it is treated as a single filename. If a file named in filenames cannot be opened, that file will be ignored. This is designed so that you can specify an iterable of potential configuration file locations (for example, the current directory, the user’s home directory, and some system-wide directory), and all existing configuration files in the iterable will be read.

如果名称对应的文件全都不存在，则 ConfigParser 实例将包含一个空数据集。 一个要求从文件加载初始值的应用应当在调用 read() 来获取任何可选文件之前使用 read_file() 来加载所要求的一个或多个文件:

import configparser, os

config = configparser.ConfigParser()
config.read_file(open('defaults.cfg'))
config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')],
            encoding='cp1250')

3.2 新版功能: encoding 形参。 在之前的版本中，所有文件都将使用 open() 的默认编码格式来读取。

3.6.1 新版功能: filenames 形参接受一个 path-like object。

read_file(f, source=None)

从 f 读取并解析配置数据，它必须是一个产生 Unicode 字符串的可迭代对象（例如以文本模式打开的文件）。

可选参数 source 指定要读取的文件名称。 如果未给出并且 f 具有 name 属性，则该属性会被用作 source；默认值为 '<???>'。

3.2 新版功能: 替代 readfp()。

read_string(string, source='<string>')

从字符串中解析配置数据。

可选参数 source 指定一个所传入字符串的上下文专属名称。 如果未给出，则会使用 '<string>'。 这通常应为一个文件系统路径或 URL。

3.2 新版功能.

read_dict(dictionary, source='<dict>')

从任意一个提供了类似于字典的 items() 方法的对象加载配置。 键为节名称，值为包含节中所出现的键和值的字典。 如果所用的字典类型会保留顺序，则节和其中的键将按顺序加入。 值会被自动转换为字符串。

可选参数 source 指定一个所传入字曲的上下文专属名称。 如果未给出，则会使用 <dict>。

此方法可被用于在解析器之间拷贝状态。

3.2 新版功能.

get(section, option, *, raw=False, vars=None[, fallback])

获取指定名称的 section 的一个 option 的值。 如果提供了 vars，则它必须为一个字典。 option 的查找顺序为 vars*（如果有提供）、*section 以及 DEFAULTSECT。 如果未找到该键并且提供了 fallback，则它会被用作回退值。 可以提供 None 作为 fallback 值。

所有 '%' 插值会在返回值中被展开，除非 raw 参数为真值。 插值键所使用的值会按与选项相同的方式来查找。

在 3.2 版更改: raw, vars 和 fallback 都是仅限关键字参数，以防止用户试图使用第三个参数作业为 fallback 回退值（特别是在使用映射 协议的时候）。

getint(section, option, *, raw=False, vars=None[, fallback])

将在 section 中指定的 option 强制转换为整数的便捷方法。 参见 get() 获取对于 raw, vars 和 fallback 的解释。

getfloat(section, option, *, raw=False, vars=None[, fallback])

将在 section 中指定的 option 强制转换为浮点数的便捷方法。 参见 get() 获取对于 raw, vars 和 fallback 的解释。

getboolean(section, option, *, raw=False, vars=None[, fallback])

将在指定 section 中的 option 强制转换为布尔值的便捷方法。 请注意选项所接受的值为 '1', 'yes', 'true' 和 'on'，它们会使得此方法返回 True，以及 '0', 'no', 'false' 和 'off'，它们会使得此方法返回 False。 这些字符串值会以对大小写不敏感的方式被检测。 任何其他值都将导致引发 ValueError。 参见 get() 获取对于 raw, vars 和 fallback 的解释。

items(raw=False, vars=None)

items(section, raw=False, vars=None)

当未给出 section 时，将返回由 section_name, section_proxy 对组成的列表，包括 DEFAULTSECT。

在其他情况下，将返回给定的 section 中的 option 的 name, value 对组成的列表。 可选参数具有与 get() 方法的参数相同的含义。

set(section, option, value)

如果给定的节存在，则将所给出的选项设为指定的值；在其他情况下将引发 NoSectionError。 option 和 value 必须为字符串；如果不是则将引发 TypeError。

write(fileobject, space_around_delimiters=True)

将配置的表示形式写入指定的 file object，该对象必须以文本模式打开（接受字符串）。 此表示形式可由将来的 read() 调用进行解析。 如果 space_around_delimiters 为真值，键和值之前的分隔符两边将加上空格。

remove_option(section, option)

将指定的 option 从指定的 section 中移除。 如果指定的节不存在则会引发 NoSectionError。 如果要移除的选项存在则返回 True；在其他情况下将返回 False。

remove_section(section)

从配置中移除指定的 section。 如果指定的节确实存在则返回 True。 在其他情况下将返回 False。

optionxform(option)

将选项名 option 转换为输入文件中的形式或客户端代码所传入的应当在内部结构中使用的形式。 默认实现将返回 option 的小写形式版本；子类可以重载此行为，或者客户端代码也可以在实例上设置一个具有此名称的属性来影响此行为。

你不需要子类化解析器来使用此方法，你也可以在一个实例上设置它，或使用一个接受字符串参数并返回字符串的函数。 例如将它设为 str 将使得选项名称变得大小写敏感:

cfgparser = ConfigParser()
cfgparser.optionxform = str

请注意当读取配置文件时，选项名称两边的空格将在调用 optionxform() 之前被去除。

readfp(fp, filename=None)

3.2 版后已移除: 使用 read_file() 来代替。

在 3.2 版更改: readfp() 现在将在 fp 上执行迭代而不是调用 fp.readline()。

对于调用 readfp() 时传入不支持迭代的参数的现有代码，可以在文件类对象外使用以下生成器作为包装器:

def readline_generator(fp):
    line = fp.readline()
    while line:
        yield line
        line = fp.readline()

不再使用 parser.readfp(fp) 而是改用 parser.read_file(readline_generator(fp))。

configparser.MAX_INTERPOLATION_DEPTH

当 raw 形参为假值时 get() 所采用的递归插值的最大深度。 这只在使用默认的 interpolation 时会起作用。

14.2.10. RawConfigParser 对象

class configparser.RawConfigParser(defaults=None, dict_type=collections.OrderedDict, allow_no_value=False, *, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section=configparser.DEFAULTSECT[, interpolation])

Legacy variant of the ConfigParser with interpolation disabled by default and unsafe add_section and set methods.

注解

考虑改用 ConfigParser，它会检查内部保存的值的类型。 如果你不想要插值，你可以使用 ConfigParser(interpolation=None)。

add_section(section)

向实例添加一个名为 section 的节。 如果给定名称的节已存在，将会引发 DuplicateSectionError。 如果传入了 default section 名称，则会引发 ValueError。

不检查 section 以允许用户创建以非字符串命名的节。 此行为已不受支持并可能导致内部错误。

set(section, option, value)

如果给定的节存在，则将给定的选项设为指定的值；在其他情况下将引发 NoSectionError。 虽然可能使用 RawConfigParser (或使用 ConfigParser 并将 raw 形参设为真值) 以便实现非字符串值的 internal 存储，但是完整功能（包括插值和输出到文件）只能使用字符串值来实现。

此方法允许用户在内部将非字符串值赋给键。 此行为已不受支持并会在尝试写入到文件或在非原始模式下获取数据时导致错误。 请使用映射协议 API，它不允许出现这样的赋值。

14.2.11. 异常

exception configparser.Error

所有其他 configparser 异常的基类。

exception configparser.NoSectionError

当找不到指定节时引发的异常。

exception configparser.DuplicateSectionError

当调用 add_section() 时传入已存在的节名称，或者在严格解析器中当单个输入文件、字符串或字典内出现重复的节时引发的异常。

3.2 新版功能: 将可选的 source 和 lineno 属性和参数添加到 __init__()。

exception configparser.DuplicateOptionError

当单个选项在从单个文件、字符串或字典读取时出现两次时引发的异常。 这会捕获拼写错误和大小写敏感相关的错误，例如一个字典可能包含两个键分别代表同一个大小写不敏感的配置键。

exception configparser.NoOptionError

当指定的选项未在指定的节中被找到时引发的异常。

exception configparser.InterpolationError

当执行字符串插值发生问题时所引发的异常的基类。

exception configparser.InterpolationDepthError

当字符串插值由于迭代次数超出 MAX_INTERPOLATION_DEPTH 而无法完成所引发的异常。 为 InterpolationError 的子类。

exception configparser.InterpolationMissingOptionError

当从某个值引用的选项并不存在时引发的异常。 为 InterpolationError 的子类。

exception configparser.InterpolationSyntaxError

当将要执行替换的源文本不符合要求的语法时引发的异常。 为 InterpolationError 的子类。

exception configparser.MissingSectionHeaderError

当尝试解析一个不带节标头的文件时引发的异常。

exception configparser.ParsingError

当尝试解析一个文件而发生错误时引发的异常。

在 3.2 版更改: filename 属性和 __init__() 参数被重命名为 source 以保持一致性。

备注

1(1,2,3,4,5,6,7,8,9,10)

配置解析器允许重度定制。 如果你有兴趣改变脚注说明中所介绍的行为，请参阅 Customizing Parser Behaviour 一节。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

14. 文件格式 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

14. 文件格式 »

|

14.3. netrc — netrc 文件处理

源代码: Lib/netrc.py [https://github.com/python/cpython/tree/3.6/Lib/netrc.py]

netrc 类解析并封装了 Unix 的 ftp 程序和其他 FTP 客户端所使用的 netrc 文件格式。

class netrc.netrc([file])

A netrc instance or subclass instance encapsulates data from a netrc file. The initialization argument, if present, specifies the file to parse. If no argument is given, the file .netrc in the user’s home directory will be read. Parse errors will raise NetrcParseError with diagnostic information including the file name, line number, and terminating token. If no argument is specified on a POSIX system, the presence of passwords in the .netrc file will raise a NetrcParseError if the file ownership or permissions are insecure (owned by a user other than the user running the process, or accessible for read or write by any other user). This implements security behavior equivalent to that of ftp and other programs that use .netrc.

在 3.4 版更改: 添加了 POSIX 权限检查。

exception netrc.NetrcParseError

当在源文本中遇到语法错误时由 netrc 类引发的异常。 此异常的实例提供了三个有用属性: msg 为错误的文本说明，filename 为源文件的名称，而 lineno 给出了错误所在的行号。

14.3.1. netrc 对象

netrc 实例具有下列方法:

netrc.authenticators(host)

针对 host 的身份验证者返回一个 3 元组 (login, account, password)。 如果 netrc 文件不包含针对给定主机的条目，则返回关联到 ‘default’ 条目的元组。 如果匹配的主机或默认条目均不可用，则返回 None。

netrc.__repr__()

将类数据以 netrc 文件的格式转储为一个字符串。 （这会丢弃注释并可能重排条目顺序。）

netrc 的实例具有一些公共实例变量:

netrc.hosts

将主机名映射到 (login, account, password) 元组的字典。 如果存在 ‘default’ 条目，则会表示为使用该名称的伪主机。

netrc.macros

将宏名称映射到字符串列表的字典。

注解

密码会被限制为 ASCII 字符集的一个子集。 所有 ASCII 标点符号均可用作密码，但是要注意空白符和非打印字符不允许用作密码。 这是 .netrc 文件解析方式带来的限制，在未来可能会被解除。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

14. 文件格式 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

14. 文件格式 »

|

14.4. xdrlib — 编码与解码 XDR 数据

源代码: Lib/xdrlib.py [https://github.com/python/cpython/tree/3.6/Lib/xdrlib.py]

xdrlib 模块为外部数据表示标准提供支持，该标准的描述见 RFC 1014 [https://tools.ietf.org/html/rfc1014.html]，由 Sun Microsystems, Inc. 在 1987 年 6 月撰写。 它支持该 RFC 中描述的大部分数据类型。

xdrlib 模块定义了两个类，一个用于将变量打包为 XDR 表示形式，另一个用于从 XDR 表示形式解包。 此外还有两个异常类。

class xdrlib.Packer

Packer 是用于将数据打包为 XDR 表示形式的类。 Packer 类的实例化不附带参数。

class xdrlib.Unpacker(data)

Unpacker 是用于相应地从字符串缓冲区解包 XDR 数据值的类。 输入缓冲区将作为 data 给出。

参见

RFC 1014 [https://tools.ietf.org/html/rfc1014.html] - XDR: 外部数据表示标准

这个 RFC 定义了最初编写此模块时 XDR 所用的数据编码格式。 显然它已被 RFC 1832 [https://tools.ietf.org/html/rfc1832.html] 所淘汰。

RFC 1832 [https://tools.ietf.org/html/rfc1832.html] - XDR: 外部数据表示标准

更新的 RFC，它提供了经修订的 XDR 定义。

14.4.1. Packer 对象

Packer 实例具有下列方法:

Packer.get_buffer()

将当前打包缓冲区以字符串的形式返回。

Packer.reset()

将打包缓冲区重置为空字符串。

总体来说，你可以通过调用适当的 pack_type() 方法来打包任何最常见的 XDR 数据类型。 每个方法都是接受单个参数，即要打包的值。 受支持的简单数据类型打包方法如下: pack_uint(), pack_int(), pack_enum(), pack_bool(), pack_uhyper() 以及 pack_hyper()。

Packer.pack_float(value)

打包单精度浮点数 value。

Packer.pack_double(value)

打包双精度浮点数 value。

以下方法支持打包字符串、字节串以及不透明数据。

Packer.pack_fstring(n, s)

打包固定长度字符串 s。 n 为字符串的长度，但它 不会 被打包进数据缓冲区。 如有必要字符串会以空字节串填充以保证 4 字节对齐。

Packer.pack_fopaque(n, data)

打包固定长度不透明数据流，类似于 pack_fstring()。

Packer.pack_string(s)

打包可变长度字符串 s。 先将字符串的长度打包为无符号整数，再用 pack_fstring() 来打包字符串数据。

Packer.pack_opaque(data)

打包可变长度不透明数据流，类似于 pack_string()。

Packer.pack_bytes(bytes)

打包可变长度字节流，类似于 pack_string()。

下列方法支持打包数组和列表:

Packer.pack_list(list, pack_item)

打包由同质条目构成的 list。 此方法适用于不确定长度的列表；即其长度无法在遍历整个列表之前获知。 对于列表中的每个条目，先打包一个无符号整数 1，再添加列表中数据的值。 pack_item 是在打包单个条目时要调用的函数。 在列表的末尾，会再打包一个无符号整数 0.

例如，要打包一个整数列表，代码看起来会是这样:

import xdrlib
p = xdrlib.Packer()
p.pack_list([1, 2, 3], p.pack_int)

Packer.pack_farray(n, array, pack_item)

打包由同质条目构成的固定长度列表 (array)。 n 为列表长度；它 不会 被打包到缓冲区，但是如果 len(array) 不等于 n 则会引发 ValueError。 如上所述，pack_item 是在打包每个元素时要使用的函数。

Packer.pack_array(list, pack_item)

打包由同质条目构成的可变长度 list。 先将列表的长度打包为无符号整数，再像上面的 pack_farray() 一样打包每个元素。

14.4.2. Unpacker 对象

Unpacker 类提供以下方法:

Unpacker.reset(data)

使用给定的 data 重置字符串缓冲区。

Unpacker.get_position()

返回数据缓冲区中的当前解包位置。

Unpacker.set_position(position)

将数据缓冲区的解包位置设为 position。 你应当小心使用 get_position() 和 set_position()。

Unpacker.get_buffer()

将当前解包数据缓冲区以字符串的形式返回。

Unpacker.done()

表明解包完成。 如果数据没有全部完成解包则会引发 Error 异常。

此外，每种可通过 Packer 打包的数据类型都可通过 Unpacker 来解包。 解包方法的形式为 unpack_type()，并且不接受任何参数。 该方法将返回解包后的对象。

Unpacker.unpack_float()

解包单精度浮点数。

Unpacker.unpack_double()

解包双精度浮点数，类似于 unpack_float()。

此外，以下方法可用来解包字符串、字节串以及不透明数据:

Unpacker.unpack_fstring(n)

解包并返回固定长度字符串。 n 为期望的字符数量。 会预设以空字节串填充以保证 4 字节对齐。

Unpacker.unpack_fopaque(n)

解包并返回固定长度数据流，类似于 unpack_fstring()。

Unpacker.unpack_string()

解包并返回可变长度字符串。 先将字符串的长度解包为无符号整数，再用 unpack_fstring() 来解包字符串数据。

Unpacker.unpack_opaque()

解包并返回可变长度不透明数据流，类似于 unpack_string()。

Unpacker.unpack_bytes()

解包并返回可变长度字节流，类似于 unpack_string()。

下列方法支持解包数组和列表:

Unpacker.unpack_list(unpack_item)

解包并返回同质条目的列表。 该列表每次解包一个元素，先解包一个无符号整数旗标。 如果旗标为 1，则解包条目并将其添加到列表。 旗标为 0 表明列表结束。 unpack_item 为在解包条目时调用的函数。

Unpacker.unpack_farray(n, unpack_item)

解包并（以列表形式）返回由同质条目构成的固定长度数组。 n 为期望的缓冲区内列表元素数量。 如上所述，unpack_item 是解包每个元素时要使用的函数。

Unpacker.unpack_array(unpack_item)

解包并返回由同质条目构成的可变长度 list。 先将列表的长度解包为无符号整数，再像上面的 unpack_farray() 一样解包每个元素。

14.4.3. 异常

此模块中的异常会表示为类实例代码:

exception xdrlib.Error

基本异常类。 Error 具有一个公共属性 msg，其中包含对错误的描述。

exception xdrlib.ConversionError

从 Error 所派生的类。 不包含额外的实例变量。

以下是一个应该如何捕获这些异常的示例:

import xdrlib
p = xdrlib.Packer()
try:
    p.pack_double(8.01)
except xdrlib.ConversionError as instance:
    print('packing the double failed:', instance.msg)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

14. 文件格式 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

14. 文件格式 »

|

14.5. plistlib — 生成与解析 Mac OS X .plist 文件

源代码: Lib/plistlib.py [https://github.com/python/cpython/tree/3.6/Lib/plistlib.py]

此模块提供了读写主要用于 Mac OS X 的 “property list” 文件的接口，并同时支持二进制和 XML plist 文件。

property list (.plist) 文件格式是一种简单的序列化格式，它支持一些基本对象类型，例如字典、列表、数字和字符串等。 通常使用一个字典作为最高层级对象。

要写入和解析 plist 文件，请使用 dump() 和 load() 函数。

要以字节串对象形式操作 plist 数据，请使用 dumps() 和 loads()。

值可以为字符串、整数、浮点数、布尔值、元组、列表、字典（但只允许用字符串作为键）、Data、bytes、bytesarray 或 datetime.datetime 对象。

在 3.4 版更改: 新版 API，旧版 API 已被弃用。 添加了对二进制 plist 格式的支持。

参见

PList manual page [https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/plist.5.html]

针对该文件格式的 Apple 文档。

这个模块定义了以下函数：

plistlib.load(fp, *, fmt=None, use_builtin_types=True, dict_type=dict)

读取 plist 文件。 fp 应当可读并且为二进制文件对象。 返回已解包的根对象（通常是一个字典）。

fmt 为文件的格式，有效的值如下:

None: 自动检测文件格式

FMT_XML: XML 文件格式

FMT_BINARY: 二进制 plist 格式

如果 use_builtin_types 为真值（默认）则将以 bytes 实例的形式返回二进制数据，否则将以 Data 实例的形式返回。

The dict_type is the type used for dictionaries that are read from the plist file. The exact structure of the plist can be recovered by using collections.OrderedDict (although the order of keys shouldn’t be important in plist files).

FMT_XML 格式的 XML 数据 会使用来自 xml.parsers.expat 的 Expat 解析器 – 请参阅其文档了解错误格式 XML 可能引发的异常。 未知元素将被 plist 解析器直接略过。

当文件无法被解析时二进制格式的解析器将引发 InvalidFileException。

3.4 新版功能.

plistlib.loads(data, *, fmt=None, use_builtin_types=True, dict_type=dict)

从一个 bytes 对象加载 plist。 参阅 load() 获取相应关键字参数的说明。

3.4 新版功能.

plistlib.dump(value, fp, *, fmt=FMT_XML, sort_keys=True, skipkeys=False)

将 value 写入 plist 文件。 Fp 应当可写并且为二进制文件对象。

fmt 参数指定 plist 文件的格式，可以是以下值之一:

FMT_XML: XML 格式的 plist 文件

FMT_BINARY: 二进制格式的 plist 文件

当 sort_keys 为真值（默认）时字典的键将经过排序再写入 plist，否则将按字典的迭代顺序写入。

当 skipkeys 为假值（默认）时该函数将在字典的键不为字符串时引发 TypeError，否则将跳过这样的键。

如果对象是不受支持的类型或者是包含不受支持类型的对象的容器则将引发 TypeError。

对于无法在（二进制）plist 文件中表示的整数值，将会引发 OverflowError。

3.4 新版功能.

plistlib.dumps(value, *, fmt=FMT_XML, sort_keys=True, skipkeys=False)

将 value 以 plist 格式字节串对象的形式返回。 参阅 dump() 的文档获取此函数的关键字参数的说明。

3.4 新版功能.

以下函数已被弃用:

plistlib.readPlist(pathOrFile)

读取 plist 文件。 pathOrFile 可以是文件名或（可读并且为二进制的）文件对象。 返回已解包的根对象（通常是一个字典）。

此函数会调用 load() 来完成实际操作，请参阅 该函数 的文档获取相应关键字参数的说明。

注解

Dict values in the result have a __getattr__ method that defers to __getitem_. This means that you can use attribute access to access items of these dictionaries.

3.4 版后已移除: 使用 load() 来代替。

plistlib.writePlist(rootObject, pathOrFile)

将 rootObject 写入 XML plist 文件。 pathOrFile 可以是文件名或（可写并且为二进制的）文件对象。

3.4 版后已移除: 使用 dump() 来代替。

plistlib.readPlistFromBytes(data)

从字节串对象读取 plist 数据。 返回根对象。

参阅 load() 获取相应关键字参数的说明。

注解

Dict values in the result have a __getattr__ method that defers to __getitem_. This means that you can use attribute access to access items of these dictionaries.

3.4 版后已移除: 使用 loads() 来代替。

plistlib.writePlistToBytes(rootObject)

将 rootObject 作为 XML plist 格式的字节串对象返回。

3.4 版后已移除: 使用 dumps() 来代替。

可以使用以下的类:

Dict([dict]):

Return an extended mapping object with the same value as dictionary dict.

This class is a subclass of dict where attribute access can be used to access items. That is, aDict.key is the same as aDict['key'] for getting, setting and deleting items in the mapping.

3.0 版后已移除.

class plistlib.Data(data)

返回一个包含字节串对象 data 的 “data” 包装器对象。 该类将在对 plist 进行双向转换的函数中被用来代表 plist 中可用的 <data> 类型。

它具有一个属性 data，可被用来提取其中所保存的 Python 字节串对象。

3.4 版后已移除: 使用 bytes 对象来代替。

可以使用以下的常量:

plistlib.FMT_XML

用于 plist 文件的 XML 格式。

3.4 新版功能.

plistlib.FMT_BINARY

用于 plist 文件的二进制格式。

3.4 新版功能.

14.5.1. 例子

生成一个 plist:

pl = dict(
    aString = "Doodah",
    aList = ["A", "B", 12, 32.1, [1, 2, 3]],
    aFloat = 0.1,
    anInt = 728,
    aDict = dict(
        anotherString = "<hello & hi there!>",
        aThirdString = "M\xe4ssig, Ma\xdf",
        aTrueValue = True,
        aFalseValue = False,
    ),
    someData = b"<binary gunk>",
    someMoreData = b"<lots of binary gunk>" * 10,
    aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())),
)
with open(fileName, 'wb') as fp:
    dump(pl, fp)

解析一个 plist:

with open(fileName, 'rb') as fp:
    pl = load(fp)
print(pl["aKey"])

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

14. 文件格式 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

15. 加密服务

本章中描述的模块实现了加密性质的各种算法。 它们可由安装人员自行决定。 在Unix系统上，crypt 模块也可以使用。 这是一个概述：

15.1. hashlib — 安全哈希与消息摘要

15.1.1. 哈希算法

15.1.2. SHAKE 可变长度摘要

15.1.3. 密钥派生

15.1.4. BLAKE2

15.1.4.1. 创建哈希对象

15.1.4.2. 常量

15.1.4.3. 例子

15.1.4.3.1. 简单哈希

15.1.4.3.2. 使用不同的摘要大小

15.1.4.3.3. 密钥哈希

15.1.4.3.4. 随机哈希

15.1.4.3.5. 个性化

15.1.4.3.6. 树形模式

15.1.4.4. 开发人员

15.2. hmac — 基于密钥的消息验证

15.3. secrets — 生成安全随机数字用于管理密码

15.3.1. 随机数

15.3.2. 生成凭据

15.3.2.1. 凭据应当使用多少个字节？

15.3.3. 其他功能

15.3.4. 应用技巧与最佳实践

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

15. 加密服务 »

|

15.1. hashlib — 安全哈希与消息摘要

源码： Lib/hashlib.py [https://github.com/python/cpython/tree/3.6/Lib/hashlib.py]

这个模块针对不同的安全哈希和消息摘要算法实现了一个通用的接口。包括 FIPS 的 SHA1, SHA224, SHA256, SHA384, and SHA512 (定义于 FIPS 180-2) 算法，以及 RSA 的 MD5 算法( 定义于 Internet RFC 1321 [https://tools.ietf.org/html/rfc1321.html])。术语“安全哈希”和“消息摘要”是可互换的，较旧的算法被称为消息摘要，现代术语是安全哈希。

注解

如果你想找到 adler32 或 crc32 哈希函数，它们在 zlib 模块中。

警告

有些算法已知存在哈希碰撞弱点，请参考最后的“另请参阅”段。

15.1.1. 哈希算法

每种类型的 hash 都有一个构造器方法。 它们都返回一个具有相同的简单接口的 hash 对象。 例如，使用 use sha256() 创建一个 SHA-256 hash 对象。 你可以使用 update() 方法向这个对象输入 字节类对象 (通常是 bytes)。 在任何时候你都可以使用 digest() 或 hexdigest() 方法获得到目前为止输入这个对象的拼接数据的 digest。

注解

为了更好的多线程性能，在对象创建或者更新时，若数据大于2047字节则 Python 的 GIL 会被释放。

注解

向 update() 输入字符串对象是不被支持的，因为哈希基于字节而非字符。

此模块中总是可用的哈希算法构造器有 sha1(), sha224(), sha256(), sha384(), sha512(), blake2b() 和 blake2s()。 md5() 通常也是可用的，但如果你在使用少见的 “FIPS 兼容” 的 Python 编译版本则可能会找不到它。 此外还可能有一些附加的算法，具体取决于你的平台上的 Python 所使用的 OpenSSL 库。 在大部分平台上可用的还有 sha3_224(), sha3_256(), sha3_384(), sha3_512(), shake_128(), shake_256() 等等。

3.6 新版功能: SHA3 (Keccak) 和 SHAKE 构造器 sha3_224(), sha3_256(), sha3_384(), sha3_512(), shake_128(), shake_256().

3.6 新版功能: 添加了 blake2b() 和 blake2s() 。

例如，如果想获取字节串 b'Nobody inspects the spammish repetition' 的摘要:

>>> import hashlib
>>> m = hashlib.sha256()
>>> m.update(b"Nobody inspects")
>>> m.update(b" the spammish repetition")
>>> m.digest()
b'\x03\x1e\xdd}Ae\x15\x93\xc5\xfe\\\x00o\xa5u+7\xfd\xdf\xf7\xbcN\x84:\xa6\xaf\x0c\x95\x0fK\x94\x06'
>>> m.digest_size
32
>>> m.block_size
64

更简要的写法：

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'

hashlib.new(name[, data])

一个接受所希望的算法对应的字符串 name 作为第一个形参的通用构造器。 它还允许访问上面列出的哈希算法以及你的 OpenSSL 库可能提供的任何其他算法。 同名的构造器要比 new() 更快所以应当优先使用。

使用 new() 并附带由 OpenSSL 所提供了算法:

>>> h = hashlib.new('ripemd160')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'cc4a5ce1b3df48aec5d22d1f16b894a0b894eccc'

Hashlib 提供下列常量属性：

hashlib.algorithms_guaranteed

一个集合，其中包含此模块在所有平台上都保证支持的哈希算法的名称。 请注意 ‘md5’ 也在此清单中，虽然某些上游厂商提供了一个怪异的排除了此算法的 “FIPS 兼容” Python 编译版本。

3.2 新版功能.

hashlib.algorithms_available

一个集合，其中包含在所运行的 Python 解释器上可用的哈希算法的名称。 将这些名称传给 new() 时将可被识别。 algorithms_guaranteed 将总是它的一个子集。 同样的算法在此集合中可能以不同的名称出现多次（这是 OpenSSL 的原因）。

3.2 新版功能.

下列值会以构造器所返回的哈希对象的常量属性的形式被提供:

hash.digest_size

以字节表示的结果哈希对象的大小。

hash.block_size

以字节表示的哈希算法的内部块大小。

hash 对象具有以下属性：

hash.name

此哈希对象的规范名称，总是为小写形式并且总是可以作为 new() 的形参用来创建另一个此类型的哈希对象。

在 3.4 版更改: 该属性名称自被引入起即存在于 CPython 中，但在 Python 3.4 之前并未正式指明，因此可能不存在于某些平台上。

哈希对象具有下列方法:

hash.update(data)

用 bytes-like object 来更新哈希对象。 重复调用相当于单次调用并传入所有参数的拼接结果: m.update(a); m.update(b) 等价于 m.update(a+b)。

在 3.1 版更改: 当使用 OpenSSL 提供的哈希算法在大于 2047 字节的数据上执行哈希更新时 Python GIL 会被释放以允许其他线程运行。

hash.digest()

返回当前已传给 update() 方法的数据摘要。 这是一个大小为 digest_size 的字节串对象，字节串中可包含 0 至 255 的完整取值范围。

hash.hexdigest()

类似于 digest() 但摘要会以两倍长度字符串对象的形式返回，其中仅包含十六进制数码。 这可以被用于在电子邮件或其他非二进制环境中安全地交换数据值。

hash.copy()

返回哈希对象的副本（“克隆”）。 这可被用来高效地计算共享相同初始子串的数据的摘要。

15.1.2. SHAKE 可变长度摘要

shake_128() 和 shake_256() 算法提供安全的 length_in_bits//2 至 128 或 256 位可变长度摘要。 为此，它们的摘要需指定一个长度。 SHAKE 算法不限制最大长度。

shake.digest(length)

返回当前已传给 update() 方法的数据摘要。 这是一个大小为 length 的字节串对象，字节串中可包含 0 to 255 的完整取值范围。

shake.hexdigest(length)

类似于 digest() 但摘要会以两倍长度字符串对象的形式返回，其中仅包含十六进制数码。 这可以被用于在电子邮件或其他非二进制环境中安全地交换数据值。

15.1.3. 密钥派生

密钥派生和密钥延展算法被设计用于安全密码哈希。 sha1(password) 这样的简单算法无法防御暴力攻击。 好的密码哈希函数必须可以微调、放慢步调，并且包含 加盐 [https://en.wikipedia.org/wiki/Salt_%28cryptography%29]。

hashlib.pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None)

此函数提供 PKCS#5 基于密码的密钥派生函数 2。 它使用 HMAC 作为伪随机函数。

字符串 hash_name 是要求用于 HMAC 的哈希摘要算法的名称，例如 ‘sha1’ 或 ‘sha256’。 password 和 salt 会以字节串缓冲区的形式被解析。 应用和库应当将 password 限制在合理长度 (例如 1024)。 salt 应当为适当来源例如 os.urandom() 的大约 16 个或更多的字节串数据。

iterations 数值应当基于哈希算法和算力来选择。 在 2013 年时，建议至少为 100,000 次 SHA-256 迭代。

dklen 为派生密钥的长度。 如果 dklen 为 None 则会使用哈希算法 hash_name 的摘要大小，例如 SHA-512 为 64。

>>> import hashlib, binascii
>>> dk = hashlib.pbkdf2_hmac('sha256', b'password', b'salt', 100000)
>>> binascii.hexlify(dk)
b'0394a2ede332c9a13eb82e9b24631604c31df978b4e2f0fbd2c549944f9d79a5'

3.4 新版功能.

注解

随同 OpenSSL 提供了一个快速的 pbkdf2_hmac 实现。 Python 实现是使用 hmac 的内联版本。 它的速度大约要慢上三倍并且不会释放 GIL。

hashlib.scrypt(password, *, salt, n, r, p, maxmem=0, dklen=64)

此函数提供基于密码加密的密钥派生函数，其定义参见 RFC 7914 [https://tools.ietf.org/html/rfc7914.html]。

password 和 salt 必须为 字节类对象。 应用和库应当将 password 限制在合理长度 (例如 1024)。 salt 应当为适当来源例如 os.urandom() 的大约 16 个或更多的字节串数据。

n is the CPU/Memory cost factor, r the block size, p parallelization factor and maxmem limits memory (OpenSSL 1.1.0 defaults to 32 MB). dklen is the length of the derived key.

Availability: OpenSSL 1.1+

3.6 新版功能.

15.1.4. BLAKE2

BLAKE2 [https://blake2.net] 是在 RFC 7693 [https://tools.ietf.org/html/rfc7693.html] 中定义的加密哈希函数，它有两种形式:

BLAKE2b，针对 64 位平台进行优化，并会生成长度介于 1 和 64 字节之间任意大小的摘要。

BLAKE2s，针对 8 至 32 位平台进行优化，并会生成长度介于 1 和 32 字节之间任意大小的摘要。

BLAKE2 支持 keyed mode (HMAC [https://en.wikipedia.org/wiki/Hash-based_message_authentication_code] 的更快速更简单的替代), salted hashing, personalization 和 tree hashing.

此模块的哈希对象遵循标准库 hashlib 对象的 API。

15.1.4.1. 创建哈希对象

新哈希对象可通过调用构造器函数来创建:

hashlib.blake2b(data=b'', *, digest_size=64, key=b'', salt=b'', person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, node_depth=0, inner_size=0, last_node=False)

hashlib.blake2s(data=b'', *, digest_size=32, key=b'', salt=b'', person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, node_depth=0, inner_size=0, last_node=False)

这些函数返回用于计算 BLAKE2b 或 BLAKE2s 的相应的哈希对象。 它们接受下列可选通用形参:

data: 要哈希的初始数据块，它必须为 bytes-like object。 它只能作为位置参数传入。

digest_size: 以字节数表示的输出摘要大小。

key: 用于密钥哈希的密钥（对于 BLAKE2b 最多 64 字节，对于 BLAKE2s 最多 32 字节）。

salt: 用于随机哈希的盐值（对于 BLAKE2b 最长 16 字节，对于 BLAKE2s 最长 8 字节）。

person: 个性化字符串（对于 BLAKE2b 最长 16 字节，对于 BLAKE2s 最长 8 字节）。

下表显示了常规参数的限制（以字节为单位）：

Hash

目标长度

长度（键）

长度（盐）

长度（个人）

BLAKE2b

64

64

16

16

BLAKE2s

32

32

8

8

注解

BLAKE2 规格描述为盐值和个性化形参定义了固定的长度，但是为了方便起见，此实现接受指定在长度以内的任意大小的字节串。 如果形参长度小于指定值，它将以零值进行填充，因此举例来说，b'salt' 和 b'salt\x00' 为相同的值 (key 的情况则并非如此。)

如下面的模块 constants 所描述，这些是可用的大小取值。

构造器函数还接受下列树形哈希形参:

fanout: 扇出值 (0 至 255，如无限制即为 0，连续模式下为 1)。

depth: 树的最大深度 (1 至 255，如无限制则为 255，连续模式下为 1)。

leaf_size: 叶子的最大字节长度 (0 至 2**32-1，如无限制或在连续模式下则为 0)。

node_offset: 节点偏移量 (对于 BLAKE2b 为 0 至 2**64-1，对于 BLAKE2s 为 0 至 2**48-1，对于最左边第一个叶子或在连续模式下则为 0)。

node_depth: 节点深度 (0 至 255，对于叶子或在连续模式下则为 0)。

inner_size: 内部摘要大小 (对于 BLAKE2b 为 0 至 64，对于 BLAKE2s 为 0 至 32，连续模式下则为 0)。

last_node: 一个布尔值，指明所处理的节点是否为最后一个 (连续模式下则为 False)。

请参阅 BLAKE2 规格描述 [https://blake2.net/blake2_20130129.pdf] 第 2.10 节了解有关树形哈希的完整介绍。

15.1.4.2. 常量

blake2b.SALT_SIZE

blake2s.SALT_SIZE

盐值长度（构造器所接受的最大长度）。

blake2b.PERSON_SIZE

blake2s.PERSON_SIZE

盐值长度（构造器所接受的最大长度）。

blake2b.MAX_KEY_SIZE

blake2s.MAX_KEY_SIZE

最大密钥长度。

blake2b.MAX_DIGEST_SIZE

blake2s.MAX_DIGEST_SIZE

哈希函数可输出的最大摘要长度。

15.1.4.3. 例子

15.1.4.3.1. 简单哈希

要计算某个数据的哈希值，你应该首先通过调用适当的构造器函数 (blake2b() 或 blake2s()) 来构造一个哈希对象，然后通过在该对象上调用 update() 来更新目标数据，最后通过调用 digest() (或针对十六进制编码字符串的 hexdigest()) 来获取该对象的摘要。

>>> from hashlib import blake2b
>>> h = blake2b()
>>> h.update(b'Hello world')
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'

作为快捷方式，你可以直接以位置参数的形式向构造器传入第一个数据块来直接更新:

>>> from hashlib import blake2b
>>> blake2b(b'Hello world').hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'

你可以多次调用 hash.update() 至你所想要的任意次数以迭代地更新哈希值:

>>> from hashlib import blake2b
>>> items = [b'Hello', b' ', b'world']
>>> h = blake2b()
>>> for item in items:
...     h.update(item)
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'

15.1.4.3.2. 使用不同的摘要大小

BLAKE2 具有可配置的摘要大小，对于 BLAKE2b 最多 64 字节，对于 BLAKE2s 最多 32 字节。 例如，要使用 BLAKE2b 来替代 SHA-1 而不改变输出大小，我们可以让 BLAKE2b 产生 20 个字节的摘要:

>>> from hashlib import blake2b
>>> h = blake2b(digest_size=20)
>>> h.update(b'Replacing SHA1 with the more secure function')
>>> h.hexdigest()
'd24f26cf8de66472d58d4e1b1774b4c9158b1f4c'
>>> h.digest_size
20
>>> len(h.digest())
20

不同摘要大小的哈希对象具有完全不同的输出（较短哈希值 并非 较长哈希值的前缀）；即使输出长度相同，BLAKE2b 和 BLAKE2s 也会产生不同的输出:

>>> from hashlib import blake2b, blake2s
>>> blake2b(digest_size=10).hexdigest()
'6fa1d8fcfd719046d762'
>>> blake2b(digest_size=11).hexdigest()
'eb6ec15daf9546254f0809'
>>> blake2s(digest_size=10).hexdigest()
'1bf21a98c78a1c376ae9'
>>> blake2s(digest_size=11).hexdigest()
'567004bf96e4a25773ebf4'

15.1.4.3.3. 密钥哈希

Keyed hashing can be used for authentication as a faster and simpler replacement for Hash-based message authentication code [http://en.wikipedia.org/wiki/Hash-based_message_authentication_code] (HMAC). BLAKE2 can be securely used in prefix-MAC mode thanks to the indifferentiability property inherited from BLAKE.

这个例子演示了如何使用密钥 b'pseudorandom key' 来为 b'message data' 获取一个（十六进制编码的）128 位验证代码:

>>> from hashlib import blake2b
>>> h = blake2b(key=b'pseudorandom key', digest_size=16)
>>> h.update(b'message data')
>>> h.hexdigest()
'3d363ff7401e02026f4a4687d4863ced'

作为实际的例子，一个 Web 应用可为发送给用户的 cookies 进行对称签名，并在之后对其进行验证以确保它们没有被篡改:

>>> from hashlib import blake2b
>>> from hmac import compare_digest
>>>
>>> SECRET_KEY = b'pseudorandomly generated server secret key'
>>> AUTH_SIZE = 16
>>>
>>> def sign(cookie):
...     h = blake2b(digest_size=AUTH_SIZE, key=SECRET_KEY)
...     h.update(cookie)
...     return h.hexdigest().encode('utf-8')
>>>
>>> def verify(cookie, sig):
...     good_sig = sign(cookie)
...     return compare_digest(good_sig, sig)
>>>
>>> cookie = b'user-alice'
>>> sig = sign(cookie)
>>> print("{0},{1}".format(cookie.decode('utf-8'), sig))
user-alice,b'43b3c982cf697e0c5ab22172d1ca7421'
>>> verify(cookie, sig)
True
>>> verify(b'user-bob', sig)
False
>>> verify(cookie, b'0102030405060708090a0b0c0d0e0f00')
False

即使存在原生的密钥哈希模式，BLAKE2 也同样可在 hmac 模块的 HMAC 构造过程中使用:

>>> import hmac, hashlib
>>> m = hmac.new(b'secret key', digestmod=hashlib.blake2s)
>>> m.update(b'message')
>>> m.hexdigest()
'e3c8102868d28b5ff85fc35dda07329970d1a01e273c37481326fe0c861c8142'

15.1.4.3.4. 随机哈希

用户可通过设置 salt 形参来为哈希函数引入随机化。 随机哈希适用于防止对数字签名中使用的哈希函数进行碰撞攻击。

随机哈希被设计用来处理当一方（消息准备者）要生成由另一方（消息签名者）进行签名的全部或部分消息的情况。 如果消息准备者能够找到加密哈希函数的碰撞现象（即两条消息产生相同的哈希值），则他们就可以准备将产生相同哈希值和数字签名但却具有不同结果的有意义的消息版本（例如向某个账户转入 $1,000,000 而不是 $10）。 加密哈希函数的设计都是以防碰撞性能为其主要目标之一的，但是当前针对加密哈希函数的集中攻击可能导致特定加密哈希函数所提供的防碰撞性能低于预期。 随机哈希为签名者提供了额外的保护，可以降低准备者在数字签名生成过程中使得两条或更多条消息最终产生相同哈希值的可能性 — 即使为特定哈希函数找到碰撞现象是可行的。 但是，当消息的所有部分均由签名者准备时，使用随机哈希可能降低数字签名所提供的安全性。

(NIST SP-800-106 “Randomized Hashing for Digital Signatures” [http://csrc.nist.gov/publications/nistpubs/800-106/NIST-SP-800-106.pdf])

在 BLAKE2 中，盐值会在初始化期间作为对哈希函数的一次性输入而不是对每个压缩函数的输入来处理。

警告

使用 BLAKE2 或任何其他通用加密哈希函数例如 SHA-256 进行 加盐哈希 (或纯哈希) 并不适用于哈希密码。 请参阅 BLAKE2 FAQ [https://blake2.net/#qa] 了解更多信息。

>>> import os
>>> from hashlib import blake2b
>>> msg = b'some message'
>>> # Calculate the first hash with a random salt.
>>> salt1 = os.urandom(blake2b.SALT_SIZE)
>>> h1 = blake2b(salt=salt1)
>>> h1.update(msg)
>>> # Calculate the second hash with a different random salt.
>>> salt2 = os.urandom(blake2b.SALT_SIZE)
>>> h2 = blake2b(salt=salt2)
>>> h2.update(msg)
>>> # The digests are different.
>>> h1.digest() != h2.digest()
True

15.1.4.3.5. 个性化

出于不同的目的强制让哈希函数为相同的输入生成不同的摘要有时也是有用的。 正如 Skein 哈希函数的作者所言:

我们建议所有应用设计者慎重考虑这种做法；我们已看到有许多协议在协议的某一部分中计算出来的哈希值在另一个完全不同的部分中也可以被使用，因为两次哈希计算是针对类似或相关的数据进行的，这样攻击者可以强制应用为相同的输入生成哈希值。 个性化协议中所使用的每个哈希函数将有效地阻止这种类型的攻击。

(Skein 哈希函数族 [http://www.skein-hash.info/sites/default/files/skein1.3.pdf], p. 21)

BLAKE2 可通过向 person 参数传入字节串来进行个性化:

>>> from hashlib import blake2b
>>> FILES_HASH_PERSON = b'MyApp Files Hash'
>>> BLOCK_HASH_PERSON = b'MyApp Block Hash'
>>> h = blake2b(digest_size=32, person=FILES_HASH_PERSON)
>>> h.update(b'the same content')
>>> h.hexdigest()
'20d9cd024d4fb086aae819a1432dd2466de12947831b75c5a30cf2676095d3b4'
>>> h = blake2b(digest_size=32, person=BLOCK_HASH_PERSON)
>>> h.update(b'the same content')
>>> h.hexdigest()
'cf68fb5761b9c44e7878bfb2c4c9aea52264a80b75005e65619778de59f383a3'

个性化配合密钥模式也可被用来从单个密钥派生出多个不同密钥。

>>> from hashlib import blake2s
>>> from base64 import b64decode, b64encode
>>> orig_key = b64decode(b'Rm5EPJai72qcK3RGBpW3vPNfZy5OZothY+kHY6h21KM=')
>>> enc_key = blake2s(key=orig_key, person=b'kEncrypt').digest()
>>> mac_key = blake2s(key=orig_key, person=b'kMAC').digest()
>>> print(b64encode(enc_key).decode('utf-8'))
rbPb15S/Z9t+agffno5wuhB77VbRi6F9Iv2qIxU7WHw=
>>> print(b64encode(mac_key).decode('utf-8'))
G9GtHFE1YluXY1zWPlYk1e/nWfu0WSEb0KRcjhDeP/o=

15.1.4.3.6. 树形模式

以下是对包含两个叶子节点的最小树进行哈希的例子:

  10
 /  \
00  01

这个例子使用 64 字节内部摘要，返回 32 字节最终摘要:

>>> from hashlib import blake2b
>>>
>>> FANOUT = 2
>>> DEPTH = 2
>>> LEAF_SIZE = 4096
>>> INNER_SIZE = 64
>>>
>>> buf = bytearray(6000)
>>>
>>> # Left leaf
... h00 = blake2b(buf[0:LEAF_SIZE], fanout=FANOUT, depth=DEPTH,
...               leaf_size=LEAF_SIZE, inner_size=INNER_SIZE,
...               node_offset=0, node_depth=0, last_node=False)
>>> # Right leaf
... h01 = blake2b(buf[LEAF_SIZE:], fanout=FANOUT, depth=DEPTH,
...               leaf_size=LEAF_SIZE, inner_size=INNER_SIZE,
...               node_offset=1, node_depth=0, last_node=True)
>>> # Root node
... h10 = blake2b(digest_size=32, fanout=FANOUT, depth=DEPTH,
...               leaf_size=LEAF_SIZE, inner_size=INNER_SIZE,
...               node_offset=0, node_depth=1, last_node=True)
>>> h10.update(h00.digest())
>>> h10.update(h01.digest())
>>> h10.hexdigest()
'3ad2a9b37c6070e374c7a8c508fe20ca86b6ed54e286e93a0318e95e881db5aa'

15.1.4.4. 开发人员

BLAKE2 [https://blake2.net] 是由 Jean-Philippe Aumasson, Samuel Neves, Zooko Wilcox-O’Hearn 和 Christian Winnerlein 基于 Jean-Philippe Aumasson, Luca Henzen, Willi Meier 和 Raphael C.-W. Phan 所创造的 SHA-3 [https://en.wikipedia.org/wiki/NIST_hash_function_competition] 入围方案 BLAKE [https://131002.net/blake/] 进行设计的。

它使用的核心算法来自由 Daniel J. Bernstein 所设计的 ChaCha [https://cr.yp.to/chacha.html] 加密。

stdlib 实现是基于 pyblake2 [https://pythonhosted.org/pyblake2/] 模块的。 它由 Dmitry Chestnykh 在 Samuel Neves 所编写的 C 实现的基础上编写。 此文档拷贝自 pyblake2 [https://pythonhosted.org/pyblake2/] 并由 Dmitry Chestnykh 撰写。

C 代码由 Christian Heimes 针对 Python 进行了部分的重写。

以下公共领域贡献同时适用于 C 哈希函数实现、扩展代码和本文档:

在法律许可的范围内，作者已将此软件的全部版权以及关联和邻接权利贡献到全球公共领域。 此软件的发布不附带任何担保。

You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see http://creativecommons.org/publicdomain/zero/1.0/.

根据创意分享公共领域贡献 1.0 通用规范，下列人士为此项目的开发提供了帮助或对公共领域的修改作出了贡献:

Alexandr Sokolovskiy

参见

模块 hmac

使用哈希运算来生成消息验证代码的模块。

模块 base64

针对非二进制环境对二进制哈希值进行编辑的另一种方式。

https://blake2.net

BLAKE2 官方网站

http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf

有关安全哈希算法的 FIPS 180-2 出版物。

https://en.wikipedia.org/wiki/Cryptographic_hash_function#Cryptographic_hash_algorithms

包含关于哪些算法存在已知问题以及对其使用所造成的影响的信息的 Wikipedia 文章。

https://www.ietf.org/rfc/rfc2898.txt

PKCS #5: 基于密码的加密规范描述 2.0 版

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

15. 加密服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

15. 加密服务 »

|

15.2. hmac — 基于密钥的消息验证

源代码: Lib/hmac.py [https://github.com/python/cpython/tree/3.6/Lib/hmac.py]

此模块实现了 HMAC 算法，算法的描述参见 RFC 2104 [https://tools.ietf.org/html/rfc2104.html]。

hmac.new(key, msg=None, digestmod=None)

返回一个新的 hmac 对象。 key 是一个指定密钥的 bytes 或 bytearray 对象。 如果提供了 msg，将会调用 update(msg) 方法。 digestmod 为 HMAC 对象要使用的摘要名称、摘要构造器或模块。 它支持任何适用于 hashlib.new() 的名称并默认设为 hashlib.md5 构造器。

在 3.4 版更改: 形参 key 可以为 bytes 或 bytearray 对象。 形参 msg 可以为 hashlib 所支持的任意类型。 形参 digestmod 可以为某种哈希算法的名称。

3.4 版后已移除: MD5 作为 digestmod 的隐式默认摘要已被弃用。

HMAC 对象具有下列方法:

HMAC.update(msg)

用 msg 来更新 hmac 对象。 重复调用相当于单次调用并传入所有参数的拼接结果: m.update(a); m.update(b) 等价于 m.update(a + b)。

在 3.4 版更改: 形参 msg 可以为 hashlib 所支持的任何类型。

HMAC.digest()

返回当前已传给 update() 方法的字节串数据的摘要。 这个字节串数据的长度将与传给构造器的摘要的长度 digest_size 相同。 它可以包含非 ASCII 的字节，包括 NUL 字节。

警告

在验证例程运行期间将 digest() 的输出与外部提供的摘要进行比较时，建议使用 compare_digest() 函数而不是 == 运算符以减少定时攻击防御力的不足。

HMAC.hexdigest()

类似于 digest() 但摘要会以两倍长度字符串的形式返回，其中仅包含十六进制数码。 这可以被用于在电子邮件或其他非二进制环境中安全地交换数据值。

警告

在验证例程运行期间将 hexdigest() 的输出与外部提供的摘要进行比较时，建议使用 compare_digest() 函数而不是 == 运算符以减少定时攻击防御力的不足。

HMAC.copy()

返回 hmac 对象的副本（“克隆）。 这可被用来高效地计算共享相同初始子串的数据的摘要。

hash 对象具有以下属性：

HMAC.digest_size

以字节表示的结果 HMAC 摘要的大小。

HMAC.block_size

以字节表示的哈希算法的内部块大小。

3.4 新版功能.

HMAC.name

HMAC 的规范名称，总是为小写形式，例如 hmac-md5。

3.4 新版功能.

这个模块还提供了下列辅助函数:

hmac.compare_digest(a, b)

返回 a == b。 此函数使用一种经专门设计的方式通过避免基于内容的短路行为来防止定时分析，使得它适合处理密码。 a 和 b 必须为相同的类型：或者是 str (仅限 ASCII 字符，如 HMAC.hexdigest() 的返回值)，或者是 bytes-like object。

注解

如果 a 和 b 具有不同的长度，或者如果发生了错误，定时攻击在理论上可以获取有关 a 和 b 的类型和长度信息 — 但不能获取它们的值。

3.3 新版功能.

参见

模块 hashlib

提供安全哈希函数的 Python 模块。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

15. 加密服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

15. 加密服务 »

|

15.3. secrets — 生成安全随机数字用于管理密码

3.6 新版功能.

源代码: Lib/secrets.py [https://github.com/python/cpython/tree/3.6/Lib/secrets.py]

secrets 模块可用于生成高加密强度的随机数，适应管理密码、账户验证、安全凭据和相关机密数据管理的需要。

特别地，应当优先使用 secrets 来替代 random 模块中默认的伪随机数生成器，后者被设计用于建模和仿真，而不适用于安全和加密。

参见

PEP 506 [https://www.python.org/dev/peps/pep-0506]

15.3.1. 随机数

通过 secrets 模块可以访问你的操作系统所能提供的最安全的随机性来源。

class secrets.SystemRandom

使用操作系统所提供的最高质量源来生成随机数的类。 请参阅 random.SystemRandom 了解更多细节。

secrets.choice(sequence)

返回从一个非空序列中随机选取的元素。

secrets.randbelow(n)

返回一个 [0, n) 范围之内的随机整数。

secrets.randbits(k)

返回一个具有 k 个随机比特位的整数。

15.3.2. 生成凭据

secrets 模块提供了一些生成安全凭据的函数，适用于诸如密码重置、难以猜测的 URL 之类的应用场景。

secrets.token_bytes([nbytes=None])

返回一个包含 nbytes 个字节的随机字节串。 如果 nbytes 为 None 或未提供，则会使用一个合理的默认值。

>>> token_bytes(16)
b'\xebr\x17D*t\xae\xd4\xe3S\xb6\xe2\xebP1\x8b'

secrets.token_hex([nbytes=None])

返回一个十六进制数码形式的随机字符串。 字符串具有 nbytes 个随机字节，每个字节转换为两个十六进制数码。 如果 nbytes 为 None 或未提供，则会使用一个合理的默认值。

>>> token_hex(16)
'f9bf78b9a18ce6d46a0cd2b0b86df9da'

secrets.token_urlsafe([nbytes=None])

返回一个 URL 安全的随机字符串，包含 nbytes 个随机字节。 文本将使用 Base64 编码，因此平均来说每个字节将对应 1.3 个结果字符。 如果 nbytes 为 None 或未提供，则会使用一个合理的默认值。

>>> token_urlsafe(16)
'Drmhze6EPcv0fN_81Bj-nA'

15.3.2.1. 凭据应当使用多少个字节？

为了在面对 暴力攻击 [https://en.wikipedia.org/wiki/Brute-force_attack] 时保证安全，凭据必须具有足够的随机性。 不幸的是，对随机性是否足够的标准会随着计算机越来越强大并能够在更短时间内进行更多猜测而不断提高。 在 2015 年时，人们认为 32 字节（256 位）的随机性对于 secrets 模块所适合的典型用例来说是足够的。

作为想要自行管理凭据长度的用户，你可以通过为各种 token_* 函数指定一个 int 参数来显式地指定凭据要使用多大的随机性。 该参数以字节数来表示要使用的随机性大小。

在其他情况下，如果未提供参数，或者如果参数为 None，则 token_* 函数将改用一个合理的默认值。

注解

该默认值可能在任何时候被改变，包括在维护版本更新的时候。

15.3.3. 其他功能

secrets.compare_digest(a, b)

Return True if strings a and b are equal, otherwise False, in such a way as to reduce the risk of timing attacks [http://codahale.com/a-lesson-in-timing-attacks/]. See hmac.compare_digest() for additional details.

15.3.4. 应用技巧与最佳实践

本节展示了一些使用 secrets 来管理基本安全级别的应用技巧和最佳实践。

生成长度为八个字符的字母数字密码:

import string
alphabet = string.ascii_letters + string.digits
password = ''.join(choice(alphabet) for i in range(8))

注解

应用程序不能 以可恢复的格式存储密码 [http://cwe.mitre.org/data/definitions/257.html]，无论是用纯文本还是加密。 它们应当使用高加密强度的单向（不可恢复）哈希函数来加盐并生成哈希值。

生成长度为十个字符的字母数字密码，其中包含至少一个小写字母，至少一个大写字母以及至少三个数字:

import string
alphabet = string.ascii_letters + string.digits
while True:
    password = ''.join(choice(alphabet) for i in range(10))
    if (any(c.islower() for c in password)
            and any(c.isupper() for c in password)
            and sum(c.isdigit() for c in password) >= 3):
        break

Generate an XKCD-style passphrase [http://xkcd.com/936/]:

# On standard Linux systems, use a convenient dictionary file.
# Other platforms may need to provide their own word-list.
with open('/usr/share/dict/words') as f:
    words = [word.strip() for word in f]
    password = ' '.join(choice(words) for i in range(4))

生成难以猜测的临时 URL，其中包含适合密码恢复应用的安全凭据:

url = 'https://mydomain.com/reset=' + token_urlsafe()

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

15. 加密服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

16. 通用操作系统服务

本章中描述的各模块提供了在（几乎）所有的操作系统上可用的操作系统特性的接口，例如文件和时钟。这些接口通常以 Unix 或 C 接口为参考对象，不过在大多数其他系统上也可用。这里有一个概述：

16.1. os — 操作系统接口模块

16.1.1. 文件名，命令行参数，以及环境变量。

16.1.2. 进程参数

16.1.3. 创建文件对象

16.1.4. 文件描述符操作

16.1.4.1. 查询终端的尺寸

16.1.4.2. 文件描述符的继承

16.1.5. 文件和目录

16.1.5.1. Linux 扩展属性

16.1.6. 进程管理

16.1.7. 调度器接口

16.1.8. 其他系统信息

16.1.9. 随机数

16.2. io — 处理流的核心工具

16.2.1. 概述

16.2.1.1. 文本 I/O

16.2.1.2. 二进制 I/O

16.2.1.3. 原始 I/O

16.2.2. 高阶模块接口

16.2.2.1. 内存中的流

16.2.3. 类的层次结构

16.2.3.1. I/O 基类

16.2.3.2. 原始文件 I/O

16.2.3.3. 缓冲流

16.2.3.4. 文本 I/O

16.2.4. 性能

16.2.4.1. 二进制 I/O

16.2.4.2. 文本 I/O

16.2.4.3. 多线程

16.2.4.4. 可重入性

16.3. time — 时间的访问和转换

16.3.1. 函数

16.3.2. Clock ID 常量

16.3.3. 时区常量

16.4. argparse — 命令行选项、参数和子命令解析器

16.4.1. 示例

16.4.1.1. 创建一个解析器

16.4.1.2. 添加参数

16.4.1.3. 解析参数

16.4.2. ArgumentParser 对象

16.4.2.1. prog

16.4.2.2. usage

16.4.2.3. description

16.4.2.4. epilog

16.4.2.5. parents

16.4.2.6. formatter_class

16.4.2.7. prefix_chars

16.4.2.8. fromfile_prefix_chars

16.4.2.9. argument_default

16.4.2.10. allow_abbrev

16.4.2.11. conflict_handler

16.4.2.12. add_help

16.4.3. add_argument() 方法

16.4.3.1. name or flags

16.4.3.2. action

16.4.3.3. nargs

16.4.3.4. const

16.4.3.5. 默认值

16.4.3.6. type – 类型

16.4.3.7. choices

16.4.3.8. required

16.4.3.9. help

16.4.3.10. metavar

16.4.3.11. dest

16.4.3.12. Action 类

16.4.4. parse_args() 方法

16.4.4.1. 选项值语法

16.4.4.2. 无效的参数

16.4.4.3. 包含 - 的参数

16.4.4.4. 参数缩写（前缀匹配）

16.4.4.5. 在 sys.argv 以外

16.4.4.6. 命名空间对象

16.4.5. 其它实用工具

16.4.5.1. 子命令

16.4.5.2. FileType 对象

16.4.5.3. 参数组

16.4.5.4. 互斥

16.4.5.5. 解析器默认值

16.4.5.6. 打印帮助

16.4.5.7. 部分解析

16.4.5.8. 自定义文件解析

16.4.5.9. 退出方法

16.4.6. 升级 optparse 代码

16.5. getopt — C 风格的命令行选项解析器

16.6. 模块 logging — Python 的日志记录工具

16.6.1. Logger 对象

16.6.2. 日志级别

16.6.3. 处理器对象

16.6.4. 格式器对象

16.6.5. Filter 对象

16.6.6. LogRecord 属性

16.6.7. LogRecord 属性

16.6.8. LoggerAdapter 对象

16.6.9. 线程安全

16.6.10. 模块级别函数

16.6.11. 模块级属性

16.6.12. 与警告模块集成

16.7. logging.config — 日志记录配置

16.7.1. 配置函数

16.7.2. 配置字典架构

16.7.2.1. 字典架构细节

16.7.2.2. 增量配置

16.7.2.3. 对象连接

16.7.2.4. 用户定义对象

16.7.2.5. 访问外部对象

16.7.2.6. 访问内部对象

16.7.2.7. 导入解析与定制导入器

16.7.3. 配置文件格式

16.8. logging.handlers — 日志处理

16.8.1. StreamHandler

16.8.2. FileHandler

16.8.3. NullHandler

16.8.4. WatchedFileHandler

16.8.5. BaseRotatingHandler

16.8.6. RotatingFileHandler

16.8.7. TimedRotatingFileHandler

16.8.8. SocketHandler

16.8.9. DatagramHandler

16.8.10. SysLogHandler

16.8.11. NTEventLogHandler

16.8.12. SMTPHandler

16.8.13. MemoryHandler

16.8.14. HTTPHandler

16.8.15. QueueHandler

16.8.16. QueueListener

16.9. getpass — 便携式密码输入工具

16.10. curses — 终端字符单元显示的处理

16.10.1. 函数

16.10.2. Window 对象

16.10.3. 常量

16.11. curses.textpad — 用于 curses 程序的文本输入控件

16.11.1. 文本框对象

16.12. curses.ascii — 用于 ASCII 字符的工具

16.13. curses.panel — curses 的 panel 栈扩展

16.13.1. 函数

16.13.2. Panel 对象

16.14. platform — 获取底层平台的标识数据

16.14.1. 跨平台

16.14.2. Java平台

16.14.3. Windows平台

16.14.3.1. Win95/98 specific

16.14.4. Mac OS平台

16.14.5. Unix 平台

16.15. errno — 标准 errno 系统符号

16.16. ctypes — Python 的外部函数库

16.16.1. ctypes 教程

16.16.1.1. 载入动态连接库

16.16.1.2. 操作导入的动态链接库中的函数

16.16.1.3. 调用函数

16.16.1.4. 基础数据类型

16.16.1.5. 调用函数，继续

16.16.1.6. 使用自定义的数据类型调用函数

16.16.1.7. 指定必选参数的类型(函数原型)

16.16.1.8. 返回类型

16.16.1.9. 传递指针(或者传递引用)

16.16.1.10. 结构体和联合

16.16.1.11. 结构体/联合 字段对齐及字节顺序

16.16.1.12. 结构体和联合中的位域

16.16.1.13. 数组

16.16.1.14. 指针

16.16.1.15. 类型强制转换

16.16.1.16. 不完整类型

16.16.1.17. 回调函数

16.16.1.18. 访问 dll 中导出的值

16.16.1.19. 意外

16.16.1.20. 变长数据类型

16.16.2. ctypes 参考手册

16.16.2.1. 寻找动态链接库

16.16.2.2. 加载动态链接库

16.16.2.3. 外部函数

16.16.2.4. 函数原型

16.16.2.5. 工具函数

16.16.2.6. 数据类型

16.16.2.7. 基础数据类型

16.16.2.8. 结构化数据类型

16.16.2.9. 数组与指针

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

16.1. os — 操作系统接口模块

源代码： Lib/os.py [https://github.com/python/cpython/tree/3.6/Lib/os.py]

该模块提供了一些方便使用操作系统相关功能的函数。 如果你是想读写一个文件，请参阅 open()，如果你想操作路径，请参阅 os.path 模块，如果你想在命令行上读取所有文件中的所有行请参阅 fileinput 模块。 有关创建临时文件和目录的方法，请参阅 tempfile 模块，对于高级文件目录处理，请参阅 shutil 模块。

关于这些函数的适用性的说明：

所有 Python 内建的操作系统相关的模块的设计都是为了使得在同一功能可用的情况下，保持接口的一致性；例如，函数 os.stat(path) 以相同的格式返回关于 path 的统计信息（这个函数同时也是起源于 POSIX 接口）。

针对特定的操作的拓展同样在可用于 os 模块，但是使用它们必然会对可移植性产生威胁。

所有接受路径或文件名的函数都同时支持字节串和字符串对象，并在返回路径或文件名时使用相应类型的对象作为结果。

An “Availability: Unix” note means that this function is commonly found on Unix systems. It does not make any claims about its existence on a specific operating system.

If not separately noted, all functions that claim “Availability: Unix” are supported on Mac OS X, which builds on a Unix core.

注解

All functions in this module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system.

exception os.error

内建的 OSError 异常的一个别名。

os.name

导入的依赖特定操作系统的模块的名称。以下名称目前已注册: 'posix', 'nt', 'java'.

参见

sys.platform 有更详细的描述. os.uname() 只给出系统提供的版本信息。

platform 模块对系统的标识有更详细的检查。

16.1.1. 文件名，命令行参数，以及环境变量。

在 Python 中，使用字符串类型表示文件名、命令行参数和环境变量。 在某些系统上，在将这些字符串传递给操作系统之前，必须将这些字符串解码为字节。 Python 使用文件系统编码来执行此转换（请参阅 sys.getfilesystemencoding() ）。

在 3.1 版更改: 在某些系统上，使用文件系统编码进行转换可能会失败。 在这种情况下，Python 会使用 代理转义编码错误处理器，这意味着在解码时，不可解码的字节被 Unicode 字符 U+DCxx 替换，并且这些字节在编码时再次转换为原始字节。

文件系统编码必须保证成功解码小于 128 的所有字节。如果文件系统编码无法提供此保证， API 函数可能会引发 UnicodeErrors 。

16.1.2. 进程参数

这些函数和数据项提供了操作当前进程和用户的信息。

os.ctermid()

返回与进程控制终端对应的文件名。

Availability: Unix.

os.environ

一个表示字符串环境的 mapping 对象。 例如，environ['HOME'] 是你的主目录（在某些平台上）的路径名，相当于 C 中的 getenv("HOME")。

这个映射是在第一次导入 os 模块时捕获的，通常作为 Python 启动时处理 site.py 的一部分。除了通过直接修改 os.environ 之外，在此之后对环境所做的更改不会反映在 os.environ 中。

如果平台支持 putenv() 函数，这个映射除了可以用于查询环境外还能用于修改环境。 当这个映射被修改时，putenv() 将被自动调用。

在Unix系统上，键和值会使用 sys.getfilesystemencoding() 和 'surrogateescape' 的错误处理。如果你想使用其他的编码，使用 environb。

注解

直接调用 putenv() 并不会影响 os.environ，所以推荐直接修改``os.environ``。

注解

在某些平台上，包括 FreeBSD 和 Mac OS X，设置 environ 可能导致内存泄露。参阅 putenv() 的系统文档。

如果平台没有提供 putenv(), 为了使启动的子进程使用修改后的环境，一份修改后的映射会被传给合适的进程创建函数。

如果平台支持 unsetenv() 函数，你可以通过删除映射中元素的方式来删除对应的环境变量。当一个元素被从 os.environ 删除时，以及 pop() 或 clear() 被调用时， unsetenv() 会被自动调用。

os.environb

字节版本的 environ: 一个以字节串表示环境的 mapping 对象。 environ 和 environb 是同步的（修改 environb 会更新 environ，反之亦然）。

environb is only available if supports_bytes_environ is True.

3.2 新版功能.

os.chdir(path)

os.fchdir(fd)

os.getcwd()

以上函数请参阅 文件和目录 。

os.fsencode(filename)

编码 路径类 文件名 为文件系统接受的形式，使用 'surrogateescape' 代理转义编码错误处理器，在Windows系统上会使用 'strict' ；返回 bytes 字节类型不变。

fsdecode() 是此函数的逆向函数。

3.2 新版功能.

在 3.6 版更改: 增加对实现了 os.PathLike 接口的对象的支持。

os.fsdecode(filename)

从文件系统编码方式解码为 路径类 文件名，使用 'surrogateescape' 代理转义编码错误处理器，在Windows系统上会使用 'strict' ；返回 str 字符串不变。

fsencode() 是此函数的逆向函数。

3.2 新版功能.

在 3.6 版更改: 增加对实现了 os.PathLike 接口的对象的支持。

os.fspath(path)

返回路径的文件系统表示。

如果传入的是 str 或 bytes 类型的字符串，将原样返回。否则 __fspath__() 将被调用，如果得到的是一个 str 或 bytes 类型的对象，那就返回这个值。其他所有情况则会抛出 TypeError  异常。

3.6 新版功能.

class os.PathLike

描述表示一个文件系统路径的 abstract base class ，如 pathlib.PurePath 。

3.6 新版功能.

abstractmethod __fspath__()

返回当前对象的文件系统表示。

这个方法只应该返回一个 str 字符串或 bytes 字节串，请优先选择 str 字符串。

os.getenv(key, default=None)

如果存在，返回环境变量 key 的值，否则返回 default。 key ， default 和返回值均为 str 字符串类型。

在Unix系统上，键和值会使用 sys.getfilesystemencoding() 和``’surrogateescape’`` 错误处理进行解码。如果你想使用其他的编码，使用 os.getenvb()。

Availability: most flavors of Unix, Windows.

os.getenvb(key, default=None)

如果存在环境变量 key 那么返回其值，否则返回 default。 key ， default 和返回值均为bytes字节串类型。

getenvb() is only available if supports_bytes_environ is True.

Availability: most flavors of Unix.

3.2 新版功能.

os.get_exec_path(env=None)

返回将用于搜索可执行文件的目录列表，与在外壳程序中启动一个进程时相似。指定的 env 应为用于搜索 PATH 的环境变量字典。默认情况下，当 env 为 None 时，将会使用 environ 。

3.2 新版功能.

os.getegid()

返回当前进程的有效组ID。对应当前进程执行文件的 “set id” 位。

Availability: Unix.

os.geteuid()

返回当前进程的有效用户ID。

Availability: Unix.

os.getgid()

返回当前进程的实际组ID。

Availability: Unix.

os.getgrouplist(user, group)

返回该用户所在的组 ID 列表。可能 group 参数没有在返回的列表中，实际上用户应该也是属于该 group。group 参数一般可以从储存账户信息的密码记录文件中找到。

Availability: Unix.

3.3 新版功能.

os.getgroups()

返回当前进程关联的附加组ID列表

Availability: Unix.

注解

在Mac OS X系统中，getgroups() 会和其他 Unix 平台有些不同。如果 Python 解释器是在 10.5 或更早版本中部署，getgroups() 返回当前用户进程相关的有效组ID列表。 该列表长度由于系统预设的接口限制，最长为 16。 而且在适当的权限下，返回结果还会因 getgroups() 而发生变化；如果 Python 解释器是在 10.5 以上版本中部署，getgroups() 返回进程所属有效用户 ID 所对应的用户的组 ID 列表，组用户列表可能因为进程的生存周期而发生变动，而且也不会因为 setgroups() 的调用而发生，返回的组用户列表长度也没有长度 16 的限制。在部署中，Python 解释器用到的变量 MACOSX_DEPLOYMENT_TARGET 可以用 sysconfig.get_config_var()。

os.getlogin()

返回通过控制终端进程进行登录的用户名。在多数情况下，使用 getpass.getuser() 会更有效，因为后者会通过检查环境变量 LOGNAME 或 USERNAME 来查找用户，再由 pwd.getpwuid(os.getuid())[0] 来获取当前用户 ID 的登录名。

Availability: Unix, Windows.

os.getpgid(pid)

根据进程id pid 返回进程的组 ID 列表。如果 pid 为 0，则返回当前进程的进程组 ID 列表

Availability: Unix.

os.getpgrp()

返回当时进程组的ID

Availability: Unix.

os.getpid()

返回当前进程ID

os.getppid()

返回父进程ID。当父进程已经结束，在Unix中返回的ID是初始进程(1)中的一个，在Windows中仍然是同一个进程ID，该进程ID有可能已经被进行进程所占用。

Availability: Unix, Windows.

在 3.2 版更改: 添加WIndows的支持。

os.getpriority(which, who)

获取程序调度优先级。which 参数值可以是 PRIO_PROCESS，PRIO_PGRP，或 PRIO_USER 中的一个，who 是相对于 which (PRIO_PROCESS 的进程标识符，PRIO_PGRP 的进程组标识符和 PRIO_USER 的用户ID)。当 who 为 0 时（分别）表示调用的进程，调用进程的进程组或调用进程所属的真实用户 ID。

Availability: Unix.

3.3 新版功能.

os.PRIO_PROCESS

os.PRIO_PGRP

os.PRIO_USER

函数 getpriority() 和 setpriority() 的参数。

Availability: Unix.

3.3 新版功能.

os.getresuid()

返回一个由 (ruid, euid, suid) 所组成的元组，分别表示当前进程的真实用户ID，有效用户ID和暂存用户ID。

Availability: Unix.

3.2 新版功能.

os.getresgid()

返回一个由 (rgid, egid, sgid) 所组成的元组，分别表示当前进程的真实组ID，有效组ID和暂存组ID。

Availability: Unix.

3.2 新版功能.

os.getuid()

返回当前进程的真实用户ID。

Availability: Unix.

os.initgroups(username, gid)

调用系统 initgroups()，使用指定用户所在的所有值来初始化组访问列表，包括指定的组ID。

Availability: Unix.

3.2 新版功能.

os.putenv(key, value)

将名为 key 的环境变量值设置为 value。该变量名修改会影响由 os.system()， popen() ，fork() 和 execv() 发起的子进程。

Availability: most flavors of Unix, Windows.

注解

在一些平台，包括 FreeBSD 和 Mac OS X，设置 environ 可能导致内存泄露。详情参考 putenv 相关系统文档。

当系统支持 putenv() 时，os.environ 中的参数赋值会自动转换为对 putenv() 的调用。不过 putenv() 的调用不会更新 os.environ，因此最好使用 os.environ 对变量赋值。

os.setegid(egid)

设置当前进程的有效组ID。

Availability: Unix.

os.seteuid(euid)

设置当前进程的有效用户ID。

Availability: Unix.

os.setgid(gid)

设置当前进程的组ID。

Availability: Unix.

os.setgroups(groups)

将 group 参数值设置为与当进程相关联的附加组ID列表。group 参数必须为一个序列，每个元素应为每个组的数字ID。该操作通常只适用于超级用户。

Availability: Unix.

注解

在 Mac OS X 中，groups 的长度不能超过系统定义的最大有效组 ID 个数，一般为 16。 如果它没有返回与调用 setgroups() 所设置的相同的组列表，请参阅 getgroups() 的文档。

os.setpgrp()

根据已实现的版本（如果有）来调用系统 setpgrp() 或 setpgrp(0, 0) 。相关说明，请参考 Unix 手册。

Availability: Unix.

os.setpgid(pid, pgrp)

使用系统调用 setpgid()，将 pid 对应进程的组ID设置为 pgrp。相关说明，请参考 Unix 手册。

Availability: Unix.

os.setpriority(which, who, priority)

设置程序调度优先级。 which 的值为 PRIO_PROCESS, PRIO_PGRP 或 PRIO_USER 之一，而 who 会相对于 which (PRIO_PROCESS 的进程标识符, PRIO_PGRP 的进程组标识符和 PRIO_USER 的用户 ID) 被解析。 who 值为零 (分别) 表示调用进程，调用进程的进程组或调用进程的真实用户 ID。 priority 是范围在 -20 至 19 的值。 默认优先级为 0；较小的优先级数值会更优先被调度。

Availability: Unix

3.3 新版功能.

os.setregid(rgid, egid)

设置当前进程的真实和有效组ID。

Availability: Unix.

os.setresgid(rgid, egid, sgid)

设置当前进程的真实，有效和暂存组ID。

Availability: Unix.

3.2 新版功能.

os.setresuid(ruid, euid, suid)

设置当前进程的真实，有效和暂存用户ID。

Availability: Unix.

3.2 新版功能.

os.setreuid(ruid, euid)

设置当前进程的真实和有效用户ID。

Availability: Unix.

os.getsid(pid)

调用系统调用 getsid()。 相关语义请参阅 Unix 手册。

Availability: Unix.

os.setsid()

使用系统调用 getsid()。相关说明，请参考 Unix 手册。

Availability: Unix.

os.setuid(uid)

设置当前进程的用户ID。

Availability: Unix.

os.strerror(code)

根据 code 中的错误码返回错误消息。 在某些平台上当给出未知错误码时 strerror() 将返回 NULL 并会引发 ValueError。

os.supports_bytes_environ

如果操作系统上原生环境类型是字节型则为 True (例如在 Windows 上为 False)。

3.2 新版功能.

os.umask(mask)

设定当前数值掩码并返回之前的掩码。

os.uname()

返回当前操作系统的识别信息。返回值是一个有5个属性的对象：

sysname - 操作系统名

nodename - 机器在网络上的名称（需要先设定）

release - 操作系统发行信息

version - 操作系统版本信息

machine - 硬件标识符

为了向后兼容，该对象也是可迭代的，像是一个按照 sysname，nodename，release，version，和 machine 顺序组成的元组。

有些系统会将 nodename 截短为 8 个字符或截短至前缀部分；获取主机名的一个更好方式是 socket.gethostname() 或甚至可以用 socket.gethostbyaddr(socket.gethostname())。

Availability: recent flavors of Unix.

在 3.3 版更改: 返回结果的类型由元组变成一个类似元组的对象，同时具有命名的属性。

os.unsetenv(key)

取消设置（删除）名为 key 的环境变量。变量名的改变会影响由 os.system()，popen()，fork() 和 execv() 触发的子进程。

当系统支持 unsetenv() ，删除在 os.environ 中的变量会自动转换为对 unsetenv() 的调用。但是 unsetenv() 不能更新 os.environ，因此最好直接删除 os.environ 中的变量。

Availability: most flavors of Unix, Windows.

16.1.3. 创建文件对象

该函数创建新的 文件对象。 （另见 open() 关于打开文件的说明。）

os.fdopen(fd, *args, **kwargs)

返回打开文件描述符 fd 对应文件的对象。类似内建 open() 函数，二者接受同样的参数。不同之处在于 fdopen() 第一个参数应该为整数。

16.1.4. 文件描述符操作

这些函数对文件描述符所引用的 I/O 流进行操作。

文件描述符是一些小的整数，对应于当前进程所打开的文件。例如，标准输入的文件描述符通常是0，标准输出是1，标准错误是2。之后被进程打开的文件的文件描述符会被依次指定为3，4，5等。“文件描述符”这个词有点误导性，在 Unix 平台中套接字和管道也被文件描述符所引用。

当需要时，可以用 fileno() 可以获得 file object 所对应的文件描述符。需要注意的是，直接使用文件描述符会绕过文件对象的方法，会忽略如数据内部缓冲等情况。

os.close(fd)

关闭文件描述符 fd。

注解

该功能适用于低级 I/O 操作，必须用于 os.open() 或 pipe() 返回的文件描述符。关闭由内建函数 open() ， popen() 或 fdopen() 返回的 “文件对象”，则使用其相应的 close() 方法。

os.closerange(fd_low, fd_high)

关闭从 fd_low （包括）到 fd_high （排除）间的文件描述符，并忽略错误。类似（但快于）:

for fd in range(fd_low, fd_high):
    try:
        os.close(fd)
    except OSError:
        pass

os.device_encoding(fd)

如果连接到终端，则返回一个与 fd 关联的设备描述字符，否则返回 None。

os.dup(fd)

返回一个文件描述符 fd 的副本。该文件描述符的副本是 不可继承的。

在 Windows 中，当复制一个标准流（0: stdin, 1: stdout, 2: stderr）时，新的文件描述符是 可继承的。

在 3.4 版更改: 新的文件描述符现在是不可继承的。

os.dup2(fd, fd2, inheritable=True)

Duplicate file descriptor fd to fd2, closing the latter first if necessary. The file descriptor fd2 is inheritable by default, or non-inheritable if inheritable is False.

在 3.4 版更改: 添加可选参数 inheritable。

os.fchmod(fd, mode)

将 fd 指定文件的权限状态修改为 mode。可以参考 chmod() 中列出 mode 的可用值。从Python 3.3开始，这相当于 os.chmod(fd, mode)。

Availability: Unix.

os.fchown(fd, uid, gid)

分别将 fd 指定文件的所有者和组 ID 修改为 uid 和 gid 的值。若不想变更其中的某个 ID，可将相应值设为 -1。参考 chown()。从 Python 3.3 开始，这相当于 os.chown(fd, uid, gid)。

Availability: Unix.

os.fdatasync(fd)

强制将文件描述符 fd 指定文件写入磁盘。不强制更新元数据。

Availability: Unix.

注解

该功能在 MacOS 中不可用。

os.fpathconf(fd, name)

返回与打开的文件有关的系统配置信息。name 指定要查找的配置名称，它可以是字符串，是一个系统已定义的名称，这些名称定义在不同标准（POSIX.1，Unix 95，Unix 98 等）中。一些平台还定义了额外的其他名称。当前操作系统已定义的名称在 pathconf_names 字典中给出。对于未包含在该映射中的配置名称，也可以传递一个整数作为 name。

如果 name 是一个字符串且不是已定义的名称，将抛出 ValueError 异常。如果当前系统不支持 name 指定的配置名称，即使该名称存在于 pathconf_names，也会抛出 OSError 异常，错误码为 errno.EINVAL。

从 Python 3.3 起，此功能等价于 os.pathconf(fd, name)。

Availability: Unix.

os.fstat(fd)

获取文件描述符 fd 的状态. 返回一个 stat_result 对象。

从 Python 3.3 起，此功能等价于 os.stat(fd)。

参见

stat() 函数。

os.fstatvfs(fd)

返回文件系统的信息，该文件系统是文件描述符 fd 指向的文件所在的文件系统，与 statvfs() 一样。从 Python 3.3 开始，它等效于 os.statvfs(fd)。

Availability: Unix.

os.fsync(fd)

强制将文件描述符 fd 指向的文件写入磁盘。在 Unix，这将调用原生 fsync() 函数；在 Windows，则是 MS _commit() 函数。

如果要写入的是缓冲区内的 Python 文件对象 f，请先执行 f.flush()，然后执行 os.fsync(f.fileno())，以确保与 f 关联的所有内部缓冲区都写入磁盘。

Availability: Unix, Windows.

os.ftruncate(fd, length)

将文件描述符 fd 对应的文件切分开，以使其最大为 length 字节。从 Python 3.3 开始，它等效于 os.truncate(fd, length)。

Availability: Unix, Windows.

在 3.5 版更改: 添加了 Windows 支持

os.get_blocking(fd)

获取文件描述符的阻塞模式：如果设置了 O_NONBLOCK 标志位，返回 False，如果该标志位被清除，返回 True。

参见 set_blocking() 和 socket.socket.setblocking()。

Availability: Unix.

3.5 新版功能.

os.isatty(fd)

如果文件描述符 fd 打开且已连接至 tty 设备（或类 tty 设备），返回 True，否则返回 False。

os.lockf(fd, cmd, len)

在打开的文件描述符上，使用、测试或删除 POSIX 锁。fd 是一个打开的文件描述符。cmd 指定要进行的操作，它们是 F_LOCK、F_TLOCK、F_ULOCK 或 F_TEST 中的一个。len 指定哪部分文件需要锁定。

Availability: Unix.

3.3 新版功能.

os.F_LOCK

os.F_TLOCK

os.F_ULOCK

os.F_TEST

标志位，用于指定 lockf() 进行哪一种操作。

Availability: Unix.

3.3 新版功能.

os.lseek(fd, pos, how)

将文件描述符 fd 的当前位置设置为 pos，位置的计算方式 how 如下：设置为 SEEK_SET 或 0 表示从文件开头计算，设置为 SEEK_CUR 或 1 表示从文件当前位置计算，设置为 SEEK_END 或 2 表示文件末尾计算。返回新指针位置，这个位置是从文件开头计算的，单位是字节。

os.SEEK_SET

os.SEEK_CUR

os.SEEK_END

lseek() 函数的参数，它们的值分别为 0、1 和 2。

3.3 新版功能: 某些操作系统可能支持其他值，例如 os.SEEK_HOLE 或 os.SEEK_DATA。

os.open(path, flags, mode=0o777, *, dir_fd=None)

打开文件 path，根据 flags 设置各种标志位，并根据 mode 设置其权限模式。当计算 mode 时，会首先根据当前 umask 值将部分权限去除。本方法返回新文件的描述符。新的文件描述符是 不可继承 的。

有关 flag 和 mode 取值的说明，请参见 C 运行时文档。标志位常量（如 O_RDONLY 和 O_WRONLY）在 os 模块中定义。特别地，在 Windows 上需要添加 O_BINARY 才能以二进制模式打开文件。

本函数带有 dir_fd 参数，支持 基于目录描述符的相对路径。

在 3.4 版更改: 新的文件描述符现在是不可继承的。

注解

本函数适用于底层的 I/O。常规用途请使用内置函数 open()，该函数的 read() 和 write() 方法（及其他方法）会返回 文件对象。要将文件描述符包装在文件对象中，请使用 fdopen()。

3.3 新版功能: dir_fd 参数。

在 3.5 版更改: 如果系统调用被中断，但信号处理程序没有触发异常，此函数现在会重试系统调用，而不是触发 InterruptedError 异常 (原因详见 PEP 475 [https://www.python.org/dev/peps/pep-0475])。

在 3.6 版更改: 接受一个 path-like object。

以下常量是 open() 函数 flags 参数的选项。可以用按位或运算符 | 将它们组合使用。部分常量并非在所有平台上都可用。有关其可用性和用法的说明，请参阅 open(2) 手册（Unix 上）或 MSDN [https://msdn.microsoft.com/en-us/library/z0kc8e3z.aspx] （Windows 上）。

os.O_RDONLY

os.O_WRONLY

os.O_RDWR

os.O_APPEND

os.O_CREAT

os.O_EXCL

os.O_TRUNC

上述常量在 Unix 和 Windows 上均可用。

os.O_DSYNC

os.O_RSYNC

os.O_SYNC

os.O_NDELAY

os.O_NONBLOCK

os.O_NOCTTY

os.O_CLOEXEC

这个常数仅在 Unix 系统中可用。

在 3.3 版更改: 增加 O_CLOEXEC 常量。

os.O_BINARY

os.O_NOINHERIT

os.O_SHORT_LIVED

os.O_TEMPORARY

os.O_RANDOM

os.O_SEQUENTIAL

os.O_TEXT

这个常数仅在 Windows 系统中可用。

os.O_ASYNC

os.O_DIRECT

os.O_DIRECTORY

os.O_NOFOLLOW

os.O_NOATIME

os.O_PATH

os.O_TMPFILE

os.O_SHLOCK

os.O_EXLOCK

上述常量是扩展常量，如果 C 库未定义它们，则不存在。

在 3.4 版更改: 在支持的系统上增加 O_PATH。增加 O_TMPFILE，仅在 Linux Kernel 3.11 或更高版本可用。

os.openpty()

打开一对新的伪终端，返回一对文件描述符``（主，从）``，分别为 pty 和 tty。新的文件描述符是 不可继承 的。对于（稍微）轻量一些的方法，请使用 pty 模块。

Availability: some flavors of Unix.

在 3.4 版更改: 新的文件描述符不再可继承。

os.pipe()

创建一个管道，返回一对分别用于读取和写入的文件描述符 (r, w)。新的文件描述符是 不可继承 的。

Availability: Unix, Windows.

在 3.4 版更改: 新的文件描述符不再可继承。

os.pipe2(flags)

创建带有 flags 标志位的管道。可通过对以下一个或多个值进行“或”运算来构造这些 flags：O_NONBLOCK、O_CLOEXEC。返回一对分别用于读取和写入的文件描述符 (r, w)。

Availability: some flavors of Unix.

3.3 新版功能.

os.posix_fallocate(fd, offset, len)

确保为 fd 指向的文件分配了足够的磁盘空间，该空间从偏移量 offset 开始，到 len 字节为止。

Availability: Unix.

3.3 新版功能.

os.posix_fadvise(fd, offset, len, advice)

声明即将以特定模式访问数据，使内核可以提前进行优化。数据范围是从 fd 所指向文件的 offset 开始，持续 len 个字节。advice 的取值是如下之一：POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED 或 POSIX_FADV_DONTNEED。

Availability: Unix.

3.3 新版功能.

os.POSIX_FADV_NORMAL

os.POSIX_FADV_SEQUENTIAL

os.POSIX_FADV_RANDOM

os.POSIX_FADV_NOREUSE

os.POSIX_FADV_WILLNEED

os.POSIX_FADV_DONTNEED

用于 posix_fadvise() 的 advice 参数的标志位，指定可能使用的访问模式。

Availability: Unix.

3.3 新版功能.

os.pread(fd, buffersize, offset)

Read from a file descriptor, fd, at a position of offset. It will read up to buffersize number of bytes. The file offset remains unchanged.

Availability: Unix.

3.3 新版功能.

os.pwrite(fd, str, offset)

Write bytestring to a file descriptor, fd, from offset, leaving the file offset unchanged.

Availability: Unix.

3.3 新版功能.

os.read(fd, n)

Read at most n bytes from file descriptor fd. Return a bytestring containing the bytes read. If the end of the file referred to by fd has been reached, an empty bytes object is returned.

注解

该功能适用于低级 I/O 操作，必须用于 os.open() 或 pipe() 返回的文件描述符。读取由内建函数 open()、popen()、fdopen() 或 sys.stdin 返回的 “文件对象”，则使用其相应的 read() 或 readline() 方法。

在 3.5 版更改: 如果系统调用被中断，但信号处理程序没有触发异常，此函数现在会重试系统调用，而不是触发 InterruptedError 异常 (原因详见 PEP 475 [https://www.python.org/dev/peps/pep-0475])。

os.sendfile(out, in, offset, count)

os.sendfile(out, in, offset, count, [headers, ][trailers, ]flags=0)

将文件描述符 in 中的 count 字节复制到文件描述符 out 的偏移位置 offset 处。返回复制的字节数，如果到达 EOF，返回0。

定义了 sendfile() 的所有平台均支持第一种函数用法。

在 Linux 上，将 offset 设置为 None，则从 in 的当前位置开始读取，并更新 in 的位置。

第二种函数用法可以在 Mac OS X 和 FreeBSD 上使用，其中，headers 和 trailers 是任意的缓冲区序列，它们分别在写入 in 的数据前、后被写入。返回值与第一种用法相同。

在 Mac OS X 和 FreeBSD 上，将 count 设为 0 表示持续复制直到 in 的结尾。

所有平台都支持将套接字作为 out 文件描述符，有些平台也支持其他类型（如常规文件或管道）。

跨平台应用程序不应使用 headers、trailers 和 flags 参数。

Availability: Unix.

注解

有关 sendfile() 的高级封装，参见 socket.socket.sendfile()。

3.3 新版功能.

os.set_blocking(fd, blocking)

设置指定文件描述符的阻塞模式：如果 blocking 为 False，则为该描述符设置 O_NONBLOCK 标志位，反之则清除该标志位。

参见 get_blocking() 和 socket.socket.setblocking()。

Availability: Unix.

3.5 新版功能.

os.SF_NODISKIO

os.SF_MNOWAIT

os.SF_SYNC

sendfile() 函数的参数（假设当前实现支持这些参数）。

Availability: Unix.

3.3 新版功能.

os.readv(fd, buffers)

Read from a file descriptor fd into a number of mutable bytes-like objects buffers. readv() will transfer data into each buffer until it is full and then move on to the next buffer in the sequence to hold the rest of the data. readv() returns the total number of bytes read (which may be less than the total capacity of all the objects).

Availability: Unix.

3.3 新版功能.

os.tcgetpgrp(fd)

返回与 fd 给定的终端相关联的进程组（fd 是由 os.open() 返回的已打开文件的描述符）。

Availability: Unix.

os.tcsetpgrp(fd, pg)

设置与 fd 指定的终端相关联的进程组为 pg*（*fd 是由 os.open() 返回的已打开的文件描述符）。

Availability: Unix.

os.ttyname(fd)

返回一个字符串，该字符串表示与文件描述符 fd 关联的终端。如果 fd 没有与终端关联，则抛出异常。

Availability: Unix.

os.write(fd, str)

Write the bytestring in str to file descriptor fd. Return the number of bytes actually written.

注解

该功能适用于低级 I/O 操作，必须用于 os.open() 或 pipe() 返回的文件描述符。若要写入由内建函数 open()、popen()、fdopen()、sys.stdout 或 sys.stderr 返回的 “文件对象”，则应使用其相应的 write() 方法。

在 3.5 版更改: 如果系统调用被中断，但信号处理程序没有触发异常，此函数现在会重试系统调用，而不是触发 InterruptedError 异常 (原因详见 PEP 475 [https://www.python.org/dev/peps/pep-0475])。

os.writev(fd, buffers)

Write the contents of buffers to file descriptor fd. buffers must be a sequence of bytes-like objects. Buffers are processed in array order. Entire contents of first buffer is written before proceeding to second, and so on. The operating system may set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.

writev() writes the contents of each object to the file descriptor and returns the total number of bytes written.

Availability: Unix.

3.3 新版功能.

16.1.4.1. 查询终端的尺寸

3.3 新版功能.

os.get_terminal_size(fd=STDOUT_FILENO)

返回终端窗口的尺寸，格式为``（列，行）``，它是类型为 terminal_size 的元组。

可选参数 fd``（默认为 ``STDOUT_FILENO 或标准输出）指定应查询的文件描述符。

如果文件描述符未连接到终端，则抛出 OSError 异常。

shutil.get_terminal_size() 是供常规使用的高阶函数，os.get_terminal_size 是其底层的实现。

Availability: Unix, Windows.

class os.terminal_size

元组的子类，存储终端窗口尺寸 (columns, lines)。

columns

终端窗口的宽度，单位为字符。

lines

终端窗口的高度，单位为字符。

16.1.4.2. 文件描述符的继承

3.4 新版功能.

每个文件描述符都有一个 “inheritable”（可继承）标志位，该标志位控制了文件描述符是否可以由子进程继承。从 Python 3.4 开始，由 Python 创建的文件描述符默认是不可继承的。

在 UNIX 上，执行新程序时，子进程中不可继承的文件描述符会被关闭，其他文件描述符将被继承。

在 Windows 上，不可继承的句柄和文件描述符在子进程中是关闭的，但标准流（文件描述符 0、1 和 2 即标准输入、标准输出和标准错误）是始终继承的。如果使用 spawn* 函数，所有可继承的句柄和文件描述符都将被继承。如果使用 subprocess 模块，将关闭除标准流以外的所有文件描述符，并且仅当 close_fds 参数为 False 时才继承可继承的句柄。

os.get_inheritable(fd)

获取指定文件描述符的“可继承”标志位（为布尔值）。

os.set_inheritable(fd, inheritable)

设置指定文件描述符的“可继承”标志位。

os.get_handle_inheritable(handle)

获取指定句柄的“可继承”标志位（为布尔值）。

Availability: Windows.

os.set_handle_inheritable(handle, inheritable)

设置指定句柄的“可继承”标志位。

Availability: Windows.

16.1.5. 文件和目录

在某些 Unix 平台上，许多函数支持以下一项或多项功能：

specifying a file descriptor: For some functions, the path argument can be not only a string giving a path name, but also a file descriptor. The function will then operate on the file referred to by the descriptor. (For POSIX systems, Python will call the f... version of the function.)

You can check whether or not path can be specified as a file descriptor on your platform using os.supports_fd. If it is unavailable, using it will raise a NotImplementedError.

If the function also supports dir_fd or follow_symlinks arguments, it is an error to specify one of those when supplying path as a file descriptor.

paths relative to directory descriptors: If dir_fd is not None, it should be a file descriptor referring to a directory, and the path to operate on should be relative; path will then be relative to that directory. If the path is absolute, dir_fd is ignored. (For POSIX systems, Python will call the ...at or f...at version of the function.)

You can check whether or not dir_fd is supported on your platform using os.supports_dir_fd. If it is unavailable, using it will raise a NotImplementedError.

not following symlinks: If follow_symlinks is False, and the last element of the path to operate on is a symbolic link, the function will operate on the symbolic link itself instead of the file the link points to. (For POSIX systems, Python will call the l... version of the function.)

You can check whether or not follow_symlinks is supported on your platform using os.supports_follow_symlinks. If it is unavailable, using it will raise a NotImplementedError.

os.access(path, mode, *, dir_fd=None, effective_ids=False, follow_symlinks=True)

使用真实的 uid/gid 测试对 path 的访问。请注意，大多数测试操作将使用有效的 uid/gid，因此可以在 suid/sgid 环境中运用此例程，来测试调用用户是否具有对 path 的指定访问权限。mode 为 F_OK 时用于测试 path 是否存在，也可以对 R_OK、W_OK 和 X_OK 中的一个或多个进行“或”运算来测试指定权限。允许访问则返回 True，否则返回 False。更多信息请参见 Unix 手册页 access(2)。

本函数支持指定 基于目录描述符的相对路径 和 不跟踪符号链接。

如果 effective_ids 为 True，access() 将使用 有效用户ID/用户组ID 而非 实际用户ID/用户组ID 进行访问检查。您的平台可能不支持 effective_ids，您可以使用 os.supports_effective_ids 检查它是否可用。如果不可用，使用它时会抛出 NotImplementedError 异常。

注解

使用 access() 来检查用户是否具有某项权限（如打开文件的权限），然后再使用 open() 打开文件，这样做存在一个安全漏洞，因为用户可能会在检查和打开文件之间的时间里做其他操作。推荐使用 EAFP 技术。如:

if os.access("myfile", os.R_OK):
    with open("myfile") as fp:
        return fp.read()
return "some default data"

最好写成:

try:
    fp = open("myfile")
except PermissionError:
    return "some default data"
else:
    with fp:
        return fp.read()

注解

即使 access() 指示 I/O 操作会成功，但实际操作仍可能失败，尤其是对网络文件系统的操作，其权限语义可能超出常规的 POSIX 权限位模型。

在 3.3 版更改: 添加 dir_fd、effective_ids 和 follow_symlinks 参数。

在 3.6 版更改: 接受一个 path-like object。

os.F_OK

os.R_OK

os.W_OK

os.X_OK

作为 access() 的 mode 参数的可选值，分别测试 path 的存在性、可读性、可写性和可执行性。

os.chdir(path)

将当前工作目录更改为 path。

本函数支持 指定文件描述符为参数。其中，描述符必须指向打开的目录，不能是打开的文件。

3.3 新版功能: 在某些平台上新增支持将 path 参数指定为文件描述符。

在 3.6 版更改: 接受一个 path-like object。

os.chflags(path, flags, *, follow_symlinks=True)

将 path 标志设置为数字 标志。标志 可以用以下值按位或组合起来（以下值在 stat 模块中定义）：

stat.UF_NODUMP

stat.UF_IMMUTABLE

stat.UF_APPEND

stat.UF_OPAQUE

stat.UF_NOUNLINK

stat.UF_COMPRESSED

stat.UF_HIDDEN

stat.SF_ARCHIVED

stat.SF_IMMUTABLE

stat.SF_APPEND

stat.SF_NOUNLINK

stat.SF_SNAPSHOT

本函数支持 不跟踪符号链接。

Availability: Unix.

3.3 新版功能: follow_symlinks 参数。

在 3.6 版更改: 接受一个 path-like object。

os.chmod(path, mode, *, dir_fd=None, follow_symlinks=True)

将 path 的 mode 更改为其他由数字表示的 mode。mode 可以用以下值之一，也可以将它们按位或组合起来（以下值在 stat 模块中定义）：

stat.S_ISUID

stat.S_ISGID

stat.S_ENFMT

stat.S_ISVTX

stat.S_IREAD

stat.S_IWRITE

stat.S_IEXEC

stat.S_IRWXU

stat.S_IRUSR

stat.S_IWUSR

stat.S_IXUSR

stat.S_IRWXG

stat.S_IRGRP

stat.S_IWGRP

stat.S_IXGRP

stat.S_IRWXO

stat.S_IROTH

stat.S_IWOTH

stat.S_IXOTH

本函数支持指定 指定文件描述符为参数、基于目录描述符的相对路径 和 不跟踪符号链接。

注解

尽管 Windows 支持 chmod()，但只能用它设置文件的只读标志（stat.S_IWRITE 和 stat.S_IREAD 常量或对应的整数值）。所有其他标志位都会被忽略。

3.3 新版功能: 添加了指定 path 为文件描述符的支持，以及 dir_fd 和 follow_symlinks 参数。

在 3.6 版更改: 接受一个 path-like object。

os.chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)

将 path 的用户和组 ID 分别修改为数字形式的 uid 和 gid。若要使其中某个 ID 保持不变，请将其置为 -1。

本函数支持指定 指定文件描述符为参数、基于目录描述符的相对路径 和 不跟踪符号链接。

参见更高阶的函数 shutil.chown()，除了数字 ID 之外，它也接受名称。

Availability: Unix.

3.3 新版功能: Added support for specifying an open file descriptor for path, and the dir_fd and follow_symlinks arguments.

在 3.6 版更改: 支持 类路径对象。

os.chroot(path)

将当前进程的根目录更改为 path。

Availability: Unix.

在 3.6 版更改: 接受一个 path-like object。

os.fchdir(fd)

将当前工作目录更改为文件描述符 fd 指向的目录。fd 必须指向打开的目录而非文件。从 Python 3.3 开始，它等效于 os.chdir(fd)。

Availability: Unix.

os.getcwd()

返回表示当前工作目录的字符串。

os.getcwdb()

返回表示当前工作目录的字节串 (bytestring)。

os.lchflags(path, flags)

将 path 的 flags 设置为其他由数字表示的 flags，与 chflags() 类似，但不跟踪符号链接。从 Python 3.3 开始，它等效于 os.chflags(path, flags, follow_symlinks=False)。

Availability: Unix.

在 3.6 版更改: 接受一个 path-like object。

os.lchmod(path, mode)

将 path 的权限状态修改为 mode。如果 path 是符号链接，则影响符号链接本身而非链接目标。可以参考 chmod() 中列出 mode 的可用值。从 Python 3.3 开始，这相当于 os.chmod(path, mode, follow_symlinks=False)。

Availability: Unix.

在 3.6 版更改: 接受一个 path-like object。

os.lchown(path, uid, gid)

将 path 的用户和组 ID 分别修改为数字形式的 uid 和 gid，本函数不跟踪符号链接。从 Python 3.3 开始，它等效于 os.chown(path, uid, gid, follow_symlinks=False)。

Availability: Unix.

在 3.6 版更改: 接受一个 path-like object。

os.link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)

创建一个指向 src 的硬链接，名为 dst。

本函数支持将 src_dir_fd 和 dst_dir_fd 中的一个或两个指定为 基于目录描述符的相对路径，支持 不跟踪符号链接。

Availability: Unix, Windows.

在 3.2 版更改: 添加了Windows 支持

3.3 新版功能: 添加 src_dir_fd、dst_dir_fd 和 follow_symlinks 参数。

在 3.6 版更改: 接受一个 类路径对象 作为 src 和 dst。

os.listdir(path='.')

返回一个列表，该列表包含了 path 中所有文件与目录的名称。该列表按任意顺序排列，并且不包含特殊条目 '.' 和 '..'，即使它们确实在目录中存在。

path 可以是 路径类对象。如果 path 是（直接/间接由 PathLike 接口返回的） bytes 类型，则返回的文件名也将是 bytes 类型，其他情况下文件名类型是 str。

本函数也支持 指定文件描述符为参数，其中描述符必须指向目录。

注解

要将 str 类型的文件名编码为 bytes，请使用 fsencode()。

参见

scandir() 函数返回目录内文件名的同时，也返回文件属性信息，它在某些具体情况下能提供更好的性能。

在 3.2 版更改: path 变为可选参数。

3.3 新版功能: Added support for specifying an open file descriptor for path.

在 3.6 版更改: 接受一个 path-like object。

os.lstat(path, *, dir_fd=None)

在给定路径上执行本函数，其操作相当于 lstat() 系统调用，类似于 stat() 但不跟踪符号链接。返回值是 stat_result 对象。

在不支持符号链接的平台上，本函数是 stat() 的别名。

从 Python 3.3 起，此功能等价于 os.stat(path, dir_fd=dir_fd, follow_symlinks=False)。

本函数支持 基于目录描述符的相对路径。

参见

stat() 函数。

在 3.2 版更改: 添加对 Windows 6.0 (Vista) 符号链接的支持。

在 3.3 版更改: 添加了 dir_fd 参数。

在 3.6 版更改: 接受一个 类路径对象 作为 src 和 dst。

os.mkdir(path, mode=0o777, *, dir_fd=None)

创建一个名为 path 的目录，应用以数字表示的权限模式 mode。

如果目录已存在，则抛出 FileExistsError 异常。

某些系统会忽略 mode。如果没有忽略它，那么将首先从它中减去当前的 umask 值。如果除最后 9 位（即 mode 八进制的最后 3 位）之外，还设置了其他位，则其他位的含义取决于各个平台。在某些平台上，它们会被忽略，应显式调用 chmod() 进行设置。

本函数支持 基于目录描述符的相对路径。

如果需要创建临时目录，请参阅 tempfile 模块中的 tempfile.mkdtemp() 函数。

3.3 新版功能: dir_fd 参数。

在 3.6 版更改: 接受一个 path-like object。

os.makedirs(name, mode=0o777, exist_ok=False)

递归目录创建函数。与 mkdir() 类似，但会自动创建到达最后一级目录所需要的中间目录。

The mode parameter is passed to mkdir(); see the mkdir() description for how it is interpreted.

If exist_ok is False (the default), an OSError is raised if the target directory already exists.

注解

如果要创建的路径元素包含 pardir (如 UNIX 系统中的 “..”) makedirs() 将无法明确目标。

本函数能正确处理 UNC 路径。

3.2 新版功能: exist_ok 参数。

在 3.4.1 版更改: 在 Python 3.4.1 以前，如果 exist_ok 为 True，且目录已存在，且 mode 与现有目录的权限不匹配，makedirs() 仍会抛出错误。由于无法安全地实现此行为，因此在 Python 3.4.1 中将该行为删除。请参阅 bpo-21082 [https://bugs.python.org/issue21082]。

在 3.6 版更改: 接受一个 path-like object。

os.mkfifo(path, mode=0o666, *, dir_fd=None)

创建一个名为 path 的 FIFO（命名管道，一种先进先出队列），具有以数字表示的权限状态 mode。将从 mode 中首先减去当前的 umask 值。

本函数支持 基于目录描述符的相对路径。

FIFO 是可以像常规文件一样访问的管道。FIFO 如果没有被删除（如使用 os.unlink()），会一直存在。通常，FIFO 用作“客户端”和“服务器”进程之间的汇合点：服务器打开 FIFO 进行读取，而客户端打开 FIFO 进行写入。请注意，mkfifo() 不会打开 FIFO — 它只是创建汇合点。

Availability: Unix.

3.3 新版功能: dir_fd 参数。

在 3.6 版更改: 接受一个 path-like object。

os.mknod(path, mode=0o600, device=0, *, dir_fd=None)

创建一个名为 path 的文件系统节点（文件，设备专用文件或命名管道）。mode 指定权限和节点类型，方法是将权限与下列节点类型 stat.S_IFREG、stat.S_IFCHR、stat.S_IFBLK 和 stat.S_IFIFO 之一（按位或）组合（这些常量可以在 stat 模块中找到）。对于 stat.S_IFCHR 和 stat.S_IFBLK，device 参数指定了新创建的设备专用文件（可能会用到 os.makedev()），否则该参数将被忽略。

本函数支持 基于目录描述符的相对路径。

Availability: Unix.

3.3 新版功能: dir_fd 参数。

在 3.6 版更改: 接受一个 path-like object。

os.major(device)

提取主设备号，提取自原始设备号（通常是 stat 中的 st_dev 或 st_rdev 字段）。

os.minor(device)

提取次设备号，提取自原始设备号（通常是 stat 中的 st_dev 或 st_rdev 字段）。

os.makedev(major, minor)

将主设备号和次设备号组合成原始设备号。

os.pathconf(path, name)

返回所给名称的文件有关的系统配置信息。name 指定要查找的配置名称，它可以是字符串，是一个系统已定义的名称，这些名称定义在不同标准（POSIX.1，Unix 95，Unix 98 等）中。一些平台还定义了额外的其他名称。当前操作系统已定义的名称在 pathconf_names 字典中给出。对于未包含在该映射中的配置名称，也可以传递一个整数作为 name。

如果 name 是一个字符串且不是已定义的名称，将抛出 ValueError 异常。如果当前系统不支持 name 指定的配置名称，即使该名称存在于 pathconf_names，也会抛出 OSError 异常，错误码为 errno.EINVAL。

本函数支持 指定文件描述符为参数。

Availability: Unix.

在 3.6 版更改: 接受一个 path-like object。

os.pathconf_names

字典，表示映射关系，为 pathconf() 和 fpathconf() 可接受名称与操作系统为这些名称定义的整数值之间的映射。这可用于判断系统已定义了哪些名称。

Availability: Unix.

os.readlink(path, *, dir_fd=None)

返回一个字符串，为符号链接指向的实际路径。其结果可以是绝对或相对路径。如果是相对路径，则可用 os.path.join(os.path.dirname(path), result) 转换为绝对路径。

如果 path 是字符串对象（直接传入或通过 PathLike 接口间接传入），则结果也将是字符串对象，且此类调用可能会引发 UnicodeDecodeError。如果 path 是字节对象（直接传入或间接传入），则结果将会是字节对象。

本函数支持 基于目录描述符的相对路径。

Availability: Unix, Windows

在 3.2 版更改: 添加对 Windows 6.0 (Vista) 符号链接的支持。

3.3 新版功能: dir_fd 参数。

在 3.6 版更改: 接受一个 path-like object。

os.remove(path, *, dir_fd=None)

Remove (delete) the file path. If path is a directory, OSError is raised. Use rmdir() to remove directories.

本函数支持 基于目录描述符的相对路径。

在 Windows 上，尝试删除正在使用的文件会抛出异常。而在 Unix 上，虽然该文件的条目会被删除，但分配给文件的存储空间仍然不可用，直到原始文件不再使用为止。

本函数在语义上与 unlink() 相同。

3.3 新版功能: dir_fd 参数。

在 3.6 版更改: 接受一个 path-like object。

os.removedirs(name)

递归删除目录。工作方式类似于 rmdir()，不同之处在于，如果成功删除了末尾一级目录，removedirs() 会尝试依次删除 path 中提到的每个父目录，直到抛出错误为止（但该错误会被忽略，因为这通常表示父目录不是空目录）。例如，os.removedirs('foo/bar/baz') 将首先删除目录 'foo/bar/baz'，然后如果 'foo/bar' 和 'foo' 为空，则继续删除它们。如果无法成功删除末尾一级目录，则抛出 OSError 异常。

在 3.6 版更改: 接受一个 path-like object。

os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)

Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). On Windows, if dst already exists, OSError will be raised even if it is a file.

本函数支持将 src_dir_fd 和 dst_dir_fd 中的一个或两个指定为 基于目录描述符的相对路径。

如果需要在不同平台上都能替换目标，请使用 replace()。

3.3 新版功能: src_dir_fd 和 dst_dir_fd 参数。

在 3.6 版更改: 接受一个 类路径对象 作为 src 和 dst。

os.renames(old, new)

递归重命名目录或文件。工作方式类似 rename()，除了会首先创建新路径所需的中间目录。重命名后，将调用 removedirs() 删除旧路径中不需要的目录。

注解

如果用户没有权限删除末级的目录或文件，则本函数可能会无法建立新的目录结构。

在 3.6 版更改: 接受一个 类路径对象 作为 old 和 new。

os.replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)

将文件或目录 src 重命名为 dst。如果 dst 是目录，将抛出 OSError 异常。如果 dst 已存在且为文件，则在用户具有权限的情况下，将对其进行静默替换。如果 src 和 dst 在不同的文件系统上，本操作可能会失败。如果成功，重命名操作将是一个原子操作（这是 POSIX 的要求）。

本函数支持将 src_dir_fd 和 dst_dir_fd 中的一个或两个指定为 基于目录描述符的相对路径。

3.3 新版功能.

在 3.6 版更改: 接受一个 类路径对象 作为 src 和 dst。

os.rmdir(path, *, dir_fd=None)

Remove (delete) the directory path. Only works when the directory is empty, otherwise, OSError is raised. In order to remove whole directory trees, shutil.rmtree() can be used.

本函数支持 基于目录描述符的相对路径。

3.3 新版功能: dir_fd 参数。

在 3.6 版更改: 接受一个 path-like object。

os.scandir(path='.')

返回一个迭代出 os.DirEntry 对象的迭代器，这些对象对应于 path 目录中的条目。条目的生成顺序是任意的，特殊条目 '.' 和 '..' 不包括在内。

如果需要文件类型或文件属性信息，使用 scandir() 代替 listdir() 可以大大提高这部分代码的性能，因为如果操作系统在扫描目录时返回的是 os.DirEntry 对象，则该对象包含了这些信息。所有 os.DirEntry 的方法都可能执行一次系统调用，但是 is_dir() 和 is_file() 通常只在有符号链接时才执行一次系统调用。os.DirEntry.stat() 在 Unix 上始终需要一次系统调用，而在 Windows 上只在有符号链接时才需要。

path 可以是 类路径对象。如果 path 是（直接/间接由 PathLike 接口返回的）``bytes`` 类型，那么每个 os.DirEntry 的 name 和 path 属性将是 bytes 类型，其他情况下是 str 类型。

scandir() 迭代器支持 上下文管理 协议，并具有以下方法：

scandir.close()

关闭迭代器并释放占用的资源。

当迭代器迭代完毕，或垃圾回收，或迭代过程出错时，将自动调用本方法。但仍建议显式调用它或使用 with 语句。

3.6 新版功能.

下面的例子演示了 scandir() 的简单用法，用来显示给定 path 中所有不以 '.' 开头的文件（不包括目录）。entry.is_file() 通常不会增加一次额外的系统调用：

with os.scandir(path) as it:
    for entry in it:
        if not entry.name.startswith('.') and entry.is_file():
            print(entry.name)

注解

在基于 Unix 的系统上，scandir() 使用系统的 opendir() [http://pubs.opengroup.org/onlinepubs/009695399/functions/opendir.html] 和 readdir() [http://pubs.opengroup.org/onlinepubs/009695399/functions/readdir_r.html] 函数。在 Windows 上，它使用 Win32 FindFirstFileW [https://msdn.microsoft.com/en-us/library/windows/desktop/aa364418(v=vs.85).aspx] 和 FindNextFileW [https://msdn.microsoft.com/en-us/library/windows/desktop/aa364428(v=vs.85).aspx] 函数。

3.5 新版功能.

3.6 新版功能: 添加了对 上下文管理 协议和 close() 方法的支持。如果 scandir() 迭代器没有迭代完毕且没有显式关闭，其析构函数将发出 ResourceWarning 警告。

本函数接受一个 类路径对象。

class os.DirEntry

由 scandir() 产生的对象，用于显示目录内某个条目的文件路径和其他文件属性。

scandir() 将在不进行额外系统调用的情况下，提供尽可能多的此类信息。每次进行 stat() 或 lstat() 系统调用时，os.DirEntry 对象会将结果缓存下来。

os.DirEntry 实例不适合存储在长期存在的数据结构中，如果你知道文件元数据已更改，或者自调用 scandir() 以来已经经过了很长时间，请调用 os.stat(entry.path) 来获取最新信息。

因为 os.DirEntry 方法可以进行系统调用，所以它也可能抛出 OSError 异常。如需精确定位错误，可以逐个调用 os.DirEntry 中的方法来捕获 OSError，并适当处理。

为了能直接用作 类路径对象，os.DirEntry 实现了 PathLike 接口。

os.DirEntry 实例所包含的属性和方法如下：

name

本条目的基本文件名，是根据 scandir() 的 path 参数得出的相对路径。

如果 scandir() 的 path 参数是 bytes 类型，则 name 属性也是 bytes 类型，否则为 str。使用 fsdecode() 解码 byte 类型的文件名。

path

The entry’s full path name: equivalent to os.path.join(scandir_path, entry.name) where scandir_path is the scandir() path argument. The path is only absolute if the scandir() path argument was absolute.

如果 scandir() 的 path 参数是 bytes 类型，则 path 属性也是 bytes 类型，否则为 str。使用 fsdecode() 解码 byte 类型的文件名。

inode()

返回本条目的索引节点号。

这一结果是缓存在 os.DirEntry 对象中的，请调用 os.stat(entry.path, follow_symlinks=False).st_ino 来获取最新信息。

一开始没有缓存时，在 Windows 上需要一次系统调用，但在 Unix 上不需要。

is_dir(*, follow_symlinks=True)

如果本条目是目录，或是指向目录的符号链接，则返回 True。如果本条目是文件，或指向任何其他类型的文件，或该文件不再存在，则返回 False。

如果 follow_symlinks 是 False，那么仅当本条目为目录时返回 True``（不跟踪符号链接），如果本条目是任何类型的文件，或该文件不再存在，则返回 ``False。

这一结果是缓存在 os.DirEntry 对象中的，且 follow_symlinks 为 True 和 False 时的缓存是分开的。请调用 os.stat() 和 stat.S_ISDIR() 来获取最新信息。

一开始没有缓存时，大多数情况下不需要系统调用。特别是对于非符号链接，Windows 和 Unix 都不需要系统调用，除非某些 Unix 文件系统（如网络文件系统）返回了 dirent.d_type == DT_UNKNOWN。如果本条目是符号链接，则需要一次系统调用来跟踪它（除非 follow_symlinks 为 False）。

本方法可能引发 OSError 异常，如 PermissionError 异常，但 FileNotFoundError 异常会被内部捕获且不会引发。

is_file(*, follow_symlinks=True)

如果本条目是文件，或是指向文件的符号链接，则返回 True。如果本条目是目录，或指向目录，或指向其他非文件条目，或该文件不再存在，则返回 False。

如果 follow_symlinks 是 False，那么仅当本条目为文件时返回 True``（不跟踪符号链接），如果本条目是目录或其他非文件条目，或该文件不再存在，则返回 ``False。

这一结果是缓存在 os.DirEntry 对象中的。缓存、系统调用、异常抛出都与 is_dir() 一致。

is_symlink()

如果本条目是符号链接（即使是断开的链接），返回 True。如果是目录或任何类型的文件，或本条目不再存在，返回 False。

这一结果是缓存在 os.DirEntry 对象中的，请调用 os.path.islink() 来获取最新信息。

一开始没有缓存时，大多数情况下不需要系统调用。其实 Windows 和 Unix 都不需要系统调用，除非某些 Unix 文件系统（如网络文件系统）返回了 dirent.d_type == DT_UNKNOWN。

本方法可能引发 OSError 异常，如 PermissionError 异常，但 FileNotFoundError 异常会被内部捕获且不会引发。

stat(*, follow_symlinks=True)

返回本条目对应的 stat_result 对象。本方法默认会跟踪符号链接，要获取符号链接本身的 stat，请添加 follow_symlinks=False 参数。

On Unix, this method always requires a system call. On Windows, it only requires a system call if follow_symlinks is True and the entry is a symbolic link.

在 Windows 上，stat_result 的 st_ino、st_dev 和 st_nlink 属性总是为零。请调用 os.stat() 以获得这些属性。

这一结果是缓存在 os.DirEntry 对象中的，且 follow_symlinks 为 True 和 False 时的缓存是分开的。请调用 os.stat() 来获取最新信息。

注意，os.DirEntry 和 pathlib.Path 的几个属性和方法之间存在很好的对应关系。具体来说是 name 属性，以及 is_dir()、is_file()、is_symlink() 和 stat() 方法，在两个类中具有相同的含义。

3.5 新版功能.

在 3.6 版更改: 添加了对 PathLike 接口的支持。在 Windows 上添加了对 bytes 类型路径的支持。

os.stat(path, *, dir_fd=None, follow_symlinks=True)

获取文件或文件描述符的状态。在所给路径上执行等效于 stat() 系统调用的操作。path 可以是字符串类型，或（直接/间接由 PathLike 接口返回的） bytes 类型，或打开的文件描述符。返回一个 stat_result 对象。

本函数默认会跟踪符号链接，要获取符号链接本身的 stat，请添加 follow_symlinks=False 参数，或使用 lstat()。

本函数支持 指定文件描述符为参数 和 不跟踪符号链接。

示例:

>>> import os
>>> statinfo = os.stat('somefile.txt')
>>> statinfo
os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026,
st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295,
st_mtime=1297230027, st_ctime=1297230027)
>>> statinfo.st_size
264

参见

fstat() 和 lstat() 函数。

3.3 新版功能: 增加 dir_fd 和 follow_symlinks 参数，可指定文件描述符代替路径。

在 3.6 版更改: 接受一个 path-like object。

class os.stat_result

本对象的属性大致对应于 stat 结构体成员，主要作为 os.stat()、os.fstat() 和 os.lstat() 的返回值。

属性：

st_mode

文件模式：包括文件类型和文件模式位（即权限位）。

st_ino

与平台有关，但如果不为零，则根据 st_dev 值唯一地标识文件。通常：

在 Unix 上该值表示索引节点号 (inode number)。

在 Windows 上该值表示 文件索引号 [https://msdn.microsoft.com/en-us/library/aa363788] 。

st_dev

该文件所在设备的标识符。

st_nlink

硬链接的数量。

st_uid

文件所有者的用户 ID。

st_gid

文件所有者的用户组 ID。

st_size

文件大小（以字节为单位），文件可以是常规文件或符号链接。符号链接的大小是它包含的路径的长度，不包括末尾的空字节。

时间戳：

st_atime

最近的访问时间，以秒为单位。

st_mtime

最近的修改时间，以秒为单位。

st_ctime

取决于平台：

在 Unix 上表示最近的元数据更改时间，

在 Windows 上表示创建时间，以秒为单位。

st_atime_ns

最近的访问时间，以纳秒表示，为整数。

st_mtime_ns

最近的修改时间，以纳秒表示，为整数。

st_ctime_ns

取决于平台：

在 Unix 上表示最近的元数据更改时间，

在 Windows 上表示创建时间，以纳秒表示，为整数。

See also the stat_float_times() function.

注解

st_atime、st_mtime 和 st_ctime 属性的确切含义和分辨率取决于操作系统和文件系统。例如，在使用 FAT 或 FAT32 文件系统的 Windows 上，st_mtime 有 2 秒的分辨率，而 st_atime 仅有 1 天的分辨率。详细信息请参阅操作系统文档。

类似地，尽管 st_atime_ns、st_mtime_ns 和 st_ctime_ns 始终以纳秒表示，但许多系统并不提供纳秒精度。在确实提供纳秒精度的系统上，用于存储 st_atime、st_mtime 和 st_ctime 的浮点对象无法保留所有精度，因此不够精确。如果需要确切的时间戳，则应始终使用 st_atime_ns、st_mtime_ns 和 st_ctime_ns。

在某些 Unix 系统上（如 Linux 上），以下属性可能也可用：

st_blocks

为文件分配的字节块数，每块 512 字节。文件是稀疏文件时，它可能小于 st_size/512。

st_blksize

“首选的” 块大小，用于提高文件系统 I/O 效率。写入文件时块大小太小可能会导致读取-修改-重写效率低下。

st_rdev

设备类型（如果是 inode 设备）。

st_flags

用户定义的文件标志位。

在其他 Unix 系统上（如 FreeBSD 上），以下属性可能可用（但仅当 root 使用它们时才被填充）：

st_gen

文件生成号。

st_birthtime

文件创建时间。

在 Mac OS 系统上，以下属性可能也可用：

st_rsize

文件的实际大小。

st_creator

文件的创建者。

st_type

文件类型。

On Windows systems, the following attribute is also available:

st_file_attributes

Windows 文件属性：dwFileAttributes，由 GetFileInformationByHandle() 返回的 BY_HANDLE_FILE_INFORMATION 结构体的成员之一。请参阅 stat 模块中的 FILE_ATTRIBUTE_* 常量。

标准模块 stat 中定义了函数和常量，这些函数和常量可用于从 stat 结构体中提取信息。（在 Windows 上，某些项填充的是虚值。）

为了向后兼容，一个 stat_result 实例还可以作为至少包含 10 个整数的元组访问，以提供 stat 结构中最重要（和可移植）的成员，整数顺序为 st_mode, st_ino, st_dev, st_nlink, st_uid, st_gid, st_size, st_atime, st_mtime, st_ctime。某些实现可能在末尾还有更多项。为了与旧版 Python 兼容，以元组形式访问 stat_result 始终返回整数。

3.3 新版功能: 添加了 st_atime_ns、st_mtime_ns 和 st_ctime_ns 成员。

3.5 新版功能: 在 Windows 上添加了 st_file_attributes 成员。

在 3.5 版更改: 在 Windows 上，如果可用，会返回文件索引作为 st_ino 的值。

os.stat_float_times([newvalue])

Determine whether stat_result represents time stamps as float objects. If newvalue is True, future calls to stat() return floats, if it is False, future calls return ints. If newvalue is omitted, return the current setting.

For compatibility with older Python versions, accessing stat_result as a tuple always returns integers.

Python now returns float values by default. Applications which do not work correctly with floating point time stamps can use this function to restore the old behaviour.

The resolution of the timestamps (that is the smallest possible fraction) depends on the system. Some systems only support second resolution; on these systems, the fraction will always be zero.

It is recommended that this setting is only changed at program startup time in the __main__ module; libraries should never change this setting. If an application uses a library that works incorrectly if floating point time stamps are processed, this application should turn the feature off until the library has been corrected.

3.3 版后已移除.

os.statvfs(path)

Perform a statvfs() system call on the given path. The return value is an object whose attributes describe the filesystem on the given path, and correspond to the members of the statvfs structure, namely: f_bsize, f_frsize, f_blocks, f_bfree, f_bavail, f_files, f_ffree, f_favail, f_flag, f_namemax.

为 f_flag 属性位定义了两个模块级常量：如果存在 ST_RDONLY 位，则文件系统以只读挂载；如果存在 ST_NOSUID 位，则文件系统禁用或不支持 setuid/setgid 位。

为基于 GNU/glibc 的系统还定义了额外的模块级常量。它们是 ST_NODEV （禁止访问设备专用文件），ST_NOEXEC （禁止执行程序），ST_SYNCHRONOUS （写入后立即同步），ST_MANDLOCK （允许文件系统上的强制锁定），ST_WRITE （写入文件/目录/符号链接），ST_APPEND （仅追加文件），ST_IMMUTABLE （不可变文件），ST_NOATIME （不更新访问时间），ST_NODIRATIME （不更新目录访问时间），ST_RELATIME （相对于 mtime/ctime 更新访问时间）。

本函数支持 指定文件描述符为参数。

Availability: Unix.

在 3.2 版更改: 添加了 ST_RDONLY 和 ST_NOSUID 常量。

3.3 新版功能: Added support for specifying an open file descriptor for path.

在 3.4 版更改: 添加了 ST_NODEV、ST_NOEXEC、ST_SYNCHRONOUS、ST_MANDLOCK、ST_WRITE、ST_APPEND、ST_IMMUTABLE、ST_NOATIME、ST_NODIRATIME 和 ST_RELATIME 常量。

在 3.6 版更改: 接受一个 path-like object。

os.supports_dir_fd

A Set object indicating which functions in the os module permit use of their dir_fd parameter. Different platforms provide different functionality, and an option that might work on one might be unsupported on another. For consistency’s sakes, functions that support dir_fd always allow specifying the parameter, but will raise an exception if the functionality is not actually available.

To check whether a particular function permits use of its dir_fd parameter, use the in operator on supports_dir_fd. As an example, this expression determines whether the dir_fd parameter of os.stat() is locally available:

os.stat in os.supports_dir_fd

目前 dir_fd 参数仅在 Unix 平台上有效，在 Windows 上均无效。

3.3 新版功能.

os.supports_effective_ids

A Set object indicating which functions in the os module permit use of the effective_ids parameter for os.access(). If the local platform supports it, the collection will contain os.access(), otherwise it will be empty.

To check whether you can use the effective_ids parameter for os.access(), use the in operator on supports_effective_ids, like so:

os.access in os.supports_effective_ids

Currently effective_ids only works on Unix platforms; it does not work on Windows.

3.3 新版功能.

os.supports_fd

A Set object indicating which functions in the os module permit specifying their path parameter as an open file descriptor. Different platforms provide different functionality, and an option that might work on one might be unsupported on another. For consistency’s sakes, functions that support fd always allow specifying the parameter, but will raise an exception if the functionality is not actually available.

To check whether a particular function permits specifying an open file descriptor for its path parameter, use the in operator on supports_fd. As an example, this expression determines whether os.chdir() accepts open file descriptors when called on your local platform:

os.chdir in os.supports_fd

3.3 新版功能.

os.supports_follow_symlinks

A Set object indicating which functions in the os module permit use of their follow_symlinks parameter. Different platforms provide different functionality, and an option that might work on one might be unsupported on another. For consistency’s sakes, functions that support follow_symlinks always allow specifying the parameter, but will raise an exception if the functionality is not actually available.

To check whether a particular function permits use of its follow_symlinks parameter, use the in operator on supports_follow_symlinks. As an example, this expression determines whether the follow_symlinks parameter of os.stat() is locally available:

os.stat in os.supports_follow_symlinks

3.3 新版功能.

os.symlink(src, dst, target_is_directory=False, *, dir_fd=None)

创建一个指向 src 的符号链接，名为 dst。

在 Windows 上，符号链接可以表示文件或目录两种类型，并且不会动态改变类型。如果目标存在，则新建链接的类型将与目标一致。否则，如果 target_is_directory 为 True，则符号链接将创建为目录链接，为 False （默认）将创建为文件链接。在非 Windows 平台上，target_is_directory 被忽略。

Symbolic link support was introduced in Windows 6.0 (Vista). symlink() will raise a NotImplementedError on Windows versions earlier than 6.0.

本函数支持 基于目录描述符的相对路径。

注解

On Windows, the SeCreateSymbolicLinkPrivilege is required in order to successfully create symlinks. This privilege is not typically granted to regular users but is available to accounts which can escalate privileges to the administrator level. Either obtaining the privilege or running your application as an administrator are ways to successfully create symlinks.

当本函数由非特权账户调用时，抛出 OSError 异常。

Availability: Unix, Windows.

在 3.2 版更改: 添加对 Windows 6.0 (Vista) 符号链接的支持。

3.3 新版功能: 添加了 dir_fd 参数，现在在非 Windows 平台上允许 target_is_directory 参数。

在 3.6 版更改: 接受一个 类路径对象 作为 src 和 dst。

os.sync()

强制将所有内容写入磁盘。

Availability: Unix.

3.3 新版功能.

os.truncate(path, length)

截断 path 对应的文件，以使其最大为 length 字节大小。

本函数支持 指定文件描述符为参数。

Availability: Unix, Windows.

3.3 新版功能.

在 3.5 版更改: 添加了 Windows 支持

在 3.6 版更改: 接受一个 path-like object。

os.unlink(path, *, dir_fd=None)

移除（删除）文件 path。该函数在语义上与 remove() 相同，unlink 是其传统的 Unix 名称。请参阅 remove() 的文档以获取更多信息。

3.3 新版功能: dir_fd 参数。

在 3.6 版更改: 接受一个 path-like object。

os.utime(path, times=None, *, [ns, ]dir_fd=None, follow_symlinks=True)

设置文件 path 的访问时间和修改时间。

utime() 有 times 和 ns 两个可选参数，它们指定了设置给 path 的时间，用法如下：

如果指定 ns，它必须是一个 (atime_ns, mtime_ns) 形式的二元组，其中每个成员都是一个表示纳秒的整数。

如果 times 不为 None，则它必须是 (atime, mtime) 形式的二元组，其中每个成员都是一个表示秒的 int 或 float。

如果 times 为 None 且未指定 ns，则相当于指定 ns=(atime_ns, mtime_ns)，其中两个时间均为当前时间。

同时为 times 和 ns 指定元组会出错。

Whether a directory can be given for path depends on whether the operating system implements directories as files (for example, Windows does not). Note that the exact times you set here may not be returned by a subsequent stat() call, depending on the resolution with which your operating system records access and modification times; see stat(). The best way to preserve exact times is to use the st_atime_ns and st_mtime_ns fields from the os.stat() result object with the ns parameter to utime.

本函数支持指定 指定文件描述符为参数、基于目录描述符的相对路径 和 不跟踪符号链接。

3.3 新版功能: Added support for specifying an open file descriptor for path, and the dir_fd, follow_symlinks, and ns parameters.

在 3.6 版更改: 接受一个 path-like object。

os.walk(top, topdown=True, onerror=None, followlinks=False)

生成目录树中的文件名，方式是按上->下或下->上顺序浏览目录树。对于以 top 为根的目录树中的每个目录（包括 top 本身），它都会生成一个三元组 (dirpath, dirnames, filenames)。

dirpath 是一个字符串，表示目录的路径。dirnames 是一个列表，内含 dirpath 中子目录的名称（不包括 '.' 和 '..' ）。filenames 也是列表，内含 dirpath 中非目录文件的名称。注意，列表中的名称不包含路径部分。要获取 dirpath 中文件或目录的完整路径（从 top 起始），请执行 os.path.join(dirpath, name)。

如果可选参数 topdown 为 True 或未指定，则在所有子目录的三元组之前生成父目录的三元组（目录是自上而下生成的）。如果 topdown 为 False，则在所有子目录的三元组生成之后再生成父目录的三元组（目录是自下而上生成的）。无论 topdown 为何值，在生成目录及其子目录的元组之前，都将检索全部子目录列表。

当 topdown 为 True 时，调用者可以就地修改 dirnames 列表（也许用到了 del 或切片），而 walk() 将仅仅递归到仍保留在 dirnames 中的子目录内。这可用于减少搜索、加入特定的访问顺序，甚至可在继续 walk() 之前告知 walk() 由调用者新建或重命名的目录的信息。当 topdown 为 False 时，修改 dirnames 对 walk 的行为没有影响，因为在自下而上模式中，dirnames 中的目录是在 dirpath 本身之前生成的。

默认将忽略 scandir() 调用中的错误。如果指定了可选参数 onerror，它应该是一个函数。出错时它会被调用，参数是一个 OSError 实例。它可以报告错误然后继续遍历，或者抛出异常然后中止遍历。注意，可以从异常对象的 filename 属性中获取出错的文件名。

walk() 默认不会递归进指向目录的符号链接。可以在支持符号链接的系统上将 followlinks 设置为 True，以访问符号链接指向的目录。

注解

注意，如果链接指向自身的父目录，则将 followlinks 设置为 True 可能导致无限递归。walk() 不会记录它已经访问过的目录。

注解

如果传入的是相对路径，请不要在恢复 walk() 之间更改当前工作目录。walk() 不会更改当前目录，并假定其调用者也不会更改当前目录。

下面的示例遍历起始目录内所有子目录，打印每个目录内的文件占用的字节数，CVS 子目录不会被遍历:

import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
    print(root, "consumes", end=" ")
    print(sum(getsize(join(root, name)) for name in files), end=" ")
    print("bytes in", len(files), "non-directory files")
    if 'CVS' in dirs:
        dirs.remove('CVS')  # don't visit CVS directories

在下一个示例（shutil.rmtree() 的简单实现）中，必须使树自下而上，因为 rmdir() 只允许在目录为空时删除目录:

# Delete everything reachable from the directory named in "top",
# assuming there are no symbolic links.
# CAUTION:  This is dangerous!  For example, if top == '/', it
# could delete all your disk files.
import os
for root, dirs, files in os.walk(top, topdown=False):
    for name in files:
        os.remove(os.path.join(root, name))
    for name in dirs:
        os.rmdir(os.path.join(root, name))

在 3.5 版更改: 现在，本函数调用的是 os.scandir() 而不是 os.listdir()，从而减少了调用 os.stat() 的次数而变得更快。

在 3.6 版更改: 接受一个 path-like object。

os.fwalk(top='.', topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None)

本方法的行为与 walk() 完全一样，除了它产生的是 4 元组 (dirpath, dirnames, filenames, dirfd)，并且它支持 dir_fd。

dirpath、dirnames 和 filenames 与 walk() 输出的相同，dirfd 是指向目录 dirpath 的文件描述符。

本函数始终支持 基于目录描述符的相对路径 和 不跟踪符号链接。但是请注意，与其他函数不同，fwalk() 的 follow_symlinks 的默认值为 False。

注解

由于 fwalk() 会生成文件描述符，而它们仅在下一个迭代步骤前有效，因此如果要将描述符保留更久，则应复制它们（比如使用 dup()）。

下面的示例遍历起始目录内所有子目录，打印每个目录内的文件占用的字节数，CVS 子目录不会被遍历:

import os
for root, dirs, files, rootfd in os.fwalk('python/Lib/email'):
    print(root, "consumes", end="")
    print(sum([os.stat(name, dir_fd=rootfd).st_size for name in files]),
          end="")
    print("bytes in", len(files), "non-directory files")
    if 'CVS' in dirs:
        dirs.remove('CVS')  # don't visit CVS directories

在下一个示例中，必须使树自下而上遍历，因为 rmdir() 只允许在目录为空时删除目录:

# Delete everything reachable from the directory named in "top",
# assuming there are no symbolic links.
# CAUTION:  This is dangerous!  For example, if top == '/', it
# could delete all your disk files.
import os
for root, dirs, files, rootfd in os.fwalk(top, topdown=False):
    for name in files:
        os.unlink(name, dir_fd=rootfd)
    for name in dirs:
        os.rmdir(name, dir_fd=rootfd)

Availability: Unix.

3.3 新版功能.

在 3.6 版更改: 接受一个 path-like object。

16.1.5.1. Linux 扩展属性

3.3 新版功能.

这些函数仅在 Linux 上可用。

os.getxattr(path, attribute, *, follow_symlinks=True)

返回 path 的扩展文件系统属性 attribute 的值。attribute 可以是 bytes 或 str （直接/间接通过 PathLike 接口返回的）。如果是 str，则使用文件系统编码来编码字符串。

本函数支持 指定文件描述符为参数 和 不跟踪符号链接。

在 3.6 版更改: 接受一个 类路径对象 作为 path 和 attribute。

os.listxattr(path=None, *, follow_symlinks=True)

返回一个列表，包含 path 的所有扩展文件系统属性。列表中的属性都表示为字符串，它们是根据文件系统编码解码出来的。如果 path 为 None，则 listxattr() 将检查当前目录。

本函数支持 指定文件描述符为参数 和 不跟踪符号链接。

在 3.6 版更改: 接受一个 path-like object。

os.removexattr(path, attribute, *, follow_symlinks=True)

从 path 中删除扩展文件系统属性 attribute。attribute 应该是 bytes 或 str （直接/间接通过 PathLike 接口返回的）。如果是字符串，则它应使用文件系统编码来编码。

本函数支持 指定文件描述符为参数 和 不跟踪符号链接。

在 3.6 版更改: 接受一个 类路径对象 作为 path 和 attribute。

os.setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)

将 path 的扩展文件系统属性 attribute 设置为 value。attribute 必须是没有空字符的 bytes 或 str （直接传入或通过 PathLike 接口间接传入）。如果是 str，则应使用文件系统编码进行编码。flags 可以是 XATTR_REPLACE 或 XATTR_CREATE。如果指定 XATTR_REPLACE 而该属性不存在，则抛出 EEXISTS 异常。如果指定 XATTR_CREATE 而该属性已经存在，则不会创建该属性，抛出 ENODATA 异常。

本函数支持 指定文件描述符为参数 和 不跟踪符号链接。

注解

Linux kernel 2.6.39 以下版本的一个 bug 导致在某些文件系统上，flags 参数会被忽略。

在 3.6 版更改: 接受一个 类路径对象 作为 path 和 attribute。

os.XATTR_SIZE_MAX

一条扩展属性的值的最大大小。在当前的 Linux 上是 64 KiB。

os.XATTR_CREATE

这是 setxattr() 的 flags 参数的可取值，它表示该操作必须创建一个属性。

os.XATTR_REPLACE

这是 setxattr() 的 flags 参数的可取值，它表示该操作必须替换现有属性。

16.1.6. 进程管理

下列函数可用于创建和管理进程。

所有 exec* 函数都接受一个参数列表，用来给新程序加载到它的进程中。在所有情况下，传递给新程序的第一个参数是程序本身的名称，而不是用户在命令行上输入的参数。对于 C 程序员来说，这就是传递给 main() 函数的 argv[0]。例如，os.execv('/bin/echo', ['foo', 'bar']) 只会在标准输出上打印 bar，而 foo 会被忽略。

os.abort()

发送 SIGABRT 信号到当前进程。在 Unix 上，默认行为是生成一个核心转储。在 Windows 上，该进程立即返回退出代码 3。请注意，使用 signal.signal() 可以为 SIGABRT 注册 Python 信号处理程序，而调用本函数将不会调用按前述方法注册的程序。

os.execl(path, arg0, arg1, ...)

os.execle(path, arg0, arg1, ..., env)

os.execlp(file, arg0, arg1, ...)

os.execlpe(file, arg0, arg1, ..., env)

os.execv(path, args)

os.execve(path, args, env)

os.execvp(file, args)

os.execvpe(file, args, env)

这些函数都将执行一个新程序，以替换当前进程。它们没有返回值。在 Unix 上，新程序会加载到当前进程中，且进程号与调用者相同。过程中的错误会被报告为 OSError 异常。

当前进程会被立即替换。打开的文件对象和描述符都不会刷新，因此如果这些文件上可能缓冲了数据，则应在调用 exec* 函数之前使用 sys.stdout.flush() 或 os.fsync() 刷新它们。

exec* 函数的 “l” 和 “v” 变体不同在于命令行参数的传递方式。如果在编码时固定了参数数量，则 “l” 变体可能是最方便的，各参数作为 execl*() 函数的附加参数传入即可。当参数数量可变时，”v” 变体更方便，参数以列表或元组的形式作为 args 参数传递。在这两种情况下，子进程的第一个参数都应该是即将运行的命令名称，但这不是强制性的。

结尾包含 “p” 的变体（execlp()、execlpe()、execvp() 和 execvpe() ）将使用 PATH 环境变量来查找程序 file。当环境被替换时（使用下一段讨论的 exec*e 变体之一），PATH 变量将来自于新环境。其他变体 execl()、execle()、execv() 和 execve() 不使用 PATH 变量来查找程序，因此 path 必须包含正确的绝对或相对路径。

对于 execle()、execlpe()、execve() 和 execvpe() （都以 “e” 结尾），env 参数是一个映射，用于定义新进程的环境变量（代替当前进程的环境变量）。而函数 execl()、execlp()、execv() 和 execvp() 会将当前进程的环境变量过继给新进程。

某些平台上的 execve() 可以将 path 指定为打开的文件描述符。当前平台可能不支持此功能，可以使用 os.supports_fd 检查它是否支持。如果不可用，则使用它会抛出 NotImplementedError 异常。

Availability: Unix, Windows.

3.3 新版功能: Added support for specifying an open file descriptor for path for execve().

在 3.6 版更改: 接受一个 path-like object。

os._exit(n)

以状态码 n 退出进程，不会调用清理处理程序，不会刷新 stdio，等等。

注解

退出的标准方法是使用 sys.exit(n)。而 _exit() 通常只应在 fork() 出的子进程中使用。

以下是已定义的退出代码，可以用于 _exit()，尽管它们不是必需的。这些退出代码通常用于 Python 编写的系统程序，例如邮件服务器的外部命令传递程序。

注解

其中部分退出代码在部分 Unix 平台上可能不可用，因为平台间存在差异。如果底层平台定义了这些常量，那上层也会定义。

os.EX_OK

退出代码，表示未发生任何错误。

Availability: Unix.

os.EX_USAGE

退出代码，表示命令使用不正确，如给出的参数数量有误。

Availability: Unix.

os.EX_DATAERR

退出代码，表示输入数据不正确。

Availability: Unix.

os.EX_NOINPUT

退出代码，表示某个输入文件不存在或不可读。

Availability: Unix.

os.EX_NOUSER

退出代码，表示指定的用户不存在。

Availability: Unix.

os.EX_NOHOST

退出代码，表示指定的主机不存在。

Availability: Unix.

os.EX_UNAVAILABLE

退出代码，表示所需的服务不可用。

Availability: Unix.

os.EX_SOFTWARE

退出代码，表示检测到内部软件错误。

Availability: Unix.

os.EX_OSERR

退出代码，表示检测到操作系统错误，例如无法 fork 或创建管道。

Availability: Unix.

os.EX_OSFILE

退出代码，意味着某些系统文件不存在，无法打开或发生其他错误。

Availability: Unix.

os.EX_CANTCREAT

退出代码，表示无法创建用户指定的输出文件。

Availability: Unix.

os.EX_IOERR

退出代码，表示对某些文件进行读写时发生错误。

Availability: Unix.

os.EX_TEMPFAIL

退出代码，表示发生了暂时性故障。它可能并非意味着真正的错误，例如在可重试的情况下无法建立网络连接。

Availability: Unix.

os.EX_PROTOCOL

退出代码，表示协议交换是非法的、无效的或无法解读的。

Availability: Unix.

os.EX_NOPERM

退出代码，表示没有足够的权限执行该操作（但不适用于文件系统问题）。

Availability: Unix.

os.EX_CONFIG

退出代码，表示发生某种配置错误。

Availability: Unix.

os.EX_NOTFOUND

退出代码，表示的内容类似于“找不到条目”。

Availability: Unix.

os.fork()

Fork 出一个子进程。在子进程中返回 0，在父进程中返回子进程的进程号。如果发生错误，则抛出 OSError 异常。

Note that some platforms including FreeBSD <= 6.3 and Cygwin have known issues when using fork() from a thread.

警告

有关 SSL 模块与 fork() 结合的应用，请参阅 ssl。

Availability: Unix.

os.forkpty()

Fork 出一个子进程，使用新的伪终端作为子进程的控制终端。返回一对 (pid, fd)，其中 pid 在子进程中为 0，这是父进程中新子进程的进程号，而 fd 是伪终端主设备的文件描述符。对于更便于移植的方法，请使用 pty 模块。如果发生错误，则抛出 OSError 异常。

Availability: some flavors of Unix.

os.kill(pid, sig)

将信号 sig 发送至进程 pid。特定平台上可用的信号常量定义在 signal 模块中。

Windows： signal.CTRL_C_EVENT 和 signal.CTRL_BREAK_EVENT 信号是特殊信号，只能发送给共享同一个控制台窗口的控制台进程，如某些子进程。sig 取任何其他值将导致该进程被 TerminateProcess API 无条件终止，且退出代码为 sig。Windows 版本的 kill() 还需要传入待结束进程的句柄。

另请参阅 signal.pthread_kill()。

3.2 新版功能: Windows 支持。

os.killpg(pgid, sig)

将信号 sig 发送给进程组 pgid。

Availability: Unix.

os.nice(increment)

将进程的优先级（nice 值）增加 increment，返回新的 nice 值。

Availability: Unix.

os.plock(op)

将程序段锁定到内存中。op 的值（定义在 <sys/lock.h> 中）决定了哪些段被锁定。

Availability: Unix.

os.popen(cmd, mode='r', buffering=-1)

打开一个管道，它通往 / 接受自命令 cmd。返回值是连接到管道的文件对象，根据 mode 是 'r' （默认）还是 'w' 决定该对象可以读取还是写入。buffering 参数与内置函数 open() 相应的参数含义相同。返回的文件对象只能读写文本字符串，不能是字节类型。

如果子进程成功退出，则 close 方法返回 None。如果发生错误，则返回子进程的返回码。在 POSIX 系统上，如果返回码为正，则它就是进程返回值左移一个字节后的值。如果返回码为负，则进程是被信号终止的，返回码取反后就是该信号。（例如，如果子进程被终止，则返回值可能是 - signal.SIGKILL。）在 Windows 系统上，返回值包含子进程的返回码（有符号整数）。

本方法是使用 subprocess.Popen 实现的，如需更强大的方法来管理和沟通子进程，请参阅该类的文档。

os.spawnl(mode, path, ...)

os.spawnle(mode, path, ..., env)

os.spawnlp(mode, file, ...)

os.spawnlpe(mode, file, ..., env)

os.spawnv(mode, path, args)

os.spawnve(mode, path, args, env)

os.spawnvp(mode, file, args)

os.spawnvpe(mode, file, args, env)

在新进程中执行程序 path。

（注意，subprocess 模块提供了更强大的工具来生成新进程并跟踪执行结果，使用该模块比使用这些函数更好。尤其应当检查 使用 subprocess 模块替换旧函数 部分。）

mode 为 P_NOWAIT 时，本函数返回新进程的进程号。mode 为 P_WAIT 时，如果进程正常退出，返回退出代码，如果被杀死，返回 -signal，其中 signal 是杀死进程的信号。在 Windows 上，进程号实际上是进程句柄，因此可以与 waitpid() 函数一起使用。

spawn* 函数的 “l” 和 “v” 变体不同在于命令行参数的传递方式。如果在编码时固定了参数数量，则 “l” 变体可能是最方便的，各参数作为 spawnl*() 函数的附加参数传入即可。当参数数量可变时，”v” 变体更方便，参数以列表或元组的形式作为 args 参数传递。在这两种情况下，子进程的第一个参数都必须是即将运行的命令名称。

结尾包含第二个 “p” 的变体（spawnlp()、spawnlpe()、spawnvp() 和 spawnvpe()）将使用 PATH 环境变量来查找程序 file。当环境被替换时（使用下一段讨论的 spawn*e 变体之一），PATH 变量将来自于新环境。其他变体 spawnl()、spawnle()、spawnv() 和 spawnve() 不使用 PATH 变量来查找程序，因此 path 必须包含正确的绝对或相对路径。

对于 spawnle()、spawnlpe()、spawnve() 和 spawnvpe() （都以 “e” 结尾），env 参数是一个映射，用于定义新进程的环境变量（代替当前进程的环境变量）。而函数 spawnl()、spawnlp()、spawnv() 和 spawnvp() 会将当前进程的环境变量过继给新进程。注意，env 字典中的键和值必须是字符串。无效的键或值将导致函数出错，返回值为 127。

例如，以下对 spawnlp() 和 spawnvpe() 的调用是等效的:

import os
os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')

L = ['cp', 'index.html', '/dev/null']
os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)

Availability: Unix, Windows. spawnlp(), spawnlpe(), spawnvp() and spawnvpe() are not available on Windows. spawnle() and spawnve() are not thread-safe on Windows; we advise you to use the subprocess module instead.

在 3.6 版更改: 接受一个 path-like object。

os.P_NOWAIT

os.P_NOWAITO

spawn* 系列函数的 mode 参数的可取值。如果给出这些值中的任何一个，则 spawn*() 函数将在创建新进程后立即返回，且返回值为进程号。

Availability: Unix, Windows.

os.P_WAIT

spawn* 系列函数的 mode 参数的可取值。如果将 mode 指定为该值，则 spawn*() 函数将在新进程运行完毕后返回，运行成功则返回进程的退出代码，被信号终止则返回 -signal。

Availability: Unix, Windows.

os.P_DETACH

os.P_OVERLAY

spawn* 系列函数的 mode 参数的可取值。它们比上面列出的值可移植性差。P_DETACH 与 P_NOWAIT 相似，但是新进程会与父进程的控制台脱离。使用 P_OVERLAY 则会替换当前进程，spawn* 函数将不会返回。

Availability: Windows.

os.startfile(path[, operation])

使用已关联的应用程序打开文件。

当 operation 未指定或指定为 'open' 时，这类似于在 Windows 资源管理器中双击文件，或在交互式命令行中将文件名作为 start 命令的参数：通过扩展名相关联的应用程序（如果有）打开文件。

当指定另一个 operation 时，它必须是一个“命令动词” (“command verb”)，该词指定对文件执行的操作。Microsoft 文档中的常用动词有 'print' 和 'edit' （用于文件），以及 'explore' 和 'find' （用于目录）。

关联的应用程序启动后 startfile() 就会立即返回。本函数没有等待应用程序关闭的选项，也没有办法检索应用程序的退出状态。path 参数是基于当前目录的相对路径。如果要使用绝对路径，请确保第一个字符不是斜杠 ('/')，是斜杠的话底层的 Win32 ShellExecute() 函数将失效。使用 os.path.normpath() 函数确保路径已针对 Win32 正确编码。

为了减少解释器的启动开销，直到第一次调用本函数后，才解析 Win32 ShellExecute() 函数。如果无法解析该函数​​，则抛出 NotImplementedError 异常。

Availability: Windows.

os.system(command)

在子 shell 中执行命令（字符串）。这是调用标准 C 函数 system() 来实现的，因此限制条件与该函数相同。对 sys.stdin 等的更改不会反映在执行命令的环境中。command 产生的任何输出将被发送到解释器标准输出流。

在 Unix 上，返回值是进程的退出状态，编码格式与为 wait() 指定的格式相同。注意，POSIX 没有指定 C 函数 system() 返回值的含义，因此 Python 函数的返回值与系统有关。

在 Windows 上，返回值是运行 command 后系统 Shell 返回的值。该 Shell 由 Windows 环境变量 COMSPEC: 给出：通常是 cmd.exe，它会返回命令的退出状态。在使用非原生 Shell 的系统上，请查阅 Shell 的文档。

subprocess 模块提供了更强大的工具来生成新进程并跟踪执行结果，使用该模块比使用本函数更好。参阅 subprocess 文档中的 使用 subprocess 模块替换旧函数 部分以获取有用的帮助。

Availability: Unix, Windows.

os.times()

返回当前的全局进程时间。返回值是一个有 5 个属性的对象：

user - 用户时间

system - 系统时间

children_user - 所有子进程的用户时间

children_system - 所有子进程的系统时间

elapsed - 从过去的固定时间点起，经过的真实时间

为了向后兼容，该对象的行为也类似于五元组，按照 user，system，children_user，children_system 和 elapsed 顺序组成。

See the Unix manual page times(2) or the corresponding Windows Platform API documentation. On Windows, only user and system are known; the other attributes are zero.

Availability: Unix, Windows.

在 3.3 版更改: 返回结果的类型由元组变成一个类似元组的对象，同时具有命名的属性。

os.wait()

等待子进程执行完毕，返回一个元组，包含其 pid 和退出状态指示：一个 16 位数字，其低字节是终止该进程的信号编号，高字节是退出状态码（信号编号为零的情况下），如果生成了核心文件，则低字节的高位会置位。

Availability: Unix.

os.waitid(idtype, id, options)

等待一个或多个子进程执行完毕。idtype 可以是 P_PID、P_PGID 或 P_ALL。id 指定要等待的 pid。options 是由 WEXITED、WSTOPPED 或 WCONTINUED 中的一个或多个进行或运算构造的，且额外可以与 WNOHANG 或 WNOWAIT 进行或运算。返回值是一个对象，对应着 siginfo_t 结构体中的数据，即： si_pid, si_uid, si_signo, si_status, si_code 或 None （如果指定了 WNOHANG 且没有子进程处于等待状态）。

Availability: Unix.

3.3 新版功能.

os.P_PID

os.P_PGID

os.P_ALL

waitid() 函数的 idtype 参数的可取值。它们影响 id 的解释方式。

Availability: Unix.

3.3 新版功能.

os.WEXITED

os.WSTOPPED

os.WNOWAIT

用于 waitid() 的 options 参数的标志位，指定要等待的子进程信号。

Availability: Unix.

3.3 新版功能.

os.CLD_EXITED

os.CLD_DUMPED

os.CLD_TRAPPED

os.CLD_CONTINUED

waitid() 返回的结果中，si_code 的可取值。

Availability: Unix.

3.3 新版功能.

os.waitpid(pid, options)

本函数的细节在 Unix 和 Windows 上有不同之处。

在 Unix 上：等待进程号为 pid 的子进程执行完毕，返回一个元组，内含其进程 ID 和退出状态指示（编码与 wait() 相同）。调用的语义受整数 options 的影响，常规操作下该值应为 0。

如果 pid 大于 0，则 waitpid() 会获取该指定进程的状态信息。如果 pid 为 0，则获取当前进程所在进程组中的所有子进程的状态。如果 pid 为 -1，则获取当前进程的子进程状态。如果 pid 小于 -1，则获取进程组 -pid （ pid 的绝对值）中所有进程的状态。

当系统调用返回 -1 时，将抛出带有错误码的 OSError 异常。

在 Windows 上：等待句柄为 pid 的进程执行完毕，返回一个元组，内含 pid 以及左移 8 位后的退出状态（移位简化了跨平台使用本函数）。小于或等于 0 的 pid 在 Windows 上没有特殊含义，且会抛出异常。整数值 options 无效。pid 可以指向任何 ID 已知的进程，不一定是子进程。调用 spawn* 函数时传入 P_NOWAIT 将返回合适的进程句柄。

在 3.5 版更改: 如果系统调用被中断，但信号处理程序没有触发异常，此函数现在会重试系统调用，而不是触发 InterruptedError 异常 (原因详见 PEP 475 [https://www.python.org/dev/peps/pep-0475])。

os.wait3(options)

与 waitpid() 相似，差别在于没有进程 ID 参数，且返回一个 3 元组，其中包括子进程 ID，退出状态指示和资源使用信息。关于资源使用信息的详情，请参考 resource.getrusage()。option 参数与传入 waitpid() 和 wait4() 的相同。

Availability: Unix.

os.wait4(pid, options)

与 waitpid() 相似，差别在本方法返回一个 3 元组，其中包括子进程 ID，退出状态指示和资源使用信息。关于资源使用信息的详情，请参考 resource.getrusage()。wait4() 的参数与 waitpid() 的参数相同。

Availability: Unix.

os.WNOHANG

用于 waitpid() 的选项，如果没有立即可用的子进程状态，则立即返回。在这种情况下，函数返回 (0, 0)。

Availability: Unix.

os.WCONTINUED

被作业控制停止的子进程，如果自上次报告状态以来，已继续进行，则此选项将报告这些子进程。

Availability: some Unix systems.

os.WUNTRACED

已停止的子进程，如果自停止以来尚未报告其当前状态，则此选项将报告这些子进程。

Availability: Unix.

下列函数采用进程状态码作为参数，状态码由 system()、wait() 或 waitpid() 返回。它们可用于确定进程上发生的操作。

os.WCOREDUMP(status)

如果为该进程生成了核心转储，返回 True，否则返回 False。

Availability: Unix.

os.WIFCONTINUED(status)

Return True if the process has been continued from a job control stop, otherwise return False.

Availability: Unix.

os.WIFSTOPPED(status)

Return True if the process has been stopped, otherwise return False.

Availability: Unix.

os.WIFSIGNALED(status)

Return True if the process exited due to a signal, otherwise return False.

Availability: Unix.

os.WIFEXITED(status)

Return True if the process exited using the exit(2) system call, otherwise return False.

Availability: Unix.

os.WEXITSTATUS(status)

If WIFEXITED(status) is true, return the integer parameter to the exit(2) system call. Otherwise, the return value is meaningless.

Availability: Unix.

os.WSTOPSIG(status)

返回导致进程停止的信号。

Availability: Unix.

os.WTERMSIG(status)

Return the signal which caused the process to exit.

Availability: Unix.

16.1.7. 调度器接口

这些函数控制操作系统如何为进程分配 CPU 时间。 它们仅在某些 Unix 平台上可用。 更多细节信息请查阅你所用 Unix 的指南页面。

3.3 新版功能.

以下调度策略如果被操作系统支持就会对外公开。

os.SCHED_OTHER

默认调度策略。

os.SCHED_BATCH

用于 CPU 密集型进程的调度策略，它会尽量为计算机中的其余任务保留交互性。

os.SCHED_IDLE

用于极低优先级的后台任务的调度策略。

os.SCHED_SPORADIC

用于偶发型服务程序的调度策略。

os.SCHED_FIFO

先进先出的调度策略。

os.SCHED_RR

循环式的调度策略。

os.SCHED_RESET_ON_FORK

此旗标可与任何其他调度策略进行 OR 运算。 当带有此旗标的进程设置分叉时，其子进程的调度策略和优先级会被重置为默认值。

class os.sched_param(sched_priority)

这个类表示在 sched_setparam(), sched_setscheduler() 和 sched_getparam() 中使用的可修改调度形参。 它属于不可变对象。

目前它只有一个可能的形参：

sched_priority

一个调度策略的调度优先级。

os.sched_get_priority_min(policy)

获取 policy 的最低优先级数值。 policy 是以上调度策略常量之一。

os.sched_get_priority_max(policy)

获取 policy 的最高优先级数值。 policy 是以上调度策略常量之一。

os.sched_setscheduler(pid, policy, param)

根据进程的 PID pid 设置其调度策略。pid 为 0 指的是调用本方法的进程。policy 是以上调度策略常量之一。param 是一个 sched_param 实例。

os.sched_getscheduler(pid)

返回 PID 为 pid 的进程的调度策略。pid 为 0 指的是调用本方法的进程。返回的结果是以上调度策略常量之一。

os.sched_setparam(pid, param)

设置 PID 为 pid 的进程的某个调度参数。pid 为 0 指的是调用本方法的进程。param 是一个 sched_param 实例。

os.sched_getparam(pid)

返回 PID 为 pid 的进程的调度参数为一个 sched_param 实例。pid 为 0 指的是调用本方法的进程。

os.sched_rr_get_interval(pid)

返回 PID 为 pid 的进程在时间片轮转调度下的时间片长度（单位为秒）。pid 为 0 指的是调用本方法的进程。

os.sched_yield()

自愿放弃 CPU。

os.sched_setaffinity(pid, mask)

将 PID 为 pid 的进程（为零则为当前进程）限制到一组 CPU 上。mask 是整数的可迭代对象，表示应将进程限制在其中的一组 CPU。

os.sched_getaffinity(pid)

返回 PID 为 pid 的进程（为零则为当前进程）被限制到的那一组 CPU。

16.1.8. 其他系统信息

os.confstr(name)

返回字符串格式的系统配置信息。name 指定要查找的配置名称，它可以是字符串，是一个系统已定义的名称，这些名称定义在不同标准（POSIX，Unix 95，Unix 98 等）中。一些平台还定义了额外的其他名称。当前操作系统已定义的名称在 confstr_names 字典的键中给出。对于未包含在该映射中的配置名称，也可以传递一个整数作为 name。

如果 name 指定的配置值未定义，返回 None。

如果 name 是一个字符串且不是已定义的名称，将抛出 ValueError 异常。如果当前系统不支持 name 指定的配置名称，即使该名称存在于 confstr_names，也会抛出 OSError 异常，错误码为 errno.EINVAL。

Availability: Unix.

os.confstr_names

字典，表示映射关系，为 confstr() 可接受名称与操作系统为这些名称定义的整数值之间的映射。这可用于判断系统已定义了哪些名称。

Availability: Unix.

os.cpu_count()

返回系统的 CPU 数量。不确定则返回 None。

该数量不同于当前进程可以使用的CPU数量。可用的CPU数量可以由 len(os.sched_getaffinity(0)) 方法获得。

3.4 新版功能.

os.getloadavg()

返回系统运行队列中最近 1、5 和 15 分钟内的平均进程数。无法获得平均负载则抛出 OSError 异常。

Availability: Unix.

os.sysconf(name)

返回整数格式的系统配置信息。如果 name 指定的配置值未定义，返回 -1。对 confstr() 的 name 参数的注释在此处也适用。当前已知的配置名称在 sysconf_names 字典中提供。

Availability: Unix.

os.sysconf_names

字典，表示映射关系，为 sysconf() 可接受名称与操作系统为这些名称定义的整数值之间的映射。这可用于判断系统已定义了哪些名称。

Availability: Unix.

以下数据值用于支持对路径本身的操作。所有平台都有定义。

对路径的高级操作在 os.path 模块中定义。

os.curdir

操作系统用来表示当前目录的常量字符串。在 Windows 和 POSIX 上是 '.'。在 os.path 中也可用。

os.pardir

操作系统用来表示父目录的常量字符串。在 Windows 和 POSIX 上是 '..'。在 os.path 中也可用。

os.sep

操作系统用来分隔路径不同部分的字符。在 POSIX 上是 '/'，在 Windows 上是是 '\\'。注意，仅了解它不足以能解析或连接路径，请使用 os.path.split() 和 os.path.join()，但它有时是有用的。在 os.path 中也可用。

os.altsep

操作系统用来分隔路径不同部分的替代字符。如果仅存在一个分隔符，则为 None。在 sep 是反斜杠的 Windows 系统上，该值被设为 '/'。在 os.path 中也可用。

os.extsep

分隔基本文件名与扩展名的字符，如 os.py 中的 '.'。在 os.path 中也可用。

os.pathsep

操作系统通常用于分隔搜索路径（如 PATH）中不同部分的字符，如 POSIX 上是 ':'，Windows 上是 ';'。在 os.path 中也可用。

os.defpath

在环境变量没有 'PATH' 键的情况下，exec*p* and spawn*p* 使用的默认搜索路径。在 os.path 中也可用。

os.linesep

当前平台用于分隔（或终止）行的字符串。它可以是单个字符，如 POSIX 上是 '\n'，也可以是多个字符，如 Windows 上是 '\r\n'。在写入以文本模式（默认模式）打开的文件时，请不要使用 os.linesep 作为行终止符，请在所有平台上都使用一个 '\n' 代替。

os.devnull

空设备的文件路径。如 POSIX 上为 '/dev/null'，Windows 上为 'nul'。在 os.path 中也可用。

os.RTLD_LAZY

os.RTLD_NOW

os.RTLD_GLOBAL

os.RTLD_LOCAL

os.RTLD_NODELETE

os.RTLD_NOLOAD

os.RTLD_DEEPBIND

setdlopenflags() 和 getdlopenflags() 函数所使用的标志。请参阅 Unix 手册页 dlopen(3) 获取不同标志的含义。

3.3 新版功能.

16.1.9. 随机数

os.getrandom(size, flags=0)

获得最多为 size 的随机字节。本函数返回的字节数可能少于请求的字节数。

这些字节可用于为用户空间的随机数生成器提供种子，或用于加密目的。

getrandom() 依赖于从设备驱动程序和其他环境噪声源收集的熵。不必要地读取大量数据将对使用 /dev/random 和 /dev/urandom 设备的其他用户产生负面影响。

flags 参数是一个位掩码，可以是零个或多个下列值以或运算组合： os.GRND_RANDOM 和 GRND_NONBLOCK。

另请参阅 Linux getrandom() 手册页 [http://man7.org/linux/man-pages/man2/getrandom.2.html] 。

Availability: Linux 3.17 and newer.

3.6 新版功能.

os.urandom(size)

返回大小为 size 的字符串，它是适合加密使用的随机字节。

本函数从系统指定的随机源获取随机字节。对于加密应用程序，返回的数据应有足够的不可预测性，尽管其确切的品质取决于操作系统的实现。

在 Linux 上，如果 getrandom() 系统调用可用，它将以阻塞模式使用：阻塞直到系统的 urandom 熵池初始化完毕（内核收集了 128 位熵）。原理请参阅 PEP 524 [https://www.python.org/dev/peps/pep-0524]。在 Linux 上，getrandom() 可以以非阻塞模式使用（使用 GRND_NONBLOCK 标志）来获取随机字节，或者轮询直到系统 urandom 熵池初始化完毕。

在类 Unix 系统上，随机字节是从 /dev/urandom 设备读取的。如果 /dev/urandom 设备不可用或不可读，则抛出 NotImplementedError 异常。

在 Windows 上将使用 CryptGenRandom()。

参见

secrets 模块提供了更高级的功能。所在平台会提供随机数生成器，有关其易于使用的接口，请参阅 random.SystemRandom。

在 3.6.0 版更改: 在 Linux 上，getrandom() 现在以阻塞模式使用，以提高安全性。

在 3.5.2 版更改: 在 Linux 上，如果 getrandom() 系统调用阻塞（urandom 熵池尚未初始化完毕），则退回一步读取 /dev/urandom。

在 3.5 版更改: 在 Linux 3.17 和更高版本上，现在使用 getrandom() 系统调用（如果可用）。在 OpenBSD 5.6 和更高版本上，现在使用 getentropy() C 函数。这些函数避免了使用内部文件描述符。

os.GRND_NONBLOCK

默认情况下，从 /dev/random 读取时，如果没有可用的随机字节，则 getrandom() 会阻塞；从 /dev/urandom 读取时，如果熵池尚未初始化，则会阻塞。

如果设置了 GRND_NONBLOCK 标志，则这些情况下 getrandom() 不会阻塞，而是立即抛出 BlockingIOError 异常。

3.6 新版功能.

os.GRND_RANDOM

如果设置了此标志位，那么将从 /dev/random 池而不是 /dev/urandom 池中提取随机字节。

3.6 新版功能.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

16.2. io — 处理流的核心工具

源代码: Lib/io.py [https://github.com/python/cpython/tree/3.6/Lib/io.py]

16.2.1. 概述

io 模块提供了 Python 用于处理各种 I/O 类型的主要工具。三种主要的 I/O类型分别为: 文本 I/O, 二进制 I/O 和 原始 I/O。这些是泛型类型，有很多种后端存储可以用在他们上面。一个隶属于任何这些类型的具体对象被称作 file object。 其他同类的术语还有 流 和 类文件对象。

独立于其类别，每个具体流对象也将具有各种功能：它可以是只读，只写或读写。它还可以允许任意随机访问（向前或向后寻找任何位置），或仅允许顺序访问（例如在套接字或管道的情况下）。

All streams are careful about the type of data you give to them. For example giving a str object to the write() method of a binary stream will raise a TypeError. So will giving a bytes object to the write() method of a text stream.

在 3.3 版更改: 由于 IOError 现在是 OSError 的别名，因此用于引发 IOError 的操作现在会引发 OSError 。

16.2.1.1. 文本 I/O

文本I/O预期并生成 str 对象。这意味着，无论何时后台存储是由字节组成的（例如在文件的情况下），数据的编码和解码都是透明的，并且可以选择转换特定于平台的换行符。

创建文本流的最简单方法是使用 open()，可以选择指定编码：

f = open("myfile.txt", "r", encoding="utf-8")

内存中文本流也可以作为 StringIO 对象使用：

f = io.StringIO("some initial text data")

TextIOBase 的文档中详细描述了文本流的API

16.2.1.2. 二进制 I/O

二进制I/O（也称为缓冲I/O）预期 bytes-like objects 并产生 bytes 对象。不执行编码、解码或换行转换。这种类型的流可以用于所有类型的非文本数据，并且还可以在需要手动控制文本数据的处理时使用。

创建二进制流的最简单方法是使用 open()，并在模式字符串中指定 'b' ：

f = open("myfile.jpg", "rb")

内存中二进制流也可以作为 BytesIO 对象使用：

f = io.BytesIO(b"some initial binary data: \x00\x01")

BufferedIOBase 的文档中详细描述了二进制流 API。

其他库模块可以提供额外的方式来创建文本或二进制流。参见 socket.socket.makefile() 的示例。

16.2.1.3. 原始 I/O

原始 I/O（也称为 非缓冲 I/O）通常用作二进制和文本流的低级构建块。用户代码直接操作原始流的用法非常罕见。不过，可以通过在禁用缓冲的情况下以二进制模式打开文件来创建原始流：

f = open("myfile.jpg", "rb", buffering=0)

The raw stream API is described in detail in the docs of RawIOBase.

16.2.2. 高阶模块接口

io.DEFAULT_BUFFER_SIZE

包含模块缓冲 I/O 类使用的默认缓冲区大小的 int。 在可能的情况下 open() 将使用文件的 blksize（由 os.stat() 获得）。

io.open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

这是内置的 open() 函数的别名。

exception io.BlockingIOError

这是内置的 BlockingIOError 异常的兼容性别名。

exception io.UnsupportedOperation

在流上调用不支持的操作时引发的继承 OSError 和 ValueError 的异常。

16.2.2.1. 内存中的流

也可以使用 str 或 bytes-like object 作为文件进行读取和写入。对于字符串， StringIO 可以像在文本模式下打开的文件一样使用。 BytesIO 可以像以二进制模式打开的文件一样使用。两者都提供完整的随机读写功能。

参见

sys

包含标准IO流: sys.stdin, sys.stdout 和 sys.stderr 。

16.2.3. 类的层次结构

I/O 流的实现被组织为类的层次结构。 首先是 抽象基类 (ABC)，用于指定流的各种类别，然后是提供标准流实现的具体类。

注解

抽象基类还提供某些方法的默认实现，以帮助实现具体的流类。例如 BufferedIOBase 提供了 readinto() 和 readline() 的未优化实现。

I/O层次结构的顶部是抽象基类 IOBase 。它定义了流的基本接口。但是请注意，对流的读取和写入之间没有分隔。如果实现不支持指定的操作，则会引发 UnsupportedOperation 。

RawIOBase ABC 是 IOBase 的子类。它负责将字节读取和写入流中。 RawIOBase 的子类 FileIO 提供计算机文件系统中文件的接口。

BufferedIOBase ABC处理原始字节流 ( RawIOBase) 上的缓冲。其子类 BufferedWriter、BufferedReader 和 BufferedRWPair 缓冲流是可读、可写以及可读写的。 BufferedRandom 为随机访问流提供缓冲接口。 BufferedIOBase 的另一个子类 BytesIO 是内存中字节流。

TextIOBase ABC是 IOBase 的另一个子类，它处理字节表示文本的流，并处理字符串之间的编码和解码。 TextIOWrapper 是其一个扩展，它是原始缓冲流（ BufferedIOBase ）的缓冲文本接口。最后， StringIO 是一个用于文本的内存流。

参数名不是规范的一部分，只有 open() 的参数才用作关键字参数。

下表总结了抽象基类提供的 io 模块：

抽象基类

继承

Stub 方法

Mixin方法和属性

IOBase

fileno, seek, 和 truncate

close, closed, __enter__, __exit__, flush, isatty, __iter__, __next__, readable, readline, readlines, seekable, tell, writable, 和 writelines

RawIOBase

IOBase

readinto 和 write

继承 IOBase 方法, read, 和 readall

BufferedIOBase

IOBase

detach, read, read1, 和 write

继承 IOBase 方法, readinto, 和 readinto1

TextIOBase

IOBase

detach, read, readline, 和 write

继承 IOBase 方法, encoding, errors, 和 newlines

16.2.3.1. I/O 基类

class io.IOBase

所有 I/O 类的抽象基类，作用于字节流。没有公共构造函数。

此类为许多方法提供了空的抽象实现，派生类可以有选择地重写。默认实现无法读取、写入或查找的文件。

Even though IOBase does not declare read(), readinto(), or write() because their signatures will vary, implementations and clients should consider those methods part of the interface. Also, implementations may raise a ValueError (or UnsupportedOperation) when operations they do not support are called.

The basic type used for binary data read from or written to a file is bytes. Other bytes-like objects are accepted as method arguments too. In some cases, such as readinto(), a writable object such as bytearray is required. Text I/O classes work with str data.

请注意，在关闭的流上调用任何方法（甚至查询）都是未定义的（undefined）。在这种情况下，实现可能会引发 ValueError 。

IOBase （及其子类）支持迭代器协议，这意味着可以迭代 IOBase 对象以产生流中的行。根据流是二进制流（产生字节）还是文本流（产生字符串），行的定义略有不同。请参见下面的 readline() 。

IOBase is also a context manager and therefore supports the with statement. In this example, file is closed after the with statement’s suite is finished—even if an exception occurs:

with open('spam.txt', 'w') as file:
    file.write('Spam and eggs!')

IOBase 提供以下数据属性和方法：

close()

刷新并关闭此流。如果文件已经关闭，则此方法无效。文件关闭后，对文件的任何操作（例如读取或写入）都会引发 ValueError 。

为方便起见，允许多次调用此方法。但是，只有第一个调用才会生效。

closed

如果流关闭，则为True。

fileno()

返回流的基础文件描述符（整数）—如果存在。如果 IO 对象不使用文件描述符，则会引发 OSError 。

flush()

刷新流的写入缓冲区（如果适用）。这对只读和非阻塞流不起作用。

isatty()

如果流是交互式的（即连接到终端/tty设备），则返回 True 。

readable()

如果可以读取流，则返回 True 。否则为 False ，且 read() 将引发 OSError 错误。

readline(size=-1)

从流中读取并返回一行。 如果指定了 size，将至多读取 size 个字节。

对于二进制文件行结束符总是 b'\n'；对于文本文件，可以用将 newline 参数传给 open() 的方式来选择要识别的行结束符。

readlines(hint=-1)

从流中读取并返回包含多行的列表。 可以指定 hint 来控制要读取的行数：如果（以字节/字符数表示的）所有行的总大小超出了 hint 则将不会读取更多的行。

请注意使用 for line in file: ... 就足够对文件对象进行迭代了，可以不必调用 file.readlines()。

seek(offset[, whence])

将流位置修改到给定的字节 offset。 offset 将相对于由 whence 指定的位置进行解析。 whence 的默认值为 SEEK_SET。 whence 的可用值有:

SEEK_SET 或 0 – 流的开头（默认值）；offset 应为零或正值

SEEK_CUR or 1 – 当前流位置；offset 可以为负值

SEEK_END or 2 – 流的末尾；offset 通常为负值

返回新的绝对位置。

3.1 新版功能: SEEK_* 常量.

3.3 新版功能: 某些操作系统还可支持其他的值，例如 os.SEEK_HOLE 或 os.SEEK_DATA。 特定文件的可用值还会取决于它是以文本还是二进制模式打开。

seekable()

如果流支持随机访问则返回 True。 如为 False，则 seek(), tell() 和 truncate() 将引发 OSError。

tell()

返回当前流的位置。

truncate(size=None)

将流的大小调整为给定的 size 个字节（如果未指定 size 则调整至当前位置）。 当前的流位置不变。 这个调整操作可扩展或减小当前文件大小。 在扩展的情况下，新文件区域的内容取决于具体平台（在大多数系统上，额外的字节会填充为零）。 返回新的文件大小。

在 3.5 版更改: 现在Windows在扩展时将文件填充为零。

writable()

如果流支持写入则返回 True。 如为 False，则 write() 和 truncate() 将引发 OSError。

writelines(lines)

将行列表写入到流。 不会添加行分隔符，因此通常所提供的每一行都带有末尾行分隔符。

__del__()

为对象销毁进行准备。 IOBase 提供了此方法的默认实现，该实现会调用实例的 close() 方法。

class io.RawIOBase

原始二进制 I/O 的基类。 它继承自 IOBase。 没有公共构造器。

原始二进制 I/O 通常提供对下层 OS 设备或 API 的低层级访问，而不尝试将其封装到高层级的基元中（这是留给缓冲 I/O 和 Text I/O 的，将在下文中描述）。

在 IOBase 的属性和方法之外，RawIOBase 还提供了下列方法:

read(size=-1)

从对象中读取 size 个字节并将其返回。 作为一个便捷选项，如果 size 未指定或为 -1，则返回所有字节直到 EOF。 在其他情况下，仅会执行一次系统调用。 如果操作系统调用返回字节数少于 size 则此方法也可能返回少于 size 个字节。

如果返回 0 个字节而 size 不为零 0，这表明到达文件末尾。 如果处于非阻塞模式并且没有更多字节可用，则返回 None。

默认实现会转至 readall() 和 readinto()。

readall()

从流中读取并返回所有字节直到 EOF，如有必要将对流执行多次调用。

readinto(b)

Read bytes into a pre-allocated, writable bytes-like object b, and return the number of bytes read. If the object is in non-blocking mode and no bytes are available, None is returned.

write(b)

将给定的 bytes-like object b 写入到下层的原始流，并返回所写入的字节数。 这可以少于 b 的总字节数，具体取决于下层原始流的设定，特别是如果它处于非阻塞模式的话。 如果原始流设为非阻塞并且不能真正向其写入单个字节时则返回 None。 调用者可以在此方法返回后释放或改变 b，因此该实现应该仅在方法调用期间访问 b。

class io.BufferedIOBase

支持某种缓冲的二进制流的基类。 它继承自 IOBase。 没有公共构造器。

与 RawIOBase 的主要差别在于 read(), readinto() 和 write() 等方法将（分别）尝试按照要求读取尽可能多的输入或是耗尽所有给定的输出，其代价是可能会执行一次以上的系统调用。

除此之外，那些方法还可能引发 BlockingIOError，如果下层的原始数据流处于非阻塞模式并且无法接受或给出足够数据的话；不同于对应的 RawIOBase 方法，它们将永远不会返回 None。

并且，read() 方法也没有转向 readinto() 的默认实现。

典型的 BufferedIOBase 实现不应当继承自 RawIOBase 实现，而要包装一个该实现，正如 BufferedWriter 和 BufferedReader 所做的那样。

BufferedIOBase 在 IOBase 的现有成员以外还提供或重载了下列方法和属性:

raw

由 BufferedIOBase 处理的下层原始流 (RawIOBase 的实例)。 它不是 BufferedIOBase API 的组成部分并且不存在于某些实现中。

detach()

从缓冲区分离出下层原始流并将其返回。

在原始流被分离之后，缓冲区将处于不可用的状态。

某些缓冲区例如 BytesIO 并无可从此方法返回的单独原始流的概念。 它们将会引发 UnsupportedOperation。

3.1 新版功能.

read(size=-1)

读取并返回最多 size 个字节。 如果此参数被省略、为 None 或为负值，则读取并返回所有数据直到 EOF。 如果流已经到达 EOF 则返回一个空的 bytes 对象。

如果此参数为正值，并且下层原始流不可交互，则可能发起多个原始读取以满足字节计数（直至先遇到 EOF）。 但对于可交互原始流，则将至多发起一个原始读取，并且简短的结果并不意味着已到达 EOF。

BlockingIOError 会在下层原始流不处于阻塞模式，并且当前没有可用数据时被引发。

read1(size=-1)

通过至多一次对下层流的 read() (或 readinto()) 方法的调用读取并返回至多 size 个字节。 这适用于在 BufferedIOBase 对象之上实现你自己的缓冲区的情况。

readinto(b)

Read bytes into a pre-allocated, writable bytes-like object b and return the number of bytes read.

类似于 read()，可能对下层原始流发起多次读取，除非后者为交互式。

BlockingIOError 会在下层原始流不处于阻塞模式，并且当前没有可用数据时被引发。

readinto1(b)

将字节数据读入预先分配的可写 bytes-like object b，其中至多使用一次对下层原始流 read() (或 readinto()) 方法的调用。 返回所读取的字节数。

BlockingIOError 会在下层原始流不处于阻塞模式，并且当前没有可用数据时被引发。

3.5 新版功能.

write(b)

写入给定的 bytes-like object b，并返回写入的字节数 (总是等于 b 的字节长度，因为如果写入失败则会引发 OSError)。 根据具体实现的不同，这些字节可能被实际写入下层流，或是出于运行效率和冗余等考虑而暂存于缓冲区。

当处于非阻塞模式时，如果需要将数据写入原始流但它无法在不阻塞的情况下接受所有数据则将引发 BlockingIOError。

调用者可能会在此方法返回后释放或改变 b，因此该实现应当仅在方法调用期间访问 b。

16.2.3.2. 原始文件 I/O

class io.FileIO(name, mode='r', closefd=True, opener=None)

FileIO 代表在 OS 层级上包含文件的字节数据。 它实现了 RawIOBase 接口（因而也实现了 IOBase 接口）。

name 可以是以下两项之一：

代表将被打开的文件路径的字符串或 bytes 对象。 在此情况下 closefd 必须为 True (默认值) 否则将会引发异常。

代表一个现有 OS 层级文件描述符的号码的整数，作为结果的 FileIO 对象将可访问该文件。 当 FileIO 对象被关闭时此 fd 也将被关闭，除非 closefd 设为 False。

mode 可以为 'r', 'w', 'x' 或 'a' 分别表示读取（默认模式）、写入、独占新建或添加。 如果以写入或添加模式打开的文件不存在将自动新建；当以写入模式打开时文件将先清空。 以新建模式打开时如果文件已存在则将引发 FileExistsError。 以新建模式打开文件也意味着要写入，因此该模式的行为与 'w' 类似。 在模式中附带 '+' 将允许同时读取和写入。

该类的 read() (当附带正值参数调用时), readinto() 和 write() 方法将只执行一次系统调用。

可以通过传入一个可调用对象作为 opener 来使用自定义文件打开器。 然后通过调用 opener 并传入 (name, flags) 来获取文件对象所对应的下层文件描述符。 opener 必须返回一个打开文件描述符（传入 os.open 作为 opener 的结果在功能上将与传入 None 类似）。

新创建的文件是 不可继承的。

有关 opener 参数的示例，请参见内置函数 open() 。

在 3.3 版更改: 增加了 opener 参数。增加了 'x' 模式。

在 3.4 版更改: 文件现在禁止继承。

在 IOBase 和 RawIOBase 的属性和方法之外，FileIO 还提供了下列数据属性:

mode

构造函数中给定的模式。

name

文件名。当构造函数中没有给定名称时，这是文件的文件描述符。

16.2.3.3. 缓冲流

相比原始 I/O，缓冲 I/O 流提供了针对 I/O 设备的更高层级接口.

class io.BytesIO([initial_bytes])

一个使用内存字节缓冲区的流实现。 它继承自 BufferedIOBase。 在 close() 方法被调用时将会丢弃缓冲区。

可选参数 initial_bytes 是一个包含初始数据的 bytes-like object。

BytesIO 在继承自 BufferedIOBase 和 IOBase 的成员以外还提供或重载了下列方法:

getbuffer()

返回一个对应于缓冲区内容的可读写视图而不必拷贝其数据。 此外，改变视图将透明地更新缓冲区内容:

>>> b = io.BytesIO(b"abcdef")
>>> view = b.getbuffer()
>>> view[2:4] = b"56"
>>> b.getvalue()
b'ab56ef'

注解

只要视图保持存在，BytesIO 对象就无法被改变大小或关闭。

3.2 新版功能.

getvalue()

返回包含整个缓冲区内容的 bytes。

read1()

In BytesIO, this is the same as read().

readinto1()

In BytesIO, this is the same as readinto().

3.5 新版功能.

class io.BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)

一个提供对可读的序列型 RawIOBase 对象更高层级访问的缓冲区。 它继承自 BufferedIOBase。 当从此对象读取数据时，可能会从下层原始流请求更大量的数据，并存放到内部缓冲区中。 接下来可以在后续读取时直接返回缓冲数据。

根据给定的可读 raw 流和 buffer_size 创建 BufferedReader 的构造器。 如果省略 buffer_size，则会使用 DEFAULT_BUFFER_SIZE。

BufferedReader 在继承自 BufferedIOBase 和 IOBase 的成员以外还提供或重载了下列方法:

peek([size])

从流返回字节数据而不前移位置。 完成此调用将至多读取一次原始流。 返回的字节数量可能少于或多于请求的数量。

read([size])

读取并返回 size 个字节，如果 size 未给定或为负值，则读取至 EOF 或是在非阻塞模式下读取调用将会阻塞。

read1(size)

在原始流上通过单次调用读取并返回至多 size 个字节。 如果至少缓冲了一个字节，则只返回缓冲的字节。 在其他情况下，将执行一次原始流读取。

class io.BufferedWriter(raw, buffer_size=DEFAULT_BUFFER_SIZE)

一个提供对可读的序列型 RawIOBase 对象更高层级访问的缓冲区。 它继承自 BufferedIOBase。 当写入到此对象时，数据通常会被放入到内部缓冲区中。 缓冲区将在满足某些条件的情况下被写到下层的 RawIOBase 对象，包括:

当缓冲区对于所有挂起数据而言太小时；

当 flush() 被调用时

当（为 BufferedRandom 对象）请求 seek() 时；

当 BufferedWriter 对象被关闭或销毁时。

该构造器会为给定的可写 raw 流创建一个 BufferedWriter。 如果未给定 buffer_size，则使用默认的 DEFAULT_BUFFER_SIZE。

BufferedWriter 在继承自 BufferedIOBase 和 IOBase 的成员以外还提供或重载了下列方法:

flush()

将缓冲区中保存的字节数据强制放入原始流。 如果原始流发生阻塞则应当引发 BlockingIOError。

write(b)

写入 bytes-like object b 并返回写入的字节数。 当处于非阻塞模式时，如果缓冲区需要被写入但原始流发生阻塞则将引发 BlockingIOError。

class io.BufferedRandom(raw, buffer_size=DEFAULT_BUFFER_SIZE)

随机访问流的带缓冲的接口。 它继承自 BufferedReader 和 BufferedWriter，并进一步支持 seek() 和 tell() 功能。

该构造器会为在第一个参数中给定的可查找原始流创建一个读取器和定稿器。 如果省略 buffer_size 则使用默认的 DEFAULT_BUFFER_SIZE。

BufferedRandom 能做到 BufferedReader 或 BufferedWriter 所能做的任何事。

class io.BufferedRWPair(reader, writer, buffer_size=DEFAULT_BUFFER_SIZE)

一个带缓冲的 I/O 对象，它将两个单向 RawIOBase 对象 – 一个可读，另一个可写 – 组合为单个双向端点。 它继承自 BufferedIOBase。

reader 和 writer 分别是可读和可写的 RawIOBase 对象。 如果省略 buffer_size 则使用默认的 DEFAULT_BUFFER_SIZE。

BufferedRWPair 实现了 BufferedIOBase 的所有方法，但 detach() 除外，调用该方法将引发 UnsupportedOperation。

警告

BufferedRWPair 不会尝试同步访问其下层的原始流。 你不应当将传给它与读取器和写入器相同的对象；而要改用 BufferedRandom。

16.2.3.4. 文本 I/O

class io.TextIOBase

Base class for text streams. This class provides a character and line based interface to stream I/O. There is no readinto() method because Python’s character strings are immutable. It inherits IOBase. There is no public constructor.

TextIOBase 在来自 IOBase 的成员以外还提供或重载了以下数据属性和方法:

encoding

用于将流的字节串解码为字符串以及将字符串编码为字节串的编码格式名称。

errors

解码器或编码器的错误设置。

newlines

一个字符串、字符串元组或者 None，表示目前已经转写的新行。 根据具体实现和初始构造器旗标的不同，此属性或许会不可用。

buffer

由 TextIOBase 处理的下层二进制缓冲区（为一个 BufferedIOBase 的实例）。 它不是 TextIOBase API 的组成部分并且不存在于某些实现中。

detach()

从 TextIOBase 分离出下层二进制缓冲区并将其返回。

在下层缓冲区被分离后，TextIOBase 将处于不可用的状态。

某些 TextIOBase 的实现，例如 StringIO 可能并无下层缓冲区的概念，因此调用此方法将引发 UnsupportedOperation。

3.1 新版功能.

read(size=-1)

从流中读取至多 size 个字符并以单个 str 的形式返回。 如果 size 为负值或 None，则读取至 EOF。

readline(size=-1)

读取至换行符或 EOF 并返回单个 str。 如果流已经到达 EOF，则将返回一个空字符串。

如果指定了 size ，最多将读取 size 个字符。

seek(offset[, whence])

将流位置改为给定的偏移位置 offset。 具体行为取决于 whence 形参。 whence 的默认值为 SEEK_SET。

SEEK_SET 或 0: 从流的开始位置起查找（默认值）；offset 必须为 TextIOBase.tell() 所返回的数值或为零。 任何其他 offset 值都将导致未定义的行为。

SEEK_CUR 或 1: “查找” 到当前位置；offset 必须为零，表示无操作（所有其他值均不受支持）。

SEEK_END 或 2: 查找到流的末尾；offset 必须为零（所有其他值均不受支持）。

以数字形式返回新的绝对位置。

3.1 新版功能: SEEK_* 常量.

tell()

以不透明数字形式返回当前流的位置。 该数字通常并不代表下层二进制存储中对应的字节数。

write(s)

将字符串 s 写入到流并返回写入的字符数。

class io.TextIOWrapper(buffer, encoding=None, errors=None, newline=None, line_buffering=False, write_through=False)

一个基于 BufferedIOBase 二进制流的缓冲文本流。 它继承自 TextIOBase。

encoding 给出流被解码或编码时将使用的编码格式。 它默认为 locale.getpreferredencoding(False)。

errors 是一个可选的字符串，它指明编码格式和编码格式错误的处理方式。 传入 'strict' 将在出现编码格式错误时引发 ValueError (默认值 None 具有相同的效果)，传入 'ignore' 将忽略错误。 (请注意忽略编码格式错误会导致数据丢失。) 'replace' 会在出现错误数据时插入一个替换标记 (例如 '?')。 'backslashreplace' 将把错误数据替换为一个反斜杠转义序列。 在写入时，还可以使用 'xmlcharrefreplace' (替换为适当的 XML 字符引用) 或 'namereplace' (替换为 \N{...} 转义序列)。 任何其他通过 codecs.register_error() 注册的错误处理方式名称也可以被接受。

newline 控制行结束符处理方式。 它可以为 None, '', '\n', '\r' 和 '\r\n'。 其工作原理如下:

当从流中读取输入时，如果 newline 为 None，则会启用 universal newlines 模式。 输入中的行结束符可以为 '\n', '\r' 或 '\r\n'，在返回给调用者之前它们会被统一转写为 '\n'。 如果参数为 ''，也会启用通用换行模式，但行结束符会不加转写即返回给调用者。 如果它具有任何其他合法的值，则输入行将仅由给定的字符串结束，并且行结束符会不加转写即返回给调用者。

将输出写入流时，如果 newline 为 None，则写入的任何 '\n' 字符都将转换为系统默认行分隔符 os.linesep。如果 newline 是 '' 或 '\n'，则不进行翻译。如果 newline 是任何其他合法值，则写入的任何 '\n' 字符将被转换为给定的字符串。

如果 line_buffering 为 True，则当一个写入调用包含换行符或回车时将会应用 flush()。

如果 write_through 为 True，对 write() 的调用会确保不被缓冲：在 TextIOWrapper 对象上写入的任何数据会立即交给其下层的 buffer 来处理。

在 3.3 版更改: 已添加 write_through 参数

在 3.3 版更改: 默认的 encoding 现在将为 locale.getpreferredencoding(False) 而非 locale.getpreferredencoding()。 不要使用 locale.setlocale() 来临时改变区域编码格式，要使用当前区域编码格式而不是用户的首选编码格式。

TextIOWrapper provides one attribute in addition to those of TextIOBase and its parents:

line_buffering

是否启用行缓冲。

class io.StringIO(initial_value='', newline='\n')

用于文本 I/O 的内存数据流。 当调用 close() 方法时将会丢弃文本缓冲区。

缓冲区的初始值可通过提供 initial_value 来设置。 如果启用了行结束符转写，换行将以 write() 所用的方式被编码。 数据流位置将被设为缓冲区的开头。

newline 参数的规则与 TextIOWrapper 所用的一致。 默认规则是仅将 \n 字符视为行结束符并且不执行换行符转写。 如果 newline 设为 None，在所有平台上换行符都将被写入为 \n，但当读取时仍然会执行通用换行编码格式。

StringIO 在继承自 TextIOBase 及其父类的现有成员以外还提供了以下方法:

getvalue()

返回一个包含缓冲区全部内容的 str。 换行符会以与 read() 相同的方式被编码，但是流的位置不会被改变。

用法示例：

import io

output = io.StringIO()
output.write('First line.\n')
print('Second line.', file=output)

# Retrieve file contents -- this will be
# 'First line.\nSecond line.\n'
contents = output.getvalue()

# Close object and discard memory buffer --
# .getvalue() will now raise an exception.
output.close()

class io.IncrementalNewlineDecoder

用于在 universal newlines 模式下解码换行符的辅助编解码器。 它继承自 codecs.IncrementalDecoder。

16.2.4. 性能

本节讨论所提供的具体 I/O 实现的性能。

16.2.4.1. 二进制 I/O

即使在用户请求单个字节时，也只读取和写入大块数据。通过该方法，缓冲 I/O 也隐藏了调用和执行操作系统无缓冲 I/O 例程时的任何低效性。。增益取决于操作系统和执行的 I/O 类型。例如，在某些现代操作系统上（例如Linux），无缓冲磁盘 I/O 可以与缓冲 I/O 一样快。但最重要的是，无论平台和支持设备如何，缓冲 I/O 都能提供可预测的性能。因此，对于二进制数据，几乎总是首选使用缓冲的I / O而不是未缓冲的I / O。

16.2.4.2. 文本 I/O

二进制存储（如文件）上的文本 I/O 比同一存储上的二进制 I/O 慢得多，因为它需要使用字符编解码器在Unicode和二进制数据之间进行转换。这在处理大量文本数据（如大型日志文件）时会变得非常明显。此外，由于使用的重构算法 TextIOWrapper.tell() 和 TextIOWrapper.seek() 都相当慢。

StringIO 是原生的内存 Unicode 容器，速度与 BytesIO 相似。

16.2.4.3. 多线程

FileIO 对象是线程安全的，只要它们包装的操作系统调用（比如Unix下的 read(2) ）也是线程安全的。

二进制缓冲对象（例如 BufferedReader, BufferedWriter, BufferedRandom 和 BufferedRWPair）使用锁来保护其内部结构；因此，可以安全地一次从多个线程中调用它们。

TextIOWrapper 对象不再是线程安全的。

16.2.4.4. 可重入性

二进制缓冲对象（ BufferedReader ， BufferedWriter ， BufferedRandom 和 BufferedRWPair 的实例）不是可重入的。虽然在正常情况下不会发生可重入调用，但是可能会在 signal 处理程序中执行 I/O 而产生。如果线程尝试重新输入已经访问的缓冲对象，则会引发 RuntimeError 。注意，这并不禁止其他线程进入缓冲对象。

上面的内容隐含地扩展到文本文件，因为 open() 函数会把缓冲对象包装在 TextIOWrapper 中。这包括标准流，因此也会影响内置函数 print() 。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

16.3. time — 时间的访问和转换

该模块提供了各种时间相关的函数。相关功能还可以参阅 datetime 和 calendar 模块。

尽管此模块始终可用，但并非所有平台上都提供所有功能。 此模块中定义的大多数函数是调用了所在平台 C 语言库的同名函数。 因为这些函数的语义因平台而异,所以使用时最好查阅平台相关文档。

下面是一些术语和惯例的解释.

epoch 是时间开始的点，并且取决于平台。对于Unix， epoch 是1970年1月1日00:00:00（UTC）。要找出给定平台上的 epoch ，请查看 time.gmtime(0) 。

术语 Unix 纪元秒数 是指自国际标准时间 1970 年 1 月 1 日零时以来经过的总秒数，通常不包括 闰秒 [https://en.wikipedia.org/wiki/Leap_second]。 在所有符合 POSIX 标准的平台上，闰秒都会从总秒数中被扣除。

此模块中的功能可能无法处理纪元之前或将来的远期日期和时间。未来的截止点由C库决定；对于32位系统，它通常在2038年。

Year 2000 (Y2K) issues: Python depends on the platform’s C library, which generally doesn’t have year 2000 issues, since all dates and times are represented internally as seconds since the epoch. Function strptime() can parse 2-digit years when given %y format code. When 2-digit years are parsed, they are converted according to the POSIX and ISO C standards: values 69–99 are mapped to 1969–1999, and values 0–68 are mapped to 2000–2068.

UTC是协调世界时（以前称为格林威治标准时间，或GMT）。缩写UTC不是错误，而是英语和法语之间的妥协。

DST是夏令时，在一年中的一部分时间（通常）调整时区一小时。 DST规则很神奇（由当地法律确定），并且每年都会发生变化。 C 库有一个包含本地规则的表（通常是从系统文件中读取以获得灵活性），并且在这方面是True Wisdom的唯一来源。

各种实时函数的精度可能低于表示其值或参数的单位所建议的精度。例如，在大多数Unix系统上，时钟 “ticks” 仅为每秒50或100次。

另一方面， time() 和 sleep() 的精度优于它们的Unix等价物：时间表示为浮点数，time() 返回最准确的时间 （使用Unix gettimeofday() 如果可用），并且 sleep() 将接受非零分数的时间（Unix select() 用于实现此功能，如果可用）。

时间值由 gmtime()，localtime() 和 strptime() 返回，并被 asctime()， mktime() 和 strftime() 接受，是一个 9 个整数的序列。 gmtime()， localtime() 和 strptime() 的返回值还提供各个字段的属性名称。

请参阅 struct_time 以获取这些对象的描述。

在 3.3 版更改: 在平台支持相应的 struct tm 成员时，struct_time 类型被扩展提供 tm_gmtoff 和 tm_zone 属性。

在 3.6 版更改: struct_time 的属性 tm_gmtoff 和 tm_zone 现在可在所有平台上使用。

使用以下函数在时间表示之间进行转换：

从

到

使用

seconds since the epoch

UTC 的 struct_time

gmtime()

seconds since the epoch

本地时间的 struct_time

localtime()

UTC 的 struct_time

seconds since the epoch

calendar.timegm()

本地时间的 struct_time

seconds since the epoch

mktime()

16.3.1. 函数

time.asctime([t])

转换一个元组或 struct_time 表示的时间，由 gmtime() 或 localtime() 返回为以下形式的字符串： 'Sun Jun 20 23:21:05 1993' 。如果未提供 t ，则使用由 localtime() 返回的当前时间。 区域信息不被函数 asctime() 使用。

注解

与同名的C函数不同， asctime() 不添加尾随换行符。

time.clock()

在Unix上，将当前处理器时间返回为以秒为单位的浮点数。精确度，实际上是“处理器时间”含义的定义，取决于同名C函数的精度。

在Windows上，此函数返回自第一次调用此函数以来经过的 wallclock 秒数，作为浮点数，基于Win32函数 QueryPerformanceCounter()。分辨率通常优于1微秒。

3.3 版后已移除: 此函数的行为取决于平台：根据你的需求，使用 perf_counter() 或 process_time() 获得具有明确定义的行为。

time.clock_getres(clk_id)

返回指定时钟 clk_id 的分辨率（精度）。有关 clk_id 的可接受值列表，请参阅 Clock ID 常量 。

Availability: Unix.

3.3 新版功能.

time.clock_gettime(clk_id)

返回指定 clk_id 时钟的时间。有关 clk_id 的可接受值列表，请参阅 Clock ID 常量 。

Availability: Unix.

3.3 新版功能.

time.clock_settime(clk_id, time)

设置指定 clk_id 时钟的时间。 目前， CLOCK_REALTIME 是 clk_id 唯一可接受的值。

Availability: Unix.

3.3 新版功能.

time.ctime([secs])

将以自 epoch 开始的秒数表示的时间转换为表示本地时间的字符串。 如果未提供 secs 或为 None，则使用 time() 所返回的当前时间。 ctime(secs) 相当于 asctime(localtime(secs))。 区域信息不会被 ctime() 使用。

time.get_clock_info(name)

获取有关指定时钟的信息作为命名空间对象。 支持的时钟名称和读取其值的相应函数是：

'clock': time.clock()

'monotonic': time.monotonic()

'perf_counter': time.perf_counter()

'process_time': time.process_time()

'time': time.time()

结果具有以下属性：

adjustable ： 如果时钟可以自动更改（例如通过NTP守护程序）或由系统管理员手动更改，则为 True ，否则为 False 。

implementation ： 用于获取时钟值的基础C函数的名称。有关可能的值，请参阅 Clock ID 常量 。

monotonic ：如果时钟不能倒退，则为 True ，否则为 False 。

resolution ： 以秒为单位的时钟分辨率（ float ）

3.3 新版功能.

time.gmtime([secs])

将以自 epoch 开始的秒数表示的时间转换为 UTC 的 struct_time ，其中 dst 标志始终为零。 如果未提供 secs 或为 None ，则使用 time() 所返回的当前时间。 一秒以内的小数将被忽略。 有关 struct_time 对象的说明请参见上文。 有关此函数的逆操作请参阅 calendar.timegm()。

time.localtime([secs])

与 gmtime() 相似但转换为当地时间。如果未提供 secs 或为 None ，则使用由 time() 返回的当前时间。当 DST 适用于给定时间时，dst标志设置为 1 。

time.mktime(t)

这是 localtime() 的反函数。它的参数是 struct_time 或者完整的 9 元组（因为需要 dst 标志；如果它是未知的则使用 -1 作为dst标志），它表示 local 的时间，而不是 UTC 。它返回一个浮点数，以便与 time() 兼容。如果输入值不能表示为有效时间，则 OverflowError 或 ValueError 将被引发（这取决于Python或底层C库是否捕获到无效值）。它可以生成时间的最早日期取决于平台。

time.monotonic()

返回单调时钟的值（以小数秒为单位），即不能倒退的时钟。时钟不受系统时钟更新的影响。返回值的参考点未定义，因此只有连续调用结果之间的差异才有效。

On Windows versions older than Vista, monotonic() detects GetTickCount() integer overflow (32 bits, roll-over after 49.7 days). It increases an internal epoch (reference time) by 2³² each time that an overflow is detected. The epoch is stored in the process-local state and so the value of monotonic() may be different in two Python processes running for more than 49 days. On more recent versions of Windows and on other operating systems, monotonic() is system-wide.

3.3 新版功能.

在 3.5 版更改: The function is now always available.

time.perf_counter()

返回性能计数器的值（以小数秒为单位），即具有最高可用分辨率的时钟，以测量短持续时间。它确实包括睡眠期间经过的时间，并且是系统范围的。返回值的参考点未定义，因此只有连续调用结果之间的差异才有效。

3.3 新版功能.

time.process_time()

返回当前进程的系统和用户CPU时间总和的值（以小数秒为单位）。它不包括睡眠期间经过的时间。根据定义，它在整个进程范围中。返回值的参考点未定义，因此只有连续调用结果之间的差异才有效。

3.3 新版功能.

time.sleep(secs)

暂停执行调用线程达到给定的秒数。参数可以是浮点数，以指示更精确的睡眠时间。实际的暂停时间可能小于请求的时间，因为任何捕获的信号将在执行该信号的捕获例程后终止 sleep() 。此外，由于系统中其他活动的安排，暂停时间可能比请求的时间长任意量。

在 3.5 版更改: 即使睡眠被信号中断，该函数现在至少睡眠 secs ，除非信号处理程序引发异常（参见 PEP 475 [https://www.python.org/dev/peps/pep-0475] 作为基本原理）。

time.strftime(format[, t])

转换一个元组或 struct_time 表示的由 gmtime() 或 localtime() 返回的时间到由 format 参数指定的字符串。如果未提供 t ，则使用由 localtime() 返回的当前时间。 format 必须是一个字符串。如果 t 中的任何字段超出允许范围，则引发 ValueError 。

0是时间元组中任何位置的合法参数；如果它通常是非法的，则该值被强制改为正确的值。

以下指令可以嵌入 format 字符串中。它们显示时没有可选的字段宽度和精度规范，并被 strftime() 结果中的指示字符替换：

指令

含义

注释

%a

本地化的缩写星期中每日的名称。

%A

本地化的星期中每日的完整名称。

%b

本地化的月缩写名称。

%B

本地化的月完整名称。

%c

本地化的适当日期和时间表示。

%d

十进制数 [01,31] 表示的月中日。

%H

十进制数 [00,23] 表示的小时（24小时制）。

%I

十进制数 [01,12] 表示的小时（12小时制）。

%j

十进制数 [001,366] 表示的年中日。

%m

十进制数 [01,12] 表示的月。

%M

十进制数 [00,59] 表示的分钟。

%p

本地化的 AM 或 PM 。

(1)

%S

十进制数 [00,61] 表示的秒。

(2)

%U

十进制数 [00,53] 表示的一年中的周数（星期日作为一周的第一天）。 在第一个星期日之前的新年中的所有日子都被认为是在第 0 周。

(3)

%w

十进制数 [0(星期日),6] 表示的周中日。

%W

十进制数 [00,53] 表示的一年中的周数（星期一作为一周的第一天）。 在第一个星期一之前的新年中的所有日子被认为是在第 0 周。

(3)

%x

本地化的适当日期表示。

%X

本地化的适当时间表示。

%y

十进制数 [00,99] 表示的没有世纪的年份。

%Y

十进制数表示的带世纪的年份。

%z

时区偏移以格式 +HHMM 或 -HHMM 形式的 UTC/GMT 的正或负时差指示，其中H表示十进制小时数字，M表示小数分钟数字 [-23:59, +23:59] 。

%Z

时区名称（如果不存在时区，则不包含字符）。

%%

字面的 '%' 字符。

注释:

当与 strptime() 函数一起使用时，如果使用 %I 指令来解析小时， %p 指令只影响输出小时字段。

范围真的是 0 到 61 ；值 60 在表示 leap seconds [https://en.wikipedia.org/wiki/Leap_second] 的时间戳中有效，并且由于历史原因支持值 61 。

当与 strptime() 函数一起使用时， %U 和 %W 仅用于指定星期几和年份的计算。

下面是一个示例，一个与 RFC 2822 [https://tools.ietf.org/html/rfc2822.html] Internet电子邮件标准以兼容的日期格式。 1

>>> from time import gmtime, strftime
>>> strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
'Thu, 28 Jun 2001 14:17:15 +0000'

某些平台可能支持其他指令，但只有此处列出的指令具有 ANSI C 标准化的含义。要查看平台支持的完整格式代码集，请参阅 strftime(3) 文档。

在某些平台上，可选的字段宽度和精度规范可以按照以下顺序紧跟在指令的初始 '%' 之后；这也不可移植。字段宽度通常为2，除了 %j ，它是3。

time.strptime(string[, format])

根据格式解析表示时间的字符串。 返回值为一个被 gmtime() 或 localtime() 返回的 struct_time 。

format 参数使用与 strftime() 相同的指令。 它默认为匹配 ctime() 所返回的格式 "%a %b %d %H:%M:%S %Y"` 。 如果 string 不能根据 format 来解析，或者解析后它有多余的数据，则会引发 ValueError。 当无法推断出更准确的值时，用于填充任何缺失数据的默认值是 (1900, 1, 1, 0, 0, 0, 0, 1, -1) 。 string 和 format 都必须为字符串。

例如:

>>> import time
>>> time.strptime("30 Nov 00", "%d %b %y")
time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0,
                 tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)

支持 %Z 指令是基于 tzname 中包含的值以及 daylight 是否为真。因此，它是特定于平台的，除了识别始终已知的 UTC 和 GMT （并且被认为是非夏令时时区）。

仅支持文档中指定的指令。因为每个平台都实现了 strftime() ，它有时会提供比列出的指令更多的指令。但是 strptime() 独立于任何平台，因此不一定支持所有未记录为支持的可用指令。

class time.struct_time

返回的时间值序列的类型为 gmtime() 、 localtime() 和 strptime() 。它是一个带有 named tuple 接口的对象：可以通过索引和属性名访问值。 存在以下值：

索引

属性

值

0

tm_year

（例如，1993）

1

tm_mon

range [1, 12]

2

tm_mday

range [1, 31]

3

tm_hour

range [0, 23]

4

tm_min

range [0, 59]

5

tm_sec

range [0, 61]； 见 strftime() 介绍中的 (2)

6

tm_wday

range [0, 6] ，周一为 0

7

tm_yday

range [1, 366]

8

tm_isdst

0, 1 或 -1；如下所示

N/A

tm_zone

时区名称的缩写

N/A

tm_gmtoff

以秒为单位的UTC以东偏离

请注意，与C结构不同，月份值是 [1,12] 的范围，而不是 [0,11] 。

在调用 mktime() 时， tm_isdst 可以在夏令时生效时设置为1，而在夏令时不生效时设置为0。 值-1表示这是未知的，并且通常会导致填写正确的状态。

当一个长度不正确的元组被传递给期望 struct_time 的函数，或者具有错误类型的元素时，会引发 TypeError 。

time.time()

返回以浮点数表示的从 epoch 开始的秒数的时间值。 epoch 的具体日期和 leap seconds [https://en.wikipedia.org/wiki/Leap_second] 的处理取决于平台。 在 Windows 和大多数 Unix 系统中， epoch 是 1970 年 1 月 1 日 00:00:00 (UTC)，并且闰秒将不计入从 epoch 开始的秒数。 这通常被称为 Unix 时间 [https://en.wikipedia.org/wiki/Unix_time]。 要了解给定平台上 epoch 的具体定义，请查看 gmtime(0)。

请注意，即使时间总是作为浮点数返回，但并非所有系统都提供高于1秒的精度。虽然此函数通常返回非递减值，但如果在两次调用之间设置了系统时钟，则它可以返回比先前调用更低的值。

返回的数字 time() 可以通过将其传递给 gmtime() 函数或转换为UTC中更常见的时间格式（即年、月、日、小时等）或通过将它传递给 localtime() 函数获得本地时间。在这两种情况下都返回一个 struct_time 对象，日历日期组件可以从中作为属性访问。

time.tzset()

Resets the time conversion rules used by the library routines. The environment variable TZ specifies how this is done.

Availability: Unix.

注解

虽然在很多情况下，更改 TZ 环境变量而不调用 tzset() 可能会影响函数的输出，例如 localtime() ，不应该依赖此行为。

TZ 不应该包含空格。

TZ 环境变量的标准格式是（为了清晰起见，添加了空格）:

std offset [dst [offset [,start[/time], end[/time]]]]

组件的位置是：

std 和 dst

三个或更多字母数字，给出时区缩写。这些将传到 time.tzname

offset

偏移量的形式为： ± hh[:mm[:ss]] 。这表示添加到达UTC的本地时间的值。如果前面有 ‘-‘ ，则时区位于本初子午线的东边；否则，在它是西边。如果dst之后没有偏移，则假设夏令时比标准时间提前一小时。

start[/time], end[/time]

指示何时更改为DST和从DST返回。开始日期和结束日期的格式为以下之一：

Jn

Julian日 n （1 <= n <= 365）。闰日不计算在内，因此在所有年份中，2月28日是第59天，3月1日是第60天。

n

从零开始的Julian日（0 <= n <= 365）。 闰日计入，可以引用2月29日。

Mm.n.d

一年中 m 月的第 n 周（1 <= n <= 5 ，1 <= m <= 12 ，第 5 周表示 “可能在 m 月第 4 周或第 5 周出现的最后第 d 日”）的第 d 天（0 <= d <= 6）。 第 1 周是第 d 天发生的第一周。 第 0 天是星期天。

time 的格式与 offset 的格式相同，但不允许使用前导符号（ ‘-‘ 或 ‘+’ ）。如果没有给出时间，则默认值为02:00:00。

>>> os.environ['TZ'] = 'EST+05EDT,M4.1.0,M10.5.0'
>>> time.tzset()
>>> time.strftime('%X %x %Z')
'02:07:36 05/08/03 EDT'
>>> os.environ['TZ'] = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
>>> time.tzset()
>>> time.strftime('%X %x %Z')
'16:08:12 05/08/03 AEST'

在许多Unix系统（包括 *BSD ， Linux ， Solaris 和 Darwin 上），使用系统的区域信息（ tzfile(5) ）数据库来指定时区规则会更方便。为此，将 TZ 环境变量设置为所需时区数据文件的路径，相对于系统 ‘zoneinfo’ 时区数据库的根目录，通常位于 /usr/share/zoneinfo 。 例如，'US/Eastern' 、 'Australia/Melbourne' 、 'Egypt' 或 'Europe/Amsterdam'。

>>> os.environ['TZ'] = 'US/Eastern'
>>> time.tzset()
>>> time.tzname
('EST', 'EDT')
>>> os.environ['TZ'] = 'Egypt'
>>> time.tzset()
>>> time.tzname
('EET', 'EEST')

16.3.2. Clock ID 常量

这些常量用作 clock_getres() 和 clock_gettime() 的参数。

time.CLOCK_HIGHRES

Solaris OS 有一个 CLOCK_HIGHRES 计时器，试图使用最佳硬件源，并可能提供接近纳秒的分辨率。 CLOCK_HIGHRES 是不可调节的高分辨率时钟。

Availability: Solaris.

3.3 新版功能.

time.CLOCK_MONOTONIC

无法设置的时钟，表示自某些未指定的起点以来的单调时间。

Availability: Unix.

3.3 新版功能.

time.CLOCK_MONOTONIC_RAW

类似于 CLOCK_MONOTONIC ，但可以访问不受NTP调整影响的原始硬件时间。

Availability: Linux 2.6.28 or later.

3.3 新版功能.

time.CLOCK_PROCESS_CPUTIME_ID

来自CPU的高分辨率每进程计时器。

Availability: Unix.

3.3 新版功能.

time.CLOCK_THREAD_CPUTIME_ID

特定于线程的CPU时钟。

Availability: Unix.

3.3 新版功能.

以下常量是唯一可以发送到 clock_settime() 的参数。

time.CLOCK_REALTIME

系统范围的实时时钟。 设置此时钟需要适当的权限。

Availability: Unix.

3.3 新版功能.

16.3.3. 时区常量

time.altzone

本地DST时区的偏移量，以UTC为单位的秒数，如果已定义。如果当地DST时区在UTC以东（如在西欧，包括英国），则是负数。 只有当 daylight 非零时才使用它。 见下面的注释。

time.daylight

如果定义了DST时区，则为非零。 见下面的注释。

time.timezone

本地（非DST）时区的偏移量，UTC以西的秒数（西欧大部分地区为负，美国为正，英国为零）。 见下面的注释。

time.tzname

两个字符串的元组：第一个是本地非DST时区的名称，第二个是本地DST时区的名称。 如果未定义DST时区，则不应使用第二个字符串。 见下面的注释。

注解

对于上述时区常量（ altzone 、 daylight 、 timezone 和 tzname ），该值由模块加载时有效的时区规则确定，或者最后一次 tzset() 被调用时，并且在过去的时间可能不正确。建议使用来自 localtime() 结果的 tm_gmtoff 和 tm_zone 来获取时区信息。

参见

模块 datetime

更多面向对象的日期和时间接口。

模块 locale

国际化服务。 区域设置会影响 strftime() 和 strptime() 中许多格式说明符的解析。

模块 calendar

一般日历相关功能。这个模块的 timegm() 是函数 gmtime() 的反函数。

备注

1

现在不推荐使用 %Z ，但是所有 ANSI C 库都不支持扩展为首选小时/分钟偏移量的``%z``转义符。 此外，严格的 1982 年原始 RFC 822 [https://tools.ietf.org/html/rfc822.html] 标准要求两位数的年份（%y而不是%Y），但是实际在2000年之前很久就转移到了4位数年。之后， RFC 822 [https://tools.ietf.org/html/rfc822.html] 已经废弃了，4位数的年份首先被推荐 RFC 1123 [https://tools.ietf.org/html/rfc1123.html] ，然后被 RFC 2822 [https://tools.ietf.org/html/rfc2822.html] 强制执行。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

16.4. argparse — 命令行选项、参数和子命令解析器

3.2 新版功能.

源代码： Lib/argparse.py [https://github.com/python/cpython/tree/3.6/Lib/argparse.py]

教程

此页面包含该 API 的参考信息。有关 Python 命令行解析更细致的介绍，请参阅 argparse 教程。

argparse 模块可以让人轻松编写用户友好的命令行接口。程序定义它需要的参数，然后 argparse 将弄清如何从 sys.argv 解析出那些参数。 argparse 模块还会自动生成帮助和使用手册，并在用户给程序传入无效参数时报出错误信息。

16.4.1. 示例

以下代码是一个 Python 程序，它获取一个整数列表并计算总和或者最大值：

import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
                    help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
                    const=sum, default=max,
                    help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))

假设上面的 Python 代码保存在名为 prog.py 的文件中，它可以在命令行运行并提供有用的帮助信息：

$ python prog.py -h
usage: prog.py [-h] [--sum] N [N ...]

Process some integers.

positional arguments:
 N           an integer for the accumulator

optional arguments:
 -h, --help  show this help message and exit
 --sum       sum the integers (default: find the max)

当使用适当的参数运行时，它会输出命令行传入整数的总和或者最大值：

$ python prog.py 1 2 3 4
4

$ python prog.py 1 2 3 4 --sum
10

如果传入无效参数，则会报出错误：

$ python prog.py a b c
usage: prog.py [-h] [--sum] N [N ...]
prog.py: error: argument N: invalid int value: 'a'

以下部分将引导你完成这个示例。

16.4.1.1. 创建一个解析器

使用 argparse 的第一步是创建一个 ArgumentParser 对象：

>>> parser = argparse.ArgumentParser(description='Process some integers.')

ArgumentParser 对象包含将命令行解析成 Python 数据类型所需的全部信息。

16.4.1.2. 添加参数

给一个 ArgumentParser 添加程序参数信息是通过调用 add_argument() 方法完成的。通常，这些调用指定 ArgumentParser 如何获取命令行字符串并将其转换为对象。这些信息在 parse_args() 调用时被存储和使用。例如：

>>> parser.add_argument('integers', metavar='N', type=int, nargs='+',
...                     help='an integer for the accumulator')
>>> parser.add_argument('--sum', dest='accumulate', action='store_const',
...                     const=sum, default=max,
...                     help='sum the integers (default: find the max)')

然后，调用 parse_args() 将返回一个具有 integers 和 accumulate 两个属性的对象。integers 属性将是一个包含一个或多个整数的列表，而 accumulate 属性当命令行中指定了 --sum 参数时将是 sum() 函数，否则则是 max() 函数。

16.4.1.3. 解析参数

ArgumentParser 通过 parse_args() 方法解析参数。它将检查命令行，把每个参数转换为适当的类型然后调用相应的操作。在大多数情况下，这意味着一个简单的 Namespace 对象将从命令行解析出的属性构建：

>>> parser.parse_args(['--sum', '7', '-1', '42'])
Namespace(accumulate=<built-in function sum>, integers=[7, -1, 42])

在脚本中，通常 parse_args() 会被不带参数调用，而 ArgumentParser 将自动从 sys.argv 中确定命令行参数。

16.4.2. ArgumentParser 对象

class argparse.ArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argparse.HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True)

创建一个新的 ArgumentParser 对象。所有的参数都应当作为关键字参数传入。每个参数在下面都有它更详细的描述，但简而言之，它们是：

prog - 程序的名称（默认值：sys.argv[0]）

usage - 描述程序用途的字符串（默认值：从添加到解析器的参数生成）

description - 在参数帮助文档之前显示的文本（默认值：无）

epilog - 在参数帮助文档之后显示的文本（默认值：无）

parents - 一个 ArgumentParser 对象的列表，它们的参数也应包含在内

formatter_class - 用于自定义帮助文档输出格式的类

prefix_chars - 可选参数的前缀字符集合（默认值： ‘-‘）

fromfile_prefix_chars - 当需要从文件中读取其他参数时，用于标识文件名的前缀字符集合（默认值： None）

argument_default - 参数的全局默认值（默认值： None）

conflict_handler - 解决冲突选项的策略（通常是不必要的）

add_help - 为解析器添加一个 -h/--help 选项（默认值： True）

allow_abbrev - 如果缩写是无歧义的，则允许缩写长选项 （默认值：True）

在 3.5 版更改: 增加了 allow_abbrev 参数。

以下部分描述这些参数如何使用。

16.4.2.1. prog

默认情况下，ArgumentParser 对象使用 sys.argv[0] 来确定如何在帮助消息中显示程序名称。这一默认值几乎总是可取的，因为它将使帮助消息与从命令行调用此程序的方式相匹配。例如，对于有如下代码的名为 myprogram.py 的文件：

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', help='foo help')
args = parser.parse_args()

该程序的帮助信息将显示 myprogram.py 作为程序名称（无论程序从何处被调用）：

$ python myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

optional arguments:
 -h, --help  show this help message and exit
 --foo FOO   foo help
$ cd ..
$ python subdir/myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

optional arguments:
 -h, --help  show this help message and exit
 --foo FOO   foo help

要更改这样的默认行为，可以使用 prog= 参数为 ArgumentParser 指定另一个值：

>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.print_help()
usage: myprogram [-h]

optional arguments:
 -h, --help  show this help message and exit

需要注意的是，无论是从 sys.argv[0] 或是从 prog= 参数确定的程序名称，都可以在帮助消息里通过 %(prog)s 格式说明符来引用。

>>> parser = argparse.ArgumentParser(prog='myprogram')
>>> parser.add_argument('--foo', help='foo of the %(prog)s program')
>>> parser.print_help()
usage: myprogram [-h] [--foo FOO]

optional arguments:
 -h, --help  show this help message and exit
 --foo FOO   foo of the myprogram program

16.4.2.2. usage

默认情况下，ArgumentParser 根据它包含的参数来构建用法消息：

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('--foo', nargs='?', help='foo help')
>>> parser.add_argument('bar', nargs='+', help='bar help')
>>> parser.print_help()
usage: PROG [-h] [--foo [FOO]] bar [bar ...]

positional arguments:
 bar          bar help

optional arguments:
 -h, --help   show this help message and exit
 --foo [FOO]  foo help

可以通过 usage= 关键字参数覆盖这一默认消息：

>>> parser = argparse.ArgumentParser(prog='PROG', usage='%(prog)s [options]')
>>> parser.add_argument('--foo', nargs='?', help='foo help')
>>> parser.add_argument('bar', nargs='+', help='bar help')
>>> parser.print_help()
usage: PROG [options]

positional arguments:
 bar          bar help

optional arguments:
 -h, --help   show this help message and exit
 --foo [FOO]  foo help

在用法消息中可以使用 %(prog)s 格式说明符来填入程序名称。

16.4.2.3. description

大多数对 ArgumentParser 构造方法的调用都会使用 description= 关键字参数。 这个参数简要描述这个程序做什么以及怎么做。 在帮助消息中，这个描述会显示在命令行用法字符串和各种参数的帮助消息之间:

>>> parser = argparse.ArgumentParser(description='A foo that bars')
>>> parser.print_help()
usage: argparse.py [-h]

A foo that bars

optional arguments:
 -h, --help  show this help message and exit

在默认情况下，description 将被换行以便适应给定的空间。如果想改变这种行为，见 formatter_class 参数。

16.4.2.4. epilog

一些程序喜欢在 description 参数后显示额外的对程序的描述。这种文字能够通过给 ArgumentParser:: 提供 epilog= 参数而被指定。

>>> parser = argparse.ArgumentParser(
...     description='A foo that bars',
...     epilog="And that's how you'd foo a bar")
>>> parser.print_help()
usage: argparse.py [-h]

A foo that bars

optional arguments:
 -h, --help  show this help message and exit

And that's how you'd foo a bar

和 description 参数一样，epilog= text 在默认情况下会换行，但是这种行为能够被调整通过提供 formatter_class 参数给 ArgumentParse.

16.4.2.5. parents

有些时候，少数解析器会使用同一系列参数。 单个解析器能够通过提供 parents= 参数给 ArgumentParser 而使用相同的参数而不是重复这些参数的定义。parents= 参数使用 ArgumentParser 对象的列表，从它们那里收集所有的位置和可选的行为，然后将这写行为加到正在构建的 ArgumentParser 对象。

>>> parent_parser = argparse.ArgumentParser(add_help=False)
>>> parent_parser.add_argument('--parent', type=int)

>>> foo_parser = argparse.ArgumentParser(parents=[parent_parser])
>>> foo_parser.add_argument('foo')
>>> foo_parser.parse_args(['--parent', '2', 'XXX'])
Namespace(foo='XXX', parent=2)

>>> bar_parser = argparse.ArgumentParser(parents=[parent_parser])
>>> bar_parser.add_argument('--bar')
>>> bar_parser.parse_args(['--bar', 'YYY'])
Namespace(bar='YYY', parent=None)

请注意大多数父解析器会指定 add_help=False . 否则， ArgumentParse 将会看到两个 -h/--help 选项（一个在父参数中一个在子参数中）并且产生一个错误。

注解

你在传``parents=``给那些解析器时必须完全初始化它们。如果你在子解析器之后改变父解析器是，这些改变不会反映在子解析器上。

16.4.2.6. formatter_class

ArgumentParser 对象允许通过指定备用格式化类来自定义帮助格式。目前，有四种这样的类。

class argparse.RawDescriptionHelpFormatter

class argparse.RawTextHelpFormatter

class argparse.ArgumentDefaultsHelpFormatter

class argparse.MetavarTypeHelpFormatter

RawDescriptionHelpFormatter 和 RawTextHelpFormatter 在正文的描述和展示上给与了更多的控制。ArgumentParser 对象会将 description 和 epilog 的文字在命令行中自动换行。

>>> parser = argparse.ArgumentParser(
...     prog='PROG',
...     description='''this description
...         was indented weird
...             but that is okay''',
...     epilog='''
...             likewise for this epilog whose whitespace will
...         be cleaned up and whose words will be wrapped
...         across a couple lines''')
>>> parser.print_help()
usage: PROG [-h]

this description was indented weird but that is okay

optional arguments:
 -h, --help  show this help message and exit

likewise for this epilog whose whitespace will be cleaned up and whose words
will be wrapped across a couple lines

传 RawDescriptionHelpFormatter 给 formatter_class= 表示 description 和 epilog 已经被正确的格式化了，不能在命令行中被自动换行:

>>> parser = argparse.ArgumentParser(
...     prog='PROG',
...     formatter_class=argparse.RawDescriptionHelpFormatter,
...     description=textwrap.dedent('''\
...         Please do not mess up this text!
...         --------------------------------
...             I have indented it
...             exactly the way
...             I want it
...         '''))
>>> parser.print_help()
usage: PROG [-h]

Please do not mess up this text!
--------------------------------
   I have indented it
   exactly the way
   I want it

optional arguments:
 -h, --help  show this help message and exit

RawTextHelpFormatter 保留所有种类文字的空格，包括参数的描述。然而，多重的新行会被替换成一行。如果你想保留多重的空白行，可以在新行之间加空格。

ArgumentDefaultsHelpFormatter 自动添加默认的值的信息到每一个帮助信息的参数中:

>>> parser = argparse.ArgumentParser(
...     prog='PROG',
...     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
>>> parser.add_argument('--foo', type=int, default=42, help='FOO!')
>>> parser.add_argument('bar', nargs='*', default=[1, 2, 3], help='BAR!')
>>> parser.print_help()
usage: PROG [-h] [--foo FOO] [bar [bar ...]]

positional arguments:
 bar         BAR! (default: [1, 2, 3])

optional arguments:
 -h, --help  show this help message and exit
 --foo FOO   FOO! (default: 42)

MetavarTypeHelpFormatter 为它的值在每一个参数中使用 type 的参数名当作它的显示名（而不是使用通常的格式 dest ):

>>> parser = argparse.ArgumentParser(
...     prog='PROG',
...     formatter_class=argparse.MetavarTypeHelpFormatter)
>>> parser.add_argument('--foo', type=int)
>>> parser.add_argument('bar', type=float)
>>> parser.print_help()
usage: PROG [-h] [--foo int] float

positional arguments:
  float

optional arguments:
  -h, --help  show this help message and exit
  --foo int

16.4.2.7. prefix_chars

许多命令行会使用 - 当作前缀，比如 -f/--foo。如果解析器需要支持不同的或者额外的字符，比如像 +f 或者 /foo 的选项，可以在参数解析构建器中使用 prefix_chars= 参数。

>>> parser = argparse.ArgumentParser(prog='PROG', prefix_chars='-+')
>>> parser.add_argument('+f')
>>> parser.add_argument('++bar')
>>> parser.parse_args('+f X ++bar Y'.split())
Namespace(bar='Y', f='X')

The prefix_chars= 参数默认使用 '-'. 支持一系列字符，但是不包括 - ，这样会产生不被允许的 -f/--foo 选项。

16.4.2.8. fromfile_prefix_chars

有些时候，先举个例子，当处理一个特别长的参数列表的时候，把它存入一个文件中而不是在命令行打出来会很有意义。如果 fromfile_prefix_chars= 参数提供给 ArgumentParser 构造函数，之后所有类型的字符的参数都会被当成文件处理，并且会被文件包含的参数替代。举个栗子:

>>> with open('args.txt', 'w') as fp:
...     fp.write('-f\nbar')
>>> parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
>>> parser.add_argument('-f')
>>> parser.parse_args(['-f', 'foo', '@args.txt'])
Namespace(f='bar')

从文件读取的参数在默认情况下必须一个一行（但是可参见 convert_arg_line_to_args()）并且它们被视为与命令行上的原始文件引用参数位于同一位置。所以在以上例子中，['-f', 'foo', '@args.txt'] 的表示和 ['-f', 'foo', '-f', 'bar'] 的表示相同。

fromfile_prefix_chars= 参数默认为 None，意味着参数不会被当作文件对待。

16.4.2.9. argument_default

一般情况下，参数默认会通过设置一个默认到 add_argument() 或者调用带一组指定键值对的 ArgumentParser.set_defaults() 方法。但是有些时候，为参数指定一个普遍适用的解析器会更有用。这能够通过传输 argument_default= 关键词参数给 ArgumentParser 来完成。举个栗子，要全局禁止在 parse_args() 中创建属性，我们提供 argument_default=SUPPRESS:

>>> parser = argparse.ArgumentParser(argument_default=argparse.SUPPRESS)
>>> parser.add_argument('--foo')
>>> parser.add_argument('bar', nargs='?')
>>> parser.parse_args(['--foo', '1', 'BAR'])
Namespace(bar='BAR', foo='1')
>>> parser.parse_args([])
Namespace()

16.4.2.10. allow_abbrev

正常情况下，当你向 ArgumentParser 的 parse_args() 方法传入一个参数列表时，它会 recognizes abbreviations。

这个特性可以设置 allow_abbrev 为 False 来关闭:

>>> parser = argparse.ArgumentParser(prog='PROG', allow_abbrev=False)
>>> parser.add_argument('--foobar', action='store_true')
>>> parser.add_argument('--foonley', action='store_false')
>>> parser.parse_args(['--foon'])
usage: PROG [-h] [--foobar] [--foonley]
PROG: error: unrecognized arguments: --foon

3.5 新版功能.

16.4.2.11. conflict_handler

ArgumentParser 对象不允许在相同选项字符串下有两种行为。默认情况下， ArgumentParser 对象会产生一个异常如果去创建一个正在使用的选项字符串参数。

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('-f', '--foo', help='old foo help')
>>> parser.add_argument('--foo', help='new foo help')
Traceback (most recent call last):
 ..
ArgumentError: argument --foo: conflicting option string(s): --foo

有些时候（例如：使用 parents），重写旧的有相同选项字符串的参数会更有用。为了产生这种行为， 'resolve' 值可以提供给 ArgumentParser 的 conflict_handler= 参数:

>>> parser = argparse.ArgumentParser(prog='PROG', conflict_handler='resolve')
>>> parser.add_argument('-f', '--foo', help='old foo help')
>>> parser.add_argument('--foo', help='new foo help')
>>> parser.print_help()
usage: PROG [-h] [-f FOO] [--foo FOO]

optional arguments:
 -h, --help  show this help message and exit
 -f FOO      old foo help
 --foo FOO   new foo help

注意 ArgumentParser 对象只能移除一个行为如果它所有的选项字符串都被重写。所以，在上面的例子中，旧的 -f/--foo 行为 回合 -f 行为保持一样, 因为只有 --foo 选项字符串被重写。

16.4.2.12. add_help

默认情况下，ArgumentParser 对象添加一个简单的显示解析器帮助信息的选项。举个栗子，考虑一个名为 myprogram.py 的文件包含如下代码:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo', help='foo help')
args = parser.parse_args()

如果 -h or --help 在命令行中被提供, 参数解析器帮助信息会打印:

$ python myprogram.py --help
usage: myprogram.py [-h] [--foo FOO]

optional arguments:
 -h, --help  show this help message and exit
 --foo FOO   foo help

有时候可能会需要关闭额外的帮助信息。这可以通过在 ArgumentParser 中设置 add_help= 参数为 False 来实现。

>>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)
>>> parser.add_argument('--foo', help='foo help')
>>> parser.print_help()
usage: PROG [--foo FOO]

optional arguments:
 --foo FOO  foo help

帮助选项一般为 -h/--help。如果 prefix_chars= 被指定并且没有包含 - 字符，在这种情况下， -h --help 不是有效的选项。此时， prefix_chars 的第一个字符将用作帮助选项的前缀。

>>> parser = argparse.ArgumentParser(prog='PROG', prefix_chars='+/')
>>> parser.print_help()
usage: PROG [+h]

optional arguments:
  +h, ++help  show this help message and exit

16.4.3. add_argument() 方法

ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])

定义单个的命令行参数应当如何解析。每个形参都在下面有它自己更多的描述，长话短说有：

name or flags - 一个命名或者一个选项字符串的列表，例如 foo 或 -f, --foo。

action - 当参数在命令行中出现时使用的动作基本类型。

nargs - 命令行参数应当消耗的数目。

const - 被一些 action 和 nargs 选择所需求的常数。

default - 当参数未在命令行中出现时使用的值。

type - 命令行参数应当被转换成的类型。

choices - 可用的参数的容器。

required - 此命令行选项是否可省略 （仅选项可用）。

help - 一个此选项作用的简单描述。

metavar - 在使用方法消息中使用的参数值示例。

dest - 被添加到 parse_args() 所返回对象上的属性名。

以下部分描述这些参数如何使用。

16.4.3.1. name or flags

add_argument() 方法必须知道它是否是一个选项，例如 -f 或 --foo，或是一个位置参数，例如一组文件名。第一个传递给 add_argument() 的参数必须是一系列 flags 或者是一个简单的参数名。例如，可以选项可以被这样创建:

>>> parser.add_argument('-f', '--foo')

而位置参数可以这么创建:

>>> parser.add_argument('bar')

当 parse_args() 被调用，选项会以 - 前缀识别，剩下的参数则会被假定为位置参数:

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('-f', '--foo')
>>> parser.add_argument('bar')
>>> parser.parse_args(['BAR'])
Namespace(bar='BAR', foo=None)
>>> parser.parse_args(['BAR', '--foo', 'FOO'])
Namespace(bar='BAR', foo='FOO')
>>> parser.parse_args(['--foo', 'FOO'])
usage: PROG [-h] [-f FOO] bar
PROG: error: the following arguments are required: bar

16.4.3.2. action

ArgumentParser 对象将命令行参数与动作相关联。这些动作可以做与它们相关联的命令行参数的任何事，尽管大多数动作只是简单的向 parse_args() 返回的对象上添加属性。action 命名参数指定了这个命令行参数应当如何处理。供应的动作有：

'store' - 存储参数的值。这是默认的动作。例如:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo')
>>> parser.parse_args('--foo 1'.split())
Namespace(foo='1')

'store_const' - 存储被 const 命名参数指定的值。 'store_const' 动作通常用在选项中来指定一些标志。例如:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action='store_const', const=42)
>>> parser.parse_args(['--foo'])
Namespace(foo=42)

'store_true' and 'store_false' - 这些是 'store_const' 分别用作存储 True 和 False 值的特殊用例。另外，它们的默认值分别为 False 和 True。例如:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action='store_true')
>>> parser.add_argument('--bar', action='store_false')
>>> parser.add_argument('--baz', action='store_false')
>>> parser.parse_args('--foo --bar'.split())
Namespace(foo=True, bar=False, baz=True)

'append' - 存储一个列表，并且将每个参数值追加到列表中。在允许多次使用选项时很有用。例如:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action='append')
>>> parser.parse_args('--foo 1 --foo 2'.split())
Namespace(foo=['1', '2'])

'append_const' - 这存储一个列表，并将 const 命名参数指定的值追加到列表中。（注意 const 命名参数默认为 None。）``’append_const’`` 动作一般在多个参数需要在同一列表中存储常数时会有用。例如:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--str', dest='types', action='append_const', const=str)
>>> parser.add_argument('--int', dest='types', action='append_const', const=int)
>>> parser.parse_args('--str --int'.split())
Namespace(types=[<class 'str'>, <class 'int'>])

'count' - 计算一个关键字参数出现的数目或次数。例如，对于一个增长的详情等级来说有用:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--verbose', '-v', action='count')
>>> parser.parse_args(['-vvv'])
Namespace(verbose=3)

'help' - 打印所有当前解析器中的选项和参数的完整帮助信息，然后退出。默认情况下，一个 help 动作会被自动加入解析器。关于输出是如何创建的，参与 ArgumentParser。

'version' - 期望有一个 version= 命名参数在 add_argument() 调用中，并打印版本信息并在调用后退出:

>>> import argparse
>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('--version', action='version', version='%(prog)s 2.0')
>>> parser.parse_args(['--version'])
PROG 2.0

您还可以通过传递 Action 子类或实现相同接口的其他对象来指定任意操作。建议的方法是扩展 Action，覆盖 __call__ 方法和可选的 __init__ 方法。

一个自定义动作的例子:

>>> class FooAction(argparse.Action):
...     def __init__(self, option_strings, dest, nargs=None, **kwargs):
...         if nargs is not None:
...             raise ValueError("nargs not allowed")
...         super(FooAction, self).__init__(option_strings, dest, **kwargs)
...     def __call__(self, parser, namespace, values, option_string=None):
...         print('%r %r %r' % (namespace, values, option_string))
...         setattr(namespace, self.dest, values)
...
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action=FooAction)
>>> parser.add_argument('bar', action=FooAction)
>>> args = parser.parse_args('1 --foo 2'.split())
Namespace(bar=None, foo=None) '1' None
Namespace(bar='1', foo=None) '2' '--foo'
>>> args
Namespace(bar='1', foo='2')

更多描述，见 Action。

16.4.3.3. nargs

ArgumentParser 对象通常关联一个单独的命令行参数到一个单独的被执行的动作。 nargs 命名参数关联不同数目的命令行参数到单一动作。支持的值有：

N （一个整数）。命令行中的 N 个参数会被聚集到一个列表中。 例如:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', nargs=2)
>>> parser.add_argument('bar', nargs=1)
>>> parser.parse_args('c --foo a b'.split())
Namespace(bar=['c'], foo=['a', 'b'])

注意 nargs=1 会产生一个单元素列表。这和默认的元素本身是不同的。

'?'。如果可能的话，会从命令行中消耗一个参数，并产生一个单一项。如果当前没有命令行参数，则会产生 default 值。注意，对于选项，有另外的用例 - 选项字符串出现但没有跟随命令行参数，则会产生 const 值。一些说用用例:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', nargs='?', const='c', default='d')
>>> parser.add_argument('bar', nargs='?', default='d')
>>> parser.parse_args(['XX', '--foo', 'YY'])
Namespace(bar='XX', foo='YY')
>>> parser.parse_args(['XX', '--foo'])
Namespace(bar='XX', foo='c')
>>> parser.parse_args([])
Namespace(bar='d', foo='d')

nargs='?' 的一个更普遍用法是允许可选的输入或输出文件:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
...                     default=sys.stdin)
>>> parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
...                     default=sys.stdout)
>>> parser.parse_args(['input.txt', 'output.txt'])
Namespace(infile=<_io.TextIOWrapper name='input.txt' encoding='UTF-8'>,
          outfile=<_io.TextIOWrapper name='output.txt' encoding='UTF-8'>)
>>> parser.parse_args([])
Namespace(infile=<_io.TextIOWrapper name='<stdin>' encoding='UTF-8'>,
          outfile=<_io.TextIOWrapper name='<stdout>' encoding='UTF-8'>)

'*'。所有当前命令行参数被聚集到一个列表中。注意通过 nargs='*' 来实现多个位置参数通常没有意义，但是多个选项是可能的。例如:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', nargs='*')
>>> parser.add_argument('--bar', nargs='*')
>>> parser.add_argument('baz', nargs='*')
>>> parser.parse_args('a b --foo x y --bar 1 2'.split())
Namespace(bar=['1', '2'], baz=['a', 'b'], foo=['x', 'y'])

'+'。和 '*' 类似，所有当前命令行参数被聚集到一个列表中。另外，当前没有至少一个命令行参数时会产生一个错误信息。例如:

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('foo', nargs='+')
>>> parser.parse_args(['a', 'b'])
Namespace(foo=['a', 'b'])
>>> parser.parse_args([])
usage: PROG [-h] foo [foo ...]
PROG: error: the following arguments are required: foo

argarse.REMAINDER。所有剩余的命令行参数被聚集到一个列表中。这通常在从一个命令行功能传递参数到另一个命令行功能中时有用:

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('--foo')
>>> parser.add_argument('command')
>>> parser.add_argument('args', nargs=argparse.REMAINDER)
>>> print(parser.parse_args('--foo B cmd --arg1 XX ZZ'.split()))
Namespace(args=['--arg1', 'XX', 'ZZ'], command='cmd', foo='B')

如果不提供 nargs 命名参数，则消耗参数的数目将被 action 决定。通常这意味着单一项目（非列表）消耗单一命令行参数。

16.4.3.4. const

add_argument() 的``const`` 参数用于保存不从命令行中读取但被各种 ArgumentParser 动作需求的常数值。最常用的两例为：

当 add_argument() 通过 action='store_const' 或 action='append_const 调用时。这些动作将 const 值添加到 parse_args() 返回的对象的属性中。在 action 的描述中查看案例。

当 add_argument() 通过选项（例如 -f 或 --foo）调用并且 nargs='?' 时。这会创建一个可以跟随零个或一个命令行参数的选项。当解析命令行时，如果选项后没有参数，则将用 const 代替。在 nargs 描述中查看案例。

对 'store_const' 和 'append_const' 动作， const 命名参数必须给出。对其他动作，默认为 None。

16.4.3.5. 默认值

所有选项和一些位置参数可能在命令行中被忽略。add_argument() 的命名参数 default，默认值为 None，指定了在命令行参数未出现时应当使用的值。对于选项， default 值在选项未在命令行中出现时使用:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', default=42)
>>> parser.parse_args(['--foo', '2'])
Namespace(foo='2')
>>> parser.parse_args([])
Namespace(foo=42)

如果 default 值是一个字符串，解析器解析此值就像一个命令行参数。特别是，在将属性设置在 Namespace 的返回值之前，解析器应用任何提供的 type 转换参数。否则解析器使用原值:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--length', default='10', type=int)
>>> parser.add_argument('--width', default=10.5, type=int)
>>> parser.parse_args()
Namespace(length=10, width=10.5)

对于 nargs 等于 ? 或 * 的位置参数， default 值在没有命令行参数出现时使用。

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('foo', nargs='?', default=42)
>>> parser.parse_args(['a'])
Namespace(foo='a')
>>> parser.parse_args([])
Namespace(foo=42)

提供 default=argparse.SUPPRESS 导致命令行参数未出现时没有属性被添加:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', default=argparse.SUPPRESS)
>>> parser.parse_args([])
Namespace()
>>> parser.parse_args(['--foo', '1'])
Namespace(foo='1')

16.4.3.6. type – 类型

默认情况下，ArgumentParser 对象将命令行参数当作简单字符串读入。然而，命令行字符串经常需要被当作其它的类型，比如 float 或者 int。add_argument() 的 type 关键词参数允许任何的类型检查和类型转换。一般的内建类型和函数可以直接被 type 参数使用。

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('foo', type=int)
>>> parser.add_argument('bar', type=open)
>>> parser.parse_args('2 temp.txt'.split())
Namespace(bar=<_io.TextIOWrapper name='temp.txt' encoding='UTF-8'>, foo=2)

当 type 参数被应用到默认参数时，请参考 default 参数的部分。

为方便使用不同类型的文件，argparse 模块提供了 FileType 工厂类，该类接受 mode=, bufsize=, encoding= 和 errors= 等 open() 函数参数。 例如，FileType('w') 可被用来创建一个可写文件:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('bar', type=argparse.FileType('w'))
>>> parser.parse_args(['out.txt'])
Namespace(bar=<_io.TextIOWrapper name='out.txt' encoding='UTF-8'>)

type= 可接受任意可调用对象，该对象应传入单个字符串参数并返回转换后的值:

>>> def perfect_square(string):
...     value = int(string)
...     sqrt = math.sqrt(value)
...     if sqrt != int(sqrt):
...         msg = "%r is not a perfect square" % string
...         raise argparse.ArgumentTypeError(msg)
...     return value
...
>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('foo', type=perfect_square)
>>> parser.parse_args(['9'])
Namespace(foo=9)
>>> parser.parse_args(['7'])
usage: PROG [-h] foo
PROG: error: argument foo: '7' is not a perfect square

choices 关键词参数可能会使类型检查者更方便的检查一个范围的值。

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('foo', type=int, choices=range(5, 10))
>>> parser.parse_args(['7'])
Namespace(foo=7)
>>> parser.parse_args(['11'])
usage: PROG [-h] {5,6,7,8,9}
PROG: error: argument foo: invalid choice: 11 (choose from 5, 6, 7, 8, 9)

详情请查阅 choices 段落。

16.4.3.7. choices

某些命令行参数应当从一组受限值中选择。 这可通过将一个容器对象作为 choices 关键字参数传给 add_argument() 来处理。 当执行命令行解析时，参数值将被检查，如果参数不是可接受的值之一就将显示错误消息:

>>> parser = argparse.ArgumentParser(prog='game.py')
>>> parser.add_argument('move', choices=['rock', 'paper', 'scissors'])
>>> parser.parse_args(['rock'])
Namespace(move='rock')
>>> parser.parse_args(['fire'])
usage: game.py [-h] {rock,paper,scissors}
game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
'paper', 'scissors')

请注意 choices 容器包含的内容会在执行任意 type 转换之后被检查，因此 choices 容器中对象的类型应当与指定的 type 相匹配:

>>> parser = argparse.ArgumentParser(prog='doors.py')
>>> parser.add_argument('door', type=int, choices=range(1, 4))
>>> print(parser.parse_args(['3']))
Namespace(door=3)
>>> parser.parse_args(['4'])
usage: doors.py [-h] {1,2,3}
doors.py: error: argument door: invalid choice: 4 (choose from 1, 2, 3)

任何支持 in 运算符的对象都可作为 choices 值传入，因此 dict 对象，set 对象，自定义容器等都是受支持的。

16.4.3.8. required

通常，argparse 模块会认为 -f 和 --bar 等旗标是指明 可选的 参数，它们总是可以在命令行中被忽略。 要让一个选项成为 必需的，则可以将 True 作为 required= 关键字参数传给 add_argument():

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', required=True)
>>> parser.parse_args(['--foo', 'BAR'])
Namespace(foo='BAR')
>>> parser.parse_args([])
usage: argparse.py [-h] [--foo FOO]
argparse.py: error: option --foo is required

如这个例子所示，如果一个选项被标记为 required，则当该选项未在命令行中出现时，parse_args() 将会报告一个错误。

注解

必需的选项通常被认为是不适宜的，因为用户会预期 options 都是 可选的，因此在可能的情况下应当避免使用它们。

16.4.3.9. help

help 值是一个包含参数简短描述的字符串。 当用户请求帮助时（一般是通过在命令行中使用 -h 或 --help 的方式），这些 help 描述将随每个参数一同显示:

>>> parser = argparse.ArgumentParser(prog='frobble')
>>> parser.add_argument('--foo', action='store_true',
...                     help='foo the bars before frobbling')
>>> parser.add_argument('bar', nargs='+',
...                     help='one of the bars to be frobbled')
>>> parser.parse_args(['-h'])
usage: frobble [-h] [--foo] bar [bar ...]

positional arguments:
 bar     one of the bars to be frobbled

optional arguments:
 -h, --help  show this help message and exit
 --foo   foo the bars before frobbling

help 字符串可包括各种格式描述符以避免重复使用程序名称或参数 default 等文本。 有效的描述符包括程序名称 %(prog)s 和传给 add_argument() 的大部分关键字参数，例如 %(default)s, %(type)s 等等:

>>> parser = argparse.ArgumentParser(prog='frobble')
>>> parser.add_argument('bar', nargs='?', type=int, default=42,
...                     help='the bar to %(prog)s (default: %(default)s)')
>>> parser.print_help()
usage: frobble [-h] [bar]

positional arguments:
 bar     the bar to frobble (default: 42)

optional arguments:
 -h, --help  show this help message and exit

由于帮助字符串支持 %-formatting，如果你希望在帮助字符串中显示 % 字面值，你必须将其转义为 %%。

argparse 支持静默特定选项的帮助，具体做法是将 help 的值设为 argparse.SUPPRESS:

>>> parser = argparse.ArgumentParser(prog='frobble')
>>> parser.add_argument('--foo', help=argparse.SUPPRESS)
>>> parser.print_help()
usage: frobble [-h]

optional arguments:
  -h, --help  show this help message and exit

16.4.3.10. metavar

当 ArgumentParser 生成帮助消息时，它需要用某种方式来引用每个预期的参数。 默认情况下，ArgumentParser 对象使用 dest 值作为每个对象的 “name”。 默认情况下，对于位置参数动作，dest 值将被直接使用，而对于可选参数动作，dest 值将被转为大写形式。 因此，一个位置参数 dest='bar' 的引用形式将为 bar。 一个带有单独命令行参数的可选参数 --foo 的引用形式将为 FOO。 示例如下:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo')
>>> parser.add_argument('bar')
>>> parser.parse_args('X --foo Y'.split())
Namespace(bar='X', foo='Y')
>>> parser.print_help()
usage:  [-h] [--foo FOO] bar

positional arguments:
 bar

optional arguments:
 -h, --help  show this help message and exit
 --foo FOO

可以使用 metavar 来指定一个替代名称:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', metavar='YYY')
>>> parser.add_argument('bar', metavar='XXX')
>>> parser.parse_args('X --foo Y'.split())
Namespace(bar='X', foo='Y')
>>> parser.print_help()
usage:  [-h] [--foo YYY] XXX

positional arguments:
 XXX

optional arguments:
 -h, --help  show this help message and exit
 --foo YYY

请注意 metavar 仅改变 显示的 名称 - parse_args() 对象的属性名称仍然会由 dest 值确定。

不同的 nargs 值可能导致 metavar 被多次使用。 提供一个元组给 metavar 即为每个参数指定不同的显示信息:

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('-x', nargs=2)
>>> parser.add_argument('--foo', nargs=2, metavar=('bar', 'baz'))
>>> parser.print_help()
usage: PROG [-h] [-x X X] [--foo bar baz]

optional arguments:
 -h, --help     show this help message and exit
 -x X X
 --foo bar baz

16.4.3.11. dest

大多数 ArgumentParser 动作会添加一些值作为 parse_args() 所返回对象的一个属性。 该属性的名称由 add_argument() 的 dest 关键字参数确定。 对于位置参数动作，dest 通常会作为 add_argument() 的第一个参数提供:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('bar')
>>> parser.parse_args(['XXX'])
Namespace(bar='XXX')

对于可选参数动作，dest 的值通常取自选项字符串。 ArgumentParser 会通过接受第一个长选项字符串并去掉开头的 -- 字符串来生成 dest 的值。 如果没有提供长选项字符串，则 dest 将通过接受第一个短选项字符串并去掉开头的 - 字符来获得。 任何内部的 - 字符都将被转换为 _ 字符以确保字符串是有效的属性名称。 下面的例子显示了这种行为:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('-f', '--foo-bar', '--foo')
>>> parser.add_argument('-x', '-y')
>>> parser.parse_args('-f 1 -x 2'.split())
Namespace(foo_bar='1', x='2')
>>> parser.parse_args('--foo 1 -y 2'.split())
Namespace(foo_bar='1', x='2')

dest 允许提供自定义属性名称:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', dest='bar')
>>> parser.parse_args('--foo XXX'.split())
Namespace(bar='XXX')

16.4.3.12. Action 类

Action 类实现了 Action API，它是一个返回可调用对象的可调用对象，返回的可调用对象可处理来自命令行的参数。 任何遵循此 API 的对象均可作为 action 形参传给 add_argument()。

class argparse.Action(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)

Action 对象会被 ArgumentParser 用来表示解析从命令行中的一个或多个字符串中解析出单个参数所必须的信息。 Action 类必须接受两个位置参数以及传给 ArgumentParser.add_argument() 的任何关键字参数，除了 action 本身。

Action 的实例（或作为or return value of any callable to the action 形参的任何可调用对象的返回值）应当定义 “dest”, “option_strings”, “default”, “type”, “required”, “help” 等属性。 确保这些属性被定义的最容易方式是调用 Action.__init__。

Action 的实例应当为可调用对象，因此所有子类都必须重载 __call__ 方法，该方法应当接受四个形参:

parser - 包含此动作的 ArgumentParser 对象。

namespace - 将由 parse_args() 返回的 Namespace 对象。 大多数动作会使用 setattr() 为此对象添加属性。

values - 已关联的命令行参数，并提供相应的类型转换。 类型转换由 add_argument() 的 type 关键字参数来指定。

option_string - 被用来发起调用此动作的选项字符串。 option_string 参数是可选的，且此参数在动作关联到位置参数时将被略去。

__call__ 方法可以执行任意动作，但通常将基于 dest 和 values 来设置 namespace 的属性。

16.4.4. parse_args() 方法

ArgumentParser.parse_args(args=None, namespace=None)

将参数字符串转换为对象并将其设为命名空间的属性。 返回带有成员的命名空间。

之前对 add_argument() 的调用决定了哪些对象被创建以及它们如何被赋值。 请参阅 add_argument() 的文档了解详情。

args - 要解析的字符串列表。 默认情况下是从 sys.argv 获取。

namespace - 用于获取属性的对象。 默认值是一个新的空 Namespace 对象。

16.4.4.1. 选项值语法

parse_args() 方法支持多种指定选项值的方式（如果它接受选项的话）。 在最简单的情况下，选项和它的值是作为两个单独参数传入的:

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('-x')
>>> parser.add_argument('--foo')
>>> parser.parse_args(['-x', 'X'])
Namespace(foo=None, x='X')
>>> parser.parse_args(['--foo', 'FOO'])
Namespace(foo='FOO', x=None)

对于长选项（名称长度超过一个字符的选项），选项和值也可以作为单个命令行参数传入，使用 = 分隔它们即可:

>>> parser.parse_args(['--foo=FOO'])
Namespace(foo='FOO', x=None)

对于短选项（长度只有一个字符的选项），选项和它的值可以拼接在一起:

>>> parser.parse_args(['-xX'])
Namespace(foo=None, x='X')

有些短选项可以使用单个 - 前缀来进行合并，如果仅有最后一个选项（或没有任何选项）需要值的话:

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('-x', action='store_true')
>>> parser.add_argument('-y', action='store_true')
>>> parser.add_argument('-z')
>>> parser.parse_args(['-xyzZ'])
Namespace(x=True, y=True, z='Z')

16.4.4.2. 无效的参数

在解析命令行时，parse_args() 会检测多种错误，包括有歧义的选项、无效的类型、无效的选项、错误的位置参数个数等等。 当遇到这种错误时，它将退出并打印出错误文本同时附带用法消息:

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('--foo', type=int)
>>> parser.add_argument('bar', nargs='?')

>>> # invalid type
>>> parser.parse_args(['--foo', 'spam'])
usage: PROG [-h] [--foo FOO] [bar]
PROG: error: argument --foo: invalid int value: 'spam'

>>> # invalid option
>>> parser.parse_args(['--bar'])
usage: PROG [-h] [--foo FOO] [bar]
PROG: error: no such option: --bar

>>> # wrong number of arguments
>>> parser.parse_args(['spam', 'badger'])
usage: PROG [-h] [--foo FOO] [bar]
PROG: error: extra arguments found: badger

16.4.4.3. 包含 - 的参数

parse_args() 方法会在用户明显出错时尝试给出错误信息，但某些情况本身就存在歧义。 例如，命令行参数 -1 可能是尝试指定一个选项也可能是尝试提供一个位置参数。 parse_args() 方法在此会谨慎行事：位置参数只有在它们看起来像负数并且解析器中没有任何选项看起来像负数时才能以 - 打头。:

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('-x')
>>> parser.add_argument('foo', nargs='?')

>>> # no negative number options, so -1 is a positional argument
>>> parser.parse_args(['-x', '-1'])
Namespace(foo=None, x='-1')

>>> # no negative number options, so -1 and -5 are positional arguments
>>> parser.parse_args(['-x', '-1', '-5'])
Namespace(foo='-5', x='-1')

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('-1', dest='one')
>>> parser.add_argument('foo', nargs='?')

>>> # negative number options present, so -1 is an option
>>> parser.parse_args(['-1', 'X'])
Namespace(foo=None, one='X')

>>> # negative number options present, so -2 is an option
>>> parser.parse_args(['-2'])
usage: PROG [-h] [-1 ONE] [foo]
PROG: error: no such option: -2

>>> # negative number options present, so both -1s are options
>>> parser.parse_args(['-1', '-1'])
usage: PROG [-h] [-1 ONE] [foo]
PROG: error: argument -1: expected one argument

如果你有必须以 - 打头的位置参数并且看起来不像负数，你可以插入伪参数 '--' 以告诉 parse_args() 在那之后的内容是一个位置参数:

>>> parser.parse_args(['--', '-f'])
Namespace(foo='-f', one=None)

16.4.4.4. 参数缩写（前缀匹配）

parse_args() 方法 在默认情况下 允许将长选项缩写为前缀，如果缩写无歧义（即前缀与一个特定选项相匹配）的话:

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('-bacon')
>>> parser.add_argument('-badger')
>>> parser.parse_args('-bac MMM'.split())
Namespace(bacon='MMM', badger=None)
>>> parser.parse_args('-bad WOOD'.split())
Namespace(bacon=None, badger='WOOD')
>>> parser.parse_args('-ba BA'.split())
usage: PROG [-h] [-bacon BACON] [-badger BADGER]
PROG: error: ambiguous option: -ba could match -badger, -bacon

可产生一个以上选项的参数会引发错误。 此特定可通过将 allow_abbrev 设为 False 来禁用。

16.4.4.5. 在 sys.argv 以外

有时在 sys.argv 以外用 ArgumentParser 解析参数也是有用的。 这可以通过将一个字符串列表传给 parse_args() 来实现。 它适用于在交互提示符下进行检测:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument(
...     'integers', metavar='int', type=int, choices=range(10),
...     nargs='+', help='an integer in the range 0..9')
>>> parser.add_argument(
...     '--sum', dest='accumulate', action='store_const', const=sum,
...     default=max, help='sum the integers (default: find the max)')
>>> parser.parse_args(['1', '2', '3', '4'])
Namespace(accumulate=<built-in function max>, integers=[1, 2, 3, 4])
>>> parser.parse_args(['1', '2', '3', '4', '--sum'])
Namespace(accumulate=<built-in function sum>, integers=[1, 2, 3, 4])

16.4.4.6. 命名空间对象

class argparse.Namespace

由 parse_args() 默认使用的简单类，可创建一个存放属性的对象并将其返回。

这个类被有意做得很简单，只是一个具有可读字符串表示形式的 object。 如果你更喜欢类似字典的属性视图，你可以使用标准 Python 中惯常的 vars():

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo')
>>> args = parser.parse_args(['--foo', 'BAR'])
>>> vars(args)
{'foo': 'BAR'}

另一个用处是让 ArgumentParser 为一个已存在对象而不是为一个新的 Namespace 对象的属性赋值。 这可以通过指定 namespace= 关键字参数来实现:

>>> class C:
...     pass
...
>>> c = C()
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo')
>>> parser.parse_args(args=['--foo', 'BAR'], namespace=c)
>>> c.foo
'BAR'

16.4.5. 其它实用工具

16.4.5.1. 子命令

ArgumentParser.add_subparsers([title][, description][, prog][, parser_class][, action][, option_string][, dest][, help][, metavar])

许多程序都会将其功能拆分为一系列子命令，例如，svn 程序包含的子命令有 svn checkout, svn update 和 svn commit。 当一个程序能执行需要多组不同种类命令行参数时这种拆分功能的方式是一个非常好的主意。 ArgumentParser 通过 add_subparsers() 方法支持创建这样的子命令。 add_subparsers() 方法通常不带参数地调用并返回一个特殊的动作对象。 这种对象只有一个方法 add_parser()，它接受一个命令名称和任意多个 ArgumentParser 构造器参数，并返回一个可以通常方式进行修改的 ArgumentParser 对象。

形参的描述

title - 输出帮助的子解析器分组的标题；如果提供了描述则默认为 “subcommands”，否则使用位置参数的标题

description - 输出帮助中对子解析器的描述，默认为 None

prog - 将与子命令帮助一同显示的用法信息，默认为程序名称和子解析器参数之前的任何位置参数。

parser_class - 将被用于创建子解析器实例的类，默认为当前解析器类（例如 ArgumentParser）

action - 当此参数在命令行中出现时要执行动作的基本类型

dest - 将被用于保存子命令名称的属性名；默认为 None 即不保存任何值

help - 在输出帮助中的子解析器分组帮助信息，默认为 None

metavar - 帮助信息中表示可用子命令的字符串；默认为 None 并以 {cmd1, cmd2, ..} 的形式表示子命令

一些使用示例:

>>> # create the top-level parser
>>> parser = argparse.ArgumentParser(prog='PROG')
>>> parser.add_argument('--foo', action='store_true', help='foo help')
>>> subparsers = parser.add_subparsers(help='sub-command help')
>>>
>>> # create the parser for the "a" command
>>> parser_a = subparsers.add_parser('a', help='a help')
>>> parser_a.add_argument('bar', type=int, help='bar help')
>>>
>>> # create the parser for the "b" command
>>> parser_b = subparsers.add_parser('b', help='b help')
>>> parser_b.add_argument('--baz', choices='XYZ', help='baz help')
>>>
>>> # parse some argument lists
>>> parser.parse_args(['a', '12'])
Namespace(bar=12, foo=False)
>>> parser.parse_args(['--foo', 'b', '--baz', 'Z'])
Namespace(baz='Z', foo=True)

请注意 parse_args() 返回的对象将只包含主解析器和由命令行所选择的子解析器的属性（而没有任何其他子解析器）。 因此在上面的例子中，当指定了 a 命令时，将只存在 foo 和 bar 属性，而当指定了 b 命令时，则只存在 foo 和 baz 属性。

类似地，当从一个子解析器请求帮助消息时，只有该特定解析器的帮助消息会被打印出来。 帮助消息将不包括父解析器或同级解析器的消息。 （每个子解析器命令一条帮助消息，但是，也可以像上面那样通过提供 help= 参数给 add_parser() 来给出。）

>>> parser.parse_args(['--help'])
usage: PROG [-h] [--foo] {a,b} ...

positional arguments:
  {a,b}   sub-command help
    a     a help
    b     b help

optional arguments:
  -h, --help  show this help message and exit
  --foo   foo help

>>> parser.parse_args(['a', '--help'])
usage: PROG a [-h] bar

positional arguments:
  bar     bar help

optional arguments:
  -h, --help  show this help message and exit

>>> parser.parse_args(['b', '--help'])
usage: PROG b [-h] [--baz {X,Y,Z}]

optional arguments:
  -h, --help     show this help message and exit
  --baz {X,Y,Z}  baz help

add_subparsers() 方法也支持 title 和 description 关键字参数。 当两者都存在时，子解析器的命令将出现在输出帮助消息中它们自己的分组内。 例如:

>>> parser = argparse.ArgumentParser()
>>> subparsers = parser.add_subparsers(title='subcommands',
...                                    description='valid subcommands',
...                                    help='additional help')
>>> subparsers.add_parser('foo')
>>> subparsers.add_parser('bar')
>>> parser.parse_args(['-h'])
usage:  [-h] {foo,bar} ...

optional arguments:
  -h, --help  show this help message and exit

subcommands:
  valid subcommands

  {foo,bar}   additional help

此外，add_parser 还支持附加的 aliases 参数，它允许多个字符串指向同一子解析器。 这个例子类似于 svn，将别名 co 设为 checkout 的缩写形式:

>>> parser = argparse.ArgumentParser()
>>> subparsers = parser.add_subparsers()
>>> checkout = subparsers.add_parser('checkout', aliases=['co'])
>>> checkout.add_argument('foo')
>>> parser.parse_args(['co', 'bar'])
Namespace(foo='bar')

一个特别有效的处理子命令的方式是将 add_subparsers() 方法与对 set_defaults() 的调用结合起来使用，这样每个子解析器就能知道应当执行哪个 Python 函数。 例如:

>>> # sub-command functions
>>> def foo(args):
...     print(args.x * args.y)
...
>>> def bar(args):
...     print('((%s))' % args.z)
...
>>> # create the top-level parser
>>> parser = argparse.ArgumentParser()
>>> subparsers = parser.add_subparsers()
>>>
>>> # create the parser for the "foo" command
>>> parser_foo = subparsers.add_parser('foo')
>>> parser_foo.add_argument('-x', type=int, default=1)
>>> parser_foo.add_argument('y', type=float)
>>> parser_foo.set_defaults(func=foo)
>>>
>>> # create the parser for the "bar" command
>>> parser_bar = subparsers.add_parser('bar')
>>> parser_bar.add_argument('z')
>>> parser_bar.set_defaults(func=bar)
>>>
>>> # parse the args and call whatever function was selected
>>> args = parser.parse_args('foo 1 -x 2'.split())
>>> args.func(args)
2.0
>>>
>>> # parse the args and call whatever function was selected
>>> args = parser.parse_args('bar XYZYX'.split())
>>> args.func(args)
((XYZYX))

通过这种方式，你可以在参数解析结束后让 parse_args() 执行调用适当函数的任务。 像这样将函数关联到动作通常是你处理每个子解析器的不同动作的最简便方式。 但是，如果有必要检查被发起调用的子解析器的名称，则 add_subparsers() 调用的 dest 关键字参数将可实现:

>>> parser = argparse.ArgumentParser()
>>> subparsers = parser.add_subparsers(dest='subparser_name')
>>> subparser1 = subparsers.add_parser('1')
>>> subparser1.add_argument('-x')
>>> subparser2 = subparsers.add_parser('2')
>>> subparser2.add_argument('y')
>>> parser.parse_args(['2', 'frobble'])
Namespace(subparser_name='2', y='frobble')

16.4.5.2. FileType 对象

class argparse.FileType(mode='r', bufsize=-1, encoding=None, errors=None)

FileType 工厂类用于创建可作为 ArgumentParser.add_argument() 的 type 参数传入的对象。 以 FileType 对象作为其类型的参数将使用命令行参数以所请求模式、缓冲区大小、编码格式和错误处理方式打开文件（请参阅 open() 函数了解详情）:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--raw', type=argparse.FileType('wb', 0))
>>> parser.add_argument('out', type=argparse.FileType('w', encoding='UTF-8'))
>>> parser.parse_args(['--raw', 'raw.dat', 'file.txt'])
Namespace(out=<_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'>, raw=<_io.FileIO name='raw.dat' mode='wb'>)

FileType 对象能理解伪参数 '-' 并会自动将其转换为 sys.stdin 用于可读的 FileType 对象，或是 sys.stdout 用于可写的 FileType 对象:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('infile', type=argparse.FileType('r'))
>>> parser.parse_args(['-'])
Namespace(infile=<_io.TextIOWrapper name='<stdin>' encoding='UTF-8'>)

3.4 新版功能: encodings 和 errors 关键字参数。

16.4.5.3. 参数组

ArgumentParser.add_argument_group(title=None, description=None)

在默认情况下，ArgumentParser 会在显示帮助消息时将命令行参数分为“位置参数”和“可选参数”两组。 当存在比默认更好的参数分组概念时，可以使用 add_argument_group() 方法来创建适当的分组:

>>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)
>>> group = parser.add_argument_group('group')
>>> group.add_argument('--foo', help='foo help')
>>> group.add_argument('bar', help='bar help')
>>> parser.print_help()
usage: PROG [--foo FOO] bar

group:
  bar    bar help
  --foo FOO  foo help

add_argument_group() 方法返回一个具有 add_argument() 方法的参数分组对象，这与常规的 ArgumentParser 一样。 当一个参数被加入分组时，解析器会将它视为一个正常的参数，但是会在不同的帮助消息分组中显示该参数。 add_argument_group() 方法接受 title 和 description 参数，它们可被用来定制显示内容:

>>> parser = argparse.ArgumentParser(prog='PROG', add_help=False)
>>> group1 = parser.add_argument_group('group1', 'group1 description')
>>> group1.add_argument('foo', help='foo help')
>>> group2 = parser.add_argument_group('group2', 'group2 description')
>>> group2.add_argument('--bar', help='bar help')
>>> parser.print_help()
usage: PROG [--bar BAR] foo

group1:
  group1 description

  foo    foo help

group2:
  group2 description

  --bar BAR  bar help

请注意任意不在你的自定义分组中的参数最终都将回到通常的“位置参数”和“可选参数”分组中。

16.4.5.4. 互斥

ArgumentParser.add_mutually_exclusive_group(required=False)

创建一个互斥组。 argparse 将会确保互斥组中只有一个参数在命令行中可用:

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> group = parser.add_mutually_exclusive_group()
>>> group.add_argument('--foo', action='store_true')
>>> group.add_argument('--bar', action='store_false')
>>> parser.parse_args(['--foo'])
Namespace(bar=True, foo=True)
>>> parser.parse_args(['--bar'])
Namespace(bar=False, foo=False)
>>> parser.parse_args(['--foo', '--bar'])
usage: PROG [-h] [--foo | --bar]
PROG: error: argument --bar: not allowed with argument --foo

add_mutually_exclusive_group() 方法也接受一个 required 参数，表示在互斥组中至少有一个参数是需要的:

>>> parser = argparse.ArgumentParser(prog='PROG')
>>> group = parser.add_mutually_exclusive_group(required=True)
>>> group.add_argument('--foo', action='store_true')
>>> group.add_argument('--bar', action='store_false')
>>> parser.parse_args([])
usage: PROG [-h] (--foo | --bar)
PROG: error: one of the arguments --foo --bar is required

注意，目前互斥参数组不支持 add_argument_group() 的 title 和 description 参数。

16.4.5.5. 解析器默认值

ArgumentParser.set_defaults(**kwargs)

在大多数时候，parse_args() 所返回对象的属性将完全通过检查命令行参数和参数动作来确定。 set_defaults() 则允许加入一些无须任何命令行检查的额外属性:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('foo', type=int)
>>> parser.set_defaults(bar=42, baz='badger')
>>> parser.parse_args(['736'])
Namespace(bar=42, baz='badger', foo=736)

请注意解析器层级的默认值总是会覆盖参数层级的默认值:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', default='bar')
>>> parser.set_defaults(foo='spam')
>>> parser.parse_args([])
Namespace(foo='spam')

解析器层级默认值在需要多解析器时会特别有用。 请参阅 add_subparsers() 方法了解此类型的一个示例。

ArgumentParser.get_default(dest)

获取一个命名空间属性的默认值，该值是由 add_argument() 或 set_defaults() 设置的:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', default='badger')
>>> parser.get_default('foo')
'badger'

16.4.5.6. 打印帮助

在大多数典型应用中，parse_args() 将负责任何用法和错误消息的格式化和打印。 但是，也可使用某些其他格式化方法:

ArgumentParser.print_usage(file=None)

打印一段简短描述，说明应当如何在命令行中发起调用 ArgumentParser。 如果 file 为 None，则默认使用 sys.stdout。

ArgumentParser.print_help(file=None)

打印一条帮助消息，包括程序用法和通过 ArgumentParser 注册的相关参数信息。 如果 file 为 None，则默认使用 sys.stdout。

还存在这些方法的几个变化形式，它们只返回字符串而不打印消息:

ArgumentParser.format_usage()

返回一个包含简短描述的字符串，说明应当如何在命令行中发起调用 ArgumentParser。

ArgumentParser.format_help()

反回一个包含帮助消息的字符串，包括程序用法和通过 ArgumentParser 注册的相关参数信息。

16.4.5.7. 部分解析

ArgumentParser.parse_known_args(args=None, namespace=None)

有时一个脚本可能只解析部分命令行参数，而将其余的参数继续传递给另一个脚本或程序。 在这种情况下，parse_known_args() 方法会很有用处。 它的作用方式很类似 parse_args() 但区别在于当存在额外参数时它不会产生错误。 而是会返回一个由两个条目构成的元组，其中包含带成员的命名空间和剩余参数字符串的列表。

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action='store_true')
>>> parser.add_argument('bar')
>>> parser.parse_known_args(['--foo', '--badger', 'BAR', 'spam'])
(Namespace(bar='BAR', foo=True), ['--badger', 'spam'])

警告

前缀匹配 规则应用于 parse_known_args()。 一个选项即使只是已知选项的前缀部分解析器也能识别该选项，不会将其放入剩余参数列表。

16.4.5.8. 自定义文件解析

ArgumentParser.convert_arg_line_to_args(arg_line)

从文件读取的参数（见 ArgumentParser 的 fromfile_prefix_chars 关键字参数）将是一行读取一个参数。 convert_arg_line_to_args() 可被重载以使用更复杂的读取方式。

此方法接受从参数文件读取的字符串形式的单个参数 arg_line。 它返回从该字符串解析出的参数列表。 此方法将在每次按顺序从参数文件读取一行时被调用一次。

此方法的一个有用的重载是将每个以空格分隔的单词视为一个参数。 下面的例子演示了如何实现此重载:

class MyArgumentParser(argparse.ArgumentParser):
    def convert_arg_line_to_args(self, arg_line):
        return arg_line.split()

16.4.5.9. 退出方法

ArgumentParser.exit(status=0, message=None)

此方法将终结程序，附带指定的 status 退出，并且如果给出了 message 则会在退出前将其打印输出。

ArgumentParser.error(message)

此方法将向标准错误打印包括 message 的用法消息并附带状态码 2 终结程序。

16.4.6. 升级 optparse 代码

起初，argparse 曾经尝试通过 optparse 来维持兼容性。 但是，optparse 很难透明地扩展，特别是那些为支持新的 nargs= 描述方式和更好的用法消息所需的修改。当When most everything in optparse 中几乎所有内容都已被复制粘贴或打上补丁时，维持向下兼容看来已是不切实际的。

argparse 模块在许多方面对标准库的 optparse 模块进行了增强，包括:

处理位置参数。

支持子命令。

允许替代选项前缀例如 + 和 /。

处理零个或多个以及一个或多个风格的参数。

生成更具信息量的用法消息。

提供用于定制 type 和 action 的更为简单的接口。

从 optparse 到 argparse 的部分升级路径:

将所有 optparse.OptionParser.add_option() 调用替换为 ArgumentParser.add_argument() 调用。

将 (options, args) = parser.parse_args() 替换为 args = parser.parse_args() 并为位置参数添加额外的 ArgumentParser.add_argument() 调用。 请注意之前所谓的 options 在 argparse 上下文中被称为 args。

Replace optparse.OptionParser.disable_interspersed_args() by setting nargs of a positional argument to argparse.REMAINDER, or use parse_known_args() to collect unparsed argument strings in a separate list.

将回调动作和 callback_* 关键字参数替换为 type 或 action 参数。

将 type 关键字参数字符串名称替换为相应的类型对象（例如 int, float, complex 等）。

将 optparse.Values 替换为 Namespace 并将 optparse.OptionError 和 optparse.OptionValueError 替换为 ArgumentError。

将隐式参数字符串例如使用标准 Python 字典语法的 %default 或 %prog 替换为格式字符串，即 %(default)s 和 %(prog)s。

将 OptionParser 构造器 version 参数替换为对 parser.add_argument('--version', action='version', version='<the version>') 的调用。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

16.5. getopt — C 风格的命令行选项解析器

资源代码： 资源：’Lib/getopt.py’

注解

getopt 模块是一个命令行选项解析器，其 API 设计会让 C getopt() 函数的用户感到熟悉。 不熟悉 C getopt() 函数或者希望写更少代码并获得更完善帮助和错误消息的用户应当考虑改用 argparse 模块。

此模块可协助脚本解析 sys.argv 中的命令行参数。 它支持与 Unix getopt() 函数相同的惯例（包括形式如 ‘-‘ 与 ‘--‘ 的参数的特殊含义）。 也能通过可选的第三个参数来使用与 GNU 软件所支持形式相类似的长选项。

此模块提供了两个函数和一个异常:

getopt.getopt(args, shortopts, longopts=[])

解析命令行选项与形参列表。 args 为要解析的参数列表，不包含最开头的对正在运行的程序的引用。 通常这意味着 sys.argv[1:]。 shortopts 为脚本所要识别的字母选项，包含要求后缀一个冒号 (':'；即与 Unix getopt() 所用的格式相同) 的选项。

注解

与 GNU getopt() 不同，在非选项参数之后，所有后续参数都会被视为非选项。 这类似于非 GNU Unix 系统的运作方式。

如果指定了 longopts，则必须为一个由应当被支持的长选项名称组成的列表。 开头的 '--' 字符不应被包括在选项名称中。 要求参数的长选项后应当带一个等号 ('=')。 可选参数不被支持。 如果想仅接受长选项，则 shortopts 应为一个空字符串。 命令行中的长选项只要提供了恰好能匹配可接受选项之一的选项名称前缀即可被识别。 举例来说，如果 longopts 为 ['foo', 'frob']，则选项 --fo 将匹配为 --foo，但 --f 将不能得到唯一匹配，因此将引发 GetoptError。

返回值由两个元素组成：第一个是 (option, value) 对的列表；第二个是在去除该选项列表后余下的程序参数列表（这也就是 args 的尾部切片）。每个被返回的选项与值对的第一个元素是选项，短选项前缀一个连字符 (例如 '-x')，长选项则前缀两个连字符 (例如 '--long-option')，第二个元素是选项参数，如果选项不带参数则为空字符串。 列表中选项的排列顺序与它们被解析的顺序相同，因此允许多次出现。 长选项与短选项可以混用。

getopt.gnu_getopt(args, shortopts, longopts=[])

此函数与 getopt() 类似，区别在于它默认使用 GNU 风格的扫描模式。 这意味着选项和非选项参数可能会混在一起。 getopt() 函数将在遇到非选项参数时立即停止处理选项。

如果选项字符串的第一个字符为 '+'，或者如果设置了环境变量 POSIXLY_CORRECT，则选项处理会在遇到非选项参数时立即停止。

exception getopt.GetoptError

This is raised当参数列表中出现不可识别的选项或者当一个需要参数的选项未带参数时将引发此异常。 此异常的参数是一个指明错误原因的字符串。 对于长选项，将一个参数传给不需要参数的选项也将导致引发此异常。 msg 和 opt 属性会给出错误消息和关联的选项；如果没有关联到异常的特定选项，则 opt 将为空字符串。

exception getopt.error

GetoptError 的别名；用于向后兼容。

一个仅使用 Unix 风格选项的例子:

>>> import getopt
>>> args = '-a -b -cfoo -d bar a1 a2'.split()
>>> args
['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'abc:d:')
>>> optlist
[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
>>> args
['a1', 'a2']

使用长选项名也同样容易:

>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
>>> args = s.split()
>>> args
['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'x', [
...     'condition=', 'output-file=', 'testing'])
>>> optlist
[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
>>> args
['a1', 'a2']

在脚本中，典型的用法类似这样:

import getopt, sys

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
    except getopt.GetoptError as err:
        # print help information and exit:
        print(err)  # will print something like "option -a not recognized"
        usage()
        sys.exit(2)
    output = None
    verbose = False
    for o, a in opts:
        if o == "-v":
            verbose = True
        elif o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("-o", "--output"):
            output = a
        else:
            assert False, "unhandled option"
    # ...

if __name__ == "__main__":
    main()

请注意通过 argparse 模块可以使用更少的代码并附带更详细的帮助与错误消息生成等价的命令行接口:

import argparse

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-o', '--output')
    parser.add_argument('-v', dest='verbose', action='store_true')
    args = parser.parse_args()
    # ... do something with args.output ...
    # ... do something with args.verbose ..

参见

模块 argparse

替代的命令行选项和参数解析库。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

16.6. 模块 logging — Python 的日志记录工具

源代码: Lib/logging/__init__.py [https://github.com/python/cpython/tree/3.6/Lib/logging/__init__.py]

Important

此页面仅包含API参考信息。有关更多高级主题的教程信息和讨论，请参阅

基础教程

进阶教程

Logging Cookbook

这个模块为应用与库定义了实现灵活的事件日志系统的函数与类.

使用标准库提提供的 logging API 最主要的好处是，所有的 Python 模块都可能参与日志输出，包括你的日志消息和第三方模块的日志消息。

这个模块提供许多强大而灵活的功能。如果你对 logging 不太熟悉的话， 掌握它最好的方式就是查看它对应的教程（详见右侧的链接）。

该模块定义的基础类和函数都列在下面。

记录器暴露了应用程序代码直接使用的接口。

处理程序将日志记录（由记录器创建）发送到适当的目标。

过滤器提供了更精细的附加功能，用于确定要输出的日志记录。

格式化程序指定最终输出中日志记录的样式。

16.6.1. Logger 对象

Loggers have the following attributes and methods. Note that Loggers are never instantiated directly, but always through the module-level function logging.getLogger(name). Multiple calls to getLogger() with the same name will always return a reference to the same Logger object.

name 是潜在的周期分割层级值, 像``foo.bar.baz`` (例如, 抛出的可以只是明文的``foo``)。Loggers是进一步在子层次列表的更高loggers列表。例如，有个名叫``foo``的logger，名叫``foo.bar``，foo.bar.baz, 和 foo.bam 都是 foo``的衍生logger. logger的名字分级类似Python 包的层级, 并且相同的如果你组织你的loggers在每模块级别基本上使用推荐的结构``logging.getLogger(__name__)。这是因为在模块里，在Python包的命名空间的模块名为``__name__``。

class logging.Logger

propagate

如果这个属性为真，记录到这个记录器的事件将会传递给这个高级别管理器的记录器（原型），此外任何关联到这个记录器的管理器。消息会直接传递给原型记录器的管理器 - 既不是这个原型记录器的级别也不是过滤器是在考虑的问题。

如果等于假，记录消息将不会传递给这个原型记录器的管理器。

构造器将这个属性初始化为 True。

注解

如果你关联了一个管理器*并且*到它自己的一个或多个记录器，它可能发出多次相同的记录。总体来说，你不需要关联管理器到一个或多个记录器 - 如果你只是关联它到一个合适的记录器等级中的最高级别记录器，它将会看到子记录器所有记录的事件，他们的传播剩下的设置为``True``。一个通用场景是只关联管理器到根记录器，并且让传播照顾剩下的.

setLevel(level)

给 logger 设置阈值为 level 。日志等级小于 level 会被忽略。严重性为 level 或更高的日志消息将由该 logger 的任何一个或多个 handler 发出，除非将处理程序的级别设置为比 level 更高的级别。

创建一个 logger 时，设置级别为 NOTSET （当 logger 是根 logger 时，将处理所有消息；当 logger 是非根 logger 时，所有消息会委派给父级）。注意根 logger 创建时使用的是 WARNING 级别。

委派给父级的意思是如果一个记录器的级别设置为NOTSET，遍历其祖先记录器链，直到找到另一个NOTSET级别的祖先或到达根为止。

如果发现某个父级的级别 不是NOTSET ，那么该父级的级别将被视为发起搜索的记录器的有效级别，并用于确定如何处理日志事件。

如果到达根 logger ，并且其级别为NOTSET，则将处理所有消息。否则，将使用根记录器的级别作为有效级别。

参见 日志级别 级别列表。

在 3.2 版更改: 现在 level 参数可以接受形如 ‘INFO’ 的级别字符串表示形式，以代替形如 INFO 的整数常量。 但是请注意，级别在内部存储为整数，并且 getEffectiveLevel() 和 isEnabledFor() 等方法的传入/返回值也为整数。

isEnabledFor(lvl)

Indicates if a message of severity lvl would be processed by this logger. This method checks first the module-level level set by logging.disable(lvl) and then the logger’s effective level as determined by getEffectiveLevel().

getEffectiveLevel()

指示此记录器的有效级别。如果通过 setLevel() 设置了除 NOTSET 以外的值，则返回该值。否则，将层次结构遍历到根，直到找到除 NOTSET 以外的其他值，然后返回该值。返回的值是一个整数，通常为 logging.DEBUG、 logging.INFO 等等。

getChild(suffix)

返回由后缀确定的，是该记录器的后代的记录器。 因此，logging.getLogger('abc').getChild('def.ghi') 与 logging.getLogger('abc.def.ghi') 将返回相同的记录器。 这是一个便捷方法，当使用如 __name__ 而不是字符串字面值命名父记录器时很有用。

3.2 新版功能.

debug(msg, *args, **kwargs)

Logs a message with level DEBUG on this logger. The msg is the message format string, and the args are the arguments which are merged into msg using the string formatting operator. (Note that this means that you can use keywords in the format string, together with a single dictionary argument.)

There are three keyword arguments in kwargs which are inspected: exc_info, stack_info, and extra.

如果 exc_info 的求值结果不为 false，则它将异常信息添加到日志消息中。如果提供了一个异常元组（按照 sys.exc_info() 返回的格式）或一个异常实例，则将其使用；否则，调用 sys.exc_info() 以获取异常信息。

第二个可选关键字参数是 stack_info，默认为 False。如果为 True，则将堆栈信息添加到日志消息中，包括实际的日志调用。请注意，这与通过指定 exc_info 显示的堆栈信息不同：前者是从堆栈底部到当前线程中的日志记录调用的堆栈帧，而后者是在搜索异常处理程序时，跟踪异常而打开的堆栈帧的信息。

您可以独立于 exc_info 来指定 stack_info，例如，即使在未引发任何异常的情况下，也可以显示如何到达代码中的特定点。堆栈帧在标题行之后打印：

Stack (most recent call last):

这模仿了显示异常帧时所使用的 Traceback (most recent call last): 。

The third keyword argument is extra which can be used to pass a dictionary which is used to populate the __dict__ of the LogRecord created for the logging event with user-defined attributes. These custom attributes can then be used as you like. For example, they could be incorporated into logged messages. For example:

FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
logging.basicConfig(format=FORMAT)
d = {'clientip': '192.168.0.1', 'user': 'fbloggs'}
logger = logging.getLogger('tcpserver')
logger.warning('Protocol problem: %s', 'connection reset', extra=d)

输出类似于

2006-02-08 22:20:02,165 192.168.0.1 fbloggs  Protocol problem: connection reset

The keys in the dictionary passed in extra should not clash with the keys used by the logging system. (See the Formatter documentation for more information on which keys are used by the logging system.)

如果你选择在已记录的消息中使用这些属性，则需要格外小心。 例如在上面的示例中，Formatter 已设置了格式字符串，其在 LogRecord 的属性字典中应有 ‘clientip’ 和 ‘user’。 如果缺少这些属性，消息将不被记录，因为会引发字符串格式化异常，你始终需要传入带有这些键的 extra 字典。

尽管这可能很烦人，但此功能旨在用于特殊情况，例如在多个上下文中执行相同代码的多线程服务器，并且出现的有趣条件取决于此上下文（例如在上面的示例中就是远程客户端IP地址和已验证用户名）。在这种情况下，很可能将专门的 Formatter 与特定的 Handler 一起使用。

3.2 新版功能: 增加了 stack_info 参数。

在 3.5 版更改: The exc_info parameter can now accept exception instances.

info(msg, *args, **kwargs)

在此记录器上记录 INFO 级别的消息。参数解释同 debug()。

warning(msg, *args, **kwargs)

在此记录器上记录 WARNING 级别的消息。参数解释同 debug()。

注解

有一个功能上与 warning 一致的方法 warn。由于 warn 已被弃用，请不要使用它 —— 改为使用 warning。

error(msg, *args, **kwargs)

在此记录器上记录 ERROR 级别的消息。参数解释同 debug()。

critical(msg, *args, **kwargs)

在此记录器上记录 CRITICAL 级别的消息。参数解释同 debug()。

log(lvl, msg, *args, **kwargs)

Logs a message with integer level lvl on this logger. The other arguments are interpreted as for debug().

exception(msg, *args, **kwargs)

在此记录器上记录 ERROR 级别的消息。参数解释同 debug()。异常信息将添加到日志消息中。仅应从异常处理程序中调用此方法。

addFilter(filter)

将指定的过滤器 filter 添加到此记录器。

removeFilter(filter)

从此记录器中删除指定的处理程序 filter。

filter(record)

Applies this logger’s filters to the record and returns a true value if the record is to be processed. The filters are consulted in turn, until one of them returns a false value. If none of them return a false value, the record will be processed (passed to handlers). If one returns a false value, no further processing of the record occurs.

addHandler(hdlr)

将指定的处理程序 hdlr 添加到此记录器。

removeHandler(hdlr)

从此记录器中删除指定的处理器 hdlr。

findCaller(stack_info=False)

查找调用源的文件名和行号，以 文件名，行号，函数名称和堆栈信息 4元素元组的形式返回。堆栈信息将返回 None``除非 *stack_info* 为 ``True。

handle(record)

通过将记录传递给与此记录器及其祖先关联的所有处理器来处理（直到某个 propagate 值为 false）。此方法用于从套接字接收的未序列化的以及在本地创建的记录。使用 filter() 进行记录程序级别过滤。

makeRecord(name, lvl, fn, lno, msg, args, exc_info, func=None, extra=None, sinfo=None)

这是一种工厂方法，可以在子类中对其进行重写以创建专门的 LogRecord 实例。

hasHandlers()

检查此记录器是否配置了任何处理器。通过在此记录器及其记录器层次结构中的父级中查找处理器完成此操作。如果找到处理器则返回 True，否则返回``False``。只要找到 “propagate” 属性设置为 false的记录器，该方法就会停止搜索层次结构 —— 其将是最后一个检查处理器是否存在的记录器。

3.2 新版功能.

在 3.7 版更改: Loggers can now be picked and unpickled.

16.6.2. 日志级别

日志记录级别的数值在下表中给出。如果你想要定义自己的级别，并且需要它们具有相对于预定义级别的特定值，那么这些内容可能是你感兴趣的。如果你定义具有相同数值的级别，它将覆盖预定义的值; 预定义的名称丢失。

级别

数值

CRITICAL

50

ERROR

40

WARNING

30

INFO

20

DEBUG

10

NOTSET

0

16.6.3. 处理器对象

Handler 有以下属性和方法。注意不要直接实例化 Handler ；这个类用来派生其他更有用的子类。但是，子类的 __init__() 方法需要调用 Handler.__init__() 。

class logging.Handler

__init__(level=NOTSET)

初始化 Handler 实例时，需要设置它的级别，将过滤列表置为空，并且创建锁（通过 createLock() ）来序列化对 I/O 的访问。

createLock()

初始化一个线程锁，用来序列化对底层的 I/O 功能的访问，底层的 I/O 功能可能不是线程安全的。

acquire()

使用 createLock() 获取线程锁。

release()

使用 acquire() 来释放线程锁。

setLevel(level)

给处理器设置阈值为 level 。日志级别小于 level 将被忽略。创建处理器时，日志级别被设置为 NOTSET （所有的消息都会被处理）。

参见 日志级别 级别列表。

在 3.2 版更改: level 形参现在接受像 ‘INFO’ 这样的字符串形式的级别表达方式，也可以使用像 INFO 这样的整数常量。

setFormatter(fmt)

将此处理器的 Formatter 设置为 fmt。

addFilter(filter)

将指定的过滤器 filter 添加到此处理器。

removeFilter(filter)

从此处理器中删除指定的过滤器 filter 。

filter(record)

Applies this handler’s filters to the record and returns a true value if the record is to be processed. The filters are consulted in turn, until one of them returns a false value. If none of them return a false value, the record will be emitted. If one returns a false value, the handler will not emit the record.

flush()

确保所有日志记录从缓存输出。此版本不执行任何操作，并且应由子类实现。

close()

整理处理器使用的所有资源。此版本不输出，但从内部处理器列表中删除处理器，内部处理器在 shutdown() 被调用时关闭 。子类应确保从重写的 close() 方法中调用此方法。

handle(record)

经已添加到处理器的过滤器过滤后，有条件地发出指定的日志记录。用获取/释放 I/O 线程锁包装记录的实际发出行为。

handleError(record)

调用 emit() 期间遇到异常时，应从处理器中调用此方法。如果模块级属性 raiseExceptions 是 False，则异常将被静默忽略。这是大多数情况下日志系统需要的 —— 大多数用户不会关心日志系统中的错误，他们对应用程序错误更感兴趣。但是，你可以根据需要将其替换为自定义处理器。指定的记录是发生异常时正在处理的记录。（raiseExceptions 的默认值是 True，因为这在开发过程中是比较有用的）。

format(record)

如果设置了格式器则用其对记录进行格式化。否则，使用模块的默认格式器。

emit(record)

执行实际记录给定日志记录所需的操作。这个版本应由子类实现，因此这里直接引发 NotImplementedError 异常。

有关作为标准随附的处理程序，请参见 logging.handlers。

16.6.4. 格式器对象

Formatter 对象拥有以下的属性和方法。一般情况下，它们负责将 LogRecord 转换为可由人或外部系统解释的字符串。基础的 Formatter 允许指定格式字符串。如果未提供任何值，则使用默认值 '%(message)s' ，它仅将消息包括在日志记录调用中。要在格式化输出中包含其他信息（如时间戳），请阅读下文。

格式器可以使用格式化字符串来初始化，该字符串利用 LogRecord 的属性 —— 例如上述默认值，用户的消息和参数预先格式化为 LogRecord 的 message 属性后被使用。此格式字符串包含标准的 Python %-s 样式映射键。有关字符串格式的更多信息，请参见 printf 风格的字符串格式化。

The useful mapping keys in a LogRecord are given in the section on LogRecord 属性.

class logging.Formatter(fmt=None, datefmt=None, style='%')

返回 Formatter 类的新实例。实例将使用整个消息的格式字符串以及消息的日期/时间部分的格式字符串进行初始化。如果未指定 fmt ，则使用 '%(message)s'。如果未指定 datefmt，则使用 formatTime() 文档中描述的格式。

style 形参可以是 ‘%’, ‘{‘ 或 ‘$’ 之一，它决定格式字符串如何与数据进行合并：使用 %-formatting, str.format() 或 string.Template 之一。 请参阅 Using particular formatting styles throughout your application 了解有关在日志消息中使用 {- 和 $-formatting 的更多信息。

在 3.2 版更改: style 参数已加入.

format(record)

记录的属性字典用作字符串格式化操作的参数。返回结果字符串。在格式化字典之前，需要执行几个准备步骤。 使用 msg % args 计算记录的 message 属性。如果格式化字符串包含 '(asctime)'，则调用 formatTime() 来格式化事件时间。如果有异常信息，则使用 formatException() 将其格式化并附加到消息中。请注意，格式化的异常信息缓存在属性 exc_text 中。这很有用，因为可以对异常信息进行序列化并通过网络发送，但是如果您有不止一个定制了异常信息格式的 Formatter 子类，则应格外小心。在这种情况下，您必须在格式器完成格式化后清除缓存的值，以便下一个处理事件的格式化程序不使用缓存的值，而是重新计算它。

如果栈信息可用，它将被添加在异常信息之后，如有必要请使用 formatStack() 来转换它。

formatTime(record, datefmt=None)

此方法应由想要使用格式化时间的格式器中的 format() 调用。可以在格式器中重写此方法以提供任何特定要求，但是基本行为如下：如果指定了 datefmt （字符串），则将其用于 time.strftime() 来格式化记录的创建时间。否则，使用格式 ‘%Y-%m-%d %H:%M:%S,uuu’，其中 uuu 部分是毫秒值，其他字母根据 time.strftime() 文档。这种时间格式的示例为 2003-01-23 00:29:50,411。返回结果字符串。

此函数使用一个用户可配置函数将创建时间转换为元组。 默认情况下，使用 time.localtime()；要为特定格式化程序实例更改此项，请将实例的 converter 属性设为具有与 time.localtime() 或 time.gmtime() 相同签名的函数。 要为所有格式化程序更改此项，例如当你希望所有日志时间都显示为 GMT，请在 Formatter 类中设置 converter 属性。

在 3.3 版更改: 在之前版本中，默认格式为写死的代码，例如这个例子: 2010-09-06 22:38:15,292 其中逗号之前的部分由 strptime 格式字符串 ('%Y-%m-%d %H:%M:%S') 处理，而逗号之后的部分为毫秒值。 因为 strptime 没有表示毫秒的占位符，毫秒值使用了另外的格式字符串来添加 '%s,%03d' — 这两个格式字符串代码都是写死在该方法中的。 经过修改，这些字符串被定义为类层级的属性，当需要时可以在实例层级上被重载。 属性的名称为 default_time_format (用于 strptime 格式字符串) 和 default_msec_format (用于添加毫秒值)。

formatException(exc_info)

将指定的异常信息（由 sys.exc_info() 返回的标准异常元组）格式化为字符串。 这个默认实现只使用了 traceback.print_exception()。 结果字符串将被返回。

formatStack(stack_info)

将指定的堆栈信息（由 traceback.print_stack() 返回的字符串，但移除末尾的换行符）格式化为字符串。 这个默认实现只是返回输入值。

16.6.5. Filter 对象

Filters 可被 Handlers 和 Loggers 用来实现比按层级提供更复杂的过滤操作。 基本过滤器类只允许低于日志记录器层级结构中低于特定层级的事件。 例如，一个用 ‘A.B’ 初始化的过滤器将允许 ‘A.B’, ‘A.B.C’, ‘A.B.C.D’, ‘A.B.D’ 等日志记录器所记录的事件。 但 ‘A.BB’, ‘B.A.B’ 等则不允许。 如果用空字符串初始化，则所有事件都会被略过。

class logging.Filter(name='')

返回一个 Filter 类的实例。 如果指定了 name，则它将被用来为日志记录器命名，该类及其子类将通过该过滤器允许指定事件通过。 如果 name 为空字符串，则允许所有事件通过。

filter(record)

是否要记录指定的记录？返回零表示否，非零表示是。如果认为合适，则可以通过此方法就地修改记录。

请注意关联到处理程序的过滤器会在事件由处理程序发出之前被查询，而关联到日志记录器的过滤器则会在有事件被记录的的任何时候（使用 debug(), info() 等等）在将事件发送给处理程序之前被查询。 这意味着由后代日志记录器生成的事件将不会被日志记录器的过滤器设置所过滤，除非该过滤器也已被应用于后代日志记录器。

你实际上不需要子类化 Filter: 你可以将传入任何包含 filter 方法的具有相同语义的的实例。

在 3.2 版更改: 你不需要创建专门的 Filter 类，或使用具有 filter 方法的其他类：你可以使用一个函数（或其他可调用对象）作为过滤器。 过滤逻辑将检查过滤器对象是否文化的 filter 属性：如果有，就会将它当作是 Filter 并调用它的 filter() 方法。 在其他情况下，则会将它当作是可调用对象并附带记录作为单一形参进行调用。 返回值应当与 filter() 的返回值相一致。

Although filters are used primarily to filter records based on more sophisticated criteria than levels, they get to see every record which is processed by the handler or logger they’re attached to: this can be useful if you want to do things like counting how many records were processed by a particular logger or handler, or adding, changing or removing attributes in the LogRecord being processed. Obviously changing the LogRecord needs to be done with some care, but it does allow the injection of contextual information into logs (see 使用过滤器传递上下文信息).

16.6.6. LogRecord 属性

LogRecord 实例是每当有日志被记录时由 Logger 自动创建的，并且可通过 makeLogRecord() 手动创建（例如根据从网络接收的已封存事件创建）。

class logging.LogRecord(name, level, pathname, lineno, msg, args, exc_info, func=None, sinfo=None)

包含与被记录的事件相关的所有信息。

主要信息是在 msg 和 args 中传递的，它们使用 msg % args 组合到一起以创建记录的 message 字段。

参数

name – 用于记录由此 LogRecord 所表示 的事件的日志记录器名称。 请注意此名称将始终为该值，即使它可以是由附加到不同（祖先）日志记录器的处理程序所发出的。

level – 以数字表示的日志记录事件层级（如 DEBUG, INFO 等）。 请注意这会转换为 LogRecord 的 两个 属性: levelno 为数字值而 levelname 为对应的层级名称。

pathname – 进行日志记录调用的文件的完整路径名。

lineno – 记录调用所在源文件中的行号。

msg – 事件描述消息，可能为带有可变数据占位符的格式字符串。

args – 要合并到 msg 参数以获得事件描述的可变数据。

exc_info – 包含当前异常信息的异常元组，或者如果没有可用异常信息则为 None。

func – 发起调用日志记录调用的函数或方法名称。

sinfo – 一个文本字符串，表示当前线程中从堆栈底部直到日志记录调用的堆栈信息。

getMessage()

在将 LogRecord 实例与任何用户提供的参数合并之后，返回此实例的消息。 如果用户提供给日志记录调用的消息参数不是字符串，则会在其上调用 str() 以将它转换为字符串。 此方法允许将用户定义的类用作消息，类的 __str__ 方法可以返回要使用的实际格式字符串。

在 3.2 版更改: The creation of a LogRecord has been made more configurable by providing a factory which is used to create the record. The factory can be set using getLogRecordFactory() and setLogRecordFactory() (see this for the factory’s signature).

This functionality can be used to inject your own values into a LogRecord at creation time. You can use the following pattern:

old_factory = logging.getLogRecordFactory()

def record_factory(*args, **kwargs):
    record = old_factory(*args, **kwargs)
    record.custom_attribute = 0xdecafbad
    return record

logging.setLogRecordFactory(record_factory)

通过此模式，多个工厂方法可以被链接起来，并且只要它们不重载彼此的属性或是在无意中覆盖了上面列出的标准属性，就不会发生意外。

16.6.7. LogRecord 属性

LogRecord 具有许多属性，它们大多数来自于传递给构造器的形参。 （请注意 LogRecord 构造器形参与 LogRecord 属性的名称并不总是完全彼此对应的。） 这些属性可被用于将来自记录的数据合并到格式字符串中。 下面的表格（按字母顺序）列出了属性名称、它们的含义以及相应的 %-style 格式字符串内占位符。

如果是使用 {}-格式化 (str.format())，你可以将 {attrname} 用作格式字符串内的占位符。 如果是使用 $-格式化 (string.Template)，则会使用 ${attrname} 的形式。 当然在这两种情况下，都应当将 attrname 替换为你想要使用的实际属性名称。

在 {}-格式化的情况下，你可以在属性名称之后放置指定的格式化旗标，并用冒号来分隔两者。 例如，占位符 {msecs:03d} 会将毫秒值 4 格式化为 004。 请参看 str.format() 文档了解你所能使用的选项的完整细节。

属性名称

格式

描述

args

不需要格式化。

合并到 msg 以产生 message 的包含参数的元组，或是其中的值将被用于合并的字典（当只有一个参数且其类型为字典时）。

asctime

%(asctime)s

表示 LogRecord 何时被创建的供人查看时间值。 默认形式为 ‘2003-07-08 16:49:45,896’ (逗号之后的数字为时间的毫秒部分)。

created

%(created)f

LogRecord 被创建的时间（即 time.time() 的返回值）。

exc_info

不需要格式化。

异常元组 (例如 sys.exc_info) 或者如未发生异常则为 None。

filename

%(filename)s

pathname 的文件名部分。

funcName

%(funcName)s

函数名包括调用日志记录.

levelname

%(levelname)s

消息文本记录级别 ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL').

levelno

%(levelno)s

消息数字记录级别 (DEBUG, INFO, WARNING, ERROR, CRITICAL).

lineno

%(lineno)d

发出日志记录调用所在的源行号（如果可用）。

message

%(message)s

记入日志的消息，即 msg % args 的结果。 这是在发起调用 Formatter.format() 时设置的。

module 模块

%(module)s

模块 (filename 的名称部分)。

msecs

%(msecs)d

LogRecord 被创建的时间的毫秒部分。

msg

不需要格式化。

在原始日志记录调用中传入的格式字符串。 与 args 合并以产生 message，或是一个任意对象 (参见 使用任意对象作为消息)。

名称

%(name)s

用于记录调用的日志记录器名称。

pathname

%(pathname)s

发出日志记录调用的源文件的完整路径名（如果可用）。

process

%(process)d

进程ID（如果可用）

processName

%(processName)s

进程名（如果可用）

relativeCreated

%(relativeCreated)d

以毫秒数表示的 LogRecord 被创建的时间，即相对于 logging 模块被加载时间的差值。

stack_info

不需要格式化。

当前线程中从堆栈底部起向上直到包括日志记录调用并导致创建此记录的堆栈帧的堆栈帧信息（如果可用）。

thread

%(thread)d

线程ID（如果可用）

threadName

%(threadName)s

线程名（如果可用）

在 3.1 版更改: 添加了 processName

16.6.8. LoggerAdapter 对象

LoggerAdapter 实例会被用来方便地将上下文信息传入日志记录调用。 要获取用法示例，请参阅 添加上下文信息到你的日志记录输出 部分。

class logging.LoggerAdapter(logger, extra)

返回一个 LoggerAdapter 的实例，该实例的初始化带有一个下层的 Logger 实例和一个字典类对象。

process(msg, kwargs)

修改传递给日志记录调用的消息和/或关键字参数以便插入上下文信息。 此实现接受以 extra 形式传给构造器的对象并使用 ‘extra’ 键名将其加入 kwargs。 返回值为一个 (msg, kwargs) 元组，其中传入了（可能经过修改的）参数版本。

在上述方法之外，LoggerAdapter 还支持 Logger 的下列方法: debug(), info(), warning(), error(), exception(), critical(), log(), isEnabledFor(), getEffectiveLevel(), setLevel() 以及 hasHandlers()。 这些方法具有与它们在 Logger 中的对应方法相同的签名，因此你可以互换使用这两种类型的实例。

在 3.2 版更改: isEnabledFor(), getEffectiveLevel(), setLevel() 和 hasHandlers() 方法已被添加到 LoggerAdapter。 这些方法会委托给下层的日志记录器。

16.6.9. 线程安全

logging 模块的目标是使客户端不必执行任何特殊操作即可确保线程安全。 它通过使用线程锁来达成这个目标；用一个锁来序列化对模块共享数据的访问，并且每个处理程序也会创建一个锁来序列化对其下层 I/O 的访问。

如果你要使用 signal 模块来实现异步信号处理程序，则可能无法在这些处理程序中使用 logging。 这是因为 threading 模块中的锁实现并非总是可重入的，所以无法从此类信号处理程序发起调用。

16.6.10. 模块级别函数

在上述的类之外，还有一些模块层级的函数。

logging.getLogger(name=None)

返回具有指定 name 的日志记录器，或者当 name 为 None 时返回层级结构中作为根日志记录器的日志记录器。 如果指定了 name，它通常是以点号分隔的带层级结构的名称如 ‘a’, ‘a.b’ 或 ‘a.b.c.d’。 这些名称的选择完全取决于使用 logging 的开发者。

附带给定 name 的所有对此函数的调用都将返回相同的日志记录器实例。 这意味着日志记录器实例不需要在应用的不同部分间传递。

logging.getLoggerClass()

返回标准的 Logger 类，或是最近传给 setLoggerClass() 的类。 此函数可以从一个新的类定义中调用，以确保安装自定义的 Logger 类不会撤销其他代码已经应用的自定义操作。 例如:

class MyLogger(logging.getLoggerClass()):
    # ... override behaviour here

logging.getLogRecordFactory()

返回一个被用来创建 LogRecord 的可调用对象。

3.2 新版功能: 此函数与 setLogRecordFactory() 一起提供，以便允许开发者对如何构造表示日志记录事件的 LogRecord 有更好的控制。

请参阅 setLogRecordFactory() 了解有关如何调用该工厂方法的更多信息。

logging.debug(msg, *args, **kwargs)

在根日志记录器上记录一条 DEBUG 级别的消息。 msg 是消息格式字符串，而 args 是要使用字符串格式化运算符合并到 msg 的参数。 （请注意这意味着你可以在格式字符串中使用关键字以及单个字典参数。）

在 kwargs 中有三个关键字参数会被检查: exc_info 参数如果不为假值则会将异常信息添加到日志记录消息。 如果提供了异常元组（为 sys.exc_info() 的返回值格式）或异常实例则它会被使用；在其他情况下，会调用 sys.exc_info() 以获取异常信息。

第二个可选关键字参数是 stack_info，默认为 False。如果为 True，则将堆栈信息添加到日志消息中，包括实际的日志调用。请注意，这与通过指定 exc_info 显示的堆栈信息不同：前者是从堆栈底部到当前线程中的日志记录调用的堆栈帧，而后者是在搜索异常处理程序时，跟踪异常而打开的堆栈帧的信息。

您可以独立于 exc_info 来指定 stack_info，例如，即使在未引发任何异常的情况下，也可以显示如何到达代码中的特定点。堆栈帧在标题行之后打印：

Stack (most recent call last):

这模仿了显示异常帧时所使用的 Traceback (most recent call last): 。

第三个可选关键字参数是 extra，它可被用来传递一个字典，该字典会被用来填充为日志记录事件创建并附带用户自定义属性的 LogRecord 的 __dict__。 之后将可按你的需要使用这些自定义属性。 例如，可以将它们合并到已记录的消息中。 举例来说:

FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
logging.basicConfig(format=FORMAT)
d = {'clientip': '192.168.0.1', 'user': 'fbloggs'}
logging.warning('Protocol problem: %s', 'connection reset', extra=d)

应当会打印出这样的内容:

2006-02-08 22:20:02,165 192.168.0.1 fbloggs  Protocol problem: connection reset

The keys in the dictionary passed in extra should not clash with the keys used by the logging system. (See the Formatter documentation for more information on which keys are used by the logging system.)

如果你选择在已记录的消息中使用这些属性，则需要格外小心。 例如在上面的示例中，Formatter 已设置了格式字符串，其在 LogRecord 的属性字典中应有 ‘clientip’ 和 ‘user’。 如果缺少这些属性，消息将不被记录，因为会引发字符串格式化异常，你始终需要传入带有这些键的 extra 字典。

尽管这可能很烦人，但此功能旨在用于特殊情况，例如在多个上下文中执行相同代码的多线程服务器，并且出现的有趣条件取决于此上下文（例如在上面的示例中就是远程客户端IP地址和已验证用户名）。在这种情况下，很可能将专门的 Formatter 与特定的 Handler 一起使用。

3.2 新版功能: 增加了 stack_info 参数。

logging.info(msg, *args, **kwargs)

在根日志记录器上记录一条 INFO 级别的消息。 参数解释同 debug()。

logging.warning(msg, *args, **kwargs)

在根日志记录器上记录一条 WARNING 级别的消息。 参数解释同 debug()。

注解

有一个已过时方法 warn 其功能与 warning 一致。 由于 warn 已被弃用，请不要使用它 —— 而要改用 warning。

logging.error(msg, *args, **kwargs)

在根日志记录器上记录一条 ERROR 级别的消息。 参数解释同 debug().

logging.critical(msg, *args, **kwargs)

在根日志记录器上记录一条 CRITICAL 级别的消息。 参数解释同 debug()。

logging.exception(msg, *args, **kwargs)

在根日志记录器上记录一条 ERROR 级别的消息。 参数解释同 debug()。 异常信息将被添加到日志消息中。 此函数应当仅从异常处理程序中调用。

logging.log(level, msg, *args, **kwargs)

在根日志记录器上记录一条 level 级别的消息。 其他参数解释同 debug()。

注解

上述模块层级的便捷函数均委托给根日志记录器，它们会调用 basicConfig() 以确保至少有一个处理程序可用。 因此，它们 不应 在线程中使用，在 Python 2.7.1 和 3.2 之前的版本中，除非线程启动 之前 已向根日志记录器添加了至少一个处理程序方可使用。 在较早的 Python 版本中，由于 basicConfig() 中存在线程安全性的不足，这（在少数情况下）可能导致处理程序被多次加入根日志记录器，这会进一步导致同一事件出现多条消息。

logging.disable(lvl=CRITICAL)

Provides an overriding level lvl for all loggers which takes precedence over the logger’s own level. When the need arises to temporarily throttle logging output down across the whole application, this function can be useful. Its effect is to disable all logging calls of severity lvl and below, so that if you call it with a value of INFO, then all INFO and DEBUG events would be discarded, whereas those of severity WARNING and above would be processed according to the logger’s effective level. If logging.disable(logging.NOTSET) is called, it effectively removes this overriding level, so that logging output again depends on the effective levels of individual loggers.

Note that if you have defined any custom logging level higher than CRITICAL (this is not recommended), you won’t be able to rely on the default value for the lvl parameter, but will have to explicitly supply a suitable value.

在 3.7 版更改: The lvl parameter was defaulted to level CRITICAL. See Issue #28524 for more information about this change.

logging.addLevelName(lvl, levelName)

Associates level lvl with text levelName in an internal dictionary, which is used to map numeric levels to a textual representation, for example when a Formatter formats a message. This function can also be used to define your own levels. The only constraints are that all levels used must be registered using this function, levels should be positive integers and they should increase in increasing order of severity.

注解

如果你考虑要定义你自己的级别，请参阅 自定义级别 部分。

logging.getLevelName(lvl)

Returns the textual representation of logging level lvl. If the level is one of the predefined levels CRITICAL, ERROR, WARNING, INFO or DEBUG then you get the corresponding string. If you have associated levels with names using addLevelName() then the name you have associated with lvl is returned. If a numeric value corresponding to one of the defined levels is passed in, the corresponding string representation is returned. Otherwise, the string ‘Level %s’ % lvl is returned.

注解

级别在内部为整数（它们需要在日志记录逻辑中相互比较）。 此函数被用于在整数级别与通过 %(levelname)s 格式描述符方式格式化的日志输出中显示的级别名称之间进行转换 (参见 LogRecord 属性)。

在 3.4 版更改: 在早于 3.4 的 Python 版本中，此函数也可传入一个文本级别名称，并将返回对应的级别数字值。 此未记入文档的行为被视为是一个错误，并在 Python 3.4 中被移除，但又在 3.4.2 中被恢复以保持向下兼容性。

logging.makeLogRecord(attrdict)

创建并返回一个新的 LogRecord 实例，实例属性由 attrdict 定义。 此函数适用于接受一个封存的 LogRecord 属性字典，通过套接字传输，并在接收端将其重建为一个 LogRecord 实例。

logging.basicConfig(**kwargs)

通过使用默认的 Formatter 创建一个 StreamHandler 并将其加入根日志记录器来为日志记录系统执行基本配置。 如果没有为根日志记录器定义处理程序则 debug(), info(), warning(), error() 和 critical() 等函数将自动调用 basicConfig()。

This function does nothing if the root logger already has handlers configured for it.

注解

此函数应当在其他线程启动之前从主线程被调用。 在 2.7.1 和 3.2 之前的 Python 版本中，如果此函数从多个线程被调用，一个处理程序（在极少的情况下）有可能被多次加入根日志记录器，导致非预期的结果例如日志中的消息出现重复。

支持以下关键字参数。

格式

描述

filename

使用指定的文件名而不是 StreamHandler 创建 FileHandler。

filemode

如果指定了 filename，则用此 模式 打开该文件。 默认模式为 'a'。

format

处理器使用的指定格式字符串。

datefmt

使用指定的日期/时间格式，与 time.strftime() 所接受的格式相同。

style

如果指定了 format，将为格式字符串使用此风格。 '%', '{' 或 '$' 分别对应于 printf 风格, str.format() 或 string.Template。 默认为 '%'。

level

设置根记录器级别去指定 level.

stream

使用指定的流初始化 StreamHandler。 请注意此参数与 filename 是不兼容的 - 如果两者同时存在，则会引发 ValueError。

handlers

如果指定，这应为一个包含要加入根日志记录器的已创建处理程序的可迭代对象。 任何尚未设置格式描述符的处理程序将被设置为在此函数中创建的默认格式描述符。 请注意此参数与 filename 或 stream 不兼容 —— 如果两者同时存在，则会引发 ValueError。

在 3.2 版更改: 增加了 style 参数。

在 3.3 版更改: 增加了 handlers 参数。 增加了额外的检查来捕获指定不兼容参数的情况 (例如同时指定 handlers 与 stream 或 filename，或者同时指定 stream 与 filename)。

logging.shutdown()

通过刷新和关闭所有处理程序来通知日志记录系统执行有序停止。 此函数应当在应用退出时被调用并且在此调用之后不应再使用日志记录系统。

logging.setLoggerClass(klass)

Tells the logging system to use the class klass when instantiating a logger. The class should define __init__() such that only a name argument is required, and the __init__() should call Logger.__init__(). This function is typically called before any loggers are instantiated by applications which need to use custom logger behavior.

logging.setLogRecordFactory(factory)

设置一个用来创建 LogRecord 的可调用对象。

参数

factory – 用来实例化日志记录的工厂可调用对象。

3.2 新版功能: 此函数与 getLogRecordFactory() 一起提供，以便允许开发者对如何构造表示日志记录事件的 LogRecord 有更好的控制。

可调用对象 factory 具有如下签名:

factory(name, level, fn, lno, msg, args, exc_info, func=None, sinfo=None, **kwargs)

名称

日志记录器名称

level

日志记录级别（数字）。

fn

进行日志记录调用的文件的完整路径名。

lno

记录调用所在文件中的行号。

msg

日志消息。

args

日志记录消息的参数。

exc_info

异常元组，或 None 。

func

调用日志记录调用的函数或方法的名称。

sinfo

与 traceback.print_stack() 所提供的类似的栈回溯信息，显示调用的层级结构。

kwargs

其他关键字参数。

16.6.11. 模块级属性

logging.lastResort

通过此属性提供的“最后处理者”。 这是一个以 WARNING 级别写入到 sys.stderr 的 StreamHandler，用于在没有任何日志记录配置的情况下处理日志记录事件。 最终结果就是将消息打印到 sys.stderr，这会替代先前形式为 “no handlers could be found for logger XYZ” 的错误消息。 如果出于某种原因你需要先前的行为，可将 lastResort 设为 None。

3.2 新版功能.

16.6.12. 与警告模块集成

captureWarnings() 函数可用来将 logging 和 warnings 模块集成。

logging.captureWarnings(capture)

此函数用于打开和关闭日志系统对警告的捕获。

如果 capture 是 True，则 warnings 模块发出的警告将重定向到日志记录系统。具体来说，将使用 warnings.formatwarning() 格式化警告信息，并将结果字符串使用 WARNING 等级记录到名为 'py.warnings' 的记录器中。

如果 capture 是 False，则将停止将警告重定向到日志记录系统，并且将警告重定向到其原始目标（即在 captureWarnings(True) 调用之前的有效目标）。

参见

模块 logging.config

日志记录模块的配置 API 。

模块 logging.handlers

日志记录模块附带的有用处理程序。

PEP 282 [https://www.python.org/dev/peps/pep-0282] - Logging 系统

该提案描述了Python标准库中包含的这个特性。

Original Python logging package [https://www.red-dove.com/python_logging.html]

这是该 logging 包的原始来源。该站点提供的软件包版本适用于 Python 1.5.2、2.1.x 和 2.2.x，它们不被 logging 包含在标准库中。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

16.7. logging.config — 日志记录配置

源代码： Lib/logging/config.py [https://github.com/python/cpython/tree/3.6/Lib/logging/config.py]

Important

此页面仅包含参考信息。有关教程，请参阅

基础教程

进阶教程

Logging Cookbook

这一节描述了用于配置 logging 模块的 API。

16.7.1. 配置函数

下列函数可配置 logging 模块。 它们位于 logging.config 模块中。 它们的使用是可选的 — 要配置 logging 模块你可以使用这些函数，也可以通过调用主 API (在 logging 本身定义) 并定义在 logging 或 logging.handlers 中声明的处理程序。

logging.config.dictConfig(config)

从一个字典获取日志记录配置。 字典的内容描述见下文的 配置字典架构。

如果在配置期间遇到错误，此函数将引发 ValueError, TypeError, AttributeError 或 ImportError 并附带适当的描述性消息。 下面是将会引发错误的（可能不完整的）条件列表:

level 不是字符串或者不是对应于实际日志记录级别的字符串。

propagate 值不是布尔类型。

id 没有对应的目标。

在增量调用期间发现不存在的处理程序 id。

无效的日志记录器名称。

无法解析为内部或外部对象。

解析由 DictConfigurator 类执行，该类的构造器可传入用于配置的字典，并且具有 configure() 方法。 logging.config 模块具有可调用属性 dictConfigClass，其初始值设为 DictConfigurator。 你可以使用你自己的适当实现来替换 dictConfigClass 的值。

dictConfig() 会调用 dictConfigClass 并传入指定的字典，然后在所返回的对象上调用 configure() 方法以使配置生效:

def dictConfig(config):
    dictConfigClass(config).configure()

例如，DictConfigurator 的子类可以在它自己的 __init__() 中调用 DictConfigurator.__init__()，然后设置可以在后续 configure() 调用中使用的自定义前缀。 dictConfigClass 将被绑定到这个新的子类，然后就可以与在默认的未定制状态下完全相同的方式调用 dictConfig()。

3.2 新版功能.

logging.config.fileConfig(fname, defaults=None, disable_existing_loggers=True)

从一个 configparser 格式文件中读取日志记录配置。 文件格式应当与 配置文件格式 中的描述一致。 此函数可在应用程序中被多次调用，以允许最终用户在多个预设配置中进行选择（如果开发者提供了展示选项并加载选定配置的机制）。

参数

fname – 一个文件名，或一个文件类对象，或是一个派生自 RawConfigParser 的实例。 如果传入了一个派生自 RawConfigParser 的实例，它会被原样使用。 否则，将会实例化一个 Configparser，并且它会从作为 fname 传入的对象中读取配置。 如果存在 readline() 方法，则它会被当作一个文件类对象并使用 read_file() 来读取；在其它情况下，它会被当作一个文件名并传递给 read()。

defaults – 要传递给 ConfigParser 的默认值可在此参数中指定。

disable_existing_loggers – If specified as False, loggers which exist when this call is made are left enabled. The default is True because this enables old behaviour in a backward-compatible way. This behaviour is to disable any existing loggers unless they or their ancestors are explicitly named in the logging configuration.

在 3.4 版更改: 现在接受 RawConfigParser 子类的实例作为 fname 的值。 这有助于:

使用一个配置文件，其中日志记录配置只是全部应用程序配置的一部分。

使用从一个文件读取的配置，它随后会在被传给 fileConfig 之前由使用配置的应用程序来修改（例如基于命令行参数或运行时环境的其他部分）。

logging.config.listen(port=DEFAULT_LOGGING_CONFIG_PORT, verify=None)

在指定的端口上启动套接字服务器，并监听新的配置。 如果未指定端口，则会使用模块默认的 DEFAULT_LOGGING_CONFIG_PORT。 日志记录配置将作为适合由 dictConfig() 或 fileConfig() 进行处理的文件来发送。 返回一个 Thread 实例，你可以在该实例上调用 start() 来启动服务器，对该服务器你可以在适当的时候执行 join()。 要停止该服务器，请调用 stopListening()。

如果指定 verify 参数，则它应当是一个可调用对象，该对象应当验证通过套接字接收的字节数据是否有效且应被处理。 这可以通过对通过套接字发送的内容进行加密和/或签名来完成，这样 verify 可调用对象就能执行签名验证和/或解密。 verify 可调用对象的调用会附带一个参数 —— 通过套接字接收的字节数据 —— 并应当返回要处理的字节数据，或者返回 None 来指明这些字节数据应当被丢弃。 返回的字节数据可以与传入的字节数据相同（例如在只执行验证的时候），或者也可以完全不同（例如在可能执行了解密的时候）。

要将配置发送到套接字，请读取配置文件并将其作为字节序列发送到套接字，字节序列要以使用 struct.pack('>L', n) 打包为二进制格式的四字节长度的字符串打头。

注解

因为配置的各部分是通过 eval() 传递的，使用此函数可能让用户面临安全风险。 虽然此函数仅绑定到 localhost 上的套接字，因此并不接受来自远端机器的连接，但在某些场景中不受信任的代码可以在调用 listen() 的进程的账户下运行。 具体来说，如果如果调用 listen() 的进程在用户无法彼此信任的多用户机器上运行，则恶意用户就能简单地通过连接到受害者的 listen() 套接字并发送运行攻击者想在受害者的进程上执行的任何代码的配置的方式，安排运行几乎任意的代码。 如果是使用默认端口这会特别容易做到，即便使用了不同端口也不难做到。 要避免发生这种情况的风险，请在 listen() 中使用 verify 参数来防止未经认可的配置被应用。

在 3.4 版更改: 添加了 verify 参数。

注解

如果你希望将配置发送给未禁用现有日志记录器的监听器，你将需要使用 JSON 格式的配置，该格式将使用 dictConfig() 进行配置。 此方法允许你在你发送的配置中将 disable_existing_loggers 指定为 False。

logging.config.stopListening()

停止通过对 listen() 的调用所创建的监听服务器。 此函数的调用通常会先于在 listen() 的返回值上调用 join()。

16.7.2. 配置字典架构

描述日志记录配置需要列出要创建的不同对象及它们之间的连接；例如，你可以创建一个名为 ‘console’ 的处理程序，然后名为 ‘startup’ 的日志记录器将可以把它的消息发送给 ‘console’ 处理程序。 这些对象并不仅限于 logging 模块所提供的对象，因为你还可以编写你自己的格式化或处理程序类。 这些类的形参可能还需要包括 sys.stderr 这样的外部对象。 描述这些对象和连接的语法会在下面的 对象连接 中定义。

16.7.2.1. 字典架构细节

传给 dictConfig() 的字典必须包含以下的键:

version - 应设为代表架构版本的整数值。 目前唯一有效的值是 1，使用此键可允许架构在继续演化的同时保持向下兼容性。

所有其他键都是可选项，但如存在它们将根据下面的描述来解读。 在下面提到 ‘configuring dict’ 的所有情况下，都将检查它的特殊键 '()' 以确定是否需要自定义实例化。 如果需要，则会使用下面 用户定义对象 所描述的机制来创建一个实例；否则，会使用上下文来确定要实例化的对象。

formatters - 对应的值将是一个字典，其中每个键是一个格式器 ID 而每个值则是一个描述如何配置相应 Formatter 实例的字典。

将在配置字典中搜索键 format 和 datefmt (默认值均为 None) 并且这些键会被用于构造 Formatter 实例。

filters - 对应的值将是一个字典，其中每个键是一个过滤器 ID 而每个值则是一个描述如何配置相应 Filter 实例的字典。

将在配置字典中搜索键 name (默认值为空字符串) 并且该键会被用于构造 logging.Filter 实例。

handlers - 对应的值将是一个字典，其中每个键是一个处理程序 ID 而每个值则是一个描述如何配置相应 Handler 实例的字典。

将在配置字典中搜索下列键:

class (强制)。 这是处理程序类的完整限定名称。

level (可选)。 处理程序的级别。

formatter (可选)。 处理程序所对应格式化器的 ID。

filters (可选)。 由处理程序所对应过滤器的 ID 组成的列表。

所有 其他 键会被作为关键字参数传递给处理程序类的构造器。 例如，给定如下代码段:

handlers:
  console:
    class : logging.StreamHandler
    formatter: brief
    level   : INFO
    filters: [allow_foo]
    stream  : ext://sys.stdout
  file:
    class : logging.handlers.RotatingFileHandler
    formatter: precise
    filename: logconfig.log
    maxBytes: 1024
    backupCount: 3

ID 为 console 的处理程序会被实例化为 logging.StreamHandler，并使用 sys.stdout 作为下层流。 ID 为 file 的处理程序会被实例化为 logging.handlers.RotatingFileHandler，并附带关键字参数 filename='logconfig.log', maxBytes=1024, backupCount=3。

loggers - 对应的值将是一个字典，其中每个键是一个日志记录器名称而每个值则是一个描述如何配置相应 Logger 实例的字典。

将在配置字典中搜索下列键:

level (可选)。 日志记录器的级别。

propagate (可选)。 日志记录器的传播设置。

filters (可选)。 由日志记录器对应过滤器的 ID 组成的列表。

handlers (可选)。 由日志记录器对应处理程序的 ID 组成的列表。

指定的日志记录器将根据指定的级别、传播、过滤器和处理程序来配置。

root - 这将成为根日志记录器对应的配置。 配置的处理方式将与所有日志记录器一致，除了 propagate 设置将不可用之外。

incremental - 配置是否要被解读为在现有配置上新增。 该值默认为 False，这意味着指定的配置将以与当前 fileConfig() API 所使用的相同语义来替代现有的配置。

如果指定的值为 True，配置会按照 增量配置 部分所描述的方式来处理。

disable_existing_loggers - whether any existing loggers are to be disabled. This setting mirrors the parameter of the same name in fileConfig(). If absent, this parameter defaults to True. This value is ignored if incremental is True.

16.7.2.2. 增量配置

为增量配置提供完全的灵活性是很困难的。 例如，由于过滤器和格式化器这样的对象是匿名的，一旦完成配置，在增加配置时就不可能引用这些匿名对象。

此外，一旦完成了配置，在运行时任意改变日志记录器、处理程序、过滤器、格式化器的对象图就不是很有必要；日志记录器和处理程序的详细程度只需通过设置级别即可实现控制（对于日志记录器则可设置传播旗标）。 在多线程环境中以安全的方式任意改变对象图也许会导致问题；虽然并非不可能，但这样做的好处不足以抵销其所增加的实现复杂度。

这样，当配置字典的 incremental 键存在且为 True 时，系统将完全忽略任何 formatters 和 filters 条目，并仅会处理 handlers 条目中的 level 设置，以及 loggers 和 root 条目中的 level 和 propagate 设置。

使用配置字典中的值可让配置以封存字典对象的形式通过线路传送给套接字监听器。 这样，长时间运行的应用程序的日志记录的详细程度可随时间改变而无须停止并重新启动应用程序。

16.7.2.3. 对象连接

该架构描述了一组日志记录对象 —— 日志记录器、处理程序、格式化器、过滤器 —— 它们在对象图中彼此连接。 因此，该架构需要能表示对象之间的连接。 例如，在配置完成后，一个特定的日志记录器关联到了一个特定的处理程序。 出于讨论的目的，我们可以说该日志记录器代表两者间连接的源头，而处理程序则代表对应的目标。 当然在已配置对象中这是由包含对处理程序的引用的日志记录器来代表的。 在配置字典中，这是通过给每个目标对象一个 ID 来无歧义地标识它，然后在源头对象中使用该 ID 来实现的。

因此，举例来说，考虑以下 YAML 代码段:

formatters:
  brief:
    # configuration for formatter with id 'brief' goes here
  precise:
    # configuration for formatter with id 'precise' goes here
handlers:
  h1: #This is an id
   # configuration of handler with id 'h1' goes here
   formatter: brief
  h2: #This is another id
   # configuration of handler with id 'h2' goes here
   formatter: precise
loggers:
  foo.bar.baz:
    # other configuration for logger 'foo.bar.baz'
    handlers: [h1, h2]

（注：这里使用 YAML 是因为它的可读性比表示字典的等价 Python 源码形式更好。）

日志记录器 ID 就是日志记录器的名称，它会在程序中被用来获取对日志记录器的引用，例如 foo.bar.baz。 格式化器和过滤器的 ID 可以是任意字符串值 (例如上面的 brief, precise) 并且它们是瞬态的，因为它们仅对处理配置字典有意义并会被用来确定对象之间的连接，而当配置调用完成时不会在任何地方保留。

上面的代码片段指明名为 foo.bar.baz 的日志记录器应当关联到两个处理程序，它们的 ID 是 h1 和 h2。 h1 的格式化器的 ID 是 brief，而 h2 的格式化器的 ID 是 precise。

16.7.2.4. 用户定义对象

此架构支持用户定义对象作为处理程序、过滤器和格式化器。 （日志记录器的不同实例不需要具有不同类型，因此这个配置架构并不支持用户定义日志记录器类。）

要配置的对象是由字典描述的，其中包含它们的配置详情。 在某些地方，日志记录系统将能够从上下文中推断出如何实例化一个对象，但是当要实例化一个用户自定义对象时，系统将不知道要如何做。 为了提供用户自定义对象实例化的完全灵活性，用户需要提供一个‘工厂’函数 —— 即在调用时传入配置字典并返回实例化对象的可调用对象。 这是用一个通过特殊键 '()' 来访问的工厂函数的绝对导入路径来标示的。 下面是一个实际的例子:

formatters:
  brief:
    format: '%(message)s'
  default:
    format: '%(asctime)s %(levelname)-8s %(name)-15s %(message)s'
    datefmt: '%Y-%m-%d %H:%M:%S'
  custom:
      (): my.package.customFormatterFactory
      bar: baz
      spam: 99.9
      answer: 42

上面的 YAML 代码片段定义了三个格式化器。 第一个的 ID 为 brief，是带有特殊格式字符串的标准 logging.Formatter 实例。 第二个的 ID 为 default，具有更长的格式同时还显式地定义了时间格式，并将最终实例化一个带有这两个格式字符串的 logging.Formatter。 以 Python 源代码形式显示的 brief 和 default 格式化器具有下列配置子字典:

{
  'format' : '%(message)s'
}

和:

{
  'format' : '%(asctime)s %(levelname)-8s %(name)-15s %(message)s',
  'datefmt' : '%Y-%m-%d %H:%M:%S'
}

并且由于这些字典不包含特殊键 '()'，实例化方式是从上下文中推断出来的：结果会创建标准的 logging.Formatter 实例。 第三个格式器的 ID 为 custom，对应配置子字典为:

{
  '()' : 'my.package.customFormatterFactory',
  'bar' : 'baz',
  'spam' : 99.9,
  'answer' : 42
}

并且它包含特殊键 '()'，这意味着需要用户自定义实例化方式。 在此情况下，将使用指定的工厂可调用对象。 如果它本身就是一个可调用对象则将被直接使用 —— 否则如果你指定了一个字符串（如这个例子所示）则将使用正常的导入机制来定位实例的可调用对象。 调用该可调用对象将传入配置子字典中 剩余的 条目作为关键字参数。 在上面的例子中，调用将预期返回 ID 为 custom 的格式化器:

my.package.customFormatterFactory(bar='baz', spam=99.9, answer=42)

将 '()' 用作特殊键是因为它不是一个有效的关键字形参名称，这样就不会与调用中使用的关键字参数发生冲突。 '()' 还被用作表明对应值为可调用对象的助记符。

16.7.2.5. 访问外部对象

有时一个配置需要引用配置以外的对象，例如 sys.stderr。 如果配置字典是使用 Python 代码构造的，这会很直观，但是当配置是通过文本文件（例如 JSON, YAML）提供的时候就会引发问题。 在一个文本文件中，没有将 sys.stderr 与字符串字面值 'sys.stderr' 区分开来的标准方式。 为了实现这种区分，配置系统会在字符串值中查找规定的特殊前缀并对其做特殊处理。 例如，如果在配置中将字符串字面值 'ext://sys.stderr' 作为一个值来提供，则 ext:// 将被去除而该值的剩余部分将使用正常导入机制来处理。

此类前缀的处理方式类似于协议处理：存在一种通用机制来查找与正则表达式 ^(?P<prefix>[a-z]+)://(?P<suffix>.*)$ 相匹配的前缀，如果识别出了 prefix，则 suffix 会以与前缀相对应的方式来处理并且处理的结果将替代原字符串值。 如果未识别出前缀，则原字符串将保持不变。

16.7.2.6. 访问内部对象

除了外部对象，有时还需要引用配置中的对象。 这将由配置系统针对它所了解的内容隐式地完成。 例如，在日志记录器或处理程序中表示 level 的字符串值 'DEBUG' 将被自动转换为值 logging.DEBUG，而 handlers, filters 和 formatter 条目将接受一个对象 ID 并解析为适当的目标对象。

但是，对于 logging 模块所不了解的用户自定义对象则需要一种更通用的机制。 例如，考虑 logging.handlers.MemoryHandler，它接受一个 target 参数即其所委托的另一个处理程序。 由于系统已经知道存在该类，因而在配置中，给定的 target 只需为相应目标处理程序的的对象 ID 即可，而系统将根据该 ID 解析出处理程序。 但是，如果用户定义了一个具有 alternate 处理程序的 my.package.MyHandler，则配置程序将不知道 alternate 指向的是一个处理程序。 为了应对这种情况，通用解析系统允许用户指定:

handlers:
  file:
    # configuration of file handler goes here

  custom:
    (): my.package.MyHandler
    alternate: cfg://handlers.file

字符串字面值 'cfg://handlers.file' 将按照与 ext:// 前缀类似的方式被解析为结果字符串，但查找操作是在配置自身而不是在导入命名空间中进行。 该机制允许按点号或按索引来访问，与 str.format 所提供的方式类似。 这样，给定以下代码段:

handlers:
  email:
    class: logging.handlers.SMTPHandler
    mailhost: localhost
    fromaddr: my_app@domain.tld
    toaddrs:
      - support_team@domain.tld
      - dev_team@domain.tld
    subject: Houston, we have a problem.

在该配置中，字符串 'cfg://handlers' 将解析为带有 handlers 键的字典，字符串 'cfg://handlers.email 将解析为 handlers 字典中带有 email 键的字典，依此类推。 字符串 'cfg://handlers.email.toaddrs[1] 将解析为 'dev_team.domain.tld' 而字符串 'cfg://handlers.email.toaddrs[0]' 将解析为值 'support_team@domain.tld'。 subject 值可以使用 'cfg://handlers.email.subject' 或者等价的 'cfg://handlers.email[subject]' 来访问。 后一种形式仅在键包含空格或非字母数字类字符的情况下才需要使用。 如果一个索引仅由十进制数码构成，则将尝试使用相应的整数值来访问，如果有必要则将回退为字符串值。

给定字符串 cfg://handlers.myhandler.mykey.123，这将解析为 config_dict['handlers']['myhandler']['mykey']['123']。 如果字符串被指定为 cfg://handlers.myhandler.mykey[123]，系统将尝试从 config_dict['handlers']['myhandler']['mykey'][123] 中提取值，并在尝试失败时回退为 config_dict['handlers']['myhandler']['mykey']['123']。

16.7.2.7. 导入解析与定制导入器

导入解析默认使用内置的 __import__() 函数来执行导入。 你可能想要将其替换为你自己的导入机制：如果是这样的话，你可以替换 DictConfigurator 或其超类 BaseConfigurator 类的 importer 属性。 但是你必须小心谨慎，因为函数是从类中通过描述器方式来访问的。 如果你使用 Python 可调用对象来执行导入，并且你希望在类层级而不是在实例层级上定义它，则你需要用 staticmethod() 来装饰它。 例如:

from importlib import import_module
from logging.config import BaseConfigurator

BaseConfigurator.importer = staticmethod(import_module)

如果你是在一个配置器的 实例 上设置导入可调用对象则你不需要用 staticmethod() 来装饰。

16.7.3. 配置文件格式

fileConfig() 所能理解的配置文件格式是基于 configparser 功能的。 该文件必须包含 [loggers], [handlers] 和 [formatters] 等小节，它们通过名称来标识文件中定义的每种类型的实体。 对于每个这样的实体，都有单独的小节来标识实体的配置方式。 因此，对于 [loggers] 小节中名为 log01 的日志记录器，相应的配置详情保存在 [logger_log01] 小节中。 类似地，对于 [handlers] 小节中名为 hand01 的处理程序，其配置将保存在名为 [handler_hand01] 的小节中，而对于 [formatters] 小节中名为 form01 的格式化器，其配置将在名为 [formatter_form01] 的小节中指定。 根日志记录器的配置必须在名为 [logger_root] 的小节中指定。

注解

fileConfig() API 比 dictConfig() API 更旧因而没有提供涵盖日志记录特定方面的功能。 例如，你无法配置 Filter 对象，该对象使用 fileConfig() 提供超出简单整数级别的消息过滤功能。 如果你想要在你的日志记录配置中包含 Filter 的实例，你将必须使用 dictConfig()。 请注意未来还将向 dictConfig() 添加对配置功能的强化，因此值得考虑在方便的时候转换到这个新 API。

在文件中这些小节的例子如下所示。

[loggers]
keys=root,log02,log03,log04,log05,log06,log07

[handlers]
keys=hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09

[formatters]
keys=form01,form02,form03,form04,form05,form06,form07,form08,form09

根日志记录器必须指定一个级别和一个处理程序列表。 根日志小节的例子如下所示。

[logger_root]
level=NOTSET
handlers=hand01

level 条目可以为 DEBUG, INFO, WARNING, ERROR, CRITICAL 或 NOTSET 之一。 其中 NOTSET 仅适用于根日志记录器，表示将会记录所有消息。 级别值会在 logging 包命名空间的上下文中通过 eval() 来得出。

handlers 条目是以逗号分隔的处理程序名称列表，它必须出现于 [handlers] 小节并且在配置文件中有相应的小节。

对于根日志记录器以外的日志记录器，还需要某些附加信息。 下面的例子演示了这些信息。

[logger_parser]
level=DEBUG
handlers=hand01
propagate=1
qualname=compiler.parser

level 和 handlers 条目的解释方式与根日志记录器的一致，不同之处在于如果一个非根日志记录器的级别被指定为 NOTSET，则系统会咨询更高层级的日志记录器来确定该日志记录器的有效级别。 propagate 条目设为 1 表示消息必须从此日志记录器传播到更高层级的处理程序，设为 0 表示消息 不会 传播到更高层级的处理程序。 qualname 条目是日志记录器的层级通道名称，也就是应用程序获取日志记录器所用的名称。

指定处理程序配置的小节说明如下。

[handler_hand01]
class=StreamHandler
level=NOTSET
formatter=form01
args=(sys.stdout,)

class 条目指明处理程序的类（由 logging 包命名空间中的 eval() 来确定）。 level 会以与日志记录器相同的方式来解读，NOTSET 会被视为表示‘记录一切消息’。

formatter 条目指明此处理程序的格式化器的键名称。 如为空白，则会使用默认的格式化器 (logging._defaultFormatter)。 如果指定了名称，则它必须出现于 [formatters] 小节并且在配置文件中有相应的小节。

The args entry, when eval()uated in the context of the logging package’s namespace, is the list of arguments to the constructor for the handler class. Refer to the constructors for the relevant handlers, or to the examples below, to see how typical entries are constructed.

[handler_hand02]
class=FileHandler
level=DEBUG
formatter=form02
args=('python.log', 'w')

[handler_hand03]
class=handlers.SocketHandler
level=INFO
formatter=form03
args=('localhost', handlers.DEFAULT_TCP_LOGGING_PORT)

[handler_hand04]
class=handlers.DatagramHandler
level=WARN
formatter=form04
args=('localhost', handlers.DEFAULT_UDP_LOGGING_PORT)

[handler_hand05]
class=handlers.SysLogHandler
level=ERROR
formatter=form05
args=(('localhost', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_USER)

[handler_hand06]
class=handlers.NTEventLogHandler
level=CRITICAL
formatter=form06
args=('Python Application', '', 'Application')

[handler_hand07]
class=handlers.SMTPHandler
level=WARN
formatter=form07
args=('localhost', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject')

[handler_hand08]
class=handlers.MemoryHandler
level=NOTSET
formatter=form08
target=
args=(10, ERROR)

[handler_hand09]
class=handlers.HTTPHandler
level=NOTSET
formatter=form09
args=('localhost:9022', '/log', 'GET')

指定格式化器配置的小节说明如下。

[formatter_form01]
format=F1 %(asctime)s %(levelname)s %(message)s
datefmt=
class=logging.Formatter

format 是整个格式字符串，而 datefmt 条目则是兼容 strftime() 的日期/时间格式字符串。 如果为空，此包将替换任何接近于日期格式字符串 '%Y-%m-%d %H:%M:%S' 的内容。 此格式还指定了毫秒，并使用逗号分隔符将其附加到结果当中。 此格式的时间示例如 2003-01-23 00:29:50,411。

class 条目是可选的。 它指明格式化器类的名称（形式为带点号的模块名加类名。） 此选项适用于实例化 Formatter 的子类。 Formatter 的子类可通过扩展或收缩格式来显示异常回溯信息。

注解

由于如上所述使用了 eval()，因此使用 listen() 通过套接字来发送和接收配置会导致潜在的安全风险。 此风险仅限于相互间没有信任的多个用户在同一台机器上运行代码的情况；请参阅 listen() 了解更多信息。

参见

模块 logging

日志记录模块的 API 参考。

模块 logging.handlers

日志记录模块附带的有用处理程序。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

16.8. logging.handlers — 日志处理

源代码: Lib/logging/handlers.py [https://github.com/python/cpython/tree/3.6/Lib/logging/handlers.py]

Important

此页面仅包含参考信息。有关教程，请参阅

基础教程

进阶教程

Logging Cookbook

这个包提供了以下有用的处理程序。 请注意有三个处理程序类 (StreamHandler, FileHandler 和 NullHandler) 实际上是在 logging 模块本身定义的，但其文档与其他处理程序一同记录在此。

16.8.1. StreamHandler

StreamHandler 类位于核心 logging 包，它可将日志记录输出发送到数据流例如 sys.stdout, sys.stderr 或任何文件类对象（或者更精确地说，任何支持 write() 和 flush() 方法的对象）。

class logging.StreamHandler(stream=None)

返回一个新的 StreamHandler 类。 如果指定了 stream，则实例将用它作为日志记录输出；在其他情况下将使用 sys.stderr。

emit(record)

If a formatter is specified, it is used to format the record. The record is then written to the stream with a terminator. If exception information is present, it is formatted using traceback.print_exception() and appended to the stream.

flush()

通过调用流的 flush() 方法来刷新它。 请注意 close() 方法是继承自 Handler 的所以没有输出，因此有时可能需要显式地调用 flush()。

在 3.2 版更改: The StreamHandler class now has a terminator attribute, default value '\n', which is used as the terminator when writing a formatted record to a stream. If you don’t want this newline termination, you can set the handler instance’s terminator attribute to the empty string. In earlier versions, the terminator was hardcoded as '\n'.

16.8.2. FileHandler

FileHandler 类位于核心 logging 包，它可将日志记录输出到磁盘文件中。 它从 StreamHandler 继承了输出功能。

class logging.FileHandler(filename, mode='a', encoding=None, delay=False)

Returns a new instance of the FileHandler class. The specified file is opened and used as the stream for logging. If mode is not specified, 'a' is used. If encoding is not None, it is used to open the file with that encoding. If delay is true, then file opening is deferred until the first call to emit(). By default, the file grows indefinitely.

在 3.6 版更改: 除了字符串值，也接受 Path 对象作为 filename 参数。

close()

关闭文件。

emit(record)

将记录输出到文件。

16.8.3. NullHandler

3.1 新版功能.

NullHandler 类位于核心 logging 包，它不执行任何格式化或输出。 它实际上是一个供库开发者使用的‘无操作’处理程序。

class logging.NullHandler

返回一个 NullHandler 类的新实例。

emit(record)

此方法不执行任何操作。

handle(record)

此方法不执行任何操作。

createLock()

此方法会对锁返回 None，因为没有下层 I/O 的访问需要被序列化。

请参阅 配置库的日志记录 了解有关如何使用 NullHandler 的更多信息。

16.8.4. WatchedFileHandler

WatchedFileHandler 类位于 logging.handlers 模块，这个 FileHandler 用于监视它所写入日志记录的文件。 如果文件发生变化，它会被关闭并使用文件名重新打开。

发生文件更改可能是由于使用了执行文件轮换的程序例如 newsyslog 和 logrotate。 这个处理程序在 Unix/Linux 下使用，它会监视文件来查看自上次发出数据后是否有更改。 （如果文件的设备或 inode 发生变化就认为已被更改。） 如果文件被更改，则会关闭旧文件流，并再打开文件以获得新文件流。

这个处理程序不适合在 Windows 下使用，因为在 Windows 下打开的日志文件无法被移动或改名 —— 日志记录会使用排他的锁来打开文件 —— 因此这样的处理程序是没有必要的。 此外，ST_INO 在 Windows 下不受支持；stat() 将总是为该值返回零。

class logging.handlers.WatchedFileHandler(filename, mode='a', encoding=None, delay=False)

Returns a new instance of the WatchedFileHandler class. The specified file is opened and used as the stream for logging. If mode is not specified, 'a' is used. If encoding is not None, it is used to open the file with that encoding. If delay is true, then file opening is deferred until the first call to emit(). By default, the file grows indefinitely.

在 3.6 版更改: 除了字符串值，也接受 Path 对象作为 filename 参数。

reopenIfNeeded()

检查文件是否已更改。 如果已更改，则会刷新并关闭现有流然后重新打开文件，这通常是将记录输出到文件的先导操作。

3.6 新版功能.

emit(record)

将记录输出到文件，但如果文件已更改则会先调用 reopenIfNeeded() 来重新打开它。

16.8.5. BaseRotatingHandler

BaseRotatingHandler 类位于 logging.handlers 模块中，它是轮换文件处理程序类 RotatingFileHandler 和 TimedRotatingFileHandler 的基类。 你不需要实例化此类，但它具有你可能需要重载的属性和方法。

class logging.handlers.BaseRotatingHandler(filename, mode, encoding=None, delay=False)

类的形参与 FileHandler 的相同。 其属性有:

namer

如果此属性被设为一个可调用对象，则 rotation_filename() 方法会委托给该可调用对象。 传给该可调用对象的形参与传给 rotation_filename() 的相同。

注解

namer 函数会在轮换期间被多次调用，因此它应当尽可能的简单快速。 它还应当对给定的输入每次都返回相同的输出，否则轮换行为可能无法按预期工作。

3.3 新版功能.

rotator

如果此属性被设为一个可调用对象，则 rotate() 方法会委托给该可调用对象。 传给该可调用对象的形参与传给 rotate() 的相同。

3.3 新版功能.

rotation_filename(default_name)

当轮换时修改日志文件的文件名。

提供该属性以便可以提供自定义文件名。

默认实现会调用处理程序的 ‘namer’ 属性，如果它是可调用对象的话，并传给它默认的名称。 如果该属性不是可调用对象 (默认值为 None)，则将名称原样返回。

参数

default_name – 日志文件的默认名称。

3.3 新版功能.

rotate(source, dest)

当执行轮换时，轮换当前日志。

默认实现会调用处理程序的 ‘rotator’ 属性，如果它是可调用对象的话，并传给它 source 和 dest 参数。 如果该属性不是可调用对象 (默认值为 None)，则将源简单地重命名为目标。

参数

source – 源文件名。 这通常为基本文件名，例如 ‘test.log’。

dest – 目标文件名。 这通常是源被轮换后的名称，例如 ‘test.log.1’。

3.3 新版功能.

该属性存在的理由是让你不必进行子类化 —— 你可以使用与 RotatingFileHandler 和 TimedRotatingFileHandler 的实例相同的可调用对象。 如果 namer 或 rotator 可调用对象引发了异常，将会按照与 emit() 调用期间的任何其他异常相同的方式来处理，例如通过处理程序的 handleError() 方法。

如果你需要对轮换进程执行更多的修改，你可以重载这些方法。

请参阅 Using a rotator and namer to customize log rotation processing 获取具体示例。

16.8.6. RotatingFileHandler

RotatingFileHandler 类位于 logging.handlers 模块，它支持磁盘日志文件的轮换。

class logging.handlers.RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False)

Returns a new instance of the RotatingFileHandler class. The specified file is opened and used as the stream for logging. If mode is not specified, 'a' is used. If encoding is not None, it is used to open the file with that encoding. If delay is true, then file opening is deferred until the first call to emit(). By default, the file grows indefinitely.

你可以使用 maxBytes 和 backupCount 值来允许文件以预定的大小执行 rollover。 当即将超出预定大小时，将关闭旧文件并打开一个新文件用于输出。 只要当前日志文件长度接近 maxBytes 就会发生轮换；但是如果 maxBytes 或 backupCount 两者之一的值为零，就不会发生轮换，因此你通常要设置 backupCount 至少为 1，而 maxBytes 不能为零。 当 backupCount 为非零值时，系统将通过为原文件名添加扩展名 ‘.1’, ‘.2’ 等来保存旧日志文件。 例如，当 backupCount 为 5 而基本文件名为 app.log 时，你将得到 app.log, app.log.1, app.log.2 直至 app.log.5。 当前被写入的文件总是 app.log。 当此文件写满时，它会被关闭并重户名为 app.log.1，而如果文件 app.log.1, app.log.2 等存在，则它们会被分别重命名为 app.log.2, app.log.3 等等。

在 3.6 版更改: 除了字符串值，也接受 Path 对象作为 filename 参数。

doRollover()

执行上文所描述的轮换。

emit(record)

将记录输出到文件，以适应上文所描述的轮换。

16.8.7. TimedRotatingFileHandler

TimedRotatingFileHandler 类位于 logging.handlers 模块，它支持基于特定时间间隔的磁盘日志文件轮换。

class logging.handlers.TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None)

返回一个新的 TimedRotatingFileHandler 类实例。 指定的文件会被打开并用作日志记录的流。 对于轮换操作它还会设置文件名前缀。 轮换的发生是基于 when 和 interval 的积。

你可以使用 when 来指定 interval 的类型。 可能的值列表如下。 请注意它们不是大小写敏感的。

值

间隔类型

如果/如何使用 atTime

'S'

秒

忽略

'M'

分钟

忽略

'H'

小时

忽略

'D'

天

忽略

'W0'-'W6'

工作日(0=星期一)

用于计算初始轮换时间

'midnight'

如果未指定 atTime 则在午夜执行轮换，否则将使用 atTime。

用于计算初始轮换时间

当使用基于星期的轮换时，星期一为 ‘W0’，星期二为 ‘W1’，以此类推直至星期日为 ‘W6’。 在这种情况下，传入的 interval 值不会被使用。

系统将通过为文件名添加扩展名来保存旧日志文件。 扩展名是基于日期和时间的，根据轮换间隔的长短使用 strftime 格式 %Y-%m-%d_%H-%M-%S 或是其中有变动的部分。

当首次计算下次轮换的时间时（即当处理程序被创建时），现有日志文件的上次被修改时间或者当前时间会被用来计算下次轮换的发生时间。

如果 utc 参数为真值，将使用 UTC 时间；否则会使用本地时间。

如果 backupCount 不为零，则最多将保留 backupCount 个文件，而如果当轮换发生时创建了更多的文件，则最旧的文件会被删除。 删除逻辑使用间隔时间来确定要删除的文件，因此改变间隔时间可能导致旧文件被继续保留。

如果 delay 为真值，则会将文件打开延迟到首次调用 emit() 的时候。

如果 atTime 不为 None，则它必须是一个 datetime.time 的实例，该实例指定轮换在一天内的发生时间，用于轮换被设为“在午夜”或“在每星期的某一天”之类的情况。 请注意在这些情况下，atTime 值实际上会被用于计算 初始 轮换，而后续轮换将会通过正常的间隔时间计算来得出。

注解

初始轮换时间的计算是在处理程序被初始化时执行的。 后续轮换时间的计算则仅在轮换发生时执行，而只有当提交输出时轮换才会发生。 如果不记住这一点，你就可能会感到困惑。 例如，如果设置时间间隔为“每分钟”，那并不意味着你总会看到（文件名中）带有间隔一分钟时间的日志文件；如果在应用程序执行期间，日志记录输出的生成频率高于每分钟一次，那么 你可以预期看到间隔一分钟时间的日志文件。 另一方面，如果（假设）日志记录消息每五分钟才输出一次，那么文件时间将会存在对应于没有输出（因而没有轮换）的缺失。

在 3.4 版更改: 添加了 atTime 形参。

在 3.6 版更改: 除了字符串值，也接受 Path 对象作为 filename 参数。

doRollover()

执行上文所描述的轮换。

emit(record)

将记录输出到文件，以适应上文所描述的轮换。

16.8.8. SocketHandler

SocketHandler 类位于 logging.handlers 模块，它会将日志记录输出发送到网络套接字。 基类所使用的是 TCP 套接字。

class logging.handlers.SocketHandler(host, port)

返回一个 SocketHandler 类的新实例，该实例旨在与使用 host 与 port 给定地址的远程主机进行通信。

在 3.4 版更改: 如果 port 指定为 None，会使用 host 中的值来创建一个 Unix 域套接字 —— 在其他情况下，则会创建一个 TCP 套接字。

close()

关闭套接字。

emit()

对记录的属性字典执行封存并以二进制格式将其写入套接字。 如果套接字存在错误，则静默地丢弃数据包。 如果连接在此之前丢失，则重新建立连接。 要在接收端将记录解封并输出到 LogRecord，请使用 makeLogRecord() 函数。

handleError()

处理在 emit() 期间发生的错误。 最可能的原因是连接丢失。 关闭套接字以便我们能在下次事件时重新尝试。

makeSocket()

这是一个工厂方法，它允许子类定义它们想要的套接字的准确类型。 默认实现会创建一个 TCP 套接字 (socket.SOCK_STREAM)。

makePickle(record)

Pickles the record’s attribute dictionary in binary format with a length prefix, and returns it ready for transmission across the socket.

请注意封存操作不是绝对安全的。 如果你关心安全问题，你可能会想要重载此方法以实现更安全的机制。 例如，你可以使用 HMAC 对封存对象进行签名然后在接收端验证它们，或者你也可以在接收端禁用全局对象的解封操作。

send(packet)

Send a pickled string packet to the socket. This function allows for partial sends which can happen when the network is busy.

createSocket()

尝试创建一个套接字；失败时将使用指数化回退算法处理。 在失败初次发生时，处理程序将丢弃它正尝试发送的消息。 当后续消息交由同一实例处理时，它将不会尝试连接直到经过一段时间以后。 默认形参设置为初始延迟一秒，如果在延迟之后连接仍然无法建立，处理程序将每次把延迟翻倍直至达到 30 秒的最大值。

此行为由下列处理程序属性控制:

retryStart (初始延迟，默认为 1.0 秒)。

retryFactor (倍数，默认为 2.0)。

retryMax (最大延迟，默认为 30.0 秒)。

这意味着如果远程监听器在处理程序被使用 之后 启动，你可能会丢失消息（因为处理程序在延迟结束之前甚至不会尝试连接，而在延迟期间静默地丢弃消息）。

16.8.9. DatagramHandler

DatagramHandler 类位于 logging.handlers 模块，它继承自 SocketHandler，支持通过 UDP 套接字发送日志记录消息。

class logging.handlers.DatagramHandler(host, port)

返回一个 DatagramHandler 类的新实例，该实例旨在与使用 host 与 port 给定地址的远程主机进行通信。

在 3.4 版更改: 如果 port 指定为 None，会使用 host 中的值来创建一个 Unix 域套接字 —— 在其他情况下，则会创建一个 UDP 套接字。

emit()

对记录的属性字典执行封存并以二进制格式将其写入套接字。 如果套接字存在错误，则静默地丢弃数据包。 要在接收端将记录解封并输出到 LogRecord，请使用 makeLogRecord() 函数。

makeSocket()

SocketHandler 的工厂方法会在此被重载以创建一个 UDP 套接字 (socket.SOCK_DGRAM)。

send(s)

Send a pickled string to a socket.

16.8.10. SysLogHandler

SysLogHandler 类位于 logging.handlers 模块，它支持将日志记录消息发送到远程或本地 Unix syslog。

class logging.handlers.SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM)

返回一个 SysLogHandler 类的新实例用来与通过 address 以 (host, port) 元组形式给出地址的远程 Unix 机器进行通讯。 如果未指定 address，则使用 ('localhost', 514)。 该地址会被用于打开套接字。 提供 (host, port) 元组的一种替代方式是提供字符串形式的地址，例如 ‘/dev/log’。 在这种情况下，会使用 Unix 域套接字将消息发送到 syslog。 如果未指定 facility，则使用 LOG_USER。 打开的套接字类型取决于 socktype 参数，该参数的默认值为 socket.SOCK_DGRAM 即打开一个 UDP 套接字。 要打开一个 TCP 套接字（用来配合较新的 syslog 守护程序例如 rsyslog 使用），请指定值为 socket.SOCK_STREAM。

请注意如果你的服务器不是在 UDP 端口 514 上进行侦听，则 SysLogHandler 可能无法正常工作。 在这种情况下，请检查你应当为域套接字所使用的地址 —— 它依赖于具体的系统。 例如，在 Linux 上通常为 ‘/dev/log’ 而在 OS/X 上则为 ‘/var/run/syslog’。 你需要检查你的系统平台并使用适当的地址（如果你的应用程序需要在多个平台上运行则可能需要在运行时进行这样的检查）。 在 Windows 上，你大概必须要使用 UDP 选项。

在 3.2 版更改: 添加了 socktype。

close()

关闭连接远程主机的套接字。

emit(record)

记录会被格式化，然后发送到 syslog 服务器。 如果存在异常信息，则它 不会 被发送到服务器。

在 3.2.1 版更改: (参见: bpo-12168 [https://bugs.python.org/issue12168]。) 在较早的版本中，发送至 syslog 守护程序的消息总是以一个 NUL 字节结束，因为守护程序的早期版本期望接收一个以 NUL 结束的消息 —— 即使它不包含于对应的规范说明 (RFC 5424 [https://tools.ietf.org/html/rfc5424.html])。 这些守护程序的较新版本不再期望接收 NUL 字节，如果其存在则会将其去除，而最新的守护程序（更紧密地遵循 RFC 5424）会将 NUL 字节作为消息的一部分传递出去。

为了在面对所有这些不同守护程序行为时能够更方便地处理 syslog 消息，通过使用类层级属性 append_nul，添加 NUL 字节的操作已被作为可配置项。 该属性默认为 True (保留现有行为) 但可在 SysLogHandler 实例上设为 False 以便让实例 不会 添加 NUL 结束符。

在 3.3 版更改: (参见: bpo-12419 [https://bugs.python.org/issue12419]。) 在较早的版本中，没有 “ident” 或 “tag” 前缀工具可以用来标识消息的来源。 现在则可以使用一个类层级属性来设置它，该属性默认为 "" 表示保留现有行为，但可在 SysLogHandler 实例上重载以便让实例不会为所处理的每条消息添加标识。 请注意所提供的标识必须为文本而非字节串，并且它会被原封不动地添加到消息中。

encodePriority(facility, priority)

将功能和优先级编码为一个整数。 你可以传入字符串或者整数 —— 如果传入的是字符串，则会使用内部的映射字典将其转换为整数。

符号 LOG_ 的值在 SysLogHandler 中定义并且是 sys/syslog.h 头文件中所定义值的镜像。

优先级

名称（字符串）

符号值

alert

LOG_ALERT

crit 或 critical

LOG_CRIT

debug

LOG_DEBUG

emerg 或 panic

LOG_EMERG

err 或 error

LOG_ERR

info

LOG_INFO

notice

LOG_NOTICE

warn 或 warning

LOG_WARNING

设备

名称（字符串）

符号值

auth

LOG_AUTH

authpriv

LOG_AUTHPRIV

cron

LOG_CRON

daemon

LOG_DAEMON

ftp

LOG_FTP

kern

LOG_KERN

lpr

LOG_LPR

mail

LOG_MAIL

news

LOG_NEWS

syslog

LOG_SYSLOG

user

LOG_USER

uucp

LOG_UUCP

local0

LOG_LOCAL0

local1

LOG_LOCAL1

local2

LOG_LOCAL2

local3

LOG_LOCAL3

local4

LOG_LOCAL4

local5

LOG_LOCAL5

local6

LOG_LOCAL6

local7

LOG_LOCAL7

mapPriority(levelname)

将日志记录级别名称映射到 syslog 优先级名称。 如果你使用自定义级别，或者如果默认算法不适合你的需要，你可能需要重载此方法。 默认算法将 DEBUG, INFO, WARNING, ERROR 和 CRITICAL 映射到等价的 syslog 名称，并将所有其他级别名称映射到 ‘warning’。

16.8.11. NTEventLogHandler

NTEventLogHandler 类位于 logging.handlers 模块，它支持将日志记录消息发送到本地 Windows NT, Windows 2000 或 Windows XP 事件日志。 在你使用它之前，你需要安装 Mark Hammond 的 Python Win32 扩展。

class logging.handlers.NTEventLogHandler(appname, dllname=None, logtype='Application')

返回一个 NTEventLogHandler 类的新实例。 appname 用来定义出现在事件日志中的应用名称。 将使用此名称创建适当的注册表条目。 dllname 应当给出要包含在日志中的消息定义的 .dll 或 .exe 的完整限定路径名称（如未指定则会使用 'win32service.pyd' —— 此文件随 Win32 扩展安装且包含一些基本的消息定义占位符。 请注意使用这些占位符将使你的事件日志变得很大，因为整个消息源都会被放入日志。 如果你希望有较小的日志，你必须自行传入包含你想要在事件日志中使用的消息定义的 .dll 或 .exe 名称）。 logtype 为 'Application', 'System' 或 'Security' 之一，且默认值为 'Application'。

close()

这时，你就可以从注册表中移除作为事件日志条目来源的应用名称。 但是，如果你这样做，你将无法如你所预期的那样在事件日志查看器中看到这些事件 —— 它必须能访问注册表来获取 .dll 名称。 当前版本并不会这样做。

emit(record)

确定消息 ID，事件类别和事件类型，然后将消息记录到 NT 事件日志中。

getEventCategory(record)

返回记录的事件类别。 如果你希望指定你自己的类别就要重载此方法。 此版本将返回 0。

getEventType(record)

返回记录的事件类型。 如果你希望指定你自己的类型就要重载此方法。 此版本将使用处理程序的 typemap 属性来执行映射，该属性在 __init__() 被设置为一个字典，其中包含 DEBUG, INFO, WARNING, ERROR 和 CRITICAL 的映射。 如果你使用你自己的级别，你将需要重载此方法或者在处理程序的 typemap 属性中放置一个合适的字典。

getMessageID(record)

返回记录的消息 ID。 如果你使用你自己的消息，你可以通过将 msg 传给日志记录器作为 ID 而非格式字符串实现此功能。 然后，你可以在这里使用字典查找来获取消息 ID。 此版本将返回 1，是 win32service.pyd 中的基本消息 ID.

16.8.12. SMTPHandler

SMTPHandler 类位于 logging.handlers 模块，它支持将日志记录消息通过 SMTP 发送到一个电子邮件地址。

class logging.handlers.SMTPHandler(mailhost, fromaddr, toaddrs, subject, credentials=None, secure=None, timeout=1.0)

返回一个 SMTPHandler 类的新实例。 该实例使用电子邮件的发件人、收件人地址和主题行进行初始化。 toaddrs 应当为字符串列表。 要指定一个非标准 SMTP 端口，请使用 (host, port) 元组格式作为 mailhost 参数。 如果你使用一个字符串，则会使用标准 SMTP 端口。 如果你的 SMTP 服务器要求验证，你可以指定一个 (username, password) 元组作为 credentials 参数。

要指定使用安全协议 (TLS)，请传入一个元组作为 secure 参数。 这将仅在提供了验证凭据时才能被使用。 元组应当或是一个空元组，或是一个包含密钥文件名的单值元组，或是一个包含密钥文件和证书文件的 2 值元组。 （此元组会被传给 smtplib.SMTP.starttls() 方法。）

可以使用 timeout 参数为与 SMTP 服务器的通信指定超时限制。

3.3 新版功能: 增加了 timeout 参数。

emit(record)

对记录执行格式化并将其发送到指定的地址。

getSubject(record)

如果你想要指定一个基于记录的主题行，请重载此方法。

16.8.13. MemoryHandler

MemoryHandler 类位于 logging.handlers 模块，它支持在内存中缓冲日志记录，并定期将其刷新到 target 处理程序中。 刷新会在缓冲区满的时候，或是在遇到特定或更高严重程度事件的时候发生。

MemoryHandler 是更通用的 BufferingHandler 的子类，后者属于抽象类。 它会在内存中缓冲日志记录。 当每条记录被添加到缓冲区时，会通过调用 shouldFlush() 来检查缓冲区是否应当刷新。 如果应当刷新，则要使用 flush() 来执行刷新。

class logging.handlers.BufferingHandler(capacity)

Initializes the handler with a buffer of the specified capacity.

emit(record)

Appends the record to the buffer. If shouldFlush() returns true, calls flush() to process the buffer.

flush()

你可以重载此方法来实现自定义的刷新行为。 此版本只是将缓冲区清空。

shouldFlush(record)

Returns true if the buffer is up to capacity. This method can be overridden to implement custom flushing strategies.

class logging.handlers.MemoryHandler(capacity, flushLevel=ERROR, target=None, flushOnClose=True)

Returns a new instance of the MemoryHandler class. The instance is initialized with a buffer size of capacity. If flushLevel is not specified, ERROR is used. If no target is specified, the target will need to be set using setTarget() before this handler does anything useful. If flushOnClose is specified as False, then the buffer is not flushed when the handler is closed. If not specified or specified as True, the previous behaviour of flushing the buffer will occur when the handler is closed.

在 3.6 版更改: 增加了 flushOnClose 形参。

close()

调用 flush()，设置目标为 None 并清空缓冲区。

flush()

对于 MemoryHandler，刷新是指将缓冲的记录发送到目标，如果存在目标的话。 当此行为发生时缓冲区也将被清空。 如果你想要不同的行为请重载此方法。

setTarget(target)

设置此处理程序的目标处理程序。

shouldFlush(record)

检测缓冲区是否已满或是有记录为 flushLevel 或更高级别。

16.8.14. HTTPHandler

HTTPHandler 类位于 logging.handlers 模块，它支持使用 GET 或 POST 语义将日志记录消息发送到 Web 服务器。

class logging.handlers.HTTPHandler(host, url, method='GET', secure=False, credentials=None, context=None)

返回一个 HTTPHandler 类的新实例。 host 可以为 host:port 的形式，如果你需要使用指定端口号的话。 如果没有指定 method，则会使用 GET。 如果 secure 为真值，则将使用 HTTPS 连接。 context 形参可以设为一个 ssl.SSLContext 实例以配置用于 HTTPS 连接的 SSL 设置。 如果指定了 credentials，它应当为包含 userid 和 password 的二元组，该元组将被放入使用 Basic 验证的 HTTP ‘Authorization’ 标头中。 如果你指定了 credentials，你还应当指定 secure=True 这样你的 userid 和 password 就不会以纯文本形式在线路上传输。

在 3.5 版更改: 增加了 context 形参。

mapLogRecord(record)

基于 record 提供一个字典，它将被执行 URL 编码并发送至 Web 服务器。 默认实现仅返回 record.__dict__。 在只需将 LogRecord 的某个子集发送至 Web 服务器，或者需要对发送至服务器的内容进行更多定制时可以重载此方法。

emit(record)

将记录以经 URL 编码的形式发送至 Web 服务器。 会使用 mapLogRecord() 方法来将要发送的记录转换为字典。

注解

由于记录发送至 Web 服务器所需的预处理与通用的格式化操作不同，使用 setFormatter() 来指定一个 Formatter 用于 HTTPHandler 是没有效果的。 此处理程序不会调用 format()，而是调用 mapLogRecord() 然后再调用 urllib.parse.urlencode() 来以适合发送至 Web 服务器的形式对字典进行编码。

16.8.15. QueueHandler

3.2 新版功能.

QueueHandler 类位于 logging.handlers 模块，它支持将日志记录消息发送到一个队列，例如在 queue 或 multiprocessing 模块中实现的队列。

配合 QueueListener 类使用，QueueHandler 可用来使处理程序在与执行日志记录的线程不同的线程上完成工作。 这对 Web 应用程序以及其他服务于客户端的线程需要尽可能快地响应的服务应用程序来说很重要，任何潜在的慢速操作（例如通过 SMTPHandler 发送邮件）都要在单独的线程上完成。

class logging.handlers.QueueHandler(queue)

Returns a new instance of the QueueHandler class. The instance is initialized with the queue to send messages to. The queue can be any queue-like object; it’s used as-is by the enqueue() method, which needs to know how to send messages to it.

emit(record)

Enqueues the result of preparing the LogRecord.

prepare(record)

准备用于队列的记录。 此方法返回的对象会被排入队列。

The base implementation formats the record to merge the message and arguments, and removes unpickleable items from the record in-place.

如果你想要将记录转换为 dict 或 JSON 字符串，或者发送记录被修改后的副本而让初始记录保持原样，则你可能会想要重载此方法。

enqueue(record)

使用 put_nowait() 将记录排入队列；如果你想要使用阻塞行为，或超时设置，或自定义的队列实现，则你可能会想要重载此方法。

16.8.16. QueueListener

3.2 新版功能.

QueueListener 类位于 logging.handlers 模块，它支持从一个队列接收日志记录消息，例如在 queue 或 multiprocessing 模块中实现的队列。 消息是在内部线程中从队列接收并在同一线程上传递到一个或多个处理程序进行处理的。 尽管 QueueListener 本身并不是一个处理程序，但由于它要与 QueueHandler 配合工作，因此也在此处介绍。

配合 QueueHandler 类使用，QueueListener 可用来使处理程序在与执行日志记录的线程不同的线程上完成工作。 这对 Web 应用程序以及其他服务与客户端的线程需要尽可能快地响应的服务应用程序来说很重要，任何潜在的慢速动作（例如通过 SMTPHandler 发送邮件）都要在单独的线程上完成。

class logging.handlers.QueueListener(queue, *handlers, respect_handler_level=False)

Returns a new instance of the QueueListener class. The instance is initialized with the queue to send messages to and a list of handlers which will handle entries placed on the queue. The queue can be any queue-like object; it’s passed as-is to the dequeue() method, which needs to know how to get messages from it. If respect_handler_level is True, a handler’s level is respected (compared with the level for the message) when deciding whether to pass messages to that handler; otherwise, the behaviour is as in previous Python versions - to always pass each message to each handler.

在 3.5 版更改: The respect_handler_levels argument was added.

dequeue(block)

从队列移出一条记录并将其返回，可以选择阻塞。

基本实现使用 get()。 如果你想要使用超时设置或自定义的队列实现，则你可能会想要重载此方法。

prepare(record)

准备一条要处理的记录。

该实现只是返回传入的记录。 如果你想要对记录执行任何自定义的 marshal 操作或在将其传给处理程序之前进行调整，则你可能会想要重载此方法。

handle(record)

处理一条记录。

此方法简单地循环遍历处理程序，向它们提供要处理的记录。 实际传给处理程序的对象就是从 prepare() 返回的对象。

start()

启动监听器。

此方法启动一个后台线程来监视 LogRecords 队列以进行处理。

stop()

停止监听器。

此方法要求线程终止，然后等待它完成终止操作。 请注意在你的应用程序退出之前如果你没有调用此方法，则可能会有一些记录在留在队列中，它们将不会被处理。

enqueue_sentinel()

将一个标记写入队列以通知监听器退出。 此实现会使用 put_nowait()。 如果你想要使得超时设置或自定义的队列实现，则你可能会想要重载此方法。

3.3 新版功能.

参见

模块 logging

日志记录模块的 API 参考。

模块 logging.config

日志记录模块的配置 API 。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

16.9. getpass — 便携式密码输入工具

源代码: Lib/getpass.py [https://github.com/python/cpython/tree/3.6/Lib/getpass.py]

getpass 模块提供了两个函数：

getpass.getpass(prompt='Password: ', stream=None)

提示用户输入一个密码且不会回显。 用户会看到字符串 prompt 作为提示，其默认值为 'Password: '。 在 Unix 上，如有必要提示会使用替换错误句柄写入到文件类对象 stream。 stream 默认指向控制终端 (/dev/tty)，如果不可用则指向 sys.stderr (此参数在 Windows 上会被忽略)。

如果回显自由输入不可用则 getpass() 将回退为打印一条警告消息到 stream 并且从 sys.stdin 读取同时发出 GetPassWarning。

注解

如果你从 IDLE 内部调用 getpass，输入可能是在你启动 IDLE 的终端中而非在 IDEL 窗口本身中完成。

exception getpass.GetPassWarning

一个当密码输入可能被回显时发出的 UserWarning 子类。

getpass.getuser()

返回用户的“登录名称”。

此函数会按顺序检查环境变量 LOGNAME, USER, LNAME 和 USERNAME，并返回其中第一个被设置为非空字符串的值。 如果均未设置，则在支持 pwd 模块的系统上将返回来自密码数据库的登录名，否则将引发一个异常。

通常情况下，此函数应优先于 os.getlogin() 使用。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

16.10. curses — 终端字符单元显示的处理

curses 模块提供了 curses 库的接口，这是可移植高级终端处理的事实标准。

虽然 curses 在 Unix 环境中使用最为广泛，但也有适用于 Windows，DOS 以及其他可能的系统的版本。此扩展模块旨在匹配 ncurses 的 API，这是一个部署在 Linux 和 Unix 的 BSD 变体上的开源 curses 库。

注解

每当文档提到 字符 时，它可以被指定为一个整数，一个单字符 Unicode 字符串或者一个单字节的字节字符串。

每当此文档提到 字符串 时，它可以被指定为一个 Unicode 字符串或者一个字节字符串。

注解

从 5.4 版本开始，ncurses 库使用 nl_langinfo 函数来决定如何解释非 ASCII 数据。这意味着你需要在程序中调用 locale.setlocale() 函数，并使用一种系统中可用的编码方法来编码 Unicode 字符串。这个例子使用了系统默认的编码:

import locale
locale.setlocale(locale.LC_ALL, '')
code = locale.getpreferredencoding()

然后使用 code 作为 str.encode() 调用的编码。

参见

模块 curses.ascii

在 ASCII 字符上工作的工具，无论你的区域设置是什么。

模块 curses.panel

为 curses 窗口添加深度的面板栈扩展。

模块 curses.textpad

用于使 curses 支持 Emacs 式绑定的可编辑文本部件。

用 Python 进行 Curses 编程

关于配合 Python 使用 curses 的教学材料，由 Andrew Kuchling 和 Eric Raymond 撰写。

Python 源码发布包的 Tools/demo/ [https://github.com/python/cpython/tree/3.6/Tools/demo/] 目录包含了一些使用此模块所提供的 curses 绑定的示例程序。

16.10.1. 函数

curses 模块定义了以下异常：

exception curses.error

当 curses 库中函数返回一个错误时引发的异常。

注解

只要一个函数或方法的 x 或 y 参数是可选项，它们会默认为当前光标位置。 而当 attr 是可选项时，它会默认为 A_NORMAL。

curses 模块定义了以下函数：

curses.baudrate()

以每秒比特数为单位返回终端输出速度。 在软件终端模拟器上它将具有一个固定的最高值。 此函数出于历史原因被包括；在以前，它被用于写输出循环以提供时间延迟，并偶尔根据线路速度来改变接口。

curses.beep()

发出短促的提醒声音。

curses.can_change_color()

根据程序员能否改变终端显示的颜色返回 True 或 False。

curses.cbreak()

进入 cbreak 模式。 在 cbreak 模式（有时也称为“稀有”模式）通常的 tty 行缓冲会被关闭并且字符可以被一个一个地读取。 但是，与原始模式不同，特殊字符（中断、退出、挂起和流程控制）会在 tty 驱动和调用程序上保留其效果。 首先调用 raw() 然后调用 cbreak() 会将终端置于 cbreak 模式。

curses.color_content(color_number)

返回颜色值 color_number 中红、绿和蓝（RGB）分量的强度，此强度值必须介于 0 和 COLORS 之间。 返回一个 3 元组，其中包含给定颜色的 R,G,B 值，它们必须介于 0 (无分量) 和 1000 (最大分量) 之间。

curses.color_pair(color_number)

返回用于显示指定颜色的文本的属性值。 该属性值可与 A_STANDOUT, A_REVERSE 以及其他 A_* 属性组合使用。 pair_number() 是此函数的对应操作。

curses.curs_set(visibility)

设置光标状态。 visibility 可设为 0, 1 或 2 表示不可见、正常与高度可见。 如果终端支持所请求的可见性，则返回之前的光标状态；否则会引发异常。 在许多终端上，“正常可见”模式为下划线光标而“高度可见”模式为方块形光标。

curses.def_prog_mode()

将当前终端模式保存为 “program” 模式，即正在运行的程序使用 curses 时的模式。 （与其相对的是 “shell” 模式，即程序不使用 curses。） 对 reset_prog_mode() 的后续调用将恢复此模式。

curses.def_shell_mode()

将当前终端模式保存为 “shell” 模式，即正在运行的程序不使用 curses 的模式。 （与其相对的是 “program” 模式，即程序使用 功能。） 对 reset_shell_mode() 的后续调用将恢复此模式。

curses.delay_output(ms)

在输出中插入 ms 毫秒的暂停。

curses.doupdate()

更新物理屏幕。 curses 库会保留两个数据结构，一个代表当前物理屏幕的内容以及一个虚拟屏幕代表需要的后续状态。 doupdate() 整体更新物理屏幕以匹配虚拟屏幕。

虚拟屏幕可以通过在写入操作例如在一个窗口上执行 addstr() 之后调用 noutrefresh() 来刷新。 普通的 refresh() 调用只是简单的 noutrefresh() 加 doupdate()；如果你需要更新多个窗口，你可以通过在所有窗口上发出 noutrefresh() 调用再加单次 doupdate() 来提升性能并可减少屏幕闪烁。

curses.echo()

进入 echo 模式。 在 echo 模式下，输入的每个字符都会在输入后回显到屏幕上。

curses.endwin()

撤销库的初始化，使终端返回正常状态。

curses.erasechar()

将用户的当前擦除字符以单字节字节串对象的形式返回。 在 Unix 操作系统下这是 curses 程序用来控制 tty 的属性，而不是由 curses 库本身来设置的。

curses.filter()

如果要使用 filter() 例程，它必须在调用 initscr() 之前被调用。 其效果是在这些调用期间，LINES 会被设为 1；clear, cup, cud, cud1, cuu1, cuu, vpa 等功能会被禁用；而 home 字符串会被设为 cr 的值。 其影响是光标会被限制在当前行内，屏幕刷新也是如此。 这可被用于启用单字符模式的行编辑而不触及屏幕的其余部分。

curses.flash()

闪烁屏幕。 也就是将其改为反显并在很短的时间内将其改回原状。 有些人更喜欢这样的‘视觉响铃’而非 beep() 所产生的听觉提醒信号。

curses.flushinp()

刷新所有输入缓冲区。 这会丢弃任何已被用户输入但尚未被程序处理的预输入内容。

curses.getmouse()

After getch() returns KEY_MOUSE to signal a mouse event, this method should be call to retrieve the queued mouse event, represented as a 5-tuple (id, x, y, z, bstate). id is an ID value used to distinguish multiple devices, and x, y, z are the event’s coordinates. (z is currently unused.) bstate is an integer value whose bits will be set to indicate the type of event, and will be the bitwise OR of one or more of the following constants, where n is the button number from 1 to 4: BUTTONn_PRESSED, BUTTONn_RELEASED, BUTTONn_CLICKED, BUTTONn_DOUBLE_CLICKED, BUTTONn_TRIPLE_CLICKED, BUTTON_SHIFT, BUTTON_CTRL, BUTTON_ALT.

curses.getsyx()

将当前虚拟屏幕光标的坐标作为元组 (y, x) 返回。 如果 leaveok 当前为 True，则返回 (-1, -1)。

curses.getwin(file)

读取由之前的 putwin() 调用存放在文件中的窗口相关数据。 该例程随后将使用该数据创建并初始化一个新窗口，并返回该新窗口对象。

curses.has_colors()

如果终端能显示彩色则返回 True；否则返回 False。

curses.has_ic()

如果终端具有插入和删除字符的功能则返回 True。 此函数仅是出于历史原因而被包括的，因为所有现代软件终端模拟器都具有这些功能。

curses.has_il()

如果终端具有插入和删除字符功能，或者能够使用滚动区域来模拟这些功能则返回 True。 此函数仅是出于历史原因而被包括的，因为所有现代软件终端模拟器都具有这些功能。

curses.has_key(ch)

接受一个键值 ch，并在当前终端类型能识别出具有该值的键时返回 True。

curses.halfdelay(tenths)

用于半延迟模式，与 cbreak 模式的类似之处是用户所键入的字符会立即对程序可用。 但是，在阻塞 tenths 个十分之一秒之后，如果还未输入任何内容则将引发异常。 tenths 值必须为 1 和 255 之间的数字。 使用 nocbreak() 可退出半延迟模式。

curses.init_color(color_number, r, g, b)

更改某个颜色的定义，接受要更改的颜色编号以及三个 RGB 值（表示红绿蓝三分量的强度）。 color_number 值必须为 0 和 COLORS 之间的数字。 r, g, b 值分别必须为 0 和 1000 之间的数字。 当使用 init_color() 时，出现在屏幕上的对应颜色会立即按照新定义来更改。 此函数在大多数终端上都是无操作的；它仅会在 can_change_color() 返回 True 时生效。

curses.init_pair(pair_number, fg, bg)

更改某个颜色对的定义。 它接受三个参数：要更改的颜色对编号，前景色编号和背景色编号。 pair_number 值必须为 1 和 COLOR_PAIRS - 1 之间的数字（并且 0 号颜色对固定为黑底白字而无法更改）。 fg 和 bg 参数值必须为 0 和 COLORS 之间的数字。 如果颜色对之前已被初始化，则屏幕会被刷新使得出现在屏幕上的该颜色对会立即按照新定义来更改。

curses.initscr()

初始化库。 返回代表整个屏幕的 窗口 对象。

注解

如果打开终端时发生错误，则下层的 curses 库可能会导致解释器退出。

curses.is_term_resized(nlines, ncols)

如果 resize_term() 会修改窗口结构则返回 True，否则返回 False。

curses.isendwin()

如果 endwin() 已经被调用（即 curses 库已经被撤销初始化则返回 True。

curses.keyname(k)

将编号为 k 的键名称作为字节串对象返回。 生成可打印 ASCII 字符的键名称就是键所对应的字符。 Ctrl-键组合的键名称则是一个两字节的字节串对象，它由插入符 (b'^') 加对应的可打印 ASCII 字符组成。 Alt-键组合 (128–255) 的键名称则是由前缀 b'M-' 加对应的可打印 ASCII 字符组成的字节串对象。

curses.killchar()

将用户的当前行删除字符以单字节字节串对象的形式返回。 在 Unix 操作系统下这是 curses 程序用来控制 tty 的属性，而不是由 curses 库本身来设置的。

curses.longname()

返回一个字节串对象，其中包含描述当前终端的 terminfo 长名称字段。 详细描述的最大长度为 128 个字符。 它仅在调用 initscr() 之后才会被定义。

curses.meta(flag)

如果 flag 为 True，则允许输入 8 比特位的字符。 如果 flag 为 False，则只允许 7 比特位的字符。

curses.mouseinterval(interval)

以毫秒为单位设置能够被识别为点击的按下和释放事件之间可以间隔的最长时间，并返回之前的间隔值。 默认值为 200 毫秒，即五分之一秒。

curses.mousemask(mousemask)

设置要报告的鼠标事件，并返回一个元组 (availmask, oldmask)。 availmask 表明指定的鼠标事件中哪些可以被报告；当完全失败时将返回 0。 oldmask 是给定窗口的鼠标事件之前的掩码值。 如果从未调用此函数，则不会报告任何鼠标事件。

curses.napms(ms)

休眠 ms 毫秒。

curses.newpad(nlines, ncols)

创建并返回一个指向具有给定行数和列数新的填充数据结构的指针。 将填充作为窗口对象返回。

面板类似于窗口，区别在于它不受屏幕大小的限制，并且不必与屏幕的特定部分相关联。 面板可以在需要使用大窗口时使用，并且每次只需将窗口的一部分放在屏幕上。 面板不会发生自动刷新（例如由于滚动或输入回显）。 面板的 refresh() 和 noutrefresh() 方法需要 6 个参数来指定面板要显示的部分以及要用于显示的屏幕位置。 这些参数是 pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol；p 参数表示要显示的面板区域的左上角而 s 参数定义了要显示的面板区域在屏幕上的剪切框。

curses.newwin(nlines, ncols)

curses.newwin(nlines, ncols, begin_y, begin_x)

返回一个新的 窗口，其左上角位于 (begin_y, begin_x)，并且其高度/宽度为 nlines/ncols。

默认情况下，窗口将从指定位置扩展到屏幕的右下角。

curses.nl()

进入换行模式。 此模式会在输入时将回车转换为换行符，并在输出时将换行符转换为回车加换行。 换行模式会在初始时启用。

curses.nocbreak()

退出 cbreak 模式。 返回具有行缓冲的正常 “cooked” 模式。

curses.noecho()

退出 echo 模式。 关闭输入字符的回显。

curses.nonl()

退出 newline 模式。 停止在输入时将回车转换为换行，并停止在输出时从换行到换行/回车的底层转换（但这不会改变 addch('\n') 的行为，此行为总是在虚拟屏幕上执行相当于回车加换行的操作）。 当停止转换时，curses 有时能使纵向移动加快一些；并且，它将能够在输入时检测回车键。

curses.noqiflush()

当使用 noqiflush() 例程时，与 INTR, QUIT 和 SUSP 字符相关联的输入和输出队列的正常刷新将不会被执行。 如果你希望在处理程序退出后还能继续输出，就像没有发生过中断一样，你可能会想要在信号处理程序中调用 noqiflush()。

curses.noraw()

退出 raw 模式。 返回具有行缓冲的正常 “cooked” 模式。

curses.pair_content(pair_number)

返回包含对应于所请求颜色对的颜色的元组 (fg, bg)。 pair_number 的值必须在 1 和 COLOR_PAIRS - 1 之间。

curses.pair_number(attr)

返回通过属性值 attr 所设置的颜色对的编号。 color_pair() 是此函数的对应操作。

curses.putp(str)

等价于 tputs(str, 1, putchar)；为当前终端发出指定 terminfo 功能的值。 请注意 putp() 的输出总是前往标准输出。

curses.qiflush([flag])

如果 flag 为 False，则效果与调用 noqiflush() 相同。 如果 flag 为 True 或未提供参数，则在读取这些控制字符时队列将被刷新。

curses.raw()

进入 raw 模式。 在 raw 模式下，正常的行缓冲和对中断、退出、挂起和流程控制键的处理会被关闭；字符会被逐个地提交给 curses 输入函数。

curses.reset_prog_mode()

将终端恢复到 “program” 模式，如之前由 def_prog_mode() 所保存的一样。

curses.reset_shell_mode()

将终端恢复到 “shell” 模式，如之前由 def_shell_mode() 所保存的一样。

curses.resetty()

将终端模式恢复到最后一次调用 savetty() 时的状态。

curses.resize_term(nlines, ncols)

由 resizeterm() 用来执行大部分工作的后端函数；当调整窗口大小时，resize_term() 会以空白填充扩展区域。 调用方应用程序应当以适当的数据填充这些区域。 resize_term() 函数会尝试调整所有窗口的大小。 但是，由于面板的调用约定，在不与应用程序进行额外交互的情况下是无法调整其大小的。

curses.resizeterm(nlines, ncols)

将标准窗口和当前窗口的大小调整为指定的尺寸，并调整由 curses 库所使用的记录窗口尺寸的其他记录数据（特别是 SIGWINCH 处理程序）。

curses.savetty()

将终端模式的当前状态保存在缓冲区中，可供 resetty() 使用。

curses.setsyx(y, x)

将虚拟屏幕光标设置到 y, x。 如果 y 和 x 均为 -1，则 leaveok 将设为 True。

curses.setupterm(term=None, fd=-1)

初始化终端。 term 为给出终端名称的字符串或为 None；如果省略或为 None，则将使用 TERM 环境变量的值。 fd 是任何初始化序列将被发送到的文件描述符；如未指定或为 -1，则将使用 sys.stdout 的文件描述符。

curses.start_color()

如果程序员想要使用颜色，则必须在任何其他颜色操作例程被调用之前调用它。 在 initscr() 之后立即调用此例程是一个很好的做法。

start_color() 会初始化八种基本颜色（黑、红、绿、黄、蓝、品、青和白）以及 curses 模块中的两个全局变量 COLORS 和 COLOR_PAIRS，其中包含终端可支持的颜色和颜色对的最大数量。 它还会将终端中的颜色恢复为终端刚启动时的值。

curses.termattrs()

返回终端所支持的所有视频属性逻辑 OR 的值。 此信息适用于当 curses 程序需要对屏幕外观进行完全控制的情况。

curses.termname()

将环境变量 TERM 的值截短至 14 个字节，作为字节串对象返回。

curses.tigetflag(capname)

将与 terminfo 功能名称 capname 相对应的布尔功能值以整数形式返回。 如果 capname 不是一个布尔功能则返回 -1，如果其被取消或不存在于终端描述中则返回 0。

curses.tigetnum(capname)

将与 terminfo 功能名称 capname 相对应的数字功能值以整数形式返回。 如果 capname 不是一个数字功能则返回 -2，如果其被取消或不存在于终端描述中则返回 -1。

curses.tigetstr(capname)

将与 terminfo 功能名称 capname 相对应的字符串功能值以字节串对象形式返回。 如果 capname 不是一个 terminfo “字符串功能” 或者如果其被取消或不存在于终端描述中则返回 None。

curses.tparm(str[, ...])

使用提供的形参初始化字节串对象 str，其中 str 应当是从 terminfo 数据库获取的参数化字符串。 例如 tparm(tigetstr("cup"), 5, 3) 的结果可能为 b'\033[6;4H'，实际结果将取决于终端类型。

curses.typeahead(fd)

指定将被用于预输入检查的文件描述符 fd。 如果 fd 为 -1，则不执行预输入检查。

curses 库会在更新屏幕时通过定期查找预输入来执行 “断行优化”。 如果找到了输入，并且输入是来自于 tty，则会将当前更新推迟至 refresh 或 doupdate 再次被调用的时候，以便允许更快地响应预先输入的命令。 此函数允许为预输入检查指定其他的文件描述符。

curses.unctrl(ch)

返回一个字节串对象作为字符 ch 的可打印表示形式。 控制字符会表示为一个变换符加相应的字符，例如 b'^C'。 可打印字符则会保持原样。

curses.ungetch(ch)

推送 ch 以便让下一个 getch() 返回该字符。

注解

在 getch() 被调用之前只能推送一个 ch。

curses.update_lines_cols()

更新 LINES 和 COLS。 适用于检测屏幕大小的手动调整。

3.5 新版功能.

curses.unget_wch(ch)

推送 ch 以便让下一个 get_wch() 返回该字符。

注解

在 get_wch() 被调用之前只能推送一个 ch。

3.3 新版功能.

curses.ungetmouse(id, x, y, z, bstate)

将 KEY_MOUSE 事件推送到输入队列，将其与给定的状态数据进行关联。

curses.use_env(flag)

如果使用此函数，则应当在调用 initscr() 或 newterm 之前调用它。 当 flag 为 False 时，将会使用在 terminfo 数据库中指定的行和列的值，即使设置了环境变量 LINES 和 COLUMNS (默认使用)，或者如果 curses 是在窗口中运行（在此情况下如果未设置 LINES 和 COLUMNS 则默认行为将是使用窗口大小）。

curses.use_default_colors()

允许在支持此特性的终端上使用默认的颜色值。 使用此函数可在你的应用程序中支持透明效果。 默认颜色会被赋给颜色编号 -1。 举例来说，在调用此函数后，init_pair(x, curses.COLOR_RED, -1) 会将颜色对 x 初始化为红色前景和默认颜色背景。

curses.wrapper(func, ...)

初始化 curses 并调用另一个可调用对象 func，该对象应当为你的使用 curses 的应用程序的其余部分。 如果应用程序引发了异常，此函数将在重新引发异常并生成回溯信息之前将终端恢复到正常状态。 随后可调用对象 func 会被传入主窗口 ‘stdscr’ 作为其第一个参数，再带上其他所有传给 wrapper() 的参数。 在调用 func 之前，wrapper() 会启用 cbreak 模式，关闭回显，启用终端键盘，并在终端具有颜色支持的情况下初始化颜色。 在退出时（无论是正常退出还是异常退出）它会恢复 cooked 模式，打开回显，并禁用终端键盘。

16.10.2. Window 对象

Window 对象会由上面的 initscr() 和 newwin() 返回，它具有以下方法和属性:

window.addch(ch[, attr])

window.addch(y, x, ch[, attr])

Paint character ch at (y, x) with attributes attr, overwriting any character previously painter at that location. By default, the character position and attributes are the current settings for the window object.

注解

在窗口、子窗口或面板之外写入会引发 curses.error。 尝试在窗口、子窗口或面板的右下角写入将在字符被打印之后导致异常被引发。

window.addnstr(str, n[, attr])

window.addnstr(y, x, str, n[, attr])

将带有属性 attr 的字符串 str 中的至多 n 个字符绘制到 (y, x)，覆盖之前在屏幕上的任何内容。

window.addstr(str[, attr])

window.addstr(y, x, str[, attr])

将带有属性 attr 的字符串 str 绘制到 (y, x)，覆盖之前在屏幕上的任何内容。

注解

在窗口、子窗口或面板之外写入会引发 curses.error。 尝试在窗口、子窗口或面板的右下角写入将在字符串被打印之后导致异常被引发。

window.attroff(attr)

从应用于写入到当前窗口的 “background” 集中移除属性 attr。

window.attron(attr)

向应用于写入到当前窗口的 “background” 集中添加属性 attr。

window.attrset(attr)

将 “background” 属性集设为 attr。 该集合初始时为 0 (无属性)。

window.bkgd(ch[, attr])

将窗口 background 特征属性设为带有属性 attr 的字符 ch。 随后此修改将应用于放置到该窗口中的每个字符。

窗口中每个字符的属性会被修改为新的 background 属性。

不论之前的 background 字符出现在哪里，它都会被修改为新的 background 字符。

window.bkgdset(ch[, attr])

设置窗口的背景。 窗口的背景由字符和属性的任意组合构成。 背景的属性部分会与写入窗口的所有非空白字符合并（即 OR 运算）。 背景和字符和属性部分均会与空白字符合并。 背景将成为字符的特征属性并在任何滚动与插入/删除行/字符操作中与字符一起移动。

window.border([ls[, rs[, ts[, bs[, tl[, tr[, bl[, br]]]]]]]])

在窗口边缘绘制边框。每个参数指定用于边界特定部分的字符;请参阅下表了解更多详情。

注解

任何形参的值为 0 都将导致该形参使用默认字符。 关键字形参 不可 被使用。 默认字符在下表中列出:

参数

描述

默认值

ls

左侧

ACS_VLINE

rs

右侧

ACS_VLINE

ts

顶部

ACS_HLINE

bs

底部

ACS_HLINE

tl

左上角

ACS_ULCORNER

tr

右上角

ACS_URCORNER

bl

左下角

ACS_LLCORNER

br

右下角

ACS_LRCORNER

window.box([vertch, horch])

类似于 border()，但 ls 和 rs 均为 vertch 而 ts 和 bs 均为 horch。 此函数总是会使用默认的转角字符。

window.chgat(attr)

window.chgat(num, attr)

window.chgat(y, x, attr)

window.chgat(y, x, num, attr)

在当前光标位置或是在所提供的位置 (y, x) 设置 num 个字符的属性。 如果 num 未给出或为 -1，则将属性设置到所有字符上直至行尾。 如果提供了位置 (y, x) 则此函数会将光标移至该位置。 修改过的行将使用 touchline() 方法处理以便下次窗口刷新时内容会重新显示。

window.clear()

类似于 erase()，但还会导致在下次调用 refresh() 时整个窗口被重新绘制。

window.clearok(flag)

如果 flag 为 True，则在下次调用 refresh() 时将完全清除窗口。

window.clrtobot()

从光标位置开始擦除直至窗口末端：光标以下的所有行都会被删除，然后会执行 clrtoeol() 的等效操作。

window.clrtoeol()

从光标位置开始擦除直至行尾。

window.cursyncup()

更新窗口所有上级窗口的当前光标位置以反映窗口的当前光标位置。

window.delch([y, x])

删除位于 (y, x) 的任何字符。

window.deleteln()

删除在光标之下的行。 所有后续的行都会上移一行。

window.derwin(begin_y, begin_x)

window.derwin(nlines, ncols, begin_y, begin_x)

“derive window” 的缩写，derwin() 与调用 subwin() 等效，不同之处在于 begin_y 和 begin_x 是想对于窗口的初始位置，而不是相对于整个屏幕。 返回代表所派生窗口的窗口对象。

window.echochar(ch[, attr])

使用属性 attr 添加字符 ch，并立即在窗口上调用 refresh()。

window.enclose(y, x)

检测给定的相对屏幕的字符-单元格坐标是否被给定的窗口所包围，返回 True 或 False。 它适用于确定是哪个屏幕窗口子集包围着某个鼠标事件的位置。

window.encoding

用于编码方法参数（Unicode 字符串和字符）的编码格式。 encoding 属性是在创建子窗口时从父窗口继承的，例如通过 window.subwin()。 默认情况下，会使用当前区域的编码格式 (参见 locale.getpreferredencoding())。

3.3 新版功能.

window.erase()

清空窗口。

window.getbegyx()

返回左上角坐标的元组 (y, x)。

window.getbkgd()

返回给定窗口的当前背景字符/属性对。

window.getch([y, x])

获取一个字符。 请注意所返回的整数 不一定 要在 ASCII 范围以内：功能键、小键盘键等等是由大于 255 的数字表示的。 在无延迟模式下，如果没有输入则返回 -1，在其他情况下都会等待直至有键被按下。

window.get_wch([y, x])

获取一个宽字符。 对于大多数键都是返回一个字符，对于功能键、小键盘键和其他特殊键则是返回一个整数。 在无延迟模式下，如果没有输入则返回一个异常。

3.3 新版功能.

window.getkey([y, x])

获取一个字符，返回一个字符串而不是像 getch() 那样返回一个整数。 功能键、小键盘键和其他特殊键则是返回一个包含键名的多字节字符串。 在无延迟模式下，如果没有输入则引发一个异常。

window.getmaxyx()

返回窗口高度和宽度的元组 (y, x)。

window.getparyx()

将此窗口相对于父窗口的起始坐标作为元组 (y, x) 返回。 如果此窗口没有父窗口则返回 (-1, -1)。

window.getstr()

window.getstr(n)

window.getstr(y, x)

window.getstr(y, x, n)

从用户读取一个字节串对象，附带基本的行编辑功能。

window.getyx()

返回当前光标相对于窗口左上角的位置的元组 (y, x)。

window.hline(ch, n)

window.hline(y, x, ch, n)

显示一条起始于 (y, x) 长度为 n 个字符 ch 的水平线。

window.idcok(flag)

如果 flag 为 False，curses 将不再考虑使用终端的硬件插入/删除字符功能；如果 flag 为 True，则会启用字符插入和删除。 当 curses 首次初始化时，默认会启用字符插入/删除。

window.idlok(flag)

如果 flag 为 True，curses 将尝试使用硬件行编辑功能。 否则，行插入/删除会被禁用。

window.immedok(flag)

如果 flag 为 True，窗口图像中的任何改变都会自动导致窗口被刷新；你不必再自己调用 refresh()。 但是，这可能会由于重复调用 wrefresh 而显著降低性能。 此选项默认被禁用。

window.inch([y, x])

返回窗口中给定位置上的字符。 下面的 8 个比特位是字符本身，上面的比特位则为属性。

window.insch(ch[, attr])

window.insch(y, x, ch[, attr])

将带有属性 attr 的字符串 ch 绘制到 (y, x)，将该行从位置 x 开始右移一个字符。

window.insdelln(nlines)

在指定窗口的当前行上方插入 nlines 行。 下面的 nlines 行将丢失。 对于 nlines 为负值的情况，则从光标下方的行开始删除 nlines 行，并将其余的行向上移动。 下面的 nlines 行会被清空。 当前光标位置将保持不变。

window.insertln()

在光标下方插入一个空行。 所有后续的行都会下移一行。

window.insnstr(str, n[, attr])

window.insnstr(y, x, str, n[, attr])

在光标下方的字符之前插入一个至多为 n 个字符的字符串（字符数量将与该行相匹配）。 如果 n 为零或负数，则插入整个字符串。 光标右边的所有字符将被右移，该行右端的字符将丢失。 光标位置将保持不变（在移到可能指定的 y, x 之后）。

window.insstr(str[, attr])

window.insstr(y, x, str[, attr])

在光标下方的字符之前插入一个字符串（字符数量将与该行相匹配）。 光标右边的所有字符将被右移，该行右端的字符将丢失。 光标位置将保持不变（在移到可能指定的 y, x 之后）。

window.instr([n])

window.instr(y, x[, n])

返回从窗口的当前光标位置，或者指定的 y, x 开始提取的字符所对应的字节串对象。 属性会从字符中去除。 如果指定了 n，instr() 将返回长度至多为 n 个字符的字符串（不包括末尾的 NUL）。

window.is_linetouched(line)

如果指定的行自上次调用 refresh() 后发生了改变则返回 True；否则返回 False。 如果 line 对于给定的窗口不可用则会引发 curses.error 异常。

window.is_wintouched()

如果指定的窗口自上次调用 refresh() 后发生了改变则返回 True；否则返回 False。

window.keypad(flag)

如果 flag 为 True，则某些键（小键盘键、功能键等）生成的转义序列将由 curses 来解析。 如果 flag 为 False，转义序列将保持在输入流中的原样。

window.leaveok(flag)

如果 flag 为 True，则在更新时光标将停留在原地，而不是在“光标位置”。 这将可以减少光标的移动。 在可能的情况下光标将变为不可见。

如果 flag 为 False，光标在更新后将总是位于“光标位置”。

window.move(new_y, new_x)

将光标移至 (new_y, new_x)。

window.mvderwin(y, x)

让窗口在其父窗口内移动。 窗口相对于屏幕的参数不会被更改。 此例程用于在屏幕的相同物理位置显示父窗口的不同部分。

window.mvwin(new_y, new_x)

移动窗口以使其左上角位于 (new_y, new_x)。

window.nodelay(flag)

如果 flag 为 True，则 getch() 将为非阻塞的。

window.notimeout(flag)

如果 flag 为 True，则转义序列将不会发生超时。

如果 flag 为 False，则在几毫秒之后，转义序列将不会被解析，并将保持在输入流中的原样。

window.noutrefresh()

标记为刷新但保持等待。 此函数会更新代表预期窗口状态的数据结构，但并不强制更新物理屏幕。 要完成后者，请调用 doupdate()。

window.overlay(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])

将窗口覆盖在 destwin 上方。 窗口的大小不必相同，只有重叠的区域会被复制。 此复制是非破坏性的，这意味着当前背景字符不会覆盖掉 destwin 的旧内容。

为了获得对被复制区域的细粒度控制，可以使用 overlay() 的第二种形式。 sminrow 和 smincol 是源窗口的左上角坐标，而其他变量则在目标窗口中标记出一个矩形。

window.overwrite(destwin[, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol])

将窗口覆盖在 destwin 上方。 窗口的大小不必相同，此时只有重叠的区域会被复制。 此复制是破坏性的，这意味着当前背景字符会覆盖掉 destwin 的旧内容。

为了获得对被复制区域的细粒度控制，可以使用 overwrite() 的第二种形式。 sminrow 和 smincol 是源窗口的左上角坐标，而其他变量则在目标窗口中标记出一个矩形。

window.putwin(file)

将关联到窗口的所有数据写入到所提供的文件对象。 此信息可在以后使用 getwin() 函数来提取。

window.redrawln(beg, num)

指明从 beg 行开始的 num 个屏幕行已被破坏并且应当在下次 refresh() 调用时完全重绘。

window.redrawwin()

标记整个窗口，以使其在下次 refresh() 调用时完全重绘。

window.refresh([pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol])

立即更新显示（将实际屏幕与之前的绘制/删除方法进行同步）。

6 个可选参数仅在窗口为使用 newpad() 创建的面板时可被指定。 需要额外的形参来指定所涉及到的是面板和屏幕的哪一部分。 pminrow 和 pmincol 指定要在面板中显示的矩形的左上角。 sminrow, smincol, smaxrow 和 smaxcol 指定要在屏幕中显示的矩形的边。 要在面板中显示的矩形的右下角是根据屏幕坐标计算出来的，由于矩形的大小必须相同。 两个矩形都必须完全包含在其各自的结构之内。 负的 pminrow, pmincol, sminrow 或 smincol 值会被视为将它们设为零值。

window.resize(nlines, ncols)

为 curses 窗口重新分配存储空间以将其尺寸调整为指定的值。 如果任一维度的尺寸大于当前值，则窗口的数据将以具有合并了当前背景渲染（由 bkgdset() 设置）的空白来填充。

window.scroll([lines=1])

将屏幕或滚动区域向上滚动 lines 行。

window.scrollok(flag)

控制当一个窗口的光标移出窗口或滚动区域边缘时会发生什么，这可能是在底端行执行换行操作，或者在最后一行输入最后一个字符导致的结果。 如果 flag 为 False，光标会留在底端行。 如果 flag 为 True，窗口会向上滚动一行。 请注意为了在终端上获得实际的滚动效果，还需要调用 idlok()。

window.setscrreg(top, bottom)

设置从 top 行至 bottom 行的滚动区域。 所有滚动操作将在此区域中进行。

window.standend()

关闭 standout 属性。 在某些终端上此操作会有关闭所有属性的副作用。

window.standout()

启用属性 A_STANDOUT。

window.subpad(begin_y, begin_x)

window.subpad(nlines, ncols, begin_y, begin_x)

返回一个子窗口，其左上角位于 (begin_y, begin_x)，并且其宽度/高度为 ncols/nlines。

window.subwin(begin_y, begin_x)

window.subwin(nlines, ncols, begin_y, begin_x)

返回一个子窗口，其左上角位于 (begin_y, begin_x)，并且其宽度/高度为 ncols/nlines。

默认情况下，子窗口将从指定位置扩展到窗口的右下角。

window.syncdown()

触碰已在上级窗口上被触碰的每个位置。 此例程由 refresh() 调用，因此几乎从不需要手动调用。

window.syncok(flag)

如果 flag 为 True，则 syncup() 会在窗口发生改变的任何时候自动被调用。

window.syncup()

触碰已在窗口中被改变的此窗口的各个上级窗口中的所有位置。

window.timeout(delay)

为窗口设置阻塞或非阻塞读取行为。 如果 delay 为负值，则会使用阻塞读取（这将无限期地等待输入）。 如果 delay 为零，则会使用非阻塞读取，并且当没有输入在等待时 getch() 将返回 -1。 如果 delay 为正值，则 getch() 将阻塞 delay 毫秒，并且当此延时结束时仍无输入将返回 -1。

window.touchline(start, count[, changed])

假定从行 start 开始的 count 行已被更改。 如果提供了 changed，它将指明是将受影响的行标记为已更改 (changed=True) 还是未更改 (changed=False)。

window.touchwin()

假定整个窗口已被更改，其目的是用于绘制优化。

window.untouchwin()

将自上次调用 refresh() 以来窗口中的所有行标记为未改变。

window.vline(ch, n)

window.vline(y, x, ch, n)

显示一条起始于 (y, x) 长度为 n 个字符 ch 的垂直线。

16.10.3. 常量

curses 模块定义了以下数据成员:

curses.ERR

Some curses routines that return an integer, such as getch(), return ERR upon failure.

curses.OK

一些返回整数的 curses 例程，例如 napms()，在成功时将返回 OK。

curses.version

一个代表当前模块版本的字节串对象。 也作 __version__。

有些常量可用于指定字符单元属性。 实际可用的常量取决于具体的系统。

属性

含义

A_ALTCHARSET

备用字符集模式

A_BLINK

闪烁模式

A_BOLD

粗体模式

A_DIM

暗淡模式

A_INVIS

不可见或空白模式

A_NORMAL

正常属性

A_PROTECT

保护模式

A_REVERSE

反转背景色和前景色

A_STANDOUT

突出模式

A_UNDERLINE

下划线模式

A_HORIZONTAL

水平突出显示

A_LEFT

左高亮

A_LOW

底部高亮

A_RIGHT

右高亮

A_TOP

顶部高亮

A_VERTICAL

垂直突出显示

A_CHARTEXT

用于提取字符的位掩码

有几个常量可用于提取某些方法返回的相应属性。

位掩码

含义

A_ATTRIBUTES

用于提取属性的位掩码

A_CHARTEXT

用于提取字符的位掩码

A_COLOR

用于提取颜色对字段信息的位掩码

键由名称以 KEY_ 开头的整数常量引用。确切的可用键取决于系统。

关键常数

（Windows 注册表的）键

KEY_MIN

最小键值

KEY_BREAK

中断键（不可靠）

KEY_DOWN

向下箭头

KEY_UP

向上箭头

KEY_LEFT

向左箭头

KEY_RIGHT

向右箭头

KEY_HOME

Home 键 (上+左箭头)

KEY_BACKSPACE

退格（不可靠）

KEY_F0

功能键。 支持至多 64 个功能键。

KEY_Fn

功能键 n 的值

KEY_DL

删除行

KEY_IL

插入行

KEY_DC

删除字符

KEY_IC

插入字符或进入插入模式

KEY_EIC

退出插入字符模式

KEY_CLEAR

清空屏幕

KEY_EOS

清空至屏幕底部

KEY_EOL

清空至行尾

KEY_SF

向前滚动 1 行

KEY_SR

向后滚动 1 行 (反转)

KEY_NPAGE

下一页

KEY_PPAGE

上一页

KEY_STAB

设置制表符

KEY_CTAB

清除制表制

KEY_CATAB

清除所有制表符

KEY_ENTER

回车或发送 (不可靠)

KEY_SRESET

软件 (部分) 重置 (不可靠)

KEY_RESET

重置或硬重置 (不可靠)

KEY_PRINT

打印

KEY_LL

Home 向下或到底 (左下)

KEY_A1

键盘的左上角

KEY_A3

键盘的右上角

KEY_B2

键盘的中心

KEY_C1

键盘左下方

KEY_C3

键盘右下方

KEY_BTAB

回退制表符

KEY_BEG

Beg (开始)

KEY_CANCEL

取消

KEY_CLOSE

关闭

KEY_COMMAND

Cmd (命令行)

KEY_COPY

复制

KEY_CREATE

创建

KEY_END

End

KEY_EXIT

退出

KEY_FIND

查找

KEY_HELP

帮助

KEY_MARK

标记

KEY_MESSAGE

消息

KEY_MOVE

移动

KEY_NEXT

下一个

KEY_OPEN

打开

KEY_OPTIONS

选项

KEY_PREVIOUS

Prev (上一个)

KEY_REDO

重做

KEY_REFERENCE

Ref (引用)

KEY_REFRESH

刷新

KEY_REPLACE

替换

KEY_RESTART

重启

KEY_RESUME

恢复

KEY_SAVE

保存

KEY_SBEG

Shift 的 Beg (开始)

KEY_SCANCEL

Shift 的 Cancel

KEY_SCOMMAND

Shift 的 Command

KEY_SCOPY

Shift + Copy

KEY_SCREATE

Shift + Create

KEY_SDC

Shift + 删除字符

KEY_SDL

Shift + 删除行

KEY_SELECT

选择

KEY_SEND

Shift + End

KEY_SEOL

Shift + 清空行

KEY_SEXIT

Shift + Exit

KEY_SFIND

Shift + 查找

KEY_SHELP

Shift + 帮助

KEY_SHOME

Shift + Home

KEY_SIC

Shift + 输入

KEY_SLEFT

Shift + 向左箭头

KEY_SMESSAGE

Shift + 消息

KEY_SMOVE

Shift + 移动

KEY_SNEXT

Shift + 下一个

KEY_SOPTIONS

Shift + 选项

KEY_SPREVIOUS

Shift + 上一个

KEY_SPRINT

Shift + 打印

KEY_SREDO

Shift + 重做

KEY_SREPLACE

Shift + 替换

KEY_SRIGHT

Shift + 向右箭头

KEY_SRSUME

Shift + 恢复

KEY_SSAVE

Shift + 保存

KEY_SSUSPEND

Shift + 挂起

KEY_SUNDO

Shift + 撤销

KEY_SUSPEND

挂起

KEY_UNDO

撤销操作

KEY_MOUSE

鼠标事件已发生

KEY_RESIZE

终端大小改变事件

KEY_MAX

最大键值

在VT100及其软件仿真（例如X终端仿真器）上，通常至少有四个功能键（ KEY_F1, KEY_F2, KEY_F3, KEY_F4 ）可用，并且箭头键以明显的方式映射到 KEY_UP, KEY_DOWN, KEY_LEFT 和 KEY_RIGHT 。如果您的机器有一个PC键盘，可以安全地使用箭头键和十二个功能键（旧的PC键盘可能只有十个功能键）;此外，以下键盘映射是标准的：

键帽

常数

Insert

KEY_IC

Delete

KEY_DC

Home

KEY_HOME

End

KEY_END

Page Up

KEY_PPAGE

Page Down

KEY_NPAGE

下表列出了替代字符集中的字符。 这些字符继承自 VT100 终端，在 X 终端等软件模拟器上通常均为可用。 当没有可用的图形时，curses 会回退为粗糙的可打印 ASCII 近似符号。

注解

只有在调用 initscr() 之后才能使用它们

ACS代码

含义

ACS_BBSS

右上角的别名

ACS_BLOCK

实心方块

ACS_BOARD

正方形

ACS_BSBS

水平线的别名

ACS_BSSB

左上角的别名

ACS_BSSS

顶部 T 型的别名

ACS_BTEE

底部 T 型

ACS_BULLET

正方形

ACS_CKBOARD

棋盘（点刻）

ACS_DARROW

向下箭头

ACS_DEGREE

等级符

ACS_DIAMOND

菱形

ACS_GEQUAL

大于或等于

ACS_HLINE

水平线

ACS_LANTERN

灯形符号

ACS_LARROW

向左箭头

ACS_LEQUAL

小于或等于

ACS_LLCORNER

左下角

ACS_LRCORNER

右下角

ACS_LTEE

左侧 T 型

ACS_NEQUAL

不等号

ACS_PI

字母π

ACS_PLMINUS

正负号

ACS_PLUS

加号

ACS_RARROW

向右箭头

ACS_RTEE

右侧 T 型

ACS_S1

扫描线 1

ACS_S3

扫描线3

ACS_S7

扫描线7

ACS_S9

扫描线 9

ACS_SBBS

右下角的别名

ACS_SBSB

垂直线的别名

ACS_SBSS

右侧 T 型的别名

ACS_SSBB

左下角的别名

ACS_SSBS

底部 T 型的别名

ACS_SSSB

左侧 T 型的别名

ACS_SSSS

交叉或大加号的替代名称

ACS_STERLING

英镑

ACS_TTEE

顶部 T 型

ACS_UARROW

向上箭头

ACS_ULCORNER

左上角

ACS_URCORNER

右上角

ACS_VLINE

垂线

下表列出了预定义的颜色：

常数

颜色

COLOR_BLACK

黑色

COLOR_BLUE

蓝色

COLOR_CYAN

青色（浅绿蓝色）

COLOR_GREEN

绿色

COLOR_MAGENTA

洋红色（紫红色）

COLOR_RED

红色

COLOR_WHITE

白色

COLOR_YELLOW

黄色

16.11. curses.textpad — 用于 curses 程序的文本输入控件

curses.textpad 模块提供了一个 Textbox 类，该类在 curses 窗口中处理基本的文本编辑，支持一组与 Emacs 类似的键绑定（因此这也适用于 Netscape Navigator, BBedit 6.x, FrameMaker 和许多其他程序）。 该模块还提供了一个绘制矩形的函数，适用于容纳文本框或其他目的。

curses.textpad 模块定义了以下函数:

curses.textpad.rectangle(win, uly, ulx, lry, lrx)

绘制一个矩形。 第一个参数必须为窗口对象；其余参数均为相对于该窗口的坐标值。 第二和第三个参数为要绘制的矩形的左上角的 y 和 x 坐标值；第四和第五个参数为其右下角的 y 和 x 坐标值。 将会使用 VT100/IBM PC 形式的字符在可用的终端上（包括 xterm 和大多数其他软件终端模拟器）绘制矩形。 在其他情况下则将使用 ASCII 横杠、竖线和加号绘制。

16.11.1. 文本框对象

你可以通过如下方式实例化一个 Textbox:

class curses.textpad.Textbox(win)

返回一个文本框控件对象。 win 参数必须是一个 curses 窗口 对象，文本框将被包含在其中。 文本框的编辑光标在初始时位于包含窗口的左上角，坐标值为 (0, 0)。 实例的 stripspaces 旗标初始时为启用。

Textbox 对象具有以下方法:

edit([validator])

这是你通常将使用的入口点。 它接受编辑按键直到键入了一个终止按键。 如果提供了 validator，它必须是一个函数。 它将在每次按键时被调用并传入相应的按键作作为形参；命令发送将在结果上执行。 此方法会以字符串形式返回窗口内容；是否包括窗口中的空白将受到 stripspaces 属性的影响。

do_command(ch)

处理单个按键命令。以下是支持的特殊按键：

按键

动作

Control-A

转到窗口的左边缘。

Control-B

光标向左，如果可能，包含前一行。

Control-D

删除光标下的字符。

Control-E

前往右边缘（stripspaces 关闭时）或者行尾（stripspaces 启用时）。

Control-F

向右移动光标，适当时换行到下一行。

Control-G

终止，返回窗口内容。

Control-H

向后删除字符。

Control-J

如果窗口是1行则终止，否则插入换行符。

Control-K

如果行为空，则删除它，否则清除到行尾。

Control-L

刷新屏幕。

Control-N

光标向下;向下移动一行。

Control-O

在光标位置插入一个空行。

Control-P

光标向上;向上移动一行。

如果光标位于无法移动的边缘，则移动操作不执行任何操作。在可能的情况下，支持以下同义词：

常数

按键

KEY_LEFT

Control-B

KEY_RIGHT

Control-F

KEY_UP

Control-P

KEY_DOWN

Control-N

KEY_BACKSPACE

Control-h

所有其他按键将被视为插入给定字符并右移的命令（带有自动折行）。

gather()

以字符串形式返回窗口内容；是否包括窗口中的空白将受到 stripspaces 成员的影响。

stripspaces

此属性是控制窗口中空白解读方式的旗标。 当启用时，每一行的末尾空白会被忽略；任何将光标定位至末尾空白的光标动作都将改为前往该行末尾，并且在收集窗口内容时将去除末尾空白。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

16.12. curses.ascii — 用于 ASCII 字符的工具

curses.ascii 模块提供了一些 ASCII 字符的名称常量以及在各种 ASCII 字符类中执行成员检测的函数。 所提供的控制字符常量如下:

名称

含义

NUL

SOH

标题开始，控制台中断

STX

文本开始

ETX

文本结束

EOT

传输结束

ENQ

查询，附带 ACK 流量控制

ACK

确认

BEL

蜂鸣器

BS

退格

TAB

Tab

HT

TAB 的别名： “水平制表符”

LF

换行

NL

LF 的别名： “新行”

VT

垂直制表符

FF

换页

CR

回车

SO

Shift-out，开始替换字符集

SI

Shift-in，恢复默认字符集

DLE

Data-link escape

DC1

XON，用于流程控制

DC2

Device control 2，阻塞模式流程控制

DC3

XOFF，用于流程控制

DC4

设备控制4

NAK

否定确认

SYN

同步空闲

ETB

末端传输块

CAN

取消

EM

媒体结束

SUB

替换

ESC

退出

FS

文件分隔符

GS

组分隔符

RS

记录分隔符，块模式终结器

US

单位分隔符

SP

空格

DEL

删除

请注意其中有许多在现今已经没有实际作用。 这些助记符是来源于数字计算机之前的电传打印机规范。

此模块提供了下列函数，对应于标准 C 库中的函数:

curses.ascii.isalnum(c)

检测 ASCII 字母数字类字符；它等价于 isalpha(c) 或 isdigit(c)。

curses.ascii.isalpha(c)

检测 ASCII 字母类字符；它等价于 isupper(c) or islower(c)。

curses.ascii.isascii(c)

检测字符值是否在 7 位 ASCII 集范围内。

curses.ascii.isblank(c)

检测 ASCII 空白字符；包括空格或水平制表符。

curses.ascii.iscntrl(c)

检测 ASCII 控制字符（在 0x00 到 0x1f 或 0x7f 范围内）。

curses.ascii.isdigit(c)

检测 ASCII 十进制数码，即 '0' 至 '9'。 它等价于 c in string.digits。

curses.ascii.isgraph(c)

检测任意 ASCII 可打印字符，不包括空白符。

curses.ascii.islower(c)

检测 ASCII 小写字母字符。

curses.ascii.isprint(c)

检测任意 ASCII 可打印字符，包括空白符。

curses.ascii.ispunct(c)

检测任意 ASCII 可打印字符，不包括空白符或字母数字类字符。

curses.ascii.isspace(c)

检测 ASCII 空白字符；包括空格，换行，回车，进纸，水平制表和垂直制表。

curses.ascii.isupper(c)

检测 ASCII 大写字母字符。

curses.ascii.isxdigit(c)

检测 ASCII 十六进制数码。 这等价于 c in string.hexdigits。

curses.ascii.isctrl(c)

检测 ASCII 控制字符（序号值 0 至 31）。

curses.ascii.ismeta(c)

检测非 ASCII 字符（码位值 0x80 及以上）。

这些函数接受整数或单字符字符串；当参数为字符串时，会先使用内置函数 ord() 进行转换。

请注意所有这些函数都是检测根据你传入的字符串的字符所生成的码位值；它们实际上完全不会知晓本机的字符编码格式。

以下两个函数接受单字符字符串或整数形式的字节值；它们会返回相同类型的值。

curses.ascii.ascii(c)

返回对应于 c 的下个 7 比特位的 ASCII 值。

curses.ascii.ctrl(c)

返回对应于给定字符的控制字符（字符比特值会与 0x1f 进行按位与运算）。

curses.ascii.alt(c)

返回对应于给定 ASCII 字符的 8 比特位字符（字符比特值会与 0x80 进行按位或运算）。

以下函数接受单字符字符串或整数值；它会返回一个字符串。

curses.ascii.unctrl(c)

返回 ASCII 字符 c 的字符串表示形式。 如果 c 是可打印字符，则字符串为字符本身。 如果该字符是控制字符 (0x00–0x1f) 则字符串由一个插入符 ('^') 加相应的大写字母组成。 如果该字符是 ASCII 删除符 (0x7f) 则字符串为 '^?'。 如果该字符设置了元比特位 (0x80)，元比特位会被去除，应用以上规则后将在结果之前添加 '!'。

curses.ascii.controlnames

一个 33 元素的字符串数据，其中按从 0 (NUL) 到 0x1f (US) 的顺序包含了三十二个 ASCII 控制字符的 ASCII 助记符，另加空格符的助记符 SP。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

16.13. curses.panel — curses 的 panel 栈扩展

面板是具有添加深度功能的窗口，因此它们可以从上至下堆叠为栈，只有显示每个窗口的可见部分会显示出来。 面板可以在栈中被添加、上移或下移，也可以被移除。

16.13.1. 函数

curses.panel 模块定义了以下函数:

curses.panel.bottom_panel()

返回面板栈中的底部面板。

curses.panel.new_panel(win)

返回一个面板对象，将其与给定的窗口 win 相关联。 请注意你必须显式地保持所返回的面板对象。 如果你不这样做，面板对象会被垃圾回收并从面板栈中被移除。

curses.panel.top_panel()

返回面板栈中的顶部面板。

curses.panel.update_panels()

在面板栈发生改变后更新虚拟屏幕。 这不会调用 curses.doupdate()，因此你不必自己执行此操作。

16.13.2. Panel 对象

Panel 对象，如上面 new_panel() 所返回的对象，是带有栈顺序的多个窗口。 总是会有一个窗口与确定内容的面板相关联，面板方法会负责窗口在面板栈中的深度。

Panel 对象具有以下方法:

Panel.above()

返回当前面板之上的面板。

Panel.below()

返回当前面板之下的面板。

Panel.bottom()

将面板推至栈底部。

Panel.hidden()

如果面板被隐藏（不可见）则返回 True，否则返回 False。

Panel.hide()

隐藏面板。 这不会删除对象，它只是让窗口在屏幕上不可见。

Panel.move(y, x)

将面板移至屏幕坐标 (y, x)。

Panel.replace(win)

将与面板相关联的窗口改为窗口 win。

Panel.set_userptr(obj)

将面板的用户指向设为 obj。 这被用来将任意数据与面板相关联，数据可以是任何 Python 对象。

Panel.show()

显示面板（面板可能已被隐藏）。

Panel.top()

将面板推至栈顶部。

Panel.userptr()

返回面板的用户指针。 这可以是任何 Python 对象。

Panel.window()

返回与面板相关联的窗口对象。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

16.14. platform — 获取底层平台的标识数据

示例代码： Lib/platform.py [https://github.com/python/cpython/tree/3.6/Lib/platform.py]

注解

特定平台按字母顺序排列，Linux 包括在 Unix 小节之中。

16.14.1. 跨平台

platform.architecture(executable=sys.executable, bits='', linkage='')

查询给定的可执行文件（默认为 Python 解释器二进制码文件）来获取各种架构信息。

返回一个元素 (bits, linkage)，其中包含可执行文件所使用的位架构和链接格式信息。 这两个值均以字符串形式返回。

无法确定的值将返回为形参预设所给出的值。 如果给出的位数为 ''，则会使用 sizeof(pointer) (或者当 Python 版本 < 1.5.2 时为 sizeof(long)) 作为所支持的指针大小的提示。

此函数依赖于系统的 file 命令来执行实际的操作。 这在几乎所有 Unix 平台和某些非 Unix 平台上只有当可执行文件指向 Python 解释器时才可用。 当以上要求不满足时将会使用合理的默认值。

注解

在 Mac OS X（也许还有其他平台）上，可执行文件可能是包含多种架构的通用文件。

要获取当前解释器的“64 位性”，更可靠的做法是查询 sys.maxsize 属性:

is_64bits = sys.maxsize > 2**32

platform.machine()

返回机器类型，例如 'i386'。 如果该值无法确定则会返回一个空字符串。

platform.node()

返回计算机的网络名称（可能不是完整限定名称！）。 如果该值无法确定则会返回一个空字符串。

platform.platform(aliased=0, terse=0)

返回一个标识底层平台的字符串，其中带有尽可能多的有用信息。

输出信息的目标是“人类易读”而非机器易解析。 它在不同平台上可能看起来不一致，这是有意为之的。

如果 aliased 为真值，此函数将使用各种平台不同与其通常名称的别名来报告系统名称，例如 SunOS 将被报告为 Solaris。 system_alias() 函数将被用于实现此功能。

将 terse 设为真值将导致此函数只返回标识平台所必须的最小量信息。

platform.processor()

返回（真实的）处理器名称，例如 'amdk6'。

如果该值无法确定则将返回空字符串。 请注意许多平台都不提供此信息或是简单地返回与 machine() 相同的值。 NetBSD 则会提供此信息。

platform.python_build()

返回一个元组 (buildno, builddate)，以字符串表示的 Python 编译代码和日期。

platform.python_compiler()

返回一个表示用于编译 Python 的编译器的的字符串。

platform.python_branch()

返回一个表示 Python 实现的 SCM 分支的字符串。

platform.python_implementation()

返回一个标识 Python 实现的字符串。 可能的返回值有: ‘CPython’, ‘IronPython’, ‘Jython’, ‘PyPy’。

platform.python_revision()

返回一个标识 Python 实现的 SCM 修订版的字符串。

platform.python_version()

将 Python 版本以字符串 'major.minor.patchlevel' 形式返回。

请注意此返回值不同于 Python sys.version，它将总是包括 patchlevel (默认为 0)。

platform.python_version_tuple()

将 Python 版本以字符串元组 (major, minor, patchlevel) 形式返回。

请注意此返回值不同于 Python sys.version，它将总是包括 patchlevel (默认为 '0')。

platform.release()

返回系统的发布版本，例如 '2.2.0' 或 'NT'，如果该值无法确定则将返回一个空字符串。

platform.system()

Returns the system/OS name, e.g. 'Linux', 'Windows', or 'Java'. An empty string is returned if the value cannot be determined.

platform.system_alias(system, release, version)

返回别名为某些系统所使用的常见营销名称的 (system, release, version)。 它还会在可能导致混淆的情况下对信息进行一些重排序操作。

platform.version()

返回系统的发布版本信息，例如 '#3 on degas'。 如果该值无法确定则将返回一个空字符串。

platform.uname()

具有高可移植性的 uname 接口。 返回包含六个属性的 namedtuple(): system, node, release, version, machine 和 processor。

请注意此函数添加的第六个属性 (processor) 并不存在于 os.uname() 的结果中。 并且前两个属性的属性名称也不一致；os.uname() 是将它们称为 sysname 和 nodename。

无法确定的条目会被设为 ''。

在 3.3 版更改: 将结果从元组改为命名元组。

16.14.2. Java平台

platform.java_ver(release='', vendor='', vminfo=('', '', ''), osinfo=('', '', ''))

Jython的版本接口

返回一个元组 (release, vendor, vminfo, osinfo)，其中 vminfo 为元组 (vm_name, vm_release, vm_vendor) 而 osinfo 为元组 (os_name, os_version, os_arch)。 无法确定的值将设为由形参所给出的默认值 (默认均为 '')。

16.14.3. Windows平台

platform.win32_ver(release='', version='', csd='', ptype='')

从 Windows 注册表获取额外的版本信息并返回一个元组 (release, version, csd, ptype) 表示 OS 发行版, 版本号, CSD 级别 (Service Pack) 和 OS 类型 (多个/单个处理器)。

一点提示: ptype 在单个处理器的 NT 机器上为 'Uniprocessor Free' 而在多个处理器的机器上为 'Multiprocessor Free'。 ‘Free’ 是指该 OS 版本没有调试代码。 它还可能显示 ‘Checked’ 表示该 OS 版本使用了调试代码，即检测参数、范围等的代码。

注解

此函数在安装了 Mark Hammond 的 win32all 包并且为 Python 2.3 或更新版本上效果最佳（此支持是在 Python 2.6 中增加的）。 显然它只能在兼容 Win32 的平台上运行。

16.14.3.1. Win95/98 specific

platform.popen(cmd, mode='r', bufsize=-1)

Portable popen() interface. Find a working popen implementation preferring win32pipe.popen(). On Windows NT, win32pipe.popen() should work; on Windows 9x it hangs due to bugs in the MS C library.

3.3 版后已移除: This function is obsolete. Use the subprocess module. Check especially the 使用 subprocess 模块替换旧函数 section.

16.14.4. Mac OS平台

platform.mac_ver(release='', versioninfo=('', '', ''), machine='')

获取 Mac OS 版本信息并将其返回为元组 (release, versioninfo, machine)，其中 versioninfo 是一个元组 (version, dev_stage, non_release_version)。

无法确定的条目会被设为 ''。 所有元组条目均为字符串。

16.14.5. Unix 平台

platform.dist(distname='', version='', id='', supported_dists=('SuSE', 'debian', 'redhat', 'mandrake', ...))

这是 linux_distribution() 的另外一个名字。

Deprecated since version 3.5, will be removed in version 3.8: See alternative like the distro [https://pypi.org/project/distro] package.

platform.linux_distribution(distname='', version='', id='', supported_dists=('SuSE', 'debian', 'redhat', 'mandrake', ...), full_distribution_name=1)

Tries to determine the name of the Linux OS distribution name.

supported_dists may be given to define the set of Linux distributions to look for. It defaults to a list of currently supported Linux distributions identified by their release file name.

If full_distribution_name is true (default), the full distribution read from the OS is returned. Otherwise the short name taken from supported_dists is used.

Returns a tuple (distname,version,id) which defaults to the args given as parameters. id is the item in parentheses after the version number. It is usually the version codename.

Deprecated since version 3.5, will be removed in version 3.8: See alternative like the distro [https://pypi.org/project/distro] package.

platform.libc_ver(executable=sys.executable, lib='', version='', chunksize=16384)

尝试确定可执行文件（默认为 Python 解释器）所链接到的 libc 版本。 返回一个字符串元组 (lib, version)，当查找失败时其默认值将设为给定的形参值。

请注意此函数对于不同 libc 版本向可执行文件添加符号的方式有深层的关联，可能仅适用于使用 gcc 编译出来的可执行文件。

文件将按 chunksize 个字节的分块来读取和扫描。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

16.15. errno — 标准 errno 系统符号

本模块提供标准的 errno 系统符号。每个符号的值是其对应的整数值。符号的名称和描述来自 linux/include/errno.h，应该是非常全面的。

errno.errorcode

提供从 errno 值到底层系统中字符串名称的映射的字典。例如， errno.errorcode[errno.EPERM] 映射为 'EPERM' 。

如果要将数字的错误代码转换为错误信息，请使用 os.strerror()。

在下面的列表中，当前平台上没有使用的符号没有被本模块定义。 已定义的符号的具体列表可参见 errno.errorcode.keys()。 可用的符号包括：

errno.EPERM

操作不被允许

errno.ENOENT

无此文件或目录

errno.ESRCH

无此进程

errno.EINTR

系统调用中断。

参见

此错误被映射到异常 InterruptedError。

errno.EIO

I/O 错误

errno.ENXIO

无此设备或地址

errno.E2BIG

参数列表过长

errno.ENOEXEC

执行格式错误

errno.EBADF

错误的文件号

errno.ECHILD

无子进程

errno.EAGAIN

重试

errno.ENOMEM

内存不足

errno.EACCES

没有权限

errno.EFAULT

错误的地址

errno.ENOTBLK

需要块设备

errno.EBUSY

设备或资源忙

errno.EEXIST

文件已存在

errno.EXDEV

跨设备链接

errno.ENODEV

无此设备

errno.ENOTDIR

不是目录

errno.EISDIR

是目录

errno.EINVAL

无效的参数

errno.ENFILE

文件表溢出

errno.EMFILE

打开的文件过多

errno.ENOTTY

不是打字机

errno.ETXTBSY

文本文件忙

errno.EFBIG

文件过大

errno.ENOSPC

设备已无可用空间

errno.ESPIPE

非法查找

errno.EROFS

只读文件系统

errno.EMLINK

链接过多

errno.EPIPE

管道已损坏

errno.EDOM

数学参数超出函数范围

errno.ERANGE

数学运算结果无法表示

errno.EDEADLK

将发生资源死锁

errno.ENAMETOOLONG

文件名过长

errno.ENOLCK

没有可用的记录锁

errno.ENOSYS

功能未实现

errno.ENOTEMPTY

目录非空

errno.ELOOP

遇到过多的符号链接

errno.EWOULDBLOCK

操作将阻塞

errno.ENOMSG

没有所需类型的消息

errno.EIDRM

标识符被移除

errno.ECHRNG

信道编号超出范围

errno.EL2NSYNC

级别 2 未同步

errno.EL3HLT

级别 3 已停止

errno.EL3RST

级别 3 重置

errno.ELNRNG

链接编号超出范围

errno.EUNATCH

未附加协议驱动

errno.ENOCSI

没有可用的 CSI 结构

errno.EL2HLT

级别 2 已停止

errno.EBADE

无效的交换

errno.EBADR

无效的请求描述符

errno.EXFULL

交换已满

errno.ENOANO

没有阳极

errno.EBADRQC

无效的请求码·

errno.EBADSLT

无效的槽位

errno.EDEADLOCK

文件锁定死锁错误

errno.EBFONT

错误的字体文件格式

errno.ENOSTR

设备不是流

errno.ENODATA

没有可用的数据

errno.ETIME

计时器已到期

errno.ENOSR

流资源不足

errno.ENONET

机器不在网络上

errno.ENOPKG

包未安装

errno.EREMOTE

对象是远程的

errno.ENOLINK

链接已被切断

errno.EADV

广告错误

errno.ESRMNT

挂载错误

errno.ECOMM

发送时通讯错误

errno.EPROTO

协议错误

errno.EMULTIHOP

已尝试多跳

errno.EDOTDOT

RFS 专属错误

errno.EBADMSG

非数据消息

errno.EOVERFLOW

值相对于已定义数据类型过大

errno.ENOTUNIQ

名称在网络上不唯一

errno.EBADFD

文件描述符处于错误状态

errno.EREMCHG

远端地址已改变

errno.ELIBACC

无法访问所需的共享库

errno.ELIBBAD

访问已损坏的共享库

errno.ELIBSCN

a.out 中的 .lib 部分已损坏

errno.ELIBMAX

尝试链接过多的共享库

errno.ELIBEXEC

无法直接执行共享库

errno.EILSEQ

非法字节序列

errno.ERESTART

已中断系统调用需要重启

errno.ESTRPIPE

流管道错误

errno.EUSERS

用户过多

errno.ENOTSOCK

在非套接字上执行套接字操作

errno.EDESTADDRREQ

需要目标地址

errno.EMSGSIZE

消息过长

errno.EPROTOTYPE

套接字的协议类型错误

errno.ENOPROTOOPT

协议不可用

errno.EPROTONOSUPPORT

协议不受支持

errno.ESOCKTNOSUPPORT

套接字类型不受支持

errno.EOPNOTSUPP

操作在传输端点上不受支持

errno.EPFNOSUPPORT

协议族不受支持

errno.EAFNOSUPPORT

地址族不受协议支持

errno.EADDRINUSE

地址已被使用

errno.EADDRNOTAVAIL

无法分配要求的地址

errno.ENETDOWN

网络已断开

errno.ENETUNREACH

网络不可达

errno.ENETRESET

网络因重置而断开连接

errno.ECONNABORTED

软件导致连接中止

errno.ECONNRESET

连接被对方重置

errno.ENOBUFS

没有可用的缓冲区空间

errno.EISCONN

传输端点已连接

errno.ENOTCONN

传输端点未连接

errno.ESHUTDOWN

传输端点关闭后无法发送

errno.ETOOMANYREFS

引用过多：无法拼接

errno.ETIMEDOUT

连接超时

errno.ECONNREFUSED

连接被拒

errno.EHOSTDOWN

主机已关闭

errno.EHOSTUNREACH

没有到主机的路由

errno.EALREADY

操作已在进行

errno.EINPROGRESS

操作正在进行

errno.ESTALE

过期的 NFS 文件句柄

errno.EUCLEAN

结构需要清理

errno.ENOTNAM

不是 XENIX 命名类型文件

errno.ENAVAIL

没有可用的 XENIX 信标

errno.EISNAM

是命名类型文件

errno.EREMOTEIO

远程 I/O 错误

errno.EDQUOT

超出配额

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

16.16. ctypes — Python 的外部函数库

ctypes 是 Python 的外部函数库。它提供了与 C 兼容的数据类型，并允许调用 DLL 或共享库中的函数。可使用该模块以纯 Python 形式对这些库进行封装。

16.16.1. ctypes 教程

注意：在本教程中的示例代码使用 doctest 进行过测试，保证其正确运行。由于有些代码在Linux，Windows或Mac OS X下的表现不同，这些代码会在 doctest 中包含相关的指令注解。

注意：部分示例代码引用了 ctypes c_int 类型。在 sizeof(long) == sizeof(int) 的平台上此类型是 c_long 的一个别名。所以，在程序输出 c_long 而不是你期望的 c_int 时不必感到迷惑 — 它们实际上是同一种类型。

16.16.1.1. 载入动态连接库

ctypes 导出了 cdll 对象，在 Windows 系统中还导出了 windll 和 oledll 对象用于载入动态连接库。

通过操作这些对象的属性，你可以载入外部的动态链接库。cdll 载入按标准的 cdecl 调用协议导出的函数，而 windll 导入的库按 stdcall 调用协议调用其中的函数。 oledll 也按 stdcall 调用协议调用其中的函数，并假定该函数返回的是 Windows HRESULT 错误代码，并当函数调用失败时，自动根据该代码甩出一个 OSError 异常。

在 3.3 版更改: 原来在 Windows 下甩出的异常类型 WindowsError 现在是 OSError 的一个别名。

这是一些 Windows 下的例子。注意：msvcrt 是微软 C 标准库，包含了大部分 C 标准函数，这些函数都是以 cdecl 调用协议进行调用的。

>>> from ctypes import *
>>> print(windll.kernel32)
<WinDLL 'kernel32', handle ... at ...>
>>> print(cdll.msvcrt)
<CDLL 'msvcrt', handle ... at ...>
>>> libc = cdll.msvcrt
>>>

Windows会自动添加通常的 .dll 文件扩展名。

注解

通过 cdll.msvcrt 调用的标准 C 函数，可能会导致调用一个过时的，与当前 Python 所不兼容的函数。因此，请尽量使用标准的 Python 函数，而不要使用 msvcrt 模块。

在 Linux 下，必须使用 包含 文件扩展名的文件名来导入共享库。因此不能简单使用对象属性的方式来导入库。因此，你可以使用方法 LoadLibrary()，或构造 CDLL 对象来导入库。

>>> cdll.LoadLibrary("libc.so.6")
<CDLL 'libc.so.6', handle ... at ...>
>>> libc = CDLL("libc.so.6")
>>> libc
<CDLL 'libc.so.6', handle ... at ...>
>>>

16.16.1.2. 操作导入的动态链接库中的函数

通过操作dll对象的属性来操作这些函数。

>>> from ctypes import *
>>> libc.printf
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.GetModuleHandleA)
<_FuncPtr object at 0x...>
>>> print(windll.kernel32.MyOwnFunction)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ctypes.py", line 239, in __getattr__
    func = _StdcallFuncPtr(name, self)
AttributeError: function 'MyOwnFunction' not found
>>>

注意：Win32系统的动态库，比如 kernel32 和 user32，通常会同时导出同一个函数的 ANSI 版本和 UNICODE 版本。UNICODE 版本通常会在名字最后以 W 结尾，而 ANSI 版本的则以 A 结尾。 win32的 GetModuleHandle 函数会根据一个模块名返回一个 模块句柄，该函数暨同时包含这样的两个版本的原型函数，并通过宏 UNICODE 是否定义，来决定宏 GetModuleHandle 导出的是哪个具体函数。

/* ANSI version */
HMODULE GetModuleHandleA(LPCSTR lpModuleName);
/* UNICODE version */
HMODULE GetModuleHandleW(LPCWSTR lpModuleName);

windll 不会通过这样的魔法手段来帮你决定选择哪一种函数，你必须显式的调用 GetModuleHandleA 或 GetModuleHandleW，并分别使用字节对象或字符串对象作参数。

有时候，dlls的导出的函数名不符合 Python 的标识符规范，比如 "??2@YAPAXI@Z"。此时，你必须使用 getattr() 方法来获得该函数。

>>> getattr(cdll.msvcrt, "??2@YAPAXI@Z")
<_FuncPtr object at 0x...>
>>>

Windows 下，有些 dll 导出的函数没有函数名，而是通过其顺序号调用。对此类函数，你也可以通过 dll 对象的数值索引来操作这些函数。

>>> cdll.kernel32[1]
<_FuncPtr object at 0x...>
>>> cdll.kernel32[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ctypes.py", line 310, in __getitem__
    func = _StdcallFuncPtr(name, self)
AttributeError: function ordinal 0 not found
>>>

16.16.1.3. 调用函数

你可以貌似是调用其它 Python 函数那样直接调用这些函数。在这个例子中，我们调用了 time() 函数，该函数返回一个系统时间戳（从 Unix 时间起点到现在的秒数），而``GetModuleHandleA()`` 函数返回一个 win32 模块句柄。

This example calls both functions with a NULL pointer (None should be used as the NULL pointer):

>>> print(libc.time(None))
1150640792
>>> print(hex(windll.kernel32.GetModuleHandleA(None)))
0x1d000000
>>>

注解

ctypes may raise a ValueError after calling the function, if it detects that an invalid number of arguments were passed. This behavior should not be relied upon. It is deprecated in 3.6.2, and will be removed in 3.7.

如果你用 cdecl 调用方式调用 stdcall 约定的函数，则会甩出一个异常 ValueError。反之亦然。

>>> cdll.kernel32.GetModuleHandleA(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Procedure probably called with not enough arguments (4 bytes missing)
>>>

>>> windll.msvcrt.printf(b"spam")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Procedure probably called with too many arguments (4 bytes in excess)
>>>

你必须阅读这些库的头文件或说明文档来确定它们的正确的调用协议。

在Windows中，ctypes 使用 win32 结构化异常处理来防止由于在调用函数时使用非法参数导致的程序崩溃。

>>> windll.kernel32.GetModuleHandleA(32)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: exception: access violation reading 0x00000020
>>>

然而，总有许多办法，通过调用 ctypes 使得 Python 程序崩溃。因此，你必须小心使用。 faulthandler 模块可以用于帮助诊断程序崩溃的原因。（比如由于错误的C库函数调用导致的段错误）。

None，整型，字节对象和（UNICODE）字符串是仅有的可以直接作为函数参数使用的四种Python本地数据类型。None` 作为C的空指针 (NULL)，字节和字符串类型作为一个指向其保存数据的内存块指针 （char * 或 wchar_t *)。Python 的整型则作为平台默认的C的 int 类型，他们的数值被截断以适应C类型的整型长度。

在我们开始调用函数前，我们必须先了解作为函数参数的 ctypes 数据类型。

16.16.1.4. 基础数据类型

ctypes 定义了一些和C兼容的基本数据类型：

ctypes 类型

C 类型

Python 类型

c_bool

_Bool

bool (1)

c_char

char

单字符字节对象

c_wchar

wchar_t

单字符字符串

c_byte

char

整型

c_ubyte

unsigned char

整型

c_short

short

整型

c_ushort

unsigned short

整型

c_int

int

整型

c_uint

unsigned int

整型

c_long

long

整型

c_ulong

unsigned long

整型

c_longlong

__int64 或 long long

整型

c_ulonglong

unsigned __int64 或 unsigned long long

整型

c_size_t

size_t

整型

c_ssize_t

ssize_t 或 Py_ssize_t

整型

c_float

float

浮点数

c_double

double

浮点数

c_longdouble

long double

浮点数

c_char_p

char * (以 NUL 结尾)

字节串对象或 None

c_wchar_p

wchar_t * (以 NUL 结尾)

字符串或 None

c_void_p

void *

int 或 None

构造函数接受任何具有真值的对象。

所有这些类型都可以通过使用正确类型和值的可选初始值调用它们来创建:

>>> c_int()
c_long(0)
>>> c_wchar_p("Hello, World")
c_wchar_p(140018365411392)
>>> c_ushort(-3)
c_ushort(65533)
>>>

由于这些类型是可变的，它们的值也可以在以后更改:

>>> i = c_int(42)
>>> print(i)
c_long(42)
>>> print(i.value)
42
>>> i.value = -99
>>> print(i.value)
-99
>>>

当给指针类型的对象 c_char_p, c_wchar_p 和 c_void_p 等赋值时，将改变它们所指向的 内存地址，而 不是 它们所指向的内存区域的 内容 (这是理所当然的，因为 Python 的 bytes 对象是不可变的):

>>> s = "Hello, World"
>>> c_s = c_wchar_p(s)
>>> print(c_s)
c_wchar_p(139966785747344)
>>> print(c_s.value)
Hello World
>>> c_s.value = "Hi, there"
>>> print(c_s)              # the memory location has changed
c_wchar_p(139966783348904)
>>> print(c_s.value)
Hi, there
>>> print(s)                # first object is unchanged
Hello, World
>>>

但你要注意不能将它们传递给会改变指针所指内存的函数。如果你需要可改变的内存块，ctypes 提供了 create_string_buffer() 函数，它提供多种方式创建这种内存块。当前的内存块内容可以通过 raw 属性存取，如果你希望将它作为NUL结束的字符串，请使用 value 属性:

>>> from ctypes import *
>>> p = create_string_buffer(3)            # create a 3 byte buffer, initialized to NUL bytes
>>> print(sizeof(p), repr(p.raw))
3 b'\x00\x00\x00'
>>> p = create_string_buffer(b"Hello")     # create a buffer containing a NUL terminated string
>>> print(sizeof(p), repr(p.raw))
6 b'Hello\x00'
>>> print(repr(p.value))
b'Hello'
>>> p = create_string_buffer(b"Hello", 10) # create a 10 byte buffer
>>> print(sizeof(p), repr(p.raw))
10 b'Hello\x00\x00\x00\x00\x00'
>>> p.value = b"Hi"
>>> print(sizeof(p), repr(p.raw))
10 b'Hi\x00lo\x00\x00\x00\x00\x00'
>>>

create_string_buffer() 函数替代以前的ctypes版本中的 c_buffer() 函数 (仍然可当作别名使用)和 c_string() 函数。create_unicode_buffer() 函数创建包含 unicode 字符的可变内存块，与之对应的C语言类型是 wchar_t。

16.16.1.5. 调用函数，继续

注意 printf 将打印到真正标准输出设备，而*不是* sys.stdout，因此这些实例只能在控制台提示符下工作，而不能在 IDLE 或 PythonWin 中运行。

>>> printf = libc.printf
>>> printf(b"Hello, %s\n", b"World!")
Hello, World!
14
>>> printf(b"Hello, %S\n", "World!")
Hello, World!
14
>>> printf(b"%d bottles of beer\n", 42)
42 bottles of beer
19
>>> printf(b"%f bottles of beer\n", 42.5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ArgumentError: argument 2: exceptions.TypeError: Don't know how to convert parameter 2
>>>

正如前面所提到过的，除了整数、字符串以及字节串之外，所有的 Python 类型都必须使用它们对应的 ctypes 类型包装，才能够被正确地转换为所需的C语言类型。

>>> printf(b"An int %d, a double %f\n", 1234, c_double(3.14))
An int 1234, a double 3.140000
31
>>>

16.16.1.6. 使用自定义的数据类型调用函数

你也可以通过自定义 ctypes 参数转换方式来允许自定义类型作为参数。 ctypes 会寻找 _as_parameter_ 属性并使用它作为函数参数。当然，它必须是数字、字符串或者二进制字符串:

>>> class Bottles:
...     def __init__(self, number):
...         self._as_parameter_ = number
...
>>> bottles = Bottles(42)
>>> printf(b"%d bottles of beer\n", bottles)
42 bottles of beer
19
>>>

如果你不想把实例的数据存储到 _as_parameter_ 属性。可以通过定义 property 函数计算出这个属性。

16.16.1.7. 指定必选参数的类型(函数原型)

可以通过设置 argtypes 属性的方法指定从 DLL 中导出函数的必选参数类型。

argtypes 必须是一个 C 数据类型的序列 (这里的 printf 可能不是个好例子，因为它是变长参数，而且每个参数的类型依赖于格式化字符串，不过尝试这个功能也很方便):

>>> printf.argtypes = [c_char_p, c_char_p, c_int, c_double]
>>> printf(b"String '%s', Int %d, Double %f\n", b"Hi", 10, 2.2)
String 'Hi', Int 10, Double 2.200000
37
>>>

指定数据类型可以防止不合理的参数传递(就像C函数的函数签名)，并且会自动尝试将参数转换为需要的类型:

>>> printf(b"%d %d %d", 1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ArgumentError: argument 2: exceptions.TypeError: wrong type
>>> printf(b"%s %d %f\n", b"X", 2, 3)
X 2 3.000000
13
>>>

如果你想通过自定义类型传递参数给函数，必须实现 from_param() 类方法，才能够将此自定义类型用于 argtypes 序列。from_param() 类方法接受一个 Python 对象作为函数输入，它应该进行类型检查或者其他必要的操作以保证接收到的对象是合法的，然后返回这个对象，或者它的 _as_parameter_ 属性，或者其他你想要传递给 C 函数的参数。这里也一样，返回的结果必须是整型、字符串、二进制字符串、 ctypes 类型，或者一个具有 _as_parameter_ 属性的对象。

16.16.1.8. 返回类型

默认情况下都会假定函数返回C int 类型。其他返回类型可以通过设置函数对象的 restype 属性来指定。

这是个更高级的例子，它调用了 strchr 函数，这个函数接收一个字符串指针以及一个字符作为参数，返回另一个字符串指针。

>>> strchr = libc.strchr
>>> strchr(b"abcdef", ord("d"))
8059983
>>> strchr.restype = c_char_p    # c_char_p is a pointer to a string
>>> strchr(b"abcdef", ord("d"))
b'def'
>>> print(strchr(b"abcdef", ord("x")))
None
>>>

如果希望避免上述的 ord("x") 调用，可以设置 argtypes  属性，第二个参数就会将单字符的 Python 二进制字符对象转换为 C 字符:

>>> strchr.restype = c_char_p
>>> strchr.argtypes = [c_char_p, c_char]
>>> strchr(b"abcdef", b"d")
'def'
>>> strchr(b"abcdef", b"def")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ArgumentError: argument 2: exceptions.TypeError: one character string expected
>>> print(strchr(b"abcdef", b"x"))
None
>>> strchr(b"abcdef", b"d")
'def'
>>>

如果外部函数返回了一个整数，你也可以使用要给可调用的 Python 对象（比如函数或者类）作为 restype 属性的值。将会以 C 函数返回的 整数 对象作为参数调用这个可调用对象，执行后的结果作为最终函数返回值。这在错误返回值校验和自动抛出异常等方面比较有用。

>>> GetModuleHandle = windll.kernel32.GetModuleHandleA
>>> def ValidHandle(value):
...     if value == 0:
...         raise WinError()
...     return value
...
>>>
>>> GetModuleHandle.restype = ValidHandle
>>> GetModuleHandle(None)
486539264
>>> GetModuleHandle("something silly")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in ValidHandle
OSError: [Errno 126] The specified module could not be found.
>>>

WinError 函数可以调用 Windows 的 FormatMessage() API 获取错误码的字符串说明，然后 返回 一个异常。 WinError 接收一个可选的错误码作为参数，如果没有的话，它将调用 GetLastError() 获取错误码。

请注意，使用 errcheck 属性可以实现更强大的错误检查手段；详情请见参考手册。

16.16.1.9. 传递指针(或者传递引用)

有时候 C 函数接口可能由于要往某个地址写入值，或者数据太大不适合作为值传递，从而希望接收一个 指针 作为数据参数类型。这和 传递参数引用 类似。

ctypes 暴露了 byref() 函数用于通过引用传递参数，使用 pointer() 函数也能达到同样的效果，只不过 pointer() 需要更多步骤，因为它要先构造一个真实指针对象。所以在 Python 代码本身不需要使用这个指针对象的情况下，使用 byref() 效率更高。

>>> i = c_int()
>>> f = c_float()
>>> s = create_string_buffer(b'\000' * 32)
>>> print(i.value, f.value, repr(s.value))
0 0.0 b''
>>> libc.sscanf(b"1 3.14 Hello", b"%d %f %s",
...             byref(i), byref(f), s)
3
>>> print(i.value, f.value, repr(s.value))
1 3.1400001049 b'Hello'
>>>

16.16.1.10. 结构体和联合

结构体和联合必须继承自 ctypes 模块中的 Structure 和 Union 。子类必须定义 _fields_ 属性。 _fields_ 是一个二元组列表，二元组中包含 field name 和 field type 。

type 字段必须是一个 ctypes 类型，比如 c_int，或者其他 ctypes 类型: 结构体、联合、数组、指针。

这是一个简单的 POINT 结构体，它包含名称为 x 和 y 的两个变量，还展示了如何通过构造函数初始化结构体。

>>> from ctypes import *
>>> class POINT(Structure):
...     _fields_ = [("x", c_int),
...                 ("y", c_int)]
...
>>> point = POINT(10, 20)
>>> print(point.x, point.y)
10 20
>>> point = POINT(y=5)
>>> print(point.x, point.y)
0 5
>>> POINT(1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many initializers
>>>

当然，你可以构造更复杂的结构体。一个结构体可以通过设置 type 字段包含其他结构体或者自身。

这是以一个 RECT 结构体，他包含了两个 POINT ，分别叫 upperleft 和 lowerright:

>>> class RECT(Structure):
...     _fields_ = [("upperleft", POINT),
...                 ("lowerright", POINT)]
...
>>> rc = RECT(point)
>>> print(rc.upperleft.x, rc.upperleft.y)
0 5
>>> print(rc.lowerright.x, rc.lowerright.y)
0 0
>>>

嵌套结构体可以通过几种方式构造初始化:

>>> r = RECT(POINT(1, 2), POINT(3, 4))
>>> r = RECT((1, 2), (3, 4))

可以通过 类 获取字段 descriptor ，它能提供很多有用的调试信息。

>>> print(POINT.x)
<Field type=c_long, ofs=0, size=4>
>>> print(POINT.y)
<Field type=c_long, ofs=4, size=4>
>>>

警告

ctypes 不支持带位域的结构体、联合以值的方式传给函数。这可能在 32 位 x86 平台上可以正常工作，但是对于一般情况，这种行为是未定义的。带位域的结构体、联合应该总是通过指针传递给函数。

16.16.1.11. 结构体/联合 字段对齐及字节顺序

By default, Structure and Union fields are aligned in the same way the C compiler does it. It is possible to override this behavior be specifying a _pack_ class attribute in the subclass definition. This must be set to a positive integer and specifies the maximum alignment for the fields. This is what #pragma pack(n) also does in MSVC.

ctypes 中的结构体和联合使用的是本地字节序。要使用非本地字节序，可以使用 BigEndianStructure, LittleEndianStructure, BigEndianUnion, and LittleEndianUnion 作为基类。这些类不能包含指针字段。

16.16.1.12. 结构体和联合中的位域

结构体和联合中是可以包含位域字段的。位域只能用于整型字段，位长度通过 _fields_ 中的第三个参数指定:

>>> class Int(Structure):
...     _fields_ = [("first_16", c_int, 16),
...                 ("second_16", c_int, 16)]
...
>>> print(Int.first_16)
<Field type=c_long, ofs=0:0, bits=16>
>>> print(Int.second_16)
<Field type=c_long, ofs=0:16, bits=16>
>>>

16.16.1.13. 数组

数组是一个序列，包含指定个数元素，且必须类型相同。

创建数组类型的推荐方式是使用一个类型乘以一个正数:

TenPointsArrayType = POINT * 10

下面是一个构造的数据案例，结构体中包含了4个 POINT 和一些其他东西。

>>> from ctypes import *
>>> class POINT(Structure):
...     _fields_ = ("x", c_int), ("y", c_int)
...
>>> class MyStruct(Structure):
...     _fields_ = [("a", c_int),
...                 ("b", c_float),
...                 ("point_array", POINT * 4)]
>>>
>>> print(len(MyStruct().point_array))
4
>>>

和平常一样，通过调用它创建实例:

arr = TenPointsArrayType()
for pt in arr:
    print(pt.x, pt.y)

以上代码会打印几行 0 0 ，因为数组内容被初始化为 0.

也能通过指定正确类型的数据来初始化:

>>> from ctypes import *
>>> TenIntegers = c_int * 10
>>> ii = TenIntegers(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>>> print(ii)
<c_long_Array_10 object at 0x...>
>>> for i in ii: print(i, end=" ")
...
1 2 3 4 5 6 7 8 9 10
>>>

16.16.1.14. 指针

指针可以通过 ctypes 中的 pointer() 函数进行创建:

>>> from ctypes import *
>>> i = c_int(42)
>>> pi = pointer(i)
>>>

指针实例拥有 contents 属性，它存储了指针指向的真实对象，如上面的 i 对象:

>>> pi.contents
c_long(42)
>>>

注意 ctypes 并没有 OOR （返回原始对象）, 每次访问这个属性时都会构造返回一个新的相同对象:

>>> pi.contents is i
False
>>> pi.contents is pi.contents
False
>>>

将这个指针的 contents 属性赋值为另一个 c_int 实例将会导致该指针指向该实例的内存地址:

>>> i = c_int(99)
>>> pi.contents = i
>>> pi.contents
c_long(99)
>>>

指针对象也可以通过整数下标进行访问:

>>> pi[0]
99
>>>

通过整数下标赋值可以改变内容。

>>> print(i)
c_long(99)
>>> pi[0] = 22
>>> print(i)
c_long(22)
>>>

使用0以外的索引也是合法的，但是你必须确保这么做的后果，就像 C 语言中: 你可以访问或者修改任意内存内容。通常只会在函数接收指针是才会使用这种特性，而且你 知道 这个指针指向的是一个数组而不是单个值。

内部细节, pointer() 函数不只是创建了一个指针实例，它首先创建了一个指针 类型 。这是通过调用 POINTER() 函数实现的，它接收 ctypes 类型为参数，返回一个新的类型:

>>> PI = POINTER(c_int)
>>> PI
<class 'ctypes.LP_c_long'>
>>> PI(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: expected c_long instead of int
>>> PI(c_int(42))
<ctypes.LP_c_long object at 0x...>
>>>

无参调用指针类型可以创建一个 NULL 指针。 NULL 指针的布尔值是 False

>>> null_ptr = POINTER(c_int)()
>>> print(bool(null_ptr))
False
>>>

解引用指针的时候， ctypes 会帮你检测是否指针为 NULL (但是解引用无效的 非 NULL 指针仍会导致 Python 崩溃):

>>> null_ptr[0]
Traceback (most recent call last):
    ....
ValueError: NULL pointer access
>>>

>>> null_ptr[0] = 1234
Traceback (most recent call last):
    ....
ValueError: NULL pointer access
>>>

16.16.1.15. 类型强制转换

通常情况下, ctypes 具有严格的类型检查。这代表着, 如果在函数 argtypes 中或者结构体定义成员中有 POINTER(c_int) 类型，只有相同类型的实例才会被接受。 也有一些例外。比如，你可以传递兼容的数组实例给指针类型。所以，对于 POINTER(c_int) ，ctypes 也可以接受 c_int 类型的数组:

>>> class Bar(Structure):
...     _fields_ = [("count", c_int), ("values", POINTER(c_int))]
...
>>> bar = Bar()
>>> bar.values = (c_int * 3)(1, 2, 3)
>>> bar.count = 3
>>> for i in range(bar.count):
...     print(bar.values[i])
...
1
2
3
>>>

另外，如果一个函数 argtypes 列表中的参数显式的定义为指针类型(如 POINTER(c_int) )，指针所指向的 类型 (这个例子中是 c_int )也可以传递给函数。ctypes 会自动调用对应的 byref() 转换。

可以给指针内容赋值为 None 将其设置为 Null

>>> bar.values = None
>>>

有时候你拥有一个不完整的类型。在 C 中，你可以将一个类型强制转换为另一个。 ctypes  中的 a cast() 函数提供了相同的功能。上面的结构体 Bar 的 value 字段接收 POINTER(c_int) 指针或者 c_int  数组，但是不能接受其他类型的实例:

>>> bar.values = (c_byte * 4)()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: incompatible types, c_byte_Array_4 instance instead of LP_c_long instance
>>>

这种情况下, 需要手动使用 cast() 函数。

cast() 函数可以将一个指针实例强制转换为另一种 ctypes 类型。 cast()  接收两个参数，一个 ctypes 指针对象或者可以被转换为指针的其他类型对象，和一个 ctypes 指针类型。返回第二个类型的一个实例，该返回实例和第一个参数指向同一片内存空间:

>>> a = (c_byte * 4)()
>>> cast(a, POINTER(c_int))
<ctypes.LP_c_long object at ...>
>>>

所以 cast() 可以用来给结构体 Bar 的 values 字段赋值:

>>> bar = Bar()
>>> bar.values = cast((c_byte * 4)(), POINTER(c_int))
>>> print(bar.values[0])
0
>>>

16.16.1.16. 不完整类型

不完整类型 即还没有定义成员的结构体、联合或者数组。在 C 中，它们通常用于前置声明，然后在后面定义:

struct cell; /* forward declaration */

struct cell {
    char *name;
    struct cell *next;
};

直接翻译成 ctypes 的代码如下，但是这行不通:

>>> class cell(Structure):
...     _fields_ = [("name", c_char_p),
...                 ("next", POINTER(cell))]
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in cell
NameError: name 'cell' is not defined
>>>

因为新的 cell 类 在 class 语句结束之前还没有完成定义。在 ctypes 中，我们可以先定义 cell 类，在 class 语句结束之后再设置 _fields_ 属性:

>>> from ctypes import *
>>> class cell(Structure):
...     pass
...
>>> cell._fields_ = [("name", c_char_p),
...                  ("next", POINTER(cell))]
>>>

Lets try it. We create two instances of cell, and let them point to each other, and finally follow the pointer chain a few times:

>>> c1 = cell()
>>> c1.name = "foo"
>>> c2 = cell()
>>> c2.name = "bar"
>>> c1.next = pointer(c2)
>>> c2.next = pointer(c1)
>>> p = c1
>>> for i in range(8):
...     print(p.name, end=" ")
...     p = p.next[0]
...
foo bar foo bar foo bar foo bar
>>>

16.16.1.17. 回调函数

ctypes 允许创建一个指向 Python 可调用对象的 C 函数。它们有时候被称为 回调函数 。

首先，你必须为回调函数创建一个类，这个类知道调用约定，包括返回值类型以及函数接收的参数类型及个数。

CFUNCTYPE() 工厂函数使用 cdecl 调用约定创建回调函数类型。在 Windows 上， WINFUNCTYPE() 工厂函数使用 stdcall 调用约定为回调函数创建类型。

这些工厂函数都是用返回值类型作为第一个参数，回掉函数的参数类型作为剩余参数。

这里展示一个使用 C 标准库函数 qsort()  的例子，它使用一个回掉函数对数据进行排序。 qsort() 将用来给整数数组排序:

>>> IntArray5 = c_int * 5
>>> ia = IntArray5(5, 1, 7, 33, 99)
>>> qsort = libc.qsort
>>> qsort.restype = None
>>>

qsort() 必须接收的参数，一个指向待排序数据的指针，元素个数，每个元素的大小，以及一个指向排序函数的指针，即回调函数。然后回调函数接收两个元素的指针，如果第一个元素小于第二个，则返回一个负整数，如果相等则返回0，否则返回一个正整数。

所以，我们的回调函数要接收两个整数指针，返回一个整数。首先我们创建回调函数的 类型

>>> CMPFUNC = CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))
>>>

首先，这是一个简单的回调，它会显示传入的值:

>>> def py_cmp_func(a, b):
...     print("py_cmp_func", a[0], b[0])
...     return 0
...
>>> cmp_func = CMPFUNC(py_cmp_func)
>>>

结果:

>>> qsort(ia, len(ia), sizeof(c_int), cmp_func)
py_cmp_func 5 1
py_cmp_func 33 99
py_cmp_func 7 33
py_cmp_func 5 7
py_cmp_func 1 7
>>>

现在我们可以比较两个元素并返回有用的结果了:

>>> def py_cmp_func(a, b):
...     print("py_cmp_func", a[0], b[0])
...     return a[0] - b[0]
...
>>>
>>> qsort(ia, len(ia), sizeof(c_int), CMPFUNC(py_cmp_func))
py_cmp_func 5 1
py_cmp_func 33 99
py_cmp_func 7 33
py_cmp_func 1 7
py_cmp_func 5 7
>>>

我们可以轻易地验证，现在数组是有序的了:

>>> for i in ia: print(i, end=" ")
...
1 5 7 33 99
>>>

这些工厂函数可以当作装饰器工厂，所以可以这样写:

>>> @CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))
... def py_cmp_func(a, b):
...     print("py_cmp_func", a[0], b[0])
...     return a[0] - b[0]
...
>>> qsort(ia, len(ia), sizeof(c_int), py_cmp_func)
py_cmp_func 5 1
py_cmp_func 33 99
py_cmp_func 7 33
py_cmp_func 1 7
py_cmp_func 5 7
>>>

注解

请确保你维持 CFUNCTYPE() 对象的引用与它们在 C 代码中的使用期一样长。 ctypes 不会确保这一点，而如果你不这样做，它们可能会被垃圾回收，导致你的程序在执行回调函数时发生崩溃。

注意，如果回调函数在Python之外的另外一个线程使用(比如，外部代码调用这个回调函数)， ctypes 会在每一次调用上创建一个虚拟 Python 线程。这个行为在大多数情况下是合理的，但也意味着如果有数据使用 threading.local 方式存储，将无法访问，就算它们是在同一个 C 线程中调用的 。

16.16.1.18. 访问 dll 中导出的值

一些动态链接库不仅仅导出函数，也会导出变量。一个例子就是 Python 库本身的 Py_OptimizeFlag ，根据启动选项 -O 、 -OO 的不同，它是值可能为 0、1、2 的整型。

ctypes 可以通过 in_dll() 类方法访问这类变量 。 pythonapi 是用于访问 Python C 接口的预定义符号:

>>> opt_flag = c_int.in_dll(pythonapi, "Py_OptimizeFlag")
>>> print(opt_flag)
c_long(0)
>>>

如果解释器使用 -O 选项启动，这个例子会打印 c_long(1) ， 如果使用 -OO 启动，则会打印 c_long(2) 。

一个扩展例子, 同时也展示了使用指针访问 Python 导出的 PyImport_FrozenModules 指针对象。

对文档中这个值的解释说明

This pointer is initialized to point to an array of struct _frozen records, terminated by one whose members are all NULL or zero. When a frozen module is imported, it is searched in this table. Third-party code could play tricks with this to provide a dynamically created collection of frozen modules.

这足以证明修改这个指针是很有用的。为了让实例大小不至于太长，这里只展示如何使用 ctypes 读取这个表:

>>> from ctypes import *
>>>
>>> class struct_frozen(Structure):
...     _fields_ = [("name", c_char_p),
...                 ("code", POINTER(c_ubyte)),
...                 ("size", c_int)]
...
>>>

我们定义了 struct _frozen  数据类型，接着就可以获取这张表的指针了:

>>> FrozenTable = POINTER(struct_frozen)
>>> table = FrozenTable.in_dll(pythonapi, "PyImport_FrozenModules")
>>>

Since table is a pointer to the array of struct_frozen records, we can iterate over it, but we just have to make sure that our loop terminates, because pointers have no size. Sooner or later it would probably crash with an access violation or whatever, so it’s better to break out of the loop when we hit the NULL entry:

>>> for item in table:
...     if item.name is None:
...         break
...     print(item.name.decode("ascii"), item.size)
...
_frozen_importlib 31764
_frozen_importlib_external 41499
__hello__ 161
__phello__ -161
__phello__.spam 161
>>>

The fact that standard Python has a frozen module and a frozen package (indicated by the negative size member) is not well known, it is only used for testing. Try it out with import __hello__ for example.

16.16.1.19. 意外

ctypes 也有自己的边界，有时候会发生一些意想不到的事情。

比如下面的例子:

>>> from ctypes import *
>>> class POINT(Structure):
...     _fields_ = ("x", c_int), ("y", c_int)
...
>>> class RECT(Structure):
...     _fields_ = ("a", POINT), ("b", POINT)
...
>>> p1 = POINT(1, 2)
>>> p2 = POINT(3, 4)
>>> rc = RECT(p1, p2)
>>> print(rc.a.x, rc.a.y, rc.b.x, rc.b.y)
1 2 3 4
>>> # now swap the two points
>>> rc.a, rc.b = rc.b, rc.a
>>> print(rc.a.x, rc.a.y, rc.b.x, rc.b.y)
3 4 3 4
>>>

嗯。我们预想应该打印 3 4 1 2 。但是为什么呢? 这是 rc.a, rc.b = rc.b, rc.a 这行代码展开后的步骤:

>>> temp0, temp1 = rc.b, rc.a
>>> rc.a = temp0
>>> rc.b = temp1
>>>

注意 temp0 和 temp1 对象始终引用了对象 rc 的内容。然后执行 rc.a = temp0 会把 temp0 的内容拷贝到 rc 的空间。这也改变了 temp1 的内容。最终导致赋值语句 rc.b = temp1 没有产生预想的效果。

记住，访问被包含在结构体、联合、数组中的对象并不会将其 复制 出来，而是得到了一个代理对象，它是对根对象的内部内容进行了一层包装。

Another example that may behave different from what one would expect is this:

>>> s = c_char_p()
>>> s.value = "abc def ghi"
>>> s.value
'abc def ghi'
>>> s.value is s.value
False
>>>

为什么这里打印了 False ？ ctypes 实例是一些内存块加上一些用于访问这些内存块的 descriptor 组成。将 Python 对象存储在内存块并不会存储对象本身，而是存储了对象的 内容 。每次访问对象的内容都会构造一个新的 Python 对象。

16.16.1.20. 变长数据类型

ctypes 对变长数组和结构体提供了一些支持 。

The resize() function can be used to resize the memory buffer of an existing ctypes object. The function takes the object as first argument, and the requested size in bytes as the second argument. The memory block cannot be made smaller than the natural memory block specified by the objects type, a ValueError is raised if this is tried:

>>> short_array = (c_short * 4)()
>>> print(sizeof(short_array))
8
>>> resize(short_array, 4)
Traceback (most recent call last):
    ...
ValueError: minimum size is 8
>>> resize(short_array, 32)
>>> sizeof(short_array)
32
>>> sizeof(type(short_array))
8
>>>

这非常好，但是要怎么访问数组中额外的元素呢？因为数组类型已经定义包含4个元素，women访问新增元素会产生以下错误:

>>> short_array[:]
[0, 0, 0, 0]
>>> short_array[7]
Traceback (most recent call last):
    ...
IndexError: invalid index
>>>

使用 ctypes 访问变长数据类型的一个可行方法是利用 Python 的动态特性，根据具体情况，在知道这个数据的大小后，(重新)指定这个数据的类型。

16.16.2. ctypes 参考手册

16.16.2.1. 寻找动态链接库

在编译型语言中，动态链接库会在编译、链接或者程序运行时访问。

The purpose of the find_library() function is to locate a library in a way similar to what the compiler or runtime loader does (on platforms with several versions of a shared library the most recent should be loaded), while the ctypes library loaders act like when a program is run, and call the runtime loader directly.

ctypes.util 模块提供了一个函数，可以帮助确定需要加载的库。

ctypes.util.find_library(name)

尝试寻找一个库然后返回其路径名， name 是库名称, 且去除了 lib 等前缀和 .so 、 .dylib 、版本号等后缀(这是 posix 连接器 -l 选项使用的格式)。如果没有找到对应的库，则返回 None 。

确切的功能取决于系统。

在 Linux 上， find_library() 会尝试运行外部程序(/sbin/ldconfig, gcc, objdump 以及 ld) 来寻找库文件。返回库文件的文件名。

在 3.6 版更改: 在Linux 上，如果其他方式找不到的话，会使用环境变量 LD_LIBRARY_PATH 搜索动态链接库。

这是一些例子:

>>> from ctypes.util import find_library
>>> find_library("m")
'libm.so.6'
>>> find_library("c")
'libc.so.6'
>>> find_library("bz2")
'libbz2.so.1.0'
>>>

在 OS X 上， find_library() 会尝试几种预定义的命名方案和路径来查找库，如果成功，则返回完整的路径名:

>>> from ctypes.util import find_library
>>> find_library("c")
'/usr/lib/libc.dylib'
>>> find_library("m")
'/usr/lib/libm.dylib'
>>> find_library("bz2")
'/usr/lib/libbz2.dylib'
>>> find_library("AGL")
'/System/Library/Frameworks/AGL.framework/AGL'
>>>

在 Windows 上， find_library() 在系统路径中搜索，然后返回全路径，但是如果没有预定义的命名方案， find_library("c") 调用会返回 None

使用 ctypes 包装动态链接库，更好的方式 可能 是在开发的时候就确定名称，然后硬编码到包装模块中去，而不是在运行时使用 find_library() 寻找库。

16.16.2.2. 加载动态链接库

有很多方式可以将动态链接库加载到 Python 进程。其中之一是实例化以下类的其中一个:

class ctypes.CDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False)

此类的实例即已加载的动态链接库。库中的函数使用标准 C 调用约定，并假定返回 int 。

class ctypes.OleDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False)

仅 Windows : 此类的实例即加载好的动态链接库，其中的函数使用 stdcall 调用约定，并且假定返回 windows 指定的 HRESULT 返回码。 HRESULT 的值包含的信息说明函数调用成功还是失败，以及额外错误码。 如果返回值表示失败，会自动抛出 OSError 异常。

在 3.3 版更改: 以前是引发 WindowsError。

class ctypes.WinDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False)

仅 Windows: 此类的实例即加载好的动态链接库，其中的函数使用 stdcall 调用约定，并假定默认返回 int 。

在 Windows CE 上，只能使用 stdcall 调用约定，为了方便， WinDLL 和 OleDLL 在这个平台上都使用标准调用约定。

调用动态库导出的函数之前，Python会释放 global interpreter lock ，并在调用后重新获取。

class ctypes.PyDLL(name, mode=DEFAULT_MODE, handle=None)

这个类实例的行为与 CDLL 类似，只不过 不会 在调用函数的时候释放 GIL 锁，且调用结束后会检查 Python 错误码。 如果错误码被设置，会抛出一个 Python 异常。

所以，它只在直接调用 Python C 接口函数的时候有用。

通过使用至少一个参数（共享库的路径名）调用它们，可以实例化所有这些类。也可以传入一个已加载的动态链接库作为 handler 参数，其他情况会调用系统底层的 dlopen 或 LoadLibrary 函数将库加载到进程，并获取其句柄。

mode 可以指定库加载方式。详情请参见 dlopen(3) 手册页。 在 Windows 上， 会忽略 mode ，在 posix 系统上， 总是会加上 RTLD_NOW ，且无法配置。

use_errno 参数如果设置为 true，可以启用ctypes的机制，通过一种安全的方法获取系统的 errno 错误码。 ctypes 维护了一个线程局部变量，它是系统 errno 的一份拷贝；如果调用了使用 use_errno=True 创建的外部函数， errno 的值会与 ctypes 自己拷贝的那一份进行交换，函数执行完后立即再交换一次。

The function ctypes.get_errno() returns the value of the ctypes private copy, and the function ctypes.set_errno() changes the ctypes private copy to a new value and returns the former value.

use_last_error 参数如果设置为 true，可以在 Windows 上启用相同的策略，它是通过 Windows API 函数 GetLastError()  和 SetLastError() 管理的。 ctypes.get_last_error() 和 ctypes.set_last_error() 可用于获取和设置 ctypes 自己维护的 windows 错误码拷贝。

ctypes.RTLD_GLOBAL

用于 mode 参数的标识值。在此标识不可用的系统上，它被定义为整数0。

ctypes.RTLD_LOCAL

Flag to use as mode parameter. On platforms where this is not available, it is the same as RTLD_GLOBAL.

ctypes.DEFAULT_MODE

加载动态链接库的默认模式。在 OSX 10.3 上，它是 RTLD_GLOBAL ，其余系统上是 RTLD_LOCAL 。

Instances of these classes have no public methods. Functions exported by the shared library can be accessed as attributes or by index. Please note that accessing the function through an attribute caches the result and therefore accessing it repeatedly returns the same object each time. On the other hand, accessing it through an index returns a new object each time:

>>> libc.time == libc.time
True
>>> libc['time'] == libc['time']
False

还有下面这些属性可用，他们的名称以下划线开头，以避免和导出函数重名:

PyDLL._handle

用于访问库的系统句柄。

PyDLL._name

传入构造函数的库名称。

共享库也可以通用使用一个预制对象来加载，这种对象是 LibraryLoader 类的实例，具体做法或是通过调用 LoadLibrary() 方法，或是通过将库作为加载实例的属性来提取。

class ctypes.LibraryLoader(dlltype)

加载共享库的类。 dlltype 应当为 CDLL, PyDLL, WinDLL 或 OleDLL 类型之一。

__getattr__() 具有特殊的行为：它允许通过将一个共享库作为库加载器实例的属性进行访问来加载它。 加载结果将被缓存，因此重复的属性访问每次都会返回相同的库。

LoadLibrary(name)

加载一个共享库到进程中并将其返回。 此方法总是返回一个新的库实例。

可用的预制库加载器有如下这些:

ctypes.cdll

创建 CDLL 实例。

ctypes.windll

仅Windows中: 创建 WinDLL 实例.

ctypes.oledll

仅Windows中: 创建 OleDLL 实例。

ctypes.pydll

创建 PyDLL 实例。

要直接访问 C Python api，可以使用一个现成的 Python 共享库对象:

ctypes.pythonapi

一个 PyDLL 的实例，它将 Python C API 函数作为属性公开。 请注意所有这些函数都应返回 C int，当然这也不是绝对的，因此你必须分配正确的 restype 属性以使用这些函数。

16.16.2.3. 外部函数

正如之前小节的说明，外部函数可作为被加载共享库的属性来访问。 用此方式创建的函数对象默认接受任意数量的参数，接受任意 ctypes 数据实例作为参数，并且返回库加载器所指定的默认结果类型。 它们是一个私有类的实例:

class ctypes._FuncPtr

C 可调用外部函数的基类。

外部函数的实例也是兼容 C 的数据类型；它们代表 C 函数指针。

此行为可通过对外部函数对象的特殊属性赋值来自定义。

restype

赋值为一个 ctypes 类型来指定外部函数的结果类型。 使用 None 表示 void，即不返回任何结果的函数。

赋值为一个不为 ctypes 类型的可调用 Python 对象也是可以的，在此情况下函数应返回 C int，该可调用对象将附带此整数被调用，以允许进一步的处理或错误检测。 这种用法已被弃用，为了更灵活的后续处理或错误检测请使用一个 ctypes 数据类型作为 restype 并将 errcheck 属性赋值为一个可调用对象。

argtypes

赋值为一个 ctypes 类型的元组来指定函数所接受的参数类型。 使用 stdcall 调用规范的函数只能附带与此元组长度相同数量的参数进行调用；使用 C 调用规范的函数还可接受额外的未指明参数。

当外部函数被调用时，每个实际参数都会被传给 argtypes 元组中条目的 from_param() 类方法，此方法允许将实际参数适配为此外部函数所接受的对象。 例如，argtypes 元组中的 c_char_p 条目将使用 ctypes 约定规则把作为参数传入的字符串转换为字节串对象。

新增：现在可以将不是 ctypes 类型的条目放入 argtypes，但每个条目都必须具有 from_param() 方法用于返回可作为参数的值（整数、字符串、ctypes 实例）。 这样就允许定义可将自定义对象适配为函数形参的适配器。

errcheck

将一个 Python 函数或其他可调用对象赋值给此属性。 该可调用对象将附带三个及以上的参数被调用。

callable(result, func, arguments)

result 是外部函数返回的结果，由 restype 属性指明。

func 是外部函数对象本身，这样就允许重新使用相同的可调用对象来对多个函数进行检查或后续处理。

arguments 是一个包含最初传递给函数调用的形参的元组，这样就允许对所用参数的行为进行特别处理。

此函数所返回的对象将会由外部函数调用返回，但它还可以在外部函数调用失败时检查结果并引发异常。

exception ctypes.ArgumentError

此异常会在外部函数无法对某个传入参数执行转换时被引发。

16.16.2.4. 函数原型

外部函数也可通过实例化函数原型来创建。 函数原型类似于 C 中的函数原型；它们在不定义具体实现的情况下描述了一个函数（返回类型、参数类型、调用约定）。 工厂函数必须使用函数所需要的结果类型和参数类型来调用，并可被用作装饰器工厂函数，在此情况下可以通过 @wrapper 语法应用于函数。 请参阅 回调函数 了解有关示例。

ctypes.CFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)

返回的函数原型会创建使用标准 C 调用约定的函数。 该函数在调用过程中将释放 GIL。 如果 use_errno 设为真值，则在调用之前和之后系统 errno 变量的 ctypes 私有副本会与真正的 errno 值进行交换；use_last_error 会为 Windows 错误码执行同样的操作。

ctypes.WINFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)

仅限 Windows only：返回的函数原型会创建使用 stdcall 调用约定的函数，但在 Windows CE 上 WINFUNCTYPE() 则会与 CFUNCTYPE() 相同。 该函数在调用过程中将释放 GIL。 use_errno 和 use_last_error 具有与前面相同的含义。

ctypes.PYFUNCTYPE(restype, *argtypes)

返回的函数原型会创建使用 Python 调用约定的函数。 该函数在调用过程中将 不会 释放 GIL。

这些工厂函数所创建的函数原型可通过不同的方式来实例化，具体取决于调用中的类型与数量:

prototype(address)

在指定地址上返回一个外部函数，地址值必须为整数。

prototype(callable)

基于 Python callable 创建一个 C 可调用函数（回调函数）。

prototype(func_spec[, paramflags])

返回由一个共享库导出的外部函数。 func_spec 必须为一个 2 元组 (name_or_ordinal, library)。 第一项是字符串形式的所导出函数名称，或小整数形式的所导出函数序号。 第二项是该共享库实例。

prototype(vtbl_index, name[, paramflags[, iid]])

返回将调用一个 COM 方法的外部函数。 vtbl_index 虚拟函数表中的索引。 name 是 COM 方法的名称。 iid 是可选的指向接口标识符的指针，它被用于扩展的错误报告。

COM 方法使用特殊的调用约定：除了在 argtypes 元组中指定的形参，它们还要求一个指向 COM 接口的指针作为第一个参数。

可选的 paramflags 形参会创建相比上述特性具有更多功能的外部函数包装器。

paramflags 必须为一个与 argtypes 长度相同的元组。

此元组中的每一项都包含有关形参的更多信息，它必须为包含一个、两个或更多条目的元组。

第一项是包含形参指令旗标组合的整数。

1

指定函数的一个输入形参。

2

输出形参。 外部函数会填入一个值。

4

默认为整数零值的输入形参。

可选的第二项是字符串形式的形参名称。 如果指定此项，则可以使用该形参名称来调用外部函数。

可选的第三项是该形参的默认值。

这个例子演示了如何包装 Windows 的 MessageBoxW 函数以使其支持默认形参和已命名参数。 相应 windows 头文件的 C 声明是这样的:

WINUSERAPI int WINAPI
MessageBoxW(
    HWND hWnd,
    LPCWSTR lpText,
    LPCWSTR lpCaption,
    UINT uType);

这是使用 ctypes 的包装:

>>> from ctypes import c_int, WINFUNCTYPE, windll
>>> from ctypes.wintypes import HWND, LPCWSTR, UINT
>>> prototype = WINFUNCTYPE(c_int, HWND, LPCWSTR, LPCWSTR, UINT)
>>> paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption", "Hello from ctypes"), (1, "flags", 0)
>>> MessageBox = prototype(("MessageBoxW", windll.user32), paramflags)

现在 MessageBox 外部函数可以通过以下方式来调用:

>>> MessageBox()
>>> MessageBox(text="Spam, spam, spam")
>>> MessageBox(flags=2, text="foo bar")

第二个例子演示了输出形参。 这个 win32 GetWindowRect 函数通过将指定窗口的维度拷贝至调用者必须提供的 RECT 结构体来提取这些值。 这是相应的 C 声明:

WINUSERAPI BOOL WINAPI
GetWindowRect(
     HWND hWnd,
     LPRECT lpRect);

这是使用 ctypes 的包装:

>>> from ctypes import POINTER, WINFUNCTYPE, windll, WinError
>>> from ctypes.wintypes import BOOL, HWND, RECT
>>> prototype = WINFUNCTYPE(BOOL, HWND, POINTER(RECT))
>>> paramflags = (1, "hwnd"), (2, "lprect")
>>> GetWindowRect = prototype(("GetWindowRect", windll.user32), paramflags)
>>>

带有输出形参的函数如果输出形参存在单一值则会自动返回该值，或是当输出形参存在多个值时返回包含这些值的元组，因此当 GetWindowRect 被调用时现在将返回一个 RECT 实例。

输出形参可以与 errcheck 协议相结合以执行进一步的输出处理与错误检查。 Win32 GetWindowRect API 函数返回一个 BOOL 来表示成功或失败，因此此函数可执行错误检查，并在 API 调用失败时引发异常:

>>> def errcheck(result, func, args):
...     if not result:
...         raise WinError()
...     return args
...
>>> GetWindowRect.errcheck = errcheck
>>>

如果 errcheck 不加更改地返回它所接收的参数元组，则 ctypes 会继续对输出形参执行常规处理。 如果你希望返回一个窗口坐标的元组而非 RECT 实例，你可以从函数中提取这些字段并返回它们，常规处理将不会再执行:

>>> def errcheck(result, func, args):
...     if not result:
...         raise WinError()
...     rc = args[1]
...     return rc.left, rc.top, rc.bottom, rc.right
...
>>> GetWindowRect.errcheck = errcheck
>>>

16.16.2.5. 工具函数

ctypes.addressof(obj)

以整数形式返回内存缓冲区地址。 obj 必须为一个 ctypes 类型的实例。

ctypes.alignment(obj_or_type)

返回一个 ctypes 类型的对齐要求。 obj_or_type 必须为一个 ctypes 类型或实例。

ctypes.byref(obj[, offset])

返回指向 obj 的轻量指针，该对象必须为一个 ctypes 类型的实例。 offset 默认值为零，且必须为一个将被添加到内部指针值的整数。

byref(obj, offset) 对应于这段 C 代码:

(((char *)&obj) + offset)

返回的对象只能被用作外部函数调用形参。 它的行为类似于 pointer(obj)，但构造起来要快很多。

ctypes.cast(obj, type)

此函数类似于 C 的强制转换运算符。 它返回一个 type 的新实例，该实例指向与 obj 相同的内存块。 type 必须为指针类型，而 obj 必须为可以被作为指针来解读的对象。

ctypes.create_string_buffer(init_or_size, size=None)

此函数会创建一个可变的字符缓冲区。 返回的对象是一个 c_char 的 ctypes 数组。

init_or_size 必须是一个指明数组大小的整数，或者是一个将被用来初始化数组条目的字节串对象。

如果将一个字节串对象指定为第一个参数，则将使缓冲区大小比其长度多一项以便数组的最后一项为一个 NUL 终结符。 可以传入一个整数作为第二个参数以允许在不使用字节串长度的情况下指定数组大小。

ctypes.create_unicode_buffer(init_or_size, size=None)

此函数会创建一个可变的 unicode 字符缓冲区。 返回的对象是一个 c_wchar 的 ctypes 数组。

init_or_size 必须是一个指明数组大小的整数，或者是一个将被用来初始化数组条目的字符串。

如果将一个字符串指定为第一个参数，则将使缓冲区大小比其长度多一项以便数组的最后一项为一个 NUL 终结符。 可以传入一个整数作为第二个参数以允许在不使用字符串长度的情况下指定数组大小。

ctypes.DllCanUnloadNow()

仅限 Windows：此函数是一个允许使用 ctypes 实现进程内 COM 服务的钩子。 它将由 _ctypes 扩展 dll 所导出的 DllCanUnloadNow 函数来调用。

ctypes.DllGetClassObject()

仅限 Windows：此函数是一个允许使用 ctypes 实现进程内 COM 服务的钩子。 它将由 _ctypes 扩展 dll 所导出的 DllGetClassObject 函数来调用。

ctypes.util.find_library(name)

尝试寻找一个库并返回路径名称。 name 是库名称并且不带任何前缀如 lib 以及后缀如 .so，.dylib 或版本号（形式与 posix 链接器选项 -l 所用的一致）。 如果找不到库，则返回 None。

确切的功能取决于系统。

ctypes.util.find_msvcrt()

仅限 Windows：返回 Python 以及扩展模块所使用的 VC 运行时库的文件名。 如果无法确定库名称，则返回 None。

如果你需要通过调用 free(void *) 来释放内存，例如某个扩展模块所分配的内存，重要的一点是你应当使用分配内存的库中的函数。

ctypes.FormatError([code])

仅限 Windows：返回错误码 code 的文本描述。 如果未指定错误码，则会通过调用 the last error code is used by calling the Windows api 函数 GetLastError 来获得最新的错误码。

ctypes.GetLastError()

仅限 Windows：返回 Windows 在调用线程中设置的最新错误码。 此函数会直接调用 Windows GetLastError() 函数，它并不返回错误码的 ctypes 私有副本。

ctypes.get_errno()

返回调用线程中系统 errno 变量的 ctypes 私有副本的当前值。

ctypes.get_last_error()

仅限 Windows：返回调用线程中系统 LastError 变量的 ctypes 私有副本的当前值。

ctypes.memmove(dst, src, count)

与标准 C memmove 库函数相同：将 count 个字节从 src 拷贝到 dst。 dst 和 src 必须为整数或可被转换为指针的 ctypes 实例。

ctypes.memset(dst, c, count)

与标准 C memset 库函数相同：将位于地址 dst 的内存块用 count 个字节的 c 值填充。 dst 必须为指定地址的整数或 ctypes 实例。

ctypes.POINTER(type)

这个工厂函数创建并返回一个新的 ctypes 指针类型。 指针类型会被缓存并在内部重用，因此重复调用此函数耗费不大。 type 必须为 ctypes 类型。

ctypes.pointer(obj)

此函数会创建一个新的指向 obj 的指针实例。 返回的对象类型为 POINTER(type(obj))。

注意：如果你只是想向外部函数调用传递一个对象指针，你应当使用更为快速的 byref(obj)。

ctypes.resize(obj, size)

此函数可改变 obj 的内部内存缓冲区大小，其参数必须为 ctypes 类型的实例。 没有可能将缓冲区设为小于对象类型的本机大小值，该值由 sizeof(type(obj)) 给出，但将缓冲区加大则是可能的。

ctypes.set_errno(value)

设置调用线程中系统 errno 变量的 ctypes 私有副本的当前值为 value 并返回原来的值。

ctypes.set_last_error(value)

仅限 Windows：设置调用线程中系统 LastError 变量的 ctypes 私有副本的当前值为 value 并返回原来的值。

ctypes.sizeof(obj_or_type)

返回 ctypes 类型或实例的内存缓冲区以字节表示的大小。 其功能与 C sizeof 运算符相同。

ctypes.string_at(address, size=-1)

此函数返回从内存地址 address 开始的以字节串表示的 C 字符串。 如果指定了 size，则将其用作长度，否则将假定字符串以零值结尾。

ctypes.WinError(code=None, descr=None)

仅限 Windows：此函数可能是 ctypes 中名字起得最差的函数。 它会创建一个 OSError 的实例。 如果未指定 code，则会调用 GetLastError 来确定错误码。 如果未指定 descr，则会调用 FormatError() 来获取错误的文本描述。

在 3.3 版更改: 以前是会创建一个 WindowsError 的实例。

ctypes.wstring_at(address, size=-1)

此函数返回从内存地址 address 开始的以字符串表示的宽字节字符串。 如果指定了 size，则将其用作字符串中的字符数量，否则将假定字符串以零值结尾。

16.16.2.6. 数据类型

class ctypes._CData

这个非公有类是所有 ctypes 数据类型的共同基类。 另外，所有 ctypes 类型的实例都包含一个存放 C 兼容数据的内存块；该内存块的地址可由 addressof() 辅助函数返回。 还有一个实例变量被公开为 _objects；此变量包含其他在内存块包含指针的情况下需要保持存活的 Python 对象。

ctypes 数据类型的通用方法，它们都是类方法（严谨地说，它们是 metaclass 的方法）:

from_buffer(source[, offset])

此方法返回一个共享 source 对象缓冲区的 ctypes 实例。 source 对象必须支持可写缓冲区接口。 可选的 offset 形参指定以字节表示的源缓冲区内偏移量；默认值为零。 如果源缓冲区不够大则会引发 ValueError。

from_buffer_copy(source[, offset])

此方法创建一个 ctypes 实例，从 source 对象缓冲区拷贝缓冲区，该对象必须是可读的。 可选的 offset 形参指定以字节表示的源缓冲区内偏移量；默认值为零。 如果源缓冲区不够大则会引发 ValueError。

from_address(address)

此方法会使用 address 所指定的内存返回一个 ctypes 类型的实例，该参数必须为一个整数。

from_param(obj)

此方法会将 obj 适配为一个 ctypes 类型。 它调用时会在当该类型存在于外部函数的 argtypes 元组时传入外部函数调用所使用的实际对象；它必须返回一个可被用作函数调用参数的对象。

所有 ctypes 数据类型都带有这个类方法的默认实现，它通常会返回 obj，如果该对象是此类型的实例的话。 某些类型也能接受其他对象。

in_dll(library, name)

此方法返回一个由共享库导出的 ctypes 类型。 name 为导出数据的符号名称，library 为所加载的共享库。

ctypes 数据类型的通用实例变量:

_b_base_

有时 ctypes 数据实例并不拥有它们所包含的内存块，它们只是共享了某个基对象的部分内存块。 _b_base_ 只读成员是拥有内存块的根 ctypes 对象。

_b_needsfree_

这个只读变量在 ctypes 数据实例自身已分配了内存块时为真值，否则为假值。

_objects

这个成员或者为 None，或者为一个包含需要保持存活以使内存块的内存保持有效的 Python 对象的字典。 这个对象只是出于调试目的而对外公开；绝对不要修改此字典的内容。

16.16.2.7. 基础数据类型

class ctypes._SimpleCData

这个非公有类是所有基本 ctypes 数据类型的基类。 它在这里被提及是因为它包含基本 ctypes 数据类型共有的属性。 _SimpleCData 是 _CData 的子类，因此继承了其方法和属性。 非指针及不包含指针的 ctypes 数据类型现在将可以被封存。

实例拥有一个属性:

value

这个属性包含实例的实际值。 对于整数和指针类型，它是一个整数，对于字符类型，它是一个单字符字符串对象或字符串，对于字符指针类型，它是一个 Python 字节串对象或字符串。

当从 ctypes 实例提取 value 属性时，通常每次会返回一个新的对象。 ctypes 并 没有 实现原始对象返回，它总是会构造一个新的对象。 所有其他 ctypes 对象实例也同样如此。

基本数据类型当作为外部函数调用结果被返回或者作为结构字段成员或数组项被提取时，会透明地转换为原生 Python 类型。 换句话说，如果某个外部函数具有 c_char_p 的 restype，你将总是得到一个 Python 字节串对象，而 不是 一个 c_char_p 实例。

基本数据类型的子类并 没有 继续此行为。 因此，如果一个外部函数的 restype 是 c_void_p 的一个子类，你将从函数调用得到一个该子类的实例。 当然，你可以通过访问 value 属性来获取指针的值。

这些是基本 ctypes 数据类型:

class ctypes.c_byte

代表 C signed char 数据类型，并将值解读为一个小整数。 该构造器接受一个可选的整数初始化器；不会执行溢出检查。

class ctypes.c_char

代表 C char 数据类型，并将值解读为单个字符。 该构造器接受一个可选的字符串初始化器，字符串的长度必须恰好为一个字符。

class ctypes.c_char_p

当指向一个以零为结束符的字符串时代表 C char * 数据类型。 对于通用字符指针来说也可能指向二进制数据，必须要使用 POINTER(c_char)。 该构造器接受一个整数地址，或者一个字节串对象。

class ctypes.c_double

代表 C double 数据类型。 该构造器接受一个可选的浮点数初始化器。

class ctypes.c_longdouble

代表 C long double 数据类型。 该构造器接受一个可选的浮点数初始化器。 在 sizeof(long double) == sizeof(double) 的平台上它是 c_double 的一个别名。

class ctypes.c_float

代表 C float 数据类型。 该构造器接受一个可选的浮点数初始化器。

class ctypes.c_int

代表 C signed int 数据类型。 该构造器接受一个可选的整数初始化器；不会执行溢出检查。 在 sizeof(int) == sizeof(long) 的平台上它是 c_long 的一个别名。

class ctypes.c_int8

代表 C 8 位 signed int 数据类型。 通常是 c_byte 的一个别名。

class ctypes.c_int16

代表 C 16 位 signed int 数据类型。 通常是 c_short 的一个别名。

class ctypes.c_int32

代表 C 32 位 signed int 数据类型。 通常是 c_int 的一个别名。

class ctypes.c_int64

代表 C 64 位 signed int 数据类型。 通常是 c_longlong 的一个别名。

class ctypes.c_long

代表 C signed long 数据类型。 该构造器接受一个可选的整数初始化器；不会执行溢出检查。

class ctypes.c_longlong

代表 C signed long long 数据类型。 该构造器接受一个可选的整数初始化器；不会执行溢出检查。

class ctypes.c_short

代表 C signed short 数据类型。 该构造器接受一个可选的整数初始化器；不会执行溢出检查。

class ctypes.c_size_t

代表 C size_t 数据类型。

class ctypes.c_ssize_t

代表 C ssize_t 数据类型。

3.2 新版功能.

class ctypes.c_ubyte

代表 C unsigned char 数据类型，它将值解读为一个小整数。 该构造器接受一个可选的整数初始化器；不会执行溢出检查。

class ctypes.c_uint

代表 C unsigned int 数据类型。 该构造器接受一个可选的整数初始化器；不会执行溢出检查。 在 sizeof(int) == sizeof(long) 的平台上它是 c_ulong 的一个别名。

class ctypes.c_uint8

代表 C 8 位 unsigned int 数据类型。 通常是 c_ubyte 的一个别名。

class ctypes.c_uint16

代表 C 16 位 unsigned int 数据类型。 通常是 c_ushort 的一个别名。

class ctypes.c_uint32

代表 C 32 位 unsigned int 数据类型。 通常是 c_uint 的一个别名。.

class ctypes.c_uint64

代表 C 64 位 unsigned int 数据类型。 通常是 c_ulonglong 的一个别名。

class ctypes.c_ulong

代表 C unsigned long 数据类型。 该构造器接受一个可选的整数初始化器；不会执行溢出检查。

class ctypes.c_ulonglong

代表 C unsigned long long 数据类型。 该构造器接受一个可选的整数初始化器；不会执行溢出检查。

class ctypes.c_ushort

代表 C unsigned short 数据类型。 该构造器接受一个可选的整数初始化器；不会执行溢出检查。

class ctypes.c_void_p

代表 C void * 类型。 该值被表示为整数形式。 该构造器接受一个可选的整数初始化器。

class ctypes.c_wchar

代表 C wchar_t 数据类型，并将值解读为一单个字符的 unicode 字符串。 该构造器接受一个可选的字符串初始化器，字符串的长度必须恰好为一个字符。

class ctypes.c_wchar_p

代表 C wchar_t * 数据类型，它必须为指向以零为结束符的宽字符串的指针。 该构造器接受一个整数地址或者一个字符串。

class ctypes.c_bool

代表 C bool 数据类型 (更准确地说是 C99 _Bool)。 它的值可以为 True 或 False，并且该构造器接受任何具有逻辑值的对象。

class ctypes.HRESULT

Windows 专属：代表一个 HRESULT 值，它包含某个函数或方法调用的成功或错误信息。

class ctypes.py_object

代表 C PyObject * 数据类型。 不带参数地调用此构造器将创建一个 NULL PyObject * 指针。

ctypes.wintypes 模块提供了其他许多 Windows 专属的数据类型，例如 HWND, WPARAM 或 DWORD。 还定义了一些有用的结构体例如 MSG 或 RECT。

16.16.2.8. 结构化数据类型

class ctypes.Union(*args, **kw)

本机字节序的联合所对应的抽象基类。

class ctypes.BigEndianStructure(*args, **kw)

大端 字节序的结构体所对应的抽象基类。

class ctypes.LittleEndianStructure(*args, **kw)

小端 字节序的结构体所对应的抽象基类。

非本机字节序的结构体不能包含指针类型字段，或任何其他包含指针类型字段的数据类型。

class ctypes.Structure(*args, **kw)

本机 字节序的结构体所对应的抽象基类。

实际的结构体和联合类型必须通过子类化这些类型之一来创建，并且至少要定义一个 _fields_ 类变量。 ctypes 将创建 descriptor，它允许通过直接属性访问来读取和写入字段。 这些是

_fields_

一个定义结构体字段的序列。 其中的条目必须为 2 元组或 3 元组。 元组的第一项是字段名称，第二项指明字段类型；它可以是任何 ctypes 数据类型。

对于整数类型字段例如 c_int，可以给定第三个可选项。 它必须是一个定义字段比特位宽度的小正整数。

字段名称在一个结构体或联合中必须唯一。 不会检查这个唯一性，但当名称出现重复时将只有一个字段可被访问。

可以在定义 Structure 子类的类语句 之后 再定义 _fields_ 类变量，这将允许创建直接或间接引用其自身的数据类型:

class List(Structure):
    pass
List._fields_ = [("pnext", POINTER(List)),
                 ...
                ]

但是，_fields_ 类变量必须在类型第一次被使用（创建实例，调用 sizeof() 等等）之前进行定义。 在此之后对 _fields_ 类变量赋值将会引发 AttributeError。

It is possible to defined sub-subclasses of structure types, they inherit the fields of the base class plus the _fields_ defined in the sub-subclass, if any.

_pack_

一个可选的小整数，它允许覆盖实体中结构体字段的对齐方式。 当 _fields_ 被赋值时必须已经定义了 _pack_，否则它将没有效果。

_anonymous_

一个可选的序列，它会列出未命名（匿名）字段的名称。 当 _fields_ 被赋值时必须已经定义了 _anonymous_，否则它将没有效果。

在此变量中列出的字段必须为结构体或联合类型字段。 ctypes 将在结构体类型中创建描述器以允许直接访问嵌套字段，而无需创建结构体或联合字段。

以下是一个示例类型（Windows）:

class _U(Union):
    _fields_ = [("lptdesc", POINTER(TYPEDESC)),
                ("lpadesc", POINTER(ARRAYDESC)),
                ("hreftype", HREFTYPE)]

class TYPEDESC(Structure):
    _anonymous_ = ("u",)
    _fields_ = [("u", _U),
                ("vt", VARTYPE)]

TYPEDESC 结构体描述了一个 COM 数据类型，vt 字段指明哪个联合字段是有效的。 由于 u 字段被定义为匿名字段，现在可以直接从 TYPEDESC 实例访问成员。 td.lptdesc 和 td.u.lptdesc 是等价的，但前者速度更快，因为它不需要创建临时的联合实例:

td = TYPEDESC()
td.vt = VT_PTR
td.lptdesc = POINTER(some_type)
td.u.lptdesc = POINTER(some_type)

It is possible to defined sub-subclasses of structures, they inherit the fields of the base class. If the subclass definition has a separate _fields_ variable, the fields specified in this are appended to the fields of the base class.

结构体和联合的构造器均可接受位置和关键字参数。 位置参数用于按照 _fields_ 中的出现顺序来初始化成员字段。 构造器中的关键字参数会被解读为属性赋值，因此它们将以相应的名称来初始化 _fields_，或为不存在于 _fields_ 中的名称创建新的属性。

16.16.2.9. 数组与指针

class ctypes.Array(*args)

数组的抽象基类。

创建实际数组类型的推荐方式是通过将任意 ctypes 类型与一个正整数相乘。 作为替代方式，你也可以子类化这个类型并定义 _length_ 和 _type_ 类变量。 数组元素可使用标准的抽取和切片方式来读写；对于切片读取，结果对象本身 并非 一个 Array。

_length_

一个指明数组中元素数量的正整数。 超出范围的抽取会导致 IndexError。 该值将由 len() 返回。

_type_

指明数组中每个元素的类型。

Array 子类构造器可接受位置参数，用来按顺序初始化元素。

class ctypes._Pointer

私有对象，指针的抽象基类。

实际的指针类型是通过调用 POINTER() 并附带其将指向的类型来创建的；这会由 pointer() 自动完成。

如果一个指针指向的是数组，则其元素可使用标准的抽取和切片方式来读写。 指针对象没有长度，因此 len() 将引发 TypeError。 抽取负值将会从指针 之前 的内存中读取（与 C 一样），并且超出范围的抽取将可能因非法访问而导致崩溃（视你的运气而定）。

_type_

指明所指向的类型。

contents

返回指针所指向的对象。 对此属性赋值会使指针改为指向所赋值的对象。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

16. 通用操作系统服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

17. 并发执行

本章中描述的模块支持并发执行代码。 适当的工具选择取决于要执行的任务（CPU密集型或IO密集型）和偏好的开发风格（事件驱动的协作式多任务或抢占式多任务处理）。 这是一个概述：

17.1. threading — 基于线程的并行

17.1.1. 线程本地数据

17.1.2. 线程对象

17.1.3. 锁对象

17.1.4. 递归锁对象

17.1.5. 条件对象

17.1.6. 信号量对象

17.1.6.1. Semaphore 例子

17.1.7. 事件对象

17.1.8. 定时器对象

17.1.9. 栅栏对象

17.1.10. Using locks, conditions, and semaphores in the with statement

17.2. multiprocessing — 基于进程的并行

17.2.1. 概述

17.2.1.1. Process 类

17.2.1.2. 上下文和启动方法

17.2.1.3. 在进程之间交换对象

17.2.1.4. 进程之间的同步

17.2.1.5. 在进程之间共享状态

17.2.1.6. 使用工作进程

17.2.2. 参考

17.2.2.1. Process 和异常

17.2.2.2. 管道和队列

17.2.2.3. 杂项

17.2.2.4. 连接对象（Connection）

17.2.2.5. 同步原语

17.2.2.6. 共享 ctypes 对象

17.2.2.6.1. multiprocessing.sharedctypes 模块

17.2.2.7. 数据管理器

17.2.2.7.1. 自定义管理器

17.2.2.7.2. 使用远程管理器

17.2.2.8. 代理对象

17.2.2.8.1. 清理

17.2.2.9. 进程池

17.2.2.10. 监听者及客户端

17.2.2.10.1. 地址格式

17.2.2.11. 认证密码

17.2.2.12. 日志记录

17.2.2.13. multiprocessing.dummy 模块

17.2.3. 编程指导

17.2.3.1. 所有启动方法

17.2.3.2. spawn 和 forkserver 启动方式

17.2.4. 例子

17.3. concurrent 包

17.4. concurrent.futures — 启动并行任务

17.4.1. Executor 对象

17.4.2. ThreadPoolExecutor

17.4.2.1. ThreadPoolExecutor 例子

17.4.3. ProcessPoolExecutor

17.4.3.1. ProcessPoolExecutor 例子

17.4.4. Future 对象

17.4.5. 模块函数

17.4.6. Exception类

17.5. subprocess — 子进程管理

17.5.1. 使用 subprocess 模块

17.5.1.1. 常用参数

17.5.1.2. Popen 构造函数

17.5.1.3. 异常

17.5.2. 安全考量

17.5.3. Popen 对象

17.5.4. Windows Popen 助手

17.5.4.1. Constants

17.5.5. 较旧的高阶 API

17.5.6. 使用 subprocess 模块替换旧函数

17.5.6.1. Replacing /bin/sh shell backquote

17.5.6.2. 替代 shell 管道

17.5.6.3. 替代 os.system()

17.5.6.4. 替代 os.spawn 函数族

17.5.6.5. 替代 os.popen(), os.popen2(), os.popen3()

17.5.6.6. 来自 popen2 模块的替代函数

17.5.7. 旧式的 Shell 发起函数

17.5.8. 注释

17.5.8.1. 在 Windows 上将参数列表转换为一个字符串

17.6. sched — 事件调度器

17.6.1. 调度器对象

17.7. queue — 一个同步的队列类

17.7.1. Queue对象

以下是上述某些服务的支持模块：

17.8. dummy_threading — 可直接替代 threading 模块。

17.9. _thread — 底层多线程 API

17.10. _dummy_thread — _thread 的替代模块

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

17.1. threading — 基于线程的并行

源代码: Lib/threading.py [https://github.com/python/cpython/tree/3.6/Lib/threading.py]

这个模块在较低级的模块 _thread 基础上建立较高级的线程接口。参见： queue 模块。

The dummy_threading module is provided for situations where threading cannot be used because _thread is missing.

注解

虽然他们没有在下面列出，这个模块仍然支持Python 2.x系列的这个模块下以 camelCase （驼峰法）命名的方法和函数。

这个模块定义了以下函数：

threading.active_count()

返回当前存活的 Thread 对象的数量。 返回值与 enumerate() 所返回的列表长度一致。

threading.current_thread()

返回当前对应调用者的控制线程的 Thread 对象。如果调用者的控制线程不是利用 threading 创建，会返回一个功能受限的虚拟线程对象。

threading.get_ident()

返回当前线程的 “线程标识符”。它是一个非零的整数。它的值没有直接含义，主要是用作 magic cookie，比如作为含有线程相关数据的字典的索引。线程标识符可能会在线程退出，新线程创建时被复用。

3.3 新版功能.

threading.enumerate()

以列表形式返回当前所有存活的 Thread 对象。 该列表包含守护线程，current_thread() 创建的虚拟线程对象和主线程。它不包含已终结的线程和尚未开始的线程。

threading.main_thread()

返回主 Thread 对象。一般情况下，主线程是Python解释器开始时创建的线程。

3.4 新版功能.

threading.settrace(func)

为所有 threading 模块开始的线程设置追踪函数。在每个线程的 run() 方法被调用前，func 会被传递给 sys.settrace() 。

threading.setprofile(func)

为所有 threading 模块开始的线程设置性能测试函数。在每个线程的 run() 方法被调用前，func 会被传递给 sys.setprofile() 。

threading.stack_size([size])

Return the thread stack size used when creating new threads. The optional size argument specifies the stack size to be used for subsequently created threads, and must be 0 (use platform or configured default) or a positive integer value of at least 32,768 (32 KiB). If size is not specified, 0 is used. If changing the thread stack size is unsupported, a RuntimeError is raised. If the specified stack size is invalid, a ValueError is raised and the stack size is unmodified. 32 KiB is currently the minimum supported stack size value to guarantee sufficient stack space for the interpreter itself. Note that some platforms may have particular restrictions on values for the stack size, such as requiring a minimum stack size > 32 KiB or requiring allocation in multiples of the system memory page size - platform documentation should be referred to for more information (4 KiB pages are common; using multiples of 4096 for the stack size is the suggested approach in the absence of more specific information). Availability: Windows, systems with POSIX threads.

这个模块同时定义了以下常量：

threading.TIMEOUT_MAX

阻塞函数（ Lock.acquire(), RLock.acquire(), Condition.wait(), …）中形参 timeout 允许的最大值。传入超过这个值的 timeout 会抛出 OverflowError 异常。

3.2 新版功能.

这个模块定义了许多类，详见以下部分。

该模块的设计基于 Java的线程模型。 但是，在Java里面，锁和条件变量是每个对象的基础特性，而在Python里面，这些被独立成了单独的对象。 Python 的 Thread 类只是 Java 的 Thread 类的一个子集；目前还没有优先级，没有线程组，线程还不能被销毁、停止、暂停、恢复或中断。 Java 的 Thread 类的静态方法在实现时会映射为模块级函数。

下述方法的执行都是原子性的。

17.1.1. 线程本地数据

线程本地数据是特定线程的数据。管理线程本地数据，只需要创建一个 local （或者一个子类型）的实例并在实例中储存属性：

mydata = threading.local()
mydata.x = 1

在不同的线程中，实例的值会不同。

class threading.local

一个代表线程本地数据的类。

更多相关细节和大量示例，参见 _threading_local 模块的文档。

17.1.2. 线程对象

The Thread class represents an activity that is run in a separate thread of control. There are two ways to specify the activity: by passing a callable object to the constructor, or by overriding the run() method in a subclass. No other methods (except for the constructor) should be overridden in a subclass. In other words, only override the __init__() and run() methods of this class.

当线程对象一但被创建，其活动一定会因调用线程的 start() 方法开始。这会在独立的控制线程调用 run() 方法。

一旦线程活动开始，该线程会被认为是 ‘存活的’ 。当它的 run() 方法终结了（不管是正常的还是抛出未被处理的异常），就不是’存活的’。 is_alive() 方法用于检查线程是否存活。

其他线程可以调用一个线程的 join() 方法。这会阻塞调用该方法的线程，直到被调用 join() 方法的线程终结。

线程有名字。名字可以传递给构造函数，也可以通过 name 属性读取或者修改。

一个线程可以被标记成一个“守护线程”。 这个标志的意义是，当剩下的线程都是守护线程时，整个 Python 程序将会退出。 初始值继承于创建线程。 这个标志可以通过 daemon 特征属性或者 daemon 构造器参数来设置。

注解

守护线程在程序关闭时会突然关闭。他们的资源（例如已经打开的文档，数据库事务等等）可能没有被正确释放。如果你想你的线程正常停止，设置他们成为非守护模式并且使用合适的信号机制，例如： Event。

有个 “主线程” 对象；这对应Python程序里面初始的控制线程。它不是一个守护线程。

“虚拟线程对象” 是可以被创建的。这些是对应于“外部线程”的线程对象，它们是在线程模块外部启动的控制线程，例如直接来自C代码。虚拟线程对象功能受限；他们总是被认为是存活的和守护模式，不能被 join() 。因为无法检测外来线程的终结，它们永远不会被删除。

class threading.Thread(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)

调用这个构造函数时，必需带有关键字参数。参数如下：

group 应该为 None；为了日后扩展 ThreadGroup 类实现而保留。

target 是用于 run() 方法调用的可调用对象。默认是 None，表示不需要调用任何方法。

name 是线程名称。默认情况下，由 “Thread-N” 格式构成一个唯一的名称，其中 N 是小的十进制数。

args 是用于调用目标函数的参数元组。默认是 ()。

kwargs 是用于调用目标函数的关键字参数字典。默认是 {}。

如果不是 None，daemon 参数将显式地设置该线程是否为守护模式。 如果是 None (默认值)，线程将继承当前线程的守护模式属性。

如果子类型重载了构造函数，它一定要确保在做任何事前，先发起调用基类构造器(Thread.__init__())。

在 3.3 版更改: 加入 daemon 参数。

start()

开始线程活动。

它在一个线程里最多只能被调用一次。它安排对象的 run() 方法在一个独立的控制进程中调用。

如果同一个线程对象中调用这个方法的次数大于一次，会抛出 RuntimeError 。

run()

代表线程活动的方法。

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

join(timeout=None)

等待，直到线程终结。这会阻塞调用这个方法的线程，直到被调用 join() 的线程终结 – 不管是正常终结还是抛出未处理异常 – 或者直到发生超时，超时选项是可选的。

当 timeout 参数存在而且不是 None 时，它应该是一个用于指定操作超时的以秒为单位的浮点数（或者分数）。因为 join() 总是返回 None ，所以你一定要在 join() 后调用 is_alive() 才能判断是否发生超时 – 如果线程仍然存活，则 join() 超时。

当 timeout 参数不存在或者是 None ，这个操作会阻塞直到线程终结。

一个线程可以被 join() 很多次。

如果尝试加入当前线程会导致死锁， join() 会引起 RuntimeError 异常。如果尝试 join() 一个尚未开始的线程，也会抛出相同的异常。

name

只用于识别的字符串。它没有语义。多个线程可以赋予相同的名称。 初始名称由构造函数设置。

getName()

setName()

旧的 name 取值/设值 API；直接当做特征属性使用它。

ident

这个线程的 ‘线程标识符’，如果线程尚未开始则为 None 。这是个非零整数。参见 get_ident() 函数。当一个线程退出而另外一个线程被创建，线程标识符会被复用。即使线程退出后，仍可得到标识符。

is_alive()

返回线程是否存活。

当 run() 方法刚开始直到 run() 方法刚结束，这个方法返回 True 。模块函数 enumerate() 返回包含所有存活线程的列表。

daemon

一个表示这个线程是（True）否（False）守护线程的布尔值。一定要在调用 start() 前设置好，不然会抛出 RuntimeError 。初始值继承于创建线程；主线程不是守护线程，因此主线程创建的所有线程默认都是 daemon = False。

当没有存活的非守护线程时，整个Python程序才会退出。

isDaemon()

setDaemon()

旧的 name 取值/设值 API；建议直接当做特征属性使用它。

CPython implementation detail: CPython下，因为 Global Interpreter Lock，一个时刻只有一个线程可以执行Python代码（尽管如此，某些性能导向的库可能会克服这个限制）。如果你想让你的应用更好的利用多核计算机的计算性能，推荐你使用 multiprocessing 或者 concurrent.futures.ProcessPoolExecutor 。但是如果你想同时运行多个I/O绑定任务，线程仍然是一个合适的模型。

17.1.3. 锁对象

原始锁是一个在锁定时不属于特定线程的同步基元组件。在Python中，它是能用的最低级的同步基元组件，由 _thread 扩展模块直接实现。

原始锁处于 “锁定” 或者 “非锁定” 两种状态之一。它被创建时为非锁定状态。它有两个基本方法， acquire() 和 release() 。当状态为非锁定时， acquire() 将状态改为 锁定 并立即返回。当状态是锁定时， acquire() 将阻塞至其他线程调用 release() 将其改为非锁定状态，然后 acquire() 调用重置其为锁定状态并返回。 release() 只在锁定状态下调用； 它将状态改为非锁定并立即返回。如果尝试释放一个非锁定的锁，则会引发 RuntimeError  异常。

锁同样支持 上下文管理协议。

当多个线程在 acquire() 等待状态转变为未锁定被阻塞，然后 release() 重置状态为未锁定时，只有一个线程能继续执行；至于哪个等待线程继续执行没有定义，并且会根据实现而不同。

所有方法的执行都是原子性的。

class threading.Lock

实现原始锁对象的类。一旦一个线程获得一个锁，会阻塞随后尝试获得锁的线程，直到它被释放；任何线程都可以释放它。

需要注意的是 Lock 其实是一个工厂函数，返回平台支持的具体锁类中最有效的版本的实例。

acquire(blocking=True, timeout=-1)

可以阻塞或非阻塞地获得锁。

当调用时参数 blocking 设置为 True （缺省值），阻塞直到锁被释放，然后将锁锁定并返回 True 。

在参数 blocking 被设置为 False 的情况下调用，将不会发生阻塞。如果调用时 blocking 设为 True 会阻塞，并立即返回 False ；否则，将锁锁定并返回 True。

当浮点型 timeout 参数被设置为正值调用时，只要无法获得锁，将最多阻塞 timeout 设定的秒数。timeout 参数被设置为 -1 时将无限等待。当 blocking 为 false 时，timeout 指定的值将被忽略。

如果成功获得锁，则返回 True，否则返回 False (例如发生 超时 的时候)。

在 3.2 版更改: 新的 timeout 形参。

在 3.2 版更改: 现在如果底层线程实现支持，则可以通过POSIX上的信号中断锁的获取。

release()

释放一个锁。这个方法可以在任何线程中调用，不单指获得锁的线程。

当锁被锁定，将它重置为未锁定，并返回。如果其他线程正在等待这个锁解锁而被阻塞，只允许其中一个允许。

在未锁定的锁调用时，会引发 RuntimeError 异常。

没有返回值。

17.1.4. 递归锁对象

重入锁是一个可以被同一个线程多次获取的同步基元组件。在内部，它在基元锁的锁定/非锁定状态上附加了 “所属线程” 和 “递归等级” 的概念。在锁定状态下，某些线程拥有锁 ； 在非锁定状态下， 没有线程拥有它。

若要锁定锁，线程调用其 acquire() 方法；一旦线程拥有了锁，方法将返回。若要解锁，线程调用 release() 方法。 acquire()/release() 对可以嵌套；只有最终 release() (最外面一对的 release() ) 将锁解开，才能让其他线程继续处理 acquire() 阻塞。

递归锁也支持 上下文管理协议。

class threading.RLock

此类实现了重入锁对象。重入锁必须由获取它的线程释放。一旦线程获得了重入锁，同一个线程再次获取它将不阻塞；线程必须在每次获取它时释放一次。

需要注意的是 RLock 其实是一个工厂函数，返回平台支持的具体递归锁类中最有效的版本的实例。

acquire(blocking=True, timeout=-1)

可以阻塞或非阻塞地获得锁。

当无参数调用时： 如果这个线程已经拥有锁，递归级别增加一，并立即返回。否则，如果其他线程拥有该锁，则阻塞至该锁解锁。一旦锁被解锁(不属于任何线程)，则抢夺所有权，设置递归等级为一，并返回。如果多个线程被阻塞，等待锁被解锁，一次只有一个线程能抢到锁的所有权。在这种情况下，没有返回值。

When invoked with the blocking argument set to true, do the same thing as when called without arguments, and return true.

When invoked with the blocking argument set to false, do not block. If a call without an argument would block, return false immediately; otherwise, do the same thing as when called without arguments, and return true.

When invoked with the floating-point timeout argument set to a positive value, block for at most the number of seconds specified by timeout and as long as the lock cannot be acquired. Return true if the lock has been acquired, false if the timeout has elapsed.

在 3.2 版更改: 新的 timeout 形参。

release()

释放锁，自减递归等级。如果减到零，则将锁重置为非锁定状态(不被任何线程拥有)，并且，如果其他线程正被阻塞着等待锁被解锁，则仅允许其中一个线程继续。如果自减后，递归等级仍然不是零，则锁保持锁定，仍由调用线程拥有。

只有当前线程拥有锁才能调用这个方法。如果锁被释放后调用这个方法，会引起 RuntimeError 异常。

没有返回值。

17.1.5. 条件对象

条件变量总是与某种类型的锁对象相关联，锁对象可以通过传入获得，或者在缺省的情况下自动创建。当多个条件变量需要共享同一个锁时，传入一个锁很有用。锁是条件对象的一部分，你不必单独地跟踪它。

条件变量服从 上下文管理协议：使用 with 语句会在它包围的代码块内获取关联的锁。 acquire() 和 release() 方法也能调用关联锁的相关方法。

其它方法必须在持有关联的锁的情况下调用。 wait() 方法释放锁，然后阻塞直到其它线程调用 notify() 方法或 notify_all() 方法唤醒它。一旦被唤醒， wait() 方法重新获取锁并返回。它也可以指定超时时间。

The notify() method wakes up one of the threads waiting for the condition variable, if any are waiting. The notify_all() method wakes up all threads waiting for the condition variable.

注意： notify() 方法和 notify_all() 方法并不会释放锁，这意味着被唤醒的线程不会立即从它们的 wait() 方法调用中返回，而是会在调用了 notify() 方法或 notify_all() 方法的线程最终放弃了锁的所有权后返回。

使用条件变量的典型编程风格是将锁用于同步某些共享状态的权限，那些对状态的某些特定改变感兴趣的线程，它们重复调用 wait() 方法，直到看到所期望的改变发生；而对于修改状态的线程，它们将当前状态改变为可能是等待者所期待的新状态后，调用 notify() 方法或者 notify_all() 方法。例如，下面的代码是一个通用的无限缓冲区容量的生产者-消费者情形：

# Consume one item
with cv:
    while not an_item_is_available():
        cv.wait()
    get_an_available_item()

# Produce one item
with cv:
    make_an_item_available()
    cv.notify()

使用 while 循环检查所要求的条件成立与否是有必要的，因为 wait() 方法可能要经过不确定长度的时间后才会返回，而此时导致 notify() 方法调用的那个条件可能已经不再成立。这是多线程编程所固有的问题。 wait_for() 方法可自动化条件检查，并简化超时计算。

# Consume an item
with cv:
    cv.wait_for(an_item_is_available)
    get_an_available_item()

选择 notify() 还是 notify_all() ，取决于一次状态改变是只能被一个还是能被多个等待线程所用。例如在一个典型的生产者-消费者情形中，添加一个项目到缓冲区只需唤醒一个消费者线程。

class threading.Condition(lock=None)

实现条件变量对象的类。一个条件变量对象允许一个或多个线程在被其它线程所通知之前进行等待。

如果给出了非 None 的 lock 参数，则它必须为 Lock 或者 RLock 对象，并且它将被用作底层锁。否则，将会创建新的 RLock 对象，并将其用作底层锁。

在 3.3 版更改: 从工厂函数变为类。

acquire(*args)

请求底层锁。此方法调用底层锁的相应方法，返回值是底层锁相应方法的返回值。

release()

释放底层锁。此方法调用底层锁的相应方法。没有返回值。

wait(timeout=None)

等待直到被通知或发生超时。如果线程在调用此方法时没有获得锁，将会引发 RuntimeError 异常。

这个方法释放底层锁，然后阻塞，直到在另外一个线程中调用同一个条件变量的 notify() 或 notify_all() 唤醒它，或者直到可选的超时发生。一旦被唤醒或者超时，它重新获得锁并返回。

当提供了 timeout 参数且不是 None 时，它应该是一个浮点数，代表操作的超时时间，以秒为单位（可以为小数）。

当底层锁是个 RLock ，不会使用它的 release() 方法释放锁，因为当它被递归多次获取时，实际上可能无法解锁。相反，使用了 RLock 类的内部接口，即使多次递归获取它也能解锁它。 然后，在重新获取锁时，使用另一个内部接口来恢复递归级别。

返回 True ，除非提供的 timeout 过期，这种情况下返回 False。

在 3.2 版更改: 很明显，方法总是返回 None。

wait_for(predicate, timeout=None)

等待，直到条件计算为真。 predicate 应该是一个可调用对象而且它的返回值可被解释为一个布尔值。可以提供 timeout 参数给出最大等待时间。

这个实用方法会重复地调用 wait() 直到满足判断式或者发生超时。返回值是判断式最后一个返回值，而且如果方法发生超时会返回 False 。

忽略超时功能，调用此方法大致相当于编写:

while not predicate():
    cv.wait()

因此，规则同样适用于 wait() ：锁必须在被调用时保持获取，并在返回时重新获取。 随着锁定执行判断式。

3.2 新版功能.

notify(n=1)

默认唤醒一个等待这个条件的线程。如果调用线程在没有获得锁的情况下调用这个方法，会引发 RuntimeError 异常。

这个方法唤醒最多 n 个正在等待这个条件变量的线程；如果没有线程在等待，这是一个空操作。

当前实现中，如果至少有 n 个线程正在等待，准确唤醒 n 个线程。但是依赖这个行为并不安全。未来，优化的实现有时会唤醒超过 n 个线程。

注意：被唤醒的线程实际上不会返回它调用的 wait() ，直到它可以重新获得锁。因为 notify() 不会释放锁，只有它的调用者应该这样做。

notify_all()

唤醒所有正在等待这个条件的线程。这个方法行为与 notify() 相似，但并不只唤醒单一线程，而是唤醒所有等待线程。如果调用线程在调用这个方法时没有获得锁，会引发 RuntimeError 异常。

17.1.6. 信号量对象

这是计算机科学史上最古老的同步原语之一，早期的荷兰科学家 Edsger W. Dijkstra 发明了它。（他使用名称 P() 和 V() 而不是 acquire() 和 release() ）。

一个信号量管理一个内部计数器，该计数器因 acquire() 方法的调用而递减，因 release() 方法的调用而递增。 计数器的值永远不会小于零；当 acquire() 方法发现计数器为零时，将会阻塞，直到其它线程调用 release() 方法。

信号量对象也支持 上下文管理协议 。

class threading.Semaphore(value=1)

该类实现信号量对象。信号量对象管理一个原子性的计数器，代表 release() 方法的调用次数减去 acquire() 的调用次数再加上一个初始值。如果需要， acquire() 方法将会阻塞直到可以返回而不会使得计数器变成负数。在没有显式给出 value 的值时，默认为1。

可选参数 value 赋予内部计数器初始值，默认值为 1 。如果 value 被赋予小于0的值，将会引发 ValueError 异常。

在 3.3 版更改: 从工厂函数变为类。

acquire(blocking=True, timeout=None)

获取一个信号量。

在不带参数的情况下调用时：

If the internal counter is larger than zero on entry, decrement it by one and return true immediately.

If the internal counter is zero on entry, block until awoken by a call to release(). Once awoken (and the counter is greater than 0), decrement the counter by 1 and return true. Exactly one thread will be awoken by each call to release(). The order in which threads are awoken should not be relied on.

When invoked with blocking set to false, do not block. If a call without an argument would block, return false immediately; otherwise, do the same thing as when called without arguments, and return true.

When invoked with a timeout other than None, it will block for at most timeout seconds. If acquire does not complete successfully in that interval, return false. Return true otherwise.

在 3.2 版更改: 新的 timeout 形参。

release()

释放一个信号量，将内部计数器的值增加1。当计数器原先的值为0且有其它线程正在等待它再次大于0时，唤醒正在等待的线程。

class threading.BoundedSemaphore(value=1)

该类实现有界信号量。有界信号量通过检查以确保它当前的值不会超过初始值。如果超过了初始值，将会引发 ValueError 异常。在大多情况下，信号量用于保护数量有限的资源。如果信号量被释放的次数过多，则表明出现了错误。没有指定时， value 的值默认为1。

在 3.3 版更改: 从工厂函数变为类。

17.1.6.1. Semaphore 例子

信号量通常用于保护数量有限的资源，例如数据库服务器。在资源数量固定的任何情况下，都应该使用有界信号量。在生成任何工作线程前，应该在主线程中初始化信号量。

maxconnections = 5
# ...
pool_sema = BoundedSemaphore(value=maxconnections)

工作线程生成后，当需要连接服务器时，这些线程将调用信号量的 acquire 和 release 方法：

with pool_sema:
    conn = connectdb()
    try:
        # ... use connection ...
    finally:
        conn.close()

使用有界信号量能减少这种编程错误：信号量的释放次数多于其请求次数。

17.1.7. 事件对象

这是线程之间通信的最简单机制之一：一个线程发出事件信号，而其他线程等待该信号。

一个事件对象管理一个内部标志，调用 set() 方法可将其设置为true，调用 clear() 方法可将其设置为false，调用 wait() 方法将进入阻塞直到标志为true。

class threading.Event

实现事件对象的类。事件对象管理一个内部标志，调用 set() 方法可将其设置为true。调用 clear() 方法可将其设置为false。调用 wait() 方法将进入阻塞直到标志为true。这个标志初始时为false。

在 3.3 版更改: 从工厂函数变为类。

is_set()

Return true if and only if the internal flag is true.

set()

将内部标志设置为true。所有正在等待这个事件的线程将被唤醒。当标志为true时，调用 wait() 方法的线程不会被被阻塞。

clear()

将内部标志设置为false。之后调用 wait() 方法的线程将会被阻塞，直到调用 set() 方法将内部标志再次设置为true。

wait(timeout=None)

阻塞线程直到内部变量为true。如果调用时内部标志为true，将立即返回。否则将阻塞线程，直到调用 set() 方法将标志设置为true或者发生可选的超时。

当提供了timeout参数且不是 None 时，它应该是一个浮点数，代表操作的超时时间，以秒为单位（可以为小数）。

This method returns true if and only if the internal flag has been set to true, either before the wait call or after the wait starts, so it will always return True except if a timeout is given and the operation times out.

在 3.1 版更改: 很明显，方法总是返回 None。

17.1.8. 定时器对象

此类表示一个操作应该在等待一定的时间之后运行 — 相当于一个定时器。 Timer 类是 Thread 类的子类，因此可以像一个自定义线程一样工作。

与线程一样，通过调用 start() 方法启动定时器。而 cancel() 方法可以停止计时器（在计时结束前）， 定时器在执行其操作之前等待的时间间隔可能与用户指定的时间间隔不完全相同。

例如

def hello():
    print("hello, world")

t = Timer(30.0, hello)
t.start()  # after 30 seconds, "hello, world" will be printed

class threading.Timer(interval, function, args=None, kwargs=None)

创建一个定时器，在经过 interval 秒的间隔事件后，将会用参数 args 和关键字参数 kwargs 调用 function。如果 args 为 None （默认值），则会使用一个空列表。如果 kwargs 为 None （默认值），则会使用一个空字典。

在 3.3 版更改: 从工厂函数变为类。

cancel()

停止定时器并取消执行计时器将要执行的操作。仅当计时器仍处于等待状态时有效。

17.1.9. 栅栏对象

3.2 新版功能.

栅栏类提供一个简单的同步原语，用于应对固定数量的线程需要彼此相互等待的情况。线程调用 wait() 方法后将阻塞，直到所有线程都调用了 wait() 方法。此时所有线程将被同时释放。

栅栏对象可以被多次使用，但进程的数量不能改变。

这是一个使用简便的方法实现客户端进程与服务端进程同步的例子：

b = Barrier(2, timeout=5)

def server():
    start_server()
    b.wait()
    while True:
        connection = accept_connection()
        process_server_connection(connection)

def client():
    b.wait()
    while True:
        connection = make_connection()
        process_client_connection(connection)

class threading.Barrier(parties, action=None, timeout=None)

创建一个需要 parties 个线程的栅栏对象。如果提供了可调用的 action 参数，它会在所有线程被释放时在其中一个线程中自动调用。 timeout 是默认的超时时间，如果没有在 wait() 方法中指定超时时间的话。

wait(timeout=None)

冲出栅栏。当栅栏中所有线程都已经调用了这个函数，它们将同时被释放。如果提供了 timeout 参数，这里的 timeout 参数优先于创建栅栏对象时提供的 timeout 参数。

函数返回值是一个整数，取值范围在0到 parties – 1，在每个线程中的返回值不相同。可用于从所有线程中选择唯一的一个线程执行一些特别的工作。例如：

i = barrier.wait()
if i == 0:
    # Only one thread needs to print this
    print("passed the barrier")

如果创建栅栏对象时在构造函数中提供了 action 参数，它将在其中一个线程释放前被调用。如果此调用引发了异常，栅栏对象将进入损坏态。

如果发生了超时，栅栏对象将进入破损态。

如果栅栏对象进入破损态，或重置栅栏时仍有线程等待释放，将会引发 BrokenBarrierError 异常。

reset()

重置栅栏为默认的初始态。如果栅栏中仍有线程等待释放，这些线程将会收到 BrokenBarrierError 异常。

注意使用此函数时，如果有某些线程状态未知，则可能需其它的同步来确保线程已被释放。如果栅栏进入了破损态，最好废弃它并新建一个栅栏。

abort()

使栅栏进入破损态。这将导致所有已经调用和未来调用的 wait() 方法中引发 BrokenBarrierError 异常。使用这个方法的一种情况是需要中止程序以避免死锁。

更好的方式是：创建栅栏时提供一个合理的超时时间，来自动避免某个线程出错。

parties

冲出栅栏所需要的线程数量。

n_waiting

当前时刻正在栅栏中阻塞的线程数量。

broken

一个布尔值，值为 True 表明栅栏为破损态。

exception threading.BrokenBarrierError

异常类，是 RuntimeError 异常的子类，在 Barrier 对象重置时仍有线程阻塞时和对象进入破损态时被引发。

17.1.10. Using locks, conditions, and semaphores in the with statement

这个模块提供的带有 acquire() 和 release() 方法的对象，可以被用作 with 语句的上下文管理器。当进入语句块时 acquire() 方法会被调用，退出语句块时 release() 会被调用。因此，以下片段:

with some_lock:
    # do something...

相当于:

some_lock.acquire()
try:
    # do something...
finally:
    some_lock.release()

现在 Lock 、 RLock 、 Condition 、 Semaphore 和 BoundedSemaphore 对象可以用作 with 语句的上下文管理器。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

17.2. multiprocessing — 基于进程的并行

源代码 Lib/multiprocessing/ [https://github.com/python/cpython/tree/3.6/Lib/multiprocessing/]

17.2.1. 概述

multiprocessing 是一个用与 threading 模块相似API的支持产生进程的包。 multiprocessing 包同时提供本地和远程并发，使用子进程代替线程，有效避免 Global Interpreter Lock 带来的影响。因此， multiprocessing 模块允许程序员充分利用机器上的多个核心。Unix 和 Windows 上都可以运行。

multiprocessing 模块还引入了在 threading 模块中没有类似物的API。这方面的一个主要例子是 Pool 对象，它提供了一种方便的方法，可以跨多个输入值并行化函数的执行，跨进程分配输入数据（数据并行）。以下示例演示了在模块中定义此类函数的常见做法，以便子进程可以成功导入该模块。这个数据并行的基本例子使用 Pool ，

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    with Pool(5) as p:
        print(p.map(f, [1, 2, 3]))

将打印到标准输出

[1, 4, 9]

17.2.1.1. Process 类

在 multiprocessing 中，通过创建一个 Process 对象然后调用它的 start() 方法来生成进程。 Process 和 threading.Thread API 相同。 一个简单的多进程程序示例是:

from multiprocessing import Process

def f(name):
    print('hello', name)

if __name__ == '__main__':
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

要显示所涉及的各个进程ID，这是一个扩展示例:

from multiprocessing import Process
import os

def info(title):
    print(title)
    print('module name:', __name__)
    print('parent process:', os.getppid())
    print('process id:', os.getpid())

def f(name):
    info('function f')
    print('hello', name)

if __name__ == '__main__':
    info('main line')
    p = Process(target=f, args=('bob',))
    p.start()
    p.join()

为了解释为什么 if __name__ == '__main__' 部分是必需的，请参见 编程指导。

17.2.1.2. 上下文和启动方法

根据不同的平台， multiprocessing 支持三种启动进程的方法。这些 启动方法 有

spawn

父进程启动一个新的Python解释器进程。子进程只会继承那些运行进程对象的 run() 方法所需的资源。特别是父进程中非必须的文件描述符和句柄不会被继承。相对于使用 fork 或者 forkserver，使用这个方法启动进程相当慢。

可在Unix和Windows上使用。 Windows上的默认设置。

fork

父进程使用 os.fork() 来产生 Python 解释器分叉。子进程在开始时实际上与父进程相同。父进程的所有资源都由子进程继承。请注意，安全分叉多线程进程是棘手的。

只存在于Unix。Unix中的默认值。

forkserver

程序启动并选择* forkserver * 启动方法时，将启动服务器进程。从那时起，每当需要一个新进程时，父进程就会连接到服务器并请求它分叉一个新进程。分叉服务器进程是单线程的，因此使用 os.fork() 是安全的。没有不必要的资源被继承。

可在Unix平台上使用，支持通过Unix管道传递文件描述符。

在 3.4 版更改: spawn 在所有unix平台上添加，并且为一些unix平台添加了 forkserver 。子进程不再继承Windows上的所有上级进程可继承的句柄。

在Unix上使用 spawn 或 forkserver 启动方法也将启动一个 信号量跟踪器 进程，该进程跟踪由程序进程创建的未链接的命名信号量。当所有进程退出时，信号量跟踪器取消链接任何剩余的信号量。通常不应该有，但如果一个进程被信号杀死，可能会有一些“泄露”的信号量。（取消链接命名的信号量是一个严重的问题，因为系统只允许有限的数量，并且在下次重新启动之前它们不会自动取消链接。）

要选择一个启动方法，你应该在主模块的 if __name__ == '__main__' 子句中调用 set_start_method() 。例如：

import multiprocessing as mp

def foo(q):
    q.put('hello')

if __name__ == '__main__':
    mp.set_start_method('spawn')
    q = mp.Queue()
    p = mp.Process(target=foo, args=(q,))
    p.start()
    print(q.get())
    p.join()

在程序中 set_start_method() 不应该被多次调用。

或者，你可以使用 get_context() 来获取上下文对象。上下文对象与多处理模块具有相同的API，并允许在同一程序中使用多个启动方法。:

import multiprocessing as mp

def foo(q):
    q.put('hello')

if __name__ == '__main__':
    ctx = mp.get_context('spawn')
    q = ctx.Queue()
    p = ctx.Process(target=foo, args=(q,))
    p.start()
    print(q.get())
    p.join()

请注意，与一个上下文相关的对象可能与不同上下文的进程不兼容。特别是，使用 fork 上下文创建的锁不能传递给使用 spawn 或 forkserver 启动方法启动的进程。

想要使用特定启动方法的库应该使用 get_context() 以避免干扰库用户的选择。

17.2.1.3. 在进程之间交换对象

multiprocessing 支持进程之间的两种通信通道：

队列

Queue 类是一个近似 queue.Queue 的克隆。 例如:

from multiprocessing import Process, Queue

def f(q):
    q.put([42, None, 'hello'])

if __name__ == '__main__':
    q = Queue()
    p = Process(target=f, args=(q,))
    p.start()
    print(q.get())    # prints "[42, None, 'hello']"
    p.join()

队列是线程和进程安全的。

管道

Pipe() 函数返回一个由管道连接的连接对象，默认情况下是双工（双向）。例如:

from multiprocessing import Process, Pipe

def f(conn):
    conn.send([42, None, 'hello'])
    conn.close()

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    p = Process(target=f, args=(child_conn,))
    p.start()
    print(parent_conn.recv())   # prints "[42, None, 'hello']"
    p.join()

返回的两个连接对象 Pipe() 表示管道的两端。每个连接对象都有 send() 和 recv() 方法（相互之间的）。请注意，如果两个进程（或线程）同时尝试读取或写入管道的 同一 端，则管道中的数据可能会损坏。当然，同时使用管道的不同端的进程不存在损坏的风险。

17.2.1.4. 进程之间的同步

multiprocessing 包含来自 threading 的所有同步原语的等价物。例如，可以使用锁来确保一次只有一个进程打印到标准输出:

from multiprocessing import Process, Lock

def f(l, i):
    l.acquire()
    try:
        print('hello world', i)
    finally:
        l.release()

if __name__ == '__main__':
    lock = Lock()

    for num in range(10):
        Process(target=f, args=(lock, num)).start()

不使用来自不同进程的锁输出容易产生混淆。

17.2.1.5. 在进程之间共享状态

如上所述，在进行并发编程时，通常最好尽量避免使用共享状态。使用多个进程时尤其如此。

但是，如果你真的需要使用一些共享数据，那么 multiprocessing 提供了两种方法。

共享内存

可以使用 Value 或 Array 将数据存储在共享内存映射中。例如，以下代码:

from multiprocessing import Process, Value, Array

def f(n, a):
    n.value = 3.1415927
    for i in range(len(a)):
        a[i] = -a[i]

if __name__ == '__main__':
    num = Value('d', 0.0)
    arr = Array('i', range(10))

    p = Process(target=f, args=(num, arr))
    p.start()
    p.join()

    print(num.value)
    print(arr[:])

将打印

3.1415927
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

创建 num 和 arr 时使用的 'd' 和 'i' 参数是 array 模块使用的类型的 typecode ： 'd' 表示双精度浮点数， 'i' 表示有符号整数。这些共享对象将是进程和线程安全的。

为了更灵活地使用共享内存，可以使用 multiprocessing.sharedctypes 模块，该模块支持创建从共享内存分配的任意ctypes对象。

服务器进程

由 Manager() 返回的管理器对象控制一个服务器进程，该进程保存Python对象并允许其他进程使用代理操作它们。

Manager() 返回的管理器支持类型： list 、 dict 、 Namespace 、 Lock 、 RLock 、 Semaphore 、 BoundedSemaphore 、 Condition 、 Event 、 Barrier 、 Queue 、 Value 和 Array 。例如

from multiprocessing import Process, Manager

def f(d, l):
    d[1] = '1'
    d['2'] = 2
    d[0.25] = None
    l.reverse()

if __name__ == '__main__':
    with Manager() as manager:
        d = manager.dict()
        l = manager.list(range(10))

        p = Process(target=f, args=(d, l))
        p.start()
        p.join()

        print(d)
        print(l)

将打印

{0.25: None, 1: '1', '2': 2}
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

服务器进程管理器比使用共享内存对象更灵活，因为它们可以支持任意对象类型。此外，单个管理器可以通过网络由不同计算机上的进程共享。但是，它们比使用共享内存慢。

17.2.1.6. 使用工作进程

Pool 类表示一个工作进程池。它具有允许以几种不同方式将任务分配到工作进程的方法。

例如

from multiprocessing import Pool, TimeoutError
import time
import os

def f(x):
    return x*x

if __name__ == '__main__':
    # start 4 worker processes
    with Pool(processes=4) as pool:

        # print "[0, 1, 4,..., 81]"
        print(pool.map(f, range(10)))

        # print same numbers in arbitrary order
        for i in pool.imap_unordered(f, range(10)):
            print(i)

        # evaluate "f(20)" asynchronously
        res = pool.apply_async(f, (20,))      # runs in *only* one process
        print(res.get(timeout=1))             # prints "400"

        # evaluate "os.getpid()" asynchronously
        res = pool.apply_async(os.getpid, ()) # runs in *only* one process
        print(res.get(timeout=1))             # prints the PID of that process

        # launching multiple evaluations asynchronously *may* use more processes
        multiple_results = [pool.apply_async(os.getpid, ()) for i in range(4)]
        print([res.get(timeout=1) for res in multiple_results])

        # make a single worker sleep for 10 secs
        res = pool.apply_async(time.sleep, (10,))
        try:
            print(res.get(timeout=1))
        except TimeoutError:
            print("We lacked patience and got a multiprocessing.TimeoutError")

        print("For the moment, the pool remains available for more work")

    # exiting the 'with'-block has stopped the pool
    print("Now the pool is closed and no longer available")

请注意，池的方法只能由创建它的进程使用。

注解

该软件包中的功能要求子项可以导入 __main__ 模块。这包含在 编程指导 中，但值得指出。这意味着一些示例，例如 multiprocessing.pool.Pool 示例在交互式解释器中不起作用。例如:

>>> from multiprocessing import Pool
>>> p = Pool(5)
>>> def f(x):
...     return x*x
...
>>> p.map(f, [1,2,3])
Process PoolWorker-1:
Process PoolWorker-2:
Process PoolWorker-3:
Traceback (most recent call last):
AttributeError: 'module' object has no attribute 'f'
AttributeError: 'module' object has no attribute 'f'
AttributeError: 'module' object has no attribute 'f'

（如果你尝试这个，它实际上会以半随机的方式输出三个完整的回溯，然后你可能不得不以某种方式停止主进程。）

17.2.2. 参考

multiprocessing 包大部分复制了 threading 模块的API。

17.2.2.1. Process 和异常

class multiprocessing.Process(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None)

进程对象表示在单独进程中运行的活动。 Process 类等价于 threading.Thread 。

应始终使用关键字参数调用构造函数。 group 应该始终是 None ；它仅用于兼容 threading.Thread 。 target 是由 run() 方法调用的可调用对象。它默认为 None ，意味着什么都没有被调用。 name 是进程名称（有关详细信息，请参阅 name ）。 args 是目标调用的参数元组。 kwargs 是目标调用的关键字参数字典。如果提供，则键参数 daemon 将进程 daemon 标志设置为 True 或 False 。如果是 None （默认值），则该标志将从创建的进程继承。

默认情况下，不会将任何参数传递给 target 。

如果子类重写构造函数，它必须确保它在对进程执行任何其他操作之前调用基类构造函数（ Process.__init__() ）。

在 3.3 版更改: 加入 daemon 参数。

run()

表示进程活动的方法。

你可以在子类中重载此方法。标准 run() 方法调用传递给对象构造函数的可调用对象作为目标参数（如果有），分别从 args 和 kwargs 参数中获取顺序和关键字参数。

start()

启动进程活动。

每个进程对象最多只能调用一次。它安排对象的 run() 方法在一个单独的进程中调用。

join([timeout])

如果可选参数 timeout 是 None （默认值），则该方法将阻塞，直到调用 join() 方法的进程终止。如果 timeout 是一个正数，它最多会阻塞 timeout 秒。请注意，如果进程终止或方法超时，则该方法返回 None 。检查进程的 exitcode 以确定它是否终止。

一个进程可以合并多次。

进程无法并入自身，因为这会导致死锁。尝试在启动进程之前合并进程是错误的。

name

进程的名称。该名称是一个字符串，仅用于识别目的。它没有语义。可以为多个进程指定相同的名称。

初始名称由构造器设定。 如果没有为构造器提供显式名称，则会构造一个形式为 ‘Process-N₁:N₂:…:Nk’ 的名称，其中每个 Nk 是其父亲的第 N 个孩子。

is_alive()

返回进程是否还活着。

粗略地说，从 start() 方法返回到子进程终止之前，进程对象仍处于活动状态。

daemon

进程的守护标志，一个布尔值。这必须在 start() 被调用之前设置。

初始值继承自创建进程。

当进程退出时，它会尝试终止其所有守护进程子进程。

请注意，不允许守护进程创建子进程。否则，守护进程会在子进程退出时终止其子进程。 另外，这些 不是 Unix守护进程或服务，它们是正常进程，如果非守护进程已经退出，它们将被终止（并且不被合并）。

除了 threading.Thread API ，Process 对象还支持以下属性和方法：

pid

返回进程ID。在生成该进程之前，这将是 None 。

exitcode

的退子进程出代码。如果进程尚未终止，这将是 None 。负值 -N 表示孩子被信号 N 终止。

authkey

进程的身份验证密钥（字节字符串）。

当 multiprocessing 初始化时，主进程使用 os.urandom() 分配一个随机字符串。

当创建 Process 对象时，它将继承其父进程的身份验证密钥，尽管可以通过将 authkey 设置为另一个字节字符串来更改。

参见 认证密码 。

sentinel

系统对象的数字句柄，当进程结束时将变为 “ready” 。

如果要使用 multiprocessing.connection.wait() 一次等待多个事件，可以使用此值。否则调用 join() 更简单。

在Windows上，这是一个操作系统句柄，可以与 WaitForSingleObject 和 WaitForMultipleObjects 系列API调用一起使用。在Unix上，这是一个文件描述符，可以使用来自 select 模块的原语。

3.3 新版功能.

terminate()

终止进程。 在Unix上，这是使用 SIGTERM 信号完成的；在Windows上使用 TerminateProcess() 。 请注意，不会执行退出处理程序和finally子句等。

请注意，进程的后代进程将不会被终止 —— 它们将简单地变成孤立的。

警告

如果在关联进程使用管道或队列时使用此方法，则管道或队列可能会损坏，并可能无法被其他进程使用。类似地，如果进程已获得锁或信号量等，则终止它可能导致其他进程死锁。

注意 start() 、 join() 、 is_alive() 、 terminate() 和 exitcode 方法只能由创建进程对象的进程调用。

Process 一些方法的示例用法：

>>> import multiprocessing, time, signal
>>> p = multiprocessing.Process(target=time.sleep, args=(1000,))
>>> print(p, p.is_alive())
<Process(Process-1, initial)> False
>>> p.start()
>>> print(p, p.is_alive())
<Process(Process-1, started)> True
>>> p.terminate()
>>> time.sleep(0.1)
>>> print(p, p.is_alive())
<Process(Process-1, stopped[SIGTERM])> False
>>> p.exitcode == -signal.SIGTERM
True

exception multiprocessing.ProcessError

所有 multiprocessing 异常的基类。

exception multiprocessing.BufferTooShort

当提供的缓冲区对象太小而无法读取消息时， Connection.recv_bytes_into() 引发的异常。

如果 e 是一个 BufferTooShort 实例，那么 e.args[0] 将把消息作为字节字符串给出。

exception multiprocessing.AuthenticationError

出现身份验证错误时引发。

exception multiprocessing.TimeoutError

有超时的方法超时时引发。

17.2.2.2. 管道和队列

使用多进程时，一般使用消息机制实现进程间通信，尽可能避免使用同步原语，例如锁。

消息机制包含： Pipe() (可以用于在两个进程间传递消息)，以及队列(能够在多个生产者和消费者之间通信)。

The Queue, SimpleQueue and JoinableQueue types are multi-producer, multi-consumer FIFO queues modelled on the queue.Queue class in the standard library. They differ in that Queue lacks the task_done() and join() methods introduced into Python 2.5’s queue.Queue class.

如果你使用了 JoinableQueue ，那么你**必须**对每个已经移出队列的任务调用 JoinableQueue.task_done() 。不然的话用于统计未完成任务的信号量最终会溢出并抛出异常。

另外还可以通过使用一个管理器对象创建一个共享队列，详见 数据管理器 。

注解

multiprocessing 使用了普通的 queue.Empty 和 queue.Full 异常去表示超时。 你需要从 queue 中导入它们，因为它们并不在 multiprocessing 的命名空间中。

注解

当一个对象被放入一个队列中时，这个对象首先会被一个后台线程用pickle序列化，并将序列化后的数据通过一个底层管道的管道传递到队列中。这种做法会有点让人惊讶，但一般不会出现什么问题。如果它们确实妨碍了你，你可以使用一个由管理器 manager 创建的队列替换它。

将一个对象放入一个空队列后，可能需要极小的延迟，队列的方法 empty()  才会返回 False 。而 get_nowait() 可以不抛出 queue.Empty 直接返回。

如果有多个进程同时将对象放入队列，那么在队列的另一端接受到的对象可能是无序的。但是由同一个进程放入的多个对象的顺序在另一端输出时总是一样的。

警告

如果一个进程通过调用 Process.terminate() 或 os.kill() 在尝试使用 Queue 期间被终止了，那么队列中的数据很可能被破坏。 这可能导致其他进程在尝试使用该队列时遇到异常。

警告

正如刚才提到的，如果一个子进程将一些对象放进队列中 (并且它没有用 JoinableQueue.cancel_join_thread 方法)，那么这个进程在所有缓冲区的对象被刷新进管道之前，是不会终止的。

这意味着，除非你确定所有放入队列中的对象都已经被消费了，否则如果你试图等待这个进程，你可能会陷入死锁中。相似地，如果该子进程不是后台进程，那么父进程可能在试图等待所有非后台进程退出时挂起。

注意用管理器创建的队列不存在这个问题，详见 编程指导 。

该 例子 展示了如何使用队列实现进程间通信。

multiprocessing.Pipe([duplex])

返回一对 Connection`对象  ``(conn1, conn2)` ， 分别表示管道的两端。

如果 duplex 被置为 True (默认值)，那么该管道是双向的。如果 duplex 被置为 False ，那么该管道是单向的，即 conn1 只能用于接收消息，而 conn2 仅能用于发送消息。

class multiprocessing.Queue([maxsize])

返回一个使用一个管道和少量锁和信号量实现的共享队列实例。当一个进程将一个对象放进队列中时，一个写入线程会启动并将对象从缓冲区写入管道中。

一旦超时，将抛出标准库 queue 模块中常见的异常 queue.Empty 和 queue.Full。

除了 task_done() 和 join() 之外，Queue  实现了标准库类 queue.Queue 中所有的方法。

qsize()

返回队列的大致长度。由于多线程或者多进程的上下文，这个数字是不可靠的。

注意，在 Unix 平台上，例如 Mac OS X ，这个方法可能会抛出 NotImplementedError  异常，因为该平台没有实现 sem_getvalue() 。

empty()

如果队列是空的，返回 True ，反之返回 False 。 由于多线程或多进程的环境，该状态是不可靠的。

full()

如果队列是满的，返回 True ，反之返回 False 。 由于多线程或多进程的环境，该状态是不可靠的。

put(obj[, block[, timeout]])

将 obj 放入队列。如果可选参数 block 是 True (默认值) 而且 timeout 是 None (默认值), 将会阻塞当前进程，直到有空的缓冲槽。如果 timeout 是正数，将会在阻塞了最多 timeout 秒之后还是没有可用的缓冲槽时抛出 queue.Full  异常。反之 (block 是 False 时)，仅当有可用缓冲槽时才放入对象，否则抛出 queue.Full 异常 (在这种情形下 timeout 参数会被忽略)。

put_nowait(obj)

相当于 put(obj, False)。

get([block[, timeout]])

从队列中取出并返回对象。如果可选参数 block 是 True (默认值) 而且 timeout 是 None (默认值), 将会阻塞当前进程，直到队列中出现可用的对象。如果 timeout 是正数，将会在阻塞了最多 timeout 秒之后还是没有可用的对象时抛出 queue.Empty 异常。反之 (block 是 False 时)，仅当有可用对象能够取出时返回，否则抛出 queue.Empty 异常 (在这种情形下 timeout 参数会被忽略)。

get_nowait()

相当于 get(False) 。

multiprocessing.Queue 类有一些在 queue.Queue 类中没有出现的方法。这些方法在大多数情形下并不是必须的。

close()

指示当前进程将不会再往队列中放入对象。一旦所有缓冲区中的数据被写入管道之后，后台的线程会退出。这个方法在队列被gc回收时会自动调用。

join_thread()

等待后台线程。这个方法仅在调用了 close() 方法之后可用。这会阻塞当前进程，直到后台线程退出，确保所有缓冲区中的数据都被写入管道中。

默认情况下，如果一个不是队列创建者的进程试图退出，它会尝试等待这个队列的后台线程。这个进程可以使用 cancel_join_thread() 让 join_thread() 方法什么都不做直接跳过。

cancel_join_thread()

防止 join_thread() 方法阻塞当前进程。具体而言，这防止进程退出时自动等待后台线程退出。详见 join_thread()。

可能这个方法称为”allow_exit_without_flush()“ 会更好。这有可能会导致正在排队进入队列的数据丢失，大多数情况下你不需要用到这个方法，仅当你不关心底层管道中可能丢失的数据，只是希望进程能够马上退出时使用。

注解

该类的功能依赖于宿主操作系统具有可用的共享信号量实现。否则该类将被禁用，任何试图实例化一个 Queue 对象的操作都会抛出 ImportError 异常，更多信息详见 bpo-3770 [https://bugs.python.org/issue3770] 。后续说明的任何专用队列对象亦如此。

class multiprocessing.SimpleQueue

这是一个简化的 Queue 类的实现，很像带锁的 Pipe 。

empty()

如果队列为空返回 True ，否则返回 False 。

get()

从队列中移出并返回一个对象。

put(item)

将 item 放入队列。

class multiprocessing.JoinableQueue([maxsize])

JoinableQueue 类是 Queue 的子类，额外添加了 task_done() 和 join() 方法。

task_done()

指出之前进入队列的任务已经完成。由队列的消费者进程使用。对于每次调用 get() 获取的任务，执行完成后调用 task_done() 告诉队列该任务已经处理完成。

如果 join() 方法正在阻塞之中，该方法会在所有对象都被处理完的时候返回 (即对之前使用 put() 放进队列中的所有对象都已经返回了对应的 task_done() ) 。

如果被调用的次数多于放入队列中的项目数量，将引发 ValueError 异常 。

join()

阻塞至队列中所有的元素都被接收和处理完毕。

当条目添加到队列的时候，未完成任务的计数就会增加。每当消费者进程调用 task_done() 表示这个条目已经被回收，该条目所有工作已经完成，未完成计数就会减少。当未完成计数降到零的时候， join() 阻塞被解除。

17.2.2.3. 杂项

multiprocessing.active_children()

返回当前进程存活的子进程的列表。

调用该方法有“等待”已经结束的进程的副作用。

multiprocessing.cpu_count()

返回系统的CPU数量。

该数量不同于当前进程可以使用的CPU数量。可用的CPU数量可以由 len(os.sched_getaffinity(0)) 方法获得。

可能引发 NotImplementedError 。

参见

os.cpu_count()

multiprocessing.current_process()

返回与当前进程相对应的 Process 对象。

和 threading.current_thread() 相同。

multiprocessing.freeze_support()

为使用了 multiprocessing  的程序，提供冻结以产生 Windows 可执行文件的支持。(在 py2exe, PyInstaller 和 cx_Freeze 上测试通过)

需要在 main 模块的 if __name__ == '__main__' 该行之后马上调用该函数。例如：

from multiprocessing import Process, freeze_support

def f():
    print('hello world!')

if __name__ == '__main__':
    freeze_support()
    Process(target=f).start()

如果没有调用 freeze_support() 在尝试运行被冻结的可执行文件时会抛出 RuntimeError 异常。

对 freeze_support() 的调用在非 Windows 平台上是无效的。如果该模块在 Windows 平台的 Python 解释器中正常运行 (该程序没有被冻结)， 调用``freeze_support()`` 也是无效的。

multiprocessing.get_all_start_methods()

返回支持的启动方法的列表，该列表的首项即为默认选项。可能的启动方法有 'fork', 'spawn' 和``’forkserver’。在 Windows 中，只有  ``'spawn' 是可用的。Unix平台总是支持``’fork’`` 和``’spawn’，且 ``'fork' 是默认值。

3.4 新版功能.

multiprocessing.get_context(method=None)

返回一个 Context 对象。该对象具有和 multiprocessing 模块相同的API。

如果 method 设置成 None 那么将返回默认上下文对象。否则 method  应该是 'fork', 'spawn', 'forkserver' 。 如果指定的启动方法不存在，将抛出 ValueError  异常。

3.4 新版功能.

multiprocessing.get_start_method(allow_none=False)

返回启动进程时使用的启动方法名。

如果启动方法已经固定，并且 allow_none 被设置成 False ，那么启动方法将被固定为默认的启动方法，并且返回其方法名。如果启动方法没有设定，并且 allow_none 被设置成 True ，那么将返回 None  。

返回值将为 'fork' , 'spawn' , 'forkserver' 或者 None 。 'fork'``是 Unix 的默认值，   ``'spawn' 是 Windows 的默认值。

3.4 新版功能.

multiprocessing.set_executable()

设置在启动子进程时使用的 Python 解释器路径。 ( 默认使用 sys.executable ) 嵌入式编程人员可能需要这样做：

set_executable(os.path.join(sys.exec_prefix, 'pythonw.exe'))

以使他们可以创建子进程。

在 3.4 版更改: 现在在 Unix 平台上使用 'spawn'  启动方法时支持调用该方法。

multiprocessing.set_start_method(method)

设置启动子进程的方法。 method 可以是 'fork' , 'spawn' 或者 'forkserver' 。

注意这最多只能调用一次，并且需要藏在 main 模块中，由 if __name__ == '__main__'  保护着。

3.4 新版功能.

注解

multiprocessing 并没有包含类似 threading.active_count() , threading.enumerate() , threading.settrace() , threading.setprofile(), threading.Timer , 或者 threading.local 的方法和类。

17.2.2.4. 连接对象（Connection）

Connection 对象允许收发可以序列化的对象或字符串。它们可以看作面向消息的连接套接字。

通常使用 Pipe 创建 Connection 对象。详见 ： 监听者及客户端.

class multiprocessing.connection.Connection

send(obj)

将一个对象发送到连接的另一端，可以用 recv() 读取。

The object must be picklable. Very large pickles (approximately 32 MB+, though it depends on the OS) may raise a ValueError exception.

recv()

返回一个由另一端使用 send() 发送的对象。该方法会一直阻塞直到接收到对象。 如果对端关闭了连接或者没有东西可接收，将抛出 EOFError  异常。

fileno()

返回由连接对象使用的描述符或者句柄。

close()

关闭连接对象。

当连接对象被垃圾回收时会自动调用。

poll([timeout])

返回连接对象中是否有可以读取的数据。

如果未指定 timeout ，此方法会马上返回。如果 timeout 是一个数字，则指定了最大阻塞的秒数。如果 timeout 是 None  ，那么将一直等待，不会超时。

注意通过使用 multiprocessing.connection.wait() 可以一次轮询多个连接对象。

send_bytes(buffer[, offset[, size]])

从一个 bytes-like object  （字节类对象）对象中取出字节数组并作为一条完整消息发送。

If offset is given then data is read from that position in buffer. If size is given then that many bytes will be read from buffer. Very large buffers (approximately 32 MB+, though it depends on the OS) may raise a ValueError exception

recv_bytes([maxlength])

以字符串形式返回一条从连接对象另一端发送过来的字节数据。此方法在接收到数据前将一直阻塞。 如果连接对象被对端关闭或者没有数据可读取，将抛出 EOFError  异常。

如果给定了 maxlength 并且消息长于 maxlength 那么将抛出 OSError 并且该连接对象将不再可读。

在 3.3 版更改: 曾经该函数抛出 IOError ，现在这是 OSError 的别名。

recv_bytes_into(buffer[, offset])

将一条完整的字节数据消息读入 buffer 中并返回消息的字节数。 此方法在接收到数据前将一直阻塞。 如果连接对象被对端关闭或者没有数据可读取，将抛出 EOFError  异常。

buffer must be a writable bytes-like object. If offset is given then the message will be written into the buffer from that position. Offset must be a non-negative integer less than the length of buffer (in bytes).

如果缓冲区太小，则将引发 BufferTooShort  异常，并且完整的消息将会存放在异常实例 e 的 e.args[0] 中。

在 3.3 版更改: 现在连接对象自身可以通过 Connection.send() 和 Connection.recv() 在进程之间传递。

3.3 新版功能: 连接对象现已支持上下文管理协议 – 参见 see 上下文管理器类型 。 __enter__() 返回连接对象， __exit__() 会调用 close() 。

例如:

>>> from multiprocessing import Pipe
>>> a, b = Pipe()
>>> a.send([1, 'hello', None])
>>> b.recv()
[1, 'hello', None]
>>> b.send_bytes(b'thank you')
>>> a.recv_bytes()
b'thank you'
>>> import array
>>> arr1 = array.array('i', range(5))
>>> arr2 = array.array('i', [0] * 10)
>>> a.send_bytes(arr1)
>>> count = b.recv_bytes_into(arr2)
>>> assert count == len(arr1) * arr1.itemsize
>>> arr2
array('i', [0, 1, 2, 3, 4, 0, 0, 0, 0, 0])

警告

Connection.recv() 方法会自动解封它收到的数据，除非你能够信任发送消息的进程，否则此处可能有安全风险。

因此， 除非连接对象是由 Pipe() 产生的，否则你应该仅在使用了某种认证手段之后才使用 recv() 和 send() 方法。 参考 认证密码。

警告

如果一个进程在试图读写管道时被终止了，那么管道中的数据很可能是不完整的，因为此时可能无法确定消息的边界。

17.2.2.5. 同步原语

通常来说同步原语在多进程环境中并不像它们在多线程环境中那么必要。参考 threading 模块的文档。

注意可以使用管理器对象创建同步原语，参考 数据管理器 。

class multiprocessing.Barrier(parties[, action[, timeout]])

类似 threading.Barrier 的栅栏对象。

3.3 新版功能.

class multiprocessing.BoundedSemaphore([value])

非常类似 threading.BoundedSemaphore 的有界信号量对象。

一个小小的不同在于，它的 acquire  方法的第一个参数名是和 Lock.acquire() 一样的 block 。

注解

在 Mac OS X 平台上， 该对象于 Semaphore  不同在于 sem_getvalue() 方法并没有在该平台上实现。

class multiprocessing.Condition([lock])

条件变量： threading.Condition 的别名。

指定的 lock 参数应该是 multiprocessing 模块中的 Lock 或者 RLock 对象。

在 3.3 版更改: 新增了 wait_for()  方法。

class multiprocessing.Event

A clone of threading.Event.

class multiprocessing.Lock

原始锁（非递归锁）对象，类似于 threading.Lock 。一旦一个进程或者线程拿到了锁，后续的任何其他进程或线程的其他请求都会被阻塞直到锁被释放。任何进程或线程都可以释放锁。除非另有说明，否则 multiprocessing.Lock  用于进程或者线程的概念和行为都和 threading.Lock  一致。

注意 Lock 实际上是一个工厂函数。它返回由默认上下文初始化的 multiprocessing.synchronize.Lock  对象。

Lock supports the context manager protocol and thus may be used in with statements.

acquire(block=True, timeout=None)

可以阻塞或非阻塞地获得锁。

如果 block 参数被设为 True ( 默认值 ) ， 对该方法的调用在锁处于释放状态之前都会阻塞，然后将锁设置为锁住状态并返回 True 。需要注意的是第一个参数名与 threading.Lock.acquire() 的不同。

如果 block 参数被设置成 False ，方法的调用将不会阻塞。 如果锁当前处于锁住状态，将返回 False ； 否则将锁设置成锁住状态，并返回 True 。

当 timeout 是一个正浮点数时，会在等待锁的过程中最多阻塞等待 timeout 秒，当 timeout 是负数时，效果和 timeout 为0时一样，当 timeout 是 None （默认值）时，等待时间是无限长。需要注意的是，对于 timeout 是负数和 None 的情况, 其行为与 threading.Lock.acquire() 是不一样的。当 block 参数为 False 时， timeout  并没有实际用处，会直接忽略， 当 block 参数为 True 时，函数会在拿到锁后返回 True 或者 超时没拿到锁后返回 False 。

release()

释放锁，可以在任何进程、线程使用，并不限于锁的拥有者。

其行为与 threading.Lock.release() 一样，只不过当尝试释放一个没有被持有的锁时，会抛出 ValueError 异常。

class multiprocessing.RLock

递归锁对象: 类似于 threading.RLock 。递归锁必须由持有线程、进程亲自释放。如果某个进程或者线程拿到了递归锁，这个进程或者线程可以再次拿到这个锁而不需要等待。但是这个进程或者线程的拿锁操作和释放锁操作的次数必须相同。

注意 RLock 是一个工厂函数，调用后返回一个使用默认 context 初始化的 multiprocessing.synchronize.RLock 对象。

RLock 支持 context manager 协议，因此可在 with 语句内使用。

acquire(block=True, timeout=None)

可以阻塞或非阻塞地获得锁。

当 block 设置为 True 时，会一直阻塞直到锁处于空闲状态（没有被任何进程、线程拥有），除非当前进程/线程已经拥有了这把锁。然后当前进程会持有这把锁（在锁没有被持有者的情况下），锁内的递归等级加一，并返回 True . 注意， 这个函数第一个参数和 threading.RLock.acquire() 有几个不同点，包括参数名本身。

当 block 参数是 False , 将不会阻塞，如果此时锁被其他进程或者线程持有，当前进程、线程获取锁操作失败，锁的递归等级也不会改变，函数返回 False , 如果当前锁已经处于释放状态，则当前进程、线程则会拿到锁，并且锁内的递归等级加一，函数返回 True 。

timeout 参数的使用方法及行为与 Lock.acquire() 一样。但是要注意 timeout 的其中一些行为和 threading.RLock.acquire() 中实现的行为是不同的。

release()

释放锁，使锁内的递归等级减一。如果释放后锁内的递归等级降低为0，则会重置锁的状态为释放状态（即没有被任何进程、线程持有），重置后如果有有其他进程和线程在等待这把锁，他们中的一个会获得这个锁而继续运行。如果释放后锁内的递归等级还没到达0，则这个锁仍将保持未释放状态且当前进程和线程仍然是持有者。

只有当前进程或线程是锁的持有者时，才允许调用这个方法。如果当前进程或线程不是这个锁的拥有者，或者这个锁处于已释放的状态(即没有任何拥有者)，调用这个方法会抛出 AssertionError 异常。注意这里抛出的异常类型和 threading.RLock.release() 中实现的行为不一样。

class multiprocessing.Semaphore([value])

一种信号量对象: 类似于 threading.Semaphore.

一个小小的不同在于，它的 acquire  方法的第一个参数名是和 Lock.acquire() 一样的 block 。

注解

在 Mac OS X 上，不支持 sem_timedwait ，所以，使用调用 acquire() 时如果使用 timeout 参数，会通过循环sleep来模拟timeout的行为。

注解

假如信号 SIGINT 是来自于 Ctrl-C ，并且主线程被 BoundedSemaphore.acquire(), Lock.acquire(), RLock.acquire(), Semaphore.acquire(), Condition.acquire() 或 Condition.wait() 阻塞，则调用会立即中断同时抛出 KeyboardInterrupt 异常。

这和 threading 的行为不同，此模块中当执行对应的阻塞式调用时，SIGINT 会被忽略。

注解

这个库的某些功能依赖于宿主机系统的共享信号量，如果系统没有这个特性， multiprocessing.synchronize 会被禁用，尝试导入这个包会引发 ImportError 异常，详细信息请查看 bpo-3770 [https://bugs.python.org/issue3770] 。

17.2.2.6. 共享 ctypes 对象

在共享内存上创建可被子进程继承的共享对象时是可行的。

multiprocessing.Value(typecode_or_type, *args, lock=True)

返回一个从共享内存上创建的 ctypes 对象。默认情况下返回的实际上是经过了同步包装器包装过的。可以通过 Value 的 value 属性访问这个对象本身。

typecode_or_type 指明了返回的对象类型: 它可能是一个 ctypes 类型或者 array  模块中每个类型对应的单字符长度的字符串。 *args 会透传给这个类的构造函数。

如果 lock 参数是 True （默认值）, 将会新建一个递归锁用于同步对于此值的访问操作。 如果 lock 是 Lock 或者 RLock 对象，那么这个传入的锁将会用于同步对这个值的访问操作，如果 lock 是 False , 那么对这个对象的访问将没有锁保护，也就是说这个变量不是进程安全的。

诸如 += 这类的操作会引发独立的读操作和写操作，也就是说这类操作符并不具有原子性。所以，如果你想让递增操作具有原子性，这样的方式并不能达到要求:

counter.value += 1

假设共享对象内部关联的锁时递归锁(默认情况下就是)， 那么你可以采用这种方式

with counter.get_lock():
    counter.value += 1

注意 locl 只能是命名参数。

multiprocessing.Array(typecode_or_type, size_or_initializer, *, lock=True)

从共享内存中申请并返回一个具有ctypes类型的数组对象。默认情况下返回值实际上是被同步器包装过的数组对象。

typecode_or_type 指明了返回的数组中的元素类型: 它可能是一个 ctypes 类型或者 array 模块中每个类型对应的单字符长度的字符串。 如果 size_or_initializer 是一个整数，那就会当做数组的长度，并且整个数组的内存会初始化为0。否则，如果 size_or_initializer 会被当成一个序列用于初始化数组中的内一个元素，并且会根据元素个数自动判断数组的长度。

如果 lock 为 True (默认值) 则将创建一个新的锁对象用于同步对值的访问。 如果 lock 为一个 Lock 或 RLock 对象则该对象将被用于同步对值的访问。 如果 lock 为 False 则对返回对象的访问将不会自动得到锁的保护，也就是说它不是“进程安全的”。

请注意 lock 是一个仅限关键字参数。

请注意 ctypes.c_char 的数组具有 value 和 raw 属性，允许被用来保存和提取字符串。

17.2.2.6.1. multiprocessing.sharedctypes 模块

multiprocessing.sharedctypes 模块提供了一些函数，用于分配来自共享内存的、可被子进程继承的 ctypes 对象。

注解

虽然可以将指针存储在共享内存中，但请记住它所引用的是特定进程地址空间中的位置。 而且，指针很可能在第二个进程的上下文中无效，尝试从第二个进程对指针进行解引用可能会导致崩溃。

multiprocessing.sharedctypes.RawArray(typecode_or_type, size_or_initializer)

从共享内存中申请并返回一个 ctypes 数组。

typecode_or_type 指明了返回的数组中的元素类型: 它可能是一个 ctypes 类型或者 array 模块中使用的类型字符。 如果 size_or_initializer 是一个整数，那就会当做数组的长度，并且整个数组的内存会初始化为0。否则，如果 size_or_initializer 会被当成一个序列用于初始化数组中的每一个元素，并且会根据元素个数自动判断数组的长度。

注意对元素的访问、赋值操作可能是非原子操作 - 使用 Array() 来借助其中的锁保证操作的原子性。

multiprocessing.sharedctypes.RawValue(typecode_or_type, *args)

从共享内存中申请并返回一个 ctypes 对象。

typecode_or_type 指明了返回的对象类型: 它可能是一个 ctypes 类型或者 array  模块中每个类型对应的单字符长度的字符串。 *args 会透传给这个类的构造函数。

注意对 value 的访问、赋值操作可能是非原子操作 - 使用 Value() 来借助其中的锁保证操作的原子性。

请注意 ctypes.c_char 的数组具有 value 和 raw 属性，允许被用来保存和提取字符串 - 请查看 ctypes 文档。

multiprocessing.sharedctypes.Array(typecode_or_type, size_or_initializer, *, lock=True)

返回一个纯 ctypes 数组, 或者在此之上经过同步器包装过的对象，这取决于 lock 参数的值，除此之外，和 RawArray() 一样。

如果 lock 为 True (默认值) 则将创建一个新的锁对象用于同步对值的访问。 如果 lock 为一个 Lock 或 RLock 对象则该对象将被用于同步对值的访问。 如果 lock 为 False 则对所返回对象的访问将不会自动得到锁的保护，也就是说它将不是“进程安全的”。

注意 locl 只能是命名参数。

multiprocessing.sharedctypes.Value(typecode_or_type, *args, lock=True)

返回一个纯 ctypes 数组, 或者在此之上经过同步器包装过的进程安全的对象，这取决于 lock 参数的值，除此之外，和 RawArray() 一样。

如果 lock 为 True (默认值) 则将创建一个新的锁对象用于同步对值的访问。 如果 lock 为一个 Lock 或 RLock 对象则该对象将被用于同步对值的访问。 如果 lock 为 False 则对所返回对象的访问将不会自动得到锁的保护，也就是说它将不是“进程安全的”。

注意 locl 只能是命名参数。

multiprocessing.sharedctypes.copy(obj)

从共享内存中申请一片空间将 ctypes 对象 obj 过来，然后返回一个新的 ctypes 对象。

multiprocessing.sharedctypes.synchronized(obj[, lock])

将一个 ctypes 对象包装为进程安全的对象并返回，使用 lock 同步对于它的操作。如果 lock 是 None (默认值) ，则会自动创建一个 multiprocessing.RLock 对象。

同步器包装后的对象会在原有对象基础上额外增加两个方法: get_obj() 返回被包装的对象， get_lock() 返回内部用于同步的锁。

需要注意的是，访问包装后的ctypes对象会比直接访问原来的纯 ctypes 对象慢得多。

在 3.5 版更改: 同步器包装后的对象支持 context manager 协议。

下面的表格对比了创建普通ctypes对象和基于共享内存上创建共享ctypes对象的语法。（表格中的 MyStruct 是 ctypes.Structure 的子类）

ctypes

使用类型的共享ctypes

使用 typecode 的共享 ctypes

c_double(2.4)

RawValue(c_double, 2.4)

RawValue(‘d’, 2.4)

MyStruct(4, 6)

RawValue(MyStruct, 4, 6)

(c_short * 7)()

RawArray(c_short, 7)

RawArray(‘h’, 7)

(c_int * 3)(9, 2, 8)

RawArray(c_int, (9, 2, 8))

RawArray(‘i’, (9, 2, 8))

下面是一个在子进程中修改多个ctypes对象的例子。

from multiprocessing import Process, Lock
from multiprocessing.sharedctypes import Value, Array
from ctypes import Structure, c_double

class Point(Structure):
    _fields_ = [('x', c_double), ('y', c_double)]

def modify(n, x, s, A):
    n.value **= 2
    x.value **= 2
    s.value = s.value.upper()
    for a in A:
        a.x **= 2
        a.y **= 2

if __name__ == '__main__':
    lock = Lock()

    n = Value('i', 7)
    x = Value(c_double, 1.0/3.0, lock=False)
    s = Array('c', b'hello world', lock=lock)
    A = Array(Point, [(1.875,-6.25), (-5.75,2.0), (2.375,9.5)], lock=lock)

    p = Process(target=modify, args=(n, x, s, A))
    p.start()
    p.join()

    print(n.value)
    print(x.value)
    print(s.value)
    print([(a.x, a.y) for a in A])

输出如下

49
0.1111111111111111
HELLO WORLD
[(3.515625, 39.0625), (33.0625, 4.0), (5.640625, 90.25)]

17.2.2.7. 数据管理器

管理器提供了一种创建共享数据的方法，从而可以在不同进程中共享，甚至可以通过网络跨机器共享数据。管理器维护一个用于管理 共享对象 的服务。其他进程可以通过代理访问这些共享对象。

multiprocessing.Manager()

返回一个已启动的 SyncManager 管理器对象，这个对象可以用于在不同进程中共享数据。返回的管理器对象对应了一个 spawned 方式启动的子进程，并且拥有一系列方法可以用于创建共享对象、返回对应的代理。

当管理器被垃圾回收或者父进程退出时，管理器进程会立即退出。管理器类定义在 multiprocessing.managers 模块:

class multiprocessing.managers.BaseManager([address[, authkey]])

创建一个 BaseManager 对象。

一旦创建，应该及时调用 start() 或者 get_server().serve_forever() 以确保管理器对象对应的管理进程已经启动。

address 是管理器对象监听的地址。如果 address 是 None ,则允许和任意主机的请求建立连接。

authkey 是认证标识，用于检查连接服务进程的请求合法性。如果 authkey 是 None, 则会使用 current_process().authkey , 否则，就使用 authkey , 需要保证它必须是 byte 类型的字符串。

start([initializer[, initargs]])

为管理器开启一个子进程，如果 initializer 不是 None , 子进程在启动时将会调用 initializer(*initargs) 。

get_server()

返回一个 Server  对象，它是管理器在后台控制的真实的服务。 Server  对象拥有 serve_forever() 方法。

>>> from multiprocessing.managers import BaseManager
>>> manager = BaseManager(address=('', 50000), authkey=b'abc')
>>> server = manager.get_server()
>>> server.serve_forever()

Server 额外拥有一个 address 属性。

connect()

将本地管理器对象连接到一个远程管理器进程:

>>> from multiprocessing.managers import BaseManager
>>> m = BaseManager(address=('127.0.0.1', 5000), authkey=b'abc')
>>> m.connect()

shutdown()

停止管理器的进程。这个方法只能用于已经使用 start() 启动的服务进程。

它可以被多次调用。

register(typeid[, callable[, proxytype[, exposed[, method_to_typeid[, create_method]]]]])

一个 classmethod，可以将一个类型或者可调用对象注册到管理器类。

typeid 是一种 “类型标识”，用于唯一表示某种共享类型，必须是一个字符串。

callable 是一个用来为此类型标识符创建对象的可调用对象。如果一个管理器实例将使用 connect() 方法连接到服务器，或者 create_method 参数为 False，那么这里可留下 None。

proxytype 是 BaseProxy  的子类，可以根据 typeid 为共享对象创建一个代理，如果是 None , 则会自动创建一个代理类。

exposed 是一个函数名组成的序列，用来指明只有这些方法可以使用 BaseProxy._callmethod() 代理。(如果 exposed 是 None, 则会在 proxytype._exposed_ 存在的情况下转而使用它) 当暴露的方法列表没有指定的时候，共享对象的所有 “公共方法” 都会被代理。（这里的“公共方法”是指所有拥有 __call__() 方法并且不是以 '_' 开头的属性）

method_to_typeid 是一个映射，用来指定那些应该返回代理对象的暴露方法所返回的类型。（如果 method_to_typeid 是 None, 则 proxytype._method_to_typeid_ 会在存在的情况下被使用）如果方法名称不在这个映射中或者映射是 None ,则方法返回的对象会是一个值拷贝。

create_method 指明，是否要创建一个以 typeid 命名并返回一个代理对象的函数，这个函数会被服务进程用于创建共享对象，默认为 True 。

BaseManager 实例也有一个只读属性。

address

管理器所用的地址。

在 3.3 版更改: 管理器对象支持上下文管理协议 - 查看 上下文管理器类型 。__enter__() 启动服务进程（如果它还没有启动）并且返回管理器对象， __exit__() 会调用 shutdown() 。

在之前的版本中，如果管理器服务进程没有启动， __enter__() 不会负责启动它。

class multiprocessing.managers.SyncManager

BaseManager 的子类，可用于进程的同步。这个类型的对象使用 multiprocessing.Manager() 创建。

它拥有一系列方法，可以为大部分常用数据类型创建并返回 代理对象 代理，用于进程间同步。甚至包括共享列表和字典。

Barrier(parties[, action[, timeout]])

创建一个共享的 threading.Barrier 对象并返回它的代理。

3.3 新版功能.

BoundedSemaphore([value])

创建一个共享的 threading.BoundedSemaphore 对象并返回它的代理。

Condition([lock])

创建一个共享的 threading.Condition 对象并返回它的代理。

如果提供了 lock 参数，那它必须是 threading.Lock 或 threading.RLock 的代理对象。

在 3.3 版更改: 新增了 wait_for()  方法。

Event()

创建一个共享的 threading.Event 对象并返回它的代理。

Lock()

创建一个共享的 threading.Lock 对象并返回它的代理。

Namespace()

创建一个共享的 Namespace` 对象并返回它的代理。

Queue([maxsize])

创建一个共享的 queue.Queue 对象并返回它的代理。

RLock()

创建一个共享的 threading.RLock 对象并返回它的代理。

Semaphore([value])

创建一个共享的 threading.Semaphore 对象并返回它的代理。

Array(typecode, sequence)

创建一个数组并返回它的代理。

Value(typecode, value)

创建一个具有可写 value 属性的对象并返回它的代理。

dict()

dict(mapping)

dict(sequence)

创建一个共享的 dict 对象并返回它的代理。

list()

list(sequence)

创建一个共享的 list 对象并返回它的代理。

在 3.6 版更改: 共享对象能够嵌套。例如, 共享的容器对象如共享列表，可以包含另一个共享对象，他们全都会在 SyncManager 中进行管理和同步。

class multiprocessing.managers.Namespace

一个可以注册到 SyncManager 的类型。

命名空间对象没有公共方法，但是拥有可写的属性。它的表示(repr)会显示所有属性的值。

值得一提的是，当对命名空间对象使用代理的时候，访问所有名称以 '_' 开头的属性都只是代理器上的属性，而不是命名空间对象的属性。

>>> manager = multiprocessing.Manager()
>>> Global = manager.Namespace()
>>> Global.x = 10
>>> Global.y = 'hello'
>>> Global._z = 12.3    # this is an attribute of the proxy
>>> print(Global)
Namespace(x=10, y='hello')

17.2.2.7.1. 自定义管理器

要创建一个自定义的管理器，需要新建一个 BaseManager 的子类，然后使用这个管理器类上的 register() 类方法将新类型或者可调用方法注册上去。例如:

from multiprocessing.managers import BaseManager

class MathsClass:
    def add(self, x, y):
        return x + y
    def mul(self, x, y):
        return x * y

class MyManager(BaseManager):
    pass

MyManager.register('Maths', MathsClass)

if __name__ == '__main__':
    with MyManager() as manager:
        maths = manager.Maths()
        print(maths.add(4, 3))         # prints 7
        print(maths.mul(7, 8))         # prints 56

17.2.2.7.2. 使用远程管理器

可以将管理器服务运行在一台机器上，然后使用客户端从其他机器上访问。(假设它们的防火墙允许这样的网络通信)

运行下面的代码可以启动一个服务，此付包含了一个共享队列，允许远程客户端访问:

>>> from multiprocessing.managers import BaseManager
>>> from queue import Queue
>>> queue = Queue()
>>> class QueueManager(BaseManager): pass
>>> QueueManager.register('get_queue', callable=lambda:queue)
>>> m = QueueManager(address=('', 50000), authkey=b'abracadabra')
>>> s = m.get_server()
>>> s.serve_forever()

远程客户端可以通过下面的方式访问服务:

>>> from multiprocessing.managers import BaseManager
>>> class QueueManager(BaseManager): pass
>>> QueueManager.register('get_queue')
>>> m = QueueManager(address=('foo.bar.org', 50000), authkey=b'abracadabra')
>>> m.connect()
>>> queue = m.get_queue()
>>> queue.put('hello')

也可以通过下面的方式:

>>> from multiprocessing.managers import BaseManager
>>> class QueueManager(BaseManager): pass
>>> QueueManager.register('get_queue')
>>> m = QueueManager(address=('foo.bar.org', 50000), authkey=b'abracadabra')
>>> m.connect()
>>> queue = m.get_queue()
>>> queue.get()
'hello'

本地进程也可以访问这个队列，利用上面的客户端代码通过远程方式访问:

>>> from multiprocessing import Process, Queue
>>> from multiprocessing.managers import BaseManager
>>> class Worker(Process):
...     def __init__(self, q):
...         self.q = q
...         super(Worker, self).__init__()
...     def run(self):
...         self.q.put('local hello')
...
>>> queue = Queue()
>>> w = Worker(queue)
>>> w.start()
>>> class QueueManager(BaseManager): pass
...
>>> QueueManager.register('get_queue', callable=lambda: queue)
>>> m = QueueManager(address=('', 50000), authkey=b'abracadabra')
>>> s = m.get_server()
>>> s.serve_forever()

17.2.2.8. 代理对象

代理是一个 指向 其他共享对象的对象，这个对象(很可能)在另外一个进程中。共享对象也可以说是代理 指涉 的对象。多个代理对象可能指向同一个指涉对象。

代理对象代理了指涉对象的一系列方法调用(虽然并不是指涉对象的每个方法都有必要被代理)。通过这种方式，代理的使用方法可以和它的指涉对象一样:

>>> from multiprocessing import Manager
>>> manager = Manager()
>>> l = manager.list([i*i for i in range(10)])
>>> print(l)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> print(repr(l))
<ListProxy object, typeid 'list' at 0x...>
>>> l[4]
16
>>> l[2:5]
[4, 9, 16]

注意，对代理使用 str() 函数会返回指涉对象的字符串表示，但是 repr() 却会返回代理本身的内部字符串表示。

被代理的对象很重要的一点是必须可以被序列化，这样才能允许他们在进程间传递。因此，指涉对象可以包含 代理对象 。这允许管理器中列表、字典或者其他 代理对象 对象之间的嵌套。

>>> a = manager.list()
>>> b = manager.list()
>>> a.append(b)         # referent of a now contains referent of b
>>> print(a, b)
[<ListProxy object, typeid 'list' at ...>] []
>>> b.append('hello')
>>> print(a[0], b)
['hello'] ['hello']

类似地，字典和列表代理也可以相互嵌套:

>>> l_outer = manager.list([ manager.dict() for i in range(2) ])
>>> d_first_inner = l_outer[0]
>>> d_first_inner['a'] = 1
>>> d_first_inner['b'] = 2
>>> l_outer[1]['c'] = 3
>>> l_outer[1]['z'] = 26
>>> print(l_outer[0])
{'a': 1, 'b': 2}
>>> print(l_outer[1])
{'c': 3, 'z': 26}

如果指涉对象包含了普通 list 或 dict 对象，对这些内部可变对象的修改不会通过管理器传播，因为代理无法得知被包含的值什么时候被修改了。但是把存放在容器代理中的值本身是会通过管理器传播的（会触发代理对象中的 __setitem__ ）从而有效修改这些对象，所以可以把修改过的值重新赋值给容器代理:

# create a list proxy and append a mutable object (a dictionary)
lproxy = manager.list()
lproxy.append({})
# now mutate the dictionary
d = lproxy[0]
d['a'] = 1
d['b'] = 2
# at this point, the changes to d are not yet synced, but by
# updating the dictionary, the proxy is notified of the change
lproxy[0] = d

在大多是使用情形下，这种实现方式并不比嵌套 代理对象 方便，但是依然演示了对于同步的一种控制级别。

注解

multiprocessing 中的代理类并没有提供任何对于代理值比较的支持。所以，我们会得到如下结果:

>>> manager.list([1,2,3]) == [1,2,3]
False

当需要比较值的时候，应该替换为使用指涉对象的拷贝。

class multiprocessing.managers.BaseProxy

代理对象是 BaseProxy 派生类的实例。

_callmethod(methodname[, args[, kwds]])

调用指涉对象的方法并返回结果。

如果 proxy 是一个代理且其指涉的是 obj , 那么下面的表达式:

proxy._callmethod(methodname, args, kwds)

相当于求取以下表达式的值:

getattr(obj, methodname)(*args, **kwds)

于管理器进程。

返回结果会是一个值拷贝或者一个新的共享对象的代理 - 见函数 BaseManager.register() 中关于参数 method_to_typeid 的文档。

如果这个调用熬出了异常，则这个异常会被 _callmethod() 透传出来。如果是管理器进程本身抛出的一些其他异常，则会被 _callmethod() 转换为 RemoteError 异常重新抛出。

特别注意，如果 methodname 没有 暴露 出来，将会引发一个异常。

_callmethod() 的一个使用示例:

>>> l = manager.list(range(10))
>>> l._callmethod('__len__')
10
>>> l._callmethod('__getitem__', (slice(2, 7),)) # equivalent to l[2:7]
[2, 3, 4, 5, 6]
>>> l._callmethod('__getitem__', (20,))          # equivalent to l[20]
Traceback (most recent call last):
...
IndexError: list index out of range

_getvalue()

返回指涉对象的一份拷贝。

如果指涉对象无法序列化，则会抛出一个异常。

__repr__()

返回代理对象的字符串表示。

__str__()

返回指涉对象的字符串表示。

17.2.2.8.1. 清理

代理对象使用了一个弱引用回调，当它被垃圾回收时，会将自己从拥有此指涉对象的管理器上反注册，

当共享对象没有被任何代理器引用时，会被管理器进程删除。

17.2.2.9. 进程池

可以创建一个进程池，它将使用 Pool 类执行提交给它的任务。

class multiprocessing.pool.Pool([processes[, initializer[, initargs[, maxtasksperchild[, context]]]]])

一个进程池对象，它控制可以提交作业的工作进程池。它支持带有超时和回调的异步结果，以及一个并行的 map 实现。

processes 是要使用的工作进程数目。如果 processes 为 None，则使用 os.cpu_count() 返回的值。

如果 initializer 不为 None，则每个工作进程将会在启动时调用 initializer(*initargs)。

maxtasksperchild 是一个工作进程在它退出或被一个新的工作进程代替之前能完成的任务数量，为了释放未使用的资源。默认的 maxtasksperchild 是 None，意味着工作进程寿与池齐。

context 可被用于指定启动的工作进程的上下文。通常一个进程池是使用函数 multiprocessing.Pool() 或者一个上下文对象的 Pool() 方法创建的。在这两种情况下， context 都是适当设置的。

注意，进程池对象的方法只有创建它的进程能够调用。

3.2 新版功能: maxtasksperchild

3.4 新版功能: context

注解

通常来说，Pool 中的 Worker 进程的生命周期和进程池的工作队列一样长。一些其他系统中（如 Apache, mod_wsgi 等）也可以发现另一种模式，他们会让工作进程在完成一些任务后退出，清理、释放资源，然后启动一个新的进程代替旧的工作进程。 Pool 的 maxtasksperchild 参数给用户提供了这种能力。

apply(func[, args[, kwds]])

使用 args 参数以及 kwds 命名参数调用 func , 它会返回结果前阻塞。这种情况下，apply_async() 更适合并行化工作。另外 func 只会在一个进程池中的一个工作进程中执行。

apply_async(func[, args[, kwds[, callback[, error_callback]]]])

apply() 方法的一个变种，返回一个结果对象。

如果指定了 callback , 它必须是一个接受单个参数的可调用对象。当执行成功时， callback 会被用于处理执行后的返回结果，否则，调用 error_callback 。

如果指定了 error_callback , 它必须是一个接受单个参数的可调用对象。当目标函数执行失败时， 会将抛出的异常对象作为参数传递给 error_callback 执行。

回调函数应该立即执行完成，否则会阻塞负责处理结果的线程。

map(func, iterable[, chunksize])

A parallel equivalent of the map() built-in function (it supports only one iterable argument though). It blocks until the result is ready.

这个方法会将可迭代对象分割为许多块，然后提交给进程池。可以将 chunksize 设置为一个正整数从而（近似）指定每个块的大小可以。

map_async(func, iterable[, chunksize[, callback[, error_callback]]])

和 map() 方法类似，但是返回一个结果对象。

如果指定了 callback , 它必须是一个接受单个参数的可调用对象。当执行成功时， callback 会被用于处理执行后的返回结果，否则，调用 error_callback 。

如果指定了 error_callback , 它必须是一个接受单个参数的可调用对象。当目标函数执行失败时， 会将抛出的异常对象作为参数传递给 error_callback 执行。

回调函数应该立即执行完成，否则会阻塞负责处理结果的线程。

imap(func, iterable[, chunksize])

A lazier version of map().

chunksize 参数的作用和 map() 方法的一样。对于很长的迭代器，给 chunksize 设置一个很大的值会比默认值 1 极大 地加快执行速度。

同样，如果 chunksize 是 1 , 那么 imap() 方法所返回的迭代器的 next() 方法拥有一个可选的 timeout 参数： 如果无法在 timeout 秒内执行得到结果，则``next(timeout)`` 会抛出 multiprocessing.TimeoutError 异常。

imap_unordered(func, iterable[, chunksize])

和 imap() 相同，只不过通过迭代器返回的结果是任意的。（当进程池中只有一个工作进程的时候，返回结果的顺序才能认为是”有序”的）

starmap(func, iterable[, chunksize])

和 map() 类似，不过 iterable 中的每一项会被解包再作为函数参数。

比如可迭代对象 [(1,2), (3, 4)] 会转化为等价于 [func(1,2), func(3,4)] 的调用。

3.3 新版功能.

starmap_async(func, iterable[, chunksize[, callback[, error_callback]]])

相当于 starmap() 与 map_async() 的结合，迭代 iterable 的每一项，解包作为 func 的参数并执行，返回用于获取结果的对象。

3.3 新版功能.

close()

阻止后续任务提交到进程池，当所有任务执行完成后，工作进程会退出。

terminate()

不必等待未完成的任务，立即停止工作进程。当进程池对象呗垃圾回收时， terminate() 会立即调用。

join()

等待工作进程结束。调用 join() 前必须先调用 close() 或者 terminate() 。

3.3 新版功能: 进程池对象现在支持上下文管理器协议 - 参见 上下文管理器类型 。__enter__() 返回进程池对象, __exit__() 会调用 terminate() 。

class multiprocessing.pool.AsyncResult

Pool.apply_async() 和 Pool.map_async() 返回对象所属的类。

get([timeout])

用于获取执行结果。如果 timeout 不是 None 并且在 timeout 秒内仍然没有执行完得到结果，则抛出 multiprocessing.TimeoutError 异常。如果远程调用发生异常，这个异常会通过 get() 重新抛出。

wait([timeout])

阻塞，直到返回结果，或者 timeout 秒后超时。

ready()

用于判断执行状态，是否已经完成。

successful()

Return whether the call completed without raising an exception. Will raise AssertionError if the result is not ready.

下面的例子演示了进程池的用法:

from multiprocessing import Pool
import time

def f(x):
    return x*x

if __name__ == '__main__':
    with Pool(processes=4) as pool:         # start 4 worker processes
        result = pool.apply_async(f, (10,)) # evaluate "f(10)" asynchronously in a single process
        print(result.get(timeout=1))        # prints "100" unless your computer is *very* slow

        print(pool.map(f, range(10)))       # prints "[0, 1, 4,..., 81]"

        it = pool.imap(f, range(10))
        print(next(it))                     # prints "0"
        print(next(it))                     # prints "1"
        print(it.next(timeout=1))           # prints "4" unless your computer is *very* slow

        result = pool.apply_async(time.sleep, (10,))
        print(result.get(timeout=1))        # raises multiprocessing.TimeoutError

17.2.2.10. 监听者及客户端

通常情况下，进程间通过队列或者 Pipe() 返回的 Connection 传递消息。

不过，multiprocessing.connection 其实提供了一些更灵活的特性。最基础的用法是通过它抽象出来的高级来操作socket或者Windows命名管道。也提供一些高级用法，如通过 hmac 模块来支持 摘要认证，以及同时监听多个管道连接。

multiprocessing.connection.deliver_challenge(connection, authkey)

发送一个随机生成的消息到另一端，并等待回复。

如果收到的回复与使用 authkey 生成的信息摘要匹配成功，就会发送一个欢迎信息给管道另一端。否则抛出 AuthenticationError 异常。

multiprocessing.connection.answer_challenge(connection, authkey)

接收一条信息，使用 authkey 作为键计算信息摘要，然后将摘要发送回去。

如果没有收到欢迎消息，就抛出 AuthenticationError 异常。

multiprocessing.connection.Client(address[, family[, authkey]])

尝试在监听者上使用 address 地址初始化一个连接，返回 Connection 。

连接的类型取决于 family 参数，但是通常可以省略，因为可以通过 address 的格式推导出来。(查看 地址格式 )

如果提供了 authkey 参数并且不是 None，那它必须是一个字符串并且会被当做基于 HMAC 认证的密钥。如果 authkey 是None 则不会有认证行为。认证失败抛出 AuthenticationError 异常，请查看 See 认证密码 。

class multiprocessing.connection.Listener([address[, family[, backlog[, authkey]]]])

可以监听连接请求，是对于绑定套接字或者 Windows 命名管道的封装。

address 是监听器对象中的绑定套接字或命名管道使用的地址。

注解

如果使用 ‘0.0.0.0’ 作为监听地址，那么在Windows上这个地址无法建立连接。想要建立一个可连接的端点，应该使用 ‘127.0.0.1’ 。

family 是套接字(或者命名管道)使用的类型。它可以是以下一种: 'AF_INET' ( TCP 套接字类型), 'AF_UNIX' ( Unix 域套接字) 或者 'AF_PIPE' ( Windows 命名管道)。其中只有第一个保证各平台可用。如果 family 是 None ,那么 family 会根据 address 的格式自动推导出来。如果 address 也是 None , 则取默认值。默认值为可用类型中速度最快的。见 地址格式 。注意，如果 family 是 'AF_UNIX' 而address是``None`` ,套接字会在一个 tempfile.mkstemp() 创建的私有临时目录中创建。

如果监听器对象使用了套接字，backlog (默认值为1) 会在套接字绑定后传递给它的 listen() 方法。

如果提供了 authkey 参数并且不是 None，那它必须是一个字符串并且会被当做基于 HMAC 认证的密钥。如果 authkey 是None 则不会有认证行为。认证失败抛出 AuthenticationError 异常，请查看 See 认证密码 。

accept()

接受一个连接并返回一个 Connection 对象，其连接到的监听器对象已绑定套接字或者命名管道。如果已经尝试过认证并且失败了，则会抛出 AuthenticationError 异常。

close()

关闭监听器上的绑定套接字或者命名管道。此函数会在监听器被垃圾回收后自动调用。不过仍然建议显式调用函数关闭。

监听器对象拥有下列只读属性:

address

被监听器对象使用的地址。

last_accepted

最后一个连接所使用的地址。如果没有的话就是 None 。

3.3 新版功能: 监听器对象现在支持了上下文管理协议 - 见 上下文管理器类型 。 __enter__() 返回一个监听器对象, __exit__() 会调用 close() 。

multiprocessing.connection.wait(object_list, timeout=None)

一直等待直到 object_list 中某个对象处于就绪状态。返回 object_list 中处于就绪状态的对象。如果 timeout 是一个浮点型，该方法会最多阻塞这么多秒。如果 timeout 是 None ，则会允许阻塞的事件没有限制。timeout为负数的情况下和为0的情况相同。

对于 Unix 和 Windows ，下列对象都可以出现在 object_list 中

可读的 Connection 对象；

一个已连接并且可读的 socket.socket 对象；或者

Process 对象中的 sentinel 属性。

当一个连接或者套接字对象拥有有效的数据可被读取的时候，或者另一端关闭后，这个对象就处于就绪状态。

Unix: wait(object_list, timeout) 和 select.select(object_list, [], [], timeout) 几乎相同。差别在于，如果 select.select() 被信号中断，它会抛出一个附带错误号为 EINTR 的 OSError 异常，而 wait() 不会。

Windows: object_list 中的元素必须是一个表示为整数的可等待的句柄(按照 Win32 函数 WaitForMultipleObjects() 的文档中所定义) 或者一个拥有 fileno() 方法的对象，这个对象返回一个套接字句柄或者管道句柄。（注意管道和套接字两种句柄 不是 可等待的句柄）

3.3 新版功能.

示例

下面的服务代码创建了一个使用 'secret password' 作为认证密码的监听器。它会等待连接然后发送一些数据给客户端:

from multiprocessing.connection import Listener
from array import array

address = ('localhost', 6000)     # family is deduced to be 'AF_INET'

with Listener(address, authkey=b'secret password') as listener:
    with listener.accept() as conn:
        print('connection accepted from', listener.last_accepted)

        conn.send([2.25, None, 'junk', float])

        conn.send_bytes(b'hello')

        conn.send_bytes(array('i', [42, 1729]))

下面的代码连接到服务然后从服务器上j接收一些数据:

from multiprocessing.connection import Client
from array import array

address = ('localhost', 6000)

with Client(address, authkey=b'secret password') as conn:
    print(conn.recv())                  # => [2.25, None, 'junk', float]

    print(conn.recv_bytes())            # => 'hello'

    arr = array('i', [0, 0, 0, 0, 0])
    print(conn.recv_bytes_into(arr))    # => 8
    print(arr)                          # => array('i', [42, 1729, 0, 0, 0])

下面的代码使用了 wait() ，以便在同时等待多个进程发来消息。

import time, random
from multiprocessing import Process, Pipe, current_process
from multiprocessing.connection import wait

def foo(w):
    for i in range(10):
        w.send((i, current_process().name))
    w.close()

if __name__ == '__main__':
    readers = []

    for i in range(4):
        r, w = Pipe(duplex=False)
        readers.append(r)
        p = Process(target=foo, args=(w,))
        p.start()
        # We close the writable end of the pipe now to be sure that
        # p is the only process which owns a handle for it.  This
        # ensures that when p closes its handle for the writable end,
        # wait() will promptly report the readable end as being ready.
        w.close()

    while readers:
        for r in wait(readers):
            try:
                msg = r.recv()
            except EOFError:
                readers.remove(r)
            else:
                print(msg)

17.2.2.10.1. 地址格式

'AF_INET' 地址是 (主机, 端口)  形式的元组类型，其中 主机 是一个字符串，端口 是整数。

'AF_UNIX' 地址是文件系统上文件名的字符串。

'AF_PIPE' 是这种格式的字符串

r'\.\pipe{PipeName}' 。如果要用 Client() 连接到一个名为 ServerName 的远程命名管道，应该替换为使用 r'\ServerName\pipe{PipeName}' 这种格式。

注意，使用两个反斜线开头的字符串默认被当做 'AF_PIPE' 地址而不是 'AF_UNIX'  。

17.2.2.11. 认证密码

当使用 Connection.recv 接收数据时，数据会自动被反序列化。不幸的是，对于一个不可信的数据源发来的数据，反序列化是存在安全风险的。所以 Listener 和 Client() 之间使用 hmac 模块进行摘要认证。

认证密钥是一个 byte 类型的字符串，可以认为是和密码一样的东西，连接建立好后，双方都会要求另一方证明知道认证密钥。（这个证明过程不会通过连接发送密钥）

如果要求认证但是没有指定认证密钥，则会使用 current_process().authkey 的返回值 (参见 Process)。 这个值将被当前进程所创建的任何 Process 对象自动继承。 这意味着 (在默认情况下) 一个包含多进程的程序中的所有进程会在相互间建立连接的时候共享单个认证密钥。

os.urandom() 也可以用来生成合适的认证密钥。

17.2.2.12. 日志记录

当前模块也提供了一些对 logging 的支持。注意， logging 模块本身并没有使用进程间共享的锁，所以来自于多个进程的日志可能（具体取决于使用的日志 handler）相互覆盖或者混杂。

multiprocessing.get_logger()

返回 multiprocessing 使用的 logger，必要的话会创建一个新的。

如果创建的首个 logger 日志级别为 logging.NOTSET 并且没有默认 handler。通过这个 logger 打印的消息不会传递到根 logger。

注意在 Windows 上，子进程只会继承父进程 logger 的日志级别 - 对于logger的其他自定义项不会继承。

multiprocessing.log_to_stderr()

此函数会调用 get_logger() 但是会在返回的 logger 上增加一个 handler，将所有输出都使用 '[%(levelname)s/%(processName)s] %(message)s' 的格式发送到 sys.stderr 。

下面是一个在交互式解释器中打开日志功能的例子:

>>> import multiprocessing, logging
>>> logger = multiprocessing.log_to_stderr()
>>> logger.setLevel(logging.INFO)
>>> logger.warning('doomed')
[WARNING/MainProcess] doomed
>>> m = multiprocessing.Manager()
[INFO/SyncManager-...] child process calling self.run()
[INFO/SyncManager-...] created temp directory /.../pymp-...
[INFO/SyncManager-...] manager serving at '/.../listener-...'
>>> del m
[INFO/MainProcess] sending shutdown message to manager
[INFO/SyncManager-...] manager exiting with exitcode 0

要查看日志等级的完整列表，见 logging 模块。

17.2.2.13. multiprocessing.dummy 模块

multiprocessing.dummy 复制了 multiprocessing 的 API，不过是在 threading 模块之上包装了一层。

17.2.3. 编程指导

使用 multiprocessing 时，应遵循一些指导原则和习惯用法。

17.2.3.1. 所有启动方法

下面这些使用于所有启动方法。

避免共享状态

应该尽可能避免在进程间传递大量数据，越少越好。

最好坚持使用队列或者管道进行进程间通信，而不是底层的同步原语。

可序列化

保证所代理的方法的参数是可以序列化的。

代理的线程安全性

不要在多线程中同时使用一个代理对象，除非你用锁保护它。

（而在不同进程中使用 相同 的代理对象从不会发生问题。）

使用 Join 避免僵尸进程

在 Unix 上，如果一个进程执行完成但是没有被 join，就会变成僵尸进程。一般来说，僵尸进程不会很多，因为每次新启动进程（或者 active_children() 被调用）时，所有已执行完成且没有被 join 的进程都会自动被 join，而且对一个执行完的进程调用 Process.is_alive 也会 join 这个进程。尽管如此，对自己启动的进程显式调用 join 依然是最佳实践。

继承优于序列化、反序列化

当使用 spawn 或者 forkserver 的启动方式时，multiprocessing 中的许多类型都必须是可序列化的，这样子进程才能使用它们。但是通常我们都应该避免使用管道和队列发送共享对象到另外一个进程，而是重新组织代码，对于其他进程创建出来的共享对象，让那些需要访问这些对象的子进程可以直接将这些对象从父进程继承过来。

避免杀死进程

听过 Process.terminate  停止一个进程很容易导致这个进程正在使用的共享资源（如锁、信号量、管道和队列）损坏或者变得不可用，无法在其他进程中继续使用。

所以，最好只对那些从来不使用共享资源的进程调用 Process.terminate 。

Join 使用队列的进程

记住，往队列放入数据的进程会一直等待直到所有缓存项被”feeder” 线程传给底层管道。（子进程可以调用队列的 Queue.cancel_join_thread 方法禁止这种行为）

这意味着，任何使用队列的时候，你都要确保在进程join之前，所有存放到队列中的项将会被其他进程、线程完全消费。否则不能保证这个写过队列的进程可以正常终止。记住非精灵进程会自动 join 。

下面是一个会导致死锁的例子:

from multiprocessing import Process, Queue

def f(q):
    q.put('X' * 1000000)

if __name__ == '__main__':
    queue = Queue()
    p = Process(target=f, args=(queue,))
    p.start()
    p.join()                    # this deadlocks
    obj = queue.get()

交换最后两行可以修复这个问题（或者直接删掉 p.join()）。

显示传递资源给子进程

在Unix上，使用 fork 方式启动的子进程可以使用父进程中全局创建的共享资源。

除了（部分原因）让代码兼容 Windows 以及其他的进程启动方式外，这种形式还保证了在子进程生命期这个对象是不会被父进程垃圾回收的。如果父进程中的某些对象被垃圾回收会导致资源释放，这就变得很重要。

所以对于实例：

from multiprocessing import Process, Lock

def f():
    ... do something using "lock" ...

if __name__ == '__main__':
    lock = Lock()
    for i in range(10):
        Process(target=f).start()

应当重写成这样：

from multiprocessing import Process, Lock

def f(l):
    ... do something using "l" ...

if __name__ == '__main__':
    lock = Lock()
    for i in range(10):
        Process(target=f, args=(lock,)).start()

谨防将 sys.stdin 数据替换为 “类似文件的对象”

multiprocessing 内部会无条件地这样调用:

os.close(sys.stdin.fileno())

在 multiprocessing.Process._bootstrap()  方法中 —— 这会导致与”进程中的进程”相关的一些问题。这已经被修改成了:

sys.stdin.close()
sys.stdin = open(os.open(os.devnull, os.O_RDONLY), closefd=False)

它解决了进程相互冲突导致文件描述符错误的根本问题，但是对使用带缓冲的“文件类对象”替换 sys.stdin() 作为输出的应用程序造成了潜在的危险。如果多个进程调用了此文件类对象的 close() 方法，会导致相同的数据多次刷写到此对象，损坏数据。

如果你写入文件类对象并实现了自己的缓存，可以在每次追加缓存数据时记录当前进程id，从而将其变成 fork 安全的，当发现进程id变化后舍弃之前的缓存，例如:

@property
def cache(self):
    pid = os.getpid()
    if pid != self._pid:
        self._pid = pid
        self._cache = []
    return self._cache

需要更多信息，请查看 bpo-5155 [https://bugs.python.org/issue5155], bpo-5313 [https://bugs.python.org/issue5313] 以及 bpo-5331 [https://bugs.python.org/issue5331]

17.2.3.2. spawn 和 forkserver 启动方式

相对于 fork 启动方式，有一些额外的限制。

更依赖序列化

Process.__init__() 的所有参数都必须可序列化。同样的，当你继承 Process 时，需要保证当调用 Process.start 方法时，实例可以被序列化。

全局变量

记住，如果子进程中的代码尝试访问一个全局变量，它所看到的值可能和父进程中执行 Process.start 那一刻的值不一样。

当全局变量知识模块级别的常量时，是不会有问题的。

安全导入主模块

确保主模块可以被新启动的Python解释器安全导入而不会引发什么副作用（比如又启动了一个子进程）

例如，使用 spawn 或 forkserver 启动方式执行下面的模块，会引发 RuntimeError 异常而失败。

from multiprocessing import Process

def foo():
    print('hello')

p = Process(target=foo)
p.start()

应该通过下面的方法使用 if __name__ == '__main__': ，从而保护程序”入口点”:

from multiprocessing import Process, freeze_support, set_start_method

def foo():
    print('hello')

if __name__ == '__main__':
    freeze_support()
    set_start_method('spawn')
    p = Process(target=foo)
    p.start()

（如果程序将正常运行而不是冻结，则可以省略 freeze_support() 行）

这允许新启动的 Python 解释器安全导入模块然后运行模块中的 foo() 函数。

如果主模块中创建了进程池或者管理器，这个规则也适用。

17.2.4. 例子

创建和使用自定义管理器、代理的示例:

from multiprocessing import freeze_support
from multiprocessing.managers import BaseManager, BaseProxy
import operator

##

class Foo:
    def f(self):
        print('you called Foo.f()')
    def g(self):
        print('you called Foo.g()')
    def _h(self):
        print('you called Foo._h()')

# A simple generator function
def baz():
    for i in range(10):
        yield i*i

# Proxy type for generator objects
class GeneratorProxy(BaseProxy):
    _exposed_ = ['__next__']
    def __iter__(self):
        return self
    def __next__(self):
        return self._callmethod('__next__')

# Function to return the operator module
def get_operator_module():
    return operator

##

class MyManager(BaseManager):
    pass

# register the Foo class; make `f()` and `g()` accessible via proxy
MyManager.register('Foo1', Foo)

# register the Foo class; make `g()` and `_h()` accessible via proxy
MyManager.register('Foo2', Foo, exposed=('g', '_h'))

# register the generator function baz; use `GeneratorProxy` to make proxies
MyManager.register('baz', baz, proxytype=GeneratorProxy)

# register get_operator_module(); make public functions accessible via proxy
MyManager.register('operator', get_operator_module)

##

def test():
    manager = MyManager()
    manager.start()

    print('-' * 20)

    f1 = manager.Foo1()
    f1.f()
    f1.g()
    assert not hasattr(f1, '_h')
    assert sorted(f1._exposed_) == sorted(['f', 'g'])

    print('-' * 20)

    f2 = manager.Foo2()
    f2.g()
    f2._h()
    assert not hasattr(f2, 'f')
    assert sorted(f2._exposed_) == sorted(['g', '_h'])

    print('-' * 20)

    it = manager.baz()
    for i in it:
        print('<%d>' % i, end=' ')
    print()

    print('-' * 20)

    op = manager.operator()
    print('op.add(23, 45) =', op.add(23, 45))
    print('op.pow(2, 94) =', op.pow(2, 94))
    print('op._exposed_ =', op._exposed_)

##

if __name__ == '__main__':
    freeze_support()
    test()

使用 Pool:

import multiprocessing
import time
import random
import sys

#
# Functions used by test code
#

def calculate(func, args):
    result = func(*args)
    return '%s says that %s%s = %s' % (
        multiprocessing.current_process().name,
        func.__name__, args, result
        )

def calculatestar(args):
    return calculate(*args)

def mul(a, b):
    time.sleep(0.5 * random.random())
    return a * b

def plus(a, b):
    time.sleep(0.5 * random.random())
    return a + b

def f(x):
    return 1.0 / (x - 5.0)

def pow3(x):
    return x ** 3

def noop(x):
    pass

#
# Test code
#

def test():
    PROCESSES = 4
    print('Creating pool with %d processes\n' % PROCESSES)

    with multiprocessing.Pool(PROCESSES) as pool:
        #
        # Tests
        #

        TASKS = [(mul, (i, 7)) for i in range(10)] + \
                [(plus, (i, 8)) for i in range(10)]

        results = [pool.apply_async(calculate, t) for t in TASKS]
        imap_it = pool.imap(calculatestar, TASKS)
        imap_unordered_it = pool.imap_unordered(calculatestar, TASKS)

        print('Ordered results using pool.apply_async():')
        for r in results:
            print('\t', r.get())
        print()

        print('Ordered results using pool.imap():')
        for x in imap_it:
            print('\t', x)
        print()

        print('Unordered results using pool.imap_unordered():')
        for x in imap_unordered_it:
            print('\t', x)
        print()

        print('Ordered results using pool.map() --- will block till complete:')
        for x in pool.map(calculatestar, TASKS):
            print('\t', x)
        print()

        #
        # Test error handling
        #

        print('Testing error handling:')

        try:
            print(pool.apply(f, (5,)))
        except ZeroDivisionError:
            print('\tGot ZeroDivisionError as expected from pool.apply()')
        else:
            raise AssertionError('expected ZeroDivisionError')

        try:
            print(pool.map(f, list(range(10))))
        except ZeroDivisionError:
            print('\tGot ZeroDivisionError as expected from pool.map()')
        else:
            raise AssertionError('expected ZeroDivisionError')

        try:
            print(list(pool.imap(f, list(range(10)))))
        except ZeroDivisionError:
            print('\tGot ZeroDivisionError as expected from list(pool.imap())')
        else:
            raise AssertionError('expected ZeroDivisionError')

        it = pool.imap(f, list(range(10)))
        for i in range(10):
            try:
                x = next(it)
            except ZeroDivisionError:
                if i == 5:
                    pass
            except StopIteration:
                break
            else:
                if i == 5:
                    raise AssertionError('expected ZeroDivisionError')

        assert i == 9
        print('\tGot ZeroDivisionError as expected from IMapIterator.next()')
        print()

        #
        # Testing timeouts
        #

        print('Testing ApplyResult.get() with timeout:', end=' ')
        res = pool.apply_async(calculate, TASKS[0])
        while 1:
            sys.stdout.flush()
            try:
                sys.stdout.write('\n\t%s' % res.get(0.02))
                break
            except multiprocessing.TimeoutError:
                sys.stdout.write('.')
        print()
        print()

        print('Testing IMapIterator.next() with timeout:', end=' ')
        it = pool.imap(calculatestar, TASKS)
        while 1:
            sys.stdout.flush()
            try:
                sys.stdout.write('\n\t%s' % it.next(0.02))
            except StopIteration:
                break
            except multiprocessing.TimeoutError:
                sys.stdout.write('.')
        print()
        print()

if __name__ == '__main__':
    multiprocessing.freeze_support()
    test()

一个演示如何使用队列来向一组工作进程提供任务并收集结果的例子：

import time
import random

from multiprocessing import Process, Queue, current_process, freeze_support

#
# Function run by worker processes
#

def worker(input, output):
    for func, args in iter(input.get, 'STOP'):
        result = calculate(func, args)
        output.put(result)

#
# Function used to calculate result
#

def calculate(func, args):
    result = func(*args)
    return '%s says that %s%s = %s' % \
        (current_process().name, func.__name__, args, result)

#
# Functions referenced by tasks
#

def mul(a, b):
    time.sleep(0.5*random.random())
    return a * b

def plus(a, b):
    time.sleep(0.5*random.random())
    return a + b

#
#
#

def test():
    NUMBER_OF_PROCESSES = 4
    TASKS1 = [(mul, (i, 7)) for i in range(20)]
    TASKS2 = [(plus, (i, 8)) for i in range(10)]

    # Create queues
    task_queue = Queue()
    done_queue = Queue()

    # Submit tasks
    for task in TASKS1:
        task_queue.put(task)

    # Start worker processes
    for i in range(NUMBER_OF_PROCESSES):
        Process(target=worker, args=(task_queue, done_queue)).start()

    # Get and print results
    print('Unordered results:')
    for i in range(len(TASKS1)):
        print('\t', done_queue.get())

    # Add more tasks using `put()`
    for task in TASKS2:
        task_queue.put(task)

    # Get and print some more results
    for i in range(len(TASKS2)):
        print('\t', done_queue.get())

    # Tell child processes to stop
    for i in range(NUMBER_OF_PROCESSES):
        task_queue.put('STOP')

if __name__ == '__main__':
    freeze_support()
    test()

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

17.3. concurrent 包

目前，此包中只有一个模块：

concurrent.futures —— 启动并行任务

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

17.4. concurrent.futures — 启动并行任务

3.2 新版功能.

源码: Lib/concurrent/futures/thread.py [https://github.com/python/cpython/tree/3.6/Lib/concurrent/futures/thread.py] 和 Lib/concurrent/futures/process.py [https://github.com/python/cpython/tree/3.6/Lib/concurrent/futures/process.py]

concurrent.futures 模块提供异步执行可调用对象高层接口。

异步执行可以由 ThreadPoolExecutor 使用线程或由 ProcessPoolExecutor 使用单独的进程来实现。 两者都是实现抽像类 Executor 定义的接口。

17.4.1. Executor 对象

class concurrent.futures.Executor

抽象类提供异步执行调用方法。要通过它的子类调用，而不是直接调用。

submit(fn, *args, **kwargs)

调度可调用对象 fn，以 fn(*args **kwargs) 方式执行并返回 Future 对像代表可调用对象的执行。:

with ThreadPoolExecutor(max_workers=1) as executor:
    future = executor.submit(pow, 323, 1235)
    print(future.result())

map(func, *iterables, timeout=None, chunksize=1)

类似于 map(func, *iterables) 函数，除了以下两点：

iterables 是立即执行而不是延迟执行的；

func 是异步执行的，对 func 的多个调用可以并发执行。

如果从原始调用到 Executor.map() 经过 timeout 秒后， __next__() 已被调用且返回的结果还不可用，那么已返回的迭代器将触发 concurrent.futures.TimeoutError 。 timeout 可以是整数或浮点数。如果 timeout 没有指定或为 None ，则没有超时限制。

如果 func 调用引发一个异常，当从迭代器中取回它的值时这个异常将被引发。

使用 ProcessPoolExecutor 时，这个方法会将 iterables 分割任务块并作为独立的任务并提交到执行池中。这些块的大概数量可以由 chunksize 指定正整数设置。 对很长的迭代器来说，使用大的 chunksize 值比默认值 1 能显著地提高性能。 chunksize 对 ThreadPoolExecutor 没有效果。

在 3.5 版更改: 加入 chunksize 参数。

shutdown(wait=True)

当待执行的 future 对象完成执行后向执行者发送信号，它就会释放正在使用的任何资源。 在关闭后调用 Executor.submit() 和 Executor.map() 将会引发 RuntimeError。

如果 wait 为 True 则此方法只有在所有待执行的 future 对象完成执行且释放已分配的资源后才会返回。 如果 wait 为 False，方法立即返回，所有待执行的 future 对象完成执行后会释放已分配的资源。 不管 wait 的值是什么，整个 Python 程序将等到所有待执行的 future 对象完成执行后才退出。

如果使用 with 语句，你就可以避免显式调用这个方法，它将会停止 Executor (就好像 Executor.shutdown() 调用时 wait 设为 True 一样等待):

import shutil
with ThreadPoolExecutor(max_workers=4) as e:
    e.submit(shutil.copy, 'src1.txt', 'dest1.txt')
    e.submit(shutil.copy, 'src2.txt', 'dest2.txt')
    e.submit(shutil.copy, 'src3.txt', 'dest3.txt')
    e.submit(shutil.copy, 'src4.txt', 'dest4.txt')

17.4.2. ThreadPoolExecutor

ThreadPoolExecutor 是 Executor 的子类，它使用线程池来异步执行调用。

当回调已关联了一个 Future 然后再等待另一个 Future 的结果时就会发产死锁情况。例如:

import time
def wait_on_b():
    time.sleep(5)
    print(b.result())  # b will never complete because it is waiting on a.
    return 5

def wait_on_a():
    time.sleep(5)
    print(a.result())  # a will never complete because it is waiting on b.
    return 6

executor = ThreadPoolExecutor(max_workers=2)
a = executor.submit(wait_on_b)
b = executor.submit(wait_on_a)

与:

def wait_on_future():
    f = executor.submit(pow, 5, 2)
    # This will never complete because there is only one worker thread and
    # it is executing this function.
    print(f.result())

executor = ThreadPoolExecutor(max_workers=1)
executor.submit(wait_on_future)

class concurrent.futures.ThreadPoolExecutor(max_workers=None, thread_name_prefix='')

Executor 子类使用最多 max_workers 个线程的线程池来异步执行调用。

在 3.5 版更改: 如果 max_workers 为 None 或没有指定，将默认为机器处理器的个数，假如 ThreadPoolExecutor 则重于I/O操作而不是CPU运算，那么可以乘以 5 ，同时工作线程的数量可以比 ProcessPoolExecutor 的数量高。

3.6 新版功能: 添加 thread_name_prefix 参数允许用户控制由线程池创建的 threading.Thread 工作线程名称以方便调试。

17.4.2.1. ThreadPoolExecutor 例子

import concurrent.futures
import urllib.request

URLS = ['http://www.foxnews.com/',
        'http://www.cnn.com/',
        'http://europe.wsj.com/',
        'http://www.bbc.co.uk/',
        'http://some-made-up-domain.com/']

# Retrieve a single page and report the URL and contents
def load_url(url, timeout):
    with urllib.request.urlopen(url, timeout=timeout) as conn:
        return conn.read()

# We can use a with statement to ensure threads are cleaned up promptly
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    # Start the load operations and mark each future with its URL
    future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
    for future in concurrent.futures.as_completed(future_to_url):
        url = future_to_url[future]
        try:
            data = future.result()
        except Exception as exc:
            print('%r generated an exception: %s' % (url, exc))
        else:
            print('%r page is %d bytes' % (url, len(data)))

17.4.3. ProcessPoolExecutor

ProcessPoolExecutor 是 Executor 的子类，它使用进程池来实现异步执行调用。 ProcessPoolExecutor 使用 multiprocessing 回避 Global Interpreter Lock 但也意味着只可以处理和返回可序列化的对象。

__main__ 模块必须可以被工作者子进程导入。这意味着 ProcessPoolExecutor 不可以工作在交互式解释器中。

从可调用对象中调用 Executor 或 Future 的方法提交给 ProcessPoolExecutor 会导致死锁。

class concurrent.futures.ProcessPoolExecutor(max_workers=None)

An Executor subclass that executes calls asynchronously using a pool of at most max_workers processes. If max_workers is None or not given, it will default to the number of processors on the machine. If max_workers is lower or equal to 0, then a ValueError will be raised.

在 3.3 版更改: 如果其中一个工作进程被突然终止，BrokenProcessPool 就会马上触发。 可预计的行为没有定义，但执行器上的操作或它的 future 对象会被冻结或死锁。

17.4.3.1. ProcessPoolExecutor 例子

import concurrent.futures
import math

PRIMES = [
    112272535095293,
    112582705942171,
    112272535095293,
    115280095190773,
    115797848077099,
    1099726899285419]

def is_prime(n):
    if n % 2 == 0:
        return False

    sqrt_n = int(math.floor(math.sqrt(n)))
    for i in range(3, sqrt_n + 1, 2):
        if n % i == 0:
            return False
    return True

def main():
    with concurrent.futures.ProcessPoolExecutor() as executor:
        for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
            print('%d is prime: %s' % (number, prime))

if __name__ == '__main__':
    main()

17.4.4. Future 对象

Future 类将可调用对象封装为异步执行。Future 实例由 Executor.submit() 创建。

class concurrent.futures.Future

将可调用对象封装为异步执行。Future 实例由 Executor.submit() 创建，除非测试，不应直接创建。

cancel()

Attempt to cancel the call. If the call is currently being executed and cannot be cancelled then the method will return False, otherwise the call will be cancelled and the method will return True.

cancelled()

如果调用成功取消返回 True。

running()

如果调用正在执行而且不能被取消那么返回 True 。

done()

如果调用已被取消或正常结束那么返回 True。

result(timeout=None)

返回调用返回的值。如果调用还没完成那么这个方法将等待 timeout 秒。如果在 timeout 秒内没有执行完成，concurrent.futures.TimeoutError 将会被触发。timeout 可以是整数或浮点数。如果 timeout 没有指定或为 None，那么等待时间就没有限制。

如果 futrue 在完成前被取消则 CancelledError 将被触发。

如果调用引发了一个异常，这个方法也会引发同样的异常。

exception(timeout=None)

返回由调用引发的异常。如果调用还没完成那么这个方法将等待 timeout 秒。如果在 timeout 秒内没有执行完成，concurrent.futures.TimeoutError 将会被触发。timeout 可以是整数或浮点数。如果 timeout 没有指定或为 None，那么等待时间就没有限制。

如果 futrue 在完成前被取消则 CancelledError 将被触发。

如果调用正常完成那么返回 None。

add_done_callback(fn)

附加可调用 fn 到 future 对象。当 future 对象被取消或完成运行时，将会调用 fn，而这个 future 对象将作为它唯一的参数。

加入的可调用对象总被属于添加它们的进程中的线程按加入的顺序调用。如果可调用对象引发一个 Exception 子类，它会被记录下来并被忽略掉。如果可调用对象引发一个 BaseException 子类，这个行为没有定义。

如果 future 对象已经完成或已取消，fn 会被立即调用。

下面这些 Future 方法用于单元测试和 Executor 实现。

set_running_or_notify_cancel()

这个方法只可以在执行关联 Future 工作之前由 Executor 实现调用或由单测试调用。

如果这个方法返回 False 那么 Future 已被取消，即 Future.cancel() 已被调用并返回 True 。等待 Future 完成 (即通过 as_completed() 或 wait()) 的线程将被唤醒。

如果这个方法返回 True 那么 Future 不会被取消并已将它变为正在运行状态，也就是说调用 Future.running() 时将返回 True。

这个方法只可以被调用一次并且不能在调用 Future.set_result() 或 Future.set_exception() 之后再调用。

set_result(result)

设置将 Future 关联工作的结果给 result 。

这个方法只可以由 Executor 实现和单元测试使用。

set_exception(exception)

设置 Future 关联工作的结果给 Exception exception 。

这个方法只可以由 Executor 实现和单元测试使用。

17.4.5. 模块函数

concurrent.futures.wait(fs, timeout=None, return_when=ALL_COMPLETED)

Wait for the Future instances (possibly created by different Executor instances) given by fs to complete. Returns a named 2-tuple of sets. The first set, named done, contains the futures that completed (finished or were cancelled) before the wait completed. The second set, named not_done, contains uncompleted futures.

timeout 可以用来控制返回前最大的等待秒数。 timeout 可以为 int 或 float 类型。 如果 timeout 未指定或为 None ，则不限制等待时间。

return_when 指定此函数应在何时返回。它必须为以下常数之一:

常数

描述

FIRST_COMPLETED

函数将在任意可等待对象结束或取消时返回。

FIRST_EXCEPTION

函数将在任意可等待对象因引发异常而结束时返回。当没有引发任何异常时它就相当于 ALL_COMPLETED。

ALL_COMPLETED

函数将在所有可等待对象结束或取消时返回。

concurrent.futures.as_completed(fs, timeout=None)

Returns an iterator over the Future instances (possibly created by different Executor instances) given by fs that yields futures as they complete (finished or were cancelled). Any futures given by fs that are duplicated will be returned once. Any futures that completed before as_completed() is called will be yielded first. The returned iterator raises a concurrent.futures.TimeoutError if __next__() is called and the result isn’t available after timeout seconds from the original call to as_completed(). timeout can be an int or float. If timeout is not specified or None, there is no limit to the wait time.

参见

PEP 3148 [https://www.python.org/dev/peps/pep-3148] – future 对象 - 异步执行指令。

该提案描述了Python标准库中包含的这个特性。

17.4.6. Exception类

exception concurrent.futures.CancelledError

future 对象被取消时会触发。

exception concurrent.futures.TimeoutError

future 对象执行超出给定的超时数值时引发。

exception concurrent.futures.process.BrokenProcessPool

Derived from RuntimeError, this exception class is raised when one of the workers of a ProcessPoolExecutor has terminated in a non-clean fashion (for example, if it was killed from the outside).

3.3 新版功能.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

17.5. subprocess — 子进程管理

源代码: Lib/subprocess.py [https://github.com/python/cpython/tree/3.6/Lib/subprocess.py]

subprocess 模块允许你生成新的进程，连接它们的输入、输出、错误管道，并且获取它们的返回码。此模块打算代替一些老旧的模块与功能：

os.system
os.spawn*

在下面的段落中，你可以找到关于 subprocess 模块如何代替这些模块和功能的相关信息。

参见

PEP 324 [https://www.python.org/dev/peps/pep-0324] – 提出 subprocess 模块的 PEP

17.5.1. 使用 subprocess 模块

推荐的调用子进程的方式是在任何它支持的用例中使用 run() 函数。对于更进阶的用例，也可以使用底层的 Popen 接口。

run() 函数是在 Python 3.5 被添加的；如果你需要与旧版本保持兼容，查看 较旧的高阶 API 段落。

subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, env=None)

运行被 arg 描述的指令。等待指令完成，然后返回一个 CompletedProcess 实例。

The arguments shown above are merely the most common ones, described below in 常用参数 (hence the use of keyword-only notation in the abbreviated signature). The full function signature is largely the same as that of the Popen constructor - apart from timeout, input and check, all the arguments to this function are passed through to that interface.

This does not capture stdout or stderr by default. To do so, pass PIPE for the stdout and/or stderr arguments.

timeout 参数将被传递给 Popen.communicate()。如果发生超时，子进程将被杀死并等待。 TimeoutExpired 异常将在子进程中断后被抛出。

The input argument is passed to Popen.communicate() and thus to the subprocess’s stdin. If used it must be a byte sequence, or a string if encoding or errors is specified or universal_newlines is true. When used, the internal Popen object is automatically created with stdin=PIPE, and the stdin argument may not be used as well.

如果 check 设为 True, 并且进程以非零状态码退出, 一个 CalledProcessError 异常将被抛出. 这个异常的属性将设置为参数, 退出码, 以及标准输出和标准错误, 如果被捕获到.

If encoding or errors are specified, or universal_newlines is true, file objects for stdin, stdout and stderr are opened in text mode using the specified encoding and errors or the io.TextIOWrapper default. Otherwise, file objects are opened in binary mode.

如果 env 不是 None, 它必须是一个字典, 为新的进程设置环境变量; 它用于替换继承的当前进程的环境的默认行为. 它将直接被传递给 Popen.

示例:

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n')

3.5 新版功能.

在 3.6 版更改: 添加了 encoding 和 errors 形参.

class subprocess.CompletedProcess

run() 的返回值, 代表一个进程已经结束.

args

被用作启动进程的参数. 可能是一个列表或字符串.

returncode

子进程的退出状态码. 通常来说, 一个为 0 的退出码表示进程运行正常.

一个负值 -N 表示子进程被信号 N 中断 (仅 POSIX).

stdout

Captured stdout from the child process. A bytes sequence, or a string if run() was called with an encoding or errors. None if stdout was not captured.

如果你通过 stderr=subprocess.STDOUT 运行, 标准输入和标准错误将被组合在一起, 并且 stderr` 将为 None.

stderr

Captured stderr from the child process. A bytes sequence, or a string if run() was called with an encoding or errors. None if stderr was not captured.

check_returncode()

如果 returncode 非零, 抛出 CalledProcessError.

3.5 新版功能.

subprocess.DEVNULL

可被 Popen 的 stdin, stdout 或者 stderr 参数使用的特殊值, 表示使用特殊文件 os.devnull.

3.3 新版功能.

subprocess.PIPE

可被 Popen 的 stdin, stdout 或者 stderr 参数使用的特殊值, 表示打开标准流的管道. 常用于 Popen.communicate().

subprocess.STDOUT

可被 Popen 的 stdin ， stdout 或者 stderr 参数使用的特殊值， 表示标准错误与标准输出使用同一句柄。

exception subprocess.SubprocessError

此模块的其他异常的基类。

3.3 新版功能.

exception subprocess.TimeoutExpired

SubprocessError 的子类，等待子进程的过程中发生超时时被抛出。

cmd

用于创建子进程的指令。

timeout

超时秒数。

output

子进程的输出， 如果被 run() 或 check_output() 捕获。否则为 None。

stdout

对 output 的别名，对应的有 stderr。

stderr

子进程的标准错误输出，如果被 run() 捕获。 否则为 None。

3.3 新版功能.

在 3.5 版更改: 添加了 stdout 和 stderr 属性。

exception subprocess.CalledProcessError

SubprocessError 的子类，当一个被 check_call() 或 check_output() 函数运行的子进程返回了非零退出码时被抛出。

returncode

子进程的退出状态。如果程序由一个信号终止，这将会被设为一个负的信号码。

cmd

用于创建子进程的指令。

output

子进程的输出， 如果被 run() 或 check_output() 捕获。否则为 None。

stdout

对 output 的别名，对应的有 stderr。

stderr

子进程的标准错误输出，如果被 run() 捕获。 否则为 None。

在 3.5 版更改: 添加了 stdout 和 stderr 属性。

17.5.1.1. 常用参数

为了支持丰富的使用案例， Popen 的构造函数（以及方便的函数）接受大量可选的参数。对于大多数典型的用例，许多参数可以被安全地留以它们的默认值。通常需要的参数有：

args 被所有调用需要，应当为一个字符串，或者一个程序参数序列。提供一个参数序列通常更好，它可以更小心地使用参数中的转义字符以及引用（例如允许文件名中的空格）。如果传递一个简单的字符串，则 shell 参数必须为 True （见下文）或者该字符串中将被运行的程序名必须用简单的命名而不指定任何参数。

stdin， stdout 和 stderr 分别指定了执行的程序的标准输入、输出和标准错误文件句柄。合法的值有 PIPE 、 DEVNULL 、 一个现存的文件描述符（一个正整数）、一个现存的文件对象以及 None。 PIPE 表示应该新建一个对子进程的管道。 DEVNULL 表示使用特殊的文件 os.devnull。当使用默认设置 None 时，将不会进行重定向，子进程的文件流将继承自父进程。另外， stderr 可能为 STDOUT，表示来自于子进程的标准错误数据应该被 stdout 相同的句柄捕获。

If encoding or errors are specified, or universal_newlines is true, the file objects stdin, stdout and stderr will be opened in text mode using the encoding and errors specified in the call or the defaults for io.TextIOWrapper.

当构造函数的 newline 参数为 None 时。对于 stdin， 输入的换行符 '\n' 将被转换为默认的换行符 os.linesep。对于 stdout 和 stderr， 所有输出的换行符都被转换为 '\n'。更多信息，查看 io.TextIOWrapper 类的文档。

如果文本模式未被使用， stdin， stdout 和 stderr 将会以二进制流模式打开。没有编码与换行符转换发生。

3.6 新版功能: 添加了 encoding 和 errors 形参。

注解

文件对象 Popen.stdin 、 Popen.stdout 和 Popen.stderr 的换行符属性不会被 Popen.communicate() 方法更新。

如果 shell 设为 True,，则使用 shell 执行指定的指令。如果您主要使用 Python 增强的控制流（它比大多数系统 shell 提供的强大），并且仍然希望方便地使用其他 shell 功能，如 shell 管道、文件通配符、环境变量展开以及 ~ 展开到用户家目录，这将非常有用。但是，注意 Python 自己也实现了许多类似 shell 的特性（例如 glob, fnmatch, os.walk(), os.path.expandvars(), os.path.expanduser() 和 shutil）。

在 3.3 版更改: 当 universal_newline 被设为 True，则类使用 locale.getpreferredencoding(False) 编码来代替 locale.getpreferredencoding()。关于它们的区别的更多信息，见 io.TextIOWrapper。

注解

在使用 shell=True 之前， 请阅读 Security Considerations 段落。

这些选项以及所有其他选项在 Popen 构造函数文档中有更详细的描述。

17.5.1.2. Popen 构造函数

此模块的底层的进程创建与管理由 Popen 类处理。它提供了很大的灵活性，因此开发者能够处理未被便利函数覆盖的不常见用例。

class subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *, encoding=None, errors=None)

在一个新的进程中执行子程序。在 POSIX，此类使用类似于 os.execvp() 的行为来执行子程序。在 Windows，此类使用了 Windows CreateProcess() 函数。 Popen 的参数如下：

args 应当是一个程序的参数列表或者一个简单的字符串。默认情况下，如果 args 是一个序列，将运行的程序是此序列的第一项。如果 args 是一个字符串，解释是平台相关的，如下所述。有关默认行为的其他差异，见 shell 和 executable 参数。除非另有说明，推荐将 args 作为序列传递。

在 POSIX，如果 args 是一个字符串，此字符串被作为将被执行的程序的命名或路径解释。但是，只有在不传递任何参数给程序的情况下才能这么做。

注解

shlex.split() can be useful when determining the correct tokenization for args, especially in complex cases:

>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!

特别注意，由 shell 中的空格分隔的选项（例如 -input）和参数（例如 eggs.txt ）位于分开的列表元素中，而在需要时使用引号或反斜杠转义的参数在 shell （例如包含空格的文件名或上面显示的 echo 命令）是单独的列表元素。

在 Windows，如果 args 是一个序列，他将通过一个在 在 Windows 上将参数列表转换为一个字符串 描述的方式被转换为一个字符串。这是因为底层的 CreateProcess() 只处理字符串。

参数 shell （默认为 False）指定是否使用 shell 执行程序。如果 shell 为 True，更推荐将 args 作为字符串传递而非序列。

在 POSIX，当 shell=True， shell 默认为 /bin/sh。如果 args 是一个字符串，此字符串指定将通过 shell 执行的命令。这意味着字符串的格式必须和在命令提示符中所输入的完全相同。这包括，例如，引号和反斜杠转义包含空格的文件名。如果 args 是一个序列，第一项指定了命令，另外的项目将作为传递给 shell （而非命令） 的参数对待。也就是说， Popen 等同于:

Popen(['/bin/sh', '-c', args[0], args[1], ...])

在 Windows，使用 shell=True，环境变量 COMSPEC 指定了默认 shell。在 Windows 你唯一需要指定 shell=True 的情况是你想要执行内置在 shell 中的命令（例如 dir 或者 copy）。在运行一个批处理文件或者基于控制台的可执行文件时，不需要 shell=True。

注解

在使用 shell=True 之前， 请阅读 Security Considerations 段落。

bufsize 将在 open() 函数创建了 stdin/stdout/stderr 管道文件对象时作为对应的参数供应:

0 表示不使用缓冲区 （读取与写入是一个系统调用并且可以返回短内容）

1 表示行缓冲（只有 universal_newlines=True 时才有用，例如，在文本模式中）

任何其他正值表示使用一个约为对应大小的缓冲区

负的 bufsize （默认）表示使用系统默认的 io.DEFAULT_BUFFER_SIZE。

在 3.3.1 版更改: bufsize 现在默认为 -1 来启用缓冲，以符合大多数代码所期望的行为。在 Python 3.2.4 和 3.3.1 之前的版本中，它错误地将默认值设为了为 0，这是无缓冲的并且允许短读取。这是无意的，并且与大多数代码所期望的 Python 2 的行为不一致。

executable 参数指定一个要执行的替换程序。这很少需要。当 shell=True， executable 替换 args 指定运行的程序。但是，原始的 args 仍然被传递给程序。大多数程序将被 args 指定的程序作为命令名对待，这可以与实际运行的程序不同。在 POSIX， args 名作为实际调用程序中可执行文件的显示名称，例如 ps。如果 shell=True，在 POSIX， executable 参数指定用于替换默认 shell /bin/sh 的 shell。

stdin, stdout 和 stderr 分别指定被运行的程序的标准输入、输出和标准错误的文件句柄。合法的值有 PIPE ， DEVNULL ， 一个存在的文件描述符（一个正整数），一个存在的 文件对象 以及 None。 PIPE 表示应创建一个新的对子进程的管道。 DEVNULL 表示使用特殊的 os.devnull 文件。使用默认的 None，则不进行成定向；子进程的文件流将继承自父进程。另外， stderr 可设为 STDOUT，表示应用程序的标准错误数据应和标准输出一同捕获。

如果 preexec_fn 被设为一个可调用对象，此对象将在子进程刚创建时被调用。（仅 POSIX）

警告

preexec_fn 形参在应用程序中存在多线程时是不安全的。子进程在调用前可能死锁。如果你必须使用它，保持警惕！最小化你调用的库的数量。

注解

如果你需要修改子进程环境，使用 env 形参而非在 preexec_fn 中进行。 start_new_session 形参可以代替之前常用的 preexec_fn 来在子进程中调用 os.setsid()。

If close_fds is true, all file descriptors except 0, 1 and 2 will be closed before the child process is executed. (POSIX only). The default varies by platform: Always true on POSIX. On Windows it is true when stdin/stdout/stderr are None, false otherwise. On Windows, if close_fds is true then no handles will be inherited by the child process. Note that on Windows, you cannot set close_fds to true and also redirect the standard handles by setting stdin, stdout or stderr.

在 3.2 版更改: close_fds 的默认值已经从 False 修改为上述值。

pass_fds 是一个可选的在父子进程间保持打开的文件描述符序列。提供任何 pass_fds 将强制 close_fds 为 True。（仅 POSIX）

3.2 新版功能: 加入了 pass_fds 形参。

如果 cwd 不为 None，此函数在执行子进程前改变当前工作目录为 cwd。 cwd 可以为一个 str 和 path-like 对象。特别要注意，当可执行文件的路径为相对路径时，此函数按相对于 cwd 的路径来寻找 executable (或者 args 的第一项）。

在 3.6 版更改: cwd 形参接受一个 path-like object。

 如果 restore_signals 为 true（默认值），则 Python 设置为 SIG_IGN 的所有信号将在 exec 之前的子进程中恢复为 SIG_DFL。目前，这包括 SIGPIPE ，SIGXFZ 和 SIGXFSZ 信号。 （仅 POSIX）

在 3.2 版更改: restore_signals 被加入。

如果 start_new_session 为 true，则 setsid() 系统调用将在子进程执行之前被执行。（仅 POSIX）

在 3.2 版更改: start_new_session 被添加。

如果 env 不为 None，则必须为一个为新进程定义了环境变量的字典；这些用于替换继承的当前进程环境的默认行为。

注解

如果指定， env 必须提供所有被子进程需求的变量。在 Windows，为了运行一个 side-by-side assembly [https://en.wikipedia.org/wiki/Side-by-Side_Assembly] ，指定的 env 必须 包含一个有效的 SystemRoot。

If encoding or errors are specified, the file objects stdin, stdout and stderr are opened in text mode with the specified encoding and errors, as described above in 常用参数. If universal_newlines is True, they are opened in text mode with default encoding. Otherwise, they are opened as binary streams.

3.6 新版功能: encoding 和 errors 被添加。

If given, startupinfo will be a STARTUPINFO object, which is passed to the underlying CreateProcess function. creationflags, if given, can be CREATE_NEW_CONSOLE or CREATE_NEW_PROCESS_GROUP. (Windows only)

Popen 对象支持通过 with 语句作为上下文管理器，在退出时关闭文件描述符并等待进程:

with Popen(["ifconfig"], stdout=PIPE) as proc:
    log.write(proc.stdout.read())

在 3.2 版更改: 添加了上下文管理器支持。

在 3.6 版更改: 现在，如果 Popen 析构时子进程仍然在运行，则析构器会发送一个 ResourceWarning 警告。

17.5.1.3. 异常

Exceptions raised in the child process, before the new program has started to execute, will be re-raised in the parent. Additionally, the exception object will have one extra attribute called child_traceback, which is a string containing traceback information from the child’s point of view.

最常见的被抛出异常是 OSError。例如，当尝试执行一个不存在的文件时就会发生。应用程序需要为 OSError 异常做好准备。

如果 Popen 调用时有无效的参数，则一个 ValueError 将被抛出。

check_all() 与 check_output() 在调用的进程返回非零退出码时将抛出 CalledProcessError。

所有接受 timeout 形参的函数与方法，例如 call() 和 Popen.communicate() 将会在进程退出前超时到期时抛出 TimeoutExpired。

此模块中定义的异常都继承自 SubprocessError。

3.3 新版功能: 基类 SubprocessError 被添加。

17.5.2. 安全考量

不像一些其他的 popen 功能，此实现绝不会隐式调用一个系统 shell。这意味着任何字符，包括 shell 元字符，可以安全地被传递给子进程。如果 shell 被明确地调用，通过 shell=True 设置，则确保所有空白字符和元字符被恰当地包裹在引号内以避免 shell 注入 [https://en.wikipedia.org/wiki/Shell_injection#Shell_injection] 漏洞就由应用程序负责了。

当使用 shell=True， shlex.quote() 函数可以作为在将被用于构造 shell 指令的字符串中转义空白字符以及 shell 元字符的方案。

17.5.3. Popen 对象

Popen 类的实例拥有以下方法：

Popen.poll()

检查子进程是否已被终止。设置并返回 returncode 属性。否则返回 None。

Popen.wait(timeout=None)

等待子进程被终止。设置并返回 returncode 属性。

如果进程在 timeout 秒后未中断，抛出一个 TimeoutExpired 异常，可以安全地捕获此异常并重新等待。

注解

当 stdout=PIPE 或者 stderr=PIPE 并且子进程产生了足以阻塞 OS 管道缓冲区接收更多数据的输出到管道时，将会发生死锁。当使用管道时用 Popen.communicate() 来规避它。

注解

此函数使用了一个 busy loop （非阻塞调用以及短睡眠） 实现。使用 asyncio 模块进行异步等待： 参阅 asyncio.create_subprocess_exec。

在 3.3 版更改: timeout 被添加

3.4 版后已移除: Do not use the endtime parameter. It is was unintentionally exposed in 3.3 but was left undocumented as it was intended to be private for internal use. Use timeout instead.

Popen.communicate(input=None, timeout=None)

与进程交互：向 stdin 传输数据。从 stdout 和 stderr 读取数据，直到文件结束符。等待进程终止。可选的 input 参数应当未被传输给子进程的数据，如果没有数据应被传输给子进程则为 None。如果流以文本模式打开， input 必须为字符串。否则，它必须为字节。

communicate() 返回一个 (stdout_data, stderr_data) 元组。如果文件以文本模式打开则为字符串；否则字节。

注意如果你想要向进程的 stdin 传输数据，你需要通过 stdin=PIPE 创建此 Popen 对象。类似的，要从结果元组获取任何非 None 值，你同样需要设置 stdout=PIPE 或者 stderr=PIPE。

如果进程在 timeout 秒后未终止，一个 TimeoutExpired 异常将被抛出。捕获此异常并重新等待将不会丢失任何输出。

如果超时到期，子进程不会被杀死，所以为了正确清理一个行为良好的应用程序应该杀死子进程并完成通讯。

proc = subprocess.Popen(...)
try:
    outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()

注解

内存里数据读取是缓冲的，所以如果数据尺寸过大或无限，不要使用此方法。

在 3.3 版更改: timeout 被添加

Popen.send_signal(signal)

将信号 signal 发送给子进程。

注解

在 Windows， SIGTERM 是一个 terminate() 的别名。 CTRL_C_EVENT 和 CTRL_BREAK_EVENT 可以被发送给以包含 CREATE_NEW_PROCESS 的 creationflags 形参启动的进程。

Popen.terminate()

Stop the child. On Posix OSs the method sends SIGTERM to the child. On Windows the Win32 API function TerminateProcess() is called to stop the child.

Popen.kill()

Kills the child. On Posix OSs the function sends SIGKILL to the child. On Windows kill() is an alias for terminate().

以下属性也是可用的：

Popen.args

args 参数传递给 Popen – 一个程序参数的序列或者一个简单字符串。

3.3 新版功能.

Popen.stdin

如果 stdin 参数为 PIPE，此属性是一个类似 open() 返回的可写的流对象。如果 encoding 或 errors 参数被指定或者 universal_newlines 参数为 True，则此流是一个文本流，否则是字节流。如果 stdin 参数非 PIPE， 此属性为 None。

Popen.stdout

如果 stdout 参数是 PIPE，此属性是一个类似 open() 返回的可读流。从流中读取子进程提供的输出。如果 encoding 或 errors 参数被指定或者 universal_newlines 参数为 True，此流为文本流，否则为字节流。如果 stdout 参数非 PIPE，此属性为 None。

Popen.stderr

如果 stderr 参数是 PIPE，此属性是一个类似 open() 返回的可读流。从流中读取子进程提供的输出。如果 encoding 或 errors 参数被指定或者 universal_newlines 参数为 True，此流为文本流，否则为字节流。如果 stderr 参数非 PIPE，此属性为 None。

警告

使用 communicate() 而非 .stdin.write， .stdout.read 或者 .stderr.read 来避免由于任意其他 OS 管道缓冲区被子进程填满阻塞而导致的死锁。

Popen.pid

子进程的进程号。

注意如果你设置了 shell 参数为 True，则这是生成的子 shell 的进程号。

Popen.returncode

此进程的退出码，由 poll() 和 wait() 设置（以及直接由 communicate() 设置）。一个 None 值 表示此进程仍未结束。

一个负值 -N 表示子进程被信号 N 中断 (仅 POSIX).

17.5.4. Windows Popen 助手

STARTUPINFO 类和以下常数仅在 Windows 有效。

class subprocess.STARTUPINFO

Partial support of the Windows STARTUPINFO [https://msdn.microsoft.com/en-us/library/ms686331(v=vs.85).aspx] structure is used for Popen creation.

dwFlags

一个位字段，用于确定进程在创建窗口时是否使用某些 STARTUPINFO 属性。

si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESTDHANDLES | subprocess.STARTF_USESHOWWINDOW

hStdInput

如果 dwFlags 被指定为 STARTF_USESTDHANDLES，则此属性是进程的标准输入句柄，如果 STARTF_USESTDHANDLES 未指定，则默认的标准输入是键盘缓冲区。

hStdOutput

如果 dwFlags 被指定为 STARTF_USESTDHANDLES，则此属性是进程的标准输出句柄。除此之外，此此属性将被忽略并且默认标准输出是控制台窗口缓冲区。

hStdError

如果 dwFlags 被指定为 STARTF_USESTDHANDLES，则此属性是进程的标准错误句柄。除此之外，此属性将被忽略并且默认标准错误为控制台窗口的缓冲区。

wShowWindow

如果 dwFlags 指定了 STARTF_USESHOWWINDOW，此属性可为能被指定为 函数 ShowWindow [https://msdn.microsoft.com/en-us/library/ms633548(v=vs.85).aspx] 的nCmdShow 的形参的任意值，除了 SW_SHOWDEFAULT。如此之外，此属性被忽略。

SW_HIDE 被提供给此属性。它在 Popen 由 shell=True 调用时使用。

17.5.4.1. Constants

subprocess 模块曝出以下常数。

subprocess.STD_INPUT_HANDLE

标准输入设备，这是控制台输入缓冲区 CONIN$。

subprocess.STD_OUTPUT_HANDLE

标准输出设备。最初，这是活动控制台屏幕缓冲区 CONOUT$。

subprocess.STD_ERROR_HANDLE

标准错误设备。最初，这是活动控制台屏幕缓冲区 CONOUT$。

subprocess.SW_HIDE

隐藏窗口。另一个窗口将被激活。

subprocess.STARTF_USESTDHANDLES

指明 STARTUPINFO.hStdInput, STARTUPINFO.hStdOutput 和 STARTUPINFO.hStdError 属性包含额外的信息。

subprocess.STARTF_USESHOWWINDOW

指明 STARTUPINFO.wShowWindow 属性包含额外的信息。

subprocess.CREATE_NEW_CONSOLE

新的进程将有新的控制台，而不是继承父进程的（默认）控制台。

subprocess.CREATE_NEW_PROCESS_GROUP

Popen creationflags 形参用于指明将创建一个新的进程组。 这个旗标对于在子进程上使用 os.kill() 来说是必须的。

如果指定了 CREATE_NEW_CONSOLE 则这个旗标会被忽略。

17.5.5. 较旧的高阶 API

在 Python 3.5 之前，这三个函数组成了 subprocess 的高阶 API。 现在你可以在许多情况下使用 run()，但有大量现在代码仍会调用这些函数。

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None)

运行由 args 所描述的命令。 等待命令完成，然后返回 returncode 属性。

这相当于:

run(...).returncode

(except that the input and check parameters are not supported)

The arguments shown above are merely the most common ones. The full function signature is largely the same as that of the Popen constructor - this function passes all supplied arguments other than timeout directly through to that interface.

注解

请不要在此函数中使用 stdout=PIPE 或 stderr=PIPE。 如果子进程向管道生成了足以填满 OS 管理缓冲区的输出而管道还未被读取时它将会阻塞。

在 3.3 版更改: timeout 被添加

subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None)

附带参数运行命令。 等待命令完成。 如果返回码为零则正常返回，否则引发 CalledProcessError。 CalledProcessError 对象将在 returncode 属性中保存返回码。

这相当于:

run(..., check=True)

(except that the input parameter is not supported)

The arguments shown above are merely the most common ones. The full function signature is largely the same as that of the Popen constructor - this function passes all supplied arguments other than timeout directly through to that interface.

注解

请不要在此函数中使用 stdout=PIPE 或 stderr=PIPE。 如果子进程向管道生成了足以填满 OS 管理缓冲区的输出而管道还未被读取时它将会阻塞。

在 3.3 版更改: timeout 被添加

subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, cwd=None, encoding=None, errors=None, universal_newlines=False, timeout=None)

附带参数运行命令并返回其输出。

如果返回码非零则会引发 CalledProcessError。 CalledProcessError 对象将在 returncode 属性中保存返回码并在 output 属性中保存所有输出。

这相当于:

run(..., check=True, stdout=PIPE).stdout

The arguments shown above are merely the most common ones. The full function signature is largely the same as that of run() - most arguments are passed directly through to that interface. However, explicitly passing input=None to inherit the parent’s standard input file handle is not supported.

默认情况下，此函数将把数据返回为已编码的字节串。 输出数据的实际编码格式将取决于发起调用的命令，因此解码为文本的操作往往需要在应用程序层级上进行处理。

This behaviour may be overridden by setting universal_newlines to True as described above in 常用参数.

要在结果中同时捕获标准错误，请使用 stderr=subprocess.STDOUT:

>>> subprocess.check_output(
...     "ls non_existent_file; exit 0",
...     stderr=subprocess.STDOUT,
...     shell=True)
'ls: non_existent_file: No such file or directory\n'

3.1 新版功能.

在 3.3 版更改: timeout 被添加

在 3.4 版更改: 增加了对 input 关键字参数的支持。

在 3.6 版更改: 增加了 encoding 和 errors。 详情参见 run()。

17.5.6. 使用 subprocess 模块替换旧函数

在这一节中，”a 改为 b” 意味着 b 可以被用作 a 的替代。

注解

在这一节中的所有 “a” 函数会在找不到被执行的程序时（差不多）静默地失败；”b” 替代则会改为引发 OSError。

此外，在使用 check_output() 时如果替代函数所请求的操作产生了非零返回值则将失败并引发 CalledProcessError。 操作的输出仍能以所引发异常的 output 属性的方式被访问。

在下列例子中，我们假定相关的函数都已从 subprocess 模块中导入了。

17.5.6.1. Replacing /bin/sh shell backquote

output=`mycmd myarg`

改为:

output = check_output(["mycmd", "myarg"])

17.5.6.2. 替代 shell 管道

output=`dmesg | grep hda`

改为:

p1 = Popen(["dmesg"], stdout=PIPE)
p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]

The p1.stdout.close() call after starting the p2 is important in order for p1 to receive a SIGPIPE if p2 exits before p1.

另外，对于受信任的输入，shell 本身的管道支持仍然可被直接使用:

output=`dmesg | grep hda`

改为:

output=check_output("dmesg | grep hda", shell=True)

17.5.6.3. 替代 os.system()

sts = os.system("mycmd" + " myarg")
# becomes
sts = call("mycmd" + " myarg", shell=True)

注释:

通过 shell 来调用程序通常是不必要的。

一个更现实的例子如下所示:

try:
    retcode = call("mycmd" + " myarg", shell=True)
    if retcode < 0:
        print("Child was terminated by signal", -retcode, file=sys.stderr)
    else:
        print("Child returned", retcode, file=sys.stderr)
except OSError as e:
    print("Execution failed:", e, file=sys.stderr)

17.5.6.4. 替代 os.spawn 函数族

P_NOWAIT 示例:

pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
==>
pid = Popen(["/bin/mycmd", "myarg"]).pid

P_WAIT 示例:

retcode = os.spawnlp(os.P_WAIT, "/bin/mycmd", "mycmd", "myarg")
==>
retcode = call(["/bin/mycmd", "myarg"])

Vector 示例:

os.spawnvp(os.P_NOWAIT, path, args)
==>
Popen([path] + args[1:])

Environment 示例:

os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env)
==>
Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})

17.5.6.5. 替代 os.popen(), os.popen2(), os.popen3()

(child_stdin, child_stdout) = os.popen2(cmd, mode, bufsize)
==>
p = Popen(cmd, shell=True, bufsize=bufsize,
          stdin=PIPE, stdout=PIPE, close_fds=True)
(child_stdin, child_stdout) = (p.stdin, p.stdout)

(child_stdin,
 child_stdout,
 child_stderr) = os.popen3(cmd, mode, bufsize)
==>
p = Popen(cmd, shell=True, bufsize=bufsize,
          stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
(child_stdin,
 child_stdout,
 child_stderr) = (p.stdin, p.stdout, p.stderr)

(child_stdin, child_stdout_and_stderr) = os.popen4(cmd, mode, bufsize)
==>
p = Popen(cmd, shell=True, bufsize=bufsize,
          stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
(child_stdin, child_stdout_and_stderr) = (p.stdin, p.stdout)

返回码以如下方式处理转写:

pipe = os.popen(cmd, 'w')
...
rc = pipe.close()
if rc is not None and rc >> 8:
    print("There were some errors")
==>
process = Popen(cmd, stdin=PIPE)
...
process.stdin.close()
if process.wait() != 0:
    print("There were some errors")

17.5.6.6. 来自 popen2 模块的替代函数

注解

如果 popen2 函数的 cmd 参数是一个字符串，命令会通过 /bin/sh 来执行。 如果是一个列表，命令会被直接执行。

(child_stdout, child_stdin) = popen2.popen2("somestring", bufsize, mode)
==>
p = Popen("somestring", shell=True, bufsize=bufsize,
          stdin=PIPE, stdout=PIPE, close_fds=True)
(child_stdout, child_stdin) = (p.stdout, p.stdin)

(child_stdout, child_stdin) = popen2.popen2(["mycmd", "myarg"], bufsize, mode)
==>
p = Popen(["mycmd", "myarg"], bufsize=bufsize,
          stdin=PIPE, stdout=PIPE, close_fds=True)
(child_stdout, child_stdin) = (p.stdout, p.stdin)

popen2.Popen3 和 popen2.Popen4 基本上类似于 subprocess.Popen，不同之处在于:

Popen 如果执行失败会引发一个异常。

the capturestderr argument is replaced with the stderr argument.

必须指定 stdin=PIPE 和 stdout=PIPE。

popen2 默认会关闭所有文件描述符，但对于 Popen 你必须指明 close_fds=True 以才能在所有平台或较旧的 Python 版本中确保此行为。

17.5.7. 旧式的 Shell 发起函数

此模块还提供了以下来自 2.x commands 模块的旧版函数。 这些操作会隐式地发起调用系统 shell 并且上文所描述的有关安全与异常处理一致性保证都不适用于这些函数。

subprocess.getstatusoutput(cmd)

返回在 shell 中执行 cmd 产生的 (exitcode, output)。

在 shell 中以 Popen.check_output() 执行字符串 cmd 并返回一个 2 元组 (exitcode, output)。 会使用当前区域设置的编码格式；请参阅 常用参数 中的说明来了解详情。

末尾的一个换行符会从输出中被去除。 命令的退出码可被解读为子进程的返回码。 例如:

>>> subprocess.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
>>> subprocess.getstatusoutput('cat /bin/junk')
(1, 'cat: /bin/junk: No such file or directory')
>>> subprocess.getstatusoutput('/bin/junk')
(127, 'sh: /bin/junk: not found')
>>> subprocess.getstatusoutput('/bin/kill $$')
(-15, '')

Availability: POSIX & Windows

在 3.3.4 版更改: 添加了 Windows 支持。

此函数现在返回 (exitcode, output) 而不是像 Python 3.3.3 及更早的版本那样返回 (status, output)。 exitcode 的值与 returncode 相同。

subprocess.getoutput(cmd)

返回在 shell 中执行 cmd 产生的输出（stdout 和 stderr）。

类似于 getstatusoutput()，但退出码会被忽略并且返回值为包含命令输出的字符串。 例如:

>>> subprocess.getoutput('ls /bin/ls')
'/bin/ls'

Availability: POSIX & Windows

在 3.3.4 版更改: 添加了 Windows 支持

17.5.8. 注释

17.5.8.1. 在 Windows 上将参数列表转换为一个字符串

在 Windows 上，args 序列会被转换为可使用以下规则来解析的字符串（对应于 MS C 运行时所使用的规则）:

参数以空白符分隔，即空格符或制表符。

用双引号标示的字符串会被解读为单个参数，而不再考虑其中的空白符。 一个参数可以嵌套用引号标示的字符串。

带有一个反斜杠前缀的双引号会被解读为双引号字面值。

反斜杠会按字面值解读，除非它是作为双引号的前缀。

如果反斜杠被作为双引号的前缀，则每个反斜杠对会被解读为一个反斜杠字面值。 如果反斜杠数量为奇数，则最后一个反斜杠会如规则 3 所描述的那样转义下一个双引号。

参见

shlex

此模块提供了用于解析和转义命令行的函数。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

17.6. sched — 事件调度器

源码： Lib/sched.py [https://github.com/python/cpython/tree/3.6/Lib/sched.py]

sched 模块定义了一个实现通用事件调度程序的类：

class sched.scheduler(timefunc=time.monotonic, delayfunc=time.sleep)

The scheduler class defines a generic interface to scheduling events. It needs two functions to actually deal with the “outside world” — timefunc should be callable without arguments, and return a number (the “time”, in any units whatsoever). If time.monotonic is not available, the timefunc default is time.time instead. The delayfunc function should be callable with one argument, compatible with the output of timefunc, and should delay that many time units. delayfunc will also be called with the argument 0 after each event is run to allow other threads an opportunity to run in multi-threaded applications.

在 3.3 版更改: timefunc 和 delayfunc 参数是可选的。

在 3.3 版更改: scheduler 类可以安全的在多线程环境中使用。

示例:

>>> import sched, time
>>> s = sched.scheduler(time.time, time.sleep)
>>> def print_time(a='default'):
...     print("From print_time", time.time(), a)
...
>>> def print_some_times():
...     print(time.time())
...     s.enter(10, 1, print_time)
...     s.enter(5, 2, print_time, argument=('positional',))
...     s.enter(5, 1, print_time, kwargs={'a': 'keyword'})
...     s.run()
...     print(time.time())
...
>>> print_some_times()
930343690.257
From print_time 930343695.274 positional
From print_time 930343695.275 keyword
From print_time 930343700.273 default
930343700.276

17.6.1. 调度器对象

scheduler 实例拥有以下方法和属性：

scheduler.enterabs(time, priority, action, argument=(), kwargs={})

安排一个新事件。 time 参数应该有一个数字类型兼容的返回值，与传递给构造函数的 timefunc 函数的返回值兼容。 计划在相同 time 的事件将按其 priority 的顺序执行。 数字越小表示优先级越高。

执行事件意为执行 action(*argument, **kwargs)。 argument 是包含有 action 的位置参数的序列。 kwargs 是包含 action 的关键字参数的字典。

返回值是一个事件，可用于以后取消事件（ 参见 cancel() ）。

在 3.3 版更改: argument 参数是可选的。

3.3 新版功能: 添加了 kwargs 形参。

scheduler.enter(delay, priority, action, argument=(), kwargs={})

安排延后 delay 时间单位的事件。 除了相对时间，其他参数、效果和返回值与 enterabs() 的相同。

在 3.3 版更改: argument 参数是可选的。

3.3 新版功能: 添加了 kwargs 形参。

scheduler.cancel(event)

从队列中删除事件。 如果 event 不是当前队列中的事件，则此方法将引发 ValueError。

scheduler.empty()

Return true if the event queue is empty.

scheduler.run(blocking=True)

运行所有预定事件。 此方法将等待（使用传递给构造函数的 delayfunc() 函数）进行下一个事件，然后执行它，依此类推，直到没有更多的计划事件。

如果 blocking 为false，则执行由于最快到期（如果有）的预定事件，然后在调度程序中返回下一个预定调用的截止时间（如果有）。

action 或 delayfunc 都可以引发异常。 在任何一种情况下，调度程序都将保持一致状态并传播异常。 如果 action 引发异常，则在将来调用 run() 时不会尝试该事件。

如果一系列事件的运行时间比下一个事件之前的可用时间长，那么调度程序将完全落后。 不会发生任何事件；调用代码负责取消不再相关的事件。

3.3 新版功能: 添加了 blocking 形参。

scheduler.queue

只读属性按照将要运行的顺序返回即将发生的事件列表。 每个事件都显示为 named tuple ，包含以下字段：time、priority、action、argument、kwargs。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

17.7. queue — 一个同步的队列类

源代码: Lib/queue.py [https://github.com/python/cpython/tree/3.6/Lib/queue.py]

queue 模块实现多生产者，多消费者队列。当信息必须安全的在多线程之间交换时，它在线程编程中是特别有用的。此模块中的 Queue 类实现了所有锁定需求的语义。它依赖于Python支持的线程可用性；请参阅 threading 模块。

模块实现了三种类型的队列，它们的区别仅仅是条目取回的顺序。在 FIFO 队列中，先添加的任务先取回。在 LIFO 队列中，最近被添加的条目先取回(操作类似一个堆栈)。优先级队列中，条目将保持排序( 使用 heapq 模块 ) 并且最小值的条目第一个返回。

Internally, the module uses locks to temporarily block competing threads; however, it is not designed to handle reentrancy within a thread.

queue 模块定义了下列类和异常：

class queue.Queue(maxsize=0)

Constructor for a FIFO queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.

class queue.LifoQueue(maxsize=0)

LIFO 队列构造函数。 maxsize 是个整数，用于设置可以放入队列中的项目数的上限。当达到这个大小的时候，插入操作将阻塞至队列中的项目被消费掉。如果 maxsize 小于等于零，队列尺寸为无限大。

class queue.PriorityQueue(maxsize=0)

优先级队列构造函数。 maxsize 是个整数，用于设置可以放入队列中的项目数的上限。当达到这个大小的时候，插入操作将阻塞至队列中的项目被消费掉。如果 maxsize 小于等于零，队列尺寸为无限大。

最小值先被取出( 最小值条目是由 sorted(list(entries))[0] 返回的条目)。条目的典型模式是一个以下形式的元组： (priority_number, data) 。

exception queue.Empty

对空的 Queue 对象，调用非阻塞的 get() (or get_nowait()) 时，引发的异常。

exception queue.Full

对满的 Queue 对象，调用非阻塞的 put() (or put_nowait()) 时，引发的异常。

17.7.1. Queue对象

队列对象 (Queue, LifoQueue, 或者 PriorityQueue) 提供下列描述的公共方法。

Queue.qsize()

返回队列的大致大小。注意，qsize() > 0 不保证后续的 get() 不被阻塞，qsize() < maxsize 也不保证 put() 不被阻塞。

Queue.empty()

如果队列为空，返回 True ，否则返回 False 。如果 empty() 返回 True ，不保证后续调用的 put() 不被阻塞。类似的，如果 empty() 返回 False ，也不保证后续调用的 get() 不被阻塞。

Queue.full()

如果队列是满的返回 True ，否则返回 False 。如果 full() 返回 True 不保证后续调用的 get() 不被阻塞。类似的，如果 full() 返回 False 也不保证后续调用的 put() 不被阻塞。

Queue.put(item, block=True, timeout=None)

将 item 放入队列。如果可选参数 block 是 true 并且 timeout 是 None (默认)，则在必要时阻塞至有空闲插槽可用。如果 timeout 是个正数，将最多阻塞 timeout 秒，如果在这段时间没有可用的空闲插槽，将引发 Full 异常。反之 (block 是 false)，如果空闲插槽立即可用，则把 item 放入队列，否则引发 Full 异常 ( 在这种情况下，timeout 将被忽略)。

Queue.put_nowait(item)

相当于 put(item, False) 。

Queue.get(block=True, timeout=None)

从队列中移除并返回一个项目。如果可选参数 block 是 true 并且 timeout 是 None (默认值)，则在必要时阻塞至项目可得到。如果 timeout 是个正数，将最多阻塞 timeout 秒，如果在这段时间内项目不能得到，将引发 Empty 异常。反之 (block 是 false) , 如果一个项目立即可得到，则返回一个项目，否则引发 Empty 异常 (这种情况下，timeout 将被忽略)。

Queue.get_nowait()

相当于 get(False) 。

提供了两个方法，用于支持跟踪 排队的任务 是否 被守护的消费者线程 完整的处理。

Queue.task_done()

表示前面排队的任务已经被完成。被队列的消费者线程使用。每个 get() 被用于获取一个任务， 后续调用 task_done() 告诉队列，该任务的处理已经完成。

如果 join() 当前正在阻塞，在所有条目都被处理后，将解除阻塞(意味着每个 put() 进队列的条目的 task_done() 都被收到)。

如果被调用的次数多于放入队列中的项目数量，将引发 ValueError 异常 。

Queue.join()

阻塞至队列中所有的元素都被接收和处理完毕。

当条目添加到队列的时候，未完成任务的计数就会增加。每当消费者线程调用 task_done() 表示这个条目已经被回收，该条目所有工作已经完成，未完成计数就会减少。当未完成计数降到零的时候， join() 阻塞被解除。

如何等待排队的任务被完成的示例：

def worker():
    while True:
        item = q.get()
        if item is None:
            break
        do_work(item)
        q.task_done()

q = queue.Queue()
threads = []
for i in range(num_worker_threads):
    t = threading.Thread(target=worker)
    t.start()
    threads.append(t)

for item in source():
    q.put(item)

# block until all tasks are done
q.join()

# stop workers
for i in range(num_worker_threads):
    q.put(None)
for t in threads:
    t.join()

参见

类 multiprocessing.Queue

一个用于多进程上下文的队列类（而不是多线程）。

collections.deque 是无界队列的替代实现，具有快速原子的 append() 和 popleft() 操作，不需要锁定。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

17.8. dummy_threading — 可直接替代 threading 模块。

源代码： Lib/dummy_threading.py [https://github.com/python/cpython/tree/3.6/Lib/dummy_threading.py]

This module provides a duplicate interface to the threading module. It is meant to be imported when the _thread module is not provided on a platform.

Suggested usage is:

try:
    import threading
except ImportError:
    import dummy_threading as threading

如果线程需要阻塞等待另一个线程被创建的话，可能会造成死锁，这通常是由于阻塞 I/O 引起的。这种场景下请不要使用这个模块。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

17.9. _thread — 底层多线程 API

该模块提供了操作多个线程（也被称为 轻量级进程 或 任务）的底层原语 —— 多个控制线程共享全局数据空间。为了处理同步问题，也提供了简单的锁机制（也称为 互斥锁 或 二进制信号）。threading 模块基于该模块提供了更易用的高级多线程 API。

The module is optional. It is supported on Windows, Linux, SGI IRIX, Solaris 2.x, as well as on systems that have a POSIX thread (a.k.a. “pthread”) implementation. For systems lacking the _thread module, the _dummy_thread module is available. It duplicates this module’s interface and can be used as a drop-in replacement.

It defines the following constants and functions:

exception _thread.error

发生线程相关错误时抛出。

在 3.3 版更改: 现在是内建异常 RuntimeError 的别名。

_thread.LockType

锁对象的类型。

_thread.start_new_thread(function, args[, kwargs])

启动一个线程，并返回其标识符。线程会用 args 作为参数（必须是元组）执行 function 函数。可选的 kwargs 参数使用字典来指定有名参数。当函数返回时，线程会静默退出，当函数由于未处理的异常而中止时，会打印一条堆栈追踪信息，然后该线程会退出（但其他线程还是会继续运行）。

_thread.interrupt_main()

Raise a KeyboardInterrupt exception in the main thread. A subthread can use this function to interrupt the main thread.

_thread.exit()

抛出 SystemExit 异常。如果没有捕获的话，这个异常会使线程退出。

_thread.allocate_lock()

返回一个新的锁对象。锁中的方法在后面描述。初始情况下锁处于解锁状态。

_thread.get_ident()

返回当前线程的 “线程标识符”。它是一个非零的整数。它的值没有直接含义，主要是用作 magic cookie，比如作为含有线程相关数据的字典的索引。线程标识符可能会在线程退出，新线程创建时被复用。

_thread.stack_size([size])

Return the thread stack size used when creating new threads. The optional size argument specifies the stack size to be used for subsequently created threads, and must be 0 (use platform or configured default) or a positive integer value of at least 32,768 (32 KiB). If size is not specified, 0 is used. If changing the thread stack size is unsupported, a RuntimeError is raised. If the specified stack size is invalid, a ValueError is raised and the stack size is unmodified. 32 KiB is currently the minimum supported stack size value to guarantee sufficient stack space for the interpreter itself. Note that some platforms may have particular restrictions on values for the stack size, such as requiring a minimum stack size > 32 KiB or requiring allocation in multiples of the system memory page size - platform documentation should be referred to for more information (4 KiB pages are common; using multiples of 4096 for the stack size is the suggested approach in the absence of more specific information). Availability: Windows, systems with POSIX threads.

_thread.TIMEOUT_MAX

Lock.acquire() 方法中 timeout 参数允许的最大值。传入超过这个值的 timeout 会抛出 OverflowError 异常。

3.2 新版功能.

锁对象有以下方法：

lock.acquire(waitflag=1, timeout=-1)

没有任何可选参数时，该方法无条件申请获得锁，有必要的话会等待其他线程释放锁（同时只有一个线程能获得锁 —— 这正是锁存在的原因）。

如果传入了整型参数 waitflag，具体的行为取决于传入的值：如果是 0 的话，只会在能够立刻获取到锁时才获取，不会等待，如果是非零的话，会像之前提到的一样，无条件获取锁。

如果传入正浮点数参数 timeout，相当于指定了返回之前等待得最大秒数。如果传入负的 timeout，相当于无限期等待。如果 waitflag 是 0 的话，不能指定 timeout。

如果成功获取到所会返回 True，否则返回 False。

在 3.2 版更改: 新的 timeout 形参。

在 3.2 版更改: 现在获取锁的操作可以被 POSIX 信号中断。

lock.release()

释放锁。锁必须已经被获取过，但不一定是同一个线程获取的。

lock.locked()

返回锁的状态：如果已被某个线程获取，返回 True，否则返回 False。

除了这些方法之外，锁对象也可以通过 with 语句使用，例如：

import _thread

a_lock = _thread.allocate_lock()

with a_lock:
    print("a_lock is locked while this executes")

注意事项：

线程与中断奇怪地交互：KeyboardInterrupt 异常可能会被任意一个线程捕获。（如果 signal 模块可用的话，中断总是会进入主线程。）

调用 sys.exit() 或是抛出 SystemExit 异常等效于调用 _thread.exit()。

不可能中断锁的 acquire() 方法 —— KeyboardInterrupt 一场会在锁获取到之后发生。

当主线程退出时，由系统决定其他线程是否存活。在大多数系统中，这些线程会直接被杀掉，不会执行 try … finally 语句，也不会执行对象析构函数。

当主线程退出时，不会进行正常的清理工作（除非使用了 try … finally 语句），标准 I/O 文件也不会刷新。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

17.10. _dummy_thread — _thread 的替代模块

源代码： Lib/_dummy_thread.py [https://github.com/python/cpython/tree/3.6/Lib/_dummy_thread.py]

This module provides a duplicate interface to the _thread module. It is meant to be imported when the _thread module is not provided on a platform.

Suggested usage is:

try:
    import _thread
except ImportError:
    import _dummy_thread as _thread

如果线程需要阻塞等待另一个线程被创建的话，可能会造成死锁，这通常是由于阻塞 I/O 引起的。这种场景下请不要使用这个模块。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

17. 并发执行 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

18. Interprocess Communication and Networking

The modules described in this chapter provide mechanisms for different processes to communicate.

某些模块仅适用于同一台机器上的两个进程，例如 signal 和 mmap 。 其他模块支持两个或多个进程可用于跨机器通信的网络协议。

本章中描述的模块列表是：

18.1. socket — 底层网络接口

18.1.1. 套接字协议族

18.1.2. 模块内容

18.1.2.1. 异常

18.1.2.2. 常量

18.1.2.3. 函数

18.1.2.3.1. 创建套接字

18.1.2.3.2. 其他功能

18.1.3. 套接字对象

18.1.4. 关于套接字超时的说明

18.1.4.1. 超时与 connect 方法

18.1.4.2. 超时与 accept 方法

18.1.5. 示例

18.2. ssl — 套接字对象的TLS/SSL封装

18.2.1. 方法、常量和异常处理

18.2.1.1. 套接字创建

18.2.1.2. 上下文创建

18.2.1.3. 随机生成

18.2.1.4. 证书处理

18.2.1.5. 常量

18.2.2. SSL 套接字

18.2.3. SSL 上下文

18.2.4. 证书

18.2.4.1. 证书链

18.2.4.2. CA 证书

18.2.4.3. 合并的密钥和证书

18.2.4.4. 自签名证书

18.2.5. 例子

18.2.5.1. 检测 SSL 支持

18.2.5.2. 客户端操作

18.2.5.3. 服务器端操作

18.2.6. 关于非阻塞套接字的说明

18.2.7. 内存 BIO 支持

18.2.8. SSL 会话

18.2.9. 安全考量

18.2.9.1. 最佳默认值

18.2.9.2. 手动设置

18.2.9.2.1. 验证证书

18.2.9.2.2. 协议版本

18.2.9.2.3. 密码选择

18.2.9.3. 多进程

18.2.10. LibreSSL 支持

18.3. select — Waiting for I/O 完成

18.3.1. /dev/poll 轮询对象

18.3.2. 边缘触发和水平触发的轮询 (epoll) 对象

18.3.3. Poll 对象

18.3.4. Kqueue 对象

18.3.5. Kevent 对象

18.4. selectors — 高级 I/O 复用库

18.4.1. 概述

18.4.2. 类

18.4.3. 例子

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks

18.5.1. Base Event Loop

18.5.1.1. Run an event loop

18.5.1.2. Calls

18.5.1.3. Delayed calls

18.5.1.4. Futures

18.5.1.5. Tasks

18.5.1.6. Creating connections

18.5.1.7. Creating listening connections

18.5.1.8. Watch file descriptors

18.5.1.9. Low-level socket operations

18.5.1.10. Resolve host name

18.5.1.11. Connect pipes

18.5.1.12. UNIX signals

18.5.1.13. Executor

18.5.1.14. 错误处理API

18.5.1.15. Debug mode

18.5.1.16. Server

18.5.1.17. Handle

18.5.1.18. Event loop examples

18.5.1.18.1. call_soon() 的 Hello World 示例。

18.5.1.18.2. 使用 call_later() 来展示当前的日期

18.5.1.18.3. 监控一个文件描述符的读事件

18.5.1.18.4. 为SIGINT和SIGTERM设置信号处理器

18.5.2. 事件循环

18.5.2.1. 事件循环函数

18.5.2.2. 可用的事件循环

18.5.2.3. 平台支持

18.5.2.3.1. Windows

18.5.2.3.2. Mac OS X

18.5.2.4. 事件循环策略和默认策略

18.5.2.5. 事件循环策略接口

18.5.2.6. 访问全局循环策略

18.5.2.7. 自定义事件循环策略

18.5.3. Tasks and coroutines

18.5.3.1. 协程

18.5.3.1.1. Example: Hello World coroutine

18.5.3.1.2. Example: Coroutine displaying the current date

18.5.3.1.3. Example: Chain coroutines

18.5.3.2. InvalidStateError

18.5.3.3. TimeoutError

18.5.3.4. Future

18.5.3.4.1. Example: Future with run_until_complete()

18.5.3.4.2. Example: Future with run_forever()

18.5.3.5. Task

18.5.3.5.1. Example: Parallel execution of tasks

18.5.3.6. Task functions

18.5.4. Transports and protocols (callback based API)

18.5.4.1. 传输

18.5.4.1.1. BaseTransport

18.5.4.1.2. ReadTransport

18.5.4.1.3. WriteTransport

18.5.4.1.4. DatagramTransport

18.5.4.1.5. BaseSubprocessTransport

18.5.4.2. 协议

18.5.4.2.1. Protocol classes

18.5.4.2.2. Connection callbacks

18.5.4.2.3. Streaming protocols

18.5.4.2.4. Datagram protocols

18.5.4.2.5. Flow control callbacks

18.5.4.2.6. Coroutines and protocols

18.5.4.3. Protocol examples

18.5.4.3.1. TCP echo client protocol

18.5.4.3.2. TCP echo server protocol

18.5.4.3.3. UDP echo client protocol

18.5.4.3.4. UDP echo server protocol

18.5.4.3.5. Register an open socket to wait for data using a protocol

18.5.5. Streams (coroutine based API)

18.5.5.1. Stream functions

18.5.5.2. StreamReader

18.5.5.3. StreamWriter

18.5.5.4. StreamReaderProtocol

18.5.5.5. IncompleteReadError

18.5.5.6. LimitOverrunError

18.5.5.7. Stream examples

18.5.5.7.1. 使用流的 TCP 回显客户端

18.5.5.7.2. 使用流的 TCP 回显服务器

18.5.5.7.3. 获取 HTTP 标头

18.5.5.7.4. 注册一个打开的套接字以等待使用流的数据

18.5.6. Subprocess

18.5.6.1. Windows event loop

18.5.6.2. Create a subprocess: high-level API using Process

18.5.6.3. Create a subprocess: low-level API using subprocess.Popen

18.5.6.4. 常量

18.5.6.5. Process

18.5.6.6. Subprocess and threads

18.5.6.7. Subprocess examples

18.5.6.7.1. Subprocess using transport and protocol

18.5.6.7.2. Subprocess using streams

18.5.7. Synchronization primitives

18.5.7.1. Locks

18.5.7.1.1. Lock

18.5.7.1.2. Event

18.5.7.1.3. Condition

18.5.7.2. Semaphores

18.5.7.2.1. Semaphore

18.5.7.2.2. BoundedSemaphore

18.5.8. 队列集

18.5.8.1. 队列

18.5.8.2. PriorityQueue

18.5.8.3. LifoQueue

18.5.8.3.1. 异常

18.5.9. Develop with asyncio

18.5.9.1. Debug mode of asyncio

18.5.9.2. Cancellation

18.5.9.3. Concurrency and multithreading

18.5.9.4. Handle blocking functions correctly

18.5.9.5. 日志记录

18.5.9.6. Detect coroutine objects never scheduled

18.5.9.7. Detect exceptions never consumed

18.5.9.8. Chain coroutines correctly

18.5.9.9. Pending task destroyed

18.5.9.10. Close transports and event loops

18.6. asyncore — 异步socket处理器

18.6.1. asyncore 示例基本 HTTP 客户端

18.6.2. asyncore 示例基本回显服务器

18.7. asynchat — 异步 socket 指令/响应 处理器

18.7.1. asynchat 示例

18.8. signal — 设置异步事件处理程序

18.8.1. 一般规则

18.8.1.1. 执行 Python 信号处理程序

18.8.1.2. 信号与线程

18.8.2. 模块内容

18.8.3. 示例

18.9. mmap — 内存映射文件支持

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

18.1. socket — 底层网络接口

源代码: Lib/socket.py [https://github.com/python/cpython/tree/3.6/Lib/socket.py]

这个模块提供了访问BSD*套接字*的接口。在所有现代Unix系统、Windows、macOS和其他一些平台上可用。

注解

一些行为可能因平台不同而异，因为调用的是操作系统的套接字API。

这个Python接口是用Python的面向对象风格对Unix系统调用和套接字库接口的直译：函数 socket() 返回一个 套接字对象 ，其方法是对各种套接字系统调用的实现。形参类型一般与C接口相比更高级：例如在Python文件 read() 和 write() 操作中，接收操作的缓冲区分配是自动的，发送操作的缓冲区长度是隐式的。

参见

模块 socketserver

用于简化网络服务端编写的类。

模块 ssl

套接字对象的TLS/SSL封装。

18.1.1. 套接字协议族

根据系统以及构建选项，此模块提供了各种 socket 协议簇。

特定的 socket 对象需要的地址格式将根据此 socket 对象被创建时指定的地址族被自动选择。socket 地址表示如下：

一个绑定在文件系统节点上的 AF_UNIX 套接字的地址表示为一个字符串，使用文件系统字符编码和 'surrogateescape' 错误回调方法（see PEP 383 [https://www.python.org/dev/peps/pep-0383]）。一个地址在 Linux 的抽象命名空间被返回为带有初始的 null 字节的 bytes-like object ；注意在这个命名空间种的套接字可能与普通文件系统套接字通信，所以打算运行在 Linux 上的程序可能需要解决两种地址类型。当传递为参数时，一个字符串或字节类对象可以用于任一类型的地址。

在 3.3 版更改: 以前，AF_UNIX 套接字路径被假设使用 UTF-8 编码。

在 3.5 版更改: 现在支持可写的 字节类对象。

一对 (host, port) 被用于 AF_INET 地址族，host 是一个代表互联网域名表示法之内主机名或者一个 IPv4 地址的字符串，例如 'daring.cwi.nl' 或 '100.50.200.5'，port 是一个整数。

对于 IPv4 地址，有两种可接受的特殊形式被用来代替一个主机地址： '' 代表 INADDR_ANY，用来绑定到所有接口；字符串 '<broadcast>' 代表 INADDR_BROADCAST。此行为不兼容 IPv6，因此，如果你的 Python 程序打算支持 IPv6，则可能需要避开这些。

对于 AF_INET6 地址族，使用一个四元组 (host, port, flowinfo, scopeid)， flowinfo 和 scopeid 代表了 C 库里 struct sockaddr_in6 中的 sin6_flowinfo 和 sin6_scope_id 成员。对于 socket 模块中的方法， flowinfo 和 scopeid 可以被省略，只为了向后兼容。注意，scopeid 的省略可能会导致 IPv6 地址的操作范围问题。

AF_NETLINK 套接字由一对 (pid, groups) 表示。

指定 AF_TIPC 地址族可以使用仅 Linux 支持的 TIPC 协议。TIPC 是一种开放的、非基于 IP 的网络协议，旨在用于集群计算环境。其地址用元组表示，其中的字段取决于地址类型。一般元组形式为 (addr_type, v1, v2, v3 [, scope])，其中：

addr_type 取 TIPC_ADDR_NAMESEQ、TIPC_ADDR_NAME 或 TIPC_ADDR_ID 中的一个。

scope 取 TIPC_ZONE_SCOPE、TIPC_CLUSTER_SCOPE 和 TIPC_NODE_SCOPE 中的一个。

如果 addr_type 为 TIPC_ADDR_NAME，那么 v1 是服务器类型，v2 是端口标识符，v3 应为 0。

如果 addr_type 为 TIPC_ADDR_NAMESEQ，那么 v1 是服务器类型，v2 是端口号下限，而 v3 是端口号上限。

如果 addr_type 为 TIPC_ADDR_ID，那么 v1 是节点 (node)，v2 是 ref，v3 应为 0。

AF_CAN 地址族使用元组 (interface, )，其中 interface 是表示网络接口名称的字符串，如 'can0'。网络接口名 '' 可以用于接收本族所有网络接口的数据包。

PF_SYSTEM 协议簇的 SYSPROTO_CONTROL 协议接受一个字符串或元组 (id, unit)。其中字符串是内核控件的名称，该控件使用动态分配的 ID。而如果 ID 和内核控件的单元 (unit) 编号都已知，或使用了已注册的 ID，可以采用元组。

3.3 新版功能.

AF_BLUETOOTH 支持以下协议和地址格式：

BTPROTO_L2CAP 接受 (bdaddr, psm)，其中 bdaddr 为字符串格式的蓝牙地址，psm 是一个整数。

BTPROTO_RFCOMM 接受 (bdaddr, channel)，其中 bdaddr 为字符串格式的蓝牙地址，channel 是一个整数。

BTPROTO_HCI 接受 (device_id,)，其中 device_id 为整数或字符串，它表示接口对应的蓝牙地址（具体取决于你的系统，NetBSD 和 DragonFlyBSD 需要蓝牙地址字符串，其他系统需要整数）。

在 3.2 版更改: 添加 NetBSD 和 DragonFlyBSD 支持。

BTPROTO_SCO 接受 bdaddr，其中 bdaddr 是 bytes 对象，其中含有字符串格式的蓝牙地址（如 b'12:23:34:45:56:67' ），FreeBSD 不支持此协议。

AF_ALG 是一个仅 Linux 可用的、基于套接字的接口，用于连接内核加密算法。算法套接字可用包括 2 至 4 个元素的元组来配置 (type, name [, feat [, mask]])，其中：

type 是表示算法类型的字符串，如 aead、hash、skcipher 或 rng。

name 是表示算法类型和操作模式的字符串，如 sha256、hmac(sha256)、cbc(aes) 或 drbg_nopr_ctr_aes256。

feat 和 mask 是无符号 32 位整数。

Availability Linux 2.6.38, some algorithm types require more recent Kernels.

3.6 新版功能.

AF_PACKET 是一个底层接口，直接连接至网卡。数据包使用元组 (ifname, proto[, pkttype[, hatype[, addr]]]) 表示，其中：

ifname - 指定设备名称的字符串。

proto - 一个用网络字节序表示的整数，指定以太网协议版本号。

pkttype - 指定数据包类型的整数（可选）：

PACKET_HOST （默认） - 数据包寻址到本地宿主机。

PACKET_BROADCAST - 物理层广播报文。

PACKET_MULTIHOST - 数据包发送到物理层多播地址。

PACKET_OTHERHOST - 被（处于混杂模式的）网卡驱动捕获的、发送到其他主机的数据包。

PACKET_OUTGOING - 来自本地主机的、回环到一个套接字的数据包。

hatype - 可选整数，指定 ARP 硬件地址类型。

addr - 可选的类字节串对象，用于指定硬件物理地址，其解释取决于各设备。

如果你在 IPv4/v6 套接字地址的 host 部分中使用了一个主机名，此程序可能会表现不确定行为，因为 Python 使用 DNS 解析返回的第一个地址。套接字地址在实际的 IPv4/v6 中以不同方式解析，根据 DNS 解析和/或 host 配置。为了确定行为，在 host 部分中使用数字地址。

所有的错误都抛出异常。对于无效参数类型和内存溢出异常情况可能抛出普通异常；从 Python 3.3 开始，与套接字或地址语义有关的错误抛出 OSError 或它的子类之一（常用 socket.error）。

可以用 setblocking() 设置非阻塞模式。一个基于超时的 generalization 通过 settimeout() 支持。

18.1.2. 模块内容

socket 模块导出以下元素。

18.1.2.1. 异常

exception socket.error

一个被弃用的 OSError 的别名。

在 3.3 版更改: 根据 PEP 3151 [https://www.python.org/dev/peps/pep-3151]，这个类是 OSError 的别名。

exception socket.herror

OSError 的子类，本异常通常表示与地址相关的错误，比如那些在 POSIX C API 中使用了 h_errno 的函数，包括 gethostbyname_ex() 和 gethostbyaddr()。附带的值是一对 (h_errno, string)，代表库调用返回的错误。h_errno 是一个数字，而 string 表示 h_errno 的描述，它们由 C 函数 hstrerror() 返回。

在 3.3 版更改: 此类是 OSError 的子类。

exception socket.gaierror

OSError 的子类，本异常来自 getaddrinfo() 和 getnameinfo()，表示与地址相关的错误。附带的值是一对 (error, string)，代表库调用返回的错误。string 表示 error 的描述，它由 C 函数 gai_strerror() 返回。数字值 error 与本模块中定义的 EAI_* 常量之一匹配。

在 3.3 版更改: 此类是 OSError 的子类。

exception socket.timeout

OSError 的子类，当套接字发生超时，且事先已调用过 settimeout() （或隐式地通过 setdefaulttimeout() ）启用了超时，则会抛出此异常。附带的值是一个字符串，其值总是 “timed out”。

在 3.3 版更改: 此类是 OSError 的子类。

18.1.2.2. 常量

AF_* 和 SOCK_* 常量现在都在 AddressFamily 和 SocketKind 这两个 IntEnum 集合内。

3.4 新版功能.

socket.AF_UNIX

socket.AF_INET

socket.AF_INET6

这些常量表示地址（和协议）簇，用于 socket() 的第一个参数。如果 AF_UNIX 常量未定义，即表示不支持该协议。不同系统可能会有更多其他常量可用。

socket.SOCK_STREAM

socket.SOCK_DGRAM

socket.SOCK_RAW

socket.SOCK_RDM

socket.SOCK_SEQPACKET

这些常量表示套接字类型，用于 socket() 的第二个参数。不同系统可能会有更多其他常量可用。（一般只有 SOCK_STREAM 和 SOCK_DGRAM 可用）

socket.SOCK_CLOEXEC

socket.SOCK_NONBLOCK

这两个常量（如果已定义）可以与上述套接字类型结合使用，并允许你设置一些原子性的 flags （从而避免可能的竞争条件和单独调用的需要）。

参见

Secure File Descriptor Handling （安全地处理文件描述符） [http://udrepper.livejournal.com/20407.html] 提供了更详尽的解释。

Availability: Linux >= 2.6.27.

3.2 新版功能.

SO_*

socket.SOMAXCONN

MSG_*

SOL_*

SCM_*

IPPROTO_*

IPPORT_*

INADDR_*

IP_*

IPV6_*

EAI_*

AI_*

NI_*

TCP_*

此列表内的许多常量，记载在 Unix 文档中的套接字和/或 IP 协议部分，同时也定义在本 socket 模块中。它们通常用于套接字对象的 setsockopt() 和 getsockopt() 方法的参数中。在大多数情况下，仅那些在 Unix 头文件中有定义的符号会在本模块中定义，部分符号提供了默认值。

在 3.6 版更改: 添加了 SO_DOMAIN、SO_PROTOCOL、SO_PEERSEC、SO_PASSSEC、TCP_USER_TIMEOUT、TCP_CONGESTION。

在 3.6.5 版更改: 在 Windows 上，如果 Windows 运行时支持，则 TCP_FASTOPEN、TCP_KEEPCNT 可用。

socket.AF_CAN

socket.PF_CAN

SOL_CAN_*

CAN_*

此列表内的许多常量，记载在 Linux 文档中，同时也定义在本 socket 模块中。

Availability: Linux >= 2.6.25.

3.3 新版功能.

socket.CAN_BCM

CAN_BCM_*

CAN 协议簇内的 CAN_BCM 是广播管理器（Bbroadcast Manager – BCM）协议，广播管理器常量在 Linux 文档中有所记载，在本 socket 模块中也有定义。

Availability: Linux >= 2.6.25.

3.4 新版功能.

socket.CAN_RAW_FD_FRAMES

Enables CAN FD support in a CAN_RAW socket. This is disabled by default. This allows your application to send both CAN and CAN FD frames; however, you one must accept both CAN and CAN FD frames when reading from the socket.

此常量在 Linux 文档中有所记载。

Availability: Linux >= 3.6.

3.5 新版功能.

socket.AF_PACKET

socket.PF_PACKET

PACKET_*

此列表内的许多常量，记载在 Linux 文档中，同时也定义在本 socket 模块中。

Availability: Linux >= 2.2.

socket.AF_RDS

socket.PF_RDS

socket.SOL_RDS

RDS_*

此列表内的许多常量，记载在 Linux 文档中，同时也定义在本 socket 模块中。

Availability: Linux >= 2.6.30.

3.3 新版功能.

socket.SIO_RCVALL

socket.SIO_KEEPALIVE_VALS

socket.SIO_LOOPBACK_FAST_PATH

RCVALL_*

Windows 的 WSAIoctl() 的常量。这些常量用于套接字对象的 ioctl() 方法的参数。

在 3.6 版更改: 添加了 SIO_LOOPBACK_FAST_PATH。

TIPC_*

TIPC 相关常量，与 C socket API 导出的常量一致。更多信息请参阅 TIPC 文档。

socket.AF_ALG

socket.SOL_ALG

ALG_*

用于 Linux 内核加密算法的常量。

Availability: Linux >= 2.6.38.

3.6 新版功能.

socket.AF_LINK

Availability: BSD, OSX.

3.4 新版功能.

socket.has_ipv6

本常量为一个布尔值，该值指示当前平台是否支持 IPv6。

socket.BDADDR_ANY

socket.BDADDR_LOCAL

这些是字符串常量，包含蓝牙地址，这些地址具有特殊含义。例如，当用 BTPROTO_RFCOMM 指定绑定套接字时， BDADDR_ANY 表示“任何地址”。

socket.HCI_FILTER

socket.HCI_TIME_STAMP

socket.HCI_DATA_DIR

与 BTPROTO_HCI 一起使用。 HCI_FILTER 在 NetBSD 或 DragonFlyBSD 上不可用。 HCI_TIME_STAMP 和 HCI_DATA_DIR 在 FreeBSD、NetBSD 或 DragonFlyBSD 上不可用。

18.1.2.3. 函数

18.1.2.3.1. 创建套接字

下列函数都能创建 套接字对象.

socket.socket(family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None)

Create a new socket using the given address family, socket type and protocol number. The address family should be AF_INET (the default), AF_INET6, AF_UNIX, AF_CAN, AF_PACKET, or AF_RDS. The socket type should be SOCK_STREAM (the default), SOCK_DGRAM, SOCK_RAW or perhaps one of the other SOCK_ constants. The protocol number is usually zero and may be omitted or in the case where the address family is AF_CAN the protocol should be one of CAN_RAW or CAN_BCM. If fileno is specified, the other arguments are ignored, causing the socket with the specified file descriptor to return. Unlike socket.fromfd(), fileno will return the same socket and not a duplicate. This may help close a detached socket using socket.close().

新创建的套接字是 不可继承的。

在 3.3 版更改: 添加了 AF_CAN 簇。添加了 AF_RDS 簇。

在 3.4 版更改: 添加了 CAN_BCM 协议。

在 3.4 版更改: 返回的套接字现在是不可继承的。

socket.socketpair([family[, type[, proto]]])

构建一对已连接的套接字对象，使用给定的地址簇、套接字类型和协议号。地址簇、套接字类型和协议号与上述 socket() 函数相同。默认地址簇为 AF_UNIX （需要当前平台支持，不支持则默认为 AF_INET ）。

新创建的套接字都是 不可继承的。

在 3.2 版更改: 现在，返回的套接字对象支持全部套接字 API，而不是全部 API 的一个子集。

在 3.4 版更改: 返回的套接字现在都是不可继承的。

在 3.5 版更改: 添加了 Windows 支持。

socket.create_connection(address[, timeout[, source_address]])

连接到一个 TCP 服务，该服务正在侦听 Internet address （用二元组 (host, port) 表示）。连接后返回套接字对象。这是比 socket.connect() 更高级的函数：如果 host 是非数字主机名，它将尝试从 AF_INET 和 AF_INET6 解析它，然后依次尝试连接到所有可能的地址，直到连接成功。这使得编写兼容 IPv4 和 IPv6 的客户端变得容易。

传入可选参数 timeout 可以在套接字实例上设置超时（在尝试连接前）。如果未提供 timeout，则使用由 getdefaulttimeout() 返回的全局默认超时设置。

如果提供了 source_address，它必须为二元组 (host, port)，以便套接字在连接之前绑定为其源地址。如果 host 或 port 分别为 ‘’ 或 0，则使用操作系统默认行为。

在 3.2 版更改: 添加了*source_address* 参数

socket.fromfd(fd, family, type, proto=0)

复制文件描述符 fd （一个由文件对象的 fileno() 方法返回的整数），然后从结果中构建一个套接字对象。地址簇、套接字类型和协议号与上述 socket() 函数相同。文件描述符应指向一个套接字，但不会专门去检查——如果文件描述符是无效的，则对该对象的后续操作可能会失败。本函数很少用到，但是在将套接字作为标准输入或输出传递给程序（如 Unix inet 守护程序启动的服务器）时，可以使用本函数获取或设置套接字选项。套接字须处于阻塞模式。

新创建的套接字是 不可继承的。

在 3.4 版更改: 返回的套接字现在是不可继承的。

socket.fromshare(data)

根据 socket.share() 方法获得的数据实例化套接字。套接字须处于阻塞模式。

Availability: Windows.

3.3 新版功能.

socket.SocketType

这是一个 Python 类型对象，表示套接字对象的类型。它等同于 type(socket(...))。

18.1.2.3.2. 其他功能

socket 模块还提供各种与网络相关的服务：

socket.getaddrinfo(host, port, family=0, type=0, proto=0, flags=0)

将 host/port 参数转换为 5 元组的序列，其中包含创建（连接到某服务的）套接字所需的所有参数。host 是域名，是字符串格式的 IPv4/v6 地址或 None。port 是字符串格式的服务名称，如 'http' 、端口号（数字）或 None。传入 None 作为 host 和 port 的值，相当于将 NULL 传递给底层 C API。

可以指定 family、type 和 proto 参数，以缩小返回的地址列表。向这些参数分别传入 0 表示保留全部结果范围。flags 参数可以是 AI_* 常量中的一个或多个，它会影响结果的计算和返回。例如，AI_NUMERICHOST 会禁用域名解析，此时如果 host 是域名，则会抛出错误。

本函数返回的 5 元组列表具有以下结构：

(family, type, proto, canonname, sockaddr)

在这些元组中，family、type、proto 都是整数，可以用于传递给 socket() 函数。如果 flags 参数有一部分是 AI_CANONNAME，那么 canonname 将是表示 host 的规范名称的字符串。否则 canonname 将为空。sockaddr 是一个表示套接字地址的元组，具体格式取决于返回的 family （对于 AF_INET，是一个 (address, port) 二元组，对于 AF_INET6，是一个 (address, port, flow info, scope id) 四元组），可以用于传递给 socket.connect() 方法。

下面的示例获取了 TCP 连接地址信息，假设该连接通过 80 端口连接至 example.org （如果系统未启用 IPv6，则结果可能会不同）：

>>> socket.getaddrinfo("example.org", 80, proto=socket.IPPROTO_TCP)
[(<AddressFamily.AF_INET6: 10>, <SocketType.SOCK_STREAM: 1>,
 6, '', ('2606:2800:220:1:248:1893:25c8:1946', 80, 0, 0)),
 (<AddressFamily.AF_INET: 2>, <SocketType.SOCK_STREAM: 1>,
 6, '', ('93.184.216.34', 80))]

在 3.2 版更改: 现在可以使用关键字参数的形式来传递参数。

socket.getfqdn([name])

返回 name 的全限定域名 (Fully Qualified Domain Name – FQDN)。如果 name 省略或为空，则将其解释为本地主机。为了查找全限定名称，首先将检查由 gethostbyaddr() 返回的主机名，然后是主机的别名（如果存在）。选中第一个包含句点的名字。如果无法获取全限定域名，则返回由 gethostname() 返回的主机名。

socket.gethostbyname(hostname)

将主机名转换为 IPv4 地址格式。IPv4 地址以字符串格式返回，如 '100.50.200.5'。如果主机名本身是 IPv4 地址，则原样返回。更完整的接口请参考 gethostbyname_ex()。gethostbyname() 不支持 IPv6 域名解析，应使用 getaddrinfo() 来支持 IPv4/v6 双协议栈。

socket.gethostbyname_ex(hostname)

将主机名转换为 IPv4 地址格式的扩展接口。返回三元组 (hostname, aliaslist, ipaddrlist)，其中 hostname 是响应给定 ip_address 的主要主机名，aliaslist 是相同地址的其他可用主机名的列表（可能为空），而 ipaddrlist 是 IPv4 地址列表，包含相同主机名、相同接口的不同地址（通常是一个地址，但不总是如此）。gethostbyname_ex() 不支持 IPv6 名称解析，应使用 getaddrinfo() 来支持 IPv4/v6 双协议栈。

socket.gethostname()

返回一个字符串，包含当前正在运行 Python 解释器的机器的主机名。

注意： gethostname() 并不总是返回全限定域名，必要的话请使用 getfqdn()。

socket.gethostbyaddr(ip_address)

返回三元组 (hostname, aliaslist, ipaddrlist)，其中 hostname 是响应给定 ip_address 的主要主机名，aliaslist 是相同地址的其他可用主机名的列表（可能为空），而 ipaddrlist 是 IPv4/v6 地址列表，包含相同主机名、相同接口的不同地址（很可能仅包含一个地址）。要查询全限定域名，请使用函数 getfqdn()。gethostbyaddr() 支持 IPv4 和 IPv6。

socket.getnameinfo(sockaddr, flags)

将套接字地址 sockaddr 转换为二元组 (host, port)。host 的形式可能是全限定域名，或是由数字表示的地址，具体取决于 flags 的设置。同样，port 可以包含字符串格式的端口名称或数字格式的端口号。

socket.getprotobyname(protocolname)

将 Internet 协议名称（如 'icmp' ）转换为常量，该常量适用于 socket() 函数的第三个（可选）参数。通常只有在 “raw” 模式 (SOCK_RAW) 中打开的套接字才需要使用该常量。对于正常的套接字模式，协议省略或为零时，会自动选择正确的协议。

socket.getservbyname(servicename[, protocolname])

将 Internet 服务名称和协议名称转换为该服务的端口号。协议名称是可选的，如果提供的话应为 'tcp' 或 'udp'，否则将匹配出所有协议。

socket.getservbyport(port[, protocolname])

将 Internet 端口号和协议名称转换为该服务的服务名称。协议名称是可选的，如果提供的话应为 'tcp' 或 'udp'，否则将匹配出所有协议。

socket.ntohl(x)

将 32 位正整数从网络字节序转换为主机字节序。在主机字节序与网络字节序相同的计算机上，这是一个空操作。字节序不同将执行 4 字节交换操作。

socket.ntohs(x)

将 16 位正整数从网络字节序转换为主机字节序。在主机字节序与网络字节序相同的计算机上，这是一个空操作。字节序不同将执行 2 字节交换操作。

socket.htonl(x)

将 32 位正整数从主机字节序转换为网络字节序。在主机字节序与网络字节序相同的计算机上，这是一个空操作。字节序不同将执行 4 字节交换操作。

socket.htons(x)

将 16 位正整数从主机字节序转换为网络字节序。在主机字节序与网络字节序相同的计算机上，这是一个空操作。字节序不同将执行 2 字节交换操作。

socket.inet_aton(ip_string)

将 IPv4 地址从点分十进制字符串格式（如 ‘123.45.67.89’ ）转换为 32 位压缩二进制格式，转换后为字节对象，长度为四个字符。与那些使用标准 C 库，且需要 struct in_addr 类型的对象的程序交换信息时，此功能很有用。 该类型即此函数返回的 32 位压缩二进制的 C 类型。

inet_aton() 也接受句点数少于三的字符串，详情请参阅 Unix 手册 inet(3)。

如果传入本函数的 IPv4 地址字符串无效，则抛出 OSError。注意，具体什么样的地址有效取决于 inet_aton() 的底层 C 实现。

inet_aton() 不支持 IPv6，在 IPv4/v6 双协议栈下应使用 inet_pton() 来代替。

socket.inet_ntoa(packed_ip)

将 32 位压缩 IPv4 地址（一个 类字节对象，长 4 个字节）转换为标准的点分十进制字符串形式（如 ‘123.45.67.89’ ）。与那些使用标准 C 库，且需要 struct in_addr 类型的对象的程序交换信息时，本函数很有用。 该类型即本函数参数中的 32 位压缩二进制数据的 C 类型。

如果传入本函数的字节序列长度不是 4 个字节，则抛出 OSError。inet_ntoa() 不支持 IPv6，在 IPv4/v6 双协议栈下应使用 inet_ntop() 来代替。

在 3.5 版更改: 现在支持可写的 字节类对象。

socket.inet_pton(address_family, ip_string)

将特定地址簇的 IP 地址（字符串）转换为压缩二进制格式。当库或网络协议需要接受 struct in_addr 类型的对象（类似 inet_aton() ）或 struct in6_addr 类型的对象时，inet_pton() 很有用。

目前 address_family 支持 AF_INET 和 AF_INET6。如果 IP 地址字符串 ip_string 无效，则抛出 OSError。注意，具体什么地址有效取决于 address_family 的值和 inet_pton() 的底层实现。

Availability: Unix (maybe not all platforms), Windows.

在 3.4 版更改: 添加了 Windows 支持

socket.inet_ntop(address_family, packed_ip)

将压缩 IP 地址（一个 类字节对象，数个字节长）转换为标准的、特定地址簇的字符串形式（如 '7.10.0.5' 或 '5aef:2b::8' ）。当库或网络协议返回 struct in_addr 类型的对象（类似 inet_ntoa() ）或 struct in6_addr 类型的对象时，inet_ntop() 很有用。

目前 address_family 支持 AF_INET 和 AF_INET6。如果字节对象 packed_ip 与指定的地址簇长度不符，则抛出 ValueError。针对 inet_ntop() 调用的错误则抛出 OSError。

Availability: Unix (maybe not all platforms), Windows.

在 3.4 版更改: 添加了 Windows 支持

在 3.5 版更改: 现在支持可写的 字节类对象。

socket.CMSG_LEN(length)

返回给定 length 所关联数据的辅助数据项的总长度（不带尾部填充）。此值通常用作 recvmsg() 接收一个辅助数据项的缓冲区大小，但是 RFC 3542 [https://tools.ietf.org/html/rfc3542.html] 要求可移植应用程序使用 CMSG_SPACE()，以此将尾部填充的空间计入，即使该项在缓冲区的最后。如果 length 超出允许范围，则抛出 OverflowError。

Availability: most Unix platforms, possibly others.

3.3 新版功能.

socket.CMSG_SPACE(length)

返回 recvmsg() 所需的缓冲区大小，以接收给定 length 所关联数据的辅助数据项，带有尾部填充。接收多个项目所需的缓冲区空间是关联数据长度的 CMSG_SPACE() 值的总和。如果 length 超出允许范围，则抛出 OverflowError。

请注意，某些系统可能支持辅助数据，但不提供本函数。还需注意，如果使用本函数的结果来设置缓冲区大小，可能无法精确限制可接收的辅助数据量，因为可能会有其他数据写入尾部填充区域。

Availability: most Unix platforms, possibly others.

3.3 新版功能.

socket.getdefaulttimeout()

返回用于新套接字对象的默认超时（以秒为单位的浮点数）。值 None 表示新套接字对象没有超时。首次导入 socket 模块时，默认值为 None。

socket.setdefaulttimeout(timeout)

设置用于新套接字对象的默认超时（以秒为单位的浮点数）。首次导入 socket 模块时，默认值为 None。可能的取值及其各自的含义请参阅 settimeout()。

socket.sethostname(name)

将计算机的主机名设置为 name。如果权限不足将抛出 OSError。

Availability: Unix.

3.3 新版功能.

socket.if_nameindex()

返回一个列表，包含网络接口（网卡）信息二元组（整数索引，名称字符串）。系统调用失败则抛出 OSError。

Availability: Unix.

3.3 新版功能.

socket.if_nametoindex(if_name)

返回网络接口名称相对应的索引号。如果没有所给名称的接口，则抛出 OSError。

Availability: Unix.

3.3 新版功能.

socket.if_indextoname(if_index)

返回网络接口索引号相对应的接口名称。如果没有所给索引号的接口，则抛出 OSError。

Availability: Unix.

3.3 新版功能.

18.1.3. 套接字对象

套接字对象具有以下方法。除了 makefile()，其他都与套接字专用的 Unix 系统调用相对应。

在 3.2 版更改: 添加了对 上下文管理器 协议的支持。退出上下文管理器与调用 close() 等效。

socket.accept()

接受一个连接。此 socket 必须绑定到一个地址上并且监听连接。返回值是一个 (conn, address) 对，其中 conn 是一个 新 的套接字对象，用于在此连接上收发数据，address 是连接另一端的套接字所绑定的地址。

新创建的套接字是 不可继承的。

在 3.4 版更改: 该套接字现在是不可继承的。

在 3.5 版更改: 如果系统调用被中断，但信号处理程序没有触发异常，此方法现在会重试系统调用，而不是触发 InterruptedError 异常 (原因详见 PEP 475 [https://www.python.org/dev/peps/pep-0475])。

socket.bind(address)

将套接字绑定到 address。套接字必须尚未绑定。（ address 的格式取决于地址簇 —— 参见上文）

socket.close()

将套接字标记为关闭。当 makefile() 创建的所有文件对象都关闭时，底层系统资源（如文件描述符）也将关闭。一旦上述情况发生，将来对套接字对象的所有操作都会失败。对端将接收不到任何数据（清空队列数据后）。

垃圾回收时，套接字会自动关闭，但建议显式 close() 它们，或在它们周围使用 with 语句。

在 3.6 版更改: 现在，如果底层的 close() 调用出错，会抛出 OSError。

注解

close() 释放与连接相关联的资源，但不一定立即关闭连接。如果需要及时关闭连接，请在调用 close() 之前调用 shutdown()。

socket.connect(address)

连接到 address 处的远程套接字。（ address 的格式取决于地址簇 —— 参见上文）

如果连接被信号中断，则本方法将等待，直到连接完成。如果信号处理程序未抛出异常，且套接字阻塞中或已超时，则在超时后抛出 socket.timeout。对于非阻塞套接字，如果连接被信号中断，则本方法将抛出 InterruptedError 异常（或信号处理程序抛出的异常）。

在 3.5 版更改: 本方法现在将等待，直到连接完成，而不是在以下情况抛出 InterruptedError 异常。该情况为，连接被信号中断，信号处理程序未抛出异常，且套接字阻塞中或已超时（具体解释请参阅 PEP 475 [https://www.python.org/dev/peps/pep-0475] ）。

socket.connect_ex(address)

类似于 connect(address)，但是对于 C 级别的 connect() 调用返回的错误，本函数将返回错误指示器，而不是抛出异常（对于其他问题，如“找不到主机”，仍然可以抛出异常）。如果操作成功，则错误指示器为 0，否则为 errno 变量的值。这对支持如异步连接很有用。

socket.detach()

将套接字对象置于关闭状态，而底层的文件描述符实际并不关闭。返回该文件描述符，使其可以重新用于其他目的。

3.2 新版功能.

socket.dup()

创建套接字的副本。

新创建的套接字是 不可继承的。

在 3.4 版更改: 该套接字现在是不可继承的。

socket.fileno()

返回套接字的文件描述符（一个小整数），失败返回 -1。配合 select.select() 使用很有用。

在 Windows 下，此方法返回的小整数在允许使用文件描述符的地方无法使用（如 os.fdopen() ）。Unix 无此限制。

socket.get_inheritable()

获取套接字文件描述符或套接字句柄的 可继承标志 ：如果子进程可以继承套接字则为 True，否则为 False。

3.4 新版功能.

socket.getpeername()

返回套接字连接到的远程地址。举例而言，这可以用于查找远程 IPv4/v6 套接字的端口号。（返回的地址格式取决于地址簇 —— 参见上文。）部分系统不支持此函数。

socket.getsockname()

返回套接字本身的地址。举例而言，这可以用于查找 IPv4/v6 套接字的端口号。（返回的地址格式取决于地址簇 —— 参见上文。）

socket.getsockopt(level, optname[, buflen])

返回指定套接字选项的值（参阅 Unix 手册页 getsockopt(2) ）。所需的符号常量（ SO_* 等）已定义在本模块中。如果未指定 buflen，则认为该选项值为整数，由本函数返回该整数值。如果指定 buflen，则它定义了用于存放选项值的缓冲区的最大长度，且该缓冲区将作为字节对象返回。对缓冲区的解码工作由调用者自行完成（针对编码为字节串的 C 结构，其解码方法请参阅可选的内置模块 struct ）。

socket.gettimeout()

返回套接字操作相关的超时秒数（浮点数），未设置超时则返回 None。它反映最后一次调用 setblocking() 或 settimeout() 后的设置。

socket.ioctl(control, option)

平台

Windows

ioctl() 方法是 WSAIoctl 系统接口的有限接口。请参考 Win32 文档 [https://msdn.microsoft.com/en-us/library/ms741621%28VS.85%29.aspx] 以获取更多信息。

在其他平台上，可以使用通用的 fcntl.fcntl() 和 fcntl.ioctl() 函数，它们接受套接字对象作为第一个参数。

当前仅支持以下控制码： SIO_RCVALL、SIO_KEEPALIVE_VALS 和 SIO_LOOPBACK_FAST_PATH。

在 3.6 版更改: 添加了 SIO_LOOPBACK_FAST_PATH。

socket.listen([backlog])

启动一个服务器用于接受连接。如果指定 backlog，则它最低为 0（小于 0 会被置为 0），它指定系统允许暂未 accept 的连接数，超过后将拒绝新连接。未指定则自动设为合理的默认值。

在 3.5 版更改: backlog 参数现在是可选的。

socket.makefile(mode='r', buffering=None, *, encoding=None, errors=None, newline=None)

返回与套接字关联的 文件对象。返回的对象的具体类型取决于 makefile() 的参数。这些参数的解释方式与内置的 open() 函数相同，其中 mode 的值仅支持 'r' （默认），'w' 和 'b'。

套接字必须处于阻塞模式，它可以有超时，但是如果发生超时，文件对象的内部缓冲区可能会以不一致的状态结尾。

关闭 makefile() 返回的文件对象不会关闭原始套接字，除非所有其他文件对象都已关闭且在套接字对象上调用了 socket.close()。

注解

在 Windows 上，由 makefile() 创建的文件类对象无法作为带文件描述符的文件对象使用，如无法作为 subprocess.Popen() 的流参数。

socket.recv(bufsize[, flags])

从套接字接收数据。返回值是一个字节对象，表示接收到的数据。bufsize 指定一次接收的最大数据量。可选参数 flags 的含义请参阅 Unix 手册页 recv(2)，它默认为零。

注解

为了最佳匹配硬件和网络的实际情况，bufsize 的值应为 2 的相对较小的幂，如 4096。

在 3.5 版更改: 如果系统调用被中断，但信号处理程序没有触发异常，此方法现在会重试系统调用，而不是触发 InterruptedError 异常 (原因详见 PEP 475 [https://www.python.org/dev/peps/pep-0475])。

socket.recvfrom(bufsize[, flags])

从套接字接收数据。返回值是一对 (bytes, address)，其中 bytes 是字节对象，表示接收到的数据，address 是发送端套接字的地址。可选参数 flags 的含义请参阅 Unix 手册页 recv(2)，它默认为零。（ address 的格式取决于地址簇 —— 参见上文）

在 3.5 版更改: 如果系统调用被中断，但信号处理程序没有触发异常，此方法现在会重试系统调用，而不是触发 InterruptedError 异常 (原因详见 PEP 475 [https://www.python.org/dev/peps/pep-0475])。

socket.recvmsg(bufsize[, ancbufsize[, flags]])

从套接字接收普通数据（至多 bufsize 字节）和辅助数据。ancbufsize 参数设置用于接收辅助数据的内部缓冲区的大小（以字节为单位），默认为 0，表示不接收辅助数据。可以使用 CMSG_SPACE() 或 CMSG_LEN() 计算辅助数据缓冲区的合适大小，无法放入缓冲区的项目可能会被截断或丢弃。flags 参数默认为 0，其含义与 recv() 中的相同。

返回值是一个四元组： (data, ancdata, msg_flags, address)。data 项是一个 bytes 对象，用于保存接收到的非辅助数据。ancdata 项是零个或多个元组 (cmsg_level, cmsg_type, cmsg_data) 组成的列表，表示接收到的辅助数据（控制消息）：cmsg_level 和 cmsg_type 是分别表示协议级别和协议类型的整数，而 cmsg_data 是保存相关数据的 bytes 对象。msg_flags 项由各种标志按位或组成，表示接收消息的情况，详细信息请参阅系统文档。如果接收端套接字断开连接，则 address 是发送端套接字的地址（如果有），否则该值无指定。

某些系统上可以利用 AF_UNIX 套接字通过 sendmsg() 和 recvmsg() 在进程之间传递文件描述符。使用此功能时（通常仅限于 SOCK_STREAM 套接字），recvmsg() 将在其辅助数据中返回以下格式的项 (socket.SOL_SOCKET, socket.SCM_RIGHTS, fds)，其中 fds 是一个 bytes 对象，是新文件描述符表示为原生 C int 类型的二进制数组。如果 recvmsg() 在系统调用返回后抛出异常，它将首先关闭此机制接收到的所有文件描述符。

对于仅接收到一部分的辅助数据项，一些系统没有指示其截断长度。如果某个项目可能超出了缓冲区的末尾，recvmsg() 将发出 RuntimeWarning，并返回其在缓冲区内的部分，前提是该对象被截断于关联数据开始后。

在支持 SCM_RIGHTS 机制的系统上，下方的函数将最多接收 maxfds 个文件描述符，返回消息数据和包含描述符的列表（同时忽略意外情况，如接收到无关的控制消息）。另请参阅 sendmsg()。

import socket, array

def recv_fds(sock, msglen, maxfds):
    fds = array.array("i")   # Array of ints
    msg, ancdata, flags, addr = sock.recvmsg(msglen, socket.CMSG_LEN(maxfds * fds.itemsize))
    for cmsg_level, cmsg_type, cmsg_data in ancdata:
        if (cmsg_level == socket.SOL_SOCKET and cmsg_type == socket.SCM_RIGHTS):
            # Append data, ignoring any truncated integers at the end.
            fds.fromstring(cmsg_data[:len(cmsg_data) - (len(cmsg_data) % fds.itemsize)])
    return msg, list(fds)

Availability: most Unix platforms, possibly others.

3.3 新版功能.

在 3.5 版更改: 如果系统调用被中断，但信号处理程序没有触发异常，此方法现在会重试系统调用，而不是触发 InterruptedError 异常 (原因详见 PEP 475 [https://www.python.org/dev/peps/pep-0475])。

socket.recvmsg_into(buffers[, ancbufsize[, flags]])

从套接字接收普通数据和辅助数据，其行为与 recvmsg() 相同，但将非辅助数据分散到一系列缓冲区中，而不是返回新的字节对象。buffers 参数必须是可迭代对象，它迭代出可供写入的缓冲区（如 bytearray 对象），这些缓冲区将被连续的非辅助数据块填充，直到数据全部写完或缓冲区用完为止。在允许使用的缓冲区数量上，操作系统可能会有限制（ sysconf() 的 SC_IOV_MAX 值）。ancbufsize 和 flags 参数的含义与 recvmsg() 中的相同。

返回值为四元组： (nbytes, ancdata, msg_flags, address)，其中 nbytes 是写入缓冲区的非辅助数据的字节总数，而 ancdata、msg_flags 和 address 与 recvmsg() 中的相同。

示例:

>>> import socket
>>> s1, s2 = socket.socketpair()
>>> b1 = bytearray(b'----')
>>> b2 = bytearray(b'0123456789')
>>> b3 = bytearray(b'--------------')
>>> s1.send(b'Mary had a little lamb')
22
>>> s2.recvmsg_into([b1, memoryview(b2)[2:9], b3])
(22, [], 0, None)
>>> [b1, b2, b3]
[bytearray(b'Mary'), bytearray(b'01 had a 9'), bytearray(b'little lamb---')]

Availability: most Unix platforms, possibly others.

3.3 新版功能.

socket.recvfrom_into(buffer[, nbytes[, flags]])

从套接字接收数据，将其写入 buffer 而不是创建新的字节串。返回值是一对 (nbytes, address)，其中 nbytes 是收到的字节数，address 是发送端套接字的地址。可选参数 flags 的含义请参阅 Unix 手册页 recv(2)，它默认为零。（ address 的格式取决于地址簇 —— 参见上文）

socket.recv_into(buffer[, nbytes[, flags]])

从套接字接收至多 nbytes 个字节，将其写入缓冲区而不是创建新的字节串。如果 nbytes 未指定（或指定为 0），则接收至所给缓冲区的最大可用大小。返回接收到的字节数。可选参数 flags 的含义请参阅 Unix 手册页 recv(2)，它默认为零。

socket.send(bytes[, flags])

发送数据给套接字。本套接字必须已连接到远程套接字。可选参数 flags 的含义与上述 recv() 中的相同。本方法返回已发送的字节数。应用程序要负责检查所有数据是否已发送，如果仅传输了部分数据，程序需要自行尝试传输其余数据。有关该主题的更多信息，请参考 套接字编程指南。

在 3.5 版更改: 如果系统调用被中断，但信号处理程序没有触发异常，此方法现在会重试系统调用，而不是触发 InterruptedError 异常 (原因详见 PEP 475 [https://www.python.org/dev/peps/pep-0475])。

socket.sendall(bytes[, flags])

发送数据给套接字。本套接字必须已连接到远程套接字。可选参数 flags 的含义与上述 recv() 中的相同。与 send() 不同，本方法持续从 bytes 发送数据，直到所有数据都已发送或发生错误为止。成功后会返回 None。出错后会抛出一个异常，此时并没有办法确定成功发送了多少数据。

在 3.5 版更改: 每次成功发送数据后，套接字超时不再重置。现在，套接字超时是发送所有数据的最大总持续时间。

在 3.5 版更改: 如果系统调用被中断，但信号处理程序没有触发异常，此方法现在会重试系统调用，而不是触发 InterruptedError 异常 (原因详见 PEP 475 [https://www.python.org/dev/peps/pep-0475])。

socket.sendto(bytes, address)

socket.sendto(bytes, flags, address)

发送数据给套接字。本套接字不应连接到远程套接字，而应由 address 指定目标套接字。可选参数 flags 的含义与上述 recv() 中的相同。本方法返回已发送的字节数。（ address 的格式取决于地址簇 —— 参见上文。）

在 3.5 版更改: 如果系统调用被中断，但信号处理程序没有触发异常，此方法现在会重试系统调用，而不是触发 InterruptedError 异常 (原因详见 PEP 475 [https://www.python.org/dev/peps/pep-0475])。

socket.sendmsg(buffers[, ancdata[, flags[, address]]])

将普通数据和辅助数据发送给套接字，将从一系列缓冲区中收集非辅助数据，并将其拼接为一条消息。buffers 参数指定的非辅助数据应为可迭代的 字节类对象 （如 bytes 对象），在允许使用的缓冲区数量上，操作系统可能会有限制（ sysconf() 的 SC_IOV_MAX 值）。ancdata 参数指定的辅助数据（控制消息）应为可迭代对象，迭代出零个或多个 (cmsg_level, cmsg_type, cmsg_data) 元组，其中 cmsg_level 和 cmsg_type 是分别指定协议级别和协议类型的整数，而 cmsg_data 是保存相关数据的字节类对象。请注意，某些系统（特别是没有 CMSG_SPACE() 的系统）可能每次调用仅支持发送一条控制消息。flags 参数默认为 0，与 send() 中的含义相同。如果 address 指定为除 None 以外的值，它将作为消息的目标地址。返回值是已发送的非辅助数据的字节数。

在支持 SCM_RIGHTS 机制的系统上，下方的函数通过一个 AF_UNIX 套接字来发送文件描述符列表 fds。另请参阅 recvmsg()。

import socket, array

def send_fds(sock, msg, fds):
    return sock.sendmsg([msg], [(socket.SOL_SOCKET, socket.SCM_RIGHTS, array.array("i", fds))])

Availability: most Unix platforms, possibly others.

3.3 新版功能.

在 3.5 版更改: 如果系统调用被中断，但信号处理程序没有触发异常，此方法现在会重试系统调用，而不是触发 InterruptedError 异常 (原因详见 PEP 475 [https://www.python.org/dev/peps/pep-0475])。

socket.sendmsg_afalg([msg, ]*, op[, iv[, assoclen[, flags]]])

为 AF_ALG 套接字定制的 sendmsg() 版本。可为 AF_ALG 套接字设置模式、IV、AEAD 关联数据的长度和标志位。

Availability: Linux >= 2.6.38

3.6 新版功能.

socket.sendfile(file, offset=0, count=None)

使用高性能的 os.sendfile 发送文件，直到达到文件的 EOF 为止，返回已发送的字节总数。file 必须是一个以二进制模式打开的常规文件对象。如果 os.sendfile 不可用（如 Windows）或 file 不是常规文件，将使用 send() 代替。offset 指示从哪里开始读取文件。如果指定了 count，它确定了要发送的字节总数，而不会持续发送直到达到文件的 EOF。返回时或发生错误时，文件位置将更新，在这种情况下，file.tell() 可用于确定已发送的字节数。套接字必须为 SOCK_STREAM 类型。不支持非阻塞的套接字。

3.5 新版功能.

socket.set_inheritable(inheritable)

设置套接字文件描述符或套接字句柄的 可继承标志。

3.4 新版功能.

socket.setblocking(flag)

设置套接字为阻塞或非阻塞模式：如果 flag 为 false，则将套接字设置为非阻塞，否则设置为阻塞。

本方法是某些 settimeout() 调用的简写：

sock.setblocking(True) 相当于 sock.settimeout(None)

sock.setblocking(False) 相当于 sock.settimeout(0.0)

socket.settimeout(value)

为阻塞套接字的操作设置超时。value 参数可以是非负浮点数，表示秒，也可以是 None。如果赋为一个非零值，那么如果在操作完成前超过了超时时间 value，后续的套接字操作将抛出 timeout 异常。如果赋为 0，则套接字将处于非阻塞模式。如果指定为 None，则套接字将处于阻塞模式。

更多信息请查阅 关于套接字超时的说明。

socket.setsockopt(level, optname, value: int)

socket.setsockopt(level, optname, value: buffer)

socket.setsockopt(level, optname, None, optlen: int)

Set the value of the given socket option (see the Unix manual page setsockopt(2)). The needed symbolic constants are defined in the socket module (SO_* etc.). The value can be an integer, None or a bytes-like object representing a buffer. In the later case it is up to the caller to ensure that the bytestring contains the proper bits (see the optional built-in module struct for a way to encode C structures as bytestrings). When value is set to None, optlen argument is required. It’s equivalent to call setsockopt C function with optval=NULL and optlen=optlen.

在 3.5 版更改: 现在支持可写的 字节类对象。

在 3.6 版更改: 添加了 setsockopt(level, optname, None, optlen: int) 调用形式。

socket.shutdown(how)

关闭一半或全部的连接。如果 how 为 SHUT_RD，则后续不再允许接收。如果 how 为 SHUT_WR，则后续不再允许发送。如果 how 为 SHUT_RDWR，则后续的发送和接收都不允许。

socket.share(process_id)

复制套接字，并准备将其与目标进程共享。目标进程必须以 process_id 形式提供。然后可以利用某种形式的进程间通信，将返回的字节对象传递给目标进程，还可以使用 fromshare() 在新进程中重新创建套接字。一旦本方法调用完毕，就可以安全地将套接字关闭，因为操作系统已经为目标进程复制了该套接字。

Availability: Windows.

3.3 新版功能.

注意此处没有 read() 或 write() 方法，请使用不带 flags 参数的 recv() 和 send() 来替代。

套接字对象还具有以下（只读）属性，这些属性与传入 socket 构造函数的值相对应。

socket.family

套接字的协议簇

socket.type

套接字的类型

socket.proto

套接字的协议。

18.1.4. 关于套接字超时的说明

一个套接字对象可以处于以下三种模式之一：阻塞、非阻塞或超时。套接字默认以阻塞模式创建，但是可以调用 setdefaulttimeout() 来更改。

在 blocking mode （阻塞模式）中，操作将阻塞，直到操作完成或系统返回错误（如连接超时）。

在 non-blocking mode （非阻塞模式）中，如果操作无法立即完成，则操作将失败（不幸的是，不同系统返回的错误不同）：位于 select 中的函数可用于了解套接字何时以及是否可以读取或写入。

在 timeout mode （超时模式）下，如果无法在指定的超时内完成操作（抛出 timeout 异常），或如果系统返回错误，则操作将失败。

注解

在操作系统层面上，超时模式 下的套接字在内部都设置为非阻塞模式。同时，阻塞和超时模式在文件描述符和套接字对象之间共享，这些描述符和对象均应指向同一个网络端点。如果，比如你决定使用套接字的 fileno()，这一实现细节可能导致明显的结果。

18.1.4.1. 超时与 connect 方法

connect() 操作也受超时设置的约束，通常建议在调用 connect() 之前调用 settimeout()，或将超时参数直接传递给 create_connection()。但是，无论 Python 套接字超时设置如何，系统网络栈都有可能返回自带的连接超时错误。

18.1.4.2. 超时与 accept 方法

如果 getdefaulttimeout() 的值不是 None，则 accept() 方法返回的套接字将继承该超时值。若是 None，返回的套接字行为取决于侦听套接字的设置：

如果侦听套接字处于 阻塞模式 或 超时模式，则 accept() 返回的套接字处于 阻塞模式；

如果侦听套接字处于 非阻塞模式，那么 accept() 返回的套接字是阻塞还是非阻塞取决于操作系统。如果要确保跨平台时的正确行为，建议手动覆盖此设置。

18.1.5. 示例

以下是 4 个使用 TCP/IP 协议的最小示例程序：一台服务器，它将收到的所有数据原样返回（仅服务于一个客户端），还有一个使用该服务器的客户端。注意，服务器必须按序执行 socket(), bind(), listen(), accept() （可能需要重复执行 accept() 以服务多个客户端），而客户端仅需要按序执行 socket(), connect()。还须注意，服务器不在侦听套接字上发送 sendall()/recv()，而是在 accept() 返回的新套接字上发送。

前两个示例仅支持 IPv4。

# Echo server program
import socket

HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen(1)
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data: break
            conn.sendall(data)

# Echo client program
import socket

HOST = 'daring.cwi.nl'    # The remote host
PORT = 50007              # The same port as used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.sendall(b'Hello, world')
    data = s.recv(1024)
print('Received', repr(data))

下两个例子与上两个很像，但是同时支持 IPv4 和 IPv6。 服务端将监听第一个可用的地址族（它本应同时监听两个）。 在大多数支持 IPv6 的系统上，IPv6 将有优先权并且服务端可能不会接受 IPv4 流量。 客户端将尝试连接到作为名称解析结果被返回的所有地址，并将流量发送给连接成功的第一个地址。

# Echo server program
import socket
import sys

HOST = None               # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC,
                              socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
    af, socktype, proto, canonname, sa = res
    try:
        s = socket.socket(af, socktype, proto)
    except OSError as msg:
        s = None
        continue
    try:
        s.bind(sa)
        s.listen(1)
    except OSError as msg:
        s.close()
        s = None
        continue
    break
if s is None:
    print('could not open socket')
    sys.exit(1)
conn, addr = s.accept()
with conn:
    print('Connected by', addr)
    while True:
        data = conn.recv(1024)
        if not data: break
        conn.send(data)

# Echo client program
import socket
import sys

HOST = 'daring.cwi.nl'    # The remote host
PORT = 50007              # The same port as used by the server
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):
    af, socktype, proto, canonname, sa = res
    try:
        s = socket.socket(af, socktype, proto)
    except OSError as msg:
        s = None
        continue
    try:
        s.connect(sa)
    except OSError as msg:
        s.close()
        s = None
        continue
    break
if s is None:
    print('could not open socket')
    sys.exit(1)
with s:
    s.sendall(b'Hello, world')
    data = s.recv(1024)
print('Received', repr(data))

下面的例子演示了如何在 Windows 上使用原始套接字编写一个非常简单的网络嗅探器。 这个例子需要管理员权限来修改接口:

import socket

# the public network interface
HOST = socket.gethostbyname(socket.gethostname())

# create a raw socket and bind it to the public interface
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_IP)
s.bind((HOST, 0))

# Include IP headers
s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)

# receive all packages
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

# receive a package
print(s.recvfrom(65565))

# disabled promiscuous mode
s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)

The last example shows how to use the socket interface to communicate to a CAN network using the raw socket protocol. To use CAN with the broadcast manager protocol instead, open a socket with:

socket.socket(socket.AF_CAN, socket.SOCK_DGRAM, socket.CAN_BCM)

在绑定 (CAN_RAW) 或连接 (CAN_BCM) socket 之后，你将可以在 socket 对象上正常使用 socket.send() 以及 socket.recv() 操作（及同类操作）。

This example might require special privileges:

import socket
import struct

# CAN frame packing/unpacking (see 'struct can_frame' in <linux/can.h>)

can_frame_fmt = "=IB3x8s"
can_frame_size = struct.calcsize(can_frame_fmt)

def build_can_frame(can_id, data):
    can_dlc = len(data)
    data = data.ljust(8, b'\x00')
    return struct.pack(can_frame_fmt, can_id, can_dlc, data)

def dissect_can_frame(frame):
    can_id, can_dlc, data = struct.unpack(can_frame_fmt, frame)
    return (can_id, can_dlc, data[:can_dlc])

# create a raw socket and bind it to the 'vcan0' interface
s = socket.socket(socket.AF_CAN, socket.SOCK_RAW, socket.CAN_RAW)
s.bind(('vcan0',))

while True:
    cf, addr = s.recvfrom(can_frame_size)

    print('Received: can_id=%x, can_dlc=%x, data=%s' % dissect_can_frame(cf))

    try:
        s.send(cf)
    except OSError:
        print('Error sending CAN frame')

    try:
        s.send(build_can_frame(0x01, b'\x01\x02\x03'))
    except OSError:
        print('Error sending CAN frame')

如果多次运行示例，且两次运行间隔很短，可能引发此错误:

OSError: [Errno 98] Address already in use

这是因为前一次运行使套接字处于 TIME_WAIT 状态，无法立即重用。

要防止这种情况，需要设置一个 socket 标志 socket.SO_REUSEADDR:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))

SO_REUSEADDR 标志告诉内核将处于 TIME_WAIT 状态的本地套接字重新使用，而不必等到固有的超时到期。

参见

关于套接字编程（C 语言）的介绍，请参阅以下文章：

An Introductory 4.3BSD Interprocess Communication Tutorial，作者 Stuart Sechrest

An Advanced 4.3BSD Interprocess Communication Tutorial，作者 Samuel J. Leffler et al,

两篇文章都在 UNIX 开发者手册，补充文档 1（第 PS1:7 和 PS1:8 节）中。那些特定于平台的参考资料，它们包含与套接字有关的各种系统调用，也是套接字语义细节的宝贵信息来源。对于 Unix，请参考手册页。对于 Windows，请参阅 WinSock（或 Winsock 2）规范。如果需要支持 IPv6 的 API，读者可能希望参考 RFC 3493 [https://tools.ietf.org/html/rfc3493.html]，标题为 Basic Socket Interface Extensions for IPv6。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

18.2. ssl — 套接字对象的TLS/SSL封装

源代码: Lib/ssl.py [https://github.com/python/cpython/tree/3.6/Lib/ssl.py]

本模块保证了对传安全传输层协议（TLS）的访问 (SSL)加密，并且提供客户端和服务端层面的网络嵌套字层面的对等连接认证技术。本模块使用OpenSSL库。适用于所有现代Unix系统、Windows以及Mac OS X。只要Open SSL存在的系统，都有机会正常使用。

注解

某些行为可能具有平台依赖，因为调用是根据操作系统的嵌套字API。不同版本的Open SSL也会引起差异：例如Open SSL版本1.0.1 自带TLSv1.1 和 TLSv1.2

警告

在阅读 安全考量 前不要使用此模块。 这样做可能会导致虚假的安全感，因为ssl模块的默认设置不一定适合你的应用程序。

本文档记录”ssl”模块的对象和函数；更多关于TLS,SSL,和证书的信息，请参阅下方的“详情”选项

This module provides a class, ssl.SSLSocket, which is derived from the socket.socket type, and provides a socket-like wrapper that also encrypts and decrypts the data going over the socket with SSL. It supports additional methods such as getpeercert(), which retrieves the certificate of the other side of the connection, and cipher(),which retrieves the cipher being used for the secure connection.

对于更复杂的应用程序，ssl.SSLContext 类有助于管理设置项和证书，进而可以被使用 SSLContext.wrap_socket() 方法创建的 SSL 套接字继承。

在 3.5.3 版更改: 更新以支持和 OpenSSL 1.1.0 的链接

在 3.6 版更改: OpenSSL 0.9.8、1.0.0 和 1.0.1 已过时，将不再被支持。在 ssl 模块未来的版本中，最低需要 OpenSSL 1.0.2 或 1.1.0。

18.2.1. 方法、常量和异常处理

exception ssl.SSLError

引发此异常以提示来自下层 SSL 实现（目前由 OpenSSL 库提供）的错误。 它表示在下层网络连接之上叠加的高层级加密和验证层存在某种问题。 此错误是 OSError 的一个子类型。 SSLError 实例的错误和消息是由 OpenSSL 库提供的。

在 3.3 版更改: SSLError 曾经是 socket.error 的一个子类型。

library

一个字符串形式的助词符，用来指明发生错误的 OpenSSL 子模块，例如 SSL, PEM 或 X509。 可能的取值范围依赖于 OpenSSL 的版本。

3.3 新版功能.

reason

一个字符串形式的助记符，用来指明发生错误的原因，例如 CERTIFICATE_VERIFY_FAILED。 可能的取值范围依赖于 OpenSSL 的版本。

3.3 新版功能.

exception ssl.SSLZeroReturnError

SSLError 的一个子类，当尝试读取或写入且 SSL 连接已被完全关闭时会被引发。 请注意这并不意味着下层的传输（读取 TCP）已被关闭。

3.3 新版功能.

exception ssl.SSLWantReadError

SSLError 的一个子类，当尝试读取或写入但，并在请求被满足之前还需要在下层的 TCP 传输上接收更多数据时会被 非阻塞型 SSL 套接字 引发。

3.3 新版功能.

exception ssl.SSLWantWriteError

SSLError 的一个子类，当尝试读取或写入数据，但在请求被满足之前还需要在下层的 TCP 传输上发送更多数据时会被 非阻塞型 SSL 套接字 引发。

3.3 新版功能.

exception ssl.SSLSyscallError

SSLError 的子类，当尝试在 SSL 套接字上执行操作时遇到系统错误时会被引发。 不幸的是，没有简单的方式能检查原始 errno 编号。

3.3 新版功能.

exception ssl.SSLEOFError

SSLError 的子类，当 SSL 连接被突然终止时会被引发。 通常，当遇到此错误时你不应再尝试重用下层的传输。

3.3 新版功能.

exception ssl.CertificateError

Raised to signal an error with a certificate (such as mismatching hostname). Certificate errors detected by OpenSSL, though, raise an SSLError.

18.2.1.1. 套接字创建

The following function allows for standalone socket creation. Starting from Python 3.2, it can be more flexible to use SSLContext.wrap_socket() instead.

ssl.wrap_socket(sock, keyfile=None, certfile=None, server_side=False, cert_reqs=CERT_NONE, ssl_version={see docs}, ca_certs=None, do_handshake_on_connect=True, suppress_ragged_eofs=True, ciphers=None)

接受一个 socket.socket 的实例 sock，并返回一个 ssl.SSLSocket 的实例，该类型是 socket.socket 的子类型，它将下层的套接字包装在一个 SSL 上下文中。 sock 必须是一个 SOCK_STREAM 套接字；其他套接字类型不被支持。

For client-side sockets, the context construction is lazy; if the underlying socket isn’t connected yet, the context construction will be performed after connect() is called on the socket. For server-side sockets, if the socket has no remote peer, it is assumed to be a listening socket, and the server-side SSL wrapping is automatically performed on client connections accepted via the accept() method. wrap_socket() may raise SSLError.

The keyfile and certfile parameters specify optional files which contain a certificate to be used to identify the local side of the connection. See the discussion of 证书 for more information on how the certificate is stored in the certfile.

形参 server_side 是一个布尔值，它标明希望从该套接字获得服务器端行为还是客户端行为。

The parameter cert_reqs specifies whether a certificate is required from the other side of the connection, and whether it will be validated if provided. It must be one of the three values CERT_NONE (certificates ignored), CERT_OPTIONAL (not required, but validated if provided), or CERT_REQUIRED (required and validated). If the value of this parameter is not CERT_NONE, then the ca_certs parameter must point to a file of CA certificates.

The ca_certs file contains a set of concatenated “certification authority” certificates, which are used to validate certificates passed from the other end of the connection. See the discussion of 证书 for more information about how to arrange the certificates in this file.

The parameter ssl_version specifies which version of the SSL protocol to use. Typically, the server chooses a particular protocol version, and the client must adapt to the server’s choice. Most of the versions are not interoperable with the other versions. If not specified, the default is PROTOCOL_TLS; it provides the most compatibility with other versions.

这个表显示了客户端（横向）的哪个版本能够连接服务器（纵向）的哪个版本。

client / server

SSLv2

SSLv3

TLS 3

TLSv1

TLSv1.1

TLSv1.2

SSLv2

是

否

no 1

否

否

否

SSLv3

否

是

no 2

否

否

否

TLS (SSLv23) 3

no 1

no 2

是

是

是

是

TLSv1

否

否

是

是

否

否

TLSv1.1

否

否

是

否

是

否

TLSv1.2

否

否

是

否

否

是

备注

1(1,2)

SSLContext 默认设置 OP_NO_SSLv2 以禁用 SSLv2。

2(1,2)

SSLContext 默认设置 OP_NO_SSLv3 以禁用 SSLv3。

3(1,2)

TLS 1.3 协议在 OpenSSL >= 1.1.1 中设置 PROTOCOL_TLS 时可用。 没有专门针对 TLS 1.3 的 PROTOCOL 常量。

注解

Which connections succeed will vary depending on the version of OpenSSL. For example, before OpenSSL 1.0.0, an SSLv23 client would always attempt SSLv2 connections.

The ciphers parameter sets the available ciphers for this SSL object. It should be a string in the OpenSSL cipher list format [https://www.openssl.org/docs/manmaster/man1/ciphers.html].

形参 do_handshake_on_connect 指明是否要在调用 socket.connect() 之后自动执行 SSL 握手，还是要通过发起调用 SSLSocket.do_handshake() 方法让应用程序显式地调用它。 显式地调用 SSLSocket.do_handshake() 可给予程序对握手中所涉及的套接字 I/O 阻塞行为的控制。

形参 suppress_ragged_eofs 指明 SSLSocket.recv() 方法应当如何从连接的另一端发送非预期的 EOF 信号。 如果指定为 True (默认值)，它将返回正常的 EOF (空字节串对象) 来响应从下层套接字引发的非预期的 EOF 错误；如果指定为 False，它将向调用方引发异常。

在 3.2 版更改: New optional argument ciphers.

18.2.1.2. 上下文创建

便捷函数，可以帮助创建 SSLContext 对象，用于常见的目的。

ssl.create_default_context(purpose=Purpose.SERVER_AUTH, cafile=None, capath=None, cadata=None)

返回一个新的 SSLContext 对象，使用给定 purpose 的默认设置。 该设置由 ssl 模块选择，并且通常是代表一个比直接调用 SSLContext 构造器时更高的安全等级。

cafile, capath, cadata 代表用于进行证书核验的可选受信任 CA 证书，与 SSLContext.load_verify_locations() 的一致。 如果三个参数均为 None，此函数可以转而选择信任系统的默认 CA 证书。

设置包括: PROTOCOL_TLS, OP_NO_SSLv2 以及 OP_NO_SSLv3，具有不带 RC4 和不带无身份验证密码套件的高度加密密码套件。 传入 SERVER_AUTH 作为 purpose 会把 verify_mode 设为 CERT_REQUIRED 并且加载指定 CA 证书（当给出 cafile, capath 和 cadata 中的至少一个）或者使用 SSLContext.load_default_certs() 来加载默认 CA 证书。

注解

协议、选项、密码和其他设置可随时更改为更具约束性的值而无须事先弃用。 这些值代表了兼容性和安全性之间的合理平衡。

如果你的应用需要特定的设置，你应当创建一个 SSLContext 并自行应用设置。

注解

如果你发现当某些较旧的客户端或服务器尝试与用此函数创建的 SSLContext 进行连接时收到了报错提示 “Protocol or cipher suite mismatch”，这可能是因为它们只支持 SSL3.0 而它被此函数用 OP_NO_SSLv3 排除掉了。 SSL3.0 被广泛认为 完全不可用 [https://en.wikipedia.org/wiki/POODLE]。 如果你仍希望继续使用此函数但仍允许 SSL 3.0 连接，你可以使用以下代码重新启用它们:

ctx = ssl.create_default_context(Purpose.CLIENT_AUTH)
ctx.options &= ~ssl.OP_NO_SSLv3

3.4 新版功能.

在 3.4.4 版更改: RC4 被从默认密码字符串中丢弃。

在 3.6 版更改: ChaCha20/Poly1305 被添加到默认密码字符串中。

3DES 被从默认密码字符串中丢弃。

18.2.1.3. 随机生成

ssl.RAND_bytes(num)

返回 num 个高加密强度伪随机字节数据。 如果 PRNG 未使用足够的数据作为随机种子或者如果当前 RAND 方法不支持该操作则会引发 SSLError。 RAND_status() 可被用来检查 PRNG 的状态而 RAND_add() 可被用来为 PRNG 设置随机种子。

对于几乎所有应用程序都更推荐使用 os.urandom()。

Read the Wikipedia article, Cryptographically secure pseudorandom number generator (CSPRNG) [https://en.wikipedia.org/wiki/Cryptographically_secure_pseudorandom_number_generator], to get the requirements of a cryptographically generator.

3.3 新版功能.

ssl.RAND_pseudo_bytes(num)

返回 (bytes, is_cryptographic): bytes 是 num 个伪随机字节数据，如果所生成的字节数据为高加密强度则 is_cryptographic 为 True。 如果当前 RAND 方法不支持此操作则会引发 SSLError。

所生成的伪随机字节序列如果具有足够的长度则将会具有唯一性，并是并非不可预测。 它们可被用于非加密目的以及加密协议中的特定目的，但通常不可被用于密钥生成等目的。

对于几乎所有应用程序都更推荐使用 os.urandom()。

3.3 新版功能.

3.6 版后已移除: OpenSSL 已弃用了 ssl.RAND_pseudo_bytes()，请改用 ssl.RAND_bytes()。

ssl.RAND_status()

如果 SSL 伪随机数生成器已使用‘足够的’随机性作为种子则返回 True，否则返回 False。 你可以使用 ssl.RAND_egd() 和 ssl.RAND_add() 来增加伪随机数生成器的随机性。

ssl.RAND_egd(path)

如果你在某处运行了一个熵收集守护程序（EGD），且 path 是向其打开的套接字连接路径名，此函数将从该套接字读取 256 个字节的随机性数据，并将其添加到 SSL 伪随机数生成器以增加所生成密钥的安全性。 此操作通常只在没有更好随机性源的系统上才是必要的。

请查看 http://egd.sourceforge.net/ 或 http://prngd.sourceforge.net/ 来了解有关熵收集守护程序源的信息。

Availability: not available with LibreSSL and OpenSSL > 1.1.0

ssl.RAND_add(bytes, entropy)

将给定的 bytes 混合到 SSL 伪随机数生成器中。 形参 entropy (float 类型) 是数据所包含的熵的下界 (因此你可以总是使用 0.0)。 请查看 RFC 1750 [https://tools.ietf.org/html/rfc1750.html] 了解有关熵源的更多信息。

在 3.5 版更改: 现在支持可写的 字节类对象。

18.2.1.4. 证书处理

ssl.match_hostname(cert, hostname)

验证 cert (使用 SSLSocket.getpeercert() 所返回的已解码格式) 是否匹配给定的 hostname。 所应用的规则是在 RFC 2818 [https://tools.ietf.org/html/rfc2818.html], RFC 5280 [https://tools.ietf.org/html/rfc5280.html] 和 RFC 6125 [https://tools.ietf.org/html/rfc6125.html] 中描述的检查 HTTPS 服务器身份的规则。 除了 HTTPS，此函数还应当适用于各种基于 SSL 协议的服务器身份检查操作，例如 FTPS, IMAPS, POPS 等等。

失败时引发 CertificateError。 成功时此函数无返回值:

>>> cert = {'subject': ((('commonName', 'example.com'),),)}
>>> ssl.match_hostname(cert, "example.com")
>>> ssl.match_hostname(cert, "example.org")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/py3k/Lib/ssl.py", line 130, in match_hostname
ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'

3.2 新版功能.

在 3.3.3 版更改: 此函数现在遵循 RFC 6125 [https://tools.ietf.org/html/rfc6125.html], 6.4.3 小节，它不会匹配多个通配符 (例如 *.*.com 或 *a*.example.org) 也不匹配国际化域名 (IDN) 片段内部的通配符。 IDN A 标签例如 www*.xn--pthon-kva.org 仍然受支持，但 x*.python.org 不再能匹配 xn--tda.python.org。

在 3.5 版更改: 现在支持匹配存在于证书的 subjectAltName 字段中的 IP 地址。

ssl.cert_time_to_seconds(cert_time)

返回距离 Unix 纪元零时的秒数，给定的 cert_time 字符串代表来自证书的 “notBefore” 或 “notAfter” 日期值，采用 "%b %d %H:%M:%S %Y %Z" strptime 格式（C 区域）。

以下为示例代码:

>>> import ssl
>>> timestamp = ssl.cert_time_to_seconds("Jan  5 09:34:43 2018 GMT")
>>> timestamp
1515144883
>>> from datetime import datetime
>>> print(datetime.utcfromtimestamp(timestamp))
2018-01-05 09:34:43

“notBefore” 或 “notAfter” 日期值必须使用 GMT (RFC 5280 [https://tools.ietf.org/html/rfc5280.html])。

在 3.5 版更改: 将输入时间解读为 UTC 时间，基于输入字符串中指明的 ‘GMT’ 时区。 在之前使用的是本地时区。 返回一个整数（不带输入格式中秒的分数部分）

ssl.get_server_certificate(addr, ssl_version=PROTOCOL_TLS, ca_certs=None)

Given the address addr of an SSL-protected server, as a (hostname, port-number) pair, fetches the server’s certificate, and returns it as a PEM-encoded string. If ssl_version is specified, uses that version of the SSL protocol to attempt to connect to the server. If ca_certs is specified, it should be a file containing a list of root certificates, the same format as used for the same parameter in wrap_socket(). The call will attempt to validate the server certificate against that set of root certificates, and will fail if the validation attempt fails.

在 3.3 版更改: 此函数现在是 IPv6 兼容的。-compatible.

在 3.5 版更改: 默认的 ssl_version 从 PROTOCOL_SSLv3 改为 PROTOCOL_TLS 以保证与现代服务器的最大兼容性。

ssl.DER_cert_to_PEM_cert(DER_cert_bytes)

根据给定的 DER 编码字节块形式的证书，返回同一证书的 PEM 编码字符串版本。

ssl.PEM_cert_to_DER_cert(PEM_cert_string)

根据给定的 ASCII PEM 字符串形式的证书，返回同一证书的 DER 编码字节序列。

ssl.get_default_verify_paths()

返回包含 OpenSSL 的默认 cafile 和 capath 的路径的命名元组。 此路径与 SSLContext.set_default_verify_paths() 所使用的相同。 返回值是一个 named tuple DefaultVerifyPaths:

cafile - 解析出的 cafile 路径或者如果文件不存在则为 None,

capath - 解析出的 capath 路径或者如果目录不存在则为 None,

openssl_cafile_env - 指向一个 cafile 的 OpenSSL 环境键,

openssl_cafile - 一个 cafile 的硬编码路径,

openssl_capath_env - 指向一个 capath 的 OpenSSL 环境键,

openssl_capath - 一个 capath 目录的硬编码路径

Availability: LibreSSL ignores the environment vars openssl_cafile_env and openssl_capath_env

3.4 新版功能.

ssl.enum_certificates(store_name)

从 Windows 的系统证书库中检索证书。 store_name 可以是 CA, ROOT 或 MY 中的一个。 Windows 也可能会提供额外的证书库。

此函数返回一个包含 (cert_bytes, encoding_type, trust) 元组的列表。 encoding_type 指明 cert_bytes 的编码格式。 它可以为 x509_asn 以表示 X.509 ASN.1 数据或是 pkcs_7_asn 以表示 PKCS#7 ASN.1 数据。 trust 以 OIDS 集合的形式指明证书的目的，或者如果证书对于所有目的都可以信任则为 True。

示例:

>>> ssl.enum_certificates("CA")
[(b'data...', 'x509_asn', {'1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2'}),
 (b'data...', 'x509_asn', True)]

Availability: Windows.

3.4 新版功能.

ssl.enum_crls(store_name)

Windows 的系统证书库中检索 CRL。 store_name 可以是 CA, ROOT 或 MY 中的一个。 Windows 也可能会提供额外的证书库。

此函数返回一个包含 (cert_bytes, encoding_type, trust) 元组的列表。 encoding_type 指明 cert_bytes 的编码格式。 它可以为 x509_asn 以表示 X.509 ASN.1 数据或是 pkcs_7_asn 以表示 PKCS#7 ASN.1 数据。

Availability: Windows.

3.4 新版功能.

18.2.1.5. 常量

所有常量现在都是 enum.IntEnum 或 enum.IntFlag 多项集的成员。

3.6 新版功能.

ssl.CERT_NONE

SSLContext.verify_mode 或 wrap_socket() 的 cert_reqs 形参可能的取值。 PROTOCOL_TLS_CLIENT 除外，这是默认的模式。 对于客户端套接字，几乎任何证书都是可接受的。 验证错误例如不受信任或过期的证书错误会被忽略并且不会中止 TLS/SSL 握手。

在服务器模式下，不会从客户端请求任何证书，因此客户端不会发送任何用于客户端证书身份验证的证书。

参见下文对于 安全考量 的讨论。

ssl.CERT_OPTIONAL

SSLContext.verify_mode 或 wrap_socket() 的 cert_reqs 形参可能的取值。 CERT_OPTIONAL 具有与 CERT_REQUIRED 相同的含义。 对于客户端套接字推荐改用 CERT_REQUIRED。

在服务器模式下，客户端证书请求会被发送给客户端。 客户端可以忽略请求也可以发送一个证书以执行 TLS 客户端证书身份验证。 如果客户端选择发送证书，则将对其执行验证。 任何验证错误都将立即中止 TLS 握手。

使用此设置要求将一组有效的 CA 证书传递给 SSLContext.load_verify_locations() 或是作为 wrap_socket() 的 ca_certs 形参值。

ssl.CERT_REQUIRED

SSLContext.verify_mode 或 wrap_socket() 的 cert_reqs 形参可能的取值。 在此模式下，需要从套接字连接的另一端获取证书；如果未提供证书或验证失败则将引发 SSLError。 此模式 不能 在客户端模式下对证书进行验证，因为它不会匹配主机名。 check_hostname 也必须被启用以验证证书的真实性。 PROTOCOL_TLS_CLIENT 会使用 CERT_REQUIRED 并默认启用 check_hostname。

对于服务器套接字，此模式会提供强制性的 TLS 客户端证书验证。 客户端证书请求会被发送给客户端并且客户端必须提供有效且受信任的证书。

使用此设置要求将一组有效的 CA 证书传递给 SSLContext.load_verify_locations() 或是作为 wrap_socket() 的 ca_certs 形参值。

class ssl.VerifyMode

CERT_* 常量的 enum.IntEnum 多项集。

3.6 新版功能.

ssl.VERIFY_DEFAULT

SSLContext.verify_flags 可能的取值。 在此模式下，证书吊销列表（CRL）并不会被检查。 OpenSSL 默认不要求也不验证 CRL。

3.4 新版功能.

ssl.VERIFY_CRL_CHECK_LEAF

Possible value for SSLContext.verify_flags. In this mode, only the peer cert is check but non of the intermediate CA certificates. The mode requires a valid CRL that is signed by the peer cert’s issuer (its direct ancestor CA). If no proper has been loaded SSLContext.load_verify_locations, validation will fail.

3.4 新版功能.

ssl.VERIFY_CRL_CHECK_CHAIN

SSLContext.verify_flags 可能的取值。 在此模式下，会检查对等证书链中所有证书的 CRL。

3.4 新版功能.

ssl.VERIFY_X509_STRICT

SSLContext.verify_flags 可能的取值，用于禁用已损坏 X.509 证书的绕过操作。

3.4 新版功能.

ssl.VERIFY_X509_TRUSTED_FIRST

SSLContext.verify_flags 可能的取值。 它指示 OpenSSL 在构建用于验证某个证书的信任链时首选受信任的证书。 此旗标将默认被启用。

3.4.4 新版功能.

class ssl.VerifyFlags

VERIFY_* 常量的 enum.IntFlag 多项集。

3.6 新版功能.

ssl.PROTOCOL_TLS

选择客户端和服务器均支持的最高协议版本。 此选项名称并不准确，实际上 “SSL” 和 “TLS” 协议均可被选择。

3.6 新版功能.

ssl.PROTOCOL_TLS_CLIENT

像 PROTOCOL_TLS 一样地自动协商最高协议版本，但是只支持客户端 SSLSocket 连接。 此协议默认会启用 CERT_REQUIRED 和 check_hostname。

3.6 新版功能.

ssl.PROTOCOL_TLS_SERVER

像 PROTOCOL_TLS 一样地自动协商最高协议版本，但是只支持服务器 SSLSocket 连接。

3.6 新版功能.

ssl.PROTOCOL_SSLv23

Alias for data:PROTOCOL_TLS.

3.6 版后已移除: 请改用 PROTOCOL_TLS。

ssl.PROTOCOL_SSLv2

选择 SSL 版本 2 作为通道加密协议。

如果 OpenSSL 编译时附带了 OPENSSL_NO_SSL2 旗标则此协议将不可用。

警告

SSL 版本 2 并不安全。 极不建议使用它。

3.6 版后已移除: OpenSSL 已经移除了对 SSLv2 的支持。

ssl.PROTOCOL_SSLv3

选择 SSL 版本 3 作为通道加密协议。

如果 OpenSSL 编译时使用了 OPENSSL_NO_SSLv3 旗标则此协议将不可用。

警告

SSL 版本 3 并不安全。 极不建议使用它。

3.6 版后已移除: OpenSSL 已经弃用了所有带有特定版本号的协议。 请改用默认协议 PROTOCOL_TLS 并附带 OP_NO_SSLv3 等旗标。

ssl.PROTOCOL_TLSv1

选择 TLS 版本 1.0 作为通道加密协议。

3.6 版后已移除: OpenSSL 已经弃用了所有带有特定版本号的协议。 请改用默认协议 PROTOCOL_TLS 并附带 OP_NO_SSLv3 等旗标。

ssl.PROTOCOL_TLSv1_1

选择 TLS 版本 1.1 作为通道加密协议。 仅适用于 openssl 版本 1.0.1+。

3.4 新版功能.

3.6 版后已移除: OpenSSL 已经弃用了所有带有特定版本号的协议。 请改用默认协议 PROTOCOL_TLS 并附带 OP_NO_SSLv3 等旗标。

ssl.PROTOCOL_TLSv1_2

选译 TLS 版本 1.2 作为通道加密协议。 这是最新的版本，也应是能提供最大保护的最佳选择，如果通信双方都支持它的话。 仅适用于 openssl 版本 1.0.1+。

3.4 新版功能.

3.6 版后已移除: OpenSSL 已经弃用了所有带有特定版本号的协议。 请改用默认协议 PROTOCOL_TLS 并附带 OP_NO_SSLv3 等旗标。

ssl.OP_ALL

对存在于其他 SSL 实现中的各种缺陷启用绕过操作。 默认会设置此选项。 没有必要设置与 OpenSSL 的 SSL_OP_ALL 常量同名的旗标。

3.2 新版功能.

ssl.OP_NO_SSLv2

阻止 SSLv2 连接。 此选项仅可与 PROTOCOL_TLS 结合使用。 它会阻止对等方选择 SSLv2 作为协议版本。

3.2 新版功能.

3.6 版后已移除: SSLv2 已被弃用。

ssl.OP_NO_SSLv3

阻止 SSLv3 连接。 此选项仅可与 PROTOCOL_TLS 结合使用。 它会阻止对等方选择 SSLv3 作为协议版本。

3.2 新版功能.

3.6 版后已移除: SSLv3 已被弃用

ssl.OP_NO_TLSv1

阻止 TLSv1 连接。 此选项仅可与 PROTOCOL_TLS 结合使用。 它会阻止对等方选择 TLSv1 作为协议版本。

3.2 新版功能.

ssl.OP_NO_TLSv1_1

阻止 TLSv1.1 连接。 此选项仅可与 PROTOCOL_TLS 结合使用。 它会阻止对等方选择 TLSv1.1 作为协议版本。 仅适用于 openssl 版本 1.0.1+。

3.4 新版功能.

ssl.OP_NO_TLSv1_2

阻止 TLSv1.2 连接。 此选项仅可与 PROTOCOL_TLS 结合使用。 它会阻止对等方选择 TLSv1.2 作为协议版本。 仅适用于 openssl 版本 1.0.1+。

3.4 新版功能.

ssl.OP_NO_TLSv1_3

阻止 TLSv1.3 连接。 此选项仅可与 PROTOCOL_TLS 结合使用。 它会阻止对等方选择 TLSv1.3 作为协议版本。 TLS 1.3 适用于 OpenSSL 1.1.1 或更新的版本。 当 Python 编译是基于较旧版本的 OpenSSL 时，该旗标默认为 0。

3.6.3 新版功能.

ssl.OP_CIPHER_SERVER_PREFERENCE

使用服务器的密码顺序首选项，而不是客户端的首选项。 此选项在客户端套接字和 SSLv2 服务器套接字上无效。

3.3 新版功能.

ssl.OP_SINGLE_DH_USE

阻止对于单独的 SSL 会话重用相同的 DH 密钥。 这会提升前向保密性但需要更多的计算资源。 此选项仅适用于服务器套接字。

3.3 新版功能.

ssl.OP_SINGLE_ECDH_USE

阻止对于单独的 SSL 会话重用相同的 ECDH 密钥。 这会提升前向保密性但需要更多的计算资源。 此选项仅适用于服务器套接字。

3.3 新版功能.

ssl.OP_ENABLE_MIDDLEBOX_COMPAT

在 TLS 1.3 握手中发送虚拟更改密码规格（CCS）消息以使得 TLS 1.3 连接看起来更像是 TLS 1.2 连接。

此选项仅适用于 OpenSSL 1.1.1 及更新的版本。

3.6.7 新版功能.

ssl.OP_NO_COMPRESSION

在 SSL 通道上禁用压缩。 这适用于应用协议支持自己的压缩方案的情况。

此选项仅适用于 OpenSSL 1.0.0 及更新的版本。

3.3 新版功能.

class ssl.Options

OP_* 常量的 enum.IntFlag 多项集。

ssl.OP_NO_TICKET

阻止客户端请求会话凭据。

3.6 新版功能.

ssl.HAS_ALPN

OpenSSL 库是否具有对 RFC 7301 [https://tools.ietf.org/html/rfc7301.html] 中描述的 应用层协议协商 TLS 扩展的内置支持。

3.5 新版功能.

ssl.HAS_ECDH

Whether the OpenSSL library has built-in support for Elliptic Curve-based Diffie-Hellman key exchange. This should be true unless the feature was explicitly disabled by the distributor.

3.3 新版功能.

ssl.HAS_SNI

OpenSSL 库是否具有对 服务器名称提示 扩展（在 RFC 6066 [https://tools.ietf.org/html/rfc6066.html] 中定义）的内置支持。

3.2 新版功能.

ssl.HAS_NPN

Whether the OpenSSL library has built-in support for Next Protocol Negotiation as described in the NPN draft specification [https://tools.ietf.org/html/draft-agl-tls-nextprotoneg]. When true, you can use the SSLContext.set_npn_protocols() method to advertise which protocols you want to support.

3.3 新版功能.

ssl.HAS_TLSv1_3

OpenSSL 库是否具有对 TLS 1.3 协议的内置支持。

3.6.3 新版功能.

ssl.CHANNEL_BINDING_TYPES

受支持的 TLS 通道绑定类型组成的列表。 此列表中的字符串可被用作传给 SSLSocket.get_channel_binding() 的参数。

3.3 新版功能.

ssl.OPENSSL_VERSION

解释器所加载的 OpenSSL 库的版本字符串:

>>> ssl.OPENSSL_VERSION
'OpenSSL 1.0.2k  26 Jan 2017'

3.2 新版功能.

ssl.OPENSSL_VERSION_INFO

代表 OpenSSL 库的版本信息的五个整数所组成的元组:

>>> ssl.OPENSSL_VERSION_INFO
(1, 0, 2, 11, 15)

3.2 新版功能.

ssl.OPENSSL_VERSION_NUMBER

OpenSSL 库的原始版本号，以单个整数表示:

>>> ssl.OPENSSL_VERSION_NUMBER
268443839
>>> hex(ssl.OPENSSL_VERSION_NUMBER)
'0x100020bf'

3.2 新版功能.

ssl.ALERT_DESCRIPTION_HANDSHAKE_FAILURE

ssl.ALERT_DESCRIPTION_INTERNAL_ERROR

ALERT_DESCRIPTION_*

来自 RFC 5246 [https://tools.ietf.org/html/rfc5246.html] 等文档的警报描述。 IANA TLS Alert Registry [https://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6] 中包含了这个列表及对定义其含义的 RFC 引用。

被用作 SSLContext.set_servername_callback() 中的回调函数的返回值。

3.4 新版功能.

class ssl.AlertDescription

ALERT_DESCRIPTION_* 常量的 enum.IntEnum 多项集。

3.6 新版功能.

Purpose.SERVER_AUTH

create_default_context() 和 SSLContext.load_default_certs() 的选项值。 这个值表明此上下文可以被用来验证 Web 服务器（因此，它将被用来创建客户端套接字）。

3.4 新版功能.

Purpose.CLIENT_AUTH

create_default_context() 和 SSLContext.load_default_certs() 的选项值。 这个值表明此上下文可以被用来验证 Web 客户端（因此，它将被用来创建服务器端套接字）。

3.4 新版功能.

class ssl.SSLErrorNumber

SSL_ERROR_* 常量的 enum.IntEnum 多项集。

3.6 新版功能.

18.2.2. SSL 套接字

class ssl.SSLSocket(socket.socket)

SSL 套接字提供了 套接字对象 的下列方法:

accept()

bind()

close()

connect()

detach()

fileno()

getpeername(), getsockname()

getsockopt(), setsockopt()

gettimeout(), settimeout(), setblocking()

listen()

makefile()

recv(), recv_into() (but passing a non-zero flags argument is not allowed)

send(), sendall() (with the same limitation)

sendfile() (but os.sendfile will be used for plain-text sockets only, else send() will be used)

shutdown()

但是，由于 SSL（和 TLS）协议在 TCP 之上具有自己的框架，因此 SSL 套接字抽象在某些方面可能与常规的 OS 层级套接字存在差异。 特别是要查看 非阻塞型套接字说明。

Usually, SSLSocket are not created directly, but using the SSLContext.wrap_socket() method.

在 3.5 版更改: 新增了 sendfile() 方法。

在 3.5 版更改: shutdown() 不会在每次接收或发送字节数据后重置套接字超时。 现在套接字超时为关闭的最大总持续时间。

3.6 版后已移除: 直接创建 SSLSocket 实例的做法已被弃用，请使用 SSLContext.wrap_socket() 来包装套接字。

SSL 套接字还具有下列方法和属性:

SSLSocket.read(len=1024, buffer=None)

从 SSL 套接字读取至多 len 个字节的数据并将结果作为 bytes 实例返回。 如果指定了 buffer，则改为读取到缓冲区，并返回所读取的字节数。

如果套接字为 非阻塞型 则会引发 SSLWantReadError 或 SSLWantWriteError 且读取将阻塞。

由于在任何时候重新协商都是可能的，因此调用 read() 也可能导致写入操作。

在 3.5 版更改: 套接字超时在每次接收或发送字节数据后不会再被重置。 现在套接字超时为读取至多 len 个字节数据的最大总持续时间。

3.6 版后已移除: 请使用 recv() 来代替 read()。

SSLSocket.write(buf)

将 buf 写入到 SSL 套接字并返回所写入的字节数。 buf 参数必须为支持缓冲区接口的对象。

如果套接字为 非阻塞型 则会引发 SSLWantReadError 或 SSLWantWriteError 且读取将阻塞。

由于在任何时候重新协商都是可能的，因此调用 write() 也可能导致读取操作。

在 3.5 版更改: 套接字超时在每次接收或发送字节数据后不会再被重置。 现在套接字超时为写入 buf 的最大总持续时间。

3.6 版后已移除: 请使用 send() 来代替 write()。

注解

read() 和 write() 方法是读写未加密的应用级数据，并将其解密/加密为带加密的线路级数据的低层级方法。 这些方法需要有激活的 SSL 连接，即握手已完成而 SSLSocket.unwrap() 尚未被调用。

通常你应当使用套接字 API 方法例如 recv() 和 send() 来代替这些方法。

SSLSocket.do_handshake()

执行 SSL 设置握手。

在 3.4 版更改: 当套接字的 context 的 check_hostname 属性为真值时此握手方法还会执行 match_hostname()。

在 3.5 版更改: 套接字超时在每次接收或发送字节数据时不会再被重置。 现在套接字超时为握手的最大总持续时间。

SSLSocket.getpeercert(binary_form=False)

如果连接另一端的对等方没有证书，则返回 None。 如果 SSL 握手还未完成，则会引发 ValueError。

如果 binary_form 形参为 False，并且从对等方接收到了证书，此方法将返回一个 dict 实例。 如果证书未通过验证，则字典将为空。 如果证书通过验证，它将返回由多个密钥组成的字典，其中包括 subject (证书颁发给的主体) 和 issuer (颁发证书的主体)。 如果证书包含一个 Subject Alternative Name 扩展的实例 (see RFC 3280 [https://tools.ietf.org/html/rfc3280.html])，则字典中还将有一个 subjectAltName 键。

subject 和 issuer 字段都是包含在证书中相应字段的数据结构中给出的相对专有名称（RDN）序列的元组，每个 RDN 均为 name-value 对的序列。 这里是一个实际的示例:

{'issuer': ((('countryName', 'IL'),),
            (('organizationName', 'StartCom Ltd.'),),
            (('organizationalUnitName',
              'Secure Digital Certificate Signing'),),
            (('commonName',
              'StartCom Class 2 Primary Intermediate Server CA'),)),
 'notAfter': 'Nov 22 08:15:19 2013 GMT',
 'notBefore': 'Nov 21 03:09:52 2011 GMT',
 'serialNumber': '95F0',
 'subject': ((('description', '571208-SLe257oHY9fVQ07Z'),),
             (('countryName', 'US'),),
             (('stateOrProvinceName', 'California'),),
             (('localityName', 'San Francisco'),),
             (('organizationName', 'Electronic Frontier Foundation, Inc.'),),
             (('commonName', '*.eff.org'),),
             (('emailAddress', 'hostmaster@eff.org'),)),
 'subjectAltName': (('DNS', '*.eff.org'), ('DNS', 'eff.org')),
 'version': 3}

注解

要验证特定服务的证书，你可以使用 match_hostname() 函数。

如果 binary_form 形参为 True，并且提供了证书，此方法会将整个证书的 DER 编码形式作为字节序列返回，或者如果对等方未提供证书则返回 None。 对等方是否提供证书取决于 SSL 套接字的角色:

对于客户端 SSL 套接字，服务器将总是提供证书，无论是否需要进行验证;

对于服务器 SSL 套接字，客户端将仅在服务器要求时才提供证书；因此如果你使用了 CERT_NONE (而不是 CERT_OPTIONAL 或 CERT_REQUIRED) 则 getpeercert() 将返回 None。

在 3.2 版更改: 返回的字典包括额外的条目例如 issuer 和 notBefore。

在 3.4 版更改: 如果握手未完成则会引发 ValueError。 返回的字典包括额外的 X509v3 扩展条目例如 crlDistributionPoints, caIssuers 和 OCSP URI。

SSLSocket.cipher()

返回由三个值组成的元组，其中包含所使用的密码名称，定义其使用方式的 SSL 协议，以及所使用的加密比特位数。 如果尚未建立连接，则返回 None。

SSLSocket.shared_ciphers()

返回在握手期间由客户端共享的密码列表。 所返回列表的每个条目都是由三个值组成的元组，其中包括密码名称，定义其使用方式的 SSL 协议版本，以及密码所使用的加密比特位数。 如果尚未建立连接或套接字为客户端套接字则 shared_ciphers() 将返回 None。

3.5 新版功能.

SSLSocket.compression()

以字符串形式返回所使用的压缩算法，或者如果连接没有使用压缩则返回 None。

如果高层级的协议支持自己的压缩机制，你可以使用 OP_NO_COMPRESSION 来禁用 SSL 层级的压缩。

3.3 新版功能.

SSLSocket.get_channel_binding(cb_type="tls-unique")

为当前连接获取字节串形式的通道绑定数据。 如果尚未连接或握手尚未完成则返回 None。

cb_type 形参允许选择需要的通道绑定类型。 有效的通道绑定类型在 CHANNEL_BINDING_TYPES 列表中列出。 目前只支持由 RFC 5929 [https://tools.ietf.org/html/rfc5929.html] 所定义的 ‘tls-unique’ 通道绑定。 如果请求了一个不受支持的通道绑定类型则将引发 ValueError。

3.3 新版功能.

SSLSocket.selected_alpn_protocol()

返回在 TLS 握手期间所选择的协议。 如果 SSLContext.set_alpn_protocols() 未被调用，如果另一方不支持 ALPN，如果此套接字不支持任何客户端所用的协议，或者如果握手尚未发生，则将返回 None。

3.5 新版功能.

SSLSocket.selected_npn_protocol()

返回在Return the higher-level protocol that was selected during the TLS/SSL 握手期间所选择的高层级协议。 如果 SSLContext.set_npn_protocols() 未被调用，或者如果另一方不支持 NPN，或者如果握手尚未发生，则将返回 None。

3.3 新版功能.

SSLSocket.unwrap()

执行 SSL 关闭握手，这会从下层的套接字中移除 TLS 层，并返回下层的套接字对象。 这可被用来通过一个连接将加密操作转为非加密。 返回的套接字应当总是被用于同连接另一方的进一步通信，而不是原始的套接字。

SSLSocket.verify_client_post_handshake()

向一个 TLS 1.3 客户端请求握手后身份验证（PHA）。 只有在初始 TLS 握手之后且双方都启用了 PHA 的情况下才能为服务器端套接字的 TLS 1.3 连接启用 PHA，参见 SSLContext.post_handshake_auth。

此方法不会立即执行证书交换。 服务器端会在下一次写入事件期间发送 CertificateRequest 并期待客户端在下一次读取事件期间附带证书进行响应。

如果有任何前置条件未被满足（例如非 TLS 1.3，PHA 未启用），则会引发 SSLError。

3.6.7 新版功能.

注解

仅在 OpenSSL 1.1.1 且 TLS 1.3 被启用时可用。 没有 TLS 1.3 支持，此方法将引发 NotImplementedError。

SSLSocket.version()

以字符串形式返回由连接协商确定的实际 SSL 协议版本，或者如果未建立安全连接则返回 None。 在撰写本文档时，可能的返回值包括 "SSLv2", "SSLv3", "TLSv1", "TLSv1.1" 和 "TLSv1.2"。 最新的 OpenSSL 版本可能会定义更多的返回值。

3.5 新版功能.

SSLSocket.pending()

返回在连接上等待被读取的已解密字节数。

SSLSocket.context

The SSLContext object this SSL socket is tied to. If the SSL socket was created using the top-level wrap_socket() function (rather than SSLContext.wrap_socket()), this is a custom context object created for this SSL socket.

3.2 新版功能.

SSLSocket.server_side

一个布尔值，对于服务器端套接字为 True 而对于客户端套接字则为 False。

3.2 新版功能.

SSLSocket.server_hostname

服务器的主机名: str 类型，对于服务器端套接字或者如果构造器中未指定主机名则为 None。

3.2 新版功能.

SSLSocket.session

用于 SSL 连接的 SSLSession。 该会话将在执行 TLS 握手后对客户端和服务器端套接字可用。 对于客户端套接字该会话可以在调用 do_handshake() 之前被设置以重用一个会话。

3.6 新版功能.

SSLSocket.session_reused

3.6 新版功能.

18.2.3. SSL 上下文

3.2 新版功能.

SSL 上下文可保存各种比单独 SSL 连接寿命更长的数据，例如 SSL 配置选项，证书和私钥等。 它还可为服务器端套接字管理缓存，以加快来自相同客户端的重复连接。

class ssl.SSLContext(protocol=PROTOCOL_TLS)

Create a new SSL context. You may pass protocol which must be one of the PROTOCOL_* constants defined in this module. PROTOCOL_TLS is currently recommended for maximum interoperability and default value.

参见

create_default_context() 让 ssl 为特定目标选择安全设置。

在 3.6 版更改: 上下文会使用安全默认值来创建。 默认设置的选项有 OP_NO_COMPRESSION, OP_CIPHER_SERVER_PREFERENCE, OP_SINGLE_DH_USE, OP_SINGLE_ECDH_USE, OP_NO_SSLv2 (except for PROTOCOL_SSLv2) 和 OP_NO_SSLv3 (except for PROTOCOL_SSLv3)。 初始密码集列表只包含 HIGH 密码，不包含 NULL 密码和 MD5 密码 (PROTOCOL_SSLv2 除外)。

SSLContext 对象具有以下方法和属性:

SSLContext.cert_store_stats()

获取以字典表示的有关已加载的 X.509 证书数量，被标记为 CA 证书的 X.509 证书数量以及证书吊销列表的的统计信息。

具有一个 CA 证书和一个其他证书的上下文示例:

>>> context.cert_store_stats()
{'crl': 0, 'x509_ca': 1, 'x509': 2}

3.4 新版功能.

SSLContext.load_cert_chain(certfile, keyfile=None, password=None)

加载一个私钥及对应的证书。 certfile 字符串必须为以 PEM 格式表示的单个文件路径，该文件中包含证书以及确立证书真实性所需的任意数量的 CA 证书。 如果存在 keyfile 字符串，它必须指向一个包含私钥的文件。 否则私钥也将从 certfile 中提取。 请参阅 证书 中的讨论来了解有关如何将证书存储至 certfile 的更多信息。

password 参数可以是一个函数，调用时将得到用于解密私钥的密码。 它在私钥被加密且需要密码时才会被调用。 它调用时将不带任何参数，并且应当返回一个字符串、字节串或字节数组。 如果返回值是一个字符串，在用它解密私钥之前它将以 UTF-8 进行编码。 或者也可以直接将字符串、字节串或字节数组值作为 password 参数提供。 如果私钥未被加密且不需要密码则它将被忽略。

如果未指定 password 参数且需要一个密码，将会使用 OpenSSL 内置的密码提示机制来交互式地提示用户输入密码。

如果私钥不能匹配证书则会引发 SSLError。

在 3.3 版更改: 新增可选参数 password。

SSLContext.load_default_certs(purpose=Purpose.SERVER_AUTH)

从默认位置加载一组默认的 “证书颁发机构” (CA) 证书。 在 Windows 上它将从 CA 和 ROOT 系统存储中加载 CA 证书。 在其他系统上它会调用 SSLContext.set_default_verify_paths()。 将来该方法也可能会从其他位置加载 CA 证书。

purpose 旗标指明要加载哪一类 CA 证书。 默认设置 Purpose.SERVER_AUTH 加载被标记且被信任用于 TLS Web 服务器验证（客户端套接字）的证书。 Purpose.CLIENT_AUTH 则加载用于在服务器端进行客户端证书验证的 CA 证书。

3.4 新版功能.

SSLContext.load_verify_locations(cafile=None, capath=None, cadata=None)

当 verify_mode 不为 CERT_NONE 时加载一组用于验证其他对等方证书的 “证书颁发机构” (CA) 证书。 必须至少指定 cafile 或 capath 中的一个。

此方法还可加载 PEM 或 DER 格式的证书吊销列表 (CRL)，为此必须正确配置 SSLContext.verify_flags。

如果存在 cafile 字符串，它应为 PEM 格式的级联 CA 证书文件的路径。 请参阅 证书 中的讨论来了解有关如何处理此文件中的证书的更多信息。

The capath string, if present, is the path to a directory containing several CA certificates in PEM format, following an OpenSSL specific layout [https://www.openssl.org/docs/man1.1.0/ssl/SSL_CTX_load_verify_locations.html].

如果存在 cadata 对象，它应为一个或多个 PEM 编码的证书的 ASCII 字符串或者 DER 编码的证书的 bytes-like object。 与 capath 一样 PEM 编码的证书之外的多余行会被忽略，但至少要有一个证书。

在 3.4 版更改: 新增可选参数 cadata

SSLContext.get_ca_certs(binary_form=False)

获取已离开法人 “证书颁发机构” (CA) 证书列表。 如果 binary_form 形参为 False 则每个列表条目都是一个类似于 SSLSocket.getpeercert() 输出的字典。 在其他情况下此方法将返回一个 DER 编码的证书的列表。 返回的列表不包含来自 capath 的证书，除非 SSL 连接请求并加载了一个证书。

注解

capath 目录中的证书不会被加载，除非它们已至少被使用过一次。

3.4 新版功能.

SSLContext.get_ciphers()

获取已启用密码的列表。 该列表将按密码的优先级排序。 参见 SSLContext.set_ciphers()。

示例:

>>> ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
>>> ctx.set_ciphers('ECDHE+AESGCM:!ECDSA')
>>> ctx.get_ciphers()  # OpenSSL 1.0.x
[{'alg_bits': 256,
  'description': 'ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2 Kx=ECDH     Au=RSA  '
                 'Enc=AESGCM(256) Mac=AEAD',
  'id': 50380848,
  'name': 'ECDHE-RSA-AES256-GCM-SHA384',
  'protocol': 'TLSv1/SSLv3',
  'strength_bits': 256},
 {'alg_bits': 128,
  'description': 'ECDHE-RSA-AES128-GCM-SHA256 TLSv1.2 Kx=ECDH     Au=RSA  '
                 'Enc=AESGCM(128) Mac=AEAD',
  'id': 50380847,
  'name': 'ECDHE-RSA-AES128-GCM-SHA256',
  'protocol': 'TLSv1/SSLv3',
  'strength_bits': 128}]

在 OpenSSL 1.1 及更新的版本中密码字典会包含额外的字段:

>>> ctx.get_ciphers()  # OpenSSL 1.1+
[{'aead': True,
  'alg_bits': 256,
  'auth': 'auth-rsa',
  'description': 'ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2 Kx=ECDH     Au=RSA  '
                 'Enc=AESGCM(256) Mac=AEAD',
  'digest': None,
  'id': 50380848,
  'kea': 'kx-ecdhe',
  'name': 'ECDHE-RSA-AES256-GCM-SHA384',
  'protocol': 'TLSv1.2',
  'strength_bits': 256,
  'symmetric': 'aes-256-gcm'},
 {'aead': True,
  'alg_bits': 128,
  'auth': 'auth-rsa',
  'description': 'ECDHE-RSA-AES128-GCM-SHA256 TLSv1.2 Kx=ECDH     Au=RSA  '
                 'Enc=AESGCM(128) Mac=AEAD',
  'digest': None,
  'id': 50380847,
  'kea': 'kx-ecdhe',
  'name': 'ECDHE-RSA-AES128-GCM-SHA256',
  'protocol': 'TLSv1.2',
  'strength_bits': 128,
  'symmetric': 'aes-128-gcm'}]

Availability: OpenSSL 1.0.2+

3.6 新版功能.

SSLContext.set_default_verify_paths()

从构建 OpenSSL 库时定义的文件系统路径中加载一组默认的 “证书颁发机构” (CA) 证书。 不幸的是，没有一种简单的方式能知道此方法是否执行成功：如果未找到任何证书也不会返回错误。 不过，当 OpenSSL 库是作为操作系统的一部分被提供时，它的配置应当是正确的。

SSLContext.set_ciphers(ciphers)

为使用此上下文创建的套接字设置可用密码。 它应当为 OpenSSL 密码列表格式 [https://www.openssl.org/docs/manmaster/man1/ciphers.html] 的字符串。 如果没有可被选择的密码（由于编译时选项或其他配置禁止使用所指定的任何密码），则将引发 SSLError。

注解

在连接后，SSL 套接字的 SSLSocket.cipher() 方法将给出当前所选择的密码。

在默认情况下 OpenSSL 1.1.1 会启用 TLS 1.3 密码套件。 该套件不能通过 set_ciphers() 来禁用。

SSLContext.set_alpn_protocols(protocols)

指定在 SSL/TLS 握手期间套接字应当通告的协议。 它应为由 ASCII 字符串组成的列表，例如 ['http/1.1', 'spdy/2']，按首选顺序排列。 协议的选择将在握手期间发生，并依据 RFC 7301 [https://tools.ietf.org/html/rfc7301.html] 来执行。 在握手成功后，SSLSocket.selected_alpn_protocol() 方法将返回已达成一致的协议。

This method will raise NotImplementedError if HAS_ALPN is False.

当双方都支持 ALPN 但不能就协议达成一致时 OpenSSL 1.1.0 至 1.1.0e 将中止并引发 SSLError。 1.1.0f+ 的行为类似于 1.0.2，SSLSocket.selected_alpn_protocol() 返回 None。

3.5 新版功能.

SSLContext.set_npn_protocols(protocols)

Specify which protocols the socket should advertise during the SSL/TLS handshake. It should be a list of strings, like ['http/1.1', 'spdy/2'], ordered by preference. The selection of a protocol will happen during the handshake, and will play out according to the NPN draft specification [https://tools.ietf.org/html/draft-agl-tls-nextprotoneg]. After a successful handshake, the SSLSocket.selected_npn_protocol() method will return the agreed-upon protocol.

This method will raise NotImplementedError if HAS_NPN is False.

3.3 新版功能.

SSLContext.set_servername_callback(server_name_callback)

注册一个回调函数，当 TLS 客户端指定了一个服务器名称提示时，该回调函数将在 SSL/TLS 服务器接收到 TLS Client Hello 握手消息后被调用。 服务器名称提示机制的定义见 RFC 6066 [https://tools.ietf.org/html/rfc6066.html] section 3 - Server Name Indication。

Only one callback can be set per SSLContext. If server_name_callback is None then the callback is disabled. Calling this function a subsequent time will disable the previously registered callback.

The callback function, server_name_callback, will be called with three arguments; the first being the ssl.SSLSocket, the second is a string that represents the server name that the client is intending to communicate (or None if the TLS Client Hello does not contain a server name) and the third argument is the original SSLContext. The server name argument is the IDNA decoded server name.

此回调的一个典型用法是将 ssl.SSLSocket 的 SSLSocket.context 属性修改为一个 SSLContext 类型的新对象，该对象代表与服务器相匹配的证书链。

由于 TLS 连接处于早期协商阶段，因此仅能使用有限的方法和属性例如 SSLSocket.selected_alpn_protocol() 和 SSLSocket.context。 SSLSocket.getpeercert(), SSLSocket.getpeercert(), SSLSocket.cipher() 和 SSLSocket.compress() 方法要求 TLS 连接已经过 TLS Client Hello 因而将既不包含返回有意义的值，也不能安全地调用它们。

The server_name_callback function must return None to allow the TLS negotiation to continue. If a TLS failure is required, a constant ALERT_DESCRIPTION_* can be returned. Other return values will result in a TLS fatal error with ALERT_DESCRIPTION_INTERNAL_ERROR.

If there is an IDNA decoding error on the server name, the TLS connection will terminate with an ALERT_DESCRIPTION_INTERNAL_ERROR fatal TLS alert message to the client.

If an exception is raised from the server_name_callback function the TLS connection will terminate with a fatal TLS alert message ALERT_DESCRIPTION_HANDSHAKE_FAILURE.

如果 OpenSSL library 库在构建时定义了 OPENSSL_NO_TLSEXT 则此方法将返回 NotImplementedError。

3.4 新版功能.

SSLContext.load_dh_params(dhfile)

加载密钥生成参数用于 Diffie-Hellman (DH) 密钥交换。 使用 DH 密钥交换能以消耗（服务器和客户端的）计算资源为代价提升前向保密性。 dhfile 参数应当为指向一个包含 PEM 格式的 DH 形参的文件的路径。

此设置不会应用于客户端套接字。 你还可以使用 OP_SINGLE_DH_USE 选项来进一步提升安全性。

3.3 新版功能.

SSLContext.set_ecdh_curve(curve_name)

为基于椭圆曲线的 Elliptic Curve-based Diffie-Hellman (ECDH) 密钥交换设置曲线名称。 ECDH 显著快于常规 DH 同时据信同样安全。 curve_name 形参应为描述某个知名椭圆曲线的字符串，例如受到广泛支持的曲线 prime256v1。

此设置不会应用于客户端套接字。 你还可以使用 OP_SINGLE_ECDH_USE 选项来进一步提升安全性。

如果 HAS_ECDH 为 False 则此方法将不可用。

3.3 新版功能.

参见

SSL/TLS & Perfect Forward Secrecy [http://vincent.bernat.im/en/blog/2011-ssl-perfect-forward-secrecy.html]

Vincent Bernat。

SSLContext.wrap_socket(sock, server_side=False, do_handshake_on_connect=True, suppress_ragged_eofs=True, server_hostname=None, session=None)

Wrap an existing Python socket sock and return an SSLSocket object. sock must be a SOCK_STREAM socket; other socket types are unsupported.

The returned SSL socket is tied to the context, its settings and certificates. The parameters server_side, do_handshake_on_connect and suppress_ragged_eofs have the same meaning as in the top-level wrap_socket() function.

在客户端连接上，可选形参 server_hostname 指定所要连接的服务的主机名。 这允许单个服务器托管具有单独证书的多个基于 SSL 的服务，很类似于 HTTP 虚拟主机。 如果 server_side 为真值则指定 server_hostname 将引发 ValueError。

session，参见 session。

在 3.5 版更改: 总是允许传送 server_hostname，即使 OpenSSL 没有 SNI。

在 3.6 版更改: 增加了 session 参数。

SSLContext.wrap_bio(incoming, outgoing, server_side=False, server_hostname=None, session=None)

Create a new SSLObject instance by wrapping the BIO objects incoming and outgoing. The SSL routines will read input data from the incoming BIO and write data to the outgoing BIO.

server_side, server_hostname 和 session 形参具有与 SSLContext.wrap_socket() 中相同的含义。

在 3.6 版更改: 增加了 session 参数。

SSLContext.session_stats()

获取由此上下文所创建或管理的 SSL 会话的相关统计信息。 返回将每个 信息片 [https://www.openssl.org/docs/man1.1.0/ssl/SSL_CTX_sess_number.html] 的名称映射到其数字值的字典。 例如，以下是自上下文被创建以来会话缓存中命中和未命中的总数:

>>> stats = context.session_stats()
>>> stats['hits'], stats['misses']
(0, 0)

SSLContext.check_hostname

Whether to match the peer cert’s hostname with match_hostname() in SSLSocket.do_handshake(). The context’s verify_mode must be set to CERT_OPTIONAL or CERT_REQUIRED, and you must pass server_hostname to wrap_socket() in order to match the hostname.

示例:

import socket, ssl

context = ssl.SSLContext()
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = True
context.load_default_certs()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = context.wrap_socket(s, server_hostname='www.verisign.com')
ssl_sock.connect(('www.verisign.com', 443))

3.4 新版功能.

注解

此特性要求 OpenSSL 0.9.8f 或更新的版本。

SSLContext.options

一个代表此上下文中所启用的 SSL 选项集的整数。 默认值为 OP_ALL，但你也可以通过在选项间进行 OR 运算来指定其他选项例如 OP_NO_SSLv2。

注解

With versions of OpenSSL older than 0.9.8m, it is only possible to set options, not to clear them. Attempting to clear an option (by resetting the corresponding bits) will raise a ValueError.

在 3.6 版更改: SSLContext.options 返回 Options 旗标:

>>> ssl.create_default_context().options
<Options.OP_ALL|OP_NO_SSLv3|OP_NO_SSLv2|OP_NO_COMPRESSION: 2197947391>

SSLContext.post_handshake_auth

启用 TLS 1.3 握手后客户端身份验证。 握手后验证默认是被禁用的，服务器只能在初始握手期间请求 TLS 客户端证书。 当启用时，服务器可以在握手之后的任何时候请求 TLS 客户端证书。

当在客户端套接字上启用时，客户端会向服务器发信号说明它支持握手后身份验证。

当在服务器端套接字上启用时，SSLContext.verify_mode 也必须被设为 CERT_OPTIONAL 或 CERT_REQUIRED。 实际的客户端证书交换会被延迟直至 SSLSocket.verify_client_post_handshake() 被调用并执行了一些 I/O 操作后再进行。

3.6.7 新版功能.

注解

仅在 OpenSSL 1.1.1 且 TLS 1.3 被启用时可用。 如果没有 TLS 1.3 支持，该属性值将为 None 且不可被更改

SSLContext.protocol

构造上下文时所选择的协议版本。 这个属性是只读的。

SSLContext.verify_flags

证书验证操作的旗标。 你可以通过对 VERIFY_CRL_CHECK_LEAF 等值执行 OR 运算来设置组合旗标。 在默认情况下 OpenSSL 不会要求也不会验证证书吊销列表（CRL）。 仅在 openssl 版本 0.9.8+ 上可用。

3.4 新版功能.

在 3.6 版更改: SSLContext.verify_flags 返回 VerifyFlags 旗标:

>>> ssl.create_default_context().verify_flags
<VerifyFlags.VERIFY_X509_TRUSTED_FIRST: 32768>

SSLContext.verify_mode

是否要尝试验证其他对等方的证书以及如果验证失败应采取何种行为。 该属性值必须为 CERT_NONE, CERT_OPTIONAL 或 CERT_REQUIRED 之一。

在 3.6 版更改: SSLContext.verify_mode 返回 VerifyMode 枚举:

>>> ssl.create_default_context().verify_mode
<VerifyMode.CERT_REQUIRED: 2>

18.2.4. 证书

总的来说证书是公钥/私钥系统的一个组成部分。 在这个系统中，每 个 主体 (可能是一台机器、一个人或者一个组织) 都会分配到唯一的包含两部分的加密密钥。 一部分密钥是公开的，称为 公钥；另一部分密钥是保密的，称为 私钥。 这两个部分是互相关联的，就是说如果你用其中一个部分来加密一条消息，你将能用并且 只能 用另一个部分来解密它。

在一个证书中包含有两个主体的相关信息。 它包含 目标方 的名称和目标方的公钥。 它还包含由第二个主体 颁发方 所发布的声明：目标方的身份与他们所宣称的一致，包含的公钥也确实是目标方的公钥。 颁发方的声明使用颁发方的私钥进行签名，该私钥的内容只有颁发方自己才知道。 但是，任何人都可以找到颁发方的公钥，用它来解密这个声明，并将其与证书中的其他信息进行比较来验证颁发方声明的真实性。 证书还包含有关其有效期限的信息。 这被表示为两个字段，即 “notBefore” 和 “notAfter”。

在 Python 中应用证书时，客户端或服务器可以用证书来证明自己的身份。 还可以要求网络连接的另一方提供证书，提供的证书可以用于验证以满足客户端或服务器的验证要求。 如果验证失败，连接尝试可被设置为引发一个异常。 验证是由下层的 OpenSSL 框架来自动执行的；应用程序本身不必关注其内部的机制。 但是应用程序通常需要提供一组证书以允许此过程的发生。

Python 使用文件来包含证书。 它们应当采用 “PEM” 格式 (参见 RFC 1422 [https://tools.ietf.org/html/rfc1422.html])，这是一种带有头部行和尾部行的 base-64 编码包装形式:

-----BEGIN CERTIFICATE-----
... (certificate in base64 PEM encoding) ...
-----END CERTIFICATE-----

18.2.4.1. 证书链

包含证书的 Python 文件可以包含一系列的证书，有时被称为 证书链。 这个证书链应当以 “作为” 客户端或服务器的主体的专属证书打头，然后是证书颁发方的证书，然后是 上述 证书的颁发方的证书，证书链就这样不断上溯直到你得到一个 自签名 的证书，即具有相同目标方和颁发方的证书，有时也称为 根证书。 在证书文件中这些证书应当被拼接为一体。 例如，假设我们有一个包含三个证书的证书链，以我们的服务器证书打头，然后是为我们的服务器证书签名的证书颁发机构的证书，最后是为证书颁发机构的证书颁发证书的机构的根证书:

-----BEGIN CERTIFICATE-----
... (certificate for your server)...
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
... (the certificate for the CA)...
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
... (the root certificate for the CA's issuer)...
-----END CERTIFICATE-----

18.2.4.2. CA 证书

如果你想要求对连接的另一方的证书进行验证，你必须提供一个 “CA 证书” 文件，其中包含了你愿意信任的每个颁发方的证书链。 同样地，这个文件的内容就是这些证书链拼接在一起的结果。 为了进行验证，Python 将使用它在文件中找到的第一个匹配的证书链。 可以通过调用 SSLContext.load_default_certs() 来使用系统平台的证书文件，这可以由 create_default_context() 自动完成。

18.2.4.3. 合并的密钥和证书

私钥往往与证书存储在相同的文件中；在此情况下，只需要将 certfile 形参传给 SSLContext.load_cert_chain() 和 wrap_socket()。 如果私钥是与证书一起存储的，则它应当放在证书链的第一个证书之前:

-----BEGIN RSA PRIVATE KEY-----
... (private key in base64 encoding) ...
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
... (certificate in base64 PEM encoding) ...
-----END CERTIFICATE-----

18.2.4.4. 自签名证书

如果你准备创建一个提供 SSL 加密连接服务的服务器，你需要为该服务获取一份证书。 有许多方式可以获取合适的证书，例如从证书颁发机构购买。 另一种常见做法是生成自签名证书。 生成自签名证书的最简单方式是使用 OpenSSL 软件包，代码如下所示:

% openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem
Generating a 1024 bit RSA private key
.......++++++
.............................++++++
writing new private key to 'cert.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:MyState
Locality Name (eg, city) []:Some City
Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Organization, Inc.
Organizational Unit Name (eg, section) []:My Group
Common Name (eg, YOUR name) []:myserver.mygroup.myorganization.com
Email Address []:ops@myserver.mygroup.myorganization.com
%

自签名证书的缺点在于它是它自身的根证书，因此不会存在于别人的已知（且信任的）根证书缓存当中。

18.2.5. 例子

18.2.5.1. 检测 SSL 支持

要检测一个 Python 安装版中是否带有 SSL 支持，用户代码应当使用以下例程:

try:
    import ssl
except ImportError:
    pass
else:
    ...  # do something that requires SSL support

18.2.5.2. 客户端操作

这个例子创建了一个 SSL 上下文并使用客户端套接字的推荐安全设置，包括自动证书验证:

>>> context = ssl.create_default_context()

如果你喜欢自行调整安全设置，你可能需要从头创建一个上下文（但是请请注意避免不正确的设置）:

>>> context = ssl.SSLContext()
>>> context.verify_mode = ssl.CERT_REQUIRED
>>> context.check_hostname = True
>>> context.load_verify_locations("/etc/ssl/certs/ca-bundle.crt")

(这段代码假定你的操作系统将所有 CA 证书打包存放于 /etc/ssl/certs/ca-bundle.crt；如果不是这样，你将收到报错信息，必须修改此位置)

When you use the context to connect to a server, CERT_REQUIRED validates the server certificate: it ensures that the server certificate was signed with one of the CA certificates, and checks the signature for correctness:

>>> conn = context.wrap_socket(socket.socket(socket.AF_INET),
...                            server_hostname="www.python.org")
>>> conn.connect(("www.python.org", 443))

你可以随后获取该证书:

>>> cert = conn.getpeercert()

可视化检查显示证书能够证明目标服务 (即 HTTPS 主机 www.python.org) 的身份:

>>> pprint.pprint(cert)
{'OCSP': ('http://ocsp.digicert.com',),
 'caIssuers': ('http://cacerts.digicert.com/DigiCertSHA2ExtendedValidationServerCA.crt',),
 'crlDistributionPoints': ('http://crl3.digicert.com/sha2-ev-server-g1.crl',
                           'http://crl4.digicert.com/sha2-ev-server-g1.crl'),
 'issuer': ((('countryName', 'US'),),
            (('organizationName', 'DigiCert Inc'),),
            (('organizationalUnitName', 'www.digicert.com'),),
            (('commonName', 'DigiCert SHA2 Extended Validation Server CA'),)),
 'notAfter': 'Sep  9 12:00:00 2016 GMT',
 'notBefore': 'Sep  5 00:00:00 2014 GMT',
 'serialNumber': '01BB6F00122B177F36CAB49CEA8B6B26',
 'subject': ((('businessCategory', 'Private Organization'),),
             (('1.3.6.1.4.1.311.60.2.1.3', 'US'),),
             (('1.3.6.1.4.1.311.60.2.1.2', 'Delaware'),),
             (('serialNumber', '3359300'),),
             (('streetAddress', '16 Allen Rd'),),
             (('postalCode', '03894-4801'),),
             (('countryName', 'US'),),
             (('stateOrProvinceName', 'NH'),),
             (('localityName', 'Wolfeboro,'),),
             (('organizationName', 'Python Software Foundation'),),
             (('commonName', 'www.python.org'),)),
 'subjectAltName': (('DNS', 'www.python.org'),
                    ('DNS', 'python.org'),
                    ('DNS', 'pypi.org'),
                    ('DNS', 'docs.python.org'),
                    ('DNS', 'testpypi.org'),
                    ('DNS', 'bugs.python.org'),
                    ('DNS', 'wiki.python.org'),
                    ('DNS', 'hg.python.org'),
                    ('DNS', 'mail.python.org'),
                    ('DNS', 'packaging.python.org'),
                    ('DNS', 'pythonhosted.org'),
                    ('DNS', 'www.pythonhosted.org'),
                    ('DNS', 'test.pythonhosted.org'),
                    ('DNS', 'us.pycon.org'),
                    ('DNS', 'id.python.org')),
 'version': 3}

现在 SSL 通道已建立并已验证了证书，你可以继续与服务器对话了:

>>> conn.sendall(b"HEAD / HTTP/1.0\r\nHost: linuxfr.org\r\n\r\n")
>>> pprint.pprint(conn.recv(1024).split(b"\r\n"))
[b'HTTP/1.1 200 OK',
 b'Date: Sat, 18 Oct 2014 18:27:20 GMT',
 b'Server: nginx',
 b'Content-Type: text/html; charset=utf-8',
 b'X-Frame-Options: SAMEORIGIN',
 b'Content-Length: 45679',
 b'Accept-Ranges: bytes',
 b'Via: 1.1 varnish',
 b'Age: 2188',
 b'X-Served-By: cache-lcy1134-LCY',
 b'X-Cache: HIT',
 b'X-Cache-Hits: 11',
 b'Vary: Cookie',
 b'Strict-Transport-Security: max-age=63072000; includeSubDomains',
 b'Connection: close',
 b'',
 b'']

参见下文对于 安全考量 的讨论。

18.2.5.3. 服务器端操作

对于服务器操作，通常你需要在文件中存放服务器证书和私钥各一份。 你将首先创建一个包含密钥和证书的上下文，这样客户端就能检查你的身份真实性。 然后你将打开一个套接字，将其绑定到一个端口，在其上调用 listen()，并开始等待客户端连接:

import socket, ssl

context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain(certfile="mycertfile", keyfile="mykeyfile")

bindsocket = socket.socket()
bindsocket.bind(('myaddr.mydomain.com', 10023))
bindsocket.listen(5)

当有客户端连接时，你将在套接字上调用 accept() 以从另一端获取新的套接字，并使用上下文的 SSLContext.wrap_socket() 方法来为连接创建一个服务器端 SSL 套接字:

while True:
    newsocket, fromaddr = bindsocket.accept()
    connstream = context.wrap_socket(newsocket, server_side=True)
    try:
        deal_with_client(connstream)
    finally:
        connstream.shutdown(socket.SHUT_RDWR)
        connstream.close()

随后你将从 connstream 读取数据并对其进行处理，直至你结束与客户端的会话（或客户端结束与你的会话）:

def deal_with_client(connstream):
    data = connstream.recv(1024)
    # empty data means the client is finished with us
    while data:
        if not do_something(connstream, data):
            # we'll assume do_something returns False
            # when we're finished with client
            break
        data = connstream.recv(1024)
    # finished with client

并返回至监听新的客户端连接（当然，真正的服务器应当会在单独的线程中处理每个客户端连接，或者将套接字设为 非阻塞模式 并使用事件循环）。

18.2.6. 关于非阻塞套接字的说明

在非阻塞模式下 SSL 套接字的行为与常规套接字略有不同。 当使用非阻塞模式时，你需要注意下面这些事情:

如果一个 I/O 操作会阻塞，大多数 SSLSocket 方法都将引发 SSLWantWriteError 或 SSLWantReadError 而非 BlockingIOError。 如果有必要在下层套接字上执行读取操作将引发 SSLWantReadError，在下层套接字上执行写入操作则将引发 SSLWantWriteError。 请注意尝试 写入 到 SSL 套接字可能需要先从下层套接字 读取，而尝试从 SSL 套接字 读取 则可能需要先向下层套接字 写入。

在 3.5 版更改: 在较早的 Python 版本中，SSLSocket.send() 方法会返回零值而非引发 SSLWantWriteError 或 SSLWantReadError。

调用 select() 将告诉你可以从 OS 层级的套接字读取（或向其写入），但这并不意味着在上面的 SSL 层有足够的数据。 例如，可能只有部分 SSL 帧已经到达。 因此，你必须准备好处理 SSLSocket.recv() 和 SSLSocket.send() 失败的情况，并在再次调用 select() 之后重新尝试。

相反地，由于 SSL 层具有自己的帧机制，一个 SSL 套接字可能仍有可读取的数据而 select() 并不知道这一点。 因此，你应当先调用 SSLSocket.recv() 取走所有潜在的可用数据，然后只在必要时对 select() 调用执行阻塞。

(当然，类似的保留规则在使用其他原语例如 poll()，或 selectors 模块中的原语时也适用)

SSL 握手本身将是非阻塞的: SSLSocket.do_handshake() 方法必须不断重试直至其成功返回。 下面是一个使用 select() 来等待套接字就绪的简短例子:

while True:
    try:
        sock.do_handshake()
        break
    except ssl.SSLWantReadError:
        select.select([sock], [], [])
    except ssl.SSLWantWriteError:
        select.select([], [sock], [])

参见

asyncio 模块支持 非阻塞 SSL 套接字 并提供了更高层级的 API。 它会使用 selectors 模块来轮询事件并处理 SSLWantWriteError, SSLWantReadError 和 BlockingIOError 等异常。 它还会异步地执行 SSL 握手。

18.2.7. 内存 BIO 支持

3.5 新版功能.

自从 SSL 模块在 Python 2.6 起被引入之后，SSLSocket 类提供了两个互相关联但彼此独立的功能分块:

SSL 协议处理

网络 IO

网络 IO API 与 socket.socket 所提供的功能一致，SSLSocket 也是从那里继承而来的。 这允许 SSL 套接字被用作常规套接字的替代，使得向现有应用程序添加 SSL 支持变得非常容易。

将 SSL 协议处理与网络 IO 结合使用通常都能运行良好，但在某些情况下则不能。 此情况的一个例子是 async IO 框架，该框架要使用不同的 IO 多路复用模型而非 (基于就绪状态的) “在文件描述器上执行选择/轮询” 模型，该模型是 socket.socket 和内部 OpenSSL 套接字 IO 例程正常运行的假设前提。 这种情况在该模型效率不高的 Windows 平台上最为常见。 为此还提供了一个 SSLSocket 的简化形式，称为 SSLObject。

class ssl.SSLObject

SSLSocket 的简化形式，表示一个不包含任何网络 IO 方法的 SSL 协议实例。 这个类通常由想要通过内存缓冲区为 SSL 实现异步 IO 的框架作者来使用。

这个类在低层级 SSL 对象上实现了一个接口，与 OpenSSL 所实现的类似。 此对象会捕获 SSL 连接的状态但其本身不提供任何网络 IO。 IO 需要通过单独的 “BIO” 对象来执行，该对象是 OpenSSL 的 IO 抽象层。

An SSLObject instance can be created using the wrap_bio() method. This method will create the SSLObject instance and bind it to a pair of BIOs. The incoming BIO is used to pass data from Python to the SSL protocol instance, while the outgoing BIO is used to pass data the other way around.

可以使用以下方法：

context

server_side

server_hostname

session

session_reused

read()

write()

getpeercert()

selected_npn_protocol()

cipher()

shared_ciphers()

compression()

pending()

do_handshake()

unwrap()

get_channel_binding()

与 SSLSocket 相比，此对象缺少下列特性:

任何形式的网络 IO; recv() 和 send() 仅对下层的 MemoryBIO 缓冲区执行读取和写入。

不存在 do_handshake_on_connect 机制。 你必须总是手动调用 do_handshake() 来开始握手操作。

不存在对 suppress_ragged_eofs 的处理。 所有违反协议的文件结束条件将通过 SSLEOFError 异常来报告。

方法 unwrap() 的调用不返回任何东西，不会如 SSL 套接字那样返回下层的套接字。

server_name_callback 回调被传给 SSLContext.set_servername_callback() 时将获得一个 SSLObject 实例而非 SSLSocket 实例作为其第一个形参。

有关 SSLObject 用法的一些说明:

在 SSLObject 上的所有 IO 都是 非阻塞的。 这意味着例如 read() 在其需要比 incoming BIO 可用的更多数据时将会引发 SSLWantReadError。

不存在模块层级的 wrap_bio() 调用，就像 wrap_socket() 那样。 SSLObject 总是通过 SSLContext 来创建。

SSLObject 会使用内存缓冲区与外部世界通信。 MemoryBIO 类提供了可被用于此目的的内存缓冲区。 它包装了一个 OpenSSL 内存 BIO (Basic IO) 对象:

class ssl.MemoryBIO

一个可被用来在 Python 和 SSL 协议实例之间传递数据的内存缓冲区。

pending

返回当前存在于内存缓冲区的字节数。

eof

一个表明内存 BIO 目前是否位于文件末尾的布尔值。

read(n=-1)

从内存缓冲区读取至多 n 个字节。 如果 n 未指定或为负值，则返回全部字节数据。

write(buf)

将字节数据从 buf 写入到内存 BIO。 buf 参数必须为支持缓冲区协议的对象。

返回值为写入的字节数，它总是与 buf 的长度相等。

write_eof()

将一个 EOF 标记写入到内存 BIO。 在此方法被调用以后，再调用 write() 将是非法的。 属性 eof will 在缓冲区当前的所有数据都被读取之后将变为真值。

18.2.8. SSL 会话

3.6 新版功能.

class ssl.SSLSession

session 所使用的会话对象。

id

time

timeout

ticket_lifetime_hint

has_ticket

18.2.9. 安全考量

18.2.9.1. 最佳默认值

针对 客户端使用，如果你对于安全策略没有任何特殊要求，则强烈推荐你使用 create_default_context() 函数来创建你的 SSL 上下文。 它将加载系统的受信任 CA 证书，启用证书验证和主机名检查，并尝试合理地选择安全的协议和密码设置。

例如，以下演示了你应当如何使用 smtplib.SMTP 类来创建指向一个 SMTP 服务器的受信任且安全的连接:

>>> import ssl, smtplib
>>> smtp = smtplib.SMTP("mail.python.org", port=587)
>>> context = ssl.create_default_context()
>>> smtp.starttls(context=context)
(220, b'2.0.0 Ready to start TLS')

如果连接需要客户端证书，可使用 SSLContext.load_cert_chain() 来添加。

作为对比，如果你通过自行调用 SSLContext 构造器来创建 SSL 上下文，它默认将不会启用证书验证和主机名检查。 如果你这样做，请阅读下面的段落以达到良好的安全级别。

18.2.9.2. 手动设置

18.2.9.2.1. 验证证书

当直接调用 SSLContext 构造器时，默认会使用 CERT_NONE。 由于它不会验证对等方的身份真实性，因此是不安全的，特别是在客户端模式下，大多数时候你都希望能保证你所连接的服务器的身份真实性。 因此，当处于客户端模式时，强烈推荐使用 CERT_REQUIRED。 但是，光这样还不够；你还必须检查服务器证书，这可以通过调用 SSLSocket.getpeercert() 来获取并匹配目标服务。 对于许多协议和应用来说，服务可通过主机名来标识；在此情况下，可以使用 match_hostname() 函数。 这种通用检测会在 SSLContext.check_hostname 被启用时自动执行。

在服务器模式下，如果你想要使用 SSL 层来验证客户端（而不是使用更高层级的验证机制），你也必须要指定 CERT_REQUIRED 并以类似方式检查客户端证书。

18.2.9.2.2. 协议版本

SSL 版本 2 和 3 被认为是不安全的因而使用它们会有风险。 如果你想要客户端和服务器之间有最大的兼容性，推荐使用 PROTOCOL_TLS_CLIENT 或 PROTOCOL_TLS_SERVER 作为协议版本。 SSLv2 和 SSLv3 默认会被禁用。

>>> client_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
>>> client_context.options |= ssl.OP_NO_TLSv1
>>> client_context.options |= ssl.OP_NO_TLSv1_1

前面创建的 SSL 上下文将只允许 TLSv1.2 及更新版本（如果你的系统支持）的服务器连接。 PROTOCOL_TLS_CLIENT 默认会使用证书验证和主机名检查。 你必须将证书加载到上下文中。

18.2.9.2.3. 密码选择

If you have advanced security requirements, fine-tuning of the ciphers enabled when negotiating a SSL session is possible through the SSLContext.set_ciphers() method. Starting from Python 3.2.3, the ssl module disables certain weak ciphers by default, but you may want to further restrict the cipher choice. Be sure to read OpenSSL’s documentation about the cipher list format [https://www.openssl.org/docs/apps/ciphers.html#CIPHER-LIST-FORMAT]. If you want to check which ciphers are enabled by a given cipher list, use SSLContext.get_ciphers() or the openssl ciphers command on your system.

18.2.9.3. 多进程

如果使用此模块作为多进程应用的一部分（例如使用 multiprocessing 或 concurrent.futures 模块），请注意 OpenSSL 的内部随机数字生成器并不能正确处理分支进程。 应用程序必须修改父进程的 PRNG 状态，如果它们要使用任何包含 os.fork() 的 SSL 特性的话。 任何对 RAND_add(), RAND_bytes() 或 RAND_pseudo_bytes() 都可以 。

18.2.10. LibreSSL 支持

LibreSSL 是 OpenSSL 1.0.1 的一个分支。 ssl 模块包含对 LibreSSL 的有限支持。 当 ssl 模块使用 LibreSSL 进行编译时某些特性将不可用。

LibreSSL >= 2.6.1 不再支持 NPN。 SSLContext.set_npn_protocols() 和 SSLSocket.selected_npn_protocol() 方法将不可用。

SSLContext.set_default_verify_paths() 会忽略环境变量 SSL_CERT_FILE 和 SSL_CERT_PATH，虽然 get_default_verify_paths() 仍然支持它们。

参见

Class socket.socket

下层 socket 类的文档

SSL/TLS 高强度加密：概述 [https://httpd.apache.org/docs/trunk/en/ssl/ssl_intro.html]

Apache HTTP Server文档介绍

RFC 1422: 因特网电子邮件的隐私加强：第二部分：基于证书的密钥管理 [https://tools.ietf.org/html/rfc1422.html]

Steve Kent

RFC 4086: 确保安全的随机性要求 [https://tools.ietf.org/html/rfc4086.html]

Donald E., Jeffrey I. Schiller

RFC 5280: 互联网 X.509 公钥基础架构证书和证书吊销列表 (CRL) 配置文件 [https://tools.ietf.org/html/rfc5280.html]

D. Cooper

RFC 5246: 传输层安全性 (TLS) 协议版本 1.2 [https://tools.ietf.org/html/rfc5246.html]

T. Dierks et. al.

RFC 6066: 传输层安全性 (TLS) 的扩展 [https://tools.ietf.org/html/rfc6066.html]

D. Eastlake

IANA TLS: 传输层安全性 (TLS) 的参数 [https://www.iana.org/assignments/tls-parameters/tls-parameters.xml]

IANA

RFC 7525: 传输层安全性 (TLS) 和数据报传输层安全性 (DTLS) 的安全使用建议 [https://tools.ietf.org/html/rfc7525.html]

IETF

Mozilla 的服务器端 TLS 建议 [https://wiki.mozilla.org/Security/Server_Side_TLS]

Mozilla

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

18.3. select — Waiting for I/O 完成

该模块提供了对 select() 和 poll() 函数的访问，这些函数在大多数操作系统中是可用的。在 Solaris 及其衍生版本上可用 devpoll()，在 Linux 2.5+ 上可用 epoll()，在大多数 BSD 上可用 kqueue()。注意，在 Windows 上，本模块仅适用于套接字；在其他操作系统上，本模块也适用于其他文件类型（特别地，在 Unix 上也适用于管道）。本模块不能用于常规文件，不能检测出（自上次读取文件后）文件是否有新数据写入。

注解

selectors 模块是在 select 模块原型的基础上进行高级且高效的 I/O 复用。推荐用户改用 selectors 模块，除非用户希望对 OS 级的函数原型进行精确控制。

该模块定义以下内容：

exception select.error

一个被弃用的 OSError 的别名。

在 3.3 版更改: 根据 PEP 3151 [https://www.python.org/dev/peps/pep-3151]，这个类是 OSError 的别名。

select.devpoll()

（仅支持 Solaris 及其衍生版本）返回一个 /dev/poll 轮询对象，请参阅下方 /dev/poll 轮询对象 获取 devpoll 对象所支持的方法。

devpoll() 对象与实例化时允许的文件描述符数量有关，如果在程序中降低了此数值，devpoll() 调用将失败。如果程序提高了此数值，devpoll() 可能会返回一个不完整的活动文件描述符列表。

新的文件描述符 non-inheritable.

3.3 新版功能.

在 3.4 版更改: 新的文件描述符现在是不可继承的。

select.epoll(sizehint=-1, flags=0)

（仅支持 Linux 2.5.44 或更高版本）返回一个轮询对象，该对象可作为 I/O 事件的边缘触发或水平触发接口。

sizehint 指示 epoll 预计需要注册的事件数。它必须为正数，或为 -1 以使用默认值。它仅在 epoll_create1() 不可用的旧系统上有用，其他情况下它没有任何作用（尽管仍会检查其值）。

flags 已经弃用且完全被忽略。但是，如果提供该值，则它必须是 0 或 select.EPOLL_CLOEXEC，否则会抛出 OSError 异常。

请参阅下方 边缘触发和水平触发的轮询 (epoll) 对象 获取 epoll 对象所支持的方法。

epoll 对象支持上下文管理器：当在 with 语句中使用时，新建的文件描述符会在运行至语句块结束时自动销毁。

新的文件描述符 non-inheritable.

在 3.3 版更改: 增加了 flags 参数。

在 3.4 版更改: 增加了对 with 语句的支持。新的文件描述符现在是不可继承的。

3.4 版后已移除: flags 参数。现在默认采用 select.EPOLL_CLOEXEC 标志。使用 os.set_inheritable() 来让文件描述符可继承。

select.poll()

（部分操作系统不支持）返回一个轮询对象，该对象支持注册和注销文件描述符，支持对描述符进行轮询以获取 I/O 事件。有关轮询对象支持的方法，请参阅下方 Poll 对象 部分。

select.kqueue()

（仅支持 BSD）返回一个内核队列对象，请参阅下方 Kqueue 对象 获取 kqueue 对象所支持的方法。

新的文件描述符 non-inheritable.

在 3.4 版更改: 新的文件描述符现在是不可继承的。

select.kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)

（仅支持 BSD）返回一个内核事件对象，请参阅下方 Kevent 对象 获取 kevent 对象所支持的方法。

select.select(rlist, wlist, xlist[, timeout])

This is a straightforward interface to the Unix select() system call. The first three arguments are sequences of ‘waitable objects’: either integers representing file descriptors or objects with a parameterless method named fileno() returning such an integer:

rlist：等待，直到有内容可以读取

wlist：等待，直到可以开始写入

xlist：等待“异常情况”（请参阅当前系统的手册，以获取哪些情况称为异常情况）

Empty sequences are allowed, but acceptance of three empty sequences is platform-dependent. (It is known to work on Unix but not on Windows.) The optional timeout argument specifies a time-out as a floating point number in seconds. When the timeout argument is omitted the function blocks until at least one file descriptor is ready. A time-out value of zero specifies a poll and never blocks.

返回值是三个列表，包含已就绪对象，返回的三个列表是前三个参数的子集。当超时时间已到且没有对象就绪时，返回三个空列表。

Among the acceptable object types in the sequences are Python file objects (e.g. sys.stdin, or objects returned by open() or os.popen()), socket objects returned by socket.socket(). You may also define a wrapper class yourself, as long as it has an appropriate fileno() method (that really returns a file descriptor, not just a random integer).

注解

Windows 上不接受文件对象，但接受套接字。在 Windows 上，底层的 select() 函数由 WinSock 库提供，且不处理不是源自 WinSock 的文件描述符。

在 3.5 版更改: 现在，当本函数被信号中断时，重试超时将从头开始计时，不会抛出 InterruptedError 异常。除非信号处理程序抛出异常（相关原理请参阅 PEP 475 [https://www.python.org/dev/peps/pep-0475]）。

select.PIPE_BUF

当一个管道已经被 select()、poll() 或本模块中的某个接口报告为可写入时，可以在不阻塞该管道的情况下写入的最小字节数。它不适用于套接字等其他类型的文件类对象。

This value is guaranteed by POSIX to be at least 512. Availability: Unix.

3.2 新版功能.

18.3.1. /dev/poll 轮询对象

Solaris 及其衍生版本具备 /dev/poll。select() 复杂度为 O（最高文件描述符），poll() 为 O（文件描述符数量），而 /dev/poll 为 O（活动的文件描述符）。

/dev/poll 的行为与标准 poll() 对象十分类似。

devpoll.close()

关闭轮询对象的文件描述符。

3.4 新版功能.

devpoll.closed

如果轮询对象已关闭，则返回 True。

3.4 新版功能.

devpoll.fileno()

返回轮询对象的文件描述符对应的数字。

3.4 新版功能.

devpoll.register(fd[, eventmask])

在轮询对象中注册文件描述符。这样，将来调用 poll() 方法时将检查文件描述符是否有未处理的 I/O 事件。fd 可以是整数，也可以是带有 fileno() 方法的对象（该方法返回一个整数）。文件对象已经实现了 fileno()，因此它们也可以用作参数。

eventmask 是可选的位掩码，用于指定要检查的事件类型。这些常量与 poll() 对象所用的相同。本参数的默认值是常量 POLLIN、POLLPRI 和 POLLOUT 的组合。

警告

注册已注册过的文件描述符不会报错，但是结果是不确定的。正确的操作是先注销或直接修改它。与 poll() 相比，这是一个重要的区别。

devpoll.modify(fd[, eventmask])

此方法先执行 unregister() 后执行 register()。直接执行此操作效率（稍微）高一些。

devpoll.unregister(fd)

删除轮询对象正在跟踪的某个文件描述符。与 register() 方法类似，fd 可以是整数，也可以是带有 fileno() 方法的对象（该方法返回一个整数）。

尝试删除从未注册过的文件描述符将被安全地忽略。

devpoll.poll([timeout])

轮询已注册的文件描述符的集合，并返回一个列表，列表可能为空，也可能有多个 (fd, event) 二元组，其中包含了要报告事件或错误的描述符。fd 是文件描述符，event 是一个位掩码，表示该描述符所报告的事件 — POLLIN 表示可以读取，POLLOUT 表示该描述符可以写入，依此类推。空列表表示调用超时，没有任何文件描述符报告事件。如果指定了 timeout，它将指定系统等待事件时，等待多长时间后返回（以毫秒为单位）。如果 timeout 为空，-1 或 None，则本调用将阻塞，直到轮询对象发生事件为止。

在 3.5 版更改: 现在，当本函数被信号中断时，重试超时将从头开始计时，不会抛出 InterruptedError 异常。除非信号处理程序抛出异常（相关原理请参阅 PEP 475 [https://www.python.org/dev/peps/pep-0475]）。

18.3.2. 边缘触发和水平触发的轮询 (epoll) 对象

http://linux.die.net/man/4/epoll

屏蔽事件

常数

含义

EPOLLIN

可读

EPOLLOUT

可写

EPOLLPRI

紧急数据读取

EPOLLERR

在关联的文件描述符上有错误情况发生

EPOLLHUP

关联的文件描述符已挂起

EPOLLET

设置触发方式为边缘触发，默认为水平触发

EPOLLONESHOT

设置 one-shot 模式。触发一次事件后，该描述符会在轮询对象内部被禁用。

EPOLLEXCLUSIVE

当已关联的描述符发生事件时，仅唤醒一个 epoll 对象。默认（如果未设置此标志）是唤醒所有轮询该描述符的 epoll 对象。

EPOLLRDHUP

流套接字的对侧关闭了连接或关闭了写入到一半的连接。

EPOLLRDNORM

Equivalent to EPOLLIN

EPOLLRDBAND

可以读取优先数据带。

EPOLLWRNORM

Equivalent to EPOLLOUT

EPOLLWRBAND

可以写入优先级数据。

EPOLLMSG

忽略

epoll.close()

关闭用于控制 epoll 对象的那个文件描述符。

epoll.closed

如果 epoll 对象已关闭，则返回 True。

epoll.fileno()

返回用于控制 epoll 对象的文件描述符对应的数字。

epoll.fromfd(fd)

根据给定的文件描述符创建 epoll 对象。

epoll.register(fd[, eventmask])

在 epoll 对象中注册一个文件描述符。

epoll.modify(fd, eventmask)

修改一个已注册的文件描述符。

epoll.unregister(fd)

从 epoll 对象中删除一个已注册的文件描述符。

epoll.poll(timeout=-1, maxevents=-1)

等待事件发生，timeout 是浮点数，单位为秒。

在 3.5 版更改: 现在，当本函数被信号中断时，重试超时将从头开始计时，不会抛出 InterruptedError 异常。除非信号处理程序抛出异常（相关原理请参阅 PEP 475 [https://www.python.org/dev/peps/pep-0475]）。

18.3.3. Poll 对象

大多数 Unix 系统支持 poll() 系统调用，为服务器提供了更好的可伸缩性，使服务器可以同时服务于大量客户端。poll() 的伸缩性更好，因为该调用内部仅列出所关注的文件描述符，而 select() 会构造一个 bitmap，在其中将所关注的描述符所对应的 bit 打开，然后重新遍历整个 bitmap。因此 select() 复杂度是 O（最高文件描述符），而 poll() 是 O（文件描述符数量）。

poll.register(fd[, eventmask])

在轮询对象中注册文件描述符。这样，将来调用 poll() 方法时将检查文件描述符是否有未处理的 I/O 事件。fd 可以是整数，也可以是带有 fileno() 方法的对象（该方法返回一个整数）。文件对象已经实现了 fileno()，因此它们也可以用作参数。

eventmask 是可选的位掩码，用于指定要检查的事件类型，它可以是常量 POLLIN、POLLPRI 和 POLLOUT 的组合，如下表所述。如果未指定本参数，默认将会检查所有 3 种类型的事件。

常数

含义

POLLIN

有要读取的数据

POLLPRI

有紧急数据需要读取

POLLOUT

准备输出：写不会阻塞

POLLERR

某种错误条件

POLLHUP

挂起

POLLRDHUP

流套接字对等体关闭连接，或关闭写入一半连接

POLLNVAL

无效的请求：描述符未打开

注册已注册过的文件描述符不会报错，且等同于只注册一次该描述符。

poll.modify(fd, eventmask)

修改一个已注册的文件描述符，等同于 register(fd, eventmask)。尝试修改未注册的文件描述符会抛出 OSError 异常，错误码为 ENOENT。

poll.unregister(fd)

删除轮询对象正在跟踪的某个文件描述符。与 register() 方法类似，fd 可以是整数，也可以是带有 fileno() 方法的对象（该方法返回一个整数）。

尝试删除从未注册过的文件描述符会抛出 KeyError 异常。

poll.poll([timeout])

轮询已注册的文件描述符的集合，并返回一个列表，列表可能为空，也可能有多个 (fd, event) 二元组，其中包含了要报告事件或错误的描述符。fd 是文件描述符，event 是一个位掩码，表示该描述符所报告的事件 — POLLIN 表示可以读取，POLLOUT 表示该描述符可以写入，依此类推。空列表表示调用超时，没有任何文件描述符报告事件。如果指定了 timeout，它将指定系统等待事件时，等待多长时间后返回（以毫秒为单位）。如果 timeout 为空、负数 或 None，则本调用将阻塞，直到轮询对象发生事件为止。

在 3.5 版更改: 现在，当本函数被信号中断时，重试超时将从头开始计时，不会抛出 InterruptedError 异常。除非信号处理程序抛出异常（相关原理请参阅 PEP 475 [https://www.python.org/dev/peps/pep-0475]）。

18.3.4. Kqueue 对象

kqueue.close()

关闭用于控制 kqueue 对象的文件描述符。

kqueue.closed

如果 kqueue 对象已关闭，则返回 True。

kqueue.fileno()

返回用于控制 epoll 对象的文件描述符对应的数字。

kqueue.fromfd(fd)

根据给定的文件描述符创建 kqueue 对象。

kqueue.control(changelist, max_events[, timeout=None]) → eventlist

Kevent 的低级接口

changelist must be an iterable of kevent object or None

max_events 必须是 0 或一个正整数。

timeout in seconds (floats possible)

在 3.5 版更改: 现在，当本函数被信号中断时，重试超时将从头开始计时，不会抛出 InterruptedError 异常。除非信号处理程序抛出异常（相关原理请参阅 PEP 475 [https://www.python.org/dev/peps/pep-0475]）。

18.3.5. Kevent 对象

https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2

kevent.ident

用于区分事件的标识值。其解释取决于筛选器，但该值通常是文件描述符。在构造函数中，该标识值可以是整数或带有 fileno() 方法的对象。kevent 在内部存储整数。

kevent.filter

内核过滤器的名称。

常数

含义

KQ_FILTER_READ

获取描述符，并在有数据可读时返回

KQ_FILTER_WRITE

获取描述符，并在有数据可写时返回

KQ_FILTER_AIO

AIO 请求

KQ_FILTER_VNODE

当在 fflag 中监视的一个或多个请求事件发生时返回

KQ_FILTER_PROC

监视进程ID上的事件

KQ_FILTER_NETDEV

观察网络设备上的事件[在Mac OS X上不可用]

KQ_FILTER_SIGNAL

每当监视的信号传递到进程时返回

KQ_FILTER_TIMER

建立一个任意的计时器

kevent.flags

筛选器操作。

常数

含义

KQ_EV_ADD

添加或修改事件

KQ_EV_DELETE

从队列中删除事件

KQ_EV_ENABLE

Permitscontrol() 返回事件

KQ_EV_DISABLE

禁用事件

KQ_EV_ONESHOT

在第一次发生后删除事件

KQ_EV_CLEAR

检索事件后重置状态

KQ_EV_SYSFLAGS

内部事件

KQ_EV_FLAG1

内部事件

KQ_EV_EOF

筛选特定EOF条件

KQ_EV_ERROR

请参阅返回值

kevent.fflags

筛选特定标志。

KQ_FILTER_READ 和 KQ_FILTER_WRITE 过滤标志：

常数

含义

KQ_NOTE_LOWAT

套接字缓冲区的低水线

KQ_FILTER_VNODE 过滤标志：

常数

含义

KQ_NOTE_DELETE

已调用 unlink()

KQ_NOTE_WRITE

发生写入

KQ_NOTE_EXTEND

文件已扩展

KQ_NOTE_ATTRIB

属性已更改

KQ_NOTE_LINK

链接计数已更改

KQ_NOTE_RENAME

文件已重命名

KQ_NOTE_REVOKE

对文件的访问权限已被撤销

KQ_FILTER_PROC filter flags:

常数

含义

KQ_NOTE_EXIT

进程已退出

KQ_NOTE_FORK

该进程调用了 fork()

KQ_NOTE_EXEC

进程已执行新进程

KQ_NOTE_PCTRLMASK

内部过滤器标志

KQ_NOTE_PDATAMASK

内部过滤器标志

KQ_NOTE_TRACK

跨 fork() 执行进程

KQ_NOTE_CHILD

在 NOTE_TRACK 的子进程上返回

KQ_NOTE_TRACKERR

无法附加到子对象

KQ_FILTER_NETDEV 过滤器标志（在Mac OS X上不可用）：

常数

含义

KQ_NOTE_LINKUP

链接已建立

KQ_NOTE_LINKDOWN

链接已断开

KQ_NOTE_LINKINV

链接状态无效

kevent.data

过滤特定数据。

kevent.udata

用户定义的值。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

18.4. selectors — 高级 I/O 复用库

3.4 新版功能.

源码: Lib/selectors.py [https://github.com/python/cpython/tree/3.6/Lib/selectors.py]

18.4.1. 概述

此模块允许高层级且高效率的 I/O 复用，它建立在 select 模块原型的基础之上。 推荐用户改用此模块，除非他们希望对所使用的 OS 层级原型进行精确控制。

它定义了一个 BaseSelector 抽象基类，以及多个实际的实现 (KqueueSelector, EpollSelector…)，它们可被用于在多个文件对象上等待 I/O 就绪通知。 在下文中，”文件对象” 是指任何具有 fileno() 方法的对象，或是一个原始文件描述器。 参见 file object。

DefaultSelector 是一个指向当前平台上可用的最高效实现的别名：这应为大多数用户的默认选择。

注解

受支持的文件对象类型取决于具体平台：在 Windows 上，支持套接字但不支持管道，而在 Unix 上两者均受支持（某些其他类型也可能受支持，例如 fifo 或特殊文件设备等）。

参见

select

低层级的 I/O 多路复用模块。

18.4.2. 类

类的层次结构:

BaseSelector
+-- SelectSelector
+-- PollSelector
+-- EpollSelector
+-- DevpollSelector
+-- KqueueSelector

下文中，events 一个位掩码，指明哪些 I/O 事件要在给定的文件对象上执行等待。 它可以是以下模块级常量的组合:

常数

含义

EVENT_READ

可读

EVENT_WRITE

可写

class selectors.SelectorKey

SelectorKey 是一个 namedtuple，用来将文件对象关联到其下层的文件描述器、选定事件掩码和附加数据等。 它会被某些 BaseSelector 方法返回。

fileobj

已注册的文件对象。

fd

下层的文件描述器。

events

必须在此文件对象上被等待的事件。

data

可选的关联到此文件对象的不透明数据：例如，这可被用来存储各个客户端的会话 ID。

class selectors.BaseSelector

一个 BaseSelector，用来在多个文件对象上等待 I/O 事件就绪。 它支持文件流注册、注销，以及在这些流上等待 I/O 事件的方法。 它是一个抽象基类，因此不能被实例化。 请改用 DefaultSelector，或者 SelectSelector, KqueueSelector 等。 如果你想要指明使用某个实现，并且你的平台支持它的话。 BaseSelector 及其具体实现支持 context manager 协议。

abstractmethod register(fileobj, events, data=None)

注册一个用于选择的文件对象，在其上监视 I/O 事件。

fileobj 是要监视的文件对象。 它可以是整数形式的文件描述符或者具有 fileno() 方法的对象。 events 是要监视的事件的位掩码。 data 是一个不透明对象。

这将返回一个新的 SelectorKey 实例，或在出现无效事件掩码或文件描述符时引发 ValueError，或在文件对象已被注册时引发 KeyError。

abstractmethod unregister(fileobj)

注销对一个文件对象的选择，移除对它的监视。 在文件对象被关闭之前应当先将其注销。

fileobj 必须是之前已注册的文件对象。

这将返回已关联的 SelectorKey 实例，或者如果 fileobj 未注册则会引发 KeyError。 It will raise ValueError 如果 fileobj 无效（例如它没有 fileno() 方法或其 fileno() 方法返回无效值）。

modify(fileobj, events, data=None)

更改已注册文件对象所监视的事件或所附带的数据。

这等价于 BaseSelector.unregister(fileobj)() 加 BaseSelector.register(fileobj, events, data)()，区别在于它可以被更高效地实现。

这将返回一个新的 SelectorKey 实例，或在出现无效事件掩码或文件描述符时引发 ValueError，或在文件对象未被注册时引发 KeyError。

abstractmethod select(timeout=None)

等待直到有已注册的文件对象就绪，或是超过时限。

如果 timeout > 0，这指定以秒数表示的最大等待时间。 如果 timeout <= 0，调用将不会阻塞，并将报告当前就绪的文件对象。 如果 timeout 为 None，调用将阻塞直到某个被监视的文件对象就绪。

这将返回由 (key, events) 元组构成的列表，每项各表示一个就绪的文件对象。

key 是对应于就绪文件对象的 SelectorKey 实例。 events 是在此文件对象上等待的事件位掩码。

注解

如果当前进程收到一个信号，此方法可在任何文件对象就绪之前或超出时限时返回：在此情况下，将返回一个空列表。

在 3.5 版更改: 现在当被某个信号中断时，如果信号处理程序没有引发异常，选择器会用重新计算的超时值进行重试（请查看 PEP 475 [https://www.python.org/dev/peps/pep-0475] 其理由），而不是在超时之前返回空的事件列表。

close()

关闭选择器。

必须调用这个方法以确保下层资源会被释放。 选择器被关闭后将不可再使用。

get_key(fileobj)

返回关联到某个已注册文件对象的键。

此方法将返回关联到文件对象的 SelectorKey 实例，或在文件对象未注册时引发 KeyError。

abstractmethod get_map()

返回从文件对象到选择器键的映射。

这将返回一个将已注册文件对象映射到与其相关联的 SelectorKey 实例的 Mapping 实例。

class selectors.DefaultSelector

默认的选择器类，使用当前平台上可用的最高效实现。 这应为大多数用户的默认选择。

class selectors.SelectSelector

基于 select.select() 的选择器。

class selectors.PollSelector

基于 select.poll() 的选择器。

class selectors.EpollSelector

基于 select.epoll() 的选择器。

fileno()

此方法将返回由下层 select.epoll() 对象所使用的文件描述符。

class selectors.DevpollSelector

基于 select.devpoll() 的选择器。

fileno()

此方法将返回由下层 select.devpoll() 对象所使用的文件描述符。

3.5 新版功能.

class selectors.KqueueSelector

基于 select.kqueue() 的选择器。

fileno()

此方法将返回由下层 select.kqueue() 对象所使用的文件描述符。

18.4.3. 例子

下面是一个简单的回显服务器实现:

import selectors
import socket

sel = selectors.DefaultSelector()

def accept(sock, mask):
    conn, addr = sock.accept()  # Should be ready
    print('accepted', conn, 'from', addr)
    conn.setblocking(False)
    sel.register(conn, selectors.EVENT_READ, read)

def read(conn, mask):
    data = conn.recv(1000)  # Should be ready
    if data:
        print('echoing', repr(data), 'to', conn)
        conn.send(data)  # Hope it won't block
    else:
        print('closing', conn)
        sel.unregister(conn)
        conn.close()

sock = socket.socket()
sock.bind(('localhost', 1234))
sock.listen(100)
sock.setblocking(False)
sel.register(sock, selectors.EVENT_READ, accept)

while True:
    events = sel.select()
    for key, mask in events:
        callback = key.data
        callback(key.fileobj, mask)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks

3.4 新版功能.

Source code: Lib/asyncio/ [https://github.com/python/cpython/tree/3.6/Lib/asyncio/]

This module provides infrastructure for writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources, running network clients and servers, and other related primitives. Here is a more detailed list of the package contents:

a pluggable event loop with various system-specific implementations;

transport and protocol abstractions (similar to those in Twisted [https://twistedmatrix.com/trac/]);

concrete support for TCP, UDP, SSL, subprocess pipes, delayed calls, and others (some may be system-dependent);

a Future class that mimics the one in the concurrent.futures module, but adapted for use with the event loop;

coroutines and tasks based on yield from (PEP 380 [https://www.python.org/dev/peps/pep-0380]), to help write concurrent code in a sequential fashion;

cancellation support for Futures and coroutines;

synchronization primitives for use between coroutines in a single thread, mimicking those in the threading module;

an interface for passing work off to a threadpool, for times when you absolutely, positively have to use a library that makes blocking I/O calls.

Asynchronous programming is more complex than classical “sequential” programming: see the Develop with asyncio page which lists common traps and explains how to avoid them. Enable the debug mode during development to detect common issues.

Table of contents:

18.5.1. Base Event Loop

18.5.1.1. Run an event loop

18.5.1.2. Calls

18.5.1.3. Delayed calls

18.5.1.4. Futures

18.5.1.5. Tasks

18.5.1.6. Creating connections

18.5.1.7. Creating listening connections

18.5.1.8. Watch file descriptors

18.5.1.9. Low-level socket operations

18.5.1.10. Resolve host name

18.5.1.11. Connect pipes

18.5.1.12. UNIX signals

18.5.1.13. Executor

18.5.1.14. 错误处理API

18.5.1.15. Debug mode

18.5.1.16. Server

18.5.1.17. Handle

18.5.1.18. Event loop examples

18.5.1.18.1. call_soon() 的 Hello World 示例。

18.5.1.18.2. 使用 call_later() 来展示当前的日期

18.5.1.18.3. 监控一个文件描述符的读事件

18.5.1.18.4. 为SIGINT和SIGTERM设置信号处理器

18.5.2. 事件循环

18.5.2.1. 事件循环函数

18.5.2.2. 可用的事件循环

18.5.2.3. 平台支持

18.5.2.3.1. Windows

18.5.2.3.2. Mac OS X

18.5.2.4. 事件循环策略和默认策略

18.5.2.5. 事件循环策略接口

18.5.2.6. 访问全局循环策略

18.5.2.7. 自定义事件循环策略

18.5.3. Tasks and coroutines

18.5.3.1. 协程

18.5.3.1.1. Example: Hello World coroutine

18.5.3.1.2. Example: Coroutine displaying the current date

18.5.3.1.3. Example: Chain coroutines

18.5.3.2. InvalidStateError

18.5.3.3. TimeoutError

18.5.3.4. Future

18.5.3.4.1. Example: Future with run_until_complete()

18.5.3.4.2. Example: Future with run_forever()

18.5.3.5. Task

18.5.3.5.1. Example: Parallel execution of tasks

18.5.3.6. Task functions

18.5.4. Transports and protocols (callback based API)

18.5.4.1. 传输

18.5.4.1.1. BaseTransport

18.5.4.1.2. ReadTransport

18.5.4.1.3. WriteTransport

18.5.4.1.4. DatagramTransport

18.5.4.1.5. BaseSubprocessTransport

18.5.4.2. 协议

18.5.4.2.1. Protocol classes

18.5.4.2.2. Connection callbacks

18.5.4.2.3. Streaming protocols

18.5.4.2.4. Datagram protocols

18.5.4.2.5. Flow control callbacks

18.5.4.2.6. Coroutines and protocols

18.5.4.3. Protocol examples

18.5.4.3.1. TCP echo client protocol

18.5.4.3.2. TCP echo server protocol

18.5.4.3.3. UDP echo client protocol

18.5.4.3.4. UDP echo server protocol

18.5.4.3.5. Register an open socket to wait for data using a protocol

18.5.5. Streams (coroutine based API)

18.5.5.1. Stream functions

18.5.5.2. StreamReader

18.5.5.3. StreamWriter

18.5.5.4. StreamReaderProtocol

18.5.5.5. IncompleteReadError

18.5.5.6. LimitOverrunError

18.5.5.7. Stream examples

18.5.5.7.1. 使用流的 TCP 回显客户端

18.5.5.7.2. 使用流的 TCP 回显服务器

18.5.5.7.3. 获取 HTTP 标头

18.5.5.7.4. 注册一个打开的套接字以等待使用流的数据

18.5.6. Subprocess

18.5.6.1. Windows event loop

18.5.6.2. Create a subprocess: high-level API using Process

18.5.6.3. Create a subprocess: low-level API using subprocess.Popen

18.5.6.4. 常量

18.5.6.5. Process

18.5.6.6. Subprocess and threads

18.5.6.7. Subprocess examples

18.5.6.7.1. Subprocess using transport and protocol

18.5.6.7.2. Subprocess using streams

18.5.7. Synchronization primitives

18.5.7.1. Locks

18.5.7.1.1. Lock

18.5.7.1.2. Event

18.5.7.1.3. Condition

18.5.7.2. Semaphores

18.5.7.2.1. Semaphore

18.5.7.2.2. BoundedSemaphore

18.5.8. 队列集

18.5.8.1. 队列

18.5.8.2. PriorityQueue

18.5.8.3. LifoQueue

18.5.8.3.1. 异常

18.5.9. Develop with asyncio

18.5.9.1. Debug mode of asyncio

18.5.9.2. Cancellation

18.5.9.3. Concurrency and multithreading

18.5.9.4. Handle blocking functions correctly

18.5.9.5. 日志记录

18.5.9.6. Detect coroutine objects never scheduled

18.5.9.7. Detect exceptions never consumed

18.5.9.8. Chain coroutines correctly

18.5.9.9. Pending task destroyed

18.5.9.10. Close transports and event loops

参见

The asyncio module was designed in PEP 3156 [https://www.python.org/dev/peps/pep-3156]. For a motivational primer on transports and protocols, see PEP 3153 [https://www.python.org/dev/peps/pep-3153].

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

18.5.1. Base Event Loop

Source code: Lib/asyncio/events.py [https://github.com/python/cpython/tree/3.6/Lib/asyncio/events.py]

The event loop is the central execution device provided by asyncio. It provides multiple facilities, including:

Registering, executing and cancelling delayed calls (timeouts).

Creating client and server transports for various kinds of communication.

Launching subprocesses and the associated transports for communication with an external program.

Delegating costly function calls to a pool of threads.

class asyncio.BaseEventLoop

This class is an implementation detail. It is a subclass of AbstractEventLoop and may be a base class of concrete event loop implementations found in asyncio. It should not be used directly; use AbstractEventLoop instead. BaseEventLoop should not be subclassed by third-party code; the internal interface is not stable.

class asyncio.AbstractEventLoop

Abstract base class of event loops.

This class is not thread safe.

18.5.1.1. Run an event loop

AbstractEventLoop.run_forever()

Run until stop() is called. If stop() is called before run_forever() is called, this polls the I/O selector once with a timeout of zero, runs all callbacks scheduled in response to I/O events (and those that were already scheduled), and then exits. If stop() is called while run_forever() is running, this will run the current batch of callbacks and then exit. Note that callbacks scheduled by callbacks will not run in that case; they will run the next time run_forever() is called.

在 3.5.1 版更改.

AbstractEventLoop.run_until_complete(future)

Run until the Future is done.

If the argument is a coroutine object, it is wrapped by ensure_future().

Return the Future’s result, or raise its exception.

AbstractEventLoop.is_running()

Returns running status of event loop.

AbstractEventLoop.stop()

Stop running the event loop.

This causes run_forever() to exit at the next suitable opportunity (see there for more details).

在 3.5.1 版更改.

AbstractEventLoop.is_closed()

Returns True if the event loop was closed.

3.4.2 新版功能.

AbstractEventLoop.close()

Close the event loop. The loop must not be running. Pending callbacks will be lost.

This clears the queues and shuts down the executor, but does not wait for the executor to finish.

This is idempotent and irreversible. No other methods should be called after this one.

coroutine AbstractEventLoop.shutdown_asyncgens()

Schedule all currently open asynchronous generator objects to close with an aclose() call. After calling this method, the event loop will issue a warning whenever a new asynchronous generator is iterated. Should be used to finalize all scheduled asynchronous generators reliably. Example:

try:
    loop.run_forever()
finally:
    loop.run_until_complete(loop.shutdown_asyncgens())
    loop.close()

3.6 新版功能.

18.5.1.2. Calls

Most asyncio functions don’t accept keywords. If you want to pass keywords to your callback, use functools.partial(). For example, loop.call_soon(functools.partial(print, "Hello", flush=True)) will call print("Hello", flush=True).

注解

functools.partial() is better than lambda functions, because asyncio can inspect functools.partial() object to display parameters in debug mode, whereas lambda functions have a poor representation.

AbstractEventLoop.call_soon(callback, *args)

Arrange for a callback to be called as soon as possible. The callback is called after call_soon() returns, when control returns to the event loop.

This operates as a FIFO queue, callbacks are called in the order in which they are registered. Each callback will be called exactly once.

Any positional arguments after the callback will be passed to the callback when it is called.

An instance of asyncio.Handle is returned, which can be used to cancel the callback.

Use functools.partial to pass keywords to the callback.

AbstractEventLoop.call_soon_threadsafe(callback, *args)

Like call_soon(), but thread safe.

参见 concurrency and multithreading 部分的文档。

18.5.1.3. Delayed calls

The event loop has its own internal clock for computing timeouts. Which clock is used depends on the (platform-specific) event loop implementation; ideally it is a monotonic clock. This will generally be a different clock than time.time().

注解

Timeouts (relative delay or absolute when) should not exceed one day.

AbstractEventLoop.call_later(delay, callback, *args)

Arrange for the callback to be called after the given delay seconds (either an int or float).

An instance of asyncio.Handle is returned, which can be used to cancel the callback.

callback will be called exactly once per call to call_later(). If two callbacks are scheduled for exactly the same time, it is undefined which will be called first.

The optional positional args will be passed to the callback when it is called. If you want the callback to be called with some named arguments, use a closure or functools.partial().

Use functools.partial to pass keywords to the callback.

AbstractEventLoop.call_at(when, callback, *args)

Arrange for the callback to be called at the given absolute timestamp when (an int or float), using the same time reference as AbstractEventLoop.time().

本方法的行为和 call_later() 方法相同。

An instance of asyncio.Handle is returned, which can be used to cancel the callback.

Use functools.partial to pass keywords to the callback.

AbstractEventLoop.time()

Return the current time, as a float value, according to the event loop’s internal clock.

参见

asyncio.sleep() 函数。

18.5.1.4. Futures

AbstractEventLoop.create_future()

Create an asyncio.Future object attached to the loop.

This is a preferred way to create futures in asyncio, as event loop implementations can provide alternative implementations of the Future class (with better performance or instrumentation).

3.5.2 新版功能.

18.5.1.5. Tasks

AbstractEventLoop.create_task(coro)

Schedule the execution of a coroutine object: wrap it in a future. Return a Task object.

第三方的事件循环可以使用它们自己的 Task 子类来满足互操作性。这种情况下结果类型是一个 Task 的子类。

This method was added in Python 3.4.2. Use the async() function to support also older Python versions.

3.4.2 新版功能.

AbstractEventLoop.set_task_factory(factory)

Set a task factory that will be used by AbstractEventLoop.create_task().

If factory is None the default task factory will be set.

If factory is a callable, it should have a signature matching (loop, coro), where loop will be a reference to the active event loop, coro will be a coroutine object. The callable must return an asyncio.Future compatible object.

3.4.4 新版功能.

AbstractEventLoop.get_task_factory()

Return a task factory, or None if the default one is in use.

3.4.4 新版功能.

18.5.1.6. Creating connections

coroutine AbstractEventLoop.create_connection(protocol_factory, host=None, port=None, *, ssl=None, family=0, proto=0, flags=0, sock=None, local_addr=None, server_hostname=None)

Create a streaming transport connection to a given Internet host and port: socket family AF_INET or AF_INET6 depending on host (or family if specified), socket type SOCK_STREAM. protocol_factory must be a callable returning a protocol instance.

This method is a coroutine which will try to establish the connection in the background. When successful, the coroutine returns a (transport, protocol) pair.

底层操作的大致的执行顺序是这样的：

The connection is established, and a transport is created to represent it.

protocol_factory is called without arguments and must return a protocol instance.

The protocol instance is tied to the transport, and its connection_made() method is called.

The coroutine returns successfully with the (transport, protocol) pair.

被创建的传输对象是一个实现相关的双向流。

注解

protocol_factory can be any kind of callable, not necessarily a class. For example, if you want to use a pre-created protocol instance, you can pass lambda: my_protocol.

Options that change how the connection is created:

ssl: if given and not false, a SSL/TLS transport is created (by default a plain TCP transport is created). If ssl is a ssl.SSLContext object, this context is used to create the transport; if ssl is True, a context with some unspecified default settings is used.

参见

SSL/TLS security considerations

server_hostname, is only for use together with ssl, and sets or overrides the hostname that the target server’s certificate will be matched against. By default the value of the host argument is used. If host is empty, there is no default and you must pass a value for server_hostname. If server_hostname is an empty string, hostname matching is disabled (which is a serious security risk, allowing for man-in-the-middle-attacks).

family, proto, flags 是可选的地址族、协议和标志，它们会被传递给 getaddrinfo() 来对 host 进行解析。如果要指定的话，这些都应该是来自于 socket 模块的对应常量。

sock，如果指定的话，其应该是一个已经存在，并且已经处于连接状态的 socket.socket 对象，其会被传输对象使用。如果指定了 sock ，那么 host, port, family, proto, flags 和 local_addr 就都不应该被指定了。

local_addr, if given, is a (local_host, local_port) tuple used to bind the socket to locally. The local_host and local_port are looked up using getaddrinfo(), similarly to host and port.

在 3.5 版更改: On Windows with ProactorEventLoop, SSL/TLS is now supported.

参见

The open_connection() function can be used to get a pair of (StreamReader, StreamWriter) instead of a protocol.

coroutine AbstractEventLoop.create_datagram_endpoint(protocol_factory, local_addr=None, remote_addr=None, *, family=0, proto=0, flags=0, reuse_address=None, reuse_port=None, allow_broadcast=None, sock=None)

注解

形参 reuse_address 已不再受支持，因为使用 SO_REUSEADDR 会对 UDP 造成显著的安全问题。 显式地传入 reuse_address=True 将会引发异常。

当具有不同 UID 的多个进程将套接字赋给具有 SO_REUSEADDR 的相同 UDP 套接字地址时，传入的数据包可能会在套接字间随机分配。

对于受支持的平台，reuse_port 可以被用作类似功能的替代。 通过 reuse_port 将改用 SO_REUSEPORT，它能够防止具有不同 UID 的进程将套接字赋给相同的套接字地址。

创建一个数据报连接。

Create datagram connection: socket family AF_INET or AF_INET6 depending on host (or family if specified), socket type SOCK_DGRAM. protocol_factory must be a callable returning a protocol instance.

This method is a coroutine which will try to establish the connection in the background. When successful, the coroutine returns a (transport, protocol) pair.

Options changing how the connection is created:

local_addr，如果指定的话，就是一个 (local_host, local_port) 元组，用于在本地绑定套接字。 local_host 和 local_port 是使用 getaddrinfo() 来查找的。

remote_addr，如果指定的话，就是一个 (remote_host, remote_port) 元组，用于同一个远程地址连接。remote_host 和 remote_port 是使用 getaddrinfo() 来查找的。

family, proto, flags 是可选的地址族，协议和标志，其会被传递给 getaddrinfo() 来完成 host 的解析。如果要指定的话，这些都应该是来自于 socket 模块的对应常量。

reuse_port tells the kernel to allow this endpoint to be bound to the same port as other existing endpoints are bound to, so long as they all set this flag when being created. This option is not supported on Windows and some UNIX’s. If the SO_REUSEPORT constant is not defined then this capability is unsupported.

allow_broadcast 告知内核允许此端点向广播地址发送消息。

sock 可选择通过指定此值用于使用一个预先存在的，已经处于连接状态的 socket.socket 对象，并将其提供给此传输对象使用。如果指定了这个值， local_addr 和 remote_addr 就应该被忽略 (必须为 None)。

On Windows with ProactorEventLoop, this method is not supported.

参见 UDP echo 客户端协议 和 UDP echo 服务端协议 的例子。

在 3.4.4 版更改: 添加了 family, proto, flags, reuse_address, reuse_port, allow_broadcast 和 sock 等参数。

在 3.6.10 版更改: The reuse_address parameter is no longer supporter due to security concerns

coroutine AbstractEventLoop.create_unix_connection(protocol_factory, path, *, ssl=None, sock=None, server_hostname=None)

Create UNIX connection: socket family AF_UNIX, socket type SOCK_STREAM. The AF_UNIX socket family is used to communicate between processes on the same machine efficiently.

This method is a coroutine which will try to establish the connection in the background. When successful, the coroutine returns a (transport, protocol) pair.

path is the name of a UNIX domain socket, and is required unless a sock parameter is specified. Abstract UNIX sockets, str, and bytes paths are supported.

See the AbstractEventLoop.create_connection() method for parameters.

Availability: UNIX.

18.5.1.7. Creating listening connections

coroutine AbstractEventLoop.create_server(protocol_factory, host=None, port=None, *, family=socket.AF_UNSPEC, flags=socket.AI_PASSIVE, sock=None, backlog=100, ssl=None, reuse_address=None, reuse_port=None)

Create a TCP server (socket type SOCK_STREAM) bound to host and port.

Return a Server object, its sockets attribute contains created sockets. Use the Server.close() method to stop the server: close listening sockets.

参数：

The host parameter can be a string, in that case the TCP server is bound to host and port. The host parameter can also be a sequence of strings and in that case the TCP server is bound to all hosts of the sequence. If host is an empty string or None, all interfaces are assumed and a list of multiple sockets will be returned (most likely one for IPv4 and another one for IPv6).

family can be set to either socket.AF_INET or AF_INET6 to force the socket to use IPv4 or IPv6. If not set it will be determined from host (defaults to socket.AF_UNSPEC).

flags 是用于 getaddrinfo() 的位掩码。

sock can optionally be specified in order to use a preexisting socket object. If specified, host and port should be omitted (must be None).

backlog 是传递给 listen() 的最大排队连接的数量（默认为100）。

ssl 可被设置为一个 SSLContext 以在接受的连接上启用 SSL。

reuse_address tells the kernel to reuse a local socket in TIME_WAIT state, without waiting for its natural timeout to expire. If not specified will automatically be set to True on UNIX.

reuse_port 告知内核，只要在创建的时候都设置了这个标志，就允许此端点绑定到其它端点列表所绑定的同样的端口上。这个选项在 Windows 上是不支持的。

This method is a coroutine.

在 3.5 版更改: On Windows with ProactorEventLoop, SSL/TLS is now supported.

参见

The function start_server() creates a (StreamReader, StreamWriter) pair and calls back a function with this pair.

在 3.5.1 版更改: The host parameter can now be a sequence of strings.

coroutine AbstractEventLoop.create_unix_server(protocol_factory, path=None, *, sock=None, backlog=100, ssl=None)

Similar to AbstractEventLoop.create_server(), but specific to the socket family AF_UNIX.

This method is a coroutine.

Availability: UNIX.

coroutine BaseEventLoop.connect_accepted_socket(protocol_factory, sock, *, ssl=None)

Handle an accepted connection.

This is used by servers that accept connections outside of asyncio but that use asyncio to handle them.

参数：

sock is a preexisting socket object returned from an accept call.

ssl 可被设置为一个 SSLContext 以在接受的连接上启用 SSL。

This method is a coroutine. When completed, the coroutine returns a (transport, protocol) pair.

3.5.3 新版功能.

18.5.1.8. Watch file descriptors

On Windows with SelectorEventLoop, only socket handles are supported (ex: pipe file descriptors are not supported).

On Windows with ProactorEventLoop, these methods are not supported.

AbstractEventLoop.add_reader(fd, callback, *args)

Start watching the file descriptor for read availability and then call the callback with specified arguments.

Use functools.partial to pass keywords to the callback.

AbstractEventLoop.remove_reader(fd)

Stop watching the file descriptor for read availability.

AbstractEventLoop.add_writer(fd, callback, *args)

Start watching the file descriptor for write availability and then call the callback with specified arguments.

Use functools.partial to pass keywords to the callback.

AbstractEventLoop.remove_writer(fd)

Stop watching the file descriptor for write availability.

The watch a file descriptor for read events example uses the low-level AbstractEventLoop.add_reader() method to register the file descriptor of a socket.

18.5.1.9. Low-level socket operations

coroutine AbstractEventLoop.sock_recv(sock, nbytes)

Receive data from the socket. Modeled after blocking socket.socket.recv() method.

The return value is a bytes object representing the data received. The maximum amount of data to be received at once is specified by nbytes.

With SelectorEventLoop event loop, the socket sock must be non-blocking.

This method is a coroutine.

coroutine AbstractEventLoop.sock_sendall(sock, data)

Send data to the socket. Modeled after blocking socket.socket.sendall() method.

The socket must be connected to a remote socket. This method continues to send data from data until either all data has been sent or an error occurs. None is returned on success. On error, an exception is raised, and there is no way to determine how much data, if any, was successfully processed by the receiving end of the connection.

With SelectorEventLoop event loop, the socket sock must be non-blocking.

This method is a coroutine.

coroutine AbstractEventLoop.sock_connect(sock, address)

Connect to a remote socket at address. Modeled after blocking socket.socket.connect() method.

With SelectorEventLoop event loop, the socket sock must be non-blocking.

This method is a coroutine.

在 3.5.2 版更改: address no longer needs to be resolved. sock_connect will try to check if the address is already resolved by calling socket.inet_pton(). If not, AbstractEventLoop.getaddrinfo() will be used to resolve the address.

参见

AbstractEventLoop.create_connection() and asyncio.open_connection().

coroutine AbstractEventLoop.sock_accept(sock)

Accept a connection. Modeled after blocking socket.socket.accept().

此 scoket 必须绑定到一个地址上并且监听连接。返回值是一个 (conn, address) 对，其中 conn 是一个 新*的套接字对象，用于在此连接上收发数据，*address 是连接的另一端的套接字所绑定的地址。

The socket sock must be non-blocking.

This method is a coroutine.

参见

AbstractEventLoop.create_server() and start_server().

18.5.1.10. Resolve host name

coroutine AbstractEventLoop.getaddrinfo(host, port, *, family=0, type=0, proto=0, flags=0)

This method is a coroutine, similar to socket.getaddrinfo() function but non-blocking.

coroutine AbstractEventLoop.getnameinfo(sockaddr, flags=0)

This method is a coroutine, similar to socket.getnameinfo() function but non-blocking.

18.5.1.11. Connect pipes

On Windows with SelectorEventLoop, these methods are not supported. Use ProactorEventLoop to support pipes on Windows.

coroutine AbstractEventLoop.connect_read_pipe(protocol_factory, pipe)

Register read pipe in eventloop.

protocol_factory should instantiate object with Protocol interface. pipe is a file-like object. Return pair (transport, protocol), where transport supports the ReadTransport interface.

使用 SelectorEventLoop 事件循环， pipe 被设置为非阻塞模式。

This method is a coroutine.

coroutine AbstractEventLoop.connect_write_pipe(protocol_factory, pipe)

Register write pipe in eventloop.

protocol_factory should instantiate object with BaseProtocol interface. pipe is file-like object. Return pair (transport, protocol), where transport supports WriteTransport interface.

使用 SelectorEventLoop 事件循环， pipe 被设置为非阻塞模式。

This method is a coroutine.

参见

The AbstractEventLoop.subprocess_exec() and AbstractEventLoop.subprocess_shell() methods.

18.5.1.12. UNIX signals

Availability: UNIX only.

AbstractEventLoop.add_signal_handler(signum, callback, *args)

Add a handler for a signal.

如果信号数字非法或者不可捕获，就抛出一个 ValueError 。如果建立处理器的过程中出现问题，会抛出一个 RuntimeError 。

Use functools.partial to pass keywords to the callback.

AbstractEventLoop.remove_signal_handler(sig)

Remove a handler for a signal.

Return True if a signal handler was removed, False if not.

参见

signal 模块。

18.5.1.13. Executor

Call a function in an Executor (pool of threads or pool of processes). By default, an event loop uses a thread pool executor (ThreadPoolExecutor).

coroutine AbstractEventLoop.run_in_executor(executor, func, *args)

Arrange for a func to be called in the specified executor.

The executor argument should be an Executor instance. The default executor is used if executor is None.

Use functools.partial to pass keywords to the *func*.

This method is a coroutine.

在 3.5.3 版更改: BaseEventLoop.run_in_executor() no longer configures the max_workers of the thread pool executor it creates, instead leaving it up to the thread pool executor (ThreadPoolExecutor) to set the default.

AbstractEventLoop.set_default_executor(executor)

Set the default executor used by run_in_executor().

18.5.1.14. 错误处理API

允许自定义事件循环中如何去处理异常。

AbstractEventLoop.set_exception_handler(handler)

将 handler 设置为新的事件循环异常处理器。

If handler is None, the default exception handler will be set.

If handler is a callable object, it should have a matching signature to (loop, context), where loop will be a reference to the active event loop, context will be a dict object (see call_exception_handler() documentation for details about context).

AbstractEventLoop.get_exception_handler()

Return the exception handler, or None if the default one is in use.

3.5.2 新版功能.

AbstractEventLoop.default_exception_handler(context)

默认的异常处理器。

This is called when an exception occurs and no exception handler is set, and can be called by a custom exception handler that wants to defer to the default behavior.

context 参数和 call_exception_handler() 中的同名参数完全相同。

AbstractEventLoop.call_exception_handler(context)

调用当前事件循环的异常处理器。

context is a dict object containing the following keys (new keys may be introduced later):

‘message’: 错误消息；

‘exception’ （可选）: 异常对象；

‘future’ （可选）: asyncio.Future 实例；

‘handle’ （可选）: asyncio.Handle 实例；

‘protocol’ （可选）: Protocol 实例；

‘transport’ （可选）: Transport 实例；

‘socket’ （可选）： socket.socket 实例。

注解

Note: this method should not be overloaded in subclassed event loops. For any custom exception handling, use set_exception_handler() method.

18.5.1.15. Debug mode

AbstractEventLoop.get_debug()

获取事件循环调试模式设置（bool）。

如果环境变量 PYTHONASYNCIODEBUG 是一个非空字符串，就返回 True ，否则就返回 False 。

3.4.2 新版功能.

AbstractEventLoop.set_debug(enabled: bool)

设置事件循环的调试模式。

3.4.2 新版功能.

参见

debug mode of asyncio.

18.5.1.16. Server

class asyncio.Server

Server listening on sockets.

Object created by the AbstractEventLoop.create_server() method and the start_server() function. Don’t instantiate the class directly.

close()

停止服务：关闭监听的套接字并且设置 sockets 属性为 None 。

用于表示已经连进来的客户端连接会保持打开的状态。

服务器是被异步关闭的，使用 wait_closed() 协程来等待服务器关闭。

coroutine wait_closed()

等待 close() 方法执行完毕。

This method is a coroutine.

sockets

List of socket.socket objects the server is listening to, or None if the server is closed.

18.5.1.17. Handle

class asyncio.Handle

A callback wrapper object returned by AbstractEventLoop.call_soon(), AbstractEventLoop.call_soon_threadsafe(), AbstractEventLoop.call_later(), and AbstractEventLoop.call_at().

cancel()

Cancel the call. If the callback is already canceled or executed, this method has no effect.

18.5.1.18. Event loop examples

18.5.1.18.1. call_soon() 的 Hello World 示例。

Example using the AbstractEventLoop.call_soon() method to schedule a callback. The callback displays "Hello World" and then stops the event loop:

import asyncio

def hello_world(loop):
    print('Hello World')
    loop.stop()

loop = asyncio.get_event_loop()

# Schedule a call to hello_world()
loop.call_soon(hello_world, loop)

# Blocking call interrupted by loop.stop()
loop.run_forever()
loop.close()

参见

The Hello World coroutine example uses a coroutine.

18.5.1.18.2. 使用 call_later() 来展示当前的日期

Example of callback displaying the current date every second. The callback uses the AbstractEventLoop.call_later() method to reschedule itself during 5 seconds, and then stops the event loop:

import asyncio
import datetime

def display_date(end_time, loop):
    print(datetime.datetime.now())
    if (loop.time() + 1.0) < end_time:
        loop.call_later(1, display_date, end_time, loop)
    else:
        loop.stop()

loop = asyncio.get_event_loop()

# Schedule the first call to display_date()
end_time = loop.time() + 5.0
loop.call_soon(display_date, end_time, loop)

# Blocking call interrupted by loop.stop()
loop.run_forever()
loop.close()

参见

The coroutine displaying the current date example uses a coroutine.

18.5.1.18.3. 监控一个文件描述符的读事件

Wait until a file descriptor received some data using the AbstractEventLoop.add_reader() method and then close the event loop:

import asyncio
try:
    from socket import socketpair
except ImportError:
    from asyncio.windows_utils import socketpair

# Create a pair of connected file descriptors
rsock, wsock = socketpair()
loop = asyncio.get_event_loop()

def reader():
    data = rsock.recv(100)
    print("Received:", data.decode())
    # We are done: unregister the file descriptor
    loop.remove_reader(rsock)
    # Stop the event loop
    loop.stop()

# Register the file descriptor for read event
loop.add_reader(rsock, reader)

# Simulate the reception of data from the network
loop.call_soon(wsock.send, 'abc'.encode())

# Run the event loop
loop.run_forever()

# We are done, close sockets and the event loop
rsock.close()
wsock.close()
loop.close()

参见

The register an open socket to wait for data using a protocol example uses a low-level protocol created by the AbstractEventLoop.create_connection() method.

The register an open socket to wait for data using streams example uses high-level streams created by the open_connection() function in a coroutine.

18.5.1.18.4. 为SIGINT和SIGTERM设置信号处理器

Register handlers for signals SIGINT and SIGTERM using the AbstractEventLoop.add_signal_handler() method:

import asyncio
import functools
import os
import signal

def ask_exit(signame):
    print("got signal %s: exit" % signame)
    loop.stop()

loop = asyncio.get_event_loop()
for signame in ('SIGINT', 'SIGTERM'):
    loop.add_signal_handler(getattr(signal, signame),
                            functools.partial(ask_exit, signame))

print("Event loop running forever, press Ctrl+C to interrupt.")
print("pid %s: send SIGINT or SIGTERM to exit." % os.getpid())
try:
    loop.run_forever()
finally:
    loop.close()

This example only works on UNIX.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

18.5.2. 事件循环

源码: Lib/asyncio/events.py [https://github.com/python/cpython/tree/3.6/Lib/asyncio/events.py]

18.5.2.1. 事件循环函数

下面的函数是访问全局策略方法的便利快捷方式。注意这只是提供了对默认策略的访问，除非要在进程开始之前设置替代的策略。

asyncio.get_event_loop()

与调用 get_event_loop_policy().get_event_loop() 等价。

asyncio.set_event_loop(loop)

等价于调用 get_event_loop_policy().set_event_loop(loop)。

asyncio.new_event_loop()

等价于调用 get_event_loop_policy().new_event_loop()。

18.5.2.2. 可用的事件循环

asyncio当前提供两种事件循环实现： SelectorEventLoop 和 ProactorEventLoop 。

class asyncio.SelectorEventLoop

基于 selectors 模块的事件循环。 AbstractEventLoop 的子类。

使用对应平台上最高效的选择器。

在Windows上，只支持套接字（举例：不支持管线）：参见 MSDN关于select的文档 <https://msdn.microsoft.com/en-us/library/windows/desktop/ms740141%28v=vs.85%29.aspx> _ 。

class asyncio.ProactorEventLoop

Windows上的 Proactor 事件循环，使用的是 “I/O 完成端口”，也叫做 IOCP。为 AbstractEventLoop 的子类。

可用性：Windows。

参见

MSDN上关于 I/O完成端口的文档 [https://msdn.microsoft.com/en-us/library/windows/desktop/aa365198%28v=vs.85%29.aspx] 。

在Windows上使用 ProactorEventLoop 的例子:

import asyncio, sys

if sys.platform == 'win32':
    loop = asyncio.ProactorEventLoop()
    asyncio.set_event_loop(loop)

18.5.2.3. 平台支持

asyncio 模块被设计为可移植的，但是每个平台仍旧有细微的不同，而且不一定支持 asyncio 的所有特性。

18.5.2.3.1. Windows

Windows事件循环的一般限制：

不支持 create_unix_connection() 和 create_unix_server()：socket.AF_UNIX 套接字族只在UNIX上可用

不支持 add_signal_handler() 和 remove_signal_handler()

不支持 EventLoopPolicy.set_child_watcher() 。ProactorEventLoop 支持子进程。监控子进程的实现只有一种，不需要对其进行设置。

特定于 SelectorEventLoop 的限制：

SelectSelector 只被用于支持套接字，并且最多支持512个套接字。

add_reader() 和 add_writer() 只接受套接字的文件描述符

不支持管线（示例：connect_read_pipe(), connect_write_pipe()）

不支持 Subprocesses （示例: subprocess_exec(), subprocess_shell()）

特定于 ProactorEventLoop 的限制：

不支持 create_datagram_endpoint() (UDP)

不支持 add_reader() 和 add_writer()

Windows上单调时钟的分辨率大约为 15.6 毫秒。最佳的分辨率是 0.5 毫秒。分辨率依赖于具体的硬件（HPET [https://en.wikipedia.org/wiki/High_Precision_Event_Timer] 的可用性）和Windows的设置。参见:ref:asyncio delayed calls <asyncio-delayed-calls> 。

在 3.5 版更改: ProactorEventLoop 现在支持SSL了。

18.5.2.3.2. Mac OS X

类似于PTY的字符设备在Mavericks （Mac OS 10.9）之前并未有很好的支持。在 Mac OS 10.5以及更老的版本上则根本不支持。

在Mac OS 10.6、10.7和10.8上，默认的事件循环是 SelectorEventLoop ，其使用了 selectors.KqueueSelector 。 selectors.KqueueSelector 在这些版本上并不支持字符设备。SelectorEventLoop 可以同 SelectSelector 或者 PollSelector 一同使用来在这些版本的Mac OS X上支持块设备。 例如:

import asyncio
import selectors

selector = selectors.SelectSelector()
loop = asyncio.SelectorEventLoop(selector)
asyncio.set_event_loop(loop)

18.5.2.4. 事件循环策略和默认策略

使用一个 策略 模式对事件循环进行管理，用以为特定平台和框架提供最大限度的灵活性。在一个进程的执行中，唯一的全局策略对象，基于调用的上下文，管理着此进程所有可用的事件循环。一个策略就是一个实现了 AbstractEventLoopPolicy 的对象。

对于 asyncio 的多数使用者来说，从来都不需要显式地处理策略，因为默认的全局策略就已经足够了（看下面）。

模块级别的函数 get_event_loop() 和 set_event_loop() 对于默认策略管理的事件循环的便利访问方式。

18.5.2.5. 事件循环策略接口

一个事件循环必须实现如下的接口：

class asyncio.AbstractEventLoopPolicy

事件循环策略。

get_event_loop()

为当前上下文获取事件循环。

返回一个实现了 AbstractEventLoop 接口的事件循环对象。如果是从一个协程中调用，就会返回当前正在运行的事件循环。

如果没有为当前上下文设置一个事件循环，并且当前策略不能创建一个事件循环，就会抛出一个错误。其务必不能返回 None 。

在 3.6 版更改.

set_event_loop(loop)

将当前上下文的事件循环设置为 loop 。

new_event_loop()

根据策略的规则，创建并返回一个新的事件循环对象。

如果有需要设置这个循环作为当前上下文的事件循环，就必须显式调用 set_event_loop()  。

The default policy defines context as the current thread, and manages an event loop per thread that interacts with asyncio. If the current thread doesn’t already have an event loop associated with it, the default policy’s get_event_loop() method creates one when called from the main thread, but raises RuntimeError otherwise.

18.5.2.6. 访问全局循环策略

asyncio.get_event_loop_policy()

获取当前事件循环策略。

asyncio.set_event_loop_policy(policy)

设置当前事件循环策略。 如果 policy 为 None ，就会恢复使用默认的策略。

18.5.2.7. 自定义事件循环策略

要实现一个新的事件循环策略，推荐你继承具体的默认事件循环策略 DefaultEventLoopPolicy 并且重写你想要改变行为的方法，例如:

class MyEventLoopPolicy(asyncio.DefaultEventLoopPolicy):

    def get_event_loop(self):
        """Get the event loop.

        This may be None or an instance of EventLoop.
        """
        loop = super().get_event_loop()
        # Do something with loop ...
        return loop

asyncio.set_event_loop_policy(MyEventLoopPolicy())

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

18.5.3. Tasks and coroutines

Source code: Lib/asyncio/tasks.py [https://github.com/python/cpython/tree/3.6/Lib/asyncio/tasks.py]

Source code: Lib/asyncio/coroutines.py [https://github.com/python/cpython/tree/3.6/Lib/asyncio/coroutines.py]

18.5.3.1. 协程

Coroutines used with asyncio may be implemented using the async def statement, or by using generators. The async def type of coroutine was added in Python 3.5, and is recommended if there is no need to support older Python versions.

Generator-based coroutines should be decorated with @asyncio.coroutine, although this is not strictly enforced. The decorator enables compatibility with async def coroutines, and also serves as documentation. Generator-based coroutines use the yield from syntax introduced in PEP 380 [https://www.python.org/dev/peps/pep-0380], instead of the original yield syntax.

The word “coroutine”, like the word “generator”, is used for two different (though related) concepts:

The function that defines a coroutine (a function definition using async def or decorated with @asyncio.coroutine). If disambiguation is needed we will call this a coroutine function (iscoroutinefunction() returns True).

The object obtained by calling a coroutine function. This object represents a computation or an I/O operation (usually a combination) that will complete eventually. If disambiguation is needed we will call it a coroutine object (iscoroutine() returns True).

Things a coroutine can do:

result = await future or result = yield from future – suspends the coroutine until the future is done, then returns the future’s result, or raises an exception, which will be propagated. (If the future is cancelled, it will raise a CancelledError exception.) Note that tasks are futures, and everything said about futures also applies to tasks.

result = await coroutine or result = yield from coroutine – wait for another coroutine to produce a result (or raise an exception, which will be propagated). The coroutine expression must be a call to another coroutine.

return expression – produce a result to the coroutine that is waiting for this one using await or yield from.

raise exception – raise an exception in the coroutine that is waiting for this one using await or yield from.

Calling a coroutine does not start its code running – the coroutine object returned by the call doesn’t do anything until you schedule its execution. There are two basic ways to start it running: call await coroutine or yield from coroutine from another coroutine (assuming the other coroutine is already running!), or schedule its execution using the ensure_future() function or the AbstractEventLoop.create_task() method.

Coroutines (and tasks) can only run when the event loop is running.

@asyncio.coroutine

Decorator to mark generator-based coroutines. This enables the generator use yield from to call async def coroutines, and also enables the generator to be called by async def coroutines, for instance using an await expression.

There is no need to decorate async def coroutines themselves.

If the generator is not yielded from before it is destroyed, an error message is logged. See Detect coroutines never scheduled.

注解

In this documentation, some methods are documented as coroutines, even if they are plain Python functions returning a Future. This is intentional to have a freedom of tweaking the implementation of these functions in the future. If such a function is needed to be used in a callback-style code, wrap its result with ensure_future().

18.5.3.1.1. Example: Hello World coroutine

Example of coroutine displaying "Hello World":

import asyncio

async def hello_world():
    print("Hello World!")

loop = asyncio.get_event_loop()
# Blocking call which returns when the hello_world() coroutine is done
loop.run_until_complete(hello_world())
loop.close()

参见

The Hello World with call_soon() example uses the AbstractEventLoop.call_soon() method to schedule a callback.

18.5.3.1.2. Example: Coroutine displaying the current date

Example of coroutine displaying the current date every second during 5 seconds using the sleep() function:

import asyncio
import datetime

async def display_date(loop):
    end_time = loop.time() + 5.0
    while True:
        print(datetime.datetime.now())
        if (loop.time() + 1.0) >= end_time:
            break
        await asyncio.sleep(1)

loop = asyncio.get_event_loop()
# Blocking call which returns when the display_date() coroutine is done
loop.run_until_complete(display_date(loop))
loop.close()

参见

The display the current date with call_later() example uses a callback with the AbstractEventLoop.call_later() method.

18.5.3.1.3. Example: Chain coroutines

Example chaining coroutines:

import asyncio

async def compute(x, y):
    print("Compute %s + %s ..." % (x, y))
    await asyncio.sleep(1.0)
    return x + y

async def print_sum(x, y):
    result = await compute(x, y)
    print("%s + %s = %s" % (x, y, result))

loop = asyncio.get_event_loop()
loop.run_until_complete(print_sum(1, 2))
loop.close()

compute() is chained to print_sum(): print_sum() coroutine waits until compute() is completed before returning its result.

Sequence diagram of the example:

The “Task” is created by the AbstractEventLoop.run_until_complete() method when it gets a coroutine object instead of a task.

The diagram shows the control flow, it does not describe exactly how things work internally. For example, the sleep coroutine creates an internal future which uses AbstractEventLoop.call_later() to wake up the task in 1 second.

18.5.3.2. InvalidStateError

exception asyncio.InvalidStateError

The operation is not allowed in this state.

18.5.3.3. TimeoutError

exception asyncio.TimeoutError

The operation exceeded the given deadline.

注解

This exception is different from the builtin TimeoutError exception!

18.5.3.4. Future

class asyncio.Future(*, loop=None)

This class is almost compatible with concurrent.futures.Future.

Differences:

result() and exception() do not take a timeout argument and raise an exception when the future isn’t done yet.

Callbacks registered with add_done_callback() are always called via the event loop’s call_soon().

This class is not compatible with the wait() and as_completed() functions in the concurrent.futures package.

This class is not thread safe.

cancel()

Cancel the future and schedule callbacks.

If the future is already done or cancelled, return False. Otherwise, change the future’s state to cancelled, schedule the callbacks and return True.

cancelled()

Return True if the future was cancelled.

done()

Return True if the future is done.

Done means either that a result / exception are available, or that the future was cancelled.

result()

Return the result this future represents.

If the future has been cancelled, raises CancelledError. If the future’s result isn’t yet available, raises InvalidStateError. If the future is done and has an exception set, this exception is raised.

exception()

Return the exception that was set on this future.

The exception (or None if no exception was set) is returned only if the future is done. If the future has been cancelled, raises CancelledError. If the future isn’t done yet, raises InvalidStateError.

add_done_callback(fn)

Add a callback to be run when the future becomes done.

The callback is called with a single argument - the future object. If the future is already done when this is called, the callback is scheduled with call_soon().

Use functools.partial to pass parameters to the callback. For example, fut.add_done_callback(functools.partial(print, "Future:", flush=True)) will call print("Future:", fut, flush=True).

remove_done_callback(fn)

Remove all instances of a callback from the “call when done” list.

Returns the number of callbacks removed.

set_result(result)

Mark the future done and set its result.

If the future is already done when this method is called, raises InvalidStateError.

set_exception(exception)

Mark the future done and set an exception.

If the future is already done when this method is called, raises InvalidStateError.

18.5.3.4.1. Example: Future with run_until_complete()

Example combining a Future and a coroutine function:

import asyncio

async def slow_operation(future):
    await asyncio.sleep(1)
    future.set_result('Future is done!')

loop = asyncio.get_event_loop()
future = asyncio.Future()
asyncio.ensure_future(slow_operation(future))
loop.run_until_complete(future)
print(future.result())
loop.close()

The coroutine function is responsible for the computation (which takes 1 second) and it stores the result into the future. The run_until_complete() method waits for the completion of the future.

注解

The run_until_complete() method uses internally the add_done_callback() method to be notified when the future is done.

18.5.3.4.2. Example: Future with run_forever()

The previous example can be written differently using the Future.add_done_callback() method to describe explicitly the control flow:

import asyncio

async def slow_operation(future):
    await asyncio.sleep(1)
    future.set_result('Future is done!')

def got_result(future):
    print(future.result())
    loop.stop()

loop = asyncio.get_event_loop()
future = asyncio.Future()
asyncio.ensure_future(slow_operation(future))
future.add_done_callback(got_result)
try:
    loop.run_forever()
finally:
    loop.close()

In this example, the future is used to link slow_operation() to got_result(): when slow_operation() is done, got_result() is called with the result.

18.5.3.5. Task

class asyncio.Task(coro, *, loop=None)

Schedule the execution of a coroutine: wrap it in a future. A task is a subclass of Future.

A task is responsible for executing a coroutine object in an event loop. If the wrapped coroutine yields from a future, the task suspends the execution of the wrapped coroutine and waits for the completion of the future. When the future is done, the execution of the wrapped coroutine restarts with the result or the exception of the future.

Event loops use cooperative scheduling: an event loop only runs one task at a time. Other tasks may run in parallel if other event loops are running in different threads. While a task waits for the completion of a future, the event loop executes a new task.

The cancellation of a task is different from the cancelation of a future. Calling cancel() will throw a CancelledError to the wrapped coroutine. cancelled() only returns True if the wrapped coroutine did not catch the CancelledError exception, or raised a CancelledError exception.

If a pending task is destroyed, the execution of its wrapped coroutine did not complete. It is probably a bug and a warning is logged: see Pending task destroyed.

Don’t directly create Task instances: use the ensure_future() function or the AbstractEventLoop.create_task() method.

This class is not thread safe.

classmethod all_tasks(loop=None)

返回一个事件循环中所有任务的集合。

By default all tasks for the current event loop are returned.

classmethod current_task(loop=None)

Return the currently running task in an event loop or None.

By default the current task for the current event loop is returned.

None is returned when called not in the context of a Task.

cancel()

Request that this task cancel itself.

This arranges for a CancelledError to be thrown into the wrapped coroutine on the next cycle through the event loop. The coroutine then has a chance to clean up or even deny the request using try/except/finally.

Unlike Future.cancel(), this does not guarantee that the task will be cancelled: the exception might be caught and acted upon, delaying cancellation of the task or preventing cancellation completely. The task may also return a value or raise a different exception.

Immediately after this method is called, cancelled() will not return True (unless the task was already cancelled). A task will be marked as cancelled when the wrapped coroutine terminates with a CancelledError exception (even if cancel() was not called).

get_stack(*, limit=None)

Return the list of stack frames for this task’s coroutine.

If the coroutine is not done, this returns the stack where it is suspended. If the coroutine has completed successfully or was cancelled, this returns an empty list. If the coroutine was terminated by an exception, this returns the list of traceback frames.

框架总是从按从旧到新排序。

The optional limit gives the maximum number of frames to return; by default all available frames are returned. Its meaning differs depending on whether a stack or a traceback is returned: the newest frames of a stack are returned, but the oldest frames of a traceback are returned. (This matches the behavior of the traceback module.)

For reasons beyond our control, only one stack frame is returned for a suspended coroutine.

print_stack(*, limit=None, file=None)

Print the stack or traceback for this task’s coroutine.

This produces output similar to that of the traceback module, for the frames retrieved by get_stack(). The limit argument is passed to get_stack(). The file argument is an I/O stream to which the output is written; by default output is written to sys.stderr.

18.5.3.5.1. Example: Parallel execution of tasks

Example executing 3 tasks (A, B, C) in parallel:

import asyncio

async def factorial(name, number):
    f = 1
    for i in range(2, number+1):
        print("Task %s: Compute factorial(%s)..." % (name, i))
        await asyncio.sleep(1)
        f *= i
    print("Task %s: factorial(%s) = %s" % (name, number, f))

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(
    factorial("A", 2),
    factorial("B", 3),
    factorial("C", 4),
))
loop.close()

Output:

Task A: Compute factorial(2)...
Task B: Compute factorial(2)...
Task C: Compute factorial(2)...
Task A: factorial(2) = 2
Task B: Compute factorial(3)...
Task C: Compute factorial(3)...
Task B: factorial(3) = 6
Task C: Compute factorial(4)...
Task C: factorial(4) = 24

A task is automatically scheduled for execution when it is created. The event loop stops when all tasks are done.

18.5.3.6. Task functions

注解

In the functions below, the optional loop argument allows explicitly setting the event loop object used by the underlying task or coroutine. If it’s not provided, the default event loop is used.

asyncio.as_completed(fs, *, loop=None, timeout=None)

Return an iterator whose values, when waited for, are Future instances.

如果在所有 Future 对象完成前发生超时则将引发 asyncio.TimeoutError。

示例:

for f in as_completed(fs):
    result = yield from f  # The 'yield from' may raise
    # Use result

注解

The futures f are not necessarily members of fs.

asyncio.ensure_future(coro_or_future, *, loop=None)

Schedule the execution of a coroutine object: wrap it in a future. Return a Task object.

If the argument is a Future, it is returned directly.

3.4.4 新版功能.

在 3.5.1 版更改: The function accepts any awaitable object.

参见

The AbstractEventLoop.create_task() method.

asyncio.async(coro_or_future, *, loop=None)

A deprecated alias to ensure_future().

3.4.4 版后已移除.

asyncio.wrap_future(future, *, loop=None)

Wrap a concurrent.futures.Future object in a Future object.

asyncio.gather(*coros_or_futures, loop=None, return_exceptions=False)

Return a future aggregating results from the given coroutine objects or futures.

All futures must share the same event loop. If all the tasks are done successfully, the returned future’s result is the list of results (in the order of the original sequence, not necessarily the order of results arrival). If return_exceptions is true, exceptions in the tasks are treated the same as successful results, and gathered in the result list; otherwise, the first raised exception will be immediately propagated to the returned future.

Cancellation: if the outer Future is cancelled, all children (that have not completed yet) are also cancelled. If any child is cancelled, this is treated as if it raised CancelledError – the outer Future is not cancelled in this case. (This is to prevent the cancellation of one child to cause other children to be cancelled.)

在 3.6.6 版更改: 如果 gather 本身被取消，则无论 return_exceptions 取值为何，消息都会被传播。

asyncio.iscoroutine(obj)

Return True if obj is a coroutine object, which may be based on a generator or an async def coroutine.

asyncio.iscoroutinefunction(func)

Return True if func is determined to be a coroutine function, which may be a decorated generator function or an async def function.

asyncio.run_coroutine_threadsafe(coro, loop)

Submit a coroutine object to a given event loop.

Return a concurrent.futures.Future to access the result.

This function is meant to be called from a different thread than the one where the event loop is running. Usage:

# Create a coroutine
coro = asyncio.sleep(1, result=3)
# Submit the coroutine to a given loop
future = asyncio.run_coroutine_threadsafe(coro, loop)
# Wait for the result with an optional timeout argument
assert future.result(timeout) == 3

If an exception is raised in the coroutine, the returned future will be notified. It can also be used to cancel the task in the event loop:

try:
    result = future.result(timeout)
except asyncio.TimeoutError:
    print('The coroutine took too long, cancelling the task...')
    future.cancel()
except Exception as exc:
    print('The coroutine raised an exception: {!r}'.format(exc))
else:
    print('The coroutine returned: {!r}'.format(result))

参见 concurrency and multithreading 部分的文档。

注解

Unlike other functions from the module, run_coroutine_threadsafe() requires the loop argument to be passed explicitly.

3.5.1 新版功能.

coroutine asyncio.sleep(delay, result=None, *, loop=None)

Create a coroutine that completes after a given time (in seconds). If result is provided, it is produced to the caller when the coroutine completes.

The resolution of the sleep depends on the granularity of the event loop.

This function is a coroutine.

coroutine asyncio.shield(arg, *, loop=None)

Wait for a future, shielding it from cancellation.

以下语句:

res = yield from shield(something())

is exactly equivalent to the statement:

res = yield from something()

except that if the coroutine containing it is cancelled, the task running in something() is not cancelled. From the point of view of something(), the cancellation did not happen. But its caller is still cancelled, so the yield-from expression still raises CancelledError. Note: If something() is cancelled by other means this will still cancel shield().

If you want to completely ignore cancellation (not recommended) you can combine shield() with a try/except clause, as follows:

try:
    res = yield from shield(something())
except CancelledError:
    res = None

coroutine asyncio.wait(futures, *, loop=None, timeout=None, return_when=ALL_COMPLETED)

Wait for the Futures and coroutine objects given by the sequence futures to complete. Coroutines will be wrapped in Tasks. Returns two sets of Future: (done, pending).

The sequence futures must not be empty.

timeout can be used to control the maximum number of seconds to wait before returning. timeout can be an int or float. If timeout is not specified or None, there is no limit to the wait time.

return_when indicates when this function should return. It must be one of the following constants of the concurrent.futures module:

常数

描述

FIRST_COMPLETED

函数将在任意可等待对象结束或取消时返回。

FIRST_EXCEPTION

函数将在任意可等待对象因引发异常而结束时返回。当没有引发任何异常时它就相当于 ALL_COMPLETED。

ALL_COMPLETED

函数将在所有可等待对象结束或取消时返回。

This function is a coroutine.

用法:

done, pending = yield from asyncio.wait(fs)

注解

This does not raise asyncio.TimeoutError! Futures that aren’t done when the timeout occurs are returned in the second set.

coroutine asyncio.wait_for(fut, timeout, *, loop=None)

Wait for the single Future or coroutine object to complete with timeout. If timeout is None, block until the future completes.

Coroutine will be wrapped in Task.

Returns result of the Future or coroutine. When a timeout occurs, it cancels the task and raises asyncio.TimeoutError. To avoid the task cancellation, wrap it in shield().

If the wait is cancelled, the future fut is also cancelled.

This function is a coroutine, usage:

result = yield from asyncio.wait_for(fut, 60.0)

在 3.4.3 版更改: If the wait is cancelled, the future fut is now also cancelled.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

18.5.4. Transports and protocols (callback based API)

Source code: Lib/asyncio/transports.py [https://github.com/python/cpython/tree/3.6/Lib/asyncio/transports.py]

Source code: Lib/asyncio/protocols.py [https://github.com/python/cpython/tree/3.6/Lib/asyncio/protocols.py]

18.5.4.1. 传输

Transports are classes provided by asyncio in order to abstract various kinds of communication channels. You generally won’t instantiate a transport yourself; instead, you will call an AbstractEventLoop method which will create the transport and try to initiate the underlying communication channel, calling you back when it succeeds.

Once the communication channel is established, a transport is always paired with a protocol instance. The protocol can then call the transport’s methods for various purposes.

asyncio currently implements transports for TCP, UDP, SSL, and subprocess pipes. The methods available on a transport depend on the transport’s kind.

传输类属于 线程不安全 。

在 3.6 版更改: The socket option TCP_NODELAY is now set by default.

18.5.4.1.1. BaseTransport

class asyncio.BaseTransport

Base class for transports.

close()

Close the transport. If the transport has a buffer for outgoing data, buffered data will be flushed asynchronously. No more data will be received. After all buffered data is flushed, the protocol’s connection_lost() method will be called with None as its argument.

is_closing()

返回 True ，如果传输正在关闭或已经关闭。

3.5.1 新版功能.

get_extra_info(name, default=None)

Return optional transport information. name is a string representing the piece of transport-specific information to get, default is the value to return if the information doesn’t exist.

This method allows transport implementations to easily expose channel-specific information.

套接字:

'peername': 套接字链接时的远端地址，socket.socket.getpeername() 方法的结果 (出错时为 None )

'socket': socket.socket 实例

'sockname': 套接字本地地址， socket.socket.getsockname() 方法的结果

SSL套接字

'compression': 用字符串指定压缩算法，或者链接没有压缩时为 None ；ssl.SSLSocket.compression() 的结果。

'cipher': 一个三值的元组，包含使用密码的名称，定义使用的SSL协议的版本，使用的加密位数。 ssl.SSLSocket.cipher() 的结果。

'peercert': 远端凭证； ssl.SSLSocket.getpeercert() 结果。

'sslcontext': ssl.SSLContext 实例

'ssl_object': ssl.SSLObject 或 ssl.SSLSocket 实例

管道：

'pipe': 管道对象

子进程：

'subprocess': subprocess.Popen 实例

set_protocol(protocol)

Set a new protocol. Switching protocol should only be done when both protocols are documented to support the switch.

3.5.3 新版功能.

get_protocol()

返回当前协议。

3.5.3 新版功能.

在 3.5.1 版更改: 'ssl_object' info was added to SSL sockets.

18.5.4.1.2. ReadTransport

class asyncio.ReadTransport

Interface for read-only transports.

pause_reading()

Pause the receiving end of the transport. No data will be passed to the protocol’s data_received() method until resume_reading() is called.

在 3.6.7 版更改: 这个方法幂等的， 它可以在传输已经暂停或关闭时调用。

resume_reading()

Resume the receiving end. The protocol’s data_received() method will be called once again if some data is available for reading.

在 3.6.7 版更改: 这个方法幂等的， 它可以在传输已经准备好读取数据时调用。

18.5.4.1.3. WriteTransport

class asyncio.WriteTransport

Interface for write-only transports.

abort()

Close the transport immediately, without waiting for pending operations to complete. Buffered data will be lost. No more data will be received. The protocol’s connection_lost() method will eventually be called with None as its argument.

can_write_eof()

Return True if the transport supports write_eof(), False if not.

get_write_buffer_size()

返回传输使用输出缓冲区的当前大小。

get_write_buffer_limits()

Get the high- and low-water limits for write flow control. Return a tuple (low, high) where low and high are positive number of bytes.

使用 set_write_buffer_limits() 设置限制。

3.4.2 新版功能.

set_write_buffer_limits(high=None, low=None)

Set the high- and low-water limits for write flow control.

These two values (measured in number of bytes) control when the protocol’s pause_writing() and resume_writing() methods are called. If specified, the low-water limit must be less than or equal to the high-water limit. Neither high nor low can be negative.

pause_writing() is called when the buffer size becomes greater than or equal to the high value. If writing has been paused, resume_writing() is called when the buffer size becomes less than or equal to the low value.

The defaults are implementation-specific. If only the high-water limit is given, the low-water limit defaults to an implementation-specific value less than or equal to the high-water limit. Setting high to zero forces low to zero as well, and causes pause_writing() to be called whenever the buffer becomes non-empty. Setting low to zero causes resume_writing() to be called only once the buffer is empty. Use of zero for either limit is generally sub-optimal as it reduces opportunities for doing I/O and computation concurrently.

Use get_write_buffer_limits() to get the limits.

write(data)

将一些 data 字节串写入传输。

此方法不会阻塞；它会缓冲数据并安排其被异步地发出。

writelines(list_of_data)

将数据字节串的列表（或任意可迭代对象）写入传输。 这在功能上等价于在可迭代对象产生的每个元素上调用 write()，但其实现可能更为高效。

write_eof()

Close the write end of the transport after flushing buffered data. Data may still be received.

This method can raise NotImplementedError if the transport (e.g. SSL) doesn’t support half-closes.

18.5.4.1.4. DatagramTransport

DatagramTransport.sendto(data, addr=None)

将 data 字节串发送到 addr (基于传输的目标地址) 所给定的远端对等方。 如果 addr 为 None，则将数据发送到传输创建时给定的目标地址。

此方法不会阻塞；它会缓冲数据并安排其被异步地发出。

DatagramTransport.abort()

Close the transport immediately, without waiting for pending operations to complete. Buffered data will be lost. No more data will be received. The protocol’s connection_lost() method will eventually be called with None as its argument.

18.5.4.1.5. BaseSubprocessTransport

class asyncio.BaseSubprocessTransport

get_pid()

将子进程的进程 ID 以整数形式返回。

get_pipe_transport(fd)

返回对应于整数文件描述符 fd 的通信管道的传输:

0: 标准输入 (stdin) 的可读流式传输，如果子进程创建时未设置 stdin=PIPE 则为 None。

1: 标准输出writable streaming transport of the standard output (stdout) 的可写流式传输，如果子进程创建时未设置 stdout=PIPE 则为 None。

2: 标准错误 (stderr) 的可写流式传输，如果子进程创建时未设置 stderr=PIPE 则为 None

其他 fd: None

get_returncode()

Return the subprocess returncode as an integer or None if it hasn’t returned, similarly to the subprocess.Popen.returncode attribute.

kill()

Kill the subprocess, as in subprocess.Popen.kill().

在 POSIX 系统中，函数会发送 SIGKILL 到子进程。 在 Windows 中，此方法是 terminate() 的别名。

send_signal(signal)

发送 signal 编号到子进程，与 subprocess.Popen.send_signal() 一样。

terminate()

Ask the subprocess to stop, as in subprocess.Popen.terminate(). This method is an alias for the close() method.

在 POSIX 系统中，此方法会发送 SIGTERM 到子进程。 在 Windows 中，则会调用 Windows API 函数 TerminateProcess() 来停止子进程。

close()

Ask the subprocess to stop by calling the terminate() method if the subprocess hasn’t returned yet, and close transports of all pipes (stdin, stdout and stderr).

18.5.4.2. 协议

asyncio provides base classes that you can subclass to implement your network protocols. Those classes are used in conjunction with transports (see below): the protocol parses incoming data and asks for the writing of outgoing data, while the transport is responsible for the actual I/O and buffering.

When subclassing a protocol class, it is recommended you override certain methods. Those methods are callbacks: they will be called by the transport on certain events (for example when some data is received); you shouldn’t call them yourself, unless you are implementing a transport.

注解

All callbacks have default implementations, which are empty. Therefore, you only need to implement the callbacks for the events in which you are interested.

18.5.4.2.1. Protocol classes

class asyncio.Protocol

The base class for implementing streaming protocols (for use with e.g. TCP and SSL transports).

class asyncio.DatagramProtocol

The base class for implementing datagram protocols (for use with e.g. UDP transports).

class asyncio.SubprocessProtocol

The base class for implementing protocols communicating with child processes (through a set of unidirectional pipes).

18.5.4.2.2. Connection callbacks

These callbacks may be called on Protocol, DatagramProtocol and SubprocessProtocol instances:

BaseProtocol.connection_made(transport)

链接建立时被调用。

The transport argument is the transport representing the connection. You are responsible for storing it somewhere (e.g. as an attribute) if you need to.

BaseProtocol.connection_lost(exc)

链接丢失或关闭时被调用。

方法的参数是一个异常对象或为 None。 后者意味着收到了常规的 EOF，或者连接被连接的一端取消或关闭。

connection_made() and connection_lost() are called exactly once per successful connection. All other callbacks will be called between those two methods, which allows for easier resource management in your protocol implementation.

The following callbacks may be called only on SubprocessProtocol instances:

SubprocessProtocol.pipe_data_received(fd, data)

Called when the child process writes data into its stdout or stderr pipe. fd is the integer file descriptor of the pipe. data is a non-empty bytes object containing the data.

SubprocessProtocol.pipe_connection_lost(fd, exc)

Called when one of the pipes communicating with the child process is closed. fd is the integer file descriptor that was closed.

SubprocessProtocol.process_exited()

子进程退出时被调用。

18.5.4.2.3. Streaming protocols

The following callbacks are called on Protocol instances:

Protocol.data_received(data)

当收到数据时被调用。 data 为包含入站数据的非空字节串对象。

注解

Whether the data is buffered, chunked or reassembled depends on the transport. In general, you shouldn’t rely on specific semantics and instead make your parsing generic and flexible enough. However, data is always received in the correct order.

Protocol.eof_received()

Called when the other end signals it won’t send any more data (for example by calling write_eof(), if the other end also uses asyncio).

This method may return a false value (including None), in which case the transport will close itself. Conversely, if this method returns a true value, closing the transport is up to the protocol. Since the default implementation returns None, it implicitly closes the connection.

注解

Some transports such as SSL don’t support half-closed connections, in which case returning true from this method will not prevent closing the connection.

data_received() can be called an arbitrary number of times during a connection. However, eof_received() is called at most once and, if called, data_received() won’t be called after it.

状态机：

start -> connection_made() [-> data_received() *] [-> eof_received() ?] -> connection_lost() -> end

18.5.4.2.4. Datagram protocols

The following callbacks are called on DatagramProtocol instances.

DatagramProtocol.datagram_received(data, addr)

当接收到数据报时被调用。 data 是包含传入数据的字节串对象。 addr 是发送数据的对等端地址；实际的格式取决于具体传输。

DatagramProtocol.error_received(exc)

当前一个发送或接收操作引发 OSError 时被调用。 exc 是 OSError 的实例。

This method is called in rare conditions, when the transport (e.g. UDP) detects that a datagram couldn’t be delivered to its recipient. In many conditions though, undeliverable datagrams will be silently dropped.

18.5.4.2.5. Flow control callbacks

These callbacks may be called on Protocol, DatagramProtocol and SubprocessProtocol instances:

BaseProtocol.pause_writing()

Called when the transport’s buffer goes over the high-water mark.

BaseProtocol.resume_writing()

Called when the transport’s buffer drains below the low-water mark.

pause_writing() and resume_writing() calls are paired – pause_writing() is called once when the buffer goes strictly over the high-water mark (even if subsequent writes increases the buffer size even more), and eventually resume_writing() is called once when the buffer size reaches the low-water mark.

注解

If the buffer size equals the high-water mark, pause_writing() is not called – it must go strictly over. Conversely, resume_writing() is called when the buffer size is equal or lower than the low-water mark. These end conditions are important to ensure that things go as expected when either mark is zero.

注解

On BSD systems (OS X, FreeBSD, etc.) flow control is not supported for DatagramProtocol, because send failures caused by writing too many packets cannot be detected easily. The socket always appears ‘ready’ and excess packets are dropped; an OSError with errno set to errno.ENOBUFS may or may not be raised; if it is raised, it will be reported to DatagramProtocol.error_received() but otherwise ignored.

18.5.4.2.6. Coroutines and protocols

Coroutines can be scheduled in a protocol method using ensure_future(), but there is no guarantee made about the execution order. Protocols are not aware of coroutines created in protocol methods and so will not wait for them.

To have a reliable execution order, use stream objects in a coroutine with yield from. For example, the StreamWriter.drain() coroutine can be used to wait until the write buffer is flushed.

18.5.4.3. Protocol examples

18.5.4.3.1. TCP echo client protocol

TCP echo client using the AbstractEventLoop.create_connection() method, send data and wait until the connection is closed:

import asyncio

class EchoClientProtocol(asyncio.Protocol):
    def __init__(self, message, loop):
        self.message = message
        self.loop = loop

    def connection_made(self, transport):
        transport.write(self.message.encode())
        print('Data sent: {!r}'.format(self.message))

    def data_received(self, data):
        print('Data received: {!r}'.format(data.decode()))

    def connection_lost(self, exc):
        print('The server closed the connection')
        print('Stop the event loop')
        self.loop.stop()

loop = asyncio.get_event_loop()
message = 'Hello World!'
coro = loop.create_connection(lambda: EchoClientProtocol(message, loop),
                              '127.0.0.1', 8888)
loop.run_until_complete(coro)
loop.run_forever()
loop.close()

The event loop is running twice. The run_until_complete() method is preferred in this short example to raise an exception if the server is not listening, instead of having to write a short coroutine to handle the exception and stop the running loop. At run_until_complete() exit, the loop is no longer running, so there is no need to stop the loop in case of an error.

参见

The TCP echo client using streams example uses the asyncio.open_connection() function.

18.5.4.3.2. TCP echo server protocol

TCP echo server using the AbstractEventLoop.create_server() method, send back received data and close the connection:

import asyncio

class EchoServerClientProtocol(asyncio.Protocol):
    def connection_made(self, transport):
        peername = transport.get_extra_info('peername')
        print('Connection from {}'.format(peername))
        self.transport = transport

    def data_received(self, data):
        message = data.decode()
        print('Data received: {!r}'.format(message))

        print('Send: {!r}'.format(message))
        self.transport.write(data)

        print('Close the client socket')
        self.transport.close()

loop = asyncio.get_event_loop()
# Each client connection will create a new protocol instance
coro = loop.create_server(EchoServerClientProtocol, '127.0.0.1', 8888)
server = loop.run_until_complete(coro)

# Serve requests until Ctrl+C is pressed
print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
    loop.run_forever()
except KeyboardInterrupt:
    pass

# Close the server
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()

Transport.close() can be called immediately after WriteTransport.write() even if data are not sent yet on the socket: both methods are asynchronous. yield from is not needed because these transport methods are not coroutines.

参见

The TCP echo server using streams example uses the asyncio.start_server() function.

18.5.4.3.3. UDP echo client protocol

UDP echo client using the AbstractEventLoop.create_datagram_endpoint() method, send data and close the transport when we received the answer:

import asyncio

class EchoClientProtocol:
    def __init__(self, message, loop):
        self.message = message
        self.loop = loop
        self.transport = None

    def connection_made(self, transport):
        self.transport = transport
        print('Send:', self.message)
        self.transport.sendto(self.message.encode())

    def datagram_received(self, data, addr):
        print("Received:", data.decode())

        print("Close the socket")
        self.transport.close()

    def error_received(self, exc):
        print('Error received:', exc)

    def connection_lost(self, exc):
        print("Socket closed, stop the event loop")
        loop = asyncio.get_event_loop()
        loop.stop()

loop = asyncio.get_event_loop()
message = "Hello World!"
connect = loop.create_datagram_endpoint(
    lambda: EchoClientProtocol(message, loop),
    remote_addr=('127.0.0.1', 9999))
transport, protocol = loop.run_until_complete(connect)
loop.run_forever()
transport.close()
loop.close()

18.5.4.3.4. UDP echo server protocol

UDP echo server using the AbstractEventLoop.create_datagram_endpoint() method, send back received data:

import asyncio

class EchoServerProtocol:
    def connection_made(self, transport):
        self.transport = transport

    def datagram_received(self, data, addr):
        message = data.decode()
        print('Received %r from %s' % (message, addr))
        print('Send %r to %s' % (message, addr))
        self.transport.sendto(data, addr)

loop = asyncio.get_event_loop()
print("Starting UDP server")
# One protocol instance will be created to serve all client requests
listen = loop.create_datagram_endpoint(
    EchoServerProtocol, local_addr=('127.0.0.1', 9999))
transport, protocol = loop.run_until_complete(listen)

try:
    loop.run_forever()
except KeyboardInterrupt:
    pass

transport.close()
loop.close()

18.5.4.3.5. Register an open socket to wait for data using a protocol

Wait until a socket receives data using the AbstractEventLoop.create_connection() method with a protocol, and then close the event loop

import asyncio
try:
    from socket import socketpair
except ImportError:
    from asyncio.windows_utils import socketpair

# Create a pair of connected sockets
rsock, wsock = socketpair()
loop = asyncio.get_event_loop()

class MyProtocol(asyncio.Protocol):
    transport = None

    def connection_made(self, transport):
        self.transport = transport

    def data_received(self, data):
        print("Received:", data.decode())

        # We are done: close the transport (it will call connection_lost())
        self.transport.close()

    def connection_lost(self, exc):
        # The socket has been closed, stop the event loop
        loop.stop()

# Register the socket to wait for data
connect_coro = loop.create_connection(MyProtocol, sock=rsock)
transport, protocol = loop.run_until_complete(connect_coro)

# Simulate the reception of data from the network
loop.call_soon(wsock.send, 'abc'.encode())

# Run the event loop
loop.run_forever()

# We are done, close sockets and the event loop
rsock.close()
wsock.close()
loop.close()

参见

The watch a file descriptor for read events example uses the low-level AbstractEventLoop.add_reader() method to register the file descriptor of a socket.

The register an open socket to wait for data using streams example uses high-level streams created by the open_connection() function in a coroutine.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

18.5.5. Streams (coroutine based API)

Source code: Lib/asyncio/streams.py [https://github.com/python/cpython/tree/3.6/Lib/asyncio/streams.py]

18.5.5.1. Stream functions

注解

The top-level functions in this module are meant as convenience wrappers only; there’s really nothing special there, and if they don’t do exactly what you want, feel free to copy their code.

coroutine asyncio.open_connection(host=None, port=None, *, loop=None, limit=None, **kwds)

A wrapper for create_connection() returning a (reader, writer) pair.

The reader returned is a StreamReader instance; the writer is a StreamWriter instance.

The arguments are all the usual arguments to AbstractEventLoop.create_connection() except protocol_factory; most common are positional host and port, with various optional keyword arguments following.

Additional optional keyword arguments are loop (to set the event loop instance to use) and limit (to set the buffer limit passed to the StreamReader).

This function is a coroutine.

coroutine asyncio.start_server(client_connected_cb, host=None, port=None, *, loop=None, limit=None, **kwds)

Start a socket server, with a callback for each client connected. The return value is the same as create_server().

The client_connected_cb parameter is called with two parameters: client_reader, client_writer. client_reader is a StreamReader object, while client_writer is a StreamWriter object. The client_connected_cb parameter can either be a plain callback function or a coroutine function; if it is a coroutine function, it will be automatically converted into a Task.

The rest of the arguments are all the usual arguments to create_server() except protocol_factory; most common are positional host and port, with various optional keyword arguments following.

Additional optional keyword arguments are loop (to set the event loop instance to use) and limit (to set the buffer limit passed to the StreamReader).

This function is a coroutine.

coroutine asyncio.open_unix_connection(path=None, *, loop=None, limit=None, **kwds)

A wrapper for create_unix_connection() returning a (reader, writer) pair.

See open_connection() for information about return value and other details.

This function is a coroutine.

Availability: UNIX.

coroutine asyncio.start_unix_server(client_connected_cb, path=None, *, loop=None, limit=None, **kwds)

Start a UNIX Domain Socket server, with a callback for each client connected.

See start_server() for information about return value and other details.

This function is a coroutine.

Availability: UNIX.

18.5.5.2. StreamReader

class asyncio.StreamReader(limit=_DEFAULT_LIMIT, loop=None)

This class is not thread safe.

The limit argument’s default value is set to _DEFAULT_LIMIT which is 2**16 (64 KiB)

exception()

Get the exception.

feed_eof()

Acknowledge the EOF.

feed_data(data)

Feed data bytes in the internal buffer. Any operations waiting for the data will be resumed.

set_exception(exc)

Set the exception.

set_transport(transport)

Set the transport.

coroutine read(n=-1)

读取 n 个byte. 如果没有设置 n , 则自动置为 -1 ，读至 EOF 并返回所有读取的byte。

If the EOF was received and the internal buffer is empty, return an empty bytes object.

This method is a coroutine.

coroutine readline()

读取一行，其中“行”指的是以 \n 结尾的字节序列。

If EOF is received, and \n was not found, the method will return the partial read bytes.

If the EOF was received and the internal buffer is empty, return an empty bytes object.

This method is a coroutine.

coroutine readexactly(n)

Read exactly n bytes. Raise an IncompleteReadError if the end of the stream is reached before n can be read, the IncompleteReadError.partial attribute of the exception contains the partial read bytes.

This method is a coroutine.

coroutine readuntil(separator=b'\n')

Read data from the stream until separator is found.

成功后，数据和指定的separator将从内部缓冲区中删除(或者说被消费掉)。返回的数据将包括在末尾的指定separator。

Configured stream limit is used to check result. Limit sets the maximal length of data that can be returned, not counting the separator.

If an EOF occurs and the complete separator is still not found, an IncompleteReadError exception will be raised, and the internal buffer will be reset. The IncompleteReadError.partial attribute may contain the separator partially.

If the data cannot be read because of over limit, a LimitOverrunError exception will be raised, and the data will be left in the internal buffer, so it can be read again.

3.5.2 新版功能.

at_eof()

如果缓冲区为空并且 feed_eof() 被调用，则返回 True 。

18.5.5.3. StreamWriter

class asyncio.StreamWriter(transport, protocol, reader, loop)

Wraps a Transport.

This exposes write(), writelines(), can_write_eof(), write_eof(), get_extra_info() and close(). It adds drain() which returns an optional Future on which you can wait for flow control. It also adds a transport attribute which references the Transport directly.

This class is not thread safe.

transport

Transport.

can_write_eof()

Return True if the transport supports write_eof(), False if not. See WriteTransport.can_write_eof().

close()

Close the transport: see BaseTransport.close().

coroutine drain()

Let the write buffer of the underlying transport a chance to be flushed.

The intended use is to write:

w.write(data)
yield from w.drain()

When the size of the transport buffer reaches the high-water limit (the protocol is paused), block until the size of the buffer is drained down to the low-water limit and the protocol is resumed. When there is nothing to wait for, the yield-from continues immediately.

Yielding from drain() gives the opportunity for the loop to schedule the write operation and flush the buffer. It should especially be used when a possibly large amount of data is written to the transport, and the coroutine does not yield-from between calls to write().

This method is a coroutine.

get_extra_info(name, default=None)

Return optional transport information: see BaseTransport.get_extra_info().

write(data)

Write some data bytes to the transport: see WriteTransport.write().

writelines(data)

Write a list (or any iterable) of data bytes to the transport: see WriteTransport.writelines().

write_eof()

Close the write end of the transport after flushing buffered data: see WriteTransport.write_eof().

18.5.5.4. StreamReaderProtocol

class asyncio.StreamReaderProtocol(stream_reader, client_connected_cb=None, loop=None)

Trivial helper class to adapt between Protocol and StreamReader. Subclass of Protocol.

stream_reader is a StreamReader instance, client_connected_cb is an optional function called with (stream_reader, stream_writer) when a connection is made, loop is the event loop instance to use.

(This is a helper class instead of making StreamReader itself a Protocol subclass, because the StreamReader has other potential uses, and to prevent the user of the StreamReader from accidentally calling inappropriate methods of the protocol.)

18.5.5.5. IncompleteReadError

exception asyncio.IncompleteReadError

Incomplete read error, subclass of EOFError.

expected

Total number of expected bytes (int).

partial

Read bytes string before the end of stream was reached (bytes).

18.5.5.6. LimitOverrunError

exception asyncio.LimitOverrunError

Reached the buffer limit while looking for a separator.

consumed

Total number of to be consumed bytes.

18.5.5.7. Stream examples

18.5.5.7.1. 使用流的 TCP 回显客户端

使用 asyncio.open_connection() 函数的 TCP 回显客户端:

import asyncio

@asyncio.coroutine
def tcp_echo_client(message, loop):
    reader, writer = yield from asyncio.open_connection('127.0.0.1', 8888,
                                                        loop=loop)

    print('Send: %r' % message)
    writer.write(message.encode())

    data = yield from reader.read(100)
    print('Received: %r' % data.decode())

    print('Close the socket')
    writer.close()

message = 'Hello World!'
loop = asyncio.get_event_loop()
loop.run_until_complete(tcp_echo_client(message, loop))
loop.close()

参见

The TCP echo client protocol example uses the AbstractEventLoop.create_connection() method.

18.5.5.7.2. 使用流的 TCP 回显服务器

TCP 回显服务器使用 asyncio.start_server() 函数:

import asyncio

@asyncio.coroutine
def handle_echo(reader, writer):
    data = yield from reader.read(100)
    message = data.decode()
    addr = writer.get_extra_info('peername')
    print("Received %r from %r" % (message, addr))

    print("Send: %r" % message)
    writer.write(data)
    yield from writer.drain()

    print("Close the client socket")
    writer.close()

loop = asyncio.get_event_loop()
coro = asyncio.start_server(handle_echo, '127.0.0.1', 8888, loop=loop)
server = loop.run_until_complete(coro)

# Serve requests until Ctrl+C is pressed
print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
    loop.run_forever()
except KeyboardInterrupt:
    pass

# Close the server
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()

参见

The TCP echo server protocol example uses the AbstractEventLoop.create_server() method.

18.5.5.7.3. 获取 HTTP 标头

查询命令行传入 URL 的 HTTP 标头的简单示例:

import asyncio
import urllib.parse
import sys

@asyncio.coroutine
def print_http_headers(url):
    url = urllib.parse.urlsplit(url)
    if url.scheme == 'https':
        connect = asyncio.open_connection(url.hostname, 443, ssl=True)
    else:
        connect = asyncio.open_connection(url.hostname, 80)
    reader, writer = yield from connect
    query = ('HEAD {path} HTTP/1.0\r\n'
             'Host: {hostname}\r\n'
             '\r\n').format(path=url.path or '/', hostname=url.hostname)
    writer.write(query.encode('latin-1'))
    while True:
        line = yield from reader.readline()
        if not line:
            break
        line = line.decode('latin1').rstrip()
        if line:
            print('HTTP header> %s' % line)

    # Ignore the body, close the socket
    writer.close()

url = sys.argv[1]
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(print_http_headers(url))
loop.run_until_complete(task)
loop.close()

用法:

python example.py http://example.com/path/page.html

或使用 HTTPS:

python example.py https://example.com/path/page.html

18.5.5.7.4. 注册一个打开的套接字以等待使用流的数据

使用 open_connection() 函数实现等待直到套接字接收到数据的协程:

import asyncio
try:
    from socket import socketpair
except ImportError:
    from asyncio.windows_utils import socketpair

@asyncio.coroutine
def wait_for_data(loop):
    # Create a pair of connected sockets
    rsock, wsock = socketpair()

    # Register the open socket to wait for data
    reader, writer = yield from asyncio.open_connection(sock=rsock, loop=loop)

    # Simulate the reception of data from the network
    loop.call_soon(wsock.send, 'abc'.encode())

    # Wait for data
    data = yield from reader.read(100)

    # Got data, we are done: close the socket
    print("Received:", data.decode())
    writer.close()

    # Close the second socket
    wsock.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(wait_for_data(loop))
loop.close()

参见

The register an open socket to wait for data using a protocol example uses a low-level protocol created by the AbstractEventLoop.create_connection() method.

The watch a file descriptor for read events example uses the low-level AbstractEventLoop.add_reader() method to register the file descriptor of a socket.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

18.5.6. Subprocess

Source code: Lib/asyncio/subprocess.py [https://github.com/python/cpython/tree/3.6/Lib/asyncio/subprocess.py]

18.5.6.1. Windows event loop

On Windows, the default event loop is SelectorEventLoop which does not support subprocesses. ProactorEventLoop should be used instead. Example to use it on Windows:

import asyncio, sys

if sys.platform == 'win32':
    loop = asyncio.ProactorEventLoop()
    asyncio.set_event_loop(loop)

参见

Available event loops and Platform support.

18.5.6.2. Create a subprocess: high-level API using Process

coroutine asyncio.create_subprocess_exec(*args, stdin=None, stdout=None, stderr=None, loop=None, limit=None, **kwds)

创建一个子进程。

The limit parameter sets the buffer limit passed to the StreamReader. See AbstractEventLoop.subprocess_exec() for other parameters.

返回一个 Process 实例。

This function is a coroutine.

coroutine asyncio.create_subprocess_shell(cmd, stdin=None, stdout=None, stderr=None, loop=None, limit=None, **kwds)

Run the shell command cmd.

The limit parameter sets the buffer limit passed to the StreamReader. See AbstractEventLoop.subprocess_shell() for other parameters.

返回一个 Process 实例。

It is the application’s responsibility to ensure that all whitespace and metacharacters are quoted appropriately to avoid shell injection [https://en.wikipedia.org/wiki/Shell_injection#Shell_injection] vulnerabilities. The shlex.quote() function can be used to properly escape whitespace and shell metacharacters in strings that are going to be used to construct shell commands.

This function is a coroutine.

Use the AbstractEventLoop.connect_read_pipe() and AbstractEventLoop.connect_write_pipe() methods to connect pipes.

18.5.6.3. Create a subprocess: low-level API using subprocess.Popen

Run subprocesses asynchronously using the subprocess module.

coroutine AbstractEventLoop.subprocess_exec(protocol_factory, *args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)

Create a subprocess from one or more string arguments (character strings or bytes strings encoded to the filesystem encoding), where the first string specifies the program to execute, and the remaining strings specify the program’s arguments. (Thus, together the string arguments form the sys.argv value of the program, assuming it is a Python script.) This is similar to the standard library subprocess.Popen class called with shell=False and the list of strings passed as the first argument; however, where Popen takes a single argument which is list of strings, subprocess_exec() takes multiple string arguments.

The protocol_factory must instantiate a subclass of the asyncio.SubprocessProtocol class.

Other parameters:

stdin: Either a file-like object representing the pipe to be connected to the subprocess’s standard input stream using connect_write_pipe(), or the constant subprocess.PIPE (the default). By default a new pipe will be created and connected.

stdout: Either a file-like object representing the pipe to be connected to the subprocess’s standard output stream using connect_read_pipe(), or the constant subprocess.PIPE (the default). By default a new pipe will be created and connected.

stderr: Either a file-like object representing the pipe to be connected to the subprocess’s standard error stream using connect_read_pipe(), or one of the constants subprocess.PIPE (the default) or subprocess.STDOUT. By default a new pipe will be created and connected. When subprocess.STDOUT is specified, the subprocess’s standard error stream will be connected to the same pipe as the standard output stream.

All other keyword arguments are passed to subprocess.Popen without interpretation, except for bufsize, universal_newlines and shell, which should not be specified at all.

Returns a pair of (transport, protocol), where transport is an instance of BaseSubprocessTransport.

This method is a coroutine.

See the constructor of the subprocess.Popen class for parameters.

coroutine AbstractEventLoop.subprocess_shell(protocol_factory, cmd, *, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)

Create a subprocess from cmd, which is a character string or a bytes string encoded to the filesystem encoding, using the platform’s “shell” syntax. This is similar to the standard library subprocess.Popen class called with shell=True.

The protocol_factory must instantiate a subclass of the asyncio.SubprocessProtocol class.

See subprocess_exec() for more details about the remaining arguments.

Returns a pair of (transport, protocol), where transport is an instance of BaseSubprocessTransport.

It is the application’s responsibility to ensure that all whitespace and metacharacters are quoted appropriately to avoid shell injection [https://en.wikipedia.org/wiki/Shell_injection#Shell_injection] vulnerabilities. The shlex.quote() function can be used to properly escape whitespace and shell metacharacters in strings that are going to be used to construct shell commands.

This method is a coroutine.

参见

The AbstractEventLoop.connect_read_pipe() and AbstractEventLoop.connect_write_pipe() methods.

18.5.6.4. 常量

asyncio.subprocess.PIPE

Special value that can be used as the stdin, stdout or stderr argument to create_subprocess_shell() and create_subprocess_exec() and indicates that a pipe to the standard stream should be opened.

asyncio.subprocess.STDOUT

Special value that can be used as the stderr argument to create_subprocess_shell() and create_subprocess_exec() and indicates that standard error should go into the same handle as standard output.

asyncio.subprocess.DEVNULL

Special value that can be used as the stdin, stdout or stderr argument to create_subprocess_shell() and create_subprocess_exec() and indicates that the special file os.devnull will be used.

18.5.6.5. Process

class asyncio.subprocess.Process

A subprocess created by the create_subprocess_exec() or the create_subprocess_shell() function.

The API of the Process class was designed to be close to the API of the subprocess.Popen class, but there are some differences:

There is no explicit poll() method

The communicate() and wait() methods don’t take a timeout parameter: use the wait_for() function

The universal_newlines parameter is not supported (only bytes strings are supported)

The wait() method of the Process class is asynchronous whereas the wait() method of the Popen class is implemented as a busy loop.

This class is not thread safe. See also the Subprocess and threads section.

coroutine wait()

Wait for child process to terminate. Set and return returncode attribute.

This method is a coroutine.

注解

This will deadlock when using stdout=PIPE or stderr=PIPE and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use the communicate() method when using pipes to avoid that.

coroutine communicate(input=None)

Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be data to be sent to the child process, or None, if no data should be sent to the child. The type of input must be bytes.

communicate() returns a tuple (stdout_data, stderr_data).

If a BrokenPipeError or ConnectionResetError exception is raised when writing input into stdin, the exception is ignored. It occurs when the process exits before all data are written into stdin.

Note that if you want to send data to the process’s stdin, you need to create the Process object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too.

This method is a coroutine.

注解

The data read is buffered in memory, so do not use this method if the data size is large or unlimited.

在 3.4.2 版更改: The method now ignores BrokenPipeError and ConnectionResetError.

send_signal(signal)

将信号 signal 发送给子进程。

注解

在 Windows 上，SIGTERM 是 terminate() 的别名。 CTRL_C_EVENT 和 CTRL_BREAK_EVENT 可被发送给创建时附带 creationflags 形参且其中包括 CREATE_NEW_PROCESS_GROUP 的进程。

terminate()

Stop the child. On Posix OSs the method sends signal.SIGTERM to the child. On Windows the Win32 API function TerminateProcess() is called to stop the child.

kill()

Kills the child. On Posix OSs the function sends SIGKILL to the child. On Windows kill() is an alias for terminate().

stdin

Standard input stream (StreamWriter), None if the process was created with stdin=None.

stdout

Standard output stream (StreamReader), None if the process was created with stdout=None.

stderr

Standard error stream (StreamReader), None if the process was created with stderr=None.

警告

Use the communicate() method rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to streams pausing reading or writing and blocking the child process.

pid

The identifier of the process.

Note that for processes created by the create_subprocess_shell() function, this attribute is the process identifier of the spawned shell.

returncode

Return code of the process when it exited. A None value indicates that the process has not terminated yet.

A negative value -N indicates that the child was terminated by signal N (Unix only).

18.5.6.6. Subprocess and threads

asyncio supports running subprocesses from different threads, but there are limits:

An event loop must run in the main thread

The child watcher must be instantiated in the main thread, before executing subprocesses from other threads. Call the get_child_watcher() function in the main thread to instantiate the child watcher.

The asyncio.subprocess.Process class is not thread safe.

参见

asyncio 中的并发和多线程 章节。

18.5.6.7. Subprocess examples

18.5.6.7.1. Subprocess using transport and protocol

Example of a subprocess protocol using to get the output of a subprocess and to wait for the subprocess exit. The subprocess is created by the AbstractEventLoop.subprocess_exec() method:

import asyncio
import sys

class DateProtocol(asyncio.SubprocessProtocol):
    def __init__(self, exit_future):
        self.exit_future = exit_future
        self.output = bytearray()

    def pipe_data_received(self, fd, data):
        self.output.extend(data)

    def process_exited(self):
        self.exit_future.set_result(True)

@asyncio.coroutine
def get_date(loop):
    code = 'import datetime; print(datetime.datetime.now())'
    exit_future = asyncio.Future(loop=loop)

    # Create the subprocess controlled by the protocol DateProtocol,
    # redirect the standard output into a pipe
    create = loop.subprocess_exec(lambda: DateProtocol(exit_future),
                                  sys.executable, '-c', code,
                                  stdin=None, stderr=None)
    transport, protocol = yield from create

    # Wait for the subprocess exit using the process_exited() method
    # of the protocol
    yield from exit_future

    # Close the stdout pipe
    transport.close()

    # Read the output which was collected by the pipe_data_received()
    # method of the protocol
    data = bytes(protocol.output)
    return data.decode('ascii').rstrip()

if sys.platform == "win32":
    loop = asyncio.ProactorEventLoop()
    asyncio.set_event_loop(loop)
else:
    loop = asyncio.get_event_loop()

date = loop.run_until_complete(get_date(loop))
print("Current date: %s" % date)
loop.close()

18.5.6.7.2. Subprocess using streams

Example using the Process class to control the subprocess and the StreamReader class to read from the standard output. The subprocess is created by the create_subprocess_exec() function:

import asyncio.subprocess
import sys

@asyncio.coroutine
def get_date():
    code = 'import datetime; print(datetime.datetime.now())'

    # Create the subprocess, redirect the standard output into a pipe
    create = asyncio.create_subprocess_exec(sys.executable, '-c', code,
                                            stdout=asyncio.subprocess.PIPE)
    proc = yield from create

    # Read one line of output
    data = yield from proc.stdout.readline()
    line = data.decode('ascii').rstrip()

    # Wait for the subprocess exit
    yield from proc.wait()
    return line

if sys.platform == "win32":
    loop = asyncio.ProactorEventLoop()
    asyncio.set_event_loop(loop)
else:
    loop = asyncio.get_event_loop()

date = loop.run_until_complete(get_date())
print("Current date: %s" % date)
loop.close()

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

18.5.7. Synchronization primitives

Source code: Lib/asyncio/locks.py [https://github.com/python/cpython/tree/3.6/Lib/asyncio/locks.py]

Locks:

Lock

Event

Condition

Semaphores:

Semaphore

BoundedSemaphore

asyncio lock API was designed to be close to classes of the threading module (Lock, Event, Condition, Semaphore, BoundedSemaphore), but it has no timeout parameter. The asyncio.wait_for() function can be used to cancel a task after a timeout.

18.5.7.1. Locks

18.5.7.1.1. Lock

class asyncio.Lock(*, loop=None)

Primitive lock objects.

A primitive lock is a synchronization primitive that is not owned by a particular coroutine when locked. A primitive lock is in one of two states, ‘locked’ or ‘unlocked’.

It is created in the unlocked state. It has two basic methods, acquire() and release(). When the state is unlocked, acquire() changes the state to locked and returns immediately. When the state is locked, acquire() blocks until a call to release() in another coroutine changes it to unlocked, then the acquire() call resets it to locked and returns. The release() method should only be called in the locked state; it changes the state to unlocked and returns immediately. If an attempt is made to release an unlocked lock, a RuntimeError will be raised.

When more than one coroutine is blocked in acquire() waiting for the state to turn to unlocked, only one coroutine proceeds when a release() call resets the state to unlocked; first coroutine which is blocked in acquire() is being processed.

acquire() is a coroutine and should be called with yield from.

Locks also support the context management protocol. (yield from lock) should be used as the context manager expression.

This class is not thread safe.

Usage:

lock = Lock()
...
yield from lock
try:
    ...
finally:
    lock.release()

Context manager usage:

lock = Lock()
...
with (yield from lock):
    ...

Lock objects can be tested for locking state:

if not lock.locked():
    yield from lock
else:
    # lock is acquired
    ...

locked()

Return True if the lock is acquired.

coroutine acquire()

Acquire a lock.

This method blocks until the lock is unlocked, then sets it to locked and returns True.

This method is a coroutine.

release()

Release a lock.

When the lock is locked, reset it to unlocked, and return. If any other coroutines are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed.

在未锁定的锁调用时，会引发 RuntimeError 异常。

There is no return value.

18.5.7.1.2. Event

class asyncio.Event(*, loop=None)

An Event implementation, asynchronous equivalent to threading.Event.

Class implementing event objects. An event manages a flag that can be set to true with the set() method and reset to false with the clear() method. The wait() method blocks until the flag is true. The flag is initially false.

This class is not thread safe.

clear()

Reset the internal flag to false. Subsequently, coroutines calling wait() will block until set() is called to set the internal flag to true again.

is_set()

Return True if and only if the internal flag is true.

set()

Set the internal flag to true. All coroutines waiting for it to become true are awakened. Coroutine that call wait() once the flag is true will not block at all.

coroutine wait()

Block until the internal flag is true.

If the internal flag is true on entry, return True immediately. Otherwise, block until another coroutine calls set() to set the flag to true, then return True.

This method is a coroutine.

18.5.7.1.3. Condition

class asyncio.Condition(lock=None, *, loop=None)

A Condition implementation, asynchronous equivalent to threading.Condition.

This class implements condition variable objects. A condition variable allows one or more coroutines to wait until they are notified by another coroutine.

If the lock argument is given and not None, it must be a Lock object, and it is used as the underlying lock. Otherwise, a new Lock object is created and used as the underlying lock.

This class is not thread safe.

coroutine acquire()

获取下层的锁。

This method blocks until the lock is unlocked, then sets it to locked and returns True.

This method is a coroutine.

notify(n=1)

By default, wake up one coroutine waiting on this condition, if any. If the calling coroutine has not acquired the lock when this method is called, a RuntimeError is raised.

This method wakes up at most n of the coroutines waiting for the condition variable; it is a no-op if no coroutines are waiting.

注解

An awakened coroutine does not actually return from its wait() call until it can reacquire the lock. Since notify() does not release the lock, its caller should.

locked()

如果下层的锁已被获取则返回 True。

notify_all()

Wake up all coroutines waiting on this condition. This method acts like notify(), but wakes up all waiting coroutines instead of one. If the calling coroutine has not acquired the lock when this method is called, a RuntimeError is raised.

release()

释放下层的锁。

When the lock is locked, reset it to unlocked, and return. If any other coroutines are blocked waiting for the lock to become unlocked, allow exactly one of them to proceed.

在未锁定的锁调用时，会引发 RuntimeError 异常。

There is no return value.

coroutine wait()

等待直至收到通知。

If the calling coroutine has not acquired the lock when this method is called, a RuntimeError is raised.

This method releases the underlying lock, and then blocks until it is awakened by a notify() or notify_all() call for the same condition variable in another coroutine. Once awakened, it re-acquires the lock and returns True.

This method is a coroutine.

coroutine wait_for(predicate)

Wait until a predicate becomes true.

The predicate should be a callable which result will be interpreted as a boolean value. The final predicate value is the return value.

This method is a coroutine.

18.5.7.2. Semaphores

18.5.7.2.1. Semaphore

class asyncio.Semaphore(value=1, *, loop=None)

A Semaphore implementation.

A semaphore manages an internal counter which is decremented by each acquire() call and incremented by each release() call. The counter can never go below zero; when acquire() finds that it is zero, it blocks, waiting until some other coroutine calls release().

Semaphores also support the context management protocol.

The optional argument gives the initial value for the internal counter; it defaults to 1. If the value given is less than 0, ValueError is raised.

This class is not thread safe.

coroutine acquire()

获取一个信号量。

If the internal counter is larger than zero on entry, decrement it by one and return True immediately. If it is zero on entry, block, waiting until some other coroutine has called release() to make it larger than 0, and then return True.

This method is a coroutine.

locked()

如果信号量对象无法被立即获取则返回 True。

release()

Release a semaphore, incrementing the internal counter by one. When it was zero on entry and another coroutine is waiting for it to become larger than zero again, wake up that coroutine.

18.5.7.2.2. BoundedSemaphore

class asyncio.BoundedSemaphore(value=1, *, loop=None)

A bounded semaphore implementation. Inherit from Semaphore.

This raises ValueError in release() if it would increase the value above the initial value.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

18.5.8. 队列集

Source code: Lib/asyncio/queues.py [https://github.com/python/cpython/tree/3.6/Lib/asyncio/queues.py]

Queues:

Queue

PriorityQueue

LifoQueue

asyncio queue API was designed to be close to classes of the queue module (Queue, PriorityQueue, LifoQueue), but it has no timeout parameter. The asyncio.wait_for() function can be used to cancel a task after a timeout.

18.5.8.1. 队列

class asyncio.Queue(maxsize=0, *, loop=None)

A queue, useful for coordinating producer and consumer coroutines.

If maxsize is less than or equal to zero, the queue size is infinite. If it is an integer greater than 0, then yield from put() will block when the queue reaches maxsize, until an item is removed by get().

Unlike the standard library queue, you can reliably know this Queue’s size with qsize(), since your single-threaded asyncio application won’t be interrupted between calling qsize() and doing an operation on the Queue.

这个类不是线程安全的（not thread safe）。

在 3.4.4 版更改: New join() and task_done() methods.

empty()

如果队列为空返回 True ，否则返回 False 。

full()

如果有 maxsize 个条目在队列中，则返回 True 。

注解

If the Queue was initialized with maxsize=0 (the default), then full() is never True.

coroutine get()

从队列中删除并返回一个元素。如果队列为空，则等待，直到队列中有元素。

This method is a coroutine.

参见

The empty() method.

get_nowait()

Remove and return an item from the queue.

立即返回一个队列中的元素，如果队列内有值，否则引发异常 QueueEmpty 。

coroutine join()

Block until all items in the queue have been gotten and processed.

The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls task_done() to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, join() unblocks.

This method is a coroutine.

3.4.4 新版功能.

coroutine put(item)

Put an item into the queue. If the queue is full, wait until a free slot is available before adding item.

This method is a coroutine.

参见

The full() method.

put_nowait(item)

不阻塞的放一个元素入队列。

如果没有立即可用的空闲槽，引发 QueueFull 异常。

qsize()

Number of items in the queue.

task_done()

表明前面排队的任务已经完成，即get出来的元素相关操作已经完成。

由队列使用者控制。每个 get() 用于获取一个任务，任务最后调用 task_done() 告诉队列，这个任务已经完成。

如果 join() 当前正在阻塞，在所有条目都被处理后，将解除阻塞(意味着每个 put() 进队列的条目的 task_done() 都被收到)。

如果被调用的次数多于放入队列中的项目数量，将引发 ValueError 。

3.4.4 新版功能.

maxsize

队列中可存放的元素数量。

18.5.8.2. PriorityQueue

class asyncio.PriorityQueue

A subclass of Queue; retrieves entries in priority order (lowest first).

Entries are typically tuples of the form: (priority number, data).

18.5.8.3. LifoQueue

class asyncio.LifoQueue

A subclass of Queue that retrieves most recently added entries first.

18.5.8.3.1. 异常

exception asyncio.QueueEmpty

Exception raised when the get_nowait() method is called on a Queue object which is empty.

exception asyncio.QueueFull

Exception raised when the put_nowait() method is called on a Queue object which is full.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

18.5.9. Develop with asyncio

Asynchronous programming is different than classical “sequential” programming. This page lists common traps and explains how to avoid them.

18.5.9.1. Debug mode of asyncio

The implementation of asyncio has been written for performance. In order to ease the development of asynchronous code, you may wish to enable debug mode.

To enable all debug checks for an application:

Enable the asyncio debug mode globally by setting the environment variable PYTHONASYNCIODEBUG to 1, or by calling AbstractEventLoop.set_debug().

Set the log level of the asyncio logger to logging.DEBUG. For example, call logging.basicConfig(level=logging.DEBUG) at startup.

Configure the warnings module to display ResourceWarning warnings. For example, use the -Wdefault command line option of Python to display them.

Examples debug checks:

Log coroutines defined but never “yielded from”

call_soon() and call_at() methods raise an exception if they are called from the wrong thread.

Log the execution time of the selector

Log callbacks taking more than 100 ms to be executed. The AbstractEventLoop.slow_callback_duration attribute is the minimum duration in seconds of “slow” callbacks.

ResourceWarning warnings are emitted when transports and event loops are not closed explicitly.

参见

The AbstractEventLoop.set_debug() method and the asyncio logger.

18.5.9.2. Cancellation

Cancellation of tasks is not common in classic programming. In asynchronous programming, not only is it something common, but you have to prepare your code to handle it.

Futures and tasks can be cancelled explicitly with their Future.cancel() method. The wait_for() function cancels the waited task when the timeout occurs. There are many other cases where a task can be cancelled indirectly.

Don’t call set_result() or set_exception() method of Future if the future is cancelled: it would fail with an exception. For example, write:

if not fut.cancelled():
    fut.set_result('done')

Don’t schedule directly a call to the set_result() or the set_exception() method of a future with AbstractEventLoop.call_soon(): the future can be cancelled before its method is called.

If you wait for a future, you should check early if the future was cancelled to avoid useless operations. Example:

@coroutine
def slow_operation(fut):
    if fut.cancelled():
        return
    # ... slow computation ...
    yield from fut
    # ...

The shield() function can also be used to ignore cancellation.

18.5.9.3. Concurrency and multithreading

An event loop runs in a thread and executes all callbacks and tasks in the same thread. While a task is running in the event loop, no other task is running in the same thread. But when the task uses yield from, the task is suspended and the event loop executes the next task.

To schedule a callback from a different thread, the AbstractEventLoop.call_soon_threadsafe() method should be used. Example:

loop.call_soon_threadsafe(callback, *args)

Most asyncio objects are not thread safe. You should only worry if you access objects outside the event loop. For example, to cancel a future, don’t call directly its Future.cancel() method, but:

loop.call_soon_threadsafe(fut.cancel)

为了能够处理信号和执行子进程，事件循环必须运行于主线程中。

To schedule a coroutine object from a different thread, the run_coroutine_threadsafe() function should be used. It returns a concurrent.futures.Future to access the result:

future = asyncio.run_coroutine_threadsafe(coro_func(), loop)
result = future.result(timeout)  # Wait for the result with a timeout

The AbstractEventLoop.run_in_executor() method can be used with a thread pool executor to execute a callback in different thread to not block the thread of the event loop.

参见

The Synchronization primitives section describes ways to synchronize tasks.

The Subprocess and threads section lists asyncio limitations to run subprocesses from different threads.

18.5.9.4. Handle blocking functions correctly

Blocking functions should not be called directly. For example, if a function blocks for 1 second, other tasks are delayed by 1 second which can have an important impact on reactivity.

For networking and subprocesses, the asyncio module provides high-level APIs like protocols.

An executor can be used to run a task in a different thread or even in a different process, to not block the thread of the event loop. See the AbstractEventLoop.run_in_executor() method.

参见

The Delayed calls section details how the event loop handles time.

18.5.9.5. 日志记录

The asyncio module logs information with the logging module in the logger 'asyncio'.

The default log level for the asyncio module is logging.INFO. For those not wanting such verbosity from asyncio the log level can be changed. For example, to change the level to logging.WARNING:

logging.getLogger('asyncio').setLevel(logging.WARNING)

18.5.9.6. Detect coroutine objects never scheduled

When a coroutine function is called and its result is not passed to ensure_future() or to the AbstractEventLoop.create_task() method, the execution of the coroutine object will never be scheduled which is probably a bug. Enable the debug mode of asyncio to log a warning to detect it.

Example with the bug:

import asyncio

@asyncio.coroutine
def test():
    print("never scheduled")

test()

调试模式的输出:

Coroutine test() at test.py:3 was never yielded from
Coroutine object created at (most recent call last):
  File "test.py", line 7, in <module>
    test()

The fix is to call the ensure_future() function or the AbstractEventLoop.create_task() method with the coroutine object.

参见

Pending task destroyed.

18.5.9.7. Detect exceptions never consumed

Python usually calls sys.excepthook() on unhandled exceptions. If Future.set_exception() is called, but the exception is never consumed, sys.excepthook() is not called. Instead, a log is emitted when the future is deleted by the garbage collector, with the traceback where the exception was raised.

Example of unhandled exception:

import asyncio

@asyncio.coroutine
def bug():
    raise Exception("not consumed")

loop = asyncio.get_event_loop()
asyncio.ensure_future(bug())
loop.run_forever()
loop.close()

输出:

Task exception was never retrieved
future: <Task finished coro=<coro() done, defined at asyncio/coroutines.py:139> exception=Exception('not consumed',)>
Traceback (most recent call last):
  File "asyncio/tasks.py", line 237, in _step
    result = next(coro)
  File "asyncio/coroutines.py", line 141, in coro
    res = func(*args, **kw)
  File "test.py", line 5, in bug
    raise Exception("not consumed")
Exception: not consumed

Enable the debug mode of asyncio to get the traceback where the task was created. Output in debug mode:

Task exception was never retrieved
future: <Task finished coro=<bug() done, defined at test.py:3> exception=Exception('not consumed',) created at test.py:8>
source_traceback: Object created at (most recent call last):
  File "test.py", line 8, in <module>
    asyncio.ensure_future(bug())
Traceback (most recent call last):
  File "asyncio/tasks.py", line 237, in _step
    result = next(coro)
  File "asyncio/coroutines.py", line 79, in __next__
    return next(self.gen)
  File "asyncio/coroutines.py", line 141, in coro
    res = func(*args, **kw)
  File "test.py", line 5, in bug
    raise Exception("not consumed")
Exception: not consumed

There are different options to fix this issue. The first option is to chain the coroutine in another coroutine and use classic try/except:

@asyncio.coroutine
def handle_exception():
    try:
        yield from bug()
    except Exception:
        print("exception consumed")

loop = asyncio.get_event_loop()
asyncio.ensure_future(handle_exception())
loop.run_forever()
loop.close()

Another option is to use the AbstractEventLoop.run_until_complete() function:

task = asyncio.ensure_future(bug())
try:
    loop.run_until_complete(task)
except Exception:
    print("exception consumed")

参见

The Future.exception() method.

18.5.9.8. Chain coroutines correctly

When a coroutine function calls other coroutine functions and tasks, they should be chained explicitly with yield from. Otherwise, the execution is not guaranteed to be sequential.

Example with different bugs using asyncio.sleep() to simulate slow operations:

import asyncio

@asyncio.coroutine
def create():
    yield from asyncio.sleep(3.0)
    print("(1) create file")

@asyncio.coroutine
def write():
    yield from asyncio.sleep(1.0)
    print("(2) write into file")

@asyncio.coroutine
def close():
    print("(3) close file")

@asyncio.coroutine
def test():
    asyncio.ensure_future(create())
    asyncio.ensure_future(write())
    asyncio.ensure_future(close())
    yield from asyncio.sleep(2.0)
    loop.stop()

loop = asyncio.get_event_loop()
asyncio.ensure_future(test())
loop.run_forever()
print("Pending tasks at exit: %s" % asyncio.Task.all_tasks(loop))
loop.close()

Expected output:

(1) create file
(2) write into file
(3) close file
Pending tasks at exit: set()

Actual output:

(3) close file
(2) write into file
Pending tasks at exit: {<Task pending create() at test.py:7 wait_for=<Future pending cb=[Task._wakeup()]>>}
Task was destroyed but it is pending!
task: <Task pending create() done at test.py:5 wait_for=<Future pending cb=[Task._wakeup()]>>

The loop stopped before the create() finished, close() has been called before write(), whereas coroutine functions were called in this order: create(), write(), close().

To fix the example, tasks must be marked with yield from:

@asyncio.coroutine
def test():
    yield from asyncio.ensure_future(create())
    yield from asyncio.ensure_future(write())
    yield from asyncio.ensure_future(close())
    yield from asyncio.sleep(2.0)
    loop.stop()

Or without asyncio.ensure_future():

@asyncio.coroutine
def test():
    yield from create()
    yield from write()
    yield from close()
    yield from asyncio.sleep(2.0)
    loop.stop()

18.5.9.9. Pending task destroyed

If a pending task is destroyed, the execution of its wrapped coroutine did not complete. It is probably a bug and so a warning is logged.

Example of log:

Task was destroyed but it is pending!
task: <Task pending coro=<kill_me() done, defined at test.py:5> wait_for=<Future pending cb=[Task._wakeup()]>>

Enable the debug mode of asyncio to get the traceback where the task was created. Example of log in debug mode:

Task was destroyed but it is pending!
source_traceback: Object created at (most recent call last):
  File "test.py", line 15, in <module>
    task = asyncio.ensure_future(coro, loop=loop)
task: <Task pending coro=<kill_me() done, defined at test.py:5> wait_for=<Future pending cb=[Task._wakeup()] created at test.py:7> created at test.py:15>

参见

Detect coroutine objects never scheduled.

18.5.9.10. Close transports and event loops

When a transport is no more needed, call its close() method to release resources. Event loops must also be closed explicitly.

If a transport or an event loop is not closed explicitly, a ResourceWarning warning will be emitted in its destructor. By default, ResourceWarning warnings are ignored. The Debug mode of asyncio section explains how to display them.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

18.6. asyncore — 异步socket处理器

源码： Lib/asyncore.py [https://github.com/python/cpython/tree/3.6/Lib/asyncore.py]

3.6 版后已移除: 请使用 asyncio 替代。

注解

该模块仅为提供向后兼容。我们推荐在新代码中使用 asyncio 。

该模块提供用于编写异步套接字服务客户端与服务端的基础构件。

只有两种方法让单个处理器上的程序“同一时间完成不止一件事”。 多线程编程是最简单和最流行的方法，但是还有另一种非常不同的技术，它可以让你拥有多线程的几乎所有优点，而无需实际使用多线程。 它仅仅在你的程序主要受 I/O 限制时有用，那么。 如果你的程序受处理器限制，那么先发制人的预定线程可能就是你真正需要的。 但是，网络服务器很少受处理器限制。

如果你的操作系统在其 I/O 库中支持 select() 系统调用（几乎所有操作系统），那么你可以使用它来同时处理多个通信通道；在 I/O 正在“后台”时进行其他工作。 虽然这种策略看起来很奇怪和复杂，特别是起初，它在很多方面比多线程编程更容易理解和控制。 asyncore 模块为您解决了许多难题，使得构建复杂的高性能网络服务器和客户端的任务变得轻而易举。 对于“会话”应用程序和协议，伴侣 asynchat 模块是非常宝贵的。

这两个模块背后的基本思想是创建一个或多个网络 通道 ，类的实例 asyncore.dispatcher 和 asynchat.async_chat 。 创建通道会将它们添加到全局映射中，如果你不为它提供自己的 映射 ，则由 loop() 函数使用。

一旦创建了初始通道，调用 loop() 函数将激活通道服务，该服务将一直持续到最后一个通道（包括在异步服务期间已添加到映射中的任何通道）关闭。

asyncore.loop([timeout[, use_poll[, map[, count]]]])

进入一个轮询循环，其在循环计数超出或所有打开的通道关闭后终止。 所有参数都是可选的。 count 形参默认为 None ，导致循环仅在所有通道关闭时终止。 timeout 形参为适当的 select() 或 poll() 调用设置超时参数，以秒为单位; 默认值为30秒。 use_poll 形参，如果为 True ，则表示 poll() 应优先使用 select() (默认为``False``)。

map 形参是一个条目为所监视通道的字典。 当通道关闭时它们会被从映射中删除。 如果省略 map，则会使用一个全局映射。 通道 (asyncore.dispatcher, asynchat.async_chat 及其子类的实例) 可以在映射中任意混合。

class asyncore.dispatcher

dispatcher 类是对低层级套接字对象的轻量包装器。 要让它更有用处，可以从异步循环调用一些事件处理方法。 在其他方面，它可以被当作是普通的非阻塞型套接字对象。

在特定时间或特定连接状态下触发的低层级事件可通知异步循环发生了特定的高层级事件。 例如，如果我们请求了一个套接字以连接到另一台主机，我们会在套接字首次变得可写时得知连接已建立（在此刻你将知道可以向其写入并预期能够成功）。 包含的高层级事件有:

Event

描述

handle_connect()

由首个读取或写入事件所包含

handle_close()

由不带可用数据的读取事件引起

handle_accepted()

由在监听套接字上的读取事件引起

在异步处理过程中，每个已映射通道的 readable() 和 writable() 方法会被用来确定是否要将通道的套接字添加到已执行 select() 或 poll() 用于读取和写入事件的通道列表中。

因此，通道事件的集合要大于基本套接字事件。 可以在你的子类中被重载的全部方法集合如下:

handle_read()

当异步循环检测到通道的套接字上的 read() 调用将要成功时会被调用。

handle_write()

当异步循环检测到一个可写套接字可以被写入时会被调用。 通常此方法将实现必要的缓冲机制以保证运行效率。 例如:

def handle_write(self):
    sent = self.send(self.buffer)
    self.buffer = self.buffer[sent:]

handle_expt()

当一个套接字连接存在带外（OOB）数据时会被调用。 这几乎从来不会发生，因为 OOB 虽然受支持但很少被使用。

handle_connect()

当活动打开方的套接字实际建立连接时会被调用。 可能会发送一条“欢迎”消息，或者向远程端点发起协议协商等。

handle_close()

当套接字关闭时会被调用。

handle_error()

当一个异常被引发并且未获得其他处理时会被调用。 默认版本将打印精简的回溯信息。

handle_accept()

当可以与发起对本地端点的 connect() 调用的新远程端点建立连接时会在侦听通道（被动打开方）上被调用。 在 3.2 版中已被弃用；请改用 handle_accepted()。

3.2 版后已移除.

handle_accepted(sock, addr)

当与发起对本地端点的 connect() 调用的新远程端点已建立连接时会在侦听通道（被动打开方）上被调用。 sock 是可被用于在连接上发送和接收数据的 新建 套接字对象，而 addr 是绑定到连接另一端的套接字的地址。

3.2 新版功能.

readable()

每次在异步循环之外被调用以确定是否应当将一个通道的套接字添加到可能在其上发生读取事件的列表中。 默认方法会简单地返回 True，表示在默认情况下，所有通道都希望能读取事件。

writable()

每次在异步循环之外被调用以确定是否应当将一个通道的套接字添加到可能在其上发生写入事件的列表中。 默认方法会简单地返回 True，表示在默认情况下，所有通道都希望能写入事件。

此外，每个通道都委托或扩展了许多套接字方法。 它们大部分都与其套接字的对应方法几乎一样。

create_socket(family=socket.AF_INET, type=socket.SOCK_STREAM)

这与普通套接字的创建相同，并会使用同样的创建选项。 请参阅 socket 文档了解有关创建套接字的信息。

在 3.3 版更改: family 和 type 参数可以被省略。

connect(address)

与普通套接字对象一样，address 是一个元组，它的第一个元素是要连接的主机，第二个元素是端口号。

send(data)

将 data 发送到套接字的远程端点。

recv(buffer_size)

从套接字的远程端点读取至多 buffer_size 个字节。 读到空字节串表明通道已从另一端被关闭。

请注意 recv() 可能会引发 BlockingIOError，即使 select.select() 或 select.poll() 报告套接字已准备好被读取。

listen(backlog)

侦听与套接字的连接。 backlog 参数指明排入连接队列的最大数量且至少应为 1；最大值取决于具体系统（通常为 5）。

bind(address)

将套接字绑定到 address。 套接字必须尚未被绑定。 (address 的格式取决于具体的地址族 — 请参阅 socket 文档了解更多信息。) 要将套接字标记为可重用的 (设置 SO_REUSEADDR 选项)，请调用 dispatcher 对象的 set_reuse_addr() 方法。

accept()

接受一个连接。 此套接字必须绑定到一个地址上并且侦听连接。 返回值可以是 None 或一个 (conn, address) 对，其中 conn 是一个可用来在此连接上发送和接收数据的 新的 套接字对象，而 address 是绑定到连接另一端套接字的地址。 当返回 None 时意味着连接没有建立，在此情况下服务器应当忽略此事件并继续侦听后续的入站连接。

close()

关闭套接字。 在此套接字对象上的后续操作都将失败。 远程端点将不再接收任何数据（在排入队列的数据被清空之后）。 当套接字被垃圾回收时会自动关闭。

class asyncore.dispatcher_with_send

dispatcher 的一个添加了简单缓冲输出功能的子类，适用于简单客户端。 对于更复杂的用法请使用 asynchat.async_chat。

class asyncore.file_dispatcher

A file_dispatcher takes a file descriptor or file object along with an optional map argument and wraps it for use with the poll() or loop() functions. If provided a file object or anything with a fileno() method, that method will be called and passed to the file_wrapper constructor. Availability: UNIX.

class asyncore.file_wrapper

A file_wrapper takes an integer file descriptor and calls os.dup() to duplicate the handle so that the original handle may be closed independently of the file_wrapper. This class implements sufficient methods to emulate a socket for use by the file_dispatcher class. Availability: UNIX.

18.6.1. asyncore 示例基本 HTTP 客户端

下面是一个非常基本的 HTTP 客户端，它使用了 dispatcher 类来实现套接字处理:

import asyncore

class HTTPClient(asyncore.dispatcher):

    def __init__(self, host, path):
        asyncore.dispatcher.__init__(self)
        self.create_socket()
        self.connect( (host, 80) )
        self.buffer = bytes('GET %s HTTP/1.0\r\nHost: %s\r\n\r\n' %
                            (path, host), 'ascii')

    def handle_connect(self):
        pass

    def handle_close(self):
        self.close()

    def handle_read(self):
        print(self.recv(8192))

    def writable(self):
        return (len(self.buffer) > 0)

    def handle_write(self):
        sent = self.send(self.buffer)
        self.buffer = self.buffer[sent:]

client = HTTPClient('www.python.org', '/')
asyncore.loop()

18.6.2. asyncore 示例基本回显服务器

下面是一个基本的回显服务器，它使用了 dispatcher 类来接受连接并将入站连接发送给处理程序:

import asyncore

class EchoHandler(asyncore.dispatcher_with_send):

    def handle_read(self):
        data = self.recv(8192)
        if data:
            self.send(data)

class EchoServer(asyncore.dispatcher):

    def __init__(self, host, port):
        asyncore.dispatcher.__init__(self)
        self.create_socket()
        self.set_reuse_addr()
        self.bind((host, port))
        self.listen(5)

    def handle_accepted(self, sock, addr):
        print('Incoming connection from %s' % repr(addr))
        handler = EchoHandler(sock)

server = EchoServer('localhost', 8080)
asyncore.loop()

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

18.7. asynchat — 异步 socket 指令/响应 处理器

源代码: Lib/asynchat.py [https://github.com/python/cpython/tree/3.6/Lib/asynchat.py]

3.6 版后已移除: 请使用 asyncio 替代。

注解

该模块仅为提供向后兼容。我们推荐在新代码中使用 asyncio 。

此模块在 asyncore 框架之上构建，简化了异步客户端和服务器并使得处理元素为以任意字符串结束或者为可变长度的协议更加容易。 asynchat 定义了一个可以由你来子类化的抽象类 async_chat，提供了 collect_incoming_data() 和 found_terminator() 等方法的实现。 它使用与 asyncore 相同的异步循环，并且可以在通道映射中自由地混合 asyncore.dispatcher 和 asynchat.async_chat 这两种类型的通道。 一般来说 asyncore.dispatcher 服务器通道在接收到传入的连接请求时会生成新的 asynchat.async_chat 通道对象。

class asynchat.async_chat

这个类是 asyncore.dispatcher 的抽象子类。 对于实际使用的代码你必须子类化 async_chat，提供有意义的 collect_incoming_data() 和 found_terminator() 方法。 asyncore.dispatcher 的方法也可以被使用，但它们在消息/响应上下文中并不是全都有意义。

与 asyncore.dispatcher 类似，async_chat 也定义了一组通过对 select() 调用之后的套接字条件进行分析所生成的事件。 一旦启动轮询循环 async_chat 对象的方法就会被事件处理框架调用而无须程序员方面做任何操作。

两个可被修改的类属性，用以提升性能，甚至也可能会节省内存。

ac_in_buffer_size

异步输入缓冲区大小 (默认为 4096)。

ac_out_buffer_size

异步输出缓冲区大小 (默认为 4096)。

与 asyncore.dispatcher 不同，async_chat 允许你定义一个 FIFO 队列 producers。 其中的生产者只需要一个方法 more()，该方法应当返回要在通道上传输的数据。 生产者通过让其 more() 方法返回空字节串对象来表明其处于耗尽状态 (意即 它已不再包含数据)。 此时 async_chat 对象会将该生产者从队列中移除并开始使用下一个生产者，如果有下一个的话。 当生产者队列为空时 handle_write() 方法将不执行任何操作。 你要使用通道对象的 set_terminator() 方法来描述如何识别来自远程端点的入站传输的结束或是重要的中断点。

要构建一个可用的 async_chat 子类，你的输入方法 collect_incoming_data() 和 found_terminator() 必须要处理通道异步接收的数据。 这些参数的描述见下文。

async_chat.close_when_done()

将 None 推入生产者队列。 当此生产者被弹出队列时它将导致通道被关闭。

async_chat.collect_incoming_data(data)

调用时附带 data，其中包含任意数量的已接收数据。 必须被重载的默认方法将引发一个 NotImplementedError 异常。

async_chat.discard_buffers()

在紧急情况下此方法将丢弃输入和/或输出缓冲区以及生产者队列中的任何数据。

async_chat.found_terminator()

当输入数据流能匹配 set_terminator() 所设定的终结条件时会被调用。 必须被重载的默认方法将引发一个 NotImplementedError 异常。 被缓冲的输入数据应当可以通过实例属性来获取。

async_chat.get_terminator()

返回通道的当前终结器。

async_chat.push(data)

将数据推入通道的队列以确保其被传输。 要让通道将数据写到网络中你只需要这样做就足够了，虽然以更复杂的方式使用你自己的生产者也是有可能的，例如为了实现加密和分块。

async_chat.push_with_producer(producer)

获取一个生产者对象并将其加入到与通道相关联的生产者队列中。 当所有当前已推入的生产者都已被耗尽时通道将通过调用其 more() 方法来耗用此生产者的数据并将数据发送至远程端点。

async_chat.set_terminator(term)

设置可在通道上被识别的终结条件。 term 可以是三种类型值中的任意一种 ，对应于处理入站协议数据的三种不同方式。

term

描述

string

当在输入流中发现该字符串时将会调用 found_terminator()

integer

当接收到指定数量的字符时将会调用 found_terminator()

None

通道会不断地持续收集数据

请注意终结器之后的任何数据将可在 found_terminator() 被调用后由通道来读取。

18.7.1. asynchat 示例

下面的例子片段显示了如何通过 async_chat 来读取 HTTP 请求。 Web 服务器可以为每个入站的客户端连接创建 http_request_handler 对象。 请注意在初始时通道终结器会被设置为匹配 HTTP 请求头末尾的空行，并且会用一个旗标来指明请求头正在被读取。

一旦完成了标头的读取，如果请求类型为 POST (表明输入流中存在更多的数据) 则会使用 Content-Length: 标头来设置一个数值终结器以从通道读取适当数量的数据。

一旦完成了对所有相关输入的处理，将会在设置通道终结器为 None 以确保忽略掉 Web 客户端所发送的任何无关数据之后调用 handle_request() 方法。

import asynchat

class http_request_handler(asynchat.async_chat):

    def __init__(self, sock, addr, sessions, log):
        asynchat.async_chat.__init__(self, sock=sock)
        self.addr = addr
        self.sessions = sessions
        self.ibuffer = []
        self.obuffer = b""
        self.set_terminator(b"\r\n\r\n")
        self.reading_headers = True
        self.handling = False
        self.cgi_data = None
        self.log = log

    def collect_incoming_data(self, data):
        """Buffer the data"""
        self.ibuffer.append(data)

    def found_terminator(self):
        if self.reading_headers:
            self.reading_headers = False
            self.parse_headers(b"".join(self.ibuffer))
            self.ibuffer = []
            if self.op.upper() == b"POST":
                clen = self.headers.getheader("content-length")
                self.set_terminator(int(clen))
            else:
                self.handling = True
                self.set_terminator(None)
                self.handle_request()
        elif not self.handling:
            self.set_terminator(None)  # browsers sometimes over-send
            self.cgi_data = parse(self.headers, b"".join(self.ibuffer))
            self.handling = True
            self.ibuffer = []
            self.handle_request()

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

18.8. signal — 设置异步事件处理程序

该模块提供了在 Python 中使用信号处理程序的机制。

18.8.1. 一般规则

The signal.signal() function allows defining custom handlers to be executed when a signal is received. A small number of default handlers are installed: SIGPIPE is ignored (so write errors on pipes and sockets can be reported as ordinary Python exceptions) and SIGINT is translated into a KeyboardInterrupt exception.

一旦设置，特定信号的处理程序将保持安装，直到它被显式重置（ Python 模拟 BSD 样式接口而不管底层实现），但 SIGCHLD 的处理程序除外，它遵循底层实现。

18.8.1.1. 执行 Python 信号处理程序

Python 信号处理程序不会在低级（ C ）信号处理程序中执行。相反，低级信号处理程序设置一个标志，告诉 virtual machine 稍后执行相应的 Python 信号处理程序（例如在下一个 bytecode 指令）。这会导致：

捕获同步错误是没有意义的，例如 SIGFPE 或 SIGSEGV ，它们是由 C 代码中的无效操作引起的。Python 将从信号处理程序返回到 C 代码，这可能会再次引发相同的信号，导致 Python 显然的挂起。 从Python 3.3开始，你可以使用 faulthandler 模块来报告同步错误。

纯 C 中实现的长时间运行的计算（例如在大量文本上的正则表达式匹配）可以在任意时间内不间断地运行，而不管接收到任何信号。计算完成后将调用 Python 信号处理程序。

18.8.1.2. 信号与线程

Python 信号处理程序总是在主 Python 线程中执行，即使信号是在另一个线程中接收的。这意味着信号不能用作线程间通信的手段。 你可以使用 threading 模块中的同步原函数。

此外，只允许主线程设置新的信号处理程序。

18.8.2. 模块内容

在 3.5 版更改: 信号（ SIG* ），处理程序（ SIG_DFL ， SIG_IGN）和 sigmask（ SIG_BLOCK ， SIG_UNBLOCK ， SIG_SETMASK ）下面列出的相关常量变成了 enums 。 getsignal() ， pthread_sigmask() ， sigpending() 和 sigwait() 函数返回人类可读的 enums 。

在 signal 模块中定义的变量是：

signal.SIG_DFL

这是两种标准信号处理选项之一；它只会执行信号的默认函数。 例如，在大多数系统上，对于 SIGQUIT 的默认操作是转储核心并退出，而对于 SIGCHLD 的默认操作是简单地忽略它。

signal.SIG_IGN

这是另一个标准信号处理程序，它将简单地忽略给定的信号。

SIG*

All the signal numbers are defined symbolically. For example, the hangup signal is defined as signal.SIGHUP; the variable names are identical to the names used in C programs, as found in <signal.h>. The Unix man page for ‘signal()’ lists the existing signals (on some systems this is signal(2), on others the list is in signal(7)). Note that not all systems define the same set of signal names; only those names defined by the system are defined by this module.

signal.CTRL_C_EVENT

对应于 Ctrl+C 击键事件的信号。此信号只能用于 os.kill() 。

Availability: Windows.

3.2 新版功能.

signal.CTRL_BREAK_EVENT

对应于 Ctrl+Break 击键事件的信号。此信号只能用于 os.kill() 。

Availability: Windows.

3.2 新版功能.

signal.NSIG

比最高信号数多一。

signal.ITIMER_REAL

实时递减间隔计时器，并在到期时发送 SIGALRM 。

signal.ITIMER_VIRTUAL

仅在进程执行时递减间隔计时器，并在到期时发送 SIGVTALRM 。

signal.ITIMER_PROF

当进程执行时以及当系统替进程执行时都会减小间隔计时器。 这个计时器与 ITIMER_VIRTUAL 相配结，通常被用于分析应用程序在用户和内核空间中花费的时间。 SIGPROF 会在超期时被发送。

signal.SIG_BLOCK

pthread_sigmask() 的 how 形参的一个可能的值，表明信号将会被阻塞。

3.3 新版功能.

signal.SIG_UNBLOCK

pthread_sigmask() 的 how 形参的是个可能的值，表明信号将被解除阻塞。

3.3 新版功能.

signal.SIG_SETMASK

pthread_sigmask() 的 how 形参的一个可能的值，表明信号掩码将要被替换。

3.3 新版功能.

signal 模块定义了一个异常:

exception signal.ItimerError

作为来自下层 setitimer() 或 getitimer() 实现错误的信号被引发。 如果将无效的定时器或负的时间值传给 setitimer() 就导致这个错误。 此错误是 OSError 的子类型。

3.3 新版功能: 此错误是 IOError 的子类型，现在则是 OSError 的别名。

signal 模块定义了以下函数:

signal.alarm(time)

If time is non-zero, this function requests that a SIGALRM signal be sent to the process in time seconds. Any previously scheduled alarm is canceled (only one alarm can be scheduled at any time). The returned value is then the number of seconds before any previously set alarm was to have been delivered. If time is zero, no alarm is scheduled, and any scheduled alarm is canceled. If the return value is zero, no alarm is currently scheduled. (See the Unix man page alarm(2).) Availability: Unix.

signal.getsignal(signalnum)

返回当前用于信号 signalnum 的信号处理程序。 返回值可以是一个 Python 可调用对象，或是特殊值 signal.SIG_IGN, signal.SIG_DFL 或 None 之一。 在这里，signal.SIG_IGN 表示信号在之前被忽略，signal.SIG_DFL 表示之前在使用默认的信号处理方式，而 None 表示之前的信号处理程序未由 Python 安装。

signal.pause()

Cause the process to sleep until a signal is received; the appropriate handler will then be called. Returns nothing. Not on Windows. (See the Unix man page signal(2).)

另请参阅 sigwait(), sigwaitinfo(), sigtimedwait() 和 sigpending()。

signal.pthread_kill(thread_id, signalnum)

Send the signal signalnum to the thread thread_id, another thread in the same process as the caller. The target thread can be executing any code (Python or not). However, if the target thread is executing the Python interpreter, the Python signal handlers will be executed by the main thread. Therefore, the only point of sending a signal to a particular Python thread would be to force a running system call to fail with InterruptedError.

使用 threading.get_ident() 或 threading.Thread 对象的 ident 属性为 thread_id 获取合适的值。

如果 signalnum 为 0，则不会发送信号，但仍然会执行错误检测；这可被用来检测目标线程是否仍在运行。

Availability: Unix (see the man page pthread_kill(3) for further information).

另请参阅 os.kill()。

3.3 新版功能.

signal.pthread_sigmask(how, mask)

获取和/或修改调用方线程的信号掩码。 信号掩码是一组传送过程目前为调用者而阻塞的信号集。 返回旧的信号掩码作为一组信号。

该调用的行为取决于 how 的值，具体见下。

SIG_BLOCK: 被阻塞的信号集是当前集与 mask 参数的并集。

SIG_UNBLOCK: mask 中的信号会从当前已阻塞信号集中被移除。 允许尝试取消对一个非阻塞信号的阻塞。

SIG_SETMASK: 已阻塞信号集会被设为 mask 参数的值。

mask is a set of signal numbers (e.g. {signal.SIGINT, signal.SIGTERM}). Use range(1, signal.NSIG) for a full mask including all signals.

例如，signal.pthread_sigmask(signal.SIG_BLOCK, []) 会读取调用方线程的信号掩码。

Availability: Unix. See the man page sigprocmask(3) and pthread_sigmask(3) for further information.

另请参阅 pause(), sigpending() 和 sigwait()。

3.3 新版功能.

signal.setitimer(which, seconds[, interval])

Sets given interval timer (one of signal.ITIMER_REAL, signal.ITIMER_VIRTUAL or signal.ITIMER_PROF) specified by which to fire after seconds (float is accepted, different from alarm()) and after that every interval seconds. The interval timer specified by which can be cleared by setting seconds to zero.

当一个间隔计时器启动时，会有信号发送至进程。 所发送的具体信号取决于所使用的计时器；signal.ITIMER_REAL 将发送 SIGALRM, signal.ITIMER_VIRTUAL 将发送 SIGVTALRM, 而 signal.ITIMER_PROF 将发送 SIGPROF.

原有的值会以元组: (delay, interval) 的形式被返回。

Attempting to pass an invalid interval timer will cause an ItimerError. Availability: Unix.

signal.getitimer(which)

Returns current value of a given interval timer specified by which. Availability: Unix.

signal.set_wakeup_fd(fd)

将唤醒文件描述符设为 fd。 当接收到信号时，会将信号编号以单个字节的形式写入 fd。 这可被其它库用来唤醒一次 poll 或 select 调用，以允许该信号被完全地处理。

原有的唤醒 fd 会被返回（或者如果未启用文件描述符唤醒则返回 -1）。 如果 fd 为 -1，文件描述符唤醒会被禁用。 如果不为 -1，则 fd 必须为非阻塞型。 需要由库来负责在重新调用 poll 或 select 之前从 fd 移除任何字节数据。

Use for example struct.unpack('%uB' % len(data), data) to decode the signal numbers list.

When threads are enabled, this function can only be called from the main thread; attempting to call it from other threads will cause a ValueError exception to be raised.

在 3.5 版更改: 在 Windows 上，此函数现在也支持套接字处理。

signal.siginterrupt(signalnum, flag)

Change system call restart behaviour: if flag is False, system calls will be restarted when interrupted by signal signalnum, otherwise system calls will be interrupted. Returns nothing. Availability: Unix (see the man page siginterrupt(3) for further information).

请注意用 signal() 安装信号处理程序将重启行为重置为可通过显式调用 siginterrupt() 并为给定信号的 flag 设置真值来实现中断。

signal.signal(signalnum, handler)

Set the handler for signal signalnum to the function handler. handler can be a callable Python object taking two arguments (see below), or one of the special values signal.SIG_IGN or signal.SIG_DFL. The previous signal handler will be returned (see the description of getsignal() above). (See the Unix man page signal(2).)

When threads are enabled, this function can only be called from the main thread; attempting to call it from other threads will cause a ValueError exception to be raised.

handler 将附带两个参数调用：信号编号和当前堆栈帧 (None 或一个帧对象；有关帧对象的描述请参阅 类型层级结构描述 或者参阅 inspect 模块中的属性描述)。

在 Windows 上，signal() 调用只能附带 SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM 或 SIGBREAK。 任何其他值都将引发 ValueError。 请注意不是所有系统都定义了同样的信号名称集合；如果一个信号名称未被定义为 SIG* 模块层级常量则将引发 AttributeError。

signal.sigpending()

检查正在等待传送给调用方线程的信号集合（即在阻塞期间被引发的信号）。 返回正在等待的信号集合。

Availability: Unix (see the man page sigpending(2) for further information).

另请参阅 pause(), pthread_sigmask() 和 sigwait()。

3.3 新版功能.

signal.sigwait(sigset)

挂起调用方线程的执行直到信号集合 sigset 中指定的信号之一被传送。 此函数会接受该信号（将其从等待信号列表中移除），并返回信号编号。

Availability: Unix (see the man page sigwait(3) for further information).

另请参阅 pause(), pthread_sigmask(), sigpending(), sigwaitinfo() 和 sigtimedwait()。

3.3 新版功能.

signal.sigwaitinfo(sigset)

挂起调用方线程的执行直到信号集合 sigset 中指定的信号之一被传送。 此函数会接受该信号并将其从等待信号列表中移除。 如果 sigset 中的信号之一已经在等待调用方线程，此函数将立即返回并附带有关该信号的信息。 被传送信号的信号处理程序不会被调用。 如果该函数被某个不在 sigset 中的信号中断则会引发 InterruptedError。

返回值是一个代表 siginfo_t 结构体所包含数据的对象，具体为: si_signo, si_code, si_errno, si_pid, si_uid, si_status, si_band。

Availability: Unix (see the man page sigwaitinfo(2) for further information).

另请参阅 pause(), sigwait() 和 sigtimedwait()。

3.3 新版功能.

在 3.5 版更改: 当被某个 不在 sigset 中的信号中断时本函数将结束并且信号处理程序不会引发异常 (其理由参见 PEP 475 [https://www.python.org/dev/peps/pep-0475])。

signal.sigtimedwait(sigset, timeout)

类似于 sigwaitinfo()，但会接受一个额外的 timeout 参数来指定超时限制。 如果将 timeout 指定为 0，则会执行轮询。 如果发生超时则返回 None。

Availability: Unix (see the man page sigtimedwait(2) for further information).

另请参阅 pause(), sigwait() 和 sigwaitinfo()。

3.3 新版功能.

在 3.5 版更改: 现在当此函数被某个不在 sigset 中的信号中断时将以计算出的 timeout 进行重试并且信号处理程序不会引发异常（请参阅 PEP 475 [https://www.python.org/dev/peps/pep-0475] 了解其理由）。

18.8.3. 示例

这是一个最小示例程序。 它使用 alarm() 函数来限制等待打开一个文件所花费的时间；这在文件为无法开启的串行设备时会很有用处，此情况通常会导致 os.open() 无限期地挂起。 解决办法是在打开文件之前设置 5 秒钟的 alarm；如果操作耗时过长，将会发送 alarm 信号，并且处理程序会引发一个异常。

import signal, os

def handler(signum, frame):
    print('Signal handler called with signal', signum)
    raise OSError("Couldn't open device!")

# Set the signal handler and a 5-second alarm
signal.signal(signal.SIGALRM, handler)
signal.alarm(5)

# This open() may hang indefinitely
fd = os.open('/dev/ttyS0', os.O_RDWR)

signal.alarm(0)          # Disable the alarm

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

18.9. mmap — 内存映射文件支持

内存映射（mmap）文件对象的行为既像 bytearray 又像 文件对象。 你可以在大部分接受 bytearray 的地方使用 mmap 对象；例如，你可以使用 re 模块来搜索一个内存映射文件。 你也可以通过执行 obj[index] = 97 来修改单个字节，或者通过对切片赋值来修改一个子序列: obj[i1:i2] = b'...'。 你还可以在文件的当前位置开始读取和写入数据，并使用 seek() 前往另一个位置。

内存映射文件是由 mmap 构造函数创建的，其在 Unix和 Windows上是不同的。 无论哪种情况，你都必须为一个打开的文件提供文件描述符以进行更新。 如果你希望映射一个已有的 Python 文件对象，请使用该对象的 fileno() 方法来获取 fileno 参数的正确值。 否则，你可以使用 os.open() 函数来打开这个文件，这会直接返回一个文件描述符（结束时仍然需要关闭该文件）。

注解

如果要为可写的缓冲文件创建内存映射，则应当首先 flush() 该文件。 这确保了对缓冲区的本地修改在内存映射中可用。

For both the Unix and Windows versions of the constructor, access may be specified as an optional keyword parameter. access accepts one of three values: ACCESS_READ, ACCESS_WRITE, or ACCESS_COPY to specify read-only, write-through or copy-on-write memory respectively. access can be used on both Unix and Windows. If access is not specified, Windows mmap returns a write-through mapping. The initial memory values for all three access types are taken from the specified file. Assignment to an ACCESS_READ memory map raises a TypeError exception. Assignment to an ACCESS_WRITE memory map affects both memory and the underlying file. Assignment to an ACCESS_COPY memory map affects memory but does not update the underlying file.

要映射匿名内存，应将 -1 作为 fileno 和 length 一起传递。

class mmap.mmap(fileno, length, tagname=None, access=ACCESS_DEFAULT[, offset])

（ Windows 版本） 映射被文件句柄 fileno 指定的文件的 length 个字节，并创建一个 mmap 对象。如果 length 大于当前文件大小，则文件将扩展为包含 length 个字节。如果 length 为 0，则映射的最大长度为当前文件大小。如果文件为空， Windows 会引发异常（你无法在Windows上创建空映射）。

如果 tagname 被指定且不是 None ，则是为映射提供标签名称的字符串。 Windows 允许你对同一文件拥有许多不同的映射。如果指定现有标签的名称，则会打开该标签，否则将创建该名称的新标签。如果省略此参数或设置为 None ，则创建的映射不带名称。避免使用 tag 参数将有助于使代码在Unix和Windows之间可移植。

offset 可以被指定为非负整数偏移量。 mmap 引用将相对于从文件开头的偏移。 offset 默认为0。 offeset 必须是 ALLOCATIONGRANULARITY 的倍数。

class mmap.mmap(fileno, length, flags=MAP_SHARED, prot=PROT_WRITE|PROT_READ, access=ACCESS_DEFAULT[, offset])

(Unix 版本) 映射文件描述符 fileno 指定的文件的 length 个字节，并返回一个 mmap 对象。如果 length 为 0 ，则当调用 mmap 时，映射的最大长度将为文件的当前大小。

flags 指明映射的性质。 MAP_PRIVATE 会创建私有的写入时拷贝映射，因此对 mmap 对象内容的修改将为该进程所私有，而 MAP_SHARED 会创建与其他映射同一文件区域的进程所共享的映射。 默认值为 MAP_SHARED。

如果指明了 prot，它将给出所需的内存保护方式；最有用的两个值是 PROT_READ 和 PROT_WRITE，分别指明页面为可读或可写。 prot 默认为 PROT_READ | PROT_WRITE。

可以指定 access 作为替代 flags 和 prot 的可选关键字形参。 同时指定 flags, prot 和 access 将导致错误。 请参阅上文中 access 的描述了解有关如何使用此形参的信息。

offset 可以被指定为非负整数偏移量。 mmap 引用将相对于从文件开头的偏移。 offset 默认为 0。 offset 必须是 ALLOCATIONGRANULARITY 的倍数，它在 Unix 系统上等价于 PAGESIZE。

要确保已创建内存映射的有效性，描述符 fileno 所指定的文件在 Mac OS X 和 OpenVMS 上会与物理后备存储进行内部自动同步。

这个例子演示了使用 mmap 的简单方式:

import mmap

# write a simple example file
with open("hello.txt", "wb") as f:
    f.write(b"Hello Python!\n")

with open("hello.txt", "r+b") as f:
    # memory-map the file, size 0 means whole file
    mm = mmap.mmap(f.fileno(), 0)
    # read content via standard file methods
    print(mm.readline())  # prints b"Hello Python!\n"
    # read content via slice notation
    print(mm[:5])  # prints b"Hello"
    # update content using slice notation;
    # note that new content must have same size
    mm[6:] = b" world!\n"
    # ... and read again using standard file methods
    mm.seek(0)
    print(mm.readline())  # prints b"Hello  world!\n"
    # close the map
    mm.close()

mmap 也可以在 with 语句中被用作上下文管理器:

import mmap

with mmap.mmap(-1, 13) as mm:
    mm.write(b"Hello world!")

3.2 新版功能: 上下文管理器支持。

下面的例子演示了如何创建一个匿名映射并在父进程和子进程之间交换数据。:

import mmap
import os

mm = mmap.mmap(-1, 13)
mm.write(b"Hello world!")

pid = os.fork()

if pid == 0:  # In a child process
    mm.seek(0)
    print(mm.readline())

    mm.close()

映射内存的文件对象支持以下方法:

close()

关闭 mmap。 后续调用该对象的其他方法将导致引发 ValueError 异常。 此方法将不会关闭打开的文件。

closed

如果文件已关闭则返回 True。

3.2 新版功能.

find(sub[, start[, end]])

返回子序列 sub 在对象内被找到的最小索引号，使得 sub 被包含在 [start, end] 范围中。 可选参数 start 和 end 会被解读为切片表示法。 如果未找到则返回 -1。

在 3.5 版更改: 现在支持可写的 字节类对象。

flush([offset[, size]])

将对文件的内存副本的修改刷新至磁盘。 如果不使用此调用则无法保证在对象被销毁前将修改写回存储。 如果指定了 offset 和 size，则只将对指定范围内字节的修改刷新至磁盘；在其他情况下，映射的全部范围都会被刷新。 offset 必须为 PAGESIZE 或 ALLOCATIONGRANULARITY 的倍数。

(Windows version) A nonzero value returned indicates success; zero indicates failure.

(Unix 版本) 返回零值以表示成功。 当调用失败时将引发异常。

move(dest, src, count)

将从偏移量 src 开始的 count 个字节拷贝到目标索引号 dest。 如果 mmap 创建时设置了 ACCESS_READ，则调用 move 将引发 TypeError 异常。

read([n])

返回一个 bytes，其中包含从当前文件位置开始的至多 n 个字节。 如果参数省略，为 None 或负数，则返回从当前文件位置开始直至映射结尾的所有字节。 文件位置会被更新为返回字节数据之后的位置。

在 3.3 版更改: 参数可被省略或为 None。

read_byte()

将当前文件位置上的一个字节以整数形式返回，并让文件位置前进 1。

readline()

Returns a single line, starting at the current file position and up to the next newline.

resize(newsize)

改变映射以及下层文件的大小，如果存在的话。 如果 mmap 创建时设置了 ACCESS_READ 或 ACCESS_COPY，则改变映射大小将引发 TypeError 异常。

rfind(sub[, start[, end]])

返回子序列 sub 在对象内被找到的最大索引号，使得 sub 被包含在 [start, end] 范围中。 可选参数 start 和 end 会被解读为切片表示法。 如果未找到则返回 -1。

在 3.5 版更改: 现在支持可写的 字节类对象。

seek(pos[, whence])

设置文件的当前位置。 whence 参数为可选项并且默认为 os.SEEK_SET 或 0 (绝对文件定位)；其他值还有 os.SEEK_CUR 或 1 (相对当前位置查找) 和 os.SEEK_END 或 2 (相对文件末尾查找)。

size()

返回文件的长度，该数值可以大于内存映射区域的大小。

tell()

返回文件指针的当前位置。

write(bytes)

将 bytes 中的字节数据写入文件指针当前位置的内存并返回写入的字节总数 (一定不小于 len(bytes)，因为如果写入失败，将会引发 ValueError)。 在字节数据被写入后文件位置将会更新。 如果 mmap 创建时设置了 ACCESS_READ，则向其写入将引发 TypeError 异常。

在 3.5 版更改: 现在支持可写的 字节类对象。

在 3.6 版更改: 现在会返回写入的字节总数。

write_byte(byte)

将整数值 byte 写入文件指针当前位置的内存；文件位置前进 1。 如果 mmap 创建时设置了 ACCESS_READ，则向其写入将引发 TypeError 异常。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

18. Interprocess Communication and Networking »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

19. 互联网数据处理

本章介绍了支持处理互联网上常用数据格式的模块。

19.1. email — 电子邮件与 MIME 处理包

19.1.1. email.message: 表示一封电子邮件信息

19.1.2. email.parser: 解析电子邮件信息

19.1.2.1. FeedParser API

19.1.2.2. Parser API

19.1.2.3. 附加说明

19.1.3. email.generator: 生成 MIME 文档

19.1.4. email.policy: 策略对象

19.1.5. email.errors: 异常和缺陷类

19.1.6. email.headerregistry: 自定义标头对象

19.1.7. email.contentmanager: 管理 MIME 内容

19.1.7.1. 内容管理器实例

19.1.8. email: 示例

19.1.9. email.message.Message: 使用 compat32 API 来表示电子邮件消息

19.1.10. email.mime: Creating email and MIME objects from scratch

19.1.11. email.header: Internationalized headers

19.1.12. email.charset: Representing character sets

19.1.13. email.encoders: 编码器

19.1.14. email.utils: 其他工具

19.1.15. email.iterators: 迭代器

19.2. json — JSON 编码和解码器

19.2.1. 基本使用

19.2.2. 编码器和解码器

19.2.3. 异常

19.2.4. 标准符合性和互操作性

19.2.4.1. 字符编码

19.2.4.2. Infinite 和 NaN 数值。

19.2.4.3. 对象中的重复名称

19.2.4.4. 顶级非对象，非数组值

19.2.4.5. 实现限制

19.2.5. 命令行界面

19.2.5.1. 命令行选项

19.3. mailcap — Mailcap 文件处理

19.4. mailbox — Manipulate mailboxes in various formats

19.4.1. Mailbox 对象

19.4.1.1. Maildir

19.4.1.2. mbox

19.4.1.3. MH

19.4.1.4. Babyl

19.4.1.5. MMDF

19.4.2. Message objects

19.4.2.1. MaildirMessage

19.4.2.2. mboxMessage

19.4.2.3. MHMessage

19.4.2.4. BabylMessage

19.4.2.5. MMDFMessage

19.4.3. 异常

19.4.4. 例子

19.5. mimetypes — Map filenames to MIME types

19.5.1. MimeTypes Objects

19.6. base64 — Base16, Base32, Base64, Base85 数据编码

19.7. binhex — 对binhex4文件进行编码和解码

19.7.1. 注释

19.8. binascii — 二进制和 ASCII 码互转

19.9. quopri — 编码与解码经过 MIME 转码的可打印数据

19.10. uu — 对 uuencode 文件进行编码与解码

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

19.1. email — 电子邮件与 MIME 处理包

源码: Lib/email/__init__.py [https://github.com/python/cpython/tree/3.6/Lib/email/__init__.py]

email 包是一个用于管理电子邮件消息的库。 它 并非 被设计为执行向 SMTP (RFC 2821 [https://tools.ietf.org/html/rfc2821.html]), NNTP 或其他服务器发送电子邮件消息的操作；这些是 smtplib 和 nntplib 等模块的功能。 email 包试图尽可能遵循 RFC，支持 RFC 5233 [https://tools.ietf.org/html/rfc5233.html] 和 RFC 6532 [https://tools.ietf.org/html/rfc6532.html]，以及与 MIME 相关的各个 RFC 例如 RFC 2045 [https://tools.ietf.org/html/rfc2045.html], RFC 2046 [https://tools.ietf.org/html/rfc2046.html], RFC 2047 [https://tools.ietf.org/html/rfc2047.html], RFC 2183 [https://tools.ietf.org/html/rfc2183.html] 和 RFC 2231 [https://tools.ietf.org/html/rfc2231.html]。

email 包的总体结构可以分为三个主要部分，另外还有第四个部分用于控制其他部分的行为。

这个包的中心组件是代表电子邮件消息的“对象模型”。 应用程序主要通过在 message 子模块中定义的对象模型接口与这个包进行交互。 应用程序可以使用此 API 来询问有关现有电子邮件的问题、构造新的电子邮件，或者添加或移除自身也使用相同对象模型接口的电子邮件子组件。 也就是说，遵循电子邮件消息及其 MIME 子组件的性质，电子邮件对象模型是所有提供 EmailMessage API 的对象所构成的树状结构。

这个包的另外两个主要组件是 parser 和 generator。 parser 接受电子邮件消息的序列化版本（字节流）并将其转换为 EmailMessage 对象树。 generator 接受 EmailMessage 并将其转回序列化的字节流。 (parser 和 generator 还能处理文本字符流，但不建议这种用法，因为这很容易导致某种形式的无效消息。

控制组件是 policy 模块。 每一个 EmailMessage、每一个 generator 和每一个 parser 都有一个相关联的 policy 对象来控制其行为。 通常应用程序只有在 EmailMessage 被创建时才需要指明控制策略，或者通过直接实例代 EmailMessage 来新建电子邮件，或者通过使用 parser 来解析输入流。 但是策略也可以在使用 generator 序列化消息时被更改。 例如，这允许从磁盘解析通用电子邮件消息，而在将消息发送到电子邮件服务器时使用标准 SMTP 设置对其进行序列化。

email 包会尽量地对应用程序隐藏各种控制类 RFC 的细节。 从概念上讲应用程序应当能够将电子邮件消息视为 Unicode 文本和二进制附件的结构化树，而不必担心在序列化时要如何表示它们。 但在实际中，经常有必要至少了解一部分控制类 MIME 消息及其结构的规划，特别是 MIME “内容类型” 的名称和性质以及它们是如何标识多部分文档的。 在大多数情况下这些知识应当仅对于更复杂的应用程序来说才是必需的，并且即便在那时它也应当仅是特定的高层级结构，而不是如何表示这些结构的细节信息。 由于 MIME 内容类型被广泛应用于现代因特网软件（而非只是电子邮件），因此这对许多程序员来说将是很熟悉的概念。

以下小节描述了 email 包的具体功能。 我们会从 message 对象模型开始，它是应用程序将要使用的主要接口，之后是 parser 和 generator 组件。 然后我们会介绍 policy 控制组件，它将完成对这个库的主要组件的处理。

接下来的三个小节会介绍这个包可能引发的异常以及 parser 可能检测到的缺陷（即与 RFC 不相符）。 然后我们会介绍 headerregistry 和 contentmanager 子组件，它们分别提供了用于更精细地操纵标题和载荷的工具。 这两个组件除了包含使用与生成非简单消息的相关特性，还记录了它们的可扩展性 API，这将是高级应用程序所感兴趣的内容。

在此之后是一组使用之前小节所介绍的 API 的基本部分的示例。

前面的内容是 email 包的现代（对 Unicode 支持良好）API。 从 Message 类开始的其余小节则介绍了旧式 compat32 API，它会更直接地处理如何表示电子邮件消息的细节。 compat32 API 不会 向应用程序隐藏 RFC 的相关细节，但对于需要进行此种层级操作的应用程序来说将是很有用的工具。 此文档对于因向下兼容理由而仍然使用 compat32 API 的应用程序也是很适合的。

在 3.6 版更改: 文档经过重新组织和撰写以鼓励使用新的 EmailMessage/EmailPolicy API。

email 包文档的内容:

19.1.1. email.message: 表示一封电子邮件信息

19.1.2. email.parser: 解析电子邮件信息

19.1.2.1. FeedParser API

19.1.2.2. Parser API

19.1.2.3. 附加说明

19.1.3. email.generator: 生成 MIME 文档

19.1.4. email.policy: 策略对象

19.1.5. email.errors: 异常和缺陷类

19.1.6. email.headerregistry: 自定义标头对象

19.1.7. email.contentmanager: 管理 MIME 内容

19.1.7.1. 内容管理器实例

19.1.8. email: 示例

旧式 API:

19.1.9. email.message.Message: 使用 compat32 API 来表示电子邮件消息

19.1.10. email.mime: Creating email and MIME objects from scratch

19.1.11. email.header: Internationalized headers

19.1.12. email.charset: Representing character sets

19.1.13. email.encoders: 编码器

19.1.14. email.utils: 其他工具

19.1.15. email.iterators: 迭代器

参见

smtplib 模块

SMTP (简单邮件传输协议) 客户端

poplib 模块

POP (邮局协议) 客户端

imaplib 模块

IMAP (互联网消息访问协议) 客户端

nntplib 模块

NNTP (网络新闻传输协议) 客户端

mailbox 模块

创建、读取和管理使用保存在磁盘中的多种标准格式的消息集的工具。

smtpd 模块

SMTP 服务器框架 (主要适用于测试)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

19.1.1. email.message: 表示一封电子邮件信息

源代码: Lib/email/message.py [https://github.com/python/cpython/tree/3.6/Lib/email/message.py]

3.6 新版功能: 1

位于 email 包的中心的类就是 EmailMessage 类。这个类导入自 email.message 模块。它是 email 对象模型的基类。EmailMessage 为设置和查询头字段内容、访问信息体的内容、以及创建和修改结构化信息提供了核心功能。

一份电子邮件信息由*头*和*负载*（又被称为*内容*）组成。头遵循 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 或者 RFC 6532 [https://tools.ietf.org/html/rfc6532.html] 风格的字段名和值，字段名和字段值之间由一个冒号隔开。这个冒号既不属于字段名，也不属于字段值。信息的负载可能是一段简单的文字消息，也可能是一个二进制的对象，更可能是由多个拥有各自头和负载的子信息组成的结构化子信息序列。对于后者类型的负载，信息的 MIME 类型将会被指明为诸如 multipart/* 或 message/rfc822 的类型。

EmailMessage 对象所提供的抽象概念模型是一个头字段组成的有序字典加一个代表 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 标准的信息体的*负载*。负载有可能是一系列子``EmailMessage``对象的列表。你除了可以通过一般的字典方法来访问头字段名和值，还可以使用特制方法来访问头的特定字段（比如说 MIME 内容类型字段）、操纵负载、生成信息的序列化版本、递归遍历对象树。

EmailMessage 的类字典接口的字典索引是头字段名，头字段名必须是ASCII值。字典值是带有一些附加方法的字符串。虽然头字段的存储和获取都是保留其原始大小写的，但是字段名的匹配是大小写不敏感的。与真正的字典不同，键与键之间不但存在顺序关系，还可以重复。我们提供了额外的方法来处理含有重复键的头。

payload 是多样的。 对于简单的消息对象，它是字符串或字节串对象；对于诸如 multipart/* 和 message/rfc822 消息对象的 MIME 容器文档，它是一个 EmailMessage 对象列表。

class email.message.EmailMessage(policy=default)

如果指定了*policy*，消息将由这个*policy*所指定的规则来更新和序列化信息的表达。如果没有指定*policy*，其将默认使用 default 策略。这个策略遵循电子邮件的RFC标准，除了行终止符号（RFC要求使用``rn``，此策略使用Python标准的``n``行终止符）。请前往 policy 的文档获取更多信息。

as_string(unixfrom=False, maxheaderlen=None, policy=None)

以一段字符串的形式返回整个消息对象。 若可选的 unixform 参数为真，返回的字符串会包含信封头。 unixform 的默认值是 False。 为了保持与基类 Message 的兼容性，maxheaderlen 是被接受的，但是其默认值是 None。 这个默认值表示行长度由策略的 max_line_length 属性所控制。从信息实例所获取到的策略可以通过 policy 参数重写。 这样可以对该方法所产生的输出进行略微的控制，因为指定的 policy 会被传递到 Generator 当中。

扁平化信息可能会对 EmailMessage 做出修改。这是因为为了完成向字符串的转换，一些内容需要使用默认值填入（举个例子，MIME 边界字段可能会被生成或被修改）。

请注意，这个方法是为了便利而提供，不一定是适合你的应用程序的最理想的序列化信息的方法。这在你处理多封信息的时候尤甚。如果你需要使用更加灵活的API来序列化信息，请参见 email.generator.Generator 。同时请注意，当 utf8 属性为``False``的时候（这是默认值），本方法将限制其行为为生成以“7 bit clean”方式序列化的信息。

在 3.6 版更改: maxheaderlen*没有被指定时的默认行为从默认为0修改为默认为策略的*max_line_length。

__str__()

与``as_string(policy=self.policy.clone(utf8=True))``等价。这将让``str(msg)``产生的字符串包含人类可读的的序列化信息内容。

在 3.4 版更改: 本方法开始使用``utf8=True``，而非 as_string() 的直接替身。使用``utf8=True``会产生类似于 RFC 6531 [https://tools.ietf.org/html/rfc6531.html] 的信息表达。

as_bytes(unixfrom=False, policy=None)

以字节串对象的形式返回整个扁平化后的消息。 当可选的 unixfrom 为真值时，返回的字符串会包含信封标头。 unixfrom 的默认值为 False。 policy 参数可被用于重载从消息实例获取的默认 policy。 这可被用来控制该方法所产生的部分格式效果，因为指定的 policy 将被传递给 BytesGenerator。

扁平化信息可能会对 EmailMessage 做出修改。这是因为为了完成向字符串的转换，一些内容需要使用默认值填入（举个例子，MIME 边界字段可能会被生成或被修改）。

请注意，这个方法是为了便利而提供，不一定是适合你的应用程序的最理想的序列化信息的方法。这在你处理多封信息的时候尤甚。如果你需要使用更加灵活的API来序列化信息，请参见 email.generator.BytesGenerator 。

__bytes__()

与 as_bytes() 等价。这将让``bytes(msg)``产生一个包含序列化信息内容的字节序列对象。

is_multipart()

如果该信息的负载是一个子 EmailMessage 对象列表，返回 True ；否则返回 False 。在 is_multipart() 返回 True 的场合下，负载应当是一个字符串对象（有可能是一个使用了内容传输编码进行编码的二进制负载）。请注意， is_multipart() 返回 True 不意味着 msg.get_content_maintype() == 'multipart' 也会返回 True 。举个例子， is_multipart 在 EmailMessage 是 message/rfc822 类型的信息的情况下，其返回值也是 True 。

set_unixfrom(unixfrom)

将信息的信封头设置为 unixform ，这应当是一个字符串。（在 mboxMessage 中有关于这个头的一段简短介绍。）

get_unixfrom()

返回消息的信封头。如果信封头从未被设置过，默认返回 None 。

以下方法实现了对信息的头字段进行访问的类映射接口。请留意，只是类映射接口，这与平常的映射接口（比如说字典映射）有一些语义上的不同。举个例子，在一个字典当中，键之间不可重复，但是信息头字段是可以重复的。不光如此，在字典当中调用 keys() 方法返回的结果，其顺序没有保证；但是在一个 EmailMessage 对象当中，返回的头字段永远以其在原信息当中出现的顺序，或以其加入信息的顺序为序。任何删了后又重新加回去的头字段总是添加在当时列表的末尾。

这些语义上的不同是刻意而为之的，是出于在绝大多数常见使用情景中都方便的初衷下设计的。

还请留意，无论在什么情况下，消息当中的任何信封头字段都不会包含在映射接口当中。

__len__()

返回头字段的总数，重复的也计算在内。

__contains__(name)

Return true if the message object has a field named name. Matching is done without regard to case and name does not include the trailing colon. Used for the in operator. For example:

if 'message-id' in myMessage:
   print('Message-ID:', myMessage['message-id'])

__getitem__(name)

返回头字段名对应的字段值。 name 不含冒号分隔符。如果字段未找到，返回 None 。 KeyError 异常永不抛出。

请注意，如果对应名字的字段找到了多个，具体返回哪个字段值是未定义的。请使用 get_all() 方法获取匹配字段名的所有字段值。

使用标准策略（非 compat32）时，返回值是 email.headerregistry.BaseHeader 的某个子类的一个实例。

__setitem__(name, val)

在信息头中添加名为 name 值为 val 的字段。这个字段会被添加在已有字段列表的结尾处。

请注意，这个方法 既不会 覆盖 也不会 删除任何字段名重名的已有字段。如果你确实想保证新字段是整个信息头当中唯一拥有 name 字段名的字段，你需要先把旧字段删除。例如：

del msg['subject']
msg['subject'] = 'Python roolz!'

如果 policy 明确要求某些字段是唯一的（至少标准策略就有这么做），对这些字段在已有同名字段的情况下仍然尝试为字段名赋值会引发 ValueError 异常。这是为了一致性而刻意设计出的行为，不过我们随时可能会突然觉得“还是在这种情况下自动把旧字段删除比较好吧”而把这个行为改掉，所以不要以为这是特性而依赖这个行为。

__delitem__(name)

删除信息头当中字段名匹配 name 的所有字段。如果匹配指定名称的字段没有找到，也不会抛出任何异常。

keys()

以列表形式返回消息头中所有的字段名。

values()

以列表形式返回消息头中所有的字段值。

items()

以二元元组的列表形式返回消息头中所有的字段名和字段值。

get(name, failobj=None)

返回对应字段名的字段值。这个方法与 __getitem__() 是一样的，只不过如果对应字段名的字段没有找到，该方法会返回 failobj 。这个参数是可选的（默认值为 None ）。

以下是一些与头有关的更多有用方法：

get_all(name, failobj=None)

返回字段名为 name 的所有字段值的列表。如果信息内不存在匹配的字段，返回 failobj （其默认值为 None ）。

add_header(_name, _value, **_params)

高级头字段设定。这个方法与 __setitem__() 类似，不过你可以使用关键字参数为字段提供附加参数。 _name 是字段名， _value 是字段 主 值。

对于关键字参数字典 _params 的每个键值对而言，它的键被用作参数的名字，其中下划线被替换为短横杠（毕竟短横杠不是合法的Python标识符）。一般来讲，参数以 键="值" 的方式添加，除非值是 None 。要真的是这样的话，只有键会被添加。

如果值含有非ASCII字符，你可以将值写成 (CHARSET, LANGUAGE, VALUE) 形式的三元组，这样你可以人为控制字符的字符集和语言。 CHARSET 是一个字符串，它为你的值的编码命名； LANGUAGE 一般可以直接设为 None ，也可以直接设为空字符串（其他可能取值参见 :rfc`2231` ）； `VALUE 是一个字符串值，其包含非ASCII的码点。如果你没有使用三元组，你的字符串又含有非ASCII字符，那么它就会使用 RFC 2231 [https://tools.ietf.org/html/rfc2231.html] 中， CHARSET 为 utf-8 ， LANGUAGE 为 None 的格式编码。

例如：

msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')

会添加一个形如下文的头字段：

Content-Disposition: attachment; filename="bud.gif"

带有非ASCII字符的拓展接口：

msg.add_header('Content-Disposition', 'attachment',
               filename=('iso-8859-1', '', 'Fußballer.ppt'))

replace_header(_name, _value)

替换头字段。只会替换掉信息内找到的第一个字段名匹配 _name 的字段值。字段的顺序不变，原字段名的大小写也不变。如果没有找到匹配的字段，抛出 KeyError 异常。

get_content_type()

返回信息的内容类型，其形如 maintype/subtype ，强制全小写。如果信息的 Content-Type 头字段不存在则返回 get_default_type() 的返回值；如果信息的 Content-Type 头字段无效则返回 text/plain 。

（根据 RFC 2045 [https://tools.ietf.org/html/rfc2045.html] 所述，信息永远都有一个默认类型，所以 get_content_type() 一定会返回一个值。 RFC 2045 [https://tools.ietf.org/html/rfc2045.html] 定义信息的默认类型为 text/plain 或 message/rfc822 ，其中后者仅出现在消息头位于一个 multipart/digest 容器中的场合中。如果消息头的 Content-Type 字段所指定的类型是无效的， RFC 2045 [https://tools.ietf.org/html/rfc2045.html] 令其默认类型为 text/plain 。）

get_content_maintype()

返回信息的主要内容类型。准确来说，此方法返回的是 get_content_type() 方法所返回的形如 maintype/subtype 的字符串当中的 maintype 部分。

get_content_subtype()

返回信息的子内容类型。准确来说，此方法返回的是 get_content_type() 方法所返回的形如 maintype/subtype 的字符串当中的 subtype 部分。

get_default_type()

返回默认的内容类型。绝大多数的信息，其默认内容类型都是 text/plain 。作为 multipart/digest 容器内子部分的信息除外，它们的默认内容类型是 message/rfc822 。

set_default_type(ctype)

设置默认的内容类型。 尽管并非强制，但是 ctype 仍应当是 text/plain 或 message/rfc822 二者取一。默认内容类型并不存储在 Content-Type 头字段当中，所以设置此项的唯一作用就是决定当 Content-Type 头字段在信息中不存在时，get_content_type 方法的返回值。

set_param(param, value, header='Content-Type', requote=True, charset=None, language='', replace=False)

在 Content-Type 头字段当中设置一个参数。如果该参数已于字段中存在，将其旧值替换为 value 。如果 header 是 Content-Type （默认值），并且该头字段于信息中尚未存在，则会先添加该字段，将其值设置为 text/plain ，并附加参数值。可选的 header 可以让你指定 Content-Type 之外的另一个头字段。

如果值包含非ASCII字符，其字符集和语言可以通过可选参数 charset 和 language 显式指定。可选参数 language 指定 RFC 2231 [https://tools.ietf.org/html/rfc2231.html] 当中的语言，其默认值是空字符串。 charset 和 language 都应当字符串。默认使用的是 utf8 charset ，language 为 None 。

如果 replace 为 False （默认值），该头字段会被移动到所有头字段的末尾。如果 replace 为 True ，字段会被原地更新。

于 EmailMessage 对象而言， requote 参数已被弃用。

请注意，头字段已有的参数值可以通过头字段的 params 属性来访问（举例： msg['Content-Type'].params['charset'] ）。

在 3.4 版更改: 添加了 replace 关键字。

del_param(param, header='content-type', requote=True)

从 Content-Type 头字段中完全移去给定的参数。头字段会被原地重写，重写后的字段不含参数和值。可选的 header 可以让你指定 Content-Type 之外的另一个字段。

于 EmailMessage 对象而言， requote 参数已被弃用。

get_filename(failobj=None)

返回信息头当中 Content-Disposition 字段当中名为 filename 的参数值。如果该字段当中没有此参数，该方法会退而寻找 Content-Type 字段当中的 name 参数值。如果这个也没有找到，或者这些个字段压根就不存在，返回 failobj 。返回的字符串永远按照 email.utils.unquote() 方法去除引号。

get_boundary(failobj=None)

返回信息头当中 Content-Type 字段当中名为 boundary 的参数值。如果字段当中没有此参数，或者这些个字段压根就不存在，返回 failobj 。返回的字符串永远按照 email.utils.unquote() 方法去除引号。

set_boundary(boundary)

将 Content-Type 头字段的 boundary 参数设置为 boundary 。 set_boundary() 方法永远都会在必要的时候为 boundary 添加引号。如果信息对象中没有 Content-Type 头字段，抛出 HeaderParseError 异常。

请注意使用这个方法与直接删除旧的 Content-Type 头字段然后使用 add_header() 方法添加一个带有新边界值参数的 Content-Type 头字段有细微差距。 set_boundary() 方法会保留 Content-Type 头字段在原信息头当中的位置。

get_content_charset(failobj=None)

返回 Content-Type 头字段中的 charset 参数，强制小写。如果字段当中没有此参数，或者这个字段压根不存在，返回 failobj 。

get_charsets(failobj=None)

返回一个包含了信息内所有字符集名字的列表。如果信息是 multipart 类型的，那么列表当中的每一项都对应其负载的子部分的字符集名字。否则，该列表是一个长度为1的列表。

列表当中的每一项都是一个字符串，其值为对应子部分的 Content-Type 头字段的 charset 参数值。如果该子部分没有此头字段，或者没有此参数，或者其主要 MIME 类型并非 text ，那么列表中的那一项即为 failobj 。

is_attachment()

如果信息头当中存在一个名为 Content-Disposition 的字段，且该字段的值为 attachment （大小写无关），返回 True 。否则，返回 False 。

在 3.4.2 版更改: 为了与 is_multipart() 方法一致，is_attachment 现在是一个方法，不再是属性了。

get_content_disposition()

如果信息的 Content-Disposition 头字段存在，返回其字段值；否则返回 None 。返回的值均为小写，不包含参数。如果信息遵循 RFC 2183 [https://tools.ietf.org/html/rfc2183.html] 标准，则返回值只可能在 inline 、 attachment 和 None 之间选择。

3.5 新版功能.

下列方法与信息内容（负载）之访问与操控有关。

walk()

walk() 方法是一个多功能生成器。它可以被用来以深度优先顺序遍历信息对象树的所有部分和子部分。一般而言， walk() 会被用作 for 循环的迭代器，每一次迭代都返回其下一个子部分。

以下例子会打印出一封具有多部分结构之信息的每个部分的 MIME 类型。

>>> for part in msg.walk():
...     print(part.get_content_type())
multipart/report
text/plain
message/delivery-status
text/plain
text/plain
message/rfc822
text/plain

walk 会遍历所有 is_multipart() 方法返回 True 的部分之子部分，哪怕 msg.get_content_maintype() == 'multipart' 返回的是 False 。使用 _structure 除错帮助函数可以帮助我们在下面这个例子当中看清楚这一点：

>>> for part in msg.walk():
...     print(part.get_content_maintype() == 'multipart',
...           part.is_multipart())
True True
False False
False True
False False
False False
False True
False False
>>> _structure(msg)
multipart/report
    text/plain
    message/delivery-status
        text/plain
        text/plain
    message/rfc822
        text/plain

在这里， message 的部分并非 multiparts ，但是它们真的包含子部分！ is_multipart() 返回 True ， walk 也深入进这些子部分中。

get_body(preferencelist=('related', 'html', 'plain'))

返回信息的 MIME 部分。这个部分是最可能成为信息体的部分。

preferencelist 必须是一个字符串序列，其内容从 related 、 html 和 plain 这三者组成的集合中选取。这个序列代表着返回的部分的内容类型之偏好。

在 get_body 方法被调用的对象上寻找匹配的候选者。

如果 related 未包括在 preferencelist 中，可考虑将所遇到的任意相关的根部分（或根部分的子部分）在该（子）部分与一个首选项相匹配时作为候选项。

当遇到一个 multipart/related 时，将检查 start 形参并且如果找到了一个匹配 Content-ID 的部分，在查找候选匹配时只考虑它。 在其他情况下则只考虑 multipart/related 的第一个（默认的根）部分。

如果一个部分具有 Content-Disposition 标头，则当标头值为 inline 时将只考虑将该部分作为候选匹配。

如果没有任何候选部分匹配 preferencelist 中的任何首选项，则返回 None。

注: (1) 对于大多数应用来说有意义的 preferencelist 组合仅有 ('plain',), ('html', 'plain') 以及默认的 ('related', 'html', 'plain')。 (2) 由于匹配是从调用 get_body 的对象开始的，因此在 multipart/related 上调用 get_body 将返回对象本身，除非 preferencelist 具有非默认值。 (3) 未指定 Content-Type 或者 Content-Type 标头无效的消息（或消息部分）将被当作具有 text/plain 类型来处理，这有时可能导致 get_body 返回非预期的结果。

iter_attachments()

返回包含所有不是候选 “body” 部分的消息的直接子部分的迭代器。 也就是说，跳过首次出现的每个 text/plain, text/html, multipart/related 或 multipart/alternative (除非通过 Content-Disposition: attachment 将它们显式地标记为附件)，并返回所有的其余部分。 当直接应用于 multipart/related 时，将返回包含除根部分之外所有相关部分的迭代器（即由 start 形参所指向的部分，或者当没有 start 形参或 start 形参不能匹配任何部分的 Content-ID 时则为第一部分）。 当直接应用于 multipart/alternative 或非 multipart 时，将返回一个空迭代器。

iter_parts()

返回包含消息的所有直接子部分的迭代器，对于非 multipart 将为空对象。 （另请参阅 walk()。）

get_content(*args, content_manager=None, **kw)

调用 content_manager 的 get_content() 方法，将自身作为消息对象传入，并将其他参数或关键字作为额外参数传入。 如果未指定 content_manager，则会使用当前 policy 所指定的 content_manager。

set_content(*args, content_manager=None, **kw)

调用 content_manager 的 set_content() 方法，将自身作为消息传入，并将其他参数或关键字作为额外参数传入。 如果未指定 content_manager，则会使用当前 policy 所指定的 content_manager。

make_related(boundary=None)

将非 multipart 消息转换为 multipart/related 消息，将任何现有的 Content- 标头和载荷移入 multipart 的（新加）首部分。 如果指定了 boundary，会用它作为 multipart 中的分界字符串，否则会在必要时自动创建分界（例如当消息被序列化时）。

make_alternative(boundary=None)

将非 multipart 或 multipart/related 转换为 multipart/alternative，将任何现有的 Content- 标头和载荷移入 multipart 的（新加）首部分。 如果指定了 boundary，会用它作为 multipart 中的分界字符串，否则会在必要时自动创建分界（例如当消息被序列化时）。

make_mixed(boundary=None)

将非 multipart, multipart/related 或 multipart-alternative 转换为 multipart/mixed，将任何现有的 Content- 标头和载荷移入 multipart 的（新加）首部分。 如果指定了 boundary，会用它作为 multipart 中的分界字符串，否则会在必要时自动创建分界（例如当消息被序列化时）。

add_related(*args, content_manager=None, **kw)

如果消息为 multipart/related，则创建一个新的消息对象，将所有参数传给其 set_content() 方法，并将其 attach() 到 multipart。 如果消息为非 multipart，则先调用 make_related() 然后再继续上述步骤。 如果消息为任何其他类型的 multipart，则会引发 TypeError。 如果未指定 content_manager，则使用当前 policy 所指定的 content_manager。 如果添加的部分没有 Content-Disposition 标头，则会添加一个值为 inline 的标头。

add_alternative(*args, content_manager=None, **kw)

如果消息为 multipart/alternative，则创建一个新的消息对象，将所有参数传给其 set_content() 方法，并将其 attach() 到 multipart。 如果消息为非 multipart 或 multipart/related，则先调用 make_alternative() 然后再继续上述步骤。 如果消息为任何其他类型的 multipart，则会引发 TypeError。 如果未指定 content_manager，则会使用当前 policy 所指定的 content_manager。

add_attachment(*args, content_manager=None, **kw)

如果消息为 multipart/mixed，则创建一个新的消息对象，将所有参数传给其 set_content() 方法，并将其 attach() 到 multipart。 如果消息为非 multipart, multipart/related 或 multipart/alternative，则先调用 make_mixed() 然后再继续上述步骤。 如果未指定 content_manager，则使用当前 policy 所指定的 content_manager。 如果添加的部分没有 Content-Disposition 标头，则会添加一个值为 attachment 的标头。 此方法对于显式附件 (Content-Disposition: attachment) 和 inline 附件 (Content-Disposition: inline) 均可使用，只须向 content_manager 传入适当的选项即可。

clear()

移除所有载荷和所有标头。

clear_content()

移除载荷以及所有 Content- 标头，其他标头不加改变并且保持其原有顺序。

EmailMessage 对象具有下列实例属性:

preamble

MIME 文档格式在标头之后的空白行以及第一个多部分的分界字符串之间允许添加一些文本， 通常，此文本在支持 MIME 的邮件阅读器中永远不可见，因为它处在标准 MIME 防护范围之外。 但是，当查看消息的原始文本，或当在不支持 MIME 的阅读器中查看消息时，此文本会变得可见。

preamble 属性包含 MIME 文档开头部分的这些处于保护范围之外的文本。 当 Parser 在标头之后及第一个分界字符串之前发现一些文本时，它会将这些文本赋值给消息的 preamble 属性。 当 Generator 写出 MIME 消息的纯文本表示形式时，如果它发现消息具有 preamble 属性，它将在标头及第一个分界之间区域写出这些文本。 请参阅 email.parser 和 email.generator 了解更多细节。

请注意如果消息对象没有前导文本，则 preamble 属性将为 None。

epilogue

epilogue 属性的作用方式与 preamble 相同，区别在于它包含在最后一个分界及消息结尾之间出现的文本。 与 preamble 类似，如果没有附加文本，则此属性将为 None。

defects

defects 属性包含在解析消息时发现的所有问题的列表。 请参阅 email.errors 了解可能的解析缺陷的详细描述。

class email.message.MIMEPart(policy=default)

这个类代表 MIME 消息的子部分。 它与 EmailMessage 相似，不同之处在于当 set_content() 被调用时不会添加 MIME-Version 标头，因为子部分不需要有它们自己的 MIME-Version 标头。

备注

1

原先在3.4版本中以 provisional module 添加。过时的文档被移动至 email.message.Message: 使用 compat32 API 来表示电子邮件消息 。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

19.1.2. email.parser: 解析电子邮件信息

源代码: Lib/email/parser.py [https://github.com/python/cpython/tree/3.6/Lib/email/parser.py]

使用以下两种方法的其中一种以创建消息对象结构：直接创建一个 EmailMessage 对象，使用字典接口添加消息头，并且使用 set_content() 和其他相关方法添加消息负载；或者通过解析一个电子邮件消息的序列化表达来创建消息对象结构。

email 包提供了一个可以理解包含 MIME 文档在内的绝大多数电子邮件文档结构的标准语法分析程序。 你可以传递给语法分析程序一个字节串、字符串或者文件对象，语法分析程序会返回给你对应于该对象结构的根 EmailMessage 实例。 对于简单的、非 MIME 的消息，这个根对象的负载很可能就是一个包含了该消息文字内容的字符串。 对于 MIME 消息，调用根对象的 is_multipart() 方法会返回 True，其子项可以通过负载操纵方法来进行访问，例如 get_body()、iter_parts() 还有 walk()。

事实上你可以使用的语法分析程序接口有两种: Parser API 和增量式的 FeedParser API。当你的全部消息内容都在内存当中，或者整个消息都保存在文件系统内的一个文件当中的时候，Parser API非常有用。当你从可能会为了等待更多输入而阻塞的数据流当中读取消息（比如从套接字当中读取电子邮件消息）的时候，FeedParser 会更合适。FeedParser 会增量读取并解析消息，并且只有在你关闭语法分析程序的时候才会返回根对象。

请注意，语法分析程序可以进行有限的拓展，你当然也可以完全从零开始实现你自己的语法分析程序。将 email 包中内置的语法分析程序和 EmailMessage 类连接起来的所有逻辑代码都包含在 policy 类当中，所以如有必要，自定义的语法分析程序可以通过实现自定义的对应 policy 方法来创建对应的消息对象树。

19.1.2.1. FeedParser API

从 email.feedparser 模块导入的 BytesFeedParser 类提供了一个适合于增量解析电子邮件消息的API，比如说在从一个可能会阻塞（例如套接字）的源当中读取消息文字的场合中它就会变得很有用。当然， BytesFeedParser 也可以用来解析一个已经完整包含在一个 bytes-like object 、字符串或文件内的电子邮件消息，但是在这些场合下使用 BytesParser API可能会更加方便。这两个语法分析程序API的语义和最终结果是一致的。

BytesFeedParser 的API十分简洁易懂：你创建一个语法分析程序的实例，向它不断输入大量的字节直到尽头，然后关闭这个语法分析程序就可以拿到根消息对象了。 在处理符合标准的消息的时候 BytesFeedParser 非常准确；在处理不符合标准的消息的时候它做的也不差，但这视消息损坏的程度而定。它会向消息对象的 defects 属性中写入它从消息中找到的问题列表。关于它能找到的所有问题类型的列表，详见 email.errors 模块。

这里是 BytesFeedParser 的 API：

class email.parser.BytesFeedParser(_factory=None, *, policy=policy.compat32)

创建一个 BytesFeedParser 实例。可选的 _factory 参数是一个不带参数的可调用对象；如果没有被指定，就会使用 policy 参数的 message_factory 属性。 每当需要一个新的消息对象的时候，_factory 都会被调用。

如果指定了 policy 参数，它就会使用这个参数所指定的规则来更新消息的表达方式。 如果没有设定 policy 参数，它就会使用 compat32 策略。 这个策略维持了对 Python 3.2 版本的 email 包的后向兼容性，并且使用 Message 作为默认的工厂。 其他策略使用 EmailMessage 作为默认的 _factory。 关于 policy 还会控制什么，参见 policy 的文档。

注: 一定要指定policy关键字。 在未来版本的 Python 当中，它的默认值会变成 email.policy.default。

3.2 新版功能.

在 3.3 版更改: 添加了*policy*关键字。

在 3.6 版更改: _factory 默认为策略 message_factory。

feed(data)

向语法分析程序输入更多数据。data 应当是一个包含一行或多行内容的 bytes-like object。 行内容可以是不完整的，语法分析程序会妥善的将这些不完整的行缝合在一起。每一行可以使用以下三种常见的终止符号的其中一种：回车符、换行符或回车符加换行符（三者甚至可以混合使用）。

close()

完成之前输入的所有数据的解析并返回根消息对象。 如果在这个方法被调用之后仍然调用 feed() 方法，结果是未定义的。

class email.parser.FeedParser(_factory=None, *, policy=policy.compat32)

行为跟 BytesFeedParser 类一致，只不过向 feed() 方法输入的内容必须是字符串。 它的实用性有限，因为这种消息只有在其只含有 ASCII 文字，或者 utf8 被设置为 True 且没有二进制格式的附件的时候，才会有效。

在 3.3 版更改: 添加了*policy*关键字。

19.1.2.2. Parser API

BytesParser 类从 email.parser 模块导入，当消息的完整内容包含在一个 bytes-like object 或文件中时它提供了可用于解析消息的 API。 email.parser 模块还提供了 Parser 用来解析字符串，以及只用来解析消息头的 BytesHeaderParser 和 HeaderParser，如果你只对消息头感兴趣就可以使用后两者。 在这种场合下 BytesHeaderParser 和 HeaderParser 速度非常快，因为它们并不会尝试解析消息体，而是将载荷设为原始数据。

class email.parser.BytesParser(_class=None, *, policy=policy.compat32)

创建一个 BytesParser 实例。 _class 和 policy 参数在含义和语义上与 BytesFeedParser 的 _factory 和 policy 参数一致。

注: 一定要指定policy关键字。 在未来版本的 Python 当中，它的默认值会变成 email.policy.default。

在 3.3 版更改: 移除了在2.4版本中被弃用的*strict*参数。新增了*policy*关键字。

在 3.6 版更改: _class 默认为策略 message_factory。

parse(fp, headersonly=False)

从二进制的类文件对象 fp 中读取全部数据、解析其字节内容、并返回消息对象。 fp 必须同时支持 readline() 方法和 read() 方法。

fp 内包含的字节内容必须是一块遵循 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] （如果 utf8 为 True，则为 RFC 6532 [https://tools.ietf.org/html/rfc6532.html] ）格式风格的消息头和消息头延续行，并可能紧跟一个信封头。 头块要么以数据结束，要么以一个空行为终止。 跟着头块的是消息体（消息体内可能包含 MIME 编码的子部分，这也包括 Content-Transfer-Encoding 字段为 8bit 的子部分）。

可选的*headersonly*指示了是否应当在读取完消息头后就终止。默认值为``False``，意味着它会解析整个文件的全部内容。

parsebytes(bytes, headersonly=False)

与 parse() 方法类似，只不过它要求输入为一个 bytes-like object 而不是类文件对象。于一个 bytes-like object 调用此方法相当于先将这些字节包装于一个 BytesIO 实例中，然后调用 parse() 方法。

可选的 headersonly 与 parse() 方法中的 headersonly 是一致的。

3.2 新版功能.

class email.parser.BytesHeaderParser(_class=None, *, policy=policy.compat32)

除了 headersonly 默认为 True，其他与 BytesParser 类完全一样。

3.3 新版功能.

class email.parser.Parser(_class=None, *, policy=policy.compat32)

这个类与 BytesParser 一样，但是处理字符串输入。

在 3.3 版更改: 移除了*strict*参数。添加了*policy*关键字。

在 3.6 版更改: _class 默认为策略 message_factory。

parse(fp, headersonly=False)

从文本模式的文件类对象 fp 读取所有数据，解析所读取的文本，并返回根消息对象。 fp 必须同时支持文件类对象上的 readline() 和 read() 方法。

除了文字模式的要求外，这个方法跟 BytesParser.parse() 的运行方式一致。

parsestr(text, headersonly=False)

与 parse() 方法类似，只不过它要求输入为一个字符串而不是类文件对象。 于一个字符串对象调用此方法相当于先将 text 包装于一个 StringIO 实例中，然后调用 parse() 方法。

可选的 headersonly 与 parse() 方法中的 headersonly 是一致的。

class email.parser.HeaderParser(_class=None, *, policy=policy.compat32)

除了 headersonly 默认为 True，其他与 Parser 类完全一样。

考虑到从一个字符串或一个文件对象中创建一个消息对象是非常常见的任务，我们提供了四个方便的函数。它们于顶层 email 包命名空间内可用。

email.message_from_bytes(s, _class=None, *, policy=policy.compat32)

从一个 bytes-like object 中返回消息对象。 这与 BytesParser().parsebytes(s) 等价。 可选的 _class 和 policy 参数与 BytesParser 类的构造函数的参数含义一致。

3.2 新版功能.

在 3.3 版更改: 移除了*strict*参数。添加了*policy*关键字。

email.message_from_binary_file(fp, _class=None, *, policy=policy.compat32)

从打开的二进制 file object 中返回消息对象。 这与 BytesParser().parse(fp) 等价。 _class 和 policy 参数与 BytesParser 类的构造函数的参数含义一致。

3.2 新版功能.

在 3.3 版更改: 移除了*strict*参数。添加了*policy*关键字。

email.message_from_string(s, _class=None, *, policy=policy.compat32)

从一个字符串中返回消息对象。 这与 Parser().parsestr(s) 等价。 _class 和 policy 参数与 Parser 类的构造函数的参数含义一致。

在 3.3 版更改: 移除了*strict*参数。添加了*policy*关键字。

email.message_from_file(fp, _class=None, *, policy=policy.compat32)

从一个打开的 file object 中返回消息对象。 这与 Parser().parse(fp) 等价。 _class 和 policy 参数与 Parser 类的构造函数的参数含义一致。

在 3.3 版更改: 移除了*strict*参数。添加了*policy*关键字。

在 3.6 版更改: _class 默认为策略 message_factory。

这里是一个展示了你如何在Python交互式命令行中使用 message_from_bytes() 的例子：

>>> import email
>>> msg = email.message_from_bytes(myBytes)

19.1.2.3. 附加说明

在解析语义的时候请注意：

大多数非 multipart 类型的消息都会被解析为一个带有字符串负载的消息对象。这些对象在调用 is_multipart() 的时候会返回``False``，调用 iter_parts() 的时候会返回一个空列表。

所有 multipart 类型的消息都会被解析成一个容器消息对象。该对象的负载是一个子消息对象列表。外层的容器消息在调用 is_multipart() 的时候会返回``True``，在调用 iter_parts() 的时候会产生一个子部分列表。

大多数内容类型为 message/* （例如 message/delivery-status 和 message/rfc822 ）的消息也会被解析为一个负载是长度为1的列表的容器对象。在它们身上调用 is_multipart() 方法会返回``True``，调用 iter_parts() 所产生的单个元素会是一个子消息对象。

一些不遵循标准的消息在其内部关于它是否为 multipart 类型前后不一。这些消息可能在消息头的 Content-Type 字段中写明为 multipart ，但它们的 is_multipart() 方法的返回值可能是``False``。如果这种消息被 FeedParser 类解析，它们的*defects*属性列表当中会有一个 MultipartInvariantViolationDefect 类的实例。关于更多信息，详见 email.errors 。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

19.1.3. email.generator: 生成 MIME 文档

源代码: Lib/email/generator.py [https://github.com/python/cpython/tree/3.6/Lib/email/generator.py]

最普通的一种任务是生成由消息对象结构体表示的电子邮件消息的扁平（序列化）版本。 如果你想通过 smtplib.SMTP.sendmail() 或 nntplib 模块来发送你的消息或是将消息打印到控制台就将需要这样做。 接受一个消息对象结构体并生成其序列化表示就是这些生成器类的工作。

与 email.parser 模块一样，你并不会受限于已捆绑生成器的功能；你可以自己从头写一个。 不过，已捆绑生成器知道如何以符合标准的方式来生成大多数电子邮件，应该能够很好地处理 MIME 和非 MIME 电子邮件消息，并且被设计为面向字节的解析和生成操作是互逆的，它假定两者都使用同样的非转换型 policy。 也就是说，通过 BytesParser 类来解析序列化字节流然后再使用 BytesGenerator 来重新生成序列化字节流应当得到与输入相同的结果 1。 （而另一方面，在由程序所构造的 EmailMessage 上使用生成器可能导致对默认填入的 EmailMessage 对象的改变。。）

可以使用 Generator 类将消息扁平化为文本（而非二进制数据）的序列化表示形式，但是由于 Unicode 无法直接表示二进制数据，因此消息有必要被转换为仅包含 ASCII 字符的数据，这将使用标准电子邮件 RFC 内容传输编码格式技术来编码电子邮件消息以便通过非 “8 比特位兼容”的信道来传输。

class email.generator.BytesGenerator(outfp, mangle_from_=None, maxheaderlen=None, *, policy=None)

返回一个 BytesGenerator 对象，该对象将把提供给 flatten() 方法的任何消息或者提供给 write() 方法的任何经过代理转义编码的文本写入到 file-like object outfp。 outfp 必须支持接受二进制数据的 write 方法。

If optional mangle_from_ is True, put a > character in front of any line in the body that starts with the exact string "From ", that is From followed by a space at the beginning of a line. mangle_from_ defaults to the value of the mangle_from_ setting of the policy (which is True for the compat32 policy and False for all others). mangle_from_ is intended for use when messages are stored in unix mbox format (see mailbox and WHY THE CONTENT-LENGTH FORMAT IS BAD [http://www.jwz.org/doc/content-length.html]).

如果 maxheaderlen 不为 None，则重新折叠任何长于 maxheaderlen 的标头行，或者如果为 0，则不重新包装任何标头。 如果 manheaderlen 为 None (默认值)，则根据 policy 设置包装标头和其他消息行。

如果指定了 policy，则使用该策略来控制消息的生成。 如果 policy 为 None (默认值)，则使用与传递给 flatten 的 Message 或 EmailMessage 对象相关联的策略来控制消息的生成。 请参阅 email.policy 了解有关 policy 所控制内容的详情。

3.2 新版功能.

在 3.3 版更改: 添加了*policy*关键字。

在 3.6 版更改: mangle_from_ 和 maxheaderlen 形参的默认行为是遵循策略。

flatten(msg, unixfrom=False, linesep=None)

将将以 msg 为根的消息对象结构体的文本表示形式打印到创建 BytesGenerator 实例时指定的输出文件。

如果 policy 选项 cte_type 为 8bit (默认值)，则会将未被修改的原始已解析消息中的任何标头拷贝到输出，其中会重新生成与原始数据相同的高比特位组字节数据，并保留具有它们的任何消息体部分的非 ASCII Content-Transfer-Encoding。 如果 cte_type 为 7bit，则会根据需要使用兼容 ASCII 的 Content-Transfer-Encoding 来转换高比特位组字节数据。 也就是说，将具有非 ASCII Content-Transfer-Encoding (Content-Transfer-Encoding: 8bit) 的部分转换为兼容 ASCII 的 Content-Transfer-Encoding，并使用 MIME unknown-8bit 字符集来编码标头中不符合 RFC 的非 ASCII 字节数据，以使其符合 RFC。

如果 unixfrom 为 True，则会在根消息对象的第一个 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 标头之前打印 Unix mailbox 格式 (参见 mailbox) 所使用的封包标头分隔符。 如果根对象没有封包标头，则会创建一个标准标头。 默认值为 False。 请注意对于子部分来说，不会打印任何封包标头。

如果 linesep 不为 None，则会将其用作扁平化消息的所有行之间的分隔符。 如果 linesep 为 None (默认值)，则使用在 policy 中指定的值。

clone(fp)

返回此Return an independent clone of this BytesGenerator 实例的独立克隆，具有完全相同的选项设置，而 fp 为新的 outfp。

write(s)

使用 ASCII 编解码器和 surrogateescape 错误处理程序编码 s，并将其传递给传入到 BytesGenerator 的构造器的 outfp 的 write 方法 。

作为一个便捷工具，EmailMessage 提供了 as_bytes() 和 bytes(aMessage) (即 __bytes__()) 等方法，它们简单地生成一个消息对象的序列化二进制表示形式。 更多细节请参阅 email.message。

因为字符串无法表示二进制数据，Generator 类必须将任何消息中扁平化的任何二进制数据转换为兼容 ASCII 的格式，具体将其转换为兼容 ASCII 的 Content-Transfer_Encoding。 使用电子邮件 RFC 的术语，你可以将其视作 Generator 序列化为不 “支持 8 比特” 的 I/O 流。 换句话说，大部分应用程序将需要使用 BytesGenerator，而非 Generator。

class email.generator.Generator(outfp, mangle_from_=None, maxheaderlen=None, *, policy=None)

返回一个 Generator，它将把提供给 flatten() 方法的任何消息，或者提供给 write() 方法的任何文本写入到 file-like object outfp。 outfp 必须支持接受字符串数据的 write 方法。

If optional mangle_from_ is True, put a > character in front of any line in the body that starts with the exact string "From ", that is From followed by a space at the beginning of a line. mangle_from_ defaults to the value of the mangle_from_ setting of the policy (which is True for the compat32 policy and False for all others). mangle_from_ is intended for use when messages are stored in unix mbox format (see mailbox and WHY THE CONTENT-LENGTH FORMAT IS BAD [http://www.jwz.org/doc/content-length.html]).

如果 maxheaderlen 不为 None，则重新折叠任何长于 maxheaderlen 的标头行，或者如果为 0，则不重新包装任何标头。 如果 manheaderlen 为 None (默认值)，则根据 policy 设置包装标头和其他消息行。

如果指定了 policy，则使用该策略来控制消息的生成。 如果 policy 为 None (默认值)，则使用与传递给 flatten 的 Message 或 EmailMessage 对象相关联的策略来控制消息的生成。 请参阅 email.policy 了解有关 policy 所控制内容的详情。

在 3.3 版更改: 添加了*policy*关键字。

在 3.6 版更改: mangle_from_ 和 maxheaderlen 形参的默认行为是遵循策略。

flatten(msg, unixfrom=False, linesep=None)

将以 msg 为根的消息对象结构体的文本表示形式打印到当 Generator 实例被创建时所指定的输出文件。

If the policy option cte_type is 8bit, generate the message as if the option were set to 7bit. (This is required because strings cannot represent non-ASCII bytes.) Convert any bytes with the high bit set as needed using an ASCII-compatible Content-Transfer-Encoding. That is, transform parts with non-ASCII Cotnent-Transfer-Encoding (Content-Transfer-Encoding: 8bit) to an ASCII compatible Content-Transfer-Encoding, and encode RFC-invalid non-ASCII bytes in headers using the MIME unknown-8bit character set, thus rendering them RFC-compliant.

如果 unixfrom 为 True，则会在根消息对象的第一个 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 标头之前打印 Unix mailbox 格式 (参见 mailbox) 所使用的封包标头分隔符。 如果根对象没有封包标头，则会创建一个标准标头。 默认值为 False。 请注意对于子部分来说，不会打印任何封包标头。

如果 linesep 不为 None，则会将其用作扁平化消息的所有行之间的分隔符。 如果 linesep 为 None (默认值)，则使用在 policy 中指定的值。

在 3.2 版更改: 添加了对重编码 8bit 消息体的支持，以及 linesep 参数。

clone(fp)

返回此 Generator 实例的独立克隆，具有完全相同的选项设置，而 fp 为新的 outfp。

write(s)

将 s 写入到传给 Generator 的构造器的 outfp 的 write 方法。 这足够为 Generator 实际提供可用于 print() 函数的文件类 API。

作为一个便捷工具，EmailMessage 提供了 as_string() 和 str(aMessage) (即 __str__()) 等方法，它们简单地生成一个消息对象的已格式化字符串表示形式。 更多细节请参阅 email.message。

email.generator 模块还提供了一个派生类 DecodedGenerator，它类似于 Generator 基类，不同之处在于非 text 部分不会被序列化，而是被表示为 基于模板并填写了有关该部分的信息的字符串输出流的形式。

class email.generator.DecodedGenerator(outfp, mangle_from_=None, maxheaderlen=None, fmt=None, *, policy=None)

行为类似于 Generator，不同之处在于对传给 Generator.flatten() 的消息的任何子部分，如果该子部分的主类型为 text，则打印该子部分的已解码载荷，而如果其主类型不为 text，则不直接打印它而是使用来自该部分的信息填入字符串 fmt 并将填写完成的字符串打印出来。

要填入 fmt，则执行 fmt % part_info，其中 part_info 是由下列键和值组成的字典:

type – 非 text 部分的完整 MIME 类型

maintype – 非 text 部分的主 MIME 类型

subtype – 非 text 部分的子 MIME 类型

filename – 非 text 部分的文件名

description – 与非 text 部分相关联的描述

encoding – 非 text 部分的内容转换编码格式

如果 fmt 为 None，则使用下列默认 fmt:

“[忽略消息的非文本 (%(type)s) 部分，文件名 %(filename)s]”

可选的 _mangle_from_ 和 maxheaderlen 与 Generator 基类的相同。

备注

1

此语句假定你使用了正确的 unixfrom 设置，并且没有用于自动调整的 policy 设置调用（例如 refold_source 必须为 none，这 不是 默认值）。 这也不是 100% 为真，因为如果消息不遵循 RFC 标准则有时实际原始文本的信息会在解析错误恢复时丢失。 它的目标是在可能的情况下修复这些后续边缘情况。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

19.1.4. email.policy: 策略对象

3.3 新版功能.

源代码: Lib/email/policy.py [https://github.com/python/cpython/tree/3.6/Lib/email/policy.py]

email 的主要焦点是按照各种电子邮件和 MIME RFC 的描述来处理电子邮件消息。 但是电子邮件消息的基本格式（一个由名称加冒号加值的标头字段构成的区块，后面再加一个空白行和任意的‘消息体’）是在电子邮件领域以外也获得应用的格式。 这些应用的规则有些与主要电子邮件 RFC 十分接近，有些则很不相同。 即使是操作电子邮件，有时也可能需要打破严格的 RFC 规则，例如生成可与某些并不遵循标准的电子邮件服务器互联的电子邮件，或者是实现希望应用某些破坏标准的操作方式的扩展。

策略对象给予 email 包处理这些不同用例的灵活性。

Policy 对象封装了一组属性和方法用来在使用期间控制 email 包中各个组件的行为。 Policy 实例可以被传给 email 包中的多个类和方法以更改它们的默认行为。 可设置的值及其默认值如下所述。

在 email 包中的所有类会使用一个默认的策略。 对于所有 parser 类及相关的便捷函数，还有对于 Message 类来说，它是 Compat32 策略，通过其对应的预定义实例 compat32 来使用。 这个策略提供了与 Python3.3 版之前的 email 包的完全向下兼容性（在某些情况下，也包括对缺陷的兼容性）。

传给 EmailMessage 的 policy 关键字的默认值是 EmailPolicy 策略，表示为其预定义的实例 default。

在创建 Message 或 EmailMessage 对象时，它需要一个策略。 如果消息是由 parser 创建的，则传给该解析器的策略将是它所创建的消息所使用的策略。 如果消息是由程序创建的，则该策略可以在创建它的时候指定。 当消息被传递给 generator 时，生成器默认会使用来自该消息的策略，但你也可以将指定的策略传递给生成器，这将覆盖存储在消息对象上的策略。

email.parser 类和解析器便捷函数的 policy 关键字的默认值在未来的 Python 版本中 将会改变。 因此在调用任何 parser 模块所描述的类和函数时你应当 总是显式地指定你想要使用的策略。

本文档的第一部分介绍了 Policy 的特性，它是一个 abstract base class，定义了所有策略对象包括 compat32 的共有特性。 这些特性包括一些由 email 包内部调用的特定钩子方法，自定义策略可以重载这些方法以获得不同行为。 第二部分描述了实体类 EmailPolicy 和 Compat32，它们分别实现了提供标准行为和向下兼容行为与特性的钩子。

Policy 实例是不可变的，但它们可以被克隆，接受与类构造器一致的关键字参数并返回一个新的 Policy 实例，新实例是原实例的副本，但具有被改变的指定属性。

例如，以下代码可以被用来从一个 Unix 系统的磁盘文件中读取电子邮件消息并将其传递给系统的 sendmail 程序:

>>> from email import message_from_binary_file
>>> from email.generator import BytesGenerator
>>> from email import policy
>>> from subprocess import Popen, PIPE
>>> with open('mymsg.txt', 'rb') as f:
...     msg = message_from_binary_file(f, policy=policy.default)
>>> p = Popen(['sendmail', msg['To'].addresses[0]], stdin=PIPE)
>>> g = BytesGenerator(p.stdin, policy=msg.policy.clone(linesep='\r\n'))
>>> g.flatten(msg)
>>> p.stdin.close()
>>> rc = p.wait()

这里我们让 BytesGenerator 在创建要送入 sendmail's stdin 的二进制字串时使用符合 RFC 的行分隔字符，默认的策略将会使用 \n 行分隔符。

某些 email 包的方法接受一个 policy 关键字参数，允许为该方法重载策略。 例如，以下代码使用了来自之前示例的 msg 对象的 as_bytes() 方法并使用其运行所在平台的本机行分隔符将消息写入一个文件:

>>> import os
>>> with open('converted.txt', 'wb') as f:
...     f.write(msg.as_bytes(policy=msg.policy.clone(linesep=os.linesep)))
17

Policy 对象也可使用附加运算符进行组合来产生一个新策略对象，其设置是被加总对象的非默认值的组合:

>>> compat_SMTP = policy.compat32.clone(linesep='\r\n')
>>> compat_strict = policy.compat32.clone(raise_on_defect=True)
>>> compat_strict_SMTP = compat_SMTP + compat_strict

此运算不满足交换律；也就是说对象的添加顺序很重要。 见以下演示:

>>> policy100 = policy.compat32.clone(max_line_length=100)
>>> policy80 = policy.compat32.clone(max_line_length=80)
>>> apolicy = policy100 + policy80
>>> apolicy.max_line_length
80
>>> apolicy = policy80 + policy100
>>> apolicy.max_line_length
100

class email.policy.Policy(**kw)

这是所有策略类的 abstract base class。 它提供了一些简单方法的默认实现，以及不可变特征属性，clone() 方法以及构造器语义的实现。

可以向策略类的构造器传入各种关键字参数。 可以指定的参数是该类的任何非方法特征属性，以及实体类的任何额外非方法特征属性。 在构造器中指定的值将覆盖相应属性的默认值。

这个类定义了下列特征属性，因此下列值可以被传给任何策略类的构造器:

max_line_length

序列化输出中任何行的最大长度，不计入行字符的末尾。 默认值为 78，基于 RFC 5322 [https://tools.ietf.org/html/rfc5322.html]。 值为 0 或 None 表示完全没有行包装。

linesep

用来在序列化输出中确定行的字符串。 默认值为 \n 因为这是 Python 所使用的内部行结束符规范，但 RFC 的要求是 \r\n。

cte_type

控制可能要求使用的内容传输编码格式类型。 可能的值包括:

7bit

所有数据必须为 “纯 7 比特位” (仅 ASCII)。 这意味着在必要情况下数据将使用可打印引用形式或 base64 编码格式进行编码。

8bit

数据不会被限制为纯 7 比特位。 标头中的数据仍要求仅 ASCII 因此将被编码（参阅下文的 fold_binary() 和 utf8 了解例外情况），但消息体部分可能使用 8bit CTE。

cte_type 值为 8bit 仅适用于 BytesGenerator 而非 Generator，因为字符串不能包含二进制数据。 如果 Generator 运行于指定了 cte_type=8bit 的策略，它的行为将与 cte_type 为 7bit 相同。

raise_on_defect

如为 True，则遇到的任何缺陷都将引发错误。 如为 False (默认值)，则缺陷将被传递给 register_defect() 方法。

mangle_from_

如为 True，则消息体中以 “From “ 开头的行会通过在其前面放一个 > 来进行转义。 当消息被生成器执行序列化时会使用此形参。 默认值t: False。

3.5 新版功能: mangle_from_ 形参。

message_factory

用来构造新的空消息对象的工厂函数。 在构建消息时由解析器使用。 默认为 None，在此情况下会使用 Message。

3.6 新版功能.

下列 Policy 方法是由使用 email 库的代码来调用以创建具有自室外设置的策略实例:

clone(**kw)

返回一个新的 Policy 实例，其属性与当前实例具有相同的值，除非是那些由关键字参数给出了新值的属性。

其余的 Policy 方法是由 email 包代码来调用的，而不应当被使用 email 包的应用程序所调用。 自定义的策略必须实现所有这些方法。

handle_defect(obj, defect)

处理在 obj 上发现的 defect。 当 email 包调用此方法时，defect 将总是 Defect 的一个子类。

默认实现会检查 raise_on_defect 旗标。 如果其为 True，则 defect 会被作为异常来引发。 如果其为 False (默认值)，则 obj 和 defect 会被传递给 register_defect()。

register_defect(obj, defect)

在 obj 上注册一个 defect。 在 email 包中，defect 将总是 Defect 的一个子类。

默认实现会调用 obj 的 defects 属性的 append 方法。 当 email 包调用 handle_defect 时，obj 通常将具有一个带 append 方法的 defects 属性。 配合 email 包使用的自定义对象类型（例如自定义的 Message 对象）也应当提供这样的属性，否则在被解析消息中的缺陷将引发非预期的错误。

header_max_count(name)

返回名为 name 的标头的最大允许数量。

当添加一个标头到 EmailMessage 或 Message 对象时被调用。 如果返回值不为 0 或 None，并且已有的名称为 name 的标头数量大于等于所返回的值，则会引发 ValueError。

由于 Message.__setitem__ 的默认行为是将值添加到标头列表，因此很容易不知情地创建重复的标头。 此方法允许在程序中限制可以被添加到 Message 中的特定标头的实例数量。 （解析器不会考虑此限制，它将忠实地产生被解析消息中存在的任意数量的标头。）

默认实现对于所有标头名称都返回 None。

header_source_parse(sourcelines)

email 包调用此方法时将传入一个字符串列表，其中每个字符串以在被解析源中找到的行分隔符结束。 第一行包括字段标头名称和分隔符。 源中的所有空白符都会被保留。 此方法应当返回 (name, value) 元组以保存至 Message 中来代表被解析的标头。

如果一个实现希望保持与现有 email 包策略的兼容性，则 name 应当为保留大小写形式的名称（所有字符直至 ‘:’ 分隔符），而 value 应当为展开后的值（移除所有行分隔符，但空白符保持不变），并移除开头的空白符。

sourcelines 可以包含经替代转义的二进制数据。

此方法没有默认实现

header_store_parse(name, value)

当一个应用通过程序代码修改 Message (而不是由解析器创建 Message) 时，email 包会调用此方法时并附带应用程序所提供的名称和值。 此方法应当返回 (name, value) 元组以保存至 Message 中用来表示标头。

如果一个实现希望保持与现有 email 包策略的兼容性，则 name 和 value 应当为字符串或字符串的子类，它们不会修改在参数中传入的内容。

此方法没有默认实现

header_fetch_parse(name, value)

当标头被应用程序所请求时，email 包会调用此方法并附带当前保存在 Message 中的 name 和 value，并且无论此方法返回什么它都会被回传给应用程序作为被提取标头的值。 请注意可能会有多个相同名称的标头被保存在 Message 中；此方法会将指定标头的名称和值返回给应用程序。

value 可能包含经替代转义的二进制数据。 此方法所返回的值应当没有经替代转义的二进制数据。

此方法没有默认实现

fold(name, value)

email 包调用此方法时会附带当前保存在 Message 中的给定标头的 name 和 value。 此方法应当返回一个代表该标头的（根据策略设置）通过处理 name 和 value 并在适当位置插入 linesep 字符来正确地“折叠”的字符串。 请参阅 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 了解有关折叠电子邮件标头的规则的讨论。

value 可能包含经替代转义的二进制数据。 此方法所返回的字符串应当没有经替代转义的二进制数据。

fold_binary(name, value)

与 fold() 类似，不同之处在于返回的值应当为字节串对象而非字符串。

value 可能包含经替代转义的二进制数据。 这些数据可以在被返回的字节串对象中被转换回二进制数据。

class email.policy.EmailPolicy(**kw)

这个实体 Policy 提供了完全遵循当前电子邮件 RFC 的行为。 这包括 (但不限于) RFC 5322 [https://tools.ietf.org/html/rfc5322.html], RFC 2047 [https://tools.ietf.org/html/rfc2047.html] 以及当前的各种 MIME RFC。

此策略添加了新的标头解析和折叠算法。 标头不是简单的字符串，而是带有依赖于字段类型的属性的 str 的子类。 这个解析和折叠算法完整实现了 RFC 2047 [https://tools.ietf.org/html/rfc2047.html] 和 RFC 5322 [https://tools.ietf.org/html/rfc5322.html]。

message_factory 属性的默认值为 EmailMessage。

除了上面列出的适用于所有策略的可设置属性，此策略还添加了下列额外属性:

3.6 新版功能: 1

utf8

如为 False，则遵循 RFC 5322 [https://tools.ietf.org/html/rfc5322.html]，通过编码为“已编码字”来支持标头中的非 ASCII 字符。 如为 True，则遵循 RFC 6532 [https://tools.ietf.org/html/rfc6532.html] 并对标头使用 utf-8 编码格式。 以此方式格式化的消息可被传递给支持 SMTPUTF8 扩展 (RFC 6531 [https://tools.ietf.org/html/rfc6531.html]) 的 SMTP 服务器。

refold_source

如果 Message 对象中标头的值源自 parser (而非由程序设置)，此属性会表明当将消息转换回序列化形式时是否应当由生成器来重新折叠该值。 可能的值如下:

none

所有源值使用原始折叠

long

具有任何长度超过 max_line_length 的行的源值将被折叠

all

所有值会被重新折叠。

默认值为 long。

header_factory

该可调用对象接受两个参数，name 和 value，其中 name 为标头字段名而 value 为展开后的标头字段值，并返回一个表示该标头的字符串子类。 已提供的默认 header_factory (参见 headerregistry) 支持对各种地址和日期 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 标头字段类型及主要 MIME 标头字段类型的自定义解析。 未来还将添加对其他自定义解析的支持。

content_manager

此对象至少有两个方法: get_content 和 set_content。 当一个 EmailMessage 对象的 get_content() 或 set_content() 方法被调用时，它会调用此对象的相应方法，将消息对象作为其第一个参数，并将传给它的任何参数或关键字作为附加参数传入。 默认情况下 content_manager 会被设为 raw_data_manager。

3.4 新版功能.

这个类提供了下列对 Policy 的抽象方法的具体实现:

header_max_count(name)

返回用来表示具有给定名称的标头的专用类的 max_count 属性的值。

header_source_parse(sourcelines)

此名称会被作为到 ‘:’ 止的所有内容来解析。 该值是通过从第一行的剩余部分去除前导空格，再将所有后续行连接到一起，并去除所有末尾回车符或换行符来确定的。

header_store_parse(name, value)

name 将会被原样返回。 如果输入值具有 name 属性并可在忽略大小写的情况下匹配 name，则 value 也会被原样返回。 在其他情况下 name 和 value 会被传递给 header_factory，并将结果标头对象作为值返回。 在此情况下如果输入值包含 CR 或 LF 字符则会引发 ValueError。

header_fetch_parse(name, value)

如果值具有 name 属性，它会被原样返回。 在其他情况下 name 和移除了所有 CR 和 LF 字符的 value 会被传递给 header_factory，并返回结果标头对象。 任何经替代转义的字节串会被转换为 unicode 未知字符字形。

fold(name, value)

标头折叠是由 refold_source 策略设置来控制的。 当且仅当一个值没有 name 属性（具有 name 属性就意味着它是某种标头对象）它才会被当作是“源值”。 如果一个原值需要按照策略来重新折叠，则会通过将 name 和去除了所有 CR 和 LF 字符的 value 传递给 header_factory 来将其转换为标头对象。 标头对象的折叠是通过调用其 fold 方法并附带当前策略来完成的。

源值会使用 splitlines() 来拆分成多行。 如果该值不被重新折叠，则会使用策略中的 linesep 重新合并这些行并将其返回。 例外的是包含非 ascii 二进制数据的行。 在此情况下无论 refold_source 如何设置该值都会被重新折叠，这会导致二进制数据使用 unknown-8bit 字符集进行 CTE 编码。

fold_binary(name, value)

如果 cte_type 为 7bit 则与 fold() 类似，不同之处在于返回的值是字节串。

如果 cte_type 为 8bit，则将非 ASCII 二进制数据转换回字节串。 带有二进制数据的标头不会被重新折叠，无论 refold_header 设置如何，因为无法知晓该二进制数据是由单字节字符还是多字节字符组成的。

以下 EmailPolicy 的实例提供了适用于特定应用领域的默认值。 请注意在未来这些实例（特别是 HTTP 实例）的行为可能会被调整以便更严格地遵循与其领域相关的 RFC。

email.policy.default

一个未改变任何默认值的 EmailPolicy 实例。 此策略使用标准的 Python \n 行结束符而非遵循 RFC 的 \r\n。

email.policy.SMTP

适用于按照符合电子邮件 RFC 的方式来序列化消息。 与 default 类似，但 linesep 被设为遵循 RFC 的 \r\n。

email.policy.SMTPUTF8

与 SMTP 类似但是 utf8 为 True。 适用于在不使用标头内已编码字的情况下对消息进行序列化。 如果发送方或接收方地址具有非 ASCII 字符则应当只被用于 SMTP 传输 (smtplib.SMTP.send_message() 方法会自动如此处理)。

email.policy.HTTP

适用于序列化标头以在 HTTP 通信中使用。 与 SMTP 类似但是 max_line_length 被设为 None (无限制)。

email.policy.strict

便捷实例。 与 default 类似但是 raise_on_defect 被设为 True。 这样可以允许通过以下写法来严格地设置任何策略:

somepolicy + policy.strict

因为所有这些 EmailPolicies，email 包的高效 API 相比 Python 3.2 API 发生了以下几方面变化:

在 Message 中设置标头将使得该标头被解析并创建一个标头对象。

从 Message 提取标头将使得该标头被解析并创建和返回一个标头对象。

任何标头对象或任何由于策略设置而被重新折叠的标头都会使用一种完全实现了 RFC 折叠算法的算法来进行折叠，包括知道在休息需要并允许已编码字。

从应用程序的视角来看，这意味着任何通过 EmailMessage 获得的标头都是具有附加属性的标头对象，其字符串值都是该标头的完全解码后的 unicode 值。 类似地，可以使用 unicode 对象为一个标头赋予新的值，或创建一个新的标头对象，并且该策略将负责把该 unicode 字符串转换为正确的 RFC 已编码形式。

标头对象及其属性的描述见 headerregistry。

class email.policy.Compat32(**kw)

这个实体 Policy 为向下兼容策略。 它复制了 Python 3.2 中 email 包的行为。 policy 模块还定义了该类的一个实例 compat32，用来作为默认策略。 因此 email 包的默认行为会保持与 Python 3.2 的兼容性。

下列属性具有与 Policy 默认值不同的值:

mangle_from_

默认值为 True。

这个类提供了下列对 Policy 的抽象方法的具体实现:

header_source_parse(sourcelines)

此名称会被作为到 ‘:’ 止的所有内容来解析。 该值是通过从第一行的剩余部分去除前导空格，再将所有后续行连接到一起，并去除所有末尾回车符或换行符来确定的。

header_store_parse(name, value)

name 和 value 会被原样返回。

header_fetch_parse(name, value)

如果 value 包含二进制数据，则会使用 unknown-8bit 字符集来将其转换为 Header 对象。 在其他情况下它会被原样返回。

fold(name, value)

标头会使用 Header 折叠算法进行折叠，该算法保留 value 中现有的换行，并将结果行长度折叠至 max_line_length。 非 ASCII 二进制数据会使用 unknown-8bit 字符串进行 CTE 编码。

fold_binary(name, value)

标头会使用 Header 折叠算法进行折叠，该算法保留 value 中现有的换行，并将每个结果行的长度折叠至 max_line_length。 如果 cte_type 为 7bit，则非 ascii 二进制数据会使用 unknown-8bit 字符集进行 CTE 编码。 在其他情况下则会使用原始的源标头，这将保留其现有的换行和所包含的任何（不符合 RFC 的）二进制数据。

email.policy.compat32

Compat32 的实例，提供与 Python 3.2 中的 email 包行为的向下兼容性。

备注

1

最初在 3.3 中作为 暂定特性 添加。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

19.1.5. email.errors: 异常和缺陷类

源代码: Lib/email/errors.py [https://github.com/python/cpython/tree/3.6/Lib/email/errors.py]

以下异常类在 email.errors 模块中定义：

exception email.errors.MessageError

这是 email 包可以引发的所有异常的基类。 它源自标准异常 Exception 类，这个类没有定义其他方法。

exception email.errors.MessageParseError

这是由 Parser 类引发的异常的基类。它派生自 MessageError。 headerregistry 使用的解析器也在内部使用这个类。

exception email.errors.HeaderParseError

在解析消息的 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 标头时，某些错误条件下会触发，此类派生自 MessageParseError。 如果在调用方法时内容类型未知，则 set_boundary() 方法将引发此错误。 当尝试创建一个看起来包含嵌入式标头的标头时 Header 可能会针对某些 base64 解码错误引发此错误（也就是说，应该是一个 没有前导空格并且看起来像标题的延续行）。

exception email.errors.BoundaryError

已弃用和不再使用的。

exception email.errors.MultipartConversionError

当使用 add_payload() 将有效负载添加到 Message 对象时，有效负载已经是一个标量，而消息的 Content-Type 主类型不是 multipart 或者缺少时触发该异常。 MultipartConversionError 多重继承自 MessageError 和内置的 TypeError。

由于 Message.add_payload() 已被弃用，此异常实际上极少会被引发。 但是如果在派生自 MIMENonMultipart 的类 (例如 MIMEImage) 的实例上调用 attach() 方法也可以引发此异常。

以下是 FeedParser 在解析消息时可发现的缺陷列表。 请注意这些缺陷会在问题被发现时加入到消息中，因此举例来说，如果某条嵌套在 multipart/alternative 中的消息具有错误的标头，该嵌套消息对象就会有一条缺陷，但外层消息对象则没有。

所有缺陷类都是 email.errors.MessageDefect 的子类。

NoBoundaryInMultipartDefect – 一条消息宣称有多个部分，但却没有 boundary 形参。

StartBoundaryNotFoundDefect – 在 Content-Type 标头中宣称的开始边界无法被找到。

CloseBoundaryNotFoundDefect – 找到了开始边界，但相应的结束边界无法被找到。

3.3 新版功能.

FirstHeaderLineIsContinuationDefect – 消息以一个继续行作为其第一个标头行。

MisplacedEnvelopeHeaderDefect - 在标头块中间发现了一个 “Unix From” 标头。

MissingHeaderBodySeparatorDefect - 在解析没有前缀空格但又不包含 ‘:’ 的标头期间找到一行内容。 解析将假定该行表示消息体的第一行以继续执行。

3.3 新版功能.

MalformedHeaderDefect – 找到一个缺失了冒号或格式错误的标头。

3.3 版后已移除: 此缺陷在近几个 Python 版本中已不再使用。

MultipartInvariantViolationDefect – A message claimed to be a multipart, but no subparts were found. Note that when a message has this defect, its is_multipart() method may return false even though its content type claims to be multipart.

InvalidBase64PaddingDefect – 当解码一个 base64 编码的字节分块时，填充的数据不正确。 虽然添加了足够的填充数据以执行解码，但作为结果的已解码字节串可能无效。

InvalidBase64CharactersDefect – 当解码一个 base64 编码的字节分块时，遇到了 base64 字符表以外的字符。 这些字符会被忽略，但作为结果的已解码字节串可能无效。

InvalidBase64LengthDefect – 当解码一个 base64 编码的字节分块时，非填充 base64 字符的数量无效（比 4 的倍数多 1）。 已编码分块会保持原样。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

19.1.6. email.headerregistry: 自定义标头对象

源代码: Lib/email/headerregistry.py [https://github.com/python/cpython/tree/3.6/Lib/email/headerregistry.py]

3.6 新版功能: 1

标头是由 str 的自定义子类来表示的。 用于表示给定标头的特定类则由创建标头时生效的 policy 的 header_factory 确定。 这一节记录了 email 包为处理兼容 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 的电子邮件消息所实现的特定 header_factory，它不仅为各种标头类型提供了自定义的标头对象，还为应用程序提供了添加其自定义标头类型的扩展机制。

当使用派生自 EmailPolicy 的任何策略对象时，所有标头都通过 HeaderRegistry 产生并且以 BaseHeader 作为其最后一个基类。 每个标头类都有一个由该标头类型确定的附加基类。 例如，许多标头都以 UnstructuredHeader 类作为其另一个基类。 一个标头专用的第二个类是由标头名称使用存储在 HeaderRegistry 中的查找表来确定的。 所有这些都针对典型应用程序进行透明的管理，但也为修改默认行为提供了接口，以便由更复杂的应用使用。

以下各节首先记录了标头基类及其属性，然后是用于修改 HeaderRegistry 行为的 API，最后是用于表示从结构化标头解析的数据的支持类。

class email.headerregistry.BaseHeader(name, value)

name 和 value 会从 header_factory 调用传递给 BaseHeader。 任何标头对象的字符串值都是完成解码为 unicode 的 value。

这个基类定义了下列只读属性:

name

标头的名称（字段在 ‘:’ 之前的部分）。 这就是 name 的 header_factory 调用所传递的值；也就是说会保持大小写形式。

defects

一个包含 HeaderDefect 实例的元组，这些实例报告了在解析期间发现的任何 RFC 合规性问题。 email 包会尝试尽可能地检测合规性问题。 请参阅 errors 模块了解可能被报告的缺陷类型的相关讨论。

max_count

此类型标头可具有相同 name 的最大数量。 None 值表示无限制。 此属性的 BaseHeader 值为 None；专用的标头类预期将根据需要重载这个值。

BaseHeader 还提供了以下方法，它由 email 库代码调用，通常不应当由应用程序来调用。

fold(*, policy)

返回一个字符串，其中包含用来根据 policy 正确地折叠标头的 linesep 字符。 cte_type 为 8bit 时将被作为 7bit 来处理，因为标头不能包含任意二进制数据。 如果 utf8 为 False，则非 ASCII 数据将根据 RFC 2047 [https://tools.ietf.org/html/rfc2047.html] 来编码。

BaseHeader 本身不能被用于创建标头对象。 它定义了一个与每个专用标头相配合的协议以便生成标头对象。 具体来说，BaseHeader 要求专用类提供一个名为 parse 的 classmethod()。 此方法的调用形式如下:

parse(string, kwds)

kwds 是包含了一个预初始化键 defects 的字典。 defects 是一个空列表。 parse 方法应当将任何已检测到的缺陷添加到此列表中。 在返回时，kwds 字典 必须 至少包含 decoded 和 defects 等键的值。 decoded 应当是标头的字符串值（即完全解码为 unicode 的标头值）。 parse 方法应当假定 string 可能包含 content-transfer-encoded 部分，但也应当正确地处理全部有效的 unicode 字符以便它能解析未经编码的标头值。

随后 BaseHeader 的 __new__ 会创建标头实例，并调用其 init 方法。 专属类如果想要设置 BaseHeader 自身所提供的属性之外的附加属性，只需提供一个 init 方法。 这样的 init 看起来应该是这样:

def init(self, *args, **kw):
    self._myattr = kw.pop('myattr')
    super().init(*args, **kw)

也就是说，专属类放入 kwds 字典的任何额外内容都应当被移除和处理，并且 kw (和 args) 的剩余内容会被传递给 BaseHeader init 方法。

class email.headerregistry.UnstructuredHeader

“非结构化” 标头是 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 中默认的标头类型。 任何没有指定语法的标头都会被视为是非结构化的。 非结构化标头的经典例子是 Subject 标头。

在 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 中，非结构化标头是指一段以 ASCII 字符集表示的任意文本。 但是 RFC 2047 [https://tools.ietf.org/html/rfc2047.html] 具有一个 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 兼容机制用来将标头值中的非 ASCII 文本编码为 ASCII 字符。 当包含已编码字的 value 被传递给构造器时，UnstructuredHeader 解析器会按照非结构化文本的 RFC 2047 [https://tools.ietf.org/html/rfc2047.html] 规则将此类已编码字转换为 unicode。 解析器会使用启发式机制来尝试解码一些不合规的已编码字。 在此种情况下各类缺陷，例如已编码字或未编码文本中的无效字符问题等缺陷将会被注册。

此标头类型未提供附加属性。

class email.headerregistry.DateHeader

RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 为电子邮件标头内的日期指定了非常明确的格式。 DateHeader 解析器会识别该日期格式，并且也能识别间或出现的一些“不规范”变种形式。

这个标头类型提供了以下附加属性。

datetime

如果标头值能被识别为某一种有效的日期形式，此属性将包含一个代表该日期的 datetime 实例。 如果输入日期的时区被指定为 -0000 (表示为 UTC 但不包含源时区的相关信息)，则 datetime 将为简单型 datetime。 如果找到了特定的时区时差值 (包括 +0000)，则 datetime 将包含使用 datetime.timezone 来记录时区时差时的感知型 datetime。

标头的 decoded 值是由按照 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 对 datetime 进行格式化来确定的；也就是说，它会被设为:

email.utils.format_datetime(self.datetime)

当创建 DateHeader 时，value 可以为 datetime 实例。 例如这意味着以下代码是有效的并能实现人们预期的行为:

msg['Date'] = datetime(2011, 7, 15, 21)

因为这是个简单型 datetime 它将被解读为 UTC 时间戳，并且结果值的时区将为 -0000。 使用来自 utils 模块的 localtime() 函数会更有用:

msg['Date'] = utils.localtime()

这个例子将日期标头设为使用当前时区时差值的当前时间和日期。

class email.headerregistry.AddressHeader

地址标头是最复杂的结构化标头类型之一。 AddressHeader 类提供了适合任何地址标头的泛用型接口。

这个标头类型提供了以下附加属性。

groups

编码了在标头值中找到的地址和分组的 Group 对象的元组。 非分组成员的地址在此列表中表示为 display_name 为 None 的单地址 Groups。

addresses

编码了来自标头值的所有单独地址的 Address 对象的元组。 如果标头值包含任何分组，则来自分组的单个地址将包含在该分组出现在值中的点上列出（也就是说，地址列表会被“展平”为一维列表）。

标头的 decoded 值将为所有已编码字解码为 unicode 的结果。 idna 编码的域名也将被解码为 unicode。 decoded 值是通过对 groups 属性的元素的 str 值使用 ', ' 进行 join 来设置的。

可以使用 Address 与 Group 对象的任意组合的列表来设置一个地址标头的值。 display_name 为 None 的 Group 对象将被解读为单独地址，这允许一个地址列表可以附带通过使用从源标头的 groups 属性获取的列表而保留原分组。

class email.headerregistry.SingleAddressHeader

AddressHeader 的子类，添加了一个额外的属性:

address

由标头值编码的单个地址。 如果标头值实际上包含一个以上的地址（这在默认 policy 下将违反 RFC），则访问此属性将导致 ValueError。

上述类中许多还具有一个 Unique 变体 (例如 UniqueUnstructuredHeader)。 其唯一差别是在 Unique 变体中 max_count 被设为 1。

class email.headerregistry.MIMEVersionHeader

实际上 MIME-Version 标头只有一个有效的值，即 1.0。 为了将来的扩展，这个标头类还支持其他的有效版本号。 如果一个版本号是 RFC 2045 [https://tools.ietf.org/html/rfc2045.html] 的有效值，则标头对象的以下属性将具有不为 None 的值:

version

字符串形式的版本号。 任何空格和/或注释都会被移除。

major

整数形式的主版本号

minor

整数形式的次版本号

class email.headerregistry.ParameterizedMIMEHeader

MIME 标头都以前缀 ‘Content-‘ 打头。 每个特定标头都具有特定的值，其描述在该标头的类之中。 有些也可以接受一个具有通用格式的补充形参形表。 这个类被用作所有接受形参的 MIME 标头的基类。

params

一个将形参名映射到形参值的字典。

class email.headerregistry.ContentTypeHeader

处理 Content-Type 标头的 ParameterizedMIMEHeader 类。

content_type

maintype/subtype 形式的内容类型字符串。

maintype

subtype

class email.headerregistry.ContentDispositionHeader

处理 Content-Disposition 标头的 ParameterizedMIMEHeader 类。

content-disposition

inline 和 attachment 是仅有的常用有效值。

class email.headerregistry.ContentTransferEncoding

处理 Content-Transfer-Encoding 标头。

cte

可用的有效值为 7bit, 8bit, base64 和 quoted-printable。 更多信息请参阅 RFC 2045 [https://tools.ietf.org/html/rfc2045.html]。

class email.headerregistry.HeaderRegistry(base_class=BaseHeader, default_class=UnstructuredHeader, use_default_map=True)

这是由 EmailPolicy 在默认情况下使用的工厂函数。 HeaderRegistry 会使用 base_class 和从它所保存的注册表中获取的专用类来构建用于动态地创建标头实例的类。 当给定的标头名称未在注册表中出现时，则会使用由 default_class 所指定的类作为专用类。 当 use_default_map 为 True (默认值) 时，则会在初始化期间把将标头名称与类的标准映射拷贝到注册表中。 base_class 始终会是所生成类的 __bases__ 列表中的最后一个类。

默认的映射有:

subject

UniqueUnstructuredHeader

date

UniqueDateHeader

resent-date

DateHeader

orig-date

UniqueDateHeader

sender

UniqueSingleAddressHeader

resent-sender

SingleAddressHeader

到

UniqueAddressHeader

resent-to

AddressHeader

cc

UniqueAddressHeader

resent-cc

AddressHeader

从

UniqueAddressHeader

resent-from

AddressHeader

reply-to

UniqueAddressHeader

HeaderRegistry 具有下列方法:

map_to_type(self, name, cls)

name 是要映射的标头名称。 它将在注册表中被转换为小写形式。 cls 是要与 base_class 一起被用来创建用于实例化与 name 相匹配的标头的类的专用类。

__getitem__(name)

构造并返回一个类来处理 name 标头的创建。

__call__(name, value)

从注册表获得与 name 相关联的专用标头 (如果 name 未在注册表中出现则使用 default_class) 并将其与 base_class 相组合以产生类，调用被构造类的构造器，传入相同的参数列表，并最终返回由此创建的类实例。

以下的类是用于表示从结构化标头解析的数据的类，并且通常会由应用程序使用以构造结构化的值并赋给特定的标头。

class email.headerregistry.Address(display_name='', username='', domain='', addr_spec=None)

用于表示电子邮件地址的类。 地址的一般形式为:

[display_name] <username@domain>

或者：

username@domain

其中每个部分都必须符合在 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 中阐述的特定语法规则。

为了方便起见可以指定 addr_spec 来替代 username 和 domain，在此情况下 username 和 domain 将从 addr_spec 中解析。 addr_spec 应当是一个正确地引用了 RFC 的字符串；如果它不是 Address 则将引发错误。 Unicode 字符也允许使用并将在序列化时被正确地编码。 但是，根据 RFC，地址的 username 部分 不允许 有 unicode。

display_name

地址的显示名称部分（如果有的话）并去除所有引用项。 如果地址没有显示名称，则此属性将为空字符串。

username

地址的 username 部分，去除所有引用项。

domain

地址的 domain 部分。

addr_spec

地址的 username@domain 部分，经过正确引用处理以作为纯地址使用（上面显示的第二种形式）。 此属性不可变。

__str__()

对象的 str 值是根据 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 规则进行引用处理的地址，但不带任何非 ASCII 字符的 Content Transfer Encoding。

为了支持 SMTP (RFC 5321 [https://tools.ietf.org/html/rfc5321.html])，Address 会处理一种特殊情况：如果 username 和 domain 均为空字符串 (或为 None)，则 Address 的字符串值为 <>。

class email.headerregistry.Group(display_name=None, addresses=None)

用于表示地址组的类。 地址组的一般形式为:

display_name: [address-list];

作为处理由组和单个地址混合构成的列表的便捷方式，Group 也可以通过将 display_name 设为 None 以用来表示不是某个组的一部分的单个地址并提供单个地址的列表作为 addresses。

display_name

组的 display_name。 如果其为 None 并且恰好有一个 Address 在 addresses 中，则 Group 表示一个不在某个组中的单独地址。

addresses

一个可能为空的表示组中地址的包含 Address 对象的元组。

__str__()

Group 的 str 值会根据 RFC 5322 [https://tools.ietf.org/html/rfc5322.html] 进行格式化，但不带任何非 ASCII 字符的 Content Transfer Encoding。 如果 display_name 为空值且只有一个单独 Address 在 addresses 列表中，则 str 值将与该单独 Address 的 str 相同。

备注

1

最初在 3.3 中作为 暂定模块 添加

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

19.1.7. email.contentmanager: 管理 MIME 内容

源代码: Lib/email/contentmanager.py [https://github.com/python/cpython/tree/3.6/Lib/email/contentmanager.py]

3.6 新版功能: 1

class email.contentmanager.ContentManager

内容管理器的基类。 提供注册 MIME 内容和其他表示形式间转换器的标准注册机制，以及 get_content 和 set_content 发送方法。

get_content(msg, *args, **kw)

基于 msg 的 mimetype 查找处理函数（参见下一段），调用该函数，传递所有参数，并返回调用的结果。 期望的效果是处理程序将从 msg 中提取有效载荷并返回编码了有关被提取数据信息的对象。

要找到处理程序，将在注册表中查找以下键，找到第一个键即停止:

表示完整 MIME 类型的字符串 (maintype/subtype)

表示 maintype 的字符串

空字符串

如果这些键都没有产生处理程序，则为完整 MIME 类型引发一个 KeyError。

set_content(msg, obj, *args, **kw)

如果 maintype 为 multipart，则引发 TypeError；否则基于 obj 的类型（参见下一段）查找处理函数，在 msg 上调用 clear_content()，并调用处理函数，传递所有参数。 预期的效果是处理程序将转换 obj 并存入 msg，并可能对 msg 进行其他更改，例如添加各种 MIME 标头来编码需要用来解释所存储数据的信息。

要找到处理程序，将获取 obj 的类型 (typ = type(obj))，并在注册表中查找以下键，找到第一个键即停止:

类型本身 (typ)

类型的完整限定名称 (typ.__module__ + '.' + typ.__qualname__)。

类型的 qualname (typ.__qualname__)

类型的 name (typ.__name__)。

如果未匹配到上述的任何一项，则在 MRO (typ.__mro__) 中为每个类型重复上述的所有检测。 最后，如果没有其他键产生处理程序，则为 None 键检测处理程序。 如果也没有 None 的处理程序，则为该类型的完整限定名称引发 KeyError。

并会添加一个 MIME-Version 标头，如果没有的话 (另请参见 MIMEPart)。

add_get_handler(key, handler)

将 handler 函数记录为 key 的处理程序。 对于可能的 key 键，请参阅 get_content()。

add_set_handler(typekey, handler)

将 handler 记录为当一个匹配 typekey 的类型对象被传递给 set_content() 时所要调用的函数。 对于可能的 typekey 值，请参阅 set_content()。

19.1.7.1. 内容管理器实例

目前 email 包只提供了一个实体内容管理器 raw_data_manager，不过在未来可能会添加更多。 raw_data_manager 是由 EmailPolicy 及其衍生工具所提供的 content_manager。

email.contentmanager.raw_data_manager

这个内容管理器仅提供了超出 Message 本身提供内容的最小接口：它只处理文本、原始字节串和 Message 对象。 不过相比基础 API，它具有显著的优势：在文本部分上执行 get_content 将返回一个 unicode 字符串而无需由应用程序来手动解码，set_content 为控制添加到一个部分的标头和控制内容传输编码格式提供了丰富的选项集合，并且它还启用了多种 add_ 方法，从而简化了多部分消息的创建过程。

email.contentmanager.get_content(msg, errors='replace')

将指定部分的有效载荷作为字符串（对于 text 部分）, EmailMessage 对象（对于 message/rfc822 部分）或 bytes 对象（对于所有其他非多部分类型）返回。 如果是在 multipart 上调用则会引发 KeyError。 如果指定部分是一个 text 部分并且指明了 errors，则会在将载荷解码为 unicode 时将其用作错误处理程序。 默认的错误处理程序是 replace。

email.contentmanager.set_content(msg, <'str'>, subtype="plain", charset='utf-8' cte=None, disposition=None, filename=None, cid=None, params=None, headers=None)

email.contentmanager.set_content(msg, <'bytes'>, maintype, subtype, cte="base64", disposition=None, filename=None, cid=None, params=None, headers=None)

email.contentmanager.set_content(msg, <'EmailMessage'>, cte=None, disposition=None, filename=None, cid=None, params=None, headers=None)

向 msg 添加标头和有效载荷:

添加一个带有 maintype/subtype 值的 Content-Type 标头。

对于 str，将 MIME maintype 设为 text，如果指定了子类型 subtype 则设为指定值，否则设为 plain。

对于 bytes，将使用指定的 maintype 和 subtype，如果未指定则会引发 TypeError。

对于 EmailMessage 对象，将 maintype 设为 message，并将指定的 subtype 设为 subtype，如果未指定则设为 rfc822。 如果 subtype 为 partial，则引发一个错误（必须使用 bytes 对象来构造 message/partial 部分）。

如果提供了 charset (这只对 str 适用)，则使用指定的字符集将字符串编码为字节串。 默认值为 utf-8。 如果指定的 charset 是某个标准 MIME 字符集名称的已知别名，则会改用该标准字符集。

如果设置了 cte，则使用指定的内容传输编码格式对有效载荷进行编码，并将 Content-Transfer-Encoding 标头设为该值。 可能的 cte 值有 quoted-printable, base64, 7bit, 8bit 和 binary。 如果输入无法以指定的编码格式被编码 (例如，对于包含非 ASCII 值的输入指定 cte 值为 7bit)，则会引发 ValueError。

对于 str 对象，如果 cte 未设置则会使用启发方式来确定最紧凑的编码格式。

对于 EmailMessage，按照 RFC 2046 [https://tools.ietf.org/html/rfc2046.html]，如果为 subtype rfc822 请求的 cte 为 quoted-printable 或 base64 ，而为 7bit 以外的任何 cte 为 subtype external-body 则会引发一个错误。 对于 message/rfc822，如果 cte 未指定则会使用 8bit。 对于所有其他 subtype 值，都会使用 7bit。

注解

cte 值为 binary 实际上还不能正确工作。 由 set_content 所修改的 EmailMessage 对象是正确的，但 BytesGenerator 不会正确地将其序列化。

如果设置了 disposition，它会被用作 Content-Disposition 标头的值。 如果未设置，并且指定了 filename，则添加值为 attachment 的标头。 如果未设置 disposition 并且也未指定 filename，则不添加标头。 disposition 的有效值仅有 attachment 和 inline。

如果设置了 filename，则将其用作 Content-Disposition 标头的 filename 参数的值。

如果设置了 cid，则添加一个 Content-ID 标头并将其值设为 cid。

如果设置了 params，则迭代其 items 方法并使用输出的 (key, value) 结果对在 Content-Type 标头上设置附加参数。

如果设置了 headers 并且为 headername: headervalue 形式的字符串的列表或为 header 对象的列表（通过一个 name 属性与字符串相区分），则将标头添加到 msg。

备注

1

最初在 3.4 中作为 暂定模块 添加

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

19.1.8. email: 示例

以下是一些如何使用 email 包来读取、写入和发送简单电子邮件以及更复杂的MIME邮件的示例。

首先，让我们看看如何创建和发送简单的文本消息（文本内容和地址都可能包含unicode字符）：

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.message import EmailMessage

# Open the plain text file whose name is in textfile for reading.
with open(textfile) as fp:
    # Create a text/plain message
    msg = EmailMessage()
    msg.set_content(fp.read())

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()

解析 RFC 822 [https://tools.ietf.org/html/rfc822.html] 标题可以通过使用 parser 模块中的类来轻松完成：

# Import the email modules we'll need
from email.parser import BytesParser, Parser
from email.policy import default

# If the e-mail headers are in a file, uncomment these two lines:
# with open(messagefile, 'rb') as fp:
#     headers = BytesParser(policy=default).parse(fp)

#  Or for parsing headers in a string (this is an uncommon operation), use:
headers = Parser(policy=default).parsestr(
        'From: Foo Bar <user@example.com>\n'
        'To: <someone_else@example.com>\n'
        'Subject: Test message\n'
        '\n'
        'Body would go here\n')

#  Now the header items can be accessed as a dictionary:
print('To: {}'.format(headers['to']))
print('From: {}'.format(headers['from']))
print('Subject: {}'.format(headers['subject']))

# You can also access the parts of the addresses:
print('Recipient username: {}'.format(headers['to'].addresses[0].username))
print('Sender name: {}'.format(headers['from'].addresses[0].display_name))

以下是如何发送包含可能在目录中的一系列家庭照片的MIME消息示例：

# Import smtplib for the actual sending function
import smtplib

# And imghdr to find the types of our images
import imghdr

# Here are the email package modules we'll need
from email.message import EmailMessage

# Create the container email message.
msg = EmailMessage()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = me
msg['To'] = ', '.join(family)
msg.preamble = 'Our family reunion'

# Open the files in binary mode.  Use imghdr to figure out the
# MIME subtype for each specific image.
for file in pngfiles:
    with open(file, 'rb') as fp:
        img_data = fp.read()
    msg.add_attachment(img_data, maintype='image',
                                 subtype=imghdr.what(None, img_data))

# Send the email via our own SMTP server.
with smtplib.SMTP('localhost') as s:
    s.send_message(msg)

以下是如何将目录的全部内容作为电子邮件消息发送的示例： 1

#!/usr/bin/env python3

"""Send the contents of a directory as a MIME message."""

import os
import smtplib
# For guessing MIME type based on file name extension
import mimetypes

from argparse import ArgumentParser

from email.message import EmailMessage
from email.policy import SMTP

def main():
    parser = ArgumentParser(description="""\
Send the contents of a directory as a MIME message.
Unless the -o option is given, the email is sent by forwarding to your local
SMTP server, which then does the normal delivery process.  Your local machine
must be running an SMTP server.
""")
    parser.add_argument('-d', '--directory',
                        help="""Mail the contents of the specified directory,
                        otherwise use the current directory.  Only the regular
                        files in the directory are sent, and we don't recurse to
                        subdirectories.""")
    parser.add_argument('-o', '--output',
                        metavar='FILE',
                        help="""Print the composed message to FILE instead of
                        sending the message to the SMTP server.""")
    parser.add_argument('-s', '--sender', required=True,
                        help='The value of the From: header (required)')
    parser.add_argument('-r', '--recipient', required=True,
                        action='append', metavar='RECIPIENT',
                        default=[], dest='recipients',
                        help='A To: header value (at least one required)')
    args = parser.parse_args()
    directory = args.directory
    if not directory:
        directory = '.'
    # Create the message
    msg = EmailMessage()
    msg['Subject'] = 'Contents of directory %s' % os.path.abspath(directory)
    msg['To'] = ', '.join(args.recipients)
    msg['From'] = args.sender
    msg.preamble = 'You will not see this in a MIME-aware mail reader.\n'

    for filename in os.listdir(directory):
        path = os.path.join(directory, filename)
        if not os.path.isfile(path):
            continue
        # Guess the content type based on the file's extension.  Encoding
        # will be ignored, although we should check for simple things like
        # gzip'd or compressed files.
        ctype, encoding = mimetypes.guess_type(path)
        if ctype is None or encoding is not None:
            # No guess could be made, or the file is encoded (compressed), so
            # use a generic bag-of-bits type.
            ctype = 'application/octet-stream'
        maintype, subtype = ctype.split('/', 1)
        with open(path, 'rb') as fp:
            msg.add_attachment(fp.read(),
                               maintype=maintype,
                               subtype=subtype,
                               filename=filename)
    # Now send or store the message
    if args.output:
        with open(args.output, 'wb') as fp:
            fp.write(msg.as_bytes(policy=SMTP))
    else:
        with smtplib.SMTP('localhost') as s:
            s.send_message(msg)

if __name__ == '__main__':
    main()

以下是如何将上述MIME消息解压缩到文件目录中的示例：

#!/usr/bin/env python3

"""Unpack a MIME message into a directory of files."""

import os
import email
import mimetypes

from email.policy import default

from argparse import ArgumentParser

def main():
    parser = ArgumentParser(description="""\
Unpack a MIME message into a directory of files.
""")
    parser.add_argument('-d', '--directory', required=True,
                        help="""Unpack the MIME message into the named
                        directory, which will be created if it doesn't already
                        exist.""")
    parser.add_argument('msgfile')
    args = parser.parse_args()

    with open(args.msgfile, 'rb') as fp:
        msg = email.message_from_binary_file(fp, policy=default)

    try:
        os.mkdir(args.directory)
    except FileExistsError:
        pass

    counter = 1
    for part in msg.walk():
        # multipart/* are just containers
        if part.get_content_maintype() == 'multipart':
            continue
        # Applications should really sanitize the given filename so that an
        # email message can't be used to overwrite important files
        filename = part.get_filename()
        if not filename:
            ext = mimetypes.guess_extension(part.get_content_type())
            if not ext:
                # Use a generic bag-of-bits extension
                ext = '.bin'
            filename = 'part-%03d%s' % (counter, ext)
        counter += 1
        with open(os.path.join(args.directory, filename), 'wb') as fp:
            fp.write(part.get_payload(decode=True))

if __name__ == '__main__':
    main()

以下是如何使用备用纯文本版本创建 HTML 消息的示例。 为了让事情变得更有趣，我们在 html 部分中包含了一个相关的图像，我们保存了一份我们要发送的内容到硬盘中，然后发送它。

#!/usr/bin/env python3

import smtplib

from email.message import EmailMessage
from email.headerregistry import Address
from email.utils import make_msgid

# Create the base text message.
msg = EmailMessage()
msg['Subject'] = "Ayons asperges pour le déjeuner"
msg['From'] = Address("Pepé Le Pew", "pepe", "example.com")
msg['To'] = (Address("Penelope Pussycat", "penelope", "example.com"),
             Address("Fabrette Pussycat", "fabrette", "example.com"))
msg.set_content("""\
Salut!

Cela ressemble à un excellent recipie[1] déjeuner.

[1] http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718

--Pepé
""")

# Add the html version.  This converts the message into a multipart/alternative
# container, with the original text message as the first part and the new html
# message as the second part.
asparagus_cid = make_msgid()
msg.add_alternative("""\
<html>
  <head></head>
  <body>
    <p>Salut!</p>
    <p>Cela ressemble à un excellent
        <a href="http://www.yummly.com/recipe/Roasted-Asparagus-Epicurious-203718">
            recipie
        </a> déjeuner.
    </p>
    <img src="cid:{asparagus_cid}" />
  </body>
</html>
""".format(asparagus_cid=asparagus_cid[1:-1]), subtype='html')
# note that we needed to peel the <> off the msgid for use in the html.

# Now add the related image to the html part.
with open("roasted-asparagus.jpg", 'rb') as img:
    msg.get_payload()[1].add_related(img.read(), 'image', 'jpeg',
                                     cid=asparagus_cid)

# Make a local copy of what we are going to send.
with open('outgoing.msg', 'wb') as f:
    f.write(bytes(msg))

# Send the message via local SMTP server.
with smtplib.SMTP('localhost') as s:
    s.send_message(msg)

如果我们发送最后一个示例中的消息，这是我们可以处理它的一种方法：

import os
import sys
import tempfile
import mimetypes
import webbrowser

# Import the email modules we'll need
from email import policy
from email.parser import BytesParser

# An imaginary module that would make this work and be safe.
from imaginary import magic_html_parser

# In a real program you'd get the filename from the arguments.
with open('outgoing.msg', 'rb') as fp:
    msg = BytesParser(policy=policy.default).parse(fp)

# Now the header items can be accessed as a dictionary, and any non-ASCII will
# be converted to unicode:
print('To:', msg['to'])
print('From:', msg['from'])
print('Subject:', msg['subject'])

# If we want to print a preview of the message content, we can extract whatever
# the least formatted payload is and print the first three lines.  Of course,
# if the message has no plain text part printing the first three lines of html
# is probably useless, but this is just a conceptual example.
simplest = msg.get_body(preferencelist=('plain', 'html'))
print()
print(''.join(simplest.get_content().splitlines(keepends=True)[:3]))

ans = input("View full message?")
if ans.lower()[0] == 'n':
    sys.exit()

# We can extract the richest alternative in order to display it:
richest = msg.get_body()
partfiles = {}
if richest['content-type'].maintype == 'text':
    if richest['content-type'].subtype == 'plain':
        for line in richest.get_content().splitlines():
            print(line)
        sys.exit()
    elif richest['content-type'].subtype == 'html':
        body = richest
    else:
        print("Don't know how to display {}".format(richest.get_content_type()))
        sys.exit()
elif richest['content-type'].content_type == 'multipart/related':
    body = richest.get_body(preferencelist=('html'))
    for part in richest.iter_attachments():
        fn = part.get_filename()
        if fn:
            extension = os.path.splitext(part.get_filename())[1]
        else:
            extension = mimetypes.guess_extension(part.get_content_type())
        with tempfile.NamedTemporaryFile(suffix=extension, delete=False) as f:
            f.write(part.get_content())
            # again strip the <> to go from email form of cid to html form.
            partfiles[part['content-id'][1:-1]] = f.name
else:
    print("Don't know how to display {}".format(richest.get_content_type()))
    sys.exit()
with tempfile.NamedTemporaryFile(mode='w', delete=False) as f:
    # The magic_html_parser has to rewrite the href="cid:...." attributes to
    # point to the filenames in partfiles.  It also has to do a safety-sanitize
    # of the html.  It could be written using html.parser.
    f.write(magic_html_parser(body.get_content(), partfiles))
webbrowser.open(f.name)
os.remove(f.name)
for fn in partfiles.values():
    os.remove(fn)

# Of course, there are lots of email messages that could break this simple
# minded program, but it will handle the most common ones.

直到输出提示，上面的输出是：

To: Penelope Pussycat <penelope@example.com>, Fabrette Pussycat <fabrette@example.com>
From: Pepé Le Pew <pepe@example.com>
Subject: Ayons asperges pour le déjeuner

Salut!

Cela ressemble à un excellent recipie[1] déjeuner.

备注

1

感谢 Matthew Dixon Cowles 提供最初的灵感和示例。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

19.1.9. email.message.Message: 使用 compat32 API 来表示电子邮件消息

Message 类与 EmailMessage 类非常相似，但没有该类所添加的方法，并且某些方法的默认行为也略有不同。 我们还在这里记录了一些虽然被 EmailMessage 类所支持但并不推荐的方法，除非你是在处理旧有代码。

在其他情况下这两个类的理念和结构都是相同的。

本文档描述了默认 (对于 Message) 策略 Compat32 之下的行为。 如果你要使用其他策略，你应当改用 EmailMessage 类。

电子邮件消息是由多个 标头 和一个 payload 载荷组成的。 标头必须为 RFC 5233 [https://tools.ietf.org/html/rfc5233.html] 风格的名称和值，其中字段名和值由冒号分隔。 冒号不是字段名或字段值的组成部分。 载荷可以是简单的文本消息，或是二进制对象，或是多个子消息的结构化序列，每个子消息都有自己的标头集合和自己的载荷。 后一种类型的载荷是由具有 multipart/* 或 message/rfc822 等 MIME 类型的消息来指明的。

Message 对象所提供了概念化模型是由标头组成的有序字典，加上用于访问标头中的特殊信息以及访问载荷的额外方法，以便能生成消息的序列化版本，并递归地遍历对象树。 请注意重复的标头是受支持的，但必须使用特殊的方法来访问它们。

Message 伪字典以标头名作为索引，标头名必须为 ASCII 值。 字典的值为应当只包含 ASCII 字符的字符串；对于非 ASCII 输入有一些特殊处理，但这并不总能产生正确的结果。 标头以保留原大小写的形式存储和返回，但字段名称匹配对大小写不敏感。 还可能会有一个单独的封包标头，也称 Unix-From 标头或 From_ 标头。 载荷 对于简单消息对象的情况是一个字符串或字节串，对于 MIME 容器文档的情况 (例如 multipart/* 和 message/rfc822) 则是一个 Message 对象。

以下是 Message 类的方法:

class email.message.Message(policy=compat32)

如果指定了 policy (它必须为 policy 类的实例) 则使用它所设置的规则来更新和序列化消息的表示形式。 如果未设置 policy，则使用 compat32 策略，该策略会保持对 Python 3.2 版 email 包的向下兼容性。 更多信息请参阅 policy 文档。

在 3.3 版更改: 增加了 policy 关键字参数。

as_string(unixfrom=False, maxheaderlen=0, policy=None)

以展平的字符串形式返回整个消息对象。 或可选的 unixfrom 为真值，返回的字符串会包括封包标头。 unixfrom 的默认值是 False。 出于保持向下兼容性的原因，maxheaderlen 的默认值是 0，因此如果你想要不同的值你必须显式地重载它（在策略中为 max_line_length 指定的值将被此方法忽略）。 policy 参数可被用于覆盖从消息实例获取的默认策略。 这可以用来对该方法所输出的格式进行一些控制，因为指定的 policy 将被传递给 Generator。

如果需要填充默认值以完成对字符串的转换则展平消息可能触发对 Message 的修改（例如，MIME 边界可能会被生成或被修改）。

请注意此方法是出于便捷原因提供的，可能无法总是以你想要的方式格式化消息。 例如，在默认情况下它不会按 unix mbox 格式的要求对以 From 打头的行执行调整。 为了获得更高灵活性，请实例化一个 Generator 实例并直接使用其 flatten() 方法。 例如:

from io import StringIO
from email.generator import Generator
fp = StringIO()
g = Generator(fp, mangle_from_=True, maxheaderlen=60)
g.flatten(msg)
text = fp.getvalue()

如果消息对象包含未按照 RFC 标准进行编码的二进制数据，则这些不合规数据将被 unicode “unknown character” 码位值所替代。 （另请参阅 as_bytes() 和 BytesGenerator。）

在 3.4 版更改: 增加了 policy 关键字参数。

__str__()

与 as_string() 等价。 这将让 str(msg) 产生一个包含已格式化消息的字符号。

as_bytes(unixfrom=False, policy=None)

以字节串对象的形式返回整个扁平化后的消息。 当可选的 unixfrom 为真值时，返回的字符串会包括封包标头。 unixfrom 的默认值为 False。 policy 参数可被用于重载从消息实例获取的默认策略。 这可被用来控制该方法所产生的部分格式化效果，因为指定的 policy 将被传递给 BytesGenerator。

如果需要填充默认值以完成对字符串的转换则展平消息可能触发对 Message 的修改（例如，MIME 边界可能会被生成或被修改）。

请注意此方法是出于便捷原因提供的，可能无法总是以你想要的方式格式化消息。 例如，在默认情况下它不会按 unix mbox 格式的要求对以 From 打头的行执行调整。 为了获得更高灵活性，请实例化一个 BytesGenerator 实例并直接使用其 flatten() 方法。 例如:

from io import BytesIO
from email.generator import BytesGenerator
fp = BytesIO()
g = BytesGenerator(fp, mangle_from_=True, maxheaderlen=60)
g.flatten(msg)
text = fp.getvalue()

3.4 新版功能.

__bytes__()

与 as_bytes() 等价。 这将让 bytes(msg) 产生一个包含已格式化消息的字节串对象。

3.4 新版功能.

is_multipart()

如果该消息的载荷是一个子 Message 对象列表则返回 True，否则返回 False。 当 is_multipart() 返回 False 时，载荷应当是一个字符串对象（有可能是一个 CTE 编码的二进制载荷）。 （请注意 is_multipart() 返回 True 并不意味着 “msg.get_content_maintype() == ‘multipart’” 将返回 True。 例如，is_multipart 在 Message 类型为 message/rfc822 时也将返回 True。）

set_unixfrom(unixfrom)

将消息的封包标头设为 unixfrom，这应当是一个字符串。

get_unixfrom()

返回消息的信封头。如果信封头从未被设置过，默认返回 None 。

attach(payload)

将给定的 payload 添加到当前载荷中，当前载荷在该调用之前必须为 None 或是一个 Message 对象列表。 在调用之后，此载荷将总是一个 Message 对象列表。 如果你想将此载荷设为一个标量对象（如字符串），请改用 set_payload()。

这是一个过时的方法。 在 EmailMessage 类上它的功能已被 set_content() 及相应的 make 和 add 方法所替代。

get_payload(i=None, decode=False)

返回当前的载荷，它在 is_multipart() 为 True 时将是一个 Message 对象列表，在 is_multipart() 为 False 时则是一个字符串。 如果该载荷是一个列表且你修改了这个列表对象，那么你就是原地修改了消息的载荷。

传入可选参数 i 时，如果 is_multipart() 为 True，get_payload() 将返回载荷从零开始计数的第 i 个元素。 如果 i 小于 0 或大于等于载荷中的条目数则将引发 IndexError。 如果载荷是一个字符串 (即 is_multipart() 为 False) 且给出了 i，则会引发 TypeError。

可选的 decode 是一个指明载荷是否应根据 Content-Transfer-Encoding 标头被解码的旗标。 当其值为 True 且消息没有多个部分时，如果此标头值为 quoted-printable 或 base64 则载荷将被解码。 如果使用了其他编码格式，或者找不到 Content-Transfer-Encoding 标头时，载荷将被原样返回（不编码）。 在所有情况下返回值都是二进制数据。 如果消息有多个部分且 decode 旗标为 True，则将返回 None。 如果载荷为 base64 但内容不完全正确（如缺少填充符、存在 base64 字母表以外的字符等），则将在消息的缺陷属性中添加适当的缺陷值 (分别为 InvalidBase64PaddingDefect 或 InvalidBase64CharactersDefect)。

当 decode 为 False (默认值) 时消息体会作为字符串返回而不解码 Content-Transfer-Encoding。 但是，对于 Content-Transfer-Encoding 为 8bit 的情况，会尝试使用 Content-Type 标头指定的 charset 来解码原始字节串，并使用 replace 错误处理程序。 如果未指定 charset，或者如果指定的 charset 未被 email 包所识别，则会使用默认的 ASCII 字符集来解码消息体。

这是一个过时的方法。 在 EmailMessage 类上它的功能已被 get_content() 和 iter_parts() 方法所替代。

set_payload(payload, charset=None)

将整个消息对象的载荷设为 payload。 客户端要负责确保载荷的不变性。 可选的 charset 用于设置消息的默认字符集；详情请参阅 set_charset()。

这是一个过时的方法。 在 EmailMessage 类上它的功能已被 set_content() 方法所替代。

set_charset(charset)

Set the character set of the payload to charset, which can either be a Charset instance (see email.charset), a string naming a character set, or None. If it is a string, it will be converted to a Charset instance. If charset is None, the charset parameter will be removed from the Content-Type header (the message will not be otherwise modified). Anything else will generate a TypeError.

If there is no existing MIME-Version header one will be added. If there is no existing Content-Type header, one will be added with a value of text/plain. Whether the Content-Type header already exists or not, its charset parameter will be set to charset.output_charset. If charset.input_charset and charset.output_charset differ, the payload will be re-encoded to the output_charset. If there is no existing Content-Transfer-Encoding header, then the payload will be transfer-encoded, if needed, using the specified Charset, and a header with the appropriate value will be added. If a Content-Transfer-Encoding header already exists, the payload is assumed to already be correctly encoded using that Content-Transfer-Encoding and is not modified.

This is a legacy method. On the EmailMessage class its functionality is replaced by the charset parameter of the email.emailmessage.EmailMessage.set_content() method.

get_charset()

Return the Charset instance associated with the message’s payload.

This is a legacy method. On the EmailMessage class it always returns None.

The following methods implement a mapping-like interface for accessing the message’s RFC 2822 [https://tools.ietf.org/html/rfc2822.html] headers. Note that there are some semantic differences between these methods and a normal mapping (i.e. dictionary) interface. For example, in a dictionary there are no duplicate keys, but here there may be duplicate message headers. Also, in dictionaries there is no guaranteed order to the keys returned by keys(), but in a Message object, headers are always returned in the order they appeared in the original message, or were added to the message later. Any header deleted and then re-added are always appended to the end of the header list.

These semantic differences are intentional and are biased toward maximal convenience.

还请留意，无论在什么情况下，消息当中的任何信封头字段都不会包含在映射接口当中。

In a model generated from bytes, any header values that (in contravention of the RFCs) contain non-ASCII bytes will, when retrieved through this interface, be represented as Header objects with a charset of unknown-8bit.

__len__()

返回头字段的总数，重复的也计算在内。

__contains__(name)

Return true if the message object has a field named name. Matching is done case-insensitively and name should not include the trailing colon. Used for the in operator, e.g.:

if 'message-id' in myMessage:
   print('Message-ID:', myMessage['message-id'])

__getitem__(name)

Return the value of the named header field. name should not include the colon field separator. If the header is missing, None is returned; a KeyError is never raised.

Note that if the named field appears more than once in the message’s headers, exactly which of those field values will be returned is undefined. Use the get_all() method to get the values of all the extant named headers.

__setitem__(name, val)

Add a header to the message with field name name and value val. The field is appended to the end of the message’s existing fields.

请注意，这个方法 既不会 覆盖 也不会 删除任何字段名重名的已有字段。如果你确实想保证新字段是整个信息头当中唯一拥有 name 字段名的字段，你需要先把旧字段删除。例如：

del msg['subject']
msg['subject'] = 'Python roolz!'

__delitem__(name)

删除信息头当中字段名匹配 name 的所有字段。如果匹配指定名称的字段没有找到，也不会抛出任何异常。

keys()

以列表形式返回消息头中所有的字段名。

values()

以列表形式返回消息头中所有的字段值。

items()

以二元元组的列表形式返回消息头中所有的字段名和字段值。

get(name, failobj=None)

Return the value of the named header field. This is identical to __getitem__() except that optional failobj is returned if the named header is missing (defaults to None).

以下是一些有用的附加方法:

get_all(name, failobj=None)

返回字段名为 name 的所有字段值的列表。如果信息内不存在匹配的字段，返回 failobj （其默认值为 None ）。

add_header(_name, _value, **_params)

高级头字段设定。这个方法与 __setitem__() 类似，不过你可以使用关键字参数为字段提供附加参数。 _name 是字段名， _value 是字段 主 值。

For each item in the keyword argument dictionary _params, the key is taken as the parameter name, with underscores converted to dashes (since dashes are illegal in Python identifiers). Normally, the parameter will be added as key="value" unless the value is None, in which case only the key will be added. If the value contains non-ASCII characters, it can be specified as a three tuple in the format (CHARSET, LANGUAGE, VALUE), where CHARSET is a string naming the charset to be used to encode the value, LANGUAGE can usually be set to None or the empty string (see RFC 2231 [https://tools.ietf.org/html/rfc2231.html] for other possibilities), and VALUE is the string value containing non-ASCII code points. If a three tuple is not passed and the value contains non-ASCII characters, it is automatically encoded in RFC 2231 [https://tools.ietf.org/html/rfc2231.html] format using a CHARSET of utf-8 and a LANGUAGE of None.

以下是为示例代码:

msg.add_header('Content-Disposition', 'attachment', filename='bud.gif')

会添加一个形如下文的头字段：

Content-Disposition: attachment; filename="bud.gif"

使用非 ASCII 字符的示例代码:

msg.add_header('Content-Disposition', 'attachment',
               filename=('iso-8859-1', '', 'Fußballer.ppt'))

它的输出结果为

Content-Disposition: attachment; filename*="iso-8859-1''Fu%DFballer.ppt"

replace_header(_name, _value)

Replace a header. Replace the first header found in the message that matches _name, retaining header order and field name case. If no matching header was found, a KeyError is raised.

get_content_type()

Return the message’s content type. The returned string is coerced to lower case of the form maintype/subtype. If there was no Content-Type header in the message the default type as given by get_default_type() will be returned. Since according to RFC 2045 [https://tools.ietf.org/html/rfc2045.html], messages always have a default type, get_content_type() will always return a value.

RFC 2045 [https://tools.ietf.org/html/rfc2045.html] defines a message’s default type to be text/plain unless it appears inside a multipart/digest container, in which case it would be message/rfc822. If the Content-Type header has an invalid type specification, RFC 2045 [https://tools.ietf.org/html/rfc2045.html] mandates that the default type be text/plain.

get_content_maintype()

返回信息的主要内容类型。准确来说，此方法返回的是 get_content_type() 方法所返回的形如 maintype/subtype 的字符串当中的 maintype 部分。

get_content_subtype()

返回信息的子内容类型。准确来说，此方法返回的是 get_content_type() 方法所返回的形如 maintype/subtype 的字符串当中的 subtype 部分。

get_default_type()

返回默认的内容类型。绝大多数的信息，其默认内容类型都是 text/plain 。作为 multipart/digest 容器内子部分的信息除外，它们的默认内容类型是 message/rfc822 。

set_default_type(ctype)

Set the default content type. ctype should either be text/plain or message/rfc822, although this is not enforced. The default content type is not stored in the Content-Type header.

get_params(failobj=None, header='content-type', unquote=True)

Return the message’s Content-Type parameters, as a list. The elements of the returned list are 2-tuples of key/value pairs, as split on the '=' sign. The left hand side of the '=' is the key, while the right hand side is the value. If there is no '=' sign in the parameter the value is the empty string, otherwise the value is as described in get_param() and is unquoted if optional unquote is True (the default).

Optional failobj is the object to return if there is no Content-Type header. Optional header is the header to search instead of Content-Type.

This is a legacy method. On the EmailMessage class its functionality is replaced by the params property of the individual header objects returned by the header access methods.

get_param(param, failobj=None, header='content-type', unquote=True)

Return the value of the Content-Type header’s parameter param as a string. If the message has no Content-Type header or if there is no such parameter, then failobj is returned (defaults to None).

Optional header if given, specifies the message header to use instead of Content-Type.

Parameter keys are always compared case insensitively. The return value can either be a string, or a 3-tuple if the parameter was RFC 2231 [https://tools.ietf.org/html/rfc2231.html] encoded. When it’s a 3-tuple, the elements of the value are of the form (CHARSET, LANGUAGE, VALUE). Note that both CHARSET and LANGUAGE can be None, in which case you should consider VALUE to be encoded in the us-ascii charset. You can usually ignore LANGUAGE.

If your application doesn’t care whether the parameter was encoded as in RFC 2231 [https://tools.ietf.org/html/rfc2231.html], you can collapse the parameter value by calling email.utils.collapse_rfc2231_value(), passing in the return value from get_param(). This will return a suitably decoded Unicode string when the value is a tuple, or the original string unquoted if it isn’t. For example:

rawparam = msg.get_param('foo')
param = email.utils.collapse_rfc2231_value(rawparam)

In any case, the parameter value (either the returned string, or the VALUE item in the 3-tuple) is always unquoted, unless unquote is set to False.

This is a legacy method. On the EmailMessage class its functionality is replaced by the params property of the individual header objects returned by the header access methods.

set_param(param, value, header='Content-Type', requote=True, charset=None, language='', replace=False)

Set a parameter in the Content-Type header. If the parameter already exists in the header, its value will be replaced with value. If the Content-Type header as not yet been defined for this message, it will be set to text/plain and the new parameter value will be appended as per RFC 2045 [https://tools.ietf.org/html/rfc2045.html].

Optional header specifies an alternative header to Content-Type, and all parameters will be quoted as necessary unless optional requote is False (the default is True).

If optional charset is specified, the parameter will be encoded according to RFC 2231 [https://tools.ietf.org/html/rfc2231.html]. Optional language specifies the RFC 2231 language, defaulting to the empty string. Both charset and language should be strings.

如果 replace 为 False （默认值），该头字段会被移动到所有头字段的末尾。如果 replace 为 True ，字段会被原地更新。

在 3.4 版更改: 添加了 replace 关键字。

del_param(param, header='content-type', requote=True)

Remove the given parameter completely from the Content-Type header. The header will be re-written in place without the parameter or its value. All values will be quoted as necessary unless requote is False (the default is True). Optional header specifies an alternative to Content-Type.

set_type(type, header='Content-Type', requote=True)

Set the main type and subtype for the Content-Type header. type must be a string in the form maintype/subtype, otherwise a ValueError is raised.

This method replaces the Content-Type header, keeping all the parameters in place. If requote is False, this leaves the existing header’s quoting as is, otherwise the parameters will be quoted (the default).

An alternative header can be specified in the header argument. When the Content-Type header is set a MIME-Version header is also added.

This is a legacy method. On the EmailMessage class its functionality is replaced by the make_ and add_ methods.

get_filename(failobj=None)

返回信息头当中 Content-Disposition 字段当中名为 filename 的参数值。如果该字段当中没有此参数，该方法会退而寻找 Content-Type 字段当中的 name 参数值。如果这个也没有找到，或者这些个字段压根就不存在，返回 failobj 。返回的字符串永远按照 email.utils.unquote() 方法去除引号。

get_boundary(failobj=None)

返回信息头当中 Content-Type 字段当中名为 boundary 的参数值。如果字段当中没有此参数，或者这些个字段压根就不存在，返回 failobj 。返回的字符串永远按照 email.utils.unquote() 方法去除引号。

set_boundary(boundary)

将 Content-Type 头字段的 boundary 参数设置为 boundary 。 set_boundary() 方法永远都会在必要的时候为 boundary 添加引号。如果信息对象中没有 Content-Type 头字段，抛出 HeaderParseError 异常。

Note that using this method is subtly different than deleting the old Content-Type header and adding a new one with the new boundary via add_header(), because set_boundary() preserves the order of the Content-Type header in the list of headers. However, it does not preserve any continuation lines which may have been present in the original Content-Type header.

get_content_charset(failobj=None)

返回 Content-Type 头字段中的 charset 参数，强制小写。如果字段当中没有此参数，或者这个字段压根不存在，返回 failobj 。

Note that this method differs from get_charset() which returns the Charset instance for the default encoding of the message body.

get_charsets(failobj=None)

返回一个包含了信息内所有字符集名字的列表。如果信息是 multipart 类型的，那么列表当中的每一项都对应其负载的子部分的字符集名字。否则，该列表是一个长度为1的列表。

Each item in the list will be a string which is the value of the charset parameter in the Content-Type header for the represented subpart. However, if the subpart has no Content-Type header, no charset parameter, or is not of the text main MIME type, then that item in the returned list will be failobj.

get_content_disposition()

如果信息的 Content-Disposition 头字段存在，返回其字段值；否则返回 None 。返回的值均为小写，不包含参数。如果信息遵循 RFC 2183 [https://tools.ietf.org/html/rfc2183.html] 标准，则返回值只可能在 inline 、 attachment 和 None 之间选择。

3.5 新版功能.

walk()

walk() 方法是一个多功能生成器。它可以被用来以深度优先顺序遍历信息对象树的所有部分和子部分。一般而言， walk() 会被用作 for 循环的迭代器，每一次迭代都返回其下一个子部分。

以下例子会打印出一封具有多部分结构之信息的每个部分的 MIME 类型。

>>> for part in msg.walk():
...     print(part.get_content_type())
multipart/report
text/plain
message/delivery-status
text/plain
text/plain
message/rfc822
text/plain

walk 会遍历所有 is_multipart() 方法返回 True 的部分之子部分，哪怕 msg.get_content_maintype() == 'multipart' 返回的是 False 。使用 _structure 除错帮助函数可以帮助我们在下面这个例子当中看清楚这一点：

>>> for part in msg.walk():
...     print(part.get_content_maintype() == 'multipart'),
...           part.is_multipart())
True True
False False
False True
False False
False False
False True
False False
>>> _structure(msg)
multipart/report
    text/plain
message/delivery-status
    text/plain
    text/plain
message/rfc822
    text/plain

在这里， message 的部分并非 multiparts ，但是它们真的包含子部分！ is_multipart() 返回 True ， walk 也深入进这些子部分中。

Message objects can also optionally contain two instance attributes, which can be used when generating the plain text of a MIME message.

preamble

MIME 文档格式在标头之后的空白行以及第一个多部分的分界字符串之间允许添加一些文本， 通常，此文本在支持 MIME 的邮件阅读器中永远不可见，因为它处在标准 MIME 防护范围之外。 但是，当查看消息的原始文本，或当在不支持 MIME 的阅读器中查看消息时，此文本会变得可见。

preamble 属性包含 MIME 文档开头部分的这些处于保护范围之外的文本。 当 Parser 在标头之后及第一个分界字符串之前发现一些文本时，它会将这些文本赋值给消息的 preamble 属性。 当 Generator 写出 MIME 消息的纯文本表示形式时，如果它发现消息具有 preamble 属性，它将在标头及第一个分界之间区域写出这些文本。 请参阅 email.parser 和 email.generator 了解更多细节。

请注意如果消息对象没有前导文本，则 preamble 属性将为 None。

epilogue

The epilogue attribute acts the same way as the preamble attribute, except that it contains text that appears between the last boundary and the end of the message.

You do not need to set the epilogue to the empty string in order for the Generator to print a newline at the end of the file.

defects

defects 属性包含在解析消息时发现的所有问题的列表。 请参阅 email.errors 了解可能的解析缺陷的详细描述。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

19.1.10. email.mime: Creating email and MIME objects from scratch

Source code: Lib/email/mime/ [https://github.com/python/cpython/tree/3.6/Lib/email/mime/]

This module is part of the legacy (Compat32) email API. Its functionality is partially replaced by the contentmanager in the new API, but in certain applications these classes may still be useful, even in non-legacy code.

Ordinarily, you get a message object structure by passing a file or some text to a parser, which parses the text and returns the root message object. However you can also build a complete message structure from scratch, or even individual Message objects by hand. In fact, you can also take an existing structure and add new Message objects, move them around, etc. This makes a very convenient interface for slicing-and-dicing MIME messages.

You can create a new object structure by creating Message instances, adding attachments and all the appropriate headers manually. For MIME messages though, the email package provides some convenient subclasses to make things easier.

Here are the classes:

class email.mime.base.MIMEBase(_maintype, _subtype, *, policy=compat32, **_params)

Module: email.mime.base

This is the base class for all the MIME-specific subclasses of Message. Ordinarily you won’t create instances specifically of MIMEBase, although you could. MIMEBase is provided primarily as a convenient base class for more specific MIME-aware subclasses.

_maintype is the Content-Type major type (e.g. text or image), and _subtype is the Content-Type minor type (e.g. plain or gif). _params is a parameter key/value dictionary and is passed directly to Message.add_header.

If policy is specified, (defaults to the compat32 policy) it will be passed to Message.

The MIMEBase class always adds a Content-Type header (based on _maintype, _subtype, and _params), and a MIME-Version header (always set to 1.0).

在 3.6 版更改: Added policy keyword-only parameter.

class email.mime.nonmultipart.MIMENonMultipart

Module: email.mime.nonmultipart

A subclass of MIMEBase, this is an intermediate base class for MIME messages that are not multipart. The primary purpose of this class is to prevent the use of the attach() method, which only makes sense for multipart messages. If attach() is called, a MultipartConversionError exception is raised.

class email.mime.multipart.MIMEMultipart(_subtype='mixed', boundary=None, _subparts=None, *, policy=compat32, **_params)

Module: email.mime.multipart

A subclass of MIMEBase, this is an intermediate base class for MIME messages that are multipart. Optional _subtype defaults to mixed, but can be used to specify the subtype of the message. A Content-Type header of multipart/_subtype will be added to the message object. A MIME-Version header will also be added.

Optional boundary is the multipart boundary string. When None (the default), the boundary is calculated when needed (for example, when the message is serialized).

_subparts is a sequence of initial subparts for the payload. It must be possible to convert this sequence to a list. You can always attach new subparts to the message by using the Message.attach method.

Optional policy argument defaults to compat32.

Additional parameters for the Content-Type header are taken from the keyword arguments, or passed into the _params argument, which is a keyword dictionary.

在 3.6 版更改: Added policy keyword-only parameter.

class email.mime.application.MIMEApplication(_data, _subtype='octet-stream', _encoder=email.encoders.encode_base64, *, policy=compat32, **_params)

Module: email.mime.application

A subclass of MIMENonMultipart, the MIMEApplication class is used to represent MIME message objects of major type application. _data is a string containing the raw byte data. Optional _subtype specifies the MIME subtype and defaults to octet-stream.

Optional _encoder is a callable (i.e. function) which will perform the actual encoding of the data for transport. This callable takes one argument, which is the MIMEApplication instance. It should use get_payload() and set_payload() to change the payload to encoded form. It should also add any Content-Transfer-Encoding or other headers to the message object as necessary. The default encoding is base64. See the email.encoders module for a list of the built-in encoders.

Optional policy argument defaults to compat32.

_params are passed straight through to the base class constructor.

在 3.6 版更改: Added policy keyword-only parameter.

class email.mime.audio.MIMEAudio(_audiodata, _subtype=None, _encoder=email.encoders.encode_base64, *, policy=compat32, **_params)

Module: email.mime.audio

A subclass of MIMENonMultipart, the MIMEAudio class is used to create MIME message objects of major type audio. _audiodata is a string containing the raw audio data. If this data can be decoded by the standard Python module sndhdr, then the subtype will be automatically included in the Content-Type header. Otherwise you can explicitly specify the audio subtype via the _subtype argument. If the minor type could not be guessed and _subtype was not given, then TypeError is raised.

Optional _encoder is a callable (i.e. function) which will perform the actual encoding of the audio data for transport. This callable takes one argument, which is the MIMEAudio instance. It should use get_payload() and set_payload() to change the payload to encoded form. It should also add any Content-Transfer-Encoding or other headers to the message object as necessary. The default encoding is base64. See the email.encoders module for a list of the built-in encoders.

Optional policy argument defaults to compat32.

_params are passed straight through to the base class constructor.

在 3.6 版更改: Added policy keyword-only parameter.

class email.mime.image.MIMEImage(_imagedata, _subtype=None, _encoder=email.encoders.encode_base64, *, policy=compat32, **_params)

Module: email.mime.image

A subclass of MIMENonMultipart, the MIMEImage class is used to create MIME message objects of major type image. _imagedata is a string containing the raw image data. If this data can be decoded by the standard Python module imghdr, then the subtype will be automatically included in the Content-Type header. Otherwise you can explicitly specify the image subtype via the _subtype argument. If the minor type could not be guessed and _subtype was not given, then TypeError is raised.

Optional _encoder is a callable (i.e. function) which will perform the actual encoding of the image data for transport. This callable takes one argument, which is the MIMEImage instance. It should use get_payload() and set_payload() to change the payload to encoded form. It should also add any Content-Transfer-Encoding or other headers to the message object as necessary. The default encoding is base64. See the email.encoders module for a list of the built-in encoders.

Optional policy argument defaults to compat32.

_params are passed straight through to the MIMEBase constructor.

在 3.6 版更改: Added policy keyword-only parameter.

class email.mime.message.MIMEMessage(_msg, _subtype='rfc822', *, policy=compat32)

Module: email.mime.message

A subclass of MIMENonMultipart, the MIMEMessage class is used to create MIME objects of main type message. _msg is used as the payload, and must be an instance of class Message (or a subclass thereof), otherwise a TypeError is raised.

Optional _subtype sets the subtype of the message; it defaults to rfc822.

Optional policy argument defaults to compat32.

在 3.6 版更改: Added policy keyword-only parameter.

class email.mime.text.MIMEText(_text, _subtype='plain', _charset=None, *, policy=compat32)

Module: email.mime.text

A subclass of MIMENonMultipart, the MIMEText class is used to create MIME objects of major type text. _text is the string for the payload. _subtype is the minor type and defaults to plain. _charset is the character set of the text and is passed as an argument to the MIMENonMultipart constructor; it defaults to us-ascii if the string contains only ascii code points, and utf-8 otherwise. The _charset parameter accepts either a string or a Charset instance.

Unless the _charset argument is explicitly set to None, the MIMEText object created will have both a Content-Type header with a charset parameter, and a Content-Transfer-Encoding header. This means that a subsequent set_payload call will not result in an encoded payload, even if a charset is passed in the set_payload command. You can “reset” this behavior by deleting the Content-Transfer-Encoding header, after which a set_payload call will automatically encode the new payload (and add a new Content-Transfer-Encoding header).

Optional policy argument defaults to compat32.

在 3.5 版更改: _charset also accepts Charset instances.

在 3.6 版更改: Added policy keyword-only parameter.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

19.1.11. email.header: Internationalized headers

Source code: Lib/email/header.py [https://github.com/python/cpython/tree/3.6/Lib/email/header.py]

This module is part of the legacy (Compat32) email API. In the current API encoding and decoding of headers is handled transparently by the dictionary-like API of the EmailMessage class. In addition to uses in legacy code, this module can be useful in applications that need to completely control the character sets used when encoding headers.

本段落中的剩余文本是该模块的原始文档。

RFC 2822 [https://tools.ietf.org/html/rfc2822.html] is the base standard that describes the format of email messages. It derives from the older RFC 822 [https://tools.ietf.org/html/rfc822.html] standard which came into widespread use at a time when most email was composed of ASCII characters only. RFC 2822 [https://tools.ietf.org/html/rfc2822.html] is a specification written assuming email contains only 7-bit ASCII characters.

Of course, as email has been deployed worldwide, it has become internationalized, such that language specific character sets can now be used in email messages. The base standard still requires email messages to be transferred using only 7-bit ASCII characters, so a slew of RFCs have been written describing how to encode email containing non-ASCII characters into RFC 2822 [https://tools.ietf.org/html/rfc2822.html]-compliant format. These RFCs include RFC 2045 [https://tools.ietf.org/html/rfc2045.html], RFC 2046 [https://tools.ietf.org/html/rfc2046.html], RFC 2047 [https://tools.ietf.org/html/rfc2047.html], and RFC 2231 [https://tools.ietf.org/html/rfc2231.html]. The email package supports these standards in its email.header and email.charset modules.

If you want to include non-ASCII characters in your email headers, say in the Subject or To fields, you should use the Header class and assign the field in the Message object to an instance of Header instead of using a string for the header value. Import the Header class from the email.header module. For example:

>>> from email.message import Message
>>> from email.header import Header
>>> msg = Message()
>>> h = Header('p\xf6stal', 'iso-8859-1')
>>> msg['Subject'] = h
>>> msg.as_string()
'Subject: =?iso-8859-1?q?p=F6stal?=\n\n'

Notice here how we wanted the Subject field to contain a non-ASCII character? We did this by creating a Header instance and passing in the character set that the byte string was encoded in. When the subsequent Message instance was flattened, the Subject field was properly RFC 2047 [https://tools.ietf.org/html/rfc2047.html] encoded. MIME-aware mail readers would show this header using the embedded ISO-8859-1 character.

Here is the Header class description:

class email.header.Header(s=None, charset=None, maxlinelen=None, header_name=None, continuation_ws=' ', errors='strict')

Create a MIME-compliant header that can contain strings in different character sets.

Optional s is the initial header value. If None (the default), the initial header value is not set. You can later append to the header with append() method calls. s may be an instance of bytes or str, but see the append() documentation for semantics.

Optional charset serves two purposes: it has the same meaning as the charset argument to the append() method. It also sets the default character set for all subsequent append() calls that omit the charset argument. If charset is not provided in the constructor (the default), the us-ascii character set is used both as s’s initial charset and as the default for subsequent append() calls.

The maximum line length can be specified explicitly via maxlinelen. For splitting the first line to a shorter value (to account for the field header which isn’t included in s, e.g. Subject) pass in the name of the field in header_name. The default maxlinelen is 76, and the default value for header_name is None, meaning it is not taken into account for the first line of a long, split header.

Optional continuation_ws must be RFC 2822 [https://tools.ietf.org/html/rfc2822.html]-compliant folding whitespace, and is usually either a space or a hard tab character. This character will be prepended to continuation lines. continuation_ws defaults to a single space character.

Optional errors is passed straight through to the append() method.

append(s, charset=None, errors='strict')

Append the string s to the MIME header.

Optional charset, if given, should be a Charset instance (see email.charset) or the name of a character set, which will be converted to a Charset instance. A value of None (the default) means that the charset given in the constructor is used.

s may be an instance of bytes or str. If it is an instance of bytes, then charset is the encoding of that byte string, and a UnicodeError will be raised if the string cannot be decoded with that character set.

If s is an instance of str, then charset is a hint specifying the character set of the characters in the string.

In either case, when producing an RFC 2822 [https://tools.ietf.org/html/rfc2822.html]-compliant header using RFC 2047 [https://tools.ietf.org/html/rfc2047.html] rules, the string will be encoded using the output codec of the charset. If the string cannot be encoded using the output codec, a UnicodeError will be raised.

Optional errors is passed as the errors argument to the decode call if s is a byte string.

encode(splitchars=';, \t', maxlinelen=None, linesep='\n')

Encode a message header into an RFC-compliant format, possibly wrapping long lines and encapsulating non-ASCII parts in base64 or quoted-printable encodings.

Optional splitchars is a string containing characters which should be given extra weight by the splitting algorithm during normal header wrapping. This is in very rough support of RFC 2822 [https://tools.ietf.org/html/rfc2822.html]’s ‘higher level syntactic breaks’: split points preceded by a splitchar are preferred during line splitting, with the characters preferred in the order in which they appear in the string. Space and tab may be included in the string to indicate whether preference should be given to one over the other as a split point when other split chars do not appear in the line being split. Splitchars does not affect RFC 2047 [https://tools.ietf.org/html/rfc2047.html] encoded lines.

maxlinelen, if given, overrides the instance’s value for the maximum line length.

linesep specifies the characters used to separate the lines of the folded header. It defaults to the most useful value for Python application code (\n), but \r\n can be specified in order to produce headers with RFC-compliant line separators.

在 3.2 版更改: Added the linesep argument.

The Header class also provides a number of methods to support standard operators and built-in functions.

__str__()

Returns an approximation of the Header as a string, using an unlimited line length. All pieces are converted to unicode using the specified encoding and joined together appropriately. Any pieces with a charset of 'unknown-8bit' are decoded as ASCII using the 'replace' error handler.

在 3.2 版更改: Added handling for the 'unknown-8bit' charset.

__eq__(other)

This method allows you to compare two Header instances for equality.

__ne__(other)

This method allows you to compare two Header instances for inequality.

The email.header module also provides the following convenient functions.

email.header.decode_header(header)

Decode a message header value without converting the character set. The header value is in header.

This function returns a list of (decoded_string, charset) pairs containing each of the decoded parts of the header. charset is None for non-encoded parts of the header, otherwise a lower case string containing the name of the character set specified in the encoded string.

以下是为示例代码:

>>> from email.header import decode_header
>>> decode_header('=?iso-8859-1?q?p=F6stal?=')
[(b'p\xf6stal', 'iso-8859-1')]

email.header.make_header(decoded_seq, maxlinelen=None, header_name=None, continuation_ws=' ')

Create a Header instance from a sequence of pairs as returned by decode_header().

decode_header() takes a header value string and returns a sequence of pairs of the format (decoded_string, charset) where charset is the name of the character set.

This function takes one of those sequence of pairs and returns a Header instance. Optional maxlinelen, header_name, and continuation_ws are as in the Header constructor.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

19.1.12. email.charset: Representing character sets

Source code: Lib/email/charset.py [https://github.com/python/cpython/tree/3.6/Lib/email/charset.py]

This module is part of the legacy (Compat32) email API. In the new API only the aliases table is used.

本段落中的剩余文本是该模块的原始文档。

This module provides a class Charset for representing character sets and character set conversions in email messages, as well as a character set registry and several convenience methods for manipulating this registry. Instances of Charset are used in several other modules within the email package.

Import this class from the email.charset module.

class email.charset.Charset(input_charset=DEFAULT_CHARSET)

Map character sets to their email properties.

This class provides information about the requirements imposed on email for a specific character set. It also provides convenience routines for converting between character sets, given the availability of the applicable codecs. Given a character set, it will do its best to provide information on how to use that character set in an email message in an RFC-compliant way.

Certain character sets must be encoded with quoted-printable or base64 when used in email headers or bodies. Certain character sets must be converted outright, and are not allowed in email.

Optional input_charset is as described below; it is always coerced to lower case. After being alias normalized it is also used as a lookup into the registry of character sets to find out the header encoding, body encoding, and output conversion codec to be used for the character set. For example, if input_charset is iso-8859-1, then headers and bodies will be encoded using quoted-printable and no output conversion codec is necessary. If input_charset is euc-jp, then headers will be encoded with base64, bodies will not be encoded, but output text will be converted from the euc-jp character set to the iso-2022-jp character set.

Charset instances have the following data attributes:

input_charset

The initial character set specified. Common aliases are converted to their official email names (e.g. latin_1 is converted to iso-8859-1). Defaults to 7-bit us-ascii.

header_encoding

If the character set must be encoded before it can be used in an email header, this attribute will be set to Charset.QP (for quoted-printable), Charset.BASE64 (for base64 encoding), or Charset.SHORTEST for the shortest of QP or BASE64 encoding. Otherwise, it will be None.

body_encoding

Same as header_encoding, but describes the encoding for the mail message’s body, which indeed may be different than the header encoding. Charset.SHORTEST is not allowed for body_encoding.

output_charset

Some character sets must be converted before they can be used in email headers or bodies. If the input_charset is one of them, this attribute will contain the name of the character set output will be converted to. Otherwise, it will be None.

input_codec

The name of the Python codec used to convert the input_charset to Unicode. If no conversion codec is necessary, this attribute will be None.

output_codec

The name of the Python codec used to convert Unicode to the output_charset. If no conversion codec is necessary, this attribute will have the same value as the input_codec.

Charset instances also have the following methods:

get_body_encoding()

Return the content transfer encoding used for body encoding.

This is either the string quoted-printable or base64 depending on the encoding used, or it is a function, in which case you should call the function with a single argument, the Message object being encoded. The function should then set the Content-Transfer-Encoding header itself to whatever is appropriate.

Returns the string quoted-printable if body_encoding is QP, returns the string base64 if body_encoding is BASE64, and returns the string 7bit otherwise.

get_output_charset()

Return the output character set.

This is the output_charset attribute if that is not None, otherwise it is input_charset.

header_encode(string)

Header-encode the string string.

The type of encoding (base64 or quoted-printable) will be based on the header_encoding attribute.

header_encode_lines(string, maxlengths)

Header-encode a string by converting it first to bytes.

This is similar to header_encode() except that the string is fit into maximum line lengths as given by the argument maxlengths, which must be an iterator: each element returned from this iterator will provide the next maximum line length.

body_encode(string)

Body-encode the string string.

The type of encoding (base64 or quoted-printable) will be based on the body_encoding attribute.

The Charset class also provides a number of methods to support standard operations and built-in functions.

__str__()

Returns input_charset as a string coerced to lower case. __repr__() is an alias for __str__().

__eq__(other)

This method allows you to compare two Charset instances for equality.

__ne__(other)

This method allows you to compare two Charset instances for inequality.

The email.charset module also provides the following functions for adding new entries to the global character set, alias, and codec registries:

email.charset.add_charset(charset, header_enc=None, body_enc=None, output_charset=None)

Add character properties to the global registry.

charset is the input character set, and must be the canonical name of a character set.

Optional header_enc and body_enc is either Charset.QP for quoted-printable, Charset.BASE64 for base64 encoding, Charset.SHORTEST for the shortest of quoted-printable or base64 encoding, or None for no encoding. SHORTEST is only valid for header_enc. The default is None for no encoding.

Optional output_charset is the character set that the output should be in. Conversions will proceed from input charset, to Unicode, to the output charset when the method Charset.convert() is called. The default is to output in the same character set as the input.

Both input_charset and output_charset must have Unicode codec entries in the module’s character set-to-codec mapping; use add_codec() to add codecs the module does not know about. See the codecs module’s documentation for more information.

The global character set registry is kept in the module global dictionary CHARSETS.

email.charset.add_alias(alias, canonical)

Add a character set alias. alias is the alias name, e.g. latin-1. canonical is the character set’s canonical name, e.g. iso-8859-1.

The global charset alias registry is kept in the module global dictionary ALIASES.

email.charset.add_codec(charset, codecname)

Add a codec that map characters in the given character set to and from Unicode.

charset is the canonical name of a character set. codecname is the name of a Python codec, as appropriate for the second argument to the str’s encode() method.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

19.1.13. email.encoders: 编码器

源代码: Lib/email/encoders.py [https://github.com/python/cpython/tree/3.6/Lib/email/encoders.py]

此模块是旧版 (Compat32) email API 的组成部分。 在新版 API 中将由 set_content() 方法的 cte 形参提供该功能。

本段落中的剩余文本是该模块的原始文档。

当创建全新的 Message 对象时，你经常需要对载荷编码以便通过兼容的邮件服务器进行传输。 对于包含二进制数据的 image/* 和 text/* 类型的消息来说尤其如此。

email 包在其 encoders 模块中提供了一些方便的编码。 这些编码器实际上由 MIMEAudio 和 MIMEImage 类构造函数使用，以提供默认编码。 所有编码器函数只接受一个参数，即要编码的消息对象。 它们通常提取有效数据，对其进行编码，并将有效数据重置为此新编码的值。 他们还应该根据需要设置 Content-Transfer-Encoding 标头。

请注意，这些函数对于多段消息没有意义。 它们必须应用到各个单独的段上面，而不是整体。如果直接传递一个多段类型的消息，会产生一个 TypeError 错误。

下面是提供的编码函数：

email.encoders.encode_quopri(msg)

将有效数据编码为quoted-printable形式，并将:mailheader:Content-Transfer-Encoding`标头设置为``quoted-printable` 1. 。当大多数实际的数据是普通的可打印数据但包含少量不可打印的字符时，这是一个很好的编码。

email.encoders.encode_base64(msg)

将有效载荷编码为 base64 形式，并将 Content-Transfer-Encoding 标头设为 base64。 当你的载荷主要包含不可打印数据时这是一种很好用的编码格式，因为它比 quoted-printable 更紧凑。 base64 编码格式的缺点是它会使文本变成人类不可读的形式。

email.encoders.encode_7or8bit(msg)

这并不实际改变消息的有效载荷，但它会基于载荷数据将 Content-Transfer-Encoding 标头相应地设为 7bit 或 8bit。

email.encoders.encode_noop(msg)

这样什么都不会做；它甚至不会设置 Content-Transfer-Encoding 标头。

备注

1

请注意使用 encode_quopri() 编码格式还会对数据中的所有制表符和空格符进行编码。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

19.1.14. email.utils: 其他工具

源代码: Lib/email/utils.py [https://github.com/python/cpython/tree/3.6/Lib/email/utils.py]

email.utils 模块提供如下几个工具

email.utils.localtime(dt=None)

以感知型 datetime 对象返回当地时间。 如果调用时参数为空，则返回当前时间。 否则 dt 参数应该是一个 datetime 实例，并根据系统时区数据库转换为当地时区。 如果 dt 是简单型的 (即 dt.tzinfo 是 None )，则假定为当地时间。 在这种情况下，为正值或零的 isdst 会使 localtime 假定夏季时间（例如，夏令时）对指定时间生效或不生效。 负值 isdst 会使 localtime 预测夏季时间对指定时间是否生效。

3.3 新版功能.

email.utils.make_msgid(idstring=None, domain=None)

返回一个适合作为兼容 RFC 2822 [https://tools.ietf.org/html/rfc2822.html] 的 Message-ID 标头的字符串。可选参数 idstring 可传入一字符串以增强该消息 ID 的唯一性。可选参数 domain 可用于提供消息 ID 中字符 ‘@’ 之后的部分，其默认值是本机的主机名。正常情况下无需覆盖此默认值，但在特定情况下覆盖默认值可能会有用，比如构建一个分布式系统，在多台主机上采用一致的域名。

在 3.2 版更改: 增加了关键字 domain

下列函数是旧（Compat32）电子邮件 API 的一部分。新 API 提供的解析和格式化在标头解析机制中已经被自动完成，故在使用新 API 时没有必要直接使用它们函数。

email.utils.quote(str)

返回一个新的字符串， str 中的反斜杠被替换为两个反斜杠，并且双引号被替换为反斜杠加双引号。

email.utils.unquote(str)

返回 str 被去除引用后的字符串。如果 str 开头和结尾均是双引号，则这对双引号被去除。类似地，如果 str 开头和结尾都是尖角括号，这对尖角括号会被去除。

email.utils.parseaddr(address)

将地址（应为诸如 To 或者 Cc 之类包含地址的字段值）解析为构成之的 真实名字 和 电子邮件地址 部分。返回包含这两个信息的一个元组；如若解析失败，则返回一个二元组 ('', '') 。

email.utils.formataddr(pair, charset='utf-8')

是 parseaddr() 的逆操作，接受一个 (真实名字, 电子邮件地址) 的二元组，并返回适合于 To or Cc 标头的字符串。如果第一个元素为假性值，则第二个元素将被原样返回。

可选地，如果指定 charset，则被视为一符合 RFC 2047 [https://tools.ietf.org/html/rfc2047.html] 的编码字符集，用于编码 真实名字 中的非 ASCII 字符。可以是一个 str 类的实例，或者一个 Charset 类。默认为 utf-8 。

在 3.3 版更改: 添加了 charset 选项。

email.utils.getaddresses(fieldvalues)

该方法返回一个形似 parseaddr() 返回的二元组的列表。 fieldvalues 是一个序列，包含了形似 Message.get_all 返回值的标头字段值。获取了一消息的所有收件人的简单示例如下：

from email.utils import getaddresses

tos = msg.get_all('to', [])
ccs = msg.get_all('cc', [])
resent_tos = msg.get_all('resent-to', [])
resent_ccs = msg.get_all('resent-cc', [])
all_recipients = getaddresses(tos + ccs + resent_tos + resent_ccs)

email.utils.parsedate(date)

尝试根据 RFC 2822 [https://tools.ietf.org/html/rfc2822.html] 的规则解析一个日期。然而，有些寄信人不严格遵守这一格式，所以这种情况下 parsedate() 会尝试猜测其形式。date 是一个字符串包含了一个形如 "Mon, 20 Nov 1995 19:12:08 -0500" 的 RFC 2822 [https://tools.ietf.org/html/rfc2822.html] 格式日期。如果日期解析成功， parsedate() 将返回一个九元组，可直接传递给 time.mktime()；否则返回 None。注意返回的元组中下标为 6、7、8 的部分是无用的。

email.utils.parsedate_tz(date)

Performs the same function as parsedate(), but returns either None or a 10-tuple; the first 9 elements make up a tuple that can be passed directly to time.mktime(), and the tenth is the offset of the date’s timezone from UTC (which is the official term for Greenwich Mean Time) 1. If the input string has no timezone, the last element of the tuple returned is None. Note that indexes 6, 7, and 8 of the result tuple are not usable.

email.utils.parsedate_to_datetime(date)

format_datetime() 的逆操作。 执行与 parsedate() 相同的功能，但会在成功时返回一个 datetime。 如果输入日期的时区值为 -0000，则 datetime 将为一个简单型 datetime，而如果日期符合 RFC 标准则它将代表一个 UTC 时间，但是并不指明日期所在消息的实际源时区。 如果输入日期具有任何其他有效的时区差值，则 datetime 将为一个感知型 datetime 并与 timezone tzinfo 相对应。

3.3 新版功能.

email.utils.mktime_tz(tuple)

将 parsedate_tz() 所返回的 10 元组转换为一个 UTC 时间戳（相距 Epoch 纪元初始的秒数）。 如果元组中的时区项为 None，则视为当地时间。

email.utils.formatdate(timeval=None, localtime=False, usegmt=False)

返回 RFC 2822 [https://tools.ietf.org/html/rfc2822.html] 标准的日期字符串，例如:

Fri, 09 Nov 2001 01:08:47 -0000

可选的 timeval 如果给出，则是一个可被 time.gmtime() 和 time.localtime() 接受的浮点数时间值，否则会使得当前时间。

可选的 localtime 是一个旗标，当为 True 时，将会解析 timeval，并返回一个相对于当地时区而非 UTC 的日期值，并会适当地考虑夏令时。 默认值 False 表示使用 UTC。

可选的 usegmt 是一个旗标，当为 True 时，将会输出一个日期字符串，其中时区表示为 ascii 字符串 GMT 而非数字形式的 -0000。 这对某些协议（例如 HTTP）来说是必要的。 这仅在 localtime 为 False 时应用。 默认值为 False。

email.utils.format_datetime(dt, usegmt=False)

类似于 formatdate，但输入的是一个 datetime 实例。 如果实例是一个简单型 datetime，它会被视为 “不带源时区信息的 UTC”，并且使用传统的 -0000 作为时区。 如果实例是一个感知型 datetime，则会使用数字形式的时区时差。 如果实例是感知型且时区时差为零，则 usegmt 可能会被设为 True，在这种情况下将使用字符串 GMT 而非数字形式的时区时差。 这提供了一种生成符合标准 HTTP 日期标头的方式。

3.3 新版功能.

email.utils.decode_rfc2231(s)

根据 RFC 2231 [https://tools.ietf.org/html/rfc2231.html] 解码字符串 s。

email.utils.encode_rfc2231(s, charset=None, language=None)

根据 RFC 2231 [https://tools.ietf.org/html/rfc2231.html] 对字符串 s 进行编码。 可选的 charset 和 language 如果给出，则为指明要使用的字符集名称和语言名称。 如果两者均未给出，则会原样返回 s。 如果给出 charset 但未给出 language，则会使用空字符串作为 language 值来对字符串进行编码。

email.utils.collapse_rfc2231_value(value, errors='replace', fallback_charset='us-ascii')

当以 RFC 2231 [https://tools.ietf.org/html/rfc2231.html] 格式来编码标头形参时，Message.get_param 可能返回一个包含字符集、语言和值的 3 元组。 collapse_rfc2231_value() 会将此返回为一个 unicode 字符串。 可选的 errors 会被传递给 str 的 encode() 方法的 errors 参数；它的默认值为 'replace'。 可选的 fallback_charset 指定当 RFC 2231 [https://tools.ietf.org/html/rfc2231.html] 标头中的字符集无法被 Python 识别时要使用的字符集；它的默认值为 'us-ascii'。

为方便起见，如果传给 collapse_rfc2231_value() 的 value 不是一个元组，则应为一个字符串并会将其原样返回。

email.utils.decode_params(params)

根据 RFC 2231 [https://tools.ietf.org/html/rfc2231.html] 解码参数列表。 params 是一个包含 (content-type, string-value) 形式的元素的 2 元组的序列。

备注

1

请注意时区时差的符号与同一时区的 time.timezone 变量的符号相反；后者遵循 POSIX 标准而此模块遵循 RFC 2822 [https://tools.ietf.org/html/rfc2822.html]。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

19.1.15. email.iterators: 迭代器

源代码: Lib/email/iterators.py [https://github.com/python/cpython/tree/3.6/Lib/email/iterators.py]

通过 Message.walk 方法来迭代消息对象树是相当容易的。 email.iterators 模块提供了一些适用于消息对象树的高层级迭代器。

email.iterators.body_line_iterator(msg, decode=False)

此对象会迭代 msg 的所有子部分中的所有载荷，逐行返回字符串载荷。 它会跳过所有子部分的标头，并且它也会跳过任何包含不为 Python 字符串的载荷的子部分。 这基本上等价于使用 readline() 从一个文件读取消息的纯文本表示形式，并跳过所有中间的标头。

可选的 decode 会被传递给 Message.get_payload。

email.iterators.typed_subpart_iterator(msg, maintype='text', subtype=None)

此函数会迭代 msg 的所有子部分，只返回其中与 maintype 和 subtype 所指定的 MIME 类型相匹配的子部分。

请注意 subtype 是可选项；如果省略，则仅使用主类型来进行子部分 MIME 类型的匹配。 maintype 也是可选项；它的默认值为 text。

因此，在默认情况下 typed_subpart_iterator() 会返回每一个 MIME 类型为 text/* 的子部分。

增加了以下函数作为有用的调试工具。 它 不应当 被视为该包所支持的公共接口的组成部分。

email.iterators._structure(msg, fp=None, level=0, include_default=False)

打印消息对象结构的内容类型的缩进表示形式。 例如:

>>> msg = email.message_from_file(somefile)
>>> _structure(msg)
multipart/mixed
    text/plain
    text/plain
    multipart/digest
        message/rfc822
            text/plain
        message/rfc822
            text/plain
        message/rfc822
            text/plain
        message/rfc822
            text/plain
        message/rfc822
            text/plain
    text/plain

可选项 fp 是一个作为打印输出目标的文件类对象。 它必须适用于 Python 的 print() 函数。 level 是供内部使用的。 include_default 如果为真值，则会同时打印默认类型。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

19.1. email — 电子邮件与 MIME 处理包 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

19.2. json — JSON 编码和解码器

源代码: Lib/json/__init__.py [https://github.com/python/cpython/tree/3.6/Lib/json/__init__.py]

JSON (JavaScript Object Notation) [http://json.org]，由 RFC 7159 [https://tools.ietf.org/html/rfc7159.html] (which obsoletes RFC 4627 [https://tools.ietf.org/html/rfc4627.html]) 和 ECMA-404 [http://www.ecma-international.org/publications/standards/Ecma-404.htm] 指定，是一个受 JavaScript [https://en.wikipedia.org/wiki/JavaScript] 的对象字面量语法启发的轻量级数据交换格式，尽管它不仅仅是一个严格意义上的 JavaScript 的字集 1。

json 提供了与标准库 marshal 和 pickle 相似的API接口。

对基本的 Python 对象层次结构进行编码：

>>> import json
>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
'["foo", {"bar": ["baz", null, 1.0, 2]}]'
>>> print(json.dumps("\"foo\bar"))
"\"foo\bar"
>>> print(json.dumps('\u1234'))
"\u1234"
>>> print(json.dumps('\\'))
"\\"
>>> print(json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True))
{"a": 0, "b": 0, "c": 0}
>>> from io import StringIO
>>> io = StringIO()
>>> json.dump(['streaming API'], io)
>>> io.getvalue()
'["streaming API"]'

紧凑编码：

>>> import json
>>> json.dumps([1, 2, 3, {'4': 5, '6': 7}], separators=(',', ':'))
'[1,2,3,{"4":5,"6":7}]'

美化输出：

>>> import json
>>> print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
{
    "4": 5,
    "6": 7
}

JSON解码：

>>> import json
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
['foo', {'bar': ['baz', None, 1.0, 2]}]
>>> json.loads('"\\"foo\\bar"')
'"foo\x08ar'
>>> from io import StringIO
>>> io = StringIO('["streaming API"]')
>>> json.load(io)
['streaming API']

特殊 JSON 对象解码：

>>> import json
>>> def as_complex(dct):
...     if '__complex__' in dct:
...         return complex(dct['real'], dct['imag'])
...     return dct
...
>>> json.loads('{"__complex__": true, "real": 1, "imag": 2}',
...     object_hook=as_complex)
(1+2j)
>>> import decimal
>>> json.loads('1.1', parse_float=decimal.Decimal)
Decimal('1.1')

扩展 JSONEncoder：

>>> import json
>>> class ComplexEncoder(json.JSONEncoder):
...     def default(self, obj):
...         if isinstance(obj, complex):
...             return [obj.real, obj.imag]
...         # Let the base class default method raise the TypeError
...         return json.JSONEncoder.default(self, obj)
...
>>> json.dumps(2 + 1j, cls=ComplexEncoder)
'[2.0, 1.0]'
>>> ComplexEncoder().encode(2 + 1j)
'[2.0, 1.0]'
>>> list(ComplexEncoder().iterencode(2 + 1j))
['[2.0', ', 1.0', ']']

从命令行使用 json.tool 来验证并美化输出：

$ echo '{"json":"obj"}' | python -m json.tool
{
    "json": "obj"
}
$ echo '{1.2:3.4}' | python -m json.tool
Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

详细文档请参见 命令行界面。

注解

JSON 是 YAML [http://yaml.org/] 1.2 的一个子集。由该模块的默认设置生成的 JSON （尤其是默认的 “分隔符” 设置值）也是 YAML 1.0 and 1.1 的一个子集。因此该模块也能够用于序列化为 YAML。

19.2.1. 基本使用

json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)

使用这个 转换表 将 obj 序列化为 JSON 格式化流形式的 fp (支持 .write() 的 file-like object)。

如果 skipkeys 是 true （默认为 False），那么那些不是基本对象（包括 str, int、float、bool、None）的字典的键会被跳过；否则引发一个 TypeError。

json 模块始终产生 str 对象而非 bytes 对象。因此，fp.write() 必须支持 str 输入。

如果 ensure_ascii 是 true （即默认值），输出保证将所有输入的非 ASCII 字符转义。如果 ensure_ascii 是 false，这些字符会原样输出。

如果 check_circular 是为假值 (默认为 True)，那么容器类型的循环引用检验会被跳过并且循环引用会引发一个 OverflowError (或者更糟的情况)。

如果 allow_nan 是 false（默认为 True），那么在对严格 JSON 规格范围外的 float 类型值（nan、inf 和 -inf）进行序列化时会引发一个 ValueError。如果 allow_nan 是 true，则使用它们的 JavaScript 等价形式（NaN、Infinity 和 -Infinity）。

如果 indent 是一个非负整数或者字符串，那么 JSON 数组元素和对象成员会被美化输出为该值指定的缩进等级。 如果缩进等级为零、负数或者 ""，则只会添加换行符。 None (默认值) 选择最紧凑的表达。 使用一个正整数会让每一层缩进同样数量的空格。 如果 indent 是一个字符串 (比如 "\t")，那个字符串会被用于缩进每一层。

在 3.2 版更改: 现允许使用字符串作为 indent 而不再仅仅是整数。

当被指定时，separators 应当是一个 (item_separator, key_separator) 元组。当 indent 为 None 时，默认值取 (', ', ': ')，否则取 (',', ': ')。为了得到最紧凑的 JSON 表达式，你应该指定其为 (',', ':') 以消除空白字符。

在 3.4 版更改: 现当 indent 不是 None 时，采用 (',', ': ') 作为默认值。

当 default 被指定时，其应该是一个函数，每当某个对象无法被序列化时它会被调用。它应该返回该对象的一个可以被 JSON 编码的版本或者引发一个 TypeError。如果没有被指定，则会直接引发 TypeError。

如果 sort_keys 是 true（默认为 False），那么字典的输出会以键的顺序排序。

为了使用一个自定义的 JSONEncoder 子类（比如：覆盖了 default() 方法来序列化额外的类型）， 通过 cls 关键字参数来指定；否则将使用 JSONEncoder。

在 3.6 版更改: 所有可选形参现在都是 仅限关键字参数。

注解

与 pickle 和 marshal 不同，JSON 不是一个具有框架的协议，所以尝试多次使用同一个 fp 调用 dump() 来序列化多个对象会产生一个不合规的 JSON 文件。

json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)

使用这个 转换表 将 obj 序列化为 JSON 格式的 str。 其参数的含义与 dump() 中的相同。

注解

JSON 中的键-值对中的键永远是 str 类型的。当一个对象被转化为 JSON 时，字典中所有的键都会被强制转换为字符串。这所造成的结果是字典被转换为 JSON 然后转换回字典时可能和原来的不相等。换句话说，如果 x 具有非字符串的键，则有 loads(dumps(x)) != x。

json.load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

使用这个 转换表 将 fp (一个支持 .read() 并包含一个 JSON 文档的 text file 或者 binary file) 反序列化为一个 Python 对象。

object_hook 是一个可选的函数，它会被调用于每一个解码出的对象字面量（即一个 dict）。object_hook 的返回值会取代原本的 dict。这一特性能够被用于实现自定义解码器（如 JSON-RPC [http://www.jsonrpc.org] 的类型提示)。

object_pairs_hook is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the dict. This feature can be used to implement custom decoders that rely on the order that the key and value pairs are decoded (for example, collections.OrderedDict() will remember the order of insertion). If object_hook is also defined, the object_pairs_hook takes priority.

在 3.1 版更改: 添加了对 object_pairs_hook 的支持。

parse_float ，如果指定，将与每个要解码 JSON 浮点数一同调用。默认状态下，相当于 float(num_str) 。可以用于对 JSON 浮点数使用其它数据类型和解析器 （比如 decimal.Decimal ）。

parse_int ，如果指定，将与每个要解码 JSON 整数的字符串一同调用。默认状态下，相当于 int(num_str) 。可以用于对 JSON 整数使用其它数据类型和语法分析程序 （比如 float ）。

parse_constant ，如果指定，将要与以下字符串中的一个一同调用： '-Infinity' ， 'Infinity' ， 'NaN' 。如果遇到无效的 JSON 数字它会被用于抛出异常。

在 3.1 版更改: parse_constant 不再调用 ‘null’ ， ‘true’ ， ‘false’ 。

要使用自定义的 JSONDecoder 子类，用 cls 指定他；否则使用 JSONDecoder 。额外的关键词参数会通过类的构造函数传递。

如果反序列化的数据不是有效 JSON 文档，抛出 JSONDecodeError 错误。

在 3.6 版更改: 所有可选形参现在都是 仅限关键字参数。

在 3.6 版更改: fp 现在可以是 binary file 。输入编码应当是 UTF-8 ， UTF-16 或者 UTF-32 。

json.loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

使用这个 转换表 将 s (一个包含 JSON 文档的 str, bytes 或 bytearray 实例) 反序列化为 Python 对象。

其他参数与在 load() 中的含义相同，只有 encoding 被忽略和弃用。

如果反序列化的数据不是有效 JSON 文档，抛出 JSONDecodeError 错误。

在 3.6 版更改: s 现在可以为 bytes 或 bytearray 类型。 输入编码应为 UTF-8, UTF-16 或 UTF-32。

19.2.2. 编码器和解码器

class json.JSONDecoder(*, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=None)

简单的JSON解码器。

默认情况下，解码执行以下翻译:

JSON

Python

object

dict

array

list

string

str

number (int)

int

number (real)

float

true

True

false

False

null

None

它还将“NaN”、“Infinity”和“-Infinity”理解为它们对应的“float”值，这超出了JSON规范。

object_hook ，如果指定，会被每个解码的 JSON 对象的结果调用，并且返回值会替代给定 dict 。它可被用于提供自定义反序列化（比如去支持 JSON-RPC 类的暗示）。

object_pairs_hook, if specified will be called with the result of every JSON object decoded with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the dict. This feature can be used to implement custom decoders that rely on the order that the key and value pairs are decoded (for example, collections.OrderedDict() will remember the order of insertion). If object_hook is also defined, the object_pairs_hook takes priority.

在 3.1 版更改: 添加了对 object_pairs_hook 的支持。

parse_float ，如果指定，将与每个要解码 JSON 浮点数一同调用。默认状态下，相当于 float(num_str) 。可以用于对 JSON 浮点数使用其它数据类型和解析器 （比如 decimal.Decimal ）。

parse_int ，如果指定，将与每个要解码 JSON 整数的字符串一同调用。默认状态下，相当于 int(num_str) 。可以用于对 JSON 整数使用其它数据类型和语法分析程序 （比如 float ）。

parse_constant ，如果指定，将要与以下字符串中的一个一同调用： '-Infinity' ， 'Infinity' ， 'NaN' 。如果遇到无效的 JSON 数字它会被用于抛出异常。

如果 strict 为 false （默认为 True ），那么控制字符将被允许在字符串内。在此上下文中的控制字符是那些编码于范围 0 – 31 的字符，包括 '\t' (制表符）， '\n' 和 '\0'。

如果反序列化的数据不是有效 JSON 文档，抛出 JSONDecodeError 错误。

在 3.6 版更改: 所有形参现在都是 仅限关键字参数。

decode(s)

返回 s 的 Python 表示形式（包含一个 JSON 文档的 str 实例）。

如果给定的 JSON 文档无效则将引发 JSONDecodeError。

raw_decode(s)

从 s 中解码出 JSON 文档（以 JSON 文档开头的一个 str 对象）并返回一个 Python 表示形式为 2 元组以及指明该文档在 s 中结束位置的序号。

这可以用于从一个字符串解码JSON文档，该字符串的末尾可能有无关的数据。

class json.JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

用于Python数据结构的可扩展JSON编码器。

默认支持以下对象和类型：

Python

JSON

dict

object

list, tuple

array

str

string

int, float, int 和 float 派生的枚举

number

True

true

False

false

None

null

在 3.4 版更改: 添加了对 int 和 float 派生的枚举类的支持

为了将其拓展至识别其他对象，需要子类化并实现 default() 方法于另一种返回 o 的可序列化对象的方法如果可行，否则它应该调用超类实现（来引发 TypeError ）。

如果 skipkeys 为假值（默认），则尝试对不是 str, int, float 或 None 的键进行编码将会引发 TypeError。 如果 skipkeys 为真值，这些条目将被直接跳过。

如果 ensure_ascii 是 true （即默认值），输出保证将所有输入的非 ASCII 字符转义。如果 ensure_ascii 是 false，这些字符会原样输出。

如果 check_circular 为 true （默认），那么列表，字典，和自定义编码的对象在编码期间会被检查重复循环引用防止无限递归（无限递归将导致 OverflowError ）。否则，这样进行检查。

如果 allow_nan 为 true （默认），那么 NaN ， Infinity ，和 -Infinity 进行编码。此行为不符合 JSON 规范，但与大多数的基于 Javascript 的编码器和解码器一致。否则，它将是一个 ValueError 来编码这些浮点数。

如果 sort_keys 为 true （默认为： False ），那么字典的输出是按照键排序；这对回归测试很有用，以确保可以每天比较 JSON 序列化。

如果 indent 是一个非负整数或者字符串，那么 JSON 数组元素和对象成员会被美化输出为该值指定的缩进等级。 如果缩进等级为零、负数或者 ""，则只会添加换行符。 None (默认值) 选择最紧凑的表达。 使用一个正整数会让每一层缩进同样数量的空格。 如果 indent 是一个字符串 (比如 "\t")，那个字符串会被用于缩进每一层。

在 3.2 版更改: 现允许使用字符串作为 indent 而不再仅仅是整数。

当被指定时，separators 应当是一个 (item_separator, key_separator) 元组。当 indent 为 None 时，默认值取 (', ', ': ')，否则取 (',', ': ')。为了得到最紧凑的 JSON 表达式，你应该指定其为 (',', ':') 以消除空白字符。

在 3.4 版更改: 现当 indent 不是 None 时，采用 (',', ': ') 作为默认值。

当 default 被指定时，其应该是一个函数，每当某个对象无法被序列化时它会被调用。它应该返回该对象的一个可以被 JSON 编码的版本或者引发一个 TypeError。如果没有被指定，则会直接引发 TypeError。

在 3.6 版更改: 所有形参现在都是 仅限关键字参数。

default(o)

在子类中实现这种方法使其返回 o 的可序列化对象，或者调用基础实现（引发 TypeError ）

比如说，为了支持任意迭代器，你可以像这样实现默认设置:

def default(self, o):
   try:
       iterable = iter(o)
   except TypeError:
       pass
   else:
       return list(iterable)
   # Let the base class default method raise the TypeError
   return json.JSONEncoder.default(self, o)

encode(o)

返回 Python o 数据结构的 JSON 字符串表达方式。比如说:

>>> json.JSONEncoder().encode({"foo": ["bar", "baz"]})
'{"foo": ["bar", "baz"]}'

iterencode(o)

编码给定对象 o ，并且让每个可用的字符串表达方式。例如:

for chunk in json.JSONEncoder().iterencode(bigobject):
    mysocket.write(chunk)

19.2.3. 异常

exception json.JSONDecodeError(msg, doc, pos)

拥有以下额外属性的 ValueError 的子类：

msg

未格式化的错误消息。

doc

正在解析的 JSON 文档。

pos

The start index of doc where parsing failed.

lineno

The line corresponding to pos.

colno

The column corresponding to pos.

3.5 新版功能.

19.2.4. 标准符合性和互操作性

JSON 格式由 RFC 7159 [https://tools.ietf.org/html/rfc7159.html] 和 ECMA-404 [http://www.ecma-international.org/publications/standards/Ecma-404.htm] 指定。此段落详细讲了这个模块符合 RFC 的级别。简单来说， JSONEncoder 和 JSONDecoder 子类，和明确提到的参数以外的参数，不作考虑。

此模块不严格遵循于 RFC ，它实现了一些扩展是有效的 Javascript 但不是有效的 JSON。尤其是：

无限和 NaN 数值是可接受的并输出；

对象内的重复名称是接受的，并且仅使用最后一对名称-值对。

自从 RFC 允许符合 RFC 的语法分析程序接收 不符合 RFC 的输入文本以来，这个模块的解串器在默认状态下默认符合 RFC 。

19.2.4.1. 字符编码

RFC 要求使用 UTF-8 ， UTF-16 ，或 UTF-32 之一来表示 JSON ，为了最大互通性推荐使用 UTF-8 。

RFC允许，尽管不是必须的，这个模块的序列化默认设置为 ensure_ascii=True ，这样消除输出以便结果字符串至容纳 ASCII 字符。

ensure_ascii 参数以外，此模块是严格的按照在 Python 对象和 Unicode strings 间的转换定义的，并且因此不能直接解决字符编码的问题。

RFC 禁止添加字符顺序标记（ BOM ）在 JSON 文本的开头，这个模块的序列化器不添加 BOM 标记在它的输出上。 RFC，准许 JSON 反序列化器忽略它们输入中的初始 BOM 标记，但不要求。此模块的反序列化器引发 ValueError 当出现初始 BOM 标记。

RFC 不会明确禁止包含字节序列的 JSON 字符串这不对应有效的 Unicode 字符（比如 不成对的 UTF-16 的替代物），但是它确实指出它们可能会导致互操作性问题。默认下，模块对这样的序列接受和输出（当在原始 str 存在时）代码点。

19.2.4.2. Infinite 和 NaN 数值。

RFC 不允许 infinite 或者 NaN 数值的表达方式。尽管这样，默认情况下，此模块接受并且输出 Infinity ， -Infinity，和 NaN 好像它们是有效的JSON数字字面值

>>> # Neither of these calls raises an exception, but the results are not valid JSON
>>> json.dumps(float('-inf'))
'-Infinity'
>>> json.dumps(float('nan'))
'NaN'
>>> # Same when deserializing
>>> json.loads('-Infinity')
-inf
>>> json.loads('NaN')
nan

序列化器中， allow_nan 参数可用于替代这个行为。反序列化器中， parse_constant 参数，可用于替代这个行为。

19.2.4.3. 对象中的重复名称

RFC 具体说明了 在 JSON对象里的名字应该是唯一的，但没有规定如何处理JSON对象中的重复名称。默认下，此模块不引发异常；作为替代，对于给定名它将忽略除姓-值对之外的所有对:

>>> weird_json = '{"x": 1, "x": 2, "x": 3}'
>>> json.loads(weird_json)
{'x': 3}

The object_pairs_hook parameter can be used to alter this behavior.

19.2.4.4. 顶级非对象，非数组值

过时的 RFC 4627 [https://tools.ietf.org/html/rfc4627.html] 指定的旧版本 JSON 要求 JSON 文本顶级值必须是 JSON 对象或数组（ Python dict 或 list ），并且不能是 JSON null 值，布尔值，数值或者字符串值。 RFC 7159 [https://tools.ietf.org/html/rfc7159.html] 移除这个限制，此模块没有并且从未在序列化器和反序列化器中实现这个限制。

无论如何，为了最大化地获取互操作性，你可能希望自己遵守该原则。

19.2.4.5. 实现限制

一些 JSON 反序列化器的实现应该在以下方面做出限制：

接受的 JSON 文本大小

嵌套 JSON 对象和数组的最高水平

JSON 数字的范围和精度

JSON 字符串的内容和最大长度

此模块不强制执行任何上述限制，除了相关的 Python 数据类型本身或者 Python 解释器本身的限制以外。

当序列化为 JSON ，在应用中当心此类限制这可能破坏你的 JSON 。特别是，通常将 JSON 数字反序列化为 IEEE 754 双精度数字，从而受到该表示形式的范围和精度限制。这是特别相关的，当序列化非常大的 Python int 值时，或者当序列化 “exotic” 数值类型的实例时比如 decimal.Decimal 。

19.2.5. 命令行界面

** 源代码： ** Lib/json/tool.py [https://github.com/python/cpython/tree/3.6/Lib/json/tool.py]

The json.tool module provides a simple command line interface to validate and pretty-print JSON objects.

如果未指定可选的 infile 和 outfile 参数，则将分别使用 sys.stdin 和 sys.stdout:

$ echo '{"json": "obj"}' | python -m json.tool
{
    "json": "obj"
}
$ echo '{1.2:3.4}' | python -m json.tool
Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

在 3.5 版更改: 输出现在将与输入顺序保持一致。 请使用 --sort-keys 选项来将输出按照键的字母顺序排序。

19.2.5.1. 命令行选项

infile

要被验证或美化打印的 JSON 文件：

$ python -m json.tool mp_films.json
[
    {
        "title": "And Now for Something Completely Different",
        "year": 1971
    },
    {
        "title": "Monty Python and the Holy Grail",
        "year": 1975
    }
]

如果 infile 未指定，则从 sys.stdin 读取。

outfile

将 infile 输出写入到给定的 outfile。 在其他情况下写入到 sys.stdout。

--sort-keys

将字典输出按照键的字母顺序排序。

3.5 新版功能.

-h, --help

显示帮助消息。

备注

1

正如 RFC 7159 的勘误表 [https://www.rfc-editor.org/errata_search.php?rfc=7159] 所说明的，JSON 允许以字符串表示字面值字符 U+2028 (LINE SEPARATOR) 和 U+2029 (PARAGRAPH SEPARATOR)，而 JavaScript (在 ECMAScript 5.1 版中) 不允许。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

19.3. mailcap — Mailcap 文件处理

源代码: Lib/mailcap.py [https://github.com/python/cpython/tree/3.6/Lib/mailcap.py]

Mailcap 文件可用来配置支持 MIME 的应用例如邮件阅读器和 Web 浏览器如何响应具有不同 MIME 类型的文件。 （”mailcap” 这个名称源自短语”mail capability”。） 例如，一个 mailcap 文件可能包含 video/mpeg; xmpeg %s 这样的行。 然后，如果用户遇到 MIME 类型为 video/mpeg 的邮件消息或 Web 文档时，%s 将被替换为一个文件名（通常是一个临时文件）并且将自动启动 xmpeg 程序来查看该文件。

The mailcap format is documented in RFC 1524 [https://tools.ietf.org/html/rfc1524.html], “A User Agent Configuration Mechanism For Multimedia Mail Format Information,” but is not an Internet standard. However, mailcap files are supported on most Unix systems.

mailcap.findmatch(caps, MIMEtype, key='view', filename='/dev/null', plist=[])

返回一个 2 元组；其中第一个元素是包含所要执行命令的字符串 (它可被传递给 os.system())，第二个元素是对应于给定 MIME 类型的 mailcap 条目。 如果找不到匹配的 MIME 类型，则将返回 (None, None)。

key 是所需字段的名称，它代表要执行的活动类型；默认值是 ‘view’，因为在最通常的情况下你只是想要查看 MIME 类型数据的正文。 其他可能的值还有 ‘compose’ 和 ‘edit’，分别用于想要创建给定 MIME 类型正文或修改现有正文数据的情况。 请参阅 RFC 1524 [https://tools.ietf.org/html/rfc1524.html] 获取这些字段的完整列表。

filename 是在命令行中用来替换 %s 的文件名；默认值 '/dev/null' 几乎肯定不是你想要的，因此通常你要通过指定一个文件名来重载它。

plist 可以是一个包含命名形参的列表；默认值只是一个空列表。 列表中的每个条目必须为包含形参名称的字符串、等于号 ('=') 以及形参的值。 Mailcap 条目可以包含形如 %{foo} 的命名形参，它将由名为 ‘foo’ 的形参的值所替换。 例如，如果命令行 showpartial %{id} %{number} %{total} 是在一个 mailcap 文件中，并且 plist 被设为 ['id=1', 'number=2', 'total=3']，则结果命令行将为 'showpartial 1 2 3'。

在 mailcap 文件中，可以指定可选的 “test” 字段来检测某些外部条件（例如所使用的机器架构或窗口系统）来确定是否要应用 mailcap 行。 findmatch() 将自动检查此类条件并在检查未通过时跳过条目。

mailcap.getcaps()

返回一个将 MIME 类型映射到 mailcap 文件条目列表的字典。 此字典必须被传给 findmatch() 函数。 条目会被存储为字典列表，但并不需要了解此表示形式的细节。

此信息来自在系统中找到的所有 mailcap 文件。 用户的 mailcap 文件 $HOME/.mailcap 中的设置将覆盖系统 mailcap 文件 /etc/mailcap, /usr/etc/mailcap 和 /usr/local/etc/mailcap 中的设置。

一个用法示例:

>>> import mailcap
>>> d = mailcap.getcaps()
>>> mailcap.findmatch(d, 'video/mpeg', filename='tmp1223')
('xmpeg tmp1223', {'view': 'xmpeg %s'})

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

19.4. mailbox — Manipulate mailboxes in various formats

源代码： Lib/mailbox.py [https://github.com/python/cpython/tree/3.6/Lib/mailbox.py]

This module defines two classes, Mailbox and Message, for accessing and manipulating on-disk mailboxes and the messages they contain. Mailbox offers a dictionary-like mapping from keys to messages. Message extends the email.message module’s Message class with format-specific state and behavior. Supported mailbox formats are Maildir, mbox, MH, Babyl, and MMDF.

参见

模块 email

Represent and manipulate messages.

19.4.1. Mailbox 对象

class mailbox.Mailbox

A mailbox, which may be inspected and modified.

The Mailbox class defines an interface and is not intended to be instantiated. Instead, format-specific subclasses should inherit from Mailbox and your code should instantiate a particular subclass.

The Mailbox interface is dictionary-like, with small keys corresponding to messages. Keys are issued by the Mailbox instance with which they will be used and are only meaningful to that Mailbox instance. A key continues to identify a message even if the corresponding message is modified, such as by replacing it with another message.

Messages may be added to a Mailbox instance using the set-like method add() and removed using a del statement or the set-like methods remove() and discard().

Mailbox interface semantics differ from dictionary semantics in some noteworthy ways. Each time a message is requested, a new representation (typically a Message instance) is generated based upon the current state of the mailbox. Similarly, when a message is added to a Mailbox instance, the provided message representation’s contents are copied. In neither case is a reference to the message representation kept by the Mailbox instance.

The default Mailbox iterator iterates over message representations, not keys as the default dictionary iterator does. Moreover, modification of a mailbox during iteration is safe and well-defined. Messages added to the mailbox after an iterator is created will not be seen by the iterator. Messages removed from the mailbox before the iterator yields them will be silently skipped, though using a key from an iterator may result in a KeyError exception if the corresponding message is subsequently removed.

警告

Be very cautious when modifying mailboxes that might be simultaneously changed by some other process. The safest mailbox format to use for such tasks is Maildir; try to avoid using single-file formats such as mbox for concurrent writing. If you’re modifying a mailbox, you must lock it by calling the lock() and unlock() methods before reading any messages in the file or making any changes by adding or deleting a message. Failing to lock the mailbox runs the risk of losing messages or corrupting the entire mailbox.

Mailbox instances have the following methods:

add(message)

Add message to the mailbox and return the key that has been assigned to it.

Parameter message may be a Message instance, an email.message.Message instance, a string, a byte string, or a file-like object (which should be open in binary mode). If message is an instance of the appropriate format-specific Message subclass (e.g., if it’s an mboxMessage instance and this is an mbox instance), its format-specific information is used. Otherwise, reasonable defaults for format-specific information are used.

在 3.2 版更改: Support for binary input was added.

remove(key)

__delitem__(key)

discard(key)

Delete the message corresponding to key from the mailbox.

If no such message exists, a KeyError exception is raised if the method was called as remove() or __delitem__() but no exception is raised if the method was called as discard(). The behavior of discard() may be preferred if the underlying mailbox format supports concurrent modification by other processes.

__setitem__(key, message)

Replace the message corresponding to key with message. Raise a KeyError exception if no message already corresponds to key.

As with add(), parameter message may be a Message instance, an email.message.Message instance, a string, a byte string, or a file-like object (which should be open in binary mode). If message is an instance of the appropriate format-specific Message subclass (e.g., if it’s an mboxMessage instance and this is an mbox instance), its format-specific information is used. Otherwise, the format-specific information of the message that currently corresponds to key is left unchanged.

iterkeys()

keys()

Return an iterator over all keys if called as iterkeys() or return a list of keys if called as keys().

itervalues()

__iter__()

values()

Return an iterator over representations of all messages if called as itervalues() or __iter__() or return a list of such representations if called as values(). The messages are represented as instances of the appropriate format-specific Message subclass unless a custom message factory was specified when the Mailbox instance was initialized.

注解

The behavior of __iter__() is unlike that of dictionaries, which iterate over keys.

iteritems()

items()

Return an iterator over (key, message) pairs, where key is a key and message is a message representation, if called as iteritems() or return a list of such pairs if called as items(). The messages are represented as instances of the appropriate format-specific Message subclass unless a custom message factory was specified when the Mailbox instance was initialized.

get(key, default=None)

__getitem__(key)

Return a representation of the message corresponding to key. If no such message exists, default is returned if the method was called as get() and a KeyError exception is raised if the method was called as __getitem__(). The message is represented as an instance of the appropriate format-specific Message subclass unless a custom message factory was specified when the Mailbox instance was initialized.

get_message(key)

Return a representation of the message corresponding to key as an instance of the appropriate format-specific Message subclass, or raise a KeyError exception if no such message exists.

get_bytes(key)

Return a byte representation of the message corresponding to key, or raise a KeyError exception if no such message exists.

3.2 新版功能.

get_string(key)

Return a string representation of the message corresponding to key, or raise a KeyError exception if no such message exists. The message is processed through email.message.Message to convert it to a 7bit clean representation.

get_file(key)

Return a file-like representation of the message corresponding to key, or raise a KeyError exception if no such message exists. The file-like object behaves as if open in binary mode. This file should be closed once it is no longer needed.

在 3.2 版更改: The file object really is a binary file; previously it was incorrectly returned in text mode. Also, the file-like object now supports the context management protocol: you can use a with statement to automatically close it.

注解

Unlike other representations of messages, file-like representations are not necessarily independent of the Mailbox instance that created them or of the underlying mailbox. More specific documentation is provided by each subclass.

__contains__(key)

Return True if key corresponds to a message, False otherwise.

__len__()

Return a count of messages in the mailbox.

clear()

Delete all messages from the mailbox.

pop(key, default=None)

Return a representation of the message corresponding to key and delete the message. If no such message exists, return default. The message is represented as an instance of the appropriate format-specific Message subclass unless a custom message factory was specified when the Mailbox instance was initialized.

popitem()

Return an arbitrary (key, message) pair, where key is a key and message is a message representation, and delete the corresponding message. If the mailbox is empty, raise a KeyError exception. The message is represented as an instance of the appropriate format-specific Message subclass unless a custom message factory was specified when the Mailbox instance was initialized.

update(arg)

Parameter arg should be a key-to-message mapping or an iterable of (key, message) pairs. Updates the mailbox so that, for each given key and message, the message corresponding to key is set to message as if by using __setitem__(). As with __setitem__(), each key must already correspond to a message in the mailbox or else a KeyError exception will be raised, so in general it is incorrect for arg to be a Mailbox instance.

注解

Unlike with dictionaries, keyword arguments are not supported.

flush()

Write any pending changes to the filesystem. For some Mailbox subclasses, changes are always written immediately and flush() does nothing, but you should still make a habit of calling this method.

lock()

Acquire an exclusive advisory lock on the mailbox so that other processes know not to modify it. An ExternalClashError is raised if the lock is not available. The particular locking mechanisms used depend upon the mailbox format. You should always lock the mailbox before making any modifications to its contents.

unlock()

Release the lock on the mailbox, if any.

close()

Flush the mailbox, unlock it if necessary, and close any open files. For some Mailbox subclasses, this method does nothing.

19.4.1.1. Maildir

class mailbox.Maildir(dirname, factory=None, create=True)

A subclass of Mailbox for mailboxes in Maildir format. Parameter factory is a callable object that accepts a file-like message representation (which behaves as if opened in binary mode) and returns a custom representation. If factory is None, MaildirMessage is used as the default message representation. If create is True, the mailbox is created if it does not exist.

It is for historical reasons that dirname is named as such rather than path.

Maildir is a directory-based mailbox format invented for the qmail mail transfer agent and now widely supported by other programs. Messages in a Maildir mailbox are stored in separate files within a common directory structure. This design allows Maildir mailboxes to be accessed and modified by multiple unrelated programs without data corruption, so file locking is unnecessary.

Maildir mailboxes contain three subdirectories, namely: tmp, new, and cur. Messages are created momentarily in the tmp subdirectory and then moved to the new subdirectory to finalize delivery. A mail user agent may subsequently move the message to the cur subdirectory and store information about the state of the message in a special “info” section appended to its file name.

Folders of the style introduced by the Courier mail transfer agent are also supported. Any subdirectory of the main mailbox is considered a folder if '.' is the first character in its name. Folder names are represented by Maildir without the leading '.'. Each folder is itself a Maildir mailbox but should not contain other folders. Instead, a logical nesting is indicated using '.' to delimit levels, e.g., “Archived.2005.07”.

注解

The Maildir specification requires the use of a colon (':') in certain message file names. However, some operating systems do not permit this character in file names, If you wish to use a Maildir-like format on such an operating system, you should specify another character to use instead. The exclamation point ('!') is a popular choice. For example:

import mailbox
mailbox.Maildir.colon = '!'

The colon attribute may also be set on a per-instance basis.

Maildir instances have all of the methods of Mailbox in addition to the following:

list_folders()

Return a list of the names of all folders.

get_folder(folder)

Return a Maildir instance representing the folder whose name is folder. A NoSuchMailboxError exception is raised if the folder does not exist.

add_folder(folder)

Create a folder whose name is folder and return a Maildir instance representing it.

remove_folder(folder)

Delete the folder whose name is folder. If the folder contains any messages, a NotEmptyError exception will be raised and the folder will not be deleted.

clean()

Delete temporary files from the mailbox that have not been accessed in the last 36 hours. The Maildir specification says that mail-reading programs should do this occasionally.

Some Mailbox methods implemented by Maildir deserve special remarks:

add(message)

__setitem__(key, message)

update(arg)

警告

These methods generate unique file names based upon the current process ID. When using multiple threads, undetected name clashes may occur and cause corruption of the mailbox unless threads are coordinated to avoid using these methods to manipulate the same mailbox simultaneously.

flush()

All changes to Maildir mailboxes are immediately applied, so this method does nothing.

lock()

unlock()

Maildir mailboxes do not support (or require) locking, so these methods do nothing.

close()

Maildir instances do not keep any open files and the underlying mailboxes do not support locking, so this method does nothing.

get_file(key)

Depending upon the host platform, it may not be possible to modify or remove the underlying message while the returned file remains open.

参见

maildir man page from qmail [http://www.qmail.org/man/man5/maildir.html]

The original specification of the format.

Using maildir format [https://cr.yp.to/proto/maildir.html]

Notes on Maildir by its inventor. Includes an updated name-creation scheme and details on “info” semantics.

maildir man page from Courier [http://www.courier-mta.org/maildir.html]

Another specification of the format. Describes a common extension for supporting folders.

19.4.1.2. mbox

class mailbox.mbox(path, factory=None, create=True)

A subclass of Mailbox for mailboxes in mbox format. Parameter factory is a callable object that accepts a file-like message representation (which behaves as if opened in binary mode) and returns a custom representation. If factory is None, mboxMessage is used as the default message representation. If create is True, the mailbox is created if it does not exist.

The mbox format is the classic format for storing mail on Unix systems. All messages in an mbox mailbox are stored in a single file with the beginning of each message indicated by a line whose first five characters are “From “.

Several variations of the mbox format exist to address perceived shortcomings in the original. In the interest of compatibility, mbox implements the original format, which is sometimes referred to as mboxo. This means that the Content-Length header, if present, is ignored and that any occurrences of “From ” at the beginning of a line in a message body are transformed to “>From ” when storing the message, although occurrences of “>From ” are not transformed to “From ” when reading the message.

Some Mailbox methods implemented by mbox deserve special remarks:

get_file(key)

Using the file after calling flush() or close() on the mbox instance may yield unpredictable results or raise an exception.

lock()

unlock()

Three locking mechanisms are used—dot locking and, if available, the flock() and lockf() system calls.

参见

mbox man page from qmail [http://www.qmail.org/man/man5/mbox.html]

A specification of the format and its variations.

mbox man page from tin [http://www.tin.org/bin/man.cgi?section=5&topic=mbox]

Another specification of the format, with details on locking.

Configuring Netscape Mail on Unix: Why The Content-Length Format is Bad [https://www.jwz.org/doc/content-length.html]

An argument for using the original mbox format rather than a variation.

“mbox” is a family of several mutually incompatible mailbox formats [https://www.loc.gov/preservation/digital/formats/fdd/fdd000383.shtml]

A history of mbox variations.

19.4.1.3. MH

class mailbox.MH(path, factory=None, create=True)

A subclass of Mailbox for mailboxes in MH format. Parameter factory is a callable object that accepts a file-like message representation (which behaves as if opened in binary mode) and returns a custom representation. If factory is None, MHMessage is used as the default message representation. If create is True, the mailbox is created if it does not exist.

MH is a directory-based mailbox format invented for the MH Message Handling System, a mail user agent. Each message in an MH mailbox resides in its own file. An MH mailbox may contain other MH mailboxes (called folders) in addition to messages. Folders may be nested indefinitely. MH mailboxes also support sequences, which are named lists used to logically group messages without moving them to sub-folders. Sequences are defined in a file called .mh_sequences in each folder.

The MH class manipulates MH mailboxes, but it does not attempt to emulate all of mh’s behaviors. In particular, it does not modify and is not affected by the context or .mh_profile files that are used by mh to store its state and configuration.

MH instances have all of the methods of Mailbox in addition to the following:

list_folders()

Return a list of the names of all folders.

get_folder(folder)

Return an MH instance representing the folder whose name is folder. A NoSuchMailboxError exception is raised if the folder does not exist.

add_folder(folder)

Create a folder whose name is folder and return an MH instance representing it.

remove_folder(folder)

Delete the folder whose name is folder. If the folder contains any messages, a NotEmptyError exception will be raised and the folder will not be deleted.

get_sequences()

Return a dictionary of sequence names mapped to key lists. If there are no sequences, the empty dictionary is returned.

set_sequences(sequences)

Re-define the sequences that exist in the mailbox based upon sequences, a dictionary of names mapped to key lists, like returned by get_sequences().

pack()

Rename messages in the mailbox as necessary to eliminate gaps in numbering. Entries in the sequences list are updated correspondingly.

注解

Already-issued keys are invalidated by this operation and should not be subsequently used.

Some Mailbox methods implemented by MH deserve special remarks:

remove(key)

__delitem__(key)

discard(key)

These methods immediately delete the message. The MH convention of marking a message for deletion by prepending a comma to its name is not used.

lock()

unlock()

Three locking mechanisms are used—dot locking and, if available, the flock() and lockf() system calls. For MH mailboxes, locking the mailbox means locking the .mh_sequences file and, only for the duration of any operations that affect them, locking individual message files.

get_file(key)

Depending upon the host platform, it may not be possible to remove the underlying message while the returned file remains open.

flush()

All changes to MH mailboxes are immediately applied, so this method does nothing.

close()

MH instances do not keep any open files, so this method is equivalent to unlock().

参见

nmh - Message Handling System [http://www.nongnu.org/nmh/]

Home page of nmh, an updated version of the original mh.

MH & nmh: Email for Users & Programmers [http://rand-mh.sourceforge.net/book/]

A GPL-licensed book on mh and nmh, with some information on the mailbox format.

19.4.1.4. Babyl

class mailbox.Babyl(path, factory=None, create=True)

A subclass of Mailbox for mailboxes in Babyl format. Parameter factory is a callable object that accepts a file-like message representation (which behaves as if opened in binary mode) and returns a custom representation. If factory is None, BabylMessage is used as the default message representation. If create is True, the mailbox is created if it does not exist.

Babyl is a single-file mailbox format used by the Rmail mail user agent included with Emacs. The beginning of a message is indicated by a line containing the two characters Control-Underscore ('\037') and Control-L ('\014'). The end of a message is indicated by the start of the next message or, in the case of the last message, a line containing a Control-Underscore ('\037') character.

Messages in a Babyl mailbox have two sets of headers, original headers and so-called visible headers. Visible headers are typically a subset of the original headers that have been reformatted or abridged to be more attractive. Each message in a Babyl mailbox also has an accompanying list of labels, or short strings that record extra information about the message, and a list of all user-defined labels found in the mailbox is kept in the Babyl options section.

Babyl instances have all of the methods of Mailbox in addition to the following:

get_labels()

Return a list of the names of all user-defined labels used in the mailbox.

注解

The actual messages are inspected to determine which labels exist in the mailbox rather than consulting the list of labels in the Babyl options section, but the Babyl section is updated whenever the mailbox is modified.

Some Mailbox methods implemented by Babyl deserve special remarks:

get_file(key)

In Babyl mailboxes, the headers of a message are not stored contiguously with the body of the message. To generate a file-like representation, the headers and body are copied together into an io.BytesIO instance, which has an API identical to that of a file. As a result, the file-like object is truly independent of the underlying mailbox but does not save memory compared to a string representation.

lock()

unlock()

Three locking mechanisms are used—dot locking and, if available, the flock() and lockf() system calls.

参见

Format of Version 5 Babyl Files [https://quimby.gnus.org/notes/BABYL]

A specification of the Babyl format.

Reading Mail with Rmail [https://www.gnu.org/software/emacs/manual/html_node/emacs/Rmail.html]

The Rmail manual, with some information on Babyl semantics.

19.4.1.5. MMDF

class mailbox.MMDF(path, factory=None, create=True)

A subclass of Mailbox for mailboxes in MMDF format. Parameter factory is a callable object that accepts a file-like message representation (which behaves as if opened in binary mode) and returns a custom representation. If factory is None, MMDFMessage is used as the default message representation. If create is True, the mailbox is created if it does not exist.

MMDF is a single-file mailbox format invented for the Multichannel Memorandum Distribution Facility, a mail transfer agent. Each message is in the same form as an mbox message but is bracketed before and after by lines containing four Control-A ('\001') characters. As with the mbox format, the beginning of each message is indicated by a line whose first five characters are “From “, but additional occurrences of “From ” are not transformed to “>From ” when storing messages because the extra message separator lines prevent mistaking such occurrences for the starts of subsequent messages.

Some Mailbox methods implemented by MMDF deserve special remarks:

get_file(key)

Using the file after calling flush() or close() on the MMDF instance may yield unpredictable results or raise an exception.

lock()

unlock()

Three locking mechanisms are used—dot locking and, if available, the flock() and lockf() system calls.

参见

mmdf man page from tin [http://www.tin.org/bin/man.cgi?section=5&topic=mmdf]

A specification of MMDF format from the documentation of tin, a newsreader.

MMDF [https://en.wikipedia.org/wiki/MMDF]

A Wikipedia article describing the Multichannel Memorandum Distribution Facility.

19.4.2. Message objects

class mailbox.Message(message=None)

A subclass of the email.message module’s Message. Subclasses of mailbox.Message add mailbox-format-specific state and behavior.

If message is omitted, the new instance is created in a default, empty state. If message is an email.message.Message instance, its contents are copied; furthermore, any format-specific information is converted insofar as possible if message is a Message instance. If message is a string, a byte string, or a file, it should contain an RFC 2822 [https://tools.ietf.org/html/rfc2822.html]-compliant message, which is read and parsed. Files should be open in binary mode, but text mode files are accepted for backward compatibility.

The format-specific state and behaviors offered by subclasses vary, but in general it is only the properties that are not specific to a particular mailbox that are supported (although presumably the properties are specific to a particular mailbox format). For example, file offsets for single-file mailbox formats and file names for directory-based mailbox formats are not retained, because they are only applicable to the original mailbox. But state such as whether a message has been read by the user or marked as important is retained, because it applies to the message itself.

There is no requirement that Message instances be used to represent messages retrieved using Mailbox instances. In some situations, the time and memory required to generate Message representations might not be acceptable. For such situations, Mailbox instances also offer string and file-like representations, and a custom message factory may be specified when a Mailbox instance is initialized.

19.4.2.1. MaildirMessage

class mailbox.MaildirMessage(message=None)

A message with Maildir-specific behaviors. Parameter message has the same meaning as with the Message constructor.

Typically, a mail user agent application moves all of the messages in the new subdirectory to the cur subdirectory after the first time the user opens and closes the mailbox, recording that the messages are old whether or not they’ve actually been read. Each message in cur has an “info” section added to its file name to store information about its state. (Some mail readers may also add an “info” section to messages in new.) The “info” section may take one of two forms: it may contain “2,” followed by a list of standardized flags (e.g., “2,FR”) or it may contain “1,” followed by so-called experimental information. Standard flags for Maildir messages are as follows:

标志

含义

解释

D

草稿

Under composition

F

已标记

标记为重要

P

已读

转发，重新发送或退回

R

已回复

回复给

S

查看

读取

T

已删除

标记为以后删除

MaildirMessage 实例提供以下方法：

get_subdir()

Return either “new” (if the message should be stored in the new subdirectory) or “cur” (if the message should be stored in the cur subdirectory).

注解

A message is typically moved from new to cur after its mailbox has been accessed, whether or not the message is has been read. A message msg has been read if "S" in msg.get_flags() is True.

set_subdir(subdir)

Set the subdirectory the message should be stored in. Parameter subdir must be either “new” or “cur”.

get_flags()

Return a string specifying the flags that are currently set. If the message complies with the standard Maildir format, the result is the concatenation in alphabetical order of zero or one occurrence of each of 'D', 'F', 'P', 'R', 'S', and 'T'. The empty string is returned if no flags are set or if “info” contains experimental semantics.

set_flags(flags)

Set the flags specified by flags and unset all others.

add_flag(flag)

Set the flag(s) specified by flag without changing other flags. To add more than one flag at a time, flag may be a string of more than one character. The current “info” is overwritten whether or not it contains experimental information rather than flags.

remove_flag(flag)

Unset the flag(s) specified by flag without changing other flags. To remove more than one flag at a time, flag maybe a string of more than one character. If “info” contains experimental information rather than flags, the current “info” is not modified.

get_date()

Return the delivery date of the message as a floating-point number representing seconds since the epoch.

set_date(date)

Set the delivery date of the message to date, a floating-point number representing seconds since the epoch.

get_info()

Return a string containing the “info” for a message. This is useful for accessing and modifying “info” that is experimental (i.e., not a list of flags).

set_info(info)

Set “info” to info, which should be a string.

When a MaildirMessage instance is created based upon an mboxMessage or MMDFMessage instance, the Status and X-Status headers are omitted and the following conversions take place:

结果状态

mboxMessage 或 MMDFMessage 状态

“cur” 子目录

O 标记

F 标记

F 标记

R 标记

A 标记

S 标记

R 标记

T 标记

D 标记

When a MaildirMessage instance is created based upon an MHMessage instance, the following conversions take place:

结果状态

MHMessage 状态

“cur” 子目录

“unseen” 序列

“cur” subdirectory and S flag

非 “unseen” 序列

F 标记

“flagged” 序列

R 标记

“replied” 序列

When a MaildirMessage instance is created based upon a BabylMessage instance, the following conversions take place:

结果状态

BabylMessage 状态

“cur” 子目录

“unseen” 标签

“cur” subdirectory and S flag

非 “unseen” 标签

P 标记

“forwarded” 或 “resent” 标签

R 标记

“answered” 标签

T 标记

“deleted” 标签

19.4.2.2. mboxMessage

class mailbox.mboxMessage(message=None)

A message with mbox-specific behaviors. Parameter message has the same meaning as with the Message constructor.

Messages in an mbox mailbox are stored together in a single file. The sender’s envelope address and the time of delivery are typically stored in a line beginning with “From ” that is used to indicate the start of a message, though there is considerable variation in the exact format of this data among mbox implementations. Flags that indicate the state of the message, such as whether it has been read or marked as important, are typically stored in Status and X-Status headers.

Conventional flags for mbox messages are as follows:

标志

含义

解释

R

读取

读取

O

Old

以前由MUA检测

D

已删除

标记为以后删除

F

已标记

标记为重要

A

已回复

回复给

The “R” and “O” flags are stored in the Status header, and the “D”, “F”, and “A” flags are stored in the X-Status header. The flags and headers typically appear in the order mentioned.

mboxMessage instances offer the following methods:

get_from()

Return a string representing the “From ” line that marks the start of the message in an mbox mailbox. The leading “From ” and the trailing newline are excluded.

set_from(from_, time_=None)

Set the “From ” line to from_, which should be specified without a leading “From ” or trailing newline. For convenience, time_ may be specified and will be formatted appropriately and appended to from_. If time_ is specified, it should be a time.struct_time instance, a tuple suitable for passing to time.strftime(), or True (to use time.gmtime()).

get_flags()

Return a string specifying the flags that are currently set. If the message complies with the conventional format, the result is the concatenation in the following order of zero or one occurrence of each of 'R', 'O', 'D', 'F', and 'A'.

set_flags(flags)

Set the flags specified by flags and unset all others. Parameter flags should be the concatenation in any order of zero or more occurrences of each of 'R', 'O', 'D', 'F', and 'A'.

add_flag(flag)

Set the flag(s) specified by flag without changing other flags. To add more than one flag at a time, flag may be a string of more than one character.

remove_flag(flag)

Unset the flag(s) specified by flag without changing other flags. To remove more than one flag at a time, flag maybe a string of more than one character.

When an mboxMessage instance is created based upon a MaildirMessage instance, a “From ” line is generated based upon the MaildirMessage instance’s delivery date, and the following conversions take place:

结果状态

MaildirMessage 状态

R 标记

S 标记

O 标记

“cur” 子目录

D 标记

T 标记

F 标记

F 标记

A 标记

R 标记

When an mboxMessage instance is created based upon an MHMessage instance, the following conversions take place:

结果状态

MHMessage 状态

R 标记 和 O 标记

非 “unseen” 序列

O 标记

“unseen” 序列

F 标记

“flagged” 序列

A 标记

“replied” 序列

When an mboxMessage instance is created based upon a BabylMessage instance, the following conversions take place:

结果状态

BabylMessage 状态

R 标记 和 O 标记

非 “unseen” 标签

O 标记

“unseen” 标签

D 标记

“deleted” 标签

A 标记

“answered” 标签

When a Message instance is created based upon an MMDFMessage instance, the “From ” line is copied and all flags directly correspond:

结果状态

MMDFMessage 状态

R 标记

R 标记

O 标记

O 标记

D 标记

D 标记

F 标记

F 标记

A 标记

A 标记

19.4.2.3. MHMessage

class mailbox.MHMessage(message=None)

A message with MH-specific behaviors. Parameter message has the same meaning as with the Message constructor.

MH messages do not support marks or flags in the traditional sense, but they do support sequences, which are logical groupings of arbitrary messages. Some mail reading programs (although not the standard mh and nmh) use sequences in much the same way flags are used with other formats, as follows:

序列

解释

未读

未读取，但先前被MUA检测到

已回复

回复给

已标记

标记为重要

MHMessage instances offer the following methods:

get_sequences()

Return a list of the names of sequences that include this message.

set_sequences(sequences)

Set the list of sequences that include this message.

add_sequence(sequence)

Add sequence to the list of sequences that include this message.

remove_sequence(sequence)

Remove sequence from the list of sequences that include this message.

When an MHMessage instance is created based upon a MaildirMessage instance, the following conversions take place:

结果状态

MaildirMessage 状态

“unseen” 序列

非 S 标记

“replied” 序列

R 标记

“flagged” 序列

F 标记

When an MHMessage instance is created based upon an mboxMessage or MMDFMessage instance, the Status and X-Status headers are omitted and the following conversions take place:

结果状态

mboxMessage 或 MMDFMessage 状态

“unseen” 序列

非 R 标记

“replied” 序列

A 标记

“flagged” 序列

F 标记

When an MHMessage instance is created based upon a BabylMessage instance, the following conversions take place:

结果状态

BabylMessage 状态

“unseen” 序列

“unseen” 标签

“replied” 序列

“answered” 标签

19.4.2.4. BabylMessage

class mailbox.BabylMessage(message=None)

A message with Babyl-specific behaviors. Parameter message has the same meaning as with the Message constructor.

Certain message labels, called attributes, are defined by convention to have special meanings. The attributes are as follows:

标签

解释

未读

未读取，但先前被MUA检测到

deleted

标记为以后删除

filed

复制到另一个文件或邮箱

answered

回复给

forwarded

已转发

edited

由用户修改

resent

已重发

By default, Rmail displays only visible headers. The BabylMessage class, though, uses the original headers because they are more complete. Visible headers may be accessed explicitly if desired.

BabylMessage instances offer the following methods:

get_labels()

返回邮件上的标签列表。

set_labels(labels)

将消息上的标签列表设置为 labels 。

add_label(label)

将 label 添加到消息上的标签列表中。

remove_label(label)

从消息上的标签列表中删除 label 。

get_visible()

Return an Message instance whose headers are the message’s visible headers and whose body is empty.

set_visible(visible)

Set the message’s visible headers to be the same as the headers in message. Parameter visible should be a Message instance, an email.message.Message instance, a string, or a file-like object (which should be open in text mode).

update_visible()

When a BabylMessage instance’s original headers are modified, the visible headers are not automatically modified to correspond. This method updates the visible headers as follows: each visible header with a corresponding original header is set to the value of the original header, each visible header without a corresponding original header is removed, and any of Date, From, Reply-To, To, CC, and Subject that are present in the original headers but not the visible headers are added to the visible headers.

When a BabylMessage instance is created based upon a MaildirMessage instance, the following conversions take place:

结果状态

MaildirMessage 状态

“unseen” 标签

非 S 标记

“deleted” 标签

T 标记

“answered” 标签

R 标记

“forwarded” 标签

P 标记

When a BabylMessage instance is created based upon an mboxMessage or MMDFMessage instance, the Status and X-Status headers are omitted and the following conversions take place:

结果状态

mboxMessage 或 MMDFMessage 状态

“unseen” 标签

非 R 标记

“deleted” 标签

D 标记

“answered” 标签

A 标记

When a BabylMessage instance is created based upon an MHMessage instance, the following conversions take place:

结果状态

MHMessage 状态

“unseen” 标签

“unseen” 序列

“answered” 标签

“replied” 序列

19.4.2.5. MMDFMessage

class mailbox.MMDFMessage(message=None)

A message with MMDF-specific behaviors. Parameter message has the same meaning as with the Message constructor.

As with message in an mbox mailbox, MMDF messages are stored with the sender’s address and the delivery date in an initial line beginning with “From “. Likewise, flags that indicate the state of the message are typically stored in Status and X-Status headers.

Conventional flags for MMDF messages are identical to those of mbox message and are as follows:

标志

含义

解释

R

读取

读取

O

Old

以前由MUA检测

D

已删除

标记为以后删除

F

已标记

标记为重要

A

已回复

回复给

The “R” and “O” flags are stored in the Status header, and the “D”, “F”, and “A” flags are stored in the X-Status header. The flags and headers typically appear in the order mentioned.

MMDFMessage instances offer the following methods, which are identical to those offered by mboxMessage:

get_from()

Return a string representing the “From ” line that marks the start of the message in an mbox mailbox. The leading “From ” and the trailing newline are excluded.

set_from(from_, time_=None)

Set the “From ” line to from_, which should be specified without a leading “From ” or trailing newline. For convenience, time_ may be specified and will be formatted appropriately and appended to from_. If time_ is specified, it should be a time.struct_time instance, a tuple suitable for passing to time.strftime(), or True (to use time.gmtime()).

get_flags()

Return a string specifying the flags that are currently set. If the message complies with the conventional format, the result is the concatenation in the following order of zero or one occurrence of each of 'R', 'O', 'D', 'F', and 'A'.

set_flags(flags)

Set the flags specified by flags and unset all others. Parameter flags should be the concatenation in any order of zero or more occurrences of each of 'R', 'O', 'D', 'F', and 'A'.

add_flag(flag)

Set the flag(s) specified by flag without changing other flags. To add more than one flag at a time, flag may be a string of more than one character.

remove_flag(flag)

Unset the flag(s) specified by flag without changing other flags. To remove more than one flag at a time, flag maybe a string of more than one character.

When an MMDFMessage instance is created based upon a MaildirMessage instance, a “From ” line is generated based upon the MaildirMessage instance’s delivery date, and the following conversions take place:

结果状态

MaildirMessage 状态

R 标记

S 标记

O 标记

“cur” 子目录

D 标记

T 标记

F 标记

F 标记

A 标记

R 标记

When an MMDFMessage instance is created based upon an MHMessage instance, the following conversions take place:

结果状态

MHMessage 状态

R 标记 和 O 标记

非 “unseen” 序列

O 标记

“unseen” 序列

F 标记

“flagged” 序列

A 标记

“replied” 序列

When an MMDFMessage instance is created based upon a BabylMessage instance, the following conversions take place:

结果状态

BabylMessage 状态

R 标记 和 O 标记

非 “unseen” 标签

O 标记

“unseen” 标签

D 标记

“deleted” 标签

A 标记

“answered” 标签

When an MMDFMessage instance is created based upon an mboxMessage instance, the “From ” line is copied and all flags directly correspond:

结果状态

mboxMessage 状态

R 标记

R 标记

O 标记

O 标记

D 标记

D 标记

F 标记

F 标记

A 标记

A 标记

19.4.3. 异常

The following exception classes are defined in the mailbox module:

exception mailbox.Error

The based class for all other module-specific exceptions.

exception mailbox.NoSuchMailboxError

Raised when a mailbox is expected but is not found, such as when instantiating a Mailbox subclass with a path that does not exist (and with the create parameter set to False), or when opening a folder that does not exist.

exception mailbox.NotEmptyError

Raised when a mailbox is not empty but is expected to be, such as when deleting a folder that contains messages.

exception mailbox.ExternalClashError

Raised when some mailbox-related condition beyond the control of the program causes it to be unable to proceed, such as when failing to acquire a lock that another program already holds a lock, or when a uniquely-generated file name already exists.

exception mailbox.FormatError

Raised when the data in a file cannot be parsed, such as when an MH instance attempts to read a corrupted .mh_sequences file.

19.4.4. 例子

A simple example of printing the subjects of all messages in a mailbox that seem interesting:

import mailbox
for message in mailbox.mbox('~/mbox'):
    subject = message['subject']       # Could possibly be None.
    if subject and 'python' in subject.lower():
        print(subject)

To copy all mail from a Babyl mailbox to an MH mailbox, converting all of the format-specific information that can be converted:

import mailbox
destination = mailbox.MH('~/Mail')
destination.lock()
for message in mailbox.Babyl('~/RMAIL'):
    destination.add(mailbox.MHMessage(message))
destination.flush()
destination.unlock()

This example sorts mail from several mailing lists into different mailboxes, being careful to avoid mail corruption due to concurrent modification by other programs, mail loss due to interruption of the program, or premature termination due to malformed messages in the mailbox:

import mailbox
import email.errors

list_names = ('python-list', 'python-dev', 'python-bugs')

boxes = {name: mailbox.mbox('~/email/%s' % name) for name in list_names}
inbox = mailbox.Maildir('~/Maildir', factory=None)

for key in inbox.iterkeys():
    try:
        message = inbox[key]
    except email.errors.MessageParseError:
        continue                # The message is malformed. Just leave it.

    for name in list_names:
        list_id = message['list-id']
        if list_id and name in list_id:
            # Get mailbox to use
            box = boxes[name]

            # Write copy to disk before removing original.
            # If there's a crash, you might duplicate a message, but
            # that's better than losing a message completely.
            box.lock()
            box.add(message)
            box.flush()
            box.unlock()

            # Remove original message
            inbox.lock()
            inbox.discard(key)
            inbox.flush()
            inbox.unlock()
            break               # Found destination, so stop looking.

for box in boxes.itervalues():
    box.close()

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

19.5. mimetypes — Map filenames to MIME types

Source code: Lib/mimetypes.py [https://github.com/python/cpython/tree/3.6/Lib/mimetypes.py]

The mimetypes module converts between a filename or URL and the MIME type associated with the filename extension. Conversions are provided from filename to MIME type and from MIME type to filename extension; encodings are not supported for the latter conversion.

The module provides one class and a number of convenience functions. The functions are the normal interface to this module, but some applications may be interested in the class as well.

The functions described below provide the primary interface for this module. If the module has not been initialized, they will call init() if they rely on the information init() sets up.

mimetypes.guess_type(url, strict=True)

Guess the type of a file based on its filename or URL, given by url. The return value is a tuple (type, encoding) where type is None if the type can’t be guessed (missing or unknown suffix) or a string of the form 'type/subtype', usable for a MIME content-type header.

encoding is None for no encoding or the name of the program used to encode (e.g. compress or gzip). The encoding is suitable for use as a Content-Encoding header, not as a Content-Transfer-Encoding header. The mappings are table driven. Encoding suffixes are case sensitive; type suffixes are first tried case sensitively, then case insensitively.

The optional strict argument is a flag specifying whether the list of known MIME types is limited to only the official types registered with IANA [https://www.iana.org/assignments/media-types/media-types.xhtml]. When strict is True (the default), only the IANA types are supported; when strict is False, some additional non-standard but commonly used MIME types are also recognized.

mimetypes.guess_all_extensions(type, strict=True)

Guess the extensions for a file based on its MIME type, given by type. The return value is a list of strings giving all possible filename extensions, including the leading dot ('.'). The extensions are not guaranteed to have been associated with any particular data stream, but would be mapped to the MIME type type by guess_type().

The optional strict argument has the same meaning as with the guess_type() function.

mimetypes.guess_extension(type, strict=True)

Guess the extension for a file based on its MIME type, given by type. The return value is a string giving a filename extension, including the leading dot ('.'). The extension is not guaranteed to have been associated with any particular data stream, but would be mapped to the MIME type type by guess_type(). If no extension can be guessed for type, None is returned.

The optional strict argument has the same meaning as with the guess_type() function.

Some additional functions and data items are available for controlling the behavior of the module.

mimetypes.init(files=None)

Initialize the internal data structures. If given, files must be a sequence of file names which should be used to augment the default type map. If omitted, the file names to use are taken from knownfiles; on Windows, the current registry settings are loaded. Each file named in files or knownfiles takes precedence over those named before it. Calling init() repeatedly is allowed.

Specifying an empty list for files will prevent the system defaults from being applied: only the well-known values will be present from a built-in list.

在 3.2 版更改: Previously, Windows registry settings were ignored.

mimetypes.read_mime_types(filename)

Load the type map given in the file filename, if it exists. The type map is returned as a dictionary mapping filename extensions, including the leading dot ('.'), to strings of the form 'type/subtype'. If the file filename does not exist or cannot be read, None is returned.

mimetypes.add_type(type, ext, strict=True)

Add a mapping from the MIME type type to the extension ext. When the extension is already known, the new type will replace the old one. When the type is already known the extension will be added to the list of known extensions.

When strict is True (the default), the mapping will be added to the official MIME types, otherwise to the non-standard ones.

mimetypes.inited

Flag indicating whether or not the global data structures have been initialized. This is set to True by init().

mimetypes.knownfiles

List of type map file names commonly installed. These files are typically named mime.types and are installed in different locations by different packages.

mimetypes.suffix_map

Dictionary mapping suffixes to suffixes. This is used to allow recognition of encoded files for which the encoding and the type are indicated by the same extension. For example, the .tgz extension is mapped to .tar.gz to allow the encoding and type to be recognized separately.

mimetypes.encodings_map

Dictionary mapping filename extensions to encoding types.

mimetypes.types_map

Dictionary mapping filename extensions to MIME types.

mimetypes.common_types

Dictionary mapping filename extensions to non-standard, but commonly found MIME types.

An example usage of the module:

>>> import mimetypes
>>> mimetypes.init()
>>> mimetypes.knownfiles
['/etc/mime.types', '/etc/httpd/mime.types', ... ]
>>> mimetypes.suffix_map['.tgz']
'.tar.gz'
>>> mimetypes.encodings_map['.gz']
'gzip'
>>> mimetypes.types_map['.tgz']
'application/x-tar-gz'

19.5.1. MimeTypes Objects

The MimeTypes class may be useful for applications which may want more than one MIME-type database; it provides an interface similar to the one of the mimetypes module.

class mimetypes.MimeTypes(filenames=(), strict=True)

This class represents a MIME-types database. By default, it provides access to the same database as the rest of this module. The initial database is a copy of that provided by the module, and may be extended by loading additional mime.types-style files into the database using the read() or readfp() methods. The mapping dictionaries may also be cleared before loading additional data if the default data is not desired.

The optional filenames parameter can be used to cause additional files to be loaded “on top” of the default database.

suffix_map

Dictionary mapping suffixes to suffixes. This is used to allow recognition of encoded files for which the encoding and the type are indicated by the same extension. For example, the .tgz extension is mapped to .tar.gz to allow the encoding and type to be recognized separately. This is initially a copy of the global suffix_map defined in the module.

encodings_map

Dictionary mapping filename extensions to encoding types. This is initially a copy of the global encodings_map defined in the module.

types_map

Tuple containing two dictionaries, mapping filename extensions to MIME types: the first dictionary is for the non-standards types and the second one is for the standard types. They are initialized by common_types and types_map.

types_map_inv

Tuple containing two dictionaries, mapping MIME types to a list of filename extensions: the first dictionary is for the non-standards types and the second one is for the standard types. They are initialized by common_types and types_map.

guess_extension(type, strict=True)

Similar to the guess_extension() function, using the tables stored as part of the object.

guess_type(url, strict=True)

Similar to the guess_type() function, using the tables stored as part of the object.

guess_all_extensions(type, strict=True)

Similar to the guess_all_extensions() function, using the tables stored as part of the object.

read(filename, strict=True)

Load MIME information from a file named filename. This uses readfp() to parse the file.

If strict is True, information will be added to list of standard types, else to the list of non-standard types.

readfp(fp, strict=True)

Load MIME type information from an open file fp. The file must have the format of the standard mime.types files.

If strict is True, information will be added to the list of standard types, else to the list of non-standard types.

read_windows_registry(strict=True)

Load MIME type information from the Windows registry. Availability: Windows.

If strict is True, information will be added to the list of standard types, else to the list of non-standard types.

3.2 新版功能.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

19.6. base64 — Base16, Base32, Base64, Base85 数据编码

源代码： Lib/base64.py [https://github.com/python/cpython/tree/3.6/Lib/base64.py]

此模块提供了将二进制数据编码为可打印的 ASCII 字符以及将这些编码解码回二进制数据的函数。它为 RFC 3548 [https://tools.ietf.org/html/rfc3548.html] 指定的 Base16, Base32 和 Base64 编码以及已被广泛接受的 Ascii85 和 Base85 编码提供了编码和解码函数。

RFC 3548 [https://tools.ietf.org/html/rfc3548.html] 编码的目的是使得二进制数据可以作为电子邮件的内容正确地发送，用作 URL 的一部分，或者作为 HTTP POST 请求的一部分。其中的编码算法和 uuencode 程序是不同的。

此模块提供了两个接口。新的接口提供了从 类字节对象 到 ASCII 字节 bytes 的编码，以及将 ASCII 的 类字节对象 或字符串解码到 bytes 的操作。此模块支持定义在 RFC 3548 [https://tools.ietf.org/html/rfc3548.html] 中的所有 base-64 字母表 （普通的、URL 安全的和文件系统安全的）。

旧的接口不提供从字符串的解码操作，但提供了操作 文件对象 的编码和解码函数。旧接口只支持标准的 Base64 字母表，并且按照 RFC 2045 [https://tools.ietf.org/html/rfc2045.html] 的规范每 76 个字符增加一个换行符。注意：如果你需要支持 RFC 2045 [https://tools.ietf.org/html/rfc2045.html]，那么使用 email 模块可能更加合适。

在 3.3 版更改: 新的接口提供的解码函数现在已经支持只包含 ASCII 的 Unicode 字符串。

在 3.4 版更改: 所有 类字节对象 现在已经被所有编码和解码函数接受。添加了对 Ascii85/Base85 的支持。

新的接口提供：

base64.b64encode(s, altchars=None)

对 bytes-like object s 进行 Base64 编码，并返回编码后的 bytes。

可选项 altchars 必须是一个长 2 字节的 bytes-like object，它指定了用于替换 + 和 / 的字符。这允许应用程序生成 URL 或文件系统安全的 Base64 字符串。默认值是 None，使用标准 Base64 字母表。

base64.b64decode(s, altchars=None, validate=False)

解码 Base64 编码过的 bytes-like object 或 ASCII 字符串 s 并返回解码过的 bytes。

可选项 altchars 必须是一个长 2 字节的 bytes-like object，它指定了用于替换 + 和 / 的字符。

如果 s 被不正确地填写，一个 binascii.Error 错误将被抛出。

如果 validate 值为 False （默认情况），则在填充检查前，将丢弃既不在标准 base-64 字母表之中也不在备用字母表中的字符。如果 validate 为 True，这些非 base64 字符将导致 binascii.Error。

base64.standard_b64encode(s)

编码 bytes-like object s，使用标准 Base64 字母表并返回编码过的 bytes。

base64.standard_b64decode(s)

解码 bytes-like object 或 ASCII 字符串 s，使用标准 Base64 字母表并返回编码过的 bytes。

base64.urlsafe_b64encode(s)

编码 bytes-like object s，使用 URL 与文件系统安全的字母表，使用 - 以及 _ 代替标准 Base64 字母表中的 + 和 /。返回编码过的 bytes。结果中可能包含 =。

base64.urlsafe_b64decode(s)

解码 bytes-like object 或 ASCII 字符串 s，使用 URL 与文件系统安全的字母表，使用 - 以及 _ 代替标准 Base64 字母表中的 + 和 /。返回解码过的 bytes

base64.b32encode(s)

用 Base32 编码 bytes-like object s 并返回编码过的 bytes

base64.b32decode(s, casefold=False, map01=None)

解码 Base32 编码过的 bytes-like object 或 ASCII 字符串 s 并返回解码过的 bytes。

可选的 casefold 是一个指定小写字幕是否可接受为输入的标志。为了安全考虑，默认值为 False。

RFC 3548 [https://tools.ietf.org/html/rfc3548.html] 允许将字母 0(zero) 映射为字母 O(oh)，并可以选择是否将字母 1(one) 映射为 I(eye) 或 L(el)。可选参数 map01 不是 None 时，它的值指定 1 的映射目标 (I 或 l)，当 map01 非 None 时， 0 总是被映射为 O。为了安全考虑，默认值被设为 None，如果是这样， 0 和 1 不允许被作为输入。

如果 s 被错误地填写或输入中存在字母表之外的字符，将抛出 binascii.Error。

base64.b16encode(s)

用 Base16 编码 bytes-like object s 并返回编码过的 bytes

base64.b16decode(s, casefold=False)

解码 Base16 编码过的 bytes-like object 或 ASCII 字符串 s 并返回解码过的 bytes。

可选的 casefold 是一个指定小写字幕是否可接受为输入的标志。为了安全考虑，默认值为 False。

如果 s 被错误地填写或输入中存在字母表之外的字符，将抛出 binascii.Error。

base64.a85encode(b, *, foldspaces=False, wrapcol=0, pad=False, adobe=False)

用 Ascii85 编码 bytes-like object s 并返回编码过的 bytes

foldspaces 是一个可选的标志，使用特殊的短序列 ‘y’ 代替 ‘btoa’ 提供的 4 个连续空格 (ASCII 0x20)。这个特性不被 “标准” Ascii85 编码支持。

wrapcol 控制了输出是否包含换行符 (b'\n'). 如果该值非零, 则每一行只有该值所限制的字符长度.

pad 控制在编码之前输入是否填充为4的倍数。请注意，btoa 实现总是填充。

adobe 控制编码后的字节序列是否要加上 <~ 和 ~>，这是 Adobe 实现所使用的。

3.4 新版功能.

base64.a85decode(b, *, foldspaces=False, adobe=False, ignorechars=b' \t\n\r\v')

解码 Ascii85 编码过的 bytes-like object 或 ASCII 字符串 s 并返回解码过的 bytes。

foldspaces 旗标指明是否应接受 ‘y’ 短序列作为 4 个连续空格 (ASCII 0x20) 的快捷方式。 此特性不被 “标准” Ascii85 编码格式所支持。

adobe 控制输入序列是否为 Adobe Ascii85 格式 (即附加 <~ 和 ~>)。

ignorechars 应当是一个 bytes-like object 或 ASCII 字符串，其中包含要从输入中忽略的字符。 这应当只包含空白字符，并且默认包含 ASCII 中所有的空白字符。

3.4 新版功能.

base64.b85encode(b, pad=False)

用 base85（如 git 风格的二进制 diff 数据所用格式）编码 bytes-like object b 并返回编码后的 bytes。

如果 pad 为真值，输入将以 b'\0' 填充以使其编码前长度为 4 字节的倍数。

3.4 新版功能.

base64.b85decode(b)

解码 base85 编码过的 bytes-like object 或 ASCII 字符串 b 并返回解码过的 bytes。 如有必要，填充会被隐式地移除。

3.4 新版功能.

旧式接口:

base64.decode(input, output)

解码二进制 input 文件的内容并将结果二进制数据写入 output 文件。 input 和 output 必须为 文件对象. input 将被读取直至 input.readline() 返回空字节串对象。

base64.decodebytes(s)

解码 bytes-like object s，该对象必须包含一行或多行 base64 编码的数据，并返回已解码的 bytes。

3.1 新版功能.

base64.decodestring(s)

已弃用的 decodebytes() 的别名。

3.1 版后已移除.

base64.encode(input, output)

编码二进制 input 文件的内容并将经 base64 编码的数据写入 output 文件。 input 和 output 必须为 文件对象。 input 将被读取直到 input.read() 返回空字节串对象。 encode() 会在每输出 76 个字节之后插入一个换行符 (b'\n')，并会确保输出总是以换行符来结束，如 RFC 2045 [https://tools.ietf.org/html/rfc2045.html] (MIME) 所规定的那样。

base64.encodebytes(s)

编码 bytes-like object s，其中可以包含任意二进制数据，并返回包含经 base64 编码数据的 bytes，每输出 76 个字节之后将带一个换行符 (b'\n')，并会确保在末尾也有一个换行符，如 RFC 2045 [https://tools.ietf.org/html/rfc2045.html] (MIME) 所规定的那样。

3.1 新版功能.

base64.encodestring(s)

一个被弃用的 encodebytes() 的别名。

3.1 版后已移除.

此模块的一个使用示例:

>>> import base64
>>> encoded = base64.b64encode(b'data to be encoded')
>>> encoded
b'ZGF0YSB0byBiZSBlbmNvZGVk'
>>> data = base64.b64decode(encoded)
>>> data
b'data to be encoded'

参见

模块 binascii

支持模块，包含ASCII到二进制和二进制到ASCII转换。

RFC 1521 [https://tools.ietf.org/html/rfc1521.html] - MIME (Multipurpose Internet Mail Extensions) 第一部分：规定并描述因特网消息体的格式的机制。

第 5.2 节，“Base64 内容转换编码格式” 提供了 base64 编码格式的定义。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

19.7. binhex — 对binhex4文件进行编码和解码

源代码: Lib/binhex.py [https://github.com/python/cpython/tree/3.6/Lib/binhex.py]

此模块以binhe4格式对文件进行编码和解码，该格式允许Macintosh文件以ASCII格式表示。仅处理数据分支。

binhex 模块定义了以下功能：

binhex.binhex(input, output)

将带有文件名 输入 的二进制文件转换为binhex文件 输出 。输出参数可以是文件名或类文件对象（ write() 和 close() 方法的任何对象）。

binhex.hexbin(input, output)

解码binhex文件输入。 输入 可以是支持 read() 和 close() 方法的文件名或类文件对象。生成的文件将写入名为 output 的文件，除非参数为 None ，在这种情况下，从binhex文件中读取输出文件名。

还定义了以下异常：

exception binhex.Error

当无法使用binhex格式编码某些内容时（例如，文件名太长而无法放入文件名字段中），或者输入未正确编码的binhex数据时，会引发异常。

参见

模块 binascii

支持模块，包含ASCII到二进制和二进制到ASCII转换。

19.7.1. 注释

还有一个替代的、功能更强大的编码器和解码器接口，详细信息请参见源代码。

如果您在非Macintosh平台上编码或解码文本文件，它们仍将使用旧的Macintosh换行符约定（回车符作为行尾）。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

19.8. binascii — 二进制和 ASCII 码互转

binascii 模块包含很多在二进制和二进制表示的各种ASCII码之间转换的方法。 通常情况不会直接使用这些函数，而是使用像 uu ， base64 ，或 binhex 这样的封装模块。 为了执行效率高，binascii 模块含有许多用 C 写的低级函数，这些底层函数被一些高级模块所使用。

注解

a2b_* 函数接受只含有 ASCII 码的Unicode 字符串。其他函数只接受 字节类对象 （例如 bytes，bytearray 和其他支持缓冲区协议的对象）。

在 3.3 版更改: ASCII-only unicode strings are now accepted by the a2b_* functions.

binascii 模块定义了以下函数：

binascii.a2b_uu(string)

将单行 uu 编码数据转换成二进制数据并返回。uu 编码每行的数据通常包含45 个（二进制）字节，最后一行除外。每行数据后面可能跟有空格。

binascii.b2a_uu(data)

Convert binary data to a line of ASCII characters, the return value is the converted line, including a newline char. The length of data should be at most 45.

binascii.a2b_base64(string)

将 base64 数据块转换成二进制并以二进制数据形式返回。一次可以传递多行数据。

binascii.b2a_base64(data, *, newline=True)

将二进制数据转换为一行用 base64 编码的ASCII字符串。返回值是转换后的行数据，如果 newline 为true，则返回值包括换行符。该函数的输出符合：rfc：3548。

在 3.6 版更改: 增加 newline 形参。

binascii.a2b_qp(data, header=False)

将一个引号可打印的数据块转换成二进制数据并返回。一次可以转换多行。如果可选参数 header 存在且为true，则数据中的下划线将被解码成空格。

binascii.b2a_qp(data, quotetabs=False, istext=True, header=False)

将二进制数据转换为一行或多行带引号可打印编码的ASCII字符串。返回值是转换后的行数据。如果可选参数 quotetabs 存在且为真值，则对所有制表符和空格进行编码。如果可选参数 istext 存在且为真值，则不对新行进行编码，但将对尾随空格进行编码。如果可选参数 header 存在且为true，则空格将被编码为下划线 RFC 1522 [https://tools.ietf.org/html/rfc1522.html]。如果可选参数 header 存在且为假值，则也会对换行符进行编码;不进行换行转换编码可能会破坏二进制数据流。

binascii.a2b_hqx(string)

将 binhex4 格式的 ASCII 数据不进行 RLE 解压缩直接转换为二进制数据。该字符串应包含完整数量的二进制字节，或者（在binhex4 数据最后部分）剩余位为零。

binascii.rledecode_hqx(data)

根据 binhex4 标准对数据执行 RLE 解压缩。该算法在一个字节的数据后使用 0x90 作为重复指示符，然后计数。计数 0 指定字节值 0x90 。该例程返回解压缩的数据，输入数据以孤立的重复指示符结束的情况下，将引发 Incomplete 异常。

在 3.2 版更改: 仅接受 bytestring 或 bytearray 对象作为输入。

binascii.rlecode_hqx(data)

在 data 上执行 binhex4 游程编码压缩并返回结果。

binascii.b2a_hqx(data)

执行 hexbin4 类型二进制到 ASCII 码的转换并返回结果字符串。输入数据应经过 RLE 编码，且数据长度可被3整除（除了最后一个片段）。

binascii.crc_hqx(data, value)

以 value 作为初始 CRC 计算 data 的16位 CRC 值，返回其结果。这里使用 CRC-CCITT 生成多项式 x¹⁶ + x¹² + x⁵ + 1 ，通常表示为0x1021。该 CRC 被用于 binhex4 格式。

binascii.crc32(data[, value])

计算 CRC-32 ，从 value 的初始 CRC 开始计算 data 的32位校验和。默认初始 CRC 为零。该算法与 ZIP 文件校验和一致。由于该算法被设计用作校验和算法，因此不适合用作通用散列算法。使用方法如下：

print(binascii.crc32(b"hello world"))
# Or, in two pieces:
crc = binascii.crc32(b"hello")
crc = binascii.crc32(b" world", crc)
print('crc32 = {:#010x}'.format(crc))

在 3.0 版更改: 校验结果始终是无符号类型的。要在所有Python版本和平台上生成相同的数值，请使用 crc32(data) & 0xffffffff 。

binascii.b2a_hex(data)

binascii.hexlify(data)

返回二进制数据 data 的十六进制表示形式。 data 的每个字节都被转换为相应的2位十六进制表示形式。因此返回的字节对象的长度是 data 的两倍。

binascii.a2b_hex(hexstr)

binascii.unhexlify(hexstr)

返回由十六进制字符串 hexstr 表示的二进制数据。此函数功能与 b2a_hex() 相反。 hexstr 必须包含偶数个十六进制数字（可以是大写或小写），否则会引发 Error 异常。

exception binascii.Error

通常是因为编程错误引发的异常。

exception binascii.Incomplete

数据不完整引发的异常。通常不是编程错误导致的，可以通过读取更多的数据并再次尝试来处理该异常。

参见

模块 base64

支持在16，32，64，85进制中进行符合 RFC 协议的 base64 样式编码。

模块 binhex

支持在 Macintosh 上使用的 binhex 格式。

模块 uu

支持在 Unix 上使用的 UU 编码。

模块 quopri

支持在 MIME 版本电子邮件中使用引号可打印编码。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

19.9. quopri — 编码与解码经过 MIME 转码的可打印数据

源代码: Lib/quopri.py [https://github.com/python/cpython/tree/3.6/Lib/quopri.py]

此模块会执行转换后可打印的传输编码与解码，具体定义见 RFC 1521 [https://tools.ietf.org/html/rfc1521.html]: “MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies”。 转换后可打印的编码格式被设计用于只包含相对较少的不可打印字符的数据；如果存在大量这样的字符，通过 base64 模块所提供的 base64 编码方案会更为紧凑，例如当发送图片文件时。

quopri.decode(input, output, header=False)

解码 input 文件的内容并将已解码二进制数据结果写入 output 文件。 input 和 output 必须为 二进制文件对象。 如果提供了可选参数 header 且为真值，下划线将被解码为空格。 此函数可用于解码“Q”编码的头数据，具体描述见 RFC 1522 [https://tools.ietf.org/html/rfc1522.html]: “MIME (Multipurpose Internet Mail Extensions) Part Two: Message Header Extensions for Non-ASCII Text”。

quopri.encode(input, output, quotetabs, header=False)

编码 input 文件的内容并将转换后可打印的数据结果写入 output 文件。 input 和 output 必须为 二进制文件对象. quotetabs 是一个非可选的旗标，它控制是否要编码内嵌的空格与制表符；当为真值时将编码此类内嵌空白符，当为假值时则保持原样不进行编码。 请注意出现在行尾的空格与制表符总是会被编码，具体描述见 RFC 1521 [https://tools.ietf.org/html/rfc1521.html]。 header 旗标控制空格符是否要编码为下划线，具体描述见 RFC 1522 [https://tools.ietf.org/html/rfc1522.html]。

quopri.decodestring(s, header=False)

类似 decode()，区别在于它接受一个源 bytes 并返回对应的已解码 bytes。

quopri.encodestring(s, quotetabs=False, header=False)

类型 encode()，区别在于它接受一个源 bytes 并返回对应的已编码 bytes。 在默认情况下，它会发送 False 值给 encode() 函数的 quotetabs 形参。

参见

模块 base64

编码与解码 MIME base64 数据

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

19.10. uu — 对 uuencode 文件进行编码与解码

源代码: Lib/uu.py [https://github.com/python/cpython/tree/3.6/Lib/uu.py]

此模块使用 uuencode 格式来编码和解码文件，以便任意二进制数据可通过仅限 ASCII 码的连接进行传输。 在任何要求文件参数的地方，这些方法都接受文件类对象。 为了保持向下兼容，也接受包含路径名称的字符串，并且将打开相应的文件进行读写；路径名称 '-' 被解读为标准输入或输出。 但是，此接口已被弃用；在 Windows 中调用者最好是自行打开文件，并在需要时确保模式为 'rb' or 'wb'。

此代码由 Lance Ellinghouse 贡献，并由 Jack Jansen 修改。

uu 模块定义了以下函数：

uu.encode(in_file, out_file, name=None, mode=None)

Uuencode file in_file into file out_file. The uuencoded file will have the header specifying name and mode as the defaults for the results of decoding the file. The default defaults are taken from in_file, or '-' and 0o666 respectively.

uu.decode(in_file, out_file=None, mode=None, quiet=False)

调用此函数会解码 uuencod 编码的 in_file 文件并将结果放入 out_file 文件。 如果 out_file 是一个路径名称，mode 会在必须创建文件时用于设置权限位。 out_file 和 mode 的默认值会从 uuencode 标头中提取。 但是，如果标头中指定的文件已存在，则会引发 uu.Error。

如果输入由不正确的 uuencode 编码器生成，decode() 可能会打印一条警告到标准错误 ，这样 Python 可以从该错误中恢复。 将 quiet 设为真值可以屏蔽此警告。

exception uu.Error

Exception 的子类，此异常可由 uu.decode() 在多种情况下引发，如上文所述，此外还包括格式错误的标头或被截断的输入文件等。

参见

模块 binascii

支持模块，包含ASCII到二进制和二进制到ASCII转换。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

19. 互联网数据处理 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

20. 结构化标记处理工具

Python 支持各种模块，以处理各种形式的结构化数据标记。 这包括使用标准通用标记语言（SGML）和超文本标记语言（HTML）的模块，以及使用可扩展标记语言（XML）的几个接口。

20.1. html — 超文本标记语言支持

20.2. html.parser — 简单的 HTML 和 XHTML 解析器

20.2.1. HTML 解析器的示例程序

20.2.2. HTMLParser 方法

20.2.3. 例子

20.3. html.entities — HTML 一般实体的定义

20.4. XML处理模块

20.4.1. XML 漏洞

20.4.2. defusedxml 和 defusedexpat 软件包

20.5. xml.etree.ElementTree — ElementTree XML API

20.5.1. 教程

20.5.1.1. XML树和元素

20.5.1.2. 解析XML

20.5.1.3. Pull API进行非阻塞解析

20.5.1.4. 寻找有趣的元素

20.5.1.5. 修改XML文件

20.5.1.6. 构建XML文档

20.5.1.7. 使用命名空间解析XML

20.5.1.8. 其他资源

20.5.2. XPath支持

20.5.2.1. 示例

20.5.2.2. 支持的XPath语法

20.5.3. 参考

20.5.3.1. 函数

20.5.3.2. 元素对象

20.5.3.3. ElementTree 对象

20.5.3.4. QName Objects

20.5.3.5. TreeBuilder Objects

20.5.3.6. XMLParser对象

20.5.3.7. XMLPullParser对象

20.5.3.8. 异常

20.6. xml.dom — The Document Object Model API

20.6.1. 模块内容

20.6.2. Objects in the DOM

20.6.2.1. DOMImplementation Objects

20.6.2.2. 节点对象

20.6.2.3. 节点列表对象

20.6.2.4. 文档类型对象

20.6.2.5. 文档对象

20.6.2.6. 元素对象

20.6.2.7. Attr 对象

20.6.2.8. NamedNodeMap 对象

20.6.2.9. 注释对象

20.6.2.10. Text 和 CDATASection 对象

20.6.2.11. ProcessingInstruction 对象

20.6.2.12. 异常

20.6.3. 一致性

20.6.3.1. 类型映射

20.6.3.2. Accessor Methods

20.7. xml.dom.minidom — Minimal DOM implementation

20.7.1. DOM Objects

20.7.2. DOM Example

20.7.3. minidom and the DOM standard

20.8. xml.dom.pulldom — Support for building partial DOM trees

20.8.1. DOMEventStream Objects

20.9. xml.sax — Support for SAX2 parsers

20.9.1. SAXException Objects

20.10. xml.sax.handler — Base classes for SAX handlers

20.10.1. ContentHandler 对象

20.10.2. DTDHandler 对象

20.10.3. EntityResolver 对象

20.10.4. ErrorHandler 对象

20.11. xml.sax.saxutils — SAX 工具集

20.12. xml.sax.xmlreader — Interface for XML parsers

20.12.1. XMLReader 对象

20.12.2. IncrementalParser 对象

20.12.3. Locator 对象

20.12.4. InputSource 对象

20.12.5. The Attributes Interface

20.12.6. The AttributesNS Interface

20.13. xml.parsers.expat — Fast XML parsing using Expat

20.13.1. XMLParser对象

20.13.2. ExpatError Exceptions

20.13.3. 示例

20.13.4. Content Model Descriptions

20.13.5. Expat error constants

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

20.1. html — 超文本标记语言支持

源码： Lib/html/__init__.py [https://github.com/python/cpython/tree/3.6/Lib/html/__init__.py]

该模块定义了操作HTML的工具。

html.escape(s, quote=True)

将字符串 s 中的字符``&`` 、 < 和 > 转换为安全的HTML序列。 如果需要在 HTML 中显示可能包含此类字符的文本，请使用此选项。 如果可选的标志 quote 为真值，则字符 (") 和 (') 也被转换；这有助于包含在由引号分隔的 HTML 属性中，如 <a href="...">。

3.2 新版功能.

html.unescape(s)

将字符串 s 中的所有命名和数字字符引用 (例如 &gt;, &#62;, &#x3e;) 转换为相应的Unicode字符。 此函数使用HTML 5标准为有效和无效字符引用定义的规则，以及 HTML 5 命名字符引用列表。

3.4 新版功能.

html 包中的子模块是：

html.parser —— 具有宽松解析模式的HTML / XHTML解析器

html.entities – HTML 实体定义

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

20.2. html.parser — 简单的 HTML 和 XHTML 解析器

源代码： Lib/html/parser.py [https://github.com/python/cpython/tree/3.6/Lib/html/parser.py]

这个模块定义了一个 HTMLParser 类，为 HTML（超文本标记语言）和 XHTML 文本文件解析提供基础。

class html.parser.HTMLParser(*, convert_charrefs=True)

创建一个能解析无效标记的解析器实例。

如果 convert_charrefs 为 True (默认值)，则所有字符引用( script/style 元素中的除外)都会自动转换为相应的 Unicode 字符。

一个 HTMLParser 类的实例用来接受 HTML 数据，并在标记开始、标记结束、文本、注释和其他元素标记出现的时候调用对应的方法。要实现具体的行为，请使用 HTMLParser 的子类并重载其方法。

这个解析器不检查结束标记是否与开始标记匹配，也不会因外层元素完毕而隐式关闭了的元素引发结束标记处理。

在 3.4 版更改: convert_charrefs 关键字参数被添加。

在 3.5 版更改: convert_charrefs 参数的默认值现在为 True。

20.2.1. HTML 解析器的示例程序

下面是简单的 HTML 解析器的一个基本示例，使用 HTMLParser 类，当遇到开始标记、结束标记以及数据的时候将内容打印出来。

from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print("Encountered a start tag:", tag)

    def handle_endtag(self, tag):
        print("Encountered an end tag :", tag)

    def handle_data(self, data):
        print("Encountered some data  :", data)

parser = MyHTMLParser()
parser.feed('<html><head><title>Test</title></head>'
            '<body><h1>Parse me!</h1></body></html>')

输出是:

Encountered a start tag: html
Encountered a start tag: head
Encountered a start tag: title
Encountered some data  : Test
Encountered an end tag : title
Encountered an end tag : head
Encountered a start tag: body
Encountered a start tag: h1
Encountered some data  : Parse me!
Encountered an end tag : h1
Encountered an end tag : body
Encountered an end tag : html

20.2.2. HTMLParser 方法

HTMLParser 实例有下列方法：

HTMLParser.feed(data)

填充一些文本到解析器中。如果包含完整的元素，则被处理；如果数据不完整，将被缓冲直到更多的数据被填充，或者 close() 被调用。data 必须为 str 类型。

HTMLParser.close()

如同后面跟着一个文件结束标记一样，强制处理所有缓冲数据。这个方法能被派生类重新定义，用于在输入的末尾定义附加处理，但是重定义的版本应当始终调用基类 HTMLParser 的 close() 方法。

HTMLParser.reset()

重置实例。丢失所有未处理的数据。在实例化阶段被隐式调用。

HTMLParser.getpos()

返回当前行号和偏移值。

HTMLParser.get_starttag_text()

返回最近打开的开始标记中的文本。 结构化处理时通常应该不需要这个，但在处理“已部署”的 HTML 或是在以最小改变来重新生成输入时可能会有用处（例如可以保留属性间的空格等）。

下列方法将在遇到数据或者标记元素的时候被调用。他们需要在子类中重载。基类的实现中没有任何实际操作（除了 handle_startendtag() ）：

HTMLParser.handle_starttag(tag, attrs)

这个方法在标签开始的时候被调用（例如： <div id="main"> ）。

tag 参数是小写的标记名。attrs 参数是一个 (name, value) 形式的列表，包含了所有在标记的 <> 括号中找到的属性。name 转换为小写，value 的引号被去除，字符和实体引用都会被替换。

实例中，对于标签 <A HREF="https://www.cwi.nl/">，这个方法将以下列形式被调用 handle_starttag('a', [('href', 'https://www.cwi.nl/')]) 。

html.entities 中的所有实体引用，会被替换为属性值。

HTMLParser.handle_endtag(tag)

此方法被用来处理元素的结束标记（例如： </div> ）。

tag 参数是小写的标签名。

HTMLParser.handle_startendtag(tag, attrs)

类似于 handle_starttag(), 只是在解析器遇到 XHTML 样式的空标记时被调用（ <img ... />）。这个方法能被需要这种特殊词法信息的子类重载；默认实现仅简单调用 handle_starttag() 和 handle_endtag() 。

HTMLParser.handle_data(data)

这个方法被用来处理任意数据（例如：文本节点和 <script>...</script> 以及 <style>...</style> 中的内容）。

HTMLParser.handle_entityref(name)

这个方法被用于处理 &name; 形式的命名字符引用（例如 &gt;），其中 name 是通用的实体引用（例如： 'gt'）。如果 convert_charrefs 为 True，该方法永远不会被调用。

HTMLParser.handle_charref(name)

这个方法被用来处理 &#NNN; 和 &#xNNN; 形式的十进制和十六进制字符引用。例如，&gt; 等效的十进制形式为 &#62; ，而十六进制形式为 &#x3E; ；在这种情况下，方法将收到 '62' 或 'x3E' 。如果 convert_charrefs 为 True ，则该方法永远不会被调用。

HTMLParser.handle_comment(data)

这个方法在遇到注释的时候被调用（例如： <!--comment--> ）。

例如， <!-- comment --> 这个注释会用 ' comment ' 作为参数调用此方法。

Internet Explorer 条件注释（condcoms）的内容也被发送到这个方法，因此，对于 <!--[if IE 9]>IE9-specific content<![endif]--> ，这个方法将接收到 '[if IE 9]>IE9-specific content<![endif]' 。

HTMLParser.handle_decl(decl)

这个方法用来处理 HTML doctype 申明（例如 <!DOCTYPE html> ）。

decl 形参为 <!...> 标记中的所有内容（例如： 'DOCTYPE html' ）。

HTMLParser.handle_pi(data)

此方法在遇到处理指令的时候被调用。data 形参将包含整个处理指令。例如，对于处理指令 <?proc color='red'> ，这个方法将以 handle_pi("proc color='red'") 形式被调用。它旨在被派生类重载；基类实现中无任何实际操作。

注解

HTMLParser 类使用 SGML 语法规则处理指令。使用 '?' 结尾的 XHTML 处理指令将导致 '?' 包含在 data 中。

HTMLParser.unknown_decl(data)

当解析器读到无法识别的声明时，此方法被调用。

data 形参为 <![...]> 标记中的所有内容。某些时候对派生类的重载很有用。基类实现中无任何实际操作。

20.2.3. 例子

下面的类实现了一个解析器，用于更多示例的演示:

from html.parser import HTMLParser
from html.entities import name2codepoint

class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        print("Start tag:", tag)
        for attr in attrs:
            print("     attr:", attr)

    def handle_endtag(self, tag):
        print("End tag  :", tag)

    def handle_data(self, data):
        print("Data     :", data)

    def handle_comment(self, data):
        print("Comment  :", data)

    def handle_entityref(self, name):
        c = chr(name2codepoint[name])
        print("Named ent:", c)

    def handle_charref(self, name):
        if name.startswith('x'):
            c = chr(int(name[1:], 16))
        else:
            c = chr(int(name))
        print("Num ent  :", c)

    def handle_decl(self, data):
        print("Decl     :", data)

parser = MyHTMLParser()

解析一个文档类型声明:

>>> parser.feed('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" '
...             '"http://www.w3.org/TR/html4/strict.dtd">')
Decl     : DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"

解析一个具有一些属性和标题的元素:

>>> parser.feed('<img src="python-logo.png" alt="The Python logo">')
Start tag: img
     attr: ('src', 'python-logo.png')
     attr: ('alt', 'The Python logo')
>>>
>>> parser.feed('<h1>Python</h1>')
Start tag: h1
Data     : Python
End tag  : h1

script 和 style 元素中的内容原样返回，无需进一步解析:

>>> parser.feed('<style type="text/css">#python { color: green }</style>')
Start tag: style
     attr: ('type', 'text/css')
Data     : #python { color: green }
End tag  : style

>>> parser.feed('<script type="text/javascript">'
...             'alert("<strong>hello!</strong>");</script>')
Start tag: script
     attr: ('type', 'text/javascript')
Data     : alert("<strong>hello!</strong>");
End tag  : script

解析注释:

>>> parser.feed('<!-- a comment -->'
...             '<!--[if IE 9]>IE-specific content<![endif]-->')
Comment  :  a comment
Comment  : [if IE 9]>IE-specific content<![endif]

解析命名或数字形式的字符引用，并把他们转换到正确的字符（注意：这 3 种转义都是 '>' ）:

>>> parser.feed('&gt;&#62;&#x3E;')
Named ent: >
Num ent  : >
Num ent  : >

填充不完整的块给 feed() 执行，handle_data() 可能会多次调用（除非 convert_charrefs 被设置为 True ）:

>>> for chunk in ['<sp', 'an>buff', 'ered ', 'text</s', 'pan>']:
...     parser.feed(chunk)
...
Start tag: span
Data     : buff
Data     : ered
Data     : text
End tag  : span

解析无效的 HTML (例如：未引用的属性）也能正常运行:

>>> parser.feed('<p><a class=link href=#main>tag soup</p ></a>')
Start tag: p
Start tag: a
     attr: ('class', 'link')
     attr: ('href', '#main')
Data     : tag soup
End tag  : p
End tag  : a

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

20.3. html.entities — HTML 一般实体的定义

源码： Lib/html/entities.py [https://github.com/python/cpython/tree/3.6/Lib/html/entities.py]

该模块定义了四个词典， html5、 name2codepoint、 codepoint2name、以及 entitydefs。

html.entities.html5

将 HTML5 命名字符引用 1 映射到等效的 Unicode 字符的字典，例如 html5['gt;'] == '>'。 请注意，尾随的分号包含在名称中（例如 'gt;' ），但是即使没有分号，一些名称也会被标准接受，在这种情况下，名称出现时带有和不带有 ';'。另见 html.unescape()。

3.3 新版功能.

html.entities.entitydefs

将 XHTML 1.0 实体定义映射到 ISO Latin-1 中的替换文本的字典。

html.entities.name2codepoint

将 HTML 实体名称映射到 Unicode 代码点的字典。

html.entities.codepoint2name

将 Unicode 代码点映射到 HTML 实体名称的字典。

备注

1

参见 https://www.w3.org/TR/html5/syntax.html#named-character-references

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

20.4. XML处理模块

源码： Lib/xml/ [https://github.com/python/cpython/tree/3.6/Lib/xml/]

用于处理XML的Python接口分组在 xml 包中。

警告

XML 模块对于错误或恶意构造的数据是不安全的。 如果需要解析不受信任或未经身份验证的数据，请参阅 XML 漏洞 和 defusedxml 和 defusedexpat 软件包 部分。

值得注意的是 xml 包中的模块要求至少有一个 SAX 兼容的 XML 解析器可用。在 Pythonm中包含 Expat 解析器，因此 xml.parsers.expat 模块将始终可用。

xml.dom 和 xml.sax 包的文档是 DOM 和 SAX 接口的 Python 绑定的定义。

XML 处理子模块包括:

xml.etree.ElementTree： ElementTree API，一个简单而轻量级的XML处理器

xml.dom：DOM API 定义

xml.dom.minidom：最小的 DOM 实现

xml.dom.pulldom：支持构建部分 DOM 树

xml.sax：SAX2 基类和便利函数

xml.parsers.expat：Expat解析器绑定

20.4.1. XML 漏洞

XML 处理模块对于恶意构造的数据是不安全的。 攻击者可能滥用 XML 功能来执行拒绝服务攻击、访问本地文件、生成与其它计算机的网络连接或绕过防火墙。

下表概述了已知的攻击以及各种模块是否容易受到攻击。

种类

sax

etree

minidom

pulldom

xmlrpc

billion laughs

易受攻击

易受攻击

易受攻击

易受攻击

易受攻击

quadratic blowup

易受攻击

易受攻击

易受攻击

易受攻击

易受攻击

external entity expansion

安全 (4)

安全 (1)

安全 (2)

安全 (4)

安全 (3)

DTD [https://en.wikipedia.org/wiki/Document_type_definition] retrieval

安全 (4)

安全

安全

安全 (4)

安全

decompression bomb

安全

安全

安全

安全

易受攻击

xml.etree.ElementTree 不会扩展外部实体并在实体发生时引发 ParserError。

xml.dom.minidom 不会扩展外部实体，只是简单地返回未扩展的实体。

xmlrpclib 不扩展外部实体并省略它们。

Since Python 3.8.0, external general entities are no longer processed by default since Python.

billion laughs / exponential entity expansion （狂笑/递归实体扩展）

Billion Laughs [https://en.wikipedia.org/wiki/Billion_laughs] 攻击 – 也称为递归实体扩展 – 使用多级嵌套实体。 每个实体多次引用另一个实体，最终实体定义包含一个小字符串。 指数级扩展导致几千 GB 的文本，并消耗大量内存和 CPU 时间。

quadratic blowup entity expansion（二次爆炸实体扩展）

二次爆炸攻击类似于 Billion Laughs [https://en.wikipedia.org/wiki/Billion_laughs] 攻击，它也滥用实体扩展。 它不是嵌套实体，而是一遍又一遍地重复一个具有几千个字符的大型实体。攻击不如递归情况有效，但它避免触发禁止深度嵌套实体的解析器对策。

external entity expansion

实体声明可以包含的不仅仅是替换文本。 它们还可以指向外部资源或本地文件。 XML 解析器访问资源并将内容嵌入到 XML 文档中。

DTD [https://en.wikipedia.org/wiki/Document_type_definition] retrieval

Python 的一些 XML 库 xml.dom.pulldom 从远程或本地位置检索文档类型定义。 该功能与外部实体扩展问题具有相似的含义。

decompression bomb

Decompression bombs（解压炸弹，又名 ZIP bomb [https://en.wikipedia.org/wiki/Zip_bomb]）适用于所有可以解析压缩 XML 流（例如 gzip 压缩的 HTTP 流或 LZMA 压缩的文件）的 XML 库。 对于攻击者来说，它可以将传输的数据量减少三个量级或更多。

PyPI上 defusedxml [https://pypi.org/project/defusedxml/] 的文档包含有关所有已知攻击向量的更多信息以及示例和参考。

20.4.2. defusedxml 和 defusedexpat 软件包

defusedxml [https://pypi.org/project/defusedxml/] 是一个纯 Python 软件包，它修改了所有标准库 XML 解析器的子类，可以防止任何潜在的恶意操作。 对于解析不受信任的XML数据的任何服务器代码，建议使用此程序包。 该软件包还提供了有关更多 XML 漏洞（如 XPath 注入）的示例漏洞和扩展文档。

defusedexpat [https://pypi.org/project/defusedexpat/] 提供了一个修改过的 libexpat 和一个打过补丁的 pyexpat 模块，它有针对实体扩展DoS攻击的对策。 defusedexpat 模块仍然允许合理且可配置的实体扩展量。 这些修改可能包含在 Python 的某些未来版本中，但不会包含在 Python 的任何修复版本中，因为它们会破坏向后兼容性。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

20.5. xml.etree.ElementTree — ElementTree XML API

源代码： Lib/xml/etree/ElementTree.py [https://github.com/python/cpython/tree/3.6/Lib/xml/etree/ElementTree.py]

xml.etree.ElementTree 模块实现了一个简单高效的API，用于解析和创建XML数据。

在 3.3 版更改: 只要有可能，这个模块将使用快速实现。xml.etree.cElementTree 模块已弃用。

警告

xml.etree.ElementTree 模块对于恶意构建的数据是不安全的。如果需要解析不可信或未经身份验证的数据，请参见 XML 漏洞 。

20.5.1. 教程

这是一个使用 xml.etree.ElementTree （简称 ET ）的简短教程。目标是演示模块的一些构建块和基本概念。

20.5.1.1. XML树和元素

XML是一种固有的分层数据格式，最自然的表示方法是使用树。为此， ET 有两个类 ElementTree 将整个XML文档表示为一个树， Element 表示该树中的单个节点。与整个文档的交互（读写文件）通常在 ElementTree 级别完成。与单个XML元素及其子元素的交互是在 Element 级别完成的。

20.5.1.2. 解析XML

我们将使用以下XML文档作为本节的示例数据：

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

我们可以通过从文件中读取来导入此数据：

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()

或直接从字符串中：

root = ET.fromstring(country_data_as_string)

fromstring() 将XML从字符串直接解析为 Element ，该元素是已解析树的根元素。其他解析函数可能会创建一个 ElementTree 。确切的信息请检查文档。

作为一个 Element ， root 有一个标记和一个属性字典:

>>> root.tag
'data'
>>> root.attrib
{}

它还有我们可以迭代的子节点：

>>> for child in root:
...     print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}

子级是可以嵌套的，我们可以通过索引访问特定的子级节点：

>>> root[0][1].text
'2008'

注解

并非XML输入的所有元素都将作为解析树的元素结束。目前，此模块跳过输入中的任何XML注释、处理指令和文档类型声明。然而，使用这个模块的API而不是从XML文本解析构建的树可以包含注释和处理指令，生成XML输出时同样包含这些注释和处理指令。可以通过将自定义 TreeBuilder 实例传递给 XMLParser 构造函数来访问文档类型声明。

20.5.1.3. Pull API进行非阻塞解析

Most parsing functions provided by this module require the whole document to be read at once before returning any result. It is possible to use an XMLParser and feed data into it incrementally, but it is a push API that calls methods on a callback target, which is too low-level and inconvenient for most needs. Sometimes what the user really wants is to be able to parse XML incrementally, without blocking operations, while enjoying the convenience of fully constructed Element objects.

The most powerful tool for doing this is XMLPullParser. It does not require a blocking read to obtain the XML data, and is instead fed with data incrementally with XMLPullParser.feed() calls. To get the parsed XML elements, call XMLPullParser.read_events(). Here is an example:

>>> parser = ET.XMLPullParser(['start', 'end'])
>>> parser.feed('<mytag>sometext')
>>> list(parser.read_events())
[('start', <Element 'mytag' at 0x7fa66db2be58>)]
>>> parser.feed(' more text</mytag>')
>>> for event, elem in parser.read_events():
...     print(event)
...     print(elem.tag, 'text=', elem.text)
...
end

The obvious use case is applications that operate in a non-blocking fashion where the XML data is being received from a socket or read incrementally from some storage device. In such cases, blocking reads are unacceptable.

Because it’s so flexible, XMLPullParser can be inconvenient to use for simpler use-cases. If you don’t mind your application blocking on reading XML data but would still like to have incremental parsing capabilities, take a look at iterparse(). It can be useful when you’re reading a large XML document and don’t want to hold it wholly in memory.

20.5.1.4. 寻找有趣的元素

Element 有一些很有效的方法，可帮助递归遍历其下的所有子树（包括子级，子级的子级，等等）。例如 Element.iter():

>>> for neighbor in root.iter('neighbor'):
...     print(neighbor.attrib)
...
{'name': 'Austria', 'direction': 'E'}
{'name': 'Switzerland', 'direction': 'W'}
{'name': 'Malaysia', 'direction': 'N'}
{'name': 'Costa Rica', 'direction': 'W'}
{'name': 'Colombia', 'direction': 'E'}

Element.findall() 仅查找当前元素的直接子元素中带有指定标签的元素。 Element.find() 找带有特定标签的 第一个 子级，然后可以用 Element.text 访问元素的文本内容。 Element.text 访问元素的属性:

>>> for country in root.findall('country'):
...     rank = country.find('rank').text
...     name = country.get('name')
...     print(name, rank)
...
Liechtenstein 1
Singapore 4
Panama 68

通过使用 XPath ，可以更精确地指定要查找的元素。

20.5.1.5. 修改XML文件

ElementTree 提供了一种构建XML文档并将其写入文件的简单方法。 ElementTree.write() 方法可达到此目的。

创建后可以直接操作 Element 对象。例如：使用 Element.text 修改文本字段，使用 Element.set() 方法添加和修改属性，以及使用 Element.append() 添加新的子元素。

假设我们要在每个国家/地区的中添加一个排名，并在rank元素中添加一个 updated 属性：

>>> for rank in root.iter('rank'):
...     new_rank = int(rank.text) + 1
...     rank.text = str(new_rank)
...     rank.set('updated', 'yes')
...
>>> tree.write('output.xml')

生成的XML现在看起来像这样：

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

可以使用 Element.remove() 删除元素。假设我们要删除排名高于50的所有国家/地区:

>>> for country in root.findall('country'):
...     rank = int(country.find('rank').text)
...     if rank > 50:
...         root.remove(country)
...
>>> tree.write('output.xml')

生成的XML现在看起来像这样：

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
</data>

20.5.1.6. 构建XML文档

SubElement() 函数还提供了一种便捷方法来为给定元素创建新的子元素:

>>> a = ET.Element('a')
>>> b = ET.SubElement(a, 'b')
>>> c = ET.SubElement(a, 'c')
>>> d = ET.SubElement(c, 'd')
>>> ET.dump(a)
<a><b /><c><d /></c></a>

20.5.1.7. 使用命名空间解析XML

If the XML input has namespaces [https://en.wikipedia.org/wiki/XML_namespace], tags and attributes with prefixes in the form prefix:sometag get expanded to {uri}sometag where the prefix is replaced by the full URI. Also, if there is a default namespace [https://www.w3.org/TR/xml-names/#defaulting], that full URI gets prepended to all of the non-prefixed tags.

Here is an XML example that incorporates two namespaces, one with the prefix “fictional” and the other serving as the default namespace:

<?xml version="1.0"?>
<actors xmlns:fictional="http://characters.example.com"
        xmlns="http://people.example.com">
    <actor>
        <name>John Cleese</name>
        <fictional:character>Lancelot</fictional:character>
        <fictional:character>Archie Leach</fictional:character>
    </actor>
    <actor>
        <name>Eric Idle</name>
        <fictional:character>Sir Robin</fictional:character>
        <fictional:character>Gunther</fictional:character>
        <fictional:character>Commander Clement</fictional:character>
    </actor>
</actors>

One way to search and explore this XML example is to manually add the URI to every tag or attribute in the xpath of a find() or findall():

root = fromstring(xml_text)
for actor in root.findall('{http://people.example.com}actor'):
    name = actor.find('{http://people.example.com}name')
    print(name.text)
    for char in actor.findall('{http://characters.example.com}character'):
        print(' |-->', char.text)

A better way to search the namespaced XML example is to create a dictionary with your own prefixes and use those in the search functions:

ns = {'real_person': 'http://people.example.com',
      'role': 'http://characters.example.com'}

for actor in root.findall('real_person:actor', ns):
    name = actor.find('real_person:name', ns)
    print(name.text)
    for char in actor.findall('role:character', ns):
        print(' |-->', char.text)

These two approaches both output:

John Cleese
 |--> Lancelot
 |--> Archie Leach
Eric Idle
 |--> Sir Robin
 |--> Gunther
 |--> Commander Clement

20.5.1.8. 其他资源

See http://effbot.org/zone/element-index.htm for tutorials and links to other docs.

20.5.2. XPath支持

This module provides limited support for XPath expressions [https://www.w3.org/TR/xpath] for locating elements in a tree. The goal is to support a small subset of the abbreviated syntax; a full XPath engine is outside the scope of the module.

20.5.2.1. 示例

Here’s an example that demonstrates some of the XPath capabilities of the module. We’ll be using the countrydata XML document from the Parsing XML section:

import xml.etree.ElementTree as ET

root = ET.fromstring(countrydata)

# Top-level elements
root.findall(".")

# All 'neighbor' grand-children of 'country' children of the top-level
# elements
root.findall("./country/neighbor")

# Nodes with name='Singapore' that have a 'year' child
root.findall(".//year/..[@name='Singapore']")

# 'year' nodes that are children of nodes with name='Singapore'
root.findall(".//*[@name='Singapore']/year")

# All 'neighbor' nodes that are the second child of their parent
root.findall(".//neighbor[2]")

20.5.2.2. 支持的XPath语法

语法

含义

tag

Selects all child elements with the given tag. For example, spam selects all child elements named spam, and spam/egg selects all grandchildren named egg in all children named spam.

*

Selects all child elements. For example, */egg selects all grandchildren named egg.

.

选择当前节点。这在路径的开头非常有用，用于指示它是相对路径。

//

Selects all subelements, on all levels beneath the current element. For example, .//egg selects all egg elements in the entire tree.

..

Selects the parent element. Returns None if the path attempts to reach the ancestors of the start element (the element find was called on).

[@attrib]

选择具有给定属性的所有元素。

[@attrib='value']

选择给定属性具有给定值的所有元素。该值不能包含引号。

[tag]

选择所有包含 tag 子元素的元素。只支持直系子元素。

[tag='text']

选择所有包含名为 tag 的子元素的元素，这些子元素（包括后代）的完整文本内容等于给定的 text 。

[position]

Selects all elements that are located at the given position. The position can be either an integer (1 is the first position), the expression last() (for the last position), or a position relative to the last position (e.g. last()-1).

谓词（方括号内的表达式）之前必须带有标签名称，星号或其他谓词。position 谓词前必须有标签名称。

20.5.3. 参考

20.5.3.1. 函数

xml.etree.ElementTree.Comment(text=None)

Comment element factory. This factory function creates a special element that will be serialized as an XML comment by the standard serializer. The comment string can be either a bytestring or a Unicode string. text is a string containing the comment string. Returns an element instance representing a comment.

Note that XMLParser skips over comments in the input instead of creating comment objects for them. An ElementTree will only contain comment nodes if they have been inserted into to the tree using one of the Element methods.

xml.etree.ElementTree.dump(elem)

Writes an element tree or element structure to sys.stdout. This function should be used for debugging only.

The exact output format is implementation dependent. In this version, it’s written as an ordinary XML file.

elem is an element tree or an individual element.

xml.etree.ElementTree.fromstring(text)

Parses an XML section from a string constant. Same as XML(). text is a string containing XML data. Returns an Element instance.

xml.etree.ElementTree.fromstringlist(sequence, parser=None)

Parses an XML document from a sequence of string fragments. sequence is a list or other sequence containing XML data fragments. parser is an optional parser instance. If not given, the standard XMLParser parser is used. Returns an Element instance.

3.2 新版功能.

xml.etree.ElementTree.iselement(element)

Checks if an object appears to be a valid element object. element is an element instance. Returns a true value if this is an element object.

xml.etree.ElementTree.iterparse(source, events=None, parser=None)

Parses an XML section into an element tree incrementally, and reports what’s going on to the user. source is a filename or file object containing XML data. events is a sequence of events to report back. The supported events are the strings "start", "end", "start-ns" and "end-ns" (the “ns” events are used to get detailed namespace information). If events is omitted, only "end" events are reported. parser is an optional parser instance. If not given, the standard XMLParser parser is used. parser must be a subclass of XMLParser and can only use the default TreeBuilder as a target. Returns an iterator providing (event, elem) pairs.

Note that while iterparse() builds the tree incrementally, it issues blocking reads on source (or the file it names). As such, it’s unsuitable for applications where blocking reads can’t be made. For fully non-blocking parsing, see XMLPullParser.

注解

iterparse() only guarantees that it has seen the “>” character of a starting tag when it emits a “start” event, so the attributes are defined, but the contents of the text and tail attributes are undefined at that point. The same applies to the element children; they may or may not be present.

If you need a fully populated element, look for “end” events instead.

3.4 版后已移除: parser 参数。

xml.etree.ElementTree.parse(source, parser=None)

Parses an XML section into an element tree. source is a filename or file object containing XML data. parser is an optional parser instance. If not given, the standard XMLParser parser is used. Returns an ElementTree instance.

xml.etree.ElementTree.ProcessingInstruction(target, text=None)

PI element factory. This factory function creates a special element that will be serialized as an XML processing instruction. target is a string containing the PI target. text is a string containing the PI contents, if given. Returns an element instance, representing a processing instruction.

Note that XMLParser skips over processing instructions in the input instead of creating comment objects for them. An ElementTree will only contain processing instruction nodes if they have been inserted into to the tree using one of the Element methods.

xml.etree.ElementTree.register_namespace(prefix, uri)

Registers a namespace prefix. The registry is global, and any existing mapping for either the given prefix or the namespace URI will be removed. prefix is a namespace prefix. uri is a namespace uri. Tags and attributes in this namespace will be serialized with the given prefix, if at all possible.

3.2 新版功能.

xml.etree.ElementTree.SubElement(parent, tag, attrib={}, **extra)

Subelement factory. This function creates an element instance, and appends it to an existing element.

The element name, attribute names, and attribute values can be either bytestrings or Unicode strings. parent is the parent element. tag is the subelement name. attrib is an optional dictionary, containing element attributes. extra contains additional attributes, given as keyword arguments. Returns an element instance.

xml.etree.ElementTree.tostring(element, encoding="us-ascii", method="xml", *, short_empty_elements=True)

Generates a string representation of an XML element, including all subelements. element is an Element instance. encoding 1 is the output encoding (default is US-ASCII). Use encoding="unicode" to generate a Unicode string (otherwise, a bytestring is generated). method is either "xml", "html" or "text" (default is "xml"). short_empty_elements has the same meaning as in ElementTree.write(). Returns an (optionally) encoded string containing the XML data.

3.4 新版功能: short_empty_elements 形参。

xml.etree.ElementTree.tostringlist(element, encoding="us-ascii", method="xml", *, short_empty_elements=True)

Generates a string representation of an XML element, including all subelements. element is an Element instance. encoding 1 is the output encoding (default is US-ASCII). Use encoding="unicode" to generate a Unicode string (otherwise, a bytestring is generated). method is either "xml", "html" or "text" (default is "xml"). short_empty_elements has the same meaning as in ElementTree.write(). Returns a list of (optionally) encoded strings containing the XML data. It does not guarantee any specific sequence, except that b"".join(tostringlist(element)) == tostring(element).

3.2 新版功能.

3.4 新版功能: short_empty_elements 形参。

xml.etree.ElementTree.XML(text, parser=None)

Parses an XML section from a string constant. This function can be used to embed “XML literals” in Python code. text is a string containing XML data. parser is an optional parser instance. If not given, the standard XMLParser parser is used. Returns an Element instance.

xml.etree.ElementTree.XMLID(text, parser=None)

Parses an XML section from a string constant, and also returns a dictionary which maps from element id:s to elements. text is a string containing XML data. parser is an optional parser instance. If not given, the standard XMLParser parser is used. Returns a tuple containing an Element instance and a dictionary.

20.5.3.2. 元素对象

class xml.etree.ElementTree.Element(tag, attrib={}, **extra)

Element class. This class defines the Element interface, and provides a reference implementation of this interface.

The element name, attribute names, and attribute values can be either bytestrings or Unicode strings. tag is the element name. attrib is an optional dictionary, containing element attributes. extra contains additional attributes, given as keyword arguments.

tag

一个标识此元素意味着何种数据的字符串(换句话说，元素类型)。

text

tail

These attributes can be used to hold additional data associated with the element. Their values are usually strings but may be any application-specific object. If the element is created from an XML file, the text attribute holds either the text between the element’s start tag and its first child or end tag, or None, and the tail attribute holds either the text between the element’s end tag and the next tag, or None. For the XML data

<a><b>1<c>2<d/>3</c></b>4</a>

the a element has None for both text and tail attributes, the b element has text "1" and tail "4", the c element has text "2" and tail None, and the d element has text None and tail "3".

To collect the inner text of an element, see itertext(), for example "".join(element.itertext()).

Applications may store arbitrary objects in these attributes.

attrib

A dictionary containing the element’s attributes. Note that while the attrib value is always a real mutable Python dictionary, an ElementTree implementation may choose to use another internal representation, and create the dictionary only if someone asks for it. To take advantage of such implementations, use the dictionary methods below whenever possible.

The following dictionary-like methods work on the element attributes.

clear()

Resets an element. This function removes all subelements, clears all attributes, and sets the text and tail attributes to None.

get(key, default=None)

Gets the element attribute named key.

Returns the attribute value, or default if the attribute was not found.

items()

Returns the element attributes as a sequence of (name, value) pairs. The attributes are returned in an arbitrary order.

keys()

Returns the elements attribute names as a list. The names are returned in an arbitrary order.

set(key, value)

Set the attribute key on the element to value.

The following methods work on the element’s children (subelements).

append(subelement)

Adds the element subelement to the end of this element’s internal list of subelements. Raises TypeError if subelement is not an Element.

extend(subelements)

Appends subelements from a sequence object with zero or more elements. Raises TypeError if a subelement is not an Element.

3.2 新版功能.

find(match, namespaces=None)

Finds the first subelement matching match. match may be a tag name or a path. Returns an element instance or None. namespaces is an optional mapping from namespace prefix to full name.

findall(match, namespaces=None)

Finds all matching subelements, by tag name or path. Returns a list containing all matching elements in document order. namespaces is an optional mapping from namespace prefix to full name.

findtext(match, default=None, namespaces=None)

Finds text for the first subelement matching match. match may be a tag name or a path. Returns the text content of the first matching element, or default if no element was found. Note that if the matching element has no text content an empty string is returned. namespaces is an optional mapping from namespace prefix to full name.

getchildren()

3.2 版后已移除: Use list(elem) or iteration.

getiterator(tag=None)

3.2 版后已移除: Use method Element.iter() instead.

insert(index, subelement)

Inserts subelement at the given position in this element. Raises TypeError if subelement is not an Element.

iter(tag=None)

Creates a tree iterator with the current element as the root. The iterator iterates over this element and all elements below it, in document (depth first) order. If tag is not None or '*', only elements whose tag equals tag are returned from the iterator. If the tree structure is modified during iteration, the result is undefined.

3.2 新版功能.

iterfind(match, namespaces=None)

Finds all matching subelements, by tag name or path. Returns an iterable yielding all matching elements in document order. namespaces is an optional mapping from namespace prefix to full name.

3.2 新版功能.

itertext()

Creates a text iterator. The iterator loops over this element and all subelements, in document order, and returns all inner text.

3.2 新版功能.

makeelement(tag, attrib)

Creates a new element object of the same type as this element. Do not call this method, use the SubElement() factory function instead.

remove(subelement)

Removes subelement from the element. Unlike the find* methods this method compares elements based on the instance identity, not on tag value or contents.

Element objects also support the following sequence type methods for working with subelements: __delitem__(), __getitem__(), __setitem__(), __len__().

Caution: Elements with no subelements will test as False. This behavior will change in future versions. Use specific len(elem) or elem is None test instead.

element = root.find('foo')

if not element:  # careful!
    print("element not found, or element has no subelements")

if element is None:
    print("element not found")

20.5.3.3. ElementTree 对象

class xml.etree.ElementTree.ElementTree(element=None, file=None)

ElementTree wrapper class. This class represents an entire element hierarchy, and adds some extra support for serialization to and from standard XML.

element is the root element. The tree is initialized with the contents of the XML file if given.

_setroot(element)

Replaces the root element for this tree. This discards the current contents of the tree, and replaces it with the given element. Use with care. element is an element instance.

find(match, namespaces=None)

Same as Element.find(), starting at the root of the tree.

findall(match, namespaces=None)

Same as Element.findall(), starting at the root of the tree.

findtext(match, default=None, namespaces=None)

Same as Element.findtext(), starting at the root of the tree.

getiterator(tag=None)

3.2 版后已移除: Use method ElementTree.iter() instead.

getroot()

Returns the root element for this tree.

iter(tag=None)

Creates and returns a tree iterator for the root element. The iterator loops over all elements in this tree, in section order. tag is the tag to look for (default is to return all elements).

iterfind(match, namespaces=None)

Same as Element.iterfind(), starting at the root of the tree.

3.2 新版功能.

parse(source, parser=None)

Loads an external XML section into this element tree. source is a file name or file object. parser is an optional parser instance. If not given, the standard XMLParser parser is used. Returns the section root element.

write(file, encoding="us-ascii", xml_declaration=None, default_namespace=None, method="xml", *, short_empty_elements=True)

Writes the element tree to a file, as XML. file is a file name, or a file object opened for writing. encoding 1 is the output encoding (default is US-ASCII). xml_declaration controls if an XML declaration should be added to the file. Use False for never, True for always, None for only if not US-ASCII or UTF-8 or Unicode (default is None). default_namespace sets the default XML namespace (for “xmlns”). method is either "xml", "html" or "text" (default is "xml"). The keyword-only short_empty_elements parameter controls the formatting of elements that contain no content. If True (the default), they are emitted as a single self-closed tag, otherwise they are emitted as a pair of start/end tags.

The output is either a string (str) or binary (bytes). This is controlled by the encoding argument. If encoding is "unicode", the output is a string; otherwise, it’s binary. Note that this may conflict with the type of file if it’s an open file object; make sure you do not try to write a string to a binary stream and vice versa.

3.4 新版功能: short_empty_elements 形参。

This is the XML file that is going to be manipulated:

<html>
    <head>
        <title>Example page</title>
    </head>
    <body>
        <p>Moved to <a href="http://example.org/">example.org</a>
        or <a href="http://example.com/">example.com</a>.</p>
    </body>
</html>

Example of changing the attribute “target” of every link in first paragraph:

>>> from xml.etree.ElementTree import ElementTree
>>> tree = ElementTree()
>>> tree.parse("index.xhtml")
<Element 'html' at 0xb77e6fac>
>>> p = tree.find("body/p")     # Finds first occurrence of tag p in body
>>> p
<Element 'p' at 0xb77ec26c>
>>> links = list(p.iter("a"))   # Returns list of all links
>>> links
[<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>]
>>> for i in links:             # Iterates through all found links
...     i.attrib["target"] = "blank"
>>> tree.write("output.xhtml")

20.5.3.4. QName Objects

class xml.etree.ElementTree.QName(text_or_uri, tag=None)

QName wrapper. This can be used to wrap a QName attribute value, in order to get proper namespace handling on output. text_or_uri is a string containing the QName value, in the form {uri}local, or, if the tag argument is given, the URI part of a QName. If tag is given, the first argument is interpreted as a URI, and this argument is interpreted as a local name. QName instances are opaque.

20.5.3.5. TreeBuilder Objects

class xml.etree.ElementTree.TreeBuilder(element_factory=None)

Generic element structure builder. This builder converts a sequence of start, data, and end method calls to a well-formed element structure. You can use this class to build an element structure using a custom XML parser, or a parser for some other XML-like format. element_factory, when given, must be a callable accepting two positional arguments: a tag and a dict of attributes. It is expected to return a new element instance.

close()

Flushes the builder buffers, and returns the toplevel document element. Returns an Element instance.

data(data)

Adds text to the current element. data is a string. This should be either a bytestring, or a Unicode string.

end(tag)

Closes the current element. tag is the element name. Returns the closed element.

start(tag, attrs)

Opens a new element. tag is the element name. attrs is a dictionary containing element attributes. Returns the opened element.

In addition, a custom TreeBuilder object can provide the following method:

doctype(name, pubid, system)

Handles a doctype declaration. name is the doctype name. pubid is the public identifier. system is the system identifier. This method does not exist on the default TreeBuilder class.

3.2 新版功能.

20.5.3.6. XMLParser对象

class xml.etree.ElementTree.XMLParser(html=0, target=None, encoding=None)

This class is the low-level building block of the module. It uses xml.parsers.expat for efficient, event-based parsing of XML. It can be fed XML data incrementally with the feed() method, and parsing events are translated to a push API - by invoking callbacks on the target object. If target is omitted, the standard TreeBuilder is used. The html argument was historically used for backwards compatibility and is now deprecated. If encoding 1 is given, the value overrides the encoding specified in the XML file.

3.4 版后已移除: The html argument. The remaining arguments should be passed via keyword to prepare for the removal of the html argument.

close()

Finishes feeding data to the parser. Returns the result of calling the close() method of the target passed during construction; by default, this is the toplevel document element.

doctype(name, pubid, system)

3.2 版后已移除: Define the TreeBuilder.doctype() method on a custom TreeBuilder target.

feed(data)

Feeds data to the parser. data is encoded data.

XMLParser.feed() calls target’s start(tag, attrs_dict) method for each opening tag, its end(tag) method for each closing tag, and data is processed by method data(data). XMLParser.close() calls target’s method close(). XMLParser can be used not only for building a tree structure. This is an example of counting the maximum depth of an XML file:

>>> from xml.etree.ElementTree import XMLParser
>>> class MaxDepth:                     # The target object of the parser
...     maxDepth = 0
...     depth = 0
...     def start(self, tag, attrib):   # Called for each opening tag.
...         self.depth += 1
...         if self.depth > self.maxDepth:
...             self.maxDepth = self.depth
...     def end(self, tag):             # Called for each closing tag.
...         self.depth -= 1
...     def data(self, data):
...         pass            # We do not need to do anything with data.
...     def close(self):    # Called when all data has been parsed.
...         return self.maxDepth
...
>>> target = MaxDepth()
>>> parser = XMLParser(target=target)
>>> exampleXml = """
... <a>
...   <b>
...   </b>
...   <b>
...     <c>
...       <d>
...       </d>
...     </c>
...   </b>
... </a>"""
>>> parser.feed(exampleXml)
>>> parser.close()
4

20.5.3.7. XMLPullParser对象

class xml.etree.ElementTree.XMLPullParser(events=None)

A pull parser suitable for non-blocking applications. Its input-side API is similar to that of XMLParser, but instead of pushing calls to a callback target, XMLPullParser collects an internal list of parsing events and lets the user read from it. events is a sequence of events to report back. The supported events are the strings "start", "end", "start-ns" and "end-ns" (the “ns” events are used to get detailed namespace information). If events is omitted, only "end" events are reported.

feed(data)

Feed the given bytes data to the parser.

close()

Signal the parser that the data stream is terminated. Unlike XMLParser.close(), this method always returns None. Any events not yet retrieved when the parser is closed can still be read with read_events().

read_events()

Return an iterator over the events which have been encountered in the data fed to the parser. The iterator yields (event, elem) pairs, where event is a string representing the type of event (e.g. "end") and elem is the encountered Element object.

Events provided in a previous call to read_events() will not be yielded again. Events are consumed from the internal queue only when they are retrieved from the iterator, so multiple readers iterating in parallel over iterators obtained from read_events() will have unpredictable results.

注解

XMLPullParser only guarantees that it has seen the “>” character of a starting tag when it emits a “start” event, so the attributes are defined, but the contents of the text and tail attributes are undefined at that point. The same applies to the element children; they may or may not be present.

If you need a fully populated element, look for “end” events instead.

3.4 新版功能.

20.5.3.8. 异常

class xml.etree.ElementTree.ParseError

XML parse error, raised by the various parsing methods in this module when parsing fails. The string representation of an instance of this exception will contain a user-friendly error message. In addition, it will have the following attributes available:

code

A numeric error code from the expat parser. See the documentation of xml.parsers.expat for the list of error codes and their meanings.

position

A tuple of line, column numbers, specifying where the error occurred.

备注

1(1,2,3,4)

The encoding string included in XML output should conform to the appropriate standards. For example, “UTF-8” is valid, but “UTF8” is not. See https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl and https://www.iana.org/assignments/character-sets/character-sets.xhtml.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

20.6. xml.dom — The Document Object Model API

Source code: Lib/xml/dom/__init__.py [https://github.com/python/cpython/tree/3.6/Lib/xml/dom/__init__.py]

The Document Object Model, or “DOM,” is a cross-language API from the World Wide Web Consortium (W3C) for accessing and modifying XML documents. A DOM implementation presents an XML document as a tree structure, or allows client code to build such a structure from scratch. It then gives access to the structure through a set of objects which provided well-known interfaces.

The DOM is extremely useful for random-access applications. SAX only allows you a view of one bit of the document at a time. If you are looking at one SAX element, you have no access to another. If you are looking at a text node, you have no access to a containing element. When you write a SAX application, you need to keep track of your program’s position in the document somewhere in your own code. SAX does not do it for you. Also, if you need to look ahead in the XML document, you are just out of luck.

Some applications are simply impossible in an event driven model with no access to a tree. Of course you could build some sort of tree yourself in SAX events, but the DOM allows you to avoid writing that code. The DOM is a standard tree representation for XML data.

The Document Object Model is being defined by the W3C in stages, or “levels” in their terminology. The Python mapping of the API is substantially based on the DOM Level 2 recommendation.

DOM applications typically start by parsing some XML into a DOM. How this is accomplished is not covered at all by DOM Level 1, and Level 2 provides only limited improvements: There is a DOMImplementation object class which provides access to Document creation methods, but no way to access an XML reader/parser/Document builder in an implementation-independent way. There is also no well-defined way to access these methods without an existing Document object. In Python, each DOM implementation will provide a function getDOMImplementation(). DOM Level 3 adds a Load/Store specification, which defines an interface to the reader, but this is not yet available in the Python standard library.

Once you have a DOM document object, you can access the parts of your XML document through its properties and methods. These properties are defined in the DOM specification; this portion of the reference manual describes the interpretation of the specification in Python.

The specification provided by the W3C defines the DOM API for Java, ECMAScript, and OMG IDL. The Python mapping defined here is based in large part on the IDL version of the specification, but strict compliance is not required (though implementations are free to support the strict mapping from IDL). See section 一致性 for a detailed discussion of mapping requirements.

参见

Document Object Model (DOM) Level 2 Specification [https://www.w3.org/TR/DOM-Level-2-Core/]

The W3C recommendation upon which the Python DOM API is based.

Document Object Model (DOM) Level 1 Specification [https://www.w3.org/TR/REC-DOM-Level-1/]

The W3C recommendation for the DOM supported by xml.dom.minidom.

Python Language Mapping Specification [http://www.omg.org/spec/PYTH/1.2/PDF]

This specifies the mapping from OMG IDL to Python.

20.6.1. 模块内容

The xml.dom contains the following functions:

xml.dom.registerDOMImplementation(name, factory)

Register the factory function with the name name. The factory function should return an object which implements the DOMImplementation interface. The factory function can return the same object every time, or a new one for each call, as appropriate for the specific implementation (e.g. if that implementation supports some customization).

xml.dom.getDOMImplementation(name=None, features=())

Return a suitable DOM implementation. The name is either well-known, the module name of a DOM implementation, or None. If it is not None, imports the corresponding module and returns a DOMImplementation object if the import succeeds. If no name is given, and if the environment variable PYTHON_DOM is set, this variable is used to find the implementation.

If name is not given, this examines the available implementations to find one with the required feature set. If no implementation can be found, raise an ImportError. The features list must be a sequence of (feature, version) pairs which are passed to the hasFeature() method on available DOMImplementation objects.

Some convenience constants are also provided:

xml.dom.EMPTY_NAMESPACE

The value used to indicate that no namespace is associated with a node in the DOM. This is typically found as the namespaceURI of a node, or used as the namespaceURI parameter to a namespaces-specific method.

xml.dom.XML_NAMESPACE

The namespace URI associated with the reserved prefix xml, as defined by Namespaces in XML [https://www.w3.org/TR/REC-xml-names/] (section 4).

xml.dom.XMLNS_NAMESPACE

The namespace URI for namespace declarations, as defined by Document Object Model (DOM) Level 2 Core Specification [https://www.w3.org/TR/DOM-Level-2-Core/core.html] (section 1.1.8).

xml.dom.XHTML_NAMESPACE

The URI of the XHTML namespace as defined by XHTML 1.0: The Extensible HyperText Markup Language [https://www.w3.org/TR/xhtml1/] (section 3.1.1).

In addition, xml.dom contains a base Node class and the DOM exception classes. The Node class provided by this module does not implement any of the methods or attributes defined by the DOM specification; concrete DOM implementations must provide those. The Node class provided as part of this module does provide the constants used for the nodeType attribute on concrete Node objects; they are located within the class rather than at the module level to conform with the DOM specifications.

20.6.2. Objects in the DOM

DOM的权威文档是来自W3C的DOM规范。

请注意，DOM属性也可以作为节点而不是简单的字符串进行操作。然而，必须这样做的情况相当少见，所以这种用法还没有记录下来。

接口

部件

目的

DOMImplementation

DOMImplementation Objects

底层实现的接口。

Node

节点对象

文档中大多数对象的基本接口。

NodeList

节点列表对象

节点序列的接口。

DocumentType

文档类型对象

有关处理文档所需声明的信息。

Document

文档对象

表示整个文档的对象。

Element

元素对象

文档层次结构中的元素节点。

Attr

Attr 对象

元素节点上的属性值节点。

Comment

注释对象

源文档中注释的表示形式。

Text

Text 和 CDATASection 对象

包含文档中文本内容的节点。

ProcessingInstruction

ProcessingInstruction 对象

Processing instruction representation.

另一节描述了在Python中使用DOM定义的异常。

20.6.2.1. DOMImplementation Objects

The DOMImplementation interface provides a way for applications to determine the availability of particular features in the DOM they are using. DOM Level 2 added the ability to create new Document and DocumentType objects using the DOMImplementation as well.

DOMImplementation.hasFeature(feature, version)

Return true if the feature identified by the pair of strings feature and version is implemented.

DOMImplementation.createDocument(namespaceUri, qualifiedName, doctype)

Return a new Document object (the root of the DOM), with a child Element object having the given namespaceUri and qualifiedName. The doctype must be a DocumentType object created by createDocumentType(), or None. In the Python DOM API, the first two arguments can also be None in order to indicate that no Element child is to be created.

DOMImplementation.createDocumentType(qualifiedName, publicId, systemId)

Return a new DocumentType object that encapsulates the given qualifiedName, publicId, and systemId strings, representing the information contained in an XML document type declaration.

20.6.2.2. 节点对象

All of the components of an XML document are subclasses of Node.

Node.nodeType

An integer representing the node type. Symbolic constants for the types are on the Node object: ELEMENT_NODE, ATTRIBUTE_NODE, TEXT_NODE, CDATA_SECTION_NODE, ENTITY_NODE, PROCESSING_INSTRUCTION_NODE, COMMENT_NODE, DOCUMENT_NODE, DOCUMENT_TYPE_NODE, NOTATION_NODE. This is a read-only attribute.

Node.parentNode

The parent of the current node, or None for the document node. The value is always a Node object or None. For Element nodes, this will be the parent element, except for the root element, in which case it will be the Document object. For Attr nodes, this is always None. This is a read-only attribute.

Node.attributes

A NamedNodeMap of attribute objects. Only elements have actual values for this; others provide None for this attribute. This is a read-only attribute.

Node.previousSibling

The node that immediately precedes this one with the same parent. For instance the element with an end-tag that comes just before the self element’s start-tag. Of course, XML documents are made up of more than just elements so the previous sibling could be text, a comment, or something else. If this node is the first child of the parent, this attribute will be None. This is a read-only attribute.

Node.nextSibling

The node that immediately follows this one with the same parent. See also previousSibling. If this is the last child of the parent, this attribute will be None. This is a read-only attribute.

Node.childNodes

A list of nodes contained within this node. This is a read-only attribute.

Node.firstChild

The first child of the node, if there are any, or None. This is a read-only attribute.

Node.lastChild

The last child of the node, if there are any, or None. This is a read-only attribute.

Node.localName

The part of the tagName following the colon if there is one, else the entire tagName. The value is a string.

Node.prefix

The part of the tagName preceding the colon if there is one, else the empty string. The value is a string, or None.

Node.namespaceURI

The namespace associated with the element name. This will be a string or None. This is a read-only attribute.

Node.nodeName

This has a different meaning for each node type; see the DOM specification for details. You can always get the information you would get here from another property such as the tagName property for elements or the name property for attributes. For all node types, the value of this attribute will be either a string or None. This is a read-only attribute.

Node.nodeValue

This has a different meaning for each node type; see the DOM specification for details. The situation is similar to that with nodeName. The value is a string or None.

Node.hasAttributes()

Returns true if the node has any attributes.

Node.hasChildNodes()

Returns true if the node has any child nodes.

Node.isSameNode(other)

Returns true if other refers to the same node as this node. This is especially useful for DOM implementations which use any sort of proxy architecture (because more than one object can refer to the same node).

注解

This is based on a proposed DOM Level 3 API which is still in the “working draft” stage, but this particular interface appears uncontroversial. Changes from the W3C will not necessarily affect this method in the Python DOM interface (though any new W3C API for this would also be supported).

Node.appendChild(newChild)

Add a new child node to this node at the end of the list of children, returning newChild. If the node was already in the tree, it is removed first.

Node.insertBefore(newChild, refChild)

Insert a new child node before an existing child. It must be the case that refChild is a child of this node; if not, ValueError is raised. newChild is returned. If refChild is None, it inserts newChild at the end of the children’s list.

Node.removeChild(oldChild)

Remove a child node. oldChild must be a child of this node; if not, ValueError is raised. oldChild is returned on success. If oldChild will not be used further, its unlink() method should be called.

Node.replaceChild(newChild, oldChild)

Replace an existing node with a new node. It must be the case that oldChild is a child of this node; if not, ValueError is raised.

Node.normalize()

Join adjacent text nodes so that all stretches of text are stored as single Text instances. This simplifies processing text from a DOM tree for many applications.

Node.cloneNode(deep)

Clone this node. Setting deep means to clone all child nodes as well. This returns the clone.

20.6.2.3. 节点列表对象

A NodeList represents a sequence of nodes. These objects are used in two ways in the DOM Core recommendation: an Element object provides one as its list of child nodes, and the getElementsByTagName() and getElementsByTagNameNS() methods of Node return objects with this interface to represent query results.

The DOM Level 2 recommendation defines one method and one attribute for these objects:

NodeList.item(i)

Return the i’th item from the sequence, if there is one, or None. The index i is not allowed to be less than zero or greater than or equal to the length of the sequence.

NodeList.length

The number of nodes in the sequence.

In addition, the Python DOM interface requires that some additional support is provided to allow NodeList objects to be used as Python sequences. All NodeList implementations must include support for __len__() and __getitem__(); this allows iteration over the NodeList in for statements and proper support for the len() built-in function.

If a DOM implementation supports modification of the document, the NodeList implementation must also support the __setitem__() and __delitem__() methods.

20.6.2.4. 文档类型对象

Information about the notations and entities declared by a document (including the external subset if the parser uses it and can provide the information) is available from a DocumentType object. The DocumentType for a document is available from the Document object’s doctype attribute; if there is no DOCTYPE declaration for the document, the document’s doctype attribute will be set to None instead of an instance of this interface.

DocumentType is a specialization of Node, and adds the following attributes:

DocumentType.publicId

The public identifier for the external subset of the document type definition. This will be a string or None.

DocumentType.systemId

The system identifier for the external subset of the document type definition. This will be a URI as a string, or None.

DocumentType.internalSubset

A string giving the complete internal subset from the document. This does not include the brackets which enclose the subset. If the document has no internal subset, this should be None.

DocumentType.name

The name of the root element as given in the DOCTYPE declaration, if present.

DocumentType.entities

This is a NamedNodeMap giving the definitions of external entities. For entity names defined more than once, only the first definition is provided (others are ignored as required by the XML recommendation). This may be None if the information is not provided by the parser, or if no entities are defined.

DocumentType.notations

This is a NamedNodeMap giving the definitions of notations. For notation names defined more than once, only the first definition is provided (others are ignored as required by the XML recommendation). This may be None if the information is not provided by the parser, or if no notations are defined.

20.6.2.5. 文档对象

A Document represents an entire XML document, including its constituent elements, attributes, processing instructions, comments etc. Remember that it inherits properties from Node.

Document.documentElement

The one and only root element of the document.

Document.createElement(tagName)

Create and return a new element node. The element is not inserted into the document when it is created. You need to explicitly insert it with one of the other methods such as insertBefore() or appendChild().

Document.createElementNS(namespaceURI, tagName)

Create and return a new element with a namespace. The tagName may have a prefix. The element is not inserted into the document when it is created. You need to explicitly insert it with one of the other methods such as insertBefore() or appendChild().

Document.createTextNode(data)

Create and return a text node containing the data passed as a parameter. As with the other creation methods, this one does not insert the node into the tree.

Document.createComment(data)

Create and return a comment node containing the data passed as a parameter. As with the other creation methods, this one does not insert the node into the tree.

Document.createProcessingInstruction(target, data)

Create and return a processing instruction node containing the target and data passed as parameters. As with the other creation methods, this one does not insert the node into the tree.

Document.createAttribute(name)

Create and return an attribute node. This method does not associate the attribute node with any particular element. You must use setAttributeNode() on the appropriate Element object to use the newly created attribute instance.

Document.createAttributeNS(namespaceURI, qualifiedName)

Create and return an attribute node with a namespace. The tagName may have a prefix. This method does not associate the attribute node with any particular element. You must use setAttributeNode() on the appropriate Element object to use the newly created attribute instance.

Document.getElementsByTagName(tagName)

Search for all descendants (direct children, children’s children, etc.) with a particular element type name.

Document.getElementsByTagNameNS(namespaceURI, localName)

Search for all descendants (direct children, children’s children, etc.) with a particular namespace URI and localname. The localname is the part of the namespace after the prefix.

20.6.2.6. 元素对象

Element is a subclass of Node, so inherits all the attributes of that class.

Element.tagName

The element type name. In a namespace-using document it may have colons in it. The value is a string.

Element.getElementsByTagName(tagName)

Same as equivalent method in the Document class.

Element.getElementsByTagNameNS(namespaceURI, localName)

Same as equivalent method in the Document class.

Element.hasAttribute(name)

Returns true if the element has an attribute named by name.

Element.hasAttributeNS(namespaceURI, localName)

Returns true if the element has an attribute named by namespaceURI and localName.

Element.getAttribute(name)

Return the value of the attribute named by name as a string. If no such attribute exists, an empty string is returned, as if the attribute had no value.

Element.getAttributeNode(attrname)

Return the Attr node for the attribute named by attrname.

Element.getAttributeNS(namespaceURI, localName)

Return the value of the attribute named by namespaceURI and localName as a string. If no such attribute exists, an empty string is returned, as if the attribute had no value.

Element.getAttributeNodeNS(namespaceURI, localName)

Return an attribute value as a node, given a namespaceURI and localName.

Element.removeAttribute(name)

Remove an attribute by name. If there is no matching attribute, a NotFoundErr is raised.

Element.removeAttributeNode(oldAttr)

Remove and return oldAttr from the attribute list, if present. If oldAttr is not present, NotFoundErr is raised.

Element.removeAttributeNS(namespaceURI, localName)

Remove an attribute by name. Note that it uses a localName, not a qname. No exception is raised if there is no matching attribute.

Element.setAttribute(name, value)

Set an attribute value from a string.

Element.setAttributeNode(newAttr)

Add a new attribute node to the element, replacing an existing attribute if necessary if the name attribute matches. If a replacement occurs, the old attribute node will be returned. If newAttr is already in use, InuseAttributeErr will be raised.

Element.setAttributeNodeNS(newAttr)

Add a new attribute node to the element, replacing an existing attribute if necessary if the namespaceURI and localName attributes match. If a replacement occurs, the old attribute node will be returned. If newAttr is already in use, InuseAttributeErr will be raised.

Element.setAttributeNS(namespaceURI, qname, value)

Set an attribute value from a string, given a namespaceURI and a qname. Note that a qname is the whole attribute name. This is different than above.

20.6.2.7. Attr 对象

Attr inherits from Node, so inherits all its attributes.

Attr.name

The attribute name. In a namespace-using document it may include a colon.

Attr.localName

The part of the name following the colon if there is one, else the entire name. This is a read-only attribute.

Attr.prefix

The part of the name preceding the colon if there is one, else the empty string.

Attr.value

The text value of the attribute. This is a synonym for the nodeValue attribute.

20.6.2.8. NamedNodeMap 对象

NamedNodeMap does not inherit from Node.

NamedNodeMap.length

The length of the attribute list.

NamedNodeMap.item(index)

Return an attribute with a particular index. The order you get the attributes in is arbitrary but will be consistent for the life of a DOM. Each item is an attribute node. Get its value with the value attribute.

There are also experimental methods that give this class more mapping behavior. You can use them or you can use the standardized getAttribute*() family of methods on the Element objects.

20.6.2.9. 注释对象

Comment represents a comment in the XML document. It is a subclass of Node, but cannot have child nodes.

Comment.data

The content of the comment as a string. The attribute contains all characters between the leading <!-- and trailing -->, but does not include them.

20.6.2.10. Text 和 CDATASection 对象

The Text interface represents text in the XML document. If the parser and DOM implementation support the DOM’s XML extension, portions of the text enclosed in CDATA marked sections are stored in CDATASection objects. These two interfaces are identical, but provide different values for the nodeType attribute.

These interfaces extend the Node interface. They cannot have child nodes.

Text.data

The content of the text node as a string.

注解

The use of a CDATASection node does not indicate that the node represents a complete CDATA marked section, only that the content of the node was part of a CDATA section. A single CDATA section may be represented by more than one node in the document tree. There is no way to determine whether two adjacent CDATASection nodes represent different CDATA marked sections.

20.6.2.11. ProcessingInstruction 对象

Represents a processing instruction in the XML document; this inherits from the Node interface and cannot have child nodes.

ProcessingInstruction.target

The content of the processing instruction up to the first whitespace character. This is a read-only attribute.

ProcessingInstruction.data

The content of the processing instruction following the first whitespace character.

20.6.2.12. 异常

The DOM Level 2 recommendation defines a single exception, DOMException, and a number of constants that allow applications to determine what sort of error occurred. DOMException instances carry a code attribute that provides the appropriate value for the specific exception.

The Python DOM interface provides the constants, but also expands the set of exceptions so that a specific exception exists for each of the exception codes defined by the DOM. The implementations must raise the appropriate specific exception, each of which carries the appropriate value for the code attribute.

exception xml.dom.DOMException

Base exception class used for all specific DOM exceptions. This exception class cannot be directly instantiated.

exception xml.dom.DomstringSizeErr

Raised when a specified range of text does not fit into a string. This is not known to be used in the Python DOM implementations, but may be received from DOM implementations not written in Python.

exception xml.dom.HierarchyRequestErr

Raised when an attempt is made to insert a node where the node type is not allowed.

exception xml.dom.IndexSizeErr

Raised when an index or size parameter to a method is negative or exceeds the allowed values.

exception xml.dom.InuseAttributeErr

Raised when an attempt is made to insert an Attr node that is already present elsewhere in the document.

exception xml.dom.InvalidAccessErr

Raised if a parameter or an operation is not supported on the underlying object.

exception xml.dom.InvalidCharacterErr

This exception is raised when a string parameter contains a character that is not permitted in the context it’s being used in by the XML 1.0 recommendation. For example, attempting to create an Element node with a space in the element type name will cause this error to be raised.

exception xml.dom.InvalidModificationErr

Raised when an attempt is made to modify the type of a node.

exception xml.dom.InvalidStateErr

Raised when an attempt is made to use an object that is not defined or is no longer usable.

exception xml.dom.NamespaceErr

If an attempt is made to change any object in a way that is not permitted with regard to the Namespaces in XML [https://www.w3.org/TR/REC-xml-names/] recommendation, this exception is raised.

exception xml.dom.NotFoundErr

Exception when a node does not exist in the referenced context. For example, NamedNodeMap.removeNamedItem() will raise this if the node passed in does not exist in the map.

exception xml.dom.NotSupportedErr

Raised when the implementation does not support the requested type of object or operation.

exception xml.dom.NoDataAllowedErr

This is raised if data is specified for a node which does not support data.

exception xml.dom.NoModificationAllowedErr

Raised on attempts to modify an object where modifications are not allowed (such as for read-only nodes).

exception xml.dom.SyntaxErr

Raised when an invalid or illegal string is specified.

exception xml.dom.WrongDocumentErr

Raised when a node is inserted in a different document than it currently belongs to, and the implementation does not support migrating the node from one document to the other.

The exception codes defined in the DOM recommendation map to the exceptions described above according to this table:

常数

异常

DOMSTRING_SIZE_ERR

DomstringSizeErr

HIERARCHY_REQUEST_ERR

HierarchyRequestErr

INDEX_SIZE_ERR

IndexSizeErr

INUSE_ATTRIBUTE_ERR

InuseAttributeErr

INVALID_ACCESS_ERR

InvalidAccessErr

INVALID_CHARACTER_ERR

InvalidCharacterErr

INVALID_MODIFICATION_ERR

InvalidModificationErr

INVALID_STATE_ERR

InvalidStateErr

NAMESPACE_ERR

NamespaceErr

NOT_FOUND_ERR

NotFoundErr

NOT_SUPPORTED_ERR

NotSupportedErr

NO_DATA_ALLOWED_ERR

NoDataAllowedErr

NO_MODIFICATION_ALLOWED_ERR

NoModificationAllowedErr

SYNTAX_ERR

SyntaxErr

WRONG_DOCUMENT_ERR

WrongDocumentErr

20.6.3. 一致性

This section describes the conformance requirements and relationships between the Python DOM API, the W3C DOM recommendations, and the OMG IDL mapping for Python.

20.6.3.1. 类型映射

将根据下表，将DOM规范中使用的IDL类型映射为Python类型。

IDL 类型

Python 类型

boolean

bool 或 int

int

int

long int

int

unsigned int

int

DOMString

str 或 bytes

null

None

20.6.3.2. Accessor Methods

The mapping from OMG IDL to Python defines accessor functions for IDL attribute declarations in much the way the Java mapping does. Mapping the IDL declarations

readonly attribute string someValue;
         attribute string anotherValue;

yields three accessor functions: a “get” method for someValue (_get_someValue()), and “get” and “set” methods for anotherValue (_get_anotherValue() and _set_anotherValue()). The mapping, in particular, does not require that the IDL attributes are accessible as normal Python attributes: object.someValue is not required to work, and may raise an AttributeError.

The Python DOM API, however, does require that normal attribute access work. This means that the typical surrogates generated by Python IDL compilers are not likely to work, and wrapper objects may be needed on the client if the DOM objects are accessed via CORBA. While this does require some additional consideration for CORBA DOM clients, the implementers with experience using DOM over CORBA from Python do not consider this a problem. Attributes that are declared readonly may not restrict write access in all DOM implementations.

In the Python DOM API, accessor functions are not required. If provided, they should take the form defined by the Python IDL mapping, but these methods are considered unnecessary since the attributes are accessible directly from Python. “Set” accessors should never be provided for readonly attributes.

The IDL definitions do not fully embody the requirements of the W3C DOM API, such as the notion of certain objects, such as the return value of getElementsByTagName(), being “live”. The Python DOM API does not require implementations to enforce such requirements.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

20.7. xml.dom.minidom — Minimal DOM implementation

Source code: Lib/xml/dom/minidom.py [https://github.com/python/cpython/tree/3.6/Lib/xml/dom/minidom.py]

xml.dom.minidom is a minimal implementation of the Document Object Model interface, with an API similar to that in other languages. It is intended to be simpler than the full DOM and also significantly smaller. Users who are not already proficient with the DOM should consider using the xml.etree.ElementTree module for their XML processing instead.

警告

The xml.dom.minidom module is not secure against maliciously constructed data. If you need to parse untrusted or unauthenticated data see XML 漏洞.

DOM applications typically start by parsing some XML into a DOM. With xml.dom.minidom, this is done through the parse functions:

from xml.dom.minidom import parse, parseString

dom1 = parse('c:\\temp\\mydata.xml')  # parse an XML file by name

datasource = open('c:\\temp\\mydata.xml')
dom2 = parse(datasource)  # parse an open file

dom3 = parseString('<myxml>Some data<empty/> some more data</myxml>')

The parse() function can take either a filename or an open file object.

xml.dom.minidom.parse(filename_or_file, parser=None, bufsize=None)

Return a Document from the given input. filename_or_file may be either a file name, or a file-like object. parser, if given, must be a SAX2 parser object. This function will change the document handler of the parser and activate namespace support; other parser configuration (like setting an entity resolver) must have been done in advance.

If you have XML in a string, you can use the parseString() function instead:

xml.dom.minidom.parseString(string, parser=None)

Return a Document that represents the string. This method creates an io.StringIO object for the string and passes that on to parse().

Both functions return a Document object representing the content of the document.

What the parse() and parseString() functions do is connect an XML parser with a “DOM builder” that can accept parse events from any SAX parser and convert them into a DOM tree. The name of the functions are perhaps misleading, but are easy to grasp when learning the interfaces. The parsing of the document will be completed before these functions return; it’s simply that these functions do not provide a parser implementation themselves.

You can also create a Document by calling a method on a “DOM Implementation” object. You can get this object either by calling the getDOMImplementation() function in the xml.dom package or the xml.dom.minidom module. Once you have a Document, you can add child nodes to it to populate the DOM:

from xml.dom.minidom import getDOMImplementation

impl = getDOMImplementation()

newdoc = impl.createDocument(None, "some_tag", None)
top_element = newdoc.documentElement
text = newdoc.createTextNode('Some textual content.')
top_element.appendChild(text)

Once you have a DOM document object, you can access the parts of your XML document through its properties and methods. These properties are defined in the DOM specification. The main property of the document object is the documentElement property. It gives you the main element in the XML document: the one that holds all others. Here is an example program:

dom3 = parseString("<myxml>Some data</myxml>")
assert dom3.documentElement.tagName == "myxml"

When you are finished with a DOM tree, you may optionally call the unlink() method to encourage early cleanup of the now-unneeded objects. unlink() is an xml.dom.minidom-specific extension to the DOM API that renders the node and its descendants are essentially useless. Otherwise, Python’s garbage collector will eventually take care of the objects in the tree.

参见

Document Object Model (DOM) Level 1 Specification [https://www.w3.org/TR/REC-DOM-Level-1/]

The W3C recommendation for the DOM supported by xml.dom.minidom.

20.7.1. DOM Objects

The definition of the DOM API for Python is given as part of the xml.dom module documentation. This section lists the differences between the API and xml.dom.minidom.

Node.unlink()

Break internal references within the DOM so that it will be garbage collected on versions of Python without cyclic GC. Even when cyclic GC is available, using this can make large amounts of memory available sooner, so calling this on DOM objects as soon as they are no longer needed is good practice. This only needs to be called on the Document object, but may be called on child nodes to discard children of that node.

You can avoid calling this method explicitly by using the with statement. The following code will automatically unlink dom when the with block is exited:

with xml.dom.minidom.parse(datasource) as dom:
    ... # Work with dom.

Node.writexml(writer, indent="", addindent="", newl="")

Write XML to the writer object. The writer should have a write() method which matches that of the file object interface. The indent parameter is the indentation of the current node. The addindent parameter is the incremental indentation to use for subnodes of the current one. The newl parameter specifies the string to use to terminate newlines.

For the Document node, an additional keyword argument encoding can be used to specify the encoding field of the XML header.

Node.toxml(encoding=None)

Return a string or byte string containing the XML represented by the DOM node.

With an explicit encoding 1 argument, the result is a byte string in the specified encoding. With no encoding argument, the result is a Unicode string, and the XML declaration in the resulting string does not specify an encoding. Encoding this string in an encoding other than UTF-8 is likely incorrect, since UTF-8 is the default encoding of XML.

Node.toprettyxml(indent="\t", newl="\n", encoding=None)

Return a pretty-printed version of the document. indent specifies the indentation string and defaults to a tabulator; newl specifies the string emitted at the end of each line and defaults to \n.

The encoding argument behaves like the corresponding argument of toxml().

20.7.2. DOM Example

This example program is a fairly realistic example of a simple program. In this particular case, we do not take much advantage of the flexibility of the DOM.

import xml.dom.minidom

document = """\
<slideshow>
<title>Demo slideshow</title>
<slide><title>Slide title</title>
<point>This is a demo</point>
<point>Of a program for processing slides</point>
</slide>

<slide><title>Another demo slide</title>
<point>It is important</point>
<point>To have more than</point>
<point>one slide</point>
</slide>
</slideshow>
"""

dom = xml.dom.minidom.parseString(document)

def getText(nodelist):
    rc = []
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc.append(node.data)
    return ''.join(rc)

def handleSlideshow(slideshow):
    print("<html>")
    handleSlideshowTitle(slideshow.getElementsByTagName("title")[0])
    slides = slideshow.getElementsByTagName("slide")
    handleToc(slides)
    handleSlides(slides)
    print("</html>")

def handleSlides(slides):
    for slide in slides:
        handleSlide(slide)

def handleSlide(slide):
    handleSlideTitle(slide.getElementsByTagName("title")[0])
    handlePoints(slide.getElementsByTagName("point"))

def handleSlideshowTitle(title):
    print("<title>%s</title>" % getText(title.childNodes))

def handleSlideTitle(title):
    print("<h2>%s</h2>" % getText(title.childNodes))

def handlePoints(points):
    print("<ul>")
    for point in points:
        handlePoint(point)
    print("</ul>")

def handlePoint(point):
    print("<li>%s</li>" % getText(point.childNodes))

def handleToc(slides):
    for slide in slides:
        title = slide.getElementsByTagName("title")[0]
        print("<p>%s</p>" % getText(title.childNodes))

handleSlideshow(dom)

20.7.3. minidom and the DOM standard

The xml.dom.minidom module is essentially a DOM 1.0-compatible DOM with some DOM 2 features (primarily namespace features).

Usage of the DOM interface in Python is straight-forward. The following mapping rules apply:

Interfaces are accessed through instance objects. Applications should not instantiate the classes themselves; they should use the creator functions available on the Document object. Derived interfaces support all operations (and attributes) from the base interfaces, plus any new operations.

Operations are used as methods. Since the DOM uses only in parameters, the arguments are passed in normal order (from left to right). There are no optional arguments. void operations return None.

IDL attributes map to instance attributes. For compatibility with the OMG IDL language mapping for Python, an attribute foo can also be accessed through accessor methods _get_foo() and _set_foo(). readonly attributes must not be changed; this is not enforced at runtime.

The types short int, unsigned int, unsigned long long, and boolean all map to Python integer objects.

The type DOMString maps to Python strings. xml.dom.minidom supports either bytes or strings, but will normally produce strings. Values of type DOMString may also be None where allowed to have the IDL null value by the DOM specification from the W3C.

const declarations map to variables in their respective scope (e.g. xml.dom.minidom.Node.PROCESSING_INSTRUCTION_NODE); they must not be changed.

DOMException is currently not supported in xml.dom.minidom. Instead, xml.dom.minidom uses standard Python exceptions such as TypeError and AttributeError.

NodeList objects are implemented using Python’s built-in list type. These objects provide the interface defined in the DOM specification, but with earlier versions of Python they do not support the official API. They are, however, much more “Pythonic” than the interface defined in the W3C recommendations.

The following interfaces have no implementation in xml.dom.minidom:

DOMTimeStamp

DocumentType

DOMImplementation

CharacterData

CDATASection

Notation

Entity

EntityReference

DocumentFragment

Most of these reflect information in the XML document that is not of general utility to most DOM users.

备注

1

The encoding name included in the XML output should conform to the appropriate standards. For example, “UTF-8” is valid, but “UTF8” is not valid in an XML document’s declaration, even though Python accepts it as an encoding name. See https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl and https://www.iana.org/assignments/character-sets/character-sets.xhtml.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

20.8. xml.dom.pulldom — Support for building partial DOM trees

Source code: Lib/xml/dom/pulldom.py [https://github.com/python/cpython/tree/3.6/Lib/xml/dom/pulldom.py]

The xml.dom.pulldom module provides a “pull parser” which can also be asked to produce DOM-accessible fragments of the document where necessary. The basic concept involves pulling “events” from a stream of incoming XML and processing them. In contrast to SAX which also employs an event-driven processing model together with callbacks, the user of a pull parser is responsible for explicitly pulling events from the stream, looping over those events until either processing is finished or an error condition occurs.

警告

The xml.dom.pulldom module is not secure against maliciously constructed data. If you need to parse untrusted or unauthenticated data see XML 漏洞.

在 3.6.7 版更改: The SAX parser no longer processes general external entities by default to increase security by default. To enable processing of external entities, pass a custom parser instance in:

from xml.dom.pulldom import parse
from xml.sax import make_parser
from xml.sax.handler import feature_external_ges

parser = make_parser()
parser.setFeature(feature_external_ges, True)
parse(filename, parser=parser)

示例:

from xml.dom import pulldom

doc = pulldom.parse('sales_items.xml')
for event, node in doc:
    if event == pulldom.START_ELEMENT and node.tagName == 'item':
        if int(node.getAttribute('price')) > 50:
            doc.expandNode(node)
            print(node.toxml())

event is a constant and can be one of:

START_ELEMENT

END_ELEMENT

COMMENT

START_DOCUMENT

END_DOCUMENT

CHARACTERS

PROCESSING_INSTRUCTION

IGNORABLE_WHITESPACE

node is an object of type xml.dom.minidom.Document, xml.dom.minidom.Element or xml.dom.minidom.Text.

Since the document is treated as a “flat” stream of events, the document “tree” is implicitly traversed and the desired elements are found regardless of their depth in the tree. In other words, one does not need to consider hierarchical issues such as recursive searching of the document nodes, although if the context of elements were important, one would either need to maintain some context-related state (i.e. remembering where one is in the document at any given point) or to make use of the DOMEventStream.expandNode() method and switch to DOM-related processing.

class xml.dom.pulldom.PullDom(documentFactory=None)

Subclass of xml.sax.handler.ContentHandler.

class xml.dom.pulldom.SAX2DOM(documentFactory=None)

Subclass of xml.sax.handler.ContentHandler.

xml.dom.pulldom.parse(stream_or_string, parser=None, bufsize=None)

Return a DOMEventStream from the given input. stream_or_string may be either a file name, or a file-like object. parser, if given, must be an XMLReader object. This function will change the document handler of the parser and activate namespace support; other parser configuration (like setting an entity resolver) must have been done in advance.

If you have XML in a string, you can use the parseString() function instead:

xml.dom.pulldom.parseString(string, parser=None)

Return a DOMEventStream that represents the (Unicode) string.

xml.dom.pulldom.default_bufsize

Default value for the bufsize parameter to parse().

The value of this variable can be changed before calling parse() and the new value will take effect.

20.8.1. DOMEventStream Objects

class xml.dom.pulldom.DOMEventStream(stream, parser, bufsize)

getEvent()

Return a tuple containing event and the current node as xml.dom.minidom.Document if event equals START_DOCUMENT, xml.dom.minidom.Element if event equals START_ELEMENT or END_ELEMENT or xml.dom.minidom.Text if event equals CHARACTERS. The current node does not contain information about its children, unless expandNode() is called.

expandNode(node)

Expands all children of node into node. Example:

from xml.dom import pulldom

xml = '<html><title>Foo</title> <p>Some text <div>and more</div></p> </html>'
doc = pulldom.parseString(xml)
for event, node in doc:
    if event == pulldom.START_ELEMENT and node.tagName == 'p':
        # Following statement only prints '<p/>'
        print(node.toxml())
        doc.expandNode(node)
        # Following statement prints node with all its children '<p>Some text <div>and more</div></p>'
        print(node.toxml())

reset()

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

20.9. xml.sax — Support for SAX2 parsers

Source code: Lib/xml/sax/__init__.py [https://github.com/python/cpython/tree/3.6/Lib/xml/sax/__init__.py]

The xml.sax package provides a number of modules which implement the Simple API for XML (SAX) interface for Python. The package itself provides the SAX exceptions and the convenience functions which will be most used by users of the SAX API.

警告

The xml.sax module is not secure against maliciously constructed data. If you need to parse untrusted or unauthenticated data see XML 漏洞.

在 3.6.7 版更改: The SAX parser no longer processes general external entities by default to increase security. Before, the parser created network connections to fetch remote files or loaded local files from the file system for DTD and entities. The feature can be enabled again with method setFeature() on the parser object and argument feature_external_ges.

The convenience functions are:

xml.sax.make_parser(parser_list=[])

Create and return a SAX XMLReader object. The first parser found will be used. If parser_list is provided, it must be a list of strings which name modules that have a function named create_parser(). Modules listed in parser_list will be used before modules in the default list of parsers.

xml.sax.parse(filename_or_stream, handler, error_handler=handler.ErrorHandler())

Create a SAX parser and use it to parse a document. The document, passed in as filename_or_stream, can be a filename or a file object. The handler parameter needs to be a SAX ContentHandler instance. If error_handler is given, it must be a SAX ErrorHandler instance; if omitted, SAXParseException will be raised on all errors. There is no return value; all work must be done by the handler passed in.

xml.sax.parseString(string, handler, error_handler=handler.ErrorHandler())

Similar to parse(), but parses from a buffer string received as a parameter. string must be a str instance or a bytes-like object.

在 3.5 版更改: Added support of str instances.

A typical SAX application uses three kinds of objects: readers, handlers and input sources. “Reader” in this context is another term for parser, i.e. some piece of code that reads the bytes or characters from the input source, and produces a sequence of events. The events then get distributed to the handler objects, i.e. the reader invokes a method on the handler. A SAX application must therefore obtain a reader object, create or open the input sources, create the handlers, and connect these objects all together. As the final step of preparation, the reader is called to parse the input. During parsing, methods on the handler objects are called based on structural and syntactic events from the input data.

For these objects, only the interfaces are relevant; they are normally not instantiated by the application itself. Since Python does not have an explicit notion of interface, they are formally introduced as classes, but applications may use implementations which do not inherit from the provided classes. The InputSource, Locator, Attributes, AttributesNS, and XMLReader interfaces are defined in the module xml.sax.xmlreader. The handler interfaces are defined in xml.sax.handler. For convenience, InputSource (which is often instantiated directly) and the handler classes are also available from xml.sax. These interfaces are described below.

In addition to these classes, xml.sax provides the following exception classes.

exception xml.sax.SAXException(msg, exception=None)

Encapsulate an XML error or warning. This class can contain basic error or warning information from either the XML parser or the application: it can be subclassed to provide additional functionality or to add localization. Note that although the handlers defined in the ErrorHandler interface receive instances of this exception, it is not required to actually raise the exception — it is also useful as a container for information.

When instantiated, msg should be a human-readable description of the error. The optional exception parameter, if given, should be None or an exception that was caught by the parsing code and is being passed along as information.

This is the base class for the other SAX exception classes.

exception xml.sax.SAXParseException(msg, exception, locator)

Subclass of SAXException raised on parse errors. Instances of this class are passed to the methods of the SAX ErrorHandler interface to provide information about the parse error. This class supports the SAX Locator interface as well as the SAXException interface.

exception xml.sax.SAXNotRecognizedException(msg, exception=None)

Subclass of SAXException raised when a SAX XMLReader is confronted with an unrecognized feature or property. SAX applications and extensions may use this class for similar purposes.

exception xml.sax.SAXNotSupportedException(msg, exception=None)

Subclass of SAXException raised when a SAX XMLReader is asked to enable a feature that is not supported, or to set a property to a value that the implementation does not support. SAX applications and extensions may use this class for similar purposes.

参见

SAX: The Simple API for XML [http://www.saxproject.org/]

This site is the focal point for the definition of the SAX API. It provides a Java implementation and online documentation. Links to implementations and historical information are also available.

Module xml.sax.handler

Definitions of the interfaces for application-provided objects.

Module xml.sax.saxutils

Convenience functions for use in SAX applications.

Module xml.sax.xmlreader

Definitions of the interfaces for parser-provided objects.

20.9.1. SAXException Objects

The SAXException exception class supports the following methods:

SAXException.getMessage()

Return a human-readable message describing the error condition.

SAXException.getException()

Return an encapsulated exception object, or None.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

20.10. xml.sax.handler — Base classes for SAX handlers

Source code: Lib/xml/sax/handler.py [https://github.com/python/cpython/tree/3.6/Lib/xml/sax/handler.py]

The SAX API defines four kinds of handlers: content handlers, DTD handlers, error handlers, and entity resolvers. Applications normally only need to implement those interfaces whose events they are interested in; they can implement the interfaces in a single object or in multiple objects. Handler implementations should inherit from the base classes provided in the module xml.sax.handler, so that all methods get default implementations.

class xml.sax.handler.ContentHandler

This is the main callback interface in SAX, and the one most important to applications. The order of events in this interface mirrors the order of the information in the document.

class xml.sax.handler.DTDHandler

Handle DTD events.

This interface specifies only those DTD events required for basic parsing (unparsed entities and attributes).

class xml.sax.handler.EntityResolver

Basic interface for resolving entities. If you create an object implementing this interface, then register the object with your Parser, the parser will call the method in your object to resolve all external entities.

class xml.sax.handler.ErrorHandler

Interface used by the parser to present error and warning messages to the application. The methods of this object control whether errors are immediately converted to exceptions or are handled in some other way.

In addition to these classes, xml.sax.handler provides symbolic constants for the feature and property names.

xml.sax.handler.feature_namespaces

value: "http://xml.org/sax/features/namespaces"

true: Perform Namespace processing.

false: Optionally do not perform Namespace processing (implies namespace-prefixes; default).

access: (parsing) read-only; (not parsing) read/write

xml.sax.handler.feature_namespace_prefixes

value: "http://xml.org/sax/features/namespace-prefixes"

true: Report the original prefixed names and attributes used for Namespace declarations.

false: Do not report attributes used for Namespace declarations, and optionally do not report original prefixed names (default).

access: (parsing) read-only; (not parsing) read/write

xml.sax.handler.feature_string_interning

value: "http://xml.org/sax/features/string-interning"

true: All element names, prefixes, attribute names, Namespace URIs, and local names are interned using the built-in intern function.

false: Names are not necessarily interned, although they may be (default).

access: (parsing) read-only; (not parsing) read/write

xml.sax.handler.feature_validation

value: "http://xml.org/sax/features/validation"

true: Report all validation errors (implies external-general-entities and external-parameter-entities).

false: Do not report validation errors.

access: (parsing) read-only; (not parsing) read/write

xml.sax.handler.feature_external_ges

value: "http://xml.org/sax/features/external-general-entities"

true: Include all external general (text) entities.

false: Do not include external general entities.

access: (parsing) read-only; (not parsing) read/write

xml.sax.handler.feature_external_pes

value: "http://xml.org/sax/features/external-parameter-entities"

true: Include all external parameter entities, including the external DTD subset.

false: Do not include any external parameter entities, even the external DTD subset.

access: (parsing) read-only; (not parsing) read/write

xml.sax.handler.all_features

List of all features.

xml.sax.handler.property_lexical_handler

value: "http://xml.org/sax/properties/lexical-handler"

data type: xml.sax.sax2lib.LexicalHandler (not supported in Python 2)

description: An optional extension handler for lexical events like comments.

access: read/write

xml.sax.handler.property_declaration_handler

value: "http://xml.org/sax/properties/declaration-handler"

data type: xml.sax.sax2lib.DeclHandler (not supported in Python 2)

description: An optional extension handler for DTD-related events other than notations and unparsed entities.

access: read/write

xml.sax.handler.property_dom_node

value: "http://xml.org/sax/properties/dom-node"

data type: org.w3c.dom.Node (not supported in Python 2)

description: When parsing, the current DOM node being visited if this is a DOM iterator; when not parsing, the root DOM node for iteration.

access: (parsing) read-only; (not parsing) read/write

xml.sax.handler.property_xml_string

value: "http://xml.org/sax/properties/xml-string"

data type: String

description: The literal string of characters that was the source for the current event.

access: read-only

xml.sax.handler.all_properties

List of all known property names.

20.10.1. ContentHandler 对象

Users are expected to subclass ContentHandler to support their application. The following methods are called by the parser on the appropriate events in the input document:

ContentHandler.setDocumentLocator(locator)

Called by the parser to give the application a locator for locating the origin of document events.

SAX parsers are strongly encouraged (though not absolutely required) to supply a locator: if it does so, it must supply the locator to the application by invoking this method before invoking any of the other methods in the DocumentHandler interface.

The locator allows the application to determine the end position of any document-related event, even if the parser is not reporting an error. Typically, the application will use this information for reporting its own errors (such as character content that does not match an application’s business rules). The information returned by the locator is probably not sufficient for use with a search engine.

Note that the locator will return correct information only during the invocation of the events in this interface. The application should not attempt to use it at any other time.

ContentHandler.startDocument()

Receive notification of the beginning of a document.

The SAX parser will invoke this method only once, before any other methods in this interface or in DTDHandler (except for setDocumentLocator()).

ContentHandler.endDocument()

Receive notification of the end of a document.

The SAX parser will invoke this method only once, and it will be the last method invoked during the parse. The parser shall not invoke this method until it has either abandoned parsing (because of an unrecoverable error) or reached the end of input.

ContentHandler.startPrefixMapping(prefix, uri)

Begin the scope of a prefix-URI Namespace mapping.

The information from this event is not necessary for normal Namespace processing: the SAX XML reader will automatically replace prefixes for element and attribute names when the feature_namespaces feature is enabled (the default).

There are cases, however, when applications need to use prefixes in character data or in attribute values, where they cannot safely be expanded automatically; the startPrefixMapping() and endPrefixMapping() events supply the information to the application to expand prefixes in those contexts itself, if necessary.

Note that startPrefixMapping() and endPrefixMapping() events are not guaranteed to be properly nested relative to each-other: all startPrefixMapping() events will occur before the corresponding startElement() event, and all endPrefixMapping() events will occur after the corresponding endElement() event, but their order is not guaranteed.

ContentHandler.endPrefixMapping(prefix)

End the scope of a prefix-URI mapping.

See startPrefixMapping() for details. This event will always occur after the corresponding endElement() event, but the order of endPrefixMapping() events is not otherwise guaranteed.

ContentHandler.startElement(name, attrs)

Signals the start of an element in non-namespace mode.

The name parameter contains the raw XML 1.0 name of the element type as a string and the attrs parameter holds an object of the Attributes interface (see The Attributes Interface) containing the attributes of the element. The object passed as attrs may be re-used by the parser; holding on to a reference to it is not a reliable way to keep a copy of the attributes. To keep a copy of the attributes, use the copy() method of the attrs object.

ContentHandler.endElement(name)

Signals the end of an element in non-namespace mode.

The name parameter contains the name of the element type, just as with the startElement() event.

ContentHandler.startElementNS(name, qname, attrs)

Signals the start of an element in namespace mode.

The name parameter contains the name of the element type as a (uri, localname) tuple, the qname parameter contains the raw XML 1.0 name used in the source document, and the attrs parameter holds an instance of the AttributesNS interface (see The AttributesNS Interface) containing the attributes of the element. If no namespace is associated with the element, the uri component of name will be None. The object passed as attrs may be re-used by the parser; holding on to a reference to it is not a reliable way to keep a copy of the attributes. To keep a copy of the attributes, use the copy() method of the attrs object.

Parsers may set the qname parameter to None, unless the feature_namespace_prefixes feature is activated.

ContentHandler.endElementNS(name, qname)

Signals the end of an element in namespace mode.

The name parameter contains the name of the element type, just as with the startElementNS() method, likewise the qname parameter.

ContentHandler.characters(content)

Receive notification of character data.

The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information.

content may be a string or bytes instance; the expat reader module always produces strings.

注解

The earlier SAX 1 interface provided by the Python XML Special Interest Group used a more Java-like interface for this method. Since most parsers used from Python did not take advantage of the older interface, the simpler signature was chosen to replace it. To convert old code to the new interface, use content instead of slicing content with the old offset and length parameters.

ContentHandler.ignorableWhitespace(whitespace)

Receive notification of ignorable whitespace in element content.

Validating Parsers must use this method to report each chunk of ignorable whitespace (see the W3C XML 1.0 recommendation, section 2.10): non-validating parsers may also use this method if they are capable of parsing and using content models.

SAX parsers may return all contiguous whitespace in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity, so that the Locator provides useful information.

ContentHandler.processingInstruction(target, data)

Receive notification of a processing instruction.

The Parser will invoke this method once for each processing instruction found: note that processing instructions may occur before or after the main document element.

A SAX parser should never report an XML declaration (XML 1.0, section 2.8) or a text declaration (XML 1.0, section 4.3.1) using this method.

ContentHandler.skippedEntity(name)

Receive notification of a skipped entity.

The Parser will invoke this method once for each entity skipped. Non-validating processors may skip entities if they have not seen the declarations (because, for example, the entity was declared in an external DTD subset). All processors may skip external entities, depending on the values of the feature_external_ges and the feature_external_pes properties.

20.10.2. DTDHandler 对象

DTDHandler instances provide the following methods:

DTDHandler.notationDecl(name, publicId, systemId)

Handle a notation declaration event.

DTDHandler.unparsedEntityDecl(name, publicId, systemId, ndata)

Handle an unparsed entity declaration event.

20.10.3. EntityResolver 对象

EntityResolver.resolveEntity(publicId, systemId)

Resolve the system identifier of an entity and return either the system identifier to read from as a string, or an InputSource to read from. The default implementation returns systemId.

20.10.4. ErrorHandler 对象

Objects with this interface are used to receive error and warning information from the XMLReader. If you create an object that implements this interface, then register the object with your XMLReader, the parser will call the methods in your object to report all warnings and errors. There are three levels of errors available: warnings, (possibly) recoverable errors, and unrecoverable errors. All methods take a SAXParseException as the only parameter. Errors and warnings may be converted to an exception by raising the passed-in exception object.

ErrorHandler.error(exception)

Called when the parser encounters a recoverable error. If this method does not raise an exception, parsing may continue, but further document information should not be expected by the application. Allowing the parser to continue may allow additional errors to be discovered in the input document.

ErrorHandler.fatalError(exception)

Called when the parser encounters an error it cannot recover from; parsing is expected to terminate when this method returns.

ErrorHandler.warning(exception)

Called when the parser presents minor warning information to the application. Parsing is expected to continue when this method returns, and document information will continue to be passed to the application. Raising an exception in this method will cause parsing to end.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

20.11. xml.sax.saxutils — SAX 工具集

源代码: Lib/xml/sax/saxutils.py [https://github.com/python/cpython/tree/3.6/Lib/xml/sax/saxutils.py]

xml.sax.saxutils 模块包含一些在创建 SAX 应用程序时十分有用的类和函数，它们可以被直接使用，或者是作为基类使用。

xml.sax.saxutils.escape(data, entities={})

对数据字符串中对 '&', '<' 和 '>' 进行转义。

你可以通过传入一个字典作为可选的 entities 形参来对其他字符串数据进行转义。 字典的键和值必须为字符串；每个键将被替换为其所对应的值。 字符 '&', '<' 和 '>' 总是会被转义，即使提供了 entities。

xml.sax.saxutils.unescape(data, entities={})

对字符串数据中的 '&amp;', '&lt;' 和 '&gt;' 进行反转义。

你可以通过传入一个字典作为可选的 entities 形参来对其他数据字符串进行转义。 字典的键和值必须都为字符串；每个键将被替换为所对应的值。 '&amp', '&lt;' 和 '&gt;' 将总是保持不被转义，即使提供了 entities。

xml.sax.saxutils.quoteattr(data, entities={})

类似于 escape()，但还会对 data 进行处理以将其用作属性值。 返回值是 data 加上任何额外要求的替换的带引号版本。quoteattr() 将基于 data 的内容选择一个引号字符，以尽量避免在字符串中编码任何引号字符。 如果单双引号字符在 data 中都存在，则双引号字符将被编码并且 data 将使用双引号来标记。 结果字符串可被直接用作属性值:

>>> print("<element attr=%s>" % quoteattr("ab ' cd \" ef"))
<element attr="ab ' cd &quot; ef">

此函数适用于为 HTML 或任何使用引用实体语法的 SGML 生成属性值。

class xml.sax.saxutils.XMLGenerator(out=None, encoding='iso-8859-1', short_empty_elements=False)

这个类通过将 SAX 事件写回到 XML 文档来实现 ContentHandler 接口。 换句话说，使用 XMLGenerator 作为内容处理程序将重新产生所解析的原始文档。 out 应当为一个文件类对象，它默认将为 sys.stdout。 encoding 为输出流的编码格式，它默认将为 'iso-8859-1'。 short_empty_elements 控制不包含内容的元素的格式化：如为 False (默认值) 则它们会以开始/结束标记对的形式被发送，如果设为 True 则它们会以单个自结束标记的形式被发送。

3.2 新版功能: short_empty_elements 形参。

class xml.sax.saxutils.XMLFilterBase(base)

这个类被设计用来分隔 XMLReader 和客户端应用的事件处理程序。 在默认情况下，它除了将请求传送给读取器并将事件传送给处理程序之外什么都不做，但其子类可以重载特定的方法以在传送它们的时候修改事件流或配置请求。

xml.sax.saxutils.prepare_input_source(source, base='')

此函数接受一个输入源和一个可选的基准 URL 并返回一个经过完整解析可供读取的 InputSource。 输入源的给出形式可以为字符串、文件类对象或 InputSource 对象；解析器将使用此函数来针对它们的 parse() 方法实现多态 source 参数。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

20.12. xml.sax.xmlreader — Interface for XML parsers

Source code: Lib/xml/sax/xmlreader.py [https://github.com/python/cpython/tree/3.6/Lib/xml/sax/xmlreader.py]

SAX parsers implement the XMLReader interface. They are implemented in a Python module, which must provide a function create_parser(). This function is invoked by xml.sax.make_parser() with no arguments to create a new parser object.

class xml.sax.xmlreader.XMLReader

Base class which can be inherited by SAX parsers.

class xml.sax.xmlreader.IncrementalParser

In some cases, it is desirable not to parse an input source at once, but to feed chunks of the document as they get available. Note that the reader will normally not read the entire file, but read it in chunks as well; still parse() won’t return until the entire document is processed. So these interfaces should be used if the blocking behaviour of parse() is not desirable.

When the parser is instantiated it is ready to begin accepting data from the feed method immediately. After parsing has been finished with a call to close the reset method must be called to make the parser ready to accept new data, either from feed or using the parse method.

Note that these methods must not be called during parsing, that is, after parse has been called and before it returns.

By default, the class also implements the parse method of the XMLReader interface using the feed, close and reset methods of the IncrementalParser interface as a convenience to SAX 2.0 driver writers.

class xml.sax.xmlreader.Locator

Interface for associating a SAX event with a document location. A locator object will return valid results only during calls to DocumentHandler methods; at any other time, the results are unpredictable. If information is not available, methods may return None.

class xml.sax.xmlreader.InputSource(system_id=None)

Encapsulation of the information needed by the XMLReader to read entities.

This class may include information about the public identifier, system identifier, byte stream (possibly with character encoding information) and/or the character stream of an entity.

Applications will create objects of this class for use in the XMLReader.parse() method and for returning from EntityResolver.resolveEntity.

An InputSource belongs to the application, the XMLReader is not allowed to modify InputSource objects passed to it from the application, although it may make copies and modify those.

class xml.sax.xmlreader.AttributesImpl(attrs)

This is an implementation of the Attributes interface (see section The Attributes Interface). This is a dictionary-like object which represents the element attributes in a startElement() call. In addition to the most useful dictionary operations, it supports a number of other methods as described by the interface. Objects of this class should be instantiated by readers; attrs must be a dictionary-like object containing a mapping from attribute names to attribute values.

class xml.sax.xmlreader.AttributesNSImpl(attrs, qnames)

Namespace-aware variant of AttributesImpl, which will be passed to startElementNS(). It is derived from AttributesImpl, but understands attribute names as two-tuples of namespaceURI and localname. In addition, it provides a number of methods expecting qualified names as they appear in the original document. This class implements the AttributesNS interface (see section The AttributesNS Interface).

20.12.1. XMLReader 对象

The XMLReader interface supports the following methods:

XMLReader.parse(source)

Process an input source, producing SAX events. The source object can be a system identifier (a string identifying the input source – typically a file name or a URL), a file-like object, or an InputSource object. When parse() returns, the input is completely processed, and the parser object can be discarded or reset.

在 3.5 版更改: Added support of character streams.

XMLReader.getContentHandler()

Return the current ContentHandler.

XMLReader.setContentHandler(handler)

Set the current ContentHandler. If no ContentHandler is set, content events will be discarded.

XMLReader.getDTDHandler()

Return the current DTDHandler.

XMLReader.setDTDHandler(handler)

Set the current DTDHandler. If no DTDHandler is set, DTD events will be discarded.

XMLReader.getEntityResolver()

Return the current EntityResolver.

XMLReader.setEntityResolver(handler)

Set the current EntityResolver. If no EntityResolver is set, attempts to resolve an external entity will result in opening the system identifier for the entity, and fail if it is not available.

XMLReader.getErrorHandler()

Return the current ErrorHandler.

XMLReader.setErrorHandler(handler)

Set the current error handler. If no ErrorHandler is set, errors will be raised as exceptions, and warnings will be printed.

XMLReader.setLocale(locale)

Allow an application to set the locale for errors and warnings.

SAX parsers are not required to provide localization for errors and warnings; if they cannot support the requested locale, however, they must raise a SAX exception. Applications may request a locale change in the middle of a parse.

XMLReader.getFeature(featurename)

Return the current setting for feature featurename. If the feature is not recognized, SAXNotRecognizedException is raised. The well-known featurenames are listed in the module xml.sax.handler.

XMLReader.setFeature(featurename, value)

Set the featurename to value. If the feature is not recognized, SAXNotRecognizedException is raised. If the feature or its setting is not supported by the parser, SAXNotSupportedException is raised.

XMLReader.getProperty(propertyname)

Return the current setting for property propertyname. If the property is not recognized, a SAXNotRecognizedException is raised. The well-known propertynames are listed in the module xml.sax.handler.

XMLReader.setProperty(propertyname, value)

Set the propertyname to value. If the property is not recognized, SAXNotRecognizedException is raised. If the property or its setting is not supported by the parser, SAXNotSupportedException is raised.

20.12.2. IncrementalParser 对象

Instances of IncrementalParser offer the following additional methods:

IncrementalParser.feed(data)

Process a chunk of data.

IncrementalParser.close()

Assume the end of the document. That will check well-formedness conditions that can be checked only at the end, invoke handlers, and may clean up resources allocated during parsing.

IncrementalParser.reset()

This method is called after close has been called to reset the parser so that it is ready to parse new documents. The results of calling parse or feed after close without calling reset are undefined.

20.12.3. Locator 对象

Instances of Locator provide these methods:

Locator.getColumnNumber()

Return the column number where the current event begins.

Locator.getLineNumber()

Return the line number where the current event begins.

Locator.getPublicId()

Return the public identifier for the current event.

Locator.getSystemId()

Return the system identifier for the current event.

20.12.4. InputSource 对象

InputSource.setPublicId(id)

Sets the public identifier of this InputSource.

InputSource.getPublicId()

Returns the public identifier of this InputSource.

InputSource.setSystemId(id)

Sets the system identifier of this InputSource.

InputSource.getSystemId()

Returns the system identifier of this InputSource.

InputSource.setEncoding(encoding)

Sets the character encoding of this InputSource.

The encoding must be a string acceptable for an XML encoding declaration (see section 4.3.3 of the XML recommendation).

The encoding attribute of the InputSource is ignored if the InputSource also contains a character stream.

InputSource.getEncoding()

Get the character encoding of this InputSource.

InputSource.setByteStream(bytefile)

Set the byte stream (a binary file) for this input source.

The SAX parser will ignore this if there is also a character stream specified, but it will use a byte stream in preference to opening a URI connection itself.

If the application knows the character encoding of the byte stream, it should set it with the setEncoding method.

InputSource.getByteStream()

Get the byte stream for this input source.

The getEncoding method will return the character encoding for this byte stream, or None if unknown.

InputSource.setCharacterStream(charfile)

Set the character stream (a text file) for this input source.

If there is a character stream specified, the SAX parser will ignore any byte stream and will not attempt to open a URI connection to the system identifier.

InputSource.getCharacterStream()

Get the character stream for this input source.

20.12.5. The Attributes Interface

Attributes objects implement a portion of the mapping protocol, including the methods copy(), get(), __contains__(), items(), keys(), and values(). The following methods are also provided:

Attributes.getLength()

Return the number of attributes.

Attributes.getNames()

Return the names of the attributes.

Attributes.getType(name)

Returns the type of the attribute name, which is normally 'CDATA'.

Attributes.getValue(name)

Return the value of attribute name.

20.12.6. The AttributesNS Interface

This interface is a subtype of the Attributes interface (see section The Attributes Interface). All methods supported by that interface are also available on AttributesNS objects.

The following methods are also available:

AttributesNS.getValueByQName(name)

Return the value for a qualified name.

AttributesNS.getNameByQName(name)

Return the (namespace, localname) pair for a qualified name.

AttributesNS.getQNameByName(name)

Return the qualified name for a (namespace, localname) pair.

AttributesNS.getQNames()

Return the qualified names of all attributes.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

20.13. xml.parsers.expat — Fast XML parsing using Expat

警告

The pyexpat module is not secure against maliciously constructed data. If you need to parse untrusted or unauthenticated data see XML 漏洞.

The xml.parsers.expat module is a Python interface to the Expat non-validating XML parser. The module provides a single extension type, xmlparser, that represents the current state of an XML parser. After an xmlparser object has been created, various attributes of the object can be set to handler functions. When an XML document is then fed to the parser, the handler functions are called for the character data and markup in the XML document.

This module uses the pyexpat module to provide access to the Expat parser. Direct use of the pyexpat module is deprecated.

This module provides one exception and one type object:

exception xml.parsers.expat.ExpatError

The exception raised when Expat reports an error. See section ExpatError Exceptions for more information on interpreting Expat errors.

exception xml.parsers.expat.error

Alias for ExpatError.

xml.parsers.expat.XMLParserType

The type of the return values from the ParserCreate() function.

The xml.parsers.expat module contains two functions:

xml.parsers.expat.ErrorString(errno)

Returns an explanatory string for a given error number errno.

xml.parsers.expat.ParserCreate(encoding=None, namespace_separator=None)

Creates and returns a new xmlparser object. encoding, if specified, must be a string naming the encoding used by the XML data. Expat doesn’t support as many encodings as Python does, and its repertoire of encodings can’t be extended; it supports UTF-8, UTF-16, ISO-8859-1 (Latin1), and ASCII. If encoding 1 is given it will override the implicit or explicit encoding of the document.

Expat can optionally do XML namespace processing for you, enabled by providing a value for namespace_separator. The value must be a one-character string; a ValueError will be raised if the string has an illegal length (None is considered the same as omission). When namespace processing is enabled, element type names and attribute names that belong to a namespace will be expanded. The element name passed to the element handlers StartElementHandler and EndElementHandler will be the concatenation of the namespace URI, the namespace separator character, and the local part of the name. If the namespace separator is a zero byte (chr(0)) then the namespace URI and the local part will be concatenated without any separator.

For example, if namespace_separator is set to a space character (' ') and the following document is parsed:

<?xml version="1.0"?>
<root xmlns    = "http://default-namespace.org/"
      xmlns:py = "http://www.python.org/ns/">
  <py:elem1 />
  <elem2 xmlns="" />
</root>

StartElementHandler will receive the following strings for each element:

http://default-namespace.org/ root
http://www.python.org/ns/ elem1
elem2

Due to limitations in the Expat library used by pyexpat, the xmlparser instance returned can only be used to parse a single XML document. Call ParserCreate for each document to provide unique parser instances.

参见

The Expat XML Parser [http://www.libexpat.org/]

Home page of the Expat project.

20.13.1. XMLParser对象

xmlparser objects have the following methods:

xmlparser.Parse(data[, isfinal])

Parses the contents of the string data, calling the appropriate handler functions to process the parsed data. isfinal must be true on the final call to this method; it allows the parsing of a single file in fragments, not the submission of multiple files. data can be the empty string at any time.

xmlparser.ParseFile(file)

Parse XML data reading from the object file. file only needs to provide the read(nbytes) method, returning the empty string when there’s no more data.

xmlparser.SetBase(base)

Sets the base to be used for resolving relative URIs in system identifiers in declarations. Resolving relative identifiers is left to the application: this value will be passed through as the base argument to the ExternalEntityRefHandler(), NotationDeclHandler(), and UnparsedEntityDeclHandler() functions.

xmlparser.GetBase()

Returns a string containing the base set by a previous call to SetBase(), or None if SetBase() hasn’t been called.

xmlparser.GetInputContext()

Returns the input data that generated the current event as a string. The data is in the encoding of the entity which contains the text. When called while an event handler is not active, the return value is None.

xmlparser.ExternalEntityParserCreate(context[, encoding])

Create a “child” parser which can be used to parse an external parsed entity referred to by content parsed by the parent parser. The context parameter should be the string passed to the ExternalEntityRefHandler() handler function, described below. The child parser is created with the ordered_attributes and specified_attributes set to the values of this parser.

xmlparser.SetParamEntityParsing(flag)

Control parsing of parameter entities (including the external DTD subset). Possible flag values are XML_PARAM_ENTITY_PARSING_NEVER, XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE and XML_PARAM_ENTITY_PARSING_ALWAYS. Return true if setting the flag was successful.

xmlparser.UseForeignDTD([flag])

Calling this with a true value for flag (the default) will cause Expat to call the ExternalEntityRefHandler with None for all arguments to allow an alternate DTD to be loaded. If the document does not contain a document type declaration, the ExternalEntityRefHandler will still be called, but the StartDoctypeDeclHandler and EndDoctypeDeclHandler will not be called.

Passing a false value for flag will cancel a previous call that passed a true value, but otherwise has no effect.

This method can only be called before the Parse() or ParseFile() methods are called; calling it after either of those have been called causes ExpatError to be raised with the code attribute set to errors.codes[errors.XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING].

xmlparser objects have the following attributes:

xmlparser.buffer_size

The size of the buffer used when buffer_text is true. A new buffer size can be set by assigning a new integer value to this attribute. When the size is changed, the buffer will be flushed.

xmlparser.buffer_text

Setting this to true causes the xmlparser object to buffer textual content returned by Expat to avoid multiple calls to the CharacterDataHandler() callback whenever possible. This can improve performance substantially since Expat normally breaks character data into chunks at every line ending. This attribute is false by default, and may be changed at any time.

xmlparser.buffer_used

If buffer_text is enabled, the number of bytes stored in the buffer. These bytes represent UTF-8 encoded text. This attribute has no meaningful interpretation when buffer_text is false.

xmlparser.ordered_attributes

Setting this attribute to a non-zero integer causes the attributes to be reported as a list rather than a dictionary. The attributes are presented in the order found in the document text. For each attribute, two list entries are presented: the attribute name and the attribute value. (Older versions of this module also used this format.) By default, this attribute is false; it may be changed at any time.

xmlparser.specified_attributes

If set to a non-zero integer, the parser will report only those attributes which were specified in the document instance and not those which were derived from attribute declarations. Applications which set this need to be especially careful to use what additional information is available from the declarations as needed to comply with the standards for the behavior of XML processors. By default, this attribute is false; it may be changed at any time.

The following attributes contain values relating to the most recent error encountered by an xmlparser object, and will only have correct values once a call to Parse() or ParseFile() has raised an xml.parsers.expat.ExpatError exception.

xmlparser.ErrorByteIndex

Byte index at which an error occurred.

xmlparser.ErrorCode

Numeric code specifying the problem. This value can be passed to the ErrorString() function, or compared to one of the constants defined in the errors object.

xmlparser.ErrorColumnNumber

Column number at which an error occurred.

xmlparser.ErrorLineNumber

Line number at which an error occurred.

The following attributes contain values relating to the current parse location in an xmlparser object. During a callback reporting a parse event they indicate the location of the first of the sequence of characters that generated the event. When called outside of a callback, the position indicated will be just past the last parse event (regardless of whether there was an associated callback).

xmlparser.CurrentByteIndex

Current byte index in the parser input.

xmlparser.CurrentColumnNumber

Current column number in the parser input.

xmlparser.CurrentLineNumber

Current line number in the parser input.

Here is the list of handlers that can be set. To set a handler on an xmlparser object o, use o.handlername = func. handlername must be taken from the following list, and func must be a callable object accepting the correct number of arguments. The arguments are all strings, unless otherwise stated.

xmlparser.XmlDeclHandler(version, encoding, standalone)

Called when the XML declaration is parsed. The XML declaration is the (optional) declaration of the applicable version of the XML recommendation, the encoding of the document text, and an optional “standalone” declaration. version and encoding will be strings, and standalone will be 1 if the document is declared standalone, 0 if it is declared not to be standalone, or -1 if the standalone clause was omitted. This is only available with Expat version 1.95.0 or newer.

xmlparser.StartDoctypeDeclHandler(doctypeName, systemId, publicId, has_internal_subset)

Called when Expat begins parsing the document type declaration (<!DOCTYPE ...). The doctypeName is provided exactly as presented. The systemId and publicId parameters give the system and public identifiers if specified, or None if omitted. has_internal_subset will be true if the document contains and internal document declaration subset. This requires Expat version 1.2 or newer.

xmlparser.EndDoctypeDeclHandler()

Called when Expat is done parsing the document type declaration. This requires Expat version 1.2 or newer.

xmlparser.ElementDeclHandler(name, model)

Called once for each element type declaration. name is the name of the element type, and model is a representation of the content model.

xmlparser.AttlistDeclHandler(elname, attname, type, default, required)

Called for each declared attribute for an element type. If an attribute list declaration declares three attributes, this handler is called three times, once for each attribute. elname is the name of the element to which the declaration applies and attname is the name of the attribute declared. The attribute type is a string passed as type; the possible values are 'CDATA', 'ID', 'IDREF', … default gives the default value for the attribute used when the attribute is not specified by the document instance, or None if there is no default value (#IMPLIED values). If the attribute is required to be given in the document instance, required will be true. This requires Expat version 1.95.0 or newer.

xmlparser.StartElementHandler(name, attributes)

Called for the start of every element. name is a string containing the element name, and attributes is the element attributes. If ordered_attributes is true, this is a list (see ordered_attributes for a full description). Otherwise it’s a dictionary mapping names to values.

xmlparser.EndElementHandler(name)

Called for the end of every element.

xmlparser.ProcessingInstructionHandler(target, data)

Called for every processing instruction.

xmlparser.CharacterDataHandler(data)

Called for character data. This will be called for normal character data, CDATA marked content, and ignorable whitespace. Applications which must distinguish these cases can use the StartCdataSectionHandler, EndCdataSectionHandler, and ElementDeclHandler callbacks to collect the required information.

xmlparser.UnparsedEntityDeclHandler(entityName, base, systemId, publicId, notationName)

Called for unparsed (NDATA) entity declarations. This is only present for version 1.2 of the Expat library; for more recent versions, use EntityDeclHandler instead. (The underlying function in the Expat library has been declared obsolete.)

xmlparser.EntityDeclHandler(entityName, is_parameter_entity, value, base, systemId, publicId, notationName)

Called for all entity declarations. For parameter and internal entities, value will be a string giving the declared contents of the entity; this will be None for external entities. The notationName parameter will be None for parsed entities, and the name of the notation for unparsed entities. is_parameter_entity will be true if the entity is a parameter entity or false for general entities (most applications only need to be concerned with general entities). This is only available starting with version 1.95.0 of the Expat library.

xmlparser.NotationDeclHandler(notationName, base, systemId, publicId)

Called for notation declarations. notationName, base, and systemId, and publicId are strings if given. If the public identifier is omitted, publicId will be None.

xmlparser.StartNamespaceDeclHandler(prefix, uri)

Called when an element contains a namespace declaration. Namespace declarations are processed before the StartElementHandler is called for the element on which declarations are placed.

xmlparser.EndNamespaceDeclHandler(prefix)

Called when the closing tag is reached for an element that contained a namespace declaration. This is called once for each namespace declaration on the element in the reverse of the order for which the StartNamespaceDeclHandler was called to indicate the start of each namespace declaration’s scope. Calls to this handler are made after the corresponding EndElementHandler for the end of the element.

xmlparser.CommentHandler(data)

Called for comments. data is the text of the comment, excluding the leading '<!--' and trailing '-->'.

xmlparser.StartCdataSectionHandler()

Called at the start of a CDATA section. This and EndCdataSectionHandler are needed to be able to identify the syntactical start and end for CDATA sections.

xmlparser.EndCdataSectionHandler()

Called at the end of a CDATA section.

xmlparser.DefaultHandler(data)

Called for any characters in the XML document for which no applicable handler has been specified. This means characters that are part of a construct which could be reported, but for which no handler has been supplied.

xmlparser.DefaultHandlerExpand(data)

This is the same as the DefaultHandler(), but doesn’t inhibit expansion of internal entities. The entity reference will not be passed to the default handler.

xmlparser.NotStandaloneHandler()

Called if the XML document hasn’t been declared as being a standalone document. This happens when there is an external subset or a reference to a parameter entity, but the XML declaration does not set standalone to yes in an XML declaration. If this handler returns 0, then the parser will raise an XML_ERROR_NOT_STANDALONE error. If this handler is not set, no exception is raised by the parser for this condition.

xmlparser.ExternalEntityRefHandler(context, base, systemId, publicId)

Called for references to external entities. base is the current base, as set by a previous call to SetBase(). The public and system identifiers, systemId and publicId, are strings if given; if the public identifier is not given, publicId will be None. The context value is opaque and should only be used as described below.

For external entities to be parsed, this handler must be implemented. It is responsible for creating the sub-parser using ExternalEntityParserCreate(context), initializing it with the appropriate callbacks, and parsing the entity. This handler should return an integer; if it returns 0, the parser will raise an XML_ERROR_EXTERNAL_ENTITY_HANDLING error, otherwise parsing will continue.

If this handler is not provided, external entities are reported by the DefaultHandler callback, if provided.

20.13.2. ExpatError Exceptions

ExpatError exceptions have a number of interesting attributes:

ExpatError.code

Expat’s internal error number for the specific error. The errors.messages dictionary maps these error numbers to Expat’s error messages. For example:

from xml.parsers.expat import ParserCreate, ExpatError, errors

p = ParserCreate()
try:
    p.Parse(some_xml_document)
except ExpatError as err:
    print("Error:", errors.messages[err.code])

The errors module also provides error message constants and a dictionary codes mapping these messages back to the error codes, see below.

ExpatError.lineno

Line number on which the error was detected. The first line is numbered 1.

ExpatError.offset

Character offset into the line where the error occurred. The first column is numbered 0.

20.13.3. 示例

The following program defines three handlers that just print out their arguments.

import xml.parsers.expat

# 3 handler functions
def start_element(name, attrs):
    print('Start element:', name, attrs)
def end_element(name):
    print('End element:', name)
def char_data(data):
    print('Character data:', repr(data))

p = xml.parsers.expat.ParserCreate()

p.StartElementHandler = start_element
p.EndElementHandler = end_element
p.CharacterDataHandler = char_data

p.Parse("""<?xml version="1.0"?>
<parent id="top"><child1 name="paul">Text goes here</child1>
<child2 name="fred">More text</child2>
</parent>""", 1)

The output from this program is:

Start element: parent {'id': 'top'}
Start element: child1 {'name': 'paul'}
Character data: 'Text goes here'
End element: child1
Character data: '\n'
Start element: child2 {'name': 'fred'}
Character data: 'More text'
End element: child2
Character data: '\n'
End element: parent

20.13.4. Content Model Descriptions

Content models are described using nested tuples. Each tuple contains four values: the type, the quantifier, the name, and a tuple of children. Children are simply additional content model descriptions.

The values of the first two fields are constants defined in the xml.parsers.expat.model module. These constants can be collected in two groups: the model type group and the quantifier group.

The constants in the model type group are:

xml.parsers.expat.model.XML_CTYPE_ANY

The element named by the model name was declared to have a content model of ANY.

xml.parsers.expat.model.XML_CTYPE_CHOICE

The named element allows a choice from a number of options; this is used for content models such as (A | B | C).

xml.parsers.expat.model.XML_CTYPE_EMPTY

Elements which are declared to be EMPTY have this model type.

xml.parsers.expat.model.XML_CTYPE_MIXED

xml.parsers.expat.model.XML_CTYPE_NAME

xml.parsers.expat.model.XML_CTYPE_SEQ

Models which represent a series of models which follow one after the other are indicated with this model type. This is used for models such as (A, B, C).

The constants in the quantifier group are:

xml.parsers.expat.model.XML_CQUANT_NONE

No modifier is given, so it can appear exactly once, as for A.

xml.parsers.expat.model.XML_CQUANT_OPT

The model is optional: it can appear once or not at all, as for A?.

xml.parsers.expat.model.XML_CQUANT_PLUS

The model must occur one or more times (like A+).

xml.parsers.expat.model.XML_CQUANT_REP

The model must occur zero or more times, as for A*.

20.13.5. Expat error constants

The following constants are provided in the xml.parsers.expat.errors module. These constants are useful in interpreting some of the attributes of the ExpatError exception objects raised when an error has occurred. Since for backwards compatibility reasons, the constants’ value is the error message and not the numeric error code, you do this by comparing its code attribute with errors.codes[errors.XML_ERROR_CONSTANT_NAME].

The errors module has the following attributes:

xml.parsers.expat.errors.codes

A dictionary mapping numeric error codes to their string descriptions.

3.2 新版功能.

xml.parsers.expat.errors.messages

A dictionary mapping string descriptions to their error codes.

3.2 新版功能.

xml.parsers.expat.errors.XML_ERROR_ASYNC_ENTITY

xml.parsers.expat.errors.XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF

An entity reference in an attribute value referred to an external entity instead of an internal entity.

xml.parsers.expat.errors.XML_ERROR_BAD_CHAR_REF

A character reference referred to a character which is illegal in XML (for example, character 0, or ‘&#0;’).

xml.parsers.expat.errors.XML_ERROR_BINARY_ENTITY_REF

An entity reference referred to an entity which was declared with a notation, so cannot be parsed.

xml.parsers.expat.errors.XML_ERROR_DUPLICATE_ATTRIBUTE

An attribute was used more than once in a start tag.

xml.parsers.expat.errors.XML_ERROR_INCORRECT_ENCODING

xml.parsers.expat.errors.XML_ERROR_INVALID_TOKEN

Raised when an input byte could not properly be assigned to a character; for example, a NUL byte (value 0) in a UTF-8 input stream.

xml.parsers.expat.errors.XML_ERROR_JUNK_AFTER_DOC_ELEMENT

Something other than whitespace occurred after the document element.

xml.parsers.expat.errors.XML_ERROR_MISPLACED_XML_PI

An XML declaration was found somewhere other than the start of the input data.

xml.parsers.expat.errors.XML_ERROR_NO_ELEMENTS

The document contains no elements (XML requires all documents to contain exactly one top-level element)..

xml.parsers.expat.errors.XML_ERROR_NO_MEMORY

Expat was not able to allocate memory internally.

xml.parsers.expat.errors.XML_ERROR_PARAM_ENTITY_REF

A parameter entity reference was found where it was not allowed.

xml.parsers.expat.errors.XML_ERROR_PARTIAL_CHAR

An incomplete character was found in the input.

xml.parsers.expat.errors.XML_ERROR_RECURSIVE_ENTITY_REF

An entity reference contained another reference to the same entity; possibly via a different name, and possibly indirectly.

xml.parsers.expat.errors.XML_ERROR_SYNTAX

Some unspecified syntax error was encountered.

xml.parsers.expat.errors.XML_ERROR_TAG_MISMATCH

An end tag did not match the innermost open start tag.

xml.parsers.expat.errors.XML_ERROR_UNCLOSED_TOKEN

Some token (such as a start tag) was not closed before the end of the stream or the next token was encountered.

xml.parsers.expat.errors.XML_ERROR_UNDEFINED_ENTITY

A reference was made to an entity which was not defined.

xml.parsers.expat.errors.XML_ERROR_UNKNOWN_ENCODING

The document encoding is not supported by Expat.

xml.parsers.expat.errors.XML_ERROR_UNCLOSED_CDATA_SECTION

A CDATA marked section was not closed.

xml.parsers.expat.errors.XML_ERROR_EXTERNAL_ENTITY_HANDLING

xml.parsers.expat.errors.XML_ERROR_NOT_STANDALONE

The parser determined that the document was not “standalone” though it declared itself to be in the XML declaration, and the NotStandaloneHandler was set and returned 0.

xml.parsers.expat.errors.XML_ERROR_UNEXPECTED_STATE

xml.parsers.expat.errors.XML_ERROR_ENTITY_DECLARED_IN_PE

xml.parsers.expat.errors.XML_ERROR_FEATURE_REQUIRES_XML_DTD

An operation was requested that requires DTD support to be compiled in, but Expat was configured without DTD support. This should never be reported by a standard build of the xml.parsers.expat module.

xml.parsers.expat.errors.XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING

A behavioral change was requested after parsing started that can only be changed before parsing has started. This is (currently) only raised by UseForeignDTD().

xml.parsers.expat.errors.XML_ERROR_UNBOUND_PREFIX

An undeclared prefix was found when namespace processing was enabled.

xml.parsers.expat.errors.XML_ERROR_UNDECLARING_PREFIX

The document attempted to remove the namespace declaration associated with a prefix.

xml.parsers.expat.errors.XML_ERROR_INCOMPLETE_PE

A parameter entity contained incomplete markup.

xml.parsers.expat.errors.XML_ERROR_XML_DECL

The document contained no document element at all.

xml.parsers.expat.errors.XML_ERROR_TEXT_DECL

There was an error parsing a text declaration in an external entity.

xml.parsers.expat.errors.XML_ERROR_PUBLICID

Characters were found in the public id that are not allowed.

xml.parsers.expat.errors.XML_ERROR_SUSPENDED

The requested operation was made on a suspended parser, but isn’t allowed. This includes attempts to provide additional input or to stop the parser.

xml.parsers.expat.errors.XML_ERROR_NOT_SUSPENDED

An attempt to resume the parser was made when the parser had not been suspended.

xml.parsers.expat.errors.XML_ERROR_ABORTED

This should not be reported to Python applications.

xml.parsers.expat.errors.XML_ERROR_FINISHED

The requested operation was made on a parser which was finished parsing input, but isn’t allowed. This includes attempts to provide additional input or to stop the parser.

xml.parsers.expat.errors.XML_ERROR_SUSPEND_PE

备注

1

The encoding string included in XML output should conform to the appropriate standards. For example, “UTF-8” is valid, but “UTF8” is not. See https://www.w3.org/TR/2006/REC-xml11-20060816/#NT-EncodingDecl and https://www.iana.org/assignments/character-sets/character-sets.xhtml.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

20. 结构化标记处理工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

21. 互联网协议和支持

本章介绍的模块实现了互联网协议并支持相关技术。 它们都是用 Python 实现的。 这些模块中的大多数都需要存在依赖于系统的模块 socket ，目前大多数流行平台都支持它。 这是一个概述：

21.1. webbrowser — 方便的Web浏览器控制器

21.1.1. 浏览器控制器对象

21.2. cgi — Common Gateway Interface support

21.2.1. 概述

21.2.2. 使用cgi模块。

21.2.3. Higher Level Interface

21.2.4. 函数

21.2.5. Caring about security

21.2.6. Installing your CGI script on a Unix system

21.2.7. Testing your CGI script

21.2.8. Debugging CGI scripts

21.2.9. Common problems and solutions

21.3. cgitb — 用于 CGI 脚本的回溯管理器

21.4. wsgiref — WSGI Utilities and Reference Implementation

21.4.1. wsgiref.util – WSGI environment utilities

21.4.2. wsgiref.headers – WSGI response header tools

21.4.3. wsgiref.simple_server – a simple WSGI HTTP server

21.4.4. wsgiref.validate — WSGI conformance checker

21.4.5. wsgiref.handlers – server/gateway base classes

21.4.6. 例子

21.5. urllib — URL 处理模块

21.6. urllib.request — 用于打开 URL 的可扩展库

21.6.1. Request 对象

21.6.2. OpenerDirector 对象

21.6.3. BaseHandler 对象

21.6.4. HTTPRedirectHandler 对象

21.6.5. HTTPCookieProcessor 对象

21.6.6. ProxyHandler 对象

21.6.7. HTTPPasswordMgr 对象

21.6.8. HTTPPasswordMgrWithPriorAuth 对象

21.6.9. AbstractBasicAuthHandler 对象

21.6.10. HTTPBasicAuthHandler 对象

21.6.11. ProxyBasicAuthHandler 对象

21.6.12. AbstractDigestAuthHandler 对象

21.6.13. HTTPDigestAuthHandler 对象

21.6.14. ProxyDigestAuthHandler 对象

21.6.15. HTTPHandler 对象

21.6.16. HTTPSHandler 对象

21.6.17. FileHandler 对象

21.6.18. DataHandler 对象

21.6.19. FTPHandler 对象

21.6.20. CacheFTPHandler 对象

21.6.21. UnknownHandler 对象

21.6.22. HTTPErrorProcessor 对象

21.6.23. 例子

21.6.24. Legacy interface

21.6.25. urllib.request Restrictions

21.7. urllib.response — urllib 使用的 Response 类

21.8. urllib.parse — Parse URLs into components

21.8.1. URL 解析

21.8.2. 解析ASCII编码字节

21.8.3. 结构化解析结果

21.8.4. URL Quoting

21.9. urllib.error — urllib.request 引发的异常类

21.10. urllib.robotparser — robots.txt 语法分析程序

21.11. http — HTTP 模块

21.11.1. HTTP 状态码

21.12. http.client — HTTP 协议客户端

21.12.1. HTTPConnection 对象

21.12.2. HTTPResponse 对象

21.12.3. 例子

21.12.4. HTTPMessage Objects

21.13. ftplib — FTP 协议客户端

21.13.1. FTP 对象

21.13.2. FTP_TLS 对象

21.14. poplib — POP3 协议客户端

21.14.1. POP3 对象

21.14.2. POP3 示例

21.15. imaplib — IMAP4 协议客户端

21.15.1. IMAP4 Objects

21.15.2. IMAP4 Example

21.16. nntplib — NNTP protocol client

21.16.1. NNTP Objects

21.16.1.1. Attributes

21.16.1.2. 方法

21.16.2. 工具函数

21.17. smtplib —SMTP协议客户端

21.17.1. SMTP Objects

21.17.2. SMTP Example

21.18. smtpd — SMTP 服务器

21.18.1. SMTPServer 对象

21.18.2. DebuggingServer 对象

21.18.3. PureProxy对象

21.18.4. MailmanProxy 对象

21.18.5. SMTPChannel 对象

21.19. telnetlib — Telnet client

21.19.1. Telnet Objects

21.19.2. Telnet Example

21.20. uuid — UUID objects according to RFC 4122

21.20.1. 示例

21.21. socketserver — A framework for network servers

21.21.1. Server Creation Notes

21.21.2. Server 对象

21.21.3. Request Handler Objects

21.21.4. 例子

21.21.4.1. socketserver.TCPServer Example

21.21.4.2. socketserver.UDPServer Example

21.21.4.3. Asynchronous Mixins

21.22. http.server — HTTP 服务器

21.23. http.cookies — HTTP状态管理

21.23.1. Cookie 对象

21.23.2. Morsel 对象

21.23.3. 示例

21.24. http.cookiejar —— HTTP 客户端的 Cookie 处理

21.24.1. CookieJar 和 FileCookieJar 对象

21.24.2. FileCookieJar subclasses and co-operation with web browsers

21.24.3. CookiePolicy 对象

21.24.4. DefaultCookiePolicy 对象

21.24.5. Cookie 对象

21.24.6. 例子

21.25. xmlrpc — XMLRPC 服务端与客户端模块

21.26. xmlrpc.client — XML-RPC client access

21.26.1. ServerProxy 对象

21.26.2. DateTime 对象

21.26.3. Binary 对象

21.26.4. Fault 对象

21.26.5. ProtocolError 对象

21.26.6. MultiCall 对象

21.26.7. Convenience Functions

21.26.8. Example of Client Usage

21.26.9. Example of Client and Server Usage

21.27. xmlrpc.server — Basic XML-RPC servers

21.27.1. SimpleXMLRPCServer Objects

21.27.1.1. SimpleXMLRPCServer Example

21.27.2. CGIXMLRPCRequestHandler

21.27.3. Documenting XMLRPC server

21.27.4. DocXMLRPCServer Objects

21.27.5. DocCGIXMLRPCRequestHandler

21.28. ipaddress — IPv4/IPv6 操作库

21.28.1. 方便的工厂函数

21.28.2. IP 地址

21.28.2.1. 地址对象

21.28.2.2. Conversion to Strings and Integers

21.28.2.3. 运算符

21.28.2.3.1. Comparison operators

21.28.2.3.2. Arithmetic operators

21.28.3. IP Network definitions

21.28.3.1. Prefix, net mask and host mask

21.28.3.2. Network objects

21.28.3.3. 运算符

21.28.3.3.1. Logical operators

21.28.3.3.2. 迭代

21.28.3.3.3. Networks as containers of addresses

21.28.4. Interface objects

21.28.4.1. 运算符

21.28.4.1.1. Logical operators

21.28.5. Other Module Level Functions

21.28.6. Custom Exceptions

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.1. webbrowser — 方便的Web浏览器控制器

源码： Lib/webbrowser.py [https://github.com/python/cpython/tree/3.6/Lib/webbrowser.py]

webbrowser 模块提供了一个高级接口，允许向用户显示基于Web的文档。 在大多数情况下，只需从该模块调用 open() 函数就可以了。

在 Unix 下，图形浏览器在 X11 下是首选，但如果图形浏览器不可用或 X11 显示不可用，则将使用文本模式浏览器。 如果使用文本模式浏览器，则调用进程将阻塞，直到用户退出浏览器。

如果存在环境变量 BROWSER ，则将其解释为 os.pathsep 分隔的浏览器列表，以便在平台默认值之前尝试。 当列表部分的值包含字符串 ％s 时，它被解释为一个文字浏览器命令行，用于替换 ％s 的参数 URL ；如果该部分不包含 ％s，则它只被解释为要启动的浏览器的名称。 1

对于非 Unix 平台，或者当 Unix 上有远程浏览器时，控制过程不会等待用户完成浏览器，而是允许远程浏览器在显示界面上维护自己的窗口。 如果 Unix 上没有远程浏览器，控制进程将启动一个新的浏览器并等待。

脚本 webbrowser 可以用作模块的命令行界面。它接受一个 URL 作为参数。还接受以下可选参数：-n 如果可能，在新的浏览器窗口中打开 URL ； -t 在新的浏览器页面（“标签”）中打开 URL。这些选择当然是相互排斥的。用法示例:

python -m webbrowser -t "http://www.python.org"

定义了以下异常：

exception webbrowser.Error

发生浏览器控件错误时引发异常。

定义了以下函数：

webbrowser.open(url, new=0, autoraise=True)

使用默认浏览器显示 url。 如果 new 为 0，则尽可能在同一浏览器窗口中打开 url。 如果 new 为 1，则尽可能打开新的浏览器窗口。 如果 new 为 2，则尽可能打开新的浏览器页面（“标签”）。 如果 autoraise 为 “True”，则会尽可能置前窗口（请注意，在许多窗口管理器下，无论此变量的设置如何，都会置前窗口）。

请注意，在某些平台上，尝试使用此函数打开文件名，可能会起作用并启动操作系统的关联程序。 但是，这种方式不被支持也不可移植。

webbrowser.open_new(url)

如果可能，在默认浏览器的新窗口中打开 url，否则，在唯一的浏览器窗口中打开 url。

webbrowser.open_new_tab(url)

如果可能，在默认浏览器的新页面（“标签”）中打开 url，否则等效于 open_new()。

webbrowser.get(using=None)

返回浏览器类型为 using 指定的控制器对象。 如果 using 为 None，则返回适用于调用者环境的默认浏览器的控制器。

webbrowser.register(name, constructor, instance=None)

注册 name 浏览器类型。 注册浏览器类型后， get() 函数可以返回该浏览器类型的控制器。 如果没有提供 instance，或者为 None，constructor 将在没有参数的情况下被调用，以在需要时创建实例。 如果提供了 instance，则永远不会调用 constructor，并且可能是 None。

This entry point is only useful if you plan to either set the BROWSER variable or call get() with a nonempty argument matching the name of a handler you declare.

预定义了许多浏览器类型。 此表给出了可以传递给 get() 函数的类型名称以及控制器类的相应实例化，这些都在此模块中定义。

类型名

类名

注释

'mozilla'

Mozilla('mozilla')

'firefox'

Mozilla('mozilla')

'netscape'

Mozilla('netscape')

'galeon'

Galeon('galeon')

'epiphany'

Galeon('epiphany')

'skipstone'

BackgroundBrowser('skipstone')

'kfmclient'

Konqueror()

(1)

'konqueror'

Konqueror()

(1)

'kfm'

Konqueror()

(1)

'mosaic'

BackgroundBrowser('mosaic')

'opera'

Opera()

'grail'

Grail()

'links'

GenericBrowser('links')

'elinks'

Elinks('elinks')

'lynx'

GenericBrowser('lynx')

'w3m'

GenericBrowser('w3m')

'windows-default'

WindowsDefault

(2)

'macosx'

MacOSX('default')

(3)

'safari'

MacOSX('safari')

(3)

'google-chrome'

Chrome('google-chrome')

'chrome'

Chrome('chrome')

'chromium'

Chromium('chromium')

'chromium-browser'

Chromium('chromium-browser')

注释:

“Konqueror” 是 Unix 的 KDE 桌面环境的文件管理器，只有在 KDE 运行时才有意义。 一些可靠地检测 KDE 的方法会很好；仅检查 KDEDIR 变量是不够的。 另请注意，KDE 2的 konqueror 命令，会使用名称 “kfm”—此实现选择运行的 Konqueror 的最佳策略。

仅限 Windows 平台。

仅限 Mac OS X 平台。

3.3 新版功能: 添加了对 Chrome/Chromium 的支持。

以下是一些简单的例子:

url = 'http://docs.python.org/'

# Open URL in a new tab, if a browser window is already open.
webbrowser.open_new_tab(url)

# Open URL in new window, raising the window if possible.
webbrowser.open_new(url)

21.1.1. 浏览器控制器对象

浏览器控制器提供三个与模块级便捷函数相同的方法：

controller.open(url, new=0, autoraise=True)

使用此控制器处理的浏览器显示 url。 如果 new 为 1，则尽可能打开新的浏览器窗口。 如果 new 为 2，则尽可能打开新的浏览器页面（“标签”）。

controller.open_new(url)

如果可能，在此控制器处理的浏览器的新窗口中打开 url ，否则，在唯一的浏览器窗口中打开 url 。 别名 open_new()。

controller.open_new_tab(url)

如果可能，在此控制器处理的浏览器的新页面（“标签”）中打开 url，否则等效于 open_new()。

备注

1

这里命名的不带完整路径的可执行文件将在 PATH 环境变量给出的目录中搜索。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.2. cgi — Common Gateway Interface support

源代码： Lib/cgi.py [https://github.com/python/cpython/tree/3.6/Lib/cgi.py]

Support module for Common Gateway Interface (CGI) scripts.

This module defines a number of utilities for use by CGI scripts written in Python.

21.2.1. 概述

A CGI script is invoked by an HTTP server, usually to process user input submitted through an HTML <FORM> or <ISINDEX> element.

Most often, CGI scripts live in the server’s special cgi-bin directory. The HTTP server places all sorts of information about the request (such as the client’s hostname, the requested URL, the query string, and lots of other goodies) in the script’s shell environment, executes the script, and sends the script’s output back to the client.

The script’s input is connected to the client too, and sometimes the form data is read this way; at other times the form data is passed via the “query string” part of the URL. This module is intended to take care of the different cases and provide a simpler interface to the Python script. It also provides a number of utilities that help in debugging scripts, and the latest addition is support for file uploads from a form (if your browser supports it).

The output of a CGI script should consist of two sections, separated by a blank line. The first section contains a number of headers, telling the client what kind of data is following. Python code to generate a minimal header section looks like this:

print("Content-Type: text/html")    # HTML is following
print()                             # blank line, end of headers

The second section is usually HTML, which allows the client software to display nicely formatted text with header, in-line images, etc. Here’s Python code that prints a simple piece of HTML:

print("<TITLE>CGI script output</TITLE>")
print("<H1>This is my first CGI script</H1>")
print("Hello, world!")

21.2.2. 使用cgi模块。

通过敲下 import cgi 来开始。

当你在写一个新脚本时，考虑加上这些语句:

import cgitb
cgitb.enable()

This activates a special exception handler that will display detailed reports in the Web browser if any errors occur. If you’d rather not show the guts of your program to users of your script, you can have the reports saved to files instead, with code like this:

import cgitb
cgitb.enable(display=0, logdir="/path/to/logdir")

It’s very helpful to use this feature during script development. The reports produced by cgitb provide information that can save you a lot of time in tracking down bugs. You can always remove the cgitb line later when you have tested your script and are confident that it works correctly.

To get at submitted form data, use the FieldStorage class. If the form contains non-ASCII characters, use the encoding keyword parameter set to the value of the encoding defined for the document. It is usually contained in the META tag in the HEAD section of the HTML document or by the Content-Type header). This reads the form contents from the standard input or the environment (depending on the value of various environment variables set according to the CGI standard). Since it may consume standard input, it should be instantiated only once.

The FieldStorage instance can be indexed like a Python dictionary. It allows membership testing with the in operator, and also supports the standard dictionary method keys() and the built-in function len(). Form fields containing empty strings are ignored and do not appear in the dictionary; to keep such values, provide a true value for the optional keep_blank_values keyword parameter when creating the FieldStorage instance.

For instance, the following code (which assumes that the Content-Type header and blank line have already been printed) checks that the fields name and addr are both set to a non-empty string:

form = cgi.FieldStorage()
if "name" not in form or "addr" not in form:
    print("<H1>Error</H1>")
    print("Please fill in the name and addr fields.")
    return
print("<p>name:", form["name"].value)
print("<p>addr:", form["addr"].value)
...further form processing here...

Here the fields, accessed through form[key], are themselves instances of FieldStorage (or MiniFieldStorage, depending on the form encoding). The value attribute of the instance yields the string value of the field. The getvalue() method returns this string value directly; it also accepts an optional second argument as a default to return if the requested key is not present.

If the submitted form data contains more than one field with the same name, the object retrieved by form[key] is not a FieldStorage or MiniFieldStorage instance but a list of such instances. Similarly, in this situation, form.getvalue(key) would return a list of strings. If you expect this possibility (when your HTML form contains multiple fields with the same name), use the getlist() method, which always returns a list of values (so that you do not need to special-case the single item case). For example, this code concatenates any number of username fields, separated by commas:

value = form.getlist("username")
usernames = ",".join(value)

If a field represents an uploaded file, accessing the value via the value attribute or the getvalue() method reads the entire file in memory as bytes. This may not be what you want. You can test for an uploaded file by testing either the filename attribute or the file attribute. You can then read the data from the file attribute before it is automatically closed as part of the garbage collection of the FieldStorage instance (the read() and readline() methods will return bytes):

fileitem = form["userfile"]
if fileitem.file:
    # It's an uploaded file; count lines
    linecount = 0
    while True:
        line = fileitem.file.readline()
        if not line: break
        linecount = linecount + 1

FieldStorage objects also support being used in a with statement, which will automatically close them when done.

If an error is encountered when obtaining the contents of an uploaded file (for example, when the user interrupts the form submission by clicking on a Back or Cancel button) the done attribute of the object for the field will be set to the value -1.

The file upload draft standard entertains the possibility of uploading multiple files from one field (using a recursive multipart/* encoding). When this occurs, the item will be a dictionary-like FieldStorage item. This can be determined by testing its type attribute, which should be multipart/form-data (or perhaps another MIME type matching multipart/*). In this case, it can be iterated over recursively just like the top-level form object.

When a form is submitted in the “old” format (as the query string or as a single data part of type application/x-www-form-urlencoded), the items will actually be instances of the class MiniFieldStorage. In this case, the list, file, and filename attributes are always None.

A form submitted via POST that also has a query string will contain both FieldStorage and MiniFieldStorage items.

在 3.4 版更改: The file attribute is automatically closed upon the garbage collection of the creating FieldStorage instance.

在 3.5 版更改: Added support for the context management protocol to the FieldStorage class.

21.2.3. Higher Level Interface

The previous section explains how to read CGI form data using the FieldStorage class. This section describes a higher level interface which was added to this class to allow one to do it in a more readable and intuitive way. The interface doesn’t make the techniques described in previous sections obsolete — they are still useful to process file uploads efficiently, for example.

The interface consists of two simple methods. Using the methods you can process form data in a generic way, without the need to worry whether only one or more values were posted under one name.

In the previous section, you learned to write following code anytime you expected a user to post more than one value under one name:

item = form.getvalue("item")
if isinstance(item, list):
    # The user is requesting more than one item.
else:
    # The user is requesting only one item.

This situation is common for example when a form contains a group of multiple checkboxes with the same name:

<input type="checkbox" name="item" value="1" />
<input type="checkbox" name="item" value="2" />

In most situations, however, there’s only one form control with a particular name in a form and then you expect and need only one value associated with this name. So you write a script containing for example this code:

user = form.getvalue("user").upper()

The problem with the code is that you should never expect that a client will provide valid input to your scripts. For example, if a curious user appends another user=foo pair to the query string, then the script would crash, because in this situation the getvalue("user") method call returns a list instead of a string. Calling the upper() method on a list is not valid (since lists do not have a method of this name) and results in an AttributeError exception.

Therefore, the appropriate way to read form data values was to always use the code which checks whether the obtained value is a single value or a list of values. That’s annoying and leads to less readable scripts.

A more convenient approach is to use the methods getfirst() and getlist() provided by this higher level interface.

FieldStorage.getfirst(name, default=None)

This method always returns only one value associated with form field name. The method returns only the first value in case that more values were posted under such name. Please note that the order in which the values are received may vary from browser to browser and should not be counted on. 1 If no such form field or value exists then the method returns the value specified by the optional parameter default. This parameter defaults to None if not specified.

FieldStorage.getlist(name)

This method always returns a list of values associated with form field name. The method returns an empty list if no such form field or value exists for name. It returns a list consisting of one item if only one such value exists.

Using these methods you can write nice compact code:

import cgi
form = cgi.FieldStorage()
user = form.getfirst("user", "").upper()    # This way it's safe.
for item in form.getlist("item"):
    do_something(item)

21.2.4. 函数

These are useful if you want more control, or if you want to employ some of the algorithms implemented in this module in other circumstances.

cgi.parse(fp=None, environ=os.environ, keep_blank_values=False, strict_parsing=False, separator="&")

Parse a query in the environment or from a file (the file defaults to sys.stdin). The keep_blank_values, strict_parsing and separator parameters are passed to urllib.parse.parse_qs() unchanged.

cgi.parse_qs(qs, keep_blank_values=False, strict_parsing=False)

This function is deprecated in this module. Use urllib.parse.parse_qs() instead. It is maintained here only for backward compatibility.

cgi.parse_qsl(qs, keep_blank_values=False, strict_parsing=False)

This function is deprecated in this module. Use urllib.parse.parse_qsl() instead. It is maintained here only for backward compatibility.

cgi.parse_multipart(fp, pdict)

Parse input of type multipart/form-data (for file uploads). Arguments are fp for the input file and pdict for a dictionary containing other parameters in the Content-Type header.

Returns a dictionary just like urllib.parse.parse_qs() keys are the field names, each value is a list of values for that field. This is easy to use but not much good if you are expecting megabytes to be uploaded — in that case, use the FieldStorage class instead which is much more flexible.

Note that this does not parse nested multipart parts — use FieldStorage for that.

在 3.6.13 版更改: Added the separator parameter.

cgi.parse_header(string)

Parse a MIME header (such as Content-Type) into a main value and a dictionary of parameters.

cgi.test()

Robust test CGI script, usable as main program. Writes minimal HTTP headers and formats all information provided to the script in HTML form.

cgi.print_environ()

Format the shell environment in HTML.

cgi.print_form(form)

Format a form in HTML.

cgi.print_directory()

Format the current directory in HTML.

cgi.print_environ_usage()

Print a list of useful (used by CGI) environment variables in HTML.

cgi.escape(s, quote=False)

Convert the characters '&', '<' and '>' in string s to HTML-safe sequences. Use this if you need to display text that might contain such characters in HTML. If the optional flag quote is true, the quotation mark character (") is also translated; this helps for inclusion in an HTML attribute value delimited by double quotes, as in <a href="...">. Note that single quotes are never translated.

3.2 版后已移除: This function is unsafe because quote is false by default, and therefore deprecated. Use html.escape() instead.

21.2.5. Caring about security

There’s one important rule: if you invoke an external program (via the os.system() or os.popen() functions. or others with similar functionality), make very sure you don’t pass arbitrary strings received from the client to the shell. This is a well-known security hole whereby clever hackers anywhere on the Web can exploit a gullible CGI script to invoke arbitrary shell commands. Even parts of the URL or field names cannot be trusted, since the request doesn’t have to come from your form!

To be on the safe side, if you must pass a string gotten from a form to a shell command, you should make sure the string contains only alphanumeric characters, dashes, underscores, and periods.

21.2.6. Installing your CGI script on a Unix system

Read the documentation for your HTTP server and check with your local system administrator to find the directory where CGI scripts should be installed; usually this is in a directory cgi-bin in the server tree.

Make sure that your script is readable and executable by “others”; the Unix file mode should be 0o755 octal (use chmod 0755 filename). Make sure that the first line of the script contains #! starting in column 1 followed by the pathname of the Python interpreter, for instance:

#!/usr/local/bin/python

Make sure the Python interpreter exists and is executable by “others”.

Make sure that any files your script needs to read or write are readable or writable, respectively, by “others” — their mode should be 0o644 for readable and 0o666 for writable. This is because, for security reasons, the HTTP server executes your script as user “nobody”, without any special privileges. It can only read (write, execute) files that everybody can read (write, execute). The current directory at execution time is also different (it is usually the server’s cgi-bin directory) and the set of environment variables is also different from what you get when you log in. In particular, don’t count on the shell’s search path for executables (PATH) or the Python module search path (PYTHONPATH) to be set to anything interesting.

If you need to load modules from a directory which is not on Python’s default module search path, you can change the path in your script, before importing other modules. For example:

import sys
sys.path.insert(0, "/usr/home/joe/lib/python")
sys.path.insert(0, "/usr/local/lib/python")

(This way, the directory inserted last will be searched first!)

Instructions for non-Unix systems will vary; check your HTTP server’s documentation (it will usually have a section on CGI scripts).

21.2.7. Testing your CGI script

Unfortunately, a CGI script will generally not run when you try it from the command line, and a script that works perfectly from the command line may fail mysteriously when run from the server. There’s one reason why you should still test your script from the command line: if it contains a syntax error, the Python interpreter won’t execute it at all, and the HTTP server will most likely send a cryptic error to the client.

Assuming your script has no syntax errors, yet it does not work, you have no choice but to read the next section.

21.2.8. Debugging CGI scripts

First of all, check for trivial installation errors — reading the section above on installing your CGI script carefully can save you a lot of time. If you wonder whether you have understood the installation procedure correctly, try installing a copy of this module file (cgi.py) as a CGI script. When invoked as a script, the file will dump its environment and the contents of the form in HTML form. Give it the right mode etc, and send it a request. If it’s installed in the standard cgi-bin directory, it should be possible to send it a request by entering a URL into your browser of the form:

http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home

If this gives an error of type 404, the server cannot find the script – perhaps you need to install it in a different directory. If it gives another error, there’s an installation problem that you should fix before trying to go any further. If you get a nicely formatted listing of the environment and form content (in this example, the fields should be listed as “addr” with value “At Home” and “name” with value “Joe Blow”), the cgi.py script has been installed correctly. If you follow the same procedure for your own script, you should now be able to debug it.

The next step could be to call the cgi module’s test() function from your script: replace its main code with the single statement

cgi.test()

This should produce the same results as those gotten from installing the cgi.py file itself.

When an ordinary Python script raises an unhandled exception (for whatever reason: of a typo in a module name, a file that can’t be opened, etc.), the Python interpreter prints a nice traceback and exits. While the Python interpreter will still do this when your CGI script raises an exception, most likely the traceback will end up in one of the HTTP server’s log files, or be discarded altogether.

Fortunately, once you have managed to get your script to execute some code, you can easily send tracebacks to the Web browser using the cgitb module. If you haven’t done so already, just add the lines:

import cgitb
cgitb.enable()

to the top of your script. Then try running it again; when a problem occurs, you should see a detailed report that will likely make apparent the cause of the crash.

If you suspect that there may be a problem in importing the cgitb module, you can use an even more robust approach (which only uses built-in modules):

import sys
sys.stderr = sys.stdout
print("Content-Type: text/plain")
print()
...your code here...

This relies on the Python interpreter to print the traceback. The content type of the output is set to plain text, which disables all HTML processing. If your script works, the raw HTML will be displayed by your client. If it raises an exception, most likely after the first two lines have been printed, a traceback will be displayed. Because no HTML interpretation is going on, the traceback will be readable.

21.2.9. Common problems and solutions

Most HTTP servers buffer the output from CGI scripts until the script is completed. This means that it is not possible to display a progress report on the client’s display while the script is running.

Check the installation instructions above.

Check the HTTP server’s log files. (tail -f logfile in a separate window may be useful!)

Always check a script for syntax errors first, by doing something like python script.py.

If your script does not have any syntax errors, try adding import cgitb; cgitb.enable() to the top of the script.

When invoking external programs, make sure they can be found. Usually, this means using absolute path names — PATH is usually not set to a very useful value in a CGI script.

When reading or writing external files, make sure they can be read or written by the userid under which your CGI script will be running: this is typically the userid under which the web server is running, or some explicitly specified userid for a web server’s suexec feature.

Don’t try to give a CGI script a set-uid mode. This doesn’t work on most systems, and is a security liability as well.

备注

1

Note that some recent versions of the HTML specification do state what order the field values should be supplied in, but knowing whether a request was received from a conforming browser, or even from a browser at all, is tedious and error-prone.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.3. cgitb — 用于 CGI 脚本的回溯管理器

源代码: Lib/cgitb.py [https://github.com/python/cpython/tree/3.6/Lib/cgitb.py]

cgitb 模块提供了用于 Python 脚本的特殊异常处理程序。 （这个名称有一点误导性。 它最初是设计用来显示 HTML 格式的 CGI 脚本详细回溯信息。 但后来被一般化为也可显示纯文本格式的回溯信息。） 激活这个模块之后，如果发生了未被捕获的异常，将会显示详细的已格式化的报告。 报告显示内容包括每个层级的源代码摘录，还有当前正在运行的函数的参数和局部变量值，以帮助你调试问题。 你也可以选择将此信息保存至文件而不是将其发送至浏览器。

要启用此特性，只需简单地将此代码添加到你的 CGI 脚本的最顶端:

import cgitb
cgitb.enable()

enable() 函数的选项可以控制是将报告显示在浏览器中，还是将报告记录到文件以供随后进行分析。

cgitb.enable(display=1, logdir=None, context=5, format="html")

此函数可通过设置 sys.excepthook 的值以使 cgitb 模块接管解释器默认的异常处理机制。

可选参数 display 默认为 1 并可被设为 0 来停止将回溯发送至浏览器。 如果给出了参数 logdir，则回溯会被写入文件。 logdir 的值应当是一个用于存放所写入文件的目录。 可选参数 context 是要在回溯中的当前源代码行前后显示的上下文行数；默认为 5。 如果可选参数 format 为 "html"，输出将为 HTML 格式。 任何其它值都会强制启用纯文本输出。 默认取值为 "html"。

cgitb.handler(info=None)

此函数使用默认设置处理异常（即在浏览器中显示报告，但不记录到文件）。 当你捕获了一个异常并希望使用 cgitb 来报告它时可以使用此函数。 可选的 info 参数应为一个包含异常类型，异常值和回溯对象的 3 元组，与 sys.exc_info() 所返回的元组完全一致。 如果未提供 info 参数，则会从 sys.exc_info() 获取当前异常。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.4. wsgiref — WSGI Utilities and Reference Implementation

The Web Server Gateway Interface (WSGI) is a standard interface between web server software and web applications written in Python. Having a standard interface makes it easy to use an application that supports WSGI with a number of different web servers.

Only authors of web servers and programming frameworks need to know every detail and corner case of the WSGI design. You don’t need to understand every detail of WSGI just to install a WSGI application or to write a web application using an existing framework.

wsgiref is a reference implementation of the WSGI specification that can be used to add WSGI support to a web server or framework. It provides utilities for manipulating WSGI environment variables and response headers, base classes for implementing WSGI servers, a demo HTTP server that serves WSGI applications, and a validation tool that checks WSGI servers and applications for conformance to the WSGI specification (PEP 3333 [https://www.python.org/dev/peps/pep-3333]).

See https://wsgi.readthedocs.org/ for more information about WSGI, and links to tutorials and other resources.

21.4.1. wsgiref.util – WSGI environment utilities

This module provides a variety of utility functions for working with WSGI environments. A WSGI environment is a dictionary containing HTTP request variables as described in PEP 3333 [https://www.python.org/dev/peps/pep-3333]. All of the functions taking an environ parameter expect a WSGI-compliant dictionary to be supplied; please see PEP 3333 [https://www.python.org/dev/peps/pep-3333] for a detailed specification.

wsgiref.util.guess_scheme(environ)

Return a guess for whether wsgi.url_scheme should be “http” or “https”, by checking for a HTTPS environment variable in the environ dictionary. The return value is a string.

This function is useful when creating a gateway that wraps CGI or a CGI-like protocol such as FastCGI. Typically, servers providing such protocols will include a HTTPS variable with a value of “1” “yes”, or “on” when a request is received via SSL. So, this function returns “https” if such a value is found, and “http” otherwise.

wsgiref.util.request_uri(environ, include_query=True)

Return the full request URI, optionally including the query string, using the algorithm found in the “URL Reconstruction” section of PEP 3333 [https://www.python.org/dev/peps/pep-3333]. If include_query is false, the query string is not included in the resulting URI.

wsgiref.util.application_uri(environ)

Similar to request_uri(), except that the PATH_INFO and QUERY_STRING variables are ignored. The result is the base URI of the application object addressed by the request.

wsgiref.util.shift_path_info(environ)

Shift a single name from PATH_INFO to SCRIPT_NAME and return the name. The environ dictionary is modified in-place; use a copy if you need to keep the original PATH_INFO or SCRIPT_NAME intact.

If there are no remaining path segments in PATH_INFO, None is returned.

Typically, this routine is used to process each portion of a request URI path, for example to treat the path as a series of dictionary keys. This routine modifies the passed-in environment to make it suitable for invoking another WSGI application that is located at the target URI. For example, if there is a WSGI application at /foo, and the request URI path is /foo/bar/baz, and the WSGI application at /foo calls shift_path_info(), it will receive the string “bar”, and the environment will be updated to be suitable for passing to a WSGI application at /foo/bar. That is, SCRIPT_NAME will change from /foo to /foo/bar, and PATH_INFO will change from /bar/baz to /baz.

When PATH_INFO is just a “/”, this routine returns an empty string and appends a trailing slash to SCRIPT_NAME, even though empty path segments are normally ignored, and SCRIPT_NAME doesn’t normally end in a slash. This is intentional behavior, to ensure that an application can tell the difference between URIs ending in /x from ones ending in /x/ when using this routine to do object traversal.

wsgiref.util.setup_testing_defaults(environ)

Update environ with trivial defaults for testing purposes.

This routine adds various parameters required for WSGI, including HTTP_HOST, SERVER_NAME, SERVER_PORT, REQUEST_METHOD, SCRIPT_NAME, PATH_INFO, and all of the PEP 3333 [https://www.python.org/dev/peps/pep-3333]-defined wsgi.* variables. It only supplies default values, and does not replace any existing settings for these variables.

This routine is intended to make it easier for unit tests of WSGI servers and applications to set up dummy environments. It should NOT be used by actual WSGI servers or applications, since the data is fake!

用法示例：

from wsgiref.util import setup_testing_defaults
from wsgiref.simple_server import make_server

# A relatively simple WSGI application. It's going to print out the
# environment dictionary after being updated by setup_testing_defaults
def simple_app(environ, start_response):
    setup_testing_defaults(environ)

    status = '200 OK'
    headers = [('Content-type', 'text/plain; charset=utf-8')]

    start_response(status, headers)

    ret = [("%s: %s\n" % (key, value)).encode("utf-8")
           for key, value in environ.items()]
    return ret

with make_server('', 8000, simple_app) as httpd:
    print("Serving on port 8000...")
    httpd.serve_forever()

In addition to the environment functions above, the wsgiref.util module also provides these miscellaneous utilities:

wsgiref.util.is_hop_by_hop(header_name)

Return true if ‘header_name’ is an HTTP/1.1 “Hop-by-Hop” header, as defined by RFC 2616 [https://tools.ietf.org/html/rfc2616.html].

class wsgiref.util.FileWrapper(filelike, blksize=8192)

A wrapper to convert a file-like object to an iterator. The resulting objects support both __getitem__() and __iter__() iteration styles, for compatibility with Python 2.1 and Jython. As the object is iterated over, the optional blksize parameter will be repeatedly passed to the filelike object’s read() method to obtain bytestrings to yield. When read() returns an empty bytestring, iteration is ended and is not resumable.

If filelike has a close() method, the returned object will also have a close() method, and it will invoke the filelike object’s close() method when called.

用法示例：

from io import StringIO
from wsgiref.util import FileWrapper

# We're using a StringIO-buffer for as the file-like object
filelike = StringIO("This is an example file-like object"*10)
wrapper = FileWrapper(filelike, blksize=5)

for chunk in wrapper:
    print(chunk)

21.4.2. wsgiref.headers – WSGI response header tools

This module provides a single class, Headers, for convenient manipulation of WSGI response headers using a mapping-like interface.

class wsgiref.headers.Headers([headers])

Create a mapping-like object wrapping headers, which must be a list of header name/value tuples as described in PEP 3333 [https://www.python.org/dev/peps/pep-3333]. The default value of headers is an empty list.

Headers objects support typical mapping operations including __getitem__(), get(), __setitem__(), setdefault(), __delitem__() and __contains__(). For each of these methods, the key is the header name (treated case-insensitively), and the value is the first value associated with that header name. Setting a header deletes any existing values for that header, then adds a new value at the end of the wrapped header list. Headers’ existing order is generally maintained, with new headers added to the end of the wrapped list.

Unlike a dictionary, Headers objects do not raise an error when you try to get or delete a key that isn’t in the wrapped header list. Getting a nonexistent header just returns None, and deleting a nonexistent header does nothing.

Headers objects also support keys(), values(), and items() methods. The lists returned by keys() and items() can include the same key more than once if there is a multi-valued header. The len() of a Headers object is the same as the length of its items(), which is the same as the length of the wrapped header list. In fact, the items() method just returns a copy of the wrapped header list.

Calling bytes() on a Headers object returns a formatted bytestring suitable for transmission as HTTP response headers. Each header is placed on a line with its value, separated by a colon and a space. Each line is terminated by a carriage return and line feed, and the bytestring is terminated with a blank line.

In addition to their mapping interface and formatting features, Headers objects also have the following methods for querying and adding multi-valued headers, and for adding headers with MIME parameters:

get_all(name)

Return a list of all the values for the named header.

The returned list will be sorted in the order they appeared in the original header list or were added to this instance, and may contain duplicates. Any fields deleted and re-inserted are always appended to the header list. If no fields exist with the given name, returns an empty list.

add_header(name, value, **_params)

Add a (possibly multi-valued) header, with optional MIME parameters specified via keyword arguments.

name is the header field to add. Keyword arguments can be used to set MIME parameters for the header field. Each parameter must be a string or None. Underscores in parameter names are converted to dashes, since dashes are illegal in Python identifiers, but many MIME parameter names include dashes. If the parameter value is a string, it is added to the header value parameters in the form name="value". If it is None, only the parameter name is added. (This is used for MIME parameters without a value.) Example usage:

h.add_header('content-disposition', 'attachment', filename='bud.gif')

The above will add a header that looks like this:

Content-Disposition: attachment; filename="bud.gif"

在 3.5 版更改: headers parameter is optional.

21.4.3. wsgiref.simple_server – a simple WSGI HTTP server

This module implements a simple HTTP server (based on http.server) that serves WSGI applications. Each server instance serves a single WSGI application on a given host and port. If you want to serve multiple applications on a single host and port, you should create a WSGI application that parses PATH_INFO to select which application to invoke for each request. (E.g., using the shift_path_info() function from wsgiref.util.)

wsgiref.simple_server.make_server(host, port, app, server_class=WSGIServer, handler_class=WSGIRequestHandler)

Create a new WSGI server listening on host and port, accepting connections for app. The return value is an instance of the supplied server_class, and will process requests using the specified handler_class. app must be a WSGI application object, as defined by PEP 3333 [https://www.python.org/dev/peps/pep-3333].

用法示例：

from wsgiref.simple_server import make_server, demo_app

with make_server('', 8000, demo_app) as httpd:
    print("Serving HTTP on port 8000...")

    # Respond to requests until process is killed
    httpd.serve_forever()

    # Alternative: serve one request, then exit
    httpd.handle_request()

wsgiref.simple_server.demo_app(environ, start_response)

This function is a small but complete WSGI application that returns a text page containing the message “Hello world!” and a list of the key/value pairs provided in the environ parameter. It’s useful for verifying that a WSGI server (such as wsgiref.simple_server) is able to run a simple WSGI application correctly.

class wsgiref.simple_server.WSGIServer(server_address, RequestHandlerClass)

Create a WSGIServer instance. server_address should be a (host,port) tuple, and RequestHandlerClass should be the subclass of http.server.BaseHTTPRequestHandler that will be used to process requests.

You do not normally need to call this constructor, as the make_server() function can handle all the details for you.

WSGIServer is a subclass of http.server.HTTPServer, so all of its methods (such as serve_forever() and handle_request()) are available. WSGIServer also provides these WSGI-specific methods:

set_app(application)

Sets the callable application as the WSGI application that will receive requests.

get_app()

Returns the currently-set application callable.

Normally, however, you do not need to use these additional methods, as set_app() is normally called by make_server(), and the get_app() exists mainly for the benefit of request handler instances.

class wsgiref.simple_server.WSGIRequestHandler(request, client_address, server)

Create an HTTP handler for the given request (i.e. a socket), client_address (a (host,port) tuple), and server (WSGIServer instance).

You do not need to create instances of this class directly; they are automatically created as needed by WSGIServer objects. You can, however, subclass this class and supply it as a handler_class to the make_server() function. Some possibly relevant methods for overriding in subclasses:

get_environ()

Returns a dictionary containing the WSGI environment for a request. The default implementation copies the contents of the WSGIServer object’s base_environ dictionary attribute and then adds various headers derived from the HTTP request. Each call to this method should return a new dictionary containing all of the relevant CGI environment variables as specified in PEP 3333 [https://www.python.org/dev/peps/pep-3333].

get_stderr()

Return the object that should be used as the wsgi.errors stream. The default implementation just returns sys.stderr.

handle()

Process the HTTP request. The default implementation creates a handler instance using a wsgiref.handlers class to implement the actual WSGI application interface.

21.4.4. wsgiref.validate — WSGI conformance checker

When creating new WSGI application objects, frameworks, servers, or middleware, it can be useful to validate the new code’s conformance using wsgiref.validate. This module provides a function that creates WSGI application objects that validate communications between a WSGI server or gateway and a WSGI application object, to check both sides for protocol conformance.

Note that this utility does not guarantee complete PEP 3333 [https://www.python.org/dev/peps/pep-3333] compliance; an absence of errors from this module does not necessarily mean that errors do not exist. However, if this module does produce an error, then it is virtually certain that either the server or application is not 100% compliant.

This module is based on the paste.lint module from Ian Bicking’s “Python Paste” library.

wsgiref.validate.validator(application)

Wrap application and return a new WSGI application object. The returned application will forward all requests to the original application, and will check that both the application and the server invoking it are conforming to the WSGI specification and to RFC 2616 [https://tools.ietf.org/html/rfc2616.html].

Any detected nonconformance results in an AssertionError being raised; note, however, that how these errors are handled is server-dependent. For example, wsgiref.simple_server and other servers based on wsgiref.handlers (that don’t override the error handling methods to do something else) will simply output a message that an error has occurred, and dump the traceback to sys.stderr or some other error stream.

This wrapper may also generate output using the warnings module to indicate behaviors that are questionable but which may not actually be prohibited by PEP 3333 [https://www.python.org/dev/peps/pep-3333]. Unless they are suppressed using Python command-line options or the warnings API, any such warnings will be written to sys.stderr (not wsgi.errors, unless they happen to be the same object).

用法示例：

from wsgiref.validate import validator
from wsgiref.simple_server import make_server

# Our callable object which is intentionally not compliant to the
# standard, so the validator is going to break
def simple_app(environ, start_response):
    status = '200 OK'  # HTTP Status
    headers = [('Content-type', 'text/plain')]  # HTTP Headers
    start_response(status, headers)

    # This is going to break because we need to return a list, and
    # the validator is going to inform us
    return b"Hello World"

# This is the application wrapped in a validator
validator_app = validator(simple_app)

with make_server('', 8000, validator_app) as httpd:
    print("Listening on port 8000....")
    httpd.serve_forever()

21.4.5. wsgiref.handlers – server/gateway base classes

This module provides base handler classes for implementing WSGI servers and gateways. These base classes handle most of the work of communicating with a WSGI application, as long as they are given a CGI-like environment, along with input, output, and error streams.

class wsgiref.handlers.CGIHandler

CGI-based invocation via sys.stdin, sys.stdout, sys.stderr and os.environ. This is useful when you have a WSGI application and want to run it as a CGI script. Simply invoke CGIHandler().run(app), where app is the WSGI application object you wish to invoke.

This class is a subclass of BaseCGIHandler that sets wsgi.run_once to true, wsgi.multithread to false, and wsgi.multiprocess to true, and always uses sys and os to obtain the necessary CGI streams and environment.

class wsgiref.handlers.IISCGIHandler

A specialized alternative to CGIHandler, for use when deploying on Microsoft’s IIS web server, without having set the config allowPathInfo option (IIS>=7) or metabase allowPathInfoForScriptMappings (IIS<7).

By default, IIS gives a PATH_INFO that duplicates the SCRIPT_NAME at the front, causing problems for WSGI applications that wish to implement routing. This handler strips any such duplicated path.

IIS can be configured to pass the correct PATH_INFO, but this causes another bug where PATH_TRANSLATED is wrong. Luckily this variable is rarely used and is not guaranteed by WSGI. On IIS<7, though, the setting can only be made on a vhost level, affecting all other script mappings, many of which break when exposed to the PATH_TRANSLATED bug. For this reason IIS<7 is almost never deployed with the fix. (Even IIS7 rarely uses it because there is still no UI for it.)

There is no way for CGI code to tell whether the option was set, so a separate handler class is provided. It is used in the same way as CGIHandler, i.e., by calling IISCGIHandler().run(app), where app is the WSGI application object you wish to invoke.

3.2 新版功能.

class wsgiref.handlers.BaseCGIHandler(stdin, stdout, stderr, environ, multithread=True, multiprocess=False)

Similar to CGIHandler, but instead of using the sys and os modules, the CGI environment and I/O streams are specified explicitly. The multithread and multiprocess values are used to set the wsgi.multithread and wsgi.multiprocess flags for any applications run by the handler instance.

This class is a subclass of SimpleHandler intended for use with software other than HTTP “origin servers”. If you are writing a gateway protocol implementation (such as CGI, FastCGI, SCGI, etc.) that uses a Status: header to send an HTTP status, you probably want to subclass this instead of SimpleHandler.

class wsgiref.handlers.SimpleHandler(stdin, stdout, stderr, environ, multithread=True, multiprocess=False)

Similar to BaseCGIHandler, but designed for use with HTTP origin servers. If you are writing an HTTP server implementation, you will probably want to subclass this instead of BaseCGIHandler.

This class is a subclass of BaseHandler. It overrides the __init__(), get_stdin(), get_stderr(), add_cgi_vars(), _write(), and _flush() methods to support explicitly setting the environment and streams via the constructor. The supplied environment and streams are stored in the stdin, stdout, stderr, and environ attributes.

The write() method of stdout should write each chunk in full, like io.BufferedIOBase.

class wsgiref.handlers.BaseHandler

This is an abstract base class for running WSGI applications. Each instance will handle a single HTTP request, although in principle you could create a subclass that was reusable for multiple requests.

BaseHandler instances have only one method intended for external use:

run(app)

Run the specified WSGI application, app.

All of the other BaseHandler methods are invoked by this method in the process of running the application, and thus exist primarily to allow customizing the process.

The following methods MUST be overridden in a subclass:

_write(data)

Buffer the bytes data for transmission to the client. It’s okay if this method actually transmits the data; BaseHandler just separates write and flush operations for greater efficiency when the underlying system actually has such a distinction.

_flush()

Force buffered data to be transmitted to the client. It’s okay if this method is a no-op (i.e., if _write() actually sends the data).

get_stdin()

Return an input stream object suitable for use as the wsgi.input of the request currently being processed.

get_stderr()

Return an output stream object suitable for use as the wsgi.errors of the request currently being processed.

add_cgi_vars()

Insert CGI variables for the current request into the environ attribute.

Here are some other methods and attributes you may wish to override. This list is only a summary, however, and does not include every method that can be overridden. You should consult the docstrings and source code for additional information before attempting to create a customized BaseHandler subclass.

Attributes and methods for customizing the WSGI environment:

wsgi_multithread

The value to be used for the wsgi.multithread environment variable. It defaults to true in BaseHandler, but may have a different default (or be set by the constructor) in the other subclasses.

wsgi_multiprocess

The value to be used for the wsgi.multiprocess environment variable. It defaults to true in BaseHandler, but may have a different default (or be set by the constructor) in the other subclasses.

wsgi_run_once

The value to be used for the wsgi.run_once environment variable. It defaults to false in BaseHandler, but CGIHandler sets it to true by default.

os_environ

The default environment variables to be included in every request’s WSGI environment. By default, this is a copy of os.environ at the time that wsgiref.handlers was imported, but subclasses can either create their own at the class or instance level. Note that the dictionary should be considered read-only, since the default value is shared between multiple classes and instances.

server_software

If the origin_server attribute is set, this attribute’s value is used to set the default SERVER_SOFTWARE WSGI environment variable, and also to set a default Server: header in HTTP responses. It is ignored for handlers (such as BaseCGIHandler and CGIHandler) that are not HTTP origin servers.

在 3.3 版更改: The term “Python” is replaced with implementation specific term like “CPython”, “Jython” etc.

get_scheme()

Return the URL scheme being used for the current request. The default implementation uses the guess_scheme() function from wsgiref.util to guess whether the scheme should be “http” or “https”, based on the current request’s environ variables.

setup_environ()

Set the environ attribute to a fully-populated WSGI environment. The default implementation uses all of the above methods and attributes, plus the get_stdin(), get_stderr(), and add_cgi_vars() methods and the wsgi_file_wrapper attribute. It also inserts a SERVER_SOFTWARE key if not present, as long as the origin_server attribute is a true value and the server_software attribute is set.

Methods and attributes for customizing exception handling:

log_exception(exc_info)

Log the exc_info tuple in the server log. exc_info is a (type, value, traceback) tuple. The default implementation simply writes the traceback to the request’s wsgi.errors stream and flushes it. Subclasses can override this method to change the format or retarget the output, mail the traceback to an administrator, or whatever other action may be deemed suitable.

traceback_limit

The maximum number of frames to include in tracebacks output by the default log_exception() method. If None, all frames are included.

error_output(environ, start_response)

This method is a WSGI application to generate an error page for the user. It is only invoked if an error occurs before headers are sent to the client.

This method can access the current error information using sys.exc_info(), and should pass that information to start_response when calling it (as described in the “Error Handling” section of PEP 3333 [https://www.python.org/dev/peps/pep-3333]).

The default implementation just uses the error_status, error_headers, and error_body attributes to generate an output page. Subclasses can override this to produce more dynamic error output.

Note, however, that it’s not recommended from a security perspective to spit out diagnostics to any old user; ideally, you should have to do something special to enable diagnostic output, which is why the default implementation doesn’t include any.

error_status

The HTTP status used for error responses. This should be a status string as defined in PEP 3333 [https://www.python.org/dev/peps/pep-3333]; it defaults to a 500 code and message.

error_headers

The HTTP headers used for error responses. This should be a list of WSGI response headers ((name, value) tuples), as described in PEP 3333 [https://www.python.org/dev/peps/pep-3333]. The default list just sets the content type to text/plain.

error_body

The error response body. This should be an HTTP response body bytestring. It defaults to the plain text, “A server error occurred. Please contact the administrator.”

Methods and attributes for PEP 3333 [https://www.python.org/dev/peps/pep-3333]’s “Optional Platform-Specific File Handling” feature:

wsgi_file_wrapper

A wsgi.file_wrapper factory, or None. The default value of this attribute is the wsgiref.util.FileWrapper class.

sendfile()

Override to implement platform-specific file transmission. This method is called only if the application’s return value is an instance of the class specified by the wsgi_file_wrapper attribute. It should return a true value if it was able to successfully transmit the file, so that the default transmission code will not be executed. The default implementation of this method just returns a false value.

Miscellaneous methods and attributes:

origin_server

This attribute should be set to a true value if the handler’s _write() and _flush() are being used to communicate directly to the client, rather than via a CGI-like gateway protocol that wants the HTTP status in a special Status: header.

This attribute’s default value is true in BaseHandler, but false in BaseCGIHandler and CGIHandler.

http_version

If origin_server is true, this string attribute is used to set the HTTP version of the response set to the client. It defaults to "1.0".

wsgiref.handlers.read_environ()

Transcode CGI variables from os.environ to PEP 3333 “bytes in unicode” strings, returning a new dictionary. This function is used by CGIHandler and IISCGIHandler in place of directly using os.environ, which is not necessarily WSGI-compliant on all platforms and web servers using Python 3 – specifically, ones where the OS’s actual environment is Unicode (i.e. Windows), or ones where the environment is bytes, but the system encoding used by Python to decode it is anything other than ISO-8859-1 (e.g. Unix systems using UTF-8).

If you are implementing a CGI-based handler of your own, you probably want to use this routine instead of just copying values out of os.environ directly.

3.2 新版功能.

21.4.6. 例子

This is a working “Hello World” WSGI application:

from wsgiref.simple_server import make_server

# Every WSGI application must have an application object - a callable
# object that accepts two arguments. For that purpose, we're going to
# use a function (note that you're not limited to a function, you can
# use a class for example). The first argument passed to the function
# is a dictionary containing CGI-style environment variables and the
# second variable is the callable object (see PEP 333).
def hello_world_app(environ, start_response):
    status = '200 OK'  # HTTP Status
    headers = [('Content-type', 'text/plain; charset=utf-8')]  # HTTP Headers
    start_response(status, headers)

    # The returned object is going to be printed
    return [b"Hello World"]

with make_server('', 8000, hello_world_app) as httpd:
    print("Serving on port 8000...")

    # Serve until process is killed
    httpd.serve_forever()

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.5. urllib — URL 处理模块

源代码: Lib/urllib/ [https://github.com/python/cpython/tree/3.6/Lib/urllib/]

urllib 是一个收集了多个用到 URL 的模块的包：

urllib.request 打开和读取 URL

urllib.error 包含 urllib.request 抛出的异常

urllib.parse 用于解析 URL

urllib.robotparser 用于解析 robots.txt 文件

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.6. urllib.request — 用于打开 URL 的可扩展库

源码： Lib/urllib/request.py [https://github.com/python/cpython/tree/3.6/Lib/urllib/request.py]

urllib.request 模块定义了适用于在各种复杂情况下打开 URL（主要为 HTTP）的函数和类 — 例如基本认证、摘要认证、重定向、cookies 及其它。

参见

The Requests package [http://docs.python-requests.org/] is recommended for a higher-level HTTP client interface.

urllib.request 模块定义了以下函数：

urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)

打开统一资源定位地址 url，可以是一个字符串或一个 Request 对象。

data must be an object specifying additional data to be sent to the server, or None if no such data is needed. See Request for details.

urllib.request 模块使用 HTTP/1.1 并且在其 HTTP 请求中包含 Connection:close 头。

The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used). This actually only works for HTTP, HTTPS and FTP connections.

If context is specified, it must be a ssl.SSLContext instance describing the various SSL options. See HTTPSConnection for more details.

The optional cafile and capath parameters specify a set of trusted CA certificates for HTTPS requests. cafile should point to a single file containing a bundle of CA certificates, whereas capath should point to a directory of hashed certificate files. More information can be found in ssl.SSLContext.load_verify_locations().

The cadefault parameter is ignored.

This function always returns an object which can work as a context manager and has methods such as

geturl() — return the URL of the resource retrieved, commonly used to determine if a redirect was followed

info() — return the meta-information of the page, such as headers, in the form of an email.message_from_string() instance (see Quick Reference to HTTP Headers [https://www.cs.tut.fi/~jkorpela/http.html])

getcode() – return the HTTP status code of the response.

For HTTP and HTTPS URLs, this function returns a http.client.HTTPResponse object slightly modified. In addition to the three new methods above, the msg attribute contains the same information as the reason attribute — the reason phrase returned by server — instead of the response headers as it is specified in the documentation for HTTPResponse.

For FTP, file, and data URLs and requests explicitly handled by legacy URLopener and FancyURLopener classes, this function returns a urllib.response.addinfourl object.

协议错误时引发 URLError。

Note that None may be returned if no handler handles the request (though the default installed global OpenerDirector uses UnknownHandler to ensure this never happens).

In addition, if proxy settings are detected (for example, when a *_proxy environment variable like http_proxy is set), ProxyHandler is default installed and makes sure the requests are handled through the proxy.

The legacy urllib.urlopen function from Python 2.6 and earlier has been discontinued; urllib.request.urlopen() corresponds to the old urllib2.urlopen. Proxy handling, which was done by passing a dictionary parameter to urllib.urlopen, can be obtained by using ProxyHandler objects.

在 3.2 版更改: 增加了 cafile 与 capath。

在 3.2 版更改: 现在如果可行（指 ssl.HAS_SNI 为真），支持 HTTPS 虚拟主机

3.2 新版功能: data 可以是一个可迭代对象。

在 3.3 版更改: 增加了 cadefault。

在 3.4.3 版更改: 增加了 context。

3.6 版后已移除: cafile, capath and cadefault are deprecated in favor of context. Please use ssl.SSLContext.load_cert_chain() instead, or let ssl.create_default_context() select the system’s trusted CA certificates for you.

urllib.request.install_opener(opener)

Install an OpenerDirector instance as the default global opener. Installing an opener is only necessary if you want urlopen to use that opener; otherwise, simply call OpenerDirector.open() instead of urlopen(). The code does not check for a real OpenerDirector, and any class with the appropriate interface will work.

urllib.request.build_opener([handler, ...])

Return an OpenerDirector instance, which chains the handlers in the order given. handlers can be either instances of BaseHandler, or subclasses of BaseHandler (in which case it must be possible to call the constructor without any parameters). Instances of the following classes will be in front of the handlers, unless the handlers contain them, instances of them or subclasses of them: ProxyHandler (if proxy settings are detected), UnknownHandler, HTTPHandler, HTTPDefaultErrorHandler, HTTPRedirectHandler, FTPHandler, FileHandler, HTTPErrorProcessor.

若 Python 安装有 SLL 支持（指可以导入 ssl 模块），亦会加入 HTTPSHandler。

A BaseHandler subclass may also change its handler_order attribute to modify its position in the handlers list.

urllib.request.pathname2url(path)

Convert the pathname path from the local syntax for a path to the form used in the path component of a URL. This does not produce a complete URL. The return value will already be quoted using the quote() function.

urllib.request.url2pathname(path)

Convert the path component path from a percent-encoded URL to the local syntax for a path. This does not accept a complete URL. This function uses unquote() to decode path.

urllib.request.getproxies()

This helper function returns a dictionary of scheme to proxy server URL mappings. It scans the environment for variables named <scheme>_proxy, in a case insensitive approach, for all operating systems first, and when it cannot find it, looks for proxy information from Mac OSX System Configuration for Mac OS X and Windows Systems Registry for Windows. If both lowercase and uppercase environment variables exist (and disagree), lowercase is preferred.

注解

If the environment variable REQUEST_METHOD is set, which usually indicates your script is running in a CGI environment, the environment variable HTTP_PROXY (uppercase _PROXY) will be ignored. This is because that variable can be injected by a client using the “Proxy:” HTTP header. If you need to use an HTTP proxy in a CGI environment, either use ProxyHandler explicitly, or make sure the variable name is in lowercase (or at least the _proxy suffix).

提供了以下类：

class urllib.request.Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None)

This class is an abstraction of a URL request.

url 应该是是一个含有一个有效的统一资源定位地址的字符串。

data must be an object specifying additional data to send to the server, or None if no such data is needed. Currently HTTP requests are the only ones that use data. The supported object types include bytes, file-like objects, and iterables. If no Content-Length nor Transfer-Encoding header field has been provided, HTTPHandler will set these headers according to the type of data. Content-Length will be used to send bytes objects, while Transfer-Encoding: chunked as specified in RFC 7230 [https://tools.ietf.org/html/rfc7230.html], Section 3.3.1 will be used to send files and other iterables.

For an HTTP POST request method, data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.parse.urlencode() function takes a mapping or sequence of 2-tuples and returns an ASCII string in this format. It should be encoded to bytes before being used as the data parameter.

headers should be a dictionary, and will be treated as if add_header() was called with each key and value as arguments. This is often used to “spoof” the User-Agent header value, which is used by a browser to identify itself – some HTTP servers only allow requests coming from common browsers as opposed to scripts. For example, Mozilla Firefox may identify itself as "Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11", while urllib’s default user agent string is "Python-urllib/2.6" (on Python 2.6).

An appropriate Content-Type header should be included if the data argument is present. If this header has not been provided and data is not None, Content-Type: application/x-www-form-urlencoded will be added as a default.

The final two arguments are only of interest for correct handling of third-party HTTP cookies:

origin_req_host should be the request-host of the origin transaction, as defined by RFC 2965 [https://tools.ietf.org/html/rfc2965.html]. It defaults to http.cookiejar.request_host(self). This is the host name or IP address of the original request that was initiated by the user. For example, if the request is for an image in an HTML document, this should be the request-host of the request for the page containing the image.

unverifiable should indicate whether the request is unverifiable, as defined by RFC 2965 [https://tools.ietf.org/html/rfc2965.html]. It defaults to False. An unverifiable request is one whose URL the user did not have the option to approve. For example, if the request is for an image in an HTML document, and the user had no option to approve the automatic fetching of the image, this should be true.

method should be a string that indicates the HTTP request method that will be used (e.g. 'HEAD'). If provided, its value is stored in the method attribute and is used by get_method(). The default is 'GET' if data is None or 'POST' otherwise. Subclasses may indicate a different default method by setting the method attribute in the class itself.

注解

The request will not work as expected if the data object is unable to deliver its content more than once (e.g. a file or an iterable that can produce the content only once) and the request is retried for HTTP redirects or authentication. The data is sent to the HTTP server right away after the headers. There is no support for a 100-continue expectation in the library.

在 3.3 版更改: Request 类增加了 Request.method 参数。

在 3.4 版更改: Default Request.method may be indicated at the class level.

在 3.6 版更改: Do not raise an error if the Content-Length has not been provided and data is neither None nor a bytes object. Fall back to use chunked transfer encoding instead.

class urllib.request.OpenerDirector

The OpenerDirector class opens URLs via BaseHandlers chained together. It manages the chaining of handlers, and recovery from errors.

class urllib.request.BaseHandler

This is the base class for all registered handlers — and handles only the simple mechanics of registration.

class urllib.request.HTTPDefaultErrorHandler

A class which defines a default handler for HTTP error responses; all responses are turned into HTTPError exceptions.

class urllib.request.HTTPRedirectHandler

一个用于处理重定向的类。

class urllib.request.HTTPCookieProcessor(cookiejar=None)

一个用于处理 HTTP Cookies 的类。

class urllib.request.ProxyHandler(proxies=None)

Cause requests to go through a proxy. If proxies is given, it must be a dictionary mapping protocol names to URLs of proxies. The default is to read the list of proxies from the environment variables <protocol>_proxy. If no proxy environment variables are set, then in a Windows environment proxy settings are obtained from the registry’s Internet Settings section, and in a Mac OS X environment proxy information is retrieved from the OS X System Configuration Framework.

To disable autodetected proxy pass an empty dictionary.

The no_proxy environment variable can be used to specify hosts which shouldn’t be reached via proxy; if set, it should be a comma-separated list of hostname suffixes, optionally with :port appended, for example cern.ch,ncsa.uiuc.edu,some.host:8080.

注解

HTTP_PROXY will be ignored if a variable REQUEST_METHOD is set; see the documentation on getproxies().

class urllib.request.HTTPPasswordMgr

Keep a database of (realm, uri) -> (user, password) mappings.

class urllib.request.HTTPPasswordMgrWithDefaultRealm

Keep a database of (realm, uri) -> (user, password) mappings. A realm of None is considered a catch-all realm, which is searched if no other realm fits.

class urllib.request.HTTPPasswordMgrWithPriorAuth

A variant of HTTPPasswordMgrWithDefaultRealm that also has a database of uri -> is_authenticated mappings. Can be used by a BasicAuth handler to determine when to send authentication credentials immediately instead of waiting for a 401 response first.

3.5 新版功能.

class urllib.request.AbstractBasicAuthHandler(password_mgr=None)

This is a mixin class that helps with HTTP authentication, both to the remote host and to a proxy. password_mgr, if given, should be something that is compatible with HTTPPasswordMgr; refer to section HTTPPasswordMgr 对象 for information on the interface that must be supported. If passwd_mgr also provides is_authenticated and update_authenticated methods (see HTTPPasswordMgrWithPriorAuth 对象), then the handler will use the is_authenticated result for a given URI to determine whether or not to send authentication credentials with the request. If is_authenticated returns True for the URI, credentials are sent. If is_authenticated is False, credentials are not sent, and then if a 401 response is received the request is re-sent with the authentication credentials. If authentication succeeds, update_authenticated is called to set is_authenticated True for the URI, so that subsequent requests to the URI or any of its super-URIs will automatically include the authentication credentials.

3.5 新版功能: 增加了对 is_authenticated 的支持。

class urllib.request.HTTPBasicAuthHandler(password_mgr=None)

Handle authentication with the remote host. password_mgr, if given, should be something that is compatible with HTTPPasswordMgr; refer to section HTTPPasswordMgr 对象 for information on the interface that must be supported. HTTPBasicAuthHandler will raise a ValueError when presented with a wrong Authentication scheme.

class urllib.request.ProxyBasicAuthHandler(password_mgr=None)

Handle authentication with the proxy. password_mgr, if given, should be something that is compatible with HTTPPasswordMgr; refer to section HTTPPasswordMgr 对象 for information on the interface that must be supported.

class urllib.request.AbstractDigestAuthHandler(password_mgr=None)

This is a mixin class that helps with HTTP authentication, both to the remote host and to a proxy. password_mgr, if given, should be something that is compatible with HTTPPasswordMgr; refer to section HTTPPasswordMgr 对象 for information on the interface that must be supported.

class urllib.request.HTTPDigestAuthHandler(password_mgr=None)

Handle authentication with the remote host. password_mgr, if given, should be something that is compatible with HTTPPasswordMgr; refer to section HTTPPasswordMgr 对象 for information on the interface that must be supported. When both Digest Authentication Handler and Basic Authentication Handler are both added, Digest Authentication is always tried first. If the Digest Authentication returns a 40x response again, it is sent to Basic Authentication handler to Handle. This Handler method will raise a ValueError when presented with an authentication scheme other than Digest or Basic.

在 3.3 版更改: Raise ValueError on unsupported Authentication Scheme.

class urllib.request.ProxyDigestAuthHandler(password_mgr=None)

Handle authentication with the proxy. password_mgr, if given, should be something that is compatible with HTTPPasswordMgr; refer to section HTTPPasswordMgr 对象 for information on the interface that must be supported.

class urllib.request.HTTPHandler

A class to handle opening of HTTP URLs.

class urllib.request.HTTPSHandler(debuglevel=0, context=None, check_hostname=None)

A class to handle opening of HTTPS URLs. context and check_hostname have the same meaning as in http.client.HTTPSConnection.

在 3.2 版更改: context and check_hostname were added.

class urllib.request.FileHandler

打开本地文件。

class urllib.request.DataHandler

Open data URLs.

3.4 新版功能.

class urllib.request.FTPHandler

打开 FTP 统一资源定位地址。

class urllib.request.CacheFTPHandler

Open FTP URLs, keeping a cache of open FTP connections to minimize delays.

class urllib.request.UnknownHandler

A catch-all class to handle unknown URLs.

class urllib.request.HTTPErrorProcessor

Process HTTP error responses.

21.6.1. Request 对象

The following methods describe Request’s public interface, and so all may be overridden in subclasses. It also defines several public attributes that can be used by clients to inspect the parsed request.

Request.full_url

传给构造函数的原始 URL。

在 3.4 版更改.

Request.full_url is a property with setter, getter and a deleter. Getting full_url returns the original request URL with the fragment, if it was present.

Request.type

The URI scheme.

Request.host

The URI authority, typically a host, but may also contain a port separated by a colon.

Request.origin_req_host

请求的原始主机，不含端口。

Request.selector

URI 路径。若 Request 使用代理，选择器将会是传给代理的完整 URL。

Request.data

The entity body for the request, or None if not specified.

在 3.4 版更改: Changing value of Request.data now deletes “Content-Length” header if it was previously set or calculated.

Request.unverifiable

布尔，表明请求是否为 RFC 2965 [https://tools.ietf.org/html/rfc2965.html] 中定义的无法证实的。

Request.method

The HTTP request method to use. By default its value is None, which means that get_method() will do its normal computation of the method to be used. Its value can be set (thus overriding the default computation in get_method()) either by providing a default value by setting it at the class level in a Request subclass, or by passing a value in to the Request constructor via the method argument.

3.3 新版功能.

在 3.4 版更改: A default value can now be set in subclasses; previously it could only be set via the constructor argument.

Request.get_method()

Return a string indicating the HTTP request method. If Request.method is not None, return its value, otherwise return 'GET' if Request.data is None, or 'POST' if it’s not. This is only meaningful for HTTP requests.

在 3.3 版更改: get_method now looks at the value of Request.method.

Request.add_header(key, val)

Add another header to the request. Headers are currently ignored by all handlers except HTTP handlers, where they are added to the list of headers sent to the server. Note that there cannot be more than one header with the same name, and later calls will overwrite previous calls in case the key collides. Currently, this is no loss of HTTP functionality, since all headers which have meaning when used more than once have a (header-specific) way of gaining the same functionality using only one header.

Request.add_unredirected_header(key, header)

添加一个不会被加入重定向请求的头部。

Request.has_header(header)

Return whether the instance has the named header (checks both regular and unredirected).

Request.remove_header(header)

Remove named header from the request instance (both from regular and unredirected headers).

3.4 新版功能.

Request.get_full_url()

返回构造器中给定的 URL。

在 3.4 版更改.

返回 Request.full_url

Request.set_proxy(host, type)

Prepare the request by connecting to a proxy server. The host and type will replace those of the instance, and the instance’s selector will be the original URL given in the constructor.

Request.get_header(header_name, default=None)

Return the value of the given header. If the header is not present, return the default value.

Request.header_items()

Return a list of tuples (header_name, header_value) of the Request headers.

在 3.4 版更改: The request methods add_data, has_data, get_data, get_type, get_host, get_selector, get_origin_req_host and is_unverifiable that were deprecated since 3.3 have been removed.

21.6.2. OpenerDirector 对象

OpenerDirector 实例有以下方法：

OpenerDirector.add_handler(handler)

handler should be an instance of BaseHandler. The following methods are searched, and added to the possible chains (note that HTTP errors are a special case).

protocol_open() — signal that the handler knows how to open protocol URLs.

http_error_type() — signal that the handler knows how to handle HTTP errors with HTTP error code type.

protocol_error() — signal that the handler knows how to handle errors from (non-http) protocol.

protocol_request() — signal that the handler knows how to pre-process protocol requests.

protocol_response() — signal that the handler knows how to post-process protocol responses.

OpenerDirector.open(url, data=None[, timeout])

Open the given url (which can be a request object or a string), optionally passing the given data. Arguments, return values and exceptions raised are the same as those of urlopen() (which simply calls the open() method on the currently installed global OpenerDirector). The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used). The timeout feature actually works only for HTTP, HTTPS and FTP connections).

OpenerDirector.error(proto, *args)

Handle an error of the given protocol. This will call the registered error handlers for the given protocol with the given arguments (which are protocol specific). The HTTP protocol is a special case which uses the HTTP response code to determine the specific error handler; refer to the http_error_*() methods of the handler classes.

Return values and exceptions raised are the same as those of urlopen().

OpenerDirector objects open URLs in three stages:

The order in which these methods are called within each stage is determined by sorting the handler instances.

Every handler with a method named like protocol_request() has that method called to pre-process the request.

Handlers with a method named like protocol_open() are called to handle the request. This stage ends when a handler either returns a non-None value (ie. a response), or raises an exception (usually URLError). Exceptions are allowed to propagate.

In fact, the above algorithm is first tried for methods named default_open(). If all such methods return None, the algorithm is repeated for methods named like protocol_open(). If all such methods return None, the algorithm is repeated for methods named unknown_open().

Note that the implementation of these methods may involve calls of the parent OpenerDirector instance’s open() and error() methods.

Every handler with a method named like protocol_response() has that method called to post-process the response.

21.6.3. BaseHandler 对象

BaseHandler objects provide a couple of methods that are directly useful, and others that are meant to be used by derived classes. These are intended for direct use:

BaseHandler.add_parent(director)

Add a director as parent.

BaseHandler.close()

Remove any parents.

The following attribute and methods should only be used by classes derived from BaseHandler.

注解

The convention has been adopted that subclasses defining protocol_request() or protocol_response() methods are named *Processor; all others are named *Handler.

BaseHandler.parent

A valid OpenerDirector, which can be used to open using a different protocol, or handle errors.

BaseHandler.default_open(req)

This method is not defined in BaseHandler, but subclasses should define it if they want to catch all URLs.

This method, if implemented, will be called by the parent OpenerDirector. It should return a file-like object as described in the return value of the open() of OpenerDirector, or None. It should raise URLError, unless a truly exceptional thing happens (for example, MemoryError should not be mapped to URLError).

This method will be called before any protocol-specific open method.

BaseHandler.protocol_open(req)

This method is not defined in BaseHandler, but subclasses should define it if they want to handle URLs with the given protocol.

This method, if defined, will be called by the parent OpenerDirector. Return values should be the same as for default_open().

BaseHandler.unknown_open(req)

This method is not defined in BaseHandler, but subclasses should define it if they want to catch all URLs with no specific registered handler to open it.

This method, if implemented, will be called by the parent OpenerDirector. Return values should be the same as for default_open().

BaseHandler.http_error_default(req, fp, code, msg, hdrs)

This method is not defined in BaseHandler, but subclasses should override it if they intend to provide a catch-all for otherwise unhandled HTTP errors. It will be called automatically by the OpenerDirector getting the error, and should not normally be called in other circumstances.

req will be a Request object, fp will be a file-like object with the HTTP error body, code will be the three-digit code of the error, msg will be the user-visible explanation of the code and hdrs will be a mapping object with the headers of the error.

Return values and exceptions raised should be the same as those of urlopen().

BaseHandler.http_error_nnn(req, fp, code, msg, hdrs)

nnn should be a three-digit HTTP error code. This method is also not defined in BaseHandler, but will be called, if it exists, on an instance of a subclass, when an HTTP error with code nnn occurs.

Subclasses should override this method to handle specific HTTP errors.

Arguments, return values and exceptions raised should be the same as for http_error_default().

BaseHandler.protocol_request(req)

This method is not defined in BaseHandler, but subclasses should define it if they want to pre-process requests of the given protocol.

This method, if defined, will be called by the parent OpenerDirector. req will be a Request object. The return value should be a Request object.

BaseHandler.protocol_response(req, response)

This method is not defined in BaseHandler, but subclasses should define it if they want to post-process responses of the given protocol.

This method, if defined, will be called by the parent OpenerDirector. req will be a Request object. response will be an object implementing the same interface as the return value of urlopen(). The return value should implement the same interface as the return value of urlopen().

21.6.4. HTTPRedirectHandler 对象

注解

Some HTTP redirections require action from this module’s client code. If this is the case, HTTPError is raised. See RFC 2616 [https://tools.ietf.org/html/rfc2616.html] for details of the precise meanings of the various redirection codes.

An HTTPError exception raised as a security consideration if the HTTPRedirectHandler is presented with a redirected URL which is not an HTTP, HTTPS or FTP URL.

HTTPRedirectHandler.redirect_request(req, fp, code, msg, hdrs, newurl)

Return a Request or None in response to a redirect. This is called by the default implementations of the http_error_30*() methods when a redirection is received from the server. If a redirection should take place, return a new Request to allow http_error_30*() to perform the redirect to newurl. Otherwise, raise HTTPError if no other handler should try to handle this URL, or return None if you can’t but another handler might.

注解

The default implementation of this method does not strictly follow RFC 2616 [https://tools.ietf.org/html/rfc2616.html], which says that 301 and 302 responses to POST requests must not be automatically redirected without confirmation by the user. In reality, browsers do allow automatic redirection of these responses, changing the POST to a GET, and the default implementation reproduces this behavior.

HTTPRedirectHandler.http_error_301(req, fp, code, msg, hdrs)

Redirect to the Location: or URI: URL. This method is called by the parent OpenerDirector when getting an HTTP ‘moved permanently’ response.

HTTPRedirectHandler.http_error_302(req, fp, code, msg, hdrs)

The same as http_error_301(), but called for the ‘found’ response.

HTTPRedirectHandler.http_error_303(req, fp, code, msg, hdrs)

The same as http_error_301(), but called for the ‘see other’ response.

HTTPRedirectHandler.http_error_307(req, fp, code, msg, hdrs)

The same as http_error_301(), but called for the ‘temporary redirect’ response.

21.6.5. HTTPCookieProcessor 对象

HTTPCookieProcessor instances have one attribute:

HTTPCookieProcessor.cookiejar

The http.cookiejar.CookieJar in which cookies are stored.

21.6.6. ProxyHandler 对象

ProxyHandler.protocol_open(request)

The ProxyHandler will have a method protocol_open() for every protocol which has a proxy in the proxies dictionary given in the constructor. The method will modify requests to go through the proxy, by calling request.set_proxy(), and call the next handler in the chain to actually execute the protocol.

21.6.7. HTTPPasswordMgr 对象

These methods are available on HTTPPasswordMgr and HTTPPasswordMgrWithDefaultRealm objects.

HTTPPasswordMgr.add_password(realm, uri, user, passwd)

uri can be either a single URI, or a sequence of URIs. realm, user and passwd must be strings. This causes (user, passwd) to be used as authentication tokens when authentication for realm and a super-URI of any of the given URIs is given.

HTTPPasswordMgr.find_user_password(realm, authuri)

Get user/password for given realm and URI, if any. This method will return (None, None) if there is no matching user/password.

For HTTPPasswordMgrWithDefaultRealm objects, the realm None will be searched if the given realm has no matching user/password.

21.6.8. HTTPPasswordMgrWithPriorAuth 对象

This password manager extends HTTPPasswordMgrWithDefaultRealm to support tracking URIs for which authentication credentials should always be sent.

HTTPPasswordMgrWithPriorAuth.add_password(realm, uri, user, passwd, is_authenticated=False)

realm, uri, user, passwd are as for HTTPPasswordMgr.add_password(). is_authenticated sets the initial value of the is_authenticated flag for the given URI or list of URIs. If is_authenticated is specified as True, realm is ignored.

HTTPPasswordMgr.find_user_password(realm, authuri)

Same as for HTTPPasswordMgrWithDefaultRealm objects

HTTPPasswordMgrWithPriorAuth.update_authenticated(self, uri, is_authenticated=False)

Update the is_authenticated flag for the given uri or list of URIs.

HTTPPasswordMgrWithPriorAuth.is_authenticated(self, authuri)

Returns the current state of the is_authenticated flag for the given URI.

21.6.9. AbstractBasicAuthHandler 对象

AbstractBasicAuthHandler.http_error_auth_reqed(authreq, host, req, headers)

Handle an authentication request by getting a user/password pair, and re-trying the request. authreq should be the name of the header where the information about the realm is included in the request, host specifies the URL and path to authenticate for, req should be the (failed) Request object, and headers should be the error headers.

host is either an authority (e.g. "python.org") or a URL containing an authority component (e.g. "http://python.org/"). In either case, the authority must not contain a userinfo component (so, "python.org" and "python.org:80" are fine, "joe:password@python.org" is not).

21.6.10. HTTPBasicAuthHandler 对象

HTTPBasicAuthHandler.http_error_401(req, fp, code, msg, hdrs)

Retry the request with authentication information, if available.

21.6.11. ProxyBasicAuthHandler 对象

ProxyBasicAuthHandler.http_error_407(req, fp, code, msg, hdrs)

Retry the request with authentication information, if available.

21.6.12. AbstractDigestAuthHandler 对象

AbstractDigestAuthHandler.http_error_auth_reqed(authreq, host, req, headers)

authreq should be the name of the header where the information about the realm is included in the request, host should be the host to authenticate to, req should be the (failed) Request object, and headers should be the error headers.

21.6.13. HTTPDigestAuthHandler 对象

HTTPDigestAuthHandler.http_error_401(req, fp, code, msg, hdrs)

Retry the request with authentication information, if available.

21.6.14. ProxyDigestAuthHandler 对象

ProxyDigestAuthHandler.http_error_407(req, fp, code, msg, hdrs)

Retry the request with authentication information, if available.

21.6.15. HTTPHandler 对象

HTTPHandler.http_open(req)

Send an HTTP request, which can be either GET or POST, depending on req.has_data().

21.6.16. HTTPSHandler 对象

HTTPSHandler.https_open(req)

Send an HTTPS request, which can be either GET or POST, depending on req.has_data().

21.6.17. FileHandler 对象

FileHandler.file_open(req)

Open the file locally, if there is no host name, or the host name is 'localhost'.

在 3.2 版更改: This method is applicable only for local hostnames. When a remote hostname is given, an URLError is raised.

21.6.18. DataHandler 对象

DataHandler.data_open(req)

Read a data URL. This kind of URL contains the content encoded in the URL itself. The data URL syntax is specified in RFC 2397 [https://tools.ietf.org/html/rfc2397.html]. This implementation ignores white spaces in base64 encoded data URLs so the URL may be wrapped in whatever source file it comes from. But even though some browsers don’t mind about a missing padding at the end of a base64 encoded data URL, this implementation will raise an ValueError in that case.

21.6.19. FTPHandler 对象

FTPHandler.ftp_open(req)

Open the FTP file indicated by req. The login is always done with empty username and password.

21.6.20. CacheFTPHandler 对象

CacheFTPHandler objects are FTPHandler objects with the following additional methods:

CacheFTPHandler.setTimeout(t)

Set timeout of connections to t seconds.

CacheFTPHandler.setMaxConns(m)

Set maximum number of cached connections to m.

21.6.21. UnknownHandler 对象

UnknownHandler.unknown_open()

Raise a URLError exception.

21.6.22. HTTPErrorProcessor 对象

HTTPErrorProcessor.http_response(request, response)

Process HTTP error responses.

For 200 error codes, the response object is returned immediately.

For non-200 error codes, this simply passes the job on to the protocol_error_code() handler methods, via OpenerDirector.error(). Eventually, HTTPDefaultErrorHandler will raise an HTTPError if no other handler handles the error.

HTTPErrorProcessor.https_response(request, response)

Process HTTPS error responses.

The behavior is same as http_response().

21.6.23. 例子

In addition to the examples below, more examples are given in HOWTO 使用 urllib 包获取网络资源.

This example gets the python.org main page and displays the first 300 bytes of it.

>>> import urllib.request
>>> with urllib.request.urlopen('http://www.python.org/') as f:
...     print(f.read(300))
...
b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n\n\n<html
xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">\n\n<head>\n
<meta http-equiv="content-type" content="text/html; charset=utf-8" />\n
<title>Python Programming '

Note that urlopen returns a bytes object. This is because there is no way for urlopen to automatically determine the encoding of the byte stream it receives from the HTTP server. In general, a program will decode the returned bytes object to string once it determines or guesses the appropriate encoding.

The following W3C document, https://www.w3.org/International/O-charset, lists the various ways in which an (X)HTML or an XML document could have specified its encoding information.

As the python.org website uses utf-8 encoding as specified in its meta tag, we will use the same for decoding the bytes object.

>>> with urllib.request.urlopen('http://www.python.org/') as f:
...     print(f.read(100).decode('utf-8'))
...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtm

It is also possible to achieve the same result without using the context manager approach.

>>> import urllib.request
>>> f = urllib.request.urlopen('http://www.python.org/')
>>> print(f.read(100).decode('utf-8'))
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtm

In the following example, we are sending a data-stream to the stdin of a CGI and reading the data it returns to us. Note that this example will only work when the Python installation supports SSL.

>>> import urllib.request
>>> req = urllib.request.Request(url='https://localhost/cgi-bin/test.cgi',
...                       data=b'This data is passed to stdin of the CGI')
>>> with urllib.request.urlopen(req) as f:
...     print(f.read().decode('utf-8'))
...
Got Data: "This data is passed to stdin of the CGI"

The code for the sample CGI used in the above example is:

#!/usr/bin/env python
import sys
data = sys.stdin.read()
print('Content-type: text/plain\n\nGot Data: "%s"' % data)

Here is an example of doing a PUT request using Request:

import urllib.request
DATA = b'some data'
req = urllib.request.Request(url='http://localhost:8080', data=DATA,method='PUT')
with urllib.request.urlopen(req) as f:
    pass
print(f.status)
print(f.reason)

Use of Basic HTTP Authentication:

import urllib.request
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm='PDQ Application',
                          uri='https://mahler:8092/site-updates.py',
                          user='klem',
                          passwd='kadidd!ehopper')
opener = urllib.request.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib.request.install_opener(opener)
urllib.request.urlopen('http://www.example.com/login.html')

build_opener() provides many handlers by default, including a ProxyHandler. By default, ProxyHandler uses the environment variables named <scheme>_proxy, where <scheme> is the URL scheme involved. For example, the http_proxy environment variable is read to obtain the HTTP proxy’s URL.

This example replaces the default ProxyHandler with one that uses programmatically-supplied proxy URLs, and adds proxy authorization support with ProxyBasicAuthHandler.

proxy_handler = urllib.request.ProxyHandler({'http': 'http://www.example.com:3128/'})
proxy_auth_handler = urllib.request.ProxyBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'host', 'username', 'password')

opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler)
# This time, rather than install the OpenerDirector, we use it directly:
opener.open('http://www.example.com/login.html')

Adding HTTP headers:

Use the headers argument to the Request constructor, or:

import urllib.request
req = urllib.request.Request('http://www.example.com/')
req.add_header('Referer', 'http://www.python.org/')
# Customize the default User-Agent header value:
req.add_header('User-Agent', 'urllib-example/0.1 (Contact: . . .)')
r = urllib.request.urlopen(req)

OpenerDirector automatically adds a User-Agent header to every Request. To change this:

import urllib.request
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
opener.open('http://www.example.com/')

Also, remember that a few standard headers (Content-Length, Content-Type and Host) are added when the Request is passed to urlopen() (or OpenerDirector.open()).

Here is an example session that uses the GET method to retrieve a URL containing parameters:

>>> import urllib.request
>>> import urllib.parse
>>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> url = "http://www.musi-cal.com/cgi-bin/query?%s" % params
>>> with urllib.request.urlopen(url) as f:
...     print(f.read().decode('utf-8'))
...

The following example uses the POST method instead. Note that params output from urlencode is encoded to bytes before it is sent to urlopen as data:

>>> import urllib.request
>>> import urllib.parse
>>> data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> data = data.encode('ascii')
>>> with urllib.request.urlopen("http://requestb.in/xrbl82xr", data) as f:
...     print(f.read().decode('utf-8'))
...

The following example uses an explicitly specified HTTP proxy, overriding environment settings:

>>> import urllib.request
>>> proxies = {'http': 'http://proxy.example.com:8080/'}
>>> opener = urllib.request.FancyURLopener(proxies)
>>> with opener.open("http://www.python.org") as f:
...     f.read().decode('utf-8')
...

The following example uses no proxies at all, overriding environment settings:

>>> import urllib.request
>>> opener = urllib.request.FancyURLopener({})
>>> with opener.open("http://www.python.org/") as f:
...     f.read().decode('utf-8')
...

21.6.24. Legacy interface

The following functions and classes are ported from the Python 2 module urllib (as opposed to urllib2). They might become deprecated at some point in the future.

urllib.request.urlretrieve(url, filename=None, reporthook=None, data=None)

Copy a network object denoted by a URL to a local file. If the URL points to a local file, the object will not be copied unless filename is supplied. Return a tuple (filename, headers) where filename is the local file name under which the object can be found, and headers is whatever the info() method of the object returned by urlopen() returned (for a remote object). Exceptions are the same as for urlopen().

The second argument, if present, specifies the file location to copy to (if absent, the location will be a tempfile with a generated name). The third argument, if present, is a callable that will be called once on establishment of the network connection and once after each block read thereafter. The callable will be passed three arguments; a count of blocks transferred so far, a block size in bytes, and the total size of the file. The third argument may be -1 on older FTP servers which do not return a file size in response to a retrieval request.

The following example illustrates the most common usage scenario:

>>> import urllib.request
>>> local_filename, headers = urllib.request.urlretrieve('http://python.org/')
>>> html = open(local_filename)
>>> html.close()

If the url uses the http: scheme identifier, the optional data argument may be given to specify a POST request (normally the request type is GET). The data argument must be a bytes object in standard application/x-www-form-urlencoded format; see the urllib.parse.urlencode() function.

urlretrieve() will raise ContentTooShortError when it detects that the amount of data available was less than the expected amount (which is the size reported by a Content-Length header). This can occur, for example, when the download is interrupted.

The Content-Length is treated as a lower bound: if there’s more data to read, urlretrieve reads more data, but if less data is available, it raises the exception.

You can still retrieve the downloaded data in this case, it is stored in the content attribute of the exception instance.

If no Content-Length header was supplied, urlretrieve can not check the size of the data it has downloaded, and just returns it. In this case you just have to assume that the download was successful.

urllib.request.urlcleanup()

Cleans up temporary files that may have been left behind by previous calls to urlretrieve().

class urllib.request.URLopener(proxies=None, **x509)

3.3 版后已移除.

Base class for opening and reading URLs. Unless you need to support opening objects using schemes other than http:, ftp:, or file:, you probably want to use FancyURLopener.

By default, the URLopener class sends a User-Agent header of urllib/VVV, where VVV is the urllib version number. Applications can define their own User-Agent header by subclassing URLopener or FancyURLopener and setting the class attribute version to an appropriate string value in the subclass definition.

The optional proxies parameter should be a dictionary mapping scheme names to proxy URLs, where an empty dictionary turns proxies off completely. Its default value is None, in which case environmental proxy settings will be used if present, as discussed in the definition of urlopen(), above.

Additional keyword parameters, collected in x509, may be used for authentication of the client when using the https: scheme. The keywords key_file and cert_file are supported to provide an SSL key and certificate; both are needed to support client authentication.

URLopener objects will raise an OSError exception if the server returns an error code.

open(fullurl, data=None)

Open fullurl using the appropriate protocol. This method sets up cache and proxy information, then calls the appropriate open method with its input arguments. If the scheme is not recognized, open_unknown() is called. The data argument has the same meaning as the data argument of urlopen().

open_unknown(fullurl, data=None)

Overridable interface to open unknown URL types.

retrieve(url, filename=None, reporthook=None, data=None)

Retrieves the contents of url and places it in filename. The return value is a tuple consisting of a local filename and either an email.message.Message object containing the response headers (for remote URLs) or None (for local URLs). The caller must then open and read the contents of filename. If filename is not given and the URL refers to a local file, the input filename is returned. If the URL is non-local and filename is not given, the filename is the output of tempfile.mktemp() with a suffix that matches the suffix of the last path component of the input URL. If reporthook is given, it must be a function accepting three numeric parameters: A chunk number, the maximum size chunks are read in and the total size of the download (-1 if unknown). It will be called once at the start and after each chunk of data is read from the network. reporthook is ignored for local URLs.

If the url uses the http: scheme identifier, the optional data argument may be given to specify a POST request (normally the request type is GET). The data argument must in standard application/x-www-form-urlencoded format; see the urllib.parse.urlencode() function.

version

Variable that specifies the user agent of the opener object. To get urllib to tell servers that it is a particular user agent, set this in a subclass as a class variable or in the constructor before calling the base constructor.

class urllib.request.FancyURLopener(...)

3.3 版后已移除.

FancyURLopener subclasses URLopener providing default handling for the following HTTP response codes: 301, 302, 303, 307 and 401. For the 30x response codes listed above, the Location header is used to fetch the actual URL. For 401 response codes (authentication required), basic HTTP authentication is performed. For the 30x response codes, recursion is bounded by the value of the maxtries attribute, which defaults to 10.

For all other response codes, the method http_error_default() is called which you can override in subclasses to handle the error appropriately.

注解

According to the letter of RFC 2616 [https://tools.ietf.org/html/rfc2616.html], 301 and 302 responses to POST requests must not be automatically redirected without confirmation by the user. In reality, browsers do allow automatic redirection of these responses, changing the POST to a GET, and urllib reproduces this behaviour.

The parameters to the constructor are the same as those for URLopener.

注解

When performing basic authentication, a FancyURLopener instance calls its prompt_user_passwd() method. The default implementation asks the users for the required information on the controlling terminal. A subclass may override this method to support more appropriate behavior if needed.

The FancyURLopener class offers one additional method that should be overloaded to provide the appropriate behavior:

prompt_user_passwd(host, realm)

Return information needed to authenticate the user at the given host in the specified security realm. The return value should be a tuple, (user, password), which can be used for basic authentication.

The implementation prompts for this information on the terminal; an application should override this method to use an appropriate interaction model in the local environment.

21.6.25. urllib.request Restrictions

Currently, only the following protocols are supported: HTTP (versions 0.9 and 1.0), FTP, local files, and data URLs.

在 3.4 版更改: Added support for data URLs.

The caching feature of urlretrieve() has been disabled until someone finds the time to hack proper processing of Expiration time headers.

There should be a function to query whether a particular URL is in the cache.

For backward compatibility, if a URL appears to point to a local file but the file can’t be opened, the URL is re-interpreted using the FTP protocol. This can sometimes cause confusing error messages.

The urlopen() and urlretrieve() functions can cause arbitrarily long delays while waiting for a network connection to be set up. This means that it is difficult to build an interactive Web client using these functions without using threads.

The data returned by urlopen() or urlretrieve() is the raw data returned by the server. This may be binary data (such as an image), plain text or (for example) HTML. The HTTP protocol provides type information in the reply header, which can be inspected by looking at the Content-Type header. If the returned data is HTML, you can use the module html.parser to parse it.

The code handling the FTP protocol cannot differentiate between a file and a directory. This can lead to unexpected behavior when attempting to read a URL that points to a file that is not accessible. If the URL ends in a /, it is assumed to refer to a directory and will be handled accordingly. But if an attempt to read a file leads to a 550 error (meaning the URL cannot be found or is not accessible, often for permission reasons), then the path is treated as a directory in order to handle the case when a directory is specified by a URL but the trailing / has been left off. This can cause misleading results when you try to fetch a file whose read permissions make it inaccessible; the FTP code will try to read it, fail with a 550 error, and then perform a directory listing for the unreadable file. If fine-grained control is needed, consider using the ftplib module, subclassing FancyURLopener, or changing _urlopener to meet your needs.

21.7. urllib.response — urllib 使用的 Response 类

The urllib.response module defines functions and classes which define a minimal file like interface, including read() and readline(). The typical response object is an addinfourl instance, which defines an info() method and that returns headers and a geturl() method that returns the url. Functions defined by this module are used internally by the urllib.request module.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.8. urllib.parse — Parse URLs into components

源代码: Lib/urllib/parse.py [https://github.com/python/cpython/tree/3.6/Lib/urllib/parse.py]

This module defines a standard interface to break Uniform Resource Locator (URL) strings up in components (addressing scheme, network location, path etc.), to combine the components back into a URL string, and to convert a “relative URL” to an absolute URL given a “base URL.”

The module has been designed to match the Internet RFC on Relative Uniform Resource Locators. It supports the following URL schemes: file, ftp, gopher, hdl, http, https, imap, mailto, mms, news, nntp, prospero, rsync, rtsp, rtspu, sftp, shttp, sip, sips, snews, svn, svn+ssh, telnet, wais, ws, wss.

The urllib.parse module defines functions that fall into two broad categories: URL parsing and URL quoting. These are covered in detail in the following sections.

21.8.1. URL 解析

The URL parsing functions focus on splitting a URL string into its components, or on combining URL components into a URL string.

urllib.parse.urlparse(urlstring, scheme='', allow_fragments=True)

Parse a URL into six components, returning a 6-tuple. This corresponds to the general structure of a URL: scheme://netloc/path;parameters?query#fragment. Each tuple item is a string, possibly empty. The components are not broken up in smaller parts (for example, the network location is a single string), and % escapes are not expanded. The delimiters as shown above are not part of the result, except for a leading slash in the path component, which is retained if present. For example:

>>> from urllib.parse import urlparse
>>> o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
>>> o
ParseResult(scheme='http', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
            params='', query='', fragment='')
>>> o.scheme
'http'
>>> o.port
80
>>> o.geturl()
'http://www.cwi.nl:80/%7Eguido/Python.html'

Following the syntax specifications in RFC 1808 [https://tools.ietf.org/html/rfc1808.html], urlparse recognizes a netloc only if it is properly introduced by ‘//’. Otherwise the input is presumed to be a relative URL and thus to start with a path component.

>>> from urllib.parse import urlparse
>>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
           params='', query='', fragment='')
>>> urlparse('www.cwi.nl/%7Eguido/Python.html')
ParseResult(scheme='', netloc='', path='www.cwi.nl/%7Eguido/Python.html',
           params='', query='', fragment='')
>>> urlparse('help/Python.html')
ParseResult(scheme='', netloc='', path='help/Python.html', params='',
           query='', fragment='')

The scheme argument gives the default addressing scheme, to be used only if the URL does not specify one. It should be the same type (text or bytes) as urlstring, except that the default value '' is always allowed, and is automatically converted to b'' if appropriate.

If the allow_fragments argument is false, fragment identifiers are not recognized. Instead, they are parsed as part of the path, parameters or query component, and fragment is set to the empty string in the return value.

The return value is actually an instance of a subclass of tuple. This class has the following additional read-only convenience attributes:

属性

索引

值

值（如果不存在）

scheme

0

URL方案说明符

scheme parameter

netloc

1

网络位置部分

空字符串

path

2

分层路径

空字符串

params

3

最后路径元素的参数

空字符串

query

4

查询组件

空字符串

fragment

5

片段识别

空字符串

username

用户名

None

password

密码

None

hostname

主机名（小写）

None

port

端口号为整数（如果存在）

None

Reading the port attribute will raise a ValueError if an invalid port is specified in the URL. See section 结构化解析结果 for more information on the result object.

Unmatched square brackets in the netloc attribute will raise a ValueError.

Characters in the netloc attribute that decompose under NFKC normalization (as used by the IDNA encoding) into any of /, ?, #, @, or : will raise a ValueError. If the URL is decomposed before parsing, no error will be raised.

在 3.2 版更改: 添加了IPv6 URL解析功能。

在 3.3 版更改: The fragment is now parsed for all URL schemes (unless allow_fragment is false), in accordance with RFC 3986 [https://tools.ietf.org/html/rfc3986.html]. Previously, a whitelist of schemes that support fragments existed.

在 3.6 版更改: Out-of-range port numbers now raise ValueError, instead of returning None.

在 3.6.9 版更改: Characters that affect netloc parsing under NFKC normalization will now raise ValueError.

urllib.parse.parse_qs(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace', max_num_fields=None, separator='&')

Parse a query string given as a string argument (data of type application/x-www-form-urlencoded). Data are returned as a dictionary. The dictionary keys are the unique query variable names and the values are lists of values for each name.

The optional argument keep_blank_values is a flag indicating whether blank values in percent-encoded queries should be treated as blank strings. A true value indicates that blanks should be retained as blank strings. The default false value indicates that blank values are to be ignored and treated as if they were not included.

The optional argument strict_parsing is a flag indicating what to do with parsing errors. If false (the default), errors are silently ignored. If true, errors raise a ValueError exception.

The optional encoding and errors parameters specify how to decode percent-encoded sequences into Unicode characters, as accepted by the bytes.decode() method.

The optional argument max_num_fields is the maximum number of fields to read. If set, then throws a ValueError if there are more than max_num_fields fields read.

The optional argument separator is the symbol to use for separating the query arguments. It defaults to &.

Use the urllib.parse.urlencode() function (with the doseq parameter set to True) to convert such dictionaries into query strings.

在 3.2 版更改: Add encoding and errors parameters.

在 3.6.8 版更改: Added max_num_fields parameter.

在 3.6.13 版更改: Added separator parameter with the default value of &. Python versions earlier than Python 3.6.13 allowed using both ; and & as query parameter separator. This has been changed to allow only a single separator key, with & as the default separator.

urllib.parse.parse_qsl(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace', max_num_fields=None, separator='&')

Parse a query string given as a string argument (data of type application/x-www-form-urlencoded). Data are returned as a list of name, value pairs.

The optional argument keep_blank_values is a flag indicating whether blank values in percent-encoded queries should be treated as blank strings. A true value indicates that blanks should be retained as blank strings. The default false value indicates that blank values are to be ignored and treated as if they were not included.

The optional argument strict_parsing is a flag indicating what to do with parsing errors. If false (the default), errors are silently ignored. If true, errors raise a ValueError exception.

The optional encoding and errors parameters specify how to decode percent-encoded sequences into Unicode characters, as accepted by the bytes.decode() method.

The optional argument max_num_fields is the maximum number of fields to read. If set, then throws a ValueError if there are more than max_num_fields fields read.

The optional argument separator is the symbol to use for separating the query arguments. It defaults to &.

Use the urllib.parse.urlencode() function to convert such lists of pairs into query strings.

在 3.2 版更改: Add encoding and errors parameters.

在 3.6.8 版更改: Added max_num_fields parameter.

在 3.6.13 版更改: Added separator parameter with the default value of &. Python versions earlier than Python 3.6.13 allowed using both ; and & as query parameter separator. This has been changed to allow only a single separator key, with & as the default separator.

urllib.parse.urlunparse(parts)

Construct a URL from a tuple as returned by urlparse(). The parts argument can be any six-item iterable. This may result in a slightly different, but equivalent URL, if the URL that was parsed originally had unnecessary delimiters (for example, a ? with an empty query; the RFC states that these are equivalent).

urllib.parse.urlsplit(urlstring, scheme='', allow_fragments=True)

This is similar to urlparse(), but does not split the params from the URL. This should generally be used instead of urlparse() if the more recent URL syntax allowing parameters to be applied to each segment of the path portion of the URL (see RFC 2396 [https://tools.ietf.org/html/rfc2396.html]) is wanted. A separate function is needed to separate the path segments and parameters. This function returns a 5-tuple: (addressing scheme, network location, path, query, fragment identifier).

The return value is actually an instance of a subclass of tuple. This class has the following additional read-only convenience attributes:

属性

索引

值

值（如果不存在）

scheme

0

URL方案说明符

scheme parameter

netloc

1

网络位置部分

空字符串

path

2

分层路径

空字符串

query

3

查询组件

空字符串

fragment

4

片段识别

空字符串

username

用户名

None

password

密码

None

hostname

主机名（小写）

None

port

端口号为整数（如果存在）

None

Reading the port attribute will raise a ValueError if an invalid port is specified in the URL. See section 结构化解析结果 for more information on the result object.

Unmatched square brackets in the netloc attribute will raise a ValueError.

Characters in the netloc attribute that decompose under NFKC normalization (as used by the IDNA encoding) into any of /, ?, #, @, or : will raise a ValueError. If the URL is decomposed before parsing, no error will be raised.

在 3.6 版更改: Out-of-range port numbers now raise ValueError, instead of returning None.

在 3.6.9 版更改: Characters that affect netloc parsing under NFKC normalization will now raise ValueError.

urllib.parse.urlunsplit(parts)

Combine the elements of a tuple as returned by urlsplit() into a complete URL as a string. The parts argument can be any five-item iterable. This may result in a slightly different, but equivalent URL, if the URL that was parsed originally had unnecessary delimiters (for example, a ? with an empty query; the RFC states that these are equivalent).

urllib.parse.urljoin(base, url, allow_fragments=True)

Construct a full (“absolute”) URL by combining a “base URL” (base) with another URL (url). Informally, this uses components of the base URL, in particular the addressing scheme, the network location and (part of) the path, to provide missing components in the relative URL. For example:

>>> from urllib.parse import urljoin
>>> urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')
'http://www.cwi.nl/%7Eguido/FAQ.html'

The allow_fragments argument has the same meaning and default as for urlparse().

注解

If url is an absolute URL (that is, starting with // or scheme://), the url’s host name and/or scheme will be present in the result. For example:

>>> urljoin('http://www.cwi.nl/%7Eguido/Python.html',
...         '//www.python.org/%7Eguido')
'http://www.python.org/%7Eguido'

If you do not want that behavior, preprocess the url with urlsplit() and urlunsplit(), removing possible scheme and netloc parts.

在 3.5 版更改: Behaviour updated to match the semantics defined in RFC 3986 [https://tools.ietf.org/html/rfc3986.html].

urllib.parse.urldefrag(url)

If url contains a fragment identifier, return a modified version of url with no fragment identifier, and the fragment identifier as a separate string. If there is no fragment identifier in url, return url unmodified and an empty string.

The return value is actually an instance of a subclass of tuple. This class has the following additional read-only convenience attributes:

属性

索引

值

值（如果不存在）

url

0

URL with no fragment

空字符串

fragment

1

片段识别

空字符串

See section 结构化解析结果 for more information on the result object.

在 3.2 版更改: Result is a structured object rather than a simple 2-tuple.

21.8.2. 解析ASCII编码字节

The URL parsing functions were originally designed to operate on character strings only. In practice, it is useful to be able to manipulate properly quoted and encoded URLs as sequences of ASCII bytes. Accordingly, the URL parsing functions in this module all operate on bytes and bytearray objects in addition to str objects.

If str data is passed in, the result will also contain only str data. If bytes or bytearray data is passed in, the result will contain only bytes data.

Attempting to mix str data with bytes or bytearray in a single function call will result in a TypeError being raised, while attempting to pass in non-ASCII byte values will trigger UnicodeDecodeError.

To support easier conversion of result objects between str and bytes, all return values from URL parsing functions provide either an encode() method (when the result contains str data) or a decode() method (when the result contains bytes data). The signatures of these methods match those of the corresponding str and bytes methods (except that the default encoding is 'ascii' rather than 'utf-8'). Each produces a value of a corresponding type that contains either bytes data (for encode() methods) or str data (for decode() methods).

Applications that need to operate on potentially improperly quoted URLs that may contain non-ASCII data will need to do their own decoding from bytes to characters before invoking the URL parsing methods.

The behaviour described in this section applies only to the URL parsing functions. The URL quoting functions use their own rules when producing or consuming byte sequences as detailed in the documentation of the individual URL quoting functions.

在 3.2 版更改: URL parsing functions now accept ASCII encoded byte sequences

21.8.3. 结构化解析结果

The result objects from the urlparse(), urlsplit() and urldefrag() functions are subclasses of the tuple type. These subclasses add the attributes listed in the documentation for those functions, the encoding and decoding support described in the previous section, as well as an additional method:

urllib.parse.SplitResult.geturl()

Return the re-combined version of the original URL as a string. This may differ from the original URL in that the scheme may be normalized to lower case and empty components may be dropped. Specifically, empty parameters, queries, and fragment identifiers will be removed.

For urldefrag() results, only empty fragment identifiers will be removed. For urlsplit() and urlparse() results, all noted changes will be made to the URL returned by this method.

The result of this method remains unchanged if passed back through the original parsing function:

>>> from urllib.parse import urlsplit
>>> url = 'HTTP://www.Python.org/doc/#'
>>> r1 = urlsplit(url)
>>> r1.geturl()
'http://www.Python.org/doc/'
>>> r2 = urlsplit(r1.geturl())
>>> r2.geturl()
'http://www.Python.org/doc/'

The following classes provide the implementations of the structured parse results when operating on str objects:

class urllib.parse.DefragResult(url, fragment)

Concrete class for urldefrag() results containing str data. The encode() method returns a DefragResultBytes instance.

3.2 新版功能.

class urllib.parse.ParseResult(scheme, netloc, path, params, query, fragment)

Concrete class for urlparse() results containing str data. The encode() method returns a ParseResultBytes instance.

class urllib.parse.SplitResult(scheme, netloc, path, query, fragment)

Concrete class for urlsplit() results containing str data. The encode() method returns a SplitResultBytes instance.

The following classes provide the implementations of the parse results when operating on bytes or bytearray objects:

class urllib.parse.DefragResultBytes(url, fragment)

Concrete class for urldefrag() results containing bytes data. The decode() method returns a DefragResult instance.

3.2 新版功能.

class urllib.parse.ParseResultBytes(scheme, netloc, path, params, query, fragment)

Concrete class for urlparse() results containing bytes data. The decode() method returns a ParseResult instance.

3.2 新版功能.

class urllib.parse.SplitResultBytes(scheme, netloc, path, query, fragment)

Concrete class for urlsplit() results containing bytes data. The decode() method returns a SplitResult instance.

3.2 新版功能.

21.8.4. URL Quoting

The URL quoting functions focus on taking program data and making it safe for use as URL components by quoting special characters and appropriately encoding non-ASCII text. They also support reversing these operations to recreate the original data from the contents of a URL component if that task isn’t already covered by the URL parsing functions above.

urllib.parse.quote(string, safe='/', encoding=None, errors=None)

Replace special characters in string using the %xx escape. Letters, digits, and the characters '_.-' are never quoted. By default, this function is intended for quoting the path section of URL. The optional safe parameter specifies additional ASCII characters that should not be quoted — its default value is '/'.

string may be either a str or a bytes.

The optional encoding and errors parameters specify how to deal with non-ASCII characters, as accepted by the str.encode() method. encoding defaults to 'utf-8'. errors defaults to 'strict', meaning unsupported characters raise a UnicodeEncodeError. encoding and errors must not be supplied if string is a bytes, or a TypeError is raised.

Note that quote(string, safe, encoding, errors) is equivalent to quote_from_bytes(string.encode(encoding, errors), safe).

Example: quote('/El Niño/') yields '/El%20Ni%C3%B1o/'.

urllib.parse.quote_plus(string, safe='', encoding=None, errors=None)

Like quote(), but also replace spaces by plus signs, as required for quoting HTML form values when building up a query string to go into a URL. Plus signs in the original string are escaped unless they are included in safe. It also does not have safe default to '/'.

Example: quote_plus('/El Niño/') yields '%2FEl+Ni%C3%B1o%2F'.

urllib.parse.quote_from_bytes(bytes, safe='/')

Like quote(), but accepts a bytes object rather than a str, and does not perform string-to-bytes encoding.

Example: quote_from_bytes(b'a&\xef') yields 'a%26%EF'.

urllib.parse.unquote(string, encoding='utf-8', errors='replace')

Replace %xx escapes by their single-character equivalent. The optional encoding and errors parameters specify how to decode percent-encoded sequences into Unicode characters, as accepted by the bytes.decode() method.

string must be a str.

encoding defaults to 'utf-8'. errors defaults to 'replace', meaning invalid sequences are replaced by a placeholder character.

Example: unquote('/El%20Ni%C3%B1o/') yields '/El Niño/'.

urllib.parse.unquote_plus(string, encoding='utf-8', errors='replace')

Like unquote(), but also replace plus signs by spaces, as required for unquoting HTML form values.

string must be a str.

Example: unquote_plus('/El+Ni%C3%B1o/') yields '/El Niño/'.

urllib.parse.unquote_to_bytes(string)

Replace %xx escapes by their single-octet equivalent, and return a bytes object.

string may be either a str or a bytes.

If it is a str, unescaped non-ASCII characters in string are encoded into UTF-8 bytes.

Example: unquote_to_bytes('a%26%EF') yields b'a&\xef'.

urllib.parse.urlencode(query, doseq=False, safe='', encoding=None, errors=None, quote_via=quote_plus)

Convert a mapping object or a sequence of two-element tuples, which may contain str or bytes objects, to a percent-encoded ASCII text string. If the resultant string is to be used as a data for POST operation with the urlopen() function, then it should be encoded to bytes, otherwise it would result in a TypeError.

The resulting string is a series of key=value pairs separated by '&' characters, where both key and value are quoted using the quote_via function. By default, quote_plus() is used to quote the values, which means spaces are quoted as a '+' character and ‘/’ characters are encoded as %2F, which follows the standard for GET requests (application/x-www-form-urlencoded). An alternate function that can be passed as quote_via is quote(), which will encode spaces as %20 and not encode ‘/’ characters. For maximum control of what is quoted, use quote and specify a value for safe.

When a sequence of two-element tuples is used as the query argument, the first element of each tuple is a key and the second is a value. The value element in itself can be a sequence and in that case, if the optional parameter doseq is evaluates to True, individual key=value pairs separated by '&' are generated for each element of the value sequence for the key. The order of parameters in the encoded string will match the order of parameter tuples in the sequence.

The safe, encoding, and errors parameters are passed down to quote_via (the encoding and errors parameters are only passed when a query element is a str).

To reverse this encoding process, parse_qs() and parse_qsl() are provided in this module to parse query strings into Python data structures.

Refer to urllib examples to find out how urlencode method can be used for generating query string for a URL or data for POST.

在 3.2 版更改: 查询参数支持字节和字符串对象。

3.5 新版功能: quote_via 参数.

参见

RFC 3986 [https://tools.ietf.org/html/rfc3986.html] - 统一资源标识符

This is the current standard (STD66). Any changes to urllib.parse module should conform to this. Certain deviations could be observed, which are mostly for backward compatibility purposes and for certain de-facto parsing requirements as commonly observed in major browsers.

RFC 2732 [https://tools.ietf.org/html/rfc2732.html] - Format for Literal IPv6 Addresses in URL’s.

This specifies the parsing requirements of IPv6 URLs.

RFC 2396 [https://tools.ietf.org/html/rfc2396.html] - 统一资源标识符（URI）：通用语法

Document describing the generic syntactic requirements for both Uniform Resource Names (URNs) and Uniform Resource Locators (URLs).

RFC 2368 [https://tools.ietf.org/html/rfc2368.html] - The mailto URL scheme.

Parsing requirements for mailto URL schemes.

RFC 1808 [https://tools.ietf.org/html/rfc1808.html] - Relative Uniform Resource Locators

This Request For Comments includes the rules for joining an absolute and a relative URL, including a fair number of “Abnormal Examples” which govern the treatment of border cases.

RFC 1738 [https://tools.ietf.org/html/rfc1738.html] - Uniform Resource Locators (URL)

This specifies the formal syntax and semantics of absolute URLs.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.9. urllib.error — urllib.request 引发的异常类

源代码： Lib/urllib/error.py [https://github.com/python/cpython/tree/3.6/Lib/urllib/error.py]

urllib.error 模块为 urllib.request 所引发的异常定义了异常类。 基础异常类是 URLError。

下列异常会被 urllib.error 按需引发：

exception urllib.error.URLError

处理程序在遇到问题时会引发此异常（或其派生的异常）。 它是 OSError 的一个子类。

reason

此错误的原因。 它可以是一个消息字符串或另一个异常实例。

在 3.3 版更改: URLError 已被设为 OSError 而不是 IOError 的子类。

exception urllib.error.HTTPError

虽然是一个异常 (URLError 的一个子类)，HTTPError 也可以作为一个非异常的文件类返回值 (与 urlopen() 返所回的对象相同)。 这适用于处理特殊 HTTP 错误例如作为认证请求的时候。

code

一个 HTTP 状态码，具体定义见 RFC 2616 [https://tools.ietf.org/html/rfc2616.html]。 这个数字值对应于存放在 http.server.BaseHTTPRequestHandler.responses 代码字典中的某个值。

reason

这通常是一个解释本次错误原因的字符串。

headers

导致了 HTTPError 的特定 HTTP 请求的 HTTP 响应头。

3.4 新版功能.

exception urllib.error.ContentTooShortError(msg, content)

此异常会在 urlretrieve() 函数检测到已下载的数据量小于期待的数据量（由 Content-Length 头给定）时被引发。 content 属性中将存放已下载（可能被截断）的数据。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.10. urllib.robotparser — robots.txt 语法分析程序

源代码： Lib/urllib/robotparser.py [https://github.com/python/cpython/tree/3.6/Lib/urllib/robotparser.py]

此模块提供了一个单独的类 RobotFileParser，它可以回答关于某个特定用户代理是否能在 Web 站点获取发布 robots.txt 文件的 URL 的问题。 有关 robots.txt 文件结构的更多细节请参阅 http://www.robotstxt.org/orig.html。

class urllib.robotparser.RobotFileParser(url='')

这个类提供了一些可以读取、解析和回答关于 url 上的 robots.txt 文件的问题的方法。

set_url(url)

设置指向 robots.txt 文件的 URL。

read()

读取 robots.txt URL 并将其输入解析器。

parse(lines)

解析行参数。

can_fetch(useragent, url)

如果允许 useragent 按照被解析 robots.txt 文件中的规则来获取 url 则返回 True。

mtime()

返回最近一次获取 robots.txt 文件的时间。 这适用于需要定期检查 robots.txt 文件更新情况的长时间运行的网页爬虫。

modified()

将最近一次获取 robots.txt 文件的时间设置为当前时间。

crawl_delay(useragent)

为指定的 useragent 从 robots.txt 返回 Crawl-delay 形参。 如果此形参不存在或不适用于指定的 useragent 或者此形参的 robots.txt 条目存在语法错误，则返回 None。

3.6 新版功能.

request_rate(useragent)

以 named tuple RequestRate(requests, seconds) 的形式从 robots.txt 返回 Request-rate 形参的内容。 如果此形参不存在或不适用于指定的 useragent 或者此形参的 robots.txt 条目存在语法错误，则返回 None。

3.6 新版功能.

下面的例子演示了 RobotFileParser 类的基本用法:

>>> import urllib.robotparser
>>> rp = urllib.robotparser.RobotFileParser()
>>> rp.set_url("http://www.musi-cal.com/robots.txt")
>>> rp.read()
>>> rrate = rp.request_rate("*")
>>> rrate.requests
3
>>> rrate.seconds
20
>>> rp.crawl_delay("*")
6
>>> rp.can_fetch("*", "http://www.musi-cal.com/cgi-bin/search?city=San+Francisco")
False
>>> rp.can_fetch("*", "http://www.musi-cal.com/")
True

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.11. http — HTTP 模块

源代码: Lib/http/__init__.py [https://github.com/python/cpython/tree/3.6/Lib/http/__init__.py]

http 是一个包，它收集了多个用于处理超文本传输协议的模块:

http.client 是一个低层级的 HTTP 协议客户端；对于高层级的 URL 访问请使用 urllib.request

http.server 包含基于 socketserver 的基本 HTTP 服务类

http.cookies 包含一些有用来实现通过 cookies 进行状态管理的工具

http.cookiejar 提供了 cookies 的持久化

http 也是一个通过 http.HTTPStatus 枚举定义了一些 HTTP 状态码以及相关联消息的模块

class http.HTTPStatus

3.5 新版功能.

enum.IntEnum 的子类，它定义了组 HTTP 状态码，原理短语以及用英语书写的长描述文本。

用法:

>>> from http import HTTPStatus
>>> HTTPStatus.OK
<HTTPStatus.OK: 200>
>>> HTTPStatus.OK == 200
True
>>> http.HTTPStatus.OK.value
200
>>> HTTPStatus.OK.phrase
'OK'
>>> HTTPStatus.OK.description
'Request fulfilled, document follows'
>>> list(HTTPStatus)
[<HTTPStatus.CONTINUE: 100>, <HTTPStatus.SWITCHING_PROTOCOLS: 101>, ...]

21.11.1. HTTP 状态码

已支持并且已在 http.HTTPStatus IANA 注册 [https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml] 的状态码有：

双字母代码

映射名

详情

100

CONTINUE：继续

HTTP/1.1 RFC 7231, Section 6.2.1

101

SWITCHING_PROTOCOLS

HTTP/1.1 RFC 7231, Section 6.2.2

102

PROCESSING

WebDAV RFC 2518, Section 10.1

200

OK

HTTP/1.1 RFC 7231, Section 6.3.1

201

CREATED

HTTP/1.1 RFC 7231, Section 6.3.2

202

ACCEPTED

HTTP/1.1 RFC 7231, Section 6.3.3

203

NON_AUTHORITATIVE_INFORMATION

HTTP/1.1 RFC 7231, Section 6.3.4

204

NO_CONTENT: 没有内容

HTTP/1.1 RFC 7231, Section 6.3.5

205

RESET_CONTENT

HTTP/1.1 RFC 7231, Section 6.3.6

206

PARTIAL_CONTENT

HTTP/1.1 RFC 7233, Section 4.1

207

MULTI_STATUS

WebDAV RFC 4918, Section 11.1

208

ALREADY_REPORTED

WebDAV Binding Extensions RFC 5842, Section 7.1 (Experimental)

226

IM_USED

Delta Encoding in HTTP RFC 3229, Section 10.4.1

300

MULTIPLE_CHOICES：有多种资源可选择

HTTP/1.1 RFC 7231, Section 6.4.1

301

MOVED_PERMANENTLY：永久移动

HTTP/1.1 RFC 7231, Section 6.4.2

302

FOUND：临时移动

HTTP/1.1 RFC 7231, Section 6.4.3

303

SEE_OTHER：已经移动

HTTP/1.1 RFC 7231, Section 6.4.4

304

NOT_MODIFIED：没有修改

HTTP/1.1 RFC 7232, Section 4.1

305

USE_PROXY：使用代理

HTTP/1.1 RFC 7231, Section 6.4.5

307

TEMPORARY_REDIRECT：临时重定向

HTTP/1.1 RFC 7231 [https://tools.ietf.org/html/rfc7231.html], Section 6.4.7

308

PERMANENT_REDIRECT：永久重定向

Permanent Redirect RFC 7238 [https://tools.ietf.org/html/rfc7238.html], Section 3 (Experimental)

400

BAD_REQUEST：错误请求

HTTP/1.1 RFC 7231, Section 6.5.1

401

UNAUTHORIZED：未授权

HTTP/1.1 Authentication RFC 7235, Section 3.1

402

PAYMENT_REQUIRED：保留，将来使用

HTTP/1.1 RFC 7231, Section 6.5.2

403

FORBIDDEN：禁止

HTTP/1.1 RFC 7231, Section 6.5.3

404

NOT_FOUND：没有找到

HTTP/1.1 RFC 7231, Section 6.5.4

405

METHOD_NOT_ALLOWED：该请求方法不允许

HTTP/1.1 RFC 7231, Section 6.5.5

406

NOT_ACCEPTABLE：不可接受

HTTP/1.1 RFC 7231, Section 6.5.6

407

PROXY_AUTHENTICATION_REQUIRED：要求使用代理验明正身

HTTP/1.1 Authentication RFC 7235, Section 3.2

408

REQUEST_TIMEOUT：请求超时

HTTP/1.1 RFC 7231, Section 6.5.7

409

CONFLICT：冲突

HTTP/1.1 RFC 7231, Section 6.5.8

410

GONE：已经不在了

HTTP/1.1 RFC 7231, Section 6.5.9

411

LENGTH_REQUIRED：长度要求

HTTP/1.1 RFC 7231, Section 6.5.10

412

PRECONDITION_FAILED：前提条件错误

HTTP/1.1 RFC 7232, Section 4.2

413

REQUEST_ENTITY_TOO_LARGE：请求体太大了

HTTP/1.1 RFC 7231, Section 6.5.11

414

REQUEST_URI_TOO_LONG：请求URI太长了

HTTP/1.1 RFC 7231, Section 6.5.12

415

UNSUPPORTED_MEDIA_TYPE：不支持的媒体格式

HTTP/1.1 RFC 7231, Section 6.5.13

416

REQUEST_RANGE_NOT_SATISFIABLE

HTTP/1.1 Range Requests RFC 7233, Section 4.4

417

EXPECTATION_FAILED：期望失败

HTTP/1.1 RFC 7231, Section 6.5.14

422

UNPROCESSABLE_ENTITY：可加工实体

WebDAV RFC 4918, Section 11.2

423

LOCKED：锁着

WebDAV RFC 4918, Section 11.3

424

FAILED_DEPENDENCY：失败的依赖

WebDAV RFC 4918, Section 11.4

426

UPGRADE_REQUIRED：升级需要

HTTP/1.1 RFC 7231, Section 6.5.15

428

PRECONDITION_REQUIRED：先决条件要求

Additional HTTP Status Codes RFC 6585

429

TOO_MANY_REQUESTS：太多的请求

Additional HTTP Status Codes RFC 6585

431

REQUEST_HEADER_FIELDS_TOO_LARGE：请求头太大

Additional HTTP Status Codes RFC 6585

500

INTERNAL_SERVER_ERROR：内部服务错误

HTTP/1.1 RFC 7231, Section 6.6.1

501

NOT_IMPLEMENTED：不可执行

HTTP/1.1 RFC 7231, Section 6.6.2

502

BAD_GATEWAY：无效网关

HTTP/1.1 RFC 7231, Section 6.6.3

503

SERVICE_UNAVAILABLE：服务不可用

HTTP/1.1 RFC 7231, Section 6.6.4

504

GATEWAY_TIMEOUT：网关超时

HTTP/1.1 RFC 7231, Section 6.6.5

505

HTTP_VERSION_NOT_SUPPORTED：HTTP版本不支持

HTTP/1.1 RFC 7231, Section 6.6.6

506

VARIANT_ALSO_NEGOTIATES：服务器存在内部配置错误

透明内容协商在： HTTP RFC 2295 [https://tools.ietf.org/html/rfc2295.html], Section 8.1 (实验性的)

507

INSUFFICIENT_STORAGE：存储不足

WebDAV RFC 4918, Section 11.5

508

LOOP_DETECTED：循环检测

WebDAV Binding Extensions RFC 5842, Section 7.2 (Experimental)

510

NOT_EXTENDED：不扩展

An HTTP Extension Framework RFC 2774 [https://tools.ietf.org/html/rfc2774.html], Section 7 (Experimental)

511

NETWORK_AUTHENTICATION_REQUIRED：要求网络身份验证

Additional HTTP Status Codes RFC 6585 [https://tools.ietf.org/html/rfc6585.html], Section 6

为了保持向后兼容性，枚举值也以常量形式出现在 http.client 模块中，。 枚举名等于常量名 (例如 http.HTTPStatus.OK 也可以是 http.client.OK)。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.12. http.client — HTTP 协议客户端

源代码： Lib/http/client.py [https://github.com/python/cpython/tree/3.6/Lib/http/client.py]

这个模块定义了实现 HTTP 和 HTTPS 协议客户端的类。 它通常不直接使用 — 模块 urllib.request 用它来处理使用 HTTP 和 HTTPS 的 URL。

参见

The Requests package [http://docs.python-requests.org/] is recommended for a higher-level HTTP client interface.

注解

HTTPS 支持仅在编译 Python 时启用了 SSL 支持的情况下（通过 ssl 模块）可用。

该模块支持以下类：

class http.client.HTTPConnection(host, port=None, [timeout, ]source_address=None)

An HTTPConnection instance represents one transaction with an HTTP server. It should be instantiated passing it a host and optional port number. If no port number is passed, the port is extracted from the host string if it has the form host:port, else the default HTTP port (80) is used. If the optional timeout parameter is given, blocking operations (like connection attempts) will timeout after that many seconds (if it is not given, the global default timeout setting is used). The optional source_address parameter may be a tuple of a (host, port) to use as the source address the HTTP connection is made from.

举个例子，以下调用都是创建连接到同一主机和端口的服务器的实例：

>>> h1 = http.client.HTTPConnection('www.python.org')
>>> h2 = http.client.HTTPConnection('www.python.org:80')
>>> h3 = http.client.HTTPConnection('www.python.org', 80)
>>> h4 = http.client.HTTPConnection('www.python.org', 80, timeout=10)

在 3.2 版更改: 添加了*source_address* 参数

在 3.4 版更改: 删除了 strict 参数，不再支持 HTTP 0.9 风格的“简单响应”。

class http.client.HTTPSConnection(host, port=None, key_file=None, cert_file=None, [timeout, ]source_address=None, *, context=None, check_hostname=None)

HTTPConnection 的子类，使用 SSL 与安全服务器进行通信。 默认端口为 443。 如果指定了 context，它必须为一个描述 SSL 各选项的 ssl.SSLContext 实例。

请参阅 安全考量 了解有关最佳实践的更多信息。

在 3.2 版更改: 添加了 source_address, context 和 check_hostname。

在 3.2 版更改: 这个类目前会在可能的情况下（即如果 ssl.HAS_SNI 为真值）支持 HTTPS 虚拟主机。

在 3.4 版更改: 删除了 strict 参数，不再支持 HTTP 0.9 风格的“简单响应”。

在 3.4.3 版更改: 目前这个类在默认情况下会执行所有必要的证书和主机检查。 要回复到先前的非验证行为，可以将 ssl._create_unverified_context() 传递给 context 参数。

3.6 版后已移除: key_file 和 cert_file 已弃用并转而推荐 context。 请改用 ssl.SSLContext.load_cert_chain() 或让 ssl.create_default_context() 为你选择系统所信任的 CA 证书。

check_hostname 参数也已弃用；应当改用 context 的 ssl.SSLContext.check_hostname 属性。

class http.client.HTTPResponse(sock, debuglevel=0, method=None, url=None)

在成功连接后返回类的实例，而不是由用户直接实例化。

在 3.4 版更改: 删除了 strict 参数，不再支持HTTP 0.9 风格的“简单响应”。

下列异常可以适当地被引发:

exception http.client.HTTPException

此模块中其他异常的基类。 它是 Exception 的一个子类。

exception http.client.NotConnected

HTTPException 的一个子类。

exception http.client.InvalidURL

HTTPException 的一个子类，如果给出了一个非数字或为空值的端口就会被引发。

exception http.client.UnknownProtocol

HTTPException 的一个子类。

exception http.client.UnknownTransferEncoding

HTTPException 的一个子类。

exception http.client.UnimplementedFileMode

HTTPException 的一个子类。

exception http.client.IncompleteRead

HTTPException 的一个子类。

exception http.client.ImproperConnectionState

HTTPException 的一个子类。

exception http.client.CannotSendRequest

ImproperConnectionState 的一个子类。

exception http.client.CannotSendHeader

ImproperConnectionState 的一个子类。

exception http.client.ResponseNotReady

ImproperConnectionState 的一个子类。

exception http.client.BadStatusLine

HTTPException 的一个子类。 如果服务器反馈了一个我们不理解的 HTTP 状态码就会被引发。

exception http.client.LineTooLong

HTTPException 的一个子类。 如果在 HTTP 协议中从服务器接收到过长的行就会被引发。

exception http.client.RemoteDisconnected

ConnectionResetError 和 BadStatusLine 的一个子类。 当尝试读取响应时的结果是未从连接读取到数据时由 HTTPConnection.getresponse() 引发，表明远端已关闭连接。

3.5 新版功能: 在此之前引发的异常为 BadStatusLine('')。

此模块中定义的常量为：

http.client.HTTP_PORT

HTTP 协议默认的端口号 (总是 80)。

http.client.HTTPS_PORT

HTTPS 协议默认的端口号 (总是 443)。

http.client.responses

这个字典把 HTTP 1.1 状态码映射到 W3C 名称。

例如：http.client.responses[http.client.NOT_FOUND] 是 'NOT FOUND （未发现）。

本模块中可用的 HTTP 状态码常量可以参见 HTTP 状态码 。

21.12.1. HTTPConnection 对象

HTTPConnection 实例拥有以下方法：

HTTPConnection.request(method, url, body=None, headers={}, *, encode_chunked=False)

这会使用 HTTP 请求方法 method 和选择器 url 向服务器发送请求。

如果给定 body，那么给定的数据会在信息头完成之后发送。它可能是一个 str 、一个 bytes-like object 、一个打开的 file object，或者 bytes 迭代器。如果 body 是字符串，它会按 HTTP 默认的 ISO-8859-1 编码；如果是一个字节类对象，它会按原样发送；如果是 file object ，文件的内容会被发送，这个文件对象应该支持 read() 方法。如果这个文件对象是一个 io.TextIOBase 实例， read() 方法返回的数据会按 ISO-8859-1 编码，否则 read() 方法返回的数据会按原样发送；如果 body 是一个迭代器，迭代器中的元素会被发送，直到迭代器耗尽。

headers 参数应是额外的随请求发送的 HTTP 信息头的字典。

如果 headers 既不包含 Content-Length 也没有 Transfer-Encoding，但存在请求正文，那么这些头字段中的一个会自动设定。如果 body 是 None，那么对于要求正文的方法 (PUT，POST，和 PATCH)，Content-Length 头会被设为 0。如果 body 是字符串或者类似字节的对象，并且也不是 文件，Content-Length 头会设为正文的长度。任何其他类型的 body （一般是文件或迭代器）会按块编码，这时会自动设定 Transfer-Encoding 头以代替 Content-Length。

在 headers 中指定 Transfer-Encoding 时， encode_chunked 是唯一相关的参数。如果 encode_chunked 为 False，HTTPConnection 对象会假定所有的编码都由调用代码处理。如果为 True，正文会按块编码。

注解

HTTP 协议在 1.1 版中添加了块传输编码。除非明确知道 HTTP 服务器可以处理 HTTP 1.1，调用者要么必须指定 Content-Length，要么必须传入 str 或字节类对象，注意该对象不能是表达 body 的文件。

3.2 新版功能: body 现在可以是可迭代对象了。

在 3.6 版更改: 如果 Content-Length 和 Transfer-Encoding 都没有在 headers 中设置，文件和可迭代的 body 对象现在会按块编码。添加了 encode_chunked 参数。不会尝试去确定文件对象的 Content-Length。

HTTPConnection.getresponse()

应当在发送一个请求从服务器获取响应时被调用。 返回一个 HTTPResponse 的实例。

注解

请注意你必须在读取了整个响应之后才能向服务器发送新的请求。

在 3.5 版更改: 如果引发了 ConnectionError 或其子类， HTTPConnection 对象将在发送新的请求时准备好重新连接。

HTTPConnection.set_debuglevel(level)

设置调试等级。 默认的调试等级为 0，意味着不会打印调试输出。 任何大于 0 的值将使得所有当前定义的调试输出被打印到 stdout。 debuglevel 会被传给任何新创建的 HTTPResponse 对象。

3.1 新版功能.

HTTPConnection.set_tunnel(host, port=None, headers=None)

为 HTTP 连接隧道设置主机和端口。 这将允许通过代理服务器运行连接。

host 和 port 参数指明隧道连接的位置（即 CONNECT 请求所包含的地址，而 不是 代理服务器的地址）。

headers 参数应为一个随 CONNECT 请求发送的额外 HTTP 标头的映射。

例如，要通过一个运行于本机 8080 端口的 HTTPS 代理服务器隧道，我们应当向 HTTPSConnection 构造器传入代理的地址，并将我们最终想要访问的主机地址传给 set_tunnel() 方法:

>>> import http.client
>>> conn = http.client.HTTPSConnection("localhost", 8080)
>>> conn.set_tunnel("www.python.org")
>>> conn.request("HEAD","/index.html")

3.2 新版功能.

HTTPConnection.connect()

当对象被创建后连接到指定的服务器。 默认情况下，如果客户端还未建立连接，此函数会在发送请求时自动被调用。

HTTPConnection.close()

关闭到服务器的连接。

作为对使用上述 request() 方法的替代同，你也可以通过使用下面的四个函数，分步骤发送请的请求。

HTTPConnection.putrequest(method, url, skip_host=False, skip_accept_encoding=False)

This should be the first call after the connection to the server has been made. It sends a line to the server consisting of the method string, the url string, and the HTTP version (HTTP/1.1). To disable automatic sending of Host: or Accept-Encoding: headers (for example to accept additional content encodings), specify skip_host or skip_accept_encoding with non-False values.

HTTPConnection.putheader(header, argument[, ...])

Send an RFC 822 [https://tools.ietf.org/html/rfc822.html]-style header to the server. It sends a line to the server consisting of the header, a colon and a space, and the first argument. If more arguments are given, continuation lines are sent, each consisting of a tab and an argument.

HTTPConnection.endheaders(message_body=None, *, encode_chunked=False)

Send a blank line to the server, signalling the end of the headers. The optional message_body argument can be used to pass a message body associated with the request.

If encode_chunked is True, the result of each iteration of message_body will be chunk-encoded as specified in RFC 7230 [https://tools.ietf.org/html/rfc7230.html], Section 3.3.1. How the data is encoded is dependent on the type of message_body. If message_body implements the buffer interface the encoding will result in a single chunk. If message_body is a collections.Iterable, each iteration of message_body will result in a chunk. If message_body is a file object, each call to .read() will result in a chunk. The method automatically signals the end of the chunk-encoded data immediately after message_body.

注解

Due to the chunked encoding specification, empty chunks yielded by an iterator body will be ignored by the chunk-encoder. This is to avoid premature termination of the read of the request by the target server due to malformed encoding.

3.6 新版功能: Chunked encoding support. The encode_chunked parameter was added.

HTTPConnection.send(data)

Send data to the server. This should be used directly only after the endheaders() method has been called and before getresponse() is called.

21.12.2. HTTPResponse 对象

An HTTPResponse instance wraps the HTTP response from the server. It provides access to the request headers and the entity body. The response is an iterable object and can be used in a with statement.

在 3.5 版更改: The io.BufferedIOBase interface is now implemented and all of its reader operations are supported.

HTTPResponse.read([amt])

Reads and returns the response body, or up to the next amt bytes.

HTTPResponse.readinto(b)

Reads up to the next len(b) bytes of the response body into the buffer b. Returns the number of bytes read.

3.3 新版功能.

HTTPResponse.getheader(name, default=None)

Return the value of the header name, or default if there is no header matching name. If there is more than one header with the name name, return all of the values joined by ‘, ‘. If ‘default’ is any iterable other than a single string, its elements are similarly returned joined by commas.

HTTPResponse.getheaders()

Return a list of (header, value) tuples.

HTTPResponse.fileno()

Return the fileno of the underlying socket.

HTTPResponse.msg

A http.client.HTTPMessage instance containing the response headers. http.client.HTTPMessage is a subclass of email.message.Message.

HTTPResponse.version

HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.

HTTPResponse.status

由服务器返回的状态码。

HTTPResponse.reason

Reason phrase returned by server.

HTTPResponse.debuglevel

A debugging hook. If debuglevel is greater than zero, messages will be printed to stdout as the response is read and parsed.

HTTPResponse.closed

Is True if the stream is closed.

21.12.3. 例子

Here is an example session that uses the GET method:

>>> import http.client
>>> conn = http.client.HTTPSConnection("www.python.org")
>>> conn.request("GET", "/")
>>> r1 = conn.getresponse()
>>> print(r1.status, r1.reason)
200 OK
>>> data1 = r1.read()  # This will return entire content.
>>> # The following example demonstrates reading data in chunks.
>>> conn.request("GET", "/")
>>> r1 = conn.getresponse()
>>> while not r1.closed:
...     print(r1.read(200))  # 200 bytes
b'<!doctype html>\n<!--[if"...
...
>>> # Example of an invalid request
>>> conn.request("GET", "/parrot.spam")
>>> r2 = conn.getresponse()
>>> print(r2.status, r2.reason)
404 Not Found
>>> data2 = r2.read()
>>> conn.close()

Here is an example session that uses the HEAD method. Note that the HEAD method never returns any data.

>>> import http.client
>>> conn = http.client.HTTPSConnection("www.python.org")
>>> conn.request("HEAD", "/")
>>> res = conn.getresponse()
>>> print(res.status, res.reason)
200 OK
>>> data = res.read()
>>> print(len(data))
0
>>> data == b''
True

Here is an example session that shows how to POST requests:

>>> import http.client, urllib.parse
>>> params = urllib.parse.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
>>> headers = {"Content-type": "application/x-www-form-urlencoded",
...            "Accept": "text/plain"}
>>> conn = http.client.HTTPConnection("bugs.python.org")
>>> conn.request("POST", "", params, headers)
>>> response = conn.getresponse()
>>> print(response.status, response.reason)
302 Found
>>> data = response.read()
>>> data
b'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>'
>>> conn.close()

Client side HTTP PUT requests are very similar to POST requests. The difference lies only the server side where HTTP server will allow resources to be created via PUT request. It should be noted that custom HTTP methods +are also handled in urllib.request.Request by sending the appropriate +method attribute.Here is an example session that shows how to do PUT request using http.client:

>>> # This creates an HTTP message
>>> # with the content of BODY as the enclosed representation
>>> # for the resource http://localhost:8080/file
...
>>> import http.client
>>> BODY = "***filecontents***"
>>> conn = http.client.HTTPConnection("localhost", 8080)
>>> conn.request("PUT", "/file", BODY)
>>> response = conn.getresponse()
>>> print(response.status, response.reason)
200, OK

21.12.4. HTTPMessage Objects

An http.client.HTTPMessage instance holds the headers from an HTTP response. It is implemented using the email.message.Message class.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.13. ftplib — FTP 协议客户端

源代码： Lib/ftplib.py [https://github.com/python/cpython/tree/3.6/Lib/ftplib.py]

本模块定义了 FTP 类和一些相关项目。FTP 类实现了 FTP 协议的客户端。可以用该类编写 Python 程序，执行各种自动化的 FTP 任务，如镜像其他 FTP 服务器。urllib.request 模块也用它来处理使用了 FTP 的 URL。关于 FTP（文件传输协议）的详情请参阅 Internet RFC 959 [https://tools.ietf.org/html/rfc959.html]。

以下是使用 ftplib 模块的会话示例:

>>> from ftplib import FTP
>>> ftp = FTP('ftp.debian.org')     # connect to host, default port
>>> ftp.login()                     # user anonymous, passwd anonymous@
'230 Login successful.'
>>> ftp.cwd('debian')               # change into "debian" directory
>>> ftp.retrlines('LIST')           # list directory contents
-rw-rw-r--    1 1176     1176         1063 Jun 15 10:18 README
...
drwxr-sr-x    5 1176     1176         4096 Dec 19  2000 pool
drwxr-sr-x    4 1176     1176         4096 Nov 17  2008 project
drwxr-xr-x    3 1176     1176         4096 Oct 10  2012 tools
'226 Directory send OK.'
>>> ftp.retrbinary('RETR README', open('README', 'wb').write)
'226 Transfer complete.'
>>> ftp.quit()

这个模块定义了以下内容：

class ftplib.FTP(host='', user='', passwd='', acct='', timeout=None, source_address=None)

Return a new instance of the FTP class. When host is given, the method call connect(host) is made. When user is given, additionally the method call login(user, passwd, acct) is made (where passwd and acct default to the empty string when not given). The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if is not specified, the global default timeout setting will be used). source_address is a 2-tuple (host, port) for the socket to bind to as its source address before connecting.

FTP 类支持 with 语句，例如：

>>> from ftplib import FTP
>>> with FTP("ftp1.at.proftpd.org") as ftp:
...     ftp.login()
...     ftp.dir()
...
'230 Anonymous login ok, restrictions apply.'
dr-xr-xr-x   9 ftp      ftp           154 May  6 10:43 .
dr-xr-xr-x   9 ftp      ftp           154 May  6 10:43 ..
dr-xr-xr-x   5 ftp      ftp          4096 May  6 10:43 CentOS
dr-xr-xr-x   3 ftp      ftp            18 Jul 10  2008 Fedora
>>>

在 3.2 版更改: 支持了 with 语句。

在 3.3 版更改: 添加了 source_address 参数。

class ftplib.FTP_TLS(host='', user='', passwd='', acct='', keyfile=None, certfile=None, context=None, timeout=None, source_address=None)

一个 FTP 的子类，它为 FTP 添加了 TLS 支持，如 RFC 4217 [https://tools.ietf.org/html/rfc4217.html] 所述。它将像通常一样连接到 21 端口，暗中保护在身份验证前的 FTP 控制连接。而保护数据连接需要用户明确调用 prot_p() 方法。context 是一个 ssl.SSLContext 对象，该对象可以将 SSL 配置选项、证书和私钥打包放入一个单独的（可以长久存在的）结构中。请阅读 安全考量 以获取最佳实践。

keyfile 和 certfile 是可以代替 context 的传统方案，它们可以分别指向 PEM 格式的私钥和证书链文件，用于进行 SSL 连接。

3.2 新版功能.

在 3.3 版更改: 添加了 source_address 参数。

在 3.4 版更改: 本类现在支持通过 ssl.SSLContext.check_hostname 和 服务器名称提示 （参阅 ssl.HAS_SNI）进行主机名检查。

3.6 版后已移除: keyfile 和 certfile 已弃用并转而推荐 context。 请改用 ssl.SSLContext.load_cert_chain() 或让 ssl.create_default_context() 为你选择系统所信任的 CA 证书。

以下是使用 FTP_TLS 类的会话示例:

>>> ftps = FTP_TLS('ftp.pureftpd.org')
>>> ftps.login()
'230 Anonymous user logged in'
>>> ftps.prot_p()
'200 Data protection level set to "private"'
>>> ftps.nlst()
['6jack', 'OpenBSD', 'antilink', 'blogbench', 'bsdcam', 'clockspeed', 'djbdns-jedi', 'docs', 'eaccelerator-jedi', 'favicon.ico', 'francotone', 'fugu', 'ignore', 'libpuzzle', 'metalog', 'minidentd', 'misc', 'mysql-udf-global-user-variables', 'php-jenkins-hash', 'php-skein-hash', 'php-webdav', 'phpaudit', 'phpbench', 'pincaster', 'ping', 'posto', 'pub', 'public', 'public_keys', 'pure-ftpd', 'qscan', 'qtc', 'sharedance', 'skycache', 'sound', 'tmp', 'ucarp']

exception ftplib.error_reply

从服务器收到意外答复时，将引发本异常。

exception ftplib.error_temp

收到表示临时错误的错误代码（响应代码在 400–499 范围内）时，将引发本异常。

exception ftplib.error_perm

收到表示永久性错误的错误代码（响应代码在 500–599 范围内）时，将引发本异常。

exception ftplib.error_proto

从服务器收到不符合 FTP 响应规范的答复，比如以数字 1–5 开头时，将引发本异常。

ftplib.all_errors

The set of all exceptions (as a tuple) that methods of FTP instances may raise as a result of problems with the FTP connection (as opposed to programming errors made by the caller). This set includes the four exceptions listed above as well as OSError.

参见

netrc 模块

.netrc 文件格式解析器。FTP 客户端在响应用户之前，通常使用 .netrc 文件来加载用户认证信息。

21.13.1. FTP 对象

一些方法可以按照两种方式来使用：一种处理文本文件，另一种处理二进制文件。方法名称与相应的命令相同，文本版中命令后面跟着 lines，二进制版中命令后面跟着 binary。

FTP 实例具有下列方法:

FTP.set_debuglevel(level)

设置实例的调试级别，它控制着调试信息的数量。默认值 0 不产生调试信息。值 1 产生中等数量的调试信息，通常每个请求产生一行。大于或等于 2 的值产生的调试信息最多，FTP 控制连接上发送和接收的每一行都将被记录下来。

FTP.connect(host='', port=0, timeout=None, source_address=None)

连接到给定的主机和端口。默认端口号由 FTP 协议规范规定，为 21。偶尔才需要指定其他端口号。每个实例只应调用一次本函数，如果在创建实例时就传入了 host，则根本不应调用它。所有其他方法只能在建立连接后使用。可选参数 timeout 指定连接尝试的超时（以秒为单位）。如果没有传入 timeout，将使用全局默认超时设置。source_address 是一个 2 元组 (host, port)，套接字在连接前绑定它，作为其源地址。

在 3.3 版更改: 添加了 source_address 参数。

FTP.getwelcome()

返回服务器发送的欢迎消息，作为连接开始的回复。（该消息有时包含与用户有关的免责声明或帮助信息。）

FTP.login(user='anonymous', passwd='', acct='')

以 user 的身份登录。passwd 和 acct 是可选参数，默认为空字符串。如果没有指定 user，则默认为 'anonymous'。如果 user 为 'anonymous'，那么默认的 passwd 是 'anonymous@'。连接建立后，每个实例只应调用一次本函数；如果在创建实例时传入了 host 和 user，则完全不应该调用本函数。在客户端登录后，才允许执行大多数 FTP 命令。acct 参数提供记账信息 (“accounting information”)；仅少数系统实现了该特性。

FTP.abort()

中止正在进行的文件传输。本方法并不总是有效，但值得一试。

FTP.sendcmd(cmd)

将一条简单的命令字符串发送到服务器，返回响应的字符串。

FTP.voidcmd(cmd)

将一条简单的命令字符串发送到服务器，并处理响应内容。如果收到的响应代码表示的是成功（代码范围 200–299），则不返回任何内容。否则将引发 error_reply。

FTP.retrbinary(cmd, callback, blocksize=8192, rest=None)

以二进制传输模式下载文件。cmd 应为恰当的 RETR 命令：'RETR 文件名'。callback 函数会在收到每个数据块时调用，传入的参数是表示数据块的一个字节。为执行实际传输，创建了底层套接字对象，可选参数 blocksize 指定了读取该对象时的最大块大小（这也是传入 callback 的数据块的最大大小）。已经选择了合理的默认值。rest 的含义与 transfercmd() 方法中的含义相同。

FTP.retrlines(cmd, callback=None)

Retrieve a file or directory listing in ASCII transfer mode. cmd should be an appropriate RETR command (see retrbinary()) or a command such as LIST or NLST (usually just the string 'LIST'). LIST retrieves a list of files and information about those files. NLST retrieves a list of file names. The callback function is called for each line with a string argument containing the line with the trailing CRLF stripped. The default callback prints the line to sys.stdout.

FTP.set_pasv(val)

如果 val 为 true，则打开“被动”模式，否则禁用被动模式。默认下被动模式是打开的。

FTP.storbinary(cmd, fp, blocksize=8192, callback=None, rest=None)

以二进制传输模式存储文件。 cmd 应为恰当的 STOR 命令: "STOR filename"。fp 是一个 文件对象 (以二进制模式打开)，将使用它的 read() 方法读取它，用于提供要存储的数据，直到遇到 EOF，读取时的块大小为 blocksize。 参数 blocksize 的默认值为 8192。 可选参数 callback 是单参数函数，在每个数据块发送后都会用该数据块调用它。rest 的含义与 transfercmd() 方法中的含义相同。

在 3.2 版更改: 添加了 rest 参数。

FTP.storlines(cmd, fp, callback=None)

Store a file in ASCII transfer mode. cmd should be an appropriate STOR command (see storbinary()). Lines are read until EOF from the file object fp (opened in binary mode) using its readline() method to provide the data to be stored. callback is an optional single parameter callable that is called on each line after it is sent.

FTP.transfercmd(cmd, rest=None)

在 FTP 数据连接上开始传输数据。如果传输处于活动状态，传输命令由 cmd 指定，需发送 EPRT 或 PORT 命令，然后接受连接 (accept)。如果服务器是被动服务器，需发送 EPSV 或 PASV 命令，连接到服务器 (connect)，然后启动传输命令。两种方式都将返回用于连接的套接字。

If optional rest is given, a REST command is sent to the server, passing rest as an argument. rest is usually a byte offset into the requested file, telling the server to restart sending the file’s bytes at the requested offset, skipping over the initial bytes. Note however that RFC 959 [https://tools.ietf.org/html/rfc959.html] requires only that rest be a string containing characters in the printable range from ASCII code 33 to ASCII code 126. The transfercmd() method, therefore, converts rest to a string, but no check is performed on the string’s contents. If the server does not recognize the REST command, an error_reply exception will be raised. If this happens, simply call transfercmd() without a rest argument.

FTP.ntransfercmd(cmd, rest=None)

类似于 transfercmd()，但返回一个元组，包括数据连接和数据的预计大小。如果预计大小无法计算，则返回的预计大小为 None。cmd 和 rest 的含义与 transfercmd() 中的相同。

FTP.mlsd(path="", facts=[])

使用 MLSD 命令以标准格式列出目录内容 (RFC 3659 [https://tools.ietf.org/html/rfc3659.html])。如果省略 path 则使用当前目录。facts 是字符串列表，表示所需的信息类型（如 ["type", "size", "perm"]）。返回一个生成器对象，每个在 path 中找到的文件都将在该对象中生成两个元素的元组。第一个元素是文件名，第二个元素是该文件的 facts 的字典。该字典的内容受 facts 参数限制，但不能保证服务器会返回所有请求的 facts。

3.3 新版功能.

FTP.nlst(argument[, ...])

返回一个文件名列表，文件名由 NLST 命令返回。可选参数 argument 是待列出的目录（默认为当前服务器目录）。可以使用多个参数，将非标准选项传递给 NLST 命令。

注解

如果目标服务器支持相关命令，那么 mlsd() 提供的 API 更好。

FTP.dir(argument[, ...])

生成目录列表，即 LIST 命令所返回的结果，并将其打印到标准输出。可选参数 argument 是待列出的目录（默认为当前服务器目录）。可以使用多个参数，将非标准选项传递给 LIST 命令。如果最后一个参数是一个函数，它将被用作 callback 函数，与 retrlines() 中的相同，默认将打印到 sys.stdout。本方法返回 None。

注解

如果目标服务器支持相关命令，那么 mlsd() 提供的 API 更好。

FTP.rename(fromname, toname)

将服务器上的文件 fromname 重命名为 toname。

FTP.delete(filename)

将服务器上名为 filename 的文件删除。如果删除成功，返回响应文本，如果删除失败，在权限错误时引发 error_perm，在其他错误时引发 error_reply。

FTP.cwd(pathname)

设置服务器端的当前目录。

FTP.mkd(pathname)

在服务器上创建一个新目录。

FTP.pwd()

返回服务器上当前目录的路径。

FTP.rmd(dirname)

将服务器上名为 dirname 的目录删除。

FTP.size(filename)

请求服务器上名为 filename 的文件大小。成功后以整数返回文件大小，未成功则返回 None。注意，SIZE 不是标准命令，但通常许多服务器的实现都支持该命令。

FTP.quit()

向服务器发送 QUIT 命令并关闭连接。 这是关闭一个连接的“礼貌”方式，但是如果服务器对 QUIT 命令的响应带有错误消息则这会引发一个异常。 这意味着对 close() 方法的调用，它将使得 FTP 实例对后继调用无效（见下文）。

FTP.close()

单方面关闭连接。 这不该被应用于已经关闭的连接，例如成功调用 quit() 之后的连接。 在此调用之后 FTP 实例不应被继续使用（在调用 close() 或 quit() 之后你不能通过再次发起调用 login() 方法重新打开连接）。

21.13.2. FTP_TLS 对象

FTP_TLS 类继承自 FTP，它定义了下述其他对象：

FTP_TLS.ssl_version

欲采用的 SSL 版本（默认为 ssl.PROTOCOL_SSLv23）。

FTP_TLS.auth()

通过使用 TLS 或 SSL 来设置一个安全控制连接，具体取决于 ssl_version 属性是如何设置的。

在 3.4 版更改: 此方法现在支持使用 ssl.SSLContext.check_hostname 和 服务器名称指示 (参见 ssl.HAS_SNI) 进行主机名检查。

FTP_TLS.ccc()

将控制通道回复为纯文本。 这适用于发挥知道如何使用非安全 FTP 处理 NAT 而无需打开固定端口的防火墙的优势。

3.3 新版功能.

FTP_TLS.prot_p()

设置加密数据连接。

FTP_TLS.prot_c()

设置明文数据连接。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.14. poplib — POP3 协议客户端

源代码： Lib/poplib.py [https://github.com/python/cpython/tree/3.6/Lib/poplib.py]

本模块定义了一个 POP3 类，该类封装了到 POP3 服务器的连接过程，并实现了 RFC 1939 [https://tools.ietf.org/html/rfc1939.html] 中定义的协议。POP3 类同时支持 RFC 1939 [https://tools.ietf.org/html/rfc1939.html] 中最小的和可选的命令集。POP3 类还支持在 RFC 2595 [https://tools.ietf.org/html/rfc2595.html] 中引入的 STLS 命令，用于在已建立的连接上启用加密通信。

本模块额外提供一个 POP3_SSL 类，在连接到 POP3 服务器时，该类为使用 SSL 作为底层协议层提供了支持。

注意，尽管 POP3 具有广泛的支持，但它已经过时。POP3 服务器的实现质量差异很大，而且大多很糟糕。如果邮件服务器支持 IMAP，则最好使用 imaplib.IMAP4 类，因为 IMAP 服务器一般实现得更好。

poplib 模块提供了两个类：

class poplib.POP3(host, port=POP3_PORT[, timeout])

本类实现实际的 POP3 协议。实例初始化时，连接就会建立。如果省略 port，则使用标准 POP3 端口（110）。可选参数 timeout 指定连接尝试的超时时间（以秒为单位，如果未指定超时，将使用全局默认超时设置）。

class poplib.POP3_SSL(host, port=POP3_SSL_PORT, keyfile=None, certfile=None, timeout=None, context=None)

一个 POP3 的子类，它使用经 SSL 加密的套接字连接到服务器。如果端口 port 未指定，则使用 995，它是标准的 POP3-over-SSL 端口。timeout 的作用与 POP3 构造函数中的相同。context 是一个可选的 ssl.SSLContext 对象，该对象可以将 SSL 配置选项、证书和私钥打包放入一个单独的（可以长久存在的）结构中。请阅读 安全考量 以获取最佳实践。

keyfile 和 certfile 是可以代替 context 的传统方案，它们可以分别指向 PEM 格式的私钥和证书链文件，用于进行 SSL 连接。

在 3.2 版更改: 添加了 context 参数。

在 3.4 版更改: 本类现在支持通过 ssl.SSLContext.check_hostname 和 服务器名称提示 （参阅 ssl.HAS_SNI）进行主机名检查。

3.6 版后已移除: keyfile 和 certfile 已弃用并转而推荐 context。 请改用 ssl.SSLContext.load_cert_chain() 或让 ssl.create_default_context() 为你选择系统所信任的 CA 证书。

定义了一个异常，它是作为 poplib 模块的属性定义的：

exception poplib.error_proto

此模块的所有错误都将引发本异常（来自 socket 模块的错误不会被捕获）。异常的原因将以字符串的形式传递给构造函数。

参见

imaplib 模块

标准的 Python IMAP 模块。

有关 Fetchmail 的常见问题 [http://www.catb.org/~esr/fetchmail/fetchmail-FAQ.html]

fetchmail POP/IMAP 客户端的“常见问题”收集了 POP3 服务器之间的差异和 RFC 不兼容的信息，如果要编写基于 POP 协议的应用程序，这可能会很有用。

21.14.1. POP3 对象

所有 POP3 命令均以同名的方法表示，小写，大多数方法返回的是服务器发送的响应文本。

POP3 实例具有下列方法：

POP3.set_debuglevel(level)

设置实例的调试级别，它控制着调试信息的数量。默认值 0 不产生调试信息。值 1 产生中等数量的调试信息，通常每个请求产生一行。大于或等于 2 的值产生的调试信息最多，FTP 控制连接上发送和接收的每一行都将被记录下来。

POP3.getwelcome()

返回 POP3 服务器发送的问候语字符串。

POP3.capa()

查询服务器支持的功能，这些功能在 RFC 2449 [https://tools.ietf.org/html/rfc2449.html] 中有说明。返回一个 {'name': ['param'...]} 形式的字典。

3.4 新版功能.

POP3.user(username)

发送 user 命令，返回的响应应该指示需要一个密码。

POP3.pass_(password)

发送密码，响应包括邮件数和邮箱大小。注意：在调用 quit() 前，服务器上的邮箱都是锁定的。

POP3.apop(user, secret)

使用更安全的 APOP 身份验证登录到 POP3 服务器。

POP3.rpop(user)

使用 RPOP 身份验证（类似于 Unix r-命令）登录到 POP3 服务器。

POP3.stat()

获取邮箱状态。结果为 2 个整数组成的元组：(message count, mailbox size)。

POP3.list([which])

请求消息列表，结果的形式为 (response, ['mesg_num octets', ...], octets)。如果设置了 which，它表示需要列出的消息。

POP3.retr(which)

检索编号为 which 的整条消息，并设置其已读标志位。结果的形式为 (response, ['line', ...], octets)。

POP3.dele(which)

将编号为 which 的消息标记为待删除。在多数服务器上，删除操作直到 QUIT 才会实际执行（主要例外是 Eudora QPOP，它在断开连接时执行删除，故意违反了 RFC）。

POP3.rset()

移除邮箱中的所有待删除标记。

POP3.noop()

什么都不做。可以用于保持活动状态。

POP3.quit()

登出：提交更改，解除邮箱锁定，断开连接。

POP3.top(which, howmuch)

检索编号为 which 的消息，范围是消息头加上消息头往后数 howmuch 行。结果的形式为 (response, ['line', ...], octets)。

本方法使用 POP3 TOP 命令，不同于 RETR 命令，它不设置邮件的已读标志位。不幸的是，TOP 在 RFC 中说明不清晰，且在小众的服务器软件中往往不可用。信任并使用它之前，请先手动对目标 POP3 服务器测试本方法。

POP3.uidl(which=None)

返回消息摘要（唯一 ID）列表。如果指定了 which，那么结果将包含那条消息的唯一 ID，形式为 'response mesgnum uid，如果未指定，那么结果为列表 (response, ['mesgnum uid', ...], octets)。

POP3.utf8()

尝试切换至 UTF-8 模式。成功则返回服务器的响应，失败则引发 error_proto 异常。在 RFC 6856 [https://tools.ietf.org/html/rfc6856.html] 中有说明。

3.5 新版功能.

POP3.stls(context=None)

在活动连接上开启 TLS 会话，在 RFC 2595 [https://tools.ietf.org/html/rfc2595.html] 中有说明。仅在用户身份验证前允许这样做。

context 参数是一个 ssl.SSLContext 对象，该对象可以将 SSL 配置选项、证书和私钥打包放入一个单独的（可以长久存在的）结构中。请阅读 安全考量 以获取最佳实践。

此方法支持通过 ssl.SSLContext.check_hostname 和 服务器名称指示 (参见 ssl.HAS_SNI) 进行主机名检查。

3.4 新版功能.

POP3_SSL 实例没有额外方法。该子类的接口与其父类的相同。

21.14.2. POP3 示例

以下是一个最短示例（不带错误检查），该示例将打开邮箱，检索并打印所有消息:

import getpass, poplib

M = poplib.POP3('localhost')
M.user(getpass.getuser())
M.pass_(getpass.getpass())
numMessages = len(M.list()[1])
for i in range(numMessages):
    for j in M.retr(i+1)[1]:
        print(j)

模块的最后有一段测试，其中包含的用法示例更加广泛。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.15. imaplib — IMAP4 协议客户端

源代码： Lib/imaplib.py [https://github.com/python/cpython/tree/3.6/Lib/imaplib.py]

This module defines three classes, IMAP4, IMAP4_SSL and IMAP4_stream, which encapsulate a connection to an IMAP4 server and implement a large subset of the IMAP4rev1 client protocol as defined in RFC 2060 [https://tools.ietf.org/html/rfc2060.html]. It is backward compatible with IMAP4 (RFC 1730 [https://tools.ietf.org/html/rfc1730.html]) servers, but note that the STATUS command is not supported in IMAP4.

Three classes are provided by the imaplib module, IMAP4 is the base class:

class imaplib.IMAP4(host='', port=IMAP4_PORT)

This class implements the actual IMAP4 protocol. The connection is created and protocol version (IMAP4 or IMAP4rev1) is determined when the instance is initialized. If host is not specified, '' (the local host) is used. If port is omitted, the standard IMAP4 port (143) is used.

The IMAP4 class supports the with statement. When used like this, the IMAP4 LOGOUT command is issued automatically when the with statement exits. E.g.:

>>> from imaplib import IMAP4
>>> with IMAP4("domain.org") as M:
...     M.noop()
...
('OK', [b'Nothing Accomplished. d25if65hy903weo.87'])

在 3.5 版更改: 支持了 with 语句。

Three exceptions are defined as attributes of the IMAP4 class:

exception IMAP4.error

Exception raised on any errors. The reason for the exception is passed to the constructor as a string.

exception IMAP4.abort

IMAP4 server errors cause this exception to be raised. This is a sub-class of IMAP4.error. Note that closing the instance and instantiating a new one will usually allow recovery from this exception.

exception IMAP4.readonly

This exception is raised when a writable mailbox has its status changed by the server. This is a sub-class of IMAP4.error. Some other client now has write permission, and the mailbox will need to be re-opened to re-obtain write permission.

There’s also a subclass for secure connections:

class imaplib.IMAP4_SSL(host='', port=IMAP4_SSL_PORT, keyfile=None, certfile=None, ssl_context=None)

This is a subclass derived from IMAP4 that connects over an SSL encrypted socket (to use this class you need a socket module that was compiled with SSL support). If host is not specified, '' (the local host) is used. If port is omitted, the standard IMAP4-over-SSL port (993) is used. ssl_context is a ssl.SSLContext object which allows bundling SSL configuration options, certificates and private keys into a single (potentially long-lived) structure. Please read 安全考量 for best practices.

keyfile and certfile are a legacy alternative to ssl_context - they can point to PEM-formatted private key and certificate chain files for the SSL connection. Note that the keyfile/certfile parameters are mutually exclusive with ssl_context, a ValueError is raised if keyfile/certfile is provided along with ssl_context.

在 3.3 版更改: ssl_context parameter added.

在 3.4 版更改: 本类现在支持通过 ssl.SSLContext.check_hostname 和 服务器名称提示 （参阅 ssl.HAS_SNI）进行主机名检查。

3.6 版后已移除: keyfile and certfile are deprecated in favor of ssl_context. Please use ssl.SSLContext.load_cert_chain() instead, or let ssl.create_default_context() select the system’s trusted CA certificates for you.

The second subclass allows for connections created by a child process:

class imaplib.IMAP4_stream(command)

This is a subclass derived from IMAP4 that connects to the stdin/stdout file descriptors created by passing command to subprocess.Popen().

The following utility functions are defined:

imaplib.Internaldate2tuple(datestr)

Parse an IMAP4 INTERNALDATE string and return corresponding local time. The return value is a time.struct_time tuple or None if the string has wrong format.

imaplib.Int2AP(num)

Converts an integer into a string representation using characters from the set [A .. P].

imaplib.ParseFlags(flagstr)

Converts an IMAP4 FLAGS response to a tuple of individual flags.

imaplib.Time2Internaldate(date_time)

Convert date_time to an IMAP4 INTERNALDATE representation. The return value is a string in the form: "DD-Mmm-YYYY HH:MM:SS +HHMM" (including double-quotes). The date_time argument can be a number (int or float) representing seconds since epoch (as returned by time.time()), a 9-tuple representing local time an instance of time.struct_time (as returned by time.localtime()), an aware instance of datetime.datetime, or a double-quoted string. In the last case, it is assumed to already be in the correct format.

Note that IMAP4 message numbers change as the mailbox changes; in particular, after an EXPUNGE command performs deletions the remaining messages are renumbered. So it is highly advisable to use UIDs instead, with the UID command.

模块的最后有一段测试，其中包含的用法示例更加广泛。

参见

Documents describing the protocol, and sources and binaries for servers implementing it, can all be found at the University of Washington’s IMAP Information Center (https://www.washington.edu/imap/).

21.15.1. IMAP4 Objects

All IMAP4rev1 commands are represented by methods of the same name, either upper-case or lower-case.

All arguments to commands are converted to strings, except for AUTHENTICATE, and the last argument to APPEND which is passed as an IMAP4 literal. If necessary (the string contains IMAP4 protocol-sensitive characters and isn’t enclosed with either parentheses or double quotes) each string is quoted. However, the password argument to the LOGIN command is always quoted. If you want to avoid having an argument string quoted (eg: the flags argument to STORE) then enclose the string in parentheses (eg: r'(\Deleted)').

Each command returns a tuple: (type, [data, ...]) where type is usually 'OK' or 'NO', and data is either the text from the command response, or mandated results from the command. Each data is either a string, or a tuple. If a tuple, then the first part is the header of the response, and the second part contains the data (ie: ‘literal’ value).

The message_set options to commands below is a string specifying one or more messages to be acted upon. It may be a simple message number ('1'), a range of message numbers ('2:4'), or a group of non-contiguous ranges separated by commas ('1:3,6:9'). A range can contain an asterisk to indicate an infinite upper bound ('3:*').

An IMAP4 instance has the following methods:

IMAP4.append(mailbox, flags, date_time, message)

Append message to named mailbox.

IMAP4.authenticate(mechanism, authobject)

Authenticate command — requires response processing.

mechanism specifies which authentication mechanism is to be used - it should appear in the instance variable capabilities in the form AUTH=mechanism.

authobject must be a callable object:

data = authobject(response)

It will be called to process server continuation responses; the response argument it is passed will be bytes. It should return bytes data that will be base64 encoded and sent to the server. It should return None if the client abort response * should be sent instead.

在 3.5 版更改: string usernames and passwords are now encoded to utf-8 instead of being limited to ASCII.

IMAP4.check()

Checkpoint mailbox on server.

IMAP4.close()

Close currently selected mailbox. Deleted messages are removed from writable mailbox. This is the recommended command before LOGOUT.

IMAP4.copy(message_set, new_mailbox)

Copy message_set messages onto end of new_mailbox.

IMAP4.create(mailbox)

Create new mailbox named mailbox.

IMAP4.delete(mailbox)

Delete old mailbox named mailbox.

IMAP4.deleteacl(mailbox, who)

Delete the ACLs (remove any rights) set for who on mailbox.

IMAP4.enable(capability)

Enable capability (see RFC 5161 [https://tools.ietf.org/html/rfc5161.html]). Most capabilities do not need to be enabled. Currently only the UTF8=ACCEPT capability is supported (see RFC 6855 [https://tools.ietf.org/html/rfc6855.html]).

3.5 新版功能: The enable() method itself, and RFC 6855 [https://tools.ietf.org/html/rfc6855.html] support.

IMAP4.expunge()

Permanently remove deleted items from selected mailbox. Generates an EXPUNGE response for each deleted message. Returned data contains a list of EXPUNGE message numbers in order received.

IMAP4.fetch(message_set, message_parts)

Fetch (parts of) messages. message_parts should be a string of message part names enclosed within parentheses, eg: "(UID BODY[TEXT])". Returned data are tuples of message part envelope and data.

IMAP4.getacl(mailbox)

Get the ACLs for mailbox. The method is non-standard, but is supported by the Cyrus server.

IMAP4.getannotation(mailbox, entry, attribute)

Retrieve the specified ANNOTATIONs for mailbox. The method is non-standard, but is supported by the Cyrus server.

IMAP4.getquota(root)

Get the quota root’s resource usage and limits. This method is part of the IMAP4 QUOTA extension defined in rfc2087.

IMAP4.getquotaroot(mailbox)

Get the list of quota roots for the named mailbox. This method is part of the IMAP4 QUOTA extension defined in rfc2087.

IMAP4.list([directory[, pattern]])

List mailbox names in directory matching pattern. directory defaults to the top-level mail folder, and pattern defaults to match anything. Returned data contains a list of LIST responses.

IMAP4.login(user, password)

Identify the client using a plaintext password. The password will be quoted.

IMAP4.login_cram_md5(user, password)

Force use of CRAM-MD5 authentication when identifying the client to protect the password. Will only work if the server CAPABILITY response includes the phrase AUTH=CRAM-MD5.

IMAP4.logout()

Shutdown connection to server. Returns server BYE response.

IMAP4.lsub(directory='""', pattern='*')

List subscribed mailbox names in directory matching pattern. directory defaults to the top level directory and pattern defaults to match any mailbox. Returned data are tuples of message part envelope and data.

IMAP4.myrights(mailbox)

Show my ACLs for a mailbox (i.e. the rights that I have on mailbox).

IMAP4.namespace()

Returns IMAP namespaces as defined in RFC 2342 [https://tools.ietf.org/html/rfc2342.html].

IMAP4.noop()

Send NOOP to server.

IMAP4.open(host, port)

Opens socket to port at host. This method is implicitly called by the IMAP4 constructor. The connection objects established by this method will be used in the IMAP4.read(), IMAP4.readline(), IMAP4.send(), and IMAP4.shutdown() methods. You may override this method.

IMAP4.partial(message_num, message_part, start, length)

Fetch truncated part of a message. Returned data is a tuple of message part envelope and data.

IMAP4.proxyauth(user)

Assume authentication as user. Allows an authorised administrator to proxy into any user’s mailbox.

IMAP4.read(size)

Reads size bytes from the remote server. You may override this method.

IMAP4.readline()

Reads one line from the remote server. You may override this method.

IMAP4.recent()

Prompt server for an update. Returned data is None if no new messages, else value of RECENT response.

IMAP4.rename(oldmailbox, newmailbox)

Rename mailbox named oldmailbox to newmailbox.

IMAP4.response(code)

Return data for response code if received, or None. Returns the given code, instead of the usual type.

IMAP4.search(charset, criterion[, ...])

Search mailbox for matching messages. charset may be None, in which case no CHARSET will be specified in the request to the server. The IMAP protocol requires that at least one criterion be specified; an exception will be raised when the server returns an error. charset must be None if the UTF8=ACCEPT capability was enabled using the enable() command.

示例:

# M is a connected IMAP4 instance...
typ, msgnums = M.search(None, 'FROM', '"LDJ"')

# or:
typ, msgnums = M.search(None, '(FROM "LDJ")')

IMAP4.select(mailbox='INBOX', readonly=False)

Select a mailbox. Returned data is the count of messages in mailbox (EXISTS response). The default mailbox is 'INBOX'. If the readonly flag is set, modifications to the mailbox are not allowed.

IMAP4.send(data)

Sends data to the remote server. You may override this method.

IMAP4.setacl(mailbox, who, what)

Set an ACL for mailbox. The method is non-standard, but is supported by the Cyrus server.

IMAP4.setannotation(mailbox, entry, attribute[, ...])

Set ANNOTATIONs for mailbox. The method is non-standard, but is supported by the Cyrus server.

IMAP4.setquota(root, limits)

Set the quota root’s resource limits. This method is part of the IMAP4 QUOTA extension defined in rfc2087.

IMAP4.shutdown()

Close connection established in open. This method is implicitly called by IMAP4.logout(). You may override this method.

IMAP4.socket()

Returns socket instance used to connect to server.

IMAP4.sort(sort_criteria, charset, search_criterion[, ...])

The sort command is a variant of search with sorting semantics for the results. Returned data contains a space separated list of matching message numbers.

Sort has two arguments before the search_criterion argument(s); a parenthesized list of sort_criteria, and the searching charset. Note that unlike search, the searching charset argument is mandatory. There is also a uid sort command which corresponds to sort the way that uid search corresponds to search. The sort command first searches the mailbox for messages that match the given searching criteria using the charset argument for the interpretation of strings in the searching criteria. It then returns the numbers of matching messages.

This is an IMAP4rev1 extension command.

IMAP4.starttls(ssl_context=None)

Send a STARTTLS command. The ssl_context argument is optional and should be a ssl.SSLContext object. This will enable encryption on the IMAP connection. Please read 安全考量 for best practices.

3.2 新版功能.

在 3.4 版更改: 此方法现在支持使用 ssl.SSLContext.check_hostname 和 服务器名称指示 (参见 ssl.HAS_SNI) 进行主机名检查。

IMAP4.status(mailbox, names)

Request named status conditions for mailbox.

IMAP4.store(message_set, command, flag_list)

Alters flag dispositions for messages in mailbox. command is specified by section 6.4.6 of RFC 2060 [https://tools.ietf.org/html/rfc2060.html] as being one of “FLAGS”, “+FLAGS”, or “-FLAGS”, optionally with a suffix of “.SILENT”.

For example, to set the delete flag on all messages:

typ, data = M.search(None, 'ALL')
for num in data[0].split():
   M.store(num, '+FLAGS', '\\Deleted')
M.expunge()

注解

Creating flags containing ‘]’ (for example: “[test]”) violates RFC 3501 [https://tools.ietf.org/html/rfc3501.html] (the IMAP protocol). However, imaplib has historically allowed creation of such tags, and popular IMAP servers, such as Gmail, accept and produce such flags. There are non-Python programs which also create such tags. Although it is an RFC violation and IMAP clients and servers are supposed to be strict, imaplib nonetheless continues to allow such tags to be created for backward compatibility reasons, and as of python 3.6, handles them if they are sent from the server, since this improves real-world compatibility.

IMAP4.subscribe(mailbox)

Subscribe to new mailbox.

IMAP4.thread(threading_algorithm, charset, search_criterion[, ...])

The thread command is a variant of search with threading semantics for the results. Returned data contains a space separated list of thread members.

Thread members consist of zero or more messages numbers, delimited by spaces, indicating successive parent and child.

Thread has two arguments before the search_criterion argument(s); a threading_algorithm, and the searching charset. Note that unlike search, the searching charset argument is mandatory. There is also a uid thread command which corresponds to thread the way that uid search corresponds to search. The thread command first searches the mailbox for messages that match the given searching criteria using the charset argument for the interpretation of strings in the searching criteria. It then returns the matching messages threaded according to the specified threading algorithm.

This is an IMAP4rev1 extension command.

IMAP4.uid(command, arg[, ...])

Execute command args with messages identified by UID, rather than message number. Returns response appropriate to command. At least one argument must be supplied; if none are provided, the server will return an error and an exception will be raised.

IMAP4.unsubscribe(mailbox)

Unsubscribe from old mailbox.

IMAP4.xatom(name[, ...])

Allow simple extension commands notified by server in CAPABILITY response.

The following attributes are defined on instances of IMAP4:

IMAP4.PROTOCOL_VERSION

The most recent supported protocol in the CAPABILITY response from the server.

IMAP4.debug

Integer value to control debugging output. The initialize value is taken from the module variable Debug. Values greater than three trace each command.

IMAP4.utf8_enabled

Boolean value that is normally False, but is set to True if an enable() command is successfully issued for the UTF8=ACCEPT capability.

3.5 新版功能.

21.15.2. IMAP4 Example

以下是一个最短示例（不带错误检查），该示例将打开邮箱，检索并打印所有消息:

import getpass, imaplib

M = imaplib.IMAP4()
M.login(getpass.getuser(), getpass.getpass())
M.select()
typ, data = M.search(None, 'ALL')
for num in data[0].split():
    typ, data = M.fetch(num, '(RFC822)')
    print('Message %s\n%s\n' % (num, data[0][1]))
M.close()
M.logout()

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.16. nntplib — NNTP protocol client

Source code: Lib/nntplib.py [https://github.com/python/cpython/tree/3.6/Lib/nntplib.py]

This module defines the class NNTP which implements the client side of the Network News Transfer Protocol. It can be used to implement a news reader or poster, or automated news processors. It is compatible with RFC 3977 [https://tools.ietf.org/html/rfc3977.html] as well as the older RFC 977 [https://tools.ietf.org/html/rfc977.html] and RFC 2980 [https://tools.ietf.org/html/rfc2980.html].

Here are two small examples of how it can be used. To list some statistics about a newsgroup and print the subjects of the last 10 articles:

>>> s = nntplib.NNTP('news.gmane.org')
>>> resp, count, first, last, name = s.group('gmane.comp.python.committers')
>>> print('Group', name, 'has', count, 'articles, range', first, 'to', last)
Group gmane.comp.python.committers has 1096 articles, range 1 to 1096
>>> resp, overviews = s.over((last - 9, last))
>>> for id, over in overviews:
...     print(id, nntplib.decode_header(over['subject']))
...
1087 Re: Commit privileges for Łukasz Langa
1088 Re: 3.2 alpha 2 freeze
1089 Re: 3.2 alpha 2 freeze
1090 Re: Commit privileges for Łukasz Langa
1091 Re: Commit privileges for Łukasz Langa
1092 Updated ssh key
1093 Re: Updated ssh key
1094 Re: Updated ssh key
1095 Hello fellow committers!
1096 Re: Hello fellow committers!
>>> s.quit()
'205 Bye!'

To post an article from a binary file (this assumes that the article has valid headers, and that you have right to post on the particular newsgroup):

>>> s = nntplib.NNTP('news.gmane.org')
>>> f = open('article.txt', 'rb')
>>> s.post(f)
'240 Article posted successfully.'
>>> s.quit()
'205 Bye!'

The module itself defines the following classes:

class nntplib.NNTP(host, port=119, user=None, password=None, readermode=None, usenetrc=False[, timeout])

Return a new NNTP object, representing a connection to the NNTP server running on host host, listening at port port. An optional timeout can be specified for the socket connection. If the optional user and password are provided, or if suitable credentials are present in /.netrc and the optional flag usenetrc is true, the AUTHINFO USER and AUTHINFO PASS commands are used to identify and authenticate the user to the server. If the optional flag readermode is true, then a mode reader command is sent before authentication is performed. Reader mode is sometimes necessary if you are connecting to an NNTP server on the local machine and intend to call reader-specific commands, such as group. If you get unexpected NNTPPermanentErrors, you might need to set readermode. The NNTP class supports the with statement to unconditionally consume OSError exceptions and to close the NNTP connection when done, e.g.:

>>> from nntplib import NNTP
>>> with NNTP('news.gmane.org') as n:
...     n.group('gmane.comp.python.committers')
...
('211 1755 1 1755 gmane.comp.python.committers', 1755, 1, 1755, 'gmane.comp.python.committers')
>>>

在 3.2 版更改: usenetrc is now False by default.

在 3.3 版更改: 支持了 with 语句。

class nntplib.NNTP_SSL(host, port=563, user=None, password=None, ssl_context=None, readermode=None, usenetrc=False[, timeout])

Return a new NNTP_SSL object, representing an encrypted connection to the NNTP server running on host host, listening at port port. NNTP_SSL objects have the same methods as NNTP objects. If port is omitted, port 563 (NNTPS) is used. ssl_context is also optional, and is a SSLContext object. Please read 安全考量 for best practices. All other parameters behave the same as for NNTP.

Note that SSL-on-563 is discouraged per RFC 4642 [https://tools.ietf.org/html/rfc4642.html], in favor of STARTTLS as described below. However, some servers only support the former.

3.2 新版功能.

在 3.4 版更改: 本类现在支持通过 ssl.SSLContext.check_hostname 和 服务器名称提示 （参阅 ssl.HAS_SNI）进行主机名检查。

exception nntplib.NNTPError

Derived from the standard exception Exception, this is the base class for all exceptions raised by the nntplib module. Instances of this class have the following attribute:

response

The response of the server if available, as a str object.

exception nntplib.NNTPReplyError

从服务器收到意外答复时，将引发本异常。

exception nntplib.NNTPTemporaryError

Exception raised when a response code in the range 400–499 is received.

exception nntplib.NNTPPermanentError

Exception raised when a response code in the range 500–599 is received.

exception nntplib.NNTPProtocolError

Exception raised when a reply is received from the server that does not begin with a digit in the range 1–5.

exception nntplib.NNTPDataError

Exception raised when there is some error in the response data.

21.16.1. NNTP Objects

When connected, NNTP and NNTP_SSL objects support the following methods and attributes.

21.16.1.1. Attributes

NNTP.nntp_version

An integer representing the version of the NNTP protocol supported by the server. In practice, this should be 2 for servers advertising RFC 3977 [https://tools.ietf.org/html/rfc3977.html] compliance and 1 for others.

3.2 新版功能.

NNTP.nntp_implementation

A string describing the software name and version of the NNTP server, or None if not advertised by the server.

3.2 新版功能.

21.16.1.2. 方法

The response that is returned as the first item in the return tuple of almost all methods is the server’s response: a string beginning with a three-digit code. If the server’s response indicates an error, the method raises one of the above exceptions.

Many of the following methods take an optional keyword-only argument file. When the file argument is supplied, it must be either a file object opened for binary writing, or the name of an on-disk file to be written to. The method will then write any data returned by the server (except for the response line and the terminating dot) to the file; any list of lines, tuples or objects that the method normally returns will be empty.

在 3.2 版更改: Many of the following methods have been reworked and fixed, which makes them incompatible with their 3.1 counterparts.

NNTP.quit()

Send a QUIT command and close the connection. Once this method has been called, no other methods of the NNTP object should be called.

NNTP.getwelcome()

返回服务器发送的欢迎消息，作为连接开始的回复。（该消息有时包含与用户有关的免责声明或帮助信息。）

NNTP.getcapabilities()

Return the RFC 3977 [https://tools.ietf.org/html/rfc3977.html] capabilities advertised by the server, as a dict instance mapping capability names to (possibly empty) lists of values. On legacy servers which don’t understand the CAPABILITIES command, an empty dictionary is returned instead.

>>> s = NNTP('news.gmane.org')
>>> 'POST' in s.getcapabilities()
True

3.2 新版功能.

NNTP.login(user=None, password=None, usenetrc=True)

Send AUTHINFO commands with the user name and password. If user and password are None and usenetrc is true, credentials from ~/.netrc will be used if possible.

Unless intentionally delayed, login is normally performed during the NNTP object initialization and separately calling this function is unnecessary. To force authentication to be delayed, you must not set user or password when creating the object, and must set usenetrc to False.

3.2 新版功能.

NNTP.starttls(context=None)

Send a STARTTLS command. This will enable encryption on the NNTP connection. The context argument is optional and should be a ssl.SSLContext object. Please read 安全考量 for best practices.

Note that this may not be done after authentication information has been transmitted, and authentication occurs by default if possible during a NNTP object initialization. See NNTP.login() for information on suppressing this behavior.

3.2 新版功能.

在 3.4 版更改: 此方法现在支持使用 ssl.SSLContext.check_hostname 和 服务器名称指示 (参见 ssl.HAS_SNI) 进行主机名检查。

NNTP.newgroups(date, *, file=None)

Send a NEWGROUPS command. The date argument should be a datetime.date or datetime.datetime object. Return a pair (response, groups) where groups is a list representing the groups that are new since the given date. If file is supplied, though, then groups will be empty.

>>> from datetime import date, timedelta
>>> resp, groups = s.newgroups(date.today() - timedelta(days=3))
>>> len(groups)
85
>>> groups[0]
GroupInfo(group='gmane.network.tor.devel', last='4', first='1', flag='m')

NNTP.newnews(group, date, *, file=None)

Send a NEWNEWS command. Here, group is a group name or '*', and date has the same meaning as for newgroups(). Return a pair (response, articles) where articles is a list of message ids.

This command is frequently disabled by NNTP server administrators.

NNTP.list(group_pattern=None, *, file=None)

Send a LIST or LIST ACTIVE command. Return a pair (response, list) where list is a list of tuples representing all the groups available from this NNTP server, optionally matching the pattern string group_pattern. Each tuple has the form (group, last, first, flag), where group is a group name, last and first are the last and first article numbers, and flag usually takes one of these values:

y: Local postings and articles from peers are allowed.

m: The group is moderated and all postings must be approved.

n: No local postings are allowed, only articles from peers.

j: Articles from peers are filed in the junk group instead.

x: No local postings, and articles from peers are ignored.

=foo.bar: Articles are filed in the foo.bar group instead.

If flag has another value, then the status of the newsgroup should be considered unknown.

This command can return very large results, especially if group_pattern is not specified. It is best to cache the results offline unless you really need to refresh them.

在 3.2 版更改: group_pattern was added.

NNTP.descriptions(grouppattern)

Send a LIST NEWSGROUPS command, where grouppattern is a wildmat string as specified in RFC 3977 [https://tools.ietf.org/html/rfc3977.html] (it’s essentially the same as DOS or UNIX shell wildcard strings). Return a pair (response, descriptions), where descriptions is a dictionary mapping group names to textual descriptions.

>>> resp, descs = s.descriptions('gmane.comp.python.*')
>>> len(descs)
295
>>> descs.popitem()
('gmane.comp.python.bio.general', 'BioPython discussion list (Moderated)')

NNTP.description(group)

Get a description for a single group group. If more than one group matches (if ‘group’ is a real wildmat string), return the first match. If no group matches, return an empty string.

This elides the response code from the server. If the response code is needed, use descriptions().

NNTP.group(name)

Send a GROUP command, where name is the group name. The group is selected as the current group, if it exists. Return a tuple (response, count, first, last, name) where count is the (estimated) number of articles in the group, first is the first article number in the group, last is the last article number in the group, and name is the group name.

NNTP.over(message_spec, *, file=None)

Send an OVER command, or an XOVER command on legacy servers. message_spec can be either a string representing a message id, or a (first, last) tuple of numbers indicating a range of articles in the current group, or a (first, None) tuple indicating a range of articles starting from first to the last article in the current group, or None to select the current article in the current group.

Return a pair (response, overviews). overviews is a list of (article_number, overview) tuples, one for each article selected by message_spec. Each overview is a dictionary with the same number of items, but this number depends on the server. These items are either message headers (the key is then the lower-cased header name) or metadata items (the key is then the metadata name prepended with ":"). The following items are guaranteed to be present by the NNTP specification:

the subject, from, date, message-id and references headers

the :bytes metadata: the number of bytes in the entire raw article (including headers and body)

the :lines metadata: the number of lines in the article body

The value of each item is either a string, or None if not present.

It is advisable to use the decode_header() function on header values when they may contain non-ASCII characters:

>>> _, _, first, last, _ = s.group('gmane.comp.python.devel')
>>> resp, overviews = s.over((last, last))
>>> art_num, over = overviews[0]
>>> art_num
117216
>>> list(over.keys())
['xref', 'from', ':lines', ':bytes', 'references', 'date', 'message-id', 'subject']
>>> over['from']
'=?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= <martin@v.loewis.de>'
>>> nntplib.decode_header(over['from'])
'"Martin v. Löwis" <martin@v.loewis.de>'

3.2 新版功能.

NNTP.help(*, file=None)

Send a HELP command. Return a pair (response, list) where list is a list of help strings.

NNTP.stat(message_spec=None)

Send a STAT command, where message_spec is either a message id (enclosed in '<' and '>') or an article number in the current group. If message_spec is omitted or None, the current article in the current group is considered. Return a triple (response, number, id) where number is the article number and id is the message id.

>>> _, _, first, last, _ = s.group('gmane.comp.python.devel')
>>> resp, number, message_id = s.stat(first)
>>> number, message_id
(9099, '<20030112190404.GE29873@epoch.metaslash.com>')

NNTP.next()

Send a NEXT command. Return as for stat().

NNTP.last()

Send a LAST command. Return as for stat().

NNTP.article(message_spec=None, *, file=None)

Send an ARTICLE command, where message_spec has the same meaning as for stat(). Return a tuple (response, info) where info is a namedtuple with three attributes number, message_id and lines (in that order). number is the article number in the group (or 0 if the information is not available), message_id the message id as a string, and lines a list of lines (without terminating newlines) comprising the raw message including headers and body.

>>> resp, info = s.article('<20030112190404.GE29873@epoch.metaslash.com>')
>>> info.number
0
>>> info.message_id
'<20030112190404.GE29873@epoch.metaslash.com>'
>>> len(info.lines)
65
>>> info.lines[0]
b'Path: main.gmane.org!not-for-mail'
>>> info.lines[1]
b'From: Neal Norwitz <neal@metaslash.com>'
>>> info.lines[-3:]
[b'There is a patch for 2.3 as well as 2.2.', b'', b'Neal']

NNTP.head(message_spec=None, *, file=None)

Same as article(), but sends a HEAD command. The lines returned (or written to file) will only contain the message headers, not the body.

NNTP.body(message_spec=None, *, file=None)

Same as article(), but sends a BODY command. The lines returned (or written to file) will only contain the message body, not the headers.

NNTP.post(data)

Post an article using the POST command. The data argument is either a file object opened for binary reading, or any iterable of bytes objects (representing raw lines of the article to be posted). It should represent a well-formed news article, including the required headers. The post() method automatically escapes lines beginning with . and appends the termination line.

If the method succeeds, the server’s response is returned. If the server refuses posting, a NNTPReplyError is raised.

NNTP.ihave(message_id, data)

Send an IHAVE command. message_id is the id of the message to send to the server (enclosed in '<' and '>'). The data parameter and the return value are the same as for post().

NNTP.date()

Return a pair (response, date). date is a datetime object containing the current date and time of the server.

NNTP.slave()

Send a SLAVE command. Return the server’s response.

NNTP.set_debuglevel(level)

Set the instance’s debugging level. This controls the amount of debugging output printed. The default, 0, produces no debugging output. A value of 1 produces a moderate amount of debugging output, generally a single line per request or response. A value of 2 or higher produces the maximum amount of debugging output, logging each line sent and received on the connection (including message text).

The following are optional NNTP extensions defined in RFC 2980 [https://tools.ietf.org/html/rfc2980.html]. Some of them have been superseded by newer commands in RFC 3977 [https://tools.ietf.org/html/rfc3977.html].

NNTP.xhdr(hdr, str, *, file=None)

Send an XHDR command. The hdr argument is a header keyword, e.g. 'subject'. The str argument should have the form 'first-last' where first and last are the first and last article numbers to search. Return a pair (response, list), where list is a list of pairs (id, text), where id is an article number (as a string) and text is the text of the requested header for that article. If the file parameter is supplied, then the output of the XHDR command is stored in a file. If file is a string, then the method will open a file with that name, write to it then close it. If file is a file object, then it will start calling write() on it to store the lines of the command output. If file is supplied, then the returned list is an empty list.

NNTP.xover(start, end, *, file=None)

Send an XOVER command. start and end are article numbers delimiting the range of articles to select. The return value is the same of for over(). It is recommended to use over() instead, since it will automatically use the newer OVER command if available.

NNTP.xpath(id)

Return a pair (resp, path), where path is the directory path to the article with message ID id. Most of the time, this extension is not enabled by NNTP server administrators.

3.3 版后已移除: The XPATH extension is not actively used.

21.16.2. 工具函数

The module also defines the following utility function:

nntplib.decode_header(header_str)

Decode a header value, un-escaping any escaped non-ASCII characters. header_str must be a str object. The unescaped value is returned. Using this function is recommended to display some headers in a human readable form:

>>> decode_header("Some subject")
'Some subject'
>>> decode_header("=?ISO-8859-15?Q?D=E9buter_en_Python?=")
'Débuter en Python'
>>> decode_header("Re: =?UTF-8?B?cHJvYmzDqG1lIGRlIG1hdHJpY2U=?=")
'Re: problème de matrice'

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.17. smtplib —SMTP协议客户端

源代码： Lib/smtplib.py [https://github.com/python/cpython/tree/3.6/Lib/smtplib.py]

smtplib 模块定义了一个 SMTP 客户端会话对象，该对象可将邮件发送到 Internet 上带有 SMTP 或 ESMTP 接收程序的计算机。 关于 SMTP 和 ESMTP 操作的详情请参阅 RFC 821 [https://tools.ietf.org/html/rfc821.html] (简单邮件传输协议) 和 RFC 1869 [https://tools.ietf.org/html/rfc1869.html] (SMTP 服务扩展)。

class smtplib.SMTP(host='', port=0, local_hostname=None, [timeout, ]source_address=None)

一个 SMTP 实例就是一个封装好的 SMTP 连接。该实例具有的方法支持所有 SMTP 和 ESMTP 操作。如果传入了可选参数 host 和 port，那么将在初始化时使用这些参数调用 SMTP connect() 方法。如果传入了 local_hostname，它将在 HELO/EHLO 命令中被用作本地主机的 FQDN。否则将使用 socket.getfqdn() 找到本地主机名。如果 connect() 返回了成功码以外的内容，则引发 SMTPConnectError 异常。可选参数 timeout 指定阻塞操作（如连接尝试）的超时（以秒为单位，如果未指定超时，将使用全局默认超时设置）。到达超时时长后会引发 socket.timeout 异常。可选参数 source_address 允许在有多张网卡的计算机中绑定到某些特定的源地址，和/或绑定到某些特定的源 TCP 端口。在连接前，套接字需要绑定一个 2 元组 (host, port) 作为其源地址。如果省略，或者是主机为 '' 和/或端口为 0，则将使用操作系统默认行为。

正常使用时，只需要初始化或 connect 方法，sendmail() 方法，再加上 SMTP.quit() 方法即可。下文包括了一个示例。

The SMTP class supports the with statement. When used like this, the SMTP QUIT command is issued automatically when the with statement exits. E.g.:

>>> from smtplib import SMTP
>>> with SMTP("domain.org") as smtp:
...     smtp.noop()
...
(250, b'Ok')
>>>

在 3.3 版更改: 支持了 with 语句。

在 3.3 版更改: source_address argument was added.

3.5 新版功能: The SMTPUTF8 extension (RFC 6531 [https://tools.ietf.org/html/rfc6531.html]) is now supported.

class smtplib.SMTP_SSL(host='', port=0, local_hostname=None, keyfile=None, certfile=None, [timeout, ]context=None, source_address=None)

SMTP_SSL 实例与 SMTP 实例的行为完全相同。在开始连接就需要 SSL，且 starttls() 不适合的情况下，应该使用 SMTP_SSL。如果未指定 host，则使用 localhost。如果 port 为 0，则使用标准 SMTP-over-SSL 端口（465）。可选参数 local_hostname、timeout 和 source_address 的含义与 SMTP 类中的相同。可选参数 context 是一个 SSLContext 对象，可以从多个方面配置安全连接。请阅读 安全考量 以获取最佳实践。

keyfile and certfile are a legacy alternative to context, and can point to a PEM formatted private key and certificate chain file for the SSL connection.

在 3.3 版更改: 增加了 context。

在 3.3 版更改: source_address argument was added.

在 3.4 版更改: 本类现在支持通过 ssl.SSLContext.check_hostname 和 服务器名称提示 （参阅 ssl.HAS_SNI）进行主机名检查。

3.6 版后已移除: keyfile 和 certfile 已弃用并转而推荐 context。 请改用 ssl.SSLContext.load_cert_chain() 或让 ssl.create_default_context() 为你选择系统所信任的 CA 证书。

class smtplib.LMTP(host='', port=LMTP_PORT, local_hostname=None, source_address=None)

LMTP 协议与 ESMTP 非常相似，它很大程度上基于标准的 SMTP 客户端。将 Unix 套接字用于 LMTP 是很常见的，因此 connect() 方法支持 Unix 套接字，也支持常规的 host:port 服务器。可选参数 local_hostname 和 source_address 的含义与 SMTP 类中的相同。要指定 Unix 套接字，host 必须使用绝对路径，以 ‘/’ 开头。

Authentication is supported, using the regular SMTP mechanism. When using a Unix socket, LMTP generally don’t support or require any authentication, but your mileage might vary.

A nice selection of exceptions is defined as well:

exception smtplib.SMTPException

Subclass of OSError that is the base exception class for all the other exceptions provided by this module.

在 3.4 版更改: SMTPException became subclass of OSError

exception smtplib.SMTPServerDisconnected

This exception is raised when the server unexpectedly disconnects, or when an attempt is made to use the SMTP instance before connecting it to a server.

exception smtplib.SMTPResponseException

Base class for all exceptions that include an SMTP error code. These exceptions are generated in some instances when the SMTP server returns an error code. The error code is stored in the smtp_code attribute of the error, and the smtp_error attribute is set to the error message.

exception smtplib.SMTPSenderRefused

Sender address refused. In addition to the attributes set by on all SMTPResponseException exceptions, this sets ‘sender’ to the string that the SMTP server refused.

exception smtplib.SMTPRecipientsRefused

All recipient addresses refused. The errors for each recipient are accessible through the attribute recipients, which is a dictionary of exactly the same sort as SMTP.sendmail() returns.

exception smtplib.SMTPDataError

The SMTP server refused to accept the message data.

exception smtplib.SMTPConnectError

Error occurred during establishment of a connection with the server.

exception smtplib.SMTPHeloError

The server refused our HELO message.

exception smtplib.SMTPNotSupportedError

The command or option attempted is not supported by the server.

3.5 新版功能.

exception smtplib.SMTPAuthenticationError

SMTP authentication went wrong. Most probably the server didn’t accept the username/password combination provided.

参见

RFC 821 [https://tools.ietf.org/html/rfc821.html] - Simple Mail Transfer Protocol

Protocol definition for SMTP. This document covers the model, operating procedure, and protocol details for SMTP.

RFC 1869 [https://tools.ietf.org/html/rfc1869.html] - SMTP Service Extensions

Definition of the ESMTP extensions for SMTP. This describes a framework for extending SMTP with new commands, supporting dynamic discovery of the commands provided by the server, and defines a few additional commands.

21.17.1. SMTP Objects

An SMTP instance has the following methods:

SMTP.set_debuglevel(level)

Set the debug output level. A value of 1 or True for level results in debug messages for connection and for all messages sent to and received from the server. A value of 2 for level results in these messages being timestamped.

在 3.5 版更改: Added debuglevel 2.

SMTP.docmd(cmd, args='')

Send a command cmd to the server. The optional argument args is simply concatenated to the command, separated by a space.

This returns a 2-tuple composed of a numeric response code and the actual response line (multiline responses are joined into one long line.)

In normal operation it should not be necessary to call this method explicitly. It is used to implement other methods and may be useful for testing private extensions.

If the connection to the server is lost while waiting for the reply, SMTPServerDisconnected will be raised.

SMTP.connect(host='localhost', port=0)

连接到某个主机的某个端口。默认是连接到 localhost 的标准 SMTP 端口（25）上。如果主机名以冒号 (':') 结尾，后跟数字，则该后缀将被删除，且数字将视作要使用的端口号。如果在实例化时指定了 host，则构造函数会自动调用本方法。返回包含响应码和响应消息的 2 元组，它们由服务器在其连接响应中发送。

SMTP.helo(name='')

Identify yourself to the SMTP server using HELO. The hostname argument defaults to the fully qualified domain name of the local host. The message returned by the server is stored as the helo_resp attribute of the object.

In normal operation it should not be necessary to call this method explicitly. It will be implicitly called by the sendmail() when necessary.

SMTP.ehlo(name='')

使用 EHLO 向 ESMTP 服务器标识自身。hostname 参数默认为 localhost 的标准域名。使用 has_extn() 来检查响应中的 ESMTP 选项，并将它们保存起来。还给一些信息性的属性赋值：服务器返回的消息存储为 ehlo_resp 属性；根据服务器是否支持 ESMTP，将 does_esmtp 设置为 true 或 false；而 esmtp_features 是一个字典，包含该服务器支持的 SMTP 服务扩展的名称及参数（如果有参数）。

Unless you wish to use has_extn() before sending mail, it should not be necessary to call this method explicitly. It will be implicitly called by sendmail() when necessary.

SMTP.ehlo_or_helo_if_needed()

This method calls ehlo() and/or helo() if there has been no previous EHLO or HELO command this session. It tries ESMTP EHLO first.

SMTPHeloError

The server didn’t reply properly to the HELO greeting.

SMTP.has_extn(name)

Return True if name is in the set of SMTP service extensions returned by the server, False otherwise. Case is ignored.

SMTP.verify(address)

Check the validity of an address on this server using SMTP VRFY. Returns a tuple consisting of code 250 and a full RFC 822 [https://tools.ietf.org/html/rfc822.html] address (including human name) if the user address is valid. Otherwise returns an SMTP error code of 400 or greater and an error string.

注解

Many sites disable SMTP VRFY in order to foil spammers.

SMTP.login(user, password, *, initial_response_ok=True)

Log in on an SMTP server that requires authentication. The arguments are the username and the password to authenticate with. If there has been no previous EHLO or HELO command this session, this method tries ESMTP EHLO first. This method will return normally if the authentication was successful, or may raise the following exceptions:

SMTPHeloError

The server didn’t reply properly to the HELO greeting.

SMTPAuthenticationError

The server didn’t accept the username/password combination.

SMTPNotSupportedError

The AUTH command is not supported by the server.

SMTPException

No suitable authentication method was found.

Each of the authentication methods supported by smtplib are tried in turn if they are advertised as supported by the server. See auth() for a list of supported authentication methods. initial_response_ok is passed through to auth().

Optional keyword argument initial_response_ok specifies whether, for authentication methods that support it, an “initial response” as specified in RFC 4954 [https://tools.ietf.org/html/rfc4954.html] can be sent along with the AUTH command, rather than requiring a challenge/response.

在 3.5 版更改: SMTPNotSupportedError may be raised, and the initial_response_ok parameter was added.

SMTP.auth(mechanism, authobject, *, initial_response_ok=True)

Issue an SMTP AUTH command for the specified authentication mechanism, and handle the challenge response via authobject.

mechanism specifies which authentication mechanism is to be used as argument to the AUTH command; the valid values are those listed in the auth element of esmtp_features.

authobject must be a callable object taking an optional single argument:

data = authobject(challenge=None)

If optional keyword argument initial_response_ok is true, authobject() will be called first with no argument. It can return the RFC 4954 [https://tools.ietf.org/html/rfc4954.html] “initial response” ASCII str which will be encoded and sent with the AUTH command as below. If the authobject() does not support an initial response (e.g. because it requires a challenge), it should return None when called with challenge=None. If initial_response_ok is false, then authobject() will not be called first with None.

If the initial response check returns None, or if initial_response_ok is false, authobject() will be called to process the server’s challenge response; the challenge argument it is passed will be a bytes. It should return ASCII str data that will be base64 encoded and sent to the server.

The SMTP class provides authobjects for the CRAM-MD5, PLAIN, and LOGIN mechanisms; they are named SMTP.auth_cram_md5, SMTP.auth_plain, and SMTP.auth_login respectively. They all require that the user and password properties of the SMTP instance are set to appropriate values.

User code does not normally need to call auth directly, but can instead call the login() method, which will try each of the above mechanisms in turn, in the order listed. auth is exposed to facilitate the implementation of authentication methods not (or not yet) supported directly by smtplib.

3.5 新版功能.

SMTP.starttls(keyfile=None, certfile=None, context=None)

Put the SMTP connection in TLS (Transport Layer Security) mode. All SMTP commands that follow will be encrypted. You should then call ehlo() again.

If keyfile and certfile are provided, they are used to create an ssl.SSLContext.

Optional context parameter is an ssl.SSLContext object; This is an alternative to using a keyfile and a certfile and if specified both keyfile and certfile should be None.

If there has been no previous EHLO or HELO command this session, this method tries ESMTP EHLO first.

3.6 版后已移除: keyfile 和 certfile 已弃用并转而推荐 context。 请改用 ssl.SSLContext.load_cert_chain() 或让 ssl.create_default_context() 为你选择系统所信任的 CA 证书。

SMTPHeloError

The server didn’t reply properly to the HELO greeting.

SMTPNotSupportedError

The server does not support the STARTTLS extension.

RuntimeError

SSL/TLS support is not available to your Python interpreter.

在 3.3 版更改: 增加了 context。

在 3.4 版更改: The method now supports hostname check with SSLContext.check_hostname and Server Name Indicator (see HAS_SNI).

在 3.5 版更改: The error raised for lack of STARTTLS support is now the SMTPNotSupportedError subclass instead of the base SMTPException.

SMTP.sendmail(from_addr, to_addrs, msg, mail_options=(), rcpt_options=())

发送邮件。必要参数是一个 RFC 822 [https://tools.ietf.org/html/rfc822.html] 发件地址字符串，一个 RFC 822 [https://tools.ietf.org/html/rfc822.html] 收件地址字符串列表（裸字符串将被视为含有 1 个地址的列表），以及一个消息字符串。调用者可以将 ESMTP 选项列表（如 8bitmime）作为 mail_options 传入，用于 MAIL FROM 命令。需要与所有 RCPT 命令一起使用的 ESMTP 选项（如 DSN 命令）可以作为 rcpt_options 传入。（如果需要对不同的收件人使用不同的 ESMTP 选项，则必须使用底层的方法来发送消息，如 mail(), rcpt() 和 data()。）

注解

The from_addr and to_addrs parameters are used to construct the message envelope used by the transport agents. sendmail does not modify the message headers in any way.

msg may be a string containing characters in the ASCII range, or a byte string. A string is encoded to bytes using the ascii codec, and lone \r and \n characters are converted to \r\n characters. A byte string is not modified.

If there has been no previous EHLO or HELO command this session, this method tries ESMTP EHLO first. If the server does ESMTP, message size and each of the specified options will be passed to it (if the option is in the feature set the server advertises). If EHLO fails, HELO will be tried and ESMTP options suppressed.

This method will return normally if the mail is accepted for at least one recipient. Otherwise it will raise an exception. That is, if this method does not raise an exception, then someone should get your mail. If this method does not raise an exception, it returns a dictionary, with one entry for each recipient that was refused. Each entry contains a tuple of the SMTP error code and the accompanying error message sent by the server.

If SMTPUTF8 is included in mail_options, and the server supports it, from_addr and to_addrs may contain non-ASCII characters.

This method may raise the following exceptions:

SMTPRecipientsRefused

All recipients were refused. Nobody got the mail. The recipients attribute of the exception object is a dictionary with information about the refused recipients (like the one returned when at least one recipient was accepted).

SMTPHeloError

The server didn’t reply properly to the HELO greeting.

SMTPSenderRefused

The server didn’t accept the from_addr.

SMTPDataError

The server replied with an unexpected error code (other than a refusal of a recipient).

SMTPNotSupportedError

SMTPUTF8 was given in the mail_options but is not supported by the server.

Unless otherwise noted, the connection will be open even after an exception is raised.

在 3.2 版更改: msg may be a byte string.

在 3.5 版更改: SMTPUTF8 support added, and SMTPNotSupportedError may be raised if SMTPUTF8 is specified but the server does not support it.

SMTP.send_message(msg, from_addr=None, to_addrs=None, mail_options=(), rcpt_options=())

本方法是一种快捷方法，用于带着消息调用 sendmail()，消息由 email.message.Message 对象表示。参数的含义与 sendmail() 中的相同，除了 msg，它是一个 Message 对象。

如果 from_addr 为 None 或 to_addrs 为 None，那么``send_message``将根据 RFC 5322 [https://tools.ietf.org/html/rfc5322.html]，从 msg 头部提取地址填充下列参数：如果头部存在 Sender 字段，则用它填充 from_addr，不存在则用 From 字段填充 from_addr。to_addrs 组合了 msg 中的 To, Cc 和 Bcc 字段的值（字段存在的情况下）。如果一组 Resent-* 头部恰好出现在 message 中，那么就忽略常规的头部，改用 Resent-* 头部。如果 message 包含多组 Resent-* 头部，则引发 ValueError，因为无法明确检测出哪一组 Resent- 头部是最新的。

send_message 使用 BytesGenerator 来序列化 msg，且将 \r\n 作为 linesep，并调用 sendmail() 来传输序列化后的结果。无论 from_addr 和 to_addrs 的值为何，send_message 都不会传输 msg 中可能出现的 Bcc 或 Resent-Bcc 头部。如果 from_addr 和 to_addrs 中的某个地址包含非 ASCII 字符，且服务器没有声明支持 SMTPUTF8，则引发 SMTPNotSupported 错误。如果服务器支持，则 Message 将按新克隆的 policy 进行序列化，其中的 utf8 属性被设置为 True，且 SMTPUTF8 和 BODY=8BITMIME 被添加到 mail_options 中。

3.2 新版功能.

3.5 新版功能: Support for internationalized addresses (SMTPUTF8).

SMTP.quit()

Terminate the SMTP session and close the connection. Return the result of the SMTP QUIT command.

Low-level methods corresponding to the standard SMTP/ESMTP commands HELP, RSET, NOOP, MAIL, RCPT, and DATA are also supported. Normally these do not need to be called directly, so they are not documented here. For details, consult the module code.

21.17.2. SMTP Example

This example prompts the user for addresses needed in the message envelope (‘To’ and ‘From’ addresses), and the message to be delivered. Note that the headers to be included with the message must be included in the message as entered; this example doesn’t do any processing of the RFC 822 [https://tools.ietf.org/html/rfc822.html] headers. In particular, the ‘To’ and ‘From’ addresses must be included in the message headers explicitly.

import smtplib

def prompt(prompt):
    return input(prompt).strip()

fromaddr = prompt("From: ")
toaddrs  = prompt("To: ").split()
print("Enter message, end with ^D (Unix) or ^Z (Windows):")

# Add the From: and To: headers at the start!
msg = ("From: %s\r\nTo: %s\r\n\r\n"
       % (fromaddr, ", ".join(toaddrs)))
while True:
    try:
        line = input()
    except EOFError:
        break
    if not line:
        break
    msg = msg + line

print("Message length is", len(msg))

server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

注解

In general, you will want to use the email package’s features to construct an email message, which you can then send via send_message(); see email: 示例.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.18. smtpd — SMTP 服务器

源代码: Lib/smtpd.py [https://github.com/python/cpython/tree/3.6/Lib/smtpd.py]

This module offers several classes to implement SMTP (email) servers.

参见

The aiosmtpd [http://aiosmtpd.readthedocs.io/] package is a recommended replacement for this module. It is based on asyncio and provides a more straightforward API. smtpd should be considered deprecated.

Several server implementations are present; one is a generic do-nothing implementation, which can be overridden, while the other two offer specific mail-sending strategies.

Additionally the SMTPChannel may be extended to implement very specific interaction behaviour with SMTP clients.

The code supports RFC 5321 [https://tools.ietf.org/html/rfc5321.html], plus the RFC 1870 [https://tools.ietf.org/html/rfc1870.html] SIZE and RFC 6531 [https://tools.ietf.org/html/rfc6531.html] SMTPUTF8 extensions.

21.18.1. SMTPServer 对象

class smtpd.SMTPServer(localaddr, remoteaddr, data_size_limit=33554432, map=None, enable_SMTPUTF8=False, decode_data=False)

Create a new SMTPServer object, which binds to local address localaddr. It will treat remoteaddr as an upstream SMTP relayer. Both localaddr and remoteaddr should be a (host, port) tuple. The object inherits from asyncore.dispatcher, and so will insert itself into asyncore’s event loop on instantiation.

data_size_limit specifies the maximum number of bytes that will be accepted in a DATA command. A value of None or 0 means no limit.

map is the socket map to use for connections (an initially empty dictionary is a suitable value). If not specified the asyncore global socket map is used.

enable_SMTPUTF8 determines whether the SMTPUTF8 extension (as defined in RFC 6531 [https://tools.ietf.org/html/rfc6531.html]) should be enabled. The default is False. When True, SMTPUTF8 is accepted as a parameter to the MAIL command and when present is passed to process_message() in the kwargs['mail_options'] list. decode_data and enable_SMTPUTF8 cannot be set to True at the same time.

decode_data specifies whether the data portion of the SMTP transaction should be decoded using UTF-8. When decode_data is False (the default), the server advertises the 8BITMIME extension (RFC 6152 [https://tools.ietf.org/html/rfc6152.html]), accepts the BODY=8BITMIME parameter to the MAIL command, and when present passes it to process_message() in the kwargs['mail_options'] list. decode_data and enable_SMTPUTF8 cannot be set to True at the same time.

process_message(peer, mailfrom, rcpttos, data, **kwargs)

Raise a NotImplementedError exception. Override this in subclasses to do something useful with this message. Whatever was passed in the constructor as remoteaddr will be available as the _remoteaddr attribute. peer is the remote host’s address, mailfrom is the envelope originator, rcpttos are the envelope recipients and data is a string containing the contents of the e-mail (which should be in RFC 5321 [https://tools.ietf.org/html/rfc5321.html] format).

If the decode_data constructor keyword is set to True, the data argument will be a unicode string. If it is set to False, it will be a bytes object.

kwargs is a dictionary containing additional information. It is empty if decode_data=True was given as an init argument, otherwise it contains the following keys:

mail_options:

a list of all received parameters to the MAIL command (the elements are uppercase strings; example: ['BODY=8BITMIME', 'SMTPUTF8']).

rcpt_options:

same as mail_options but for the RCPT command. Currently no RCPT TO options are supported, so for now this will always be an empty list.

Implementations of process_message should use the **kwargs signature to accept arbitrary keyword arguments, since future feature enhancements may add keys to the kwargs dictionary.

Return None to request a normal 250 Ok response; otherwise return the desired response string in RFC 5321 [https://tools.ietf.org/html/rfc5321.html] format.

channel_class

Override this in subclasses to use a custom SMTPChannel for managing SMTP clients.

3.4 新版功能: The map constructor argument.

在 3.5 版更改: localaddr and remoteaddr may now contain IPv6 addresses.

3.5 新版功能: The decode_data and enable_SMTPUTF8 constructor parameters, and the kwargs parameter to process_message() when decode_data is False.

在 3.6 版更改: decode_data is now False by default.

21.18.2. DebuggingServer 对象

class smtpd.DebuggingServer(localaddr, remoteaddr)

Create a new debugging server. Arguments are as per SMTPServer. Messages will be discarded, and printed on stdout.

21.18.3. PureProxy对象

class smtpd.PureProxy(localaddr, remoteaddr)

Create a new pure proxy server. Arguments are as per SMTPServer. Everything will be relayed to remoteaddr. Note that running this has a good chance to make you into an open relay, so please be careful.

21.18.4. MailmanProxy 对象

class smtpd.MailmanProxy(localaddr, remoteaddr)

Create a new pure proxy server. Arguments are as per SMTPServer. Everything will be relayed to remoteaddr, unless local mailman configurations knows about an address, in which case it will be handled via mailman. Note that running this has a good chance to make you into an open relay, so please be careful.

21.18.5. SMTPChannel 对象

class smtpd.SMTPChannel(server, conn, addr, data_size_limit=33554432, map=None, enable_SMTPUTF8=False, decode_data=False)

Create a new SMTPChannel object which manages the communication between the server and a single SMTP client.

conn and addr are as per the instance variables described below.

data_size_limit specifies the maximum number of bytes that will be accepted in a DATA command. A value of None or 0 means no limit.

enable_SMTPUTF8 determines whether the SMTPUTF8 extension (as defined in RFC 6531 [https://tools.ietf.org/html/rfc6531.html]) should be enabled. The default is False. decode_data and enable_SMTPUTF8 cannot be set to True at the same time.

A dictionary can be specified in map to avoid using a global socket map.

decode_data specifies whether the data portion of the SMTP transaction should be decoded using UTF-8. The default is False. decode_data and enable_SMTPUTF8 cannot be set to True at the same time.

To use a custom SMTPChannel implementation you need to override the SMTPServer.channel_class of your SMTPServer.

在 3.5 版更改: The decode_data and enable_SMTPUTF8 parameters were added.

在 3.6 版更改: decode_data is now False by default.

The SMTPChannel has the following instance variables:

smtp_server

Holds the SMTPServer that spawned this channel.

conn

Holds the socket object connecting to the client.

addr

Holds the address of the client, the second value returned by socket.accept

received_lines

Holds a list of the line strings (decoded using UTF-8) received from the client. The lines have their "\r\n" line ending translated to "\n".

smtp_state

Holds the current state of the channel. This will be either COMMAND initially and then DATA after the client sends a “DATA” line.

seen_greeting

Holds a string containing the greeting sent by the client in its “HELO”.

mailfrom

Holds a string containing the address identified in the “MAIL FROM:” line from the client.

rcpttos

Holds a list of strings containing the addresses identified in the “RCPT TO:” lines from the client.

received_data

Holds a string containing all of the data sent by the client during the DATA state, up to but not including the terminating "\r\n.\r\n".

fqdn

Holds the fully-qualified domain name of the server as returned by socket.getfqdn().

peer

Holds the name of the client peer as returned by conn.getpeername() where conn is conn.

The SMTPChannel operates by invoking methods named smtp_<command> upon reception of a command line from the client. Built into the base SMTPChannel class are methods for handling the following commands (and responding to them appropriately):

命令

所采取的行动

HELO

接受来自客户端的问候语，并将其存储在 seen_greeting 中。将服务器设置为基本命令模式。

EHLO

接受来自客户的问候并将其存储在 seen_greeting 中。将服务器设置为扩展命令模式。

NOOP

不采取任何措施。

QUIT

干净地关闭连接。

MAIL

Accepts the “MAIL FROM:” syntax and stores the supplied address as mailfrom. In extended command mode, accepts the RFC 1870 [https://tools.ietf.org/html/rfc1870.html] SIZE attribute and responds appropriately based on the value of data_size_limit.

RCPT

Accepts the “RCPT TO:” syntax and stores the supplied addresses in the rcpttos list.

RSET

重置 mailfrom, rcpttos, 和 received_data ，但不重置问候语。

DATA

Sets the internal state to DATA and stores remaining lines from the client in received_data until the terminator "\r\n.\r\n" is received.

HELP

返回有关命令语法的最少信息

VRFY

返回代码252（服务器不知道该地址是否有效）

EXPN

报告该命令未实现。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.19. telnetlib — Telnet client

Source code: Lib/telnetlib.py [https://github.com/python/cpython/tree/3.6/Lib/telnetlib.py]

The telnetlib module provides a Telnet class that implements the Telnet protocol. See RFC 854 [https://tools.ietf.org/html/rfc854.html] for details about the protocol. In addition, it provides symbolic constants for the protocol characters (see below), and for the telnet options. The symbolic names of the telnet options follow the definitions in arpa/telnet.h, with the leading TELOPT_ removed. For symbolic names of options which are traditionally not included in arpa/telnet.h, see the module source itself.

The symbolic constants for the telnet commands are: IAC, DONT, DO, WONT, WILL, SE (Subnegotiation End), NOP (No Operation), DM (Data Mark), BRK (Break), IP (Interrupt process), AO (Abort output), AYT (Are You There), EC (Erase Character), EL (Erase Line), GA (Go Ahead), SB (Subnegotiation Begin).

class telnetlib.Telnet(host=None, port=0[, timeout])

Telnet represents a connection to a Telnet server. The instance is initially not connected by default; the open() method must be used to establish a connection. Alternatively, the host name and optional port number can be passed to the constructor too, in which case the connection to the server will be established before the constructor returns. The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used).

Do not reopen an already connected instance.

This class has many read_*() methods. Note that some of them raise EOFError when the end of the connection is read, because they can return an empty string for other reasons. See the individual descriptions below.

A Telnet object is a context manager and can be used in a with statement. When the with block ends, the close() method is called:

>>> from telnetlib import Telnet
>>> with Telnet('localhost', 23) as tn:
...     tn.interact()
...

在 3.6 版更改: Context manager support added

参见

RFC 854 [https://tools.ietf.org/html/rfc854.html] - Telnet Protocol Specification

Definition of the Telnet protocol.

21.19.1. Telnet Objects

Telnet instances have the following methods:

Telnet.read_until(expected, timeout=None)

Read until a given byte string, expected, is encountered or until timeout seconds have passed.

When no match is found, return whatever is available instead, possibly empty bytes. Raise EOFError if the connection is closed and no cooked data is available.

Telnet.read_all()

Read all data until EOF as bytes; block until connection closed.

Telnet.read_some()

Read at least one byte of cooked data unless EOF is hit. Return b'' if EOF is hit. Block if no data is immediately available.

Telnet.read_very_eager()

Read everything that can be without blocking in I/O (eager).

Raise EOFError if connection closed and no cooked data available. Return b'' if no cooked data available otherwise. Do not block unless in the midst of an IAC sequence.

Telnet.read_eager()

Read readily available data.

Raise EOFError if connection closed and no cooked data available. Return b'' if no cooked data available otherwise. Do not block unless in the midst of an IAC sequence.

Telnet.read_lazy()

Process and return data already in the queues (lazy).

Raise EOFError if connection closed and no data available. Return b'' if no cooked data available otherwise. Do not block unless in the midst of an IAC sequence.

Telnet.read_very_lazy()

Return any data available in the cooked queue (very lazy).

Raise EOFError if connection closed and no data available. Return b'' if no cooked data available otherwise. This method never blocks.

Telnet.read_sb_data()

Return the data collected between a SB/SE pair (suboption begin/end). The callback should access these data when it was invoked with a SE command. This method never blocks.

Telnet.open(host, port=0[, timeout])

Connect to a host. The optional second argument is the port number, which defaults to the standard Telnet port (23). The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used).

Do not try to reopen an already connected instance.

Telnet.msg(msg, *args)

Print a debug message when the debug level is > 0. If extra arguments are present, they are substituted in the message using the standard string formatting operator.

Telnet.set_debuglevel(debuglevel)

Set the debug level. The higher the value of debuglevel, the more debug output you get (on sys.stdout).

Telnet.close()

关闭连接对象。

Telnet.get_socket()

Return the socket object used internally.

Telnet.fileno()

Return the file descriptor of the socket object used internally.

Telnet.write(buffer)

Write a byte string to the socket, doubling any IAC characters. This can block if the connection is blocked. May raise OSError if the connection is closed.

在 3.3 版更改: This method used to raise socket.error, which is now an alias of OSError.

Telnet.interact()

Interaction function, emulates a very dumb Telnet client.

Telnet.mt_interact()

Multithreaded version of interact().

Telnet.expect(list, timeout=None)

Read until one from a list of a regular expressions matches.

The first argument is a list of regular expressions, either compiled (regex objects) or uncompiled (byte strings). The optional second argument is a timeout, in seconds; the default is to block indefinitely.

Return a tuple of three items: the index in the list of the first regular expression that matches; the match object returned; and the bytes read up till and including the match.

If end of file is found and no bytes were read, raise EOFError. Otherwise, when nothing matches, return (-1, None, data) where data is the bytes received so far (may be empty bytes if a timeout happened).

If a regular expression ends with a greedy match (such as .*) or if more than one expression can match the same input, the results are non-deterministic, and may depend on the I/O timing.

Telnet.set_option_negotiation_callback(callback)

Each time a telnet option is read on the input flow, this callback (if set) is called with the following parameters: callback(telnet socket, command (DO/DONT/WILL/WONT), option). No other action is done afterwards by telnetlib.

21.19.2. Telnet Example

A simple example illustrating typical use:

import getpass
import telnetlib

HOST = "localhost"
user = input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

tn.read_until(b"login: ")
tn.write(user.encode('ascii') + b"\n")
if password:
    tn.read_until(b"Password: ")
    tn.write(password.encode('ascii') + b"\n")

tn.write(b"ls\n")
tn.write(b"exit\n")

print(tn.read_all().decode('ascii'))

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.20. uuid — UUID objects according to RFC 4122 [https://tools.ietf.org/html/rfc4122.html]

Source code: Lib/uuid.py [https://github.com/python/cpython/tree/3.6/Lib/uuid.py]

This module provides immutable UUID objects (the UUID class) and the functions uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5 UUIDs as specified in RFC 4122 [https://tools.ietf.org/html/rfc4122.html].

If all you want is a unique ID, you should probably call uuid1() or uuid4(). Note that uuid1() may compromise privacy since it creates a UUID containing the computer’s network address. uuid4() creates a random UUID.

class uuid.UUID(hex=None, bytes=None, bytes_le=None, fields=None, int=None, version=None)

Create a UUID from either a string of 32 hexadecimal digits, a string of 16 bytes in big-endian order as the bytes argument, a string of 16 bytes in little-endian order as the bytes_le argument, a tuple of six integers (32-bit time_low, 16-bit time_mid, 16-bit time_hi_version, 8-bit clock_seq_hi_variant, 8-bit clock_seq_low, 48-bit node) as the fields argument, or a single 128-bit integer as the int argument. When a string of hex digits is given, curly braces, hyphens, and a URN prefix are all optional. For example, these expressions all yield the same UUID:

UUID('{12345678-1234-5678-1234-567812345678}')
UUID('12345678123456781234567812345678')
UUID('urn:uuid:12345678-1234-5678-1234-567812345678')
UUID(bytes=b'\x12\x34\x56\x78'*4)
UUID(bytes_le=b'\x78\x56\x34\x12\x34\x12\x78\x56' +
              b'\x12\x34\x56\x78\x12\x34\x56\x78')
UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678))
UUID(int=0x12345678123456781234567812345678)

Exactly one of hex, bytes, bytes_le, fields, or int must be given. The version argument is optional; if given, the resulting UUID will have its variant and version number set according to RFC 4122 [https://tools.ietf.org/html/rfc4122.html], overriding bits in the given hex, bytes, bytes_le, fields, or int.

Comparison of UUID objects are made by way of comparing their UUID.int attributes. Comparison with a non-UUID object raises a TypeError.

str(uuid) returns a string in the form 12345678-1234-5678-1234-567812345678 where the 32 hexadecimal digits represent the UUID.

UUID instances have these read-only attributes:

UUID.bytes

The UUID as a 16-byte string (containing the six integer fields in big-endian byte order).

UUID.bytes_le

The UUID as a 16-byte string (with time_low, time_mid, and time_hi_version in little-endian byte order).

UUID.fields

以元组形式存放的UUID的6个整数域，有六个单独的属性和两个派生属性：

域

含义

time_low

UUID的前32位

time_mid

接前一域的16位

time_hi_version

接前一域的16位

clock_seq_hi_variant

接前一域的8位

clock_seq_low

接前一域的8位

node

UUID的最后48位

time

UUID的总长60位的时间戳

clock_seq

14位的序列号

UUID.hex

The UUID as a 32-character hexadecimal string.

UUID.int

The UUID as a 128-bit integer.

UUID.urn

The UUID as a URN as specified in RFC 4122 [https://tools.ietf.org/html/rfc4122.html].

UUID.variant

The UUID variant, which determines the internal layout of the UUID. This will be one of the constants RESERVED_NCS, RFC_4122, RESERVED_MICROSOFT, or RESERVED_FUTURE.

UUID.version

The UUID version number (1 through 5, meaningful only when the variant is RFC_4122).

The uuid module defines the following functions:

uuid.getnode()

Get the hardware address as a 48-bit positive integer. The first time this runs, it may launch a separate program, which could be quite slow. If all attempts to obtain the hardware address fail, we choose a random 48-bit number with its eighth bit set to 1 as recommended in RFC 4122 [https://tools.ietf.org/html/rfc4122.html]. “Hardware address” means the MAC address of a network interface, and on a machine with multiple network interfaces the MAC address of any one of them may be returned.

uuid.uuid1(node=None, clock_seq=None)

Generate a UUID from a host ID, sequence number, and the current time. If node is not given, getnode() is used to obtain the hardware address. If clock_seq is given, it is used as the sequence number; otherwise a random 14-bit sequence number is chosen.

uuid.uuid3(namespace, name)

Generate a UUID based on the MD5 hash of a namespace identifier (which is a UUID) and a name (which is a string).

uuid.uuid4()

Generate a random UUID.

uuid.uuid5(namespace, name)

Generate a UUID based on the SHA-1 hash of a namespace identifier (which is a UUID) and a name (which is a string).

The uuid module defines the following namespace identifiers for use with uuid3() or uuid5().

uuid.NAMESPACE_DNS

When this namespace is specified, the name string is a fully-qualified domain name.

uuid.NAMESPACE_URL

When this namespace is specified, the name string is a URL.

uuid.NAMESPACE_OID

When this namespace is specified, the name string is an ISO OID.

uuid.NAMESPACE_X500

When this namespace is specified, the name string is an X.500 DN in DER or a text output format.

The uuid module defines the following constants for the possible values of the variant attribute:

uuid.RESERVED_NCS

Reserved for NCS compatibility.

uuid.RFC_4122

Specifies the UUID layout given in RFC 4122 [https://tools.ietf.org/html/rfc4122.html].

uuid.RESERVED_MICROSOFT

Reserved for Microsoft compatibility.

uuid.RESERVED_FUTURE

Reserved for future definition.

参见

RFC 4122 [https://tools.ietf.org/html/rfc4122.html] - A Universally Unique IDentifier (UUID) URN Namespace

This specification defines a Uniform Resource Name namespace for UUIDs, the internal format of UUIDs, and methods of generating UUIDs.

21.20.1. 示例

Here are some examples of typical usage of the uuid module:

>>> import uuid

>>> # make a UUID based on the host ID and current time
>>> uuid.uuid1()
UUID('a8098c1a-f86e-11da-bd1a-00112444be1e')

>>> # make a UUID using an MD5 hash of a namespace UUID and a name
>>> uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
UUID('6fa459ea-ee8a-3ca4-894e-db77e160355e')

>>> # make a random UUID
>>> uuid.uuid4()
UUID('16fd2706-8baf-433b-82eb-8c7fada847da')

>>> # make a UUID using a SHA-1 hash of a namespace UUID and a name
>>> uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
UUID('886313e1-3b8a-5372-9b90-0c9aee199e5d')

>>> # make a UUID from a string of hex digits (braces and hyphens ignored)
>>> x = uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}')

>>> # convert a UUID to a string of hex digits in standard form
>>> str(x)
'00010203-0405-0607-0809-0a0b0c0d0e0f'

>>> # get the raw 16 bytes of the UUID
>>> x.bytes
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'

>>> # make a UUID from a 16-byte string
>>> uuid.UUID(bytes=x.bytes)
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.21. socketserver — A framework for network servers

Source code: Lib/socketserver.py [https://github.com/python/cpython/tree/3.6/Lib/socketserver.py]

The socketserver module simplifies the task of writing network servers.

There are four basic concrete server classes:

class socketserver.TCPServer(server_address, RequestHandlerClass, bind_and_activate=True)

This uses the Internet TCP protocol, which provides for continuous streams of data between the client and server. If bind_and_activate is true, the constructor automatically attempts to invoke server_bind() and server_activate(). The other parameters are passed to the BaseServer base class.

class socketserver.UDPServer(server_address, RequestHandlerClass, bind_and_activate=True)

This uses datagrams, which are discrete packets of information that may arrive out of order or be lost while in transit. The parameters are the same as for TCPServer.

class socketserver.UnixStreamServer(server_address, RequestHandlerClass, bind_and_activate=True)

class socketserver.UnixDatagramServer(server_address, RequestHandlerClass, bind_and_activate=True)

These more infrequently used classes are similar to the TCP and UDP classes, but use Unix domain sockets; they’re not available on non-Unix platforms. The parameters are the same as for TCPServer.

These four classes process requests synchronously; each request must be completed before the next request can be started. This isn’t suitable if each request takes a long time to complete, because it requires a lot of computation, or because it returns a lot of data which the client is slow to process. The solution is to create a separate process or thread to handle each request; the ForkingMixIn and ThreadingMixIn mix-in classes can be used to support asynchronous behaviour.

Creating a server requires several steps. First, you must create a request handler class by subclassing the BaseRequestHandler class and overriding its handle() method; this method will process incoming requests. Second, you must instantiate one of the server classes, passing it the server’s address and the request handler class. It is recommended to use the server in a with statement. Then call the handle_request() or serve_forever() method of the server object to process one or many requests. Finally, call server_close() to close the socket (unless you used a with statement).

When inheriting from ThreadingMixIn for threaded connection behavior, you should explicitly declare how you want your threads to behave on an abrupt shutdown. The ThreadingMixIn class defines an attribute daemon_threads, which indicates whether or not the server should wait for thread termination. You should set the flag explicitly if you would like threads to behave autonomously; the default is False, meaning that Python will not exit until all threads created by ThreadingMixIn have exited.

Server classes have the same external methods and attributes, no matter what network protocol they use.

21.21.1. Server Creation Notes

There are five classes in an inheritance diagram, four of which represent synchronous servers of four types:

+------------+
| BaseServer |
+------------+
      |
      v
+-----------+        +------------------+
| TCPServer |------->| UnixStreamServer |
+-----------+        +------------------+
      |
      v
+-----------+        +--------------------+
| UDPServer |------->| UnixDatagramServer |
+-----------+        +--------------------+

Note that UnixDatagramServer derives from UDPServer, not from UnixStreamServer — the only difference between an IP and a Unix stream server is the address family, which is simply repeated in both Unix server classes.

class socketserver.ForkingMixIn

class socketserver.ThreadingMixIn

Forking and threading versions of each type of server can be created using these mix-in classes. For instance, ThreadingUDPServer is created as follows:

class ThreadingUDPServer(ThreadingMixIn, UDPServer):
    pass

The mix-in class comes first, since it overrides a method defined in UDPServer. Setting the various attributes also changes the behavior of the underlying server mechanism.

ForkingMixIn and the Forking classes mentioned below are only available on POSIX platforms that support fork().

class socketserver.ForkingTCPServer

class socketserver.ForkingUDPServer

class socketserver.ThreadingTCPServer

class socketserver.ThreadingUDPServer

These classes are pre-defined using the mix-in classes.

To implement a service, you must derive a class from BaseRequestHandler and redefine its handle() method. You can then run various versions of the service by combining one of the server classes with your request handler class. The request handler class must be different for datagram or stream services. This can be hidden by using the handler subclasses StreamRequestHandler or DatagramRequestHandler.

Of course, you still have to use your head! For instance, it makes no sense to use a forking server if the service contains state in memory that can be modified by different requests, since the modifications in the child process would never reach the initial state kept in the parent process and passed to each child. In this case, you can use a threading server, but you will probably have to use locks to protect the integrity of the shared data.

On the other hand, if you are building an HTTP server where all data is stored externally (for instance, in the file system), a synchronous class will essentially render the service “deaf” while one request is being handled – which may be for a very long time if a client is slow to receive all the data it has requested. Here a threading or forking server is appropriate.

In some cases, it may be appropriate to process part of a request synchronously, but to finish processing in a forked child depending on the request data. This can be implemented by using a synchronous server and doing an explicit fork in the request handler class handle() method.

Another approach to handling multiple simultaneous requests in an environment that supports neither threads nor fork() (or where these are too expensive or inappropriate for the service) is to maintain an explicit table of partially finished requests and to use selectors to decide which request to work on next (or whether to handle a new incoming request). This is particularly important for stream services where each client can potentially be connected for a long time (if threads or subprocesses cannot be used). See asyncore for another way to manage this.

21.21.2. Server 对象

class socketserver.BaseServer(server_address, RequestHandlerClass)

This is the superclass of all Server objects in the module. It defines the interface, given below, but does not implement most of the methods, which is done in subclasses. The two parameters are stored in the respective server_address and RequestHandlerClass attributes.

fileno()

Return an integer file descriptor for the socket on which the server is listening. This function is most commonly passed to selectors, to allow monitoring multiple servers in the same process.

handle_request()

Process a single request. This function calls the following methods in order: get_request(), verify_request(), and process_request(). If the user-provided handle() method of the handler class raises an exception, the server’s handle_error() method will be called. If no request is received within timeout seconds, handle_timeout() will be called and handle_request() will return.

serve_forever(poll_interval=0.5)

Handle requests until an explicit shutdown() request. Poll for shutdown every poll_interval seconds. Ignores the timeout attribute. It also calls service_actions(), which may be used by a subclass or mixin to provide actions specific to a given service. For example, the ForkingMixIn class uses service_actions() to clean up zombie child processes.

在 3.3 版更改: Added service_actions call to the serve_forever method.

service_actions()

This is called in the serve_forever() loop. This method can be overridden by subclasses or mixin classes to perform actions specific to a given service, such as cleanup actions.

3.3 新版功能.

shutdown()

Tell the serve_forever() loop to stop and wait until it does.

server_close()

Clean up the server. May be overridden.

address_family

The family of protocols to which the server’s socket belongs. Common examples are socket.AF_INET and socket.AF_UNIX.

RequestHandlerClass

The user-provided request handler class; an instance of this class is created for each request.

server_address

The address on which the server is listening. The format of addresses varies depending on the protocol family; see the documentation for the socket module for details. For Internet protocols, this is a tuple containing a string giving the address, and an integer port number: ('127.0.0.1', 80), for example.

socket

The socket object on which the server will listen for incoming requests.

The server classes support the following class variables:

allow_reuse_address

Whether the server will allow the reuse of an address. This defaults to False, and can be set in subclasses to change the policy.

request_queue_size

The size of the request queue. If it takes a long time to process a single request, any requests that arrive while the server is busy are placed into a queue, up to request_queue_size requests. Once the queue is full, further requests from clients will get a “Connection denied” error. The default value is usually 5, but this can be overridden by subclasses.

socket_type

The type of socket used by the server; socket.SOCK_STREAM and socket.SOCK_DGRAM are two common values.

timeout

Timeout duration, measured in seconds, or None if no timeout is desired. If handle_request() receives no incoming requests within the timeout period, the handle_timeout() method is called.

There are various server methods that can be overridden by subclasses of base server classes like TCPServer; these methods aren’t useful to external users of the server object.

finish_request(request, client_address)

Actually processes the request by instantiating RequestHandlerClass and calling its handle() method.

get_request()

Must accept a request from the socket, and return a 2-tuple containing the new socket object to be used to communicate with the client, and the client’s address.

handle_error(request, client_address)

This function is called if the handle() method of a RequestHandlerClass instance raises an exception. The default action is to print the traceback to standard error and continue handling further requests.

在 3.6 版更改: Now only called for exceptions derived from the Exception class.

handle_timeout()

This function is called when the timeout attribute has been set to a value other than None and the timeout period has passed with no requests being received. The default action for forking servers is to collect the status of any child processes that have exited, while in threading servers this method does nothing.

process_request(request, client_address)

Calls finish_request() to create an instance of the RequestHandlerClass. If desired, this function can create a new process or thread to handle the request; the ForkingMixIn and ThreadingMixIn classes do this.

server_activate()

Called by the server’s constructor to activate the server. The default behavior for a TCP server just invokes listen() on the server’s socket. May be overridden.

server_bind()

Called by the server’s constructor to bind the socket to the desired address. May be overridden.

verify_request(request, client_address)

Must return a Boolean value; if the value is True, the request will be processed, and if it’s False, the request will be denied. This function can be overridden to implement access controls for a server. The default implementation always returns True.

在 3.6 版更改: Support for the context manager protocol was added. Exiting the context manager is equivalent to calling server_close().

21.21.3. Request Handler Objects

class socketserver.BaseRequestHandler

This is the superclass of all request handler objects. It defines the interface, given below. A concrete request handler subclass must define a new handle() method, and can override any of the other methods. A new instance of the subclass is created for each request.

setup()

Called before the handle() method to perform any initialization actions required. The default implementation does nothing.

handle()

This function must do all the work required to service a request. The default implementation does nothing. Several instance attributes are available to it; the request is available as self.request; the client address as self.client_address; and the server instance as self.server, in case it needs access to per-server information.

The type of self.request is different for datagram or stream services. For stream services, self.request is a socket object; for datagram services, self.request is a pair of string and socket.

finish()

Called after the handle() method to perform any clean-up actions required. The default implementation does nothing. If setup() raises an exception, this function will not be called.

class socketserver.StreamRequestHandler

class socketserver.DatagramRequestHandler

These BaseRequestHandler subclasses override the setup() and finish() methods, and provide self.rfile and self.wfile attributes. The self.rfile and self.wfile attributes can be read or written, respectively, to get the request data or return data to the client.

The rfile attributes of both classes support the io.BufferedIOBase readable interface, and DatagramRequestHandler.wfile supports the io.BufferedIOBase writable interface.

在 3.6 版更改: StreamRequestHandler.wfile also supports the io.BufferedIOBase writable interface.

21.21.4. 例子

21.21.4.1. socketserver.TCPServer Example

This is the server side:

import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler):
    """
    The request handler class for our server.

    It is instantiated once per connection to the server, and must
    override the handle() method to implement communication to the
    client.
    """

    def handle(self):
        # self.request is the TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        print("{} wrote:".format(self.client_address[0]))
        print(self.data)
        # just send back the same data, but upper-cased
        self.request.sendall(self.data.upper())

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999

    # Create the server, binding to localhost on port 9999
    with socketserver.TCPServer((HOST, PORT), MyTCPHandler) as server:
        # Activate the server; this will keep running until you
        # interrupt the program with Ctrl-C
        server.serve_forever()

An alternative request handler class that makes use of streams (file-like objects that simplify communication by providing the standard file interface):

class MyTCPHandler(socketserver.StreamRequestHandler):

    def handle(self):
        # self.rfile is a file-like object created by the handler;
        # we can now use e.g. readline() instead of raw recv() calls
        self.data = self.rfile.readline().strip()
        print("{} wrote:".format(self.client_address[0]))
        print(self.data)
        # Likewise, self.wfile is a file-like object used to write back
        # to the client
        self.wfile.write(self.data.upper())

The difference is that the readline() call in the second handler will call recv() multiple times until it encounters a newline character, while the single recv() call in the first handler will just return what has been sent from the client in one sendall() call.

This is the client side:

import socket
import sys

HOST, PORT = "localhost", 9999
data = " ".join(sys.argv[1:])

# Create a socket (SOCK_STREAM means a TCP socket)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
    # Connect to server and send data
    sock.connect((HOST, PORT))
    sock.sendall(bytes(data + "\n", "utf-8"))

    # Receive data from the server and shut down
    received = str(sock.recv(1024), "utf-8")

print("Sent:     {}".format(data))
print("Received: {}".format(received))

The output of the example should look something like this:

Server:

$ python TCPServer.py
127.0.0.1 wrote:
b'hello world with TCP'
127.0.0.1 wrote:
b'python is nice'

Client:

$ python TCPClient.py hello world with TCP
Sent:     hello world with TCP
Received: HELLO WORLD WITH TCP
$ python TCPClient.py python is nice
Sent:     python is nice
Received: PYTHON IS NICE

21.21.4.2. socketserver.UDPServer Example

This is the server side:

import socketserver

class MyUDPHandler(socketserver.BaseRequestHandler):
    """
    This class works similar to the TCP handler class, except that
    self.request consists of a pair of data and client socket, and since
    there is no connection the client address must be given explicitly
    when sending data back via sendto().
    """

    def handle(self):
        data = self.request[0].strip()
        socket = self.request[1]
        print("{} wrote:".format(self.client_address[0]))
        print(data)
        socket.sendto(data.upper(), self.client_address)

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999
    with socketserver.UDPServer((HOST, PORT), MyUDPHandler) as server:
        server.serve_forever()

This is the client side:

import socket
import sys

HOST, PORT = "localhost", 9999
data = " ".join(sys.argv[1:])

# SOCK_DGRAM is the socket type to use for UDP sockets
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# As you can see, there is no connect() call; UDP has no connections.
# Instead, data is directly sent to the recipient via sendto().
sock.sendto(bytes(data + "\n", "utf-8"), (HOST, PORT))
received = str(sock.recv(1024), "utf-8")

print("Sent:     {}".format(data))
print("Received: {}".format(received))

The output of the example should look exactly like for the TCP server example.

21.21.4.3. Asynchronous Mixins

To build asynchronous handlers, use the ThreadingMixIn and ForkingMixIn classes.

An example for the ThreadingMixIn class:

import socket
import threading
import socketserver

class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):

    def handle(self):
        data = str(self.request.recv(1024), 'ascii')
        cur_thread = threading.current_thread()
        response = bytes("{}: {}".format(cur_thread.name, data), 'ascii')
        self.request.sendall(response)

class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
    pass

def client(ip, port, message):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.connect((ip, port))
        sock.sendall(bytes(message, 'ascii'))
        response = str(sock.recv(1024), 'ascii')
        print("Received: {}".format(response))

if __name__ == "__main__":
    # Port 0 means to select an arbitrary unused port
    HOST, PORT = "localhost", 0

    server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
    with server:
        ip, port = server.server_address

        # Start a thread with the server -- that thread will then start one
        # more thread for each request
        server_thread = threading.Thread(target=server.serve_forever)
        # Exit the server thread when the main thread terminates
        server_thread.daemon = True
        server_thread.start()
        print("Server loop running in thread:", server_thread.name)

        client(ip, port, "Hello World 1")
        client(ip, port, "Hello World 2")
        client(ip, port, "Hello World 3")

        server.shutdown()

The output of the example should look something like this:

$ python ThreadedTCPServer.py
Server loop running in thread: Thread-1
Received: Thread-2: Hello World 1
Received: Thread-3: Hello World 2
Received: Thread-4: Hello World 3

The ForkingMixIn class is used in the same way, except that the server will spawn a new process for each request. Available only on POSIX platforms that support fork().

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.22. http.server — HTTP 服务器

源代码： Lib/http/server.py [https://github.com/python/cpython/tree/3.6/Lib/http/server.py]

这个模块定义了实现 HTTP 服务器（ Web 服务器）的类。

警告

http.server is not recommended for production. It only implements only basic security checks.

HTTPServer 是 socketserver.TCPServer 的一个子类。它会创建和侦听 HTTP 套接字，并将请求调度给处理程序。用于创建和运行服务器的代码看起来像这样：

def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
    server_address = ('', 8000)
    httpd = server_class(server_address, handler_class)
    httpd.serve_forever()

class http.server.HTTPServer(server_address, RequestHandlerClass)

该类基于 TCPServer 类，并会将服务器地址存入名为 server_name 和 server_port 的实例变量中。服务器可被处理程序通过 server 实例变量访问。

The HTTPServer must be given a RequestHandlerClass on instantiation, of which this module provides three different variants:

class http.server.BaseHTTPRequestHandler(request, client_address, server)

这个类用于处理到达服务器的 HTTP 请求。 它本身无法响应任何实际的 HTTP 请求；它必须被子类化以处理每个请求方法（例如 GET 或 POST）。 BaseHTTPRequestHandler 提供了许多供子类使用的类和实例变量以及方法。

这个处理程序将解析请求和标头，然后调用特定请求类型对应的方法。 方法名称将根据请求来构造。 例如，对于请求方法 SPAM，将不带参数地调用 do_SPAM() 方法。 所有相关信息会被保存在该处理程序的实际变量中。 子类不需要重载或扩展 __init__() 方法。

BaseHTTPRequestHandler 具有下列实例变量:

client_address

包含 (host, port) 形式的指向客户端地址的元组。

server

包含服务器实例。

close_connection

应当在 handle_one_request() 返回之前设定的布尔值，指明是否要期待另一个请求，还是应当关闭连接。

requestline

包含 HTTP 请求行的字符串表示。 末尾的 CRLF 会被去除。 该属性应当由 handle_one_request() 来设定。 如果无有效请求行被处理，则它应当被设为空字符串。

command

包含具体的命令（请求类型）。 例如 'GET'。

path

包含请求路径。

request_version

包含请求的版本字符串。 例如 'HTTP/1.0'。

headers

Holds an instance of the class specified by the MessageClass class variable. This instance parses and manages the headers in the HTTP request. The parse_headers() function from http.client is used to parse the headers and it requires that the HTTP request provide a valid RFC 2822 [https://tools.ietf.org/html/rfc2822.html] style header.

rfile

An io.BufferedIOBase input stream, ready to read from the start of the optional input data.

wfile

Contains the output stream for writing a response back to the client. Proper adherence to the HTTP protocol must be used when writing to this stream in order to achieve successful interoperation with HTTP clients.

在 3.6 版更改: This is an io.BufferedIOBase stream.

BaseHTTPRequestHandler has the following attributes:

server_version

Specifies the server software version. You may want to override this. The format is multiple whitespace-separated strings, where each string is of the form name[/version]. For example, 'BaseHTTP/0.2'.

sys_version

Contains the Python system version, in a form usable by the version_string method and the server_version class variable. For example, 'Python/1.4'.

error_message_format

Specifies a format string that should be used by send_error() method for building an error response to the client. The string is filled by default with variables from responses based on the status code that passed to send_error().

error_content_type

Specifies the Content-Type HTTP header of error responses sent to the client. The default value is 'text/html'.

protocol_version

This specifies the HTTP protocol version used in responses. If set to 'HTTP/1.1', the server will permit HTTP persistent connections; however, your server must then include an accurate Content-Length header (using send_header()) in all of its responses to clients. For backwards compatibility, the setting defaults to 'HTTP/1.0'.

MessageClass

Specifies an email.message.Message-like class to parse HTTP headers. Typically, this is not overridden, and it defaults to http.client.HTTPMessage.

responses

This attribute contains a mapping of error code integers to two-element tuples containing a short and long message. For example, {code: (shortmessage, longmessage)}. The shortmessage is usually used as the message key in an error response, and longmessage as the explain key. It is used by send_response_only() and send_error() methods.

A BaseHTTPRequestHandler instance has the following methods:

handle()

Calls handle_one_request() once (or, if persistent connections are enabled, multiple times) to handle incoming HTTP requests. You should never need to override it; instead, implement appropriate do_*() methods.

handle_one_request()

This method will parse and dispatch the request to the appropriate do_*() method. You should never need to override it.

handle_expect_100()

When a HTTP/1.1 compliant server receives an Expect: 100-continue request header it responds back with a 100 Continue followed by 200 OK headers. This method can be overridden to raise an error if the server does not want the client to continue. For e.g. server can chose to send 417 Expectation Failed as a response header and return False.

3.2 新版功能.

send_error(code, message=None, explain=None)

Sends and logs a complete error reply to the client. The numeric code specifies the HTTP error code, with message as an optional, short, human readable description of the error. The explain argument can be used to provide more detailed information about the error; it will be formatted using the error_message_format attribute and emitted, after a complete set of headers, as the response body. The responses attribute holds the default values for message and explain that will be used if no value is provided; for unknown codes the default value for both is the string ???. The body will be empty if the method is HEAD or the response code is one of the following: 1xx, 204 No Content, 205 Reset Content, 304 Not Modified.

在 3.4 版更改: The error response includes a Content-Length header. Added the explain argument.

send_response(code, message=None)

Adds a response header to the headers buffer and logs the accepted request. The HTTP response line is written to the internal buffer, followed by Server and Date headers. The values for these two headers are picked up from the version_string() and date_time_string() methods, respectively. If the server does not intend to send any other headers using the send_header() method, then send_response() should be followed by an end_headers() call.

在 3.3 版更改: Headers are stored to an internal buffer and end_headers() needs to be called explicitly.

send_header(keyword, value)

Adds the HTTP header to an internal buffer which will be written to the output stream when either end_headers() or flush_headers() is invoked. keyword should specify the header keyword, with value specifying its value. Note that, after the send_header calls are done, end_headers() MUST BE called in order to complete the operation.

在 3.2 版更改: Headers are stored in an internal buffer.

send_response_only(code, message=None)

Sends the response header only, used for the purposes when 100 Continue response is sent by the server to the client. The headers not buffered and sent directly the output stream.If the message is not specified, the HTTP message corresponding the response code is sent.

3.2 新版功能.

end_headers()

Adds a blank line (indicating the end of the HTTP headers in the response) to the headers buffer and calls flush_headers().

在 3.2 版更改: The buffered headers are written to the output stream.

flush_headers()

Finally send the headers to the output stream and flush the internal headers buffer.

3.3 新版功能.

log_request(code='-', size='-')

Logs an accepted (successful) request. code should specify the numeric HTTP code associated with the response. If a size of the response is available, then it should be passed as the size parameter.

log_error(...)

Logs an error when a request cannot be fulfilled. By default, it passes the message to log_message(), so it takes the same arguments (format and additional values).

log_message(format, ...)

Logs an arbitrary message to sys.stderr. This is typically overridden to create custom error logging mechanisms. The format argument is a standard printf-style format string, where the additional arguments to log_message() are applied as inputs to the formatting. The client ip address and current date and time are prefixed to every message logged.

version_string()

Returns the server software’s version string. This is a combination of the server_version and sys_version attributes.

date_time_string(timestamp=None)

Returns the date and time given by timestamp (which must be None or in the format returned by time.time()), formatted for a message header. If timestamp is omitted, it uses the current date and time.

结果看起来像 'Sun, 06 Nov 1994 08:49:37 GMT'。

log_date_time_string()

返回当前的日期和时间，为日志格式化

address_string()

返回客户端的地址

在 3.3 版更改: Previously, a name lookup was performed. To avoid name resolution delays, it now always returns the IP address.

class http.server.SimpleHTTPRequestHandler(request, client_address, server)

这个类为当前目录及以下的文件提供服务，直接映射目录结构到HTTP请求

A lot of the work, such as parsing the request, is done by the base class BaseHTTPRequestHandler. This class implements the do_GET() and do_HEAD() functions.

The following are defined as class-level attributes of SimpleHTTPRequestHandler:

server_version

This will be "SimpleHTTP/" + __version__, where __version__ is defined at the module level.

extensions_map

A dictionary mapping suffixes into MIME types. The default is signified by an empty string, and is considered to be application/octet-stream. The mapping is used case-insensitively, and so should contain only lower-cased keys.

The SimpleHTTPRequestHandler class defines the following methods:

do_HEAD()

This method serves the 'HEAD' request type: it sends the headers it would send for the equivalent GET request. See the do_GET() method for a more complete explanation of the possible headers.

do_GET()

这一请求通过把其解释为当前工作目录的相对路径来映射到本地文件

If the request was mapped to a directory, the directory is checked for a file named index.html or index.htm (in that order). If found, the file’s contents are returned; otherwise a directory listing is generated by calling the list_directory() method. This method uses os.listdir() to scan the directory, and returns a 404 error response if the listdir() fails.

If the request was mapped to a file, it is opened and the contents are returned. Any OSError exception in opening the requested file is mapped to a 404, 'File not found' error. Otherwise, the content type is guessed by calling the guess_type() method, which in turn uses the extensions_map variable.

A 'Content-type:' header with the guessed content type is output, followed by a 'Content-Length:' header with the file’s size and a 'Last-Modified:' header with the file’s modification time.

Then follows a blank line signifying the end of the headers, and then the contents of the file are output. If the file’s MIME type starts with text/ the file is opened in text mode; otherwise binary mode is used.

For example usage, see the implementation of the test() function invocation in the http.server module.

The SimpleHTTPRequestHandler class can be used in the following manner in order to create a very basic webserver serving files relative to the current directory:

import http.server
import socketserver

PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

http.server can also be invoked directly using the -m switch of the interpreter with a port number argument. Similar to the previous example, this serves files relative to the current directory:

python -m http.server 8000

By default, server binds itself to all interfaces. The option -b/--bind specifies a specific address to which it should bind. For example, the following command causes the server to bind to localhost only:

python -m http.server 8000 --bind 127.0.0.1

3.4 新版功能: --bind argument was introduced.

class http.server.CGIHTTPRequestHandler(request, client_address, server)

This class is used to serve either files or output of CGI scripts from the current directory and below. Note that mapping HTTP hierarchic structure to local directory structure is exactly as in SimpleHTTPRequestHandler.

注解

CGI scripts run by the CGIHTTPRequestHandler class cannot execute redirects (HTTP code 302), because code 200 (script output follows) is sent prior to execution of the CGI script. This pre-empts the status code.

The class will however, run the CGI script, instead of serving it as a file, if it guesses it to be a CGI script. Only directory-based CGI are used — the other common server configuration is to treat special extensions as denoting CGI scripts.

The do_GET() and do_HEAD() functions are modified to run CGI scripts and serve the output, instead of serving files, if the request leads to somewhere below the cgi_directories path.

The CGIHTTPRequestHandler defines the following data member:

cgi_directories

This defaults to ['/cgi-bin', '/htbin'] and describes directories to treat as containing CGI scripts.

The CGIHTTPRequestHandler defines the following method:

do_POST()

This method serves the 'POST' request type, only allowed for CGI scripts. Error 501, “Can only POST to CGI scripts”, is output when trying to POST to a non-CGI url.

Note that CGI scripts will be run with UID of user nobody, for security reasons. Problems with the CGI script will be translated to error 403.

CGIHTTPRequestHandler can be enabled in the command line by passing the --cgi option:

python -m http.server --cgi 8000

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.23. http.cookies — HTTP状态管理

源代码: Lib/http/cookies.py [https://github.com/python/cpython/tree/3.6/Lib/http/cookies.py]

The http.cookies module defines classes for abstracting the concept of cookies, an HTTP state management mechanism. It supports both simple string-only cookies, and provides an abstraction for having any serializable data-type as cookie value.

The module formerly strictly applied the parsing rules described in the RFC 2109 [https://tools.ietf.org/html/rfc2109.html] and RFC 2068 [https://tools.ietf.org/html/rfc2068.html] specifications. It has since been discovered that MSIE 3.0x doesn’t follow the character rules outlined in those specs and also many current day browsers and servers have relaxed parsing rules when comes to Cookie handling. As a result, the parsing rules used are a bit less strict.

The character set, string.ascii_letters, string.digits and !#$%&'*+-.^_`|~: denote the set of valid characters allowed by this module in Cookie name (as key).

在 3.3 版更改: Allowed ‘:’ as a valid Cookie name character.

注解

On encountering an invalid cookie, CookieError is raised, so if your cookie data comes from a browser you should always prepare for invalid data and catch CookieError on parsing.

exception http.cookies.CookieError

Exception failing because of RFC 2109 [https://tools.ietf.org/html/rfc2109.html] invalidity: incorrect attributes, incorrect Set-Cookie header, etc.

class http.cookies.BaseCookie([input])

This class is a dictionary-like object whose keys are strings and whose values are Morsel instances. Note that upon setting a key to a value, the value is first converted to a Morsel containing the key and the value.

If input is given, it is passed to the load() method.

class http.cookies.SimpleCookie([input])

This class derives from BaseCookie and overrides value_decode() and value_encode() to be the identity and str() respectively.

参见

Module http.cookiejar

HTTP cookie handling for web clients. The http.cookiejar and http.cookies modules do not depend on each other.

RFC 2109 [https://tools.ietf.org/html/rfc2109.html] - HTTP State Management Mechanism

This is the state management specification implemented by this module.

21.23.1. Cookie 对象

BaseCookie.value_decode(val)

Return a decoded value from a string representation. Return value can be any type. This method does nothing in BaseCookie — it exists so it can be overridden.

BaseCookie.value_encode(val)

Return an encoded value. val can be any type, but return value must be a string. This method does nothing in BaseCookie — it exists so it can be overridden.

In general, it should be the case that value_encode() and value_decode() are inverses on the range of value_decode.

BaseCookie.output(attrs=None, header='Set-Cookie:', sep='\r\n')

Return a string representation suitable to be sent as HTTP headers. attrs and header are sent to each Morsel’s output() method. sep is used to join the headers together, and is by default the combination '\r\n' (CRLF).

BaseCookie.js_output(attrs=None)

Return an embeddable JavaScript snippet, which, if run on a browser which supports JavaScript, will act the same as if the HTTP headers was sent.

The meaning for attrs is the same as in output().

BaseCookie.load(rawdata)

If rawdata is a string, parse it as an HTTP_COOKIE and add the values found there as Morsels. If it is a dictionary, it is equivalent to:

for k, v in rawdata.items():
    cookie[k] = v

21.23.2. Morsel 对象

class http.cookies.Morsel

Abstract a key/value pair, which has some RFC 2109 [https://tools.ietf.org/html/rfc2109.html] attributes.

Morsels are dictionary-like objects, whose set of keys is constant — the valid RFC 2109 [https://tools.ietf.org/html/rfc2109.html] attributes, which are

expires

path

comment

domain

max-age

secure

version

httponly

The attribute httponly specifies that the cookie is only transferred in HTTP requests, and is not accessible through JavaScript. This is intended to mitigate some forms of cross-site scripting.

The keys are case-insensitive and their default value is ''.

在 3.5 版更改: __eq__() now takes key and value into account.

Morsel.value

Cookie的值。

3.5 版后已移除: assigning to value; use set() instead.

Morsel.coded_value

The encoded value of the cookie — this is what should be sent.

3.5 版后已移除: assigning to coded_value; use set() instead.

Morsel.key

The name of the cookie.

3.5 版后已移除: assigning to key; use set() instead.

Morsel.set(key, value, coded_value)

Set the key, value and coded_value attributes.

3.5 版后已移除: The undocumented LegalChars parameter is ignored and will be removed in a future version.

Morsel.isReservedKey(K)

Whether K is a member of the set of keys of a Morsel.

Morsel.output(attrs=None, header='Set-Cookie:')

Return a string representation of the Morsel, suitable to be sent as an HTTP header. By default, all the attributes are included, unless attrs is given, in which case it should be a list of attributes to use. header is by default "Set-Cookie:".

Morsel.js_output(attrs=None)

Return an embeddable JavaScript snippet, which, if run on a browser which supports JavaScript, will act the same as if the HTTP header was sent.

The meaning for attrs is the same as in output().

Morsel.OutputString(attrs=None)

Return a string representing the Morsel, without any surrounding HTTP or JavaScript.

The meaning for attrs is the same as in output().

Morsel.update(values)

Update the values in the Morsel dictionary with the values in the dictionary values. Raise an error if any of the keys in the values dict is not a valid RFC 2109 [https://tools.ietf.org/html/rfc2109.html] attribute.

在 3.5 版更改: an error is raised for invalid keys.

Morsel.copy(value)

Return a shallow copy of the Morsel object.

在 3.5 版更改: return a Morsel object instead of a dict.

Morsel.setdefault(key, value=None)

Raise an error if key is not a valid RFC 2109 [https://tools.ietf.org/html/rfc2109.html] attribute, otherwise behave the same as dict.setdefault().

21.23.3. 示例

The following example demonstrates how to use the http.cookies module.

>>> from http import cookies
>>> C = cookies.SimpleCookie()
>>> C["fig"] = "newton"
>>> C["sugar"] = "wafer"
>>> print(C) # generate HTTP headers
Set-Cookie: fig=newton
Set-Cookie: sugar=wafer
>>> print(C.output()) # same thing
Set-Cookie: fig=newton
Set-Cookie: sugar=wafer
>>> C = cookies.SimpleCookie()
>>> C["rocky"] = "road"
>>> C["rocky"]["path"] = "/cookie"
>>> print(C.output(header="Cookie:"))
Cookie: rocky=road; Path=/cookie
>>> print(C.output(attrs=[], header="Cookie:"))
Cookie: rocky=road
>>> C = cookies.SimpleCookie()
>>> C.load("chips=ahoy; vienna=finger") # load from a string (HTTP header)
>>> print(C)
Set-Cookie: chips=ahoy
Set-Cookie: vienna=finger
>>> C = cookies.SimpleCookie()
>>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
>>> print(C)
Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"
>>> C = cookies.SimpleCookie()
>>> C["oreo"] = "doublestuff"
>>> C["oreo"]["path"] = "/"
>>> print(C)
Set-Cookie: oreo=doublestuff; Path=/
>>> C = cookies.SimpleCookie()
>>> C["twix"] = "none for you"
>>> C["twix"].value
'none for you'
>>> C = cookies.SimpleCookie()
>>> C["number"] = 7 # equivalent to C["number"] = str(7)
>>> C["string"] = "seven"
>>> C["number"].value
'7'
>>> C["string"].value
'seven'
>>> print(C)
Set-Cookie: number=7
Set-Cookie: string=seven

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.24. http.cookiejar —— HTTP 客户端的 Cookie 处理

源代码： Lib/http/cookiejar.py [https://github.com/python/cpython/tree/3.6/Lib/http/cookiejar.py]

http.cookiejar 模块定义了用于自动处理 HTTP cookie 的类。这对访问需要小段数据 —— cookies 的网站很有用，这些数据由 Web 服务器的 HTTP 响应在客户端计算机上设置，然后在以后的 HTTP 请求中返回给服务器。

常规的 Netscape Cookie 协议和 RFC 2965 [https://tools.ietf.org/html/rfc2965.html] 定义的协议都可以处理。RFC 2965 处理默认情况下处于关闭状态。 RFC 2109 [https://tools.ietf.org/html/rfc2109.html] cookie 被解析为 Netscape cookie，随后根据有效的 ‘policy’ 被视为 Netscape 或 RFC 2965 cookie。 请注意，Internet 上的大多数 cookie 是 Netscape cookie。 http.cookiejar 尝试遵循事实上的 Netscape cookie 协议（与原始 Netscape 规范中所设定的协议大不相同），包括注意 max-age 和 port RFC 2965 引入的 cookie 属性。

注解

在 Set-Cookie 和 Set-Cookie2 头中找到的各种命名参数通常指 attributes。为了不与 Python 属性相混淆，模块文档使用 cookie-attribute 代替。

此模块定义了以下异常：

exception http.cookiejar.LoadError

FileCookieJar 实例在从文件加载 cookies 出错时抛出这个异常。 LoadError 是 OSError 的一个子类。

在 3.3 版更改: LoadError 成为 OSError 的子类而不是 IOError。

提供了以下类：

class http.cookiejar.CookieJar(policy=None)

policy 是实现了 CookiePolicy 接口的一个对象。

CookieJar 类储存 HTTP cookies。它从 HTTP 请求提取 cookies，并在 HTTP 响应中返回它们。 CookieJar 实例在必要时自动处理包含 cookie 的到期情况。子类还负责储存和从文件或数据库中查找 cookies。

class http.cookiejar.FileCookieJar(filename, delayload=None, policy=None)

policy 是实现了 CookiePolicy 接口的一个对象。对于其他参数，参考相应属性的文档。

一个可以从磁盘文件中加载或保存 cookie 的 CookieJar。 cookie 不会 从指定的文件中加载，直到调用 load() 或 revert() 方法。 该类的子类文档请参阅 FileCookieJar subclasses and co-operation with web browsers。

class http.cookiejar.CookiePolicy

此类负责确定是否应从服务器接受每个 cookie 或将其返回给服务器。

class http.cookiejar.DefaultCookiePolicy(blocked_domains=None, allowed_domains=None, netscape=True, rfc2965=False, rfc2109_as_netscape=None, hide_cookie2=False, strict_domain=False, strict_rfc2965_unverifiable=True, strict_ns_unverifiable=False, strict_ns_domain=DefaultCookiePolicy.DomainLiberal, strict_ns_set_initial_dollar=False, strict_ns_set_path=False)

构造器的参数应当只作为关键字参数传入。 blocked_domains 是一个域名序列，程序将绝不接受其 cookie 也绝不向其返回 cookie。 allowed_domains 如果不为 None，则也应是一个域名序列，程序将只对其中的域名接受和返回 cookie。 对于所有其他参数，请参阅 CookiePolicy 和 DefaultCookiePolicy 对象的文档。

DefaultCookiePolicy implements the standard accept / reject rules for Netscape and RFC 2965 [https://tools.ietf.org/html/rfc2965.html] cookies. By default, RFC 2109 [https://tools.ietf.org/html/rfc2109.html] cookies (ie. cookies received in a Set-Cookie header with a version cookie-attribute of 1) are treated according to the RFC 2965 rules. However, if RFC 2965 handling is turned off or rfc2109_as_netscape is True, RFC 2109 cookies are ‘downgraded’ by the CookieJar instance to Netscape cookies, by setting the version attribute of the Cookie instance to 0. DefaultCookiePolicy also provides some parameters to allow some fine-tuning of policy.

class http.cookiejar.Cookie

This class represents Netscape, RFC 2109 [https://tools.ietf.org/html/rfc2109.html] and RFC 2965 [https://tools.ietf.org/html/rfc2965.html] cookies. It is not expected that users of http.cookiejar construct their own Cookie instances. Instead, if necessary, call make_cookies() on a CookieJar instance.

参见

Module urllib.request

URL opening with automatic cookie handling.

Module http.cookies

HTTP cookie classes, principally useful for server-side code. The http.cookiejar and http.cookies modules do not depend on each other.

https://curl.haxx.se/rfc/cookie_spec.html

The specification of the original Netscape cookie protocol. Though this is still the dominant protocol, the ‘Netscape cookie protocol’ implemented by all the major browsers (and http.cookiejar) only bears a passing resemblance to the one sketched out in cookie_spec.html.

RFC 2109 [https://tools.ietf.org/html/rfc2109.html] - HTTP State Management Mechanism

Obsoleted by RFC 2965 [https://tools.ietf.org/html/rfc2965.html]. Uses Set-Cookie with version=1.

RFC 2965 [https://tools.ietf.org/html/rfc2965.html] - HTTP State Management Mechanism

The Netscape protocol with the bugs fixed. Uses Set-Cookie2 in place of Set-Cookie. Not widely used.

http://kristol.org/cookie/errata.html

Unfinished errata to RFC 2965 [https://tools.ietf.org/html/rfc2965.html].

RFC 2964 [https://tools.ietf.org/html/rfc2964.html] - Use of HTTP State Management

21.24.1. CookieJar 和 FileCookieJar 对象

CookieJar objects support the iterator protocol for iterating over contained Cookie objects.

CookieJar has the following methods:

CookieJar.add_cookie_header(request)

Add correct Cookie header to request.

If policy allows (ie. the rfc2965 and hide_cookie2 attributes of the CookieJar’s CookiePolicy instance are true and false respectively), the Cookie2 header is also added when appropriate.

The request object (usually a urllib.request..Request instance) must support the methods get_full_url(), get_host(), get_type(), unverifiable(), has_header(), get_header(), header_items(), add_unredirected_header() and origin_req_host attribute as documented by urllib.request.

在 3.3 版更改: request object needs origin_req_host attribute. Dependency on a deprecated method get_origin_req_host() has been removed.

CookieJar.extract_cookies(response, request)

Extract cookies from HTTP response and store them in the CookieJar, where allowed by policy.

The CookieJar will look for allowable Set-Cookie and Set-Cookie2 headers in the response argument, and store cookies as appropriate (subject to the CookiePolicy.set_ok() method’s approval).

The response object (usually the result of a call to urllib.request.urlopen(), or similar) should support an info() method, which returns an email.message.Message instance.

The request object (usually a urllib.request.Request instance) must support the methods get_full_url(), get_host(), unverifiable(), and origin_req_host attribute, as documented by urllib.request. The request is used to set default values for cookie-attributes as well as for checking that the cookie is allowed to be set.

在 3.3 版更改: request object needs origin_req_host attribute. Dependency on a deprecated method get_origin_req_host() has been removed.

CookieJar.set_policy(policy)

Set the CookiePolicy instance to be used.

CookieJar.make_cookies(response, request)

Return sequence of Cookie objects extracted from response object.

See the documentation for extract_cookies() for the interfaces required of the response and request arguments.

CookieJar.set_cookie_if_ok(cookie, request)

Set a Cookie if policy says it’s OK to do so.

CookieJar.set_cookie(cookie)

Set a Cookie, without checking with policy to see whether or not it should be set.

CookieJar.clear([domain[, path[, name]]])

Clear some cookies.

If invoked without arguments, clear all cookies. If given a single argument, only cookies belonging to that domain will be removed. If given two arguments, cookies belonging to the specified domain and URL path are removed. If given three arguments, then the cookie with the specified domain, path and name is removed.

Raises KeyError if no matching cookie exists.

CookieJar.clear_session_cookies()

Discard all session cookies.

Discards all contained cookies that have a true discard attribute (usually because they had either no max-age or expires cookie-attribute, or an explicit discard cookie-attribute). For interactive browsers, the end of a session usually corresponds to closing the browser window.

Note that the save() method won’t save session cookies anyway, unless you ask otherwise by passing a true ignore_discard argument.

FileCookieJar implements the following additional methods:

FileCookieJar.save(filename=None, ignore_discard=False, ignore_expires=False)

Save cookies to a file.

This base class raises NotImplementedError. Subclasses may leave this method unimplemented.

filename is the name of file in which to save cookies. If filename is not specified, self.filename is used (whose default is the value passed to the constructor, if any); if self.filename is None, ValueError is raised.

ignore_discard: save even cookies set to be discarded. ignore_expires: save even cookies that have expired

The file is overwritten if it already exists, thus wiping all the cookies it contains. Saved cookies can be restored later using the load() or revert() methods.

FileCookieJar.load(filename=None, ignore_discard=False, ignore_expires=False)

Load cookies from a file.

Old cookies are kept unless overwritten by newly loaded ones.

Arguments are as for save().

The named file must be in the format understood by the class, or LoadError will be raised. Also, OSError may be raised, for example if the file does not exist.

在 3.3 版更改: 过去触发的 IOError，现在是 OSError 的别名。

FileCookieJar.revert(filename=None, ignore_discard=False, ignore_expires=False)

Clear all cookies and reload cookies from a saved file.

revert() can raise the same exceptions as load(). If there is a failure, the object’s state will not be altered.

FileCookieJar instances have the following public attributes:

FileCookieJar.filename

Filename of default file in which to keep cookies. This attribute may be assigned to.

FileCookieJar.delayload

If true, load cookies lazily from disk. This attribute should not be assigned to. This is only a hint, since this only affects performance, not behaviour (unless the cookies on disk are changing). A CookieJar object may ignore it. None of the FileCookieJar classes included in the standard library lazily loads cookies.

21.24.2. FileCookieJar subclasses and co-operation with web browsers

The following CookieJar subclasses are provided for reading and writing.

class http.cookiejar.MozillaCookieJar(filename, delayload=None, policy=None)

A FileCookieJar that can load from and save cookies to disk in the Mozilla cookies.txt file format (which is also used by the Lynx and Netscape browsers).

注解

This loses information about RFC 2965 [https://tools.ietf.org/html/rfc2965.html] cookies, and also about newer or non-standard cookie-attributes such as port.

警告

Back up your cookies before saving if you have cookies whose loss / corruption would be inconvenient (there are some subtleties which may lead to slight changes in the file over a load / save round-trip).

Also note that cookies saved while Mozilla is running will get clobbered by Mozilla.

class http.cookiejar.LWPCookieJar(filename, delayload=None, policy=None)

A FileCookieJar that can load from and save cookies to disk in format compatible with the libwww-perl library’s Set-Cookie3 file format. This is convenient if you want to store cookies in a human-readable file.

21.24.3. CookiePolicy 对象

Objects implementing the CookiePolicy interface have the following methods:

CookiePolicy.set_ok(cookie, request)

Return boolean value indicating whether cookie should be accepted from server.

cookie is a Cookie instance. request is an object implementing the interface defined by the documentation for CookieJar.extract_cookies().

CookiePolicy.return_ok(cookie, request)

Return boolean value indicating whether cookie should be returned to server.

cookie is a Cookie instance. request is an object implementing the interface defined by the documentation for CookieJar.add_cookie_header().

CookiePolicy.domain_return_ok(domain, request)

Return false if cookies should not be returned, given cookie domain.

This method is an optimization. It removes the need for checking every cookie with a particular domain (which might involve reading many files). Returning true from domain_return_ok() and path_return_ok() leaves all the work to return_ok().

If domain_return_ok() returns true for the cookie domain, path_return_ok() is called for the cookie path. Otherwise, path_return_ok() and return_ok() are never called for that cookie domain. If path_return_ok() returns true, return_ok() is called with the Cookie object itself for a full check. Otherwise, return_ok() is never called for that cookie path.

Note that domain_return_ok() is called for every cookie domain, not just for the request domain. For example, the function might be called with both ".example.com" and "www.example.com" if the request domain is "www.example.com". The same goes for path_return_ok().

The request argument is as documented for return_ok().

CookiePolicy.path_return_ok(path, request)

Return false if cookies should not be returned, given cookie path.

See the documentation for domain_return_ok().

In addition to implementing the methods above, implementations of the CookiePolicy interface must also supply the following attributes, indicating which protocols should be used, and how. All of these attributes may be assigned to.

CookiePolicy.netscape

Implement Netscape protocol.

CookiePolicy.rfc2965

Implement RFC 2965 [https://tools.ietf.org/html/rfc2965.html] protocol.

CookiePolicy.hide_cookie2

Don’t add Cookie2 header to requests (the presence of this header indicates to the server that we understand RFC 2965 [https://tools.ietf.org/html/rfc2965.html] cookies).

The most useful way to define a CookiePolicy class is by subclassing from DefaultCookiePolicy and overriding some or all of the methods above. CookiePolicy itself may be used as a ‘null policy’ to allow setting and receiving any and all cookies (this is unlikely to be useful).

21.24.4. DefaultCookiePolicy 对象

Implements the standard rules for accepting and returning cookies.

Both RFC 2965 [https://tools.ietf.org/html/rfc2965.html] and Netscape cookies are covered. RFC 2965 handling is switched off by default.

The easiest way to provide your own policy is to override this class and call its methods in your overridden implementations before adding your own additional checks:

import http.cookiejar
class MyCookiePolicy(http.cookiejar.DefaultCookiePolicy):
    def set_ok(self, cookie, request):
        if not http.cookiejar.DefaultCookiePolicy.set_ok(self, cookie, request):
            return False
        if i_dont_want_to_store_this_cookie(cookie):
            return False
        return True

In addition to the features required to implement the CookiePolicy interface, this class allows you to block and allow domains from setting and receiving cookies. There are also some strictness switches that allow you to tighten up the rather loose Netscape protocol rules a little bit (at the cost of blocking some benign cookies).

A domain blacklist and whitelist is provided (both off by default). Only domains not in the blacklist and present in the whitelist (if the whitelist is active) participate in cookie setting and returning. Use the blocked_domains constructor argument, and blocked_domains() and set_blocked_domains() methods (and the corresponding argument and methods for allowed_domains). If you set a whitelist, you can turn it off again by setting it to None.

Domains in block or allow lists that do not start with a dot must equal the cookie domain to be matched. For example, "example.com" matches a blacklist entry of "example.com", but "www.example.com" does not. Domains that do start with a dot are matched by more specific domains too. For example, both "www.example.com" and "www.coyote.example.com" match ".example.com" (but "example.com" itself does not). IP addresses are an exception, and must match exactly. For example, if blocked_domains contains "192.168.1.2" and ".168.1.2", 192.168.1.2 is blocked, but 193.168.1.2 is not.

DefaultCookiePolicy implements the following additional methods:

DefaultCookiePolicy.blocked_domains()

Return the sequence of blocked domains (as a tuple).

DefaultCookiePolicy.set_blocked_domains(blocked_domains)

Set the sequence of blocked domains.

DefaultCookiePolicy.is_blocked(domain)

Return whether domain is on the blacklist for setting or receiving cookies.

DefaultCookiePolicy.allowed_domains()

Return None, or the sequence of allowed domains (as a tuple).

DefaultCookiePolicy.set_allowed_domains(allowed_domains)

Set the sequence of allowed domains, or None.

DefaultCookiePolicy.is_not_allowed(domain)

Return whether domain is not on the whitelist for setting or receiving cookies.

DefaultCookiePolicy instances have the following attributes, which are all initialised from the constructor arguments of the same name, and which may all be assigned to.

DefaultCookiePolicy.rfc2109_as_netscape

If true, request that the CookieJar instance downgrade RFC 2109 [https://tools.ietf.org/html/rfc2109.html] cookies (ie. cookies received in a Set-Cookie header with a version cookie-attribute of 1) to Netscape cookies by setting the version attribute of the Cookie instance to 0. The default value is None, in which case RFC 2109 cookies are downgraded if and only if RFC 2965 [https://tools.ietf.org/html/rfc2965.html] handling is turned off. Therefore, RFC 2109 cookies are downgraded by default.

General strictness switches:

DefaultCookiePolicy.strict_domain

Don’t allow sites to set two-component domains with country-code top-level domains like .co.uk, .gov.uk, .co.nz.etc. This is far from perfect and isn’t guaranteed to work!

RFC 2965 [https://tools.ietf.org/html/rfc2965.html] protocol strictness switches:

DefaultCookiePolicy.strict_rfc2965_unverifiable

Follow RFC 2965 [https://tools.ietf.org/html/rfc2965.html] rules on unverifiable transactions (usually, an unverifiable transaction is one resulting from a redirect or a request for an image hosted on another site). If this is false, cookies are never blocked on the basis of verifiability

Netscape protocol strictness switches:

DefaultCookiePolicy.strict_ns_unverifiable

Apply RFC 2965 [https://tools.ietf.org/html/rfc2965.html] rules on unverifiable transactions even to Netscape cookies.

DefaultCookiePolicy.strict_ns_domain

Flags indicating how strict to be with domain-matching rules for Netscape cookies. See below for acceptable values.

DefaultCookiePolicy.strict_ns_set_initial_dollar

Ignore cookies in Set-Cookie: headers that have names starting with '$'.

DefaultCookiePolicy.strict_ns_set_path

Don’t allow setting cookies whose path doesn’t path-match request URI.

strict_ns_domain is a collection of flags. Its value is constructed by or-ing together (for example, DomainStrictNoDots|DomainStrictNonDomain means both flags are set).

DefaultCookiePolicy.DomainStrictNoDots

When setting cookies, the ‘host prefix’ must not contain a dot (eg. www.foo.bar.com can’t set a cookie for .bar.com, because www.foo contains a dot).

DefaultCookiePolicy.DomainStrictNonDomain

Cookies that did not explicitly specify a domain cookie-attribute can only be returned to a domain equal to the domain that set the cookie (eg. spam.example.com won’t be returned cookies from example.com that had no domain cookie-attribute).

DefaultCookiePolicy.DomainRFC2965Match

When setting cookies, require a full RFC 2965 [https://tools.ietf.org/html/rfc2965.html] domain-match.

The following attributes are provided for convenience, and are the most useful combinations of the above flags:

DefaultCookiePolicy.DomainLiberal

Equivalent to 0 (ie. all of the above Netscape domain strictness flags switched off).

DefaultCookiePolicy.DomainStrict

Equivalent to DomainStrictNoDots|DomainStrictNonDomain.

21.24.5. Cookie 对象

Cookie instances have Python attributes roughly corresponding to the standard cookie-attributes specified in the various cookie standards. The correspondence is not one-to-one, because there are complicated rules for assigning default values, because the max-age and expires cookie-attributes contain equivalent information, and because RFC 2109 [https://tools.ietf.org/html/rfc2109.html] cookies may be ‘downgraded’ by http.cookiejar from version 1 to version 0 (Netscape) cookies.

Assignment to these attributes should not be necessary other than in rare circumstances in a CookiePolicy method. The class does not enforce internal consistency, so you should know what you’re doing if you do that.

Cookie.version

Integer or None. Netscape cookies have version 0. RFC 2965 [https://tools.ietf.org/html/rfc2965.html] and RFC 2109 [https://tools.ietf.org/html/rfc2109.html] cookies have a version cookie-attribute of 1. However, note that http.cookiejar may ‘downgrade’ RFC 2109 cookies to Netscape cookies, in which case version is 0.

Cookie.name

Cookie name (a string).

Cookie.value

Cookie value (a string), or None.

Cookie.port

String representing a port or a set of ports (eg. ‘80’, or ‘80,8080’), or None.

Cookie.path

Cookie path (a string, eg. '/acme/rocket_launchers').

Cookie.secure

True if cookie should only be returned over a secure connection.

Cookie.expires

Integer expiry date in seconds since epoch, or None. See also the is_expired() method.

Cookie.discard

True if this is a session cookie.

Cookie.comment

String comment from the server explaining the function of this cookie, or None.

Cookie.comment_url

URL linking to a comment from the server explaining the function of this cookie, or None.

Cookie.rfc2109

True if this cookie was received as an RFC 2109 [https://tools.ietf.org/html/rfc2109.html] cookie (ie. the cookie arrived in a Set-Cookie header, and the value of the Version cookie-attribute in that header was 1). This attribute is provided because http.cookiejar may ‘downgrade’ RFC 2109 cookies to Netscape cookies, in which case version is 0.

Cookie.port_specified

True if a port or set of ports was explicitly specified by the server (in the Set-Cookie / Set-Cookie2 header).

Cookie.domain_specified

True if a domain was explicitly specified by the server.

Cookie.domain_initial_dot

True if the domain explicitly specified by the server began with a dot ('.').

Cookies may have additional non-standard cookie-attributes. These may be accessed using the following methods:

Cookie.has_nonstandard_attr(name)

Return true if cookie has the named cookie-attribute.

Cookie.get_nonstandard_attr(name, default=None)

If cookie has the named cookie-attribute, return its value. Otherwise, return default.

Cookie.set_nonstandard_attr(name, value)

Set the value of the named cookie-attribute.

The Cookie class also defines the following method:

Cookie.is_expired(now=None)

True if cookie has passed the time at which the server requested it should expire. If now is given (in seconds since the epoch), return whether the cookie has expired at the specified time.

21.24.6. 例子

The first example shows the most common usage of http.cookiejar:

import http.cookiejar, urllib.request
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
r = opener.open("http://example.com/")

This example illustrates how to open a URL using your Netscape, Mozilla, or Lynx cookies (assumes Unix/Netscape convention for location of the cookies file):

import os, http.cookiejar, urllib.request
cj = http.cookiejar.MozillaCookieJar()
cj.load(os.path.join(os.path.expanduser("~"), ".netscape", "cookies.txt"))
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
r = opener.open("http://example.com/")

The next example illustrates the use of DefaultCookiePolicy. Turn on RFC 2965 [https://tools.ietf.org/html/rfc2965.html] cookies, be more strict about domains when setting and returning Netscape cookies, and block some domains from setting cookies or having them returned:

import urllib.request
from http.cookiejar import CookieJar, DefaultCookiePolicy
policy = DefaultCookiePolicy(
    rfc2965=True, strict_ns_domain=Policy.DomainStrict,
    blocked_domains=["ads.net", ".ads.net"])
cj = CookieJar(policy)
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
r = opener.open("http://example.com/")

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.25. xmlrpc — XMLRPC 服务端与客户端模块

XML-RPC 是一种远程过程调用方法，它使用通过 HTTP 传递的 XML 作为载体。 有了它，客户端可以在远程服务器上调用带参数的方法（服务器以 URI 命名）并获取结构化的数据。

xmlrpc 是一个集合了 XML-RPC 服务端与客户端实现模块的包。 这些模块是:

xmlrpc.client

xmlrpc.server

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.26. xmlrpc.client — XML-RPC client access

源代码: Lib/xmlrpc/client.py [https://github.com/python/cpython/tree/3.6/Lib/xmlrpc/client.py]

XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP(S) as a transport. With it, a client can call methods with parameters on a remote server (the server is named by a URI) and get back structured data. This module supports writing XML-RPC client code; it handles all the details of translating between conformable Python objects and XML on the wire.

警告

The xmlrpc.client module is not secure against maliciously constructed data. If you need to parse untrusted or unauthenticated data see XML 漏洞.

在 3.5 版更改: For HTTPS URIs, xmlrpc.client now performs all the necessary certificate and hostname checks by default.

class xmlrpc.client.ServerProxy(uri, transport=None, encoding=None, verbose=False, allow_none=False, use_datetime=False, use_builtin_types=False, *, context=None)

在 3.3 版更改: The use_builtin_types flag was added.

A ServerProxy instance is an object that manages communication with a remote XML-RPC server. The required first argument is a URI (Uniform Resource Indicator), and will normally be the URL of the server. The optional second argument is a transport factory instance; by default it is an internal SafeTransport instance for https: URLs and an internal HTTP Transport instance otherwise. The optional third argument is an encoding, by default UTF-8. The optional fourth argument is a debugging flag.

The following parameters govern the use of the returned proxy instance. If allow_none is true, the Python constant None will be translated into XML; the default behaviour is for None to raise a TypeError. This is a commonly-used extension to the XML-RPC specification, but isn’t supported by all clients and servers; see http://ontosys.com/xml-rpc/extensions.php [https://web.archive.org/web/20130120074804/http://ontosys.com/xml-rpc/extensions.php] for a description. The use_builtin_types flag can be used to cause date/time values to be presented as datetime.datetime objects and binary data to be presented as bytes objects; this flag is false by default. datetime.datetime, bytes and bytearray objects may be passed to calls. The obsolete use_datetime flag is similar to use_builtin_types but it applies only to date/time values.

Both the HTTP and HTTPS transports support the URL syntax extension for HTTP Basic Authentication: http://user:pass@host:port/path. The user:pass portion will be base64-encoded as an HTTP ‘Authorization’ header, and sent to the remote server as part of the connection process when invoking an XML-RPC method. You only need to use this if the remote server requires a Basic Authentication user and password. If an HTTPS URL is provided, context may be ssl.SSLContext and configures the SSL settings of the underlying HTTPS connection.

The returned instance is a proxy object with methods that can be used to invoke corresponding RPC calls on the remote server. If the remote server supports the introspection API, the proxy can also be used to query the remote server for the methods it supports (service discovery) and fetch other server-associated metadata.

Types that are conformable (e.g. that can be marshalled through XML), include the following (and except where noted, they are unmarshalled as the same Python type):

XML-RPC类型

Python 类型

boolean

bool

int, i1, i2, i4, i8 或者 biginteger

int 的范围从 -2147483648 到 2147483647。值将获得 <int> 标志。

double 或 float

float. 值将获得 <double> 标志.

string

str

array

list 或 tuple 包含整合元素。数组以 lists 形式返回。

struct

dict. Keys must be strings, values may be any conformable type. Objects of user-defined classes can be passed in; only their __dict__ attribute is transmitted.

dateTime.iso8601

DateTime 或 datetime.datetime。返回的类型取决于 use_builtin_types 和 use_datetime 标志的值。

base64

Binary, bytes 或 bytearray。返回的类型取决于 use_builtin_types 标志的值。

nil

None 常量。仅当 allow_none 为true时才允许传递。

bigdecimal

decimal.Decimal. 仅返回类型。

This is the full set of data types supported by XML-RPC. Method calls may also raise a special Fault instance, used to signal XML-RPC server errors, or ProtocolError used to signal an error in the HTTP/HTTPS transport layer. Both Fault and ProtocolError derive from a base class called Error. Note that the xmlrpc client module currently does not marshal instances of subclasses of built-in types.

When passing strings, characters special to XML such as <, >, and & will be automatically escaped. However, it’s the caller’s responsibility to ensure that the string is free of characters that aren’t allowed in XML, such as the control characters with ASCII values between 0 and 31 (except, of course, tab, newline and carriage return); failing to do this will result in an XML-RPC request that isn’t well-formed XML. If you have to pass arbitrary bytes via XML-RPC, use bytes or bytearray classes or the Binary wrapper class described below.

Server is retained as an alias for ServerProxy for backwards compatibility. New code should use ServerProxy.

在 3.5 版更改: Added the context argument.

在 3.6 版更改: Added support of type tags with prefixes (e.g. ex:nil). Added support of unmarshalling additional types used by Apache XML-RPC implementation for numerics: i1, i2, i8, biginteger, float and bigdecimal. See http://ws.apache.org/xmlrpc/types.html for a description.

参见

XML-RPC HOWTO [http://www.tldp.org/HOWTO/XML-RPC-HOWTO/index.html]

A good description of XML-RPC operation and client software in several languages. Contains pretty much everything an XML-RPC client developer needs to know.

XML-RPC Introspection [http://xmlrpc-c.sourceforge.net/introspection.html]

Describes the XML-RPC protocol extension for introspection.

XML-RPC Specification [http://xmlrpc.scripting.com/spec.html]

The official specification.

Unofficial XML-RPC Errata [http://effbot.org/zone/xmlrpc-errata.htm]

Fredrik Lundh’s “unofficial errata, intended to clarify certain details in the XML-RPC specification, as well as hint at ‘best practices’ to use when designing your own XML-RPC implementations.”

21.26.1. ServerProxy 对象

A ServerProxy instance has a method corresponding to each remote procedure call accepted by the XML-RPC server. Calling the method performs an RPC, dispatched by both name and argument signature (e.g. the same method name can be overloaded with multiple argument signatures). The RPC finishes by returning a value, which may be either returned data in a conformant type or a Fault or ProtocolError object indicating an error.

Servers that support the XML introspection API support some common methods grouped under the reserved system attribute:

ServerProxy.system.listMethods()

This method returns a list of strings, one for each (non-system) method supported by the XML-RPC server.

ServerProxy.system.methodSignature(name)

This method takes one parameter, the name of a method implemented by the XML-RPC server. It returns an array of possible signatures for this method. A signature is an array of types. The first of these types is the return type of the method, the rest are parameters.

Because multiple signatures (ie. overloading) is permitted, this method returns a list of signatures rather than a singleton.

Signatures themselves are restricted to the top level parameters expected by a method. For instance if a method expects one array of structs as a parameter, and it returns a string, its signature is simply “string, array”. If it expects three integers and returns a string, its signature is “string, int, int, int”.

If no signature is defined for the method, a non-array value is returned. In Python this means that the type of the returned value will be something other than list.

ServerProxy.system.methodHelp(name)

This method takes one parameter, the name of a method implemented by the XML-RPC server. It returns a documentation string describing the use of that method. If no such string is available, an empty string is returned. The documentation string may contain HTML markup.

在 3.5 版更改: Instances of ServerProxy support the context manager protocol for closing the underlying transport.

A working example follows. The server code:

from xmlrpc.server import SimpleXMLRPCServer

def is_even(n):
    return n % 2 == 0

server = SimpleXMLRPCServer(("localhost", 8000))
print("Listening on port 8000...")
server.register_function(is_even, "is_even")
server.serve_forever()

The client code for the preceding server:

import xmlrpc.client

with xmlrpc.client.ServerProxy("http://localhost:8000/") as proxy:
    print("3 is even: %s" % str(proxy.is_even(3)))
    print("100 is even: %s" % str(proxy.is_even(100)))

21.26.2. DateTime 对象

class xmlrpc.client.DateTime

This class may be initialized with seconds since the epoch, a time tuple, an ISO 8601 time/date string, or a datetime.datetime instance. It has the following methods, supported mainly for internal use by the marshalling/unmarshalling code:

decode(string)

Accept a string as the instance’s new time value.

encode(out)

Write the XML-RPC encoding of this DateTime item to the out stream object.

It also supports certain of Python’s built-in operators through rich comparison and __repr__() methods.

A working example follows. The server code:

import datetime
from xmlrpc.server import SimpleXMLRPCServer
import xmlrpc.client

def today():
    today = datetime.datetime.today()
    return xmlrpc.client.DateTime(today)

server = SimpleXMLRPCServer(("localhost", 8000))
print("Listening on port 8000...")
server.register_function(today, "today")
server.serve_forever()

The client code for the preceding server:

import xmlrpc.client
import datetime

proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")

today = proxy.today()
# convert the ISO8601 string to a datetime object
converted = datetime.datetime.strptime(today.value, "%Y%m%dT%H:%M:%S")
print("Today: %s" % converted.strftime("%d.%m.%Y, %H:%M"))

21.26.3. Binary 对象

class xmlrpc.client.Binary

This class may be initialized from bytes data (which may include NULs). The primary access to the content of a Binary object is provided by an attribute:

data

The binary data encapsulated by the Binary instance. The data is provided as a bytes object.

Binary objects have the following methods, supported mainly for internal use by the marshalling/unmarshalling code:

decode(bytes)

Accept a base64 bytes object and decode it as the instance’s new data.

encode(out)

Write the XML-RPC base 64 encoding of this binary item to the out stream object.

The encoded data will have newlines every 76 characters as per RFC 2045 section 6.8 [https://tools.ietf.org/html/rfc2045.html#section-6.8], which was the de facto standard base64 specification when the XML-RPC spec was written.

It also supports certain of Python’s built-in operators through __eq__() and __ne__() methods.

Example usage of the binary objects. We’re going to transfer an image over XMLRPC:

from xmlrpc.server import SimpleXMLRPCServer
import xmlrpc.client

def python_logo():
    with open("python_logo.jpg", "rb") as handle:
        return xmlrpc.client.Binary(handle.read())

server = SimpleXMLRPCServer(("localhost", 8000))
print("Listening on port 8000...")
server.register_function(python_logo, 'python_logo')

server.serve_forever()

The client gets the image and saves it to a file:

import xmlrpc.client

proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
with open("fetched_python_logo.jpg", "wb") as handle:
    handle.write(proxy.python_logo().data)

21.26.4. Fault 对象

class xmlrpc.client.Fault

A Fault object encapsulates the content of an XML-RPC fault tag. Fault objects have the following attributes:

faultCode

A string indicating the fault type.

faultString

A string containing a diagnostic message associated with the fault.

In the following example we’re going to intentionally cause a Fault by returning a complex type object. The server code:

from xmlrpc.server import SimpleXMLRPCServer

# A marshalling error is going to occur because we're returning a
# complex number
def add(x, y):
    return x+y+0j

server = SimpleXMLRPCServer(("localhost", 8000))
print("Listening on port 8000...")
server.register_function(add, 'add')

server.serve_forever()

The client code for the preceding server:

import xmlrpc.client

proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
try:
    proxy.add(2, 5)
except xmlrpc.client.Fault as err:
    print("A fault occurred")
    print("Fault code: %d" % err.faultCode)
    print("Fault string: %s" % err.faultString)

21.26.5. ProtocolError 对象

class xmlrpc.client.ProtocolError

A ProtocolError object describes a protocol error in the underlying transport layer (such as a 404 ‘not found’ error if the server named by the URI does not exist). It has the following attributes:

url

The URI or URL that triggered the error.

errcode

The error code.

errmsg

The error message or diagnostic string.

headers

A dict containing the headers of the HTTP/HTTPS request that triggered the error.

In the following example we’re going to intentionally cause a ProtocolError by providing an invalid URI:

import xmlrpc.client

# create a ServerProxy with a URI that doesn't respond to XMLRPC requests
proxy = xmlrpc.client.ServerProxy("http://google.com/")

try:
    proxy.some_method()
except xmlrpc.client.ProtocolError as err:
    print("A protocol error occurred")
    print("URL: %s" % err.url)
    print("HTTP/HTTPS headers: %s" % err.headers)
    print("Error code: %d" % err.errcode)
    print("Error message: %s" % err.errmsg)

21.26.6. MultiCall 对象

The MultiCall object provides a way to encapsulate multiple calls to a remote server into a single request 1.

class xmlrpc.client.MultiCall(server)

Create an object used to boxcar method calls. server is the eventual target of the call. Calls can be made to the result object, but they will immediately return None, and only store the call name and parameters in the MultiCall object. Calling the object itself causes all stored calls to be transmitted as a single system.multicall request. The result of this call is a generator; iterating over this generator yields the individual results.

A usage example of this class follows. The server code:

from xmlrpc.server import SimpleXMLRPCServer

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    return x // y

# A simple server with simple arithmetic functions
server = SimpleXMLRPCServer(("localhost", 8000))
print("Listening on port 8000...")
server.register_multicall_functions()
server.register_function(add, 'add')
server.register_function(subtract, 'subtract')
server.register_function(multiply, 'multiply')
server.register_function(divide, 'divide')
server.serve_forever()

The client code for the preceding server:

import xmlrpc.client

proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
multicall = xmlrpc.client.MultiCall(proxy)
multicall.add(7, 3)
multicall.subtract(7, 3)
multicall.multiply(7, 3)
multicall.divide(7, 3)
result = multicall()

print("7+3=%d, 7-3=%d, 7*3=%d, 7//3=%d" % tuple(result))

21.26.7. Convenience Functions

xmlrpc.client.dumps(params, methodname=None, methodresponse=None, encoding=None, allow_none=False)

Convert params into an XML-RPC request. or into a response if methodresponse is true. params can be either a tuple of arguments or an instance of the Fault exception class. If methodresponse is true, only a single value can be returned, meaning that params must be of length 1. encoding, if supplied, is the encoding to use in the generated XML; the default is UTF-8. Python’s None value cannot be used in standard XML-RPC; to allow using it via an extension, provide a true value for allow_none.

xmlrpc.client.loads(data, use_datetime=False, use_builtin_types=False)

Convert an XML-RPC request or response into Python objects, a (params, methodname). params is a tuple of argument; methodname is a string, or None if no method name is present in the packet. If the XML-RPC packet represents a fault condition, this function will raise a Fault exception. The use_builtin_types flag can be used to cause date/time values to be presented as datetime.datetime objects and binary data to be presented as bytes objects; this flag is false by default.

The obsolete use_datetime flag is similar to use_builtin_types but it applies only to date/time values.

在 3.3 版更改: The use_builtin_types flag was added.

21.26.8. Example of Client Usage

# simple test program (from the XML-RPC specification)
from xmlrpc.client import ServerProxy, Error

# server = ServerProxy("http://localhost:8000") # local server
with ServerProxy("http://betty.userland.com") as proxy:

    print(proxy)

    try:
        print(proxy.examples.getStateName(41))
    except Error as v:
        print("ERROR", v)

To access an XML-RPC server through a HTTP proxy, you need to define a custom transport. The following example shows how:

import http.client
import xmlrpc.client

class ProxiedTransport(xmlrpc.client.Transport):

    def set_proxy(self, host, port=None, headers=None):
        self.proxy = host, port
        self.proxy_headers = headers

    def make_connection(self, host):
        connection = http.client.HTTPConnection(*self.proxy)
        connection.set_tunnel(host, headers=self.proxy_headers)
        self._connection = host, connection
        return connection

transport = ProxiedTransport()
transport.set_proxy('proxy-server', 8080)
server = xmlrpc.client.ServerProxy('http://betty.userland.com', transport=transport)
print(server.examples.getStateName(41))

21.26.9. Example of Client and Server Usage

See SimpleXMLRPCServer Example.

备注

1

This approach has been first presented in a discussion on xmlrpc.com [https://web.archive.org/web/20060624230303/http://www.xmlrpc.com/discuss/msgReader$1208?mode=topic].

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.27. xmlrpc.server — Basic XML-RPC servers

Source code: Lib/xmlrpc/server.py [https://github.com/python/cpython/tree/3.6/Lib/xmlrpc/server.py]

The xmlrpc.server module provides a basic server framework for XML-RPC servers written in Python. Servers can either be free standing, using SimpleXMLRPCServer, or embedded in a CGI environment, using CGIXMLRPCRequestHandler.

警告

The xmlrpc.server module is not secure against maliciously constructed data. If you need to parse untrusted or unauthenticated data see XML 漏洞.

class xmlrpc.server.SimpleXMLRPCServer(addr, requestHandler=SimpleXMLRPCRequestHandler, logRequests=True, allow_none=False, encoding=None, bind_and_activate=True, use_builtin_types=False)

Create a new server instance. This class provides methods for registration of functions that can be called by the XML-RPC protocol. The requestHandler parameter should be a factory for request handler instances; it defaults to SimpleXMLRPCRequestHandler. The addr and requestHandler parameters are passed to the socketserver.TCPServer constructor. If logRequests is true (the default), requests will be logged; setting this parameter to false will turn off logging. The allow_none and encoding parameters are passed on to xmlrpc.client and control the XML-RPC responses that will be returned from the server. The bind_and_activate parameter controls whether server_bind() and server_activate() are called immediately by the constructor; it defaults to true. Setting it to false allows code to manipulate the allow_reuse_address class variable before the address is bound. The use_builtin_types parameter is passed to the loads() function and controls which types are processed when date/times values or binary data are received; it defaults to false.

在 3.3 版更改: The use_builtin_types flag was added.

class xmlrpc.server.CGIXMLRPCRequestHandler(allow_none=False, encoding=None, use_builtin_types=False)

Create a new instance to handle XML-RPC requests in a CGI environment. The allow_none and encoding parameters are passed on to xmlrpc.client and control the XML-RPC responses that will be returned from the server. The use_builtin_types parameter is passed to the loads() function and controls which types are processed when date/times values or binary data are received; it defaults to false.

在 3.3 版更改: The use_builtin_types flag was added.

class xmlrpc.server.SimpleXMLRPCRequestHandler

Create a new request handler instance. This request handler supports POST requests and modifies logging so that the logRequests parameter to the SimpleXMLRPCServer constructor parameter is honored.

21.27.1. SimpleXMLRPCServer Objects

The SimpleXMLRPCServer class is based on socketserver.TCPServer and provides a means of creating simple, stand alone XML-RPC servers.

SimpleXMLRPCServer.register_function(function, name=None)

Register a function that can respond to XML-RPC requests. If name is given, it will be the method name associated with function, otherwise function.__name__ will be used. name can be either a normal or Unicode string, and may contain characters not legal in Python identifiers, including the period character.

SimpleXMLRPCServer.register_instance(instance, allow_dotted_names=False)

Register an object which is used to expose method names which have not been registered using register_function(). If instance contains a _dispatch() method, it is called with the requested method name and the parameters from the request. Its API is def _dispatch(self, method, params) (note that params does not represent a variable argument list). If it calls an underlying function to perform its task, that function is called as func(*params), expanding the parameter list. The return value from _dispatch() is returned to the client as the result. If instance does not have a _dispatch() method, it is searched for an attribute matching the name of the requested method.

If the optional allow_dotted_names argument is true and the instance does not have a _dispatch() method, then if the requested method name contains periods, each component of the method name is searched for individually, with the effect that a simple hierarchical search is performed. The value found from this search is then called with the parameters from the request, and the return value is passed back to the client.

警告

Enabling the allow_dotted_names option allows intruders to access your module’s global variables and may allow intruders to execute arbitrary code on your machine. Only use this option on a secure, closed network.

SimpleXMLRPCServer.register_introspection_functions()

Registers the XML-RPC introspection functions system.listMethods, system.methodHelp and system.methodSignature.

SimpleXMLRPCServer.register_multicall_functions()

Registers the XML-RPC multicall function system.multicall.

SimpleXMLRPCRequestHandler.rpc_paths

An attribute value that must be a tuple listing valid path portions of the URL for receiving XML-RPC requests. Requests posted to other paths will result in a 404 “no such page” HTTP error. If this tuple is empty, all paths will be considered valid. The default value is ('/', '/RPC2').

21.27.1.1. SimpleXMLRPCServer Example

Server code:

from xmlrpc.server import SimpleXMLRPCServer
from xmlrpc.server import SimpleXMLRPCRequestHandler

# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
    rpc_paths = ('/RPC2',)

# Create server
with SimpleXMLRPCServer(("localhost", 8000),
                        requestHandler=RequestHandler) as server:
    server.register_introspection_functions()

    # Register pow() function; this will use the value of
    # pow.__name__ as the name, which is just 'pow'.
    server.register_function(pow)

    # Register a function under a different name
    def adder_function(x,y):
        return x + y
    server.register_function(adder_function, 'add')

    # Register an instance; all the methods of the instance are
    # published as XML-RPC methods (in this case, just 'mul').
    class MyFuncs:
        def mul(self, x, y):
            return x * y

    server.register_instance(MyFuncs())

    # Run the server's main loop
    server.serve_forever()

The following client code will call the methods made available by the preceding server:

import xmlrpc.client

s = xmlrpc.client.ServerProxy('http://localhost:8000')
print(s.pow(2,3))  # Returns 2**3 = 8
print(s.add(2,3))  # Returns 5
print(s.mul(5,2))  # Returns 5*2 = 10

# Print list of available methods
print(s.system.listMethods())

The following example included in the Lib/xmlrpc/server.py module shows a server allowing dotted names and registering a multicall function.

警告

Enabling the allow_dotted_names option allows intruders to access your module’s global variables and may allow intruders to execute arbitrary code on your machine. Only use this example only within a secure, closed network.

import datetime

class ExampleService:
    def getData(self):
        return '42'

    class currentTime:
        @staticmethod
        def getCurrentTime():
            return datetime.datetime.now()

with SimpleXMLRPCServer(("localhost", 8000)) as server:
    server.register_function(pow)
    server.register_function(lambda x,y: x+y, 'add')
    server.register_instance(ExampleService(), allow_dotted_names=True)
    server.register_multicall_functions()
    print('Serving XML-RPC on localhost port 8000')
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        print("\nKeyboard interrupt received, exiting.")
        sys.exit(0)

This ExampleService demo can be invoked from the command line:

python -m xmlrpc.server

The client that interacts with the above server is included in Lib/xmlrpc/client.py:

server = ServerProxy("http://localhost:8000")

try:
    print(server.currentTime.getCurrentTime())
except Error as v:
    print("ERROR", v)

multi = MultiCall(server)
multi.getData()
multi.pow(2,9)
multi.add(1,2)
try:
    for response in multi():
        print(response)
except Error as v:
    print("ERROR", v)

This client which interacts with the demo XMLRPC server can be invoked as:

python -m xmlrpc.client

21.27.2. CGIXMLRPCRequestHandler

The CGIXMLRPCRequestHandler class can be used to handle XML-RPC requests sent to Python CGI scripts.

CGIXMLRPCRequestHandler.register_function(function, name=None)

Register a function that can respond to XML-RPC requests. If name is given, it will be the method name associated with function, otherwise function.__name__ will be used. name can be either a normal or Unicode string, and may contain characters not legal in Python identifiers, including the period character.

CGIXMLRPCRequestHandler.register_instance(instance)

Register an object which is used to expose method names which have not been registered using register_function(). If instance contains a _dispatch() method, it is called with the requested method name and the parameters from the request; the return value is returned to the client as the result. If instance does not have a _dispatch() method, it is searched for an attribute matching the name of the requested method; if the requested method name contains periods, each component of the method name is searched for individually, with the effect that a simple hierarchical search is performed. The value found from this search is then called with the parameters from the request, and the return value is passed back to the client.

CGIXMLRPCRequestHandler.register_introspection_functions()

Register the XML-RPC introspection functions system.listMethods, system.methodHelp and system.methodSignature.

CGIXMLRPCRequestHandler.register_multicall_functions()

Register the XML-RPC multicall function system.multicall.

CGIXMLRPCRequestHandler.handle_request(request_text=None)

Handle an XML-RPC request. If request_text is given, it should be the POST data provided by the HTTP server, otherwise the contents of stdin will be used.

示例:

class MyFuncs:
    def mul(self, x, y):
        return x * y

handler = CGIXMLRPCRequestHandler()
handler.register_function(pow)
handler.register_function(lambda x,y: x+y, 'add')
handler.register_introspection_functions()
handler.register_instance(MyFuncs())
handler.handle_request()

21.27.3. Documenting XMLRPC server

These classes extend the above classes to serve HTML documentation in response to HTTP GET requests. Servers can either be free standing, using DocXMLRPCServer, or embedded in a CGI environment, using DocCGIXMLRPCRequestHandler.

class xmlrpc.server.DocXMLRPCServer(addr, requestHandler=DocXMLRPCRequestHandler, logRequests=True, allow_none=False, encoding=None, bind_and_activate=True, use_builtin_types=True)

Create a new server instance. All parameters have the same meaning as for SimpleXMLRPCServer; requestHandler defaults to DocXMLRPCRequestHandler.

在 3.3 版更改: The use_builtin_types flag was added.

class xmlrpc.server.DocCGIXMLRPCRequestHandler

Create a new instance to handle XML-RPC requests in a CGI environment.

class xmlrpc.server.DocXMLRPCRequestHandler

Create a new request handler instance. This request handler supports XML-RPC POST requests, documentation GET requests, and modifies logging so that the logRequests parameter to the DocXMLRPCServer constructor parameter is honored.

21.27.4. DocXMLRPCServer Objects

The DocXMLRPCServer class is derived from SimpleXMLRPCServer and provides a means of creating self-documenting, stand alone XML-RPC servers. HTTP POST requests are handled as XML-RPC method calls. HTTP GET requests are handled by generating pydoc-style HTML documentation. This allows a server to provide its own web-based documentation.

DocXMLRPCServer.set_server_title(server_title)

Set the title used in the generated HTML documentation. This title will be used inside the HTML “title” element.

DocXMLRPCServer.set_server_name(server_name)

Set the name used in the generated HTML documentation. This name will appear at the top of the generated documentation inside a “h1” element.

DocXMLRPCServer.set_server_documentation(server_documentation)

Set the description used in the generated HTML documentation. This description will appear as a paragraph, below the server name, in the documentation.

21.27.5. DocCGIXMLRPCRequestHandler

The DocCGIXMLRPCRequestHandler class is derived from CGIXMLRPCRequestHandler and provides a means of creating self-documenting, XML-RPC CGI scripts. HTTP POST requests are handled as XML-RPC method calls. HTTP GET requests are handled by generating pydoc-style HTML documentation. This allows a server to provide its own web-based documentation.

DocCGIXMLRPCRequestHandler.set_server_title(server_title)

Set the title used in the generated HTML documentation. This title will be used inside the HTML “title” element.

DocCGIXMLRPCRequestHandler.set_server_name(server_name)

Set the name used in the generated HTML documentation. This name will appear at the top of the generated documentation inside a “h1” element.

DocCGIXMLRPCRequestHandler.set_server_documentation(server_documentation)

Set the description used in the generated HTML documentation. This description will appear as a paragraph, below the server name, in the documentation.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

21.28. ipaddress — IPv4/IPv6 操作库

源代码： Lib/ipaddress.py [https://github.com/python/cpython/tree/3.6/Lib/ipaddress.py]

ipaddress 提供了创建、处理和操作 IPv4 和 IPv6 地址和网络的功能。

该模块中的函数和类可以直接处理与IP地址相关的各种任务，包括检查两个主机是否在同一个子网中，遍历某个子网中的所有主机，检查一个字符串是否是一个有效的IP地址或网络定义等等。

这是完整的模块 API 参考—若要查看概述，请见 ipaddress模块介绍.

3.3 新版功能.

21.28.1. 方便的工厂函数

ipaddress 模块提供来工厂函数来方便地创建 IP 地址，网络和接口：

ipaddress.ip_address(address)

返回一个 IPv4Address 或 IPv6Address 对象，取决于作为参数传递的 IP 地址。可以提供IPv4或IPv6地址，小于 2**32 的整数默认被认为是 IPv4。如果 address 不是有效的 IPv4 或 IPv6 地址，则会抛出 ValueError。

>>> ipaddress.ip_address('192.168.0.1')
IPv4Address('192.168.0.1')
>>> ipaddress.ip_address('2001:db8::')
IPv6Address('2001:db8::')

ipaddress.ip_network(address, strict=True)

返回一个 IPv4Network 或 IPv6Network 对象，具体取决于作为参数传入的 IP 地址。 address 是表示 IP 网址的字符串或整数。 可以提供 IPv4 或 IPv6 网址；小于 2**32 的整数默认被视为 IPv4。 strict 会被传给 IPv4Network 或 IPv6Network 构造器。 如果 address 不表示有效的 IPv4 或 IPv6 网址，或者网络设置了 host 比特位，则会引发 ValueError。

>>> ipaddress.ip_network('192.168.0.0/28')
IPv4Network('192.168.0.0/28')

ipaddress.ip_interface(address)

返回一个 IPv4Interface 或 IPv6Interface 对象，取决于作为参数传递的 IP 地址。 address 是代表 IP 地址的字符串或整数。 可以提供 IPv4 或 IPv6 地址，小于 2**32 的整数默认认为是 IPv4。 如果 address 不是有效的IPv4 或 IPv6 地址，则会抛出一个 ValueError。

这些方便的函数的一个缺点是需要同时处理IPv4和IPv6格式，这意味着提供的错误信息并不精准，因为函数不知道是打算采用IPv4还是IPv6格式。更详细的错误报告可以通过直接调用相应版本的类构造函数来获得。

21.28.2. IP 地址

21.28.2.1. 地址对象

IPv4Address 和 IPv6Address 对象有很多共同的属性。一些只对IPv6 地址有意义的属性也在 IPv4Address 对象实现，以便更容易编写正确处理两种 IP 版本的代码。地址对象是可哈希的 hashable，所以它们可以作为字典中的键来使用。

class ipaddress.IPv4Address(address)

构造一个 IPv4 地址。 如果 address 不是一个有效的 IPv4 地址，会抛出 AddressValueError 。

以下是有效的 IPv4 地址：

以十进制小数点表示的字符串，由四个十进制整数组成，范围为0–255，用点隔开(例如 192.168.0.1 )。每个整数代表地址中的八位（一个字节）。只有对于小于8的值，才允许使用前导零(因为对这种字符串的十进制和八进制解释之间没有任何歧义)。

一个32位可容纳的整数。

一个长度为 4 的封装在 bytes 对象中的整数(高位优先)。

>>> ipaddress.IPv4Address('192.168.0.1')
IPv4Address('192.168.0.1')
>>> ipaddress.IPv4Address(3232235521)
IPv4Address('192.168.0.1')
>>> ipaddress.IPv4Address(b'\xC0\xA8\x00\x01')
IPv4Address('192.168.0.1')

version

The appropriate version number: 4 for IPv4, 6 for IPv6.

max_prefixlen

The total number of bits in the address representation for this version: 32 for IPv4, 128 for IPv6.

The prefix defines the number of leading bits in an address that are compared to determine whether or not an address is part of a network.

compressed

exploded

The string representation in dotted decimal notation. Leading zeroes are never included in the representation.

As IPv4 does not define a shorthand notation for addresses with octets set to zero, these two attributes are always the same as str(addr) for IPv4 addresses. Exposing these attributes makes it easier to write display code that can handle both IPv4 and IPv6 addresses.

packed

The binary representation of this address - a bytes object of the appropriate length (most significant octet first). This is 4 bytes for IPv4 and 16 bytes for IPv6.

reverse_pointer

The name of the reverse DNS PTR record for the IP address, e.g.:

>>> ipaddress.ip_address("127.0.0.1").reverse_pointer
'1.0.0.127.in-addr.arpa'
>>> ipaddress.ip_address("2001:db8::1").reverse_pointer
'1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa'

This is the name that could be used for performing a PTR lookup, not the resolved hostname itself.

3.5 新版功能.

is_multicast

True if the address is reserved for multicast use. See RFC 3171 [https://tools.ietf.org/html/rfc3171.html] (for IPv4) or RFC 2373 [https://tools.ietf.org/html/rfc2373.html] (for IPv6).

is_private

True if the address is allocated for private networks. See iana-ipv4-special-registry [https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml] (for IPv4) or iana-ipv6-special-registry [https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml] (for IPv6).

is_global

True if the address is allocated for public networks. See iana-ipv4-special-registry [https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml] (for IPv4) or iana-ipv6-special-registry [https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml] (for IPv6).

3.4 新版功能.

is_unspecified

True if the address is unspecified. See RFC 5735 [https://tools.ietf.org/html/rfc5735.html] (for IPv4) or RFC 2373 [https://tools.ietf.org/html/rfc2373.html] (for IPv6).

is_reserved

True if the address is otherwise IETF reserved.

is_loopback

True if this is a loopback address. See RFC 3330 [https://tools.ietf.org/html/rfc3330.html] (for IPv4) or RFC 2373 [https://tools.ietf.org/html/rfc2373.html] (for IPv6).

is_link_local

True if the address is reserved for link-local usage. See RFC 3927 [https://tools.ietf.org/html/rfc3927.html].

class ipaddress.IPv6Address(address)

Construct an IPv6 address. An AddressValueError is raised if address is not a valid IPv6 address.

The following constitutes a valid IPv6 address:

A string consisting of eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons. This describes an exploded (longhand) notation. The string can also be compressed (shorthand notation) by various means. See RFC 4291 [https://tools.ietf.org/html/rfc4291.html] for details. For example, "0000:0000:0000:0000:0000:0abc:0007:0def" can be compressed to "::abc:7:def".

An integer that fits into 128 bits.

An integer packed into a bytes object of length 16, big-endian.

>>> ipaddress.IPv6Address('2001:db8::1000')
IPv6Address('2001:db8::1000')

compressed

The short form of the address representation, with leading zeroes in groups omitted and the longest sequence of groups consisting entirely of zeroes collapsed to a single empty group.

This is also the value returned by str(addr) for IPv6 addresses.

exploded

The long form of the address representation, with all leading zeroes and groups consisting entirely of zeroes included.

For the following attributes, see the corresponding documentation of the IPv4Address class:

packed

reverse_pointer

version

max_prefixlen

is_multicast

is_private

is_global

is_unspecified

is_reserved

is_loopback

is_link_local

3.4 新版功能: is_global

is_site_local

True if the address is reserved for site-local usage. Note that the site-local address space has been deprecated by RFC 3879 [https://tools.ietf.org/html/rfc3879.html]. Use is_private to test if this address is in the space of unique local addresses as defined by RFC 4193 [https://tools.ietf.org/html/rfc4193.html].

ipv4_mapped

For addresses that appear to be IPv4 mapped addresses (starting with ::FFFF/96), this property will report the embedded IPv4 address. For any other address, this property will be None.

sixtofour

For addresses that appear to be 6to4 addresses (starting with 2002::/16) as defined by RFC 3056 [https://tools.ietf.org/html/rfc3056.html], this property will report the embedded IPv4 address. For any other address, this property will be None.

teredo

For addresses that appear to be Teredo addresses (starting with 2001::/32) as defined by RFC 4380 [https://tools.ietf.org/html/rfc4380.html], this property will report the embedded (server, client) IP address pair. For any other address, this property will be None.

21.28.2.2. Conversion to Strings and Integers

To interoperate with networking interfaces such as the socket module, addresses must be converted to strings or integers. This is handled using the str() and int() builtin functions:

>>> str(ipaddress.IPv4Address('192.168.0.1'))
'192.168.0.1'
>>> int(ipaddress.IPv4Address('192.168.0.1'))
3232235521
>>> str(ipaddress.IPv6Address('::1'))
'::1'
>>> int(ipaddress.IPv6Address('::1'))
1

21.28.2.3. 运算符

Address objects support some operators. Unless stated otherwise, operators can only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with IPv6).

21.28.2.3.1. Comparison operators

Address objects can be compared with the usual set of comparison operators. Some examples:

>>> IPv4Address('127.0.0.2') > IPv4Address('127.0.0.1')
True
>>> IPv4Address('127.0.0.2') == IPv4Address('127.0.0.1')
False
>>> IPv4Address('127.0.0.2') != IPv4Address('127.0.0.1')
True

21.28.2.3.2. Arithmetic operators

Integers can be added to or subtracted from address objects. Some examples:

>>> IPv4Address('127.0.0.2') + 3
IPv4Address('127.0.0.5')
>>> IPv4Address('127.0.0.2') - 3
IPv4Address('126.255.255.255')
>>> IPv4Address('255.255.255.255') + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ipaddress.AddressValueError: 4294967296 (>= 2**32) is not permitted as an IPv4 address

21.28.3. IP Network definitions

The IPv4Network and IPv6Network objects provide a mechanism for defining and inspecting IP network definitions. A network definition consists of a mask and a network address, and as such defines a range of IP addresses that equal the network address when masked (binary AND) with the mask. For example, a network definition with the mask 255.255.255.0 and the network address 192.168.1.0 consists of IP addresses in the inclusive range 192.168.1.0 to 192.168.1.255.

21.28.3.1. Prefix, net mask and host mask

There are several equivalent ways to specify IP network masks. A prefix /<nbits> is a notation that denotes how many high-order bits are set in the network mask. A net mask is an IP address with some number of high-order bits set. Thus the prefix /24 is equivalent to the net mask 255.255.255.0 in IPv4, or ffff:ff00:: in IPv6. In addition, a host mask is the logical inverse of a net mask, and is sometimes used (for example in Cisco access control lists) to denote a network mask. The host mask equivalent to /24 in IPv4 is 0.0.0.255.

21.28.3.2. Network objects

All attributes implemented by address objects are implemented by network objects as well. In addition, network objects implement additional attributes. All of these are common between IPv4Network and IPv6Network, so to avoid duplication they are only documented for IPv4Network. Network objects are hashable, so they can be used as keys in dictionaries.

class ipaddress.IPv4Network(address, strict=True)

Construct an IPv4 network definition. address can be one of the following:

A string consisting of an IP address and an optional mask, separated by a slash (/). The IP address is the network address, and the mask can be either a single number, which means it’s a prefix, or a string representation of an IPv4 address. If it’s the latter, the mask is interpreted as a net mask if it starts with a non-zero field, or as a host mask if it starts with a zero field, with the single exception of an all-zero mask which is treated as a net mask. If no mask is provided, it’s considered to be /32.

For example, the following address specifications are equivalent: 192.168.1.0/24, 192.168.1.0/255.255.255.0 and 192.168.1.0/0.0.0.255.

An integer that fits into 32 bits. This is equivalent to a single-address network, with the network address being address and the mask being /32.

An integer packed into a bytes object of length 4, big-endian. The interpretation is similar to an integer address.

A two-tuple of an address description and a netmask, where the address description is either a string, a 32-bits integer, a 4-bytes packed integer, or an existing IPv4Address object; and the netmask is either an integer representing the prefix length (e.g. 24) or a string representing the prefix mask (e.g. 255.255.255.0).

An AddressValueError is raised if address is not a valid IPv4 address. A NetmaskValueError is raised if the mask is not valid for an IPv4 address.

If strict is True and host bits are set in the supplied address, then ValueError is raised. Otherwise, the host bits are masked out to determine the appropriate network address.

Unless stated otherwise, all network methods accepting other network/address objects will raise TypeError if the argument’s IP version is incompatible to self.

在 3.5 版更改: Added the two-tuple form for the address constructor parameter.

version

max_prefixlen

Refer to the corresponding attribute documentation in IPv4Address.

is_multicast

is_private

is_unspecified

is_reserved

is_loopback

is_link_local

These attributes are true for the network as a whole if they are true for both the network address and the broadcast address.

network_address

The network address for the network. The network address and the prefix length together uniquely define a network.

broadcast_address

The broadcast address for the network. Packets sent to the broadcast address should be received by every host on the network.

hostmask

The host mask, as an IPv4Address object.

netmask

The net mask, as an IPv4Address object.

with_prefixlen

compressed

exploded

A string representation of the network, with the mask in prefix notation.

with_prefixlen and compressed are always the same as str(network). exploded uses the exploded form the network address.

with_netmask

A string representation of the network, with the mask in net mask notation.

with_hostmask

A string representation of the network, with the mask in host mask notation.

num_addresses

The total number of addresses in the network.

prefixlen

Length of the network prefix, in bits.

hosts()

Returns an iterator over the usable hosts in the network. The usable hosts are all the IP addresses that belong to the network, except the network address itself and the network broadcast address. For networks with a mask length of 31, the network address and network broadcast address are also included in the result.

>>> list(ip_network('192.0.2.0/29').hosts())
[IPv4Address('192.0.2.1'), IPv4Address('192.0.2.2'),
 IPv4Address('192.0.2.3'), IPv4Address('192.0.2.4'),
 IPv4Address('192.0.2.5'), IPv4Address('192.0.2.6')]
>>> list(ip_network('192.0.2.0/31').hosts())
[IPv4Address('192.0.2.0'), IPv4Address('192.0.2.1')]

overlaps(other)

True if this network is partly or wholly contained in other or other is wholly contained in this network.

address_exclude(network)

Computes the network definitions resulting from removing the given network from this one. Returns an iterator of network objects. Raises ValueError if network is not completely contained in this network.

>>> n1 = ip_network('192.0.2.0/28')
>>> n2 = ip_network('192.0.2.1/32')
>>> list(n1.address_exclude(n2))
[IPv4Network('192.0.2.8/29'), IPv4Network('192.0.2.4/30'),
 IPv4Network('192.0.2.2/31'), IPv4Network('192.0.2.0/32')]

subnets(prefixlen_diff=1, new_prefix=None)

The subnets that join to make the current network definition, depending on the argument values. prefixlen_diff is the amount our prefix length should be increased by. new_prefix is the desired new prefix of the subnets; it must be larger than our prefix. One and only one of prefixlen_diff and new_prefix must be set. Returns an iterator of network objects.

>>> list(ip_network('192.0.2.0/24').subnets())
[IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]
>>> list(ip_network('192.0.2.0/24').subnets(prefixlen_diff=2))
[IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
 IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
>>> list(ip_network('192.0.2.0/24').subnets(new_prefix=26))
[IPv4Network('192.0.2.0/26'), IPv4Network('192.0.2.64/26'),
 IPv4Network('192.0.2.128/26'), IPv4Network('192.0.2.192/26')]
>>> list(ip_network('192.0.2.0/24').subnets(new_prefix=23))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    raise ValueError('new prefix must be longer')
ValueError: new prefix must be longer
>>> list(ip_network('192.0.2.0/24').subnets(new_prefix=25))
[IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/25')]

supernet(prefixlen_diff=1, new_prefix=None)

The supernet containing this network definition, depending on the argument values. prefixlen_diff is the amount our prefix length should be decreased by. new_prefix is the desired new prefix of the supernet; it must be smaller than our prefix. One and only one of prefixlen_diff and new_prefix must be set. Returns a single network object.

>>> ip_network('192.0.2.0/24').supernet()
IPv4Network('192.0.2.0/23')
>>> ip_network('192.0.2.0/24').supernet(prefixlen_diff=2)
IPv4Network('192.0.0.0/22')
>>> ip_network('192.0.2.0/24').supernet(new_prefix=20)
IPv4Network('192.0.0.0/20')

compare_networks(other)

Compare this network to other. In this comparison only the network addresses are considered; host bits aren’t. Returns either -1, 0 or 1.

>>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.2/32'))
-1
>>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.0/32'))
1
>>> ip_network('192.0.2.1/32').compare_networks(ip_network('192.0.2.1/32'))
0

class ipaddress.IPv6Network(address, strict=True)

Construct an IPv6 network definition. address can be one of the following:

A string consisting of an IP address and an optional prefix length, separated by a slash (/). The IP address is the network address, and the prefix length must be a single number, the prefix. If no prefix length is provided, it’s considered to be /128.

Note that currently expanded netmasks are not supported. That means 2001:db00::0/24 is a valid argument while 2001:db00::0/ffff:ff00:: not.

An integer that fits into 128 bits. This is equivalent to a single-address network, with the network address being address and the mask being /128.

An integer packed into a bytes object of length 16, big-endian. The interpretation is similar to an integer address.

A two-tuple of an address description and a netmask, where the address description is either a string, a 128-bits integer, a 16-bytes packed integer, or an existing IPv6Address object; and the netmask is an integer representing the prefix length.

An AddressValueError is raised if address is not a valid IPv6 address. A NetmaskValueError is raised if the mask is not valid for an IPv6 address.

If strict is True and host bits are set in the supplied address, then ValueError is raised. Otherwise, the host bits are masked out to determine the appropriate network address.

在 3.5 版更改: Added the two-tuple form for the address constructor parameter.

version

max_prefixlen

is_multicast

is_private

is_unspecified

is_reserved

is_loopback

is_link_local

network_address

broadcast_address

hostmask

netmask

with_prefixlen

compressed

exploded

with_netmask

with_hostmask

num_addresses

prefixlen

hosts()

Returns an iterator over the usable hosts in the network. The usable hosts are all the IP addresses that belong to the network, except the Subnet-Router anycast address. For networks with a mask length of 127, the Subnet-Router anycast address is also included in the result.

overlaps(other)

address_exclude(network)

subnets(prefixlen_diff=1, new_prefix=None)

supernet(prefixlen_diff=1, new_prefix=None)

compare_networks(other)

Refer to the corresponding attribute documentation in IPv4Network.

is_site_local

These attribute is true for the network as a whole if it is true for both the network address and the broadcast address.

21.28.3.3. 运算符

Network objects support some operators. Unless stated otherwise, operators can only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with IPv6).

21.28.3.3.1. Logical operators

Network objects can be compared with the usual set of logical operators. Network objects are ordered first by network address, then by net mask.

21.28.3.3.2. 迭代

Network objects can be iterated to list all the addresses belonging to the network. For iteration, all hosts are returned, including unusable hosts (for usable hosts, use the hosts() method). An example:

>>> for addr in IPv4Network('192.0.2.0/28'):
...     addr
...
IPv4Address('192.0.2.0')
IPv4Address('192.0.2.1')
IPv4Address('192.0.2.2')
IPv4Address('192.0.2.3')
IPv4Address('192.0.2.4')
IPv4Address('192.0.2.5')
IPv4Address('192.0.2.6')
IPv4Address('192.0.2.7')
IPv4Address('192.0.2.8')
IPv4Address('192.0.2.9')
IPv4Address('192.0.2.10')
IPv4Address('192.0.2.11')
IPv4Address('192.0.2.12')
IPv4Address('192.0.2.13')
IPv4Address('192.0.2.14')
IPv4Address('192.0.2.15')

21.28.3.3.3. Networks as containers of addresses

Network objects can act as containers of addresses. Some examples:

>>> IPv4Network('192.0.2.0/28')[0]
IPv4Address('192.0.2.0')
>>> IPv4Network('192.0.2.0/28')[15]
IPv4Address('192.0.2.15')
>>> IPv4Address('192.0.2.6') in IPv4Network('192.0.2.0/28')
True
>>> IPv4Address('192.0.3.6') in IPv4Network('192.0.2.0/28')
False

21.28.4. Interface objects

Interface objects are hashable, so they can be used as keys in dictionaries.

class ipaddress.IPv4Interface(address)

Construct an IPv4 interface. The meaning of address is as in the constructor of IPv4Network, except that arbitrary host addresses are always accepted.

IPv4Interface is a subclass of IPv4Address, so it inherits all the attributes from that class. In addition, the following attributes are available:

ip

The address (IPv4Address) without network information.

>>> interface = IPv4Interface('192.0.2.5/24')
>>> interface.ip
IPv4Address('192.0.2.5')

network

The network (IPv4Network) this interface belongs to.

>>> interface = IPv4Interface('192.0.2.5/24')
>>> interface.network
IPv4Network('192.0.2.0/24')

with_prefixlen

A string representation of the interface with the mask in prefix notation.

>>> interface = IPv4Interface('192.0.2.5/24')
>>> interface.with_prefixlen
'192.0.2.5/24'

with_netmask

A string representation of the interface with the network as a net mask.

>>> interface = IPv4Interface('192.0.2.5/24')
>>> interface.with_netmask
'192.0.2.5/255.255.255.0'

with_hostmask

A string representation of the interface with the network as a host mask.

>>> interface = IPv4Interface('192.0.2.5/24')
>>> interface.with_hostmask
'192.0.2.5/0.0.0.255'

class ipaddress.IPv6Interface(address)

Construct an IPv6 interface. The meaning of address is as in the constructor of IPv6Network, except that arbitrary host addresses are always accepted.

IPv6Interface is a subclass of IPv6Address, so it inherits all the attributes from that class. In addition, the following attributes are available:

ip

network

with_prefixlen

with_netmask

with_hostmask

Refer to the corresponding attribute documentation in IPv4Interface.

21.28.4.1. 运算符

Interface objects support some operators. Unless stated otherwise, operators can only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with IPv6).

21.28.4.1.1. Logical operators

Interface objects can be compared with the usual set of logical operators.

For equality comparison (== and !=), both the IP address and network must be the same for the objects to be equal. An interface will not compare equal to any address or network object.

For ordering (<, >, etc) the rules are different. Interface and address objects with the same IP version can be compared, and the address objects will always sort before the interface objects. Two interface objects are first compared by their networks and, if those are the same, then by their IP addresses.

21.28.5. Other Module Level Functions

The module also provides the following module level functions:

ipaddress.v4_int_to_packed(address)

Represent an address as 4 packed bytes in network (big-endian) order. address is an integer representation of an IPv4 IP address. A ValueError is raised if the integer is negative or too large to be an IPv4 IP address.

>>> ipaddress.ip_address(3221225985)
IPv4Address('192.0.2.1')
>>> ipaddress.v4_int_to_packed(3221225985)
b'\xc0\x00\x02\x01'

ipaddress.v6_int_to_packed(address)

Represent an address as 16 packed bytes in network (big-endian) order. address is an integer representation of an IPv6 IP address. A ValueError is raised if the integer is negative or too large to be an IPv6 IP address.

ipaddress.summarize_address_range(first, last)

Return an iterator of the summarized network range given the first and last IP addresses. first is the first IPv4Address or IPv6Address in the range and last is the last IPv4Address or IPv6Address in the range. A TypeError is raised if first or last are not IP addresses or are not of the same version. A ValueError is raised if last is not greater than first or if first address version is not 4 or 6.

>>> [ipaddr for ipaddr in ipaddress.summarize_address_range(
...    ipaddress.IPv4Address('192.0.2.0'),
...    ipaddress.IPv4Address('192.0.2.130'))]
[IPv4Network('192.0.2.0/25'), IPv4Network('192.0.2.128/31'), IPv4Network('192.0.2.130/32')]

ipaddress.collapse_addresses(addresses)

Return an iterator of the collapsed IPv4Network or IPv6Network objects. addresses is an iterator of IPv4Network or IPv6Network objects. A TypeError is raised if addresses contains mixed version objects.

>>> [ipaddr for ipaddr in
... ipaddress.collapse_addresses([ipaddress.IPv4Network('192.0.2.0/25'),
... ipaddress.IPv4Network('192.0.2.128/25')])]
[IPv4Network('192.0.2.0/24')]

ipaddress.get_mixed_type_key(obj)

Return a key suitable for sorting between networks and addresses. Address and Network objects are not sortable by default; they’re fundamentally different, so the expression:

IPv4Address('192.0.2.0') <= IPv4Network('192.0.2.0/24')

doesn’t make sense. There are some times however, where you may wish to have ipaddress sort these anyway. If you need to do this, you can use this function as the key argument to sorted().

obj is either a network or address object.

21.28.6. Custom Exceptions

To support more specific error reporting from class constructors, the module defines the following exceptions:

exception ipaddress.AddressValueError(ValueError)

Any value error related to the address.

exception ipaddress.NetmaskValueError(ValueError)

Any value error related to the net mask.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

21. 互联网协议和支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

22. 多媒体服务

本章描述的模块实现了主要用于多媒体应用的各种算法或接口。 它们可在安装时自行决定。 这是一个概述：

22.1. audioop — 处理原始音频数据

22.2. aifc — 读写 AIFF 和 AIFC 文件

22.3. sunau — 读写 Sun AU 文件

22.3.1. AU_read 对象

22.3.2. AU_write 对象

22.4. wave — 读写WAV格式文件

22.4.1. Wave_read对象

22.4.2. Wave_write 对象

22.5. chunk — 读取 IFF 分块数据

22.6. colorsys — 颜色系统间的转换

22.7. imghdr — 推测图像类型

22.8. sndhdr — 推测声音文件的类型

22.9. ossaudiodev — Access to OSS-compatible audio devices

22.9.1. Audio Device Objects

22.9.2. Mixer Device Objects

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

22.1. audioop — 处理原始音频数据

audioop 模块包含针对声音片段的一些有用操作。它操作的声音片段由 8、16、24 或 32 位宽的有符号整型样本组成，存储在 类字节串对象 中。除非特别说明，否则所有标量项目均为整数。

在 3.4 版更改: 增加了对 24 位样本的支持。现在，所有函数都接受任何 类字节串对象。而传入字符串会立即导致错误。

本模块提供对 a-LAW、u-LAW 和 Intel/DVI ADPCM 编码的支持。

部分更复杂的操作仅接受 16 位样本，而其他操作始终需要样本大小（以字节为单位）作为该操作的参数。

此模块定义了下列变量和函数：

exception audioop.error

所有错误都会抛出此异常，比如样本的字节数未知等等。

audioop.add(fragment1, fragment2, width)

两个样本作为参数传入，返回一个片段，该片段是两个样本的和。width 是样本宽度（以字节为单位），可以取 1, 2, 3 或 4。两个片段的长度应相同。如果发生溢出，较长的样本将被截断。

audioop.adpcm2lin(adpcmfragment, width, state)

将 Intel/DVI ADPCM 编码的片段解码为线性片段。关于 ADPCM 编码的详情请参阅 lin2adpcm() 的描述。返回一个元组 (sample, newstate)，其中 sample 的位宽由 width 指定。

audioop.alaw2lin(fragment, width)

将 a-LAW 编码的声音片段转换为线性编码声音片段。由于 a-LAW 编码样本始终为 8 位，因此这里的 width 仅指输出片段的样本位宽。

audioop.avg(fragment, width)

返回片段中所有样本的平均值。

audioop.avgpp(fragment, width)

返回片段中所有样本的平均峰峰值。由于没有进行过滤，因此该例程的实用性尚存疑。

audioop.bias(fragment, width, bias)

返回一个片段，该片段由原始片段中的每个样本加上偏差组成。在溢出时样本会回卷 (wrap around)。

audioop.byteswap(fragment, width)

“按字节交换”片段中的所有样本，返回修改后的片段。将大端序样本转换为小端序样本，反之亦然。

3.4 新版功能.

audioop.cross(fragment, width)

将片段作为参数传入，返回其中过零点的数量。

audioop.findfactor(fragment, reference)

返回一个系数 F 使得 rms(add(fragment, mul(reference, -F))) 最小，即返回乘以 reference 后最匹配 fragment 的那个乘数。两个片段都应包含 2 字节宽的样本。

本例程所需的时间与 len(fragment) 成正比。

audioop.findfit(fragment, reference)

尽可能尝试让 reference 匹配 fragment 的一部分（fragment 应较长）。从概念上讲，完成这些靠从 fragment 中取出切片，使用 findfactor() 计算最佳匹配，并最小化结果。两个片段都应包含 2 字节宽的样本。返回一个元组 (offset, factor)，其中 offset 是在 fragment 中的偏移量（整数），表示从此处开始最佳匹配，而 factor 是由 findfactor() 定义的因数（浮点数）。

audioop.findmax(fragment, length)

在 fragment 中搜索所有长度为 length 的样本切片（不是字节！）中，能量最大的那一个切片，即返回 i 使得 rms(fragment[i*2:(i+length)*2]) 最大。两个片段都应包含 2 字节宽的样本。

本例程所需的时间与 len(fragment) 成正比。

audioop.getsample(fragment, width, index)

返回片段中样本索引 index 的值。

audioop.lin2adpcm(fragment, width, state)

将样本转换为 4 位 Intel/DVI ADPCM 编码。ADPCM 编码是一种自适应编码方案，其中每个 4 比特数字是一个采样值与下一个采样值之间的差除以（不定的）步长。IMA 已选择使用 Intel/DVI ADPCM 算法，因此它很可能成为标准。

state 是一个表示编码器状态的元组。编码器返回一个元组 (adpcmfrag, newstate)，而 newstate 要在下一次调用 lin2adpcm() 时传入。在初始调用中，可以将 None 作为 state 传递。adpcmfrag 是 ADPCM 编码的片段，每个字节打包了 2 个 4 比特值。

audioop.lin2alaw(fragment, width)

将音频片段中的采样值转换为 a-LAW 编码，并将其作为字节对象返回。a-LAW 是一种音频编码格式，仅使用 8 位样本即可获得大约 13 位的动态范围。Sun 音频硬件等使用该编码。

audioop.lin2lin(fragment, width, newwidth)

将采样在 1、2、3 和 4 字节格式之间转换。

注解

在某些音频格式（如 .WAV 文件）中，16、24 和 32 位采样是有符号的，但 8 位采样是无符号的。因此，当将这些格式转换为 8 位宽采样时，还需使结果加上 128:

new_frames = audioop.lin2lin(frames, old_width, 1)
new_frames = audioop.bias(new_frames, 1, 128)

反之，将 8 位宽的采样转换为 16、24 或 32 位时，必须采用相同的处理。

audioop.lin2ulaw(fragment, width)

将音频片段中的采样值转换为 u-LAW 编码，并将其作为字节对象返回。u-LAW 是一种音频编码格式，仅使用 8 位采样即可获得大约 14 位的动态范围。Sun 音频硬件等使用该编码。

audioop.max(fragment, width)

返回片段中所有采样值的最大 绝对值。

audioop.maxpp(fragment, width)

返回声音片段中的最大峰峰值。

audioop.minmax(fragment, width)

返回声音片段中所有采样值的最小值和最大值组成的元组。

audioop.mul(fragment, width, factor)

返回一个片段，该片段由原始片段中的每个采样值乘以浮点值 factor 组成。如果发生溢出，采样将被截断。

audioop.ratecv(fragment, width, nchannels, inrate, outrate, state[, weightA[, weightB]])

转换输入片段的帧速率。

state 是一个表示转换器状态的元组。转换器返回一个元组 (newfragment, newstate)，而 newstate 要在下一次调用 ratecv() 时传入。初始调用应传入 None 作为 state。

参数 weightA 和 weightB 是简单数字滤波器的参数，默认分别为 1 和 0。

audioop.reverse(fragment, width)

将片段中的采样值反转，返回修改后的片段。

audioop.rms(fragment, width)

返回片段的均方根值，即 sqrt(sum(S_i^2)/n)。

测量音频信号的能量。

audioop.tomono(fragment, width, lfactor, rfactor)

将立体声片段转换为单声道片段。左通道乘以 lfactor，右通道乘以 rfactor，然后两个通道相加得到单声道信号。

audioop.tostereo(fragment, width, lfactor, rfactor)

由单声道片段生成立体声片段。立体声片段中的两对采样都是从单声道计算而来的，即左声道是乘以 lfactor，右声道是乘以 rfactor。

audioop.ulaw2lin(fragment, width)

将 u-LAW 编码的声音片段转换为线性编码声音片段。由于 u-LAW 编码采样值始终为 8 位，因此这里的 width 仅指输出片段的采样位宽。

请注意，诸如 mul() 或 max() 之类的操作在单声道和立体声间没有区别，即所有采样都作相同处理。如果出现问题，应先将立体声片段拆分为两个单声道片段，之后再重组。以下是如何进行该操作的示例:

def mul_stereo(sample, width, lfactor, rfactor):
    lsample = audioop.tomono(sample, width, 1, 0)
    rsample = audioop.tomono(sample, width, 0, 1)
    lsample = audioop.mul(lsample, width, lfactor)
    rsample = audioop.mul(rsample, width, rfactor)
    lsample = audioop.tostereo(lsample, width, 1, 0)
    rsample = audioop.tostereo(rsample, width, 0, 1)
    return audioop.add(lsample, rsample, width)

如果使用 ADPCM 编码器构造网络数据包，并且希望协议是无状态的（即能够容忍数据包丢失），则不仅需要传输数据，还应该传输状态。请注意，必须将*初始*状态（传入 lin2adpcm() 的状态）发送给解码器，不能发送最终状态（编码器返回的状态）。如果要使用 struct.Struct 以二进制保存状态，可以将第一个元素（预测值）用 16 位编码，将第二个元素（增量索引）用 8 位编码。

本 ADPCM 编码器从不与其他 ADPCM 编码器对立，仅针对自身。本开发者可能会误读标准，这种情况下它们将无法与相应标准互操作。

乍看之下 find*() 例程可能有些可笑。它们主要是用于回声消除，一种快速有效的方法是选取输出样本中能量最高的片段，在输入样本中定位该片段，然后从输入样本中减去整个输出样本:

def echocancel(outputdata, inputdata):
    pos = audioop.findmax(outputdata, 800)    # one tenth second
    out_test = outputdata[pos*2:]
    in_test = inputdata[pos*2:]
    ipos, factor = audioop.findfit(in_test, out_test)
    # Optional (for better cancellation):
    # factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],
    #              out_test)
    prefill = '\0'*(pos+ipos)*2
    postfill = '\0'*(len(inputdata)-len(prefill)-len(outputdata))
    outputdata = prefill + audioop.mul(outputdata, 2, -factor) + postfill
    return audioop.add(inputdata, outputdata, 2)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

22.2. aifc — 读写 AIFF 和 AIFC 文件

源代码： Lib/aifc.py [https://github.com/python/cpython/tree/3.6/Lib/aifc.py]

本模块提供读写 AIFF 和 AIFF-C 文件的支持。AIFF 是音频交换文件格式 (Audio Interchange File Format)，一种用于在文件中存储数字音频采样的格式。AIFF-C 是该格式的更新版本，其中包括压缩音频数据的功能。

音频文件内有许多参数，用于描述音频数据。采样率或帧率是每秒对声音采样的次数。通道数表示音频是单声道，双声道还是四声道。每个通道的每个帧包含一次采样。采样大小是以字节表示的每次采样的大小。因此，一帧由 nchannels * samplesize （通道数*采样大小）字节组成，而一秒钟的音频包含 nchannels * samplesize * framerate （通道数*采样大小*帧率）字节。

例如，CD 质量的音频采样大小为 2 字节（16位），使用 2 个声道（立体声），且帧速率为 44,100 帧/秒。这表示帧大小为 4 字节 (2*2)，一秒钟占用 2*2*44100 字节（176,400 字节）。

aifc 模块定义了以下函数：

aifc.open(file, mode=None)

Open an AIFF or AIFF-C file and return an object instance with methods that are described below. The argument file is either a string naming a file or a file object. mode must be 'r' or 'rb' when the file must be opened for reading, or 'w' or 'wb' when the file must be opened for writing. If omitted, file.mode is used if it exists, otherwise 'rb' is used. When used for writing, the file object should be seekable, unless you know ahead of time how many samples you are going to write in total and use writeframesraw() and setnframes(). The open() function may be used in a with statement. When the with block completes, the close() method is called.

在 3.4 版更改: 支持了 with 语句。

当打开文件用于读取时，由 open() 返回的对象有以下几种方法：

aifc.getnchannels()

返回音频的通道数（单声道为 1，立体声为 2）。

aifc.getsampwidth()

返回以字节表示的单个采样的大小。

aifc.getframerate()

返回采样率（每秒的音频帧数）。

aifc.getnframes()

返回文件中的音频帧总数。

aifc.getcomptype()

返回一个长度为 4 的字节数组，描述了音频文件中使用的压缩类型。对于 AIFF 文件，返回值为 b'NONE'。

aifc.getcompname()

返回一个字节数组，可转换为人类可读的描述，描述的是音频文件中使用的压缩类型。对于 AIFF 文件，返回值为 b'not compressed'。

aifc.getparams()

返回一个 namedtuple() (nchannels, sampwidth, framerate, nframes, comptype, compname)，与 get*() 方法的输出相同。

aifc.getmarkers()

返回一个列表，包含音频文件中的所有标记。标记由一个 3 元素的元组组成。第一个元素是标记 ID（整数），第二个是标记位置，从数据开头算起的帧数（整数），第三个是标记的名称（字符串）。

aifc.getmark(id)

根据传入的标记 id 返回元组，元组与 getmarkers() 中描述的一致。

aifc.readframes(nframes)

从音频文件读取并返回后续 nframes 个帧。返回的数据是一个字符串，包含每个帧所有通道的未压缩采样值。

aifc.rewind()

倒回读取指针。下一次 readframes() 将从头开始。

aifc.setpos(pos)

移动读取指针到指定的帧上。

aifc.tell()

返回当前的帧号。

aifc.close()

关闭 AIFF 文件。调用此方法后，对象将无法再使用。

打开文件用于写入时，open() 返回的对象具有上述所有方法，但 readframes() 和 setpos() 除外，并额外具备了以下方法。只有调用了 set*() 方法之后，才能调用相应的 get*() 方法。在首次调用 writeframes() 或 writeframesraw() 之前，必须填写除帧数以外的所有参数。

aifc.aiff()

创建一个 AIFF 文件，默认创建 AIFF-C 文件，除非文件名以 '.aiff' 为后缀，在此情况下默认创建 AIFF 文件。

aifc.aifc()

创建一个 AIFF-C 文件。 默认创建 AIFF-C 文件，除非文件名以 '.aiff' 为后缀，在此情况下默认创建 AIFF 文件。

aifc.setnchannels(nchannels)

指明音频文件中的通道数。

aifc.setsampwidth(width)

指明以字节为单位的音频采样大小。

aifc.setframerate(rate)

指明以每秒帧数表示的采样频率。

aifc.setnframes(nframes)

指明要写入到音频文件的帧数。 如果未设定此形参或者未正确设定，则文件需要支持位置查找。

aifc.setcomptype(type, name)

指明压缩类型。 如果未指明，则音频数据将不会被压缩。 在 AIFF 文件中，压缩是无法实现的。 name 参数应当为以字节数组表示的人类可读的压缩类型描述，type 参数应当为长度为 4 的字节数组。 目前支持的压缩类型如下: b'NONE', b'ULAW', b'ALAW', b'G722'。

aifc.setparams(nchannels, sampwidth, framerate, comptype, compname)

一次性设置上述所有参数。 该参数是由多个形参组成的元组。 这意味着可以使用 getparams() 调用的结果作为 setparams() 的参数。

aifc.setmark(id, pos, name)

添加具有给定 id (大于 0)，以及在给定位置上给定名称的标记。 此方法可在 close() 之前的任何时候被调用。

aifc.tell()

返回输出文件中的当前写入位置。 适用于与 setmark() 进行协同配合。

aifc.writeframes(data)

将数据写入到输出文件。 此方法只能在设置了音频文件形参之后被调用。

在 3.4 版更改: 现在可接受任意 bytes-like object。

aifc.writeframesraw(data)

类似于 writeframes()，不同之处在于音频文件的标头不会被更新。

在 3.4 版更改: 现在可接受任意 bytes-like object。

aifc.close()

关闭 AIFF 文件。 文件的标头会被更新以反映音频数据的实际大小。 在调用此方法之后，对象将无法再被使用。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

22.3. sunau — 读写 Sun AU 文件

源代码: Lib/sunau.py [https://github.com/python/cpython/tree/3.6/Lib/sunau.py]

sunau 模拟提供了一个处理 Sun AU 声音格式的便利接口。请注意此模块与 aifc 和 wave 是兼容接口的。

音频文件由标头和数据组成。标头的字段为：

域

目录

magic word

四个字节 .snd

header size

标头的大小，包括信息，以字节为单位。

data size

数据的物理大小，以字节为单位。

编码

指示音频样本的编码方式。

sample rate

采样率

# of channels

的通道数。

info

提供音频文件描述的ASCII字符串（用空字节填充）。

Apart from the info field, all header fields are 4 bytes in size. They are all 32-bit unsigned integers encoded in big-endian byte order.

The sunau module defines the following functions:

sunau.open(file, mode)

If file is a string, open the file by that name, otherwise treat it as a seekable file-like object. mode can be any of

'r'

只读模式。

'w'

只写模式。

Note that it does not allow read/write files.

A mode of 'r' returns an AU_read object, while a mode of 'w' or 'wb' returns an AU_write object.

sunau.openfp(file, mode)

同 open()，用于向后兼容。

The sunau module defines the following exception:

exception sunau.Error

An error raised when something is impossible because of Sun AU specs or implementation deficiency.

The sunau module defines the following data items:

sunau.AUDIO_FILE_MAGIC

An integer every valid Sun AU file begins with, stored in big-endian form. This is the string .snd interpreted as an integer.

sunau.AUDIO_FILE_ENCODING_MULAW_8

sunau.AUDIO_FILE_ENCODING_LINEAR_8

sunau.AUDIO_FILE_ENCODING_LINEAR_16

sunau.AUDIO_FILE_ENCODING_LINEAR_24

sunau.AUDIO_FILE_ENCODING_LINEAR_32

sunau.AUDIO_FILE_ENCODING_ALAW_8

Values of the encoding field from the AU header which are supported by this module.

sunau.AUDIO_FILE_ENCODING_FLOAT

sunau.AUDIO_FILE_ENCODING_DOUBLE

sunau.AUDIO_FILE_ENCODING_ADPCM_G721

sunau.AUDIO_FILE_ENCODING_ADPCM_G722

sunau.AUDIO_FILE_ENCODING_ADPCM_G723_3

sunau.AUDIO_FILE_ENCODING_ADPCM_G723_5

Additional known values of the encoding field from the AU header, but which are not supported by this module.

22.3.1. AU_read 对象

AU_read objects, as returned by open() above, have the following methods:

AU_read.close()

Close the stream, and make the instance unusable. (This is called automatically on deletion.)

AU_read.getnchannels()

Returns number of audio channels (1 for mono, 2 for stereo).

AU_read.getsampwidth()

返回采样字节长度。

AU_read.getframerate()

返回采样频率。

AU_read.getnframes()

返回音频总帧数。

AU_read.getcomptype()

Returns compression type. Supported compression types are 'ULAW', 'ALAW' and 'NONE'.

AU_read.getcompname()

Human-readable version of getcomptype(). The supported types have the respective names 'CCITT G.711 u-law', 'CCITT G.711 A-law' and 'not compressed'.

AU_read.getparams()

返回一个 namedtuple() (nchannels, sampwidth, framerate, nframes, comptype, compname)，与 get*() 方法的输出相同。

AU_read.readframes(n)

Reads and returns at most n frames of audio, as a bytes object. The data will be returned in linear format. If the original data is in u-LAW format, it will be converted.

AU_read.rewind()

重置文件指针至音频开头.

以下两个方法都使用指针，具体实现由其底层决定。

AU_read.setpos(pos)

Set the file pointer to the specified position. Only values returned from tell() should be used for pos.

AU_read.tell()

Return current file pointer position. Note that the returned value has nothing to do with the actual position in the file.

The following two functions are defined for compatibility with the aifc, and don’t do anything interesting.

AU_read.getmarkers()

返回 None。

AU_read.getmark(id)

引发错误异常。

22.3.2. AU_write 对象

AU_write objects, as returned by open() above, have the following methods:

AU_write.setnchannels(n)

设置声道数。

AU_write.setsampwidth(n)

Set the sample width (in bytes.)

在 3.4 版更改: Added support for 24-bit samples.

AU_write.setframerate(n)

Set the frame rate.

AU_write.setnframes(n)

Set the number of frames. This can be later changed, when and if more frames are written.

AU_write.setcomptype(type, name)

Set the compression type and description. Only 'NONE' and 'ULAW' are supported on output.

AU_write.setparams(tuple)

The tuple should be (nchannels, sampwidth, framerate, nframes, comptype, compname), with values valid for the set*() methods. Set all parameters.

AU_write.tell()

Return current position in the file, with the same disclaimer for the AU_read.tell() and AU_read.setpos() methods.

AU_write.writeframesraw(data)

写入音频数据但不更新 nframes。

在 3.4 版更改: 现在可接受任意 bytes-like object。

AU_write.writeframes(data)

写入音频数据并更新 nframes。

在 3.4 版更改: 现在可接受任意 bytes-like object。

AU_write.close()

Make sure nframes is correct, and close the file.

This method is called upon deletion.

Note that it is invalid to set any parameters after calling writeframes() or writeframesraw().

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

22.4. wave — 读写WAV格式文件

源代码: Lib/wave.py [https://github.com/python/cpython/tree/3.6/Lib/wave.py]

wave 模块提供了一个处理 WAV 声音格式的便利接口。它不支持压缩/解压，但是支持单声道/立体声。

wave 模块定义了以下函数和异常:

wave.open(file, mode=None)

如果 file 是一个字符串，打开对应文件名的文件。否则就把它作为文件类对象来处理。mode 可以为以下值：

'rb'

只读模式。

'wb'

只写模式。

注意不支持同时读写WAV文件。

mode 设为 'rb' 时返回一个 Wave_read 对象，而 mode 设为 'wb' 时返回一个 Wave_write 对象。如果省略 mode 并指定 file 来传入一个文件类对象，则 file.mode 会被用作 mode 的默认值。

如果操作的是文件对象，当使用 wave 对象的 close() 方法时，并不会真正关闭文件对象，这需要调用者负责来关闭文件对象。

The open() function may be used in a with statement. When the with block completes, the Wave_read.close() or Wave_write.close() method is called.

在 3.4 版更改: 添加了对不可搜索文件的支持。

wave.openfp(file, mode)

同 open()，用于向后兼容。

exception wave.Error

当不符合WAV格式或无法操作时引发的错误。

22.4.1. Wave_read对象

由 open() 返回的 Wave_read 对象，有以下几种方法:

Wave_read.close()

关闭 wave 打开的数据流并使对象不可用。当对象销毁时会自动调用。

Wave_read.getnchannels()

返回声道数量（1 为单声道，2 为立体声）

Wave_read.getsampwidth()

返回采样字节长度。

Wave_read.getframerate()

返回采样频率。

Wave_read.getnframes()

返回音频总帧数。

Wave_read.getcomptype()

返回压缩类型（只支持 'NONE' 类型）

Wave_read.getcompname()

getcomptype() 的通俗版本。使用 'not compressed' 代替 'NONE'。

Wave_read.getparams()

返回一个 namedtuple() (nchannels, sampwidth, framerate, nframes, comptype, compname)，与 get*() 方法的输出相同。

Wave_read.readframes(n)

读取并返回以 bytes 对象表示的最多 n 帧音频。

Wave_read.rewind()

重置文件指针至音频开头.

后面两个方法是为了和 aifc 保持兼容，实际不做任何事情。

Wave_read.getmarkers()

返回 None。

Wave_read.getmark(id)

引发错误异常。

以下两个方法都使用指针，具体实现由其底层决定。

Wave_read.setpos(pos)

设置文件指针到指定位置。

Wave_read.tell()

返回当前文件指针位置。

22.4.2. Wave_write 对象

对于可查找的输出流，wave 头将自动更新以反映实际写入的帧数。 对于不可查找的流，当写入第一帧时 nframes 值必须准确。 获取准确的 nframes 值可以通过调用 setnframes() 或 setparams() 并附带 close() 被调用之前将要写入的帧数，然后使用 writeframesraw() 来写入帧数据，或者通过调用 writeframes() 并附带所有要写入的帧。 在后一种情况下 writeframes() 将计算数据中的帧数并在写入帧数据之前相应地设置 nframes。

由 open() 返回的 Wave_write 对象，有以下几种方法:

在 3.4 版更改: 添加了对不可搜索文件的支持。

Wave_write.close()

确保 nframes 是正确的，并在文件被 wave 打开时关闭它。 此方法会在对象收集时被调用。 如果输出流不可查找且 nframes 与实际写入的帧数不匹配时引发异常。

Wave_write.setnchannels(n)

设置声道数。

Wave_write.setsampwidth(n)

设置采样字节长度为 n。

Wave_write.setframerate(n)

设置采样频率为 n。

在 3.2 版更改: 对此方法的非整数输入会被舍入到最接近的整数。

Wave_write.setnframes(n)

设置总帧数为 n。 如果与之后实际写入的帧数不一致此值将会被更改（ 如果输出流不可查找则此更改尝试将引发错误）。

Wave_write.setcomptype(type, name)

设置压缩格式。目前只支持 NONE 即无压缩格式。

Wave_write.setparams(tuple)

tuple 应该是 (nchannels, sampwidth, framerate, nframes, comptype, compname)，每项的值应可用于 set*() 方法。设置所有形参。

Wave_write.tell()

返回当前文件指针，其指针含义和 Wave_read.tell() 以及 Wave_read.setpos() 是一致的。

Wave_write.writeframesraw(data)

写入音频数据但不更新 nframes。

在 3.4 版更改: 现在可接受任意 bytes-like object。

Wave_write.writeframes(data)

写入音频帧并确保 nframes 是正确的。 如果输出流不可查找且在 data 被写入之后写入的总帧数与之前设定的has been written does not match the previously set value for nframes 值不匹配将会引发错误。

在 3.4 版更改: 现在可接受任意 bytes-like object。

注意在调用 writeframes() 或 writeframesraw() 之后再设置任何格式参数是无效的，而且任何这样的尝试将引发 wave.Error。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

22.5. chunk — 读取 IFF 分块数据

源代码: Lib/chunk.py [https://github.com/python/cpython/tree/3.6/Lib/chunk.py]

本模块提供了一个读取使用 EA IFF 85 分块的数据的接口chunks. 1 这种格式使用的场合有 Audio Interchange File Format (AIFF/AIFF-C) 和 Real Media File Format (RMFF) 等。 与它们密切相关的 WAVE 音频文件也可使用此模块来读取。

一个chunk具有以下结构:

偏移

长度

目录

0

4

区块ID

4

4

大端字节顺序的块大小，不包括头

8

n

数据字节，其中 n 是前一字段中给出的大小

8 + n

0 或 1

如果 n 为奇数且使用块对齐，则需要填充字节

ID是一个4字节的字符串，用于标识块的类型。

大小字段（32 位的值，使用大端字节序编码）给出分块数据的大小，不包括 8 字节的标头。

使用由一个或更多分块组成的 IFF 类型文件。 此处定义的 Chunk 类的建议使用方式是在每个分块开始时实例化一个实例并从实例读取直到其末尾，在那之后可以再实例化新的实例。 到达文件末尾时，创建新实例将会失败并引发 EOFError 异常。

class chunk.Chunk(file, align=True, bigendian=True, inclheader=False)

代表一个分块的类。 file 参数预期为一个文件类对象。 特别地也允许该类的实例。 唯一必需的方法是 read()。 如果存在 seek() 和 tell() 方法并且没有引发异常，它们也会被使用。 如果存在这些方法并且引发了异常，则它们不应改变目标对象。 如果可选参数 align 为真值，则分块应当以 2 字节边界对齐。 如果 align 为假值，则不使用对齐。 此参数默认为真值。 如果可选参数 bigendian 为假值，分块大小应当为小端序。 这对于 WAVE 音频文件是必须的。 此参数默认为真值。 如果可选参数 inclheader 为真值，则分块标头中给出的大小将包括标头的大小。 此参数默认为假值。

Chunk 对象支持下列方法：

getname()

返回分块的名称（ID）。 这是分块的头 4 个字节。

getsize()

返回分块的大小。

close()

关闭并跳转到分块的末尾。 这不会关闭下层的文件。

在 close() 方法已被调用后其余方法将会引发 OSError。 在 Python 3.3 之前，它们曾会引发 IOError，现在这是 OSError 的一个别名。

isatty()

返回 False。

seek(pos, whence=0)

设置分块的当前位置。 whence 参数为可选项并且默认为 0 (绝对文件定位)；其他值还有 1 (相对当前位置查找) 和 2 (相对文件末尾查找)。 没有返回值。 如果下层文件不支持查找，则只允许向前查找。

tell()

将当前位置返回到分块。

read(size=-1)

从分块读取至多 size 个字节（如果在获得 size 个字节之前已到达分块末尾则读取的字节会少于此数量）。 如果 size 参数为负值或被省略，则读取所有字节直到分块末尾。 当立即遇到分块末尾则返回空字节串对象。

skip()

跳到分块末尾。此后对分块再次调用 read() 将返回 b''。 如果你对分块的内容不感兴趣，则应当调用此方法以使文件指向下一分块的开头。

备注

1

“EA IFF 85” 交换格式文件标准, Jerry Morrison, Electronic Arts, 1985 年 1 月。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

22.6. colorsys — 颜色系统间的转换

源代码： Lib/colorsys.py [https://github.com/python/cpython/tree/3.6/Lib/colorsys.py]

colorsys 模块定义了计算机显示器所用的 RGB (Red Green Blue) 色彩空间与三种其他色彩坐标系统 YIQ, HLS (Hue Lightness Saturation) 和 HSV (Hue Saturation Value) 表示的颜色值之间的双向转换。 所有这些色彩空间的坐标都使用浮点数值来表示。 在 YIQ 空间中，Y 坐标取值为 0 和 1 之间，而 I 和 Q 坐标均可以为正数或负数。 在所有其他空间中，坐标取值均为 0 和 1 之间。

参见

More information about color spaces can be found at http://www.poynton.com/ColorFAQ.html and https://www.cambridgeincolour.com/tutorials/color-spaces.htm.

colorsys 模块定义了如下函数：

colorsys.rgb_to_yiq(r, g, b)

把颜色从RGB值转为YIQ值。

colorsys.yiq_to_rgb(y, i, q)

把颜色从YIQ值转为RGB值。

colorsys.rgb_to_hls(r, g, b)

把颜色从RGB值转为HLS值。

colorsys.hls_to_rgb(h, l, s)

把颜色从HLS值转为RGB值。

colorsys.rgb_to_hsv(r, g, b)

把颜色从RGB值转为HSV值。

colorsys.hsv_to_rgb(h, s, v)

把颜色从HSV值转为RGB值。

示例:

>>> import colorsys
>>> colorsys.rgb_to_hsv(0.2, 0.4, 0.4)
(0.5, 0.5, 0.4)
>>> colorsys.hsv_to_rgb(0.5, 0.5, 0.4)
(0.2, 0.4, 0.4)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

22.7. imghdr — 推测图像类型

源代码 Lib/imghdr.py [https://github.com/python/cpython/tree/3.6/Lib/imghdr.py]

imghdr 模块推测文件或字节流中的图像的类型。

imghdr 模块定义了以下类型：

imghdr.what(filename, h=None)

测试包含在命名为 filename 的文件中的图像数据，并且返回描述此类图片的字符串。如果可选的 h 被提供， filename 将被忽略并且 h 包含将被测试的二进制流。

在 3.6 版更改: 接受一个 path-like object。

接下来的图像类型是可识别的，返回值来自 what()：

值

图像格式

'rgb'

SGI 图像库文件

'gif'

GIF 87a 和 89a 文件

'pbm'

便携式位图文件

'pgm'

便携式灰度图文件

'ppm'

便携式像素表文件

'tiff'

TIFF 文件

'rast'

Sun 光栅文件

'xbm'

X 位图文件

'jpeg'

JFIF 或 Exif 格式的 JPEG 数据

'bmp'

BMP 文件

'png'

便携式网络图像

'webp'

WebP 文件

'exr'

OpenEXR 文件

3.5 新版功能: exr 和 webp 格式被添加。

你可以扩展此 imghdr 可以被追加的这个变量识别的文件格式的列表：

imghdr.tests

执行单个测试的函数列表。每个函数都有两个参数：字节流和类似开放文件的对象。当 what() 用字节流调用时，类文件对象将是 None。

如果测试成功，这个测试函数应当返回一个描述图像类型的字符串，否则返回 None。

示例:

>>> import imghdr
>>> imghdr.what('bass.gif')
'gif'

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

22.8. sndhdr — 推测声音文件的类型

源代码 Lib/sndhdr.py [https://github.com/python/cpython/tree/3.6/Lib/sndhdr.py]

sndhdr 提供了企图猜测文件中的声音数据类型的功能函数。当这些函数可以推测出存储在文件中的声音数据的类型是，它们返回一个 collections.namedtuple()，包含了五种属性：（filetype, framerate, nchannels, nframes, sampwidth）。这些 type 的值表示数据的类型，会是以下字符串之一： 'aifc', 'aiff', 'au', 'hcom', 'sndr', 'sndt', 'voc', 'wav', '8svx', 'sb', 'ub', or 'ul' 。 sampling_rate 可能是实际值或者当未知或者难以解码时的 0。类似的， channels 也会返回实际值或者在无法推测或者难以解码时返回 0。 frames 则是实际值或 -1。 元组的最后一项， bits_per_sample 将会为比特表示的 sample 大小或者 A-LAW 时为 'A'， u-LAW 时为 'U'。

sndhdr.what(filename)

使用 whathdr() 推测存储在 filename 文件中的声音数据的类型。如果成功，返回上述的命名元组，否则返回 None。

在 3.5 版更改: 将结果从元组改为命名元组。

sndhdr.whathdr(filename)

基于文件头推测存储在文件中的声音数据类型。文件名由 filename 给出。这个函数在成功时返回上述命名元组，或者在失败时返回 None。

在 3.5 版更改: 将结果从元组改为命名元组。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

22.9. ossaudiodev — Access to OSS-compatible audio devices

This module allows you to access the OSS (Open Sound System) audio interface. OSS is available for a wide range of open-source and commercial Unices, and is the standard audio interface for Linux and recent versions of FreeBSD.

在 3.3 版更改: 此模块中过去会引发 IOError 的操作现在将引发 OSError。

参见

Open Sound System Programmer’s Guide [http://www.opensound.com/pguide/oss.pdf]

the official documentation for the OSS C API

The module defines a large number of constants supplied by the OSS device driver; see <sys/soundcard.h> on either Linux or FreeBSD for a listing.

ossaudiodev defines the following variables and functions:

exception ossaudiodev.OSSAudioError

This exception is raised on certain errors. The argument is a string describing what went wrong.

(If ossaudiodev receives an error from a system call such as open(), write(), or ioctl(), it raises OSError. Errors detected directly by ossaudiodev result in OSSAudioError.)

(For backwards compatibility, the exception class is also available as ossaudiodev.error.)

ossaudiodev.open(mode)

ossaudiodev.open(device, mode)

Open an audio device and return an OSS audio device object. This object supports many file-like methods, such as read(), write(), and fileno() (although there are subtle differences between conventional Unix read/write semantics and those of OSS audio devices). It also supports a number of audio-specific methods; see below for the complete list of methods.

device is the audio device filename to use. If it is not specified, this module first looks in the environment variable AUDIODEV for a device to use. If not found, it falls back to /dev/dsp.

mode is one of 'r' for read-only (record) access, 'w' for write-only (playback) access and 'rw' for both. Since many sound cards only allow one process to have the recorder or player open at a time, it is a good idea to open the device only for the activity needed. Further, some sound cards are half-duplex: they can be opened for reading or writing, but not both at once.

Note the unusual calling syntax: the first argument is optional, and the second is required. This is a historical artifact for compatibility with the older linuxaudiodev module which ossaudiodev supersedes.

ossaudiodev.openmixer([device])

Open a mixer device and return an OSS mixer device object. device is the mixer device filename to use. If it is not specified, this module first looks in the environment variable MIXERDEV for a device to use. If not found, it falls back to /dev/mixer.

22.9.1. Audio Device Objects

Before you can write to or read from an audio device, you must call three methods in the correct order:

setfmt() to set the output format

channels() to set the number of channels

speed() to set the sample rate

Alternately, you can use the setparameters() method to set all three audio parameters at once. This is more convenient, but may not be as flexible in all cases.

The audio device objects returned by open() define the following methods and (read-only) attributes:

oss_audio_device.close()

Explicitly close the audio device. When you are done writing to or reading from an audio device, you should explicitly close it. A closed device cannot be used again.

oss_audio_device.fileno()

Return the file descriptor associated with the device.

oss_audio_device.read(size)

Read size bytes from the audio input and return them as a Python string. Unlike most Unix device drivers, OSS audio devices in blocking mode (the default) will block read() until the entire requested amount of data is available.

oss_audio_device.write(data)

Write a bytes-like object data to the audio device and return the number of bytes written. If the audio device is in blocking mode (the default), the entire data is always written (again, this is different from usual Unix device semantics). If the device is in non-blocking mode, some data may not be written—see writeall().

在 3.5 版更改: 现在支持可写的 字节类对象。

oss_audio_device.writeall(data)

Write a bytes-like object data to the audio device: waits until the audio device is able to accept data, writes as much data as it will accept, and repeats until data has been completely written. If the device is in blocking mode (the default), this has the same effect as write(); writeall() is only useful in non-blocking mode. Has no return value, since the amount of data written is always equal to the amount of data supplied.

在 3.5 版更改: 现在支持可写的 字节类对象。

在 3.2 版更改: Audio device objects also support the context management protocol, i.e. they can be used in a with statement.

The following methods each map to exactly one ioctl() system call. The correspondence is obvious: for example, setfmt() corresponds to the SNDCTL_DSP_SETFMT ioctl, and sync() to SNDCTL_DSP_SYNC (this can be useful when consulting the OSS documentation). If the underlying ioctl() fails, they all raise OSError.

oss_audio_device.nonblock()

Put the device into non-blocking mode. Once in non-blocking mode, there is no way to return it to blocking mode.

oss_audio_device.getfmts()

Return a bitmask of the audio output formats supported by the soundcard. Some of the formats supported by OSS are:

格式

描述

AFMT_MU_LAW

a logarithmic encoding (used by Sun .au files and /dev/audio)

AFMT_A_LAW

a logarithmic encoding

AFMT_IMA_ADPCM

a 4:1 compressed format defined by the Interactive Multimedia Association

AFMT_U8

Unsigned, 8-bit audio

AFMT_S16_LE

Signed, 16-bit audio, little-endian byte order (as used by Intel processors)

AFMT_S16_BE

Signed, 16-bit audio, big-endian byte order (as used by 68k, PowerPC, Sparc)

AFMT_S8

Signed, 8 bit audio

AFMT_U16_LE

Unsigned, 16-bit little-endian audio

AFMT_U16_BE

Unsigned, 16-bit big-endian audio

Consult the OSS documentation for a full list of audio formats, and note that most devices support only a subset of these formats. Some older devices only support AFMT_U8; the most common format used today is AFMT_S16_LE.

oss_audio_device.setfmt(format)

Try to set the current audio format to format—see getfmts() for a list. Returns the audio format that the device was set to, which may not be the requested format. May also be used to return the current audio format—do this by passing an “audio format” of AFMT_QUERY.

oss_audio_device.channels(nchannels)

Set the number of output channels to nchannels. A value of 1 indicates monophonic sound, 2 stereophonic. Some devices may have more than 2 channels, and some high-end devices may not support mono. Returns the number of channels the device was set to.

oss_audio_device.speed(samplerate)

Try to set the audio sampling rate to samplerate samples per second. Returns the rate actually set. Most sound devices don’t support arbitrary sampling rates. Common rates are:

采样率

描述

8000

/dev/audio 的默认采样率

11025

语音录音

22050

44100

CD质量的音频（16位采样和2通道）

96000

DVD质量的音频（24位采样）

oss_audio_device.sync()

Wait until the sound device has played every byte in its buffer. (This happens implicitly when the device is closed.) The OSS documentation recommends closing and re-opening the device rather than using sync().

oss_audio_device.reset()

Immediately stop playing or recording and return the device to a state where it can accept commands. The OSS documentation recommends closing and re-opening the device after calling reset().

oss_audio_device.post()

Tell the driver that there is likely to be a pause in the output, making it possible for the device to handle the pause more intelligently. You might use this after playing a spot sound effect, before waiting for user input, or before doing disk I/O.

The following convenience methods combine several ioctls, or one ioctl and some simple calculations.

oss_audio_device.setparameters(format, nchannels, samplerate[, strict=False])

Set the key audio sampling parameters—sample format, number of channels, and sampling rate—in one method call. format, nchannels, and samplerate should be as specified in the setfmt(), channels(), and speed() methods. If strict is true, setparameters() checks to see if each parameter was actually set to the requested value, and raises OSSAudioError if not. Returns a tuple (format, nchannels, samplerate) indicating the parameter values that were actually set by the device driver (i.e., the same as the return values of setfmt(), channels(), and speed()).

For example,

(fmt, channels, rate) = dsp.setparameters(fmt, channels, rate)

is equivalent to

fmt = dsp.setfmt(fmt)
channels = dsp.channels(channels)
rate = dsp.rate(rate)

oss_audio_device.bufsize()

Returns the size of the hardware buffer, in samples.

oss_audio_device.obufcount()

Returns the number of samples that are in the hardware buffer yet to be played.

oss_audio_device.obuffree()

Returns the number of samples that could be queued into the hardware buffer to be played without blocking.

Audio device objects also support several read-only attributes:

oss_audio_device.closed

Boolean indicating whether the device has been closed.

oss_audio_device.name

String containing the name of the device file.

oss_audio_device.mode

The I/O mode for the file, either "r", "rw", or "w".

22.9.2. Mixer Device Objects

The mixer object provides two file-like methods:

oss_mixer_device.close()

This method closes the open mixer device file. Any further attempts to use the mixer after this file is closed will raise an OSError.

oss_mixer_device.fileno()

Returns the file handle number of the open mixer device file.

在 3.2 版更改: Mixer objects also support the context management protocol.

The remaining methods are specific to audio mixing:

oss_mixer_device.controls()

This method returns a bitmask specifying the available mixer controls (“Control” being a specific mixable “channel”, such as SOUND_MIXER_PCM or SOUND_MIXER_SYNTH). This bitmask indicates a subset of all available mixer controls—the SOUND_MIXER_* constants defined at module level. To determine if, for example, the current mixer object supports a PCM mixer, use the following Python code:

mixer=ossaudiodev.openmixer()
if mixer.controls() & (1 << ossaudiodev.SOUND_MIXER_PCM):
    # PCM is supported
    ... code ...

For most purposes, the SOUND_MIXER_VOLUME (master volume) and SOUND_MIXER_PCM controls should suffice—but code that uses the mixer should be flexible when it comes to choosing mixer controls. On the Gravis Ultrasound, for example, SOUND_MIXER_VOLUME does not exist.

oss_mixer_device.stereocontrols()

Returns a bitmask indicating stereo mixer controls. If a bit is set, the corresponding control is stereo; if it is unset, the control is either monophonic or not supported by the mixer (use in combination with controls() to determine which).

See the code example for the controls() function for an example of getting data from a bitmask.

oss_mixer_device.reccontrols()

Returns a bitmask specifying the mixer controls that may be used to record. See the code example for controls() for an example of reading from a bitmask.

oss_mixer_device.get(control)

Returns the volume of a given mixer control. The returned volume is a 2-tuple (left_volume,right_volume). Volumes are specified as numbers from 0 (silent) to 100 (full volume). If the control is monophonic, a 2-tuple is still returned, but both volumes are the same.

Raises OSSAudioError if an invalid control is specified, or OSError if an unsupported control is specified.

oss_mixer_device.set(control, (left, right))

Sets the volume for a given mixer control to (left,right). left and right must be ints and between 0 (silent) and 100 (full volume). On success, the new volume is returned as a 2-tuple. Note that this may not be exactly the same as the volume specified, because of the limited resolution of some soundcard’s mixers.

Raises OSSAudioError if an invalid mixer control was specified, or if the specified volumes were out-of-range.

oss_mixer_device.get_recsrc()

This method returns a bitmask indicating which control(s) are currently being used as a recording source.

oss_mixer_device.set_recsrc(bitmask)

Call this function to specify a recording source. Returns a bitmask indicating the new recording source (or sources) if successful; raises OSError if an invalid source was specified. To set the current recording source to the microphone input:

mixer.setrecsrc (1 << ossaudiodev.SOUND_MIXER_MIC)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

22. 多媒体服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

23. 国际化

本章中介绍的模块通过提供选择要在程序信息中使用的语言的机制或通过定制输出以匹配本地约定来帮助你编写不依赖于语言和区域设置的软件。

本章中描述的模块列表是：

23.1. gettext — 多语种国际化服务

23.1.1. GNU gettext API

23.1.2. 基于类的 API

23.1.2.1. NullTranslations 类

23.1.2.2. GNUTranslations 类

23.1.2.3. Solaris 消息编目支持

23.1.2.4. 编目构造器

23.1.3. 国际化 (I18N) 你的程序和模块

23.1.3.1. 本地化你的模块

23.1.3.2. 本地化你的应用程序

23.1.3.3. 即时更改语言

23.1.3.4. 延迟翻译

23.1.4. 致谢

23.2. locale — 国际化服务

23.2.1. Background, details, hints, tips and caveats

23.2.2. For extension writers and programs that embed Python

23.2.3. Access to message catalogs

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

23. 国际化 »

|

23.1. gettext — 多语种国际化服务

源代码： Lib/gettext.py [https://github.com/python/cpython/tree/3.6/Lib/gettext.py]

The gettext module provides internationalization (I18N) and localization (L10N) services for your Python modules and applications. It supports both the GNU gettext message catalog API and a higher level, class-based API that may be more appropriate for Python files. The interface described below allows you to write your module and application messages in one natural language, and provide a catalog of translated messages for running under different natural languages.

同时还给出一些本地化 Python 模块及应用程序的小技巧。

23.1.1. GNU gettext API

模块 gettext 定义了下列 API，这与 gettext API 类似。如果你使用该 API，将会对整个应用程序产生全局的影响。如果你的应用程序支持多语种，而语言选择取决于用户的区域设置，这通常正是你所想要的。而如果你正在本地化某个 Python 模块，或者你的应用程序需要在运行时切换语言，相反你或许想用基于类的API。

gettext.bindtextdomain(domain, localedir=None)

Bind the domain to the locale directory localedir. More concretely, gettext will look for binary .mo files for the given domain using the path (on Unix): localedir/language/LC_MESSAGES/domain.mo, where languages is searched for in the environment variables LANGUAGE, LC_ALL, LC_MESSAGES, and LANG respectively.

如果遗漏了 localedir 或者设置为 None，那么将返回当前 domain 所绑定的值 1

gettext.bind_textdomain_codeset(domain, codeset=None)

将 domain 绑定到 codeset，修改 lgettext(), ldgettext(), lngettext() 和 ldngettext() 函数返回的字节串的字符编码。如果省略了 codeset，则返回当前绑定的编码集。

gettext.textdomain(domain=None)

修改或查询当前的全局域。如果 domain 为 None，则返回当前的全局域，不为 None 则将全局域设置为 domain，并返回它。

gettext.gettext(message)

返回 message 的本地化翻译，依据包括当前的全局域、语言和区域目录。本函数在本地命名空间中通常有别名 _() （参考下面的示例）。

gettext.dgettext(domain, message)

与 gettext() 类似，但在指定的 domain 中查找 message。

gettext.ngettext(singular, plural, n)

与 gettext() 类似，但考虑了复数形式。如果找到了翻译，则将 n 代入复数公式，然后返回得出的消息（某些语言具有两种以上的复数形式）。如果未找到翻译，则 n 为 1 时返回 singular，为其他数时返回 plural。

复数公式取自编目头文件。它是 C 或 Python 表达式，有一个自变量 n，该表达式计算的是所需复数形式在编目中的索引号。关于在 .po 文件中使用的确切语法和各种语言的公式，请参阅 GNU gettext 文档 [https://www.gnu.org/software/gettext/manual/gettext.html] 。

gettext.dngettext(domain, singular, plural, n)

与 ngettext() 类似，但在指定的 domain 中查找 message。

gettext.lgettext(message)

gettext.ldgettext(domain, message)

gettext.lngettext(singular, plural, n)

gettext.ldngettext(domain, singular, plural, n)

与前缀中没有 l 的相应函数等效（ gettext(), dgettext(), ngettext() 和 dngettext() ），但是如果没有用 bind_textdomain_codeset() 显式设置其他编码，则返回的翻译将以首选系统编码来编码字节串。

警告

These functions should be avoided in Python 3, because they return encoded bytes. It’s much better to use alternatives which return Unicode strings instead, since most Python applications will want to manipulate human readable text as strings instead of bytes. Further, it’s possible that you may get unexpected Unicode-related exceptions if there are encoding problems with the translated strings. It is possible that the l*() functions will be deprecated in future Python versions due to their inherent problems and limitations.

注意，GNU gettext 还定义了 dcgettext() 方法，但它被认为不实用，因此目前没有实现它。

这是该 API 的典型用法示例:

import gettext
gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
gettext.textdomain('myapplication')
_ = gettext.gettext
# ...
print(_('This is a translatable string.'))

23.1.2. 基于类的 API

The class-based API of the gettext module gives you more flexibility and greater convenience than the GNU gettext API. It is the recommended way of localizing your Python applications and modules. gettext defines a “translations” class which implements the parsing of GNU .mo format files, and has methods for returning strings. Instances of this “translations” class can also install themselves in the built-in namespace as the function _().

gettext.find(domain, localedir=None, languages=None, all=False)

This function implements the standard .mo file search algorithm. It takes a domain, identical to what textdomain() takes. Optional localedir is as in bindtextdomain() Optional languages is a list of strings, where each string is a language code.

如果没有传入 localedir，则使用默认的系统区域设置目录。 2 如果没有传入 languages，则搜索以下环境变量：LANGUAGE、LC_ALL、LC_MESSAGES 和 LANG。从这些变量返回的第一个非空值将用作 languages 变量。环境变量应包含一个语言列表，由冒号分隔，该列表会被按冒号拆分，以产生所需的语言代码字符串列表。

find() 将扩展并规范化 language，然后遍历它们，搜索由这些组件构建的现有文件:

localedir/language/LC_MESSAGES/domain.mo

find() 返回找到类似的第一个文件名。如果找不到这样的文件，则返回 None。如果传入了 all，它将返回一个列表，包含所有文件名，并按它们在语言列表或环境变量中出现的顺序排列。

gettext.translation(domain, localedir=None, languages=None, class_=None, fallback=False, codeset=None)

Return a Translations instance based on the domain, localedir, and languages, which are first passed to find() to get a list of the associated .mo file paths. Instances with identical .mo file names are cached. The actual class instantiated is either class_ if provided, otherwise GNUTranslations. The class’s constructor must take a single file object argument. If provided, codeset will change the charset used to encode translated strings in the lgettext() and lngettext() methods.

如果找到多个文件，后找到的文件将用作先前文件的替补。为了设置替补，将使用 copy.copy() 从缓存中克隆每个 translation 对象。实际的实例数据仍在缓存中共享。

如果 .mo 文件未找到，且 fallback 为 false（默认值），则本函数引发 OSError 异常，如果 fallback 为 true，则返回一个 NullTranslations 实例。

在 3.3 版更改: IOError 代替 OSError 被引发。

gettext.install(domain, localedir=None, codeset=None, names=None)

根据传入 translation() 函数的 domain、localedir 和 codeset，在 Python 内建命名空间中安装 _() 函数。

names 参数的信息请参阅 translation 对象的 install() 方法的描述。

如下所示，通常将字符串包括在 _() 函数的调用中，以标记应用程序中待翻译的字符串，就像这样:

print(_('This string will be translated.'))

为了方便，一般将 _() 函数安装在 Python 内建命名空间中，以便在应用程序的所有模块中轻松访问它。

23.1.2.1. NullTranslations 类

translation 类实际实现的是，将原始源文件消息字符串转换为已翻译的消息字符串。所有 translation 类使用的基类为 NullTranslations，它提供了基本的接口，可用于编写自己定制的 translation 类。以下是 NullTranslations 的方法：

class gettext.NullTranslations(fp=None)

接受一个可选参数 文件对象 fp，该参数会被基类忽略。初始化由派生类设置的 “protected” （受保护的）实例变量 _info 和 _charset，与 _fallback 类似，但它是通过 add_fallback() 来设置的。如果 fp 不为 None，就会调用 self._parse(fp)。

_parse(fp)

No-op’d in the base class, this method takes file object fp, and reads the data from the file, initializing its message catalog. If you have an unsupported message catalog file format, you should override this method to parse your format.

add_fallback(fallback)

添加 fallback 为当前 translation 对象的替补对象。如果 translation 对象无法为指定消息提供翻译，则应向替补查询。

gettext(message)

如果设置了替补，则转发 gettext() 给替补。否则返回 message。须在派生类中重写。

ngettext(singular, plural, n)

如果设置了替补，则转发 ngettext() 给替补。否则，n 为 1 时返回 singular，为其他时返回 plural。须在派生类中重写。

lgettext(message)

lngettext(singular, plural, n)

与 gettext() 和 ngettext() 等效，但是如果没有用 set_output_charset() 显式设置编码，则返回的翻译将以首选系统编码来编码字节串。在派生类中被重写。

警告

应避免在 Python 3 中使用这些方法。请参阅 lgettext() 函数的警告。

info()

Return the “protected” _info variable.

charset()

返回消息编目文件的编码。

output_charset()

返回由 lgettext() 和 lngettext() 翻译的消息的编码。

set_output_charset(charset)

更改翻译出的消息的编码。

install(names=None)

本方法将 gettext() 安装至内建命名空间，并绑定为 _。

If the names parameter is given, it must be a sequence containing the names of functions you want to install in the builtins namespace in addition to _(). Supported names are 'gettext', 'ngettext', 'lgettext' and 'lngettext'.

注意，这仅仅是使 _() 函数在应用程序中生效的一种方法，尽管也是最方便的方法。由于它会影响整个应用程序全局，特别是内建命名空间，因此已经本地化的模块不应该安装 _()，而是应该用下列代码使 _() 在各自模块中生效:

import gettext
t = gettext.translation('mymodule', ...)
_ = t.gettext

这只将 _() 放在其模块的全局命名空间中，所以只影响其模块内的调用。

23.1.2.2. GNUTranslations 类

gettext 模块提供了一个派生自 NullTranslations 的其他类：GNUTranslations。该类重写了 _parse()，同时能以大端序和小端序格式读取 GNU gettext 格式的 .mo 文件。

GNUTranslations parses optional meta-data out of the translation catalog. It is convention with GNU gettext to include meta-data as the translation for the empty string. This meta-data is in RFC 822 [https://tools.ietf.org/html/rfc822.html]-style key: value pairs, and should contain the Project-Id-Version key. If the key Content-Type is found, then the charset property is used to initialize the “protected” _charset instance variable, defaulting to None if not found. If the charset encoding is specified, then all message ids and message strings read from the catalog are converted to Unicode using this encoding, else ASCII encoding is assumed.

由于消息 ID 也是作为 Unicode 字符串读取的，因此所有 *gettext() 方法都假定消息 ID 为 Unicode 字符串，而不是字节串。

整个键/值对集合将被放入一个字典，并设置为 “protected”（受保护的） _info 实例变量。

如果 .mo 文件的魔法值 (magic number) 无效，或遇到意外的主版本号，或在读取文件时发生其他问题，则实例化 GNUTranslations 类会引发 OSError。

class gettext.GNUTranslations

下列方法是根据基类实现重写的：

gettext(message)

在编目中查找 message ID，并以 Unicode 字符串形式返回相应的消息字符串。如果在编目中没有 message ID 条目，且配置了替补，则查找请求将被转发到替补的 gettext() 方法。否则，返回 message ID。

ngettext(singular, plural, n)

查找消息 ID 的复数形式。singular 用作消息 ID，用于在编目中查找，同时 n 用于确定使用哪种复数形式。返回的消息字符串是 Unicode 字符串。

如果在编目中没有找到消息 ID，且配置了替补，则查找请求将被转发到替补的 ngettext() 方法。否则，当 n 为 1 时返回 singular，其他情况返回 plural。

例如：

n = len(os.listdir('.'))
cat = GNUTranslations(somefile)
message = cat.ngettext(
    'There is %(num)d file in this directory',
    'There are %(num)d files in this directory',
    n) % {'num': n}

lgettext(message)

lngettext(singular, plural, n)

与 gettext() 和 ngettext() 等效，但是如果没有用 set_output_charset() 显式设置编码，则返回的翻译将以首选系统编码来编码字节串。

警告

应避免在 Python 3 中使用这些方法。请参阅 lgettext() 函数的警告。

23.1.2.3. Solaris 消息编目支持

Solaris 操作系统定义了自己的二进制 .mo 文件格式，但由于找不到该格式的文档，因此目前不支持该格式。

23.1.2.4. 编目构造器

GNOME 用的 gettext 模块是 James Henstridge 写的版本，但该版本的 API 略有不同。它文档中的用法是:

import gettext
cat = gettext.Catalog(domain, localedir)
_ = cat.gettext
print(_('hello world'))

为了与本模块的旧版本兼容，Catalog() 函数是上述 translation() 函数的别名。

本模块与 Henstridge 的模块有一个区别：他的编目对象支持通过映射 API 进行访问，但是该特性似乎从未使用过，因此目前不支持该特性。

23.1.3. 国际化 (I18N) 你的程序和模块

国际化 (I18N) 是指使程序可切换多种语言的操作。本地化 (L10N) 是指程序的适配能力，一旦程序被国际化，就能适配当地的语言和文化习惯。为了向 Python 程序提供不同语言的消息，需要执行以下步骤：

准备程序或模块，将可翻译的字符串特别标记起来

在已标记的文件上运行一套工具，用来生成原始消息编目

create language specific translations of the message catalogs

使用 gettext 模块，以便正确翻译消息字符串

为了准备代码以达成 I18N，需要巡视文件中的所有字符串。所有要翻译的字符串都应包括在 _('...') 内，以此打上标记——也就是调用 _() 函数。例如:

filename = 'mylog.txt'
message = _('writing a log message')
fp = open(filename, 'w')
fp.write(message)
fp.close()

在这个例子中，字符串 'writing a log message' 被标记为待翻译，而字符串 'mylog.txt' 和 'w' 没有被标记。

有一些工具可以将待翻译的字符串提取出来。原版的 GNU gettext 仅支持 C 或 C++ 源代码，但其扩展版 xgettext 可以扫描多种语言的代码（包括 Python），来找出标记为可翻译的字符串。Babel [http://babel.pocoo.org/] 是一个 Python 国际化库，其中包含一个 pybabel 脚本，用于提取并编译消息编目。François Pinard 写的称为 xpot 的程序也能完成类似的工作，可从他的 po-utils 软件包 [https://github.com/pinard/po-utils] 中获取。

（Python 还包括了这些程序的纯 Python 版本，称为 pygettext.py 和 msgfmt.py，某些 Python 发行版已经安装了它们。pygettext.py 类似于 xgettext，但只能理解 Python 源代码，无法处理诸如 C 或 C++ 的其他编程语言。pygettext.py 支持的命令行界面类似于 xgettext，查看其详细用法请运行 pygettext.py --help。msgfmt.py 与 GNU msgfmt 是二进制兼容的。有了这两个程序，可以不需要 GNU gettext 包来国际化 Python 应用程序。）

xgettext、pygettext 或类似工具生成的 .po 文件就是消息编目。它们是结构化的人类可读文件，包含源代码中所有被标记的字符串，以及这些字符串的翻译的占位符。

然后把这些 .po 文件的副本交给各个人工译者，他们为所支持的每种自然语言编写翻译。译者以 <语言名称>.po 文件的形式发送回翻译完的某个语言的版本，将该文件用 msgfmt 程序编译为机器可读的 .mo 二进制编目文件。gettext 模块使用 .mo 文件在运行时进行实际的翻译处理。

如何在代码中使用 gettext 模块取决于国际化单个模块还是整个应用程序。接下来的两节将讨论每种情况。

23.1.3.1. 本地化你的模块

If you are localizing your module, you must take care not to make global changes, e.g. to the built-in namespace. You should not use the GNU gettext API but instead the class-based API.

假设你的模块叫做 “spam”，并且该模块的各种自然语言翻译 .mo 文件存放于 /usr/share/locale，为 GNU gettext 格式。以下内容应放在模块顶部:

import gettext
t = gettext.translation('spam', '/usr/share/locale')
_ = t.gettext

23.1.3.2. 本地化你的应用程序

如果正在本地化应用程序，可以将 _() 函数全局安装到内建命名空间中，通常在应用程序的主文件中安装。这将使某个应用程序的所有文件都能使用 _('...')，而不必在每个文件中显式安装它。

最简单的情况，就只需将以下代码添加到应用程序的主程序文件中:

import gettext
gettext.install('myapplication')

如果需要设置语言环境目录，可以将其传递给 install() 函数:

import gettext
gettext.install('myapplication', '/usr/share/locale')

23.1.3.3. 即时更改语言

如果程序需要同时支持多种语言，则可能需要创建多个翻译实例，然后在它们之间进行显式切换，如下所示:

import gettext

lang1 = gettext.translation('myapplication', languages=['en'])
lang2 = gettext.translation('myapplication', languages=['fr'])
lang3 = gettext.translation('myapplication', languages=['de'])

# start by using language1
lang1.install()

# ... time goes by, user selects language 2
lang2.install()

# ... more time goes by, user selects language 3
lang3.install()

23.1.3.4. 延迟翻译

在大多数代码中，字符串会在编写位置进行翻译。但偶尔需要将字符串标记为待翻译，实际翻译却推迟到后面。一个典型的例子是:

animals = ['mollusk',
           'albatross',
           'rat',
           'penguin',
           'python', ]
# ...
for a in animals:
    print(a)

此处希望将 animals 列表中的字符串标记为可翻译，但不希望在打印之前对它们进行翻译。

这是处理该情况的一种方式:

def _(message): return message

animals = [_('mollusk'),
           _('albatross'),
           _('rat'),
           _('penguin'),
           _('python'), ]

del _

# ...
for a in animals:
    print(_(a))

该方法之所以可行，是因为 _() 的虚定义只是简单地返回了原本的字符串。并且该虚定义将临时覆盖内建命名空间中 _() 的定义（直到 del 命令）。即使先前在本地命名空间中已经有了 _() 的定义也请注意。

注意，第二次使用 _() 不会认为 “a” 可以传递给 gettext 程序去翻译，因为该参数不是字符串文字。

解决该问题的另一种方法是下面这个例子:

def N_(message): return message

animals = [N_('mollusk'),
           N_('albatross'),
           N_('rat'),
           N_('penguin'),
           N_('python'), ]

# ...
for a in animals:
    print(_(a))

这种情况下标记可翻译的字符串使用的是函数 N_()，该函数不会与 _() 的任何定义冲突。但是，需要教会消息提取程序去寻找用 N_() 标记的可翻译字符串。xgettext, pygettext, pybabel extract 和 xpot 都支持此功能，使用 -k 命令行开关即可。这里选择 N_() 为名称完全是任意的，它也能轻易改为 MarkThisStringForTranslation()。

23.1.4. 致谢

以下人员为创建此模块贡献了代码、反馈、设计建议、早期实现和宝贵的经验：

Peter Funk

James Henstridge

Juan David Ibáñez Palomar

Marc-André Lemburg

Martin von Löwis

François Pinard

Barry Warsaw

Gustavo Niemeyer

备注

1

The default locale directory is system dependent; for example, on RedHat Linux it is /usr/share/locale, but on Solaris it is /usr/lib/locale. The gettext module does not try to support these system dependent defaults; instead its default is sys.prefix/share/locale. For this reason, it is always best to call bindtextdomain() with an explicit absolute path at the start of your application.

2

参阅上方 bindtextdomain() 的脚注。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

23. 国际化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

23. 国际化 »

|

23.2. locale — 国际化服务

源代码： Lib/locale.py [https://github.com/python/cpython/tree/3.6/Lib/locale.py]

The locale module opens access to the POSIX locale database and functionality. The POSIX locale mechanism allows programmers to deal with certain cultural issues in an application, without requiring the programmer to know all the specifics of each country where the software is executed.

The locale module is implemented on top of the _locale module, which in turn uses an ANSI C locale implementation if available.

The locale module defines the following exception and functions:

exception locale.Error

Exception raised when the locale passed to setlocale() is not recognized.

locale.setlocale(category, locale=None)

If locale is given and not None, setlocale() modifies the locale setting for the category. The available categories are listed in the data description below. locale may be a string, or an iterable of two strings (language code and encoding). If it’s an iterable, it’s converted to a locale name using the locale aliasing engine. An empty string specifies the user’s default settings. If the modification of the locale fails, the exception Error is raised. If successful, the new locale setting is returned.

If locale is omitted or None, the current setting for category is returned.

setlocale() is not thread-safe on most systems. Applications typically start with a call of

import locale
locale.setlocale(locale.LC_ALL, '')

This sets the locale for all categories to the user’s default setting (typically specified in the LANG environment variable). If the locale is not changed thereafter, using multithreading should not cause problems.

locale.localeconv()

以字典的形式返回本地约定的数据库。此字典具有以下字符串作为键：

类别

（Windows 注册表的）键

含义

LC_NUMERIC

'decimal_point'

小数点字符。

'grouping'

Sequence of numbers specifying which relative positions the 'thousands_sep' is expected. If the sequence is terminated with CHAR_MAX, no further grouping is performed. If the sequence terminates with a 0, the last group size is repeatedly used.

'thousands_sep'

组之间使用的字符。

LC_MONETARY

'int_curr_symbol'

国际货币符号。

'currency_symbol'

当地货币符号。

'p_cs_precedes/n_cs_precedes'

货币符号是否在值之前（对于正值或负值）。

'p_sep_by_space/n_sep_by_space'

货币符号是否通过空格与值分隔（对于正值或负值）。

'mon_decimal_point'

用于货币金额的小数点。

'frac_digits'

货币值的本地格式中使用的小数位数。

'int_frac_digits'

货币价值的国际格式中使用的小数位数。

'mon_thousands_sep'

用于货币值的组分隔符。

'mon_grouping'

相当于 'grouping' ，用于货币价值。

'positive_sign'

用于标注正货币价值的符号。

'negative_sign'

用于注释负货币价值的符号。

'p_sign_posn/n_sign_posn'

符号的位置（对于正值或负值），见下文。

可以将所有数值设置为 CHAR_MAX ，以指示此语言环境中未指定任何值。

下面给出了 'p_sign_posn' 和 'n_sign_posn' 的可能值。

值

解释

0

被括号括起来的货币和金额。

1

该标志应位于值和货币符号之前。

2

该标志应位于值和货币符号之后。

3

标志应该紧跟在值之前。

4

标志应该紧跟值项。

CHAR_MAX

此语言环境中未指定任何内容。

The function sets temporarily the LC_CTYPE locale to the LC_NUMERIC locale or the LC_MONETARY locale if locales are different and numeric or monetary strings are non-ASCII. This temporary change affects other threads.

在 3.6.5 版更改: The function now sets temporarily the LC_CTYPE locale to the LC_NUMERIC locale in some cases.

locale.nl_langinfo(option)

Return some locale-specific information as a string. This function is not available on all systems, and the set of possible options might also vary across platforms. The possible argument values are numbers, for which symbolic constants are available in the locale module.

The nl_langinfo() function accepts one of the following keys. Most descriptions are taken from the corresponding description in the GNU C library.

locale.CODESET

Get a string with the name of the character encoding used in the selected locale.

locale.D_T_FMT

Get a string that can be used as a format string for time.strftime() to represent date and time in a locale-specific way.

locale.D_FMT

Get a string that can be used as a format string for time.strftime() to represent a date in a locale-specific way.

locale.T_FMT

Get a string that can be used as a format string for time.strftime() to represent a time in a locale-specific way.

locale.T_FMT_AMPM

Get a format string for time.strftime() to represent time in the am/pm format.

DAY_1 ... DAY_7

Get the name of the n-th day of the week.

注解

This follows the US convention of DAY_1 being Sunday, not the international convention (ISO 8601) that Monday is the first day of the week.

ABDAY_1 ... ABDAY_7

Get the abbreviated name of the n-th day of the week.

MON_1 ... MON_12

Get the name of the n-th month.

ABMON_1 ... ABMON_12

Get the abbreviated name of the n-th month.

locale.RADIXCHAR

Get the radix character (decimal dot, decimal comma, etc.).

locale.THOUSEP

Get the separator character for thousands (groups of three digits).

locale.YESEXPR

Get a regular expression that can be used with the regex function to recognize a positive response to a yes/no question.

注解

The expression is in the syntax suitable for the regex() function from the C library, which might differ from the syntax used in re.

locale.NOEXPR

Get a regular expression that can be used with the regex(3) function to recognize a negative response to a yes/no question.

locale.CRNCYSTR

Get the currency symbol, preceded by “-” if the symbol should appear before the value, “+” if the symbol should appear after the value, or “.” if the symbol should replace the radix character.

locale.ERA

Get a string that represents the era used in the current locale.

Most locales do not define this value. An example of a locale which does define this value is the Japanese one. In Japan, the traditional representation of dates includes the name of the era corresponding to the then-emperor’s reign.

Normally it should not be necessary to use this value directly. Specifying the E modifier in their format strings causes the time.strftime() function to use this information. The format of the returned string is not specified, and therefore you should not assume knowledge of it on different systems.

locale.ERA_D_T_FMT

Get a format string for time.strftime() to represent date and time in a locale-specific era-based way.

locale.ERA_D_FMT

Get a format string for time.strftime() to represent a date in a locale-specific era-based way.

locale.ERA_T_FMT

Get a format string for time.strftime() to represent a time in a locale-specific era-based way.

locale.ALT_DIGITS

Get a representation of up to 100 values used to represent the values 0 to 99.

locale.getdefaultlocale([envvars])

Tries to determine the default locale settings and returns them as a tuple of the form (language code, encoding).

According to POSIX, a program which has not called setlocale(LC_ALL, '') runs using the portable 'C' locale. Calling setlocale(LC_ALL, '') lets it use the default locale as defined by the LANG variable. Since we do not want to interfere with the current locale setting we thus emulate the behavior in the way described above.

To maintain compatibility with other platforms, not only the LANG variable is tested, but a list of variables given as envvars parameter. The first found to be defined will be used. envvars defaults to the search path used in GNU gettext; it must always contain the variable name 'LANG'. The GNU gettext search path contains 'LC_ALL', 'LC_CTYPE', 'LANG' and 'LANGUAGE', in that order.

Except for the code 'C', the language code corresponds to RFC 1766 [https://tools.ietf.org/html/rfc1766.html]. language code and encoding may be None if their values cannot be determined.

locale.getlocale(category=LC_CTYPE)

Returns the current setting for the given locale category as sequence containing language code, encoding. category may be one of the LC_* values except LC_ALL. It defaults to LC_CTYPE.

Except for the code 'C', the language code corresponds to RFC 1766 [https://tools.ietf.org/html/rfc1766.html]. language code and encoding may be None if their values cannot be determined.

locale.getpreferredencoding(do_setlocale=True)

Return the encoding used for text data, according to user preferences. User preferences are expressed differently on different systems, and might not be available programmatically on some systems, so this function only returns a guess.

On some systems, it is necessary to invoke setlocale() to obtain the user preferences, so this function is not thread-safe. If invoking setlocale is not necessary or desired, do_setlocale should be set to False.

locale.normalize(localename)

Returns a normalized locale code for the given locale name. The returned locale code is formatted for use with setlocale(). If normalization fails, the original name is returned unchanged.

If the given encoding is not known, the function defaults to the default encoding for the locale code just like setlocale().

locale.resetlocale(category=LC_ALL)

Sets the locale for category to the default setting.

The default setting is determined by calling getdefaultlocale(). category defaults to LC_ALL.

locale.strcoll(string1, string2)

Compares two strings according to the current LC_COLLATE setting. As any other compare function, returns a negative, or a positive value, or 0, depending on whether string1 collates before or after string2 or is equal to it.

locale.strxfrm(string)

Transforms a string to one that can be used in locale-aware comparisons. For example, strxfrm(s1) < strxfrm(s2) is equivalent to strcoll(s1, s2) < 0. This function can be used when the same string is compared repeatedly, e.g. when collating a sequence of strings.

locale.format(format, val, grouping=False, monetary=False)

Formats a number val according to the current LC_NUMERIC setting. The format follows the conventions of the % operator. For floating point values, the decimal point is modified if appropriate. If grouping is true, also takes the grouping into account.

If monetary is true, the conversion uses monetary thousands separator and grouping strings.

Please note that this function will only work for exactly one %char specifier. For whole format strings, use format_string().

locale.format_string(format, val, grouping=False)

Processes formatting specifiers as in format % val, but takes the current locale settings into account.

locale.currency(val, symbol=True, grouping=False, international=False)

Formats a number val according to the current LC_MONETARY settings.

The returned string includes the currency symbol if symbol is true, which is the default. If grouping is true (which is not the default), grouping is done with the value. If international is true (which is not the default), the international currency symbol is used.

Note that this function will not work with the ‘C’ locale, so you have to set a locale via setlocale() first.

locale.str(float)

Formats a floating point number using the same format as the built-in function str(float), but takes the decimal point into account.

locale.delocalize(string)

Converts a string into a normalized number string, following the LC_NUMERIC settings.

3.5 新版功能.

locale.atof(string)

Converts a string to a floating point number, following the LC_NUMERIC settings.

locale.atoi(string)

Converts a string to an integer, following the LC_NUMERIC conventions.

locale.LC_CTYPE

Locale category for the character type functions. Depending on the settings of this category, the functions of module string dealing with case change their behaviour.

locale.LC_COLLATE

Locale category for sorting strings. The functions strcoll() and strxfrm() of the locale module are affected.

locale.LC_TIME

Locale category for the formatting of time. The function time.strftime() follows these conventions.

locale.LC_MONETARY

Locale category for formatting of monetary values. The available options are available from the localeconv() function.

locale.LC_MESSAGES

Locale category for message display. Python currently does not support application specific locale-aware messages. Messages displayed by the operating system, like those returned by os.strerror() might be affected by this category.

locale.LC_NUMERIC

Locale category for formatting numbers. The functions format(), atoi(), atof() and str() of the locale module are affected by that category. All other numeric formatting operations are not affected.

locale.LC_ALL

Combination of all locale settings. If this flag is used when the locale is changed, setting the locale for all categories is attempted. If that fails for any category, no category is changed at all. When the locale is retrieved using this flag, a string indicating the setting for all categories is returned. This string can be later used to restore the settings.

locale.CHAR_MAX

This is a symbolic constant used for different values returned by localeconv().

示例:

>>> import locale
>>> loc = locale.getlocale()  # get current locale
# use German locale; name might vary with platform
>>> locale.setlocale(locale.LC_ALL, 'de_DE')
>>> locale.strcoll('f\xe4n', 'foo')  # compare a string containing an umlaut
>>> locale.setlocale(locale.LC_ALL, '')   # use user's preferred locale
>>> locale.setlocale(locale.LC_ALL, 'C')  # use default (C) locale
>>> locale.setlocale(locale.LC_ALL, loc)  # restore saved locale

23.2.1. Background, details, hints, tips and caveats

The C standard defines the locale as a program-wide property that may be relatively expensive to change. On top of that, some implementation are broken in such a way that frequent locale changes may cause core dumps. This makes the locale somewhat painful to use correctly.

Initially, when a program is started, the locale is the C locale, no matter what the user’s preferred locale is. There is one exception: the LC_CTYPE category is changed at startup to set the current locale encoding to the user’s preferred locale encoding. The program must explicitly say that it wants the user’s preferred locale settings for other categories by calling setlocale(LC_ALL, '').

It is generally a bad idea to call setlocale() in some library routine, since as a side effect it affects the entire program. Saving and restoring it is almost as bad: it is expensive and affects other threads that happen to run before the settings have been restored.

If, when coding a module for general use, you need a locale independent version of an operation that is affected by the locale (such as certain formats used with time.strftime()), you will have to find a way to do it without using the standard library routine. Even better is convincing yourself that using locale settings is okay. Only as a last resort should you document that your module is not compatible with non-C locale settings.

The only way to perform numeric operations according to the locale is to use the special functions defined by this module: atof(), atoi(), format(), str().

There is no way to perform case conversions and character classifications according to the locale. For (Unicode) text strings these are done according to the character value only, while for byte strings, the conversions and classifications are done according to the ASCII value of the byte, and bytes whose high bit is set (i.e., non-ASCII bytes) are never converted or considered part of a character class such as letter or whitespace.

23.2.2. For extension writers and programs that embed Python

Extension modules should never call setlocale(), except to find out what the current locale is. But since the return value can only be used portably to restore it, that is not very useful (except perhaps to find out whether or not the locale is C).

When Python code uses the locale module to change the locale, this also affects the embedding application. If the embedding application doesn’t want this to happen, it should remove the _locale extension module (which does all the work) from the table of built-in modules in the config.c file, and make sure that the _locale module is not accessible as a shared library.

23.2.3. Access to message catalogs

locale.gettext(msg)

locale.dgettext(domain, msg)

locale.dcgettext(domain, msg, category)

locale.textdomain(domain)

locale.bindtextdomain(domain, dir)

The locale module exposes the C library’s gettext interface on systems that provide this interface. It consists of the functions gettext(), dgettext(), dcgettext(), textdomain(), bindtextdomain(), and bind_textdomain_codeset(). These are similar to the same functions in the gettext module, but use the C library’s binary format for message catalogs, and the C library’s search algorithms for locating message catalogs.

Python applications should normally find no need to invoke these functions, and should use gettext instead. A known exception to this rule are applications that link with additional C libraries which internally invoke gettext() or dcgettext(). For these applications, it may be necessary to bind the text domain, so that the libraries can properly locate their message catalogs.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

23. 国际化 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

24. 程序框架

本章中描述的模块是很大程度上决定程序结构的框架。 目前，这里描述的模块都面向编写命令行接口。

本章描述的完整模块列表如下：

24.1. turtle — 海龟绘图

24.1.1. 概述

24.1.2. 可用的 Turtle 和 Screen 方法概览

24.1.2.1. Turtle 方法

24.1.2.2. TurtleScreen/Screen 方法

24.1.3. RawTurtle/Turtle 方法和对应函数

24.1.3.1. 海龟动作

24.1.3.2. 获取海龟的状态

24.1.3.3. 度量单位设置

24.1.3.4. 画笔控制

24.1.3.4.1. 绘图状态

24.1.3.4.2. 颜色控制

24.1.3.4.3. 填充

24.1.3.4.4. 更多绘图控制

24.1.3.5. 海龟状态

24.1.3.5.1. 可见性

24.1.3.5.2. 外观

24.1.3.6. 使用事件

24.1.3.7. 特殊海龟方法

24.1.3.8. 复合形状

24.1.4. TurtleScreen/Screen 方法及对应函数

24.1.4.1. 窗口控制

24.1.4.2. 动画控制

24.1.4.3. 使用屏幕事件

24.1.4.4. 输入方法

24.1.4.5. 设置与特殊方法

24.1.4.6. Screen 专有方法, 而非继承自 TurtleScreen

24.1.5. 公共类

24.1.6. 帮助与配置

24.1.6.1. 如何使用帮助

24.1.6.2. 文档字符串翻译为不同的语言

24.1.6.3. 如何配置 Screen 和 Turtle

24.1.7. turtledemo — 演示脚本集

24.1.8. Python 2.6 之后的变化

24.1.9. Python 3.0 之后的变化

24.2. cmd — 支持面向行的命令解释器

24.2.1. Cmd 对象

24.2.2. Cmd 例子

24.3. shlex — Simple lexical analysis

24.3.1. shlex Objects

24.3.2. Parsing Rules

24.3.3. Improved Compatibility with Shells

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

24. 程序框架 »

|

24.1. turtle — 海龟绘图

源码： Lib/turtle.py [https://github.com/python/cpython/tree/3.6/Lib/turtle.py]

24.1.1. 概述

Turtle graphics is a popular way for introducing programming to kids. It was part of the original Logo programming language developed by Wally Feurzig and Seymour Papert in 1966.

请想象绘图区有一只机器海龟，起始位置在 x-y 平面的 (0, 0) 点。先执行 import turtle，再执行 turtle.forward(15)，它将(在屏幕上)朝所面对的 x 轴正方向前进 15 像素，随着它的移动画出一条线段。再执行 turtle.right(25)，它将原地右转 25 度。

Turtle star

使用海龟绘图可以编写重复执行简单动作的程序画出精细复杂的形状。

from turtle import *
color('red', 'yellow')
begin_fill()
while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()

通过组合使用此类命令，可以轻松地绘制出精美的形状和图案。

turtle 模块是基于 Python 标准发行版 2.5 以来的同名模块重新编写并进行了功能扩展。

新模块尽量保持了原模块的特点，并且(几乎)100%与其兼容。这就意味着初学编程者能够以交互方式使用模块的所有命令、类和方法——运行 IDLE 时注意加 -n 参数。

turtle 模块提供面向对象和面向过程两种形式的海龟绘图基本组件。由于它使用 tkinter 实现基本图形界面，因此需要安装了 Tk 支持的 Python 版本。

面向对象的接口主要使用“2+2”个类：

TurtleScreen 类定义图形窗口作为绘图海龟的运动场。它的构造器需要一个 tkinter.Canvas 或 ScrolledCanvas 作为参数。应在 turtle 作为某个程序的一部分的时候使用。

Screen() 函数返回一个 TurtleScreen 子类的单例对象。此函数应在 turtle 作为独立绘图工具时使用。作为一个单例对象，其所属的类是不可被继承的。

TurtleScreen/Screen 的所有方法还存在对应的函数，即作为面向过程的接口组成部分。

RawTurtle (别名: RawPen) 类定义海龟对象在 TurtleScreen 上绘图。它的构造器需要一个 Canvas, ScrolledCanvas 或 TurtleScreen 作为参数，以指定 RawTurtle 对象在哪里绘图。

从 RawTurtle 派生出子类 Turtle (别名: Pen)，该类对象在 Screen 实例上绘图，如果实例不存在则会自动创建。

RawTurtle/Turtle 的所有方法也存在对应的函数，即作为面向过程的接口组成部分。

过程式接口提供与 Screen 和 Turtle 类的方法相对应的函数。函数名与对应的方法名相同。当 Screen 类的方法对应函数被调用时会自动创建一个 Screen 对象。当 Turtle 类的方法对应函数被调用时会自动创建一个 (匿名的) Turtle 对象。

如果屏幕上需要有多个海龟，就必须使用面向对象的接口。

注解

以下文档给出了函数的参数列表。对于方法来说当然还有额外的第一个参数 self，这里省略了。

24.1.2. 可用的 Turtle 和 Screen 方法概览

24.1.2.1. Turtle 方法

海龟动作

移动和绘制

forward() | fd() 前进

backward() | bk() | back() 后退

right() | rt() 右转

left() | lt() 左转

goto() | setpos() | setposition() 前往/定位

setx() 设置x坐标

sety() 设置y坐标

setheading() | seth() 设置朝向

home() 返回原点

circle() 画圆

dot() 画点

stamp() 印章

clearstamp() 清除印章

clearstamps() 清除多个印章

undo() 撤消

speed() 速度

获取海龟的状态

position() | pos() 位置

towards() 目标方向

xcor() x坐标

ycor() y坐标

heading() 朝向

distance() 距离

设置与度量单位

degrees() 角度

radians() 弧度

画笔控制

绘图状态

pendown() | pd() | down() 画笔落下

penup() | pu() | up() 画笔抬起

pensize() | width() 画笔粗细

pen() 画笔

isdown() 画笔是否落下

颜色控制

color() 颜色

pencolor() 画笔颜色

fillcolor() 填充颜色

填充

filling() 是否填充

begin_fill() 开始填充

end_fill() 结束填充

更多绘图控制

reset() 重置

clear() 清空

write() 书写

海龟状态

可见性

showturtle() | st() 显示海龟

hideturtle() | ht() 隐藏海龟

isvisible() 是否可见

外观

shape() 形状

resizemode() 大小调整模式

shapesize() | turtlesize() 形状大小

shearfactor() 剪切因子

settiltangle() 设置倾角

tiltangle() 倾角

tilt() 倾斜

shapetransform() 变形

get_shapepoly() 获取形状多边形

使用事件

onclick() 当鼠标点击

onrelease() 当鼠标释放

ondrag() 当鼠标拖动

特殊海龟方法

begin_poly() 开始记录多边形

end_poly() 结束记录多边形

get_poly() 获取多边形

clone() 克隆

getturtle() | getpen() 获取海龟画笔

getscreen() 获取屏幕

setundobuffer() 设置撤消缓冲区

undobufferentries() 撤消缓冲区条目数

24.1.2.2. TurtleScreen/Screen 方法

窗口控制

bgcolor() 背景颜色

bgpic() 背景图片

clear() | clearscreen() 清屏

reset() | resetscreen() 重置

screensize() 屏幕大小

setworldcoordinates() 设置世界坐标系

动画控制

delay() 延迟

tracer() 追踪

update() 更新

使用屏幕事件

listen() 监听

onkey() | onkeyrelease() 当键盘按下并释放

onkeypress() 当键盘按下

onclick() | onscreenclick() 当点击屏幕

ontimer() 当达到定时

mainloop() | done() 主循环

设置与特殊方法

mode()

colormode() 颜色模式

getcanvas() 获取画布

getshapes() 获取形状

register_shape() | addshape() 添加形状

turtles() 所有海龟

window_height() 窗口高度

window_width() 窗口宽度

输入方法

textinput() 文本输入

numinput() 数字输入

Screen 专有方法

bye() 退出

exitonclick() 当点击时退出

setup() 设置

title() 标题

24.1.3. RawTurtle/Turtle 方法和对应函数

本节中的大部分示例都使用 Turtle 类的一个实例，命名为 turtle。

24.1.3.1. 海龟动作

turtle.forward(distance)

turtle.fd(distance)

参数

distance – 一个数值 (整型或浮点型)

海龟前进 distance 指定的距离，方向为海龟的朝向。

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)

turtle.back(distance)

turtle.bk(distance)

turtle.backward(distance)

参数

distance – 一个数值

海龟后退 distance 指定的距离，方向与海龟的朝向相反。不改变海龟的朝向。

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)

turtle.right(angle)

turtle.rt(angle)

参数

angle – 一个数值 (整型或浮点型)

海龟右转 angle 个单位。(单位默认为角度，但可通过 degrees() 和 radians() 函数改变设置。) 角度的正负由海龟模式确定，参见 mode()。

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0

turtle.left(angle)

turtle.lt(angle)

参数

angle – 一个数值 (整型或浮点型)

海龟左转 angle 个单位。(单位默认为角度，但可通过 degrees() 和 radians() 函数改变设置。) 角度的正负由海龟模式确定，参见 mode()。

>>> turtle.heading()
22.0
>>> turtle.left(45)
>>> turtle.heading()
67.0

turtle.goto(x, y=None)

turtle.setpos(x, y=None)

turtle.setposition(x, y=None)

参数

x – 一个数值或数值对/向量

y – 一个数值或 None

如果 y 为 None，x 应为一个表示坐标的数值对或 Vec2D 类对象 (例如 pos() 返回的对象).

海龟移动到一个绝对坐标。如果画笔已落下将会画线。不改变海龟的朝向。

>>> tp = turtle.pos()
>>> tp
(0.00,0.00)
>>> turtle.setpos(60,30)
>>> turtle.pos()
(60.00,30.00)
>>> turtle.setpos((20,80))
>>> turtle.pos()
(20.00,80.00)
>>> turtle.setpos(tp)
>>> turtle.pos()
(0.00,0.00)

turtle.setx(x)

参数

x – 一个数值 (整型或浮点型)

设置海龟的横坐标为 x，纵坐标保持不变。

>>> turtle.position()
(0.00,240.00)
>>> turtle.setx(10)
>>> turtle.position()
(10.00,240.00)

turtle.sety(y)

参数

y – 一个数值 (整型或浮点型)

设置海龟的纵坐标为 y，横坐标保持不变。

>>> turtle.position()
(0.00,40.00)
>>> turtle.sety(-10)
>>> turtle.position()
(0.00,-10.00)

turtle.setheading(to_angle)

turtle.seth(to_angle)

参数

to_angle – 一个数值 (整型或浮点型)

设置海龟的朝向为 to_angle。以下是以角度表示的几个常用方向：

标准模式

logo 模式

0 - 东

0 - 北

90 - 北

90 - 东

180 - 西

180 - 南

270 - 南

270 - 西

>>> turtle.setheading(90)
>>> turtle.heading()
90.0

turtle.home()

海龟移至初始坐标 (0,0)，并设置朝向为初始方向 (由海龟模式确定，参见 mode())。

>>> turtle.heading()
90.0
>>> turtle.position()
(0.00,-10.00)
>>> turtle.home()
>>> turtle.position()
(0.00,0.00)
>>> turtle.heading()
0.0

turtle.circle(radius, extent=None, steps=None)

参数

radius – 一个数值

extent – 一个数值 (或 None)

steps – 一个整型数 (或 None)

绘制一个 radius 指定半径的圆。圆心在海龟左边 radius 个单位；extent 为一个夹角，用来决定绘制圆的一部分。如未指定 extent*则绘制整个圆。如果 *extent 不是完整圆周，则以当前画笔位置为一个端点绘制圆弧。如果 radius 为正值则朝逆时针方向绘制圆弧，否则朝顺时针方向。最终海龟的朝向会依据 extent 的值而改变。

圆实际是以其内切正多边形来近似表示的，其边的数量由 steps 指定。如果未指定边数则会自动确定。此方法也可用来绘制正多边形。

>>> turtle.home()
>>> turtle.position()
(0.00,0.00)
>>> turtle.heading()
0.0
>>> turtle.circle(50)
>>> turtle.position()
(-0.00,0.00)
>>> turtle.heading()
0.0
>>> turtle.circle(120, 180)  # draw a semicircle
>>> turtle.position()
(0.00,240.00)
>>> turtle.heading()
180.0

turtle.dot(size=None, *color)

参数

size – 一个整型数 >= 1 (如果指定)

color – 一个颜色字符串或颜色数值元组

绘制一个直径为 size，颜色为 color 的圆点。如果 size 未指定，则直径取 pensize+4 和 2*pensize 中的较大值。

>>> turtle.home()
>>> turtle.dot()
>>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50)
>>> turtle.position()
(100.00,-0.00)
>>> turtle.heading()
0.0

turtle.stamp()

在海龟当前位置印制一个海龟形状。返回该印章的 stamp_id，印章可以通过调用 clearstamp(stamp_id) 来删除。

>>> turtle.color("blue")
>>> turtle.stamp()
11
>>> turtle.fd(50)

turtle.clearstamp(stampid)

参数

stampid – 一个整型数，必须是之前 stamp() 调用的返回值

删除 stampid 指定的印章。

>>> turtle.position()
(150.00,-0.00)
>>> turtle.color("blue")
>>> astamp = turtle.stamp()
>>> turtle.fd(50)
>>> turtle.position()
(200.00,-0.00)
>>> turtle.clearstamp(astamp)
>>> turtle.position()
(200.00,-0.00)

turtle.clearstamps(n=None)

参数

n – 一个整型数 (或 None)

删除全部或前/后 n 个海龟印章。如果 n 为 None 则删除全部印章，如果 n > 0 则删除前 n 个印章，否则如果 n < 0 则删除后 n 个印章。

>>> for i in range(8):
...     turtle.stamp(); turtle.fd(30)
13
14
15
16
17
18
19
20
>>> turtle.clearstamps(2)
>>> turtle.clearstamps(-2)
>>> turtle.clearstamps()

turtle.undo()

撤消 (或连续撤消) 最近的一个 (或多个) 海龟动作。可撤消的次数由撤消缓冲区的大小决定。

>>> for i in range(4):
...     turtle.fd(50); turtle.lt(80)
...
>>> for i in range(8):
...     turtle.undo()

turtle.speed(speed=None)

参数

speed – 一个 0..10 范围内的整型数或速度字符串 (见下)

设置海龟移动的速度为 0..10 表示的整型数值。如未指定参数则返回当前速度。

如果输入数值大于 10 或小于 0.5 则速度设为 0。速度字符串与速度值的对应关系如下:

“fastest”: 0 最快

“fast”: 10 快

“normal”: 6 正常

“slow”: 3 慢

“slowest”: 1 最慢

速度值从 1 到 10，画线和海龟转向的动画效果逐级加快。

注意: speed = 0 表示 没有 动画效果。forward/back 将使海龟向前/向后跳跃，同样的 left/right 将使海龟立即改变朝向。

>>> turtle.speed()
3
>>> turtle.speed('normal')
>>> turtle.speed()
6
>>> turtle.speed(9)
>>> turtle.speed()
9

24.1.3.2. 获取海龟的状态

turtle.position()

turtle.pos()

返回海龟当前的坐标 (x,y) (为 Vec2D 矢量类对象)。

>>> turtle.pos()
(440.00,-0.00)

turtle.towards(x, y=None)

参数

x – 一个数值或数值对/矢量，或一个海龟实例

y – 一个数值——如果 x 是一个数值，否则为 None

从海龟位置到由 (x,y)，矢量或另一海龟对应位置的连线的夹角。此数值依赖于海龟初始朝向 - 由 “standard”/”world” 或 “logo” 模式设置所决定)。

>>> turtle.goto(10, 10)
>>> turtle.towards(0,0)
225.0

turtle.xcor()

返回海龟的 x 坐标。

>>> turtle.home()
>>> turtle.left(50)
>>> turtle.forward(100)
>>> turtle.pos()
(64.28,76.60)
>>> print(round(turtle.xcor(), 5))
64.27876

turtle.ycor()

返回海龟的 y 坐标。

>>> turtle.home()
>>> turtle.left(60)
>>> turtle.forward(100)
>>> print(turtle.pos())
(50.00,86.60)
>>> print(round(turtle.ycor(), 5))
86.60254

turtle.heading()

返回海龟当前的朝向 (数值依赖于海龟模式参见 mode())。

>>> turtle.home()
>>> turtle.left(67)
>>> turtle.heading()
67.0

turtle.distance(x, y=None)

参数

x – 一个数值或数值对/矢量，或一个海龟实例

y – 一个数值——如果 x 是一个数值，否则为 None

返回从海龟位置到由 (x,y)，适量或另一海龟对应位置的单位距离。

>>> turtle.home()
>>> turtle.distance(30,40)
50.0
>>> turtle.distance((30,40))
50.0
>>> joe = Turtle()
>>> joe.forward(77)
>>> turtle.distance(joe)
77.0

24.1.3.3. 度量单位设置

turtle.degrees(fullcircle=360.0)

参数

fullcircle – 一个数值

设置角度的度量单位，即设置一个圆周为多少 “度”。默认值为 360 度。

>>> turtle.home()
>>> turtle.left(90)
>>> turtle.heading()
90.0

Change angle measurement unit to grad (also known as gon,
grade, or gradian and equals 1/100-th of the right angle.)
>>> turtle.degrees(400.0)
>>> turtle.heading()
100.0
>>> turtle.degrees(360)
>>> turtle.heading()
90.0

turtle.radians()

设置角度的度量单位为弧度。其值等于 degrees(2*math.pi)。

>>> turtle.home()
>>> turtle.left(90)
>>> turtle.heading()
90.0
>>> turtle.radians()
>>> turtle.heading()
1.5707963267948966

24.1.3.4. 画笔控制

24.1.3.4.1. 绘图状态

turtle.pendown()

turtle.pd()

turtle.down()

画笔落下 – 移动时将画线。

turtle.penup()

turtle.pu()

turtle.up()

画笔抬起 – 移动时不画线。

turtle.pensize(width=None)

turtle.width(width=None)

参数

width – 一个正数值

设置线条的粗细为 width 或返回该值。如果 resizemode 设为 “auto” 并且 turtleshape 为多边形，该多边形也以同样组细的线条绘制。如未指定参数，则返回当前的 pensize。

>>> turtle.pensize()
1
>>> turtle.pensize(10)   # from here on lines of width 10 are drawn

turtle.pen(pen=None, **pendict)

参数

pen – 一个包含部分或全部下列键的字典

pendict – 一个或多个以下列键为关键字的关键字参数

返回或设置画笔的属性，以一个包含以下键值对的 “画笔字典” 表示:

“shown”: True/False

“pendown”: True/False

“pencolor”: 颜色字符串或颜色元组

“fillcolor”: 颜色字符串或颜色元组

“pensize”: 正数值

“speed”: 0..10 范围内的数值

“resizemode”: “auto” 或 “user” 或 “noresize”

“stretchfactor”: (正数值, 正数值)

“outline”: 正数值

“tilt”: 数值

此字典可作为后续调用 pen() 时的参数，以恢复之前的画笔状态。另外还可将这些属性作为关键词参数提交。使用此方式可以用一条语句设置画笔的多个属性。

>>> turtle.pen(fillcolor="black", pencolor="red", pensize=10)
>>> sorted(turtle.pen().items())
[('fillcolor', 'black'), ('outline', 1), ('pencolor', 'red'),
 ('pendown', True), ('pensize', 10), ('resizemode', 'noresize'),
 ('shearfactor', 0.0), ('shown', True), ('speed', 9),
 ('stretchfactor', (1.0, 1.0)), ('tilt', 0.0)]
>>> penstate=turtle.pen()
>>> turtle.color("yellow", "")
>>> turtle.penup()
>>> sorted(turtle.pen().items())[:3]
[('fillcolor', ''), ('outline', 1), ('pencolor', 'yellow')]
>>> turtle.pen(penstate, fillcolor="green")
>>> sorted(turtle.pen().items())[:3]
[('fillcolor', 'green'), ('outline', 1), ('pencolor', 'red')]

turtle.isdown()

如果画笔落下返回 True，如果画笔抬起返回 False。

>>> turtle.penup()
>>> turtle.isdown()
False
>>> turtle.pendown()
>>> turtle.isdown()
True

24.1.3.4.2. 颜色控制

turtle.pencolor(*args)

返回或设置画笔颜色。

允许以下四种输入格式:

pencolor()

返回以颜色描述字符串或元组 (见示例) 表示的当前画笔颜色。可用作其他 color/pencolor/fillcolor 调用的输入。

pencolor(colorstring)

设置画笔颜色为 colorstring 指定的 Tk 颜色描述字符串，例如 "red"、"yellow" 或 "#33cc8c"。

pencolor((r, g, b))

设置画笔颜色为以 r, g, b 元组表示的 RGB 颜色。r, g, b 的取值范围应为 0..colormode，colormode 的值为 1.0 或 255 (参见 colormode())。

pencolor(r, g, b)

设置画笔颜色为以 r, g, b 表示的 RGB 颜色。r, g, b 的取值范围应为 0..colormode。

如果 turtleshape 为多边形，该多边形轮廓也以新设置的画笔颜色绘制。

>>> colormode()
1.0
>>> turtle.pencolor()
'red'
>>> turtle.pencolor("brown")
>>> turtle.pencolor()
'brown'
>>> tup = (0.2, 0.8, 0.55)
>>> turtle.pencolor(tup)
>>> turtle.pencolor()
(0.2, 0.8, 0.5490196078431373)
>>> colormode(255)
>>> turtle.pencolor()
(51.0, 204.0, 140.0)
>>> turtle.pencolor('#32c18f')
>>> turtle.pencolor()
(50.0, 193.0, 143.0)

turtle.fillcolor(*args)

返回或设置填充颜色。

允许以下四种输入格式:

fillcolor()

返回以颜色描述字符串或元组 (见示例) 表示的当前填充颜色。可用作其他 color/pencolor/fillcolor 调用的输入。

fillcolor(colorstring)

设置填充颜色为 colorstring 指定的 Tk 颜色描述字符串，例如 "red"、"yellow" 或 "#33cc8c"。

fillcolor((r, g, b))

设置填充颜色为以 r, g, b 元组表示的 RGB 颜色。r, g, b 的取值范围应为 0..colormode，colormode 的值为 1.0 或 255 (参见 colormode())。

fillcolor(r, g, b)

设置填充颜色为 r, g, b 表示的 RGB 颜色。r, g, b 的取值范围应为 0..colormode。

如果 turtleshape 为多边形，该多边形内部也以新设置的填充颜色填充。

>>> turtle.fillcolor("violet")
>>> turtle.fillcolor()
'violet'
>>> col = turtle.pencolor()
>>> col
(50.0, 193.0, 143.0)
>>> turtle.fillcolor(col)
>>> turtle.fillcolor()
(50.0, 193.0, 143.0)
>>> turtle.fillcolor('#ffffff')
>>> turtle.fillcolor()
(255.0, 255.0, 255.0)

turtle.color(*args)

返回或设置画笔颜色和填充颜色。

允许多种输入格式。使用如下 0 至 3 个参数:

color()

返回以一对颜色描述字符串或元组表示的当前画笔颜色和填充颜色，两者可分别由 pencolor() 和 fillcolor() 返回。

color(colorstring), color((r,g,b)), color(r,g,b)

输入格式与 pencolor() 相同，同时设置填充颜色和画笔颜色为指定的值。

color(colorstring1, colorstring2), color((r1,g1,b1), (r2,g2,b2))

相当于 pencolor(colorstring1) 加 fillcolor(colorstring2)，使用其他输入格式的方法也与之类似。

如果 turtleshape 为多边形，该多边形轮廓与填充也使用新设置的颜色。

>>> turtle.color("red", "green")
>>> turtle.color()
('red', 'green')
>>> color("#285078", "#a0c8f0")
>>> color()
((40.0, 80.0, 120.0), (160.0, 200.0, 240.0))

另参见: Screen 方法 colormode()。

24.1.3.4.3. 填充

turtle.filling()

返回填充状态 (填充为 True，否则为 False)。

>>> turtle.begin_fill()
>>> if turtle.filling():
...    turtle.pensize(5)
... else:
...    turtle.pensize(3)

turtle.begin_fill()

在绘制要填充的形状之前调用。

turtle.end_fill()

填充上次调用 begin_fill() 之后绘制的形状。

>>> turtle.color("black", "red")
>>> turtle.begin_fill()
>>> turtle.circle(80)
>>> turtle.end_fill()

24.1.3.4.4. 更多绘图控制

turtle.reset()

从屏幕中删除海龟的绘图，海龟回到原点并设置所有变量为默认值。

>>> turtle.goto(0,-22)
>>> turtle.left(100)
>>> turtle.position()
(0.00,-22.00)
>>> turtle.heading()
100.0
>>> turtle.reset()
>>> turtle.position()
(0.00,0.00)
>>> turtle.heading()
0.0

turtle.clear()

从屏幕中删除指定海龟的绘图。不移动海龟。海龟的状态和位置以及其他海龟的绘图不受影响。

turtle.write(arg, move=False, align="left", font=("Arial", 8, "normal"))

参数

arg – 要书写到 TurtleScreen 的对象

move – True/False

align – 字符串 “left”, “center” 或 “right”

font – 一个三元组 (fontname, fontsize, fonttype)

书写文本 - arg 指定的字符串 - 到当前海龟位置，align 指定对齐方式 (“left”, “center” 或 right”)，font 指定字体。如果 move 为 True，画笔会移动到文本的右下角。默认 move 为 False。

>>> turtle.write("Home = ", True, align="center")
>>> turtle.write((0,0), True)

24.1.3.5. 海龟状态

24.1.3.5.1. 可见性

turtle.hideturtle()

turtle.ht()

使海龟不可见。当你绘制复杂图形时这是个好主意，因为隐藏海龟可显著加快绘制速度。

>>> turtle.hideturtle()

turtle.showturtle()

turtle.st()

使海龟可见。

>>> turtle.showturtle()

turtle.isvisible()

如果海龟显示返回 True，如果海龟隐藏返回 False。

>>> turtle.hideturtle()
>>> turtle.isvisible()
False
>>> turtle.showturtle()
>>> turtle.isvisible()
True

24.1.3.5.2. 外观

turtle.shape(name=None)

参数

name – 一个有效的形状名字符串

设置海龟形状为 name 指定的形状名，如未指定形状名则返回当前的形状名。name 指定的形状名应存在于 TurtleScreen 的 shape 字典中。多边形的形状初始时有以下几种: “arrow”, “turtle”, “circle”, “square”, “triangle”, “classic”。要了解如何处理形状请参看 Screen 方法 register_shape()。

>>> turtle.shape()
'classic'
>>> turtle.shape("turtle")
>>> turtle.shape()
'turtle'

turtle.resizemode(rmode=None)

参数

rmode – 字符串 “auto”, “user”, “noresize” 其中之一

设置大小调整模式为以下值之一: “auto”, “user”, “noresize”。如未指定 rmode 则返回当前的大小调整模式。不同的大小调整模式的效果如下:

“auto”: 根据画笔粗细值调整海龟的外观。

“user”: 根据拉伸因子和轮廓宽度 (outline) 值调整海龟的外观，两者是由 shapesize() 设置的。

“noresize”: 不调整海龟的外观大小。

大小调整模式 (“user”) 会在 shapesize() 带参数调用时生效。

>>> turtle.resizemode()
'noresize'
>>> turtle.resizemode("auto")
>>> turtle.resizemode()
'auto'

turtle.shapesize(stretch_wid=None, stretch_len=None, outline=None)

turtle.turtlesize(stretch_wid=None, stretch_len=None, outline=None)

参数

stretch_wid – 正数值

stretch_len – 正数值

outline – 正数值

返回或设置画笔的属性 x/y-拉伸因子和/或轮廓。设置大小调整模式为 “user”。当且仅当大小调整模式设为 “user” 时海龟会基于其拉伸因子调整外观: stretch_wid 为垂直于其朝向的宽度拉伸因子，stretch_len 为平等于其朝向的长度拉伸因子，决定形状轮廓线的粗细。

>>> turtle.shapesize()
(1.0, 1.0, 1)
>>> turtle.resizemode("user")
>>> turtle.shapesize(5, 5, 12)
>>> turtle.shapesize()
(5, 5, 12)
>>> turtle.shapesize(outline=8)
>>> turtle.shapesize()
(5, 5, 8)

turtle.shearfactor(shear=None)

参数

shear – 数值 (可选)

设置或返回当前的剪切因子。根据 share 指定的剪切因子即剪切角度的切线来剪切海龟形状。不 改变海龟的朝向 (移动方向)。如未指定 shear 参数: 返回当前的剪切因子即剪切角度的切线，与海龟朝向平行的线条将被剪切。

>>> turtle.shape("circle")
>>> turtle.shapesize(5,2)
>>> turtle.shearfactor(0.5)
>>> turtle.shearfactor()
0.5

turtle.tilt(angle)

参数

angle – 一个数值

海龟形状自其当前的倾角转动 angle 指定的角度，但 不 改变海龟的朝向 (移动方向)。

>>> turtle.reset()
>>> turtle.shape("circle")
>>> turtle.shapesize(5,2)
>>> turtle.tilt(30)
>>> turtle.fd(50)
>>> turtle.tilt(30)
>>> turtle.fd(50)

turtle.settiltangle(angle)

参数

angle – 一个数值

旋转海龟形状使其指向 angle 指定的方向，忽略其当前的倾角，不 改变海龟的朝向 (移动方向)。

>>> turtle.reset()
>>> turtle.shape("circle")
>>> turtle.shapesize(5,2)
>>> turtle.settiltangle(45)
>>> turtle.fd(50)
>>> turtle.settiltangle(-45)
>>> turtle.fd(50)

3.1 版后已移除.

turtle.tiltangle(angle=None)

参数

angle – 一个数值 (可选)

设置或返回当前的倾角。如果指定 angle 则旋转海龟形状使其指向 angle 指定的方向，忽略其当前的倾角。不 改变海龟的朝向 (移动方向)。如果未指定 angle: 返回当前的倾角，即海龟形状的方向和海龟朝向 (移动方向) 之间的夹角。

>>> turtle.reset()
>>> turtle.shape("circle")
>>> turtle.shapesize(5,2)
>>> turtle.tilt(45)
>>> turtle.tiltangle()
45.0

turtle.shapetransform(t11=None, t12=None, t21=None, t22=None)

参数

t11 – 一个数值 (可选)

t12 – 一个数值 (可选)

t21 – 一个数值 (可选)

t12 – 一个数值 (可选)

设置或返回海龟形状的当前变形矩阵。

如不指定任何矩阵元素，则返回以4元素元组表示的变形矩阵。否则使用指定元素设置变形矩阵改变海龟形状，矩阵第一排的值为 t11, t12，第二排的值为 t21, t22。行列式 t11 * t22 - t12 * t21 的值不能为零，否则会出错。根据指定的矩阵修改拉伸因子，剪切因子和倾角。

>>> turtle = Turtle()
>>> turtle.shape("square")
>>> turtle.shapesize(4,2)
>>> turtle.shearfactor(-0.5)
>>> turtle.shapetransform()
(4.0, -1.0, -0.0, 2.0)

turtle.get_shapepoly()

返回以坐标值对元组表示的当前形状多边形。这可以用于定义一个新形状或一个复合形状的多个组成部分。

>>> turtle.shape("square")
>>> turtle.shapetransform(4, -1, 0, 2)
>>> turtle.get_shapepoly()
((50, -20), (30, 20), (-50, 20), (-30, -20))

24.1.3.6. 使用事件

turtle.onclick(fun, btn=1, add=None)

参数

fun – 一个函数，调用时将传入两个参数表示在画布上点击的坐标。

btn – 鼠标按钮编号，默认值为 1 (鼠标左键)

add – True 或 False – 如为 True 则将添加一个新绑定，否则将取代先前的绑定

将 fun 指定的函数绑定到鼠标点击此海龟事件。如果 fun 值为 None，则移除现有的绑定。以下为使用匿名海龟即过程式的示例:

>>> def turn(x, y):
...     left(180)
...
>>> onclick(turn)  # Now clicking into the turtle will turn it.
>>> onclick(None)  # event-binding will be removed

turtle.onrelease(fun, btn=1, add=None)

参数

fun – 一个函数，调用时将传入两个参数表示在画布上点击的坐标。

btn – 鼠标按钮编号，默认值为 1 (鼠标左键)

add – True 或 False – 如为 True 则将添加一个新绑定，否则将取代先前的绑定

将 fun 指定的函数绑定到在此海龟上释放鼠标按键事件。如果 fun 值为 None，则移除现有的绑定。

>>> class MyTurtle(Turtle):
...     def glow(self,x,y):
...         self.fillcolor("red")
...     def unglow(self,x,y):
...         self.fillcolor("")
...
>>> turtle = MyTurtle()
>>> turtle.onclick(turtle.glow)     # clicking on turtle turns fillcolor red,
>>> turtle.onrelease(turtle.unglow) # releasing turns it to transparent.

turtle.ondrag(fun, btn=1, add=None)

参数

fun – 一个函数，调用时将传入两个参数表示在画布上点击的坐标。

btn – 鼠标按钮编号，默认值为 1 (鼠标左键)

add – True 或 False – 如为 True 则将添加一个新绑定，否则将取代先前的绑定

将 fun 指定的函数绑定到在此海龟上移动鼠标事件。如果 fun 值为 None，则移除现有的绑定。

注: 在海龟上移动鼠标事件之前应先发生在此海龟上点击鼠标事件。

>>> turtle.ondrag(turtle.goto)

在此之后点击并拖动海龟可在屏幕上手绘线条 (如果画笔为落下)。

24.1.3.7. 特殊海龟方法

turtle.begin_poly()

开始记录多边形的顶点。当前海龟位置为多边形的第一个顶点。

turtle.end_poly()

停止记录多边形的顶点。当前海龟位置为多边形的最后一个顶点。它将连线到第一个顶点。

turtle.get_poly()

返回最新记录的多边形。

>>> turtle.home()
>>> turtle.begin_poly()
>>> turtle.fd(100)
>>> turtle.left(20)
>>> turtle.fd(30)
>>> turtle.left(60)
>>> turtle.fd(50)
>>> turtle.end_poly()
>>> p = turtle.get_poly()
>>> register_shape("myFavouriteShape", p)

turtle.clone()

创建并返回海龟的克隆体，具有相同的位置、朝向和海龟属性。

>>> mick = Turtle()
>>> joe = mick.clone()

turtle.getturtle()

turtle.getpen()

返回海龟对象自身。唯一合理的用法: 作为一个函数来返回 “匿名海龟”:

>>> pet = getturtle()
>>> pet.fd(50)
>>> pet
<turtle.Turtle object at 0x...>

turtle.getscreen()

返回作为海龟绘图场所的 TurtleScreen 类对象。该对象将可调用 TurtleScreen 方法。

>>> ts = turtle.getscreen()
>>> ts
<turtle._Screen object at 0x...>
>>> ts.bgcolor("pink")

turtle.setundobuffer(size)

参数

size – 一个整型数值或 None

设置或禁用撤消缓冲区。如果 size 为一个整型数则将开辟一个指定大小的空缓冲区。size 表示可使用 undo() 方法/函数撤消的海龟命令的次数上限。如果 size 为 None 则禁用撤消缓冲区。

>>> turtle.setundobuffer(42)

turtle.undobufferentries()

返回撤销缓冲区里的条目数。

>>> while undobufferentries():
...     undo()

24.1.3.8. 复合形状

要使用由多个不同颜色多边形构成的复合海龟形状，你必须明确地使用辅助类 Shape，具体步骤如下:

创建一个空 Shape 对象，类型为 “compound”。

按照需要使用 addcomponent() 方法向此对象添加多个部件。

例如:

>>> s = Shape("compound")
>>> poly1 = ((0,0),(10,-5),(0,10),(-10,-5))
>>> s.addcomponent(poly1, "red", "blue")
>>> poly2 = ((0,0),(10,-5),(-10,-5))
>>> s.addcomponent(poly2, "blue", "red")

接下来将 Shape 对象添加到 Screen 对象的形状列表并使用它:

>>> register_shape("myshape", s)
>>> shape("myshape")

注解

Shape 类在 register_shape() 方法的内部以多种方式使用。应用程序编写者 只有 在使用上述的复合形状时才需要处理 Shape 类。

24.1.4. TurtleScreen/Screen 方法及对应函数

本节中的大部分示例都使用 TurtleScreen 类的一个实例，命名为 screen。

24.1.4.1. 窗口控制

turtle.bgcolor(*args)

参数

args – 一个颜色字符串或三个取值范围 0..colormode 内的数值或一个取值范围相同的数值3元组

设置或返回 TurtleScreen 的背景颜色。

>>> screen.bgcolor("orange")
>>> screen.bgcolor()
'orange'
>>> screen.bgcolor("#800080")
>>> screen.bgcolor()
(128.0, 0.0, 128.0)

turtle.bgpic(picname=None)

参数

picname – 一个字符串, gif-文件名, "nopic", 或 None

设置背景图片或返回当前背景图片名称。如果 picname 为一个文件名，则将相应图片设为背景。如果 picname 为 "nopic"，则删除当前背景图片。如果 picname 为 None，则返回当前背景图片文件名。:

>>> screen.bgpic()
'nopic'
>>> screen.bgpic("landscape.gif")
>>> screen.bgpic()
"landscape.gif"

turtle.clear()

turtle.clearscreen()

从中删除所有海龟的全部绘图。将已清空的 TurtleScreen 重置为初始状态: 白色背景，无背景片，无事件绑定并启用追踪。

注解

此 TurtleScreen 方法作为全局函数时只有一个名字 clearscreen。全局函数 clear 所对应的是 Turtle 方法 clear。

turtle.reset()

turtle.resetscreen()

重置屏幕上的所有海龟为其初始状态。

注解

此 TurtleScreen 方法作为全局函数时只有一个名字 resetscreen。全局函数 reset 所对应的是 Turtle 方法 reset。

turtle.screensize(canvwidth=None, canvheight=None, bg=None)

参数

canvwidth – 正整型数，以像素表示画布的新宽度值

canvheight – 正整型数，以像素表示画面的新高度值

bg – 颜色字符串或颜色元组，新的背景颜色

如未指定任何参数，则返回当前的 (canvaswidth, canvasheight)。否则改变作为海龟绘图场所的画布大小。不改变绘图窗口。要观察画布的隐藏区域，可以使用滚动条。通过此方法可以令之前绘制于画布之外的图形变为可见。

>>> screen.screensize()
(400, 300)
>>> screen.screensize(2000,1500)
>>> screen.screensize()
(2000, 1500)

也可以用来寻找意外逃走的海龟 ;-)

turtle.setworldcoordinates(llx, lly, urx, ury)

参数

llx – 一个数值, 画布左下角的 x-坐标

lly – 一个数值, 画布左下角的 y-坐标

urx – 一个数值, 画面右上角的 x-坐标

ury – 一个数值, 画布右上角的 y-坐标

设置用户自定义坐标系并在必要时切换模式为 “world”。这会执行一次 screen.reset()。如果 “world” 模式已激活，则所有图形将根据新的坐标系重绘。

注意: 在用户自定义坐标系中，角度可能显得扭曲。

>>> screen.reset()
>>> screen.setworldcoordinates(-50,-7.5,50,7.5)
>>> for _ in range(72):
...     left(10)
...
>>> for _ in range(8):
...     left(45); fd(2)   # a regular octagon

24.1.4.2. 动画控制

turtle.delay(delay=None)

参数

delay – 正整型数

设置或返回以毫秒数表示的延迟值 delay。(这约等于连续两次画布刷新的间隔时间。) 绘图延迟越长，动画速度越慢。

可选参数:

>>> screen.delay()
10
>>> screen.delay(5)
>>> screen.delay()
5

turtle.tracer(n=None, delay=None)

参数

n – 非负整型数

delay – 非负整型数

启用/禁用海龟动画并设置刷新图形的延迟时间。如果指定 n 值，则只有每第 n 次屏幕刷新会实际执行。(可被用来加速复杂图形的绘制。) 如果调用时不带参数，则返回当前保存的 n 值。第二个参数设置延迟值 (参见 delay())。

>>> screen.tracer(8, 25)
>>> dist = 2
>>> for i in range(200):
...     fd(dist)
...     rt(90)
...     dist += 2

turtle.update()

执行一次 TurtleScreen 刷新。在禁用追踪时使用。

另参见 RawTurtle/Turtle 方法 speed()。

24.1.4.3. 使用屏幕事件

turtle.listen(xdummy=None, ydummy=None)

设置焦点到 TurtleScreen (以便接收按键事件)。使用两个 Dummy 参数以便能够传递 listen() 给 onclick 方法。

turtle.onkey(fun, key)

turtle.onkeyrelease(fun, key)

参数

fun – 一个无参数的函数或 None

key – 一个字符串: 键 (例如 “a”) 或键标 (例如 “space”)

绑定 fun 指定的函数到按键释放事件。如果 fun 值为 None，则移除事件绑定。注: 为了能够注册按键事件，TurtleScreen 必须得到焦点。(参见 method listen() 方法。)

>>> def f():
...     fd(50)
...     lt(60)
...
>>> screen.onkey(f, "Up")
>>> screen.listen()

turtle.onkeypress(fun, key=None)

参数

fun – 一个无参数的函数或 None

key – 一个字符串: 键 (例如 “a”) 或键标 (例如 “space”)

绑定 fun 指定的函数到指定键的按下事件。如未指定键则绑定到任意键的按下事件。注: 为了能够注册按键事件，必须得到焦点。(参见 listen() 方法。)

>>> def f():
...     fd(50)
...
>>> screen.onkey(f, "Up")
>>> screen.listen()

turtle.onclick(fun, btn=1, add=None)

turtle.onscreenclick(fun, btn=1, add=None)

参数

fun – 一个函数，调用时将传入两个参数表示在画布上点击的坐标。

btn – 鼠标按钮编号，默认值为 1 (鼠标左键)

add – True 或 False – 如为 True 则将添加一个新绑定，否则将取代先前的绑定

绑定 fun 指定的函数到鼠标点击屏幕事件。如果 fun 值为 None，则移除现有的绑定。

以下示例使用一个 TurtleScreen 实例 screen 和一个 Turtle 实例 turtle:

>>> screen.onclick(turtle.goto) # Subsequently clicking into the TurtleScreen will
>>>                             # make the turtle move to the clicked point.
>>> screen.onclick(None)        # remove event binding again

注解

此 TurtleScreen 方法作为全局函数时只有一个名字 onscreenclick。全局函数 onclick 所对应的是 Turtle 方法 onclick。

turtle.ontimer(fun, t=0)

参数

fun – 一个无参数的函数

t – 一个数值 >= 0

安装一个计时器，在 t 毫秒后调用 fun 函数。

>>> running = True
>>> def f():
...     if running:
...         fd(50)
...         lt(60)
...         screen.ontimer(f, 250)
>>> f()   ### makes the turtle march around
>>> running = False

turtle.mainloop()

turtle.done()

开始事件循环 - 调用 Tkinter 的 mainloop 函数。必须作为一个海龟绘图程序的结束语句。如果一个脚本是在以 -n 模式 (无子进程) 启动的 IDLE 中运行时 不可 使用 - 用于实现海龟绘图的交互功能。:

>>> screen.mainloop()

24.1.4.4. 输入方法

turtle.textinput(title, prompt)

参数

title – string

prompt – string

弹出一个对话框窗口用来输入一个字符串。形参 title 为对话框窗口的标题，prompt 为一条文本，通常用来提示要输入什么信息。返回输入的字符串。如果对话框被取消则返回 None。:

>>> screen.textinput("NIM", "Name of first player:")

turtle.numinput(title, prompt, default=None, minval=None, maxval=None)

参数

title – string

prompt – string

default – 数值 (可选)

minval – 数值 (可选)

maxval – 数值 (可选)

弹出一个对话框窗口用来输入一个数值。title 为对话框窗口的标题，prompt 为一条文本，通常用来描述要输入的数值信息。default: 默认值, minval: 可输入的最小值, maxval: 可输入的最大值。输入数值的必须在指定的 minval .. maxval 范围之内，否则将给出一条提示，对话框保持打开等待修改。返回输入的数值。如果对话框被取消则返回 None。:

>>> screen.numinput("Poker", "Your stakes:", 1000, minval=10, maxval=10000)

24.1.4.5. 设置与特殊方法

turtle.mode(mode=None)

参数

mode – 字符串 “standard”, “logo” 或 “world” 其中之一

设置海龟模式 (“standard”, “logo” 或 “world”) 并执行重置。如未指定模式则返回当前的模式。

“standard” 模式与旧的 turtle 兼容。”logo” 模式与大部分 Logo 海龟绘图兼容。”world” 模式使用用户自定义的 “世界坐标系”。注意: 在此模式下，如果 x/y 单位比率不等于 1 则角度会显得扭曲。

模式

初始海龟朝向

正数角度

“standard”

朝右 (东)

逆时针

“logo”

朝上 (北)

顺时针

>>> mode("logo")   # resets turtle heading to north
>>> mode()
'logo'

turtle.colormode(cmode=None)

参数

cmode – 数值 1.0 或 255 其中之一

返回颜色模式或将其设为 1.0 或 255。构成颜色三元组的 r, g, b 数值必须在 0..cmode 范围之内。

>>> screen.colormode(1)
>>> turtle.pencolor(240, 160, 80)
Traceback (most recent call last):
     ...
TurtleGraphicsError: bad color sequence: (240, 160, 80)
>>> screen.colormode()
1.0
>>> screen.colormode(255)
>>> screen.colormode()
255
>>> turtle.pencolor(240,160,80)

turtle.getcanvas()

返回此 TurtleScreen 的 Canvas 对象。供了解 Tkinter 的 Canvas 对象内部机理的人士使用。

>>> cv = screen.getcanvas()
>>> cv
<turtle.ScrolledCanvas object ...>

turtle.getshapes()

返回所有当前可用海龟形状的列表。

>>> screen.getshapes()
['arrow', 'blank', 'circle', ..., 'turtle']

turtle.register_shape(name, shape=None)

turtle.addshape(name, shape=None)

调用此函数有三种不同方式:

name 为一个 gif 文件的文件名， shape 为 None: 安装相应的图像形状。:

>>> screen.register_shape("turtle.gif")

注解

当海龟转向时图像形状 不会 转动，因此无法显示海龟的朝向!

name 为指定的字符串，shape 为由坐标值对构成的元组: 安装相应的多边形形状。

>>> screen.register_shape("triangle", ((5,-3), (0,5), (-5,-3)))

name 为指定的字符串， 为一个 (复合) Shape 类对象: 安装相应的复合形状。

将一个海龟形状加入 TurtleScreen 的形状列表。只有这样注册过的形状才能通过执行 shape(shapename) 命令来使用。

turtle.turtles()

返回屏幕上的海龟列表。

>>> for turtle in screen.turtles():
...     turtle.color("red")

turtle.window_height()

返回海龟窗口的高度。:

>>> screen.window_height()
480

turtle.window_width()

返回海龟窗口的宽度。:

>>> screen.window_width()
640

24.1.4.6. Screen 专有方法, 而非继承自 TurtleScreen

turtle.bye()

关闭海龟绘图窗口。

turtle.exitonclick()

将 bye() 方法绑定到 Screen 上的鼠标点击事件。

如果配置字典中 “using_IDLE” 的值为 False (默认值) 则同时进入主事件循环。注: 如果启动 IDLE 时使用了 -n 开关 (无子进程)，turtle.cfg 中此数值应设为 True。在此情况下 IDLE 本身的主事件循环同样会作用于客户脚本。

turtle.setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"])

设置主窗口的大小和位置。默认参数值保存在配置字典中，可通过 turtle.cfg 文件进行修改。

参数

width – 如为一个整型数值，表示大小为多少像素，如为一个浮点数值，则表示屏幕的占比；默认为屏幕的 50%

height – 如为一个整型数值，表示高度为多少像素，如为一个浮点数值，则表示屏幕的占比；默认为屏幕的 75%

startx – 如为正值，表示初始位置距离屏幕左边缘多少像素，负值表示距离右边缘，None 表示窗口水平居中

starty – 如为正值，表示初始位置距离屏幕上边缘多少像素，负值表示距离下边缘，None 表示窗口垂直居中

>>> screen.setup (width=200, height=200, startx=0, starty=0)
>>>              # sets window to 200x200 pixels, in upper left of screen
>>> screen.setup(width=.75, height=0.5, startx=None, starty=None)
>>>              # sets window to 75% of screen by 50% of screen and centers

turtle.title(titlestring)

参数

titlestring – 一个字符串，显示为海龟绘图窗口的标题栏文本

设置海龟窗口标题为 titlestring 指定的文本。

>>> screen.title("Welcome to the turtle zoo!")

24.1.5. 公共类

class turtle.RawTurtle(canvas)

class turtle.RawPen(canvas)

参数

canvas – 一个 tkinter.Canvas , ScrolledCanvas 或 TurtleScreen 类对象

创建一个海龟。海龟对象具有 “Turtle/RawTurtle 方法” 一节所述的全部方法。

class turtle.Turtle

RawTurtle 的子类，具有相同的接口，但其绘图场所为默认的 Screen 类对象，在首次使用时自动创建。

class turtle.TurtleScreen(cv)

参数

cv – 一个 tkinter.Canvas 类对象

提供面向屏幕的方法例如 setbg() 等。说明见上文。

class turtle.Screen

TurtleScreen 的子类，增加了四个方法.

class turtle.ScrolledCanvas(master)

参数

master – 可容纳 ScrolledCanvas 的 Tkinter 部件，即添加了滚动条的 Tkinter-canvas

由 Screen 类使用，使其能够自动提供一个 ScrolledCanvas 作为海龟的绘图场所。

class turtle.Shape(type_, data)

参数

type_ – 字符串 “polygon”, “image”, “compound” 其中之一

实现形状的数据结构。(type_, data) 必须遵循以下定义:

type_

data

“polygon”

一个多边形元组，即由坐标值对构成的元组

“image”

一个图片 (此形式仅限内部使用!)

“compound”

None (复合形状必须使用 addcomponent() 方法来构建)

addcomponent(poly, fill, outline=None)

参数

poly – 一个多边形，即由数值对构成的元组

fill – 一种颜色，将用来填充 poly 指定的多边形

outline – 一种颜色，用于多边形的轮廓 (如有指定)

示例:

>>> poly = ((0,0),(10,-5),(0,10),(-10,-5))
>>> s = Shape("compound")
>>> s.addcomponent(poly, "red", "blue")
>>> # ... add more components and then use register_shape()

参见 复合形状。

class turtle.Vec2D(x, y)

一个二维矢量类，用来作为实现海龟绘图的辅助类。也可能在海龟绘图程序中使用。派生自元组，因此矢量也属于元组!

提供的运算 (a, b 为矢量, k 为数值):

a + b 矢量加法

a - b 矢量减法

a * b 内积

k * a 和 a * k 与标量相乘

abs(a) a 的绝对值

a.rotate(angle) 旋转

24.1.6. 帮助与配置

24.1.6.1. 如何使用帮助

Screen 和 Turtle 类的公用方法以文档字符串提供了详细的文档。因此可以利用 Python 帮助工具获取这些在线帮助信息:

当使用 IDLE 时，输入函数/方法调用将弹出工具提示显示其签名和文档字符串的头几行。

对文法或函数调用 help() 将显示其文档字符串:

>>> help(Screen.bgcolor)
Help on method bgcolor in module turtle:

bgcolor(self, *args) unbound turtle.Screen method
    Set or return backgroundcolor of the TurtleScreen.

    Arguments (if given): a color string or three numbers
    in the range 0..colormode or a 3-tuple of such numbers.

      >>> screen.bgcolor("orange")
      >>> screen.bgcolor()
      "orange"
      >>> screen.bgcolor(0.5,0,0.5)
      >>> screen.bgcolor()
      "#800080"

>>> help(Turtle.penup)
Help on method penup in module turtle:

penup(self) unbound turtle.Turtle method
    Pull the pen up -- no drawing when moving.

    Aliases: penup | pu | up

    No argument

    >>> turtle.penup()

方法对应函数的文档字符串的形式会有一些修改:

>>> help(bgcolor)
Help on function bgcolor in module turtle:

bgcolor(*args)
    Set or return backgroundcolor of the TurtleScreen.

    Arguments (if given): a color string or three numbers
    in the range 0..colormode or a 3-tuple of such numbers.

    Example::

      >>> bgcolor("orange")
      >>> bgcolor()
      "orange"
      >>> bgcolor(0.5,0,0.5)
      >>> bgcolor()
      "#800080"

>>> help(penup)
Help on function penup in module turtle:

penup()
    Pull the pen up -- no drawing when moving.

    Aliases: penup | pu | up

    No argument

    Example:
    >>> penup()

这些修改版文档字符串是在导入时与方法对应函数的定义一起自动生成的。

24.1.6.2. 文档字符串翻译为不同的语言

可使用工具创建一个字典，键为方法名，值为 Screen 和 Turtle 类公共方法的文档字符串。

turtle.write_docstringdict(filename="turtle_docstringdict")

参数

filename – 一个字符串，表示文件名

创建文档字符串字典并将其写入 filename 指定的 Python 脚本文件。此函数必须显示地调用 (海龟绘图类并不使用此函数)。文档字符串字典将被写入到 Python 脚本文件 filename.py。该文件可作为模板用来将文档字符串翻译为不同语言。

如果你 (或你的学生) 想使用本国语言版本的 turtle 在线帮助，你必须翻译文档字符串并保存结果文件，例如 turtle_docstringdict_german.py.

如果你在 turtle.cfg 文件中加入了相应的条目，此字典将在导入模块时被读取并替代原有的英文版文档字符串。

在撰写本文档时已经有了德语和意大利语版的文档字符串字典。(更多需求请联系 glingl@aon.at)

24.1.6.3. 如何配置 Screen 和 Turtle

内置的默认配置是模仿旧 turtle 模块的外观和行为，以便尽可能地与其保持兼容。

如果你想使用不同的配置，以便更好地反映此模块的特性或是更适合你的需求，例如在课堂中使用，你可以准备一个配置文件 turtle.cfg，该文件将在导入模块时被读取并根据其中的设定修改模块配置。

内置的配置对应以下的 turtle.cfg:

width = 0.5
height = 0.75
leftright = None
topbottom = None
canvwidth = 400
canvheight = 300
mode = standard
colormode = 1.0
delay = 10
undobuffersize = 1000
shape = classic
pencolor = black
fillcolor = black
resizemode = noresize
visible = True
language = english
exampleturtle = turtle
examplescreen = screen
title = Python Turtle Graphics
using_IDLE = False

选定条目的简短说明:

开头的四行对应 Screen.setup() 方法的参数。

第 5 和 6 行对应 Screen.screensize() 方法的参数。

shape 可以是任何内置形状，即: arrow, turtle 等。更多信息可用 help(shape) 查看。

如果你想使用无填充色 (即令海龟变透明)，你必须写 fillcolor = "" (但 cfg 文件中所有非空字符串都不可加引号)。

如果你想令海龟反映其状态，你必须使用 resizemode = auto。

如果你设置语言例如 language = italian 则文档字符串字典 turtle_docstringdict_italian.py 将在导入模块时被加载 (如果导入路径即 turtle 的目录中存在此文件。

exampleturtle 和 examplescreen 条目定义了相应对象在文档字符串中显示的名称。方法文档字符串转换为函数文档字符串时将从文档字符串中删去这些名称。

using_IDLE: 如果你经常使用 IDLE 并启用其 -n 开关 (“无子进程”) 则应将此项设为 True，这将阻止 exitonclick() 进入主事件循环。

turtle.cfg 文件可以保存于 turtle 所在目录，当前工作目录也可以有一个同名文件。后者会重载覆盖前者的设置。

Lib/turtledemo 目录中也有一个 turtle.cfg 文件。你可以将其作为示例进行研究，并在运行演示时查看其作用效果 (但最好不要在演示查看器中运行)。

24.1.7. turtledemo — 演示脚本集

turtledemo 包汇集了一组演示脚本。这些脚本可以通过以下命令打开所提供的演示查看器运行和查看:

python -m turtledemo

此外，你也可以单独运行其中的演示脚本。例如，:

python -m turtledemo.bytedesign

turtledemo 包目录中的内容:

一个演示查看器 __main__.py，可用来查看脚本的源码并即时运行。

多个脚本文件，演示 turtle 模块的不同特性。所有示例可通过 Examples 菜单打开。也可以单独运行每个脚本。

一个 turtle.cfg 文件，作为说明如何编写并使用模块配置文件的示例模板。

演示脚本清单如下:

名称

描述

相关特性

bytedesign

复杂的传统海龟绘图模式

tracer(), delay, update()

chaos

绘制 Verhulst 动态模型，演示通过计算机的运算可能会生成令人惊叹的结果

世界坐标系

clock

绘制模拟时钟显示本机的当前时间

海龟作为表针, ontimer

colormixer

试验 r, g, b 颜色模式

ondrag() 当鼠标拖动

forest

绘制 3 棵广度优先树

随机化

fractalcurves

绘制 Hilbert & Koch 曲线

递归

lindenmayer

文化数学 (印度装饰艺术)

L-系统

minimal_hanoi

汉诺塔

矩形海龟作为汉诺盘 (shape, shapesize)

nim

玩经典的“尼姆”游戏，开始时有三堆小棒，与电脑对战。

海龟作为小棒，事件驱动 (鼠标, 键盘)

paint

超极简主义绘画程序

onclick() 当鼠标点击

peace

初级技巧

海龟: 外观与动画

penrose

非周期性地使用风筝和飞镖形状铺满平面

stamp() 印章

planet_and_moon

模拟引力系统

复合开关, Vec2D 类

round_dance

两两相对并不断旋转舞蹈的海龟

复合形状, clone shapesize, tilt, get_shapepoly, update

sorting_animate

动态演示不同的排序方法

简单对齐, 随机化

tree

一棵 (图形化的) 广度优先树 (使用生成器)

clone() 克隆

two_canvases

简单设计

两块画布上的海龟

wikipedia

一个来自介绍海龟绘图的维基百科文章的图案

clone(), undo()

yinyang

另一个初级示例

circle() 画圆

祝你玩得开心！

24.1.8. Python 2.6 之后的变化

Turtle.tracer(), Turtle.window_width() 和 Turtle.window_height() 方法已被去除。具有这些名称和功能的方法现在只限于 Screen 类的方法。但其对应的函数仍然可用。(实际上在 Python 2.6 中这些方法就已经只是从对应的 TurtleScreen/Screen 类的方法复制而来。)

Turtle.fill() 方法已被去除。begin_fill() 和 end_fill() 的行为则有细微改变: 现在每个填充过程必须以一个 end_fill() 调用来结束。

新增了一个 Turtle.filling() 方法。该方法返回一个布尔值: 如果填充过程正在进行为 True，否则为 False。此行为相当于 Python 2.6 中不带参数的 fill() 调用。

24.1.9. Python 3.0 之后的变化

新增了 Turtle.shearfactor(), Turtle.shapetransform() 和 Turtle.get_shapepoly() 方法。这样就可以使用所有标准线性变换来调整海龟形状。Turtle.tiltangle() 的功能已被加强: 现在可被用来获取或设置倾角。Turtle.settiltangle() 已弃用。

新增了 Screen.onkeypress() 方法作为对 Screen.onkey() 的补充，实际就是将行为绑定到 keyrelease 事件。后者相应增加了一个别名: Screen.onkeyrelease()。

新增了 Screen.mainloop() 方法。这样当仅需使用 Screen 和 Turtle 对象时不需要再额外导入 mainloop()。

新增了两个方法 Screen.textinput() 和 Screen.numinput()。用来弹出对话框接受输入并分别返回字符串和数值。

两个新的示例脚本 tdemo_nim.py 和 tdemo_round_dance.py 被加入到 Lib/turtledemo 目录中。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

24. 程序框架 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

24. 程序框架 »

|

24.2. cmd — 支持面向行的命令解释器

源代码: Lib/cmd.py [https://github.com/python/cpython/tree/3.6/Lib/cmd.py]

Cmd 类提供简单框架用于编写面向行的命令解释器。 这些通常对测试工具，管理工具和原型有用，这些工具随后将被包含在更复杂的接口中。

class cmd.Cmd(completekey='tab', stdin=None, stdout=None)

一个 Cmd 实例或子类实例是面向行的解释器框架结构。 实例化 Cmd 本身是没有充分理由的， 它作为自定义解释器类的超类是非常有用的为了继承 Cmd 的方法并且封装动作方法。

可选参数 completekey 是完成键的 readline 名称；默认是 Tab 。如果 completekey 不是 None 并且 readline 是可用的， 命令完成会自动完成。

可选参数 stdin 和 stdout 指定了Cmd实例或子类实例将用于输入和输出的输入和输出文件对象。如果没有指定，他们将默认为 sys.stdin 和 sys.stdout 。

如果你想要使用一个给定的 stdin ，确保将实例的 use_rawinput 属性设置为 False ，否则 stdin 将被忽略。

24.2.1. Cmd 对象

Cmd 实例有下列方法：

Cmd.cmdloop(intro=None)

反复发出提示，接受输入，从收到的输入中解析出一个初始前缀，并分派给操作方法，将其余的行作为参数传递给它们。

可选参数是在第一个提示之前发布的横幅或介绍字符串（这将覆盖 intro 类属性）。

如果 readline 继承模块被加载，输入将自动继承类似 bash的历史列表编辑（例如， Control-P 滚动回到最后一个命令， Control-N 转到下一个命令，以 Control-F 非破坏性的方式向右 Control-B 移动光标，破坏性地等）。

输入的文件结束符被作为字符串传回 'EOF' 。

解释器实例将会识别命令名称 foo 当且仅当它有方法 do_foo() 。有一个特殊情况，分派始于字符 '?' 的行到方法 do_help() 。另一种特殊情况，分派始于字符 '!' 的行到方法 do_shell() （如果定义了这个方法）

这个方法将返回当 postcmd() 方法返回一个真值 。参数 stop 到 postcmd() 是命令对应的返回值 do_*() 的方法。

如果激活了完成，全部命令将会自动完成，并且通过调用 complete_foo() 参数 text , line, begidx ,和 endidx 完成全部命令参数。 text 是我们试图匹配的字符串前缀，所有返回的匹配项必须以它为开头。 line 是删除了前导空格的当前的输入行， begidx 和 endidx 是前缀文本的开始和结束索引。，可以用于根据参数位置提供不同的完成。

所有 Cmd 的子类继承一个预定义 do_help() 。 这个方法使用参数 'bar' 调用， 调用对应的方法 help_bar() ，如果不存在，打印 do_bar() 的文档字符串，如果可用。没有参数的情况下， do_help() 方法会列出所有可用的帮助主题 （即所有具有相应的 help_*() 方法或命令的 文档字符串），也会列举所有未被记录的命令。

Cmd.onecmd(str)

解释该参数，就好像它是为响应提示而键入的一样。 这可能会被覆盖，但通常不应该被覆盖; 请参阅： precmd() 和 postcmd() 方法，用于执行有用的挂钩。 返回值是一个标志，指示解释器对命令的解释是否应该停止。 如果命令 str 有一个 do_*() 方法，则返回该方法的返回值，否则返回 default() 方法的返回值。

Cmd.emptyline()

在响应提示输入空行时调用的方法。如果此方法未被覆盖，则重复输入的最后一个非空命令。

Cmd.default(line)

当命令前缀不能被识别的时候在输入行调用的方法。如果此方法未被覆盖，它将输出一个错误信息并返回。

Cmd.completedefault(text, line, begidx, endidx)

当没有特定于命令的 complete_*() 方法可用时，调用此方法完成输入行。默认情况下，它返回一个空列表。

Cmd.precmd(line)

钩方法在命令行 line 被解释之前执行，但是在输入提示被生成和发出后。这个方法是一个在 Cmd 中的存根；它的存在是为了被子类覆盖。返回值被用作 onecmd() 方法执行的命令； precmd() 的实现或许会重写命令或者简单的返回 line 不变。

Cmd.postcmd(stop, line)

钩方法只在命令调度完成后执行。这个方法是一个在 Cmd 中的存根；它的存在是为了子类被覆盖。 line 是被执行的命令行， stop 是一个表示在调用 postcmd() 之后是否终止执行的标志；这将作为 onecmd() 方法的返回值。这个方法的返回值被用作与 stop 相关联的内部标志的新值；返回 false 将导致解释继续。

Cmd.preloop()

钩方法当 cmdloop() 被调用时执行一次。方法是一个在 Cmd 中的存根；它的存在是为了被子类覆盖。

Cmd.postloop()

钩方法在 cmdloop() 即将返回时执行一次。这个方法是一个在 Cmd 中的存根；塔顶存在是为了被子类覆盖。

Instances of Cmd subclasses have some public instance variables:

Cmd.prompt

发出提示以请求输入。

Cmd.identchars

接受命令前缀的字符串。

Cmd.lastcmd

看到最后一个非空命令前缀。

Cmd.cmdqueue

排队的输入行列表。当需要新的输入时，在 cmdloop() 中检查 cmdqueue 列表；如果它不是空的，它的元素将被按顺序处理，就像在提示符处输入一样。

Cmd.intro

要作为简介或横幅发出的字符串。 可以通过给 cmdloop() 方法一个参数来覆盖它。

Cmd.doc_header

如果帮助输出具有记录命令的段落，则发出头文件。

Cmd.misc_header

如果帮助输出其他帮助主题的部分（即与 do_*() 方法没有关联的 help_*() 方法），则发出头文件。

Cmd.undoc_header

如果帮助输出未被记录命令的部分（即与 help_*() 方法没有关联的 do_*() 方法），则发出头文件。

Cmd.ruler

用于在帮助信息标题的下方绘制分隔符的字符，如果为空，则不绘制标尺线。这个字符默认是 '=' 。

Cmd.use_rawinput

这是一个标志，默认为 true 。如果为 true ，, cmdloop() 使用 input() 先是提示并且阅读下一个命令；如果为 false ， sys.stdout.write() 和 sys.stdin.readline() 被使用。（这意味着解释器将会自动支持类似于 Emacs的行编辑和命令历史记录按键操作，通过导入 readline 在支持它的系统上。）

24.2.2. Cmd 例子

The cmd module is mainly useful for building custom shells that let a user work with a program interactively.

这部分提供了一个简单的例子来介绍如何使用一部分在 turtle 模块中的命令构建一个 shell 。

基础的 turtle 命令比如 forward() 被添加进一个 Cmd 子类，方法名为 do_forward() 。参数被转换成数字并且分发至 turtle 模块中。 docstring 是 shell 提供的帮助实用程序。

例子也包含使用 precmd() 方法实现基础的记录和回放的功能，这个方法负责将输入转换为小写并且将命令写入文件。 do_playback() 方法读取文件并添加记录命令至 cmdqueue 用于即时回放:

import cmd, sys
from turtle import *

class TurtleShell(cmd.Cmd):
    intro = 'Welcome to the turtle shell.   Type help or ? to list commands.\n'
    prompt = '(turtle) '
    file = None

    # ----- basic turtle commands -----
    def do_forward(self, arg):
        'Move the turtle forward by the specified distance:  FORWARD 10'
        forward(*parse(arg))
    def do_right(self, arg):
        'Turn turtle right by given number of degrees:  RIGHT 20'
        right(*parse(arg))
    def do_left(self, arg):
        'Turn turtle left by given number of degrees:  LEFT 90'
        left(*parse(arg))
    def do_goto(self, arg):
        'Move turtle to an absolute position with changing orientation.  GOTO 100 200'
        goto(*parse(arg))
    def do_home(self, arg):
        'Return turtle to the home position:  HOME'
        home()
    def do_circle(self, arg):
        'Draw circle with given radius an options extent and steps:  CIRCLE 50'
        circle(*parse(arg))
    def do_position(self, arg):
        'Print the current turtle position:  POSITION'
        print('Current position is %d %d\n' % position())
    def do_heading(self, arg):
        'Print the current turtle heading in degrees:  HEADING'
        print('Current heading is %d\n' % (heading(),))
    def do_color(self, arg):
        'Set the color:  COLOR BLUE'
        color(arg.lower())
    def do_undo(self, arg):
        'Undo (repeatedly) the last turtle action(s):  UNDO'
    def do_reset(self, arg):
        'Clear the screen and return turtle to center:  RESET'
        reset()
    def do_bye(self, arg):
        'Stop recording, close the turtle window, and exit:  BYE'
        print('Thank you for using Turtle')
        self.close()
        bye()
        return True

    # ----- record and playback -----
    def do_record(self, arg):
        'Save future commands to filename:  RECORD rose.cmd'
        self.file = open(arg, 'w')
    def do_playback(self, arg):
        'Playback commands from a file:  PLAYBACK rose.cmd'
        self.close()
        with open(arg) as f:
            self.cmdqueue.extend(f.read().splitlines())
    def precmd(self, line):
        line = line.lower()
        if self.file and 'playback' not in line:
            print(line, file=self.file)
        return line
    def close(self):
        if self.file:
            self.file.close()
            self.file = None

def parse(arg):
    'Convert a series of zero or more numbers to an argument tuple'
    return tuple(map(int, arg.split()))

if __name__ == '__main__':
    TurtleShell().cmdloop()

这是一个示例会话，其中 turtle shell 显示帮助功能，使用空行重复命令，以及简单的记录和回放功能：

Welcome to the turtle shell.   Type help or ? to list commands.

(turtle) ?

Documented commands (type help <topic>):
========================================
bye     color    goto     home  playback  record  right
circle  forward  heading  left  position  reset   undo

(turtle) help forward
Move the turtle forward by the specified distance:  FORWARD 10
(turtle) record spiral.cmd
(turtle) position
Current position is 0 0

(turtle) heading
Current heading is 0

(turtle) reset
(turtle) circle 20
(turtle) right 30
(turtle) circle 40
(turtle) right 30
(turtle) circle 60
(turtle) right 30
(turtle) circle 80
(turtle) right 30
(turtle) circle 100
(turtle) right 30
(turtle) circle 120
(turtle) right 30
(turtle) circle 120
(turtle) heading
Current heading is 180

(turtle) forward 100
(turtle)
(turtle) right 90
(turtle) forward 100
(turtle)
(turtle) right 90
(turtle) forward 400
(turtle) right 90
(turtle) forward 500
(turtle) right 90
(turtle) forward 400
(turtle) right 90
(turtle) forward 300
(turtle) playback spiral.cmd
Current position is 0 0

Current heading is 0

Current heading is 180

(turtle) bye
Thank you for using Turtle

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

24. 程序框架 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

24. 程序框架 »

|

24.3. shlex — Simple lexical analysis

Source code: Lib/shlex.py [https://github.com/python/cpython/tree/3.6/Lib/shlex.py]

The shlex class makes it easy to write lexical analyzers for simple syntaxes resembling that of the Unix shell. This will often be useful for writing minilanguages, (for example, in run control files for Python applications) or for parsing quoted strings.

The shlex module defines the following functions:

shlex.split(s, comments=False, posix=True)

Split the string s using shell-like syntax. If comments is False (the default), the parsing of comments in the given string will be disabled (setting the commenters attribute of the shlex instance to the empty string). This function operates in POSIX mode by default, but uses non-POSIX mode if the posix argument is false.

注解

Since the split() function instantiates a shlex instance, passing None for s will read the string to split from standard input.

shlex.quote(s)

Return a shell-escaped version of the string s. The returned value is a string that can safely be used as one token in a shell command line, for cases where you cannot use a list.

This idiom would be unsafe:

>>> filename = 'somefile; rm -rf ~'
>>> command = 'ls -l {}'.format(filename)
>>> print(command)  # executed by a shell: boom!
ls -l somefile; rm -rf ~

quote() lets you plug the security hole:

>>> command = 'ls -l {}'.format(quote(filename))
>>> print(command)
ls -l 'somefile; rm -rf ~'
>>> remote_command = 'ssh home {}'.format(quote(command))
>>> print(remote_command)
ssh home 'ls -l '"'"'somefile; rm -rf ~'"'"''

The quoting is compatible with UNIX shells and with split():

>>> remote_command = split(remote_command)
>>> remote_command
['ssh', 'home', "ls -l 'somefile; rm -rf ~'"]
>>> command = split(remote_command[-1])
>>> command
['ls', '-l', 'somefile; rm -rf ~']

3.3 新版功能.

The shlex module defines the following class:

class shlex.shlex(instream=None, infile=None, posix=False, punctuation_chars=False)

A shlex instance or subclass instance is a lexical analyzer object. The initialization argument, if present, specifies where to read characters from. It must be a file-/stream-like object with read() and readline() methods, or a string. If no argument is given, input will be taken from sys.stdin. The second optional argument is a filename string, which sets the initial value of the infile attribute. If the instream argument is omitted or equal to sys.stdin, this second argument defaults to “stdin”. The posix argument defines the operational mode: when posix is not true (default), the shlex instance will operate in compatibility mode. When operating in POSIX mode, shlex will try to be as close as possible to the POSIX shell parsing rules. The punctuation_chars argument provides a way to make the behaviour even closer to how real shells parse. This can take a number of values: the default value, False, preserves the behaviour seen under Python 3.5 and earlier. If set to True, then parsing of the characters ();<>|& is changed: any run of these characters (considered punctuation characters) is returned as a single token. If set to a non-empty string of characters, those characters will be used as the punctuation characters. Any characters in the wordchars attribute that appear in punctuation_chars will be removed from wordchars. See Improved Compatibility with Shells for more information.

在 3.6 版更改: The punctuation_chars parameter was added.

参见

Module configparser

Parser for configuration files similar to the Windows .ini files.

24.3.1. shlex Objects

A shlex instance has the following methods:

shlex.get_token()

Return a token. If tokens have been stacked using push_token(), pop a token off the stack. Otherwise, read one from the input stream. If reading encounters an immediate end-of-file, eof is returned (the empty string ('') in non-POSIX mode, and None in POSIX mode).

shlex.push_token(str)

Push the argument onto the token stack.

shlex.read_token()

Read a raw token. Ignore the pushback stack, and do not interpret source requests. (This is not ordinarily a useful entry point, and is documented here only for the sake of completeness.)

shlex.sourcehook(filename)

When shlex detects a source request (see source below) this method is given the following token as argument, and expected to return a tuple consisting of a filename and an open file-like object.

Normally, this method first strips any quotes off the argument. If the result is an absolute pathname, or there was no previous source request in effect, or the previous source was a stream (such as sys.stdin), the result is left alone. Otherwise, if the result is a relative pathname, the directory part of the name of the file immediately before it on the source inclusion stack is prepended (this behavior is like the way the C preprocessor handles #include "file.h").

The result of the manipulations is treated as a filename, and returned as the first component of the tuple, with open() called on it to yield the second component. (Note: this is the reverse of the order of arguments in instance initialization!)

This hook is exposed so that you can use it to implement directory search paths, addition of file extensions, and other namespace hacks. There is no corresponding ‘close’ hook, but a shlex instance will call the close() method of the sourced input stream when it returns EOF.

For more explicit control of source stacking, use the push_source() and pop_source() methods.

shlex.push_source(newstream, newfile=None)

Push an input source stream onto the input stack. If the filename argument is specified it will later be available for use in error messages. This is the same method used internally by the sourcehook() method.

shlex.pop_source()

Pop the last-pushed input source from the input stack. This is the same method used internally when the lexer reaches EOF on a stacked input stream.

shlex.error_leader(infile=None, lineno=None)

This method generates an error message leader in the format of a Unix C compiler error label; the format is '"%s", line %d: ', where the %s is replaced with the name of the current source file and the %d with the current input line number (the optional arguments can be used to override these).

This convenience is provided to encourage shlex users to generate error messages in the standard, parseable format understood by Emacs and other Unix tools.

Instances of shlex subclasses have some public instance variables which either control lexical analysis or can be used for debugging:

shlex.commenters

The string of characters that are recognized as comment beginners. All characters from the comment beginner to end of line are ignored. Includes just '#' by default.

shlex.wordchars

The string of characters that will accumulate into multi-character tokens. By default, includes all ASCII alphanumerics and underscore. In POSIX mode, the accented characters in the Latin-1 set are also included. If punctuation_chars is not empty, the characters ~-./*?=, which can appear in filename specifications and command line parameters, will also be included in this attribute, and any characters which appear in punctuation_chars will be removed from wordchars if they are present there.

shlex.whitespace

Characters that will be considered whitespace and skipped. Whitespace bounds tokens. By default, includes space, tab, linefeed and carriage-return.

shlex.escape

Characters that will be considered as escape. This will be only used in POSIX mode, and includes just '\' by default.

shlex.quotes

Characters that will be considered string quotes. The token accumulates until the same quote is encountered again (thus, different quote types protect each other as in the shell.) By default, includes ASCII single and double quotes.

shlex.escapedquotes

Characters in quotes that will interpret escape characters defined in escape. This is only used in POSIX mode, and includes just '"' by default.

shlex.whitespace_split

If True, tokens will only be split in whitespaces. This is useful, for example, for parsing command lines with shlex, getting tokens in a similar way to shell arguments. If this attribute is True, punctuation_chars will have no effect, and splitting will happen only on whitespaces. When using punctuation_chars, which is intended to provide parsing closer to that implemented by shells, it is advisable to leave whitespace_split as False (the default value).

shlex.infile

The name of the current input file, as initially set at class instantiation time or stacked by later source requests. It may be useful to examine this when constructing error messages.

shlex.instream

The input stream from which this shlex instance is reading characters.

shlex.source

This attribute is None by default. If you assign a string to it, that string will be recognized as a lexical-level inclusion request similar to the source keyword in various shells. That is, the immediately following token will be opened as a filename and input will be taken from that stream until EOF, at which point the close() method of that stream will be called and the input source will again become the original input stream. Source requests may be stacked any number of levels deep.

shlex.debug

If this attribute is numeric and 1 or more, a shlex instance will print verbose progress output on its behavior. If you need to use this, you can read the module source code to learn the details.

shlex.lineno

Source line number (count of newlines seen so far plus one).

shlex.token

The token buffer. It may be useful to examine this when catching exceptions.

shlex.eof

Token used to determine end of file. This will be set to the empty string (''), in non-POSIX mode, and to None in POSIX mode.

shlex.punctuation_chars

Characters that will be considered punctuation. Runs of punctuation characters will be returned as a single token. However, note that no semantic validity checking will be performed: for example, ‘>>>’ could be returned as a token, even though it may not be recognised as such by shells.

3.6 新版功能.

24.3.2. Parsing Rules

When operating in non-POSIX mode, shlex will try to obey to the following rules.

Quote characters are not recognized within words (Do"Not"Separate is parsed as the single word Do"Not"Separate);

Escape characters are not recognized;

Enclosing characters in quotes preserve the literal value of all characters within the quotes;

Closing quotes separate words ("Do"Separate is parsed as "Do" and Separate);

If whitespace_split is False, any character not declared to be a word character, whitespace, or a quote will be returned as a single-character token. If it is True, shlex will only split words in whitespaces;

EOF is signaled with an empty string ('');

It’s not possible to parse empty strings, even if quoted.

When operating in POSIX mode, shlex will try to obey to the following parsing rules.

Quotes are stripped out, and do not separate words ("Do"Not"Separate" is parsed as the single word DoNotSeparate);

Non-quoted escape characters (e.g. '\') preserve the literal value of the next character that follows;

Enclosing characters in quotes which are not part of escapedquotes (e.g. "'") preserve the literal value of all characters within the quotes;

Enclosing characters in quotes which are part of escapedquotes (e.g. '"') preserves the literal value of all characters within the quotes, with the exception of the characters mentioned in escape. The escape characters retain its special meaning only when followed by the quote in use, or the escape character itself. Otherwise the escape character will be considered a normal character.

EOF is signaled with a None value;

Quoted empty strings ('') are allowed.

24.3.3. Improved Compatibility with Shells

3.6 新版功能.

The shlex class provides compatibility with the parsing performed by common Unix shells like bash, dash, and sh. To take advantage of this compatibility, specify the punctuation_chars argument in the constructor. This defaults to False, which preserves pre-3.6 behaviour. However, if it is set to True, then parsing of the characters ();<>|& is changed: any run of these characters is returned as a single token. While this is short of a full parser for shells (which would be out of scope for the standard library, given the multiplicity of shells out there), it does allow you to perform processing of command lines more easily than you could otherwise. To illustrate, you can see the difference in the following snippet:

 >>> import shlex
 >>> text = "a && b; c && d || e; f >'abc'; (def \"ghi\")"
 >>> list(shlex.shlex(text))
 ['a', '&', '&', 'b', ';', 'c', '&', '&', 'd', '|', '|', 'e', ';', 'f', '>',
 "'abc'", ';', '(', 'def', '"ghi"', ')']
 >>> list(shlex.shlex(text, punctuation_chars=True))
 ['a', '&&', 'b', ';', 'c', '&&', 'd', '||', 'e', ';', 'f', '>', "'abc'",
 ';', '(', 'def', '"ghi"', ')']

Of course, tokens will be returned which are not valid for shells, and you’ll need to implement your own error checks on the returned tokens.

Instead of passing True as the value for the punctuation_chars parameter, you can pass a string with specific characters, which will be used to determine which characters constitute punctuation. For example:

>>> import shlex
>>> s = shlex.shlex("a && b || c", punctuation_chars="|")
>>> list(s)
['a', '&', '&', 'b', '||', 'c']

注解

When punctuation_chars is specified, the wordchars attribute is augmented with the characters ~-./*?=. That is because these characters can appear in file names (including wildcards) and command-line arguments (e.g. --color=auto). Hence:

>>> import shlex
>>> s = shlex.shlex('~/a && b-c --color=auto || d *.py?',
...                 punctuation_chars=True)
>>> list(s)
['~/a', '&&', 'b-c', '--color=auto', '||', 'd', '*.py?']

For best effect, punctuation_chars should be set in conjunction with posix=True. (Note that posix=False is the default for shlex.)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

24. 程序框架 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

25. Tk图形用户界面(GUI)

Tcl/Tk集成到Python中已经有一些年头了。Python程序员可以通过 tkinter 包和它的扩展， tkinter.tix 模块和 tkinter.ttk 模块，来使用这套鲁棒的、平台无关的窗口工具集。

tkinter 包使用面向对象的方式对Tcl/Tk进行了一层薄包装。使用 tkinter ，你不需要写Tcl代码，但可能需要参考Tk文档，甚至Tcl文档。 tkinter 使用Python类，对Tk的窗体小部件（Widgets）进行了一系列的封装。除此之外，内部模块 _tkinter 针对Python和Tcl之间的交互，提供了一套线程安全的机制。

tkinter 最大的优点就一个字：快，再一个，是Python自带的。尽管官方文档不太完整，但有其他资源可以参考，比如Tk手册，教程等。 tkinter 也以比较过时的外观为人所知，但在Tk 8.5中，这一点得到了极大的改观。除此之外，如果有兴趣，还有其他的一些GUI库可供使用。更多信息，请参考 其他图形用户界面（GUI）包 小节。

25.1. tkinter — Tcl/Tk的Python接口

25.1.1. Tkinter 模块

25.1.2. Tkinter Life Preserver

25.1.2.1. How To Use This Section

25.1.2.2. A Simple Hello World Program

25.1.3. A (Very) Quick Look at Tcl/Tk

25.1.4. Mapping Basic Tk into Tkinter

25.1.5. How Tk and Tkinter are Related

25.1.6. Handy Reference

25.1.6.1. Setting Options

25.1.6.2. The Packer

25.1.6.3. Packer Options

25.1.6.4. Coupling Widget Variables

25.1.6.5. The Window Manager

25.1.6.6. Tk Option Data Types

25.1.6.7. Bindings and Events

25.1.6.8. The index Parameter

25.1.6.9. Images

25.1.7. File Handlers

25.2. tkinter.ttk — Tk主题小部件

25.2.1. 使用 Ttk

25.2.2. Ttk 部件

25.2.3. 控件

25.2.3.1. 标准选项

25.2.3.2. 可滚动控件选项

25.2.3.3. 标签选项

25.2.3.4. 兼容性选项

25.2.3.5. 控件状态

25.2.3.6. ttk.Widget

25.2.4. 组合框

25.2.4.1. 选项

25.2.4.2. 虚拟事件

25.2.4.3. ttk.Combobox

25.2.5. 笔记本

25.2.5.1. 选项

25.2.5.2. Tab 选项

25.2.5.3. Tab Identifiers

25.2.5.4. Virtual Events

25.2.5.5. ttk.Notebook

25.2.6. Progressbar

25.2.6.1. 选项

25.2.6.2. ttk.Progressbar

25.2.7. Separator

25.2.7.1. 选项

25.2.8. Sizegrip

25.2.8.1. Platform-specific notes

25.2.8.2. Bugs

25.2.9. Treeview

25.2.9.1. 选项

25.2.9.2. Item Options

25.2.9.3. Tag Options

25.2.9.4. Column Identifiers

25.2.9.5. Virtual Events

25.2.9.6. ttk.Treeview

25.2.10. Ttk Styling

25.2.10.1. Layouts

25.3. tkinter.tix — Extension widgets for Tk

25.3.1. Using Tix

25.3.2. Tix Widgets

25.3.2.1. Basic Widgets

25.3.2.2. File Selectors

25.3.2.3. Hierarchical ListBox

25.3.2.4. Tabular ListBox

25.3.2.5. Manager Widgets

25.3.2.6. Image Types

25.3.2.7. Miscellaneous Widgets

25.3.2.8. Form Geometry Manager

25.3.3. Tix Commands

25.4. tkinter.scrolledtext — 滚动文字控件

25.5. IDLE

25.5.1. 目录

25.5.1.1. 文件目录 （命令行和编辑器）

25.5.1.2. 编辑目录（命令行和编辑器）

25.5.1.3. 格式菜单（仅 window 编辑器）

25.5.1.4. 运行菜单（仅 window 编辑器）

25.5.1.5. Shell 菜单（仅 window 编辑器）

25.5.1.6. 调试菜单（仅 window 编辑器）

25.5.1.7. 选项菜单（命令行和编辑器）

25.5.1.8. Window 菜单（命令行和编辑器）

25.5.1.9. 帮助菜单（命令行和编辑器）

25.5.1.10. 上下文菜单

25.5.2. 编辑和导航

25.5.2.1. 编辑窗口

25.5.2.2. 按键绑定

25.5.2.3. 自动缩进

25.5.2.4. 完成

25.5.2.5. 提示

25.5.2.6. Python Shell 窗口

25.5.2.7. 文本颜色

25.5.3. 启动和代码执行

25.5.3.1. 命令行语法

25.5.3.2. 启动失败

25.5.3.3. 运行用户代码

25.5.3.4. Shell中的用户输出

25.5.3.5. 开发 tkinter 应用程序

25.5.3.6. 在没有子进程的情况下运行

25.5.4. 帮助和偏好

25.5.4.1. 帮助资源

25.5.4.2. 偏好设定

25.5.4.3. macOS 上的IDLE

25.5.4.4. 扩展

25.6. 其他图形用户界面（GUI）包

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

25. Tk图形用户界面(GUI) »

|

25.1. tkinter — Tcl/Tk的Python接口

源代码: Lib/tkinter/__init__.py [https://github.com/python/cpython/tree/3.6/Lib/tkinter/__init__.py]

The tkinter package (“Tk interface”) is the standard Python interface to the Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, as well as on Windows systems. (Tk itself is not part of Python; it is maintained at ActiveState.)

在命令行中运行 python -m tkinter，应该会弹出一个Tk界面的窗口，表明 tkinter 包已经正确安装，而且告诉你 Tcl/Tk 的版本号，通过这个版本号，你就可以参考对应的 Tcl/Tk 文档了。

参见

Tkinter文档：

Python Tkinter 资源 [https://wiki.python.org/moin/TkInter]

The Python Tkinter Topic Guide 提供了在 Python 中使用 Tk 的很多信息， 同时包含了Tk其他信息的链接。

TKDocs [http://www.tkdocs.com/]

大量的教程，部分可视化组件的介绍说明。

Tkinter 8.5 reference: a GUI for Python [https://web.archive.org/web/20190524140835/https://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html]

在线参考资料。

Tkinter docs from effbot [http://effbot.org/tkinterbook/]

effbot.org 提供的 tkinter 在线参考资料。

使用 Python 编程 [http://learning-python.com/about-pp4e.html]

由 Mark Lutz 所著的书籍，对 Tkinter 进行了完美的介绍。

为繁忙的 Python 开发者所准备的现代 Tkinter [https://www.amazon.com/Modern-Tkinter-Python-Developers-ebook/dp/B0071QDNLO/]

由 Mark Rozerman 所著的关于如何使用 Python 和 Tkinter 来搭建有吸引力的和现代化的图形用户界面的书籍

Python 和 Tkinter 编程 [https://www.manning.com/books/python-and-tkinter-programming]

作者： John Grayson (ISBN 1-884777-81-3).

Tcl/Tk 文档:

Tk 命令 [https://www.tcl.tk/man/tcl8.6/TkCmd/contents.htm]

多数命令以 tkinter 或者 tkinter.ttk 类的形式存在。改变 ‘8.6’ 以匹配所安装的 Tcl/Tk 版本。

Tcl/Tk 最新手册页面 [https://www.tcl.tk/doc/]

www.tcl.tk 上面最新的 Tcl/Tk 手册。

ActiveState Tcl Home Page [http://tcl.activestate.com/]

Tk/Tcl 的多数开发工作发生在 ActiveState 。

Tcl 及 Tk 工具集 [https://www.amazon.com/exec/obidos/ASIN/020163337X]

由 Tcl 发明者 John Ousterhout 所著的书籍。

` Tcl 和 Tk 编程实战 <http://www.beedub.com/book/>`_

Brent Welch 所著的百科全局式书籍。

25.1.1. Tkinter 模块

在大多数时候你只需要 tkinter 就足够了，但也有一些额外的模块可供使用。Tk 接口位于一个名字 _tkinter 的二进制模块当中。此模块包含了低层级的 Tk 接口，它不应该被应用程序员所直接使用。它通常是一个共享库（或 DLL），但在某些情况下也可能被静态链接到 Python 解释器。

除了Tk接口， tkinter 也包含了若干 Python 模块，tkinter.constants 是其中最重要的。导入 tkinter 会自动导入 tkinter.constants ，所以，要使用 Tkinter 通常你只需要一条简单的 import 语句:

import tkinter

或者更常用的:

from tkinter import *

class tkinter.Tk(screenName=None, baseName=None, className='Tk', useTk=1)

Tk 类被初始化时无参数。此时会创建一个 Tk 顶级控件，通常是应用程序的主窗口。每个实例都有自己关联的 Tcl 解释器。

tkinter.Tcl(screenName=None, baseName=None, className='Tk', useTk=0)

Tcl() 函数是一个工厂函数，它创建的对象与 Tk 类创建的对象非常相似，只是它不初始化 Tk 子系统。 在不想创建或无法创建（如没有 X Server 的 Unix/Linux 系统）额外的顶层窗口的环境中驱动 Tcl 解释器时，这一点非常有用。 由 Tcl() 对象创建的对象可以通过调用其 loadtk() 方法来创建顶层窗口（并初始化 Tk 子系统）。

提供Tk支持的其他模块包括：

tkinter.scrolledtext

Text widget with a vertical scroll bar built in.

tkinter.colorchooser

让用户选择颜色的对话框。

tkinter.commondialog

在此处列出的其他模块中定义的对话框的基类。

tkinter.filedialog

Common dialogs to allow the user to specify a file to open or save.

tkinter.font

Utilities to help work with fonts.

tkinter.messagebox

Access to standard Tk dialog boxes.

tkinter.simpledialog

Basic dialogs and convenience functions.

tkinter.dnd

Drag-and-drop support for tkinter. This is experimental and should become deprecated when it is replaced with the Tk DND.

turtle

Turtle graphics in a Tk window.

25.1.2. Tkinter Life Preserver

This section is not designed to be an exhaustive tutorial on either Tk or Tkinter. Rather, it is intended as a stop gap, providing some introductory orientation on the system.

Credits:

Tk was written by John Ousterhout while at Berkeley.

Tkinter was written by Steen Lumholt and Guido van Rossum.

This Life Preserver was written by Matt Conway at the University of Virginia.

The HTML rendering, and some liberal editing, was produced from a FrameMaker version by Ken Manheimer.

Fredrik Lundh elaborated and revised the class interface descriptions, to get them current with Tk 4.2.

Mike Clarkson converted the documentation to LaTeX, and compiled the User Interface chapter of the reference manual.

25.1.2.1. How To Use This Section

This section is designed in two parts: the first half (roughly) covers background material, while the second half can be taken to the keyboard as a handy reference.

When trying to answer questions of the form “how do I do blah”, it is often best to find out how to do “blah” in straight Tk, and then convert this back into the corresponding tkinter call. Python programmers can often guess at the correct Python command by looking at the Tk documentation. This means that in order to use Tkinter, you will have to know a little bit about Tk. This document can’t fulfill that role, so the best we can do is point you to the best documentation that exists. Here are some hints:

The authors strongly suggest getting a copy of the Tk man pages. Specifically, the man pages in the manN directory are most useful. The man3 man pages describe the C interface to the Tk library and thus are not especially helpful for script writers.

Addison-Wesley publishes a book called Tcl and the Tk Toolkit by John Ousterhout (ISBN 0-201-63337-X) which is a good introduction to Tcl and Tk for the novice. The book is not exhaustive, and for many details it defers to the man pages.

tkinter/__init__.py is a last resort for most, but can be a good place to go when nothing else makes sense.

25.1.2.2. A Simple Hello World Program

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red",
                              command=self.master.destroy)
        self.quit.pack(side="bottom")

    def say_hi(self):
        print("hi there, everyone!")

root = tk.Tk()
app = Application(master=root)
app.mainloop()

25.1.3. A (Very) Quick Look at Tcl/Tk

The class hierarchy looks complicated, but in actual practice, application programmers almost always refer to the classes at the very bottom of the hierarchy.

注释:

These classes are provided for the purposes of organizing certain functions under one namespace. They aren’t meant to be instantiated independently.

The Tk class is meant to be instantiated only once in an application. Application programmers need not instantiate one explicitly, the system creates one whenever any of the other classes are instantiated.

The Widget class is not meant to be instantiated, it is meant only for subclassing to make “real” widgets (in C++, this is called an ‘abstract class’).

To make use of this reference material, there will be times when you will need to know how to read short passages of Tk and how to identify the various parts of a Tk command. (See section Mapping Basic Tk into Tkinter for the tkinter equivalents of what’s below.)

Tk scripts are Tcl programs. Like all Tcl programs, Tk scripts are just lists of tokens separated by spaces. A Tk widget is just its class, the options that help configure it, and the actions that make it do useful things.

To make a widget in Tk, the command is always of the form:

classCommand newPathname options

classCommand

denotes which kind of widget to make (a button, a label, a menu…)

newPathname

is the new name for this widget. All names in Tk must be unique. To help enforce this, widgets in Tk are named with pathnames, just like files in a file system. The top level widget, the root, is called . (period) and children are delimited by more periods. For example, .myApp.controlPanel.okButton might be the name of a widget.

options

configure the widget’s appearance and in some cases, its behavior. The options come in the form of a list of flags and values. Flags are preceded by a ‘-‘, like Unix shell command flags, and values are put in quotes if they are more than one word.

例如

button   .fred   -fg red -text "hi there"
   ^       ^     \______________________/
   |       |                |
 class    new            options
command  widget  (-opt val -opt val ...)

Once created, the pathname to the widget becomes a new command. This new widget command is the programmer’s handle for getting the new widget to perform some action. In C, you’d express this as someAction(fred, someOptions), in C++, you would express this as fred.someAction(someOptions), and in Tk, you say:

.fred someAction someOptions

Note that the object name, .fred, starts with a dot.

As you’d expect, the legal values for someAction will depend on the widget’s class: .fred disable works if fred is a button (fred gets greyed out), but does not work if fred is a label (disabling of labels is not supported in Tk).

The legal values of someOptions is action dependent. Some actions, like disable, require no arguments, others, like a text-entry box’s delete command, would need arguments to specify what range of text to delete.

25.1.4. Mapping Basic Tk into Tkinter

Class commands in Tk correspond to class constructors in Tkinter.

button .fred                =====>  fred = Button()

The master of an object is implicit in the new name given to it at creation time. In Tkinter, masters are specified explicitly.

button .panel.fred          =====>  fred = Button(panel)

The configuration options in Tk are given in lists of hyphened tags followed by values. In Tkinter, options are specified as keyword-arguments in the instance constructor, and keyword-args for configure calls or as instance indices, in dictionary style, for established instances. See section Setting Options on setting options.

button .fred -fg red        =====>  fred = Button(panel, fg="red")
.fred configure -fg red     =====>  fred["fg"] = red
                            OR ==>  fred.config(fg="red")

In Tk, to perform an action on a widget, use the widget name as a command, and follow it with an action name, possibly with arguments (options). In Tkinter, you call methods on the class instance to invoke actions on the widget. The actions (methods) that a given widget can perform are listed in tkinter/__init__.py.

.fred invoke                =====>  fred.invoke()

To give a widget to the packer (geometry manager), you call pack with optional arguments. In Tkinter, the Pack class holds all this functionality, and the various forms of the pack command are implemented as methods. All widgets in tkinter are subclassed from the Packer, and so inherit all the packing methods. See the tkinter.tix module documentation for additional information on the Form geometry manager.

pack .fred -side left       =====>  fred.pack(side="left")

25.1.5. How Tk and Tkinter are Related

From the top down:

Your App Here (Python)

A Python application makes a tkinter call.

tkinter (Python Package)

This call (say, for example, creating a button widget), is implemented in the tkinter package, which is written in Python. This Python function will parse the commands and the arguments and convert them into a form that makes them look as if they had come from a Tk script instead of a Python script.

_tkinter (C)

These commands and their arguments will be passed to a C function in the _tkinter - note the underscore - extension module.

Tk Widgets (C and Tcl)

This C function is able to make calls into other C modules, including the C functions that make up the Tk library. Tk is implemented in C and some Tcl. The Tcl part of the Tk widgets is used to bind certain default behaviors to widgets, and is executed once at the point where the Python tkinter package is imported. (The user never sees this stage).

Tk (C)

The Tk part of the Tk Widgets implement the final mapping to …

Xlib (C)

the Xlib library to draw graphics on the screen.

25.1.6. Handy Reference

25.1.6.1. Setting Options

Options control things like the color and border width of a widget. Options can be set in three ways:

At object creation time, using keyword arguments

fred = Button(self, fg="red", bg="blue")

After object creation, treating the option name like a dictionary index

fred["fg"] = "red"
fred["bg"] = "blue"

Use the config() method to update multiple attrs subsequent to object creation

fred.config(fg="red", bg="blue")

For a complete explanation of a given option and its behavior, see the Tk man pages for the widget in question.

Note that the man pages list “STANDARD OPTIONS” and “WIDGET SPECIFIC OPTIONS” for each widget. The former is a list of options that are common to many widgets, the latter are the options that are idiosyncratic to that particular widget. The Standard Options are documented on the options(3) man page.

No distinction between standard and widget-specific options is made in this document. Some options don’t apply to some kinds of widgets. Whether a given widget responds to a particular option depends on the class of the widget; buttons have a command option, labels do not.

The options supported by a given widget are listed in that widget’s man page, or can be queried at runtime by calling the config() method without arguments, or by calling the keys() method on that widget. The return value of these calls is a dictionary whose key is the name of the option as a string (for example, 'relief') and whose values are 5-tuples.

Some options, like bg are synonyms for common options with long names (bg is shorthand for “background”). Passing the config() method the name of a shorthand option will return a 2-tuple, not 5-tuple. The 2-tuple passed back will contain the name of the synonym and the “real” option (such as ('bg', 'background')).

索引

含义

示例

0

选项名称

'relief'

1

数据库查找的选项名称

'relief'

2

数据库查找的选项类

'Relief'

3

默认值

'raised'

4

当前值

'groove'

示例:

>>> print(fred.config())
{'relief': ('relief', 'relief', 'Relief', 'raised', 'groove')}

Of course, the dictionary printed will include all the options available and their values. This is meant only as an example.

25.1.6.2. The Packer

The packer is one of Tk’s geometry-management mechanisms. Geometry managers are used to specify the relative positioning of the positioning of widgets within their container - their mutual master. In contrast to the more cumbersome placer (which is used less commonly, and we do not cover here), the packer takes qualitative relationship specification - above, to the left of, filling, etc - and works everything out to determine the exact placement coordinates for you.

The size of any master widget is determined by the size of the “slave widgets” inside. The packer is used to control where slave widgets appear inside the master into which they are packed. You can pack widgets into frames, and frames into other frames, in order to achieve the kind of layout you desire. Additionally, the arrangement is dynamically adjusted to accommodate incremental changes to the configuration, once it is packed.

Note that widgets do not appear until they have had their geometry specified with a geometry manager. It’s a common early mistake to leave out the geometry specification, and then be surprised when the widget is created but nothing appears. A widget will appear only after it has had, for example, the packer’s pack() method applied to it.

The pack() method can be called with keyword-option/value pairs that control where the widget is to appear within its container, and how it is to behave when the main application window is resized. Here are some examples:

fred.pack()                     # defaults to side = "top"
fred.pack(side="left")
fred.pack(expand=1)

25.1.6.3. Packer Options

For more extensive information on the packer and the options that it can take, see the man pages and page 183 of John Ousterhout’s book.

anchor

Anchor type. Denotes where the packer is to place each slave in its parcel.

expand

Boolean, 0 or 1.

fill

Legal values: 'x', 'y', 'both', 'none'.

ipadx 和 ipady

A distance - designating internal padding on each side of the slave widget.

padx 和 pady

A distance - designating external padding on each side of the slave widget.

side

Legal values are: 'left', 'right', 'top', 'bottom'.

25.1.6.4. Coupling Widget Variables

The current-value setting of some widgets (like text entry widgets) can be connected directly to application variables by using special options. These options are variable, textvariable, onvalue, offvalue, and value. This connection works both ways: if the variable changes for any reason, the widget it’s connected to will be updated to reflect the new value.

Unfortunately, in the current implementation of tkinter it is not possible to hand over an arbitrary Python variable to a widget through a variable or textvariable option. The only kinds of variables for which this works are variables that are subclassed from a class called Variable, defined in tkinter.

There are many useful subclasses of Variable already defined: StringVar, IntVar, DoubleVar, and BooleanVar. To read the current value of such a variable, call the get() method on it, and to change its value you call the set() method. If you follow this protocol, the widget will always track the value of the variable, with no further intervention on your part.

例如

class App(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()

        self.entrythingy = Entry()
        self.entrythingy.pack()

        # here is the application variable
        self.contents = StringVar()
        # set it to some value
        self.contents.set("this is a variable")
        # tell the entry widget to watch this variable
        self.entrythingy["textvariable"] = self.contents

        # and here we get a callback when the user hits return.
        # we will have the program print out the value of the
        # application variable when the user hits return
        self.entrythingy.bind('<Key-Return>',
                              self.print_contents)

    def print_contents(self, event):
        print("hi. contents of entry is now ---->",
              self.contents.get())

25.1.6.5. The Window Manager

In Tk, there is a utility command, wm, for interacting with the window manager. Options to the wm command allow you to control things like titles, placement, icon bitmaps, and the like. In tkinter, these commands have been implemented as methods on the Wm class. Toplevel widgets are subclassed from the Wm class, and so can call the Wm methods directly.

To get at the toplevel window that contains a given widget, you can often just refer to the widget’s master. Of course if the widget has been packed inside of a frame, the master won’t represent a toplevel window. To get at the toplevel window that contains an arbitrary widget, you can call the _root() method. This method begins with an underscore to denote the fact that this function is part of the implementation, and not an interface to Tk functionality.

Here are some examples of typical usage:

import tkinter as tk

class App(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()

# create the application
myapp = App()

#
# here are method calls to the window manager class
#
myapp.master.title("My Do-Nothing Application")
myapp.master.maxsize(1000, 400)

# start the program
myapp.mainloop()

25.1.6.6. Tk Option Data Types

anchor

Legal values are points of the compass: "n", "ne", "e", "se", "s", "sw", "w", "nw", and also "center".

位图

There are eight built-in, named bitmaps: 'error', 'gray25', 'gray50', 'hourglass', 'info', 'questhead', 'question', 'warning'. To specify an X bitmap filename, give the full path to the file, preceded with an @, as in "@/usr/contrib/bitmap/gumby.bit".

boolean

You can pass integers 0 or 1 or the strings "yes" or "no".

callback – 回调

This is any Python function that takes no arguments. For example:

def print_it():
    print("hi there")
fred["command"] = print_it

color

Colors can be given as the names of X colors in the rgb.txt file, or as strings representing RGB values in 4 bit: "#RGB", 8 bit: "#RRGGBB", 12 bit” "#RRRGGGBBB", or 16 bit "#RRRRGGGGBBBB" ranges, where R,G,B here represent any legal hex digit. See page 160 of Ousterhout’s book for details.

cursor

The standard X cursor names from cursorfont.h can be used, without the XC_ prefix. For example to get a hand cursor (XC_hand2), use the string "hand2". You can also specify a bitmap and mask file of your own. See page 179 of Ousterhout’s book.

distance

Screen distances can be specified in either pixels or absolute distances. Pixels are given as numbers and absolute distances as strings, with the trailing character denoting units: c for centimetres, i for inches, m for millimetres, p for printer’s points. For example, 3.5 inches is expressed as "3.5i".

font

Tk uses a list font name format, such as {courier 10 bold}. Font sizes with positive numbers are measured in points; sizes with negative numbers are measured in pixels.

geometry

This is a string of the form widthxheight, where width and height are measured in pixels for most widgets (in characters for widgets displaying text). For example: fred["geometry"] = "200x100".

justify

Legal values are the strings: "left", "center", "right", and "fill".

region

This is a string with four space-delimited elements, each of which is a legal distance (see above). For example: "2 3 4 5" and "3i 2i 4.5i 2i" and "3c 2c 4c 10.43c" are all legal regions.

relief

Determines what the border style of a widget will be. Legal values are: "raised", "sunken", "flat", "groove", and "ridge".

scrollcommand

This is almost always the set() method of some scrollbar widget, but can be any widget method that takes a single argument.

wrap:

Must be one of: "none", "char", or "word".

25.1.6.7. Bindings and Events

The bind method from the widget command allows you to watch for certain events and to have a callback function trigger when that event type occurs. The form of the bind method is:

def bind(self, sequence, func, add=''):

where:

sequence – 序列

is a string that denotes the target kind of event. (See the bind man page and page 201 of John Ousterhout’s book for details).

func

is a Python function, taking one argument, to be invoked when the event occurs. An Event instance will be passed as the argument. (Functions deployed this way are commonly known as callbacks.)

add

is optional, either '' or '+'. Passing an empty string denotes that this binding is to replace any other bindings that this event is associated with. Passing a '+' means that this function is to be added to the list of functions bound to this event type.

例如

def turn_red(self, event):
    event.widget["activeforeground"] = "red"

self.button.bind("<Enter>", self.turn_red)

Notice how the widget field of the event is being accessed in the turn_red() callback. This field contains the widget that caught the X event. The following table lists the other event fields you can access, and how they are denoted in Tk, which can be useful when referring to the Tk man pages.

Tk

Tkinter Event Field

Tk

Tkinter Event Field

%f

焦点

%A

char

%h

height

%E

send_event

%k

keycode

%K

keysym

%s

状况

%N

keysym_num

%t

time

%T

type – 类型

%w

宽度

%W

widget

%x

x

%X

x_root

%y

y

%Y

y_root

25.1.6.8. The index Parameter

A number of widgets require “index” parameters to be passed. These are used to point at a specific place in a Text widget, or to particular characters in an Entry widget, or to particular menu items in a Menu widget.

Entry widget indexes (index, view index, etc.)

Entry widgets have options that refer to character positions in the text being displayed. You can use these tkinter functions to access these special points in text widgets:

Text widget indexes

The index notation for Text widgets is very rich and is best described in the Tk man pages.

Menu indexes (menu.invoke(), menu.entryconfig(), etc.)

Some options and methods for menus manipulate specific menu entries. Anytime a menu index is needed for an option or a parameter, you may pass in:

an integer which refers to the numeric position of the entry in the widget, counted from the top, starting with 0;

the string "active", which refers to the menu position that is currently under the cursor;

the string "last" which refers to the last menu item;

An integer preceded by @, as in @6, where the integer is interpreted as a y pixel coordinate in the menu’s coordinate system;

the string "none", which indicates no menu entry at all, most often used with menu.activate() to deactivate all entries, and finally,

a text string that is pattern matched against the label of the menu entry, as scanned from the top of the menu to the bottom. Note that this index type is considered after all the others, which means that matches for menu items labelled last, active, or none may be interpreted as the above literals, instead.

25.1.6.9. Images

Images of different formats can be created through the corresponding subclass of tkinter.Image:

BitmapImage for images in XBM format.

PhotoImage for images in PGM, PPM, GIF and PNG formats. The latter is supported starting with Tk 8.6.

Either type of image is created through either the file or the data option (other options are available as well).

The image object can then be used wherever an image option is supported by some widget (e.g. labels, buttons, menus). In these cases, Tk will not keep a reference to the image. When the last Python reference to the image object is deleted, the image data is deleted as well, and Tk will display an empty box wherever the image was used.

参见

The Pillow [http://python-pillow.org/] package adds support for formats such as BMP, JPEG, TIFF, and WebP, among others.

25.1.7. File Handlers

Tk allows you to register and unregister a callback function which will be called from the Tk mainloop when I/O is possible on a file descriptor. Only one handler may be registered per file descriptor. Example code:

import tkinter
widget = tkinter.Tk()
mask = tkinter.READABLE | tkinter.WRITABLE
widget.tk.createfilehandler(file, mask, callback)
...
widget.tk.deletefilehandler(file)

This feature is not available on Windows.

Since you don’t know how many bytes are available for reading, you may not want to use the BufferedIOBase or TextIOBase read() or readline() methods, since these will insist on reading a predefined number of bytes. For sockets, the recv() or recvfrom() methods will work fine; for other files, use raw reads or os.read(file.fileno(), maxbytecount).

Widget.tk.createfilehandler(file, mask, func)

Registers the file handler callback function func. The file argument may either be an object with a fileno() method (such as a file or socket object), or an integer file descriptor. The mask argument is an ORed combination of any of the three constants below. The callback is called as follows:

callback(file, mask)

Widget.tk.deletefilehandler(file)

Unregisters a file handler.

tkinter.READABLE

tkinter.WRITABLE

tkinter.EXCEPTION

Constants used in the mask arguments.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

25. Tk图形用户界面(GUI) »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

25. Tk图形用户界面(GUI) »

|

25.2. tkinter.ttk — Tk主题小部件

源代码: Lib/tkinter/ttk.py [https://github.com/python/cpython/tree/3.6/Lib/tkinter/ttk.py]

The tkinter.ttk module provides access to the Tk themed widget set, introduced in Tk 8.5. If Python has not been compiled against Tk 8.5, this module can still be accessed if Tile has been installed. The former method using Tk 8.5 provides additional benefits including anti-aliased font rendering under X11 and window transparency (requiring a composition window manager on X11).

The basic idea for tkinter.ttk is to separate, to the extent possible, the code implementing a widget’s behavior from the code implementing its appearance.

参见

Tk Widget Styling Support [https://www.tcl.tk/cgi-bin/tct/tip/48]

一份文档介绍Tk支持的主题

25.2.1. 使用 Ttk

开始使用 Ttk, 导入模块:

from tkinter import ttk

重写基础Tk部件,导入应跟随Tk导入:

from tkinter import *
from tkinter.ttk import *

That code causes several tkinter.ttk widgets (Button, Checkbutton, Entry, Frame, Label, LabelFrame, Menubutton, PanedWindow, Radiobutton, Scale and Scrollbar) to automatically replace the Tk widgets.

This has the direct benefit of using the new widgets which gives a better look and feel across platforms; however, the replacement widgets are not completely compatible. The main difference is that widget options such as “fg”, “bg” and others related to widget styling are no longer present in Ttk widgets. Instead, use the ttk.Style class for improved styling effects.

参见

Converting existing applications to use Tile widgets [http://tktable.sourceforge.net/tile/doc/converting.txt]

A monograph (using Tcl terminology) about differences typically encountered when moving applications to use the new widgets.

25.2.2. Ttk 部件

Ttk comes with 17 widgets, eleven of which already existed in tkinter: Button, Checkbutton, Entry, Frame, Label, LabelFrame, Menubutton, PanedWindow, Radiobutton, Scale and Scrollbar. The other six are new: Combobox, Notebook, Progressbar, Separator, Sizegrip and Treeview. And all them are subclasses of Widget.

Using the Ttk widgets gives the application an improved look and feel. As discussed above, there are differences in how the styling is coded.

Tk 代码:

l1 = tkinter.Label(text="Test", fg="black", bg="white")
l2 = tkinter.Label(text="Test", fg="black", bg="white")

Ttk 代码:

style = ttk.Style()
style.configure("BW.TLabel", foreground="black", background="white")

l1 = ttk.Label(text="Test", style="BW.TLabel")
l2 = ttk.Label(text="Test", style="BW.TLabel")

有关 TtkStyling 的更多信息，请参阅 Style 类文档。

25.2.3. 控件

ttk.Widget 定义了 Tk 主题小部件支持的标准选项和方法，不应该直接实例化。

25.2.3.1. 标准选项

所有 ttk 小部件接受以下选项：

选项

描述

class – 类

指定窗口类。在查询选项数据库中窗口的其他选项时，使用该类，确定窗口的默认绑定标签，以及选择窗口小部件的默认布局和样式。此选项仅为只读，并且只能在创建窗口时指定。

cursor

指定要用于窗口小部件的鼠标光标。如果设置为空字符串（默认值），则为父窗口小部件继承光标.

takefocus

确定窗口是否在键盘遍历期间接受焦点。返回0或1，返回空字符串。如果返回0，则表示在键盘遍历期间应该跳过该窗口。如果为1，则表示只要可以查看窗口就应该接收输入焦点。并且空字符串意味着遍历脚本决定是否关注窗口。

style

可用于指定自定义窗口小部件样式。

25.2.3.2. 可滚动控件选项

控件支持以下选项使用滚动条控制.

选项

描述

xscroll命令

用于与垂直滚动条通讯.

当视图在部件窗口改变,部件将会基于scroll命令生成Tcl命令.

Usually this option consists of the method Scrollbar.set() of some scrollbar. This will cause the scrollbar to be updated whenever the view in the window changes.

yscroll命令

用于与垂直滚动条通讯.更多信息请参考上面的信息.

25.2.3.3. 标签选项

以下选项支持标签,按钮已经其他类按钮的控件.

选项

描述

文本

Specifies a text string to be displayed inside the widget.

文本变量

Specifies a name whose value will be used in place of the text option resource.

下划线

If set, specifies the index (0-based) of a character to underline in the text string. The underline character is used for mnemonic activation.

图片

Specifies an image to display. This is a list of 1 or more elements. The first element is the default image name. The rest of the list if a sequence of statespec/value pairs as defined by Style.map(), specifying different images to use when the widget is in a particular state or a combination of states. All images in the list should have the same size.

compound

Specifies how to display the image relative to the text, in the case both text and images options are present. Valid values are:

text: 只显示文本

image: 只显示图片

top, bottom, left, right: 分别显示图片的上,下,左,右的文本.

none: 默认. 如果设置显示图片, 否则文本.

宽度

If greater than zero, specifies how much space, in character widths, to allocate for the text label, if less than zero, specifies a minimum width. If zero or unspecified, the natural width of the text label is used.

25.2.3.4. 兼容性选项

选项

描述

状况

May be set to “normal” or “disabled” to control the “disabled” state bit. This is a write-only option: setting it changes the widget state, but the Widget.state() method does not affect this option.

25.2.3.5. 控件状态

控件状态是无关状态标志的位图.

标志

描述

活动

The mouse cursor is over the widget and pressing a mouse button will cause some action to occur

禁用

在程序控制下控件是禁用的

焦点

控件有键盘焦点

按压

Widget is being pressed

选择

“On”, “true”, or “current” for things like Checkbuttons and radiobuttons

背景

Windows and Mac have a notion of an “active” or foreground window. The background state is set for widgets in a background window, and cleared for those in the foreground window

只读

Widget should not allow user modification

alternate

A widget-specific alternate display format

无效的

控件的值是无效的

A state specification is a sequence of state names, optionally prefixed with an exclamation point indicating that the bit is off.

25.2.3.6. ttk.Widget

Besides the methods described below, the ttk.Widget supports the methods tkinter.Widget.cget() and tkinter.Widget.configure().

class tkinter.ttk.Widget

identify(x, y)

Returns the name of the element at position x y, or the empty string if the point does not lie within any element.

x and y are pixel coordinates relative to the widget.

instate(statespec, callback=None, *args, **kw)

Test the widget’s state. If a callback is not specified, returns True if the widget state matches statespec and False otherwise. If callback is specified then it is called with args if widget state matches statespec.

state(statespec=None)

Modify or inquire widget state. If statespec is specified, sets the widget state according to it and return a new statespec indicating which flags were changed. If statespec is not specified, returns the currently-enabled state flags.

statespec will usually be a list or a tuple.

25.2.4. 组合框

The ttk.Combobox widget combines a text field with a pop-down list of values. This widget is a subclass of Entry.

Besides the methods inherited from Widget: Widget.cget(), Widget.configure(), Widget.identify(), Widget.instate() and Widget.state(), and the following inherited from Entry: Entry.bbox(), Entry.delete(), Entry.icursor(), Entry.index(), Entry.insert(), Entry.selection(), Entry.xview(), it has some other methods, described at ttk.Combobox.

25.2.4.1. 选项

This widget accepts the following specific options:

选项

描述

exportselection

Boolean value. If set, the widget selection is linked to the Window Manager selection (which can be returned by invoking Misc.selection_get, for example).

justify

Specifies how the text is aligned within the widget. One of “left”, “center”, or “right”.

height

Specifies the height of the pop-down listbox, in rows.

postcommand

A script (possibly registered with Misc.register) that is called immediately before displaying the values. It may specify which values to display.

状况

One of “normal”, “readonly”, or “disabled”. In the “readonly” state, the value may not be edited directly, and the user can only selection of the values from the dropdown list. In the “normal” state, the text field is directly editable. In the “disabled” state, no interaction is possible.

文本变量

Specifies a name whose value is linked to the widget value. Whenever the value associated with that name changes, the widget value is updated, and vice versa. See tkinter.StringVar.

值

Specifies the list of values to display in the drop-down listbox.

宽度

Specifies an integer value indicating the desired width of the entry window, in average-size characters of the widget’s font.

25.2.4.2. 虚拟事件

The combobox widgets generates a <<ComboboxSelected>> virtual event when the user selects an element from the list of values.

25.2.4.3. ttk.Combobox

class tkinter.ttk.Combobox

current(newindex=None)

If newindex is specified, sets the combobox value to the element position newindex. Otherwise, returns the index of the current value or -1 if the current value is not in the values list.

get()

Returns the current value of the combobox.

set(value)

Sets the value of the combobox to value.

25.2.5. 笔记本

Ttk Notebook widget manages a collection of windows and displays a single one at a time. Each child window is associated with a tab, which the user may select to change the currently-displayed window.

25.2.5.1. 选项

This widget accepts the following specific options:

选项

描述

height

If present and greater than zero, specifies the desired height of the pane area (not including internal padding or tabs). Otherwise, the maximum height of all panes is used.

padding

Specifies the amount of extra space to add around the outside of the notebook. The padding is a list up to four length specifications left top right bottom. If fewer than four elements are specified, bottom defaults to top, right defaults to left, and top defaults to left.

宽度

If present and greater than zero, specified the desired width of the pane area (not including internal padding). Otherwise, the maximum width of all panes is used.

25.2.5.2. Tab 选项

There are also specific options for tabs:

选项

描述

状况

Either “normal”, “disabled” or “hidden”. If “disabled”, then the tab is not selectable. If “hidden”, then the tab is not shown.

sticky

Specifies how the child window is positioned within the pane area. Value is a string containing zero or more of the characters “n”, “s”, “e” or “w”. Each letter refers to a side (north, south, east or west) that the child window will stick to, as per the grid() geometry manager.

padding

Specifies the amount of extra space to add between the notebook and this pane. Syntax is the same as for the option padding used by this widget.

文本

Specifies a text to be displayed in the tab.

图片

Specifies an image to display in the tab. See the option image described in Widget.

compound

Specifies how to display the image relative to the text, in the case both options text and image are present. See Label Options for legal values.

下划线

Specifies the index (0-based) of a character to underline in the text string. The underlined character is used for mnemonic activation if Notebook.enable_traversal() is called.

25.2.5.3. Tab Identifiers

The tab_id present in several methods of ttk.Notebook may take any of the following forms:

An integer between zero and the number of tabs

The name of a child window

A positional specification of the form “@x,y”, which identifies the tab

The literal string “current”, which identifies the currently-selected tab

The literal string “end”, which returns the number of tabs (only valid for Notebook.index())

25.2.5.4. Virtual Events

This widget generates a <<NotebookTabChanged>> virtual event after a new tab is selected.

25.2.5.5. ttk.Notebook

class tkinter.ttk.Notebook

add(child, **kw)

Adds a new tab to the notebook.

If window is currently managed by the notebook but hidden, it is restored to its previous position.

See Tab Options for the list of available options.

forget(tab_id)

Removes the tab specified by tab_id, unmaps and unmanages the associated window.

hide(tab_id)

Hides the tab specified by tab_id.

The tab will not be displayed, but the associated window remains managed by the notebook and its configuration remembered. Hidden tabs may be restored with the add() command.

identify(x, y)

Returns the name of the tab element at position x, y, or the empty string if none.

index(tab_id)

Returns the numeric index of the tab specified by tab_id, or the total number of tabs if tab_id is the string “end”.

insert(pos, child, **kw)

Inserts a pane at the specified position.

pos is either the string “end”, an integer index, or the name of a managed child. If child is already managed by the notebook, moves it to the specified position.

See Tab Options for the list of available options.

select(tab_id=None)

Selects the specified tab_id.

The associated child window will be displayed, and the previously-selected window (if different) is unmapped. If tab_id is omitted, returns the widget name of the currently selected pane.

tab(tab_id, option=None, **kw)

Query or modify the options of the specific tab_id.

If kw is not given, returns a dictionary of the tab option values. If option is specified, returns the value of that option. Otherwise, sets the options to the corresponding values.

tabs()

Returns a list of windows managed by the notebook.

enable_traversal()

Enable keyboard traversal for a toplevel window containing this notebook.

This will extend the bindings for the toplevel window containing the notebook as follows:

Control-Tab: selects the tab following the currently selected one.

Shift-Control-Tab: selects the tab preceding the currently selected one.

Alt-K: where K is the mnemonic (underlined) character of any tab, will select that tab.

Multiple notebooks in a single toplevel may be enabled for traversal, including nested notebooks. However, notebook traversal only works properly if all panes have the notebook they are in as master.

25.2.6. Progressbar

The ttk.Progressbar widget shows the status of a long-running operation. It can operate in two modes: 1) the determinate mode which shows the amount completed relative to the total amount of work to be done and 2) the indeterminate mode which provides an animated display to let the user know that work is progressing.

25.2.6.1. 选项

This widget accepts the following specific options:

选项

描述

orient

One of “horizontal” or “vertical”. Specifies the orientation of the progress bar.

length

Specifies the length of the long axis of the progress bar (width if horizontal, height if vertical).

模式

One of “determinate” or “indeterminate”.

maximum

A number specifying the maximum value. Defaults to 100.

值

The current value of the progress bar. In “determinate” mode, this represents the amount of work completed. In “indeterminate” mode, it is interpreted as modulo maximum; that is, the progress bar completes one “cycle” when its value increases by maximum.

variable

A name which is linked to the option value. If specified, the value of the progress bar is automatically set to the value of this name whenever the latter is modified.

phase

Read-only option. The widget periodically increments the value of this option whenever its value is greater than 0 and, in determinate mode, less than maximum. This option may be used by the current theme to provide additional animation effects.

25.2.6.2. ttk.Progressbar

class tkinter.ttk.Progressbar

start(interval=None)

Begin autoincrement mode: schedules a recurring timer event that calls Progressbar.step() every interval milliseconds. If omitted, interval defaults to 50 milliseconds.

step(amount=None)

Increments the progress bar’s value by amount.

amount defaults to 1.0 if omitted.

stop()

Stop autoincrement mode: cancels any recurring timer event initiated by Progressbar.start() for this progress bar.

25.2.7. Separator

The ttk.Separator widget displays a horizontal or vertical separator bar.

It has no other methods besides the ones inherited from ttk.Widget.

25.2.7.1. 选项

This widget accepts the following specific option:

选项

描述

orient

One of “horizontal” or “vertical”. Specifies the orientation of the separator.

25.2.8. Sizegrip

The ttk.Sizegrip widget (also known as a grow box) allows the user to resize the containing toplevel window by pressing and dragging the grip.

This widget has neither specific options nor specific methods, besides the ones inherited from ttk.Widget.

25.2.8.1. Platform-specific notes

On MacOS X, toplevel windows automatically include a built-in size grip by default. Adding a Sizegrip is harmless, since the built-in grip will just mask the widget.

25.2.8.2. Bugs

If the containing toplevel’s position was specified relative to the right or bottom of the screen (e.g. ….), the Sizegrip widget will not resize the window.

This widget supports only “southeast” resizing.

25.2.9. Treeview

The ttk.Treeview widget displays a hierarchical collection of items. Each item has a textual label, an optional image, and an optional list of data values. The data values are displayed in successive columns after the tree label.

The order in which data values are displayed may be controlled by setting the widget option displaycolumns. The tree widget can also display column headings. Columns may be accessed by number or symbolic names listed in the widget option columns. See Column Identifiers.

Each item is identified by a unique name. The widget will generate item IDs if they are not supplied by the caller. There is a distinguished root item, named {}. The root item itself is not displayed; its children appear at the top level of the hierarchy.

Each item also has a list of tags, which can be used to associate event bindings with individual items and control the appearance of the item.

The Treeview widget supports horizontal and vertical scrolling, according to the options described in Scrollable Widget Options and the methods Treeview.xview() and Treeview.yview().

25.2.9.1. 选项

This widget accepts the following specific options:

选项

描述

columns

A list of column identifiers, specifying the number of columns and their names.

displaycolumns

A list of column identifiers (either symbolic or integer indices) specifying which data columns are displayed and the order in which they appear, or the string “#all”.

height

Specifies the number of rows which should be visible. Note: the requested width is determined from the sum of the column widths.

padding

Specifies the internal padding for the widget. The padding is a list of up to four length specifications.

selectmode

Controls how the built-in class bindings manage the selection. One of “extended”, “browse” or “none”. If set to “extended” (the default), multiple items may be selected. If “browse”, only a single item will be selected at a time. If “none”, the selection will not be changed.

Note that the application code and tag bindings can set the selection however they wish, regardless of the value of this option.

show

A list containing zero or more of the following values, specifying which elements of the tree to display.

tree: display tree labels in column #0.

headings: display the heading row.

The default is “tree headings”, i.e., show all elements.

Note: Column #0 always refers to the tree column, even if show=”tree” is not specified.

25.2.9.2. Item Options

The following item options may be specified for items in the insert and item widget commands.

选项

描述

文本

The textual label to display for the item.

图片

A Tk Image, displayed to the left of the label.

值

The list of values associated with the item.

Each item should have the same number of values as the widget option columns. If there are fewer values than columns, the remaining values are assumed empty. If there are more values than columns, the extra values are ignored.

open

True/False value indicating whether the item’s children should be displayed or hidden.

tags

A list of tags associated with this item.

25.2.9.3. Tag Options

The following options may be specified on tags:

选项

描述

foreground

Specifies the text foreground color.

背景

Specifies the cell or item background color.

font

Specifies the font to use when drawing text.

图片

Specifies the item image, in case the item’s image option is empty.

25.2.9.4. Column Identifiers

Column identifiers take any of the following forms:

A symbolic name from the list of columns option.

An integer n, specifying the nth data column.

A string of the form #n, where n is an integer, specifying the nth display column.

注释:

Item’s option values may be displayed in a different order than the order in which they are stored.

Column #0 always refers to the tree column, even if show=”tree” is not specified.

A data column number is an index into an item’s option values list; a display column number is the column number in the tree where the values are displayed. Tree labels are displayed in column #0. If option displaycolumns is not set, then data column n is displayed in column #n+1. Again, column #0 always refers to the tree column.

25.2.9.5. Virtual Events

The Treeview widget generates the following virtual events.

Event

描述

<<TreeviewSelect>>

Generated whenever the selection changes.

<<TreeviewOpen>>

Generated just before settings the focus item to open=True.

<<TreeviewClose>>

Generated just after setting the focus item to open=False.

The Treeview.focus() and Treeview.selection() methods can be used to determine the affected item or items.

25.2.9.6. ttk.Treeview

class tkinter.ttk.Treeview

bbox(item, column=None)

Returns the bounding box (relative to the treeview widget’s window) of the specified item in the form (x, y, width, height).

If column is specified, returns the bounding box of that cell. If the item is not visible (i.e., if it is a descendant of a closed item or is scrolled offscreen), returns an empty string.

get_children(item=None)

Returns the list of children belonging to item.

If item is not specified, returns root children.

set_children(item, *newchildren)

Replaces item’s child with newchildren.

Children present in item that are not present in newchildren are detached from the tree. No items in newchildren may be an ancestor of item. Note that not specifying newchildren results in detaching item’s children.

column(column, option=None, **kw)

Query or modify the options for the specified column.

If kw is not given, returns a dict of the column option values. If option is specified then the value for that option is returned. Otherwise, sets the options to the corresponding values.

The valid options/values are:

id

Returns the column name. This is a read-only option.

anchor: One of the standard Tk anchor values.

Specifies how the text in this column should be aligned with respect to the cell.

minwidth: width

The minimum width of the column in pixels. The treeview widget will not make the column any smaller than specified by this option when the widget is resized or the user drags a column.

stretch: True/False

指明列宽度是否应该在部件大小被改变时进行相应的调整。

width: width

以像素表示的列宽度。

要配置树的列，则调用此方法并附带参数 column = “#0”

delete(*items)

Delete all specified items and all their descendants.

The root item may not be deleted.

detach(*items)

Unlinks all of the specified items from the tree.

The items and all of their descendants are still present, and may be reinserted at another point in the tree, but will not be displayed.

The root item may not be detached.

exists(item)

Returns True if the specified item is present in the tree.

focus(item=None)

If item is specified, sets the focus item to item. Otherwise, returns the current focus item, or ‘’ if there is none.

heading(column, option=None, **kw)

Query or modify the heading options for the specified column.

If kw is not given, returns a dict of the heading option values. If option is specified then the value for that option is returned. Otherwise, sets the options to the corresponding values.

The valid options/values are:

text: text

The text to display in the column heading.

image: imageName

Specifies an image to display to the right of the column heading.

anchor: anchor

Specifies how the heading text should be aligned. One of the standard Tk anchor values.

command: callback

A callback to be invoked when the heading label is pressed.

To configure the tree column heading, call this with column = “#0”.

identify(component, x, y)

Returns a description of the specified component under the point given by x and y, or the empty string if no such component is present at that position.

identify_row(y)

Returns the item ID of the item at position y.

identify_column(x)

Returns the data column identifier of the cell at position x.

The tree column has ID #0.

identify_region(x, y)

Returns one of:

region

meaning

heading

Tree heading area.

separator

Space between two columns headings.

tree

The tree area.

cell

A data cell.

Availability: Tk 8.6.

identify_element(x, y)

Returns the element at position x, y.

Availability: Tk 8.6.

index(item)

Returns the integer index of item within its parent’s list of children.

insert(parent, index, iid=None, **kw)

Creates a new item and returns the item identifier of the newly created item.

parent is the item ID of the parent item, or the empty string to create a new top-level item. index is an integer, or the value “end”, specifying where in the list of parent’s children to insert the new item. If index is less than or equal to zero, the new node is inserted at the beginning; if index is greater than or equal to the current number of children, it is inserted at the end. If iid is specified, it is used as the item identifier; iid must not already exist in the tree. Otherwise, a new unique identifier is generated.

See Item Options for the list of available points.

item(item, option=None, **kw)

Query or modify the options for the specified item.

If no options are given, a dict with options/values for the item is returned. If option is specified then the value for that option is returned. Otherwise, sets the options to the corresponding values as given by kw.

move(item, parent, index)

Moves item to position index in parent’s list of children.

It is illegal to move an item under one of its descendants. If index is less than or equal to zero, item is moved to the beginning; if greater than or equal to the number of children, it is moved to the end. If item was detached it is reattached.

next(item)

Returns the identifier of item’s next sibling, or ‘’ if item is the last child of its parent.

parent(item)

Returns the ID of the parent of item, or ‘’ if item is at the top level of the hierarchy.

prev(item)

Returns the identifier of item’s previous sibling, or ‘’ if item is the first child of its parent.

reattach(item, parent, index)

An alias for Treeview.move().

see(item)

Ensure that item is visible.

Sets all of item’s ancestors open option to True, and scrolls the widget if necessary so that item is within the visible portion of the tree.

selection(selop=None, items=None)

If selop is not specified, returns selected items. Otherwise, it will act according to the following selection methods.

Deprecated since version 3.6, will be removed in version 3.8: Using selection() for changing the selection state is deprecated. Use the following selection methods instead.

selection_set(*items)

items becomes the new selection.

在 3.6 版更改: items can be passed as separate arguments, not just as a single tuple.

selection_add(*items)

Add items to the selection.

在 3.6 版更改: items can be passed as separate arguments, not just as a single tuple.

selection_remove(*items)

Remove items from the selection.

在 3.6 版更改: items can be passed as separate arguments, not just as a single tuple.

selection_toggle(*items)

Toggle the selection state of each item in items.

在 3.6 版更改: items can be passed as separate arguments, not just as a single tuple.

set(item, column=None, value=None)

With one argument, returns a dictionary of column/value pairs for the specified item. With two arguments, returns the current value of the specified column. With three arguments, sets the value of given column in given item to the specified value.

tag_bind(tagname, sequence=None, callback=None)

Bind a callback for the given event sequence to the tag tagname. When an event is delivered to an item, the callbacks for each of the item’s tags option are called.

tag_configure(tagname, option=None, **kw)

Query or modify the options for the specified tagname.

If kw is not given, returns a dict of the option settings for tagname. If option is specified, returns the value for that option for the specified tagname. Otherwise, sets the options to the corresponding values for the given tagname.

tag_has(tagname, item=None)

If item is specified, returns 1 or 0 depending on whether the specified item has the given tagname. Otherwise, returns a list of all items that have the specified tag.

Availability: Tk 8.6

xview(*args)

Query or modify horizontal position of the treeview.

yview(*args)

Query or modify vertical position of the treeview.

25.2.10. Ttk Styling

Each widget in ttk is assigned a style, which specifies the set of elements making up the widget and how they are arranged, along with dynamic and default settings for element options. By default the style name is the same as the widget’s class name, but it may be overridden by the widget’s style option. If you don’t know the class name of a widget, use the method Misc.winfo_class() (somewidget.winfo_class()).

参见

Tcl’2004 conference presentation [http://tktable.sourceforge.net/tile/tile-tcl2004.pdf]

This document explains how the theme engine works

class tkinter.ttk.Style

This class is used to manipulate the style database.

configure(style, query_opt=None, **kw)

Query or set the default value of the specified option(s) in style.

Each key in kw is an option and each value is a string identifying the value for that option.

For example, to change every default button to be a flat button with some padding and a different background color:

from tkinter import ttk
import tkinter

root = tkinter.Tk()

ttk.Style().configure("TButton", padding=6, relief="flat",
   background="#ccc")

btn = ttk.Button(text="Sample")
btn.pack()

root.mainloop()

map(style, query_opt=None, **kw)

Query or sets dynamic values of the specified option(s) in style.

Each key in kw is an option and each value should be a list or a tuple (usually) containing statespecs grouped in tuples, lists, or some other preference. A statespec is a compound of one or more states and then a value.

An example may make it more understandable:

import tkinter
from tkinter import ttk

root = tkinter.Tk()

style = ttk.Style()
style.map("C.TButton",
    foreground=[('pressed', 'red'), ('active', 'blue')],
    background=[('pressed', '!disabled', 'black'), ('active', 'white')]
    )

colored_btn = ttk.Button(text="Test", style="C.TButton").pack()

root.mainloop()

Note that the order of the (states, value) sequences for an option does matter, if the order is changed to [('active', 'blue'), ('pressed', 'red')] in the foreground option, for example, the result would be a blue foreground when the widget were in active or pressed states.

lookup(style, option, state=None, default=None)

Returns the value specified for option in style.

If state is specified, it is expected to be a sequence of one or more states. If the default argument is set, it is used as a fallback value in case no specification for option is found.

To check what font a Button uses by default:

from tkinter import ttk

print(ttk.Style().lookup("TButton", "font"))

layout(style, layoutspec=None)

Define the widget layout for given style. If layoutspec is omitted, return the layout specification for given style.

layoutspec, if specified, is expected to be a list or some other sequence type (excluding strings), where each item should be a tuple and the first item is the layout name and the second item should have the format described in Layouts.

To understand the format, see the following example (it is not intended to do anything useful):

from tkinter import ttk
import tkinter

root = tkinter.Tk()

style = ttk.Style()
style.layout("TMenubutton", [
   ("Menubutton.background", None),
   ("Menubutton.button", {"children":
       [("Menubutton.focus", {"children":
           [("Menubutton.padding", {"children":
               [("Menubutton.label", {"side": "left", "expand": 1})]
           })]
       })]
   }),
])

mbtn = ttk.Menubutton(text='Text')
mbtn.pack()
root.mainloop()

element_create(elementname, etype, *args, **kw)

Create a new element in the current theme, of the given etype which is expected to be either “image”, “from” or “vsapi”. The latter is only available in Tk 8.6a for Windows XP and Vista and is not described here.

If “image” is used, args should contain the default image name followed by statespec/value pairs (this is the imagespec), and kw may have the following options:

border=padding

padding is a list of up to four integers, specifying the left, top, right, and bottom borders, respectively.

height=height

Specifies a minimum height for the element. If less than zero, the base image’s height is used as a default.

padding=padding

Specifies the element’s interior padding. Defaults to border’s value if not specified.

sticky=spec

Specifies how the image is placed within the final parcel. spec contains zero or more characters “n”, “s”, “w”, or “e”.

width=width

Specifies a minimum width for the element. If less than zero, the base image’s width is used as a default.

If “from” is used as the value of etype, element_create() will clone an existing element. args is expected to contain a themename, from which the element will be cloned, and optionally an element to clone from. If this element to clone from is not specified, an empty element will be used. kw is discarded.

element_names()

Returns the list of elements defined in the current theme.

element_options(elementname)

Returns the list of elementname’s options.

theme_create(themename, parent=None, settings=None)

Create a new theme.

It is an error if themename already exists. If parent is specified, the new theme will inherit styles, elements and layouts from the parent theme. If settings are present they are expected to have the same syntax used for theme_settings().

theme_settings(themename, settings)

Temporarily sets the current theme to themename, apply specified settings and then restore the previous theme.

Each key in settings is a style and each value may contain the keys ‘configure’, ‘map’, ‘layout’ and ‘element create’ and they are expected to have the same format as specified by the methods Style.configure(), Style.map(), Style.layout() and Style.element_create() respectively.

As an example, let’s change the Combobox for the default theme a bit:

from tkinter import ttk
import tkinter

root = tkinter.Tk()

style = ttk.Style()
style.theme_settings("default", {
   "TCombobox": {
       "configure": {"padding": 5},
       "map": {
           "background": [("active", "green2"),
                          ("!disabled", "green4")],
           "fieldbackground": [("!disabled", "green3")],
           "foreground": [("focus", "OliveDrab1"),
                          ("!disabled", "OliveDrab2")]
       }
   }
})

combo = ttk.Combobox().pack()

root.mainloop()

theme_names()

Returns a list of all known themes.

theme_use(themename=None)

If themename is not given, returns the theme in use. Otherwise, sets the current theme to themename, refreshes all widgets and emits a <<ThemeChanged>> event.

25.2.10.1. Layouts

A layout can be just None, if it takes no options, or a dict of options specifying how to arrange the element. The layout mechanism uses a simplified version of the pack geometry manager: given an initial cavity, each element is allocated a parcel. Valid options/values are:

side: whichside

Specifies which side of the cavity to place the element; one of top, right, bottom or left. If omitted, the element occupies the entire cavity.

sticky: nswe

Specifies where the element is placed inside its allocated parcel.

unit: 0 or 1

If set to 1, causes the element and all of its descendants to be treated as a single element for the purposes of Widget.identify() et al. It’s used for things like scrollbar thumbs with grips.

children: [sublayout… ]

Specifies a list of elements to place inside the element. Each element is a tuple (or other sequence type) where the first item is the layout name, and the other is a Layout.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

25. Tk图形用户界面(GUI) »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

25. Tk图形用户界面(GUI) »

|

25.3. tkinter.tix — Extension widgets for Tk

Source code: Lib/tkinter/tix.py [https://github.com/python/cpython/tree/3.6/Lib/tkinter/tix.py]

3.6 版后已移除: This Tk extension is unmaintained and should not be used in new code. Use tkinter.ttk instead.

The tkinter.tix (Tk Interface Extension) module provides an additional rich set of widgets. Although the standard Tk library has many useful widgets, they are far from complete. The tkinter.tix library provides most of the commonly needed widgets that are missing from standard Tk: HList, ComboBox, Control (a.k.a. SpinBox) and an assortment of scrollable widgets. tkinter.tix also includes many more widgets that are generally useful in a wide range of applications: NoteBook, FileEntry, PanedWindow, etc; there are more than 40 of them.

With all these new widgets, you can introduce new interaction techniques into applications, creating more useful and more intuitive user interfaces. You can design your application by choosing the most appropriate widgets to match the special needs of your application and users.

参见

Tix Homepage [http://tix.sourceforge.net/]

The home page for Tix. This includes links to additional documentation and downloads.

Tix Man Pages [http://tix.sourceforge.net/dist/current/man/]

On-line version of the man pages and reference material.

Tix Programming Guide [http://tix.sourceforge.net/dist/current/docs/tix-book/tix.book.html]

On-line version of the programmer’s reference material.

Tix Development Applications [http://tix.sourceforge.net/Tixapps/src/Tide.html]

Tix applications for development of Tix and Tkinter programs. Tide applications work under Tk or Tkinter, and include TixInspect, an inspector to remotely modify and debug Tix/Tk/Tkinter applications.

25.3.1. Using Tix

class tkinter.tix.Tk(screenName=None, baseName=None, className='Tix')

Toplevel widget of Tix which represents mostly the main window of an application. It has an associated Tcl interpreter.

Classes in the tkinter.tix module subclasses the classes in the tkinter. The former imports the latter, so to use tkinter.tix with Tkinter, all you need to do is to import one module. In general, you can just import tkinter.tix, and replace the toplevel call to tkinter.Tk with tix.Tk:

from tkinter import tix
from tkinter.constants import *
root = tix.Tk()

To use tkinter.tix, you must have the Tix widgets installed, usually alongside your installation of the Tk widgets. To test your installation, try the following:

from tkinter import tix
root = tix.Tk()
root.tk.eval('package require Tix')

If this fails, you have a Tk installation problem which must be resolved before proceeding. Use the environment variable TIX_LIBRARY to point to the installed Tix library directory, and make sure you have the dynamic object library (tix8183.dll or libtix8183.so) in the same directory that contains your Tk dynamic object library (tk8183.dll or libtk8183.so). The directory with the dynamic object library should also have a file called pkgIndex.tcl (case sensitive), which contains the line:

package ifneeded Tix 8.1 [list load "[file join $dir tix8183.dll]" Tix]

25.3.2. Tix Widgets

Tix [http://tix.sourceforge.net/dist/current/man/html/TixCmd/TixIntro.htm] introduces over 40 widget classes to the tkinter repertoire.

25.3.2.1. Basic Widgets

class tkinter.tix.Balloon

A Balloon [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixBalloon.htm] that pops up over a widget to provide help. When the user moves the cursor inside a widget to which a Balloon widget has been bound, a small pop-up window with a descriptive message will be shown on the screen.

class tkinter.tix.ButtonBox

The ButtonBox [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixButtonBox.htm] widget creates a box of buttons, such as is commonly used for Ok Cancel.

class tkinter.tix.ComboBox

The ComboBox [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixComboBox.htm] widget is similar to the combo box control in MS Windows. The user can select a choice by either typing in the entry subwidget or selecting from the listbox subwidget.

class tkinter.tix.Control

The Control [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixControl.htm] widget is also known as the SpinBox widget. The user can adjust the value by pressing the two arrow buttons or by entering the value directly into the entry. The new value will be checked against the user-defined upper and lower limits.

class tkinter.tix.LabelEntry

The LabelEntry [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixLabelEntry.htm] widget packages an entry widget and a label into one mega widget. It can be used to simplify the creation of “entry-form” type of interface.

class tkinter.tix.LabelFrame

The LabelFrame [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixLabelFrame.htm] widget packages a frame widget and a label into one mega widget. To create widgets inside a LabelFrame widget, one creates the new widgets relative to the frame subwidget and manage them inside the frame subwidget.

class tkinter.tix.Meter

The Meter [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixMeter.htm] widget can be used to show the progress of a background job which may take a long time to execute.

class tkinter.tix.OptionMenu

The OptionMenu [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixOptionMenu.htm] creates a menu button of options.

class tkinter.tix.PopupMenu

The PopupMenu [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixPopupMenu.htm] widget can be used as a replacement of the tk_popup command. The advantage of the Tix PopupMenu widget is it requires less application code to manipulate.

class tkinter.tix.Select

The Select [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixSelect.htm] widget is a container of button subwidgets. It can be used to provide radio-box or check-box style of selection options for the user.

class tkinter.tix.StdButtonBox

The StdButtonBox [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixStdButtonBox.htm] widget is a group of standard buttons for Motif-like dialog boxes.

25.3.2.2. File Selectors

class tkinter.tix.DirList

The DirList [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixDirList.htm] widget displays a list view of a directory, its previous directories and its sub-directories. The user can choose one of the directories displayed in the list or change to another directory.

class tkinter.tix.DirTree

The DirTree [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixDirTree.htm] widget displays a tree view of a directory, its previous directories and its sub-directories. The user can choose one of the directories displayed in the list or change to another directory.

class tkinter.tix.DirSelectDialog

The DirSelectDialog [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixDirSelectDialog.htm] widget presents the directories in the file system in a dialog window. The user can use this dialog window to navigate through the file system to select the desired directory.

class tkinter.tix.DirSelectBox

The DirSelectBox is similar to the standard Motif(TM) directory-selection box. It is generally used for the user to choose a directory. DirSelectBox stores the directories mostly recently selected into a ComboBox widget so that they can be quickly selected again.

class tkinter.tix.ExFileSelectBox

The ExFileSelectBox [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixExFileSelectBox.htm] widget is usually embedded in a tixExFileSelectDialog widget. It provides a convenient method for the user to select files. The style of the ExFileSelectBox widget is very similar to the standard file dialog on MS Windows 3.1.

class tkinter.tix.FileSelectBox

The FileSelectBox [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixFileSelectBox.htm] is similar to the standard Motif(TM) file-selection box. It is generally used for the user to choose a file. FileSelectBox stores the files mostly recently selected into a ComboBox widget so that they can be quickly selected again.

class tkinter.tix.FileEntry

The FileEntry [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixFileEntry.htm] widget can be used to input a filename. The user can type in the filename manually. Alternatively, the user can press the button widget that sits next to the entry, which will bring up a file selection dialog.

25.3.2.3. Hierarchical ListBox

class tkinter.tix.HList

The HList [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixHList.htm] widget can be used to display any data that have a hierarchical structure, for example, file system directory trees. The list entries are indented and connected by branch lines according to their places in the hierarchy.

class tkinter.tix.CheckList

The CheckList [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixCheckList.htm] widget displays a list of items to be selected by the user. CheckList acts similarly to the Tk checkbutton or radiobutton widgets, except it is capable of handling many more items than checkbuttons or radiobuttons.

class tkinter.tix.Tree

The Tree [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixTree.htm] widget can be used to display hierarchical data in a tree form. The user can adjust the view of the tree by opening or closing parts of the tree.

25.3.2.4. Tabular ListBox

class tkinter.tix.TList

The TList [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixTList.htm] widget can be used to display data in a tabular format. The list entries of a TList widget are similar to the entries in the Tk listbox widget. The main differences are (1) the TList widget can display the list entries in a two dimensional format and (2) you can use graphical images as well as multiple colors and fonts for the list entries.

25.3.2.5. Manager Widgets

class tkinter.tix.PanedWindow

The PanedWindow [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixPanedWindow.htm] widget allows the user to interactively manipulate the sizes of several panes. The panes can be arranged either vertically or horizontally. The user changes the sizes of the panes by dragging the resize handle between two panes.

class tkinter.tix.ListNoteBook

The ListNoteBook [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixListNoteBook.htm] widget is very similar to the TixNoteBook widget: it can be used to display many windows in a limited space using a notebook metaphor. The notebook is divided into a stack of pages (windows). At one time only one of these pages can be shown. The user can navigate through these pages by choosing the name of the desired page in the hlist subwidget.

class tkinter.tix.NoteBook

The NoteBook [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixNoteBook.htm] widget can be used to display many windows in a limited space using a notebook metaphor. The notebook is divided into a stack of pages. At one time only one of these pages can be shown. The user can navigate through these pages by choosing the visual “tabs” at the top of the NoteBook widget.

25.3.2.6. Image Types

The tkinter.tix module adds:

pixmap [http://tix.sourceforge.net/dist/current/man/html/TixCmd/pixmap.htm] capabilities to all tkinter.tix and tkinter widgets to create color images from XPM files.

Compound [http://tix.sourceforge.net/dist/current/man/html/TixCmd/compound.htm] image types can be used to create images that consists of multiple horizontal lines; each line is composed of a series of items (texts, bitmaps, images or spaces) arranged from left to right. For example, a compound image can be used to display a bitmap and a text string simultaneously in a Tk Button widget.

25.3.2.7. Miscellaneous Widgets

class tkinter.tix.InputOnly

The InputOnly [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixInputOnly.htm] widgets are to accept inputs from the user, which can be done with the bind command (Unix only).

25.3.2.8. Form Geometry Manager

In addition, tkinter.tix augments tkinter by providing:

class tkinter.tix.Form

The Form [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tixForm.htm] geometry manager based on attachment rules for all Tk widgets.

25.3.3. Tix Commands

class tkinter.tix.tixCommand

The tix commands [http://tix.sourceforge.net/dist/current/man/html/TixCmd/tix.htm] provide access to miscellaneous elements of Tix’s internal state and the Tix application context. Most of the information manipulated by these methods pertains to the application as a whole, or to a screen or display, rather than to a particular window.

To view the current settings, the common usage is:

from tkinter import tix
root = tix.Tk()
print(root.tix_configure())

tixCommand.tix_configure(cnf=None, **kw)

Query or modify the configuration options of the Tix application context. If no option is specified, returns a dictionary all of the available options. If option is specified with no value, then the method returns a list describing the one named option (this list will be identical to the corresponding sublist of the value returned if no option is specified). If one or more option-value pairs are specified, then the method modifies the given option(s) to have the given value(s); in this case the method returns an empty string. Option may be any of the configuration options.

tixCommand.tix_cget(option)

Returns the current value of the configuration option given by option. Option may be any of the configuration options.

tixCommand.tix_getbitmap(name)

Locates a bitmap file of the name name.xpm or name in one of the bitmap directories (see the tix_addbitmapdir() method). By using tix_getbitmap(), you can avoid hard coding the pathnames of the bitmap files in your application. When successful, it returns the complete pathname of the bitmap file, prefixed with the character @. The returned value can be used to configure the bitmap option of the Tk and Tix widgets.

tixCommand.tix_addbitmapdir(directory)

Tix maintains a list of directories under which the tix_getimage() and tix_getbitmap() methods will search for image files. The standard bitmap directory is $TIX_LIBRARY/bitmaps. The tix_addbitmapdir() method adds directory into this list. By using this method, the image files of an applications can also be located using the tix_getimage() or tix_getbitmap() method.

tixCommand.tix_filedialog([dlgclass])

Returns the file selection dialog that may be shared among different calls from this application. This method will create a file selection dialog widget when it is called the first time. This dialog will be returned by all subsequent calls to tix_filedialog(). An optional dlgclass parameter can be passed as a string to specified what type of file selection dialog widget is desired. Possible options are tix, FileSelectDialog or tixExFileSelectDialog.

tixCommand.tix_getimage(self, name)

Locates an image file of the name name.xpm, name.xbm or name.ppm in one of the bitmap directories (see the tix_addbitmapdir() method above). If more than one file with the same name (but different extensions) exist, then the image type is chosen according to the depth of the X display: xbm images are chosen on monochrome displays and color images are chosen on color displays. By using tix_getimage(), you can avoid hard coding the pathnames of the image files in your application. When successful, this method returns the name of the newly created image, which can be used to configure the image option of the Tk and Tix widgets.

tixCommand.tix_option_get(name)

Gets the options maintained by the Tix scheme mechanism.

tixCommand.tix_resetoptions(newScheme, newFontSet[, newScmPrio])

Resets the scheme and fontset of the Tix application to newScheme and newFontSet, respectively. This affects only those widgets created after this call. Therefore, it is best to call the resetoptions method before the creation of any widgets in a Tix application.

The optional parameter newScmPrio can be given to reset the priority level of the Tk options set by the Tix schemes.

Because of the way Tk handles the X option database, after Tix has been has imported and inited, it is not possible to reset the color schemes and font sets using the tix_config() method. Instead, the tix_resetoptions() method must be used.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

25. Tk图形用户界面(GUI) »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

25. Tk图形用户界面(GUI) »

|

25.4. tkinter.scrolledtext — 滚动文字控件

源代码： Lib/tkinter/scrolledtext.py [https://github.com/python/cpython/tree/3.6/Lib/tkinter/scrolledtext.py]

tkinter.scrolledtext 模块提供一个同名的类，实现了一个带有垂直滚动条的文字控件。使用 ScrolledText 类会比直接配置一个文本控件和滚动条简单。它的构造函数与 tkinter.Text 类相同。

文本控件与滚动条打包在一个 Frame 中， Grid 方法和 Pack 方法的布局管理器从 Frame 对象中获得。这允许 ScrolledText 控件可以直接用于实现大多数正常的布局管理行为。

如果需要更具体的控制，可以使用以下属性：

ScrolledText.frame

围绕文本和滚动条控件的框架。

ScrolledText.vbar

滚动条控件。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

25. Tk图形用户界面(GUI) »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

25. Tk图形用户界面(GUI) »

|

25.5. IDLE

源代码： Lib/idlelib/ [https://github.com/python/cpython/tree/3.6/Lib/idlelib/]

IDLE 是 Python 所内置的开发与学习环境。

IDLE 具有以下特性：

编码于 100% 纯正的 Python，使用名为 tkinter 的图形用户界面工具

跨平台：在 Windows、Unix 和 macOS 上工作近似。

提供输入输出高亮和错误信息的 Python 命令行窗口 （交互解释器）

提供多次撤销操作、Python 语法高亮、智能缩进、函数调用提示、自动补全等功能的多窗口文本编辑器

在多个窗口中检索，在编辑器中替换文本，以及在多个文件中检索（通过 grep）

提供持久保存的断点调试、单步调试、查看本地和全局命名空间功能的调试器

配置、浏览以及其它对话框

25.5.1. 目录

IDEL 具有两个主要窗口类型，分别是命令行窗口和编辑器窗口。用户可以同时打开多个编辑器窗口。对于 Windows 和 Linux 平台，都有各自的主菜单。如下记录的每个菜单标识着与之关联的窗口类型。

导出窗口，例如使用 编辑=>在文件中查找 是编辑器窗口的的一个子类型。它们目前有着相同的主菜单，但是默认标题和上下文菜单不同。

On macOS, there is one application menu. It dynamically changes according to the window currently selected. It has an IDLE menu, and some entries described below are moved around to conform to Apple guidlines.

25.5.1.1. 文件目录 （命令行和编辑器）

新建文件

创建一个文件编辑器窗口。

打开…

使用打开窗口以打开一个已存在的文件。

近期文件

打开一个近期文件列表，选取一个以打开它。

打开模块…

打开一个已存在的模块 （搜索 sys.path）

类浏览器

于当前所编辑的文件中使用树形结构展示函数、类以及方法。在命令行中，首先打开一个模块。

路径浏览

在树状结构中展示 sys.path 目录、模块、函数、类和方法。

保存

如果文件已经存在，则将当前窗口保存至对应的文件。自打开或上次保存之后经过修改的文件的窗口标题栏首尾将出现星号 * 。如果没有对应的文件，则使用“另存为”代替。

保存为…

使用“保存为”对话框保存当前窗口。被保存的文件将作为当前窗口新的对应文件。

另存为副本…

保存当前窗口至另一个文件，而不修改当前对应文件。

打印窗口

通过默认打印机打印当前窗口。

关闭

关闭当前窗口（如果未保存则询问）。

退出

关闭所有窗口并退出 IDLE（如果未保存则询问）

25.5.1.2. 编辑目录（命令行和编辑器）

撤销操作

撤销当前窗口的最近一次操作。最高可以撤回 1000 条操作记录。

重做

重做当前窗口最近一次所撤销的操作。

剪切

复制选区至系统剪贴板，然后删除选区。

复制

复制选区至系统剪贴板。

粘贴

插入系统剪贴板的内容至当前窗口。

剪贴板功能也可用于上下文目录。

全选

选择当前窗口的全部内容。

查找…

打开一个提供多选项的查找窗口。

再次查找

重复上次搜索，如果结果存在。

查找选区

查找当前选中的字符串，如果存在

在文件中查找…

打开文件查找对话框。将结果输出至新的输出窗口。

替换…

打开 查找并替换 对话框。

前往行

Move cursor to the line number requested and make that line visible.

提示完成

Open a scrollable list allowing selection of keywords and attributes. See Completions in the Tips sections below.

展开文本

展开键入的前缀以匹配同一窗口中的完整单词；重复以获得不同的扩展。

显示调用贴士

After an unclosed parenthesis for a function, open a small window with function parameter hints.

显示周围括号

突出显示周围的括号。

25.5.1.3. 格式菜单（仅 window 编辑器）

缩进区域

将选定的行右移缩进宽度（默认为4个空格）。

区域减少缩进

将选定的行向左移动缩进宽度（默认为4个空格）。

区域注释

在所选行的前面插入 ##。

区域取消注释

从所选行中删除开头的 ＃ 或 ##。

区域添加制表符

将 前导 空格变成制表符。 （注意：我们建议使用4个空格来缩进Python代码。）

区域取消制表符

将 所有 制表符转换为正确的空格数。

切换标签

打开一个对话框，以在缩进和空格之间切换。

新缩进宽度

打开一个对话框以更改缩进宽度。 Python社区接受的默认值为4个空格。

格式段落

在注释块或多行字符串或字符串中的选定行中，重新格式化当前以空行分隔的段落。段落中的所有行的格式都将少于N列，其中N默认为72。

尾随空格

Remove trailing space and other whitespace characters after the last non-whitespace character of a line by applying str.rstrip to each line, including lines within multiline strings.

25.5.1.4. 运行菜单（仅 window 编辑器）

Python Shell

打开或唤醒Python Shell窗口。

检查模块

检查 “编辑器” 窗口中当前打开的模块的语法。如果尚未保存该模块，则 IDLE会提示用户保存或自动保存，如在 “空闲设置” 对话框的 “常规” 选项卡中所选择的那样。如果存在语法错误，则会在 “编辑器” 窗口中指示大概位置。

运行模块

Do Check Module (above). If no error, restart the shell to clean the environment, then execute the module. Output is displayed in the Shell window. Note that output requires use of print or write. When execution is complete, the Shell retains focus and displays a prompt. At this point, one may interactively explore the result of execution. This is similar to executing a file with python -i file at a command line.

25.5.1.5. Shell 菜单（仅 window 编辑器）

查看最近重启

将Shell窗口滚动到上一次Shell重启。

重启Shell

重新启动shell 以清理环境。

中断执行

停止正在运行的程序。

25.5.1.6. 调试菜单（仅 window 编辑器）

跳转到文件/行

Look on the current line. with the cursor, and the line above for a filename and line number. If found, open the file if not already open, and show the line. Use this to view source lines referenced in an exception traceback and lines found by Find in Files. Also available in the context menu of the Shell window and Output windows.

调试器（切换）

激活后，在Shell中输入的代码或从编辑器中运行的代码将在调试器下运行。在编辑器中，可以使用上下文菜单设置断点。此功能不完整，具有实验性。

堆栈查看器

在树状目录中显示最后一个异常的堆栈回溯，可以访问本地和全局。

自动打开堆栈查看器

在未处理的异常上切换自动打开堆栈查看器。

25.5.1.7. 选项菜单（命令行和编辑器）

配置 IDLE

Open a configuration dialog and change preferences for the following: fonts, indentation, keybindings, text color themes, startup windows and size, additional help sources, and extensions (see below). On macOS, open the configuration dialog by selecting Preferences in the application menu. To use a new built-in color theme (IDLE Dark) with older IDLEs, save it as a new custom theme.

Non-default user settings are saved in a .idlerc directory in the user’s home directory. Problems caused by bad user configuration files are solved by editing or deleting one or more of the files in .idlerc.

Code Context (toggle)(Editor Window only)

Open a pane at the top of the edit window which shows the block context of the code which has scrolled above the top of the window. Clicking a line in this pane exposes that line at the top of the editor.

25.5.1.8. Window 菜单（命令行和编辑器）

Zoom Height

Toggles the window between normal size and maximum height. The initial size defaults to 40 lines by 80 chars unless changed on the General tab of the Configure IDLE dialog.

The rest of this menu lists the names of all open windows; select one to bring it to the foreground (deiconifying it if necessary).

25.5.1.9. 帮助菜单（命令行和编辑器）

关于 IDLE

显示版本，版权，许可证，荣誉等。

IDLE 帮助

显示此 IDLE 文档，详细介绍菜单选项，基本编辑和导航以及其他技巧。

Python 文档

访问本地Python文档（如果已安装），或启动Web浏览器并打开docs.python.org显示最新的Python文档。

海龟演示

Run the turtledemo module with example python code and turtle drawings.

Additional help sources may be added here with the Configure IDLE dialog under the General tab. See the “Help sources” subsection below for more on Help menu choices.

25.5.1.10. 上下文菜单

Open a context menu by right-clicking in a window (Control-click on macOS). Context menus have the standard clipboard functions also on the Edit menu.

剪切

复制选区至系统剪贴板，然后删除选区。

复制

复制选区至系统剪贴板。

粘贴

插入系统剪贴板的内容至当前窗口。

Editor windows also have breakpoint functions. Lines with a breakpoint set are specially marked. Breakpoints only have an effect when running under the debugger. Breakpoints for a file are saved in the user’s .idlerc directory.

设置断点

在当前行设置断点

清除断点

清除当前行断点

shell 和输出窗口还具有以下内容。

跳转到文件/行

与调试菜单相同。

The Shell window also has an output squeezing facility explained in the the Python Shell window subsection below.

压缩

If the cursor is over an output line, squeeze all the output between the code above and the prompt below down to a ‘Squeezed text’ label.

25.5.2. 编辑和导航

25.5.2.1. 编辑窗口

IDLE may open editor windows when it starts, depending on settings and how you start IDLE. Thereafter, use the File menu. There can be only one open editor window for a given file.

The title bar contains the name of the file, the full path, and the version of Python and IDLE running the window. The status bar contains the line number (‘Ln’) and column number (‘Col’). Line numbers start with 1; column numbers with 0.

IDLE assumes that files with a known .py* extension contain Python code and that other files do not. Run Python code with the Run menu.

25.5.2.2. 按键绑定

在本节中，’C’ 是指 Windows 和 Unix 上的 Control 键，以及 macOS 上的 Command 键。

Backspace 向左删除; Del 向右删除

C-Backspace 向左删除单词; C-Del 向右删除单词

方向键和 Page Up/Page Down 移动

C-LeftArrow 和 C-RightArrow 按字移动

Home/End 跳转到行首/尾

C-Home/C-End 跳转到文档首/尾

一些有用的Emacs绑定是从Tcl / Tk继承的：

C-a 行首

C-e 行尾

C-k 删除行（但未将其放入剪贴板）

C-l center window around the insertion point

C-b go backward one character without deleting (usually you can also use the cursor key for this)

C-f go forward one character without deleting (usually you can also use the cursor key for this)

C-p go up one line (usually you can also use the cursor key for this)

C-d 删除下一个字符

Standard keybindings (like C-c to copy and C-v to paste) may work. Keybindings are selected in the Configure IDLE dialog.

25.5.2.3. 自动缩进

After a block-opening statement, the next line is indented by 4 spaces (in the Python Shell window by one tab). After certain keywords (break, return etc.) the next line is dedented. In leading indentation, Backspace deletes up to 4 spaces if they are there. Tab inserts spaces (in the Python Shell window one tab), number depends on Indent width. Currently, tabs are restricted to four spaces due to Tcl/Tk limitations.

See also the indent/dedent region commands in the edit menu.

25.5.2.4. 完成

Completions are supplied for functions, classes, and attributes of classes, both built-in and user-defined. Completions are also provided for filenames.

The AutoCompleteWindow (ACW) will open after a predefined delay (default is two seconds) after a ‘.’ or (in a string) an os.sep is typed. If after one of those characters (plus zero or more other characters) a tab is typed the ACW will open immediately if a possible continuation is found.

If there is only one possible completion for the characters entered, a Tab will supply that completion without opening the ACW.

‘Show Completions’ will force open a completions window, by default the C-space will open a completions window. In an empty string, this will contain the files in the current directory. On a blank line, it will contain the built-in and user-defined functions and classes in the current namespaces, plus any modules imported. If some characters have been entered, the ACW will attempt to be more specific.

If a string of characters is typed, the ACW selection will jump to the entry most closely matching those characters. Entering a tab will cause the longest non-ambiguous match to be entered in the Editor window or Shell. Two tab in a row will supply the current ACW selection, as will return or a double click. Cursor keys, Page Up/Down, mouse selection, and the scroll wheel all operate on the ACW.

“Hidden” attributes can be accessed by typing the beginning of hidden name after a ‘.’, e.g. ‘_’. This allows access to modules with __all__ set, or to class-private attributes.

Completions and the ‘Expand Word’ facility can save a lot of typing!

Completions are currently limited to those in the namespaces. Names in an Editor window which are not via __main__ and sys.modules will not be found. Run the module once with your imports to correct this situation. Note that IDLE itself places quite a few modules in sys.modules, so much can be found by default, e.g. the re module.

If you don’t like the ACW popping up unbidden, simply make the delay longer or disable the extension.

25.5.2.5. 提示

A calltip is shown when one types ( after the name of an accessible function. A name expression may include dots and subscripts. A calltip remains until it is clicked, the cursor is moved out of the argument area, or ) is typed. When the cursor is in the argument part of a definition, the menu or shortcut display a calltip.

A calltip consists of the function signature and the first line of the docstring. For builtins without an accessible signature, the calltip consists of all lines up the fifth line or the first blank line. These details may change.

The set of accessible functions depends on what modules have been imported into the user process, including those imported by Idle itself, and what definitions have been run, all since the last restart.

For example, restart the Shell and enter itertools.count(. A calltip appears because Idle imports itertools into the user process for its own use. (This could change.) Enter turtle.write( and nothing appears. Idle does not import turtle. The menu or shortcut do nothing either. Enter import turtle and then turtle.write( will work.

In an editor, import statements have no effect until one runs the file. One might want to run a file after writing the import statements at the top, or immediately run an existing file before editing.

25.5.2.6. Python Shell 窗口

With IDLE’s Shell, one enters, edits, and recalls complete statements. Most consoles and terminals only work with a single physical line at a time.

When one pastes code into Shell, it is not compiled and possibly executed until one hits Return. One may edit pasted code first. If one pastes more that one statement into Shell, the result will be a SyntaxError when multiple statements are compiled as if they were one.

The editing features described in previous subsections work when entering code interactively. IDLE’s Shell window also responds to the following keys.

C-c 中断执行命令

C-d sends end-of-file; closes window if typed at a >>> prompt

Alt-/ (Expand word) is also useful to reduce typing

历史命令

Alt-p retrieves previous command matching what you have typed. On macOS use C-p.

Alt-n retrieves next. On macOS use C-n.

Return while on any previous command retrieves that command

25.5.2.7. 文本颜色

Idle defaults to black on white text, but colors text with special meanings. For the shell, these are shell output, shell error, user output, and user error. For Python code, at the shell prompt or in an editor, these are keywords, builtin class and function names, names following class and def, strings, and comments. For any text window, these are the cursor (when present), found text (when possible), and selected text.

Text coloring is done in the background, so uncolorized text is occasionally visible. To change the color scheme, use the Configure IDLE dialog Highlighting tab. The marking of debugger breakpoint lines in the editor and text in popups and dialogs is not user-configurable.

25.5.3. 启动和代码执行

Upon startup with the -s option, IDLE will execute the file referenced by the environment variables IDLESTARTUP or PYTHONSTARTUP. IDLE first checks for IDLESTARTUP; if IDLESTARTUP is present the file referenced is run. If IDLESTARTUP is not present, IDLE checks for PYTHONSTARTUP. Files referenced by these environment variables are convenient places to store functions that are used frequently from the IDLE shell, or for executing import statements to import common modules.

In addition, Tk also loads a startup file if it is present. Note that the Tk file is loaded unconditionally. This additional file is .Idle.py and is looked for in the user’s home directory. Statements in this file will be executed in the Tk namespace, so this file is not useful for importing functions to be used from IDLE’s Python shell.

25.5.3.1. 命令行语法

idle.py [-c command] [-d] [-e] [-h] [-i] [-r file] [-s] [-t title] [-] [arg] ...

-c command  run command in the shell window
-d          enable debugger and open shell window
-e          open editor window
-h          print help message with legal combinations and exit
-i          open shell window
-r file     run file in shell window
-s          run $IDLESTARTUP or $PYTHONSTARTUP first, in shell window
-t title    set title of shell window
-           run stdin in shell (- must be last option before args)

如果有参数：

If -, -c, or r is used, all arguments are placed in sys.argv[1:...] and sys.argv[0] is set to '', '-c', or '-r'. No editor window is opened, even if that is the default set in the Options dialog.

Otherwise, arguments are files opened for editing and sys.argv reflects the arguments passed to IDLE itself.

25.5.3.2. 启动失败

IDLE uses a socket to communicate between the IDLE GUI process and the user code execution process. A connection must be established whenever the Shell starts or restarts. (The latter is indicated by a divider line that says ‘RESTART’). If the user process fails to connect to the GUI process, it displays a Tk error box with a ‘cannot connect’ message that directs the user here. It then exits.

A common cause of failure is a user-written file with the same name as a standard library module, such as random.py and tkinter.py. When such a file is located in the same directory as a file that is about to be run, IDLE cannot import the stdlib file. The current fix is to rename the user file.

Though less common than in the past, an antivirus or firewall program may stop the connection. If the program cannot be taught to allow the connection, then it must be turned off for IDLE to work. It is safe to allow this internal connection because no data is visible on external ports. A similar problem is a network mis-configuration that blocks connections.

Python installation issues occasionally stop IDLE: multiple versions can clash, or a single installation might need admin access. If one undo the clash, or cannot or does not want to run as admin, it might be easiest to completely remove Python and start over.

A zombie pythonw.exe process could be a problem. On Windows, use Task Manager to detect and stop one. Sometimes a restart initiated by a program crash or Keyboard Interrupt (control-C) may fail to connect. Dismissing the error box or Restart Shell on the Shell menu may fix a temporary problem.

When IDLE first starts, it attempts to read user configuration files in ~/.idlerc/ (~ is one’s home directory). If there is a problem, an error message should be displayed. Leaving aside random disk glitches, this can be prevented by never editing the files by hand, using the configuration dialog, under Options, instead Options. Once it happens, the solution may be to delete one or more of the configuration files.

If IDLE quits with no message, and it was not started from a console, try starting from a console (python -m idlelib) and see if a message appears.

25.5.3.3. 运行用户代码

With rare exceptions, the result of executing Python code with IDLE is intended to be the same as executing the same code by the default method, directly with Python in a text-mode system console or terminal window. However, the different interface and operation occasionally affect visible results. For instance, sys.modules starts with more entries, and threading.activeCount() returns 2 instead of 1.

By default, IDLE runs user code in a separate OS process rather than in the user interface process that runs the shell and editor. In the execution process, it replaces sys.stdin, sys.stdout, and sys.stderr with objects that get input from and send output to the Shell window. The original values stored in sys.__stdin__, sys.__stdout__, and sys.__stderr__ are not touched, but may be None.

When Shell has the focus, it controls the keyboard and screen. This is normally transparent, but functions that directly access the keyboard and screen will not work. These include system-specific functions that determine whether a key has been pressed and if so, which.

IDLE’s standard stream replacements are not inherited by subprocesses created in the execution process, whether directly by user code or by modules such as multiprocessing. If such subprocess use input from sys.stdin or print or write to sys.stdout or sys.stderr, IDLE should be started in a command line window. The secondary subprocess will then be attached to that window for input and output.

If sys is reset by user code, such as with importlib.reload(sys), IDLE’s changes are lost and input from the keyboard and output to the screen will not work correctly.

25.5.3.4. Shell中的用户输出

When a program outputs text, the result is determined by the corresponding output device. When IDLE executes user code, sys.stdout and sys.stderr are connected to the display area of IDLE’s Shell. Some of its features are inherited from the underlying Tk Text widget. Others are programmed additions. Where it matters, Shell is designed for development rather than production runs.

For instance, Shell never throws away output. A program that sends unlimited output to Shell will eventually fill memory, resulting in a memory error. In contrast, some system text windows only keep the last n lines of output. A Windows console, for instance, keeps a user-settable 1 to 9999 lines, with 300 the default.

Text widgets display a subset of Unicode, the Basic Multilingual Plane (BMP). Which characters get a proper glyph instead of a replacement box depends on the operating system and installed fonts. Newline characters cause following text to appear on a new line, but other control characters are either replaced with a box or deleted. However, repr(), which is used for interactive echo of expression values, replaces control characters, some BMP codepoints, and all non-BMP characters with escape codes before they are output.

Normal and error output are generally kept separate (on separate lines) from code input and each other. They each get different highlight colors.

For SyntaxError tracebacks, the normal ‘^’ marking where the error was detected is replaced by coloring the text with an error highlight. When code run from a file causes other exceptions, one may right click on a traceback line to jump to the corresponding line in an IDLE editor. The file will be opened if necessary.

Shell has a special facility for squeezing output lines down to a ‘Squeezed text’ label. This is done automatically for output over N lines (N = 50 by default). N can be changed in the PyShell section of the General page of the Settings dialog. Output with fewer lines can be squeezed by right clicking on the output. This can be useful lines long enough to slow down scrolling.

Squeezed output is expanded in place by double-clicking the label. It can also be sent to the clipboard or a separate view window by right-clicking the label.

25.5.3.5. 开发 tkinter 应用程序

IDLE is intentionally different from standard Python in order to facilitate development of tkinter programs. Enter import tkinter as tk; root = tk.Tk() in standard Python and nothing appears. Enter the same in IDLE and a tk window appears. In standard Python, one must also enter root.update() to see the window. IDLE does the equivalent in the background, about 20 times a second, which is about every 50 milleseconds. Next enter b = tk.Button(root, text='button'); b.pack(). Again, nothing visibly changes in standard Python until one enters root.update().

Most tkinter programs run root.mainloop(), which usually does not return until the tk app is destroyed. If the program is run with python -i or from an IDLE editor, a >>> shell prompt does not appear until mainloop() returns, at which time there is nothing left to interact with.

When running a tkinter program from an IDLE editor, one can comment out the mainloop call. One then gets a shell prompt immediately and can interact with the live application. One just has to remember to re-enable the mainloop call when running in standard Python.

25.5.3.6. 在没有子进程的情况下运行

By default, IDLE executes user code in a separate subprocess via a socket, which uses the internal loopback interface. This connection is not externally visible and no data is sent to or received from the Internet. If firewall software complains anyway, you can ignore it.

If the attempt to make the socket connection fails, Idle will notify you. Such failures are sometimes transient, but if persistent, the problem may be either a firewall blocking the connection or misconfiguration of a particular system. Until the problem is fixed, one can run Idle with the -n command line switch.

If IDLE is started with the -n command line switch it will run in a single process and will not create the subprocess which runs the RPC Python execution server. This can be useful if Python cannot create the subprocess or the RPC socket interface on your platform. However, in this mode user code is not isolated from IDLE itself. Also, the environment is not restarted when Run/Run Module (F5) is selected. If your code has been modified, you must reload() the affected modules and re-import any specific items (e.g. from foo import baz) if the changes are to take effect. For these reasons, it is preferable to run IDLE with the default subprocess if at all possible.

3.4 版后已移除.

25.5.4. 帮助和偏好

25.5.4.1. 帮助资源

Help menu entry “IDLE Help” displays a formatted html version of the IDLE chapter of the Library Reference. The result, in a read-only tkinter text window, is close to what one sees in a web browser. Navigate through the text with a mousewheel, the scrollbar, or up and down arrow keys held down. Or click the TOC (Table of Contents) button and select a section header in the opened box.

Help menu entry “Python Docs” opens the extensive sources of help, including tutorials, available at docs.python.org/x.y, where ‘x.y’ is the currently running Python version. If your system has an off-line copy of the docs (this may be an installation option), that will be opened instead.

Selected URLs can be added or removed from the help menu at any time using the General tab of the Configure IDLE dialog .

25.5.4.2. 偏好设定

The font preferences, highlighting, keys, and general preferences can be changed via Configure IDLE on the Option menu. Keys can be user defined; IDLE ships with four built-in key sets. In addition, a user can create a custom key set in the Configure IDLE dialog under the keys tab.

25.5.4.3. macOS 上的IDLE

Under System Preferences: Dock, one can set “Prefer tabs when opening documents” to “Always”. This setting is not compatible with the tk/tkinter GUI framework used by IDLE, and it breaks a few IDLE features.

25.5.4.4. 扩展

IDLE contains an extension facility. Preferences for extensions can be changed with the Extensions tab of the preferences dialog. See the beginning of config-extensions.def in the idlelib directory for further information. The only current default extension is zzdummy, an example also used for testing.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

25. Tk图形用户界面(GUI) »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

25. Tk图形用户界面(GUI) »

|

25.6. 其他图形用户界面（GUI）包

Python 可用的主要跨平台（Windows，Mac OS X，类Unix）GUI 工具：

参见

PyGObject [https://wiki.gnome.org/Projects/PyGObject]

PyGObject provides introspection bindings for C libraries using GObject [https://developer.gnome.org/gobject/stable/]. One of these libraries is the GTK+ 3 [http://www.gtk.org/] widget set. GTK+ comes with many more widgets than Tkinter provides. An online Python GTK+ 3 Tutorial [https://python-gtk-3-tutorial.readthedocs.org/en/latest/] is available.

PyGTK [http://www.pygtk.org/]

PyGTK 提供了对较旧版本的库 GTK+ 2 的绑定。 它使用面向对象接口，比 C 库的抽象层级略高。 此外也有对 GNOME [https://www.gnome.org/] 的绑定。 请参阅在线 教程 [http://www.pygtk.org/pygtk2tutorial/index.html]。

PyQt [https://riverbankcomputing.com/software/pyqt/intro]

PyQt 是一个针对 Qt 工具集通过 sip 包装的绑定。 Qt 是一个庞大的 C++ GUI 应用开发框架，同时适用于 Unix, Windows 和 Mac OS X。 sip 是一个用于为 C++ 库生成 Python 类绑定的库，它是针对 Python 特别设计的。

PySide [https://wiki.qt.io/PySide]

PySide is a newer binding to the Qt toolkit, provided by Nokia. Compared to PyQt, its licensing scheme is friendlier to non-open source applications.

wxPython [http://www.wxpython.org]

wxPython 是一个针对 Python 的跨平台 GUI 工具集，它基于热门的 wxWidgets [https://www.wxwidgets.org/] (原名 wxWindows) C++ 工具集进行构建。 它为 Windows, Mac OS X 和 Unix 系统上的应用提供了原生的外观效果，在可能的情况下尽量使用各平台的原生可视化部件。 （在类 Unix 系统上使用 GTK+）。 除了包含庞大的可视化部件集，wxPython 还提供了许多类用于在线文档和上下文感知帮助、打印、HTML 视图、低层级设备上下文绘图、拖放操作、系统剪贴板访问、基于 XML 的资源格式等等，并且包含一个不断增长的用户贡献模块库。

PyGTK, PyQt, and wxPython, all have a modern look and feel and more widgets than Tkinter. In addition, there are many other GUI toolkits for Python, both cross-platform, and platform-specific. See the GUI Programming [https://wiki.python.org/moin/GuiProgramming] page in the Python Wiki for a much more complete list, and also for links to documents where the different GUI toolkits are compared.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

25. Tk图形用户界面(GUI) »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

26. 开发工具

本章中描述的各模块可帮你编写 Python 程序。例如，pydoc 模块接受一个模块并根据该模块的内容来生成文档。doctest 和 unittest 这两个模块包含了用于编写单元测试的框架，并可用于自动测试所编写的代码，验证预期的输出是否产生。2to3 程序能够将 Python 2.x 源代码翻译成有效的 Python 3.x 源代码。

本章中描述的模块列表是：

26.1. typing — 类型标注支持

26.1.1. 类型别名

26.1.2. NewType

26.1.3. Callable

26.1.4. 泛型(Generic)

26.1.5. 用户定义的泛型类型

26.1.6. Any 类型

26.1.7. 类,函数和修饰器.

26.2. pydoc — 文档生成器和在线帮助系统

26.3. doctest — 测试交互性的Python示例

26.3.1. 简单用法：检查Docstrings中的示例

26.3.2. Simple Usage: Checking Examples in a Text File

26.3.3. How It Works

26.3.3.1. Which Docstrings Are Examined?

26.3.3.2. How are Docstring Examples Recognized?

26.3.3.3. What’s the Execution Context?

26.3.3.4. What About Exceptions?

26.3.3.5. Option Flags

26.3.3.6. Directives

26.3.3.7. 警告

26.3.4. Basic API

26.3.5. Unittest API

26.3.6. Advanced API

26.3.6.1. DocTest 对象

26.3.6.2. Example Objects

26.3.6.3. DocTestFinder 对象

26.3.6.4. DocTestParser 对象

26.3.6.5. DocTestRunner 对象

26.3.6.6. OutputChecker 对象

26.3.7. 调试

26.3.8. Soapbox

26.4. unittest — 单元测试框架

26.4.1. 基本实例

26.4.2. 命令行界面

26.4.2.1. 命令行选项

26.4.3. 探索性测试

26.4.4. 组织你的测试代码

26.4.5. 复用已有的测试代码

26.4.6. 跳过测试与预计的失败

26.4.7. Distinguishing test iterations using subtests

26.4.8. 类与函数

26.4.8.1. 测试用例

26.4.8.1.1. Deprecated aliases

26.4.8.2. Grouping tests

26.4.8.3. Loading and running tests

26.4.8.3.1. load_tests Protocol

26.4.9. Class and Module Fixtures

26.4.9.1. setUpClass and tearDownClass

26.4.9.2. setUpModule and tearDownModule

26.4.10. Signal Handling

26.5. unittest.mock — 模拟对象库

26.5.1. 快速上手

26.5.2. Mock 类

26.5.2.1. Calling

26.5.2.2. Deleting Attributes

26.5.2.3. Mock names and the name attribute

26.5.2.4. Attaching Mocks as Attributes

26.5.3. The patchers

26.5.3.1. patch

26.5.3.2. patch.object

26.5.3.3. patch.dict

26.5.3.4. patch.multiple

26.5.3.5. patch methods: start and stop

26.5.3.6. patch builtins

26.5.3.7. TEST_PREFIX

26.5.3.8. Nesting Patch Decorators

26.5.3.9. Where to patch

26.5.3.10. Patching Descriptors and Proxy Objects

26.5.4. MagicMock and magic method support

26.5.4.1. Mocking Magic Methods

26.5.4.2. Magic Mock

26.5.5. Helpers

26.5.5.1. sentinel

26.5.5.2. DEFAULT

26.5.5.3. call

26.5.5.4. create_autospec

26.5.5.5. ANY

26.5.5.6. FILTER_DIR

26.5.5.7. mock_open

26.5.5.8. Autospeccing

26.6. unittest.mock 上手指南

26.6.1. 使用 mock

26.6.1.1. 模拟方法调用

26.6.1.2. 对象上的方法调用的 mock

26.6.1.3. Mocking Classes

26.6.1.4. Naming your mocks

26.6.1.5. Tracking all Calls

26.6.1.6. Setting Return Values and Attributes

26.6.1.7. Raising exceptions with mocks

26.6.1.8. Side effect functions and iterables

26.6.1.9. Creating a Mock from an Existing Object

26.6.2. Patch Decorators

26.6.3. Further Examples

26.6.3.1. Mocking chained calls

26.6.3.2. Partial mocking

26.6.3.3. Mocking a Generator Method

26.6.3.4. Applying the same patch to every test method

26.6.3.5. Mocking Unbound Methods

26.6.3.6. Checking multiple calls with mock

26.6.3.7. Coping with mutable arguments

26.6.3.8. Nesting Patches

26.6.3.9. Mocking a dictionary with MagicMock

26.6.3.10. Mock subclasses and their attributes

26.6.3.11. Mocking imports with patch.dict

26.6.3.12. Tracking order of calls and less verbose call assertions

26.6.3.13. More complex argument matching

26.7. 2to3 - 自动将 Python 2 代码转为 Python 3 代码

26.7.1. 使用 2to3

26.7.2. 修复器

26.7.3. lib2to3 —— 2to3 支持库

26.8. test — Python回归测试包

26.8.1. Writing Unit Tests for the test package

26.8.2. Running tests using the command-line interface

26.9. test.support — Utilities for the Python test suite

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

26.1. typing — 类型标注支持

3.5 新版功能.

源码： Lib/typing.py [https://github.com/python/cpython/tree/3.6/Lib/typing.py]

注解

The typing module has been included in the standard library on a provisional basis. New features might be added and API may change even between minor releases if deemed necessary by the core developers.

此模块支持 PEP 484 [https://www.python.org/dev/peps/pep-0484] 和 PEP 526 [https://www.python.org/dev/peps/pep-0526] 指定的类型提示。最基本的支持由 Any，Union，Tuple，Callable，TypeVar 和 Generic 类型组成。有关完整的规范，请参阅 PEP 484 [https://www.python.org/dev/peps/pep-0484]。有关类型提示的简单介绍，请参阅 PEP 483 [https://www.python.org/dev/peps/pep-0483]。

函数接受并返回一个字符串，注释像下面这样:

def greeting(name: str) -> str:
    return 'Hello ' + name

在函数 greeting 中，参数 name 预期是 str 类型，并且返回 str 类型。子类型允许作为参数。

26.1.1. 类型别名

类型别名通过将类型分配给别名来定义。在这个例子中， Vector 和 List[float] 将被视为可互换的同义词:

from typing import List
Vector = List[float]

def scale(scalar: float, vector: Vector) -> Vector:
    return [scalar * num for num in vector]

# typechecks; a list of floats qualifies as a Vector.
new_vector = scale(2.0, [1.0, -4.2, 5.4])

类型别名可用于简化复杂类型签名。例如:

from typing import Dict, Tuple, List

ConnectionOptions = Dict[str, str]
Address = Tuple[str, int]
Server = Tuple[Address, ConnectionOptions]

def broadcast_message(message: str, servers: List[Server]) -> None:
    ...

# The static type checker will treat the previous type signature as
# being exactly equivalent to this one.
def broadcast_message(
        message: str,
        servers: List[Tuple[Tuple[str, int], Dict[str, str]]]) -> None:
    ...

请注意，None 作为类型提示是一种特殊情况，并且由 type(None) 取代。

26.1.2. NewType

使用 NewType() 辅助函数创建不同的类型:

from typing import NewType

UserId = NewType('UserId', int)
some_id = UserId(524313)

静态类型检查器会将新类型视为它是原始类型的子类。这对于帮助捕捉逻辑错误非常有用:

def get_user_name(user_id: UserId) -> str:
    ...

# typechecks
user_a = get_user_name(UserId(42351))

# does not typecheck; an int is not a UserId
user_b = get_user_name(-1)

您仍然可以对 UserId 类型的变量执行所有的 int 支持的操作，但结果将始终为 int 类型。这可以让你在需要 int 的地方传入 UserId，但会阻止你以无效的方式无意中创建 UserId:

# 'output' is of type 'int', not 'UserId'
output = UserId(23413) + UserId(54341)

请注意，这些检查仅通过静态类型检查程序强制执行。在运行时，Derived = NewType('Derived'，Base) 将 Derived 一个函数，该函数立即返回您传递它的任何参数。这意味着表达式 Derived(some_value) 不会创建一个新的类或引入任何超出常规函数调用的开销。

更确切地说，表达式 some_value is Derived(some_value) 在运行时总是为真。

这也意味着无法创建 Derived 的子类型，因为它是运行时的标识函数，而不是实际的类型:

from typing import NewType

UserId = NewType('UserId', int)

# Fails at runtime and does not typecheck
class AdminUserId(UserId): pass

但是，可以基于’derived’ NewType 创建 NewType()

from typing import NewType

UserId = NewType('UserId', int)

ProUserId = NewType('ProUserId', UserId)

并且 ProUserId 的类型检查将按预期工作。

有关更多详细信息，请参阅 PEP 484 [https://www.python.org/dev/peps/pep-0484]。

注解

回想一下，使用类型别名声明两种类型彼此 等效 。Alias = Original 将使静态类型检查对待所有情况下 Alias 完全等同于 Original。当您想简化复杂类型签名时，这很有用。

相反，NewType 声明一种类型是另一种类型的子类型。Derived = NewType('Derived', Original) 将使静态类型检查器将 Derived 当作 Original 的 子类 ，这意味着 Original 类型的值不能用于 Derived 类型的值需要的地方。当您想以最小的运行时间成本防止逻辑错误时，这非常有用。

3.5.2 新版功能.

26.1.3. Callable

期望特定签名的回调函数的框架可以将类型标注为 Callable[[Arg1Type, Arg2Type], ReturnType]。

例如

from typing import Callable

def feeder(get_next_item: Callable[[], str]) -> None:
    # Body

def async_query(on_success: Callable[[int], None],
                on_error: Callable[[int, Exception], None]) -> None:
    # Body

通过用文字省略号替换类型提示中的参数列表： Callable[...，ReturnType]，可以声明可调用的返回类型，而无需指定调用签名。

26.1.4. 泛型(Generic)

由于无法以通用方式静态推断有关保存在容器中的对象的类型信息，因此抽象基类已扩展为支持订阅以表示容器元素的预期类型。

from typing import Mapping, Sequence

def notify_by_email(employees: Sequence[Employee],
                    overrides: Mapping[str, str]) -> None: ...

泛型可以通过使用typing模块中名为 TypeVar 的新工厂进行参数化。

from typing import Sequence, TypeVar

T = TypeVar('T')      # Declare type variable

def first(l: Sequence[T]) -> T:   # Generic function
    return l[0]

26.1.5. 用户定义的泛型类型

用户定义的类可以定义为泛型类。

from typing import TypeVar, Generic
from logging import Logger

T = TypeVar('T')

class LoggedVar(Generic[T]):
    def __init__(self, value: T, name: str, logger: Logger) -> None:
        self.name = name
        self.logger = logger
        self.value = value

    def set(self, new: T) -> None:
        self.log('Set ' + repr(self.value))
        self.value = new

    def get(self) -> T:
        self.log('Get ' + repr(self.value))
        return self.value

    def log(self, message: str) -> None:
        self.logger.info('%s: %s', self.name, message)

Generic[T] 作为基类定义了类 LoggedVar 采用单个类型参数 T。这也使得 T 作为类体内的一个类型有效。

The Generic base class uses a metaclass that defines __getitem__() so that LoggedVar[t] is valid as a type:

from typing import Iterable

def zero_all_vars(vars: Iterable[LoggedVar[int]]) -> None:
    for var in vars:
        var.set(0)

泛型类型可以有任意数量的类型变量，并且类型变量可能会受到限制:

from typing import TypeVar, Generic
...

T = TypeVar('T')
S = TypeVar('S', int, str)

class StrangePair(Generic[T, S]):
    ...

Generic 每个参数的类型变量必须是不同的。这是无效的:

from typing import TypeVar, Generic
...

T = TypeVar('T')

class Pair(Generic[T, T]):   # INVALID
    ...

您可以对 Generic 使用多重继承:

from typing import TypeVar, Generic, Sized

T = TypeVar('T')

class LinkedList(Sized, Generic[T]):
    ...

从泛型类继承时，某些类型变量可能是固定的:

from typing import TypeVar, Mapping

T = TypeVar('T')

class MyDict(Mapping[str, T]):
    ...

在这种情况下，MyDict 只有一个参数，T。

在不指定类型参数的情况下使用泛型类别会为每个位置假设 Any。在下面的例子中，MyIterable 不是泛型，但是隐式继承自 Iterable[Any]:

from typing import Iterable

class MyIterable(Iterable): # Same as Iterable[Any]

用户定义的通用类型别名也受支持。例子:

from typing import TypeVar, Iterable, Tuple, Union
S = TypeVar('S')
Response = Union[Iterable[S], int]

# Return type here is same as Union[Iterable[str], int]
def response(query: str) -> Response[str]:
    ...

T = TypeVar('T', int, float, complex)
Vec = Iterable[Tuple[T, T]]

def inproduct(v: Vec[T]) -> T: # Same as Iterable[Tuple[T, T]]
    return sum(x*y for x, y in v)

The metaclass used by Generic is a subclass of abc.ABCMeta. A generic class can be an ABC by including abstract methods or properties, and generic classes can also have ABCs as base classes without a metaclass conflict. Generic metaclasses are not supported. The outcome of parameterizing generics is cached, and most types in the typing module are hashable and comparable for equality.

26.1.6. Any 类型

Any 是一种特殊的类型。静态类型检查器将所有类型视为与 Any 兼容，反之亦然， Any 也与所有类型相兼容。

这意味着可对类型为 Any 的值执行任何操作或方法调用，并将其赋值给任何变量:

from typing import Any

a = None    # type: Any
a = []      # OK
a = 2       # OK

s = ''      # type: str
s = a       # OK

def foo(item: Any) -> int:
    # Typechecks; 'item' could be any type,
    # and that type might have a 'bar' method
    item.bar()
    ...

需要注意的是，将 Any 类型的值赋值给另一个更具体的类型时，Python不会执行类型检查。例如，当把 a 赋值给 s 时，即使 s 被声明为 str 类型，在运行时接收到的是 int 值，静态类型检查器也不会报错。

此外，所有返回值无类型或形参无类型的函数将隐式地默认使用 Any 类型:

def legacy_parser(text):
    ...
    return data

# A static type checker will treat the above
# as having the same signature as:
def legacy_parser(text: Any) -> Any:
    ...
    return data

当需要混用动态类型和静态类型的代码时，上述行为可以让 Any 被用作 应急出口 。

Any 和 object 的行为对比。与 Any 相似，所有的类型都是 object 的子类型。然而不同于 Any，反之并不成立： object 不是 其他所有类型的子类型。

这意味着当一个值的类型是 object 的时候，类型检查器会拒绝对它的几乎所有的操作。把它赋值给一个指定了类型的变量（或者当作返回值）是一个类型错误。比如说：

def hash_a(item: object) -> int:
    # Fails; an object does not have a 'magic' method.
    item.magic()
    ...

def hash_b(item: Any) -> int:
    # Typechecks
    item.magic()
    ...

# Typechecks, since ints and strs are subclasses of object
hash_a(42)
hash_a("foo")

# Typechecks, since Any is compatible with all types
hash_b(42)
hash_b("foo")

使用 object 示意一个值可以类型安全地兼容任何类型。使用 Any 示意一个值地类型是动态定义的。

26.1.7. 类,函数和修饰器.

这个模块定义了如下的类,模块和修饰器.

class typing.TypeVar

类型变量

用法:

T = TypeVar('T')  # Can be anything
A = TypeVar('A', str, bytes)  # Must be str or bytes

Type variables exist primarily for the benefit of static type checkers. They serve as the parameters for generic types as well as for generic function definitions. See class Generic for more information on generic types. Generic functions work as follows:

def repeat(x: T, n: int) -> Sequence[T]:
    """Return a list containing n references to x."""
    return [x]*n

def longest(x: A, y: A) -> A:
    """Return the longest of two strings."""
    return x if len(x) >= len(y) else y

本质上，后例的签名重载了 (str, str) -> str 与 (bytes, bytes) -> bytes。注意，参数是 str 子类的实例时，返回类型仍是纯 str。

isinstance(x, T) 会在运行时抛出 TypeError 异常。一般地说， isinstance() 和 issubclass() 不应该和类型一起使用。

通过 covariant=True 或 contravariant=True 可以把类型变量标记为协变量或逆变量。详见 PEP 484 [https://www.python.org/dev/peps/pep-0484]。默认情况下，类型变量是不变量。类型变量还可以用 bound=<type> 指定上限。这里的意思是，（显式或隐式地）取代类型变量的实际类型必须是限定类型的子类，详见 PEP 484 [https://www.python.org/dev/peps/pep-0484]。

class typing.Generic

用于泛型类型的抽象基类。

泛型类型一般通过继承含一个或多个类型变量的类实例进行声明。例如，泛型映射类型定义如下：

class Mapping(Generic[KT, VT]):
    def __getitem__(self, key: KT) -> VT:
        ...
        # Etc.

这个类之后可以被这样用:

X = TypeVar('X')
Y = TypeVar('Y')

def lookup_name(mapping: Mapping[X, Y], key: X, default: Y) -> Y:
    try:
        return mapping[key]
    except KeyError:
        return default

class typing.Type(Generic[CT_co])

一个注解为 C 的变量可以接受一个类型为 C 的值。相对地，一个注解为 Type[C] 的变量可以接受本身为类的值 —— 更精确地说它接受 C 的 类对象 ，例如：

a = 3         # Has type 'int'
b = int       # Has type 'Type[int]'
c = type(a)   # Also has type 'Type[int]'

注意 Type[C] 是协变的：

class User: ...
class BasicUser(User): ...
class ProUser(User): ...
class TeamUser(User): ...

# Accepts User, BasicUser, ProUser, TeamUser, ...
def make_new_user(user_class: Type[User]) -> User:
    # ...
    return user_class()

 Type[C] 是协变的这一事实暗示了任何 C 的子类应当实现与 C 相同的构造器签名和类方法签名。类型检查器应当标记违反的情况，但应当也允许子类中调用构造器符合指示的基类。类型检查器被要求如何处理这种情况可能会在 PEP 484 [https://www.python.org/dev/peps/pep-0484] 将来的版本中改变。

 Type 合法的参数仅有类、 Any 、类型变量 以及上述类型的联合类型。例如：

def new_non_team_user(user_class: Type[Union[BaseUser, ProUser]]): ...

 Type[Any] 等价于 Type，因此继而等价于 type，它是 Python 的元类层级的根部。

3.5.2 新版功能.

class typing.Iterable(Generic[T_co])

collections.abc.Iterable 的泛型版本。

class typing.Iterator(Iterable[T_co])

collections.abc.Iterator 的泛型版本。

class typing.Reversible(Iterable[T_co])

collections.abc.Reversible 的泛型版本。

class typing.SupportsInt

含抽象方法 __int__ 的抽象基类。

class typing.SupportsFloat

含抽象方法 __float__ 的抽象基类。

class typing.SupportsComplex

含抽象方法 __complex__ 的抽象基类。

class typing.SupportsBytes

含抽象方法 __bytes__ 的抽象基类（ABC）

class typing.SupportsAbs

含抽象方法 __abs__ 的抽象基类（ABC）是其返回类型里的协变量。

class typing.SupportsRound

含抽象方法 __round__ 的抽象基类，是其返回类型的协变量。

class typing.Container(Generic[T_co])

collections.abc.Container 的泛型版本。

class typing.Hashable

collections.abc.Hashable 的别名。

class typing.Sized

collections.abc.Sized 的别名。

class typing.Collection(Sized, Iterable[T_co], Container[T_co])

collections.abc.Collection 的泛型版本。

3.6 新版功能.

class typing.AbstractSet(Sized, Collection[T_co])

collections.abc.Set 的泛型版本。

class typing.MutableSet(AbstractSet[T])

collections.abc.MutableSet 的泛型版本。

class typing.Mapping(Sized, Collection[KT], Generic[VT_co])

A generic version of collections.abc.Mapping.

class typing.MutableMapping(Mapping[KT, VT])

collections.abc.MutableMapping 的泛型版本。

class typing.Sequence(Reversible[T_co], Collection[T_co])

collections.abc.Sequence 的泛型版本。

class typing.MutableSequence(Sequence[T])

collections.abc.MutableSequence 的泛型版本。

class typing.ByteString(Sequence[int])

collections.abc.ByteString 的泛型版本。

This type represents the types bytes, bytearray, and memoryview.

作为该类型的简称，bytes 可用于标注上述任意类型的参数。

class typing.Deque(deque, MutableSequence[T])

collections.deque 的泛型版本。

3.6.1 新版功能.

class typing.List(list, MutableSequence[T])

Generic version of list. Useful for annotating return types. To annotate arguments it is preferred to use abstract collection types such as Mapping, Sequence, or AbstractSet.

这个类型可以这样用:

T = TypeVar('T', int, float)

def vec2(x: T, y: T) -> List[T]:
    return [x, y]

def keep_positives(vector: Sequence[T]) -> List[T]:
    return [item for item in vector if item > 0]

class typing.Set(set, MutableSet[T])

A generic version of builtins.set.

class typing.FrozenSet(frozenset, AbstractSet[T_co])

builtins.frozenset 的泛型版本。

class typing.MappingView(Sized, Iterable[T_co])

collections.abc.MappingView 的泛型版本。

class typing.KeysView(MappingView[KT_co], AbstractSet[KT_co])

collections.abc.KeysView 的泛型版本。

class typing.ItemsView(MappingView, Generic[KT_co, VT_co])

collections.abc.ItemsView 的泛型版本。

class typing.ValuesView(MappingView[VT_co])

collections.abc.ValuesView 的泛型版本。

class typing.Awaitable(Generic[T_co])

collections.abc.Awaitable 的泛型版本。

class typing.Coroutine(Awaitable[V_co], Generic[T_co T_contra, V_co])

collections.abc.Coroutine 的泛型版本。类型变量的的差异和顺序与 Generator 的内容相对应，例如：

from typing import List, Coroutine
c = None # type: Coroutine[List[str], str, int]
...
x = c.send('hi') # type: List[str]
async def bar() -> None:
    x = await c # type: int

class typing.AsyncIterable(Generic[T_co])

collections.abc.AsyncIterable 的泛型版本。

class typing.AsyncIterator(AsyncIterable[T_co])

collections.abc.AsyncIterator 的泛型版本。

class typing.ContextManager(Generic[T_co])

contextlib.AbstractContextManager 的泛型版本。

3.6 新版功能.

class typing.AsyncContextManager(Generic[T_co])

An ABC with async abstract __aenter__() and __aexit__() methods.

3.6 新版功能.

class typing.Dict(dict, MutableMapping[KT, VT])

A generic version of dict. The usage of this type is as follows:

def get_position_in_index(word_list: Dict[str, int], word: str) -> int:
    return word_list[word]

class typing.DefaultDict(collections.defaultdict, MutableMapping[KT, VT])

collections.defaultdict 的泛型版本。

3.5.2 新版功能.

class typing.Counter(collections.Counter, Dict[T, int])

collections.Counter 的泛型版本。

3.6.1 新版功能.

class typing.ChainMap(collections.ChainMap, MutableMapping[KT, VT])

collections.ChainMap 的泛型版本。

3.6.1 新版功能.

class typing.Generator(Iterator[T_co], Generic[T_co, T_contra, V_co])

生成器可以由泛型类型 Generator[YieldType, SendType, ReturnType] 注解。例如：

def echo_round() -> Generator[int, float, str]:
    sent = yield 0
    while sent >= 0:
        sent = yield round(sent)
    return 'Done'

注意，与 typing 模块里的其他泛型不同， Generator 的``SendType`` 的操作是逆变的， 不是协变，也是不变。

如果生成器只生成值，可将 SendType 与 ReturnType 设为 None：

def infinite_stream(start: int) -> Generator[int, None, None]:
    while True:
        yield start
        start += 1

另外，把生成器注解为返回类型

def infinite_stream(start: int) -> Iterator[int]:
    while True:
        yield start
        start += 1

class typing.AsyncGenerator(AsyncIterator[T_co], Generic[T_co, T_contra])

异步生成器可由泛型类型 AsyncGenerator[YieldType, SendType] 注解。例如：

async def echo_round() -> AsyncGenerator[int, float]:
    sent = yield 0
    while sent >= 0.0:
        rounded = await round(sent)
        sent = yield rounded

与常规生成器不同，异步生成器不能返回值，因此没有 ReturnType 类型参数。 与 Generator 类似，SendType 也属于逆变行为。

如果生成器只产生值，可将 SendType 设置为 None：

async def infinite_stream(start: int) -> AsyncGenerator[int, None]:
    while True:
        yield start
        start = await increment(start)

此外，可用 AsyncIterable[YieldType] 或 AsyncIterator[YieldType] 注解生成器的类型：

async def infinite_stream(start: int) -> AsyncIterator[int]:
    while True:
        yield start
        start = await increment(start)

3.5.4 新版功能.

class typing.Text

Text 是 str 的别名。提供了对 Python 2 代码的向下兼容：Python 2 中，Text 是``unicode`` 的别名。

用

def add_unicode_checkmark(text: Text) -> Text:
    return text + u' \u2713'

3.5.2 新版功能.

class typing.IO

class typing.TextIO

class typing.BinaryIO

泛型类型 IO[AnyStr] 及其子类 TextIO(IO[str]) 与 BinaryIO(IO[bytes]) 表示 I/O 流的类型，例如 open() 所返回的对象。

class typing.Pattern

class typing.Match

These type aliases correspond to the return types from re.compile() and re.match(). These types (and the corresponding functions) are generic in AnyStr and can be made specific by writing Pattern[str], Pattern[bytes], Match[str], or Match[bytes].

class typing.NamedTuple

Typed version of namedtuple.

用法:

class Employee(NamedTuple):
    name: str
    id: int

这相当于:

Employee = collections.namedtuple('Employee', ['name', 'id'])

为字段提供默认值，要在类体内赋值：

class Employee(NamedTuple):
    name: str
    id: int = 3

employee = Employee('Guido')
assert employee.id == 3

带默认值的字段必须在不带默认值的字段后面。

The resulting class has two extra attributes: _field_types, giving a dict mapping field names to types, and _field_defaults, a dict mapping field names to default values. (The field names are in the _fields attribute, which is part of the namedtuple API.)

NamedTuple 子类也支持文档字符串与方法：

class Employee(NamedTuple):
    """Represents an employee."""
    name: str
    id: int = 3

    def __repr__(self) -> str:
        return f'<Employee {self.name}, id={self.id}>'

反向兼容用法：

Employee = NamedTuple('Employee', [('name', str), ('id', int)])

在 3.6 版更改: 添加了对 PEP 526 [https://www.python.org/dev/peps/pep-0526] 中变量注解句法的支持。

在 3.6.1 版更改: 添加了对默认值、方法、文档字符串的支持。

typing.NewType(typ)

A helper function to indicate a distinct types to a typechecker, see NewType. At runtime it returns a function that returns its argument. Usage:

UserId = NewType('UserId', int)
first_user = UserId(1)

3.5.2 新版功能.

typing.cast(typ, val)

把值强制转换为类型。

不变更返回值。对类型检查器来说，这代表了返回值具有指定的类型，但在运行时，故意不做任何检查（目的是让该检查速度尽量快）。

typing.get_type_hints(obj[, globals[, locals]])

返回一个字典，字典内含有函数、方法、模块或类对象的类型提示。

一般情况下，与 obj.__annotations__ 相同。此外，可通过在 globals 与 locals 命名空间里进行评估，以此来处理编码为字符串字面量的前向引用。如有需要，在默认值设置为 None``时，可为函数或方法注解添加 ``Optional[t]。对于类 C，则返回一个由所有 __annotations__ 与 C.__mro__ 逆序合并而成的字典。

@typing.overload

The @overload decorator allows describing functions and methods that support multiple different combinations of argument types. A series of @overload-decorated definitions must be followed by exactly one non-@overload-decorated definition (for the same function/method). The @overload-decorated definitions are for the benefit of the type checker only, since they will be overwritten by the non-@overload-decorated definition, while the latter is used at runtime but should be ignored by a type checker. At runtime, calling a @overload-decorated function directly will raise NotImplementedError. An example of overload that gives a more precise type than can be expressed using a union or a type variable:

@overload
def process(response: None) -> None:
    ...
@overload
def process(response: int) -> Tuple[int, str]:
    ...
@overload
def process(response: bytes) -> str:
    ...
def process(response):
    <actual implementation>

详见 PEP 484 [https://www.python.org/dev/peps/pep-0484]，与其他类型语义进行对比。

@typing.no_type_check

用于指明标注不是类型提示的装饰器。

此 decorator 装饰器生效于类或函数上。如果作用于类上的话，它会递归地作用于这个类的所定义的所有方法上（但是对于超类或子类所定义的方法不会生效）。

此方法会就地地修改函数。

@typing.no_type_check_decorator

使其它装饰器起到 no_type_check() 效果的装饰器。

本装饰器用 no_type_check() 里的装饰函数打包其他装饰器。

typing.Any

特殊类型，表明类型没有任何限制。

每一个类型都对 Any 兼容。

Any 对每一个类型都兼容。

typing.NoReturn

标记一个函数没有返回值的特殊类型。比如说:

from typing import NoReturn

def stop() -> NoReturn:
    raise RuntimeError('no way')

3.6.5 新版功能.

typing.Union

联合类型； Union[X, Y] 意味着：要不是 X，要不是 Y。

使用形如 Union[int, str] 的形式来定义一个联合类型。细节如下:

参数必须是类型，而且必须至少有一个参数。

联合类型的联合类型会被展开打平，比如:

Union[Union[int, str], float] == Union[int, str, float]

仅有一个参数的联合类型会坍缩成参数自身，比如:

Union[int] == int  # The constructor actually returns int

多余的参数会被跳过，比如:

Union[int, str, int] == Union[int, str]

在比较联合类型的时候，参数顺序会被忽略，比如:

Union[int, str] == Union[str, int]

When a class and its subclass are present, the latter is skipped, e.g.:

Union[int, object] == object

你不能继承或者实例化一个联合类型。

你不能写成 Union[X][Y] 。

你可以使用 Optional[X] 作为 Union[X, None] 的缩写。

typing.Optional

可选类型。

 Optional[X] 等价于 Union[X, None] 。

请注意，这与可选参数并非相同的概念。可选参数是一个具有默认值的参数。可选参数的类型注解并不因为它是可选的就需要 Optional 限定符。例如：

def foo(arg: int = 0) -> None:
    ...

另一方面，如果允许显式地传递值 None ， 使用 Optional 也是正当的，无论该参数是否是可选的。例如：

def foo(arg: Optional[int] = None) -> None:
    ...

typing.Tuple

Tuple type; Tuple[X, Y] is the type of a tuple of two items with the first item of type X and the second of type Y.

举例： Tuple[T1, T2] 是一个二元组，类型分别为 T1 和 T2。 Tuple[int, float, str] 是一个由整数、浮点数和字符串组成的三元组。

为表达一个同类型元素的变长元组，使用省略号字面量，如 Tuple[int, ...] 。单独的一个 Tuple 等价于 Tuple[Any, ...]，进而等价于 tuple 。

typing.Callable

可调用类型； Callable[[int], str] 是一个函数，接受一个 int 参数，返回一个 str 。

下标值的语法必须恰为两个值：参数列表和返回类型。参数列表必须是一个类型和省略号组成的列表；返回值必须是单一一个类型。

不存在语法来表示可选的或关键词参数，这类函数类型罕见用于回调函数。 Callable[..., ReturnType] （使用字面省略号）能被用于提示一个可调用对象，接受任意数量的参数并且返回 ReturnType。单独的 Callable 等价于 Callable[..., Any] ，并且进而等价于 collections.abc.Callable 。

typing.ClassVar

特殊的类型构造器，用以标记类变量。

在 PEP 526 [https://www.python.org/dev/peps/pep-0526] 中被引入，ClassVar 包裹起来的变量注解指示了给定属性预期用于类变量，并且不应在类的实例上被设置。用法：

class Starship:
    stats: ClassVar[Dict[str, int]] = {} # class variable
    damage: int = 10                     # instance variable

ClassVar 仅接受类型，并且不能被再次下标。

ClassVar 本身并不是一个类，并且不应与 isinstance() or issubclass() 一起使用。 ClassVar 并不改变 Python 运行时行为，但它可以被用于第三方类型检查器。例如，某个类型检查器可能会标记以下代码为错误的：

enterprise_d = Starship(3000)
enterprise_d.stats = {} # Error, setting class variable on instance
Starship.stats = {}     # This is OK

3.5.3 新版功能.

typing.AnyStr

AnyStr 类型变量的定义为 AnyStr = TypeVar('AnyStr', str, bytes)。

这里指的是，它可以接受任意同类字符串，但不支持混用不同类别的字符串。例如：

def concat(a: AnyStr, b: AnyStr) -> AnyStr:
    return a + b

concat(u"foo", u"bar")  # Ok, output has type 'unicode'
concat(b"foo", b"bar")  # Ok, output has type 'bytes'
concat(u"foo", b"bar")  # Error, cannot mix unicode and bytes

typing.TYPE_CHECKING

被第三方静态类型检查器假定为 True 的特殊常量。 在运行时为 False。 用法如下:

if TYPE_CHECKING:
    import expensive_mod

def fun(arg: 'expensive_mod.SomeType') -> None:
    local_var: expensive_mod.AnotherType = other_fun()

Note that the first type annotation must be enclosed in quotes, making it a “forward reference”, to hide the expensive_mod reference from the interpreter runtime. Type annotations for local variables are not evaluated, so the second annotation does not need to be enclosed in quotes.

3.5.2 新版功能.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

26.2. pydoc — 文档生成器和在线帮助系统

源代码: Lib/pydoc.py [https://github.com/python/cpython/tree/3.6/Lib/pydoc.py]

The pydoc module automatically generates documentation from Python modules. The documentation can be presented as pages of text on the console, served to a Web browser, or saved to HTML files.

对于模块、类、函数和方法，显示的文档内容取自文档字符串（即 __doc__ 属性），并会递归地从其带文档的成员中获取。 如果没有文档字符串，pydoc 会尝试从类、函数或方法定义上方，或是模块顶部的注释行段落获取 (参见 inspect.getcomments()).

内置函数 help() 会发起调用交互式解释器的在线帮助系统，该系统使用 pydoc 在终端上生成文本形式的文档内容。 同样的文本文档也可以在 Python 解释器以外通过在操作系统的命令提示符下以脚本方式运行 pydoc 来查看。 例如，运行

pydoc sys

在终端提示符下将通过 sys 模块显示文档内容，其样式类似于 Unix man 命令所显示的指南页面。 pydoc 的参数可以为函数、模块、包，或带点号的对模块中的类、方法或函数以及包中的模块的引用。 如果传给 pydoc 的参数像是一个路径（即包含所在操作系统的路径分隔符，例如 Unix 的正斜杠），并且其指向一个现有的 Python 源文件，则会为该文件生成文档内容。

注解

为了找到对象及其文档内容，pydoc 会导入文档所在的模块。 因此，任何模块层级的代码都将被执行。 请使用 if __name__ == '__main__': 语句来确保一个文件的特定代码仅在作为脚本被发起调用时执行而不是在被导入时执行。

当打印输出到控制台时，pydoc 会尝试对输出进行分页以方便阅读。 如果设置了 PAGER 环境变量，pydoc 将使用该变量值作为分页程序。

在参数前指定 -w 旗标将把 HTML 文档写入到当前目录下的一个文件中，而不是在控制台中显示文本。

在参数前指定 -k 旗标将在全部可用模块的提要行中搜索参数所给定的关键字，具体方式同样类似于 Unix man 命令。 模块的提要行就是其文档字符串的第一行。

你还可以使用 pydoc 在本机上启动一个 HTTP 服务，这将向来访的 Web 浏览器提供文档服务。 pydoc -p 1234 将在 1234 端口上启动 HTTP 服务，允许你在你喜欢的 Web 服务器中通过 http://localhost:1234/ 浏览文档内容。 指定 0 作为端口号将会任意选择一个未使用的端口。

pydoc -b 将启动服务并额外打开一个 Web 浏览器访问模块索引页。 所发布的每个页面顶端都带有导航栏，你可以点击 Get 获取特定条目的帮助，点击 Search 在所有模块的提要行中搜索特定关键词，或是点击 Module index, Topics 和 Keywords 前往相应的页面。

当 pydoc 生成文档内容时，它会使用当前环境和路径来定位模块。 因此，发起调用 pydoc spam 得到的文档版本会与你启动 Python 解释器并输入 import spam 时得到的模块版本完全相同。

核心模块的模块文档位置对应于 https://docs.python.org/X.Y/library/ 其中 X 和 Y 是 Python 解释器的主要版本号和小版本号。 这可通过设置 PYTHONDOCS 环境变量来重载为指向不同的 URL 或包含 Library Reference Manual 页面的本地目录。

在 3.2 版更改: 添加 -b 选项。

在 3.3 版更改: 命令行选项 -g 已经移除。

在 3.4 版更改: pydoc 现在会使用 inspect.signature() 而非 inspect.getfullargspec() 来从可调用对象中提取签名信息。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

26.3. doctest — 测试交互性的Python示例

** 源代码 ** Lib/doctest.py [https://github.com/python/cpython/tree/3.6/Lib/doctest.py]

doctest 模块寻找像Python交互式代码的文本，然后执行这些代码来确保它们的确就像展示的那样正确运行，有许多方法来使用doctest：

通过验证所有交互式示例仍然按照记录的方式工作，以此来检查模块的文档字符串是否是最新的。

To perform regression testing by verifying that interactive examples from a test file or a test object work as expected.

To write tutorial documentation for a package, liberally illustrated with input-output examples. Depending on whether the examples or the expository text are emphasized, this has the flavor of “literate testing” or “executable documentation”.

下面是一个小却完整的示例模块:

"""
This is the "example" module.

The example module supplies one function, factorial().  For example,

>>> factorial(5)
120
"""

def factorial(n):
    """Return the factorial of n, an exact integer >= 0.

    >>> [factorial(n) for n in range(6)]
    [1, 1, 2, 6, 24, 120]
    >>> factorial(30)
    265252859812191058636308480000000
    >>> factorial(-1)
    Traceback (most recent call last):
        ...
    ValueError: n must be >= 0

    Factorials of floats are OK, but the float must be an exact integer:
    >>> factorial(30.1)
    Traceback (most recent call last):
        ...
    ValueError: n must be exact integer
    >>> factorial(30.0)
    265252859812191058636308480000000

    It must also not be ridiculously large:
    >>> factorial(1e100)
    Traceback (most recent call last):
        ...
    OverflowError: n too large
    """

    import math
    if not n >= 0:
        raise ValueError("n must be >= 0")
    if math.floor(n) != n:
        raise ValueError("n must be exact integer")
    if n+1 == n:  # catch a value like 1e300
        raise OverflowError("n too large")
    result = 1
    factor = 2
    while factor <= n:
        result *= factor
        factor += 1
    return result

if __name__ == "__main__":
    import doctest
    doctest.testmod()

如果你直接在命令行里运行 example.py ， doctest 将发挥它的作用。

$ python example.py
$

There’s no output! That’s normal, and it means all the examples worked. Pass -v to the script, and doctest prints a detailed log of what it’s trying, and prints a summary at the end:

$ python example.py -v
Trying:
    factorial(5)
Expecting:
    120
ok
Trying:
    [factorial(n) for n in range(6)]
Expecting:
    [1, 1, 2, 6, 24, 120]
ok

And so on, eventually ending with:

Trying:
    factorial(1e100)
Expecting:
    Traceback (most recent call last):
        ...
    OverflowError: n too large
ok
2 items passed all tests:
   1 tests in __main__
   8 tests in __main__.factorial
9 tests in 2 items.
9 passed and 0 failed.
Test passed.
$

That’s all you need to know to start making productive use of doctest! Jump in. The following sections provide full details. Note that there are many examples of doctests in the standard Python test suite and libraries. Especially useful examples can be found in the standard test file Lib/test/test_doctest.py.

26.3.1. 简单用法：检查Docstrings中的示例

开始使用doctest的最简单方法（但不一定是你将继续这样做的方式）是结束每个模块 M 使用:

if __name__ == "__main__":
    import doctest
    doctest.testmod()

doctest then examines docstrings in module M.

Running the module as a script causes the examples in the docstrings to get executed and verified:

python M.py

This won’t display anything unless an example fails, in which case the failing example(s) and the cause(s) of the failure(s) are printed to stdout, and the final line of output is ***Test Failed*** N failures., where N is the number of examples that failed.

Run it with the -v switch instead:

python M.py -v

and a detailed report of all examples tried is printed to standard output, along with assorted summaries at the end.

You can force verbose mode by passing verbose=True to testmod(), or prohibit it by passing verbose=False. In either of those cases, sys.argv is not examined by testmod() (so passing -v or not has no effect).

There is also a command line shortcut for running testmod(). You can instruct the Python interpreter to run the doctest module directly from the standard library and pass the module name(s) on the command line:

python -m doctest -v example.py

This will import example.py as a standalone module and run testmod() on it. Note that this may not work correctly if the file is part of a package and imports other submodules from that package.

For more information on testmod(), see section Basic API.

26.3.2. Simple Usage: Checking Examples in a Text File

Another simple application of doctest is testing interactive examples in a text file. This can be done with the testfile() function:

import doctest
doctest.testfile("example.txt")

That short script executes and verifies any interactive Python examples contained in the file example.txt. The file content is treated as if it were a single giant docstring; the file doesn’t need to contain a Python program! For example, perhaps example.txt contains this:

The ``example`` module
======================

Using ``factorial``
-------------------

This is an example text file in reStructuredText format.  First import
``factorial`` from the ``example`` module:

    >>> from example import factorial

Now use it:

    >>> factorial(6)
    120

Running doctest.testfile("example.txt") then finds the error in this documentation:

File "./example.txt", line 14, in example.txt
Failed example:
    factorial(6)
Expected:
    120
Got:
    720

As with testmod(), testfile() won’t display anything unless an example fails. If an example does fail, then the failing example(s) and the cause(s) of the failure(s) are printed to stdout, using the same format as testmod().

By default, testfile() looks for files in the calling module’s directory. See section Basic API for a description of the optional arguments that can be used to tell it to look for files in other locations.

Like testmod(), testfile()’s verbosity can be set with the -v command-line switch or with the optional keyword argument verbose.

There is also a command line shortcut for running testfile(). You can instruct the Python interpreter to run the doctest module directly from the standard library and pass the file name(s) on the command line:

python -m doctest -v example.txt

Because the file name does not end with .py, doctest infers that it must be run with testfile(), not testmod().

For more information on testfile(), see section Basic API.

26.3.3. How It Works

This section examines in detail how doctest works: which docstrings it looks at, how it finds interactive examples, what execution context it uses, how it handles exceptions, and how option flags can be used to control its behavior. This is the information that you need to know to write doctest examples; for information about actually running doctest on these examples, see the following sections.

26.3.3.1. Which Docstrings Are Examined?

The module docstring, and all function, class and method docstrings are searched. Objects imported into the module are not searched.

In addition, if M.__test__ exists and “is true”, it must be a dict, and each entry maps a (string) name to a function object, class object, or string. Function and class object docstrings found from M.__test__ are searched, and strings are treated as if they were docstrings. In output, a key K in M.__test__ appears with name

<name of M>.__test__.K

Any classes found are recursively searched similarly, to test docstrings in their contained methods and nested classes.

CPython implementation detail: Prior to version 3.4, extension modules written in C were not fully searched by doctest.

26.3.3.2. How are Docstring Examples Recognized?

In most cases a copy-and-paste of an interactive console session works fine, but doctest isn’t trying to do an exact emulation of any specific Python shell.

>>> # comments are ignored
>>> x = 12
>>> x
12
>>> if x == 13:
...     print("yes")
... else:
...     print("no")
...     print("NO")
...     print("NO!!!")
...
no
NO
NO!!!
>>>

Any expected output must immediately follow the final '>>> ' or '... ' line containing the code, and the expected output (if any) extends to the next '>>> ' or all-whitespace line.

The fine print:

Expected output cannot contain an all-whitespace line, since such a line is taken to signal the end of expected output. If expected output does contain a blank line, put <BLANKLINE> in your doctest example each place a blank line is expected.

All hard tab characters are expanded to spaces, using 8-column tab stops. Tabs in output generated by the tested code are not modified. Because any hard tabs in the sample output are expanded, this means that if the code output includes hard tabs, the only way the doctest can pass is if the NORMALIZE_WHITESPACE option or directive is in effect. Alternatively, the test can be rewritten to capture the output and compare it to an expected value as part of the test. This handling of tabs in the source was arrived at through trial and error, and has proven to be the least error prone way of handling them. It is possible to use a different algorithm for handling tabs by writing a custom DocTestParser class.

Output to stdout is captured, but not output to stderr (exception tracebacks are captured via a different means).

If you continue a line via backslashing in an interactive session, or for any other reason use a backslash, you should use a raw docstring, which will preserve your backslashes exactly as you type them:

>>> def f(x):
...     r'''Backslashes in a raw docstring: m\n'''
>>> print(f.__doc__)
Backslashes in a raw docstring: m\n

Otherwise, the backslash will be interpreted as part of the string. For example, the \n above would be interpreted as a newline character. Alternatively, you can double each backslash in the doctest version (and not use a raw string):

>>> def f(x):
...     '''Backslashes in a raw docstring: m\\n'''
>>> print(f.__doc__)
Backslashes in a raw docstring: m\n

The starting column doesn’t matter:

>>> assert "Easy!"
      >>> import math
          >>> math.floor(1.9)
          1

and as many leading whitespace characters are stripped from the expected output as appeared in the initial '>>> ' line that started the example.

26.3.3.3. What’s the Execution Context?

By default, each time doctest finds a docstring to test, it uses a shallow copy of M’s globals, so that running tests doesn’t change the module’s real globals, and so that one test in M can’t leave behind crumbs that accidentally allow another test to work. This means examples can freely use any names defined at top-level in M, and names defined earlier in the docstring being run. Examples cannot see names defined in other docstrings.

You can force use of your own dict as the execution context by passing globs=your_dict to testmod() or testfile() instead.

26.3.3.4. What About Exceptions?

No problem, provided that the traceback is the only output produced by the example: just paste in the traceback. 1 Since tracebacks contain details that are likely to change rapidly (for example, exact file paths and line numbers), this is one case where doctest works hard to be flexible in what it accepts.

Simple example:

>>> [1, 2, 3].remove(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list

That doctest succeeds if ValueError is raised, with the list.remove(x): x not in list detail as shown.

The expected output for an exception must start with a traceback header, which may be either of the following two lines, indented the same as the first line of the example:

Traceback (most recent call last):
Traceback (innermost last):

The traceback header is followed by an optional traceback stack, whose contents are ignored by doctest. The traceback stack is typically omitted, or copied verbatim from an interactive session.

The traceback stack is followed by the most interesting part: the line(s) containing the exception type and detail. This is usually the last line of a traceback, but can extend across multiple lines if the exception has a multi-line detail:

>>> raise ValueError('multi\n    line\ndetail')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: multi
    line
detail

The last three lines (starting with ValueError) are compared against the exception’s type and detail, and the rest are ignored.

Best practice is to omit the traceback stack, unless it adds significant documentation value to the example. So the last example is probably better as:

>>> raise ValueError('multi\n    line\ndetail')
Traceback (most recent call last):
    ...
ValueError: multi
    line
detail

Note that tracebacks are treated very specially. In particular, in the rewritten example, the use of ... is independent of doctest’s ELLIPSIS option. The ellipsis in that example could be left out, or could just as well be three (or three hundred) commas or digits, or an indented transcript of a Monty Python skit.

Some details you should read once, but won’t need to remember:

Doctest can’t guess whether your expected output came from an exception traceback or from ordinary printing. So, e.g., an example that expects ValueError: 42 is prime will pass whether ValueError is actually raised or if the example merely prints that traceback text. In practice, ordinary output rarely begins with a traceback header line, so this doesn’t create real problems.

Each line of the traceback stack (if present) must be indented further than the first line of the example, or start with a non-alphanumeric character. The first line following the traceback header indented the same and starting with an alphanumeric is taken to be the start of the exception detail. Of course this does the right thing for genuine tracebacks.

When the IGNORE_EXCEPTION_DETAIL doctest option is specified, everything following the leftmost colon and any module information in the exception name is ignored.

The interactive shell omits the traceback header line for some SyntaxErrors. But doctest uses the traceback header line to distinguish exceptions from non-exceptions. So in the rare case where you need to test a SyntaxError that omits the traceback header, you will need to manually add the traceback header line to your test example.

For some SyntaxErrors, Python displays the character position of the syntax error, using a ^ marker:

>>> 1 1
  File "<stdin>", line 1
    1 1
      ^
SyntaxError: invalid syntax

Since the lines showing the position of the error come before the exception type and detail, they are not checked by doctest. For example, the following test would pass, even though it puts the ^ marker in the wrong location:

>>> 1 1
  File "<stdin>", line 1
    1 1
    ^
SyntaxError: invalid syntax

26.3.3.5. Option Flags

A number of option flags control various aspects of doctest’s behavior. Symbolic names for the flags are supplied as module constants, which can be bitwise ORed together and passed to various functions. The names can also be used in doctest directives, and may be passed to the doctest command line interface via the -o option.

3.4 新版功能: The -o command line option.

The first group of options define test semantics, controlling aspects of how doctest decides whether actual output matches an example’s expected output:

doctest.DONT_ACCEPT_TRUE_FOR_1

By default, if an expected output block contains just 1, an actual output block containing just 1 or just True is considered to be a match, and similarly for 0 versus False. When DONT_ACCEPT_TRUE_FOR_1 is specified, neither substitution is allowed. The default behavior caters to that Python changed the return type of many functions from integer to boolean; doctests expecting “little integer” output still work in these cases. This option will probably go away, but not for several years.

doctest.DONT_ACCEPT_BLANKLINE

By default, if an expected output block contains a line containing only the string <BLANKLINE>, then that line will match a blank line in the actual output. Because a genuinely blank line delimits the expected output, this is the only way to communicate that a blank line is expected. When DONT_ACCEPT_BLANKLINE is specified, this substitution is not allowed.

doctest.NORMALIZE_WHITESPACE

When specified, all sequences of whitespace (blanks and newlines) are treated as equal. Any sequence of whitespace within the expected output will match any sequence of whitespace within the actual output. By default, whitespace must match exactly. NORMALIZE_WHITESPACE is especially useful when a line of expected output is very long, and you want to wrap it across multiple lines in your source.

doctest.ELLIPSIS

When specified, an ellipsis marker (...) in the expected output can match any substring in the actual output. This includes substrings that span line boundaries, and empty substrings, so it’s best to keep usage of this simple. Complicated uses can lead to the same kinds of “oops, it matched too much!” surprises that .* is prone to in regular expressions.

doctest.IGNORE_EXCEPTION_DETAIL

When specified, an example that expects an exception passes if an exception of the expected type is raised, even if the exception detail does not match. For example, an example expecting ValueError: 42 will pass if the actual exception raised is ValueError: 3*14, but will fail, e.g., if TypeError is raised.

It will also ignore the module name used in Python 3 doctest reports. Hence both of these variations will work with the flag specified, regardless of whether the test is run under Python 2.7 or Python 3.2 (or later versions):

>>> raise CustomError('message')
Traceback (most recent call last):
CustomError: message

>>> raise CustomError('message')
Traceback (most recent call last):
my_module.CustomError: message

Note that ELLIPSIS can also be used to ignore the details of the exception message, but such a test may still fail based on whether or not the module details are printed as part of the exception name. Using IGNORE_EXCEPTION_DETAIL and the details from Python 2.3 is also the only clear way to write a doctest that doesn’t care about the exception detail yet continues to pass under Python 2.3 or earlier (those releases do not support doctest directives and ignore them as irrelevant comments). For example:

>>> (1, 2)[3] = 'moo'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object doesn't support item assignment

passes under Python 2.3 and later Python versions with the flag specified, even though the detail changed in Python 2.4 to say “does not” instead of “doesn’t”.

在 3.2 版更改: IGNORE_EXCEPTION_DETAIL now also ignores any information relating to the module containing the exception under test.

doctest.SKIP

When specified, do not run the example at all. This can be useful in contexts where doctest examples serve as both documentation and test cases, and an example should be included for documentation purposes, but should not be checked. E.g., the example’s output might be random; or the example might depend on resources which would be unavailable to the test driver.

The SKIP flag can also be used for temporarily “commenting out” examples.

doctest.COMPARISON_FLAGS

A bitmask or’ing together all the comparison flags above.

The second group of options controls how test failures are reported:

doctest.REPORT_UDIFF

When specified, failures that involve multi-line expected and actual outputs are displayed using a unified diff.

doctest.REPORT_CDIFF

When specified, failures that involve multi-line expected and actual outputs will be displayed using a context diff.

doctest.REPORT_NDIFF

When specified, differences are computed by difflib.Differ, using the same algorithm as the popular ndiff.py utility. This is the only method that marks differences within lines as well as across lines. For example, if a line of expected output contains digit 1 where actual output contains letter l, a line is inserted with a caret marking the mismatching column positions.

doctest.REPORT_ONLY_FIRST_FAILURE

When specified, display the first failing example in each doctest, but suppress output for all remaining examples. This will prevent doctest from reporting correct examples that break because of earlier failures; but it might also hide incorrect examples that fail independently of the first failure. When REPORT_ONLY_FIRST_FAILURE is specified, the remaining examples are still run, and still count towards the total number of failures reported; only the output is suppressed.

doctest.FAIL_FAST

When specified, exit after the first failing example and don’t attempt to run the remaining examples. Thus, the number of failures reported will be at most 1. This flag may be useful during debugging, since examples after the first failure won’t even produce debugging output.

The doctest command line accepts the option -f as a shorthand for -o FAIL_FAST.

3.4 新版功能.

doctest.REPORTING_FLAGS

A bitmask or’ing together all the reporting flags above.

There is also a way to register new option flag names, though this isn’t useful unless you intend to extend doctest internals via subclassing:

doctest.register_optionflag(name)

Create a new option flag with a given name, and return the new flag’s integer value. register_optionflag() can be used when subclassing OutputChecker or DocTestRunner to create new options that are supported by your subclasses. register_optionflag() should always be called using the following idiom:

MY_FLAG = register_optionflag('MY_FLAG')

26.3.3.6. Directives

Doctest directives may be used to modify the option flags for an individual example. Doctest directives are special Python comments following an example’s source code:

directive             ::=  "#" "doctest:" directive_options
directive_options     ::=  directive_option ("," directive_option)\*
directive_option      ::=  on_or_off directive_option_name
on_or_off             ::=  "+" \| "-"
directive_option_name ::=  "DONT_ACCEPT_BLANKLINE" \| "NORMALIZE_WHITESPACE" \| ...

Whitespace is not allowed between the + or - and the directive option name. The directive option name can be any of the option flag names explained above.

An example’s doctest directives modify doctest’s behavior for that single example. Use + to enable the named behavior, or - to disable it.

For example, this test passes:

>>> print(list(range(20)))
[0,   1,  2,  3,  4,  5,  6,  7,  8,  9,
10,  11, 12, 13, 14, 15, 16, 17, 18, 19]

Without the directive it would fail, both because the actual output doesn’t have two blanks before the single-digit list elements, and because the actual output is on a single line. This test also passes, and also requires a directive to do so:

>>> print(list(range(20)))
[0, 1, ..., 18, 19]

Multiple directives can be used on a single physical line, separated by commas:

>>> print(list(range(20)))
[0,    1, ...,   18,    19]

If multiple directive comments are used for a single example, then they are combined:

>>> print(list(range(20)))
...
[0,    1, ...,   18,    19]

As the previous example shows, you can add ... lines to your example containing only directives. This can be useful when an example is too long for a directive to comfortably fit on the same line:

>>> print(list(range(5)) + list(range(10, 20)) + list(range(30, 40)))
...
[0, ..., 4, 10, ..., 19, 30, ..., 39]

Note that since all options are disabled by default, and directives apply only to the example they appear in, enabling options (via + in a directive) is usually the only meaningful choice. However, option flags can also be passed to functions that run doctests, establishing different defaults. In such cases, disabling an option via - in a directive can be useful.

26.3.3.7. 警告

doctest is serious about requiring exact matches in expected output. If even a single character doesn’t match, the test fails. This will probably surprise you a few times, as you learn exactly what Python does and doesn’t guarantee about output. For example, when printing a dict, Python doesn’t guarantee that the key-value pairs will be printed in any particular order, so a test like

>>> foo()
{"Hermione": "hippogryph", "Harry": "broomstick"}

is vulnerable! One workaround is to do

>>> foo() == {"Hermione": "hippogryph", "Harry": "broomstick"}
True

instead. Another is to do

>>> d = sorted(foo().items())
>>> d
[('Harry', 'broomstick'), ('Hermione', 'hippogryph')]

There are others, but you get the idea.

Another bad idea is to print things that embed an object address, like

>>> id(1.0) # certain to fail some of the time
7948648
>>> class C: pass
>>> C()   # the default repr() for instances embeds an address
<__main__.C instance at 0x00AC18F0>

The ELLIPSIS directive gives a nice approach for the last example:

>>> C()
<__main__.C instance at 0x...>

Floating-point numbers are also subject to small output variations across platforms, because Python defers to the platform C library for float formatting, and C libraries vary widely in quality here.

>>> 1./7  # risky
0.14285714285714285
>>> print(1./7) # safer
0.142857142857
>>> print(round(1./7, 6)) # much safer
0.142857

Numbers of the form I/2.**J are safe across all platforms, and I often contrive doctest examples to produce numbers of that form:

>>> 3./4  # utterly safe
0.75

Simple fractions are also easier for people to understand, and that makes for better documentation.

26.3.4. Basic API

The functions testmod() and testfile() provide a simple interface to doctest that should be sufficient for most basic uses. For a less formal introduction to these two functions, see sections 简单用法：检查Docstrings中的示例 and Simple Usage: Checking Examples in a Text File.

doctest.testfile(filename, module_relative=True, name=None, package=None, globs=None, verbose=None, report=True, optionflags=0, extraglobs=None, raise_on_error=False, parser=DocTestParser(), encoding=None)

All arguments except filename are optional, and should be specified in keyword form.

Test examples in the file named filename. Return (failure_count, test_count).

Optional argument module_relative specifies how the filename should be interpreted:

If module_relative is True (the default), then filename specifies an OS-independent module-relative path. By default, this path is relative to the calling module’s directory; but if the package argument is specified, then it is relative to that package. To ensure OS-independence, filename should use / characters to separate path segments, and may not be an absolute path (i.e., it may not begin with /).

If module_relative is False, then filename specifies an OS-specific path. The path may be absolute or relative; relative paths are resolved with respect to the current working directory.

Optional argument name gives the name of the test; by default, or if None, os.path.basename(filename) is used.

Optional argument package is a Python package or the name of a Python package whose directory should be used as the base directory for a module-relative filename. If no package is specified, then the calling module’s directory is used as the base directory for module-relative filenames. It is an error to specify package if module_relative is False.

Optional argument globs gives a dict to be used as the globals when executing examples. A new shallow copy of this dict is created for the doctest, so its examples start with a clean slate. By default, or if None, a new empty dict is used.

Optional argument extraglobs gives a dict merged into the globals used to execute examples. This works like dict.update(): if globs and extraglobs have a common key, the associated value in extraglobs appears in the combined dict. By default, or if None, no extra globals are used. This is an advanced feature that allows parameterization of doctests. For example, a doctest can be written for a base class, using a generic name for the class, then reused to test any number of subclasses by passing an extraglobs dict mapping the generic name to the subclass to be tested.

Optional argument verbose prints lots of stuff if true, and prints only failures if false; by default, or if None, it’s true if and only if '-v' is in sys.argv.

Optional argument report prints a summary at the end when true, else prints nothing at the end. In verbose mode, the summary is detailed, else the summary is very brief (in fact, empty if all tests passed).

Optional argument optionflags (default value 0) takes the bitwise OR of option flags. See section Option Flags.

Optional argument raise_on_error defaults to false. If true, an exception is raised upon the first failure or unexpected exception in an example. This allows failures to be post-mortem debugged. Default behavior is to continue running examples.

Optional argument parser specifies a DocTestParser (or subclass) that should be used to extract tests from the files. It defaults to a normal parser (i.e., DocTestParser()).

Optional argument encoding specifies an encoding that should be used to convert the file to unicode.

doctest.testmod(m=None, name=None, globs=None, verbose=None, report=True, optionflags=0, extraglobs=None, raise_on_error=False, exclude_empty=False)

All arguments are optional, and all except for m should be specified in keyword form.

Test examples in docstrings in functions and classes reachable from module m (or module __main__ if m is not supplied or is None), starting with m.__doc__.

Also test examples reachable from dict m.__test__, if it exists and is not None. m.__test__ maps names (strings) to functions, classes and strings; function and class docstrings are searched for examples; strings are searched directly, as if they were docstrings.

Only docstrings attached to objects belonging to module m are searched.

Return (failure_count, test_count).

Optional argument name gives the name of the module; by default, or if None, m.__name__ is used.

Optional argument exclude_empty defaults to false. If true, objects for which no doctests are found are excluded from consideration. The default is a backward compatibility hack, so that code still using doctest.master.summarize() in conjunction with testmod() continues to get output for objects with no tests. The exclude_empty argument to the newer DocTestFinder constructor defaults to true.

Optional arguments extraglobs, verbose, report, optionflags, raise_on_error, and globs are the same as for function testfile() above, except that globs defaults to m.__dict__.

doctest.run_docstring_examples(f, globs, verbose=False, name="NoName", compileflags=None, optionflags=0)

Test examples associated with object f; for example, f may be a string, a module, a function, or a class object.

A shallow copy of dictionary argument globs is used for the execution context.

Optional argument name is used in failure messages, and defaults to "NoName".

If optional argument verbose is true, output is generated even if there are no failures. By default, output is generated only in case of an example failure.

Optional argument compileflags gives the set of flags that should be used by the Python compiler when running the examples. By default, or if None, flags are deduced corresponding to the set of future features found in globs.

Optional argument optionflags works as for function testfile() above.

26.3.5. Unittest API

As your collection of doctest’ed modules grows, you’ll want a way to run all their doctests systematically. doctest provides two functions that can be used to create unittest test suites from modules and text files containing doctests. To integrate with unittest test discovery, include a load_tests() function in your test module:

import unittest
import doctest
import my_module_with_doctests

def load_tests(loader, tests, ignore):
    tests.addTests(doctest.DocTestSuite(my_module_with_doctests))
    return tests

There are two main functions for creating unittest.TestSuite instances from text files and modules with doctests:

doctest.DocFileSuite(*paths, module_relative=True, package=None, setUp=None, tearDown=None, globs=None, optionflags=0, parser=DocTestParser(), encoding=None)

Convert doctest tests from one or more text files to a unittest.TestSuite.

The returned unittest.TestSuite is to be run by the unittest framework and runs the interactive examples in each file. If an example in any file fails, then the synthesized unit test fails, and a failureException exception is raised showing the name of the file containing the test and a (sometimes approximate) line number.

Pass one or more paths (as strings) to text files to be examined.

Options may be provided as keyword arguments:

Optional argument module_relative specifies how the filenames in paths should be interpreted:

If module_relative is True (the default), then each filename in paths specifies an OS-independent module-relative path. By default, this path is relative to the calling module’s directory; but if the package argument is specified, then it is relative to that package. To ensure OS-independence, each filename should use / characters to separate path segments, and may not be an absolute path (i.e., it may not begin with /).

If module_relative is False, then each filename in paths specifies an OS-specific path. The path may be absolute or relative; relative paths are resolved with respect to the current working directory.

Optional argument package is a Python package or the name of a Python package whose directory should be used as the base directory for module-relative filenames in paths. If no package is specified, then the calling module’s directory is used as the base directory for module-relative filenames. It is an error to specify package if module_relative is False.

Optional argument setUp specifies a set-up function for the test suite. This is called before running the tests in each file. The setUp function will be passed a DocTest object. The setUp function can access the test globals as the globs attribute of the test passed.

Optional argument tearDown specifies a tear-down function for the test suite. This is called after running the tests in each file. The tearDown function will be passed a DocTest object. The setUp function can access the test globals as the globs attribute of the test passed.

Optional argument globs is a dictionary containing the initial global variables for the tests. A new copy of this dictionary is created for each test. By default, globs is a new empty dictionary.

Optional argument optionflags specifies the default doctest options for the tests, created by or-ing together individual option flags. See section Option Flags. See function set_unittest_reportflags() below for a better way to set reporting options.

Optional argument parser specifies a DocTestParser (or subclass) that should be used to extract tests from the files. It defaults to a normal parser (i.e., DocTestParser()).

Optional argument encoding specifies an encoding that should be used to convert the file to unicode.

The global __file__ is added to the globals provided to doctests loaded from a text file using DocFileSuite().

doctest.DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None, setUp=None, tearDown=None, checker=None)

Convert doctest tests for a module to a unittest.TestSuite.

The returned unittest.TestSuite is to be run by the unittest framework and runs each doctest in the module. If any of the doctests fail, then the synthesized unit test fails, and a failureException exception is raised showing the name of the file containing the test and a (sometimes approximate) line number.

Optional argument module provides the module to be tested. It can be a module object or a (possibly dotted) module name. If not specified, the module calling this function is used.

Optional argument globs is a dictionary containing the initial global variables for the tests. A new copy of this dictionary is created for each test. By default, globs is a new empty dictionary.

Optional argument extraglobs specifies an extra set of global variables, which is merged into globs. By default, no extra globals are used.

Optional argument test_finder is the DocTestFinder object (or a drop-in replacement) that is used to extract doctests from the module.

Optional arguments setUp, tearDown, and optionflags are the same as for function DocFileSuite() above.

This function uses the same search technique as testmod().

在 3.5 版更改: DocTestSuite() returns an empty unittest.TestSuite if module contains no docstrings instead of raising ValueError.

Under the covers, DocTestSuite() creates a unittest.TestSuite out of doctest.DocTestCase instances, and DocTestCase is a subclass of unittest.TestCase. DocTestCase isn’t documented here (it’s an internal detail), but studying its code can answer questions about the exact details of unittest integration.

Similarly, DocFileSuite() creates a unittest.TestSuite out of doctest.DocFileCase instances, and DocFileCase is a subclass of DocTestCase.

So both ways of creating a unittest.TestSuite run instances of DocTestCase. This is important for a subtle reason: when you run doctest functions yourself, you can control the doctest options in use directly, by passing option flags to doctest functions. However, if you’re writing a unittest framework, unittest ultimately controls when and how tests get run. The framework author typically wants to control doctest reporting options (perhaps, e.g., specified by command line options), but there’s no way to pass options through unittest to doctest test runners.

For this reason, doctest also supports a notion of doctest reporting flags specific to unittest support, via this function:

doctest.set_unittest_reportflags(flags)

Set the doctest reporting flags to use.

Argument flags takes the bitwise OR of option flags. See section Option Flags. Only “reporting flags” can be used.

This is a module-global setting, and affects all future doctests run by module unittest: the runTest() method of DocTestCase looks at the option flags specified for the test case when the DocTestCase instance was constructed. If no reporting flags were specified (which is the typical and expected case), doctest’s unittest reporting flags are bitwise ORed into the option flags, and the option flags so augmented are passed to the DocTestRunner instance created to run the doctest. If any reporting flags were specified when the DocTestCase instance was constructed, doctest’s unittest reporting flags are ignored.

The value of the unittest reporting flags in effect before the function was called is returned by the function.

26.3.6. Advanced API

The basic API is a simple wrapper that’s intended to make doctest easy to use. It is fairly flexible, and should meet most users’ needs; however, if you require more fine-grained control over testing, or wish to extend doctest’s capabilities, then you should use the advanced API.

The advanced API revolves around two container classes, which are used to store the interactive examples extracted from doctest cases:

Example: A single Python statement, paired with its expected output.

DocTest: A collection of Examples, typically extracted from a single docstring or text file.

Additional processing classes are defined to find, parse, and run, and check doctest examples:

DocTestFinder: Finds all docstrings in a given module, and uses a DocTestParser to create a DocTest from every docstring that contains interactive examples.

DocTestParser: Creates a DocTest object from a string (such as an object’s docstring).

DocTestRunner: Executes the examples in a DocTest, and uses an OutputChecker to verify their output.

OutputChecker: Compares the actual output from a doctest example with the expected output, and decides whether they match.

The relationships among these processing classes are summarized in the following diagram:

                            list of:
+------+                   +---------+
|module| --DocTestFinder-> | DocTest | --DocTestRunner-> results
+------+    |        ^     +---------+     |       ^    (printed)
            |        |     | Example |     |       |
            v        |     |   ...   |     v       |
           DocTestParser   | Example |   OutputChecker
                           +---------+

26.3.6.1. DocTest 对象

class doctest.DocTest(examples, globs, name, filename, lineno, docstring)

A collection of doctest examples that should be run in a single namespace. The constructor arguments are used to initialize the attributes of the same names.

DocTest defines the following attributes. They are initialized by the constructor, and should not be modified directly.

examples

A list of Example objects encoding the individual interactive Python examples that should be run by this test.

globs

The namespace (aka globals) that the examples should be run in. This is a dictionary mapping names to values. Any changes to the namespace made by the examples (such as binding new variables) will be reflected in globs after the test is run.

name

A string name identifying the DocTest. Typically, this is the name of the object or file that the test was extracted from.

filename

The name of the file that this DocTest was extracted from; or None if the filename is unknown, or if the DocTest was not extracted from a file.

lineno

The line number within filename where this DocTest begins, or None if the line number is unavailable. This line number is zero-based with respect to the beginning of the file.

docstring

The string that the test was extracted from, or None if the string is unavailable, or if the test was not extracted from a string.

26.3.6.2. Example Objects

class doctest.Example(source, want, exc_msg=None, lineno=0, indent=0, options=None)

A single interactive example, consisting of a Python statement and its expected output. The constructor arguments are used to initialize the attributes of the same names.

Example defines the following attributes. They are initialized by the constructor, and should not be modified directly.

source

A string containing the example’s source code. This source code consists of a single Python statement, and always ends with a newline; the constructor adds a newline when necessary.

want

The expected output from running the example’s source code (either from stdout, or a traceback in case of exception). want ends with a newline unless no output is expected, in which case it’s an empty string. The constructor adds a newline when necessary.

exc_msg

The exception message generated by the example, if the example is expected to generate an exception; or None if it is not expected to generate an exception. This exception message is compared against the return value of traceback.format_exception_only(). exc_msg ends with a newline unless it’s None. The constructor adds a newline if needed.

lineno

The line number within the string containing this example where the example begins. This line number is zero-based with respect to the beginning of the containing string.

indent

The example’s indentation in the containing string, i.e., the number of space characters that precede the example’s first prompt.

options

A dictionary mapping from option flags to True or False, which is used to override default options for this example. Any option flags not contained in this dictionary are left at their default value (as specified by the DocTestRunner’s optionflags). By default, no options are set.

26.3.6.3. DocTestFinder 对象

class doctest.DocTestFinder(verbose=False, parser=DocTestParser(), recurse=True, exclude_empty=True)

A processing class used to extract the DocTests that are relevant to a given object, from its docstring and the docstrings of its contained objects. DocTests can be extracted from modules, classes, functions, methods, staticmethods, classmethods, and properties.

The optional argument verbose can be used to display the objects searched by the finder. It defaults to False (no output).

The optional argument parser specifies the DocTestParser object (or a drop-in replacement) that is used to extract doctests from docstrings.

If the optional argument recurse is false, then DocTestFinder.find() will only examine the given object, and not any contained objects.

If the optional argument exclude_empty is false, then DocTestFinder.find() will include tests for objects with empty docstrings.

DocTestFinder defines the following method:

find(obj[, name][, module][, globs][, extraglobs])

Return a list of the DocTests that are defined by obj’s docstring, or by any of its contained objects’ docstrings.

The optional argument name specifies the object’s name; this name will be used to construct names for the returned DocTests. If name is not specified, then obj.__name__ is used.

The optional parameter module is the module that contains the given object. If the module is not specified or is None, then the test finder will attempt to automatically determine the correct module. The object’s module is used:

As a default namespace, if globs is not specified.

To prevent the DocTestFinder from extracting DocTests from objects that are imported from other modules. (Contained objects with modules other than module are ignored.)

To find the name of the file containing the object.

To help find the line number of the object within its file.

If module is False, no attempt to find the module will be made. This is obscure, of use mostly in testing doctest itself: if module is False, or is None but cannot be found automatically, then all objects are considered to belong to the (non-existent) module, so all contained objects will (recursively) be searched for doctests.

The globals for each DocTest is formed by combining globs and extraglobs (bindings in extraglobs override bindings in globs). A new shallow copy of the globals dictionary is created for each DocTest. If globs is not specified, then it defaults to the module’s __dict__, if specified, or {} otherwise. If extraglobs is not specified, then it defaults to {}.

26.3.6.4. DocTestParser 对象

class doctest.DocTestParser

A processing class used to extract interactive examples from a string, and use them to create a DocTest object.

DocTestParser defines the following methods:

get_doctest(string, globs, name, filename, lineno)

Extract all doctest examples from the given string, and collect them into a DocTest object.

globs, name, filename, and lineno are attributes for the new DocTest object. See the documentation for DocTest for more information.

get_examples(string, name='<string>')

Extract all doctest examples from the given string, and return them as a list of Example objects. Line numbers are 0-based. The optional argument name is a name identifying this string, and is only used for error messages.

parse(string, name='<string>')

Divide the given string into examples and intervening text, and return them as a list of alternating Examples and strings. Line numbers for the Examples are 0-based. The optional argument name is a name identifying this string, and is only used for error messages.

26.3.6.5. DocTestRunner 对象

class doctest.DocTestRunner(checker=None, verbose=None, optionflags=0)

A processing class used to execute and verify the interactive examples in a DocTest.

The comparison between expected outputs and actual outputs is done by an OutputChecker. This comparison may be customized with a number of option flags; see section Option Flags for more information. If the option flags are insufficient, then the comparison may also be customized by passing a subclass of OutputChecker to the constructor.

The test runner’s display output can be controlled in two ways. First, an output function can be passed to TestRunner.run(); this function will be called with strings that should be displayed. It defaults to sys.stdout.write. If capturing the output is not sufficient, then the display output can be also customized by subclassing DocTestRunner, and overriding the methods report_start(), report_success(), report_unexpected_exception(), and report_failure().

The optional keyword argument checker specifies the OutputChecker object (or drop-in replacement) that should be used to compare the expected outputs to the actual outputs of doctest examples.

The optional keyword argument verbose controls the DocTestRunner’s verbosity. If verbose is True, then information is printed about each example, as it is run. If verbose is False, then only failures are printed. If verbose is unspecified, or None, then verbose output is used iff the command-line switch -v is used.

The optional keyword argument optionflags can be used to control how the test runner compares expected output to actual output, and how it displays failures. For more information, see section Option Flags.

DocTestParser defines the following methods:

report_start(out, test, example)

Report that the test runner is about to process the given example. This method is provided to allow subclasses of DocTestRunner to customize their output; it should not be called directly.

example is the example about to be processed. test is the test containing example. out is the output function that was passed to DocTestRunner.run().

report_success(out, test, example, got)

Report that the given example ran successfully. This method is provided to allow subclasses of DocTestRunner to customize their output; it should not be called directly.

example is the example about to be processed. got is the actual output from the example. test is the test containing example. out is the output function that was passed to DocTestRunner.run().

report_failure(out, test, example, got)

Report that the given example failed. This method is provided to allow subclasses of DocTestRunner to customize their output; it should not be called directly.

example is the example about to be processed. got is the actual output from the example. test is the test containing example. out is the output function that was passed to DocTestRunner.run().

report_unexpected_exception(out, test, example, exc_info)

Report that the given example raised an unexpected exception. This method is provided to allow subclasses of DocTestRunner to customize their output; it should not be called directly.

example is the example about to be processed. exc_info is a tuple containing information about the unexpected exception (as returned by sys.exc_info()). test is the test containing example. out is the output function that was passed to DocTestRunner.run().

run(test, compileflags=None, out=None, clear_globs=True)

Run the examples in test (a DocTest object), and display the results using the writer function out.

The examples are run in the namespace test.globs. If clear_globs is true (the default), then this namespace will be cleared after the test runs, to help with garbage collection. If you would like to examine the namespace after the test completes, then use clear_globs=False.

compileflags gives the set of flags that should be used by the Python compiler when running the examples. If not specified, then it will default to the set of future-import flags that apply to globs.

The output of each example is checked using the DocTestRunner’s output checker, and the results are formatted by the DocTestRunner.report_*() methods.

summarize(verbose=None)

Print a summary of all the test cases that have been run by this DocTestRunner, and return a named tuple TestResults(failed, attempted).

The optional verbose argument controls how detailed the summary is. If the verbosity is not specified, then the DocTestRunner’s verbosity is used.

26.3.6.6. OutputChecker 对象

class doctest.OutputChecker

A class used to check the whether the actual output from a doctest example matches the expected output. OutputChecker defines two methods: check_output(), which compares a given pair of outputs, and returns true if they match; and output_difference(), which returns a string describing the differences between two outputs.

OutputChecker defines the following methods:

check_output(want, got, optionflags)

Return True iff the actual output from an example (got) matches the expected output (want). These strings are always considered to match if they are identical; but depending on what option flags the test runner is using, several non-exact match types are also possible. See section Option Flags for more information about option flags.

output_difference(example, got, optionflags)

Return a string describing the differences between the expected output for a given example (example) and the actual output (got). optionflags is the set of option flags used to compare want and got.

26.3.7. 调试

Doctest provides several mechanisms for debugging doctest examples:

Several functions convert doctests to executable Python programs, which can be run under the Python debugger, pdb.

The DebugRunner class is a subclass of DocTestRunner that raises an exception for the first failing example, containing information about that example. This information can be used to perform post-mortem debugging on the example.

The unittest cases generated by DocTestSuite() support the debug() method defined by unittest.TestCase.

You can add a call to pdb.set_trace() in a doctest example, and you’ll drop into the Python debugger when that line is executed. Then you can inspect current values of variables, and so on. For example, suppose a.py contains just this module docstring:

"""
>>> def f(x):
...     g(x*2)
>>> def g(x):
...     print(x+3)
...     import pdb; pdb.set_trace()
>>> f(3)
9
"""

Then an interactive Python session may look like this:

>>> import a, doctest
>>> doctest.testmod(a)
--Return--
> <doctest a[1]>(3)g()->None
-> import pdb; pdb.set_trace()
(Pdb) list
  1     def g(x):
  2         print(x+3)
  3  ->     import pdb; pdb.set_trace()
[EOF]
(Pdb) p x
6
(Pdb) step
--Return--
> <doctest a[0]>(2)f()->None
-> g(x*2)
(Pdb) list
  1     def f(x):
  2  ->     g(x*2)
[EOF]
(Pdb) p x
3
(Pdb) step
--Return--
> <doctest a[2]>(1)?()->None
-> f(3)
(Pdb) cont
(0, 3)
>>>

Functions that convert doctests to Python code, and possibly run the synthesized code under the debugger:

doctest.script_from_examples(s)

Convert text with examples to a script.

Argument s is a string containing doctest examples. The string is converted to a Python script, where doctest examples in s are converted to regular code, and everything else is converted to Python comments. The generated script is returned as a string. For example,

import doctest
print(doctest.script_from_examples(r"""
    Set x and y to 1 and 2.
    >>> x, y = 1, 2

    Print their sum:
    >>> print(x+y)
    3
"""))

displays:

# Set x and y to 1 and 2.
x, y = 1, 2
#
# Print their sum:
print(x+y)
# Expected:
## 3

This function is used internally by other functions (see below), but can also be useful when you want to transform an interactive Python session into a Python script.

doctest.testsource(module, name)

Convert the doctest for an object to a script.

Argument module is a module object, or dotted name of a module, containing the object whose doctests are of interest. Argument name is the name (within the module) of the object with the doctests of interest. The result is a string, containing the object’s docstring converted to a Python script, as described for script_from_examples() above. For example, if module a.py contains a top-level function f(), then

import a, doctest
print(doctest.testsource(a, "a.f"))

prints a script version of function f()’s docstring, with doctests converted to code, and the rest placed in comments.

doctest.debug(module, name, pm=False)

Debug the doctests for an object.

The module and name arguments are the same as for function testsource() above. The synthesized Python script for the named object’s docstring is written to a temporary file, and then that file is run under the control of the Python debugger, pdb.

A shallow copy of module.__dict__ is used for both local and global execution context.

Optional argument pm controls whether post-mortem debugging is used. If pm has a true value, the script file is run directly, and the debugger gets involved only if the script terminates via raising an unhandled exception. If it does, then post-mortem debugging is invoked, via pdb.post_mortem(), passing the traceback object from the unhandled exception. If pm is not specified, or is false, the script is run under the debugger from the start, via passing an appropriate exec() call to pdb.run().

doctest.debug_src(src, pm=False, globs=None)

Debug the doctests in a string.

This is like function debug() above, except that a string containing doctest examples is specified directly, via the src argument.

Optional argument pm has the same meaning as in function debug() above.

Optional argument globs gives a dictionary to use as both local and global execution context. If not specified, or None, an empty dictionary is used. If specified, a shallow copy of the dictionary is used.

The DebugRunner class, and the special exceptions it may raise, are of most interest to testing framework authors, and will only be sketched here. See the source code, and especially DebugRunner’s docstring (which is a doctest!) for more details:

class doctest.DebugRunner(checker=None, verbose=None, optionflags=0)

A subclass of DocTestRunner that raises an exception as soon as a failure is encountered. If an unexpected exception occurs, an UnexpectedException exception is raised, containing the test, the example, and the original exception. If the output doesn’t match, then a DocTestFailure exception is raised, containing the test, the example, and the actual output.

For information about the constructor parameters and methods, see the documentation for DocTestRunner in section Advanced API.

There are two exceptions that may be raised by DebugRunner instances:

exception doctest.DocTestFailure(test, example, got)

An exception raised by DocTestRunner to signal that a doctest example’s actual output did not match its expected output. The constructor arguments are used to initialize the attributes of the same names.

DocTestFailure defines the following attributes:

DocTestFailure.test

The DocTest object that was being run when the example failed.

DocTestFailure.example

The Example that failed.

DocTestFailure.got

The example’s actual output.

exception doctest.UnexpectedException(test, example, exc_info)

An exception raised by DocTestRunner to signal that a doctest example raised an unexpected exception. The constructor arguments are used to initialize the attributes of the same names.

UnexpectedException defines the following attributes:

UnexpectedException.test

The DocTest object that was being run when the example failed.

UnexpectedException.example

The Example that failed.

UnexpectedException.exc_info

A tuple containing information about the unexpected exception, as returned by sys.exc_info().

26.3.8. Soapbox

As mentioned in the introduction, doctest has grown to have three primary uses:

Checking examples in docstrings.

Regression testing.

Executable documentation / literate testing.

These uses have different requirements, and it is important to distinguish them. In particular, filling your docstrings with obscure test cases makes for bad documentation.

When writing a docstring, choose docstring examples with care. There’s an art to this that needs to be learned—it may not be natural at first. Examples should add genuine value to the documentation. A good example can often be worth many words. If done with care, the examples will be invaluable for your users, and will pay back the time it takes to collect them many times over as the years go by and things change. I’m still amazed at how often one of my doctest examples stops working after a “harmless” change.

Doctest also makes an excellent tool for regression testing, especially if you don’t skimp on explanatory text. By interleaving prose and examples, it becomes much easier to keep track of what’s actually being tested, and why. When a test fails, good prose can make it much easier to figure out what the problem is, and how it should be fixed. It’s true that you could write extensive comments in code-based testing, but few programmers do. Many have found that using doctest approaches instead leads to much clearer tests. Perhaps this is simply because doctest makes writing prose a little easier than writing code, while writing comments in code is a little harder. I think it goes deeper than just that: the natural attitude when writing a doctest-based test is that you want to explain the fine points of your software, and illustrate them with examples. This in turn naturally leads to test files that start with the simplest features, and logically progress to complications and edge cases. A coherent narrative is the result, instead of a collection of isolated functions that test isolated bits of functionality seemingly at random. It’s a different attitude, and produces different results, blurring the distinction between testing and explaining.

Regression testing is best confined to dedicated objects or files. There are several options for organizing tests:

Write text files containing test cases as interactive examples, and test the files using testfile() or DocFileSuite(). This is recommended, although is easiest to do for new projects, designed from the start to use doctest.

Define functions named _regrtest_topic that consist of single docstrings, containing test cases for the named topics. These functions can be included in the same file as the module, or separated out into a separate test file.

Define a __test__ dictionary mapping from regression test topics to docstrings containing test cases.

When you have placed your tests in a module, the module can itself be the test runner. When a test fails, you can arrange for your test runner to re-run only the failing doctest while you debug the problem. Here is a minimal example of such a test runner:

if __name__ == '__main__':
    import doctest
    flags = doctest.REPORT_NDIFF|doctest.FAIL_FAST
    if len(sys.argv) > 1:
        name = sys.argv[1]
        if name in globals():
            obj = globals()[name]
        else:
            obj = __test__[name]
        doctest.run_docstring_examples(obj, globals(), name=name,
                                       optionflags=flags)
    else:
        fail, total = doctest.testmod(optionflags=flags)
        print("{} failures out of {} tests".format(fail, total))

备注

1

Examples containing both expected output and an exception are not supported. Trying to guess where one ends and the other begins is too error-prone, and that also makes for a confusing test.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

26.4. unittest — 单元测试框架

源代码： Lib/unittest/__init__.py [https://github.com/python/cpython/tree/3.6/Lib/unittest/__init__.py]

（如果你已经对测试的概念比较熟悉了，你可能想直接跳转到这一部分 断言方法。）

unittest 单元测试框架是受到 JUnit 的启发，与其他语言中的主流单元测试框架有着相似的风格。其支持测试自动化，配置共享和关机代码测试。支持将测试样例聚合到测试集中，并将测试与报告框架独立。

为了实现这些，unittest 通过面向对象的方式支持了一些重要的概念。

测试脚手架

test fixture 表示为了开展一项或多项测试所需要进行的准备工作，以及所有相关的清理操作。举个例子，这可能包含创建临时或代理的数据库、目录，再或者启动一个服务器进程。

测试用例

一个测试用例是一个独立的测试单元。它检查输入特定的数据时的响应。 unittest 提供一个基类： TestCase ，用于新建测试用例。

测试套件

test suite 是一系列的测试用例，或测试套件，或两者皆有。它用于归档需要一起执行的测试。

测试运行器（test runner）

test runner 是一个用于执行和输出测试结果的组件。这个运行器可能使用图形接口、文本接口，或返回一个特定的值表示运行测试的结果。

参见

doctest — 文档测试模块

另一个风格完全不同的测试模块。

Simple Smalltalk Testing: With Patterns [https://web.archive.org/web/20150315073817/http://www.xprogramming.com/testfram.htm]

Kent Beck’s original paper on testing frameworks using the pattern shared by unittest.

Nose [https://nose.readthedocs.io/] and pytest [https://docs.pytest.org/]

Third-party unittest frameworks with a lighter-weight syntax for writing tests. For example, assert func(10) == 42.

Python 测试工具分类

一个 Python 测试工具的详细列表，包含测试框架和模拟对象库。

Python 中的测试 邮件列表

一个讨论 Python 中的测试和测试工具的特别兴趣小组。

包含在源代码分发中的 Tools/unittestgui/unittestgui.py 文件是一个用于显示测试覆盖率和执行情况的 GUI 工具。它的目的是给那些单元测试的新手们提供方便。在生产环境中建议测试应由持续集成系统（continuous integration system）驱动，例如：Buildbot [https://buildbot.net/]、 Jenkins [https://jenkins.io/] 或 Hudson [http://hudson-ci.org/]。

26.4.1. 基本实例

unittest 模块提供了一系列创建和运行测试的工具。这一段落演示了这些工具的一小部分，但也足以满足大部分用户的需求。

这是一段简短的代码，来测试三种字符串方法:

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()

继承 unittest.TestCase 就创建了一个测试样例。上述三个独立的测试是三个类的方法，这些方法的命名都以 test 开头。 这个命名约定告诉测试运行者类的哪些方法表示测试。

每个测试的关键是：调用 assertEqual() 来检查预期的输出； 调用 assertTrue() 或 assertFalse() 来验证一个条件；调用 assertRaises() 来验证抛出了一个特定的异常。使用这些方法而不是 assert 语句是为了让测试运行者能聚合所有的测试结果并产生结果报告。

通过 setUp() 和 tearDown() 方法，可以设置测试开始前与完成后需要执行的指令。 在 组织你的测试代码 中，对此有更为详细的描述。

最后的代码块中，演示了运行测试的一个简单的方法。 unittest.main() 提供了一个测试脚本的命令行接口。当在命令行运行该测试脚本，上文的脚本生成如以下格式的输出:

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

在调用测试脚本时添加 -v 参数使 unittest.main() 显示更为详细的信息，生成如以下形式的输出:

test_isupper (__main__.TestStringMethods) ... ok
test_split (__main__.TestStringMethods) ... ok
test_upper (__main__.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

OK

以上例子演示了 unittest 中最常用的、足够满足许多日常测试需求的特性。文档的剩余部分详述该框架的完整特性。

26.4.2. 命令行界面

unittest 模块可以通过命令行运行模块、类和独立测试方法的测试:

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method

你可以传入模块名、类或方法名或他们的任意组合。

同样的，测试模块可以通过文件路径指定:

python -m unittest tests/test_something.py

这样就可以使用 shell 的文件名补全指定测试模块。所指定的文件仍需要可以被作为模块导入。路径通过去除 ‘.py’ 、把分隔符转换为 ‘.’ 转换为模块名。若你需要执行不能被作为模块导入的测试文件，你需要直接执行该测试文件。

在运行测试时，你可以通过添加 -v 参数获取更详细（更多的冗余）的信息。

python -m unittest -v test_module

当运行时不包含参数，开始 探索性测试

python -m unittest

用于获取命令行选项列表：

python -m unittest -h

在 3.2 版更改: 在早期版本中，只支持运行独立的测试方法，而不支持模块和类。

26.4.2.1. 命令行选项

unittest supports these command-line options:

-b, --buffer

在测试运行时，标准输出流与标准错误流会被放入缓冲区。成功的测试的运行时输出会被丢弃；测试不通过时，测试运行中的输出会正常显示，错误会被加入到测试失败信息。

-c, --catch

当测试正在运行时， Control-C 会等待当前测试完成，并在完成后报告已执行的测试的结果。当再次按下 Control-C 时，引发平常的 KeyboardInterrupt 异常。

See Signal Handling for the functions that provide this functionality.

-f, --failfast

当出现第一个错误或者失败时，停止运行测试。

--locals

在回溯中显示局部变量。

3.2 新版功能: 添加命令行选项 -b, -c 和 -f 。

3.5 新版功能: 命令行选项 --locals 。

命令行亦可用于探索性测试，以运行一个项目的所有测试或其子集。

26.4.3. 探索性测试

3.2 新版功能.

Unittest支持简单的测试搜索。若需要使用探索性测试，所有的测试文件必须是 modules 或 packages （包括 namespace packages )并可从项目根目录导入（即它们的文件名必须是有效的 identifiers ）。

探索性测试在 TestLoader.discover() 中实现，但也可以通过命令行使用。它在命令行中的基本用法如下：

cd project_directory
python -m unittest discover

注解

方便起见， python -m unittest 与 python -m unittest discover 等价。如果你需要向探索性测试传入参数，必须显式地使用 discover 子命令。

discover 有以下选项：

-v, --verbose

更详细地输出结果。

-s, --start-directory directory

开始进行搜索的目录(默认值为当前目录 . )。

-p, --pattern pattern

用于匹配测试文件的模式（默认为 test*.py ）。

-t, --top-level-directory directory

指定项目的最上层目录（通常为开始时所在目录）。

-s ，-p 和 -t 选项可以按顺序作为位置参数传入。以下两条命令是等价的：

python -m unittest discover -s project_directory -p "*_test.py"
python -m unittest discover project_directory "*_test.py"

正如可以传入路径那样，传入一个包名作为起始目录也是可行的，如 myproject.subpackage.test 。你提供的包名会被导入，它在文件系统中的位置会被作为起始目录。

警告

探索性测试通过导入测试对测试进行加载。在找到所有你指定的开始目录下的所有测试文件后，它把路径转换为包名并进行导入。如 foo/bar/baz.py 会被导入为 foo.bar.baz 。

如果你有一个全局安装的包，并尝试对这个包的副本进行探索性测试，可能会从错误的地方开始导入。如果出现这种情况，测试会输出警告并退出。

如果你使用包名而不是路径作为开始目录，搜索时会假定它导入的是你想要的目录，所以你不会收到警告。

测试模块和包可以通过 load_tests protocol 自定义测试的加载和搜索。

在 3.4 版更改: 探索性测试支持命名空间包（ namespace packages ）。

26.4.4. 组织你的测试代码

单元测试的构建单位是 test cases ：独立的、包含执行条件与正确性检查的方案。在 unittest 中，测试用例表示为 unittest.TestCase 的实例。通过编写 TestCase 的子类或使用 FunctionTestCase 编写你自己的测试用例。

一个 TestCase 实例的测试代码必须是完全自含的，因此它可以独立运行，或与其它任意组合任意数量的测试用例一起运行。

TestCase 的最简单的子类需要实现一个测试方法（例如一个命名以 test 开头的方法）以执行特定的测试代码：

import unittest

class DefaultWidgetSizeTestCase(unittest.TestCase):
    def test_default_widget_size(self):
        widget = Widget('The widget')
        self.assertEqual(widget.size(), (50, 50))

可以看到，为了进行测试，我们使用了基类 TestCase 提供的其中一个 assert*() 方法。若测试不通过，将会引发一个带有说明信息的异常，并且 unittest 会将这个测试用例标记为测试不通过。任何其它类型的异常将会被当做错误处理。

可能同时存在多个前置操作相同的测试，我们可以把测试的前置操作从测试代码中拆解出来，并实现测试前置方法 setUp() 。在运行测试时，测试框架会自动地为每个单独测试调用前置方法。

import unittest

class WidgetTestCase(unittest.TestCase):
    def setUp(self):
        self.widget = Widget('The widget')

    def test_default_widget_size(self):
        self.assertEqual(self.widget.size(), (50,50),
                         'incorrect default size')

    def test_widget_resize(self):
        self.widget.resize(100,150)
        self.assertEqual(self.widget.size(), (100,150),
                         'wrong size after resize')

注解

多个测试运行的顺序由内置字符串排序方法对测试名进行排序的结果决定。

在测试运行时，若 setUp() 方法引发异常，测试框架会认为测试发生了错误，因此测试方法不会被运行。

相似的，我们提供了一个 tearDown() 方法在测试方法运行后进行清理工作。

import unittest

class WidgetTestCase(unittest.TestCase):
    def setUp(self):
        self.widget = Widget('The widget')

    def tearDown(self):
        self.widget.dispose()

若 setUp() 成功运行，无论测试方法是否成功，都会运行 tearDown() 。

这样的一个测试代码运行的环境被称为 test fixture 。一个新的 TestCase 实例作为一个测试脚手架，用于运行各个独立的测试方法。在运行每个测试时，setUp() 、tearDown() 和 __init__() 会被调用一次。

建议你根据所测试的功能，将测试用 TestCase 实现集合起来。unittest 为此提供了机制：test suite，以 unittest 的类 TestSuite 为代表。大部分情况下，调用 unittest.main() 即可，并且它会为你集合所有模块的测试用例并执行。

然而，如果你需要自定义你的测试套件的话，你可以参考以下方法组织你的测试：

def suite():
    suite = unittest.TestSuite()
    suite.addTest(WidgetTestCase('test_default_widget_size'))
    suite.addTest(WidgetTestCase('test_widget_resize'))
    return suite

if __name__ == '__main__':
    runner = unittest.TextTestRunner()
    runner.run(suite())

You can place the definitions of test cases and test suites in the same modules as the code they are to test (such as widget.py), but there are several advantages to placing the test code in a separate module, such as test_widget.py:

The test module can be run standalone from the command line.

The test code can more easily be separated from shipped code.

There is less temptation to change test code to fit the code it tests without a good reason.

Test code should be modified much less frequently than the code it tests.

Tested code can be refactored more easily.

Tests for modules written in C must be in separate modules anyway, so why not be consistent?

If the testing strategy changes, there is no need to change the source code.

26.4.5. 复用已有的测试代码

一些用户希望直接使用 unittest 运行已有的测试代码，而不需要把已有的每个测试函数转化为一个 TestCase 的子类。

因此， unittest 提供 FunctionTestCase 类。这个 TestCase 的子类可用于打包已有的测试函数，并支持设置前置与后置函数。

假定有一个测试函数：

def testSomething():
    something = makeSomething()
    assert something.name is not None
    # ...

可以创建等价的测试用例如下，其中前置和后置方法是可选的。

testcase = unittest.FunctionTestCase(testSomething,
                                     setUp=makeSomethingDB,
                                     tearDown=deleteSomethingDB)

注解

Even though FunctionTestCase can be used to quickly convert an existing test base over to a unittest-based system, this approach is not recommended. Taking the time to set up proper TestCase subclasses will make future test refactorings infinitely easier.

In some cases, the existing tests may have been written using the doctest module. If so, doctest provides a DocTestSuite class that can automatically build unittest.TestSuite instances from the existing doctest-based tests.

26.4.6. 跳过测试与预计的失败

3.1 新版功能.

Unittest supports skipping individual test methods and even whole classes of tests. In addition, it supports marking a test as an “expected failure,” a test that is broken and will fail, but shouldn’t be counted as a failure on a TestResult.

Skipping a test is simply a matter of using the skip() decorator or one of its conditional variants.

跳过测试的基本用法如下:

class MyTestCase(unittest.TestCase):

    @unittest.skip("demonstrating skipping")
    def test_nothing(self):
        self.fail("shouldn't happen")

    @unittest.skipIf(mylib.__version__ < (1, 3),
                     "not supported in this library version")
    def test_format(self):
        # Tests that work for only a certain version of the library.
        pass

    @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
    def test_windows_support(self):
        # windows specific testing code
        pass

在啰嗦模式下运行以上测试例子时，程序输出如下：

test_format (__main__.MyTestCase) ... skipped 'not supported in this library version'
test_nothing (__main__.MyTestCase) ... skipped 'demonstrating skipping'
test_windows_support (__main__.MyTestCase) ... skipped 'requires Windows'

----------------------------------------------------------------------
Ran 3 tests in 0.005s

OK (skipped=3)

跳过测试类的写法跟跳过测试方法的写法相似:

@unittest.skip("showing class skipping")
class MySkippedTestCase(unittest.TestCase):
    def test_not_run(self):
        pass

TestCase.setUp() 也可以跳过测试。可以用于所需资源不可用的情况下跳过接下来的测试。

使用 expectedFailure() 装饰器表明这个测试预计失败。:

class ExpectedFailureTestCase(unittest.TestCase):
    @unittest.expectedFailure
    def test_fail(self):
        self.assertEqual(1, 0, "broken")

It’s easy to roll your own skipping decorators by making a decorator that calls skip() on the test when it wants it to be skipped. This decorator skips the test unless the passed object has a certain attribute:

def skipUnlessHasattr(obj, attr):
    if hasattr(obj, attr):
        return lambda func: func
    return unittest.skip("{!r} doesn't have {!r}".format(obj, attr))

The following decorators implement test skipping and expected failures:

@unittest.skip(reason)

跳过被此装饰器装饰的测试。 reason 为测试被跳过的原因。

@unittest.skipIf(condition, reason)

当 condition 为真时，跳过被装饰的测试。

@unittest.skipUnless(condition, reason)

跳过被装饰的测试，除非 condition 为真。

@unittest.expectedFailure

Mark the test as an expected failure. If the test fails when run, the test is not counted as a failure.

exception unittest.SkipTest(reason)

引发此异常以跳过一个测试。

通常来说，你可以使用 TestCase.skipTest() 或其中一个跳过测试的装饰器实现跳过测试的功能，而不是直接引发此异常。

被跳过的测试的 setUp() 和 tearDown() 不会被运行。被跳过的类的 setUpClass() 和 tearDownClass() 不会被运行。被跳过的模组的 setUpModule() 和 tearDownModule() 不会被运行。

26.4.7. Distinguishing test iterations using subtests

3.4 新版功能.

When there are very small differences among your tests, for instance some parameters, unittest allows you to distinguish them inside the body of a test method using the subTest() context manager.

例如，以下测试:

class NumbersTest(unittest.TestCase):

    def test_even(self):
        """
        Test that numbers between 0 and 5 are all even.
        """
        for i in range(0, 6):
            with self.subTest(i=i):
                self.assertEqual(i % 2, 0)

可以得到以下输出:

======================================================================
FAIL: test_even (__main__.NumbersTest) (i=1)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "subtests.py", line 32, in test_even
    self.assertEqual(i % 2, 0)
AssertionError: 1 != 0

======================================================================
FAIL: test_even (__main__.NumbersTest) (i=3)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "subtests.py", line 32, in test_even
    self.assertEqual(i % 2, 0)
AssertionError: 1 != 0

======================================================================
FAIL: test_even (__main__.NumbersTest) (i=5)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "subtests.py", line 32, in test_even
    self.assertEqual(i % 2, 0)
AssertionError: 1 != 0

Without using a subtest, execution would stop after the first failure, and the error would be less easy to diagnose because the value of i wouldn’t be displayed:

======================================================================
FAIL: test_even (__main__.NumbersTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "subtests.py", line 32, in test_even
    self.assertEqual(i % 2, 0)
AssertionError: 1 != 0

26.4.8. 类与函数

本节深入介绍了 unittest 的 API。

26.4.8.1. 测试用例

class unittest.TestCase(methodName='runTest')

Instances of the TestCase class represent the logical test units in the unittest universe. This class is intended to be used as a base class, with specific tests being implemented by concrete subclasses. This class implements the interface needed by the test runner to allow it to drive the tests, and methods that the test code can use to check for and report various kinds of failure.

Each instance of TestCase will run a single base method: the method named methodName. In most uses of TestCase, you will neither change the methodName nor reimplement the default runTest() method.

在 3.2 版更改: TestCase can be instantiated successfully without providing a methodName. This makes it easier to experiment with TestCase from the interactive interpreter.

TestCase instances provide three groups of methods: one group used to run the test, another used by the test implementation to check conditions and report failures, and some inquiry methods allowing information about the test itself to be gathered.

Methods in the first group (running the test) are:

setUp()

Method called to prepare the test fixture. This is called immediately before calling the test method; other than AssertionError or SkipTest, any exception raised by this method will be considered an error rather than a test failure. The default implementation does nothing.

tearDown()

Method called immediately after the test method has been called and the result recorded. This is called even if the test method raised an exception, so the implementation in subclasses may need to be particularly careful about checking internal state. Any exception, other than AssertionError or SkipTest, raised by this method will be considered an additional error rather than a test failure (thus increasing the total number of reported errors). This method will only be called if the setUp() succeeds, regardless of the outcome of the test method. The default implementation does nothing.

setUpClass()

A class method called before tests in an individual class are run. setUpClass is called with the class as the only argument and must be decorated as a classmethod():

@classmethod
def setUpClass(cls):
    ...

查看 Class and Module Fixtures 获取更详细的说明。

3.2 新版功能.

tearDownClass()

A class method called after tests in an individual class have run. tearDownClass is called with the class as the only argument and must be decorated as a classmethod():

@classmethod
def tearDownClass(cls):
    ...

查看 Class and Module Fixtures 获取更详细的说明。

3.2 新版功能.

run(result=None)

Run the test, collecting the result into the TestResult object passed as result. If result is omitted or None, a temporary result object is created (by calling the defaultTestResult() method) and used. The result object is returned to run()’s caller.

The same effect may be had by simply calling the TestCase instance.

在 3.3 版更改: Previous versions of run did not return the result. Neither did calling an instance.

skipTest(reason)

Calling this during a test method or setUp() skips the current test. See 跳过测试与预计的失败 for more information.

3.1 新版功能.

subTest(msg=None, **params)

Return a context manager which executes the enclosed code block as a subtest. msg and params are optional, arbitrary values which are displayed whenever a subtest fails, allowing you to identify them clearly.

A test case can contain any number of subtest declarations, and they can be arbitrarily nested.

查看 Distinguishing test iterations using subtests 获取更详细的信息。

3.4 新版功能.

debug()

Run the test without collecting the result. This allows exceptions raised by the test to be propagated to the caller, and can be used to support running tests under a debugger.

The TestCase class provides several assert methods to check for and report failures. The following table lists the most commonly used methods (see the tables below for more assert methods):

Method

Checks that

New in

assertEqual(a, b)

a == b

assertNotEqual(a, b)

a != b

assertTrue(x)

bool(x) is True

assertFalse(x)

bool(x) is False

assertIs(a, b)

a is b

3.1

assertIsNot(a, b)

a is not b

3.1

assertIsNone(x)

x is None

3.1

assertIsNotNone(x)

x is not None

3.1

assertIn(a, b)

a in b

3.1

assertNotIn(a, b)

a not in b

3.1

assertIsInstance(a, b)

isinstance(a, b)

3.2

assertNotIsInstance(a, b)

not isinstance(a, b)

3.2

All the assert methods accept a msg argument that, if specified, is used as the error message on failure (see also longMessage). Note that the msg keyword argument can be passed to assertRaises(), assertRaisesRegex(), assertWarns(), assertWarnsRegex() only when they are used as a context manager.

assertEqual(first, second, msg=None)

Test that first and second are equal. If the values do not compare equal, the test will fail.

In addition, if first and second are the exact same type and one of list, tuple, dict, set, frozenset or str or any type that a subclass registers with addTypeEqualityFunc() the type-specific equality function will be called in order to generate a more useful default error message (see also the list of type-specific methods).

在 3.1 版更改: Added the automatic calling of type-specific equality function.

在 3.2 版更改: assertMultiLineEqual() added as the default type equality function for comparing strings.

assertNotEqual(first, second, msg=None)

Test that first and second are not equal. If the values do compare equal, the test will fail.

assertTrue(expr, msg=None)

assertFalse(expr, msg=None)

Test that expr is true (or false).

Note that this is equivalent to bool(expr) is True and not to expr is True (use assertIs(expr, True) for the latter). This method should also be avoided when more specific methods are available (e.g. assertEqual(a, b) instead of assertTrue(a == b)), because they provide a better error message in case of failure.

assertIs(first, second, msg=None)

assertIsNot(first, second, msg=None)

Test that first and second evaluate (or don’t evaluate) to the same object.

3.1 新版功能.

assertIsNone(expr, msg=None)

assertIsNotNone(expr, msg=None)

Test that expr is (or is not) None.

3.1 新版功能.

assertIn(first, second, msg=None)

assertNotIn(first, second, msg=None)

Test that first is (or is not) in second.

3.1 新版功能.

assertIsInstance(obj, cls, msg=None)

assertNotIsInstance(obj, cls, msg=None)

Test that obj is (or is not) an instance of cls (which can be a class or a tuple of classes, as supported by isinstance()). To check for the exact type, use assertIs(type(obj), cls).

3.2 新版功能.

It is also possible to check the production of exceptions, warnings, and log messages using the following methods:

Method

Checks that

New in

assertRaises(exc, fun, *args, **kwds)

fun(*args, **kwds) raises exc

assertRaisesRegex(exc, r, fun, *args, **kwds)

fun(*args, **kwds) raises exc and the message matches regex r

3.1

assertWarns(warn, fun, *args, **kwds)

fun(*args, **kwds) raises warn

3.2

assertWarnsRegex(warn, r, fun, *args, **kwds)

fun(*args, **kwds) raises warn and the message matches regex r

3.2

assertLogs(logger, level)

The with block logs on logger with minimum level

3.4

assertRaises(exception, callable, *args, **kwds)

assertRaises(exception, *, msg=None)

Test that an exception is raised when callable is called with any positional or keyword arguments that are also passed to assertRaises(). The test passes if exception is raised, is an error if another exception is raised, or fails if no exception is raised. To catch any of a group of exceptions, a tuple containing the exception classes may be passed as exception.

If only the exception and possibly the msg arguments are given, return a context manager so that the code under test can be written inline rather than as a function:

with self.assertRaises(SomeException):
    do_something()

When used as a context manager, assertRaises() accepts the additional keyword argument msg.

The context manager will store the caught exception object in its exception attribute. This can be useful if the intention is to perform additional checks on the exception raised:

with self.assertRaises(SomeException) as cm:
    do_something()

the_exception = cm.exception
self.assertEqual(the_exception.error_code, 3)

在 3.1 版更改: Added the ability to use assertRaises() as a context manager.

在 3.2 版更改: Added the exception attribute.

在 3.3 版更改: Added the msg keyword argument when used as a context manager.

assertRaisesRegex(exception, regex, callable, *args, **kwds)

assertRaisesRegex(exception, regex, *, msg=None)

Like assertRaises() but also tests that regex matches on the string representation of the raised exception. regex may be a regular expression object or a string containing a regular expression suitable for use by re.search(). Examples:

self.assertRaisesRegex(ValueError, "invalid literal for.*XYZ'$",
                       int, 'XYZ')

或者：

with self.assertRaisesRegex(ValueError, 'literal'):
   int('XYZ')

3.1 新版功能: under the name assertRaisesRegexp.

在 3.2 版更改: Renamed to assertRaisesRegex().

在 3.3 版更改: Added the msg keyword argument when used as a context manager.

assertWarns(warning, callable, *args, **kwds)

assertWarns(warning, *, msg=None)

Test that a warning is triggered when callable is called with any positional or keyword arguments that are also passed to assertWarns(). The test passes if warning is triggered and fails if it isn’t. Any exception is an error. To catch any of a group of warnings, a tuple containing the warning classes may be passed as warnings.

If only the warning and possibly the msg arguments are given, return a context manager so that the code under test can be written inline rather than as a function:

with self.assertWarns(SomeWarning):
    do_something()

When used as a context manager, assertWarns() accepts the additional keyword argument msg.

The context manager will store the caught warning object in its warning attribute, and the source line which triggered the warnings in the filename and lineno attributes. This can be useful if the intention is to perform additional checks on the warning caught:

with self.assertWarns(SomeWarning) as cm:
    do_something()

self.assertIn('myfile.py', cm.filename)
self.assertEqual(320, cm.lineno)

This method works regardless of the warning filters in place when it is called.

3.2 新版功能.

在 3.3 版更改: Added the msg keyword argument when used as a context manager.

assertWarnsRegex(warning, regex, callable, *args, **kwds)

assertWarnsRegex(warning, regex, *, msg=None)

Like assertWarns() but also tests that regex matches on the message of the triggered warning. regex may be a regular expression object or a string containing a regular expression suitable for use by re.search(). Example:

self.assertWarnsRegex(DeprecationWarning,
                      r'legacy_function\(\) is deprecated',
                      legacy_function, 'XYZ')

或者：

with self.assertWarnsRegex(RuntimeWarning, 'unsafe frobnicating'):
    frobnicate('/etc/passwd')

3.2 新版功能.

在 3.3 版更改: Added the msg keyword argument when used as a context manager.

assertLogs(logger=None, level=None)

A context manager to test that at least one message is logged on the logger or one of its children, with at least the given level.

If given, logger should be a logging.Logger object or a str giving the name of a logger. The default is the root logger, which will catch all messages.

If given, level should be either a numeric logging level or its string equivalent (for example either "ERROR" or logging.ERROR). The default is logging.INFO.

The test passes if at least one message emitted inside the with block matches the logger and level conditions, otherwise it fails.

The object returned by the context manager is a recording helper which keeps tracks of the matching log messages. It has two attributes:

records

A list of logging.LogRecord objects of the matching log messages.

output

A list of str objects with the formatted output of matching messages.

示例:

with self.assertLogs('foo', level='INFO') as cm:
   logging.getLogger('foo').info('first message')
   logging.getLogger('foo.bar').error('second message')
self.assertEqual(cm.output, ['INFO:foo:first message',
                             'ERROR:foo.bar:second message'])

3.4 新版功能.

There are also other methods used to perform more specific checks, such as:

Method

Checks that

New in

assertAlmostEqual(a, b)

round(a-b, 7) == 0

assertNotAlmostEqual(a, b)

round(a-b, 7) != 0

assertGreater(a, b)

a > b

3.1

assertGreaterEqual(a, b)

a >= b

3.1

assertLess(a, b)

a < b

3.1

assertLessEqual(a, b)

a <= b

3.1

assertRegex(s, r)

r.search(s)

3.1

assertNotRegex(s, r)

not r.search(s)

3.2

assertCountEqual(a, b)

a and b have the same elements in the same number, regardless of their order

3.2

assertAlmostEqual(first, second, places=7, msg=None, delta=None)

assertNotAlmostEqual(first, second, places=7, msg=None, delta=None)

Test that first and second are approximately (or not approximately) equal by computing the difference, rounding to the given number of decimal places (default 7), and comparing to zero. Note that these methods round the values to the given number of decimal places (i.e. like the round() function) and not significant digits.

If delta is supplied instead of places then the difference between first and second must be less or equal to (or greater than) delta.

Supplying both delta and places raises a TypeError.

在 3.2 版更改: assertAlmostEqual() automatically considers almost equal objects that compare equal. assertNotAlmostEqual() automatically fails if the objects compare equal. Added the delta keyword argument.

assertGreater(first, second, msg=None)

assertGreaterEqual(first, second, msg=None)

assertLess(first, second, msg=None)

assertLessEqual(first, second, msg=None)

Test that first is respectively >, >=, < or <= than second depending on the method name. If not, the test will fail:

>>> self.assertGreaterEqual(3, 4)
AssertionError: "3" unexpectedly not greater than or equal to "4"

3.1 新版功能.

assertRegex(text, regex, msg=None)

assertNotRegex(text, regex, msg=None)

Test that a regex search matches (or does not match) text. In case of failure, the error message will include the pattern and the text (or the pattern and the part of text that unexpectedly matched). regex may be a regular expression object or a string containing a regular expression suitable for use by re.search().

3.1 新版功能: under the name assertRegexpMatches.

在 3.2 版更改: The method assertRegexpMatches() has been renamed to assertRegex().

3.2 新版功能: assertNotRegex()

3.5 新版功能: The name assertNotRegexpMatches is a deprecated alias for assertNotRegex().

assertCountEqual(first, second, msg=None)

Test that sequence first contains the same elements as second, regardless of their order. When they don’t, an error message listing the differences between the sequences will be generated.

Duplicate elements are not ignored when comparing first and second. It verifies whether each element has the same count in both sequences. Equivalent to: assertEqual(Counter(list(first)), Counter(list(second))) but works with sequences of unhashable objects as well.

3.2 新版功能.

The assertEqual() method dispatches the equality check for objects of the same type to different type-specific methods. These methods are already implemented for most of the built-in types, but it’s also possible to register new methods using addTypeEqualityFunc():

addTypeEqualityFunc(typeobj, function)

Registers a type-specific method called by assertEqual() to check if two objects of exactly the same typeobj (not subclasses) compare equal. function must take two positional arguments and a third msg=None keyword argument just as assertEqual() does. It must raise self.failureException(msg) when inequality between the first two parameters is detected – possibly providing useful information and explaining the inequalities in details in the error message.

3.1 新版功能.

The list of type-specific methods automatically used by assertEqual() are summarized in the following table. Note that it’s usually not necessary to invoke these methods directly.

Method

Used to compare

New in

assertMultiLineEqual(a, b)

strings

3.1

assertSequenceEqual(a, b)

sequences

3.1

assertListEqual(a, b)

lists

3.1

assertTupleEqual(a, b)

tuples

3.1

assertSetEqual(a, b)

sets or frozensets

3.1

assertDictEqual(a, b)

dicts

3.1

assertMultiLineEqual(first, second, msg=None)

Test that the multiline string first is equal to the string second. When not equal a diff of the two strings highlighting the differences will be included in the error message. This method is used by default when comparing strings with assertEqual().

3.1 新版功能.

assertSequenceEqual(first, second, msg=None, seq_type=None)

Tests that two sequences are equal. If a seq_type is supplied, both first and second must be instances of seq_type or a failure will be raised. If the sequences are different an error message is constructed that shows the difference between the two.

This method is not called directly by assertEqual(), but it’s used to implement assertListEqual() and assertTupleEqual().

3.1 新版功能.

assertListEqual(first, second, msg=None)

assertTupleEqual(first, second, msg=None)

Tests that two lists or tuples are equal. If not, an error message is constructed that shows only the differences between the two. An error is also raised if either of the parameters are of the wrong type. These methods are used by default when comparing lists or tuples with assertEqual().

3.1 新版功能.

assertSetEqual(first, second, msg=None)

Tests that two sets are equal. If not, an error message is constructed that lists the differences between the sets. This method is used by default when comparing sets or frozensets with assertEqual().

Fails if either of first or second does not have a set.difference() method.

3.1 新版功能.

assertDictEqual(first, second, msg=None)

Test that two dictionaries are equal. If not, an error message is constructed that shows the differences in the dictionaries. This method will be used by default to compare dictionaries in calls to assertEqual().

3.1 新版功能.

Finally the TestCase provides the following methods and attributes:

fail(msg=None)

Signals a test failure unconditionally, with msg or None for the error message.

failureException

This class attribute gives the exception raised by the test method. If a test framework needs to use a specialized exception, possibly to carry additional information, it must subclass this exception in order to “play fair” with the framework. The initial value of this attribute is AssertionError.

longMessage

This class attribute determines what happens when a custom failure message is passed as the msg argument to an assertXYY call that fails. True is the default value. In this case, the custom message is appended to the end of the standard failure message. When set to False, the custom message replaces the standard message.

The class setting can be overridden in individual test methods by assigning an instance attribute, self.longMessage, to True or False before calling the assert methods.

The class setting gets reset before each test call.

3.1 新版功能.

maxDiff

This attribute controls the maximum length of diffs output by assert methods that report diffs on failure. It defaults to 80*8 characters. Assert methods affected by this attribute are assertSequenceEqual() (including all the sequence comparison methods that delegate to it), assertDictEqual() and assertMultiLineEqual().

Setting maxDiff to None means that there is no maximum length of diffs.

3.2 新版功能.

Testing frameworks can use the following methods to collect information on the test:

countTestCases()

Return the number of tests represented by this test object. For TestCase instances, this will always be 1.

defaultTestResult()

Return an instance of the test result class that should be used for this test case class (if no other result instance is provided to the run() method).

For TestCase instances, this will always be an instance of TestResult; subclasses of TestCase should override this as necessary.

id()

Return a string identifying the specific test case. This is usually the full name of the test method, including the module and class name.

shortDescription()

Returns a description of the test, or None if no description has been provided. The default implementation of this method returns the first line of the test method’s docstring, if available, or None.

在 3.1 版更改: In 3.1 this was changed to add the test name to the short description even in the presence of a docstring. This caused compatibility issues with unittest extensions and adding the test name was moved to the TextTestResult in Python 3.2.

addCleanup(function, *args, **kwargs)

Add a function to be called after tearDown() to cleanup resources used during the test. Functions will be called in reverse order to the order they are added (LIFO). They are called with any arguments and keyword arguments passed into addCleanup() when they are added.

If setUp() fails, meaning that tearDown() is not called, then any cleanup functions added will still be called.

3.1 新版功能.

doCleanups()

This method is called unconditionally after tearDown(), or after setUp() if setUp() raises an exception.

It is responsible for calling all the cleanup functions added by addCleanup(). If you need cleanup functions to be called prior to tearDown() then you can call doCleanups() yourself.

doCleanups() pops methods off the stack of cleanup functions one at a time, so it can be called at any time.

3.1 新版功能.

class unittest.FunctionTestCase(testFunc, setUp=None, tearDown=None, description=None)

This class implements the portion of the TestCase interface which allows the test runner to drive the test, but does not provide the methods which test code can use to check and report errors. This is used to create test cases using legacy test code, allowing it to be integrated into a unittest-based test framework.

26.4.8.1.1. Deprecated aliases

For historical reasons, some of the TestCase methods had one or more aliases that are now deprecated. The following table lists the correct names along with their deprecated aliases:

方法名

Deprecated alias

Deprecated alias

assertEqual()

failUnlessEqual

assertEquals

assertNotEqual()

failIfEqual

assertNotEquals

assertTrue()

failUnless

assert_

assertFalse()

failIf

assertRaises()

failUnlessRaises

assertAlmostEqual()

failUnlessAlmostEqual

assertAlmostEquals

assertNotAlmostEqual()

failIfAlmostEqual

assertNotAlmostEquals

assertRegex()

assertRegexpMatches

assertNotRegex()

assertNotRegexpMatches

assertRaisesRegex()

assertRaisesRegexp

3.1 版后已移除: the fail* aliases listed in the second column.

3.2 版后已移除: the assert* aliases listed in the third column.

3.2 版后已移除: assertRegexpMatches and assertRaisesRegexp have been renamed to assertRegex() and assertRaisesRegex().

3.5 版后已移除: the assertNotRegexpMatches name in favor of assertNotRegex().

26.4.8.2. Grouping tests

class unittest.TestSuite(tests=())

This class represents an aggregation of individual test cases and test suites. The class presents the interface needed by the test runner to allow it to be run as any other test case. Running a TestSuite instance is the same as iterating over the suite, running each test individually.

If tests is given, it must be an iterable of individual test cases or other test suites that will be used to build the suite initially. Additional methods are provided to add test cases and suites to the collection later on.

TestSuite objects behave much like TestCase objects, except they do not actually implement a test. Instead, they are used to aggregate tests into groups of tests that should be run together. Some additional methods are available to add tests to TestSuite instances:

addTest(test)

Add a TestCase or TestSuite to the suite.

addTests(tests)

Add all the tests from an iterable of TestCase and TestSuite instances to this test suite.

This is equivalent to iterating over tests, calling addTest() for each element.

TestSuite shares the following methods with TestCase:

run(result)

Run the tests associated with this suite, collecting the result into the test result object passed as result. Note that unlike TestCase.run(), TestSuite.run() requires the result object to be passed in.

debug()

Run the tests associated with this suite without collecting the result. This allows exceptions raised by the test to be propagated to the caller and can be used to support running tests under a debugger.

countTestCases()

Return the number of tests represented by this test object, including all individual tests and sub-suites.

__iter__()

Tests grouped by a TestSuite are always accessed by iteration. Subclasses can lazily provide tests by overriding __iter__(). Note that this method may be called several times on a single suite (for example when counting tests or comparing for equality) so the tests returned by repeated iterations before TestSuite.run() must be the same for each call iteration. After TestSuite.run(), callers should not rely on the tests returned by this method unless the caller uses a subclass that overrides TestSuite._removeTestAtIndex() to preserve test references.

在 3.2 版更改: In earlier versions the TestSuite accessed tests directly rather than through iteration, so overriding __iter__() wasn’t sufficient for providing tests.

在 3.4 版更改: In earlier versions the TestSuite held references to each TestCase after TestSuite.run(). Subclasses can restore that behavior by overriding TestSuite._removeTestAtIndex().

In the typical usage of a TestSuite object, the run() method is invoked by a TestRunner rather than by the end-user test harness.

26.4.8.3. Loading and running tests

class unittest.TestLoader

The TestLoader class is used to create test suites from classes and modules. Normally, there is no need to create an instance of this class; the unittest module provides an instance that can be shared as unittest.defaultTestLoader. Using a subclass or instance, however, allows customization of some configurable properties.

TestLoader objects have the following attributes:

errors

A list of the non-fatal errors encountered while loading tests. Not reset by the loader at any point. Fatal errors are signalled by the relevant a method raising an exception to the caller. Non-fatal errors are also indicated by a synthetic test that will raise the original error when run.

3.5 新版功能.

TestLoader objects have the following methods:

loadTestsFromTestCase(testCaseClass)

Return a suite of all test cases contained in the TestCase-derived testCaseClass.

A test case instance is created for each method named by getTestCaseNames(). By default these are the method names beginning with test. If getTestCaseNames() returns no methods, but the runTest() method is implemented, a single test case is created for that method instead.

loadTestsFromModule(module, pattern=None)

Return a suite of all test cases contained in the given module. This method searches module for classes derived from TestCase and creates an instance of the class for each test method defined for the class.

注解

While using a hierarchy of TestCase-derived classes can be convenient in sharing fixtures and helper functions, defining test methods on base classes that are not intended to be instantiated directly does not play well with this method. Doing so, however, can be useful when the fixtures are different and defined in subclasses.

If a module provides a load_tests function it will be called to load the tests. This allows modules to customize test loading. This is the load_tests protocol. The pattern argument is passed as the third argument to load_tests.

在 3.2 版更改: Support for load_tests added.

在 3.5 版更改: The undocumented and unofficial use_load_tests default argument is deprecated and ignored, although it is still accepted for backward compatibility. The method also now accepts a keyword-only argument pattern which is passed to load_tests as the third argument.

loadTestsFromName(name, module=None)

Return a suite of all test cases given a string specifier.

The specifier name is a “dotted name” that may resolve either to a module, a test case class, a test method within a test case class, a TestSuite instance, or a callable object which returns a TestCase or TestSuite instance. These checks are applied in the order listed here; that is, a method on a possible test case class will be picked up as “a test method within a test case class”, rather than “a callable object”.

For example, if you have a module SampleTests containing a TestCase-derived class SampleTestCase with three test methods (test_one(), test_two(), and test_three()), the specifier 'SampleTests.SampleTestCase' would cause this method to return a suite which will run all three test methods. Using the specifier 'SampleTests.SampleTestCase.test_two' would cause it to return a test suite which will run only the test_two() test method. The specifier can refer to modules and packages which have not been imported; they will be imported as a side-effect.

The method optionally resolves name relative to the given module.

在 3.5 版更改: If an ImportError or AttributeError occurs while traversing name then a synthetic test that raises that error when run will be returned. These errors are included in the errors accumulated by self.errors.

loadTestsFromNames(names, module=None)

Similar to loadTestsFromName(), but takes a sequence of names rather than a single name. The return value is a test suite which supports all the tests defined for each name.

getTestCaseNames(testCaseClass)

Return a sorted sequence of method names found within testCaseClass; this should be a subclass of TestCase.

discover(start_dir, pattern='test*.py', top_level_dir=None)

Find all the test modules by recursing into subdirectories from the specified start directory, and return a TestSuite object containing them. Only test files that match pattern will be loaded. (Using shell style pattern matching.) Only module names that are importable (i.e. are valid Python identifiers) will be loaded.

All test modules must be importable from the top level of the project. If the start directory is not the top level directory then the top level directory must be specified separately.

If importing a module fails, for example due to a syntax error, then this will be recorded as a single error and discovery will continue. If the import failure is due to SkipTest being raised, it will be recorded as a skip instead of an error.

If a package (a directory containing a file named __init__.py) is found, the package will be checked for a load_tests function. If this exists then it will be called package.load_tests(loader, tests, pattern). Test discovery takes care to ensure that a package is only checked for tests once during an invocation, even if the load_tests function itself calls loader.discover.

If load_tests exists then discovery does not recurse into the package, load_tests is responsible for loading all tests in the package.

The pattern is deliberately not stored as a loader attribute so that packages can continue discovery themselves. top_level_dir is stored so load_tests does not need to pass this argument in to loader.discover().

start_dir can be a dotted module name as well as a directory.

3.2 新版功能.

在 3.4 版更改: Modules that raise SkipTest on import are recorded as skips, not errors. Discovery works for namespace packages. Paths are sorted before being imported so that execution order is the same even if the underlying file system’s ordering is not dependent on file name.

在 3.5 版更改: Found packages are now checked for load_tests regardless of whether their path matches pattern, because it is impossible for a package name to match the default pattern.

The following attributes of a TestLoader can be configured either by subclassing or assignment on an instance:

testMethodPrefix

String giving the prefix of method names which will be interpreted as test methods. The default value is 'test'.

This affects getTestCaseNames() and all the loadTestsFrom*() methods.

sortTestMethodsUsing

Function to be used to compare method names when sorting them in getTestCaseNames() and all the loadTestsFrom*() methods.

suiteClass

Callable object that constructs a test suite from a list of tests. No methods on the resulting object are needed. The default value is the TestSuite class.

This affects all the loadTestsFrom*() methods.

class unittest.TestResult

This class is used to compile information about which tests have succeeded and which have failed.

A TestResult object stores the results of a set of tests. The TestCase and TestSuite classes ensure that results are properly recorded; test authors do not need to worry about recording the outcome of tests.

Testing frameworks built on top of unittest may want access to the TestResult object generated by running a set of tests for reporting purposes; a TestResult instance is returned by the TestRunner.run() method for this purpose.

TestResult instances have the following attributes that will be of interest when inspecting the results of running a set of tests:

errors

A list containing 2-tuples of TestCase instances and strings holding formatted tracebacks. Each tuple represents a test which raised an unexpected exception.

failures

A list containing 2-tuples of TestCase instances and strings holding formatted tracebacks. Each tuple represents a test where a failure was explicitly signalled using the TestCase.assert*() methods.

skipped

A list containing 2-tuples of TestCase instances and strings holding the reason for skipping the test.

3.1 新版功能.

expectedFailures

A list containing 2-tuples of TestCase instances and strings holding formatted tracebacks. Each tuple represents an expected failure of the test case.

unexpectedSuccesses

A list containing TestCase instances that were marked as expected failures, but succeeded.

shouldStop

Set to True when the execution of tests should stop by stop().

testsRun

The total number of tests run so far.

buffer

If set to true, sys.stdout and sys.stderr will be buffered in between startTest() and stopTest() being called. Collected output will only be echoed onto the real sys.stdout and sys.stderr if the test fails or errors. Any output is also attached to the failure / error message.

3.2 新版功能.

failfast

If set to true stop() will be called on the first failure or error, halting the test run.

3.2 新版功能.

tb_locals

If set to true then local variables will be shown in tracebacks.

3.5 新版功能.

wasSuccessful()

Return True if all tests run so far have passed, otherwise returns False.

在 3.4 版更改: Returns False if there were any unexpectedSuccesses from tests marked with the expectedFailure() decorator.

stop()

This method can be called to signal that the set of tests being run should be aborted by setting the shouldStop attribute to True. TestRunner objects should respect this flag and return without running any additional tests.

For example, this feature is used by the TextTestRunner class to stop the test framework when the user signals an interrupt from the keyboard. Interactive tools which provide TestRunner implementations can use this in a similar manner.

The following methods of the TestResult class are used to maintain the internal data structures, and may be extended in subclasses to support additional reporting requirements. This is particularly useful in building tools which support interactive reporting while tests are being run.

startTest(test)

Called when the test case test is about to be run.

stopTest(test)

Called after the test case test has been executed, regardless of the outcome.

startTestRun()

Called once before any tests are executed.

3.1 新版功能.

stopTestRun()

Called once after all tests are executed.

3.1 新版功能.

addError(test, err)

Called when the test case test raises an unexpected exception. err is a tuple of the form returned by sys.exc_info(): (type, value, traceback).

The default implementation appends a tuple (test, formatted_err) to the instance’s errors attribute, where formatted_err is a formatted traceback derived from err.

addFailure(test, err)

Called when the test case test signals a failure. err is a tuple of the form returned by sys.exc_info(): (type, value, traceback).

The default implementation appends a tuple (test, formatted_err) to the instance’s failures attribute, where formatted_err is a formatted traceback derived from err.

addSuccess(test)

Called when the test case test succeeds.

The default implementation does nothing.

addSkip(test, reason)

Called when the test case test is skipped. reason is the reason the test gave for skipping.

The default implementation appends a tuple (test, reason) to the instance’s skipped attribute.

addExpectedFailure(test, err)

Called when the test case test fails, but was marked with the expectedFailure() decorator.

The default implementation appends a tuple (test, formatted_err) to the instance’s expectedFailures attribute, where formatted_err is a formatted traceback derived from err.

addUnexpectedSuccess(test)

Called when the test case test was marked with the expectedFailure() decorator, but succeeded.

The default implementation appends the test to the instance’s unexpectedSuccesses attribute.

addSubTest(test, subtest, outcome)

Called when a subtest finishes. test is the test case corresponding to the test method. subtest is a custom TestCase instance describing the subtest.

If outcome is None, the subtest succeeded. Otherwise, it failed with an exception where outcome is a tuple of the form returned by sys.exc_info(): (type, value, traceback).

The default implementation does nothing when the outcome is a success, and records subtest failures as normal failures.

3.4 新版功能.

class unittest.TextTestResult(stream, descriptions, verbosity)

A concrete implementation of TestResult used by the TextTestRunner.

3.2 新版功能: This class was previously named _TextTestResult. The old name still exists as an alias but is deprecated.

unittest.defaultTestLoader

Instance of the TestLoader class intended to be shared. If no customization of the TestLoader is needed, this instance can be used instead of repeatedly creating new instances.

class unittest.TextTestRunner(stream=None, descriptions=True, verbosity=1, failfast=False, buffer=False, resultclass=None, warnings=None, *, tb_locals=False)

A basic test runner implementation that outputs results to a stream. If stream is None, the default, sys.stderr is used as the output stream. This class has a few configurable parameters, but is essentially very simple. Graphical applications which run test suites should provide alternate implementations. Such implementations should accept **kwargs as the interface to construct runners changes when features are added to unittest.

By default this runner shows DeprecationWarning, PendingDeprecationWarning, ResourceWarning and ImportWarning even if they are ignored by default. Deprecation warnings caused by deprecated unittest methods are also special-cased and, when the warning filters are 'default' or 'always', they will appear only once per-module, in order to avoid too many warning messages. This behavior can be overridden using Python’s -Wd or -Wa options (see Warning control) and leaving warnings to None.

在 3.2 版更改: Added the warnings argument.

在 3.2 版更改: The default stream is set to sys.stderr at instantiation time rather than import time.

在 3.5 版更改: Added the tb_locals parameter.

_makeResult()

This method returns the instance of TestResult used by run(). It is not intended to be called directly, but can be overridden in subclasses to provide a custom TestResult.

_makeResult() instantiates the class or callable passed in the TextTestRunner constructor as the resultclass argument. It defaults to TextTestResult if no resultclass is provided. The result class is instantiated with the following arguments:

stream, descriptions, verbosity

run(test)

This method is the main public interface to the TextTestRunner. This method takes a TestSuite or TestCase instance. A TestResult is created by calling _makeResult() and the test(s) are run and the results printed to stdout.

unittest.main(module='__main__', defaultTest=None, argv=None, testRunner=None, testLoader=unittest.defaultTestLoader, exit=True, verbosity=1, failfast=None, catchbreak=None, buffer=None, warnings=None)

A command-line program that loads a set of tests from module and runs them; this is primarily for making test modules conveniently executable. The simplest use for this function is to include the following line at the end of a test script:

if __name__ == '__main__':
    unittest.main()

You can run tests with more detailed information by passing in the verbosity argument:

if __name__ == '__main__':
    unittest.main(verbosity=2)

The defaultTest argument is either the name of a single test or an iterable of test names to run if no test names are specified via argv. If not specified or None and no test names are provided via argv, all tests found in module are run.

The argv argument can be a list of options passed to the program, with the first element being the program name. If not specified or None, the values of sys.argv are used.

The testRunner argument can either be a test runner class or an already created instance of it. By default main calls sys.exit() with an exit code indicating success or failure of the tests run.

The testLoader argument has to be a TestLoader instance, and defaults to defaultTestLoader.

main supports being used from the interactive interpreter by passing in the argument exit=False. This displays the result on standard output without calling sys.exit():

>>> from unittest import main
>>> main(module='test_module', exit=False)

The failfast, catchbreak and buffer parameters have the same effect as the same-name command-line options.

The warnings argument specifies the warning filter that should be used while running the tests. If it’s not specified, it will remain None if a -W option is passed to python (see Warning control), otherwise it will be set to 'default'.

Calling main actually returns an instance of the TestProgram class. This stores the result of the tests run as the result attribute.

在 3.1 版更改: The exit parameter was added.

在 3.2 版更改: The verbosity, failfast, catchbreak, buffer and warnings parameters were added.

在 3.4 版更改: The defaultTest parameter was changed to also accept an iterable of test names.

26.4.8.3.1. load_tests Protocol

3.2 新版功能.

Modules or packages can customize how tests are loaded from them during normal test runs or test discovery by implementing a function called load_tests.

If a test module defines load_tests it will be called by TestLoader.loadTestsFromModule() with the following arguments:

load_tests(loader, standard_tests, pattern)

where pattern is passed straight through from loadTestsFromModule. It defaults to None.

It should return a TestSuite.

loader is the instance of TestLoader doing the loading. standard_tests are the tests that would be loaded by default from the module. It is common for test modules to only want to add or remove tests from the standard set of tests. The third argument is used when loading packages as part of test discovery.

A typical load_tests function that loads tests from a specific set of TestCase classes may look like:

test_cases = (TestCase1, TestCase2, TestCase3)

def load_tests(loader, tests, pattern):
    suite = TestSuite()
    for test_class in test_cases:
        tests = loader.loadTestsFromTestCase(test_class)
        suite.addTests(tests)
    return suite

If discovery is started in a directory containing a package, either from the command line or by calling TestLoader.discover(), then the package __init__.py will be checked for load_tests. If that function does not exist, discovery will recurse into the package as though it were just another directory. Otherwise, discovery of the package’s tests will be left up to load_tests which is called with the following arguments:

load_tests(loader, standard_tests, pattern)

This should return a TestSuite representing all the tests from the package. (standard_tests will only contain tests collected from __init__.py.)

Because the pattern is passed into load_tests the package is free to continue (and potentially modify) test discovery. A ‘do nothing’ load_tests function for a test package would look like:

def load_tests(loader, standard_tests, pattern):
    # top level directory cached on loader instance
    this_dir = os.path.dirname(__file__)
    package_tests = loader.discover(start_dir=this_dir, pattern=pattern)
    standard_tests.addTests(package_tests)
    return standard_tests

在 3.5 版更改: Discovery no longer checks package names for matching pattern due to the impossibility of package names matching the default pattern.

26.4.9. Class and Module Fixtures

Class and module level fixtures are implemented in TestSuite. When the test suite encounters a test from a new class then tearDownClass() from the previous class (if there is one) is called, followed by setUpClass() from the new class.

Similarly if a test is from a different module from the previous test then tearDownModule from the previous module is run, followed by setUpModule from the new module.

After all the tests have run the final tearDownClass and tearDownModule are run.

Note that shared fixtures do not play well with [potential] features like test parallelization and they break test isolation. They should be used with care.

The default ordering of tests created by the unittest test loaders is to group all tests from the same modules and classes together. This will lead to setUpClass / setUpModule (etc) being called exactly once per class and module. If you randomize the order, so that tests from different modules and classes are adjacent to each other, then these shared fixture functions may be called multiple times in a single test run.

Shared fixtures are not intended to work with suites with non-standard ordering. A BaseTestSuite still exists for frameworks that don’t want to support shared fixtures.

If there are any exceptions raised during one of the shared fixture functions the test is reported as an error. Because there is no corresponding test instance an _ErrorHolder object (that has the same interface as a TestCase) is created to represent the error. If you are just using the standard unittest test runner then this detail doesn’t matter, but if you are a framework author it may be relevant.

26.4.9.1. setUpClass and tearDownClass

These must be implemented as class methods:

import unittest

class Test(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls._connection = createExpensiveConnectionObject()

    @classmethod
    def tearDownClass(cls):
        cls._connection.destroy()

If you want the setUpClass and tearDownClass on base classes called then you must call up to them yourself. The implementations in TestCase are empty.

If an exception is raised during a setUpClass then the tests in the class are not run and the tearDownClass is not run. Skipped classes will not have setUpClass or tearDownClass run. If the exception is a SkipTest exception then the class will be reported as having been skipped instead of as an error.

26.4.9.2. setUpModule and tearDownModule

These should be implemented as functions:

def setUpModule():
    createConnection()

def tearDownModule():
    closeConnection()

If an exception is raised in a setUpModule then none of the tests in the module will be run and the tearDownModule will not be run. If the exception is a SkipTest exception then the module will be reported as having been skipped instead of as an error.

26.4.10. Signal Handling

3.2 新版功能.

The -c/--catch command-line option to unittest, along with the catchbreak parameter to unittest.main(), provide more friendly handling of control-C during a test run. With catch break behavior enabled control-C will allow the currently running test to complete, and the test run will then end and report all the results so far. A second control-c will raise a KeyboardInterrupt in the usual way.

The control-c handling signal handler attempts to remain compatible with code or tests that install their own signal.SIGINT handler. If the unittest handler is called but isn’t the installed signal.SIGINT handler, i.e. it has been replaced by the system under test and delegated to, then it calls the default handler. This will normally be the expected behavior by code that replaces an installed handler and delegates to it. For individual tests that need unittest control-c handling disabled the removeHandler() decorator can be used.

There are a few utility functions for framework authors to enable control-c handling functionality within test frameworks.

unittest.installHandler()

Install the control-c handler. When a signal.SIGINT is received (usually in response to the user pressing control-c) all registered results have stop() called.

unittest.registerResult(result)

Register a TestResult object for control-c handling. Registering a result stores a weak reference to it, so it doesn’t prevent the result from being garbage collected.

Registering a TestResult object has no side-effects if control-c handling is not enabled, so test frameworks can unconditionally register all results they create independently of whether or not handling is enabled.

unittest.removeResult(result)

Remove a registered result. Once a result has been removed then stop() will no longer be called on that result object in response to a control-c.

unittest.removeHandler(function=None)

When called without arguments this function removes the control-c handler if it has been installed. This function can also be used as a test decorator to temporarily remove the handler while the test is being executed:

@unittest.removeHandler
def test_signal_handling(self):
    ...

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

26.5. unittest.mock — 模拟对象库

3.3 新版功能.

源代码： Lib/unittest/mock.py [https://github.com/python/cpython/tree/3.6/Lib/unittest/mock.py]

unittest.mock 是一个用于测试的Python库。它允许使用模拟对象来替换受测系统的部分，并对它们如何已经被使用进行断言。

unittest.mock 提供了一个核心类 Mock 用于消除了在整个测试套件中创建大量存根(stub)的需求。创建后，就可以断言调用了哪些方法/属性及其参数。还可以以常规方式指定返回值并设置所需的属性。

此外，mock 提供了用于修补测试范围内模块和类级别属性的 patch() 装饰器，和用于创建独特对象的 sentinel 。 阅读 quick guide 中的案例了解如何使用 Mock ，MagicMock 和 patch() 。

Mock 是为 unittest 而设计，且简单易用。模拟基于 ‘action -> assertion’ 模式，而不是许多模拟框架所使用的 ‘record -> replay’模式。

在 Python 的早期版本要单独使用 unittest.mock ，在 PyPI 获取 mock [https://pypi.org/project/mock]

26.5.1. 快速上手

当您访问对象时， Mock 和 MagicMock 将创建所有属性和方法，并保存他们在使用时的细节。你可以通过配置，指定返回值或者限制可访问属性，然后断言他们如何被调用。

>>> from unittest.mock import MagicMock
>>> thing = ProductionClass()
>>> thing.method = MagicMock(return_value=3)
>>> thing.method(3, 4, 5, key='value')
3
>>> thing.method.assert_called_with(3, 4, 5, key='value')

通过 side_effect 设置副作用(side effects) ，可以是一个 mock 被调用是抛出的异常

>>> mock = Mock(side_effect=KeyError('foo'))
>>> mock()
Traceback (most recent call last):
 ...
KeyError: 'foo'

>>> values = {'a': 1, 'b': 2, 'c': 3}
>>> def side_effect(arg):
...     return values[arg]
...
>>> mock.side_effect = side_effect
>>> mock('a'), mock('b'), mock('c')
(1, 2, 3)
>>> mock.side_effect = [5, 4, 3, 2, 1]
>>> mock(), mock(), mock()
(5, 4, 3)

Mock 还可以通过其他方法配置和控制其行为。例如 mock 可以通过设置 spec 参数来从一个对象中获取其规格(specification)。如果访问 mock 的属性或方法不在 spec 中，会报 AttributeError 错误。

The patch() decorator / context manager makes it easy to mock classes or objects in a module under test. The object you specify will be replaced with a mock (or other object) during the test and restored when the test ends:

>>> from unittest.mock import patch
>>> @patch('module.ClassName2')
... @patch('module.ClassName1')
... def test(MockClass1, MockClass2):
...     module.ClassName1()
...     module.ClassName2()
...     assert MockClass1 is module.ClassName1
...     assert MockClass2 is module.ClassName2
...     assert MockClass1.called
...     assert MockClass2.called
...
>>> test()

注解

When you nest patch decorators the mocks are passed in to the decorated function in the same order they applied (the normal python order that decorators are applied). This means from the bottom up, so in the example above the mock for module.ClassName1 is passed in first.

在查找对象的名称空间中修补对象使用 patch() 。使用起来很简单，阅读 在哪里打补丁 来快速上手。

patch() 也可以 with 语句中使用上下文管理。

>>> with patch.object(ProductionClass, 'method', return_value=None) as mock_method:
...     thing = ProductionClass()
...     thing.method(1, 2, 3)
...
>>> mock_method.assert_called_once_with(1, 2, 3)

还有一个 patch.dict() 用于在一定范围内设置字典中的值，并在测试结束时将字典恢复为其原始状态：

>>> foo = {'key': 'value'}
>>> original = foo.copy()
>>> with patch.dict(foo, {'newkey': 'newvalue'}, clear=True):
...     assert foo == {'newkey': 'newvalue'}
...
>>> assert foo == original

Mock支持 Python 魔术方法 。使用模式方法最简单的方式是使用 MagicMock class. 。它可以做如下事情：

>>> mock = MagicMock()
>>> mock.__str__.return_value = 'foobarbaz'
>>> str(mock)
'foobarbaz'
>>> mock.__str__.assert_called_with()

Mock 能指定函数（或其他 Mock 实例）为魔术方法，它们将被适当地调用。 MagicMock 是预先创建了所有魔术方法（所有有用的方法） 的 Mock 。

下面是一个使用了普通 Mock 类的魔术方法的例子

>>> mock = Mock()
>>> mock.__str__ = Mock(return_value='wheeeeee')
>>> str(mock)
'wheeeeee'

使用 auto-speccing 可以保证测试中的模拟对象与要替换的对象具有相同的api 。在 patch 中可以通过 autospec 参数实现自动推断，或者使用 create_autospec() 函数。自动推断会创建一个与要替换对象相同的属相和方法的模拟对象，并且任何函数和方法（包括构造函数）都具有与真实对象相同的调用签名。

这么做是为了因确保不当地使用 mock 导致与生产代码相同的失败：

>>> from unittest.mock import create_autospec
>>> def function(a, b, c):
...     pass
...
>>> mock_function = create_autospec(function, return_value='fishy')
>>> mock_function(1, 2, 3)
'fishy'
>>> mock_function.assert_called_once_with(1, 2, 3)
>>> mock_function('wrong arguments')
Traceback (most recent call last):
 ...
TypeError: <lambda>() takes exactly 3 arguments (1 given)

在类中使用 create_autospec() 时，会复制 __init__ 的签名，另外在可调用对象上使用时，会复制 __call__ 的签名。

26.5.2. Mock 类

Mock 是一个可以灵活的替换存根 (stubs) 的对象，可以测试所有代码。 Mock 是可调用的，在访问其属性时创建一个新的 mock 1 。访问相同的属性只会返回相同的 mock 。 Mock 保存调用记录，可以通过断言获悉代码的调用。

MagicMock 是 Mock 的子类，它有所有预创建且可使用的魔术方法。在需要模拟不可调用对象时，可以使用 NonCallableMock  和 NonCallableMagicMock

patch() 装饰器使得用 Mock 对象临时替换特定模块中的类非常方便。 默认情况下 patch() 将为你创建一个 MagicMock。 你可以使用 patch() 的 new_callable 参数指定替代 Mock 的类。

class unittest.mock.Mock(spec=None, side_effect=None, return_value=DEFAULT, wraps=None, name=None, spec_set=None, unsafe=False, **kwargs)

创建一个新的 Mock 对象。通过可选参数指定 Mock 对象的行为：

spec: This can be either a list of strings or an existing object (a class or instance) that acts as the specification for the mock object. If you pass in an object then a list of strings is formed by calling dir on the object (excluding unsupported magic attributes and methods). Accessing any attribute not in this list will raise an AttributeError.

If spec is an object (rather than a list of strings) then __class__ returns the class of the spec object. This allows mocks to pass isinstance() tests.

spec_set: A stricter variant of spec. If used, attempting to set or get an attribute on the mock that isn’t on the object passed as spec_set will raise an AttributeError.

side_effect: A function to be called whenever the Mock is called. See the side_effect attribute. Useful for raising exceptions or dynamically changing return values. The function is called with the same arguments as the mock, and unless it returns DEFAULT, the return value of this function is used as the return value.

Alternatively side_effect can be an exception class or instance. In this case the exception will be raised when the mock is called.

If side_effect is an iterable then each call to the mock will return the next value from the iterable.

A side_effect can be cleared by setting it to None.

return_value: The value returned when the mock is called. By default this is a new Mock (created on first access). See the return_value attribute.

unsafe: By default if any attribute starts with assert or assret will raise an AttributeError. Passing unsafe=True will allow access to these attributes.

3.5 新版功能.

wraps: Item for the mock object to wrap. If wraps is not None then calling the Mock will pass the call through to the wrapped object (returning the real result). Attribute access on the mock will return a Mock object that wraps the corresponding attribute of the wrapped object (so attempting to access an attribute that doesn’t exist will raise an AttributeError).

If the mock has an explicit return_value set then calls are not passed to the wrapped object and the return_value is returned instead.

name: If the mock has a name then it will be used in the repr of the mock. This can be useful for debugging. The name is propagated to child mocks.

Mocks can also be called with arbitrary keyword arguments. These will be used to set attributes on the mock after it is created. See the configure_mock() method for details.

assert_called(*args, **kwargs)

Assert that the mock was called at least once.

>>> mock = Mock()
>>> mock.method()
<Mock name='mock.method()' id='...'>
>>> mock.method.assert_called()

3.6 新版功能.

assert_called_once(*args, **kwargs)

Assert that the mock was called exactly once.

>>> mock = Mock()
>>> mock.method()
<Mock name='mock.method()' id='...'>
>>> mock.method.assert_called_once()
>>> mock.method()
<Mock name='mock.method()' id='...'>
>>> mock.method.assert_called_once()
Traceback (most recent call last):
...
AssertionError: Expected 'method' to have been called once. Called 2 times.

3.6 新版功能.

assert_called_with(*args, **kwargs)

This method is a convenient way of asserting that calls are made in a particular way:

>>> mock = Mock()
>>> mock.method(1, 2, 3, test='wow')
<Mock name='mock.method()' id='...'>
>>> mock.method.assert_called_with(1, 2, 3, test='wow')

assert_called_once_with(*args, **kwargs)

Assert that the mock was called exactly once and that that call was with the specified arguments.

>>> mock = Mock(return_value=None)
>>> mock('foo', bar='baz')
>>> mock.assert_called_once_with('foo', bar='baz')
>>> mock('other', bar='values')
>>> mock.assert_called_once_with('other', bar='values')
Traceback (most recent call last):
  ...
AssertionError: Expected 'mock' to be called once. Called 2 times.

assert_any_call(*args, **kwargs)

assert the mock has been called with the specified arguments.

The assert passes if the mock has ever been called, unlike assert_called_with() and assert_called_once_with() that only pass if the call is the most recent one, and in the case of assert_called_once_with() it must also be the only call.

>>> mock = Mock(return_value=None)
>>> mock(1, 2, arg='thing')
>>> mock('some', 'thing', 'else')
>>> mock.assert_any_call(1, 2, arg='thing')

assert_has_calls(calls, any_order=False)

assert the mock has been called with the specified calls. The mock_calls list is checked for the calls.

If any_order is false (the default) then the calls must be sequential. There can be extra calls before or after the specified calls.

If any_order is true then the calls can be in any order, but they must all appear in mock_calls.

>>> mock = Mock(return_value=None)
>>> mock(1)
>>> mock(2)
>>> mock(3)
>>> mock(4)
>>> calls = [call(2), call(3)]
>>> mock.assert_has_calls(calls)
>>> calls = [call(4), call(2), call(3)]
>>> mock.assert_has_calls(calls, any_order=True)

assert_not_called()

Assert the mock was never called.

>>> m = Mock()
>>> m.hello.assert_not_called()
>>> obj = m.hello()
>>> m.hello.assert_not_called()
Traceback (most recent call last):
  ...
AssertionError: Expected 'hello' to not have been called. Called 1 times.

3.5 新版功能.

reset_mock(*, return_value=False, side_effect=False)

The reset_mock method resets all the call attributes on a mock object:

>>> mock = Mock(return_value=None)
>>> mock('hello')
>>> mock.called
True
>>> mock.reset_mock()
>>> mock.called
False

在 3.6 版更改: Added two keyword only argument to the reset_mock function.

This can be useful where you want to make a series of assertions that reuse the same object. Note that reset_mock() doesn’t clear the return value, side_effect or any child attributes you have set using normal assignment by default. In case you want to reset return_value or side_effect, then pass the corresponding parameter as True. Child mocks and the return value mock (if any) are reset as well.

注解

return_value, and side_effect are keyword only argument.

mock_add_spec(spec, spec_set=False)

Add a spec to a mock. spec can either be an object or a list of strings. Only attributes on the spec can be fetched as attributes from the mock.

If spec_set is true then only attributes on the spec can be set.

attach_mock(mock, attribute)

Attach a mock as an attribute of this one, replacing its name and parent. Calls to the attached mock will be recorded in the method_calls and mock_calls attributes of this one.

configure_mock(**kwargs)

Set attributes on the mock through keyword arguments.

Attributes plus return values and side effects can be set on child mocks using standard dot notation and unpacking a dictionary in the method call:

>>> mock = Mock()
>>> attrs = {'method.return_value': 3, 'other.side_effect': KeyError}
>>> mock.configure_mock(**attrs)
>>> mock.method()
3
>>> mock.other()
Traceback (most recent call last):
  ...
KeyError

The same thing can be achieved in the constructor call to mocks:

>>> attrs = {'method.return_value': 3, 'other.side_effect': KeyError}
>>> mock = Mock(some_attribute='eggs', **attrs)
>>> mock.some_attribute
'eggs'
>>> mock.method()
3
>>> mock.other()
Traceback (most recent call last):
  ...
KeyError

configure_mock() exists to make it easier to do configuration after the mock has been created.

__dir__()

Mock objects limit the results of dir(some_mock) to useful results. For mocks with a spec this includes all the permitted attributes for the mock.

See FILTER_DIR for what this filtering does, and how to switch it off.

_get_child_mock(**kw)

Create the child mocks for attributes and return value. By default child mocks will be the same type as the parent. Subclasses of Mock may want to override this to customize the way child mocks are made.

For non-callable mocks the callable variant will be used (rather than any custom subclass).

called

A boolean representing whether or not the mock object has been called:

>>> mock = Mock(return_value=None)
>>> mock.called
False
>>> mock()
>>> mock.called
True

call_count

An integer telling you how many times the mock object has been called:

>>> mock = Mock(return_value=None)
>>> mock.call_count
0
>>> mock()
>>> mock()
>>> mock.call_count
2

return_value

Set this to configure the value returned by calling the mock:

>>> mock = Mock()
>>> mock.return_value = 'fish'
>>> mock()
'fish'

The default return value is a mock object and you can configure it in the normal way:

>>> mock = Mock()
>>> mock.return_value.attribute = sentinel.Attribute
>>> mock.return_value()
<Mock name='mock()()' id='...'>
>>> mock.return_value.assert_called_with()

return_value can also be set in the constructor:

>>> mock = Mock(return_value=3)
>>> mock.return_value
3
>>> mock()
3

side_effect

This can either be a function to be called when the mock is called, an iterable or an exception (class or instance) to be raised.

If you pass in a function it will be called with same arguments as the mock and unless the function returns the DEFAULT singleton the call to the mock will then return whatever the function returns. If the function returns DEFAULT then the mock will return its normal value (from the return_value).

If you pass in an iterable, it is used to retrieve an iterator which must yield a value on every call. This value can either be an exception instance to be raised, or a value to be returned from the call to the mock (DEFAULT handling is identical to the function case).

An example of a mock that raises an exception (to test exception handling of an API):

>>> mock = Mock()
>>> mock.side_effect = Exception('Boom!')
>>> mock()
Traceback (most recent call last):
  ...
Exception: Boom!

Using side_effect to return a sequence of values:

>>> mock = Mock()
>>> mock.side_effect = [3, 2, 1]
>>> mock(), mock(), mock()
(3, 2, 1)

Using a callable:

>>> mock = Mock(return_value=3)
>>> def side_effect(*args, **kwargs):
...     return DEFAULT
...
>>> mock.side_effect = side_effect
>>> mock()
3

side_effect can be set in the constructor. Here’s an example that adds one to the value the mock is called with and returns it:

>>> side_effect = lambda value: value + 1
>>> mock = Mock(side_effect=side_effect)
>>> mock(3)
4
>>> mock(-8)
-7

Setting side_effect to None clears it:

>>> m = Mock(side_effect=KeyError, return_value=3)
>>> m()
Traceback (most recent call last):
 ...
KeyError
>>> m.side_effect = None
>>> m()
3

call_args

This is either None (if the mock hasn’t been called), or the arguments that the mock was last called with. This will be in the form of a tuple: the first member is any ordered arguments the mock was called with (or an empty tuple) and the second member is any keyword arguments (or an empty dictionary).

>>> mock = Mock(return_value=None)
>>> print(mock.call_args)
None
>>> mock()
>>> mock.call_args
call()
>>> mock.call_args == ()
True
>>> mock(3, 4)
>>> mock.call_args
call(3, 4)
>>> mock.call_args == ((3, 4),)
True
>>> mock(3, 4, 5, key='fish', next='w00t!')
>>> mock.call_args
call(3, 4, 5, key='fish', next='w00t!')

call_args, along with members of the lists call_args_list, method_calls and mock_calls are call objects. These are tuples, so they can be unpacked to get at the individual arguments and make more complex assertions. See calls as tuples.

call_args_list

This is a list of all the calls made to the mock object in sequence (so the length of the list is the number of times it has been called). Before any calls have been made it is an empty list. The call object can be used for conveniently constructing lists of calls to compare with call_args_list.

>>> mock = Mock(return_value=None)
>>> mock()
>>> mock(3, 4)
>>> mock(key='fish', next='w00t!')
>>> mock.call_args_list
[call(), call(3, 4), call(key='fish', next='w00t!')]
>>> expected = [(), ((3, 4),), ({'key': 'fish', 'next': 'w00t!'},)]
>>> mock.call_args_list == expected
True

Members of call_args_list are call objects. These can be unpacked as tuples to get at the individual arguments. See calls as tuples.

method_calls

As well as tracking calls to themselves, mocks also track calls to methods and attributes, and their methods and attributes:

>>> mock = Mock()
>>> mock.method()
<Mock name='mock.method()' id='...'>
>>> mock.property.method.attribute()
<Mock name='mock.property.method.attribute()' id='...'>
>>> mock.method_calls
[call.method(), call.property.method.attribute()]

Members of method_calls are call objects. These can be unpacked as tuples to get at the individual arguments. See calls as tuples.

mock_calls

mock_calls records all calls to the mock object, its methods, magic methods and return value mocks.

>>> mock = MagicMock()
>>> result = mock(1, 2, 3)
>>> mock.first(a=3)
<MagicMock name='mock.first()' id='...'>
>>> mock.second()
<MagicMock name='mock.second()' id='...'>
>>> int(mock)
1
>>> result(1)
<MagicMock name='mock()()' id='...'>
>>> expected = [call(1, 2, 3), call.first(a=3), call.second(),
... call.__int__(), call()(1)]
>>> mock.mock_calls == expected
True

Members of mock_calls are call objects. These can be unpacked as tuples to get at the individual arguments. See calls as tuples.

注解

The way mock_calls are recorded means that where nested calls are made, the parameters of ancestor calls are not recorded and so will always compare equal:

>>> mock = MagicMock()
>>> mock.top(a=3).bottom()
<MagicMock name='mock.top().bottom()' id='...'>
>>> mock.mock_calls
[call.top(a=3), call.top().bottom()]
>>> mock.mock_calls[-1] == call.top(a=-1).bottom()
True

__class__

Normally the __class__ attribute of an object will return its type. For a mock object with a spec, __class__ returns the spec class instead. This allows mock objects to pass isinstance() tests for the object they are replacing / masquerading as:

>>> mock = Mock(spec=3)
>>> isinstance(mock, int)
True

__class__ is assignable to, this allows a mock to pass an isinstance() check without forcing you to use a spec:

>>> mock = Mock()
>>> mock.__class__ = dict
>>> isinstance(mock, dict)
True

class unittest.mock.NonCallableMock(spec=None, wraps=None, name=None, spec_set=None, **kwargs)

A non-callable version of Mock. The constructor parameters have the same meaning of Mock, with the exception of return_value and side_effect which have no meaning on a non-callable mock.

Mock objects that use a class or an instance as a spec or spec_set are able to pass isinstance() tests:

>>> mock = Mock(spec=SomeClass)
>>> isinstance(mock, SomeClass)
True
>>> mock = Mock(spec_set=SomeClass())
>>> isinstance(mock, SomeClass)
True

The Mock classes have support for mocking magic methods. See magic methods for the full details.

The mock classes and the patch() decorators all take arbitrary keyword arguments for configuration. For the patch() decorators the keywords are passed to the constructor of the mock being created. The keyword arguments are for configuring attributes of the mock:

>>> m = MagicMock(attribute=3, other='fish')
>>> m.attribute
3
>>> m.other
'fish'

The return value and side effect of child mocks can be set in the same way, using dotted notation. As you can’t use dotted names directly in a call you have to create a dictionary and unpack it using **:

>>> attrs = {'method.return_value': 3, 'other.side_effect': KeyError}
>>> mock = Mock(some_attribute='eggs', **attrs)
>>> mock.some_attribute
'eggs'
>>> mock.method()
3
>>> mock.other()
Traceback (most recent call last):
  ...
KeyError

A callable mock which was created with a spec (or a spec_set) will introspect the specification object’s signature when matching calls to the mock. Therefore, it can match the actual call’s arguments regardless of whether they were passed positionally or by name:

>>> def f(a, b, c): pass
...
>>> mock = Mock(spec=f)
>>> mock(1, 2, c=3)
<Mock name='mock()' id='140161580456576'>
>>> mock.assert_called_with(1, 2, 3)
>>> mock.assert_called_with(a=1, b=2, c=3)

This applies to assert_called_with(), assert_called_once_with(), assert_has_calls() and assert_any_call(). When Autospeccing, it will also apply to method calls on the mock object.

在 3.4 版更改: Added signature introspection on specced and autospecced mock objects.

class unittest.mock.PropertyMock(*args, **kwargs)

A mock intended to be used as a property, or other descriptor, on a class. PropertyMock provides __get__() and __set__() methods so you can specify a return value when it is fetched.

Fetching a PropertyMock instance from an object calls the mock, with no args. Setting it calls the mock with the value being set.

>>> class Foo:
...     @property
...     def foo(self):
...         return 'something'
...     @foo.setter
...     def foo(self, value):
...         pass
...
>>> with patch('__main__.Foo.foo', new_callable=PropertyMock) as mock_foo:
...     mock_foo.return_value = 'mockity-mock'
...     this_foo = Foo()
...     print(this_foo.foo)
...     this_foo.foo = 6
...
mockity-mock
>>> mock_foo.mock_calls
[call(), call(6)]

Because of the way mock attributes are stored you can’t directly attach a PropertyMock to a mock object. Instead you can attach it to the mock type object:

>>> m = MagicMock()
>>> p = PropertyMock(return_value=3)
>>> type(m).foo = p
>>> m.foo
3
>>> p.assert_called_once_with()

26.5.2.1. Calling

Mock objects are callable. The call will return the value set as the return_value attribute. The default return value is a new Mock object; it is created the first time the return value is accessed (either explicitly or by calling the Mock) - but it is stored and the same one returned each time.

Calls made to the object will be recorded in the attributes like call_args and call_args_list.

If side_effect is set then it will be called after the call has been recorded, so if side_effect raises an exception the call is still recorded.

The simplest way to make a mock raise an exception when called is to make side_effect an exception class or instance:

>>> m = MagicMock(side_effect=IndexError)
>>> m(1, 2, 3)
Traceback (most recent call last):
  ...
IndexError
>>> m.mock_calls
[call(1, 2, 3)]
>>> m.side_effect = KeyError('Bang!')
>>> m('two', 'three', 'four')
Traceback (most recent call last):
  ...
KeyError: 'Bang!'
>>> m.mock_calls
[call(1, 2, 3), call('two', 'three', 'four')]

If side_effect is a function then whatever that function returns is what calls to the mock return. The side_effect function is called with the same arguments as the mock. This allows you to vary the return value of the call dynamically, based on the input:

>>> def side_effect(value):
...     return value + 1
...
>>> m = MagicMock(side_effect=side_effect)
>>> m(1)
2
>>> m(2)
3
>>> m.mock_calls
[call(1), call(2)]

If you want the mock to still return the default return value (a new mock), or any set return value, then there are two ways of doing this. Either return mock.return_value from inside side_effect, or return DEFAULT:

>>> m = MagicMock()
>>> def side_effect(*args, **kwargs):
...     return m.return_value
...
>>> m.side_effect = side_effect
>>> m.return_value = 3
>>> m()
3
>>> def side_effect(*args, **kwargs):
...     return DEFAULT
...
>>> m.side_effect = side_effect
>>> m()
3

To remove a side_effect, and return to the default behaviour, set the side_effect to None:

>>> m = MagicMock(return_value=6)
>>> def side_effect(*args, **kwargs):
...     return 3
...
>>> m.side_effect = side_effect
>>> m()
3
>>> m.side_effect = None
>>> m()
6

The side_effect can also be any iterable object. Repeated calls to the mock will return values from the iterable (until the iterable is exhausted and a StopIteration is raised):

>>> m = MagicMock(side_effect=[1, 2, 3])
>>> m()
1
>>> m()
2
>>> m()
3
>>> m()
Traceback (most recent call last):
  ...
StopIteration

If any members of the iterable are exceptions they will be raised instead of returned:

>>> iterable = (33, ValueError, 66)
>>> m = MagicMock(side_effect=iterable)
>>> m()
33
>>> m()
Traceback (most recent call last):
 ...
ValueError
>>> m()
66

26.5.2.2. Deleting Attributes

Mock objects create attributes on demand. This allows them to pretend to be objects of any type.

You may want a mock object to return False to a hasattr() call, or raise an AttributeError when an attribute is fetched. You can do this by providing an object as a spec for a mock, but that isn’t always convenient.

You “block” attributes by deleting them. Once deleted, accessing an attribute will raise an AttributeError.

>>> mock = MagicMock()
>>> hasattr(mock, 'm')
True
>>> del mock.m
>>> hasattr(mock, 'm')
False
>>> del mock.f
>>> mock.f
Traceback (most recent call last):
    ...
AttributeError: f

26.5.2.3. Mock names and the name attribute

Since “name” is an argument to the Mock constructor, if you want your mock object to have a “name” attribute you can’t just pass it in at creation time. There are two alternatives. One option is to use configure_mock():

>>> mock = MagicMock()
>>> mock.configure_mock(name='my_name')
>>> mock.name
'my_name'

A simpler option is to simply set the “name” attribute after mock creation:

>>> mock = MagicMock()
>>> mock.name = "foo"

26.5.2.4. Attaching Mocks as Attributes

When you attach a mock as an attribute of another mock (or as the return value) it becomes a “child” of that mock. Calls to the child are recorded in the method_calls and mock_calls attributes of the parent. This is useful for configuring child mocks and then attaching them to the parent, or for attaching mocks to a parent that records all calls to the children and allows you to make assertions about the order of calls between mocks:

>>> parent = MagicMock()
>>> child1 = MagicMock(return_value=None)
>>> child2 = MagicMock(return_value=None)
>>> parent.child1 = child1
>>> parent.child2 = child2
>>> child1(1)
>>> child2(2)
>>> parent.mock_calls
[call.child1(1), call.child2(2)]

The exception to this is if the mock has a name. This allows you to prevent the “parenting” if for some reason you don’t want it to happen.

>>> mock = MagicMock()
>>> not_a_child = MagicMock(name='not-a-child')
>>> mock.attribute = not_a_child
>>> mock.attribute()
<MagicMock name='not-a-child()' id='...'>
>>> mock.mock_calls
[]

Mocks created for you by patch() are automatically given names. To attach mocks that have names to a parent you use the attach_mock() method:

>>> thing1 = object()
>>> thing2 = object()
>>> parent = MagicMock()
>>> with patch('__main__.thing1', return_value=None) as child1:
...     with patch('__main__.thing2', return_value=None) as child2:
...         parent.attach_mock(child1, 'child1')
...         parent.attach_mock(child2, 'child2')
...         child1('one')
...         child2('two')
...
>>> parent.mock_calls
[call.child1('one'), call.child2('two')]

1

The only exceptions are magic methods and attributes (those that have leading and trailing double underscores). Mock doesn’t create these but instead raises an AttributeError. This is because the interpreter will often implicitly request these methods, and gets very confused to get a new Mock object when it expects a magic method. If you need magic method support see magic methods.

26.5.3. The patchers

The patch decorators are used for patching objects only within the scope of the function they decorate. They automatically handle the unpatching for you, even if exceptions are raised. All of these functions can also be used in with statements or as class decorators.

26.5.3.1. patch

注解

patch() is straightforward to use. The key is to do the patching in the right namespace. See the section where to patch.

unittest.mock.patch(target, new=DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs)

patch() acts as a function decorator, class decorator or a context manager. Inside the body of the function or with statement, the target is patched with a new object. When the function/with statement exits the patch is undone.

If new is omitted, then the target is replaced with a MagicMock. If patch() is used as a decorator and new is omitted, the created mock is passed in as an extra argument to the decorated function. If patch() is used as a context manager the created mock is returned by the context manager.

target should be a string in the form 'package.module.ClassName'. The target is imported and the specified object replaced with the new object, so the target must be importable from the environment you are calling patch() from. The target is imported when the decorated function is executed, not at decoration time.

The spec and spec_set keyword arguments are passed to the MagicMock if patch is creating one for you.

In addition you can pass spec=True or spec_set=True, which causes patch to pass in the object being mocked as the spec/spec_set object.

new_callable allows you to specify a different class, or callable object, that will be called to create the new object. By default MagicMock is used.

A more powerful form of spec is autospec. If you set autospec=True then the mock will be created with a spec from the object being replaced. All attributes of the mock will also have the spec of the corresponding attribute of the object being replaced. Methods and functions being mocked will have their arguments checked and will raise a TypeError if they are called with the wrong signature. For mocks replacing a class, their return value (the ‘instance’) will have the same spec as the class. See the create_autospec() function and Autospeccing.

Instead of autospec=True you can pass autospec=some_object to use an arbitrary object as the spec instead of the one being replaced.

By default patch() will fail to replace attributes that don’t exist. If you pass in create=True, and the attribute doesn’t exist, patch will create the attribute for you when the patched function is called, and delete it again afterwards. This is useful for writing tests against attributes that your production code creates at runtime. It is off by default because it can be dangerous. With it switched on you can write passing tests against APIs that don’t actually exist!

注解

在 3.5 版更改: If you are patching builtins in a module then you don’t need to pass create=True, it will be added by default.

Patch can be used as a TestCase class decorator. It works by decorating each test method in the class. This reduces the boilerplate code when your test methods share a common patchings set. patch() finds tests by looking for method names that start with patch.TEST_PREFIX. By default this is 'test', which matches the way unittest finds tests. You can specify an alternative prefix by setting patch.TEST_PREFIX.

Patch can be used as a context manager, with the with statement. Here the patching applies to the indented block after the with statement. If you use “as” then the patched object will be bound to the name after the “as”; very useful if patch() is creating a mock object for you.

patch() takes arbitrary keyword arguments. These will be passed to the Mock (or new_callable) on construction.

patch.dict(...), patch.multiple(...) and patch.object(...) are available for alternate use-cases.

patch() as function decorator, creating the mock for you and passing it into the decorated function:

>>> @patch('__main__.SomeClass')
... def function(normal_argument, mock_class):
...     print(mock_class is SomeClass)
...
>>> function(None)
True

Patching a class replaces the class with a MagicMock instance. If the class is instantiated in the code under test then it will be the return_value of the mock that will be used.

If the class is instantiated multiple times you could use side_effect to return a new mock each time. Alternatively you can set the return_value to be anything you want.

To configure return values on methods of instances on the patched class you must do this on the return_value. For example:

>>> class Class:
...     def method(self):
...         pass
...
>>> with patch('__main__.Class') as MockClass:
...     instance = MockClass.return_value
...     instance.method.return_value = 'foo'
...     assert Class() is instance
...     assert Class().method() == 'foo'
...

If you use spec or spec_set and patch() is replacing a class, then the return value of the created mock will have the same spec.

>>> Original = Class
>>> patcher = patch('__main__.Class', spec=True)
>>> MockClass = patcher.start()
>>> instance = MockClass()
>>> assert isinstance(instance, Original)
>>> patcher.stop()

The new_callable argument is useful where you want to use an alternative class to the default MagicMock for the created mock. For example, if you wanted a NonCallableMock to be used:

>>> thing = object()
>>> with patch('__main__.thing', new_callable=NonCallableMock) as mock_thing:
...     assert thing is mock_thing
...     thing()
...
Traceback (most recent call last):
  ...
TypeError: 'NonCallableMock' object is not callable

Another use case might be to replace an object with an io.StringIO instance:

>>> from io import StringIO
>>> def foo():
...     print('Something')
...
>>> @patch('sys.stdout', new_callable=StringIO)
... def test(mock_stdout):
...     foo()
...     assert mock_stdout.getvalue() == 'Something\n'
...
>>> test()

When patch() is creating a mock for you, it is common that the first thing you need to do is to configure the mock. Some of that configuration can be done in the call to patch. Any arbitrary keywords you pass into the call will be used to set attributes on the created mock:

>>> patcher = patch('__main__.thing', first='one', second='two')
>>> mock_thing = patcher.start()
>>> mock_thing.first
'one'
>>> mock_thing.second
'two'

As well as attributes on the created mock attributes, like the return_value and side_effect, of child mocks can also be configured. These aren’t syntactically valid to pass in directly as keyword arguments, but a dictionary with these as keys can still be expanded into a patch() call using **:

>>> config = {'method.return_value': 3, 'other.side_effect': KeyError}
>>> patcher = patch('__main__.thing', **config)
>>> mock_thing = patcher.start()
>>> mock_thing.method()
3
>>> mock_thing.other()
Traceback (most recent call last):
  ...
KeyError

26.5.3.2. patch.object

patch.object(target, attribute, new=DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs)

patch the named member (attribute) on an object (target) with a mock object.

patch.object() can be used as a decorator, class decorator or a context manager. Arguments new, spec, create, spec_set, autospec and new_callable have the same meaning as for patch(). Like patch(), patch.object() takes arbitrary keyword arguments for configuring the mock object it creates.

When used as a class decorator patch.object() honours patch.TEST_PREFIX for choosing which methods to wrap.

You can either call patch.object() with three arguments or two arguments. The three argument form takes the object to be patched, the attribute name and the object to replace the attribute with.

When calling with the two argument form you omit the replacement object, and a mock is created for you and passed in as an extra argument to the decorated function:

>>> @patch.object(SomeClass, 'class_method')
... def test(mock_method):
...     SomeClass.class_method(3)
...     mock_method.assert_called_with(3)
...
>>> test()

spec, create and the other arguments to patch.object() have the same meaning as they do for patch().

26.5.3.3. patch.dict

patch.dict(in_dict, values=(), clear=False, **kwargs)

Patch a dictionary, or dictionary like object, and restore the dictionary to its original state after the test.

in_dict can be a dictionary or a mapping like container. If it is a mapping then it must at least support getting, setting and deleting items plus iterating over keys.

in_dict can also be a string specifying the name of the dictionary, which will then be fetched by importing it.

values can be a dictionary of values to set in the dictionary. values can also be an iterable of (key, value) pairs.

If clear is true then the dictionary will be cleared before the new values are set.

patch.dict() can also be called with arbitrary keyword arguments to set values in the dictionary.

patch.dict() can be used as a context manager, decorator or class decorator. When used as a class decorator patch.dict() honours patch.TEST_PREFIX for choosing which methods to wrap.

patch.dict() can be used to add members to a dictionary, or simply let a test change a dictionary, and ensure the dictionary is restored when the test ends.

>>> foo = {}
>>> with patch.dict(foo, {'newkey': 'newvalue'}):
...     assert foo == {'newkey': 'newvalue'}
...
>>> assert foo == {}

>>> import os
>>> with patch.dict('os.environ', {'newkey': 'newvalue'}):
...     print(os.environ['newkey'])
...
newvalue
>>> assert 'newkey' not in os.environ

Keywords can be used in the patch.dict() call to set values in the dictionary:

>>> mymodule = MagicMock()
>>> mymodule.function.return_value = 'fish'
>>> with patch.dict('sys.modules', mymodule=mymodule):
...     import mymodule
...     mymodule.function('some', 'args')
...
'fish'

patch.dict() can be used with dictionary like objects that aren’t actually dictionaries. At the very minimum they must support item getting, setting, deleting and either iteration or membership test. This corresponds to the magic methods __getitem__(), __setitem__(), __delitem__() and either __iter__() or __contains__().

>>> class Container:
...     def __init__(self):
...         self.values = {}
...     def __getitem__(self, name):
...         return self.values[name]
...     def __setitem__(self, name, value):
...         self.values[name] = value
...     def __delitem__(self, name):
...         del self.values[name]
...     def __iter__(self):
...         return iter(self.values)
...
>>> thing = Container()
>>> thing['one'] = 1
>>> with patch.dict(thing, one=2, two=3):
...     assert thing['one'] == 2
...     assert thing['two'] == 3
...
>>> assert thing['one'] == 1
>>> assert list(thing) == ['one']

26.5.3.4. patch.multiple

patch.multiple(target, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs)

Perform multiple patches in a single call. It takes the object to be patched (either as an object or a string to fetch the object by importing) and keyword arguments for the patches:

with patch.multiple(settings, FIRST_PATCH='one', SECOND_PATCH='two'):
    ...

Use DEFAULT as the value if you want patch.multiple() to create mocks for you. In this case the created mocks are passed into a decorated function by keyword, and a dictionary is returned when patch.multiple() is used as a context manager.

patch.multiple() can be used as a decorator, class decorator or a context manager. The arguments spec, spec_set, create, autospec and new_callable have the same meaning as for patch(). These arguments will be applied to all patches done by patch.multiple().

When used as a class decorator patch.multiple() honours patch.TEST_PREFIX for choosing which methods to wrap.

If you want patch.multiple() to create mocks for you, then you can use DEFAULT as the value. If you use patch.multiple() as a decorator then the created mocks are passed into the decorated function by keyword.

>>> thing = object()
>>> other = object()

>>> @patch.multiple('__main__', thing=DEFAULT, other=DEFAULT)
... def test_function(thing, other):
...     assert isinstance(thing, MagicMock)
...     assert isinstance(other, MagicMock)
...
>>> test_function()

patch.multiple() can be nested with other patch decorators, but put arguments passed by keyword after any of the standard arguments created by patch():

>>> @patch('sys.exit')
... @patch.multiple('__main__', thing=DEFAULT, other=DEFAULT)
... def test_function(mock_exit, other, thing):
...     assert 'other' in repr(other)
...     assert 'thing' in repr(thing)
...     assert 'exit' in repr(mock_exit)
...
>>> test_function()

If patch.multiple() is used as a context manager, the value returned by the context manger is a dictionary where created mocks are keyed by name:

>>> with patch.multiple('__main__', thing=DEFAULT, other=DEFAULT) as values:
...     assert 'other' in repr(values['other'])
...     assert 'thing' in repr(values['thing'])
...     assert values['thing'] is thing
...     assert values['other'] is other
...

26.5.3.5. patch methods: start and stop

All the patchers have start() and stop() methods. These make it simpler to do patching in setUp methods or where you want to do multiple patches without nesting decorators or with statements.

To use them call patch(), patch.object() or patch.dict() as normal and keep a reference to the returned patcher object. You can then call start() to put the patch in place and stop() to undo it.

If you are using patch() to create a mock for you then it will be returned by the call to patcher.start.

>>> patcher = patch('package.module.ClassName')
>>> from package import module
>>> original = module.ClassName
>>> new_mock = patcher.start()
>>> assert module.ClassName is not original
>>> assert module.ClassName is new_mock
>>> patcher.stop()
>>> assert module.ClassName is original
>>> assert module.ClassName is not new_mock

A typical use case for this might be for doing multiple patches in the setUp method of a TestCase:

>>> class MyTest(TestCase):
...     def setUp(self):
...         self.patcher1 = patch('package.module.Class1')
...         self.patcher2 = patch('package.module.Class2')
...         self.MockClass1 = self.patcher1.start()
...         self.MockClass2 = self.patcher2.start()
...
...     def tearDown(self):
...         self.patcher1.stop()
...         self.patcher2.stop()
...
...     def test_something(self):
...         assert package.module.Class1 is self.MockClass1
...         assert package.module.Class2 is self.MockClass2
...
>>> MyTest('test_something').run()

警告

If you use this technique you must ensure that the patching is “undone” by calling stop. This can be fiddlier than you might think, because if an exception is raised in the setUp then tearDown is not called. unittest.TestCase.addCleanup() makes this easier:

>>> class MyTest(TestCase):
...     def setUp(self):
...         patcher = patch('package.module.Class')
...         self.MockClass = patcher.start()
...         self.addCleanup(patcher.stop)
...
...     def test_something(self):
...         assert package.module.Class is self.MockClass
...

As an added bonus you no longer need to keep a reference to the patcher object.

It is also possible to stop all patches which have been started by using patch.stopall().

patch.stopall()

Stop all active patches. Only stops patches started with start.

26.5.3.6. patch builtins

You can patch any builtins within a module. The following example patches builtin ord():

>>> @patch('__main__.ord')
... def test(mock_ord):
...     mock_ord.return_value = 101
...     print(ord('c'))
...
>>> test()
101

26.5.3.7. TEST_PREFIX

All of the patchers can be used as class decorators. When used in this way they wrap every test method on the class. The patchers recognise methods that start with 'test' as being test methods. This is the same way that the unittest.TestLoader finds test methods by default.

It is possible that you want to use a different prefix for your tests. You can inform the patchers of the different prefix by setting patch.TEST_PREFIX:

>>> patch.TEST_PREFIX = 'foo'
>>> value = 3
>>>
>>> @patch('__main__.value', 'not three')
... class Thing:
...     def foo_one(self):
...         print(value)
...     def foo_two(self):
...         print(value)
...
>>>
>>> Thing().foo_one()
not three
>>> Thing().foo_two()
not three
>>> value
3

26.5.3.8. Nesting Patch Decorators

If you want to perform multiple patches then you can simply stack up the decorators.

You can stack up multiple patch decorators using this pattern:

>>> @patch.object(SomeClass, 'class_method')
... @patch.object(SomeClass, 'static_method')
... def test(mock1, mock2):
...     assert SomeClass.static_method is mock1
...     assert SomeClass.class_method is mock2
...     SomeClass.static_method('foo')
...     SomeClass.class_method('bar')
...     return mock1, mock2
...
>>> mock1, mock2 = test()
>>> mock1.assert_called_once_with('foo')
>>> mock2.assert_called_once_with('bar')

Note that the decorators are applied from the bottom upwards. This is the standard way that Python applies decorators. The order of the created mocks passed into your test function matches this order.

26.5.3.9. Where to patch

patch() works by (temporarily) changing the object that a name points to with another one. There can be many names pointing to any individual object, so for patching to work you must ensure that you patch the name used by the system under test.

The basic principle is that you patch where an object is looked up, which is not necessarily the same place as where it is defined. A couple of examples will help to clarify this.

Imagine we have a project that we want to test with the following structure:

a.py
    -> Defines SomeClass

b.py
    -> from a import SomeClass
    -> some_function instantiates SomeClass

Now we want to test some_function but we want to mock out SomeClass using patch(). The problem is that when we import module b, which we will have to do then it imports SomeClass from module a. If we use patch() to mock out a.SomeClass then it will have no effect on our test; module b already has a reference to the real SomeClass and it looks like our patching had no effect.

The key is to patch out SomeClass where it is used (or where it is looked up ). In this case some_function will actually look up SomeClass in module b, where we have imported it. The patching should look like:

@patch('b.SomeClass')

However, consider the alternative scenario where instead of from a import SomeClass module b does import a and some_function uses a.SomeClass. Both of these import forms are common. In this case the class we want to patch is being looked up in the module and so we have to patch a.SomeClass instead:

@patch('a.SomeClass')

26.5.3.10. Patching Descriptors and Proxy Objects

Both patch and patch.object correctly patch and restore descriptors: class methods, static methods and properties. You should patch these on the class rather than an instance. They also work with some objects that proxy attribute access, like the django settings object [http://www.voidspace.org.uk/python/weblog/arch_d7_2010_12_04.shtml#e1198].

26.5.4. MagicMock and magic method support

26.5.4.1. Mocking Magic Methods

Mock supports mocking the Python protocol methods, also known as “magic methods”. This allows mock objects to replace containers or other objects that implement Python protocols.

Because magic methods are looked up differently from normal methods 2, this support has been specially implemented. This means that only specific magic methods are supported. The supported list includes almost all of them. If there are any missing that you need please let us know.

You mock magic methods by setting the method you are interested in to a function or a mock instance. If you are using a function then it must take self as the first argument 3.

>>> def __str__(self):
...     return 'fooble'
...
>>> mock = Mock()
>>> mock.__str__ = __str__
>>> str(mock)
'fooble'

>>> mock = Mock()
>>> mock.__str__ = Mock()
>>> mock.__str__.return_value = 'fooble'
>>> str(mock)
'fooble'

>>> mock = Mock()
>>> mock.__iter__ = Mock(return_value=iter([]))
>>> list(mock)
[]

One use case for this is for mocking objects used as context managers in a with statement:

>>> mock = Mock()
>>> mock.__enter__ = Mock(return_value='foo')
>>> mock.__exit__ = Mock(return_value=False)
>>> with mock as m:
...     assert m == 'foo'
...
>>> mock.__enter__.assert_called_with()
>>> mock.__exit__.assert_called_with(None, None, None)

Calls to magic methods do not appear in method_calls, but they are recorded in mock_calls.

注解

If you use the spec keyword argument to create a mock then attempting to set a magic method that isn’t in the spec will raise an AttributeError.

The full list of supported magic methods is:

__hash__, __sizeof__, __repr__ and __str__

__dir__, __format__ and __subclasses__

__floor__, __trunc__ and __ceil__

Comparisons: __lt__, __gt__, __le__, __ge__, __eq__ and __ne__

Container methods: __getitem__, __setitem__, __delitem__, __contains__, __len__, __iter__, __reversed__ and __missing__

Context manager: __enter__ and __exit__

Unary numeric methods: __neg__, __pos__ and __invert__

The numeric methods (including right hand and in-place variants): __add__, __sub__, __mul__, __matmul__, __div__, __truediv__, __floordiv__, __mod__, __divmod__, __lshift__, __rshift__, __and__, __xor__, __or__, and __pow__

Numeric conversion methods: __complex__, __int__, __float__ and __index__

Descriptor methods: __get__, __set__ and __delete__

Pickling: __reduce__, __reduce_ex__, __getinitargs__, __getnewargs__, __getstate__ and __setstate__

The following methods exist but are not supported as they are either in use by mock, can’t be set dynamically, or can cause problems:

__getattr__, __setattr__, __init__ and __new__

__prepare__, __instancecheck__, __subclasscheck__, __del__

26.5.4.2. Magic Mock

There are two MagicMock variants: MagicMock and NonCallableMagicMock.

class unittest.mock.MagicMock(*args, **kw)

MagicMock is a subclass of Mock with default implementations of most of the magic methods. You can use MagicMock without having to configure the magic methods yourself.

The constructor parameters have the same meaning as for Mock.

If you use the spec or spec_set arguments then only magic methods that exist in the spec will be created.

class unittest.mock.NonCallableMagicMock(*args, **kw)

A non-callable version of MagicMock.

The constructor parameters have the same meaning as for MagicMock, with the exception of return_value and side_effect which have no meaning on a non-callable mock.

The magic methods are setup with MagicMock objects, so you can configure them and use them in the usual way:

>>> mock = MagicMock()
>>> mock[3] = 'fish'
>>> mock.__setitem__.assert_called_with(3, 'fish')
>>> mock.__getitem__.return_value = 'result'
>>> mock[2]
'result'

By default many of the protocol methods are required to return objects of a specific type. These methods are preconfigured with a default return value, so that they can be used without you having to do anything if you aren’t interested in the return value. You can still set the return value manually if you want to change the default.

Methods and their defaults:

__lt__: NotImplemented

__gt__: NotImplemented

__le__: NotImplemented

__ge__: NotImplemented

__int__: 1

__contains__: False

__len__: 0

__iter__: iter([])

__exit__: False

__complex__: 1j

__float__: 1.0

__bool__: True

__index__: 1

__hash__: default hash for the mock

__str__: default str for the mock

__sizeof__: default sizeof for the mock

例如:

>>> mock = MagicMock()
>>> int(mock)
1
>>> len(mock)
0
>>> list(mock)
[]
>>> object() in mock
False

The two equality methods, __eq__() and __ne__(), are special. They do the default equality comparison on identity, using the side_effect attribute, unless you change their return value to return something else:

>>> MagicMock() == 3
False
>>> MagicMock() != 3
True
>>> mock = MagicMock()
>>> mock.__eq__.return_value = True
>>> mock == 3
True

The return value of MagicMock.__iter__() can be any iterable object and isn’t required to be an iterator:

>>> mock = MagicMock()
>>> mock.__iter__.return_value = ['a', 'b', 'c']
>>> list(mock)
['a', 'b', 'c']
>>> list(mock)
['a', 'b', 'c']

If the return value is an iterator, then iterating over it once will consume it and subsequent iterations will result in an empty list:

>>> mock.__iter__.return_value = iter(['a', 'b', 'c'])
>>> list(mock)
['a', 'b', 'c']
>>> list(mock)
[]

MagicMock has all of the supported magic methods configured except for some of the obscure and obsolete ones. You can still set these up if you want.

Magic methods that are supported but not setup by default in MagicMock are:

__subclasses__

__dir__

__format__

__get__, __set__ and __delete__

__reversed__ and __missing__

__reduce__, __reduce_ex__, __getinitargs__, __getnewargs__, __getstate__ and __setstate__

__getformat__ and __setformat__

2

Magic methods should be looked up on the class rather than the instance. Different versions of Python are inconsistent about applying this rule. The supported protocol methods should work with all supported versions of Python.

3

The function is basically hooked up to the class, but each Mock instance is kept isolated from the others.

26.5.5. Helpers

26.5.5.1. sentinel

unittest.mock.sentinel

The sentinel object provides a convenient way of providing unique objects for your tests.

Attributes are created on demand when you access them by name. Accessing the same attribute will always return the same object. The objects returned have a sensible repr so that test failure messages are readable.

The sentinel attributes don’t preserve their identity when they are copied or pickled.

Sometimes when testing you need to test that a specific object is passed as an argument to another method, or returned. It can be common to create named sentinel objects to test this. sentinel provides a convenient way of creating and testing the identity of objects like this.

In this example we monkey patch method to return sentinel.some_object:

>>> real = ProductionClass()
>>> real.method = Mock(name="method")
>>> real.method.return_value = sentinel.some_object
>>> result = real.method()
>>> assert result is sentinel.some_object
>>> sentinel.some_object
sentinel.some_object

26.5.5.2. DEFAULT

unittest.mock.DEFAULT

The DEFAULT object is a pre-created sentinel (actually sentinel.DEFAULT). It can be used by side_effect functions to indicate that the normal return value should be used.

26.5.5.3. call

unittest.mock.call(*args, **kwargs)

call() is a helper object for making simpler assertions, for comparing with call_args, call_args_list, mock_calls and method_calls. call() can also be used with assert_has_calls().

>>> m = MagicMock(return_value=None)
>>> m(1, 2, a='foo', b='bar')
>>> m()
>>> m.call_args_list == [call(1, 2, a='foo', b='bar'), call()]
True

call.call_list()

For a call object that represents multiple calls, call_list() returns a list of all the intermediate calls as well as the final call.

call_list is particularly useful for making assertions on “chained calls”. A chained call is multiple calls on a single line of code. This results in multiple entries in mock_calls on a mock. Manually constructing the sequence of calls can be tedious.

call_list() can construct the sequence of calls from the same chained call:

>>> m = MagicMock()
>>> m(1).method(arg='foo').other('bar')(2.0)
<MagicMock name='mock().method().other()()' id='...'>
>>> kall = call(1).method(arg='foo').other('bar')(2.0)
>>> kall.call_list()
[call(1),
 call().method(arg='foo'),
 call().method().other('bar'),
 call().method().other()(2.0)]
>>> m.mock_calls == kall.call_list()
True

A call object is either a tuple of (positional args, keyword args) or (name, positional args, keyword args) depending on how it was constructed. When you construct them yourself this isn’t particularly interesting, but the call objects that are in the Mock.call_args, Mock.call_args_list and Mock.mock_calls attributes can be introspected to get at the individual arguments they contain.

The call objects in Mock.call_args and Mock.call_args_list are two-tuples of (positional args, keyword args) whereas the call objects in Mock.mock_calls, along with ones you construct yourself, are three-tuples of (name, positional args, keyword args).

You can use their “tupleness” to pull out the individual arguments for more complex introspection and assertions. The positional arguments are a tuple (an empty tuple if there are no positional arguments) and the keyword arguments are a dictionary:

>>> m = MagicMock(return_value=None)
>>> m(1, 2, 3, arg='one', arg2='two')
>>> kall = m.call_args
>>> args, kwargs = kall
>>> args
(1, 2, 3)
>>> kwargs
{'arg2': 'two', 'arg': 'one'}
>>> args is kall[0]
True
>>> kwargs is kall[1]
True

>>> m = MagicMock()
>>> m.foo(4, 5, 6, arg='two', arg2='three')
<MagicMock name='mock.foo()' id='...'>
>>> kall = m.mock_calls[0]
>>> name, args, kwargs = kall
>>> name
'foo'
>>> args
(4, 5, 6)
>>> kwargs
{'arg2': 'three', 'arg': 'two'}
>>> name is m.mock_calls[0][0]
True

26.5.5.4. create_autospec

unittest.mock.create_autospec(spec, spec_set=False, instance=False, **kwargs)

Create a mock object using another object as a spec. Attributes on the mock will use the corresponding attribute on the spec object as their spec.

Functions or methods being mocked will have their arguments checked to ensure that they are called with the correct signature.

If spec_set is True then attempting to set attributes that don’t exist on the spec object will raise an AttributeError.

If a class is used as a spec then the return value of the mock (the instance of the class) will have the same spec. You can use a class as the spec for an instance object by passing instance=True. The returned mock will only be callable if instances of the mock are callable.

create_autospec() also takes arbitrary keyword arguments that are passed to the constructor of the created mock.

See Autospeccing for examples of how to use auto-speccing with create_autospec() and the autospec argument to patch().

26.5.5.5. ANY

unittest.mock.ANY

Sometimes you may need to make assertions about some of the arguments in a call to mock, but either not care about some of the arguments or want to pull them individually out of call_args and make more complex assertions on them.

To ignore certain arguments you can pass in objects that compare equal to everything. Calls to assert_called_with() and assert_called_once_with() will then succeed no matter what was passed in.

>>> mock = Mock(return_value=None)
>>> mock('foo', bar=object())
>>> mock.assert_called_once_with('foo', bar=ANY)

ANY can also be used in comparisons with call lists like mock_calls:

>>> m = MagicMock(return_value=None)
>>> m(1)
>>> m(1, 2)
>>> m(object())
>>> m.mock_calls == [call(1), call(1, 2), ANY]
True

26.5.5.6. FILTER_DIR

unittest.mock.FILTER_DIR

FILTER_DIR is a module level variable that controls the way mock objects respond to dir() (only for Python 2.6 or more recent). The default is True, which uses the filtering described below, to only show useful members. If you dislike this filtering, or need to switch it off for diagnostic purposes, then set mock.FILTER_DIR = False.

With filtering on, dir(some_mock) shows only useful attributes and will include any dynamically created attributes that wouldn’t normally be shown. If the mock was created with a spec (or autospec of course) then all the attributes from the original are shown, even if they haven’t been accessed yet:

>>> dir(Mock())
['assert_any_call',
 'assert_called_once_with',
 'assert_called_with',
 'assert_has_calls',
 'attach_mock',
 ...
>>> from urllib import request
>>> dir(Mock(spec=request))
['AbstractBasicAuthHandler',
 'AbstractDigestAuthHandler',
 'AbstractHTTPHandler',
 'BaseHandler',
 ...

Many of the not-very-useful (private to Mock rather than the thing being mocked) underscore and double underscore prefixed attributes have been filtered from the result of calling dir() on a Mock. If you dislike this behaviour you can switch it off by setting the module level switch FILTER_DIR:

>>> from unittest import mock
>>> mock.FILTER_DIR = False
>>> dir(mock.Mock())
['_NonCallableMock__get_return_value',
 '_NonCallableMock__get_side_effect',
 '_NonCallableMock__return_value_doc',
 '_NonCallableMock__set_return_value',
 '_NonCallableMock__set_side_effect',
 '__call__',
 '__class__',
 ...

Alternatively you can just use vars(my_mock) (instance members) and dir(type(my_mock)) (type members) to bypass the filtering irrespective of mock.FILTER_DIR.

26.5.5.7. mock_open

unittest.mock.mock_open(mock=None, read_data=None)

A helper function to create a mock to replace the use of open(). It works for open() called directly or used as a context manager.

The mock argument is the mock object to configure. If None (the default) then a MagicMock will be created for you, with the API limited to methods or attributes available on standard file handles.

read_data is a string for the read(), readline(), and readlines() methods of the file handle to return. Calls to those methods will take data from read_data until it is depleted. The mock of these methods is pretty simplistic: every time the mock is called, the read_data is rewound to the start. If you need more control over the data that you are feeding to the tested code you will need to customize this mock for yourself. When that is insufficient, one of the in-memory filesystem packages on PyPI [https://pypi.org] can offer a realistic filesystem for testing.

在 3.4 版更改: Added readline() and readlines() support. The mock of read() changed to consume read_data rather than returning it on each call.

在 3.5 版更改: read_data is now reset on each call to the mock.

Using open() as a context manager is a great way to ensure your file handles are closed properly and is becoming common:

with open('/some/path', 'w') as f:
    f.write('something')

The issue is that even if you mock out the call to open() it is the returned object that is used as a context manager (and has __enter__() and __exit__() called).

Mocking context managers with a MagicMock is common enough and fiddly enough that a helper function is useful.

>>> m = mock_open()
>>> with patch('__main__.open', m):
...     with open('foo', 'w') as h:
...         h.write('some stuff')
...
>>> m.mock_calls
[call('foo', 'w'),
 call().__enter__(),
 call().write('some stuff'),
 call().__exit__(None, None, None)]
>>> m.assert_called_once_with('foo', 'w')
>>> handle = m()
>>> handle.write.assert_called_once_with('some stuff')

And for reading files:

>>> with patch('__main__.open', mock_open(read_data='bibble')) as m:
...     with open('foo') as h:
...         result = h.read()
...
>>> m.assert_called_once_with('foo')
>>> assert result == 'bibble'

26.5.5.8. Autospeccing

Autospeccing is based on the existing spec feature of mock. It limits the api of mocks to the api of an original object (the spec), but it is recursive (implemented lazily) so that attributes of mocks only have the same api as the attributes of the spec. In addition mocked functions / methods have the same call signature as the original so they raise a TypeError if they are called incorrectly.

Before I explain how auto-speccing works, here’s why it is needed.

Mock is a very powerful and flexible object, but it suffers from two flaws when used to mock out objects from a system under test. One of these flaws is specific to the Mock api and the other is a more general problem with using mock objects.

First the problem specific to Mock. Mock has two assert methods that are extremely handy: assert_called_with() and assert_called_once_with().

>>> mock = Mock(name='Thing', return_value=None)
>>> mock(1, 2, 3)
>>> mock.assert_called_once_with(1, 2, 3)
>>> mock(1, 2, 3)
>>> mock.assert_called_once_with(1, 2, 3)
Traceback (most recent call last):
 ...
AssertionError: Expected 'mock' to be called once. Called 2 times.

Because mocks auto-create attributes on demand, and allow you to call them with arbitrary arguments, if you misspell one of these assert methods then your assertion is gone:

>>> mock = Mock(name='Thing', return_value=None)
>>> mock(1, 2, 3)
>>> mock.assret_called_once_with(4, 5, 6)

Your tests can pass silently and incorrectly because of the typo.

The second issue is more general to mocking. If you refactor some of your code, rename members and so on, any tests for code that is still using the old api but uses mocks instead of the real objects will still pass. This means your tests can all pass even though your code is broken.

Note that this is another reason why you need integration tests as well as unit tests. Testing everything in isolation is all fine and dandy, but if you don’t test how your units are “wired together” there is still lots of room for bugs that tests might have caught.

mock already provides a feature to help with this, called speccing. If you use a class or instance as the spec for a mock then you can only access attributes on the mock that exist on the real class:

>>> from urllib import request
>>> mock = Mock(spec=request.Request)
>>> mock.assret_called_with
Traceback (most recent call last):
 ...
AttributeError: Mock object has no attribute 'assret_called_with'

The spec only applies to the mock itself, so we still have the same issue with any methods on the mock:

>>> mock.has_data()
<mock.Mock object at 0x...>
>>> mock.has_data.assret_called_with()

Auto-speccing solves this problem. You can either pass autospec=True to patch() / patch.object() or use the create_autospec() function to create a mock with a spec. If you use the autospec=True argument to patch() then the object that is being replaced will be used as the spec object. Because the speccing is done “lazily” (the spec is created as attributes on the mock are accessed) you can use it with very complex or deeply nested objects (like modules that import modules that import modules) without a big performance hit.

Here’s an example of it in use:

>>> from urllib import request
>>> patcher = patch('__main__.request', autospec=True)
>>> mock_request = patcher.start()
>>> request is mock_request
True
>>> mock_request.Request
<MagicMock name='request.Request' spec='Request' id='...'>

You can see that request.Request has a spec. request.Request takes two arguments in the constructor (one of which is self). Here’s what happens if we try to call it incorrectly:

>>> req = request.Request()
Traceback (most recent call last):
 ...
TypeError: <lambda>() takes at least 2 arguments (1 given)

The spec also applies to instantiated classes (i.e. the return value of specced mocks):

>>> req = request.Request('foo')
>>> req
<NonCallableMagicMock name='request.Request()' spec='Request' id='...'>

Request objects are not callable, so the return value of instantiating our mocked out request.Request is a non-callable mock. With the spec in place any typos in our asserts will raise the correct error:

>>> req.add_header('spam', 'eggs')
<MagicMock name='request.Request().add_header()' id='...'>
>>> req.add_header.assret_called_with
Traceback (most recent call last):
 ...
AttributeError: Mock object has no attribute 'assret_called_with'
>>> req.add_header.assert_called_with('spam', 'eggs')

In many cases you will just be able to add autospec=True to your existing patch() calls and then be protected against bugs due to typos and api changes.

As well as using autospec through patch() there is a create_autospec() for creating autospecced mocks directly:

>>> from urllib import request
>>> mock_request = create_autospec(request)
>>> mock_request.Request('foo', 'bar')
<NonCallableMagicMock name='mock.Request()' spec='Request' id='...'>

This isn’t without caveats and limitations however, which is why it is not the default behaviour. In order to know what attributes are available on the spec object, autospec has to introspect (access attributes) the spec. As you traverse attributes on the mock a corresponding traversal of the original object is happening under the hood. If any of your specced objects have properties or descriptors that can trigger code execution then you may not be able to use autospec. On the other hand it is much better to design your objects so that introspection is safe 4.

A more serious problem is that it is common for instance attributes to be created in the __init__() method and not to exist on the class at all. autospec can’t know about any dynamically created attributes and restricts the api to visible attributes.

>>> class Something:
...   def __init__(self):
...     self.a = 33
...
>>> with patch('__main__.Something', autospec=True):
...   thing = Something()
...   thing.a
...
Traceback (most recent call last):
  ...
AttributeError: Mock object has no attribute 'a'

There are a few different ways of resolving this problem. The easiest, but not necessarily the least annoying, way is to simply set the required attributes on the mock after creation. Just because autospec doesn’t allow you to fetch attributes that don’t exist on the spec it doesn’t prevent you setting them:

>>> with patch('__main__.Something', autospec=True):
...   thing = Something()
...   thing.a = 33
...

There is a more aggressive version of both spec and autospec that does prevent you setting non-existent attributes. This is useful if you want to ensure your code only sets valid attributes too, but obviously it prevents this particular scenario:

>>> with patch('__main__.Something', autospec=True, spec_set=True):
...   thing = Something()
...   thing.a = 33
...
Traceback (most recent call last):
 ...
AttributeError: Mock object has no attribute 'a'

Probably the best way of solving the problem is to add class attributes as default values for instance members initialised in __init__(). Note that if you are only setting default attributes in __init__() then providing them via class attributes (shared between instances of course) is faster too. e.g.

class Something:
    a = 33

This brings up another issue. It is relatively common to provide a default value of None for members that will later be an object of a different type. None would be useless as a spec because it wouldn’t let you access any attributes or methods on it. As None is never going to be useful as a spec, and probably indicates a member that will normally of some other type, autospec doesn’t use a spec for members that are set to None. These will just be ordinary mocks (well - MagicMocks):

>>> class Something:
...     member = None
...
>>> mock = create_autospec(Something)
>>> mock.member.foo.bar.baz()
<MagicMock name='mock.member.foo.bar.baz()' id='...'>

If modifying your production classes to add defaults isn’t to your liking then there are more options. One of these is simply to use an instance as the spec rather than the class. The other is to create a subclass of the production class and add the defaults to the subclass without affecting the production class. Both of these require you to use an alternative object as the spec. Thankfully patch() supports this - you can simply pass the alternative object as the autospec argument:

>>> class Something:
...   def __init__(self):
...     self.a = 33
...
>>> class SomethingForTest(Something):
...   a = 33
...
>>> p = patch('__main__.Something', autospec=SomethingForTest)
>>> mock = p.start()
>>> mock.a
<NonCallableMagicMock name='Something.a' spec='int' id='...'>

4

This only applies to classes or already instantiated objects. Calling a mocked class to create a mock instance does not create a real instance. It is only attribute lookups - along with calls to dir() - that are done.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

26.6. unittest.mock 上手指南

3.3 新版功能.

26.6.1. 使用 mock

26.6.1.1. 模拟方法调用

使用 Mock 的常见场景：

模拟函数调用

记录“对象上的方法调用”

你可能需要替换一个对象上的方法，用于确认此方法被系统中的其他部分调用过，并且调用时使用了正确的参数。

>>> real = SomeClass()
>>> real.method = MagicMock(name='method')
>>> real.method(3, 4, 5, key='value')
<MagicMock name='method()' id='...'>

使用了 mock（本例中的 real.method）之后，它有方法和属性可以让你针对它是被如何使用的下断言。

注解

在多数示例中，Mock 与 MagicMock 两个类可以相互替换，而 MagicMock 是一个更适用的类，通常情况下，使用它就可以了。

如果 mock 被调用，它的 called 属性就会变成 True，更重要的是，我们可以使用 assert_called_with() 或者 assert_called_once_with() 方法来确认它在被调用时使用了正确的参数。

在如下的测试示例中，验证对于 ProductionClass().method 的调用会导致 something 的调用。

>>> class ProductionClass:
...     def method(self):
...         self.something(1, 2, 3)
...     def something(self, a, b, c):
...         pass
...
>>> real = ProductionClass()
>>> real.something = MagicMock()
>>> real.method()
>>> real.something.assert_called_once_with(1, 2, 3)

26.6.1.2. 对象上的方法调用的 mock

In the last example we patched a method directly on an object to check that it was called correctly. Another common use case is to pass an object into a method (or some part of the system under test) and then check that it is used in the correct way.

The simple ProductionClass below has a closer method. If it is called with an object then it calls close on it.

>>> class ProductionClass:
...     def closer(self, something):
...         something.close()
...

So to test it we need to pass in an object with a close method and check that it was called correctly.

>>> real = ProductionClass()
>>> mock = Mock()
>>> real.closer(mock)
>>> mock.close.assert_called_with()

We don’t have to do any work to provide the ‘close’ method on our mock. Accessing close creates it. So, if ‘close’ hasn’t already been called then accessing it in the test will create it, but assert_called_with() will raise a failure exception.

26.6.1.3. Mocking Classes

A common use case is to mock out classes instantiated by your code under test. When you patch a class, then that class is replaced with a mock. Instances are created by calling the class. This means you access the “mock instance” by looking at the return value of the mocked class.

In the example below we have a function some_function that instantiates Foo and calls a method on it. The call to patch() replaces the class Foo with a mock. The Foo instance is the result of calling the mock, so it is configured by modifying the mock return_value.

>>> def some_function():
...     instance = module.Foo()
...     return instance.method()
...
>>> with patch('module.Foo') as mock:
...     instance = mock.return_value
...     instance.method.return_value = 'the result'
...     result = some_function()
...     assert result == 'the result'

26.6.1.4. Naming your mocks

It can be useful to give your mocks a name. The name is shown in the repr of the mock and can be helpful when the mock appears in test failure messages. The name is also propagated to attributes or methods of the mock:

>>> mock = MagicMock(name='foo')
>>> mock
<MagicMock name='foo' id='...'>
>>> mock.method
<MagicMock name='foo.method' id='...'>

26.6.1.5. Tracking all Calls

Often you want to track more than a single call to a method. The mock_calls attribute records all calls to child attributes of the mock - and also to their children.

>>> mock = MagicMock()
>>> mock.method()
<MagicMock name='mock.method()' id='...'>
>>> mock.attribute.method(10, x=53)
<MagicMock name='mock.attribute.method()' id='...'>
>>> mock.mock_calls
[call.method(), call.attribute.method(10, x=53)]

If you make an assertion about mock_calls and any unexpected methods have been called, then the assertion will fail. This is useful because as well as asserting that the calls you expected have been made, you are also checking that they were made in the right order and with no additional calls:

You use the call object to construct lists for comparing with mock_calls:

>>> expected = [call.method(), call.attribute.method(10, x=53)]
>>> mock.mock_calls == expected
True

However, parameters to calls that return mocks are not recorded, which means it is not possible to track nested calls where the parameters used to create ancestors are important:

>>> m = Mock()
>>> m.factory(important=True).deliver()
<Mock name='mock.factory().deliver()' id='...'>
>>> m.mock_calls[-1] == call.factory(important=False).deliver()
True

26.6.1.6. Setting Return Values and Attributes

Setting the return values on a mock object is trivially easy:

>>> mock = Mock()
>>> mock.return_value = 3
>>> mock()
3

Of course you can do the same for methods on the mock:

>>> mock = Mock()
>>> mock.method.return_value = 3
>>> mock.method()
3

The return value can also be set in the constructor:

>>> mock = Mock(return_value=3)
>>> mock()
3

If you need an attribute setting on your mock, just do it:

>>> mock = Mock()
>>> mock.x = 3
>>> mock.x
3

Sometimes you want to mock up a more complex situation, like for example mock.connection.cursor().execute("SELECT 1"). If we wanted this call to return a list, then we have to configure the result of the nested call.

We can use call to construct the set of calls in a “chained call” like this for easy assertion afterwards:

>>> mock = Mock()
>>> cursor = mock.connection.cursor.return_value
>>> cursor.execute.return_value = ['foo']
>>> mock.connection.cursor().execute("SELECT 1")
['foo']
>>> expected = call.connection.cursor().execute("SELECT 1").call_list()
>>> mock.mock_calls
[call.connection.cursor(), call.connection.cursor().execute('SELECT 1')]
>>> mock.mock_calls == expected
True

It is the call to .call_list() that turns our call object into a list of calls representing the chained calls.

26.6.1.7. Raising exceptions with mocks

A useful attribute is side_effect. If you set this to an exception class or instance then the exception will be raised when the mock is called.

>>> mock = Mock(side_effect=Exception('Boom!'))
>>> mock()
Traceback (most recent call last):
  ...
Exception: Boom!

26.6.1.8. Side effect functions and iterables

side_effect can also be set to a function or an iterable. The use case for side_effect as an iterable is where your mock is going to be called several times, and you want each call to return a different value. When you set side_effect to an iterable every call to the mock returns the next value from the iterable:

>>> mock = MagicMock(side_effect=[4, 5, 6])
>>> mock()
4
>>> mock()
5
>>> mock()
6

For more advanced use cases, like dynamically varying the return values depending on what the mock is called with, side_effect can be a function. The function will be called with the same arguments as the mock. Whatever the function returns is what the call returns:

>>> vals = {(1, 2): 1, (2, 3): 2}
>>> def side_effect(*args):
...     return vals[args]
...
>>> mock = MagicMock(side_effect=side_effect)
>>> mock(1, 2)
1
>>> mock(2, 3)
2

26.6.1.9. Creating a Mock from an Existing Object

One problem with over use of mocking is that it couples your tests to the implementation of your mocks rather than your real code. Suppose you have a class that implements some_method. In a test for another class, you provide a mock of this object that also provides some_method. If later you refactor the first class, so that it no longer has some_method - then your tests will continue to pass even though your code is now broken!

Mock allows you to provide an object as a specification for the mock, using the spec keyword argument. Accessing methods / attributes on the mock that don’t exist on your specification object will immediately raise an attribute error. If you change the implementation of your specification, then tests that use that class will start failing immediately without you having to instantiate the class in those tests.

>>> mock = Mock(spec=SomeClass)
>>> mock.old_method()
Traceback (most recent call last):
   ...
AttributeError: object has no attribute 'old_method'

Using a specification also enables a smarter matching of calls made to the mock, regardless of whether some parameters were passed as positional or named arguments:

>>> def f(a, b, c): pass
...
>>> mock = Mock(spec=f)
>>> mock(1, 2, 3)
<Mock name='mock()' id='140161580456576'>
>>> mock.assert_called_with(a=1, b=2, c=3)

If you want this smarter matching to also work with method calls on the mock, you can use auto-speccing.

If you want a stronger form of specification that prevents the setting of arbitrary attributes as well as the getting of them then you can use spec_set instead of spec.

26.6.2. Patch Decorators

注解

在查找对象的名称空间中修补对象使用 patch() 。使用起来很简单，阅读 在哪里打补丁 来快速上手。

A common need in tests is to patch a class attribute or a module attribute, for example patching a builtin or patching a class in a module to test that it is instantiated. Modules and classes are effectively global, so patching on them has to be undone after the test or the patch will persist into other tests and cause hard to diagnose problems.

mock provides three convenient decorators for this: patch(), patch.object() and patch.dict(). patch takes a single string, of the form package.module.Class.attribute to specify the attribute you are patching. It also optionally takes a value that you want the attribute (or class or whatever) to be replaced with. ‘patch.object’ takes an object and the name of the attribute you would like patched, plus optionally the value to patch it with.

patch.object:

>>> original = SomeClass.attribute
>>> @patch.object(SomeClass, 'attribute', sentinel.attribute)
... def test():
...     assert SomeClass.attribute == sentinel.attribute
...
>>> test()
>>> assert SomeClass.attribute == original

>>> @patch('package.module.attribute', sentinel.attribute)
... def test():
...     from package.module import attribute
...     assert attribute is sentinel.attribute
...
>>> test()

If you are patching a module (including builtins) then use patch() instead of patch.object():

>>> mock = MagicMock(return_value=sentinel.file_handle)
>>> with patch('builtins.open', mock):
...     handle = open('filename', 'r')
...
>>> mock.assert_called_with('filename', 'r')
>>> assert handle == sentinel.file_handle, "incorrect file handle returned"

The module name can be ‘dotted’, in the form package.module if needed:

>>> @patch('package.module.ClassName.attribute', sentinel.attribute)
... def test():
...     from package.module import ClassName
...     assert ClassName.attribute == sentinel.attribute
...
>>> test()

A nice pattern is to actually decorate test methods themselves:

>>> class MyTest(unittest.TestCase):
...     @patch.object(SomeClass, 'attribute', sentinel.attribute)
...     def test_something(self):
...         self.assertEqual(SomeClass.attribute, sentinel.attribute)
...
>>> original = SomeClass.attribute
>>> MyTest('test_something').test_something()
>>> assert SomeClass.attribute == original

If you want to patch with a Mock, you can use patch() with only one argument (or patch.object() with two arguments). The mock will be created for you and passed into the test function / method:

>>> class MyTest(unittest.TestCase):
...     @patch.object(SomeClass, 'static_method')
...     def test_something(self, mock_method):
...         SomeClass.static_method()
...         mock_method.assert_called_with()
...
>>> MyTest('test_something').test_something()

You can stack up multiple patch decorators using this pattern:

>>> class MyTest(unittest.TestCase):
...     @patch('package.module.ClassName1')
...     @patch('package.module.ClassName2')
...     def test_something(self, MockClass2, MockClass1):
...         self.assertIs(package.module.ClassName1, MockClass1)
...         self.assertIs(package.module.ClassName2, MockClass2)
...
>>> MyTest('test_something').test_something()

When you nest patch decorators the mocks are passed in to the decorated function in the same order they applied (the normal python order that decorators are applied). This means from the bottom up, so in the example above the mock for test_module.ClassName2 is passed in first.

还有一个 patch.dict() 用于在一定范围内设置字典中的值，并在测试结束时将字典恢复为其原始状态：

>>> foo = {'key': 'value'}
>>> original = foo.copy()
>>> with patch.dict(foo, {'newkey': 'newvalue'}, clear=True):
...     assert foo == {'newkey': 'newvalue'}
...
>>> assert foo == original

patch, patch.object and patch.dict can all be used as context managers.

Where you use patch() to create a mock for you, you can get a reference to the mock using the “as” form of the with statement:

>>> class ProductionClass:
...     def method(self):
...         pass
...
>>> with patch.object(ProductionClass, 'method') as mock_method:
...     mock_method.return_value = None
...     real = ProductionClass()
...     real.method(1, 2, 3)
...
>>> mock_method.assert_called_with(1, 2, 3)

As an alternative patch, patch.object and patch.dict can be used as class decorators. When used in this way it is the same as applying the decorator individually to every method whose name starts with “test”.

26.6.3. Further Examples

Here are some more examples for some slightly more advanced scenarios.

26.6.3.1. Mocking chained calls

Mocking chained calls is actually straightforward with mock once you understand the return_value attribute. When a mock is called for the first time, or you fetch its return_value before it has been called, a new Mock is created.

This means that you can see how the object returned from a call to a mocked object has been used by interrogating the return_value mock:

>>> mock = Mock()
>>> mock().foo(a=2, b=3)
<Mock name='mock().foo()' id='...'>
>>> mock.return_value.foo.assert_called_with(a=2, b=3)

From here it is a simple step to configure and then make assertions about chained calls. Of course another alternative is writing your code in a more testable way in the first place…

So, suppose we have some code that looks a little bit like this:

>>> class Something:
...     def __init__(self):
...         self.backend = BackendProvider()
...     def method(self):
...         response = self.backend.get_endpoint('foobar').create_call('spam', 'eggs').start_call()
...         # more code

Assuming that BackendProvider is already well tested, how do we test method()? Specifically, we want to test that the code section # more code uses the response object in the correct way.

As this chain of calls is made from an instance attribute we can monkey patch the backend attribute on a Something instance. In this particular case we are only interested in the return value from the final call to start_call so we don’t have much configuration to do. Let’s assume the object it returns is ‘file-like’, so we’ll ensure that our response object uses the builtin open() as its spec.

To do this we create a mock instance as our mock backend and create a mock response object for it. To set the response as the return value for that final start_call we could do this:

mock_backend.get_endpoint.return_value.create_call.return_value.start_call.return_value = mock_response

We can do that in a slightly nicer way using the configure_mock() method to directly set the return value for us:

>>> something = Something()
>>> mock_response = Mock(spec=open)
>>> mock_backend = Mock()
>>> config = {'get_endpoint.return_value.create_call.return_value.start_call.return_value': mock_response}
>>> mock_backend.configure_mock(**config)

With these we monkey patch the “mock backend” in place and can make the real call:

>>> something.backend = mock_backend
>>> something.method()

Using mock_calls we can check the chained call with a single assert. A chained call is several calls in one line of code, so there will be several entries in mock_calls. We can use call.call_list() to create this list of calls for us:

>>> chained = call.get_endpoint('foobar').create_call('spam', 'eggs').start_call()
>>> call_list = chained.call_list()
>>> assert mock_backend.mock_calls == call_list

26.6.3.2. Partial mocking

In some tests I wanted to mock out a call to datetime.date.today() to return a known date, but I didn’t want to prevent the code under test from creating new date objects. Unfortunately datetime.date is written in C, and so I couldn’t just monkey-patch out the static date.today() method.

I found a simple way of doing this that involved effectively wrapping the date class with a mock, but passing through calls to the constructor to the real class (and returning real instances).

The patch decorator is used here to mock out the date class in the module under test. The side_effect attribute on the mock date class is then set to a lambda function that returns a real date. When the mock date class is called a real date will be constructed and returned by side_effect.

>>> from datetime import date
>>> with patch('mymodule.date') as mock_date:
...     mock_date.today.return_value = date(2010, 10, 8)
...     mock_date.side_effect = lambda *args, **kw: date(*args, **kw)
...
...     assert mymodule.date.today() == date(2010, 10, 8)
...     assert mymodule.date(2009, 6, 8) == date(2009, 6, 8)
...

Note that we don’t patch datetime.date globally, we patch date in the module that uses it. See where to patch.

When date.today() is called a known date is returned, but calls to the date(...) constructor still return normal dates. Without this you can find yourself having to calculate an expected result using exactly the same algorithm as the code under test, which is a classic testing anti-pattern.

Calls to the date constructor are recorded in the mock_date attributes (call_count and friends) which may also be useful for your tests.

An alternative way of dealing with mocking dates, or other builtin classes, is discussed in this blog entry [https://williambert.online/2011/07/how-to-unit-testing-in-django-with-mocking-and-patching/].

26.6.3.3. Mocking a Generator Method

A Python generator is a function or method that uses the yield statement to return a series of values when iterated over 1.

A generator method / function is called to return the generator object. It is the generator object that is then iterated over. The protocol method for iteration is __iter__(), so we can mock this using a MagicMock.

Here’s an example class with an “iter” method implemented as a generator:

>>> class Foo:
...     def iter(self):
...         for i in [1, 2, 3]:
...             yield i
...
>>> foo = Foo()
>>> list(foo.iter())
[1, 2, 3]

How would we mock this class, and in particular its “iter” method?

To configure the values returned from the iteration (implicit in the call to list), we need to configure the object returned by the call to foo.iter().

>>> mock_foo = MagicMock()
>>> mock_foo.iter.return_value = iter([1, 2, 3])
>>> list(mock_foo.iter())
[1, 2, 3]

1

There are also generator expressions and more advanced uses [http://www.dabeaz.com/coroutines/index.html] of generators, but we aren’t concerned about them here. A very good introduction to generators and how powerful they are is: Generator Tricks for Systems Programmers [http://www.dabeaz.com/generators/].

26.6.3.4. Applying the same patch to every test method

If you want several patches in place for multiple test methods the obvious way is to apply the patch decorators to every method. This can feel like unnecessary repetition. For Python 2.6 or more recent you can use patch() (in all its various forms) as a class decorator. This applies the patches to all test methods on the class. A test method is identified by methods whose names start with test:

>>> @patch('mymodule.SomeClass')
... class MyTest(TestCase):
...
...     def test_one(self, MockSomeClass):
...         self.assertIs(mymodule.SomeClass, MockSomeClass)
...
...     def test_two(self, MockSomeClass):
...         self.assertIs(mymodule.SomeClass, MockSomeClass)
...
...     def not_a_test(self):
...         return 'something'
...
>>> MyTest('test_one').test_one()
>>> MyTest('test_two').test_two()
>>> MyTest('test_two').not_a_test()
'something'

An alternative way of managing patches is to use the patch methods: start and stop. These allow you to move the patching into your setUp and tearDown methods.

>>> class MyTest(TestCase):
...     def setUp(self):
...         self.patcher = patch('mymodule.foo')
...         self.mock_foo = self.patcher.start()
...
...     def test_foo(self):
...         self.assertIs(mymodule.foo, self.mock_foo)
...
...     def tearDown(self):
...         self.patcher.stop()
...
>>> MyTest('test_foo').run()

If you use this technique you must ensure that the patching is “undone” by calling stop. This can be fiddlier than you might think, because if an exception is raised in the setUp then tearDown is not called. unittest.TestCase.addCleanup() makes this easier:

>>> class MyTest(TestCase):
...     def setUp(self):
...         patcher = patch('mymodule.foo')
...         self.addCleanup(patcher.stop)
...         self.mock_foo = patcher.start()
...
...     def test_foo(self):
...         self.assertIs(mymodule.foo, self.mock_foo)
...
>>> MyTest('test_foo').run()

26.6.3.5. Mocking Unbound Methods

Whilst writing tests today I needed to patch an unbound method (patching the method on the class rather than on the instance). I needed self to be passed in as the first argument because I want to make asserts about which objects were calling this particular method. The issue is that you can’t patch with a mock for this, because if you replace an unbound method with a mock it doesn’t become a bound method when fetched from the instance, and so it doesn’t get self passed in. The workaround is to patch the unbound method with a real function instead. The patch() decorator makes it so simple to patch out methods with a mock that having to create a real function becomes a nuisance.

If you pass autospec=True to patch then it does the patching with a real function object. This function object has the same signature as the one it is replacing, but delegates to a mock under the hood. You still get your mock auto-created in exactly the same way as before. What it means though, is that if you use it to patch out an unbound method on a class the mocked function will be turned into a bound method if it is fetched from an instance. It will have self passed in as the first argument, which is exactly what I wanted:

>>> class Foo:
...   def foo(self):
...     pass
...
>>> with patch.object(Foo, 'foo', autospec=True) as mock_foo:
...   mock_foo.return_value = 'foo'
...   foo = Foo()
...   foo.foo()
...
'foo'
>>> mock_foo.assert_called_once_with(foo)

If we don’t use autospec=True then the unbound method is patched out with a Mock instance instead, and isn’t called with self.

26.6.3.6. Checking multiple calls with mock

mock has a nice API for making assertions about how your mock objects are used.

>>> mock = Mock()
>>> mock.foo_bar.return_value = None
>>> mock.foo_bar('baz', spam='eggs')
>>> mock.foo_bar.assert_called_with('baz', spam='eggs')

If your mock is only being called once you can use the assert_called_once_with() method that also asserts that the call_count is one.

>>> mock.foo_bar.assert_called_once_with('baz', spam='eggs')
>>> mock.foo_bar()
>>> mock.foo_bar.assert_called_once_with('baz', spam='eggs')
Traceback (most recent call last):
    ...
AssertionError: Expected to be called once. Called 2 times.

Both assert_called_with and assert_called_once_with make assertions about the most recent call. If your mock is going to be called several times, and you want to make assertions about all those calls you can use call_args_list:

>>> mock = Mock(return_value=None)
>>> mock(1, 2, 3)
>>> mock(4, 5, 6)
>>> mock()
>>> mock.call_args_list
[call(1, 2, 3), call(4, 5, 6), call()]

The call helper makes it easy to make assertions about these calls. You can build up a list of expected calls and compare it to call_args_list. This looks remarkably similar to the repr of the call_args_list:

>>> expected = [call(1, 2, 3), call(4, 5, 6), call()]
>>> mock.call_args_list == expected
True

26.6.3.7. Coping with mutable arguments

Another situation is rare, but can bite you, is when your mock is called with mutable arguments. call_args and call_args_list store references to the arguments. If the arguments are mutated by the code under test then you can no longer make assertions about what the values were when the mock was called.

Here’s some example code that shows the problem. Imagine the following functions defined in ‘mymodule’:

def frob(val):
    pass

def grob(val):
    "First frob and then clear val"
    frob(val)
    val.clear()

When we try to test that grob calls frob with the correct argument look what happens:

>>> with patch('mymodule.frob') as mock_frob:
...     val = {6}
...     mymodule.grob(val)
...
>>> val
set()
>>> mock_frob.assert_called_with({6})
Traceback (most recent call last):
    ...
AssertionError: Expected: (({6},), {})
Called with: ((set(),), {})

One possibility would be for mock to copy the arguments you pass in. This could then cause problems if you do assertions that rely on object identity for equality.

Here’s one solution that uses the side_effect functionality. If you provide a side_effect function for a mock then side_effect will be called with the same args as the mock. This gives us an opportunity to copy the arguments and store them for later assertions. In this example I’m using another mock to store the arguments so that I can use the mock methods for doing the assertion. Again a helper function sets this up for me.

>>> from copy import deepcopy
>>> from unittest.mock import Mock, patch, DEFAULT
>>> def copy_call_args(mock):
...     new_mock = Mock()
...     def side_effect(*args, **kwargs):
...         args = deepcopy(args)
...         kwargs = deepcopy(kwargs)
...         new_mock(*args, **kwargs)
...         return DEFAULT
...     mock.side_effect = side_effect
...     return new_mock
...
>>> with patch('mymodule.frob') as mock_frob:
...     new_mock = copy_call_args(mock_frob)
...     val = {6}
...     mymodule.grob(val)
...
>>> new_mock.assert_called_with({6})
>>> new_mock.call_args
call({6})

copy_call_args is called with the mock that will be called. It returns a new mock that we do the assertion on. The side_effect function makes a copy of the args and calls our new_mock with the copy.

注解

If your mock is only going to be used once there is an easier way of checking arguments at the point they are called. You can simply do the checking inside a side_effect function.

>>> def side_effect(arg):
...     assert arg == {6}
...
>>> mock = Mock(side_effect=side_effect)
>>> mock({6})
>>> mock(set())
Traceback (most recent call last):
    ...
AssertionError

An alternative approach is to create a subclass of Mock or MagicMock that copies (using copy.deepcopy()) the arguments. Here’s an example implementation:

>>> from copy import deepcopy
>>> class CopyingMock(MagicMock):
...     def __call__(self, *args, **kwargs):
...         args = deepcopy(args)
...         kwargs = deepcopy(kwargs)
...         return super(CopyingMock, self).__call__(*args, **kwargs)
...
>>> c = CopyingMock(return_value=None)
>>> arg = set()
>>> c(arg)
>>> arg.add(1)
>>> c.assert_called_with(set())
>>> c.assert_called_with(arg)
Traceback (most recent call last):
    ...
AssertionError: Expected call: mock({1})
Actual call: mock(set())
>>> c.foo
<CopyingMock name='mock.foo' id='...'>

When you subclass Mock or MagicMock all dynamically created attributes, and the return_value will use your subclass automatically. That means all children of a CopyingMock will also have the type CopyingMock.

26.6.3.8. Nesting Patches

Using patch as a context manager is nice, but if you do multiple patches you can end up with nested with statements indenting further and further to the right:

>>> class MyTest(TestCase):
...
...     def test_foo(self):
...         with patch('mymodule.Foo') as mock_foo:
...             with patch('mymodule.Bar') as mock_bar:
...                 with patch('mymodule.Spam') as mock_spam:
...                     assert mymodule.Foo is mock_foo
...                     assert mymodule.Bar is mock_bar
...                     assert mymodule.Spam is mock_spam
...
>>> original = mymodule.Foo
>>> MyTest('test_foo').test_foo()
>>> assert mymodule.Foo is original

With unittest cleanup functions and the patch methods: start and stop we can achieve the same effect without the nested indentation. A simple helper method, create_patch, puts the patch in place and returns the created mock for us:

>>> class MyTest(TestCase):
...
...     def create_patch(self, name):
...         patcher = patch(name)
...         thing = patcher.start()
...         self.addCleanup(patcher.stop)
...         return thing
...
...     def test_foo(self):
...         mock_foo = self.create_patch('mymodule.Foo')
...         mock_bar = self.create_patch('mymodule.Bar')
...         mock_spam = self.create_patch('mymodule.Spam')
...
...         assert mymodule.Foo is mock_foo
...         assert mymodule.Bar is mock_bar
...         assert mymodule.Spam is mock_spam
...
>>> original = mymodule.Foo
>>> MyTest('test_foo').run()
>>> assert mymodule.Foo is original

26.6.3.9. Mocking a dictionary with MagicMock

You may want to mock a dictionary, or other container object, recording all access to it whilst having it still behave like a dictionary.

We can do this with MagicMock, which will behave like a dictionary, and using side_effect to delegate dictionary access to a real underlying dictionary that is under our control.

When the __getitem__() and __setitem__() methods of our MagicMock are called (normal dictionary access) then side_effect is called with the key (and in the case of __setitem__ the value too). We can also control what is returned.

After the MagicMock has been used we can use attributes like call_args_list to assert about how the dictionary was used:

>>> my_dict = {'a': 1, 'b': 2, 'c': 3}
>>> def getitem(name):
...      return my_dict[name]
...
>>> def setitem(name, val):
...     my_dict[name] = val
...
>>> mock = MagicMock()
>>> mock.__getitem__.side_effect = getitem
>>> mock.__setitem__.side_effect = setitem

注解

An alternative to using MagicMock is to use Mock and only provide the magic methods you specifically want:

>>> mock = Mock()
>>> mock.__getitem__ = Mock(side_effect=getitem)
>>> mock.__setitem__ = Mock(side_effect=setitem)

A third option is to use MagicMock but passing in dict as the spec (or spec_set) argument so that the MagicMock created only has dictionary magic methods available:

>>> mock = MagicMock(spec_set=dict)
>>> mock.__getitem__.side_effect = getitem
>>> mock.__setitem__.side_effect = setitem

With these side effect functions in place, the mock will behave like a normal dictionary but recording the access. It even raises a KeyError if you try to access a key that doesn’t exist.

>>> mock['a']
1
>>> mock['c']
3
>>> mock['d']
Traceback (most recent call last):
    ...
KeyError: 'd'
>>> mock['b'] = 'fish'
>>> mock['d'] = 'eggs'
>>> mock['b']
'fish'
>>> mock['d']
'eggs'

After it has been used you can make assertions about the access using the normal mock methods and attributes:

>>> mock.__getitem__.call_args_list
[call('a'), call('c'), call('d'), call('b'), call('d')]
>>> mock.__setitem__.call_args_list
[call('b', 'fish'), call('d', 'eggs')]
>>> my_dict
{'a': 1, 'c': 3, 'b': 'fish', 'd': 'eggs'}

26.6.3.10. Mock subclasses and their attributes

There are various reasons why you might want to subclass Mock. One reason might be to add helper methods. Here’s a silly example:

>>> class MyMock(MagicMock):
...     def has_been_called(self):
...         return self.called
...
>>> mymock = MyMock(return_value=None)
>>> mymock
<MyMock id='...'>
>>> mymock.has_been_called()
False
>>> mymock()
>>> mymock.has_been_called()
True

The standard behaviour for Mock instances is that attributes and the return value mocks are of the same type as the mock they are accessed on. This ensures that Mock attributes are Mocks and MagicMock attributes are MagicMocks 2. So if you’re subclassing to add helper methods then they’ll also be available on the attributes and return value mock of instances of your subclass.

>>> mymock.foo
<MyMock name='mock.foo' id='...'>
>>> mymock.foo.has_been_called()
False
>>> mymock.foo()
<MyMock name='mock.foo()' id='...'>
>>> mymock.foo.has_been_called()
True

Sometimes this is inconvenient. For example, one user [https://code.google.com/p/mock/issues/detail?id=105] is subclassing mock to created a Twisted adaptor [https://twistedmatrix.com/documents/11.0.0/api/twisted.python.components.html]. Having this applied to attributes too actually causes errors.

Mock (in all its flavours) uses a method called _get_child_mock to create these “sub-mocks” for attributes and return values. You can prevent your subclass being used for attributes by overriding this method. The signature is that it takes arbitrary keyword arguments (**kwargs) which are then passed onto the mock constructor:

>>> class Subclass(MagicMock):
...     def _get_child_mock(self, **kwargs):
...         return MagicMock(**kwargs)
...
>>> mymock = Subclass()
>>> mymock.foo
<MagicMock name='mock.foo' id='...'>
>>> assert isinstance(mymock, Subclass)
>>> assert not isinstance(mymock.foo, Subclass)
>>> assert not isinstance(mymock(), Subclass)

2

An exception to this rule are the non-callable mocks. Attributes use the callable variant because otherwise non-callable mocks couldn’t have callable methods.

26.6.3.11. Mocking imports with patch.dict

One situation where mocking can be hard is where you have a local import inside a function. These are harder to mock because they aren’t using an object from the module namespace that we can patch out.

Generally local imports are to be avoided. They are sometimes done to prevent circular dependencies, for which there is usually a much better way to solve the problem (refactor the code) or to prevent “up front costs” by delaying the import. This can also be solved in better ways than an unconditional local import (store the module as a class or module attribute and only do the import on first use).

That aside there is a way to use mock to affect the results of an import. Importing fetches an object from the sys.modules dictionary. Note that it fetches an object, which need not be a module. Importing a module for the first time results in a module object being put in sys.modules, so usually when you import something you get a module back. This need not be the case however.

This means you can use patch.dict() to temporarily put a mock in place in sys.modules. Any imports whilst this patch is active will fetch the mock. When the patch is complete (the decorated function exits, the with statement body is complete or patcher.stop() is called) then whatever was there previously will be restored safely.

Here’s an example that mocks out the ‘fooble’ module.

>>> mock = Mock()
>>> with patch.dict('sys.modules', {'fooble': mock}):
...    import fooble
...    fooble.blob()
...
<Mock name='mock.blob()' id='...'>
>>> assert 'fooble' not in sys.modules
>>> mock.blob.assert_called_once_with()

As you can see the import fooble succeeds, but on exit there is no ‘fooble’ left in sys.modules.

This also works for the from module import name form:

>>> mock = Mock()
>>> with patch.dict('sys.modules', {'fooble': mock}):
...    from fooble import blob
...    blob.blip()
...
<Mock name='mock.blob.blip()' id='...'>
>>> mock.blob.blip.assert_called_once_with()

With slightly more work you can also mock package imports:

>>> mock = Mock()
>>> modules = {'package': mock, 'package.module': mock.module}
>>> with patch.dict('sys.modules', modules):
...    from package.module import fooble
...    fooble()
...
<Mock name='mock.module.fooble()' id='...'>
>>> mock.module.fooble.assert_called_once_with()

26.6.3.12. Tracking order of calls and less verbose call assertions

The Mock class allows you to track the order of method calls on your mock objects through the method_calls attribute. This doesn’t allow you to track the order of calls between separate mock objects, however we can use mock_calls to achieve the same effect.

Because mocks track calls to child mocks in mock_calls, and accessing an arbitrary attribute of a mock creates a child mock, we can create our separate mocks from a parent one. Calls to those child mock will then all be recorded, in order, in the mock_calls of the parent:

>>> manager = Mock()
>>> mock_foo = manager.foo
>>> mock_bar = manager.bar

>>> mock_foo.something()
<Mock name='mock.foo.something()' id='...'>
>>> mock_bar.other.thing()
<Mock name='mock.bar.other.thing()' id='...'>

>>> manager.mock_calls
[call.foo.something(), call.bar.other.thing()]

We can then assert about the calls, including the order, by comparing with the mock_calls attribute on the manager mock:

>>> expected_calls = [call.foo.something(), call.bar.other.thing()]
>>> manager.mock_calls == expected_calls
True

If patch is creating, and putting in place, your mocks then you can attach them to a manager mock using the attach_mock() method. After attaching calls will be recorded in mock_calls of the manager.

>>> manager = MagicMock()
>>> with patch('mymodule.Class1') as MockClass1:
...     with patch('mymodule.Class2') as MockClass2:
...         manager.attach_mock(MockClass1, 'MockClass1')
...         manager.attach_mock(MockClass2, 'MockClass2')
...         MockClass1().foo()
...         MockClass2().bar()
...
<MagicMock name='mock.MockClass1().foo()' id='...'>
<MagicMock name='mock.MockClass2().bar()' id='...'>
>>> manager.mock_calls
[call.MockClass1(),
 call.MockClass1().foo(),
 call.MockClass2(),
 call.MockClass2().bar()]

If many calls have been made, but you’re only interested in a particular sequence of them then an alternative is to use the assert_has_calls() method. This takes a list of calls (constructed with the call object). If that sequence of calls are in mock_calls then the assert succeeds.

>>> m = MagicMock()
>>> m().foo().bar().baz()
<MagicMock name='mock().foo().bar().baz()' id='...'>
>>> m.one().two().three()
<MagicMock name='mock.one().two().three()' id='...'>
>>> calls = call.one().two().three().call_list()
>>> m.assert_has_calls(calls)

Even though the chained call m.one().two().three() aren’t the only calls that have been made to the mock, the assert still succeeds.

Sometimes a mock may have several calls made to it, and you are only interested in asserting about some of those calls. You may not even care about the order. In this case you can pass any_order=True to assert_has_calls:

>>> m = MagicMock()
>>> m(1), m.two(2, 3), m.seven(7), m.fifty('50')
(...)
>>> calls = [call.fifty('50'), call(1), call.seven(7)]
>>> m.assert_has_calls(calls, any_order=True)

26.6.3.13. More complex argument matching

Using the same basic concept as ANY we can implement matchers to do more complex assertions on objects used as arguments to mocks.

Suppose we expect some object to be passed to a mock that by default compares equal based on object identity (which is the Python default for user defined classes). To use assert_called_with() we would need to pass in the exact same object. If we are only interested in some of the attributes of this object then we can create a matcher that will check these attributes for us.

You can see in this example how a ‘standard’ call to assert_called_with isn’t sufficient:

>>> class Foo:
...     def __init__(self, a, b):
...         self.a, self.b = a, b
...
>>> mock = Mock(return_value=None)
>>> mock(Foo(1, 2))
>>> mock.assert_called_with(Foo(1, 2))
Traceback (most recent call last):
    ...
AssertionError: Expected: call(<__main__.Foo object at 0x...>)
Actual call: call(<__main__.Foo object at 0x...>)

A comparison function for our Foo class might look something like this:

>>> def compare(self, other):
...     if not type(self) == type(other):
...         return False
...     if self.a != other.a:
...         return False
...     if self.b != other.b:
...         return False
...     return True
...

And a matcher object that can use comparison functions like this for its equality operation would look something like this:

>>> class Matcher:
...     def __init__(self, compare, some_obj):
...         self.compare = compare
...         self.some_obj = some_obj
...     def __eq__(self, other):
...         return self.compare(self.some_obj, other)
...

Putting all this together:

>>> match_foo = Matcher(compare, Foo(1, 2))
>>> mock.assert_called_with(match_foo)

The Matcher is instantiated with our compare function and the Foo object we want to compare against. In assert_called_with the Matcher equality method will be called, which compares the object the mock was called with against the one we created our matcher with. If they match then assert_called_with passes, and if they don’t an AssertionError is raised:

>>> match_wrong = Matcher(compare, Foo(3, 4))
>>> mock.assert_called_with(match_wrong)
Traceback (most recent call last):
    ...
AssertionError: Expected: ((<Matcher object at 0x...>,), {})
Called with: ((<Foo object at 0x...>,), {})

With a bit of tweaking you could have the comparison function raise the AssertionError directly and provide a more useful failure message.

As of version 1.5, the Python testing library PyHamcrest [https://pyhamcrest.readthedocs.org/] provides similar functionality, that may be useful here, in the form of its equality matcher (hamcrest.library.integration.match_equality [https://pyhamcrest.readthedocs.org/en/release-1.8/integration/#module-hamcrest.library.integration.match_equality]).

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

26.7. 2to3 - 自动将 Python 2 代码转为 Python 3 代码

2to3 是一个 Python 程序，它可以用来读取 Python 2.x 版本的代码，并使用一系列的 修复器 来将其转换为合法的 Python 3.x 代码。标准库中已经包含了丰富的修复器，这足以处理绝大多数代码。不过 2to3 的支持库 lib2to3 是一个很灵活通用的库，所以你也可以为 2to3 编写你自己的修复器。lib2to3 也可以用在那些需要自动处理 Python 代码的应用中。

26.7.1. 使用 2to3

2to3 通常会作为脚本和 Python 解释器一起安装，你可以在 Python 根目录的 Tools/scripts 文件夹下找到它。

2to3 的基本调用参数是一个需要转换的文件或目录列表。对于目录，会递归地寻找其中的 Python 源码。

这里有一个 Python 2.x 的源码文件，example.py：

def greet(name):
    print "Hello, {0}!".format(name)
print "What's your name?"
name = raw_input()
greet(name)

它可以在命令行中使用 2to3 转换成 Python 3.x 版本的代码：

$ 2to3 example.py

这个命令会打印出和源文件的区别。通过传入 -w 参数，2to3 也可以把需要的修改写回到原文件中（除非传入了 -n 参数，否则会为原始文件创建一个副本）：

$ 2to3 -w example.py

在转换完成后，example.py 看起来像是这样：

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)

注释和缩进都会在转换过程中保持不变。

默认情况下，2to3 会执行 预定义修复器 的集合。使用 -l 参数可以列出所有可用的修复器。使用 -f 参数可以明确指定需要使用的修复器集合。而使用 -x 参数则可以明确指定不使用的修复器。下面的例子会只使用 imports 和 has_key 修复器运行：

$ 2to3 -f imports -f has_key example.py

这个命令会执行除了 apply 之外的所有修复器：

$ 2to3 -x apply example.py

有一些修复器是需要 显式指定 的，它们默认不会执行，必须在命令行中列出才会执行。比如下面的例子，除了默认的修复器以外，还会执行 idioms 修复器：

$ 2to3 -f all -f idioms example.py

注意这里使用 all 来启用所有默认的修复器。

有些情况下 2to3 会找到源码中有一些需要修改，但是无法自动处理的代码。在这种情况下，2to3 会在差异处下面打印一个警告信息。你应该定位到相应的代码并对其进行修改，以使其兼容 Python 3.x。

2to3 也可以重构 doctests。使用 -d 开启这个模式。需要注意*只有* doctests 会被重构。这种模式下不需要文件是合法的 Python 代码。举例来说，reST 文档中类似 doctests 的示例也可以使用这个选项进行重构。

-v 选项可以输出更多转换程序的详细信息。

由于某些 print 语句可被解读为函数调用或是语句，2to3 并不是总能读取包含 print 函数的文件。当 2to3 检测到存在 from __future__ import print_function 编译器指令时，会修改其内部语法将 print() 解读为函数。这一变动也可以使用 -p 选项手动开启。使用 -p 来为已经转换过 print 语句的代码运行修复器。

-o 或 --output-dir 选项可以指定将转换后的文件写入其他目录中。由于这种情况下不会覆写原始文件，所以创建副本文件毫无意义，因此也需要使用 -n 选项来禁用创建副本。

3.2.3 新版功能: 增加了 -o 选项。

-W 或 --write-unchanged-files 选项用来告诉 2to3 始终需要输出文件，即使没有任何改动。这在使用 -o 参数时十分有用，这样就可以将整个 Python 源码包完整地转换到另一个目录。这个选项隐含了 -w 选项，否则等于没有作用。

3.2.3 新版功能: 增加了 -W 选项。

--add-suffix 选项接受一个字符串，用来作为后缀附加在输出文件名后面的后面。由于写入的文件名与原始文件不同，所以没有必要创建副本，因此 -n 选项也是必要的。举个例子：

$ 2to3 -n -W --add-suffix=3 example.py

这样会把转换后的文件写入 example.py3 文件。

3.2.3 新版功能: 增加了 --add-suffix 选项。

将整个项目从一个目录转换到另一个目录可以用这样的命令：

$ 2to3 --output-dir=python3-version/mycode -W -n python2-version/mycode

26.7.2. 修复器

转换代码的每一个步骤都封装在修复器中。可以使用 2to3 -l 来列出可用的修复器。之前已经提到，每个修复器都可以独立地打开或是关闭。下面会对各个修复器做更详细的描述。

apply

移除对 apply() 的使用，举例来说，apply(function, *args, **kwargs) 会被转换成 function(*args, **kwargs)。

asserts

将已弃用的 unittest 方法替换为正确的。

从

到

failUnlessEqual(a, b)

assertEqual(a, b)

assertEquals(a, b)

assertEqual(a, b)

failIfEqual(a, b)

assertNotEqual(a, b)

assertNotEquals(a, b)

assertNotEqual(a, b)

failUnless(a)

assertTrue(a)

assert_(a)

assertTrue(a)

failIf(a)

assertFalse(a)

failUnlessRaises(exc, cal)

assertRaises(exc, cal)

failUnlessAlmostEqual(a, b)

assertAlmostEqual(a, b)

assertAlmostEquals(a, b)

assertAlmostEqual(a, b)

failIfAlmostEqual(a, b)

assertNotAlmostEqual(a, b)

assertNotAlmostEquals(a, b)

assertNotAlmostEqual(a, b)

basestring

将 basestring 转换为 str。

buffer

将 buffer 转换为 memoryview。这个修复器是可选的，因为 memoryview API 和 buffer 很相似，但不完全一样。

dict

修复字典迭代方法。dict.iteritems() 会转换成 dict.items()，dict.iterkeys() 会转换成 dict.keys()，dict.itervalues() 会转换成 dict.values()。类似的，dict.viewitems()，dict.viewkeys() 和 dict.viewvalues() 会分别转换成 dict.items()，dict.keys() 和 dict.values()。另外也会将原有的 dict.items()，dict.keys() 和 dict.values() 方法调用用 list 包装一层。

except

将 except X, T 转换为 except X as T。

exec

将 exec 语句转换为 exec() 函数调用。

execfile

移除 execfile() 的使用。execfile() 的实参会使用 open()，compile() 和 exec() 包装。

exitfunc

将对 sys.exitfunc 的赋值改为使用 atexit 模块代替。

filter

将 filter() 函数用 list 包装一层。

funcattrs

修复已经重命名的函数属性。比如 my_function.func_closure 会被转换为 my_function.__closure__。

future

移除 from __future__ import new_feature 语句。

getcwdu

将 os.getcwdu() 重命名为 os.getcwd()。

has_key

将 dict.has_key(key) 转换为 key in dict。

idioms

这是一个可选的修复器，会进行多种转换，将 Python 代码变成更加常见的写法。类似 type(x) is SomeClass 和 type(x) == SomeClass 的类型对比会被转换成 isinstance(x, SomeClass)。while 1 转换成 while True。这个修复器还会在合适的地方使用 sorted() 函数。举个例子，这样的代码块：

L = list(some_iterable)
L.sort()

会被转换为：

L = sorted(some_iterable)

import

检测 sibling imports，并将其转换成相对 import。

imports

处理标准库模块的重命名。

imports2

处理标准库中其他模块的重命名。这个修复器由于一些技术上的限制，因此和 imports 拆分开了。

input

将 input(prompt) 转换为 eval(input(prompt))。

intern

将 intern() 转换为 sys.intern()。

isinstance

修复 isinstance() 函数第二个实参中重复的类型。举例来说，isinstance(x, (int, int)) 会转换为 isinstance(x, int), isinstance(x, (int, float, int)) 会转换为 isinstance(x, (int, float))。

itertools_imports

移除 itertools.ifilter()，itertools.izip() 以及 itertools.imap() 的 import。对 itertools.ifilterfalse() 的 import 也会替换成 itertools.filterfalse()。

itertools

修改 itertools.ifilter()，itertools.izip() 和 itertools.imap() 的调用为对应的内建实现。itertools.ifilterfalse() 会替换成 itertools.filterfalse()。

long

将 long 重命名为 int。

map

用 list 包装 map()。同时也会将 map(None, x) 替换为 list(x)。使用 from future_builtins import map 禁用这个修复器。

metaclass

将老的元类语法（类体中的 __metaclass__ = Meta）替换为新的（class X(metaclass=Meta)）。

methodattrs

修复老的方法属性名。例如 meth.im_func 会被转换为 meth.__func__。

ne

转换老的不等语法，将 <> 转为 !=。

next

将迭代器的 next() 方法调用转为 next() 函数。也会将 next() 方法重命名为 __next__()。

nonzero

将 __nonzero__() 转换为 __bool__()。

numliterals

将八进制字面量转为新的语法。

operator

Converts calls to various functions in the operator module to other, but equivalent, function calls. When needed, the appropriate import statements are added, e.g. import collections. The following mapping are made:

从

到

operator.isCallable(obj)

hasattr(obj, '__call__')

operator.sequenceIncludes(obj)

operator.contains(obj)

operator.isSequenceType(obj)

isinstance(obj, collections.Sequence)

operator.isMappingType(obj)

isinstance(obj, collections.Mapping)

operator.isNumberType(obj)

isinstance(obj, numbers.Number)

operator.repeat(obj, n)

operator.mul(obj, n)

operator.irepeat(obj, n)

operator.imul(obj, n)

paren

在列表生成式中增加必须的括号。例如将 [x for x in 1, 2] 转换为 [x for x in (1, 2)]。

print

将 print 语句转换为 print() 函数。

raise

将 raise E, V 转换为 raise E(V)，将 raise E, V, T 转换为 raise E(V).with_traceback(T)。如果 E 是元组，这样的转换是不正确的，因为用元组代替异常的做法在 3.0 中已经移除了。

raw_input

将 raw_input() 转换为 input()。

reduce

将 reduce() 转换为 functools.reduce()。

reload

Converts reload() to imp.reload().

renames

将 sys.maxint 转换为 sys.maxsize。

repr

将反引号 repr 表达式替换为 repr() 函数。

set_literal

将 set 构造函数替换为 set literals 写法。这个修复器是可选的。

standarderror

将 StandardError 重命名为 Exception。

sys_exc

将弃用的 sys.exc_value，sys.exc_type，sys.exc_traceback 替换为 sys.exc_info() 的用法。

throw

修复生成器的 throw() 方法的 API 变更。

tuple_params

移除隐式的元组参数解包。这个修复器会插入临时变量。

types

修复 type 模块中一些成员的移除引起的代码问题。

unicode

将 unicode 重命名为 str。

urllib

将 urllib 和 urllib2 重命名为 urllib 包。

ws_comma

移除逗号分隔的元素之间多余的空白。这个修复器是可选的。

xrange

将 xrange() 重命名为 range()，并用 list 包装原有的 range()。

xreadlines

将 for x in file.xreadlines() 转换为 for x in file。

zip

用 list 包装 zip()。如果使用了 from future_builtins import zip 的话会禁用。

26.7.3. lib2to3 —— 2to3 支持库

源代码： Lib/lib2to3/ [https://github.com/python/cpython/tree/3.6/Lib/lib2to3/]

注解

lib2to3 API 并不稳定，并可能在未来大幅修改。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

26.8. test — Python回归测试包

注解

The test package is meant for internal use by Python only. It is documented for the benefit of the core developers of Python. Any use of this package outside of Python’s standard library is discouraged as code mentioned here can change or be removed without notice between releases of Python.

The test package contains all regression tests for Python as well as the modules test.support and test.regrtest. test.support is used to enhance your tests while test.regrtest drives the testing suite.

Each module in the test package whose name starts with test_ is a testing suite for a specific module or feature. All new tests should be written using the unittest or doctest module. Some older tests are written using a “traditional” testing style that compares output printed to sys.stdout; this style of test is considered deprecated.

参见

模块 unittest

编写 PyUnit 回归测试.

doctest — 文档测试模块

Tests embedded in documentation strings.

26.8.1. Writing Unit Tests for the test package

It is preferred that tests that use the unittest module follow a few guidelines. One is to name the test module by starting it with test_ and end it with the name of the module being tested. The test methods in the test module should start with test_ and end with a description of what the method is testing. This is needed so that the methods are recognized by the test driver as test methods. Also, no documentation string for the method should be included. A comment (such as # Tests function returns only True or False) should be used to provide documentation for test methods. This is done because documentation strings get printed out if they exist and thus what test is being run is not stated.

A basic boilerplate is often used:

import unittest
from test import support

class MyTestCase1(unittest.TestCase):

    # Only use setUp() and tearDown() if necessary

    def setUp(self):
        ... code to execute in preparation for tests ...

    def tearDown(self):
        ... code to execute to clean up after tests ...

    def test_feature_one(self):
        # Test feature one.
        ... testing code ...

    def test_feature_two(self):
        # Test feature two.
        ... testing code ...

    ... more test methods ...

class MyTestCase2(unittest.TestCase):
    ... same structure as MyTestCase1 ...

... more test classes ...

if __name__ == '__main__':
    unittest.main()

This code pattern allows the testing suite to be run by test.regrtest, on its own as a script that supports the unittest CLI, or via the python -m unittest CLI.

The goal for regression testing is to try to break code. This leads to a few guidelines to be followed:

The testing suite should exercise all classes, functions, and constants. This includes not just the external API that is to be presented to the outside world but also “private” code.

Whitebox testing (examining the code being tested when the tests are being written) is preferred. Blackbox testing (testing only the published user interface) is not complete enough to make sure all boundary and edge cases are tested.

Make sure all possible values are tested including invalid ones. This makes sure that not only all valid values are acceptable but also that improper values are handled correctly.

Exhaust as many code paths as possible. Test where branching occurs and thus tailor input to make sure as many different paths through the code are taken.

Add an explicit test for any bugs discovered for the tested code. This will make sure that the error does not crop up again if the code is changed in the future.

Make sure to clean up after your tests (such as close and remove all temporary files).

If a test is dependent on a specific condition of the operating system then verify the condition already exists before attempting the test.

Import as few modules as possible and do it as soon as possible. This minimizes external dependencies of tests and also minimizes possible anomalous behavior from side-effects of importing a module.

Try to maximize code reuse. On occasion, tests will vary by something as small as what type of input is used. Minimize code duplication by subclassing a basic test class with a class that specifies the input:

class TestFuncAcceptsSequencesMixin:

    func = mySuperWhammyFunction

    def test_func(self):
        self.func(self.arg)

class AcceptLists(TestFuncAcceptsSequencesMixin, unittest.TestCase):
    arg = [1, 2, 3]

class AcceptStrings(TestFuncAcceptsSequencesMixin, unittest.TestCase):
    arg = 'abc'

class AcceptTuples(TestFuncAcceptsSequencesMixin, unittest.TestCase):
    arg = (1, 2, 3)

When using this pattern, remember that all classes that inherit from unittest.TestCase are run as tests. The Mixin class in the example above does not have any data and so can’t be run by itself, thus it does not inherit from unittest.TestCase.

参见

Test Driven Development

A book by Kent Beck on writing tests before code.

26.8.2. Running tests using the command-line interface

The test package can be run as a script to drive Python’s regression test suite, thanks to the -m option: python -m test. Under the hood, it uses test.regrtest; the call python -m test.regrtest used in previous Python versions still works. Running the script by itself automatically starts running all regression tests in the test package. It does this by finding all modules in the package whose name starts with test_, importing them, and executing the function test_main() if present or loading the tests via unittest.TestLoader.loadTestsFromModule if test_main does not exist. The names of tests to execute may also be passed to the script. Specifying a single regression test (python -m test test_spam) will minimize output and only print whether the test passed or failed.

Running test directly allows what resources are available for tests to use to be set. You do this by using the -u command-line option. Specifying all as the value for the -u option enables all possible resources: python -m test -uall. If all but one resource is desired (a more common case), a comma-separated list of resources that are not desired may be listed after all. The command python -m test -uall,-audio,-largefile will run test with all resources except the audio and largefile resources. For a list of all resources and more command-line options, run python -m test -h.

Some other ways to execute the regression tests depend on what platform the tests are being executed on. On Unix, you can run make test at the top-level directory where Python was built. On Windows, executing rt.bat from your PCBuild directory will run all regression tests.

26.9. test.support — Utilities for the Python test suite

The test.support module provides support for Python’s regression test suite.

注解

test.support is not a public module. It is documented here to help Python developers write tests. The API of this module is subject to change without backwards compatibility concerns between releases.

This module defines the following exceptions:

exception test.support.TestFailed

Exception to be raised when a test fails. This is deprecated in favor of unittest-based tests and unittest.TestCase’s assertion methods.

exception test.support.ResourceDenied

Subclass of unittest.SkipTest. Raised when a resource (such as a network connection) is not available. Raised by the requires() function.

The test.support module defines the following constants:

test.support.verbose

True when verbose output is enabled. Should be checked when more detailed information is desired about a running test. verbose is set by test.regrtest.

test.support.is_jython

True if the running interpreter is Jython.

test.support.TESTFN

Set to a name that is safe to use as the name of a temporary file. Any temporary file that is created should be closed and unlinked (removed).

The test.support module defines the following functions:

test.support.forget(module_name)

Remove the module named module_name from sys.modules and delete any byte-compiled files of the module.

test.support.is_resource_enabled(resource)

Return True if resource is enabled and available. The list of available resources is only set when test.regrtest is executing the tests.

test.support.requires(resource, msg=None)

Raise ResourceDenied if resource is not available. msg is the argument to ResourceDenied if it is raised. Always returns True if called by a function whose __name__ is '__main__'. Used when tests are executed by test.regrtest.

test.support.findfile(filename, subdir=None)

Return the path to the file named filename. If no match is found filename is returned. This does not equal a failure since it could be the path to the file.

Setting subdir indicates a relative path to use to find the file rather than looking directly in the path directories.

test.support.run_unittest(*classes)

Execute unittest.TestCase subclasses passed to the function. The function scans the classes for methods starting with the prefix test_ and executes the tests individually.

It is also legal to pass strings as parameters; these should be keys in sys.modules. Each associated module will be scanned by unittest.TestLoader.loadTestsFromModule(). This is usually seen in the following test_main() function:

def test_main():
    support.run_unittest(__name__)

This will run all tests defined in the named module.

test.support.run_doctest(module, verbosity=None)

Run doctest.testmod() on the given module. Return (failure_count, test_count).

If verbosity is None, doctest.testmod() is run with verbosity set to verbose. Otherwise, it is run with verbosity set to None.

test.support.check_warnings(*filters, quiet=True)

A convenience wrapper for warnings.catch_warnings() that makes it easier to test that a warning was correctly raised. It is approximately equivalent to calling warnings.catch_warnings(record=True) with warnings.simplefilter() set to always and with the option to automatically validate the results that are recorded.

check_warnings accepts 2-tuples of the form ("message regexp", WarningCategory) as positional arguments. If one or more filters are provided, or if the optional keyword argument quiet is False, it checks to make sure the warnings are as expected: each specified filter must match at least one of the warnings raised by the enclosed code or the test fails, and if any warnings are raised that do not match any of the specified filters the test fails. To disable the first of these checks, set quiet to True.

If no arguments are specified, it defaults to:

check_warnings(("", Warning), quiet=True)

In this case all warnings are caught and no errors are raised.

On entry to the context manager, a WarningRecorder instance is returned. The underlying warnings list from catch_warnings() is available via the recorder object’s warnings attribute. As a convenience, the attributes of the object representing the most recent warning can also be accessed directly through the recorder object (see example below). If no warning has been raised, then any of the attributes that would otherwise be expected on an object representing a warning will return None.

The recorder object also has a reset() method, which clears the warnings list.

The context manager is designed to be used like this:

with check_warnings(("assertion is always true", SyntaxWarning),
                    ("", UserWarning)):
    exec('assert(False, "Hey!")')
    warnings.warn(UserWarning("Hide me!"))

In this case if either warning was not raised, or some other warning was raised, check_warnings() would raise an error.

When a test needs to look more deeply into the warnings, rather than just checking whether or not they occurred, code like this can be used:

with check_warnings(quiet=True) as w:
    warnings.warn("foo")
    assert str(w.args[0]) == "foo"
    warnings.warn("bar")
    assert str(w.args[0]) == "bar"
    assert str(w.warnings[0].args[0]) == "foo"
    assert str(w.warnings[1].args[0]) == "bar"
    w.reset()
    assert len(w.warnings) == 0

Here all warnings will be caught, and the test code tests the captured warnings directly.

在 3.2 版更改: New optional arguments filters and quiet.

test.support.captured_stdin()

test.support.captured_stdout()

test.support.captured_stderr()

A context managers that temporarily replaces the named stream with io.StringIO object.

Example use with output streams:

with captured_stdout() as stdout, captured_stderr() as stderr:
    print("hello")
    print("error", file=sys.stderr)
assert stdout.getvalue() == "hello\n"
assert stderr.getvalue() == "error\n"

Example use with input stream:

with captured_stdin() as stdin:
    stdin.write('hello\n')
    stdin.seek(0)
    # call test code that consumes from sys.stdin
    captured = input()
self.assertEqual(captured, "hello")

test.support.temp_dir(path=None, quiet=False)

A context manager that creates a temporary directory at path and yields the directory.

If path is None, the temporary directory is created using tempfile.mkdtemp(). If quiet is False, the context manager raises an exception on error. Otherwise, if path is specified and cannot be created, only a warning is issued.

test.support.change_cwd(path, quiet=False)

A context manager that temporarily changes the current working directory to path and yields the directory.

If quiet is False, the context manager raises an exception on error. Otherwise, it issues only a warning and keeps the current working directory the same.

test.support.temp_cwd(name='tempcwd', quiet=False)

A context manager that temporarily creates a new directory and changes the current working directory (CWD).

The context manager creates a temporary directory in the current directory with name name before temporarily changing the current working directory. If name is None, the temporary directory is created using tempfile.mkdtemp().

If quiet is False and it is not possible to create or change the CWD, an error is raised. Otherwise, only a warning is raised and the original CWD is used.

test.support.temp_umask(umask)

A context manager that temporarily sets the process umask.

test.support.can_symlink()

Return True if the OS supports symbolic links, False otherwise.

@test.support.skip_unless_symlink

A decorator for running tests that require support for symbolic links.

@test.support.anticipate_failure(condition)

A decorator to conditionally mark tests with unittest.expectedFailure(). Any use of this decorator should have an associated comment identifying the relevant tracker issue.

@test.support.run_with_locale(catstr, *locales)

A decorator for running a function in a different locale, correctly resetting it after it has finished. catstr is the locale category as a string (for example "LC_ALL"). The locales passed will be tried sequentially, and the first valid locale will be used.

test.support.make_bad_fd()

Create an invalid file descriptor by opening and closing a temporary file, and returning its descriptor.

test.support.import_module(name, deprecated=False)

This function imports and returns the named module. Unlike a normal import, this function raises unittest.SkipTest if the module cannot be imported.

Module and package deprecation messages are suppressed during this import if deprecated is True.

3.1 新版功能.

test.support.import_fresh_module(name, fresh=(), blocked=(), deprecated=False)

This function imports and returns a fresh copy of the named Python module by removing the named module from sys.modules before doing the import. Note that unlike reload(), the original module is not affected by this operation.

fresh is an iterable of additional module names that are also removed from the sys.modules cache before doing the import.

blocked is an iterable of module names that are replaced with None in the module cache during the import to ensure that attempts to import them raise ImportError.

The named module and any modules named in the fresh and blocked parameters are saved before starting the import and then reinserted into sys.modules when the fresh import is complete.

Module and package deprecation messages are suppressed during this import if deprecated is True.

This function will raise ImportError if the named module cannot be imported.

Example use:

# Get copies of the warnings module for testing without affecting the
# version being used by the rest of the test suite. One copy uses the
# C implementation, the other is forced to use the pure Python fallback
# implementation
py_warnings = import_fresh_module('warnings', blocked=['_warnings'])
c_warnings = import_fresh_module('warnings', fresh=['_warnings'])

3.1 新版功能.

test.support.bind_port(sock, host=HOST)

Bind the socket to a free port and return the port number. Relies on ephemeral ports in order to ensure we are using an unbound port. This is important as many tests may be running simultaneously, especially in a buildbot environment. This method raises an exception if the sock.family is AF_INET and sock.type is SOCK_STREAM, and the socket has SO_REUSEADDR or SO_REUSEPORT set on it. Tests should never set these socket options for TCP/IP sockets. The only case for setting these options is testing multicasting via multiple UDP sockets.

Additionally, if the SO_EXCLUSIVEADDRUSE socket option is available (i.e. on Windows), it will be set on the socket. This will prevent anyone else from binding to our host/port for the duration of the test.

test.support.find_unused_port(family=socket.AF_INET, socktype=socket.SOCK_STREAM)

Returns an unused port that should be suitable for binding. This is achieved by creating a temporary socket with the same family and type as the sock parameter (default is AF_INET, SOCK_STREAM), and binding it to the specified host address (defaults to 0.0.0.0) with the port set to 0, eliciting an unused ephemeral port from the OS. The temporary socket is then closed and deleted, and the ephemeral port is returned.

Either this method or bind_port() should be used for any tests where a server socket needs to be bound to a particular port for the duration of the test. Which one to use depends on whether the calling code is creating a python socket, or if an unused port needs to be provided in a constructor or passed to an external program (i.e. the -accept argument to openssl’s s_server mode). Always prefer bind_port() over find_unused_port() where possible. Using a hard coded port is discouraged since it can make multiple instances of the test impossible to run simultaneously, which is a problem for buildbots.

test.support.load_package_tests(pkg_dir, loader, standard_tests, pattern)

Generic implementation of the unittest load_tests protocol for use in test packages. pkg_dir is the root directory of the package; loader, standard_tests, and pattern are the arguments expected by load_tests. In simple cases, the test package’s __init__.py can be the following:

import os
from test.support import load_package_tests

def load_tests(*args):
    return load_package_tests(os.path.dirname(__file__), *args)

test.support.detect_api_mismatch(ref_api, other_api, *, ignore=())

Returns the set of attributes, functions or methods of ref_api not found on other_api, except for a defined list of items to be ignored in this check specified in ignore.

By default this skips private attributes beginning with ‘_’ but includes all magic methods, i.e. those starting and ending in ‘__’.

3.5 新版功能.

test.support.check__all__(test_case, module, name_of_module=None, extra=(), blacklist=())

Assert that the __all__ variable of module contains all public names.

The module’s public names (its API) are detected automatically based on whether they match the public name convention and were defined in module.

The name_of_module argument can specify (as a string or tuple thereof) what module(s) an API could be defined in in order to be detected as a public API. One case for this is when module imports part of its public API from other modules, possibly a C backend (like csv and its _csv).

The extra argument can be a set of names that wouldn’t otherwise be automatically detected as “public”, like objects without a proper __module__ attribute. If provided, it will be added to the automatically detected ones.

The blacklist argument can be a set of names that must not be treated as part of the public API even though their names indicate otherwise.

Example use:

import bar
import foo
import unittest
from test import support

class MiscTestCase(unittest.TestCase):
    def test__all__(self):
        support.check__all__(self, foo)

class OtherTestCase(unittest.TestCase):
    def test__all__(self):
        extra = {'BAR_CONST', 'FOO_CONST'}
        blacklist = {'baz'}  # Undocumented name.
        # bar imports part of its API from _bar.
        support.check__all__(self, bar, ('bar', '_bar'),
                             extra=extra, blacklist=blacklist)

3.6 新版功能.

The test.support module defines the following classes:

class test.support.TransientResource(exc, **kwargs)

Instances are a context manager that raises ResourceDenied if the specified exception type is raised. Any keyword arguments are treated as attribute/value pairs to be compared against any exception raised within the with statement. Only if all pairs match properly against attributes on the exception is ResourceDenied raised.

class test.support.EnvironmentVarGuard

Class used to temporarily set or unset environment variables. Instances can be used as a context manager and have a complete dictionary interface for querying/modifying the underlying os.environ. After exit from the context manager all changes to environment variables done through this instance will be rolled back.

在 3.1 版更改: Added dictionary interface.

EnvironmentVarGuard.set(envvar, value)

Temporarily set the environment variable envvar to the value of value.

EnvironmentVarGuard.unset(envvar)

Temporarily unset the environment variable envvar.

class test.support.SuppressCrashReport

A context manager used to try to prevent crash dialog popups on tests that are expected to crash a subprocess.

On Windows, it disables Windows Error Reporting dialogs using SetErrorMode [https://msdn.microsoft.com/en-us/library/windows/desktop/ms680621.aspx].

On UNIX, resource.setrlimit() is used to set resource.RLIMIT_CORE’s soft limit to 0 to prevent coredump file creation.

On both platforms, the old value is restored by __exit__().

class test.support.WarningsRecorder

Class used to record warnings for unit tests. See documentation of check_warnings() above for more details.

class test.support.FakePath(path)

Simple path-like object. It implements the __fspath__() method which just returns the path argument. If path is an exception, it will be raised in __fspath__().

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

26. 开发工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

27. 调试和分析

这些库可以帮助你进行Python开发：调试器使你能够逐步执行代码，分析堆栈帧并设置断点等，而分析器运行代码并为你提供执行时间的详细分类，从而使你能够找出你程序中的瓶颈。

27.1. bdb — Debugger framework

27.2. faulthandler — Dump the Python traceback

27.2.1. Dumping the traceback

27.2.2. Fault handler state

27.2.3. Dumping the tracebacks after a timeout

27.2.4. Dumping the traceback on a user signal

27.2.5. Issue with file descriptors

27.2.6. 示例

27.3. pdb — Python的调试器

27.3.1. 调试器命令

27.4. Python 分析器

27.4.1. 分析器简介

27.4.2. 即时用户手册

27.4.3. profile 和 cProfile 模块参考

27.4.4. Stats 类

27.4.5. 什么是确定性性能分析？

27.4.6. 局限性

27.4.7. 准确估量

27.4.8. 使用自定义计时器

27.5. timeit — 测量小代码片段的执行时间

27.5.1. 基本示例

27.5.2. Python 接口

27.5.3. 命令行界面

27.5.4. 例子

27.6. trace — 跟踪Python语句执行

27.6.1. Command-Line Usage

27.6.1.1. Main options

27.6.1.2. Modifiers

27.6.1.3. Filters

27.6.2. 编程接口

27.7. tracemalloc — 跟踪内存分配

27.7.1. 例子

27.7.1.1. 显示前10项

27.7.1.2. 计算差异

27.7.1.3. Get the traceback of a memory block

27.7.1.4. Pretty top

27.7.2. API

27.7.2.1. 函数

27.7.2.2. 域过滤器

27.7.2.3. 过滤器

27.7.2.4. Frame

27.7.2.5. 快照

27.7.2.6. 统计

27.7.2.7. StatisticDiff

27.7.2.8. 跟踪

27.7.2.9. 回溯

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

27. 调试和分析 »

|

27.1. bdb — Debugger framework

Source code: Lib/bdb.py [https://github.com/python/cpython/tree/3.6/Lib/bdb.py]

The bdb module handles basic debugger functions, like setting breakpoints or managing execution via the debugger.

定义了以下异常：

exception bdb.BdbQuit

Exception raised by the Bdb class for quitting the debugger.

The bdb module also defines two classes:

class bdb.Breakpoint(self, file, line, temporary=0, cond=None, funcname=None)

This class implements temporary breakpoints, ignore counts, disabling and (re-)enabling, and conditionals.

Breakpoints are indexed by number through a list called bpbynumber and by (file, line) pairs through bplist. The former points to a single instance of class Breakpoint. The latter points to a list of such instances since there may be more than one breakpoint per line.

When creating a breakpoint, its associated filename should be in canonical form. If a funcname is defined, a breakpoint hit will be counted when the first line of that function is executed. A conditional breakpoint always counts a hit.

Breakpoint instances have the following methods:

deleteMe()

Delete the breakpoint from the list associated to a file/line. If it is the last breakpoint in that position, it also deletes the entry for the file/line.

enable()

Mark the breakpoint as enabled.

disable()

Mark the breakpoint as disabled.

bpformat()

Return a string with all the information about the breakpoint, nicely formatted:

The breakpoint number.

If it is temporary or not.

Its file,line position.

The condition that causes a break.

If it must be ignored the next N times.

The breakpoint hit count.

3.2 新版功能.

bpprint(out=None)

Print the output of bpformat() to the file out, or if it is None, to standard output.

class bdb.Bdb(skip=None)

The Bdb class acts as a generic Python debugger base class.

This class takes care of the details of the trace facility; a derived class should implement user interaction. The standard debugger class (pdb.Pdb) is an example.

The skip argument, if given, must be an iterable of glob-style module name patterns. The debugger will not step into frames that originate in a module that matches one of these patterns. Whether a frame is considered to originate in a certain module is determined by the __name__ in the frame globals.

3.1 新版功能: skip 参数。

The following methods of Bdb normally don’t need to be overridden.

canonic(filename)

Auxiliary method for getting a filename in a canonical form, that is, as a case-normalized (on case-insensitive filesystems) absolute path, stripped of surrounding angle brackets.

reset()

Set the botframe, stopframe, returnframe and quitting attributes with values ready to start debugging.

trace_dispatch(frame, event, arg)

This function is installed as the trace function of debugged frames. Its return value is the new trace function (in most cases, that is, itself).

The default implementation decides how to dispatch a frame, depending on the type of event (passed as a string) that is about to be executed. event can be one of the following:

"line": A new line of code is going to be executed.

"call": A function is about to be called, or another code block entered.

"return": A function or other code block is about to return.

"exception": An exception has occurred.

"c_call": A C function is about to be called.

"c_return": A C function has returned.

"c_exception": A C function has raised an exception.

For the Python events, specialized functions (see below) are called. For the C events, no action is taken.

The arg parameter depends on the previous event.

See the documentation for sys.settrace() for more information on the trace function. For more information on code and frame objects, refer to 标准类型层级结构.

dispatch_line(frame)

If the debugger should stop on the current line, invoke the user_line() method (which should be overridden in subclasses). Raise a BdbQuit exception if the Bdb.quitting flag is set (which can be set from user_line()). Return a reference to the trace_dispatch() method for further tracing in that scope.

dispatch_call(frame, arg)

If the debugger should stop on this function call, invoke the user_call() method (which should be overridden in subclasses). Raise a BdbQuit exception if the Bdb.quitting flag is set (which can be set from user_call()). Return a reference to the trace_dispatch() method for further tracing in that scope.

dispatch_return(frame, arg)

If the debugger should stop on this function return, invoke the user_return() method (which should be overridden in subclasses). Raise a BdbQuit exception if the Bdb.quitting flag is set (which can be set from user_return()). Return a reference to the trace_dispatch() method for further tracing in that scope.

dispatch_exception(frame, arg)

If the debugger should stop at this exception, invokes the user_exception() method (which should be overridden in subclasses). Raise a BdbQuit exception if the Bdb.quitting flag is set (which can be set from user_exception()). Return a reference to the trace_dispatch() method for further tracing in that scope.

Normally derived classes don’t override the following methods, but they may if they want to redefine the definition of stopping and breakpoints.

stop_here(frame)

This method checks if the frame is somewhere below botframe in the call stack. botframe is the frame in which debugging started.

break_here(frame)

This method checks if there is a breakpoint in the filename and line belonging to frame or, at least, in the current function. If the breakpoint is a temporary one, this method deletes it.

break_anywhere(frame)

This method checks if there is a breakpoint in the filename of the current frame.

Derived classes should override these methods to gain control over debugger operation.

user_call(frame, argument_list)

This method is called from dispatch_call() when there is the possibility that a break might be necessary anywhere inside the called function.

user_line(frame)

This method is called from dispatch_line() when either stop_here() or break_here() yields True.

user_return(frame, return_value)

This method is called from dispatch_return() when stop_here() yields True.

user_exception(frame, exc_info)

This method is called from dispatch_exception() when stop_here() yields True.

do_clear(arg)

Handle how a breakpoint must be removed when it is a temporary one.

This method must be implemented by derived classes.

Derived classes and clients can call the following methods to affect the stepping state.

set_step()

Stop after one line of code.

set_next(frame)

Stop on the next line in or below the given frame.

set_return(frame)

Stop when returning from the given frame.

set_until(frame)

Stop when the line with the line no greater than the current one is reached or when returning from current frame.

set_trace([frame])

Start debugging from frame. If frame is not specified, debugging starts from caller’s frame.

set_continue()

Stop only at breakpoints or when finished. If there are no breakpoints, set the system trace function to None.

set_quit()

Set the quitting attribute to True. This raises BdbQuit in the next call to one of the dispatch_*() methods.

Derived classes and clients can call the following methods to manipulate breakpoints. These methods return a string containing an error message if something went wrong, or None if all is well.

set_break(filename, lineno, temporary=0, cond, funcname)

Set a new breakpoint. If the lineno line doesn’t exist for the filename passed as argument, return an error message. The filename should be in canonical form, as described in the canonic() method.

clear_break(filename, lineno)

Delete the breakpoints in filename and lineno. If none were set, an error message is returned.

clear_bpbynumber(arg)

Delete the breakpoint which has the index arg in the Breakpoint.bpbynumber. If arg is not numeric or out of range, return an error message.

clear_all_file_breaks(filename)

Delete all breakpoints in filename. If none were set, an error message is returned.

clear_all_breaks()

Delete all existing breakpoints.

get_bpbynumber(arg)

Return a breakpoint specified by the given number. If arg is a string, it will be converted to a number. If arg is a non-numeric string, if the given breakpoint never existed or has been deleted, a ValueError is raised.

3.2 新版功能.

get_break(filename, lineno)

Check if there is a breakpoint for lineno of filename.

get_breaks(filename, lineno)

Return all breakpoints for lineno in filename, or an empty list if none are set.

get_file_breaks(filename)

Return all breakpoints in filename, or an empty list if none are set.

get_all_breaks()

Return all breakpoints that are set.

Derived classes and clients can call the following methods to get a data structure representing a stack trace.

get_stack(f, t)

Get a list of records for a frame and all higher (calling) and lower frames, and the size of the higher part.

format_stack_entry(frame_lineno, lprefix=': ')

Return a string with information about a stack entry, identified by a (frame, lineno) tuple:

The canonical form of the filename which contains the frame.

The function name, or "<lambda>".

The input arguments.

The return value.

The line of code (if it exists).

The following two methods can be called by clients to use a debugger to debug a statement, given as a string.

run(cmd, globals=None, locals=None)

Debug a statement executed via the exec() function. globals defaults to __main__.__dict__, locals defaults to globals.

runeval(expr, globals=None, locals=None)

Debug an expression executed via the eval() function. globals and locals have the same meaning as in run().

runctx(cmd, globals, locals)

For backwards compatibility. Calls the run() method.

runcall(func, *args, **kwds)

Debug a single function call, and return its result.

Finally, the module defines the following functions:

bdb.checkfuncname(b, frame)

Check whether we should break here, depending on the way the breakpoint b was set.

If it was set via line number, it checks if b.line is the same as the one in the frame also passed as argument. If the breakpoint was set via function name, we have to check we are in the right frame (the right function) and if we are in its first executable line.

bdb.effective(file, line, frame)

Determine if there is an effective (active) breakpoint at this line of code. Return a tuple of the breakpoint and a boolean that indicates if it is ok to delete a temporary breakpoint. Return (None, None) if there is no matching breakpoint.

bdb.set_trace()

Start debugging with a Bdb instance from caller’s frame.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

27. 调试和分析 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

27. 调试和分析 »

|

27.2. faulthandler — Dump the Python traceback

3.3 新版功能.

This module contains functions to dump Python tracebacks explicitly, on a fault, after a timeout, or on a user signal. Call faulthandler.enable() to install fault handlers for the SIGSEGV, SIGFPE, SIGABRT, SIGBUS, and SIGILL signals. You can also enable them at startup by setting the PYTHONFAULTHANDLER environment variable or by using the -X faulthandler command line option.

The fault handler is compatible with system fault handlers like Apport or the Windows fault handler. The module uses an alternative stack for signal handlers if the sigaltstack() function is available. This allows it to dump the traceback even on a stack overflow.

The fault handler is called on catastrophic cases and therefore can only use signal-safe functions (e.g. it cannot allocate memory on the heap). Because of this limitation traceback dumping is minimal compared to normal Python tracebacks:

Only ASCII is supported. The backslashreplace error handler is used on encoding.

Each string is limited to 500 characters.

Only the filename, the function name and the line number are displayed. (no source code)

It is limited to 100 frames and 100 threads.

The order is reversed: the most recent call is shown first.

By default, the Python traceback is written to sys.stderr. To see tracebacks, applications must be run in the terminal. A log file can alternatively be passed to faulthandler.enable().

The module is implemented in C, so tracebacks can be dumped on a crash or when Python is deadlocked.

27.2.1. Dumping the traceback

faulthandler.dump_traceback(file=sys.stderr, all_threads=True)

Dump the tracebacks of all threads into file. If all_threads is False, dump only the current thread.

在 3.5 版更改: Added support for passing file descriptor to this function.

27.2.2. Fault handler state

faulthandler.enable(file=sys.stderr, all_threads=True)

Enable the fault handler: install handlers for the SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL signals to dump the Python traceback. If all_threads is True, produce tracebacks for every running thread. Otherwise, dump only the current thread.

The file must be kept open until the fault handler is disabled: see issue with file descriptors.

在 3.5 版更改: Added support for passing file descriptor to this function.

在 3.6 版更改: On Windows, a handler for Windows exception is also installed.

faulthandler.disable()

Disable the fault handler: uninstall the signal handlers installed by enable().

faulthandler.is_enabled()

Check if the fault handler is enabled.

27.2.3. Dumping the tracebacks after a timeout

faulthandler.dump_traceback_later(timeout, repeat=False, file=sys.stderr, exit=False)

Dump the tracebacks of all threads, after a timeout of timeout seconds, or every timeout seconds if repeat is True. If exit is True, call _exit() with status=1 after dumping the tracebacks. (Note _exit() exits the process immediately, which means it doesn’t do any cleanup like flushing file buffers.) If the function is called twice, the new call replaces previous parameters and resets the timeout. The timer has a sub-second resolution.

The file must be kept open until the traceback is dumped or cancel_dump_traceback_later() is called: see issue with file descriptors.

This function is implemented using a watchdog thread and therefore is not available if Python is compiled with threads disabled.

在 3.5 版更改: Added support for passing file descriptor to this function.

faulthandler.cancel_dump_traceback_later()

Cancel the last call to dump_traceback_later().

27.2.4. Dumping the traceback on a user signal

faulthandler.register(signum, file=sys.stderr, all_threads=True, chain=False)

Register a user signal: install a handler for the signum signal to dump the traceback of all threads, or of the current thread if all_threads is False, into file. Call the previous handler if chain is True.

The file must be kept open until the signal is unregistered by unregister(): see issue with file descriptors.

Not available on Windows.

在 3.5 版更改: Added support for passing file descriptor to this function.

faulthandler.unregister(signum)

Unregister a user signal: uninstall the handler of the signum signal installed by register(). Return True if the signal was registered, False otherwise.

Not available on Windows.

27.2.5. Issue with file descriptors

enable(), dump_traceback_later() and register() keep the file descriptor of their file argument. If the file is closed and its file descriptor is reused by a new file, or if os.dup2() is used to replace the file descriptor, the traceback will be written into a different file. Call these functions again each time that the file is replaced.

27.2.6. 示例

Example of a segmentation fault on Linux with and without enabling the fault handler:

$ python3 -c "import ctypes; ctypes.string_at(0)"
Segmentation fault

$ python3 -q -X faulthandler
>>> import ctypes
>>> ctypes.string_at(0)
Fatal Python error: Segmentation fault

Current thread 0x00007fb899f39700 (most recent call first):
  File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at
  File "<stdin>", line 1 in <module>
Segmentation fault

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

27. 调试和分析 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

27. 调试和分析 »

|

27.3. pdb — Python的调试器

源代码： Lib/pdb.py [https://github.com/python/cpython/tree/3.6/Lib/pdb.py]

pdb 模块定义了一个交互式源代码调试器，用于 Python 程序。它支持在源码行间设置（有条件的）断点和单步执行，检视堆栈帧，列出源码列表，以及在任何堆栈帧的上下文中运行任意 Python 代码。它还支持事后调试，可以在程序控制下调用。

调试器是可扩展的——调试器实际被定义为 Pdb 类。该类目前没有文档，但通过阅读源码很容易理解它。扩展接口使用了 bdb 和 cmd 模块。

调试器的提示符是 (Pdb)。在调试器的控制下运行程序的典型用法是:

>>> import pdb
>>> import mymodule
>>> pdb.run('mymodule.test()')
> <string>(0)?()
(Pdb) continue
> <string>(1)?()
(Pdb) continue
NameError: 'spam'
> <string>(1)?()
(Pdb)

在 3.3 版更改: 由 readline 模块实现的 Tab 补全可用于补全本模块的命令和命令的参数，例如，p 命令的参数可补全为当前的全局变量和局部变量。

也可以将 pdb.py 作为脚本调用来调试其他脚本。例如:

python3 -m pdb myscript.py

当作为脚本调用时，如果要调试的程序异常退出，pdb 调试将自动进入事后调试。事后调试之后（或程序正常退出之后），pdb 将重新启动程序。自动重启会保留 pdb 的状态（如断点），在大多数情况下，这比在退出程序的同时退出调试器更加实用。

3.2 新版功能: pdb.py 现在接受 -c 选项，可以执行命令，这与将该命令写入 .pdbrc 文件相同，请参阅 调试器命令。

从正在运行的程序进入调试器的典型用法是插入

import pdb; pdb.set_trace()

到你想进入调试器的位置。然后就可以单步执行上述语句之后的代码，要关闭调试器继续运行，请使用 continue 命令。

检查已崩溃程序的典型用法是:

>>> import pdb
>>> import mymodule
>>> mymodule.test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "./mymodule.py", line 4, in test
    test2()
  File "./mymodule.py", line 3, in test2
    print(spam)
NameError: spam
>>> pdb.pm()
> ./mymodule.py(3)test2()
-> print(spam)
(Pdb)

本模块定义了下列函数，每个函数进入调试器的方式略有不同：

pdb.run(statement, globals=None, locals=None)

在调试器控制范围内执行 statement （以字符串或代码对象的形式提供）。调试器提示符会在执行代码前出现，你可以设置断点并键入 continue，也可以使用 step 或 next 逐步执行语句（上述所有命令在后文有说明）。可选参数 globals 和 locals 指定代码执行环境，默认时使用 __main__ 模块的字典。（请参阅内置函数 exec() 或 eval() 的说明。）

pdb.runeval(expression, globals=None, locals=None)

在调试器控制范围内执行 expression （以字符串或代码对象的形式提供）。runeval() 返回时将返回表达式的值。本函数在其他方面与 run() 类似。

pdb.runcall(function, *args, **kwds)

使用给定的参数调用 function （以函数或方法对象的形式提供，不能是字符串）。runcall() 返回的是所调用函数的返回值。调试器提示符将在进入函数后立即出现。

pdb.set_trace()

Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged (e.g. when an assertion fails).

pdb.post_mortem(traceback=None)

进入 traceback 对象的事后调试。如果没有给定 traceback，默认使用当前正在处理的异常之一（默认时，必须存在正在处理的异常）。

pdb.pm()

在 sys.last_traceback 中查找 traceback，并进入其事后调试。

run* 函数和 set_trace() 都是别名，用于实例化 Pdb 类和调用同名方法。如果要使用其他功能，则必须自己执行以下操作：

class pdb.Pdb(completekey='tab', stdin=None, stdout=None, skip=None, nosigint=False, readrc=True)

Pdb 是调试器类。

completekey、stdin 和 stdout 参数都会传递给底层的 cmd.Cmd 类，请参考相应的描述。

如果给出 skip 参数，则它必须是一个迭代器，可以迭代出 glob-style 样式的模块名称。如果遇到匹配上述样式的模块，调试器将不会进入来自该模块的堆栈帧。 1

默认情况下，当发出 continue 命令时，Pdb 将为 SIGINT 信号设置一个处理程序（SIGINT 信号是用户在控制台按 Ctrl-C 时发出的）。这使用户可以按 Ctrl-C 再次进入调试器。如果希望 Pdb 不要修改 SIGINT 处理程序，请将 nosigint 设置为 true。

readrc 参数默认为 true，它控制 Pdb 是否从文件系统加载 .pdbrc 文件。

启用跟踪且带有 skip 参数的调用示范:

import pdb; pdb.Pdb(skip=['django.*']).set_trace()

3.1 新版功能: skip 参数。

3.2 新版功能: nosigint 参数。在这之前，Pdb 不为 SIGINT 设置处理程序。

在 3.6 版更改: readrc 参数。

run(statement, globals=None, locals=None)

runeval(expression, globals=None, locals=None)

runcall(function, *args, **kwds)

set_trace()

请参阅上文解释同名函数的文档。

27.3.1. 调试器命令

下方列出的是调试器可接受的命令。如下所示，大多数命令可以缩写为一个或两个字母。如 h(elp) 表示可以输入 h 或 help 来输入帮助命令（但不能输入 he 或 hel，也不能是 H 或 Help 或 HELP）。命令的参数必须用空格（空格符或制表符）分隔。在命令语法中，可选参数括在方括号 ([]) 中，使用时请勿输入方括号。命令语法中的选择项由竖线 (|) 分隔。

输入一个空白行将重复最后输入的命令。例外：如果最后一个命令是 list 命令，则会列出接下来的 11 行。

调试器无法识别的命令将被认为是 Python 语句，并在正在调试的程序的上下文中执行。Python 语句也可以用感叹号 (!) 作为前缀。这是检查正在调试的程序的强大方法，甚至可以修改变量或调用函数。当此类语句发生异常，将打印异常名称，但调试器的状态不会改变。

调试器支持 别名。别名可以有参数，使得调试器对被检查的上下文有一定程度的适应性。

在一行中可以输入多个命令，以 ;; 分隔。（不能使用单个 ;，因为它用于分隔传递给 Python 解释器的一行中的多条语句。）切分命令很无脑，总是在第一个 ;; 对处将输入切分开，即使它位于带引号的字符串之中。

如果文件 .pdbrc 存在于用户主目录或当前目录中，则将其读入并执行，等同于在调试器提示符下键入该文件。这对于别名很有用。若两个文件都存在则首先读取主目录中的文件，且本地文件可以覆盖其中定义的别名。

在 3.2 版更改: .pdbrc 现在可以包含继续调试的命令，如 continue 或 next。文件中的这些命令以前是无效的。

h(elp) [command]

不带参数时，显示可用的命令列表。参数为 command 时，打印有关该命令的帮助。help pdb 显示完整文档（即 pdb 模块的文档字符串）。由于 command 参数必须是标识符，因此要获取 ! 的帮助必须输入 help exec。

w(here)

打印堆栈回溯，最新一帧在底部。有一个箭头指向当前帧，该帧决定了大多数命令的上下文。

d(own) [count]

在堆栈回溯中，将当前帧向下移动 count 级（默认为 1 级，移向更新的帧）。

u(p) [count]

在堆栈回溯中，将当前帧向上移动 count 级（默认为 1 级，移向更老的帧）。

b(reak) [([filename:]lineno | function) [, condition]]

如果带有 lineno 参数，则在当前文件相应行处设置一个断点。如果带有 function 参数，则在该函数的第一条可执行语句处设置一个断点。行号可以加上文件名和冒号作为前缀，以在另一个文件（可能是尚未加载的文件）中设置一个断点。另一个文件将在 sys.path 范围内搜索。请注意，每个断点都分配有一个编号，其他所有断点命令都引用该编号。

如果第二个参数存在，它应该是一个表达式，且它的计算值为 true 时断点才起作用。

如果不带参数执行，将列出所有中断，包括每个断点、命中该断点的次数、当前的忽略次数以及关联的条件（如果有）。

tbreak [([filename:]lineno | function) [, condition]]

临时断点，在第一次命中时会自动删除。它的参数与 break 相同。

cl(ear) [filename:lineno | bpnumber [bpnumber ...]]

如果参数是 filename:lineno，则清除此行上的所有断点。如果参数是空格分隔的断点编号列表，则清除这些断点。如果不带参数，则清除所有断点（但会先提示确认）。

disable [bpnumber [bpnumber ...]]

禁用断点，断点以空格分隔的断点编号列表给出。禁用断点表示它不会导致程序停止执行，但是与清除断点不同，禁用的断点将保留在断点列表中并且可以（重新）启用。

enable [bpnumber [bpnumber ...]]

启用指定的断点。

ignore bpnumber [count]

为指定的断点编号设置忽略次数。如果省略 count，则忽略次数将设置为 0。忽略次数为 0 时断点将变为活动状态。如果为非零值，在每次达到断点，且断点未禁用，且关联条件计算值为 true 的情况下，该忽略次数会递减。

condition bpnumber [condition]

为断点设置一个新 condition，它是一个表达式，且它的计算值为 true 时断点才起作用。如果没有给出 condition，则删除现有条件，也就是将断点设为无条件。

commands [bpnumber]

为编号是 bpnumber 的断点指定一系列命令。命令内容将显示在后续的几行中。输入仅包含 end 的行来结束命令列表。举个例子:

(Pdb) commands 1
(com) p some_variable
(com) end
(Pdb)

To remove all commands from a breakpoint, type commands and follow it immediately with end; that is, give no commands.

With no bpnumber argument, commands refers to the last breakpoint set.

You can use breakpoint commands to start your program up again. Simply use the continue command, or step, or any other command that resumes execution.

Specifying any command resuming execution (currently continue, step, next, return, jump, quit and their abbreviations) terminates the command list (as if that command was immediately followed by end). This is because any time you resume execution (even with a simple next or step), you may encounter another breakpoint—which could have its own command list, leading to ambiguities about which list to execute.

如果在命令列表中加入 ‘silent’ 命令，那么在该断点处停下时就不会打印常规信息。如果希望断点打印特定信息后继续运行，这可能是理想的。如果没有其他命令来打印一些信息，则看不到已达到断点的迹象。

s(tep)

执行当前行，在第一个可以停止的位置（在调用的函数中或在当前函数的下一行）停下。

n(ext)

继续运行，直到运行到当前函数的下一行，或当前函数返回为止。（ next 和 step 之间的区别在于，step 进入被调用函数内部并停止，而 next （几乎）全速运行被调用函数，仅在当前函数的下一行停止。）

unt(il) [lineno]

如果不带参数，则继续运行，直到行号比当前行大时停止。

如果带有行号，则继续运行，直到行号大于或等于该行号时停止。在这两种情况下，当前帧返回时也将停止。

在 3.2 版更改: 允许明确给定行号。

r(eturn)

继续运行，直到当前函数返回。

c(ont(inue))

继续运行，仅在遇到断点时停止。

j(ump) lineno

设置即将运行的下一行。仅可用于堆栈最底部的帧。它可以往回跳来再次运行代码，也可以往前跳来跳过不想运行的代码。

需要注意的是，不是所有的跳转都是允许的 – 例如，不能跳转到 for 循环的中间或跳出 finally 子句。

l(ist) [first[, last]]

列出当前文件的源代码。如果不带参数，则列出当前行周围的 11 行，或继续前一个列表。如果用 . 作为参数，则列出当前行周围的 11 行。如果带有一个参数，则列出那一行周围的 11 行。如果带有两个参数，则列出所给的范围中的代码；如果第二个参数小于第一个参数，则将其解释为列出行数的计数。

当前帧中的当前行用 -> 标记。如果正在调试异常，且最早抛出或传递该异常的行不是当前行，则那一行用 >> 标记。

3.2 新版功能: >> 标记。

ll | longlist

列出当前函数或帧的所有源代码。相关行的标记与 list 相同。

3.2 新版功能.

a(rgs)

打印当前函数的参数列表。

p expression

在当前上下文中运行 expression 并打印它的值。

注解

print() 也可以使用，但它不是一个调试器命令 — 它执行 Python print() 函数。

pp expression

与 p 命令类似，但表达式的值使用 pprint 模块美观地打印。

whatis expression

打印 expression 的类型。

source expression

尝试获取给定对象的源代码并显示出来。

3.2 新版功能.

display [expression]

如果表达式的值发生改变则显示它的值，每次将停止执行当前帧。

不带表达式则列出当前帧的所有显示表达式。

3.2 新版功能.

undisplay [expression]

不再显示当前帧中的表达式。 不带表达式则清除当前帧的所有显示表达式。

3.2 新版功能.

interact

启动一个交互式解释器（使用 code 模块），它的全局命名空间将包含当前作用域中的所有（全局和局部）名称。

3.2 新版功能.

alias [name [command]]

创建一个标识为 name 的别名来执行 command。 执行的命令 不可 加上引号。 可替换形参可通过 %1, %2 等来标示，而 %* 会被所有形参所替换。 如果没有给出命令，则会显示 name 的当前别名。 如果没有给出参数，则会列出所有别名。

别名允许嵌套并可包含能在 pdb 提示符下合法输入的任何内容。 请注意内部 pdb 命令 可以 被别名所覆盖。 这样的命令将被隐藏直到别名被移除。 别名会递归地应用到命令行的第一个单词；行内的其他单词不会受影响。

作为示例，这里列出了两个有用的别名（特别适合放在 .pdbrc 文件中）:

# Print instance variables (usage "pi classInst")
alias pi for k in %1.__dict__.keys(): print("%1.",k,"=",%1.__dict__[k])
# Print instance variables in self
alias ps pi self

unalias name

删除指定的别名。

! statement

在当前堆栈帧的上下文中执行 (单行) statement。 感叹号可以被省略，除非语句的第一个单词与调试器命令重名。 要设置全局变量，你可以在同一行上为赋值命令添加前缀的 global 语句，例如:

(Pdb) global list_options; list_options = ['-l']
(Pdb)

run [args ...]

restart [args ...]

重启被调试的 Python 程序。 如果提供了参数，它会用 shlex 来拆分且拆分结果将被用作新的 sys.argv。 历史、中断点、动作和调试器选项将被保留。 restart 是 run 的一个别名。

q(uit)

退出调试器。 被执行的程序将被中止。

备注

1

一个帧是否会被认为源自特定模块是由帧全局变量 __name__ 来决定的。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

27. 调试和分析 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

27. 调试和分析 »

|

27.4. Python 分析器

源代码： Lib/profile.py [https://github.com/python/cpython/tree/3.6/Lib/profile.py] and Lib/pstats.py [https://github.com/python/cpython/tree/3.6/Lib/pstats.py]

27.4.1. 分析器简介

cProfile 和 profile 提供了 Python 程序的 deterministic profiling 。 profile 是一组统计数据，描述程序的各个部分执行的频率和时间。这些统计数据可以通过 pstats 模块格式化为报告。

Python 标准库提供了同一分析接口的两种不同实现：

对于大多数用户，建议使用 cProfile ；这是一个C扩展插件，开销合理，适合于分析长时间运行的程序。该插件基于 lsprof ，由 Brett Rosen 和 Ted Chaotter 贡献。

profile 是一个纯 Python 模块（cProfile 就是模仿其接口的 C 实现），但它会显著增加配置程序的开销。如果你正在尝试以某种方式扩展分析器，则使用此模块可能会更容易完成任务。该模块最初由Jim Roskind 设计和编写。

注解

profiler 分析器模块被设计为给指定的程序提供执行概要文件，而不是用于基准测试目的（ timeit 才是用于此目标的，它能获得合理准确的结果）。这特别适用于将 Python 代码与 C 代码进行基准测试：分析器为Python 代码引入开销，但不会为C级函数引入开销，因此 C 代码似乎比任何Python 代码都更快。

27.4.2. 即时用户手册

本节是为 “不想阅读手册” 的用户提供的。它提供了非常简短的概述，并允许用户快速对现有应用程序执行评测。

要分析采用单个参数的函数，可以执行以下操作：

import cProfile
import re
cProfile.run('re.compile("foo|bar")')

（如果 cProfile 在您的系统上不可用，请使用 profile 。）

上述操作将运行 re.compile() 并打印分析结果，如下所示：

      197 function calls (192 primitive calls) in 0.002 seconds

Ordered by: standard name

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    0.000    0.000    0.001    0.001 <string>:1(<module>)
     1    0.000    0.000    0.001    0.001 re.py:212(compile)
     1    0.000    0.000    0.001    0.001 re.py:268(_compile)
     1    0.000    0.000    0.000    0.000 sre_compile.py:172(_compile_charset)
     1    0.000    0.000    0.000    0.000 sre_compile.py:201(_optimize_charset)
     4    0.000    0.000    0.000    0.000 sre_compile.py:25(_identityfunction)
   3/1    0.000    0.000    0.000    0.000 sre_compile.py:33(_compile)

第一行显示监听了197个调用。在这些调用中，有192个是 原始的 ，这意味着调用不是通过递归引发的。下一行: Ordered by: standard name ，表示最右边列中的文本字符串用于对输出进行排序。列标题包括：

ncalls

调用次数

tottime

在指定函数中花费的总时间（不包括调用子函数的时间）

percall

是 tottime 除以 ncalls 的商

cumtime

指定的函数及其所有子函数（从调用到退出）消耗的累积时间。这个数字对于递归函数来说是准确的。

percall

是 cumtime 除以原始调用（次数）的商

filename:lineno(function)

提供相应数据的每个函数

如果第一列中有两个数字（例如3/1），则表示函数递归。第二个值是原始调用次数，第一个是调用的总次数。请注意，当函数不递归时，这两个值是相同的，并且只打印单个数字。

profile 运行结束时，打印输出不是必须的。也可以通过为 run() 函数指定文件名，将结果保存到文件中：

import cProfile
import re
cProfile.run('re.compile("foo|bar")', 'restats')

pstats.Stats 类从文件中读取 profile 结果，并以各种方式对其进行格式化。

The file cProfile can also be invoked as a script to profile another script. For example:

python -m cProfile [-o output_file] [-s sort_order] myscript.py

-o 将profile 结果写入文件而不是标准输出

-s 指定 sort_stats() 排序值之一以对输出进行排序。这仅适用于未提供 -o 的情况

The pstats module’s Stats class has a variety of methods for manipulating and printing the data saved into a profile results file:

import pstats
p = pstats.Stats('restats')
p.strip_dirs().sort_stats(-1).print_stats()

The strip_dirs() method removed the extraneous path from all the module names. The sort_stats() method sorted all the entries according to the standard module/line/name string that is printed. The print_stats() method printed out all the statistics. You might try the following sort calls:

p.sort_stats('name')
p.print_stats()

The first call will actually sort the list by function name, and the second call will print out the statistics. The following are some interesting calls to experiment with:

p.sort_stats('cumulative').print_stats(10)

This sorts the profile by cumulative time in a function, and then only prints the ten most significant lines. If you want to understand what algorithms are taking time, the above line is what you would use.

If you were looking to see what functions were looping a lot, and taking a lot of time, you would do:

p.sort_stats('time').print_stats(10)

to sort according to time spent within each function, and then print the statistics for the top ten functions.

您也可以尝试：

p.sort_stats('file').print_stats('__init__')

This will sort all the statistics by file name, and then print out statistics for only the class init methods (since they are spelled with __init__ in them). As one final example, you could try:

p.sort_stats('time', 'cumulative').print_stats(.5, 'init')

This line sorts statistics with a primary key of time, and a secondary key of cumulative time, and then prints out some of the statistics. To be specific, the list is first culled down to 50% (re: .5) of its original size, then only lines containing init are maintained, and that sub-sub-list is printed.

If you wondered what functions called the above functions, you could now (p is still sorted according to the last criteria) do:

p.print_callers(.5, 'init')

and you would get a list of callers for each of the listed functions.

If you want more functionality, you’re going to have to read the manual, or guess what the following functions do:

p.print_callees()
p.add('restats')

Invoked as a script, the pstats module is a statistics browser for reading and examining profile dumps. It has a simple line-oriented interface (implemented using cmd) and interactive help.

27.4.3. profile 和 cProfile 模块参考

profile 和 cProfile 模块都提供下列函数：

profile.run(command, filename=None, sort=-1)

This function takes a single argument that can be passed to the exec() function, and an optional file name. In all cases this routine executes:

exec(command, __main__.__dict__, __main__.__dict__)

and gathers profiling statistics from the execution. If no file name is present, then this function automatically creates a Stats instance and prints a simple profiling report. If the sort value is specified, it is passed to this Stats instance to control how the results are sorted.

profile.runctx(command, globals, locals, filename=None, sort=-1)

This function is similar to run(), with added arguments to supply the globals and locals dictionaries for the command string. This routine executes:

exec(command, globals, locals)

and gathers profiling statistics as in the run() function above.

class profile.Profile(timer=None, timeunit=0.0, subcalls=True, builtins=True)

This class is normally only used if more precise control over profiling is needed than what the cProfile.run() function provides.

A custom timer can be supplied for measuring how long code takes to run via the timer argument. This must be a function that returns a single number representing the current time. If the number is an integer, the timeunit specifies a multiplier that specifies the duration of each unit of time. For example, if the timer returns times measured in thousands of seconds, the time unit would be .001.

Directly using the Profile class allows formatting profile results without writing the profile data to a file:

import cProfile, pstats, io
pr = cProfile.Profile()
pr.enable()
# ... do something ...
pr.disable()
s = io.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())

enable()

Start collecting profiling data.

disable()

Stop collecting profiling data.

create_stats()

停止收集分析数据，并在内部将结果记录为当前 profile。

print_stats(sort=-1)

Create a Stats object based on the current profile and print the results to stdout.

dump_stats(filename)

将当前profile 的结果写入 filename 。

run(cmd)

Profile the cmd via exec().

runctx(cmd, globals, locals)

Profile the cmd via exec() with the specified global and local environment.

runcall(func, *args, **kwargs)

Profile func(*args, **kwargs)

Note that profiling will only work if the called command/function actually returns. If the interpreter is terminated (e.g. via a sys.exit() call during the called command/function execution) no profiling results will be printed.

27.4.4. Stats 类

Analysis of the profiler data is done using the Stats class.

class pstats.Stats(*filenames or profile, stream=sys.stdout)

This class constructor creates an instance of a “statistics object” from a filename (or list of filenames) or from a Profile instance. Output will be printed to the stream specified by stream.

The file selected by the above constructor must have been created by the corresponding version of profile or cProfile. To be specific, there is no file compatibility guaranteed with future versions of this profiler, and there is no compatibility with files produced by other profilers, or the same profiler run on a different operating system. If several files are provided, all the statistics for identical functions will be coalesced, so that an overall view of several processes can be considered in a single report. If additional files need to be combined with data in an existing Stats object, the add() method can be used.

Instead of reading the profile data from a file, a cProfile.Profile or profile.Profile object can be used as the profile data source.

Stats 对象有以下方法:

strip_dirs()

This method for the Stats class removes all leading path information from file names. It is very useful in reducing the size of the printout to fit within (close to) 80 columns. This method modifies the object, and the stripped information is lost. After performing a strip operation, the object is considered to have its entries in a “random” order, as it was just after object initialization and loading. If strip_dirs() causes two function names to be indistinguishable (they are on the same line of the same filename, and have the same function name), then the statistics for these two entries are accumulated into a single entry.

add(*filenames)

This method of the Stats class accumulates additional profiling information into the current profiling object. Its arguments should refer to filenames created by the corresponding version of profile.run() or cProfile.run(). Statistics for identically named (re: file, line, name) functions are automatically accumulated into single function statistics.

dump_stats(filename)

Save the data loaded into the Stats object to a file named filename. The file is created if it does not exist, and is overwritten if it already exists. This is equivalent to the method of the same name on the profile.Profile and cProfile.Profile classes.

sort_stats(*keys)

This method modifies the Stats object by sorting it according to the supplied criteria. The argument is typically a string identifying the basis of a sort (example: 'time' or 'name').

When more than one key is provided, then additional keys are used as secondary criteria when there is equality in all keys selected before them. For example, sort_stats('name', 'file') will sort all the entries according to their function name, and resolve all ties (identical function names) by sorting by file name.

Abbreviations can be used for any key names, as long as the abbreviation is unambiguous. The following are the keys currently defined:

Valid Arg

含义

'calls'

调用次数

'cumulative'

累积时间

'cumtime'

累积时间

'file'

文件名

'filename'

文件名

'module'

文件名

'ncalls'

调用次数

'pcalls'

原始调用计数

'line'

行号

'name'

函数名称

'nfl'

名称/文件/行

'stdname'

标准名称

'time'

内部时间

'tottime'

内部时间

Note that all sorts on statistics are in descending order (placing most time consuming items first), where as name, file, and line number searches are in ascending order (alphabetical). The subtle distinction between 'nfl' and 'stdname' is that the standard name is a sort of the name as printed, which means that the embedded line numbers get compared in an odd way. For example, lines 3, 20, and 40 would (if the file names were the same) appear in the string order 20, 3 and 40. In contrast, 'nfl' does a numeric compare of the line numbers. In fact, sort_stats('nfl') is the same as sort_stats('name', 'file', 'line').

For backward-compatibility reasons, the numeric arguments -1, 0, 1, and 2 are permitted. They are interpreted as 'stdname', 'calls', 'time', and 'cumulative' respectively. If this old style format (numeric) is used, only one sort key (the numeric key) will be used, and additional arguments will be silently ignored.

reverse_order()

This method for the Stats class reverses the ordering of the basic list within the object. Note that by default ascending vs descending order is properly selected based on the sort key of choice.

print_stats(*restrictions)

This method for the Stats class prints out a report as described in the profile.run() definition.

The order of the printing is based on the last sort_stats() operation done on the object (subject to caveats in add() and strip_dirs()).

The arguments provided (if any) can be used to limit the list down to the significant entries. Initially, the list is taken to be the complete set of profiled functions. Each restriction is either an integer (to select a count of lines), or a decimal fraction between 0.0 and 1.0 inclusive (to select a percentage of lines), or a string that will interpreted as a regular expression (to pattern match the standard name that is printed). If several restrictions are provided, then they are applied sequentially. For example:

print_stats(.1, 'foo:')

would first limit the printing to first 10% of list, and then only print functions that were part of filename .*foo:. In contrast, the command:

print_stats('foo:', .1)

would limit the list to all functions having file names .*foo:, and then proceed to only print the first 10% of them.

print_callers(*restrictions)

This method for the Stats class prints a list of all functions that called each function in the profiled database. The ordering is identical to that provided by print_stats(), and the definition of the restricting argument is also identical. Each caller is reported on its own line. The format differs slightly depending on the profiler that produced the stats:

With profile, a number is shown in parentheses after each caller to show how many times this specific call was made. For convenience, a second non-parenthesized number repeats the cumulative time spent in the function at the right.

With cProfile, each caller is preceded by three numbers: the number of times this specific call was made, and the total and cumulative times spent in the current function while it was invoked by this specific caller.

print_callees(*restrictions)

This method for the Stats class prints a list of all function that were called by the indicated function. Aside from this reversal of direction of calls (re: called vs was called by), the arguments and ordering are identical to the print_callers() method.

27.4.5. 什么是确定性性能分析？

确定性性能分析 旨在反映这样一个事实：即所有 函数调用 、 函数返回 和 异常 事件都被监控，并且对这些事件之间的间隔（在此期间用户的代码正在执行）进行精确计时。相反，统计分析（不是由该模块完成）随机采样有效指令指针，并推断时间花费在哪里。后一种技术传统上涉及较少的开销（因为代码不需要检测），但只提供了时间花在哪里的相对指示。

In Python, since there is an interpreter active during execution, the presence of instrumented code is not required to do deterministic profiling. Python automatically provides a hook (optional callback) for each event. In addition, the interpreted nature of Python tends to add so much overhead to execution, that deterministic profiling tends to only add small processing overhead in typical applications. The result is that deterministic profiling is not that expensive, yet provides extensive run time statistics about the execution of a Python program.

调用计数统计信息可用于识别代码中的错误（意外计数），并识别可能的内联扩展点（高频调用）。内部时间统计可用于识别应仔细优化的 “热循环” 。累积时间统计可用于识别算法选择上的高级别错误。注意，该分析器中对累积时间的异常处理允许将算法的递归实现与迭代实现的统计信息直接进行比较。

27.4.6. 局限性

一个限制是关于时间信息的准确性。确定性性能分析存在一个涉及精度的基本问题。最明显的限制是，底层的 “时钟” 只以大约0.001秒的速度（通常）运行。因此，没有什么测量会比底层时钟更精确。如果进行了足够的测量，那么 “误差” 将趋于平均。不幸的是，删除第一个错误会导致第二个错误来源。

第二个问题是，从调度事件到分析器获取时间的调用实际获取时钟状态，这需要 “一段时间” 。类似地，从获取时钟值（然后保存）开始，直到再次执行用户代码为止，退出分析器事件句柄时也存在一定的延迟。因此，多次调用单个函数或调用多个函数通常会累积此错误。以这种方式累积的误差通常小于时钟的精度（小于一个时钟周期），但它 可以 累积并变得非常客观。

与开销较低的 cProfile 相比， profile 的问题更为严重。出于这个原因， profile 提供了一种针对指定平台的自我校准方法，以便可以在很大程度上（平均地）消除此误差。

27.4.7. 准确估量

profile 模块的 profiler 会从每个事件处理时间中减去一个常量，以补偿调用 time 函数和存储结果的开销。默认情况下，常数为0。对于特定的平台，可用以下程序获得更好修正常数（ 局限性 ）。

import profile
pr = profile.Profile()
for i in range(5):
    print(pr.calibrate(10000))

The method executes the number of Python calls given by the argument, directly and again under the profiler, measuring the time for both. It then computes the hidden overhead per profiler event, and returns that as a float. For example, on a 1.8Ghz Intel Core i5 running Mac OS X, and using Python’s time.clock() as the timer, the magical number is about 4.04e-6.

The object of this exercise is to get a fairly consistent result. If your computer is very fast, or your timer function has poor resolution, you might have to pass 100000, or even 1000000, to get consistent results.

当你有一个一致的答案时，有三种方法可以使用：

import profile

# 1. Apply computed bias to all Profile instances created hereafter.
profile.Profile.bias = your_computed_bias

# 2. Apply computed bias to a specific Profile instance.
pr = profile.Profile()
pr.bias = your_computed_bias

# 3. Specify computed bias in instance constructor.
pr = profile.Profile(bias=your_computed_bias)

If you have a choice, you are better off choosing a smaller constant, and then your results will “less often” show up as negative in profile statistics.

27.4.8. 使用自定义计时器

If you want to change how current time is determined (for example, to force use of wall-clock time or elapsed process time), pass the timing function you want to the Profile class constructor:

pr = profile.Profile(your_time_func)

The resulting profiler will then call your_time_func. Depending on whether you are using profile.Profile or cProfile.Profile, your_time_func’s return value will be interpreted differently:

profile.Profile

your_time_func should return a single number, or a list of numbers whose sum is the current time (like what os.times() returns). If the function returns a single time number, or the list of returned numbers has length 2, then you will get an especially fast version of the dispatch routine.

Be warned that you should calibrate the profiler class for the timer function that you choose (see 准确估量). For most machines, a timer that returns a lone integer value will provide the best results in terms of low overhead during profiling. (os.times() is pretty bad, as it returns a tuple of floating point values). If you want to substitute a better timer in the cleanest fashion, derive a class and hardwire a replacement dispatch method that best handles your timer call, along with the appropriate calibration constant.

cProfile.Profile

your_time_func should return a single number. If it returns integers, you can also invoke the class constructor with a second argument specifying the real duration of one unit of time. For example, if your_integer_time_func returns times measured in thousands of seconds, you would construct the Profile instance as follows:

pr = cProfile.Profile(your_integer_time_func, 0.001)

As the cProfile.Profile class cannot be calibrated, custom timer functions should be used with care and should be as fast as possible. For the best results with a custom timer, it might be necessary to hard-code it in the C source of the internal _lsprof module.

Python 3.3 adds several new functions in time that can be used to make precise measurements of process or wall-clock time. For example, see time.perf_counter().

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

27. 调试和分析 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

27. 调试和分析 »

|

27.5. timeit — 测量小代码片段的执行时间

源码： Lib/timeit.py [https://github.com/python/cpython/tree/3.6/Lib/timeit.py]

该模块提供了一种简单的方法来计算一小段 Python 代码的耗时。它有 命令行界面 以及一个 可调用 方法。它避免了许多用于测量执行时间的常见陷阱。另见 Tim Peters 对 O’Reilly 出版的 Python Cookbook 中“算法”章节的介绍。

27.5.1. 基本示例

以下示例显示了如何使用 命令行界面 来比较三个不同的表达式：

$ python3 -m timeit '"-".join(str(n) for n in range(100))'
10000 loops, best of 3: 30.2 usec per loop
$ python3 -m timeit '"-".join([str(n) for n in range(100)])'
10000 loops, best of 3: 27.5 usec per loop
$ python3 -m timeit '"-".join(map(str, range(100)))'
10000 loops, best of 3: 23.2 usec per loop

这可以通过 Python 接口 实现

>>> import timeit
>>> timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)
0.3018611848820001
>>> timeit.timeit('"-".join([str(n) for n in range(100)])', number=10000)
0.2727368790656328
>>> timeit.timeit('"-".join(map(str, range(100)))', number=10000)
0.23702679807320237

Note however that timeit will automatically determine the number of repetitions only when the command-line interface is used. In the 例子 section you can find more advanced examples.

27.5.2. Python 接口

该模块定义了三个便利函数和一个公共类：

timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000, globals=None)

使用给定语句、 setup 代码和 timer 函数创建一个 Timer 实例，并执行 number 次其 timeit() 方法。可选的 globals 参数指定用于执行代码的命名空间。

在 3.5 版更改: 添加可选参数 globals 。

timeit.repeat(stmt='pass', setup='pass', timer=<default timer>, repeat=3, number=1000000, globals=None)

使用给定语句、 setup 代码和 timer 函数创建一个 Timer 实例，并使用给定的 repeat 计数和 number 执行运行其 repeat() 方法。可选的 globals 参数指定用于执行代码的命名空间。

在 3.5 版更改: 添加可选参数 globals 。

timeit.default_timer()

默认的计时器，总是 time.perf_counter() 。

在 3.3 版更改: time.perf_counter() 现在是默认计时器。

class timeit.Timer(stmt='pass', setup='pass', timer=<timer function>, globals=None)

用于小代码片段的计数执行速度的类。

构造函数接受一个将计时的语句、一个用于设置的附加语句和一个定时器函数。两个语句都默认为 'pass' ；计时器函数与平台有关（请参阅模块文档字符串）。 stmt 和 setup 也可能包含多个以 ; 或换行符分隔的语句，只要它们不包含多行字符串文字即可。该语句默认在 timeit 的命名空间内执行；可以通过将命名空间传递给 globals 来控制此行为。

要测量第一个语句的执行时间，请使用 timeit() 方法。 repeat() 和 autorange() 方法是方便的方法来调用 timeit() 多次。

setup 的执行时间从总体计时执行中排除。

stmt 和 setup 参数也可以使用不带参数的可调用对象。这将在一个计时器函数中嵌入对它们的调用，然后由 timeit() 执行。请注意，由于额外的函数调用，在这种情况下，计时开销会略大一些。

在 3.5 版更改: 添加可选参数 globals 。

timeit(number=1000000)

执行 number 次主要语句。这将执行一次 setup 语句，然后返回执行主语句多次所需的时间，以秒为单位测量为浮点数。参数是通过循环的次数，默认为一百万。要使用的主语句、 setup 语句和 timer 函数将传递给构造函数。

注解

By default, timeit() temporarily turns off garbage collection during the timing. The advantage of this approach is that it makes independent timings more comparable. This disadvantage is that GC may be an important component of the performance of the function being measured. If so, GC can be re-enabled as the first statement in the setup string. For example:

timeit.Timer('for i in range(10): oct(i)', 'gc.enable()').timeit()

autorange(callback=None)

自动决定调用多少次 timeit() 。

This is a convenience function that calls timeit() repeatedly so that the total time >= 0.2 second, returning the eventual (number of loops, time taken for that number of loops). It calls timeit() with number set to successive powers of ten (10, 100, 1000, …) up to a maximum of one billion, until the time taken is at least 0.2 second, or the maximum is reached.

如果给出 callback 并且不是 None ，则在每次试验后将使用两个参数调用它： callback(number, time_taken) 。

3.6 新版功能.

repeat(repeat=3, number=1000000)

调用 timeit() 几次。

这是一个方便的函数，它反复调用 timeit() ，返回结果列表。第一个参数指定调用 timeit() 的次数。第二个参数指定 timeit() 的 number 参数。

注解

从结果向量计算并报告平均值和标准差这些是很诱人的。但是，这不是很有用。在典型情况下，最低值给出了机器运行给定代码段的速度的下限；结果向量中较高的值通常不是由Python的速度变化引起的，而是由于其他过程干扰你的计时准确性。所以结果的 min() 可能是你应该感兴趣的唯一数字。之后，你应该看看整个向量并应用常识而不是统计。

print_exc(file=None)

帮助程序从计时代码中打印回溯。

典型使用:

t = Timer(...)       # outside the try/except
try:
    t.timeit(...)    # or t.repeat(...)
except Exception:
    t.print_exc()

与标准回溯相比，优势在于将显示已编译模板中的源行。可选的 file 参数指向发送回溯的位置；它默认为 sys.stderr 。

27.5.3. 命令行界面

从命令行调用程序时，使用以下表单:

python -m timeit [-n N] [-r N] [-u U] [-s S] [-t] [-c] [-h] [statement ...]

如果了解以下选项：

-n N, --number=N

执行 ‘语句’ 多少次

-r N, --repeat=N

how many times to repeat the timer (default 3)

-s S, --setup=S

最初要执行一次的语句（默认为 pass ）

-p, --process

测量进程时间，而不是 wallclock 时间，使用 time.process_time() 而不是 time.perf_counter() ，这是默认值

3.3 新版功能.

-t, --time

use time.time() (deprecated)

-u, --unit=U

specify a time unit for timer output; can select usec, msec, or sec

3.5 新版功能.

-c, --clock

use time.clock() (deprecated)

-v, --verbose

打印原始计时结果；重复更多位数精度

-h, --help

打印一条简短的使用信息并退出

可以通过将每一行指定为单独的语句参数来给出多行语句；通过在引号中包含参数并使用前导空格可以缩进行。多个 -s 选项的处理方式相似。

If -n is not given, a suitable number of loops is calculated by trying successive powers of 10 until the total time is at least 0.2 seconds.

default_timer() measurements can be affected by other programs running on the same machine, so the best thing to do when accurate timing is necessary is to repeat the timing a few times and use the best time. The -r option is good for this; the default of 3 repetitions is probably enough in most cases. You can use time.process_time() to measure CPU time.

注解

执行 pass 语句会产生一定的基线开销。这里的代码不会试图隐藏它，但你应该知道它。可以通过不带参数调用程序来测量基线开销，并且Python版本之间可能会有所不同。

27.5.4. 例子

可以提供一个在开头只执行一次的 setup 语句：

$ python -m timeit -s 'text = "sample string"; char = "g"'  'char in text'
10000000 loops, best of 3: 0.0877 usec per loop
$ python -m timeit -s 'text = "sample string"; char = "g"'  'text.find(char)'
1000000 loops, best of 3: 0.342 usec per loop

>>> import timeit
>>> timeit.timeit('char in text', setup='text = "sample string"; char = "g"')
0.41440500499993504
>>> timeit.timeit('text.find(char)', setup='text = "sample string"; char = "g"')
1.7246671520006203

使用 Timer 类及其方法可以完成同样的操作:

>>> import timeit
>>> t = timeit.Timer('char in text', setup='text = "sample string"; char = "g"')
>>> t.timeit()
0.3955516149999312
>>> t.repeat()
[0.40193588800002544, 0.3960157959998014, 0.39594301399984033]

以下示例显示如何计算包含多行的表达式。 在这里我们对比使用 hasattr() 与 try/except 的开销来测试缺失与提供对象属性:

$ python -m timeit 'try:' '  str.__bool__' 'except AttributeError:' '  pass'
100000 loops, best of 3: 15.7 usec per loop
$ python -m timeit 'if hasattr(str, "__bool__"): pass'
100000 loops, best of 3: 4.26 usec per loop

$ python -m timeit 'try:' '  int.__bool__' 'except AttributeError:' '  pass'
1000000 loops, best of 3: 1.43 usec per loop
$ python -m timeit 'if hasattr(int, "__bool__"): pass'
100000 loops, best of 3: 2.23 usec per loop

>>> import timeit
>>> # attribute is missing
>>> s = """\
... try:
...     str.__bool__
... except AttributeError:
...     pass
... """
>>> timeit.timeit(stmt=s, number=100000)
0.9138244460009446
>>> s = "if hasattr(str, '__bool__'): pass"
>>> timeit.timeit(stmt=s, number=100000)
0.5829014980008651
>>>
>>> # attribute is present
>>> s = """\
... try:
...     int.__bool__
... except AttributeError:
...     pass
... """
>>> timeit.timeit(stmt=s, number=100000)
0.04215312199994514
>>> s = "if hasattr(int, '__bool__'): pass"
>>> timeit.timeit(stmt=s, number=100000)
0.08588060699912603

要让 timeit 模块访问你定义的函数，你可以传递一个包含 import 语句的 setup 参数:

def test():
    """Stupid test function"""
    L = [i for i in range(100)]

if __name__ == '__main__':
    import timeit
    print(timeit.timeit("test()", setup="from __main__ import test"))

另一种选择是将 globals() 传递给 globals 参数，这将导致代码在当前的全局命名空间中执行。这比单独指定 import 更方便

def f(x):
    return x**2
def g(x):
    return x**4
def h(x):
    return x**8

import timeit
print(timeit.timeit('[func(42) for func in (f,g,h)]', globals=globals()))

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

27. 调试和分析 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

27. 调试和分析 »

|

27.6. trace — 跟踪Python语句执行

源代码: Lib/trace.py [https://github.com/python/cpython/tree/3.6/Lib/trace.py]

模块 trace module 允许你跟踪程序的执行过程，生成带注释的语句覆盖率列表，打印调用/被调用关系以及列出在程序运行期间执行过的函数。可以在其他程序或者命令行中使用它

参见

Coverage.py [https://coverage.readthedocs.io/]

A popular third-party coverage tool that provides HTML output along with advanced features such as branch coverage.

27.6.1. Command-Line Usage

The trace module can be invoked from the command line. It can be as simple as

python -m trace --count -C . somefile.py ...

The above will execute somefile.py and generate annotated listings of all Python modules imported during the execution into the current directory.

--help

Display usage and exit.

--version

Display the version of the module and exit.

27.6.1.1. Main options

At least one of the following options must be specified when invoking trace. The --listfuncs option is mutually exclusive with the --trace and --count options. When --listfuncs is provided, neither --count nor --trace are accepted, and vice versa.

-c, --count

Produce a set of annotated listing files upon program completion that shows how many times each statement was executed. See also --coverdir, --file and --no-report below.

-t, --trace

Display lines as they are executed.

-l, --listfuncs

Display the functions executed by running the program.

-r, --report

Produce an annotated list from an earlier program run that used the --count and --file option. This does not execute any code.

-T, --trackcalls

Display the calling relationships exposed by running the program.

27.6.1.2. Modifiers

-f, --file=<file>

Name of a file to accumulate counts over several tracing runs. Should be used with the --count option.

-C, --coverdir=<dir>

Directory where the report files go. The coverage report for package.module is written to file dir/package/module.cover.

-m, --missing

When generating annotated listings, mark lines which were not executed with >>>>>>.

-s, --summary

When using --count or --report, write a brief summary to stdout for each file processed.

-R, --no-report

Do not generate annotated listings. This is useful if you intend to make several runs with --count, and then produce a single set of annotated listings at the end.

-g, --timing

Prefix each line with the time since the program started. Only used while tracing.

27.6.1.3. Filters

These options may be repeated multiple times.

--ignore-module=<mod>

Ignore each of the given module names and its submodules (if it is a package). The argument can be a list of names separated by a comma.

--ignore-dir=<dir>

Ignore all modules and packages in the named directory and subdirectories. The argument can be a list of directories separated by os.pathsep.

27.6.2. 编程接口

class trace.Trace(count=1, trace=1, countfuncs=0, countcallers=0, ignoremods=(), ignoredirs=(), infile=None, outfile=None, timing=False)

Create an object to trace execution of a single statement or expression. All parameters are optional. count enables counting of line numbers. trace enables line execution tracing. countfuncs enables listing of the functions called during the run. countcallers enables call relationship tracking. ignoremods is a list of modules or packages to ignore. ignoredirs is a list of directories whose modules or packages should be ignored. infile is the name of the file from which to read stored count information. outfile is the name of the file in which to write updated count information. timing enables a timestamp relative to when tracing was started to be displayed.

run(cmd)

Execute the command and gather statistics from the execution with the current tracing parameters. cmd must be a string or code object, suitable for passing into exec().

runctx(cmd, globals=None, locals=None)

Execute the command and gather statistics from the execution with the current tracing parameters, in the defined global and local environments. If not defined, globals and locals default to empty dictionaries.

runfunc(func, *args, **kwds)

Call func with the given arguments under control of the Trace object with the current tracing parameters.

results()

Return a CoverageResults object that contains the cumulative results of all previous calls to run, runctx and runfunc for the given Trace instance. Does not reset the accumulated trace results.

class trace.CoverageResults

A container for coverage results, created by Trace.results(). Should not be created directly by the user.

update(other)

Merge in data from another CoverageResults object.

write_results(show_missing=True, summary=False, coverdir=None)

Write coverage results. Set show_missing to show lines that had no hits. Set summary to include in the output the coverage summary per module. coverdir specifies the directory into which the coverage result files will be output. If None, the results for each source file are placed in its directory.

A simple example demonstrating the use of the programmatic interface:

import sys
import trace

# create a Trace object, telling it what to ignore, and whether to
# do tracing or line-counting or both.
tracer = trace.Trace(
    ignoredirs=[sys.prefix, sys.exec_prefix],
    trace=0,
    count=1)

# run the new command using the given tracer
tracer.run('main()')

# make a report, placing output in the current directory
r = tracer.results()
r.write_results(show_missing=True, coverdir=".")

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

27. 调试和分析 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

27. 调试和分析 »

|

27.7. tracemalloc — 跟踪内存分配

3.4 新版功能.

源代码： Lib/tracemalloc.py [https://github.com/python/cpython/tree/3.6/Lib/tracemalloc.py]

The tracemalloc module is a debug tool to trace memory blocks allocated by Python. It provides the following information:

Traceback where an object was allocated

Statistics on allocated memory blocks per filename and per line number: total size, number and average size of allocated memory blocks

Compute the differences between two snapshots to detect memory leaks

To trace most memory blocks allocated by Python, the module should be started as early as possible by setting the PYTHONTRACEMALLOC environment variable to 1, or by using -X tracemalloc command line option. The tracemalloc.start() function can be called at runtime to start tracing Python memory allocations.

By default, a trace of an allocated memory block only stores the most recent frame (1 frame). To store 25 frames at startup: set the PYTHONTRACEMALLOC environment variable to 25, or use the -X tracemalloc=25 command line option.

27.7.1. 例子

27.7.1.1. 显示前10项

显示内存分配最多的10个文件：

import tracemalloc

tracemalloc.start()

# ... run your application ...

snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('lineno')

print("[ Top 10 ]")
for stat in top_stats[:10]:
    print(stat)

Python测试套件的输出示例：

[ Top 10 ]
<frozen importlib._bootstrap>:716: size=4855 KiB, count=39328, average=126 B
<frozen importlib._bootstrap>:284: size=521 KiB, count=3199, average=167 B
/usr/lib/python3.4/collections/__init__.py:368: size=244 KiB, count=2315, average=108 B
/usr/lib/python3.4/unittest/case.py:381: size=185 KiB, count=779, average=243 B
/usr/lib/python3.4/unittest/case.py:402: size=154 KiB, count=378, average=416 B
/usr/lib/python3.4/abc.py:133: size=88.7 KiB, count=347, average=262 B
<frozen importlib._bootstrap>:1446: size=70.4 KiB, count=911, average=79 B
<frozen importlib._bootstrap>:1454: size=52.0 KiB, count=25, average=2131 B
<string>:5: size=49.7 KiB, count=148, average=344 B
/usr/lib/python3.4/sysconfig.py:411: size=48.0 KiB, count=1, average=48.0 KiB

We can see that Python loaded 4855 KiB data (bytecode and constants) from modules and that the collections module allocated 244 KiB to build namedtuple types.

See Snapshot.statistics() for more options.

27.7.1.2. 计算差异

获取两个快照并显示差异：

import tracemalloc
tracemalloc.start()
# ... start your application ...

snapshot1 = tracemalloc.take_snapshot()
# ... call the function leaking memory ...
snapshot2 = tracemalloc.take_snapshot()

top_stats = snapshot2.compare_to(snapshot1, 'lineno')

print("[ Top 10 differences ]")
for stat in top_stats[:10]:
    print(stat)

Example of output before/after running some tests of the Python test suite:

[ Top 10 differences ]
<frozen importlib._bootstrap>:716: size=8173 KiB (+4428 KiB), count=71332 (+39369), average=117 B
/usr/lib/python3.4/linecache.py:127: size=940 KiB (+940 KiB), count=8106 (+8106), average=119 B
/usr/lib/python3.4/unittest/case.py:571: size=298 KiB (+298 KiB), count=589 (+589), average=519 B
<frozen importlib._bootstrap>:284: size=1005 KiB (+166 KiB), count=7423 (+1526), average=139 B
/usr/lib/python3.4/mimetypes.py:217: size=112 KiB (+112 KiB), count=1334 (+1334), average=86 B
/usr/lib/python3.4/http/server.py:848: size=96.0 KiB (+96.0 KiB), count=1 (+1), average=96.0 KiB
/usr/lib/python3.4/inspect.py:1465: size=83.5 KiB (+83.5 KiB), count=109 (+109), average=784 B
/usr/lib/python3.4/unittest/mock.py:491: size=77.7 KiB (+77.7 KiB), count=143 (+143), average=557 B
/usr/lib/python3.4/urllib/parse.py:476: size=71.8 KiB (+71.8 KiB), count=969 (+969), average=76 B
/usr/lib/python3.4/contextlib.py:38: size=67.2 KiB (+67.2 KiB), count=126 (+126), average=546 B

We can see that Python has loaded 8173 KiB of module data (bytecode and constants), and that this is 4428 KiB more than had been loaded before the tests, when the previous snapshot was taken. Similarly, the linecache module has cached 940 KiB of Python source code to format tracebacks, all of it since the previous snapshot.

If the system has little free memory, snapshots can be written on disk using the Snapshot.dump() method to analyze the snapshot offline. Then use the Snapshot.load() method reload the snapshot.

27.7.1.3. Get the traceback of a memory block

Code to display the traceback of the biggest memory block:

import tracemalloc

# Store 25 frames
tracemalloc.start(25)

# ... run your application ...

snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('traceback')

# pick the biggest memory block
stat = top_stats[0]
print("%s memory blocks: %.1f KiB" % (stat.count, stat.size / 1024))
for line in stat.traceback.format():
    print(line)

Example of output of the Python test suite (traceback limited to 25 frames):

903 memory blocks: 870.1 KiB
  File "<frozen importlib._bootstrap>", line 716
  File "<frozen importlib._bootstrap>", line 1036
  File "<frozen importlib._bootstrap>", line 934
  File "<frozen importlib._bootstrap>", line 1068
  File "<frozen importlib._bootstrap>", line 619
  File "<frozen importlib._bootstrap>", line 1581
  File "<frozen importlib._bootstrap>", line 1614
  File "/usr/lib/python3.4/doctest.py", line 101
    import pdb
  File "<frozen importlib._bootstrap>", line 284
  File "<frozen importlib._bootstrap>", line 938
  File "<frozen importlib._bootstrap>", line 1068
  File "<frozen importlib._bootstrap>", line 619
  File "<frozen importlib._bootstrap>", line 1581
  File "<frozen importlib._bootstrap>", line 1614
  File "/usr/lib/python3.4/test/support/__init__.py", line 1728
    import doctest
  File "/usr/lib/python3.4/test/test_pickletools.py", line 21
    support.run_doctest(pickletools)
  File "/usr/lib/python3.4/test/regrtest.py", line 1276
    test_runner()
  File "/usr/lib/python3.4/test/regrtest.py", line 976
    display_failure=not verbose)
  File "/usr/lib/python3.4/test/regrtest.py", line 761
    match_tests=ns.match_tests)
  File "/usr/lib/python3.4/test/regrtest.py", line 1563
    main()
  File "/usr/lib/python3.4/test/__main__.py", line 3
    regrtest.main_in_temp_cwd()
  File "/usr/lib/python3.4/runpy.py", line 73
    exec(code, run_globals)
  File "/usr/lib/python3.4/runpy.py", line 160
    "__main__", fname, loader, pkg_name)

We can see that the most memory was allocated in the importlib module to load data (bytecode and constants) from modules: 870.1 KiB. The traceback is where the importlib loaded data most recently: on the import pdb line of the doctest module. The traceback may change if a new module is loaded.

27.7.1.4. Pretty top

Code to display the 10 lines allocating the most memory with a pretty output, ignoring <frozen importlib._bootstrap> and <unknown> files:

import linecache
import os
import tracemalloc

def display_top(snapshot, key_type='lineno', limit=10):
    snapshot = snapshot.filter_traces((
        tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
        tracemalloc.Filter(False, "<unknown>"),
    ))
    top_stats = snapshot.statistics(key_type)

    print("Top %s lines" % limit)
    for index, stat in enumerate(top_stats[:limit], 1):
        frame = stat.traceback[0]
        # replace "/path/to/module/file.py" with "module/file.py"
        filename = os.sep.join(frame.filename.split(os.sep)[-2:])
        print("#%s: %s:%s: %.1f KiB"
              % (index, filename, frame.lineno, stat.size / 1024))
        line = linecache.getline(frame.filename, frame.lineno).strip()
        if line:
            print('    %s' % line)

    other = top_stats[limit:]
    if other:
        size = sum(stat.size for stat in other)
        print("%s other: %.1f KiB" % (len(other), size / 1024))
    total = sum(stat.size for stat in top_stats)
    print("Total allocated size: %.1f KiB" % (total / 1024))

tracemalloc.start()

# ... run your application ...

snapshot = tracemalloc.take_snapshot()
display_top(snapshot)

Python测试套件的输出示例：

Top 10 lines
#1: Lib/base64.py:414: 419.8 KiB
    _b85chars2 = [(a + b) for a in _b85chars for b in _b85chars]
#2: Lib/base64.py:306: 419.8 KiB
    _a85chars2 = [(a + b) for a in _a85chars for b in _a85chars]
#3: collections/__init__.py:368: 293.6 KiB
    exec(class_definition, namespace)
#4: Lib/abc.py:133: 115.2 KiB
    cls = super().__new__(mcls, name, bases, namespace)
#5: unittest/case.py:574: 103.1 KiB
    testMethod()
#6: Lib/linecache.py:127: 95.4 KiB
    lines = fp.readlines()
#7: urllib/parse.py:476: 71.8 KiB
    for a in _hexdig for b in _hexdig}
#8: <string>:5: 62.0 KiB
#9: Lib/_weakrefset.py:37: 60.0 KiB
    self.data = set()
#10: Lib/base64.py:142: 59.8 KiB
    _b32tab2 = [a + b for a in _b32tab for b in _b32tab]
6220 other: 3602.8 KiB
Total allocated size: 5303.1 KiB

See Snapshot.statistics() for more options.

27.7.2. API

27.7.2.1. 函数

tracemalloc.clear_traces()

Clear traces of memory blocks allocated by Python.

See also stop().

tracemalloc.get_object_traceback(obj)

Get the traceback where the Python object obj was allocated. Return a Traceback instance, or None if the tracemalloc module is not tracing memory allocations or did not trace the allocation of the object.

See also gc.get_referrers() and sys.getsizeof() functions.

tracemalloc.get_traceback_limit()

Get the maximum number of frames stored in the traceback of a trace.

The tracemalloc module must be tracing memory allocations to get the limit, otherwise an exception is raised.

The limit is set by the start() function.

tracemalloc.get_traced_memory()

Get the current size and peak size of memory blocks traced by the tracemalloc module as a tuple: (current: int, peak: int).

tracemalloc.get_tracemalloc_memory()

Get the memory usage in bytes of the tracemalloc module used to store traces of memory blocks. Return an int.

tracemalloc.is_tracing()

True if the tracemalloc module is tracing Python memory allocations, False otherwise.

See also start() and stop() functions.

tracemalloc.start(nframe: int=1)

Start tracing Python memory allocations: install hooks on Python memory allocators. Collected tracebacks of traces will be limited to nframe frames. By default, a trace of a memory block only stores the most recent frame: the limit is 1. nframe must be greater or equal to 1.

Storing more than 1 frame is only useful to compute statistics grouped by 'traceback' or to compute cumulative statistics: see the Snapshot.compare_to() and Snapshot.statistics() methods.

Storing more frames increases the memory and CPU overhead of the tracemalloc module. Use the get_tracemalloc_memory() function to measure how much memory is used by the tracemalloc module.

The PYTHONTRACEMALLOC environment variable (PYTHONTRACEMALLOC=NFRAME) and the -X tracemalloc=NFRAME command line option can be used to start tracing at startup.

See also stop(), is_tracing() and get_traceback_limit() functions.

tracemalloc.stop()

Stop tracing Python memory allocations: uninstall hooks on Python memory allocators. Also clears all previously collected traces of memory blocks allocated by Python.

Call take_snapshot() function to take a snapshot of traces before clearing them.

See also start(), is_tracing() and clear_traces() functions.

tracemalloc.take_snapshot()

Take a snapshot of traces of memory blocks allocated by Python. Return a new Snapshot instance.

The snapshot does not include memory blocks allocated before the tracemalloc module started to trace memory allocations.

Tracebacks of traces are limited to get_traceback_limit() frames. Use the nframe parameter of the start() function to store more frames.

The tracemalloc module must be tracing memory allocations to take a snapshot, see the start() function.

See also the get_object_traceback() function.

27.7.2.2. 域过滤器

class tracemalloc.DomainFilter(inclusive: bool, domain: int)

Filter traces of memory blocks by their address space (domain).

3.6 新版功能.

inclusive

If inclusive is True (include), match memory blocks allocated in the address space domain.

If inclusive is False (exclude), match memory blocks not allocated in the address space domain.

domain

Address space of a memory block (int). Read-only property.

27.7.2.3. 过滤器

class tracemalloc.Filter(inclusive: bool, filename_pattern: str, lineno: int=None, all_frames: bool=False, domain: int=None)

对内存块的跟踪进行筛选。

See the fnmatch.fnmatch() function for the syntax of filename_pattern. The '.pyc' file extension is replaced with '.py'.

示例：

Filter(True, subprocess.__file__) only includes traces of the subprocess module

Filter(False, tracemalloc.__file__) excludes traces of the tracemalloc module

Filter(False, "<unknown>") excludes empty tracebacks

在 3.5 版更改: The '.pyo' file extension is no longer replaced with '.py'.

在 3.6 版更改: Added the domain attribute.

domain

Address space of a memory block (int or None).

inclusive

If inclusive is True (include), only match memory blocks allocated in a file with a name matching filename_pattern at line number lineno.

If inclusive is False (exclude), ignore memory blocks allocated in a file with a name matching filename_pattern at line number lineno.

lineno

Line number (int) of the filter. If lineno is None, the filter matches any line number.

filename_pattern

Filename pattern of the filter (str). Read-only property.

all_frames

If all_frames is True, all frames of the traceback are checked. If all_frames is False, only the most recent frame is checked.

This attribute has no effect if the traceback limit is 1. See the get_traceback_limit() function and Snapshot.traceback_limit attribute.

27.7.2.4. Frame

class tracemalloc.Frame

Frame of a traceback.

The Traceback class is a sequence of Frame instances.

filename

文件名(str).

lineno

行号 (int).

27.7.2.5. 快照

class tracemalloc.Snapshot

Snapshot of traces of memory blocks allocated by Python.

The take_snapshot() function creates a snapshot instance.

compare_to(old_snapshot: Snapshot, key_type: str, cumulative: bool=False)

Compute the differences with an old snapshot. Get statistics as a sorted list of StatisticDiff instances grouped by key_type.

See the Snapshot.statistics() method for key_type and cumulative parameters.

The result is sorted from the biggest to the smallest by: absolute value of StatisticDiff.size_diff, StatisticDiff.size, absolute value of StatisticDiff.count_diff, Statistic.count and then by StatisticDiff.traceback.

dump(filename)

将快照写入文件

使用 load() 重载快照。

filter_traces(filters)

Create a new Snapshot instance with a filtered traces sequence, filters is a list of DomainFilter and Filter instances. If filters is an empty list, return a new Snapshot instance with a copy of the traces.

All inclusive filters are applied at once, a trace is ignored if no inclusive filters match it. A trace is ignored if at least one exclusive filter matches it.

在 3.6 版更改: DomainFilter instances are now also accepted in filters.

classmethod load(filename)

从文件载入快照。

另见 dump().

statistics(key_type: str, cumulative: bool=False)

获取 Statistic 信息列表，按 key_type 分组排序：

key_type

描述

'filename'

filename

'lineno'

文件名和行号

'traceback'

回溯

If cumulative is True, cumulate size and count of memory blocks of all frames of the traceback of a trace, not only the most recent frame. The cumulative mode can only be used with key_type equals to 'filename' and 'lineno'.

The result is sorted from the biggest to the smallest by: Statistic.size, Statistic.count and then by Statistic.traceback.

traceback_limit

Maximum number of frames stored in the traceback of traces: result of the get_traceback_limit() when the snapshot was taken.

traces

Traces of all memory blocks allocated by Python: sequence of Trace instances.

The sequence has an undefined order. Use the Snapshot.statistics() method to get a sorted list of statistics.

27.7.2.6. 统计

class tracemalloc.Statistic

统计内存分配

Snapshot.statistics() 返回 Statistic 实例的列表。.

参见 StatisticDiff 类。

count

内存块数（整形）。

size

Total size of memory blocks in bytes (int).

traceback

Traceback where the memory block was allocated, Traceback instance.

27.7.2.7. StatisticDiff

class tracemalloc.StatisticDiff

Statistic difference on memory allocations between an old and a new Snapshot instance.

Snapshot.compare_to() returns a list of StatisticDiff instances. See also the Statistic class.

count

Number of memory blocks in the new snapshot (int): 0 if the memory blocks have been released in the new snapshot.

count_diff

Difference of number of memory blocks between the old and the new snapshots (int): 0 if the memory blocks have been allocated in the new snapshot.

size

Total size of memory blocks in bytes in the new snapshot (int): 0 if the memory blocks have been released in the new snapshot.

size_diff

Difference of total size of memory blocks in bytes between the old and the new snapshots (int): 0 if the memory blocks have been allocated in the new snapshot.

traceback

Traceback where the memory blocks were allocated, Traceback instance.

27.7.2.8. 跟踪

class tracemalloc.Trace

Trace of a memory block.

The Snapshot.traces attribute is a sequence of Trace instances.

size

Size of the memory block in bytes (int).

traceback

Traceback where the memory block was allocated, Traceback instance.

27.7.2.9. 回溯

class tracemalloc.Traceback

Sequence of Frame instances sorted from the most recent frame to the oldest frame.

A traceback contains at least 1 frame. If the tracemalloc module failed to get a frame, the filename "<unknown>" at line number 0 is used.

When a snapshot is taken, tracebacks of traces are limited to get_traceback_limit() frames. See the take_snapshot() function.

The Trace.traceback attribute is an instance of Traceback instance.

format(limit=None)

Format the traceback as a list of lines with newlines. Use the linecache module to retrieve lines from the source code. If limit is set, only format the limit most recent frames.

Similar to the traceback.format_tb() function, except that format() does not include newlines.

示例:

print("Traceback (most recent call first):")
for line in traceback:
    print(line)

输出:

Traceback (most recent call first):
  File "test.py", line 9
    obj = Object()
  File "test.py", line 12
    tb = tracemalloc.get_object_traceback(f())

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

27. 调试和分析 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

28. 软件打包和分发

这些库可帮助你发布和安装 Python 软件。 虽然这些模块设计为与`Python 包索引 <https://pypi.org>`__结合使用，但它们也可以与本地索引服务器一起使用，或者根本不使用任何索引服务器。

28.1. distutils — 构建和安装 Python 模块

28.2. ensurepip — Bootstrapping the pip installer

28.2.1. Command line interface

28.2.2. Module API

28.3. venv — 创建虚拟环境

28.3.1. 创建虚拟环境

28.3.2. API

28.3.3. 一个扩展 EnvBuilder 的例子

28.4. zipapp — Manage executable python zip archives

28.4.1. Basic Example

28.4.2. 命令行界面

28.4.3. Python API

28.4.4. 例子

28.4.5. Specifying the Interpreter

28.4.6. Creating Standalone Applications with zipapp

28.4.6.1. Making a Windows executable

28.4.6.2. Caveats

28.4.7. The Python Zip Application Archive Format

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

28. 软件打包和分发 »

|

28.1. distutils — 构建和安装 Python 模块

distutils 包为将待构建和安装的额外的模块，打包成 Python 安装包提供支持。新模块既可以是百分百的纯 Python，也可以是用 C 写的扩展模块，或者可以是一组包含了同时用 Python 和 C 编码的 Python 包。

大多数 Python 用户 不会 想要直接使用这个包，而是使用 Python 包官方维护的跨版本工具。特别地， setuptools [https://setuptools.readthedocs.io/en/latest/] 是一个对于提供的 distutils 的增强选项。

对声明项目依赖的支持。

额外的用于配置哪些文件包含在源代码发布中的装置（包括与版本控制系统集成需要的插件）

生成项目“进入点”的能力，进入点可用作应用插件系统的基础

自动在安装时间生成 Windows 命令行可执行文件的能力，而不是需要预编译它们

跨所有受支持的 Python 版本上的一致的表现

推荐的 pip [https://pip.pypa.io/] 安装器用 setuptools 运行所有的 setup.py 脚本，即使脚本本身只引了 distutils 包。参考 Python Packaging User Guide [https://packaging.python.org]  获得更多信息。

为了打包工具的作者和用户能更好理解当前的打包和分发系统，遗留的基于 distutils 的用户文档和 API 参考保持可用：

安装Python模块（旧版）

分发 Python 模块（遗留版本）

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

28. 软件打包和分发 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

28. 软件打包和分发 »

|

28.2. ensurepip — Bootstrapping the pip installer

3.4 新版功能.

The ensurepip package provides support for bootstrapping the pip installer into an existing Python installation or virtual environment. This bootstrapping approach reflects the fact that pip is an independent project with its own release cycle, and the latest available stable version is bundled with maintenance and feature releases of the CPython reference interpreter.

In most cases, end users of Python shouldn’t need to invoke this module directly (as pip should be bootstrapped by default), but it may be needed if installing pip was skipped when installing Python (or when creating a virtual environment) or after explicitly uninstalling pip.

注解

This module does not access the internet. All of the components needed to bootstrap pip are included as internal parts of the package.

参见

安装 Python 模块

The end user guide for installing Python packages

PEP 453 [https://www.python.org/dev/peps/pep-0453]: Explicit bootstrapping of pip in Python installations

The original rationale and specification for this module.

28.2.1. Command line interface

The command line interface is invoked using the interpreter’s -m switch.

The simplest possible invocation is:

python -m ensurepip

This invocation will install pip if it is not already installed, but otherwise does nothing. To ensure the installed version of pip is at least as recent as the one bundled with ensurepip, pass the --upgrade option:

python -m ensurepip --upgrade

By default, pip is installed into the current virtual environment (if one is active) or into the system site packages (if there is no active virtual environment). The installation location can be controlled through two additional command line options:

--root <dir>: Installs pip relative to the given root directory rather than the root of the currently active virtual environment (if any) or the default root for the current Python installation.

--user: Installs pip into the user site packages directory rather than globally for the current Python installation (this option is not permitted inside an active virtual environment).

By default, the scripts pipX and pipX.Y will be installed (where X.Y stands for the version of Python used to invoke ensurepip). The scripts installed can be controlled through two additional command line options:

--altinstall: if an alternate installation is requested, the pipX script will not be installed.

--default-pip: if a “default pip” installation is requested, the

pip script will be installed in addition to the two regular scripts.

Providing both of the script selection options will trigger an exception.

在 3.6.3 版更改: The exit status is non-zero if the command fails.

28.2.2. Module API

ensurepip exposes two functions for programmatic use:

ensurepip.version()

Returns a string specifying the bundled version of pip that will be installed when bootstrapping an environment.

ensurepip.bootstrap(root=None, upgrade=False, user=False, altinstall=False, default_pip=False, verbosity=0)

Bootstraps pip into the current or designated environment.

root specifies an alternative root directory to install relative to. If root is None, then installation uses the default install location for the current environment.

upgrade indicates whether or not to upgrade an existing installation of an earlier version of pip to the bundled version.

user indicates whether to use the user scheme rather than installing globally.

By default, the scripts pipX and pipX.Y will be installed (where X.Y stands for the current version of Python).

If altinstall is set, then pipX will not be installed.

If default_pip is set, then pip will be installed in addition to the two regular scripts.

Setting both altinstall and default_pip will trigger ValueError.

verbosity controls the level of output to sys.stdout from the bootstrapping operation.

注解

The bootstrapping process has side effects on both sys.path and os.environ. Invoking the command line interface in a subprocess instead allows these side effects to be avoided.

注解

The bootstrapping process may install additional modules required by pip, but other software should not assume those dependencies will always be present by default (as the dependencies may be removed in a future version of pip).

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

28. 软件打包和分发 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

28. 软件打包和分发 »

|

28.3. venv — 创建虚拟环境

3.3 新版功能.

源码： Lib/venv/ [https://github.com/python/cpython/tree/3.6/Lib/venv/]

The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (allowing creation of environments with various Python versions) and can have its own independent set of installed Python packages in its site directories.

有关 Python 虚拟环境的更多信息，请参阅 PEP 405 [https://www.python.org/dev/peps/pep-0405] 。

注解

从 Python 3.6 开始，不推荐使用 pyvenv 脚本，而是使用 python3 -m venv 来帮助防止任何关于虚拟环境将基于哪个 Python 解释器的混淆。

28.3.1. 创建虚拟环境

通过执行 venv 指令来创建一个 虚拟环境:

python3 -m venv /path/to/new/virtual/environment

Running this command creates the target directory (creating any parent directories that don’t exist already) and places a pyvenv.cfg file in it with a home key pointing to the Python installation from which the command was run. It also creates a bin (or Scripts on Windows) subdirectory containing a copy/symlink of the Python binary/binaries (as appropriate for the platform or arguments used at environment creation time). It also creates an (initially empty) lib/pythonX.Y/site-packages subdirectory (on Windows, this is Lib\site-packages). If an existing directory is specified, it will be re-used.

3.6 版后已移除: pyvenv 是 Python 3.3 和 3.4 中创建虚拟环境的推荐工具，不过 在 Python 3.6 中已弃用 [https://docs.python.org/dev/whatsnew/3.6.html#deprecated-features]。

在 3.5 版更改: 现在推荐使用 venv 来创建虚拟环境。

参见

Python 软件包用户指南：创建和使用虚拟环境 [https://packaging.python.org/installing/#creating-virtual-environments]

在 Windows 上，调用 venv 命令如下:

c:\>c:\Python35\python -m venv c:\path\to\myenv

或者，如果已经为 Python 安装 配置好 PATH 和 PATHEXT 变量:

c:\>python -m venv c:\path\to\myenv

本命令如果以 -h 参数运行，将显示可用的选项:

usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
            [--upgrade] [--without-pip]
            ENV_DIR [ENV_DIR ...]

Creates virtual Python environments in one or more target directories.

positional arguments:
  ENV_DIR               A directory to create the environment in.

optional arguments:
  -h, --help            show this help message and exit
  --system-site-packages
                        Give the virtual environment access to the system
                        site-packages dir.
  --symlinks            Try to use symlinks rather than copies, when symlinks
                        are not the default for the platform.
  --copies              Try to use copies rather than symlinks, even when
                        symlinks are the default for the platform.
  --clear               Delete the contents of the environment directory if it
                        already exists, before environment creation.
  --upgrade             Upgrade the environment directory to use this version
                        of Python, assuming Python has been upgraded in-place.
  --without-pip         Skips installing or upgrading pip in the virtual
                        environment (pip is bootstrapped by default)

Once an environment has been created, you may wish to activate it, e.g. by
sourcing an activate script in its bin directory.

在 3.4 版更改: 默认安装 pip，并添加 --without-pip 和 --copies 选项

在 3.4 版更改: 在早期版本中，如果目标目录已存在，将引发错误，除非使用了 --clear 或 --upgrade 选项。

生成的 pyvenv.cfg 文件还包括 include-system-site-packages 键，如果运行 venv 带有 --system-site-packages 选项，则键值为 true，否则为 false。

除非采用 --without-pip 选项，否则将会调用 ensurepip 将 pip 引导到虚拟环境中。

可以向 venv 传入多个路径，此时将根据给定的选项，在所给的每个路径上创建相同的虚拟环境。

创建虚拟环境后，可以使用虚拟环境的二进制目录中的脚本来“激活”该环境。不同平台调用的脚本是不同的（须将 <venv> 替换为包含虚拟环境的目录路径）：

平台

Shell

用于激活虚拟环境的命令

Posix

bash/zsh

$ source <venv>/bin/activate

fish

$ . <venv>/bin/activate.fish

csh/tcsh

$ source <venv>/bin/activate.csh

Windows

cmd.exe

C:\> <venv>\Scripts\activate.bat

PowerShell

PS C:\> <venv>\Scripts\Activate.ps1

激活环境不是 必须 的，激活只是将虚拟环境的二进制目录添加到搜索路径中，这样 “python” 命令将调用虚拟环境的 Python 解释器，可以运行其中已安装的脚本，而不必输入其完整路径。但是，安装在虚拟环境中的所有脚本都应在不激活的情况下可运行，并自动与虚拟环境的 Python 一起运行。

You can deactivate a virtual environment by typing “deactivate” in your shell. The exact mechanism is platform-specific: for example, the Bash activation script defines a “deactivate” function, whereas on Windows there are separate scripts called deactivate.bat and Deactivate.ps1 which are installed when the virtual environment is created.

3.4 新版功能: fish 和 csh 激活脚本。

注解

虚拟环境是一个 Python 环境，安装到其中的 Python 解释器、库和脚本与其他虚拟环境中的内容是隔离的，且（默认）与“系统级” Python（操作系统的一部分）中安装的库是隔离的。

虚拟环境是一个目录树，其中包含 Python 可执行文件和其他文件，其他文件指示了这是一个是虚拟环境。

Common installation tools such as Setuptools and pip work as expected with virtual environments. In other words, when a virtual environment is active, they install Python packages into the virtual environment without needing to be told to do so explicitly.

当虚拟环境被激活（即虚拟环境的 Python 解释器正在运行），属性 sys.prefix 和 sys.exec_prefix 指向的是虚拟环境的基础目录，而 sys.base_prefix 和 sys.base_exec_prefix 指向非虚拟环境的 Python 安装，即曾用于创建虚拟环境的那个 Python 安装。如果虚拟环境没有被激活，则 sys.prefix 与 sys.base_prefix 相同，且 sys.exec_prefix 与 sys.base_exec_prefix 相同（它们均指向非虚拟环境的 Python 安装）。

When a virtual environment is active, any options that change the installation path will be ignored from all distutils configuration files to prevent projects being inadvertently installed outside of the virtual environment.

When working in a command shell, users can make a virtual environment active by running an activate script in the virtual environment’s executables directory (the precise filename is shell-dependent), which prepends the virtual environment’s directory for executables to the PATH environment variable for the running shell. There should be no need in other circumstances to activate a virtual environment—scripts installed into virtual environments have a “shebang” line which points to the virtual environment’s Python interpreter. This means that the script will run with that interpreter regardless of the value of PATH. On Windows, “shebang” line processing is supported if you have the Python Launcher for Windows installed (this was added to Python in 3.3 - see PEP 397 [https://www.python.org/dev/peps/pep-0397] for more details). Thus, double-clicking an installed script in a Windows Explorer window should run the script with the correct interpreter without there needing to be any reference to its virtual environment in PATH.

28.3.2. API

上述的高级方法使用了一个简单的 API，该 API 提供了一种机制，第三方虚拟环境创建者可以根据其需求自定义环境创建过程，该 API 为 EnvBuilder 类。

class venv.EnvBuilder(system_site_packages=False, clear=False, symlinks=False, upgrade=False, with_pip=False, prompt=None)

EnvBuilder 类在实例化时接受以下关键字参数：

system_site_packages – 一个布尔值，要求系统 Python 的 site-packages 对环境可用（默认为 False）。

clear – 一个布尔值，如果为 true，则在创建环境前将删除目标目录的现有内容。

symlinks – a Boolean value indicating whether to attempt to symlink the Python binary (and any necessary DLLs or other binaries, e.g. pythonw.exe), rather than copying.

upgrade – 一个布尔值，如果为 true，则将使用当前运行的 Python 去升级一个现有的环境，这主要在原位置的 Python 更新后使用（默认为 False）。

with_pip – 一个布尔值，如果为 true，则确保在虚拟环境中已安装 pip。这使用的是带有 --default-pip 选项的 ensurepip。

prompt – a String to be used after virtual environment is activated (defaults to None which means directory name of the environment would be used).

在 3.4 版更改: 添加 with_pip 参数

3.6 新版功能: 添加 prompt 参数

Creators of third-party virtual environment tools will be free to use the provided EnvBuilder class as a base class.

返回的 env-builder 是一个对象，包含一个 create 方法：

create(env_dir)

This method takes as required argument the path (absolute or relative to the current directory) of the target directory which is to contain the virtual environment. The create method will either create the environment in the specified directory, or raise an appropriate exception.

The create method of the EnvBuilder class illustrates the hooks available for subclass customization:

def create(self, env_dir):
    """
    Create a virtualized Python environment in a directory.
    env_dir is the target directory to create an environment in.
    """
    env_dir = os.path.abspath(env_dir)
    context = self.ensure_directories(env_dir)
    self.create_configuration(context)
    self.setup_python(context)
    self.setup_scripts(context)
    self.post_setup(context)

每个方法 ensure_directories(), create_configuration(), setup_python(), setup_scripts() 和 post_setup() 都可以被重写。

ensure_directories(env_dir)

创建环境目录和所有必需的目录，并返回一个上下文对象。该对象只是一个容器，保存属性（如路径），供其他方法使用。允许目录已经存在，如果指定了 clear 或 upgrade 就允许在现有环境目录上进行操作。

create_configuration(context)

在环境中创建 pyvenv.cfg 配置文件。

setup_python(context)

Creates a copy of the Python executable (and, under Windows, DLLs) in the environment. On a POSIX system, if a specific executable python3.x was used, symlinks to python and python3 will be created pointing to that executable, unless files with those names already exist.

setup_scripts(context)

将适用于平台的激活脚本安装到虚拟环境中。

post_setup(context)

占位方法，可以在第三方实现中重写，用于在虚拟环境中预安装软件包，或是其他创建后要执行的步骤。

此外，EnvBuilder 提供了如下实用方法，可以从子类的 setup_scripts() 或 post_setup() 调用，用来将自定义脚本安装到虚拟环境中。

install_scripts(context, path)

path 是一个目录的路径，该目录应包含子目录 “common”, “posix”, “nt”，每个子目录存有发往对应环境中 bin 目录的脚本。在下列占位符替换完毕后，将复制 “common” 的内容和与 os.name 对应的子目录：

__VENV_DIR__ 会被替换为环境目录的绝对路径。

__VENV_NAME__ 会被替换为环境名称（环境目录的最后一个字段）。

__VENV_PROMPT__ 会被替换为提示符（用括号括起来的环境名称紧跟着一个空格）。

__VENV_BIN_NAME__ 会被替换为 bin 目录的名称（ bin 或 Scripts ）。

__VENV_PYTHON__ 会被替换为环境可执行文件的绝对路径。

允许目录已存在（用于升级现有环境时）。

有一个方便实用的模块级别的函数:

venv.create(env_dir, system_site_packages=False, clear=False, symlinks=False, with_pip=False)

通过关键词参数来创建一个 EnvBuilder，并且使用 env_dir 参数来调用它的 create() 方法。

在 3.4 版更改: 添加 with_pip 参数

28.3.3. 一个扩展 EnvBuilder 的例子

下面的脚本展示了如何通过实现一个子类来扩展 EnvBuilder。这个子类会安装 setuptotols 和 pip 的到被创建的虚拟环境中。

import os
import os.path
from subprocess import Popen, PIPE
import sys
from threading import Thread
from urllib.parse import urlparse
from urllib.request import urlretrieve
import venv

class ExtendedEnvBuilder(venv.EnvBuilder):
    """
    This builder installs setuptools and pip so that you can pip or
    easy_install other packages into the created virtual environment.

    :param nodist: If True, setuptools and pip are not installed into the
                   created virtual environment.
    :param nopip: If True, pip is not installed into the created
                  virtual environment.
    :param progress: If setuptools or pip are installed, the progress of the
                     installation can be monitored by passing a progress
                     callable. If specified, it is called with two
                     arguments: a string indicating some progress, and a
                     context indicating where the string is coming from.
                     The context argument can have one of three values:
                     'main', indicating that it is called from virtualize()
                     itself, and 'stdout' and 'stderr', which are obtained
                     by reading lines from the output streams of a subprocess
                     which is used to install the app.

                     If a callable is not specified, default progress
                     information is output to sys.stderr.
    """

    def __init__(self, *args, **kwargs):
        self.nodist = kwargs.pop('nodist', False)
        self.nopip = kwargs.pop('nopip', False)
        self.progress = kwargs.pop('progress', None)
        self.verbose = kwargs.pop('verbose', False)
        super().__init__(*args, **kwargs)

    def post_setup(self, context):
        """
        Set up any packages which need to be pre-installed into the
        virtual environment being created.

        :param context: The information for the virtual environment
                        creation request being processed.
        """
        os.environ['VIRTUAL_ENV'] = context.env_dir
        if not self.nodist:
            self.install_setuptools(context)
        # Can't install pip without setuptools
        if not self.nopip and not self.nodist:
            self.install_pip(context)

    def reader(self, stream, context):
        """
        Read lines from a subprocess' output stream and either pass to a progress
        callable (if specified) or write progress information to sys.stderr.
        """
        progress = self.progress
        while True:
            s = stream.readline()
            if not s:
                break
            if progress is not None:
                progress(s, context)
            else:
                if not self.verbose:
                    sys.stderr.write('.')
                else:
                    sys.stderr.write(s.decode('utf-8'))
                sys.stderr.flush()
        stream.close()

    def install_script(self, context, name, url):
        _, _, path, _, _, _ = urlparse(url)
        fn = os.path.split(path)[-1]
        binpath = context.bin_path
        distpath = os.path.join(binpath, fn)
        # Download script into the virtual environment's binaries folder
        urlretrieve(url, distpath)
        progress = self.progress
        if self.verbose:
            term = '\n'
        else:
            term = ''
        if progress is not None:
            progress('Installing %s ...%s' % (name, term), 'main')
        else:
            sys.stderr.write('Installing %s ...%s' % (name, term))
            sys.stderr.flush()
        # Install in the virtual environment
        args = [context.env_exe, fn]
        p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=binpath)
        t1 = Thread(target=self.reader, args=(p.stdout, 'stdout'))
        t1.start()
        t2 = Thread(target=self.reader, args=(p.stderr, 'stderr'))
        t2.start()
        p.wait()
        t1.join()
        t2.join()
        if progress is not None:
            progress('done.', 'main')
        else:
            sys.stderr.write('done.\n')
        # Clean up - no longer needed
        os.unlink(distpath)

    def install_setuptools(self, context):
        """
        Install setuptools in the virtual environment.

        :param context: The information for the virtual environment
                        creation request being processed.
        """
        url = 'https://bitbucket.org/pypa/setuptools/downloads/ez_setup.py'
        self.install_script(context, 'setuptools', url)
        # clear up the setuptools archive which gets downloaded
        pred = lambda o: o.startswith('setuptools-') and o.endswith('.tar.gz')
        files = filter(pred, os.listdir(context.bin_path))
        for f in files:
            f = os.path.join(context.bin_path, f)
            os.unlink(f)

    def install_pip(self, context):
        """
        Install pip in the virtual environment.

        :param context: The information for the virtual environment
                        creation request being processed.
        """
        url = 'https://raw.github.com/pypa/pip/master/contrib/get-pip.py'
        self.install_script(context, 'pip', url)

def main(args=None):
    compatible = True
    if sys.version_info < (3, 3):
        compatible = False
    elif not hasattr(sys, 'base_prefix'):
        compatible = False
    if not compatible:
        raise ValueError('This script is only for use with '
                         'Python 3.3 or later')
    else:
        import argparse

        parser = argparse.ArgumentParser(prog=__name__,
                                         description='Creates virtual Python '
                                                     'environments in one or '
                                                     'more target '
                                                     'directories.')
        parser.add_argument('dirs', metavar='ENV_DIR', nargs='+',
                            help='A directory in which to create the
                                 'virtual environment.')
        parser.add_argument('--no-setuptools', default=False,
                            action='store_true', dest='nodist',
                            help="Don't install setuptools or pip in the "
                                 "virtual environment.")
        parser.add_argument('--no-pip', default=False,
                            action='store_true', dest='nopip',
                            help="Don't install pip in the virtual "
                                 "environment.")
        parser.add_argument('--system-site-packages', default=False,
                            action='store_true', dest='system_site',
                            help='Give the virtual environment access to the '
                                 'system site-packages dir.')
        if os.name == 'nt':
            use_symlinks = False
        else:
            use_symlinks = True
        parser.add_argument('--symlinks', default=use_symlinks,
                            action='store_true', dest='symlinks',
                            help='Try to use symlinks rather than copies, '
                                 'when symlinks are not the default for '
                                 'the platform.')
        parser.add_argument('--clear', default=False, action='store_true',
                            dest='clear', help='Delete the contents of the '
                                               'virtual environment '
                                               'directory if it already '
                                               'exists, before virtual '
                                               'environment creation.')
        parser.add_argument('--upgrade', default=False, action='store_true',
                            dest='upgrade', help='Upgrade the virtual '
                                                 'environment directory to '
                                                 'use this version of '
                                                 'Python, assuming Python '
                                                 'has been upgraded '
                                                 'in-place.')
        parser.add_argument('--verbose', default=False, action='store_true',
                            dest='verbose', help='Display the output '
                                               'from the scripts which '
                                               'install setuptools and pip.')
        options = parser.parse_args(args)
        if options.upgrade and options.clear:
            raise ValueError('you cannot supply --upgrade and --clear together.')
        builder = ExtendedEnvBuilder(system_site_packages=options.system_site,
                                       clear=options.clear,
                                       symlinks=options.symlinks,
                                       upgrade=options.upgrade,
                                       nodist=options.nodist,
                                       nopip=options.nopip,
                                       verbose=options.verbose)
        for d in options.dirs:
            builder.create(d)

if __name__ == '__main__':
    rc = 1
    try:
        main()
        rc = 0
    except Exception as e:
        print('Error: %s' % e, file=sys.stderr)
    sys.exit(rc)

This script is also available for download online [https://gist.github.com/4673395].

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

28. 软件打包和分发 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

28. 软件打包和分发 »

|

28.4. zipapp — Manage executable python zip archives

3.5 新版功能.

Source code: Lib/zipapp.py [https://github.com/python/cpython/tree/3.6/Lib/zipapp.py]

This module provides tools to manage the creation of zip files containing Python code, which can be executed directly by the Python interpreter. The module provides both a 命令行界面 and a Python API.

28.4.1. Basic Example

The following example shows how the 命令行界面 can be used to create an executable archive from a directory containing Python code. When run, the archive will execute the main function from the module myapp in the archive.

$ python -m zipapp myapp -m "myapp:main"
$ python myapp.pyz
<output from myapp>

28.4.2. 命令行界面

When called as a program from the command line, the following form is used:

$ python -m zipapp source [options]

If source is a directory, this will create an archive from the contents of source. If source is a file, it should be an archive, and it will be copied to the target archive (or the contents of its shebang line will be displayed if the –info option is specified).

The following options are understood:

-o <output>, --output=<output>

Write the output to a file named output. If this option is not specified, the output filename will be the same as the input source, with the extension .pyz added. If an explicit filename is given, it is used as is (so a .pyz extension should be included if required).

An output filename must be specified if the source is an archive (and in that case, output must not be the same as source).

-p <interpreter>, --python=<interpreter>

Add a #! line to the archive specifying interpreter as the command to run. Also, on POSIX, make the archive executable. The default is to write no #! line, and not make the file executable.

-m <mainfn>, --main=<mainfn>

Write a __main__.py file to the archive that executes mainfn. The mainfn argument should have the form “pkg.mod:fn”, where “pkg.mod” is a package/module in the archive, and “fn” is a callable in the given module. The __main__.py file will execute that callable.

--main cannot be specified when copying an archive.

--info

Display the interpreter embedded in the archive, for diagnostic purposes. In this case, any other options are ignored and SOURCE must be an archive, not a directory.

-h, --help

Print a short usage message and exit.

28.4.3. Python API

The module defines two convenience functions:

zipapp.create_archive(source, target=None, interpreter=None, main=None)

Create an application archive from source. The source can be any of the following:

The name of a directory, or a pathlib.Path object referring to a directory, in which case a new application archive will be created from the content of that directory.

The name of an existing application archive file, or a pathlib.Path object referring to such a file, in which case the file is copied to the target (modifying it to reflect the value given for the interpreter argument). The file name should include the .pyz extension, if required.

A file object open for reading in bytes mode. The content of the file should be an application archive, and the file object is assumed to be positioned at the start of the archive.

The target argument determines where the resulting archive will be written:

If it is the name of a file, or a pathlb.Path object, the archive will be written to that file.

If it is an open file object, the archive will be written to that file object, which must be open for writing in bytes mode.

If the target is omitted (or None), the source must be a directory and the target will be a file with the same name as the source, with a .pyz extension added.

The interpreter argument specifies the name of the Python interpreter with which the archive will be executed. It is written as a “shebang” line at the start of the archive. On POSIX, this will be interpreted by the OS, and on Windows it will be handled by the Python launcher. Omitting the interpreter results in no shebang line being written. If an interpreter is specified, and the target is a filename, the executable bit of the target file will be set.

The main argument specifies the name of a callable which will be used as the main program for the archive. It can only be specified if the source is a directory, and the source does not already contain a __main__.py file. The main argument should take the form “pkg.module:callable” and the archive will be run by importing “pkg.module” and executing the given callable with no arguments. It is an error to omit main if the source is a directory and does not contain a __main__.py file, as otherwise the resulting archive would not be executable.

If a file object is specified for source or target, it is the caller’s responsibility to close it after calling create_archive.

When copying an existing archive, file objects supplied only need read and readline, or write methods. When creating an archive from a directory, if the target is a file object it will be passed to the zipfile.ZipFile class, and must supply the methods needed by that class.

zipapp.get_interpreter(archive)

Return the interpreter specified in the #! line at the start of the archive. If there is no #! line, return None. The archive argument can be a filename or a file-like object open for reading in bytes mode. It is assumed to be at the start of the archive.

28.4.4. 例子

Pack up a directory into an archive, and run it.

$ python -m zipapp myapp
$ python myapp.pyz
<output from myapp>

The same can be done using the create_archive() functon:

>>> import zipapp
>>> zipapp.create_archive('myapp.pyz', 'myapp')

To make the application directly executable on POSIX, specify an interpreter to use.

$ python -m zipapp myapp -p "/usr/bin/env python"
$ ./myapp.pyz
<output from myapp>

To replace the shebang line on an existing archive, create a modified archive using the create_archive() function:

>>> import zipapp
>>> zipapp.create_archive('old_archive.pyz', 'new_archive.pyz', '/usr/bin/python3')

To update the file in place, do the replacement in memory using a BytesIO object, and then overwrite the source afterwards. Note that there is a risk when overwriting a file in place that an error will result in the loss of the original file. This code does not protect against such errors, but production code should do so. Also, this method will only work if the archive fits in memory:

>>> import zipapp
>>> import io
>>> temp = io.BytesIO()
>>> zipapp.create_archive('myapp.pyz', temp, '/usr/bin/python2')
>>> with open('myapp.pyz', 'wb') as f:
>>>     f.write(temp.getvalue())

28.4.5. Specifying the Interpreter

Note that if you specify an interpreter and then distribute your application archive, you need to ensure that the interpreter used is portable. The Python launcher for Windows supports most common forms of POSIX #! line, but there are other issues to consider:

If you use “/usr/bin/env python” (or other forms of the “python” command, such as “/usr/bin/python”), you need to consider that your users may have either Python 2 or Python 3 as their default, and write your code to work under both versions.

If you use an explicit version, for example “/usr/bin/env python3” your application will not work for users who do not have that version. (This may be what you want if you have not made your code Python 2 compatible).

There is no way to say “python X.Y or later”, so be careful of using an exact version like “/usr/bin/env python3.4” as you will need to change your shebang line for users of Python 3.5, for example.

Typically, you should use an “/usr/bin/env python2” or “/usr/bin/env python3”, depending on whether your code is written for Python 2 or 3.

28.4.6. Creating Standalone Applications with zipapp

Using the zipapp module, it is possible to create self-contained Python programs, which can be distributed to end users who only need to have a suitable version of Python installed on their system. The key to doing this is to bundle all of the application’s dependencies into the archive, along with the application code.

The steps to create a standalone archive are as follows:

Create your application in a directory as normal, so you have a myapp directory containing a __main__.py file, and any supporting application code.

Install all of your application’s dependencies into the myapp directory, using pip:

$ python -m pip install -r requirements.txt --target myapp

(this assumes you have your project requirements in a requirements.txt file - if not, you can just list the dependencies manually on the pip command line).

Optionally, delete the .dist-info directories created by pip in the myapp directory. These hold metadata for pip to manage the packages, and as you won’t be making any further use of pip they aren’t required - although it won’t do any harm if you leave them.

Package the application using:

$ python -m zipapp -p "interpreter" myapp

This will produce a standalone executable, which can be run on any machine with the appropriate interpreter available. See Specifying the Interpreter for details. It can be shipped to users as a single file.

On Unix, the myapp.pyz file is executable as it stands. You can rename the file to remove the .pyz extension if you prefer a “plain” command name. On Windows, the myapp.pyz[w] file is executable by virtue of the fact that the Python interpreter registers the .pyz and .pyzw file extensions when installed.

28.4.6.1. Making a Windows executable

On Windows, registration of the .pyz extension is optional, and furthermore, there are certain places that don’t recognise registered extensions “transparently” (the simplest example is that subprocess.run(['myapp']) won’t find your application - you need to explicitly specify the extension).

On Windows, therefore, it is often preferable to create an executable from the zipapp. This is relatively easy, although it does require a C compiler. The basic approach relies on the fact that zipfiles can have arbitrary data prepended, and Windows exe files can have arbitrary data appended. So by creating a suitable launcher and tacking the .pyz file onto the end of it, you end up with a single-file executable that runs your application.

A suitable launcher can be as simple as the following:

#define Py_LIMITED_API 1
#include "Python.h"

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#ifdef WINDOWS
int WINAPI wWinMain(
    HINSTANCE hInstance,      /* handle to current instance */
    HINSTANCE hPrevInstance,  /* handle to previous instance */
    LPWSTR lpCmdLine,         /* pointer to command line */
    int nCmdShow              /* show state of window */
)
#else
int wmain()
#endif
{
    wchar_t **myargv = _alloca((__argc + 1) * sizeof(wchar_t*));
    myargv[0] = __wargv[0];
    memcpy(myargv + 1, __wargv, __argc * sizeof(wchar_t *));
    return Py_Main(__argc+1, myargv);
}

If you define the WINDOWS preprocessor symbol, this will generate a GUI executable, and without it, a console executable.

To compile the executable, you can either just use the standard MSVC command line tools, or you can take advantage of the fact that distutils knows how to compile Python source:

>>> from distutils.ccompiler import new_compiler
>>> import distutils.sysconfig
>>> import sys
>>> import os
>>> from pathlib import Path

>>> def compile(src):
>>>     src = Path(src)
>>>     cc = new_compiler()
>>>     exe = src.stem
>>>     cc.add_include_dir(distutils.sysconfig.get_python_inc())
>>>     cc.add_library_dir(os.path.join(sys.base_exec_prefix, 'libs'))
>>>     # First the CLI executable
>>>     objs = cc.compile([str(src)])
>>>     cc.link_executable(objs, exe)
>>>     # Now the GUI executable
>>>     cc.define_macro('WINDOWS')
>>>     objs = cc.compile([str(src)])
>>>     cc.link_executable(objs, exe + 'w')

>>> if __name__ == "__main__":
>>>     compile("zastub.c")

The resulting launcher uses the “Limited ABI”, so it will run unchanged with any version of Python 3.x. All it needs is for Python (python3.dll) to be on the user’s PATH.

For a fully standalone distribution, you can distribute the launcher with your application appended, bundled with the Python “embedded” distribution. This will run on any PC with the appropriate architecture (32 bit or 64 bit).

28.4.6.2. Caveats

There are some limitations to the process of bundling your application into a single file. In most, if not all, cases they can be addressed without needing major changes to your application.

If your application depends on a package that includes a C extension, that package cannot be run from a zip file (this is an OS limitation, as executable code must be present in the filesystem for the OS loader to load it). In this case, you can exclude that dependency from the zipfile, and either require your users to have it installed, or ship it alongside your zipfile and add code to your __main__.py to include the directory containing the unzipped module in sys.path. In this case, you will need to make sure to ship appropriate binaries for your target architecture(s) (and potentially pick the correct version to add to sys.path at runtime, based on the user’s machine).

If you are shipping a Windows executable as described above, you either need to ensure that your users have python3.dll on their PATH (which is not the default behaviour of the installer) or you should bundle your application with the embedded distribution.

The suggested launcher above uses the Python embedding API. This means that in your application, sys.executable will be your application, and not a conventional Python interpreter. Your code and its dependencies need to be prepared for this possibility. For example, if your application uses the multiprocessing module, it will need to call multiprocessing.set_executable() to let the module know where to find the standard Python interpreter.

28.4.7. The Python Zip Application Archive Format

Python has been able to execute zip files which contain a __main__.py file since version 2.6. In order to be executed by Python, an application archive simply has to be a standard zip file containing a __main__.py file which will be run as the entry point for the application. As usual for any Python script, the parent of the script (in this case the zip file) will be placed on sys.path and thus further modules can be imported from the zip file.

The zip file format allows arbitrary data to be prepended to a zip file. The zip application format uses this ability to prepend a standard POSIX “shebang” line to the file (#!/path/to/interpreter).

Formally, the Python zip application format is therefore:

An optional shebang line, containing the characters b'#!' followed by an interpreter name, and then a newline (b'\n') character. The interpreter name can be anything acceptable to the OS “shebang” processing, or the Python launcher on Windows. The interpreter should be encoded in UTF-8 on Windows, and in sys.getfilesystemencoding() on POSIX.

Standard zipfile data, as generated by the zipfile module. The zipfile content must include a file called __main__.py (which must be in the “root” of the zipfile - i.e., it cannot be in a subdirectory). The zipfile data can be compressed or uncompressed.

If an application archive has a shebang line, it may have the executable bit set on POSIX systems, to allow it to be executed directly.

There is no requirement that the tools in this module are used to create application archives - the module is a convenience, but archives in the above format created by any means are acceptable to Python.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

28. 软件打包和分发 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

29. Python运行时服务

本章里描述的模块提供了和Python解释器及其环境交互相关的广泛服务。以下是综述：

29.1. sys — 系统相关的参数和函数

29.2. sysconfig — Provide access to Python’s configuration information

29.2.1. 配置变量

29.2.2. 安装路径

29.2.3. 其他功能

29.2.4. Using sysconfig as a script

29.3. builtins — 内建对象

29.4. __main__ — 顶层脚本环境

29.5. warnings — Warning control

29.5.1. 警告类别

29.5.2. The Warnings Filter

29.5.2.1. Default Warning Filters

29.5.3. 暂时禁止警告

29.5.4. 测试警告

29.5.5. Updating Code For New Versions of Python

29.5.6. Available Functions

29.5.7. Available Context Managers

29.6. contextlib — Utilities for with-statement contexts

29.6.1. 工具

29.6.2. 例子和配方

29.6.2.1. Supporting a variable number of context managers

29.6.2.2. Simplifying support for single optional context managers

29.6.2.3. Catching exceptions from __enter__ methods

29.6.2.4. Cleaning up in an __enter__ implementation

29.6.2.5. Replacing any use of try-finally and flag variables

29.6.2.6. Using a context manager as a function decorator

29.6.3. Single use, reusable and reentrant context managers

29.6.3.1. Reentrant context managers

29.6.3.2. Reusable context managers

29.7. abc — 抽象基类

29.8. atexit — 退出处理器

29.8.1. atexit 示例

29.9. traceback — 打印或检索堆栈回溯

29.9.1. TracebackException Objects

29.9.2. StackSummary Objects

29.9.3. FrameSummary Objects

29.9.4. Traceback Examples

29.10. __future__ — Future 语句定义

29.11. gc — 垃圾回收器接口

29.12. inspect — 检查对象

29.12.1. 类型和成员

29.12.2. Retrieving source code

29.12.3. Introspecting callables with the Signature object

29.12.4. 类与函数

29.12.5. The interpreter stack

29.12.6. Fetching attributes statically

29.12.7. Current State of Generators and Coroutines

29.12.8. Code Objects Bit Flags

29.12.9. 命令行界面

29.13. site —— 指定 Site 的配置钩子

29.13.1. Readline configuration

29.13.2. 模块内容

29.14. fpectl — Floating point exception control

29.14.1. Example

29.14.2. Limitations and other considerations

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

29.1. sys — 系统相关的参数和函数

该模块提供了一些变量和函数。这些变量可能被解释器使用，也可能由解释器提供。这些函数会影响解释器。本模块总是可用的。

sys.abiflags

在POSIX系统上，以标准的 configure 脚本构建的 Python 中，这个变量会包含 PEP 3149 [https://www.python.org/dev/peps/pep-3149] 中定义的ABI标签。

3.2 新版功能.

sys.argv

一个列表，其中包含了被传递给 Python 脚本的命令行参数。 argv[0] 为脚本的名称（是否是完整的路径名取决于操作系统）。如果是通过 Python 解释器的命令行参数 -c 来执行的， argv[0] 会被设置成字符串 '-c' 。如果没有脚本名被传递给 Python 解释器， argv[0] 为空字符串。

为了遍历标准输入，或者通过命令行传递的文件列表，参照 fileinput 模块

sys.base_exec_prefix

在 site.py 运行之前， Python 启动的时候被设置为跟 exec_prefix 同样的值。如果不是运行在 虚拟环境 中，两个值会保持相同；如果 site.py 发现处于一个虚拟环境中， prefix 和 exec_prefix 将会指向虚拟环境。然而 base_prefix 和 base_exec_prefix 将仍然会指向基础的 Python 环境（用来创建虚拟环境的 Python 环境）

3.3 新版功能.

sys.base_prefix

在 site.py 运行之前， Python 启动的时候被设置为跟 prefix 同样的值。如果不是运行在 虚拟环境 中, 两个值会保持相同；如果 site.py 发现处于一个虚拟环境中， prefix 和 exec_prefix 将会指向虚拟环境。然而 base_prefix 和 base_exec_prefix 将仍然会指向基础的 Python 环境（用来创建虚拟环境的 Python 环境）

3.3 新版功能.

sys.byteorder

本地字节顺序的指示符。在大端序（最高有效位优先）操作系统上值为 'big' ，在小端序（最低有效位优先）操作系统上为 'little' 。

sys.builtin_module_names

一个元素为字符串的元组。包含了所有的被编译进 Python 解释器的模块。（这个信息无法通过其他的办法获取， modules.keys() 只包括被导入过的模块。）

sys.call_tracing(func, args)

在启用跟踪时调用 func(*args) 来保存跟踪状态，然后恢复跟踪状态。这将从检查点的调试器调用，以便递归地调试其他的一些代码。

sys.copyright

一个字符串，包含了 Python 解释器有关的版权信息

sys._clear_type_cache()

清除内部的类型缓存。类型缓存是为了加速查找方法和属性的。在调试引用泄漏的时候调用这个函数 只会 清除不必要的引用。

这个函数应该只在内部为了一些特定的目的使用。

sys._current_frames()

返回一个字典，将每个线程的标识符映射到调用函数时该线程中当前活动的最顶层堆栈帧。注意 traceback 模块中的函数可以在给定帧的情况下构建调用堆栈。

这对于调试死锁最有用：本函数不需要死锁线程的配合，并且只要这些线程的调用栈保持死锁，它们就是冻结的。在调用本代码来检查栈顶的帧的那一刻，非死锁线程返回的帧可能与该线程当前活动的帧没有任何关系。

这个函数应该只在内部为了一些特定的目的使用。

sys._debugmallocstats()

将有关 CPython 内存分配器状态的底层的信息打印至 stderr。

如果 Python 被配置为 –with-pydebug，本方法还将执行一些开销较大的内部一致性检查。

3.3 新版功能.

CPython implementation detail: 本函数仅限 CPython。此处没有定义确切的输出格式，且可能会更改。

sys.dllhandle

Integer specifying the handle of the Python DLL. Availability: Windows.

sys.displayhook(value)

如果 value 不是 None，则本函数会将 repr(value) 打印至 sys.stdout，并将 value 保存在 builtins._ 中。如果 repr(value) 无法用 sys.stdout.errors 错误回调方法（可能较为“严格”）编码为 sys.stdout.encoding，请用 'backslashreplace' 错误回调方法将其编码为 sys.stdout.encoding。

在交互式 Python 会话中运行 expression 产生结果后，将在结果上调用 sys.displayhook。若要自定义这些 value 的显示，可以将 sys.displayhook 指定为另一个单参数函数。

伪代码:

def displayhook(value):
    if value is None:
        return
    # Set '_' to None to avoid recursion
    builtins._ = None
    text = repr(value)
    try:
        sys.stdout.write(text)
    except UnicodeEncodeError:
        bytes = text.encode(sys.stdout.encoding, 'backslashreplace')
        if hasattr(sys.stdout, 'buffer'):
            sys.stdout.buffer.write(bytes)
        else:
            text = bytes.decode(sys.stdout.encoding, 'strict')
            sys.stdout.write(text)
    sys.stdout.write("\n")
    builtins._ = value

在 3.2 版更改: 在发生 UnicodeEncodeError 时使用 'backslashreplace' 错误回调方法。

sys.dont_write_bytecode

如果该值为 true，则 Python 在导入源码模块时将不会尝试写入 .pyc 文件。该值会被初始化为 True 或 False，依据是 -B 命令行选项和 PYTHONDONTWRITEBYTECODE 环境变量，可以自行设置该值，来控制是否生成字节码文件。

sys.excepthook(type, value, traceback)

本函数会将所给的回溯和异常输出到 sys.stderr 中。

当抛出一个异常，且未被捕获时，解释器将调用 sys.excepthook 并带有三个参数：异常类、异常实例和一个回溯对象。在交互式会话中，这会在控制权返回到提示符之前发生。在 Python 程序中，这会在程序退出之前发生。如果要自定义此类顶级异常的处理过程，可以将另一个 3 个参数的函数赋给 sys.excepthook。

sys.__displayhook__

sys.__excepthook__

These objects contain the original values of displayhook and excepthook at the start of the program. They are saved so that displayhook and excepthook can be restored in case they happen to get replaced with broken objects.

sys.exc_info()

本函数返回的元组包含三个值，它们给出当前正在处理的异常的信息。返回的信息仅限于当前线程和当前堆栈帧。如果当前堆栈帧没有正在处理的异常，则信息将从调用的下级堆栈帧或上级调用者等位置获取，依此类推，直到找到正在处理异常的堆栈帧为止。此处的“处理异常”被定义为“执行 except 子句”。任何堆栈帧都只能访问当前正在处理的异常的信息。

If no exception is being handled anywhere on the stack, a tuple containing three None values is returned. Otherwise, the values returned are (type, value, traceback). Their meaning is: type gets the type of the exception being handled (a subclass of BaseException); value gets the exception instance (an instance of the exception type); traceback gets a traceback object (see the Reference Manual) which encapsulates the call stack at the point where the exception originally occurred.

sys.exec_prefix

一个字符串，给出特定域的目录前缀，该目录中安装了与平台相关的 Python 文件，默认也是 '/usr/local'。该目录前缀可以在构建时使用 configure 脚本的 --exec-prefix 参数进行设置。具体而言，所有配置文件（如 pyconfig.h 头文件）都安装在目录 exec_prefix/lib/pythonX.Y/config 中，共享库模块安装在 exec_prefix/lib/pythonX.Y/lib-dynload 中，其中 X.Y 是 Python 的版本号，如 3.2。

注解

如果在一个 虚拟环境 中，那么该值将在 site.py 中被修改，指向虚拟环境。Python 安装位置仍然可以用 base_exec_prefix 来获取。

sys.executable

一个字符串，提供 Python 解释器的可执行二进制文件的绝对路径，仅在部分系统中此值有意义。如果 Python 无法获取其可执行文件的真实路径，则 sys.executable 将为空字符串或 None。

sys.exit([arg])

从Python中退出。实现方式是抛出一个 SystemExit 异常。异常抛出后 try 声明的 finally 分支语句的清除动作将被出发。此动作有可能打断更外层的退出尝试。

可选参数 arg 可以是表示退出状态的整数（默认为 0），也可以是其他类型的对象。如果它是整数，则 shell 等将 0 视为“成功终止”，非零值视为“异常终止”。大多数系统要求该值的范围是 0–127，否则会产生不确定的结果。某些系统为退出代码约定了特定的含义，但通常尚不完善；Unix 程序通常用 2 表示命令行语法错误，用 1 表示所有其他类型的错误。传入其他类型的对象，如果传入 None 等同于传入 0，如果传入其他对象则将其打印至 stderr，且退出代码为 1。特别地，sys.exit("some error message") 可以在发生错误时快速退出程序。

由于 exit() 最终“只是”抛出一个异常，因此当从主线程调用时，只会从进程退出；而异常不会因此被打断。

在 3.6 版更改: 在 Python 解释器捕获 SystemExit 后，如果在清理中发生错误（如清除标准流中的缓冲数据时出错），则退出状态码将变为 120。

sys.flags

The struct sequence flags exposes the status of command line flags. The attributes are read only.

attribute – 属性

标志

debug

-d

inspect

-i

interactive

-i

isolated

-I

optimize

-O 或 -OO

dont_write_bytecode

-B

no_user_site

-s

no_site

-S

ignore_environment

-E

verbose

-v

bytes_warning

-b

quiet

-q

hash_randomization

-R

在 3.2 版更改: 为新的 -q 标志添加了 quiet 属性

3.2.3 新版功能: hash_randomization 属性

在 3.3 版更改: 删除了过时的 division_warning 属性

在 3.4 版更改: 为 -I isolated 标志添加了 isolated 属性。

sys.float_info

A struct sequence holding information about the float type. It contains low level information about the precision and internal representation. The values correspond to the various floating-point constants defined in the standard header file float.h for the ‘C’ programming language; see section 5.2.4.2.2 of the 1999 ISO/IEC C standard [C99], ‘Characteristics of floating types’, for details.

attribute – 属性

float.h 宏

解释

epsilon

DBL_EPSILON

difference between 1 and the least value greater than 1 that is representable as a float

dig

DBL_DIG

浮点数可以真实表示的最大十进制数字；见下文

mant_dig

DBL_MANT_DIG

浮点数精度：radix 基数下的浮点数有效位数

max

DBL_MAX

maximum representable finite float

max_exp

DBL_MAX_EXP

maximum integer e such that radix**(e-1) is a representable finite float

max_10_exp

DBL_MAX_10_EXP

maximum integer e such that 10**e is in the range of representable finite floats

min

DBL_MIN

minimum positive normalized float

min_exp

DBL_MIN_EXP

minimum integer e such that radix**(e-1) is a normalized float

min_10_exp

DBL_MIN_10_EXP

minimum integer e such that 10**e is a normalized float

radix

FLT_RADIX

指数表示法中采用的基数

rounds

FLT_ROUNDS

整数常数，表示算术运算中的舍入方式。它反映了解释器启动时系统的 FLT_ROUNDS 宏的值。关于可能的值及其含义的说明，请参阅 C99 标准 5.2.4.2.2 节。

关于 sys.float_info.dig 属性的进一步说明。如果 s 是表示十进制数的字符串，而该数最多有 sys.float_info.dig 位有效数字，则将 s 转换为 float 再转回去将恢复原先相同十进制值的字符串:

>>> import sys
>>> sys.float_info.dig
15
>>> s = '3.14159265358979'    # decimal string with 15 significant digits
>>> format(float(s), '.15g')  # convert to float and back -> same value
'3.14159265358979'

但是对于超过 sys.float_info.dig 位有效数字的字符串，转换前后并非总是相同:

>>> s = '9876543211234567'    # 16 significant digits is too many!
>>> format(float(s), '.16g')  # conversion changes value
'9876543211234568'

sys.float_repr_style

一个字符串，反映 repr() 函数在浮点数上的行为。如果该字符串是 'short'，那么对于（非无穷的）浮点数 x，repr(x) 将会生成一个短字符串，满足 float(repr(x)) == x 的特性。这是 Python 3.1 及更高版本中的常见行为。否则 float_repr_style 的值将是 'legacy'，此时 repr(x) 的行为方式将与 Python 3.1 之前的版本相同。

3.1 新版功能.

sys.getallocatedblocks()

返回解释器当前已分配的内存块数，无论它们大小如何。本函数主要用于跟踪和调试内存泄漏。因为解释器有内部缓存，所以不同调用之间结果会变化。可能需要调用 _clear_type_cache() 和 gc.collect() 使结果更容易预测。

如果当前 Python 构建或实现无法合理地计算此信息，允许 getallocatedblocks() 返回 0。

3.4 新版功能.

sys.getcheckinterval()

Return the interpreter’s “check interval”; see setcheckinterval().

3.2 版后已移除: Use getswitchinterval() instead.

sys.getdefaultencoding()

返回当前 Unicode 实现所使用的默认字符串编码名称。

sys.getdlopenflags()

Return the current value of the flags that are used for dlopen() calls. Symbolic names for the flag values can be found in the os module (RTLD_xxx constants, e.g. os.RTLD_LAZY). Availability: Unix.

sys.getfilesystemencoding()

返回编码名称，该编码用于在 Unicode 文件名和 bytes 文件名之间转换。为获得最佳兼容性，任何时候都应使用 str 表示文件名，尽管用字节来表示文件名也是支持的。函数如果需要接受或返回文件名，它应支持 str 或 bytes，并在内部将其转换为系统首选的表示形式。

该编码始终是 ASCII 兼容的。

应使用 os.fsencode() 和 os.fsdecode() 来保证所采用的编码和错误模式都是正确的。

On Mac OS X, the encoding is 'utf-8'.

在Unix上，编码是语言环境编码。

在Windows上取决于用户配置，编码可能是 'utf-8' 或 'mbcs'。

在 3.2 版更改: getfilesystemencoding() 的结果将不再有可能是 None。

在 3.6 版更改: Windows 不再保证会返回 'mbcs'。详情请参阅 PEP 529 [https://www.python.org/dev/peps/pep-0529] 和 _enablelegacywindowsfsencoding()。

sys.getfilesystemencodeerrors()

返回错误回调函数的名称，该错误回调函数将在 Unicode 文件名和 bytes 文件名转换时生效。编码的名称是由 getfilesystemencoding() 返回的。

应使用 os.fsencode() 和 os.fsdecode() 来保证所采用的编码和错误模式都是正确的。

3.6 新版功能.

sys.getrefcount(object)

返回 object 的引用计数。返回的计数通常比预期的多一，因为它包括了作为 getrefcount() 参数的这一次（临时）引用。

sys.getrecursionlimit()

返回当前的递归限制值，即 Python 解释器堆栈的最大深度。此限制可防止无限递归导致的 C 堆栈溢出和 Python 崩溃。该值可以通过 setrecursionlimit() 设置。

sys.getsizeof(object[, default])

返回对象的大小（以字节为单位）。该对象可以是任何类型。所有内建对象返回的结果都是正确的，但对于第三方扩展不一定正确，因为这与具体实现有关。

只计算直接分配给对象的内存消耗，不计算它所引用的对象的内存消耗。

对象不提供计算大小的方法时，如果有给出 default 则返回它，否则抛出 TypeError 异常。

如果对象由垃圾回收器管理，则 getsizeof() 将调用对象的 __sizeof__ 方法，并在上层添加额外的垃圾回收器。

可以参考 recursive sizeof recipe [https://code.activestate.com/recipes/577504] 中的示例，关于递归调用 getsizeof() 来得到各个容器及其所有内容物的大小。

sys.getswitchinterval()

返回解释器的“线程切换间隔时间”，请参阅 setswitchinterval()。

3.2 新版功能.

sys._getframe([depth])

返回来自调用栈的一个帧对象。如果传入可选整数 depth，则返回从栈顶往下相应调用层数的帧对象。如果该数比调用栈更深，则抛出 ValueError。depth 的默认值是 0，返回调用栈顶部的帧。

CPython implementation detail: 这个函数应该只在内部为了一些特定的目的使用。不保证它在所有 Python 实现中都存在。

sys.getprofile()

返回由 setprofile() 设置的性能分析函数

sys.gettrace()

返回由 settrace() 设置的跟踪函数。

CPython implementation detail: gettrace() 函数仅用于实现调试器，性能分析器，打包工具等。它的行为是实现平台的一部分，而不是语言定义的一部分，因此并非在所有 Python 实现中都可用。

sys.getwindowsversion()

返回一个具名元组，描述当前正在运行的 Windows 版本。元素名称包括 major, minor, build, platform, service_pack, service_pack_minor, service_pack_major, suite_mask, product_type 和 platform_version。service_pack 包含一个字符串，platform_version 包含一个三元组，其他所有值都是整数。元素也可以通过名称来访问，所以 sys.getwindowsversion()[0] 与 sys.getwindowsversion().major 是等效的。为保持与旧版本的兼容性，只有前 5 个元素可以用索引检索。

platform 将会是 2 (VER_PLATFORM_WIN32_NT)。

product_type 可能是以下值之一：

常数

含义

1 (VER_NT_WORKSTATION)

系统是工作站。

2 (VER_NT_DOMAIN_CONTROLLER)

系统是域控制器。

3 (VER_NT_SERVER)

系统是服务器，但不是域控制器。

本函数包装了 Win32 GetVersionEx() 函数，参阅 Microsoft 文档有关 OSVERSIONINFOEX() 的内容可获取这些字段的更多信息。

platform_version 返回的是当前操作系统真实准确的主要版本、次要版本和内部版本号，不是为该进程模拟的版本。它旨在用于记录日志，不用于检测功能。

Availability: Windows.

在 3.2 版更改: 更改为具名元组，添加 service_pack_minor, service_pack_major, suite_mask 和 product_type。

在 3.6 版更改: 添加了 platform_version

sys.get_asyncgen_hooks()

Returns an asyncgen_hooks object, which is similar to a namedtuple of the form (firstiter, finalizer), where firstiter and finalizer are expected to be either None or functions which take an asynchronous generator iterator as an argument, and are used to schedule finalization of an asychronous generator by an event loop.

3.6 新版功能: 详情请参阅 PEP 525 [https://www.python.org/dev/peps/pep-0525]。

注解

本函数已添加至暂定软件包（详情请参阅 PEP 411 [https://www.python.org/dev/peps/pep-0411] ）。

sys.get_coroutine_wrapper()

Returns None, or a wrapper set by set_coroutine_wrapper().

3.5 新版功能: See PEP 492 [https://www.python.org/dev/peps/pep-0492] for more details.

注解

本函数已添加至暂定软件包（详情请参阅 PEP 411 [https://www.python.org/dev/peps/pep-0411] ）。仅将其用于调试目的。

sys.hash_info

A struct sequence giving parameters of the numeric hash implementation. For more details about hashing of numeric types, see 数字类型的哈希运算.

attribute – 属性

解释

width

用于哈希值的位宽度

modulus

用于数字散列方案的素数模数P。

inf

为正无穷大返回的哈希值

nan

为nan返回的哈希值

imag

用于复数虚部的乘数

algorithm

字符串、字节和内存视图的哈希算法的名称

hash_bits

哈希算法的内部输出大小。

seed_bits

散列算法的种子密钥的大小

3.2 新版功能.

在 3.4 版更改: 添加了 algorithm, hash_bits 和 seed_bits

sys.hexversion

编码为单个整数的版本号。该整数会确保每个版本都自增，其中适当包括了未发布版本。举例来说，要测试 Python 解释器的版本不低于 1.5.2，请使用:

if sys.hexversion >= 0x010502F0:
    # use some advanced feature
    ...
else:
    # use an alternative implementation or warn the user
    ...

This is called hexversion since it only really looks meaningful when viewed as the result of passing it to the built-in hex() function. The struct sequence sys.version_info may be used for a more human-friendly encoding of the same information.

关于 hexversion 的更多信息可以在 API 和 ABI 版本管理 中找到。

sys.implementation

一个对象，该对象包含当前运行的 Python 解释器的实现信息。所有 Python 实现中都必须存在下列属性。

name 是当前实现的标识符，如 'cpython'。实际的字符串由 Python 实现定义，但保证是小写字母。

version 是一个具名元组，格式与 sys.version_info 相同。它表示 Python 实现 的版本。 另一个（由 sys.version_info 表示）是当前解释器遵循的相应 Python 语言 的版本，两者具有不同的含义。 例如，对于 PyPy 1.8，sys.implementation.version 可能是 sys.version_info(1, 8, 0, 'final', 0)，而 sys.version_info 则是 sys.version_info(2, 7, 2, 'final', 0)。对于 CPython 而言两个值是相同的，因为它是参考实现。

hexversion 是十六进制的实现版本，类似于 sys.hexversion。

cache_tag 是导入机制使用的标记，用于已缓存模块的文件名。按照惯例，它将由实现的名称和版本组成，如 'cpython-33'。但如果合适，Python 实现可以使用其他值。如果 cache_tag 被置为 None，表示模块缓存已禁用。

sys.implementation 可能包含相应 Python 实现的其他属性。这些非标准属性必须以下划线开头，此处不详细阐述。无论其内容如何，sys.implementation 在解释器运行期间或不同实现版本之间都不会更改。（但是不同 Python 语言版本间可能会不同。）详情请参阅 PEP 421 [https://www.python.org/dev/peps/pep-0421]。

3.3 新版功能.

sys.int_info

A struct sequence that holds information about Python’s internal representation of integers. The attributes are read only.

属性

解释

bits_per_digit

每个数字占有的位数。Python 内部将整数存储在基底 2**int_info.bits_per_digit

sizeof_digit

用于表示数字的C类型的字节大小

3.1 新版功能.

sys.__interactivehook__

当本属性存在，则以 交互模式 启动解释器时，将自动（不带参数地）调用本属性的值。该过程是在读取 PYTHONSTARTUP 文件之后完成的，所以可以在该文件中设置这一钩子。site 模块 设置了这一属性。

3.4 新版功能.

sys.intern(string)

将 string 插入 “interned” （驻留）字符串表，返回被插入的字符串 – 它是 string 本身或副本。驻留字符串对提高字典查找的性能很有用 – 如果字典中的键已驻留，且所查找的键也已驻留，则键（取散列后）的比较可以用指针代替字符串来比较。通常，Python 程序使用到的名称会被自动驻留，且用于保存模块、类或实例属性的字典的键也已驻留。

驻留字符串不是永久存在的，对 intern() 返回值的引用必须保留下来，才能发挥驻留字符串的优势。

sys.is_finalizing()

如果 Python 解释器 正在关闭，则返回 True，否则返回 False。

3.5 新版功能.

sys.last_type

sys.last_value

sys.last_traceback

这三个变量并非总是有定义，仅当有异常未处理，且解释器打印了错误消息和堆栈回溯时，才会给它们赋值。它们的预期用途，是允许交互中的用户导入调试器模块，进行事后调试，而不必重新运行导致错误的命令。（通常使用 import pdb; pdb.pm() 进入事后调试器，详情请参阅 pdb 模块。）

这些变量的含义与上述 exc_info() 返回值的含义相同。

sys.maxsize

一个整数，表示 Py_ssize_t 类型的变量可以取到的最大值。在 32 位平台上通常为 2**31 - 1，在 64 位平台上通常为 2**63 - 1。

sys.maxunicode

一个整数，表示最大的 Unicode 码点值，如 1114111 （十六进制为 0x10FFFF ）。

在 3.3 版更改: 在 PEP 393 [https://www.python.org/dev/peps/pep-0393] 之前，sys.maxunicode 曾是 0xFFFF 或 0x10FFFF，具体取决于配置选项，该选项指定将 Unicode 字符存储为 UCS-2 还是 UCS-4。

sys.meta_path

一个由 元路径查找器 对象组成的列表，当查找需要导入的模块时，会调用这些对象的 find_spec() 方法，观察这些对象是否能找到所需模块。调用 find_spec() 方法最少需要传入待导入模块的绝对名称。如果待导入模块包含在一个包中，则父包的 __path__ 属性将作为第二个参数被传入。该方法返回 模块规格，找不到模块则返回 None。

参见

importlib.abc.MetaPathFinder

抽象基类，定义了 meta_path 内的查找器对象的接口。

importlib.machinery.ModuleSpec

find_spec() 返回的实例所对应的具体类。

在 3.4 版更改: 在 Python 3.4 中通过 PEP 451 [https://www.python.org/dev/peps/pep-0451] 引入了 模块规格。早期版本的 Python 会寻找一个称为 find_module() 的方法。如果某个 meta_path 条目没有 find_spec() 方法，就会回退去调用前一种方法。

sys.modules

一个字典，将模块名称映射到已加载的模块。可以操作该字典来强制重新加载模块，或是实现其他技巧。但是，替换的字典不一定会按预期工作，并且从字典中删除必要的项目可能会导致 Python 崩溃。

sys.path

一个由字符串组成的列表，用于指定模块的搜索路径。初始化自环境变量 PYTHONPATH，再加上一条与安装有关的默认路径。

程序启动时将初始化该列表，列表的第一项 path[0] 目录含有调用 Python 解释器的脚本。如果脚本目录不可用（比如以交互方式调用了解释器，或脚本是从标准输入中读取的），则 path[0] 为空字符串，这将导致 Python 优先搜索当前目录中的模块。注意，脚本目录将插入在 PYTHONPATH 的条目*之前*。

程序可以随意修改本列表用于自己的目的。只能向 sys.path 中添加 string 和 bytes 类型，其他数据类型将在导入期间被忽略。

参见

site 模块，该模块描述了如何使用 .pth 文件来扩展 sys.path。

sys.path_hooks

一个由可调用对象组成的列表，这些对象接受一个路径作为参数，并尝试为该路径创建一个 查找器。如果成功创建查找器，则可调用对象将返回它，否则将引发 ImportError 异常。

本特性最早在 PEP 302 [https://www.python.org/dev/peps/pep-0302] 中被提及。

sys.path_importer_cache

一个字典，作为 查找器 对象的缓存。key 是传入 sys.path_hooks 的路径，value 是相应已找到的查找器。如果路径是有效的文件系统路径，但在 sys.path_hooks 中未找到查找器，则存入 None。

本特性最早在 PEP 302 [https://www.python.org/dev/peps/pep-0302] 中被提及。

在 3.3 版更改: 未找到查找器时，改为存储 None，而不是 imp.NullImporter。

sys.platform

本字符串是一个平台标识符，举例而言，该标识符可用于将特定平台的组件追加到 sys.path 中。

For Unix systems, except on Linux, this is the lowercased OS name as returned by uname -s with the first part of the version as returned by uname -r appended, e.g. 'sunos5' or 'freebsd8', at the time when Python was built. Unless you want to test for a specific system version, it is therefore recommended to use the following idiom:

if sys.platform.startswith('freebsd'):
    # FreeBSD-specific code here...
elif sys.platform.startswith('linux'):
    # Linux-specific code here...

对于其他系统，值是：

系统

平台 值

Linux

'linux'

Windows

'win32'

Windows/Cygwin

'cygwin'

Mac OS X

'darwin'

在 3.3 版更改: 在 Linux 上，sys.platform 将不再包含副版本号。它将总是 'linux' 而不是 'linux2' 或 'linux3'。由于旧版本的 Python 会包含该版本号，因此推荐总是使用上述 startswith 习惯用法。

参见

os.name 更加简略。os.uname() 提供系统的版本信息。

platform 模块对系统的标识有更详细的检查。

sys.prefix

一个字符串，给出特定域的目录前缀，该目录中安装了与平台不相关的 Python 文件，默认为 '/usr/local'。该目录前缀可以在构建时使用 configure 脚本的 --prefix 参数进行设置。Python 库模块的的主要集合安装在目录 prefix/lib/pythonX.Y ，而与平台无关的头文件 (除了 pyconfig.h) 保存在 prefix/include/pythonX.Y, 其中 X.Y 是 Python 的版本号, 例如 3.2.

注解

如果在一个 虚拟环境 中，那么该值将在 site.py 中被修改，指向虚拟环境。Python 安装位置仍然可以用 base_prefix 来获取。

sys.ps1

sys.ps2

字符串，指定解释器的首要和次要提示符。仅当解释器处于交互模式时，它们才有定义。这种情况下，它们的初值为 '>>> ' 和 '... '。如果赋给其中某个变量的是非字符串对象，则每次解释器准备读取新的交互式命令时，都会重新运行该对象的 str()，这可以用来实现动态的提示符。

sys.setcheckinterval(interval)

Set the interpreter’s “check interval”. This integer value determines how often the interpreter checks for periodic things such as thread switches and signal handlers. The default is 100, meaning the check is performed every 100 Python virtual instructions. Setting it to a larger value may increase performance for programs using threads. Setting it to a value <= 0 checks every virtual instruction, maximizing responsiveness as well as overhead.

3.2 版后已移除: This function doesn’t have an effect anymore, as the internal logic for thread switching and asynchronous tasks has been rewritten. Use setswitchinterval() instead.

sys.setdlopenflags(n)

设置解释器在调用 dlopen() 时用到的标志，例如解释器在加载扩展模块时。首先，调用 sys.setdlopenflags(0) 将在导入模块时对符号启用惰性解析。要在扩展模块之间共享符号，请调用 sys.setdlopenflags(os.RTLD_GLOBAL)。标志值的符号名称可以在 os 模块中找到（即 RTLD_xxx 常数，如 os.RTLD_LAZY ）。

Availability: Unix.

sys.setprofile(profilefunc)

Set the system’s profile function, which allows you to implement a Python source code profiler in Python. See chapter Python 分析器 for more information on the Python profiler. The system’s profile function is called similarly to the system’s trace function (see settrace()), but it is called with different events, for example it isn’t called for each executed line of code (only on call and return, but the return event is reported even when an exception has been set). The function is thread-specific, but there is no way for the profiler to know about context switches between threads, so it does not make sense to use this in the presence of multiple threads. Also, its return value is not used, so it can simply return None.

性能分析函数应接收三个参数：frame、event 和 arg。frame 是当前的堆栈帧。event 是一个字符串：'call'、'return'、'c_call'、'c_return' 或 'c_exception'。arg 取决于事件类型。

这些事件具有以下含义：

'call'

表示调用了某个函数（或进入了其他的代码块）。性能分析函数将被调用，arg 为 None。

'return'

表示某个函数（或别的代码块）即将返回。性能分析函数将被调用，arg 是即将返回的值，如果此次事件是由抛出的异常引起的，arg 为 None。

'c_call'

表示即将调用某个 C 函数。它可能是扩展函数或是内建函数。arg 是 C 函数对象。

'c_return'

表示返回了某个 C 函数。arg 是 C 函数对象。

'c_exception'

表示某个 C 函数抛出了异常。arg 是 C 函数对象。

sys.setrecursionlimit(limit)

将 Python 解释器堆栈的最大深度设置为 limit。此限制可防止无限递归导致的 C 堆栈溢出和 Python 崩溃。

不同平台所允许的最高限值不同。当用户有需要深度递归的程序且平台支持更高的限值，可能就需要调高限值。进行该操作需要谨慎，因为过高的限值可能会导致崩溃。

如果新的限值低于当前的递归深度，将抛出 RecursionError 异常。

在 3.5.1 版更改: 如果新的限值低于当前的递归深度，现在将抛出 RecursionError 异常。

sys.setswitchinterval(interval)

设置解释器的线程切换间隔时间（单位为秒）。该浮点数决定了“时间片”的理想持续时间，时间片将分配给同时运行的 Python 线程。请注意，实际值可能更高，尤其是使用了运行时间长的内部函数或方法时。同时，在时间间隔末尾调度哪个线程是操作系统的决定。解释器没有自己的调度程序。

3.2 新版功能.

sys.settrace(tracefunc)

Set the system’s trace function, which allows you to implement a Python source code debugger in Python. The function is thread-specific; for a debugger to support multiple threads, it must be registered using settrace() for each thread being debugged.

Trace functions should have three arguments: frame, event, and arg. frame is the current stack frame. event is a string: 'call', 'line', 'return' or 'exception'. arg depends on the event type.

The trace function is invoked (with event set to 'call') whenever a new local scope is entered; it should return a reference to a local trace function to be used that scope, or None if the scope shouldn’t be traced.

本地跟踪函数应返回对自身的引用（或对另一个函数的引用，用来在其作用范围内进行进一步的跟踪），或者返回 None 来停止跟踪其作用范围。

这些事件具有以下含义：

'call'

表示调用了某个函数（或进入了其他的代码块）。全局跟踪函数将被调用，arg 为 None。返回值取决于本地跟踪函数。

'line'

The interpreter is about to execute a new line of code or re-execute the condition of a loop. The local trace function is called; arg is None; the return value specifies the new local trace function. See Objects/lnotab_notes.txt for a detailed explanation of how this works.

'return'

表示某个函数（或别的代码块）即将返回。局部跟踪函数将被调用，arg 是即将返回的值，如果此次返回事件是由于抛出异常，arg 为 None。跟踪函数的返回值将被忽略。

'exception'

表示发生了某个异常。局部跟踪函数将被调用，arg 是一个 (exception, value, traceback) 元组，返回值将指定新的局部跟踪函数。

注意，由于异常是在链式调用中传播的，所以每一级都会产生一个 'exception' 事件。

关于代码对象和帧对象的更多信息请参考 标准类型层级结构。

CPython implementation detail: settrace() 函数仅用于实现调试器，性能分析器，打包工具等。它的行为是实现平台的一部分，而不是语言定义的一部分，因此并非在所有 Python 实现中都可用。

sys.set_asyncgen_hooks(firstiter, finalizer)

接受两个可选的关键字参数，要求它们是可调用对象，且接受一个 异步生成器迭代器 作为参数。firstiter 对象将在异步生成器第一次迭代时调用。finalizer 将在异步生成器即将被销毁时调用。

3.6 新版功能: 更多详情请参阅 PEP 525 [https://www.python.org/dev/peps/pep-0525]，finalizer 方法的参考示例可参阅 Lib/asyncio/base_events.py [https://github.com/python/cpython/tree/3.6/Lib/asyncio/base_events.py] 中 asyncio.Loop.shutdown_asyncgens 的实现。

注解

本函数已添加至暂定软件包（详情请参阅 PEP 411 [https://www.python.org/dev/peps/pep-0411] ）。

sys.set_coroutine_wrapper(wrapper)

Allows intercepting creation of coroutine objects (only ones that are created by an async def function; generators decorated with types.coroutine() or asyncio.coroutine() will not be intercepted).

The wrapper argument must be either:

a callable that accepts one argument (a coroutine object);

None, to reset the wrapper.

If called twice, the new wrapper replaces the previous one. The function is thread-specific.

The wrapper callable cannot define new coroutines directly or indirectly:

def wrapper(coro):
    async def wrap(coro):
        return await coro
    return wrap(coro)
sys.set_coroutine_wrapper(wrapper)

async def foo():
    pass

# The following line will fail with a RuntimeError, because
# ``wrapper`` creates a ``wrap(coro)`` coroutine:
foo()

See also get_coroutine_wrapper().

3.5 新版功能: See PEP 492 [https://www.python.org/dev/peps/pep-0492] for more details.

注解

本函数已添加至暂定软件包（详情请参阅 PEP 411 [https://www.python.org/dev/peps/pep-0411] ）。仅将其用于调试目的。

sys._enablelegacywindowsfsencoding()

将默认文件系统编码和错误处理方案分别更改为 ‘mbcs’ 和 ‘replace’，这是为了与 Python 3.6 前的版本保持一致。

这等同于在启动 Python 前先定义好 PYTHONLEGACYWINDOWSFSENCODING 环境变量。

Availability: Windows

3.6 新版功能: 有关更多详细信息，请参阅 PEP 529 [https://www.python.org/dev/peps/pep-0529]。

sys.stdin

sys.stdout

sys.stderr

解释器用于标准输入、标准输出和标准错误的 文件对象：

stdin 用于所有交互式输入（包括对 input() 的调用）；

stdout 用于 print() 和 expression 语句的输出，以及用于 input() 的提示符；

解释器自身的提示符和它的错误消息都发往 stderr。

这些流都是常规 文本文件，与 open() 函数返回的对象一致。它们的参数选择如下：

The character encoding is platform-dependent. Under Windows, if the stream is interactive (that is, if its isatty() method returns True), the console codepage is used, otherwise the ANSI code page. Under other platforms, the locale encoding is used (see locale.getpreferredencoding()).

Under all platforms though, you can override this value by setting the PYTHONIOENCODING environment variable before starting Python.

When interactive, standard streams are line-buffered. Otherwise, they are block-buffered like regular text files. You can override this value with the -u command-line option.

注解

要从标准流写入或读取二进制数据，请使用底层二进制 buffer 对象。例如，要将字节写入 stdout，请使用 sys.stdout.buffer.write(b'abc')。

但是，如果你在写一个库（并且不限制执行库代码时的上下文），那么请注意，标准流可能会被替换为文件类对象，如 io.StringIO，它们是不支持 buffer 属性的。

sys.__stdin__

sys.__stdout__

sys.__stderr__

程序开始时，这些对象存有 stdin、stderr 和 stdout 的初始值。它们在程序结束前都可以使用，且在需要向实际的标准流打印内容时很有用，无论 sys.std* 对象是否已重定向。

如果实际文件已经被覆盖成一个损坏的对象了，那它也可用于将实际文件还原成能正常工作的文件对象。但是，本过程的最佳方法应该是，在原来的流被替换之前就显式地保存它，并使用这一保存的对象来还原。

注解

某些情况下的 stdin、stdout 和 stderr 以及初始值 __stdin__、__stdout__ 和 __stderr__ 可以是 None。通常发生在未连接到控制台的 Windows GUI app 中，以及在用 pythonw 启动的 Python app 中。

sys.thread_info

A struct sequence holding information about the thread implementation.

属性

解释

name

线程实现的名称：

'nt': Windows 线程

'pthread': POSIX 线程

'solaris': Solaris 线程

lock

锁实现的名称：

'semaphore': 锁使用信号量

'mutex+cond': 锁使用互斥和条件变量

None 如果此信息未知

version

Name and version of the thread library. It is a string, or None if these informations are unknown.

3.3 新版功能.

sys.tracebacklimit

当该变量值设置为整数，在发生未处理的异常时，它将决定打印的回溯信息的最大层级数。默认为 1000。当将其设置为 0 或小于 0，将关闭所有回溯信息，并且只打印异常类型和异常值。

sys.version

一个包含 Python 解释器版本号加编译版本号以及所用编译器等额外信息的字符串。 此字符串会在交互式解释器启动时显示。 请不要从中提取版本信息，而应当使用 version_info 以及 platform 模块所提供的函数。

sys.api_version

这个解释器的 C API 版本。当你在调试 Python及期扩展模板的版本冲突这个功能非常有用。

sys.version_info

一个包含版本号五部分的元组: major, minor, micro, releaselevel 和 serial。 除 releaselevel 外的所有值均为整数；发布级别值则为 'alpha', 'beta', 'candidate' 或 'final'。 对应于 Python 版本 2.0 的 version_info 值为 (2, 0, 0, 'final', 0)。 这些部分也可按名称访问，因此 sys.version_info[0] 就等价于 sys.version_info.major，依此类推。

在 3.1 版更改: 增加了以名称表示的各部分属性。

sys.warnoptions

这是警告框架的一个实现细节；请不要修改此值。 有关警告框架的更多信息请参阅 warnings 模块。

sys.winver

The version number used to form registry keys on Windows platforms. This is stored as string resource 1000 in the Python DLL. The value is normally the first three characters of version. It is provided in the sys module for informational purposes; modifying this value has no effect on the registry keys used by Python. Availability: Windows.

sys._xoptions

由多个具体实现专属的通过 -X 命令行选项传递的旗标构成的字典。 选项名称将会映射到显式指定的值或者 True。 例如:

$ ./python -Xa=b -Xc
Python 3.2a3+ (py3k, Oct 16 2010, 20:14:50)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys._xoptions
{'a': 'b', 'c': True}

CPython implementation detail: 这是 CPython 专属的访问通过 -X 传递的选项的方式。 其他实现可能会通过其他方式导出它们，或者完全不导出。

3.2 新版功能.

引用

C99

ISO/IEC 9899:1999. “Programming languages – C.” 该标准的公开草案可从 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf获得。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

29.2. sysconfig — Provide access to Python’s configuration information

3.2 新版功能.

源代码： Lib/sysconfig.py [https://github.com/python/cpython/tree/3.6/Lib/sysconfig.py]

The sysconfig module provides access to Python’s configuration information like the list of installation paths and the configuration variables relevant for the current platform.

29.2.1. 配置变量

A Python distribution contains a Makefile and a pyconfig.h header file that are necessary to build both the Python binary itself and third-party C extensions compiled using distutils.

sysconfig puts all variables found in these files in a dictionary that can be accessed using get_config_vars() or get_config_var().

Notice that on Windows, it’s a much smaller set.

sysconfig.get_config_vars(*args)

With no arguments, return a dictionary of all configuration variables relevant for the current platform.

With arguments, return a list of values that result from looking up each argument in the configuration variable dictionary.

For each argument, if the value is not found, return None.

sysconfig.get_config_var(name)

Return the value of a single variable name. Equivalent to get_config_vars().get(name).

If name is not found, return None.

用法示例:

>>> import sysconfig
>>> sysconfig.get_config_var('Py_ENABLE_SHARED')
0
>>> sysconfig.get_config_var('LIBDIR')
'/usr/local/lib'
>>> sysconfig.get_config_vars('AR', 'CXX')
['ar', 'g++']

29.2.2. 安装路径

Python uses an installation scheme that differs depending on the platform and on the installation options. These schemes are stored in sysconfig under unique identifiers based on the value returned by os.name.

Every new component that is installed using distutils or a Distutils-based system will follow the same scheme to copy its file in the right places.

Python currently supports seven schemes:

posix_prefix: scheme for Posix platforms like Linux or Mac OS X. This is the default scheme used when Python or a component is installed.

posix_home: scheme for Posix platforms used when a home option is used upon installation. This scheme is used when a component is installed through Distutils with a specific home prefix.

posix_user: scheme for Posix platforms used when a component is installed through Distutils and the user option is used. This scheme defines paths located under the user home directory.

nt: scheme for NT platforms like Windows.

nt_user: scheme for NT platforms, when the user option is used.

Each scheme is itself composed of a series of paths and each path has a unique identifier. Python currently uses eight paths:

stdlib: directory containing the standard Python library files that are not platform-specific.

platstdlib: directory containing the standard Python library files that are platform-specific.

platlib: directory for site-specific, platform-specific files.

purelib: directory for site-specific, non-platform-specific files.

include: directory for non-platform-specific header files.

platinclude: directory for platform-specific header files.

scripts: directory for script files.

data: directory for data files.

sysconfig provides some functions to determine these paths.

sysconfig.get_scheme_names()

Return a tuple containing all schemes currently supported in sysconfig.

sysconfig.get_path_names()

Return a tuple containing all path names currently supported in sysconfig.

sysconfig.get_path(name[, scheme[, vars[, expand]]])

Return an installation path corresponding to the path name, from the install scheme named scheme.

name has to be a value from the list returned by get_path_names().

sysconfig stores installation paths corresponding to each path name, for each platform, with variables to be expanded. For instance the stdlib path for the nt scheme is: {base}/Lib.

get_path() will use the variables returned by get_config_vars() to expand the path. All variables have default values for each platform so one may call this function and get the default value.

If scheme is provided, it must be a value from the list returned by get_scheme_names(). Otherwise, the default scheme for the current platform is used.

If vars is provided, it must be a dictionary of variables that will update the dictionary return by get_config_vars().

If expand is set to False, the path will not be expanded using the variables.

If name is not found, return None.

sysconfig.get_paths([scheme[, vars[, expand]]])

Return a dictionary containing all installation paths corresponding to an installation scheme. See get_path() for more information.

If scheme is not provided, will use the default scheme for the current platform.

If vars is provided, it must be a dictionary of variables that will update the dictionary used to expand the paths.

If expand is set to false, the paths will not be expanded.

If scheme is not an existing scheme, get_paths() will raise a KeyError.

29.2.3. 其他功能

sysconfig.get_python_version()

Return the MAJOR.MINOR Python version number as a string. Similar to '%d.%d' % sys.version_info[:2].

sysconfig.get_platform()

Return a string that identifies the current platform.

This is used mainly to distinguish platform-specific build directories and platform-specific built distributions. Typically includes the OS name and version and the architecture (as supplied by os.uname()), although the exact information included depends on the OS; e.g. for IRIX the architecture isn’t particularly important (IRIX only runs on SGI hardware), but for Linux the kernel version isn’t particularly important.

返回值的示例：

linux-i586

linux-alpha (?)

solaris-2.6-sun4u

irix-5.3

irix64-6.2

Windows将返回以下之一：

win-amd64 (在 AMD64, aka x86_64, Intel64, 和 EM64T上的64位 Windows )

win-ia64 (64bit Windows on Itanium)

win32 (all others - specifically, sys.platform is returned)

Mac OS X 返回：

macosx-10.6-ppc

macosx-10.4-ppc64

macosx-10.3-i386

macosx-10.4-fat

对于其他非-POSIX 平台， 目前只是返回 sys.platform 。

sysconfig.is_python_build()

Return True if the running Python interpreter was built from source and is being run from its built location, and not from a location resulting from e.g. running make install or installing via a binary installer.

sysconfig.parse_config_h(fp[, vars])

Parse a config.h-style file.

fp is a file-like object pointing to the config.h-like file.

A dictionary containing name/value pairs is returned. If an optional dictionary is passed in as the second argument, it is used instead of a new dictionary, and updated with the values read in the file.

sysconfig.get_config_h_filename()

返回 pyconfig.h 的目录

sysconfig.get_makefile_filename()

返回 Makefile 的目录

29.2.4. Using sysconfig as a script

You can use sysconfig as a script with Python’s -m option:

$ python -m sysconfig
Platform: "macosx-10.4-i386"
Python version: "3.2"
Current installation scheme: "posix_prefix"

Paths:
        data = "/usr/local"
        include = "/Users/tarek/Dev/svn.python.org/py3k/Include"
        platinclude = "."
        platlib = "/usr/local/lib/python3.2/site-packages"
        platstdlib = "/usr/local/lib/python3.2"
        purelib = "/usr/local/lib/python3.2/site-packages"
        scripts = "/usr/local/bin"
        stdlib = "/usr/local/lib/python3.2"

Variables:
        AC_APPLE_UNIVERSAL_BUILD = "0"
        AIX_GENUINE_CPLUSPLUS = "0"
        AR = "ar"
        ARFLAGS = "rc"
        ...

This call will print in the standard output the information returned by get_platform(), get_python_version(), get_path() and get_config_vars().

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

29.3. builtins — 内建对象

该模块提供对Python的所有“内置”标识符的直接访问；例如，builtins.open 是内置函数的全名 open() 。请参阅 内置函数 和 内置常量 的文档。

大多数应用程序通常不会显式访问此模块，但在提供与内置值同名的对象的模块中可能很有用，但其中还需要内置该名称。例如，在一个想要实现 open() 函数的模块中，它包装了内置的 open() ，这个模块可以直接使用:

import builtins

def open(path):
    f = builtins.open(path, 'r')
    return UpperCaser(f)

class UpperCaser:
    '''Wrapper around a file that converts output to upper-case.'''

    def __init__(self, f):
        self._f = f

    def read(self, count=-1):
        return self._f.read(count).upper()

    # ...

作为一个实现细节，大多数模块都将名称 __builtins__ 作为其全局变量的一部分提供。 __builtins__ 的值通常是这个模块或者这个模块的值 __dict__ 属性。由于这是一个实现细节，因此 Python 的替代实现可能不会使用它。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

29.4. __main__ — 顶层脚本环境

'__main__' 是顶层代码执行的作用域的名称。模块的 __name__ 在通过标准输入、脚本文件或是交互式命令读入的时候会等于 '__main__'。

模块可以通过检查自己的 __name__ 来得知是否运行在 main 作用域中，这使得模块可以在作为脚本或是通过 python -m 运行时条件性地执行一些代码，而在被 import 时不会执行。

if __name__ == "__main__":
    # execute only if run as a script
    main()

对软件包来说，通过加入 __main__.py 模块可以达到同样的效果，当使用 -m 运行模块时，其中的代码会被执行。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

29.5. warnings — Warning control

源代码： Lib/warnings.py [https://github.com/python/cpython/tree/3.6/Lib/warnings.py]

Warning messages are typically issued in situations where it is useful to alert the user of some condition in a program, where that condition (normally) doesn’t warrant raising an exception and terminating the program. For example, one might want to issue a warning when a program uses an obsolete module.

Python programmers issue warnings by calling the warn() function defined in this module. (C programmers use PyErr_WarnEx(); see 异常处理 for details).

Warning messages are normally written to sys.stderr, but their disposition can be changed flexibly, from ignoring all warnings to turning them into exceptions. The disposition of warnings can vary based on the warning category (see below), the text of the warning message, and the source location where it is issued. Repetitions of a particular warning for the same source location are typically suppressed.

There are two stages in warning control: first, each time a warning is issued, a determination is made whether a message should be issued or not; next, if a message is to be issued, it is formatted and printed using a user-settable hook.

The determination whether to issue a warning message is controlled by the warning filter, which is a sequence of matching rules and actions. Rules can be added to the filter by calling filterwarnings() and reset to its default state by calling resetwarnings().

The printing of warning messages is done by calling showwarning(), which may be overridden; the default implementation of this function formats the message by calling formatwarning(), which is also available for use by custom implementations.

参见

logging.captureWarnings() allows you to handle all warnings with the standard logging infrastructure.

29.5.1. 警告类别

There are a number of built-in exceptions that represent warning categories. This categorization is useful to be able to filter out groups of warnings. The following warnings category classes are currently defined:

Class

描述

Warning

This is the base class of all warning category classes. It is a subclass of Exception.

UserWarning

The default category for warn().

DeprecationWarning

Base category for warnings about deprecated features (ignored by default).

SyntaxWarning

Base category for warnings about dubious syntactic features.

RuntimeWarning

Base category for warnings about dubious runtime features.

FutureWarning

Base category for warnings about constructs that will change semantically in the future.

PendingDeprecationWarning

Base category for warnings about features that will be deprecated in the future (ignored by default).

ImportWarning

Base category for warnings triggered during the process of importing a module (ignored by default).

UnicodeWarning

Base category for warnings related to Unicode.

BytesWarning

Base category for warnings related to bytes and bytearray.

ResourceWarning

Base category for warnings related to resource usage.

While these are technically built-in exceptions, they are documented here, because conceptually they belong to the warnings mechanism.

User code can define additional warning categories by subclassing one of the standard warning categories. A warning category must always be a subclass of the Warning class.

29.5.2. The Warnings Filter

The warnings filter controls whether warnings are ignored, displayed, or turned into errors (raising an exception).

Conceptually, the warnings filter maintains an ordered list of filter specifications; any specific warning is matched against each filter specification in the list in turn until a match is found; the match determines the disposition of the match. Each entry is a tuple of the form (action, message, category, module, lineno), where:

action is one of the following strings:

值

处置

"error"

将匹配警告转换为异常

"ignore"

从不打印匹配的警告

"always"

总是打印匹配的警告

"default"

print the first occurrence of matching warnings for each location where the warning is issued

"module"

print the first occurrence of matching warnings for each module where the warning is issued

"once"

无论位置如何，仅打印第一次出现的匹配警告

message is a string containing a regular expression that the start of the warning message must match. The expression is compiled to always be case-insensitive.

category is a class (a subclass of Warning) of which the warning category must be a subclass in order to match.

module is a string containing a regular expression that the module name must match. The expression is compiled to be case-sensitive.

lineno is an integer that the line number where the warning occurred must match, or 0 to match all line numbers.

Since the Warning class is derived from the built-in Exception class, to turn a warning into an error we simply raise category(message).

The warnings filter is initialized by -W options passed to the Python interpreter command line. The interpreter saves the arguments for all -W options without interpretation in sys.warnoptions; the warnings module parses these when it is first imported (invalid options are ignored, after printing a message to sys.stderr).

29.5.2.1. Default Warning Filters

By default, Python installs several warning filters, which can be overridden by the command-line options passed to -W and calls to filterwarnings().

DeprecationWarning and PendingDeprecationWarning, and ImportWarning are ignored.

BytesWarning is ignored unless the -b option is given once or twice; in this case this warning is either printed (-b) or turned into an exception (-bb).

ResourceWarning is ignored unless Python was built in debug mode.

在 3.2 版更改: DeprecationWarning is now ignored by default in addition to PendingDeprecationWarning.

29.5.3. 暂时禁止警告

If you are using code that you know will raise a warning, such as a deprecated function, but do not want to see the warning, then it is possible to suppress the warning using the catch_warnings context manager:

import warnings

def fxn():
    warnings.warn("deprecated", DeprecationWarning)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    fxn()

While within the context manager all warnings will simply be ignored. This allows you to use known-deprecated code without having to see the warning while not suppressing the warning for other code that might not be aware of its use of deprecated code. Note: this can only be guaranteed in a single-threaded application. If two or more threads use the catch_warnings context manager at the same time, the behavior is undefined.

29.5.4. 测试警告

To test warnings raised by code, use the catch_warnings context manager. With it you can temporarily mutate the warnings filter to facilitate your testing. For instance, do the following to capture all raised warnings to check:

import warnings

def fxn():
    warnings.warn("deprecated", DeprecationWarning)

with warnings.catch_warnings(record=True) as w:
    # Cause all warnings to always be triggered.
    warnings.simplefilter("always")
    # Trigger a warning.
    fxn()
    # Verify some things
    assert len(w) == 1
    assert issubclass(w[-1].category, DeprecationWarning)
    assert "deprecated" in str(w[-1].message)

One can also cause all warnings to be exceptions by using error instead of always. One thing to be aware of is that if a warning has already been raised because of a once/default rule, then no matter what filters are set the warning will not be seen again unless the warnings registry related to the warning has been cleared.

Once the context manager exits, the warnings filter is restored to its state when the context was entered. This prevents tests from changing the warnings filter in unexpected ways between tests and leading to indeterminate test results. The showwarning() function in the module is also restored to its original value. Note: this can only be guaranteed in a single-threaded application. If two or more threads use the catch_warnings context manager at the same time, the behavior is undefined.

When testing multiple operations that raise the same kind of warning, it is important to test them in a manner that confirms each operation is raising a new warning (e.g. set warnings to be raised as exceptions and check the operations raise exceptions, check that the length of the warning list continues to increase after each operation, or else delete the previous entries from the warnings list before each new operation).

29.5.5. Updating Code For New Versions of Python

Warnings that are only of interest to the developer are ignored by default. As such you should make sure to test your code with typically ignored warnings made visible. You can do this from the command-line by passing -Wd to the interpreter (this is shorthand for -W default). This enables default handling for all warnings, including those that are ignored by default. To change what action is taken for encountered warnings you simply change what argument is passed to -W, e.g. -W error. See the -W flag for more details on what is possible.

To programmatically do the same as -Wd, use:

warnings.simplefilter('default')

Make sure to execute this code as soon as possible. This prevents the registering of what warnings have been raised from unexpectedly influencing how future warnings are treated.

Having certain warnings ignored by default is done to prevent a user from seeing warnings that are only of interest to the developer. As you do not necessarily have control over what interpreter a user uses to run their code, it is possible that a new version of Python will be released between your release cycles. The new interpreter release could trigger new warnings in your code that were not there in an older interpreter, e.g. DeprecationWarning for a module that you are using. While you as a developer want to be notified that your code is using a deprecated module, to a user this information is essentially noise and provides no benefit to them.

The unittest module has been also updated to use the 'default' filter while running tests.

29.5.6. Available Functions

warnings.warn(message, category=None, stacklevel=1, source=None)

Issue a warning, or maybe ignore it or raise an exception. The category argument, if given, must be a warning category class (see above); it defaults to UserWarning. Alternatively message can be a Warning instance, in which case category will be ignored and message.__class__ will be used. In this case the message text will be str(message). This function raises an exception if the particular warning issued is changed into an error by the warnings filter see above. The stacklevel argument can be used by wrapper functions written in Python, like this:

def deprecation(message):
    warnings.warn(message, DeprecationWarning, stacklevel=2)

This makes the warning refer to deprecation()’s caller, rather than to the source of deprecation() itself (since the latter would defeat the purpose of the warning message).

source, if supplied, is the destroyed object which emitted a ResourceWarning.

在 3.6 版更改: Added source parameter.

warnings.warn_explicit(message, category, filename, lineno, module=None, registry=None, module_globals=None, source=None)

This is a low-level interface to the functionality of warn(), passing in explicitly the message, category, filename and line number, and optionally the module name and the registry (which should be the __warningregistry__ dictionary of the module). The module name defaults to the filename with .py stripped; if no registry is passed, the warning is never suppressed. message must be a string and category a subclass of Warning or message may be a Warning instance, in which case category will be ignored.

module_globals, if supplied, should be the global namespace in use by the code for which the warning is issued. (This argument is used to support displaying source for modules found in zipfiles or other non-filesystem import sources).

source, if supplied, is the destroyed object which emitted a ResourceWarning.

在 3.6 版更改: Add the source parameter.

warnings.showwarning(message, category, filename, lineno, file=None, line=None)

Write a warning to a file. The default implementation calls formatwarning(message, category, filename, lineno, line) and writes the resulting string to file, which defaults to sys.stderr. You may replace this function with any callable by assigning to warnings.showwarning. line is a line of source code to be included in the warning message; if line is not supplied, showwarning() will try to read the line specified by filename and lineno.

warnings.formatwarning(message, category, filename, lineno, line=None)

Format a warning the standard way. This returns a string which may contain embedded newlines and ends in a newline. line is a line of source code to be included in the warning message; if line is not supplied, formatwarning() will try to read the line specified by filename and lineno.

warnings.filterwarnings(action, message='', category=Warning, module='', lineno=0, append=False)

Insert an entry into the list of warnings filter specifications. The entry is inserted at the front by default; if append is true, it is inserted at the end. This checks the types of the arguments, compiles the message and module regular expressions, and inserts them as a tuple in the list of warnings filters. Entries closer to the front of the list override entries later in the list, if both match a particular warning. Omitted arguments default to a value that matches everything.

warnings.simplefilter(action, category=Warning, lineno=0, append=False)

Insert a simple entry into the list of warnings filter specifications. The meaning of the function parameters is as for filterwarnings(), but regular expressions are not needed as the filter inserted always matches any message in any module as long as the category and line number match.

warnings.resetwarnings()

Reset the warnings filter. This discards the effect of all previous calls to filterwarnings(), including that of the -W command line options and calls to simplefilter().

29.5.7. Available Context Managers

class warnings.catch_warnings(*, record=False, module=None)

A context manager that copies and, upon exit, restores the warnings filter and the showwarning() function. If the record argument is False (the default) the context manager returns None on entry. If record is True, a list is returned that is progressively populated with objects as seen by a custom showwarning() function (which also suppresses output to sys.stdout). Each object in the list has attributes with the same names as the arguments to showwarning().

The module argument takes a module that will be used instead of the module returned when you import warnings whose filter will be protected. This argument exists primarily for testing the warnings module itself.

注解

The catch_warnings manager works by replacing and then later restoring the module’s showwarning() function and internal list of filter specifications. This means the context manager is modifying global state and therefore is not thread-safe.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

29.6. contextlib — Utilities for with-statement contexts

源代码 Lib/contextlib.py [https://github.com/python/cpython/tree/3.6/Lib/contextlib.py]

此模块为涉及 with 语句的常见任务提供了实用的程序。更多信息请参见 上下文管理器类型 和 with 语句上下文管理器。

29.6.1. 工具

提供的函数和类：

class contextlib.AbstractContextManager

一个为实现了 object.__aenter__() 与 object.__aexit__() 的类提供的 abstract base class。 为 object.__aenter__() 提供的一个默认实现是返回 self 而 object.__aexit__() 是一个默认返回 None 的抽象方法。 参见 async-context-managers 的定义。

3.6 新版功能.

@contextlib.contextmanager

这个函数是一个 decorator ，它可以定义一个支持 with 语句上下文的工厂函数， 而不需要创建一个类或区 __enter__() 与 __exit__() 方法。

尽管许多对象原生支持使用 with 语句，但有些需要被管理的资源并不是上下文管理器，并且没有实现 close() 方法而不能使用 contextlib.closing 。

下面是一个抽象的示例，展示如何确保正确的资源管理：

from contextlib import contextmanager

@contextmanager
def managed_resource(*args, **kwds):
    # Code to acquire resource, e.g.:
    resource = acquire_resource(*args, **kwds)
    try:
        yield resource
    finally:
        # Code to release resource, e.g.:
        release_resource(resource)

>>> with managed_resource(timeout=3600) as resource:
...     # Resource is released at the end of this block,
...     # even if code in the block raises an exception

The function being decorated must return a generator-iterator when called. This iterator must yield exactly one value, which will be bound to the targets in the with statement’s as clause, if any.

At the point where the generator yields, the block nested in the with statement is executed. The generator is then resumed after the block is exited. If an unhandled exception occurs in the block, it is reraised inside the generator at the point where the yield occurred. Thus, you can use a try…except…finally statement to trap the error (if any), or ensure that some cleanup takes place. If an exception is trapped merely in order to log it or to perform some action (rather than to suppress it entirely), the generator must reraise that exception. Otherwise the generator context manager will indicate to the with statement that the exception has been handled, and execution will resume with the statement immediately following the with statement.

contextmanager() 使用 ContextDecorator 因此它创建的上下文管理器不仅可以用在 with 语句中，还可以用作一个装饰器。当它用作一个装饰器时，每一次函数调用时都会隐式创建一个新的生成器实例（这使得 contextmanager() 创建的上下文管理器满足了支持多次调用以用作装饰器的需求，而非“一次性”的上下文管理器）。

在 3.2 版更改: ContextDecorator 的使用。

contextlib.closing(thing)

返回一个在语句块执行完成时关闭 things 的上下文管理器。这基本上等价于

from contextlib import contextmanager

@contextmanager
def closing(thing):
    try:
        yield thing
    finally:
        thing.close()

并允许你编写这样的代码

from contextlib import closing
from urllib.request import urlopen

with closing(urlopen('http://www.python.org')) as page:
    for line in page:
        print(line)

而无需显式地关闭 page 。 即使发生错误，在退出 with 语句块时， page.close() 也同样会被调用。

contextlib.suppress(*exceptions)

返回一个上下文管理器，如果任何一个指定的异常发生在使用该上下文管理器的 with 语句中，该异常将被它抑制，然后程序将从 with 语句结束后的第一个语句开始恢复执行。

与完全抑制异常的任何其他机制一样，该上下文管理器应当只用来抑制非常具体的错误，并确保该场景下静默地继续执行程序是通用的正确做法。

例如

from contextlib import suppress

with suppress(FileNotFoundError):
    os.remove('somefile.tmp')

with suppress(FileNotFoundError):
    os.remove('someotherfile.tmp')

这段代码等价于：

try:
    os.remove('somefile.tmp')
except FileNotFoundError:
    pass

try:
    os.remove('someotherfile.tmp')
except FileNotFoundError:
    pass

该上下文管理器是 reentrant 。

3.4 新版功能.

contextlib.redirect_stdout(new_target)

用于将 sys.stdout 临时重定向到一个文件或类文件对象的上下文管理器。

该工具给已有的将输出硬编码写到 stdout 的函数或类提供了额外的灵活性。

例如， help() 通常把输出写到 sys.stdout 。你可以通过重定向到一个 io.StringIO 来捕获该输出到一个字符串中。

f = io.StringIO()
with redirect_stdout(f):
    help(pow)
s = f.getvalue()

如果要把 help() 的输出写到磁盘上的一个文件，重定向该输出到一个常规文件：

with open('help.txt', 'w') as f:
    with redirect_stdout(f):
        help(pow)

如果要把 help() 的输出写到 sys.stderr ：

with redirect_stdout(sys.stderr):
    help(pow)

需要注意的点在于， sys.stdout 的全局副作用意味着此上下文管理器不适合在库代码和大多数多线程应用程序中使用。它对子进程的输出没有影响。不过对于许多工具脚本而言，它仍然是一个有用的方法。

该上下文管理器是 reentrant 。

3.4 新版功能.

contextlib.redirect_stderr(new_target)

与 redirect_stdout() 类似，不过是将 sys.stderr 重定向到一个文件或类文件对象。

该上下文管理器是 reentrant 。

3.5 新版功能.

class contextlib.ContextDecorator

一个使上下文管理器能用作装饰器的基类。

与往常一样，继承自 ContextDecorator  的上下文管理器必须实现 __enter__ 与 __exit__ 。即使用作装饰器， __exit__ 依旧会保持可能的异常处理。

ContextDecorator 被用在 contextmanager() 中，因此你自然获得了这项功能。

ContextDecorator 的示例:

from contextlib import ContextDecorator

class mycontext(ContextDecorator):
    def __enter__(self):
        print('Starting')
        return self

    def __exit__(self, *exc):
        print('Finishing')
        return False

>>> @mycontext()
... def function():
...     print('The bit in the middle')
...
>>> function()
Starting
The bit in the middle
Finishing

>>> with mycontext():
...     print('The bit in the middle')
...
Starting
The bit in the middle
Finishing

这个改动只是针对如下形式的一个语法糖：

def f():
    with cm():
        # Do stuff

ContextDecorator 使得你可以这样改写：

@cm()
def f():
    # Do stuff

这能清楚地表明， cm 作用于整个函数，而不仅仅是函数的一部分（同时也能保持不错的缩进层级）。

现有的上下文管理器即使已经有基类，也可以使用 ContextDecorator 作为混合类进行扩展：

from contextlib import ContextDecorator

class mycontext(ContextBaseClass, ContextDecorator):
    def __enter__(self):
        return self

    def __exit__(self, *exc):
        return False

注解

As the decorated function must be able to be called multiple times, the underlying context manager must support use in multiple with statements. If this is not the case, then the original construct with the explicit with statement inside the function should be used.

3.2 新版功能.

class contextlib.ExitStack

该上下文管理器的设计目标是使得在编码中组合其他上下文管理器和清理函数更加容易，尤其是那些可选的或由输入数据驱动的上下文管理器。

例如，通过一个如下的 with 语句可以很容易处理一组文件：

with ExitStack() as stack:
    files = [stack.enter_context(open(fname)) for fname in filenames]
    # All opened files will automatically be closed at the end of
    # the with statement, even if attempts to open files later
    # in the list raise an exception

每个实例维护一个注册了一组回调的栈，这些回调在实例关闭时以相反的顺序被调用（显式或隐式地在 with 语句的末尾）。请注意，当一个栈实例被垃圾回收时，这些回调将 不会 被隐式调用。

通过使用这个基于栈的模型，那些通过 __init__ 方法获取资源的上下文管理器（如文件对象）能够被正确处理。

由于注册的回调函数是按照与注册相反的顺序调用的，因此最终的行为就像多个嵌套的 with 语句用在这些注册的回调函数上。这个行为甚至扩展到了异常处理：如果内部的回调函数抑制或替换了异常，则外部回调收到的参数是基于该更新后的状态得到的。

这是一个相对底层的 API，它负责正确处理栈里回调退出时依次展开的细节。它为相对高层的上下文管理器提供了一个合适的基础，使得它能根据应用程序的需求使用特定方式操作栈。

3.3 新版功能.

enter_context(cm)

Enters a new context manager and adds its __exit__() method to the callback stack. The return value is the result of the context manager’s own __enter__() method.

These context managers may suppress exceptions just as they normally would if used directly as part of a with statement.

push(exit)

Adds a context manager’s __exit__() method to the callback stack.

As __enter__ is not invoked, this method can be used to cover part of an __enter__() implementation with a context manager’s own __exit__() method.

If passed an object that is not a context manager, this method assumes it is a callback with the same signature as a context manager’s __exit__() method and adds it directly to the callback stack.

By returning true values, these callbacks can suppress exceptions the same way context manager __exit__() methods can.

The passed in object is returned from the function, allowing this method to be used as a function decorator.

callback(callback, *args, **kwds)

Accepts an arbitrary callback function and arguments and adds it to the callback stack.

Unlike the other methods, callbacks added this way cannot suppress exceptions (as they are never passed the exception details).

The passed in callback is returned from the function, allowing this method to be used as a function decorator.

pop_all()

Transfers the callback stack to a fresh ExitStack instance and returns it. No callbacks are invoked by this operation - instead, they will now be invoked when the new stack is closed (either explicitly or implicitly at the end of a with statement).

For example, a group of files can be opened as an “all or nothing” operation as follows:

with ExitStack() as stack:
    files = [stack.enter_context(open(fname)) for fname in filenames]
    # Hold onto the close method, but don't call it yet.
    close_files = stack.pop_all().close
    # If opening any file fails, all previously opened files will be
    # closed automatically. If all files are opened successfully,
    # they will remain open even after the with statement ends.
    # close_files() can then be invoked explicitly to close them all.

close()

Immediately unwinds the callback stack, invoking callbacks in the reverse order of registration. For any context managers and exit callbacks registered, the arguments passed in will indicate that no exception occurred.

29.6.2. 例子和配方

This section describes some examples and recipes for making effective use of the tools provided by contextlib.

29.6.2.1. Supporting a variable number of context managers

The primary use case for ExitStack is the one given in the class documentation: supporting a variable number of context managers and other cleanup operations in a single with statement. The variability may come from the number of context managers needed being driven by user input (such as opening a user specified collection of files), or from some of the context managers being optional:

with ExitStack() as stack:
    for resource in resources:
        stack.enter_context(resource)
    if need_special_resource():
        special = acquire_special_resource()
        stack.callback(release_special_resource, special)
    # Perform operations that use the acquired resources

As shown, ExitStack also makes it quite easy to use with statements to manage arbitrary resources that don’t natively support the context management protocol.

29.6.2.2. Simplifying support for single optional context managers

In the specific case of a single optional context manager, ExitStack instances can be used as a “do nothing” context manager, allowing a context manager to easily be omitted without affecting the overall structure of the source code:

def debug_trace(details):
    if __debug__:
        return TraceContext(details)
    # Don't do anything special with the context in release mode
    return ExitStack()

with debug_trace():
    # Suite is traced in debug mode, but runs normally otherwise

29.6.2.3. Catching exceptions from __enter__ methods

It is occasionally desirable to catch exceptions from an __enter__ method implementation, without inadvertently catching exceptions from the with statement body or the context manager’s __exit__ method. By using ExitStack the steps in the context management protocol can be separated slightly in order to allow this:

stack = ExitStack()
try:
    x = stack.enter_context(cm)
except Exception:
    # handle __enter__ exception
else:
    with stack:
        # Handle normal case

Actually needing to do this is likely to indicate that the underlying API should be providing a direct resource management interface for use with try/except/finally statements, but not all APIs are well designed in that regard. When a context manager is the only resource management API provided, then ExitStack can make it easier to handle various situations that can’t be handled directly in a with statement.

29.6.2.4. Cleaning up in an __enter__ implementation

As noted in the documentation of ExitStack.push(), this method can be useful in cleaning up an already allocated resource if later steps in the __enter__() implementation fail.

Here’s an example of doing this for a context manager that accepts resource acquisition and release functions, along with an optional validation function, and maps them to the context management protocol:

from contextlib import contextmanager, AbstractContextManager, ExitStack

class ResourceManager(AbstractContextManager):

    def __init__(self, acquire_resource, release_resource, check_resource_ok=None):
        self.acquire_resource = acquire_resource
        self.release_resource = release_resource
        if check_resource_ok is None:
            def check_resource_ok(resource):
                return True
        self.check_resource_ok = check_resource_ok

    @contextmanager
    def _cleanup_on_error(self):
        with ExitStack() as stack:
            stack.push(self)
            yield
            # The validation check passed and didn't raise an exception
            # Accordingly, we want to keep the resource, and pass it
            # back to our caller
            stack.pop_all()

    def __enter__(self):
        resource = self.acquire_resource()
        with self._cleanup_on_error():
            if not self.check_resource_ok(resource):
                msg = "Failed validation for {!r}"
                raise RuntimeError(msg.format(resource))
        return resource

    def __exit__(self, *exc_details):
        # We don't need to duplicate any of our resource release logic
        self.release_resource()

29.6.2.5. Replacing any use of try-finally and flag variables

A pattern you will sometimes see is a try-finally statement with a flag variable to indicate whether or not the body of the finally clause should be executed. In its simplest form (that can’t already be handled just by using an except clause instead), it looks something like this:

cleanup_needed = True
try:
    result = perform_operation()
    if result:
        cleanup_needed = False
finally:
    if cleanup_needed:
        cleanup_resources()

As with any try statement based code, this can cause problems for development and review, because the setup code and the cleanup code can end up being separated by arbitrarily long sections of code.

ExitStack makes it possible to instead register a callback for execution at the end of a with statement, and then later decide to skip executing that callback:

from contextlib import ExitStack

with ExitStack() as stack:
    stack.callback(cleanup_resources)
    result = perform_operation()
    if result:
        stack.pop_all()

This allows the intended cleanup up behaviour to be made explicit up front, rather than requiring a separate flag variable.

If a particular application uses this pattern a lot, it can be simplified even further by means of a small helper class:

from contextlib import ExitStack

class Callback(ExitStack):
    def __init__(self, callback, *args, **kwds):
        super(Callback, self).__init__()
        self.callback(callback, *args, **kwds)

    def cancel(self):
        self.pop_all()

with Callback(cleanup_resources) as cb:
    result = perform_operation()
    if result:
        cb.cancel()

If the resource cleanup isn’t already neatly bundled into a standalone function, then it is still possible to use the decorator form of ExitStack.callback() to declare the resource cleanup in advance:

from contextlib import ExitStack

with ExitStack() as stack:
    @stack.callback
    def cleanup_resources():
        ...
    result = perform_operation()
    if result:
        stack.pop_all()

Due to the way the decorator protocol works, a callback function declared this way cannot take any parameters. Instead, any resources to be released must be accessed as closure variables.

29.6.2.6. Using a context manager as a function decorator

ContextDecorator makes it possible to use a context manager in both an ordinary with statement and also as a function decorator.

For example, it is sometimes useful to wrap functions or groups of statements with a logger that can track the time of entry and time of exit. Rather than writing both a function decorator and a context manager for the task, inheriting from ContextDecorator provides both capabilities in a single definition:

from contextlib import ContextDecorator
import logging

logging.basicConfig(level=logging.INFO)

class track_entry_and_exit(ContextDecorator):
    def __init__(self, name):
        self.name = name

    def __enter__(self):
        logging.info('Entering: %s', self.name)

    def __exit__(self, exc_type, exc, exc_tb):
        logging.info('Exiting: %s', self.name)

Instances of this class can be used as both a context manager:

with track_entry_and_exit('widget loader'):
    print('Some time consuming activity goes here')
    load_widget()

And also as a function decorator:

@track_entry_and_exit('widget loader')
def activity():
    print('Some time consuming activity goes here')
    load_widget()

Note that there is one additional limitation when using context managers as function decorators: there’s no way to access the return value of __enter__(). If that value is needed, then it is still necessary to use an explicit with statement.

参见

PEP 343 [https://www.python.org/dev/peps/pep-0343] - “with” 语句

Python with 语句的规范描述、背景和示例。

29.6.3. Single use, reusable and reentrant context managers

Most context managers are written in a way that means they can only be used effectively in a with statement once. These single use context managers must be created afresh each time they’re used - attempting to use them a second time will trigger an exception or otherwise not work correctly.

This common limitation means that it is generally advisable to create context managers directly in the header of the with statement where they are used (as shown in all of the usage examples above).

Files are an example of effectively single use context managers, since the first with statement will close the file, preventing any further IO operations using that file object.

Context managers created using contextmanager() are also single use context managers, and will complain about the underlying generator failing to yield if an attempt is made to use them a second time:

>>> from contextlib import contextmanager
>>> @contextmanager
... def singleuse():
...     print("Before")
...     yield
...     print("After")
...
>>> cm = singleuse()
>>> with cm:
...     pass
...
Before
After
>>> with cm:
...     pass
...
Traceback (most recent call last):
    ...
RuntimeError: generator didn't yield

29.6.3.1. Reentrant context managers

More sophisticated context managers may be “reentrant”. These context managers can not only be used in multiple with statements, but may also be used inside a with statement that is already using the same context manager.

threading.RLock is an example of a reentrant context manager, as are suppress() and redirect_stdout(). Here’s a very simple example of reentrant use:

>>> from contextlib import redirect_stdout
>>> from io import StringIO
>>> stream = StringIO()
>>> write_to_stream = redirect_stdout(stream)
>>> with write_to_stream:
...     print("This is written to the stream rather than stdout")
...     with write_to_stream:
...         print("This is also written to the stream")
...
>>> print("This is written directly to stdout")
This is written directly to stdout
>>> print(stream.getvalue())
This is written to the stream rather than stdout
This is also written to the stream

Real world examples of reentrancy are more likely to involve multiple functions calling each other and hence be far more complicated than this example.

Note also that being reentrant is not the same thing as being thread safe. redirect_stdout(), for example, is definitely not thread safe, as it makes a global modification to the system state by binding sys.stdout to a different stream.

29.6.3.2. Reusable context managers

Distinct from both single use and reentrant context managers are “reusable” context managers (or, to be completely explicit, “reusable, but not reentrant” context managers, since reentrant context managers are also reusable). These context managers support being used multiple times, but will fail (or otherwise not work correctly) if the specific context manager instance has already been used in a containing with statement.

threading.Lock is an example of a reusable, but not reentrant, context manager (for a reentrant lock, it is necessary to use threading.RLock instead).

Another example of a reusable, but not reentrant, context manager is ExitStack, as it invokes all currently registered callbacks when leaving any with statement, regardless of where those callbacks were added:

>>> from contextlib import ExitStack
>>> stack = ExitStack()
>>> with stack:
...     stack.callback(print, "Callback: from first context")
...     print("Leaving first context")
...
Leaving first context
Callback: from first context
>>> with stack:
...     stack.callback(print, "Callback: from second context")
...     print("Leaving second context")
...
Leaving second context
Callback: from second context
>>> with stack:
...     stack.callback(print, "Callback: from outer context")
...     with stack:
...         stack.callback(print, "Callback: from inner context")
...         print("Leaving inner context")
...     print("Leaving outer context")
...
Leaving inner context
Callback: from inner context
Callback: from outer context
Leaving outer context

As the output from the example shows, reusing a single stack object across multiple with statements works correctly, but attempting to nest them will cause the stack to be cleared at the end of the innermost with statement, which is unlikely to be desirable behaviour.

Using separate ExitStack instances instead of reusing a single instance avoids that problem:

>>> from contextlib import ExitStack
>>> with ExitStack() as outer_stack:
...     outer_stack.callback(print, "Callback: from outer context")
...     with ExitStack() as inner_stack:
...         inner_stack.callback(print, "Callback: from inner context")
...         print("Leaving inner context")
...     print("Leaving outer context")
...
Leaving inner context
Callback: from inner context
Leaving outer context
Callback: from outer context

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

29.7. abc — 抽象基类

源代码： Lib/abc.py [https://github.com/python/cpython/tree/3.6/Lib/abc.py]

该模块提供了在 Python 中定义 抽象基类 (ABC) 的组件，在 PEP 3119 [https://www.python.org/dev/peps/pep-3119] 中已有概述。查看 PEP 文档了解为什么需要在 Python 中增加这个模块。（也可查看 PEP 3141 [https://www.python.org/dev/peps/pep-3141] 以及 numbers 模块了解基于 ABC 的数字类型继承关系。）

collections 模块中有一些派生自 ABC 的具体类；当然这些类还可以进一步被派生。此外，collections.abc 子模块中有一些 ABC 可被用于测试一个类或实例是否提供特定的接口，例如它是否可哈希或它是否为映射等。

该模块提供了一个元类 ABCMeta，可以用来定义抽象类，另外还提供一个工具类 ABC，可以用它以继承的方式定义抽象基类。

class abc.ABC

一个使用 ABCMeta 作为元类的工具类。抽象基类可以通过从 ABC 派生来简单地创建，这就避免了在某些情况下会令人混淆的元类用法，例如：

from abc import ABC

class MyABC(ABC):
    pass

注意 ABC 的类型仍然是 ABCMeta，因此继承 ABC 仍然需要关注元类使用中的注意事项，比如可能会导致元类冲突的多重继承。当然你也可以直接使用 ABCMeta 作为元类来定义抽象基类，例如：

from abc import ABCMeta

class MyABC(metaclass=ABCMeta):
    pass

3.4 新版功能.

class abc.ABCMeta

用于定义抽象基类（ABC）的元类。

使用该元类以创建抽象基类。抽象基类可以像 mix-in 类一样直接被子类继承。你也可以将不相关的具体类（包括内建类）和抽象基类注册为“抽象子类” —— 这些类以及它们的子类会被内建函数 issubclass() 识别为对应的抽象基类的子类，但是该抽象基类不会出现在其 MRO（Method Resolution Order，方法解析顺序）中，抽象基类中实现的方法也不可调用（即使通过 super() 调用也不行）。1

使用 ABCMeta 作为元类创建的类含有如下方法：

register(subclass)

将“子类”注册为该抽象基类的“抽象子类”，例如：

from abc import ABC

class MyABC(ABC):
    pass

MyABC.register(tuple)

assert issubclass(tuple, MyABC)
assert isinstance((), MyABC)

在 3.3 版更改: 返回注册的子类，使其能够作为类装饰器。

在 3.4 版更改: 你可以使用 get_cache_token() 函数来检测对 register() 的调用。

你也可以在虚基类中重载这个方法。

__subclasshook__(subclass)

（必须定义为类方法。）

检查 subclass 是否是该抽象基类的子类。也就是说对于那些你希望定义为该抽象基类的子类的类，你不用对每个类都调用 register() 方法了，而是可以直接自定义 issubclass 的行为。（这个类方法是在抽象基类的 __subclasscheck__() 方法中调用的。）

该方法必须返回 True, False 或是 NotImplemented。如果返回 True，subclass 就会被认为是这个抽象基类的子类。如果返回 False，无论正常情况是否应该认为是其子类，统一视为不是。如果返回 NotImplemented，子类检查会按照正常机制继续执行。

为了对这些概念做一演示，请看以下定义 ABC 的示例：

class Foo:
    def __getitem__(self, index):
        ...
    def __len__(self):
        ...
    def get_iterator(self):
        return iter(self)

class MyIterable(ABC):

    @abstractmethod
    def __iter__(self):
        while False:
            yield None

    def get_iterator(self):
        return self.__iter__()

    @classmethod
    def __subclasshook__(cls, C):
        if cls is MyIterable:
            if any("__iter__" in B.__dict__ for B in C.__mro__):
                return True
        return NotImplemented

MyIterable.register(Foo)

ABC MyIterable 定义了标准的迭代方法 __iter__() 作为一个抽象方法。这里给出的实现仍可在子类中被调用。get_iterator() 方法也是 MyIterable 抽象基类的一部分，但它并非必须被非抽象派生类所重载。

这里定义的 __subclasshook__() 类方法指明了任何在其 __dict__ (或在其通过 __mro__ 列表访问的基类) 中具有 __iter__() 方法的类也都会被视为 MyIterable。

最后，末尾行使得 Foo 成为 MyIterable 的一个虚子类，即使它没有定义 __iter__() 方法（它使用了以 __len__() 和 __getitem__() 术语定义的旧式可迭代对象协议）。 请注意这将不会使 get_iterator 成为 Foo 的一个可用方法，它是被另外提供的。

此外，abc 模块还提供了这些装饰器：

@abc.abstractmethod

用于声明抽象方法的装饰器。

使用此装饰器要求类的元类是 ABCMeta 或是从该类派生。一个具有派生自 ABCMeta 的元类的类不可以被实例化，除非它全部的抽象方法和特征属性均已被重载。抽象方法可通过任何普通的“super”调用机制来调用。 abstractmethod() 可被用于声明特性属性和描述器的抽象方法。

不支持动态添加抽象方法到一个类，或试图在方法或类被创建后修改其抽象状态等操作。 abstractmethod() 只会影响使用常规继承所派生的子类；通过 ABC 的 register() 方法注册的“虚子类”不会受到影响。

当 abstractmethod() 与其他方法描述符配合应用时，它应当被应用为最内层的装饰器，如以下用法示例所示:

class C(ABC):
    @abstractmethod
    def my_abstract_method(self, ...):
        ...
    @classmethod
    @abstractmethod
    def my_abstract_classmethod(cls, ...):
        ...
    @staticmethod
    @abstractmethod
    def my_abstract_staticmethod(...):
        ...

    @property
    @abstractmethod
    def my_abstract_property(self):
        ...
    @my_abstract_property.setter
    @abstractmethod
    def my_abstract_property(self, val):
        ...

    @abstractmethod
    def _get_x(self):
        ...
    @abstractmethod
    def _set_x(self, val):
        ...
    x = property(_get_x, _set_x)

为了能正确地与抽象基类机制实现互操作，描述符必须使用 __isabstractmethod__ 将自身标识为抽象的。 通常，如果被用于组成描述符的任何方法都是抽象的则此属性应当为 True。 例如，Python 的内置 property 所做的就等价于:

class Descriptor:
    ...
    @property
    def __isabstractmethod__(self):
        return any(getattr(f, '__isabstractmethod__', False) for
                   f in (self._fget, self._fset, self._fdel))

注解

不同于 Java 抽象方法，这些抽象方法可能具有一个实现。 这个实现可在重载它的类上通过 super() 机制来调用。 这在使用协作多重继承的框架中可以被用作超调用的一个端点。

abc 模块还支持下列旧式装饰器:

@abc.abstractclassmethod

3.2 新版功能.

3.3 版后已移除: 现在可以让 classmethod 配合 abstractmethod() 使用，使得此装饰器变得冗余。

内置 classmethod() 的子类，指明一个抽象类方法。 在其他情况下它都类似于 abstractmethod()。

这个特殊情况已被弃用，因为现在 classmethod() 当将装饰器应用于抽象方法时它会被正确地标识为抽象的:

class C(ABC):
    @classmethod
    @abstractmethod
    def my_abstract_classmethod(cls, ...):
        ...

@abc.abstractstaticmethod

3.2 新版功能.

3.3 版后已移除: 现在可以让 staticmethod 配合 abstractmethod() 使用，使得此装饰器变得冗余。

内置 staticmethod() 的子类，指明一个抽象静态方法。 在其他方面它都类似于 abstractmethod()。

这个特殊情况已被弃用，因为现在当 staticmethod() 装饰器应用于抽象方法时它会被正确地标识为抽象的:

class C(ABC):
    @staticmethod
    @abstractmethod
    def my_abstract_staticmethod(...):
        ...

@abc.abstractproperty

3.3 版后已移除: 现在可以让 property, property.getter(), property.setter() 和 property.deleter() 配合 abstractmethod() 使用，使得此装饰器变得冗余。

内置 property() 的子类，指明一个抽象特性属性。

这个特例已被弃用，因为现在当 property() 装饰器应用于抽象方法时它会被正确地标识为抽象的:

class C(ABC):
    @property
    @abstractmethod
    def my_abstract_property(self):
        ...

上面的例子定义了一个只读特征属性；你也可以通过适当地将一个或多个下层方法标记为抽象的来定义可读写的抽象特征属性:

class C(ABC):
    @property
    def x(self):
        ...

    @x.setter
    @abstractmethod
    def x(self, val):
        ...

如果只有某些组件是抽象的，则只需更新那些组件即可在子类中创建具体的特征属性:

class D(C):
    @C.x.setter
    def x(self, val):
        ...

abc 模块还提供了这些函数：

abc.get_cache_token()

返回当前抽象基类的缓存令牌

此令牌是一个不透明对象（支持相等性测试），用于为虚子类标识抽象基类缓存的当前版本。 此令牌会在任何 ABC 上每次调用 ABCMeta.register() 时发生更改。

3.4 新版功能.

备注

1

C++ 程序员需要注意：Python 中虚基类的概念和 C++ 中的并不相同。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

29.8. atexit — 退出处理器

atexit 模块定义了清理函数的注册和反注册函数. 被注册的函数会在解释器正常终止时执行. atexit 会按照注册顺序的*逆序*执行; 如果你注册了 A, B 和 C, 那么在解释器终止时会依序执行 C, B, A.

注意: 通过该模块注册的函数, 在程序被未被 Python 捕获的信号杀死时并不会执行, 在检测到 Python 内部致命错误以及调用了 os._exit() 时也不会执行.

atexit.register(func, *args, **kwargs)

将 func 注册为终止时执行的函数. 任何传给 func 的可选的参数都应当作为参数传给 register(). 可以多次注册同样的函数及参数.

在正常的程序终止时 (举例来说, 当调用了 sys.exit() 或是主模块的执行完成时), 所有注册过的函数都会以后进先出的顺序执行. 这样做是假定更底层的模块通常会比高层模块更早引入, 因此需要更晚清理.

如果在 exit 处理程序执行期间引发了异常，将会打印回溯信息 (除非引发的是 SystemExit) 并且异常信息会被保存。 在所有 exit 处理程序获得运行机会之后，所引发的最后一个异常会被重新引发。

这个函数返回 func 对象，可以把它当作装饰器使用。

atexit.unregister(func)

从解释器关闭前要运行的函数列表中移除 func。 在调用 unregister() 之后，当解释器关闭时会确保 func 不会被调用，即使它被多次注册。 如果 func 之前没有被注册，unregister() 会静默地不做任何操作。

参见

模块 readline

使用 atexit 读写 readline 历史文件的有用的例子。

29.8.1. atexit 示例

以下简单例子演示了一个模块在被导入时如何从文件初始化一个计数器，并在程序终结时自动保存计数器的更新值，此操作不依赖于应用在终结时对此模块进行显式调用。:

try:
    with open("counterfile") as infile:
        _count = int(infile.read())
except FileNotFoundError:
    _count = 0

def incrcounter(n):
    global _count
    _count = _count + n

def savecounter():
    with open("counterfile", "w") as outfile:
        outfile.write("%d" % _count)

import atexit
atexit.register(savecounter)

位置和关键字参数也可传入 register() 以便传递给被调用的已注册函数:

def goodbye(name, adjective):
    print('Goodbye, %s, it was %s to meet you.' % (name, adjective))

import atexit
atexit.register(goodbye, 'Donny', 'nice')

# or:
atexit.register(goodbye, adjective='nice', name='Donny')

作为 decorator: 使用:

import atexit

@atexit.register
def goodbye():
    print("You are now leaving the Python sector.")

只有在函数不需要任何参数调用时才能工作.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

29.9. traceback — 打印或检索堆栈回溯

源代码： Lib/traceback.py [https://github.com/python/cpython/tree/3.6/Lib/traceback.py]

该模块提供了一个标准接口来提取、格式化和打印 Python 程序的堆栈跟踪结果。它完全模仿Python 解释器在打印堆栈跟踪结果时的行为。当您想要在程序控制下打印堆栈跟踪结果时，例如在“封装”解释器时，这是非常有用的。

这个模块使用 traceback 对象 —— 这是存储在 sys.last_traceback 中的对象类型变量，并作为 sys.exc_info() 的第三项被返回。

这个模块定义了以下函数：

traceback.print_tb(tb, limit=None, file=None)

Print up to limit stack trace entries from traceback object tb (starting from the caller’s frame) if limit is positive. Otherwise, print the last abs(limit) entries. If limit is omitted or None, all entries are printed. If file is omitted or None, the output goes to sys.stderr; otherwise it should be an open file or file-like object to receive the output.

在 3.5 版更改: Added negative limit support.

traceback.print_exception(etype, value, tb, limit=None, file=None, chain=True)

打印回溯对象 tb 到 file 的异常信息和整个堆栈回溯。这和 print_tb() 比有以下方面不同：

如果 tb 不为 None，它将打印头部 Traceback (most recent call last):

it prints the exception etype and value after the stack trace

if type(value) is SyntaxError and value has the appropriate format, it prints the line where the syntax error occurred with a caret indicating the approximate position of the error.

The optional limit argument has the same meaning as for print_tb(). If chain is true (the default), then chained exceptions (the __cause__ or __context__ attributes of the exception) will be printed as well, like the interpreter itself does when printing an unhandled exception.

在 3.5 版更改: The etype argument is ignored and inferred from the type of value.

traceback.print_exc(limit=None, file=None, chain=True)

This is a shorthand for print_exception(*sys.exc_info(), limit, file, chain).

traceback.print_last(limit=None, file=None, chain=True)

This is a shorthand for print_exception(sys.last_type, sys.last_value, sys.last_traceback, limit, file, chain). In general it will work only after an exception has reached an interactive prompt (see sys.last_type).

traceback.print_stack(f=None, limit=None, file=None)

Print up to limit stack trace entries (starting from the invocation point) if limit is positive. Otherwise, print the last abs(limit) entries. If limit is omitted or None, all entries are printed. The optional f argument can be used to specify an alternate stack frame to start. The optional file argument has the same meaning as for print_tb().

在 3.5 版更改: Added negative limit support.

traceback.extract_tb(tb, limit=None)

Return a StackSummary object representing a list of “pre-processed” stack trace entries extracted from the traceback object tb. It is useful for alternate formatting of stack traces. The optional limit argument has the same meaning as for print_tb(). A “pre-processed” stack trace entry is a FrameSummary object containing attributes filename, lineno, name, and line representing the information that is usually printed for a stack trace. The line is a string with leading and trailing whitespace stripped; if the source is not available it is None.

traceback.extract_stack(f=None, limit=None)

Extract the raw traceback from the current stack frame. The return value has the same format as for extract_tb(). The optional f and limit arguments have the same meaning as for print_stack().

traceback.format_list(extracted_list)

Given a list of tuples or FrameSummary objects as returned by extract_tb() or extract_stack(), return a list of strings ready for printing. Each string in the resulting list corresponds to the item with the same index in the argument list. Each string ends in a newline; the strings may contain internal newlines as well, for those items whose source text line is not None.

traceback.format_exception_only(etype, value)

Format the exception part of a traceback. The arguments are the exception type and value such as given by sys.last_type and sys.last_value. The return value is a list of strings, each ending in a newline. Normally, the list contains a single string; however, for SyntaxError exceptions, it contains several lines that (when printed) display detailed information about where the syntax error occurred. The message indicating which exception occurred is the always last string in the list.

traceback.format_exception(etype, value, tb, limit=None, chain=True)

Format a stack trace and the exception information. The arguments have the same meaning as the corresponding arguments to print_exception(). The return value is a list of strings, each ending in a newline and some containing internal newlines. When these lines are concatenated and printed, exactly the same text is printed as does print_exception().

在 3.5 版更改: The etype argument is ignored and inferred from the type of value.

traceback.format_exc(limit=None, chain=True)

This is like print_exc(limit) but returns a string instead of printing to a file.

traceback.format_tb(tb, limit=None)

A shorthand for format_list(extract_tb(tb, limit)).

traceback.format_stack(f=None, limit=None)

A shorthand for format_list(extract_stack(f, limit)).

traceback.clear_frames(tb)

Clears the local variables of all the stack frames in a traceback tb by calling the clear() method of each frame object.

3.4 新版功能.

traceback.walk_stack(f)

Walk a stack following f.f_back from the given frame, yielding the frame and line number for each frame. If f is None, the current stack is used. This helper is used with StackSummary.extract().

3.5 新版功能.

traceback.walk_tb(tb)

Walk a traceback following tb_next yielding the frame and line number for each frame. This helper is used with StackSummary.extract().

3.5 新版功能.

The module also defines the following classes:

29.9.1. TracebackException Objects

3.5 新版功能.

TracebackException objects are created from actual exceptions to capture data for later printing in a lightweight fashion.

class traceback.TracebackException(exc_type, exc_value, exc_traceback, *, limit=None, lookup_lines=True, capture_locals=False)

Capture an exception for later rendering. limit, lookup_lines and capture_locals are as for the StackSummary class.

Note that when locals are captured, they are also shown in the traceback.

__cause__

A TracebackException of the original __cause__.

__context__

A TracebackException of the original __context__.

__suppress_context__

The __suppress_context__ value from the original exception.

stack

A StackSummary representing the traceback.

exc_type

The class of the original traceback.

filename

For syntax errors - the file name where the error occurred.

lineno

For syntax errors - the line number where the error occurred.

text

For syntax errors - the text where the error occurred.

offset

For syntax errors - the offset into the text where the error occurred.

msg

For syntax errors - the compiler error message.

classmethod from_exception(exc, *, limit=None, lookup_lines=True, capture_locals=False)

Capture an exception for later rendering. limit, lookup_lines and capture_locals are as for the StackSummary class.

Note that when locals are captured, they are also shown in the traceback.

format(*, chain=True)

Format the exception.

If chain is not True, __cause__ and __context__ will not be formatted.

The return value is a generator of strings, each ending in a newline and some containing internal newlines. print_exception() is a wrapper around this method which just prints the lines to a file.

The message indicating which exception occurred is always the last string in the output.

format_exception_only()

Format the exception part of the traceback.

The return value is a generator of strings, each ending in a newline.

Normally, the generator emits a single string; however, for SyntaxError exceptions, it emits several lines that (when printed) display detailed information about where the syntax error occurred.

The message indicating which exception occurred is always the last string in the output.

29.9.2. StackSummary Objects

3.5 新版功能.

StackSummary objects represent a call stack ready for formatting.

class traceback.StackSummary

classmethod extract(frame_gen, *, limit=None, lookup_lines=True, capture_locals=False)

Construct a StackSummary object from a frame generator (such as is returned by walk_stack() or walk_tb()).

If limit is supplied, only this many frames are taken from frame_gen. If lookup_lines is False, the returned FrameSummary objects will not have read their lines in yet, making the cost of creating the StackSummary cheaper (which may be valuable if it may not actually get formatted). If capture_locals is True the local variables in each FrameSummary are captured as object representations.

classmethod from_list(a_list)

Construct a StackSummary object from a supplied list of FrameSummary objects or old-style list of tuples. Each tuple should be a 4-tuple with filename, lineno, name, line as the elements.

format()

Returns a list of strings ready for printing. Each string in the resulting list corresponds to a single frame from the stack. Each string ends in a newline; the strings may contain internal newlines as well, for those items with source text lines.

For long sequences of the same frame and line, the first few repetitions are shown, followed by a summary line stating the exact number of further repetitions.

在 3.6 版更改: Long sequences of repeated frames are now abbreviated.

29.9.3. FrameSummary Objects

3.5 新版功能.

FrameSummary objects represent a single frame in a traceback.

class traceback.FrameSummary(filename, lineno, name, lookup_line=True, locals=None, line=None)

Represent a single frame in the traceback or stack that is being formatted or printed. It may optionally have a stringified version of the frames locals included in it. If lookup_line is False, the source code is not looked up until the FrameSummary has the line attribute accessed (which also happens when casting it to a tuple). line may be directly provided, and will prevent line lookups happening at all. locals is an optional local variable dictionary, and if supplied the variable representations are stored in the summary for later display.

29.9.4. Traceback Examples

This simple example implements a basic read-eval-print loop, similar to (but less useful than) the standard Python interactive interpreter loop. For a more complete implementation of the interpreter loop, refer to the code module.

import sys, traceback

def run_user_code(envdir):
    source = input(">>> ")
    try:
        exec(source, envdir)
    except Exception:
        print("Exception in user code:")
        print("-"*60)
        traceback.print_exc(file=sys.stdout)
        print("-"*60)

envdir = {}
while True:
    run_user_code(envdir)

The following example demonstrates the different ways to print and format the exception and traceback:

import sys, traceback

def lumberjack():
    bright_side_of_death()

def bright_side_of_death():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    # exc_type below is ignored on 3.5 and later
    traceback.print_exception(exc_type, exc_value, exc_traceback,
                              limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc(limit=2, file=sys.stdout)
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    # exc_type below is ignored on 3.5 and later
    print(repr(traceback.format_exception(exc_type, exc_value,
                                          exc_traceback)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)

The output for the example would look similar to this:

*** print_tb:
  File "<doctest...>", line 10, in <module>
    lumberjack()
*** print_exception:
Traceback (most recent call last):
  File "<doctest...>", line 10, in <module>
    lumberjack()
  File "<doctest...>", line 4, in lumberjack
    bright_side_of_death()
IndexError: tuple index out of range
*** print_exc:
Traceback (most recent call last):
  File "<doctest...>", line 10, in <module>
    lumberjack()
  File "<doctest...>", line 4, in lumberjack
    bright_side_of_death()
IndexError: tuple index out of range
*** format_exc, first and last line:
Traceback (most recent call last):
IndexError: tuple index out of range
*** format_exception:
['Traceback (most recent call last):\n',
 '  File "<doctest...>", line 10, in <module>\n    lumberjack()\n',
 '  File "<doctest...>", line 4, in lumberjack\n    bright_side_of_death()\n',
 '  File "<doctest...>", line 7, in bright_side_of_death\n    return tuple()[0]\n',
 'IndexError: tuple index out of range\n']
*** extract_tb:
[<FrameSummary file <doctest...>, line 10 in <module>>,
 <FrameSummary file <doctest...>, line 4 in lumberjack>,
 <FrameSummary file <doctest...>, line 7 in bright_side_of_death>]
*** format_tb:
['  File "<doctest...>", line 10, in <module>\n    lumberjack()\n',
 '  File "<doctest...>", line 4, in lumberjack\n    bright_side_of_death()\n',
 '  File "<doctest...>", line 7, in bright_side_of_death\n    return tuple()[0]\n']
*** tb_lineno: 10

The following example shows the different ways to print and format the stack:

>>> import traceback
>>> def another_function():
...     lumberstack()
...
>>> def lumberstack():
...     traceback.print_stack()
...     print(repr(traceback.extract_stack()))
...     print(repr(traceback.format_stack()))
...
>>> another_function()
  File "<doctest>", line 10, in <module>
    another_function()
  File "<doctest>", line 3, in another_function
    lumberstack()
  File "<doctest>", line 6, in lumberstack
    traceback.print_stack()
[('<doctest>', 10, '<module>', 'another_function()'),
 ('<doctest>', 3, 'another_function', 'lumberstack()'),
 ('<doctest>', 7, 'lumberstack', 'print(repr(traceback.extract_stack()))')]
['  File "<doctest>", line 10, in <module>\n    another_function()\n',
 '  File "<doctest>", line 3, in another_function\n    lumberstack()\n',
 '  File "<doctest>", line 8, in lumberstack\n    print(repr(traceback.format_stack()))\n']

This last example demonstrates the final few formatting functions:

>>> import traceback
>>> traceback.format_list([('spam.py', 3, '<module>', 'spam.eggs()'),
...                        ('eggs.py', 42, 'eggs', 'return "bacon"')])
['  File "spam.py", line 3, in <module>\n    spam.eggs()\n',
 '  File "eggs.py", line 42, in eggs\n    return "bacon"\n']
>>> an_error = IndexError('tuple index out of range')
>>> traceback.format_exception_only(type(an_error), an_error)
['IndexError: tuple index out of range\n']

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

29.10. __future__ — Future 语句定义

源代码： Lib/__future__.py [https://github.com/python/cpython/tree/3.6/Lib/__future__.py]

__future__ 是一个真正的模块，这主要有 3 个原因：

避免混淆已有的分析 import 语句并查找 import 的模块的工具。

确保 future 语句 在 2.1 之前的版本运行时至少能抛出 runtime 异常（import __future__ 会失败，因为 2.1 版本之前没有这个模块）。

当引入不兼容的修改时，可以记录其引入的时间以及强制使用的时间。这是一种可执行的文档，并且可以通过 import __future__ 来做程序性的检查。

__future__.py 中的每一条语句都是以下格式的：

FeatureName = _Feature(OptionalRelease, MandatoryRelease,
                       CompilerFlag)

通常 OptionalRelease 要比 MandatoryRelease 小，并且都是和 sys.version_info 格式一致的 5 元素元组。

(PY_MAJOR_VERSION, # the 2 in 2.1.0a3; an int
 PY_MINOR_VERSION, # the 1; an int
 PY_MICRO_VERSION, # the 0; an int
 PY_RELEASE_LEVEL, # "alpha", "beta", "candidate" or "final"; string
 PY_RELEASE_SERIAL # the 3; an int
)

OptionalRelease 记录了一个特性首次发布时的 Python 版本。

在 MandatoryRelases 还没有发布时，MandatoryRelease 表示该特性会变成语言的一部分的预测时间。

其他情况下，MandatoryRelease 用来记录这个特性是何时成为语言的一部分的。从该版本往后，使用该特性将不需要 future 语句，不过很多人还是会加上对应的 import。

MandatoryRelease 也可能是 None, 表示这个特性已经被撤销。

_Feature 类的实例有两个对应的方法，getOptionalRelease() 和 getMandatoryRelease()。

CompilerFlag 是一个（位）标记，对于动态编译的代码，需要将这个标记作为第四个参数传入内建函数 compile() 中以开启对应的特性。这个标记存储在 _Feature 类实例的 compiler_flag 属性中。

__future__ 中不会删除特性的描述。从 Python 2.1 中首次加入以来，通过这种方式引入了以下特性：

特性

可选版本

强制加入版本

效果

nested_scopes

2.1.0b1

2.2

PEP 227 [https://www.python.org/dev/peps/pep-0227]: 静态嵌套作用域

generators

2.2.0a1

2.3

PEP 255 [https://www.python.org/dev/peps/pep-0255]: 简单生成器

division

2.2.0a2

3.0

PEP 238 [https://www.python.org/dev/peps/pep-0238]: 修改除法运算符

absolute_import

2.5.0a1

3.0

PEP 328 [https://www.python.org/dev/peps/pep-0328]: 导入：多行与绝对/相对

with_statement

2.5.0a1

2.6

PEP 343 [https://www.python.org/dev/peps/pep-0343]: * “with” 语句*

print_function

2.6.0a2

3.0

PEP 3105 [https://www.python.org/dev/peps/pep-3105]: print 改为函数

unicode_literals

2.6.0a2

3.0

PEP 3112 [https://www.python.org/dev/peps/pep-3112]: Python 3000 中的字节字面值

generator_stop

3.5.0b1

3.7

PEP 479 [https://www.python.org/dev/peps/pep-0479]: 在生成器中处理 StopIteration

参见

future 语句

编译器怎样处理 future import。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

29.11. gc — 垃圾回收器接口

此模块提供可选的垃圾回收器的接口，提供的功能包括：关闭收集器、调整收集频率、设置调试选项。它同时提供对回收器找到但是无法释放的不可达对象的访问。由于 Python 使用了带有引用计数的回收器，如果你确定你的程序不会产生循环引用，你可以关闭回收器。可以通过调用 gc.disable() 关闭自动垃圾回收。若要调试一个存在内存泄漏的程序，调用 gc.set_debug(gc.DEBUG_LEAK) ；需要注意的是，它包含 gc.DEBUG_SAVEALL ，使得被垃圾回收的对象会被存放在 gc.garbage 中以待检查。

gc 模块提供下列函数：

gc.enable()

启用自动垃圾回收

gc.disable()

停用自动垃圾回收

gc.isenabled()

Returns true if automatic collection is enabled.

gc.collect(generation=2)

若被调用时不包含参数，则启动完全的垃圾回收。可选的参数 generation 可以是一个整数，指明需要回收哪一代（从 0 到 2 ）的垃圾。当参数 generation 无效时，会引发 ValueError 异常。返回发现的不可达对象的数目。

每当运行完整收集或最高代 (2) 收集时，为多个内置类型所维护的空闲列表会被清空。 由于特定类型特别是 float 的实现，在某些空闲列表中并非所有项都会被释放。

gc.set_debug(flags)

设置垃圾回收器的调试标识位。调试信息会被写入 sys.stderr 。此文档末尾列出了各个标志位及其含义；可以使用位操作对多个标志位进行设置以控制调试器。

gc.get_debug()

返回当前调试标识位。

gc.get_objects()

返回一个含有所有被收集器跟踪的对象的列表。返回的列表中不包括该函数返回的对象。

gc.get_stats()

返回一个包含三个字典对象的列表，每个字典分别包含对应代的从解释器开始运行的垃圾回收统计数据。字典的键的数目在将来可能发生改变，目前每个字典包含以下内容：

collections 是该代被回收的次数；

collected 是该代中被回收的对象总数；

uncollectable 是在这一代中被发现无法收集的对象总数 （因此被移动到 garbage 列表中）。

3.4 新版功能.

gc.set_threshold(threshold0[, threshold1[, threshold2]])

设置垃圾回收阈值（收集频率）。 将 threshold0 设为零会禁用回收。

The GC classifies objects into three generations depending on how many collection sweeps they have survived. New objects are placed in the youngest generation (generation 0). If an object survives a collection it is moved into the next older generation. Since generation 2 is the oldest generation, objects in that generation remain there after a collection. In order to decide when to run, the collector keeps track of the number object allocations and deallocations since the last collection. When the number of allocations minus the number of deallocations exceeds threshold0, collection starts. Initially only generation 0 is examined. If generation 0 has been examined more than threshold1 times since generation 1 has been examined, then generation 1 is examined as well. Similarly, threshold2 controls the number of collections of generation 1 before collecting generation 2.

gc.get_count()

将当前回收计数以形为 (count0, count1, count2) 的元组返回。

gc.get_threshold()

将当前回收阈值以形为 (threshold0, threshold1, threshold2) 的元组返回。

gc.get_referrers(*objs)

返回直接引用任意一个 ojbs 的对象列表。这个函数只定位支持垃圾回收的容器；引用了其它对象但不支持垃圾回收的扩展类型不会被找到。

需要注意的是，已经解除对 objs 引用的对象，但仍存在于循环引用中未被回收时，仍然会被作为引用者出现在返回的列表当中。若要获取当前正在引用 objs 的对象，需要调用 collect() 然后再调用 get_referrers() 。

在使用 get_referrers() 返回的对象时必须要小心，因为其中一些对象可能仍在构造中因此处于暂时的无效状态。不要把 get_referrers() 用于调试以外的其它目的。

gc.get_referents(*objs)

返回被任意一个参数中的对象直接引用的对象的列表。返回的被引用对象是被参数中的对象的C语言级别方法（若存在） tp_traverse 访问到的对象，可能不是所有的实际直接可达对象。只有支持垃圾回收的对象支持 tp_traverse  方法，并且此方法只会在需要访问涉及循环引用的对象时使用。因此，可以有以下例子：一个整数对其中一个参数是直接可达的，这个整数有可能出现或不出现在返回的结果列表当中。

gc.is_tracked(obj)

当对象正在被垃圾回收器监控时返回 True ，否则返回 False 。一般来说，原子类的实例不会被监控，而非原子类（如容器、用户自定义的对象）会被监控。然而，会有一些特定类型的优化以便减少垃圾回收器在简单实例（如只含有原子性的键和值的字典）上的消耗。

>>> gc.is_tracked(0)
False
>>> gc.is_tracked("a")
False
>>> gc.is_tracked([])
True
>>> gc.is_tracked({})
False
>>> gc.is_tracked({"a": 1})
False
>>> gc.is_tracked({"a": []})
True

3.1 新版功能.

提供以下变量仅供只读访问（你可以修改但不应该重绑定它们）：

gc.garbage

A list of objects which the collector found to be unreachable but could not be freed (uncollectable objects). Starting with Python 3.4, this list should be empty most of the time, except when using instances of C extension types with a non-NULL tp_del slot.

如果设置了 DEBUG_SAVEALL ，则所有不可访问对象将被添加至该列表而不会被释放。

在 3.2 版更改: 当 interpreter shutdown 即解释器关闭时，若此列表非空，会产生 ResourceWarning ，即资源警告，在默认情况下此警告不会被提醒。如果设置了 DEBUG_UNCOLLECTABLE ，所有无法被回收的对象会被打印。

在 3.4 版更改: 根据 PEP 442 [https://www.python.org/dev/peps/pep-0442] ，带有 __del__() 方法的对象最终不再会进入 gc.garbage 。

gc.callbacks

在垃圾回收器开始前和完成后会被调用的一系列回调函数。这些回调函数在被调用时使用两个参数： phase 和 info 。

phase 可为以下两值之一：

“start”: 垃圾回收即将开始。

“stop”: 垃圾回收已结束。

info is a dict providing more information for the callback. The following keys are currently defined:

“generation”（代） ：正在被回收的最久远的一代。

“collected”（已回收的 ）: 当*phase* 为 “stop” 时，被成功回收的对象的数目。

“uncollectable”（不可回收的）: 当 phase 为 “stop” 时，不能被回收并被放入 garbage 的对象的数目。

应用程序可以把他们自己的回调函数加入此列表。主要的使用场景有：

统计垃圾回收的数据，如：不同代的回收频率、回收所花费的时间。

使应用程序可以识别和清理他们自己的在 garbage 中的不可回收类型的对象。

3.3 新版功能.

以下常量被用于 set_debug() ：

gc.DEBUG_STATS

在回收完成后打印统计信息。当回收频率设置较高时，这些信息会比较有用。

gc.DEBUG_COLLECTABLE

当发现可回收对象时打印信息。

gc.DEBUG_UNCOLLECTABLE

打印找到的不可回收对象的信息（指不能被回收器回收的不可达对象）。这些对象会被添加到 garbage 列表中。

在 3.2 版更改: 当 interpreter shutdown 时，即解释器关闭时，若 garbage 列表中存在对象，这些对象也会被打印输出。

gc.DEBUG_SAVEALL

设置后，所有回收器找到的不可达对象会被添加进 garbage 而不是直接被释放。这在调试一个内存泄漏的程序时会很有用。

gc.DEBUG_LEAK

调试内存泄漏的程序时，使回收器打印信息的调试标识位。（等价于 DEBUG_COLLECTABLE | DEBUG_UNCOLLECTABLE | DEBUG_SAVEALL ）。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

29.12. inspect — 检查对象

源代码: Lib/inspect.py [https://github.com/python/cpython/tree/3.6/Lib/inspect.py]

inspect 模块提供了一些有用的函数帮助获取对象的信息，例如模块、类、方法、函数、回溯、帧对象以及代码对象。例如它可以帮助你检查类的内容，获取某个方法的源代码，取得并格式化某个函数的参数列表，或者获取你需要显示的回溯的详细信息。

该模块提供了4种主要的功能：类型检查、获取源代码、检查类与函数、检查解释器的调用堆栈。

29.12.1. 类型和成员

getmembers() 函数获取对象的成员，例如类或模块。函数名以”is”开始的函数主要作为 getmembers() 的第2个参数使用。它们也可用于判定某对象是否有如下的特殊属性:

类型

属性

描述

module 模块

__doc__

文档字符串

__file__

文件名(内置模块没有文件名)

class – 类

__doc__

文档字符串

__name__

类定义时所使用的名称

__qualname__

qualified name – 限定名称

__module__

该类型被定义时所在的模块的名称

method 方法

__doc__

文档字符串

__name__

该方法定义时所使用的名称

__qualname__

qualified name – 限定名称

__func__

实现该方法的函数对象

__self__

该方法被绑定的实例，若没有绑定则为 None

function – 函数

__doc__

文档字符串

__name__

用于定义此函数的名称

__qualname__

qualified name – 限定名称

__code__

包含已编译函数的代码对象 bytecode

__defaults__

tuple of any default values for positional or keyword parameters

__kwdefaults__

mapping of any default values for keyword-only parameters

__globals__

global namespace in which this function was defined

__annotations__

mapping of parameters names to annotations; "return" key is reserved for return annotations.

回溯

tb_frame

此级别的框架对象

tb_lasti

index of last attempted instruction in bytecode

tb_lineno

current line number in Python source code

tb_next

next inner traceback object (called by this level)

框架

f_back

next outer frame object (this frame’s caller)

f_builtins

builtins namespace seen by this frame

f_code

code object being executed in this frame

f_globals

global namespace seen by this frame

f_lasti

index of last attempted instruction in bytecode

f_lineno

current line number in Python source code

f_locals

local namespace seen by this frame

f_trace

tracing function for this frame, or None

code

co_argcount

number of arguments (not including keyword only arguments, * or ** args)

co_code

原始编译字节码的字符串

co_cellvars

单元变量名称的元组(通过包含作用域引用)

co_consts

字节码中使用的常量元组

co_filename

创建此代码对象的文件的名称

co_firstlineno

number of first line in Python source code

co_flags

bitmap of CO_* flags, read more here

co_lnotab

编码的行号到字节码索引的映射

co_freevars

tuple of names of free variables (referenced via a function’s closure)

co_kwonlyargcount

number of keyword only arguments (not including ** arg)

co_name

定义此代码对象的名称

co_names

局部变量名称的元组

co_nlocals

局部变量的数量

co_stacksize

需要虚拟机堆栈空间

co_varnames

参数名和局部变量的元组

generator – 生成器

__name__

名称

__qualname__

qualified name – 限定名称

gi_frame

框架

gi_running

生成器在运行吗？

gi_code

code

gi_yieldfrom

object being iterated by yield from, or None

coroutine – 协程

__name__

名称

__qualname__

qualified name – 限定名称

cr_await

object being awaited on, or None

cr_frame

框架

cr_running

is the coroutine running?

cr_code

code

builtin

__doc__

文档字符串

__name__

此函数或方法的原始名称

__qualname__

qualified name – 限定名称

__self__

instance to which a method is bound, or None

在 3.5 版更改: Add __qualname__ and gi_yieldfrom attributes to generators.

The __name__ attribute of generators is now set from the function name, instead of the code name, and it can now be modified.

inspect.getmembers(object[, predicate])

Return all the members of an object in a list of (name, value) pairs sorted by name. If the optional predicate argument is supplied, only members for which the predicate returns a true value are included.

注解

getmembers() will only return class attributes defined in the metaclass when the argument is a class and those attributes have been listed in the metaclass’ custom __dir__().

inspect.getmodulename(path)

Return the name of the module named by the file path, without including the names of enclosing packages. The file extension is checked against all of the entries in importlib.machinery.all_suffixes(). If it matches, the final path component is returned with the extension removed. Otherwise, None is returned.

Note that this function only returns a meaningful name for actual Python modules - paths that potentially refer to Python packages will still return None.

在 3.3 版更改: The function is based directly on importlib.

inspect.ismodule(object)

Return true if the object is a module.

inspect.isclass(object)

Return true if the object is a class, whether built-in or created in Python code.

inspect.ismethod(object)

Return true if the object is a bound method written in Python.

inspect.isfunction(object)

Return true if the object is a Python function, which includes functions created by a lambda expression.

inspect.isgeneratorfunction(object)

Return true if the object is a Python generator function.

inspect.isgenerator(object)

Return true if the object is a generator.

inspect.iscoroutinefunction(object)

Return true if the object is a coroutine function (a function defined with an async def syntax).

3.5 新版功能.

inspect.iscoroutine(object)

Return true if the object is a coroutine created by an async def function.

3.5 新版功能.

inspect.isawaitable(object)

Return true if the object can be used in await expression.

Can also be used to distinguish generator-based coroutines from regular generators:

def gen():
    yield
@types.coroutine
def gen_coro():
    yield

assert not isawaitable(gen())
assert isawaitable(gen_coro())

3.5 新版功能.

inspect.isasyncgenfunction(object)

Return true if the object is an asynchronous generator function, for example:

>>> async def agen():
...     yield 1
...
>>> inspect.isasyncgenfunction(agen)
True

3.6 新版功能.

inspect.isasyncgen(object)

Return true if the object is an asynchronous generator iterator created by an asynchronous generator function.

3.6 新版功能.

inspect.istraceback(object)

Return true if the object is a traceback.

inspect.isframe(object)

Return true if the object is a frame.

inspect.iscode(object)

Return true if the object is a code.

inspect.isbuiltin(object)

Return true if the object is a built-in function or a bound built-in method.

inspect.isroutine(object)

Return true if the object is a user-defined or built-in function or method.

inspect.isabstract(object)

Return true if the object is an abstract base class.

inspect.ismethoddescriptor(object)

Return true if the object is a method descriptor, but not if ismethod(), isclass(), isfunction() or isbuiltin() are true.

This, for example, is true of int.__add__. An object passing this test has a __get__() method but not a __set__() method, but beyond that the set of attributes varies. A __name__ attribute is usually sensible, and __doc__ often is.

Methods implemented via descriptors that also pass one of the other tests return false from the ismethoddescriptor() test, simply because the other tests promise more – you can, e.g., count on having the __func__ attribute (etc) when an object passes ismethod().

inspect.isdatadescriptor(object)

Return true if the object is a data descriptor.

Data descriptors have both a __get__ and a __set__ method. Examples are properties (defined in Python), getsets, and members. The latter two are defined in C and there are more specific tests available for those types, which is robust across Python implementations. Typically, data descriptors will also have __name__ and __doc__ attributes (properties, getsets, and members have both of these attributes), but this is not guaranteed.

inspect.isgetsetdescriptor(object)

Return true if the object is a getset descriptor.

CPython implementation detail: getsets are attributes defined in extension modules via PyGetSetDef structures. For Python implementations without such types, this method will always return False.

inspect.ismemberdescriptor(object)

Return true if the object is a member descriptor.

CPython implementation detail: Member descriptors are attributes defined in extension modules via PyMemberDef structures. For Python implementations without such types, this method will always return False.

29.12.2. Retrieving source code

inspect.getdoc(object)

Get the documentation string for an object, cleaned up with cleandoc(). If the documentation string for an object is not provided and the object is a class, a method, a property or a descriptor, retrieve the documentation string from the inheritance hierarchy.

在 3.5 版更改: Documentation strings are now inherited if not overridden.

inspect.getcomments(object)

Return in a single string any lines of comments immediately preceding the object’s source code (for a class, function, or method), or at the top of the Python source file (if the object is a module). If the object’s source code is unavailable, return None. This could happen if the object has been defined in C or the interactive shell.

inspect.getfile(object)

Return the name of the (text or binary) file in which an object was defined. This will fail with a TypeError if the object is a built-in module, class, or function.

inspect.getmodule(object)

Try to guess which module an object was defined in.

inspect.getsourcefile(object)

Return the name of the Python source file in which an object was defined. This will fail with a TypeError if the object is a built-in module, class, or function.

inspect.getsourcelines(object)

Return a list of source lines and starting line number for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a list of the lines corresponding to the object and the line number indicates where in the original source file the first line of code was found. An OSError is raised if the source code cannot be retrieved.

在 3.3 版更改: OSError is raised instead of IOError, now an alias of the former.

inspect.getsource(object)

Return the text of the source code for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a single string. An OSError is raised if the source code cannot be retrieved.

在 3.3 版更改: OSError is raised instead of IOError, now an alias of the former.

inspect.cleandoc(doc)

Clean up indentation from docstrings that are indented to line up with blocks of code.

All leading whitespace is removed from the first line. Any leading whitespace that can be uniformly removed from the second line onwards is removed. Empty lines at the beginning and end are subsequently removed. Also, all tabs are expanded to spaces.

29.12.3. Introspecting callables with the Signature object

3.3 新版功能.

The Signature object represents the call signature of a callable object and its return annotation. To retrieve a Signature object, use the signature() function.

inspect.signature(callable, *, follow_wrapped=True)

Return a Signature object for the given callable:

>>> from inspect import signature
>>> def foo(a, *, b:int, **kwargs):
...     pass

>>> sig = signature(foo)

>>> str(sig)
'(a, *, b:int, **kwargs)'

>>> str(sig.parameters['b'])
'b:int'

>>> sig.parameters['b'].annotation
<class 'int'>

Accepts a wide range of python callables, from plain functions and classes to functools.partial() objects.

Raises ValueError if no signature can be provided, and TypeError if that type of object is not supported.

3.5 新版功能: follow_wrapped parameter. Pass False to get a signature of callable specifically (callable.__wrapped__ will not be used to unwrap decorated callables.)

注解

Some callables may not be introspectable in certain implementations of Python. For example, in CPython, some built-in functions defined in C provide no metadata about their arguments.

class inspect.Signature(parameters=None, *, return_annotation=Signature.empty)

A Signature object represents the call signature of a function and its return annotation. For each parameter accepted by the function it stores a Parameter object in its parameters collection.

The optional parameters argument is a sequence of Parameter objects, which is validated to check that there are no parameters with duplicate names, and that the parameters are in the right order, i.e. positional-only first, then positional-or-keyword, and that parameters with defaults follow parameters without defaults.

The optional return_annotation argument, can be an arbitrary Python object, is the “return” annotation of the callable.

Signature objects are immutable. Use Signature.replace() to make a modified copy.

在 3.5 版更改: Signature objects are picklable and hashable.

empty

A special class-level marker to specify absence of a return annotation.

parameters

An ordered mapping of parameters’ names to the corresponding Parameter objects.

return_annotation

The “return” annotation for the callable. If the callable has no “return” annotation, this attribute is set to Signature.empty.

bind(*args, **kwargs)

Create a mapping from positional and keyword arguments to parameters. Returns BoundArguments if *args and **kwargs match the signature, or raises a TypeError.

bind_partial(*args, **kwargs)

Works the same way as Signature.bind(), but allows the omission of some required arguments (mimics functools.partial() behavior.) Returns BoundArguments, or raises a TypeError if the passed arguments do not match the signature.

replace(*[, parameters][, return_annotation])

Create a new Signature instance based on the instance replace was invoked on. It is possible to pass different parameters and/or return_annotation to override the corresponding properties of the base signature. To remove return_annotation from the copied Signature, pass in Signature.empty.

>>> def test(a, b):
...     pass
>>> sig = signature(test)
>>> new_sig = sig.replace(return_annotation="new return anno")
>>> str(new_sig)
"(a, b) -> 'new return anno'"

classmethod from_callable(obj, *, follow_wrapped=True)

Return a Signature (or its subclass) object for a given callable obj. Pass follow_wrapped=False to get a signature of obj without unwrapping its __wrapped__ chain.

This method simplifies subclassing of Signature:

class MySignature(Signature):
    pass
sig = MySignature.from_callable(min)
assert isinstance(sig, MySignature)

3.5 新版功能.

class inspect.Parameter(name, kind, *, default=Parameter.empty, annotation=Parameter.empty)

Parameter objects are immutable. Instead of modifying a Parameter object, you can use Parameter.replace() to create a modified copy.

在 3.5 版更改: Parameter objects are picklable and hashable.

empty

A special class-level marker to specify absence of default values and annotations.

name

The name of the parameter as a string. The name must be a valid Python identifier.

CPython implementation detail: CPython generates implicit parameter names of the form .0 on the code objects used to implement comprehensions and generator expressions.

在 3.6 版更改: These parameter names are exposed by this module as names like implicit0.

default

The default value for the parameter. If the parameter has no default value, this attribute is set to Parameter.empty.

annotation

The annotation for the parameter. If the parameter has no annotation, this attribute is set to Parameter.empty.

kind

Describes how argument values are bound to the parameter. Possible values (accessible via Parameter, like Parameter.KEYWORD_ONLY):

名称

含义

POSITIONAL_ONLY

Value must be supplied as a positional argument.

Python has no explicit syntax for defining positional-only parameters, but many built-in and extension module functions (especially those that accept only one or two parameters) accept them.

POSITIONAL_OR_KEYWORD

Value may be supplied as either a keyword or positional argument (this is the standard binding behaviour for functions implemented in Python.)

VAR_POSITIONAL

A tuple of positional arguments that aren’t bound to any other parameter. This corresponds to a *args parameter in a Python function definition.

KEYWORD_ONLY

Value must be supplied as a keyword argument. Keyword only parameters are those which appear after a * or *args entry in a Python function definition.

VAR_KEYWORD

A dict of keyword arguments that aren’t bound to any other parameter. This corresponds to a **kwargs parameter in a Python function definition.

Example: print all keyword-only arguments without default values:

>>> def foo(a, b, *, c, d=10):
...     pass

>>> sig = signature(foo)
>>> for param in sig.parameters.values():
...     if (param.kind == param.KEYWORD_ONLY and
...                        param.default is param.empty):
...         print('Parameter:', param)
Parameter: c

replace(*[, name][, kind][, default][, annotation])

Create a new Parameter instance based on the instance replaced was invoked on. To override a Parameter attribute, pass the corresponding argument. To remove a default value or/and an annotation from a Parameter, pass Parameter.empty.

>>> from inspect import Parameter
>>> param = Parameter('foo', Parameter.KEYWORD_ONLY, default=42)
>>> str(param)
'foo=42'

>>> str(param.replace()) # Will create a shallow copy of 'param'
'foo=42'

>>> str(param.replace(default=Parameter.empty, annotation='spam'))
"foo:'spam'"

在 3.4 版更改: In Python 3.3 Parameter objects were allowed to have name set to None if their kind was set to POSITIONAL_ONLY. This is no longer permitted.

class inspect.BoundArguments

Result of a Signature.bind() or Signature.bind_partial() call. Holds the mapping of arguments to the function’s parameters.

arguments

An ordered, mutable mapping (collections.OrderedDict) of parameters’ names to arguments’ values. Contains only explicitly bound arguments. Changes in arguments will reflect in args and kwargs.

Should be used in conjunction with Signature.parameters for any argument processing purposes.

注解

Arguments for which Signature.bind() or Signature.bind_partial() relied on a default value are skipped. However, if needed, use BoundArguments.apply_defaults() to add them.

args

A tuple of positional arguments values. Dynamically computed from the arguments attribute.

kwargs

A dict of keyword arguments values. Dynamically computed from the arguments attribute.

signature

A reference to the parent Signature object.

apply_defaults()

Set default values for missing arguments.

For variable-positional arguments (*args) the default is an empty tuple.

For variable-keyword arguments (**kwargs) the default is an empty dict.

>>> def foo(a, b='ham', *args): pass
>>> ba = inspect.signature(foo).bind('spam')
>>> ba.apply_defaults()
>>> ba.arguments
OrderedDict([('a', 'spam'), ('b', 'ham'), ('args', ())])

3.5 新版功能.

The args and kwargs properties can be used to invoke functions:

def test(a, *, b):
    ...

sig = signature(test)
ba = sig.bind(10, b=20)
test(*ba.args, **ba.kwargs)

参见

PEP 362 [https://www.python.org/dev/peps/pep-0362] - Function Signature Object.

The detailed specification, implementation details and examples.

29.12.4. 类与函数

inspect.getclasstree(classes, unique=False)

Arrange the given list of classes into a hierarchy of nested lists. Where a nested list appears, it contains classes derived from the class whose entry immediately precedes the list. Each entry is a 2-tuple containing a class and a tuple of its base classes. If the unique argument is true, exactly one entry appears in the returned structure for each class in the given list. Otherwise, classes using multiple inheritance and their descendants will appear multiple times.

inspect.getargspec(func)

Get the names and default values of a Python function’s parameters. A named tuple ArgSpec(args, varargs, keywords, defaults) is returned. args is a list of the parameter names. varargs and keywords are the names of the * and ** parameters or None. defaults is a tuple of default argument values or None if there are no default arguments; if this tuple has n elements, they correspond to the last n elements listed in args.

3.0 版后已移除: Use getfullargspec() for an updated API that is usually a drop-in replacement, but also correctly handles function annotations and keyword-only parameters.

Alternatively, use signature() and Signature Object, which provide a more structured introspection API for callables.

inspect.getfullargspec(func)

Get the names and default values of a Python function’s parameters. A named tuple is returned:

FullArgSpec(args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations)

args is a list of the positional parameter names. varargs is the name of the * parameter or None if arbitrary positional arguments are not accepted. varkw is the name of the ** parameter or None if arbitrary keyword arguments are not accepted. defaults is an n-tuple of default argument values corresponding to the last n positional parameters, or None if there are no such defaults defined. kwonlyargs is a list of keyword-only parameter names. kwonlydefaults is a dictionary mapping parameter names from kwonlyargs to the default values used if no argument is supplied. annotations is a dictionary mapping parameter names to annotations. The special key "return" is used to report the function return value annotation (if any).

Note that signature() and Signature Object provide the recommended API for callable introspection, and support additional behaviours (like positional-only arguments) that are sometimes encountered in extension module APIs. This function is retained primarily for use in code that needs to maintain compatibility with the Python 2 inspect module API.

在 3.4 版更改: This function is now based on signature(), but still ignores __wrapped__ attributes and includes the already bound first parameter in the signature output for bound methods.

在 3.6 版更改: This method was previously documented as deprecated in favour of signature() in Python 3.5, but that decision has been reversed in order to restore a clearly supported standard interface for single-source Python 2/3 code migrating away from the legacy getargspec() API.

inspect.getargvalues(frame)

Get information about arguments passed into a particular frame. A named tuple ArgInfo(args, varargs, keywords, locals) is returned. args is a list of the argument names. varargs and keywords are the names of the * and ** arguments or None. locals is the locals dictionary of the given frame.

注解

This function was inadvertently marked as deprecated in Python 3.5.

inspect.formatargspec(args[, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations[, formatarg, formatvarargs, formatvarkw, formatvalue, formatreturns, formatannotations]])

Format a pretty argument spec from the values returned by getfullargspec().

The first seven arguments are (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations).

The other six arguments are functions that are called to turn argument names, * argument name, ** argument name, default values, return annotation and individual annotations into strings, respectively.

例如:

>>> from inspect import formatargspec, getfullargspec
>>> def f(a: int, b: float):
...     pass
...
>>> formatargspec(*getfullargspec(f))
'(a: int, b: float)'

3.5 版后已移除: Use signature() and Signature Object, which provide a better introspecting API for callables.

inspect.formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue])

Format a pretty argument spec from the four values returned by getargvalues(). The format* arguments are the corresponding optional formatting functions that are called to turn names and values into strings.

注解

This function was inadvertently marked as deprecated in Python 3.5.

inspect.getmro(cls)

Return a tuple of class cls’s base classes, including cls, in method resolution order. No class appears more than once in this tuple. Note that the method resolution order depends on cls’s type. Unless a very peculiar user-defined metatype is in use, cls will be the first element of the tuple.

inspect.getcallargs(func, *args, **kwds)

Bind the args and kwds to the argument names of the Python function or method func, as if it was called with them. For bound methods, bind also the first argument (typically named self) to the associated instance. A dict is returned, mapping the argument names (including the names of the * and ** arguments, if any) to their values from args and kwds. In case of invoking func incorrectly, i.e. whenever func(*args, **kwds) would raise an exception because of incompatible signature, an exception of the same type and the same or similar message is raised. For example:

>>> from inspect import getcallargs
>>> def f(a, b=1, *pos, **named):
...     pass
>>> getcallargs(f, 1, 2, 3) == {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)}
True
>>> getcallargs(f, a=2, x=4) == {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()}
True
>>> getcallargs(f)
Traceback (most recent call last):
...
TypeError: f() missing 1 required positional argument: 'a'

3.2 新版功能.

3.5 版后已移除: Use Signature.bind() and Signature.bind_partial() instead.

inspect.getclosurevars(func)

Get the mapping of external name references in a Python function or method func to their current values. A named tuple ClosureVars(nonlocals, globals, builtins, unbound) is returned. nonlocals maps referenced names to lexical closure variables, globals to the function’s module globals and builtins to the builtins visible from the function body. unbound is the set of names referenced in the function that could not be resolved at all given the current module globals and builtins.

TypeError is raised if func is not a Python function or method.

3.3 新版功能.

inspect.unwrap(func, *, stop=None)

Get the object wrapped by func. It follows the chain of __wrapped__ attributes returning the last object in the chain.

stop is an optional callback accepting an object in the wrapper chain as its sole argument that allows the unwrapping to be terminated early if the callback returns a true value. If the callback never returns a true value, the last object in the chain is returned as usual. For example, signature() uses this to stop unwrapping if any object in the chain has a __signature__ attribute defined.

ValueError is raised if a cycle is encountered.

3.4 新版功能.

29.12.5. The interpreter stack

When the following functions return “frame records,” each record is a named tuple FrameInfo(frame, filename, lineno, function, code_context, index). The tuple contains the frame object, the filename, the line number of the current line, the function name, a list of lines of context from the source code, and the index of the current line within that list.

在 3.5 版更改: Return a named tuple instead of a tuple.

注解

Keeping references to frame objects, as found in the first element of the frame records these functions return, can cause your program to create reference cycles. Once a reference cycle has been created, the lifespan of all objects which can be accessed from the objects which form the cycle can become much longer even if Python’s optional cycle detector is enabled. If such cycles must be created, it is important to ensure they are explicitly broken to avoid the delayed destruction of objects and increased memory consumption which occurs.

Though the cycle detector will catch these, destruction of the frames (and local variables) can be made deterministic by removing the cycle in a finally clause. This is also important if the cycle detector was disabled when Python was compiled or using gc.disable(). For example:

def handle_stackframe_without_leak():
    frame = inspect.currentframe()
    try:
        # do something with the frame
    finally:
        del frame

If you want to keep the frame around (for example to print a traceback later), you can also break reference cycles by using the frame.clear() method.

The optional context argument supported by most of these functions specifies the number of lines of context to return, which are centered around the current line.

inspect.getframeinfo(frame, context=1)

Get information about a frame or traceback object. A named tuple Traceback(filename, lineno, function, code_context, index) is returned.

inspect.getouterframes(frame, context=1)

Get a list of frame records for a frame and all outer frames. These frames represent the calls that lead to the creation of frame. The first entry in the returned list represents frame; the last entry represents the outermost call on frame’s stack.

在 3.5 版更改: A list of named tuples FrameInfo(frame, filename, lineno, function, code_context, index) is returned.

inspect.getinnerframes(traceback, context=1)

Get a list of frame records for a traceback’s frame and all inner frames. These frames represent calls made as a consequence of frame. The first entry in the list represents traceback; the last entry represents where the exception was raised.

在 3.5 版更改: A list of named tuples FrameInfo(frame, filename, lineno, function, code_context, index) is returned.

inspect.currentframe()

Return the frame object for the caller’s stack frame.

CPython implementation detail: This function relies on Python stack frame support in the interpreter, which isn’t guaranteed to exist in all implementations of Python. If running in an implementation without Python stack frame support this function returns None.

inspect.stack(context=1)

Return a list of frame records for the caller’s stack. The first entry in the returned list represents the caller; the last entry represents the outermost call on the stack.

在 3.5 版更改: A list of named tuples FrameInfo(frame, filename, lineno, function, code_context, index) is returned.

inspect.trace(context=1)

Return a list of frame records for the stack between the current frame and the frame in which an exception currently being handled was raised in. The first entry in the list represents the caller; the last entry represents where the exception was raised.

在 3.5 版更改: A list of named tuples FrameInfo(frame, filename, lineno, function, code_context, index) is returned.

29.12.6. Fetching attributes statically

Both getattr() and hasattr() can trigger code execution when fetching or checking for the existence of attributes. Descriptors, like properties, will be invoked and __getattr__() and __getattribute__() may be called.

For cases where you want passive introspection, like documentation tools, this can be inconvenient. getattr_static() has the same signature as getattr() but avoids executing code when it fetches attributes.

inspect.getattr_static(obj, attr, default=None)

Retrieve attributes without triggering dynamic lookup via the descriptor protocol, __getattr__() or __getattribute__().

Note: this function may not be able to retrieve all attributes that getattr can fetch (like dynamically created attributes) and may find attributes that getattr can’t (like descriptors that raise AttributeError). It can also return descriptors objects instead of instance members.

If the instance __dict__ is shadowed by another member (for example a property) then this function will be unable to find instance members.

3.2 新版功能.

getattr_static() does not resolve descriptors, for example slot descriptors or getset descriptors on objects implemented in C. The descriptor object is returned instead of the underlying attribute.

You can handle these with code like the following. Note that for arbitrary getset descriptors invoking these may trigger code execution:

# example code for resolving the builtin descriptor types
class _foo:
    __slots__ = ['foo']

slot_descriptor = type(_foo.foo)
getset_descriptor = type(type(open(__file__)).name)
wrapper_descriptor = type(str.__dict__['__add__'])
descriptor_types = (slot_descriptor, getset_descriptor, wrapper_descriptor)

result = getattr_static(some_object, 'foo')
if type(result) in descriptor_types:
    try:
        result = result.__get__()
    except AttributeError:
        # descriptors can raise AttributeError to
        # indicate there is no underlying value
        # in which case the descriptor itself will
        # have to do
        pass

29.12.7. Current State of Generators and Coroutines

When implementing coroutine schedulers and for other advanced uses of generators, it is useful to determine whether a generator is currently executing, is waiting to start or resume or execution, or has already terminated. getgeneratorstate() allows the current state of a generator to be determined easily.

inspect.getgeneratorstate(generator)

Get current state of a generator-iterator.

Possible states are:

GEN_CREATED: Waiting to start execution.

GEN_RUNNING: Currently being executed by the interpreter.

GEN_SUSPENDED: Currently suspended at a yield expression.

GEN_CLOSED: Execution has completed.

3.2 新版功能.

inspect.getcoroutinestate(coroutine)

Get current state of a coroutine object. The function is intended to be used with coroutine objects created by async def functions, but will accept any coroutine-like object that has cr_running and cr_frame attributes.

Possible states are:

CORO_CREATED: Waiting to start execution.

CORO_RUNNING: Currently being executed by the interpreter.

CORO_SUSPENDED: Currently suspended at an await expression.

CORO_CLOSED: Execution has completed.

3.5 新版功能.

The current internal state of the generator can also be queried. This is mostly useful for testing purposes, to ensure that internal state is being updated as expected:

inspect.getgeneratorlocals(generator)

Get the mapping of live local variables in generator to their current values. A dictionary is returned that maps from variable names to values. This is the equivalent of calling locals() in the body of the generator, and all the same caveats apply.

If generator is a generator with no currently associated frame, then an empty dictionary is returned. TypeError is raised if generator is not a Python generator object.

CPython implementation detail: This function relies on the generator exposing a Python stack frame for introspection, which isn’t guaranteed to be the case in all implementations of Python. In such cases, this function will always return an empty dictionary.

3.3 新版功能.

inspect.getcoroutinelocals(coroutine)

This function is analogous to getgeneratorlocals(), but works for coroutine objects created by async def functions.

3.5 新版功能.

29.12.8. Code Objects Bit Flags

Python code objects have a co_flags attribute, which is a bitmap of the following flags:

inspect.CO_OPTIMIZED

The code object is optimized, using fast locals.

inspect.CO_NEWLOCALS

If set, a new dict will be created for the frame’s f_locals when the code object is executed.

inspect.CO_VARARGS

The code object has a variable positional parameter (*args-like).

inspect.CO_VARKEYWORDS

The code object has a variable keyword parameter (**kwargs-like).

inspect.CO_NESTED

The flag is set when the code object is a nested function.

inspect.CO_GENERATOR

The flag is set when the code object is a generator function, i.e. a generator object is returned when the code object is executed.

inspect.CO_NOFREE

The flag is set if there are no free or cell variables.

inspect.CO_COROUTINE

The flag is set when the code object is a coroutine function. When the code object is executed it returns a coroutine object. See PEP 492 [https://www.python.org/dev/peps/pep-0492] for more details.

3.5 新版功能.

inspect.CO_ITERABLE_COROUTINE

The flag is used to transform generators into generator-based coroutines. Generator objects with this flag can be used in await expression, and can yield from coroutine objects. See PEP 492 [https://www.python.org/dev/peps/pep-0492] for more details.

3.5 新版功能.

inspect.CO_ASYNC_GENERATOR

The flag is set when the code object is an asynchronous generator function. When the code object is executed it returns an asynchronous generator object. See PEP 525 [https://www.python.org/dev/peps/pep-0525] for more details.

3.6 新版功能.

注解

The flags are specific to CPython, and may not be defined in other Python implementations. Furthermore, the flags are an implementation detail, and can be removed or deprecated in future Python releases. It’s recommended to use public APIs from the inspect module for any introspection needs.

29.12.9. 命令行界面

The inspect module also provides a basic introspection capability from the command line.

By default, accepts the name of a module and prints the source of that module. A class or function within the module can be printed instead by appended a colon and the qualified name of the target object.

--details

Print information about the specified object rather than the source code

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

29.13. site —— 指定 Site 的配置钩子

源代码: Lib/site.py [https://github.com/python/cpython/tree/3.6/Lib/site.py]

这个模块将在初始化时被自动导入。 此自动导入可以通过使用解释器的 -S 选项来屏蔽。

导入此模块将会附加域特定的路径到模块搜索路径并且添加一些内建对象，除非使用了 -S 选项。在此例中，模块可以被安全地导入，而不会对模块搜索路径和内建对象有自动的修改或添加。要明确地触发通常域特定的添加，调用函数 site.main()。

在 3.3 版更改: 即便使用了 -S，也会导入用于触发路径操纵的模块。

It starts by constructing up to four directories from a head and a tail part. For the head part, it uses sys.prefix and sys.exec_prefix; empty heads are skipped. For the tail part, it uses the empty string and then lib/site-packages (on Windows) or lib/pythonX.Y/site-packages (on Unix and Macintosh). For each of the distinct head-tail combinations, it sees if it refers to an existing directory, and if so, adds it to sys.path and also inspects the newly added path for configuration files.

在 3.5 版更改: 对 “site-python” 目录的支持已被移除。

If a file named “pyvenv.cfg” exists one directory above sys.executable, sys.prefix and sys.exec_prefix are set to that directory and it is also checked for site-packages (sys.base_prefix and sys.base_exec_prefix will always be the “real” prefixes of the Python installation). If “pyvenv.cfg” (a bootstrap configuration file) contains the key “include-system-site-packages” set to anything other than “false” (case-insensitive), the system-level prefixes will still also be searched for site-packages; otherwise they won’t.

一个路径配置文件是具有 name.pth 命名格式的文件，并且存在上面提到的四个目录之一中；它的内容是要添加到 sys.path 中的额外项目（每行一个）。不存在的项目不会添加到 sys.path，并且不会检查项目指向的是目录还是文件。项目不会被添加到 sys.path 超过一次。空行和由 # 起始的行会被跳过。以 import 开始的行（跟着空格或 TAB）会被执行。

例如，假设 sys.prefix 和 sys.exec_prefix 已经被设置为 /usr/local。 Python X.Y 的库之后被安装为 /usr/local/lib/pythonX.Y。假设有一个拥有三个孙目录 foo, bar 和 spam 的子目录 /usr/local/lib/pythonX.Y/site-packages，并且有两个路径配置文件 foot.pth 和 bar.pth。假定 foo.pth 内容如下:

# foo package configuration

foo
bar
bletch

并且 bar.pth 包含:

# bar package configuration

bar

则下面特定版目录将以如下顺序被添加到 sys.path。

/usr/local/lib/pythonX.Y/site-packages/bar
/usr/local/lib/pythonX.Y/site-packages/foo

Note that bletch is omitted because it doesn’t exist; the bar directory precedes the foo directory because bar.pth comes alphabetically before foo.pth; and spam is omitted because it is not mentioned in either path configuration file.

After these path manipulations, an attempt is made to import a module named sitecustomize, which can perform arbitrary site-specific customizations. It is typically created by a system administrator in the site-packages directory. If this import fails with an ImportError exception, it is silently ignored. If Python is started without output streams available, as with pythonw.exe on Windows (which is used by default to start IDLE), attempted output from sitecustomize is ignored. Any exception other than ImportError causes a silent and perhaps mysterious failure of the process.

After this, an attempt is made to import a module named usercustomize, which can perform arbitrary user-specific customizations, if ENABLE_USER_SITE is true. This file is intended to be created in the user site-packages directory (see below), which is part of sys.path unless disabled by -s. An ImportError will be silently ignored.

Note that for some non-Unix systems, sys.prefix and sys.exec_prefix are empty, and the path manipulations are skipped; however the import of sitecustomize and usercustomize is still attempted.

29.13.1. Readline configuration

On systems that support readline, this module will also import and configure the rlcompleter module, if Python is started in interactive mode and without the -S option. The default behavior is enable tab-completion and to use ~/.python_history as the history save file. To disable it, delete (or override) the sys.__interactivehook__ attribute in your sitecustomize or usercustomize module or your PYTHONSTARTUP file.

在 3.4 版更改: Activation of rlcompleter and history was made automatic.

29.13.2. 模块内容

site.PREFIXES

A list of prefixes for site-packages directories.

site.ENABLE_USER_SITE

Flag showing the status of the user site-packages directory. True means that it is enabled and was added to sys.path. False means that it was disabled by user request (with -s or PYTHONNOUSERSITE). None means it was disabled for security reasons (mismatch between user or group id and effective id) or by an administrator.

site.USER_SITE

Path to the user site-packages for the running Python. Can be None if getusersitepackages() hasn’t been called yet. Default value is ~/.local/lib/pythonX.Y/site-packages for UNIX and non-framework Mac OS X builds, ~/Library/Python/X.Y/lib/python/site-packages for Mac framework builds, and %APPDATA%\Python\PythonXY\site-packages on Windows. This directory is a site directory, which means that .pth files in it will be processed.

site.USER_BASE

Path to the base directory for the user site-packages. Can be None if getuserbase() hasn’t been called yet. Default value is ~/.local for UNIX and Mac OS X non-framework builds, ~/Library/Python/X.Y for Mac framework builds, and %APPDATA%\Python for Windows. This value is used by Distutils to compute the installation directories for scripts, data files, Python modules, etc. for the user installation scheme. See also PYTHONUSERBASE.

site.main()

Adds all the standard site-specific directories to the module search path. This function is called automatically when this module is imported, unless the Python interpreter was started with the -S flag.

在 3.3 版更改: This function used to be called unconditionally.

site.addsitedir(sitedir, known_paths=None)

Add a directory to sys.path and process its .pth files. Typically used in sitecustomize or usercustomize (see above).

site.getsitepackages()

Return a list containing all global site-packages directories.

3.2 新版功能.

site.getuserbase()

Return the path of the user base directory, USER_BASE. If it is not initialized yet, this function will also set it, respecting PYTHONUSERBASE.

3.2 新版功能.

site.getusersitepackages()

Return the path of the user-specific site-packages directory, USER_SITE. If it is not initialized yet, this function will also set it, respecting PYTHONNOUSERSITE and USER_BASE.

3.2 新版功能.

site 模块还提供了一个从命令行获取用户目录的方式:

$ python3 -m site --user-site
/home/user/.local/lib/python3.3/site-packages

If it is called without arguments, it will print the contents of sys.path on the standard output, followed by the value of USER_BASE and whether the directory exists, then the same thing for USER_SITE, and finally the value of ENABLE_USER_SITE.

--user-base

Print the path to the user base directory.

--user-site

Print the path to the user site-packages directory.

If both options are given, user base and user site will be printed (always in this order), separated by os.pathsep.

If any option is given, the script will exit with one of these values: 0 if the user site-packages directory is enabled, 1 if it was disabled by the user, 2 if it is disabled for security reasons or by an administrator, and a value greater than 2 if there is an error.

参见

PEP 370 [https://www.python.org/dev/peps/pep-0370] – 分用户的 site-packages 目录

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

29.14. fpectl — Floating point exception control

注解

The fpectl module is not built by default, and its usage is discouraged and may be dangerous except in the hands of experts. See also the section Limitations and other considerations on limitations for more details.

Most computers carry out floating point operations in conformance with the so-called IEEE-754 standard. On any real computer, some floating point operations produce results that cannot be expressed as a normal floating point value. For example, try

>>> import math
>>> math.exp(1000)
inf
>>> math.exp(1000) / math.exp(1000)
nan

(The example above will work on many platforms. DEC Alpha may be one exception.) “Inf” is a special, non-numeric value in IEEE-754 that stands for “infinity”, and “nan” means “not a number.” Note that, other than the non-numeric results, nothing special happened when you asked Python to carry out those calculations. That is in fact the default behaviour prescribed in the IEEE-754 standard, and if it works for you, stop reading now.

In some circumstances, it would be better to raise an exception and stop processing at the point where the faulty operation was attempted. The fpectl module is for use in that situation. It provides control over floating point units from several hardware manufacturers, allowing the user to turn on the generation of SIGFPE whenever any of the IEEE-754 exceptions Division by Zero, Overflow, or Invalid Operation occurs. In tandem with a pair of wrapper macros that are inserted into the C code comprising your python system, SIGFPE is trapped and converted into the Python FloatingPointError exception.

The fpectl module defines the following functions and may raise the given exception:

fpectl.turnon_sigfpe()

Turn on the generation of SIGFPE, and set up an appropriate signal handler.

fpectl.turnoff_sigfpe()

Reset default handling of floating point exceptions.

exception fpectl.FloatingPointError

After turnon_sigfpe() has been executed, a floating point operation that raises one of the IEEE-754 exceptions Division by Zero, Overflow, or Invalid operation will in turn raise this standard Python exception.

29.14.1. Example

The following example demonstrates how to start up and test operation of the fpectl module.

>>> import fpectl
>>> import fpetest
>>> fpectl.turnon_sigfpe()
>>> fpetest.test()
overflow        PASS
FloatingPointError: Overflow

div by 0        PASS
FloatingPointError: Division by zero
  [ more output from test elided ]
>>> import math
>>> math.exp(1000)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FloatingPointError: in math_1

29.14.2. Limitations and other considerations

Setting up a given processor to trap IEEE-754 floating point errors currently requires custom code on a per-architecture basis. You may have to modify fpectl to control your particular hardware.

Conversion of an IEEE-754 exception to a Python exception requires that the wrapper macros PyFPE_START_PROTECT and PyFPE_END_PROTECT be inserted into your code in an appropriate fashion. Python itself has been modified to support the fpectl module, but many other codes of interest to numerical analysts have not.

The fpectl module is not thread-safe.

参见

Some files in the source distribution may be interesting in learning more about how this module operates. The include file Include/pyfpe.h discusses the implementation of this module at some length. Modules/fpetestmodule.c gives several examples of use. Many additional examples can be found in Objects/floatobject.c.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

29. Python运行时服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

30. 自定义 Python 解释器

本章中描述的模块允许编写类似于 Python 的交互式解释器的接口。 如果你想要一个支持附加一些特殊功能到 Python 语言的 Python 解释器，你应该看看 code 模块。 （ codeop 模块是低层级的，用于支持编译可能不完整的 Python 代码块。）

本章描述的完整模块列表如下：

30.1. code — 解释器基础类

30.1.1. 交互解释器对象

30.1.2. 交互式控制台对象

30.2. codeop — 编译Python代码

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

30. 自定义 Python 解释器 »

|

30.1. code — 解释器基础类

源代码： Lib/code.py [https://github.com/python/cpython/tree/3.6/Lib/code.py]

code 模块提供了在 Python 中实现 read-eval-print 循环的功能。它包含两个类和一些快捷功能，可用于构建提供交互式解释器的应用程序。

class code.InteractiveInterpreter(locals=None)

这个类处理解析器和解释器状态（用户命名空间的）；它不处理缓冲器、终端提示区或着输入文件名（文件名总是显示地传递）。可选的 locals 参数指定一个字典，字典里面包含将在此类执行的代码；它默认创建新的字典，其键 '__name__' 设置为 '__console__' ，键 '__doc__' 设置为 None 。

class code.InteractiveConsole(locals=None, filename="<console>")

尽可能模拟交互式 Python 解释器的行为。此类建立在 InteractiveInterpreter 的基础上，使用熟悉的 sys.ps1 和 sys.ps2 作为输入提示符，并有输入缓冲。

code.interact(banner=None, readfunc=None, local=None, exitmsg=None)

运行一个 read-eval-print 循环的便捷函数。这会创建一个新的 InteractiveConsole 实例。如果提供了 readfunc ，会设置为 InteractiveConsole.raw_input() 方法。如果提供了 local ，则将其传递给 InteractiveConsole 的构造函数，以用作解释器循环的默认命名空间。然后，如果提供了 banner 和 exitmsg ，实例的 interact() 方法会以此为标题和退出消息。控制台对象在使用后将被丢弃。

在 3.6 版更改: 加入 exitmsg 参数。

code.compile_command(source, filename="<input>", symbol="single")

这个函数主要用来模拟 Python 解释器的主循环（即 read-eval-print 循环）。难点的部分是当用户输入不完整命令时，判断能否通过之后的输入来完成（要么成为完整的命令，要么语法错误）。该函数 几乎 和实际的解释器主循环的判断是相同的。

source is the source string; filename is the optional filename from which source was read, defaulting to '<input>'; and symbol is the optional grammar start symbol, which should be either 'single' (the default) or 'eval'.

如果命令完整且有效则返回一个代码对象 (等价于 compile(source, filename, symbol))；如果命令不完整则返回 None；如果命令完整但包含语法错误则会引发 SyntaxError 或 OverflowError 而如果命令包含无效字面值则将引发 ValueError。

30.1.1. 交互解释器对象

InteractiveInterpreter.runsource(source, filename="<input>", symbol="single")

Compile and run some source in the interpreter. Arguments are the same as for compile_command(); the default for filename is '<input>', and for symbol is 'single'. One several things can happen:

输入不正确；compile_command() 引发了一个异常 (SyntaxError 或 OverflowError)。 将通过调用 showsyntaxerror() 方法打印语法回溯信息。 runsource() 返回 False。

输入不完整，需要更多输入；函数 compile_command() 返回 None 。方法 runsource() 返回 True 。

输入完整；compile_command() 返回了一个代码对象。 将通过调用 runcode() 执行代码（该方法也会处理运行时异常，SystemExit 除外）。 runsource() 返回 False。

该返回值用于决定使用 sys.ps1 还是 sys.ps2 来作为下一行的输入提示符。

InteractiveInterpreter.runcode(code)

执行一个代码对象。当发生异常时，调用 showtraceback() 来显示回溯。除 SystemExit （允许传播）以外的所有异常都会被捕获。

有关 KeyboardInterrupt 的说明，该异常可能发生于此代码的其他位置，并且并不总能被捕获。 调用者应当准备好处理它。

InteractiveInterpreter.showsyntaxerror(filename=None)

显示刚发生的语法错误。 这不会显示堆栈回溯因为语法错误并无此种信息。 如果给出了 filename，它会被放入异常来替代 Python 解析器所提供的默认文件名，因为它在从一个字符串读取时总是会使用 '<string>'。 输出将由 write() 方法来写入。

InteractiveInterpreter.showtraceback()

显示刚发生的异常。 我们移除了第一个堆栈条目因为它从属于解释器对象的实现。 输出将由 write() 方法来写入。

在 3.5 版更改: 将显示完整的链式回溯，而不只是主回溯。

InteractiveInterpreter.write(data)

将一个字符串写入到标准错误流 (sys.stderr)。 所有派生类都应重载此方法以提供必要的正确输出处理。

30.1.2. 交互式控制台对象

InteractiveConsole 类是 InteractiveInterpreter 的子类，因此它提供了解释器对象的所有方法，还有以下的额外方法。

InteractiveConsole.interact(banner=None, exitmsg=None)

近似地模拟交互式 Python 终端。 可选的 banner 参数指定要在第一次交互前打印的条幅；默认情况下会类似于标准 Python 解释器所打印的内容，并附上外加圆括号的终端对象类名（这样就不会与真正的解释器混淆 —— 因为确实太像了！）

可选的 exitmsg 参数指定要在退出时打印的退出消息。 传入空字符串可以屏蔽退出消息。 如果 exitmsg 未给出或为 None，则将打印默认消息。

在 3.4 版更改: 要禁止打印任何标志，请传递一个空字符串。

在 3.6 版更改: 退出时打印相关消息。

InteractiveConsole.push(line)

将一行源文本推入解释器。 行内容不应带有末尾换行符；它可以有内部换行符。 行内容会被添加到一个缓冲区并且会调用解释器的 runsource() 方法，附带缓冲区内容的拼接结果作为源文本。 如果显示命令已执行或不合法，缓冲区将被重置；否则，则命令尚未结束，缓冲区将在添加行后保持原样。 如果要求更多输入则返回值为 True，如果行已按某种方式被处理则返回值为 False (这与 runsource() 相同)。

InteractiveConsole.resetbuffer()

从输入缓冲区中删除所有未处理的内容。

InteractiveConsole.raw_input(prompt="")

输出提示并读取一行。返回的行不包含末尾的换行符。当用户输入 EOF 键序列时，会引发 EOFError 异常。默认实现是从 sys.stdin 读取；子类可以用其他实现代替。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

30. 自定义 Python 解释器 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

30. 自定义 Python 解释器 »

|

30.2. codeop — 编译Python代码

源代码： Lib/codeop.py [https://github.com/python/cpython/tree/3.6/Lib/codeop.py]

codeop 模块提供了可以模拟Python读取-执行-打印循环的实用程序，就像在 code 模块中一样。因此，您可能不希望直接使用该模块；如果你想在程序中包含这样一个循环，你可能需要使用 code 模块。

这个任务有两个部分:

能够判断一行输入是否完成了一个Python语句：简而言之，告诉我们是否要打印 ‘>>>’ 或 ‘...’。

记住用户已输入了哪些 future 语句，这样后续的输入可以在这些语句被启用的状态下被编译。

codeop 模块提供了分别以及同时执行这两个部分的方式。

只执行前一部分：

codeop.compile_command(source, filename="<input>", symbol="single")

尝试编译 source，这应当是一个 Python 代码字符串，并且在 source 是有效的 Python 代码时返回一个代码对象。 在此情况下，代码对象的 filename 属性将为 filename，其默认值为 '<input>'。 如果 source 不是 有效的 Python 代码而是有效的 Python 代码的一个前缀时将返回 None。

如果 source 存在问题，将引发异常。 如果存在无效的 Python 语法将引发 SyntaxError，而如果存在无效的字面值则将引发 OverflowError 或 ValueError。

The symbol argument determines whether source is compiled as a statement ('single', the default) or as an expression ('eval'). Any other value will cause ValueError to be raised.

注解

解析器有可能（但很不常见）会在到达源码结尾之前停止解析并成功输出结果；在这种情况下，末尾的符号可能会被忽略而不是引发错误。 例如，一个反斜杠加两个换行符之后可以跟随任何无意义的符号。 一旦解析器 API 得到改进将修正这个问题。

class codeop.Compile

这个类的实例具有 __call__() 方法，其签名与内置函数 compile() 相似，区别在于如果该实例编译了包含 __future__ 语句的程序文本，则实例会‘记住’并使用已生效的语句编译所有后续程序文本。

class codeop.CommandCompiler

这个类的实例具有 __call__() 方法，其签名与 compile_command() 相似；区别在于如果该实例编译了包含 __future__ 语句的程序文本，则实例会‘记住’并使用已生效的语句编译编译所有后续程序文本。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

30. 自定义 Python 解释器 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

31. 导入模块

本章中介绍的模块提供了导入其他Python模块和挂钩以自定义导入过程的新方法。

本章描述的完整模块列表如下：

31.1. zipimport — 从 Zip 存档中导入模块

31.1.1. zipimporter 对象

31.1.2. 例子

31.2. pkgutil — 包扩展模块工具

31.3. modulefinder — 查找脚本使用的模块

31.3.1. ModuleFinder 的示例用法

31.4. runpy — Locating and executing Python modules

31.5. importlib — The implementation of import

31.5.1. 概述

31.5.2. 函数

31.5.3. importlib.abc —— 关于导入的抽象基类

31.5.4. importlib.machinery – Importers and path hooks

31.5.5. importlib.util – Utility code for importers

31.5.6. 例子

31.5.6.1. Importing programmatically

31.5.6.2. Checking if a module can be imported

31.5.6.3. Importing a source file directly

31.5.6.4. Setting up an importer

31.5.6.5. Approximating importlib.import_module()

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

31. 导入模块 »

|

31.1. zipimport — 从 Zip 存档中导入模块

此模块添加了从 ZIP 格式档案中导入 Python 模块（ *.py ， *.pyc ）和包的能力。通常不需要明确地使用 zipimport 模块，内置的 import 机制会自动将此模块用于 ZIP 档案路径的 sys.path 项目上。

通常， sys.path 是字符串的目录名称列表。此模块同样允许 sys.path 的一项成为命名 ZIP 文件档案的字符串。 ZIP 档案可以容纳子目录结构去支持包的导入，并且可以将归档文件中的路径指定为仅从子目录导入。比如说，路径 example.zip/lib/ 将只会从档案中的 lib/ 子目录导入。

任何文件都可以存在于 ZIP档案之中，但是只有 .py 和 .pyc 文件是能够导入的。不允许导入 ZIP 中的动态模组（ .pyd ， .so ）。请注意，如果档案中只包含 .py 文件， Python不会尝试通过添加对应的 .pyc 文件修改档案，意思是如果 ZIP 档案不包含 .pyc 文件，导入或许会变慢。

目前不支持带有档案注释的 ZIP 归档。

参见

PKZIP Application Note [https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT]

Phil Katz 编写的 ZIP 文件格式文档，此格式和使用的算法的创建者。

PEP 273 [https://www.python.org/dev/peps/pep-0273] -  从ZIP压缩包导入模块

由 James C. Ahlstrom 编写，他还提供了一个具体实现。 Python 2.3 遵循 PEP 273 的规范，但使用了一个由 van Rossum 本人所编写的实现，该实现使用了 PEP 302 所描述的导入钩子。

PEP 302 [https://www.python.org/dev/peps/pep-0302] - 新导入钩

PEP 添加导入钩来有助于模块运作。

此模块定义了一个异常：

exception zipimport.ZipImportError

异常由 zipimporter 对象引发。这是 ImportError 的子类，因此，也可以捕获为 ImportError 。

31.1.1. zipimporter 对象

zipimporter 是用于导入 ZIP 文件的类。

class zipimport.zipimporter(archivepath)

创建新的 zipimporter 实例。 archivepath 必须是指向 ZIP 文件的路径，或者 ZIP 文件中的特定路径。例如， foo/bar.zip/lib 的 archivepath 将在 ZIP 文件 foo/bar.zip 中的 lib 目录中查找模块。

如果 archivepath 没有指向一个有效的 ZIP 档案，引发 ZipImportError 。

find_module(fullname[, path])

搜索由 fullname 指定的模块。 fullname 必须是完全合格的（含加点作分割的拓展名）模块名。它返回 zipimporter 实例本身如果模块被找到，或者返回 None 如果没找到指定模块。可选的 path 被忽略，这是为了与导入器协议兼容。

get_code(fullname)

返回指定模块的代码对象。如果不能找到模块会引发 ZipImportError 错误。

get_data(pathname)

返回与 pathname 相关联的数据。如果不能找到文件则引发 OSError 错误。

在 3.3 版更改: IOError 代替 OSError 被引发。

get_filename(fullname)

如果导入了指定的模块 __file__ ，则返回为该模块设置的值。如果未找到模块则引发 ZipImportError 错误。

3.1 新版功能.

get_source(fullname)

返回指定模块的源代码。如果没有找到模块则引发 ZipImportError ，如果档案包含模块但是没有源代码，返回 None 。

is_package(fullname)

如果由 fullname 指定的模块是一个包则返回 True 。如果不能找到模块则引发 ZipImportError 错误。

load_module(fullname)

加载由 fullname 指定的模块。 fullname 必须是完全合格的（含加点作为分割的拓展名）模块名。它返回已加载模块，或者当找不到模块时引发 ZipImportError 错误。

archive

导入器关联的 ZIP 文件的文件名，不含可能的子路径。

prefix

ZIP 文件中搜索的模块的子路径。这是一个指向 ZIP 文件 根目录的 zipimporter 对象的空字符串。

当与斜杠结合使用时， archive 和 prefix 属性等价于赋予 zipimporter 构造器的原始 archivepath 参数。

31.1.2. 例子

这是一个从 ZIP 档案中导入模块的例子 - 请注意 zipimport 不需要明确地使用。

$ unzip -l example.zip
Archive:  example.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
     8467  11-26-02 22:30   jwzthreading.py
 --------                   -------
     8467                   1 file
$ ./python
Python 2.3 (#1, Aug 1 2003, 19:54:32)
>>> import sys
>>> sys.path.insert(0, 'example.zip')  # Add .zip file to front of path
>>> import jwzthreading
>>> jwzthreading.__file__
'example.zip/jwzthreading.py'

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

31. 导入模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

31. 导入模块 »

|

31.2. pkgutil — 包扩展模块工具

源代码: Lib/pkgutil.py [https://github.com/python/cpython/tree/3.6/Lib/pkgutil.py]

该模块为导入系统提供了工具，尤其是在包支持方面。

class pkgutil.ModuleInfo(module_finder, name, ispkg)

一个包含模块信息的简短摘要的命名元组。

3.6 新版功能.

pkgutil.extend_path(path, name)

Extend the search path for the modules which comprise a package. Intended use is to place the following code in a package’s __init__.py:

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)

This will add to the package’s __path__ all subdirectories of directories on sys.path named after the package. This is useful if one wants to distribute different parts of a single logical package as multiple directories.

It also looks for *.pkg files beginning where * matches the name argument. This feature is similar to *.pth files (see the site module for more information), except that it doesn’t special-case lines starting with import. A *.pkg file is trusted at face value: apart from checking for duplicates, all entries found in a *.pkg file are added to the path, regardless of whether they exist on the filesystem. (This is a feature.)

If the input path is not a list (as is the case for frozen packages) it is returned unchanged. The input path is not modified; an extended copy is returned. Items are only appended to the copy at the end.

It is assumed that sys.path is a sequence. Items of sys.path that are not strings referring to existing directories are ignored. Unicode items on sys.path that cause errors when used as filenames may cause this function to raise an exception (in line with os.path.isdir() behavior).

class pkgutil.ImpImporter(dirname=None)

PEP 302 [https://www.python.org/dev/peps/pep-0302] Finder that wraps Python’s “classic” import algorithm.

If dirname is a string, a PEP 302 [https://www.python.org/dev/peps/pep-0302] finder is created that searches that directory. If dirname is None, a PEP 302 [https://www.python.org/dev/peps/pep-0302] finder is created that searches the current sys.path, plus any modules that are frozen or built-in.

Note that ImpImporter does not currently support being used by placement on sys.meta_path.

3.3 版后已移除: This emulation is no longer needed, as the standard import mechanism is now fully PEP 302 compliant and available in importlib.

class pkgutil.ImpLoader(fullname, file, filename, etc)

Loader that wraps Python’s “classic” import algorithm.

3.3 版后已移除: This emulation is no longer needed, as the standard import mechanism is now fully PEP 302 compliant and available in importlib.

pkgutil.find_loader(fullname)

Retrieve a module loader for the given fullname.

This is a backwards compatibility wrapper around importlib.util.find_spec() that converts most failures to ImportError and only returns the loader rather than the full ModuleSpec.

在 3.3 版更改: Updated to be based directly on importlib rather than relying on the package internal PEP 302 import emulation.

在 3.4 版更改: Updated to be based on PEP 451 [https://www.python.org/dev/peps/pep-0451]

pkgutil.get_importer(path_item)

Retrieve a finder for the given path_item.

The returned finder is cached in sys.path_importer_cache if it was newly created by a path hook.

The cache (or part of it) can be cleared manually if a rescan of sys.path_hooks is necessary.

在 3.3 版更改: Updated to be based directly on importlib rather than relying on the package internal PEP 302 import emulation.

pkgutil.get_loader(module_or_name)

Get a loader object for module_or_name.

If the module or package is accessible via the normal import mechanism, a wrapper around the relevant part of that machinery is returned. Returns None if the module cannot be found or imported. If the named module is not already imported, its containing package (if any) is imported, in order to establish the package __path__.

在 3.3 版更改: Updated to be based directly on importlib rather than relying on the package internal PEP 302 import emulation.

在 3.4 版更改: Updated to be based on PEP 451 [https://www.python.org/dev/peps/pep-0451]

pkgutil.iter_importers(fullname='')

Yield finder objects for the given module name.

If fullname contains a ‘.’, the finders will be for the package containing fullname, otherwise they will be all registered top level finders (i.e. those on both sys.meta_path and sys.path_hooks).

If the named module is in a package, that package is imported as a side effect of invoking this function.

If no module name is specified, all top level finders are produced.

在 3.3 版更改: Updated to be based directly on importlib rather than relying on the package internal PEP 302 import emulation.

pkgutil.iter_modules(path=None, prefix='')

Yields ModuleInfo for all submodules on path, or, if path is None, all top-level modules on sys.path.

path should be either None or a list of paths to look for modules in.

prefix is a string to output on the front of every module name on output.

注解

Only works for a finder which defines an iter_modules() method. This interface is non-standard, so the module also provides implementations for importlib.machinery.FileFinder and zipimport.zipimporter.

在 3.3 版更改: Updated to be based directly on importlib rather than relying on the package internal PEP 302 import emulation.

pkgutil.walk_packages(path=None, prefix='', onerror=None)

Yields ModuleInfo for all modules recursively on path, or, if path is None, all accessible modules.

path should be either None or a list of paths to look for modules in.

prefix is a string to output on the front of every module name on output.

Note that this function must import all packages (not all modules!) on the given path, in order to access the __path__ attribute to find submodules.

onerror is a function which gets called with one argument (the name of the package which was being imported) if any exception occurs while trying to import a package. If no onerror function is supplied, ImportErrors are caught and ignored, while all other exceptions are propagated, terminating the search.

示例:

# list all modules python can access
walk_packages()

# list all submodules of ctypes
walk_packages(ctypes.__path__, ctypes.__name__ + '.')

注解

Only works for a finder which defines an iter_modules() method. This interface is non-standard, so the module also provides implementations for importlib.machinery.FileFinder and zipimport.zipimporter.

在 3.3 版更改: Updated to be based directly on importlib rather than relying on the package internal PEP 302 import emulation.

pkgutil.get_data(package, resource)

从包中获取一个资源。

This is a wrapper for the loader get_data API. The package argument should be the name of a package, in standard module format (foo.bar). The resource argument should be in the form of a relative filename, using / as the path separator. The parent directory name .. is not allowed, and nor is a rooted name (starting with a /).

The function returns a binary string that is the contents of the specified resource.

For packages located in the filesystem, which have already been imported, this is the rough equivalent of:

d = os.path.dirname(sys.modules[package].__file__)
data = open(os.path.join(d, resource), 'rb').read()

If the package cannot be located or loaded, or it uses a loader which does not support get_data, then None is returned. In particular, the loader for namespace packages does not support get_data.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

31. 导入模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

31. 导入模块 »

|

31.3. modulefinder — 查找脚本使用的模块

源码： Lib/modulefinder.py [https://github.com/python/cpython/tree/3.6/Lib/modulefinder.py]

该模块提供了一个 ModuleFinder 类，可用于确定脚本导入的模块集。 modulefinder.py 也可以作为脚本运行，给出 Python 脚本的文件名作为参数，之后将打印导入模块的报告。

modulefinder.AddPackagePath(pkg_name, path)

记录名为 pkg_name 的包可以在指定的 path 中找到。

modulefinder.ReplacePackage(oldname, newname)

允许指定名为 oldname 的模块实际上是名为 newname 的包。

class modulefinder.ModuleFinder(path=None, debug=0, excludes=[], replace_paths=[])

该类提供 run_script() 和 report() 方法，用于确定脚本导入的模块集。 path 可以是搜索模块的目录列表；如果没有指定，则使用 sys.path 。 debug 设置调试级别；更高的值使类打印调试消息，关于它正在做什么。 excludes 是要从分析中排除的模块名称列表。 replace_paths 是将在模块路径中替换的 (oldpath, newpath) 元组的列表。

report()

将报告打印到标准输出，列出脚本导入的模块及其路径，以及缺少或似乎缺失的模块。

run_script(pathname)

分析 pathname 文件的内容，该文件必须包含 Python 代码。

modules

一个将模块名称映射到模块的字典。 请参阅 ModuleFinder 的示例用法。

31.3.1. ModuleFinder 的示例用法

稍后将分析的脚本（bacon.py）:

import re, itertools

try:
    import baconhameggs
except ImportError:
    pass

try:
    import guido.python.ham
except ImportError:
    pass

将输出 bacon.py 报告的脚本:

from modulefinder import ModuleFinder

finder = ModuleFinder()
finder.run_script('bacon.py')

print('Loaded modules:')
for name, mod in finder.modules.items():
    print('%s: ' % name, end='')
    print(','.join(list(mod.globalnames.keys())[:3]))

print('-'*50)
print('Modules not imported:')
print('\n'.join(finder.badmodules.keys()))

输出样例（可能因架构而异）:

Loaded modules:
_types:
copyreg:  _inverted_registry,_slotnames,__all__
sre_compile:  isstring,_sre,_optimize_unicode
_sre:
sre_constants:  REPEAT_ONE,makedict,AT_END_LINE
sys:
re:  __module__,finditer,_expand
itertools:
__main__:  re,itertools,baconhameggs
sre_parse:  _PATTERNENDERS,SRE_FLAG_UNICODE
array:
types:  __module__,IntType,TypeType
---------------------------------------------------
Modules not imported:
guido.python.ham
baconhameggs

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

31. 导入模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

31. 导入模块 »

|

31.4. runpy — Locating and executing Python modules

Source code: Lib/runpy.py [https://github.com/python/cpython/tree/3.6/Lib/runpy.py]

The runpy module is used to locate and run Python modules without importing them first. Its main use is to implement the -m command line switch that allows scripts to be located using the Python module namespace rather than the filesystem.

Note that this is not a sandbox module - all code is executed in the current process, and any side effects (such as cached imports of other modules) will remain in place after the functions have returned.

Furthermore, any functions and classes defined by the executed code are not guaranteed to work correctly after a runpy function has returned. If that limitation is not acceptable for a given use case, importlib is likely to be a more suitable choice than this module.

The runpy module provides two functions:

runpy.run_module(mod_name, init_globals=None, run_name=None, alter_sys=False)

Execute the code of the specified module and return the resulting module globals dictionary. The module’s code is first located using the standard import mechanism (refer to PEP 302 [https://www.python.org/dev/peps/pep-0302] for details) and then executed in a fresh module namespace.

The mod_name argument should be an absolute module name. If the module name refers to a package rather than a normal module, then that package is imported and the __main__ submodule within that package is then executed and the resulting module globals dictionary returned.

The optional dictionary argument init_globals may be used to pre-populate the module’s globals dictionary before the code is executed. The supplied dictionary will not be modified. If any of the special global variables below are defined in the supplied dictionary, those definitions are overridden by run_module().

The special global variables __name__, __spec__, __file__, __cached__, __loader__ and __package__ are set in the globals dictionary before the module code is executed (Note that this is a minimal set of variables - other variables may be set implicitly as an interpreter implementation detail).

__name__ is set to run_name if this optional argument is not None, to mod_name + '.__main__' if the named module is a package and to the mod_name argument otherwise.

__spec__ will be set appropriately for the actually imported module (that is, __spec__.name will always be mod_name or mod_name + '.__main__, never run_name).

__file__, __cached__, __loader__ and __package__ are set as normal based on the module spec.

If the argument alter_sys is supplied and evaluates to True, then sys.argv[0] is updated with the value of __file__ and sys.modules[__name__] is updated with a temporary module object for the module being executed. Both sys.argv[0] and sys.modules[__name__] are restored to their original values before the function returns.

Note that this manipulation of sys is not thread-safe. Other threads may see the partially initialised module, as well as the altered list of arguments. It is recommended that the sys module be left alone when invoking this function from threaded code.

参见

The -m option offering equivalent functionality from the command line.

在 3.1 版更改: Added ability to execute packages by looking for a __main__ submodule.

在 3.2 版更改: Added __cached__ global variable (see PEP 3147 [https://www.python.org/dev/peps/pep-3147]).

在 3.4 版更改: Updated to take advantage of the module spec feature added by PEP 451 [https://www.python.org/dev/peps/pep-0451]. This allows __cached__ to be set correctly for modules run this way, as well as ensuring the real module name is always accessible as __spec__.name.

runpy.run_path(file_path, init_globals=None, run_name=None)

Execute the code at the named filesystem location and return the resulting module globals dictionary. As with a script name supplied to the CPython command line, the supplied path may refer to a Python source file, a compiled bytecode file or a valid sys.path entry containing a __main__ module (e.g. a zipfile containing a top-level __main__.py file).

For a simple script, the specified code is simply executed in a fresh module namespace. For a valid sys.path entry (typically a zipfile or directory), the entry is first added to the beginning of sys.path. The function then looks for and executes a __main__ module using the updated path. Note that there is no special protection against invoking an existing __main__ entry located elsewhere on sys.path if there is no such module at the specified location.

The optional dictionary argument init_globals may be used to pre-populate the module’s globals dictionary before the code is executed. The supplied dictionary will not be modified. If any of the special global variables below are defined in the supplied dictionary, those definitions are overridden by run_path().

The special global variables __name__, __spec__, __file__, __cached__, __loader__ and __package__ are set in the globals dictionary before the module code is executed (Note that this is a minimal set of variables - other variables may be set implicitly as an interpreter implementation detail).

__name__ is set to run_name if this optional argument is not None and to '<run_path>' otherwise.

If the supplied path directly references a script file (whether as source or as precompiled byte code), then __file__ will be set to the supplied path, and __spec__, __cached__, __loader__ and __package__ will all be set to None.

If the supplied path is a reference to a valid sys.path entry, then __spec__ will be set appropriately for the imported __main__ module (that is, __spec__.name will always be __main__). __file__, __cached__, __loader__ and __package__ will be set as normal based on the module spec.

A number of alterations are also made to the sys module. Firstly, sys.path may be altered as described above. sys.argv[0] is updated with the value of file_path and sys.modules[__name__] is updated with a temporary module object for the module being executed. All modifications to items in sys are reverted before the function returns.

Note that, unlike run_module(), the alterations made to sys are not optional in this function as these adjustments are essential to allowing the execution of sys.path entries. As the thread-safety limitations still apply, use of this function in threaded code should be either serialised with the import lock or delegated to a separate process.

参见

接口选项 for equivalent functionality on the command line (python path/to/script).

3.2 新版功能.

在 3.4 版更改: Updated to take advantage of the module spec feature added by PEP 451 [https://www.python.org/dev/peps/pep-0451]. This allows __cached__ to be set correctly in the case where __main__ is imported from a valid sys.path entry rather than being executed directly.

参见

PEP 338 [https://www.python.org/dev/peps/pep-0338] – 将模块作为脚本执行

PEP 由 Nick Coghlan 撰写并实现。

PEP 366 [https://www.python.org/dev/peps/pep-0366] – Main module explicit relative imports

PEP 由 Nick Coghlan 撰写并实现。

PEP 451 [https://www.python.org/dev/peps/pep-0451] – A ModuleSpec Type for the Import System

PEP written and implemented by Eric Snow

命令行与环境 - CPython command line details

The importlib.import_module() function

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

31. 导入模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

31. 导入模块 »

|

31.5. importlib — The implementation of import

3.1 新版功能.

源代码 Lib/importlib/__init__.py [https://github.com/python/cpython/tree/3.6/Lib/importlib/__init__.py]

31.5.1. 概述

The purpose of the importlib package is two-fold. One is to provide the implementation of the import statement (and thus, by extension, the __import__() function) in Python source code. This provides an implementation of import which is portable to any Python interpreter. This also provides an implementation which is easier to comprehend than one implemented in a programming language other than Python.

第二个目的是实现 import 的部分被公开在这个包中，使得用户更容易创建他们自己的自定义对象 (通常被称为 importer) 来参与到导入过程中。

参见

The import statement

import 语句的语言参考

包规格说明 [https://www.python.org/doc/essays/packages/]

包的初始规范。自从编写这个文档开始，一些语义已经发生改变了（比如基于 sys.modules 中 None 的重定向）。

__import__() 函数

import 语句是这个函数的语法糖。

PEP 235 [https://www.python.org/dev/peps/pep-0235]

在忽略大小写的平台上进行导入

PEP 263 [https://www.python.org/dev/peps/pep-0263]

定义 Python 源代码编码

PEP 302 [https://www.python.org/dev/peps/pep-0302]

新导入钩子

PEP 328 [https://www.python.org/dev/peps/pep-0328]

导入：多行和绝对/相对

PEP 366 [https://www.python.org/dev/peps/pep-0366]

主模块显式相对导入

PEP 420 [https://www.python.org/dev/peps/pep-0420]

隐式命名空间包

PEP 451 [https://www.python.org/dev/peps/pep-0451]

导入系统的一个模块规范类型

PEP 488 [https://www.python.org/dev/peps/pep-0488]

消除PYO文件

PEP 489 [https://www.python.org/dev/peps/pep-0489]

多阶段扩展模块初始化

PEP 3120 [https://www.python.org/dev/peps/pep-3120]

使用 UTF-8 作为默认的源编码

PEP 3147 [https://www.python.org/dev/peps/pep-3147]

PYC 仓库目录

31.5.2. 函数

importlib.__import__(name, globals=None, locals=None, fromlist=(), level=0)

内置 __import__() 函数的实现。

注解

程序式地导入模块应该使用 import_module() 而不是这个函数。

importlib.import_module(name, package=None)

导入一个模块。参数 name 指定了以绝对或相对导入方式导入什么模块 (比如要么像这样 pkg.mod 或者这样 ..mod)。如果参数 name 使用相对导入的方式来指定，那么那个参数 packages 必须设置为那个包名，这个包名作为解析这个包名的锚点 (比如 import_module('..mod', 'pkg.subpkg') 将会导入 pkg.mod)。

import_module() 函数是一个对 importlib.__import__() 进行简化的包装器。 这意味着该函数的所有主义都来自于 importlib.__import__()。 这两个函数之间最重要的不同点在于 import_module() 返回指定的包或模块 (例如 pkg.mod)，而 __import__() 返回最高层级的包或模块 (例如 pkg)。

如果动态导入一个自从解释器开始执行以来被创建的模块（即创建了一个 Python 源代码文件），为了让导入系统知道这个新模块，可能需要调用 invalidate_caches()。

在 3.3 版更改: 父包会被自动导入。

importlib.find_loader(name, path=None)

查找一个模块的加载器，可选择地在指定的 path 里面。如果这个模块是在 sys.modules，那么返回 sys.modules[name].__loader__ (除非这个加载器是 None 或者是没有被设置， 在这样的情况下，会引起 ValueError 异常）。 否则使用 sys.meta_path 的一次搜索就结束。如果未发现加载器，则返回 None。

点状的名称没有使得它父包或模块隐式地导入，因为它需要加载它们并且可能不需要。为了适当地导入一个子模块，需要导入子模块的所有父包并且使用正确的参数提供给 path。

3.3 新版功能.

在 3.4 版更改: 如果没有设置 __loader__，会引起 ValueError 异常，就像属性设置为 None 的时候一样。

3.4 版后已移除: 使用 importlib.util.find_spec() 来代替。

importlib.invalidate_caches()

使查找器存储在 sys.meta_path 中的内部缓存无效。如果一个查找器实现了 invalidate_caches()，那么它会被调用来执行那个无效过程。 如果创建/安装任何模块，同时正在运行的程序是为了保证所有的查找器知道新模块的存在，那么应该调用这个函数。

3.3 新版功能.

importlib.reload(module)

重新加载之前导入的 module。那个参数必须是一个模块对象，所以它之前必须已经成功导入了。这样做是有用的，如果使用外部编辑器编已经辑过了那个模块的源代码文件并且想在退出 Python 解释器之前试验这个新版本的模块。函数的返回值是那个模块对象（如果重新导入导致一个不同的对象放置在 sys.modules 中，那么那个模块对象是有可能会不同）。

当执行 reload() 的时候：

Python 模块的代码会被重新编译并且那个模块级的代码被重新执行，通过重新使用一开始加载那个模块的 loader，定义一个新的绑定在那个模块字典中的名称的对象集合。扩展模块的``init``函数不会被调用第二次。

与Python中的所有的其它对象一样，旧的对象只有在它们的引用计数为0之后才会被回收。

模块命名空间中的名称重新指向任何新的或更改后的对象。

其他旧对象的引用（例如那个模块的外部名称）不会被重新绑定到引用的新对象的，并且如果有需要，必须在出现的每个命名空间中进行更新。

有一些其他注意事项：

当一个模块被重新加载的时候，它的字典（包含了那个模块的全区变量）会被保留。名称的重新定义会覆盖旧的定义，所以通常来说这不是问题。如果一个新模块没有定义在旧版本模块中定义的名称，则将保留旧版本中的定义。这一特性可用于作为那个模块的优点，如果它维护一个全局表或者对象的缓存 —— 使用 try 语句，就可以测试表的存在并且跳过它的初始化，如果有需要的话:

try:
    cache
except NameError:
    cache = {}

重新加载内置的或者动态加载模块，通常来说不是很有用处。不推荐重新加载”sys，__main__，builtins 和其它关键模块。在很多例子中，扩展模块并不是设计为不止一次的初始化，并且当重新加载时，可能会以任意方式失败。

If a module imports objects from another module using from … import …, calling reload() for the other module does not redefine the objects imported from it — one way around this is to re-execute the from statement, another is to use import and qualified names (module.name) instead.

如果一个模块创建一个类的实例，重新加载定义那个类的模块不影响那些实例的方法定义———它们继续使用旧类中的定义。对于子类来说同样是正确的。

3.4 新版功能.

31.5.3. importlib.abc —— 关于导入的抽象基类

源代码： Lib/importlib/abc.py [https://github.com/python/cpython/tree/3.6/Lib/importlib/abc.py]

The importlib.abc module contains all of the core abstract base classes used by import. Some subclasses of the core abstract base classes are also provided to help in implementing the core ABCs.

ABC 类的层次结构：

object
 +-- Finder (deprecated)
 |    +-- MetaPathFinder
 |    +-- PathEntryFinder
 +-- Loader
      +-- ResourceLoader --------+
      +-- InspectLoader          |
           +-- ExecutionLoader --+
                                 +-- FileLoader
                                 +-- SourceLoader

class importlib.abc.Finder

代表 finder 的一个抽象基类

3.3 版后已移除: 使用 MetaPathFinder 或 PathEntryFinder 来代替。

abstractmethod find_module(fullname, path=None)

为指定的模块查找 loader 定义的抽象方法。本来是在 PEP 302 [https://www.python.org/dev/peps/pep-0302] 指定的，这个方法是在 sys.meta_path 和基于路径的导入子系统中使用。

在 3.4 版更改: 当被调用的时候，返回 None 而不是引发 NotImplementedError。

class importlib.abc.MetaPathFinder

代表 meta path finder 的一个抽象基类。 为了保持兼容性，这是 Finder 的一个子类。

3.3 新版功能.

find_spec(fullname, path, target=None)

An abstract method for finding a spec for the specified module. If this is a top-level import, path will be None. Otherwise, this is a search for a subpackage or module and path will be the value of __path__ from the parent package. If a spec cannot be found, None is returned. When passed in, target is a module object that the finder may use to make a more educated guess about what spec to return.

3.4 新版功能.

find_module(fullname, path)

一个用于查找指定的模块中 loader 的遗留方法。如果这是最高层级的导入，path 的值将会是 None。否则，这是一个查找子包或者模块的方法，并且 path 的值将会是来自父包的 __path__ 的值。如果未发现加载器，返回 None。

如果定义了 find_spec() 方法，则提供了向后兼容的功能。

在 3.4 版更改: 当调用这个方法的时候返回 None 而不是引发 NotImplementedError。 可以使用 find_spec() 来提供功能。

3.4 版后已移除: 使用 find_spec() 来代替。

invalidate_caches()

当被调用的时候，一个可选的方法应该将查找器使用的任何内部缓存进行无效。将在 sys.meta_path 上的所有查找器的缓存进行无效的时候，这个函数被 importlib.invalidate_caches() 所使用。

在 3.4 版更改: 当方法被调用的时候，方法返回是 None 而不是 NotImplemented 。

class importlib.abc.PathEntryFinder

path entry finder 的一个抽象基类。尽管这个基类和 MetaPathFinder 有一些相似之处，但是 PathEntryFinder 只在由 PathFinder 提供的基于路径导入子系统中使用。这个抽象类是 Finder 的一个子类，仅仅是因为兼容性的原因。

3.3 新版功能.

find_spec(fullname, target=None)

An abstract method for finding a spec for the specified module. The finder will search for the module only within the path entry to which it is assigned. If a spec cannot be found, None is returned. When passed in, target is a module object that the finder may use to make a more educated guess about what spec to return.

3.4 新版功能.

find_loader(fullname)

一个用于在模块中查找一个 loader 的遗留方法。 返回一个 (loader, portion) 的2元组，portion 是一个贡献给命名空间包部分的文件系统位置的序列。 加载器可能是 None，同时正在指定的 portion 表示的是贡献给命名空间包的文件系统位置。portion 可以使用一个空列表来表示加载器不是命名空间包的一部分。 如果 loader 是 None 并且 portion 是一个空列表，那么命名空间包中无加载器或者文件系统位置可查找到（即在那个模块中未能找到任何东西）。

如果定义了 find_spec() ，则提供了向后兼容的功能。

在 3.4 版更改: 返回 (None, []) 而不是引发 NotImplementedError。 当可于提供相应的功能的时候，使用 find_spec()。

3.4 版后已移除: 使用 find_spec() 来代替。

find_module(fullname)

Finder.find_module`的具体实现，该方法等价于``self.find_loader(fullname)[0]`()。

3.4 版后已移除: 使用 find_spec() 来代替。

invalidate_caches()

当被调用的时候，一个可选的方法应该将查找器使用的任何内部缓存进行无效。当将所有缓存的查找器的缓存进行无效的时候，该函数被 PathFinder.invalidate_caches() 使用。

class importlib.abc.Loader

loader 的抽象基类。 关于一个加载器的实际定义请查看 PEP 302 [https://www.python.org/dev/peps/pep-0302]。

create_module(spec)

当导入一个模块的时候，一个返回将要使用的那个模块对象的方法。这个方法可能返回 None ，这暗示着应该发生默认的模块创建语义。”

3.4 新版功能.

在 3.5 版更改: 从 Python 3.6 开始，当定义了 exec_module() 的时候，这个方法将不会是可选的。

exec_module(module)

当一个模块被导入或重新加载时，一个抽象方法在它自己的命名空间中执行那个模块。当调用 exec_module() 的时候，那个模块应该已经被初始化 了。当这个方法存在时，必须定义 create_module() 。

3.4 新版功能.

在 3.6 版更改: create_module() 也必须被定义。

load_module(fullname)

用于加载一个模块的传统方法。如果这个模块不能被导入，将引起 ImportError 异常，否则返回那个被加载的模块。

如果请求的模块已经存在 sys.modules，应该使用并且重新加载那个模块。 否则加载器应该是创建一个新的模块并且在任何家过程开始之前将这个新模块插入到 sys.modules 中，来阻止递归导入。 如果加载器插入了一个模块并且加载失败了，加载器必须从 sys.modules 中将这个模块移除。在加载器开始执行之前，已经在 sys.modules 中的模块应该被忽略 (查看 importlib.util.module_for_loader())。

加载器应该在模块上面设置几个属性。（要知道当重新加载一个模块的时候，那些属性某部分可以改变）：

__name__

模块的名字

__file__

模块数据存储的路径(不是为了内置的模块而设置)

__cached__

被存储或应该被存储的模块的编译版本的路径（当这个属性不恰当的时候不设置）。

__path__

指定在一个包中搜索路径的一个字符串列表。这个属性不在模块上面进行设置。

__package__

模块/包的父包。如果这个模块是最上层的，那么它是一个为空字符串的值。 importlib.util.module_for_loader() 装饰器可以处理 __package__ 的细节。

__loader__

用来加载那个模块的加载器。 importlib.util.module_for_loader() 装饰器可以处理 __package__ 的细节。

当 exec_module() 可用的时候，那么则提供了向后兼容的功能。

在 3.4 版更改: 当这个方法被调用的时候，触发 ImportError 异常而不是 NotImplementedError。当 exec_module() 可用的时候，使用它的功能。

3.4 版后已移除: 加载模块推荐的使用的 API 是 exec_module() (和 create_module())。 加载器应该实现它而不是 load_module()。 当 exec_module() 被实现的时候，导入机制关心的是 load_module() 所有其他的责任。

module_repr(module)

一个遗留方法，在实现时计算并返回给定模块的 repr，作为字符串。 模块类型的默认 repr() 将根据需要使用此方法的结果。

3.3 新版功能.

在 3.4 版更改: 是可选的方法而不是一个抽象方法。

3.4 版后已移除: 现在导入机制会自动地关注这个方法。

class importlib.abc.ResourceLoader

一个 loader 的抽象基类，它实现了可选的 PEP 302 [https://www.python.org/dev/peps/pep-0302] 协议用于从存储后端加载任意资源。

abstractmethod get_data(path)

一个用于返回位于 path 的字节数据的抽象方法。有一个允许存储任意数据的类文件存储后端的加载器能够实现这个抽象方法来直接访问这些被存储的数据。如果不能够找到 path，则会引发 OSError 异常。path 被希望使用一个模块的 __file 属性或来自一个包的 __path__ 来构建。

在 3.4 版更改: 引发 OSError 异常而不是 NotImplementedError 异常。

class importlib.abc.InspectLoader

一个实现加载器检查模块可选的 PEP 302 [https://www.python.org/dev/peps/pep-0302] 协议的 loader 的抽象基类。

get_code(fullname)

返回一个模块的代码对象，或如果模块没有一个代码对象（例如，对于内置的模块来说，这会是这种情况），则为 None。 如果加载器不能找到请求的模块，则引发 ImportError 异常。

注解

当这个方法有一个默认的实现的时候，出于性能方面的考虑，如果有可能的话，建议覆盖它。

在 3.4 版更改: 不再抽象并且提供一个具体的实现。

abstractmethod get_source(fullname)

一个返回模块源的抽象方法。使用 universal newlines 作为文本字符串被返回，将所有可识别行分割符翻译成 '\n' 字符。 如果没有可用的源（例如，一个内置模块），则返回 None。 如果加载器不能找到指定的模块，则引发 ImportError 异常。

在 3.4 版更改: 引发 ImportError 而不是 NotImplementedError。

is_package(fullname)

一个抽象方法，如果这个模块是一个包则返回真值，否则返回假值。 如果 loader 不能找到这个模块，则引发 ImportError。

在 3.4 版更改: 引发 ImportError 而不是 NotImplementedError。

static source_to_code(data, path='<string>')

创建一个来自Python源码的代码对象。

参数 data 可以是任意 compile() 函数支持的类型（例如字符串或字节串）。 参数 path 应该是源代码来源的路径，这可能是一个抽象概念（例如位于一个 zip 文件中）。

在有后续代码对象的情况下，可以在一个模块中通过运行``exec(code, module.__dict__)``来执行它。

3.4 新版功能.

在 3.5 版更改: 使得这个方法变成静态的。

exec_module(module)

Loader.exec_module() 的实现。

3.4 新版功能.

load_module(fullname)

Loader.load_module() 的实现。

3.4 版后已移除: 使用 exec_module() 来代替。

class importlib.abc.ExecutionLoader

一个继承自 InspectLoader 的抽象基类，当被实现时，帮助一个模块作为脚本来执行。 这个抽象基类表示可选的 PEP 302 [https://www.python.org/dev/peps/pep-0302] 协议。

abstractmethod get_filename(fullname)

一个用来为指定模块返回 __file__ 的值的抽象方法。如果无路径可用，则引发 ImportError。

如果源代码可用，那么这个方法返回源文件的路径，不管是否是用来加载模块的字节码。

在 3.4 版更改: 引发 ImportError 而不是 NotImplementedError。

class importlib.abc.FileLoader(fullname, path)

一个继承自 ResourceLoader 和 ExecutionLoader，提供 ResourceLoader.get_data() 和 ExecutionLoader.get_filename() 具体实现的抽象基类。

参数*fullname*是加载器要处理的模块的完全解析的名字。参数*path*是模块文件的路径。

3.3 新版功能.

name

加载器可以处理的模块的名字。

path

模块的文件路径

load_module(fullname)

调用super的``load_module()``。

3.4 版后已移除: 使用 Loader.exec_module() 来代替。

abstractmethod get_filename(fullname)

返回 path。

abstractmethod get_data(path)

读取 path 作为二进制文件并且返回来自它的字节数据。

class importlib.abc.SourceLoader

一个用于实现源文件（和可选地字节码）加载的抽象基类。这个类继承自 ResourceLoader 和 ExecutionLoader，需要实现：

ResourceLoader.get_data()

ExecutionLoader.get_filename()

应该是只返回源文件的路径；不支持无源加载。

由这个类定义的抽象方法用来添加可选的字节码文件支持。不实现这些可选的方法（或导致它们引发 NotImplementedError 异常）导致这个加载器只能与源代码一起工作。 实现这些方法允许加载器能与源 和 字节码文件一起工作。不允许只提供字节码的 无源式 加载。字节码文件是通过移除 Python 编译器的解析步骤来加速加载的优化，并且因此没有开放出字节码专用的 API。

path_stats(path)

返回一个包含关于指定路径的元数据的 dict 的可选的抽象方法。 支持的字典键有：

'mtime' (必选项): 一个表示源码修改时间的整数或浮点数；

'size' (可选项)：源码的字节大小。

字典中任何其他键会被忽略，以允许将来的扩展。 如果不能处理该路径，则会引发 OSError。

3.3 新版功能.

在 3.4 版更改: 引发 OSError 而不是 NotImplemented。

path_mtime(path)

返回指定文件路径修改时间的可选的抽象方法。

3.3 版后已移除: 在有了 path_stats() 的情况下，这个方法被弃用了。 没必要去实现它了，但是为了兼容性，它依然处于可用状态。 如果文件路径不能被处理，则引发 OSError 异常。

在 3.4 版更改: 引发 OSError 而不是 NotImplemented。

set_data(path, data)

往一个文件路径写入指定字节的的可选的抽象方法。任何中间不存在的目录不会被自动创建。

由于路径是只读的，当写入的路径产生错误时（errno.EACCES/PermissionError），不会传播异常。

在 3.4 版更改: 当被调用时，不再引起 NotImplementedError 异常。

get_code(fullname)

InspectLoader.get_code() 的具体实现。

exec_module(module)

Loader.exec_module() 的具体实现。

3.4 新版功能.

load_module(fullname)

Concrete implementation of Loader.load_module().

3.4 版后已移除: 使用 exec_module() 来代替。

get_source(fullname)

InspectLoader.get_source() 的具体实现。

is_package(fullname)

InspectLoader.is_package() 的具体实现。一个模块被确定为一个包的条件是：它的文件路径（由 ExecutionLoader.get_filename() 提供）当文件扩展名被移除时是一个命名为 __init__ 的文件，并且 这个模块名字本身不是以``__init__``结束。

31.5.4. importlib.machinery – Importers and path hooks

Source code: Lib/importlib/machinery.py [https://github.com/python/cpython/tree/3.6/Lib/importlib/machinery.py]

This module contains the various objects that help import find and load modules.

importlib.machinery.SOURCE_SUFFIXES

A list of strings representing the recognized file suffixes for source modules.

3.3 新版功能.

importlib.machinery.DEBUG_BYTECODE_SUFFIXES

A list of strings representing the file suffixes for non-optimized bytecode modules.

3.3 新版功能.

3.5 版后已移除: Use BYTECODE_SUFFIXES instead.

importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES

A list of strings representing the file suffixes for optimized bytecode modules.

3.3 新版功能.

3.5 版后已移除: Use BYTECODE_SUFFIXES instead.

importlib.machinery.BYTECODE_SUFFIXES

A list of strings representing the recognized file suffixes for bytecode modules (including the leading dot).

3.3 新版功能.

在 3.5 版更改: The value is no longer dependent on __debug__.

importlib.machinery.EXTENSION_SUFFIXES

A list of strings representing the recognized file suffixes for extension modules.

3.3 新版功能.

importlib.machinery.all_suffixes()

Returns a combined list of strings representing all file suffixes for modules recognized by the standard import machinery. This is a helper for code which simply needs to know if a filesystem path potentially refers to a module without needing any details on the kind of module (for example, inspect.getmodulename()).

3.3 新版功能.

class importlib.machinery.BuiltinImporter

An importer for built-in modules. All known built-in modules are listed in sys.builtin_module_names. This class implements the importlib.abc.MetaPathFinder and importlib.abc.InspectLoader ABCs.

Only class methods are defined by this class to alleviate the need for instantiation.

在 3.5 版更改: As part of PEP 489 [https://www.python.org/dev/peps/pep-0489], the builtin importer now implements Loader.create_module() and Loader.exec_module()

class importlib.machinery.FrozenImporter

An importer for frozen modules. This class implements the importlib.abc.MetaPathFinder and importlib.abc.InspectLoader ABCs.

Only class methods are defined by this class to alleviate the need for instantiation.

class importlib.machinery.WindowsRegistryFinder

Finder for modules declared in the Windows registry. This class implements the importlib.abc.MetaPathFinder ABC.

Only class methods are defined by this class to alleviate the need for instantiation.

3.3 新版功能.

3.6 版后已移除: Use site configuration instead. Future versions of Python may not enable this finder by default.

class importlib.machinery.PathFinder

A Finder for sys.path and package __path__ attributes. This class implements the importlib.abc.MetaPathFinder ABC.

Only class methods are defined by this class to alleviate the need for instantiation.

classmethod find_spec(fullname, path=None, target=None)

Class method that attempts to find a spec for the module specified by fullname on sys.path or, if defined, on path. For each path entry that is searched, sys.path_importer_cache is checked. If a non-false object is found then it is used as the path entry finder to look for the module being searched for. If no entry is found in sys.path_importer_cache, then sys.path_hooks is searched for a finder for the path entry and, if found, is stored in sys.path_importer_cache along with being queried about the module. If no finder is ever found then None is both stored in the cache and returned.

3.4 新版功能.

在 3.5 版更改: If the current working directory – represented by an empty string – is no longer valid then None is returned but no value is cached in sys.path_importer_cache.

classmethod find_module(fullname, path=None)

A legacy wrapper around find_spec().

3.4 版后已移除: 使用 find_spec() 来代替。

classmethod invalidate_caches()

Calls importlib.abc.PathEntryFinder.invalidate_caches() on all finders stored in sys.path_importer_cache.

在 3.4 版更改: Calls objects in sys.path_hooks with the current working directory for '' (i.e. the empty string).

class importlib.machinery.FileFinder(path, *loader_details)

A concrete implementation of importlib.abc.PathEntryFinder which caches results from the file system.

The path argument is the directory for which the finder is in charge of searching.

The loader_details argument is a variable number of 2-item tuples each containing a loader and a sequence of file suffixes the loader recognizes. The loaders are expected to be callables which accept two arguments of the module’s name and the path to the file found.

The finder will cache the directory contents as necessary, making stat calls for each module search to verify the cache is not outdated. Because cache staleness relies upon the granularity of the operating system’s state information of the file system, there is a potential race condition of searching for a module, creating a new file, and then searching for the module the new file represents. If the operations happen fast enough to fit within the granularity of stat calls, then the module search will fail. To prevent this from happening, when you create a module dynamically, make sure to call importlib.invalidate_caches().

3.3 新版功能.

path

The path the finder will search in.

find_spec(fullname, target=None)

Attempt to find the spec to handle fullname within path.

3.4 新版功能.

find_loader(fullname)

Attempt to find the loader to handle fullname within path.

invalidate_caches()

Clear out the internal cache.

classmethod path_hook(*loader_details)

A class method which returns a closure for use on sys.path_hooks. An instance of FileFinder is returned by the closure using the path argument given to the closure directly and loader_details indirectly.

If the argument to the closure is not an existing directory, ImportError is raised.

class importlib.machinery.SourceFileLoader(fullname, path)

A concrete implementation of importlib.abc.SourceLoader by subclassing importlib.abc.FileLoader and providing some concrete implementations of other methods.

3.3 新版功能.

name

The name of the module that this loader will handle.

path

The path to the source file.

is_package(fullname)

Return true if path appears to be for a package.

path_stats(path)

Concrete implementation of importlib.abc.SourceLoader.path_stats().

set_data(path, data)

Concrete implementation of importlib.abc.SourceLoader.set_data().

load_module(name=None)

Concrete implementation of importlib.abc.Loader.load_module() where specifying the name of the module to load is optional.

3.6 版后已移除: Use importlib.abc.Loader.exec_module() instead.

class importlib.machinery.SourcelessFileLoader(fullname, path)

A concrete implementation of importlib.abc.FileLoader which can import bytecode files (i.e. no source code files exist).

Please note that direct use of bytecode files (and thus not source code files) inhibits your modules from being usable by all Python implementations or new versions of Python which change the bytecode format.

3.3 新版功能.

name

The name of the module the loader will handle.

path

The path to the bytecode file.

is_package(fullname)

Determines if the module is a package based on path.

get_code(fullname)

Returns the code object for name created from path.

get_source(fullname)

Returns None as bytecode files have no source when this loader is used.

load_module(name=None)

Concrete implementation of importlib.abc.Loader.load_module() where specifying the name of the module to load is optional.

3.6 版后已移除: Use importlib.abc.Loader.exec_module() instead.

class importlib.machinery.ExtensionFileLoader(fullname, path)

A concrete implementation of importlib.abc.ExecutionLoader for extension modules.

The fullname argument specifies the name of the module the loader is to support. The path argument is the path to the extension module’s file.

3.3 新版功能.

name

Name of the module the loader supports.

path

Path to the extension module.

create_module(spec)

Creates the module object from the given specification in accordance with PEP 489 [https://www.python.org/dev/peps/pep-0489].

3.5 新版功能.

exec_module(module)

Initializes the given module object in accordance with PEP 489 [https://www.python.org/dev/peps/pep-0489].

3.5 新版功能.

is_package(fullname)

Returns True if the file path points to a package’s __init__ module based on EXTENSION_SUFFIXES.

get_code(fullname)

Returns None as extension modules lack a code object.

get_source(fullname)

Returns None as extension modules do not have source code.

get_filename(fullname)

返回 path。

3.4 新版功能.

class importlib.machinery.ModuleSpec(name, loader, *, origin=None, loader_state=None, is_package=None)

A specification for a module’s import-system-related state. This is typically exposed as the module’s __spec__ attribute. In the descriptions below, the names in parentheses give the corresponding attribute available directly on the module object. E.g. module.__spec__.origin == module.__file__. Note however that while the values are usually equivalent, they can differ since there is no synchronization between the two objects. Thus it is possible to update the module’s __path__ at runtime, and this will not be automatically reflected in __spec__.submodule_search_locations.

3.4 新版功能.

name

(__name__)

A string for the fully-qualified name of the module.

loader

(__loader__)

The loader to use for loading. For namespace packages this should be set to None.

origin

(__file__)

Name of the place from which the module is loaded, e.g. “builtin” for built-in modules and the filename for modules loaded from source. Normally “origin” should be set, but it may be None (the default) which indicates it is unspecified.

submodule_search_locations

(__path__)

List of strings for where to find submodules, if a package (None otherwise).

loader_state

Container of extra module-specific data for use during loading (or None).

cached

(__cached__)

String for where the compiled module should be stored (or None).

parent

(__package__)

(Read-only) Fully-qualified name of the package to which the module belongs as a submodule (or None).

has_location

Boolean indicating whether or not the module’s “origin” attribute refers to a loadable location.

31.5.5. importlib.util – Utility code for importers

Source code: Lib/importlib/util.py [https://github.com/python/cpython/tree/3.6/Lib/importlib/util.py]

This module contains the various objects that help in the construction of an importer.

importlib.util.MAGIC_NUMBER

The bytes which represent the bytecode version number. If you need help with loading/writing bytecode then consider importlib.abc.SourceLoader.

3.4 新版功能.

importlib.util.cache_from_source(path, debug_override=None, *, optimization=None)

Return the PEP 3147 [https://www.python.org/dev/peps/pep-3147]/PEP 488 [https://www.python.org/dev/peps/pep-0488] path to the byte-compiled file associated with the source path. For example, if path is /foo/bar/baz.py the return value would be /foo/bar/__pycache__/baz.cpython-32.pyc for Python 3.2. The cpython-32 string comes from the current magic tag (see get_tag(); if sys.implementation.cache_tag is not defined then NotImplementedError will be raised).

The optimization parameter is used to specify the optimization level of the bytecode file. An empty string represents no optimization, so /foo/bar/baz.py with an optimization of '' will result in a bytecode path of /foo/bar/__pycache__/baz.cpython-32.pyc. None causes the interpter’s optimization level to be used. Any other value’s string representation being used, so /foo/bar/baz.py with an optimization of 2 will lead to the bytecode path of /foo/bar/__pycache__/baz.cpython-32.opt-2.pyc. The string representation of optimization can only be alphanumeric, else ValueError is raised.

The debug_override parameter is deprecated and can be used to override the system’s value for __debug__. A True value is the equivalent of setting optimization to the empty string. A False value is the same as setting optimization to 1. If both debug_override an optimization are not None then TypeError is raised.

3.4 新版功能.

在 3.5 版更改: The optimization parameter was added and the debug_override parameter was deprecated.

在 3.6 版更改: 接受一个 path-like object。

importlib.util.source_from_cache(path)

Given the path to a PEP 3147 [https://www.python.org/dev/peps/pep-3147] file name, return the associated source code file path. For example, if path is /foo/bar/__pycache__/baz.cpython-32.pyc the returned path would be /foo/bar/baz.py. path need not exist, however if it does not conform to PEP 3147 [https://www.python.org/dev/peps/pep-3147] or PEP 488 [https://www.python.org/dev/peps/pep-0488] format, a ValueError is raised. If sys.implementation.cache_tag is not defined, NotImplementedError is raised.

3.4 新版功能.

在 3.6 版更改: 接受一个 path-like object。

importlib.util.decode_source(source_bytes)

Decode the given bytes representing source code and return it as a string with universal newlines (as required by importlib.abc.InspectLoader.get_source()).

3.4 新版功能.

importlib.util.resolve_name(name, package)

Resolve a relative module name to an absolute one.

If name has no leading dots, then name is simply returned. This allows for usage such as importlib.util.resolve_name('sys', __package__) without doing a check to see if the package argument is needed.

ValueError is raised if name is a relative module name but package is a false value (e.g. None or the empty string). ValueError is also raised a relative name would escape its containing package (e.g. requesting ..bacon from within the spam package).

3.3 新版功能.

importlib.util.find_spec(name, package=None)

Find the spec for a module, optionally relative to the specified package name. If the module is in sys.modules, then sys.modules[name].__spec__ is returned (unless the spec would be None or is not set, in which case ValueError is raised). Otherwise a search using sys.meta_path is done. None is returned if no spec is found.

If name is for a submodule (contains a dot), the parent module is automatically imported.

name and package work the same as for import_module().

3.4 新版功能.

importlib.util.module_from_spec(spec)

Create a new module based on spec and spec.loader.create_module.

If spec.loader.create_module does not return None, then any pre-existing attributes will not be reset. Also, no AttributeError will be raised if triggered while accessing spec or setting an attribute on the module.

This function is preferred over using types.ModuleType to create a new module as spec is used to set as many import-controlled attributes on the module as possible.

3.5 新版功能.

@importlib.util.module_for_loader

A decorator for importlib.abc.Loader.load_module() to handle selecting the proper module object to load with. The decorated method is expected to have a call signature taking two positional arguments (e.g. load_module(self, module)) for which the second argument will be the module object to be used by the loader. Note that the decorator will not work on static methods because of the assumption of two arguments.

The decorated method will take in the name of the module to be loaded as expected for a loader. If the module is not found in sys.modules then a new one is constructed. Regardless of where the module came from, __loader__ set to self and __package__ is set based on what importlib.abc.InspectLoader.is_package() returns (if available). These attributes are set unconditionally to support reloading.

If an exception is raised by the decorated method and a module was added to sys.modules, then the module will be removed to prevent a partially initialized module from being in left in sys.modules. If the module was already in sys.modules then it is left alone.

在 3.3 版更改: __loader__ and __package__ are automatically set (when possible).

在 3.4 版更改: Set __name__, __loader__ __package__ unconditionally to support reloading.

3.4 版后已移除: The import machinery now directly performs all the functionality provided by this function.

@importlib.util.set_loader

A decorator for importlib.abc.Loader.load_module() to set the __loader__ attribute on the returned module. If the attribute is already set the decorator does nothing. It is assumed that the first positional argument to the wrapped method (i.e. self) is what __loader__ should be set to.

在 3.4 版更改: Set __loader__ if set to None, as if the attribute does not exist.

3.4 版后已移除: The import machinery takes care of this automatically.

@importlib.util.set_package

A decorator for importlib.abc.Loader.load_module() to set the __package__ attribute on the returned module. If __package__ is set and has a value other than None it will not be changed.

3.4 版后已移除: The import machinery takes care of this automatically.

importlib.util.spec_from_loader(name, loader, *, origin=None, is_package=None)

A factory function for creating a ModuleSpec instance based on a loader. The parameters have the same meaning as they do for ModuleSpec. The function uses available loader APIs, such as InspectLoader.is_package(), to fill in any missing information on the spec.

3.4 新版功能.

importlib.util.spec_from_file_location(name, location, *, loader=None, submodule_search_locations=None)

A factory function for creating a ModuleSpec instance based on the path to a file. Missing information will be filled in on the spec by making use of loader APIs and by the implication that the module will be file-based.

3.4 新版功能.

在 3.6 版更改: 接受一个 path-like object。

class importlib.util.LazyLoader(loader)

A class which postpones the execution of the loader of a module until the module has an attribute accessed.

This class only works with loaders that define exec_module() as control over what module type is used for the module is required. For those same reasons, the loader’s create_module() method must return None or a type for which its __class__ attribute can be mutated along with not using slots. Finally, modules which substitute the object placed into sys.modules will not work as there is no way to properly replace the module references throughout the interpreter safely; ValueError is raised if such a substitution is detected.

注解

For projects where startup time is critical, this class allows for potentially minimizing the cost of loading a module if it is never used. For projects where startup time is not essential then use of this class is heavily discouraged due to error messages created during loading being postponed and thus occurring out of context.

3.5 新版功能.

在 3.6 版更改: Began calling create_module(), removing the compatibility warning for importlib.machinery.BuiltinImporter and importlib.machinery.ExtensionFileLoader.

classmethod factory(loader)

A static method which returns a callable that creates a lazy loader. This is meant to be used in situations where the loader is passed by class instead of by instance.

suffixes = importlib.machinery.SOURCE_SUFFIXES
loader = importlib.machinery.SourceFileLoader
lazy_loader = importlib.util.LazyLoader.factory(loader)
finder = importlib.machinery.FileFinder(path, (lazy_loader, suffixes))

31.5.6. 例子

31.5.6.1. Importing programmatically

To programmatically import a module, use importlib.import_module().

import importlib

itertools = importlib.import_module('itertools')

31.5.6.2. Checking if a module can be imported

If you need to find out if a module can be imported without actually doing the import, then you should use importlib.util.find_spec().

import importlib.util
import sys

# For illustrative purposes.
name = 'itertools'

spec = importlib.util.find_spec(name)
if spec is None:
    print("can't find the itertools module")
else:
    # If you chose to perform the actual import ...
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    # Adding the module to sys.modules is optional.
    sys.modules[name] = module

31.5.6.3. Importing a source file directly

To import a Python source file directly, use the following recipe (Python 3.5 and newer only):

import importlib.util
import sys

# For illustrative purposes.
import tokenize
file_path = tokenize.__file__
module_name = tokenize.__name__

spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
# Optional; only necessary if you want to be able to import the module
# by name later.
sys.modules[module_name] = module

31.5.6.4. Setting up an importer

For deep customizations of import, you typically want to implement an importer. This means managing both the finder and loader side of things. For finders there are two flavours to choose from depending on your needs: a meta path finder or a path entry finder. The former is what you would put on sys.meta_path while the latter is what you create using a path entry hook on sys.path_hooks which works with sys.path entries to potentially create a finder. This example will show you how to register your own importers so that import will use them (for creating an importer for yourself, read the documentation for the appropriate classes defined within this package):

import importlib.machinery
import sys

# For illustrative purposes only.
SpamMetaPathFinder = importlib.machinery.PathFinder
SpamPathEntryFinder = importlib.machinery.FileFinder
loader_details = (importlib.machinery.SourceFileLoader,
                  importlib.machinery.SOURCE_SUFFIXES)

# Setting up a meta path finder.
# Make sure to put the finder in the proper location in the list in terms of
# priority.
sys.meta_path.append(SpamMetaPathFinder)

# Setting up a path entry finder.
# Make sure to put the path hook in the proper location in the list in terms
# of priority.
sys.path_hooks.append(SpamPathEntryFinder.path_hook(loader_details))

31.5.6.5. Approximating importlib.import_module()

Import itself is implemented in Python code, making it possible to expose most of the import machinery through importlib. The following helps illustrate the various APIs that importlib exposes by providing an approximate implementation of importlib.import_module() (Python 3.4 and newer for the importlib usage, Python 3.6 and newer for other parts of the code).

import importlib.util
import sys

def import_module(name, package=None):
    """An approximate implementation of import."""
    absolute_name = importlib.util.resolve_name(name, package)
    try:
        return sys.modules[absolute_name]
    except KeyError:
        pass

    path = None
    if '.' in absolute_name:
        parent_name, _, child_name = absolute_name.rpartition('.')
        parent_module = import_module(parent_name)
        path = parent_module.__spec__.submodule_search_locations
    for finder in sys.meta_path:
        spec = finder.find_spec(absolute_name, path)
        if spec is not None:
            break
    else:
        msg = f'No module named {absolute_name!r}'
        raise ModuleNotFoundError(msg, name=absolute_name)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    sys.modules[absolute_name] = module
    if path is not None:
        setattr(parent_module, child_name, module)
    return module

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

31. 导入模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

32. Python 语言服务

Python 提供了许多模块来帮助使用 Python 语言。 这些模块支持标记化、解析、语法分析、字节码反汇编以及各种其他工具。

这些模块包括：

32.1. parser — 访问 Python 解析树

32.1.1. 创建 ST 对象

32.1.2. 转换 ST 对象

32.1.3. Queries on ST Objects

32.1.4. 异常和错误处理

32.1.5. ST 对象

32.1.6. 示例: compile() 的模拟

32.2. ast — 抽象语法树

32.2.1. 节点类

32.2.2. 抽象文法

32.2.3. ast 中的辅助函数

32.3. symtable — Access to the compiler’s symbol tables

32.3.1. Generating Symbol Tables

32.3.2. Examining Symbol Tables

32.4. symbol — 与 Python 解析树一起使用的常量

32.5. token — 与Python解析树一起使用的常量

32.6. keyword — 检验Python关键字

32.7. tokenize – 对 Python 代码使用的标记解析器

32.7.1. 对输入进行解析标记

32.7.2. Command-Line Usage

32.7.3. 例子

32.8. tabnanny — 模糊缩进检测

32.9. pyclbr — Python class browser support

32.9.1. 类对象

32.9.2. 函数对象

32.10. py_compile — 编译 Python 源文件

32.11. compileall — Byte-compile Python libraries

32.11.1. Command-line use

32.11.2. Public functions

32.12. dis — Python 字节码反汇编器

32.12.1. 字节码分析

32.12.2. 分析函数

32.12.3. Python字节码说明

32.12.4. 操作码集合

32.13. pickletools — pickle 开发者工具集

32.13.1. 命令行语法

32.13.1.1. 命令行选项

32.13.2. 编程接口

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

32.1. parser — 访问 Python 解析树

The parser module provides an interface to Python’s internal parser and byte-code compiler. The primary purpose for this interface is to allow Python code to edit the parse tree of a Python expression and create executable code from this. This is better than trying to parse and modify an arbitrary Python code fragment as a string because parsing is performed in a manner identical to the code forming the application. It is also faster.

注解

From Python 2.5 onward, it’s much more convenient to cut in at the Abstract Syntax Tree (AST) generation and compilation stage, using the ast module.

There are a few things to note about this module which are important to making use of the data structures created. This is not a tutorial on editing the parse trees for Python code, but some examples of using the parser module are presented.

Most importantly, a good understanding of the Python grammar processed by the internal parser is required. For full information on the language syntax, refer to Python语言参考. The parser itself is created from a grammar specification defined in the file Grammar/Grammar in the standard Python distribution. The parse trees stored in the ST objects created by this module are the actual output from the internal parser when created by the expr() or suite() functions, described below. The ST objects created by sequence2st() faithfully simulate those structures. Be aware that the values of the sequences which are considered “correct” will vary from one version of Python to another as the formal grammar for the language is revised. However, transporting code from one Python version to another as source text will always allow correct parse trees to be created in the target version, with the only restriction being that migrating to an older version of the interpreter will not support more recent language constructs. The parse trees are not typically compatible from one version to another, whereas source code has always been forward-compatible.

Each element of the sequences returned by st2list() or st2tuple() has a simple form. Sequences representing non-terminal elements in the grammar always have a length greater than one. The first element is an integer which identifies a production in the grammar. These integers are given symbolic names in the C header file Include/graminit.h and the Python module symbol. Each additional element of the sequence represents a component of the production as recognized in the input string: these are always sequences which have the same form as the parent. An important aspect of this structure which should be noted is that keywords used to identify the parent node type, such as the keyword if in an if_stmt, are included in the node tree without any special treatment. For example, the if keyword is represented by the tuple (1, 'if'), where 1 is the numeric value associated with all NAME tokens, including variable and function names defined by the user. In an alternate form returned when line number information is requested, the same token might be represented as (1, 'if', 12), where the 12 represents the line number at which the terminal symbol was found.

Terminal elements are represented in much the same way, but without any child elements and the addition of the source text which was identified. The example of the if keyword above is representative. The various types of terminal symbols are defined in the C header file Include/token.h and the Python module token.

The ST objects are not required to support the functionality of this module, but are provided for three purposes: to allow an application to amortize the cost of processing complex parse trees, to provide a parse tree representation which conserves memory space when compared to the Python list or tuple representation, and to ease the creation of additional modules in C which manipulate parse trees. A simple “wrapper” class may be created in Python to hide the use of ST objects.

The parser module defines functions for a few distinct purposes. The most important purposes are to create ST objects and to convert ST objects to other representations such as parse trees and compiled code objects, but there are also functions which serve to query the type of parse tree represented by an ST object.

参见

模块 symbol

代表解析树内部节点的有用常量。

模块 token

代表解析树叶子节点和测试节点值的函数的有用常量。

32.1.1. 创建 ST 对象

ST objects may be created from source code or from a parse tree. When creating an ST object from source, different functions are used to create the 'eval' and 'exec' forms.

parser.expr(source)

The expr() function parses the parameter source as if it were an input to compile(source, 'file.py', 'eval'). If the parse succeeds, an ST object is created to hold the internal parse tree representation, otherwise an appropriate exception is raised.

parser.suite(source)

The suite() function parses the parameter source as if it were an input to compile(source, 'file.py', 'exec'). If the parse succeeds, an ST object is created to hold the internal parse tree representation, otherwise an appropriate exception is raised.

parser.sequence2st(sequence)

This function accepts a parse tree represented as a sequence and builds an internal representation if possible. If it can validate that the tree conforms to the Python grammar and all nodes are valid node types in the host version of Python, an ST object is created from the internal representation and returned to the called. If there is a problem creating the internal representation, or if the tree cannot be validated, a ParserError exception is raised. An ST object created this way should not be assumed to compile correctly; normal exceptions raised by compilation may still be initiated when the ST object is passed to compilest(). This may indicate problems not related to syntax (such as a MemoryError exception), but may also be due to constructs such as the result of parsing del f(0), which escapes the Python parser but is checked by the bytecode compiler.

Sequences representing terminal tokens may be represented as either two-element lists of the form (1, 'name') or as three-element lists of the form (1, 'name', 56). If the third element is present, it is assumed to be a valid line number. The line number may be specified for any subset of the terminal symbols in the input tree.

parser.tuple2st(sequence)

This is the same function as sequence2st(). This entry point is maintained for backward compatibility.

32.1.2. 转换 ST 对象

ST objects, regardless of the input used to create them, may be converted to parse trees represented as list- or tuple- trees, or may be compiled into executable code objects. Parse trees may be extracted with or without line numbering information.

parser.st2list(st, line_info=False, col_info=False)

This function accepts an ST object from the caller in st and returns a Python list representing the equivalent parse tree. The resulting list representation can be used for inspection or the creation of a new parse tree in list form. This function does not fail so long as memory is available to build the list representation. If the parse tree will only be used for inspection, st2tuple() should be used instead to reduce memory consumption and fragmentation. When the list representation is required, this function is significantly faster than retrieving a tuple representation and converting that to nested lists.

If line_info is true, line number information will be included for all terminal tokens as a third element of the list representing the token. Note that the line number provided specifies the line on which the token ends. This information is omitted if the flag is false or omitted.

parser.st2tuple(st, line_info=False, col_info=False)

This function accepts an ST object from the caller in st and returns a Python tuple representing the equivalent parse tree. Other than returning a tuple instead of a list, this function is identical to st2list().

If line_info is true, line number information will be included for all terminal tokens as a third element of the list representing the token. This information is omitted if the flag is false or omitted.

parser.compilest(st, filename='<syntax-tree>')

The Python byte compiler can be invoked on an ST object to produce code objects which can be used as part of a call to the built-in exec() or eval() functions. This function provides the interface to the compiler, passing the internal parse tree from st to the parser, using the source file name specified by the filename parameter. The default value supplied for filename indicates that the source was an ST object.

Compiling an ST object may result in exceptions related to compilation; an example would be a SyntaxError caused by the parse tree for del f(0): this statement is considered legal within the formal grammar for Python but is not a legal language construct. The SyntaxError raised for this condition is actually generated by the Python byte-compiler normally, which is why it can be raised at this point by the parser module. Most causes of compilation failure can be diagnosed programmatically by inspection of the parse tree.

32.1.3. Queries on ST Objects

Two functions are provided which allow an application to determine if an ST was created as an expression or a suite. Neither of these functions can be used to determine if an ST was created from source code via expr() or suite() or from a parse tree via sequence2st().

parser.isexpr(st)

When st represents an 'eval' form, this function returns true, otherwise it returns false. This is useful, since code objects normally cannot be queried for this information using existing built-in functions. Note that the code objects created by compilest() cannot be queried like this either, and are identical to those created by the built-in compile() function.

parser.issuite(st)

This function mirrors isexpr() in that it reports whether an ST object represents an 'exec' form, commonly known as a “suite.” It is not safe to assume that this function is equivalent to not isexpr(st), as additional syntactic fragments may be supported in the future.

32.1.4. 异常和错误处理

The parser module defines a single exception, but may also pass other built-in exceptions from other portions of the Python runtime environment. See each function for information about the exceptions it can raise.

exception parser.ParserError

Exception raised when a failure occurs within the parser module. This is generally produced for validation failures rather than the built-in SyntaxError raised during normal parsing. The exception argument is either a string describing the reason of the failure or a tuple containing a sequence causing the failure from a parse tree passed to sequence2st() and an explanatory string. Calls to sequence2st() need to be able to handle either type of exception, while calls to other functions in the module will only need to be aware of the simple string values.

Note that the functions compilest(), expr(), and suite() may raise exceptions which are normally raised by the parsing and compilation process. These include the built in exceptions MemoryError, OverflowError, SyntaxError, and SystemError. In these cases, these exceptions carry all the meaning normally associated with them. Refer to the descriptions of each function for detailed information.

32.1.5. ST 对象

Ordered and equality comparisons are supported between ST objects. Pickling of ST objects (using the pickle module) is also supported.

parser.STType

The type of the objects returned by expr(), suite() and sequence2st().

ST 对象具有以下方法:

ST.compile(filename='<syntax-tree>')

和 compilest(st, filename) 相同.

ST.isexpr()

和 isexpr(st) 相同。

ST.issuite()

和 issuite(st) 相同。

ST.tolist(line_info=False, col_info=False)

和 st2list(st, line_info, col_info) 相同。

ST.totuple(line_info=False, col_info=False)

和 st2tuple(st, line_info, col_info) 相同。

32.1.6. 示例: compile() 的模拟

While many useful operations may take place between parsing and bytecode generation, the simplest operation is to do nothing. For this purpose, using the parser module to produce an intermediate data structure is equivalent to the code

>>> code = compile('a + 5', 'file.py', 'eval')
>>> a = 5
>>> eval(code)
10

The equivalent operation using the parser module is somewhat longer, and allows the intermediate internal parse tree to be retained as an ST object:

>>> import parser
>>> st = parser.expr('a + 5')
>>> code = st.compile('file.py')
>>> a = 5
>>> eval(code)
10

An application which needs both ST and code objects can package this code into readily available functions:

import parser

def load_suite(source_string):
    st = parser.suite(source_string)
    return st, st.compile()

def load_expression(source_string):
    st = parser.expr(source_string)
    return st, st.compile()

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

32.2. ast — 抽象语法树

源代码： Lib/ast.py [https://github.com/python/cpython/tree/3.6/Lib/ast.py]

ast 模块帮助 Python 程序处理 Python 语法的抽象语法树。抽象语法或许会随着 Python 的更新发布而改变；该模块能够帮助理解当前语法在编程层面的样貌。

抽象语法树可通过将 ast.PyCF_ONLY_AST 作为旗标传递给 compile() 内置函数来生成，或是使用此模块中提供的 parse() 辅助函数。返回结果将是一个对象树，，其中的类都继承自 ast.AST。抽象语法树可被内置的 compile() 函数编译为一个 Python 代码对象。

32.2.1. 节点类

class ast.AST

这是所有 AST 节点类的基类。实际上，这些节点类派生自 Parser/Python.asdl 文件，其中定义的语法树示例 如下。它们在 C 语言模块 _ast 中定义，并被导出至 ast 模块。

抽象语法定义的每个左侧符号(比方说， ast.stmt 或者 ast.expr)定义了一个类。另外，在抽象语法定义的右侧，对每一个构造器也定义了一个类；这些类继承自树左侧的类。比如，ast.BinOp 继承自 ast.expr。对于多分支产生式(也就是”和规则”)，树右侧的类是抽象的；只有特定构造器结点的实例能被构造。

_fields

每个具体类都有个属性 _fields, 用来给出所有子节点的名字。

每个具体类的实例对它每个子节点都有一个属性，对应类型如文法中所定义。比如，ast.BinOp 的实例有个属性 left，类型是 ast.expr.

如果这些属性在文法中标记为可选（使用问号），对应值可能会是 None。如果这些属性有零或多个（用星号标记），对应值会用Python的列表来表示。所有可能的属性必须在用 compile() 编译得到AST时给出，且是有效的值。

lineno

col_offset

ast.expr 和 ast.stmt 子类的实例有 lineno 和 col_offset 属性。lineno 是源代码的行数(从1开始, 所以第一行行数是1)，而 col_offset 是该生成节点第一个token的UTF-8字节偏移量。记录下UTF-8偏移量的原因是parser内部使用UTF-8。

一个类的构造器 ast.T 像下面这样parse它的参数。

如果有位置参数，它们必须和 T._fields 中的元素一样多；他们会像这些名字的属性一样被赋值。

如果有关键字参数，它们必须被设为和给定值同名的属性。

比方说，要创建和填充节点 ast.UnaryOp，你得用

node = ast.UnaryOp()
node.op = ast.USub()
node.operand = ast.Num()
node.operand.n = 5
node.operand.lineno = 0
node.operand.col_offset = 0
node.lineno = 0
node.col_offset = 0

或者更紧凑点

node = ast.UnaryOp(ast.USub(), ast.Num(5, lineno=0, col_offset=0),
                   lineno=0, col_offset=0)

32.2.2. 抽象文法

抽象文法目前定义如下

-- ASDL's 7 builtin types are:
-- identifier, int, string, bytes, object, singleton, constant
--
-- singleton: None, True or False
-- constant can be None, whereas None means "no value" for object.

module Python
{
    mod = Module(stmt* body)
        | Interactive(stmt* body)
        | Expression(expr body)

        -- not really an actual node but useful in Jython's typesystem.
        | Suite(stmt* body)

    stmt = FunctionDef(identifier name, arguments args,
                       stmt* body, expr* decorator_list, expr? returns)
          | AsyncFunctionDef(identifier name, arguments args,
                             stmt* body, expr* decorator_list, expr? returns)

          | ClassDef(identifier name,
             expr* bases,
             keyword* keywords,
             stmt* body,
             expr* decorator_list)
          | Return(expr? value)

          | Delete(expr* targets)
          | Assign(expr* targets, expr value)
          | AugAssign(expr target, operator op, expr value)
          -- 'simple' indicates that we annotate simple name without parens
          | AnnAssign(expr target, expr annotation, expr? value, int simple)

          -- use 'orelse' because else is a keyword in target languages
          | For(expr target, expr iter, stmt* body, stmt* orelse)
          | AsyncFor(expr target, expr iter, stmt* body, stmt* orelse)
          | While(expr test, stmt* body, stmt* orelse)
          | If(expr test, stmt* body, stmt* orelse)
          | With(withitem* items, stmt* body)
          | AsyncWith(withitem* items, stmt* body)

          | Raise(expr? exc, expr? cause)
          | Try(stmt* body, excepthandler* handlers, stmt* orelse, stmt* finalbody)
          | Assert(expr test, expr? msg)

          | Import(alias* names)
          | ImportFrom(identifier? module, alias* names, int? level)

          | Global(identifier* names)
          | Nonlocal(identifier* names)
          | Expr(expr value)
          | Pass | Break | Continue

          -- XXX Jython will be different
          -- col_offset is the byte offset in the utf8 string the parser uses
          attributes (int lineno, int col_offset)

          -- BoolOp() can use left & right?
    expr = BoolOp(boolop op, expr* values)
         | BinOp(expr left, operator op, expr right)
         | UnaryOp(unaryop op, expr operand)
         | Lambda(arguments args, expr body)
         | IfExp(expr test, expr body, expr orelse)
         | Dict(expr* keys, expr* values)
         | Set(expr* elts)
         | ListComp(expr elt, comprehension* generators)
         | SetComp(expr elt, comprehension* generators)
         | DictComp(expr key, expr value, comprehension* generators)
         | GeneratorExp(expr elt, comprehension* generators)
         -- the grammar constrains where yield expressions can occur
         | Await(expr value)
         | Yield(expr? value)
         | YieldFrom(expr value)
         -- need sequences for compare to distinguish between
         -- x < 4 < 3 and (x < 4) < 3
         | Compare(expr left, cmpop* ops, expr* comparators)
         | Call(expr func, expr* args, keyword* keywords)
         | Num(object n) -- a number as a PyObject.
         | Str(string s) -- need to specify raw, unicode, etc?
         | FormattedValue(expr value, int? conversion, expr? format_spec)
         | JoinedStr(expr* values)
         | Bytes(bytes s)
         | NameConstant(singleton value)
         | Ellipsis
         | Constant(constant value)

         -- the following expression can appear in assignment context
         | Attribute(expr value, identifier attr, expr_context ctx)
         | Subscript(expr value, slice slice, expr_context ctx)
         | Starred(expr value, expr_context ctx)
         | Name(identifier id, expr_context ctx)
         | List(expr* elts, expr_context ctx)
         | Tuple(expr* elts, expr_context ctx)

          -- col_offset is the byte offset in the utf8 string the parser uses
          attributes (int lineno, int col_offset)

    expr_context = Load | Store | Del | AugLoad | AugStore | Param

    slice = Slice(expr? lower, expr? upper, expr? step)
          | ExtSlice(slice* dims)
          | Index(expr value)

    boolop = And | Or

    operator = Add | Sub | Mult | MatMult | Div | Mod | Pow | LShift
                 | RShift | BitOr | BitXor | BitAnd | FloorDiv

    unaryop = Invert | Not | UAdd | USub

    cmpop = Eq | NotEq | Lt | LtE | Gt | GtE | Is | IsNot | In | NotIn

    comprehension = (expr target, expr iter, expr* ifs, int is_async)

    excepthandler = ExceptHandler(expr? type, identifier? name, stmt* body)
                    attributes (int lineno, int col_offset)

    arguments = (arg* args, arg? vararg, arg* kwonlyargs, expr* kw_defaults,
                 arg? kwarg, expr* defaults)

    arg = (identifier arg, expr? annotation)
           attributes (int lineno, int col_offset)

    -- keyword arguments supplied to call (NULL identifier for **kwargs)
    keyword = (identifier? arg, expr value)

    -- import name with optional 'as' alias.
    alias = (identifier name, identifier? asname)

    withitem = (expr context_expr, expr? optional_vars)
}

32.2.3. ast 中的辅助函数

除了节点类， ast 模块里为遍历抽象语法树定义了这些工具函数和类:

ast.parse(source, filename='<unknown>', mode='exec')

把源码解析为AST节点。和 compile(source, filename, mode,ast.PyCF_ONLY_AST) 等价。

警告

足够复杂或是巨大的字符串可能导致Python解释器的崩溃，因为Python的AST编译器是有栈深限制的。

ast.literal_eval(node_or_string)

对表达式节点以及包含Python字面量或容器的字符串进行安全的求值。传入的字符串或者节点里可能只包含下列的Python字面量结构: 字符串，字节对象(bytes)，数值，元组，列表，字典，集合，布尔值和 None。

This can be used for safely evaluating strings containing Python values from untrusted sources without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.

警告

足够复杂或是巨大的字符串可能导致Python解释器的崩溃，因为Python的AST编译器是有栈深限制的。

在 3.2 版更改: 目前支持字节和集合。

ast.get_docstring(node, clean=True)

Return the docstring of the given node (which must be a FunctionDef, ClassDef or Module node), or None if it has no docstring. If clean is true, clean up the docstring’s indentation with inspect.cleandoc().

ast.fix_missing_locations(node)

When you compile a node tree with compile(), the compiler expects lineno and col_offset attributes for every node that supports them. This is rather tedious to fill in for generated nodes, so this helper adds these attributes recursively where not already set, by setting them to the values of the parent node. It works recursively starting at node.

ast.increment_lineno(node, n=1)

Increment the line number of each node in the tree starting at node by n. This is useful to “move code” to a different location in a file.

ast.copy_location(new_node, old_node)

Copy source location (lineno and col_offset) from old_node to new_node if possible, and return new_node.

ast.iter_fields(node)

Yield a tuple of (fieldname, value) for each field in node._fields that is present on node.

ast.iter_child_nodes(node)

Yield all direct child nodes of node, that is, all fields that are nodes and all items of fields that are lists of nodes.

ast.walk(node)

Recursively yield all descendant nodes in the tree starting at node (including node itself), in no specified order. This is useful if you only want to modify nodes in place and don’t care about the context.

class ast.NodeVisitor

A node visitor base class that walks the abstract syntax tree and calls a visitor function for every node found. This function may return a value which is forwarded by the visit() method.

This class is meant to be subclassed, with the subclass adding visitor methods.

visit(node)

Visit a node. The default implementation calls the method called self.visit_classname where classname is the name of the node class, or generic_visit() if that method doesn’t exist.

generic_visit(node)

This visitor calls visit() on all children of the node.

Note that child nodes of nodes that have a custom visitor method won’t be visited unless the visitor calls generic_visit() or visits them itself.

Don’t use the NodeVisitor if you want to apply changes to nodes during traversal. For this a special visitor exists (NodeTransformer) that allows modifications.

class ast.NodeTransformer

子类 NodeVisitor 用于遍历抽象语法树，并允许修改节点。

NodeTransformer 将遍历抽象语法树并使用visitor方法的返回值去替换或移除旧节点。如果visitor方法的返回值为 None , 则该节点将从其位置移除，否则将替换为返回值。当返回值是原始节点时，无需替换。

如下是一个转换器示例，它将所有出现的名称 (foo) 重写为 data['foo']:

class RewriteName(NodeTransformer):

    def visit_Name(self, node):
        return copy_location(Subscript(
            value=Name(id='data', ctx=Load()),
            slice=Index(value=Str(s=node.id)),
            ctx=node.ctx
        ), node)

请记住，如果您正在操作的节点具有子节点，则必须先转换其子节点或为该节点调用 generic_visit() 方法。

对于属于语句集合（适用于所有语句节点）的节点，访问者还可以返回节点列表而不仅仅是单个节点。

通常你可以像这样使用转换器:

node = YourTransformer().visit(node)

ast.dump(node, annotate_fields=True, include_attributes=False)

Return a formatted dump of the tree in node. This is mainly useful for debugging purposes. The returned string will show the names and the values for fields. This makes the code impossible to evaluate, so if evaluation is wanted annotate_fields must be set to False. Attributes such as line numbers and column offsets are not dumped by default. If this is wanted, include_attributes can be set to True.

参见

Green Tree Snakes [https://greentreesnakes.readthedocs.org/], an external documentation resource, has good details on working with Python ASTs.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

32.3. symtable — Access to the compiler’s symbol tables

Source code: Lib/symtable.py [https://github.com/python/cpython/tree/3.6/Lib/symtable.py]

Symbol tables are generated by the compiler from AST just before bytecode is generated. The symbol table is responsible for calculating the scope of every identifier in the code. symtable provides an interface to examine these tables.

32.3.1. Generating Symbol Tables

symtable.symtable(code, filename, compile_type)

Return the toplevel SymbolTable for the Python source code. filename is the name of the file containing the code. compile_type is like the mode argument to compile().

32.3.2. Examining Symbol Tables

class symtable.SymbolTable

A namespace table for a block. The constructor is not public.

get_type()

Return the type of the symbol table. Possible values are 'class', 'module', and 'function'.

get_id()

Return the table’s identifier.

get_name()

Return the table’s name. This is the name of the class if the table is for a class, the name of the function if the table is for a function, or 'top' if the table is global (get_type() returns 'module').

get_lineno()

Return the number of the first line in the block this table represents.

is_optimized()

Return True if the locals in this table can be optimized.

is_nested()

Return True if the block is a nested class or function.

has_children()

Return True if the block has nested namespaces within it. These can be obtained with get_children().

has_exec()

Return True if the block uses exec.

get_identifiers()

Return a list of names of symbols in this table.

lookup(name)

Lookup name in the table and return a Symbol instance.

get_symbols()

Return a list of Symbol instances for names in the table.

get_children()

Return a list of the nested symbol tables.

class symtable.Function

A namespace for a function or method. This class inherits SymbolTable.

get_parameters()

Return a tuple containing names of parameters to this function.

get_locals()

Return a tuple containing names of locals in this function.

get_globals()

Return a tuple containing names of globals in this function.

get_frees()

Return a tuple containing names of free variables in this function.

class symtable.Class

A namespace of a class. This class inherits SymbolTable.

get_methods()

Return a tuple containing the names of methods declared in the class.

class symtable.Symbol

An entry in a SymbolTable corresponding to an identifier in the source. The constructor is not public.

get_name()

Return the symbol’s name.

is_referenced()

Return True if the symbol is used in its block.

is_imported()

Return True if the symbol is created from an import statement.

is_parameter()

Return True if the symbol is a parameter.

is_global()

Return True if the symbol is global.

is_declared_global()

Return True if the symbol is declared global with a global statement.

is_local()

Return True if the symbol is local to its block.

is_free()

Return True if the symbol is referenced in its block, but not assigned to.

is_assigned()

Return True if the symbol is assigned to in its block.

is_namespace()

Return True if name binding introduces new namespace.

If the name is used as the target of a function or class statement, this will be true.

例如

>>> table = symtable.symtable("def some_func(): pass", "string", "exec")
>>> table.lookup("some_func").is_namespace()
True

Note that a single name can be bound to multiple objects. If the result is True, the name may also be bound to other objects, like an int or list, that does not introduce a new namespace.

get_namespaces()

Return a list of namespaces bound to this name.

get_namespace()

Return the namespace bound to this name. If more than one namespace is bound, ValueError is raised.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

32.4. symbol — 与 Python 解析树一起使用的常量

源代码: Lib/symbol.py [https://github.com/python/cpython/tree/3.6/Lib/symbol.py]

此模块提供用于表示解析树内部节点数值的常量。 与大多数 Python 不同，这些常量使用小写字符名称。 请参阅 Python 发行版中的 Grammar/Grammar 文件来获取该语言语法上下文中对这些名称的定义。 这些名称所映射的特定数字值可能会在 Python 版本之间更改。

此模块还提供了一个额外的数据对象:

symbol.sym_name

将此模块中定义的常量的数值映射回名称字符串的字典，允许生成更加人类可读的解析树表示。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

32.5. token — 与Python解析树一起使用的常量

源码： Lib/token.py [https://github.com/python/cpython/tree/3.6/Lib/token.py]

此模块提供表示解析树（终端令牌）的叶节点的数值的常量。 请参阅 Python 发行版中的文件 Grammar/Grammar ，以获取语言语法上下文中名称的定义。 名称映射到的特定数值可能会在 Python 版本之间更改。

该模块还提供从数字代码到名称和一些函数的映射。 这些函数镜像了 Python C 头文件中的定义。

token.tok_name

将此模块中定义的常量的数值映射回名称字符串的字典，允许生成更加人类可读的解析树表示。

token.ISTERMINAL(x)

Return true for terminal token values.

token.ISNONTERMINAL(x)

Return true for non-terminal token values.

token.ISEOF(x)

Return true if x is the marker indicating the end of input.

标记常量是：

token.ENDMARKER

token.NAME

token.NUMBER

token.STRING

token.NEWLINE

token.INDENT

token.DEDENT

token.LPAR

token.RPAR

token.LSQB

token.RSQB

token.COLON

token.COMMA

token.SEMI

token.PLUS

token.MINUS

token.STAR

token.SLASH

token.VBAR

token.AMPER

token.LESS

token.GREATER

token.EQUAL

token.DOT

token.PERCENT

token.LBRACE

token.RBRACE

token.EQEQUAL

token.NOTEQUAL

token.LESSEQUAL

token.GREATEREQUAL

token.TILDE

token.CIRCUMFLEX

token.LEFTSHIFT

token.RIGHTSHIFT

token.DOUBLESTAR

token.PLUSEQUAL

token.MINEQUAL

token.STAREQUAL

token.SLASHEQUAL

token.PERCENTEQUAL

token.AMPEREQUAL

token.VBAREQUAL

token.CIRCUMFLEXEQUAL

token.LEFTSHIFTEQUAL

token.RIGHTSHIFTEQUAL

token.DOUBLESTAREQUAL

token.DOUBLESLASH

token.DOUBLESLASHEQUAL

token.AT

token.ATEQUAL

token.RARROW

token.ELLIPSIS

token.OP

token.AWAIT

token.ASYNC

token.ERRORTOKEN

token.N_TOKENS

token.NT_OFFSET

在 3.5 版更改: Added AWAIT and ASYNC tokens. Starting with Python 3.7, “async” and “await” will be tokenized as NAME tokens, and AWAIT and ASYNC will be removed.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

32.6. keyword — 检验Python关键字

源码： Lib/keyword.py [https://github.com/python/cpython/tree/3.6/Lib/keyword.py]

This module allows a Python program to determine if a string is a keyword.

keyword.iskeyword(s)

Return true if s is a Python keyword.

keyword.kwlist

Sequence containing all the keywords defined for the interpreter. If any keywords are defined to only be active when particular __future__ statements are in effect, these will be included as well.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

32.7. tokenize – 对 Python 代码使用的标记解析器

源码： Lib/tokenize.py [https://github.com/python/cpython/tree/3.6/Lib/tokenize.py]

The tokenize module provides a lexical scanner for Python source code, implemented in Python. The scanner in this module returns comments as tokens as well, making it useful for implementing “pretty-printers,” including colorizers for on-screen displays.

为了简化标记流的处理，所有的 运算符 和 定界符 以及 Ellipsis 返回时都会打上通用的 OP 标记。 可以通过 tokenize.tokenize() 返回的 named tuple 对象的 exact_type 属性来获得确切的标记类型。

32.7.1. 对输入进行解析标记

主要的入口是一个 generator:

tokenize.tokenize(readline)

生成器 tokenize() 需要一个 readline 参数，这个参数必须是一个可调用对象，且能提供与文件对象的 io.IOBase.readline() 方法相同的接口。每次调用这个函数都要 返回字节类型输入的一行数据。

The generator produces 5-tuples with these members: the token type; the token string; a 2-tuple (srow, scol) of ints specifying the row and column where the token begins in the source; a 2-tuple (erow, ecol) of ints specifying the row and column where the token ends in the source; and the line on which the token was found. The line passed (the last tuple item) is the logical line; continuation lines are included. The 5 tuple is returned as a named tuple with the field names: type string start end line.

The returned named tuple has an additional property named exact_type that contains the exact operator type for token.OP tokens. For all other token types exact_type equals the named tuple type field.

在 3.1 版更改: Added support for named tuples.

在 3.3 版更改: Added support for exact_type.

tokenize() determines the source encoding of the file by looking for a UTF-8 BOM or encoding cookie, according to PEP 263 [https://www.python.org/dev/peps/pep-0263].

All constants from the token module are also exported from tokenize, as are three additional token type values:

tokenize.COMMENT

Token value used to indicate a comment.

tokenize.NL

Token value used to indicate a non-terminating newline. The NEWLINE token indicates the end of a logical line of Python code; NL tokens are generated when a logical line of code is continued over multiple physical lines.

tokenize.ENCODING

Token value that indicates the encoding used to decode the source bytes into text. The first token returned by tokenize() will always be an ENCODING token.

Another function is provided to reverse the tokenization process. This is useful for creating tools that tokenize a script, modify the token stream, and write back the modified script.

tokenize.untokenize(iterable)

Converts tokens back into Python source code. The iterable must return sequences with at least two elements, the token type and the token string. Any additional sequence elements are ignored.

The reconstructed script is returned as a single string. The result is guaranteed to tokenize back to match the input so that the conversion is lossless and round-trips are assured. The guarantee applies only to the token type and token string as the spacing between tokens (column positions) may change.

It returns bytes, encoded using the ENCODING token, which is the first token sequence output by tokenize().

tokenize() needs to detect the encoding of source files it tokenizes. The function it uses to do this is available:

tokenize.detect_encoding(readline)

The detect_encoding() function is used to detect the encoding that should be used to decode a Python source file. It requires one argument, readline, in the same way as the tokenize() generator.

It will call readline a maximum of twice, and return the encoding used (as a string) and a list of any lines (not decoded from bytes) it has read in.

It detects the encoding from the presence of a UTF-8 BOM or an encoding cookie as specified in PEP 263 [https://www.python.org/dev/peps/pep-0263]. If both a BOM and a cookie are present, but disagree, a SyntaxError will be raised. Note that if the BOM is found, 'utf-8-sig' will be returned as an encoding.

If no encoding is specified, then the default of 'utf-8' will be returned.

Use open() to open Python source files: it uses detect_encoding() to detect the file encoding.

tokenize.open(filename)

Open a file in read only mode using the encoding detected by detect_encoding().

3.2 新版功能.

exception tokenize.TokenError

Raised when either a docstring or expression that may be split over several lines is not completed anywhere in the file, for example:

"""Beginning of
docstring

或者：

[1,
 2,
 3

Note that unclosed single-quoted strings do not cause an error to be raised. They are tokenized as ERRORTOKEN, followed by the tokenization of their contents.

32.7.2. Command-Line Usage

3.3 新版功能.

The tokenize module can be executed as a script from the command line. It is as simple as:

python -m tokenize [-e] [filename.py]

The following options are accepted:

-h, --help

show this help message and exit

-e, --exact

display token names using the exact type

If filename.py is specified its contents are tokenized to stdout. Otherwise, tokenization is performed on stdin.

32.7.3. 例子

Example of a script rewriter that transforms float literals into Decimal objects:

from tokenize import tokenize, untokenize, NUMBER, STRING, NAME, OP
from io import BytesIO

def decistmt(s):
    """Substitute Decimals for floats in a string of statements.

    >>> from decimal import Decimal
    >>> s = 'print(+21.3e-5*-.1234/81.7)'
    >>> decistmt(s)
    "print (+Decimal ('21.3e-5')*-Decimal ('.1234')/Decimal ('81.7'))"

    The format of the exponent is inherited from the platform C library.
    Known cases are "e-007" (Windows) and "e-07" (not Windows).  Since
    we're only showing 12 digits, and the 13th isn't close to 5, the
    rest of the output should be platform-independent.

    >>> exec(s)  #doctest: +ELLIPSIS
    -3.21716034272e-0...7

    Output from calculations with Decimal should be identical across all
    platforms.

    >>> exec(decistmt(s))
    -3.217160342717258261933904529E-7
    """
    result = []
    g = tokenize(BytesIO(s.encode('utf-8')).readline)  # tokenize the string
    for toknum, tokval, _, _, _ in g:
        if toknum == NUMBER and '.' in tokval:  # replace NUMBER tokens
            result.extend([
                (NAME, 'Decimal'),
                (OP, '('),
                (STRING, repr(tokval)),
                (OP, ')')
            ])
        else:
            result.append((toknum, tokval))
    return untokenize(result).decode('utf-8')

Example of tokenizing from the command line. The script:

def say_hello():
    print("Hello, World!")

say_hello()

will be tokenized to the following output where the first column is the range of the line/column coordinates where the token is found, the second column is the name of the token, and the final column is the value of the token (if any)

$ python -m tokenize hello.py
0,0-0,0:            ENCODING       'utf-8'
1,0-1,3:            NAME           'def'
1,4-1,13:           NAME           'say_hello'
1,13-1,14:          OP             '('
1,14-1,15:          OP             ')'
1,15-1,16:          OP             ':'
1,16-1,17:          NEWLINE        '\n'
2,0-2,4:            INDENT         '    '
2,4-2,9:            NAME           'print'
2,9-2,10:           OP             '('
2,10-2,25:          STRING         '"Hello, World!"'
2,25-2,26:          OP             ')'
2,26-2,27:          NEWLINE        '\n'
3,0-3,1:            NL             '\n'
4,0-4,0:            DEDENT         ''
4,0-4,9:            NAME           'say_hello'
4,9-4,10:           OP             '('
4,10-4,11:          OP             ')'
4,11-4,12:          NEWLINE        '\n'
5,0-5,0:            ENDMARKER      ''

The exact token type names can be displayed using the -e option:

$ python -m tokenize -e hello.py
0,0-0,0:            ENCODING       'utf-8'
1,0-1,3:            NAME           'def'
1,4-1,13:           NAME           'say_hello'
1,13-1,14:          LPAR           '('
1,14-1,15:          RPAR           ')'
1,15-1,16:          COLON          ':'
1,16-1,17:          NEWLINE        '\n'
2,0-2,4:            INDENT         '    '
2,4-2,9:            NAME           'print'
2,9-2,10:           LPAR           '('
2,10-2,25:          STRING         '"Hello, World!"'
2,25-2,26:          RPAR           ')'
2,26-2,27:          NEWLINE        '\n'
3,0-3,1:            NL             '\n'
4,0-4,0:            DEDENT         ''
4,0-4,9:            NAME           'say_hello'
4,9-4,10:           LPAR           '('
4,10-4,11:          RPAR           ')'
4,11-4,12:          NEWLINE        '\n'
5,0-5,0:            ENDMARKER      ''

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

32.8. tabnanny — 模糊缩进检测

源代码: Lib/tabnanny.py [https://github.com/python/cpython/tree/3.6/Lib/tabnanny.py]

目前，该模块旨在作为脚本调用。但是可以使用下面描述的 check() 函数将其导入IDE。

注解

此模块提供的API可能会在将来的版本中更改；此类更改可能无法向后兼容。

tabnanny.check(file_or_dir)

如果 file_or_dir 是目录而非符号链接，则递归地在名为 file_or_dir 的目录树中下行，沿途检查所有 .py 文件。 如果 file_or_dir 是一个普通 Python 源文件，将检查其中的空格相关问题。 诊断消息将使用 print() 函数写入到标准输出。

tabnanny.verbose

此标志指明是否打印详细消息。 如果作为脚本调用则是通过 -v 选项来增加。

tabnanny.filename_only

此旗标指明是否只打印包含空格相关问题文件的文件名。 如果作为脚本调用则是通过 -q 选项来设为真值。

exception tabnanny.NannyNag

如果检测到模糊缩进则由 process_tokens() 引发。 在 check() 中捕获并处理。

tabnanny.process_tokens(tokens)

此函数由 check() 用来处理由 tokenize 模块所生成的标记。

参见

模块 tokenize

用于Python源代码的词法扫描程序。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

32.9. pyclbr — Python class browser support

源代码: Lib/pyclbr.py [https://github.com/python/cpython/tree/3.6/Lib/pyclbr.py]

The pyclbr module can be used to determine some limited information about the classes, methods and top-level functions defined in a module. The information provided is sufficient to implement a traditional three-pane class browser. The information is extracted from the source code rather than by importing the module, so this module is safe to use with untrusted code. This restriction makes it impossible to use this module with modules not implemented in Python, including all standard and optional extension modules.

pyclbr.readmodule(module, path=None)

Read a module and return a dictionary mapping class names to class descriptor objects. The parameter module should be the name of a module as a string; it may be the name of a module within a package. The path parameter should be a sequence, and is used to augment the value of sys.path, which is used to locate module source code.

pyclbr.readmodule_ex(module, path=None)

Like readmodule(), but the returned dictionary, in addition to mapping class names to class descriptor objects, also maps top-level function names to function descriptor objects. Moreover, if the module being read is a package, the key '__path__' in the returned dictionary has as its value a list which contains the package search path.

32.9.1. 类对象

The Class objects used as values in the dictionary returned by readmodule() and readmodule_ex() provide the following data attributes:

Class.module

The name of the module defining the class described by the class descriptor.

Class.name

类名称。

Class.super

A list of Class objects which describe the immediate base classes of the class being described. Classes which are named as superclasses but which are not discoverable by readmodule() are listed as a string with the class name instead of as Class objects.

Class.methods

A dictionary mapping method names to line numbers.

Class.file

Name of the file containing the class statement defining the class.

Class.lineno

The line number of the class statement within the file named by file.

32.9.2. 函数对象

The Function objects used as values in the dictionary returned by readmodule_ex() provide the following attributes:

Function.module

The name of the module defining the function described by the function descriptor.

Function.name

函数名称。

Function.file

Name of the file containing the def statement defining the function.

Function.lineno

The line number of the def statement within the file named by file.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

32.10. py_compile — 编译 Python 源文件

源代码: Lib/py_compile.py [https://github.com/python/cpython/tree/3.6/Lib/py_compile.py]

py_compile 模块提供了用来从源文件生成字节码的函数和另一个用于当模块源文件作为脚本被调用时的函数。

虽然不太常用，但这个函数在安装共享模块时还是很有用的，特别是当一些用户可能没有权限在包含源代码的目录中写字节码缓存文件时。

exception py_compile.PyCompileError

当编译文件过程中发生错误时，抛出的异常。

py_compile.compile(file, cfile=None, dfile=None, doraise=False, optimize=-1)

将源文件编译成字节码，并写出字节码缓存文件。源代码从名为 file 的文件中加载。 字节码写入 cfile ，默认为 PEP 3147 [https://www.python.org/dev/peps/pep-3147]/PEP 488 [https://www.python.org/dev/peps/pep-0488] 路径，以 .pyc 结尾。例如，如果 file 是 /foo/bar/baz.py ，那么对于 Python 3.2，cfile 默认为 /foo/bar/__pycache__/baz.cpython-32.pyc 。 如果指定了 dfile ，那么在错误信息中，它将代替 file 作为源文件的名称。 如果 doraise 为true，当编译 file 遇到错误时，会抛出一个 PyCompileError 。如果 doraise 为 false (默认值)，错误字符串将写入 sys.stderr ，但不会抛出异常。 该函数返回编译后字节文件的路径，即 cfile 的值。

如果 cfile 所表示（显式指定或计算得出）的路径为符号链接或非常规文件，则将引发 FileExistsError。 此行为是用来警告如果允许写入编译后字节码文件到这些路径则导入操作将会把它们转为常规文件。 这是使用文件重命名来将最终编译后字节码文件放置到位以防止并发文件写入问题的导入操作的附带效果。

optimize 控制优化级别并会被传给内置的 compile() 函数。 默认值 -1 表示选择当前解释器的优化级别。

在 3.2 版更改: 将 cfile 的默认值改成与 PEP 3147 [https://www.python.org/dev/peps/pep-3147] 兼容。 之前的默认值是 file + 'c' (如果启用优化则为 'o')。 同时也添加了 optimize 形参。

在 3.4 版更改: 将代码更改为使用 importlib 执行字节码缓存文件写入。 这意味着文件创建/写入的语义现在与 importlib 所做的相匹配，例如权限、写入和移动语义等等。 同时也添加了当 cfile 为符号链接或非常规文件时引发 FileExistsError 的预警设置。

py_compile.main(args=None)

编译多个源文件。 在 args 中（或者当 args 为 None 时则是在命令行中）指定的文件会被编译并将结果字节码以正常方式来缓存。 此函数不会搜索目录结构来定位源文件；它只编译显式指定的文件。 如果 '-' 是 args 中唯一的值，则会从标准输入获取文件列表。

在 3.2 版更改: 添加了对 '-' 的支持。

当此模块作为脚本运行时，会使用 main() 来编译命令行中指定的所有文件。 如果某个文件无法被编译则退出状态将为非零值。

参见

模块 compileall

编译一个目录树中所有 Python 源文件的工具。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

32.11. compileall — Byte-compile Python libraries

Source code: Lib/compileall.py [https://github.com/python/cpython/tree/3.6/Lib/compileall.py]

This module provides some utility functions to support installing Python libraries. These functions compile Python source files in a directory tree. This module can be used to create the cached byte-code files at library installation time, which makes them available for use even by users who don’t have write permission to the library directories.

32.11.1. Command-line use

This module can work as a script (using python -m compileall) to compile Python sources.

directory ...

file ...

Positional arguments are files to compile or directories that contain source files, traversed recursively. If no argument is given, behave as if the command line was -l <directories from sys.path>.

-l

Do not recurse into subdirectories, only compile source code files directly contained in the named or implied directories.

-f

Force rebuild even if timestamps are up-to-date.

-q

Do not print the list of files compiled. If passed once, error messages will still be printed. If passed twice (-qq), all output is suppressed.

-d destdir

Directory prepended to the path to each file being compiled. This will appear in compilation time tracebacks, and is also compiled in to the byte-code file, where it will be used in tracebacks and other messages in cases where the source file does not exist at the time the byte-code file is executed.

-x regex

regex is used to search the full path to each file considered for compilation, and if the regex produces a match, the file is skipped.

-i list

Read the file list and add each line that it contains to the list of files and directories to compile. If list is -, read lines from stdin.

-b

Write the byte-code files to their legacy locations and names, which may overwrite byte-code files created by another version of Python. The default is to write files to their PEP 3147 [https://www.python.org/dev/peps/pep-3147] locations and names, which allows byte-code files from multiple versions of Python to coexist.

-r

Control the maximum recursion level for subdirectories. If this is given, then -l option will not be taken into account. python -m compileall <directory> -r 0 is equivalent to python -m compileall <directory> -l.

-j N

Use N workers to compile the files within the given directory. If 0 is used, then the result of os.cpu_count() will be used.

在 3.2 版更改: Added the -i, -b and -h options.

在 3.5 版更改: Added the -j, -r, and -qq options. -q option was changed to a multilevel value. -b will always produce a byte-code file ending in .pyc, never .pyo.

There is no command-line option to control the optimization level used by the compile() function, because the Python interpreter itself already provides the option: python -O -m compileall.

32.11.2. Public functions

compileall.compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1, workers=1)

Recursively descend the directory tree named by dir, compiling all .py files along the way. Return a true value if all the files compiled successfully, and a false value otherwise.

The maxlevels parameter is used to limit the depth of the recursion; it defaults to 10.

If ddir is given, it is prepended to the path to each file being compiled for use in compilation time tracebacks, and is also compiled in to the byte-code file, where it will be used in tracebacks and other messages in cases where the source file does not exist at the time the byte-code file is executed.

If force is true, modules are re-compiled even if the timestamps are up to date.

If rx is given, its search method is called on the complete path to each file considered for compilation, and if it returns a true value, the file is skipped.

If quiet is False or 0 (the default), the filenames and other information are printed to standard out. Set to 1, only errors are printed. Set to 2, all output is suppressed.

If legacy is true, byte-code files are written to their legacy locations and names, which may overwrite byte-code files created by another version of Python. The default is to write files to their PEP 3147 [https://www.python.org/dev/peps/pep-3147] locations and names, which allows byte-code files from multiple versions of Python to coexist.

optimize specifies the optimization level for the compiler. It is passed to the built-in compile() function.

The argument workers specifies how many workers are used to compile files in parallel. The default is to not use multiple workers. If the platform can’t use multiple workers and workers argument is given, then sequential compilation will be used as a fallback. If workers is lower than 0, a ValueError will be raised.

在 3.2 版更改: Added the legacy and optimize parameter.

在 3.5 版更改: Added the workers parameter.

在 3.5 版更改: quiet parameter was changed to a multilevel value.

在 3.5 版更改: The legacy parameter only writes out .pyc files, not .pyo files no matter what the value of optimize is.

在 3.6 版更改: 接受一个 path-like object。

compileall.compile_file(fullname, ddir=None, force=False, rx=None, quiet=0, legacy=False, optimize=-1)

Compile the file with path fullname. Return a true value if the file compiled successfully, and a false value otherwise.

If ddir is given, it is prepended to the path to the file being compiled for use in compilation time tracebacks, and is also compiled in to the byte-code file, where it will be used in tracebacks and other messages in cases where the source file does not exist at the time the byte-code file is executed.

If rx is given, its search method is passed the full path name to the file being compiled, and if it returns a true value, the file is not compiled and True is returned.

If quiet is False or 0 (the default), the filenames and other information are printed to standard out. Set to 1, only errors are printed. Set to 2, all output is suppressed.

If legacy is true, byte-code files are written to their legacy locations and names, which may overwrite byte-code files created by another version of Python. The default is to write files to their PEP 3147 [https://www.python.org/dev/peps/pep-3147] locations and names, which allows byte-code files from multiple versions of Python to coexist.

optimize specifies the optimization level for the compiler. It is passed to the built-in compile() function.

3.2 新版功能.

在 3.5 版更改: quiet parameter was changed to a multilevel value.

在 3.5 版更改: The legacy parameter only writes out .pyc files, not .pyo files no matter what the value of optimize is.

compileall.compile_path(skip_curdir=True, maxlevels=0, force=False, quiet=0, legacy=False, optimize=-1)

Byte-compile all the .py files found along sys.path. Return a true value if all the files compiled successfully, and a false value otherwise.

If skip_curdir is true (the default), the current directory is not included in the search. All other parameters are passed to the compile_dir() function. Note that unlike the other compile functions, maxlevels defaults to 0.

在 3.2 版更改: Added the legacy and optimize parameter.

在 3.5 版更改: quiet parameter was changed to a multilevel value.

在 3.5 版更改: The legacy parameter only writes out .pyc files, not .pyo files no matter what the value of optimize is.

To force a recompile of all the .py files in the Lib/ subdirectory and all its subdirectories:

import compileall

compileall.compile_dir('Lib/', force=True)

# Perform same compilation, excluding files in .svn directories.
import re
compileall.compile_dir('Lib/', rx=re.compile(r'[/\\][.]svn'), force=True)

# pathlib.Path objects can also be used.
import pathlib
compileall.compile_dir(pathlib.Path('Lib/'), force=True)

参见

模块 py_compile

Byte-compile a single source file.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

32.12. dis — Python 字节码反汇编器

Source code: Lib/dis.py [https://github.com/python/cpython/tree/3.6/Lib/dis.py]

dis 模块通过反汇编支持CPython的 bytecode 分析。该模块作为输入的 CPython 字节码在文件 Include/opcode.h 中定义，并由编译器和解释器使用。

CPython implementation detail: 字节码是 CPython 解释器的实现细节。不保证不会在Python版本之间添加、删除或更改字节码。不应考虑将此模块的跨 Python VM 或 Python 版本的使用。

在 3.6 版更改: 每条指令使用2个字节。以前字节数因指令而异。

示例：给出函数 myfunc():

def myfunc(alist):
    return len(alist)

可以使用以下命令显示 myfunc() 的反汇编

>>> dis.dis(myfunc)
  2           0 LOAD_GLOBAL              0 (len)
              2 LOAD_FAST                0 (alist)
              4 CALL_FUNCTION            1
              6 RETURN_VALUE

(“2” 是行号)。

32.12.1. 字节码分析

3.4 新版功能.

字节码分析 API 允许将 Python 代码片段包装在 Bytecode 对象中，以便轻松访问已编译代码的详细信息。

class dis.Bytecode(x, *, first_line=None, current_offset=None)

Analyse the bytecode corresponding to a function, generator, method, string of source code, or a code object (as returned by compile()).

这是下面列出的许多函数的便利包装，最值得注意的是 get_instructions() ，迭代于 Bytecode 的实例产生字节码操作 Instruction 的实例。

如果 first_line 不是 None ，则表示应该为反汇编代码中的第一个源代码行报告的行号。否则，源行信息（如果有的话）直接来自反汇编的代码对象。

如果 current_offset 不是 None ，则它指的是反汇编代码中的指令偏移量。设置它意味着 dis() 将针对指定的操作码显示“当前指令”标记。

classmethod from_traceback(tb)

从给定回溯构造一个 Bytecode 实例，设置 current_offset 为异常负责的指令。

codeobj

已编译的代码对象。

first_line

代码对象的第一个源代码行（如果可用）

dis()

返回字节码操作的格式化视图（与 dis.dis() 打印相同，但作为多行字符串返回）。

info()

返回带有关于代码对象的详细信息的格式化多行字符串，如 code_info() 。

示例:

>>> bytecode = dis.Bytecode(myfunc)
>>> for instr in bytecode:
...     print(instr.opname)
...
LOAD_GLOBAL
LOAD_FAST
CALL_FUNCTION
RETURN_VALUE

32.12.2. 分析函数

dis 模块还定义了以下分析函数，它们将输入直接转换为所需的输出。如果只执行单个操作，它们可能很有用，因此中间分析对象没用：

dis.code_info(x)

Return a formatted multi-line string with detailed code object information for the supplied function, generator, method, source code string or code object.

请注意，代码信息字符串的确切内容是高度依赖于实现的，它们可能会在Python VM或Python版本中任意更改。

3.2 新版功能.

dis.show_code(x, *, file=None)

将提供的函数、方法。源代码字符串或代码对象的详细代码对象信息打印到 file （如果未指定 file ，则为 sys.stdout ）。

这是 print(code_info(x), file= file) 的便捷简写，用于在解释器提示符下进行交互式探索。

3.2 新版功能.

在 3.4 版更改: 添加 file 参数。

dis.dis(x=None, *, file=None)

Disassemble the x object. x can denote either a module, a class, a method, a function, a generator, a code object, a string of source code or a byte sequence of raw bytecode. For a module, it disassembles all functions. For a class, it disassembles all methods (including class and static methods). For a code object or sequence of raw bytecode, it prints one line per bytecode instruction. Strings are first compiled to code objects with the compile() built-in function before being disassembled. If no object is provided, this function disassembles the last traceback.

如果提供的话，反汇编将作为文本写入提供的 file 参数，否则写入 sys.stdout 。

在 3.4 版更改: 添加 file 参数。

dis.distb(tb=None, *, file=None)

如果没有传递，则使用最后一个回溯来反汇编回溯的堆栈顶部函数。 指示了导致异常的指令。

如果提供的话，反汇编将作为文本写入提供的 file 参数，否则写入 sys.stdout 。

在 3.4 版更改: 添加 file 参数。

dis.disassemble(code, lasti=-1, *, file=None)

dis.disco(code, lasti=-1, *, file=None)

反汇编代码对象，如果提供了 lasti ，则指示最后一条指令。输出分为以下几列：

行号，用于每行的第一条指令

当前指令，表示为 --> ，

一个标记的指令，用 >> 表示，

指令的地址，

操作码名称，

操作参数，和

括号中的参数解释。

参数解释识别本地和全局变量名称、常量值、分支目标和比较运算符。

如果提供的话，反汇编将作为文本写入提供的 file 参数，否则写入 sys.stdout 。

在 3.4 版更改: 添加 file 参数。

dis.get_instructions(x, *, first_line=None)

在所提供的函数、方法、源代码字符串或代码对象中的指令上返回一个迭代器。

迭代器生成一系列 Instruction ，命名为元组，提供所提供代码中每个操作的详细信息。

如果 first_line 不是 None ，则表示应该为反汇编代码中的第一个源代码行报告的行号。否则，源行信息（如果有的话）直接来自反汇编的代码对象。

3.4 新版功能.

dis.findlinestarts(code)

此生成器函数使用代码对象 code 的 co_firstlineno 和 co_lnotab 属性来查找源代码中行开头的偏移量。它们生成为 (offset, lineno)` `对。请参阅 :source:`objects/lnotab_notes.txt` ，了解 ``co_lnotab 格式以及如何解码它。

在 3.6 版更改: 行号可能会减少。 以前，他们总是在增加。

dis.findlabels(code)

Detect all offsets in the code object code which are jump targets, and return a list of these offsets.

dis.stack_effect(opcode[, oparg])

使用参数 oparg 计算 opcode 的堆栈效果。

3.4 新版功能.

32.12.3. Python字节码说明

get_instructions() 函数和 Bytecode 类提供字节码指令的详细信息的 Instruction 实例：

class dis.Instruction

字节码操作的详细信息

opcode

操作的数字代码，对应于下面列出的操作码值和 操作码集合 中的字节码值。

opname

人类可读的操作名称

arg

操作的数字参数（如果有的话），否则为 None

argval

已解析的 arg 值（如果已知），否则与 arg 相同

argrepr

人类可读的操作参数描述

offset

在字节码序列中启动操作索引

starts_line

行由此操作码（如果有）启动，否则为 None

is_jump_target

如果其他代码跳到这里，则为 True ，否则为 False

3.4 新版功能.

Python编译器当前生成以下字节码指令。

一般指令

NOP

什么都不做。 用作字节码优化器的占位符。

POP_TOP

删除堆栈顶部（TOS）项。

ROT_TWO

交换两个最顶层的堆栈项。

ROT_THREE

将第二个和第三个堆栈项向上提升一个位置，顶项移动到位置三。

DUP_TOP

复制堆栈顶部的引用。

3.2 新版功能.

DUP_TOP_TWO

复制堆栈顶部的两个引用，使它们保持相同的顺序。

3.2 新版功能.

一元操作

一元操作获取堆栈顶部元素，应用操作，并将结果推回堆栈。

UNARY_POSITIVE

实现 TOS = +TOS 。

UNARY_NEGATIVE

实现 TOS = -TOS 。

UNARY_NOT

实现 TOS = not TOS 。

UNARY_INVERT

实现 TOS = ~TOS 。

GET_ITER

实现 TOS = iter(TOS) 。

GET_YIELD_FROM_ITER

如果 TOS 是一个 generator iterator 或 coroutine 对象则保持原样。否则实现 TOS = iter(TOS) 。

3.5 新版功能.

二元操作

二元操作从堆栈中删除堆栈顶部（TOS）和第二个最顶层堆栈项（TOS1）。 它们执行操作，并将结果放回堆栈。

BINARY_POWER

实现 TOS = TOS1 ** TOS 。

BINARY_MULTIPLY

实现 TOS = TOS1 * TOS 。

BINARY_MATRIX_MULTIPLY

实现 TOS = TOS1 @ TOS 。

3.5 新版功能.

BINARY_FLOOR_DIVIDE

实现 TOS = TOS1 // TOS。

BINARY_TRUE_DIVIDE

实现 TOS = TOS1 / TOS 。

BINARY_MODULO

实现 TOS = TOS1 % TOS 。

BINARY_ADD

实现 TOS = TOS1 + TOS 。

BINARY_SUBTRACT

实现 TOS = TOS1 - TOS 。

BINARY_SUBSCR

实现 TOS = TOS1[TOS] 。

BINARY_LSHIFT

实现 TOS = TOS1 << TOS 。

BINARY_RSHIFT

实现 TOS = TOS1 >> TOS 。

BINARY_AND

实现 TOS = TOS1 & TOS 。

BINARY_XOR

实现 TOS = TOS1 ^ TOS 。

BINARY_OR

实现 TOS = TOS1 | TOS 。

就地操作

就地操作就像二元操作，因为它们删除了TOS和TOS1，并将结果推回到堆栈上，但是当TOS1支持它时，操作就地完成，并且产生的TOS可能是（但不一定） 原来的TOS1。

INPLACE_POWER

就地实现 TOS = TOS1 ** TOS 。

INPLACE_MULTIPLY

就地实现 TOS = TOS1 * TOS 。

INPLACE_MATRIX_MULTIPLY

就地实现 TOS = TOS1 @ TOS 。

3.5 新版功能.

INPLACE_FLOOR_DIVIDE

就地实现 TOS = TOS1 // TOS 。

INPLACE_TRUE_DIVIDE

就地实现 TOS = TOS1 / TOS 。

INPLACE_MODULO

就地实现 TOS = TOS1 % TOS 。

INPLACE_ADD

就地实现 TOS = TOS1 + TOS 。

INPLACE_SUBTRACT

就地实现 TOS = TOS1 - TOS 。

INPLACE_LSHIFT

就地实现 TOS = TOS1 << TOS 。

INPLACE_RSHIFT

就地实现 TOS = TOS1 >> TOS 。

INPLACE_AND

就地实现 TOS = TOS1 & TOS 。

INPLACE_XOR

就地实现 TOS = TOS1 ^ TOS 。

INPLACE_OR

就地实现 TOS = TOS1 | TOS 。

STORE_SUBSCR

实现 TOS1[TOS] = TOS2 。

DELETE_SUBSCR

实现 del TOS1[TOS] 。

协程操作码

GET_AWAITABLE

实现 TOS = get_awaitable(TOS) ，其中 get_awaitable(o) 返回 o 如果 o 是一个有 CO_ITERABLE_COROUTINE 标志的协程对象或生成器对象，否则解析 o.__await__ 。

3.5 新版功能.

GET_AITER

Implements TOS = get_awaitable(TOS.__aiter__()). See GET_AWAITABLE for details about get_awaitable

3.5 新版功能.

GET_ANEXT

实现 PUSH(get_awaitable(TOS.__anext__())) 。参见 GET_AWAITABLE 获取更多 get_awaitable 的细节

3.5 新版功能.

BEFORE_ASYNC_WITH

从栈顶元素解析 __aenter__ 和 __aexit__ 。将 __aexit__ 和 __aenter__() 的结果推入堆栈。

3.5 新版功能.

SETUP_ASYNC_WITH

创建一个新的帧对象。

3.5 新版功能.

其他操作码

PRINT_EXPR

实现交互模式的表达式语句。TOS从堆栈中被移除并打印。在非交互模式下，表达式语句以 POP_TOP 终止。

BREAK_LOOP

Terminates a loop due to a break statement.

CONTINUE_LOOP(target)

Continues a loop due to a continue statement. target is the address to jump to (which should be a FOR_ITER instruction).

SET_ADD(i)

调用 set.add(TOS1[-i], TOS) 。 用于实现集合推导。

LIST_APPEND(i)

调用 list.append(TOS[-i], TOS) 。 用于实现列表推导。

MAP_ADD(i)

Calls dict.setitem(TOS1[-i], TOS, TOS1). Used to implement dict comprehensions.

3.1 新版功能.

对于所有 SET_ADD 、 LIST_APPEND 和 MAP_ADD 指令，当弹出添加的值或键值对时，容器对象保留在堆栈上，以便它可用于循环的进一步迭代。

RETURN_VALUE

返回 TOS 到函数的调用者。

YIELD_VALUE

弹出 TOS 并从一个 generator 生成它。

YIELD_FROM

弹出 TOS 并将其委托给它作为 generator 的子迭代器。

3.3 新版功能.

SETUP_ANNOTATIONS

检查 __annotations__ 是否在 locals() 中定义，如果没有，它被设置为空 dict 。只有在类或模块体静态地包含 variable annotations 时才会发出此操作码。

3.6 新版功能.

IMPORT_STAR

将所有不以 '_' 开头的符号直接从模块 TOS 加载到本地名称空间。加载所有名称后弹出该模块。这个操作码实现了 from module import * 。

POP_BLOCK

Removes one block from the block stack. Per frame, there is a stack of blocks, denoting nested loops, try statements, and such.

POP_EXCEPT

从块堆栈中删除一个块。 弹出的块必须是异常处理程序块，在进入 except 处理程序时隐式创建。除了从帧堆栈弹出无关值之外，最后三个弹出值还用于恢复异常状态。

END_FINALLY

Terminates a finally clause. The interpreter recalls whether the exception has to be re-raised, or whether the function returns, and continues with the outer-next block.

LOAD_BUILD_CLASS

将 builtins .__ build_class__() 推到堆栈上。它之后被 CALL_FUNCTION 调用来构造一个类。

SETUP_WITH(delta)

This opcode performs several operations before a with block starts. First, it loads __exit__() from the context manager and pushes it onto the stack for later use by WITH_CLEANUP. Then, __enter__() is called, and a finally block pointing to delta is pushed. Finally, the result of calling the enter method is pushed onto the stack. The next opcode will either ignore it (POP_TOP), or store it in (a) variable(s) (STORE_FAST, STORE_NAME, or UNPACK_SEQUENCE).

3.2 新版功能.

WITH_CLEANUP_START

Cleans up the stack when a with statement block exits. TOS is the context manager’s __exit__() bound method. Below TOS are 1–3 values indicating how/why the finally clause was entered:

SECOND = None

(SECOND, THIRD) = (WHY_{RETURN,CONTINUE}), retval

SECOND = WHY_*; no retval below it

(SECOND, THIRD, FOURTH) = exc_info()

In the last case, TOS(SECOND, THIRD, FOURTH) is called, otherwise TOS(None, None, None). Pushes SECOND and result of the call to the stack.

WITH_CLEANUP_FINISH

Pops exception type and result of ‘exit’ function call from the stack.

If the stack represents an exception, and the function call returns a ‘true’ value, this information is “zapped” and replaced with a single WHY_SILENCED to prevent END_FINALLY from re-raising the exception. (But non-local gotos will still be resumed.)

以下所有操作码均使用其参数。

STORE_NAME(namei)

实现 name = TOS。 namei 是 name 在代码对象的 co_names 属性中的索引。 在可能的情况下，编译器会尝试使用 STORE_FAST 或 STORE_GLOBAL。

DELETE_NAME(namei)

实现 del name ，其中 namei 是代码对象的 co_names 属性的索引。

UNPACK_SEQUENCE(count)

将 TOS 解包为 count 个单独的值，它们将按从右至左的顺序被放入堆栈。

UNPACK_EX(counts)

实现使用带星号的目标进行赋值：将 TOS 中的可迭代对象解包为单独的值，其中值的总数可以小于可迭代对象中的项数：新值之一将是由所有剩余项构成的列表。

counts 的低字节是列表值之前的值的数量，counts 中的高字节则是之后的值的数量。 结果值会按从右至左的顺序入栈。

STORE_ATTR(namei)

实现 TOS.name = TOS1，其中 namei 是 name 在 co_names 中的索引号。

DELETE_ATTR(namei)

实现 del TOS.name，使用 namei 作为 co_names 中的索引号。

STORE_GLOBAL(namei)

类似于 STORE_NAME 但会将 name 存储为全局变量。

DELETE_GLOBAL(namei)

类似于 DELETE_NAME 但会删除一个全局变量。

LOAD_CONST(consti)

将 co_consts[consti] 推入栈顶。

LOAD_NAME(namei)

将与 co_names[namei] 相关联的值推入栈顶。

BUILD_TUPLE(count)

创建一个使用了来自栈的 count 个项的元组，并将结果元组推入栈顶。

BUILD_LIST(count)

类似于 BUILD_TUPLE 但会创建一个列表。

BUILD_SET(count)

类似于 BUILD_TUPLE 但会创建一个集合。

BUILD_MAP(count)

将一个新字典对象推入栈顶。 弹出 2 * count 项使得字典包含 count 个条目: {..., TOS3: TOS2, TOS1: TOS}。

在 3.5 版更改: 字典是根据栈中的项创建而不是创建一个预设大小包含 count 项的空字典。

BUILD_CONST_KEY_MAP(count)

The version of BUILD_MAP specialized for constant keys. count values are consumed from the stack. The top element on the stack contains a tuple of keys.

3.6 新版功能.

BUILD_STRING(count)

拼接 count 个来自栈的字符串并将结果字符串推入栈顶。

3.6 新版功能.

BUILD_TUPLE_UNPACK(count)

从栈中弹出 count 个可迭代对象，将它们合并为单个元组，并将结果推入栈顶。 实现可迭代对象解包为元组形式 (*x, *y, *z)。

3.5 新版功能.

BUILD_TUPLE_UNPACK_WITH_CALL(count)

这类似于 BUILD_TUPLE_UNPACK 但专用于 f(*x, *y, *z) 调用语法。 栈中 count + 1 位置上的项应当是相应的可调用对象 f。

3.6 新版功能.

BUILD_LIST_UNPACK(count)

这类似于 BUILD_TUPLE_UNPACK 但会将一个列表而非元组推入栈顶。 实现可迭代对象解包为列表形式 [*x, *y, *z]。

3.5 新版功能.

BUILD_SET_UNPACK(count)

这类似于 BUILD_TUPLE_UNPACK 但会将一个集合而非元组推入栈顶。 实现可迭代对象解包为集合形式 {*x, *y, *z}。

3.5 新版功能.

BUILD_MAP_UNPACK(count)

从栈中弹出 count 个映射对象，将它们合并为单个字典，并将结果推入栈顶。 实现字典解包为字典形式 {**x, **y, **z}。

3.5 新版功能.

BUILD_MAP_UNPACK_WITH_CALL(count)

这类似于 BUILD_MAP_UNPACK 但专用于 f(**x, **y, **z) 调用语法。 栈中 count + 2 位置上的项应当是相应的可调用对象 f。

3.5 新版功能.

在 3.6 版更改: 可迭代对象的位置的确定方式是将操作码参数加 2 而不是将其编码到参数的第二个字节。

LOAD_ATTR(namei)

将 TOS 替换为 getattr(TOS, co_names[namei])。

COMPARE_OP(opname)

执行布尔运算操作。 操作名称可在 cmp_op[opname] 中找到。

IMPORT_NAME(namei)

导入模块 co_names[namei]。 会弹出 TOS 和 TOS1 以提供 fromlist 和 level 参数给 __import__()。 模块对象会被推入栈顶。 当前命名空间不受影响：对于一条标准 import 语句，会执行后续的 STORE_FAST 指令来修改命名空间。

IMPORT_FROM(namei)

从在 TOS 内找到的模块中加载属性 co_names[namei]。 结果对象会被推入栈顶，以便由后续的 STORE_FAST 指令来保存。

JUMP_FORWARD(delta)

将字节码计数器的值增加 delta。

POP_JUMP_IF_TRUE(target)

如果 TOS 为真值，则将字节码计数器的值设为 target。 TOS 会被弹出。

3.1 新版功能.

POP_JUMP_IF_FALSE(target)

如果 TOS 为假值，则将字节码计数器的值设为 target。 TOS 会被弹出。

3.1 新版功能.

JUMP_IF_TRUE_OR_POP(target)

如果 TOS 为真值，则将字节码计数器的值设为 target 并将 TOS 留在栈顶。 否则（如 TOS 为假值），TOS 会被弹出。

3.1 新版功能.

JUMP_IF_FALSE_OR_POP(target)

如果 TOS 为假值，则将字节码计数器的值设为 target 并将 TOS 留在栈顶。 否则（如 TOS 为真值），TOS 会被弹出。

3.1 新版功能.

JUMP_ABSOLUTE(target)

将字节码计数器的值设为 target。

FOR_ITER(delta)

TOS 是一个 iterator。 可调用它的 __next__() 方法。 如果产生了一个新值，则将其推入栈顶（将迭代器留在其下方）。 如果迭代器提示已耗尽则 TOS 会被弹出，并将字节码计数器的值增加 delta。

LOAD_GLOBAL(namei)

加载名称为 co_names[namei] 的全局对象推入栈顶。

SETUP_LOOP(delta)

Pushes a block for a loop onto the block stack. The block spans from the current instruction with a size of delta bytes.

SETUP_EXCEPT(delta)

Pushes a try block from a try-except clause onto the block stack. delta points to the first except block.

SETUP_FINALLY(delta)

Pushes a try block from a try-except clause onto the block stack. delta points to the finally block.

LOAD_FAST(var_num)

将指向局部对象 co_varnames[var_num] 的引用推入栈顶。

STORE_FAST(var_num)

将 TOS 存放到局部变量 co_varnames[var_num]。

DELETE_FAST(var_num)

移除局部对象 co_varnames[var_num]。

STORE_ANNOTATION(namei)

Stores TOS as locals()['__annotations__'][co_names[namei]] = TOS.

3.6 新版功能.

LOAD_CLOSURE(i)

将一个包含在单元的第 i 个空位中的对单元的引用推入栈顶并释放可用的存储空间。 如果 i 小于 co_cellvars 的长度则变量的名称为 co_cellvars[i]。 否则为 co_freevars[i - len(co_cellvars)]。

LOAD_DEREF(i)

加载包含在单元的第 i 个空位中的单元并释放可用的存储空间。 将一个对单元所包含对象的引用推入栈顶。

LOAD_CLASSDEREF(i)

类似于 LOAD_DEREF 但在查询单元之前会首先检查局部对象字典。 这被用于加载类语句体中的自由变量。

3.4 新版功能.

STORE_DEREF(i)

将 TOS 存放到包含在单元的第 i 个空位中的单元内并释放可用存储空间。

DELETE_DEREF(i)

清空包含在单元的第 i 个空位中的单元并释放可用存储空间。 被用于 del 语句。

3.2 新版功能.

RAISE_VARARGS(argc)

Raises an exception. argc indicates the number of arguments to the raise statement, ranging from 0 to 3. The handler will find the traceback as TOS2, the parameter as TOS1, and the exception as TOS.

CALL_FUNCTION(argc)

调用一个可调用对象并传入位置参数。 argc 指明位置参数的数量。 栈顶包含位置参数，其中最右边的参数在最顶端。 在参数之下是一个待调用的可调用对象。 CALL_FUNCTION 会从栈中弹出所有参数以及可调用对象，附带这些参数调用该可调用对象，并将可调用对象所返回的返回值推入栈顶。

在 3.6 版更改: 此操作码仅用于附带位置参数的调用。

CALL_FUNCTION_KW(argc)

调用一个可调用对象并传入位置参数（如果有的话）和关键字参数。 argc 指明位置参数和关键字参数的总数量。 栈顶元素包含一个关键字参数名称的元组。 在元组之下是根据元组排序的关键字参数。 在关键字参数之下是位置参数，其中最右边的参数在最顶端。 在参数之下是一个待调用的可调用对象。 CALL_FUNCTION_KW 会从栈中弹出所有参数以及可调用对象，附带这些参数调用该可调用对象，并将可调用对象所返回的返回值推入栈顶。

在 3.6 版更改: 关键字参数会被打包为一个元组而非字典，argc 指明参数的总数量。

CALL_FUNCTION_EX(flags)

调用一个可调用对象并附带位置参数和关键字参数变量集合。 如果设置了 flags 的最低位，则栈顶包含一个由额外关键字参数组成的映射对象。 在该对象之下是一个包含位置参数的可迭代对象和一个待调用的可调用对象。 BUILD_MAP_UNPACK_WITH_CALL 和 BUILD_TUPLE_UNPACK_WITH_CALL 可用于合并多个映射对象和包含参数的可迭代对象。 在该可调用对象被调用之前，映射对象和可迭代对象会被分别“解包”并将它们的内容分别作为关键字参数和位置参数传入。 CALL_FUNCTION_EX 会从栈中弹出所有参数以及可调用对象，附带这些参数调用该可调用对象，并将可调用对象所返回的返回值推入栈顶。

3.6 新版功能.

MAKE_FUNCTION(argc)

将一个新函数对象推入栈顶。 从底端到顶端，如果参数带有指定的旗标值则所使用的栈必须由这些值组成。

0x01 是一个默认值的元组，用于按位置排序的仅限位置形参以及位置或关键字形参

0x02 是一个仅限关键字形参的默认值的字典

0x04 是一个标注字典

0x08 一个包含用于自由变量的单元的元组，生成一个闭包

与函数 (在 TOS1) 相关联的代码

函数的 qualified name (在 TOS)

BUILD_SLICE(argc)

将一个切片对象推入栈顶。 argc 必须为 2 或 3。 如果为 2，则推入 slice(TOS1, TOS)；如果为 3，则推入 slice(TOS2, TOS1, TOS)。 请参阅 slice() 内置函数了解详细信息。

EXTENDED_ARG(ext)

Prefixes any opcode which has an argument too big to fit into the default two bytes. ext holds two additional bytes which, taken together with the subsequent opcode’s argument, comprise a four-byte argument, ext being the two most-significant bytes.

FORMAT_VALUE(flags)

用于实现格式化字面值字符串（f-字符串）。 从栈中弹出一个可选的 fmt_spec，然后是一个必须的 value。 flags 的解读方式如下：

(flags & 0x03) == 0x00: value 按原样格式化。

(flags & 0x03) == 0x01: 在格式化 value 之前调用其 str()。

(flags & 0x03) == 0x02: 在格式化 value 之前调用其 repr()。

(flags & 0x03) == 0x03: 在格式化 value 之前调用其 ascii()。

(flags & 0x04) == 0x04: 从栈中弹出 fmt_spec 并使用它，否则使用空的 fmt_spec。

使用 PyObject_Format() 执行格式化。 结果会被推入栈顶。

3.6 新版功能.

HAVE_ARGUMENT

这不是一个真正的操作码。 它用于标明使用参数和不使用参数的操作码 (分别为 < HAVE_ARGUMENT 和 >= HAVE_ARGUMENT) 之间的分隔线。

在 3.6 版更改: 现在每条指令都带有参数，但操作码 < HAVE_ARGUMENT 会忽略它。 之前仅限操作码 >= HAVE_ARGUMENT 带有参数。

32.12.4. 操作码集合

提供这些集合用于字节码指令的自动内省：

dis.opname

操作名称序列，可使用字节码来索引。

dis.opmap

映射操作名称到字节码的字典

dis.cmp_op

所有比较操作名称的序列。

dis.hasconst

访问常量的字节码序列。

dis.hasfree

访问自由变量的字节码序列（请注意这里所说的‘自由’是指在当前作用域中被内部作用域所引用的名称，或在外部作用域中被此作用域所引用的名称。 它 并不 包括对全局或内置作用域的引用）。

dis.hasname

按名称访问属性的字节码序列

dis.hasjrel

具有相对跳转目标的字节码序列。

dis.hasjabs

具有绝对跳转目标的字节码序列。

dis.haslocal

访问局部变量的字节码序列。

dis.hascompare

布尔运算的字节码序列

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

32.13. pickletools — pickle 开发者工具集

源代码: Lib/pickletools.py [https://github.com/python/cpython/tree/3.6/Lib/pickletools.py]

此模块包含与 pickle 模块内部细节有关的多个常量，一些关于具体实现的详细注释，以及一些能够分析封存数据的有用函数。 此模块的内容对需要操作 pickle 的 Python 核心开发者来说很有用处；pickle 的一般用户则可能会感觉 pickletools 模块与他们无关。

32.13.1. 命令行语法

3.2 新版功能.

当从命令行发起调用时，python -m pickletools 将对一个或更多 pickle 文件的内容进行拆解。 请注意如果你查看 pickle 中保存的 Python 对象而非 pickle 格式的细节，你可能需要改用 -m pickle。 但是，当你想检查的 pickle 文件来自某个不受信任的源时，-m pickletools 是更安全的选择，因为它不会执行 pickle 字节码。

例如，对于一个封存在文件 x.pickle 中的元组 (1, 2):

$ python -m pickle x.pickle
(1, 2)

$ python -m pickletools x.pickle
    0: \x80 PROTO      3
    2: K    BININT1    1
    4: K    BININT1    2
    6: \x86 TUPLE2
    7: q    BINPUT     0
    9: .    STOP
highest protocol among opcodes = 2

32.13.1.1. 命令行选项

-a, --annotate

使用简短的操作码描述来标注每一行。

-o, --output=<file>

输出应当写入到的文件名称。

-l, --indentlevel=<num>

一个新的 MARK 层级所需缩进的空格数。

-m, --memo

当拆解多个对象时，保留各个拆解的备忘记录。

-p, --preamble=<preamble>

当指定一个以上的 pickle 文件时，在每次反汇编之前打印给定的前言。

32.13.2. 编程接口

pickletools.dis(pickle, out=None, memo=None, indentlevel=4, annotate=0)

将 pickle 的符号化反汇编数据输出到文件类对象 out，默认为 sys.stdout。 pickle 可以是一个字符串或一个文件类对象。 memo 可以是一个将被用作 pickle 的备忘记录的 Python 字典；它可被用来对由同一封存器创建的多个封存对象执行反汇编。 由 MARK 操作码指明的每个连续级别将会缩进 indentlevel 个空格。 如果为 annotate 指定了一个非零值，则输出中的每个操作码将以一个简短描述来标注。 annotate 的值会被用作标注所应开始的列的提示。

3.2 新版功能: annotate 参数。

pickletools.genops(pickle)

提供包含 pickle 中所有操作码的 iterator，返回一个 (opcode, arg, pos) 三元组的序列。 opcode 是 OpcodeInfo 类的一个实例；arg 是 Python 对象形式的 opcode 参数的已解码值；pos 是 opcode 所在的位置。 pickle 可以是一个字符串或一个文件类对象。

pickletools.optimize(picklestring)

在消除未使用的 PUT 操作码之后返回一个新的等效 pickle 字符串。 优化后的 pickle 将更为简短，耗费更为的传输时间，要求更少的存储空间并能更高效地解封。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

32. Python 语言服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

33. 杂项服务

本章中介绍的模块提供了所有 Python 版本中提供的各种杂项服务。 这是一个概述：

33.1. formatter — 通用格式化输出

33.1.1. The Formatter Interface

33.1.2. Formatter Implementations

33.1.3. The Writer Interface

33.1.4. Writer Implementations

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

33. 杂项服务 »

|

33.1. formatter — 通用格式化输出

3.4 版后已移除: 因为被使用的次数很少，此格式化模块已经被弃用了。

This module supports two interface definitions, each with multiple implementations: The formatter interface, and the writer interface which is required by the formatter interface.

Formatter objects transform an abstract flow of formatting events into specific output events on writer objects. Formatters manage several stack structures to allow various properties of a writer object to be changed and restored; writers need not be able to handle relative changes nor any sort of “change back” operation. Specific writer properties which may be controlled via formatter objects are horizontal alignment, font, and left margin indentations. A mechanism is provided which supports providing arbitrary, non-exclusive style settings to a writer as well. Additional interfaces facilitate formatting events which are not reversible, such as paragraph separation.

Writer objects encapsulate device interfaces. Abstract devices, such as file formats, are supported as well as physical devices. The provided implementations all work with abstract devices. The interface makes available mechanisms for setting the properties which formatter objects manage and inserting data into the output.

33.1.1. The Formatter Interface

Interfaces to create formatters are dependent on the specific formatter class being instantiated. The interfaces described below are the required interfaces which all formatters must support once initialized.

One data element is defined at the module level:

formatter.AS_IS

Value which can be used in the font specification passed to the push_font() method described below, or as the new value to any other push_property() method. Pushing the AS_IS value allows the corresponding pop_property() method to be called without having to track whether the property was changed.

The following attributes are defined for formatter instance objects:

formatter.writer

The writer instance with which the formatter interacts.

formatter.end_paragraph(blanklines)

Close any open paragraphs and insert at least blanklines before the next paragraph.

formatter.add_line_break()

Add a hard line break if one does not already exist. This does not break the logical paragraph.

formatter.add_hor_rule(*args, **kw)

Insert a horizontal rule in the output. A hard break is inserted if there is data in the current paragraph, but the logical paragraph is not broken. The arguments and keywords are passed on to the writer’s send_line_break() method.

formatter.add_flowing_data(data)

Provide data which should be formatted with collapsed whitespace. Whitespace from preceding and successive calls to add_flowing_data() is considered as well when the whitespace collapse is performed. The data which is passed to this method is expected to be word-wrapped by the output device. Note that any word-wrapping still must be performed by the writer object due to the need to rely on device and font information.

formatter.add_literal_data(data)

Provide data which should be passed to the writer unchanged. Whitespace, including newline and tab characters, are considered legal in the value of data.

formatter.add_label_data(format, counter)

Insert a label which should be placed to the left of the current left margin. This should be used for constructing bulleted or numbered lists. If the format value is a string, it is interpreted as a format specification for counter, which should be an integer. The result of this formatting becomes the value of the label; if format is not a string it is used as the label value directly. The label value is passed as the only argument to the writer’s send_label_data() method. Interpretation of non-string label values is dependent on the associated writer.

Format specifications are strings which, in combination with a counter value, are used to compute label values. Each character in the format string is copied to the label value, with some characters recognized to indicate a transform on the counter value. Specifically, the character '1' represents the counter value formatter as an Arabic number, the characters 'A' and 'a' represent alphabetic representations of the counter value in upper and lower case, respectively, and 'I' and 'i' represent the counter value in Roman numerals, in upper and lower case. Note that the alphabetic and roman transforms require that the counter value be greater than zero.

formatter.flush_softspace()

Send any pending whitespace buffered from a previous call to add_flowing_data() to the associated writer object. This should be called before any direct manipulation of the writer object.

formatter.push_alignment(align)

Push a new alignment setting onto the alignment stack. This may be AS_IS if no change is desired. If the alignment value is changed from the previous setting, the writer’s new_alignment() method is called with the align value.

formatter.pop_alignment()

Restore the previous alignment.

formatter.push_font((size, italic, bold, teletype))

Change some or all font properties of the writer object. Properties which are not set to AS_IS are set to the values passed in while others are maintained at their current settings. The writer’s new_font() method is called with the fully resolved font specification.

formatter.pop_font()

Restore the previous font.

formatter.push_margin(margin)

Increase the number of left margin indentations by one, associating the logical tag margin with the new indentation. The initial margin level is 0. Changed values of the logical tag must be true values; false values other than AS_IS are not sufficient to change the margin.

formatter.pop_margin()

Restore the previous margin.

formatter.push_style(*styles)

Push any number of arbitrary style specifications. All styles are pushed onto the styles stack in order. A tuple representing the entire stack, including AS_IS values, is passed to the writer’s new_styles() method.

formatter.pop_style(n=1)

Pop the last n style specifications passed to push_style(). A tuple representing the revised stack, including AS_IS values, is passed to the writer’s new_styles() method.

formatter.set_spacing(spacing)

Set the spacing style for the writer.

formatter.assert_line_data(flag=1)

Inform the formatter that data has been added to the current paragraph out-of-band. This should be used when the writer has been manipulated directly. The optional flag argument can be set to false if the writer manipulations produced a hard line break at the end of the output.

33.1.2. Formatter Implementations

Two implementations of formatter objects are provided by this module. Most applications may use one of these classes without modification or subclassing.

class formatter.NullFormatter(writer=None)

A formatter which does nothing. If writer is omitted, a NullWriter instance is created. No methods of the writer are called by NullFormatter instances. Implementations should inherit from this class if implementing a writer interface but don’t need to inherit any implementation.

class formatter.AbstractFormatter(writer)

The standard formatter. This implementation has demonstrated wide applicability to many writers, and may be used directly in most circumstances. It has been used to implement a full-featured World Wide Web browser.

33.1.3. The Writer Interface

Interfaces to create writers are dependent on the specific writer class being instantiated. The interfaces described below are the required interfaces which all writers must support once initialized. Note that while most applications can use the AbstractFormatter class as a formatter, the writer must typically be provided by the application.

writer.flush()

Flush any buffered output or device control events.

writer.new_alignment(align)

Set the alignment style. The align value can be any object, but by convention is a string or None, where None indicates that the writer’s “preferred” alignment should be used. Conventional align values are 'left', 'center', 'right', and 'justify'.

writer.new_font(font)

Set the font style. The value of font will be None, indicating that the device’s default font should be used, or a tuple of the form (size, italic, bold, teletype). Size will be a string indicating the size of font that should be used; specific strings and their interpretation must be defined by the application. The italic, bold, and teletype values are Boolean values specifying which of those font attributes should be used.

writer.new_margin(margin, level)

Set the margin level to the integer level and the logical tag to margin. Interpretation of the logical tag is at the writer’s discretion; the only restriction on the value of the logical tag is that it not be a false value for non-zero values of level.

writer.new_spacing(spacing)

Set the spacing style to spacing.

writer.new_styles(styles)

Set additional styles. The styles value is a tuple of arbitrary values; the value AS_IS should be ignored. The styles tuple may be interpreted either as a set or as a stack depending on the requirements of the application and writer implementation.

writer.send_line_break()

Break the current line.

writer.send_paragraph(blankline)

Produce a paragraph separation of at least blankline blank lines, or the equivalent. The blankline value will be an integer. Note that the implementation will receive a call to send_line_break() before this call if a line break is needed; this method should not include ending the last line of the paragraph. It is only responsible for vertical spacing between paragraphs.

writer.send_hor_rule(*args, **kw)

Display a horizontal rule on the output device. The arguments to this method are entirely application- and writer-specific, and should be interpreted with care. The method implementation may assume that a line break has already been issued via send_line_break().

writer.send_flowing_data(data)

Output character data which may be word-wrapped and re-flowed as needed. Within any sequence of calls to this method, the writer may assume that spans of multiple whitespace characters have been collapsed to single space characters.

writer.send_literal_data(data)

Output character data which has already been formatted for display. Generally, this should be interpreted to mean that line breaks indicated by newline characters should be preserved and no new line breaks should be introduced. The data may contain embedded newline and tab characters, unlike data provided to the send_formatted_data() interface.

writer.send_label_data(data)

Set data to the left of the current left margin, if possible. The value of data is not restricted; treatment of non-string values is entirely application- and writer-dependent. This method will only be called at the beginning of a line.

33.1.4. Writer Implementations

Three implementations of the writer object interface are provided as examples by this module. Most applications will need to derive new writer classes from the NullWriter class.

class formatter.NullWriter

A writer which only provides the interface definition; no actions are taken on any methods. This should be the base class for all writers which do not need to inherit any implementation methods.

class formatter.AbstractWriter

A writer which can be used in debugging formatters, but not much else. Each method simply announces itself by printing its name and arguments on standard output.

class formatter.DumbWriter(file=None, maxcol=72)

Simple writer class which writes output on the file object passed in as file or, if file is omitted, on standard output. The output is simply word-wrapped to the number of columns specified by maxcol. This class is suitable for reflowing a sequence of paragraphs.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

33. 杂项服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

34. Windows系统相关模块

本章节叙述的模块只在 Windows 平台上可用。

34.1. msilib — Read and write Microsoft Installer files

34.1.1. Database Objects

34.1.2. View Objects

34.1.3. Summary Information Objects

34.1.4. Record Objects

34.1.5. Errors

34.1.6. CAB Objects

34.1.7. Directory Objects

34.1.8. 相关特性

34.1.9. GUI classes

34.1.10. Precomputed tables

34.2. msvcrt — 来自 MS VC++ 运行时的有用例程

34.2.1. 文件操作

34.2.2. 控制台 I/O

34.2.3. 其他函数

34.3. winreg — Windows 注册表访问

34.3.1. 函数

34.3.2. 常量

34.3.2.1. HKEY_* Constants

34.3.2.2. Access Rights

34.3.2.2.1. 64-bit Specific

34.3.2.3. Value Types

34.3.3. Registry Handle Objects

34.4. winsound — Sound-playing interface for Windows

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

34. Windows系统相关模块 »

|

34.1. msilib — Read and write Microsoft Installer files

Source code: Lib/msilib/__init__.py [https://github.com/python/cpython/tree/3.6/Lib/msilib/__init__.py]

The msilib supports the creation of Microsoft Installer (.msi) files. Because these files often contain an embedded “cabinet” file (.cab), it also exposes an API to create CAB files. Support for reading .cab files is currently not implemented; read support for the .msi database is possible.

This package aims to provide complete access to all tables in an .msi file, therefore, it is a fairly low-level API. Two primary applications of this package are the distutils command bdist_msi, and the creation of Python installer package itself (although that currently uses a different version of msilib).

The package contents can be roughly split into four parts: low-level CAB routines, low-level MSI routines, higher-level MSI routines, and standard table structures.

msilib.FCICreate(cabname, files)

Create a new CAB file named cabname. files must be a list of tuples, each containing the name of the file on disk, and the name of the file inside the CAB file.

The files are added to the CAB file in the order they appear in the list. All files are added into a single CAB file, using the MSZIP compression algorithm.

Callbacks to Python for the various steps of MSI creation are currently not exposed.

msilib.UuidCreate()

Return the string representation of a new unique identifier. This wraps the Windows API functions UuidCreate() and UuidToString().

msilib.OpenDatabase(path, persist)

Return a new database object by calling MsiOpenDatabase. path is the file name of the MSI file; persist can be one of the constants MSIDBOPEN_CREATEDIRECT, MSIDBOPEN_CREATE, MSIDBOPEN_DIRECT, MSIDBOPEN_READONLY, or MSIDBOPEN_TRANSACT, and may include the flag MSIDBOPEN_PATCHFILE. See the Microsoft documentation for the meaning of these flags; depending on the flags, an existing database is opened, or a new one created.

msilib.CreateRecord(count)

Return a new record object by calling MSICreateRecord(). count is the number of fields of the record.

msilib.init_database(name, schema, ProductName, ProductCode, ProductVersion, Manufacturer)

Create and return a new database name, initialize it with schema, and set the properties ProductName, ProductCode, ProductVersion, and Manufacturer.

schema must be a module object containing tables and _Validation_records attributes; typically, msilib.schema should be used.

The database will contain just the schema and the validation records when this function returns.

msilib.add_data(database, table, records)

Add all records to the table named table in database.

The table argument must be one of the predefined tables in the MSI schema, e.g. 'Feature', 'File', 'Component', 'Dialog', 'Control', etc.

records should be a list of tuples, each one containing all fields of a record according to the schema of the table. For optional fields, None can be passed.

Field values can be ints, strings, or instances of the Binary class.

class msilib.Binary(filename)

Represents entries in the Binary table; inserting such an object using add_data() reads the file named filename into the table.

msilib.add_tables(database, module)

Add all table content from module to database. module must contain an attribute tables listing all tables for which content should be added, and one attribute per table that has the actual content.

This is typically used to install the sequence tables.

msilib.add_stream(database, name, path)

Add the file path into the _Stream table of database, with the stream name name.

msilib.gen_uuid()

Return a new UUID, in the format that MSI typically requires (i.e. in curly braces, and with all hexdigits in upper-case).

参见

FCICreate [https://msdn.microsoft.com/en-us/library/bb432265.aspx] UuidCreate [https://msdn.microsoft.com/en-us/library/windows/desktop/aa379205.aspx] UuidToString [https://msdn.microsoft.com/en-us/library/windows/desktop/aa379352.aspx]

34.1.1. Database Objects

Database.OpenView(sql)

Return a view object, by calling MSIDatabaseOpenView(). sql is the SQL statement to execute.

Database.Commit()

Commit the changes pending in the current transaction, by calling MSIDatabaseCommit().

Database.GetSummaryInformation(count)

Return a new summary information object, by calling MsiGetSummaryInformation(). count is the maximum number of updated values.

参见

MSIDatabaseOpenView [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370082.aspx] MSIDatabaseCommit [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370075.aspx] MSIGetSummaryInformation [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370301.aspx]

34.1.2. View Objects

View.Execute(params)

Execute the SQL query of the view, through MSIViewExecute(). If params is not None, it is a record describing actual values of the parameter tokens in the query.

View.GetColumnInfo(kind)

Return a record describing the columns of the view, through calling MsiViewGetColumnInfo(). kind can be either MSICOLINFO_NAMES or MSICOLINFO_TYPES.

View.Fetch()

Return a result record of the query, through calling MsiViewFetch().

View.Modify(kind, data)

Modify the view, by calling MsiViewModify(). kind can be one of MSIMODIFY_SEEK, MSIMODIFY_REFRESH, MSIMODIFY_INSERT, MSIMODIFY_UPDATE, MSIMODIFY_ASSIGN, MSIMODIFY_REPLACE, MSIMODIFY_MERGE, MSIMODIFY_DELETE, MSIMODIFY_INSERT_TEMPORARY, MSIMODIFY_VALIDATE, MSIMODIFY_VALIDATE_NEW, MSIMODIFY_VALIDATE_FIELD, or MSIMODIFY_VALIDATE_DELETE.

data must be a record describing the new data.

View.Close()

Close the view, through MsiViewClose().

参见

MsiViewExecute [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370513.aspx] MSIViewGetColumnInfo [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370516.aspx] MsiViewFetch [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370514.aspx] MsiViewModify [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370519.aspx] MsiViewClose [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370510.aspx]

34.1.3. Summary Information Objects

SummaryInformation.GetProperty(field)

Return a property of the summary, through MsiSummaryInfoGetProperty(). field is the name of the property, and can be one of the constants PID_CODEPAGE, PID_TITLE, PID_SUBJECT, PID_AUTHOR, PID_KEYWORDS, PID_COMMENTS, PID_TEMPLATE, PID_LASTAUTHOR, PID_REVNUMBER, PID_LASTPRINTED, PID_CREATE_DTM, PID_LASTSAVE_DTM, PID_PAGECOUNT, PID_WORDCOUNT, PID_CHARCOUNT, PID_APPNAME, or PID_SECURITY.

SummaryInformation.GetPropertyCount()

Return the number of summary properties, through MsiSummaryInfoGetPropertyCount().

SummaryInformation.SetProperty(field, value)

Set a property through MsiSummaryInfoSetProperty(). field can have the same values as in GetProperty(), value is the new value of the property. Possible value types are integer and string.

SummaryInformation.Persist()

Write the modified properties to the summary information stream, using MsiSummaryInfoPersist().

参见

MsiSummaryInfoGetProperty [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370409.aspx] MsiSummaryInfoGetPropertyCount [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370488.aspx] MsiSummaryInfoSetProperty [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370491.aspx] MsiSummaryInfoPersist [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370490.aspx]

34.1.4. Record Objects

Record.GetFieldCount()

Return the number of fields of the record, through MsiRecordGetFieldCount().

Record.GetInteger(field)

Return the value of field as an integer where possible. field must be an integer.

Record.GetString(field)

Return the value of field as a string where possible. field must be an integer.

Record.SetString(field, value)

Set field to value through MsiRecordSetString(). field must be an integer; value a string.

Record.SetStream(field, value)

Set field to the contents of the file named value, through MsiRecordSetStream(). field must be an integer; value a string.

Record.SetInteger(field, value)

Set field to value through MsiRecordSetInteger(). Both field and value must be an integer.

Record.ClearData()

Set all fields of the record to 0, through MsiRecordClearData().

参见

MsiRecordGetFieldCount [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370366.aspx] MsiRecordSetString [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370373.aspx] MsiRecordSetStream [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370372.aspx] MsiRecordSetInteger [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370371.aspx] MsiRecordClearData [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370364.aspx]

34.1.5. Errors

All wrappers around MSI functions raise MSIError; the string inside the exception will contain more detail.

34.1.6. CAB Objects

class msilib.CAB(name)

The class CAB represents a CAB file. During MSI construction, files will be added simultaneously to the Files table, and to a CAB file. Then, when all files have been added, the CAB file can be written, then added to the MSI file.

name is the name of the CAB file in the MSI file.

append(full, file, logical)

Add the file with the pathname full to the CAB file, under the name logical. If there is already a file named logical, a new file name is created.

Return the index of the file in the CAB file, and the new name of the file inside the CAB file.

commit(database)

Generate a CAB file, add it as a stream to the MSI file, put it into the Media table, and remove the generated file from the disk.

34.1.7. Directory Objects

class msilib.Directory(database, cab, basedir, physical, logical, default[, componentflags])

Create a new directory in the Directory table. There is a current component at each point in time for the directory, which is either explicitly created through start_component(), or implicitly when files are added for the first time. Files are added into the current component, and into the cab file. To create a directory, a base directory object needs to be specified (can be None), the path to the physical directory, and a logical directory name. default specifies the DefaultDir slot in the directory table. componentflags specifies the default flags that new components get.

start_component(component=None, feature=None, flags=None, keyfile=None, uuid=None)

Add an entry to the Component table, and make this component the current component for this directory. If no component name is given, the directory name is used. If no feature is given, the current feature is used. If no flags are given, the directory’s default flags are used. If no keyfile is given, the KeyPath is left null in the Component table.

add_file(file, src=None, version=None, language=None)

Add a file to the current component of the directory, starting a new one if there is no current component. By default, the file name in the source and the file table will be identical. If the src file is specified, it is interpreted relative to the current directory. Optionally, a version and a language can be specified for the entry in the File table.

glob(pattern, exclude=None)

Add a list of files to the current component as specified in the glob pattern. Individual files can be excluded in the exclude list.

remove_pyc()

Remove .pyc files on uninstall.

参见

Directory Table [https://msdn.microsoft.com/en-us/library/windows/desktop/aa368295.aspx] File Table [https://msdn.microsoft.com/en-us/library/windows/desktop/aa368596.aspx] Component Table [https://msdn.microsoft.com/en-us/library/windows/desktop/aa368007.aspx] FeatureComponents Table [https://msdn.microsoft.com/en-us/library/windows/desktop/aa368579.aspx]

34.1.8. 相关特性

class msilib.Feature(db, id, title, desc, display, level=1, parent=None, directory=None, attributes=0)

Add a new record to the Feature table, using the values id, parent.id, title, desc, display, level, directory, and attributes. The resulting feature object can be passed to the start_component() method of Directory.

set_current()

Make this feature the current feature of msilib. New components are automatically added to the default feature, unless a feature is explicitly specified.

参见

Feature Table [https://msdn.microsoft.com/en-us/library/windows/desktop/aa368585.aspx]

34.1.9. GUI classes

msilib provides several classes that wrap the GUI tables in an MSI database. However, no standard user interface is provided; use bdist_msi to create MSI files with a user-interface for installing Python packages.

class msilib.Control(dlg, name)

Base class of the dialog controls. dlg is the dialog object the control belongs to, and name is the control’s name.

event(event, argument, condition=1, ordering=None)

Make an entry into the ControlEvent table for this control.

mapping(event, attribute)

Make an entry into the EventMapping table for this control.

condition(action, condition)

Make an entry into the ControlCondition table for this control.

class msilib.RadioButtonGroup(dlg, name, property)

Create a radio button control named name. property is the installer property that gets set when a radio button is selected.

add(name, x, y, width, height, text, value=None)

Add a radio button named name to the group, at the coordinates x, y, width, height, and with the label text. If value is None, it defaults to name.

class msilib.Dialog(db, name, x, y, w, h, attr, title, first, default, cancel)

Return a new Dialog object. An entry in the Dialog table is made, with the specified coordinates, dialog attributes, title, name of the first, default, and cancel controls.

control(name, type, x, y, width, height, attributes, property, text, control_next, help)

Return a new Control object. An entry in the Control table is made with the specified parameters.

This is a generic method; for specific types, specialized methods are provided.

text(name, x, y, width, height, attributes, text)

Add and return a Text control.

bitmap(name, x, y, width, height, text)

Add and return a Bitmap control.

line(name, x, y, width, height)

Add and return a Line control.

pushbutton(name, x, y, width, height, attributes, text, next_control)

Add and return a PushButton control.

radiogroup(name, x, y, width, height, attributes, property, text, next_control)

Add and return a RadioButtonGroup control.

checkbox(name, x, y, width, height, attributes, property, text, next_control)

Add and return a CheckBox control.

参见

Dialog Table [https://msdn.microsoft.com/en-us/library/windows/desktop/aa368286.aspx] Control Table [https://msdn.microsoft.com/en-us/library/windows/desktop/aa368044.aspx] Control Types [https://msdn.microsoft.com/en-us/library/windows/desktop/aa368039.aspx] ControlCondition Table [https://msdn.microsoft.com/en-us/library/windows/desktop/aa368035.aspx] ControlEvent Table [https://msdn.microsoft.com/en-us/library/windows/desktop/aa368037.aspx] EventMapping Table [https://msdn.microsoft.com/en-us/library/windows/desktop/aa368559.aspx] RadioButton Table [https://msdn.microsoft.com/en-us/library/windows/desktop/aa370962.aspx]

34.1.10. Precomputed tables

msilib provides a few subpackages that contain only schema and table definitions. Currently, these definitions are based on MSI version 2.0.

msilib.schema

This is the standard MSI schema for MSI 2.0, with the tables variable providing a list of table definitions, and _Validation_records providing the data for MSI validation.

msilib.sequence

This module contains table contents for the standard sequence tables: AdminExecuteSequence, AdminUISequence, AdvtExecuteSequence, InstallExecuteSequence, and InstallUISequence.

msilib.text

This module contains definitions for the UIText and ActionText tables, for the standard installer actions.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

34. Windows系统相关模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

34. Windows系统相关模块 »

|

34.2. msvcrt — 来自 MS VC++ 运行时的有用例程

这些函数提供了对 Windows 平台上一些有用功能的访问。一些更高级别的模块使用这些函数来构建其服务的 Windows 实现。 例如， getpass 模块在实现 getpass() 函数时使用了这些函数。

关于这些函数的更多信息可以在平台 API 文档中找到。

该模块实现了控制台 I/O API 的普通和宽字符变体。普通的 API 只处理ASCII字符，国际化应用受限。应该尽可能地使用宽字符 API 。

在 3.3 版更改: 此模块中过去会引发 IOError 的操作现在将引发 OSError。

34.2.1. 文件操作

msvcrt.locking(fd, mode, nbytes)

基于文件描述符 fd 从 C 运行时锁定文件的某一部分。 失败时引发 OSError。 锁定的文件区域从当前文件位置开始扩展 nbytes 个字节，并可能持续到超出文件末尾。 mode 必须为下面列出的 LK_* 之一。 一个文件中的多个区域可以被同时锁定，但是不能重叠。 相邻区域不会被合并；它们必须单独被解锁。

msvcrt.LK_LOCK

msvcrt.LK_RLCK

锁定指定的字节数据。 如果字节数据无法被锁定，程序会在 1 秒之后立即重试。 如果在 10 次尝试后字节数据仍无法被锁定，则会引发 OSError。

msvcrt.LK_NBLCK

msvcrt.LK_NBRLCK

锁定指定的字节数据。 如果字节数据无法被锁定，则会引发 OSError。

msvcrt.LK_UNLCK

解锁指定的字节数据，该对象必须在之前被锁定。

msvcrt.setmode(fd, flags)

设置文件描述符 fd 的行结束符转写模式。 要将其设为文本模式，则 flags 应当为 os.O_TEXT；设为二进制模式，则应当为 os.O_BINARY。

msvcrt.open_osfhandle(handle, flags)

基于文件句柄 handle 创建一个 C 运行时文件描述符。 flags 形参应当 os.O_APPEND, os.O_RDONLY 和 os.O_TEXT 按位 OR 的结果。 返回的文件描述符可以被用作 os.fdopen() 的形参以创建一个文件对象。

msvcrt.get_osfhandle(fd)

返回文件描述符 fd 的文件句柄。 如果 fd 不能被识别则会引发 OSError。

34.2.2. 控制台 I/O

msvcrt.kbhit()

Return true if a keypress is waiting to be read.

msvcrt.getch()

读取一个按键并将结果字符返回为一个字节串。 不会有内容回显到缝制台。 如果还没有任何键被按下此调用将会阻塞，但它将不会等待 Enter 被按下。 如果按下的键是一个特殊功能键，此函数将返回 '\000' 或 '\xe0'；下一次调用将返回键代码。 Control-C 按钮无法使用此函数来读取。

msvcrt.getwch()

getch() 的宽字符版本，返回一个 Unicode 值。

msvcrt.getche()

类似于 getch()，但按键如果表示一个可打印字符则它将被回显。

msvcrt.getwche()

getche() 的宽字符版本，返回一个 Unicode 值。

msvcrt.putch(char)

将字符串 char 打印到终端，不使用缓冲区。

msvcrt.putwch(unicode_char)

putch() 的宽字符版本，接受一个 Unicode 值。

msvcrt.ungetch(char)

使得字节串 char 被“推回”终端缓冲区；它将是被 getch() 或 getche() 读取的下一个字符。

msvcrt.ungetwch(unicode_char)

ungetch() 的宽字符版本，接受一个 Unicode 值。

34.2.3. 其他函数

msvcrt.heapmin()

强制 malloc() 堆清空自身并将未使用的块返回给操作系统。 失败时，这将引发 OSError。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

34. Windows系统相关模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

34. Windows系统相关模块 »

|

34.3. winreg — Windows 注册表访问

这些函数将 Windows 注册表 API 暴露给 Python。为了确保即便程序员忽略了显式关闭句柄，该句柄依然能够正确关闭，它使用了一个 handle 对象 而不是整数来作为注册表句柄。

在 3.3 版更改: 该模块中的几个函数被用于引发 WindowsError，该异常现在是 OSError 的别名。

34.3.1. 函数

该模块提供了下列函数：

winreg.CloseKey(hkey)

关闭之前打开的注册表键。参数 hkey 指之前打开的键。

注解

如果没有使用该方法关闭 hkey (或者通过 hkey.Close())，在对象 hkey 被 Python 销毁时会将其关闭。

winreg.ConnectRegistry(computer_name, key)

建立到另一台计算上上的预定义注册表句柄的连接，并返回一个 handle 对象.

computer_name 是远程计算机的名称，以 r"\\computername" 的形式。如果是 None ，将会使用本地计算机。

key 是所连接到的预定义句柄。

返回值是所开打键的句柄。如果函数失败，则引发一个 OSError 异常。

在 3.3 版更改: 参考 上文。

winreg.CreateKey(key, sub_key)

创建或打开特定的键，返回一个 handle 对象。

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

sub_key 是用于命名该方法所打开或创建的键的字符串。

如果 key 是预定义键之一，sub_key 可能会是 None。该情况下，返回的句柄就是传入函数的句柄。

如果键已经存在，则该函数打开已经存在的该键。

返回值是所开打键的句柄。如果函数失败，则引发一个 OSError 异常。

在 3.3 版更改: 参考 上文。

winreg.CreateKeyEx(key, sub_key, reserved=0, access=KEY_WRITE)

创建或打开特定的键，返回一个 handle 对象。

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

sub_key 是用于命名该方法所打开或创建的键的字符串。

reserved 是一个保留的证书，必须是零。默认值为零。

access 为一个整数，用于给键的预期安全访问指定访问掩码。默认值为 KEY_WRITE。 参阅 Access Rights 了解其它允许值。

如果 key 是预定义键之一，sub_key 可能会是 None。该情况下，返回的句柄就是传入函数的句柄。

如果键已经存在，则该函数打开已经存在的该键。

返回值是所开打键的句柄。如果函数失败，则引发一个 OSError 异常。

3.2 新版功能.

在 3.3 版更改: 参考 上文。

winreg.DeleteKey(key, sub_key)

删除指定的键。

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

sub_key 这个字符串必须是由 key 参数所指定键的一个子项。该值项不可以是 None，同时键也不可以有子项。

该方法不能删除带有子项的键。

如果方法成功，则整个键，包括其所有值项都会被移除。如果方法失败，则引发一个 OSError 异常。

在 3.3 版更改: 参考 上文。

winreg.DeleteKeyEx(key, sub_key, access=KEY_WOW64_64KEY, reserved=0)

删除指定的键。

注解

函数 DeleteKeyEx() 通过 RegDeleteKeyEx 这个 Windows API 函数实现，该函数为 Windows 的64位版本专属。 参阅 RegDeleteKeyEx 文档 [https://msdn.microsoft.com/en-us/library/ms724847%28VS.85%29.aspx]。

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

sub_key 这个字符串必须是由 key 参数所指定键的一个子项。该值项不可以是 None，同时键也不可以有子项。

reserved 是一个保留的证书，必须是零。默认值为零。

access 为一个整数，用于给键的预期安全访问指定访问掩码。默认值为常量 _WOW64_64KEY 。参阅 Access Rights 了解其它允许值。

该方法不能删除带有子项的键。

如果方法成功，则整个键，包括其所有值项都会被移除。如果方法失败，则引发一个 OSError 异常。

在不支持的 Windows 版本之上，将会引发 NotImplementedError 异常。

3.2 新版功能.

在 3.3 版更改: 参考 上文。

winreg.DeleteValue(key, value)

从某个注册键中删除一个命名值项。

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

value 为标识所要删除值项的字符串。

winreg.EnumKey(key, index)

列举某个已经打开注册表键的子项，并返回一个字符串。

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

index 为一个整数，用于标识所获取键的索引。

每次调用该函数都会获取一个子项的名字。通常它会被反复调用，直到引发 OSError 异常，这说明已经没有更多的可用值了。

在 3.3 版更改: 参考 上文。

winreg.EnumValue(key, index)

列举某个已经打开注册表键的值项，并返回一个元组。

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

index 为一个整数，用于标识要获取值项的索引。

每次调用该函数都会获取一个子项的名字。通常它会被反复调用，直到引发 OSError 异常，这说明已经没有更多的可用值了。

结果为3元素的元组。

索引

含义

0

用于标识值项名称的字符串。

1

保存值项数据的对象，其类型取决于背后的注册表类型。

2

标识值项数据类型的整数。（请查阅 SetValueEx() 文档中的表格）

在 3.3 版更改: 参考 上文。

winreg.ExpandEnvironmentStrings(str)

Expands environment variable placeholders %NAME% in strings like REG_EXPAND_SZ:

>>> ExpandEnvironmentStrings('%windir%')
'C:\\Windows'

winreg.FlushKey(key)

将某个键的所有属性写入注册表。

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

It is not necessary to call FlushKey() to change a key. Registry changes are flushed to disk by the registry using its lazy flusher. Registry changes are also flushed to disk at system shutdown. Unlike CloseKey(), the FlushKey() method returns only when all the data has been written to the registry. An application should only call FlushKey() if it requires absolute certainty that registry changes are on disk.

注解

If you don’t know whether a FlushKey() call is required, it probably isn’t.

winreg.LoadKey(key, sub_key, file_name)

Creates a subkey under the specified key and stores registration information from a specified file into that subkey.

key is a handle returned by ConnectRegistry() or one of the constants HKEY_USERS or HKEY_LOCAL_MACHINE.

sub_key is a string that identifies the subkey to load.

file_name is the name of the file to load registry data from. This file must have been created with the SaveKey() function. Under the file allocation table (FAT) file system, the filename may not have an extension.

A call to LoadKey() fails if the calling process does not have the SE_RESTORE_PRIVILEGE privilege. Note that privileges are different from permissions – see the RegLoadKey documentation [https://msdn.microsoft.com/en-us/library/ms724889%28v=VS.85%29.aspx] for more details.

If key is a handle returned by ConnectRegistry(), then the path specified in file_name is relative to the remote computer.

winreg.OpenKey(key, sub_key, reserved=0, access=KEY_READ)

winreg.OpenKeyEx(key, sub_key, reserved=0, access=KEY_READ)

Opens the specified key, returning a handle object.

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

sub_key is a string that identifies the sub_key to open.

reserved is a reserved integer, and must be zero. The default is zero.

access is an integer that specifies an access mask that describes the desired security access for the key. Default is KEY_READ. See Access Rights for other allowed values.

The result is a new handle to the specified key.

If the function fails, OSError is raised.

在 3.2 版更改: Allow the use of named arguments.

在 3.3 版更改: 参考 上文。

winreg.QueryInfoKey(key)

Returns information about a key, as a tuple.

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

结果为3元素的元组。

索引

含义

0

An integer giving the number of sub keys this key has.

1

An integer giving the number of values this key has.

2

An integer giving when the key was last modified (if available) as 100’s of nanoseconds since Jan 1, 1601.

winreg.QueryValue(key, sub_key)

Retrieves the unnamed value for a key, as a string.

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

sub_key is a string that holds the name of the subkey with which the value is associated. If this parameter is None or empty, the function retrieves the value set by the SetValue() method for the key identified by key.

Values in the registry have name, type, and data components. This method retrieves the data for a key’s first value that has a NULL name. But the underlying API call doesn’t return the type, so always use QueryValueEx() if possible.

winreg.QueryValueEx(key, value_name)

Retrieves the type and data for a specified value name associated with an open registry key.

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

value_name is a string indicating the value to query.

The result is a tuple of 2 items:

索引

含义

0

The value of the registry item.

1

An integer giving the registry type for this value (see table in docs for SetValueEx())

winreg.SaveKey(key, file_name)

Saves the specified key, and all its subkeys to the specified file.

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

file_name is the name of the file to save registry data to. This file cannot already exist. If this filename includes an extension, it cannot be used on file allocation table (FAT) file systems by the LoadKey() method.

If key represents a key on a remote computer, the path described by file_name is relative to the remote computer. The caller of this method must possess the SeBackupPrivilege security privilege. Note that privileges are different than permissions – see the Conflicts Between User Rights and Permissions documentation [https://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx] for more details.

This function passes NULL for security_attributes to the API.

winreg.SetValue(key, sub_key, type, value)

Associates a value with a specified key.

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

sub_key is a string that names the subkey with which the value is associated.

type is an integer that specifies the type of the data. Currently this must be REG_SZ, meaning only strings are supported. Use the SetValueEx() function for support for other data types.

value is a string that specifies the new value.

If the key specified by the sub_key parameter does not exist, the SetValue function creates it.

Value lengths are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the filenames stored in the configuration registry. This helps the registry perform efficiently.

The key identified by the key parameter must have been opened with KEY_SET_VALUE access.

winreg.SetValueEx(key, value_name, reserved, type, value)

Stores data in the value field of an open registry key.

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

value_name is a string that names the subkey with which the value is associated.

reserved can be anything – zero is always passed to the API.

type is an integer that specifies the type of the data. See Value Types for the available types.

value is a string that specifies the new value.

This method can also set additional value and type information for the specified key. The key identified by the key parameter must have been opened with KEY_SET_VALUE access.

To open the key, use the CreateKey() or OpenKey() methods.

Value lengths are limited by available memory. Long values (more than 2048 bytes) should be stored as files with the filenames stored in the configuration registry. This helps the registry perform efficiently.

winreg.DisableReflectionKey(key)

Disables registry reflection for 32-bit processes running on a 64-bit operating system.

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

Will generally raise NotImplemented if executed on a 32-bit operating system.

If the key is not on the reflection list, the function succeeds but has no effect. Disabling reflection for a key does not affect reflection of any subkeys.

winreg.EnableReflectionKey(key)

Restores registry reflection for the specified disabled key.

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

Will generally raise NotImplemented if executed on a 32-bit operating system.

Restoring reflection for a key does not affect reflection of any subkeys.

winreg.QueryReflectionKey(key)

Determines the reflection state for the specified key.

key 为某个已经打开的键，或者预定义的 HKEY_* 常量 之一。

Returns True if reflection is disabled.

Will generally raise NotImplemented if executed on a 32-bit operating system.

34.3.2. 常量

The following constants are defined for use in many _winreg functions.

34.3.2.1. HKEY_* Constants

winreg.HKEY_CLASSES_ROOT

Registry entries subordinate to this key define types (or classes) of documents and the properties associated with those types. Shell and COM applications use the information stored under this key.

winreg.HKEY_CURRENT_USER

Registry entries subordinate to this key define the preferences of the current user. These preferences include the settings of environment variables, data about program groups, colors, printers, network connections, and application preferences.

winreg.HKEY_LOCAL_MACHINE

Registry entries subordinate to this key define the physical state of the computer, including data about the bus type, system memory, and installed hardware and software.

winreg.HKEY_USERS

Registry entries subordinate to this key define the default user configuration for new users on the local computer and the user configuration for the current user.

winreg.HKEY_PERFORMANCE_DATA

Registry entries subordinate to this key allow you to access performance data. The data is not actually stored in the registry; the registry functions cause the system to collect the data from its source.

winreg.HKEY_CURRENT_CONFIG

Contains information about the current hardware profile of the local computer system.

winreg.HKEY_DYN_DATA

This key is not used in versions of Windows after 98.

34.3.2.2. Access Rights

For more information, see Registry Key Security and Access [https://msdn.microsoft.com/en-us/library/ms724878%28v=VS.85%29.aspx].

winreg.KEY_ALL_ACCESS

Combines the STANDARD_RIGHTS_REQUIRED, KEY_QUERY_VALUE, KEY_SET_VALUE, KEY_CREATE_SUB_KEY, KEY_ENUMERATE_SUB_KEYS, KEY_NOTIFY, and KEY_CREATE_LINK access rights.

winreg.KEY_WRITE

Combines the STANDARD_RIGHTS_WRITE, KEY_SET_VALUE, and KEY_CREATE_SUB_KEY access rights.

winreg.KEY_READ

Combines the STANDARD_RIGHTS_READ, KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS, and KEY_NOTIFY values.

winreg.KEY_EXECUTE

Equivalent to KEY_READ.

winreg.KEY_QUERY_VALUE

Required to query the values of a registry key.

winreg.KEY_SET_VALUE

Required to create, delete, or set a registry value.

winreg.KEY_CREATE_SUB_KEY

Required to create a subkey of a registry key.

winreg.KEY_ENUMERATE_SUB_KEYS

Required to enumerate the subkeys of a registry key.

winreg.KEY_NOTIFY

Required to request change notifications for a registry key or for subkeys of a registry key.

winreg.KEY_CREATE_LINK

Reserved for system use.

34.3.2.2.1. 64-bit Specific

For more information, see Accessing an Alternate Registry View [https://msdn.microsoft.com/en-us/library/aa384129(v=VS.85).aspx].

winreg.KEY_WOW64_64KEY

Indicates that an application on 64-bit Windows should operate on the 64-bit registry view.

winreg.KEY_WOW64_32KEY

Indicates that an application on 64-bit Windows should operate on the 32-bit registry view.

34.3.2.3. Value Types

For more information, see Registry Value Types [https://msdn.microsoft.com/en-us/library/ms724884%28v=VS.85%29.aspx].

winreg.REG_BINARY

Binary data in any form.

winreg.REG_DWORD

32-bit number.

winreg.REG_DWORD_LITTLE_ENDIAN

A 32-bit number in little-endian format. Equivalent to REG_DWORD.

winreg.REG_DWORD_BIG_ENDIAN

A 32-bit number in big-endian format.

winreg.REG_EXPAND_SZ

Null-terminated string containing references to environment variables (%PATH%).

winreg.REG_LINK

A Unicode symbolic link.

winreg.REG_MULTI_SZ

A sequence of null-terminated strings, terminated by two null characters. (Python handles this termination automatically.)

winreg.REG_NONE

No defined value type.

winreg.REG_QWORD

A 64-bit number.

3.6 新版功能.

winreg.REG_QWORD_LITTLE_ENDIAN

A 64-bit number in little-endian format. Equivalent to REG_QWORD.

3.6 新版功能.

winreg.REG_RESOURCE_LIST

A device-driver resource list.

winreg.REG_FULL_RESOURCE_DESCRIPTOR

A hardware setting.

winreg.REG_RESOURCE_REQUIREMENTS_LIST

A hardware resource list.

winreg.REG_SZ

A null-terminated string.

34.3.3. Registry Handle Objects

This object wraps a Windows HKEY object, automatically closing it when the object is destroyed. To guarantee cleanup, you can call either the Close() method on the object, or the CloseKey() function.

All registry functions in this module return one of these objects.

All registry functions in this module which accept a handle object also accept an integer, however, use of the handle object is encouraged.

Handle objects provide semantics for __bool__() – thus

if handle:
    print("Yes")

will print Yes if the handle is currently valid (has not been closed or detached).

The object also support comparison semantics, so handle objects will compare true if they both reference the same underlying Windows handle value.

Handle objects can be converted to an integer (e.g., using the built-in int() function), in which case the underlying Windows handle value is returned. You can also use the Detach() method to return the integer handle, and also disconnect the Windows handle from the handle object.

PyHKEY.Close()

Closes the underlying Windows handle.

If the handle is already closed, no error is raised.

PyHKEY.Detach()

Detaches the Windows handle from the handle object.

The result is an integer that holds the value of the handle before it is detached. If the handle is already detached or closed, this will return zero.

After calling this function, the handle is effectively invalidated, but the handle is not closed. You would call this function when you need the underlying Win32 handle to exist beyond the lifetime of the handle object.

PyHKEY.__enter__()

PyHKEY.__exit__(*exc_info)

The HKEY object implements __enter__() and __exit__() and thus supports the context protocol for the with statement:

with OpenKey(HKEY_LOCAL_MACHINE, "foo") as key:
    ...  # work with key

will automatically close key when control leaves the with block.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

34. Windows系统相关模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

34. Windows系统相关模块 »

|

34.4. winsound — Sound-playing interface for Windows

The winsound module provides access to the basic sound-playing machinery provided by Windows platforms. It includes functions and several constants.

winsound.Beep(frequency, duration)

Beep the PC’s speaker. The frequency parameter specifies frequency, in hertz, of the sound, and must be in the range 37 through 32,767. The duration parameter specifies the number of milliseconds the sound should last. If the system is not able to beep the speaker, RuntimeError is raised.

winsound.PlaySound(sound, flags)

Call the underlying PlaySound() function from the Platform API. The sound parameter may be a filename, a system sound alias, audio data as a bytes-like object, or None. Its interpretation depends on the value of flags, which can be a bitwise ORed combination of the constants described below. If the sound parameter is None, any currently playing waveform sound is stopped. If the system indicates an error, RuntimeError is raised.

winsound.MessageBeep(type=MB_OK)

Call the underlying MessageBeep() function from the Platform API. This plays a sound as specified in the registry. The type argument specifies which sound to play; possible values are -1, MB_ICONASTERISK, MB_ICONEXCLAMATION, MB_ICONHAND, MB_ICONQUESTION, and MB_OK, all described below. The value -1 produces a “simple beep”; this is the final fallback if a sound cannot be played otherwise. If the system indicates an error, RuntimeError is raised.

winsound.SND_FILENAME

The sound parameter is the name of a WAV file. Do not use with SND_ALIAS.

winsound.SND_ALIAS

The sound parameter is a sound association name from the registry. If the registry contains no such name, play the system default sound unless SND_NODEFAULT is also specified. If no default sound is registered, raise RuntimeError. Do not use with SND_FILENAME.

All Win32 systems support at least the following; most systems support many more:

PlaySound() name

Corresponding Control Panel Sound name

'SystemAsterisk'

Asterisk

'SystemExclamation'

Exclamation

'SystemExit'

Exit Windows

'SystemHand'

Critical Stop

'SystemQuestion'

Question

例如

import winsound
# Play Windows exit sound.
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)

# Probably play Windows default sound, if any is registered (because
# "*" probably isn't the registered name of any sound).
winsound.PlaySound("*", winsound.SND_ALIAS)

winsound.SND_LOOP

Play the sound repeatedly. The SND_ASYNC flag must also be used to avoid blocking. Cannot be used with SND_MEMORY.

winsound.SND_MEMORY

The sound parameter to PlaySound() is a memory image of a WAV file, as a bytes-like object.

注解

This module does not support playing from a memory image asynchronously, so a combination of this flag and SND_ASYNC will raise RuntimeError.

winsound.SND_PURGE

Stop playing all instances of the specified sound.

注解

This flag is not supported on modern Windows platforms.

winsound.SND_ASYNC

Return immediately, allowing sounds to play asynchronously.

winsound.SND_NODEFAULT

If the specified sound cannot be found, do not play the system default sound.

winsound.SND_NOSTOP

Do not interrupt sounds currently playing.

winsound.SND_NOWAIT

Return immediately if the sound driver is busy.

注解

This flag is not supported on modern Windows platforms.

winsound.MB_ICONASTERISK

Play the SystemDefault sound.

winsound.MB_ICONEXCLAMATION

Play the SystemExclamation sound.

winsound.MB_ICONHAND

Play the SystemHand sound.

winsound.MB_ICONQUESTION

Play the SystemQuestion sound.

winsound.MB_OK

Play the SystemDefault sound.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

34. Windows系统相关模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

35. Unix 专有服务

本章描述的模块提供了 Unix 操作系统独有特性的接口，在某些情况下也适用于它的某些或许多衍生版。 以下为模块概览：

35.1. posix — 最常见的 POSIX 系统调用

35.1.1. 大文件支持

35.1.2. 重要的模块内容

35.2. pwd — 用户密码数据库

35.3. spwd — The shadow password database

35.4. grp — 组数据库

35.5. crypt — Function to check Unix passwords

35.5.1. Hashing Methods

35.5.2. Module Attributes

35.5.3. 模块函数

35.5.4. 例子

35.6. termios — POSIX 风格的 tty 控制

35.6.1. 示例

35.7. tty — 终端控制功能

35.8. pty — 伪终端工具

35.8.1. 示例

35.9. fcntl — The fcntl and ioctl system calls

35.10. pipes — 终端管道接口

35.10.1. 模板对象

35.11. resource — Resource usage information

35.11.1. Resource Limits

35.11.2. Resource Usage

35.12. nis — Sun 的 NIS (黄页) 接口

35.13. Unix syslog 库例程

35.13.1. 例子

35.13.1.1. 简单示例

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

35.1. posix — 最常见的 POSIX 系统调用

此模块提供了对基于 C 标准和 POSIX 标准（一种稍加修改的 Unix 接口）进行标准化的系统功能的访问。

请勿直接导入此模块。 而应导入 os 模块，它提供了此接口的 可移植 版本。 在 Unix 上，os 模块提供了 posix 接口的一个超集。 在非 Unix 操作系统上 posix 模块将不可用，但会通过 os 接口提供它的一个可用子集。 一旦导入了 os，用它替代 posix 时就 没有 性能惩罚。 此外，os 还提供了一些附加功能，例如在 os.environ 中的某个条目被修改时会自动调用 putenv()。

错误将作为异常被报告；对于类型错误会给出普通异常，而系统调用所报告的异常则会引发 OSError。

35.1.1. 大文件支持

某些操作系统（包括 AIX, HP-UX, Irix 和 Solaris）可对 int 和 long 为 32 位值的 C 编程模型提供大于 2 GiB 的文件的支持。 这在通常情况下是以将相关数据长度和偏移类型定义为 64 位值的方式来实现的。 这样的文件有时被称为 大文件。

Large file support is enabled in Python when the size of an off_t is larger than a long and the long long type is available and is at least as large as an off_t. It may be necessary to configure and compile Python with certain compiler flags to enable this mode. For example, it is enabled by default with recent versions of Irix, but with Solaris 2.6 and 2.7 you need to do something like:

CFLAGS="`getconf LFS_CFLAGS`" OPT="-g -O2 $CFLAGS" \
        ./configure

在支持大文件的 Linux 系统中，可以这样做:

CFLAGS='-D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64' OPT="-g -O2 $CFLAGS" \
        ./configure

35.1.2. 重要的模块内容

除了 os 模块文档已说明的许多函数，posix 还定义了下列数据项:

posix.environ

一个表示解释器启动时间点的字符串环境的字典。 键和值的类型在Unix 上为 bytes 而在 Windows 上为 str。 例如，environ[b'HOME'] (Windows 上的 environ['HOME']) 是你的家目录的路径名，等价于 C 中的 getenv("HOME")。

修改此字典不会影响由 execv(), popen() 或 system() 所传入的字符串环境；如果你需要修改环境，请将 environ 传给 execve() 或者为 system() 或 popen() 的命令字符串添加变量赋值和 export 语句。

在 3.2 版更改: 在 Unix 上，键和值为 bytes 类型。

注解

os 模块提供了对 environ 的替代实现，它会在被修改时更新环境。 还要注意更新 os.environ 将导致此字典失效。 推荐使用这个 os 模块版本而不是直接访问 posix 模块。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

35.2. pwd — 用户密码数据库

此模块可以访问 Unix 用户账户名及密码数据库，在所有 Unix 版本上均可使用。

密码数据库中的条目以元组对象返回，属性对应 passwd 中的结构（属性如下所示，可参考 <pwd.h>）：

索引

属性

含义

0

pw_name

登录名

1

pw_passwd

密码，可能已经加密

2

pw_uid

用户 ID 数值

3

pw_gid

组 ID 数值

4

pw_gecos

用户名或备注

5

pw_dir

用户主目录

6

pw_shell

用户的命令解释器

其中 uid 和 gid 是整数，其他是字符串，如果找不到对应的项目，抛出 KeyError 异常。

注解

传统的 Unix 系统中，pw_passwd 的值通常使用 DES 导出的算法加密（参阅 crypt 模块）。不过现在的 unix 系统使用 影子密码 系统。在这些 unix 上，pw_passwd 只包含星号（'*'）或字母（'x'），而加密的密码存储在文件 /etc/shadow 中，此文件不是全局可读的。在 pw_passwd 中是否包含有用信息是系统相关的。如果可以访问到加密的密码，就需要使用 spwd 模块了。

本模块定义如下内容：

pwd.getpwuid(uid)

给定用户的数值 ID，返回密码数据库的对应项目。

pwd.getpwnam(name)

给定用户名，返回密码数据库的对应项目。

pwd.getpwall()

返回密码数据库中所有项目的列表，顺序不是固定的。

参见

模块 grp

针对用户组数据库的接口，与本模块类似。

模块 spwd

针对影子密码数据库的接口，与本模块类似。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

35.3. spwd — The shadow password database

This module provides access to the Unix shadow password database. It is available on various Unix versions.

You must have enough privileges to access the shadow password database (this usually means you have to be root).

Shadow password database entries are reported as a tuple-like object, whose attributes correspond to the members of the spwd structure (Attribute field below, see <shadow.h>):

索引

属性

含义

0

sp_namp

登录名

1

sp_pwdp

Encrypted password

2

sp_lstchg

Date of last change

3

sp_min

Minimal number of days between changes

4

sp_max

Maximum number of days between changes

5

sp_warn

Number of days before password expires to warn user about it

6

sp_inact

Number of days after password expires until account is disabled

7

sp_expire

Number of days since 1970-01-01 when account expires

8

sp_flag

Reserved

The sp_namp and sp_pwdp items are strings, all others are integers. KeyError is raised if the entry asked for cannot be found.

定义了以下函数：

spwd.getspnam(name)

Return the shadow password database entry for the given user name.

在 3.6 版更改: Raises a PermissionError instead of KeyError if the user doesn’t have privileges.

spwd.getspall()

Return a list of all available shadow password database entries, in arbitrary order.

参见

模块 grp

针对用户组数据库的接口，与本模块类似。

模块 pwd

An interface to the normal password database, similar to this.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

35.4. grp — 组数据库

该模块提供对Unix组数据库的访问。 它在所有Unix版本上都可用。

组数据库条目被报告为类似元组的对象，其属性对应于 group 结构的成员（下面的属性字段，请参见 <pwd.h> ）：

索引

属性

含义

0

gr_name

组名

1

gr_passwd

（加密的）组密码； 通常为空

2

gr_gid

数字组ID

3

gr_mem

组内所有成员的用户名

gid 是整数，名称和密码是字符串，成员列表是字符串列表。 （注意，大多数用户未根据密码数据库显式列为所属组的成员。请检查两个数据库以获取完整的成员资格信息。还要注意，以 + 或 - 开头的 gr_name 可能是 YP/NIS 引用，可能无法通过 getgrnam() 或 getgrgid() 访问。）

本模块定义如下内容：

grp.getgrgid(gid)

返回给定数字组 ID 的组数据库条目。 如果请求的条目无法找到则会引发 KeyError。

3.6 版后已移除: 从 Python 3.6 开始，不再支持对 getgrgid() 中的 float 或 string 等非 integer 参数的支持。

grp.getgrnam(name)

返回给定组名的组数据库条目。 如果找不到要求的条目，则会引发 KeyError 错误。

grp.getgrall()

以任意顺序返回所有可用组条目的列表。

参见

模块 pwd

用户数据库的接口，与此类似。

模块 spwd

针对影子密码数据库的接口，与本模块类似。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

35.5. crypt — Function to check Unix passwords

Source code: Lib/crypt.py [https://github.com/python/cpython/tree/3.6/Lib/crypt.py]

This module implements an interface to the crypt(3) routine, which is a one-way hash function based upon a modified DES algorithm; see the Unix man page for further details. Possible uses include storing hashed passwords so you can check passwords without storing the actual password, or attempting to crack Unix passwords with a dictionary.

Notice that the behavior of this module depends on the actual implementation of the crypt(3) routine in the running system. Therefore, any extensions available on the current implementation will also be available on this module.

35.5.1. Hashing Methods

3.3 新版功能.

The crypt module defines the list of hashing methods (not all methods are available on all platforms):

crypt.METHOD_SHA512

A Modular Crypt Format method with 16 character salt and 86 character hash. This is the strongest method.

crypt.METHOD_SHA256

Another Modular Crypt Format method with 16 character salt and 43 character hash.

crypt.METHOD_MD5

Another Modular Crypt Format method with 8 character salt and 22 character hash.

crypt.METHOD_CRYPT

The traditional method with a 2 character salt and 13 characters of hash. This is the weakest method.

35.5.2. Module Attributes

3.3 新版功能.

crypt.methods

A list of available password hashing algorithms, as crypt.METHOD_* objects. This list is sorted from strongest to weakest.

35.5.3. 模块函数

The crypt module defines the following functions:

crypt.crypt(word, salt=None)

word will usually be a user’s password as typed at a prompt or in a graphical interface. The optional salt is either a string as returned from mksalt(), one of the crypt.METHOD_* values (though not all may be available on all platforms), or a full encrypted password including salt, as returned by this function. If salt is not provided, the strongest method will be used (as returned by methods()).

Checking a password is usually done by passing the plain-text password as word and the full results of a previous crypt() call, which should be the same as the results of this call.

salt (either a random 2 or 16 character string, possibly prefixed with $digit$ to indicate the method) which will be used to perturb the encryption algorithm. The characters in salt must be in the set [./a-zA-Z0-9], with the exception of Modular Crypt Format which prefixes a $digit$.

Returns the hashed password as a string, which will be composed of characters from the same alphabet as the salt.

Since a few crypt(3) extensions allow different values, with different sizes in the salt, it is recommended to use the full crypted password as salt when checking for a password.

在 3.3 版更改: Accept crypt.METHOD_* values in addition to strings for salt.

crypt.mksalt(method=None)

Return a randomly generated salt of the specified method. If no method is given, the strongest method available as returned by methods() is used.

The return value is a string either of 2 characters in length for crypt.METHOD_CRYPT, or 19 characters starting with $digit$ and 16 random characters from the set [./a-zA-Z0-9], suitable for passing as the salt argument to crypt().

3.3 新版功能.

35.5.4. 例子

A simple example illustrating typical use (a constant-time comparison operation is needed to limit exposure to timing attacks. hmac.compare_digest() is suitable for this purpose):

import pwd
import crypt
import getpass
from hmac import compare_digest as compare_hash

def login():
    username = input('Python login: ')
    cryptedpasswd = pwd.getpwnam(username)[1]
    if cryptedpasswd:
        if cryptedpasswd == 'x' or cryptedpasswd == '*':
            raise ValueError('no support for shadow passwords')
        cleartext = getpass.getpass()
        return compare_hash(crypt.crypt(cleartext, cryptedpasswd), cryptedpasswd)
    else:
        return True

To generate a hash of a password using the strongest available method and check it against the original:

import crypt
from hmac import compare_digest as compare_hash

hashed = crypt.crypt(plaintext)
if not compare_hash(hashed, crypt.crypt(plaintext, hashed)):
    raise ValueError("hashed version doesn't validate against original")

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

35.6. termios — POSIX 风格的 tty 控制

此模块提供了针对tty I/O 控制的 POSIX 调用的接口。 有关此类调用的完整描述，请参阅 termios(3) Unix 指南页。 它仅在当安装时配置了支持 POSIX termios 风格的 tty I/O 控制的 Unix 版本上可用。

此模块中的所有函数均接受一个文件描述符 fd 作为第一个参数。 这可以是一个整数形式的文件描述符，例如 sys.stdin.fileno() 所返回的对象，或是一个 file object，例如 sys.stdin 本身。

这个模块还定义了与此处所提供的函数一起使用的所有必要的常量；这些常量与它们在 C 中的对应常量同名。 请参考你的系统文档了解有关如何使用这些终端控制接口的更多信息。

这个模块定义了以下函数：

termios.tcgetattr(fd)

对于文件描述符 fd 返回一个包含 tty 属性的列表，形式如下: [iflag, oflag, cflag, lflag, ispeed, ospeed, cc]，其中 cc 为一个包含 tty 特殊字符的列表（每一项都是长度为 1 的字符串，索引号为 VMIN 和 VTIME 的项除外，这些字段如有定义则应为整数）。 对旗标和速度以及 cc 数组中索引的解读必须使用在 termios 模块中定义的符号常量来完成。

termios.tcsetattr(fd, when, attributes)

根据 attributes 列表设置文件描述符 fd 的 tty 属性，该列表即 tcgetattr() 所返回的对象。 when 参数确定何时改变属性: TCSANOW 表示立即改变，TCSADRAIN 表示在传输所有队列输出后再改变，或 TCSAFLUSH 表示在传输所有队列输出并丢失所有队列输入后再改变。

termios.tcsendbreak(fd, duration)

在文件描述符 fd 上发送一个中断。 duration 为零表示发送时长为 0.25–0.5 秒的中断；duration 非零值的含义取决于具体系统。

termios.tcdrain(fd)

进入等待状态直到写入文件描述符 fd 的所有输出都传送完毕。

termios.tcflush(fd, queue)

在文件描述符 fd 上丢弃队列数据。 queue 选择器指定哪个队列: TCIFLUSH 表示输入队列，TCOFLUSH 表示输出队列，或 TCIOFLUSH 表示两个队列同时。

termios.tcflow(fd, action)

在文件描述符 fd 上挂起一战恢复输入或输出。 action 参数可以为 TCOOFF 表示挂起输出，TCOON 表示重启输出，TCIOFF 表示挂起输入，或 TCION 表示重启输入。

参见

模块 tty

针对常用终端控制操作的便捷函数。

35.6.1. 示例

这个函数可提示输入密码并且关闭回显。 请注意其采取的技巧是使用一个单独的 tcgetattr() 调用和一个 try … finally 语句来确保旧的 tty 属性无论在何种情况下都会被原样保存:

def getpass(prompt="Password: "):
    import termios, sys
    fd = sys.stdin.fileno()
    old = termios.tcgetattr(fd)
    new = termios.tcgetattr(fd)
    new[3] = new[3] & ~termios.ECHO          # lflags
    try:
        termios.tcsetattr(fd, termios.TCSADRAIN, new)
        passwd = input(prompt)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old)
    return passwd

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

35.7. tty — 终端控制功能

Source code: Lib/tty.py [https://github.com/python/cpython/tree/3.6/Lib/tty.py]

tty 模块定义了将 tty 放入 cbreak 和 raw 模式的函数。

因为它需要 termios 模块，所以只能在 Unix 上运行。

tty 模块定义了以下函数：

tty.setraw(fd, when=termios.TCSAFLUSH)

将文件描述符 fd 的模式更改为 raw 。如果 when 被省略，则默认为 termios.TCSAFLUSH ，并传递给 termios.tcsetattr() 。

tty.setcbreak(fd, when=termios.TCSAFLUSH)

将文件描述符 fd 的模式更改为 cbreak 。如果 when 被省略，则默认为 termios.TCSAFLUSH ，并传递给 termios.tcsetattr() 。

参见

模块 termios

低级终端控制接口。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

35.8. pty — 伪终端工具

源代码: Lib/pty.py [https://github.com/python/cpython/tree/3.6/Lib/pty.py]

pty 模块定义了一些处理“伪终端”概念的操作：启动另一个进程并能以程序方式在其控制终端中进行读写。

由于伪终端处理高度依赖于具体平台，因此此功能只有针对 Linux 的代码。 （Linux 代码也可在其他平台上工作，但是未经测试。）

pty 模块定义了下列函数:

pty.fork()

分叉。 将子进程的控制终端连接到一个伪终端。 返回值为 (pid, fd)。 请注意子进程获得 pid 0 而 fd 为 invalid。 父进程返回值为子进程的 pid 而 fd 为一个连接到子进程的控制终端（并同时连接到子进程的标准输入和输出）的文件描述符。

pty.openpty()

打开一个新的伪终端对，如果可能将使用 os.openpty()，或是针对通用 Unix 系统的模拟代码。 返回一个文件描述符对 (master, slave)，分别表示主从两端。

pty.spawn(argv[, master_read[, stdin_read]])

Spawn a process, and connect its controlling terminal with the current process’s standard io. This is often used to baffle programs which insist on reading from the controlling terminal.

The functions master_read and stdin_read should be functions which read from a file descriptor. The defaults try to read 1024 bytes each time they are called.

在 3.4 版更改: spawn() 现在会从子进程的 os.waitpid() 返回状态值。

35.8.1. 示例

以下程序的作用类似于 Unix 命令 script(1)，它使用一个伪终端来记录一个 “typescript” 里终端进程的所有输入和输出:

import argparse
import os
import pty
import sys
import time

parser = argparse.ArgumentParser()
parser.add_argument('-a', dest='append', action='store_true')
parser.add_argument('-p', dest='use_python', action='store_true')
parser.add_argument('filename', nargs='?', default='typescript')
options = parser.parse_args()

shell = sys.executable if options.use_python else os.environ.get('SHELL', 'sh')
filename = options.filename
mode = 'ab' if options.append else 'wb'

with open(filename, mode) as script:
    def read(fd):
        data = os.read(fd, 1024)
        script.write(data)
        return data

    print('Script started, file is', filename)
    script.write(('Script started on %s\n' % time.asctime()).encode())

    pty.spawn(shell, read)

    script.write(('Script done on %s\n' % time.asctime()).encode())
    print('Script done, file is', filename)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

35.9. fcntl — The fcntl and ioctl system calls

This module performs file control and I/O control on file descriptors. It is an interface to the fcntl() and ioctl() Unix routines. For a complete description of these calls, see fcntl(2) and ioctl(2) Unix manual pages.

All functions in this module take a file descriptor fd as their first argument. This can be an integer file descriptor, such as returned by sys.stdin.fileno(), or an io.IOBase object, such as sys.stdin itself, which provides a fileno() that returns a genuine file descriptor.

在 3.3 版更改: Operations in this module used to raise an IOError where they now raise an OSError.

这个模块定义了以下函数：

fcntl.fcntl(fd, cmd, arg=0)

Perform the operation cmd on file descriptor fd (file objects providing a fileno() method are accepted as well). The values used for cmd are operating system dependent, and are available as constants in the fcntl module, using the same names as used in the relevant C header files. The argument arg can either be an integer value, or a bytes object. With an integer value, the return value of this function is the integer return value of the C fcntl() call. When the argument is bytes it represents a binary structure, e.g. created by struct.pack(). The binary data is copied to a buffer whose address is passed to the C fcntl() call. The return value after a successful call is the contents of the buffer, converted to a bytes object. The length of the returned object will be the same as the length of the arg argument. This is limited to 1024 bytes. If the information returned in the buffer by the operating system is larger than 1024 bytes, this is most likely to result in a segmentation violation or a more subtle data corruption.

If the fcntl() fails, an OSError is raised.

fcntl.ioctl(fd, request, arg=0, mutate_flag=True)

This function is identical to the fcntl() function, except that the argument handling is even more complicated.

The request parameter is limited to values that can fit in 32-bits. Additional constants of interest for use as the request argument can be found in the termios module, under the same names as used in the relevant C header files.

The parameter arg can be one of an integer, an object supporting the read-only buffer interface (like bytes) or an object supporting the read-write buffer interface (like bytearray).

In all but the last case, behaviour is as for the fcntl() function.

If a mutable buffer is passed, then the behaviour is determined by the value of the mutate_flag parameter.

If it is false, the buffer’s mutability is ignored and behaviour is as for a read-only buffer, except that the 1024 byte limit mentioned above is avoided – so long as the buffer you pass is at least as long as what the operating system wants to put there, things should work.

If mutate_flag is true (the default), then the buffer is (in effect) passed to the underlying ioctl() system call, the latter’s return code is passed back to the calling Python, and the buffer’s new contents reflect the action of the ioctl(). This is a slight simplification, because if the supplied buffer is less than 1024 bytes long it is first copied into a static buffer 1024 bytes long which is then passed to ioctl() and copied back into the supplied buffer.

If the ioctl() fails, an OSError exception is raised.

举个例子：

>>> import array, fcntl, struct, termios, os
>>> os.getpgrp()
13341
>>> struct.unpack('h', fcntl.ioctl(0, termios.TIOCGPGRP, "  "))[0]
13341
>>> buf = array.array('h', [0])
>>> fcntl.ioctl(0, termios.TIOCGPGRP, buf, 1)
0
>>> buf
array('h', [13341])

fcntl.flock(fd, operation)

Perform the lock operation operation on file descriptor fd (file objects providing a fileno() method are accepted as well). See the Unix manual flock(2) for details. (On some systems, this function is emulated using fcntl().)

If the flock() fails, an OSError exception is raised.

fcntl.lockf(fd, cmd, len=0, start=0, whence=0)

This is essentially a wrapper around the fcntl() locking calls. fd is the file descriptor of the file to lock or unlock, and cmd is one of the following values:

LOCK_UN – unlock

LOCK_SH – acquire a shared lock

LOCK_EX – acquire an exclusive lock

When cmd is LOCK_SH or LOCK_EX, it can also be bitwise ORed with LOCK_NB to avoid blocking on lock acquisition. If LOCK_NB is used and the lock cannot be acquired, an OSError will be raised and the exception will have an errno attribute set to EACCES or EAGAIN (depending on the operating system; for portability, check for both values). On at least some systems, LOCK_EX can only be used if the file descriptor refers to a file opened for writing.

len is the number of bytes to lock, start is the byte offset at which the lock starts, relative to whence, and whence is as with io.IOBase.seek(), specifically:

0 – relative to the start of the file (os.SEEK_SET)

1 – relative to the current buffer position (os.SEEK_CUR)

2 – relative to the end of the file (os.SEEK_END)

The default for start is 0, which means to start at the beginning of the file. The default for len is 0 which means to lock to the end of the file. The default for whence is also 0.

Examples (all on a SVR4 compliant system):

import struct, fcntl, os

f = open(...)
rv = fcntl.fcntl(f, fcntl.F_SETFL, os.O_NDELAY)

lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
rv = fcntl.fcntl(f, fcntl.F_SETLKW, lockdata)

Note that in the first example the return value variable rv will hold an integer value; in the second example it will hold a bytes object. The structure lay-out for the lockdata variable is system dependent — therefore using the flock() call may be better.

参见

模块 os

If the locking flags O_SHLOCK and O_EXLOCK are present in the os module (on BSD only), the os.open() function provides an alternative to the lockf() and flock() functions.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

35.10. pipes — 终端管道接口

源代码： Lib/pipes.py [https://github.com/python/cpython/tree/3.6/Lib/pipes.py]

pipes 定义了一个类用来抽象 pipeline 的概念 — 将数据从一个文件转到另一文件的转换器序列。

由于模块使用了 /bin/sh 命令行，因此要求有 POSIX 或兼容 os.system() 和 os.popen() 的终端程序。

pipes 模块定义了以下的类:

class pipes.Template

对 pipeline 的抽象。

示例:

>>> import pipes
>>> t = pipes.Template()
>>> t.append('tr a-z A-Z', '--')
>>> f = t.open('pipefile', 'w')
>>> f.write('hello world')
>>> f.close()
>>> open('pipefile').read()
'HELLO WORLD'

35.10.1. 模板对象

模板对象有以下方法:

Template.reset()

将一个管道模板恢复为初始状态。

Template.clone()

返回一个新的等价的管道模板。

Template.debug(flag)

如果 flag 为真值，则启用调试。 否则禁用调试。 当启用调试时，要执行的命令会被打印出来，并且会给予终端 set -x 命令以输出更详细的信息。

Template.append(cmd, kind)

在末尾添加一个新的动作。 cmd 变量必须为一个有效的 bourne 终端命令。 kind 变量由两个字母组成。

第一个字母可以为 '-' (这表示命令将读取其标准输入), 'f' (这表示命令将读取在命令行中给定的文件) 或 '.' (这表示命令将不读取输入，因而必须放在前面。)

类似地，第二个字母可以为 '-' (这表示命令将写入到标准输出), 'f' (这表示命令将写入在命令行中给定的文件) 或 '.' (这表示命令将不执行写入，因而必须放在末尾。)

Template.prepend(cmd, kind)

在开头添加一个新的动作。 请参阅 append() 获取相应参数的说明。

Template.open(file, mode)

返回一个文件类对象，打开到 file，但是将从管道读取或写入。 请注意只能给出 'r', 'w' 中的一个。

Template.copy(infile, outfile)

通过管道将 infile 拷贝到 outfile。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

35.11. resource — Resource usage information

This module provides basic mechanisms for measuring and controlling system resources utilized by a program.

Symbolic constants are used to specify particular system resources and to request usage information about either the current process or its children.

An OSError is raised on syscall failure.

exception resource.error

一个被弃用的 OSError 的别名。

在 3.3 版更改: 根据 PEP 3151 [https://www.python.org/dev/peps/pep-3151]，这个类是 OSError 的别名。

35.11.1. Resource Limits

Resources usage can be limited using the setrlimit() function described below. Each resource is controlled by a pair of limits: a soft limit and a hard limit. The soft limit is the current limit, and may be lowered or raised by a process over time. The soft limit can never exceed the hard limit. The hard limit can be lowered to any value greater than the soft limit, but not raised. (Only processes with the effective UID of the super-user can raise a hard limit.)

The specific resources that can be limited are system dependent. They are described in the getrlimit(2) man page. The resources listed below are supported when the underlying operating system supports them; resources which cannot be checked or controlled by the operating system are not defined in this module for those platforms.

resource.RLIM_INFINITY

Constant used to represent the limit for an unlimited resource.

resource.getrlimit(resource)

Returns a tuple (soft, hard) with the current soft and hard limits of resource. Raises ValueError if an invalid resource is specified, or error if the underlying system call fails unexpectedly.

resource.setrlimit(resource, limits)

Sets new limits of consumption of resource. The limits argument must be a tuple (soft, hard) of two integers describing the new limits. A value of RLIM_INFINITY can be used to request a limit that is unlimited.

Raises ValueError if an invalid resource is specified, if the new soft limit exceeds the hard limit, or if a process tries to raise its hard limit. Specifying a limit of RLIM_INFINITY when the hard or system limit for that resource is not unlimited will result in a ValueError. A process with the effective UID of super-user can request any valid limit value, including unlimited, but ValueError will still be raised if the requested limit exceeds the system imposed limit.

setrlimit may also raise error if the underlying system call fails.

resource.prlimit(pid, resource[, limits])

Combines setrlimit() and getrlimit() in one function and supports to get and set the resources limits of an arbitrary process. If pid is 0, then the call applies to the current process. resource and limits have the same meaning as in setrlimit(), except that limits is optional.

When limits is not given the function returns the resource limit of the process pid. When limits is given the resource limit of the process is set and the former resource limit is returned.

Raises ProcessLookupError when pid can’t be found and PermissionError when the user doesn’t have CAP_SYS_RESOURCE for the process.

Availability: Linux 2.6.36 or later with glibc 2.13 or later

3.4 新版功能.

These symbols define resources whose consumption can be controlled using the setrlimit() and getrlimit() functions described below. The values of these symbols are exactly the constants used by C programs.

The Unix man page for getrlimit(2) lists the available resources. Note that not all systems use the same symbol or same value to denote the same resource. This module does not attempt to mask platform differences — symbols not defined for a platform will not be available from this module on that platform.

resource.RLIMIT_CORE

The maximum size (in bytes) of a core file that the current process can create. This may result in the creation of a partial core file if a larger core would be required to contain the entire process image.

resource.RLIMIT_CPU

The maximum amount of processor time (in seconds) that a process can use. If this limit is exceeded, a SIGXCPU signal is sent to the process. (See the signal module documentation for information about how to catch this signal and do something useful, e.g. flush open files to disk.)

resource.RLIMIT_FSIZE

The maximum size of a file which the process may create.

resource.RLIMIT_DATA

The maximum size (in bytes) of the process’s heap.

resource.RLIMIT_STACK

The maximum size (in bytes) of the call stack for the current process. This only affects the stack of the main thread in a multi-threaded process.

resource.RLIMIT_RSS

The maximum resident set size that should be made available to the process.

resource.RLIMIT_NPROC

The maximum number of processes the current process may create.

resource.RLIMIT_NOFILE

The maximum number of open file descriptors for the current process.

resource.RLIMIT_OFILE

The BSD name for RLIMIT_NOFILE.

resource.RLIMIT_MEMLOCK

The maximum address space which may be locked in memory.

resource.RLIMIT_VMEM

The largest area of mapped memory which the process may occupy.

resource.RLIMIT_AS

The maximum area (in bytes) of address space which may be taken by the process.

resource.RLIMIT_MSGQUEUE

The number of bytes that can be allocated for POSIX message queues.

Availability: Linux 2.6.8 or later.

3.4 新版功能.

resource.RLIMIT_NICE

The ceiling for the process’s nice level (calculated as 20 - rlim_cur).

Availability: Linux 2.6.12 or later.

3.4 新版功能.

resource.RLIMIT_RTPRIO

The ceiling of the real-time priority.

Availability: Linux 2.6.12 or later.

3.4 新版功能.

resource.RLIMIT_RTTIME

The time limit (in microseconds) on CPU time that a process can spend under real-time scheduling without making a blocking syscall.

Availability: Linux 2.6.25 or later.

3.4 新版功能.

resource.RLIMIT_SIGPENDING

The number of signals which the process may queue.

Availability: Linux 2.6.8 or later.

3.4 新版功能.

resource.RLIMIT_SBSIZE

The maximum size (in bytes) of socket buffer usage for this user. This limits the amount of network memory, and hence the amount of mbufs, that this user may hold at any time.

Availability: FreeBSD 9 or later.

3.4 新版功能.

resource.RLIMIT_SWAP

The maximum size (in bytes) of the swap space that may be reserved or used by all of this user id’s processes. This limit is enforced only if bit 1 of the vm.overcommit sysctl is set. Please see tuning(7) for a complete description of this sysctl.

Availability: FreeBSD 9 or later.

3.4 新版功能.

resource.RLIMIT_NPTS

The maximum number of pseudo-terminals created by this user id.

Availability: FreeBSD 9 or later.

3.4 新版功能.

35.11.2. Resource Usage

These functions are used to retrieve resource usage information:

resource.getrusage(who)

This function returns an object that describes the resources consumed by either the current process or its children, as specified by the who parameter. The who parameter should be specified using one of the RUSAGE_* constants described below.

The fields of the return value each describe how a particular system resource has been used, e.g. amount of time spent running is user mode or number of times the process was swapped out of main memory. Some values are dependent on the clock tick internal, e.g. the amount of memory the process is using.

For backward compatibility, the return value is also accessible as a tuple of 16 elements.

The fields ru_utime and ru_stime of the return value are floating point values representing the amount of time spent executing in user mode and the amount of time spent executing in system mode, respectively. The remaining values are integers. Consult the getrusage(2) man page for detailed information about these values. A brief summary is presented here:

索引

域

Resource

0

ru_utime

time in user mode (float)

1

ru_stime

time in system mode (float)

2

ru_maxrss

maximum resident set size

3

ru_ixrss

shared memory size

4

ru_idrss

unshared memory size

5

ru_isrss

unshared stack size

6

ru_minflt

page faults not requiring I/O

7

ru_majflt

page faults requiring I/O

8

ru_nswap

number of swap outs

9

ru_inblock

block input operations

10

ru_oublock

block output operations

11

ru_msgsnd

messages sent

12

ru_msgrcv

messages received

13

ru_nsignals

signals received

14

ru_nvcsw

voluntary context switches

15

ru_nivcsw

involuntary context switches

This function will raise a ValueError if an invalid who parameter is specified. It may also raise error exception in unusual circumstances.

resource.getpagesize()

Returns the number of bytes in a system page. (This need not be the same as the hardware page size.)

The following RUSAGE_* symbols are passed to the getrusage() function to specify which processes information should be provided for.

resource.RUSAGE_SELF

Pass to getrusage() to request resources consumed by the calling process, which is the sum of resources used by all threads in the process.

resource.RUSAGE_CHILDREN

Pass to getrusage() to request resources consumed by child processes of the calling process which have been terminated and waited for.

resource.RUSAGE_BOTH

Pass to getrusage() to request resources consumed by both the current process and child processes. May not be available on all systems.

resource.RUSAGE_THREAD

Pass to getrusage() to request resources consumed by the current thread. May not be available on all systems.

3.2 新版功能.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

35.12. nis — Sun 的 NIS (黄页) 接口

nis 模块提供了对 NIS 库的轻量级包装，适用于多个主机的集中管理。

因为 NIS 仅存在于 Unix 系统，此模块仅在 Unix 上可用。

nis 模块定义了以下函数：

nis.match(key, mapname, domain=default_domain)

返回 key 在映射 mapname 中的匹配结果，如无结果则会引发错误 (nis.error)。 两个参数都应为字符串，key 定长 8 个比特。 返回值为任意字节数组（可包含 NULL 和其他特殊值）。

请注意如果 mapname 是另一名称的别名则会先检查别名。

domain 参数可允许重载用于查找的 NIS 域。 如果未指定，则会在默认 NIS 域中查找。

nis.cat(mapname, domain=default_domain)

返回一个字典，其元素为 key 到 value 的映射使得 match(key, mapname)==value。 请注意字典的键和值均为任意字节数组。

请注意如果 mapname 是另一名称的别名则会先检查别名。

domain 参数可允许重载用于查找的 NIS 域。 如果未指定，则会在默认 NIS 域中查找。

nis.maps(domain=default_domain)

返回全部可用映射的列表。

domain 参数可允许重载用于查找的 NIS 域。 如果未指定，则会在默认 NIS 域中查找。

nis.get_default_domain()

返回系统默认的 NIS 域。

nis 模块定义了以下异常：

exception nis.error

当 NIS 函数返回一个错误码时引发的异常。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

35.13. Unix syslog 库例程

此模块提供一个接口到Unix syslog 日常库. 参考 Unix 手册页关于 syslog 设施的详细描述.

此模块包装了系统的 syslog 例程族。 一个能与 syslog 服务器对话的纯 Python 库则以 logging.handlers 模块中 SysLogHandler 类的形式提供。

这个模块定义了以下函数：

syslog.syslog(message)

syslog.syslog(priority, message)

将字符串 message 发送到系统日志记录器。 如有必要会添加末尾换行符。 每条消息都带有一个由 facility 和 level 组成的优先级标价签。 可选的 priority 参数默认值为 LOG_INFO，它确定消息的优先级。 如果未在 priority 中使用逻辑或 (LOG_INFO | LOG_USER) 对 facility 进行编码，则会使用在 openlog() 调用中所给定的值。

如果 openlog() 未在对 syslog() 的调用之前被调用，则将不带参数地调用 openlog()。

syslog.openlog([ident[, logoption[, facility]]])

后续 syslog() 调用的日志选项可以通过调用 openlog() 来设置。 如果日志当前未打开则 syslog() 将不带参数地调用 openlog()。

可选的 ident 关键字参数是在每条消息前添加的字符串，默认为 sys.argv[0] 去除打头的路径部分。 可选的 logoption 关键字参数（默认为 0）是一个位字段 – 请参见下文了解可能的组合值。 可选的 facility 关键字参数 (默认为 LOG_USER) 为没有显式编码 facility 的消息设置默认的 facility。

在 3.2 版更改: In previous versions, keyword arguments were not allowed, and ident was required. The default for ident was dependent on the system libraries, and often was python instead of the name of the python program file.

syslog.closelog()

重置日志模块值并且调用系统库 closelog().

这使得此模块在初始导入时行为固定。 例如，openlog() 将在首次调用 syslog() 时被调用（如果 openlog() 还未被调用过），并且 ident 和其他 openlog() 形参会被重置为默认值。

syslog.setlogmask(maskpri)

将优先级掩码设为 maskpri 并返回之前的掩码值。 调用 syslog() 并附带未在 maskpri 中设置的优先级将会被忽略。 默认设置为记录所有优先级。 函数 LOG_MASK(pri) 可计算单个优先级 pri 的掩码。 函数 LOG_UPTO(pri) 可计算包括 pri 在内的所有优先级的掩码。

此莫款定义了一下常量:

优先级级别 (高到低):

LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG.

设施:

LOG_KERN, LOG_USER, LOG_MAIL, LOG_DAEMON, LOG_AUTH, LOG_LPR, LOG_NEWS, LOG_UUCP, LOG_CRON, LOG_SYSLOG, LOG_LOCAL0 to LOG_LOCAL7，如果 <syslog.h> 中有定义则还有 LOG_AUTHPRIV。

日志选项:

LOG_PID, LOG_CONS, LOG_NDELAY，如果 <syslog.h> 中有定义则还有 LOG_ODELAY, LOG_NOWAIT 以及 LOG_PERROR。

35.13.1. 例子

35.13.1.1. 简单示例

一个简单的示例集:

import syslog

syslog.syslog('Processing started')
if error:
    syslog.syslog(syslog.LOG_ERR, 'Processing started')

一个设置多种日志选项的示例，其中有在日志消息中包含进程 ID，以及将消息写入用于邮件日志记录的目标设施等:

syslog.openlog(logoption=syslog.LOG_PID, facility=syslog.LOG_MAIL)
syslog.syslog('E-mail processing initiated...')

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

35. Unix 专有服务 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

36. 被取代的模块

本章中描述的模块均已弃用，仅保留用于向后兼容。 它们已经被其他模块所取代。

36.1. optparse — 解析器的命令行选项

36.1.1. 背景

36.1.1.1. 术语

36.1.1.2. What are options for?

36.1.1.3. 位置位置

36.1.2. 教程

36.1.2.1. Understanding option actions

36.1.2.2. The store action

36.1.2.3. Handling boolean (flag) options

36.1.2.4. Other actions

36.1.2.5. 默认值

36.1.2.6. Generating help

36.1.2.6.1. Grouping Options

36.1.2.7. Printing a version string

36.1.2.8. How optparse handles errors

36.1.2.9. Putting it all together

36.1.3. 参考指南

36.1.3.1. 创建解析器

36.1.3.2. 填充解析器

36.1.3.3. 定义选项

36.1.3.4. Option attributes

36.1.3.5. Standard option actions

36.1.3.6. Standard option types

36.1.3.7. 解析参数

36.1.3.8. Querying and manipulating your option parser

36.1.3.9. Conflicts between options

36.1.3.10. 清理

36.1.3.11. Other methods

36.1.4. Option Callbacks

36.1.4.1. Defining a callback option

36.1.4.2. How callbacks are called

36.1.4.3. Raising errors in a callback

36.1.4.4. Callback example 1: trivial callback

36.1.4.5. Callback example 2: check option order

36.1.4.6. Callback example 3: check option order (generalized)

36.1.4.7. Callback example 4: check arbitrary condition

36.1.4.8. Callback example 5: fixed arguments

36.1.4.9. Callback example 6: variable arguments

36.1.5. Extending optparse

36.1.5.1. Adding new types

36.1.5.2. Adding new actions

36.2. imp — Access the import internals

36.2.1. 例子

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

36. 被取代的模块 »

|

36.1. optparse — 解析器的命令行选项

源代码： Lib/optparse.py [https://github.com/python/cpython/tree/3.6/Lib/optparse.py]

3.2 版后已移除: optparse 模块已被弃用并且将不再继续开发；开发将转至 argparse 模块进行。

optparse is a more convenient, flexible, and powerful library for parsing command-line options than the old getopt module. optparse uses a more declarative style of command-line parsing: you create an instance of OptionParser, populate it with options, and parse the command line. optparse allows users to specify options in the conventional GNU/POSIX syntax, and additionally generates usage and help messages for you.

Here’s an example of using optparse in a simple script:

from optparse import OptionParser
...
parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
                  help="write report to FILE", metavar="FILE")
parser.add_option("-q", "--quiet",
                  action="store_false", dest="verbose", default=True,
                  help="don't print status messages to stdout")

(options, args) = parser.parse_args()

With these few lines of code, users of your script can now do the “usual thing” on the command-line, for example:

<yourscript> --file=outfile -q

As it parses the command line, optparse sets attributes of the options object returned by parse_args() based on user-supplied command-line values. When parse_args() returns from parsing this command line, options.filename will be "outfile" and options.verbose will be False. optparse supports both long and short options, allows short options to be merged together, and allows options to be associated with their arguments in a variety of ways. Thus, the following command lines are all equivalent to the above example:

<yourscript> -f outfile --quiet
<yourscript> --quiet --file outfile
<yourscript> -q -foutfile
<yourscript> -qfoutfile

Additionally, users can run one of

<yourscript> -h
<yourscript> --help

and optparse will print out a brief summary of your script’s options:

Usage: <yourscript> [options]

Options:
  -h, --help            show this help message and exit
  -f FILE, --file=FILE  write report to FILE
  -q, --quiet           don't print status messages to stdout

where the value of yourscript is determined at runtime (normally from sys.argv[0]).

36.1.1. 背景

optparse was explicitly designed to encourage the creation of programs with straightforward, conventional command-line interfaces. To that end, it supports only the most common command-line syntax and semantics conventionally used under Unix. If you are unfamiliar with these conventions, read this section to acquaint yourself with them.

36.1.1.1. 术语

argument – 参数

a string entered on the command-line, and passed by the shell to execl() or execv(). In Python, arguments are elements of sys.argv[1:] (sys.argv[0] is the name of the program being executed). Unix shells also use the term “word”.

It is occasionally desirable to substitute an argument list other than sys.argv[1:], so you should read “argument” as “an element of sys.argv[1:], or of some other list provided as a substitute for sys.argv[1:]”.

选项

an argument used to supply extra information to guide or customize the execution of a program. There are many different syntaxes for options; the traditional Unix syntax is a hyphen (“-“) followed by a single letter, e.g. -x or -F. Also, traditional Unix syntax allows multiple options to be merged into a single argument, e.g. -x -F is equivalent to -xF. The GNU project introduced -- followed by a series of hyphen-separated words, e.g. --file or --dry-run. These are the only two option syntaxes provided by optparse.

Some other option syntaxes that the world has seen include:

a hyphen followed by a few letters, e.g. -pf (this is not the same as multiple options merged into a single argument)

a hyphen followed by a whole word, e.g. -file (this is technically equivalent to the previous syntax, but they aren’t usually seen in the same program)

a plus sign followed by a single letter, or a few letters, or a word, e.g. +f, +rgb

a slash followed by a letter, or a few letters, or a word, e.g. /f, /file

These option syntaxes are not supported by optparse, and they never will be. This is deliberate: the first three are non-standard on any environment, and the last only makes sense if you’re exclusively targeting VMS, MS-DOS, and/or Windows.

可选参数:

an argument that follows an option, is closely associated with that option, and is consumed from the argument list when that option is. With optparse, option arguments may either be in a separate argument from their option:

-f foo
--file foo

or included in the same argument:

-ffoo
--file=foo

Typically, a given option either takes an argument or it doesn’t. Lots of people want an “optional option arguments” feature, meaning that some options will take an argument if they see it, and won’t if they don’t. This is somewhat controversial, because it makes parsing ambiguous: if -a takes an optional argument and -b is another option entirely, how do we interpret -ab? Because of this ambiguity, optparse does not support this feature.

positional argument – 位置参数

something leftover in the argument list after options have been parsed, i.e. after options and their arguments have been parsed and removed from the argument list.

必选选项

an option that must be supplied on the command-line; note that the phrase “required option” is self-contradictory in English. optparse doesn’t prevent you from implementing required options, but doesn’t give you much help at it either.

For example, consider this hypothetical command-line:

prog -v --report report.txt foo bar

-v and --report are both options. Assuming that --report takes one argument, report.txt is an option argument. foo and bar are positional arguments.

36.1.1.2. What are options for?

Options are used to provide extra information to tune or customize the execution of a program. In case it wasn’t clear, options are usually optional. A program should be able to run just fine with no options whatsoever. (Pick a random program from the Unix or GNU toolsets. Can it run without any options at all and still make sense? The main exceptions are find, tar, and dd—all of which are mutant oddballs that have been rightly criticized for their non-standard syntax and confusing interfaces.)

Lots of people want their programs to have “required options”. Think about it. If it’s required, then it’s not optional! If there is a piece of information that your program absolutely requires in order to run successfully, that’s what positional arguments are for.

As an example of good command-line interface design, consider the humble cp utility, for copying files. It doesn’t make much sense to try to copy files without supplying a destination and at least one source. Hence, cp fails if you run it with no arguments. However, it has a flexible, useful syntax that does not require any options at all:

cp SOURCE DEST
cp SOURCE ... DEST-DIR

You can get pretty far with just that. Most cp implementations provide a bunch of options to tweak exactly how the files are copied: you can preserve mode and modification time, avoid following symlinks, ask before clobbering existing files, etc. But none of this distracts from the core mission of cp, which is to copy either one file to another, or several files to another directory.

36.1.1.3. 位置位置

Positional arguments are for those pieces of information that your program absolutely, positively requires to run.

A good user interface should have as few absolute requirements as possible. If your program requires 17 distinct pieces of information in order to run successfully, it doesn’t much matter how you get that information from the user—most people will give up and walk away before they successfully run the program. This applies whether the user interface is a command-line, a configuration file, or a GUI: if you make that many demands on your users, most of them will simply give up.

In short, try to minimize the amount of information that users are absolutely required to supply—use sensible defaults whenever possible. Of course, you also want to make your programs reasonably flexible. That’s what options are for. Again, it doesn’t matter if they are entries in a config file, widgets in the “Preferences” dialog of a GUI, or command-line options—the more options you implement, the more flexible your program is, and the more complicated its implementation becomes. Too much flexibility has drawbacks as well, of course; too many options can overwhelm users and make your code much harder to maintain.

36.1.2. 教程

While optparse is quite flexible and powerful, it’s also straightforward to use in most cases. This section covers the code patterns that are common to any optparse-based program.

First, you need to import the OptionParser class; then, early in the main program, create an OptionParser instance:

from optparse import OptionParser
...
parser = OptionParser()

Then you can start defining options. The basic syntax is:

parser.add_option(opt_str, ...,
                  attr=value, ...)

Each option has one or more option strings, such as -f or --file, and several option attributes that tell optparse what to expect and what to do when it encounters that option on the command line.

Typically, each option will have one short option string and one long option string, e.g.:

parser.add_option("-f", "--file", ...)

You’re free to define as many short option strings and as many long option strings as you like (including zero), as long as there is at least one option string overall.

The option strings passed to OptionParser.add_option() are effectively labels for the option defined by that call. For brevity, we will frequently refer to encountering an option on the command line; in reality, optparse encounters option strings and looks up options from them.

Once all of your options are defined, instruct optparse to parse your program’s command line:

(options, args) = parser.parse_args()

(If you like, you can pass a custom argument list to parse_args(), but that’s rarely necessary: by default it uses sys.argv[1:].)

parse_args() 返回两个值：

options, an object containing values for all of your options—e.g. if --file takes a single string argument, then options.file will be the filename supplied by the user, or None if the user did not supply that option

args, the list of positional arguments leftover after parsing options

This tutorial section only covers the four most important option attributes: action, type, dest (destination), and help. Of these, action is the most fundamental.

36.1.2.1. Understanding option actions

Actions tell optparse what to do when it encounters an option on the command line. There is a fixed set of actions hard-coded into optparse; adding new actions is an advanced topic covered in section Extending optparse. Most actions tell optparse to store a value in some variable—for example, take a string from the command line and store it in an attribute of options.

If you don’t specify an option action, optparse defaults to store.

36.1.2.2. The store action

The most common option action is store, which tells optparse to take the next argument (or the remainder of the current argument), ensure that it is of the correct type, and store it to your chosen destination.

例如

parser.add_option("-f", "--file",
                  action="store", type="string", dest="filename")

Now let’s make up a fake command line and ask optparse to parse it:

args = ["-f", "foo.txt"]
(options, args) = parser.parse_args(args)

When optparse sees the option string -f, it consumes the next argument, foo.txt, and stores it in options.filename. So, after this call to parse_args(), options.filename is "foo.txt".

Some other option types supported by optparse are int and float. Here’s an option that expects an integer argument:

parser.add_option("-n", type="int", dest="num")

Note that this option has no long option string, which is perfectly acceptable. Also, there’s no explicit action, since the default is store.

Let’s parse another fake command-line. This time, we’ll jam the option argument right up against the option: since -n42 (one argument) is equivalent to -n 42 (two arguments), the code

(options, args) = parser.parse_args(["-n42"])
print(options.num)

will print 42.

If you don’t specify a type, optparse assumes string. Combined with the fact that the default action is store, that means our first example can be a lot shorter:

parser.add_option("-f", "--file", dest="filename")

If you don’t supply a destination, optparse figures out a sensible default from the option strings: if the first long option string is --foo-bar, then the default destination is foo_bar. If there are no long option strings, optparse looks at the first short option string: the default destination for -f is f.

optparse also includes the built-in complex type. Adding types is covered in section Extending optparse.

36.1.2.3. Handling boolean (flag) options

Flag options—set a variable to true or false when a particular option is seen—are quite common. optparse supports them with two separate actions, store_true and store_false. For example, you might have a verbose flag that is turned on with -v and off with -q:

parser.add_option("-v", action="store_true", dest="verbose")
parser.add_option("-q", action="store_false", dest="verbose")

Here we have two different options with the same destination, which is perfectly OK. (It just means you have to be a bit careful when setting default values—see below.)

When optparse encounters -v on the command line, it sets options.verbose to True; when it encounters -q, options.verbose is set to False.

36.1.2.4. Other actions

Some other actions supported by optparse are:

"store_const"

store a constant value

"append"

append this option’s argument to a list

"count"

increment a counter by one

"callback"

调用指定函数

These are covered in section 参考指南, Reference Guide and section Option Callbacks.

36.1.2.5. 默认值

All of the above examples involve setting some variable (the “destination”) when certain command-line options are seen. What happens if those options are never seen? Since we didn’t supply any defaults, they are all set to None. This is usually fine, but sometimes you want more control. optparse lets you supply a default value for each destination, which is assigned before the command line is parsed.

First, consider the verbose/quiet example. If we want optparse to set verbose to True unless -q is seen, then we can do this:

parser.add_option("-v", action="store_true", dest="verbose", default=True)
parser.add_option("-q", action="store_false", dest="verbose")

Since default values apply to the destination rather than to any particular option, and these two options happen to have the same destination, this is exactly equivalent:

parser.add_option("-v", action="store_true", dest="verbose")
parser.add_option("-q", action="store_false", dest="verbose", default=True)

考虑一下：

parser.add_option("-v", action="store_true", dest="verbose", default=False)
parser.add_option("-q", action="store_false", dest="verbose", default=True)

Again, the default value for verbose will be True: the last default value supplied for any particular destination is the one that counts.

A clearer way to specify default values is the set_defaults() method of OptionParser, which you can call at any time before calling parse_args():

parser.set_defaults(verbose=True)
parser.add_option(...)
(options, args) = parser.parse_args()

As before, the last value specified for a given option destination is the one that counts. For clarity, try to use one method or the other of setting default values, not both.

36.1.2.6. Generating help

optparse’s ability to generate help and usage text automatically is useful for creating user-friendly command-line interfaces. All you have to do is supply a help value for each option, and optionally a short usage message for your whole program. Here’s an OptionParser populated with user-friendly (documented) options:

usage = "usage: %prog [options] arg1 arg2"
parser = OptionParser(usage=usage)
parser.add_option("-v", "--verbose",
                  action="store_true", dest="verbose", default=True,
                  help="make lots of noise [default]")
parser.add_option("-q", "--quiet",
                  action="store_false", dest="verbose",
                  help="be vewwy quiet (I'm hunting wabbits)")
parser.add_option("-f", "--filename",
                  metavar="FILE", help="write output to FILE")
parser.add_option("-m", "--mode",
                  default="intermediate",
                  help="interaction mode: novice, intermediate, "
                       "or expert [default: %default]")

If optparse encounters either -h or --help on the command-line, or if you just call parser.print_help(), it prints the following to standard output:

Usage: <yourscript> [options] arg1 arg2

Options:
  -h, --help            show this help message and exit
  -v, --verbose         make lots of noise [default]
  -q, --quiet           be vewwy quiet (I'm hunting wabbits)
  -f FILE, --filename=FILE
                        write output to FILE
  -m MODE, --mode=MODE  interaction mode: novice, intermediate, or
                        expert [default: intermediate]

(If the help output is triggered by a help option, optparse exits after printing the help text.)

There’s a lot going on here to help optparse generate the best possible help message:

the script defines its own usage message:

usage = "usage: %prog [options] arg1 arg2"

optparse expands %prog in the usage string to the name of the current program, i.e. os.path.basename(sys.argv[0]). The expanded string is then printed before the detailed option help.

If you don’t supply a usage string, optparse uses a bland but sensible default: "Usage: %prog [options]", which is fine if your script doesn’t take any positional arguments.

every option defines a help string, and doesn’t worry about line-wrapping—optparse takes care of wrapping lines and making the help output look good.

options that take a value indicate this fact in their automatically-generated help message, e.g. for the “mode” option:

-m MODE, --mode=MODE

Here, “MODE” is called the meta-variable: it stands for the argument that the user is expected to supply to -m/--mode. By default, optparse converts the destination variable name to uppercase and uses that for the meta-variable. Sometimes, that’s not what you want—for example, the --filename option explicitly sets metavar="FILE", resulting in this automatically-generated option description:

-f FILE, --filename=FILE

This is important for more than just saving space, though: the manually written help text uses the meta-variable FILE to clue the user in that there’s a connection between the semi-formal syntax -f FILE and the informal semantic description “write output to FILE”. This is a simple but effective way to make your help text a lot clearer and more useful for end users.

options that have a default value can include %default in the help string—optparse will replace it with str() of the option’s default value. If an option has no default value (or the default value is None), %default expands to none.

36.1.2.6.1. Grouping Options

When dealing with many options, it is convenient to group these options for better help output. An OptionParser can contain several option groups, each of which can contain several options.

An option group is obtained using the class OptionGroup:

class optparse.OptionGroup(parser, title, description=None)

where

parser is the OptionParser instance the group will be inserted in to

title is the group title

description, optional, is a long description of the group

OptionGroup inherits from OptionContainer (like OptionParser) and so the add_option() method can be used to add an option to the group.

Once all the options are declared, using the OptionParser method add_option_group() the group is added to the previously defined parser.

Continuing with the parser defined in the previous section, adding an OptionGroup to a parser is easy:

group = OptionGroup(parser, "Dangerous Options",
                    "Caution: use these options at your own risk.  "
                    "It is believed that some of them bite.")
group.add_option("-g", action="store_true", help="Group option.")
parser.add_option_group(group)

This would result in the following help output:

Usage: <yourscript> [options] arg1 arg2

Options:
  -h, --help            show this help message and exit
  -v, --verbose         make lots of noise [default]
  -q, --quiet           be vewwy quiet (I'm hunting wabbits)
  -f FILE, --filename=FILE
                        write output to FILE
  -m MODE, --mode=MODE  interaction mode: novice, intermediate, or
                        expert [default: intermediate]

  Dangerous Options:
    Caution: use these options at your own risk.  It is believed that some
    of them bite.

    -g                  Group option.

A bit more complete example might involve using more than one group: still extending the previous example:

group = OptionGroup(parser, "Dangerous Options",
                    "Caution: use these options at your own risk.  "
                    "It is believed that some of them bite.")
group.add_option("-g", action="store_true", help="Group option.")
parser.add_option_group(group)

group = OptionGroup(parser, "Debug Options")
group.add_option("-d", "--debug", action="store_true",
                 help="Print debug information")
group.add_option("-s", "--sql", action="store_true",
                 help="Print all SQL statements executed")
group.add_option("-e", action="store_true", help="Print every action done")
parser.add_option_group(group)

that results in the following output:

Usage: <yourscript> [options] arg1 arg2

Options:
  -h, --help            show this help message and exit
  -v, --verbose         make lots of noise [default]
  -q, --quiet           be vewwy quiet (I'm hunting wabbits)
  -f FILE, --filename=FILE
                        write output to FILE
  -m MODE, --mode=MODE  interaction mode: novice, intermediate, or expert
                        [default: intermediate]

  Dangerous Options:
    Caution: use these options at your own risk.  It is believed that some
    of them bite.

    -g                  Group option.

  Debug Options:
    -d, --debug         Print debug information
    -s, --sql           Print all SQL statements executed
    -e                  Print every action done

Another interesting method, in particular when working programmatically with option groups is:

OptionParser.get_option_group(opt_str)

Return the OptionGroup to which the short or long option string opt_str (e.g. '-o' or '--option') belongs. If there’s no such OptionGroup, return None.

36.1.2.7. Printing a version string

Similar to the brief usage string, optparse can also print a version string for your program. You have to supply the string as the version argument to OptionParser:

parser = OptionParser(usage="%prog [-f] [-q]", version="%prog 1.0")

%prog is expanded just like it is in usage. Apart from that, version can contain anything you like. When you supply it, optparse automatically adds a --version option to your parser. If it encounters this option on the command line, it expands your version string (by replacing %prog), prints it to stdout, and exits.

For example, if your script is called /usr/bin/foo:

$ /usr/bin/foo --version
foo 1.0

The following two methods can be used to print and get the version string:

OptionParser.print_version(file=None)

Print the version message for the current program (self.version) to file (default stdout). As with print_usage(), any occurrence of %prog in self.version is replaced with the name of the current program. Does nothing if self.version is empty or undefined.

OptionParser.get_version()

Same as print_version() but returns the version string instead of printing it.

36.1.2.8. How optparse handles errors

There are two broad classes of errors that optparse has to worry about: programmer errors and user errors. Programmer errors are usually erroneous calls to OptionParser.add_option(), e.g. invalid option strings, unknown option attributes, missing option attributes, etc. These are dealt with in the usual way: raise an exception (either optparse.OptionError or TypeError) and let the program crash.

Handling user errors is much more important, since they are guaranteed to happen no matter how stable your code is. optparse can automatically detect some user errors, such as bad option arguments (passing -n 4x where -n takes an integer argument), missing arguments (-n at the end of the command line, where -n takes an argument of any type). Also, you can call OptionParser.error() to signal an application-defined error condition:

(options, args) = parser.parse_args()
...
if options.a and options.b:
    parser.error("options -a and -b are mutually exclusive")

In either case, optparse handles the error the same way: it prints the program’s usage message and an error message to standard error and exits with error status 2.

Consider the first example above, where the user passes 4x to an option that takes an integer:

$ /usr/bin/foo -n 4x
Usage: foo [options]

foo: error: option -n: invalid integer value: '4x'

Or, where the user fails to pass a value at all:

$ /usr/bin/foo -n
Usage: foo [options]

foo: error: -n option requires an argument

optparse-generated error messages take care always to mention the option involved in the error; be sure to do the same when calling OptionParser.error() from your application code.

If optparse’s default error-handling behaviour does not suit your needs, you’ll need to subclass OptionParser and override its exit() and/or error() methods.

36.1.2.9. Putting it all together

Here’s what optparse-based scripts usually look like:

from optparse import OptionParser
...
def main():
    usage = "usage: %prog [options] arg"
    parser = OptionParser(usage)
    parser.add_option("-f", "--file", dest="filename",
                      help="read data from FILENAME")
    parser.add_option("-v", "--verbose",
                      action="store_true", dest="verbose")
    parser.add_option("-q", "--quiet",
                      action="store_false", dest="verbose")
    ...
    (options, args) = parser.parse_args()
    if len(args) != 1:
        parser.error("incorrect number of arguments")
    if options.verbose:
        print("reading %s..." % options.filename)
    ...

if __name__ == "__main__":
    main()

36.1.3. 参考指南

36.1.3.1. 创建解析器

The first step in using optparse is to create an OptionParser instance.

class optparse.OptionParser(...)

The OptionParser constructor has no required arguments, but a number of optional keyword arguments. You should always pass them as keyword arguments, i.e. do not rely on the order in which the arguments are declared.

usage (默认: "%prog [options]")

The usage summary to print when your program is run incorrectly or with a help option. When optparse prints the usage string, it expands %prog to os.path.basename(sys.argv[0]) (or to prog if you passed that keyword argument). To suppress a usage message, pass the special value optparse.SUPPRESS_USAGE.

option_list (默认: [])

A list of Option objects to populate the parser with. The options in option_list are added after any options in standard_option_list (a class attribute that may be set by OptionParser subclasses), but before any version or help options. Deprecated; use add_option() after creating the parser instead.

option_class (默认: optparse.Option)

Class to use when adding options to the parser in add_option().

version (默认: None)

A version string to print when the user supplies a version option. If you supply a true value for version, optparse automatically adds a version option with the single option string --version. The substring %prog is expanded the same as for usage.

conflict_handler (默认: "error")

Specifies what to do when options with conflicting option strings are added to the parser; see section Conflicts between options.

description (默认: None)

A paragraph of text giving a brief overview of your program. optparse reformats this paragraph to fit the current terminal width and prints it when the user requests help (after usage, but before the list of options).

formatter (default: a new IndentedHelpFormatter)

An instance of optparse.HelpFormatter that will be used for printing help text. optparse provides two concrete classes for this purpose: IndentedHelpFormatter and TitledHelpFormatter.

add_help_option (默认: True)

If true, optparse will add a help option (with option strings -h and --help) to the parser.

prog

The string to use when expanding %prog in usage and version instead of os.path.basename(sys.argv[0]).

epilog (默认: None)

A paragraph of help text to print after the option help.

36.1.3.2. 填充解析器

There are several ways to populate the parser with options. The preferred way is by using OptionParser.add_option(), as shown in section 教程. add_option() can be called in one of two ways:

pass it an Option instance (as returned by make_option())

pass it any combination of positional and keyword arguments that are acceptable to make_option() (i.e., to the Option constructor), and it will create the Option instance for you

The other alternative is to pass a list of pre-constructed Option instances to the OptionParser constructor, as in:

option_list = [
    make_option("-f", "--filename",
                action="store", type="string", dest="filename"),
    make_option("-q", "--quiet",
                action="store_false", dest="verbose"),
    ]
parser = OptionParser(option_list=option_list)

(make_option() is a factory function for creating Option instances; currently it is an alias for the Option constructor. A future version of optparse may split Option into several classes, and make_option() will pick the right class to instantiate. Do not instantiate Option directly.)

36.1.3.3. 定义选项

Each Option instance represents a set of synonymous command-line option strings, e.g. -f and --file. You can specify any number of short or long option strings, but you must specify at least one overall option string.

The canonical way to create an Option instance is with the add_option() method of OptionParser.

OptionParser.add_option(option)

OptionParser.add_option(*opt_str, attr=value, ...)

To define an option with only a short option string:

parser.add_option("-f", attr=value, ...)

And to define an option with only a long option string:

parser.add_option("--foo", attr=value, ...)

The keyword arguments define attributes of the new Option object. The most important option attribute is action, and it largely determines which other attributes are relevant or required. If you pass irrelevant option attributes, or fail to pass required ones, optparse raises an OptionError exception explaining your mistake.

An option’s action determines what optparse does when it encounters this option on the command-line. The standard option actions hard-coded into optparse are:

"store"

存储此选项的参数（默认）

"store_const"

store a constant value

"store_true"

store a true value

"store_false"

store a false value

"append"

append this option’s argument to a list

"append_const"

将常量值附加到列表

"count"

increment a counter by one

"callback"

调用指定函数

"help"

打印用法消息，包括所有选项和文档

(If you don’t supply an action, the default is "store". For this action, you may also supply type and dest option attributes; see Standard option actions.)

As you can see, most actions involve storing or updating a value somewhere. optparse always creates a special object for this, conventionally called options (it happens to be an instance of optparse.Values). Option arguments (and various other values) are stored as attributes of this object, according to the dest (destination) option attribute.

For example, when you call

parser.parse_args()

one of the first things optparse does is create the options object:

options = Values()

If one of the options in this parser is defined with

parser.add_option("-f", "--file", action="store", type="string", dest="filename")

and the command-line being parsed includes any of the following:

-ffoo
-f foo
--file=foo
--file foo

then optparse, on seeing this option, will do the equivalent of

options.filename = "foo"

The type and dest option attributes are almost as important as action, but action is the only one that makes sense for all options.

36.1.3.4. Option attributes

The following option attributes may be passed as keyword arguments to OptionParser.add_option(). If you pass an option attribute that is not relevant to a particular option, or fail to pass a required option attribute, optparse raises OptionError.

Option.action

(默认: "store")

Determines optparse’s behaviour when this option is seen on the command line; the available options are documented here.

Option.type

(默认: "string")

The argument type expected by this option (e.g., "string" or "int"); the available option types are documented here.

Option.dest

(default: derived from option strings)

If the option’s action implies writing or modifying a value somewhere, this tells optparse where to write it: dest names an attribute of the options object that optparse builds as it parses the command line.

Option.default

The value to use for this option’s destination if the option is not seen on the command line. See also OptionParser.set_defaults().

Option.nargs

(默认: 1)

How many arguments of type type should be consumed when this option is seen. If > 1, optparse will store a tuple of values to dest.

Option.const

For actions that store a constant value, the constant value to store.

Option.choices

For options of type "choice", the list of strings the user may choose from.

Option.callback

For options with action "callback", the callable to call when this option is seen. See section Option Callbacks for detail on the arguments passed to the callable.

Option.callback_args

Option.callback_kwargs

Additional positional and keyword arguments to pass to callback after the four standard callback arguments.

Option.help

Help text to print for this option when listing all available options after the user supplies a help option (such as --help). If no help text is supplied, the option will be listed without help text. To hide this option, use the special value optparse.SUPPRESS_HELP.

Option.metavar

(default: derived from option strings)

Stand-in for the option argument(s) to use when printing help text. See section 教程 for an example.

36.1.3.5. Standard option actions

The various option actions all have slightly different requirements and effects. Most actions have several relevant option attributes which you may specify to guide optparse’s behaviour; a few have required attributes, which you must specify for any option using that action.

"store" [relevant: type, dest, nargs, choices]

The option must be followed by an argument, which is converted to a value according to type and stored in dest. If nargs > 1, multiple arguments will be consumed from the command line; all will be converted according to type and stored to dest as a tuple. See the Standard option types section.

If choices is supplied (a list or tuple of strings), the type defaults to "choice".

If type is not supplied, it defaults to "string".

If dest is not supplied, optparse derives a destination from the first long option string (e.g., --foo-bar implies foo_bar). If there are no long option strings, optparse derives a destination from the first short option string (e.g., -f implies f).

示例:

parser.add_option("-f")
parser.add_option("-p", type="float", nargs=3, dest="point")

As it parses the command line

-f foo.txt -p 1 -3.5 4 -fbar.txt

optparse will set

options.f = "foo.txt"
options.point = (1.0, -3.5, 4.0)
options.f = "bar.txt"

"store_const" [required: const; relevant: dest]

The value const is stored in dest.

示例:

parser.add_option("-q", "--quiet",
                  action="store_const", const=0, dest="verbose")
parser.add_option("-v", "--verbose",
                  action="store_const", const=1, dest="verbose")
parser.add_option("--noisy",
                  action="store_const", const=2, dest="verbose")

If --noisy is seen, optparse will set

options.verbose = 2

"store_true" [relevant: dest]

A special case of "store_const" that stores a true value to dest.

"store_false" [relevant: dest]

Like "store_true", but stores a false value.

示例:

parser.add_option("--clobber", action="store_true", dest="clobber")
parser.add_option("--no-clobber", action="store_false", dest="clobber")

"append" [relevant: type, dest, nargs, choices]

The option must be followed by an argument, which is appended to the list in dest. If no default value for dest is supplied, an empty list is automatically created when optparse first encounters this option on the command-line. If nargs > 1, multiple arguments are consumed, and a tuple of length nargs is appended to dest.

The defaults for type and dest are the same as for the "store" action.

示例:

parser.add_option("-t", "--tracks", action="append", type="int")

If -t3 is seen on the command-line, optparse does the equivalent of:

options.tracks = []
options.tracks.append(int("3"))

If, a little later on, --tracks=4 is seen, it does:

options.tracks.append(int("4"))

The append action calls the append method on the current value of the option. This means that any default value specified must have an append method. It also means that if the default value is non-empty, the default elements will be present in the parsed value for the option, with any values from the command line appended after those default values:

>>> parser.add_option("--files", action="append", default=['~/.mypkg/defaults'])
>>> opts, args = parser.parse_args(['--files', 'overrides.mypkg'])
>>> opts.files
['~/.mypkg/defaults', 'overrides.mypkg']

"append_const" [required: const; relevant: dest]

Like "store_const", but the value const is appended to dest; as with "append", dest defaults to None, and an empty list is automatically created the first time the option is encountered.

"count" [relevant: dest]

Increment the integer stored at dest. If no default value is supplied, dest is set to zero before being incremented the first time.

示例:

parser.add_option("-v", action="count", dest="verbosity")

The first time -v is seen on the command line, optparse does the equivalent of:

options.verbosity = 0
options.verbosity += 1

Every subsequent occurrence of -v results in

options.verbosity += 1

"callback" [required: callback; relevant: type, nargs, callback_args, callback_kwargs]

Call the function specified by callback, which is called as

func(option, opt_str, value, parser, *args, **kwargs)

See section Option Callbacks for more detail.

"help"

Prints a complete help message for all the options in the current option parser. The help message is constructed from the usage string passed to OptionParser’s constructor and the help string passed to every option.

If no help string is supplied for an option, it will still be listed in the help message. To omit an option entirely, use the special value optparse.SUPPRESS_HELP.

optparse automatically adds a help option to all OptionParsers, so you do not normally need to create one.

示例:

from optparse import OptionParser, SUPPRESS_HELP

# usually, a help option is added automatically, but that can
# be suppressed using the add_help_option argument
parser = OptionParser(add_help_option=False)

parser.add_option("-h", "--help", action="help")
parser.add_option("-v", action="store_true", dest="verbose",
                  help="Be moderately verbose")
parser.add_option("--file", dest="filename",
                  help="Input file to read data from")
parser.add_option("--secret", help=SUPPRESS_HELP)

If optparse sees either -h or --help on the command line, it will print something like the following help message to stdout (assuming sys.argv[0] is "foo.py"):

Usage: foo.py [options]

Options:
  -h, --help        Show this help message and exit
  -v                Be moderately verbose
  --file=FILENAME   Input file to read data from

After printing the help message, optparse terminates your process with sys.exit(0).

"version"

Prints the version number supplied to the OptionParser to stdout and exits. The version number is actually formatted and printed by the print_version() method of OptionParser. Generally only relevant if the version argument is supplied to the OptionParser constructor. As with help options, you will rarely create version options, since optparse automatically adds them when needed.

36.1.3.6. Standard option types

optparse has five built-in option types: "string", "int", "choice", "float" and "complex". If you need to add new option types, see section Extending optparse.

Arguments to string options are not checked or converted in any way: the text on the command line is stored in the destination (or passed to the callback) as-is.

Integer arguments (type "int") are parsed as follows:

if the number starts with 0x, it is parsed as a hexadecimal number

if the number starts with 0, it is parsed as an octal number

if the number starts with 0b, it is parsed as a binary number

otherwise, the number is parsed as a decimal number

The conversion is done by calling int() with the appropriate base (2, 8, 10, or 16). If this fails, so will optparse, although with a more useful error message.

"float" and "complex" option arguments are converted directly with float() and complex(), with similar error-handling.

"choice" options are a subtype of "string" options. The choices option attribute (a sequence of strings) defines the set of allowed option arguments. optparse.check_choice() compares user-supplied option arguments against this master list and raises OptionValueError if an invalid string is given.

36.1.3.7. 解析参数

The whole point of creating and populating an OptionParser is to call its parse_args() method:

(options, args) = parser.parse_args(args=None, values=None)

输入参数的位置

args

the list of arguments to process (default: sys.argv[1:])

values

an optparse.Values object to store option arguments in (default: a new instance of Values) – if you give an existing object, the option defaults will not be initialized on it

and the return values are

options

the same object that was passed in as values, or the optparse.Values instance created by optparse

args

the leftover positional arguments after all options have been processed

The most common usage is to supply neither keyword argument. If you supply values, it will be modified with repeated setattr() calls (roughly one for every option argument stored to an option destination) and returned by parse_args().

If parse_args() encounters any errors in the argument list, it calls the OptionParser’s error() method with an appropriate end-user error message. This ultimately terminates your process with an exit status of 2 (the traditional Unix exit status for command-line errors).

36.1.3.8. Querying and manipulating your option parser

The default behavior of the option parser can be customized slightly, and you can also poke around your option parser and see what’s there. OptionParser provides several methods to help you out:

OptionParser.disable_interspersed_args()

Set parsing to stop on the first non-option. For example, if -a and -b are both simple options that take no arguments, optparse normally accepts this syntax:

prog -a arg1 -b arg2

and treats it as equivalent to

prog -a -b arg1 arg2

To disable this feature, call disable_interspersed_args(). This restores traditional Unix syntax, where option parsing stops with the first non-option argument.

Use this if you have a command processor which runs another command which has options of its own and you want to make sure these options don’t get confused. For example, each command might have a different set of options.

OptionParser.enable_interspersed_args()

Set parsing to not stop on the first non-option, allowing interspersing switches with command arguments. This is the default behavior.

OptionParser.get_option(opt_str)

Returns the Option instance with the option string opt_str, or None if no options have that option string.

OptionParser.has_option(opt_str)

Return true if the OptionParser has an option with option string opt_str (e.g., -q or --verbose).

OptionParser.remove_option(opt_str)

If the OptionParser has an option corresponding to opt_str, that option is removed. If that option provided any other option strings, all of those option strings become invalid. If opt_str does not occur in any option belonging to this OptionParser, raises ValueError.

36.1.3.9. Conflicts between options

If you’re not careful, it’s easy to define options with conflicting option strings:

parser.add_option("-n", "--dry-run", ...)
...
parser.add_option("-n", "--noisy", ...)

(This is particularly true if you’ve defined your own OptionParser subclass with some standard options.)

Every time you add an option, optparse checks for conflicts with existing options. If it finds any, it invokes the current conflict-handling mechanism. You can set the conflict-handling mechanism either in the constructor:

parser = OptionParser(..., conflict_handler=handler)

or with a separate call:

parser.set_conflict_handler(handler)

The available conflict handlers are:

"error" (默认)

assume option conflicts are a programming error and raise OptionConflictError

"resolve"

resolve option conflicts intelligently (see below)

As an example, let’s define an OptionParser that resolves conflicts intelligently and add conflicting options to it:

parser = OptionParser(conflict_handler="resolve")
parser.add_option("-n", "--dry-run", ..., help="do no harm")
parser.add_option("-n", "--noisy", ..., help="be noisy")

At this point, optparse detects that a previously-added option is already using the -n option string. Since conflict_handler is "resolve", it resolves the situation by removing -n from the earlier option’s list of option strings. Now --dry-run is the only way for the user to activate that option. If the user asks for help, the help message will reflect that:

Options:
  --dry-run     do no harm
  ...
  -n, --noisy   be noisy

It’s possible to whittle away the option strings for a previously-added option until there are none left, and the user has no way of invoking that option from the command-line. In that case, optparse removes that option completely, so it doesn’t show up in help text or anywhere else. Carrying on with our existing OptionParser:

parser.add_option("--dry-run", ..., help="new dry-run option")

At this point, the original -n/--dry-run option is no longer accessible, so optparse removes it, leaving this help text:

Options:
  ...
  -n, --noisy   be noisy
  --dry-run     new dry-run option

36.1.3.10. 清理

OptionParser instances have several cyclic references. This should not be a problem for Python’s garbage collector, but you may wish to break the cyclic references explicitly by calling destroy() on your OptionParser once you are done with it. This is particularly useful in long-running applications where large object graphs are reachable from your OptionParser.

36.1.3.11. Other methods

OptionParser supports several other public methods:

OptionParser.set_usage(usage)

Set the usage string according to the rules described above for the usage constructor keyword argument. Passing None sets the default usage string; use optparse.SUPPRESS_USAGE to suppress a usage message.

OptionParser.print_usage(file=None)

Print the usage message for the current program (self.usage) to file (default stdout). Any occurrence of the string %prog in self.usage is replaced with the name of the current program. Does nothing if self.usage is empty or not defined.

OptionParser.get_usage()

Same as print_usage() but returns the usage string instead of printing it.

OptionParser.set_defaults(dest=value, ...)

Set default values for several option destinations at once. Using set_defaults() is the preferred way to set default values for options, since multiple options can share the same destination. For example, if several “mode” options all set the same destination, any one of them can set the default, and the last one wins:

parser.add_option("--advanced", action="store_const",
                  dest="mode", const="advanced",
                  default="novice")    # overridden below
parser.add_option("--novice", action="store_const",
                  dest="mode", const="novice",
                  default="advanced")  # overrides above setting

To avoid this confusion, use set_defaults():

parser.set_defaults(mode="advanced")
parser.add_option("--advanced", action="store_const",
                  dest="mode", const="advanced")
parser.add_option("--novice", action="store_const",
                  dest="mode", const="novice")

36.1.4. Option Callbacks

When optparse’s built-in actions and types aren’t quite enough for your needs, you have two choices: extend optparse or define a callback option. Extending optparse is more general, but overkill for a lot of simple cases. Quite often a simple callback is all you need.

There are two steps to defining a callback option:

define the option itself using the "callback" action

write the callback; this is a function (or method) that takes at least four arguments, as described below

36.1.4.1. Defining a callback option

As always, the easiest way to define a callback option is by using the OptionParser.add_option() method. Apart from action, the only option attribute you must specify is callback, the function to call:

parser.add_option("-c", action="callback", callback=my_callback)

callback is a function (or other callable object), so you must have already defined my_callback() when you create this callback option. In this simple case, optparse doesn’t even know if -c takes any arguments, which usually means that the option takes no arguments—the mere presence of -c on the command-line is all it needs to know. In some circumstances, though, you might want your callback to consume an arbitrary number of command-line arguments. This is where writing callbacks gets tricky; it’s covered later in this section.

optparse always passes four particular arguments to your callback, and it will only pass additional arguments if you specify them via callback_args and callback_kwargs. Thus, the minimal callback function signature is:

def my_callback(option, opt, value, parser):

The four arguments to a callback are described below.

There are several other option attributes that you can supply when you define a callback option:

type

has its usual meaning: as with the "store" or "append" actions, it instructs optparse to consume one argument and convert it to type. Rather than storing the converted value(s) anywhere, though, optparse passes it to your callback function.

nargs

also has its usual meaning: if it is supplied and > 1, optparse will consume nargs arguments, each of which must be convertible to type. It then passes a tuple of converted values to your callback.

callback_args

a tuple of extra positional arguments to pass to the callback

callback_kwargs

a dictionary of extra keyword arguments to pass to the callback

36.1.4.2. How callbacks are called

All callbacks are called as follows:

func(option, opt_str, value, parser, *args, **kwargs)

where

option

is the Option instance that’s calling the callback

opt_str

is the option string seen on the command-line that’s triggering the callback. (If an abbreviated long option was used, opt_str will be the full, canonical option string—e.g. if the user puts --foo on the command-line as an abbreviation for --foobar, then opt_str will be "--foobar".)

value

is the argument to this option seen on the command-line. optparse will only expect an argument if type is set; the type of value will be the type implied by the option’s type. If type for this option is None (no argument expected), then value will be None. If nargs > 1, value will be a tuple of values of the appropriate type.

parser

is the OptionParser instance driving the whole thing, mainly useful because you can access some other interesting data through its instance attributes:

parser.largs

the current list of leftover arguments, ie. arguments that have been consumed but are neither options nor option arguments. Feel free to modify parser.largs, e.g. by adding more arguments to it. (This list will become args, the second return value of parse_args().)

parser.rargs

the current list of remaining arguments, ie. with opt_str and value (if applicable) removed, and only the arguments following them still there. Feel free to modify parser.rargs, e.g. by consuming more arguments.

parser.values

the object where option values are by default stored (an instance of optparse.OptionValues). This lets callbacks use the same mechanism as the rest of optparse for storing option values; you don’t need to mess around with globals or closures. You can also access or modify the value(s) of any options already encountered on the command-line.

args

is a tuple of arbitrary positional arguments supplied via the callback_args option attribute.

kwargs

is a dictionary of arbitrary keyword arguments supplied via callback_kwargs.

36.1.4.3. Raising errors in a callback

The callback function should raise OptionValueError if there are any problems with the option or its argument(s). optparse catches this and terminates the program, printing the error message you supply to stderr. Your message should be clear, concise, accurate, and mention the option at fault. Otherwise, the user will have a hard time figuring out what they did wrong.

36.1.4.4. Callback example 1: trivial callback

Here’s an example of a callback option that takes no arguments, and simply records that the option was seen:

def record_foo_seen(option, opt_str, value, parser):
    parser.values.saw_foo = True

parser.add_option("--foo", action="callback", callback=record_foo_seen)

Of course, you could do that with the "store_true" action.

36.1.4.5. Callback example 2: check option order

Here’s a slightly more interesting example: record the fact that -a is seen, but blow up if it comes after -b in the command-line.

def check_order(option, opt_str, value, parser):
    if parser.values.b:
        raise OptionValueError("can't use -a after -b")
    parser.values.a = 1
...
parser.add_option("-a", action="callback", callback=check_order)
parser.add_option("-b", action="store_true", dest="b")

36.1.4.6. Callback example 3: check option order (generalized)

If you want to re-use this callback for several similar options (set a flag, but blow up if -b has already been seen), it needs a bit of work: the error message and the flag that it sets must be generalized.

def check_order(option, opt_str, value, parser):
    if parser.values.b:
        raise OptionValueError("can't use %s after -b" % opt_str)
    setattr(parser.values, option.dest, 1)
...
parser.add_option("-a", action="callback", callback=check_order, dest='a')
parser.add_option("-b", action="store_true", dest="b")
parser.add_option("-c", action="callback", callback=check_order, dest='c')

36.1.4.7. Callback example 4: check arbitrary condition

Of course, you could put any condition in there—you’re not limited to checking the values of already-defined options. For example, if you have options that should not be called when the moon is full, all you have to do is this:

def check_moon(option, opt_str, value, parser):
    if is_moon_full():
        raise OptionValueError("%s option invalid when moon is full"
                               % opt_str)
    setattr(parser.values, option.dest, 1)
...
parser.add_option("--foo",
                  action="callback", callback=check_moon, dest="foo")

(The definition of is_moon_full() is left as an exercise for the reader.)

36.1.4.8. Callback example 5: fixed arguments

Things get slightly more interesting when you define callback options that take a fixed number of arguments. Specifying that a callback option takes arguments is similar to defining a "store" or "append" option: if you define type, then the option takes one argument that must be convertible to that type; if you further define nargs, then the option takes nargs arguments.

Here’s an example that just emulates the standard "store" action:

def store_value(option, opt_str, value, parser):
    setattr(parser.values, option.dest, value)
...
parser.add_option("--foo",
                  action="callback", callback=store_value,
                  type="int", nargs=3, dest="foo")

Note that optparse takes care of consuming 3 arguments and converting them to integers for you; all you have to do is store them. (Or whatever; obviously you don’t need a callback for this example.)

36.1.4.9. Callback example 6: variable arguments

Things get hairy when you want an option to take a variable number of arguments. For this case, you must write a callback, as optparse doesn’t provide any built-in capabilities for it. And you have to deal with certain intricacies of conventional Unix command-line parsing that optparse normally handles for you. In particular, callbacks should implement the conventional rules for bare -- and - arguments:

either -- or - can be option arguments

bare -- (if not the argument to some option): halt command-line processing and discard the --

bare - (if not the argument to some option): halt command-line processing but keep the - (append it to parser.largs)

If you want an option that takes a variable number of arguments, there are several subtle, tricky issues to worry about. The exact implementation you choose will be based on which trade-offs you’re willing to make for your application (which is why optparse doesn’t support this sort of thing directly).

Nevertheless, here’s a stab at a callback for an option with variable arguments:

def vararg_callback(option, opt_str, value, parser):
    assert value is None
    value = []

    def floatable(str):
        try:
            float(str)
            return True
        except ValueError:
            return False

    for arg in parser.rargs:
        # stop on --foo like options
        if arg[:2] == "--" and len(arg) > 2:
            break
        # stop on -a, but not on -3 or -3.0
        if arg[:1] == "-" and len(arg) > 1 and not floatable(arg):
            break
        value.append(arg)

    del parser.rargs[:len(value)]
    setattr(parser.values, option.dest, value)

...
parser.add_option("-c", "--callback", dest="vararg_attr",
                  action="callback", callback=vararg_callback)

36.1.5. Extending optparse

Since the two major controlling factors in how optparse interprets command-line options are the action and type of each option, the most likely direction of extension is to add new actions and new types.

36.1.5.1. Adding new types

To add new types, you need to define your own subclass of optparse’s Option class. This class has a couple of attributes that define optparse’s types: TYPES and TYPE_CHECKER.

Option.TYPES

A tuple of type names; in your subclass, simply define a new tuple TYPES that builds on the standard one.

Option.TYPE_CHECKER

A dictionary mapping type names to type-checking functions. A type-checking function has the following signature:

def check_mytype(option, opt, value)

where option is an Option instance, opt is an option string (e.g., -f), and value is the string from the command line that must be checked and converted to your desired type. check_mytype() should return an object of the hypothetical type mytype. The value returned by a type-checking function will wind up in the OptionValues instance returned by OptionParser.parse_args(), or be passed to a callback as the value parameter.

Your type-checking function should raise OptionValueError if it encounters any problems. OptionValueError takes a single string argument, which is passed as-is to OptionParser’s error() method, which in turn prepends the program name and the string "error:" and prints everything to stderr before terminating the process.

Here’s a silly example that demonstrates adding a "complex" option type to parse Python-style complex numbers on the command line. (This is even sillier than it used to be, because optparse 1.3 added built-in support for complex numbers, but never mind.)

First, the necessary imports:

from copy import copy
from optparse import Option, OptionValueError

You need to define your type-checker first, since it’s referred to later (in the TYPE_CHECKER class attribute of your Option subclass):

def check_complex(option, opt, value):
    try:
        return complex(value)
    except ValueError:
        raise OptionValueError(
            "option %s: invalid complex value: %r" % (opt, value))

Finally, the Option subclass:

class MyOption (Option):
    TYPES = Option.TYPES + ("complex",)
    TYPE_CHECKER = copy(Option.TYPE_CHECKER)
    TYPE_CHECKER["complex"] = check_complex

(If we didn’t make a copy() of Option.TYPE_CHECKER, we would end up modifying the TYPE_CHECKER attribute of optparse’s Option class. This being Python, nothing stops you from doing that except good manners and common sense.)

That’s it! Now you can write a script that uses the new option type just like any other optparse-based script, except you have to instruct your OptionParser to use MyOption instead of Option:

parser = OptionParser(option_class=MyOption)
parser.add_option("-c", type="complex")

Alternately, you can build your own option list and pass it to OptionParser; if you don’t use add_option() in the above way, you don’t need to tell OptionParser which option class to use:

option_list = [MyOption("-c", action="store", type="complex", dest="c")]
parser = OptionParser(option_list=option_list)

36.1.5.2. Adding new actions

Adding new actions is a bit trickier, because you have to understand that optparse has a couple of classifications for actions:

“store” actions

actions that result in optparse storing a value to an attribute of the current OptionValues instance; these options require a dest attribute to be supplied to the Option constructor.

“typed” actions

actions that take a value from the command line and expect it to be of a certain type; or rather, a string that can be converted to a certain type. These options require a type attribute to the Option constructor.

These are overlapping sets: some default “store” actions are "store", "store_const", "append", and "count", while the default “typed” actions are "store", "append", and "callback".

When you add an action, you need to categorize it by listing it in at least one of the following class attributes of Option (all are lists of strings):

Option.ACTIONS

All actions must be listed in ACTIONS.

Option.STORE_ACTIONS

“store” actions are additionally listed here.

Option.TYPED_ACTIONS

“typed” actions are additionally listed here.

Option.ALWAYS_TYPED_ACTIONS

Actions that always take a type (i.e. whose options always take a value) are additionally listed here. The only effect of this is that optparse assigns the default type, "string", to options with no explicit type whose action is listed in ALWAYS_TYPED_ACTIONS.

In order to actually implement your new action, you must override Option’s take_action() method and add a case that recognizes your action.

For example, let’s add an "extend" action. This is similar to the standard "append" action, but instead of taking a single value from the command-line and appending it to an existing list, "extend" will take multiple values in a single comma-delimited string, and extend an existing list with them. That is, if --names is an "extend" option of type "string", the command line

--names=foo,bar --names blah --names ding,dong

would result in a list

["foo", "bar", "blah", "ding", "dong"]

Again we define a subclass of Option:

class MyOption(Option):

    ACTIONS = Option.ACTIONS + ("extend",)
    STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
    TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
    ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)

    def take_action(self, action, dest, opt, value, values, parser):
        if action == "extend":
            lvalue = value.split(",")
            values.ensure_value(dest, []).extend(lvalue)
        else:
            Option.take_action(
                self, action, dest, opt, value, values, parser)

Features of note:

"extend" both expects a value on the command-line and stores that value somewhere, so it goes in both STORE_ACTIONS and TYPED_ACTIONS.

to ensure that optparse assigns the default type of "string" to "extend" actions, we put the "extend" action in ALWAYS_TYPED_ACTIONS as well.

MyOption.take_action() implements just this one new action, and passes control back to Option.take_action() for the standard optparse actions.

values is an instance of the optparse_parser.Values class, which provides the very useful ensure_value() method. ensure_value() is essentially getattr() with a safety valve; it is called as

values.ensure_value(attr, value)

If the attr attribute of values doesn’t exist or is None, then ensure_value() first sets it to value, and then returns ‘value. This is very handy for actions like "extend", "append", and "count", all of which accumulate data in a variable and expect that variable to be of a certain type (a list for the first two, an integer for the latter). Using ensure_value() means that scripts using your action don’t have to worry about setting a default value for the option destinations in question; they can just leave the default as None and ensure_value() will take care of getting it right when it’s needed.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

36. 被取代的模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

36. 被取代的模块 »

|

36.2. imp — Access the import internals

Source code: Lib/imp.py [https://github.com/python/cpython/tree/3.6/Lib/imp.py]

3.4 版后已移除: The imp package is pending deprecation in favor of importlib.

This module provides an interface to the mechanisms used to implement the import statement. It defines the following constants and functions:

imp.get_magic()

Return the magic string value used to recognize byte-compiled code files (.pyc files). (This value may be different for each Python version.)

3.4 版后已移除: Use importlib.util.MAGIC_NUMBER instead.

imp.get_suffixes()

Return a list of 3-element tuples, each describing a particular type of module. Each triple has the form (suffix, mode, type), where suffix is a string to be appended to the module name to form the filename to search for, mode is the mode string to pass to the built-in open() function to open the file (this can be 'r' for text files or 'rb' for binary files), and type is the file type, which has one of the values PY_SOURCE, PY_COMPILED, or C_EXTENSION, described below.

3.3 版后已移除: Use the constants defined on importlib.machinery instead.

imp.find_module(name[, path])

Try to find the module name. If path is omitted or None, the list of directory names given by sys.path is searched, but first a few special places are searched: the function tries to find a built-in module with the given name (C_BUILTIN), then a frozen module (PY_FROZEN), and on some systems some other places are looked in as well (on Windows, it looks in the registry which may point to a specific file).

Otherwise, path must be a list of directory names; each directory is searched for files with any of the suffixes returned by get_suffixes() above. Invalid names in the list are silently ignored (but all list items must be strings).

If search is successful, the return value is a 3-element tuple (file, pathname, description):

file is an open file object positioned at the beginning, pathname is the pathname of the file found, and description is a 3-element tuple as contained in the list returned by get_suffixes() describing the kind of module found.

If the module does not live in a file, the returned file is None, pathname is the empty string, and the description tuple contains empty strings for its suffix and mode; the module type is indicated as given in parentheses above. If the search is unsuccessful, ImportError is raised. Other exceptions indicate problems with the arguments or environment.

If the module is a package, file is None, pathname is the package path and the last item in the description tuple is PKG_DIRECTORY.

This function does not handle hierarchical module names (names containing dots). In order to find P.M, that is, submodule M of package P, use find_module() and load_module() to find and load package P, and then use find_module() with the path argument set to P.__path__. When P itself has a dotted name, apply this recipe recursively.

3.3 版后已移除: Use importlib.util.find_spec() instead unless Python 3.3 compatibility is required, in which case use importlib.find_loader(). For example usage of the former case, see the 例子 section of the importlib documentation.

imp.load_module(name, file, pathname, description)

Load a module that was previously found by find_module() (or by an otherwise conducted search yielding compatible results). This function does more than importing the module: if the module was already imported, it will reload the module! The name argument indicates the full module name (including the package name, if this is a submodule of a package). The file argument is an open file, and pathname is the corresponding file name; these can be None and '', respectively, when the module is a package or not being loaded from a file. The description argument is a tuple, as would be returned by get_suffixes(), describing what kind of module must be loaded.

If the load is successful, the return value is the module object; otherwise, an exception (usually ImportError) is raised.

Important: the caller is responsible for closing the file argument, if it was not None, even when an exception is raised. This is best done using a try … finally statement.

3.3 版后已移除: If previously used in conjunction with imp.find_module() then consider using importlib.import_module(), otherwise use the loader returned by the replacement you chose for imp.find_module(). If you called imp.load_module() and related functions directly with file path arguments then use a combination of importlib.util.spec_from_file_location() and importlib.util.module_from_spec(). See the 例子 section of the importlib documentation for details of the various approaches.

imp.new_module(name)

Return a new empty module object called name. This object is not inserted in sys.modules.

3.4 版后已移除: Use importlib.util.module_from_spec() instead.

imp.reload(module)

Reload a previously imported module. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter. The return value is the module object (the same as the module argument).

When reload(module) is executed:

Python modules’ code is recompiled and the module-level code reexecuted, defining a new set of objects which are bound to names in the module’s dictionary. The init function of extension modules is not called a second time.

与Python中的所有的其它对象一样，旧的对象只有在它们的引用计数为0之后才会被回收。

模块命名空间中的名称重新指向任何新的或更改后的对象。

其他旧对象的引用（例如那个模块的外部名称）不会被重新绑定到引用的新对象的，并且如果有需要，必须在出现的每个命名空间中进行更新。

有一些其他注意事项：

当一个模块被重新加载的时候，它的字典（包含了那个模块的全区变量）会被保留。名称的重新定义会覆盖旧的定义，所以通常来说这不是问题。如果一个新模块没有定义在旧版本模块中定义的名称，则将保留旧版本中的定义。这一特性可用于作为那个模块的优点，如果它维护一个全局表或者对象的缓存 —— 使用 try 语句，就可以测试表的存在并且跳过它的初始化，如果有需要的话:

try:
    cache
except NameError:
    cache = {}

It is legal though generally not very useful to reload built-in or dynamically loaded modules, except for sys, __main__ and builtins. In many cases, however, extension modules are not designed to be initialized more than once, and may fail in arbitrary ways when reloaded.

If a module imports objects from another module using from … import …, calling reload() for the other module does not redefine the objects imported from it — one way around this is to re-execute the from statement, another is to use import and qualified names (module.*name*) instead.

如果一个模块创建一个类的实例，重新加载定义那个类的模块不影响那些实例的方法定义———它们继续使用旧类中的定义。对于子类来说同样是正确的。

在 3.3 版更改: Relies on both __name__ and __loader__ being defined on the module being reloaded instead of just __name__.

3.4 版后已移除: Use importlib.reload() instead.

The following functions are conveniences for handling PEP 3147 [https://www.python.org/dev/peps/pep-3147] byte-compiled file paths.

3.2 新版功能.

imp.cache_from_source(path, debug_override=None)

Return the PEP 3147 [https://www.python.org/dev/peps/pep-3147] path to the byte-compiled file associated with the source path. For example, if path is /foo/bar/baz.py the return value would be /foo/bar/__pycache__/baz.cpython-32.pyc for Python 3.2. The cpython-32 string comes from the current magic tag (see get_tag(); if sys.implementation.cache_tag is not defined then NotImplementedError will be raised). By passing in True or False for debug_override you can override the system’s value for __debug__, leading to optimized bytecode.

path need not exist.

在 3.3 版更改: If sys.implementation.cache_tag is None, then NotImplementedError is raised.

3.4 版后已移除: Use importlib.util.cache_from_source() instead.

在 3.5 版更改: The debug_override parameter no longer creates a .pyo file.

imp.source_from_cache(path)

Given the path to a PEP 3147 [https://www.python.org/dev/peps/pep-3147] file name, return the associated source code file path. For example, if path is /foo/bar/__pycache__/baz.cpython-32.pyc the returned path would be /foo/bar/baz.py. path need not exist, however if it does not conform to PEP 3147 [https://www.python.org/dev/peps/pep-3147] format, a ValueError is raised. If sys.implementation.cache_tag is not defined, NotImplementedError is raised.

在 3.3 版更改: Raise NotImplementedError when sys.implementation.cache_tag is not defined.

3.4 版后已移除: Use importlib.util.source_from_cache() instead.

imp.get_tag()

Return the PEP 3147 [https://www.python.org/dev/peps/pep-3147] magic tag string matching this version of Python’s magic number, as returned by get_magic().

3.4 版后已移除: Use sys.implementation.cache_tag directly starting in Python 3.3.

The following functions help interact with the import system’s internal locking mechanism. Locking semantics of imports are an implementation detail which may vary from release to release. However, Python ensures that circular imports work without any deadlocks.

imp.lock_held()

Return True if the global import lock is currently held, else False. On platforms without threads, always return False.

On platforms with threads, a thread executing an import first holds a global import lock, then sets up a per-module lock for the rest of the import. This blocks other threads from importing the same module until the original import completes, preventing other threads from seeing incomplete module objects constructed by the original thread. An exception is made for circular imports, which by construction have to expose an incomplete module object at some point.

在 3.3 版更改: The locking scheme has changed to per-module locks for the most part. A global import lock is kept for some critical tasks, such as initializing the per-module locks.

3.4 版后已移除.

imp.acquire_lock()

Acquire the interpreter’s global import lock for the current thread. This lock should be used by import hooks to ensure thread-safety when importing modules.

Once a thread has acquired the import lock, the same thread may acquire it again without blocking; the thread must release it once for each time it has acquired it.

On platforms without threads, this function does nothing.

在 3.3 版更改: The locking scheme has changed to per-module locks for the most part. A global import lock is kept for some critical tasks, such as initializing the per-module locks.

3.4 版后已移除.

imp.release_lock()

Release the interpreter’s global import lock. On platforms without threads, this function does nothing.

在 3.3 版更改: The locking scheme has changed to per-module locks for the most part. A global import lock is kept for some critical tasks, such as initializing the per-module locks.

3.4 版后已移除.

The following constants with integer values, defined in this module, are used to indicate the search result of find_module().

imp.PY_SOURCE

The module was found as a source file.

3.3 版后已移除.

imp.PY_COMPILED

The module was found as a compiled code object file.

3.3 版后已移除.

imp.C_EXTENSION

The module was found as dynamically loadable shared library.

3.3 版后已移除.

imp.PKG_DIRECTORY

The module was found as a package directory.

3.3 版后已移除.

imp.C_BUILTIN

The module was found as a built-in module.

3.3 版后已移除.

imp.PY_FROZEN

The module was found as a frozen module.

3.3 版后已移除.

class imp.NullImporter(path_string)

The NullImporter type is a PEP 302 [https://www.python.org/dev/peps/pep-0302] import hook that handles non-directory path strings by failing to find any modules. Calling this type with an existing directory or empty string raises ImportError. Otherwise, a NullImporter instance is returned.

Instances have only one method:

find_module(fullname[, path])

This method always returns None, indicating that the requested module could not be found.

在 3.3 版更改: None is inserted into sys.path_importer_cache instead of an instance of NullImporter.

3.4 版后已移除: Insert None into sys.path_importer_cache instead.

36.2.1. 例子

The following function emulates what was the standard import statement up to Python 1.4 (no hierarchical module names). (This implementation wouldn’t work in that version, since find_module() has been extended and load_module() has been added in 1.4.)

import imp
import sys

def __import__(name, globals=None, locals=None, fromlist=None):
    # Fast path: see if the module has already been imported.
    try:
        return sys.modules[name]
    except KeyError:
        pass

    # If any of the following calls raises an exception,
    # there's a problem we can't handle -- let the caller handle it.

    fp, pathname, description = imp.find_module(name)

    try:
        return imp.load_module(name, fp, pathname, description)
    finally:
        # Since we may exit via an exception, close fp explicitly.
        if fp:
            fp.close()

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

36. 被取代的模块 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

37. 未创建文档的模块

以下是目前未创建文档模块的速览列表，但其它们应创建文档。 欢迎为他们提供文档！ （通过电子邮件发送到 docs@python.org ）

本章的想法和原内容取自 Fredrik Lundh 的帖子；本章的具体内容已经大幅修改。

37.1. 平台特定模块

这些模块用于实现 os.path 模块，除此之外没有文档。几乎没有必要创建这些文档。

ntpath

— 在 Win32 和 Win64 平台上实现 os.path 。

posixpath

— 在 POSIX 上实现 os.path 。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 标准库 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

扩展和嵌入 Python 解释器

本文档描述了如何使用 C 或 C++ 编写模块以使用新模块来扩展 Python 解释器的功能。 这些模块不仅可以定义新的函数，还可以定义新的对象类型及其方法。 该文档还描述了如何将 Python 解释器嵌入到另一个应用程序中，以用作扩展语言。 最后，它展示了如何编译和链接扩展模块，以便它们可以动态地（在运行时）加载到解释器中，如果底层操作系统支持此特性的话。

本文档假设你具备有关 Python 的基本知识。有关该语言的非正式介绍，请参阅 Python 教程 。 Python语言参考 给出了更正式的语言定义。 Python 标准库 包含现有的对象类型、函数和模块（内置和用 Python 编写）的文档，使语言具有广泛的应用范围。

关于整个 Python/C API 的详细介绍，请参阅独立的 Python/C API 参考手册 。

推荐的第三方工具

本指南仅介绍了作为此 CPython 版本的一部分提供的创建扩展的基本工具。 第三方工具，如 Cython [http://cython.org/] 、 cffi [https://cffi.readthedocs.io] 、 SWIG [http://www.swig.org] 和 Numba [https://numba.pydata.org/] 提供了更简单和更复杂的方法来为 Python 创建 C 和 C ++ 扩展。

参见

Python Packaging User Guide: Binary Extensions [https://packaging.python.org/en/latest/extensions/]

“ Python Packaging User Guide ”不仅涵盖了几个简化二进制扩展创建的可用工具，还讨论了为什么首先创建扩展模块的各种原因。

不使用第三方工具创建扩展

本指南的这一部分包括在没有第三方工具帮助的情况下创建 C 和 C ++ 扩展。它主要用于这些工具的创建者，而不是建议你创建自己的 C 扩展的方法。

1. 使用 C 或 C++ 扩展 Python

1.1. 一个简单的例子

1.2. 关于错误和异常

1.3. 回到例子

1.4. 模块方法表和初始化函数

1.5. 编译和链接

1.6. 在C中调用Python函数

1.7. 提取扩展函数的参数

1.8. 给扩展函数的关键字参数

1.9. 构造任意值

1.10. 引用计数

1.11. 在C++中编写扩展

1.12. 给扩展模块提供C API

2. 自定义扩展类型：教程

2.1. 基础

2.2. Adding data and methods to the Basic example

2.3. Providing finer control over data attributes

2.4. Supporting cyclic garbage collection

2.5. Subclassing other types

3. 定义扩展类型：已分类主题

3.1. 终结和内存释放

3.2. 对象展示

3.3. 属性管理

3.4. Object Comparison

3.5. Abstract Protocol Support

3.6. Weak Reference Support

3.7. 更多建议

4. 构建C/C++扩展

4.1. 使用distutils构建C和C++扩展

4.2. 发布你的扩展模块

5. 在Windows平台编译C和C++扩展

5.1. 菜谱式说明

5.2. Unix 和 Windows 之间的差异

5.3. DLL 的实际使用

在更大的应用程序中嵌入 CPython 运行时

有时，不是要创建在 Python 解释器中作为主应用程序运行的扩展，而是希望将 CPython 运行时嵌入到更大的应用程序中。 本节介绍了成功完成此操作所涉及的一些细节。

1. 在其它应用程序嵌入 Python

1.1. Very High Level Embedding

1.2. Beyond Very High Level Embedding: An overview

1.3. 纯嵌入

1.4. Extending Embedded Python

1.5. 在 C++ 中嵌入 Python

1.6. 在类 Unix 系统中编译和链接

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

扩展和嵌入 Python 解释器 »

|

1. 使用 C 或 C++ 扩展 Python

如果你会用 C，添加新的 Python 内置模块会很简单。以下两件不能用 Python 直接做的事，可以通过 extension modules 来实现：实现新的内置对象类型；调用 C 的库函数和系统调用。

为了支持扩展，Python API（应用程序编程接口）定义了一系列函数、宏和变量，可以访问 Python 运行时系统的大部分内容。Python 的 API 可以通过在一个 C 源文件中引用 "Python.h" 头文件来使用。

扩展模块的编写方式取决与你的目的以及系统设置；下面章节会详细介绍。

注解

The C extension interface is specific to CPython, and extension modules do not work on other Python implementations. In many cases, it is possible to avoid writing C extensions and preserve portability to other implementations. For example, if your use case is calling C library functions or system calls, you should consider using the ctypes module or the cffi [https://cffi.readthedocs.org] library rather than writing custom C code. These modules let you write Python code to interface with C code and are more portable between implementations of Python than writing and compiling a C extension module.

1.1. 一个简单的例子

让我们创建一个扩展模块 spam (Monty Python 粉丝最喜欢的食物…) 并且想要创建对应 C 库函数 system() 1 的 Python 接口。 这个函数接受一个以 null 结尾的字符串参数并返回一个整数。 我们希望可以在 Python 中以如下方式调用此函数:

>>> import spam
>>> status = spam.system("ls -l")

首先创建一个 spammodule.c 文件。（传统上，如果一个模块叫 spam，则对应实现它的 C 文件叫 spammodule.c；如果这个模块名字非常长，比如 spammify，则这个模块的文件可以直接叫 spammify.c。）

The first line of our file can be:

#include <Python.h>

这会导入 Python API（如果你喜欢，你可以在这里添加描述模块目标和版权信息的注释)。

注解

由于 Python 可能会定义一些能在某些系统上影响标准头文件的预处理器定义，因此在包含任何标准头文件之前，你 必须 先包含 Python.h。

所有在 Python.h 中定义的用户可见的符号都具有 Py 或 PY 前缀，已在标准头文件中定义的那些除外。 考虑到便利性，也由于其在 Python 解释器中被广泛使用，"Python.h" 还包含了一些标准头文件: <stdio.h>，<string.h>，<errno.h> 和 <stdlib.h>。 如果后面的头文件在你的系统上不存在，它还会直接声明函数 malloc()，free() 和 realloc()。

下面添加C函数到扩展模块，当调用 spam.system(string) 时会做出响应，(我们稍后会看到调用)：

static PyObject *
spam_system(PyObject *self, PyObject *args)
{
    const char *command;
    int sts;

    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;
    sts = system(command);
    return PyLong_FromLong(sts);
}

有个直接翻译参数列表的方法(举个例子，单独的 "ls -l" )到要传递给C函数的参数。C函数总是有两个参数，通常名字是 self 和 args 。

对模块级函数， self 参数指向模块对象；对于对象实例则指向方法。

args 参数是指向一个 Python 的 tuple 对象的指针，其中包含参数。 每个 tuple 项对应一个调用参数。 这些参数也全都是 Python 对象 — 要在我们的 C 函数中使用它们就需要先将其转换为 C 值。 Python API 中的函数 PyArg_ParseTuple() 会检查参数类型并将其转换为 C 值。 它使用模板字符串确定需要的参数类型以及存储被转换的值的 C 变量类型。 细节将稍后说明。

PyArg_ParseTuple() returns true (nonzero) if all arguments have the right type and its components have been stored in the variables whose addresses are passed. It returns false (zero) if an invalid argument list was passed. In the latter case it also raises an appropriate exception so the calling function can return NULL immediately (as we saw in the example).

1.2. 关于错误和异常

An important convention throughout the Python interpreter is the following: when a function fails, it should set an exception condition and return an error value (usually a NULL pointer). Exceptions are stored in a static global variable inside the interpreter; if this variable is NULL no exception has occurred. A second global variable stores the “associated value” of the exception (the second argument to raise). A third variable contains the stack traceback in case the error originated in Python code. These three variables are the C equivalents of the result in Python of sys.exc_info() (see the section on module sys in the Python Library Reference). It is important to know about them to understand how errors are passed around.

Python API中定义了一些函数来设置这些变量。

最常用的就是 PyErr_SetString()。 其参数是异常对象和 C 字符串。 异常对象一般是像 PyExc_ZeroDivisionError 这样的预定义对象。 C 字符串指明异常原因，并被转换为一个 Python 字符串对象存储为异常的“关联值”。

另一个有用的函数是 PyErr_SetFromErrno() ，仅接受一个异常对象，异常描述包含在全局变量 errno 中。最通用的函数还是 PyErr_SetObject() ，包含两个参数，分别为异常对象和异常描述。你不需要使用 Py_INCREF() 来增加传递到其他函数的参数对象的引用计数。

You can test non-destructively whether an exception has been set with PyErr_Occurred(). This returns the current exception object, or NULL if no exception has occurred. You normally don’t need to call PyErr_Occurred() to see whether an error occurred in a function call, since you should be able to tell from the return value.

When a function f that calls another function g detects that the latter fails, f should itself return an error value (usually NULL or -1). It should not call one of the PyErr_*() functions — one has already been called by g. f’s caller is then supposed to also return an error indication to its caller, again without calling PyErr_*(), and so on — the most detailed cause of the error was already reported by the function that first detected it. Once the error reaches the Python interpreter’s main loop, this aborts the currently executing Python code and tries to find an exception handler specified by the Python programmer.

（在某些情况下，当模块确实能够通过调用其它 PyErr_*() 函数给出更加详细的错误消息，并且在这些情况是可以这样做的。 但是按照一般规则，这是不必要的，并可能导致有关错误原因的信息丢失：大多数操作会由于种种原因而失败。）

想要忽略由一个失败的函数调用所设置的异常，异常条件必须通过调用 PyErr_Clear() 显式地被清除。 C 代码应当调用 PyErr_Clear() 的唯一情况是如果它不想将错误传给解释器而是想完全由自己来处理它（可能是尝试其他方法，或是假装没有出错）。

每次失败的 malloc() 调用必须转换为一个异常。 malloc() (或 realloc() )的直接调用者必须调用 PyErr_NoMemory() 来返回错误来提示。所有对象创建函数(例如 PyLong_FromLong() )已经这么做了，所以这个提示仅用于直接调用 malloc() 的情况。

还要注意的是，除了 PyArg_ParseTuple() 等重要的例外，返回整数状态码的函数通常都是返回正值或零来表示成功，而以 -1 表示失败，如同 Unix 系统调用一样。

最后，当你返回一个错误指示器时要注意清理垃圾（通过为你已经创建的对象执行 Py_XDECREF() 或 Py_DECREF() 调用）！

选择引发哪个异常完全取决于你的喜好。 所有内置的 Python 异常都有对应的预声明 C 对象，例如 PyExc_ZeroDivisionError，你可以直接使用它们。 当然，你应当明智地选择异常 — 不要使用 PyExc_TypeError 来表示一个文件无法被打开 (那大概应该用 PyExc_IOError)。 如果参数列表有问题，PyArg_ParseTuple() 函数通常会引发 PyExc_TypeError。 如果你想要一个参数的值必须处于特定范围之内或必须满足其他条件，则适宜使用 PyExc_ValueError。

你也可以为你的模块定义一个唯一的新异常。需要在文件前部声明一个静态对象变量，如:

static PyObject *SpamError;

and initialize it in your module’s initialization function (PyInit_spam()) with an exception object (leaving out the error checking for now):

PyMODINIT_FUNC
PyInit_spam(void)
{
    PyObject *m;

    m = PyModule_Create(&spammodule);
    if (m == NULL)
        return NULL;

    SpamError = PyErr_NewException("spam.error", NULL, NULL);
    Py_INCREF(SpamError);
    PyModule_AddObject(m, "error", SpamError);
    return m;
}

Note that the Python name for the exception object is spam.error. The PyErr_NewException() function may create a class with the base class being Exception (unless another class is passed in instead of NULL), described in 内置异常.

同样注意的是创建类保存了 SpamError 的一个引用，这是有意的。为了防止被垃圾回收掉，否则 SpamError 随时会成为野指针。

一会讨论 PyMODINIT_FUNC 作为函数返回类型的用法。

spam.error 异常可以在扩展模块中抛出，通过 PyErr_SetString() 函数调用，如下：

static PyObject *
spam_system(PyObject *self, PyObject *args)
{
    const char *command;
    int sts;

    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;
    sts = system(command);
    if (sts < 0) {
        PyErr_SetString(SpamError, "System command failed");
        return NULL;
    }
    return PyLong_FromLong(sts);
}

1.3. 回到例子

回到前面的例子，你应该明白下面的代码:

if (!PyArg_ParseTuple(args, "s", &command))
    return NULL;

It returns NULL (the error indicator for functions returning object pointers) if an error is detected in the argument list, relying on the exception set by PyArg_ParseTuple(). Otherwise the string value of the argument has been copied to the local variable command. This is a pointer assignment and you are not supposed to modify the string to which it points (so in Standard C, the variable command should properly be declared as const char *command).

下一个语句使用UNIX系统函数 system() ，传递给他的参数是刚才从 PyArg_ParseTuple() 取出的:

sts = system(command);

我们的 spam.system() 函数必须返回 sts 的值作为Python对象。这通过使用函数 PyLong_FromLong() 来实现。

return PyLong_FromLong(sts);

在这种情况下，会返回一个整数对象，(这个对象会在Python堆里面管理)。

如果你的C函数没有有用的返回值(返回 void 的函数)，则必须返回 None 。(你可以用 Py_RETUN_NONE 宏来完成):

Py_INCREF(Py_None);
return Py_None;

Py_None is the C name for the special Python object None. It is a genuine Python object rather than a NULL pointer, which means “error” in most contexts, as we have seen.

1.4. 模块方法表和初始化函数

为了展示 spam_system() 如何被Python程序调用。把函数声明为可以被Python调用，需要先定义一个方法表 “method table” 。

static PyMethodDef SpamMethods[] = {
    ...
    {"system",  spam_system, METH_VARARGS,
     "Execute a shell command."},
    ...
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

注意第三个参数 ( METH_VARARGS ) ，这个标志指定会使用C的调用惯例。可选值有 METH_VARARGS 、 METH_VARARGS | METH_KEYWORDS 。值 0 代表使用 PyArg_ParseTuple() 的陈旧变量。

如果单独使用 METH_VARARGS ，函数会等待Python传来tuple格式的参数，并最终使用 PyArg_ParseTuple() 进行解析。

METH_KEYWORDS 值表示接受关键字参数。这种情况下C函数需要接受第三个 PyObject * 对象，表示字典参数，使用 PyArg_ParseTupleAndKeywords() 来解析出参数。

这个方法表必须被模块定义结构所引用。

static struct PyModuleDef spammodule = {
    PyModuleDef_HEAD_INIT,
    "spam",   /* name of module */
    spam_doc, /* module documentation, may be NULL */
    -1,       /* size of per-interpreter state of the module,
                 or -1 if the module keeps state in global variables. */
    SpamMethods
};

这个结构体必须传递给解释器的模块初始化函数。初始化函数必须命名为 PyInit_name() ，其中 name 是模块的名字，并应该定义为非 static ，且在模块文件里：

PyMODINIT_FUNC
PyInit_spam(void)
{
    return PyModule_Create(&spammodule);
}

注意 PyMODINIT_FUNC 声明了函数作为 PyObject * 返回类型，声明任何平台的链接生命，以及给C++生命函数的 extern "C" 。

When the Python program imports module spam for the first time, PyInit_spam() is called. (See below for comments about embedding Python.) It calls PyModule_Create(), which returns a module object, and inserts built-in function objects into the newly created module based upon the table (an array of PyMethodDef structures) found in the module definition. PyModule_Create() returns a pointer to the module object that it creates. It may abort with a fatal error for certain errors, or return NULL if the module could not be initialized satisfactorily. The init function must return the module object to its caller, so that it then gets inserted into sys.modules.

当嵌入Python时， PyInit_spam() 函数不会被自动调用，除非放在 PyImport_Inittab 表里。要添加模块到初始化表，使用 PyImport_AppendInittab() ，可选的跟着一个模块的导入。

int
main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    if (program == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }

    /* Add a built-in module, before Py_Initialize */
    PyImport_AppendInittab("spam", PyInit_spam);

    /* Pass argv[0] to the Python interpreter */
    Py_SetProgramName(program);

    /* Initialize the Python interpreter.  Required. */
    Py_Initialize();

    /* Optionally import the module; alternatively,
       import can be deferred until the embedded script
       imports it. */
    PyImport_ImportModule("spam");

    ...

    PyMem_RawFree(program);
    return 0;
}

注解

要从 sys.modules 删除实体或导入已编译模块到一个进程里的多个解释器(或使用 fork() 而没用 exec() )会在一些扩展模块上产生错误。扩展模块作者可以在初始化内部数据结构时给出警告。

更多关于模块的现实的例子包含在Python源码包的 Modules/xxmodule.c 中。这些文件可以用作你的代码模板，或者学习。脚本 modulator.py 包含在源码发行版或Windows安装中，提供了一个简单的GUI，用来声明需要实现的函数和对象，并且可以生成供填入的模板。脚本在 Tools/modulator/ 目录。查看README以了解用法。

注解

不像我们的 spam 例子， xxmodule 使用了 多阶段初始化 (Python3.5开始引入)， PyInit_spam 会返回一个 PyModuleDef 结构体，然后创建的模块放到导入机制。细节参考 PEP 489 [https://www.python.org/dev/peps/pep-0489] 的多阶段初始化。

1.5. 编译和链接

在你能使用你的新写的扩展之前，你还需要做两件事情：使用 Python 系统来编译和链接。如果你使用动态加载，这取决于你使用的操作系统的动态加载机制；更多信息请参考编译扩展模块的章节（ 构建C/C++扩展 章节），以及在 Windows 上编译需要的额外信息（ 在Windows平台编译C和C++扩展 章节）。

如果你不使用动态加载，或者想要让模块永久性的作为Python解释器的一部分，就必须修改配置设置，并重新构建解释器。幸运的是在Unix上很简单，只需要把你的文件 ( spammodule.c 为例) 放在解压缩源码发行包的 Modules/ 目录下，添加一行到 Modules/Setup.local 来描述你的文件：

spam spammodule.o

然后在顶层目录运行 make 来重新构建解释器。你也可以在 Modules/ 子目录使用 make，但是你必须先重建 Makefile 文件，然后运行 ‘make Makefile’ 命令。（你每次修改 Setup 文件都需要这样操作。）

如果你的模块需要额外的链接，这些内容可以列出在配置文件里，举个实例：

spam spammodule.o -lX11

1.6. 在C中调用Python函数

迄今为止，我们一直把注意力集中于让Python调用C函数，其实反过来也很有用，就是用C调用Python函数。这在回调函数中尤其有用。如果一个C接口使用回调，那么就要实现这个回调机制。

幸运的是，Python解释器是比较方便回调的，并给标准Python函数提供了标准接口。(这里就不再详述解析Python字符串作为输入的方式，如果有兴趣可以参考 Python/pythonmain.c 中的 -c 命令行代码。)

调用Python函数很简单，首先Python程序要传递Python函数对象。应该提供个函数(或其他接口)来实现。当调用这个函数时，用全局变量保存Python函数对象的指针，还要调用 (Py_INCREF()) 来增加引用计数，当然不用全局变量也没什么关系。举个例子，如下函数可能是模块定义的一部分：

static PyObject *my_callback = NULL;

static PyObject *
my_set_callback(PyObject *dummy, PyObject *args)
{
    PyObject *result = NULL;
    PyObject *temp;

    if (PyArg_ParseTuple(args, "O:set_callback", &temp)) {
        if (!PyCallable_Check(temp)) {
            PyErr_SetString(PyExc_TypeError, "parameter must be callable");
            return NULL;
        }
        Py_XINCREF(temp);         /* Add a reference to new callback */
        Py_XDECREF(my_callback);  /* Dispose of previous callback */
        my_callback = temp;       /* Remember new callback */
        /* Boilerplate to return "None" */
        Py_INCREF(Py_None);
        result = Py_None;
    }
    return result;
}

这个函数必须使用 METH_VARARGS 标志注册到解释器，这在 模块方法表和初始化函数 章节会描述。 PyArg_ParseTuple() 函数及其参数的文档在 提取扩展函数的参数 。

The macros Py_XINCREF() and Py_XDECREF() increment/decrement the reference count of an object and are safe in the presence of NULL pointers (but note that temp will not be NULL in this context). More info on them in section 引用计数.

Later, when it is time to call the function, you call the C function PyObject_CallObject(). This function has two arguments, both pointers to arbitrary Python objects: the Python function, and the argument list. The argument list must always be a tuple object, whose length is the number of arguments. To call the Python function with no arguments, pass in NULL, or an empty tuple; to call it with one argument, pass a singleton tuple. Py_BuildValue() returns a tuple when its format string consists of zero or more format codes between parentheses. For example:

int arg;
PyObject *arglist;
PyObject *result;
...
arg = 123;
...
/* Time to call the callback */
arglist = Py_BuildValue("(i)", arg);
result = PyObject_CallObject(my_callback, arglist);
Py_DECREF(arglist);

PyObject_CallObject() 返回Python对象指针，这也是Python函数的返回值。 PyObject_CallObject() 是一个对其参数 “引用计数无关” 的函数。例子中新的元组创建用于参数列表，并且在 PyObject_CallObject() 之后立即使用了 Py_DECREF() 。

PyEval_CallObject() 的返回值总是“新”的：要么是一个新建的对象；要么是已有对象，但增加了引用计数。所以除非你想把结果保存在全局变量中，你需要对这个值使用 Py_DECREF()，即使你对里面的内容（特别！）不感兴趣。

Before you do this, however, it is important to check that the return value isn’t NULL. If it is, the Python function terminated by raising an exception. If the C code that called PyObject_CallObject() is called from Python, it should now return an error indication to its Python caller, so the interpreter can print a stack trace, or the calling Python code can handle the exception. If this is not possible or desirable, the exception should be cleared by calling PyErr_Clear(). For example:

if (result == NULL)
    return NULL; /* Pass error back */
...use result...
Py_DECREF(result);

依赖于具体的回调函数，你还要提供一个参数列表到 PyEval_CallObject() 。在某些情况下参数列表是由Python程序提供的，通过接口再传到回调函数对象。这样就可以不改变形式直接传递。另外一些时候你要构造一个新的元组来传递参数。最简单的方法就是 Py_BuildValue() 函数构造tuple。举个例子，你要传递一个事件代码时可以用如下代码:

PyObject *arglist;
...
arglist = Py_BuildValue("(l)", eventcode);
result = PyObject_CallObject(my_callback, arglist);
Py_DECREF(arglist);
if (result == NULL)
    return NULL; /* Pass error back */
/* Here maybe use the result */
Py_DECREF(result);

注意 Py_DECREF(arglist) 所在处会立即调用，在错误检查之前。当然还要注意一些常规的错误，比如 Py_BuildValue() 可能会遭遇内存不足等等。

当你调用函数时还需要注意，用关键字参数调用 PyObject_Call() ，需要支持普通参数和关键字参数。有如如上例子中，我们使用 Py_BuildValue() 来构造字典。

PyObject *dict;
...
dict = Py_BuildValue("{s:i}", "name", val);
result = PyObject_Call(my_callback, NULL, dict);
Py_DECREF(dict);
if (result == NULL)
    return NULL; /* Pass error back */
/* Here maybe use the result */
Py_DECREF(result);

1.7. 提取扩展函数的参数

函数 PyArg_ParseTuple() 的声明如下：

int PyArg_ParseTuple(PyObject *arg, const char *format, ...);

参数 arg 必须是一个元组对象，包含从 Python 传递给 C 函数的参数列表。format 参数必须是一个格式字符串，语法请参考 Python C/API 手册中的 解析参数并构建值变量。剩余参数是各个变量的地址，类型要与格式字符串对应。

注意 PyArg_ParseTuple() 会检测他需要的Python参数类型，却无法检测传递给他的C变量地址，如果这里出错了，可能会在内存中随机写入东西，小心。

注意任何由调用者提供的Python对象引用是 借来的 引用；不要递减它们的引用计数！

一些调用的例子：

#define PY_SSIZE_T_CLEAN  /* Make "s#" use Py_ssize_t rather than int. */
#include <Python.h>

int ok;
int i, j;
long k, l;
const char *s;
Py_ssize_t size;

ok = PyArg_ParseTuple(args, ""); /* No arguments */
    /* Python call: f() */

ok = PyArg_ParseTuple(args, "s", &s); /* A string */
    /* Possible Python call: f('whoops!') */

ok = PyArg_ParseTuple(args, "lls", &k, &l, &s); /* Two longs and a string */
    /* Possible Python call: f(1, 2, 'three') */

ok = PyArg_ParseTuple(args, "(ii)s#", &i, &j, &s, &size);
    /* A pair of ints and a string, whose size is also returned */
    /* Possible Python call: f((1, 2), 'three') */

{
    const char *file;
    const char *mode = "r";
    int bufsize = 0;
    ok = PyArg_ParseTuple(args, "s|si", &file, &mode, &bufsize);
    /* A string, and optionally another string and an integer */
    /* Possible Python calls:
       f('spam')
       f('spam', 'w')
       f('spam', 'wb', 100000) */
}

{
    int left, top, right, bottom, h, v;
    ok = PyArg_ParseTuple(args, "((ii)(ii))(ii)",
             &left, &top, &right, &bottom, &h, &v);
    /* A rectangle and a point */
    /* Possible Python call:
       f(((0, 0), (400, 300)), (10, 10)) */
}

{
    Py_complex c;
    ok = PyArg_ParseTuple(args, "D:myfunction", &c);
    /* a complex, also providing a function name for errors */
    /* Possible Python call: myfunction(1+2j) */
}

1.8. 给扩展函数的关键字参数

函数 PyArg_ParseTupleAndKeywords() 声明如下：

int PyArg_ParseTupleAndKeywords(PyObject *arg, PyObject *kwdict,
                                const char *format, char *kwlist[], ...);

The arg and format parameters are identical to those of the PyArg_ParseTuple() function. The kwdict parameter is the dictionary of keywords received as the third parameter from the Python runtime. The kwlist parameter is a NULL-terminated list of strings which identify the parameters; the names are matched with the type information from format from left to right. On success, PyArg_ParseTupleAndKeywords() returns true, otherwise it returns false and raises an appropriate exception.

注解

嵌套的元组在使用关键字参数时无法生效，不在 kwlist 中的关键字参数会导致 TypeError 异常。

如下例子是使用关键字参数的例子模块，作者是 Geoff Philbrick (philbrick@hks.com):

#include "Python.h"

static PyObject *
keywdarg_parrot(PyObject *self, PyObject *args, PyObject *keywds)
{
    int voltage;
    char *state = "a stiff";
    char *action = "voom";
    char *type = "Norwegian Blue";

    static char *kwlist[] = {"voltage", "state", "action", "type", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, keywds, "i|sss", kwlist,
                                     &voltage, &state, &action, &type))
        return NULL;

    printf("-- This parrot wouldn't %s if you put %i Volts through it.\n",
           action, voltage);
    printf("-- Lovely plumage, the %s -- It's %s!\n", type, state);

    Py_RETURN_NONE;
}

static PyMethodDef keywdarg_methods[] = {
    /* The cast of the function is necessary since PyCFunction values
     * only take two PyObject* parameters, and keywdarg_parrot() takes
     * three.
     */
    {"parrot", (PyCFunction)keywdarg_parrot, METH_VARARGS | METH_KEYWORDS,
     "Print a lovely skit to standard output."},
    {NULL, NULL, 0, NULL}   /* sentinel */
};

static struct PyModuleDef keywdargmodule = {
    PyModuleDef_HEAD_INIT,
    "keywdarg",
    NULL,
    -1,
    keywdarg_methods
};

PyMODINIT_FUNC
PyInit_keywdarg(void)
{
    return PyModule_Create(&keywdargmodule);
}

1.9. 构造任意值

这个函数与 PyArg_ParseTuple() 很相似，声明如下：

PyObject *Py_BuildValue(const char *format, ...);

接受一个格式字符串，与 PyArg_ParseTuple() 相同，但是参数必须是原变量的地址指针(输入给函数，而非输出)。最终返回一个Python对象适合于返回C函数调用给Python代码。

一个与 PyArg_ParseTuple() 的不同是，后面可能需要的要求返回一个元组(Python参数里诶包总是在内部描述为元组)，比如用于传递给其他Python函数以参数。 Py_BuildValue() 并不总是生成元组，在多于1个格式字符串时会生成元组，而如果格式字符串为空则返回 None ，一个参数则直接返回该参数的对象。如果要求强制生成一个长度为0的元组，或包含一个元素的元组，需要在格式字符串中加上括号。

例子(左侧是调用，右侧是Python值结果)：

Py_BuildValue("")                        None
Py_BuildValue("i", 123)                  123
Py_BuildValue("iii", 123, 456, 789)      (123, 456, 789)
Py_BuildValue("s", "hello")              'hello'
Py_BuildValue("y", "hello")              b'hello'
Py_BuildValue("ss", "hello", "world")    ('hello', 'world')
Py_BuildValue("s#", "hello", 4)          'hell'
Py_BuildValue("y#", "hello", 4)          b'hell'
Py_BuildValue("()")                      ()
Py_BuildValue("(i)", 123)                (123,)
Py_BuildValue("(ii)", 123, 456)          (123, 456)
Py_BuildValue("(i,i)", 123, 456)         (123, 456)
Py_BuildValue("[i,i]", 123, 456)         [123, 456]
Py_BuildValue("{s:i,s:i}",
              "abc", 123, "def", 456)    {'abc': 123, 'def': 456}
Py_BuildValue("((ii)(ii)) (ii)",
              1, 2, 3, 4, 5, 6)          (((1, 2), (3, 4)), (5, 6))

1.10. 引用计数

在C/C++语言中，程序员负责动态分配和回收堆heap当中的内存。在C里，通过函数 malloc() 和 free() 来完成。在C++里是操作 new 和 delete 来实现相同的功能。

每个由 malloc() 分配的内存块，最终都要由 free() 退回到可用内存池里面去。而调用 free() 的时机非常重要，如果一个内存块忘了 free() 则会导致内存泄漏，这块内存在程序结束前将无法重新使用。这叫做 内存泄漏 。而如果对同一内存块 free() 了以后，另外一个指针再次访问，则再次使用 malloc() 复用这块内存会导致冲突。这叫做 野指针 。等同于使用未初始化的数据，core dump，错误结果，神秘的崩溃等。

内存泄露往往发生在一些并不常见的代码流程上面。比如一个函数申请了内存以后，做了些计算，然后释放内存块。现在一些对函数的修改可能增加对计算的测试并检测错误条件，然后过早的从函数返回了。这很容易忘记在退出前释放内存，特别是后期修改的代码。这种内存泄漏，一旦引入，通常很长时间都难以检测到，错误退出被调用的频度较低，而现代电脑又有非常巨大的虚拟内存，所以泄漏仅在长期运行或频繁调用泄漏函数时才会变得明显。因此，有必要避免内存泄漏，通过代码规范会策略来最小化此类错误。

Python通过 malloc() 和 free() 包含大量的内存分配和释放，同样需要避免内存泄漏和野指针。他选择的方法就是 引用计数 。其原理比较简单：每个对象都包含一个计数器，计数器的增减与对象引用的增减直接相关，当引用计数为0时，表示对象已经没有存在的意义了，对象就可以删除了。

另一个叫法是 自动垃圾回收 。(有时引用计数也被看作是垃圾回收策略，于是这里的”自动”用以区分两者)。自动垃圾回收的优点是用户不需要明确的调用 free() 。(另一个优点是改善速度或内存使用，然而这并不难)。缺点是对C，没有可移植的自动垃圾回收器，而引用计数则可以可移植的实现(只要 malloc() 和 free() 函数是可用的，这也是C标准担保的)。也许以后有一天会出现可移植的自动垃圾回收器，但在此前我们必须与引用计数一起工作。

Python使用传统的引用计数实现，也提供了循环监测器，用以检测引用循环。这使得应用无需担心直接或间接的创建了循环引用，这是引用计数垃圾收集的一个弱点。引用循环是对象(可能直接)的引用了本身，所以循环中的每个对象的引用计数都不是0。典型的引用计数实现无法回收处于引用循环中的对象，或者被循环所引用的对象，哪怕没有循环以外的引用了。

循环探测器可以检测垃圾循环并回收。 gc 模块提供了方法运行探测器 ( collect() 函数) ，而且可以在运行时配置禁用探测器。循环探测器被当作可选组件，默认是包含的，也可以在构建时禁用，在Unix平台(包括Mac OS X)使用 --without-cycle-gc 选项到 configure 脚本。如果循环探测器被禁用， gc 模块就不可用了。

1.10.1. Python中的引用计数

有两个宏 Py_INCREF(x) 和 Py_DECREF(x) ，会处理引用计数的增减。 Py_DECREF() 也会在引用计数到达0时释放对象。为了灵活，并不会直接调用 free() ，而是通过对象的 类型对象 的函数指针来调用。为了这个目的(或其他的)，每个对象同时包含一个指向自身类型对象的指针。

最大的问题依旧：何时使用 Py_INCREF(x) 和 Py_DECREF(x) ？我们首先引入一些概念。没有人”拥有”一个对象，你可以 拥有一个引用 到一个对象。一个对象的引用计数定义为拥有引用的数量。引用的拥有者有责任调用 Py_DECREF() ，在引用不再需要时。引用的拥有关系可以被传递。有三种办法来处置拥有的引用：传递、存储、调用 Py_DECREF() 。忘记处置一个拥有的引用会导致内存泄漏。

还可以 借用 2 一个对象的引用。借用的引用不应该调用 Py_DECREF() 。借用者必须确保不能持有对象超过拥有者借出的时间。在拥有者处置对象后使用借用的引用是有风险的，应该完全避免 3 。

借用相对于引用的优点是你无需担心整条路径上代码的引用，或者说，通过借用你无需担心内存泄漏的风险。借用的缺点是一些看起来正确代码上的借用可能会在拥有者处置后使用对象。

借用可以变为拥有引用，通过调用 Py_INCREF() 。这不会影响已经借出的拥有者的状态。这回创建一个新的拥有引用，并给予完全的拥有者责任(新的拥有者必须恰当的处置引用，就像之前的拥有者那样)。

1.10.2. 拥有规则

当一个对象引用传递进出一个函数时，函数的接口应该指定拥有关系的传递是否包含引用。

大多数函数返回一个对象的引用，并传递引用拥有关系。通常，所有创建对象的函数，例如 PyLong_FromLong() 和 Py_BuildValue() ，会传递拥有关系给接收者。即便是对象不是真正新的，你仍然可以获得对象的新引用。一个实例是 PyLong_FromLong() 维护了一个流行值的缓存，并可以返回已缓存项目的新引用。

很多另一个对象提取对象的函数，也会传递引用关系，例如 PyObject_GetAttrString() 。这里的情况不够清晰，一些不太常用的例程是例外的 PyTuple_GetItem() ， PyList_GetItem() ， PyDict_GetItem() ， PyDict_GetItemString() 都是返回从元组、列表、字典里借用的引用。

函数 PyImport_AddModule() 也会返回借用的引用，哪怕可能会返回创建的对象：这个可能因为一个拥有的引用对象是存储在 sys.modules 里。

当你传递一个对象引用到另一个函数时，通常函数是借用出去的。如果需要存储，就使用 Py_INCREF() 来变成独立的拥有者。这个规则有两个重要的例外： PyTuple_SetItem() 和 PyList_SetItem() 。这些函数接受传递来的引用关系，哪怕会失败！(注意 PyDict_SetItem() 及其同类不会接受引用关系，他们是”正常的”)。

当一个C函数被Python调用时，会从调用方传来的参数借用引用。调用者拥有对象的引用，所以借用的引用生命周期可以保证到函数返回。只要当借用的引用需要存储或传递时，就必须转换为拥有的引用，通过调用 Py_INCREF() 。

Python调用从C函数返回的对象引用时必须是拥有的引用—拥有关系被从函数传递给调用者。

1.10.3. 危险的薄冰

有少数情况下，借用的引用看起来无害，但却可能导致问题。这通常是因为解释器的隐式调用，并可能导致引用拥有者处置这个引用。

首先需要特别注意的情况是使用 Py_DECREF() 到一个无关对象，而这个对象的引用是借用自一个列表的元素。举个实例：

void
bug(PyObject *list)
{
    PyObject *item = PyList_GetItem(list, 0);

    PyList_SetItem(list, 1, PyLong_FromLong(0L));
    PyObject_Print(item, stdout, 0); /* BUG! */
}

这个函数首先借用一个引用 list[0] ，然后替换 list[1] 为值 0 ，最后打印借用的引用。看起来无害是吧，但却不是。

我们跟着控制流进入 PyList_SetItem() 。列表拥有者引用了其所有成员，所以当成员1被替换时，就必须处置原来的成员1。现在假设原来的成员1是用户定义类的实例，且假设这个类定义了 __del__() 方法。如果这个类实例的引用计数是1，那么处置动作就会调用 __del__() 方法。

既然是Python写的， __del__() 方法可以执行任意Python代码。是否可能在 bug() 的 item 废止引用呢，是的。假设列表传递到 bug() 会被 __del__() 方法所访问，就可以执行一个语句来实现 del list[0] ，然后假设这是最后一个对对象的引用，就需要释放内存，从而使得 item 无效化。

解决方法是，当你知道了问题的根源，就容易了：临时增加引用计数。正确版本的函数代码如下：

void
no_bug(PyObject *list)
{
    PyObject *item = PyList_GetItem(list, 0);

    Py_INCREF(item);
    PyList_SetItem(list, 1, PyLong_FromLong(0L));
    PyObject_Print(item, stdout, 0);
    Py_DECREF(item);
}

这是个真实的故事。一个旧版本的Python包含了这个bug的变种，而一些人花费了大量时间在C调试器上去寻找为什么 __del__() 方法会失败。

这个问题的第二种情况是借用的引用涉及线程的变种。通常，Python解释器里多个线程无法进入对方的路径，因为有个全局锁保护着Python整个对象空间。但可以使用宏 Py_BEGIN_ALLOW_THREADS 来临时释放这个锁，重新获取锁用 Py_END_ALLOW_THREADS 。这通常围绕在阻塞I/O调用外，使得其他线程可以在等待I/O期间使用处理器。显然，如下函数会跟之前那个有一样的问题：

void
bug(PyObject *list)
{
    PyObject *item = PyList_GetItem(list, 0);
    Py_BEGIN_ALLOW_THREADS
    ...some blocking I/O call...
    Py_END_ALLOW_THREADS
    PyObject_Print(item, stdout, 0); /* BUG! */
}

1.10.4. NULL指针

In general, functions that take object references as arguments do not expect you to pass them NULL pointers, and will dump core (or cause later core dumps) if you do so. Functions that return object references generally return NULL only to indicate that an exception occurred. The reason for not testing for NULL arguments is that functions often pass the objects they receive on to other function — if each function were to test for NULL, there would be a lot of redundant tests and the code would run more slowly.

It is better to test for NULL only at the “source:” when a pointer that may be NULL is received, for example, from malloc() or from a function that may raise an exception.

The macros Py_INCREF() and Py_DECREF() do not check for NULL pointers — however, their variants Py_XINCREF() and Py_XDECREF() do.

The macros for checking for a particular object type (Pytype_Check()) don’t check for NULL pointers — again, there is much code that calls several of these in a row to test an object against various different expected types, and this would generate redundant tests. There are no variants with NULL checking.

The C function calling mechanism guarantees that the argument list passed to C functions (args in the examples) is never NULL — in fact it guarantees that it is always a tuple 4.

It is a severe error to ever let a NULL pointer “escape” to the Python user.

1.11. 在C++中编写扩展

还可以在C++中编写扩展模块，只是有些限制。如果主程序(Python解释器)是使用C编译器来编译和链接的，全局或静态对象的构造器就不能使用。而如果是C++编译器来链接的就没有这个问题。函数会被Python解释器调用(通常就是模块初始化函数)必须声明为 extern "C" 。而是否在 extern "C" {...} 里包含Python头文件则不是那么重要，因为如果定义了符号 __cplusplus 则已经是这么声明的了(所有现代C++编译器都会定义这个符号)。

1.12. 给扩展模块提供C API

很多扩展模块提供了新的函数和类型供Python使用，但有时扩展模块里的代码也可以被其他扩展模块使用。例如，一个扩展模块可以实现一个类型 “collection” 看起来是没有顺序的。就像是Python列表类型，拥有C API允许扩展模块来创建和维护列表，这个新的集合类型可以有一堆C函数用于给其他扩展模块直接使用。

开始看起来很简单：只需要编写函数(无需声明为 static )，提供恰当的头文件，以及C API的文档。实际上在所有扩展模块都是静态链接到Python解释器时也是可以正常工作的。当模块以共享库链接时，一个模块中的符号定义对另一个模块不可见。可见的细节依赖于操作系统，一些系统的Python解释器使用全局命名空间(例如Windows)，有些则在链接时需要一个严格的已导入符号列表(一个例子是AIX)，或者提供可选的不同策略(如Unix系列)。即便是符号是全局可见的，你要调用的模块也可能尚未加载。

可移植性需要不能对符号可见性做任何假设。这意味着扩展模块里的所有符号都应该声明为 static ，除了模块的初始化函数，来避免与其他扩展模块的命名冲突(在段落 模块方法表和初始化函数 中讨论) 。这意味着符号应该 必须 通过其他导出方式来供其他扩展模块访问。

Python提供了一个特别的机制来传递C级别信息(指针)，从一个扩展模块到另一个：Capsules。一个Capsule是一个Python数据类型，会保存指针( void * )。Capsule只能通过其C API来创建和访问，但可以像其他Python对象一样的传递。通常，我们可以指定一个扩展模块命名空间的名字。其他扩展模块可以导入这个模块，获取这个名字的值，然后从Capsule获取指针。

Capsule可以用多种方式导出C API给扩展模块。每个函数可以用自己的Capsule，或者所有C API指针可以存储在一个数组里，数组地址再发布给Capsule。存储和获取指针也可以用多种方式，供客户端模块使用。

Whichever method you choose, it’s important to name your Capsules properly. The function PyCapsule_New() takes a name parameter (const char *); you’re permitted to pass in a NULL name, but we strongly encourage you to specify a name. Properly named Capsules provide a degree of runtime type-safety; there is no feasible way to tell one unnamed Capsule from another.

通常来说，Capsule用于暴露C API，其名字应该遵循如下规范：

modulename.attributename

便利函数 PyCapsule_Import() 可以方便的载入通过Capsule提供的C API，仅在Capsule的名字匹配时。这个行为为C API用户提供了高度的确定性来载入正确的C API。

如下例子展示了将大部分负担交由导出模块作者的方法，适用于常用的库模块。其会存储所有C API指针(例子里只有一个)在 void 指针的数组里，并使其值变为Capsule。对应的模块头文件提供了宏来管理导入模块和获取C API指针；客户端模块只需要在访问C API前调用这个宏即可。

导出的模块修改自 spam 模块，来自 一个简单的例子 段落。函数 spam.system() 不会直接调用C库函数 system() ，但一个函数 PySpam_System() 会负责调用，当然现实中会更复杂些(例如添加 “spam” 到每个命令)。函数 PySpam_System() 也会导出给其他扩展模块。

函数 PySpam_System() 是个纯C函数，声明 static 就像其他地方那样:

static int
PySpam_System(const char *command)
{
    return system(command);
}

函数 spam_system() 按照如下方式修改:

static PyObject *
spam_system(PyObject *self, PyObject *args)
{
    const char *command;
    int sts;

    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;
    sts = PySpam_System(command);
    return PyLong_FromLong(sts);
}

在模块开头，在此行后:

#include "Python.h"

添加另外两行:

#define SPAM_MODULE
#include "spammodule.h"

#define 用于告知头文件需要包含给导出的模块，而不是客户端模块。最终，模块的初始化函数必须负责初始化C API指针数组:

PyMODINIT_FUNC
PyInit_spam(void)
{
    PyObject *m;
    static void *PySpam_API[PySpam_API_pointers];
    PyObject *c_api_object;

    m = PyModule_Create(&spammodule);
    if (m == NULL)
        return NULL;

    /* Initialize the C API pointer array */
    PySpam_API[PySpam_System_NUM] = (void *)PySpam_System;

    /* Create a Capsule containing the API pointer array's address */
    c_api_object = PyCapsule_New((void *)PySpam_API, "spam._C_API", NULL);

    if (c_api_object != NULL)
        PyModule_AddObject(m, "_C_API", c_api_object);
    return m;
}

注意 PySpam_API 声明为 static ；此外指针数组会在 PyInit_spam() 结束后消失!

头文件 spammodule.h 里的一堆工作，看起来如下所示:

#ifndef Py_SPAMMODULE_H
#define Py_SPAMMODULE_H
#ifdef __cplusplus
extern "C" {
#endif

/* Header file for spammodule */

/* C API functions */
#define PySpam_System_NUM 0
#define PySpam_System_RETURN int
#define PySpam_System_PROTO (const char *command)

/* Total number of C API pointers */
#define PySpam_API_pointers 1

#ifdef SPAM_MODULE
/* This section is used when compiling spammodule.c */

static PySpam_System_RETURN PySpam_System PySpam_System_PROTO;

#else
/* This section is used in modules that use spammodule's API */

static void **PySpam_API;

#define PySpam_System \
 (*(PySpam_System_RETURN (*)PySpam_System_PROTO) PySpam_API[PySpam_System_NUM])

/* Return -1 on error, 0 on success.
 * PyCapsule_Import will set an exception if there's an error.
 */
static int
import_spam(void)
{
    PySpam_API = (void **)PyCapsule_Import("spam._C_API", 0);
    return (PySpam_API != NULL) ? 0 : -1;
}

#endif

#ifdef __cplusplus
}
#endif

#endif /* !defined(Py_SPAMMODULE_H) */

客户端模块必须在其初始化函数里按顺序调用函数 import_spam() (或其他宏)才能访问函数 PySpam_System() 。

PyMODINIT_FUNC
PyInit_client(void)
{
    PyObject *m;

    m = PyModule_Create(&clientmodule);
    if (m == NULL)
        return NULL;
    if (import_spam() < 0)
        return NULL;
    /* additional initialization can happen here */
    return m;
}

这种方法的主要缺点是，文件 spammodule.h 过于复杂。当然，对每个要导出的函数，基本结构是相似的，所以只需要学习一次。

最后需要提醒的是Capsule提供了额外的功能，用于存储在Capsule里的指针的内存分配和释放。细节参考 Python/C API参考手册的章节 胶囊 和Capsule的实现(在Python源码发行包的 Include/pycapsule.h 和 Objects/pycapsule.c )。

备注

1

这个函数的接口已经在标准模块 os 里了，这里作为一个简单而直接的例子。

2

术语”借用”一个引用是不完全正确的：拥有者仍然有引用的拷贝。

3

检查引用计数至少为1 没有用 ，引用计数本身可以在已经释放的内存里，并有可能被其他对象所用。

4

当你使用 “旧式” 风格调用约定时，这些保证不成立，尽管这依旧存在于很多旧代码中。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

扩展和嵌入 Python 解释器 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

扩展和嵌入 Python 解释器 »

|

2. 自定义扩展类型：教程

Python 允许编写 C 扩展模块定义可以从 Python 代码中操纵的新类型，这很像内置的 str 和 list 类型。所有扩展类型的代码都遵循一个模式，但是在您开始之前，您需要了解一些细节。这份文件是对这个主题介绍。

2.1. 基础

CPython 运行时将所有 Python 对象都视为类型 PyObject* 的变量，即所有 Python 对象的”基础类型”。 PyObject 结构体本身包含了对象的 reference count 和对象的”类型对象”。 类型对象确定解释器需要调用哪些 (C) 函数，例如一个属性查询一个对象，一个方法调用，或者与另一个对象相乘。 这些 C 函数被称为“类型方法”。

所以，如果你想要定义新的扩展类型，需要创建新的类型对象。

这类事情只能用例子解释，这里用一个最小化但完整的的模块，定义了新的类型叫做 Custom 在C扩展模块 custom 里。

注解

这里展示的方法是定义 static 扩展类型的传统方法。可以适合大部分用途。C API也可以定义在堆上分配的扩展类型，使用 PyType_FromSpec() 函数，但不在本入门里讨论。

#include <Python.h>

typedef struct {
    PyObject_HEAD
    /* Type-specific fields go here. */
} CustomObject;

static PyTypeObject CustomType = {
    PyVarObject_HEAD_INIT(NULL, 0)
    .tp_name = "custom.Custom",
    .tp_doc = "Custom objects",
    .tp_basicsize = sizeof(CustomObject),
    .tp_itemsize = 0,
    .tp_flags = Py_TPFLAGS_DEFAULT,
    .tp_new = PyType_GenericNew,
};

static PyModuleDef custommodule = {
    PyModuleDef_HEAD_INIT,
    .m_name = "custom",
    .m_doc = "Example module that creates an extension type.",
    .m_size = -1,
};

PyMODINIT_FUNC
PyInit_custom(void)
{
    PyObject *m;
    if (PyType_Ready(&CustomType) < 0)
        return NULL;

    m = PyModule_Create(&custommodule);
    if (m == NULL)
        return NULL;

    Py_INCREF(&CustomType);
    PyModule_AddObject(m, "Custom", (PyObject *) &CustomType);
    return m;
}

这部分很容易理解，这是为了跟上一章能对接上。这个文件定义了三件事：

Custom 类的对象 object 包含了： CustomObject 结构，这会为每个 Custom 实例分配一次。

Custom type 的行为：这是 CustomType 结构体，其定义了一堆标识和函数指针，会指向解释器里请求的操作。

初始化 custom 模块： PyInit_custom 函数和对应的 custommodule 结构体。

结构的第一块是

typedef struct {
    PyObject_HEAD
} CustomObject;

这就是一个自定义对象所包含的。 PyObject_HEAD 是强制要求必须在每个对象结构体之前，用以定义一个类型为 PyObject 的字段叫 ob_base ，包含了一个指针指向类型对象和一个引用计数(这可以用宏 Py_REFCNT 和 Py_TYPE 来区分)。用宏来抽象，使得附加字段可以用调试构建。

注解

注意在宏 PyObject_HEAD 后没有分号。意外添加分号会导致编译器提示出错。

当然，对象除了在 PyObject_HEAD 存储数据外，还有额外数据；例如，如下定义了标准的Python浮点数:

typedef struct {
    PyObject_HEAD
    double ob_fval;
} PyFloatObject;

第二个位是类型对象的定义:

static PyTypeObject CustomType = {
    PyVarObject_HEAD_INIT(NULL, 0)
    .tp_name = "custom.Custom",
    .tp_doc = "Custom objects",
    .tp_basicsize = sizeof(CustomObject),
    .tp_itemsize = 0,
    .tp_new = PyType_GenericNew,
};

注解

推荐使用如上C99风格的初始化，以避免列出所有的 PyTypeObject 字段，其中很多是你不需要关心的，这样也可以避免关注字段的定义顺序。

在 object.h 中实际定义的 PyTypeObject 具有比如上定义更多的 字段。 剩余的字段会由 C 编译器用零来填充，通常的做法是不显式地指定它们，除非你确实需要它们。

我们先挑选一部分，每次一个字段:

PyVarObject_HEAD_INIT(NULL, 0)

这一行是强制的样板，用以初始化如上提到的 ob_base 字段:

.tp_name = "custom.Custom",

我们的类型的名称。 这将出现在我们的对象的默认文本表示形式和某些错误消息中，例如:

>>> "" + custom.Custom()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "custom.Custom") to str

Note that the name is a dotted name that includes both the module name and the name of the type within the module. The module in this case is custom and the type is Custom, so we set the type name to custom.Custom. Using the real dotted import path is important to make your type compatible with the pydoc and pickle modules.

.tp_basicsize = sizeof(CustomObject),
.tp_itemsize = 0,

This is so that Python knows how much memory to allocate when creating new Custom instances. tp_itemsize is only used for variable-sized objects and should otherwise be zero.

注解

If you want your type to be subclassable from Python, and your type has the same tp_basicsize as its base type, you may have problems with multiple inheritance. A Python subclass of your type will have to list your type first in its __bases__, or else it will not be able to call your type’s __new__() method without getting an error. You can avoid this problem by ensuring that your type has a larger value for tp_basicsize than its base type does. Most of the time, this will be true anyway, because either your base type will be object, or else you will be adding data members to your base type, and therefore increasing its size.

We set the class flags to Py_TPFLAGS_DEFAULT.

.tp_flags = Py_TPFLAGS_DEFAULT,

All types should include this constant in their flags. It enables all of the members defined until at least Python 3.3. If you need further members, you will need to OR the corresponding flags.

We provide a doc string for the type in tp_doc.

.tp_doc = "Custom objects",

To enable object creation, we have to provide a tp_new handler. This is the equivalent of the Python method __new__(), but has to be specified explicitly. In this case, we can just use the default implementation provided by the API function PyType_GenericNew().

.tp_new = PyType_GenericNew,

Everything else in the file should be familiar, except for some code in PyInit_custom():

if (PyType_Ready(&CustomType) < 0)
    return;

This initializes the Custom type, filling in a number of members to the appropriate default values, including ob_type that we initially set to NULL.

PyModule_AddObject(m, "Custom", (PyObject *) &CustomType);

This adds the type to the module dictionary. This allows us to create Custom instances by calling the Custom class:

>>> import custom
>>> mycustom = custom.Custom()

That’s it! All that remains is to build it; put the above code in a file called custom.c and:

from distutils.core import setup, Extension
setup(name="custom", version="1.0",
      ext_modules=[Extension("custom", ["custom.c"])])

in a file called setup.py; then typing

$ python setup.py build

at a shell should produce a file custom.so in a subdirectory; move to that directory and fire up Python — you should be able to import custom and play around with Custom objects.

That wasn’t so hard, was it?

Of course, the current Custom type is pretty uninteresting. It has no data and doesn’t do anything. It can’t even be subclassed.

注解

While this documentation showcases the standard distutils module for building C extensions, it is recommended in real-world use cases to use the newer and better-maintained setuptools library. Documentation on how to do this is out of scope for this document and can be found in the Python Packaging User’s Guide [https://packaging.python.org/tutorials/distributing-packages/].

2.2. Adding data and methods to the Basic example

Let’s extend the basic example to add some data and methods. Let’s also make the type usable as a base class. We’ll create a new module, custom2 that adds these capabilities:

#include <Python.h>
#include "structmember.h"

typedef struct {
    PyObject_HEAD
    PyObject *first; /* first name */
    PyObject *last;  /* last name */
    int number;
} CustomObject;

static void
Custom_dealloc(CustomObject *self)
{
    Py_XDECREF(self->first);
    Py_XDECREF(self->last);
    Py_TYPE(self)->tp_free((PyObject *) self);
}

static PyObject *
Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    CustomObject *self;
    self = (CustomObject *) type->tp_alloc(type, 0);
    if (self != NULL) {
        self->first = PyUnicode_FromString("");
        if (self->first == NULL) {
            Py_DECREF(self);
            return NULL;
        }
        self->last = PyUnicode_FromString("");
        if (self->last == NULL) {
            Py_DECREF(self);
            return NULL;
        }
        self->number = 0;
    }
    return (PyObject *) self;
}

static int
Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
{
    static char *kwlist[] = {"first", "last", "number", NULL};
    PyObject *first = NULL, *last = NULL, *tmp;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist,
                                     &first, &last,
                                     &self->number))
        return -1;

    if (first) {
        tmp = self->first;
        Py_INCREF(first);
        self->first = first;
        Py_XDECREF(tmp);
    }
    if (last) {
        tmp = self->last;
        Py_INCREF(last);
        self->last = last;
        Py_XDECREF(tmp);
    }
    return 0;
}

static PyMemberDef Custom_members[] = {
    {"first", T_OBJECT_EX, offsetof(CustomObject, first), 0,
     "first name"},
    {"last", T_OBJECT_EX, offsetof(CustomObject, last), 0,
     "last name"},
    {"number", T_INT, offsetof(CustomObject, number), 0,
     "custom number"},
    {NULL}  /* Sentinel */
};

static PyObject *
Custom_name(CustomObject *self, PyObject *Py_UNUSED(ignored))
{
    if (self->first == NULL) {
        PyErr_SetString(PyExc_AttributeError, "first");
        return NULL;
    }
    if (self->last == NULL) {
        PyErr_SetString(PyExc_AttributeError, "last");
        return NULL;
    }
    return PyUnicode_FromFormat("%S %S", self->first, self->last);
}

static PyMethodDef Custom_methods[] = {
    {"name", (PyCFunction) Custom_name, METH_NOARGS,
     "Return the name, combining the first and last name"
    },
    {NULL}  /* Sentinel */
};

static PyTypeObject CustomType = {
    PyVarObject_HEAD_INIT(NULL, 0)
    .tp_name = "custom2.Custom",
    .tp_doc = "Custom objects",
    .tp_basicsize = sizeof(CustomObject),
    .tp_itemsize = 0,
    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
    .tp_new = Custom_new,
    .tp_init = (initproc) Custom_init,
    .tp_dealloc = (destructor) Custom_dealloc,
    .tp_members = Custom_members,
    .tp_methods = Custom_methods,
};

static PyModuleDef custommodule = {
    PyModuleDef_HEAD_INIT,
    .m_name = "custom2",
    .m_doc = "Example module that creates an extension type.",
    .m_size = -1,
};

PyMODINIT_FUNC
PyInit_custom2(void)
{
    PyObject *m;
    if (PyType_Ready(&CustomType) < 0)
        return NULL;

    m = PyModule_Create(&custommodule);
    if (m == NULL)
        return NULL;

    Py_INCREF(&CustomType);
    PyModule_AddObject(m, "Custom", (PyObject *) &CustomType);
    return m;
}

This version of the module has a number of changes.

We’ve added an extra include:

#include <structmember.h>

This include provides declarations that we use to handle attributes, as described a bit later.

The Custom type now has three data attributes in its C struct, first, last, and number. The first and last variables are Python strings containing first and last names. The number attribute is a C integer.

The object structure is updated accordingly:

typedef struct {
    PyObject_HEAD
    PyObject *first; /* first name */
    PyObject *last;  /* last name */
    int number;
} CustomObject;

Because we now have data to manage, we have to be more careful about object allocation and deallocation. At a minimum, we need a deallocation method:

static void
Custom_dealloc(CustomObject *self)
{
    Py_XDECREF(self->first);
    Py_XDECREF(self->last);
    Py_TYPE(self)->tp_free((PyObject *) self);
}

which is assigned to the tp_dealloc member:

.tp_dealloc = (destructor) Custom_dealloc,

This method first clears the reference counts of the two Python attributes. Py_XDECREF() correctly handles the case where its argument is NULL (which might happen here if tp_new failed midway). It then calls the tp_free member of the object’s type (computed by Py_TYPE(self)) to free the object’s memory. Note that the object’s type might not be CustomType, because the object may be an instance of a subclass.

注解

The explicit cast to destructor above is needed because we defined Custom_dealloc to take a CustomObject * argument, but the tp_dealloc function pointer expects to receive a PyObject * argument. Otherwise, the compiler will emit a warning. This is object-oriented polymorphism, in C!

We want to make sure that the first and last names are initialized to empty strings, so we provide a tp_new implementation:

static PyObject *
Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    CustomObject *self;
    self = (CustomObject *) type->tp_alloc(type, 0);
    if (self != NULL) {
        self->first = PyUnicode_FromString("");
        if (self->first == NULL) {
            Py_DECREF(self);
            return NULL;
        }
        self->last = PyUnicode_FromString("");
        if (self->last == NULL) {
            Py_DECREF(self);
            return NULL;
        }
        self->number = 0;
    }
    return (PyObject *) self;
}

and install it in the tp_new member:

.tp_new = Custom_new,

The tp_new handler is responsible for creating (as opposed to initializing) objects of the type. It is exposed in Python as the __new__() method. It is not required to define a tp_new member, and indeed many extension types will simply reuse PyType_GenericNew() as done in the first version of the Custom type above. In this case, we use the tp_new handler to initialize the first and last attributes to non-NULL default values.

tp_new is passed the type being instantiated (not necessarily CustomType, if a subclass is instantiated) and any arguments passed when the type was called, and is expected to return the instance created. tp_new handlers always accept positional and keyword arguments, but they often ignore the arguments, leaving the argument handling to initializer (a.k.a. tp_init in C or __init__ in Python) methods.

注解

tp_new shouldn’t call tp_init explicitly, as the interpreter will do it itself.

The tp_new implementation calls the tp_alloc slot to allocate memory:

self = (CustomObject *) type->tp_alloc(type, 0);

Since memory allocation may fail, we must check the tp_alloc result against NULL before proceeding.

注解

We didn’t fill the tp_alloc slot ourselves. Rather PyType_Ready() fills it for us by inheriting it from our base class, which is object by default. Most types use the default allocation strategy.

注解

If you are creating a co-operative tp_new (one that calls a base type’s tp_new or __new__()), you must not try to determine what method to call using method resolution order at runtime. Always statically determine what type you are going to call, and call its tp_new directly, or via type->tp_base->tp_new. If you do not do this, Python subclasses of your type that also inherit from other Python-defined classes may not work correctly. (Specifically, you may not be able to create instances of such subclasses without getting a TypeError.)

We also define an initialization function which accepts arguments to provide initial values for our instance:

static int
Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
{
    static char *kwlist[] = {"first", "last", "number", NULL};
    PyObject *first = NULL, *last = NULL, *tmp;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi", kwlist,
                                     &first, &last,
                                     &self->number))
        return -1;

    if (first) {
        tmp = self->first;
        Py_INCREF(first);
        self->first = first;
        Py_XDECREF(tmp);
    }
    if (last) {
        tmp = self->last;
        Py_INCREF(last);
        self->last = last;
        Py_XDECREF(tmp);
    }
    return 0;
}

by filling the tp_init slot.

.tp_init = (initproc) Custom_init,

The tp_init slot is exposed in Python as the __init__() method. It is used to initialize an object after it’s created. Initializers always accept positional and keyword arguments, and they should return either 0 on success or -1 on error.

Unlike the tp_new handler, there is no guarantee that tp_init is called at all (for example, the pickle module by default doesn’t call __init__() on unpickled instances). It can also be called multiple times. Anyone can call the __init__() method on our objects. For this reason, we have to be extra careful when assigning the new attribute values. We might be tempted, for example to assign the first member like this:

if (first) {
    Py_XDECREF(self->first);
    Py_INCREF(first);
    self->first = first;
}

But this would be risky. Our type doesn’t restrict the type of the first member, so it could be any kind of object. It could have a destructor that causes code to be executed that tries to access the first member; or that destructor could release the Global interpreter Lock and let arbitrary code run in other threads that accesses and modifies our object.

To be paranoid and protect ourselves against this possibility, we almost always reassign members before decrementing their reference counts. When don’t we have to do this?

when we absolutely know that the reference count is greater than 1;

when we know that deallocation of the object 1 will neither release the GIL nor cause any calls back into our type’s code;

when decrementing a reference count in a tp_dealloc handler on a type which doesn’t support cyclic garbage collection 2.

We want to expose our instance variables as attributes. There are a number of ways to do that. The simplest way is to define member definitions:

static PyMemberDef Custom_members[] = {
    {"first", T_OBJECT_EX, offsetof(CustomObject, first), 0,
     "first name"},
    {"last", T_OBJECT_EX, offsetof(CustomObject, last), 0,
     "last name"},
    {"number", T_INT, offsetof(CustomObject, number), 0,
     "custom number"},
    {NULL}  /* Sentinel */
};

and put the definitions in the tp_members slot:

.tp_members = Custom_members,

Each member definition has a member name, type, offset, access flags and documentation string. See the 泛型属性管理 section below for details.

A disadvantage of this approach is that it doesn’t provide a way to restrict the types of objects that can be assigned to the Python attributes. We expect the first and last names to be strings, but any Python objects can be assigned. Further, the attributes can be deleted, setting the C pointers to NULL. Even though we can make sure the members are initialized to non-NULL values, the members can be set to NULL if the attributes are deleted.

We define a single method, Custom.name(), that outputs the objects name as the concatenation of the first and last names.

static PyObject *
Custom_name(CustomObject *self)
{
    if (self->first == NULL) {
        PyErr_SetString(PyExc_AttributeError, "first");
        return NULL;
    }
    if (self->last == NULL) {
        PyErr_SetString(PyExc_AttributeError, "last");
        return NULL;
    }
    return PyUnicode_FromFormat("%S %S", self->first, self->last);
}

The method is implemented as a C function that takes a Custom (or Custom subclass) instance as the first argument. Methods always take an instance as the first argument. Methods often take positional and keyword arguments as well, but in this case we don’t take any and don’t need to accept a positional argument tuple or keyword argument dictionary. This method is equivalent to the Python method:

def name(self):
    return "%s %s" % (self.first, self.last)

Note that we have to check for the possibility that our first and last members are NULL. This is because they can be deleted, in which case they are set to NULL. It would be better to prevent deletion of these attributes and to restrict the attribute values to be strings. We’ll see how to do that in the next section.

Now that we’ve defined the method, we need to create an array of method definitions:

static PyMethodDef Custom_methods[] = {
    {"name", (PyCFunction) Custom_name, METH_NOARGS,
     "Return the name, combining the first and last name"
    },
    {NULL}  /* Sentinel */
};

(note that we used the METH_NOARGS flag to indicate that the method is expecting no arguments other than self)

and assign it to the tp_methods slot:

.tp_methods = Custom_methods,

Finally, we’ll make our type usable as a base class for subclassing. We’ve written our methods carefully so far so that they don’t make any assumptions about the type of the object being created or used, so all we need to do is to add the Py_TPFLAGS_BASETYPE to our class flag definition:

.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,

We rename PyInit_custom() to PyInit_custom2(), update the module name in the PyModuleDef struct, and update the full class name in the PyTypeObject struct.

Finally, we update our setup.py file to build the new module:

from distutils.core import setup, Extension
setup(name="custom", version="1.0",
      ext_modules=[
         Extension("custom", ["custom.c"]),
         Extension("custom2", ["custom2.c"]),
         ])

2.3. Providing finer control over data attributes

In this section, we’ll provide finer control over how the first and last attributes are set in the Custom example. In the previous version of our module, the instance variables first and last could be set to non-string values or even deleted. We want to make sure that these attributes always contain strings.

#include <Python.h>
#include "structmember.h"

typedef struct {
    PyObject_HEAD
    PyObject *first; /* first name */
    PyObject *last;  /* last name */
    int number;
} CustomObject;

static void
Custom_dealloc(CustomObject *self)
{
    Py_XDECREF(self->first);
    Py_XDECREF(self->last);
    Py_TYPE(self)->tp_free((PyObject *) self);
}

static PyObject *
Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    CustomObject *self;
    self = (CustomObject *) type->tp_alloc(type, 0);
    if (self != NULL) {
        self->first = PyUnicode_FromString("");
        if (self->first == NULL) {
            Py_DECREF(self);
            return NULL;
        }
        self->last = PyUnicode_FromString("");
        if (self->last == NULL) {
            Py_DECREF(self);
            return NULL;
        }
        self->number = 0;
    }
    return (PyObject *) self;
}

static int
Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
{
    static char *kwlist[] = {"first", "last", "number", NULL};
    PyObject *first = NULL, *last = NULL, *tmp;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|UUi", kwlist,
                                     &first, &last,
                                     &self->number))
        return -1;

    if (first) {
        tmp = self->first;
        Py_INCREF(first);
        self->first = first;
        Py_DECREF(tmp);
    }
    if (last) {
        tmp = self->last;
        Py_INCREF(last);
        self->last = last;
        Py_DECREF(tmp);
    }
    return 0;
}

static PyMemberDef Custom_members[] = {
    {"number", T_INT, offsetof(CustomObject, number), 0,
     "custom number"},
    {NULL}  /* Sentinel */
};

static PyObject *
Custom_getfirst(CustomObject *self, void *closure)
{
    Py_INCREF(self->first);
    return self->first;
}

static int
Custom_setfirst(CustomObject *self, PyObject *value, void *closure)
{
    PyObject *tmp;
    if (value == NULL) {
        PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute");
        return -1;
    }
    if (!PyUnicode_Check(value)) {
        PyErr_SetString(PyExc_TypeError,
                        "The first attribute value must be a string");
        return -1;
    }
    tmp = self->first;
    Py_INCREF(value);
    self->first = value;
    Py_DECREF(tmp);
    return 0;
}

static PyObject *
Custom_getlast(CustomObject *self, void *closure)
{
    Py_INCREF(self->last);
    return self->last;
}

static int
Custom_setlast(CustomObject *self, PyObject *value, void *closure)
{
    PyObject *tmp;
    if (value == NULL) {
        PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute");
        return -1;
    }
    if (!PyUnicode_Check(value)) {
        PyErr_SetString(PyExc_TypeError,
                        "The last attribute value must be a string");
        return -1;
    }
    tmp = self->last;
    Py_INCREF(value);
    self->last = value;
    Py_DECREF(tmp);
    return 0;
}

static PyGetSetDef Custom_getsetters[] = {
    {"first", (getter) Custom_getfirst, (setter) Custom_setfirst,
     "first name", NULL},
    {"last", (getter) Custom_getlast, (setter) Custom_setlast,
     "last name", NULL},
    {NULL}  /* Sentinel */
};

static PyObject *
Custom_name(CustomObject *self, PyObject *Py_UNUSED(ignored))
{
    return PyUnicode_FromFormat("%S %S", self->first, self->last);
}

static PyMethodDef Custom_methods[] = {
    {"name", (PyCFunction) Custom_name, METH_NOARGS,
     "Return the name, combining the first and last name"
    },
    {NULL}  /* Sentinel */
};

static PyTypeObject CustomType = {
    PyVarObject_HEAD_INIT(NULL, 0)
    .tp_name = "custom3.Custom",
    .tp_doc = "Custom objects",
    .tp_basicsize = sizeof(CustomObject),
    .tp_itemsize = 0,
    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
    .tp_new = Custom_new,
    .tp_init = (initproc) Custom_init,
    .tp_dealloc = (destructor) Custom_dealloc,
    .tp_members = Custom_members,
    .tp_methods = Custom_methods,
    .tp_getset = Custom_getsetters,
};

static PyModuleDef custommodule = {
    PyModuleDef_HEAD_INIT,
    .m_name = "custom3",
    .m_doc = "Example module that creates an extension type.",
    .m_size = -1,
};

PyMODINIT_FUNC
PyInit_custom3(void)
{
    PyObject *m;
    if (PyType_Ready(&CustomType) < 0)
        return NULL;

    m = PyModule_Create(&custommodule);
    if (m == NULL)
        return NULL;

    Py_INCREF(&CustomType);
    PyModule_AddObject(m, "Custom", (PyObject *) &CustomType);
    return m;
}

To provide greater control, over the first and last attributes, we’ll use custom getter and setter functions. Here are the functions for getting and setting the first attribute:

static PyObject *
Custom_getfirst(CustomObject *self, void *closure)
{
    Py_INCREF(self->first);
    return self->first;
}

static int
Custom_setfirst(CustomObject *self, PyObject *value, void *closure)
{
    PyObject *tmp;
    if (value == NULL) {
        PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute");
        return -1;
    }
    if (!PyUnicode_Check(value)) {
        PyErr_SetString(PyExc_TypeError,
                        "The first attribute value must be a string");
        return -1;
    }
    tmp = self->first;
    Py_INCREF(value);
    self->first = value;
    Py_DECREF(tmp);
    return 0;
}

The getter function is passed a Custom object and a “closure”, which is a void pointer. In this case, the closure is ignored. (The closure supports an advanced usage in which definition data is passed to the getter and setter. This could, for example, be used to allow a single set of getter and setter functions that decide the attribute to get or set based on data in the closure.)

The setter function is passed the Custom object, the new value, and the closure. The new value may be NULL, in which case the attribute is being deleted. In our setter, we raise an error if the attribute is deleted or if its new value is not a string.

We create an array of PyGetSetDef structures:

static PyGetSetDef Custom_getsetters[] = {
    {"first", (getter) Custom_getfirst, (setter) Custom_setfirst,
     "first name", NULL},
    {"last", (getter) Custom_getlast, (setter) Custom_setlast,
     "last name", NULL},
    {NULL}  /* Sentinel */
};

and register it in the tp_getset slot:

.tp_getset = Custom_getsetters,

The last item in a PyGetSetDef structure is the “closure” mentioned above. In this case, we aren’t using a closure, so we just pass NULL.

We also remove the member definitions for these attributes:

static PyMemberDef Custom_members[] = {
    {"number", T_INT, offsetof(CustomObject, number), 0,
     "custom number"},
    {NULL}  /* Sentinel */
};

We also need to update the tp_init handler to only allow strings 3 to be passed:

static int
Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
{
    static char *kwlist[] = {"first", "last", "number", NULL};
    PyObject *first = NULL, *last = NULL, *tmp;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|UUi", kwlist,
                                     &first, &last,
                                     &self->number))
        return -1;

    if (first) {
        tmp = self->first;
        Py_INCREF(first);
        self->first = first;
        Py_DECREF(tmp);
    }
    if (last) {
        tmp = self->last;
        Py_INCREF(last);
        self->last = last;
        Py_DECREF(tmp);
    }
    return 0;
}

With these changes, we can assure that the first and last members are never NULL so we can remove checks for NULL values in almost all cases. This means that most of the Py_XDECREF() calls can be converted to Py_DECREF() calls. The only place we can’t change these calls is in the tp_dealloc implementation, where there is the possibility that the initialization of these members failed in tp_new.

We also rename the module initialization function and module name in the initialization function, as we did before, and we add an extra definition to the setup.py file.

2.4. Supporting cyclic garbage collection

Python has a cyclic garbage collector (GC) that can identify unneeded objects even when their reference counts are not zero. This can happen when objects are involved in cycles. For example, consider:

>>> l = []
>>> l.append(l)
>>> del l

In this example, we create a list that contains itself. When we delete it, it still has a reference from itself. Its reference count doesn’t drop to zero. Fortunately, Python’s cyclic garbage collector will eventually figure out that the list is garbage and free it.

In the second version of the Custom example, we allowed any kind of object to be stored in the first or last attributes 4. Besides, in the second and third versions, we allowed subclassing Custom, and subclasses may add arbitrary attributes. For any of those two reasons, Custom objects can participate in cycles:

>>> import custom3
>>> class Derived(custom3.Custom): pass
...
>>> n = Derived()
>>> n.some_attribute = n

To allow a Custom instance participating in a reference cycle to be properly detected and collected by the cyclic GC, our Custom type needs to fill two additional slots and to enable a flag that enables these slots:

#include <Python.h>
#include "structmember.h"

typedef struct {
    PyObject_HEAD
    PyObject *first; /* first name */
    PyObject *last;  /* last name */
    int number;
} CustomObject;

static int
Custom_traverse(CustomObject *self, visitproc visit, void *arg)
{
    Py_VISIT(self->first);
    Py_VISIT(self->last);
    return 0;
}

static int
Custom_clear(CustomObject *self)
{
    Py_CLEAR(self->first);
    Py_CLEAR(self->last);
    return 0;
}

static void
Custom_dealloc(CustomObject *self)
{
    PyObject_GC_UnTrack(self);
    Custom_clear(self);
    Py_TYPE(self)->tp_free((PyObject *) self);
}

static PyObject *
Custom_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    CustomObject *self;
    self = (CustomObject *) type->tp_alloc(type, 0);
    if (self != NULL) {
        self->first = PyUnicode_FromString("");
        if (self->first == NULL) {
            Py_DECREF(self);
            return NULL;
        }
        self->last = PyUnicode_FromString("");
        if (self->last == NULL) {
            Py_DECREF(self);
            return NULL;
        }
        self->number = 0;
    }
    return (PyObject *) self;
}

static int
Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
{
    static char *kwlist[] = {"first", "last", "number", NULL};
    PyObject *first = NULL, *last = NULL, *tmp;

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|UUi", kwlist,
                                     &first, &last,
                                     &self->number))
        return -1;

    if (first) {
        tmp = self->first;
        Py_INCREF(first);
        self->first = first;
        Py_DECREF(tmp);
    }
    if (last) {
        tmp = self->last;
        Py_INCREF(last);
        self->last = last;
        Py_DECREF(tmp);
    }
    return 0;
}

static PyMemberDef Custom_members[] = {
    {"number", T_INT, offsetof(CustomObject, number), 0,
     "custom number"},
    {NULL}  /* Sentinel */
};

static PyObject *
Custom_getfirst(CustomObject *self, void *closure)
{
    Py_INCREF(self->first);
    return self->first;
}

static int
Custom_setfirst(CustomObject *self, PyObject *value, void *closure)
{
    if (value == NULL) {
        PyErr_SetString(PyExc_TypeError, "Cannot delete the first attribute");
        return -1;
    }
    if (!PyUnicode_Check(value)) {
        PyErr_SetString(PyExc_TypeError,
                        "The first attribute value must be a string");
        return -1;
    }
    Py_INCREF(value);
    Py_CLEAR(self->first);
    self->first = value;
    return 0;
}

static PyObject *
Custom_getlast(CustomObject *self, void *closure)
{
    Py_INCREF(self->last);
    return self->last;
}

static int
Custom_setlast(CustomObject *self, PyObject *value, void *closure)
{
    if (value == NULL) {
        PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute");
        return -1;
    }
    if (!PyUnicode_Check(value)) {
        PyErr_SetString(PyExc_TypeError,
                        "The last attribute value must be a string");
        return -1;
    }
    Py_INCREF(value);
    Py_CLEAR(self->last);
    self->last = value;
    return 0;
}

static PyGetSetDef Custom_getsetters[] = {
    {"first", (getter) Custom_getfirst, (setter) Custom_setfirst,
     "first name", NULL},
    {"last", (getter) Custom_getlast, (setter) Custom_setlast,
     "last name", NULL},
    {NULL}  /* Sentinel */
};

static PyObject *
Custom_name(CustomObject *self, PyObject *Py_UNUSED(ignored))
{
    return PyUnicode_FromFormat("%S %S", self->first, self->last);
}

static PyMethodDef Custom_methods[] = {
    {"name", (PyCFunction) Custom_name, METH_NOARGS,
     "Return the name, combining the first and last name"
    },
    {NULL}  /* Sentinel */
};

static PyTypeObject CustomType = {
    PyVarObject_HEAD_INIT(NULL, 0)
    .tp_name = "custom4.Custom",
    .tp_doc = "Custom objects",
    .tp_basicsize = sizeof(CustomObject),
    .tp_itemsize = 0,
    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
    .tp_new = Custom_new,
    .tp_init = (initproc) Custom_init,
    .tp_dealloc = (destructor) Custom_dealloc,
    .tp_traverse = (traverseproc) Custom_traverse,
    .tp_clear = (inquiry) Custom_clear,
    .tp_members = Custom_members,
    .tp_methods = Custom_methods,
    .tp_getset = Custom_getsetters,
};

static PyModuleDef custommodule = {
    PyModuleDef_HEAD_INIT,
    .m_name = "custom4",
    .m_doc = "Example module that creates an extension type.",
    .m_size = -1,
};

PyMODINIT_FUNC
PyInit_custom4(void)
{
    PyObject *m;
    if (PyType_Ready(&CustomType) < 0)
        return NULL;

    m = PyModule_Create(&custommodule);
    if (m == NULL)
        return NULL;

    Py_INCREF(&CustomType);
    PyModule_AddObject(m, "Custom", (PyObject *) &CustomType);
    return m;
}

First, the traversal method lets the cyclic GC know about subobjects that could participate in cycles:

static int
Custom_traverse(CustomObject *self, visitproc visit, void *arg)
{
    int vret;
    if (self->first) {
        vret = visit(self->first, arg);
        if (vret != 0)
            return vret;
    }
    if (self->last) {
        vret = visit(self->last, arg);
        if (vret != 0)
            return vret;
    }
    return 0;
}

For each subobject that can participate in cycles, we need to call the visit() function, which is passed to the traversal method. The visit() function takes as arguments the subobject and the extra argument arg passed to the traversal method. It returns an integer value that must be returned if it is non-zero.

Python provides a Py_VISIT() macro that automates calling visit functions. With Py_VISIT(), we can minimize the amount of boilerplate in Custom_traverse:

static int
Custom_traverse(CustomObject *self, visitproc visit, void *arg)
{
    Py_VISIT(self->first);
    Py_VISIT(self->last);
    return 0;
}

注解

The tp_traverse implementation must name its arguments exactly visit and arg in order to use Py_VISIT().

Second, we need to provide a method for clearing any subobjects that can participate in cycles:

static int
Custom_clear(CustomObject *self)
{
    Py_CLEAR(self->first);
    Py_CLEAR(self->last);
    return 0;
}

Notice the use of the Py_CLEAR() macro. It is the recommended and safe way to clear data attributes of arbitrary types while decrementing their reference counts. If you were to call Py_XDECREF() instead on the attribute before setting it to NULL, there is a possibility that the attribute’s destructor would call back into code that reads the attribute again (especially if there is a reference cycle).

注解

You could emulate Py_CLEAR() by writing:

PyObject *tmp;
tmp = self->first;
self->first = NULL;
Py_XDECREF(tmp);

Nevertheless, it is much easier and less error-prone to always use Py_CLEAR() when deleting an attribute. Don’t try to micro-optimize at the expense of robustness!

The deallocator Custom_dealloc may call arbitrary code when clearing attributes. It means the circular GC can be triggered inside the function. Since the GC assumes reference count is not zero, we need to untrack the object from the GC by calling PyObject_GC_UnTrack() before clearing members. Here is our reimplemented deallocator using PyObject_GC_UnTrack() and Custom_clear:

static void
Custom_dealloc(CustomObject *self)
{
    PyObject_GC_UnTrack(self);
    Custom_clear(self);
    Py_TYPE(self)->tp_free((PyObject *) self);
}

Finally, we add the Py_TPFLAGS_HAVE_GC flag to the class flags:

.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,

That’s pretty much it. If we had written custom tp_alloc or tp_free handlers, we’d need to modify them for cyclic garbage collection. Most extensions will use the versions automatically provided.

2.5. Subclassing other types

It is possible to create new extension types that are derived from existing types. It is easiest to inherit from the built in types, since an extension can easily use the PyTypeObject it needs. It can be difficult to share these PyTypeObject structures between extension modules.

In this example we will create a SubList type that inherits from the built-in list type. The new type will be completely compatible with regular lists, but will have an additional increment() method that increases an internal counter:

>>> import sublist
>>> s = sublist.SubList(range(3))
>>> s.extend(s)
>>> print(len(s))
6
>>> print(s.increment())
1
>>> print(s.increment())
2

#include <Python.h>

typedef struct {
    PyListObject list;
    int state;
} SubListObject;

static PyObject *
SubList_increment(SubListObject *self, PyObject *unused)
{
    self->state++;
    return PyLong_FromLong(self->state);
}

static PyMethodDef SubList_methods[] = {
    {"increment", (PyCFunction) SubList_increment, METH_NOARGS,
     PyDoc_STR("increment state counter")},
    {NULL},
};

static int
SubList_init(SubListObject *self, PyObject *args, PyObject *kwds)
{
    if (PyList_Type.tp_init((PyObject *) self, args, kwds) < 0)
        return -1;
    self->state = 0;
    return 0;
}

static PyTypeObject SubListType = {
    PyVarObject_HEAD_INIT(NULL, 0)
    .tp_name = "sublist.SubList",
    .tp_doc = "SubList objects",
    .tp_basicsize = sizeof(SubListObject),
    .tp_itemsize = 0,
    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
    .tp_init = (initproc) SubList_init,
    .tp_methods = SubList_methods,
};

static PyModuleDef sublistmodule = {
    PyModuleDef_HEAD_INIT,
    .m_name = "sublist",
    .m_doc = "Example module that creates an extension type.",
    .m_size = -1,
};

PyMODINIT_FUNC
PyInit_sublist(void)
{
    PyObject *m;
    SubListType.tp_base = &PyList_Type;
    if (PyType_Ready(&SubListType) < 0)
        return NULL;

    m = PyModule_Create(&sublistmodule);
    if (m == NULL)
        return NULL;

    Py_INCREF(&SubListType);
    PyModule_AddObject(m, "SubList", (PyObject *) &SubListType);
    return m;
}

As you can see, the source code closely resembles the Custom examples in previous sections. We will break down the main differences between them.

typedef struct {
    PyListObject list;
    int state;
} SubListObject;

The primary difference for derived type objects is that the base type’s object structure must be the first value. The base type will already include the PyObject_HEAD() at the beginning of its structure.

When a Python object is a SubList instance, its PyObject * pointer can be safely cast to both PyListObject * and SubListObject *:

static int
SubList_init(SubListObject *self, PyObject *args, PyObject *kwds)
{
    if (PyList_Type.tp_init((PyObject *) self, args, kwds) < 0)
        return -1;
    self->state = 0;
    return 0;
}

We see above how to call through to the __init__ method of the base type.

This pattern is important when writing a type with custom tp_new and tp_dealloc members. The tp_new handler should not actually create the memory for the object with its tp_alloc, but let the base class handle it by calling its own tp_new.

The PyTypeObject struct supports a tp_base specifying the type’s concrete base class. Due to cross-platform compiler issues, you can’t fill that field directly with a reference to PyList_Type; it should be done later in the module initialization function:

PyMODINIT_FUNC
PyInit_sublist(void)
{
    PyObject* m;
    SubListType.tp_base = &PyList_Type;
    if (PyType_Ready(&SubListType) < 0)
        return NULL;

    m = PyModule_Create(&sublistmodule);
    if (m == NULL)
        return NULL;

    Py_INCREF(&SubListType);
    PyModule_AddObject(m, "SubList", (PyObject *) &SubListType);
    return m;
}

Before calling PyType_Ready(), the type structure must have the tp_base slot filled in. When we are deriving an existing type, it is not necessary to fill out the tp_alloc slot with PyType_GenericNew() – the allocation function from the base type will be inherited.

After that, calling PyType_Ready() and adding the type object to the module is the same as with the basic Custom examples.

备注

1

This is true when we know that the object is a basic type, like a string or a float.

2

We relied on this in the tp_dealloc handler in this example, because our type doesn’t support garbage collection.

3

We now know that the first and last members are strings, so perhaps we could be less careful about decrementing their reference counts, however, we accept instances of string subclasses. Even though deallocating normal strings won’t call back into our objects, we can’t guarantee that deallocating an instance of a string subclass won’t call back into our objects.

4

Also, even with our attributes restricted to strings instances, the user could pass arbitrary str subclasses and therefore still create reference cycles.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

扩展和嵌入 Python 解释器 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

扩展和嵌入 Python 解释器 »

|

3. 定义扩展类型：已分类主题

本章节目标是提供一个各种你可以实现的类型方法及其功能的简短介绍。

这是C类型 PyTypeObject 的定义，省略了只用于调试构建的字段:

typedef struct _typeobject {
    PyObject_VAR_HEAD
    const char *tp_name; /* For printing, in format "<module>.<name>" */
    Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */

    /* Methods to implement standard operations */

    destructor tp_dealloc;
    printfunc tp_print;
    getattrfunc tp_getattr;
    setattrfunc tp_setattr;
    PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2)
                                    or tp_reserved (Python 3) */
    reprfunc tp_repr;

    /* Method suites for standard classes */

    PyNumberMethods *tp_as_number;
    PySequenceMethods *tp_as_sequence;
    PyMappingMethods *tp_as_mapping;

    /* More standard operations (here for binary compatibility) */

    hashfunc tp_hash;
    ternaryfunc tp_call;
    reprfunc tp_str;
    getattrofunc tp_getattro;
    setattrofunc tp_setattro;

    /* Functions to access object as input/output buffer */
    PyBufferProcs *tp_as_buffer;

    /* Flags to define presence of optional/expanded features */
    unsigned long tp_flags;

    const char *tp_doc; /* Documentation string */

    /* call function for all accessible objects */
    traverseproc tp_traverse;

    /* delete references to contained objects */
    inquiry tp_clear;

    /* rich comparisons */
    richcmpfunc tp_richcompare;

    /* weak reference enabler */
    Py_ssize_t tp_weaklistoffset;

    /* Iterators */
    getiterfunc tp_iter;
    iternextfunc tp_iternext;

    /* Attribute descriptor and subclassing stuff */
    struct PyMethodDef *tp_methods;
    struct PyMemberDef *tp_members;
    struct PyGetSetDef *tp_getset;
    struct _typeobject *tp_base;
    PyObject *tp_dict;
    descrgetfunc tp_descr_get;
    descrsetfunc tp_descr_set;
    Py_ssize_t tp_dictoffset;
    initproc tp_init;
    allocfunc tp_alloc;
    newfunc tp_new;
    freefunc tp_free; /* Low-level free-memory routine */
    inquiry tp_is_gc; /* For PyObject_IS_GC */
    PyObject *tp_bases;
    PyObject *tp_mro; /* method resolution order */
    PyObject *tp_cache;
    PyObject *tp_subclasses;
    PyObject *tp_weaklist;
    destructor tp_del;

    /* Type attribute cache version tag. Added in version 2.6 */
    unsigned int tp_version_tag;

    destructor tp_finalize;

} PyTypeObject;

这里有 很多 方法。但是不要太担心，如果你要定义一个类型，通常只需要实现少量的方法。

正如你猜到的一样，我们正要一步一步详细介绍各种处理程序。因为有大量的历史包袱影响字段的排序，所以我们不会根据它们在结构体里定义的顺序讲解。通常非常容易找到一个包含你需要的字段的例子，然后改变值去适应你新的类型。

const char *tp_name; /* For printing */

类型的名字 - 上一章提到过的，会出现在很多地方，几乎全部都是为了诊断目的。尝试选择一个好名字，对于诊断很有帮助。

Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */

这些字段告诉运行时在创造这个类型的新对象时需要分配多少内存。Python为了可变长度的结构（想下：字符串，元组）有些内置支持，这是 tp_itemsize 字段存在的原由。这部分稍后解释。

const char *tp_doc;

这里你可以放置一段字符串（或者它的地址），当你想在Python脚本引用 obj.__doc__ 时返回这段文档字符串。

现在我们来看一下基本类型方法 - 大多数扩展类型将实现的方法。

3.1. 终结和内存释放

destructor tp_dealloc;

当您的类型实例的引用计数减少为零并且Python解释器想要回收它时，将调用此函数。如果你的类型有内存可供释放或执行其他清理，你可以把它放在这里。 对象本身也需要在这里释放。 以下是此函数的示例：

static void
newdatatype_dealloc(newdatatypeobject *obj)
{
    free(obj->obj_UnderlyingDatatypePtr);
    Py_TYPE(obj)->tp_free(obj);
}

一个重要的释放器函数实现要求是把所有未决异常放着不动。这很重要是因为释放器会被解释器频繁的调用，当栈异常退出时(而非正常返回)，不会有任何办法保护释放器看到一个异常尚未被设置。此事释放器的任何行为都会导致额外增加的Python代码来检查异常是否被设置。这可能导致解释器的误导性错误。正确的保护方法是，在任何不安全的操作前，保存未决异常，然后在其完成后恢复。者可以通过 PyErr_Fetch() 和 PyErr_Restore() 函数来实现:

static void
my_dealloc(PyObject *obj)
{
    MyObject *self = (MyObject *) obj;
    PyObject *cbresult;

    if (self->my_callback != NULL) {
        PyObject *err_type, *err_value, *err_traceback;

        /* This saves the current exception state */
        PyErr_Fetch(&err_type, &err_value, &err_traceback);

        cbresult = PyObject_CallObject(self->my_callback, NULL);
        if (cbresult == NULL)
            PyErr_WriteUnraisable(self->my_callback);
        else
            Py_DECREF(cbresult);

        /* This restores the saved exception state */
        PyErr_Restore(err_type, err_value, err_traceback);

        Py_DECREF(self->my_callback);
    }
    Py_TYPE(obj)->tp_free((PyObject*)self);
}

注解

There are limitations to what you can safely do in a deallocator function. First, if your type supports garbage collection (using tp_traverse and/or tp_clear), some of the object’s members can have been cleared or finalized by the time tp_dealloc is called. Second, in tp_dealloc, your object is in an unstable state: its reference count is equal to zero. Any call to a non-trivial object or API (as in the example above) might end up calling tp_dealloc again, causing a double free and a crash.

从 Python 3.4 开始，推荐不要在 tp_dealloc 放复杂的终结代码，而是使用新的 tp_finalize 类型方法。

参见

PEP 442 [https://www.python.org/dev/peps/pep-0442] 解释了新的终结方案。

3.2. 对象展示

在 Python 中，有两种方式可以生成对象的文本表示: repr() 函数和 str() 函数。 (print() 函数会直接调用 str()。) 这些处理程序都是可选的。

reprfunc tp_repr;
reprfunc tp_str;

tp_repr 处理程序应该返回一个字符串对象，其中包含调用它的实例的表示形式。 下面是一个简单的例子:

static PyObject *
newdatatype_repr(newdatatypeobject * obj)
{
    return PyUnicode_FromFormat("Repr-ified_newdatatype{{size:%d}}",
                                obj->obj_UnderlyingDatatypePtr->size);
}

如果没有指定 tp_repr 处理程序，解释器将提供一个使用 tp_name 的表示形式以及对象的惟一标识值。

The tp_str handler is to str() what the tp_repr handler described above is to repr(); that is, it is called when Python code calls str() on an instance of your object. Its implementation is very similar to the tp_repr function, but the resulting string is intended for human consumption. If tp_str is not specified, the tp_repr handler is used instead.

下面是一个简单的例子:

static PyObject *
newdatatype_str(newdatatypeobject * obj)
{
    return PyUnicode_FromFormat("Stringified_newdatatype{{size:%d}}",
                                obj->obj_UnderlyingDatatypePtr->size);
}

3.3. 属性管理

For every object which can support attributes, the corresponding type must provide the functions that control how the attributes are resolved. There needs to be a function which can retrieve attributes (if any are defined), and another to set attributes (if setting attributes is allowed). Removing an attribute is a special case, for which the new value passed to the handler is NULL.

Python supports two pairs of attribute handlers; a type that supports attributes only needs to implement the functions for one pair. The difference is that one pair takes the name of the attribute as a char*, while the other accepts a PyObject*. Each type can use whichever pair makes more sense for the implementation’s convenience.

getattrfunc  tp_getattr;        /* char * version */
setattrfunc  tp_setattr;
/* ... */
getattrofunc tp_getattro;       /* PyObject * version */
setattrofunc tp_setattro;

If accessing attributes of an object is always a simple operation (this will be explained shortly), there are generic implementations which can be used to provide the PyObject* version of the attribute management functions. The actual need for type-specific attribute handlers almost completely disappeared starting with Python 2.2, though there are many examples which have not been updated to use some of the new generic mechanism that is available.

3.3.1. 泛型属性管理

Most extension types only use simple attributes. So, what makes the attributes simple? There are only a couple of conditions that must be met:

The name of the attributes must be known when PyType_Ready() is called.

不需要特殊的处理来记录属性是否被查找或设置，也不需要根据值采取操作。

请注意，此列表不对属性的值、值的计算时间或相关数据的存储方式施加任何限制。

When PyType_Ready() is called, it uses three tables referenced by the type object to create descriptors which are placed in the dictionary of the type object. Each descriptor controls access to one attribute of the instance object. Each of the tables is optional; if all three are NULL, instances of the type will only have attributes that are inherited from their base type, and should leave the tp_getattro and tp_setattro fields NULL as well, allowing the base type to handle attributes.

表被声明为object::类型的三个字段:

struct PyMethodDef *tp_methods;
struct PyMemberDef *tp_members;
struct PyGetSetDef *tp_getset;

If tp_methods is not NULL, it must refer to an array of PyMethodDef structures. Each entry in the table is an instance of this structure:

typedef struct PyMethodDef {
    const char  *ml_name;       /* method name */
    PyCFunction  ml_meth;       /* implementation function */
    int          ml_flags;      /* flags */
    const char  *ml_doc;        /* docstring */
} PyMethodDef;

One entry should be defined for each method provided by the type; no entries are needed for methods inherited from a base type. One additional entry is needed at the end; it is a sentinel that marks the end of the array. The ml_name field of the sentinel must be NULL.

The second table is used to define attributes which map directly to data stored in the instance. A variety of primitive C types are supported, and access may be read-only or read-write. The structures in the table are defined as:

typedef struct PyMemberDef {
    char *name;
    int   type;
    int   offset;
    int   flags;
    char *doc;
} PyMemberDef;

For each entry in the table, a descriptor will be constructed and added to the type which will be able to extract a value from the instance structure. The type field should contain one of the type codes defined in the structmember.h header; the value will be used to determine how to convert Python values to and from C values. The flags field is used to store flags which control how the attribute can be accessed.

以下标志常量定义在:file: ‘ structmember.h ‘;它们可以使用bitwise-OR组合。

常数

含义

READONLY

没有可写的

READ_RESTRICTED

Not readable in restricted mode.

WRITE_RESTRICTED

Not writable in restricted mode.

RESTRICTED

在受限模式下不可读，也不可写。

An interesting advantage of using the tp_members table to build descriptors that are used at runtime is that any attribute defined this way can have an associated doc string simply by providing the text in the table. An application can use the introspection API to retrieve the descriptor from the class object, and get the doc string using its __doc__ attribute.

As with the tp_methods table, a sentinel entry with a name value of NULL is required.

3.3.2. Type-specific Attribute Management

For simplicity, only the char* version will be demonstrated here; the type of the name parameter is the only difference between the char* and PyObject* flavors of the interface. This example effectively does the same thing as the generic example above, but does not use the generic support added in Python 2.2. It explains how the handler functions are called, so that if you do need to extend their functionality, you’ll understand what needs to be done.

The tp_getattr handler is called when the object requires an attribute look-up. It is called in the same situations where the __getattr__() method of a class would be called.

例如：

static PyObject *
newdatatype_getattr(newdatatypeobject *obj, char *name)
{
    if (strcmp(name, "data") == 0)
    {
        return PyLong_FromLong(obj->data);
    }

    PyErr_Format(PyExc_AttributeError,
                 "'%.50s' object has no attribute '%.400s'",
                 tp->tp_name, name);
    return NULL;
}

The tp_setattr handler is called when the __setattr__() or __delattr__() method of a class instance would be called. When an attribute should be deleted, the third parameter will be NULL. Here is an example that simply raises an exception; if this were really all you wanted, the tp_setattr handler should be set to NULL.

static int
newdatatype_setattr(newdatatypeobject *obj, char *name, PyObject *v)
{
    PyErr_Format(PyExc_RuntimeError, "Read-only attribute: %s", name);
    return -1;
}

3.4. Object Comparison

richcmpfunc tp_richcompare;

The tp_richcompare handler is called when comparisons are needed. It is analogous to the rich comparison methods, like __lt__(), and also called by PyObject_RichCompare() and PyObject_RichCompareBool().

This function is called with two Python objects and the operator as arguments, where the operator is one of Py_EQ, Py_NE, Py_LE, Py_GT, Py_LT or Py_GT. It should compare the two objects with respect to the specified operator and return Py_True or Py_False if the comparison is successful, Py_NotImplemented to indicate that comparison is not implemented and the other object’s comparison method should be tried, or NULL if an exception was set.

Here is a sample implementation, for a datatype that is considered equal if the size of an internal pointer is equal:

static PyObject *
newdatatype_richcmp(PyObject *obj1, PyObject *obj2, int op)
{
    PyObject *result;
    int c, size1, size2;

    /* code to make sure that both arguments are of type
       newdatatype omitted */

    size1 = obj1->obj_UnderlyingDatatypePtr->size;
    size2 = obj2->obj_UnderlyingDatatypePtr->size;

    switch (op) {
    case Py_LT: c = size1 <  size2; break;
    case Py_LE: c = size1 <= size2; break;
    case Py_EQ: c = size1 == size2; break;
    case Py_NE: c = size1 != size2; break;
    case Py_GT: c = size1 >  size2; break;
    case Py_GE: c = size1 >= size2; break;
    }
    result = c ? Py_True : Py_False;
    Py_INCREF(result);
    return result;
 }

3.5. Abstract Protocol Support

Python supports a variety of abstract ‘protocols;’ the specific interfaces provided to use these interfaces are documented in 抽象对象层.

A number of these abstract interfaces were defined early in the development of the Python implementation. In particular, the number, mapping, and sequence protocols have been part of Python since the beginning. Other protocols have been added over time. For protocols which depend on several handler routines from the type implementation, the older protocols have been defined as optional blocks of handlers referenced by the type object. For newer protocols there are additional slots in the main type object, with a flag bit being set to indicate that the slots are present and should be checked by the interpreter. (The flag bit does not indicate that the slot values are non-NULL. The flag may be set to indicate the presence of a slot, but a slot may still be unfilled.)

PyNumberMethods   *tp_as_number;
PySequenceMethods *tp_as_sequence;
PyMappingMethods  *tp_as_mapping;

If you wish your object to be able to act like a number, a sequence, or a mapping object, then you place the address of a structure that implements the C type PyNumberMethods, PySequenceMethods, or PyMappingMethods, respectively. It is up to you to fill in this structure with appropriate values. You can find examples of the use of each of these in the Objects directory of the Python source distribution.

hashfunc tp_hash;

This function, if you choose to provide it, should return a hash number for an instance of your data type. Here is a simple example:

static Py_hash_t
newdatatype_hash(newdatatypeobject *obj)
{
    Py_hash_t result;
    result = obj->some_size + 32767 * obj->some_number;
    if (result == -1)
       result = -2;
    return result;
}

Py_hash_t is a signed integer type with a platform-varying width. Returning -1 from tp_hash indicates an error, which is why you should be careful to avoid returning it when hash computation is successful, as seen above.

ternaryfunc tp_call;

This function is called when an instance of your data type is “called”, for example, if obj1 is an instance of your data type and the Python script contains obj1('hello'), the tp_call handler is invoked.

This function takes three arguments:

self is the instance of the data type which is the subject of the call. If the call is obj1('hello'), then self is obj1.

args is a tuple containing the arguments to the call. You can use PyArg_ParseTuple() to extract the arguments.

kwds is a dictionary of keyword arguments that were passed. If this is non-NULL and you support keyword arguments, use PyArg_ParseTupleAndKeywords() to extract the arguments. If you do not want to support keyword arguments and this is non-NULL, raise a TypeError with a message saying that keyword arguments are not supported.

Here is a toy tp_call implementation:

static PyObject *
newdatatype_call(newdatatypeobject *self, PyObject *args, PyObject *kwds)
{
    PyObject *result;
    char *arg1;
    char *arg2;
    char *arg3;

    if (!PyArg_ParseTuple(args, "sss:call", &arg1, &arg2, &arg3)) {
        return NULL;
    }
    result = PyUnicode_FromFormat(
        "Returning -- value: [%d] arg1: [%s] arg2: [%s] arg3: [%s]\n",
        obj->obj_UnderlyingDatatypePtr->size,
        arg1, arg2, arg3);
    return result;
}

/* Iterators */
getiterfunc tp_iter;
iternextfunc tp_iternext;

These functions provide support for the iterator protocol. Both handlers take exactly one parameter, the instance for which they are being called, and return a new reference. In the case of an error, they should set an exception and return NULL. tp_iter corresponds to the Python __iter__() method, while tp_iternext corresponds to the Python __next__() method.

Any iterable object must implement the tp_iter handler, which must return an iterator object. Here the same guidelines apply as for Python classes:

For collections (such as lists and tuples) which can support multiple independent iterators, a new iterator should be created and returned by each call to tp_iter.

Objects which can only be iterated over once (usually due to side effects of iteration, such as file objects) can implement tp_iter by returning a new reference to themselves – and should also therefore implement the tp_iternext handler.

Any iterator object should implement both tp_iter and tp_iternext. An iterator’s tp_iter handler should return a new reference to the iterator. Its tp_iternext handler should return a new reference to the next object in the iteration, if there is one. If the iteration has reached the end, tp_iternext may return NULL without setting an exception, or it may set StopIteration in addition to returning NULL; avoiding the exception can yield slightly better performance. If an actual error occurs, tp_iternext should always set an exception and return NULL.

3.6. Weak Reference Support

One of the goals of Python’s weak reference implementation is to allow any type to participate in the weak reference mechanism without incurring the overhead on performance-critical objects (such as numbers).

参见

Documentation for the weakref module.

For an object to be weakly referencable, the extension type must do two things:

Include a PyObject* field in the C object structure dedicated to the weak reference mechanism. The object’s constructor should leave it NULL (which is automatic when using the default tp_alloc).

Set the tp_weaklistoffset type member to the offset of the aforementioned field in the C object structure, so that the interpreter knows how to access and modify that field.

Concretely, here is how a trivial object structure would be augmented with the required field:

typedef struct {
    PyObject_HEAD
    PyObject *weakreflist;  /* List of weak references */
} TrivialObject;

And the corresponding member in the statically-declared type object:

static PyTypeObject TrivialType = {
    PyVarObject_HEAD_INIT(NULL, 0)
    /* ... other members omitted for brevity ... */
    .tp_weaklistoffset = offsetof(TrivialObject, weakreflist),
};

The only further addition is that tp_dealloc needs to clear any weak references (by calling PyObject_ClearWeakRefs()) if the field is non-NULL:

static void
Trivial_dealloc(TrivialObject *self)
{
    /* Clear weakrefs first before calling any destructors */
    if (self->weakreflist != NULL)
        PyObject_ClearWeakRefs((PyObject *) self);
    /* ... remainder of destruction code omitted for brevity ... */
    Py_TYPE(self)->tp_free((PyObject *) self);
}

3.7. 更多建议

In order to learn how to implement any specific method for your new data type, get the CPython source code. Go to the Objects directory, then search the C source files for tp_ plus the function you want (for example, tp_richcompare). You will find examples of the function you want to implement.

When you need to verify that an object is a concrete instance of the type you are implementing, use the PyObject_TypeCheck() function. A sample of its use might be something like the following:

if (!PyObject_TypeCheck(some_object, &MyType)) {
    PyErr_SetString(PyExc_TypeError, "arg #1 not a mything");
    return NULL;
}

参见

下载CPython源代码版本。

https://www.python.org/downloads/source/

GitHub上开发CPython源代码的CPython项目。

https://github.com/python/cpython

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

扩展和嵌入 Python 解释器 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

扩展和嵌入 Python 解释器 »

|

4. 构建C/C++扩展

一个CPython的C扩展是一个共享库(例如一个Linux上的 .so ，或者Windows上的 .pyd )，其会导出一个 初始化函数 。

为了可导入，共享库必须在 PYTHONPATH 中有效，且必须命名遵循模块名字，通过适当的扩展。当使用distutils时，会自动生成正确的文件名。

初始化函数的声明如下：

PyObject* PyInit_modulename(void)

该函数返回完整初始化过的模块，或一个 PyModuleDef 实例。查看 Initializing C modules 了解更多细节。

对于仅有ASCII编码的模块名，函数必须是 PyInit_<modulename> ，将 <modulename> 替换为模块的名字。当使用 Multi-phase initialization 时，允许使用非ASCII编码的模块名。此时初始化函数的名字是 PyInitU_<modulename> ，而 <modulename> 需要用Python的 punycode 编码，连字号需替换为下划线。在Python里:

def initfunc_name(name):
    try:
        suffix = b'_' + name.encode('ascii')
    except UnicodeEncodeError:
        suffix = b'U_' + name.encode('punycode').replace(b'-', b'_')
    return b'PyInit' + suffix

可以在一个动态库里导出多个模块，通过定义多个初始化函数。而导入他们需要符号链接或自定义导入器，因为缺省时只有对应了文件名的函数才会被发现。查看 “一个库里的多模块” 章节，在 PEP 489 [https://www.python.org/dev/peps/pep-0489] 了解更多细节。

4.1. 使用distutils构建C和C++扩展

扩展模块可以用distutils来构建，这是Python自带的。distutils也支持创建二进制包，用户无需编译器而distutils就能安装扩展。

一个distutils包包含了一个驱动脚本 setup.py 。这是个纯Python文件，大多数时候也很简单，看起来如下：

from distutils.core import setup, Extension

module1 = Extension('demo',
                    sources = ['demo.c'])

setup (name = 'PackageName',
       version = '1.0',
       description = 'This is a demo package',
       ext_modules = [module1])

通过文件 setup.py ，和文件 demo.c ，运行如下

python setup.py build

这会编译 demo.c ，然后产生一个扩展模块叫做 demo 在目录 build 里。依赖于系统，模块文件会放在某个子目录形如 build/lib.system ，名字可能是 demo.so 或 demo.pyd 。

在文件 setup.py 里，所有动作的入口通过 setup 函数。该函数可以接受可变数量个关键字参数，上面的例子只使用了一个子集。特别需要注意的例子指定了构建包的元信息，以及指定了包内容。通常一个包会包括多个模块，就像Python的源码模块、文档、子包等。请参数distutils的文档，在 分发 Python 模块（遗留版本） 来了解更多distutils的特性；本章节只解释构建扩展模块的部分。

通常预计算参数给 setup() ，想要更好的结构化驱动脚本。有如如上例子函数 setup() 的 ext_modules 参数是一列扩展模块，每个是一个 Extension 类的实例。例子中的实例定义了扩展命名为 demo ，从单一源码文件构建 demo.c 。

更多时候，构建一个扩展会复杂的多，需要额外的预处理器定义和库。如下例子展示了这些。

from distutils.core import setup, Extension

module1 = Extension('demo',
                    define_macros = [('MAJOR_VERSION', '1'),
                                     ('MINOR_VERSION', '0')],
                    include_dirs = ['/usr/local/include'],
                    libraries = ['tcl83'],
                    library_dirs = ['/usr/local/lib'],
                    sources = ['demo.c'])

setup (name = 'PackageName',
       version = '1.0',
       description = 'This is a demo package',
       author = 'Martin v. Loewis',
       author_email = 'martin@v.loewis.de',
       url = 'https://docs.python.org/extending/building',
       long_description = '''
This is really just a demo package.
''',
       ext_modules = [module1])

例子中函数 setup() 在调用时额外传递了元信息，是推荐发布包构建时的内容。对于这个扩展，其指定了预处理器定义，include目录，库目录，库。依赖于编译器，distutils还会用其他方式传递信息给编译器。例如在Unix上，结果是如下编译命令

gcc -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC -DMAJOR_VERSION=1 -DMINOR_VERSION=0 -I/usr/local/include -I/usr/local/include/python2.2 -c demo.c -o build/temp.linux-i686-2.2/demo.o

gcc -shared build/temp.linux-i686-2.2/demo.o -L/usr/local/lib -ltcl83 -o build/lib.linux-i686-2.2/demo.so

这些行代码仅用于展示目的；distutils用户应该相信distutils能正确调用。

4.2. 发布你的扩展模块

当一个扩展已经成功的构建过，有三种方式使用。

最终用户通常想要安装模块，可以这么运行

python setup.py install

模块维护者应该制作源码包；要实现可以运行

python setup.py sdist

有些情况下，需要在源码发布包里包含额外的文件；这通过 MANIFEST.in 文件实现，查看 Specifying the files to distribute 了解细节。

如果源码发行包成功构建了，维护者也可以创建二进制发行包。依赖于平台，一个可用的命令如下

python setup.py bdist_wininst
python setup.py bdist_rpm
python setup.py bdist_dumb

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

扩展和嵌入 Python 解释器 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

扩展和嵌入 Python 解释器 »

|

5. 在Windows平台编译C和C++扩展

这一章简要介绍了如何使用 Microsoft Visual C++ 创建 Python 的 Windows 扩展模块，然后再提供有关其工作机理的详细背景信息。 这些说明材料同时适用于 Windows 程序员学习构建 Python 扩展以及 Unix 程序员学习如何生成在 Unix 和 Windows 上均能成功构建的软件。

鼓励模块作者使用 distutils 方式来构建扩展模块，而不使用本节所描述的方式。 你仍将需要使用 C 编译器来构建 Python；通常为 Microsoft Visual C++。

注解

这一章提及了多个包括已编码 Python 版本号的文件名。 这些文件名以显示为 XY 的版本号来代表；在实践中，'X' 将为你所使用的 Python 发布版的主版本号而 'Y' 将为次版本号。 例如，如果你所使用的是 Python 2.2.1，XY 将为 22。

5.1. 菜谱式说明

在 Windows 和 Unix 上构建扩展模块都有两种方式：使用 distutils 包来控制构建过程，或者全手动操作。 distutils 方式适用于大多数扩展；使用 distutils 构建和打包扩展模块的文档见 分发 Python 模块（遗留版本）。 如果你发现你确实需要手动操作，那么研究一下 winsound [https://github.com/python/cpython/tree/3.6/PCbuild/winsound.vcxproj] 标准库模块的项目文件可能会很有帮助。

5.2. Unix 和 Windows 之间的差异

Unix 和 Windows 对于代码的运行时加载使用了完全不同的范式。 在你尝试构建可动态加载的模块之前，要先了解你所用系统是如何工作的。

在 Unix 中，一个共享对象 (.so) 文件中包含将由程序来使用的代码，也包含在程序中可被找到的函数名称和数据。 当文件被合并到程序中时，对在文件代码中这些函数和数据的全部引用都会被改为指向程序中函数和数据在内存中所放置的实际位置。 这基本上是一个链接操作。

在 Windows 中，一个动态链接库 (.dll) 文件中没有悬挂的引用。 而是通过一个查找表执行对函数或数据的访问。 因此在运行时 DLL 代码不必在运行时进行修改；相反地，代码已经使用了 DLL 的查找表，并且在运行时查找表会被修改以指向特定的函数和数据。

在 Unix 中，只存在一种库文件 (.a)，它包含来自多个对象文件 (.o) 的代码。 在创建共享对象文件 (.so) 的链接阶段，链接器可能会发现它不知道某个标识符是在哪里定义的。 链接器将在各个库的对象文件中查找它；如果找到了它，链接器将会包括来自该对象文件的所有代码。

在 Windows 中，存在两种库类型，静态库和导入库 (扩展名都是 .lib)。 静态库类似于 Unix 的 .a 文件；它包含在必要时可被包括的代码。 导入库基本上仅用于让链接器能确保特定标识符是合法的，并且将在 DLL 被加载时出现于程序中。 这样链接器可使用来自导入库的信息构建查找表以便使用未包括在 DLL 中的标识符。 当一个应用程序或 DLL 被链接时，可能会生成一个导入库，它将需要被用于应用程序或 DLL 中未来所有依赖于这些符号的 DLL。

假设你正在编译两个动态加载模块 B 和 C，它们应当共享另一个代码块 A。 在 Unix 上，你 不应 将 A.a 传给链接器作为 B.so 和 C.so；那会导致它被包括两次，这样 B 和 C 将分别拥有它们自己的副本。 在 Windows 上，编译 A.dll 将同时编译 A.lib。 你 应当 将 A.lib 传给链接器用于 B 和 C。 A.lib 并不包含代码；它只包含将在运行时被用于访问 A 的代码的信息。

在 Windows 上，使用导入库有点像是使用 import spam；它让你可以访问 spam 中的名称，但并不会创建一个单独副本。 在 Unix 上，链接到一个库更像是 from spam import *；它会创建一个单独副本。

5.3. DLL 的实际使用

Windows 版 Python 是用 Microsoft Visual C++ 编译的；使用其他编译器可能行也可能不行（但 Borland 看来是可以的）。 这一节的剩余部分只适用于 MSVC++。

当在 Windows 中创建 DLL 时，你必须将 pythonXY.lib 传给链接器。 要编译两个 DLL，spam 和 ni (会使用 spam 中找到的 C 函数)，你应当使用以下命令:

cl /LD /I/python/include spam.c ../libs/pythonXY.lib
cl /LD /I/python/include ni.c spam.lib ../libs/pythonXY.lib

第一个命令创建了三个文件: spam.obj, spam.dll 和 spam.lib。 Spam.dll 不包含任何 Python 函数 (例如 PyArg_ParseTuple())，但它通过 pythonXY.lib 可以知道如何找到所需的 Python 代码。

第二条命令创建了 ni.dll (以及 .obj 和 .lib)，它知道如何从 spam 以及 Python 可执行文件中找到所需的函数。

不是每个标识符都会被导出到查找表。 如果你想要任何其他模块（包括 Python）都能看到你的标识符，你必须写上 _declspec(dllexport)，就如在 void _declspec(dllexport) initspam(void) 或 PyObject _declspec(dllexport) *NiGetSpamData(void) 中一样。

Developer Studio 将加入大量你并不真正需要的导入库，使你的可执行文件大小增加 100K。 要摆脱它们，请使用项目设置对话框的链接选项卡指定 忽略默认库。 将正确的 msvcrtxx.lib 添加到库列表中。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

扩展和嵌入 Python 解释器 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

扩展和嵌入 Python 解释器 »

|

1. 在其它应用程序嵌入 Python

The previous chapters discussed how to extend Python, that is, how to extend the functionality of Python by attaching a library of C functions to it. It is also possible to do it the other way around: enrich your C/C++ application by embedding Python in it. Embedding provides your application with the ability to implement some of the functionality of your application in Python rather than C or C++. This can be used for many purposes; one example would be to allow users to tailor the application to their needs by writing some scripts in Python. You can also use it yourself if some of the functionality can be written in Python more easily.

Embedding Python is similar to extending it, but not quite. The difference is that when you extend Python, the main program of the application is still the Python interpreter, while if you embed Python, the main program may have nothing to do with Python — instead, some parts of the application occasionally call the Python interpreter to run some Python code.

So if you are embedding Python, you are providing your own main program. One of the things this main program has to do is initialize the Python interpreter. At the very least, you have to call the function Py_Initialize(). There are optional calls to pass command line arguments to Python. Then later you can call the interpreter from any part of the application.

There are several different ways to call the interpreter: you can pass a string containing Python statements to PyRun_SimpleString(), or you can pass a stdio file pointer and a file name (for identification in error messages only) to PyRun_SimpleFile(). You can also call the lower-level operations described in the previous chapters to construct and use Python objects.

参见

Python/C API 参考手册

The details of Python’s C interface are given in this manual. A great deal of necessary information can be found here.

1.1. Very High Level Embedding

The simplest form of embedding Python is the use of the very high level interface. This interface is intended to execute a Python script without needing to interact with the application directly. This can for example be used to perform some operation on a file.

#include <Python.h>

int
main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    if (program == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }
    Py_SetProgramName(program);  /* optional but recommended */
    Py_Initialize();
    PyRun_SimpleString("from time import time,ctime\n"
                       "print('Today is', ctime(time()))\n");
    if (Py_FinalizeEx() < 0) {
        exit(120);
    }
    PyMem_RawFree(program);
    return 0;
}

The Py_SetProgramName() function should be called before Py_Initialize() to inform the interpreter about paths to Python run-time libraries. Next, the Python interpreter is initialized with Py_Initialize(), followed by the execution of a hard-coded Python script that prints the date and time. Afterwards, the Py_FinalizeEx() call shuts the interpreter down, followed by the end of the program. In a real program, you may want to get the Python script from another source, perhaps a text-editor routine, a file, or a database. Getting the Python code from a file can better be done by using the PyRun_SimpleFile() function, which saves you the trouble of allocating memory space and loading the file contents.

1.2. Beyond Very High Level Embedding: An overview

The high level interface gives you the ability to execute arbitrary pieces of Python code from your application, but exchanging data values is quite cumbersome to say the least. If you want that, you should use lower level calls. At the cost of having to write more C code, you can achieve almost anything.

It should be noted that extending Python and embedding Python is quite the same activity, despite the different intent. Most topics discussed in the previous chapters are still valid. To show this, consider what the extension code from Python to C really does:

转换 Python 的数据值到 C,

Perform a function call to a C routine using the converted values, and

Convert the data values from the call from C to Python.

When embedding Python, the interface code does:

转换 C 的数据值到 Python,

Perform a function call to a Python interface routine using the converted values, and

Convert the data values from the call from Python to C.

As you can see, the data conversion steps are simply swapped to accommodate the different direction of the cross-language transfer. The only difference is the routine that you call between both data conversions. When extending, you call a C routine, when embedding, you call a Python routine.

This chapter will not discuss how to convert data from Python to C and vice versa. Also, proper use of references and dealing with errors is assumed to be understood. Since these aspects do not differ from extending the interpreter, you can refer to earlier chapters for the required information.

1.3. 纯嵌入

The first program aims to execute a function in a Python script. Like in the section about the very high level interface, the Python interpreter does not directly interact with the application (but that will change in the next section).

The code to run a function defined in a Python script is:

#include <Python.h>

int
main(int argc, char *argv[])
{
    PyObject *pName, *pModule, *pFunc;
    PyObject *pArgs, *pValue;
    int i;

    if (argc < 3) {
        fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
        return 1;
    }

    Py_Initialize();
    pName = PyUnicode_DecodeFSDefault(argv[1]);
    /* Error checking of pName left out */

    pModule = PyImport_Import(pName);
    Py_DECREF(pName);

    if (pModule != NULL) {
        pFunc = PyObject_GetAttrString(pModule, argv[2]);
        /* pFunc is a new reference */

        if (pFunc && PyCallable_Check(pFunc)) {
            pArgs = PyTuple_New(argc - 3);
            for (i = 0; i < argc - 3; ++i) {
                pValue = PyLong_FromLong(atoi(argv[i + 3]));
                if (!pValue) {
                    Py_DECREF(pArgs);
                    Py_DECREF(pModule);
                    fprintf(stderr, "Cannot convert argument\n");
                    return 1;
                }
                /* pValue reference stolen here: */
                PyTuple_SetItem(pArgs, i, pValue);
            }
            pValue = PyObject_CallObject(pFunc, pArgs);
            Py_DECREF(pArgs);
            if (pValue != NULL) {
                printf("Result of call: %ld\n", PyLong_AsLong(pValue));
                Py_DECREF(pValue);
            }
            else {
                Py_DECREF(pFunc);
                Py_DECREF(pModule);
                PyErr_Print();
                fprintf(stderr,"Call failed\n");
                return 1;
            }
        }
        else {
            if (PyErr_Occurred())
                PyErr_Print();
            fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
        }
        Py_XDECREF(pFunc);
        Py_DECREF(pModule);
    }
    else {
        PyErr_Print();
        fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
        return 1;
    }
    if (Py_FinalizeEx() < 0) {
        return 120;
    }
    return 0;
}

This code loads a Python script using argv[1], and calls the function named in argv[2]. Its integer arguments are the other values of the argv array. If you compile and link this program (let’s call the finished executable call), and use it to execute a Python script, such as:

def multiply(a,b):
    print("Will compute", a, "times", b)
    c = 0
    for i in range(0, a):
        c = c + b
    return c

然后结果应该是:

$ call multiply multiply 3 2
Will compute 3 times 2
Result of call: 6

Although the program is quite large for its functionality, most of the code is for data conversion between Python and C, and for error reporting. The interesting part with respect to embedding Python starts with

Py_Initialize();
pName = PyUnicode_DecodeFSDefault(argv[1]);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);

After initializing the interpreter, the script is loaded using PyImport_Import(). This routine needs a Python string as its argument, which is constructed using the PyUnicode_FromString() data conversion routine.

pFunc = PyObject_GetAttrString(pModule, argv[2]);
/* pFunc is a new reference */

if (pFunc && PyCallable_Check(pFunc)) {
    ...
}
Py_XDECREF(pFunc);

Once the script is loaded, the name we’re looking for is retrieved using PyObject_GetAttrString(). If the name exists, and the object returned is callable, you can safely assume that it is a function. The program then proceeds by constructing a tuple of arguments as normal. The call to the Python function is then made with:

pValue = PyObject_CallObject(pFunc, pArgs);

Upon return of the function, pValue is either NULL or it contains a reference to the return value of the function. Be sure to release the reference after examining the value.

1.4. Extending Embedded Python

Until now, the embedded Python interpreter had no access to functionality from the application itself. The Python API allows this by extending the embedded interpreter. That is, the embedded interpreter gets extended with routines provided by the application. While it sounds complex, it is not so bad. Simply forget for a while that the application starts the Python interpreter. Instead, consider the application to be a set of subroutines, and write some glue code that gives Python access to those routines, just like you would write a normal Python extension. For example:

static int numargs=0;

/* Return the number of arguments of the application command line */
static PyObject*
emb_numargs(PyObject *self, PyObject *args)
{
    if(!PyArg_ParseTuple(args, ":numargs"))
        return NULL;
    return PyLong_FromLong(numargs);
}

static PyMethodDef EmbMethods[] = {
    {"numargs", emb_numargs, METH_VARARGS,
     "Return the number of arguments received by the process."},
    {NULL, NULL, 0, NULL}
};

static PyModuleDef EmbModule = {
    PyModuleDef_HEAD_INIT, "emb", NULL, -1, EmbMethods,
    NULL, NULL, NULL, NULL
};

static PyObject*
PyInit_emb(void)
{
    return PyModule_Create(&EmbModule);
}

Insert the above code just above the main() function. Also, insert the following two statements before the call to Py_Initialize():

numargs = argc;
PyImport_AppendInittab("emb", &PyInit_emb);

These two lines initialize the numargs variable, and make the emb.numargs() function accessible to the embedded Python interpreter. With these extensions, the Python script can do things like

import emb
print("Number of arguments", emb.numargs())

In a real application, the methods will expose an API of the application to Python.

1.5. 在 C++ 中嵌入 Python

It is also possible to embed Python in a C++ program; precisely how this is done will depend on the details of the C++ system used; in general you will need to write the main program in C++, and use the C++ compiler to compile and link your program. There is no need to recompile Python itself using C++.

1.6. 在类 Unix 系统中编译和链接

It is not necessarily trivial to find the right flags to pass to your compiler (and linker) in order to embed the Python interpreter into your application, particularly because Python needs to load library modules implemented as C dynamic extensions (.so files) linked against it.

To find out the required compiler and linker flags, you can execute the pythonX.Y-config script which is generated as part of the installation process (a python3-config script may also be available). This script has several options, of which the following will be directly useful to you:

pythonX.Y-config --cflags will give you the recommended flags when compiling:

$ /opt/bin/python3.4-config --cflags
-I/opt/include/python3.4m -I/opt/include/python3.4m -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes

pythonX.Y-config --ldflags will give you the recommended flags when linking:

$ /opt/bin/python3.4-config --ldflags
-L/opt/lib/python3.4/config-3.4m -lpthread -ldl -lutil -lm -lpython3.4m -Xlinker -export-dynamic

注解

To avoid confusion between several Python installations (and especially between the system Python and your own compiled Python), it is recommended that you use the absolute path to pythonX.Y-config, as in the above example.

If this procedure doesn’t work for you (it is not guaranteed to work for all Unix-like platforms; however, we welcome bug reports) you will have to read your system’s documentation about dynamic linking and/or examine Python’s Makefile (use sysconfig.get_makefile_filename() to find its location) and compilation options. In this case, the sysconfig module is a useful tool to programmatically extract the configuration values that you will want to combine together. For example:

>>> import sysconfig
>>> sysconfig.get_config_var('LIBS')
'-lpthread -ldl  -lutil'
>>> sysconfig.get_config_var('LINKFORSHARED')
'-Xlinker -export-dynamic'

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

扩展和嵌入 Python 解释器 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

Python/C API 参考手册

本手册描述了希望编写扩展模块并将 Python 解释器嵌入其应用程序中的 C 和 C++ 程序员可用的 API。同时可以参阅 扩展和嵌入 Python 解释器 ，其中描述了扩展编写的一般原则，但没有详细描述 API 函数。

概述

包含文件

对象、类型和引用计数

异常

嵌入Python

调试构建

稳定的应用程序二进制接口

The Very High Level Layer

引用计数

异常处理

Printing and clearing

抛出异常

警告

Querying the error indicator

Signal Handling

Exception Classes

异常对象

Unicode Exception Objects

递归控制

标准异常

标准警告类别

工具

操作系统实用程序

系统功能

过程控制

导入模块

数据 marshal 操作支持

解析参数并构建值变量

字符串转换与格式化

反射

编解码器注册与支持功能

抽象对象层

对象协议

数字协议

序列协议

映射协议

迭代器协议

缓冲协议

旧缓冲协议

具体的对象层

基本对象

数值对象

序列对象

容器对象

函数对象

其他对象

Initialization, Finalization, and Threads

Initializing and finalizing the interpreter

Process-wide parameters

Thread State and the Global Interpreter Lock

Sub-interpreter support

异步通知

分析和跟踪

高级调试器支持

内存管理

概述

原始内存接口

内存接口

对象分配器

自定义内存分配器

pymalloc 分配器

例子

对象实现支持

在堆上分配对象

Common Object Structures

类型对象

Number Object Structures

Mapping Object Structures

Sequence Object Structures

Buffer Object Structures

Async Object Structures

使对象类型支持循环垃圾回收

API 和 ABI 版本管理

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

概述

Python 的应用编程接口（API）使得 C 和 C++ 程序员可以在多个层级上访问 Python 解释器。该 API 在 C++ 中同样可用，但为简化描述，通常将其称为 Python/C API。使用 Python/C API 有两个基本的理由。第一个理由是为了特定目的而编写 扩展模块；它们是扩展 Python 解释器功能的 C 模块。这可能是最常见的使用场景。第二个理由是将 Python 用作更大规模应用的组件；这种技巧通常被称为在一个应用中 embedding Python。

Writing an extension module is a relatively well-understood process, where a “cookbook” approach works well. There are several tools that automate the process to some extent. While people have embedded Python in other applications since its early existence, the process of embedding Python is less straightforward than writing an extension.

许多 API 函数在你嵌入或是扩展 Python 这两种场景下都能发挥作用；此外，大多数嵌入 Python 的应用程序也需要提供自定义扩展，因此在尝试在实际应用中嵌入 Python 之前先熟悉编写扩展应该会是个好主意。

包含文件

使用 Python/C API 所需要的全部函数、类型和宏定义可通过下面这行语句包含到你的代码之中：

#include "Python.h"

这意味着包含以下标准头文件：<stdio.h>，<string.h>，<errno.h>，<limits.h>，<assert.h> 和 <stdlib.h>（如果可用）。

注解

由于 Python 可能会定义一些能在某些系统上影响标准头文件的预处理器定义，因此在包含任何标准头文件之前，你 必须 先包含 Python.h。

Python.h 所定义的全部用户可见名称（由包含的标准头文件所定义的除外）都带有前缀 Py 或者 _Py。以 _Py 打头的名称是供 Python 实现内部使用的，不应被扩展编写者使用。结构成员名称没有保留前缀。

Important: user code should never define names that begin with Py or _Py. This confuses the reader, and jeopardizes the portability of the user code to future Python versions, which may define additional names beginning with one of these prefixes.

头文件通常会与 Python 一起安装。在 Unix 上，它们位于以下目录：prefix/include/pythonversion/ 和 exec_prefix/include/pythonversion/，其中 prefix 和 exec_prefix 是由向 Python 的 configure 脚本传入的对应形参所定义，而 version 则为 '%d.%d' % sys.version_info[:2]。在 Windows 上，头文件安装于 prefix/include，其中 prefix 是向安装程序指定的安装目录。

要包含头文件，请将两个目录（如果不同）都放到你所用编译器的包含搜索路径中。请 不要 将父目录放入搜索路径然后使用 #include <pythonX.Y/Python.h>；这将使得多平台编译不可用，因为 prefix 下平台无关的头文件需要包含来自 exec_prefix 下特定平台的头文件。

C++ users should note that though the API is defined entirely using C, the header files do properly declare the entry points to be extern "C", so there is no need to do anything special to use the API from C++.

对象、类型和引用计数

大多数 Python/C API 函数都有一个或多个参数以及一个 PyObject* 类型的返回值。此类型是一个指针，指向表示一个任意 Python 对象的不透明数据类型。由于在大多数情况下（例如赋值、作用域规则和参数传递） Python 语言都会以同样的方式处理所有 Python 对象类型，因此它们由一个单独的 C 类型来表示是很适宜的。几乎所有 Python 对象都生存在堆上：你绝不会声明一个 PyObject 类型的自动或静态变量，只有 PyObject* 类型的指针变量可以被声明。唯一的例外是 type 对象；由于此种对象永远不能被释放，所以它们通常是静态 PyTypeObject 对象。

所有 Python 对象（甚至 Python 整数）都有一个 type 和一个 reference count。对象的类型确定它是什么类型的对象（例如整数、列表或用户定义函数；还有更多，如 标准类型层级结构 中所述）。对于每个众所周知的类型，都有一个宏来检查对象是否属于该类型；例如，当（且仅当） a 所指的对象是 Python 列表时 PyList_Check(a) 为真。

引用计数

The reference count is important because today’s computers have a finite (and often severely limited) memory size; it counts how many different places there are that have a reference to an object. Such a place could be another object, or a global (or static) C variable, or a local variable in some C function. When an object’s reference count becomes zero, the object is deallocated. If it contains references to other objects, their reference count is decremented. Those other objects may be deallocated in turn, if this decrement makes their reference count become zero, and so on. (There’s an obvious problem with objects that reference each other here; for now, the solution is “don’t do that.”)

Reference counts are always manipulated explicitly. The normal way is to use the macro Py_INCREF() to increment an object’s reference count by one, and Py_DECREF() to decrement it by one. The Py_DECREF() macro is considerably more complex than the incref one, since it must check whether the reference count becomes zero and then cause the object’s deallocator to be called. The deallocator is a function pointer contained in the object’s type structure. The type-specific deallocator takes care of decrementing the reference counts for other objects contained in the object if this is a compound object type, such as a list, as well as performing any additional finalization that’s needed. There’s no chance that the reference count can overflow; at least as many bits are used to hold the reference count as there are distinct memory locations in virtual memory (assuming sizeof(Py_ssize_t) >= sizeof(void*)). Thus, the reference count increment is a simple operation.

It is not necessary to increment an object’s reference count for every local variable that contains a pointer to an object. In theory, the object’s reference count goes up by one when the variable is made to point to it and it goes down by one when the variable goes out of scope. However, these two cancel each other out, so at the end the reference count hasn’t changed. The only real reason to use the reference count is to prevent the object from being deallocated as long as our variable is pointing to it. If we know that there is at least one other reference to the object that lives at least as long as our variable, there is no need to increment the reference count temporarily. An important situation where this arises is in objects that are passed as arguments to C functions in an extension module that are called from Python; the call mechanism guarantees to hold a reference to every argument for the duration of the call.

However, a common pitfall is to extract an object from a list and hold on to it for a while without incrementing its reference count. Some other operation might conceivably remove the object from the list, decrementing its reference count and possible deallocating it. The real danger is that innocent-looking operations may invoke arbitrary Python code which could do this; there is a code path which allows control to flow back to the user from a Py_DECREF(), so almost any operation is potentially dangerous.

A safe approach is to always use the generic operations (functions whose name begins with PyObject_, PyNumber_, PySequence_ or PyMapping_). These operations always increment the reference count of the object they return. This leaves the caller with the responsibility to call Py_DECREF() when they are done with the result; this soon becomes second nature.

Reference Count Details

The reference count behavior of functions in the Python/C API is best explained in terms of ownership of references. Ownership pertains to references, never to objects (objects are not owned: they are always shared). “Owning a reference” means being responsible for calling Py_DECREF on it when the reference is no longer needed. Ownership can also be transferred, meaning that the code that receives ownership of the reference then becomes responsible for eventually decref’ing it by calling Py_DECREF() or Py_XDECREF() when it’s no longer needed—or passing on this responsibility (usually to its caller). When a function passes ownership of a reference on to its caller, the caller is said to receive a new reference. When no ownership is transferred, the caller is said to borrow the reference. Nothing needs to be done for a borrowed reference.

Conversely, when a calling function passes in a reference to an object, there are two possibilities: the function steals a reference to the object, or it does not. Stealing a reference means that when you pass a reference to a function, that function assumes that it now owns that reference, and you are not responsible for it any longer.

Few functions steal references; the two notable exceptions are PyList_SetItem() and PyTuple_SetItem(), which steal a reference to the item (but not to the tuple or list into which the item is put!). These functions were designed to steal a reference because of a common idiom for populating a tuple or list with newly created objects; for example, the code to create the tuple (1, 2, "three") could look like this (forgetting about error handling for the moment; a better way to code this is shown below):

PyObject *t;

t = PyTuple_New(3);
PyTuple_SetItem(t, 0, PyLong_FromLong(1L));
PyTuple_SetItem(t, 1, PyLong_FromLong(2L));
PyTuple_SetItem(t, 2, PyUnicode_FromString("three"));

Here, PyLong_FromLong() returns a new reference which is immediately stolen by PyTuple_SetItem(). When you want to keep using an object although the reference to it will be stolen, use Py_INCREF() to grab another reference before calling the reference-stealing function.

Incidentally, PyTuple_SetItem() is the only way to set tuple items; PySequence_SetItem() and PyObject_SetItem() refuse to do this since tuples are an immutable data type. You should only use PyTuple_SetItem() for tuples that you are creating yourself.

Equivalent code for populating a list can be written using PyList_New() and PyList_SetItem().

However, in practice, you will rarely use these ways of creating and populating a tuple or list. There’s a generic function, Py_BuildValue(), that can create most common objects from C values, directed by a format string. For example, the above two blocks of code could be replaced by the following (which also takes care of the error checking):

PyObject *tuple, *list;

tuple = Py_BuildValue("(iis)", 1, 2, "three");
list = Py_BuildValue("[iis]", 1, 2, "three");

It is much more common to use PyObject_SetItem() and friends with items whose references you are only borrowing, like arguments that were passed in to the function you are writing. In that case, their behaviour regarding reference counts is much saner, since you don’t have to increment a reference count so you can give a reference away (“have it be stolen”). For example, this function sets all items of a list (actually, any mutable sequence) to a given item:

int
set_all(PyObject *target, PyObject *item)
{
    Py_ssize_t i, n;

    n = PyObject_Length(target);
    if (n < 0)
        return -1;
    for (i = 0; i < n; i++) {
        PyObject *index = PyLong_FromSsize_t(i);
        if (!index)
            return -1;
        if (PyObject_SetItem(target, index, item) < 0) {
            Py_DECREF(index);
            return -1;
        }
        Py_DECREF(index);
    }
    return 0;
}

The situation is slightly different for function return values. While passing a reference to most functions does not change your ownership responsibilities for that reference, many functions that return a reference to an object give you ownership of the reference. The reason is simple: in many cases, the returned object is created on the fly, and the reference you get is the only reference to the object. Therefore, the generic functions that return object references, like PyObject_GetItem() and PySequence_GetItem(), always return a new reference (the caller becomes the owner of the reference).

It is important to realize that whether you own a reference returned by a function depends on which function you call only — the plumage (the type of the object passed as an argument to the function) doesn’t enter into it! Thus, if you extract an item from a list using PyList_GetItem(), you don’t own the reference — but if you obtain the same item from the same list using PySequence_GetItem() (which happens to take exactly the same arguments), you do own a reference to the returned object.

Here is an example of how you could write a function that computes the sum of the items in a list of integers; once using PyList_GetItem(), and once using PySequence_GetItem().

long
sum_list(PyObject *list)
{
    Py_ssize_t i, n;
    long total = 0, value;
    PyObject *item;

    n = PyList_Size(list);
    if (n < 0)
        return -1; /* Not a list */
    for (i = 0; i < n; i++) {
        item = PyList_GetItem(list, i); /* Can't fail */
        if (!PyLong_Check(item)) continue; /* Skip non-integers */
        value = PyLong_AsLong(item);
        if (value == -1 && PyErr_Occurred())
            /* Integer too big to fit in a C long, bail out */
            return -1;
        total += value;
    }
    return total;
}

long
sum_sequence(PyObject *sequence)
{
    Py_ssize_t i, n;
    long total = 0, value;
    PyObject *item;
    n = PySequence_Length(sequence);
    if (n < 0)
        return -1; /* Has no length */
    for (i = 0; i < n; i++) {
        item = PySequence_GetItem(sequence, i);
        if (item == NULL)
            return -1; /* Not a sequence, or other failure */
        if (PyLong_Check(item)) {
            value = PyLong_AsLong(item);
            Py_DECREF(item);
            if (value == -1 && PyErr_Occurred())
                /* Integer too big to fit in a C long, bail out */
                return -1;
            total += value;
        }
        else {
            Py_DECREF(item); /* Discard reference ownership */
        }
    }
    return total;
}

类型

There are few other data types that play a significant role in the Python/C API; most are simple C types such as int, long, double and char*. A few structure types are used to describe static tables used to list the functions exported by a module or the data attributes of a new object type, and another is used to describe the value of a complex number. These will be discussed together with the functions that use them.

异常

Python程序员只需要处理特定需要处理的错误异常；未处理的异常会自动传递给调用者，然后传递给调用者的调用者，依此类推，直到他们到达顶级解释器，在那里将它们报告给用户并伴随堆栈回溯。

For C programmers, however, error checking always has to be explicit. All functions in the Python/C API can raise exceptions, unless an explicit claim is made otherwise in a function’s documentation. In general, when a function encounters an error, it sets an exception, discards any object references that it owns, and returns an error indicator. If not documented otherwise, this indicator is either NULL or -1, depending on the function’s return type. A few functions return a Boolean true/false result, with false indicating an error. Very few functions return no explicit error indicator or have an ambiguous return value, and require explicit testing for errors with PyErr_Occurred(). These exceptions are always explicitly documented.

Exception state is maintained in per-thread storage (this is equivalent to using global storage in an unthreaded application). A thread can be in one of two states: an exception has occurred, or not. The function PyErr_Occurred() can be used to check for this: it returns a borrowed reference to the exception type object when an exception has occurred, and NULL otherwise. There are a number of functions to set the exception state: PyErr_SetString() is the most common (though not the most general) function to set the exception state, and PyErr_Clear() clears the exception state.

The full exception state consists of three objects (all of which can be NULL): the exception type, the corresponding exception value, and the traceback. These have the same meanings as the Python result of sys.exc_info(); however, they are not the same: the Python objects represent the last exception being handled by a Python try … except statement, while the C level exception state only exists while an exception is being passed on between C functions until it reaches the Python bytecode interpreter’s main loop, which takes care of transferring it to sys.exc_info() and friends.

Note that starting with Python 1.5, the preferred, thread-safe way to access the exception state from Python code is to call the function sys.exc_info(), which returns the per-thread exception state for Python code. Also, the semantics of both ways to access the exception state have changed so that a function which catches an exception will save and restore its thread’s exception state so as to preserve the exception state of its caller. This prevents common bugs in exception handling code caused by an innocent-looking function overwriting the exception being handled; it also reduces the often unwanted lifetime extension for objects that are referenced by the stack frames in the traceback.

As a general principle, a function that calls another function to perform some task should check whether the called function raised an exception, and if so, pass the exception state on to its caller. It should discard any object references that it owns, and return an error indicator, but it should not set another exception — that would overwrite the exception that was just raised, and lose important information about the exact cause of the error.

A simple example of detecting exceptions and passing them on is shown in the sum_sequence() example above. It so happens that this example doesn’t need to clean up any owned references when it detects an error. The following example function shows some error cleanup. First, to remind you why you like Python, we show the equivalent Python code:

def incr_item(dict, key):
    try:
        item = dict[key]
    except KeyError:
        item = 0
    dict[key] = item + 1

Here is the corresponding C code, in all its glory:

int
incr_item(PyObject *dict, PyObject *key)
{
    /* Objects all initialized to NULL for Py_XDECREF */
    PyObject *item = NULL, *const_one = NULL, *incremented_item = NULL;
    int rv = -1; /* Return value initialized to -1 (failure) */

    item = PyObject_GetItem(dict, key);
    if (item == NULL) {
        /* Handle KeyError only: */
        if (!PyErr_ExceptionMatches(PyExc_KeyError))
            goto error;

        /* Clear the error and use zero: */
        PyErr_Clear();
        item = PyLong_FromLong(0L);
        if (item == NULL)
            goto error;
    }
    const_one = PyLong_FromLong(1L);
    if (const_one == NULL)
        goto error;

    incremented_item = PyNumber_Add(item, const_one);
    if (incremented_item == NULL)
        goto error;

    if (PyObject_SetItem(dict, key, incremented_item) < 0)
        goto error;
    rv = 0; /* Success */
    /* Continue with cleanup code */

 error:
    /* Cleanup code, shared by success and failure path */

    /* Use Py_XDECREF() to ignore NULL references */
    Py_XDECREF(item);
    Py_XDECREF(const_one);
    Py_XDECREF(incremented_item);

    return rv; /* -1 for error, 0 for success */
}

This example represents an endorsed use of the goto statement in C! It illustrates the use of PyErr_ExceptionMatches() and PyErr_Clear() to handle specific exceptions, and the use of Py_XDECREF() to dispose of owned references that may be NULL (note the 'X' in the name; Py_DECREF() would crash when confronted with a NULL reference). It is important that the variables used to hold owned references are initialized to NULL for this to work; likewise, the proposed return value is initialized to -1 (failure) and only set to success after the final call made is successful.

嵌入Python

The one important task that only embedders (as opposed to extension writers) of the Python interpreter have to worry about is the initialization, and possibly the finalization, of the Python interpreter. Most functionality of the interpreter can only be used after the interpreter has been initialized.

The basic initialization function is Py_Initialize(). This initializes the table of loaded modules, and creates the fundamental modules builtins, __main__, and sys. It also initializes the module search path (sys.path).

Py_Initialize() does not set the “script argument list” (sys.argv). If this variable is needed by Python code that will be executed later, it must be set explicitly with a call to PySys_SetArgvEx(argc, argv, updatepath) after the call to Py_Initialize().

On most systems (in particular, on Unix and Windows, although the details are slightly different), Py_Initialize() calculates the module search path based upon its best guess for the location of the standard Python interpreter executable, assuming that the Python library is found in a fixed location relative to the Python interpreter executable. In particular, it looks for a directory named lib/pythonX.Y relative to the parent directory where the executable named python is found on the shell command search path (the environment variable PATH).

For instance, if the Python executable is found in /usr/local/bin/python, it will assume that the libraries are in /usr/local/lib/pythonX.Y. (In fact, this particular path is also the “fallback” location, used when no executable file named python is found along PATH.) The user can override this behavior by setting the environment variable PYTHONHOME, or insert additional directories in front of the standard path by setting PYTHONPATH.

The embedding application can steer the search by calling Py_SetProgramName(file) before calling Py_Initialize(). Note that PYTHONHOME still overrides this and PYTHONPATH is still inserted in front of the standard path. An application that requires total control has to provide its own implementation of Py_GetPath(), Py_GetPrefix(), Py_GetExecPrefix(), and Py_GetProgramFullPath() (all defined in Modules/getpath.c).

Sometimes, it is desirable to “uninitialize” Python. For instance, the application may want to start over (make another call to Py_Initialize()) or the application is simply done with its use of Python and wants to free memory allocated by Python. This can be accomplished by calling Py_FinalizeEx(). The function Py_IsInitialized() returns true if Python is currently in the initialized state. More information about these functions is given in a later chapter. Notice that Py_FinalizeEx() does not free all memory allocated by the Python interpreter, e.g. memory allocated by extension modules currently cannot be released.

调试构建

Python can be built with several macros to enable extra checks of the interpreter and extension modules. These checks tend to add a large amount of overhead to the runtime so they are not enabled by default.

A full list of the various types of debugging builds is in the file Misc/SpecialBuilds.txt in the Python source distribution. Builds are available that support tracing of reference counts, debugging the memory allocator, or low-level profiling of the main interpreter loop. Only the most frequently-used builds will be described in the remainder of this section.

Compiling the interpreter with the Py_DEBUG macro defined produces what is generally meant by “a debug build” of Python. Py_DEBUG is enabled in the Unix build by adding --with-pydebug to the ./configure command. It is also implied by the presence of the not-Python-specific _DEBUG macro. When Py_DEBUG is enabled in the Unix build, compiler optimization is disabled.

除了前面描述的引用计数调试之外，还执行以下额外检查：

额外检查将添加到对象分配器。

额外的检查将添加到解析器和编译器中。

Downcasts from wide types to narrow types are checked for loss of information.

许多断言被添加到字典和集合实现中。另外，集合对象包含 test_c_api() 方法。

添加输入参数的完整性检查到框架创建中。

使用已知的无效模式初始化整型的存储，以捕获对未初始化数字的引用。

添加底层跟踪和额外的异常检查到虚拟机的运行时中。

Extra checks are added to the memory arena implementation.

添加额外调试到线程模块。

这里可能没有提到的额外的检查。

Defining Py_TRACE_REFS enables reference tracing. When defined, a circular doubly linked list of active objects is maintained by adding two extra fields to every PyObject. Total allocations are tracked as well. Upon exit, all existing references are printed. (In interactive mode this happens after every statement run by the interpreter.) Implied by Py_DEBUG.

有关更多详细信息，请参阅Python源代码中的 Misc/SpecialBuilds.txt 。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

稳定的应用程序二进制接口

传统上，Python的C API将随每个版本而变化。大多数更改都与源代码兼容，通常只添加API，而不是更改现有API或删除API（尽管某些接口会首先弃用然后再删除）。

不幸的是，API兼容性没有扩展到二进制兼容性（ABI）。原因主要是结构定义的演变，在这里添加新字段或更改字段类型可能不会破坏API，但可能会破坏ABI。因此，每个Python版本都需要重新编译扩展模块（即使在未使用任何受影响的接口的情况下，Unix上也可能会出现异常）。此外，在Windows上，扩展模块与特定的pythonXY.dll链接，需要重新编译才能与新的pythonXY.dll链接。

从Python3.2起，已经声明了一个API的子集，以确保稳定的ABI。如果使用此API（也被称为“受限API”）的扩展模块需要定义``Py_LIMITED_API``。许多解释器细节将从扩展模块中隐藏; 反过来，在任何3.x版本（x>=2）上构建的模块都不需要重新编译

在某些情况下，需要添加新函数来扩展稳定版 ABI。希望使用这些新 API 的扩展模块需要将 Py_LIMITED_API 设置为他们想要支持的最低 Python 版本的 PY_VERSION_HEX 值（例如： Python 3.3 为 0x03030000）（参见 API 和 ABI 版本管理）。此类模块将适用于所有后续 Python版本，但无法在旧版本上加载（因为缺少符号）。

从Python 3.2开始，受限API可用的函数集记录在 PEP 384 [https://www.python.org/dev/peps/pep-0384] 。在C API文档中，不属于受限API的API元素标记为 “不属于受限API” 。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

The Very High Level Layer

The functions in this chapter will let you execute Python source code given in a file or a buffer, but they will not let you interact in a more detailed way with the interpreter.

Several of these functions accept a start symbol from the grammar as a parameter. The available start symbols are Py_eval_input, Py_file_input, and Py_single_input. These are described following the functions which accept them as parameters.

Note also that several of these functions take FILE* parameters. One particular issue which needs to be handled carefully is that the FILE structure for different C libraries can be different and incompatible. Under Windows (at least), it is possible for dynamically linked extensions to actually use different libraries, so care should be taken that FILE* parameters are only passed to these functions if it is certain that they were created by the same library that the Python runtime is using.

int Py_Main(int argc, wchar_t **argv)

The main program for the standard interpreter. This is made available for programs which embed Python. The argc and argv parameters should be prepared exactly as those which are passed to a C program’s main() function (converted to wchar_t according to the user’s locale). It is important to note that the argument list may be modified (but the contents of the strings pointed to by the argument list are not). The return value will be 0 if the interpreter exits normally (i.e., without an exception), 1 if the interpreter exits due to an exception, or 2 if the parameter list does not represent a valid Python command line.

Note that if an otherwise unhandled SystemExit is raised, this function will not return 1, but exit the process, as long as Py_InspectFlag is not set.

int PyRun_AnyFile(FILE *fp, const char *filename)

This is a simplified interface to PyRun_AnyFileExFlags() below, leaving closeit set to 0 and flags set to NULL.

int PyRun_AnyFileFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)

This is a simplified interface to PyRun_AnyFileExFlags() below, leaving the closeit argument set to 0.

int PyRun_AnyFileEx(FILE *fp, const char *filename, int closeit)

This is a simplified interface to PyRun_AnyFileExFlags() below, leaving the flags argument set to NULL.

int PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags)

If fp refers to a file associated with an interactive device (console or terminal input or Unix pseudo-terminal), return the value of PyRun_InteractiveLoop(), otherwise return the result of PyRun_SimpleFile(). filename is decoded from the filesystem encoding (sys.getfilesystemencoding()). If filename is NULL, this function uses "???" as the filename.

int PyRun_SimpleString(const char *command)

This is a simplified interface to PyRun_SimpleStringFlags() below, leaving the PyCompilerFlags* argument set to NULL.

int PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)

Executes the Python source code from command in the __main__ module according to the flags argument. If __main__ does not already exist, it is created. Returns 0 on success or -1 if an exception was raised. If there was an error, there is no way to get the exception information. For the meaning of flags, see below.

Note that if an otherwise unhandled SystemExit is raised, this function will not return -1, but exit the process, as long as Py_InspectFlag is not set.

int PyRun_SimpleFile(FILE *fp, const char *filename)

This is a simplified interface to PyRun_SimpleFileExFlags() below, leaving closeit set to 0 and flags set to NULL.

int PyRun_SimpleFileEx(FILE *fp, const char *filename, int closeit)

This is a simplified interface to PyRun_SimpleFileExFlags() below, leaving flags set to NULL.

int PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit, PyCompilerFlags *flags)

Similar to PyRun_SimpleStringFlags(), but the Python source code is read from fp instead of an in-memory string. filename should be the name of the file, it is decoded from the filesystem encoding (sys.getfilesystemencoding()). If closeit is true, the file is closed before PyRun_SimpleFileExFlags returns.

int PyRun_InteractiveOne(FILE *fp, const char *filename)

This is a simplified interface to PyRun_InteractiveOneFlags() below, leaving flags set to NULL.

int PyRun_InteractiveOneFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)

Read and execute a single statement from a file associated with an interactive device according to the flags argument. The user will be prompted using sys.ps1 and sys.ps2. filename is decoded from the filesystem encoding (sys.getfilesystemencoding()).

Returns 0 when the input was executed successfully, -1 if there was an exception, or an error code from the errcode.h include file distributed as part of Python if there was a parse error. (Note that errcode.h is not included by Python.h, so must be included specifically if needed.)

int PyRun_InteractiveLoop(FILE *fp, const char *filename)

This is a simplified interface to PyRun_InteractiveLoopFlags() below, leaving flags set to NULL.

int PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)

Read and execute statements from a file associated with an interactive device until EOF is reached. The user will be prompted using sys.ps1 and sys.ps2. filename is decoded from the filesystem encoding (sys.getfilesystemencoding()). Returns 0 at EOF or a negative number upon failure.

int (*PyOS_InputHook)(void)

Can be set to point to a function with the prototype int func(void). The function will be called when Python’s interpreter prompt is about to become idle and wait for user input from the terminal. The return value is ignored. Overriding this hook can be used to integrate the interpreter’s prompt with other event loops, as done in the Modules/_tkinter.c in the Python source code.

char* (*PyOS_ReadlineFunctionPointer)(FILE *, FILE *, const char *)

Can be set to point to a function with the prototype char *func(FILE *stdin, FILE *stdout, char *prompt), overriding the default function used to read a single line of input at the interpreter’s prompt. The function is expected to output the string prompt if it’s not NULL, and then read a line of input from the provided standard input file, returning the resulting string. For example, The readline module sets this hook to provide line-editing and tab-completion features.

The result must be a string allocated by PyMem_RawMalloc() or PyMem_RawRealloc(), or NULL if an error occurred.

在 3.4 版更改: The result must be allocated by PyMem_RawMalloc() or PyMem_RawRealloc(), instead of being allocated by PyMem_Malloc() or PyMem_Realloc().

struct _node* PyParser_SimpleParseString(const char *str, int start)

This is a simplified interface to PyParser_SimpleParseStringFlagsFilename() below, leaving filename set to NULL and flags set to 0.

struct _node* PyParser_SimpleParseStringFlags(const char *str, int start, int flags)

This is a simplified interface to PyParser_SimpleParseStringFlagsFilename() below, leaving filename set to NULL.

struct _node* PyParser_SimpleParseStringFlagsFilename(const char *str, const char *filename, int start, int flags)

Parse Python source code from str using the start token start according to the flags argument. The result can be used to create a code object which can be evaluated efficiently. This is useful if a code fragment must be evaluated many times. filename is decoded from the filesystem encoding (sys.getfilesystemencoding()).

struct _node* PyParser_SimpleParseFile(FILE *fp, const char *filename, int start)

This is a simplified interface to PyParser_SimpleParseFileFlags() below, leaving flags set to 0.

struct _node* PyParser_SimpleParseFileFlags(FILE *fp, const char *filename, int start, int flags)

Similar to PyParser_SimpleParseStringFlagsFilename(), but the Python source code is read from fp instead of an in-memory string.

PyObject* PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals)

Return value: New reference.

This is a simplified interface to PyRun_StringFlags() below, leaving flags set to NULL.

PyObject* PyRun_StringFlags(const char *str, int start, PyObject *globals, PyObject *locals, PyCompilerFlags *flags)

Return value: New reference.

Execute Python source code from str in the context specified by the objects globals and locals with the compiler flags specified by flags. globals must be a dictionary; locals can be any object that implements the mapping protocol. The parameter start specifies the start token that should be used to parse the source code.

Returns the result of executing the code as a Python object, or NULL if an exception was raised.

PyObject* PyRun_File(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals)

Return value: New reference.

This is a simplified interface to PyRun_FileExFlags() below, leaving closeit set to 0 and flags set to NULL.

PyObject* PyRun_FileEx(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, int closeit)

Return value: New reference.

This is a simplified interface to PyRun_FileExFlags() below, leaving flags set to NULL.

PyObject* PyRun_FileFlags(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, PyCompilerFlags *flags)

Return value: New reference.

This is a simplified interface to PyRun_FileExFlags() below, leaving closeit set to 0.

PyObject* PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals, PyObject *locals, int closeit, PyCompilerFlags *flags)

Return value: New reference.

Similar to PyRun_StringFlags(), but the Python source code is read from fp instead of an in-memory string. filename should be the name of the file, it is decoded from the filesystem encoding (sys.getfilesystemencoding()). If closeit is true, the file is closed before PyRun_FileExFlags() returns.

PyObject* Py_CompileString(const char *str, const char *filename, int start)

Return value: New reference.

This is a simplified interface to Py_CompileStringFlags() below, leaving flags set to NULL.

PyObject* Py_CompileStringFlags(const char *str, const char *filename, int start, PyCompilerFlags *flags)

Return value: New reference.

This is a simplified interface to Py_CompileStringExFlags() below, with optimize set to -1.

PyObject* Py_CompileStringObject(const char *str, PyObject *filename, int start, PyCompilerFlags *flags, int optimize)

Parse and compile the Python source code in str, returning the resulting code object. The start token is given by start; this can be used to constrain the code which can be compiled and should be Py_eval_input, Py_file_input, or Py_single_input. The filename specified by filename is used to construct the code object and may appear in tracebacks or SyntaxError exception messages. This returns NULL if the code cannot be parsed or compiled.

The integer optimize specifies the optimization level of the compiler; a value of -1 selects the optimization level of the interpreter as given by -O options. Explicit levels are 0 (no optimization; __debug__ is true), 1 (asserts are removed, __debug__ is false) or 2 (docstrings are removed too).

3.4 新版功能.

PyObject* Py_CompileStringExFlags(const char *str, const char *filename, int start, PyCompilerFlags *flags, int optimize)

Like Py_CompileStringObject(), but filename is a byte string decoded from the filesystem encoding (os.fsdecode()).

3.2 新版功能.

PyObject* PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)

Return value: New reference.

This is a simplified interface to PyEval_EvalCodeEx(), with just the code object, and global and local variables. The other arguments are set to NULL.

PyObject* PyEval_EvalCodeEx(PyObject *co, PyObject *globals, PyObject *locals, PyObject **args, int argcount, PyObject **kws, int kwcount, PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)

Evaluate a precompiled code object, given a particular environment for its evaluation. This environment consists of a dictionary of global variables, a mapping object of local variables, arrays of arguments, keywords and defaults, a dictionary of default values for keyword-only arguments and a closure tuple of cells.

PyFrameObject

The C structure of the objects used to describe frame objects. The fields of this type are subject to change at any time.

PyObject* PyEval_EvalFrame(PyFrameObject *f)

Evaluate an execution frame. This is a simplified interface to PyEval_EvalFrameEx(), for backward compatibility.

PyObject* PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)

This is the main, unvarnished function of Python interpretation. It is literally 2000 lines long. The code object associated with the execution frame f is executed, interpreting bytecode and executing calls as needed. The additional throwflag parameter can mostly be ignored - if true, then it causes an exception to immediately be thrown; this is used for the throw() methods of generator objects.

在 3.4 版更改: This function now includes a debug assertion to help ensure that it does not silently discard an active exception.

int PyEval_MergeCompilerFlags(PyCompilerFlags *cf)

This function changes the flags of the current evaluation frame, and returns true on success, false on failure.

int Py_eval_input

The start symbol from the Python grammar for isolated expressions; for use with Py_CompileString().

int Py_file_input

The start symbol from the Python grammar for sequences of statements as read from a file or other source; for use with Py_CompileString(). This is the symbol to use when compiling arbitrarily long Python source code.

int Py_single_input

The start symbol from the Python grammar for a single statement; for use with Py_CompileString(). This is the symbol used for the interactive interpreter loop.

struct PyCompilerFlags

This is the structure used to hold compiler flags. In cases where code is only being compiled, it is passed as int flags, and in cases where code is being executed, it is passed as PyCompilerFlags *flags. In this case, from __future__ import can modify flags.

Whenever PyCompilerFlags *flags is NULL, cf_flags is treated as equal to 0, and any modification due to from __future__ import is discarded.

struct PyCompilerFlags {
    int cf_flags;
}

int CO_FUTURE_DIVISION

This bit can be set in flags to cause division operator / to be interpreted as “true division” according to PEP 238 [https://www.python.org/dev/peps/pep-0238].

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

引用计数

本节介绍的宏被用于管理 Python 对象的引用计数。

void Py_INCREF(PyObject *o)

Increment the reference count for object o. The object must not be NULL; if you aren’t sure that it isn’t NULL, use Py_XINCREF().

void Py_XINCREF(PyObject *o)

Increment the reference count for object o. The object may be NULL, in which case the macro has no effect.

void Py_DECREF(PyObject *o)

Decrement the reference count for object o. The object must not be NULL; if you aren’t sure that it isn’t NULL, use Py_XDECREF(). If the reference count reaches zero, the object’s type’s deallocation function (which must not be NULL) is invoked.

警告

释放函数可导致任意 Python 代码被发起调用（例如当一个带有 __del__() 方法的类实例被释放时就是如此）。 虽然此类代码中的异常不会被传播，但被执行的代码能够自由访问所有 Python 全局变量。 这意味着任何可通过全局变量获取的对象在 Py_DECREF() 被发起调用之前都应当处于完好状态。 例如，从一个列表中删除对象的代码应当将被删除对象的引用拷贝到一个临时变量中，更新列表数据结构，然后再为临时变量调用 Py_DECREF()。

void Py_XDECREF(PyObject *o)

Decrement the reference count for object o. The object may be NULL, in which case the macro has no effect; otherwise the effect is the same as for Py_DECREF(), and the same warning applies.

void Py_CLEAR(PyObject *o)

Decrement the reference count for object o. The object may be NULL, in which case the macro has no effect; otherwise the effect is the same as for Py_DECREF(), except that the argument is also set to NULL. The warning for Py_DECREF() does not apply with respect to the object passed because the macro carefully uses a temporary variable and sets the argument to NULL before decrementing its reference count.

当要减少在垃圾回收期间可能会被遍历的变量的值时，使用该宏是一个好主意。

以下函数适用于 Python 的运行时动态嵌入: Py_IncRef(PyObject *o), Py_DecRef(PyObject *o)。 它们分别只是 Py_XINCREF() 和 Py_XDECREF() 的简单导出函数版本。

以下函数或宏仅可在解释器核心内部使用: _Py_Dealloc(), _Py_ForgetReference(), _Py_NewReference() 以及全局变量 _Py_RefTotal。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

异常处理

The functions described in this chapter will let you handle and raise Python exceptions. It is important to understand some of the basics of Python exception handling. It works somewhat like the POSIX errno variable: there is a global indicator (per thread) of the last error that occurred. Most C API functions don’t clear this on success, but will set it to indicate the cause of the error on failure. Most C API functions also return an error indicator, usually NULL if they are supposed to return a pointer, or -1 if they return an integer (exception: the PyArg_*() functions return 1 for success and 0 for failure).

Concretely, the error indicator consists of three object pointers: the exception’s type, the exception’s value, and the traceback object. Any of those pointers can be NULL if non-set (although some combinations are forbidden, for example you can’t have a non-NULL traceback if the exception type is NULL).

When a function must fail because some function it called failed, it generally doesn’t set the error indicator; the function it called already set it. It is responsible for either handling the error and clearing the exception or returning after cleaning up any resources it holds (such as object references or memory allocations); it should not continue normally if it is not prepared to handle the error. If returning due to an error, it is important to indicate to the caller that an error has been set. If the error is not handled or carefully propagated, additional calls into the Python/C API may not behave as intended and may fail in mysterious ways.

注解

The error indicator is not the result of sys.exc_info(). The former corresponds to an exception that is not yet caught (and is therefore still propagating), while the latter returns an exception after it is caught (and has therefore stopped propagating).

Printing and clearing

void PyErr_Clear()

Clear the error indicator. If the error indicator is not set, there is no effect.

void PyErr_PrintEx(int set_sys_last_vars)

Print a standard traceback to sys.stderr and clear the error indicator. Unless the error is a SystemExit. In that case the no traceback is printed and Python process will exit with the error code specified by the SystemExit instance.

只有在错误指示器被设置时才需要调用这个函数，否则这会导致错误！

If set_sys_last_vars is nonzero, the variables sys.last_type, sys.last_value and sys.last_traceback will be set to the type, value and traceback of the printed exception, respectively.

void PyErr_Print()

PyErr_PrintEx(1) 的别名。

void PyErr_WriteUnraisable(PyObject *obj)

This utility function prints a warning message to sys.stderr when an exception has been set but it is impossible for the interpreter to actually raise the exception. It is used, for example, when an exception occurs in an __del__() method.

The function is called with a single argument obj that identifies the context in which the unraisable exception occurred. If possible, the repr of obj will be printed in the warning message.

抛出异常

These functions help you set the current thread’s error indicator. For convenience, some of these functions will always return a NULL pointer for use in a return statement.

void PyErr_SetString(PyObject *type, const char *message)

This is the most common way to set the error indicator. The first argument specifies the exception type; it is normally one of the standard exceptions, e.g. PyExc_RuntimeError. You need not increment its reference count. The second argument is an error message; it is decoded from 'utf-8’.

void PyErr_SetObject(PyObject *type, PyObject *value)

This function is similar to PyErr_SetString() but lets you specify an arbitrary Python object for the “value” of the exception.

PyObject* PyErr_Format(PyObject *exception, const char *format, ...)

Return value: Always NULL.

This function sets the error indicator and returns NULL. exception should be a Python exception class. The format and subsequent parameters help format the error message; they have the same meaning and values as in PyUnicode_FromFormat(). format is an ASCII-encoded string.

PyObject* PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)

Return value: Always NULL.

Same as PyErr_Format(), but taking a va_list argument rather than a variable number of arguments.

3.5 新版功能.

void PyErr_SetNone(PyObject *type)

This is a shorthand for PyErr_SetObject(type, Py_None).

int PyErr_BadArgument()

This is a shorthand for PyErr_SetString(PyExc_TypeError, message), where message indicates that a built-in operation was invoked with an illegal argument. It is mostly for internal use.

PyObject* PyErr_NoMemory()

Return value: Always NULL.

This is a shorthand for PyErr_SetNone(PyExc_MemoryError); it returns NULL so an object allocation function can write return PyErr_NoMemory(); when it runs out of memory.

PyObject* PyErr_SetFromErrno(PyObject *type)

Return value: Always NULL.

This is a convenience function to raise an exception when a C library function has returned an error and set the C variable errno. It constructs a tuple object whose first item is the integer errno value and whose second item is the corresponding error message (gotten from strerror()), and then calls PyErr_SetObject(type, object). On Unix, when the errno value is EINTR, indicating an interrupted system call, this calls PyErr_CheckSignals(), and if that set the error indicator, leaves it set to that. The function always returns NULL, so a wrapper function around a system call can write return PyErr_SetFromErrno(type); when the system call returns an error.

PyObject* PyErr_SetFromErrnoWithFilenameObject(PyObject *type, PyObject *filenameObject)

Similar to PyErr_SetFromErrno(), with the additional behavior that if filenameObject is not NULL, it is passed to the constructor of type as a third parameter. In the case of OSError exception, this is used to define the filename attribute of the exception instance.

PyObject* PyErr_SetFromErrnoWithFilenameObjects(PyObject *type, PyObject *filenameObject, PyObject *filenameObject2)

Similar to PyErr_SetFromErrnoWithFilenameObject(), but takes a second filename object, for raising errors when a function that takes two filenames fails.

3.4 新版功能.

PyObject* PyErr_SetFromErrnoWithFilename(PyObject *type, const char *filename)

Return value: Always NULL.

Similar to PyErr_SetFromErrnoWithFilenameObject(), but the filename is given as a C string. filename is decoded from the filesystem encoding (os.fsdecode()).

PyObject* PyErr_SetFromWindowsErr(int ierr)

Return value: Always NULL.

This is a convenience function to raise WindowsError. If called with ierr of 0, the error code returned by a call to GetLastError() is used instead. It calls the Win32 function FormatMessage() to retrieve the Windows description of error code given by ierr or GetLastError(), then it constructs a tuple object whose first item is the ierr value and whose second item is the corresponding error message (gotten from FormatMessage()), and then calls PyErr_SetObject(PyExc_WindowsError, object). This function always returns NULL. Availability: Windows.

PyObject* PyErr_SetExcFromWindowsErr(PyObject *type, int ierr)

Return value: Always NULL.

Similar to PyErr_SetFromWindowsErr(), with an additional parameter specifying the exception type to be raised. Availability: Windows.

PyObject* PyErr_SetFromWindowsErrWithFilename(int ierr, const char *filename)

Return value: Always NULL.

Similar to PyErr_SetFromWindowsErrWithFilenameObject(), but the filename is given as a C string. filename is decoded from the filesystem encoding (os.fsdecode()). Availability: Windows.

PyObject* PyErr_SetExcFromWindowsErrWithFilenameObject(PyObject *type, int ierr, PyObject *filename)

Similar to PyErr_SetFromWindowsErrWithFilenameObject(), with an additional parameter specifying the exception type to be raised. Availability: Windows.

PyObject* PyErr_SetExcFromWindowsErrWithFilenameObjects(PyObject *type, int ierr, PyObject *filename, PyObject *filename2)

Similar to PyErr_SetExcFromWindowsErrWithFilenameObject(), but accepts a second filename object. Availability: Windows.

3.4 新版功能.

PyObject* PyErr_SetExcFromWindowsErrWithFilename(PyObject *type, int ierr, const char *filename)

Return value: Always NULL.

Similar to PyErr_SetFromWindowsErrWithFilename(), with an additional parameter specifying the exception type to be raised. Availability: Windows.

PyObject* PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)

This is a convenience function to raise ImportError. msg will be set as the exception’s message string. name and path, both of which can be NULL, will be set as the ImportError’s respective name and path attributes.

3.3 新版功能.

void PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)

Set file, line, and offset information for the current exception. If the current exception is not a SyntaxError, then it sets additional attributes, which make the exception printing subsystem think the exception is a SyntaxError.

3.4 新版功能.

void PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)

Like PyErr_SyntaxLocationObject(), but filename is a byte string decoded from the filesystem encoding (os.fsdecode()).

3.2 新版功能.

void PyErr_SyntaxLocation(const char *filename, int lineno)

Like PyErr_SyntaxLocationEx(), but the col_offset parameter is omitted.

void PyErr_BadInternalCall()

This is a shorthand for PyErr_SetString(PyExc_SystemError, message), where message indicates that an internal operation (e.g. a Python/C API function) was invoked with an illegal argument. It is mostly for internal use.

警告

Use these functions to issue warnings from C code. They mirror similar functions exported by the Python warnings module. They normally print a warning message to sys.stderr; however, it is also possible that the user has specified that warnings are to be turned into errors, and in that case they will raise an exception. It is also possible that the functions raise an exception because of a problem with the warning machinery. The return value is 0 if no exception is raised, or -1 if an exception is raised. (It is not possible to determine whether a warning message is actually printed, nor what the reason is for the exception; this is intentional.) If an exception is raised, the caller should do its normal exception handling (for example, Py_DECREF() owned references and return an error value).

int PyErr_WarnEx(PyObject *category, const char *message, Py_ssize_t stack_level)

Issue a warning message. The category argument is a warning category (see below) or NULL; the message argument is a UTF-8 encoded string. stack_level is a positive number giving a number of stack frames; the warning will be issued from the currently executing line of code in that stack frame. A stack_level of 1 is the function calling PyErr_WarnEx(), 2 is the function above that, and so forth.

Warning categories must be subclasses of PyExc_Warning; PyExc_Warning is a subclass of PyExc_Exception; the default warning category is PyExc_RuntimeWarning. The standard Python warning categories are available as global variables whose names are enumerated at 标准警告类别.

For information about warning control, see the documentation for the warnings module and the -W option in the command line documentation. There is no C API for warning control.

PyObject* PyErr_SetImportErrorSubclass(PyObject *msg, PyObject *name, PyObject *path)

Much like PyErr_SetImportError() but this function allows for specifying a subclass of ImportError to raise.

3.6 新版功能.

int PyErr_WarnExplicitObject(PyObject *category, PyObject *message, PyObject *filename, int lineno, PyObject *module, PyObject *registry)

Issue a warning message with explicit control over all warning attributes. This is a straightforward wrapper around the Python function warnings.warn_explicit(), see there for more information. The module and registry arguments may be set to NULL to get the default effect described there.

3.4 新版功能.

int PyErr_WarnExplicit(PyObject *category, const char *message, const char *filename, int lineno, const char *module, PyObject *registry)

Similar to PyErr_WarnExplicitObject() except that message and module are UTF-8 encoded strings, and filename is decoded from the filesystem encoding (os.fsdecode()).

int PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level, const char *format, ...)

Function similar to PyErr_WarnEx(), but use PyUnicode_FromFormat() to format the warning message. format is an ASCII-encoded string.

3.2 新版功能.

int PyErr_ResourceWarning(PyObject *source, Py_ssize_t stack_level, const char *format, ...)

Function similar to PyErr_WarnFormat(), but category is ResourceWarning and pass source to warnings.WarningMessage().

3.6 新版功能.

Querying the error indicator

PyObject* PyErr_Occurred()

Return value: Borrowed reference.

Test whether the error indicator is set. If set, return the exception type (the first argument to the last call to one of the PyErr_Set*() functions or to PyErr_Restore()). If not set, return NULL. You do not own a reference to the return value, so you do not need to Py_DECREF() it.

注解

Do not compare the return value to a specific exception; use PyErr_ExceptionMatches() instead, shown below. (The comparison could easily fail since the exception may be an instance instead of a class, in the case of a class exception, or it may be a subclass of the expected exception.)

int PyErr_ExceptionMatches(PyObject *exc)

Equivalent to PyErr_GivenExceptionMatches(PyErr_Occurred(), exc). This should only be called when an exception is actually set; a memory access violation will occur if no exception has been raised.

int PyErr_GivenExceptionMatches(PyObject *given, PyObject *exc)

Return true if the given exception matches the exception type in exc. If exc is a class object, this also returns true when given is an instance of a subclass. If exc is a tuple, all exception types in the tuple (and recursively in subtuples) are searched for a match.

void PyErr_Fetch(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback)

Retrieve the error indicator into three variables whose addresses are passed. If the error indicator is not set, set all three variables to NULL. If it is set, it will be cleared and you own a reference to each object retrieved. The value and traceback object may be NULL even when the type object is not.

注解

This function is normally only used by code that needs to catch exceptions or by code that needs to save and restore the error indicator temporarily, e.g.:

{
   PyObject *type, *value, *traceback;
   PyErr_Fetch(&type, &value, &traceback);

   /* ... code that might produce other errors ... */

   PyErr_Restore(type, value, traceback);
}

void PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)

Set the error indicator from the three objects. If the error indicator is already set, it is cleared first. If the objects are NULL, the error indicator is cleared. Do not pass a NULL type and non-NULL value or traceback. The exception type should be a class. Do not pass an invalid exception type or value. (Violating these rules will cause subtle problems later.) This call takes away a reference to each object: you must own a reference to each object before the call and after the call you no longer own these references. (If you don’t understand this, don’t use this function. I warned you.)

注解

This function is normally only used by code that needs to save and restore the error indicator temporarily. Use PyErr_Fetch() to save the current error indicator.

void PyErr_NormalizeException(PyObject**exc, PyObject**val, PyObject**tb)

Under certain circumstances, the values returned by PyErr_Fetch() below can be “unnormalized”, meaning that *exc is a class object but *val is not an instance of the same class. This function can be used to instantiate the class in that case. If the values are already normalized, nothing happens. The delayed normalization is implemented to improve performance.

注解

This function does not implicitly set the __traceback__ attribute on the exception value. If setting the traceback appropriately is desired, the following additional snippet is needed:

if (tb != NULL) {
  PyException_SetTraceback(val, tb);
}

void PyErr_GetExcInfo(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback)

Retrieve the exception info, as known from sys.exc_info(). This refers to an exception that was already caught, not to an exception that was freshly raised. Returns new references for the three objects, any of which may be NULL. Does not modify the exception info state.

注解

This function is not normally used by code that wants to handle exceptions. Rather, it can be used when code needs to save and restore the exception state temporarily. Use PyErr_SetExcInfo() to restore or clear the exception state.

3.3 新版功能.

void PyErr_SetExcInfo(PyObject *type, PyObject *value, PyObject *traceback)

Set the exception info, as known from sys.exc_info(). This refers to an exception that was already caught, not to an exception that was freshly raised. This function steals the references of the arguments. To clear the exception state, pass NULL for all three arguments. For general rules about the three arguments, see PyErr_Restore().

注解

This function is not normally used by code that wants to handle exceptions. Rather, it can be used when code needs to save and restore the exception state temporarily. Use PyErr_GetExcInfo() to read the exception state.

3.3 新版功能.

Signal Handling

int PyErr_CheckSignals()

This function interacts with Python’s signal handling. It checks whether a signal has been sent to the processes and if so, invokes the corresponding signal handler. If the signal module is supported, this can invoke a signal handler written in Python. In all cases, the default effect for SIGINT is to raise the KeyboardInterrupt exception. If an exception is raised the error indicator is set and the function returns -1; otherwise the function returns 0. The error indicator may or may not be cleared if it was previously set.

void PyErr_SetInterrupt()

This function simulates the effect of a SIGINT signal arriving — the next time PyErr_CheckSignals() is called, KeyboardInterrupt will be raised. It may be called without holding the interpreter lock.

int PySignal_SetWakeupFd(int fd)

This utility function specifies a file descriptor to which the signal number is written as a single byte whenever a signal is received. fd must be non-blocking. It returns the previous such file descriptor.

The value -1 disables the feature; this is the initial state. This is equivalent to signal.set_wakeup_fd() in Python, but without any error checking. fd should be a valid file descriptor. The function should only be called from the main thread.

在 3.5 版更改: 在 Windows 上，此函数现在也支持套接字处理。

Exception Classes

PyObject* PyErr_NewException(const char *name, PyObject *base, PyObject *dict)

Return value: New reference.

This utility function creates and returns a new exception class. The name argument must be the name of the new exception, a C string of the form module.classname. The base and dict arguments are normally NULL. This creates a class object derived from Exception (accessible in C as PyExc_Exception).

The __module__ attribute of the new class is set to the first part (up to the last dot) of the name argument, and the class name is set to the last part (after the last dot). The base argument can be used to specify alternate base classes; it can either be only one class or a tuple of classes. The dict argument can be used to specify a dictionary of class variables and methods.

PyObject* PyErr_NewExceptionWithDoc(const char *name, const char *doc, PyObject *base, PyObject *dict)

Return value: New reference.

Same as PyErr_NewException(), except that the new exception class can easily be given a docstring: If doc is non-NULL, it will be used as the docstring for the exception class.

3.2 新版功能.

异常对象

PyObject* PyException_GetTraceback(PyObject *ex)

Return value: New reference.

Return the traceback associated with the exception as a new reference, as accessible from Python through __traceback__. If there is no traceback associated, this returns NULL.

int PyException_SetTraceback(PyObject *ex, PyObject *tb)

Set the traceback associated with the exception to tb. Use Py_None to clear it.

PyObject* PyException_GetContext(PyObject *ex)

Return the context (another exception instance during whose handling ex was raised) associated with the exception as a new reference, as accessible from Python through __context__. If there is no context associated, this returns NULL.

void PyException_SetContext(PyObject *ex, PyObject *ctx)

Set the context associated with the exception to ctx. Use NULL to clear it. There is no type check to make sure that ctx is an exception instance. This steals a reference to ctx.

PyObject* PyException_GetCause(PyObject *ex)

Return the cause (either an exception instance, or None, set by raise ... from ...) associated with the exception as a new reference, as accessible from Python through __cause__.

void PyException_SetCause(PyObject *ex, PyObject *cause)

Set the cause associated with the exception to cause. Use NULL to clear it. There is no type check to make sure that cause is either an exception instance or None. This steals a reference to cause.

__suppress_context__ is implicitly set to True by this function.

Unicode Exception Objects

The following functions are used to create and modify Unicode exceptions from C.

PyObject* PyUnicodeDecodeError_Create(const char *encoding, const char *object, Py_ssize_t length, Py_ssize_t start, Py_ssize_t end, const char *reason)

Create a UnicodeDecodeError object with the attributes encoding, object, length, start, end and reason. encoding and reason are UTF-8 encoded strings.

PyObject* PyUnicodeEncodeError_Create(const char *encoding, const Py_UNICODE *object, Py_ssize_t length, Py_ssize_t start, Py_ssize_t end, const char *reason)

Create a UnicodeEncodeError object with the attributes encoding, object, length, start, end and reason. encoding and reason are UTF-8 encoded strings.

PyObject* PyUnicodeTranslateError_Create(const Py_UNICODE *object, Py_ssize_t length, Py_ssize_t start, Py_ssize_t end, const char *reason)

Create a UnicodeTranslateError object with the attributes object, length, start, end and reason. reason is a UTF-8 encoded string.

PyObject* PyUnicodeDecodeError_GetEncoding(PyObject *exc)

PyObject* PyUnicodeEncodeError_GetEncoding(PyObject *exc)

Return the encoding attribute of the given exception object.

PyObject* PyUnicodeDecodeError_GetObject(PyObject *exc)

PyObject* PyUnicodeEncodeError_GetObject(PyObject *exc)

PyObject* PyUnicodeTranslateError_GetObject(PyObject *exc)

Return the object attribute of the given exception object.

int PyUnicodeDecodeError_GetStart(PyObject *exc, Py_ssize_t *start)

int PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)

int PyUnicodeTranslateError_GetStart(PyObject *exc, Py_ssize_t *start)

Get the start attribute of the given exception object and place it into *start. start must not be NULL. Return 0 on success, -1 on failure.

int PyUnicodeDecodeError_SetStart(PyObject *exc, Py_ssize_t start)

int PyUnicodeEncodeError_SetStart(PyObject *exc, Py_ssize_t start)

int PyUnicodeTranslateError_SetStart(PyObject *exc, Py_ssize_t start)

Set the start attribute of the given exception object to start. Return 0 on success, -1 on failure.

int PyUnicodeDecodeError_GetEnd(PyObject *exc, Py_ssize_t *end)

int PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)

int PyUnicodeTranslateError_GetEnd(PyObject *exc, Py_ssize_t *end)

Get the end attribute of the given exception object and place it into *end. end must not be NULL. Return 0 on success, -1 on failure.

int PyUnicodeDecodeError_SetEnd(PyObject *exc, Py_ssize_t end)

int PyUnicodeEncodeError_SetEnd(PyObject *exc, Py_ssize_t end)

int PyUnicodeTranslateError_SetEnd(PyObject *exc, Py_ssize_t end)

Set the end attribute of the given exception object to end. Return 0 on success, -1 on failure.

PyObject* PyUnicodeDecodeError_GetReason(PyObject *exc)

PyObject* PyUnicodeEncodeError_GetReason(PyObject *exc)

PyObject* PyUnicodeTranslateError_GetReason(PyObject *exc)

Return the reason attribute of the given exception object.

int PyUnicodeDecodeError_SetReason(PyObject *exc, const char *reason)

int PyUnicodeEncodeError_SetReason(PyObject *exc, const char *reason)

int PyUnicodeTranslateError_SetReason(PyObject *exc, const char *reason)

Set the reason attribute of the given exception object to reason. Return 0 on success, -1 on failure.

递归控制

These two functions provide a way to perform safe recursive calls at the C level, both in the core and in extension modules. They are needed if the recursive code does not necessarily invoke Python code (which tracks its recursion depth automatically).

int Py_EnterRecursiveCall(const char *where)

Marks a point where a recursive C-level call is about to be performed.

If USE_STACKCHECK is defined, this function checks if the OS stack overflowed using PyOS_CheckStack(). In this is the case, it sets a MemoryError and returns a nonzero value.

The function then checks if the recursion limit is reached. If this is the case, a RecursionError is set and a nonzero value is returned. Otherwise, zero is returned.

where should be a string such as " in instance check" to be concatenated to the RecursionError message caused by the recursion depth limit.

void Py_LeaveRecursiveCall()

Ends a Py_EnterRecursiveCall(). Must be called once for each successful invocation of Py_EnterRecursiveCall().

Properly implementing tp_repr for container types requires special recursion handling. In addition to protecting the stack, tp_repr also needs to track objects to prevent cycles. The following two functions facilitate this functionality. Effectively, these are the C equivalent to reprlib.recursive_repr().

int Py_ReprEnter(PyObject *object)

Called at the beginning of the tp_repr implementation to detect cycles.

If the object has already been processed, the function returns a positive integer. In that case the tp_repr implementation should return a string object indicating a cycle. As examples, dict objects return {...} and list objects return [...].

The function will return a negative integer if the recursion limit is reached. In that case the tp_repr implementation should typically return NULL.

Otherwise, the function returns zero and the tp_repr implementation can continue normally.

void Py_ReprLeave(PyObject *object)

Ends a Py_ReprEnter(). Must be called once for each invocation of Py_ReprEnter() that returns zero.

标准异常

All standard Python exceptions are available as global variables whose names are PyExc_ followed by the Python exception name. These have the type PyObject*; they are all class objects. For completeness, here are all the variables:

C 名称

Python 名称

注释

PyExc_BaseException

BaseException

(1)

PyExc_Exception

Exception

(1)

PyExc_ArithmeticError

ArithmeticError

(1)

PyExc_AssertionError

AssertionError

PyExc_AttributeError

AttributeError

PyExc_BlockingIOError

BlockingIOError

PyExc_BrokenPipeError

BrokenPipeError

PyExc_BufferError

BufferError

PyExc_ChildProcessError

ChildProcessError

PyExc_ConnectionAbortedError

ConnectionAbortedError

PyExc_ConnectionError

ConnectionError

PyExc_ConnectionRefusedError

ConnectionRefusedError

PyExc_ConnectionResetError

ConnectionResetError

PyExc_EOFError

EOFError

PyExc_FileExistsError

FileExistsError

PyExc_FileNotFoundError

FileNotFoundError

PyExc_FloatingPointError

FloatingPointError

PyExc_GeneratorExit

GeneratorExit

PyExc_ImportError

ImportError

PyExc_IndentationError

IndentationError

PyExc_IndexError

IndexError

PyExc_InterruptedError

InterruptedError

PyExc_IsADirectoryError

IsADirectoryError

PyExc_KeyError

KeyError

PyExc_KeyboardInterrupt

KeyboardInterrupt

PyExc_LookupError

LookupError

(1)

PyExc_MemoryError

MemoryError

PyExc_ModuleNotFoundError

ModuleNotFoundError

PyExc_NameError

NameError

PyExc_NotADirectoryError

NotADirectoryError

PyExc_NotImplementedError

NotImplementedError

PyExc_OSError

OSError

(1)

PyExc_OverflowError

OverflowError

PyExc_PermissionError

PermissionError

PyExc_ProcessLookupError

ProcessLookupError

PyExc_RecursionError

RecursionError

PyExc_ReferenceError

ReferenceError

(2)

PyExc_RuntimeError

RuntimeError

PyExc_StopAsyncIteration

StopAsyncIteration

PyExc_StopIteration

StopIteration

PyExc_SyntaxError

SyntaxError

PyExc_SystemError

SystemError

PyExc_SystemExit

SystemExit

PyExc_TabError

TabError

PyExc_TimeoutError

TimeoutError

PyExc_TypeError

TypeError

PyExc_UnboundLocalError

UnboundLocalError

PyExc_UnicodeDecodeError

UnicodeDecodeError

PyExc_UnicodeEncodeError

UnicodeEncodeError

PyExc_UnicodeError

UnicodeError

PyExc_UnicodeTranslateError

UnicodeTranslateError

PyExc_ValueError

ValueError

PyExc_ZeroDivisionError

ZeroDivisionError

3.3 新版功能: PyExc_BlockingIOError, PyExc_BrokenPipeError, PyExc_ChildProcessError, PyExc_ConnectionError, PyExc_ConnectionAbortedError, PyExc_ConnectionRefusedError, PyExc_ConnectionResetError, PyExc_FileExistsError, PyExc_FileNotFoundError, PyExc_InterruptedError, PyExc_IsADirectoryError, PyExc_NotADirectoryError, PyExc_PermissionError, PyExc_ProcessLookupError and PyExc_TimeoutError 介绍如下 PEP 3151 [https://www.python.org/dev/peps/pep-3151].

3.5 新版功能: PyExc_StopAsyncIteration 和 PyExc_RecursionError.

3.6 新版功能: PyExc_ModuleNotFoundError.

这些是兼容性别名 PyExc_OSError:

C 名称

注释

PyExc_EnvironmentError

PyExc_IOError

PyExc_WindowsError

(3)

在 3.3 版更改: 这些别名曾经是单独的异常类型。

注释:

这是其他标准异常的基类。

This is the same as weakref.ReferenceError.

Only defined on Windows; protect code that uses this by testing that the preprocessor macro MS_WINDOWS is defined.

标准警告类别

All standard Python warning categories are available as global variables whose names are PyExc_ followed by the Python exception name. These have the type PyObject*; they are all class objects. For completeness, here are all the variables:

C 名称

Python 名称

注释

PyExc_Warning

Warning

(1)

PyExc_BytesWarning

BytesWarning

PyExc_DeprecationWarning

DeprecationWarning

PyExc_FutureWarning

FutureWarning

PyExc_ImportWarning

ImportWarning

PyExc_PendingDeprecationWarning

PendingDeprecationWarning

PyExc_ResourceWarning

ResourceWarning

PyExc_RuntimeWarning

RuntimeWarning

PyExc_SyntaxWarning

SyntaxWarning

PyExc_UnicodeWarning

UnicodeWarning

PyExc_UserWarning

UserWarning

3.2 新版功能: PyExc_ResourceWarning.

注释:

这是其他标准警告类别的基类。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

工具

本章中的函数执行各种实用工具任务，包括帮助 C 代码提升跨平台可移植性，在 C 中使用 Python 模块，以及解析函数参数并根据 C 中的值构建 Python 中的值等等。

操作系统实用程序

系统功能

过程控制

导入模块

数据 marshal 操作支持

解析参数并构建值变量

解析参数

字符串和缓存区

数字

其他对象

API 函数

创建变量

字符串转换与格式化

反射

编解码器注册与支持功能

Codec 查找API

用于Unicode编码错误处理程序的注册表API

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

工具 »

|

操作系统实用程序

PyObject* PyOS_FSPath(PyObject *path)

Return value: New reference.

Return the file system representation for path. If the object is a str or bytes object, then its reference count is incremented. If the object implements the os.PathLike interface, then __fspath__() is returned as long as it is a str or bytes object. Otherwise TypeError is raised and NULL is returned.

3.6 新版功能.

int Py_FdIsInteractive(FILE *fp, const char *filename)

Return true (nonzero) if the standard I/O file fp with name filename is deemed interactive. This is the case for files for which isatty(fileno(fp)) is true. If the global flag Py_InteractiveFlag is true, this function also returns true if the filename pointer is NULL or if the name is equal to one of the strings '<stdin>' or '???'.

void PyOS_AfterFork()

Function to update some internal state after a process fork; this should be called in the new process if the Python interpreter will continue to be used. If a new executable is loaded into the new process, this function does not need to be called.

int PyOS_CheckStack()

Return true when the interpreter runs out of stack space. This is a reliable check, but is only available when USE_STACKCHECK is defined (currently on Windows using the Microsoft Visual C++ compiler). USE_STACKCHECK will be defined automatically; you should never change the definition in your own code.

PyOS_sighandler_t PyOS_getsig(int i)

Return the current signal handler for signal i. This is a thin wrapper around either sigaction() or signal(). Do not call those functions directly! PyOS_sighandler_t is a typedef alias for void (*)(int).

PyOS_sighandler_t PyOS_setsig(int i, PyOS_sighandler_t h)

Set the signal handler for signal i to be h; return the old signal handler. This is a thin wrapper around either sigaction() or signal(). Do not call those functions directly! PyOS_sighandler_t is a typedef alias for void (*)(int).

wchar_t* Py_DecodeLocale(const char* arg, size_t *size)

Decode a byte string from the locale encoding with the surrogateescape error handler: undecodable bytes are decoded as characters in range U+DC80..U+DCFF. If a byte sequence can be decoded as a surrogate character, escape the bytes using the surrogateescape error handler instead of decoding them.

Encoding, highest priority to lowest priority:

UTF-8 在 macOS 和 Android上;

ASCII if the LC_CTYPE locale is "C", nl_langinfo(CODESET) returns the ASCII encoding (or an alias), and mbstowcs() and wcstombs() functions use the ISO-8859-1 encoding.

the current locale encoding (LC_CTYPE locale).

Return a pointer to a newly allocated wide character string, use PyMem_RawFree() to free the memory. If size is not NULL, write the number of wide characters excluding the null character into *size.

Return NULL on decoding error or memory allocation error. If size is not NULL, *size is set to (size_t)-1 on memory error or set to (size_t)-2 on decoding error.

Decoding errors should never happen, unless there is a bug in the C library.

Use the Py_EncodeLocale() function to encode the character string back to a byte string.

参见

The PyUnicode_DecodeFSDefaultAndSize() and PyUnicode_DecodeLocaleAndSize() functions.

3.5 新版功能.

char* Py_EncodeLocale(const wchar_t *text, size_t *error_pos)

Encode a wide character string to the locale encoding with the surrogateescape error handler: surrogate characters in the range U+DC80..U+DCFF are converted to bytes 0x80..0xFF.

Encoding, highest priority to lowest priority:

UTF-8 在 macOS 和 Android上;

ASCII if the LC_CTYPE locale is "C", nl_langinfo(CODESET) returns the ASCII encoding (or an alias), and mbstowcs() and wcstombs() functions uses the ISO-8859-1 encoding.

the current locale encoding.

Return a pointer to a newly allocated byte string, use PyMem_Free() to free the memory. Return NULL on encoding error or memory allocation error

If error_pos is not NULL, *error_pos is set to the index of the invalid character on encoding error, or set to (size_t)-1 otherwise.

Use the Py_DecodeLocale() function to decode the bytes string back to a wide character string.

参见

The PyUnicode_EncodeFSDefault() and PyUnicode_EncodeLocale() functions.

3.5 新版功能.

系统功能

These are utility functions that make functionality from the sys module accessible to C code. They all work with the current interpreter thread’s sys module’s dict, which is contained in the internal thread state structure.

PyObject *PySys_GetObject(const char *name)

Return value: Borrowed reference.

Return the object name from the sys module or NULL if it does not exist, without setting an exception.

int PySys_SetObject(const char *name, PyObject *v)

Set name in the sys module to v unless v is NULL, in which case name is deleted from the sys module. Returns 0 on success, -1 on error.

void PySys_ResetWarnOptions()

Reset sys.warnoptions to an empty list.

void PySys_AddWarnOption(wchar_t *s)

Append s to sys.warnoptions.

void PySys_AddWarnOptionUnicode(PyObject *unicode)

Append unicode to sys.warnoptions.

void PySys_SetPath(wchar_t *path)

Set sys.path to a list object of paths found in path which should be a list of paths separated with the platform’s search path delimiter (: on Unix, ; on Windows).

void PySys_WriteStdout(const char *format, ...)

Write the output string described by format to sys.stdout. No exceptions are raised, even if truncation occurs (see below).

format should limit the total size of the formatted output string to 1000 bytes or less – after 1000 bytes, the output string is truncated. In particular, this means that no unrestricted “%s” formats should occur; these should be limited using “%.<N>s” where <N> is a decimal number calculated so that <N> plus the maximum size of other formatted text does not exceed 1000 bytes. Also watch out for “%f”, which can print hundreds of digits for very large numbers.

If a problem occurs, or sys.stdout is unset, the formatted message is written to the real (C level) stdout.

void PySys_WriteStderr(const char *format, ...)

As PySys_WriteStdout(), but write to sys.stderr or stderr instead.

void PySys_FormatStdout(const char *format, ...)

Function similar to PySys_WriteStdout() but format the message using PyUnicode_FromFormatV() and don’t truncate the message to an arbitrary length.

3.2 新版功能.

void PySys_FormatStderr(const char *format, ...)

As PySys_FormatStdout(), but write to sys.stderr or stderr instead.

3.2 新版功能.

void PySys_AddXOption(const wchar_t *s)

Parse s as a set of -X options and add them to the current options mapping as returned by PySys_GetXOptions().

3.2 新版功能.

PyObject *PySys_GetXOptions()

Return value: Borrowed reference.

Return the current dictionary of -X options, similarly to sys._xoptions. On error, NULL is returned and an exception is set.

3.2 新版功能.

过程控制

void Py_FatalError(const char *message)

Print a fatal error message and kill the process. No cleanup is performed. This function should only be invoked when a condition is detected that would make it dangerous to continue using the Python interpreter; e.g., when the object administration appears to be corrupted. On Unix, the standard C library function abort() is called which will attempt to produce a core file.

void Py_Exit(int status)

Exit the current process. This calls Py_FinalizeEx() and then calls the standard C library function exit(status). If Py_FinalizeEx() indicates an error, the exit status is set to 120.

在 3.6 版更改: Errors from finalization no longer ignored.

int Py_AtExit(void (*func)())

Register a cleanup function to be called by Py_FinalizeEx(). The cleanup function will be called with no arguments and should return no value. At most 32 cleanup functions can be registered. When the registration is successful, Py_AtExit() returns 0; on failure, it returns -1. The cleanup function registered last is called first. Each cleanup function will be called at most once. Since Python’s internal finalization will have completed before the cleanup function, no Python APIs should be called by func.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

工具 »

|

导入模块

PyObject* PyImport_ImportModule(const char *name)

Return value: New reference.

This is a simplified interface to PyImport_ImportModuleEx() below, leaving the globals and locals arguments set to NULL and level set to 0. When the name argument contains a dot (when it specifies a submodule of a package), the fromlist argument is set to the list ['*'] so that the return value is the named module rather than the top-level package containing it as would otherwise be the case. (Unfortunately, this has an additional side effect when name in fact specifies a subpackage instead of a submodule: the submodules specified in the package’s __all__ variable are loaded.) Return a new reference to the imported module, or NULL with an exception set on failure. A failing import of a module doesn’t leave the module in sys.modules.

该函数总是使用绝对路径导入。

PyObject* PyImport_ImportModuleNoBlock(const char *name)

该函数是 PyImport_ImportModule() 的一个被遗弃的别名。

在 3.3 版更改: This function used to fail immediately when the import lock was held by another thread. In Python 3.3 though, the locking scheme switched to per-module locks for most purposes, so this function’s special behaviour isn’t needed anymore.

PyObject* PyImport_ImportModuleEx(const char *name, PyObject *globals, PyObject *locals, PyObject *fromlist)

Return value: New reference.

Import a module. This is best described by referring to the built-in Python function __import__().

The return value is a new reference to the imported module or top-level package, or NULL with an exception set on failure. Like for __import__(), the return value when a submodule of a package was requested is normally the top-level package, unless a non-empty fromlist was given.

Failing imports remove incomplete module objects, like with PyImport_ImportModule().

PyObject* PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)

Return value: New reference.

Import a module. This is best described by referring to the built-in Python function __import__(), as the standard __import__() function calls this function directly.

The return value is a new reference to the imported module or top-level package, or NULL with an exception set on failure. Like for __import__(), the return value when a submodule of a package was requested is normally the top-level package, unless a non-empty fromlist was given.

3.3 新版功能.

PyObject* PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals, PyObject *fromlist, int level)

Return value: New reference.

Similar to PyImport_ImportModuleLevelObject(), but the name is a UTF-8 encoded string instead of a Unicode object.

在 3.3 版更改: Negative values for level are no longer accepted.

PyObject* PyImport_Import(PyObject *name)

Return value: New reference.

This is a higher-level interface that calls the current “import hook function” (with an explicit level of 0, meaning absolute import). It invokes the __import__() function from the __builtins__ of the current globals. This means that the import is done using whatever import hooks are installed in the current environment.

该函数总是使用绝对路径导入。

PyObject* PyImport_ReloadModule(PyObject *m)

Return value: New reference.

Reload a module. Return a new reference to the reloaded module, or NULL with an exception set on failure (the module still exists in this case).

PyObject* PyImport_AddModuleObject(PyObject *name)

Return value: Borrowed reference.

Return the module object corresponding to a module name. The name argument may be of the form package.module. First check the modules dictionary if there’s one there, and if not, create a new one and insert it in the modules dictionary. Return NULL with an exception set on failure.

注解

This function does not load or import the module; if the module wasn’t already loaded, you will get an empty module object. Use PyImport_ImportModule() or one of its variants to import a module. Package structures implied by a dotted name for name are not created if not already present.

3.3 新版功能.

PyObject* PyImport_AddModule(const char *name)

Return value: Borrowed reference.

Similar to PyImport_AddModuleObject(), but the name is a UTF-8 encoded string instead of a Unicode object.

PyObject* PyImport_ExecCodeModule(const char *name, PyObject *co)

Return value: New reference.

Given a module name (possibly of the form package.module) and a code object read from a Python bytecode file or obtained from the built-in function compile(), load the module. Return a new reference to the module object, or NULL with an exception set if an error occurred. name is removed from sys.modules in error cases, even if name was already in sys.modules on entry to PyImport_ExecCodeModule(). Leaving incompletely initialized modules in sys.modules is dangerous, as imports of such modules have no way to know that the module object is an unknown (and probably damaged with respect to the module author’s intents) state.

The module’s __spec__ and __loader__ will be set, if not set already, with the appropriate values. The spec’s loader will be set to the module’s __loader__ (if set) and to an instance of SourceFileLoader otherwise.

The module’s __file__ attribute will be set to the code object’s co_filename. If applicable, __cached__ will also be set.

This function will reload the module if it was already imported. See PyImport_ReloadModule() for the intended way to reload a module.

If name points to a dotted name of the form package.module, any package structures not already created will still not be created.

See also PyImport_ExecCodeModuleEx() and PyImport_ExecCodeModuleWithPathnames().

PyObject* PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)

Return value: New reference.

Like PyImport_ExecCodeModule(), but the __file__ attribute of the module object is set to pathname if it is non-NULL.

参见 PyImport_ExecCodeModuleWithPathnames()。

PyObject* PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, PyObject *cpathname)

Return value: New reference.

Like PyImport_ExecCodeModuleEx(), but the __cached__ attribute of the module object is set to cpathname if it is non-NULL. Of the three functions, this is the preferred one to use.

3.3 新版功能.

PyObject* PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, const char *pathname, const char *cpathname)

Return value: New reference.

Like PyImport_ExecCodeModuleObject(), but name, pathname and cpathname are UTF-8 encoded strings. Attempts are also made to figure out what the value for pathname should be from cpathname if the former is set to NULL.

3.2 新版功能.

在 3.3 版更改: Uses imp.source_from_cache() in calculating the source path if only the bytecode path is provided.

long PyImport_GetMagicNumber()

Return the magic number for Python bytecode files (a.k.a. .pyc file). The magic number should be present in the first four bytes of the bytecode file, in little-endian byte order. Returns -1 on error.

在 3.3 版更改: 失败时返回值 -1。

const char * PyImport_GetMagicTag()

Return the magic tag string for PEP 3147 [https://www.python.org/dev/peps/pep-3147] format Python bytecode file names. Keep in mind that the value at sys.implementation.cache_tag is authoritative and should be used instead of this function.

3.2 新版功能.

PyObject* PyImport_GetModuleDict()

Return value: Borrowed reference.

Return the dictionary used for the module administration (a.k.a. sys.modules). Note that this is a per-interpreter variable.

PyObject* PyImport_GetImporter(PyObject *path)

Return value: New reference.

Return a finder object for a sys.path/pkg.__path__ item path, possibly by fetching it from the sys.path_importer_cache dict. If it wasn’t yet cached, traverse sys.path_hooks until a hook is found that can handle the path item. Return None if no hook could; this tells our caller that the path based finder could not find a finder for this path item. Cache the result in sys.path_importer_cache. Return a new reference to the finder object.

void _PyImport_Init()

Initialize the import mechanism. For internal use only.

void PyImport_Cleanup()

Empty the module table. For internal use only.

void _PyImport_Fini()

Finalize the import mechanism. For internal use only.

PyObject* _PyImport_FindExtension(char *, char *)

For internal use only.

int PyImport_ImportFrozenModuleObject(PyObject *name)

Return value: New reference.

Load a frozen module named name. Return 1 for success, 0 if the module is not found, and -1 with an exception set if the initialization failed. To access the imported module on a successful load, use PyImport_ImportModule(). (Note the misnomer — this function would reload the module if it was already imported.)

3.3 新版功能.

在 3.4 版更改: The __file__ attribute is no longer set on the module.

int PyImport_ImportFrozenModule(const char *name)

Similar to PyImport_ImportFrozenModuleObject(), but the name is a UTF-8 encoded string instead of a Unicode object.

struct _frozen

This is the structure type definition for frozen module descriptors, as generated by the freeze utility (see Tools/freeze/ in the Python source distribution). Its definition, found in Include/import.h, is:

struct _frozen {
    char *name;
    unsigned char *code;
    int size;
};

const struct _frozen* PyImport_FrozenModules

This pointer is initialized to point to an array of struct _frozen records, terminated by one whose members are all NULL or zero. When a frozen module is imported, it is searched in this table. Third-party code could play tricks with this to provide a dynamically created collection of frozen modules.

int PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))

Add a single module to the existing table of built-in modules. This is a convenience wrapper around PyImport_ExtendInittab(), returning -1 if the table could not be extended. The new module can be imported by the name name, and uses the function initfunc as the initialization function called on the first attempted import. This should be called before Py_Initialize().

struct _inittab

Structure describing a single entry in the list of built-in modules. Each of these structures gives the name and initialization function for a module built into the interpreter. The name is an ASCII encoded string. Programs which embed Python may use an array of these structures in conjunction with PyImport_ExtendInittab() to provide additional built-in modules. The structure is defined in Include/import.h as:

struct _inittab {
    char *name;                 /* ASCII encoded string */
    PyObject* (*initfunc)(void);
};

int PyImport_ExtendInittab(struct _inittab *newtab)

Add a collection of modules to the table of built-in modules. The newtab array must end with a sentinel entry which contains NULL for the name field; failure to provide the sentinel value can result in a memory fault. Returns 0 on success or -1 if insufficient memory could be allocated to extend the internal table. In the event of failure, no modules are added to the internal table. This should be called before Py_Initialize().

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

工具 »

|

数据 marshal 操作支持

这些例程允许 C 代码处理与 marshal 模块所用相同数据格式的序列化对象。 其中有些函数可用来将数据写入这种序列化格式，另一些函数则可用来读取并恢复数据。 用于存储 marshal 数据的文件必须以二进制模式打开。

数字值在存储时会将最低位字节放在开头。

The module supports two versions of the data format: version 0 is the historical version, version 1 shares interned strings in the file, and upon unmarshalling. Version 2 uses a binary format for floating point numbers. Py_MARSHAL_VERSION indicates the current file format (currently 2).

void PyMarshal_WriteLongToFile(long value, FILE *file, int version)

将一个 long 整数 value 以 marshal 格式写入 file。 这将只写入 value 最低的 32 位；无论本机 long 类型的长度如何。 version 指明文件格式的版本。

void PyMarshal_WriteObjectToFile(PyObject *value, FILE *file, int version)

将一个 Python 对象 value 以 marshal 格式写入 file。 version 指明文件格式的版本。

PyObject* PyMarshal_WriteObjectToString(PyObject *value, int version)

Return value: New reference.

返回一个包含 value 的 marshal 表示形式的字节串对象。 version 指明文件格式的版本。

以下函数允许读取并恢复存储为 marshal 格式的值。

long PyMarshal_ReadLongFromFile(FILE *file)

从打开用于读取的 FILE* 的对应数据流返回一个 C long。 使用此函数只能读取 32 位的值，无论本机 long 类型的长度如何。

发生错误时，将设置适当的异常 (EOFError) 并返回 -1。

int PyMarshal_ReadShortFromFile(FILE *file)

从打开用于读取的 FILE* 的对应数据流返回一个 C short。 使用此函数只能读取 16 位的值，无论本机 short 的长度如何。

发生错误时，将设置适当的异常 (EOFError) 并返回 -1。

PyObject* PyMarshal_ReadObjectFromFile(FILE *file)

Return value: New reference.

从打开用于读取的 FILE* 的对应数据流返回一个 Python 对象。

On error, sets the appropriate exception (EOFError, ValueError or TypeError) and returns NULL.

PyObject* PyMarshal_ReadLastObjectFromFile(FILE *file)

Return value: New reference.

从打开用于读取的 FILE* 的对应数据流返回一个 Python 对象。 不同于 PyMarshal_ReadObjectFromFile()，此函数假定将不再从该文件读取更多的对象，允许其将文件数据积极地载入内存，以便反序列化过程可以在内存中的数据上操作而不是每次从文件读取一个字节。 只有当你确定不会再从文件读取任何内容时方可使用此方式。

On error, sets the appropriate exception (EOFError, ValueError or TypeError) and returns NULL.

PyObject* PyMarshal_ReadObjectFromString(const char *data, Py_ssize_t len)

Return value: New reference.

从包含指向 data 的 len 个字节的字节缓冲区对应的数据流返回一个 Python 对象。

On error, sets the appropriate exception (EOFError, ValueError or TypeError) and returns NULL.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

工具 »

|

解析参数并构建值变量

在创建你自己的扩展函数和方法时，这些函数是有用的。其它的信息和样例见 扩展和嵌入 Python 解释器 。

这些函数描述的前三个，PyArg_ParseTuple()，PyArg_ParseTupleAndKeywords()，以及 PyArg_Parse()，它们都使用 格式化字符串 来将函数期待的参数告知函数。这些函数都使用相同语法规则的格式化字符串。

解析参数

一个格式化字符串包含0或者更多的格式单元。一个格式单元用来描述一个Python对象；它通常是一个字符或者由括号括起来的格式单元序列。除了少数例外，一个非括号序列的格式单元通常对应这些函数的具有单一地址的参数。在接下来的描述中，双引号内的表达式是格式单元；圆括号()内的是对应这个格式单元的Python对象类型；方括号[]内的是传递的C变量(变量集)类型。

字符串和缓存区

这些格式允许将对象按照连续的内存块形式进行访问。你没必要提供返回的unicode字符或者字节区的原始数据存储。

一般的，当一个表达式设置一个指针指向一个缓冲区，这个缓冲区可以被相应的Python对象管理，并且这个缓冲区共享这个对象的生存周期。你不需要人为的释放任何内存空间。除了这些 es, es#, et and et#.

然而，当一个 Py_buffer 结构被赋值，其包含的缓冲区被锁住，所以调用者在随后使用这个缓冲区，即使在 Py_BEGIN_ALLOW_THREADS 块中，可以避免可变数据因为调整大小或者被销毁所带来的风险。因此，你不得不调用 PyBuffer_Release() 在你结束数据的处理时(或者在之前任何中断事件中)

除非另有说明，缓冲区是不会以空终止的。

Some formats require a read-only bytes-like object, and set a pointer instead of a buffer structure. They work by checking that the object’s PyBufferProcs.bf_releasebuffer field is NULL, which disallows mutable objects such as bytearray.

注解

所有 # 表达式的变式(s#，y#，等等)，长度参数的类型(整型或者 Py_ssize_t)在包含 Python.h 头文件之前由 PY_SSIZE_T_CLEAN 宏的定义控制。如果这个宏被定义，长度是一个 Py_ssize_t Python元大小类型而不是一个 int 整型。在未来的Python版本中将会改变，只支持 Py_ssize_t 而放弃支持 int 整型。最好一直定义 PY_SSIZE_T_CLEAN 这个宏。

s (str) [const char *]

将一个Unicode对象转换成一个指向字符串的C指针。一个指针指向一个已经存在的字符串，这个字符串存储的是传如的字符指针变量。C字符串是已空结束的。Python字符串不能包含嵌入的无效的代码点；如果由，一个 ValueError 异常会被引发。Unicode对象被转化成 'utf-8' 编码的C字符串。如果转换失败，一个 UnicodeError 异常被引发。

注解

这个表达式不接受 bytes-like objects。如果你想接受文件系统路径并将它们转化成C字符串，建议使用 O& 表达式配合 PyUnicode_FSConverter() 作为 转化函数。

在 3.5 版更改: 以前，当Python字符串中遇到了嵌入的null代码点会引发 TypeError 。

s* (str or bytes-like object) [Py_buffer]

这个表达式既接受Unicode对象也接受类字节类型对象。它为由调用者提供的 Py_buffer 结构赋值。这里结果的C字符串可能包含嵌入的NUL字节。Unicode对象通过 'utf-8' 编码转化成C字符串。

s# (str, 只读 bytes-like object) [const char *, int or Py_ssize_t]

像 s*，除了它不接受易变的对象。结果存储在两个C 变量中，第一个是指向C字符串的指针，第二个是它的长度。字符串可能包含嵌入的null字节。Unicode对象都被通过 'utf-8' 编码转化成C字符串。

z (str or None) [const char *]

Like s, but the Python object may also be None, in which case the C pointer is set to NULL.

z* (str, bytes-like object or None) [Py_buffer]

Like s*, but the Python object may also be None, in which case the buf member of the Py_buffer structure is set to NULL.

z# (str, read-only bytes-like object or None) [const char *, int]

Like s#, but the Python object may also be None, in which case the C pointer is set to NULL.

y (read-only bytes-like object) [const char *]

这个表达式将一个类字节类型对象转化成一个指向字符串的C指针；它不接受Unicode对象。字节缓存区必须不包含嵌入的null字节；如果包含了null字节，会引发一个 ValueError 异常。

在 3.5 版更改: 以前，当字节缓冲区中遇到了嵌入的null字节会引发 TypeError 。

y* (bytes-like object) [Py_buffer]

s* 的变式，不接受Unicode对象，只接受类字节类型变量。这是接受二进制数据的推荐方法。

y# (read-only bytes-like object) [const char *, int]

This variant on s# doesn’t accept Unicode objects, only bytes-like objects.

S (bytes) [PyBytesObject *]

要求Python对象是一个 bytes 类型对象，没有尝试任何的转换。如果不是一个字节类型对象会引发 TypeError 异常。C变量也可能声明为 PyObject* 类型。

Y (bytearray) [PyByteArrayObject *]

要求Python对象是一个 bytearray 类型对象，没有尝试任何的转换。如果不是一个 bytearray 类型对象会引发 TypeError 异常。C变量也可能声明为 PyObject* 类型。

u (str) [Py_UNICODE *]

将一个Python Unicode对象转化成指向一个以空终止的Unicode字符缓冲区的指针。你必须传入一个 Py_UNICODE 指针变量的地址，存储了一个指向已经存在的Unicode缓冲区的指针。请注意一个 Py_UNICODE 类型的字符宽度取决于编译选项(16位或者32位)。Python字符串必须不能包含嵌入的null代码点；如果有，引发一个 ValueError 异常。

在 3.5 版更改: 以前，当Python字符串中遇到了嵌入的null代码点会引发 TypeError 。

Deprecated since version 3.3, will be removed in version 4.0: 这是旧版样式 Py_UNICODE API; 请迁移至 PyUnicode_AsWideCharString().

u# (str) [Py_UNICODE *, int]

u 的变式，存储两个C变量，第一个指针指向一个Unicode数据缓存区，第二个是它的长度。它允许null代码点。

Deprecated since version 3.3, will be removed in version 4.0: 这是旧版样式 Py_UNICODE API; 请迁移至 PyUnicode_AsWideCharString().

Z (str or None) [Py_UNICODE *]

Like u, but the Python object may also be None, in which case the Py_UNICODE pointer is set to NULL.

Deprecated since version 3.3, will be removed in version 4.0: 这是旧版样式 Py_UNICODE API; 请迁移至 PyUnicode_AsWideCharString().

Z# (str or None) [Py_UNICODE *, int]

Like u#, but the Python object may also be None, in which case the Py_UNICODE pointer is set to NULL.

Deprecated since version 3.3, will be removed in version 4.0: 这是旧版样式 Py_UNICODE API; 请迁移至 PyUnicode_AsWideCharString().

U (str) [PyObject *]

要求Python对象是一个Unicode对象，没有尝试任何的转换。如果不是一个Unicode对象会引发 TypeError 异常。C变量也可能声明为 PyObject* 类型。

w* (可读写 bytes-like object) [Py_buffer]

这个表达式接受任何实现可读写缓存区接口的对象。它为调用者提供的 Py_buffer 结构赋值。缓冲区可能存在嵌入的null字节。当缓冲区使用完后调用者需要调用 PyBuffer_Release()。

es (str) [const char *encoding, char **buffer]

s 的变式，它将编码后的Unicode字符存入字符缓冲区。它只处理没有嵌入NUL字节的已编码数据

This format requires two arguments. The first is only used as input, and must be a const char* which points to the name of an encoding as a NUL-terminated string, or NULL, in which case 'utf-8' encoding is used. An exception is raised if the named encoding is not known to Python. The second argument must be a char**; the value of the pointer it references will be set to a buffer with the contents of the argument text. The text will be encoded in the encoding specified by the first argument.

PyArg_ParseTuple() 会分配一个足够大小的缓冲区，将编码后的数据拷贝进这个缓冲区并且设置 *buffer 引用这个新分配的内存空间。调用者有责任在使用后调用 PyMem_Free() 去释放已经分配的缓冲区。

et (str, bytes or bytearray) [const char *encoding, char **buffer]

和 es 相同，除了不用重编码传入的字符串对象。相反，它假设传入的参数是编码后的字符串类型。

es# (str) [const char *encoding, char **buffer, int *buffer_length]

s# 的变式，它将已编码的Unicode字符存入字符缓冲区。不像 es 表达式，它允许传入的数据包含NUL字符。

It requires three arguments. The first is only used as input, and must be a const char* which points to the name of an encoding as a NUL-terminated string, or NULL, in which case 'utf-8' encoding is used. An exception is raised if the named encoding is not known to Python. The second argument must be a char**; the value of the pointer it references will be set to a buffer with the contents of the argument text. The text will be encoded in the encoding specified by the first argument. The third argument must be a pointer to an integer; the referenced integer will be set to the number of bytes in the output buffer.

有两种操作方式：

If *buffer points a NULL pointer, the function will allocate a buffer of the needed size, copy the encoded data into this buffer and set *buffer to reference the newly allocated storage. The caller is responsible for calling PyMem_Free() to free the allocated buffer after usage.

If *buffer points to a non-NULL pointer (an already allocated buffer), PyArg_ParseTuple() will use this location as the buffer and interpret the initial value of *buffer_length as the buffer size. It will then copy the encoded data into the buffer and NUL-terminate it. If the buffer is not large enough, a ValueError will be set.

在这两个例子中，*buffer_length 被设置为编码后结尾不为NUL的数据的长度。

et# (str, bytes or bytearray) [const char *encoding, char **buffer, int *buffer_length]

和 es# 相同，除了不用重编码传入的字符串对象。相反，它假设传入的参数是编码后的字符串类型。

数字

b (int) [unsigned char]

将一个非负的Python整型转化成一个无符号的微整型，存储在一个C unsigned char 类型中。

B (int) [unsigned char]

将一个Python整型转化成一个微整型并不检查溢出问题，存储在一个C unsigned char 类型中。

h (int) [short int]

将一个Python整型转化成一个C short int 短整型。

H (int) [unsigned short int]

将一个Python整型转化成一个C unsigned short int 无符号短整型，并不检查溢出问题。

i (int) [int]

将一个Python整型转化成一个C int 整型。

I (int) [unsigned int]

将一个Python整型转化成一个C unsigned int 无符号整型，并不检查溢出问题。

l (int) [long int]

将一个Python整型转化成一个C long int 长整型。

k (int) [unsigned long]

将一个Python整型转化成一个C unsigned long int 无符号长整型，并不检查溢出问题。

L (int) [long long]

将一个Python整型转化成一个C long long 长长整型。

K (int) [unsigned long long]

将一个Python整型转化成一个C unsigned long long 无符号长长整型，并不检查溢出问题。

n (int) [Py_ssize_t]

将一个Python整型转化成一个C Py_ssize_t Python元大小类型。

c (bytes 或者 bytearray 长度为1) [char]

将一个Python字节类型，如一个长度为1的 bytes 或者 bytearray 对象，转化成一个C char 字符类型。

在 3.3 版更改: 允许 bytearray 类型的对象。

C (str 长度为1) [int]

将一个Python字符，如一个长度为1的 str 字符串对象，转化成一个C int 整型类型。

f (float) [float]

将一个Python浮点数转化成一个C float 浮点数。

d (float) [double]

将一个Python浮点数转化成一个C double 双精度浮点数。

D (complex) [Py_complex]

将一个Python复数类型转化成一个C Py_complex Python复数类型。

其他对象

O (object) [PyObject *]

Store a Python object (without any conversion) in a C object pointer. The C program thus receives the actual object that was passed. The object’s reference count is not increased. The pointer stored is not NULL.

O! (object) [typeobject, PyObject *]

将一个Python对象存入一个C指针。和 O 类似，但是需要两个C参数：第一个是Python类型对象的地址，第二个是存储对象指针的C变量( PyObject* 变量)的地址。如果Python对象类型不对，会抛出 TypeError 异常。

O& (object) [converter, anything]

通过一个 converter 函数将一个Python对象转换成一个C变量。这需要两个参数：第一个是一个函数，第二个是一个C变量的地址(任意类型的)，转化为 void * 类型。converter 函数像这样被调用：

status = converter(object, address);

object*是待转化的Python对象并且 *address 是传入 PyArg_Parse*() 函数的 void* 类型参数。返回的 status 是1代表转换成功，0代表转换失败。当转换失败，converter*函数会引发一个异常并且不会修改 *address 的内容。

If the converter returns Py_CLEANUP_SUPPORTED, it may get called a second time if the argument parsing eventually fails, giving the converter a chance to release any memory that it had already allocated. In this second call, the object parameter will be NULL; address will have the same value as in the original call.

在 3.1 版更改: Py_CLEANUP_SUPPORTED 被添加。

p (bool) [int]

测试传入的值是否为真(一个布尔判断)并且将结果转化为相对应的C true/false整型值。如果表达式为真置``1``，假则置``0``。它接受任何合法的Python值。参见 逻辑值检测 获取更多关于Python如何测试值为真的信息。

3.3 新版功能.

(items) (tuple) [matching-items]

对象必须是Python序列，它的长度是 items 中格式单元的数量。C参数必须对应 items 中每一个独立的格式单元。序列中的格式单元可能有嵌套。

传递 “长”整型(整型的值超过了平台的 LONG_MAX 限制)是可能的，然而没有进行适当的范围检测——当接收字段太小而接收不到值时，最重要的位被静默地截断(实际上，C语言会在语义继承的基础上强制类型转换——期望的值可能会发生变化)。

格式化字符串中还有一些其他的字符具有特殊的涵义。这些可能并不嵌套在圆括号中。它们是：

|

表明在Python参数列表中剩下的参数都是可选的。C变量对应的可选参数需要初始化为默认值——当一个可选参数没有指定时， PyArg_ParseTuple() 不能访问相应的C变量(变量集)的内容。

$

PyArg_ParseTupleAndKeywords() only：表明在Python参数列表中剩下的参数都是强制关键字参数。当前，所有强制关键字参数都必须也是可选参数，所以格式化字符串中 | 必须一直在 $ 前面。

3.3 新版功能.

:

格式单元的列表结束标志；冒号后的字符串被用来作为错误消息中的函数名(PyArg_ParseTuple() 函数引发的“关联值”异常)。

;

格式单元的列表结束标志；分号后的字符串被用来作为错误消息取代默认的错误消息。 : 和 ; 相互排斥。

注意任何由调用者提供的Python对象引用是 借来的 引用；不要递减它们的引用计数！

传递给这些函数的附加参数必须是由格式化字符串确定的变量的地址；这些都是用来存储输入元组的值。有一些情况，如上面的格式单元列表中所描述的，这些参数作为输入值使用；在这种情况下，它们应该匹配指定的相应的格式单元。

为了转换成功，arg 对象必须匹配格式并且格式必须用尽。成功的话，PyArg_Parse*() 函数返回true，反之它们返回false并且引发一个合适的异常。当 PyArg_Parse*() 函数因为某一个格式单元转化失败而失败时，对应的以及后续的格式单元地址内的变量都不会被使用。

API 函数

int PyArg_ParseTuple(PyObject *args, const char *format, ...)

解析一个函数的参数，表达式中的参数按参数位置顺序存入局部变量中。成功返回true；失败返回false并且引发相应的异常。

int PyArg_VaParse(PyObject *args, const char *format, va_list vargs)

和 PyArg_ParseTuple() 相同，然而它接受一个va_list类型的参数而不是可变数量的参数集。

int PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], ...)

Parse the parameters of a function that takes both positional and keyword parameters into local variables. The keywords argument is a NULL-terminated array of keyword parameter names. Empty names denote positional-only parameters. Returns true on success; on failure, it returns false and raises the appropriate exception.

在 3.6 版更改: 添加了 positional-only parameters 的支持。

int PyArg_VaParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, char *keywords[], va_list vargs)

和 PyArg_ParseTupleAndKeywords() 相同，然而它接受一个va_list类型的参数而不是可变数量的参数集。

int PyArg_ValidateKeywordArguments(PyObject *)

确保字典中的关键字参数都是字符串。这个函数只被使用于 PyArg_ParseTupleAndKeywords() 不被使用的情况下，后者已经不再做这样的检查。

3.2 新版功能.

int PyArg_Parse(PyObject *args, const char *format, ...)

函数被用来析构“旧类型”函数的参数列表——这些函数使用的 METH_OLDARGS 参数解析方法已从Python 3中移除。这不被推荐用于新代码的参数解析，并且在标准解释器中的大多数代码已被修改，已不再用于该目的。它仍然方便于分解其他元组，然而可能因为这个目的被继续使用。

int PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)

一个不使用格式化字符串指定参数类型的简单形式的参数检索。使用这种方法来检索参数的函数应该在函数或者方法表中声明 METH_VARARGS。包含实际参数的元组应该以 args 形式被传入；它必须是一个实际的元组。元组的长度必须至少是 min 并且不超过 max； min 和 max 可能相同。额外的参数必须传递给函数，每一个参数必须是一个指向 PyObject* 类型变量的指针；它们将被赋值为 args 的值；它们将包含借来的引用。不在 args 里面的可选参数不会被赋值；由调用者完成初始化。函数成功则返回true并且如果 args 不是元组或者包含错误数量的元素则返回false；如果失败了会引发一个异常。

这是一个使用此函数的示例，取自 _weakref 帮助模块用来弱化引用的源代码：

static PyObject *
weakref_ref(PyObject *self, PyObject *args)
{
    PyObject *object;
    PyObject *callback = NULL;
    PyObject *result = NULL;

    if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) {
        result = PyWeakref_NewRef(object, callback);
    }
    return result;
}

这个例子中调用 PyArg_UnpackTuple() 完全等价于调用 PyArg_ParseTuple():

PyArg_ParseTuple(args, "O|O:ref", &object, &callback)

创建变量

PyObject* Py_BuildValue(const char *format, ...)

Return value: New reference.

Create a new value based on a format string similar to those accepted by the PyArg_Parse*() family of functions and a sequence of values. Returns the value or NULL in the case of an error; an exception will be raised if NULL is returned.

Py_BuildValue() 并不一直创建一个元组。只有当它的格式化字符串包含两个或更多的格式单元才会创建一个元组。如果格式化字符串是空，它返回 None；如果它包含一个格式单元，它返回由格式单元描述的的任一对象。用圆括号包裹格式化字符串可以强制它返回一个大小为0或者1的元组

当内存缓存区的数据以参数形式传递用来构建对象时，如 s 和 s# 格式单元，会拷贝需要的数据。调用者提供的缓冲区从来都不会被由 Py_BuildValue() 创建的对象来引用。换句话说，如果你的代码调用 malloc() 并且将分配的内存空间传递给 Py_BuildValue()，你的代码就有责任在 Py_BuildValue() 返回时调用 free() 。

在下面的描述中，双引号的表达式使格式单元；圆括号()内的是格式单元将要返回的Python对象类型；方括号[]内的是传递的C变量(变量集)的类型

字符例如空格，制表符，冒号和逗号在格式化字符串中会被忽略(但是不包括格式单元，如 s#)。这可以使很长的格式化字符串具有更好的可读性。

s (str or None) [char *]

Convert a null-terminated C string to a Python str object using 'utf-8' encoding. If the C string pointer is NULL, None is used.

s# (str or None) [char *, int]

Convert a C string and its length to a Python str object using 'utf-8' encoding. If the C string pointer is NULL, the length is ignored and None is returned.

y (bytes) [char *]

This converts a C string to a Python bytes object. If the C string pointer is NULL, None is returned.

y# (bytes) [char *, int]

This converts a C string and its lengths to a Python object. If the C string pointer is NULL, None is returned.

z (str or None) [char *]

和``s``一样。

z# (str or None) [char *, int]

和``s#``一样。

u (str) [wchar_t *]

Convert a null-terminated wchar_t buffer of Unicode (UTF-16 or UCS-4) data to a Python Unicode object. If the Unicode buffer pointer is NULL, None is returned.

u# (str) [wchar_t *, int]

Convert a Unicode (UTF-16 or UCS-4) data buffer and its length to a Python Unicode object. If the Unicode buffer pointer is NULL, the length is ignored and None is returned.

U (str or None) [char *]

和``s``一样。

U# (str or None) [char *, int]

和``s#``一样。

i (int) [int]

将一个C int 整型转化成Python整型对象。

b (int) [char]

将一个C char 字符型转化成Python整型对象。

h (int) [short int]

将一个C short int 短整型转化成Python整型对象。

l (int) [long int]

将一个C long int 长整型转化成Python整型对象。

B (int) [unsigned char]

将一个C unsigned char 无符号字符型转化成Python整型对象。

H (int) [unsigned short int]

将一个C unsigned long 无符号短整型转化成Python整型对象。

I (int) [unsigned int]

将一个C unsigned long 无符号整型转化成Python整型对象。

k (int) [unsigned long]

将一个C unsigned long 无符号长整型转化成Python整型对象。

L (int) [long long]

将一个C long long 长长整形转化成Python整形对象。

K (int) [unsigned long long]

将一个C unsigned long long 无符号长长整型转化成Python整型对象。

n (int) [Py_ssize_t]

将一个C Py_ssize_t 类型转化为Python整型。

c (bytes 长度为1 ) [char]

将一个C int 整型代表的字符转化为Python bytes 长度为1的字节对象。

C (str 长度为1) [int]

将一个C int 整型代表的字符转化为Python str 长度为1的字符串对象。

d (float) [double]

将一个C double 双精度浮点数转化为Python浮点数类型数字。

f (float) [float]

将一个C float 单精度浮点数转化为Python浮点数类型数字。

D (复数) [Py_complex *]

将一个C Py_complex 类型的结构转化为Python复数类型。

O (object) [PyObject *]

Pass a Python object untouched (except for its reference count, which is incremented by one). If the object passed in is a NULL pointer, it is assumed that this was caused because the call producing the argument found an error and set an exception. Therefore, Py_BuildValue() will return NULL but won’t raise an exception. If no exception has been raised yet, SystemError is set.

S (object) [PyObject *]

和``O``相同。

N (object) [PyObject *]

和``O``相同，然而它并不增加对象的引用计数。当通过调用参数列表中的对象构造器创建对象时很实用。

O& (object) [converter, anything]

Convert anything to a Python object through a converter function. The function is called with anything (which should be compatible with void *) as its argument and should return a “new” Python object, or NULL if an error occurred.

(items) (tuple) [matching-items]

将一个C变量序列转换成Python元组并保持相同的元素数量

[items] (list) [相关的元素]

将一个C变量序列转换成Python列表并保持相同的元素数量

{items} (dict) [相关的元素]

将一个C变量序列转换成Python字典。每一对连续的C变量对作为一个元素插入字典中，分别作为关键字和值。

If there is an error in the format string, the SystemError exception is set and NULL returned.

PyObject* Py_VaBuildValue(const char *format, va_list vargs)

和 Py_BuildValue() 相同，然而它接受一个va_list类型的参数而不是可变数量的参数集。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

工具 »

|

字符串转换与格式化

用于数字转换和格式化字符串输出的函数

int PyOS_snprintf(char *str, size_t size, const char *format, ...)

根据格式字符串 format 和额外参数，输出不超过 size 字节到 str 。请参见Unix手册页 snprintf(2) 。

int PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)

根据格式字符串 format 和 变量参数列表 va ，不能输出超过 size 字节到 str 。请参见Unix手册页 vsnprintf(2) 。

PyOS_snprintf() and PyOS_vsnprintf() wrap the Standard C library functions snprintf() and vsnprintf(). Their purpose is to guarantee consistent behavior in corner cases, which the Standard C functions do not.

包装器确保 str*[*size-1] 在返回时始终是 '\0' 。它们从不写入超过 size 字节（包括结尾的 '\0' ）到字符串。两函数都需要满足 str != NULL , size > 0 和 format != NULL 。

If the platform doesn’t have vsnprintf() and the buffer size needed to avoid truncation exceeds size by more than 512 bytes, Python aborts with a Py_FatalError.

这些函数的返回值（ rv ）应按照以下规则被解释：

当 0 <= rv < size ，输出转换成功而且 rv 个字符被写入 str （不包含末尾 str*[*rv] 的 '\0' 字节）

当 rv >= size ，输出转换被截断并且成功需要一个带有 rv + 1 字节的缓冲区。在这种情况下， str*[*size-1] 的值是 '\0' 。

当 rv < 0 ，会发生一些不好的事情。 在这种情况下， str*[*size-1] 的值也是 '\0' ， 但是 str 的其余部分未被定义。错误的确切原因取决于底层平台。

以下函数提供与语言环境无关的字符串到数字转换。

double PyOS_string_to_double(const char *s, char **endptr, PyObject *overflow_exception)

将字符串 s 转换为 double 类型，失败时引发Python异常。接受的字符串的集合对应于被 Python 的 float() 构造函数接受的字符串的集合，除了 s 必须没有前导或尾随空格。转换必须独立于当前的区域。

If endptr is NULL, convert the whole string. Raise ValueError and return -1.0 if the string is not a valid representation of a floating-point number.

如果 endptr 不是 NULL ，尽可能多的转换字符串并将 *endptr 设置为指向第一个未转换的字符。如果字符串的初始段不是浮点数的有效的表达方式，将 * endptr 设置为指向字符串的开头，引发 ValueError 异常，并且返回 -1.0 。

如果 s 表示一个太大而不能存储在一个浮点数中的值（比方说， "1e500" 在许多平台上是一个字符串）然后如果 overflow_exception 是 NULL 返回 Py_HUGE_VAL （用适当的符号）并且不设置任何异常。 在其他方面， overflow_exception 必须指向一个 Python 异常对象；引发异常并返回 -1.0 。在这两种情况下，设置 *endptr 指向转换值之后的第一个字符。

如果在转换期间发生任何其他错误（比如一个内存不足的错误），设置适当的 Python 异常并且返回 -1.0 。

3.1 新版功能.

char* PyOS_double_to_string(double val, char format_code, int precision, int flags, int *ptype)

转换 double val 为一个使用 format_code， precision 和 flags 的字符串

格式码 必须是以下其中之一, 'e', 'E', 'f', 'F', 'g', 'G' 或者 'r'。对于 'r' , 提供的 精度 必须是0。'r' 格式码指定了标准函数 repr() 格式。

flags can be zero or more of the values Py_DTSF_SIGN, Py_DTSF_ADD_DOT_0, or Py_DTSF_ALT, or-ed together:

Py_DTSF_SIGN means to always precede the returned string with a sign character, even if val is non-negative.

Py_DTSF_ADD_DOT_0 means to ensure that the returned string will not look like an integer.

Py_DTSF_ALT means to apply “alternate” formatting rules. See the documentation for the PyOS_snprintf() '#' specifier for details.

If ptype is non-NULL, then the value it points to will be set to one of Py_DTST_FINITE, Py_DTST_INFINITE, or Py_DTST_NAN, signifying that val is a finite number, an infinite number, or not a number, respectively.

The return value is a pointer to buffer with the converted string or NULL if the conversion failed. The caller is responsible for freeing the returned string by calling PyMem_Free().

3.1 新版功能.

int PyOS_stricmp(const char *s1, const char *s2)

字符串不区分大小写。该函数几乎与 strcmp() 的工作方式相同，只是它忽略了大小写。

int PyOS_strnicmp(const char *s1, const char *s2, Py_ssize_t  size)

字符串不区分大小写。该函数几乎与 strncmp() 的工作方式相同，只是它忽略了大小写。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

工具 »

|

反射

PyObject* PyEval_GetBuiltins()

Return value: Borrowed reference.

返回当前执行帧中内置函数的字典，如果当前没有帧正在执行，则返回线程状态的解释器。

PyObject* PyEval_GetLocals()

Return value: Borrowed reference.

Return a dictionary of the local variables in the current execution frame, or NULL if no frame is currently executing.

PyObject* PyEval_GetGlobals()

Return value: Borrowed reference.

Return a dictionary of the global variables in the current execution frame, or NULL if no frame is currently executing.

PyFrameObject* PyEval_GetFrame()

Return value: Borrowed reference.

Return the current thread state’s frame, which is NULL if no frame is currently executing.

int PyFrame_GetLineNumber(PyFrameObject *frame)

返回 frame 当前正在执行的行号。

const char* PyEval_GetFuncName(PyObject *func)

如果 func 是函数、类或实例对象，则返回它的名称，否则返回 func 的类型的名称。

const char* PyEval_GetFuncDesc(PyObject *func)

根据 func 的类型返回描述字符串。 返回值包括函数和方法的 “()”, ” constructor”, ” instance” 和 ” object”。 与 PyEval_GetFuncName() 的结果连接，结果将是 func 的描述。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

工具 »

|

编解码器注册与支持功能

int PyCodec_Register(PyObject *search_function)

注册一个新的编解码器搜索函数。

作为副作用，其尝试加载 encodings 包，如果尚未完成，请确保它始终位于搜索函数列表的第一位。

int PyCodec_KnownEncoding(const char *encoding)

根据注册的给定 encoding 的编解码器是否已存在而返回 1 或 0。此函数总能成功。

PyObject* PyCodec_Encode(PyObject *object, const char *encoding, const char *errors)

泛型编解码器基本编码 API。

object is passed through the encoder function found for the given encoding using the error handling method defined by errors. errors may be NULL to use the default method defined for the codec. Raises a LookupError if no encoder can be found.

PyObject* PyCodec_Decode(PyObject *object, const char *encoding, const char *errors)

泛型编解码器基本解码 API。

object is passed through the decoder function found for the given encoding using the error handling method defined by errors. errors may be NULL to use the default method defined for the codec. Raises a LookupError if no encoder can be found.

Codec 查找API

In the following functions, the encoding string is looked up converted to all lower-case characters, which makes encodings looked up through this mechanism effectively case-insensitive. If no codec is found, a KeyError is set and NULL returned.

PyObject* PyCodec_Encoder(const char *encoding)

为给定的 encoding 获取一个编码器函数。

PyObject* PyCodec_Decoder(const char *encoding)

为给定的 encoding 获取一个解码器函数。

PyObject* PyCodec_IncrementalEncoder(const char *encoding, const char *errors)

为给定的 encoding 获取一个 IncrementalEncoder 对象。

PyObject* PyCodec_IncrementalDecoder(const char *encoding, const char *errors)

为给定的 encoding 获取一个 IncrementalDecoder 对象。

PyObject* PyCodec_StreamReader(const char *encoding, PyObject *stream, const char *errors)

为给定的 encoding 获取一个 StreamReader 工厂函数。

PyObject* PyCodec_StreamWriter(const char *encoding, PyObject *stream, const char *errors)

为给定的 encoding 获取一个 StreamWriter 工厂函数。

用于Unicode编码错误处理程序的注册表API

int PyCodec_RegisterError(const char *name, PyObject *error)

在给定的 name 之下注册错误处理回调函数 error。 该回调函数将在一个编解码器遇到无法编码的字符/无法解码的字节数据并且 name 被指定为 encode/decode 函数调用的 error 形参时由该编解码器来调用。

该回调函数会接受一个 UnicodeEncodeError, UnicodeDecodeError 或 UnicodeTranslateError 的实例作为单独参数，其中包含关于有问题字符或字节序列及其在原始序列的偏移量信息（请参阅 Unicode Exception Objects 了解提取此信息的函数详情）。 该回调函数必须引发给定的异常，或者返回一个包含有问题序列及相应替换序列的二元组，以及一个表示偏移量的整数，该整数指明应在什么位置上恢复编码/解码操作。

成功则返回``0`` ，失败则返回``-1``

PyObject* PyCodec_LookupError(const char *name)

Lookup the error handling callback function registered under name. As a special case NULL can be passed, in which case the error handling callback for “strict” will be returned.

PyObject* PyCodec_StrictErrors(PyObject *exc)

引发 exc 作为异常。

PyObject* PyCodec_IgnoreErrors(PyObject *exc)

忽略 unicode 错误，跳过错误的输入。

PyObject* PyCodec_ReplaceErrors(PyObject *exc)

使用 ? 或 U+FFFD 替换 unicode 编码错误。

PyObject* PyCodec_XMLCharRefReplaceErrors(PyObject *exc)

使用 XML 字符引用替换 unicode 编码错误。

PyObject* PyCodec_BackslashReplaceErrors(PyObject *exc)

使用反斜杠转义符 (\x, \u 和 \U) 替换 unicode 编码错误。

PyObject* PyCodec_NameReplaceErrors(PyObject *exc)

使用 \N{...} 转义符替换 unicode 编码错误。

3.5 新版功能.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

工具 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

抽象对象层

本章中的函数与 Python对象交互，无论其类型，或具有广泛类的对象类型（例如，所有数值类型，或所有序列类型）。当使用对象类型并不适用时，他们会产生一个 Python 异常。

这些函数是不可能用于未正确初始化的对象的，如一个列表对象被 PyList_New() 创建，但其中的项目没有被设置为一些非``NULL``的值。

对象协议

数字协议

序列协议

映射协议

迭代器协议

缓冲协议

缓冲区结构

Buffer request types

request-independent fields

只读，格式

形状，步幅，子偏移量

连续性的请求

复合请求

复杂数组

NumPy-风格：形状和步幅

PIL-风格：形状，步幅和子偏移量

缓冲区相关函数

旧缓冲协议

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

抽象对象层 »

|

对象协议

PyObject* Py_NotImplemented

The NotImplemented singleton, used to signal that an operation is not implemented for the given type combination.

Py_RETURN_NOTIMPLEMENTED

Properly handle returning Py_NotImplemented from within a C function (that is, increment the reference count of NotImplemented and return it).

int PyObject_Print(PyObject *o, FILE *fp, int flags)

Print an object o, on file fp. Returns -1 on error. The flags argument is used to enable certain printing options. The only option currently supported is Py_PRINT_RAW; if given, the str() of the object is written instead of the repr().

int PyObject_HasAttr(PyObject *o, PyObject *attr_name)

Returns 1 if o has the attribute attr_name, and 0 otherwise. This is equivalent to the Python expression hasattr(o, attr_name). This function always succeeds.

Note that exceptions which occur while calling __getattr__() and __getattribute__() methods will get suppressed. To get error reporting use PyObject_GetAttr() instead.

int PyObject_HasAttrString(PyObject *o, const char *attr_name)

Returns 1 if o has the attribute attr_name, and 0 otherwise. This is equivalent to the Python expression hasattr(o, attr_name). This function always succeeds.

Note that exceptions which occur while calling __getattr__() and __getattribute__() methods and creating a temporary string object will get suppressed. To get error reporting use PyObject_GetAttrString() instead.

PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name)

Return value: New reference.

Retrieve an attribute named attr_name from object o. Returns the attribute value on success, or NULL on failure. This is the equivalent of the Python expression o.attr_name.

PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name)

Return value: New reference.

Retrieve an attribute named attr_name from object o. Returns the attribute value on success, or NULL on failure. This is the equivalent of the Python expression o.attr_name.

PyObject* PyObject_GenericGetAttr(PyObject *o, PyObject *name)

Generic attribute getter function that is meant to be put into a type object’s tp_getattro slot. It looks for a descriptor in the dictionary of classes in the object’s MRO as well as an attribute in the object’s __dict__ (if present). As outlined in 实现描述器, data descriptors take preference over instance attributes, while non-data descriptors don’t. Otherwise, an AttributeError is raised.

int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)

Set the value of the attribute named attr_name, for object o, to the value v. Raise an exception and return -1 on failure; return 0 on success. This is the equivalent of the Python statement o.attr_name = v.

If v is NULL, the attribute is deleted, however this feature is deprecated in favour of using PyObject_DelAttr().

int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v)

Set the value of the attribute named attr_name, for object o, to the value v. Raise an exception and return -1 on failure; return 0 on success. This is the equivalent of the Python statement o.attr_name = v.

If v is NULL, the attribute is deleted, however this feature is deprecated in favour of using PyObject_DelAttrString().

int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)

Generic attribute setter and deleter function that is meant to be put into a type object’s tp_setattro slot. It looks for a data descriptor in the dictionary of classes in the object’s MRO, and if found it takes preference over setting or deleting the attribute in the instance dictionary. Otherwise, the attribute is set or deleted in the object’s __dict__ (if present). On success, 0 is returned, otherwise an AttributeError is raised and -1 is returned.

int PyObject_DelAttr(PyObject *o, PyObject *attr_name)

Delete attribute named attr_name, for object o. Returns -1 on failure. This is the equivalent of the Python statement del o.attr_name.

int PyObject_DelAttrString(PyObject *o, const char *attr_name)

Delete attribute named attr_name, for object o. Returns -1 on failure. This is the equivalent of the Python statement del o.attr_name.

PyObject* PyObject_GenericGetDict(PyObject *o, void *context)

A generic implementation for the getter of a __dict__ descriptor. It creates the dictionary if necessary.

3.3 新版功能.

int PyObject_GenericSetDict(PyObject *o, void *context)

A generic implementation for the setter of a __dict__ descriptor. This implementation does not allow the dictionary to be deleted.

3.3 新版功能.

PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid)

Return value: New reference.

Compare the values of o1 and o2 using the operation specified by opid, which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or Py_GE, corresponding to <, <=, ==, !=, >, or >= respectively. This is the equivalent of the Python expression o1 op o2, where op is the operator corresponding to opid. Returns the value of the comparison on success, or NULL on failure.

int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid)

Compare the values of o1 and o2 using the operation specified by opid, which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or Py_GE, corresponding to <, <=, ==, !=, >, or >= respectively. Returns -1 on error, 0 if the result is false, 1 otherwise. This is the equivalent of the Python expression o1 op o2, where op is the operator corresponding to opid.

注解

If o1 and o2 are the same object, PyObject_RichCompareBool() will always return 1 for Py_EQ and 0 for Py_NE.

PyObject* PyObject_Repr(PyObject *o)

Return value: New reference.

Compute a string representation of object o. Returns the string representation on success, NULL on failure. This is the equivalent of the Python expression repr(o). Called by the repr() built-in function.

在 3.4 版更改: This function now includes a debug assertion to help ensure that it does not silently discard an active exception.

PyObject* PyObject_ASCII(PyObject *o)

As PyObject_Repr(), compute a string representation of object o, but escape the non-ASCII characters in the string returned by PyObject_Repr() with \x, \u or \U escapes. This generates a string similar to that returned by PyObject_Repr() in Python 2. Called by the ascii() built-in function.

PyObject* PyObject_Str(PyObject *o)

Return value: New reference.

Compute a string representation of object o. Returns the string representation on success, NULL on failure. This is the equivalent of the Python expression str(o). Called by the str() built-in function and, therefore, by the print() function.

在 3.4 版更改: This function now includes a debug assertion to help ensure that it does not silently discard an active exception.

PyObject* PyObject_Bytes(PyObject *o)

Compute a bytes representation of object o. NULL is returned on failure and a bytes object on success. This is equivalent to the Python expression bytes(o), when o is not an integer. Unlike bytes(o), a TypeError is raised when o is an integer instead of a zero-initialized bytes object.

int PyObject_IsSubclass(PyObject *derived, PyObject *cls)

Return 1 if the class derived is identical to or derived from the class cls, otherwise return 0. In case of an error, return -1.

If cls is a tuple, the check will be done against every entry in cls. The result will be 1 when at least one of the checks returns 1, otherwise it will be 0.

If cls has a __subclasscheck__() method, it will be called to determine the subclass status as described in PEP 3119 [https://www.python.org/dev/peps/pep-3119]. Otherwise, derived is a subclass of cls if it is a direct or indirect subclass, i.e. contained in cls.__mro__.

Normally only class objects, i.e. instances of type or a derived class, are considered classes. However, objects can override this by having a __bases__ attribute (which must be a tuple of base classes).

int PyObject_IsInstance(PyObject *inst, PyObject *cls)

Return 1 if inst is an instance of the class cls or a subclass of cls, or 0 if not. On error, returns -1 and sets an exception.

If cls is a tuple, the check will be done against every entry in cls. The result will be 1 when at least one of the checks returns 1, otherwise it will be 0.

If cls has a __instancecheck__() method, it will be called to determine the subclass status as described in PEP 3119 [https://www.python.org/dev/peps/pep-3119]. Otherwise, inst is an instance of cls if its class is a subclass of cls.

An instance inst can override what is considered its class by having a __class__ attribute.

An object cls can override if it is considered a class, and what its base classes are, by having a __bases__ attribute (which must be a tuple of base classes).

int PyCallable_Check(PyObject *o)

Determine if the object o is callable. Return 1 if the object is callable and 0 otherwise. This function always succeeds.

PyObject* PyObject_Call(PyObject *callable_object, PyObject *args, PyObject *kw)

Return value: New reference.

Call a callable Python object callable_object, with arguments given by the tuple args, and named arguments given by the dictionary kw. If no named arguments are needed, kw may be NULL. args must not be NULL, use an empty tuple if no arguments are needed. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression callable_object(*args, **kw).

PyObject* PyObject_CallObject(PyObject *callable_object, PyObject *args)

Return value: New reference.

Call a callable Python object callable_object, with arguments given by the tuple args. If no arguments are needed, then args may be NULL. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression callable_object(*args).

PyObject* PyObject_CallFunction(PyObject *callable, const char *format, ...)

Return value: New reference.

Call a callable Python object callable, with a variable number of C arguments. The C arguments are described using a Py_BuildValue() style format string. The format may be NULL, indicating that no arguments are provided. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression callable(*args). Note that if you only pass PyObject * args, PyObject_CallFunctionObjArgs() is a faster alternative.

在 3.4 版更改: The type of format was changed from char *.

PyObject* PyObject_CallMethod(PyObject *o, const char *method, const char *format, ...)

Return value: New reference.

Call the method named method of object o with a variable number of C arguments. The C arguments are described by a Py_BuildValue() format string that should produce a tuple. The format may be NULL, indicating that no arguments are provided. Returns the result of the call on success, or NULL on failure. This is the equivalent of the Python expression o.method(args). Note that if you only pass PyObject * args, PyObject_CallMethodObjArgs() is a faster alternative.

在 3.4 版更改: The types of method and format were changed from char *.

PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL)

Return value: New reference.

Call a callable Python object callable, with a variable number of PyObject* arguments. The arguments are provided as a variable number of parameters followed by NULL. Returns the result of the call on success, or NULL on failure.

PyObject* PyObject_CallMethodObjArgs(PyObject *o, PyObject *name, ..., NULL)

Return value: New reference.

Calls a method of the object o, where the name of the method is given as a Python string object in name. It is called with a variable number of PyObject* arguments. The arguments are provided as a variable number of parameters followed by NULL. Returns the result of the call on success, or NULL on failure.

Py_hash_t PyObject_Hash(PyObject *o)

Compute and return the hash value of an object o. On failure, return -1. This is the equivalent of the Python expression hash(o).

在 3.2 版更改: The return type is now Py_hash_t. This is a signed integer the same size as Py_ssize_t.

Py_hash_t PyObject_HashNotImplemented(PyObject *o)

Set a TypeError indicating that type(o) is not hashable and return -1. This function receives special treatment when stored in a tp_hash slot, allowing a type to explicitly indicate to the interpreter that it is not hashable.

int PyObject_IsTrue(PyObject *o)

Returns 1 if the object o is considered to be true, and 0 otherwise. This is equivalent to the Python expression not not o. On failure, return -1.

int PyObject_Not(PyObject *o)

Returns 0 if the object o is considered to be true, and 1 otherwise. This is equivalent to the Python expression not o. On failure, return -1.

PyObject* PyObject_Type(PyObject *o)

Return value: New reference.

When o is non-NULL, returns a type object corresponding to the object type of object o. On failure, raises SystemError and returns NULL. This is equivalent to the Python expression type(o). This function increments the reference count of the return value. There’s really no reason to use this function instead of the common expression o->ob_type, which returns a pointer of type PyTypeObject*, except when the incremented reference count is needed.

int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)

Return true if the object o is of type type or a subtype of type. Both parameters must be non-NULL.

Py_ssize_t PyObject_Size(PyObject *o)

Py_ssize_t PyObject_Length(PyObject *o)

Return the length of object o. If the object o provides either the sequence and mapping protocols, the sequence length is returned. On error, -1 is returned. This is the equivalent to the Python expression len(o).

Py_ssize_t PyObject_LengthHint(PyObject *o, Py_ssize_t default)

Return an estimated length for the object o. First try to return its actual length, then an estimate using __length_hint__(), and finally return the default value. On error return -1. This is the equivalent to the Python expression operator.length_hint(o, default).

3.4 新版功能.

PyObject* PyObject_GetItem(PyObject *o, PyObject *key)

Return value: New reference.

Return element of o corresponding to the object key or NULL on failure. This is the equivalent of the Python expression o[key].

int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)

Map the object key to the value v. Raise an exception and return -1 on failure; return 0 on success. This is the equivalent of the Python statement o[key] = v.

int PyObject_DelItem(PyObject *o, PyObject *key)

Remove the mapping for the object key from the object o. Return -1 on failure. This is equivalent to the Python statement del o[key].

PyObject* PyObject_Dir(PyObject *o)

Return value: New reference.

This is equivalent to the Python expression dir(o), returning a (possibly empty) list of strings appropriate for the object argument, or NULL if there was an error. If the argument is NULL, this is like the Python dir(), returning the names of the current locals; in this case, if no execution frame is active then NULL is returned but PyErr_Occurred() will return false.

PyObject* PyObject_GetIter(PyObject *o)

Return value: New reference.

This is equivalent to the Python expression iter(o). It returns a new iterator for the object argument, or the object itself if the object is already an iterator. Raises TypeError and returns NULL if the object cannot be iterated.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

抽象对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

抽象对象层 »

|

数字协议

int PyNumber_Check(PyObject *o)

如果对象 o 提供数字的协议，返回真 1，否则返回假。这个函数不会调用失败。

PyObject* PyNumber_Add(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the result of adding o1 and o2, or NULL on failure. This is the equivalent of the Python expression o1 + o2.

PyObject* PyNumber_Subtract(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the result of subtracting o2 from o1, or NULL on failure. This is the equivalent of the Python expression o1 - o2.

PyObject* PyNumber_Multiply(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the result of multiplying o1 and o2, or NULL on failure. This is the equivalent of the Python expression o1 * o2.

PyObject* PyNumber_MatrixMultiply(PyObject *o1, PyObject *o2)

Returns the result of matrix multiplication on o1 and o2, or NULL on failure. This is the equivalent of the Python expression o1 @ o2.

3.5 新版功能.

PyObject* PyNumber_FloorDivide(PyObject *o1, PyObject *o2)

Return value: New reference.

Return the floor of o1 divided by o2, or NULL on failure. This is equivalent to the “classic” division of integers.

PyObject* PyNumber_TrueDivide(PyObject *o1, PyObject *o2)

Return value: New reference.

Return a reasonable approximation for the mathematical value of o1 divided by o2, or NULL on failure. The return value is “approximate” because binary floating point numbers are approximate; it is not possible to represent all real numbers in base two. This function can return a floating point value when passed two integers.

PyObject* PyNumber_Remainder(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the remainder of dividing o1 by o2, or NULL on failure. This is the equivalent of the Python expression o1 % o2.

PyObject* PyNumber_Divmod(PyObject *o1, PyObject *o2)

Return value: New reference.

See the built-in function divmod(). Returns NULL on failure. This is the equivalent of the Python expression divmod(o1, o2).

PyObject* PyNumber_Power(PyObject *o1, PyObject *o2, PyObject *o3)

Return value: New reference.

See the built-in function pow(). Returns NULL on failure. This is the equivalent of the Python expression pow(o1, o2, o3), where o3 is optional. If o3 is to be ignored, pass Py_None in its place (passing NULL for o3 would cause an illegal memory access).

PyObject* PyNumber_Negative(PyObject *o)

Return value: New reference.

Returns the negation of o on success, or NULL on failure. This is the equivalent of the Python expression -o.

PyObject* PyNumber_Positive(PyObject *o)

Return value: New reference.

Returns o on success, or NULL on failure. This is the equivalent of the Python expression +o.

PyObject* PyNumber_Absolute(PyObject *o)

Return value: New reference.

Returns the absolute value of o, or NULL on failure. This is the equivalent of the Python expression abs(o).

PyObject* PyNumber_Invert(PyObject *o)

Return value: New reference.

Returns the bitwise negation of o on success, or NULL on failure. This is the equivalent of the Python expression ~o.

PyObject* PyNumber_Lshift(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the result of left shifting o1 by o2 on success, or NULL on failure. This is the equivalent of the Python expression o1 << o2.

PyObject* PyNumber_Rshift(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the result of right shifting o1 by o2 on success, or NULL on failure. This is the equivalent of the Python expression o1 >> o2.

PyObject* PyNumber_And(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the “bitwise and” of o1 and o2 on success and NULL on failure. This is the equivalent of the Python expression o1 & o2.

PyObject* PyNumber_Xor(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the “bitwise exclusive or” of o1 by o2 on success, or NULL on failure. This is the equivalent of the Python expression o1 ^ o2.

PyObject* PyNumber_Or(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the “bitwise or” of o1 and o2 on success, or NULL on failure. This is the equivalent of the Python expression o1 | o2.

PyObject* PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the result of adding o1 and o2, or NULL on failure. The operation is done in-place when o1 supports it. This is the equivalent of the Python statement o1 += o2.

PyObject* PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the result of subtracting o2 from o1, or NULL on failure. The operation is done in-place when o1 supports it. This is the equivalent of the Python statement o1 -= o2.

PyObject* PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the result of multiplying o1 and o2, or NULL on failure. The operation is done in-place when o1 supports it. This is the equivalent of the Python statement o1 *= o2.

PyObject* PyNumber_InPlaceMatrixMultiply(PyObject *o1, PyObject *o2)

Returns the result of matrix multiplication on o1 and o2, or NULL on failure. The operation is done in-place when o1 supports it. This is the equivalent of the Python statement o1 @= o2.

3.5 新版功能.

PyObject* PyNumber_InPlaceFloorDivide(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the mathematical floor of dividing o1 by o2, or NULL on failure. The operation is done in-place when o1 supports it. This is the equivalent of the Python statement o1 //= o2.

PyObject* PyNumber_InPlaceTrueDivide(PyObject *o1, PyObject *o2)

Return value: New reference.

Return a reasonable approximation for the mathematical value of o1 divided by o2, or NULL on failure. The return value is “approximate” because binary floating point numbers are approximate; it is not possible to represent all real numbers in base two. This function can return a floating point value when passed two integers. The operation is done in-place when o1 supports it.

PyObject* PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the remainder of dividing o1 by o2, or NULL on failure. The operation is done in-place when o1 supports it. This is the equivalent of the Python statement o1 %= o2.

PyObject* PyNumber_InPlacePower(PyObject *o1, PyObject *o2, PyObject *o3)

Return value: New reference.

See the built-in function pow(). Returns NULL on failure. The operation is done in-place when o1 supports it. This is the equivalent of the Python statement o1 **= o2 when o3 is Py_None, or an in-place variant of pow(o1, o2, o3) otherwise. If o3 is to be ignored, pass Py_None in its place (passing NULL for o3 would cause an illegal memory access).

PyObject* PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the result of left shifting o1 by o2 on success, or NULL on failure. The operation is done in-place when o1 supports it. This is the equivalent of the Python statement o1 <<= o2.

PyObject* PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the result of right shifting o1 by o2 on success, or NULL on failure. The operation is done in-place when o1 supports it. This is the equivalent of the Python statement o1 >>= o2.

PyObject* PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the “bitwise and” of o1 and o2 on success and NULL on failure. The operation is done in-place when o1 supports it. This is the equivalent of the Python statement o1 &= o2.

PyObject* PyNumber_InPlaceXor(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the “bitwise exclusive or” of o1 by o2 on success, or NULL on failure. The operation is done in-place when o1 supports it. This is the equivalent of the Python statement o1 ^= o2.

PyObject* PyNumber_InPlaceOr(PyObject *o1, PyObject *o2)

Return value: New reference.

Returns the “bitwise or” of o1 and o2 on success, or NULL on failure. The operation is done in-place when o1 supports it. This is the equivalent of the Python statement o1 |= o2.

PyObject* PyNumber_Long(PyObject *o)

Return value: New reference.

Returns the o converted to an integer object on success, or NULL on failure. This is the equivalent of the Python expression int(o).

PyObject* PyNumber_Float(PyObject *o)

Return value: New reference.

Returns the o converted to a float object on success, or NULL on failure. This is the equivalent of the Python expression float(o).

PyObject* PyNumber_Index(PyObject *o)

Returns the o converted to a Python int on success or NULL with a TypeError exception raised on failure.

PyObject* PyNumber_ToBase(PyObject *n, int base)

返回整数 n 转换成以 base 为基数的字符串后的结果。这个 base 参数必须是 2，8，10 或者 16 。对于基数 2，8，或 16 ，返回的字符串将分别加上基数标识 '0b', '0o', or '0x'。如果 n 不是 Python 中的整数 int 类型，就先通过 PyNumber_Index() 将它转换成整数类型。

Py_ssize_t PyNumber_AsSsize_t(PyObject *o, PyObject *exc)

如果 o 是一个整数类型的解释型，返回 o 转换成一个 Py_ssize_t 值项后的结果。如果调用失败，返回 -1 并引发异常。

If o can be converted to a Python int but the attempt to convert to a Py_ssize_t value would raise an OverflowError, then the exc argument is the type of exception that will be raised (usually IndexError or OverflowError). If exc is NULL, then the exception is cleared and the value is clipped to PY_SSIZE_T_MIN for a negative integer or PY_SSIZE_T_MAX for a positive integer.

int PyIndex_Check(PyObject *o)

如果 o 是一个索引整数（存有 nb_index 位置并有 tp_as_number 填入其中）则返回 1，否则返回 0 。这个函数不会调用失败。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

抽象对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

抽象对象层 »

|

序列协议

int PySequence_Check(PyObject *o)

如果对象提供序列协议，函数返回 1，否则返回 0。 请注意它将为具有 __getitem__() 方法的 Python 类返回 1，除非它们是 dict 的子类，因为在一般情况下无法确定它所支持键类型。 此函数总是会成功执行。

Py_ssize_t PySequence_Size(PyObject *o)

Py_ssize_t PySequence_Length(PyObject *o)

成功时返回序列中*o*的对象数,失败时返回``-1``. 相当于Python的``len(o)``表达式.

PyObject* PySequence_Concat(PyObject *o1, PyObject *o2)

Return value: New reference.

Return the concatenation of o1 and o2 on success, and NULL on failure. This is the equivalent of the Python expression o1 + o2.

PyObject* PySequence_Repeat(PyObject *o, Py_ssize_t count)

Return value: New reference.

Return the result of repeating sequence object o count times, or NULL on failure. This is the equivalent of the Python expression o * count.

PyObject* PySequence_InPlaceConcat(PyObject *o1, PyObject *o2)

Return value: New reference.

Return the concatenation of o1 and o2 on success, and NULL on failure. The operation is done in-place when o1 supports it. This is the equivalent of the Python expression o1 += o2.

PyObject* PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)

Return value: New reference.

Return the result of repeating sequence object o count times, or NULL on failure. The operation is done in-place when o supports it. This is the equivalent of the Python expression o *= count.

PyObject* PySequence_GetItem(PyObject *o, Py_ssize_t i)

Return value: New reference.

Return the ith element of o, or NULL on failure. This is the equivalent of the Python expression o[i].

PyObject* PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)

Return value: New reference.

Return the slice of sequence object o between i1 and i2, or NULL on failure. This is the equivalent of the Python expression o[i1:i2].

int PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v)

将对象 v 赋值给 o 的第 i 号元素。 失败时会引发异常并返回 -1；成功时返回 0。 这相当于 Python 语句 o[i] = v。 此函数 不会 改变对 v 的引用。

If v is NULL, the element is deleted, however this feature is deprecated in favour of using PySequence_DelItem().

int PySequence_DelItem(PyObject *o, Py_ssize_t i)

删除对象 o 的第 i 号元素。 失败时返回 -1。 这相当于 Python 语句 del o[i]。

int PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2, PyObject *v)

将序列对象 v 赋值给序列对象 o 的从 i1 到 i2 切片。 这相当于 Python 语句 o[i1:i2] = v。

int PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2)

删除序列对象 o 的从 i1 到 i2 的切片。 失败时返回 -1。 这相当于 Python 语句 del o[i1:i2]。

Py_ssize_t PySequence_Count(PyObject *o, PyObject *value)

返回 value 在 o 中出现的次数，即返回使得 o[key] == value 的键的数量。 失败时返回 -1。 这相当于 Python 表达式 o.count(value)。

int PySequence_Contains(PyObject *o, PyObject *value)

确定 o 是否包含 value。 如果 o 中的某一项等于 value，则返回 1，否则返回 0。 出错时，返回 -1。 这相当于 Python 表达式 value in o。

Py_ssize_t PySequence_Index(PyObject *o, PyObject *value)

返回第一个索引*i*,其中 o[i] == value.出错时,返回 -1.相当于Python的``o.index(value)``表达式.

PyObject* PySequence_List(PyObject *o)

Return value: New reference.

Return a list object with the same contents as the sequence or iterable o, or NULL on failure. The returned list is guaranteed to be new. This is equivalent to the Python expression list(o).

PyObject* PySequence_Tuple(PyObject *o)

Return value: New reference.

Return a tuple object with the same contents as the sequence or iterable o, or NULL on failure. If o is a tuple, a new reference will be returned, otherwise a tuple will be constructed with the appropriate contents. This is equivalent to the Python expression tuple(o).

PyObject* PySequence_Fast(PyObject *o, const char *m)

Return value: New reference.

Return the sequence or iterable o as a list, unless it is already a tuple or list, in which case o is returned. Use PySequence_Fast_GET_ITEM() to access the members of the result. Returns NULL on failure. If the object is not a sequence or iterable, raises TypeError with m as the message text.

Py_ssize_t PySequence_Fast_GET_SIZE(PyObject *o)

Returns the length of o, assuming that o was returned by PySequence_Fast() and that o is not NULL. The size can also be gotten by calling PySequence_Size() on o, but PySequence_Fast_GET_SIZE() is faster because it can assume o is a list or tuple.

PyObject* PySequence_Fast_GET_ITEM(PyObject *o, Py_ssize_t i)

Return value: Borrowed reference.

Return the ith element of o, assuming that o was returned by PySequence_Fast(), o is not NULL, and that i is within bounds.

PyObject** PySequence_Fast_ITEMS(PyObject *o)

Return the underlying array of PyObject pointers. Assumes that o was returned by PySequence_Fast() and o is not NULL.

请注意,如果列表调整大小,重新分配可能会重新定位items数组.因此,仅在序列无法更改的上下文中使用基础数组指针.

PyObject* PySequence_ITEM(PyObject *o, Py_ssize_t i)

Return value: New reference.

Return the ith element of o or NULL on failure. Macro form of PySequence_GetItem() but without checking that PySequence_Check() on o is true and without adjustment for negative indices.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

抽象对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

抽象对象层 »

|

映射协议

参见 PyObject_GetItem()、PyObject_SetItem() 与 PyObject_DelItem()。

int PyMapping_Check(PyObject *o)

Return 1 if the object provides mapping protocol or supports slicing, and 0 otherwise. Note that it returns 1 for Python classes with a __getitem__() method since in general case it is impossible to determine what the type of keys it supports. This function always succeeds.

Py_ssize_t PyMapping_Size(PyObject *o)

Py_ssize_t PyMapping_Length(PyObject *o)

成功时返回对象 o 中键的数量，失败时返回 -1。 这相当于 Python 表达式 len(o)。

PyObject* PyMapping_GetItemString(PyObject *o, const char *key)

Return value: New reference.

Return element of o corresponding to the string key or NULL on failure. This is the equivalent of the Python expression o[key]. See also PyObject_GetItem().

int PyMapping_SetItemString(PyObject *o, const char *key, PyObject *v)

在对象 o 中将字符串 key 映射到值 v。 失败时返回 -1。 这相当于 Python 语句 o[key] = v。 另请参见 PyObject_SetItem()。

int PyMapping_DelItem(PyObject *o, PyObject *key)

从对象 o 中移除对象 key 的映射。 失败时返回 -1。 这相当于 Python 语句 del o[key]。 这是 PyObject_DelItem() 的一个别名。

int PyMapping_DelItemString(PyObject *o, const char *key)

从对象 o 中移除字符串 key 的映射。 失败时返回 -1。 这相当于 Python 语句 del o[key]。

int PyMapping_HasKey(PyObject *o, PyObject *key)

如果映射对象具有键 key 则返回 1，否则返回 0。 这相当于 Python 表达式 key in o。 此函数总是会成功执行。

请注意在调用 __getitem__() 方法期间发生的异常将会被屏蔽。 要获取错误报告请改用 PyObject_GetItem()。

int PyMapping_HasKeyString(PyObject *o, const char *key)

如果映射对象具有键 key 则返回 1，否则返回 0。 这相当于 Python 表达式 key in o。 此函数总是会成功执行。

请注意在调用 __getitem__() 方法期间发生的异常将会被屏蔽。 要获取错误报告请改用 PyMapping_GetItemString()。

PyObject* PyMapping_Keys(PyObject *o)

Return value: New reference.

On success, return a list or tuple of the keys in object o. On failure, return NULL.

PyObject* PyMapping_Values(PyObject *o)

Return value: New reference.

On success, return a list or tuple of the values in object o. On failure, return NULL.

PyObject* PyMapping_Items(PyObject *o)

Return value: New reference.

On success, return a list or tuple of the items in object o, where each item is a tuple containing a key-value pair. On failure, return NULL.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

抽象对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

抽象对象层 »

|

迭代器协议

迭代器有两个函数。

int PyIter_Check(PyObject *o)

返回 true ， 如果对象 o 支持迭代器协议的话。

PyObject* PyIter_Next(PyObject *o)

Return value: New reference.

Return the next value from the iteration o. The object must be an iterator (it is up to the caller to check this). If there are no remaining values, returns NULL with no exception set. If an error occurs while retrieving the item, returns NULL and passes along the exception.

要为迭代器编写一个一个循环，C代码应该看起来像这样

PyObject *iterator = PyObject_GetIter(obj);
PyObject *item;

if (iterator == NULL) {
    /* propagate error */
}

while (item = PyIter_Next(iterator)) {
    /* do something with item */
    ...
    /* release reference when done */
    Py_DECREF(item);
}

Py_DECREF(iterator);

if (PyErr_Occurred()) {
    /* propagate error */
}
else {
    /* continue doing useful work */
}

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

抽象对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

抽象对象层 »

|

缓冲协议

在 Python 中可使用一些对象来包装对底层内存数组或称 缓冲 的访问。此类对象包括内置的 bytes 和 bytearray 以及一些如 array.array 这样的扩展类型。第三方库也可能会为了特殊的目的而定义它们自己的类型，例如用于图像处理和数值分析等。

虽然这些类型中的每一种都有自己的语义，但它们具有由可能较大的内存缓冲区支持的共同特征。 在某些情况下，希望直接访问该缓冲区而无需中间复制。

Python 以 缓冲协议 的形式在 C 层级上提供这样的功能。 此协议包括两个方面:

在生产者这一方面，该类型的协议可以导出一个“缓冲区接口”，允许公开它的底层缓冲区信息。该接口的描述信息在 Buffer Object Structures 一节中；

在消费者一侧，有几种方法可用于获得指向对象的原始底层数据的指针（例如一个方法的形参）。

一些简单的对象例如 bytes 和 bytearray 会以面向字节的形式公开它们的底层缓冲区。 也可能会用其他形式；例如 array.array 所公开的元素可以是多字节值。

缓冲区接口的消费者的一个例子是文件对象的 write() 方法：任何可以输出为一系列字节流的对象可以被写入文件。然而 write() 方法只需要对于传入对象的只读权限，其他的方法，如 readinto() 需要参数内容的写入权限。缓冲区接口使得对象可以选择性地允许或拒绝读写或只读缓冲区的导出。

对于缓冲接口的消费者而言，有两种方式来获取一个目的对象的缓冲。

使用正确的参数来调用 PyObject_GetBuffer()  函数

调用 PyArg_ParseTuple() (或其同级对象之一) 并传入 y*, w* or s* 格式代码 中的一个。

在这两种情况下，当不再需要缓冲区时必须调用 PyBuffer_Release() 。如果此操作失败，可能会导致各种问题，例如资源泄漏。

缓冲区结构

缓冲区结构(或者简单地称为“buffers”)对于将二进制数据从另一个对象公开给Python程序员非常有用。它们还可以用作零拷贝切片机制。使用它们引用内存块的能力，可以很容易地将任何数据公开给Python程序员。内存可以是C扩展中的一个大的常量数组，也可以是在传递到操作系统库之前用于操作的原始内存块，或者可以用来传递本机内存格式的结构化数据。

与 Python 解释器公开的大多部数据类型不同，缓冲区不是 PyObject 指针而是简单的 C 结构。 这使得它们可以非常简单地创建和复制。 当需要为缓冲区加上泛型包装器时，可以创建一个 内存视图 对象。

有关如何编写并导出对象的简短说明，请参阅 缓冲区对象结构。 要获取缓冲区对象，请参阅 PyObject_GetBuffer()。

Py_buffer

void *buf

指向由缓冲区字段描述的逻辑结构开始的指针。 这可以是导出程序底层物理内存块中的任何位置。 例如，使用负的 strides 值可能指向内存块的末尾。

对于 contiguous ，‘邻接’数组，值指向内存块的开头。

void *obj

A new reference to the exporting object. The reference is owned by the consumer and automatically decremented and set to NULL by PyBuffer_Release(). The field is the equivalent of the return value of any standard C-API function.

As a special case, for temporary buffers that are wrapped by PyMemoryView_FromBuffer() or PyBuffer_FillInfo() this field is NULL. In general, exporting objects MUST NOT use this scheme.

Py_ssize_t len

product(shape) * itemsize。对于连续数组，这是基础内存块的长度。对于非连续数组，如果逻辑结构复制到连续表示形式，则该长度将具有该长度。

仅当缓冲区是通过保证连续性的请求获取时，才访问 ((char *)buf)[0] up to ((char *)buf)[len-1] 时才有效。在大多数情况下，此类请求将为 PyBUF_SIMPLE 或 PyBUF_WRITABLE。

int readonly

缓冲区是否为只读的指示器。此字段由 PyBUF_WRITABLE 标志控制。

Py_ssize_t itemsize

Item size in bytes of a single element. Same as the value of struct.calcsize() called on non-NULL format values.

Important exception: If a consumer requests a buffer without the PyBUF_FORMAT flag, format will be set to NULL, but itemsize still has the value for the original format.

如果 shape 存在，则相等的 product(shape) * itemsize == len 仍然存在，使用者可以使用 itemsize 来导航缓冲区。

If shape is NULL as a result of a PyBUF_SIMPLE or a PyBUF_WRITABLE request, the consumer must disregard itemsize and assume itemsize == 1.

const char *format

A NUL terminated string in struct module style syntax describing the contents of a single item. If this is NULL, "B" (unsigned bytes) is assumed.

此字段由 PyBUF_FORMAT 标志控制。

int ndim

The number of dimensions the memory represents as an n-dimensional array. If it is 0, buf points to a single item representing a scalar. In this case, shape, strides and suboffsets MUST be NULL.

The macro PyBUF_MAX_NDIM limits the maximum number of dimensions to 64. Exporters MUST respect this limit, consumers of multi-dimensional buffers SHOULD be able to handle up to PyBUF_MAX_NDIM dimensions.

Py_ssize_t *shape

An array of Py_ssize_t of length ndim indicating the shape of the memory as an n-dimensional array. Note that shape[0] * ... * shape[ndim-1] * itemsize MUST be equal to len.

Shape 形状数组中的值被限定在 shape[n] >= 0 。 shape[n] == 0 这一情形需要特别注意。更多信息请参阅 complex arrays 。

shape数组对于使用者来说是只读的。

Py_ssize_t *strides

An array of Py_ssize_t of length ndim giving the number of bytes to skip to get to a new element in each dimension.

Stride 步幅数组中的值可以为任何整数。对于常规数组，步幅通常为正数，但是使用者必须能够处理 strides[n] <= 0 的情况。更多信息请参阅 complex arrays 。

strides数组对用户来说是只读的。

Py_ssize_t *suboffsets

An array of Py_ssize_t of length ndim. If suboffsets[n] >= 0, the values stored along the nth dimension are pointers and the suboffset value dictates how many bytes to add to each pointer after de-referencing. A suboffset value that is negative indicates that no de-referencing should occur (striding in a contiguous memory block).

If all suboffsets are negative (i.e. no de-referencing is needed), then this field must be NULL (the default value).

Python Imaging Library (PIL) 中使用了这种数组的表达方式。请参阅 complex arrays 来了解如何从这样一个数组中访问元素。

suboffsets 数组对于使用者来说是只读的。

void *internal

This is for use internally by the exporting object. For example, this might be re-cast as an integer by the exporter and used to store flags about whether or not the shape, strides, and suboffsets arrays must be freed when the buffer is released. The consumer MUST NOT alter this value.

Buffer request types

Buffers are usually obtained by sending a buffer request to an exporting object via PyObject_GetBuffer(). Since the complexity of the logical structure of the memory can vary drastically, the consumer uses the flags argument to specify the exact buffer type it can handle.

All Py_buffer fields are unambiguously defined by the request type.

request-independent fields

The following fields are not influenced by flags and must always be filled in with the correct values: obj, buf, len, itemsize, ndim.

只读，格式

PyBUF_WRITABLE

Controls the readonly field. If set, the exporter MUST provide a writable buffer or else report failure. Otherwise, the exporter MAY provide either a read-only or writable buffer, but the choice MUST be consistent for all consumers.

PyBUF_FORMAT

Controls the format field. If set, this field MUST be filled in correctly. Otherwise, this field MUST be NULL.

PyBUF_WRITABLE can be |’d to any of the flags in the next section. Since PyBUF_SIMPLE is defined as 0, PyBUF_WRITABLE can be used as a stand-alone flag to request a simple writable buffer.

PyBUF_FORMAT can be |’d to any of the flags except PyBUF_SIMPLE. The latter already implies format B (unsigned bytes).

形状，步幅，子偏移量

The flags that control the logical structure of the memory are listed in decreasing order of complexity. Note that each flag contains all bits of the flags below it.

请求

形状

步幅

子偏移量

PyBUF_INDIRECT

是

是

如果需要的话

PyBUF_STRIDES

是

是

NULL

PyBUF_ND

是

NULL

NULL

PyBUF_SIMPLE

NULL

NULL

NULL

连续性的请求

C or Fortran contiguity can be explicitly requested, with and without stride information. Without stride information, the buffer must be C-contiguous.

请求

形状

步幅

子偏移量

邻接

PyBUF_C_CONTIGUOUS

是

是

NULL

C

PyBUF_F_CONTIGUOUS

是

是

NULL

F

PyBUF_ANY_CONTIGUOUS

是

是

NULL

C 或 F

PyBUF_ND

是

NULL

NULL

C

复合请求

所有可能的请求都由上一节中某些标志的组合完全定义。为方便起见，缓冲区协议提供常用的组合作为单个标志。

In the following table U stands for undefined contiguity. The consumer would have to call PyBuffer_IsContiguous() to determine contiguity.

请求

形状

步幅

子偏移量

邻接

只读

格式

PyBUF_FULL

是

是

如果需要的话

U

0

是

PyBUF_FULL_RO

是

是

如果需要的话

U

1 或 0

是

PyBUF_RECORDS

是

是

NULL

U

0

是

PyBUF_RECORDS_RO

是

是

NULL

U

1 或 0

是

PyBUF_STRIDED

是

是

NULL

U

0

NULL

PyBUF_STRIDED_RO

是

是

NULL

U

1 或 0

NULL

PyBUF_CONTIG

是

NULL

NULL

C

0

NULL

PyBUF_CONTIG_RO

是

NULL

NULL

C

1 或 0

NULL

复杂数组

NumPy-风格：形状和步幅

The logical structure of NumPy-style arrays is defined by itemsize, ndim, shape and strides.

If ndim == 0, the memory location pointed to by buf is interpreted as a scalar of size itemsize. In that case, both shape and strides are NULL.

If strides is NULL, the array is interpreted as a standard n-dimensional C-array. Otherwise, the consumer must access an n-dimensional array as follows:

ptr = (char *)buf + indices[0] * strides[0] + ... + indices[n-1] * strides[n-1] item = *((typeof(item) *)ptr);

As noted above, buf can point to any location within the actual memory block. An exporter can check the validity of a buffer with this function:

def verify_structure(memlen, itemsize, ndim, shape, strides, offset):
    """Verify that the parameters represent a valid array within
       the bounds of the allocated memory:
           char *mem: start of the physical memory block
           memlen: length of the physical memory block
           offset: (char *)buf - mem
    """
    if offset % itemsize:
        return False
    if offset < 0 or offset+itemsize > memlen:
        return False
    if any(v % itemsize for v in strides):
        return False

    if ndim <= 0:
        return ndim == 0 and not shape and not strides
    if 0 in shape:
        return True

    imin = sum(strides[j]*(shape[j]-1) for j in range(ndim)
               if strides[j] <= 0)
    imax = sum(strides[j]*(shape[j]-1) for j in range(ndim)
               if strides[j] > 0)

    return 0 <= offset+imin and offset+imax+itemsize <= memlen

PIL-风格：形状，步幅和子偏移量

In addition to the regular items, PIL-style arrays can contain pointers that must be followed in order to get to the next element in a dimension. For example, the regular three-dimensional C-array char v[2][2][3] can also be viewed as an array of 2 pointers to 2 two-dimensional arrays: char (*v[2])[2][3]. In suboffsets representation, those two pointers can be embedded at the start of buf, pointing to two char x[2][3] arrays that can be located anywhere in memory.

Here is a function that returns a pointer to the element in an N-D array pointed to by an N-dimensional index when there are both non-NULL strides and suboffsets:

void *get_item_pointer(int ndim, void *buf, Py_ssize_t *strides,
                       Py_ssize_t *suboffsets, Py_ssize_t *indices) {
    char *pointer = (char*)buf;
    int i;
    for (i = 0; i < ndim; i++) {
        pointer += strides[i] * indices[i];
        if (suboffsets[i] >=0 ) {
            pointer = *((char**)pointer) + suboffsets[i];
        }
    }
    return (void*)pointer;
}

缓冲区相关函数

int PyObject_CheckBuffer(PyObject *obj)

Return 1 if obj supports the buffer interface otherwise 0. When 1 is returned, it doesn’t guarantee that PyObject_GetBuffer() will succeed. This function always succeeds.

int PyObject_GetBuffer(PyObject *exporter, Py_buffer *view, int flags)

Send a request to exporter to fill in view as specified by flags. If the exporter cannot provide a buffer of the exact type, it MUST raise PyExc_BufferError, set view->obj to NULL and return -1.

On success, fill in view, set view->obj to a new reference to exporter and return 0. In the case of chained buffer providers that redirect requests to a single object, view->obj MAY refer to this object instead of exporter (See Buffer Object Structures).

Successful calls to PyObject_GetBuffer() must be paired with calls to PyBuffer_Release(), similar to malloc() and free(). Thus, after the consumer is done with the buffer, PyBuffer_Release() must be called exactly once.

void PyBuffer_Release(Py_buffer *view)

Release the buffer view and decrement the reference count for view->obj. This function MUST be called when the buffer is no longer being used, otherwise reference leaks may occur.

It is an error to call this function on a buffer that was not obtained via PyObject_GetBuffer().

Py_ssize_t PyBuffer_SizeFromFormat(const char *)

Return the implied itemsize from format. This function is not yet implemented.

int PyBuffer_IsContiguous(Py_buffer *view, char order)

Return 1 if the memory defined by the view is C-style (order is 'C') or Fortran-style (order is 'F') contiguous or either one (order is 'A'). Return 0 otherwise. This function always succeeds.

int PyBuffer_ToContiguous(void *buf, Py_buffer *src, Py_ssize_t len, char order)

Copy len bytes from src to its contiguous representation in buf. order can be 'C' or 'F' (for C-style or Fortran-style ordering). 0 is returned on success, -1 on error.

如果 len != src->len 则此函数将报错。

void PyBuffer_FillContiguousStrides(int ndims, Py_ssize_t *shape, Py_ssize_t *strides, int itemsize, char order)

Fill the strides array with byte-strides of a contiguous (C-style if order is 'C' or Fortran-style if order is 'F') array of the given shape with the given number of bytes per element.

int PyBuffer_FillInfo(Py_buffer *view, PyObject *exporter, void *buf, Py_ssize_t len, int readonly, int flags)

Handle buffer requests for an exporter that wants to expose buf of size len with writability set according to readonly. buf is interpreted as a sequence of unsigned bytes.

The flags argument indicates the request type. This function always fills in view as specified by flags, unless buf has been designated as read-only and PyBUF_WRITABLE is set in flags.

On success, set view->obj to a new reference to exporter and return 0. Otherwise, raise PyExc_BufferError, set view->obj to NULL and return -1;

If this function is used as part of a getbufferproc, exporter MUST be set to the exporting object and flags must be passed unmodified. Otherwise, exporter MUST be NULL.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

抽象对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

抽象对象层 »

|

旧缓冲协议

3.0 版后已移除.

这些函数是 Python 2 中“旧缓冲协议”API 的组成部分。 在 Python 3 中，此协议已不复存在，但这些函数仍然被公开以便移植 2.x 的代码。 它们被用作 新缓冲协议 的兼容性包装器，但它们并不会在缓冲被导出时向你提供对所获资源的生命周期控制。

因此，推荐你调用 PyObject_GetBuffer() (或者配合 PyArg_ParseTuple() 函数族使用 y* 或 w* 格式码) 来获取一个对象的缓冲视图，并在缓冲视图可被释放时调用 PyBuffer_Release()。

int PyObject_AsCharBuffer(PyObject *obj, const char **buffer, Py_ssize_t *buffer_len)

返回一个可用作基于字符的输入的只读内存地址的指针。 obj 参数必须支持单段字符缓冲接口。 成功时返回 0，将 buffer 设为内存地址并将 buffer_len 设为缓冲区长度。 出错时返回 -1 并设置一个 TypeError。

int PyObject_AsReadBuffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len)

返回一个指向包含任意数据的只读内存地址的指针。 obj 参数必须支持单段可读缓冲接口。 成功时返回 0，将 buffer 设为内存地址并将 buffer_len 设为缓冲区长度。 出错时返回 -1 并设置一个 TypeError。

int PyObject_CheckReadBuffer(PyObject *o)

如果 o 支持单段可读缓冲接口则返回 1。 否则返回 0。 此函数总是会成功执行。

Note that this function tries to get and release a buffer, and exceptions which occur while calling correspoding functions will get suppressed. To get error reporting use PyObject_GetBuffer() instead.

int PyObject_AsWriteBuffer(PyObject *obj, void **buffer, Py_ssize_t *buffer_len)

返回一个指向可写内存地址的指针。 obj 必须支持单段字符缓冲接口。 成功时返回 0，将 buffer 设为内存地址并将 buffer_len 设为缓冲区长度。 出错时返回 -1 并设置一个 TypeError。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

抽象对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

具体的对象层

本章中的函数特定于某些 Python 对象类型。 将错误类型的对象传递给它们并不是一个好主意；如果您从 Python 程序接收到一个对象，但不确定它是否具有正确的类型，则必须首先执行类型检查；例如，要检查对象是否为字典，请使用 PyDict_Check()。 本章的结构类似于 Python 对象类型的“家族树”。

警告

While the functions described in this chapter carefully check the type of the objects which are passed in, many of them do not check for NULL being passed instead of a valid object. Allowing NULL to be passed in can cause memory access violations and immediate termination of the interpreter.

基本对象

本节描述Python类型对象和单一实例对象 象None。

类型对象

None 对象

数值对象

整数型对象

布尔对象

浮点数对象

复数对象

表示复数的C结构体

表示复数的Python对象

序列对象

序列对象的一般操作在前一章中讨论过;本节介绍Python语言固有的特定类型的序列对象。

bytes 对象

字节数组对象

类型检查宏

直接 API 函数

宏

Unicode对象和编码解码器

Unicode对象

Unicode类型

Unicode字符属性

Creating and accessing Unicode strings

Deprecated Py_UNICODE APIs

Locale Encoding

File System Encoding

wchar_t Support

Built-in Codecs

Generic Codecs

UTF-8 Codecs

UTF-32 Codecs

UTF-16 Codecs

UTF-7 Codecs

Unicode-Escape Codecs

Raw-Unicode-Escape Codecs

Latin-1 Codecs

ASCII Codecs

Character Map Codecs

MBCS codecs for Windows

Methods & Slots

Methods and Slot Functions

元组对象

结构序列对象

列表对象

容器对象

字典对象

集合对象

函数对象

函数对象

实例方法对象

方法对象

Cell 对象

代码对象

其他对象

文件对象

模块对象

Initializing C modules

Single-phase initialization

Multi-phase initialization

Low-level module creation functions

Support functions

Module lookup

迭代器对象

描述符对象

切片对象

Ellipsis 对象

MemoryView 对象

弱引用对象

胶囊

生成器对象

协程对象

DateTime 对象

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

类型对象

PyTypeObject

对象的 C 结构用于描述 built-in 类型。

PyObject* PyType_Type

这是属于 type 对象的 type object，它在 Python 层面和 type 是相同的对象。

int PyType_Check(PyObject *o)

如果对象 o 是一个类型对象，包括继承于标准类型对象的类型实例，返回真。在其它所有情况下返回假。

int PyType_CheckExact(PyObject *o)

如果对象 o 是一个类型对象，但不是标准类型对象的子类型时，返回真。在其它所有情况下返回假。

unsigned int PyType_ClearCache()

Clear the internal lookup cache. Return the current version tag.

unsigned long PyType_GetFlags(PyTypeObject* type)

Return the tp_flags member of type. This function is primarily meant for use with Py_LIMITED_API; the individual flag bits are guaranteed to be stable across Python releases, but access to tp_flags itself is not part of the limited API.

3.2 新版功能.

在 3.4 版更改: The return type is now unsigned long rather than long.

void PyType_Modified(PyTypeObject *type)

Invalidate the internal lookup cache for the type and all of its subtypes. This function must be called after any manual modification of the attributes or base classes of the type.

int PyType_HasFeature(PyTypeObject *o, int feature)

Return true if the type object o sets the feature feature. Type features are denoted by single bit flags.

int PyType_IS_GC(PyTypeObject *o)

Return true if the type object includes support for the cycle detector; this tests the type flag Py_TPFLAGS_HAVE_GC.

int PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)

Return true if a is a subtype of b.

This function only checks for actual subtypes, which means that __subclasscheck__() is not called on b. Call PyObject_IsSubclass() to do the same check that issubclass() would do.

PyObject* PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)

Return value: New reference.

Generic handler for the tp_alloc slot of a type object. Use Python’s default memory allocation mechanism to allocate a new instance and initialize all its contents to NULL.

PyObject* PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)

Return value: New reference.

Generic handler for the tp_new slot of a type object. Create a new instance using the type’s tp_alloc slot.

int PyType_Ready(PyTypeObject *type)

Finalize a type object. This should be called on all type objects to finish their initialization. This function is responsible for adding inherited slots from a type’s base class. Return 0 on success, or return -1 and sets an exception on error.

PyObject* PyType_FromSpec(PyType_Spec *spec)

Creates and returns a heap type object from the spec passed to the function.

PyObject* PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)

Creates and returns a heap type object from the spec. In addition to that, the created heap type contains all types contained by the bases tuple as base types. This allows the caller to reference other heap types as base types.

3.3 新版功能.

void* PyType_GetSlot(PyTypeObject *type, int slot)

Return the function pointer stored in the given slot. If the result is NULL, this indicates that either the slot is NULL, or that the function was called with invalid parameters. Callers will typically cast the result pointer into the appropriate function type.

3.4 新版功能.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

None 对象

请注意， None 的 PyTypeObject 不会直接在 Python / C API 中公开。 由于 None 是单例，测试对象标识（在C中使用 == ）就足够了。 由于同样的原因，没有 PyNone_Check() 函数。

PyObject* Py_None

Python None 对象，表示缺乏值。 这个对象没有方法。 它需要像引用计数一样处理任何其他对象。

Py_RETURN_NONE

正确处理来自C函数内的 Py_None 返回（也就是说，增加 None 的引用计数并返回它。）

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

整数型对象

所有整数都使用以任意大小的长整数对象表示。

在出错时，大多数 PyLong_As* API返回 (返回值类型)-1 ，无法与一般的数字区分开来。请使用 PyErr_Occurred() 来区分。

PyLongObject

表示 Python 整数对象的 PyObject 子类型。

PyTypeObject PyLong_Type

这个 PyTypeObject 的实例表示 Python 的整数类型。与Python层中的 int 相同。

int PyLong_Check(PyObject *p)

如果参数是 PyLongObject 或 PyLongObject 的子类型，返回 true 。

int PyLong_CheckExact(PyObject *p)

如果参数是 PyLongObject 但不是 PyLongObject 的子类型，返回 true。

PyObject* PyLong_FromLong(long v)

Return value: New reference.

Return a new PyLongObject object from v, or NULL on failure.

The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object. So it should be possible to change the value of 1. I suspect the behaviour of Python in this case is undefined. :-)

PyObject* PyLong_FromUnsignedLong(unsigned long v)

Return value: New reference.

Return a new PyLongObject object from a C unsigned long, or NULL on failure.

PyObject* PyLong_FromSsize_t(Py_ssize_t v)

Return a new PyLongObject object from a C Py_ssize_t, or NULL on failure.

PyObject* PyLong_FromSize_t(size_t v)

Return a new PyLongObject object from a C size_t, or NULL on failure.

PyObject* PyLong_FromLongLong(long long v)

Return value: New reference.

Return a new PyLongObject object from a C long long, or NULL on failure.

PyObject* PyLong_FromUnsignedLongLong(unsigned long long v)

Return value: New reference.

Return a new PyLongObject object from a C unsigned long long, or NULL on failure.

PyObject* PyLong_FromDouble(double v)

Return value: New reference.

Return a new PyLongObject object from the integer part of v, or NULL on failure.

PyObject* PyLong_FromString(const char *str, char **pend, int base)

Return value: New reference.

Return a new PyLongObject based on the string value in str, which is interpreted according to the radix in base. If pend is non-NULL, *pend will point to the first character in str which follows the representation of the number. If base is 0, str is interpreted using the 整型数字面值 definition; in this case, leading zeros in a non-zero decimal number raises a ValueError. If base is not 0, it must be between 2 and 36, inclusive. Leading spaces and single underscores after a base specifier and between digits are ignored. If there are no digits, ValueError will be raised.

PyObject* PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)

Return value: New reference.

Convert a sequence of Unicode digits to a Python integer value. The Unicode string is first encoded to a byte string using PyUnicode_EncodeDecimal() and then converted using PyLong_FromString().

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Py_UNICODE API; please migrate to using PyLong_FromUnicodeObject().

PyObject* PyLong_FromUnicodeObject(PyObject *u, int base)

Convert a sequence of Unicode digits in the string u to a Python integer value. The Unicode string is first encoded to a byte string using PyUnicode_EncodeDecimal() and then converted using PyLong_FromString().

3.3 新版功能.

PyObject* PyLong_FromVoidPtr(void *p)

Return value: New reference.

从指针 p 创建一个 Python 整数。可以使用 PyLong_AsVoidPtr() 返回的指针值。

long PyLong_AsLong(PyObject *obj)

Return a C long representation of obj. If obj is not an instance of PyLongObject, first call its __int__() method (if present) to convert it to a PyLongObject.

如果 obj 的值溢出了 long 的范围，会抛出 OverflowError。

发生错误时返回 -1 。使用 PyErr_Occurred() 来消歧义。

long PyLong_AsLongAndOverflow(PyObject *obj, int *overflow)

Return a C long representation of obj. If obj is not an instance of PyLongObject, first call its __int__() method (if present) to convert it to a PyLongObject.

If the value of obj is greater than LONG_MAX or less than LONG_MIN, set *overflow to 1 or -1, respectively, and return -1; otherwise, set *overflow to 0. If any other exception occurs set *overflow to 0 and return -1 as usual.

发生错误时返回 -1 。使用 PyErr_Occurred() 来消歧义。

long long PyLong_AsLongLong(PyObject *obj)

Return a C long long representation of obj. If obj is not an instance of PyLongObject, first call its __int__() method (if present) to convert it to a PyLongObject.

如果 obj 的值溢出了 long 的范围，会抛出 OverflowError。

发生错误时返回 -1 。使用 PyErr_Occurred() 来消歧义。

long long PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)

Return a C long long representation of obj. If obj is not an instance of PyLongObject, first call its __int__() method (if present) to convert it to a PyLongObject.

If the value of obj is greater than PY_LLONG_MAX or less than PY_LLONG_MIN, set *overflow to 1 or -1, respectively, and return -1; otherwise, set *overflow to 0. If any other exception occurs set *overflow to 0 and return -1 as usual.

发生错误时返回 -1 。使用 PyErr_Occurred() 来消歧义。

3.2 新版功能.

Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)

Return a C Py_ssize_t representation of pylong. pylong must be an instance of PyLongObject.

Raise OverflowError if the value of pylong is out of range for a Py_ssize_t.

发生错误时返回 -1 。使用 PyErr_Occurred() 来消歧义。

unsigned long PyLong_AsUnsignedLong(PyObject *pylong)

Return a C unsigned long representation of pylong. pylong must be an instance of PyLongObject.

Raise OverflowError if the value of pylong is out of range for a unsigned long.

Returns (unsigned long)-1 on error. Use PyErr_Occurred() to disambiguate.

size_t PyLong_AsSize_t(PyObject *pylong)

Return a C size_t representation of pylong. pylong must be an instance of PyLongObject.

Raise OverflowError if the value of pylong is out of range for a size_t.

Returns (size_t)-1 on error. Use PyErr_Occurred() to disambiguate.

unsigned long long PyLong_AsUnsignedLongLong(PyObject *pylong)

Return a C unsigned long long representation of pylong. pylong must be an instance of PyLongObject.

Raise OverflowError if the value of pylong is out of range for an unsigned long long.

Returns (unsigned long long)-1 on error. Use PyErr_Occurred() to disambiguate.

在 3.1 版更改: A negative pylong now raises OverflowError, not TypeError.

unsigned long PyLong_AsUnsignedLongMask(PyObject *obj)

Return a C unsigned long representation of obj. If obj is not an instance of PyLongObject, first call its __int__() method (if present) to convert it to a PyLongObject.

If the value of obj is out of range for an unsigned long, return the reduction of that value modulo ULONG_MAX + 1.

发生错误时返回 -1 。使用 PyErr_Occurred() 来消歧义。

unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj)

Return a C unsigned long long representation of obj. If obj is not an instance of PyLongObject, first call its __int__() method (if present) to convert it to a PyLongObject.

If the value of obj is out of range for an unsigned long long, return the reduction of that value modulo PY_ULLONG_MAX + 1.

发生错误时返回 -1 。使用 PyErr_Occurred() 来消歧义。

double PyLong_AsDouble(PyObject *pylong)

Return a C double representation of pylong. pylong must be an instance of PyLongObject.

Raise OverflowError if the value of pylong is out of range for a double.

Returns -1.0 on error. Use PyErr_Occurred() to disambiguate.

void* PyLong_AsVoidPtr(PyObject *pylong)

Convert a Python integer pylong to a C void pointer. If pylong cannot be converted, an OverflowError will be raised. This is only assured to produce a usable void pointer for values created with PyLong_FromVoidPtr().

Returns NULL on error. Use PyErr_Occurred() to disambiguate.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

布尔对象

Python 中的布尔值是作为整数的子类实现的。只有 Py_False 和 Py_True 两个布尔值。因此，正常的创建和删除功能不适用于布尔值。但是，下列宏可用。

int PyBool_Check(PyObject *o)

如果 o 是 PyBool_Type 类型，则返回true。

PyObject* Py_False

Python的``False``对象。该对象没有任何方法。它应该象其它使用引用计数管理的对象一样使用。

PyObject* Py_True

Python的``True``对象。该对象没有任何方法。它应该象其它使用引用计数管理的对象一样使用。

Py_RETURN_FALSE

从函数返回 Py_False 时，需要增加它的引用计数。

Py_RETURN_TRUE

从函数返回 Py_True 时，需要增加它的引用计数。

PyObject* PyBool_FromLong(long v)

Return value: New reference.

根据 v 的实际值，返回一个 Py_True 或者 Py_False 的新引用。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

浮点数对象

PyFloatObject

这个C类型 PyObject 的子类型代表一个Python浮点数对象。

PyTypeObject PyFloat_Type

这是个属于C类型 PyTypeObject 的代表Python浮点类型的实例。在Python层面的类型 float 是同一个对象。

int PyFloat_Check(PyObject *p)

当他的参数是一个C类型 PyFloatObject 或者是C类型 PyFloatObject 的子类型时，返回真。

int PyFloat_CheckExact(PyObject *p)

当他的参数是一个C类型 PyFloatObject 但不是C类型 PyFloatObject 的子类型时，返回真。

PyObject* PyFloat_FromString(PyObject *str)

Return value: New reference.

Create a PyFloatObject object based on the string value in str, or NULL on failure.

PyObject* PyFloat_FromDouble(double v)

Return value: New reference.

Create a PyFloatObject object from v, or NULL on failure.

double PyFloat_AsDouble(PyObject *pyfloat)

返回一个代表 pyfloat 内容的C类型 double。如果 float 不是一个Python浮点数对象，但是包含 __float__() 方法，这个方法会首先被调用，将 pyfloat 转换成一个浮点数。失败时这个方法返回 -1.0，所以应该调用C函数 PyErr_Occurred() 检查错误。

double PyFloat_AS_DOUBLE(PyObject *pyfloat)

返回一个 pyfloat 内容的 C double 表示，但没有错误检查。

PyObject* PyFloat_GetInfo(void)

返回一个 structseq 实例，其中包含有关 float 的精度、最小值和最大值的信息。 它是头文件 float.h 的一个简单包装。

double PyFloat_GetMax()

返回最大可表示的有限浮点数 DBL_MAX 为 C double 。

double PyFloat_GetMin()

返回最小可表示归一化正浮点数 DBL_MIN 为 C double 。

int PyFloat_ClearFreeList()

清空浮点数释放列表。 返回无法释放的项目数。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

复数对象

从C API看，Python的复数对象由两个不同的部分实现：一个是在Python程序使用的Python对象，另外的是一个代表真正复数值的C结构体。API提供了函数共同操作两者。

表示复数的C结构体

需要注意的是接受这些结构体的作为参数并当做结果返回的函数，都是传递“值”而不是引用指针。此规则适用于整个API。

Py_complex

这是一个对应Python复数对象的值部分的C结构体。绝大部分处理复数对象的函数都用这类型的结构体作为输入或者输出值，它可近似地定义为：

typedef struct {
   double real;
   double imag;
} Py_complex;

Py_complex _Py_c_sum(Py_complex left, Py_complex right)

返回两个复数的和，用 C 类型 Py_complex 表示。

Py_complex _Py_c_diff(Py_complex left, Py_complex right)

返回两个复数的差，用 C 类型 Py_complex 表示。

Py_complex _Py_c_neg(Py_complex complex)

返回复数 complex 的负值，用 C 类型 Py_complex 表示。

Py_complex _Py_c_prod(Py_complex left, Py_complex right)

返回两个复数的乘积，用 C 类型 Py_complex 表示。

Py_complex _Py_c_quot(Py_complex dividend, Py_complex divisor)

返回两个复数的商，用 C 类型 Py_complex 表示。

如果 divisor 为空，这个方法返回零并设置 errno 为 EDOM。

Py_complex _Py_c_pow(Py_complex num, Py_complex exp)

返回 num 的 exp 次幂，用 C 类型 Py_complex 表示。

如果 num 为空且 exp 不是正实数，这个方法返回零并设置 errno 为 EDOM。

表示复数的Python对象

PyComplexObject

这个C类型 PyObject 的子类型代表一个 Python 复数对象。

PyTypeObject PyComplex_Type

这是个属于 PyTypeObject 的代表Python复数类型的实例。在Python层面的类型 complex 是同一个对象。

int PyComplex_Check(PyObject *p)

如果它的变量是一个C类型 PyComplexObject 或者是C类型 PyComplexObject 的子类型，返回真。

int PyComplex_CheckExact(PyObject *p)

如果它的参数是一个C类型 PyComplexObject 但不是C类型 PyComplexObject 的子类型，返回真。

PyObject* PyComplex_FromCComplex(Py_complex v)

Return value: New reference.

根据C类型 Py_complex 的值生成一个新的Python复数对象。

PyObject* PyComplex_FromDoubles(double real, double imag)

Return value: New reference.

根据 real 和 imag 返回一个新的C类型 PyComplexObject 对象。

double PyComplex_RealAsDouble(PyObject *op)

以C类型 double 返回 op 的实部。

double PyComplex_ImagAsDouble(PyObject *op)

以C类型 double 返回 op 的虚部。

Py_complex PyComplex_AsCComplex(PyObject *op)

返回复数 op 的C类型 Py_complex 值。

如果 op 不是一个Python复数对象，但是有一个 __complex__() 方法，这个方法会首先被调用，将 op 转换成为一个Python复数对象。失败时，此方法返回 -1.0 作为实数值。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

bytes 对象

当期望带一个字节串形参但却带一个非字节串形参被调用时，这些函数会引发 TypeError。

PyBytesObject

这种 PyObject 的子类型表示一个 Python 字节对象。

PyTypeObject PyBytes_Type

PyTypeObject 的实例代表一个 Python 字节类型，在 Python 层面它与 bytes 是相同的对象。

int PyBytes_Check(PyObject *o)

如果对象 o 是字节对象或字节类型的子类型的实例，则返回 true。

int PyBytes_CheckExact(PyObject *o)

如果对象 o 是字节对象，但不是字节类型子类型的实例，则返回 true。

PyObject* PyBytes_FromString(const char *v)

Return a new bytes object with a copy of the string v as value on success, and NULL on failure. The parameter v must not be NULL; it will not be checked.

PyObject* PyBytes_FromStringAndSize(const char *v, Py_ssize_t len)

Return a new bytes object with a copy of the string v as value and length len on success, and NULL on failure. If v is NULL, the contents of the bytes object are uninitialized.

PyObject* PyBytes_FromFormat(const char *format, ...)

接受一个 C printf() 风格的 format 字符串和可变数量的参数，计算结果 Python 字节串对象的大小并返回参数值经格式化后的字节串对象。 可变数量的参数必须均为 C 类型并且必须恰好与 format 字符串中的格式字符相对应。 允许使用下列格式字符串:

格式字符

类型

注释

%%

不适用

文字%字符。

%c

整型

一个字节，被表示为一个 C 语言的整型

%d

整型

Exactly equivalent to printf("%d").

%u

无符号整型

Exactly equivalent to printf("%u").

%ld

长整型

Exactly equivalent to printf("%ld").

%lu

无符号长整型

Exactly equivalent to printf("%lu").

%zd

Py_ssize_t

Exactly equivalent to printf("%zd").

%zu

size_t

Exactly equivalent to printf("%zu").

%i

整型

Exactly equivalent to printf("%i").

%x

整型

Exactly equivalent to printf("%x").

%s

char*

以 null 为终止符的 C 字符数组。

%p

void*

一个 C 指针的十六进制表示形式。 基本等价于 printf("%p") 但它会确保以字面值 0x 开头，不论系统平台上 printf 的输出是什么。

无法识别的格式字符会导致将格式字符串的其余所有内容原样复制到结果对象，并丢弃所有多余的参数。

PyObject* PyBytes_FromFormatV(const char *format, va_list vargs)

与 PyBytes_FromFormat() 完全相同，除了它需要两个参数。

PyObject* PyBytes_FromObject(PyObject *o)

返回字节表示实现缓冲区协议的对象*o*。

Py_ssize_t PyBytes_Size(PyObject *o)

返回字节对象*o*中字节的长度。

Py_ssize_t PyBytes_GET_SIZE(PyObject *o)

宏版本的 PyBytes_Size() 但是不带错误检查。

char* PyBytes_AsString(PyObject *o)

Return a pointer to the contents of o. The pointer refers to the internal buffer of o, which consists of len(o) + 1 bytes. The last byte in the buffer is always null, regardless of whether there are any other null bytes. The data must not be modified in any way, unless the object was just created using PyBytes_FromStringAndSize(NULL, size). It must not be deallocated. If o is not a bytes object at all, PyBytes_AsString() returns NULL and raises TypeError.

char* PyBytes_AS_STRING(PyObject *string)

宏版本的 PyBytes_AsString() 但是不带错误检测。

int PyBytes_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)

通过输出变量 buffer 和 length 返回以 null 为终止符的对象 obj 的内容。

If length is NULL, the bytes object may not contain embedded null bytes; if it does, the function returns -1 and a ValueError is raised.

该缓冲区指向 obj 的内部缓冲，它的末尾包含一个额外的空字节（不算在 length 当中）。 该数据不可通过任何方式来修改，除非是刚使用 PyBytes_FromStringAndSize(NULL, size) 创建该对象。 它不可被撤销分配。 如果 obj 根本不是一个字节串对象，则 PyBytes_AsStringAndSize() 将返回 -1 并引发 TypeError。

在 3.5 版更改: 以前，当字节串对象中出现嵌入的空字节时将引发 TypeError。

void PyBytes_Concat(PyObject **bytes, PyObject *newpart)

Create a new bytes object in *bytes containing the contents of newpart appended to bytes; the caller will own the new reference. The reference to the old value of bytes will be stolen. If the new object cannot be created, the old reference to bytes will still be discarded and the value of *bytes will be set to NULL; the appropriate exception will be set.

void PyBytes_ConcatAndDel(PyObject **bytes, PyObject *newpart)

在 *bytes 中创建新的字节串对象，其中包含添加到 bytes 的 newpart 的内容。 此版本会减少 newpart 的引用计数。

int _PyBytes_Resize(PyObject **bytes, Py_ssize_t newsize)

A way to resize a bytes object even though it is “immutable”. Only use this to build up a brand new bytes object; don’t use this if the bytes may already be known in other parts of the code. It is an error to call this function if the refcount on the input bytes object is not one. Pass the address of an existing bytes object as an lvalue (it may be written into), and the new size desired. On success, *bytes holds the resized bytes object and 0 is returned; the address in *bytes may differ from its input value. If the reallocation fails, the original bytes object at *bytes is deallocated, *bytes is set to NULL, MemoryError is set, and -1 is returned.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

字节数组对象

PyByteArrayObject

这个 PyObject 的子类型表示一个 Python 字节数组对象。

PyTypeObject PyByteArray_Type

Python bytearray 类型表示为 PyTypeObject 的实例；这与Python层面的 bytearray 是相同的对象。

类型检查宏

int PyByteArray_Check(PyObject *o)

当对象 o 是一个字节数组对象而且是一个字节数组类型的子类型实例时，返回真。

int PyByteArray_CheckExact(PyObject *o)

当对象 o 是一个字节数组对象，但不是一个字节数组类型的子类型实例时，返回真。

直接 API 函数

PyObject* PyByteArray_FromObject(PyObject *o)

根据任何实现了 缓冲区协议 的对象 o，返回一个新的字节数组对象。

PyObject* PyByteArray_FromStringAndSize(const char *string, Py_ssize_t len)

Create a new bytearray object from string and its length, len. On failure, NULL is returned.

PyObject* PyByteArray_Concat(PyObject *a, PyObject *b)

连接字节数组 a 和 b 并返回一个带有结果的新的字节数组。

Py_ssize_t PyByteArray_Size(PyObject *bytearray)

Return the size of bytearray after checking for a NULL pointer.

char* PyByteArray_AsString(PyObject *bytearray)

Return the contents of bytearray as a char array after checking for a NULL pointer. The returned array always has an extra null byte appended.

int PyByteArray_Resize(PyObject *bytearray, Py_ssize_t len)

将 bytearray 的内部缓冲区的大小调整为 len。

宏

这些宏减低安全性以换取性能，它们不检查指针。

char* PyByteArray_AS_STRING(PyObject *bytearray)

C函数 PyByteArray_AsString() 的宏版本。

Py_ssize_t PyByteArray_GET_SIZE(PyObject *bytearray)

C函数 PyByteArray_Size() 的宏版本。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

Unicode对象和编码解码器

Unicode对象

自从python3.3中实现了:pep:393 以来，Unicode对象在内部使用各种表示形式，以便在保持内存效率的同时处理完整范围的Unicode字符。对于所有代码点都低于128、256或65536的字符串，有一些特殊情况；否则，代码点必须低于1114112（这是完整的Unicode范围）。

Py_UNICODE* and UTF-8 representations are created on demand and cached in the Unicode object. The Py_UNICODE* representation is deprecated and inefficient; it should be avoided in performance- or memory-sensitive situations.

Due to the transition between the old APIs and the new APIs, unicode objects can internally be in two states depending on how they were created:

“canonical” unicode objects are all objects created by a non-deprecated unicode API. They use the most efficient representation allowed by the implementation.

“legacy” unicode objects have been created through one of the deprecated APIs (typically PyUnicode_FromUnicode()) and only bear the Py_UNICODE* representation; you will have to call PyUnicode_READY() on them before calling any other API.

Unicode类型

These are the basic Unicode object types used for the Unicode implementation in Python:

Py_UCS4

Py_UCS2

Py_UCS1

These types are typedefs for unsigned integer types wide enough to contain characters of 32 bits, 16 bits and 8 bits, respectively. When dealing with single Unicode characters, use Py_UCS4.

3.3 新版功能.

Py_UNICODE

This is a typedef of wchar_t, which is a 16-bit type or 32-bit type depending on the platform.

在 3.3 版更改: In previous versions, this was a 16-bit type or a 32-bit type depending on whether you selected a “narrow” or “wide” Unicode version of Python at build time.

PyASCIIObject

PyCompactUnicodeObject

PyUnicodeObject

These subtypes of PyObject represent a Python Unicode object. In almost all cases, they shouldn’t be used directly, since all API functions that deal with Unicode objects take and return PyObject pointers.

3.3 新版功能.

PyTypeObject PyUnicode_Type

This instance of PyTypeObject represents the Python Unicode type. It is exposed to Python code as str.

The following APIs are really C macros and can be used to do fast checks and to access internal read-only data of Unicode objects:

int PyUnicode_Check(PyObject *o)

Return true if the object o is a Unicode object or an instance of a Unicode subtype.

int PyUnicode_CheckExact(PyObject *o)

Return true if the object o is a Unicode object, but not an instance of a subtype.

int PyUnicode_READY(PyObject *o)

Ensure the string object o is in the “canonical” representation. This is required before using any of the access macros described below.

Returns 0 on success and -1 with an exception set on failure, which in particular happens if memory allocation fails.

3.3 新版功能.

Py_ssize_t PyUnicode_GET_LENGTH(PyObject *o)

Return the length of the Unicode string, in code points. o has to be a Unicode object in the “canonical” representation (not checked).

3.3 新版功能.

Py_UCS1* PyUnicode_1BYTE_DATA(PyObject *o)

Py_UCS2* PyUnicode_2BYTE_DATA(PyObject *o)

Py_UCS4* PyUnicode_4BYTE_DATA(PyObject *o)

Return a pointer to the canonical representation cast to UCS1, UCS2 or UCS4 integer types for direct character access. No checks are performed if the canonical representation has the correct character size; use PyUnicode_KIND() to select the right macro. Make sure PyUnicode_READY() has been called before accessing this.

3.3 新版功能.

PyUnicode_WCHAR_KIND

PyUnicode_1BYTE_KIND

PyUnicode_2BYTE_KIND

PyUnicode_4BYTE_KIND

Return values of the PyUnicode_KIND() macro.

3.3 新版功能.

int PyUnicode_KIND(PyObject *o)

Return one of the PyUnicode kind constants (see above) that indicate how many bytes per character this Unicode object uses to store its data. o has to be a Unicode object in the “canonical” representation (not checked).

3.3 新版功能.

void* PyUnicode_DATA(PyObject *o)

Return a void pointer to the raw unicode buffer. o has to be a Unicode object in the “canonical” representation (not checked).

3.3 新版功能.

void PyUnicode_WRITE(int kind, void *data, Py_ssize_t index, Py_UCS4 value)

Write into a canonical representation data (as obtained with PyUnicode_DATA()). This macro does not do any sanity checks and is intended for usage in loops. The caller should cache the kind value and data pointer as obtained from other macro calls. index is the index in the string (starts at 0) and value is the new code point value which should be written to that location.

3.3 新版功能.

Py_UCS4 PyUnicode_READ(int kind, void *data, Py_ssize_t index)

Read a code point from a canonical representation data (as obtained with PyUnicode_DATA()). No checks or ready calls are performed.

3.3 新版功能.

Py_UCS4 PyUnicode_READ_CHAR(PyObject *o, Py_ssize_t index)

Read a character from a Unicode object o, which must be in the “canonical” representation. This is less efficient than PyUnicode_READ() if you do multiple consecutive reads.

3.3 新版功能.

PyUnicode_MAX_CHAR_VALUE(PyObject *o)

Return the maximum code point that is suitable for creating another string based on o, which must be in the “canonical” representation. This is always an approximation but more efficient than iterating over the string.

3.3 新版功能.

int PyUnicode_ClearFreeList()

清空释放列表。 返回所释放的条目数。

Py_ssize_t PyUnicode_GET_SIZE(PyObject *o)

Return the size of the deprecated Py_UNICODE representation, in code units (this includes surrogate pairs as 2 units). o has to be a Unicode object (not checked).

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Unicode API, please migrate to using PyUnicode_GET_LENGTH().

Py_ssize_t PyUnicode_GET_DATA_SIZE(PyObject *o)

Return the size of the deprecated Py_UNICODE representation in bytes. o has to be a Unicode object (not checked).

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Unicode API, please migrate to using PyUnicode_GET_LENGTH().

Py_UNICODE* PyUnicode_AS_UNICODE(PyObject *o)

const char* PyUnicode_AS_DATA(PyObject *o)

Return a pointer to a Py_UNICODE representation of the object. The returned buffer is always terminated with an extra null code point. It may also contain embedded null code points, which would cause the string to be truncated when used in most C functions. The AS_DATA form casts the pointer to const char *. The o argument has to be a Unicode object (not checked).

在 3.3 版更改: This macro is now inefficient – because in many cases the Py_UNICODE representation does not exist and needs to be created – and can fail (return NULL with an exception set). Try to port the code to use the new PyUnicode_nBYTE_DATA() macros or use PyUnicode_WRITE() or PyUnicode_READ().

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Unicode API, please migrate to using the PyUnicode_nBYTE_DATA() family of macros.

Unicode字符属性

Unicode provides many different character properties. The most often needed ones are available through these macros which are mapped to C functions depending on the Python configuration.

int Py_UNICODE_ISSPACE(Py_UNICODE ch)

Return 1 or 0 depending on whether ch is a whitespace character.

int Py_UNICODE_ISLOWER(Py_UNICODE ch)

Return 1 or 0 depending on whether ch is a lowercase character.

int Py_UNICODE_ISUPPER(Py_UNICODE ch)

Return 1 or 0 depending on whether ch is an uppercase character.

int Py_UNICODE_ISTITLE(Py_UNICODE ch)

Return 1 or 0 depending on whether ch is a titlecase character.

int Py_UNICODE_ISLINEBREAK(Py_UNICODE ch)

Return 1 or 0 depending on whether ch is a linebreak character.

int Py_UNICODE_ISDECIMAL(Py_UNICODE ch)

Return 1 or 0 depending on whether ch is a decimal character.

int Py_UNICODE_ISDIGIT(Py_UNICODE ch)

Return 1 or 0 depending on whether ch is a digit character.

int Py_UNICODE_ISNUMERIC(Py_UNICODE ch)

Return 1 or 0 depending on whether ch is a numeric character.

int Py_UNICODE_ISALPHA(Py_UNICODE ch)

Return 1 or 0 depending on whether ch is an alphabetic character.

int Py_UNICODE_ISALNUM(Py_UNICODE ch)

Return 1 or 0 depending on whether ch is an alphanumeric character.

int Py_UNICODE_ISPRINTABLE(Py_UNICODE ch)

Return 1 or 0 depending on whether ch is a printable character. Nonprintable characters are those characters defined in the Unicode character database as “Other” or “Separator”, excepting the ASCII space (0x20) which is considered printable. (Note that printable characters in this context are those which should not be escaped when repr() is invoked on a string. It has no bearing on the handling of strings written to sys.stdout or sys.stderr.)

These APIs can be used for fast direct character conversions:

Py_UNICODE Py_UNICODE_TOLOWER(Py_UNICODE ch)

Return the character ch converted to lower case.

3.3 版后已移除: This function uses simple case mappings.

Py_UNICODE Py_UNICODE_TOUPPER(Py_UNICODE ch)

Return the character ch converted to upper case.

3.3 版后已移除: This function uses simple case mappings.

Py_UNICODE Py_UNICODE_TOTITLE(Py_UNICODE ch)

Return the character ch converted to title case.

3.3 版后已移除: This function uses simple case mappings.

int Py_UNICODE_TODECIMAL(Py_UNICODE ch)

Return the character ch converted to a decimal positive integer. Return -1 if this is not possible. This macro does not raise exceptions.

int Py_UNICODE_TODIGIT(Py_UNICODE ch)

Return the character ch converted to a single digit integer. Return -1 if this is not possible. This macro does not raise exceptions.

double Py_UNICODE_TONUMERIC(Py_UNICODE ch)

Return the character ch converted to a double. Return -1.0 if this is not possible. This macro does not raise exceptions.

These APIs can be used to work with surrogates:

Py_UNICODE_IS_SURROGATE(ch)

Check if ch is a surrogate (0xD800 <= ch <= 0xDFFF).

Py_UNICODE_IS_HIGH_SURROGATE(ch)

Check if ch is a high surrogate (0xD800 <= ch <= 0xDBFF).

Py_UNICODE_IS_LOW_SURROGATE(ch)

Check if ch is a low surrogate (0xDC00 <= ch <= 0xDFFF).

Py_UNICODE_JOIN_SURROGATES(high, low)

Join two surrogate characters and return a single Py_UCS4 value. high and low are respectively the leading and trailing surrogates in a surrogate pair.

Creating and accessing Unicode strings

To create Unicode objects and access their basic sequence properties, use these APIs:

PyObject* PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar)

Create a new Unicode object. maxchar should be the true maximum code point to be placed in the string. As an approximation, it can be rounded up to the nearest value in the sequence 127, 255, 65535, 1114111.

This is the recommended way to allocate a new Unicode object. Objects created using this function are not resizable.

3.3 新版功能.

PyObject* PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size)

Create a new Unicode object with the given kind (possible values are PyUnicode_1BYTE_KIND etc., as returned by PyUnicode_KIND()). The buffer must point to an array of size units of 1, 2 or 4 bytes per character, as given by the kind.

3.3 新版功能.

PyObject* PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)

Create a Unicode object from the char buffer u. The bytes will be interpreted as being UTF-8 encoded. The buffer is copied into the new object. If the buffer is not NULL, the return value might be a shared object, i.e. modification of the data is not allowed.

If u is NULL, this function behaves like PyUnicode_FromUnicode() with the buffer set to NULL. This usage is deprecated in favor of PyUnicode_New().

PyObject *PyUnicode_FromString(const char *u)

Create a Unicode object from a UTF-8 encoded null-terminated char buffer u.

PyObject* PyUnicode_FromFormat(const char *format, ...)

Take a C printf()-style format string and a variable number of arguments, calculate the size of the resulting Python unicode string and return a string with the values formatted into it. The variable arguments must be C types and must correspond exactly to the format characters in the format ASCII-encoded string. The following format characters are allowed:

格式字符

类型

注释

%%

不适用

文字%字符。

%c

整型

单个字符，表示为C 语言的整型。

%d

整型

Exactly equivalent to printf("%d").

%u

无符号整型

Exactly equivalent to printf("%u").

%ld

长整型

Exactly equivalent to printf("%ld").

%li

长整型

Exactly equivalent to printf("%li").

%lu

无符号长整型

Exactly equivalent to printf("%lu").

%lld

long long

Exactly equivalent to printf("%lld").

%lli

long long

Exactly equivalent to printf("%lli").

%llu

无符号 long long

Exactly equivalent to printf("%llu").

%zd

Py_ssize_t

Exactly equivalent to printf("%zd").

%zi

Py_ssize_t

Exactly equivalent to printf("%zi").

%zu

size_t

Exactly equivalent to printf("%zu").

%i

整型

Exactly equivalent to printf("%i").

%x

整型

Exactly equivalent to printf("%x").

%s

char*

以 null 为终止符的 C 字符数组。

%p

void*

一个 C 指针的十六进制表示形式。 基本等价于 printf("%p") 但它会确保以字面值 0x 开头，不论系统平台上 printf 的输出是什么。

%A

PyObject*

ascii() 调用的结果。

%U

PyObject*

A unicode object.

%V

PyObject*, char *

A unicode object (which may be NULL) and a null-terminated C character array as a second parameter (which will be used, if the first parameter is NULL).

%S

PyObject*

The result of calling PyObject_Str().

%R

PyObject*

The result of calling PyObject_Repr().

An unrecognized format character causes all the rest of the format string to be copied as-is to the result string, and any extra arguments discarded.

注解

The width formatter unit is number of characters rather than bytes. The precision formatter unit is number of bytes for "%s" and "%V" (if the PyObject* argument is NULL), and a number of characters for "%A", "%U", "%S", "%R" and "%V" (if the PyObject* argument is not NULL).

在 3.2 版更改: Support for "%lld" and "%llu" added.

在 3.3 版更改: Support for "%li", "%lli" and "%zi" added.

在 3.4 版更改: Support width and precision formatter for "%s", "%A", "%U", "%V", "%S", "%R" added.

PyObject* PyUnicode_FromFormatV(const char *format, va_list vargs)

Identical to PyUnicode_FromFormat() except that it takes exactly two arguments.

PyObject* PyUnicode_FromEncodedObject(PyObject *obj, const char *encoding, const char *errors)

Return value: New reference.

Decode an encoded object obj to a Unicode object.

bytes, bytearray and other bytes-like objects are decoded according to the given encoding and using the error handling defined by errors. Both can be NULL to have the interface use the default values (see Built-in Codecs for details).

All other objects, including Unicode objects, cause a TypeError to be set.

The API returns NULL if there was an error. The caller is responsible for decref’ing the returned objects.

Py_ssize_t PyUnicode_GetLength(PyObject *unicode)

Return the length of the Unicode object, in code points.

3.3 新版功能.

Py_ssize_t PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, PyObject *from, Py_ssize_t from_start, Py_ssize_t how_many)

Copy characters from one Unicode object into another. This function performs character conversion when necessary and falls back to memcpy() if possible. Returns -1 and sets an exception on error, otherwise returns the number of copied characters.

3.3 新版功能.

Py_ssize_t PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length, Py_UCS4 fill_char)

Fill a string with a character: write fill_char into unicode[start:start+length].

Fail if fill_char is bigger than the string maximum character, or if the string has more than 1 reference.

Return the number of written character, or return -1 and raise an exception on error.

3.3 新版功能.

int PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 character)

Write a character to a string. The string must have been created through PyUnicode_New(). Since Unicode strings are supposed to be immutable, the string must not be shared, or have been hashed yet.

This function checks that unicode is a Unicode object, that the index is not out of bounds, and that the object can be modified safely (i.e. that it its reference count is one).

3.3 新版功能.

Py_UCS4 PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index)

Read a character from a string. This function checks that unicode is a Unicode object and the index is not out of bounds, in contrast to the macro version PyUnicode_READ_CHAR().

3.3 新版功能.

PyObject* PyUnicode_Substring(PyObject *str, Py_ssize_t start, Py_ssize_t end)

Return a substring of str, from character index start (included) to character index end (excluded). Negative indices are not supported.

3.3 新版功能.

Py_UCS4* PyUnicode_AsUCS4(PyObject *u, Py_UCS4 *buffer, Py_ssize_t buflen, int copy_null)

Copy the string u into a UCS4 buffer, including a null character, if copy_null is set. Returns NULL and sets an exception on error (in particular, a SystemError if buflen is smaller than the length of u). buffer is returned on success.

3.3 新版功能.

Py_UCS4* PyUnicode_AsUCS4Copy(PyObject *u)

Copy the string u into a new UCS4 buffer that is allocated using PyMem_Malloc(). If this fails, NULL is returned with a MemoryError set. The returned buffer always has an extra null code point appended.

3.3 新版功能.

Deprecated Py_UNICODE APIs

Deprecated since version 3.3, will be removed in version 4.0.

These API functions are deprecated with the implementation of PEP 393 [https://www.python.org/dev/peps/pep-0393]. Extension modules can continue using them, as they will not be removed in Python 3.x, but need to be aware that their use can now cause performance and memory hits.

PyObject* PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size)

Return value: New reference.

Create a Unicode object from the Py_UNICODE buffer u of the given size. u may be NULL which causes the contents to be undefined. It is the user’s responsibility to fill in the needed data. The buffer is copied into the new object.

If the buffer is not NULL, the return value might be a shared object. Therefore, modification of the resulting Unicode object is only allowed when u is NULL.

If the buffer is NULL, PyUnicode_READY() must be called once the string content has been filled before using any of the access macros such as PyUnicode_KIND().

Please migrate to using PyUnicode_FromKindAndData(), PyUnicode_FromWideChar() or PyUnicode_New().

Py_UNICODE* PyUnicode_AsUnicode(PyObject *unicode)

Return a read-only pointer to the Unicode object’s internal Py_UNICODE buffer, or NULL on error. This will create the Py_UNICODE* representation of the object if it is not yet available. The buffer is always terminated with an extra null code point. Note that the resulting Py_UNICODE string may also contain embedded null code points, which would cause the string to be truncated when used in most C functions.

Please migrate to using PyUnicode_AsUCS4(), PyUnicode_AsWideChar(), PyUnicode_ReadChar() or similar new APIs.

PyObject* PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, Py_ssize_t size)

Create a Unicode object by replacing all decimal digits in Py_UNICODE buffer of the given size by ASCII digits 0–9 according to their decimal value. Return NULL if an exception occurs.

Py_UNICODE* PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size)

Like PyUnicode_AsUnicode(), but also saves the Py_UNICODE() array length (excluding the extra null terminator) in size. Note that the resulting Py_UNICODE* string may contain embedded null code points, which would cause the string to be truncated when used in most C functions.

3.3 新版功能.

Py_UNICODE* PyUnicode_AsUnicodeCopy(PyObject *unicode)

Create a copy of a Unicode string ending with a null code point. Return NULL and raise a MemoryError exception on memory allocation failure, otherwise return a new allocated buffer (use PyMem_Free() to free the buffer). Note that the resulting Py_UNICODE* string may contain embedded null code points, which would cause the string to be truncated when used in most C functions.

3.2 新版功能.

Please migrate to using PyUnicode_AsUCS4Copy() or similar new APIs.

Py_ssize_t PyUnicode_GetSize(PyObject *unicode)

Return the size of the deprecated Py_UNICODE representation, in code units (this includes surrogate pairs as 2 units).

Please migrate to using PyUnicode_GetLength().

PyObject* PyUnicode_FromObject(PyObject *obj)

Return value: New reference.

Copy an instance of a Unicode subtype to a new true Unicode object if necessary. If obj is already a true Unicode object (not a subtype), return the reference with incremented refcount.

Objects other than Unicode or its subtypes will cause a TypeError.

Locale Encoding

The current locale encoding can be used to decode text from the operating system.

PyObject* PyUnicode_DecodeLocaleAndSize(const char *str, Py_ssize_t len, const char *errors)

Decode a string from the current locale encoding. The supported error handlers are "strict" and "surrogateescape" (PEP 383 [https://www.python.org/dev/peps/pep-0383]). The decoder uses "strict" error handler if errors is NULL. str must end with a null character but cannot contain embedded null characters.

Use PyUnicode_DecodeFSDefaultAndSize() to decode a string from Py_FileSystemDefaultEncoding (the locale encoding read at Python startup).

参见

The Py_DecodeLocale() function.

3.3 新版功能.

在 3.6.5 版更改: The function now also uses the current locale encoding for the surrogateescape error handler. Previously, Py_DecodeLocale() was used for the surrogateescape, and the current locale encoding was used for strict.

PyObject* PyUnicode_DecodeLocale(const char *str, const char *errors)

Similar to PyUnicode_DecodeLocaleAndSize(), but compute the string length using strlen().

3.3 新版功能.

PyObject* PyUnicode_EncodeLocale(PyObject *unicode, const char *errors)

Encode a Unicode object to the current locale encoding. The supported error handlers are "strict" and "surrogateescape" (PEP 383 [https://www.python.org/dev/peps/pep-0383]). The encoder uses "strict" error handler if errors is NULL. Return a bytes object. unicode cannot contain embedded null characters.

Use PyUnicode_EncodeFSDefault() to encode a string to Py_FileSystemDefaultEncoding (the locale encoding read at Python startup).

参见

The Py_EncodeLocale() function.

3.3 新版功能.

在 3.6.5 版更改: The function now also uses the current locale encoding for the surrogateescape error handler. Previously, Py_EncodeLocale() was used for the surrogateescape, and the current locale encoding was used for strict.

File System Encoding

To encode and decode file names and other environment strings, Py_FileSystemDefaultEncoding should be used as the encoding, and Py_FileSystemDefaultEncodeErrors should be used as the error handler (PEP 383 [https://www.python.org/dev/peps/pep-0383] and PEP 529 [https://www.python.org/dev/peps/pep-0529]). To encode file names to bytes during argument parsing, the "O&" converter should be used, passing PyUnicode_FSConverter() as the conversion function:

int PyUnicode_FSConverter(PyObject* obj, void* result)

ParseTuple converter: encode str objects – obtained directly or through the os.PathLike interface – to bytes using PyUnicode_EncodeFSDefault(); bytes objects are output as-is. result must be a PyBytesObject* which must be released when it is no longer used.

3.1 新版功能.

在 3.6 版更改: 接受一个 path-like object。

To decode file names to str during argument parsing, the "O&" converter should be used, passing PyUnicode_FSDecoder() as the conversion function:

int PyUnicode_FSDecoder(PyObject* obj, void* result)

ParseTuple converter: decode bytes objects – obtained either directly or indirectly through the os.PathLike interface – to str using PyUnicode_DecodeFSDefaultAndSize(); str objects are output as-is. result must be a PyUnicodeObject* which must be released when it is no longer used.

3.2 新版功能.

在 3.6 版更改: 接受一个 path-like object。

PyObject* PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)

Decode a string using Py_FileSystemDefaultEncoding and the Py_FileSystemDefaultEncodeErrors error handler.

If Py_FileSystemDefaultEncoding is not set, fall back to the locale encoding.

Py_FileSystemDefaultEncoding is initialized at startup from the locale encoding and cannot be modified later. If you need to decode a string from the current locale encoding, use PyUnicode_DecodeLocaleAndSize().

参见

The Py_DecodeLocale() function.

在 3.6 版更改: Use Py_FileSystemDefaultEncodeErrors error handler.

PyObject* PyUnicode_DecodeFSDefault(const char *s)

Decode a null-terminated string using Py_FileSystemDefaultEncoding and the Py_FileSystemDefaultEncodeErrors error handler.

If Py_FileSystemDefaultEncoding is not set, fall back to the locale encoding.

Use PyUnicode_DecodeFSDefaultAndSize() if you know the string length.

在 3.6 版更改: Use Py_FileSystemDefaultEncodeErrors error handler.

PyObject* PyUnicode_EncodeFSDefault(PyObject *unicode)

Encode a Unicode object to Py_FileSystemDefaultEncoding with the Py_FileSystemDefaultEncodeErrors error handler, and return bytes. Note that the resulting bytes object may contain null bytes.

If Py_FileSystemDefaultEncoding is not set, fall back to the locale encoding.

Py_FileSystemDefaultEncoding is initialized at startup from the locale encoding and cannot be modified later. If you need to encode a string to the current locale encoding, use PyUnicode_EncodeLocale().

参见

The Py_EncodeLocale() function.

3.2 新版功能.

在 3.6 版更改: Use Py_FileSystemDefaultEncodeErrors error handler.

wchar_t Support

wchar_t support for platforms which support it:

PyObject* PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size)

Return value: New reference.

Create a Unicode object from the wchar_t buffer w of the given size. Passing -1 as the size indicates that the function must itself compute the length, using wcslen. Return NULL on failure.

Py_ssize_t PyUnicode_AsWideChar(PyObject *unicode, wchar_t *w, Py_ssize_t size)

Copy the Unicode object contents into the wchar_t buffer w. At most size wchar_t characters are copied (excluding a possibly trailing null termination character). Return the number of wchar_t characters copied or -1 in case of an error. Note that the resulting wchar_t* string may or may not be null-terminated. It is the responsibility of the caller to make sure that the wchar_t* string is null-terminated in case this is required by the application. Also, note that the wchar_t* string might contain null characters, which would cause the string to be truncated when used with most C functions.

wchar_t* PyUnicode_AsWideCharString(PyObject *unicode, Py_ssize_t *size)

Convert the Unicode object to a wide character string. The output string always ends with a null character. If size is not NULL, write the number of wide characters (excluding the trailing null termination character) into *size.

Returns a buffer allocated by PyMem_Alloc() (use PyMem_Free() to free it) on success. On error, returns NULL, *size is undefined and raises a MemoryError. Note that the resulting wchar_t string might contain null characters, which would cause the string to be truncated when used with most C functions.

3.2 新版功能.

Built-in Codecs

Python provides a set of built-in codecs which are written in C for speed. All of these codecs are directly usable via the following functions.

Many of the following APIs take two arguments encoding and errors, and they have the same semantics as the ones of the built-in str() string object constructor.

Setting encoding to NULL causes the default encoding to be used which is ASCII. The file system calls should use PyUnicode_FSConverter() for encoding file names. This uses the variable Py_FileSystemDefaultEncoding internally. This variable should be treated as read-only: on some systems, it will be a pointer to a static string, on others, it will change at run-time (such as when the application invokes setlocale).

Error handling is set by errors which may also be set to NULL meaning to use the default handling defined for the codec. Default error handling for all built-in codecs is “strict” (ValueError is raised).

The codecs all use a similar interface. Only deviation from the following generic ones are documented for simplicity.

Generic Codecs

These are the generic codec APIs:

PyObject* PyUnicode_Decode(const char *s, Py_ssize_t size, const char *encoding, const char *errors)

Return value: New reference.

Create a Unicode object by decoding size bytes of the encoded string s. encoding and errors have the same meaning as the parameters of the same name in the str() built-in function. The codec to be used is looked up using the Python codec registry. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_AsEncodedString(PyObject *unicode, const char *encoding, const char *errors)

Return value: New reference.

Encode a Unicode object and return the result as Python bytes object. encoding and errors have the same meaning as the parameters of the same name in the Unicode encode() method. The codec to be used is looked up using the Python codec registry. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_Encode(const Py_UNICODE *s, Py_ssize_t size, const char *encoding, const char *errors)

Return value: New reference.

Encode the Py_UNICODE buffer s of the given size and return a Python bytes object. encoding and errors have the same meaning as the parameters of the same name in the Unicode encode() method. The codec to be used is looked up using the Python codec registry. Return NULL if an exception was raised by the codec.

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Py_UNICODE API; please migrate to using PyUnicode_AsEncodedString().

UTF-8 Codecs

These are the UTF-8 codec APIs:

PyObject* PyUnicode_DecodeUTF8(const char *s, Py_ssize_t size, const char *errors)

Return value: New reference.

Create a Unicode object by decoding size bytes of the UTF-8 encoded string s. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_DecodeUTF8Stateful(const char *s, Py_ssize_t size, const char *errors, Py_ssize_t *consumed)

Return value: New reference.

If consumed is NULL, behave like PyUnicode_DecodeUTF8(). If consumed is not NULL, trailing incomplete UTF-8 byte sequences will not be treated as an error. Those bytes will not be decoded and the number of bytes that have been decoded will be stored in consumed.

PyObject* PyUnicode_AsUTF8String(PyObject *unicode)

Return value: New reference.

Encode a Unicode object using UTF-8 and return the result as Python bytes object. Error handling is “strict”. Return NULL if an exception was raised by the codec.

char* PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *size)

Return a pointer to the UTF-8 encoding of the Unicode object, and store the size of the encoded representation (in bytes) in size. The size argument can be NULL; in this case no size will be stored. The returned buffer always has an extra null byte appended (not included in size), regardless of whether there are any other null code points.

In the case of an error, NULL is returned with an exception set and no size is stored.

This caches the UTF-8 representation of the string in the Unicode object, and subsequent calls will return a pointer to the same buffer. The caller is not responsible for deallocating the buffer.

3.3 新版功能.

char* PyUnicode_AsUTF8(PyObject *unicode)

As PyUnicode_AsUTF8AndSize(), but does not store the size.

3.3 新版功能.

PyObject* PyUnicode_EncodeUTF8(const Py_UNICODE *s, Py_ssize_t size, const char *errors)

Return value: New reference.

Encode the Py_UNICODE buffer s of the given size using UTF-8 and return a Python bytes object. Return NULL if an exception was raised by the codec.

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Py_UNICODE API; please migrate to using PyUnicode_AsUTF8String(), PyUnicode_AsUTF8AndSize() or PyUnicode_AsEncodedString().

UTF-32 Codecs

These are the UTF-32 codec APIs:

PyObject* PyUnicode_DecodeUTF32(const char *s, Py_ssize_t size, const char *errors, int *byteorder)

Decode size bytes from a UTF-32 encoded buffer string and return the corresponding Unicode object. errors (if non-NULL) defines the error handling. It defaults to “strict”.

If byteorder is non-NULL, the decoder starts decoding using the given byte order:

*byteorder == -1: little endian
*byteorder == 0:  native order
*byteorder == 1:  big endian

If *byteorder is zero, and the first four bytes of the input data are a byte order mark (BOM), the decoder switches to this byte order and the BOM is not copied into the resulting Unicode string. If *byteorder is -1 or 1, any byte order mark is copied to the output.

After completion, *byteorder is set to the current byte order at the end of input data.

If byteorder is NULL, the codec starts in native order mode.

Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_DecodeUTF32Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed)

If consumed is NULL, behave like PyUnicode_DecodeUTF32(). If consumed is not NULL, PyUnicode_DecodeUTF32Stateful() will not treat trailing incomplete UTF-32 byte sequences (such as a number of bytes not divisible by four) as an error. Those bytes will not be decoded and the number of bytes that have been decoded will be stored in consumed.

PyObject* PyUnicode_AsUTF32String(PyObject *unicode)

Return a Python byte string using the UTF-32 encoding in native byte order. The string always starts with a BOM mark. Error handling is “strict”. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_EncodeUTF32(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder)

Return a Python bytes object holding the UTF-32 encoded value of the Unicode data in s. Output is written according to the following byte order:

byteorder == -1: little endian
byteorder == 0:  native byte order (writes a BOM mark)
byteorder == 1:  big endian

If byteorder is 0, the output string will always start with the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is prepended.

If Py_UNICODE_WIDE is not defined, surrogate pairs will be output as a single code point.

Return NULL if an exception was raised by the codec.

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Py_UNICODE API; please migrate to using PyUnicode_AsUTF32String() or PyUnicode_AsEncodedString().

UTF-16 Codecs

These are the UTF-16 codec APIs:

PyObject* PyUnicode_DecodeUTF16(const char *s, Py_ssize_t size, const char *errors, int *byteorder)

Return value: New reference.

Decode size bytes from a UTF-16 encoded buffer string and return the corresponding Unicode object. errors (if non-NULL) defines the error handling. It defaults to “strict”.

If byteorder is non-NULL, the decoder starts decoding using the given byte order:

*byteorder == -1: little endian
*byteorder == 0:  native order
*byteorder == 1:  big endian

If *byteorder is zero, and the first two bytes of the input data are a byte order mark (BOM), the decoder switches to this byte order and the BOM is not copied into the resulting Unicode string. If *byteorder is -1 or 1, any byte order mark is copied to the output (where it will result in either a \ufeff or a \ufffe character).

After completion, *byteorder is set to the current byte order at the end of input data.

If byteorder is NULL, the codec starts in native order mode.

Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_DecodeUTF16Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed)

Return value: New reference.

If consumed is NULL, behave like PyUnicode_DecodeUTF16(). If consumed is not NULL, PyUnicode_DecodeUTF16Stateful() will not treat trailing incomplete UTF-16 byte sequences (such as an odd number of bytes or a split surrogate pair) as an error. Those bytes will not be decoded and the number of bytes that have been decoded will be stored in consumed.

PyObject* PyUnicode_AsUTF16String(PyObject *unicode)

Return value: New reference.

Return a Python byte string using the UTF-16 encoding in native byte order. The string always starts with a BOM mark. Error handling is “strict”. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_EncodeUTF16(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder)

Return value: New reference.

Return a Python bytes object holding the UTF-16 encoded value of the Unicode data in s. Output is written according to the following byte order:

byteorder == -1: little endian
byteorder == 0:  native byte order (writes a BOM mark)
byteorder == 1:  big endian

If byteorder is 0, the output string will always start with the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is prepended.

If Py_UNICODE_WIDE is defined, a single Py_UNICODE value may get represented as a surrogate pair. If it is not defined, each Py_UNICODE values is interpreted as a UCS-2 character.

Return NULL if an exception was raised by the codec.

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Py_UNICODE API; please migrate to using PyUnicode_AsUTF16String() or PyUnicode_AsEncodedString().

UTF-7 Codecs

These are the UTF-7 codec APIs:

PyObject* PyUnicode_DecodeUTF7(const char *s, Py_ssize_t size, const char *errors)

Create a Unicode object by decoding size bytes of the UTF-7 encoded string s. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_DecodeUTF7Stateful(const char *s, Py_ssize_t size, const char *errors, Py_ssize_t *consumed)

If consumed is NULL, behave like PyUnicode_DecodeUTF7(). If consumed is not NULL, trailing incomplete UTF-7 base-64 sections will not be treated as an error. Those bytes will not be decoded and the number of bytes that have been decoded will be stored in consumed.

PyObject* PyUnicode_EncodeUTF7(const Py_UNICODE *s, Py_ssize_t size, int base64SetO, int base64WhiteSpace, const char *errors)

Encode the Py_UNICODE buffer of the given size using UTF-7 and return a Python bytes object. Return NULL if an exception was raised by the codec.

If base64SetO is nonzero, “Set O” (punctuation that has no otherwise special meaning) will be encoded in base-64. If base64WhiteSpace is nonzero, whitespace will be encoded in base-64. Both are set to zero for the Python “utf-7” codec.

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Py_UNICODE API; please migrate to using PyUnicode_AsEncodedString().

Unicode-Escape Codecs

These are the “Unicode Escape” codec APIs:

PyObject* PyUnicode_DecodeUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)

Return value: New reference.

Create a Unicode object by decoding size bytes of the Unicode-Escape encoded string s. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_AsUnicodeEscapeString(PyObject *unicode)

Return value: New reference.

Encode a Unicode object using Unicode-Escape and return the result as a bytes object. Error handling is “strict”. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)

Return value: New reference.

Encode the Py_UNICODE buffer of the given size using Unicode-Escape and return a bytes object. Return NULL if an exception was raised by the codec.

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Py_UNICODE API; please migrate to using PyUnicode_AsUnicodeEscapeString().

Raw-Unicode-Escape Codecs

These are the “Raw Unicode Escape” codec APIs:

PyObject* PyUnicode_DecodeRawUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)

Return value: New reference.

Create a Unicode object by decoding size bytes of the Raw-Unicode-Escape encoded string s. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)

Return value: New reference.

Encode a Unicode object using Raw-Unicode-Escape and return the result as a bytes object. Error handling is “strict”. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)

Return value: New reference.

Encode the Py_UNICODE buffer of the given size using Raw-Unicode-Escape and return a bytes object. Return NULL if an exception was raised by the codec.

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Py_UNICODE API; please migrate to using PyUnicode_AsRawUnicodeEscapeString() or PyUnicode_AsEncodedString().

Latin-1 Codecs

These are the Latin-1 codec APIs: Latin-1 corresponds to the first 256 Unicode ordinals and only these are accepted by the codecs during encoding.

PyObject* PyUnicode_DecodeLatin1(const char *s, Py_ssize_t size, const char *errors)

Return value: New reference.

Create a Unicode object by decoding size bytes of the Latin-1 encoded string s. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_AsLatin1String(PyObject *unicode)

Return value: New reference.

Encode a Unicode object using Latin-1 and return the result as Python bytes object. Error handling is “strict”. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_EncodeLatin1(const Py_UNICODE *s, Py_ssize_t size, const char *errors)

Return value: New reference.

Encode the Py_UNICODE buffer of the given size using Latin-1 and return a Python bytes object. Return NULL if an exception was raised by the codec.

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Py_UNICODE API; please migrate to using PyUnicode_AsLatin1String() or PyUnicode_AsEncodedString().

ASCII Codecs

These are the ASCII codec APIs. Only 7-bit ASCII data is accepted. All other codes generate errors.

PyObject* PyUnicode_DecodeASCII(const char *s, Py_ssize_t size, const char *errors)

Return value: New reference.

Create a Unicode object by decoding size bytes of the ASCII encoded string s. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_AsASCIIString(PyObject *unicode)

Return value: New reference.

Encode a Unicode object using ASCII and return the result as Python bytes object. Error handling is “strict”. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_EncodeASCII(const Py_UNICODE *s, Py_ssize_t size, const char *errors)

Return value: New reference.

Encode the Py_UNICODE buffer of the given size using ASCII and return a Python bytes object. Return NULL if an exception was raised by the codec.

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Py_UNICODE API; please migrate to using PyUnicode_AsASCIIString() or PyUnicode_AsEncodedString().

Character Map Codecs

This codec is special in that it can be used to implement many different codecs (and this is in fact what was done to obtain most of the standard codecs included in the encodings package). The codec uses mapping to encode and decode characters. The mapping objects provided must support the __getitem__() mapping interface; dictionaries and sequences work well.

These are the mapping codec APIs:

PyObject* PyUnicode_DecodeCharmap(const char *data, Py_ssize_t size, PyObject *mapping, const char *errors)

Return value: New reference.

Create a Unicode object by decoding size bytes of the encoded string s using the given mapping object. Return NULL if an exception was raised by the codec.

If mapping is NULL, Latin-1 decoding will be applied. Else mapping must map bytes ordinals (integers in the range from 0 to 255) to Unicode strings, integers (which are then interpreted as Unicode ordinals) or None. Unmapped data bytes – ones which cause a LookupError, as well as ones which get mapped to None, 0xFFFE or '\ufffe', are treated as undefined mappings and cause an error.

PyObject* PyUnicode_AsCharmapString(PyObject *unicode, PyObject *mapping)

Return value: New reference.

Encode a Unicode object using the given mapping object and return the result as a bytes object. Error handling is “strict”. Return NULL if an exception was raised by the codec.

The mapping object must map Unicode ordinal integers to bytes objects, integers in the range from 0 to 255 or None. Unmapped character ordinals (ones which cause a LookupError) as well as mapped to None are treated as “undefined mapping” and cause an error.

PyObject* PyUnicode_EncodeCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *mapping, const char *errors)

Return value: New reference.

Encode the Py_UNICODE buffer of the given size using the given mapping object and return the result as a bytes object. Return NULL if an exception was raised by the codec.

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Py_UNICODE API; please migrate to using PyUnicode_AsCharmapString() or PyUnicode_AsEncodedString().

The following codec API is special in that maps Unicode to Unicode.

PyObject* PyUnicode_Translate(PyObject *unicode, PyObject *mapping, const char *errors)

Return value: New reference.

Translate a Unicode object using the given mapping object and return the resulting Unicode object. Return NULL if an exception was raised by the codec.

The mapping object must map Unicode ordinal integers to Unicode strings, integers (which are then interpreted as Unicode ordinals) or None (causing deletion of the character). Unmapped character ordinals (ones which cause a LookupError) are left untouched and are copied as-is.

PyObject* PyUnicode_TranslateCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *mapping, const char *errors)

Return value: New reference.

Translate a Py_UNICODE buffer of the given size by applying a character mapping table to it and return the resulting Unicode object. Return NULL when an exception was raised by the codec.

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Py_UNICODE API; please migrate to using PyUnicode_Translate(). or generic codec based API

MBCS codecs for Windows

These are the MBCS codec APIs. They are currently only available on Windows and use the Win32 MBCS converters to implement the conversions. Note that MBCS (or DBCS) is a class of encodings, not just one. The target encoding is defined by the user settings on the machine running the codec.

PyObject* PyUnicode_DecodeMBCS(const char *s, Py_ssize_t size, const char *errors)

Return value: New reference.

Create a Unicode object by decoding size bytes of the MBCS encoded string s. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_DecodeMBCSStateful(const char *s, Py_ssize_t size, const char *errors, Py_ssize_t *consumed)

If consumed is NULL, behave like PyUnicode_DecodeMBCS(). If consumed is not NULL, PyUnicode_DecodeMBCSStateful() will not decode trailing lead byte and the number of bytes that have been decoded will be stored in consumed.

PyObject* PyUnicode_AsMBCSString(PyObject *unicode)

Return value: New reference.

Encode a Unicode object using MBCS and return the result as Python bytes object. Error handling is “strict”. Return NULL if an exception was raised by the codec.

PyObject* PyUnicode_EncodeCodePage(int code_page, PyObject *unicode, const char *errors)

Encode the Unicode object using the specified code page and return a Python bytes object. Return NULL if an exception was raised by the codec. Use CP_ACP code page to get the MBCS encoder.

3.3 新版功能.

PyObject* PyUnicode_EncodeMBCS(const Py_UNICODE *s, Py_ssize_t size, const char *errors)

Return value: New reference.

Encode the Py_UNICODE buffer of the given size using MBCS and return a Python bytes object. Return NULL if an exception was raised by the codec.

Deprecated since version 3.3, will be removed in version 4.0: Part of the old-style Py_UNICODE API; please migrate to using PyUnicode_AsMBCSString(), PyUnicode_EncodeCodePage() or PyUnicode_AsEncodedString().

Methods & Slots

Methods and Slot Functions

The following APIs are capable of handling Unicode objects and strings on input (we refer to them as strings in the descriptions) and return Unicode objects or integers as appropriate.

They all return NULL or -1 if an exception occurs.

PyObject* PyUnicode_Concat(PyObject *left, PyObject *right)

Return value: New reference.

Concat two strings giving a new Unicode string.

PyObject* PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit)

Return value: New reference.

Split a string giving a list of Unicode strings. If sep is NULL, splitting will be done at all whitespace substrings. Otherwise, splits occur at the given separator. At most maxsplit splits will be done. If negative, no limit is set. Separators are not included in the resulting list.

PyObject* PyUnicode_Splitlines(PyObject *s, int keepend)

Return value: New reference.

Split a Unicode string at line breaks, returning a list of Unicode strings. CRLF is considered to be one line break. If keepend is 0, the Line break characters are not included in the resulting strings.

PyObject* PyUnicode_Translate(PyObject *str, PyObject *table, const char *errors)

Translate a string by applying a character mapping table to it and return the resulting Unicode object.

The mapping table must map Unicode ordinal integers to Unicode ordinal integers or None (causing deletion of the character).

Mapping tables need only provide the __getitem__() interface; dictionaries and sequences work well. Unmapped character ordinals (ones which cause a LookupError) are left untouched and are copied as-is.

errors has the usual meaning for codecs. It may be NULL which indicates to use the default error handling.

PyObject* PyUnicode_Join(PyObject *separator, PyObject *seq)

Return value: New reference.

Join a sequence of strings using the given separator and return the resulting Unicode string.

Py_ssize_t PyUnicode_Tailmatch(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction)

Return 1 if substr matches str[start:end] at the given tail end (direction == -1 means to do a prefix match, direction == 1 a suffix match), 0 otherwise. Return -1 if an error occurred.

Py_ssize_t PyUnicode_Find(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction)

Return the first position of substr in str[start:end] using the given direction (direction == 1 means to do a forward search, direction == -1 a backward search). The return value is the index of the first match; a value of -1 indicates that no match was found, and -2 indicates that an error occurred and an exception has been set.

Py_ssize_t PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, Py_ssize_t start, Py_ssize_t end, int direction)

Return the first position of the character ch in str[start:end] using the given direction (direction == 1 means to do a forward search, direction == -1 a backward search). The return value is the index of the first match; a value of -1 indicates that no match was found, and -2 indicates that an error occurred and an exception has been set.

3.3 新版功能.

Py_ssize_t PyUnicode_Count(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end)

Return the number of non-overlapping occurrences of substr in str[start:end]. Return -1 if an error occurred.

PyObject* PyUnicode_Replace(PyObject *str, PyObject *substr, PyObject *replstr, Py_ssize_t maxcount)

Return value: New reference.

Replace at most maxcount occurrences of substr in str with replstr and return the resulting Unicode object. maxcount == -1 means replace all occurrences.

int PyUnicode_Compare(PyObject *left, PyObject *right)

Compare two strings and return -1, 0, 1 for less than, equal, and greater than, respectively.

This function returns -1 upon failure, so one should call PyErr_Occurred() to check for errors.

int PyUnicode_CompareWithASCIIString(PyObject *uni, const char *string)

Compare a unicode object, uni, with string and return -1, 0, 1 for less than, equal, and greater than, respectively. It is best to pass only ASCII-encoded strings, but the function interprets the input string as ISO-8859-1 if it contains non-ASCII characters.

This function does not raise exceptions.

PyObject* PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)

Rich compare two unicode strings and return one of the following:

NULL in case an exception was raised

Py_True or Py_False for successful comparisons

Py_NotImplemented in case the type combination is unknown

Possible values for op are Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, and Py_LE.

PyObject* PyUnicode_Format(PyObject *format, PyObject *args)

Return value: New reference.

Return a new string object from format and args; this is analogous to format % args.

int PyUnicode_Contains(PyObject *container, PyObject *element)

Check whether element is contained in container and return true or false accordingly.

element has to coerce to a one element Unicode string. -1 is returned if there was an error.

void PyUnicode_InternInPlace(PyObject **string)

Intern the argument *string in place. The argument must be the address of a pointer variable pointing to a Python unicode string object. If there is an existing interned string that is the same as *string, it sets *string to it (decrementing the reference count of the old string object and incrementing the reference count of the interned string object), otherwise it leaves *string alone and interns it (incrementing its reference count). (Clarification: even though there is a lot of talk about reference counts, think of this function as reference-count-neutral; you own the object after the call if and only if you owned it before the call.)

PyObject* PyUnicode_InternFromString(const char *v)

A combination of PyUnicode_FromString() and PyUnicode_InternInPlace(), returning either a new unicode string object that has been interned, or a new (“owned”) reference to an earlier interned string object with the same value.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

元组对象

PyTupleObject

这个 PyObject 的子类型代表一个 Python 的元组对象。

PyTypeObject PyTuple_Type

PyTypeObject 的实例代表一个 Python 元组类型，这与 Python 层面的 tuple 是相同的对象。

int PyTuple_Check(PyObject *p)

如果 p 是一个元组对象或者元组类型的子类型的实例，则返回真值。

int PyTuple_CheckExact(PyObject *p)

如果 p 是一个元组对象，而不是一个元组子类型的实例，则返回真值。

PyObject* PyTuple_New(Py_ssize_t len)

Return value: New reference.

Return a new tuple object of size len, or NULL on failure.

PyObject* PyTuple_Pack(Py_ssize_t n, ...)

Return value: New reference.

Return a new tuple object of size n, or NULL on failure. The tuple values are initialized to the subsequent n C arguments pointing to Python objects. PyTuple_Pack(2, a, b) is equivalent to Py_BuildValue("(OO)", a, b).

Py_ssize_t PyTuple_Size(PyObject *p)

获取指向元组对象的指针，并返回该元组的大小。

Py_ssize_t PyTuple_GET_SIZE(PyObject *p)

Return the size of the tuple p, which must be non-NULL and point to a tuple; no error checking is performed.

PyObject* PyTuple_GetItem(PyObject *p, Py_ssize_t pos)

Return value: Borrowed reference.

Return the object at position pos in the tuple pointed to by p. If pos is out of bounds, return NULL and sets an IndexError exception.

PyObject* PyTuple_GET_ITEM(PyObject *p, Py_ssize_t pos)

Return value: Borrowed reference.

类似于 PyTuple_GetItem()，但不检查其参数。

PyObject* PyTuple_GetSlice(PyObject *p, Py_ssize_t low, Py_ssize_t high)

Return value: New reference.

Take a slice of the tuple pointed to by p from low to high and return it as a new tuple.

int PyTuple_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)

Insert a reference to object o at position pos of the tuple pointed to by p. Return 0 on success.

注解

这个函数“窃取”了指向*o*的一个引用。

void PyTuple_SET_ITEM(PyObject *p, Py_ssize_t pos, PyObject *o)

类似于 PyTuple_SetItem()，但不进行错误检查，并且应该 只是 被用来填充全新的元组。

注解

这个函数“窃取”了指向*o*的一个引用。

int _PyTuple_Resize(PyObject **p, Py_ssize_t newsize)

Can be used to resize a tuple. newsize will be the new length of the tuple. Because tuples are supposed to be immutable, this should only be used if there is only one reference to the object. Do not use this if the tuple may already be known to some other part of the code. The tuple will always grow or shrink at the end. Think of this as destroying the old tuple and creating a new one, only more efficiently. Returns 0 on success. Client code should never assume that the resulting value of *p will be the same as before calling this function. If the object referenced by *p is replaced, the original *p is destroyed. On failure, returns -1 and sets *p to NULL, and raises MemoryError or SystemError.

int PyTuple_ClearFreeList()

清空释放列表。 返回所释放的条目数。

结构序列对象

结构序列对象是等价于 namedtuple() 的 C 对象，即一个序列，其中的条目也可以通过属性访问。 要创建结构序列，你首先必须创建特定的结构序列类型。

PyTypeObject* PyStructSequence_NewType(PyStructSequence_Desc *desc)

根据 desc 中的数据创建一个新的结构序列类型，如下所述。 可以使用 PyStructSequence_New() 创建结果类型的实例。

void PyStructSequence_InitType(PyTypeObject *type, PyStructSequence_Desc *desc)

从*desc*就地初始化结构序列类型*type*。

int PyStructSequence_InitType2(PyTypeObject *type, PyStructSequence_Desc *desc)

与 PyStructSequence_InitType 相同，但成功时返回 0 ，失败时返回 -1 。

3.4 新版功能.

PyStructSequence_Desc

包含要创建的结构序列类型的元信息。

域

C 类型

含义

name

char *

结构序列类型的名称

doc

char *

pointer to docstring for the type or NULL to omit

fields

PyStructSequence_Field *

pointer to NULL-terminated array with field names of the new type

n_in_sequence

int

Python侧可见的字段数（如果用作元组）

PyStructSequence_Field

描述一个结构序列的字段。 当结构序列被建模为元组时，所有字段的类型都是 PyObject*。 PyStructSequence_Desc 的 fields 数组的索引确定了描述的是结构序列的哪个字段。

域

C 类型

含义

name

char *

name for the field or NULL to end the list of named fields, set to PyStructSequence_UnnamedField to leave unnamed

doc

char *

field docstring or NULL to omit

char* PyStructSequence_UnnamedField

字段名的特殊值将保持未命名状态。

PyObject* PyStructSequence_New(PyTypeObject *type)

创建 type 的实例，该实例必须使用 PyStructSequence_NewType() 创建。

PyObject* PyStructSequence_GetItem(PyObject *p, Py_ssize_t pos)

返回*p*所指向的结构序列中，位于*pos*处的对象。不需要进行边界检查。

PyObject* PyStructSequence_GET_ITEM(PyObject *p, Py_ssize_t pos)

PyStructSequence_GetItem() 的宏版本。

void PyStructSequence_SetItem(PyObject *p, Py_ssize_t pos, PyObject *o)

将结构序列 p 的索引 pos 处的字段设置为值 o。 与 PyTuple_SET_ITEM() 一样，它应该只用于填充全新的实例。

注解

这个函数“窃取”了指向*o*的一个引用。

void PyStructSequence_SET_ITEM(PyObject *p, Py_ssize_t *pos, PyObject *o)

PyStructSequence_SetItem() 的宏版本。

注解

这个函数“窃取”了指向*o*的一个引用。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

列表对象

PyListObject

这个C类型 PyObject 的子类型代表一个Python列表对象。

PyTypeObject PyList_Type

这是个属于 PyTypeObject 的代表Python列表类型的实例。在Python层面和类型 list 是同一个对象。

int PyList_Check(PyObject *p)

如果 p 是一个列表对象或者是一个列表类型的子类型实例时，返回真。

int PyList_CheckExact(PyObject *p)

当 p 是一个列表对象，但是不是列表类型的子类型实例时，返回真。

PyObject* PyList_New(Py_ssize_t len)

Return value: New reference.

Return a new list of length len on success, or NULL on failure.

注解

当 len 大于零时，被返回的列表对象项目被设成 NULL。因此你不能用类似C函数 PySequence_SetItem() 的抽象API或者用C函数 PyList_SetItem() 将所有项目设置成真实对象前对Python代码公开这个对象。

Py_ssize_t PyList_Size(PyObject *list)

返回 list 中列表对象的长度；这等于在列表对象调用 len(list) 。

Py_ssize_t PyList_GET_SIZE(PyObject *list)

宏版本的C函数 PyList_Size() ，没有错误检测。

PyObject* PyList_GetItem(PyObject *list, Py_ssize_t index)

Return value: Borrowed reference.

Return the object at position index in the list pointed to by list. The position must be positive, indexing from the end of the list is not supported. If index is out of bounds, return NULL and set an IndexError exception.

PyObject* PyList_GET_ITEM(PyObject *list, Py_ssize_t i)

Return value: Borrowed reference.

宏版本的C函数 PyList_GetItem() ，没有错误检测。

int PyList_SetItem(PyObject *list, Py_ssize_t index, PyObject *item)

Set the item at index index in list to item. Return 0 on success or -1 on failure.

注解

此函数会“偷走”一个对 item 的引用并丢弃一个对列表中受影响位置上的已有条目的引用。

void PyList_SET_ITEM(PyObject *list, Py_ssize_t i, PyObject *o)

不带错误检测的宏版本 PyList_SetItem()。 这通常只被用于新列表中之前没有内容的位置进行填充。

注解

该宏会“偷走”一个对 item 的引用，但与 PyList_SetItem() 不同的是它 不会 丢弃对任何被替换条目的引用；在 list 的 i 位置上的任何引用都将被泄露。

int PyList_Insert(PyObject *list, Py_ssize_t index, PyObject *item)

将条目 item 插入到列表 list 索引号 index 之前的位置。 如果成功将返回 0；如果不成功则返回 -1 并设置一个异常。 相当于 list.insert(index, item)。

int PyList_Append(PyObject *list, PyObject *item)

将对象 item 添加到列表 list 的末尾。 如果成功将返回 0；如果不成功则返回 -1 并设置一个异常。 相当于 list.append(item)。

PyObject* PyList_GetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high)

Return value: New reference.

Return a list of the objects in list containing the objects between low and high. Return NULL and set an exception if unsuccessful. Analogous to list[low:high]. Negative indices, as when slicing from Python, are not supported.

int PyList_SetSlice(PyObject *list, Py_ssize_t low, Py_ssize_t high, PyObject *itemlist)

Set the slice of list between low and high to the contents of itemlist. Analogous to list[low:high] = itemlist. The itemlist may be NULL, indicating the assignment of an empty list (slice deletion). Return 0 on success, -1 on failure. Negative indices, as when slicing from Python, are not supported.

int PyList_Sort(PyObject *list)

对 list 中的条目进行原地排序。 成功时返回 0，失败时返回 -1。 这等价于 list.sort()。

int PyList_Reverse(PyObject *list)

对 list 中的条目进行原地反转。 成功时返回 0，失败时返回 -1。 这等价于 list.reverse()。

PyObject* PyList_AsTuple(PyObject *list)

Return value: New reference.

返回一个新的元组对象，其中包含 list 的内容；等价于 tuple(list)。

int PyList_ClearFreeList()

清空释放列表。 返回所释放的条目数。

3.3 新版功能.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

字典对象

PyDictObject

这个 PyObject 的子类型代表一个Python字典对象。

PyTypeObject PyDict_Type

Python字典类型表示为 PyTypeObject 的实例。这与Python层面的 dict 是相同的对象。

int PyDict_Check(PyObject *p)

如果 p 是字典对象或者字典类型的子类型的实例，则返回真。

int PyDict_CheckExact(PyObject *p)

如果 p 是字典对象但不是字典类型的子类型的实例，则返回真。

PyObject* PyDict_New()

Return value: New reference.

Return a new empty dictionary, or NULL on failure.

PyObject* PyDictProxy_New(PyObject *mapping)

Return value: New reference.

返回 types.MappingProxyType 对象，用于强制执行只读行为的映射。这通常用于创建视图以防止修改非动态类类型的字典。

void PyDict_Clear(PyObject *p)

清空现有字典的所有键值对。

int PyDict_Contains(PyObject *p, PyObject *key)

确定 key 是否包含在字典 p 中。如果 key 匹配上 p 的某一项，则返回 1 ，否则返回 0 。返回 -1 表示出错。这等同于Python表达式 key in p 。

PyObject* PyDict_Copy(PyObject *p)

Return value: New reference.

返回与 p 包含相同键值对的新字典。

int PyDict_SetItem(PyObject *p, PyObject *key, PyObject *val)

使用 key 作为键将 value 插入字典 p 。 key 必须为 hashable ；如果不是，会抛出 TypeError 异常。成功返回 0 ，失败返回 -1 。

int PyDict_SetItemString(PyObject *p, const char *key, PyObject *val)

Insert value into the dictionary p using key as a key. key should be a char*. The key object is created using PyUnicode_FromString(key). Return 0 on success or -1 on failure.

int PyDict_DelItem(PyObject *p, PyObject *key)

使用键 key 删除字典 p 中的条目。 key 必须是可哈希的；如果不是，则抛出 TypeError 异常。成功时返回 0 ，失败时返回 -1 。

int PyDict_DelItemString(PyObject *p, const char *key)

删除字典 p 中由字符串 key 作为键的条目。成功时返回 0 ，失败时返回 -1。

PyObject* PyDict_GetItem(PyObject *p, PyObject *key)

Return value: Borrowed reference.

Return the object from dictionary p which has a key key. Return NULL if the key key is not present, but without setting an exception.

需要注意的是，调用 __hash__() 和 __eq__() 方法产生的异常不会被抛出。改用 PyDict_GetItemWithError() 获得错误报告。

PyObject* PyDict_GetItemWithError(PyObject *p, PyObject *key)

Variant of PyDict_GetItem() that does not suppress exceptions. Return NULL with an exception set if an exception occurred. Return NULL without an exception set if the key wasn’t present.

PyObject* PyDict_GetItemString(PyObject *p, const char *key)

Return value: Borrowed reference.

This is the same as PyDict_GetItem(), but key is specified as a char*, rather than a PyObject*.

需要注意的是，调用 __hash__() 、 __eq__() 方法和创建一个临时的字符串对象时产生的异常不会被抛出。改用 PyDict_GetItemWithError() 获得错误报告。

PyObject* PyDict_SetDefault(PyObject *p, PyObject *key, PyObject *default)

Return value: Borrowed reference.

这跟Python层面的 dict.setdefault() 一样。如果键 key 存在，它返回在字典 p 里面对应的值。如果键不存在，它会和值 defaultobj 一起插入并返回 defaultobj 。这个函数只计算 key 的哈希函数一次，而不是在查找和插入时分别计算它。

3.4 新版功能.

PyObject* PyDict_Items(PyObject *p)

Return value: New reference.

返回一个包含字典中所有键值项的 PyListObject。

PyObject* PyDict_Keys(PyObject *p)

Return value: New reference.

返回一个包含字典中所有键(keys)的 PyListObject。

PyObject* PyDict_Values(PyObject *p)

Return value: New reference.

返回一个包含字典中所有值(values)的 PyListObject。

Py_ssize_t PyDict_Size(PyObject *p)

返回字典中项目数，等价于对字典 p 使用 len(p)。

int PyDict_Next(PyObject *p, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)

Iterate over all key-value pairs in the dictionary p. The Py_ssize_t referred to by ppos must be initialized to 0 prior to the first call to this function to start the iteration; the function returns true for each pair in the dictionary, and false once all pairs have been reported. The parameters pkey and pvalue should either point to PyObject* variables that will be filled in with each key and value, respectively, or may be NULL. Any references returned through them are borrowed. ppos should not be altered during iteration. Its value represents offsets within the internal dictionary structure, and since the structure is sparse, the offsets are not consecutive.

例如

PyObject *key, *value;
Py_ssize_t pos = 0;

while (PyDict_Next(self->dict, &pos, &key, &value)) {
    /* do something interesting with the values... */
    ...
}

字典 p 不应该在遍历期间发生改变。在遍历字典时，改变键中的值是安全的，但仅限于键的集合不发生改变。例如:

PyObject *key, *value;
Py_ssize_t pos = 0;

while (PyDict_Next(self->dict, &pos, &key, &value)) {
    long i = PyLong_AsLong(value);
    if (i == -1 && PyErr_Occurred()) {
        return -1;
    }
    PyObject *o = PyLong_FromLong(i + 1);
    if (o == NULL)
        return -1;
    if (PyDict_SetItem(self->dict, key, o) < 0) {
        Py_DECREF(o);
        return -1;
    }
    Py_DECREF(o);
}

int PyDict_Merge(PyObject *a, PyObject *b, int override)

对映射对象 b 进行迭代，将键值对添加到字典 a。 b 可以是一个字典，或任何支持 PyMapping_Keys() 和 PyObject_GetItem() 的对象。 如果 override 为真值，则如果在 b 中找到相同的键则 a 中已存在的相应键值对将被替换，否则如果在 a 中没有相同的键则只是添加键值对。 当成功时返回 0 或者当引发异常时返回 -1。

int PyDict_Update(PyObject *a, PyObject *b)

这与 C 中的 PyDict_Merge(a, b, 1) 一样，也类似于 Python 中的 a.update(b)，差别在于 PyDict_Update() 在第二个参数没有 “keys” 属性时不会回退到迭代键值对的序列。 当成功时返回 0 或者当引发异常时返回 -1。

int PyDict_MergeFromSeq2(PyObject *a, PyObject *seq2, int override)

将 seq2 中的键值对更新或合并到字典 a。 seq2 必须为产生长度为 2 的用作键值对的元素的可迭代对象。 当存在重复的键时，如果 override 真值则最后出现的键胜出。 当成功时返回 0 或者当引发异常时返回 -1。 等价的 Python 代码（返回值除外）:

def PyDict_MergeFromSeq2(a, seq2, override):
    for key, value in seq2:
        if override or key not in a:
            a[key] = value

int PyDict_ClearFreeList()

清空释放列表。 返回所释放的条目数。

3.3 新版功能.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

集合对象

这一章节详细介绍了 set 和 frozenset 对象的公共 API。 任何未在下面列出的功能最好是使用抽象对象协议 (包括 PyObject_CallMethod(), PyObject_RichCompareBool(), PyObject_Hash(), PyObject_Repr(), PyObject_IsTrue(), PyObject_Print() 以及 PyObject_GetIter()) 或者抽象数字协议 (包括 PyNumber_And(), PyNumber_Subtract(), PyNumber_Or(), PyNumber_Xor(), PyNumber_InPlaceAnd(), PyNumber_InPlaceSubtract(), PyNumber_InPlaceOr() 以及 PyNumber_InPlaceXor()) 来访问。

PySetObject

这个 PyObject 的子类型被用来保存 set 和 frozenset 对象的内部数据。 它类似于 PyDictObject，因为对于小尺寸集合来说它是固定大小的（很像元组的存储方式），并且对于中等和大尺寸集合来说它将指向单独的可变大小的内存块（很像列表的存储方式）。 此结构体的字段不应被视为公有并且可能发生改变。 所有访问应当通过已写入文档的 API 来进行，而不可通过操纵结构体中的值。

PyTypeObject PySet_Type

这是一个 PyTypeObject 实例，表示 Python set 类型。

PyTypeObject PyFrozenSet_Type

这是一个 PyTypeObject 实例，表示 Python frozenset 类型。

下列类型检查宏适用于指向任意 Python 对象的指针。 类似地，这些构造函数也适用于任意可迭代的 Python 对象。

int PySet_Check(PyObject *p)

如果 p 是一个 set 对象或者是其子类型的实例则返回真值。

int PyFrozenSet_Check(PyObject *p)

如果 p 是一个 frozenset 对象或者是其子类型的实例则返回真值。

int PyAnySet_Check(PyObject *p)

如果 p 是一个 set 对象、frozenset 对象或者是其子类型的实例则返回真值。

int PyAnySet_CheckExact(PyObject *p)

如果 p 是一个 set 对象或 frozenset 对象但不是其子类型的实例则返回真值。

int PyFrozenSet_CheckExact(PyObject *p)

如果 p 是一个 frozenset 对象但不是其子类型的实例则返回真值。

PyObject* PySet_New(PyObject *iterable)

Return value: New reference.

Return a new set containing objects returned by the iterable. The iterable may be NULL to create a new empty set. Return the new set on success or NULL on failure. Raise TypeError if iterable is not actually iterable. The constructor is also useful for copying a set (c=set(s)).

PyObject* PyFrozenSet_New(PyObject *iterable)

Return value: New reference.

Return a new frozenset containing objects returned by the iterable. The iterable may be NULL to create a new empty frozenset. Return the new set on success or NULL on failure. Raise TypeError if iterable is not actually iterable.

下列函数和宏适用于 set 或 frozenset 的实例或是其子类型的实例。

Py_ssize_t PySet_Size(PyObject *anyset)

返回 set 或 frozenset 对象的长度。 等价于 len(anyset)。 如果 anyset 不是 set, frozenset 或其子类型的实例则会引发 PyExc_SystemError。

Py_ssize_t PySet_GET_SIZE(PyObject *anyset)

宏版本的 PySet_Size()，不带错误检测。

int PySet_Contains(PyObject *anyset, PyObject *key)

如果找到返回 1，如果未找到返回 0，如果遇到错误则返回 -1。 不同于 Python __contains__() 方法，此函数不会自动将不可哈希的集合转换为临时的冻结集合。 如果 key 为不可哈希对象则会引发 TypeError。 如果 anyset 不是 set, frozenset 或其子类型的实例则会引发 PyExc_SystemError。

int PySet_Add(PyObject *set, PyObject *key)

添加 key 到一个 set 实例。 也可用于 frozenset 实例（类似于 PyTuple_SetItem()，它可被用来为全新冻结集合在公开给其他代码之前填充全新的值）。 成功时返回 0，失败时返回 -1。 如果 key 为不可哈希对象则会引发 TypeError。 如果没有增长空间则会引发 MemoryError。 如果 set 不是 set 或其子类型的实例则会引发 SystemError。

下列函数适用于 set 或其子类型的实例，但不可用于 frozenset 或其子类型的实例。

int PySet_Discard(PyObject *set, PyObject *key)

如果找到并移除返回 1，如果未找到（无操作）返回 0，如果遇到错误则返回 -1。 对于不存在的键不会引发 KeyError。 如果 key 为不可哈希对象则会引发 TypeError。 不同于 Python discard() 方法，此函数不会自动将不可哈希的集合转换为临时的冻结集合。 如果 set 不是 set 或其子类型的实例则会引发 PyExc_SystemError。

PyObject* PySet_Pop(PyObject *set)

Return value: New reference.

Return a new reference to an arbitrary object in the set, and removes the object from the set. Return NULL on failure. Raise KeyError if the set is empty. Raise a SystemError if set is not an instance of set or its subtype.

int PySet_Clear(PyObject *set)

清空现有字典的所有键值对。

int PySet_ClearFreeList()

清空释放列表。 返回所释放的条目数。

3.3 新版功能.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

函数对象

有一些特定于 Python 函数的函数。

PyFunctionObject

用于函数的 C 结构体。

PyTypeObject PyFunction_Type

这是一个 PyTypeObject 实例并表示 Python 函数类型。 它作为 types.FunctionType 向 Python 程序员公开。

int PyFunction_Check(PyObject *o)

Return true if o is a function object (has type PyFunction_Type). The parameter must not be NULL.

PyObject* PyFunction_New(PyObject *code, PyObject *globals)

Return value: New reference.

返回与代码对象 code 关联的新函数对象。 globals 必须是一个字典，该函数可以访问全局变量。

The function’s docstring and name are retrieved from the code object. __module__ is retrieved from globals. The argument defaults, annotations and closure are set to NULL. __qualname__ is set to the same value as the function’s name.

PyObject* PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname)

Return value: New reference.

As PyFunction_New(), but also allows setting the function object’s __qualname__ attribute. qualname should be a unicode object or NULL; if NULL, the __qualname__ attribute is set to the same value as its __name__ attribute.

3.3 新版功能.

PyObject* PyFunction_GetCode(PyObject *op)

Return value: Borrowed reference.

返回与函数对象 op 关联的代码对象。

PyObject* PyFunction_GetGlobals(PyObject *op)

Return value: Borrowed reference.

返回与函数对象*op*相关联的全局字典。

PyObject* PyFunction_GetModule(PyObject *op)

Return value: Borrowed reference.

返回函数对象 op 的 __module__ 属性，通常为一个包含了模块名称的字符串，但可以通过 Python 代码设为返回其他任意对象。

PyObject* PyFunction_GetDefaults(PyObject *op)

Return value: Borrowed reference.

Return the argument default values of the function object op. This can be a tuple of arguments or NULL.

int PyFunction_SetDefaults(PyObject *op, PyObject *defaults)

Set the argument default values for the function object op. defaults must be Py_None or a tuple.

失败时引发 SystemError 异常并返回 -1 。

PyObject* PyFunction_GetClosure(PyObject *op)

Return value: Borrowed reference.

Return the closure associated with the function object op. This can be NULL or a tuple of cell objects.

int PyFunction_SetClosure(PyObject *op, PyObject *closure)

Set the closure associated with the function object op. closure must be Py_None or a tuple of cell objects.

失败时引发 SystemError 异常并返回 -1 。

PyObject *PyFunction_GetAnnotations(PyObject *op)

Return the annotations of the function object op. This can be a mutable dictionary or NULL.

int PyFunction_SetAnnotations(PyObject *op, PyObject *annotations)

Set the annotations for the function object op. annotations must be a dictionary or Py_None.

失败时引发 SystemError 异常并返回 -1 。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

实例方法对象

实例方法是 PyCFunction 的包装器，也是将 PyCFunction 绑定到类对象的一种新方式。 它替代了原先的调用 PyMethod_New(func, NULL, class)。

PyTypeObject PyInstanceMethod_Type

这个 PyTypeObject 实例代表 Python 实例方法类型。 它并不对 Python 程序公开。

int PyInstanceMethod_Check(PyObject *o)

Return true if o is an instance method object (has type PyInstanceMethod_Type). The parameter must not be NULL.

PyObject* PyInstanceMethod_New(PyObject *func)

返回一个新的实例方法对象，func 应为任意可调用对象，func 将在实例方法被调用时作为函数被调用。

PyObject* PyInstanceMethod_Function(PyObject *im)

返回关联到实例方法 im 的函数对象。

PyObject* PyInstanceMethod_GET_FUNCTION(PyObject *im)

宏版本的 PyInstanceMethod_Function()，略去了错误检测。

方法对象

方法是绑定的函数对象。 方法总是会被绑定到一个用户自定义类的实例。 未绑定方法（绑定到一个类的方法）已不再可用。

PyTypeObject PyMethod_Type

这个 PyTypeObject 实例代表 Python 方法类型。 它作为 types.MethodType 向 Python 程序公开。

int PyMethod_Check(PyObject *o)

Return true if o is a method object (has type PyMethod_Type). The parameter must not be NULL.

PyObject* PyMethod_New(PyObject *func, PyObject *self)

Return value: New reference.

Return a new method object, with func being any callable object and self the instance the method should be bound. func is the function that will be called when the method is called. self must not be NULL.

PyObject* PyMethod_Function(PyObject *meth)

Return value: Borrowed reference.

返回关联到方法 meth 的函数对象。

PyObject* PyMethod_GET_FUNCTION(PyObject *meth)

Return value: Borrowed reference.

宏版本的 PyMethod_Function()，略去了错误检测。

PyObject* PyMethod_Self(PyObject *meth)

Return value: Borrowed reference.

返回关联到方法 meth 的实例。

PyObject* PyMethod_GET_SELF(PyObject *meth)

Return value: Borrowed reference.

宏版本的 PyMethod_Self()，省略了错误检测。

int PyMethod_ClearFreeList()

清空释放列表。 返回所释放的条目数。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

Cell 对象

“Cell”对象用于实现由多个作用域引用的变量。 对于每个这样的变量，一个“Cell”对象为了存储该值而被创建；引用该值的每个堆栈框架的局部变量包含同样使用该变量的对外部作用域的“Cell”引用。 访问该值时，将使用“Cell”中包含的值而不是单元格对象本身。 这种对“Cell”对象的非关联化的引用需要支持生成的字节码；访问时不会自动非关联化这些内容。 “Cell”对象在其他地方可能不太有用。

PyCellObject

用于Cell对象的C结构体。

PyTypeObject PyCell_Type

与 Cell 对象对应的类型对​​象。

int PyCell_Check(ob)

Return true if ob is a cell object; ob must not be NULL.

PyObject* PyCell_New(PyObject *ob)

Return value: New reference.

Create and return a new cell object containing the value ob. The parameter may be NULL.

PyObject* PyCell_Get(PyObject *cell)

Return value: New reference.

返回 cell 对象 cell 的内容。

PyObject* PyCell_GET(PyObject *cell)

Return value: Borrowed reference.

Return the contents of the cell cell, but without checking that cell is non-NULL and a cell object.

int PyCell_Set(PyObject *cell, PyObject *value)

Set the contents of the cell object cell to value. This releases the reference to any current content of the cell. value may be NULL. cell must be non-NULL; if it is not a cell object, -1 will be returned. On success, 0 will be returned.

void PyCell_SET(PyObject *cell, PyObject *value)

Sets the value of the cell object cell to value. No reference counts are adjusted, and no checks are made for safety; cell must be non-NULL and must be a cell object.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

代码对象

代码对象是 CPython 实现的低级细节。 每个代表一块尚未绑定到函数中的可执行代码。

PyCodeObject

用于描述代码对象的对象的 C 结构。 此类型字段可随时更改。

PyTypeObject PyCode_Type

这是一个 PyTypeObject 实例，其表示Python的 code 类型。

int PyCode_Check(PyObject *co)

如果 co 是一个 code 对象则返回 true。

int PyCode_GetNumFree(PyCodeObject *co)

返回 co 中的自由变量数。

PyCodeObject* PyCode_New(int argcount, int kwonlyargcount, int nlocals, int stacksize, int flags, PyObject *code, PyObject *consts, PyObject *names, PyObject *varnames, PyObject *freevars, PyObject *cellvars, PyObject *filename, PyObject *name, int firstlineno, PyObject *lnotab)

返回一个新的代码对象。 如果你需要一个虚拟代码对象来创建一个代码帧，请使用 PyCode_NewEmpty()。 调用 PyCode_New() 直接可以绑定到准确的 Python 版本，因为字节码的定义经常变化。

PyCodeObject* PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)

返回具有指定文件名、函数名和第一行号的新空代码对象。 对于 exec() 或 eval() 生成的代码对象是非法的。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

文件对象

这些 API 是内置文件对象的 Python 2 C API 的最小仿真，它过去依赖于C标准库的缓冲 I/O（ FILE* ）支持。 在Python 3中，文件和流使用新的 io 模块，该模块在操作系统的低级无缓冲 I/O 上定义了几个层。 下面描述的函数是针对这些新API的便捷 C 包装器，主要用于解释器中的内部错误报告；建议第三方代码访问 io API。

PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const char *encoding, const char *errors, const char *newline, int closefd)

Create a Python file object from the file descriptor of an already opened file fd. The arguments name, encoding, errors and newline can be NULL to use the defaults; buffering can be -1 to use the default. name is ignored and kept for backward compatibility. Return NULL on failure. For a more comprehensive description of the arguments, please refer to the io.open() function documentation.

警告

由于Python流具有自己的缓冲层，因此将它们与 OS 级文件描述符混合会产生各种问题（例如数据的意外排序）。

在 3.2 版更改: 忽略 name 属性。

int PyObject_AsFileDescriptor(PyObject *p)

将与 p 关联的文件描述器返回为 int 。 如果对象是整数，则返回其值。 如果没有，则调用对象的 fileno() 方法（如果存在）; 该方法必须返回一个整数，该整数作为文件描述器值返回。 设置异常并在失败时返回 -1。

PyObject* PyFile_GetLine(PyObject *p, int n)

Return value: New reference.

等价于 p.readline([n]) ，这个函数从对象 p 中读取一行。 p 可以是文件对象或具有 readline() 方法的任何对象。 如果 n 是 0 ，则无论该行的长度如何，都会读取一行。 如果 n 大于``0``，则从文件中读取不超过 n 个字节；可以返回行的一部分。 在这两种情况下，如果立即到达文件末尾，则返回空字符串。 但是，如果 n 小于 0 ，则无论长度如何都会读取一行，但是如果立即到达文件末尾，则引发 EOFError。

int PyFile_WriteObject(PyObject *obj, PyObject *p, int flags)

将对象 obj 写入文件对象 p 。 flags 唯一支持的标志是 Py_PRINT_RAW；如果给定，则写入对象的 str() 而不是 repr()。成功时返回 0，失败时返回 -1。 将设置适当的例外。

int PyFile_WriteString(const char *s, PyObject *p)

将字符串 s 写入文件对象 p。 成功返回 0 失败返回 -1；将设定相应的异常。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

模块对象

PyTypeObject PyModule_Type

This instance of PyTypeObject represents the Python module type. This is exposed to Python programs as types.ModuleType.

int PyModule_Check(PyObject *p)

Return true if p is a module object, or a subtype of a module object.

int PyModule_CheckExact(PyObject *p)

Return true if p is a module object, but not a subtype of PyModule_Type.

PyObject* PyModule_NewObject(PyObject *name)

Return a new module object with the __name__ attribute set to name. The module’s __name__, __doc__, __package__, and __loader__ attributes are filled in (all but __name__ are set to None); the caller is responsible for providing a __file__ attribute.

3.3 新版功能.

在 3.4 版更改: __package__ and __loader__ are set to None.

PyObject* PyModule_New(const char *name)

Return value: New reference.

Similar to PyModule_NewObject(), but the name is a UTF-8 encoded string instead of a Unicode object.

PyObject* PyModule_GetDict(PyObject *module)

Return value: Borrowed reference.

Return the dictionary object that implements module’s namespace; this object is the same as the __dict__ attribute of the module object. If module is not a module object (or a subtype of a module object), SystemError is raised and NULL is returned.

It is recommended extensions use other PyModule_*() and PyObject_*() functions rather than directly manipulate a module’s __dict__.

PyObject* PyModule_GetNameObject(PyObject *module)

Return module’s __name__ value. If the module does not provide one, or if it is not a string, SystemError is raised and NULL is returned.

3.3 新版功能.

char* PyModule_GetName(PyObject *module)

Similar to PyModule_GetNameObject() but return the name encoded to 'utf-8'.

void* PyModule_GetState(PyObject *module)

Return the “state” of the module, that is, a pointer to the block of memory allocated at module creation time, or NULL. See PyModuleDef.m_size.

PyModuleDef* PyModule_GetDef(PyObject *module)

Return a pointer to the PyModuleDef struct from which the module was created, or NULL if the module wasn’t created from a definition.

PyObject* PyModule_GetFilenameObject(PyObject *module)

Return the name of the file from which module was loaded using module’s __file__ attribute. If this is not defined, or if it is not a unicode string, raise SystemError and return NULL; otherwise return a reference to a Unicode object.

3.2 新版功能.

char* PyModule_GetFilename(PyObject *module)

Similar to PyModule_GetFilenameObject() but return the filename encoded to ‘utf-8’.

3.2 版后已移除: PyModule_GetFilename() raises UnicodeEncodeError on unencodable filenames, use PyModule_GetFilenameObject() instead.

Initializing C modules

Modules objects are usually created from extension modules (shared libraries which export an initialization function), or compiled-in modules (where the initialization function is added using PyImport_AppendInittab()). See 构建C/C++扩展 or Extending Embedded Python for details.

The initialization function can either pass a module definition instance to PyModule_Create(), and return the resulting module object, or request “multi-phase initialization” by returning the definition struct itself.

PyModuleDef

The module definition struct, which holds all information needed to create a module object. There is usually only one statically initialized variable of this type for each module.

PyModuleDef_Base m_base

Always initialize this member to PyModuleDef_HEAD_INIT.

char* m_name

Name for the new module.

char* m_doc

Docstring for the module; usually a docstring variable created with PyDoc_STRVAR() is used.

Py_ssize_t m_size

Module state may be kept in a per-module memory area that can be retrieved with PyModule_GetState(), rather than in static globals. This makes modules safe for use in multiple sub-interpreters.

This memory area is allocated based on m_size on module creation, and freed when the module object is deallocated, after the m_free function has been called, if present.

Setting m_size to -1 means that the module does not support sub-interpreters, because it has global state.

Setting it to a non-negative value means that the module can be re-initialized and specifies the additional amount of memory it requires for its state. Non-negative m_size is required for multi-phase initialization.

See PEP 3121 [https://www.python.org/dev/peps/pep-3121] for more details.

PyMethodDef* m_methods

A pointer to a table of module-level functions, described by PyMethodDef values. Can be NULL if no functions are present.

PyModuleDef_Slot* m_slots

An array of slot definitions for multi-phase initialization, terminated by a {0, NULL} entry. When using single-phase initialization, m_slots must be NULL.

在 3.5 版更改: Prior to version 3.5, this member was always set to NULL, and was defined as:

inquiry m_reload

traverseproc m_traverse

A traversal function to call during GC traversal of the module object, or NULL if not needed. This function may be called before module state is allocated (PyModule_GetState() may return NULL), and before the Py_mod_exec function is executed.

inquiry m_clear

A clear function to call during GC clearing of the module object, or NULL if not needed. This function may be called before module state is allocated (PyModule_GetState() may return NULL), and before the Py_mod_exec function is executed.

freefunc m_free

A function to call during deallocation of the module object, or NULL if not needed. This function may be called before module state is allocated (PyModule_GetState() may return NULL), and before the Py_mod_exec function is executed.

Single-phase initialization

The module initialization function may create and return the module object directly. This is referred to as “single-phase initialization”, and uses one of the following two module creation functions:

PyObject* PyModule_Create(PyModuleDef *def)

Create a new module object, given the definition in def. This behaves like PyModule_Create2() with module_api_version set to PYTHON_API_VERSION.

PyObject* PyModule_Create2(PyModuleDef *def, int module_api_version)

Create a new module object, given the definition in def, assuming the API version module_api_version. If that version does not match the version of the running interpreter, a RuntimeWarning is emitted.

注解

Most uses of this function should be using PyModule_Create() instead; only use this if you are sure you need it.

Before it is returned from in the initialization function, the resulting module object is typically populated using functions like PyModule_AddObject().

Multi-phase initialization

An alternate way to specify extensions is to request “multi-phase initialization”. Extension modules created this way behave more like Python modules: the initialization is split between the creation phase, when the module object is created, and the execution phase, when it is populated. The distinction is similar to the __new__() and __init__() methods of classes.

Unlike modules created using single-phase initialization, these modules are not singletons: if the sys.modules entry is removed and the module is re-imported, a new module object is created, and the old module is subject to normal garbage collection – as with Python modules. By default, multiple modules created from the same definition should be independent: changes to one should not affect the others. This means that all state should be specific to the module object (using e.g. using PyModule_GetState()), or its contents (such as the module’s __dict__ or individual classes created with PyType_FromSpec()).

All modules created using multi-phase initialization are expected to support sub-interpreters. Making sure multiple modules are independent is typically enough to achieve this.

To request multi-phase initialization, the initialization function (PyInit_modulename) returns a PyModuleDef instance with non-empty m_slots. Before it is returned, the PyModuleDef instance must be initialized with the following function:

PyObject* PyModuleDef_Init(PyModuleDef *def)

Ensures a module definition is a properly initialized Python object that correctly reports its type and reference count.

Returns def cast to PyObject*, or NULL if an error occurred.

3.5 新版功能.

The m_slots member of the module definition must point to an array of PyModuleDef_Slot structures:

PyModuleDef_Slot

int slot

A slot ID, chosen from the available values explained below.

void* value

Value of the slot, whose meaning depends on the slot ID.

3.5 新版功能.

The m_slots array must be terminated by a slot with id 0.

The available slot types are:

Py_mod_create

Specifies a function that is called to create the module object itself. The value pointer of this slot must point to a function of the signature:

PyObject* create_module(PyObject *spec, PyModuleDef *def)

The function receives a ModuleSpec instance, as defined in PEP 451 [https://www.python.org/dev/peps/pep-0451], and the module definition. It should return a new module object, or set an error and return NULL.

This function should be kept minimal. In particular, it should not call arbitrary Python code, as trying to import the same module again may result in an infinite loop.

Multiple Py_mod_create slots may not be specified in one module definition.

If Py_mod_create is not specified, the import machinery will create a normal module object using PyModule_New(). The name is taken from spec, not the definition, to allow extension modules to dynamically adjust to their place in the module hierarchy and be imported under different names through symlinks, all while sharing a single module definition.

There is no requirement for the returned object to be an instance of PyModule_Type. Any type can be used, as long as it supports setting and getting import-related attributes. However, only PyModule_Type instances may be returned if the PyModuleDef has non-NULL m_traverse, m_clear, m_free; non-zero m_size; or slots other than Py_mod_create.

Py_mod_exec

Specifies a function that is called to execute the module. This is equivalent to executing the code of a Python module: typically, this function adds classes and constants to the module. The signature of the function is:

int exec_module(PyObject* module)

If multiple Py_mod_exec slots are specified, they are processed in the order they appear in the m_slots array.

See PEP 489 [https://www.python.org/dev/peps/pep-0489] for more details on multi-phase initialization.

Low-level module creation functions

The following functions are called under the hood when using multi-phase initialization. They can be used directly, for example when creating module objects dynamically. Note that both PyModule_FromDefAndSpec and PyModule_ExecDef must be called to fully initialize a module.

PyObject * PyModule_FromDefAndSpec(PyModuleDef *def, PyObject *spec)

Create a new module object, given the definition in module and the ModuleSpec spec. This behaves like PyModule_FromDefAndSpec2() with module_api_version set to PYTHON_API_VERSION.

3.5 新版功能.

PyObject * PyModule_FromDefAndSpec2(PyModuleDef *def, PyObject *spec, int module_api_version)

Create a new module object, given the definition in module and the ModuleSpec spec, assuming the API version module_api_version. If that version does not match the version of the running interpreter, a RuntimeWarning is emitted.

注解

Most uses of this function should be using PyModule_FromDefAndSpec() instead; only use this if you are sure you need it.

3.5 新版功能.

int PyModule_ExecDef(PyObject *module, PyModuleDef *def)

Process any execution slots (Py_mod_exec) given in def.

3.5 新版功能.

int PyModule_SetDocString(PyObject *module, const char *docstring)

Set the docstring for module to docstring. This function is called automatically when creating a module from PyModuleDef, using either PyModule_Create or PyModule_FromDefAndSpec.

3.5 新版功能.

int PyModule_AddFunctions(PyObject *module, PyMethodDef *functions)

Add the functions from the NULL terminated functions array to module. Refer to the PyMethodDef documentation for details on individual entries (due to the lack of a shared module namespace, module level “functions” implemented in C typically receive the module as their first parameter, making them similar to instance methods on Python classes). This function is called automatically when creating a module from PyModuleDef, using either PyModule_Create or PyModule_FromDefAndSpec.

3.5 新版功能.

Support functions

The module initialization function (if using single phase initialization) or a function called from a module execution slot (if using multi-phase initialization), can use the following functions to help initialize the module state:

int PyModule_AddObject(PyObject *module, const char *name, PyObject *value)

Add an object to module as name. This is a convenience function which can be used from the module’s initialization function. This steals a reference to value. Return -1 on error, 0 on success.

int PyModule_AddIntConstant(PyObject *module, const char *name, long value)

Add an integer constant to module as name. This convenience function can be used from the module’s initialization function. Return -1 on error, 0 on success.

int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value)

Add a string constant to module as name. This convenience function can be used from the module’s initialization function. The string value must be NULL-terminated. Return -1 on error, 0 on success.

int PyModule_AddIntMacro(PyObject *module, macro)

Add an int constant to module. The name and the value are taken from macro. For example PyModule_AddIntMacro(module, AF_INET) adds the int constant AF_INET with the value of AF_INET to module. Return -1 on error, 0 on success.

int PyModule_AddStringMacro(PyObject *module, macro)

Add a string constant to module.

Module lookup

Single-phase initialization creates singleton modules that can be looked up in the context of the current interpreter. This allows the module object to be retrieved later with only a reference to the module definition.

These functions will not work on modules created using multi-phase initialization, since multiple such modules can be created from a single definition.

PyObject* PyState_FindModule(PyModuleDef *def)

Returns the module object that was created from def for the current interpreter. This method requires that the module object has been attached to the interpreter state with PyState_AddModule() beforehand. In case the corresponding module object is not found or has not been attached to the interpreter state yet, it returns NULL.

int PyState_AddModule(PyObject *module, PyModuleDef *def)

Attaches the module object passed to the function to the interpreter state. This allows the module object to be accessible via PyState_FindModule().

Only effective on modules created using single-phase initialization.

3.3 新版功能.

int PyState_RemoveModule(PyModuleDef *def)

Removes the module object created from def from the interpreter state.

3.3 新版功能.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

迭代器对象

Python 提供了两个通用迭代器对象。 第一个是序列迭代器，它使用支持 __getitem__() 方法的任意序列。 第二个使用可调用对象和一个 sentinel 值，为序列中的每个项调用可调用对象，并在返回 sentinel 值时结束迭代。

PyTypeObject PySeqIter_Type

PySeqIter_New() 返回迭代器对象的类型对象和内置序列类型内置函数 iter() 的单参数形式。

int PySeqIter_Check(op)

如果 op 的类型为 PySeqIter_Type 则返回 true。

PyObject* PySeqIter_New(PyObject *seq)

Return value: New reference.

返回一个与常规序列对象一起使用的迭代器 seq。 当序列订阅操作引发 IndexError 时，迭代结束。

PyTypeObject PyCallIter_Type

由函数 PyCallIter_New() 和 iter() 内置函数的双参数形式返回的迭代器对象类型对象。

int PyCallIter_Check(op)

如果 op 的类型为 PyCallIter_Type 则返回 true。

PyObject* PyCallIter_New(PyObject *callable, PyObject *sentinel)

Return value: New reference.

返回一个新的迭代器。 第一个参数 callable 可以是任何可以在没有参数的情况下调用的 Python 可调用对象；每次调用都应该返回迭代中的下一个项目。 当 callable 返回等于 sentinel 的值时，迭代将终止。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

描述符对象

“描述符”是描述对象的某些属性的对象。它们存在于类型对象的字典中。

PyTypeObject PyProperty_Type

内建描述符类型的类型对象。

PyObject* PyDescr_NewGetSet(PyTypeObject *type, struct PyGetSetDef *getset)

Return value: New reference.

PyObject* PyDescr_NewMember(PyTypeObject *type, struct PyMemberDef *meth)

Return value: New reference.

PyObject* PyDescr_NewMethod(PyTypeObject *type, struct PyMethodDef *meth)

Return value: New reference.

PyObject* PyDescr_NewWrapper(PyTypeObject *type, struct wrapperbase *wrapper, void *wrapped)

Return value: New reference.

PyObject* PyDescr_NewClassMethod(PyTypeObject *type, PyMethodDef *method)

Return value: New reference.

int PyDescr_IsData(PyObject *descr)

如果描述符对象 descr 描述数据属性，则返回true；如果描述方法，则返回false。 descr 必须是描述符对象；没有错误检查。

PyObject* PyWrapper_New(PyObject *, PyObject *)

Return value: New reference.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

切片对象

PyTypeObject PySlice_Type

切片对象的类型对象。 它与 Python 层面的 slice 是相同的对象。

int PySlice_Check(PyObject *ob)

Return true if ob is a slice object; ob must not be NULL.

PyObject* PySlice_New(PyObject *start, PyObject *stop, PyObject *step)

Return value: New reference.

Return a new slice object with the given values. The start, stop, and step parameters are used as the values of the slice object attributes of the same names. Any of the values may be NULL, in which case the None will be used for the corresponding attribute. Return NULL if the new object could not be allocated.

int PySlice_GetIndices(PyObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)

从 slice 对象 slice 提取 start, stop 和 step 索引号，将序列长度视为 length。 大于 length 的序列号将被当作错误。

成功时返回 0，出错时返回 -1 并且不设置异常（除非某个序列号不为 None 且无法被转换为整数，在这种情况下会返回 -1 并且设置一个异常）。

你可能不会打算使用此函数。

在 3.2 版更改: 之前 slice 形参的形参类型是 PySliceObject*。

int PySlice_GetIndicesEx(PyObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength)

PySlice_GetIndices() 的可用替代。 从 slice 对象 slice 提取 start, stop 和 step 索引号，将序列长度视为 length，并将切片的长度保存在 slicelength 中，超出范围的索引号会以与普通切片一致的方式进行剪切。

成功时返回 0，出错时返回 -1 并且不设置异常。

在 3.2 版更改: 之前 slice 形参的形参类型是 PySliceObject*。

Ellipsis 对象

PyObject *Py_Ellipsis

Python 的 Ellipsis 对象。 该对象没有任何方法。 它必须以与任何其他对象一样的方式遵循引用计数。 它与 Py_None 一样是单例对象。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

MemoryView 对象

一个 memoryview 对象C级别的 缓冲区接口 暴露为一个可以像任何其他对象一样传递的 Python 对象。

PyObject *PyMemoryView_FromObject(PyObject *obj)

从提供缓冲区接口的对象创建 memoryview 对象。 如果 obj 支持可写缓冲区导出，则 memoryview 对象将可以被读/写，否则它可能是只读的，也可以是导出器自行决定的读/写。

PyObject *PyMemoryView_FromMemory(char *mem, Py_ssize_t size, int flags)

使用 mem 作为底层缓冲区创建一个 memoryview 对象。 flags 可以是 PyBUF_READ 或者 PyBUF_WRITE 之一.

3.3 新版功能.

PyObject *PyMemoryView_FromBuffer(Py_buffer *view)

创建一个包含给定缓冲区结构 view 的 memoryview 对象。 对于简单的字节缓冲区，PyMemoryView_FromMemory() 是首选函数。

PyObject *PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char order)

从定义缓冲区接口的对象创建一个 memoryview 对象 contiguous 内存块（在 ‘C’ 或 ‘F’ortran order 中）。 如果内存是连续的，则 memoryview 对象指向原始内存。 否则，复制并且 memoryview 指向新的 bytes 对象。

int PyMemoryView_Check(PyObject *obj)

如果对象 obj 是 memoryview 对象，则返回 true 。 目前不允许创建 memoryview 的子类。

Py_buffer *PyMemoryView_GET_BUFFER(PyObject *mview)

返回指向 memoryview 的导出缓冲区私有副本的指针。 mview 必须 是一个 memoryview 实例；这个宏不检查它的类型，你必须自己检查，否则你将面临崩溃风险。

Py_buffer *PyMemoryView_GET_BASE(PyObject *mview)

Return either a pointer to the exporting object that the memoryview is based on or NULL if the memoryview has been created by one of the functions PyMemoryView_FromMemory() or PyMemoryView_FromBuffer(). mview must be a memoryview instance.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

弱引用对象

Python 支持 “弱引用” 作为一类对象。具体来说，有两种直接实现弱引用的对象。第一种就是简单的引用对象，第二种尽可能地作用为一个原对象的代理。

int PyWeakref_Check(ob)

如果 “ob” 是一个引用或者一个代理对象，则返回 true。

int PyWeakref_CheckRef(ob)

如果 “ob” 是一个引用，则返回 true。

int PyWeakref_CheckProxy(ob)

如果 “ob” 是一个代理对象，则返回 true。

PyObject* PyWeakref_NewRef(PyObject *ob, PyObject *callback)

Return value: New reference.

Return a weak reference object for the object ob. This will always return a new reference, but is not guaranteed to create a new object; an existing reference object may be returned. The second parameter, callback, can be a callable object that receives notification when ob is garbage collected; it should accept a single parameter, which will be the weak reference object itself. callback may also be None or NULL. If ob is not a weakly-referencable object, or if callback is not callable, None, or NULL, this will return NULL and raise TypeError.

PyObject* PyWeakref_NewProxy(PyObject *ob, PyObject *callback)

Return value: New reference.

Return a weak reference proxy object for the object ob. This will always return a new reference, but is not guaranteed to create a new object; an existing proxy object may be returned. The second parameter, callback, can be a callable object that receives notification when ob is garbage collected; it should accept a single parameter, which will be the weak reference object itself. callback may also be None or NULL. If ob is not a weakly-referencable object, or if callback is not callable, None, or NULL, this will return NULL and raise TypeError.

PyObject* PyWeakref_GetObject(PyObject *ref)

Return value: Borrowed reference.

返回弱引用对象 ref 的被引用对象。如果被引用对象不再存在，则返回 Py_None。

注解

该函数返回被引用对象的一个**借来的引用**。这意味着除非你很清楚在你使用期间这个对象不可能被销毁，否则你应该始终对该对象调用 Py_INCREF()。

PyObject* PyWeakref_GET_OBJECT(PyObject *ref)

Return value: Borrowed reference.

类似 PyWeakref_GetObject()，但实现为一个不做类型检查的宏。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

胶囊

有关使用这些对象的更多信息请参阅 给扩展模块提供C API。

3.1 新版功能.

PyCapsule

这个 PyObject 的子类型代表着一个任意值，当需要通过 Python 代码将任意值（以 void* 指针的形式）从 C 扩展模块传递给其他 C 代码时非常有用。它通常用于将指向一个模块中定义的 C 语言函数指针传递给其他模块，以便可以从那里调用它们。这允许通过正常的模块导入机制访问动态加载的模块中的 C API。

PyCapsule_Destructor

这种类型的一个析构器返回一个胶囊，定义如下：

typedef void (*PyCapsule_Destructor)(PyObject *);

参阅 PyCapsule_New() 来获取 PyCapsule_Destructor 返回值的语义。

int PyCapsule_CheckExact(PyObject *p)

如果参数是一个 PyCapsule 则返回 True

PyObject* PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)

Return value: New reference.

Create a PyCapsule encapsulating the pointer. The pointer argument may not be NULL.

On failure, set an exception and return NULL.

The name string may either be NULL or a pointer to a valid C string. If non-NULL, this string must outlive the capsule. (Though it is permitted to free it inside the destructor.)

If the destructor argument is not NULL, it will be called with the capsule as its argument when it is destroyed.

如果此 capsule 将被保存为一个模块的属性，则 name 应当被指定为 modulename.attributename。 这将允许其他模块使用 PyCapsule_Import() 来导入此 capsule。

void* PyCapsule_GetPointer(PyObject *capsule, const char *name)

Retrieve the pointer stored in the capsule. On failure, set an exception and return NULL.

The name parameter must compare exactly to the name stored in the capsule. If the name stored in the capsule is NULL, the name passed in must also be NULL. Python uses the C function strcmp() to compare capsule names.

PyCapsule_Destructor PyCapsule_GetDestructor(PyObject *capsule)

Return the current destructor stored in the capsule. On failure, set an exception and return NULL.

It is legal for a capsule to have a NULL destructor. This makes a NULL return code somewhat ambiguous; use PyCapsule_IsValid() or PyErr_Occurred() to disambiguate.

void* PyCapsule_GetContext(PyObject *capsule)

Return the current context stored in the capsule. On failure, set an exception and return NULL.

It is legal for a capsule to have a NULL context. This makes a NULL return code somewhat ambiguous; use PyCapsule_IsValid() or PyErr_Occurred() to disambiguate.

const char* PyCapsule_GetName(PyObject *capsule)

Return the current name stored in the capsule. On failure, set an exception and return NULL.

It is legal for a capsule to have a NULL name. This makes a NULL return code somewhat ambiguous; use PyCapsule_IsValid() or PyErr_Occurred() to disambiguate.

void* PyCapsule_Import(const char *name, int no_block)

从一个模块的 capsule 属性导入指向 C 对象的指针。 name 形参应当指定属性的完整名称，与 module.attribute 中的一致。 保存在 capsule 中的 name 必须完全匹配此字符串。 如果 no_block 为真值，则以无阻塞模式导入模块 (使用 PyImport_ImportModuleNoBlock())。 如果 no_block 为假值，则以传统模式导入模块 (使用 PyImport_ImportModule())。

Return the capsule’s internal pointer on success. On failure, set an exception and return NULL.

int PyCapsule_IsValid(PyObject *capsule, const char *name)

Determines whether or not capsule is a valid capsule. A valid capsule is non-NULL, passes PyCapsule_CheckExact(), has a non-NULL pointer stored in it, and its internal name matches the name parameter. (See PyCapsule_GetPointer() for information on how capsule names are compared.)

换句话说，如果 PyCapsule_IsValid() 返回真值，则任何对访问器（以 PyCapsule_Get() 开头的任何函数）的调用都保证会成功。

如果对象有效并且匹配传入的名称则返回非零值。 否则返回 0。 此函数一定不会失败。

int PyCapsule_SetContext(PyObject *capsule, void *context)

将 capsule 内部的上下文指针设为 context。

成功时返回 0。 失败时返回非零值并设置一个异常。

int PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor)

将 capsule 内部的析构器设为 destructor。

成功时返回 0。 失败时返回非零值并设置一个异常。

int PyCapsule_SetName(PyObject *capsule, const char *name)

Set the name inside capsule to name. If non-NULL, the name must outlive the capsule. If the previous name stored in the capsule was not NULL, no attempt is made to free it.

成功时返回 0。 失败时返回非零值并设置一个异常。

int PyCapsule_SetPointer(PyObject *capsule, void *pointer)

Set the void pointer inside capsule to pointer. The pointer may not be NULL.

成功时返回 0。 失败时返回非零值并设置一个异常。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

生成器对象

生成器对象是Python用来实现生成器迭代器的对象。它们通常通过迭代产生值的函数来创建，而不是显式调用 PyGen_New() 或 PyGen_NewWithQualName()。

PyGenObject

用于生成器对象的C结构体。

PyTypeObject PyGen_Type

与生成器对象对应的类型对​​象。

int PyGen_Check(PyObject *ob)

Return true if ob is a generator object; ob must not be NULL.

int PyGen_CheckExact(PyObject *ob)

Return true if ob’s type is PyGen_Type; ob must not be NULL.

PyObject* PyGen_New(PyFrameObject *frame)

Return value: New reference.

Create and return a new generator object based on the frame object. A reference to frame is stolen by this function. The argument must not be NULL.

PyObject* PyGen_NewWithQualName(PyFrameObject *frame, PyObject *name, PyObject *qualname)

Return value: New reference.

Create and return a new generator object based on the frame object, with __name__ and __qualname__ set to name and qualname. A reference to frame is stolen by this function. The frame argument must not be NULL.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

协程对象

3.5 新版功能.

协程对象是使用 async 关键字声明的函数返回的。

PyCoroObject

用于协程对象的C结构体。

PyTypeObject PyCoro_Type

与协程对象对应的类型对​​象。

int PyCoro_CheckExact(PyObject *ob)

Return true if ob’s type is PyCoro_Type; ob must not be NULL.

PyObject* PyCoro_New(PyFrameObject *frame, PyObject *name, PyObject *qualname)

Return value: New reference.

Create and return a new coroutine object based on the frame object, with __name__ and __qualname__ set to name and qualname. A reference to frame is stolen by this function. The frame argument must not be NULL.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

DateTime 对象

datetime 模块提供了各种日期和时间对象。 在使用任何这些函数之前，必须在你的源码中包含头文件 datetime.h (请注意此文件并未包含在 Python.h 中)，并且宏 PyDateTime_IMPORT 必须被发起调用，通常是作为模块初始化函数的一部分。 这个宏会将指向特定 C 结构的指针放入一个静态变量 PyDateTimeAPI 中，它会由下面的宏来使用。

类型检查宏：

int PyDate_Check(PyObject *ob)

Return true if ob is of type PyDateTime_DateType or a subtype of PyDateTime_DateType. ob must not be NULL.

int PyDate_CheckExact(PyObject *ob)

Return true if ob is of type PyDateTime_DateType. ob must not be NULL.

int PyDateTime_Check(PyObject *ob)

Return true if ob is of type PyDateTime_DateTimeType or a subtype of PyDateTime_DateTimeType. ob must not be NULL.

int PyDateTime_CheckExact(PyObject *ob)

Return true if ob is of type PyDateTime_DateTimeType. ob must not be NULL.

int PyTime_Check(PyObject *ob)

Return true if ob is of type PyDateTime_TimeType or a subtype of PyDateTime_TimeType. ob must not be NULL.

int PyTime_CheckExact(PyObject *ob)

Return true if ob is of type PyDateTime_TimeType. ob must not be NULL.

int PyDelta_Check(PyObject *ob)

Return true if ob is of type PyDateTime_DeltaType or a subtype of PyDateTime_DeltaType. ob must not be NULL.

int PyDelta_CheckExact(PyObject *ob)

Return true if ob is of type PyDateTime_DeltaType. ob must not be NULL.

int PyTZInfo_Check(PyObject *ob)

Return true if ob is of type PyDateTime_TZInfoType or a subtype of PyDateTime_TZInfoType. ob must not be NULL.

int PyTZInfo_CheckExact(PyObject *ob)

Return true if ob is of type PyDateTime_TZInfoType. ob must not be NULL.

用于创建对象的宏：

PyObject* PyDate_FromDate(int year, int month, int day)

Return value: New reference.

Return a datetime.date object with the specified year, month and day.

PyObject* PyDateTime_FromDateAndTime(int year, int month, int day, int hour, int minute, int second, int usecond)

Return value: New reference.

Return a datetime.datetime object with the specified year, month, day, hour, minute, second and microsecond.

PyObject* PyTime_FromTime(int hour, int minute, int second, int usecond)

Return value: New reference.

Return a datetime.time object with the specified hour, minute, second and microsecond.

PyObject* PyDelta_FromDSU(int days, int seconds, int useconds)

Return value: New reference.

Return a datetime.timedelta object representing the given number of days, seconds and microseconds. Normalization is performed so that the resulting number of microseconds and seconds lie in the ranges documented for datetime.timedelta objects.

Macros to extract fields from date objects. The argument must be an instance of PyDateTime_Date, including subclasses (such as PyDateTime_DateTime). The argument must not be NULL, and the type is not checked:

int PyDateTime_GET_YEAR(PyDateTime_Date *o)

以正整数的形式返回年份值。

int PyDateTime_GET_MONTH(PyDateTime_Date *o)

返回月，从0到12的整数。

int PyDateTime_GET_DAY(PyDateTime_Date *o)

返回日期，从0到31的整数。

Macros to extract fields from datetime objects. The argument must be an instance of PyDateTime_DateTime, including subclasses. The argument must not be NULL, and the type is not checked:

int PyDateTime_DATE_GET_HOUR(PyDateTime_DateTime *o)

返回小时，从0到23的整数。

int PyDateTime_DATE_GET_MINUTE(PyDateTime_DateTime *o)

返回分钟，从0到59的整数。

int PyDateTime_DATE_GET_SECOND(PyDateTime_DateTime *o)

返回秒，从0到59的整数。

int PyDateTime_DATE_GET_MICROSECOND(PyDateTime_DateTime *o)

返回微秒，从0到999999的整数。

Macros to extract fields from time objects. The argument must be an instance of PyDateTime_Time, including subclasses. The argument must not be NULL, and the type is not checked:

int PyDateTime_TIME_GET_HOUR(PyDateTime_Time *o)

返回小时，从0到23的整数。

int PyDateTime_TIME_GET_MINUTE(PyDateTime_Time *o)

返回分钟，从0到59的整数。

int PyDateTime_TIME_GET_SECOND(PyDateTime_Time *o)

返回秒，从0到59的整数。

int PyDateTime_TIME_GET_MICROSECOND(PyDateTime_Time *o)

返回微秒，从0到999999的整数。

Macros to extract fields from time delta objects. The argument must be an instance of PyDateTime_Delta, including subclasses. The argument must not be NULL, and the type is not checked:

int PyDateTime_DELTA_GET_DAYS(PyDateTime_Delta *o)

返回天数，从-999999999到999999999的整数。

3.3 新版功能.

int PyDateTime_DELTA_GET_SECONDS(PyDateTime_Delta *o)

返回秒数，从0到86399的整数。

3.3 新版功能.

int PyDateTime_DELTA_GET_MICROSECONDS(PyDateTime_Delta *o)

返回微秒数，从0到999999的整数。

3.3 新版功能.

一些便于模块实现 DB API 的宏:

PyObject* PyDateTime_FromTimestamp(PyObject *args)

Return value: New reference.

Create and return a new datetime.datetime object given an argument tuple suitable for passing to datetime.datetime.fromtimestamp().

PyObject* PyDate_FromTimestamp(PyObject *args)

Return value: New reference.

Create and return a new datetime.date object given an argument tuple suitable for passing to datetime.date.fromtimestamp().

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

具体的对象层 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

Initialization, Finalization, and Threads

Initializing and finalizing the interpreter

void Py_Initialize()

Initialize the Python interpreter. In an application embedding Python, this should be called before using any other Python/C API functions; with the exception of Py_SetProgramName(), Py_SetPythonHome() and Py_SetPath(). This initializes the table of loaded modules (sys.modules), and creates the fundamental modules builtins, __main__ and sys. It also initializes the module search path (sys.path). It does not set sys.argv; use PySys_SetArgvEx() for that. This is a no-op when called for a second time (without calling Py_FinalizeEx() first). There is no return value; it is a fatal error if the initialization fails.

注解

On Windows, changes the console mode from O_TEXT to O_BINARY, which will also affect non-Python uses of the console using the C Runtime.

void Py_InitializeEx(int initsigs)

This function works like Py_Initialize() if initsigs is 1. If initsigs is 0, it skips initialization registration of signal handlers, which might be useful when Python is embedded.

int Py_IsInitialized()

Return true (nonzero) when the Python interpreter has been initialized, false (zero) if not. After Py_FinalizeEx() is called, this returns false until Py_Initialize() is called again.

int Py_FinalizeEx()

Undo all initializations made by Py_Initialize() and subsequent use of Python/C API functions, and destroy all sub-interpreters (see Py_NewInterpreter() below) that were created and not yet destroyed since the last call to Py_Initialize(). Ideally, this frees all memory allocated by the Python interpreter. This is a no-op when called for a second time (without calling Py_Initialize() again first). Normally the return value is 0. If there were errors during finalization (flushing buffered data), -1 is returned.

This function is provided for a number of reasons. An embedding application might want to restart Python without having to restart the application itself. An application that has loaded the Python interpreter from a dynamically loadable library (or DLL) might want to free all memory allocated by Python before unloading the DLL. During a hunt for memory leaks in an application a developer might want to free all memory allocated by Python before exiting from the application.

Bugs and caveats: The destruction of modules and objects in modules is done in random order; this may cause destructors (__del__() methods) to fail when they depend on other objects (even functions) or modules. Dynamically loaded extension modules loaded by Python are not unloaded. Small amounts of memory allocated by the Python interpreter may not be freed (if you find a leak, please report it). Memory tied up in circular references between objects is not freed. Some memory allocated by extension modules may not be freed. Some extensions may not work properly if their initialization routine is called more than once; this can happen if an application calls Py_Initialize() and Py_FinalizeEx() more than once.

3.6 新版功能.

void Py_Finalize()

This is a backwards-compatible version of Py_FinalizeEx() that disregards the return value.

Process-wide parameters

int Py_SetStandardStreamEncoding(const char *encoding, const char *errors)

This function should be called before Py_Initialize(), if it is called at all. It specifies which encoding and error handling to use with standard IO, with the same meanings as in str.encode().

It overrides PYTHONIOENCODING values, and allows embedding code to control IO encoding when the environment variable does not work.

encoding and/or errors may be NULL to use PYTHONIOENCODING and/or default values (depending on other settings).

Note that sys.stderr always uses the “backslashreplace” error handler, regardless of this (or any other) setting.

If Py_FinalizeEx() is called, this function will need to be called again in order to affect subsequent calls to Py_Initialize().

Returns 0 if successful, a nonzero value on error (e.g. calling after the interpreter has already been initialized).

3.4 新版功能.

void Py_SetProgramName(wchar_t *name)

This function should be called before Py_Initialize() is called for the first time, if it is called at all. It tells the interpreter the value of the argv[0] argument to the main() function of the program (converted to wide characters). This is used by Py_GetPath() and some other functions below to find the Python run-time libraries relative to the interpreter executable. The default value is 'python'. The argument should point to a zero-terminated wide character string in static storage whose contents will not change for the duration of the program’s execution. No code in the Python interpreter will change the contents of this storage.

Use Py_DecodeLocale() to decode a bytes string to get a wchar_* string.

wchar* Py_GetProgramName()

Return the program name set with Py_SetProgramName(), or the default. The returned string points into static storage; the caller should not modify its value.

wchar_t* Py_GetPrefix()

Return the prefix for installed platform-independent files. This is derived through a number of complicated rules from the program name set with Py_SetProgramName() and some environment variables; for example, if the program name is '/usr/local/bin/python', the prefix is '/usr/local'. The returned string points into static storage; the caller should not modify its value. This corresponds to the prefix variable in the top-level Makefile and the --prefix argument to the configure script at build time. The value is available to Python code as sys.prefix. It is only useful on Unix. See also the next function.

wchar_t* Py_GetExecPrefix()

Return the exec-prefix for installed platform-dependent files. This is derived through a number of complicated rules from the program name set with Py_SetProgramName() and some environment variables; for example, if the program name is '/usr/local/bin/python', the exec-prefix is '/usr/local'. The returned string points into static storage; the caller should not modify its value. This corresponds to the exec_prefix variable in the top-level Makefile and the --exec-prefix argument to the configure script at build time. The value is available to Python code as sys.exec_prefix. It is only useful on Unix.

Background: The exec-prefix differs from the prefix when platform dependent files (such as executables and shared libraries) are installed in a different directory tree. In a typical installation, platform dependent files may be installed in the /usr/local/plat subtree while platform independent may be installed in /usr/local.

Generally speaking, a platform is a combination of hardware and software families, e.g. Sparc machines running the Solaris 2.x operating system are considered the same platform, but Intel machines running Solaris 2.x are another platform, and Intel machines running Linux are yet another platform. Different major revisions of the same operating system generally also form different platforms. Non-Unix operating systems are a different story; the installation strategies on those systems are so different that the prefix and exec-prefix are meaningless, and set to the empty string. Note that compiled Python bytecode files are platform independent (but not independent from the Python version by which they were compiled!).

System administrators will know how to configure the mount or automount programs to share /usr/local between platforms while having /usr/local/plat be a different filesystem for each platform.

wchar_t* Py_GetProgramFullPath()

Return the full program name of the Python executable; this is computed as a side-effect of deriving the default module search path from the program name (set by Py_SetProgramName() above). The returned string points into static storage; the caller should not modify its value. The value is available to Python code as sys.executable.

wchar_t* Py_GetPath()

Return the default module search path; this is computed from the program name (set by Py_SetProgramName() above) and some environment variables. The returned string consists of a series of directory names separated by a platform dependent delimiter character. The delimiter character is ':' on Unix and Mac OS X, ';' on Windows. The returned string points into static storage; the caller should not modify its value. The list sys.path is initialized with this value on interpreter startup; it can be (and usually is) modified later to change the search path for loading modules.

void Py_SetPath(const wchar_t *)

Set the default module search path. If this function is called before Py_Initialize(), then Py_GetPath() won’t attempt to compute a default search path but uses the one provided instead. This is useful if Python is embedded by an application that has full knowledge of the location of all modules. The path components should be separated by the platform dependent delimiter character, which is ':' on Unix and Mac OS X, ';' on Windows.

This also causes sys.executable to be set only to the raw program name (see Py_SetProgramName()) and for sys.prefix and sys.exec_prefix to be empty. It is up to the caller to modify these if required after calling Py_Initialize().

Use Py_DecodeLocale() to decode a bytes string to get a wchar_* string.

The path argument is copied internally, so the caller may free it after the call completes.

const char* Py_GetVersion()

Return the version of this Python interpreter. This is a string that looks something like

"3.0a5+ (py3k:63103M, May 12 2008, 00:53:55) \n[GCC 4.2.3]"

The first word (up to the first space character) is the current Python version; the first three characters are the major and minor version separated by a period. The returned string points into static storage; the caller should not modify its value. The value is available to Python code as sys.version.

const char* Py_GetPlatform()

Return the platform identifier for the current platform. On Unix, this is formed from the “official” name of the operating system, converted to lower case, followed by the major revision number; e.g., for Solaris 2.x, which is also known as SunOS 5.x, the value is 'sunos5'. On Mac OS X, it is 'darwin'. On Windows, it is 'win'. The returned string points into static storage; the caller should not modify its value. The value is available to Python code as sys.platform.

const char* Py_GetCopyright()

Return the official copyright string for the current Python version, for example

'Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam'

The returned string points into static storage; the caller should not modify its value. The value is available to Python code as sys.copyright.

const char* Py_GetCompiler()

Return an indication of the compiler used to build the current Python version, in square brackets, for example:

"[GCC 2.7.2.2]"

The returned string points into static storage; the caller should not modify its value. The value is available to Python code as part of the variable sys.version.

const char* Py_GetBuildInfo()

Return information about the sequence number and build date and time of the current Python interpreter instance, for example

"#67, Aug  1 1997, 22:34:28"

The returned string points into static storage; the caller should not modify its value. The value is available to Python code as part of the variable sys.version.

void PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath)

Set sys.argv based on argc and argv. These parameters are similar to those passed to the program’s main() function with the difference that the first entry should refer to the script file to be executed rather than the executable hosting the Python interpreter. If there isn’t a script that will be run, the first entry in argv can be an empty string. If this function fails to initialize sys.argv, a fatal condition is signalled using Py_FatalError().

If updatepath is zero, this is all the function does. If updatepath is non-zero, the function also modifies sys.path according to the following algorithm:

If the name of an existing script is passed in argv[0], the absolute path of the directory where the script is located is prepended to sys.path.

Otherwise (that is, if argc is 0 or argv[0] doesn’t point to an existing file name), an empty string is prepended to sys.path, which is the same as prepending the current working directory (".").

Use Py_DecodeLocale() to decode a bytes string to get a wchar_* string.

注解

It is recommended that applications embedding the Python interpreter for purposes other than executing a single script pass 0 as updatepath, and update sys.path themselves if desired. See CVE-2008-5983 [https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983].

On versions before 3.1.3, you can achieve the same effect by manually popping the first sys.path element after having called PySys_SetArgv(), for example using:

PyRun_SimpleString("import sys; sys.path.pop(0)\n");

3.1.3 新版功能.

void PySys_SetArgv(int argc, wchar_t **argv)

This function works like PySys_SetArgvEx() with updatepath set to 1 unless the python interpreter was started with the -I.

Use Py_DecodeLocale() to decode a bytes string to get a wchar_* string.

在 3.4 版更改: The updatepath value depends on -I.

void Py_SetPythonHome(wchar_t *home)

Set the default “home” directory, that is, the location of the standard Python libraries. See PYTHONHOME for the meaning of the argument string.

The argument should point to a zero-terminated character string in static storage whose contents will not change for the duration of the program’s execution. No code in the Python interpreter will change the contents of this storage.

Use Py_DecodeLocale() to decode a bytes string to get a wchar_* string.

w_char* Py_GetPythonHome()

Return the default “home”, that is, the value set by a previous call to Py_SetPythonHome(), or the value of the PYTHONHOME environment variable if it is set.

Thread State and the Global Interpreter Lock

The Python interpreter is not fully thread-safe. In order to support multi-threaded Python programs, there’s a global lock, called the global interpreter lock or GIL, that must be held by the current thread before it can safely access Python objects. Without the lock, even the simplest operations could cause problems in a multi-threaded program: for example, when two threads simultaneously increment the reference count of the same object, the reference count could end up being incremented only once instead of twice.

Therefore, the rule exists that only the thread that has acquired the GIL may operate on Python objects or call Python/C API functions. In order to emulate concurrency of execution, the interpreter regularly tries to switch threads (see sys.setswitchinterval()). The lock is also released around potentially blocking I/O operations like reading or writing a file, so that other Python threads can run in the meantime.

The Python interpreter keeps some thread-specific bookkeeping information inside a data structure called PyThreadState. There’s also one global variable pointing to the current PyThreadState: it can be retrieved using PyThreadState_Get().

Releasing the GIL from extension code

Most extension code manipulating the GIL has the following simple structure:

Save the thread state in a local variable.
Release the global interpreter lock.
... Do some blocking I/O operation ...
Reacquire the global interpreter lock.
Restore the thread state from the local variable.

This is so common that a pair of macros exists to simplify it:

Py_BEGIN_ALLOW_THREADS
... Do some blocking I/O operation ...
Py_END_ALLOW_THREADS

The Py_BEGIN_ALLOW_THREADS macro opens a new block and declares a hidden local variable; the Py_END_ALLOW_THREADS macro closes the block. These two macros are still available when Python is compiled without thread support (they simply have an empty expansion).

When thread support is enabled, the block above expands to the following code:

PyThreadState *_save;

_save = PyEval_SaveThread();
...Do some blocking I/O operation...
PyEval_RestoreThread(_save);

Here is how these functions work: the global interpreter lock is used to protect the pointer to the current thread state. When releasing the lock and saving the thread state, the current thread state pointer must be retrieved before the lock is released (since another thread could immediately acquire the lock and store its own thread state in the global variable). Conversely, when acquiring the lock and restoring the thread state, the lock must be acquired before storing the thread state pointer.

注解

Calling system I/O functions is the most common use case for releasing the GIL, but it can also be useful before calling long-running computations which don’t need access to Python objects, such as compression or cryptographic functions operating over memory buffers. For example, the standard zlib and hashlib modules release the GIL when compressing or hashing data.

非Python创建的线程

When threads are created using the dedicated Python APIs (such as the threading module), a thread state is automatically associated to them and the code showed above is therefore correct. However, when threads are created from C (for example by a third-party library with its own thread management), they don’t hold the GIL, nor is there a thread state structure for them.

If you need to call Python code from these threads (often this will be part of a callback API provided by the aforementioned third-party library), you must first register these threads with the interpreter by creating a thread state data structure, then acquiring the GIL, and finally storing their thread state pointer, before you can start using the Python/C API. When you are done, you should reset the thread state pointer, release the GIL, and finally free the thread state data structure.

The PyGILState_Ensure() and PyGILState_Release() functions do all of the above automatically. The typical idiom for calling into Python from a C thread is:

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

/* Perform Python actions here. */
result = CallSomeFunction();
/* evaluate result or handle exception */

/* Release the thread. No Python API allowed beyond this point. */
PyGILState_Release(gstate);

Note that the PyGILState_*() functions assume there is only one global interpreter (created automatically by Py_Initialize()). Python supports the creation of additional interpreters (using Py_NewInterpreter()), but mixing multiple interpreters and the PyGILState_*() API is unsupported.

Another important thing to note about threads is their behaviour in the face of the C fork() call. On most systems with fork(), after a process forks only the thread that issued the fork will exist. That also means any locks held by other threads will never be released. Python solves this for os.fork() by acquiring the locks it uses internally before the fork, and releasing them afterwards. In addition, it resets any 锁对象 in the child. When extending or embedding Python, there is no way to inform Python of additional (non-Python) locks that need to be acquired before or reset after a fork. OS facilities such as pthread_atfork() would need to be used to accomplish the same thing. Additionally, when extending or embedding Python, calling fork() directly rather than through os.fork() (and returning to or calling into Python) may result in a deadlock by one of Python’s internal locks being held by a thread that is defunct after the fork. PyOS_AfterFork() tries to reset the necessary locks, but is not always able to.

高阶 API

These are the most commonly used types and functions when writing C extension code, or when embedding the Python interpreter:

PyInterpreterState

This data structure represents the state shared by a number of cooperating threads. Threads belonging to the same interpreter share their module administration and a few other internal items. There are no public members in this structure.

Threads belonging to different interpreters initially share nothing, except process state like available memory, open file descriptors and such. The global interpreter lock is also shared by all threads, regardless of to which interpreter they belong.

PyThreadState

This data structure represents the state of a single thread. The only public data member is PyInterpreterState *interp, which points to this thread’s interpreter state.

void PyEval_InitThreads()

Initialize and acquire the global interpreter lock. It should be called in the main thread before creating a second thread or engaging in any other thread operations such as PyEval_ReleaseThread(tstate). It is not needed before calling PyEval_SaveThread() or PyEval_RestoreThread().

This is a no-op when called for a second time.

在 3.2 版更改: This function cannot be called before Py_Initialize() anymore.

注解

When only the main thread exists, no GIL operations are needed. This is a common situation (most Python programs do not use threads), and the lock operations slow the interpreter down a bit. Therefore, the lock is not created initially. This situation is equivalent to having acquired the lock: when there is only a single thread, all object accesses are safe. Therefore, when this function initializes the global interpreter lock, it also acquires it. Before the Python _thread module creates a new thread, knowing that either it has the lock or the lock hasn’t been created yet, it calls PyEval_InitThreads(). When this call returns, it is guaranteed that the lock has been created and that the calling thread has acquired it.

It is not safe to call this function when it is unknown which thread (if any) currently has the global interpreter lock.

This function is not available when thread support is disabled at compile time.

int PyEval_ThreadsInitialized()

Returns a non-zero value if PyEval_InitThreads() has been called. This function can be called without holding the GIL, and therefore can be used to avoid calls to the locking API when running single-threaded. This function is not available when thread support is disabled at compile time.

PyThreadState* PyEval_SaveThread()

Release the global interpreter lock (if it has been created and thread support is enabled) and reset the thread state to NULL, returning the previous thread state (which is not NULL). If the lock has been created, the current thread must have acquired it. (This function is available even when thread support is disabled at compile time.)

void PyEval_RestoreThread(PyThreadState *tstate)

Acquire the global interpreter lock (if it has been created and thread support is enabled) and set the thread state to tstate, which must not be NULL. If the lock has been created, the current thread must not have acquired it, otherwise deadlock ensues. (This function is available even when thread support is disabled at compile time.)

PyThreadState* PyThreadState_Get()

Return the current thread state. The global interpreter lock must be held. When the current thread state is NULL, this issues a fatal error (so that the caller needn’t check for NULL).

PyThreadState* PyThreadState_Swap(PyThreadState *tstate)

Swap the current thread state with the thread state given by the argument tstate, which may be NULL. The global interpreter lock must be held and is not released.

void PyEval_ReInitThreads()

This function is called from PyOS_AfterFork() to ensure that newly created child processes don’t hold locks referring to threads which are not running in the child process.

The following functions use thread-local storage, and are not compatible with sub-interpreters:

PyGILState_STATE PyGILState_Ensure()

Ensure that the current thread is ready to call the Python C API regardless of the current state of Python, or of the global interpreter lock. This may be called as many times as desired by a thread as long as each call is matched with a call to PyGILState_Release(). In general, other thread-related APIs may be used between PyGILState_Ensure() and PyGILState_Release() calls as long as the thread state is restored to its previous state before the Release(). For example, normal usage of the Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS macros is acceptable.

The return value is an opaque “handle” to the thread state when PyGILState_Ensure() was called, and must be passed to PyGILState_Release() to ensure Python is left in the same state. Even though recursive calls are allowed, these handles cannot be shared - each unique call to PyGILState_Ensure() must save the handle for its call to PyGILState_Release().

When the function returns, the current thread will hold the GIL and be able to call arbitrary Python code. Failure is a fatal error.

void PyGILState_Release(PyGILState_STATE)

Release any resources previously acquired. After this call, Python’s state will be the same as it was prior to the corresponding PyGILState_Ensure() call (but generally this state will be unknown to the caller, hence the use of the GILState API).

Every call to PyGILState_Ensure() must be matched by a call to PyGILState_Release() on the same thread.

PyThreadState* PyGILState_GetThisThreadState()

Get the current thread state for this thread. May return NULL if no GILState API has been used on the current thread. Note that the main thread always has such a thread-state, even if no auto-thread-state call has been made on the main thread. This is mainly a helper/diagnostic function.

int PyGILState_Check()

Return 1 if the current thread is holding the GIL and 0 otherwise. This function can be called from any thread at any time. Only if it has had its Python thread state initialized and currently is holding the GIL will it return 1. This is mainly a helper/diagnostic function. It can be useful for example in callback contexts or memory allocation functions when knowing that the GIL is locked can allow the caller to perform sensitive actions or otherwise behave differently.

3.4 新版功能.

The following macros are normally used without a trailing semicolon; look for example usage in the Python source distribution.

Py_BEGIN_ALLOW_THREADS

This macro expands to { PyThreadState *_save; _save = PyEval_SaveThread();. Note that it contains an opening brace; it must be matched with a following Py_END_ALLOW_THREADS macro. See above for further discussion of this macro. It is a no-op when thread support is disabled at compile time.

Py_END_ALLOW_THREADS

This macro expands to PyEval_RestoreThread(_save); }. Note that it contains a closing brace; it must be matched with an earlier Py_BEGIN_ALLOW_THREADS macro. See above for further discussion of this macro. It is a no-op when thread support is disabled at compile time.

Py_BLOCK_THREADS

This macro expands to PyEval_RestoreThread(_save);: it is equivalent to Py_END_ALLOW_THREADS without the closing brace. It is a no-op when thread support is disabled at compile time.

Py_UNBLOCK_THREADS

This macro expands to _save = PyEval_SaveThread();: it is equivalent to Py_BEGIN_ALLOW_THREADS without the opening brace and variable declaration. It is a no-op when thread support is disabled at compile time.

Low-level API

All of the following functions are only available when thread support is enabled at compile time, and must be called only when the global interpreter lock has been created.

PyInterpreterState* PyInterpreterState_New()

Create a new interpreter state object. The global interpreter lock need not be held, but may be held if it is necessary to serialize calls to this function.

void PyInterpreterState_Clear(PyInterpreterState *interp)

Reset all information in an interpreter state object. The global interpreter lock must be held.

void PyInterpreterState_Delete(PyInterpreterState *interp)

Destroy an interpreter state object. The global interpreter lock need not be held. The interpreter state must have been reset with a previous call to PyInterpreterState_Clear().

PyThreadState* PyThreadState_New(PyInterpreterState *interp)

Create a new thread state object belonging to the given interpreter object. The global interpreter lock need not be held, but may be held if it is necessary to serialize calls to this function.

void PyThreadState_Clear(PyThreadState *tstate)

Reset all information in a thread state object. The global interpreter lock must be held.

void PyThreadState_Delete(PyThreadState *tstate)

Destroy a thread state object. The global interpreter lock need not be held. The thread state must have been reset with a previous call to PyThreadState_Clear().

PyObject* PyThreadState_GetDict()

Return value: Borrowed reference.

Return a dictionary in which extensions can store thread-specific state information. Each extension should use a unique key to use to store state in the dictionary. It is okay to call this function when no current thread state is available. If this function returns NULL, no exception has been raised and the caller should assume no current thread state is available.

int PyThreadState_SetAsyncExc(long id, PyObject *exc)

Asynchronously raise an exception in a thread. The id argument is the thread id of the target thread; exc is the exception object to be raised. This function does not steal any references to exc. To prevent naive misuse, you must write your own C extension to call this. Must be called with the GIL held. Returns the number of thread states modified; this is normally one, but will be zero if the thread id isn’t found. If exc is NULL, the pending exception (if any) for the thread is cleared. This raises no exceptions.

void PyEval_AcquireThread(PyThreadState *tstate)

Acquire the global interpreter lock and set the current thread state to tstate, which should not be NULL. The lock must have been created earlier. If this thread already has the lock, deadlock ensues.

PyEval_RestoreThread() is a higher-level function which is always available (even when thread support isn’t enabled or when threads have not been initialized).

void PyEval_ReleaseThread(PyThreadState *tstate)

Reset the current thread state to NULL and release the global interpreter lock. The lock must have been created earlier and must be held by the current thread. The tstate argument, which must not be NULL, is only used to check that it represents the current thread state — if it isn’t, a fatal error is reported.

PyEval_SaveThread() is a higher-level function which is always available (even when thread support isn’t enabled or when threads have not been initialized).

void PyEval_AcquireLock()

Acquire the global interpreter lock. The lock must have been created earlier. If this thread already has the lock, a deadlock ensues.

3.2 版后已移除: This function does not update the current thread state. Please use PyEval_RestoreThread() or PyEval_AcquireThread() instead.

void PyEval_ReleaseLock()

Release the global interpreter lock. The lock must have been created earlier.

3.2 版后已移除: This function does not update the current thread state. Please use PyEval_SaveThread() or PyEval_ReleaseThread() instead.

Sub-interpreter support

While in most uses, you will only embed a single Python interpreter, there are cases where you need to create several independent interpreters in the same process and perhaps even in the same thread. Sub-interpreters allow you to do that. You can switch between sub-interpreters using the PyThreadState_Swap() function. You can create and destroy them using the following functions:

PyThreadState* Py_NewInterpreter()

Create a new sub-interpreter. This is an (almost) totally separate environment for the execution of Python code. In particular, the new interpreter has separate, independent versions of all imported modules, including the fundamental modules builtins, __main__ and sys. The table of loaded modules (sys.modules) and the module search path (sys.path) are also separate. The new environment has no sys.argv variable. It has new standard I/O stream file objects sys.stdin, sys.stdout and sys.stderr (however these refer to the same underlying file descriptors).

The return value points to the first thread state created in the new sub-interpreter. This thread state is made in the current thread state. Note that no actual thread is created; see the discussion of thread states below. If creation of the new interpreter is unsuccessful, NULL is returned; no exception is set since the exception state is stored in the current thread state and there may not be a current thread state. (Like all other Python/C API functions, the global interpreter lock must be held before calling this function and is still held when it returns; however, unlike most other Python/C API functions, there needn’t be a current thread state on entry.)

Extension modules are shared between (sub-)interpreters as follows: the first time a particular extension is imported, it is initialized normally, and a (shallow) copy of its module’s dictionary is squirreled away. When the same extension is imported by another (sub-)interpreter, a new module is initialized and filled with the contents of this copy; the extension’s init function is not called. Note that this is different from what happens when an extension is imported after the interpreter has been completely re-initialized by calling Py_FinalizeEx() and Py_Initialize(); in that case, the extension’s initmodule function is called again.

void Py_EndInterpreter(PyThreadState *tstate)

Destroy the (sub-)interpreter represented by the given thread state. The given thread state must be the current thread state. See the discussion of thread states below. When the call returns, the current thread state is NULL. All thread states associated with this interpreter are destroyed. (The global interpreter lock must be held before calling this function and is still held when it returns.) Py_FinalizeEx() will destroy all sub-interpreters that haven’t been explicitly destroyed at that point.

错误和警告

Because sub-interpreters (and the main interpreter) are part of the same process, the insulation between them isn’t perfect — for example, using low-level file operations like os.close() they can (accidentally or maliciously) affect each other’s open files. Because of the way extensions are shared between (sub-)interpreters, some extensions may not work properly; this is especially likely when the extension makes use of (static) global variables, or when the extension manipulates its module’s dictionary after its initialization. It is possible to insert objects created in one sub-interpreter into a namespace of another sub-interpreter; this should be done with great care to avoid sharing user-defined functions, methods, instances or classes between sub-interpreters, since import operations executed by such objects may affect the wrong (sub-)interpreter’s dictionary of loaded modules.

Also note that combining this functionality with PyGILState_*() APIs is delicate, because these APIs assume a bijection between Python thread states and OS-level threads, an assumption broken by the presence of sub-interpreters. It is highly recommended that you don’t switch sub-interpreters between a pair of matching PyGILState_Ensure() and PyGILState_Release() calls. Furthermore, extensions (such as ctypes) using these APIs to allow calling of Python code from non-Python created threads will probably be broken when using sub-interpreters.

异步通知

A mechanism is provided to make asynchronous notifications to the main interpreter thread. These notifications take the form of a function pointer and a void pointer argument.

int Py_AddPendingCall(int (*func)(void *), void *arg)

Schedule a function to be called from the main interpreter thread. On success, 0 is returned and func is queued for being called in the main thread. On failure, -1 is returned without setting any exception.

When successfully queued, func will be eventually called from the main interpreter thread with the argument arg. It will be called asynchronously with respect to normally running Python code, but with both these conditions met:

on a bytecode boundary;

with the main thread holding the global interpreter lock (func can therefore use the full C API).

func must return 0 on success, or -1 on failure with an exception set. func won’t be interrupted to perform another asynchronous notification recursively, but it can still be interrupted to switch threads if the global interpreter lock is released.

This function doesn’t need a current thread state to run, and it doesn’t need the global interpreter lock.

警告

This is a low-level function, only useful for very special cases. There is no guarantee that func will be called as quick as possible. If the main thread is busy executing a system call, func won’t be called before the system call returns. This function is generally not suitable for calling Python code from arbitrary C threads. Instead, use the PyGILState API.

3.1 新版功能.

分析和跟踪

The Python interpreter provides some low-level support for attaching profiling and execution tracing facilities. These are used for profiling, debugging, and coverage analysis tools.

This C interface allows the profiling or tracing code to avoid the overhead of calling through Python-level callable objects, making a direct C function call instead. The essential attributes of the facility have not changed; the interface allows trace functions to be installed per-thread, and the basic events reported to the trace function are the same as had been reported to the Python-level trace functions in previous versions.

int (*Py_tracefunc)(PyObject *obj, PyFrameObject *frame, int what, PyObject *arg)

The type of the trace function registered using PyEval_SetProfile() and PyEval_SetTrace(). The first parameter is the object passed to the registration function as obj, frame is the frame object to which the event pertains, what is one of the constants PyTrace_CALL, PyTrace_EXCEPTION, PyTrace_LINE, PyTrace_RETURN, PyTrace_C_CALL, PyTrace_C_EXCEPTION, or PyTrace_C_RETURN, and arg depends on the value of what:

what 的值

arg 的含义

PyTrace_CALL

总是 Py_None.

PyTrace_EXCEPTION

sys.exc_info() 返回的异常信息。

PyTrace_LINE

总是 Py_None.

PyTrace_RETURN

Value being returned to the caller, or NULL if caused by an exception.

PyTrace_C_CALL

正在调用函数对象。

PyTrace_C_EXCEPTION

正在调用函数对象。

PyTrace_C_RETURN

正在调用函数对象。

int PyTrace_CALL

The value of the what parameter to a Py_tracefunc function when a new call to a function or method is being reported, or a new entry into a generator. Note that the creation of the iterator for a generator function is not reported as there is no control transfer to the Python bytecode in the corresponding frame.

int PyTrace_EXCEPTION

The value of the what parameter to a Py_tracefunc function when an exception has been raised. The callback function is called with this value for what when after any bytecode is processed after which the exception becomes set within the frame being executed. The effect of this is that as exception propagation causes the Python stack to unwind, the callback is called upon return to each frame as the exception propagates. Only trace functions receives these events; they are not needed by the profiler.

int PyTrace_LINE

The value passed as the what parameter to a trace function (but not a profiling function) when a line-number event is being reported.

int PyTrace_RETURN

The value for the what parameter to Py_tracefunc functions when a call is about to return.

int PyTrace_C_CALL

The value for the what parameter to Py_tracefunc functions when a C function is about to be called.

int PyTrace_C_EXCEPTION

The value for the what parameter to Py_tracefunc functions when a C function has raised an exception.

int PyTrace_C_RETURN

The value for the what parameter to Py_tracefunc functions when a C function has returned.

void PyEval_SetProfile(Py_tracefunc func, PyObject *obj)

Set the profiler function to func. The obj parameter is passed to the function as its first parameter, and may be any Python object, or NULL. If the profile function needs to maintain state, using a different value for obj for each thread provides a convenient and thread-safe place to store it. The profile function is called for all monitored events except PyTrace_LINE and PyTrace_EXCEPTION.

void PyEval_SetTrace(Py_tracefunc func, PyObject *obj)

Set the tracing function to func. This is similar to PyEval_SetProfile(), except the tracing function does receive line-number events and does not receive any event related to C function objects being called. Any trace function registered using PyEval_SetTrace() will not receive PyTrace_C_CALL, PyTrace_C_EXCEPTION or PyTrace_C_RETURN as a value for the what parameter.

PyObject* PyEval_GetCallStats(PyObject *self)

Return a tuple of function call counts. There are constants defined for the positions within the tuple:

Name

Value

PCALL_ALL

0

PCALL_FUNCTION

1

PCALL_FAST_FUNCTION

2

PCALL_FASTER_FUNCTION

3

PCALL_METHOD

4

PCALL_BOUND_METHOD

5

PCALL_CFUNCTION

6

PCALL_TYPE

7

PCALL_GENERATOR

8

PCALL_OTHER

9

PCALL_POP

10

PCALL_FAST_FUNCTION means no argument tuple needs to be created. PCALL_FASTER_FUNCTION means that the fast-path frame setup code is used.

If there is a method call where the call can be optimized by changing the argument tuple and calling the function directly, it gets recorded twice.

This function is only present if Python is compiled with CALL_PROFILE defined.

高级调试器支持

These functions are only intended to be used by advanced debugging tools.

PyInterpreterState* PyInterpreterState_Head()

Return the interpreter state object at the head of the list of all such objects.

PyInterpreterState* PyInterpreterState_Next(PyInterpreterState *interp)

Return the next interpreter state object after interp from the list of all such objects.

PyThreadState * PyInterpreterState_ThreadHead(PyInterpreterState *interp)

Return the pointer to the first PyThreadState object in the list of threads associated with the interpreter interp.

PyThreadState* PyThreadState_Next(PyThreadState *tstate)

Return the next thread state object after tstate from the list of all such objects belonging to the same PyInterpreterState object.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

内存管理

概述

在 Python 中，内存管理涉及到一个包含所有 Python 对象和数据结构的私有堆（heap）。这个私有堆的管理由内部的 Python 内存管理器（Python memory manager） 保证。Python 内存管理器有不同的组件来处理各种动态存储管理方面的问题，如共享、分割、预分配或缓存。

在最底层，一个原始内存分配器通过与操作系统的内存管理器交互，确保私有堆中有足够的空间来存储所有与 Python 相关的数据。在原始内存分配器的基础上，几个对象特定的分配器在同一堆上运行，并根据每种对象类型的特点实现不同的内存管理策略。例如，整数对象在堆内的管理方式不同于字符串、元组或字典，因为整数需要不同的存储需求和速度与空间的权衡。因此，Python 内存管理器将一些工作分配给对象特定分配器，但确保后者在私有堆的范围内运行。

Python 堆内存的管理是由解释器来执行，用户对它没有控制权，即使他们经常操作指向堆内内存块的对象指针，理解这一点十分重要。Python 对象和其他内部缓冲区的堆空间分配是由 Python 内存管理器按需通过本文档中列出的 Python/C API 函数进行的。

为了避免内存破坏，扩展的作者永远不应该试图用 C 库函数导出的函数来对 Python 对象进行操作，这些函数包括： malloc(), calloc(), realloc() 和 free()。这将导致 C 分配器和 Python 内存管理器之间的混用，引发严重后果，这是由于它们实现了不同的算法，并在不同的堆上操作。但是，我们可以安全地使用 C 库分配器为单独的目的分配和释放内存块，如下例所示：

PyObject *res;
char *buf = (char *) malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
...Do some I/O operation involving buf...
res = PyBytes_FromString(buf);
free(buf); /* malloc'ed */
return res;

In this example, the memory request for the I/O buffer is handled by the C library allocator. The Python memory manager is involved only in the allocation of the string object returned as a result.

然而，在大多数情况下，建议专门从 Python 堆中分配内存，因为后者由 Python 内存管理器控制。例如，当解释器扩展了用 C 写的新对象类型时，就必须这样做。使用 Python 堆的另一个原因是希望*通知* Python 内存管理器关于扩展模块的内存需求。即使所请求的内存全部只用于内部的、高度特定的目的，将所有的内存请求交给 Python 内存管理器能让解释器对其内存占用的整体情况有更准确的了解。因此，在某些情况下，Python 内存管理器可能会触发或不触发适当的操作，如垃圾回收、内存压缩或其他预防性操作。请注意，通过使用前面例子中所示的 C 库分配器，为 I/O 缓冲区分配的内存会完全不受 Python 内存管理器管理。

参见

环境变量 PYTHONMALLOC 可被用来配置 Python 所使用的内存分配器。

环境变量 PYTHONMALLOCSTATS 可以用来在每次创建和关闭新的 pymalloc 对象区域时打印 pymalloc 内存分配器 的统计数据。

原始内存接口

以下函数集封装了系统分配器。这些函数是线程安全的，不需要持有 GIL。

The default raw memory block allocator uses the following functions: malloc(), calloc(), realloc() and free(); call malloc(1) (or calloc(1, 1)) when requesting zero bytes.

3.4 新版功能.

void* PyMem_RawMalloc(size_t n)

Allocates n bytes and returns a pointer of type void* to the allocated memory, or NULL if the request fails.

Requesting zero bytes returns a distinct non-NULL pointer if possible, as if PyMem_RawMalloc(1) had been called instead. The memory will not have been initialized in any way.

void* PyMem_RawCalloc(size_t nelem, size_t elsize)

Allocates nelem elements each whose size in bytes is elsize and returns a pointer of type void* to the allocated memory, or NULL if the request fails. The memory is initialized to zeros.

Requesting zero elements or elements of size zero bytes returns a distinct non-NULL pointer if possible, as if PyMem_RawCalloc(1, 1) had been called instead.

3.5 新版功能.

void* PyMem_RawRealloc(void *p, size_t n)

将 p 指向的内存块大小调整为 n 字节。以新旧内存块大小中的最小值为准，其中内容保持不变，

If p is NULL, the call is equivalent to PyMem_RawMalloc(n); else if n is equal to zero, the memory block is resized but is not freed, and the returned pointer is non-NULL.

Unless p is NULL, it must have been returned by a previous call to PyMem_RawMalloc(), PyMem_RawRealloc() or PyMem_RawCalloc().

If the request fails, PyMem_RawRealloc() returns NULL and p remains a valid pointer to the previous memory area.

void PyMem_RawFree(void *p)

释放 p 指向的内存块。除非 p 是 NULL ，否则它必须是之前调用 PyMem_RawMalloc() 、 PyMem_RawRealloc() 或 PyMem_RawCalloc() 所返回的指针。否则，或在 PyMem_RawFree(p) 之前已经调用过的情况下，未定义的行为会发生。

If p is NULL, no operation is performed.

内存接口

以下函数集，仿照 ANSI C 标准，并指定了请求零字节时的行为，可用于从Python堆分配和释放内存。

By default, these functions use pymalloc memory allocator.

警告

在使用这些函数时，必须持有 全局解释器锁（GIL） 。

在 3.6 版更改: 现在默认的分配器是 pymalloc 而非系统的 malloc() 。

void* PyMem_Malloc(size_t n)

Allocates n bytes and returns a pointer of type void* to the allocated memory, or NULL if the request fails.

Requesting zero bytes returns a distinct non-NULL pointer if possible, as if PyMem_Malloc(1) had been called instead. The memory will not have been initialized in any way.

void* PyMem_Calloc(size_t nelem, size_t elsize)

Allocates nelem elements each whose size in bytes is elsize and returns a pointer of type void* to the allocated memory, or NULL if the request fails. The memory is initialized to zeros.

Requesting zero elements or elements of size zero bytes returns a distinct non-NULL pointer if possible, as if PyMem_Calloc(1, 1) had been called instead.

3.5 新版功能.

void* PyMem_Realloc(void *p, size_t n)

将 p 指向的内存块大小调整为 n 字节。以新旧内存块大小中的最小值为准，其中内容保持不变，

If p is NULL, the call is equivalent to PyMem_Malloc(n); else if n is equal to zero, the memory block is resized but is not freed, and the returned pointer is non-NULL.

Unless p is NULL, it must have been returned by a previous call to PyMem_Malloc(), PyMem_Realloc() or PyMem_Calloc().

If the request fails, PyMem_Realloc() returns NULL and p remains a valid pointer to the previous memory area.

void PyMem_Free(void *p)

释放 p 指向的内存块。除非 p 是 NULL ，否则它必须是之前调用 PyMem_Malloc() 、 PyMem_Realloc() 或 PyMem_Calloc() 所返回的指针。否则，或在 PyMem_Free(p) 之前已经调用过的情况下，未定义的行为会发生。

If p is NULL, no operation is performed.

以下面向类型的宏为方便而提供。 注意 TYPE 可以指任何 C 类型。

TYPE* PyMem_New(TYPE, size_t n)

与 PyMem_Malloc() 相同，但分配 (n * sizeof(TYPE)) 字节的内存。返回一个转换为 TYPE* 的指针。内存不会以任何方式被初始化。

TYPE* PyMem_Resize(void *p, TYPE, size_t n)

Same as PyMem_Realloc(), but the memory block is resized to (n * sizeof(TYPE)) bytes. Returns a pointer cast to TYPE*. On return, p will be a pointer to the new memory area, or NULL in the event of failure.

这是一个 C 预处理宏， p 总是被重新赋值。请保存 p 的原始值，以避免在处理错误时丢失内存。

void PyMem_Del(void *p)

与 PyMem_Free() 相同

此外，我们还提供了以下宏集用于直接调用 Python 内存分配器，而不涉及上面列出的 C API 函数。但是请注意，使用它们并不能保证跨 Python 版本的二进制兼容性，因此在扩展模块被弃用。

PyMem_MALLOC(size)

PyMem_NEW(type, size)

PyMem_REALLOC(ptr, size)

PyMem_RESIZE(ptr, type, size)

PyMem_FREE(ptr)

PyMem_DEL(ptr)

对象分配器

以下函数集，仿照 ANSI C 标准，并指定了请求零字节时的行为，可用于从Python堆分配和释放内存。

By default, these functions use pymalloc memory allocator.

警告

在使用这些函数时，必须持有 全局解释器锁（GIL） 。

void* PyObject_Malloc(size_t n)

Allocates n bytes and returns a pointer of type void* to the allocated memory, or NULL if the request fails.

Requesting zero bytes returns a distinct non-NULL pointer if possible, as if PyObject_Malloc(1) had been called instead. The memory will not have been initialized in any way.

void* PyObject_Calloc(size_t nelem, size_t elsize)

Allocates nelem elements each whose size in bytes is elsize and returns a pointer of type void* to the allocated memory, or NULL if the request fails. The memory is initialized to zeros.

Requesting zero elements or elements of size zero bytes returns a distinct non-NULL pointer if possible, as if PyObject_Calloc(1, 1) had been called instead.

3.5 新版功能.

void* PyObject_Realloc(void *p, size_t n)

将 p 指向的内存块大小调整为 n 字节。以新旧内存块大小中的最小值为准，其中内容保持不变，

If p is NULL, the call is equivalent to PyObject_Malloc(n); else if n is equal to zero, the memory block is resized but is not freed, and the returned pointer is non-NULL.

Unless p is NULL, it must have been returned by a previous call to PyObject_Malloc(), PyObject_Realloc() or PyObject_Calloc().

If the request fails, PyObject_Realloc() returns NULL and p remains a valid pointer to the previous memory area.

void PyObject_Free(void *p)

释放 p 指向的内存块。除非 p 是 NULL ，否则它必须是之前调用 PyObject_Malloc() 、 PyObject_Realloc() 或 PyObject_Calloc() 所返回的指针。否则，或在 PyObject_Free(p) 之前已经调用过的情况下，未定义的行为会发生。

If p is NULL, no operation is performed.

自定义内存分配器

3.4 新版功能.

PyMemAllocatorEx

用于描述内存块分配器的结构体。包含四个字段：

域

含义

void *ctx

作为第一个参数传入的用户上下文

void* malloc(void *ctx, size_t size)

分配一个内存块

void* calloc(void *ctx, size_t nelem, size_t elsize)

分配一个初始化为 0 的内存块

void* realloc(void *ctx, void *ptr, size_t new_size)

分配一个内存块或调整其大小

void free(void *ctx, void *ptr)

释放一个内存块

在 3.5 版更改: The PyMemAllocator structure was renamed to PyMemAllocatorEx and a new calloc field was added.

PyMemAllocatorDomain

用来识别分配器域的枚举类。域有：

PYMEM_DOMAIN_RAW

函数

PyMem_RawMalloc()

PyMem_RawRealloc()

PyMem_RawCalloc()

PyMem_RawFree()

PYMEM_DOMAIN_MEM

函数

PyMem_Malloc(),

PyMem_Realloc()

PyMem_Calloc()

PyMem_Free()

PYMEM_DOMAIN_OBJ

函数

PyObject_Malloc()

PyObject_Realloc()

PyObject_Calloc()

PyObject_Free()

void PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)

获取指定域的内存块分配器。

void PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)

设置指定域的内存块分配器。

The new allocator must return a distinct non-NULL pointer when requesting zero bytes.

对于 PYMEM_DOMAIN_RAW 域，分配器必须是线程安全的：当分配器被调用时，不持有 全局解释器锁 。

如果新的分配器不是钩子（不调用之前的分配器），必须调用 PyMem_SetupDebugHooks() 函数在新分配器上重新安装调试钩子。

void PyMem_SetupDebugHooks(void)

设置检测 Python 内存分配器函数中错误的钩子。

Newly allocated memory is filled with the byte 0xCB, freed memory is filled with the byte 0xDB.

运行时检查：

检测对 API 的违反，例如：对用 PyMem_Malloc() 分配的缓冲区调用 PyObject_Free() 。

检测缓冲区起始位置前的写入（缓冲区下溢）。

检测缓冲区终止位置后的写入（缓冲区溢出）。

检测当调用 PYMEM_DOMAIN_OBJ (如: PyObject_Malloc()) 和 PYMEM_DOMAIN_MEM (如: PyMem_Malloc()) 域的分配器函数时 GIL 已被保持。

在出错时，调试钩子使用 tracemalloc 模块来回溯内存块被分配的位置。只有当 tracemalloc 正在追踪 Python 内存分配，并且内存块被追踪时，才会显示回溯。

These hooks are installed by default if Python is compiled in debug mode. The PYTHONMALLOC environment variable can be used to install debug hooks on a Python compiled in release mode.

在 3.6 版更改: 这个函数现在也适用于以 发布模式编译的 Python。在出错时，调试钩子现在使用 tracemalloc 来回溯内存块被分配的位置。调试钩子现在也检查当 PYMEM_DOMAIN_OBJ 和 PYMEM_DOMAIN_MEM 域的函数被调用时，全局解释器锁是否被持有。

pymalloc 分配器

Python has a pymalloc allocator optimized for small objects (smaller or equal to 512 bytes) with a short lifetime. It uses memory mappings called “arenas” with a fixed size of 256 KB. It falls back to PyMem_RawMalloc() and PyMem_RawRealloc() for allocations larger than 512 bytes.

pymalloc is the default allocator of the PYMEM_DOMAIN_MEM (ex: PyMem_Malloc()) and PYMEM_DOMAIN_OBJ (ex: PyObject_Malloc()) domains.

arena 分配器使用以下函数：

Windows 上的 VirtualAlloc() and VirtualFree() ,

mmap() 和 munmap() ，如果可用，

否则， malloc() 和 free() 。

自定义 pymalloc Arena 分配器

3.4 新版功能.

PyObjectArenaAllocator

用来描述一个 arena 分配器的结构体。这个结构体有三个字段：

域

含义

void *ctx

作为第一个参数传入的用户上下文

void* alloc(void *ctx, size_t size)

分配一块 size 字节的区域

void free(void *ctx, size_t size, void *ptr)

释放一块区域

PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator)

获取 arena 分配器

PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator)

设置 arena 分配器

例子

以下是来自 概述 小节的示例，经过重写以使 I/O 缓冲区是通过使用第一个函数集从 Python 堆中分配的:

PyObject *res;
char *buf = (char *) PyMem_Malloc(BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Free(buf); /* allocated with PyMem_Malloc */
return res;

使用面向类型函数集的相同代码:

PyObject *res;
char *buf = PyMem_New(char, BUFSIZ); /* for I/O */

if (buf == NULL)
    return PyErr_NoMemory();
/* ...Do some I/O operation involving buf... */
res = PyBytes_FromString(buf);
PyMem_Del(buf); /* allocated with PyMem_New */
return res;

请注意在以上两个示例中，缓冲区总是通过归属于相同集的函数来操纵的。 事实上，对于一个给定的内存块必须使用相同的内存 API 族，以便使得混合不同分配器的风险减至最低。 以下代码序列包含两处错误，其中一个被标记为 fatal 因为它混合了两种在不同堆上操作的不同分配器。

char *buf1 = PyMem_New(char, BUFSIZ);
char *buf2 = (char *) malloc(BUFSIZ);
char *buf3 = (char *) PyMem_Malloc(BUFSIZ);
...
PyMem_Del(buf3);  /* Wrong -- should be PyMem_Free() */
free(buf2);       /* Right -- allocated via malloc() */
free(buf1);       /* Fatal -- should be PyMem_Del()  */

除了旨在处理来自 Python 堆的原始内存块的函数之外， Python 中的对象是通过 PyObject_New(), PyObject_NewVar() 和 PyObject_Del() 来分配和释放的。

这些将在有关如何在 C 中定义和实现新对象类型的下一章中讲解。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

对象实现支持

本章描述了定义新对象类型时所使用的函数、类型和宏。

在堆上分配对象

Common Object Structures

类型对象

Number Object Structures

Mapping Object Structures

Sequence Object Structures

Buffer Object Structures

Async Object Structures

使对象类型支持循环垃圾回收

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

对象实现支持 »

|

在堆上分配对象

PyObject* _PyObject_New(PyTypeObject *type)

Return value: New reference.

PyVarObject* _PyObject_NewVar(PyTypeObject *type, Py_ssize_t size)

Return value: New reference.

PyObject* PyObject_Init(PyObject *op, PyTypeObject *type)

Return value: Borrowed reference.

用它的类型和初始引用来初始化新分配对象 op。返回已初始化对象。如果 type 表明该对象参与循环垃圾检测器，则将其添加到检测器的观察对象集中。 对象的其他字段不受影响。

PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size)

Return value: Borrowed reference.

它的功能和 PyObject_Init() 一样，并且会初始化变量大小对象的长度信息。

TYPE* PyObject_New(TYPE, PyTypeObject *type)

Return value: New reference.

使用 C 结构类型 TYPE 和 Python 类型对象 type 分配一个新的 Python 对象。 未在该 Python 对象标头中定义的字段不会被初始化；对象的引用计数将为一。 内存分配大小由 type 对象的 tp_basicsize 字段来确定。

TYPE* PyObject_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)

Return value: New reference.

使用C的数据结构类型 TYPE 和Python的类型对象 type 分配一个新的Python对象。Python对象头文件中没有定义的字段不会被初始化。被分配的内存空间预留了 TYPE 结构加 type 对象中 tp_itemsize 字段提供的 size 字段的值。这对于实现类似元组这种能够在构造期决定自己大小的对象是很实用的。将字段的数组嵌入到相同的内存分配中可以减少内存分配的次数，这提高了内存分配的效率。

void PyObject_Del(PyObject *op)

释放由 PyObject_New() 或者 PyObject_NewVar() 分配内存的对象。这通常由对象的type字段定义的 tp_dealloc 处理函数来调用。调用这个函数以后op对象中的字段都不可以被访问，因为原分配的内存空间已不再是一个有效的Python对象。

PyObject _Py_NoneStruct

这个对象是像 None 一样的 Python 对象。它可以使用 Py_None 宏访问，该宏的拿到指向该对象的指针。

参见

PyModule_Create()

分配内存和创建扩展模块

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

对象实现支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

对象实现支持 »

|

Common Object Structures

There are a large number of structures which are used in the definition of object types for Python. This section describes these structures and how they are used.

All Python objects ultimately share a small number of fields at the beginning of the object’s representation in memory. These are represented by the PyObject and PyVarObject types, which are defined, in turn, by the expansions of some macros also used, whether directly or indirectly, in the definition of all other Python objects.

PyObject

All object types are extensions of this type. This is a type which contains the information Python needs to treat a pointer to an object as an object. In a normal “release” build, it contains only the object’s reference count and a pointer to the corresponding type object. Nothing is actually declared to be a PyObject, but every pointer to a Python object can be cast to a PyObject*. Access to the members must be done by using the macros Py_REFCNT and Py_TYPE.

PyVarObject

This is an extension of PyObject that adds the ob_size field. This is only used for objects that have some notion of length. This type does not often appear in the Python/C API. Access to the members must be done by using the macros Py_REFCNT, Py_TYPE, and Py_SIZE.

PyObject_HEAD

This is a macro used when declaring new types which represent objects without a varying length. The PyObject_HEAD macro expands to:

PyObject ob_base;

See documentation of PyObject above.

PyObject_VAR_HEAD

This is a macro used when declaring new types which represent objects with a length that varies from instance to instance. The PyObject_VAR_HEAD macro expands to:

PyVarObject ob_base;

See documentation of PyVarObject above.

Py_TYPE(o)

This macro is used to access the ob_type member of a Python object. It expands to:

(((PyObject*)(o))->ob_type)

Py_REFCNT(o)

This macro is used to access the ob_refcnt member of a Python object. It expands to:

(((PyObject*)(o))->ob_refcnt)

Py_SIZE(o)

This macro is used to access the ob_size member of a Python object. It expands to:

(((PyVarObject*)(o))->ob_size)

PyObject_HEAD_INIT(type)

This is a macro which expands to initialization values for a new PyObject type. This macro expands to:

_PyObject_EXTRA_INIT
1, type,

PyVarObject_HEAD_INIT(type, size)

This is a macro which expands to initialization values for a new PyVarObject type, including the ob_size field. This macro expands to:

_PyObject_EXTRA_INIT
1, type, size,

PyCFunction

Type of the functions used to implement most Python callables in C. Functions of this type take two PyObject* parameters and return one such value. If the return value is NULL, an exception shall have been set. If not NULL, the return value is interpreted as the return value of the function as exposed in Python. The function must return a new reference.

PyCFunctionWithKeywords

Type of the functions used to implement Python callables in C that take keyword arguments: they take three PyObject* parameters and return one such value. See PyCFunction above for the meaning of the return value.

PyMethodDef

Structure used to describe a method of an extension type. This structure has four fields:

域

C 类型

含义

ml_name

char *

name of the method

ml_meth

PyCFunction

pointer to the C implementation

ml_flags

整型

flag bits indicating how the call should be constructed

ml_doc

char *

points to the contents of the docstring

The ml_meth is a C function pointer. The functions may be of different types, but they always return PyObject*. If the function is not of the PyCFunction, the compiler will require a cast in the method table. Even though PyCFunction defines the first parameter as PyObject*, it is common that the method implementation uses the specific C type of the self object.

The ml_flags field is a bitfield which can include the following flags. The individual flags indicate either a calling convention or a binding convention. Of the calling convention flags, only METH_VARARGS and METH_KEYWORDS can be combined. Any of the calling convention flags can be combined with a binding flag.

METH_VARARGS

This is the typical calling convention, where the methods have the type PyCFunction. The function expects two PyObject* values. The first one is the self object for methods; for module functions, it is the module object. The second parameter (often called args) is a tuple object representing all arguments. This parameter is typically processed using PyArg_ParseTuple() or PyArg_UnpackTuple().

METH_KEYWORDS

Methods with these flags must be of type PyCFunctionWithKeywords. The function expects three parameters: self, args, and a dictionary of all the keyword arguments. The flag must be combined with METH_VARARGS, and the parameters are typically processed using PyArg_ParseTupleAndKeywords().

METH_NOARGS

Methods without parameters don’t need to check whether arguments are given if they are listed with the METH_NOARGS flag. They need to be of type PyCFunction. The first parameter is typically named self and will hold a reference to the module or object instance. In all cases the second parameter will be NULL.

METH_O

Methods with a single object argument can be listed with the METH_O flag, instead of invoking PyArg_ParseTuple() with a "O" argument. They have the type PyCFunction, with the self parameter, and a PyObject* parameter representing the single argument.

These two constants are not used to indicate the calling convention but the binding when use with methods of classes. These may not be used for functions defined for modules. At most one of these flags may be set for any given method.

METH_CLASS

The method will be passed the type object as the first parameter rather than an instance of the type. This is used to create class methods, similar to what is created when using the classmethod() built-in function.

METH_STATIC

The method will be passed NULL as the first parameter rather than an instance of the type. This is used to create static methods, similar to what is created when using the staticmethod() built-in function.

One other constant controls whether a method is loaded in place of another definition with the same method name.

METH_COEXIST

The method will be loaded in place of existing definitions. Without METH_COEXIST, the default is to skip repeated definitions. Since slot wrappers are loaded before the method table, the existence of a sq_contains slot, for example, would generate a wrapped method named __contains__() and preclude the loading of a corresponding PyCFunction with the same name. With the flag defined, the PyCFunction will be loaded in place of the wrapper object and will co-exist with the slot. This is helpful because calls to PyCFunctions are optimized more than wrapper object calls.

PyMemberDef

Structure which describes an attribute of a type which corresponds to a C struct member. Its fields are:

域

C 类型

含义

name

char *

name of the member

type

整型

the type of the member in the C struct

offset

Py_ssize_t

the offset in bytes that the member is located on the type’s object struct

flags

整型

flag bits indicating if the field should be read-only or writable

doc

char *

points to the contents of the docstring

type can be one of many T_ macros corresponding to various C types. When the member is accessed in Python, it will be converted to the equivalent Python type.

Macro name

C 类型

T_SHORT

short

T_INT

整型

T_LONG

长整型

T_FLOAT

浮点数

T_DOUBLE

double

T_STRING

char *

T_OBJECT

PyObject *

T_OBJECT_EX

PyObject *

T_CHAR

char

T_BYTE

char

T_UBYTE

unsigned char

T_UINT

无符号整型

T_USHORT

unsigned short

T_ULONG

无符号长整型

T_BOOL

char

T_LONGLONG

long long

T_ULONGLONG

无符号 long long

T_PYSSIZET

Py_ssize_t

T_OBJECT and T_OBJECT_EX differ in that T_OBJECT returns None if the member is NULL and T_OBJECT_EX raises an AttributeError. Try to use T_OBJECT_EX over T_OBJECT because T_OBJECT_EX handles use of the del statement on that attribute more correctly than T_OBJECT.

flags can be 0 for write and read access or READONLY for read-only access. Using T_STRING for type implies READONLY. T_STRING data is interpreted as UTF-8. Only T_OBJECT and T_OBJECT_EX members can be deleted. (They are set to NULL).

PyGetSetDef

Structure to define property-like access for a type. See also description of the PyTypeObject.tp_getset slot.

域

C 类型

含义

名称

char *

attribute name

get

getter

C Function to get the attribute

set

setter

optional C function to set or delete the attribute, if omitted the attribute is readonly

doc

char *

optional docstring

closure

void *

optional function pointer, providing additional data for getter and setter

The get function takes one PyObject* parameter (the instance) and a function pointer (the associated closure):

typedef PyObject *(*getter)(PyObject *, void *);

It should return a new reference on success or NULL with a set exception on failure.

set functions take two PyObject* parameters (the instance and the value to be set) and a function pointer (the associated closure):

typedef int (*setter)(PyObject *, PyObject *, void *);

In case the attribute should be deleted the second parameter is NULL. Should return 0 on success or -1 with a set exception on failure.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

对象实现支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

对象实现支持 »

|

类型对象

Perhaps one of the most important structures of the Python object system is the structure that defines a new type: the PyTypeObject structure. Type objects can be handled using any of the PyObject_*() or PyType_*() functions, but do not offer much that’s interesting to most Python applications. These objects are fundamental to how objects behave, so they are very important to the interpreter itself and to any extension module that implements new types.

Type objects are fairly large compared to most of the standard types. The reason for the size is that each type object stores a large number of values, mostly C function pointers, each of which implements a small part of the type’s functionality. The fields of the type object are examined in detail in this section. The fields will be described in the order in which they occur in the structure.

Typedefs: unaryfunc, binaryfunc, ternaryfunc, inquiry, intargfunc, intintargfunc, intobjargproc, intintobjargproc, objobjargproc, destructor, freefunc, printfunc, getattrfunc, getattrofunc, setattrfunc, setattrofunc, reprfunc, hashfunc

The structure definition for PyTypeObject can be found in Include/object.h. For convenience of reference, this repeats the definition found there:

typedef struct _typeobject {
    PyObject_VAR_HEAD
    const char *tp_name; /* For printing, in format "<module>.<name>" */
    Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */

    /* Methods to implement standard operations */

    destructor tp_dealloc;
    printfunc tp_print;
    getattrfunc tp_getattr;
    setattrfunc tp_setattr;
    PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2)
                                    or tp_reserved (Python 3) */
    reprfunc tp_repr;

    /* Method suites for standard classes */

    PyNumberMethods *tp_as_number;
    PySequenceMethods *tp_as_sequence;
    PyMappingMethods *tp_as_mapping;

    /* More standard operations (here for binary compatibility) */

    hashfunc tp_hash;
    ternaryfunc tp_call;
    reprfunc tp_str;
    getattrofunc tp_getattro;
    setattrofunc tp_setattro;

    /* Functions to access object as input/output buffer */
    PyBufferProcs *tp_as_buffer;

    /* Flags to define presence of optional/expanded features */
    unsigned long tp_flags;

    const char *tp_doc; /* Documentation string */

    /* call function for all accessible objects */
    traverseproc tp_traverse;

    /* delete references to contained objects */
    inquiry tp_clear;

    /* rich comparisons */
    richcmpfunc tp_richcompare;

    /* weak reference enabler */
    Py_ssize_t tp_weaklistoffset;

    /* Iterators */
    getiterfunc tp_iter;
    iternextfunc tp_iternext;

    /* Attribute descriptor and subclassing stuff */
    struct PyMethodDef *tp_methods;
    struct PyMemberDef *tp_members;
    struct PyGetSetDef *tp_getset;
    struct _typeobject *tp_base;
    PyObject *tp_dict;
    descrgetfunc tp_descr_get;
    descrsetfunc tp_descr_set;
    Py_ssize_t tp_dictoffset;
    initproc tp_init;
    allocfunc tp_alloc;
    newfunc tp_new;
    freefunc tp_free; /* Low-level free-memory routine */
    inquiry tp_is_gc; /* For PyObject_IS_GC */
    PyObject *tp_bases;
    PyObject *tp_mro; /* method resolution order */
    PyObject *tp_cache;
    PyObject *tp_subclasses;
    PyObject *tp_weaklist;
    destructor tp_del;

    /* Type attribute cache version tag. Added in version 2.6 */
    unsigned int tp_version_tag;

    destructor tp_finalize;

} PyTypeObject;

The type object structure extends the PyVarObject structure. The ob_size field is used for dynamic types (created by type_new(), usually called from a class statement). Note that PyType_Type (the metatype) initializes tp_itemsize, which means that its instances (i.e. type objects) must have the ob_size field.

PyObject* PyObject._ob_next

PyObject* PyObject._ob_prev

These fields are only present when the macro Py_TRACE_REFS is defined. Their initialization to NULL is taken care of by the PyObject_HEAD_INIT macro. For statically allocated objects, these fields always remain NULL. For dynamically allocated objects, these two fields are used to link the object into a doubly-linked list of all live objects on the heap. This could be used for various debugging purposes; currently the only use is to print the objects that are still alive at the end of a run when the environment variable PYTHONDUMPREFS is set.

These fields are not inherited by subtypes.

Py_ssize_t PyObject.ob_refcnt

This is the type object’s reference count, initialized to 1 by the PyObject_HEAD_INIT macro. Note that for statically allocated type objects, the type’s instances (objects whose ob_type points back to the type) do not count as references. But for dynamically allocated type objects, the instances do count as references.

This field is not inherited by subtypes.

PyTypeObject* PyObject.ob_type

This is the type’s type, in other words its metatype. It is initialized by the argument to the PyObject_HEAD_INIT macro, and its value should normally be &PyType_Type. However, for dynamically loadable extension modules that must be usable on Windows (at least), the compiler complains that this is not a valid initializer. Therefore, the convention is to pass NULL to the PyObject_HEAD_INIT macro and to initialize this field explicitly at the start of the module’s initialization function, before doing anything else. This is typically done like this:

Foo_Type.ob_type = &PyType_Type;

This should be done before any instances of the type are created. PyType_Ready() checks if ob_type is NULL, and if so, initializes it to the ob_type field of the base class. PyType_Ready() will not change this field if it is non-zero.

This field is inherited by subtypes.

Py_ssize_t PyVarObject.ob_size

For statically allocated type objects, this should be initialized to zero. For dynamically allocated type objects, this field has a special internal meaning.

This field is not inherited by subtypes.

const char* PyTypeObject.tp_name

Pointer to a NUL-terminated string containing the name of the type. For types that are accessible as module globals, the string should be the full module name, followed by a dot, followed by the type name; for built-in types, it should be just the type name. If the module is a submodule of a package, the full package name is part of the full module name. For example, a type named T defined in module M in subpackage Q in package P should have the tp_name initializer "P.Q.M.T".

For dynamically allocated type objects, this should just be the type name, and the module name explicitly stored in the type dict as the value for key '__module__'.

For statically allocated type objects, the tp_name field should contain a dot. Everything before the last dot is made accessible as the __module__ attribute, and everything after the last dot is made accessible as the __name__ attribute.

If no dot is present, the entire tp_name field is made accessible as the __name__ attribute, and the __module__ attribute is undefined (unless explicitly set in the dictionary, as explained above). This means your type will be impossible to pickle. Additionally, it will not be listed in module documentations created with pydoc.

This field is not inherited by subtypes.

Py_ssize_t PyTypeObject.tp_basicsize

Py_ssize_t PyTypeObject.tp_itemsize

These fields allow calculating the size in bytes of instances of the type.

There are two kinds of types: types with fixed-length instances have a zero tp_itemsize field, types with variable-length instances have a non-zero tp_itemsize field. For a type with fixed-length instances, all instances have the same size, given in tp_basicsize.

For a type with variable-length instances, the instances must have an ob_size field, and the instance size is tp_basicsize plus N times tp_itemsize, where N is the “length” of the object. The value of N is typically stored in the instance’s ob_size field. There are exceptions: for example, ints use a negative ob_size to indicate a negative number, and N is abs(ob_size) there. Also, the presence of an ob_size field in the instance layout doesn’t mean that the instance structure is variable-length (for example, the structure for the list type has fixed-length instances, yet those instances have a meaningful ob_size field).

The basic size includes the fields in the instance declared by the macro PyObject_HEAD or PyObject_VAR_HEAD (whichever is used to declare the instance struct) and this in turn includes the _ob_prev and _ob_next fields if they are present. This means that the only correct way to get an initializer for the tp_basicsize is to use the sizeof operator on the struct used to declare the instance layout. The basic size does not include the GC header size.

These fields are inherited separately by subtypes. If the base type has a non-zero tp_itemsize, it is generally not safe to set tp_itemsize to a different non-zero value in a subtype (though this depends on the implementation of the base type).

A note about alignment: if the variable items require a particular alignment, this should be taken care of by the value of tp_basicsize. Example: suppose a type implements an array of double. tp_itemsize is sizeof(double). It is the programmer’s responsibility that tp_basicsize is a multiple of sizeof(double) (assuming this is the alignment requirement for double).

destructor PyTypeObject.tp_dealloc

A pointer to the instance destructor function. This function must be defined unless the type guarantees that its instances will never be deallocated (as is the case for the singletons None and Ellipsis).

The destructor function is called by the Py_DECREF() and Py_XDECREF() macros when the new reference count is zero. At this point, the instance is still in existence, but there are no references to it. The destructor function should free all references which the instance owns, free all memory buffers owned by the instance (using the freeing function corresponding to the allocation function used to allocate the buffer), and finally (as its last action) call the type’s tp_free function. If the type is not subtypable (doesn’t have the Py_TPFLAGS_BASETYPE flag bit set), it is permissible to call the object deallocator directly instead of via tp_free. The object deallocator should be the one used to allocate the instance; this is normally PyObject_Del() if the instance was allocated using PyObject_New() or PyObject_VarNew(), or PyObject_GC_Del() if the instance was allocated using PyObject_GC_New() or PyObject_GC_NewVar().

This field is inherited by subtypes.

printfunc PyTypeObject.tp_print

Reserved slot, formerly used for print formatting in Python 2.x.

getattrfunc PyTypeObject.tp_getattr

An optional pointer to the get-attribute-string function.

This field is deprecated. When it is defined, it should point to a function that acts the same as the tp_getattro function, but taking a C string instead of a Python string object to give the attribute name. The signature is

PyObject * tp_getattr(PyObject *o, char *attr_name);

This field is inherited by subtypes together with tp_getattro: a subtype inherits both tp_getattr and tp_getattro from its base type when the subtype’s tp_getattr and tp_getattro are both NULL.

setattrfunc PyTypeObject.tp_setattr

An optional pointer to the function for setting and deleting attributes.

This field is deprecated. When it is defined, it should point to a function that acts the same as the tp_setattro function, but taking a C string instead of a Python string object to give the attribute name. The signature is

PyObject * tp_setattr(PyObject *o, char *attr_name, PyObject *v);

The v argument is set to NULL to delete the attribute. This field is inherited by subtypes together with tp_setattro: a subtype inherits both tp_setattr and tp_setattro from its base type when the subtype’s tp_setattr and tp_setattro are both NULL.

PyAsyncMethods* tp_as_async

Pointer to an additional structure that contains fields relevant only to objects which implement awaitable and asynchronous iterator protocols at the C-level. See Async Object Structures for details.

3.5 新版功能: Formerly known as tp_compare and tp_reserved.

reprfunc PyTypeObject.tp_repr

An optional pointer to a function that implements the built-in function repr().

The signature is the same as for PyObject_Repr(); it must return a string or a Unicode object. Ideally, this function should return a string that, when passed to eval(), given a suitable environment, returns an object with the same value. If this is not feasible, it should return a string starting with '<' and ending with '>' from which both the type and the value of the object can be deduced.

When this field is not set, a string of the form <%s object at %p> is returned, where %s is replaced by the type name, and %p by the object’s memory address.

This field is inherited by subtypes.

PyNumberMethods* tp_as_number

Pointer to an additional structure that contains fields relevant only to objects which implement the number protocol. These fields are documented in Number Object Structures.

The tp_as_number field is not inherited, but the contained fields are inherited individually.

PySequenceMethods* tp_as_sequence

Pointer to an additional structure that contains fields relevant only to objects which implement the sequence protocol. These fields are documented in Sequence Object Structures.

The tp_as_sequence field is not inherited, but the contained fields are inherited individually.

PyMappingMethods* tp_as_mapping

Pointer to an additional structure that contains fields relevant only to objects which implement the mapping protocol. These fields are documented in Mapping Object Structures.

The tp_as_mapping field is not inherited, but the contained fields are inherited individually.

hashfunc PyTypeObject.tp_hash

An optional pointer to a function that implements the built-in function hash().

The signature is the same as for PyObject_Hash(); it must return a value of the type Py_hash_t. The value -1 should not be returned as a normal return value; when an error occurs during the computation of the hash value, the function should set an exception and return -1.

This field can be set explicitly to PyObject_HashNotImplemented() to block inheritance of the hash method from a parent type. This is interpreted as the equivalent of __hash__ = None at the Python level, causing isinstance(o, collections.Hashable) to correctly return False. Note that the converse is also true - setting __hash__ = None on a class at the Python level will result in the tp_hash slot being set to PyObject_HashNotImplemented().

When this field is not set, an attempt to take the hash of the object raises TypeError.

This field is inherited by subtypes together with tp_richcompare: a subtype inherits both of tp_richcompare and tp_hash, when the subtype’s tp_richcompare and tp_hash are both NULL.

ternaryfunc PyTypeObject.tp_call

An optional pointer to a function that implements calling the object. This should be NULL if the object is not callable. The signature is the same as for PyObject_Call().

This field is inherited by subtypes.

reprfunc PyTypeObject.tp_str

An optional pointer to a function that implements the built-in operation str(). (Note that str is a type now, and str() calls the constructor for that type. This constructor calls PyObject_Str() to do the actual work, and PyObject_Str() will call this handler.)

The signature is the same as for PyObject_Str(); it must return a string or a Unicode object. This function should return a “friendly” string representation of the object, as this is the representation that will be used, among other things, by the print() function.

When this field is not set, PyObject_Repr() is called to return a string representation.

This field is inherited by subtypes.

getattrofunc PyTypeObject.tp_getattro

An optional pointer to the get-attribute function.

The signature is the same as for PyObject_GetAttr(). It is usually convenient to set this field to PyObject_GenericGetAttr(), which implements the normal way of looking for object attributes.

This field is inherited by subtypes together with tp_getattr: a subtype inherits both tp_getattr and tp_getattro from its base type when the subtype’s tp_getattr and tp_getattro are both NULL.

setattrofunc PyTypeObject.tp_setattro

An optional pointer to the function for setting and deleting attributes.

The signature is the same as for PyObject_SetAttr(), but setting v to NULL to delete an attribute must be supported. It is usually convenient to set this field to PyObject_GenericSetAttr(), which implements the normal way of setting object attributes.

This field is inherited by subtypes together with tp_setattr: a subtype inherits both tp_setattr and tp_setattro from its base type when the subtype’s tp_setattr and tp_setattro are both NULL.

PyBufferProcs* PyTypeObject.tp_as_buffer

Pointer to an additional structure that contains fields relevant only to objects which implement the buffer interface. These fields are documented in Buffer Object Structures.

The tp_as_buffer field is not inherited, but the contained fields are inherited individually.

unsigned long PyTypeObject.tp_flags

This field is a bit mask of various flags. Some flags indicate variant semantics for certain situations; others are used to indicate that certain fields in the type object (or in the extension structures referenced via tp_as_number, tp_as_sequence, tp_as_mapping, and tp_as_buffer) that were historically not always present are valid; if such a flag bit is clear, the type fields it guards must not be accessed and must be considered to have a zero or NULL value instead.

Inheritance of this field is complicated. Most flag bits are inherited individually, i.e. if the base type has a flag bit set, the subtype inherits this flag bit. The flag bits that pertain to extension structures are strictly inherited if the extension structure is inherited, i.e. the base type’s value of the flag bit is copied into the subtype together with a pointer to the extension structure. The Py_TPFLAGS_HAVE_GC flag bit is inherited together with the tp_traverse and tp_clear fields, i.e. if the Py_TPFLAGS_HAVE_GC flag bit is clear in the subtype and the tp_traverse and tp_clear fields in the subtype exist and have NULL values.

The following bit masks are currently defined; these can be ORed together using the | operator to form the value of the tp_flags field. The macro PyType_HasFeature() takes a type and a flags value, tp and f, and checks whether tp->tp_flags & f is non-zero.

Py_TPFLAGS_HEAPTYPE

This bit is set when the type object itself is allocated on the heap. In this case, the ob_type field of its instances is considered a reference to the type, and the type object is INCREF’ed when a new instance is created, and DECREF’ed when an instance is destroyed (this does not apply to instances of subtypes; only the type referenced by the instance’s ob_type gets INCREF’ed or DECREF’ed).

Py_TPFLAGS_BASETYPE

This bit is set when the type can be used as the base type of another type. If this bit is clear, the type cannot be subtyped (similar to a “final” class in Java).

Py_TPFLAGS_READY

This bit is set when the type object has been fully initialized by PyType_Ready().

Py_TPFLAGS_READYING

This bit is set while PyType_Ready() is in the process of initializing the type object.

Py_TPFLAGS_HAVE_GC

This bit is set when the object supports garbage collection. If this bit is set, instances must be created using PyObject_GC_New() and destroyed using PyObject_GC_Del(). More information in section 使对象类型支持循环垃圾回收. This bit also implies that the GC-related fields tp_traverse and tp_clear are present in the type object.

Py_TPFLAGS_DEFAULT

This is a bitmask of all the bits that pertain to the existence of certain fields in the type object and its extension structures. Currently, it includes the following bits: Py_TPFLAGS_HAVE_STACKLESS_EXTENSION, Py_TPFLAGS_HAVE_VERSION_TAG.

Py_TPFLAGS_LONG_SUBCLASS

Py_TPFLAGS_LIST_SUBCLASS

Py_TPFLAGS_TUPLE_SUBCLASS

Py_TPFLAGS_BYTES_SUBCLASS

Py_TPFLAGS_UNICODE_SUBCLASS

Py_TPFLAGS_DICT_SUBCLASS

Py_TPFLAGS_BASE_EXC_SUBCLASS

Py_TPFLAGS_TYPE_SUBCLASS

These flags are used by functions such as PyLong_Check() to quickly determine if a type is a subclass of a built-in type; such specific checks are faster than a generic check, like PyObject_IsInstance(). Custom types that inherit from built-ins should have their tp_flags set appropriately, or the code that interacts with such types will behave differently depending on what kind of check is used.

Py_TPFLAGS_HAVE_FINALIZE

This bit is set when the tp_finalize slot is present in the type structure.

3.4 新版功能.

const char* PyTypeObject.tp_doc

An optional pointer to a NUL-terminated C string giving the docstring for this type object. This is exposed as the __doc__ attribute on the type and instances of the type.

This field is not inherited by subtypes.

traverseproc PyTypeObject.tp_traverse

An optional pointer to a traversal function for the garbage collector. This is only used if the Py_TPFLAGS_HAVE_GC flag bit is set. More information about Python’s garbage collection scheme can be found in section 使对象类型支持循环垃圾回收.

The tp_traverse pointer is used by the garbage collector to detect reference cycles. A typical implementation of a tp_traverse function simply calls Py_VISIT() on each of the instance’s members that are Python objects. For example, this is function local_traverse() from the _thread extension module:

static int
local_traverse(localobject *self, visitproc visit, void *arg)
{
    Py_VISIT(self->args);
    Py_VISIT(self->kw);
    Py_VISIT(self->dict);
    return 0;
}

Note that Py_VISIT() is called only on those members that can participate in reference cycles. Although there is also a self->key member, it can only be NULL or a Python string and therefore cannot be part of a reference cycle.

On the other hand, even if you know a member can never be part of a cycle, as a debugging aid you may want to visit it anyway just so the gc module’s get_referents() function will include it.

Note that Py_VISIT() requires the visit and arg parameters to local_traverse() to have these specific names; don’t name them just anything.

This field is inherited by subtypes together with tp_clear and the Py_TPFLAGS_HAVE_GC flag bit: the flag bit, tp_traverse, and tp_clear are all inherited from the base type if they are all zero in the subtype.

inquiry PyTypeObject.tp_clear

An optional pointer to a clear function for the garbage collector. This is only used if the Py_TPFLAGS_HAVE_GC flag bit is set.

The tp_clear member function is used to break reference cycles in cyclic garbage detected by the garbage collector. Taken together, all tp_clear functions in the system must combine to break all reference cycles. This is subtle, and if in any doubt supply a tp_clear function. For example, the tuple type does not implement a tp_clear function, because it’s possible to prove that no reference cycle can be composed entirely of tuples. Therefore the tp_clear functions of other types must be sufficient to break any cycle containing a tuple. This isn’t immediately obvious, and there’s rarely a good reason to avoid implementing tp_clear.

Implementations of tp_clear should drop the instance’s references to those of its members that may be Python objects, and set its pointers to those members to NULL, as in the following example:

static int
local_clear(localobject *self)
{
    Py_CLEAR(self->key);
    Py_CLEAR(self->args);
    Py_CLEAR(self->kw);
    Py_CLEAR(self->dict);
    return 0;
}

The Py_CLEAR() macro should be used, because clearing references is delicate: the reference to the contained object must not be decremented until after the pointer to the contained object is set to NULL. This is because decrementing the reference count may cause the contained object to become trash, triggering a chain of reclamation activity that may include invoking arbitrary Python code (due to finalizers, or weakref callbacks, associated with the contained object). If it’s possible for such code to reference self again, it’s important that the pointer to the contained object be NULL at that time, so that self knows the contained object can no longer be used. The Py_CLEAR() macro performs the operations in a safe order.

Because the goal of tp_clear functions is to break reference cycles, it’s not necessary to clear contained objects like Python strings or Python integers, which can’t participate in reference cycles. On the other hand, it may be convenient to clear all contained Python objects, and write the type’s tp_dealloc function to invoke tp_clear.

More information about Python’s garbage collection scheme can be found in section 使对象类型支持循环垃圾回收.

This field is inherited by subtypes together with tp_traverse and the Py_TPFLAGS_HAVE_GC flag bit: the flag bit, tp_traverse, and tp_clear are all inherited from the base type if they are all zero in the subtype.

richcmpfunc PyTypeObject.tp_richcompare

An optional pointer to the rich comparison function, whose signature is PyObject *tp_richcompare(PyObject *a, PyObject *b, int op). The first parameter is guaranteed to be an instance of the type that is defined by PyTypeObject.

The function should return the result of the comparison (usually Py_True or Py_False). If the comparison is undefined, it must return Py_NotImplemented, if another error occurred it must return NULL and set an exception condition.

注解

If you want to implement a type for which only a limited set of comparisons makes sense (e.g. == and !=, but not < and friends), directly raise TypeError in the rich comparison function.

This field is inherited by subtypes together with tp_hash: a subtype inherits tp_richcompare and tp_hash when the subtype’s tp_richcompare and tp_hash are both NULL.

The following constants are defined to be used as the third argument for tp_richcompare and for PyObject_RichCompare():

常数

对照

Py_LT

<

Py_LE

<=

Py_EQ

==

Py_NE

!=

Py_GT

>

Py_GE

>=

Py_ssize_t PyTypeObject.tp_weaklistoffset

If the instances of this type are weakly referenceable, this field is greater than zero and contains the offset in the instance structure of the weak reference list head (ignoring the GC header, if present); this offset is used by PyObject_ClearWeakRefs() and the PyWeakref_*() functions. The instance structure needs to include a field of type PyObject* which is initialized to NULL.

Do not confuse this field with tp_weaklist; that is the list head for weak references to the type object itself.

This field is inherited by subtypes, but see the rules listed below. A subtype may override this offset; this means that the subtype uses a different weak reference list head than the base type. Since the list head is always found via tp_weaklistoffset, this should not be a problem.

When a type defined by a class statement has no __slots__ declaration, and none of its base types are weakly referenceable, the type is made weakly referenceable by adding a weak reference list head slot to the instance layout and setting the tp_weaklistoffset of that slot’s offset.

When a type’s __slots__ declaration contains a slot named __weakref__, that slot becomes the weak reference list head for instances of the type, and the slot’s offset is stored in the type’s tp_weaklistoffset.

When a type’s __slots__ declaration does not contain a slot named __weakref__, the type inherits its tp_weaklistoffset from its base type.

getiterfunc PyTypeObject.tp_iter

An optional pointer to a function that returns an iterator for the object. Its presence normally signals that the instances of this type are iterable (although sequences may be iterable without this function).

This function has the same signature as PyObject_GetIter().

This field is inherited by subtypes.

iternextfunc PyTypeObject.tp_iternext

An optional pointer to a function that returns the next item in an iterator. When the iterator is exhausted, it must return NULL; a StopIteration exception may or may not be set. When another error occurs, it must return NULL too. Its presence signals that the instances of this type are iterators.

Iterator types should also define the tp_iter function, and that function should return the iterator instance itself (not a new iterator instance).

This function has the same signature as PyIter_Next().

This field is inherited by subtypes.

struct PyMethodDef* PyTypeObject.tp_methods

An optional pointer to a static NULL-terminated array of PyMethodDef structures, declaring regular methods of this type.

For each entry in the array, an entry is added to the type’s dictionary (see tp_dict below) containing a method descriptor.

This field is not inherited by subtypes (methods are inherited through a different mechanism).

struct PyMemberDef* PyTypeObject.tp_members

An optional pointer to a static NULL-terminated array of PyMemberDef structures, declaring regular data members (fields or slots) of instances of this type.

For each entry in the array, an entry is added to the type’s dictionary (see tp_dict below) containing a member descriptor.

This field is not inherited by subtypes (members are inherited through a different mechanism).

struct PyGetSetDef* PyTypeObject.tp_getset

An optional pointer to a static NULL-terminated array of PyGetSetDef structures, declaring computed attributes of instances of this type.

For each entry in the array, an entry is added to the type’s dictionary (see tp_dict below) containing a getset descriptor.

This field is not inherited by subtypes (computed attributes are inherited through a different mechanism).

PyTypeObject* PyTypeObject.tp_base

An optional pointer to a base type from which type properties are inherited. At this level, only single inheritance is supported; multiple inheritance require dynamically creating a type object by calling the metatype.

This field is not inherited by subtypes (obviously), but it defaults to &PyBaseObject_Type (which to Python programmers is known as the type object).

PyObject* PyTypeObject.tp_dict

The type’s dictionary is stored here by PyType_Ready().

This field should normally be initialized to NULL before PyType_Ready is called; it may also be initialized to a dictionary containing initial attributes for the type. Once PyType_Ready() has initialized the type, extra attributes for the type may be added to this dictionary only if they don’t correspond to overloaded operations (like __add__()).

This field is not inherited by subtypes (though the attributes defined in here are inherited through a different mechanism).

警告

It is not safe to use PyDict_SetItem() on or otherwise modify tp_dict with the dictionary C-API.

descrgetfunc PyTypeObject.tp_descr_get

An optional pointer to a “descriptor get” function.

The function signature is

PyObject * tp_descr_get(PyObject *self, PyObject *obj, PyObject *type);

This field is inherited by subtypes.

descrsetfunc PyTypeObject.tp_descr_set

An optional pointer to a function for setting and deleting a descriptor’s value.

The function signature is

int tp_descr_set(PyObject *self, PyObject *obj, PyObject *value);

The value argument is set to NULL to delete the value. This field is inherited by subtypes.

Py_ssize_t PyTypeObject.tp_dictoffset

If the instances of this type have a dictionary containing instance variables, this field is non-zero and contains the offset in the instances of the type of the instance variable dictionary; this offset is used by PyObject_GenericGetAttr().

Do not confuse this field with tp_dict; that is the dictionary for attributes of the type object itself.

If the value of this field is greater than zero, it specifies the offset from the start of the instance structure. If the value is less than zero, it specifies the offset from the end of the instance structure. A negative offset is more expensive to use, and should only be used when the instance structure contains a variable-length part. This is used for example to add an instance variable dictionary to subtypes of str or tuple. Note that the tp_basicsize field should account for the dictionary added to the end in that case, even though the dictionary is not included in the basic object layout. On a system with a pointer size of 4 bytes, tp_dictoffset should be set to -4 to indicate that the dictionary is at the very end of the structure.

The real dictionary offset in an instance can be computed from a negative tp_dictoffset as follows:

dictoffset = tp_basicsize + abs(ob_size)*tp_itemsize + tp_dictoffset
if dictoffset is not aligned on sizeof(void*):
    round up to sizeof(void*)

where tp_basicsize, tp_itemsize and tp_dictoffset are taken from the type object, and ob_size is taken from the instance. The absolute value is taken because ints use the sign of ob_size to store the sign of the number. (There’s never a need to do this calculation yourself; it is done for you by _PyObject_GetDictPtr().)

This field is inherited by subtypes, but see the rules listed below. A subtype may override this offset; this means that the subtype instances store the dictionary at a difference offset than the base type. Since the dictionary is always found via tp_dictoffset, this should not be a problem.

When a type defined by a class statement has no __slots__ declaration, and none of its base types has an instance variable dictionary, a dictionary slot is added to the instance layout and the tp_dictoffset is set to that slot’s offset.

When a type defined by a class statement has a __slots__ declaration, the type inherits its tp_dictoffset from its base type.

(Adding a slot named __dict__ to the __slots__ declaration does not have the expected effect, it just causes confusion. Maybe this should be added as a feature just like __weakref__ though.)

initproc PyTypeObject.tp_init

An optional pointer to an instance initialization function.

This function corresponds to the __init__() method of classes. Like __init__(), it is possible to create an instance without calling __init__(), and it is possible to reinitialize an instance by calling its __init__() method again.

The function signature is

int tp_init(PyObject *self, PyObject *args, PyObject *kwds)

The self argument is the instance to be initialized; the args and kwds arguments represent positional and keyword arguments of the call to __init__().

The tp_init function, if not NULL, is called when an instance is created normally by calling its type, after the type’s tp_new function has returned an instance of the type. If the tp_new function returns an instance of some other type that is not a subtype of the original type, no tp_init function is called; if tp_new returns an instance of a subtype of the original type, the subtype’s tp_init is called.

This field is inherited by subtypes.

allocfunc PyTypeObject.tp_alloc

An optional pointer to an instance allocation function.

The function signature is

PyObject *tp_alloc(PyTypeObject *self, Py_ssize_t nitems)

The purpose of this function is to separate memory allocation from memory initialization. It should return a pointer to a block of memory of adequate length for the instance, suitably aligned, and initialized to zeros, but with ob_refcnt set to 1 and ob_type set to the type argument. If the type’s tp_itemsize is non-zero, the object’s ob_size field should be initialized to nitems and the length of the allocated memory block should be tp_basicsize + nitems*tp_itemsize, rounded up to a multiple of sizeof(void*); otherwise, nitems is not used and the length of the block should be tp_basicsize.

Do not use this function to do any other instance initialization, not even to allocate additional memory; that should be done by tp_new.

This field is inherited by static subtypes, but not by dynamic subtypes (subtypes created by a class statement); in the latter, this field is always set to PyType_GenericAlloc(), to force a standard heap allocation strategy. That is also the recommended value for statically defined types.

newfunc PyTypeObject.tp_new

An optional pointer to an instance creation function.

If this function is NULL for a particular type, that type cannot be called to create new instances; presumably there is some other way to create instances, like a factory function.

The function signature is

PyObject *tp_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds)

The subtype argument is the type of the object being created; the args and kwds arguments represent positional and keyword arguments of the call to the type. Note that subtype doesn’t have to equal the type whose tp_new function is called; it may be a subtype of that type (but not an unrelated type).

The tp_new function should call subtype->tp_alloc(subtype, nitems) to allocate space for the object, and then do only as much further initialization as is absolutely necessary. Initialization that can safely be ignored or repeated should be placed in the tp_init handler. A good rule of thumb is that for immutable types, all initialization should take place in tp_new, while for mutable types, most initialization should be deferred to tp_init.

This field is inherited by subtypes, except it is not inherited by static types whose tp_base is NULL or &PyBaseObject_Type.

destructor PyTypeObject.tp_free

An optional pointer to an instance deallocation function. Its signature is freefunc:

void tp_free(void *)

An initializer that is compatible with this signature is PyObject_Free().

This field is inherited by static subtypes, but not by dynamic subtypes (subtypes created by a class statement); in the latter, this field is set to a deallocator suitable to match PyType_GenericAlloc() and the value of the Py_TPFLAGS_HAVE_GC flag bit.

inquiry PyTypeObject.tp_is_gc

An optional pointer to a function called by the garbage collector.

The garbage collector needs to know whether a particular object is collectible or not. Normally, it is sufficient to look at the object’s type’s tp_flags field, and check the Py_TPFLAGS_HAVE_GC flag bit. But some types have a mixture of statically and dynamically allocated instances, and the statically allocated instances are not collectible. Such types should define this function; it should return 1 for a collectible instance, and 0 for a non-collectible instance. The signature is

int tp_is_gc(PyObject *self)

(The only example of this are types themselves. The metatype, PyType_Type, defines this function to distinguish between statically and dynamically allocated types.)

This field is inherited by subtypes.

PyObject* PyTypeObject.tp_bases

Tuple of base types.

This is set for types created by a class statement. It should be NULL for statically defined types.

This field is not inherited.

PyObject* PyTypeObject.tp_mro

Tuple containing the expanded set of base types, starting with the type itself and ending with object, in Method Resolution Order.

This field is not inherited; it is calculated fresh by PyType_Ready().

destructor PyTypeObject.tp_finalize

An optional pointer to an instance finalization function. Its signature is destructor:

void tp_finalize(PyObject *)

If tp_finalize is set, the interpreter calls it once when finalizing an instance. It is called either from the garbage collector (if the instance is part of an isolated reference cycle) or just before the object is deallocated. Either way, it is guaranteed to be called before attempting to break reference cycles, ensuring that it finds the object in a sane state.

tp_finalize should not mutate the current exception status; therefore, a recommended way to write a non-trivial finalizer is:

static void
local_finalize(PyObject *self)
{
    PyObject *error_type, *error_value, *error_traceback;

    /* Save the current exception, if any. */
    PyErr_Fetch(&error_type, &error_value, &error_traceback);

    /* ... */

    /* Restore the saved exception. */
    PyErr_Restore(error_type, error_value, error_traceback);
}

For this field to be taken into account (even through inheritance), you must also set the Py_TPFLAGS_HAVE_FINALIZE flags bit.

This field is inherited by subtypes.

3.4 新版功能.

参见

“Safe object finalization” (PEP 442 [https://www.python.org/dev/peps/pep-0442])

PyObject* PyTypeObject.tp_cache

Unused. Not inherited. Internal use only.

PyObject* PyTypeObject.tp_subclasses

List of weak references to subclasses. Not inherited. Internal use only.

PyObject* PyTypeObject.tp_weaklist

Weak reference list head, for weak references to this type object. Not inherited. Internal use only.

The remaining fields are only defined if the feature test macro COUNT_ALLOCS is defined, and are for internal use only. They are documented here for completeness. None of these fields are inherited by subtypes.

Py_ssize_t PyTypeObject.tp_allocs

Number of allocations.

Py_ssize_t PyTypeObject.tp_frees

Number of frees.

Py_ssize_t PyTypeObject.tp_maxalloc

Maximum simultaneously allocated objects.

PyTypeObject* PyTypeObject.tp_next

Pointer to the next type object with a non-zero tp_allocs field.

Also, note that, in a garbage collected Python, tp_dealloc may be called from any Python thread, not just the thread which created the object (if the object becomes part of a refcount cycle, that cycle might be collected by a garbage collection on any thread). This is not a problem for Python API calls, since the thread on which tp_dealloc is called will own the Global Interpreter Lock (GIL). However, if the object being destroyed in turn destroys objects from some other C or C++ library, care should be taken to ensure that destroying those objects on the thread which called tp_dealloc will not violate any assumptions of the library.

Number Object Structures

PyNumberMethods

This structure holds pointers to the functions which an object uses to implement the number protocol. Each function is used by the function of similar name documented in the 数字协议 section.

Here is the structure definition:

typedef struct {
     binaryfunc nb_add;
     binaryfunc nb_subtract;
     binaryfunc nb_multiply;
     binaryfunc nb_remainder;
     binaryfunc nb_divmod;
     ternaryfunc nb_power;
     unaryfunc nb_negative;
     unaryfunc nb_positive;
     unaryfunc nb_absolute;
     inquiry nb_bool;
     unaryfunc nb_invert;
     binaryfunc nb_lshift;
     binaryfunc nb_rshift;
     binaryfunc nb_and;
     binaryfunc nb_xor;
     binaryfunc nb_or;
     unaryfunc nb_int;
     void *nb_reserved;
     unaryfunc nb_float;

     binaryfunc nb_inplace_add;
     binaryfunc nb_inplace_subtract;
     binaryfunc nb_inplace_multiply;
     binaryfunc nb_inplace_remainder;
     ternaryfunc nb_inplace_power;
     binaryfunc nb_inplace_lshift;
     binaryfunc nb_inplace_rshift;
     binaryfunc nb_inplace_and;
     binaryfunc nb_inplace_xor;
     binaryfunc nb_inplace_or;

     binaryfunc nb_floor_divide;
     binaryfunc nb_true_divide;
     binaryfunc nb_inplace_floor_divide;
     binaryfunc nb_inplace_true_divide;

     unaryfunc nb_index;

     binaryfunc nb_matrix_multiply;
     binaryfunc nb_inplace_matrix_multiply;
} PyNumberMethods;

注解

Binary and ternary functions must check the type of all their operands, and implement the necessary conversions (at least one of the operands is an instance of the defined type). If the operation is not defined for the given operands, binary and ternary functions must return Py_NotImplemented, if another error occurred they must return NULL and set an exception.

注解

The nb_reserved field should always be NULL. It was previously called nb_long, and was renamed in Python 3.0.1.

Mapping Object Structures

PyMappingMethods

This structure holds pointers to the functions which an object uses to implement the mapping protocol. It has three members:

lenfunc PyMappingMethods.mp_length

This function is used by PyMapping_Size() and PyObject_Size(), and has the same signature. This slot may be set to NULL if the object has no defined length.

binaryfunc PyMappingMethods.mp_subscript

This function is used by PyObject_GetItem() and PySequence_GetSlice(), and has the same signature as PyObject_GetItem(). This slot must be filled for the PyMapping_Check() function to return 1, it can be NULL otherwise.

objobjargproc PyMappingMethods.mp_ass_subscript

This function is used by PyObject_SetItem(), PyObject_DelItem(), PyObject_SetSlice() and PyObject_DelSlice(). It has the same signature as PyObject_SetItem(), but v can also be set to NULL to delete an item. If this slot is NULL, the object does not support item assignment and deletion.

Sequence Object Structures

PySequenceMethods

This structure holds pointers to the functions which an object uses to implement the sequence protocol.

lenfunc PySequenceMethods.sq_length

This function is used by PySequence_Size() and PyObject_Size(), and has the same signature. It is also used for handling negative indices via the sq_item and the sq_ass_item slots.

binaryfunc PySequenceMethods.sq_concat

This function is used by PySequence_Concat() and has the same signature. It is also used by the + operator, after trying the numeric addition via the nb_add slot.

ssizeargfunc PySequenceMethods.sq_repeat

This function is used by PySequence_Repeat() and has the same signature. It is also used by the * operator, after trying numeric multiplication via the nb_multiply slot.

ssizeargfunc PySequenceMethods.sq_item

This function is used by PySequence_GetItem() and has the same signature. It is also used by PyObject_GetItem(), after trying the subscription via the mp_subscript slot. This slot must be filled for the PySequence_Check() function to return 1, it can be NULL otherwise.

Negative indexes are handled as follows: if the sq_length slot is filled, it is called and the sequence length is used to compute a positive index which is passed to sq_item. If sq_length is NULL, the index is passed as is to the function.

ssizeobjargproc PySequenceMethods.sq_ass_item

This function is used by PySequence_SetItem() and has the same signature. It is also used by PyObject_SetItem() and PyObject_DelItem(), after trying the item assignment and deletion via the mp_ass_subscript slot. This slot may be left to NULL if the object does not support item assignment and deletion.

objobjproc PySequenceMethods.sq_contains

This function may be used by PySequence_Contains() and has the same signature. This slot may be left to NULL, in this case PySequence_Contains() simply traverses the sequence until it finds a match.

binaryfunc PySequenceMethods.sq_inplace_concat

This function is used by PySequence_InPlaceConcat() and has the same signature. It should modify its first operand, and return it. This slot may be left to NULL, in this case PySequence_InPlaceConcat() will fall back to PySequence_Concat(). It is also used by the augmented assignment +=, after trying numeric inplace addition via the nb_inplace_add slot.

ssizeargfunc PySequenceMethods.sq_inplace_repeat

This function is used by PySequence_InPlaceRepeat() and has the same signature. It should modify its first operand, and return it. This slot may be left to NULL, in this case PySequence_InPlaceRepeat() will fall back to PySequence_Repeat(). It is also used by the augmented assignment *=, after trying numeric inplace multiplication via the nb_inplace_multiply slot.

Buffer Object Structures

PyBufferProcs

This structure holds pointers to the functions required by the Buffer protocol. The protocol defines how an exporter object can expose its internal data to consumer objects.

getbufferproc PyBufferProcs.bf_getbuffer

The signature of this function is:

int (PyObject *exporter, Py_buffer *view, int flags);

Handle a request to exporter to fill in view as specified by flags. Except for point (3), an implementation of this function MUST take these steps:

Check if the request can be met. If not, raise PyExc_BufferError, set view->obj to NULL and return -1.

Fill in the requested fields.

Increment an internal counter for the number of exports.

Set view->obj to exporter and increment view->obj.

Return 0.

If exporter is part of a chain or tree of buffer providers, two main schemes can be used:

Re-export: Each member of the tree acts as the exporting object and sets view->obj to a new reference to itself.

Redirect: The buffer request is redirected to the root object of the tree. Here, view->obj will be a new reference to the root object.

The individual fields of view are described in section Buffer structure, the rules how an exporter must react to specific requests are in section Buffer request types.

All memory pointed to in the Py_buffer structure belongs to the exporter and must remain valid until there are no consumers left. format, shape, strides, suboffsets and internal are read-only for the consumer.

PyBuffer_FillInfo() provides an easy way of exposing a simple bytes buffer while dealing correctly with all request types.

PyObject_GetBuffer() is the interface for the consumer that wraps this function.

releasebufferproc PyBufferProcs.bf_releasebuffer

The signature of this function is:

void (PyObject *exporter, Py_buffer *view);

Handle a request to release the resources of the buffer. If no resources need to be released, PyBufferProcs.bf_releasebuffer may be NULL. Otherwise, a standard implementation of this function will take these optional steps:

Decrement an internal counter for the number of exports.

If the counter is 0, free all memory associated with view.

The exporter MUST use the internal field to keep track of buffer-specific resources. This field is guaranteed to remain constant, while a consumer MAY pass a copy of the original buffer as the view argument.

This function MUST NOT decrement view->obj, since that is done automatically in PyBuffer_Release() (this scheme is useful for breaking reference cycles).

PyBuffer_Release() is the interface for the consumer that wraps this function.

Async Object Structures

3.5 新版功能.

PyAsyncMethods

This structure holds pointers to the functions required to implement awaitable and asynchronous iterator objects.

Here is the structure definition:

typedef struct {
    unaryfunc am_await;
    unaryfunc am_aiter;
    unaryfunc am_anext;
} PyAsyncMethods;

unaryfunc PyAsyncMethods.am_await

The signature of this function is:

PyObject *am_await(PyObject *self)

The returned object must be an iterator, i.e. PyIter_Check() must return 1 for it.

This slot may be set to NULL if an object is not an awaitable.

unaryfunc PyAsyncMethods.am_aiter

The signature of this function is:

PyObject *am_aiter(PyObject *self)

Must return an awaitable object. See __anext__() for details.

This slot may be set to NULL if an object does not implement asynchronous iteration protocol.

unaryfunc PyAsyncMethods.am_anext

The signature of this function is:

PyObject *am_anext(PyObject *self)

Must return an awaitable object. See __anext__() for details. This slot may be set to NULL.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

对象实现支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

对象实现支持 »

|

使对象类型支持循环垃圾回收

Python 对循环引用的垃圾检测与回收需要“容器”对象类型的支持，此类型的容器对象中可能包含其它容器对象。不保存其它对象的引用的类型，或者只保存原子类型（如数字或字符串）的引用的类型，不需要显式提供垃圾回收的支持。

若要创建一个容器类，类型对象的 tp_flags 字段必须包含 Py_TPFLAGS_HAVE_GC 并提供一个 tp_traverse 处理的实现。如果该类型的实例是可变的，还需要实现 tp_clear 。

Py_TPFLAGS_HAVE_GC

设置了此标志位的类型的对象必须符合此处记录的规则。为方便起见，下文把这些对象称为容器对象。

容器类型的构造函数必须符合两个规则：

必须使用 PyObject_GC_New() 或 PyObject_GC_NewVar() 为这些对象分配内存。

初始化了所有可能包含其他容器的引用的字段后，它必须调用 PyObject_GC_Track() 。

TYPE* PyObject_GC_New(TYPE, PyTypeObject *type)

类似于 PyObject_New() ，适用于设置了 Py_TPFLAGS_HAVE_GC 标签的容器对象。

TYPE* PyObject_GC_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)

类似于 PyObject_NewVar() ，适用于设置了 Py_TPFLAGS_HAVE_GC 标签的容器对象。

TYPE* PyObject_GC_Resize(TYPE, PyVarObject *op, Py_ssize_t newsize)

Resize an object allocated by PyObject_NewVar(). Returns the resized object or NULL on failure. op must not be tracked by the collector yet.

void PyObject_GC_Track(PyObject *op)

把对象 op 加入到垃圾回收器跟踪的容器对象中。对象在被回收器跟踪时必须保持有效的，因为回收器可能在任何时候开始运行。在 tp_traverse 处理前的所有字段变为有效后，必须调用此函数，通常在靠近构造函数末尾的位置。

void _PyObject_GC_TRACK(PyObject *op)

PyObject_GC_Track() 的宏实现版本。它不能被用于扩展模块。

3.6 版后已移除: 这个宏在 Python 3.8 中被移除。

同样的，对象的释放器必须符合两个类似的规则：

在引用其它容器的字段失效前，必须调用 PyObject_GC_UnTrack() 。

必须使用 PyObject_GC_Del() 释放对象的内存。

void PyObject_GC_Del(void *op)

释放对象的内存，该对象初始化时由 PyObject_GC_New() 或 PyObject_GC_NewVar() 分配内存。

void PyObject_GC_UnTrack(void *op)

从回收器跟踪的容器对象集合中移除 op 对象。 请注意可以在此对象上再次调用 PyObject_GC_Track() 以将其加回到被跟踪对象集合。 释放器 (tp_dealloc 句柄) 应当在 tp_traverse 句柄所使用的任何字段失效之前为对象调用此函数。

void _PyObject_GC_UNTRACK(PyObject *op)

PyObject_GC_UnTrack() 的使用宏实现的版本。不能用于扩展模块。

3.6 版后已移除: 这个宏在 Python 3.8 中被移除。

tp_traverse 处理接收以下类型的函数形参。

int (*visitproc)(PyObject *object, void *arg)

传给 tp_traverse 处理的访问函数的类型。object 是容器中需要被遍历的一个对象，第三个形参对应于 tp_traverse 处理的 arg 。Python核心使用多个访问者函数实现循环引用的垃圾检测，不需要用户自行实现访问者函数。

tp_traverse 处理必须是以下类型：

int (*traverseproc)(PyObject *self, visitproc visit, void *arg)

Traversal function for a container object. Implementations must call the visit function for each object directly contained by self, with the parameters to visit being the contained object and the arg value passed to the handler. The visit function must not be called with a NULL object argument. If visit returns a non-zero value that value should be returned immediately.

为了简化 tp_traverse 处理的实现，Python提供了一个 Py_VISIT() 宏。若要使用这个宏，必须把 tp_traverse 的参数命名为 visit 和 arg 。

void Py_VISIT(PyObject *o)

If o is not NULL, call the visit callback, with arguments o and arg. If visit returns a non-zero value, then return it. Using this macro, tp_traverse handlers look like:

static int
my_traverse(Noddy *self, visitproc visit, void *arg)
{
    Py_VISIT(self->foo);
    Py_VISIT(self->bar);
    return 0;
}

The tp_clear handler must be of the inquiry type, or NULL if the object is immutable.

int (*inquiry)(PyObject *self)

丢弃产生循环引用的引用。不可变对象不需要声明此方法，因为他们不可能直接产生循环引用。需要注意的是，对象在调用此方法后必须仍是有效的（不能对引用只调用 Py_DECREF() 方法）。当垃圾回收器检测到该对象在循环引用中时，此方法会被调用。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

对象实现支持 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

API 和 ABI 版本管理

PY_VERSION_HEX 是Python的版本号的单一整数形式。

例如, 如果 “PY _ VERSION _ HEX” 设置为 “0x0304012”, 则可以通过按以下方式将其视为32位数字来查找基础版本信息:

字节串

位数（大端字节序）

含义

1

1-8

PY_MAJOR_VERSION (the 3 in 3.4.1a2)

2

9-16

PY_MINOR_VERSION (the 4 in 3.4.1a2)

3

17-24

PY_MICRO_VERSION (the 1 in 3.4.1a2)

4

25-28

PY_RELEASE_LEVEL (0xA 是 alpha版本, 0xB 是 beta版本, 0xC 发布的候选版本并且 0xF 是最终版本)，在这个例子中这个版本是alpha版本。

29-32

PY_RELEASE_SERIAL (3.4.1a2``中的``2，最终版本用0)

因此 3.4.1a2 的16进制版本号是 0x030401a2 。

所有提到的宏都定义在 Include/patchlevel.h [https://github.com/python/cpython/tree/3.6/Include/patchlevel.h]。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python/C API 参考手册 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

分发 Python 模块

电子邮箱

distutils-sig@python.org

作为一个流行的开源开发项目，Python拥有一个活跃的贡献者和用户支持社区，这些社区也可以让他们的软件可供其他Python开发人员在开源许可条款下使用。

这允许Python用户有效地共享和协作，从其他人已经创建的解决方案中受益于常见（有时甚至是罕见的）问题，以及可以提供他们自己的解决方案。

本指南涵盖了分发部分的流程。有关安装其他Python项目的指南，请参阅 安装指南。

注解

对于企业和其他机构用户，请注意许多组织都有自己的政策来使用和贡献开源软件。在使用Python提供的分发和安装工具时，请考虑这些政策。

关键术语

Python Packaging Index [https://pypi.org] 是一个开源许可的软件包公共存储库，可供所有 Python 用户使用。

the Python Packaging Authority [https://www.pypa.io/] are the group of developers and documentation authors responsible for the maintenance and evolution of the standard packaging tools and the associated metadata and file format standards. They maintain a variety of tools, documentation and issue trackers on both GitHub [https://github.com/pypa] and BitBucket [https://bitbucket.org/pypa/].

distutils 是1998年首次添加到Python标准库的原始构建和分发系统。虽然直接使用 distutils 正在逐步淘汰，但它仍然为当前的打包和分发基础架构奠定了基础它不仅仍然是标准库的一部分，而且它的名称还以其他方式存在（例如用于协调Python包装标准开发的邮件列表的名称）。

setuptools [https://setuptools.readthedocs.io/en/latest/] （在很大程序上）是作为 distutils 的取代者，于 2004 年首次发布。它对未经修改的 distutils 工具最重要的补充是能够声明对其他包的依赖。目前它被推荐用来替代 distutils，其更新更为频繁，在更为多样的 Python 版本之上为最新的包标准提供持续支持。

wheel [https://wheel.readthedocs.org] （在此上下文中）是一个将 bdist_wheel 命令添加到 distutils/setuptools [https://setuptools.readthedocs.io/en/latest/] 的项目。这产生了一个跨平台的二进制打包格式（称为“轮子”或“轮子文件”，并在 PEP 427 [https://www.python.org/dev/peps/pep-0427] 中定义），它允许在系统上安装Python库，甚至包括二进制扩展的库，而不需在本地进行构建。

开源许可与协作

在世界上大多数地方，软件自动受版权保护。这意味着其他开发人员需要明确的权限来复制，使用，修改和重新分发软件。

开源许可是一种以相对一致的方式明确授予此类权限的方式，允许开发人员通过为各种问题免费提供通用解决方案来有效地共享和协作。这使得许多开发人员可以将更多时间用于关注他们特定情况相对独特的问题。

Python提供的分发工具旨在使开发人员选择开源时，可以合理地直接将其自己的贡献回馈到该公共软件池。

无论该软件是否作为开源软件发布，相同的分发工具也可用于在组织内分发软件。

安装工具

标准库不包括支持现代Python打包标准的构建工具，因为核心开发团队已经发现，即使在旧版本的Python上，使用一致工作的标准工具也很重要。

可以通过在命令行调用 pip 模块来安装当前推荐的构建和分发工具:

python -m pip install setuptools wheel twine

注解

对于POSIX用户（包括Mac OS X和Linux用户），这些说明假定使用 virtual environment 。

对于Windows用户，这些说明假定在安装Python时选择了调整系统PATH环境变量的选项。

Python打包指南包含有关 currently recommended tools [https://packaging.python.org/en/latest/current/#packaging-tool-recommendations] 的更多详细信息。

Reading the guide

The Python Packaging User Guide covers the various key steps and elements involved in creating a project:

项目的结构

项目的构建与打包

上传项目到 Python Packaging Index

我该如何…？

这是一些常见任务的快速解答或相关链接。

…为我的项目选择一个名字？

这不是一个简单的主题，但这里有一些提示：

检查Python Packaging Index以查看该名称是否已被使用

check popular hosting sites like GitHub, BitBucket, etc to see if there is already a project with that name

检查您正在考虑的名称在网络搜索中出现的内容

避免使用特别常见的单词，尤其是具有多重含义的单词，因为它们会使用户在搜索时难以找到您的软件

…创建和分发二进制扩展？

这实际上是一个非常复杂的主题，根据您的目标，可以提供各种替代方案。有关更多信息和建议，请参阅Python打包指南。

参见

Python Packaging User Guide: Binary Extensions [https://packaging.python.org/en/latest/extensions]

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

安装 Python 模块

电子邮箱

distutils-sig@python.org

作为一个流行的开源开发项目，Python拥有一个活跃的贡献者和用户支持社区，这些社区也可以让他们的软件可供其他Python开发人员在开源许可条款下使用。

这允许Python用户有效地共享和协作，从其他人已经创建的解决方案中受益于常见（有时甚至是罕见的）问题，以及可以提供他们自己的解决方案。

本指南涵盖了分发部分的流程。有关安装其他Python项目的指南，请参阅 安装指南。

注解

对于企业和其他机构用户，请注意许多组织都有自己的政策来使用和贡献开源软件。在使用Python提供的分发和安装工具时，请考虑这些政策。

关键术语

pip 是首选的安装程序。从Python 3.4开始，它默认包含在Python二进制安装程序中。

virtual environment 是一种半隔离的 Python 环境，允许为特定的应用安装各自的包，而不是安装到整个系统。

venv 是创建虚拟环境的标准工具，从 Python 3.3 开始成为 Python 的组成部分。 从 Python 3.4 开始，它会默认安装 pip 到所创建的全部虚拟环境。

virtualenv 是 venv 的第三方替代（及其前身）。 它允许在 Python 3.4 之前的版本中使用虚拟环境，那些版本或是完全不提供 venv，或是不会自动安装 pip 到所创建的虚拟环境。

Python Packaging Index [https://pypi.org] 是一个由 Python 用户向其他用户发布开源许可软件包的公共仓库。

the Python Packaging Authority [https://www.pypa.io/en/latest/] are the group of developers and documentation authors responsible for the maintenance and evolution of the standard packaging tools and the associated metadata and file format standards. They maintain a variety of tools, documentation, and issue trackers on both GitHub [https://github.com/pypa] and BitBucket [https://bitbucket.org/pypa/].

distutils 是最初的构建和分发系统，于 1998 年首次加入 Python 标准库。 虽然直接使用 distutils 的方式已被淘汰，它仍然是当前打包和分发架构的基础，而且它不仅仍然是标准库的一部分，这个名称还以其他方式存在（例如用于协调 Python 打包标准开发流程的邮件列表就以此命名）。

3.6 版后已移除: pyvenv 是 Python 3.3 和 3.4 中创建虚拟环境的推荐工具，不过 在 Python 3.6 中已弃用 [https://docs.python.org/dev/whatsnew/3.6.html#deprecated-features]。

在 3.5 版更改: 现在推荐使用 venv 来创建虚拟环境。

参见

Python 软件包用户指南：创建和使用虚拟环境 [https://packaging.python.org/installing/#creating-virtual-environments]

基本使用

标准打包工具完全是针对命令行使用方式来设计的。

以下命令将从 Python Packaging Index 安装一个模块的最新版本及其依赖项:

python -m pip install SomePackage

注解

对于 POSIX 用户（包括 Mac OS X 和 Linux 用户）本指南中的示例假定使用了 virtual environment。

对于 Windows 用户，本指南中的示例假定在安装 Python 时选择了修改系统 PATH 环境变量。

在命令行中指定一个准确或最小版本也是可以的。 当使用比较运算符例如 >, < 或其他某些可以被终端所解析的特殊字符时，包名称与版本号应当用双引号括起来:

python -m pip install SomePackage==1.0.4    # specific version
python -m pip install "SomePackage>=1.0.4"  # minimum version

通常，如果一个匹配的模块已安装，尝试再次安装将不会有任何效果。 要升级现有模块必须显式地发出请求:

python -m pip install --upgrade SomePackage

更多有关 pip 及其功能的信息和资源可以在 Python 软件包用户指南 [https://packaging.python.org] 中找到。

虚拟环境的创建可使用 venv 模块来完成。 向已激活虚拟环境安装软件包可使用上文所介绍的命令。

参见

Python 软件包用户指南：安装 Python 分发包 [https://packaging.python.org/installing/]

我应如何 …？

这是一些常见任务的快速解答或相关链接。

… 在 Python 3.4 之前的 Python 版本中安装 pip ？

Python 捆绑 pip 是从 Python 3.4 才开始的。 对于更早的版本，pip 需要“引导安装bootstrapped”，具体说明参见 Python 软件包用户指南。

参见

Python 软件包用户指南：安装软件包的前提要求 [https://packaging.python.org/installing/#requirements-for-installing-packages]

… 只为当前用户安装软件包？

将 --user 选项传入 python -m pip install 将只为当前用户而非为系统中的所有用户安装软件包。

… 安装科学计算类 Python 软件包？

许多科学计算类 Python 软件包都有复杂的二进制编译文件依赖，直接使用 pip 安装目前并不太容易。 在当前情况下，通过 其他方式 [https://packaging.python.org/science/] 而非尝试用 pip 安装这些软件包对用户来说通常会更容易。

参见

Python 软件包用户指南：安装科学计算类软件包 [https://packaging.python.org/science/]

… 使用并行安装的多个 Python 版本？

在 Linux, Mac OS X 以及其他 POSIX 系统中，使用带版本号的 Python 命令配合 -m 开关选项来运行特定版本的 pip:

python2   -m pip install SomePackage  # default Python 2
python2.7 -m pip install SomePackage  # specifically Python 2.7
python3   -m pip install SomePackage  # default Python 3
python3.4 -m pip install SomePackage  # specifically Python 3.4

也可以使用带特定版本号的 pip 命令。

在 Windows 中，使用 py Python 启动器命令配合 -m 开关选项:

py -2   -m pip install SomePackage  # default Python 2
py -2.7 -m pip install SomePackage  # specifically Python 2.7
py -3   -m pip install SomePackage  # default Python 3
py -3.4 -m pip install SomePackage  # specifically Python 3.4

常见的安装问题

在 Linux 的系统 Python 版本上安装

Linux 系统通常会将某个 Python 版本作为发行版的一部分包含在内。 将软件包安装到这个 Python 版本上需要系统 root 权限，并可能会干扰到系统包管理器和其他系统组件的运作，如果这些组件在使用 pip 时被意外升级的话。

在这样的系统上，通过 pip 安装软件包通常最好是使用虚拟环境或分用户安装。

未安装 pip

默认情况下可能未安装 pip，一种可选解决方案是:

python -m ensurepip --default-pip

还有其他资源可用来 安装 pip [https://packaging.python.org/tutorials/installing-packages/#install-pip-setuptools-and-wheel]

安装二进制编译扩展

Python 通常非常依赖基于源代码的发布方式，也就是期望最终用户在安装过程中使用源码来编译生成扩展模块。

随着对二进制码 wheel 格式支持的引入，以及通过 Python Packaging Index 至少发布 Windows 和 Mac OS X 版的 wheel 文件，预计此问题将逐步得到解决，因为用户将能够更频繁地安装预编译扩展，而不再需要自己编译它们。

某些用来安装 科学计算类软件包 [https://packaging.python.org/science/] 的解决方案对于尚未提供预编译 wheel 文件的那些扩展模块来说，也有助于用户在无需进行本机编译的情况下获取二进制码扩展模块。

参见

Python 软件包用户指南：二进制码扩展 [https://packaging.python.org/extensions/]

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

Python 常用指引

Python常用指引的形式来源于Linux文档项目的常用指引章节，是一系列独立、指定主题并尝试完全覆盖该主题的文章集合。致力于提供比Python库参考帮助更详尽的文档。

目前，提供了如下指引：

将 Python 2 代码迁移到 Python 3

将扩展模块移植到 Python 3

用 Python 进行 Curses 编程

实现描述器

函数式编程指引

日志 HOWTO

日志操作手册

正则表达式HOWTO

套接字编程指南

排序指南

Unicode 指南

HOWTO 使用 urllib 包获取网络资源

Argparse 教程

ipaddress模块介绍

Argument Clinic How-To

使用 DTrace 和 SystemTap 检测CPython

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

将 Python 2 代码迁移到 Python 3

作者

Brett Cannon

摘要

Python 3 是 Python 的未来，但 Python 2 仍处于活跃使用阶段，最好让您的项目在两个主要版本的Python 上都可用。本指南旨在帮助您了解如何最好地同时支持 Python 2 和 3。

如果您希望迁移扩展模块而不是纯 Python 代码，请参阅 将扩展模块移植到 Python 3。

如果你想了解核心 Python 开发者对于 Python 3 的出现有何看法，你可以阅读 Nick Coghlan 的 Python 3 Q & A [https://ncoghlan-devs-python-notes.readthedocs.org/en/latest/python3/questions_and_answers.html] 或 Brett Cannon 的 为什么要有 Python 3 [http://www.snarky.ca/why-python-3-exists].

有关迁移的帮助，您可以通过电子邮件向 python-porting [https://mail.python.org/mailman/listinfo/python-porting] 邮件列表发送问题。

简要说明

To make your project be single-source Python 2/3 compatible, the basic steps are:

Only worry about supporting Python 2.7

确保有良好的测试覆盖率（可以用 coverage.py [https://pypi.org/project/coverage] 来完成此任务；用 pip install coverage 安装）

了解Python 2 和 3之间的区别

使用 Futurize [http://python-future.org/automatic_conversion.html] (或 Modernize [https://python-modernize.readthedocs.org/en/latest/]) 升级你的代码 (例如. pip install future)

Use Pylint [https://pypi.org/project/pylint] to help make sure you don’t regress on your Python 3 support (pip install pylint)

Use caniusepython3 [https://pypi.org/project/caniusepython3] to find out which of your dependencies are blocking your use of Python 3 (pip install caniusepython3)

Once your dependencies are no longer blocking you, use continuous integration to make sure you stay compatible with Python 2 & 3 (tox [https://pypi.org/project/tox] can help test against multiple versions of Python; pip install tox)

Consider using optional static type checking to make sure your type usage works in both Python 2 & 3 (e.g. use mypy [http://mypy-lang.org/] to check your typing under both Python 2 & Python 3).

详情

A key point about supporting Python 2 & 3 simultaneously is that you can start today! Even if your dependencies are not supporting Python 3 yet that does not mean you can’t modernize your code now to support Python 3. Most changes required to support Python 3 lead to cleaner code using newer practices even in Python 2 code.

Another key point is that modernizing your Python 2 code to also support Python 3 is largely automated for you. While you might have to make some API decisions thanks to Python 3 clarifying text data versus binary data, the lower-level work is now mostly done for you and thus can at least benefit from the automated changes immediately.

Keep those key points in mind while you read on about the details of porting your code to support Python 2 & 3 simultaneously.

删除对Python 2.6及更早版本的支持

While you can make Python 2.5 work with Python 3, it is much easier if you only have to work with Python 2.7. If dropping Python 2.5 is not an option then the six [https://pypi.org/project/six] project can help you support Python 2.5 & 3 simultaneously (pip install six). Do realize, though, that nearly all the projects listed in this HOWTO will not be available to you.

If you are able to skip Python 2.5 and older, then the required changes to your code should continue to look and feel like idiomatic Python code. At worst you will have to use a function instead of a method in some instances or have to import a function instead of using a built-in one, but otherwise the overall transformation should not feel foreign to you.

But you should aim for only supporting Python 2.7. Python 2.6 is no longer freely supported and thus is not receiving bugfixes. This means you will have to work around any issues you come across with Python 2.6. There are also some tools mentioned in this HOWTO which do not support Python 2.6 (e.g., Pylint [https://pypi.org/project/pylint]), and this will become more commonplace as time goes on. It will simply be easier for you if you only support the versions of Python that you have to support.

Make sure you specify the proper version support in your setup.py file

In your setup.py file you should have the proper trove classifier [https://pypi.org/classifiers] specifying what versions of Python you support. As your project does not support Python 3 yet you should at least have Programming Language :: Python :: 2 :: Only specified. Ideally you should also specify each major/minor version of Python that you do support, e.g. Programming Language :: Python :: 2.7.

良好的测试覆盖率

Once you have your code supporting the oldest version of Python 2 you want it to, you will want to make sure your test suite has good coverage. A good rule of thumb is that if you want to be confident enough in your test suite that any failures that appear after having tools rewrite your code are actual bugs in the tools and not in your code. If you want a number to aim for, try to get over 80% coverage (and don’t feel bad if you find it hard to get better than 90% coverage). If you don’t already have a tool to measure test coverage then coverage.py [https://pypi.org/project/coverage] is recommended.

了解Python 2 和 3之间的区别

Once you have your code well-tested you are ready to begin porting your code to Python 3! But to fully understand how your code is going to change and what you want to look out for while you code, you will want to learn what changes Python 3 makes in terms of Python 2. Typically the two best ways of doing that is reading the “What’s New” [https://docs.python.org/3/whatsnew/index.html] doc for each release of Python 3 and the Porting to Python 3 [http://python3porting.com/] book (which is free online). There is also a handy cheat sheet [http://python-future.org/compatible_idioms.html] from the Python-Future project.

更新代码

Once you feel like you know what is different in Python 3 compared to Python 2, it’s time to update your code! You have a choice between two tools in porting your code automatically: Futurize [http://python-future.org/automatic_conversion.html] and Modernize [https://python-modernize.readthedocs.org/en/latest/]. Which tool you choose will depend on how much like Python 3 you want your code to be. Futurize [http://python-future.org/automatic_conversion.html] does its best to make Python 3 idioms and practices exist in Python 2, e.g. backporting the bytes type from Python 3 so that you have semantic parity between the major versions of Python. Modernize [https://python-modernize.readthedocs.org/en/latest/], on the other hand, is more conservative and targets a Python 2/3 subset of Python, directly relying on six [https://pypi.org/project/six] to help provide compatibility. As Python 3 is the future, it might be best to consider Futurize to begin adjusting to any new practices that Python 3 introduces which you are not accustomed to yet.

Regardless of which tool you choose, they will update your code to run under Python 3 while staying compatible with the version of Python 2 you started with. Depending on how conservative you want to be, you may want to run the tool over your test suite first and visually inspect the diff to make sure the transformation is accurate. After you have transformed your test suite and verified that all the tests still pass as expected, then you can transform your application code knowing that any tests which fail is a translation failure.

Unfortunately the tools can’t automate everything to make your code work under Python 3 and so there are a handful of things you will need to update manually to get full Python 3 support (which of these steps are necessary vary between the tools). Read the documentation for the tool you choose to use to see what it fixes by default and what it can do optionally to know what will (not) be fixed for you and what you may have to fix on your own (e.g. using io.open() over the built-in open() function is off by default in Modernize). Luckily, though, there are only a couple of things to watch out for which can be considered large issues that may be hard to debug if not watched for.

除法

In Python 3, 5 / 2 == 2.5 and not 2; all division between int values result in a float. This change has actually been planned since Python 2.2 which was released in 2002. Since then users have been encouraged to add from __future__ import division to any and all files which use the / and // operators or to be running the interpreter with the -Q flag. If you have not been doing this then you will need to go through your code and do two things:

Add from __future__ import division to your files

Update any division operator as necessary to either use // to use floor division or continue using / and expect a float

The reason that / isn’t simply translated to // automatically is that if an object defines a __truediv__ method but not __floordiv__ then your code would begin to fail (e.g. a user-defined class that uses / to signify some operation but not // for the same thing or at all).

文本与二进制数据

In Python 2 you could use the str type for both text and binary data. Unfortunately this confluence of two different concepts could lead to brittle code which sometimes worked for either kind of data, sometimes not. It also could lead to confusing APIs if people didn’t explicitly state that something that accepted str accepted either text or binary data instead of one specific type. This complicated the situation especially for anyone supporting multiple languages as APIs wouldn’t bother explicitly supporting unicode when they claimed text data support.

To make the distinction between text and binary data clearer and more pronounced, Python 3 did what most languages created in the age of the internet have done and made text and binary data distinct types that cannot blindly be mixed together (Python predates widespread access to the internet). For any code that deals only with text or only binary data, this separation doesn’t pose an issue. But for code that has to deal with both, it does mean you might have to now care about when you are using text compared to binary data, which is why this cannot be entirely automated.

To start, you will need to decide which APIs take text and which take binary (it is highly recommended you don’t design APIs that can take both due to the difficulty of keeping the code working; as stated earlier it is difficult to do well). In Python 2 this means making sure the APIs that take text can work with unicode and those that work with binary data work with the bytes type from Python 3 (which is a subset of str in Python 2 and acts as an alias for bytes type in Python 2). Usually the biggest issue is realizing which methods exist on which types in Python 2 & 3 simultaneously (for text that’s unicode in Python 2 and str in Python 3, for binary that’s str/bytes in Python 2 and bytes in Python 3). The following table lists the unique methods of each data type across Python 2 & 3 (e.g., the decode() method is usable on the equivalent binary data type in either Python 2 or 3, but it can’t be used by the textual data type consistently between Python 2 and 3 because str in Python 3 doesn’t have the method). Do note that as of Python 3.5 the __mod__ method was added to the bytes type.

文本数据

二进制数据

decode

encode

格式

isdecimal

isnumeric

Making the distinction easier to handle can be accomplished by encoding and decoding between binary data and text at the edge of your code. This means that when you receive text in binary data, you should immediately decode it. And if your code needs to send text as binary data then encode it as late as possible. This allows your code to work with only text internally and thus eliminates having to keep track of what type of data you are working with.

The next issue is making sure you know whether the string literals in your code represent text or binary data. You should add a b prefix to any literal that presents binary data. For text you should add a u prefix to the text literal. (there is a __future__ import to force all unspecified literals to be Unicode, but usage has shown it isn’t as effective as adding a b or u prefix to all literals explicitly)

As part of this dichotomy you also need to be careful about opening files. Unless you have been working on Windows, there is a chance you have not always bothered to add the b mode when opening a binary file (e.g., rb for binary reading). Under Python 3, binary files and text files are clearly distinct and mutually incompatible; see the io module for details. Therefore, you must make a decision of whether a file will be used for binary access (allowing binary data to be read and/or written) or textual access (allowing text data to be read and/or written). You should also use io.open() for opening files instead of the built-in open() function as the io module is consistent from Python 2 to 3 while the built-in open() function is not (in Python 3 it’s actually io.open()). Do not bother with the outdated practice of using codecs.open() as that’s only necessary for keeping compatibility with Python 2.5.

The constructors of both str and bytes have different semantics for the same arguments between Python 2 & 3. Passing an integer to bytes in Python 2 will give you the string representation of the integer: bytes(3) == '3'. But in Python 3, an integer argument to bytes will give you a bytes object as long as the integer specified, filled with null bytes: bytes(3) == b'\x00\x00\x00'. A similar worry is necessary when passing a bytes object to str. In Python 2 you just get the bytes object back: str(b'3') == b'3'. But in Python 3 you get the string representation of the bytes object: str(b'3') == "b'3'".

Finally, the indexing of binary data requires careful handling (slicing does not require any special handling). In Python 2, b'123'[1] == b'2' while in Python 3 b'123'[1] == 50. Because binary data is simply a collection of binary numbers, Python 3 returns the integer value for the byte you index on. But in Python 2 because bytes == str, indexing returns a one-item slice of bytes. The six [https://pypi.org/project/six] project has a function named six.indexbytes() which will return an integer like in Python 3: six.indexbytes(b'123', 1).

To summarize:

Decide which of your APIs take text and which take binary data

Make sure that your code that works with text also works with unicode and code for binary data works with bytes in Python 2 (see the table above for what methods you cannot use for each type)

Mark all binary literals with a b prefix, textual literals with a u prefix

Decode binary data to text as soon as possible, encode text as binary data as late as possible

Open files using io.open() and make sure to specify the b mode when appropriate

Be careful when indexing into binary data

Use feature detection instead of version detection

Inevitably you will have code that has to choose what to do based on what version of Python is running. The best way to do this is with feature detection of whether the version of Python you’re running under supports what you need. If for some reason that doesn’t work then you should make the version check be against Python 2 and not Python 3. To help explain this, let’s look at an example.

Let’s pretend that you need access to a feature of importlib [https://docs.python.org/3/library/importlib.html#module-importlib] that is available in Python’s standard library since Python 3.3 and available for Python 2 through importlib2 [https://pypi.org/project/importlib2] on PyPI. You might be tempted to write code to access e.g. the importlib.abc module by doing the following:

import sys

if sys.version_info[0] == 3:
    from importlib import abc
else:
    from importlib2 import abc

The problem with this code is what happens when Python 4 comes out? It would be better to treat Python 2 as the exceptional case instead of Python 3 and assume that future Python versions will be more compatible with Python 3 than Python 2:

import sys

if sys.version_info[0] > 2:
    from importlib import abc
else:
    from importlib2 import abc

The best solution, though, is to do no version detection at all and instead rely on feature detection. That avoids any potential issues of getting the version detection wrong and helps keep you future-compatible:

try:
    from importlib import abc
except ImportError:
    from importlib2 import abc

Prevent compatibility regressions

Once you have fully translated your code to be compatible with Python 3, you will want to make sure your code doesn’t regress and stop working under Python 3. This is especially true if you have a dependency which is blocking you from actually running under Python 3 at the moment.

To help with staying compatible, any new modules you create should have at least the following block of code at the top of it:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

You can also run Python 2 with the -3 flag to be warned about various compatibility issues your code triggers during execution. If you turn warnings into errors with -Werror then you can make sure that you don’t accidentally miss a warning.

You can also use the Pylint [https://pypi.org/project/pylint] project and its --py3k flag to lint your code to receive warnings when your code begins to deviate from Python 3 compatibility. This also prevents you from having to run Modernize [https://python-modernize.readthedocs.org/en/latest/] or Futurize [http://python-future.org/automatic_conversion.html] over your code regularly to catch compatibility regressions. This does require you only support Python 2.7 and Python 3.4 or newer as that is Pylint’s minimum Python version support.

Check which dependencies block your transition

After you have made your code compatible with Python 3 you should begin to care about whether your dependencies have also been ported. The caniusepython3 [https://pypi.org/project/caniusepython3] project was created to help you determine which projects – directly or indirectly – are blocking you from supporting Python 3. There is both a command-line tool as well as a web interface at https://caniusepython3.com.

The project also provides code which you can integrate into your test suite so that you will have a failing test when you no longer have dependencies blocking you from using Python 3. This allows you to avoid having to manually check your dependencies and to be notified quickly when you can start running on Python 3.

Update your setup.py file to denote Python 3 compatibility

Once your code works under Python 3, you should update the classifiers in your setup.py to contain Programming Language :: Python :: 3 and to not specify sole Python 2 support. This will tell anyone using your code that you support Python 2 and 3. Ideally you will also want to add classifiers for each major/minor version of Python you now support.

Use continuous integration to stay compatible

Once you are able to fully run under Python 3 you will want to make sure your code always works under both Python 2 & 3. Probably the best tool for running your tests under multiple Python interpreters is tox [https://pypi.org/project/tox]. You can then integrate tox with your continuous integration system so that you never accidentally break Python 2 or 3 support.

You may also want to use the -bb flag with the Python 3 interpreter to trigger an exception when you are comparing bytes to strings or bytes to an int (the latter is available starting in Python 3.5). By default type-differing comparisons simply return False, but if you made a mistake in your separation of text/binary data handling or indexing on bytes you wouldn’t easily find the mistake. This flag will raise an exception when these kinds of comparisons occur, making the mistake much easier to track down.

And that’s mostly it! At this point your code base is compatible with both Python 2 and 3 simultaneously. Your testing will also be set up so that you don’t accidentally break Python 2 or 3 compatibility regardless of which version you typically run your tests under while developing.

考虑使用可选的静态类型检查

Another way to help port your code is to use a static type checker like mypy [http://mypy-lang.org/] or pytype [https://github.com/google/pytype] on your code. These tools can be used to analyze your code as if it’s being run under Python 2, then you can run the tool a second time as if your code is running under Python 3. By running a static type checker twice like this you can discover if you’re e.g. misusing binary data type in one version of Python compared to another. If you add optional type hints to your code you can also explicitly state whether your APIs use textual or binary data, helping to make sure everything functions as expected in both versions of Python.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

将扩展模块移植到 Python 3

作者

Benjamin Peterson

摘要

尽管更改 C-API 并不是 Python 3 的目标之一，但许多 Python 级别的更改使得 Python 2 的 API 无法完整实现。实际上，一些变化如 int() 和 long() 的统一在 C 级别更明显。本文档致力于记录不兼容性以及如何解决这些问题。

条件编译

仅编译 Python 3 的一些代码的最简单方法是检查 PY_MAJOR_VERSION 是否大于或等于3。

#if PY_MAJOR_VERSION >= 3
#define IS_PY3K
#endif

不存在的 API 函数可以在条件块中别名为它们的等价物。

对象API的更改

Python 3 将一些具有类似功能的类型合并在一起，同时干净地分离了其他类型。

str/unicode 统一

Python 3 的 str() 类型相当于 Python 2 的 unicode() ； C函数被称为 PyUnicode_* 。旧的 8 位字符串类型变为 bytes() ，其中 C 函数称为 PyBytes_* 。 Python 2.6 及更高版本提供了一个兼容性头文件 bytesobject.h ，将 PyBytes 名称映射到 PyString 。 为了保持与 Python 3 的最佳兼容性， PyUnicode 应该用于文本数据，并且 PyBytes 用于二进制数据。同样重要的是要记住 pyBytes 和 Python 3中的 PyUnicode 不可互换，如 PyString 和 PyUnicode 在 Python 2 以下中示例显示了以下方面的最佳实践 PyUnicode 、 PyString 和 PyBytes 。:

#include "stdlib.h"
#include "Python.h"
#include "bytesobject.h"

/* text example */
static PyObject *
say_hello(PyObject *self, PyObject *args) {
    PyObject *name, *result;

    if (!PyArg_ParseTuple(args, "U:say_hello", &name))
        return NULL;

    result = PyUnicode_FromFormat("Hello, %S!", name);
    return result;
}

/* just a forward */
static char * do_encode(PyObject *);

/* bytes example */
static PyObject *
encode_object(PyObject *self, PyObject *args) {
    char *encoded;
    PyObject *result, *myobj;

    if (!PyArg_ParseTuple(args, "O:encode_object", &myobj))
        return NULL;

    encoded = do_encode(myobj);
    if (encoded == NULL)
        return NULL;
    result = PyBytes_FromString(encoded);
    free(encoded);
    return result;
}

long/int 统一

Python 3 只有一个整数类型， int() 。但它实际上对应于Python 2 long() 类型 —— 删除了 Python 2 中使用的 int() 类型。在 C-API 中， PyInt_* 函数被它们等价的 PyLong_* 替换。

模块初始化和状态

Python 3 有一个改进的扩展模块初始化系统。（参见 PEP 3121 [https://www.python.org/dev/peps/pep-3121] 。）而不是将模块状态存储在全局变量中，它们应该存储在特定于解释器的结构中。创建在 Python 2 和 Python 3 中正确运行的模块非常棘手。以下简单示例演示了如何操作。:

#include "Python.h"

struct module_state {
    PyObject *error;
};

#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
#else
#define GETSTATE(m) (&_state)
static struct module_state _state;
#endif

static PyObject *
error_out(PyObject *m) {
    struct module_state *st = GETSTATE(m);
    PyErr_SetString(st->error, "something bad happened");
    return NULL;
}

static PyMethodDef myextension_methods[] = {
    {"error_out", (PyCFunction)error_out, METH_NOARGS, NULL},
    {NULL, NULL}
};

#if PY_MAJOR_VERSION >= 3

static int myextension_traverse(PyObject *m, visitproc visit, void *arg) {
    Py_VISIT(GETSTATE(m)->error);
    return 0;
}

static int myextension_clear(PyObject *m) {
    Py_CLEAR(GETSTATE(m)->error);
    return 0;
}

static struct PyModuleDef moduledef = {
        PyModuleDef_HEAD_INIT,
        "myextension",
        NULL,
        sizeof(struct module_state),
        myextension_methods,
        NULL,
        myextension_traverse,
        myextension_clear,
        NULL
};

#define INITERROR return NULL

PyMODINIT_FUNC
PyInit_myextension(void)

#else
#define INITERROR return

void
initmyextension(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
    PyObject *module = PyModule_Create(&moduledef);
#else
    PyObject *module = Py_InitModule("myextension", myextension_methods);
#endif

    if (module == NULL)
        INITERROR;
    struct module_state *st = GETSTATE(module);

    st->error = PyErr_NewException("myextension.Error", NULL, NULL);
    if (st->error == NULL) {
        Py_DECREF(module);
        INITERROR;
    }

#if PY_MAJOR_VERSION >= 3
    return module;
#endif
}

CObject 替换为 Capsule

Capsule 对象是在 Python 3.1 和 2.7 中引入的，用于替换 CObject 。 CObject 是有用的，但是 CObject API 是有问题的：它不允许区分有效的 CObject ，这导致不匹配的 CObject 使解释器崩溃，并且它的一些 API 依赖于 C 中的未定义行为。有关 Capsule 背后的基本原理的进一步阅读，请参阅 bpo-5630 [https://bugs.python.org/issue5630] 。）

如果你当前正在使用 CObject ，并且想要迁移到 3.1 或更高版本，则需要切换到 Capsules 。 CObject 在 3.1 和 2.7 中已弃用，在 Python 3.2 中已完全删除。如果你只支持 2.7 或 3.1 及以上，你可以简单地切换到 Capsule 。如果你需要支持 Python 3.0 或早于 2.7 的 Python 版本，则必须同时支持 CObject 和 Capsule 。（请注意，不再支持 Python 3.0 ，不建议将其用于生产用途。）

以下示例头文件 capsulethunk.h 可以为你解决问题。只需针对 Capsule API 编写代码，并在以下文件后包含此头文件 Python.h 。你的代码将自动在带有 Capsule 的 Python 版本中使用 Capsules ，并在 Capsule 不可用时切换到 CObjects 。

capsulethunk.h 使用 CObject 模拟 Capsules 。 但是， CObject 没有提供存储胶囊的“名称”的地方。因此，模拟 Capsule 对象由 capsulethunk.h 创建，其行为与真实 Capsule 略有不同。特别地：

传递给 PyCapsule_New() 的 name 参数被忽略。

传入以下命令的 name 参数 PyCapsule_IsValid() 和 PyCapsule_GetPointer() 被忽略，并且不执行错误检查。

PyCapsule_GetName() 总是返回 NULL 。

PyCapsule_SetName() 总是引发异常并返回失败。（由于无法在 CObject 中存储名称，因此 PyCapsule_SetName() 的明显失败被认为优于静默失败。如果这样不方便，请随意根据需要修改本地副本。）

你可以在 Python 源代码分发中的 Doc/includes/capsulethunk.h [https://github.com/python/cpython/tree/3.6/Doc/includes/capsulethunk.h] 找到 capsulethunk.h 。为方便起见，我们还将其包含在此处：

#ifndef __CAPSULETHUNK_H
#define __CAPSULETHUNK_H

#if (    (PY_VERSION_HEX <  0x02070000) \
     || ((PY_VERSION_HEX >= 0x03000000) \
      && (PY_VERSION_HEX <  0x03010000)) )

#define __PyCapsule_GetField(capsule, field, default_value) \
    ( PyCapsule_CheckExact(capsule) \
        ? (((PyCObject *)capsule)->field) \
        : (default_value) \
    ) \

#define __PyCapsule_SetField(capsule, field, value) \
    ( PyCapsule_CheckExact(capsule) \
        ? (((PyCObject *)capsule)->field = value), 1 \
        : 0 \
    ) \

#define PyCapsule_Type PyCObject_Type

#define PyCapsule_CheckExact(capsule) (PyCObject_Check(capsule))
#define PyCapsule_IsValid(capsule, name) (PyCObject_Check(capsule))

#define PyCapsule_New(pointer, name, destructor) \
    (PyCObject_FromVoidPtr(pointer, destructor))

#define PyCapsule_GetPointer(capsule, name) \
    (PyCObject_AsVoidPtr(capsule))

/* Don't call PyCObject_SetPointer here, it fails if there's a destructor */
#define PyCapsule_SetPointer(capsule, pointer) \
    __PyCapsule_SetField(capsule, cobject, pointer)

#define PyCapsule_GetDestructor(capsule) \
    __PyCapsule_GetField(capsule, destructor)

#define PyCapsule_SetDestructor(capsule, dtor) \
    __PyCapsule_SetField(capsule, destructor, dtor)

/*
 * Sorry, there's simply no place
 * to store a Capsule "name" in a CObject.
 */
#define PyCapsule_GetName(capsule) NULL

static int
PyCapsule_SetName(PyObject *capsule, const char *unused)
{
    unused = unused;
    PyErr_SetString(PyExc_NotImplementedError,
        "can't use PyCapsule_SetName with CObjects");
    return 1;
}

#define PyCapsule_GetContext(capsule) \
    __PyCapsule_GetField(capsule, descr)

#define PyCapsule_SetContext(capsule, context) \
    __PyCapsule_SetField(capsule, descr, context)

static void *
PyCapsule_Import(const char *name, int no_block)
{
    PyObject *object = NULL;
    void *return_value = NULL;
    char *trace;
    size_t name_length = (strlen(name) + 1) * sizeof(char);
    char *name_dup = (char *)PyMem_MALLOC(name_length);

    if (!name_dup) {
        return NULL;
    }

    memcpy(name_dup, name, name_length);

    trace = name_dup;
    while (trace) {
        char *dot = strchr(trace, '.');
        if (dot) {
            *dot++ = '\0';
        }

        if (object == NULL) {
            if (no_block) {
                object = PyImport_ImportModuleNoBlock(trace);
            } else {
                object = PyImport_ImportModule(trace);
                if (!object) {
                    PyErr_Format(PyExc_ImportError,
                        "PyCapsule_Import could not "
                        "import module \"%s\"", trace);
                }
            }
        } else {
            PyObject *object2 = PyObject_GetAttrString(object, trace);
            Py_DECREF(object);
            object = object2;
        }
        if (!object) {
            goto EXIT;
        }

        trace = dot;
    }

    if (PyCObject_Check(object)) {
        PyCObject *cobject = (PyCObject *)object;
        return_value = cobject->cobject;
    } else {
        PyErr_Format(PyExc_AttributeError,
            "PyCapsule_Import \"%s\" is not valid",
            name);
    }

EXIT:
    Py_XDECREF(object);
    if (name_dup) {
        PyMem_FREE(name_dup);
    }
    return return_value;
}

#endif /* #if PY_VERSION_HEX < 0x02070000 */

#endif /* __CAPSULETHUNK_H */

其他选项

如果你正在编写新的扩展模块，你可能会考虑 Cython [http://cython.org/] 。 它将类似 Python 的语言转换为 C 。它创建的扩展模块与 Python 3 和 Python 2 兼容。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

用 Python 进行 Curses 编程

作者

A.M. Kuchling, Eric S. Raymond

发布版本

2.04

摘要

本文档介绍了如何使用 curses 扩展模块控制文本模式的显示。

curses 是什么？

curses 库为基于文本的终端提供了独立于终端的屏幕绘制和键盘处理功能；这些终端包括 VT100，Linux 控制台以及各种程序提供的模拟终端。显示终端支持各种控制代码以执行常见的操作，例如移动光标，滚动屏幕和擦除区域。不同的终端使用相差很大的代码，并且往往有自己的小怪癖。

在普遍使用图形显示的世界中，人们可能会问“为什么自找要麻烦”？毕竟字符单元显示终端确实是一种过时的技术，但是在某些领域中，能够用它们做花哨的事情仍然很有价值。一个小众市场是在不运行 X server 的小型或嵌入式 Unix 上。另一个是需要在提供图形支持之前运行的工具，例如操作系统安装程序和内核配置程序。

curses 库提供了相当基础的功能，为程序员提供了包含多个非重叠文本窗口的显示的抽象。窗口的内容可以通过多种方式更改—添加文本，擦除文本，更改其外观—以及curses库将确定需要向终端发送哪些控制代码以产生正确的输出。 curses并没有提供诸多用户界面概念，例如按钮，复选框或对话框。如果需要这些功能，请考虑用户界面库，例如 Urwid [https://pypi.org/project/urwid/] 。

curses 库最初是为BSD Unix 编写的。 AT＆T 的Unix 的后来的System V 版本增加了许多增强功能和新功能。如今BSD curses不再维护，被ncurses取代，ncurses是AT＆T接口的开源实现。如果您使用的是Linux 或FreeBSD 等开源Unix，则您的系统几乎肯定会使用ncurses。由于大多数当前的商业Unix版本都基于System V代码，因此这里描述的所有功能可能都可用。但是，某些专有Unix所带来的较早版本的curses可能无法支持所有功能。

Windows 版本的 Python 不包含 curses 模块。提供了一个名为 UniCurses [https://pypi.org/project/UniCurses] 的移植版本。也可以尝试使用 Fredrik Lundh 编写 the Console module [http://effbot.org/zone/console-index.htm]，它使用与curses不相同的API，但提供了可光标定位的文本输出，完全支持鼠标和键盘输入。

Python 的 curses 模块

此 Python 模块相当简单地封装了 curses 提供的 C 函数；如果你已经熟悉在 C 语言中使用 curses 编程，把这些知识转移的 Python 是非常容易的。最大的差异在于 Python 中的接口通过把不同的 C 函数合并来让事情变得更简单，比如 addstr()、mvaddstr() 和 mvwaddstr() 三个 C 函数被并入 addstr() 这一个方法。下文中会描述更多的细节。

本 HOWTO 是关于使用 curses 和 Python 编写文本模式程序的概述。它并不被设计为一个 curses API 的完整指南；如需完整指南，请参见 ncurses 的 Python 库指南章节和 ncurses 的 C 手册页。相对地，本 HOWTO 将会给你一些基本思路。

开始和结束curses应用程序

在做任何事情之前，curses 必须先被初始化。可以通过调用函数 initscr() 来实现，它将查明终端的类型，向终端发送任何必须的设置代码，并创建多种内部数据结构。如果此操作成功，initscr() 将会返回一个代表整个屏幕的窗口对象；它通常会遵循对应的 C 变量名被称作 stdscr：

import curses
stdscr = curses.initscr()

使用 curses 的应用程序通常会关闭按键自动上屏，目的是读取按键并只在特定情况下展示它们。这需要调用函数 noecho()：

curses.noecho()

应用程序也会广泛地需要立即响应按键，而不需要按下回车键；这被称为 “cbreak” 模式，与通常的缓冲输入模式相对：

curses.cbreak()

终端通常会以多字节转义序列的形式返回特殊按键，比如光标键和导航键比如 Page Up 键和 Home 键。尽管你可以编写你的程序来应对这些序列，curses 能够代替你做到这件事，返回一个特殊值比如 curses.KEY_LEFT。为了让 curses 做这项工作，你需要启用 keypad 模式：

stdscr.keypad(True)

终止一个 curses 应用程序比建立一个容易得多，你只需要调用：

curses.nocbreak()
stdscr.keypad(False)
curses.echo()

来还原对终端作出的 curses 友好设置。然后，调用函数 endwin() 来将终端还原到它的原始操作模式：

curses.endwin()

调试一个 curses 应用程序时常会出现，一个应用程序还未能还原终端到原本的状态就意外退出了，这会搅乱你的终端。在 Python 中这常常会发生在你的代码中有 bug 并抛出了一个未捕获的异常。当你尝试输入时按键不会上屏，这使得使用终端变得困难。

在 Python 中你可以避免这些复杂问题并让调试变得更简单，只需要导入 curses.wrapper() 函数并像这样使用它：

from curses import wrapper

def main(stdscr):
    # Clear screen
    stdscr.clear()

    # This raises ZeroDivisionError when i == 10.
    for i in range(0, 11):
        v = i-10
        stdscr.addstr(i, 0, '10 divided by {} is {}'.format(v, 10/v))

    stdscr.refresh()
    stdscr.getkey()

wrapper(main)

函数 wrapper() 接受一个可调用对象并首先进行上述初始化过程，在终端支持着色时还会初始化颜色。接着 wrapper() 运行你提供的可调用对象。当该可调用对象返回时，wrapper() 会还原终端到初始状态。该可调用对象会在 try…except 这样的结构内被调用，当它捕获到异常时，会先还原终端再重新抛出这个异常。所以你的终端不会因为异常而被留在一个搞笑的状态，你也可以正常阅读异常消息和回溯信息。

Windows 和 Pad

窗口是 curses 中的基本抽象。一个窗口对象表示了屏幕上的一个矩形区域，并且提供方法来显示文本、擦除文本、允许用户输入字符串等等。

函数 initscr() 返回的 stdscr 对象覆盖整个屏幕。许多程序可能只需要这一个窗口，但你可能希望把屏幕分割为多个更小的窗口，来分别重绘或者清除它们。函数 newwin() 根据给定的尺寸创建一个新窗口，并返回这个新的窗口对象：

begin_x = 20; begin_y = 7
height = 5; width = 40
win = curses.newwin(height, width, begin_y, begin_x)

注意 curses 使用的坐标系统与寻常的不同。坐标始终是以 y,x 的顺序传递，并且左上角是坐标 (0,0)。这打破了正常的坐标处理约定，即 x 坐标在前。这是一个与其他计算机应用程序糟糕的差异，但这从 curses 最初被编写出来就已是它的一部分，现在想要修改它已为时已晚。

你的应用程序能够查明屏幕的尺寸，curses.LINES 和 curses.COLS 分别代表了 y 和 x 方向上的尺寸。合理的坐标应位于 (0,0) 到 (curses.LINES - 1, curses.COLS - 1) 范围内。

当你调用一个方法来显示或擦除文本时，效果并不会立即显示。相反，你必须调用窗口对象的 refresh() 方法来更新屏幕。

这是因为 curses 最初是为 300 波特的龟速终端连接编写的；在这些终端上，压制重绘屏幕的时间就非常重要。相对地，当你调用 refresh() 时，curses 会累积屏幕的修改并以效率最高的方式显示它们。打个比方，如果你的程序在一个窗口内显示一些文本然后清楚了这个窗口，那么这些原始文本不需要被发送，因为它们甚至不曾能被看见。

在实践中，显式地告诉 curses 来重绘一个窗口并不会太复杂化 curses 编程。大部分程序会显示一堆内容然后等待按键或者其他某些用户侧动作。你要做的事情就是，保证屏幕在暂停并等待用户输入前被重绘，只需要先调用 stdscr.refresh() 或者其他相关窗口的 refresh() 方法。

一个面板是一种特殊的窗口，它可以比实际的显示屏幕更大，并且能只显示它的一部分。创建面板需要指定面板的高度和宽度，但刷新一个面板需要给出屏幕坐标和面板的需要显示的局部。

pad = curses.newpad(100, 100)
# These loops fill the pad with letters; addch() is
# explained in the next section
for y in range(0, 99):
    for x in range(0, 99):
        pad.addch(y,x, ord('a') + (x*x+y*y) % 26)

# Displays a section of the pad in the middle of the screen.
# (0,0) : coordinate of upper-left corner of pad area to display.
# (5,5) : coordinate of upper-left corner of window area to be filled
#         with pad content.
# (20, 75) : coordinate of lower-right corner of window area to be
#          : filled with pad content.
pad.refresh( 0,0, 5,5, 20,75)

此 refresh() 调用会在屏幕坐标 (5,5) 到坐标 (20,75) 的矩形范围内显示面板的一个部分，被显示的部分在面板上的坐标是 (0,0)。除了上述差异，面板与常规的窗口相同，也支持相同的方法。

如果你在屏幕上有多个窗口和面板，有一个更有效率的方法来更新窗口，避免每个部分单独更新时烦人的屏幕闪烁。refresh() 实际上做了两件事：

调用每个窗口的 noutrefresh() 方法来更新一个表达屏幕期望状态的底层的数据结构。

调用函数 doupdate() 来改变物理屏幕来符合这个数据结构中记录的期望状态。

你可以改为调用在多个窗口上 noutrefresh() 方法来更新该数据结构，然后调用函数 doupdate() 来更新屏幕。

显示文字

从一名 C 语言程序员的视角来看，curses 有时看起来就像是一堆略有差异的函数组成的扭曲迷宫。举个例子，addstr() 在 stdscr 窗口的当前光标位置显示一个字符串，而 mvaddstr() 则是先移动到一个给定的 y,x 坐标再显示字符串。waddstr() 与 addstr() 类似，但允许指定一个窗口而非默认的 stdscr。mvwaddstr() 允许同时指定一个窗口和一个坐标。

幸运的是，Python 接口隐藏了所有这些细节。stdscr 和其他任何窗口一样是一个窗口对象，并且诸如 addstr() 之类的方法接受多种参数形式。通常有四种形式。

形式

描述

str 或 ch

在当前位置显示字符串 str 或字符 ch

str 或 ch, attr

在当前位置使用 attr 属性显示字符串 str 或字符 ch

y, x, str 或 ch

移动到窗口内的 y,x 位置，并显示 str 或 ch

y, x, str 或 ch, attr

移至窗口内的 y,x 位置，并使用 attr 属性显示 str 或 ch

属性允许以突出显示形态显示文本，比如加粗、下划线、反相或添加颜色。这些属性将来下一小节细说。

方法 addstr() 接受一个 Python 字符串或字节串作为用于显示的值。字节串的内容会被原样发送到终端。字符串会使用窗口的 encoding 属性值编码为字节，它默认为 locale.getpreferredencoding() 返回的系统默认编码。

方法 addch() 接受一个字符，可以是长度为 1 的字符串，长度为 1 的字节串或者一个整数。

对于特殊扩展字符有一些常量，这些常量是大于 255 的整数。比如，ACS_PLMINUS 是一个 “加减” 符号，ACS_ULCORNER 是一个框的左上角（方便绘制边界）。你也可以使用正确的 Unicode 字符。

窗口会记住上次操作之后光标所在位置，所以如果你忽略 y,x 坐标，字符串和字符会出现在上次操作结束的位置。你也可以通过 move(y,x) 的方法来移动光标。因为一些终端始终会显示一个闪烁的光标，你可能会想要保证光标处于一些不会让人感到分心的位置。在看似随机的位置出现一个闪烁的光标会令人非常迷惑。

如果你的应用程序完全不需要一个闪烁的光标，你可以调用 curs_set(False) 来使它隐形。为与旧版本 curses 的兼容性的关系，有函数 leaveok(bool) 作为 curs_set() 的等价替换。如果 bool 是真值，curses 库会尝试移除闪烁光标，并且你也不必担心它会留在一些奇怪的位置。

属性和颜色

字符可以以不同的方式显示。基于文本的应用程序常常以反相显示状态行，一个文本查看器可能需要突出显示某些单词。为了支持这种用法，curses 允许你为屏幕上的每个单元指定一个属性。

属性值是一个整数，它的每一个二进制位代表一个不同的属性。你可以尝试以多种不属性位组合来显示文本，但 curses 不保证所有的组合都是有效的，或者看上去有明显不同。这一点取决于用户终端的能力，所以最稳妥的方式是只采用最常见的有效属性，见下表。

属性

描述

A_BLINK

闪烁文字

A_BOLD

超亮或粗体文字

A_DIM

半明亮的文字

A_REVERSE

反向视频文本

A_STANDOUT

可用的最佳突出显示模式

A_UNDERLINE

带下划线的文字

所以，为了在屏幕顶部显示一个反相的状态行，你可以这么编写：

stdscr.addstr(0, 0, "Current mode: Typing mode",
              curses.A_REVERSE)
stdscr.refresh()

curses 库还支持在提供了颜色功能的终端上显示颜色的功能。最常见的提供颜色的终端很可能是 Linux 控制台，采用了 xterms 配色方案。

为了使用颜色，你必须在调用完函数 initscr() 后尽快调用函数 start_color()，来初始化默认颜色集（curses.wrapper() 函数自动完成了这一点）。当它完成后，如果使用中的终端支持显示颜色， has_colors() 会返回 True。（注意：curses 使用美式拼写 “color”，而不是英式／加拿大拼写 “colour”。如果你习惯了英式拼写，你需要避免自己在这些函数上拼写错误。）

curses 库维护一个有限数量的颜色对，包括一个前景（文本）色和一个背景色。你可以使用函数 color_pair() 获得一个颜色对对应的属性值。它可以通过按位或运算与其他属性，比如 A_REVERSE 组合。但再说明一遍，这种组合并不保证在所有终端上都有效。

一个样例，用 1 号颜色对显示一行文本：

stdscr.addstr("Pretty text", curses.color_pair(1))
stdscr.refresh()

如前所述， 颜色对由前景色和背景色组成。 init_pair(n, f, b) 函数改变颜色对*n*的定义， 为前景色f和背景色b。 颜色对0硬连接到黑色上的白色，不能改变。

颜色已经被编号，并且当其激活 color 模式时 start_color() 会初始化 8 种基本颜色。 它们是: 0:black, 1:red, 2:green, 3:yellow, 4:blue, 5:magenta, 6:cyan 和 7:white。 curses 模块为这些颜色定义了相应的名称常量: curses.COLOR_BLACK, curses.COLOR_RED 等等。

让我们来做个综合练习。 要将颜色 1 改为红色文本白色背景，你应当调用:

curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)

当你改变一个颜色对时，任何已经使用该颜色对来显示的文本将会更改为新的颜色。 你还可以这样来显示新颜色的文本:

stdscr.addstr(0,0, "RED ALERT!", curses.color_pair(1))

某些非常花哨的终端可以将实际颜色定义修改为给定的 RGB 值。 这允许你将通常为红色的 1 号颜色改成紫色或蓝色或者任何你喜欢的颜色。 不幸的是，Linux 控制台不支持此特性，所以我无法尝试它，也无法提供任何示例。 想要检查你的终端是否能做到你可以调用 can_change_color()，如果有此功能则它将返回 True。 如果你幸运地拥有一个如此优秀的终端，请查询你的系统的帮助页面来了解详情。

用户输入

C curses 库提供了非常简单的输入机制。 Python 的 curses 模块添加了一个基本的文本输入控件。 （其他的库例如 Urwid [https://pypi.org/project/urwid/] 拥有更丰富的控件集。）

有两个方法可以从窗口获取输入:

getch() 会刷新屏幕然后等待用户按键，如果之前调用过 echo() 还会显示所按的键。 你还可以选择指定一个坐标以便在暂停之前让光标移动到那里。

getkey() 将做同样的事但是会把整数转换为字符串。 每个字符将返回为长度为 1 个字符的字符串，特殊键例如函数键将返回包含键名的较长字符串例如 KEY_UP 或 ^G。

使用 nodelay() 窗口方法可以做到不等待用户。 在 nodelay(True) 之后，窗口的 getch() 和 getkey() 将成为非阻塞的。 为表明输入未就绪，getch() 会返回 curses.ERR (值为 -1) 而 getkey() 会引发异常。 此外还有 halfdelay() 函数，它可被用来（实际地）在每个 getch() 上设置一个计时器；如果在指定的延迟内没有输入可用（以十分之一秒为单位），curses 将引发异常。

getch() 方法返回一个整数；如果数值在 0 到 255 之间，它代表所按下键的 ASCII 码。 大于 255 的值为特殊键例如 Page Up, Home 或方向键等。 你可以将返回的值与 curses.KEY_PPAGE, curses.KEY_HOME 或 curses.KEY_LEFT 等常量做比较。 你的程序主循环看起来可能是这样:

while True:
    c = stdscr.getch()
    if c == ord('p'):
        PrintDocument()
    elif c == ord('q'):
        break  # Exit the while loop
    elif c == curses.KEY_HOME:
        x = y = 0

curses.ascii 模块提供了一些 ASCII 类成员函数，它们接受整数或长度为 1 个字符的字符串参数；这些函数在为这样的循环编写更具可读性的测试时可能会很有用。 它还提供了一些转换函数，它们接受整数或长度为 1 个字符的字符串参数并返回同样的类型。 例如，curses.ascii.ctrl() 返回与其参数相对应的控制字符。

还有一个可以提取整个字符串的方法 getstr()。 它并不经常被使用，因为它的功能相当受限；可用的编辑键只有 Backspace 和 Enter 键，它们会结束字符串。 也可以选择限制为固定数量的字符。

curses.echo()            # Enable echoing of characters

# Get a 15-character string, with the cursor on the top line
s = stdscr.getstr(0,0, 15)

curses.textpad 模块提供了一个文本框，它支持类似 Emacs 的键绑定集。 Textbox 类的各种方法支持带输入验证的编辑及包含或不包含末尾空格地收集编辑结果。 下面是一个例子:

import curses
from curses.textpad import Textbox, rectangle

def main(stdscr):
    stdscr.addstr(0, 0, "Enter IM message: (hit Ctrl-G to send)")

    editwin = curses.newwin(5,30, 2,1)
    rectangle(stdscr, 1,0, 1+5+1, 1+30+1)
    stdscr.refresh()

    box = Textbox(editwin)

    # Let the user edit until Ctrl-G is struck.
    box.edit()

    # Get resulting contents
    message = box.gather()

请查看 curses.textpad 的库文档了解更多细节。

更多的信息

本 HOWTO 没有涵盖一些进阶主题，例如读取屏幕的内容或从 xterm 实例捕获鼠标事件等，但是 curses 模块的 Python 库文档页面现在已相当完善。 接下来你应当去浏览一下其中的内容。

如果你对 curses 函数的细节行为有疑问，请查看你的 curses 实现版本的说明页面，不论它是 ncurses 还是特定 Unix 厂商的版本。 说明页面将记录任何具体问题，并提供所有函数、属性以及可用 ACS_* 字符的完整列表。

由于 curses API 是如此的庞大，某些函数并不被 Python 接口所支持。 这往往不是因为它们难以实现，而是因为还没有人需要它们。 此外，Python 尚不支持与 ncurses 相关联的菜单库。 欢迎提供添加这些功能的补丁；请参阅 Python 开发者指南 [https://devguide.python.org/] 了解有关为 Python 提交补丁的详情。

使用 NCURSES 编写程序 [http://invisible-island.net/ncurses/ncurses-intro.html]: 一篇面向 C 程序员的详细教程。

The ncurses man page [http://linux.die.net/man/3/ncurses]

ncurses 常见问题 <http://invisible-island.net/ncurses/ncurses.faq.html>`_

“使用 curses… 请勿爆粗” [https://www.youtube.com/watch?v=eN1eZtjLEnU]: 一场有关使用 curses 或 Urwid 来控制终端的 PyCon 2013 演讲的视频。

“使用 Urwid 的控制台应用程序” [http://www.pyvideo.org/video/1568/console-applications-with-urwid]: 一场演示使用 Urwid 编写应用程序的 PyCon CA 2012 演讲的视频。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

实现描述器

作者

Raymond Hettinger

联系方式

<python at rcn dot com>

目录

实现描述器

摘要

定义和简介

描述器协议

发起调用描述器

描述符示例

属性

函数和方法

静态方法和类方法

摘要

Defines descriptors, summarizes the protocol, and shows how descriptors are called. Examines a custom descriptor and several built-in python descriptors including functions, properties, static methods, and class methods. Shows how each works by giving a pure Python equivalent and a sample application.

学习描述器不仅能提供接触到更多工具集的途径，还能更深地理解 Python 工作的原理并更加体会到其设计的优雅性。

定义和简介

一般地，一个描述器是一个包含 “绑定行为” 的对象，对其属性的访问被描述器协议中定义的方法覆盖。这些方法有：__get__()，__set__() 和 __delete__()。如果某个对象中定义了这些方法中的任意一个，那么这个对象就可以被称为一个描述器。

属性访问的默认行为是从一个对象的字典中获取、设置或删除属性。例如，a.x 的查找顺序会从 a.__dict__['x'] 开始，然后是 type(a).__dict__['x']，接下来依次查找 type(a) 的基类，不包括元类。 如果找到的值是定义了某个描述器方法的对象，则 Python 可能会重载默认行为并转而发起调用描述器方法。这具体发生在优先级链的哪个环节则要根据所定义的描述器方法及其被调用的方式来决定。

描述器是一个强大而通用的协议。 它们是特征属性、方法静态方法、类方法和 super() 背后的实现机制。 它们在 Python 内部被广泛使用来实现自 2.2 版中引入的新式类。 描述器简化了底层的 C 代码并为 Python 的日常程序提供了一组灵活的新工具。

描述器协议

descr.__get__(self, obj, type=None) --> value

descr.__set__(self, obj, value) --> None

descr.__delete__(self, obj) --> None

以上就是全部。定义这些方法中的任何一个的对象被视为描述器，并覆盖其默认行为。

如果一个对象同时定义了 __get__() 和 __set__()，则它会被视为数据描述器。 仅定义了 __get__() 的称为非数据描述器（它们通常被用于方法，但也可以有其他用途）。

数据和非数据描述器的不同之处在于，如何计算实例字典中条目的替代值。如果实例的字典具有与数据描述器同名的条目，则数据描述器优先。如果实例的字典具有与非数据描述器同名的条目，则该字典条目优先。

为了使只读数据描述符，同时定义 __get__() 和 __set__() ，并在 __set__() 中引发 AttributeError 。用引发异常的占位符定义 __set__() 方法能够使其成为数据描述符。

发起调用描述器

描述符可以通过其方法名称直接调用。例如， d.__get__(obj) 。

或者，更常见的是在属性访问时自动调用描述符。例如，在中 obj.d 会在 d 的字典中查找 obj 。如果 d 定义了方法 __get__() ，则 d.__get__(obj) 根据下面列出的优先级规则进行调用。

调用的细节取决于 obj 是对象还是类。

对于对象来说， object.__getattribute__() 中的机制是将 b.x 转换为 type(b).__dict__['x'].__get__(b, type(b)) 。 这个实现通过一个优先级链完成，该优先级链赋予数据描述器优先于实例变量的优先级，实例变量优先于非数据描述符的优先级，并如果 __getattr__() 方法存在，为其分配最低的优先级。 完整的C实现可在 Objects/object.c [https://github.com/python/cpython/tree/3.6/Objects/object.c] 中的 PyObject_GenericGetAttr() 找到。

对于类来说，机制是 type.__getattribute__() 中将 B.x 转换为 B.__dict__['x'].__get__(None, B) 。在纯Python中，它就像:

def __getattribute__(self, key):
    "Emulate type_getattro() in Objects/typeobject.c"
    v = object.__getattribute__(self, key)
    if hasattr(v, '__get__'):
        return v.__get__(None, self)
    return v

要记住的重要点是：

描述器由 __getattribute__() 方法调用

重写 __getattribute__() 会阻止描述器的自动调用

object.__getattribute__() 和 type.__getattribute__() 会用不同的方式调用 __get__().

数据描述符始终会覆盖实例字典。

非数据描述器会被实例字典覆盖。

super() 返回的对象还有一个自定义的 __getattribute__() 方法用来发起调用描述器。 调用 super(B, obj).m() 会搜索 obj.__class__.__mro__ 紧随 B 的基类 A，然后返回 A.__dict__['m'].__get__(obj, B)。 如果其不是描述器，则原样返回 m。 如果不在字典中，m 会转而使用 object.__getattribute__() 进行搜索。

这个实现的具体细节在 Objects/typeobject.c [https://github.com/python/cpython/tree/3.6/Objects/typeobject.c]. 的 super_getattro() 中，并且你还可以在 Guido’s Tutorial [https://www.python.org/download/releases/2.2.3/descrintro/#cooperation] 中找到等价的纯Python实现。

以上展示的关于描述器机制的细节嵌入在 object ， type ， 和 super() 中的 __getattribute__() 。当类派生自类 object 或有提供类似功能的元类时，它们将继承此机制。同样，类可以通过重写 __getattribute__() 阻止描述器调用。

描述符示例

以下代码创建一个类，其对象是数据描述器，该描述器为每个 get 或 set 打印一条消息。覆盖 __getattribute__() 是可以对每个属性执行此操作的替代方法。但是，此描述器对于跟踪仅几个选定的属性很有用：

class RevealAccess(object):
    """A data descriptor that sets and returns values
       normally and prints a message logging their access.
    """

    def __init__(self, initval=None, name='var'):
        self.val = initval
        self.name = name

    def __get__(self, obj, objtype):
        print('Retrieving', self.name)
        return self.val

    def __set__(self, obj, val):
        print('Updating', self.name)
        self.val = val

>>> class MyClass(object):
...     x = RevealAccess(10, 'var "x"')
...     y = 5
...
>>> m = MyClass()
>>> m.x
Retrieving var "x"
10
>>> m.x = 20
Updating var "x"
>>> m.x
Retrieving var "x"
20
>>> m.y
5

这个协议很简单，并提供了令人兴奋的可能性。有几种用例非常普遍，以至于它们被打包到单独的函数调用中。属性、绑定方法、静态方法和类方法均基于描述器协议。

属性

调用 property() 是构建数据描述器的简洁方式，该数据描述器在访问属性时触发函数调用。它的签名是：

property(fget=None, fset=None, fdel=None, doc=None) -> property attribute

该文档显示了定义托管属性 x 的典型用法:

class C(object):
    def getx(self): return self.__x
    def setx(self, value): self.__x = value
    def delx(self): del self.__x
    x = property(getx, setx, delx, "I'm the 'x' property.")

要了解 property() 如何根据描述符协议实现，这里是一个纯Python的等价实现：

class Property(object):
    "Emulate PyProperty_Type() in Objects/descrobject.c"

    def __init__(self, fget=None, fset=None, fdel=None, doc=None):
        self.fget = fget
        self.fset = fset
        self.fdel = fdel
        if doc is None and fget is not None:
            doc = fget.__doc__
        self.__doc__ = doc

    def __get__(self, obj, objtype=None):
        if obj is None:
            return self
        if self.fget is None:
            raise AttributeError("unreadable attribute")
        return self.fget(obj)

    def __set__(self, obj, value):
        if self.fset is None:
            raise AttributeError("can't set attribute")
        self.fset(obj, value)

    def __delete__(self, obj):
        if self.fdel is None:
            raise AttributeError("can't delete attribute")
        self.fdel(obj)

    def getter(self, fget):
        return type(self)(fget, self.fset, self.fdel, self.__doc__)

    def setter(self, fset):
        return type(self)(self.fget, fset, self.fdel, self.__doc__)

    def deleter(self, fdel):
        return type(self)(self.fget, self.fset, fdel, self.__doc__)

这个内置的 property() 每当用户访问属性时生效，随后的变化需要一个方法的参与。

例如，一个电子表格类可以通过 Cell('b10').value 授予对单元格值的访问权限。对程序的后续改进要求每次访问都要重新计算单元格；但是，程序员不希望影响直接访问该属性的现有客户端代码。解决方案是将对value属性的访问包装在属性数据描述器中:

class Cell(object):
    . . .
    def getvalue(self):
        "Recalculate the cell before returning value"
        self.recalc()
        return self._value
    value = property(getvalue)

函数和方法

Python 的面向对象功能是在基于函数的环境构建的。使用非数据描述符，两者完成了无缝融合。

类字典将方法存储为函数。在类定义中，方法是用 def 或 lambda 这两个创建函数的常用工具编写的。方法与常规函数的不同之处仅在于第一个参数是为对象实例保留的。按照 Python 约定，实例引用称为 self ，但也可以称为 this 或任何其他变量名称。

To support method calls, functions include the __get__() method for binding methods during attribute access. This means that all functions are non-data descriptors which return bound methods when they are invoked from an object. In pure python, it works like this:

class Function(object):
    . . .
    def __get__(self, obj, objtype=None):
        "Simulate func_descr_get() in Objects/funcobject.c"
        if obj is None:
            return self
        return types.MethodType(self, obj)

运行解释器显示了函数描述器在实践中的工作方式：

>>> class D(object):
...     def f(self, x):
...         return x
...
>>> d = D()

# Access through the class dictionary does not invoke __get__.
# It just returns the underlying function object.
>>> D.__dict__['f']
<function D.f at 0x00C45070>

# Dotted access from a class calls __get__() which just returns
# the underlying function unchanged.
>>> D.f
<function D.f at 0x00C45070>

# The function has a __qualname__ attribute to support introspection
>>> D.f.__qualname__
'D.f'

# Dotted access from an instance calls __get__() which returns the
# function wrapped in a bound method object
>>> d.f
<bound method D.f of <__main__.D object at 0x00B18C90>>

# Internally, the bound method stores the underlying function,
# the bound instance, and the class of the bound instance.
>>> d.f.__func__
<function D.f at 0x1012e5ae8>
>>> d.f.__self__
<__main__.D object at 0x1012e1f98>
>>> d.f.__class__
<class 'method'>

静态方法和类方法

非数据描述器为把函数绑定到方法的通常模式提供了一种简单的机制。

概括地说，函数具有 __get__() 方法，以便在作为属性访问时可以将其转换为方法。非数据描述符将 obj.f(*args) 的调用转换为 f(obj, *args) 。调用 klass.f(*args)` 因而变成 f(*args) 。

下表总结了绑定及其两个最有用的变体：

转换形式

通过对象调用

通过类调用

function – 函数

f(obj, *args)

f(*args)

静态方法

f(*args)

f(*args)

类方法

f(type(obj), *args)

f(klass, *args)

静态方法返回底层函数，不做任何更改。调用 c.f 或 C.f 等效于通过 object.__getattribute__(c, "f") 或 object.__getattribute__(C, "f") 查找。结果，该函数变得可以从对象或类中进行相同的访问。

适合于作为静态方法的是那些不引用 self 变量的方法。

例如，一个统计用的包可能包含一个实验数据的容器类。该容器类提供了用于计算数据的平均值，均值，中位数和其他描述性统计信息的常规方法。但是，可能有在概念上相关但不依赖于数据的函数。例如， erf(x) 是在统计中的便捷转换，但并不直接依赖于特定的数据集。可以从对象或类中调用它： s.erf(1.5) --> .9332 或 Sample.erf(1.5) --> .9332。

由于静态方法直接返回了底层的函数，因此示例调用是平淡的：

>>> class E(object):
...     def f(x):
...         print(x)
...     f = staticmethod(f)
...
>>> print(E.f(3))
3
>>> print(E().f(3))
3

使用非数据描述器，纯Python的版本 staticmethod() 如下所示：

class StaticMethod(object):
    "Emulate PyStaticMethod_Type() in Objects/funcobject.c"

    def __init__(self, f):
        self.f = f

    def __get__(self, obj, objtype=None):
        return self.f

与静态方法不同，类方法在调用函数之前将类引用放在参数列表的最前。无论调用方是对象还是类，此格式相同：

>>> class E(object):
...     def f(klass, x):
...         return klass.__name__, x
...     f = classmethod(f)
...
>>> print(E.f(3))
('E', 3)
>>> print(E().f(3))
('E', 3)

此行为适用于当函数仅需要使用类引用并且不关心任何底层数据时的情况。 类方法的一种用途是创建替代的类构造器。 在 Python 2.3 中，类方法 dict.fromkeys() 会从键列表中创建一个新的字典。 纯 Python 的等价形式是:

class Dict(object):
    . . .
    def fromkeys(klass, iterable, value=None):
        "Emulate dict_fromkeys() in Objects/dictobject.c"
        d = klass()
        for key in iterable:
            d[key] = value
        return d
    fromkeys = classmethod(fromkeys)

现在可以这样构造一个新的唯一键字典：

>>> Dict.fromkeys('abracadabra')
{'a': None, 'r': None, 'b': None, 'c': None, 'd': None}

使用非数据描述符协议，纯 Python 版本的 classmethod() 如下：

class ClassMethod(object):
    "Emulate PyClassMethod_Type() in Objects/funcobject.c"

    def __init__(self, f):
        self.f = f

    def __get__(self, obj, klass=None):
        if klass is None:
            klass = type(obj)
        def newfunc(*args):
            return self.f(klass, *args)
        return newfunc

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

函数式编程指引

作者

A. M. Kuchling

发布版本

0.32

本文档提供恰当的 Python 函数式编程范例，在函数式编程简单的介绍之后，将简单介绍Python中关于函数式编程的特性如 iterator 和 generator 以及相关库模块如 itertools 和 functools 等。

概述

本章介绍函数式编程的基本概念。如您仅想学习 Python 语言的特性，可跳过本章直接查看 迭代器.

编程语言支持通过以下几种方式来解构具体问题：

大多数的编程语言都是 过程式 的，所谓程序就是一连串告诉计算机怎样处理程序输入的指令。C、Pascal 甚至 Unix shells 都是过程式语言。

在 声明式 语言中，你编写一个用来描述待解决问题的说明，并且这个语言的具体实现会指明怎样高效的进行计算。 SQL 可能是你最熟悉的声明式语言了。 一个 SQL 查询语句描述了你想要检索的数据集，并且 SQL 引擎会决定是扫描整张表还是使用索引，应该先执行哪些子句等等。

面向对象 程序会操作一组对象。 对象拥有内部状态，并能够以某种方式支持请求和修改这个内部状态的方法。Smalltalk 和 Java 都是面向对象的语言。 C++ 和 Python 支持面向对象编程，但并不强制使用面向对象特性。

函数式 编程则将一个问题分解成一系列函数。 理想情况下，函数只接受输入并输出结果，对一个给定的输入也不会有影响输出的内部状态。 著名的函数式语言有 ML 家族（Standard ML，Ocaml 以及其他变种）和 Haskell。

一些语言的设计者选择强调一种特定的编程方式。 这通常会让以不同的方式来编写程序变得困难。其他多范式语言则支持几种不同的编程方式。Lisp，C++ 和 Python 都是多范式语言；使用这些语言，你可以编写主要为过程式，面向对象或者函数式的程序和函数库。在大型程序中，不同的部分可能会采用不同的方式编写；比如 GUI 可能是面向对象的而处理逻辑则是过程式或者函数式。

在函数式程序里，输入会流经一系列函数。每个函数接受输入并输出结果。函数式风格反对使用带有副作用的函数，这些副作用会修改内部状态，或者引起一些无法体现在函数的返回值中的变化。完全不产生副作用的函数被称作“纯函数”。消除副作用意味着不能使用随程序运行而更新的数据结构；每个函数的输出必须只依赖于输入。

一些语言对纯洁性要求非常严格，以至于没有像 a=3 或 c = a + b 这样的赋值表达式，但是完全消除副作用非常困难。 比如，显示在屏幕上或者写到磁盘文件中都是副作用。举个例子，在 Python 里，调用函数 print() 或者 time.sleep() 并不会返回有用的结果；它们的用途只在于副作用，向屏幕发送一段文字或暂停一秒钟。

函数式风格的 Python 程序并不会极端到消除所有 I/O 或者赋值的程度；相反，他们会提供像函数式一样的接口，但会在内部使用非函数式的特性。比如，函数的实现仍然会使用局部变量，但不会修改全局变量或者有其他副作用。

函数式编程可以被认为是面向对象编程的对立面。对象就像是颗小胶囊，包裹着内部状态和随之而来的能让你修改这个内部状态的一组调用方法，以及由正确的状态变化所构成的程序。函数式编程希望尽可能地消除状态变化，只和流经函数的数据打交道。在 Python 里你可以把两种编程方式结合起来，在你的应用（电子邮件信息，事务处理）中编写接受和返回对象实例的函数。

函数式设计在工作中看起来是个奇怪的约束。为什么你要消除对象和副作用呢？不过函数式风格有其理论和实践上的优点：

形式证明。

模块化。

组合性。

易于调试和测试。

形式证明

一个理论上的优点是，构造数学证明来说明函数式程序是正确的相对更容易些。

很长时间，研究者们对寻找证明程序正确的数学方法都很感兴趣。这和通过大量输入来测试，并得出程序的输出基本正确，或者阅读一个程序的源代码然后得出代码看起来没问题不同；相反，这里的目标是一个严格的证明，证明程序对所有可能的输入都能给出正确的结果。

证明程序正确性所用到的技术是写出 不变量，也就是对于输入数据和程序中的变量永远为真的特性。然后对每行代码，你说明这行代码执行前的不变量 X 和 Y 以及执行后稍有不同的不变量 X’ 和 Y’ 为真。如此一直到程序结束，这时候在程序的输出上，不变量应该会与期望的状态一致。

函数式编程之所以要消除赋值，是因为赋值在这个技术中难以处理；赋值可能会破坏赋值前为真的不变量，却并不产生任何可以传递下去的新的不变量。

不幸的是，证明程序的正确性很大程度上是经验性质的，而且和 Python 软件无关。即使是微不足道的程序都需要几页长的证明；一个中等复杂的程序的正确性证明会非常庞大，而且，极少甚至没有你日常所使用的程序（Python 解释器，XML 解析器，浏览器）的正确性能够被证明。即使你写出或者生成一个证明，验证证明也会是一个问题；里面可能出了差错，而你错误地相信你证明了程序的正确性。

模块化

函数式编程的一个更实用的优点是，它强制你把问题分解成小的方面。因此程序会更加模块化。相对于一个进行了复杂变换的大型函数，一个小的函数更明确，更易于编写, 也更易于阅读和检查错误。

易于调试和测试

测试和调试函数式程序相对来说更容易。

调试很简单是因为函数通常都很小而且清晰明确。当程序无法工作的时候，每个函数都是一个可以检查数据是否正确的接入点。你可以通过查看中间输入和输出迅速找到出错的函数。

测试更容易是因为每个函数都是单元测试的潜在目标。在执行测试前，函数并不依赖于需要重现的系统状态；相反，你只需要给出正确的输入，然后检查输出是否和期望的结果一致。

组合性

当你编写函数式风格的程序时，你会写出很多带有不同输入和输出的函数。其中一些不可避免地会局限于特定的应用，但其他的却可以广泛的用在程序中。举例来说，一个接受文件夹目录返回所有文件夹中的 XML 文件的函数； 或是一个接受文件名，然后返回文件内容的函数，都可以应用在很多不同的场合。

久而久之你会形成一个个人工具库。通常你可以重新组织已有的函数来组成新的程序，然后为当前的工作写一些特殊的函数。

迭代器

我会从 Python 的一个语言特性， 编写函数式风格程序的重要基石开始说起：迭代器。

迭代器是一个表示数据流的对象；这个对象每次只返回一个元素。Python 迭代器必须支持 __next__() 方法；这个方法不接受参数，并总是返回数据流中的下一个元素。如果数据流中没有元素，__next__() 会抛出 StopIteration 异常。迭代器未必是有限的；完全有理由构造一个输出无限数据流的迭代器。

内置的 iter() 函数接受任意对象并试图返回一个迭代器来输出对象的内容或元素，并会在对象不支持迭代的时候抛出 TypeError 异常。Python 有几种内置数据类型支持迭代，最常见的就是列表和字典。如果一个对象能生成迭代器，那么它就会被称作 iterable。

你可以手动试验迭代器的接口。

>>> L = [1, 2, 3]
>>> it = iter(L)
>>> it
<...iterator object at ...>
>>> it.__next__()  # same as next(it)
1
>>> next(it)
2
>>> next(it)
3
>>> next(it)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>>

Python 有不少要求使用可迭代的对象的地方，其中最重要的就是 for 表达式。在表达式 for X in Y，Y 要么自身是一个迭代器，要么能够由 iter() 创建一个迭代器。以下两种表达是等价的:

for i in iter(obj):
    print(i)

for i in obj:
    print(i)

可以用 list() 或 tuple() 这样的构造函数把迭代器具体化成列表或元组:

>>> L = [1, 2, 3]
>>> iterator = iter(L)
>>> t = tuple(iterator)
>>> t
(1, 2, 3)

序列的解压操作也支持迭代器：如果你知道一个迭代器能够返回 N 个元素，你可以把他们解压到有 N 个元素的元组:

>>> L = [1, 2, 3]
>>> iterator = iter(L)
>>> a, b, c = iterator
>>> a, b, c
(1, 2, 3)

像 max() 和 min() 这样的内置函数可以接受单个迭代器参数，然后返回其中最大或者最小的元素。"in" 和 "not in" 操作也支持迭代器：如果能够在迭代器 iterator 返回的数据流中找到 X 的话，则``X in iterator`` 为真。很显然，如果迭代器是无限的，这么做你就会遇到问题；max() 和 min() 永远也不会返回；如果元素 X 也不出现在数据流中，"in" 和 "not in" 操作同样也永远不会返回。

注意你只能在迭代器中顺序前进；没有获取前一个元素的方法，除非重置迭代器，或者重新复制一份。迭代器对象可以提供这些额外的功能，但迭代器协议只明确了 __next__() 方法。函数可能因此而耗尽迭代器的输出，如果你要对同样的数据流做不同的操作，你必须重新创建一个迭代器。

支持迭代器的数据类型

我们已经知道列表和元组支持迭代器。实际上，Python 中的任何序列类型，比如字符串，都自动支持创建迭代器。

对字典调用 iter() 会返回一个遍历字典的键的迭代器:

>>> m = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
...      'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
>>> for key in m:
...     print(key, m[key])
Mar 3
Feb 2
Aug 8
Sep 9
Apr 4
Jun 6
Jul 7
Jan 1
May 5
Nov 11
Dec 12
Oct 10

Note that the order is essentially random, because it’s based on the hash ordering of the objects in the dictionary.

对字典使用 iter() 总是会遍历键，但字典也有返回其他迭代器的方法。如果你只遍历值或者键/值对，你可以明确地调用 values() 或 items() 方法得到合适的迭代器。

dict() 构造函数可以接受一个迭代器，然后返回一个有限的 (key, value) 元组的数据流:

>>> L = [('Italy', 'Rome'), ('France', 'Paris'), ('US', 'Washington DC')]
>>> dict(iter(L))
{'Italy': 'Rome', 'US': 'Washington DC', 'France': 'Paris'}

文件也可以通过调用 readline() 来遍历，直到穷尽文件中所有的行。这意味着你可以像这样读取文件中的每一行:

for line in file:
    # do something for each line
    ...

集合可以从可遍历的对象获取内容，也可以让你遍历集合的元素:

S = {2, 3, 5, 7, 11, 13}
for i in S:
    print(i)

生成器表达式和列表推导式

迭代器的输出有两个很常见的使用方式，1) 对每一个元素执行操作，2) 选择一个符合条件的元素子集。比如，给定一个字符串列表，你可能想去掉每个字符串尾部的空白字符，或是选出所有包含给定子串的字符串。

列表推导式和生成器表达时（简写：”listcomps” 和 “genexps”）让这些操作更加简明，这个形式借鉴自函数式程序语言 Haskell（https://www.haskell.org/）。你可以用以下代码去掉一个字符串流中的所有空白字符:

line_list = ['  line 1\n', 'line 2  \n', ...]

# Generator expression -- returns iterator
stripped_iter = (line.strip() for line in line_list)

# List comprehension -- returns list
stripped_list = [line.strip() for line in line_list]

你可以加上条件语句 "if" 来选取特定的元素:

stripped_list = [line.strip() for line in line_list
                 if line != ""]

通过列表推导式，你会获得一个 Python 列表；stripped_list 就是一个包含所有结果行的列表，并不是迭代器。 生成器表达式会返回一个迭代器，它在必要的时候计算结果，避免一次性生成所有的值。 这意味着，如果迭代器返回一个无限数据流或者大量的数据，列表推导式就不太好用了。 这种情况下生成器表达式会更受青睐。

生成器表达式两边使用圆括号 (“()”) ，而列表推导式则使用方括号 (“[]”)。生成器表达式的形式为:

( expression for expr in sequence1
             if condition1
             for expr2 in sequence2
             if condition2
             for expr3 in sequence3 ...
             if condition3
             for exprN in sequenceN
             if conditionN )

再次说明，列表推导式只有两边的括号不一样（方括号而不是圆括号）。

这些生成用于输出的元素会成为 expression 的后继值。其中 if 语句是可选的；如果给定的话 expression 只会在符合条件时计算并加入到结果中。

生成器表达式总是写在圆括号里面，不过也可以算上调用函数时用的括号。如果你想即时创建一个传递给函数的迭代器，可以这么写:

obj_total = sum(obj.count for obj in list_all_objects())

其中 for...in 语句包含了将要遍历的序列。这些序列并不必须同样长，因为它们会从左往右开始遍历，而 不是 同时执行。对每个 sequence1 中的元素，sequence2 会从头开始遍历。sequence3 会对每个 sequence1 和 sequence2 的元素对开始遍历。

换句话说，列表推导式器是和下面的 Python 代码等价:

for expr1 in sequence1:
    if not (condition1):
        continue   # Skip this element
    for expr2 in sequence2:
        if not (condition2):
            continue   # Skip this element
        ...
        for exprN in sequenceN:
            if not (conditionN):
                continue   # Skip this element

            # Output the value of
            # the expression.

这说明，如果有多个 for...in 语句而没有 if 语句，输出结果的长度就是所有序列长度的乘积。如果你的两个列表长度为3，那么输出的列表长度就是9:

>>> seq1 = 'abc'
>>> seq2 = (1, 2, 3)
>>> [(x, y) for x in seq1 for y in seq2]
[('a', 1), ('a', 2), ('a', 3),
 ('b', 1), ('b', 2), ('b', 3),
 ('c', 1), ('c', 2), ('c', 3)]

为了不让 Python 语法变得含糊，如果 expression 会生成元组，那这个元组必须要用括号括起来。下面第一个列表推导式语法错误，第二个则是正确的:

# Syntax error
[x, y for x in seq1 for y in seq2]
# Correct
[(x, y) for x in seq1 for y in seq2]

生成器

生成器是一类用来简化编写迭代器工作的特殊函数。普通的函数计算并返回一个值，而生成器返回一个能返回数据流的迭代器。

毫无疑问，你已经对如何在 Python 和 C 中调用普通函数很熟悉了，这时候函数会获得一个创建局部变量的私有命名空间。当函数到达 return 表达式时，局部变量会被销毁然后把返回给调用者。之后调用同样的函数时会创建一个新的私有命名空间和一组全新的局部变量。但是，如果在退出一个函数时不扔掉局部变量会如何呢？如果稍后你能够从退出函数的地方重新恢复又如何呢？这就是生成器所提供的；他们可以被看成可恢复的函数。

这里有简单的生成器函数示例:

>>> def generate_ints(N):
...    for i in range(N):
...        yield i

任何包含了 yield 关键字的函数都是生成器函数；Python 的 bytecode 编译器会在编译的时候检测到并因此而特殊处理。

当你调用一个生成器函数，它并不会返回单独的值，而是返回一个支持生成器协议的生成器对象。当执行 yield 表达式时，生成器会输出 i 的值，就像 return 表达式一样。yield 和 return 最大的区别在于，到达 yield 的时候生成器的执行状态会挂起并保留局部变量。在下一次调用生成器 __next__() 方法的时候，函数会恢复执行。

这里有一个 generate_ints() 生成器的示例:

>>> gen = generate_ints(3)
>>> gen
<generator object generate_ints at ...>
>>> next(gen)
0
>>> next(gen)
1
>>> next(gen)
2
>>> next(gen)
Traceback (most recent call last):
  File "stdin", line 1, in <module>
  File "stdin", line 2, in generate_ints
StopIteration

同样，你可以写出 for i in generate_ints(5)，或者 a, b, c = generate_ints(3)。

在生成器函数里面，return value 会触发从 __next__() 方法抛出 StopIteration(value) 异常。一旦抛出这个异常，或者函数结束，处理数据的过程就会停止，生成器也不会再生成新的值。

你可以手动编写自己的类来达到生成器的效果，把生成器的所有局部变量作为实例的成员变量存储起来。比如，可以这么返回一个整数列表：把 self.count 设为0，然后通过 count`()。然而，对于一个中等复杂程度的生成器，写出一个相应的类可能会相当繁杂。

包含在 Python 库中的测试套件 Lib/test/test_generators.py [https://github.com/python/cpython/tree/3.6/Lib/test/test_generators.py] 里有很多非常有趣的例子。这里是一个用生成器实现树的递归中序遍历示例。:

# A recursive generator that generates Tree leaves in in-order.
def inorder(t):
    if t:
        for x in inorder(t.left):
            yield x

        yield t.label

        for x in inorder(t.right):
            yield x

另外两个 test_generators.py 中的例子给出了 N 皇后问题（在 NxN 的棋盘上放置 N 个皇后，任何一个都不能吃掉另一个），以及马的遍历路线（在NxN 的棋盘上给马找出一条不重复的走过所有格子的路线）的解。

向生成器传递值

在 Python 2.4 及之前的版本中，生成器只产生输出。一旦调用生成器的代码创建一个迭代器，就没有办法在函数恢复执行的时候向它传递新的信息。你可以设法实现这个功能，让生成器引用一个全局变量或者一个调用者可以修改的可变对象，但是这些方法都很繁杂。

在 Python 2.5 里有一个简单的将值传递给生成器的方法。yield 变成了一个表达式，返回一个可以赋给变量或执行操作的值:

val = (yield i)

我建议你在处理 yield 表达式返回值的时候， 总是 两边写上括号，就像上面的例子一样。括号并不总是必须的，但是比起记住什么时候需要括号，写出来会更容易一点。

（PEP 342 [https://www.python.org/dev/peps/pep-0342] 解释了具体的规则，也就是 yield 表达式必须括起来，除非是出现在最顶级的赋值表达式的右边。这意味着你可以写 val = yield i，但是必须在操作的时候加上括号，就像``val = (yield i) + 12``）

可以调用 send(value)() <generator.send> 方法向生成器发送值。这个方法会恢复执行生成器的代码，然后 yield 表达式返回特定的值。如果调用普通的 __next__`方法，``yield`() 会返回 None.

这里有一个简单的每次加1的计数器，并允许改变内部计数器的值。

def counter(maximum):
    i = 0
    while i < maximum:
        val = (yield i)
        # If value provided, change counter
        if val is not None:
            i = val
        else:
            i += 1

这是改变计数器的一个示例

>>> it = counter(10)
>>> next(it)
0
>>> next(it)
1
>>> it.send(8)
8
>>> next(it)
9
>>> next(it)
Traceback (most recent call last):
  File "t.py", line 15, in <module>
    it.next()
StopIteration

因为 yield 很多时候会返回 None，所以你应该总是检查这个情况。不要在表达式中使用 yield 的值，除非你确定 send() 是唯一的用来恢复你的生成器函数的方法。

除了 send() 之外，生成器还有两个其他的方法:

throw(type, value=None, traceback=None) 用于在生成器内部抛出异常；这个异常会在生成器暂停执行的时候由 yield 表达式抛出。

generator.close() 会在生成器内部抛出 GeneratorExit 异常来结束迭代。当接收到这个异常时，生成器的代码会抛出 GeneratorExit 或者 StopIteration；捕捉这个异常作其他处理是非法的，并会出发 RuntimeError。close() 也会在 Python 垃圾回收器回收生成器的时候调用。

如果你要在 GeneratorExit 发生的时候清理代码，我建议使用 try: ... finally: 组合来代替 GeneratorExit。

这些改变的累积效应是，让生成器从单向的信息生产者变成了既是生产者，又是消费者。

生成器也可以成为 协程 ，一种更广义的子过程形式。子过程可以从一个地方进入，然后从另一个地方退出（从函数的顶端进入，从 return 语句退出），而协程可以进入，退出，然后在很多不同的地方恢复（yield 语句）。

内置函数

我们可以看看迭代器常常用到的函数的更多细节。

Python 内置的两个函数 map() 和 filter() 复制了生成器表达式的两个特性:

map(f, iterA, iterB, ...) 返回一个遍历序列的迭代器

f(iterA[0], iterB[0]), f(iterA[1], iterB[1]), f(iterA[2], iterB[2]), ....

>>> def upper(s):
...     return s.upper()

>>> list(map(upper, ['sentence', 'fragment']))
['SENTENCE', 'FRAGMENT']
>>> [upper(s) for s in ['sentence', 'fragment']]
['SENTENCE', 'FRAGMENT']

你当然也可以用列表推导式达到同样的效果。

filter(predicate, iter) 返回一个遍历序列中满足指定条件的元素的迭代器，和列表推导式的功能相似。 predicate （谓词）是一个在特定条件下返回真值的函数；要使用函数 filter()，谓词函数必须只能接受一个参数。

>>> def is_even(x):
...     return (x % 2) == 0

>>> list(filter(is_even, range(10)))
[0, 2, 4, 6, 8]

这也可以写成列表推导式:

>>> list(x for x in range(10) if is_even(x))
[0, 2, 4, 6, 8]

enumerate(iter, start=0) 计数可迭代对象中的元素，然后返回包含每个计数（从 start 开始）和元素两个值的元组。:

>>> for item in enumerate(['subject', 'verb', 'object']):
...     print(item)
(0, 'subject')
(1, 'verb')
(2, 'object')

enumerate() 常常用于遍历列表并记录达到特定条件时的下标:

f = open('data.txt', 'r')
for i, line in enumerate(f):
    if line.strip() == '':
        print('Blank line at line #%i' % i)

sorted(iterable, key=None, reverse=False) 会将 iterable 中的元素收集到一个列表中，然后排序并返回结果。其中 key 和 reverse 参数会传递给所创建列表的 sort() 方法。:

>>> import random
>>> # Generate 8 random numbers between [0, 10000)
>>> rand_list = random.sample(range(10000), 8)
>>> rand_list
[769, 7953, 9828, 6431, 8442, 9878, 6213, 2207]
>>> sorted(rand_list)
[769, 2207, 6213, 6431, 7953, 8442, 9828, 9878]
>>> sorted(rand_list, reverse=True)
[9878, 9828, 8442, 7953, 6431, 6213, 2207, 769]

（对排序更详细的讨论可参见 排序指南。）

内置函数 any(iter) 和 all(iter) 会查看一个可迭代对象内容的逻辑值。any() 在可迭代对象中任意一个元素为真时返回 True，而 all() 在所有元素为真时返回 True:

>>> any([0, 1, 0])
True
>>> any([0, 0, 0])
False
>>> any([1, 1, 1])
True
>>> all([0, 1, 0])
False
>>> all([0, 0, 0])
False
>>> all([1, 1, 1])
True

zip(iterA, iterB, ...) 从每个可迭代对象中选取单个元素组成列表并返回:

zip(['a', 'b', 'c'], (1, 2, 3)) =>
  ('a', 1), ('b', 2), ('c', 3)

它并不会在内存创建一个列表并因此在返回前而耗尽输入的迭代器；相反，只有在被请求的时候元组才会创建并返回。（这种行为的技术术语叫惰性计算，参见 lazy evaluation [https://en.wikipedia.org/wiki/Lazy_evaluation].）

这个迭代器设计用于长度相同的可迭代对象。如果可迭代对象的长度不一致，返回的数据流的长度会和最短的可迭代对象相同

zip(['a', 'b'], (1, 2, 3)) =>
  ('a', 1), ('b', 2)

然而，你应该避免这种情况，因为所有从更长的迭代器中取出的元素都会被丢弃。这意味着之后你也无法冒着跳过被丢弃元素的风险来继续使用这个迭代器。

itertools 模块

itertools 模块包含很多常用的迭代器以及用来组合迭代器的函数。本节会用些小的例子来介绍这个模块的内容。

这个模块里的函数大致可以分为几类：

从已有的迭代器创建新的迭代器的函数。

接受迭代器元素作为参数的函数。

选取部分迭代器输出的函数。

给迭代器输出分组的函数。

创建新的迭代器

itertools.count(start, step) 返回一个等分的无限数据流。初始值默认为0，间隔默认为1，你也选择可以指定初始值和间隔:

itertools.count() =>
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...
itertools.count(10) =>
  10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ...
itertools.count(10, 5) =>
  10, 15, 20, 25, 30, 35, 40, 45, 50, 55, ...

itertools.cycle(iter) 保存一份所提供的可迭代对象的副本，并返回一个能产生整个可迭代对象序列的新迭代器。新迭代器会无限重复这些元素。:

itertools.cycle([1, 2, 3, 4, 5]) =>
  1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...

itertools.repeat(elem, [n]) 返回 n 次所提供的元素，当 n 不存在时，返回无数次所提供的元素。

itertools.repeat('abc') =>
  abc, abc, abc, abc, abc, abc, abc, abc, abc, abc, ...
itertools.repeat('abc', 5) =>
  abc, abc, abc, abc, abc

itertools.chain(iterA, iterB, ...) 接受任意数量的可迭代对象作为输入，首先返回第一个迭代器的所有元素，然后是第二个的所有元素，如此一直进行下去，直到消耗掉所有输入的可迭代对象。

itertools.chain(['a', 'b', 'c'], (1, 2, 3)) =>
  a, b, c, 1, 2, 3

itertools.islice(iter, [start], stop, [step]) 返回一个所输入的迭代器切片的数据流。如果只单独给定 stop 参数的话，它会返回从起始算起 stop 个数量的元素。如果你提供了起始下标 start，你会得到 stop-start 个元素；如果你给定了 step 参数，数据流会跳过相应的元素。和 Python 里的字符串和列表切片不同，你不能在 start, stop 或者 step 这些参数中使用负数。:

itertools.islice(range(10), 8) =>
  0, 1, 2, 3, 4, 5, 6, 7
itertools.islice(range(10), 2, 8) =>
  2, 3, 4, 5, 6, 7
itertools.islice(range(10), 2, 8, 2) =>
  2, 4, 6

itertools.tee(iter, [n]) 可以复制一个迭代器；它返回 n 个能够返回源迭代器内容的独立迭代器。如果你不提供参数 n，默认值为 2。复制迭代器需要保存源迭代器的一部分内容，因此在源迭代器比较大的时候会显著地占用内存；同时，在所有新迭代器中，有一个迭代器会比其他迭代器占用更多的内存。

itertools.tee( itertools.count() ) =>
   iterA, iterB

where iterA ->
   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...

and   iterB ->
   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...

对元素使用函数

operator 模块包含一组对应于 Python 操作符的函数。比如 operator.add(a, b) （把两个数加起来），operator.ne(a, b) （和 a != b 相同），以及 operator.attrgetter('id') （返回获取 .id 属性的可调用对象）。

itertools.starmap(func, iter) 假定可迭代对象能够返回一个元组的流，并且利用这些元组作为参数来调用 func:

itertools.starmap(os.path.join,
                  [('/bin', 'python'), ('/usr', 'bin', 'java'),
                   ('/usr', 'bin', 'perl'), ('/usr', 'bin', 'ruby')])
=>
  /bin/python, /usr/bin/java, /usr/bin/perl, /usr/bin/ruby

选择元素

另外一系列函数根据谓词选取一个迭代器中元素的子集。

itertools.filterfalse(predicate, iter) 和 filter() 相反，返回所有让 predicate 返回 false 的元素:

itertools.filterfalse(is_even, itertools.count()) =>
  1, 3, 5, 7, 9, 11, 13, 15, ...

itertools.takewhile(predicate, iter) 返回一直让 predicate 返回 true 的元素。一旦 predicate 返回 false，迭代器就会发出终止结果的信号。:

def less_than_10(x):
    return x < 10

itertools.takewhile(less_than_10, itertools.count()) =>
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9

itertools.takewhile(is_even, itertools.count()) =>
  0

itertools.dropwhile(predicate, iter) 在 predicate 返回 true 的时候丢弃元素，并且返回可迭代对象的剩余结果。:

itertools.dropwhile(less_than_10, itertools.count()) =>
  10, 11, 12, 13, 14, 15, 16, 17, 18, 19, ...

itertools.dropwhile(is_even, itertools.count()) =>
  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...

itertools.compress(data, selectors) 接受两个迭代器，然后返回 data 中使相应地 selector 中的元素为真的元素；它会在任一个迭代器耗尽的时候停止:

itertools.compress([1, 2, 3, 4, 5], [True, True, False, False, True]) =>
   1, 2, 5

组合函数

itertools.combinations(iterable, r) 返回一个迭代器，它能给出输入迭代器中所包含的元素的所有可能的 r 元元组的组合。:

itertools.combinations([1, 2, 3, 4, 5], 2) =>
  (1, 2), (1, 3), (1, 4), (1, 5),
  (2, 3), (2, 4), (2, 5),
  (3, 4), (3, 5),
  (4, 5)

itertools.combinations([1, 2, 3, 4, 5], 3) =>
  (1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (1, 4, 5),
  (2, 3, 4), (2, 3, 5), (2, 4, 5),
  (3, 4, 5)

每个元组中的元素保持着 可迭代对象 返回他们的顺序。例如，在上面的例子中数字 1 总是会在 2, 3, 4 或 5 前面。一个类似的函数，itertools.permutations(iterable, r=None)，取消了保持顺序的限制，返回所有可能的长度为 r 的排列:

itertools.permutations([1, 2, 3, 4, 5], 2) =>
  (1, 2), (1, 3), (1, 4), (1, 5),
  (2, 1), (2, 3), (2, 4), (2, 5),
  (3, 1), (3, 2), (3, 4), (3, 5),
  (4, 1), (4, 2), (4, 3), (4, 5),
  (5, 1), (5, 2), (5, 3), (5, 4)

itertools.permutations([1, 2, 3, 4, 5]) =>
  (1, 2, 3, 4, 5), (1, 2, 3, 5, 4), (1, 2, 4, 3, 5),
  ...
  (5, 4, 3, 2, 1)

如果你不提供 r 参数的值，它会使用可迭代对象的长度，也就是说会排列所有的元素。

注意这些函数会输出所有可能的位置组合，并不要求 可迭代对象 的内容不重复:

itertools.permutations('aba', 3) =>
  ('a', 'b', 'a'), ('a', 'a', 'b'), ('b', 'a', 'a'),
  ('b', 'a', 'a'), ('a', 'a', 'b'), ('a', 'b', 'a')

同一个元组 ('a', 'a', 'b') 出现了两次，但是两个 ‘a’ 字符来自不同的位置。

itertools.combinations_with_replacement(iterable, r) 函数放松了一个不同的限制：元组中的元素可以重复。从概念讲，为每个元组第一个位置选取一个元素，然后在选择第二个元素前替换掉它。:

itertools.combinations_with_replacement([1, 2, 3, 4, 5], 2) =>
  (1, 1), (1, 2), (1, 3), (1, 4), (1, 5),
  (2, 2), (2, 3), (2, 4), (2, 5),
  (3, 3), (3, 4), (3, 5),
  (4, 4), (4, 5),
  (5, 5)

为元素分组

我要讨论的最后一个函数，itertools.groupby(iter,key_func=None)，是最复杂的函数。 key_func(elem) 是一个可以对迭代器返回的每个元素计算键值的函数。 如果你不提供这个键值函数，它就会简化成每个元素自身。

groupby() 从所依据的可迭代对象中连续地收集具有相同值的元素，然后返回一个长度为2的元组的数据流, 每个元组包含键值以及对应这个键值的元素所组成的迭代器。

city_list = [('Decatur', 'AL'), ('Huntsville', 'AL'), ('Selma', 'AL'),
             ('Anchorage', 'AK'), ('Nome', 'AK'),
             ('Flagstaff', 'AZ'), ('Phoenix', 'AZ'), ('Tucson', 'AZ'),
             ...
            ]

def get_state(city_state):
    return city_state[1]

itertools.groupby(city_list, get_state) =>
  ('AL', iterator-1),
  ('AK', iterator-2),
  ('AZ', iterator-3), ...

where
iterator-1 =>
  ('Decatur', 'AL'), ('Huntsville', 'AL'), ('Selma', 'AL')
iterator-2 =>
  ('Anchorage', 'AK'), ('Nome', 'AK')
iterator-3 =>
  ('Flagstaff', 'AZ'), ('Phoenix', 'AZ'), ('Tucson', 'AZ')

groupby() 假定了所依据的可迭代对象的内容已经根据键值排序。注意，返回的迭代器也会使用所依据的可迭代对象，所以在请求迭代器 2和相应的键之前你必须先消耗迭代器 1 的结果。

functools 模块

Python 2.5 中的 functools 模块包含了一些高阶函数。 高阶函数 接受一个或多个函数作为输入，返回新的函数。 这个模块中最有用的工具是 functools.partial() 函数。

对于用函数式风格编写的程序，有时你会希望通过给定部分参数，将已有的函数构变形称新的函数。考虑一个 Python 函数 f(a, b, c)；你希望创建一个和 f(1, b, c) 等价的新函数 g(b, c)；也就是说你给定了 f() 的一个参数的值。这就是所谓的“部分函数应用”。

partial() 接受参数 (function, arg1, arg2, ..., kwarg1=value1, kwarg2=value2)。它会返回一个可调用的对象，所以你能够直接调用这个结果以使用给定参数的 function。

这里有一个很小但很现实的例子:

import functools

def log(message, subsystem):
    """Write the contents of 'message' to the specified subsystem."""
    print('%s: %s' % (subsystem, message))
    ...

server_log = functools.partial(log, subsystem='server')
server_log('Unable to open socket')

functools.reduce(func, iter, [initial_value]) 持续地在可迭代对象的所有元素上执行操作，因此它不能够用在无限的可迭代对象上。func 必须是一个接受两个元素并返回一个值的函数。functools.reduce() 接受迭代器返回的前两个元素 A 和 B 并计算 func(A, B) 。然后它会请求第三个元素，C，计算 func(func(A, B), C)，然后把这个结果再和第四个元素组合并返回，如此继续下去直到消耗整个可迭代对象。如果输入的可迭代对象完全不返回任何值，TypeError 异常就会抛出。如果提供了初值(initial value)，它会被用作起始值，也就是先计算 func(initial_value, A) 。:

>>> import operator, functools
>>> functools.reduce(operator.concat, ['A', 'BB', 'C'])
'ABBC'
>>> functools.reduce(operator.concat, [])
Traceback (most recent call last):
  ...
TypeError: reduce() of empty sequence with no initial value
>>> functools.reduce(operator.mul, [1, 2, 3], 1)
6
>>> functools.reduce(operator.mul, [], 1)
1

如果你在 functools.reduce() 中使用 operator.add()，你就会把可迭代对象中的所有元素加起来.这种情况非常常见, 所以 Python 有一个特殊的内置函数 sum():

>>> import functools, operator
>>> functools.reduce(operator.add, [1, 2, 3, 4], 0)
10
>>> sum([1, 2, 3, 4])
10
>>> sum([])
0

不过, 对于很多使用 functools.reduce() 的情形, 使用明显的 for 循环会更清晰:

import functools
# Instead of:
product = functools.reduce(operator.mul, [1, 2, 3], 1)

# You can write:
product = 1
for i in [1, 2, 3]:
    product *= i

一个相关的函数是 itertools.accumulate(iterable, func=operator.add) 。它执行同样的计算, 不过相对于只返回最终结果，accumulate() 会返回一个迭代器来输出所有中间结果:

itertools.accumulate([1, 2, 3, 4, 5]) =>
  1, 3, 6, 10, 15

itertools.accumulate([1, 2, 3, 4, 5], operator.mul) =>
  1, 2, 6, 24, 120

operator 模块

前面已经提到了 operator 模块。它包含一系列对应于 Python 操作符的函数。在函数式风格的代码中，这些函数通常很有用，可以帮你省下不少时间，避免写一些琐碎的仅仅执行一个简单操作的函数。

这个模块里的一些函数：

数学运算： add()，sub()，mul()，floordiv()，abs()， …

逻辑运算： not_()，truth()。

位运算： and_()，or_()，invert()。

比较： eq()，ne()，lt()，le()，gt()，和 ge()。

确认对象： is_()，is_not()。

全部函数列表可以参考 operator 模块的文档。

小函数和 lambda 表达式

编写函数式风格程序时，你会经常需要很小的函数，作为谓词函数或者以某种方式来组合元素。

如果合适的 Python 内置的或者其他模块中的函数，你就一点也不需要定义新的函数:

stripped_lines = [line.strip() for line in lines]
existing_files = filter(os.path.exists, file_list)

If the function you need doesn’t exist, you need to write it. One way to write small functions is to use the lambda statement. lambda takes a number of parameters and an expression combining these parameters, and creates an anonymous function that returns the value of the expression:

adder = lambda x, y: x+y

print_assign = lambda name, value: name + '=' + str(value)

另一种替代方案就是通常的使用 def 语句来定义函数:

def adder(x, y):
    return x + y

def print_assign(name, value):
    return name + '=' + str(value)

哪一种更受青睐呢？这是一个风格问题；我通常的做法是避免使用 lambda。

我这么偏好的一个原因是，lambda 能够定义的函数非常受限。函数的结果必须能够作为单独的表达式来计算，这意味着你不能使用多路 if... elif... else 比较，或者 try... except 语句。如果你尝试在 lambda 语句中做太多事情，你最终会把表达式过于复杂以至于难以阅读。你能快速的说出下面的代码做了什么事情吗？:

import functools
total = functools.reduce(lambda a, b: (0, a[1] + b[1]), items)[1]

你可以弄明白，不过要花上时间来理清表达式来搞清楚发生了什么。使用一个简短的嵌套的 def 语句可以让情况变得更好:

import functools
def combine(a, b):
    return 0, a[1] + b[1]

total = functools.reduce(combine, items)[1]

如果我仅仅使用一个 for 循环会更好:

total = 0
for a, b in items:
    total += b

或者使用内置的 sum() 和一个生成器表达式:

total = sum(b for a, b in items)

许多使用 functools.reduce() 的情形可以更清晰地写成 for 循环的形式。

Fredrik Lundh 曾经建议以下一组规则来重构 lambda 的使用:

写一个 lambda 函数。

写一句注释来说明这个 lambda 究竟干了什么。

研究一会这个注释，然后想出一个抓住注释本质的名字。

用这个名字，把这个 lambda 改写成 def 语句。

把注释去掉。

我非常喜欢这些规则，不过你完全有权利争辩这种消除 lambda 的风格是不是更好。

修订记录和致谢

作者要感谢以下人员对本文各种草稿给予的建议，更正和协助：Ian Bicking,Nick Coghlan, Nick Efford, Raymond Hettinger, Jim Jewett, Mike Krell,Leandro Lameiro, Jussi Salmela, Collin Winter, Blake Winton。

0.1 版: 2006 年 6 月 30 日发布。

0.11 版: 2006 年 7 月 1 日发布。 修正拼写错误。

0.2 版: 2006 年 7 月 10 日发布。 将 genexp 与 listcomp 两节合二为一。 修正拼写错误。

0.21 版: 加入了 tutor 邮件列表中建议的更多参考文件。

0.30 版: 添加了有关 functional 模块的小节，由 Collin Winter 撰写；添加了有关 operator 模块的简短小节；其他少量修改。

参考文献

通用文献

Structure and Interpretation of Computer Programs, Harold Abelson, Gerald Jay Sussman 和 Julie Sussman 著。全文可见 https://mitpress.mit.edu/sicp/ 。在这部计算机科学的经典教科书中，第二和第三章讨论了使用序列和流来组织程序内部的数据传递。书中的示例采用 Scheme 语言，但其中这些章节中描述的很多设计方法同样适用于函数式风格的 Python 代码。

http://www.defmacro.org/ramblings/fp.html: 一个使用 Java 示例的函数式编程的总体介绍，有很长的历史说明。

https://en.wikipedia.org/wiki/Functional_programming: 一般性的函数式编程的 Wikipedia 条目。

https://en.wikipedia.org/wiki/Coroutine: 协程条目。

https://en.wikipedia.org/wiki/Currying: 函数柯里化条目。

Python 相关

http://gnosis.cx/TPiP/：David Mertz 书中的第一章 Text Processing in Python，”Utilizing Higher-Order Functions in Text Processing” 标题部分讨论了文本处理的函数式编程。

Mertz also wrote a 3-part series of articles on functional programming for IBM’s DeveloperWorks site; see part 1 [https://www.ibm.com/developerworks/linux/library/l-prog/index.html], part 2 [https://www.ibm.com/developerworks/linux/library/l-prog2/index.html], and part 3 [https://www.ibm.com/developerworks/linux/library/l-prog3/index.html],

Python 文档

itertools 模块文档。

functools 模块文档。

operator 模块文档。

PEP 289 [https://www.python.org/dev/peps/pep-0289]: “Generator Expressions”

PEP 342 [https://www.python.org/dev/peps/pep-0342]: “Coroutines via Enhanced Generators” 描述了 Python 2.5 中新的生成器特性。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

日志 HOWTO

作者

Vinay Sajip <vinay_sajip at red-dove dot com>

日志基础教程

日志是对软件执行时所发生事件的一种追踪方式。软件开发人员对他们的代码添加日志调用，借此来指示某事件的发生。一个事件通过一些包含变量数据的描述信息来描述（比如：每个事件发生时的数据都是不同的）。开发者还会区分事件的重要性，重要性也被称为 等级 或 严重性

什么时候使用 Logging

对于简单的日志使用来说日志功能提供了一系列便利的函数。它们是 debug()，info()，warning()，error() 和 critical()。想要决定何时使用日志，请看下表，其中显示了对于每个通用任务集合来说最好的工具。

你想要执行的任务

此任务最好的工具

对于命令行或程序的应用，结果显示在控制台。

print()

在对程序的普通操作发生时提交事件报告(比如：状态监控和错误调查)

logging.info() 函数(当有诊断目的需要详细输出信息时使用 logging.debug() 函数)

提出一个警告信息基于一个特殊的运行时事件

warnings.warn() 位于代码库中，该事件是可以避免的，需要修改客户端应用以消除告警

logging.warning() 不需要修改客户端应用，但是该事件还是需要引起关注

对一个特殊的运行时事件报告错误

引发异常

报告错误而不引发异常(如在长时间运行中的服务端进程的错误处理)

logging.error(), logging.exception() 或 logging.critical() 分别适用于特定的错误及应用领域

日志功能应以所追踪事件级别或严重性而定。各级别适用性如下（以严重性递增）：

级别

何时使用

DEBUG

细节信息，仅当诊断问题时适用。

INFO

确认程序按预期运行

WARNING

表明有已经或即将发生的意外（例如：磁盘空间不足）。程序仍按预期进行

ERROR

由于严重的问题，程序的某些功能已经不能正常执行

CRITICAL

严重的错误，表明程序已不能继续执行

默认的级别是``WARNING``，意味着只会追踪该级别及以上的事件，除非更改日志配置。

所追踪事件可以以不同形式处理。最简单的方式是输出到控制台。另一种常用的方式是写入磁盘文件。

一个简单的例子

一个非常简单的例子：

import logging
logging.warning('Watch out!')  # will print a message to the console
logging.info('I told you so')  # will not print anything

如果你在命令行中输入这些代码并运行，你将会看到：

WARNING:root:Watch out!

输出到命令行。INFO 消息并没有出现，因为默认级别是 WARNING 。打印的信息包含事件的级别以及在日志调用中的对于事件的描述，例如“Watch out!”。暂时不用担心“root”部分：之后会作出解释。输出格式可按需要进行调整，格式化选项同样会在之后作出解释。

记录日志到文件

一种非常常见的情况是将日志事件记录到文件，让我们继续往下看。请确认启动新的Python 解释器，不要在上一个环境中继续操作:

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

现在，如果我们打开日志文件，我们应当能看到日志信息：

DEBUG:root:This message should go to the log file
INFO:root:So should this
WARNING:root:And this, too

该示例同样展示了如何设置日志追踪级别的阈值。该示例中，由于我们设置的阈值是 DEBUG，所有信息全部打印

如果你想从命令行设置日志级别，例如：

--log=INFO

并且在一些 loglevel 变量中你可以获得 --log 命令的参数，你可以使用：

getattr(logging, loglevel.upper())

通过 level 参数获得你将传递给 basicConfig() 的值。你需要对用户输入数据进行错误排查，可如下例：

# assuming loglevel is bound to the string value obtained from the
# command line argument. Convert to upper case to allow the user to
# specify --log=DEBUG or --log=debug
numeric_level = getattr(logging, loglevel.upper(), None)
if not isinstance(numeric_level, int):
    raise ValueError('Invalid log level: %s' % loglevel)
logging.basicConfig(level=numeric_level, ...)

对 basicConfig() 的调用应该在 debug() ， info() 等的前面。因为它被设计为一次性的配置，只有第一次调用会进行操作，随后的调用不会产生有效操作。

如果多次运行上述脚本，则连续运行的消息将追加到文件 example.log 。 如果你希望每次运行重新开始，而不是记住先前运行的消息，则可以通过将上例中的调用更改为来指定 filemode 参数:

logging.basicConfig(filename='example.log', filemode='w', level=logging.DEBUG)

输出将与之前相同，但不再追加进日志文件，因此早期运行的消息将丢失。

从多个模块记录日志

如果你的程序包含多个模块，这里有一个如何组织日志记录的示例:

# myapp.py
import logging
import mylib

def main():
    logging.basicConfig(filename='myapp.log', level=logging.INFO)
    logging.info('Started')
    mylib.do_something()
    logging.info('Finished')

if __name__ == '__main__':
    main()

# mylib.py
import logging

def do_something():
    logging.info('Doing something')

如果你运行 myapp.py ，你应该在 myapp.log 中看到：

INFO:root:Started
INFO:root:Doing something
INFO:root:Finished

这是你期待看到的。 你可以使用 mylib.py 中的模式将此概括为多个模块。 请注意，对于这种简单的使用模式，除了查看事件描述之外，你不能通过查看日志文件来了解应用程序中消息的 来源 。 如果要跟踪消息的位置，则需要参考教程级别以外的文档 - 请参阅 进阶日志教程 。

记录变量数据

要记录变量数据，请使用格式字符串作为事件描述消息，并将变量数据作为参数附加。 例如:

import logging
logging.warning('%s before you %s', 'Look', 'leap!')

将显示：

WARNING:root:Look before you leap!

如你所见，将可变数据合并到事件描述消息中使用旧的 %-s形式的字符串格式化。 这是为了向后兼容：logging 包的出现时间早于较新的格式化选项例如 str.format() 和 string.Template。 这些较新格式化选项 是 受支持的，但探索它们超出了本教程的范围：有关详细信息，请参阅 Using particular formatting styles throughout your application。

更改显示消息的格式

要更改用于显示消息的格式，你需要指定要使用的格式:

import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
logging.debug('This message should appear on the console')
logging.info('So should this')
logging.warning('And this, too')

这将输出：

DEBUG:This message should appear on the console
INFO:So should this
WARNING:And this, too

请注意，前面示例中出现的“root”已消失。 对于可以出现在格式字符串中的全部内容，你可以参考以下文档 LogRecord 属性 ，但为了简单使用，你只需要 levelname （严重性）， message （事件描述，包括可变数据），也许在事件发生时显示。 这将在下一节中介绍。

在消息中显示日期/时间

要显示事件的日期和时间，你可以在格式字符串中放置 ‘%(asctime)s’

import logging
logging.basicConfig(format='%(asctime)s %(message)s')
logging.warning('is when this event was logged.')

应该打印这样的东西：

2010-12-12 11:41:42,612 is when this event was logged.

日期/时间显示的默认格式（如上所示）类似于 ISO8601 或 RFC 3339 [https://tools.ietf.org/html/rfc3339.html] 。 如果你需要更多地控制日期/时间的格式，请为 basicConfig 提供 datefmt 参数，如下例所示:

import logging
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.warning('is when this event was logged.')

这会显示如下内容：

12/12/2010 11:46:36 AM is when this event was logged.

datefmt 参数的格式与 time.strftime() 支持的格式相同。

后续步骤

基本教程到此结束。 它应该足以让你启动并运行日志记录。 日志包提供了更多功能，但为了充分利用它，你需要花更多的时间来阅读以下部分。 如果你准备好了，可以拿一些你最喜欢的饮料然后继续。

If your logging needs are simple, then use the above examples to incorporate logging into your own scripts, and if you run into problems or don’t understand something, please post a question on the comp.lang.python Usenet group (available at https://groups.google.com/group/comp.lang.python) and you should receive help before too long.

还不够？ 你可以继续阅读接下来的几个部分，这些部分提供了比上面基本部分更高级或深入的教程。 之后，你可以看一下 日志操作手册 。

进阶日志教程

日志库采用模块化方法，并提供几类组件：记录器、处理程序、过滤器和格式化程序。

记录器暴露了应用程序代码直接使用的接口。

处理程序将日志记录（由记录器创建）发送到适当的目标。

过滤器提供了更精细的附加功能，用于确定要输出的日志记录。

格式化程序指定最终输出中日志记录的样式。

日志事件信息在 LogRecord 实例中的记录器、处理程序、过滤器和格式化程序之间传递。

通过调用 Logger 类（以下称为 loggers ， 记录器）的实例来执行日志记录。 每个实例都有一个名称，它们在概念上以点（句点）作为分隔符排列在命名空间的层次结构中。 例如，名为 ‘scan’ 的记录器是记录器 ‘scan.text’ ，’scan.html’ 和 ‘scan.pdf’ 的父级。 记录器名称可以是你想要的任何名称，并指示记录消息源自的应用程序区域。

在命名记录器时使用的一个好习惯是在每个使用日志记录的模块中使用模块级记录器，命名如下:

logger = logging.getLogger(__name__)

这意味着记录器名称跟踪包或模块的层次结构，并且直观地从记录器名称显示记录事件的位置。

记录器层次结构的根称为根记录器。 这是函数 debug() 、 info() 、 warning() 、 error() 和 critical() 使用的记录器，它们只调用根记录器的同名方法。 功能和方法具有相同的签名。 根记录器的名称在记录的输出中打印为 ‘root’ 。

当然，可以将消息记录到不同的地方。 软件包中的支持包含，用于将日志消息写入文件、 HTTP GET/POST 位置、通过 SMTP 发送电子邮件、通用套接字、队列或特定于操作系统的日志记录机制（如 syslog 或 Windows NT 事件日志）。 目标由 handler 类提供。 如果你有任何内置处理程序类未满足的特殊要求，则可以创建自己的日志目标类。

默认情况下，没有为任何日志记录消息设置目标。 你可以使用 basicConfig() 指定目标（例如控制台或文件），如教程示例中所示。 如果你调用函数 debug() 、 info() 、 warning() 、 error() 和 critical() ，他们将检查是否有设置目的地；如果没有设置，它们将在委托给根记录器进行实际的消息输出之前设置目标为控制台（ sys.stderr ）和默认格式的显示消息。

由 basicConfig() 设置的消息默认格式为：

severity:logger name:message

你可以通过使用 format 参数将格式字符串传递给 basicConfig() 来更改此设置。有关如何构造格式字符串的所有选项，请参阅 格式器对象 。

记录流程

记录器和处理程序中的日志事件信息流程如下图所示。

记录器

Logger 对象有三重任务。首先，它们向应用程序代码公开了几种方法，以便应用程序可以在运行时记录消息。其次，记录器对象根据严重性（默认过滤工具）或过滤器对象确定要处理的日志消息。第三，记录器对象将相关的日志消息传递给所有感兴趣的日志处理程序。

记录器对象上使用最广泛的方法分为两类：配置和消息发送。

这些是最常见的配置方法：

Logger.setLevel() 指定记录器将处理的最低严重性日志消息，其中 debug 是最低内置严重性级别， critical 是最高内置严重性级别。 例如，如果严重性级别为 INFO ，则记录器将仅处理 INFO 、 WARNING 、 ERROR 和 CRITICAL 消息，并将忽略 DEBUG 消息。

Logger.addHandler() 和 Logger.removeHandler() 从记录器对象中添加和删除处理程序对象。处理程序在以下内容中有更详细的介绍 处理程序 。

Logger.addFilter() 和 Logger.removeFilter() 可以添加或移除记录器对象中的过滤器。 Filter 对象 包含更多的过滤器细节。

你不需要始终在你创建的每个记录器上调用这些方法。 请参阅本节的最后两段。

配置记录器对象后，以下方法将创建日志消息：

Logger.debug() 、 Logger.info() 、 Logger.warning() 、 Logger.error() 和 Logger.critical() 都创建日志记录，包含消息和与其各自方法名称对应的级别。该消息实际上是一个格式化字符串，它可能包含标题字符串替换语法 %s 、 %d 、 %f 等等。其余参数是与消息中的替换字段对应的对象列表。关于 **kwargs ，日志记录方法只关注 exc_info 的关键字，并用它来确定是否记录异常信息。

Logger.exception() 创建与 Logger.error() 相似的日志信息。 不同之处是， Logger.exception() 同时还记录当前的堆栈追踪。仅从异常处理程序调用此方法。

Logger.log() 将日志级别作为显式参数。对于记录消息而言，这比使用上面列出的日志级别方便方法更加冗长，但这是自定义日志级别的方法。

getLogger() 返回对具有指定名称的记录器实例的引用（如果已提供），或者如果没有则返回 root 。名称是以句点分隔的层次结构。多次调用 getLogger() 具有相同的名称将返回对同一记录器对象的引用。在分层列表中较低的记录器是列表中较高的记录器的子项。例如，给定一个名为 foo 的记录器，名称为 foo.bar 、 foo.bar.baz 和 foo.bam 的记录器都是 foo 子项。

记录器具有 有效等级 的概念。如果未在记录器上显式设置级别，则使用其父级别作为其有效级别。如果父级没有明确的级别设置，则检查 其 父级。依此类推，搜索所有上级元素，直到找到明确设置的级别。根记录器始终具有显式级别集（默认情况下为 WARNING ）。在决定是否处理事件时，记录器的有效级别用于确定事件是否传递给记录器的处理程序。

子记录器将消息传播到与其上级记录器关联的处理程序。因此，不必为应用程序使用的所有记录器定义和配置处理程序。为顶级记录器配置处理程序并根据需要创建子记录器就足够了。（但是，你可以通过将记录器的 propagate 属性设置 False 来关闭传播。）

处理程序

Handler 对象负责将适当的日志消息（基于日志消息的严重性）分派给处理程序的指定目标。 Logger 对象可以使用 addHandler() 方法向自己添加零个或多个处理程序对象。作为示例场景，应用程序可能希望将所有日志消息发送到日志文件，将错误或更高的所有日志消息发送到标准输出，以及将所有关键消息发送至一个邮件地址。 此方案需要三个单独的处理程序，其中每个处理程序负责将特定严重性的消息发送到特定位置。

标准库包含很多处理程序类型（参见 有用的处理程序 ）；教程主要使用 StreamHandler 和 FileHandler 。

处理程序中很少有方法可供应用程序开发人员使用。与使用内置处理程序对象（即不创建自定义处理程序）的应用程序开发人员相关的唯一处理程序方法是以下配置方法：

setLevel() 方法，就像在记录器对象中一样，指定将被分派到适当目标的最低严重性。为什么有两个 setLevel() 方法？记录器中设置的级别确定将传递给其处理程序的消息的严重性。每个处理程序中设置的级别确定处理程序将发送哪些消息。

setFormatter() 选择一个该处理程序使用的 Formatter 对象。

addFilter() 和 removeFilter() 分别在处理程序上配置和取消配置过滤器对象。

应用程序代码不应直接实例化并使用 Handler 的实例。 相反， Handler 类是一个基类，它定义了所有处理程序应该具有的接口，并建立了子类可以使用（或覆盖）的一些默认行为。

格式化程序

格式化程序对象配置日志消息的最终顺序、结构和内容。 与 logging.Handler 类不同，应用程序代码可以实例化格式化程序类，但如果应用程序需要特殊行为，则可能会对格式化程序进行子类化。构造函数有三个可选参数 —— 消息格式字符串、日期格式字符串和样式指示符。

logging.Formatter.__init__(fmt=None, datefmt=None, style='%')

如果没有消息格式字符串，则默认使用原始消息。如果没有日期格式字符串，则默认日期格式为：

%Y-%m-%d %H:%M:%S

最后加上毫秒数。 style 是 ％，’{ ‘ 或 ‘$’ 之一。 如果未指定其中一个，则将使用 ‘％’。

如果 style 是 ‘％’，则消息格式字符串使用 %(<dictionary key>)s 样式字符串替换；可能的键值在 LogRecord 属性 中。 如果样式为 ‘{‘，则假定消息格式字符串与 str.format() （使用关键字参数）兼容，而如果样式为 ‘$’ ，则消息格式字符串应符合 string.Template.substitute() 。

在 3.2 版更改: 添加 style 形参。

以下消息格式字符串将以人类可读的格式记录时间、消息的严重性以及消息的内容，按此顺序:

'%(asctime)s - %(levelname)s - %(message)s'

格式化程序使用用户可配置的函数将记录的创建时间转换为元组。 默认情况下，使用 time.localtime() ；要为特定格式化程序实例更改此项，请将实例的 converter 属性设置为具有相同签名的函数 time.localtime() 或 time.gmtime() 。 要为所有格式化程序更改它，例如，如果你希望所有记录时间都以 GMT 显示，请在格式化程序类中设置 converter 属性（对于 GMT 显示，设置为 time.gmtime ）。

配置日志记录

开发者可以通过三种方式配置日志记录：

使用调用上面列出的配置方法的 Python 代码显式创建记录器、处理程序和格式化程序。

创建日志配置文件并使用 fileConfig() 函数读取它。

创建配置信息字典并将其传递给 dictConfig() 函数。

有关最后两个选项的参考文档，请参阅 配置函数 。 以下示例使用 Python 代码配置一个非常简单的记录器/一个控制台处理程序和一个简单的格式化程序:

import logging

# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

从命令行运行此模块将生成以下输出：

$ python simple_logging_module.py
2005-03-19 15:10:26,618 - simple_example - DEBUG - debug message
2005-03-19 15:10:26,620 - simple_example - INFO - info message
2005-03-19 15:10:26,695 - simple_example - WARNING - warn message
2005-03-19 15:10:26,697 - simple_example - ERROR - error message
2005-03-19 15:10:26,773 - simple_example - CRITICAL - critical message

以下 Python 模块创建的记录器、处理程序和格式化程序几乎与上面列出的示例中的相同，唯一的区别是对象的名称:

import logging
import logging.config

logging.config.fileConfig('logging.conf')

# create logger
logger = logging.getLogger('simpleExample')

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

这是 logging.conf 文件：

[loggers]
keys=root,simpleExample

[handlers]
keys=consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

输出几乎与不基于配置文件的示例相同：

$ python simple_logging_config.py
2005-03-19 15:38:55,977 - simpleExample - DEBUG - debug message
2005-03-19 15:38:55,979 - simpleExample - INFO - info message
2005-03-19 15:38:56,054 - simpleExample - WARNING - warn message
2005-03-19 15:38:56,055 - simpleExample - ERROR - error message
2005-03-19 15:38:56,130 - simpleExample - CRITICAL - critical message

你可以看到配置文件方法比 Python 代码方法有一些优势，主要是配置和代码的分离以及非开发者轻松修改日志记录属性的能力。

警告

The fileConfig() function takes a default parameter, disable_existing_loggers, which defaults to True for reasons of backward compatibility. This may or may not be what you want, since it will cause any loggers existing before the fileConfig() call to be disabled unless they (or an ancestor) are explicitly named in the configuration. Please refer to the reference documentation for more information, and specify False for this parameter if you wish.

The dictionary passed to dictConfig() can also specify a Boolean value with key disable_existing_loggers, which if not specified explicitly in the dictionary also defaults to being interpreted as True. This leads to the logger-disabling behaviour described above, which may not be what you want - in which case, provide the key explicitly with a value of False.

请注意，配置文件中引用的类名称需要相对于日志记录模块，或者可以使用常规导入机制解析的绝对值。因此，你可以使用 WatchedFileHandler （相对于日志记录模块）或 mypackage.mymodule.MyHandler （对于在 mypackage 包中定义的类和模块 mymodule ，其中 mypackage 在Python导入路径上可用）。

在 Python 3.2 中，引入了一种新的配置日志记录的方法，使用字典来保存配置信息。 这提供了上述基于配置文件方法的功能的超集，并且是新应用程序和部署的推荐配置方法。 因为 Python 字典用于保存配置信息，并且由于你可以使用不同的方式填充该字典，因此你有更多的配置选项。 例如，你可以使用 JSON 格式的配置文件，或者如果你有权访问 YAML 处理功能，则可以使用 YAML 格式的文件来填充配置字典。当然，你可以在 Python 代码中构造字典，通过套接字以 pickle 形式接收它，或者使用对你的应用程序合理的任何方法。

以下是与上述相同配置的示例，采用 YAML 格式，用于新的基于字典的方法：

version: 1
formatters:
  simple:
    format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
  console:
    class: logging.StreamHandler
    level: DEBUG
    formatter: simple
    stream: ext://sys.stdout
loggers:
  simpleExample:
    level: DEBUG
    handlers: [console]
    propagate: no
root:
  level: DEBUG
  handlers: [console]

有关使用字典进行日志记录的更多信息，请参阅 配置函数 。

如果没有提供配置会发生什么

如果未提供日志记录配置，则可能出现需要输出日志记录事件但无法找到输出事件的处理程序的情况。 在这些情况下，日志包的行为取决于 Python 版本。

对于3.2之前的Python版本，行为如下：

如果 logging.raiseExceptions 为 False （生产模式），则会以静默方式丢弃该事件。

如果 logging.raiseExceptions 为 True （开发模式），则会打印一条消息“无法找到记录器 X.Y.Z 的处理程序”。

在 Python 3.2 及更高版本中，行为如下：

事件使用“最后的处理程序”输出，存储在 logging.lastResort 中。 这个内部处理程序与任何记录器都没有关联，它的作用类似于 StreamHandler ，它将事件描述消息写入 sys.stderr 的当前值（因此服从任何可能的重定向影响）。 没有对消息进行格式化——只打印裸事件描述消息。处理程序的级别设置为“警告”，因此将输出此级别和更高级别的所有事件。

要获得 3.2 之前的行为， logging.lastResort 可以设置为 None 。

配置库的日志记录

在开发使用日志记录的库时，你应该注意记录库如何使用日志记录——例如，使用的记录器的名称。还需要考虑其日志记录配置。如果应用程序不使用日志记录，并且库代码进行日志记录调用，那么（如上一节所述）严重性为“WARNING”及更高级别的事件将打印到 sys.stderr 。这被认为是最好的默认行为。

If for some reason you don’t want these messages printed in the absence of any logging configuration, you can attach a do-nothing handler to the top-level logger for your library. This avoids the message being printed, since a handler will be always be found for the library’s events: it just doesn’t produce any output. If the library user configures logging for application use, presumably that configuration will add some handlers, and if levels are suitably configured then logging calls made in library code will send output to those handlers, as normal.

日志包中包含一个不做任何事情的处理程序： NullHandler （自 Python 3.1 起）。可以将此处理程序的实例添加到库使用的日志记录命名空间的顶级记录器中（ 如果 你希望在没有日志记录配置的情况下阻止库的记录事件输出到 sys.stderr ）。如果库 foo 的所有日志记录都是使用名称匹配 ‘foo.x’ ， ‘foo.x.y’ 等的记录器完成的，那么代码:

import logging
logging.getLogger('foo').addHandler(logging.NullHandler())

应该有预计的效果。如果一个组织生成了许多库，则指定的记录器名称可以是 “orgname.foo” 而不仅仅是 “foo” 。

注解

强烈建议你 不要将 NullHandler 以外的任何处理程序添加到库的记录器中 。这是因为处理程序的配置是使用你的库的应用程序开发人员的权利。应用程序开发人员了解他们的目标受众以及哪些处理程序最适合他们的应用程序：如果你在“底层”添加处理程序，则可能会干扰他们执行单元测试和提供符合其要求的日志的能力。

日志级别

日志记录级别的数值在下表中给出。如果你想要定义自己的级别，并且需要它们具有相对于预定义级别的特定值，那么这些内容可能是你感兴趣的。如果你定义具有相同数值的级别，它将覆盖预定义的值; 预定义的名称丢失。

级别

数值

CRITICAL

50

ERROR

40

WARNING

30

INFO

20

DEBUG

10

NOTSET

0

级别也可以与记录器相关联，由开发人员设置或通过加载已保存的日志记录配置。在记录器上调用日志记录方法时，记录器会将其自己的级别与与方法调用关联的级别进行比较。如果记录器的级别高于方法调用的级别，则实际上不会生成任何记录消息。这是控制日志记录输出详细程度的基本机制。

记录消息被编码为 LogRecord 类的实例。当记录器决定实际记录事件时，从记录消息创建 LogRecord 实例。

记录消息通过使用 handlers 进行调度机制，它们是 Handler 类的子类的实例。处理程序负责确保记录的消息（以 LogRecord 的形式）最终位于特定位置（或一组位置），这对该消息的目标受众（例如最终用户、 支持服务台员工、系统管理员、开发人员）。传递处理程序用于特定目标的 LogRecord 实例。 每个记录器可以有零个、一个或多个与之关联的处理程序（通过 Logger 的 addHandler() 方法）。除了与记录器直接关联的任何处理程序之外，还调用与记录器的 所有祖先相关联的所有处理程序来分派消息（除非记录器的 *propagate 标志设置为false值，这将停止传递到上级处理程序）。

就像记录器一样，处理程序可以具有与它们相关联的级别。处理程序的级别作为过滤器，其方式与记录器级别相同。如果处理程序决定调度一个事件，则使用 emit() 方法将消息发送到其目标。大多数用户定义的 Handler 子类都需要重载 emit() 。

自定义级别

定义你自己的级别是可能的，但不一定是必要的，因为现有级别是根据实践经验选择的。但是，如果你确信需要自定义级别，那么在执行此操作时应特别小心，如果你正在开发库，则 定义自定义级别可能是一个非常糟糕的主意 。 这是因为如果多个库作者都定义了他们自己的自定义级别，那么使用开发人员很难控制和解释这些多个库的日志记录输出，因为给定的数值可能意味着不同的东西 对于不同的库。

有用的处理程序

作为 Handler 基类的补充，提供了很多有用的子类：

StreamHandler 实例发送消息到流（类似文件对象）。

FileHandler 实例将消息发送到硬盘文件。

BaseRotatingHandler 是轮换日志文件的处理程序的基类。它并不应该直接实例化。而应该使用 RotatingFileHandler 或 TimedRotatingFileHandler 代替它。

RotatingFileHandler 实例将消息发送到硬盘文件，支持最大日志文件大小和日志文件轮换。

TimedRotatingFileHandler 实例将消息发送到硬盘文件，以特定的时间间隔轮换日志文件。

SocketHandler 实例将消息发送到 TCP/IP 套接字。从 3.4 开始，也支持 Unix 域套接字。

DatagramHandler 实例将消息发送到 UDP 套接字。从 3.4 开始，也支持 Unix 域套接字。

SMTPHandler 实例将消息发送到指定的电子邮件地址。

SysLogHandler 实例将消息发送到 Unix syslog 守护程序，可能在远程计算机上。

NTEventLogHandler 实例将消息发送到 Windows NT/2000/XP 事件日志。

MemoryHandler 实例将消息发送到内存中的缓冲区，只要满足特定条件，缓冲区就会刷新。

HTTPHandler 实例使用 GET 或 POST 方法将消息发送到 HTTP 服务器。

WatchedFileHandler 实例会监视他们要写入日志的文件。如果文件发生更改，则会关闭该文件并使用文件名重新打开。此处理程序仅在类 Unix 系统上有用； Windows 不支持依赖的基础机制。

QueueHandler 实例将消息发送到队列，例如在 queue 或 multiprocessing 模块中实现的队列。

NullHandler 实例对错误消息不执行任何操作。它们由想要使用日志记录的库开发人员使用，但是想要避免如果库用户没有配置日志记录，则显示 “无法找到记录器XXX的消息处理器” 消息的情况。有关更多信息，请参阅 配置库的日志记录 。

3.1 新版功能: NullHandler 类。

3.2 新版功能: QueueHandler 类。

The NullHandler 、 StreamHandler 和 FileHandler 类在核心日志包中定义。其他处理程序定义在 logging.handlers 中。（还有另一个子模块 logging.config ，用于配置功能）

记录的消息被格式化为通过 Formatter 类的实例进行呈现。 它们使用适合与 ％ 运算符一起使用的格式字符串和字典进行初始化。

对于批量格式化多个消息，可以使用 BufferingFormatter 的实例。除了格式字符串（应用于批处理中的每个消息）之外，还提供了标题和尾部格式字符串。

当基于记录器级别和处理程序级别的过滤不够时，可以将 Filter 的实例添加到 Logger 和 Handler 实例（通过它们的 addFilter() 方法）。在决定进一步处理消息之前，记录器和处理程序都会查询其所有过滤器以获得许可。如果任何过滤器返回 false 值，则不会进一步处理该消息。

基本 Filter 的功能允许按特定的记录器名称进行过滤。如果使用此功能，则允许通过过滤器发送到指定记录器及其子项的消息，并丢弃所有消息。

记录日志中引发的异常

日志包设计为忽略记录日志生产时发生的异常。这样，处理日志记录事件时发生的错误（例如日志记录错误配置、网络或其他类似错误）不会导致使用日志记录的应用程序过早终止。

SystemExit 和 KeyboardInterrupt 异常永远不会被忽略。 在 Handler 子类的 emit() 方法中发生的其他异常被传递给它的 handleError() 方法。

Handler 中默认实现的 handleError() 检查是否设置了模块级变量 raiseExceptions 。如果有设置，则会将跟踪打印到 sys.stderr 。如果未设置，则忽略异常。

注解

raiseExceptions 默认值是 True。 这是因为在开发期间，你通常希望收到任何发生异常的通知。建议你将 raiseExceptions 设置为 False 以供生产环境使用。

使用任意对象作为消息

在前面的部分和示例中，都假设记录事件时传递的消息是字符串。 但是，这不是唯一的可能性。你可以将任意对象作为消息传递，并且当日志记录系统需要将其转换为字符串表示时，将调用其 __ str__() 方法。实际上，如果你愿意，你可以完全避免计算字符串表示。例如， SocketHandler 通过 pickle 并网络发送来发出事件。

优化

消息参数的格式化将被推迟，直到无法避免。但是，计算传递给日志记录方法的参数也可能很消耗资源，如果记录器只是丢弃你的事件，你可能希望避免这样做。要决定做什么，可以调用 isEnabledFor() 方法，该方法接受一个 level 参数，如果记录器为该级别的调用创建了该事件，则返回 true 。 你可以写这样的代码:

if logger.isEnabledFor(logging.DEBUG):
    logger.debug('Message with %s, %s', expensive_func1(),
                                        expensive_func2())

因此，如果记录器的阈值设置在“DEBUG”以上，则永远不会调用 expensive_func1() 和 expensive_func2() 。

注解

在某些情况下， isEnabledFor() 本身可能比你想要的更消耗资源（例如，对于深度嵌套的记录器，其中显式级别仅在记录器层次结构中设置为高）。在这种情况下（或者如果你想避免在紧密循环中调用方法），你可以在本地或实例变量中将调用的结果缓存到 isEnabledFor() ，并使用它而不是调用每次方法。只在日志记录配置在应用程序运行时动态更改（这不常见）时需重新计算这样的缓存值。

对于需要对收集的日志信息进行更精确控制的特定应用程序，还可以进行其他优化。 以下是你可以执行的操作列表，以避免在你不需要的日志记录期间进行处理：

你不想收集的内容

如何避免收集它

有关调用来源的信息

将 logging._srcfile 设置为 None 。这避免了调用 sys._getframe() ，如果 PyPy 支持 Python 3.x ,这可能有助于加速 PyPy （无法加速使用 sys._getframe() 的代码）等环境中的代码.

线程信息

将 logging.logThreads 置为 0 。

进程信息

将 logging.logProcesses 置为 0 。

另请注意，核心日志记录模块仅包含基本处理程序。如果你不导入 logging.handlers 和 logging.config ，它们将不会占用任何内存。

参见

模块 logging

日志记录模块的 API 参考。

模块 logging.config

日志记录模块的配置 API 。

模块 logging.handlers

日志记录模块附带的有用处理程序。

A logging cookbook

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

日志操作手册

作者

Vinay Sajip <vinay_sajip at red-dove dot com>

本页包含了许多日志记录相关的概念，这些概念在过去一直被认为很有用。

在多个模块中记录日志

多次调用``logging.getLogger(‘someLogger’)``会返回对同一个日志记录器对象的引用。不仅在同一个模块中是这样的，而且在不同模块之间，只要是在同一个Python解释器进程中，也是如此。 这就是对同一个对象的多个引用；此外，应用程序代码也可以在一个模块中定义和配置父日志记录器，在单独的模块中创建（但不配置）一个子日志记录器，并且对子日志记录器的所有调用都将传递给父日志记录器。 这里是一个主要模块:

import logging
import auxiliary_module

# create logger with 'spam_application'
logger = logging.getLogger('spam_application')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('spam.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)

logger.info('creating an instance of auxiliary_module.Auxiliary')
a = auxiliary_module.Auxiliary()
logger.info('created an instance of auxiliary_module.Auxiliary')
logger.info('calling auxiliary_module.Auxiliary.do_something')
a.do_something()
logger.info('finished auxiliary_module.Auxiliary.do_something')
logger.info('calling auxiliary_module.some_function()')
auxiliary_module.some_function()
logger.info('done with auxiliary_module.some_function()')

这里是辅助模块:

import logging

# create logger
module_logger = logging.getLogger('spam_application.auxiliary')

class Auxiliary:
    def __init__(self):
        self.logger = logging.getLogger('spam_application.auxiliary.Auxiliary')
        self.logger.info('creating an instance of Auxiliary')

    def do_something(self):
        self.logger.info('doing something')
        a = 1 + 1
        self.logger.info('done doing something')

def some_function():
    module_logger.info('received a call to "some_function"')

输出结果会像这样:

2005-03-23 23:47:11,663 - spam_application - INFO -
   creating an instance of auxiliary_module.Auxiliary
2005-03-23 23:47:11,665 - spam_application.auxiliary.Auxiliary - INFO -
   creating an instance of Auxiliary
2005-03-23 23:47:11,665 - spam_application - INFO -
   created an instance of auxiliary_module.Auxiliary
2005-03-23 23:47:11,668 - spam_application - INFO -
   calling auxiliary_module.Auxiliary.do_something
2005-03-23 23:47:11,668 - spam_application.auxiliary.Auxiliary - INFO -
   doing something
2005-03-23 23:47:11,669 - spam_application.auxiliary.Auxiliary - INFO -
   done doing something
2005-03-23 23:47:11,670 - spam_application - INFO -
   finished auxiliary_module.Auxiliary.do_something
2005-03-23 23:47:11,671 - spam_application - INFO -
   calling auxiliary_module.some_function()
2005-03-23 23:47:11,672 - spam_application.auxiliary - INFO -
   received a call to 'some_function'
2005-03-23 23:47:11,673 - spam_application - INFO -
   done with auxiliary_module.some_function()

在多个线程中记录日志

在多个线程中记录日志并不需要特殊的处理，以下示例展示了如何在主（初始）线程和另一个线程中记录日志:

import logging
import threading
import time

def worker(arg):
    while not arg['stop']:
        logging.debug('Hi from myfunc')
        time.sleep(0.5)

def main():
    logging.basicConfig(level=logging.DEBUG, format='%(relativeCreated)6d %(threadName)s %(message)s')
    info = {'stop': False}
    thread = threading.Thread(target=worker, args=(info,))
    thread.start()
    while True:
        try:
            logging.debug('Hello from main')
            time.sleep(0.75)
        except KeyboardInterrupt:
            info['stop'] = True
            break
    thread.join()

if __name__ == '__main__':
    main()

脚本会运行输出类似下面的内容:

   0 Thread-1 Hi from myfunc
   3 MainThread Hello from main
 505 Thread-1 Hi from myfunc
 755 MainThread Hello from main
1007 Thread-1 Hi from myfunc
1507 MainThread Hello from main
1508 Thread-1 Hi from myfunc
2010 Thread-1 Hi from myfunc
2258 MainThread Hello from main
2512 Thread-1 Hi from myfunc
3009 MainThread Hello from main
3013 Thread-1 Hi from myfunc
3515 Thread-1 Hi from myfunc
3761 MainThread Hello from main
4017 Thread-1 Hi from myfunc
4513 MainThread Hello from main
4518 Thread-1 Hi from myfunc

这表明不同线程的日志像期望的那样穿插输出，当然更多的线程也会像这样输出。

多个日志处理器以及多种格式化器

日志记录器是普通的Python对象。addHandler() 方法对可以添加的日志处理器的数量没有限制。有时候，应用程序需要将所有严重性的所有消息记录到一个文本文件，而将错误或更高等级的消息输出到控制台。要进行这样的设定，只需配置适当的日志处理器即可。在应用程序代码中，记录日志的调用将保持不变。以下是对之前基于模块的简单配置示例的略微修改:

import logging

logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('spam.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add the handlers to logger
logger.addHandler(ch)
logger.addHandler(fh)

# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

需要注意的是，’应用程序’ 代码并不关心是否有多个日志处理器。所有的改变的只是添加和配置了一个新的名为*fh*的日志处理器。

在编写和测试应用程序时，能够创建带有更高或更低消息等级的过滤器的日志处理器是非常有用的。为了避免过多地使用 print 语句去调试，请使用 logger.debug ：它不像 print 语句需要你不得不在调试结束后注释或删除掉，logger.debug 语句可以在源代码中保持不变，在你再一次需要它之前保持无效。那时，唯一需要改变的是修改日志记录器和/或日志处理器的消息等级，以进行调试。

在多个地方记录日志

假设有这样一种情况，你需要将日志按不同的格式和不同的情况存储在控制台和文件中。比如说想把日志等级为DEBUG或更高的消息记录于文件中，而把那些等级为INFO或更高的消息输出在控制台。而且记录在文件中的消息格式需要包含时间戳，打印在控制台的不需要。以下示例展示了如何做到:

import logging

# set up logging to file - see previous section for more details
logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    datefmt='%m-%d %H:%M',
                    filename='/temp/myapp.log',
                    filemode='w')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)

# Now, we can log to the root logger, or any other logger. First the root...
logging.info('Jackdaws love my big sphinx of quartz.')

# Now, define a couple of other loggers which might represent areas in your
# application:

logger1 = logging.getLogger('myapp.area1')
logger2 = logging.getLogger('myapp.area2')

logger1.debug('Quick zephyrs blow, vexing daft Jim.')
logger1.info('How quickly daft jumping zebras vex.')
logger2.warning('Jail zesty vixen who grabbed pay from quack.')
logger2.error('The five boxing wizards jump quickly.')

当运行后，你会看到控制台如下所示

root        : INFO     Jackdaws love my big sphinx of quartz.
myapp.area1 : INFO     How quickly daft jumping zebras vex.
myapp.area2 : WARNING  Jail zesty vixen who grabbed pay from quack.
myapp.area2 : ERROR    The five boxing wizards jump quickly.

而在文件中会看到像这样

10-22 22:19 root         INFO     Jackdaws love my big sphinx of quartz.
10-22 22:19 myapp.area1  DEBUG    Quick zephyrs blow, vexing daft Jim.
10-22 22:19 myapp.area1  INFO     How quickly daft jumping zebras vex.
10-22 22:19 myapp.area2  WARNING  Jail zesty vixen who grabbed pay from quack.
10-22 22:19 myapp.area2  ERROR    The five boxing wizards jump quickly.

正如你所看到的，DEBUG级别的消息只展示在文件中，而其他消息两个地方都会输出。

这个示例只演示了在控制台和文件中去记录日志，但你也可以自由组合任意数量的日志处理器。

日志服务器配置示例

以下是在一个模块中使用日志服务器配置的示例:

import logging
import logging.config
import time
import os

# read initial config file
logging.config.fileConfig('logging.conf')

# create and start listener on port 9999
t = logging.config.listen(9999)
t.start()

logger = logging.getLogger('simpleExample')

try:
    # loop through logging calls to see the difference
    # new configurations make, until Ctrl+C is pressed
    while True:
        logger.debug('debug message')
        logger.info('info message')
        logger.warn('warn message')
        logger.error('error message')
        logger.critical('critical message')
        time.sleep(5)
except KeyboardInterrupt:
    # cleanup
    logging.config.stopListening()
    t.join()

然后如下的脚本，它接收文件名做为命令行参数，并将该文件以二进制编码的方式传给服务器，做为新的日志服务器配置:

#!/usr/bin/env python
import socket, sys, struct

with open(sys.argv[1], 'rb') as f:
    data_to_send = f.read()

HOST = 'localhost'
PORT = 9999
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('connecting...')
s.connect((HOST, PORT))
print('sending config...')
s.send(struct.pack('>L', len(data_to_send)))
s.send(data_to_send)
s.close()
print('complete')

处理日志处理器的阻塞

有时候需要让日志处理程序在不阻塞当前正在记录线程的情况下完成工作。 这在Web应用程序中很常见，当然也会在其他场景中出现。

一个常见的缓慢行为是 SMTPHandler: 由于开发者无法控制的多种原因（例如，性能不佳的邮件或网络基础架构），发送电子邮件可能需要很长时间。 其实几乎所有基于网络的处理程序都可能造成阻塞：即便是 SocketHandler 也可能在底层进行 DNS 查询，这太慢了（这个查询会深入至套接字代码，位于 Python 层之下，这是不受开发者控制的）。

一种解决方案是分成两部分去处理。第一部分，针对那些对性能有要求的关键线程的日志记录附加一个 QueueHandler。 日志记录器只需简单写入队列，该队列可以设置一个足够大的容量甚至不设置容量上限。通常写入队列是一个快速的操作，即使可能需要在代码中去捕获例如 queue.Full 等异常。 如果你是一名处理关键线程的开发者，请务必记录这些信息 (包括建议只为日志处理器附加 QueueHandlers) 以便于其他开发者使用你的代码。

解决方案的另一部分是 QueueListener，它被设计用来作为 QueueHandler 的对应。 QueueListener 非常简单：向其传入一个队列和一些处理句柄，它会启动一个内部线程来监听从 QueueHandlers (或任何其他可用的 LogRecords 源) 发送过来的 LogRecords 队列。 LogRecords 会从队列中被移除，并被传递给句柄进行处理。

使用一个单独的类 QueueListener 优点是可以使用同一个实例去服务于多个``QueueHandlers``。这样会更节省资源，否则每个处理程序都占用一个线程没有任何益处。

以下是使用了这样两个类的示例(省略了导入语句):

que = queue.Queue(-1)  # no limit on size
queue_handler = QueueHandler(que)
handler = logging.StreamHandler()
listener = QueueListener(que, handler)
root = logging.getLogger()
root.addHandler(queue_handler)
formatter = logging.Formatter('%(threadName)s: %(message)s')
handler.setFormatter(formatter)
listener.start()
# The log output will display the thread which generated
# the event (the main thread) rather than the internal
# thread which monitors the internal queue. This is what
# you want to happen.
root.warning('Look out!')
listener.stop()

在运行后会产生:

MainThread: Look out!

在 3.5 版更改: 在Python 3.5之前，QueueListener 总是把从队列中接收的每个消息都传给它初始化的日志处理程序。(这是因为它会假设过滤级别总是在队列的另一侧去设置的。) 从Python 3.5开始，可以通过在监听器构造函数中添加一个参数``respect_handler_level=True``改变这种情况。当这样设置时，监听器会比较每条消息的等级和日志处理器中设置的等级，只把需要传递的消息传给对应的日志处理器。

通过网络发送和接收日志

如果你想在网络上发送日志，并在接收端处理它们。一个简单的方式是通过附加一个 SocketHandler 的实例在发送端的根日志处理器中:

import logging, logging.handlers

rootLogger = logging.getLogger('')
rootLogger.setLevel(logging.DEBUG)
socketHandler = logging.handlers.SocketHandler('localhost',
                    logging.handlers.DEFAULT_TCP_LOGGING_PORT)
# don't bother with a formatter, since a socket handler sends the event as
# an unformatted pickle
rootLogger.addHandler(socketHandler)

# Now, we can log to the root logger, or any other logger. First the root...
logging.info('Jackdaws love my big sphinx of quartz.')

# Now, define a couple of other loggers which might represent areas in your
# application:

logger1 = logging.getLogger('myapp.area1')
logger2 = logging.getLogger('myapp.area2')

logger1.debug('Quick zephyrs blow, vexing daft Jim.')
logger1.info('How quickly daft jumping zebras vex.')
logger2.warning('Jail zesty vixen who grabbed pay from quack.')
logger2.error('The five boxing wizards jump quickly.')

在接收端，你可以使用 socketserver 模块设置一个接收器。这里是一个基础示例:

import pickle
import logging
import logging.handlers
import socketserver
import struct

class LogRecordStreamHandler(socketserver.StreamRequestHandler):
    """Handler for a streaming logging request.

    This basically logs the record using whatever logging policy is
    configured locally.
    """

    def handle(self):
        """
        Handle multiple requests - each expected to be a 4-byte length,
        followed by the LogRecord in pickle format. Logs the record
        according to whatever policy is configured locally.
        """
        while True:
            chunk = self.connection.recv(4)
            if len(chunk) < 4:
                break
            slen = struct.unpack('>L', chunk)[0]
            chunk = self.connection.recv(slen)
            while len(chunk) < slen:
                chunk = chunk + self.connection.recv(slen - len(chunk))
            obj = self.unPickle(chunk)
            record = logging.makeLogRecord(obj)
            self.handleLogRecord(record)

    def unPickle(self, data):
        return pickle.loads(data)

    def handleLogRecord(self, record):
        # if a name is specified, we use the named logger rather than the one
        # implied by the record.
        if self.server.logname is not None:
            name = self.server.logname
        else:
            name = record.name
        logger = logging.getLogger(name)
        # N.B. EVERY record gets logged. This is because Logger.handle
        # is normally called AFTER logger-level filtering. If you want
        # to do filtering, do it at the client end to save wasting
        # cycles and network bandwidth!
        logger.handle(record)

class LogRecordSocketReceiver(socketserver.ThreadingTCPServer):
    """
    Simple TCP socket-based logging receiver suitable for testing.
    """

    allow_reuse_address = True

    def __init__(self, host='localhost',
                 port=logging.handlers.DEFAULT_TCP_LOGGING_PORT,
                 handler=LogRecordStreamHandler):
        socketserver.ThreadingTCPServer.__init__(self, (host, port), handler)
        self.abort = 0
        self.timeout = 1
        self.logname = None

    def serve_until_stopped(self):
        import select
        abort = 0
        while not abort:
            rd, wr, ex = select.select([self.socket.fileno()],
                                       [], [],
                                       self.timeout)
            if rd:
                self.handle_request()
            abort = self.abort

def main():
    logging.basicConfig(
        format='%(relativeCreated)5d %(name)-15s %(levelname)-8s %(message)s')
    tcpserver = LogRecordSocketReceiver()
    print('About to start TCP server...')
    tcpserver.serve_until_stopped()

if __name__ == '__main__':
    main()

首先运行服务端，然后是客户端。在客户端，没有什么内容会打印在控制台中；在服务端，你应该会看到如下内容：

About to start TCP server...
   59 root            INFO     Jackdaws love my big sphinx of quartz.
   59 myapp.area1     DEBUG    Quick zephyrs blow, vexing daft Jim.
   69 myapp.area1     INFO     How quickly daft jumping zebras vex.
   69 myapp.area2     WARNING  Jail zesty vixen who grabbed pay from quack.
   69 myapp.area2     ERROR    The five boxing wizards jump quickly.

请注意，在某些情况下序列化会存在一些安全。如果这影响到你，那么你可以通过覆盖 makePickle() 方法，使用自己的实现来解决，并调整上述脚本也使用覆盖后的序列化方法。

在日志记录中添加上下文信息

有时，除了传递给日志记录器调用的参数外，我们还希望日志记录中包含上下文信息。例如，有一个网络应用，可能需要记录一些特殊的客户端信息在日志中（比如客户端的用户名、IP地址等）。虽然你可以通过设置额外的参数去达到这个目的，但这种方式不一定方便。或者你可能想到在每个连接的基础上创建一个 Logger 的实例，但这些实例是不会被垃圾回收的，这在练习中也许不是问题，但当 Logger 的实例数量取决于你应用程序中想记录的细致程度时，如果 Logger 的实例数量不受限制的话，将会变得难以管理。

使用日志适配器传递上下文信息

一个传递上下文信息和日志事件信息的简单办法是使用类 LoggerAdapter。 这个类设计的像 Logger，所以可以直接调用 debug()、info()、 warning()、 error()、exception()、 critical() 和 log()。 这些方法在对应的 Logger 中使用相同的签名，所以可以交替使用两种类型的实例。

当你创建一个 LoggerAdapter 的实例时，你会传入一个 Logger 的实例和一个包含了上下文信息的字典对象。当你调用一个 LoggerAdapter 实例的方法时，它会把调用委托给内部的 Logger 的实例，并为其整理相关的上下文信息。这是 LoggerAdapter 的一个代码片段:

def debug(self, msg, *args, **kwargs):
    """
    Delegate a debug call to the underlying logger, after adding
    contextual information from this adapter instance.
    """
    msg, kwargs = self.process(msg, kwargs)
    self.logger.debug(msg, *args, **kwargs)

LoggerAdapter 的 process() 方法是将上下文信息添加到日志的输出中。 它传入日志消息和日志调用的关键字参数，并传回（隐式的）这些修改后的内容去调用底层的日志记录器。此方法的默认参数只是一个消息字段，但留有一个 ‘extra’ 的字段作为关键字参数传给构造器。当然，如果你在调用适配器时传入了一个 ‘extra’ 字段的参数，它会被静默覆盖。

使用 ‘extra’ 的优点是这些键值对会被传入 LogRecord 实例的 __dict__ 中，让你通过 Formatter 的实例直接使用定制的字符串，实例能找到这个字典类对象的键。 如果你需要一个其他的方法，比如说，想要在消息字符串前后增加上下文信息，你只需要创建一个 LoggerAdapter 的子类，并覆盖它的 process() 方法来做你想做的事情，以下是一个简单的示例:

class CustomAdapter(logging.LoggerAdapter):
    """
    This example adapter expects the passed in dict-like object to have a
    'connid' key, whose value in brackets is prepended to the log message.
    """
    def process(self, msg, kwargs):
        return '[%s] %s' % (self.extra['connid'], msg), kwargs

你可以这样使用:

logger = logging.getLogger(__name__)
adapter = CustomAdapter(logger, {'connid': some_conn_id})

然后，你记录在适配器中的任何事件消息前将添加``some_conn_id``的值。

使用除字典之外的其它对象传递上下文信息

你不需要将一个实际的字典传递给 LoggerAdapter-你可以传入一个实现了``__getitem__`` 和``__iter__``的类的实例，这样它就像是一个字典。这对于你想动态生成值（而字典中的值往往是常量）将很有帮助。

使用过滤器传递上下文信息

你也可以使用一个用户定义的类 Filter 在日志输出中添加上下文信息。Filter 的实例是被允许修改传入的 LogRecords，包括添加其他的属性，然后可以使用合适的格式化字符串输出，或者可以使用一个自定义的类 Formatter。

例如，在一个web应用程序中，正在处理的请求（或者至少是请求的一部分），可以存储在一个线程本地(threading.local) 变量中，然后从``Filter``中去访问。请求中的信息，如IP地址和用户名将被存储在``LogRecord``中，使用上例``LoggerAdapter``中的 ‘ip’ 和 ‘user’ 属性名。在这种情况下，可以使用相同的格式化字符串来得到上例中类似的输出结果。这是一段示例代码:

import logging
from random import choice

class ContextFilter(logging.Filter):
    """
    This is a filter which injects contextual information into the log.

    Rather than use actual contextual information, we just use random
    data in this demo.
    """

    USERS = ['jim', 'fred', 'sheila']
    IPS = ['123.231.231.123', '127.0.0.1', '192.168.0.1']

    def filter(self, record):

        record.ip = choice(ContextFilter.IPS)
        record.user = choice(ContextFilter.USERS)
        return True

if __name__ == '__main__':
    levels = (logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL)
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)-15s %(name)-5s %(levelname)-8s IP: %(ip)-15s User: %(user)-8s %(message)s')
    a1 = logging.getLogger('a.b.c')
    a2 = logging.getLogger('d.e.f')

    f = ContextFilter()
    a1.addFilter(f)
    a2.addFilter(f)
    a1.debug('A debug message')
    a1.info('An info message with %s', 'some parameters')
    for x in range(10):
        lvl = choice(levels)
        lvlname = logging.getLevelName(lvl)
        a2.log(lvl, 'A message at %s level with %d %s', lvlname, 2, 'parameters')

在运行时，产生如下内容:

2010-09-06 22:38:15,292 a.b.c DEBUG    IP: 123.231.231.123 User: fred     A debug message
2010-09-06 22:38:15,300 a.b.c INFO     IP: 192.168.0.1     User: sheila   An info message with some parameters
2010-09-06 22:38:15,300 d.e.f CRITICAL IP: 127.0.0.1       User: sheila   A message at CRITICAL level with 2 parameters
2010-09-06 22:38:15,300 d.e.f ERROR    IP: 127.0.0.1       User: jim      A message at ERROR level with 2 parameters
2010-09-06 22:38:15,300 d.e.f DEBUG    IP: 127.0.0.1       User: sheila   A message at DEBUG level with 2 parameters
2010-09-06 22:38:15,300 d.e.f ERROR    IP: 123.231.231.123 User: fred     A message at ERROR level with 2 parameters
2010-09-06 22:38:15,300 d.e.f CRITICAL IP: 192.168.0.1     User: jim      A message at CRITICAL level with 2 parameters
2010-09-06 22:38:15,300 d.e.f CRITICAL IP: 127.0.0.1       User: sheila   A message at CRITICAL level with 2 parameters
2010-09-06 22:38:15,300 d.e.f DEBUG    IP: 192.168.0.1     User: jim      A message at DEBUG level with 2 parameters
2010-09-06 22:38:15,301 d.e.f ERROR    IP: 127.0.0.1       User: sheila   A message at ERROR level with 2 parameters
2010-09-06 22:38:15,301 d.e.f DEBUG    IP: 123.231.231.123 User: fred     A message at DEBUG level with 2 parameters
2010-09-06 22:38:15,301 d.e.f INFO     IP: 123.231.231.123 User: fred     A message at INFO level with 2 parameters

从多个进程记录至单个文件

尽管 logging 是线程安全的，将单个进程中的多个线程日志记录至单个文件也 是 受支持的，但将 多个进程 中的日志记录至单个文件则 不是 受支持的，因为在 Python 中并没有在多个进程中实现对单个文件访问的序列化的标准方案。 如果你需要将多个进程中的日志记录至单个文件，有一个方案是让所有进程都将日志记录至一个 SocketHandler，然后用一个实现了套接字服务器的单独进程一边从套接字中读取一边将日志记录至文件。 （如果愿意的话，你可以在一个现有进程中专门开一个线程来执行此项功能。） 这一部分 文档对此方式有更详细的介绍，并包含一个可用的套接字接收器，你自己的应用可以在此基础上进行适配。

如果你使用的是包含了 multiprocessing 模块的较新版本的 Python，你也可以使用 Lock 来编写自己的处理程序让其从多个进程中按顺序记录至文件。 现有的 FileHandler 和它的子类目前没有使用 multiprocessing，尽管将来可能会这样做。 请注意目前 multiprocessing 模块并非在所有平台上提供可用的锁功能 (参见 https://bugs.python.org/issue3770)。

或者，你也可以使用 Queue 和 QueueHandler 将所有的日志事件发送至你的多进程应用的一个进程中。 以下示例脚本演示了如何执行此操作。 在示例中，一个单独的监听进程负责监听其他进程的日志事件，并根据自己的配置记录。 尽管示例只演示了这种方法（例如你可能希望使用单独的监听线程而非监听进程 —— 它们的实现是类似的），但你也可以在应用程序的监听进程和其他进程使用不同的配置，它可以作为满足你特定需求的一个基础:

# You'll need these imports in your own code
import logging
import logging.handlers
import multiprocessing

# Next two import lines for this demo only
from random import choice, random
import time

#
# Because you'll want to define the logging configurations for listener and workers, the
# listener and worker process functions take a configurer parameter which is a callable
# for configuring logging for that process. These functions are also passed the queue,
# which they use for communication.
#
# In practice, you can configure the listener however you want, but note that in this
# simple example, the listener does not apply level or filter logic to received records.
# In practice, you would probably want to do this logic in the worker processes, to avoid
# sending events which would be filtered out between processes.
#
# The size of the rotated files is made small so you can see the results easily.
def listener_configurer():
    root = logging.getLogger()
    h = logging.handlers.RotatingFileHandler('mptest.log', 'a', 300, 10)
    f = logging.Formatter('%(asctime)s %(processName)-10s %(name)s %(levelname)-8s %(message)s')
    h.setFormatter(f)
    root.addHandler(h)

# This is the listener process top-level loop: wait for logging events
# (LogRecords)on the queue and handle them, quit when you get a None for a
# LogRecord.
def listener_process(queue, configurer):
    configurer()
    while True:
        try:
            record = queue.get()
            if record is None:  # We send this as a sentinel to tell the listener to quit.
                break
            logger = logging.getLogger(record.name)
            logger.handle(record)  # No level or filter logic applied - just do it!
        except Exception:
            import sys, traceback
            print('Whoops! Problem:', file=sys.stderr)
            traceback.print_exc(file=sys.stderr)

# Arrays used for random selections in this demo

LEVELS = [logging.DEBUG, logging.INFO, logging.WARNING,
          logging.ERROR, logging.CRITICAL]

LOGGERS = ['a.b.c', 'd.e.f']

MESSAGES = [
    'Random message #1',
    'Random message #2',
    'Random message #3',
]

# The worker configuration is done at the start of the worker process run.
# Note that on Windows you can't rely on fork semantics, so each process
# will run the logging configuration code when it starts.
def worker_configurer(queue):
    h = logging.handlers.QueueHandler(queue)  # Just the one handler needed
    root = logging.getLogger()
    root.addHandler(h)
    # send all messages, for demo; no other level or filter logic applied.
    root.setLevel(logging.DEBUG)

# This is the worker process top-level loop, which just logs ten events with
# random intervening delays before terminating.
# The print messages are just so you know it's doing something!
def worker_process(queue, configurer):
    configurer(queue)
    name = multiprocessing.current_process().name
    print('Worker started: %s' % name)
    for i in range(10):
        time.sleep(random())
        logger = logging.getLogger(choice(LOGGERS))
        level = choice(LEVELS)
        message = choice(MESSAGES)
        logger.log(level, message)
    print('Worker finished: %s' % name)

# Here's where the demo gets orchestrated. Create the queue, create and start
# the listener, create ten workers and start them, wait for them to finish,
# then send a None to the queue to tell the listener to finish.
def main():
    queue = multiprocessing.Queue(-1)
    listener = multiprocessing.Process(target=listener_process,
                                       args=(queue, listener_configurer))
    listener.start()
    workers = []
    for i in range(10):
        worker = multiprocessing.Process(target=worker_process,
                                         args=(queue, worker_configurer))
        workers.append(worker)
        worker.start()
    for w in workers:
        w.join()
    queue.put_nowait(None)
    listener.join()

if __name__ == '__main__':
    main()

上面脚本的一个变种，仍然在主进程中记录日志，但使用一个单独的线程:

import logging
import logging.config
import logging.handlers
from multiprocessing import Process, Queue
import random
import threading
import time

def logger_thread(q):
    while True:
        record = q.get()
        if record is None:
            break
        logger = logging.getLogger(record.name)
        logger.handle(record)

def worker_process(q):
    qh = logging.handlers.QueueHandler(q)
    root = logging.getLogger()
    root.setLevel(logging.DEBUG)
    root.addHandler(qh)
    levels = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR,
              logging.CRITICAL]
    loggers = ['foo', 'foo.bar', 'foo.bar.baz',
               'spam', 'spam.ham', 'spam.ham.eggs']
    for i in range(100):
        lvl = random.choice(levels)
        logger = logging.getLogger(random.choice(loggers))
        logger.log(lvl, 'Message no. %d', i)

if __name__ == '__main__':
    q = Queue()
    d = {
        'version': 1,
        'formatters': {
            'detailed': {
                'class': 'logging.Formatter',
                'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
            }
        },
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
                'level': 'INFO',
            },
            'file': {
                'class': 'logging.FileHandler',
                'filename': 'mplog.log',
                'mode': 'w',
                'formatter': 'detailed',
            },
            'foofile': {
                'class': 'logging.FileHandler',
                'filename': 'mplog-foo.log',
                'mode': 'w',
                'formatter': 'detailed',
            },
            'errors': {
                'class': 'logging.FileHandler',
                'filename': 'mplog-errors.log',
                'mode': 'w',
                'level': 'ERROR',
                'formatter': 'detailed',
            },
        },
        'loggers': {
            'foo': {
                'handlers': ['foofile']
            }
        },
        'root': {
            'level': 'DEBUG',
            'handlers': ['console', 'file', 'errors']
        },
    }
    workers = []
    for i in range(5):
        wp = Process(target=worker_process, name='worker %d' % (i + 1), args=(q,))
        workers.append(wp)
        wp.start()
    logging.config.dictConfig(d)
    lp = threading.Thread(target=logger_thread, args=(q,))
    lp.start()
    # At this point, the main process could do some useful work of its own
    # Once it's done that, it can wait for the workers to terminate...
    for wp in workers:
        wp.join()
    # And now tell the logging thread to finish up, too
    q.put(None)
    lp.join()

这段变种的代码展示了如何使用特定的日志记录配置 - 例如``foo``记录器使用了特殊的处理程序，将 foo 子系统中所有的事件记录至一个文件 mplog-foo.log。在主进程（即使是在工作进程中产生的日志事件）的日志记录机制中将直接使用恰当的配置。

轮换日志文件

有时，你希望当日志文件不断记录增长至一定大小时，打开一个新的文件接着记录。 你可能希望只保留一定数量的日志文件，当不断的创建文件到达该数量时，又覆盖掉最开始的文件形成循环。 对于这种使用场景，日志包提供了 RotatingFileHandler:

import glob
import logging
import logging.handlers

LOG_FILENAME = 'logging_rotatingfile_example.out'

# Set up a specific logger with our desired output level
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)

# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(
              LOG_FILENAME, maxBytes=20, backupCount=5)

my_logger.addHandler(handler)

# Log some messages
for i in range(20):
    my_logger.debug('i = %d' % i)

# See what files are created
logfiles = glob.glob('%s*' % LOG_FILENAME)

for filename in logfiles:
    print(filename)

结果应该是6个单独的文件，每个文件都包含了应用程序的部分历史日志:

logging_rotatingfile_example.out
logging_rotatingfile_example.out.1
logging_rotatingfile_example.out.2
logging_rotatingfile_example.out.3
logging_rotatingfile_example.out.4
logging_rotatingfile_example.out.5

最新的文件始终是:file:logging_rotatingfile_example.out，每次到达大小限制时，都会使用后缀``.1``重命名。每个现有的备份文件都会被重命名并增加其后缀（例如``.1`` 变为``.2``），而``.6``文件会被删除掉。

显然，这个例子将日志长度设置得太小，这是一个极端的例子。 你可能希望将*maxBytes*设置为一个合适的值。

使用其他日志格式化方式

当日志模块被添加至 Python 标准库时，只有一种格式化消息内容的方法即 %-formatting。 在那之后，Python 又增加了两种格式化方法: string.Template (在 Python 2.4 中新增) 和 str.format() (在 Python 2.6 中新增)。

日志（从 3.2 开始）为这两种格式化方式提供了更多支持。Formatter 类可以添加一个额外的可选关键字参数 style。它的默认值是 '%'，其他的值 '{' 和 '$' 也支持，对应了其他两种格式化样式。其保持了向后兼容（如您所愿），但通过显示指定样式参数，你可以指定格式化字符串的方式是使用 str.format() 或 string.Template。 这里是一个控制台会话的示例，展示了这些方式：

>>> import logging
>>> root = logging.getLogger()
>>> root.setLevel(logging.DEBUG)
>>> handler = logging.StreamHandler()
>>> bf = logging.Formatter('{asctime} {name} {levelname:8s} {message}',
...                        style='{')
>>> handler.setFormatter(bf)
>>> root.addHandler(handler)
>>> logger = logging.getLogger('foo.bar')
>>> logger.debug('This is a DEBUG message')
2010-10-28 15:11:55,341 foo.bar DEBUG    This is a DEBUG message
>>> logger.critical('This is a CRITICAL message')
2010-10-28 15:12:11,526 foo.bar CRITICAL This is a CRITICAL message
>>> df = logging.Formatter('$asctime $name ${levelname} $message',
...                        style='$')
>>> handler.setFormatter(df)
>>> logger.debug('This is a DEBUG message')
2010-10-28 15:13:06,924 foo.bar DEBUG This is a DEBUG message
>>> logger.critical('This is a CRITICAL message')
2010-10-28 15:13:11,494 foo.bar CRITICAL This is a CRITICAL message
>>>

请注意最终输出到日志的消息格式完全独立于单条日志消息的构造方式。 它仍然可以使用 %-formatting，如下所示:

>>> logger.error('This is an%s %s %s', 'other,', 'ERROR,', 'message')
2010-10-28 15:19:29,833 foo.bar ERROR This is another, ERROR, message
>>>

Logging calls (logger.debug(), logger.info() etc.) only take positional parameters for the actual logging message itself, with keyword parameters used only for determining options for how to handle the actual logging call (e.g. the exc_info keyword parameter to indicate that traceback information should be logged, or the extra keyword parameter to indicate additional contextual information to be added to the log). So you cannot directly make logging calls using str.format() or string.Template syntax, because internally the logging package uses %-formatting to merge the format string and the variable arguments. There would no changing this while preserving backward compatibility, since all logging calls which are out there in existing code will be using %-format strings.

There is, however, a way that you can use {}- and $- formatting to construct your individual log messages. Recall that for a message you can use an arbitrary object as a message format string, and that the logging package will call str() on that object to get the actual format string. Consider the following two classes:

class BraceMessage:
    def __init__(self, fmt, *args, **kwargs):
        self.fmt = fmt
        self.args = args
        self.kwargs = kwargs

    def __str__(self):
        return self.fmt.format(*self.args, **self.kwargs)

class DollarMessage:
    def __init__(self, fmt, **kwargs):
        self.fmt = fmt
        self.kwargs = kwargs

    def __str__(self):
        from string import Template
        return Template(self.fmt).substitute(**self.kwargs)

Either of these can be used in place of a format string, to allow {}- or $-formatting to be used to build the actual “message” part which appears in the formatted log output in place of “%(message)s” or “{message}” or “$message”. It’s a little unwieldy to use the class names whenever you want to log something, but it’s quite palatable if you use an alias such as __ (double underscore — not to be confused with _, the single underscore used as a synonym/alias for gettext.gettext() or its brethren).

The above classes are not included in Python, though they’re easy enough to copy and paste into your own code. They can be used as follows (assuming that they’re declared in a module called wherever):

>>> from wherever import BraceMessage as __
>>> print(__('Message with {0} {name}', 2, name='placeholders'))
Message with 2 placeholders
>>> class Point: pass
...
>>> p = Point()
>>> p.x = 0.5
>>> p.y = 0.5
>>> print(__('Message with coordinates: ({point.x:.2f}, {point.y:.2f})',
...       point=p))
Message with coordinates: (0.50, 0.50)
>>> from wherever import DollarMessage as __
>>> print(__('Message with $num $what', num=2, what='placeholders'))
Message with 2 placeholders
>>>

While the above examples use print() to show how the formatting works, you would of course use logger.debug() or similar to actually log using this approach.

One thing to note is that you pay no significant performance penalty with this approach: the actual formatting happens not when you make the logging call, but when (and if) the logged message is actually about to be output to a log by a handler. So the only slightly unusual thing which might trip you up is that the parentheses go around the format string and the arguments, not just the format string. That’s because the __ notation is just syntax sugar for a constructor call to one of the XXXMessage classes.

If you prefer, you can use a LoggerAdapter to achieve a similar effect to the above, as in the following example:

import logging

class Message(object):
    def __init__(self, fmt, args):
        self.fmt = fmt
        self.args = args

    def __str__(self):
        return self.fmt.format(*self.args)

class StyleAdapter(logging.LoggerAdapter):
    def __init__(self, logger, extra=None):
        super(StyleAdapter, self).__init__(logger, extra or {})

    def log(self, level, msg, *args, **kwargs):
        if self.isEnabledFor(level):
            msg, kwargs = self.process(msg, kwargs)
            self.logger._log(level, Message(msg, args), (), **kwargs)

logger = StyleAdapter(logging.getLogger(__name__))

def main():
    logger.debug('Hello, {}', 'world!')

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    main()

The above script should log the message Hello, world! when run with Python 3.2 or later.

Customizing LogRecord

Every logging event is represented by a LogRecord instance. When an event is logged and not filtered out by a logger’s level, a LogRecord is created, populated with information about the event and then passed to the handlers for that logger (and its ancestors, up to and including the logger where further propagation up the hierarchy is disabled). Before Python 3.2, there were only two places where this creation was done:

Logger.makeRecord(), which is called in the normal process of logging an event. This invoked LogRecord directly to create an instance.

makeLogRecord(), which is called with a dictionary containing attributes to be added to the LogRecord. This is typically invoked when a suitable dictionary has been received over the network (e.g. in pickle form via a SocketHandler, or in JSON form via an HTTPHandler).

This has usually meant that if you need to do anything special with a LogRecord, you’ve had to do one of the following.

Create your own Logger subclass, which overrides Logger.makeRecord(), and set it using setLoggerClass() before any loggers that you care about are instantiated.

Add a Filter to a logger or handler, which does the necessary special manipulation you need when its filter() method is called.

The first approach would be a little unwieldy in the scenario where (say) several different libraries wanted to do different things. Each would attempt to set its own Logger subclass, and the one which did this last would win.

The second approach works reasonably well for many cases, but does not allow you to e.g. use a specialized subclass of LogRecord. Library developers can set a suitable filter on their loggers, but they would have to remember to do this every time they introduced a new logger (which they would do simply by adding new packages or modules and doing

logger = logging.getLogger(__name__)

at module level). It’s probably one too many things to think about. Developers could also add the filter to a NullHandler attached to their top-level logger, but this would not be invoked if an application developer attached a handler to a lower-level library logger — so output from that handler would not reflect the intentions of the library developer.

In Python 3.2 and later, LogRecord creation is done through a factory, which you can specify. The factory is just a callable you can set with setLogRecordFactory(), and interrogate with getLogRecordFactory(). The factory is invoked with the same signature as the LogRecord constructor, as LogRecord is the default setting for the factory.

This approach allows a custom factory to control all aspects of LogRecord creation. For example, you could return a subclass, or just add some additional attributes to the record once created, using a pattern similar to this:

old_factory = logging.getLogRecordFactory()

def record_factory(*args, **kwargs):
    record = old_factory(*args, **kwargs)
    record.custom_attribute = 0xdecafbad
    return record

logging.setLogRecordFactory(record_factory)

This pattern allows different libraries to chain factories together, and as long as they don’t overwrite each other’s attributes or unintentionally overwrite the attributes provided as standard, there should be no surprises. However, it should be borne in mind that each link in the chain adds run-time overhead to all logging operations, and the technique should only be used when the use of a Filter does not provide the desired result.

Subclassing QueueHandler - a ZeroMQ example

You can use a QueueHandler subclass to send messages to other kinds of queues, for example a ZeroMQ ‘publish’ socket. In the example below,the socket is created separately and passed to the handler (as its ‘queue’):

import zmq   # using pyzmq, the Python binding for ZeroMQ
import json  # for serializing records portably

ctx = zmq.Context()
sock = zmq.Socket(ctx, zmq.PUB)  # or zmq.PUSH, or other suitable value
sock.bind('tcp://*:5556')        # or wherever

class ZeroMQSocketHandler(QueueHandler):
    def enqueue(self, record):
        self.queue.send_json(record.__dict__)

handler = ZeroMQSocketHandler(sock)

Of course there are other ways of organizing this, for example passing in the data needed by the handler to create the socket:

class ZeroMQSocketHandler(QueueHandler):
    def __init__(self, uri, socktype=zmq.PUB, ctx=None):
        self.ctx = ctx or zmq.Context()
        socket = zmq.Socket(self.ctx, socktype)
        socket.bind(uri)
        super().__init__(socket)

    def enqueue(self, record):
        self.queue.send_json(record.__dict__)

    def close(self):
        self.queue.close()

Subclassing QueueListener - a ZeroMQ example

You can also subclass QueueListener to get messages from other kinds of queues, for example a ZeroMQ ‘subscribe’ socket. Here’s an example:

class ZeroMQSocketListener(QueueListener):
    def __init__(self, uri, *handlers, **kwargs):
        self.ctx = kwargs.get('ctx') or zmq.Context()
        socket = zmq.Socket(self.ctx, zmq.SUB)
        socket.setsockopt_string(zmq.SUBSCRIBE, '')  # subscribe to everything
        socket.connect(uri)
        super().__init__(socket, *handlers, **kwargs)

    def dequeue(self):
        msg = self.queue.recv_json()
        return logging.makeLogRecord(msg)

参见

模块 logging

日志记录模块的 API 参考。

模块 logging.config

日志记录模块的配置 API 。

模块 logging.handlers

日志记录模块附带的有用处理程序。

A basic logging tutorial

A more advanced logging tutorial

An example dictionary-based configuration

Below is an example of a logging configuration dictionary - it’s taken from the documentation on the Django project [https://docs.djangoproject.com/en/1.9/topics/logging/#configuring-logging]. This dictionary is passed to dictConfig() to put the configuration into effect:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'filters': {
        'special': {
            '()': 'project.logging.SpecialFilter',
            'foo': 'bar',
        }
    },
    'handlers': {
        'null': {
            'level':'DEBUG',
            'class':'django.utils.log.NullHandler',
        },
        'console':{
            'level':'DEBUG',
            'class':'logging.StreamHandler',
            'formatter': 'simple'
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'filters': ['special']
        }
    },
    'loggers': {
        'django': {
            'handlers':['null'],
            'propagate': True,
            'level':'INFO',
        },
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': False,
        },
        'myproject.custom': {
            'handlers': ['console', 'mail_admins'],
            'level': 'INFO',
            'filters': ['special']
        }
    }
}

For more information about this configuration, you can see the relevant section [https://docs.djangoproject.com/en/1.9/topics/logging/#configuring-logging] of the Django documentation.

Using a rotator and namer to customize log rotation processing

An example of how you can define a namer and rotator is given in the following snippet, which shows zlib-based compression of the log file:

def namer(name):
    return name + ".gz"

def rotator(source, dest):
    with open(source, "rb") as sf:
        data = sf.read()
        compressed = zlib.compress(data, 9)
        with open(dest, "wb") as df:
            df.write(compressed)
    os.remove(source)

rh = logging.handlers.RotatingFileHandler(...)
rh.rotator = rotator
rh.namer = namer

These are not “true” .gz files, as they are bare compressed data, with no “container” such as you’d find in an actual gzip file. This snippet is just for illustration purposes.

A more elaborate multiprocessing example

The following working example shows how logging can be used with multiprocessing using configuration files. The configurations are fairly simple, but serve to illustrate how more complex ones could be implemented in a real multiprocessing scenario.

In the example, the main process spawns a listener process and some worker processes. Each of the main process, the listener and the workers have three separate configurations (the workers all share the same configuration). We can see logging in the main process, how the workers log to a QueueHandler and how the listener implements a QueueListener and a more complex logging configuration, and arranges to dispatch events received via the queue to the handlers specified in the configuration. Note that these configurations are purely illustrative, but you should be able to adapt this example to your own scenario.

Here’s the script - the docstrings and the comments hopefully explain how it works:

import logging
import logging.config
import logging.handlers
from multiprocessing import Process, Queue, Event, current_process
import os
import random
import time

class MyHandler:
    """
    A simple handler for logging events. It runs in the listener process and
    dispatches events to loggers based on the name in the received record,
    which then get dispatched, by the logging system, to the handlers
    configured for those loggers.
    """
    def handle(self, record):
        logger = logging.getLogger(record.name)
        # The process name is transformed just to show that it's the listener
        # doing the logging to files and console
        record.processName = '%s (for %s)' % (current_process().name, record.processName)
        logger.handle(record)

def listener_process(q, stop_event, config):
    """
    This could be done in the main process, but is just done in a separate
    process for illustrative purposes.

    This initialises logging according to the specified configuration,
    starts the listener and waits for the main process to signal completion
    via the event. The listener is then stopped, and the process exits.
    """
    logging.config.dictConfig(config)
    listener = logging.handlers.QueueListener(q, MyHandler())
    listener.start()
    if os.name == 'posix':
        # On POSIX, the setup logger will have been configured in the
        # parent process, but should have been disabled following the
        # dictConfig call.
        # On Windows, since fork isn't used, the setup logger won't
        # exist in the child, so it would be created and the message
        # would appear - hence the "if posix" clause.
        logger = logging.getLogger('setup')
        logger.critical('Should not appear, because of disabled logger ...')
    stop_event.wait()
    listener.stop()

def worker_process(config):
    """
    A number of these are spawned for the purpose of illustration. In
    practice, they could be a heterogeneous bunch of processes rather than
    ones which are identical to each other.

    This initialises logging according to the specified configuration,
    and logs a hundred messages with random levels to randomly selected
    loggers.

    A small sleep is added to allow other processes a chance to run. This
    is not strictly needed, but it mixes the output from the different
    processes a bit more than if it's left out.
    """
    logging.config.dictConfig(config)
    levels = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR,
              logging.CRITICAL]
    loggers = ['foo', 'foo.bar', 'foo.bar.baz',
               'spam', 'spam.ham', 'spam.ham.eggs']
    if os.name == 'posix':
        # On POSIX, the setup logger will have been configured in the
        # parent process, but should have been disabled following the
        # dictConfig call.
        # On Windows, since fork isn't used, the setup logger won't
        # exist in the child, so it would be created and the message
        # would appear - hence the "if posix" clause.
        logger = logging.getLogger('setup')
        logger.critical('Should not appear, because of disabled logger ...')
    for i in range(100):
        lvl = random.choice(levels)
        logger = logging.getLogger(random.choice(loggers))
        logger.log(lvl, 'Message no. %d', i)
        time.sleep(0.01)

def main():
    q = Queue()
    # The main process gets a simple configuration which prints to the console.
    config_initial = {
        'version': 1,
        'formatters': {
            'detailed': {
                'class': 'logging.Formatter',
                'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
            }
        },
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
                'level': 'INFO',
            },
        },
        'root': {
            'level': 'DEBUG',
            'handlers': ['console']
        },
    }
    # The worker process configuration is just a QueueHandler attached to the
    # root logger, which allows all messages to be sent to the queue.
    # We disable existing loggers to disable the "setup" logger used in the
    # parent process. This is needed on POSIX because the logger will
    # be there in the child following a fork().
    config_worker = {
        'version': 1,
        'disable_existing_loggers': True,
        'handlers': {
            'queue': {
                'class': 'logging.handlers.QueueHandler',
                'queue': q,
            },
        },
        'root': {
            'level': 'DEBUG',
            'handlers': ['queue']
        },
    }
    # The listener process configuration shows that the full flexibility of
    # logging configuration is available to dispatch events to handlers however
    # you want.
    # We disable existing loggers to disable the "setup" logger used in the
    # parent process. This is needed on POSIX because the logger will
    # be there in the child following a fork().
    config_listener = {
        'version': 1,
        'disable_existing_loggers': True,
        'formatters': {
            'detailed': {
                'class': 'logging.Formatter',
                'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
            },
            'simple': {
                'class': 'logging.Formatter',
                'format': '%(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
            }
        },
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
                'level': 'INFO',
                'formatter': 'simple',
            },
            'file': {
                'class': 'logging.FileHandler',
                'filename': 'mplog.log',
                'mode': 'w',
                'formatter': 'detailed',
            },
            'foofile': {
                'class': 'logging.FileHandler',
                'filename': 'mplog-foo.log',
                'mode': 'w',
                'formatter': 'detailed',
            },
            'errors': {
                'class': 'logging.FileHandler',
                'filename': 'mplog-errors.log',
                'mode': 'w',
                'level': 'ERROR',
                'formatter': 'detailed',
            },
        },
        'loggers': {
            'foo': {
                'handlers': ['foofile']
            }
        },
        'root': {
            'level': 'DEBUG',
            'handlers': ['console', 'file', 'errors']
        },
    }
    # Log some initial events, just to show that logging in the parent works
    # normally.
    logging.config.dictConfig(config_initial)
    logger = logging.getLogger('setup')
    logger.info('About to create workers ...')
    workers = []
    for i in range(5):
        wp = Process(target=worker_process, name='worker %d' % (i + 1),
                     args=(config_worker,))
        workers.append(wp)
        wp.start()
        logger.info('Started worker: %s', wp.name)
    logger.info('About to create listener ...')
    stop_event = Event()
    lp = Process(target=listener_process, name='listener',
                 args=(q, stop_event, config_listener))
    lp.start()
    logger.info('Started listener')
    # We now hang around for the workers to finish their work.
    for wp in workers:
        wp.join()
    # Workers all done, listening can now stop.
    # Logging in the parent still works normally.
    logger.info('Telling listener to stop ...')
    stop_event.set()
    lp.join()
    logger.info('All done.')

if __name__ == '__main__':
    main()

Inserting a BOM into messages sent to a SysLogHandler

RFC 5424 [https://tools.ietf.org/html/rfc5424.html] requires that a Unicode message be sent to a syslog daemon as a set of bytes which have the following structure: an optional pure-ASCII component, followed by a UTF-8 Byte Order Mark (BOM), followed by Unicode encoded using UTF-8. (See the relevant section of the specification [https://tools.ietf.org/html/rfc5424.html#section-6].)

In Python 3.1, code was added to SysLogHandler to insert a BOM into the message, but unfortunately, it was implemented incorrectly, with the BOM appearing at the beginning of the message and hence not allowing any pure-ASCII component to appear before it.

As this behaviour is broken, the incorrect BOM insertion code is being removed from Python 3.2.4 and later. However, it is not being replaced, and if you want to produce RFC 5424 [https://tools.ietf.org/html/rfc5424.html]-compliant messages which include a BOM, an optional pure-ASCII sequence before it and arbitrary Unicode after it, encoded using UTF-8, then you need to do the following:

Attach a Formatter instance to your SysLogHandler instance, with a format string such as:

'ASCII section\ufeffUnicode section'

The Unicode code point U+FEFF, when encoded using UTF-8, will be encoded as a UTF-8 BOM – the byte-string b'\xef\xbb\xbf'.

Replace the ASCII section with whatever placeholders you like, but make sure that the data that appears in there after substitution is always ASCII (that way, it will remain unchanged after UTF-8 encoding).

Replace the Unicode section with whatever placeholders you like; if the data which appears there after substitution contains characters outside the ASCII range, that’s fine – it will be encoded using UTF-8.

The formatted message will be encoded using UTF-8 encoding by SysLogHandler. If you follow the above rules, you should be able to produce RFC 5424 [https://tools.ietf.org/html/rfc5424.html]-compliant messages. If you don’t, logging may not complain, but your messages will not be RFC 5424-compliant, and your syslog daemon may complain.

Implementing structured logging

Although most logging messages are intended for reading by humans, and thus not readily machine-parseable, there might be circumstances where you want to output messages in a structured format which is capable of being parsed by a program (without needing complex regular expressions to parse the log message). This is straightforward to achieve using the logging package. There are a number of ways in which this could be achieved, but the following is a simple approach which uses JSON to serialise the event in a machine-parseable manner:

import json
import logging

class StructuredMessage(object):
    def __init__(self, message, **kwargs):
        self.message = message
        self.kwargs = kwargs

    def __str__(self):
        return '%s >>> %s' % (self.message, json.dumps(self.kwargs))

_ = StructuredMessage   # optional, to improve readability

logging.basicConfig(level=logging.INFO, format='%(message)s')
logging.info(_('message 1', foo='bar', bar='baz', num=123, fnum=123.456))

If the above script is run, it prints:

message 1 >>> {"fnum": 123.456, "num": 123, "bar": "baz", "foo": "bar"}

Note that the order of items might be different according to the version of Python used.

If you need more specialised processing, you can use a custom JSON encoder, as in the following complete example:

from __future__ import unicode_literals

import json
import logging

# This next bit is to ensure the script runs unchanged on 2.x and 3.x
try:
    unicode
except NameError:
    unicode = str

class Encoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, set):
            return tuple(o)
        elif isinstance(o, unicode):
            return o.encode('unicode_escape').decode('ascii')
        return super(Encoder, self).default(o)

class StructuredMessage(object):
    def __init__(self, message, **kwargs):
        self.message = message
        self.kwargs = kwargs

    def __str__(self):
        s = Encoder().encode(self.kwargs)
        return '%s >>> %s' % (self.message, s)

_ = StructuredMessage   # optional, to improve readability

def main():
    logging.basicConfig(level=logging.INFO, format='%(message)s')
    logging.info(_('message 1', set_value={1, 2, 3}, snowman='\u2603'))

if __name__ == '__main__':
    main()

When the above script is run, it prints:

message 1 >>> {"snowman": "\u2603", "set_value": [1, 2, 3]}

Note that the order of items might be different according to the version of Python used.

Customizing handlers with dictConfig()

There are times when you want to customize logging handlers in particular ways, and if you use dictConfig() you may be able to do this without subclassing. As an example, consider that you may want to set the ownership of a log file. On POSIX, this is easily done using shutil.chown(), but the file handlers in the stdlib don’t offer built-in support. You can customize handler creation using a plain function such as:

def owned_file_handler(filename, mode='a', encoding=None, owner=None):
    if owner:
        if not os.path.exists(filename):
            open(filename, 'a').close()
        shutil.chown(filename, *owner)
    return logging.FileHandler(filename, mode, encoding)

You can then specify, in a logging configuration passed to dictConfig(), that a logging handler be created by calling this function:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'default': {
            'format': '%(asctime)s %(levelname)s %(name)s %(message)s'
        },
    },
    'handlers': {
        'file':{
            # The values below are popped from this dictionary and
            # used to create the handler, set the handler's level and
            # its formatter.
            '()': owned_file_handler,
            'level':'DEBUG',
            'formatter': 'default',
            # The values below are passed to the handler creator callable
            # as keyword arguments.
            'owner': ['pulse', 'pulse'],
            'filename': 'chowntest.log',
            'mode': 'w',
            'encoding': 'utf-8',
        },
    },
    'root': {
        'handlers': ['file'],
        'level': 'DEBUG',
    },
}

In this example I am setting the ownership using the pulse user and group, just for the purposes of illustration. Putting it together into a working script, chowntest.py:

import logging, logging.config, os, shutil

def owned_file_handler(filename, mode='a', encoding=None, owner=None):
    if owner:
        if not os.path.exists(filename):
            open(filename, 'a').close()
        shutil.chown(filename, *owner)
    return logging.FileHandler(filename, mode, encoding)

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'default': {
            'format': '%(asctime)s %(levelname)s %(name)s %(message)s'
        },
    },
    'handlers': {
        'file':{
            # The values below are popped from this dictionary and
            # used to create the handler, set the handler's level and
            # its formatter.
            '()': owned_file_handler,
            'level':'DEBUG',
            'formatter': 'default',
            # The values below are passed to the handler creator callable
            # as keyword arguments.
            'owner': ['pulse', 'pulse'],
            'filename': 'chowntest.log',
            'mode': 'w',
            'encoding': 'utf-8',
        },
    },
    'root': {
        'handlers': ['file'],
        'level': 'DEBUG',
    },
}

logging.config.dictConfig(LOGGING)
logger = logging.getLogger('mylogger')
logger.debug('A debug message')

To run this, you will probably need to run as root:

$ sudo python3.3 chowntest.py
$ cat chowntest.log
2013-11-05 09:34:51,128 DEBUG mylogger A debug message
$ ls -l chowntest.log
-rw-r--r-- 1 pulse pulse 55 2013-11-05 09:34 chowntest.log

Note that this example uses Python 3.3 because that’s where shutil.chown() makes an appearance. This approach should work with any Python version that supports dictConfig() - namely, Python 2.7, 3.2 or later. With pre-3.3 versions, you would need to implement the actual ownership change using e.g. os.chown().

In practice, the handler-creating function may be in a utility module somewhere in your project. Instead of the line in the configuration:

'()': owned_file_handler,

you could use e.g.:

'()': 'ext://project.util.owned_file_handler',

where project.util can be replaced with the actual name of the package where the function resides. In the above working script, using 'ext://__main__.owned_file_handler' should work. Here, the actual callable is resolved by dictConfig() from the ext:// specification.

This example hopefully also points the way to how you could implement other types of file change - e.g. setting specific POSIX permission bits - in the same way, using os.chmod().

Of course, the approach could also be extended to types of handler other than a FileHandler - for example, one of the rotating file handlers, or a different type of handler altogether.

Using particular formatting styles throughout your application

In Python 3.2, the Formatter gained a style keyword parameter which, while defaulting to % for backward compatibility, allowed the specification of { or $ to support the formatting approaches supported by str.format() and string.Template. Note that this governs the formatting of logging messages for final output to logs, and is completely orthogonal to how an individual logging message is constructed.

Logging calls (debug(), info() etc.) only take positional parameters for the actual logging message itself, with keyword parameters used only for determining options for how to handle the logging call (e.g. the exc_info keyword parameter to indicate that traceback information should be logged, or the extra keyword parameter to indicate additional contextual information to be added to the log). So you cannot directly make logging calls using str.format() or string.Template syntax, because internally the logging package uses %-formatting to merge the format string and the variable arguments. There would no changing this while preserving backward compatibility, since all logging calls which are out there in existing code will be using %-format strings.

There have been suggestions to associate format styles with specific loggers, but that approach also runs into backward compatibility problems because any existing code could be using a given logger name and using %-formatting.

For logging to work interoperably between any third-party libraries and your code, decisions about formatting need to be made at the level of the individual logging call. This opens up a couple of ways in which alternative formatting styles can be accommodated.

Using LogRecord factories

In Python 3.2, along with the Formatter changes mentioned above, the logging package gained the ability to allow users to set their own LogRecord subclasses, using the setLogRecordFactory() function. You can use this to set your own subclass of LogRecord, which does the Right Thing by overriding the getMessage() method. The base class implementation of this method is where the msg % args formatting happens, and where you can substitute your alternate formatting; however, you should be careful to support all formatting styles and allow %-formatting as the default, to ensure interoperability with other code. Care should also be taken to call str(self.msg), just as the base implementation does.

Refer to the reference documentation on setLogRecordFactory() and LogRecord for more information.

Using custom message objects

There is another, perhaps simpler way that you can use {}- and $- formatting to construct your individual log messages. You may recall (from 使用任意对象作为消息) that when logging you can use an arbitrary object as a message format string, and that the logging package will call str() on that object to get the actual format string. Consider the following two classes:

class BraceMessage(object):
    def __init__(self, fmt, *args, **kwargs):
        self.fmt = fmt
        self.args = args
        self.kwargs = kwargs

    def __str__(self):
        return self.fmt.format(*self.args, **self.kwargs)

class DollarMessage(object):
    def __init__(self, fmt, **kwargs):
        self.fmt = fmt
        self.kwargs = kwargs

    def __str__(self):
        from string import Template
        return Template(self.fmt).substitute(**self.kwargs)

Either of these can be used in place of a format string, to allow {}- or $-formatting to be used to build the actual “message” part which appears in the formatted log output in place of “%(message)s” or “{message}” or “$message”. If you find it a little unwieldy to use the class names whenever you want to log something, you can make it more palatable if you use an alias such as M or _ for the message (or perhaps __, if you are using _ for localization).

Examples of this approach are given below. Firstly, formatting with str.format():

>>> __ = BraceMessage
>>> print(__('Message with {0} {1}', 2, 'placeholders'))
Message with 2 placeholders
>>> class Point: pass
...
>>> p = Point()
>>> p.x = 0.5
>>> p.y = 0.5
>>> print(__('Message with coordinates: ({point.x:.2f}, {point.y:.2f})', point=p))
Message with coordinates: (0.50, 0.50)

Secondly, formatting with string.Template:

>>> __ = DollarMessage
>>> print(__('Message with $num $what', num=2, what='placeholders'))
Message with 2 placeholders
>>>

One thing to note is that you pay no significant performance penalty with this approach: the actual formatting happens not when you make the logging call, but when (and if) the logged message is actually about to be output to a log by a handler. So the only slightly unusual thing which might trip you up is that the parentheses go around the format string and the arguments, not just the format string. That’s because the __ notation is just syntax sugar for a constructor call to one of the XXXMessage classes shown above.

Configuring filters with dictConfig()

You can configure filters using dictConfig(), though it might not be obvious at first glance how to do it (hence this recipe). Since Filter is the only filter class included in the standard library, and it is unlikely to cater to many requirements (it’s only there as a base class), you will typically need to define your own Filter subclass with an overridden filter() method. To do this, specify the () key in the configuration dictionary for the filter, specifying a callable which will be used to create the filter (a class is the most obvious, but you can provide any callable which returns a Filter instance). Here is a complete example:

import logging
import logging.config
import sys

class MyFilter(logging.Filter):
    def __init__(self, param=None):
        self.param = param

    def filter(self, record):
        if self.param is None:
            allow = True
        else:
            allow = self.param not in record.msg
        if allow:
            record.msg = 'changed: ' + record.msg
        return allow

LOGGING = {
    'version': 1,
    'filters': {
        'myfilter': {
            '()': MyFilter,
            'param': 'noshow',
        }
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'filters': ['myfilter']
        }
    },
    'root': {
        'level': 'DEBUG',
        'handlers': ['console']
    },
}

if __name__ == '__main__':
    logging.config.dictConfig(LOGGING)
    logging.debug('hello')
    logging.debug('hello - noshow')

This example shows how you can pass configuration data to the callable which constructs the instance, in the form of keyword parameters. When run, the above script will print:

changed: hello

which shows that the filter is working as configured.

A couple of extra points to note:

If you can’t refer to the callable directly in the configuration (e.g. if it lives in a different module, and you can’t import it directly where the configuration dictionary is), you can use the form ext://... as described in 访问外部对象. For example, you could have used the text 'ext://__main__.MyFilter' instead of MyFilter in the above example.

As well as for filters, this technique can also be used to configure custom handlers and formatters. See 用户定义对象 for more information on how logging supports using user-defined objects in its configuration, and see the other cookbook recipe Customizing handlers with dictConfig() above.

Customized exception formatting

There might be times when you want to do customized exception formatting - for argument’s sake, let’s say you want exactly one line per logged event, even when exception information is present. You can do this with a custom formatter class, as shown in the following example:

import logging

class OneLineExceptionFormatter(logging.Formatter):
    def formatException(self, exc_info):
        """
        Format an exception so that it prints on a single line.
        """
        result = super(OneLineExceptionFormatter, self).formatException(exc_info)
        return repr(result)  # or format into one line however you want to

    def format(self, record):
        s = super(OneLineExceptionFormatter, self).format(record)
        if record.exc_text:
            s = s.replace('\n', '') + '|'
        return s

def configure_logging():
    fh = logging.FileHandler('output.txt', 'w')
    f = OneLineExceptionFormatter('%(asctime)s|%(levelname)s|%(message)s|',
                                  '%d/%m/%Y %H:%M:%S')
    fh.setFormatter(f)
    root = logging.getLogger()
    root.setLevel(logging.DEBUG)
    root.addHandler(fh)

def main():
    configure_logging()
    logging.info('Sample message')
    try:
        x = 1 / 0
    except ZeroDivisionError as e:
        logging.exception('ZeroDivisionError: %s', e)

if __name__ == '__main__':
    main()

When run, this produces a file with exactly two lines:

28/01/2015 07:21:23|INFO|Sample message|
28/01/2015 07:21:23|ERROR|ZeroDivisionError: integer division or modulo by zero|'Traceback (most recent call last):\n  File "logtest7.py", line 30, in main\n    x = 1 / 0\nZeroDivisionError: integer division or modulo by zero'|

While the above treatment is simplistic, it points the way to how exception information can be formatted to your liking. The traceback module may be helpful for more specialized needs.

Speaking logging messages

There might be situations when it is desirable to have logging messages rendered in an audible rather than a visible format. This is easy to do if you have text-to-speech (TTS) functionality available in your system, even if it doesn’t have a Python binding. Most TTS systems have a command line program you can run, and this can be invoked from a handler using subprocess. It’s assumed here that TTS command line programs won’t expect to interact with users or take a long time to complete, and that the frequency of logged messages will be not so high as to swamp the user with messages, and that it’s acceptable to have the messages spoken one at a time rather than concurrently, The example implementation below waits for one message to be spoken before the next is processed, and this might cause other handlers to be kept waiting. Here is a short example showing the approach, which assumes that the espeak TTS package is available:

import logging
import subprocess
import sys

class TTSHandler(logging.Handler):
    def emit(self, record):
        msg = self.format(record)
        # Speak slowly in a female English voice
        cmd = ['espeak', '-s150', '-ven+f3', msg]
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)
        # wait for the program to finish
        p.communicate()

def configure_logging():
    h = TTSHandler()
    root = logging.getLogger()
    root.addHandler(h)
    # the default formatter just returns the message
    root.setLevel(logging.DEBUG)

def main():
    logging.info('Hello')
    logging.debug('Goodbye')

if __name__ == '__main__':
    configure_logging()
    sys.exit(main())

When run, this script should say “Hello” and then “Goodbye” in a female voice.

The above approach can, of course, be adapted to other TTS systems and even other systems altogether which can process messages via external programs run from a command line.

缓冲日志消息并有条件地输出它们

在某些情况下，你可能希望在临时区域中记录日志消息，并且只在发生某种特定的情况下才输出它们。 例如，你可能希望起始在函数中记录调试事件，如果函数执行完成且没有错误，你不希望输出收集的调试信息以避免造成日志混乱，但如果出现错误，那么你希望所有调试以及错误消息被输出。

下面是一个示例，展示如何在你的日志记录函数上使用装饰器以实现这一功能。该示例使用 logging.handlers.MemoryHandler ，它允许缓冲已记录的事件直到某些条件发生，缓冲的事件才会被刷新（flushed） - 传递给另一个处理程序（ target handler）进行处理。 默认情况下， MemoryHandler 在其缓冲区被填满时被刷新，或者看到一个级别大于或等于指定阈值的事件。 如果想要自定义刷新行为，你可以通过更专业的 MemoryHandler 子类来使用这个秘诀。

这个示例脚本有一个简单的函数 foo ，它只是在所有的日志级别中循环运行，写到 sys.stderr ，说明它要记录在哪个级别上，然后在这个级别上实际记录一个消息。你可以给 foo 传递一个参数，如果为 true ，它将在ERROR和CRITICAL级别记录，否则，它只在DEBUG、INFO和WARNING级别记录。

The script just arranges to decorate foo with a decorator which will do the conditional logging that’s required. The decorator takes a logger as a parameter and attaches a memory handler for the duration of the call to the decorated function. The decorator can be additionally parameterised using a target handler, a level at which flushing should occur, and a capacity for the buffer. These default to a StreamHandler which writes to sys.stderr, logging.ERROR and 100 respectively.

以下是脚本：

import logging
from logging.handlers import MemoryHandler
import sys

logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())

def log_if_errors(logger, target_handler=None, flush_level=None, capacity=None):
    if target_handler is None:
        target_handler = logging.StreamHandler()
    if flush_level is None:
        flush_level = logging.ERROR
    if capacity is None:
        capacity = 100
    handler = MemoryHandler(capacity, flushLevel=flush_level, target=target_handler)

    def decorator(fn):
        def wrapper(*args, **kwargs):
            logger.addHandler(handler)
            try:
                return fn(*args, **kwargs)
            except Exception:
                logger.exception('call failed')
                raise
            finally:
                super(MemoryHandler, handler).flush()
                logger.removeHandler(handler)
        return wrapper

    return decorator

def write_line(s):
    sys.stderr.write('%s\n' % s)

def foo(fail=False):
    write_line('about to log at DEBUG ...')
    logger.debug('Actually logged at DEBUG')
    write_line('about to log at INFO ...')
    logger.info('Actually logged at INFO')
    write_line('about to log at WARNING ...')
    logger.warning('Actually logged at WARNING')
    if fail:
        write_line('about to log at ERROR ...')
        logger.error('Actually logged at ERROR')
        write_line('about to log at CRITICAL ...')
        logger.critical('Actually logged at CRITICAL')
    return fail

decorated_foo = log_if_errors(logger)(foo)

if __name__ == '__main__':
    logger.setLevel(logging.DEBUG)
    write_line('Calling undecorated foo with False')
    assert not foo(False)
    write_line('Calling undecorated foo with True')
    assert foo(True)
    write_line('Calling decorated foo with False')
    assert not decorated_foo(False)
    write_line('Calling decorated foo with True')
    assert decorated_foo(True)

运行此脚本时，应看到以下输出：

Calling undecorated foo with False
about to log at DEBUG ...
about to log at INFO ...
about to log at WARNING ...
Calling undecorated foo with True
about to log at DEBUG ...
about to log at INFO ...
about to log at WARNING ...
about to log at ERROR ...
about to log at CRITICAL ...
Calling decorated foo with False
about to log at DEBUG ...
about to log at INFO ...
about to log at WARNING ...
Calling decorated foo with True
about to log at DEBUG ...
about to log at INFO ...
about to log at WARNING ...
about to log at ERROR ...
Actually logged at DEBUG
Actually logged at INFO
Actually logged at WARNING
Actually logged at ERROR
about to log at CRITICAL ...
Actually logged at CRITICAL

如你所见，实际日志记录输出仅在消息等级为ERROR或更高的事件时发生，但在这种情况下，任何之前较低消息等级的事件还会被记录。

你当然可以使用传统的装饰方法:

@log_if_errors(logger)
def foo(fail=False):
    ...

通过配置使用UTC (GMT) 格式化时间

有时候，你希望使用UTC来格式化时间，这可以通过使用一个类来实现，例如`UTCFormatter`，如下所示：

import logging
import time

class UTCFormatter(logging.Formatter):
    converter = time.gmtime

然后你可以在你的代码中使用 UTCFormatter，而不是 Formatter。 如果你想通过配置来实现这一功能，你可以使用 dictConfig() API 来完成，该方法在以下完整示例中展示:

import logging
import logging.config
import time

class UTCFormatter(logging.Formatter):
    converter = time.gmtime

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'utc': {
            '()': UTCFormatter,
            'format': '%(asctime)s %(message)s',
        },
        'local': {
            'format': '%(asctime)s %(message)s',
        }
    },
    'handlers': {
        'console1': {
            'class': 'logging.StreamHandler',
            'formatter': 'utc',
        },
        'console2': {
            'class': 'logging.StreamHandler',
            'formatter': 'local',
        },
    },
    'root': {
        'handlers': ['console1', 'console2'],
   }
}

if __name__ == '__main__':
    logging.config.dictConfig(LOGGING)
    logging.warning('The local time is %s', time.asctime())

脚本会运行输出类似下面的内容:

2015-10-17 12:53:29,501 The local time is Sat Oct 17 13:53:29 2015
2015-10-17 13:53:29,501 The local time is Sat Oct 17 13:53:29 2015

展示了如何将时间格式化为本地时间和UTC两种形式，其中每种形式对应一个日志处理器 。

使用上下文管理器的可选的日志记录

有时候，我们需要暂时更改日志配置，并在执行某些操作后将其还原。为此，上下文管理器是实现保存和恢复日志上下文的最明显的方式。这是一个关于上下文管理器的简单例子，它允许你在上下文管理器的作用域内更改日志记录等级以及增加日志处理器：

import logging
import sys

class LoggingContext(object):
    def __init__(self, logger, level=None, handler=None, close=True):
        self.logger = logger
        self.level = level
        self.handler = handler
        self.close = close

    def __enter__(self):
        if self.level is not None:
            self.old_level = self.logger.level
            self.logger.setLevel(self.level)
        if self.handler:
            self.logger.addHandler(self.handler)

    def __exit__(self, et, ev, tb):
        if self.level is not None:
            self.logger.setLevel(self.old_level)
        if self.handler:
            self.logger.removeHandler(self.handler)
        if self.handler and self.close:
            self.handler.close()
        # implicit return of None => don't swallow exceptions

如果指定上下文管理器的日志记录等级属性，则在上下文管理器的with语句所涵盖的代码中，日志记录器的记录等级将临时设置为上下文管理器所配置的日志记录等级。 如果指定上下文管理的日志处理器属性，则该句柄在进入上下文管理器的上下文时添加到记录器中，并在退出时被删除。 如果你再也不需要该日志处理器时，你可以让上下文管理器在退出上下文管理器的上下文时关闭它。

为了说明它是如何工作的，我们可以在上面添加以下代码块:

if __name__ == '__main__':
    logger = logging.getLogger('foo')
    logger.addHandler(logging.StreamHandler())
    logger.setLevel(logging.INFO)
    logger.info('1. This should appear just once on stderr.')
    logger.debug('2. This should not appear.')
    with LoggingContext(logger, level=logging.DEBUG):
        logger.debug('3. This should appear once on stderr.')
    logger.debug('4. This should not appear.')
    h = logging.StreamHandler(sys.stdout)
    with LoggingContext(logger, level=logging.DEBUG, handler=h, close=True):
        logger.debug('5. This should appear twice - once on stderr and once on stdout.')
    logger.info('6. This should appear just once on stderr.')
    logger.debug('7. This should not appear.')

我们最初设置日志记录器的消息等级为``INFO``，因此消息#1出现，消息#2没有出现。在接下来的``with``代码块中我们暂时将消息等级变更为``DEBUG``，从而消息#3出现。在这一代码块退出后，日志记录器的消息等级恢复为``INFO``，从而消息#4没有出现。在下一个``with``代码块中，我们再一次将设置消息等级设置为``DEBUG``，同时添加一个将消息写入``sys.stdout``的日志处理器。因此，消息#5在控制台出现两次（分别通过``stderr``和``stdout``）。在``with``语句完成后，状态与之前一样，因此消息#6出现（类似消息#1），而消息#7没有出现（类似消息#2）。

如果我们运行生成的脚本，结果如下：

$ python logctx.py
1. This should appear just once on stderr.
3. This should appear once on stderr.
5. This should appear twice - once on stderr and once on stdout.
5. This should appear twice - once on stderr and once on stdout.
6. This should appear just once on stderr.

我们将``stderr``标准错误重定向到``/dev/null``，我再次运行生成的脚步，唯一被写入``stdout``标准输出的消息，即我们所能看见的消息，如下：

$ python logctx.py 2>/dev/null
5. This should appear twice - once on stderr and once on stdout.

再一次，将``stdout``标准输出重定向到``/dev/null``，我获得如下结果：

$ python logctx.py >/dev/null
1. This should appear just once on stderr.
3. This should appear once on stderr.
5. This should appear twice - once on stderr and once on stdout.
6. This should appear just once on stderr.

在这种情况下，与预期一致，打印到``stdout``标准输出的消息＃5不会出现。

当然，这里描述的方法可以被推广，例如临时附加日志记录过滤器。 请注意，上面的代码适用于Python 2以及Python 3。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

正则表达式HOWTO

作者

A.M. Kuchling <amk@amk.ca>

摘要

本文档是在Python中使用 re 模块使用正则表达式的入门教程。 它提供了比“标准库参考”中相应部分更平和的介绍。

概述

正则表达式（称为RE，或正则，或正则表达式模式）本质上是嵌入在Python中的一种微小的、高度专业化的编程语言，可通过 re 模块获得。 使用这种小语言，你可以为要匹配的可能字符串集指定规则；此集可能包含英语句子，电子邮件地址，TeX命令或你喜欢的任何内容。 然后，您可以询问诸如“此字符串是否与模式匹配？”或“此字符串中的模式是否匹配？”等问题。 你还可以使用正则修改字符串或以各种方式将其拆分。

正则表达式模式被编译成一系列字节码，然后由用 C 编写的匹配引擎执行。对于高级用途，可能需要特别注意引擎如何执行给定的正则，并将正则写入以某种方式生成运行速度更快的字节码。 本文档未涉及优化，因为它要求你充分了解匹配引擎的内部结构。

正则表达式语言相对较小且受限制，因此并非所有可能的字符串处理任务都可以使用正则表达式完成。 还有一些任务 可以 用正则表达式完成，但表达式变得非常复杂。 在这些情况下，你最好编写 Python 代码来进行处理；虽然 Python 代码比精心设计的正则表达式慢，但它也可能更容易理解。

简单模式

我们首先要了解最简单的正则表达式。 由于正则表达式用于对字符串进行操作，因此我们将从最常见的任务开始：匹配字符。

有关正则表达式（确定性和非确定性有限自动机）的计算机科学的详细解释，你可以参考几乎所有有关编写编译器的教科书。

匹配字符

大多数字母和字符只会匹配自己。 例如，正则表达式 test 将完全匹配字符串 test 。 （你可以启用一个不区分大小写的模式，让这个正则匹配 Test 或 TEST，稍后会详细介绍。）

这条规则有例外；一些字符是特殊的 metacharacters ，并且不匹配自己。 相反，它们表示应该匹配一些与众不同的东西，或者通过重复它们或改变它们的含义来影响正则的其他部分。 本文档的大部分内容都致力于讨论各种元字符及其功能。

这是元字符的完整列表；它们的意思将在本HOWTO的其余部分讨论。

. ^ $ * + ? { } [ ] \ | ( )

我们将看到的第一个元字符是 [``和]``。 它们用于指定字符类，它是你希望匹配的一组字符。 可以单独列出字符，也可以通过给出两个字符并用 '-' 标记将它们分开来表示一系列字符。 例如，[abc] 将匹配任何字符 a、b 或 c; 这与 [a-c] 相同，它使用一个范围来表示同一组字符。 如果你只想匹配小写字母，你的正则是 [a-z]。

字符类中的元字符不生效。 例如，[akm$] 将匹配``’a’，’k’、’m’或’$’`` 中的任意字符；'$' 通常是一个元字符，但在一个字符类中它被剥夺了特殊性。

You can match the characters not listed within the class by complementing the set. This is indicated by including a '^' as the first character of the class; '^' outside a character class will simply match the '^' character. For example, [^5] will match any character except '5'.

也许最重要的元字符是反斜杠，\。 与 Python 字符串文字一样，反斜杠后面可以跟各种字符，以指示各种特殊序列。它也用于转义所有元字符，因此您仍然可以在模式中匹配它们；例如，如果你需要匹配 [ 或 \，你可以在它们前面加一个反斜杠来移除它们的特殊含义：\[ 或 \\。

一些以 '\' 开头的特殊序列表示通常有用的预定义字符集，例如数字集、字母集或任何非空格的集合。

让我们举一个例子：\w 匹配任何字母数字字符。 如果正则表达式模式以字节类表示，这相当于类 [a-zA-Z0-9_]。如果正则表达式是一个字符串，\w 将匹配由 unicodedata 模块提供的 Unicode 数据库中标记为字母的所有字符。 通过在编译正则表达式时提供 re.ASCII 标志，可以在字符串模式中使用更为受限制的 \w 定义。

以下特殊序列列表不完整。 有关 Unicode 字符串模式的序列和扩展类定义的完整列表，请参阅标准库参考中的最后一部分 正则表达式语法 。通常，Unicode 版本匹配 Unicode 数据库中相应类别中的任何字符。

\d

匹配任何十进制数字；这等价于类 [0-9]。

\D

匹配任何非数字字符；这等价于类 [^0-9]。

\s

匹配任何空白字符；这等价于类 [ \t\n\r\f\v]。

\S

匹配任何非空白字符；这相当于类 [^ \t\n\r\f\v]。

\w

匹配任何字母与数字字符；这相当于类 [a-zA-Z0-9_]。

\W

匹配任何非字母与数字字符；这相当于类 [^a-zA-Z0-9_]。

这些序列可以包含在字符类中。 例如，[\s,.] 是一个匹配任何空格字符的字符类或者 ',' ，或 '.'。

本节的最后一个元字符是 . 。 它匹配除换行符之外的任何内容，并且有一个可选模式（ re.DOTALL ）甚至可以匹配换行符。 . 常用于你想匹配“任何字符”的地方。

重复

能够匹配不同的字符集合是正则表达式可以做的第一件事，这对于字符串可用方法来说是不可能的。 但是，如果这是正则表达式的唯一额外功能，那么它们就不会有太大的优势。 另一个功能是你可以指定正则的某些部分必须重复一定次数。

重复中我们要了解的第一个元字符是 *。 * 与字面字符 '*' 不匹配；相反，它指定前一个字符可以匹配零次或多次，而不是恰好一次。

例如，ca*t 将匹配 'ct' (0个 'a' 字符)，'cat' (1个``’a’)，` `'caaat' (3个 'a' 字符)，等等。

类似 * 这样的重复是 贪婪的；当重复正则时，匹配引擎将尝试尽可能多地重复它。 如果模式的后续部分不匹配，则匹配引擎将回退并以较少的重复次数再次尝试。

一个逐步的例子将使这更加明显。 让我们考虑表达式 a[bcd]*b。 这个正则匹配字母 'a'，类 [bcd] 中的零或多个字母，最后以 'b' 结尾。 现在想象一下这个正则与字符串 'abcbd' 匹配。

步骤

匹配

解释

1

a

正则中的 a 匹配。

2

abcbd

引擎尽可能多地匹配 [bcd]* ，直到字符串结束。

3

失败

引擎尝试匹配 b ，但是当前位置位于字符串结束，所以匹配失败。

4

abcb

回退一次，[bcd]* 少匹配一个字符。

5

失败

再次尝试匹配 b ， 但是当前位置是最后一个字符 'd' 。

6

abc

再次回退，所以 [bcd]* 只匹配 bc 。

6

abcb

再试一次 b 。 这次当前位置的字符是 'b' ，所以它成功了。

正则现在已经结束了，它已经匹配了 'abcb'。 这演示了匹配引擎最初如何进行，如果没有找到匹配，它将逐步回退并一次又一次地重试正则的其余部分。 它将回退，直到它为 [bcd]* 尝试零匹配，如果随后失败，引擎将断定该字符串与正则完全不匹配。

另一个重复的元字符是 +，它匹配一次或多次。 要特别注意 * 和 + 之间的区别；* 匹配 零次 或更多次，因此重复的任何东西都可能根本不存在，而 + 至少需要 一次。 使用类似的例子，ca+t 将匹配 'cat' (1 个 'a')，'caaat' (3 个 'a')，但不会匹配 'ct'。

还有两个重复限定符。 问号字符 ? 匹配一次或零次；你可以把它想象成是可选的。 例如，home-?brew 匹配 'homebrew' 或 'home-brew'。

最复杂的重复限定符是 {m,n}，其中 m 和 n 是十进制整数。 这个限定符意味着必须至少重复 m 次，最多重复 n 次。 例如，a/{1,3}b 将匹配 'a/b' ，'a//b' 和 'a///b' 。 它不匹配没有斜线的 'ab'，或者有四个的 'a////b'。

你可以省略 m 或 n; 在这种情况下，将假定缺失值的合理值。 省略 m 被解释为 0 下限，而省略 n 则为无穷大的上限。

还原论者的读者可能会注意到其他三个限定符都可以用这种表示法表达。 {0,}``与``*``相同，``{1,}``相当于``+，{0,1}``和?``相同。 最好使用``*``，+``或?``，只要因为它们更短更容易阅读。

使用正则表达式

现在我们已经看了一些简单的正则表达式，我们如何在 Python 中实际使用它们？ re 模块提供了正则表达式引擎的接口，允许你将正则编译为对象，然后用它们进行匹配。

编译正则表达式

正则表达式被编译成模式对象，模式对象具有各种操作的方法，例如搜索模式匹配或执行字符串替换。:

>>> import re
>>> p = re.compile('ab*')
>>> p
re.compile('ab*')

re.compile() 也接受一个可选的 flags 参数，用于启用各种特殊功能和语法变体。 我们稍后将介绍可用的设置，但现在只需一个例子

>>> p = re.compile('ab*', re.IGNORECASE)

正则作为字符串传递给 re.compile() 。 正则被处理为字符串，因为正则表达式不是核心Python语言的一部分，并且没有创建用于表达它们的特殊语法。 （有些应用程序根本不需要正则，因此不需要通过包含它们来扩展语言规范。）相反，re 模块只是Python附带的C扩展模块，就类似于 socket 或 zlib 模块。

将正则放在字符串中可以使 Python 语言更简单，但有一个缺点是下一节的主题。

反斜杠灾难

如前所述，正则表达式使用反斜杠字符 ('\') 来表示特殊形式或允许使用特殊字符而不调用它们的特殊含义。 这与 Python 在字符串文字中用于相同目的的相同字符的使用相冲突。

假设你想要编写一个与字符串 \section 相匹配的正则，它可以在 LaTeX 文件中找到。 要找出在程序代码中写入的内容，请从要匹配的字符串开始。 接下来，您必须通过在反斜杠前面添加反斜杠和其他元字符，从而产生字符串 \\section。 必须传递给 re.compile() 的结果字符串必须是 \\section。 但是，要将其表示为 Python 字符串文字，必须 再次 转义两个反斜杠。

字符

阶段

\section

被匹配的字符串

\\section

为 re.compile() 转义的反斜杠

"\\\\section"

为字符串字面转义的反斜杠

简而言之，要匹配文字反斜杠，必须将 '\\\\' 写为正则字符串，因为正则表达式必须是 \\，并且每个反斜杠必须表示为 \\ 在常规Python字符串字面中。 在反复使用反斜杠的正则中，这会导致大量重复的反斜杠，并使得生成的字符串难以理解。

解决方案是使用 Python 的原始字符串表示法来表示正则表达式；反斜杠不以任何特殊的方式处理前缀为 'r' 的字符串字面，因此 r"\n" 是一个包含 '\' 和 'n' 的双字符字符串，而 "\n" 是一个包含换行符的单字符字符串。 正则表达式通常使用这种原始字符串表示法用 Python 代码编写。

此外，在正则表达式中有效但在 Python 字符串文字中无效的特殊转义序列现在导致 DeprecationWarning 并最终变为 SyntaxError。 这意味着如果未使用原始字符串表示法或转义反斜杠，序列将无效。

常规字符串

原始字符串

"ab*"

r"ab*"

"\\\\section"

r"\\section"

"\\w+\\s+\\1"

r"\w+\s+\1"

应用匹配

一旦你有一个表示编译正则表达式的对象，你用它做什么？ 模式对象有几种方法和属性。 这里只介绍最重要的内容；请参阅 re 文档获取完整列表。

方法 / 属性

目的

match()

确定正则是否从字符串的开头匹配。

search()

扫描字符串，查找此正则匹配的任何位置。

findall()

找到正则匹配的所有子字符串，并将它们作为列表返回。

finditer()

找到正则匹配的所有子字符串，并将它们返回为一个 iterator。

match() and search() return None if no match can be found. If they’re successful, a match object instance is returned, containing information about the match: where it starts and ends, the substring it matched, and more.

你可以通过交互式实验 re 模块来了解这一点。 如果你有 tkinter，你可能还想查看 Tools/demo/redemo.py [https://github.com/python/cpython/tree/3.6/Tools/demo/redemo.py]，这是 Python 发行附带的演示程序。 它允许你输入正则和字符串，并显示RE是匹配还是失败。 redemo.py 在尝试调试复杂的正则时非常有用。

本 HOWTO 使用标准 Python 解释器作为示例。 首先，运行 Python 解释器，导入 re 模块，然后编译一个正则

>>> import re
>>> p = re.compile('[a-z]+')
>>> p
re.compile('[a-z]+')

Now, you can try matching various strings against the RE [a-z]+. An empty string shouldn’t match at all, since + means ‘one or more repetitions’. match() should return None in this case, which will cause the interpreter to print no output. You can explicitly print the result of match() to make this clear.

>>> p.match("")
>>> print(p.match(""))
None

Now, let’s try it on a string that it should match, such as tempo. In this case, match() will return a match object, so you should store the result in a variable for later use.

>>> m = p.match('tempo')
>>> m
<_sre.SRE_Match object; span=(0, 5), match='tempo'>

现在你可以检查 匹配对象 以获取有关匹配字符串的信息。 匹配对象实例也有几个方法和属性；最重要的是：

方法 / 属性

目的

group()

返回正则匹配的字符串

start()

返回匹配的开始位置

end()

返回匹配的结束位置

span()

返回包含匹配 (start, end) 位置的元组

尝试这些方法很快就会清楚它们的含义:

>>> m.group()
'tempo'
>>> m.start(), m.end()
(0, 5)
>>> m.span()
(0, 5)

group() returns the substring that was matched by the RE. start() and end() return the starting and ending index of the match. span() returns both start and end indexes in a single tuple. Since the match() method only checks if the RE matches at the start of a string, start() will always be zero. However, the search() method of patterns scans through the string, so the match may not start at zero in that case.

>>> print(p.match('::: message'))
None
>>> m = p.search('::: message'); print(m)
<_sre.SRE_Match object; span=(4, 11), match='message'>
>>> m.group()
'message'
>>> m.span()
(4, 11)

在实际程序中，最常见的样式是在变量中存储 匹配对象，然后检查它是否为 None。 这通常看起来像:

p = re.compile( ... )
m = p.match( 'string goes here' )
if m:
    print('Match found: ', m.group())
else:
    print('No match')

Two pattern methods return all of the matches for a pattern. findall() returns a list of matching strings:

>>> p = re.compile(r'\d+')
>>> p.findall('12 drummers drumming, 11 pipers piping, 10 lords a-leaping')
['12', '11', '10']

在这个例子中需要 r 前缀，使字面为原始字符串字面，因为普通的“加工”字符串字面中的转义序列不能被 Python 识别为正则表达式，导致 DeprecationWarning 并最终产生 SyntaxError。 请参阅 反斜杠灾难。

findall() 必须先创建整个列表才能返回结果。 finditer() 方法将一个 匹配对象 的序列返回为一个 iterator

>>> iterator = p.finditer('12 drummers drumming, 11 ... 10 ...')
>>> iterator
<callable_iterator object at 0x...>
>>> for match in iterator:
...     print(match.span())
...
(0, 2)
(22, 24)
(29, 31)

模块级别函数

你不必创建模式对象并调用其方法；re 模块还提供了顶级函数 match()，search()，findall()，sub() 等等。 这些函数采用与相应模式方法相同的参数，并将正则字符串作为第一个参数添加，并仍然返回 None 或 匹配对象 实例。:

>>> print(re.match(r'From\s+', 'Fromage amk'))
None
>>> re.match(r'From\s+', 'From amk Thu May 14 19:12:10 1998')
<_sre.SRE_Match object; span=(0, 5), match='From '>

本质上，这些函数只是为你创建一个模式对象，并在其上调用适当的方法。 它们还将编译对象存储在缓存中，因此使用相同的未来调用将不需要一次又一次地解析该模式。

你是否应该使用这些模块级函数，还是应该自己获取模式并调用其方法？ 如果你正在循环中访问正则表达式，预编译它将节省一些函数调用。 在循环之外，由于有内部缓存，没有太大区别。

编译标志

编译标志允许你修改正则表达式的工作方式。 标志在 re 模块中有两个名称，长名称如 IGNORECASE 和一个简短的单字母形式，例如 I。 （如果你熟悉 Perl 的模式修饰符，则单字母形式使用和其相同的字母；例如， re.VERBOSE 的缩写形式为 re.X。）多个标志可以 通过按位或运算来指定它们；例如，re.I | re.M 设置 I 和 M 标志。

这是一个可用标志表，以及每个标志的更详细说明。

标志

含义

ASCII, A

使几个转义如 \w、\b、\s 和 \d 匹配仅与具有相应特征属性的 ASCII 字符匹配。

DOTALL, S

使 . 匹配任何字符，包括换行符。

IGNORECASE, I

进行大小写不敏感匹配。

LOCALE, L

进行区域设置感知匹配。

MULTILINE, M

多行匹配，影响 ^ 和 $。

VERBOSE, X （为 ‘扩展’）

启用详细的正则，可以更清晰，更容易理解。

I

IGNORECASE

执行不区分大小写的匹配；字符类和字面字符串将通过忽略大小写来匹配字母。 例如，[A-Z] 也匹配小写字母。 除非使用 ASCII 标志来禁用非ASCII匹配，否则完全 Unicode 匹配也有效。 当 Unicode 模式 [a-z] 或 [A-Z] 与 IGNORECASE 标志结合使用时，它们将匹配 52 个 ASCII 字母和 4 个额外的非 ASCII 字母：’İ’ (U+0130，拉丁大写字母 I，带上面的点)，’ı’ (U+0131，拉丁文小写字母无点 i)，’s’ (U+017F，拉丁文小写字母长 s) 和’K’ (U+212A，开尔文符号)。 Spam 将匹配 'Spam'，'spam'，'spAM' 或 'ſpam' (后者仅在 Unicode 模式下匹配)。 此小写不考虑当前区域设置；如果你还设置了 LOCALE 标志，则将考虑。

L

LOCALE

使 \w、\W、\b、\B 和大小写敏感匹配依赖于当前区域而不是 Unicode 数据库。

区域设置是 C 库的一个功能，旨在帮助编写考虑到语言差异的程序。例如，如果你正在处理编码的法语文本，那么你希望能够编写 \w+ 来匹配单词，但 \w 只匹配字符类 [A-Za-z] 字节模式；它不会匹配对应于 é 或 ç 的字节。如果你的系统配置正确并且选择了法语区域设置，某些C函数将告诉程序对应于 é 的字节也应该被视为字母。在编译正则表达式时设置 LOCALE 标志将导致生成的编译对象将这些C函数用于 \w；这比较慢，但也可以使 \w+ 匹配你所期望的法语单词。在 Python 3 中不鼓励使用此标志，因为语言环境机制非常不可靠，它一次只处理一个“文化”，它只适用于 8 位语言环境。默认情况下，Python 3 中已经为 Unicode（str）模式启用了 Unicode 匹配，并且它能够处理不同的区域/语言。

M

MULTILINE

(^ 和 $ 还没有解释；它们将在以下部分介绍 更多元字符。)

通常 ^ 只匹配字符串的开头，而 $ 只匹配字符串的结尾，紧接在字符串末尾的换行符（如果有的话）之前。 当指定了这个标志时，^ 匹配字符串的开头和字符串中每一行的开头，紧跟在每个换行符之后。 类似地，$ 元字符匹配字符串的结尾和每行的结尾（紧接在每个换行符之前）。

S

DOTALL

使 '.' 特殊字符匹配任何字符，包括换行符；没有这个标志，'.' 将匹配任何字符 除了 换行符。

A

ASCII

使 \w、\W、\b、\B、\s 和 \S 执行仅 ASCII 匹配而不是完整匹配 Unicode 匹配。 这仅对 Unicode 模式有意义，并且对于字节模式将被忽略。

X

VERBOSE

此标志允许你编写更易读的正则表达式，方法是为您提供更灵活的格式化方式。 指定此标志后，将忽略正则字符串中的空格，除非空格位于字符类中或前面带有未转义的反斜杠；这使你可以更清楚地组织和缩进正则。 此标志还允许你将注释放在正则中，引擎将忽略该注释；注释标记为 '#' 既不是在字符类中，也不是在未转义的反斜杠之前。

例如，这里的正则使用 re.VERBOSE；看看阅读有多容易？:

charref = re.compile(r"""
 &[#]                # Start of a numeric entity reference
 (
     0[0-7]+         # Octal form
   | [0-9]+          # Decimal form
   | x[0-9a-fA-F]+   # Hexadecimal form
 )
 ;                   # Trailing semicolon
""", re.VERBOSE)

如果没有详细设置，正则将如下所示:

charref = re.compile("&#(0[0-7]+"
                     "|[0-9]+"
                     "|x[0-9a-fA-F]+);")

在上面的例子中，Python的字符串文字的自动连接已被用于将正则分解为更小的部分，但它仍然比以下使用 re.VERBOSE 版本更难理解。

更多模式能力

到目前为止，我们只介绍了正则表达式的一部分功能。 在本节中，我们将介绍一些新的元字符，以及如何使用组来检索匹配的文本部分。

更多元字符

我们还没有涉及到一些元字符。 其中大部分内容将在本节中介绍。

要讨论的其余一些元字符是 零宽度断言 。 它们不会使解析引擎在字符串中前进一个字符；相反，它们根本不占用任何字符，只是成功或失败。例如，\b 是一个断言，指明当前位置位于字边界；这个位置根本不会被 \b 改变。这意味着永远不应重复零宽度断言，因为如果它们在给定位置匹配一次，它们显然可以无限次匹配。

|

或者“or”运算符。 如果 A 和 B 是正则表达式，A|B 将匹配任何与 A 或 B 匹配的字符串。 | 具有非常低的优先级，以便在交替使用多字符字符串时使其合理地工作。 Crow|Servo 将匹配 'Crow' 或 'Servo'，而不是 'Cro'、'w' 或 'S' 和 'ervo'。

要匹配字面 '|'，请使用 \|，或将其括在字符类中，如 [|]。

^

在行的开头匹配。 除非设置了 MULTILINE 标志，否则只会在字符串的开头匹配。 在 MULTILINE 模式下，这也在字符串中的每个换行符后立即匹配。

例如，如果你希望仅在行的开头匹配单词 From，则要使用的正则 ^From。:

>>> print(re.search('^From', 'From Here to Eternity'))
<_sre.SRE_Match object; span=(0, 4), match='From'>
>>> print(re.search('^From', 'Reciting From Memory'))
None

要匹配字面 '^'，使用 \^。

$

匹配行的末尾，定义为字符串的结尾，或者后跟换行符的任何位置。:

>>> print(re.search('}$', '{block}'))
<_sre.SRE_Match object; span=(6, 7), match='}'>
>>> print(re.search('}$', '{block} '))
None
>>> print(re.search('}$', '{block}\n'))
<_sre.SRE_Match object; span=(6, 7), match='}'>

以匹配字面 '$'，使用 \$ 或者将其包裹在一个字符类中，例如 [$]。

\A

仅匹配字符串的开头。 当不在 MULTILINE 模式时，\A 和 ^ 实际上是相同的。 在 MULTILINE 模式中，它们是不同的: \A 仍然只在字符串的开头匹配，但 ^ 可以匹配在换行符之后的字符串内的任何位置。

\Z

只匹配字符串尾。

\b

字边界。 这是一个零宽度断言，仅在单词的开头或结尾处匹配。 单词被定义为一个字母数字字符序列，因此单词的结尾由空格或非字母数字字符表示。

以下示例仅当它是一个完整的单词时匹配 class；当它包含在另一个单词中时将不会匹配。

>>> p = re.compile(r'\bclass\b')
>>> print(p.search('no class at all'))
<_sre.SRE_Match object; span=(3, 8), match='class'>
>>> print(p.search('the declassified algorithm'))
None
>>> print(p.search('one subclass is'))
None

使用这个特殊序列时，你应该记住两个细微之处。 首先，这是 Python 的字符串文字和正则表达式序列之间最严重的冲突。 在 Python 的字符串文字中，\b 是退格字符，ASCII 值为8。 如果你没有使用原始字符串，那么 Python 会将 \b 转换为退格，你的正则不会按照你的预期匹配。 以下示例与我们之前的正则看起来相同，但省略了正则字符串前面的 'r'。:

>>> p = re.compile('\bclass\b')
>>> print(p.search('no class at all'))
None
>>> print(p.search('\b' + 'class' + '\b'))
<_sre.SRE_Match object; span=(0, 7), match='\x08class\x08'>

其次，在一个字符类中，这个断言没有用处，\b 表示退格字符，以便与 Python 的字符串文字兼容。

\B

另一个零宽度断言，这与 \b 相反，仅在当前位置不在字边界时才匹配。

分组

通常，你需要获取更多信息，而不仅仅是正则是否匹配。 正则表达式通常用于通过将正则分成几个子组来解析字符串，这些子组匹配不同的感兴趣组件。 例如，RFC-822 标题行分为标题名称和值，用 ':' 分隔，如下所示：

From: author@example.com
User-Agent: Thunderbird 1.5.0.9 (X11/20061227)
MIME-Version: 1.0
To: editor@example.com

这可以通过编写与整个标题行匹配的正则表达式来处理，并且具有与标题名称匹配的一个组，以及与标题的值匹配的另一个组。

组由 '('，')'` 元字符标记。 '(' 和 ')' 与数学表达式的含义大致相同；它们将包含在其中的表达式组合在一起，你可以使用重复限定符重复组的内容，例如 *，+，? 或 {m,n}。 例如，(ab)* 将匹配 ab 的零次或多次重复。:

>>> p = re.compile('(ab)*')
>>> print(p.match('ababababab').span())
(0, 10)

Groups indicated with '(', ')' also capture the starting and ending index of the text that they match; this can be retrieved by passing an argument to group(), start(), end(), and span(). Groups are numbered starting with 0. Group 0 is always present; it’s the whole RE, so match object methods all have group 0 as their default argument. Later we’ll see how to express groups that don’t capture the span of text that they match.

>>> p = re.compile('(a)b')
>>> m = p.match('ab')
>>> m.group()
'ab'
>>> m.group(0)
'ab'

子组从左到右编号，从 1 向上编号。 组可以嵌套；要确定编号，只需计算从左到右的左括号字符。:

>>> p = re.compile('(a(b)c)d')
>>> m = p.match('abcd')
>>> m.group(0)
'abcd'
>>> m.group(1)
'abc'
>>> m.group(2)
'b'

group() can be passed multiple group numbers at a time, in which case it will return a tuple containing the corresponding values for those groups.

>>> m.group(2,1,2)
('b', 'abc', 'b')

The groups() method returns a tuple containing the strings for all the subgroups, from 1 up to however many there are.

>>> m.groups()
('abc', 'b')

模式中的后向引用允许你指定还必须在字符串中的当前位置找到先前捕获组的内容。 例如，如果可以在当前位置找到组 1 的确切内容，则 \1 将成功，否则将失败。 请记住，Python 的字符串文字也使用反斜杠后跟数字以允许在字符串中包含任意字符，因此正则中引入反向引用时务必使用原始字符串。

例如，以下正则检测字符串中的双字。:

>>> p = re.compile(r'\b(\w+)\s+\1\b')
>>> p.search('Paris in the the spring').group()
'the the'

像这样的后向引用通常不仅仅用于搜索字符串 —— 很少有文本格式以这种方式重复数据 —— 但是你很快就会发现它们在执行字符串替换时 非常 有用。

非捕获和命名组

精心设计的正则可以使用许多组，既可以捕获感兴趣的子串，也可以对正则本身进行分组和构建。 在复杂的正则中，很难跟踪组号。 有两个功能可以帮助解决这个问题。 它们都使用常用语法进行正则表达式扩展，因此我们首先看一下。

Perl 5 以其对标准正则表达式的强大补充而闻名。 对于这些新功能，Perl 开发人员无法选择新的单键击元字符或以 \ 开头的新特殊序列，否则 Perl 的正则表达式与标准正则容易混淆。 例如，如果他们选择 & 作为一个新的元字符，旧的表达式将假设 & 是一个普通字符，并且不会编写 \& 或 [&]。

Perl 开发人员选择的解决方案是使用 (?...) 作为扩展语法。 括号后面的 ? 是一个语法错误，因为 ? 没有什么可重复的，所以这并没有引入任何兼容性问题。 紧跟在 ? 之后的字符表示正在使用什么扩展名，所以 (?=foo) 是一个东西（一个正向的先行断言）和 (?:foo) 是其它东西（ 包含子表达式 foo 的非捕获组）。

Python 支持一些 Perl 的扩展，并增加了新的扩展语法用于 Perl 的扩展语法。 如果在问号之后的第一个字符为 P，即表明其为 Python 专属的扩展。

现在我们已经了解了一般的扩展语法，我们可以回到简化复杂正则中组处理的功能。

有时你会想要使用组来表示正则表达式的一部分，但是对检索组的内容不感兴趣。 你可以通过使用非捕获组来显式表达这个事实: (?:...)，你可以用任何其他正则表达式替换 ...。:

>>> m = re.match("([abc])+", "abc")
>>> m.groups()
('c',)
>>> m = re.match("(?:[abc])+", "abc")
>>> m.groups()
()

除了你无法检索组匹配内容的事实外，非捕获组的行为与捕获组完全相同；你可以在里面放任何东西，用重复元字符重复它，比如 *，然后把它嵌入其他组（捕获或不捕获）。 (?:...) 在修改现有模式时特别有用，因为你可以添加新组而不更改所有其他组的编号方式。 值得一提的是，捕获和非捕获组之间的搜索没有性能差异；两种形式没有一种更快。

更重要的功能是命名组：不是通过数字引用它们，而是可以通过名称引用组。

命名组的语法是Python特定的扩展之一: (?P<name>...)。 name 显然是该组的名称。 命名组的行为与捕获组完全相同，并且还将名称与组关联。 处理捕获组的 匹配对象 方法都接受按编号引用组的整数或包含所需组名的字符串。 命名组仍然是给定的数字，因此你可以通过两种方式检索有关组的信息:

>>> p = re.compile(r'(?P<word>\b\w+\b)')
>>> m = p.search( '(((( Lots of punctuation )))' )
>>> m.group('word')
'Lots'
>>> m.group(1)
'Lots'

命名组很有用，因为它们允许你使用容易记住的名称，而不必记住数字。 这是来自 imaplib 模块的示例正则

InternalDate = re.compile(r'INTERNALDATE "'
        r'(?P<day>[ 123][0-9])-(?P<mon>[A-Z][a-z][a-z])-'
        r'(?P<year>[0-9][0-9][0-9][0-9])'
        r' (?P<hour>[0-9][0-9]):(?P<min>[0-9][0-9]):(?P<sec>[0-9][0-9])'
        r' (?P<zonen>[-+])(?P<zoneh>[0-9][0-9])(?P<zonem>[0-9][0-9])'
        r'"')

检索 m.group('zonem') 显然要容易得多，而不必记住检索第 9 组。

表达式中的后向引用语法，例如 (...)\1，指的是组的编号。 当然有一种变体使用组名而不是数字。 这是另一个 Python 扩展: (?P=name) 表示在当前点再次匹配名为 name 的组的内容。 用于查找双字的正则表达式，\b(\w+)\s+\1\b 也可以写为 \b(?P<word>\w+)\s+(?P=word)\b:

>>> p = re.compile(r'\b(?P<word>\w+)\s+(?P=word)\b')
>>> p.search('Paris in the the spring').group()
'the the'

前向断言

另一个零宽度断言是前向断言。 前向断言以正面和负面形式提供，如下所示：

(?=…)

正向前向断言。 如果包含的正则表达式，由 ... 表示，在当前位置成功匹配，则成功，否则失败。 但是，一旦尝试了包含的表达式，匹配的引擎就不会前进；模式其余的部分会在在断言开始的地方尝试。

(?!…)

负向前向断言。 这与积正向断言相反；如果包含的表达式在字符串中的当前位置 不 匹配，则成功。

更具体一些，让我们看看前向是有用的情况。 考虑一个简单的模式来匹配文件名并将其拆分为基本名称和扩展名，用 . 分隔。 例如，在 news.rc 中，news 是基本名称，rc 是文件名的扩展名。

与此匹配的模式非常简单：

.*[.].*$

请注意，. 需要特别处理，因为它是元字符，所以它在字符类中只能匹配特定字符。 还要注意尾随的 $；添加此项以确保扩展名中的所有其余字符串都必须包含在扩展名中。 这个正则表达式匹配 foo.bar、autoexec.bat、sendmail.cf 和 printers.conf。

现在，考虑使更复杂一点的问题；如果你想匹配扩展名不是 bat 的文件名怎么办？ 一些错误的尝试：

.*[.][^b].*$ 上面的第一次尝试试图通过要求扩展名的第一个字符不是 b 来排除 bat。 这是错误的，因为模式也与 foo.bar 不匹配。

.*[.]([^b]..|.[^a].|..[^t])$

当你尝试通过要求以下一种情况匹配来修补第一个解决方案时，表达式变得更加混乱：扩展的第一个字符不是 b。 第二个字符不 a；或者第三个字符不是 t。 这接受 foo.bar 并拒绝 autoexec.bat，但它需要三个字母的扩展名，并且不接受带有两个字母扩展名的文件名，例如 sendmail.cf。 为了解决这个问题，我们会再次使模式复杂化。

.*[.]([^b].?.?|.[^a]?.?|..?[^t]?)$

在第三次尝试中，第二个和第三个字母都是可选的，以便允许匹配的扩展名短于三个字符，例如 sendmail.cf。

模式现在变得非常复杂，这使得它难以阅读和理解。 更糟糕的是，如果问题发生变化并且你想要将 bat 和 exe 排除为扩展，那么该模式将变得更加复杂和混乱。

负面前向消除了所有这些困扰：

.*[.](?!bat$)[^.]*$ 负向前向意味着：如果表达式 bat 此时不匹配，请尝试其余的模式；如果 bat$ 匹配，整个模式将失败。 尾随的 $ 是必需的，以确保允许像 sample.batch 这样的扩展只以 bat 开头的文件能通过。 [^.]* 确保当文件名中有多个点时，模式有效。

现在很容易排除另一个文件扩展名；只需在断言中添加它作为替代。 以下模块排除以 bat 或 exe:

.*[.](?!bat$|exe$)[^.]*$

修改字符串

到目前为止，我们只是针对静态字符串执行搜索。 正则表达式通常也用于以各种方式修改字符串，使用以下模式方法：

方法 / 属性

目的

split()

将字符串拆分为一个列表，在正则匹配的任何地方将其拆分

sub()

找到正则匹配的所有子字符串，并用不同的字符串替换它们

subn()

与 sub() 相同，但返回新字符串和替换次数

分割字符串

The split() method of a pattern splits a string apart wherever the RE matches, returning a list of the pieces. It’s similar to the split() method of strings but provides much more generality in the delimiters that you can split by; string split() only supports splitting by whitespace or by a fixed string. As you’d expect, there’s a module-level re.split() function, too.

.split(string[, maxsplit=0])

通过正则表达式的匹配拆分 字符串。 如果在正则中使用捕获括号，则它们的内容也将作为结果列表的一部分返回。 如果 maxsplit 非零，则最多执行 maxsplit 次拆分。

你可以通过传递 maxsplit 的值来限制分割的数量。 当 maxsplit 非零时，将最多进行 maxsplit 次拆分，并且字符串的其余部分将作为列表的最后一个元素返回。 在以下示例中，分隔符是任何非字母数字字符序列。:

>>> p = re.compile(r'\W+')
>>> p.split('This is a test, short and sweet, of split().')
['This', 'is', 'a', 'test', 'short', 'and', 'sweet', 'of', 'split', '']
>>> p.split('This is a test, short and sweet, of split().', 3)
['This', 'is', 'a', 'test, short and sweet, of split().']

有时你不仅对分隔符之间的文本感兴趣，而且还需要知道分隔符是什么。 如果在正则中使用捕获括号，则它们的值也将作为列表的一部分返回。 比较以下调用:

>>> p = re.compile(r'\W+')
>>> p2 = re.compile(r'(\W+)')
>>> p.split('This... is a test.')
['This', 'is', 'a', 'test', '']
>>> p2.split('This... is a test.')
['This', '... ', 'is', ' ', 'a', ' ', 'test', '.', '']

模块级函数 re.split() 添加要正则作为第一个参数，但在其他方面是相同的。:

>>> re.split(r'[\W]+', 'Words, words, words.')
['Words', 'words', 'words', '']
>>> re.split(r'([\W]+)', 'Words, words, words.')
['Words', ', ', 'words', ', ', 'words', '.', '']
>>> re.split(r'[\W]+', 'Words, words, words.', 1)
['Words', 'words, words.']

搜索和替换

Another common task is to find all the matches for a pattern, and replace them with a different string. The sub() method takes a replacement value, which can be either a string or a function, and the string to be processed.

.sub(replacement, string[, count=0])

返回通过替换 replacement 替换 string 中正则的最左边非重叠出现而获得的字符串。 如果未找到模式，则 string 将保持不变。

可选参数 count 是要替换的模式最大的出现次数；count 必须是非负整数。 默认值 0 表示替换所有。

Here’s a simple example of using the sub() method. It replaces colour names with the word colour:

>>> p = re.compile('(blue|white|red)')
>>> p.sub('colour', 'blue socks and red shoes')
'colour socks and colour shoes'
>>> p.sub('colour', 'blue socks and red shoes', count=1)
'colour socks and red shoes'

The subn() method does the same work, but returns a 2-tuple containing the new string value and the number of replacements that were performed:

>>> p = re.compile('(blue|white|red)')
>>> p.subn('colour', 'blue socks and red shoes')
('colour socks and colour shoes', 2)
>>> p.subn('colour', 'no colours at all')
('no colours at all', 0)

Empty matches are replaced only when they’re not adjacent to a previous match.

>>> p = re.compile('x*')
>>> p.sub('-', 'abxd')
'-a-b-d-'

如果 replacement 是一个字符串，则处理其中的任何反斜杠转义。 也就是说，\n 被转换为单个换行符，\r 被转换为回车符，依此类推。 诸如 \& 之类的未知转义是孤立的。 后向引用，例如 \6，被替换为正则中相应组匹配的子字符串。 这使你可以在生成的替换字符串中合并原始文本的部分内容。

这个例子匹配单词 section 后跟一个用 {，} 括起来的字符串，并将 section 改为 subsection

>>> p = re.compile('section{ ( [^}]* ) }', re.VERBOSE)
>>> p.sub(r'subsection{\1}','section{First} section{second}')
'subsection{First} subsection{second}'

还有一种语法用于引用由 (?P<name>...) 语法定义的命名组。 \g<name> 将使用名为 name 的组匹配的子字符串，\g<number> 使用相应的组号。 因此 \g<2> 等同于 \2，但在诸如 \g<2>0 之类的替换字符串中并不模糊。 (\20 将被解释为对组 20 的引用，而不是对组 2 的引用，后跟字面字符 '0'。) 以下替换都是等效的，但使用所有三种变体替换字符串。:

>>> p = re.compile('section{ (?P<name> [^}]* ) }', re.VERBOSE)
>>> p.sub(r'subsection{\1}','section{First}')
'subsection{First}'
>>> p.sub(r'subsection{\g<1>}','section{First}')
'subsection{First}'
>>> p.sub(r'subsection{\g<name>}','section{First}')
'subsection{First}'

replacement 也可以是一个函数，它可以为你提供更多控制。 如果 replacement 是一个函数，则为 pattern 的每次非重叠出现将调用该函数。 在每次调用时，函数都会传递一个匹配的 匹配对象 参数，并可以使用此信息计算所需的替换字符串并将其返回。

在以下示例中，替换函数将小数转换为十六进制:

>>> def hexrepl(match):
...     "Return the hex string for a decimal number"
...     value = int(match.group())
...     return hex(value)
...
>>> p = re.compile(r'\d+')
>>> p.sub(hexrepl, 'Call 65490 for printing, 49152 for user code.')
'Call 0xffd2 for printing, 0xc000 for user code.'

使用模块级别 re.sub() 函数时，模式作为第一个参数传递。 图案可以作为对象或字符串提供；如果需要指定正则表达式标志，则必须使用模式对象作为第一个参数，或者在模式字符串中使用嵌入式修饰符，例如: sub("(?i)b+", "x", "bbbb BBBB") 返回 'x x'。

常见问题

正则表达式对于某些应用程序来说是一个强大的工具，但在某些方面，它们的行为并不直观，有时它们的行为方式与你的预期不同。 本节将指出一些最常见的陷阱。

使用字符串方法

有时使用 re 模块是一个错误。 如果你匹配固定字符串或单个字符类，并且你没有使用任何 re 功能，例如 IGNORECASE 标志，那么正则表达式的全部功能可能不是必需的。 字符串有几种方法可以使用固定字符串执行操作，它们通常要快得多，因为实现是一个针对此目的而优化的单个小 C 循环，而不是大型、更通用的正则表达式引擎。

一个例子可能是用另一个固定字符串替换一个固定字符串；例如，你可以用 deed 替换 word 。 re.sub() 看起来像是用于此的函数，但请考虑 replace() 方法。 注意 replace() 也会替换单词里面的 word ，把 swordfish 变成 sdeedfish ，但简单的正则 word 也会这样做。 （为了避免对单词的部分进行替换，模式必须是 \bword\b，以便要求 word 在任何一方都有一个单词边界。这使得工作超出了 replace() 的能力。）

另一个常见任务是从字符串中删除单个字符的每个匹配项或将其替换为另一个字符。 你可以用 re.sub('\n', ' ', S) 之类的东西来做这件事，但是 translate() 能够完成这两项任务，并且比任何正则表达式都快。

简而言之，在转向 re 模块之前，请考虑是否可以使用更快更简单的字符串方法解决问题。

match() 和 search()

The match() function only checks if the RE matches at the beginning of the string while search() will scan forward through the string for a match. It’s important to keep this distinction in mind. Remember, match() will only report a successful match which will start at 0; if the match wouldn’t start at zero, match() will not report it.

>>> print(re.match('super', 'superstition').span())
(0, 5)
>>> print(re.match('super', 'insuperable'))
None

另一方面， search() 将向前扫描字符串，报告它找到的第一个匹配项。:

>>> print(re.search('super', 'superstition').span())
(0, 5)
>>> print(re.search('super', 'insuperable').span())
(2, 7)

有时你会被诱惑继续使用 re.match() ，只需在你的正则前面添加 .* 。抵制这种诱惑并使用 re.search() 代替。 正则表达式编译器对正则进行一些分析，以加快寻找匹配的过程。 其中一个分析可以确定匹配的第一个特征必须是什么；例如，以 Crow 开头的模式必须与 'C' 匹配。 分析让引擎快速扫描字符串，寻找起始字符，只在找到 'C' 时尝试完全匹配。

添加 .* 会使这个优化失效，需要扫描到字符串的末尾，然后回溯以找到正则的其余部分的匹配。 使用 re.search() 代替。

贪婪与非贪婪

当重复一个正则表达式时，就像在 a* 中一样，最终的动作就是消耗尽可能多的模式。 当你尝试匹配一对对称分隔符，例如 HTML 标记周围的尖括号时，这个事实经常会让你感到困惑。因为 .* 的贪婪性质， 用于匹配单个 HTML 标记的简单模式不起作用。

>>> s = '<html><head><title>Title</title>'
>>> len(s)
32
>>> print(re.match('<.*>', s).span())
(0, 32)
>>> print(re.match('<.*>', s).group())
<html><head><title>Title</title>

正则匹配 '<' 中的 '<html>' 和 .* 消耗字符串的其余部分。 正则中还有更多的剩余东西，并且 > 在字符串的末尾不能匹配，所以正则表达式引擎必须逐个字符地回溯，直到它找到匹配 > 。最终匹配从 '<html>' 中的 '<' 扩展到 '</title>' 中的 '>' ，而这并不是你想要的结果。

在这种情况下，解决方案是使用非贪婪的限定符 *? 、 +? 、 ?? 或 {m,n}? ，匹配为尽可能 少 的文字。 在上面的例子中，在第一次 '<' 匹配后立即尝试 '>' ，当它失败时，引擎一次前进一个字符，每一步都重试 '>' 。 这产生了正确的结果:

>>> print(re.match('<.*?>', s).group())
<html>

（请注意，使用正则表达式解析 HTML 或 XML 很痛苦。快而脏的模式将处理常见情况，但 HTML 和 XML 有特殊情况会破坏明显的正则表达式；当你编写正则表达式处理所有可能的情况时，模式将非常复杂。使用 HTML 或 XML 解析器模块来执行此类任务。）

使用 re.VERBOSE

到目前为止，你可能已经注意到正则表达式是一种非常紧凑的表示法，但它们并不是非常易读。 具有中等复杂度的正则可能会成为反斜杠、括号和元字符的冗长集合，使其难以阅读和理解。

对于这样的正则，在编译正则表达式时指定 re.VERBOSE 标志可能会有所帮助，因为它允许你更清楚地格式化正则表达式。

re.VERBOSE 标志有几种效果。 正则表达式中的 不是 在字符类中的空格将被忽略。 这意味着表达式如 dog | cat 等同于不太可读的 dog|cat ，但 [a b] 仍将匹配字符 'a' 、 'b' 或空格。 此外，你还可以在正则中放置注释；注释从 # 字符扩展到下一个换行符。 当与三引号字符串一起使用时，这使正则的格式更加整齐:

pat = re.compile(r"""
 \s*                 # Skip leading whitespace
 (?P<header>[^:]+)   # Header name
 \s* :               # Whitespace, and a colon
 (?P<value>.*?)      # The header's value -- *? used to
                     # lose the following trailing whitespace
 \s*$                # Trailing whitespace to end-of-line
""", re.VERBOSE)

这更具有可读性:

pat = re.compile(r"\s*(?P<header>[^:]+)\s*:(?P<value>.*?)\s*$")

反馈

正则表达式是一个复杂的主题。 这份文档是否有助于你理解它们？ 是否存在不清楚的部分，或者你遇到的问题未在此处涉及？ 如果是，请向作者发送改进建议。

关于正则表达式的最完整的书几乎肯定是由 O’Reilly 出版的 Jeffrey Friedl 的 Mastering Regular Expressions 。 不幸的是，它专注于 Perl 和 Java 的正则表达式，并且根本不包含任何 Python 材料，因此它不能用作 Python 编程的参考。 （第一版涵盖了 Python 现在删除的 regex 模块，这对你没有多大帮助。）考虑从你的图书馆中查找它。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

套接字编程指南

作者

Gordon McMillan

摘要

套接字几乎无处不在，但是它却是被误解最严重的技术之一。这是一篇简单的套接字概述。并不是一篇真正的教程 —— 你需要做更多的事情才能让它工作起来。其中也并没有涵盖细节（细节会有很多），但是我希望它能提供足够的背景知识，让你像模像样的开始使用套接字

套接字

我将只讨论关于 INET（比如：IPv4 地址族）的套接字，但是它将覆盖几乎 99% 的套接字使用场景。并且我将仅讨论 STREAM（比如：TCP）类型的套接字 - 除非你真的知道你在做什么（那么这篇 HOWTO 可能并不适合你），使用 STREAM 类型的套接字将会得到比其它类型更好的表现与性能。我将尝试揭开套接字的神秘面纱，也会讲到一些阻塞与非阻塞套接字的使用。但是我将以阻塞套接字为起点开始讨论。只有你了解它是如何工作的以后才能处理非阻塞套接字。

理解这些东西的难点之一在于「套接字」可以表示很多微妙差异的东西，这取决于上下文。所以首先，让我们先分清楚「客户端」套接字和「服务端」套接字之间的不同，客户端套接字表示对话的一端，服务端套接字更像是总机接线员。客户端程序只能（比如：你的浏览器）使用「客户端」套接字；网络服务器则可以使用「服务端」套接字和「客户端」套接字来会话

历史

目前为止，在各种形式的 IPC 中，套接字是最流行的。在任何指定的平台上，可能会有其它更快的 IPC 形式，但是就跨平台通信来说，套接字大概是唯一的玩法

套接字做为 BSD Unix 操作系统的一部分在伯克利诞生，像野火一样在因特网传播。有一个很好的原因 —— 套接字与 INET 的结合使得与世界各地的任意机器间通信变得令人难以置信的简单（至少对比与其他方案来说）

创建套接字

简略地说，当你点击带你来到这个页面的链接时，你的浏览器就已经做了下面这几件事情:

# create an INET, STREAMing socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# now connect to the web server on port 80 - the normal http port
s.connect(("www.python.org", 80))

当连接完成，套接字可以用来发送请求来接收页面上显示的文字。同样是这个套接字也会用来读取响应，最后再被销毁。是的，被销毁了。客户端套接字通常用来做一次交换（或者说一小组序列的交换）。

网络服务器发生了什么这个问题就有点复杂了。首页，服务器创建一个「服务端套接字」:

# create an INET, STREAMing socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind the socket to a public host, and a well-known port
serversocket.bind((socket.gethostname(), 80))
# become a server socket
serversocket.listen(5)

有几件事需要注意：我们使用了 socket.gethostname()，所以套接字将外网可见。如果我们使用的是 s.bind(('localhost', 80)) 或者 s.bind(('127.0.0.1', 80))，也会得到一个「服务端」套接字，但是后者只在同一机器上可见。s.bind(('', 80)) 则指定套接字可以被机器上的任何地址碰巧连接

第二个需要注点是：低端口号通常被一些「常用的」服务（HTTP, SNMP 等）所保留。如果你想把程序跑起来，最好使用一个高位端口号（通常是4位的数字）。

最后，listen 方法的参数会告诉套接字库，我们希望在队列中累积多达 5 个（通常的最大值）连接请求后再拒绝外部连接。 如果所有其他代码都准确无误，这个队列长度应该是足够的。

现在我们已经有一个「服务端」套接字，监听了 80 端口，我们可以进入网络服务器的主循环了:

while True:
    # accept connections from outside
    (clientsocket, address) = serversocket.accept()
    # now do something with the clientsocket
    # in this case, we'll pretend this is a threaded server
    ct = client_thread(clientsocket)
    ct.run()

事际上，通常有 3 种方法可以让这个循环工作起来 - 调度一个线程来处理 客户端套接字，或者把这个应用改成使用非阻塞模式套接字，亦或是使用 select 库来实现「服务端」套接字与任意活动 客户端套接字 之间的多路复用。稍后会详细介绍。现在最重要的是理解：这就是一个 服务端 套接字做的 所有 事情。它并没有发送任何数据。也没有接收任何数据。它只创建「客户端」套接字。每个 客户端套接字 都是为了响应某些其它客户端套接字 connect() 到我们绑定的主机。一旦创建 客户端套接字 完成，就会返回并监听更多的连接请求。现个客户端可以随意通信 - 它们使用了一些动态分配的端口，会话结束时端口才会被回收

进程间通信

如果你需要在同一台机器上进行两个进程间的快速 IPC 通信，你应该了解管道或者共享内存。如果你决定使用 AF_INET 类型的套接字，绑定「服务端」套接字到 'localhost' 。在大多数平台，这将会使用一个许多网络层间的通用快捷方式（本地回环地址）并且速度会快很多

参见

multiprocessing 模块使跨平台 IPC 通信成为一个高层的 API

使用一个套接字

首先需要注意，浏览器的「客户端」套接字和网络服务器的「客户端」套接字是极为相似的。即这种会话是「点对点」的。或者也可以说 你作为设计师需要自行决定会话的规则和礼节 。通常情况下，连接 套接字通过发送一个请求或者信号来开始一次会话。但这属于设计决定，并不是套接字规则。

现在有两组用于通信的动词。你可以使用 send 和 recv ，或者你可以把客户端套接字改成文件类型的形式来使用 read 和 write 方法。后者是 Java 语言中表示套接字的方法，我将不会在这儿讨论这个，但是要提醒你需要调用套接字的 flush 方法。这些是“缓冲”的文件，一个经常出现的错误是 write 一些东西，然后就直接开始 read 一个响应。如果不调用 flush ，你可能会一直等待这个响应，因为请求可能还在你的输出缓冲中。

现在我来到了套接字的两个主要的绊脚石 - send 和 recv 操作网络缓冲区。它们并不一定可以处理所有你想要（期望）的字节，因为它们主要关注点是处理网络缓冲。通常，它们在关联的网络缓冲区 send 或者清空 recv 时返回。然后告诉你处理了多少个字节。你 的责任是一直调用它们直到你所有的消息处理完成。

当 recv 方法返回 0 字节时，就表示另一端已经关闭（或者它所在的进程关闭）了连接。你再也不能从这个连接上获取到任何数据了。你可以成功的发送数据；我将在后面讨论这一点。

像 HTTP 这样的协议只使用一个套接字进行一次传输。客户端发送一个请求，然后读取响应。就这么简单。套接字会被销毁。 表示客户端可以通过接收 0 字节序列表示检测到响应的结束。

但是如果你打算在随后来的传输中复用套接字的话，你需要明白 套接字里面是不存在 :abbr:`EOT (传输结束)` 的。重复一下：套接字 send 或者 recv 完 0 字节后返回，连接会中断。如果连接没有被断开，你可能会永远处于等待 recv 的状态，因为（就目前来说）套接字 不会 告诉你不用再读取了。现在如果你细心一点，你可能会意识到套接字基本事实：消息必须要么具有固定长度，要么可以界定，要么指定了长度（比较好的做法），要么以关闭连接为结束。选择完全由你而定（这比让别人定更合理）。

假定你不希望结束连接，那么最简单的解决方案就是使用定长消息:

class MySocket:
    """demonstration class only
      - coded for clarity, not efficiency
    """

    def __init__(self, sock=None):
        if sock is None:
            self.sock = socket.socket(
                            socket.AF_INET, socket.SOCK_STREAM)
        else:
            self.sock = sock

    def connect(self, host, port):
        self.sock.connect((host, port))

    def mysend(self, msg):
        totalsent = 0
        while totalsent < MSGLEN:
            sent = self.sock.send(msg[totalsent:])
            if sent == 0:
                raise RuntimeError("socket connection broken")
            totalsent = totalsent + sent

    def myreceive(self):
        chunks = []
        bytes_recd = 0
        while bytes_recd < MSGLEN:
            chunk = self.sock.recv(min(MSGLEN - bytes_recd, 2048))
            if chunk == b'':
                raise RuntimeError("socket connection broken")
            chunks.append(chunk)
            bytes_recd = bytes_recd + len(chunk)
        return b''.join(chunks)

发送分部代码几乎可用于任何消息传递方案 —— 在 Python 中你发送字符串，可以使用 len() 方法来确定它的长度（即使它嵌入了 \0 字符），主要是接收代码变得更复杂。（在 C 语言中，并没有更糟糕，除非消息嵌入了 \0 字符而且你又无法使用 strlen ）

最简单的改进是让消息的第一个字符表示消息类型，由类型决定长度。现在你需要两次 recv- 第一次取（至少）第一个字符来知晓长度，第二次在循环中获取剩余所有的消息。如果你决定到分界线，你将收到一些任意大小的块，（4096 或者 8192 通常是比较合适的网络缓冲区大小），扫描你接收到的分界符

一个需要意识到的复杂情况是：如果你的会话协议允许多个消息被发送回来（没有响应），调用 recv 传入任意大小的块，你可能会因为读到后续接收的消息而停止读取。你需要将它放在一边并保存，直到它需要为止。

以其长度（例如，作为5个数字字符）作为消息前缀时会变得更复杂，因为（信不信由你）你可能无法在一个 recv 中获得所有5个字符。在一般使用时，你会侥幸避免该状况；但是在高网络负载中，除非你使用两个 recv 循环，否则你的代码将很快中断 —— 第一个用于确定长度，第二个用于获取消息的数据部分。这很讨厌。当你发现 send 并不总是设法在支持搞定一切时，你也会有这种感觉。 尽管已经阅读过这篇文章，但最终还是会有所了解！

限于篇幅，建立你的角色，（保持与我的竞争位置），这些改进将留给读者做为练习。现在让我们继续。

二进制数据

通过套接字传送二进制数据是可行的。主要问题在于并非所有机器都用同样的二进制数据格式。比如 Motorola 芯片用两个十六进制字节 00 01 来表示一个 16 位整数值 1。而 Intel 和 DEC 则会做字节反转 —— 即用 01 00 来表示 1。套接字库要求转换 16 位和 32 位整数 —— ntohl, htonl, ntohs, htons 其中的「n」表示 network，「h」表示 host，「s」表示 short，「l」表示 long。在网络序列就是主机序列时它们什么都不做，但是如果机器是字节反转的则会适当地交换字节序。

在现今的 32 位机器中，二进制数据的 ascii 表示往往比二进制表示要小。这是因为在非常多的时候所有 long 的值均为 0 或者 1。字符串形式的 “0” 为两个字节，而二进制形式则为四个。当然这不适用于固定长度的信息。自行决定，请自行决定。

断开连接

严格地讲，你应该在 close 它之前将套接字 shutdown 。 shutdown 是发送给套接字另一端的一种建议。调用时参数不同意义也不一样，它可能意味着「我不会再发送了，但我仍然会监听」，或者「我没有监听了，真棒！」。然而，大多数套接字库或者程序员都习惯了忽略使用这种礼节，因为通常情况下 close 与 shutdown(); close() 是一样的。所以在大多数情况下，不需要显式的 shutdown 。

高效使用 shutdown 的一种方法是在类似 HTTP 的交换中。客户端发送请求，然后执行 shutdown(1) 。 这告诉服务器“此客户端已完成发送，但仍可以接收”。服务器可以通过接收 0 字节来检测 “EOF” 。它可以假设它有完整的请求。服务器发送回复。如果 send 成功完成，那么客户端仍在接收。

Python 进一步自动关闭，并说当一个套接字被垃圾收集时，如果需要它会自动执行 close 。但依靠这个机制是一个非常坏的习惯。如果你的套接字在没有 close 的情况下就消失了，那么另一端的套接字可能会无限期地挂起，以为你只是慢了一步。完成后 请 close 你的套接字。

套接字何时销毁

使用阻塞套接字最糟糕的事情可能就是当另一边下线时（没有 close ）会发生什么。你的套接字可能会挂起。 TCP 是一种可靠的协议，它会在放弃连接之前等待很长时间。如果你正在使用线程，那么整个线程基本上已经死了。你无能为力。只要你没有做一些愚蠢的事情，比如在进行阻塞读取时持有一个锁，那么线程并没有真正消耗掉资源。 不要 尝试杀死线程 —— 线程比进程更有效的部分原因是它们避免了与自动回收资源相关的开销。换句话说，如果你设法杀死线程，你的整个进程很可能被搞坏。

非阻塞的套接字

如果你已理解上述内容，那么你已经了解了使用套接字的机制所需了解的大部分内容。你仍将以相同的方式使用相同的函数调用。 只是，如果你做得对，你的应用程序几乎是由内到外的。

In Python, you use socket.setblocking(0) to make it non-blocking. In C, it’s more complex, (for one thing, you’ll need to choose between the BSD flavor O_NONBLOCK and the almost indistinguishable Posix flavor O_NDELAY, which is completely different from TCP_NODELAY), but it’s the exact same idea. You do this after creating the socket, but before using it. (Actually, if you’re nuts, you can switch back and forth.)

主要的机制差异是 send 、 recv 、 connect 和 accept 可以在没有做任何事情的情况下返回。 你（当然）有很多选择。你可以检查返回代码和错误代码，通常会让自己发疯。如果你不相信我，请尝试一下。你的应用程序将变得越来越大、越来越 Bug 、吸干 CPU。因此，让我们跳过脑死亡的解决方案并做正确的事。

使用 select 库

在 C 中，编码 select 相当复杂。 在 Python 中，它是很简单，但它与 C 版本足够接近，如果你在 Python 中理解 select ，那么在 C 中你会几乎不会遇到麻烦:

ready_to_read, ready_to_write, in_error = \
               select.select(
                  potential_readers,
                  potential_writers,
                  potential_errs,
                  timeout)

你传递给 select 三个列表：第一个包含你可能想要尝试读取的所有套接字；第二个是你可能想要尝试写入的所有套接字，以及要检查错误的最后一个（通常为空）。你应该注意，套接字可以进入多个列表。 select 调用是阻塞的，但你可以给它一个超时。这通常是一件明智的事情 —— 给它一个很长的超时（比如一分钟），除非你有充分的理由不这样做。

作为返回，你将获得三个列表。它们包含实际可读、可写和有错误的套接字。 这些列表中的每一个都是你传入的相应列表的子集（可能为空）。

如果一个套接字在输出可读列表中，那么你可以像我们一样接近这个业务，那个套接字上的 recv 将返回 一些内容 。可写列表的也相同，你将能够发送 一些内容 。 也许不是你想要的全部，但 有些东西 比没有东西更好。 （实际上，任何合理健康的套接字都将以可写方式返回 —— 它只是意味着出站网络缓冲区空间可用。）

如果你有一个“服务器”套接字，请将其放在 potential_readers 列表中。如果它出现在可读列表中，那么你的 accept （几乎肯定）会起作用。如果你已经创建了一个新的套接字 connect 其他人，请将它放在 potential_writers 列表中。如果它出现在可写列表中，那么它有可能已连接。

实际上，即使使用阻塞套接字， select 也很方便。这是确定是否阻塞的一种方法 —— 当缓冲区中存在某些内容时，套接字返回为可读。然而，这仍然无助于确定另一端是否完成或者只是忙于其他事情的问题。

可移植性警告 ：在 Unix 上， select 适用于套接字和文件。 不要在 Windows 上尝试。在 Windows 上， select 仅适用于套接字。另请注意，在 C 中，许多更高级的套接字选项在 Windows 上的执行方式不同。事实上，在 Windows 上我通常在使用我的套接字使用线程（非常非常好）。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

排序指南

作者

Andrew Dalke 和 Raymond Hettinger

发布版本

0.1

Python 列表有一个内置的 list.sort() 方法可以直接修改列表。还有一个 sorted() 内置函数，它会从一个可迭代对象构建一个新的排序列表。

在本文档中，我们将探索使用Python对数据进行排序的各种技术。

基本排序

简单的升序排序非常简单：只需调用 sorted() 函数即可。它会返回一个新的已排序列表。

>>> sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]

你也可以使用 list.sort() 方法，它会直接修改原列表（并返回 None 以避免混淆），通常来说它不如 sorted() 方便 ——— 但如果你不需要原列表，它会更有效率。

>>> a = [5, 2, 3, 1, 4]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5]

另外一个区别是， list.sort() 方法只是为列表定义的，而 sorted() 函数可以接受任何可迭代对象。

>>> sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'})
[1, 2, 3, 4, 5]

关键函数

list.sort() 和 sorted() 都有一个 key 形参来指定在进行比较之前要在每个列表元素上进行调用的函数。

例如，下面是一个不区分大小写的字符串比较：

>>> sorted("This is a test string from Andrew".split(), key=str.lower)
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']

key 形参的值应该是一个函数，它接受一个参数并并返回一个用于排序的键。这种技巧速度很快，因为对于每个输入记录只会调用一次 key 函数。

一种常见的模式是使用对象的一些索引作为键对复杂对象进行排序。例如：

>>> student_tuples = [
...     ('john', 'A', 15),
...     ('jane', 'B', 12),
...     ('dave', 'B', 10),
... ]
>>> sorted(student_tuples, key=lambda student: student[2])   # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

同样的技术也适用于具有命名属性的对象。例如：

>>> class Student:
...     def __init__(self, name, grade, age):
...         self.name = name
...         self.grade = grade
...         self.age = age
...     def __repr__(self):
...         return repr((self.name, self.grade, self.age))

>>> student_objects = [
...     Student('john', 'A', 15),
...     Student('jane', 'B', 12),
...     Student('dave', 'B', 10),
... ]
>>> sorted(student_objects, key=lambda student: student.age)   # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

Operator 模块函数

上面显示的键函数模式非常常见，因此 Python 提供了便利功能，使访问器功能更容易，更快捷。 operator 模块有 itemgetter() 、 attrgetter() 和 methodcaller() 函数。

使用这些函数，上述示例变得更简单，更快捷：

>>> from operator import itemgetter, attrgetter

>>> sorted(student_tuples, key=itemgetter(2))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

>>> sorted(student_objects, key=attrgetter('age'))
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

Operator 模块功能允许多级排序。 例如，按 grade 排序，然后按 age 排序：

>>> sorted(student_tuples, key=itemgetter(1,2))
[('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]

>>> sorted(student_objects, key=attrgetter('grade', 'age'))
[('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]

升序和降序

list.sort() 和 sorted() 接受布尔值的 reverse 参数。这用于标记降序排序。 例如，要以反向 age 顺序获取学生数据：

>>> sorted(student_tuples, key=itemgetter(2), reverse=True)
[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]

>>> sorted(student_objects, key=attrgetter('age'), reverse=True)
[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]

排序稳定性和排序复杂度

排序保证是 稳定 [https://en.wikipedia.org/wiki/Sorting_algorithm#Stability] 的。 这意味着当多个记录具有相同的键值时，将保留其原始顺序。

>>> data = [('red', 1), ('blue', 1), ('red', 2), ('blue', 2)]
>>> sorted(data, key=itemgetter(0))
[('blue', 1), ('blue', 2), ('red', 1), ('red', 2)]

注意 blue 的两个记录如何保留它们的原始顺序，以便 ('blue', 1) 保证在 ('blue', 2) 之前。

这个美妙的属性允许你在一系列排序步骤中构建复杂的排序。例如，要按 grade 降序然后 age 升序对学生数据进行排序，请先 age 排序，然后再使用 grade 排序：

>>> s = sorted(student_objects, key=attrgetter('age'))     # sort on secondary key
>>> sorted(s, key=attrgetter('grade'), reverse=True)       # now sort on primary key, descending
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

Python 中使用的 Timsort [https://en.wikipedia.org/wiki/Timsort] 算法可以有效地进行多种排序，因为它可以利用数据集中已存在的任何排序。

使用装饰-排序-去装饰的旧方法

这个三个步骤被称为 Decorate-Sort-Undecorate ：

首先，初始列表使用控制排序顺序的新值进行修饰。

然后，装饰列表已排序。

最后，删除装饰，创建一个仅包含新排序中初始值的列表。

例如，要使用DSU方法按 grade 对学生数据进行排序：

>>> decorated = [(student.grade, i, student) for i, student in enumerate(student_objects)]
>>> decorated.sort()
>>> [student for grade, i, student in decorated]               # undecorate
[('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]

这方法语有效是因为元组按字典顺序进行比较，先比较第一项；如果它们相同则比较第二个项目，依此类推。

不一定在所有情况下都要在装饰列表中包含索引 i ，但包含它有两个好处：

排序是稳定的——如果两个项具有相同的键，它们的顺序将保留在排序列表中。

原始项目不必具有可比性，因为装饰元组的排序最多由前两项决定。 因此，例如原始列表可能包含无法直接排序的复数。

这个方法的另一个名字是 Randal L. Schwartz 在 Perl 程序员中推广的 Schwartzian transform [https://en.wikipedia.org/wiki/Schwartzian_transform]。

既然 Python 排序提供了键函数，那么通常不需要这种技术。

使用 cmp 参数的旧方法

本 HOWTO 中给出的许多结构都假定为 Python 2.4 或更高版本。在此之前，没有内置 sorted() ， list.sort() 也没有关键字参数。相反，所有 Py2.x 版本都支持 cmp 参数来处理用户指定的比较函数。

在 Py3.0 中， cmp 参数被完全删除（作为简化和统一语言努力的一部分，消除了丰富的比较与 __cmp__() 魔术方法之间的冲突）。

在 Py2.x 中， sort 允许一个可选函数，可以调用它来进行比较。该函数应该采用两个参数进行比较，然后返回负值为小于，如果它们相等则返回零，或者返回大于大于的正值。例如，我们可以这样做：

>>> def numeric_compare(x, y):
...     return x - y
>>> sorted([5, 2, 4, 1, 3], cmp=numeric_compare)
[1, 2, 3, 4, 5]

或者你可反转比较的顺序：

>>> def reverse_numeric(x, y):
...     return y - x
>>> sorted([5, 2, 4, 1, 3], cmp=reverse_numeric)
[5, 4, 3, 2, 1]

将代码从 Python 2.x 移植到 3.x 时，如果用户提供比较功能并且需要将其转换为键函数，则会出现这种情况。 以下包装器使这很容易：

def cmp_to_key(mycmp):
    'Convert a cmp= function into a key= function'
    class K:
        def __init__(self, obj, *args):
            self.obj = obj
        def __lt__(self, other):
            return mycmp(self.obj, other.obj) < 0
        def __gt__(self, other):
            return mycmp(self.obj, other.obj) > 0
        def __eq__(self, other):
            return mycmp(self.obj, other.obj) == 0
        def __le__(self, other):
            return mycmp(self.obj, other.obj) <= 0
        def __ge__(self, other):
            return mycmp(self.obj, other.obj) >= 0
        def __ne__(self, other):
            return mycmp(self.obj, other.obj) != 0
    return K

要转换为键函数，只需包装旧的比较函数：

>>> sorted([5, 2, 4, 1, 3], key=cmp_to_key(reverse_numeric))
[5, 4, 3, 2, 1]

在 Python 3.2 中， functools.cmp_to_key() 函数被添加到标准库中的 functools 模块中。

其它

对于区域相关的排序，请使用 locale.strxfrm() 作为键函数，或者 locale.strcoll() 作为比较函数。

reverse 参数仍然保持排序稳定性（因此具有相等键的记录保留原始顺序）。 有趣的是，通过使用内置的 reversed() 函数两次，可以在没有参数的情况下模拟该效果：

>>> data = [('red', 1), ('blue', 1), ('red', 2), ('blue', 2)]
>>> standard_way = sorted(data, key=itemgetter(0), reverse=True)
>>> double_reversed = list(reversed(sorted(reversed(data), key=itemgetter(0))))
>>> assert standard_way == double_reversed
>>> standard_way
[('red', 1), ('red', 2), ('blue', 1), ('blue', 2)]

在两个对象之间进行比较时，保证排序例程使用 __lt__() 。 因此，通过定义 __lt__() 方法，可以很容易地为类添加标准排序顺序:

>>> Student.__lt__ = lambda self, other: self.age < other.age
>>> sorted(student_objects)
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

键函数不需要直接依赖于被排序的对象。键函数还可以访问外部资源。例如，如果学生成绩存储在字典中，则可以使用它们对单独的学生姓名列表进行排序：

>>> students = ['dave', 'john', 'jane']
>>> newgrades = {'john': 'F', 'jane':'A', 'dave': 'C'}
>>> sorted(students, key=newgrades.__getitem__)
['jane', 'dave', 'john']

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

Unicode 指南

发布版本

1.12

This HOWTO discusses Python support for Unicode, and explains various problems that people commonly encounter when trying to work with Unicode.

Unicode 概述

History of Character Codes

In 1968, the American Standard Code for Information Interchange, better known by its acronym ASCII, was standardized. ASCII defined numeric codes for various characters, with the numeric values running from 0 to 127. For example, the lowercase letter ‘a’ is assigned 97 as its code value.

ASCII was an American-developed standard, so it only defined unaccented characters. There was an ‘e’, but no ‘é’ or ‘Í’. This meant that languages which required accented characters couldn’t be faithfully represented in ASCII. (Actually the missing accents matter for English, too, which contains words such as ‘naïve’ and ‘café’, and some publications have house styles which require spellings such as ‘coöperate’.)

For a while people just wrote programs that didn’t display accents. In the mid-1980s an Apple II BASIC program written by a French speaker might have lines like these:

PRINT "MISE A JOUR TERMINEE"
PRINT "PARAMETRES ENREGISTRES"

Those messages should contain accents (terminée, paramètre, enregistrés) and they just look wrong to someone who can read French.

In the 1980s, almost all personal computers were 8-bit, meaning that bytes could hold values ranging from 0 to 255. ASCII codes only went up to 127, so some machines assigned values between 128 and 255 to accented characters. Different machines had different codes, however, which led to problems exchanging files. Eventually various commonly used sets of values for the 128–255 range emerged. Some were true standards, defined by the International Organization for Standardization, and some were de facto conventions that were invented by one company or another and managed to catch on.

255 characters aren’t very many. For example, you can’t fit both the accented characters used in Western Europe and the Cyrillic alphabet used for Russian into the 128–255 range because there are more than 128 such characters.

You could write files using different codes (all your Russian files in a coding system called KOI8, all your French files in a different coding system called Latin1), but what if you wanted to write a French document that quotes some Russian text? In the 1980s people began to want to solve this problem, and the Unicode standardization effort began.

Unicode started out using 16-bit characters instead of 8-bit characters. 16 bits means you have 2^16 = 65,536 distinct values available, making it possible to represent many different characters from many different alphabets; an initial goal was to have Unicode contain the alphabets for every single human language. It turns out that even 16 bits isn’t enough to meet that goal, and the modern Unicode specification uses a wider range of codes, 0 through 1,114,111 ( 0x10FFFF in base 16).

There’s a related ISO standard, ISO 10646. Unicode and ISO 10646 were originally separate efforts, but the specifications were merged with the 1.1 revision of Unicode.

(This discussion of Unicode’s history is highly simplified. The precise historical details aren’t necessary for understanding how to use Unicode effectively, but if you’re curious, consult the Unicode consortium site listed in the References or the Wikipedia entry for Unicode [https://en.wikipedia.org/wiki/Unicode#History] for more information.)

定义

A character is the smallest possible component of a text. ‘A’, ‘B’, ‘C’, etc., are all different characters. So are ‘È’ and ‘Í’. Characters are abstractions, and vary depending on the language or context you’re talking about. For example, the symbol for ohms (Ω) is usually drawn much like the capital letter omega (Ω) in the Greek alphabet (they may even be the same in some fonts), but these are two different characters that have different meanings.

The Unicode standard describes how characters are represented by code points. A code point is an integer value, usually denoted in base 16. In the standard, a code point is written using the notation U+12CA to mean the character with value 0x12ca (4,810 decimal). The Unicode standard contains a lot of tables listing characters and their corresponding code points:

0061    'a'; LATIN SMALL LETTER A
0062    'b'; LATIN SMALL LETTER B
0063    'c'; LATIN SMALL LETTER C
...
007B    '{'; LEFT CURLY BRACKET

Strictly, these definitions imply that it’s meaningless to say ‘this is character U+12CA’. U+12CA is a code point, which represents some particular character; in this case, it represents the character ‘ETHIOPIC SYLLABLE WI’. In informal contexts, this distinction between code points and characters will sometimes be forgotten.

一个字符在屏幕上或在纸上被表示为一组图形元素，被称为**字形**。比如，大写字母 A 的字形，是斜向的两笔和水平的一笔，而具体的细节取决于所使用的字体。大部分 Python 代码不必担心字形，找到应被显示的正确字形一般来说是用户图形界面工具箱或者终端的字体渲染器的工作。

编码

To summarize the previous section: a Unicode string is a sequence of code points, which are numbers from 0 through 0x10FFFF (1,114,111 decimal). This sequence needs to be represented as a set of bytes (meaning, values from 0 through 255) in memory. The rules for translating a Unicode string into a sequence of bytes are called an encoding.

The first encoding you might think of is an array of 32-bit integers. In this representation, the string “Python” would look like this:

   P           y           t           h           o           n
0x50 00 00 00 79 00 00 00 74 00 00 00 68 00 00 00 6f 00 00 00 6e 00 00 00
   0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

这个表达方式非常直接，但同时也存在 一些问题。

不够方便；不同的处理器对字节的排序不同。

非常浪费空间。多数编码都小于127，或者255，所以很多空间都是 0x00。上面的字符串takes 24 bytes compared to the 6 bytes needed for an ASCII representation. Increased RAM usage doesn’t matter too much (desktop computers have gigabytes of RAM, and strings aren’t usually that large), but expanding our usage of disk and network bandwidth by a factor of 4 is intolerable.

It’s not compatible with existing C functions such as strlen(), so a new family of wide string functions would need to be used.

Many Internet standards are defined in terms of textual data, and can’t handle content with embedded zero bytes.

Generally people don’t use this encoding, instead choosing other encodings that are more efficient and convenient. UTF-8 is probably the most commonly supported encoding; it will be discussed below.

Encodings don’t have to handle every possible Unicode character, and most encodings don’t. The rules for converting a Unicode string into the ASCII encoding, for example, are simple; for each code point:

If the code point is < 128, each byte is the same as the value of the code point.

If the code point is 128 or greater, the Unicode string can’t be represented in this encoding. (Python raises a UnicodeEncodeError exception in this case.)

Latin-1, also known as ISO-8859-1, is a similar encoding. Unicode code points 0–255 are identical to the Latin-1 values, so converting to this encoding simply requires converting code points to byte values; if a code point larger than 255 is encountered, the string can’t be encoded into Latin-1.

Encodings don’t have to be simple one-to-one mappings like Latin-1. Consider IBM’s EBCDIC, which was used on IBM mainframes. Letter values weren’t in one block: ‘a’ through ‘i’ had values from 129 to 137, but ‘j’ through ‘r’ were 145 through 153. If you wanted to use EBCDIC as an encoding, you’d probably use some sort of lookup table to perform the conversion, but this is largely an internal detail.

UTF-8 is one of the most commonly used encodings. UTF stands for “Unicode Transformation Format”, and the ‘8’ means that 8-bit numbers are used in the encoding. (There are also a UTF-16 and UTF-32 encodings, but they are less frequently used than UTF-8.) UTF-8 uses the following rules:

If the code point is < 128, it’s represented by the corresponding byte value.

If the code point is >= 128, it’s turned into a sequence of two, three, or four bytes, where each byte of the sequence is between 128 and 255.

UTF-8 has several convenient properties:

It can handle any Unicode code point.

A Unicode string is turned into a sequence of bytes containing no embedded zero bytes. This avoids byte-ordering issues, and means UTF-8 strings can be processed by C functions such as strcpy() and sent through protocols that can’t handle zero bytes.

A string of ASCII text is also valid UTF-8 text.

UTF-8 is fairly compact; the majority of commonly used characters can be represented with one or two bytes.

If bytes are corrupted or lost, it’s possible to determine the start of the next UTF-8-encoded code point and resynchronize. It’s also unlikely that random 8-bit data will look like valid UTF-8.

参考文献

The Unicode Consortium site [http://www.unicode.org] has character charts, a glossary, and PDF versions of the Unicode specification. Be prepared for some difficult reading. A chronology [http://www.unicode.org/history/] of the origin and development of Unicode is also available on the site.

To help understand the standard, Jukka Korpela has written an introductory guide [https://www.cs.tut.fi/~jkorpela/unicode/guide.html] to reading the Unicode character tables.

Another good introductory article [http://www.joelonsoftware.com/articles/Unicode.html] was written by Joel Spolsky. If this introduction didn’t make things clear to you, you should try reading this alternate article before continuing.

Wikipedia entries are often helpful; see the entries for “character encoding [https://en.wikipedia.org/wiki/Character_encoding]” and UTF-8 [https://en.wikipedia.org/wiki/UTF-8], for example.

Python’s Unicode Support

Now that you’ve learned the rudiments of Unicode, we can look at Python’s Unicode features.

The String Type

Since Python 3.0, the language features a str type that contain Unicode characters, meaning any string created using "unicode rocks!", 'unicode rocks!', or the triple-quoted string syntax is stored as Unicode.

The default encoding for Python source code is UTF-8, so you can simply include a Unicode character in a string literal:

try:
    with open('/tmp/input.txt', 'r') as f:
        ...
except OSError:
    # 'File not found' error message.
    print("Fichier non trouvé")

You can use a different encoding from UTF-8 by putting a specially-formatted comment as the first or second line of the source code:

# -*- coding: <encoding name> -*-

Side note: Python 3 also supports using Unicode characters in identifiers:

répertoire = "/tmp/records.log"
with open(répertoire, "w") as f:
    f.write("test\n")

If you can’t enter a particular character in your editor or want to keep the source code ASCII-only for some reason, you can also use escape sequences in string literals. (Depending on your system, you may see the actual capital-delta glyph instead of a u escape.)

>>> "\N{GREEK CAPITAL LETTER DELTA}"  # Using the character name
'\u0394'
>>> "\u0394"                          # Using a 16-bit hex value
'\u0394'
>>> "\U00000394"                      # Using a 32-bit hex value
'\u0394'

In addition, one can create a string using the decode() method of bytes. This method takes an encoding argument, such as UTF-8, and optionally an errors argument.

The errors argument specifies the response when the input string can’t be converted according to the encoding’s rules. Legal values for this argument are 'strict' (raise a UnicodeDecodeError exception), 'replace' (use U+FFFD, REPLACEMENT CHARACTER), 'ignore' (just leave the character out of the Unicode result), or 'backslashreplace' (inserts a \xNN escape sequence). The following examples show the differences:

>>> b'\x80abc'.decode("utf-8", "strict")
Traceback (most recent call last):
    ...
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0:
  invalid start byte
>>> b'\x80abc'.decode("utf-8", "replace")
'\ufffdabc'
>>> b'\x80abc'.decode("utf-8", "backslashreplace")
'\\x80abc'
>>> b'\x80abc'.decode("utf-8", "ignore")
'abc'

Encodings are specified as strings containing the encoding’s name. Python 3.2 comes with roughly 100 different encodings; see the Python Library Reference at 标准编码 for a list. Some encodings have multiple names; for example, 'latin-1', 'iso_8859_1' and '8859’ are all synonyms for the same encoding.

One-character Unicode strings can also be created with the chr() built-in function, which takes integers and returns a Unicode string of length 1 that contains the corresponding code point. The reverse operation is the built-in ord() function that takes a one-character Unicode string and returns the code point value:

>>> chr(57344)
'\ue000'
>>> ord('\ue000')
57344

Converting to Bytes

The opposite method of bytes.decode() is str.encode(), which returns a bytes representation of the Unicode string, encoded in the requested encoding.

The errors parameter is the same as the parameter of the decode() method but supports a few more possible handlers. As well as 'strict', 'ignore', and 'replace' (which in this case inserts a question mark instead of the unencodable character), there is also 'xmlcharrefreplace' (inserts an XML character reference), backslashreplace (inserts a \uNNNN escape sequence) and namereplace (inserts a \N{...} escape sequence).

The following example shows the different results:

>>> u = chr(40960) + 'abcd' + chr(1972)
>>> u.encode('utf-8')
b'\xea\x80\x80abcd\xde\xb4'
>>> u.encode('ascii')
Traceback (most recent call last):
    ...
UnicodeEncodeError: 'ascii' codec can't encode character '\ua000' in
  position 0: ordinal not in range(128)
>>> u.encode('ascii', 'ignore')
b'abcd'
>>> u.encode('ascii', 'replace')
b'?abcd?'
>>> u.encode('ascii', 'xmlcharrefreplace')
b'&#40960;abcd&#1972;'
>>> u.encode('ascii', 'backslashreplace')
b'\\ua000abcd\\u07b4'
>>> u.encode('ascii', 'namereplace')
b'\\N{YI SYLLABLE IT}abcd\\u07b4'

The low-level routines for registering and accessing the available encodings are found in the codecs module. Implementing new encodings also requires understanding the codecs module. However, the encoding and decoding functions returned by this module are usually more low-level than is comfortable, and writing new encodings is a specialized task, so the module won’t be covered in this HOWTO.

Unicode Literals in Python Source Code

In Python source code, specific Unicode code points can be written using the \u escape sequence, which is followed by four hex digits giving the code point. The \U escape sequence is similar, but expects eight hex digits, not four:

>>> s = "a\xac\u1234\u20ac\U00008000"
... #     ^^^^ two-digit hex escape
... #         ^^^^^^ four-digit Unicode escape
... #                     ^^^^^^^^^^ eight-digit Unicode escape
>>> [ord(c) for c in s]
[97, 172, 4660, 8364, 32768]

Using escape sequences for code points greater than 127 is fine in small doses, but becomes an annoyance if you’re using many accented characters, as you would in a program with messages in French or some other accent-using language. You can also assemble strings using the chr() built-in function, but this is even more tedious.

Ideally, you’d want to be able to write literals in your language’s natural encoding. You could then edit Python source code with your favorite editor which would display the accented characters naturally, and have the right characters used at runtime.

Python supports writing source code in UTF-8 by default, but you can use almost any encoding if you declare the encoding being used. This is done by including a special comment as either the first or second line of the source file:

#!/usr/bin/env python
# -*- coding: latin-1 -*-

u = 'abcdé'
print(ord(u[-1]))

The syntax is inspired by Emacs’s notation for specifying variables local to a file. Emacs supports many different variables, but Python only supports ‘coding’. The -*- symbols indicate to Emacs that the comment is special; they have no significance to Python but are a convention. Python looks for coding: name or coding=name in the comment.

If you don’t include such a comment, the default encoding used will be UTF-8 as already mentioned. See also PEP 263 [https://www.python.org/dev/peps/pep-0263] for more information.

Unicode Properties

The Unicode specification includes a database of information about code points. For each defined code point, the information includes the character’s name, its category, the numeric value if applicable (Unicode has characters representing the Roman numerals and fractions such as one-third and four-fifths). There are also properties related to the code point’s use in bidirectional text and other display-related properties.

The following program displays some information about several characters, and prints the numeric value of one particular character:

import unicodedata

u = chr(233) + chr(0x0bf2) + chr(3972) + chr(6000) + chr(13231)

for i, c in enumerate(u):
    print(i, '%04x' % ord(c), unicodedata.category(c), end=" ")
    print(unicodedata.name(c))

# Get numeric value of second character
print(unicodedata.numeric(u[1]))

When run, this prints:

0 00e9 Ll LATIN SMALL LETTER E WITH ACUTE
1 0bf2 No TAMIL NUMBER ONE THOUSAND
2 0f84 Mn TIBETAN MARK HALANTA
3 1770 Lo TAGBANWA LETTER SA
4 33af So SQUARE RAD OVER S SQUARED
1000.0

The category codes are abbreviations describing the nature of the character. These are grouped into categories such as “Letter”, “Number”, “Punctuation”, or “Symbol”, which in turn are broken up into subcategories. To take the codes from the above output, 'Ll' means ‘Letter, lowercase’, 'No' means “Number, other”, 'Mn' is “Mark, nonspacing”, and 'So' is “Symbol, other”. See the General Category Values section of the Unicode Character Database documentation [http://www.unicode.org/reports/tr44/#General_Category_Values] for a list of category codes.

Unicode Regular Expressions

The regular expressions supported by the re module can be provided either as bytes or strings. Some of the special character sequences such as \d and \w have different meanings depending on whether the pattern is supplied as bytes or a string. For example, \d will match the characters [0-9] in bytes but in strings will match any character that’s in the 'Nd' category.

The string in this example has the number 57 written in both Thai and Arabic numerals:

import re
p = re.compile(r'\d+')

s = "Over \u0e55\u0e57 57 flavours"
m = p.search(s)
print(repr(m.group()))

When executed, \d+ will match the Thai numerals and print them out. If you supply the re.ASCII flag to compile(), \d+ will match the substring “57” instead.

Similarly, \w matches a wide variety of Unicode characters but only [a-zA-Z0-9_] in bytes or if re.ASCII is supplied, and \s will match either Unicode whitespace characters or [ \t\n\r\f\v].

参考文献

Some good alternative discussions of Python’s Unicode support are:

Processing Text Files in Python 3 [http://python-notes.curiousefficiency.org/en/latest/python3/text_file_processing.html], by Nick Coghlan.

Pragmatic Unicode [http://nedbatchelder.com/text/unipain.html], a PyCon 2012 presentation by Ned Batchelder.

The str type is described in the Python library reference at 文本序列类型 — str.

The documentation for the unicodedata module.

The documentation for the codecs module.

Marc-André Lemburg gave a presentation titled “Python and Unicode” (PDF slides) [https://downloads.egenix.com/python/Unicode-EPC2002-Talk.pdf] at EuroPython 2002. The slides are an excellent overview of the design of Python 2’s Unicode features (where the Unicode string type is called unicode and literals start with u).

Reading and Writing Unicode Data

Once you’ve written some code that works with Unicode data, the next problem is input/output. How do you get Unicode strings into your program, and how do you convert Unicode into a form suitable for storage or transmission?

It’s possible that you may not need to do anything depending on your input sources and output destinations; you should check whether the libraries used in your application support Unicode natively. XML parsers often return Unicode data, for example. Many relational databases also support Unicode-valued columns and can return Unicode values from an SQL query.

Unicode data is usually converted to a particular encoding before it gets written to disk or sent over a socket. It’s possible to do all the work yourself: open a file, read an 8-bit bytes object from it, and convert the bytes with bytes.decode(encoding). However, the manual approach is not recommended.

One problem is the multi-byte nature of encodings; one Unicode character can be represented by several bytes. If you want to read the file in arbitrary-sized chunks (say, 1024 or 4096 bytes), you need to write error-handling code to catch the case where only part of the bytes encoding a single Unicode character are read at the end of a chunk. One solution would be to read the entire file into memory and then perform the decoding, but that prevents you from working with files that are extremely large; if you need to read a 2 GiB file, you need 2 GiB of RAM. (More, really, since for at least a moment you’d need to have both the encoded string and its Unicode version in memory.)

The solution would be to use the low-level decoding interface to catch the case of partial coding sequences. The work of implementing this has already been done for you: the built-in open() function can return a file-like object that assumes the file’s contents are in a specified encoding and accepts Unicode parameters for methods such as read() and write(). This works through open()’s encoding and errors parameters which are interpreted just like those in str.encode() and bytes.decode().

Reading Unicode from a file is therefore simple:

with open('unicode.txt', encoding='utf-8') as f:
    for line in f:
        print(repr(line))

It’s also possible to open files in update mode, allowing both reading and writing:

with open('test', encoding='utf-8', mode='w+') as f:
    f.write('\u4500 blah blah blah\n')
    f.seek(0)
    print(repr(f.readline()[:1]))

The Unicode character U+FEFF is used as a byte-order mark (BOM), and is often written as the first character of a file in order to assist with autodetection of the file’s byte ordering. Some encodings, such as UTF-16, expect a BOM to be present at the start of a file; when such an encoding is used, the BOM will be automatically written as the first character and will be silently dropped when the file is read. There are variants of these encodings, such as ‘utf-16-le’ and ‘utf-16-be’ for little-endian and big-endian encodings, that specify one particular byte ordering and don’t skip the BOM.

In some areas, it is also convention to use a “BOM” at the start of UTF-8 encoded files; the name is misleading since UTF-8 is not byte-order dependent. The mark simply announces that the file is encoded in UTF-8. Use the ‘utf-8-sig’ codec to automatically skip the mark if present for reading such files.

Unicode filenames

Most of the operating systems in common use today support filenames that contain arbitrary Unicode characters. Usually this is implemented by converting the Unicode string into some encoding that varies depending on the system. For example, Mac OS X uses UTF-8 while Windows uses a configurable encoding; on Windows, Python uses the name “mbcs” to refer to whatever the currently configured encoding is. On Unix systems, there will only be a filesystem encoding if you’ve set the LANG or LC_CTYPE environment variables; if you haven’t, the default encoding is UTF-8.

The sys.getfilesystemencoding() function returns the encoding to use on your current system, in case you want to do the encoding manually, but there’s not much reason to bother. When opening a file for reading or writing, you can usually just provide the Unicode string as the filename, and it will be automatically converted to the right encoding for you:

filename = 'filename\u4500abc'
with open(filename, 'w') as f:
    f.write('blah\n')

Functions in the os module such as os.stat() will also accept Unicode filenames.

The os.listdir() function returns filenames and raises an issue: should it return the Unicode version of filenames, or should it return bytes containing the encoded versions? os.listdir() will do both, depending on whether you provided the directory path as bytes or a Unicode string. If you pass a Unicode string as the path, filenames will be decoded using the filesystem’s encoding and a list of Unicode strings will be returned, while passing a byte path will return the filenames as bytes. For example, assuming the default filesystem encoding is UTF-8, running the following program:

fn = 'filename\u4500abc'
f = open(fn, 'w')
f.close()

import os
print(os.listdir(b'.'))
print(os.listdir('.'))

will produce the following output:

amk:~$ python t.py
[b'filename\xe4\x94\x80abc', ...]
['filename\u4500abc', ...]

The first list contains UTF-8-encoded filenames, and the second list contains the Unicode versions.

Note that on most occasions, the Unicode APIs should be used. The bytes APIs should only be used on systems where undecodable file names can be present, i.e. Unix systems.

Tips for Writing Unicode-aware Programs

This section provides some suggestions on writing software that deals with Unicode.

The most important tip is:

Software should only work with Unicode strings internally, decoding the input data as soon as possible and encoding the output only at the end.

If you attempt to write processing functions that accept both Unicode and byte strings, you will find your program vulnerable to bugs wherever you combine the two different kinds of strings. There is no automatic encoding or decoding: if you do e.g. str + bytes, a TypeError will be raised.

When using data coming from a web browser or some other untrusted source, a common technique is to check for illegal characters in a string before using the string in a generated command line or storing it in a database. If you’re doing this, be careful to check the decoded string, not the encoded bytes data; some encodings may have interesting properties, such as not being bijective or not being fully ASCII-compatible. This is especially true if the input data also specifies the encoding, since the attacker can then choose a clever way to hide malicious text in the encoded bytestream.

Converting Between File Encodings

The StreamRecoder class can transparently convert between encodings, taking a stream that returns data in encoding #1 and behaving like a stream returning data in encoding #2.

For example, if you have an input file f that’s in Latin-1, you can wrap it with a StreamRecoder to return bytes encoded in UTF-8:

new_f = codecs.StreamRecoder(f,
    # en/decoder: used by read() to encode its results and
    # by write() to decode its input.
    codecs.getencoder('utf-8'), codecs.getdecoder('utf-8'),

    # reader/writer: used to read and write to the stream.
    codecs.getreader('latin-1'), codecs.getwriter('latin-1') )

Files in an Unknown Encoding

What can you do if you need to make a change to a file, but don’t know the file’s encoding? If you know the encoding is ASCII-compatible and only want to examine or modify the ASCII parts, you can open the file with the surrogateescape error handler:

with open(fname, 'r', encoding="ascii", errors="surrogateescape") as f:
    data = f.read()

# make changes to the string 'data'

with open(fname + '.new', 'w',
          encoding="ascii", errors="surrogateescape") as f:
    f.write(data)

The surrogateescape error handler will decode any non-ASCII bytes as code points in the Unicode Private Use Area ranging from U+DC80 to U+DCFF. These private code points will then be turned back into the same bytes when the surrogateescape error handler is used when encoding the data and writing it back out.

参考文献

One section of Mastering Python 3 Input/Output [http://pyvideo.org/video/289/pycon-2010--mastering-python-3-i-o], a PyCon 2010 talk by David Beazley, discusses text processing and binary data handling.

The PDF slides for Marc-André Lemburg’s presentation “Writing Unicode-aware Applications in Python” [https://downloads.egenix.com/python/LSM2005-Developing-Unicode-aware-applications-in-Python.pdf] discuss questions of character encodings as well as how to internationalize and localize an application. These slides cover Python 2.x only.

The Guts of Unicode in Python [http://pyvideo.org/video/1768/the-guts-of-unicode-in-python] is a PyCon 2013 talk by Benjamin Peterson that discusses the internal Unicode representation in Python 3.3.

致谢

The initial draft of this document was written by Andrew Kuchling. It has since been revised further by Alexander Belopolsky, Georg Brandl, Andrew Kuchling, and Ezio Melotti.

Thanks to the following people who have noted errors or offered suggestions on this article: Éric Araujo, Nicholas Bastin, Nick Coghlan, Marius Gedminas, Kent Johnson, Ken Krugler, Marc-André Lemburg, Martin von Löwis, Terry J. Reedy, Chad Whitacre.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

HOWTO 使用 urllib 包获取网络资源

作者

Michael Foord [http://www.voidspace.org.uk/python/index.shtml]

注解

这份 HOWTO 文档的早期版本有一份法语的译文，可在 urllib2 - Le Manuel manquant [http://www.voidspace.org.uk/python/articles/urllib2_francais.shtml] 处查阅。

概述

Related Articles

关于使用 Python 获取网页资源，你或许还可以找到下列有用的文章：

基本的验证 [http://www.voidspace.org.uk/python/articles/authentication.shtml]

关于 基本的验证 的入门指南，带有一些 Python 的示例。

urllib.request 是一个用于获取 URL （统一资源定位地址）的 Python 模块。它以 urlopen 函数的形式提供了一个非常简单的接口。该接口能够使用不同的协议获取 URL。同时它也提供了一个略微复杂的接口来处理常见情形——如：基本验证、cookies、代理等等。这些功能是通过叫做 handlers 和 opener 的对象来提供的。

urllib.request 支持多种 “URL 网址方案” （通过 URL中 ":" 之前的字符串加以区分——如 URL 地址 "ftp://python.org/"` 中的 ``"ftp"`） ，使用与之相关的网络协议（如：FTP、 HTTP）来获取 URL 资源。本指南重点关注最常用的情形—— HTTP。

For straightforward situations urlopen is very easy to use. But as soon as you encounter errors or non-trivial cases when opening HTTP URLs, you will need some understanding of the HyperText Transfer Protocol. The most comprehensive and authoritative reference to HTTP is RFC 2616 [https://tools.ietf.org/html/rfc2616.html]. This is a technical document and not intended to be easy to read. This HOWTO aims to illustrate using urllib, with enough detail about HTTP to help you through. It is not intended to replace the urllib.request docs, but is supplementary to them.

提取URL

下面是使用 urllib.request 最简单的方式：

import urllib.request
with urllib.request.urlopen('http://python.org/') as response:
   html = response.read()

如果你想通过 URL 获取资源并保存某个临时的地方，你可以通过 shutil.copyfileobj() 和 tempfile.NamedTemporaryFile() 函数:

import shutil
import tempfile
import urllib.request

with urllib.request.urlopen('http://python.org/') as response:
    with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
        shutil.copyfileobj(response, tmp_file)

with open(tmp_file.name) as html:
    pass

urllib很易于使用（注意URL不仅仅可以以’http:’开头，也可以是’ftp:’，’file:’等）。但是，这篇教程的目的是介绍更加复杂的用法，大多数是以HTTP举例。

HTTP基于请求和回应——客户端像服务器请求，服务器回应。urllib.request将你的HTTP请求保存为一个``Request``对象。在最简单的情况下，一个Request对象里包含你所请求的特定URL。以当前的Request对象作为参数调用``urlopen``返回服务器对你正在请求的URL的回应。回应是个文件类对象，所以你可以调用如``.read()``等命令。

import urllib.request

req = urllib.request.Request('http://www.voidspace.org.uk')
with urllib.request.urlopen(req) as response:
   the_page = response.read()

注意urllib.request中的Request接口也支持处理所有的协议。比如，你可以像这样做一个 FTP 请求：

req = urllib.request.Request('ftp://example.com/')

在 HTTP 的情况下，Request 对象允许你做两件额外的事：一，你可以向服务器发送数据。二，你可以向服务器发送额外的信息（“元数据”）： 关于 数据或请求本身的。信息将以“HTTP头”的方式发过去。让我们一个个看过去。

数据

有时候你想要给一个 URL 发送数据（通常这个URL指向一个CGI（通用网关接口）脚本或者其他 web 应用）。 对于 HTTP，这通常使用一个 POST 请求来完成。 比如在浏览器上提交一个 HTML 表单。 但并不是所有的 POST 都来自表单：你能使用一个 POST 来传输任何数据到你自己的应用上。 在使用常见的 HTML 表单的情况下，数据需要以标准的方式编码，然后再作为 data 参数传给 Request 对象。 编码需要使用一个来自 urllib.parse 库的函数。

import urllib.parse
import urllib.request

url = 'http://www.someserver.com/cgi-bin/register.cgi'
values = {'name' : 'Michael Foord',
          'location' : 'Northampton',
          'language' : 'Python' }

data = urllib.parse.urlencode(values)
data = data.encode('ascii') # data should be bytes
req = urllib.request.Request(url, data)
with urllib.request.urlopen(req) as response:
   the_page = response.read()

Note that other encodings are sometimes required (e.g. for file upload from HTML forms - see HTML Specification, Form Submission [https://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13] for more details).

If you do not pass the data argument, urllib uses a GET request. One way in which GET and POST requests differ is that POST requests often have “side-effects”: they change the state of the system in some way (for example by placing an order with the website for a hundredweight of tinned spam to be delivered to your door). Though the HTTP standard makes it clear that POSTs are intended to always cause side-effects, and GET requests never to cause side-effects, nothing prevents a GET request from having side-effects, nor a POST requests from having no side-effects. Data can also be passed in an HTTP GET request by encoding it in the URL itself.

具体操作如下:

>>> import urllib.request
>>> import urllib.parse
>>> data = {}
>>> data['name'] = 'Somebody Here'
>>> data['location'] = 'Northampton'
>>> data['language'] = 'Python'
>>> url_values = urllib.parse.urlencode(data)
>>> print(url_values)  # The order may differ from below.
name=Somebody+Here&language=Python&location=Northampton
>>> url = 'http://www.example.com/example.cgi'
>>> full_url = url + '?' + url_values
>>> data = urllib.request.urlopen(full_url)

Notice that the full URL is created by adding a ? to the URL, followed by the encoded values.

Headers

We’ll discuss here one particular HTTP header, to illustrate how to add headers to your HTTP request.

Some websites 1 dislike being browsed by programs, or send different versions to different browsers 2. By default urllib identifies itself as Python-urllib/x.y (where x and y are the major and minor version numbers of the Python release, e.g. Python-urllib/2.5), which may confuse the site, or just plain not work. The way a browser identifies itself is through the User-Agent header 3. When you create a Request object you can pass a dictionary of headers in. The following example makes the same request as above, but identifies itself as a version of Internet Explorer 4.

import urllib.parse
import urllib.request

url = 'http://www.someserver.com/cgi-bin/register.cgi'
user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'
values = {'name': 'Michael Foord',
          'location': 'Northampton',
          'language': 'Python' }
headers = {'User-Agent': user_agent}

data = urllib.parse.urlencode(values)
data = data.encode('ascii')
req = urllib.request.Request(url, data, headers)
with urllib.request.urlopen(req) as response:
   the_page = response.read()

The response also has two useful methods. See the section on info and geturl which comes after we have a look at what happens when things go wrong.

处理异常

urlopen raises URLError when it cannot handle a response (though as usual with Python APIs, built-in exceptions such as ValueError, TypeError etc. may also be raised).

HTTPError is the subclass of URLError raised in the specific case of HTTP URLs.

异常类从 urllib.error 模块中导出。

URLError

通常，引发 URLError 的原因是没有网络连接（或者没有到指定服务器的路由），或者指定的服务器不存在。该情况下，将会引发该异常，并带有一个 ‘reason’ 属性，该属性是一个包含错误代码和文本错误信息的元组。

例如

>>> req = urllib.request.Request('http://www.pretend_server.org')
>>> try: urllib.request.urlopen(req)
... except urllib.error.URLError as e:
...     print(e.reason)
...
(4, 'getaddrinfo failed')

HTTPError

从服务器返回的每个 HTTP 响应都包含一个数字的 “状态码”。有时该状态码表明服务器无法完成该请求。默认的处理器（函数？）将会为你处理这其中的一些响应。（例如，如果响应包含了 “redirection”，将会要求客户端去向另外的 URL 获取文档，urllib 将会为你处理该情形）。对于那些它无法处理的（状态代码），urlopen 将会引发一个 HTTPError 。典型的错误包括：‘404’（页面无法找到）、‘403’（请求遭拒绝）和 ’401‘ （需要身份验证）。

See section 10 of RFC 2616 [https://tools.ietf.org/html/rfc2616.html] for a reference on all the HTTP error codes.

The HTTPError instance raised will have an integer ‘code’ attribute, which corresponds to the error sent by the server.

错误代码

Because the default handlers handle redirects (codes in the 300 range), and codes in the 100–299 range indicate success, you will usually only see error codes in the 400–599 range.

http.server.BaseHTTPRequestHandler.responses is a useful dictionary of response codes in that shows all the response codes used by RFC 2616 [https://tools.ietf.org/html/rfc2616.html]. The dictionary is reproduced here for convenience

# Table mapping response codes to messages; entries have the
# form {code: (shortmessage, longmessage)}.
responses = {
    100: ('Continue', 'Request received, please continue'),
    101: ('Switching Protocols',
          'Switching to new protocol; obey Upgrade header'),

    200: ('OK', 'Request fulfilled, document follows'),
    201: ('Created', 'Document created, URL follows'),
    202: ('Accepted',
          'Request accepted, processing continues off-line'),
    203: ('Non-Authoritative Information', 'Request fulfilled from cache'),
    204: ('No Content', 'Request fulfilled, nothing follows'),
    205: ('Reset Content', 'Clear input form for further input.'),
    206: ('Partial Content', 'Partial content follows.'),

    300: ('Multiple Choices',
          'Object has several resources -- see URI list'),
    301: ('Moved Permanently', 'Object moved permanently -- see URI list'),
    302: ('Found', 'Object moved temporarily -- see URI list'),
    303: ('See Other', 'Object moved -- see Method and URL list'),
    304: ('Not Modified',
          'Document has not changed since given time'),
    305: ('Use Proxy',
          'You must use proxy specified in Location to access this '
          'resource.'),
    307: ('Temporary Redirect',
          'Object moved temporarily -- see URI list'),

    400: ('Bad Request',
          'Bad request syntax or unsupported method'),
    401: ('Unauthorized',
          'No permission -- see authorization schemes'),
    402: ('Payment Required',
          'No payment -- see charging schemes'),
    403: ('Forbidden',
          'Request forbidden -- authorization will not help'),
    404: ('Not Found', 'Nothing matches the given URI'),
    405: ('Method Not Allowed',
          'Specified method is invalid for this server.'),
    406: ('Not Acceptable', 'URI not available in preferred format.'),
    407: ('Proxy Authentication Required', 'You must authenticate with '
          'this proxy before proceeding.'),
    408: ('Request Timeout', 'Request timed out; try again later.'),
    409: ('Conflict', 'Request conflict.'),
    410: ('Gone',
          'URI no longer exists and has been permanently removed.'),
    411: ('Length Required', 'Client must specify Content-Length.'),
    412: ('Precondition Failed', 'Precondition in headers is false.'),
    413: ('Request Entity Too Large', 'Entity is too large.'),
    414: ('Request-URI Too Long', 'URI is too long.'),
    415: ('Unsupported Media Type', 'Entity body in unsupported format.'),
    416: ('Requested Range Not Satisfiable',
          'Cannot satisfy request range.'),
    417: ('Expectation Failed',
          'Expect condition could not be satisfied.'),

    500: ('Internal Server Error', 'Server got itself in trouble'),
    501: ('Not Implemented',
          'Server does not support this operation'),
    502: ('Bad Gateway', 'Invalid responses from another server/proxy.'),
    503: ('Service Unavailable',
          'The server cannot process the request due to a high load'),
    504: ('Gateway Timeout',
          'The gateway server did not receive a timely response'),
    505: ('HTTP Version Not Supported', 'Cannot fulfill request.'),
    }

When an error is raised the server responds by returning an HTTP error code and an error page. You can use the HTTPError instance as a response on the page returned. This means that as well as the code attribute, it also has read, geturl, and info, methods as returned by the urllib.response module:

>>> req = urllib.request.Request('http://www.python.org/fish.html')
>>> try:
...     urllib.request.urlopen(req)
... except urllib.error.HTTPError as e:
...     print(e.code)
...     print(e.read())
...
404
b'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n\n\n<html
  ...
  <title>Page Not Found</title>\n
  ...

包装起来

So if you want to be prepared for HTTPError or URLError there are two basic approaches. I prefer the second approach.

数字1

from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
req = Request(someurl)
try:
    response = urlopen(req)
except HTTPError as e:
    print('The server couldn\'t fulfill the request.')
    print('Error code: ', e.code)
except URLError as e:
    print('We failed to reach a server.')
    print('Reason: ', e.reason)
else:
    # everything is fine

注解

The except HTTPError must come first, otherwise except URLError will also catch an HTTPError.

Number 2

from urllib.request import Request, urlopen
from urllib.error import URLError
req = Request(someurl)
try:
    response = urlopen(req)
except URLError as e:
    if hasattr(e, 'reason'):
        print('We failed to reach a server.')
        print('Reason: ', e.reason)
    elif hasattr(e, 'code'):
        print('The server couldn\'t fulfill the request.')
        print('Error code: ', e.code)
else:
    # everything is fine

info and geturl

由 urlopen （或者 HTTPError 实例）所返回的响应包含两个有用的方法： info() 和 geturl()，该响应由模块 urllib.response 定义。

geturl - 返回所获取页面的真实 URL。该方法很有用，因为 urlopen （或者所使用的 opener 对象）可能回包括一次重定向。所获取页面的 URL 未必就是所请求的 URL 。

info - 该方法返回一个类似字典的对象，描述了所获取的页面，特别是由服务器送出的头部信息（headers） 。目前它是一个 http.client.HTTPMessage 实例。

Typical headers include ‘Content-length’, ‘Content-type’, and so on. See the Quick Reference to HTTP Headers [https://www.cs.tut.fi/~jkorpela/http.html] for a useful listing of HTTP headers with brief explanations of their meaning and use.

Openers and Handlers

When you fetch a URL you use an opener (an instance of the perhaps confusingly-named urllib.request.OpenerDirector). Normally we have been using the default opener - via urlopen - but you can create custom openers. Openers use handlers. All the “heavy lifting” is done by the handlers. Each handler knows how to open URLs for a particular URL scheme (http, ftp, etc.), or how to handle an aspect of URL opening, for example HTTP redirections or HTTP cookies.

You will want to create openers if you want to fetch URLs with specific handlers installed, for example to get an opener that handles cookies, or to get an opener that does not handle redirections.

To create an opener, instantiate an OpenerDirector, and then call .add_handler(some_handler_instance) repeatedly.

Alternatively, you can use build_opener, which is a convenience function for creating opener objects with a single function call. build_opener adds several handlers by default, but provides a quick way to add more and/or override the default handlers.

Other sorts of handlers you might want to can handle proxies, authentication, and other common but slightly specialised situations.

install_opener can be used to make an opener object the (global) default opener. This means that calls to urlopen will use the opener you have installed.

Opener objects have an open method, which can be called directly to fetch urls in the same way as the urlopen function: there’s no need to call install_opener, except as a convenience.

基本认证

To illustrate creating and installing a handler we will use the HTTPBasicAuthHandler. For a more detailed discussion of this subject – including an explanation of how Basic Authentication works - see the Basic Authentication Tutorial [http://www.voidspace.org.uk/python/articles/authentication.shtml].

When authentication is required, the server sends a header (as well as the 401 error code) requesting authentication. This specifies the authentication scheme and a ‘realm’. The header looks like: WWW-Authenticate: SCHEME realm="REALM".

例如

WWW-Authenticate: Basic realm="cPanel Users"

The client should then retry the request with the appropriate name and password for the realm included as a header in the request. This is ‘basic authentication’. In order to simplify this process we can create an instance of HTTPBasicAuthHandler and an opener to use this handler.

The HTTPBasicAuthHandler uses an object called a password manager to handle the mapping of URLs and realms to passwords and usernames. If you know what the realm is (from the authentication header sent by the server), then you can use a HTTPPasswordMgr. Frequently one doesn’t care what the realm is. In that case, it is convenient to use HTTPPasswordMgrWithDefaultRealm. This allows you to specify a default username and password for a URL. This will be supplied in the absence of you providing an alternative combination for a specific realm. We indicate this by providing None as the realm argument to the add_password method.

The top-level URL is the first URL that requires authentication. URLs “deeper” than the URL you pass to .add_password() will also match.

# create a password manager
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()

# Add the username and password.
# If we knew the realm, we could use it instead of None.
top_level_url = "http://example.com/foo/"
password_mgr.add_password(None, top_level_url, username, password)

handler = urllib.request.HTTPBasicAuthHandler(password_mgr)

# create "opener" (OpenerDirector instance)
opener = urllib.request.build_opener(handler)

# use the opener to fetch a URL
opener.open(a_url)

# Install the opener.
# Now all calls to urllib.request.urlopen use our opener.
urllib.request.install_opener(opener)

注解

In the above example we only supplied our HTTPBasicAuthHandler to build_opener. By default openers have the handlers for normal situations – ProxyHandler (if a proxy setting such as an http_proxy environment variable is set), UnknownHandler, HTTPHandler, HTTPDefaultErrorHandler, HTTPRedirectHandler, FTPHandler, FileHandler, DataHandler, HTTPErrorProcessor.

top_level_url is in fact either a full URL (including the ‘http:’ scheme component and the hostname and optionally the port number) e.g. "http://example.com/" or an “authority” (i.e. the hostname, optionally including the port number) e.g. "example.com" or "example.com:8080" (the latter example includes a port number). The authority, if present, must NOT contain the “userinfo” component - for example "joe:password@example.com" is not correct.

代理

urllib will auto-detect your proxy settings and use those. This is through the ProxyHandler, which is part of the normal handler chain when a proxy setting is detected. Normally that’s a good thing, but there are occasions when it may not be helpful 5. One way to do this is to setup our own ProxyHandler, with no proxies defined. This is done using similar steps to setting up a Basic Authentication handler:

>>> proxy_support = urllib.request.ProxyHandler({})
>>> opener = urllib.request.build_opener(proxy_support)
>>> urllib.request.install_opener(opener)

注解

Currently urllib.request does not support fetching of https locations through a proxy. However, this can be enabled by extending urllib.request as shown in the recipe 6.

注解

HTTP_PROXY will be ignored if a variable REQUEST_METHOD is set; see the documentation on getproxies().

Sockets and Layers

The Python support for fetching resources from the web is layered. urllib uses the http.client library, which in turn uses the socket library.

As of Python 2.3 you can specify how long a socket should wait for a response before timing out. This can be useful in applications which have to fetch web pages. By default the socket module has no timeout and can hang. Currently, the socket timeout is not exposed at the http.client or urllib.request levels. However, you can set the default timeout globally for all sockets using

import socket
import urllib.request

# timeout in seconds
timeout = 10
socket.setdefaulttimeout(timeout)

# this call to urllib.request.urlopen now uses the default timeout
# we have set in the socket module
req = urllib.request.Request('http://www.voidspace.org.uk')
response = urllib.request.urlopen(req)

备注

这篇文档由 John Lee 审订。

1

例如 Google。

2

Browser sniffing is a very bad practice for website design - building sites using web standards is much more sensible. Unfortunately a lot of sites still send different versions to different browsers.

3

The user agent for MSIE 6 is ‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)’

4

For details of more HTTP request headers, see Quick Reference to HTTP Headers [https://www.cs.tut.fi/~jkorpela/http.html].

5

In my case I have to use a proxy to access the internet at work. If you attempt to fetch localhost URLs through this proxy it blocks them. IE is set to use the proxy, which urllib picks up on. In order to test scripts with a localhost server, I have to prevent urllib from using the proxy.

6

urllib opener for SSL proxy (CONNECT method): ASPN Cookbook Recipe [https://code.activestate.com/recipes/456195/].

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

Argparse 教程

作者

Tshepang Lekhonkhobe

这篇教程旨在作为 argparse 的入门介绍，此模块是 Python 标准库中推荐的命令行解析模块。

注解

还有另外两个模块可以完成同样的任务，称为 getopt (对应于 C 语言中的 getopt() 函数) 和被弃用的 optparse。还要注意 argparse 是基于 optparse 的，因此用法与其非常相似。

概念

让我们利用 ls 命令来展示我们将要在这篇入门教程中探索的功能：

$ ls
cpython  devguide  prog.py  pypy  rm-unused-function.patch
$ ls pypy
ctypes_configure  demo  dotviewer  include  lib_pypy  lib-python ...
$ ls -l
total 20
drwxr-xr-x 19 wena wena 4096 Feb 18 18:51 cpython
drwxr-xr-x  4 wena wena 4096 Feb  8 12:04 devguide
-rwxr-xr-x  1 wena wena  535 Feb 19 00:05 prog.py
drwxr-xr-x 14 wena wena 4096 Feb  7 00:59 pypy
-rw-r--r--  1 wena wena  741 Feb 18 01:01 rm-unused-function.patch
$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
...

我们可以从这四个命令中学到几个概念：

ls 是一个即使在运行的时候没有提供任何选项，也非常有用的命令。在默认情况下他会输出当前文件夹包含的文件和文件夹。

如果我们想要使用比它默认提供的更多功能，我们需要告诉该命令更多信息。在这个例子里，我们想要查看一个不同的目录，pypy。我们所做的是指定所谓的位置参数。之所以这样命名，是因为程序应该如何处理该参数值，完全取决于它在命令行出现的位置。更能体现这个概念的命令如 cp，它最基本的用法是 cp SRC DEST。第一个位置参数指的是*你想要复制的*，第二个位置参数指的是*你想要复制到的位置*。

现在假设我们想要改变这个程序的行为。在我们的例子中，我们不仅仅只是输出每个文件的文件名，还输出了更多信息。在这个例子中，-l 被称为可选参数。

这是一段帮助文档的文字。它是非常有用的，因为当你遇到一个你从未使用过的程序时，你可以通过阅读它的帮助文档来弄清楚它是如何运行的。

基础

让我们从一个简单到（几乎）什么也做不了的例子开始：

import argparse
parser = argparse.ArgumentParser()
parser.parse_args()

以下是该代码的运行结果：

$ python3 prog.py
$ python3 prog.py --help
usage: prog.py [-h]

optional arguments:
  -h, --help  show this help message and exit
$ python3 prog.py --verbose
usage: prog.py [-h]
prog.py: error: unrecognized arguments: --verbose
$ python3 prog.py foo
usage: prog.py [-h]
prog.py: error: unrecognized arguments: foo

程序运行情况如下：

在没有任何选项的情况下运行脚本不会在标准输出显示任何内容。这没有什么用处。

第二行代码开始展现出 argparse 模块的作用。我们几乎什么也没有做，但已经得到一条很好的帮助信息。

--help 选项，也可缩写为 -h，是唯一一个可以直接使用的选项（即不需要指定该选项的内容）。指定任何内容都会导致错误。即便如此，我们也能直接得到一条有用的用法信息。

位置参数介绍

举个例子：

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo")
args = parser.parse_args()
print(args.echo)

运行此程序：

$ python3 prog.py
usage: prog.py [-h] echo
prog.py: error: the following arguments are required: echo
$ python3 prog.py --help
usage: prog.py [-h] echo

positional arguments:
  echo

optional arguments:
  -h, --help  show this help message and exit
$ python3 prog.py foo
foo

程序运行情况如下：

我们增加了 add_argument() 方法，该方法用于指定程序能够接受哪些命令行选项。在这个例子中，我将选项命名为 echo，与其功能一致。

现在调用我们的程序必须要指定一个选项。

The parse_args() method actually returns some data from the options specified, in this case, echo.

这一变量是 argparse 免费施放的某种 “魔法”（即是说，不需要指定哪个变量是存储哪个值的）。你也可以注意到，这一名称与传递给方法的字符串参数一致，都是 echo。

然而请注意，尽管显示的帮助看起来清楚完整，但它可以比现在更有帮助。比如我们可以知道 echo 是一个位置参数，但我们除了靠猜或者看源代码，没法知道它是用来干什么的。所以，我们可以把它改造得更有用：

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo", help="echo the string you use here")
args = parser.parse_args()
print(args.echo)

然后我们得到：

$ python3 prog.py -h
usage: prog.py [-h] echo

positional arguments:
  echo        echo the string you use here

optional arguments:
  -h, --help  show this help message and exit

现在，做一些更有用的事情怎么样:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", help="display a square of a given number")
args = parser.parse_args()
print(args.square**2)

以下是该代码的运行结果：

$ python3 prog.py 4
Traceback (most recent call last):
  File "prog.py", line 5, in <module>
    print(args.square**2)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

进展不太顺利。那是因为 argparse 会把我们传递给它的选项视作为字符串，除非我们告诉它别这样。所以，让我们来告诉 argparse 来把这一输入视为整数：

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", help="display a square of a given number",
                    type=int)
args = parser.parse_args()
print(args.square**2)

以下是该代码的运行结果：

$ python3 prog.py 4
16
$ python3 prog.py four
usage: prog.py [-h] square
prog.py: error: argument square: invalid int value: 'four'

做得不错。当这个程序在收到错误的无效的输入时，它甚至能在执行计算之前先退出，还能显示很有帮助的错误信息。

可选参数介绍

到目前为止，我们一直在研究位置参数。让我们看看如何添加可选的:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--verbosity", help="increase output verbosity")
args = parser.parse_args()
if args.verbosity:
    print("verbosity turned on")

和输出：

$ python3 prog.py --verbosity 1
verbosity turned on
$ python3 prog.py
$ python3 prog.py --help
usage: prog.py [-h] [--verbosity VERBOSITY]

optional arguments:
  -h, --help            show this help message and exit
  --verbosity VERBOSITY
                        increase output verbosity
$ python3 prog.py --verbosity
usage: prog.py [-h] [--verbosity VERBOSITY]
prog.py: error: argument --verbosity: expected one argument

程序运行情况如下：

这一程序被设计为当指定 --verbosity 参数时显示某些东西，否则不显示。

不添加这一选项时程序没有提示任何错误而退出，表明这一选项确实是可选的。注意，如果一个可选参数没有被使用时，相关变量被赋值为 None，在此例中是 args.verbosity，这也就是为什么它在 if 语句中被当作逻辑假。

帮助信息有点不同。

使用 --verbosity 选项时，必须指定一些值（任何值）。

上述例子接受任何整数值作为 --verbosity 的参数，但对于我们的简单程序而言，只有两个值有实际意义：True 或者 False。让我们据此修改代码：

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", help="increase output verbosity",
                    action="store_true")
args = parser.parse_args()
if args.verbose:
    print("verbosity turned on")

和输出：

$ python3 prog.py --verbose
verbosity turned on
$ python3 prog.py --verbose 1
usage: prog.py [-h] [--verbose]
prog.py: error: unrecognized arguments: 1
$ python3 prog.py --help
usage: prog.py [-h] [--verbose]

optional arguments:
  -h, --help  show this help message and exit
  --verbose   increase output verbosity

程序运行情况如下：

现在，这一选项更多地是一个标志，而非需要接受一个值的什么东西。我们甚至改变了选项的名字来符合这一思路。注意我们现在指定了一个新的关键词 action，并赋值为 "store_true"。这意味着，当这一选项存在时，为 args.verbose 赋值为 True。没有指定时则隐含地赋值为 False。

当你为其指定一个值时，它会报错，符合作为标志的真正的精神。

留意不同的帮助文字。

短选项

如果你熟悉命令行的用法，你会发现我还没讲到这一选项的短版本。这也很简单：

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="increase output verbosity",
                    action="store_true")
args = parser.parse_args()
if args.verbose:
    print("verbosity turned on")

效果就像这样：

$ python3 prog.py -v
verbosity turned on
$ python3 prog.py --help
usage: prog.py [-h] [-v]

optional arguments:
  -h, --help     show this help message and exit
  -v, --verbose  increase output verbosity

注意，这一新的能力也反映在帮助文本里。

结合位置参数和可选参数

我们的程序变得越来越复杂了:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type=int,
                    help="display a square of a given number")
parser.add_argument("-v", "--verbose", action="store_true",
                    help="increase output verbosity")
args = parser.parse_args()
answer = args.square**2
if args.verbose:
    print("the square of {} equals {}".format(args.square, answer))
else:
    print(answer)

接着是输出：

$ python3 prog.py
usage: prog.py [-h] [-v] square
prog.py: error: the following arguments are required: square
$ python3 prog.py 4
16
$ python3 prog.py 4 --verbose
the square of 4 equals 16
$ python3 prog.py --verbose 4
the square of 4 equals 16

我们带回了一个位置参数，结果发生了报错。

注意顺序无关紧要。

给我们的程序加上接受多个冗长度的值，然后实际来用用：

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type=int,
                    help="display a square of a given number")
parser.add_argument("-v", "--verbosity", type=int,
                    help="increase output verbosity")
args = parser.parse_args()
answer = args.square**2
if args.verbosity == 2:
    print("the square of {} equals {}".format(args.square, answer))
elif args.verbosity == 1:
    print("{}^2 == {}".format(args.square, answer))
else:
    print(answer)

和输出：

$ python3 prog.py 4
16
$ python3 prog.py 4 -v
usage: prog.py [-h] [-v VERBOSITY] square
prog.py: error: argument -v/--verbosity: expected one argument
$ python3 prog.py 4 -v 1
4^2 == 16
$ python3 prog.py 4 -v 2
the square of 4 equals 16
$ python3 prog.py 4 -v 3
16

除了最后一个，看上去都不错。最后一个暴露了我们的程序中有一个 bug。我们可以通过限制 --verbosity 选项可以接受的值来修复它：

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type=int,
                    help="display a square of a given number")
parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2],
                    help="increase output verbosity")
args = parser.parse_args()
answer = args.square**2
if args.verbosity == 2:
    print("the square of {} equals {}".format(args.square, answer))
elif args.verbosity == 1:
    print("{}^2 == {}".format(args.square, answer))
else:
    print(answer)

和输出：

$ python3 prog.py 4 -v 3
usage: prog.py [-h] [-v {0,1,2}] square
prog.py: error: argument -v/--verbosity: invalid choice: 3 (choose from 0, 1, 2)
$ python3 prog.py 4 -h
usage: prog.py [-h] [-v {0,1,2}] square

positional arguments:
  square                display a square of a given number

optional arguments:
  -h, --help            show this help message and exit
  -v {0,1,2}, --verbosity {0,1,2}
                        increase output verbosity

注意这一改变同时反应在错误信息和帮助信息里。

现在，让我们使用另一种的方式来改变冗长度。这种方式更常见，也和 CPython 的可执行文件处理它自己的冗长度参数的方式一致（参考 python --help 的输出）：

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type=int,
                    help="display the square of a given number")
parser.add_argument("-v", "--verbosity", action="count",
                    help="increase output verbosity")
args = parser.parse_args()
answer = args.square**2
if args.verbosity == 2:
    print("the square of {} equals {}".format(args.square, answer))
elif args.verbosity == 1:
    print("{}^2 == {}".format(args.square, answer))
else:
    print(answer)

我们引入了另一种动作 count，来数某一个可选参数出现了几次：

$ python3 prog.py 4
16
$ python3 prog.py 4 -v
4^2 == 16
$ python3 prog.py 4 -vv
the square of 4 equals 16
$ python3 prog.py 4 --verbosity --verbosity
the square of 4 equals 16
$ python3 prog.py 4 -v 1
usage: prog.py [-h] [-v] square
prog.py: error: unrecognized arguments: 1
$ python3 prog.py 4 -h
usage: prog.py [-h] [-v] square

positional arguments:
  square           display a square of a given number

optional arguments:
  -h, --help       show this help message and exit
  -v, --verbosity  increase output verbosity
$ python3 prog.py 4 -vvv
16

是的，它现在比之前更像是一个标志（和 action="store_true" 相似）。这能解释它为什么报错。

它也表现得与 “store_true” 的行为相似。

这给出了一个关于 count 动作的效果的演示。你之前很可能应该已经看过这种用法。

如果你不添加 -v 标志，这一标志的值会是 None。

如期望的那样，添加该标志的长形态能够获得相同的输出。

可惜的是，对于我们的脚本获得的新能力，我们的帮助输出并没有提供很多信息，但我们总是可以通过改善文档来修复这一问题（比如通过 help 关键字参数）。

最后一个输出暴露了我们程序中的一个 bug。

让我们修复一下:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type=int,
                    help="display a square of a given number")
parser.add_argument("-v", "--verbosity", action="count",
                    help="increase output verbosity")
args = parser.parse_args()
answer = args.square**2

# bugfix: replace == with >=
if args.verbosity >= 2:
    print("the square of {} equals {}".format(args.square, answer))
elif args.verbosity >= 1:
    print("{}^2 == {}".format(args.square, answer))
else:
    print(answer)

这是它给我们的输出：

$ python3 prog.py 4 -vvv
the square of 4 equals 16
$ python3 prog.py 4 -vvvv
the square of 4 equals 16
$ python3 prog.py 4
Traceback (most recent call last):
  File "prog.py", line 11, in <module>
    if args.verbosity >= 2:
TypeError: '>=' not supported between instances of 'NoneType' and 'int'

第一组输出很好，修复了之前的 bug。也就是说，我们希望任何 >= 2 的值尽可能详尽。

第三组输出并不理想。

让我们修复那个 bug:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type=int,
                    help="display a square of a given number")
parser.add_argument("-v", "--verbosity", action="count", default=0,
                    help="increase output verbosity")
args = parser.parse_args()
answer = args.square**2
if args.verbosity >= 2:
    print("the square of {} equals {}".format(args.square, answer))
elif args.verbosity >= 1:
    print("{}^2 == {}".format(args.square, answer))
else:
    print(answer)

我们刚刚引入了又一个新的关键字 default。我们把它设置为 0 来让它可以与其他整数值相互比较。记住，默认情况下如果一个可选参数没有被指定，它的值会是 None，并且它不能和整数值相比较（所以产生了 TypeError 异常）。

然后：

$ python3 prog.py 4
16

凭借我们目前已学的东西你就可以做到许多事情，而我们还仅仅学了一些皮毛而已。 argparse 模块是非常强大的，在结束篇教程之前我们将再探索更多一些内容。

进行一些小小的改进

如果我们想扩展我们的简短程序来执行其他幂次的运算，而不仅是乘方:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("x", type=int, help="the base")
parser.add_argument("y", type=int, help="the exponent")
parser.add_argument("-v", "--verbosity", action="count", default=0)
args = parser.parse_args()
answer = args.x**args.y
if args.verbosity >= 2:
    print("{} to the power {} equals {}".format(args.x, args.y, answer))
elif args.verbosity >= 1:
    print("{}^{} == {}".format(args.x, args.y, answer))
else:
    print(answer)

输出：

$ python3 prog.py
usage: prog.py [-h] [-v] x y
prog.py: error: the following arguments are required: x, y
$ python3 prog.py -h
usage: prog.py [-h] [-v] x y

positional arguments:
  x                the base
  y                the exponent

optional arguments:
  -h, --help       show this help message and exit
  -v, --verbosity
$ python3 prog.py 4 2 -v
4^2 == 16

请注意到目前为止我们一直在使用详细级别来 更改 所显示的文本。 以下示例则使用详细级别来显示 更多的 文本:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("x", type=int, help="the base")
parser.add_argument("y", type=int, help="the exponent")
parser.add_argument("-v", "--verbosity", action="count", default=0)
args = parser.parse_args()
answer = args.x**args.y
if args.verbosity >= 2:
    print("Running '{}'".format(__file__))
if args.verbosity >= 1:
    print("{}^{} == ".format(args.x, args.y), end="")
print(answer)

输出：

$ python3 prog.py 4 2
16
$ python3 prog.py 4 2 -v
4^2 == 16
$ python3 prog.py 4 2 -vv
Running 'prog.py'
4^2 == 16

矛盾的选项

到目前为止，我们一直在使用 argparse.ArgumentParser 实例的两个方法。 让我们再介绍第三个方法 add_mutually_exclusive_group()。 它允许我们指定彼此相互冲突的选项。 让我们再更改程序的其余部分以便使用新功能更有意义：我们将引入 --quiet 选项，它将与 --verbose 正好相反:

import argparse

parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument("-v", "--verbose", action="store_true")
group.add_argument("-q", "--quiet", action="store_true")
parser.add_argument("x", type=int, help="the base")
parser.add_argument("y", type=int, help="the exponent")
args = parser.parse_args()
answer = args.x**args.y

if args.quiet:
    print(answer)
elif args.verbose:
    print("{} to the power {} equals {}".format(args.x, args.y, answer))
else:
    print("{}^{} == {}".format(args.x, args.y, answer))

我们的程序现在变得更简洁了，我们出于演示需要略去了一些功能。 无论如何，输出是这样的:

$ python3 prog.py 4 2
4^2 == 16
$ python3 prog.py 4 2 -q
16
$ python3 prog.py 4 2 -v
4 to the power 2 equals 16
$ python3 prog.py 4 2 -vq
usage: prog.py [-h] [-v | -q] x y
prog.py: error: argument -q/--quiet: not allowed with argument -v/--verbose
$ python3 prog.py 4 2 -v --quiet
usage: prog.py [-h] [-v | -q] x y
prog.py: error: argument -q/--quiet: not allowed with argument -v/--verbose

这应该很容易理解。 我添加了末尾的输出这样你就可以看到其所达到的灵活性，即混合使用长和短两种形式的选项。

在我们收尾之前，你也许希望告诉你的用户这个程序的主要目标，以免他们还不清楚:

import argparse

parser = argparse.ArgumentParser(description="calculate X to the power of Y")
group = parser.add_mutually_exclusive_group()
group.add_argument("-v", "--verbose", action="store_true")
group.add_argument("-q", "--quiet", action="store_true")
parser.add_argument("x", type=int, help="the base")
parser.add_argument("y", type=int, help="the exponent")
args = parser.parse_args()
answer = args.x**args.y

if args.quiet:
    print(answer)
elif args.verbose:
    print("{} to the power {} equals {}".format(args.x, args.y, answer))
else:
    print("{}^{} == {}".format(args.x, args.y, answer))

请注意用法文本中有细微的差异。 注意 [-v | -q]，它的意思是说我们可以使用 -v 或 -q，但不能同时使用两者:

$ python3 prog.py --help
usage: prog.py [-h] [-v | -q] x y

calculate X to the power of Y

positional arguments:
  x              the base
  y              the exponent

optional arguments:
  -h, --help     show this help message and exit
  -v, --verbose
  -q, --quiet

后记

除了这里显示的内容，argparse 模块还提供了更多功能。 它的文档相当详细和完整，包含大量示例。 完成这个教程之后，你应该能毫不困难地阅读该文档。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

ipaddress模块介绍

作者

Peter Moody

作者

Nick Coghlan

概述

本文档旨在简要介绍 ipaddress 模块。 它主要针对那些不熟悉 IP 网络术语的用户，但也可能对想要速览 ipaddress 如何代表IP网络寻址概念的网络工程师有用。

创建 Address/Network/Interface 对象

因为 ipaddress 是一个用于检查和操作 IP 地址的模块，你要做的第一件事就是创建一些对象。 您可以使用 ipaddress 从字符串和整数创建对象。

关于IP版本的说明

对于不太熟悉 IP 寻址的读者，重要的是要知道 Internet 协议当前正在从协议的版本4转移到版本6。转换很大程度上是因为协议的版本4没有提供足够的地址来满足整个世界的需求，特别是考虑到越来越多的设备直接连接到互联网。

解释协议的两个版本之间的差异的细节超出了本介绍的范围，但读者需要至少知道存在这两个版本，并且有时需要强制使用一个版本或其他版本。

IP主机地址

通常称为“主机地址”的地址是使用IP寻址时最基本的单元。 创建地址的最简单方法是使用 ipaddress.ip_address() 工厂函数，该函数根据传入的值自动确定是创建 IPv4 还是 IPv6 地址：

>>> ipaddress.ip_address('192.0.2.1')
IPv4Address('192.0.2.1')
>>> ipaddress.ip_address('2001:DB8::1')
IPv6Address('2001:db8::1')

地址也可以直接从整数创建，适配32位的值并假定为IPv4地址:

>>> ipaddress.ip_address(3221225985)
IPv4Address('192.0.2.1')
>>> ipaddress.ip_address(42540766411282592856903984951653826561)
IPv6Address('2001:db8::1')

要强制使用IPv4或IPv6地址，可以直接调用相关的类。 这对于强制为小整数创建IPv6地址特别有用:

>>> ipaddress.ip_address(1)
IPv4Address('0.0.0.1')
>>> ipaddress.IPv4Address(1)
IPv4Address('0.0.0.1')
>>> ipaddress.IPv6Address(1)
IPv6Address('::1')

定义网络

主机地址通常组合在一起形成IP网络，因此 ipaddress 提供了一种创建、检查和操作网络定义的方法。 IP网络对象由字符串构成，这些字符串定义作为该网络一部分的主机地址范围。 该信息的最简单形式是“网络地址/网络前缀”对，其中前缀定义了比较的前导比特数，以确定地址是否是网络的一部分，并且网络地址定义了那些位的预期值。

对于地址，提供了一个自动确定正确IP版本的工厂函数:

>>> ipaddress.ip_network('192.0.2.0/24')
IPv4Network('192.0.2.0/24')
>>> ipaddress.ip_network('2001:db8::0/96')
IPv6Network('2001:db8::/96')

网络对象不能设置任何主机位。 这样做的实际效果是``192.0.2.1/24``没有描述网络。 这种定义被称为接口对象，因为网络上IP表示法通常用于描述给定网络上的计算机的网络接口，并在下一节中进一步描述。

默认情况下，尝试创建一个设置了主机位的网络对象将导致 ValueError 被引发。 要请求将附加位强制为零，可以将标志``strict=False`` 传递给构造函数:

>>> ipaddress.ip_network('192.0.2.1/24')
Traceback (most recent call last):
   ...
ValueError: 192.0.2.1/24 has host bits set
>>> ipaddress.ip_network('192.0.2.1/24', strict=False)
IPv4Network('192.0.2.0/24')

虽然字符串形式提供了更大的灵活性，但网络也可以用整数定义，就像主机地址一样。 在这种情况下，网络被认为只包含由整数标识的单个地址，因此网络前缀包括整个网络地址:

>>> ipaddress.ip_network(3221225984)
IPv4Network('192.0.2.0/32')
>>> ipaddress.ip_network(42540766411282592856903984951653826560)
IPv6Network('2001:db8::/128')

与地址一样，可以通过直接调用类构造函数而不是使用工厂函数来强制创建特定类型的网络。

主机接口

如上所述，如果您需要描述特定网络上的地址，则地址和网络类都不够。 像 192.0.2.1/24 这样的表示法通常被网络工程师和为防火墙和路由器编写工具的人用作“ 192.0.2.0/24 网络上的主机 192.0.2.1 ”的简写。因此，ipaddress 提供了一组将地址与特定网络相关联的混合类。用于创建的接口与用于定义网络对象的接口相同，除了地址部分不限于是网络地址。

>>> ipaddress.ip_interface('192.0.2.1/24')
IPv4Interface('192.0.2.1/24')
>>> ipaddress.ip_interface('2001:db8::1/96')
IPv6Interface('2001:db8::1/96')

接受整数输入（与网络一样），并且可以通过直接调用相关构造函数来强制使用特定IP版本。

审查 Address/Network/Interface 对象

你已经遇到了创建IPv(4|6)(Address|Network|Interface) 对象的麻烦，因此你可能希望获得有关它的信息。 ipaddress 试图让这个过程变得简单直观。

提取 IP 版本:

>>> addr4 = ipaddress.ip_address('192.0.2.1')
>>> addr6 = ipaddress.ip_address('2001:db8::1')
>>> addr6.version
6
>>> addr4.version
4

从接口获取网络:

>>> host4 = ipaddress.ip_interface('192.0.2.1/24')
>>> host4.network
IPv4Network('192.0.2.0/24')
>>> host6 = ipaddress.ip_interface('2001:db8::1/96')
>>> host6.network
IPv6Network('2001:db8::/96')

找出网络中有多少独立地址:

>>> net4 = ipaddress.ip_network('192.0.2.0/24')
>>> net4.num_addresses
256
>>> net6 = ipaddress.ip_network('2001:db8::0/96')
>>> net6.num_addresses
4294967296

迭代网络上的“可用”地址:

>>> net4 = ipaddress.ip_network('192.0.2.0/24')
>>> for x in net4.hosts():
...     print(x)
192.0.2.1
192.0.2.2
192.0.2.3
192.0.2.4
...
192.0.2.252
192.0.2.253
192.0.2.254

获取网络掩码（即对应于网络前缀的设置位）或主机掩码（不属于网络掩码的任何位）：

>>> net4 = ipaddress.ip_network('192.0.2.0/24')
>>> net4.netmask
IPv4Address('255.255.255.0')
>>> net4.hostmask
IPv4Address('0.0.0.255')
>>> net6 = ipaddress.ip_network('2001:db8::0/96')
>>> net6.netmask
IPv6Address('ffff:ffff:ffff:ffff:ffff:ffff::')
>>> net6.hostmask
IPv6Address('::ffff:ffff')

展开或压缩地址:

>>> addr6.exploded
'2001:0db8:0000:0000:0000:0000:0000:0001'
>>> addr6.compressed
'2001:db8::1'
>>> net6.exploded
'2001:0db8:0000:0000:0000:0000:0000:0000/96'
>>> net6.compressed
'2001:db8::/96'

虽然IPv4不支持展开或压缩，但关联对象仍提供相关属性，因此版本中性代码可以轻松确保最简洁或最详细的形式用于IPv6地址，同时仍能正确处理IPv4地址。

Network 作为 Address 列表

将网络视为列表有时很有用。 这意味着它可以像这样索引它们:

>>> net4[1]
IPv4Address('192.0.2.1')
>>> net4[-1]
IPv4Address('192.0.2.255')
>>> net6[1]
IPv6Address('2001:db8::1')
>>> net6[-1]
IPv6Address('2001:db8::ffff:ffff')

它还意味着网络对象可以使用像这样的列表成员测试语法:

if address in network:
    # do something

根据网络前缀有效地完成包含性测试:

>>> addr4 = ipaddress.ip_address('192.0.2.1')
>>> addr4 in ipaddress.ip_network('192.0.2.0/24')
True
>>> addr4 in ipaddress.ip_network('192.0.3.0/24')
False

比较运算

ipaddress 有意义地提供了一些简单、希望直观的比较对象的方法:

>>> ipaddress.ip_address('192.0.2.1') < ipaddress.ip_address('192.0.2.2')
True

如果你尝试比较不同版本或不同类型的对象，则会引发 TypeError 异常。

将IP地址与其他模块一起使用

其他使用IP地址的模块（例如 socket ）通常不会直接接受来自该模块的对象。 相反，它们必须被强制转换为另一个模块可接受的整数或字符串:

>>> addr4 = ipaddress.ip_address('192.0.2.1')
>>> str(addr4)
'192.0.2.1'
>>> int(addr4)
3221225985

实例创建失败时获取更多详细信息

使用与版本无关的工厂函数创建 address/network/interface 对象时，任何错误都将报告为 ValueError ，带有一般错误消息，只是说传入的值未被识别为该类型的对象。 缺少特定错误是因为有必要知道该值是*假设*是IPv4还是IPv6，以便提供有关其被拒绝原因的更多详细信息。

为了支持访问这些额外细节的用例，各个类构造函数实际上引发了 ValueError 子类 ipaddress.AddressValueError 和 ipaddress.NetmaskValueError 以准确指示定义的哪一部分无法正确解析。

直接使用类构造函数时，错误消息更加详细。 例如:

>>> ipaddress.ip_address("192.168.0.256")
Traceback (most recent call last):
  ...
ValueError: '192.168.0.256' does not appear to be an IPv4 or IPv6 address
>>> ipaddress.IPv4Address("192.168.0.256")
Traceback (most recent call last):
  ...
ipaddress.AddressValueError: Octet 256 (> 255) not permitted in '192.168.0.256'

>>> ipaddress.ip_network("192.168.0.1/64")
Traceback (most recent call last):
  ...
ValueError: '192.168.0.1/64' does not appear to be an IPv4 or IPv6 network
>>> ipaddress.IPv4Network("192.168.0.1/64")
Traceback (most recent call last):
  ...
ipaddress.NetmaskValueError: '64' is not a valid netmask

但是，两个模块特定的异常都有 ValueError 作为它们的父类，所以如果你不关心特定类型的错误，你仍然可以编写如下代码:

try:
    network = ipaddress.IPv4Network(address)
except ValueError:
    print('address/netmask is invalid for IPv4:', address)

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

Argument Clinic How-To

作者

Larry Hastings

摘要

Argument Clinic is a preprocessor for CPython C files. Its purpose is to automate all the boilerplate involved with writing argument parsing code for “builtins”. This document shows you how to convert your first C function to work with Argument Clinic, and then introduces some advanced topics on Argument Clinic usage.

Currently Argument Clinic is considered internal-only for CPython. Its use is not supported for files outside CPython, and no guarantees are made regarding backwards compatibility for future versions. In other words: if you maintain an external C extension for CPython, you’re welcome to experiment with Argument Clinic in your own code. But the version of Argument Clinic that ships with the next version of CPython could be totally incompatible and break all your code.

The Goals Of Argument Clinic

Argument Clinic’s primary goal is to take over responsibility for all argument parsing code inside CPython. This means that, when you convert a function to work with Argument Clinic, that function should no longer do any of its own argument parsing—the code generated by Argument Clinic should be a “black box” to you, where CPython calls in at the top, and your code gets called at the bottom, with PyObject *args (and maybe PyObject *kwargs) magically converted into the C variables and types you need.

In order for Argument Clinic to accomplish its primary goal, it must be easy to use. Currently, working with CPython’s argument parsing library is a chore, requiring maintaining redundant information in a surprising number of places. When you use Argument Clinic, you don’t have to repeat yourself.

Obviously, no one would want to use Argument Clinic unless it’s solving their problem—and without creating new problems of its own. So it’s paramount that Argument Clinic generate correct code. It’d be nice if the code was faster, too, but at the very least it should not introduce a major speed regression. (Eventually Argument Clinic should make a major speedup possible—we could rewrite its code generator to produce tailor-made argument parsing code, rather than calling the general-purpose CPython argument parsing library. That would make for the fastest argument parsing possible!)

Additionally, Argument Clinic must be flexible enough to work with any approach to argument parsing. Python has some functions with some very strange parsing behaviors; Argument Clinic’s goal is to support all of them.

Finally, the original motivation for Argument Clinic was to provide introspection “signatures” for CPython builtins. It used to be, the introspection query functions would throw an exception if you passed in a builtin. With Argument Clinic, that’s a thing of the past!

One idea you should keep in mind, as you work with Argument Clinic: the more information you give it, the better job it’ll be able to do. Argument Clinic is admittedly relatively simple right now. But as it evolves it will get more sophisticated, and it should be able to do many interesting and smart things with all the information you give it.

Basic Concepts And Usage

Argument Clinic ships with CPython; you’ll find it in Tools/clinic/clinic.py. If you run that script, specifying a C file as an argument:

$ python3 Tools/clinic/clinic.py foo.c

Argument Clinic will scan over the file looking for lines that look exactly like this:

/*[clinic input]

When it finds one, it reads everything up to a line that looks exactly like this:

[clinic start generated code]*/

Everything in between these two lines is input for Argument Clinic. All of these lines, including the beginning and ending comment lines, are collectively called an Argument Clinic “block”.

When Argument Clinic parses one of these blocks, it generates output. This output is rewritten into the C file immediately after the block, followed by a comment containing a checksum. The Argument Clinic block now looks like this:

/*[clinic input]
... clinic input goes here ...
[clinic start generated code]*/
... clinic output goes here ...
/*[clinic end generated code: checksum=...]*/

If you run Argument Clinic on the same file a second time, Argument Clinic will discard the old output and write out the new output with a fresh checksum line. However, if the input hasn’t changed, the output won’t change either.

You should never modify the output portion of an Argument Clinic block. Instead, change the input until it produces the output you want. (That’s the purpose of the checksum—to detect if someone changed the output, as these edits would be lost the next time Argument Clinic writes out fresh output.)

For the sake of clarity, here’s the terminology we’ll use with Argument Clinic:

The first line of the comment (/*[clinic input]) is the start line.

The last line of the initial comment ([clinic start generated code]*/) is the end line.

The last line (/*[clinic end generated code: checksum=...]*/) is the checksum line.

In between the start line and the end line is the input.

In between the end line and the checksum line is the output.

All the text collectively, from the start line to the checksum line inclusively, is the block. (A block that hasn’t been successfully processed by Argument Clinic yet doesn’t have output or a checksum line, but it’s still considered a block.)

Converting Your First Function

The best way to get a sense of how Argument Clinic works is to convert a function to work with it. Here, then, are the bare minimum steps you’d need to follow to convert a function to work with Argument Clinic. Note that for code you plan to check in to CPython, you really should take the conversion farther, using some of the advanced concepts you’ll see later on in the document (like “return converters” and “self converters”). But we’ll keep it simple for this walkthrough so you can learn.

Let’s dive in!

Make sure you’re working with a freshly updated checkout of the CPython trunk.

Find a Python builtin that calls either PyArg_ParseTuple() or PyArg_ParseTupleAndKeywords(), and hasn’t been converted to work with Argument Clinic yet. For my example I’m using _pickle.Pickler.dump().

If the call to the PyArg_Parse function uses any of the following format units:

O&
O!
es
es#
et
et#

or if it has multiple calls to PyArg_ParseTuple(), you should choose a different function. Argument Clinic does support all of these scenarios. But these are advanced topics—let’s do something simpler for your first function.

Also, if the function has multiple calls to PyArg_ParseTuple() or PyArg_ParseTupleAndKeywords() where it supports different types for the same argument, or if the function uses something besides PyArg_Parse functions to parse its arguments, it probably isn’t suitable for conversion to Argument Clinic. Argument Clinic doesn’t support generic functions or polymorphic parameters.

Add the following boilerplate above the function, creating our block:

/*[clinic input]
[clinic start generated code]*/

Cut the docstring and paste it in between the [clinic] lines, removing all the junk that makes it a properly quoted C string. When you’re done you should have just the text, based at the left margin, with no line wider than 80 characters. (Argument Clinic will preserve indents inside the docstring.)

If the old docstring had a first line that looked like a function signature, throw that line away. (The docstring doesn’t need it anymore—when you use help() on your builtin in the future, the first line will be built automatically based on the function’s signature.)

Sample:

/*[clinic input]
Write a pickled representation of obj to the open file.
[clinic start generated code]*/

If your docstring doesn’t have a “summary” line, Argument Clinic will complain. So let’s make sure it has one. The “summary” line should be a paragraph consisting of a single 80-column line at the beginning of the docstring.

(Our example docstring consists solely of a summary line, so the sample code doesn’t have to change for this step.)

Above the docstring, enter the name of the function, followed by a blank line. This should be the Python name of the function, and should be the full dotted path to the function—it should start with the name of the module, include any sub-modules, and if the function is a method on a class it should include the class name too.

Sample:

/*[clinic input]
_pickle.Pickler.dump

Write a pickled representation of obj to the open file.
[clinic start generated code]*/

If this is the first time that module or class has been used with Argument Clinic in this C file, you must declare the module and/or class. Proper Argument Clinic hygiene prefers declaring these in a separate block somewhere near the top of the C file, in the same way that include files and statics go at the top. (In our sample code we’ll just show the two blocks next to each other.)

The name of the class and module should be the same as the one seen by Python. Check the name defined in the PyModuleDef or PyTypeObject as appropriate.

When you declare a class, you must also specify two aspects of its type in C: the type declaration you’d use for a pointer to an instance of this class, and a pointer to the PyTypeObject for this class.

Sample:

/*[clinic input]
module _pickle
class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
[clinic start generated code]*/

/*[clinic input]
_pickle.Pickler.dump

Write a pickled representation of obj to the open file.
[clinic start generated code]*/

Declare each of the parameters to the function. Each parameter should get its own line. All the parameter lines should be indented from the function name and the docstring.

The general form of these parameter lines is as follows:

name_of_parameter: converter

If the parameter has a default value, add that after the converter:

name_of_parameter: converter = default_value

Argument Clinic’s support for “default values” is quite sophisticated; please see the section below on default values for more information.

Add a blank line below the parameters.

What’s a “converter”? It establishes both the type of the variable used in C, and the method to convert the Python value into a C value at runtime. For now you’re going to use what’s called a “legacy converter”—a convenience syntax intended to make porting old code into Argument Clinic easier.

For each parameter, copy the “format unit” for that parameter from the PyArg_Parse() format argument and specify that as its converter, as a quoted string. (“format unit” is the formal name for the one-to-three character substring of the format parameter that tells the argument parsing function what the type of the variable is and how to convert it. For more on format units please see 解析参数并构建值变量.)

For multicharacter format units like z#, use the entire two-or-three character string.

Sample:

 /*[clinic input]
 module _pickle
 class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
 [clinic start generated code]*/

 /*[clinic input]
 _pickle.Pickler.dump

    obj: 'O'

Write a pickled representation of obj to the open file.
[clinic start generated code]*/

If your function has | in the format string, meaning some parameters have default values, you can ignore it. Argument Clinic infers which parameters are optional based on whether or not they have default values.

If your function has $ in the format string, meaning it takes keyword-only arguments, specify * on a line by itself before the first keyword-only argument, indented the same as the parameter lines.

(_pickle.Pickler.dump has neither, so our sample is unchanged.)

If the existing C function calls PyArg_ParseTuple() (as opposed to PyArg_ParseTupleAndKeywords()), then all its arguments are positional-only.

To mark all parameters as positional-only in Argument Clinic, add a / on a line by itself after the last parameter, indented the same as the parameter lines.

Currently this is all-or-nothing; either all parameters are positional-only, or none of them are. (In the future Argument Clinic may relax this restriction.)

Sample:

/*[clinic input]
module _pickle
class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
[clinic start generated code]*/

/*[clinic input]
_pickle.Pickler.dump

    obj: 'O'
    /

Write a pickled representation of obj to the open file.
[clinic start generated code]*/

It’s helpful to write a per-parameter docstring for each parameter. But per-parameter docstrings are optional; you can skip this step if you prefer.

Here’s how to add a per-parameter docstring. The first line of the per-parameter docstring must be indented further than the parameter definition. The left margin of this first line establishes the left margin for the whole per-parameter docstring; all the text you write will be outdented by this amount. You can write as much text as you like, across multiple lines if you wish.

Sample:

/*[clinic input]
module _pickle
class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
[clinic start generated code]*/

/*[clinic input]
_pickle.Pickler.dump

    obj: 'O'
        The object to be pickled.
    /

Write a pickled representation of obj to the open file.
[clinic start generated code]*/

Save and close the file, then run Tools/clinic/clinic.py on it. With luck everything worked—your block now has output, and a .c.h file has been generated! Reopen the file in your text editor to see:

/*[clinic input]
_pickle.Pickler.dump

    obj: 'O'
        The object to be pickled.
    /

Write a pickled representation of obj to the open file.
[clinic start generated code]*/

static PyObject *
_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/

Obviously, if Argument Clinic didn’t produce any output, it’s because it found an error in your input. Keep fixing your errors and retrying until Argument Clinic processes your file without complaint.

For readability, most of the glue code has been generated to a .c.h file. You’ll need to include that in your original .c file, typically right after the clinic module block:

#include "clinic/_pickle.c.h"

Double-check that the argument-parsing code Argument Clinic generated looks basically the same as the existing code.

First, ensure both places use the same argument-parsing function. The existing code must call either PyArg_ParseTuple() or PyArg_ParseTupleAndKeywords(); ensure that the code generated by Argument Clinic calls the exact same function.

Second, the format string passed in to PyArg_ParseTuple() or PyArg_ParseTupleAndKeywords() should be exactly the same as the hand-written one in the existing function, up to the colon or semi-colon.

(Argument Clinic always generates its format strings with a : followed by the name of the function. If the existing code’s format string ends with ;, to provide usage help, this change is harmless—don’t worry about it.)

Third, for parameters whose format units require two arguments (like a length variable, or an encoding string, or a pointer to a conversion function), ensure that the second argument is exactly the same between the two invocations.

Fourth, inside the output portion of the block you’ll find a preprocessor macro defining the appropriate static PyMethodDef structure for this builtin:

#define __PICKLE_PICKLER_DUMP_METHODDEF    \
{"dump", (PyCFunction)__pickle_Pickler_dump, METH_O, __pickle_Pickler_dump__doc__},

This static structure should be exactly the same as the existing static PyMethodDef structure for this builtin.

If any of these items differ in any way, adjust your Argument Clinic function specification and rerun Tools/clinic/clinic.py until they are the same.

Notice that the last line of its output is the declaration of your “impl” function. This is where the builtin’s implementation goes. Delete the existing prototype of the function you’re modifying, but leave the opening curly brace. Now delete its argument parsing code and the declarations of all the variables it dumps the arguments into. Notice how the Python arguments are now arguments to this impl function; if the implementation used different names for these variables, fix it.

Let’s reiterate, just because it’s kind of weird. Your code should now look like this:

static return_type
your_function_impl(...)
/*[clinic end generated code: checksum=...]*/
{
...

Argument Clinic generated the checksum line and the function prototype just above it. You should write the opening (and closing) curly braces for the function, and the implementation inside.

Sample:

/*[clinic input]
module _pickle
class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
[clinic start generated code]*/
/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/

/*[clinic input]
_pickle.Pickler.dump

    obj: 'O'
        The object to be pickled.
    /

Write a pickled representation of obj to the open file.
[clinic start generated code]*/

PyDoc_STRVAR(__pickle_Pickler_dump__doc__,
"Write a pickled representation of obj to the open file.\n"
"\n"
...
static PyObject *
_pickle_Pickler_dump_impl(PicklerObject *self, PyObject *obj)
/*[clinic end generated code: checksum=3bd30745bf206a48f8b576a1da3d90f55a0a4187]*/
{
    /* Check whether the Pickler was initialized correctly (issue3664).
       Developers often forget to call __init__() in their subclasses, which
       would trigger a segfault without this check. */
    if (self->write == NULL) {
        PyErr_Format(PicklingError,
                     "Pickler.__init__() was not called by %s.__init__()",
                     Py_TYPE(self)->tp_name);
        return NULL;
    }

    if (_Pickler_ClearBuffer(self) < 0)
        return NULL;

    ...

Remember the macro with the PyMethodDef structure for this function? Find the existing PyMethodDef structure for this function and replace it with a reference to the macro. (If the builtin is at module scope, this will probably be very near the end of the file; if the builtin is a class method, this will probably be below but relatively near to the implementation.)

Note that the body of the macro contains a trailing comma. So when you replace the existing static PyMethodDef structure with the macro, don’t add a comma to the end.

Sample:

static struct PyMethodDef Pickler_methods[] = {
    __PICKLE_PICKLER_DUMP_METHODDEF
    __PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
    {NULL, NULL}                /* sentinel */
};

Compile, then run the relevant portions of the regression-test suite. This change should not introduce any new compile-time warnings or errors, and there should be no externally-visible change to Python’s behavior.

Well, except for one difference: inspect.signature() run on your function should now provide a valid signature!

Congratulations, you’ve ported your first function to work with Argument Clinic!

Advanced Topics

Now that you’ve had some experience working with Argument Clinic, it’s time for some advanced topics.

Symbolic default values

The default value you provide for a parameter can’t be any arbitrary expression. Currently the following are explicitly supported:

Numeric constants (integer and float)

字符串常量

True, False, and None

Simple symbolic constants like sys.maxsize, which must start with the name of the module

In case you’re curious, this is implemented in from_builtin() in Lib/inspect.py.

(In the future, this may need to get even more elaborate, to allow full expressions like CONSTANT - 1.)

Renaming the C functions and variables generated by Argument Clinic

Argument Clinic automatically names the functions it generates for you. Occasionally this may cause a problem, if the generated name collides with the name of an existing C function. There’s an easy solution: override the names used for the C functions. Just add the keyword "as" to your function declaration line, followed by the function name you wish to use. Argument Clinic will use that function name for the base (generated) function, then add "_impl" to the end and use that for the name of the impl function.

For example, if we wanted to rename the C function names generated for pickle.Pickler.dump, it’d look like this:

/*[clinic input]
pickle.Pickler.dump as pickler_dumper

...

The base function would now be named pickler_dumper(), and the impl function would now be named pickler_dumper_impl().

Similarly, you may have a problem where you want to give a parameter a specific Python name, but that name may be inconvenient in C. Argument Clinic allows you to give a parameter different names in Python and in C, using the same "as" syntax:

/*[clinic input]
pickle.Pickler.dump

    obj: object
    file as file_obj: object
    protocol: object = NULL
    *
    fix_imports: bool = True

Here, the name used in Python (in the signature and the keywords array) would be file, but the C variable would be named file_obj.

You can use this to rename the self parameter too!

Converting functions using PyArg_UnpackTuple

To convert a function parsing its arguments with PyArg_UnpackTuple(), simply write out all the arguments, specifying each as an object. You may specify the type argument to cast the type as appropriate. All arguments should be marked positional-only (add a / on a line by itself after the last argument).

Currently the generated code will use PyArg_ParseTuple(), but this will change soon.

Optional Groups

Some legacy functions have a tricky approach to parsing their arguments: they count the number of positional arguments, then use a switch statement to call one of several different PyArg_ParseTuple() calls depending on how many positional arguments there are. (These functions cannot accept keyword-only arguments.) This approach was used to simulate optional arguments back before PyArg_ParseTupleAndKeywords() was created.

While functions using this approach can often be converted to use PyArg_ParseTupleAndKeywords(), optional arguments, and default values, it’s not always possible. Some of these legacy functions have behaviors PyArg_ParseTupleAndKeywords() doesn’t directly support. The most obvious example is the builtin function range(), which has an optional argument on the left side of its required argument! Another example is curses.window.addch(), which has a group of two arguments that must always be specified together. (The arguments are called x and y; if you call the function passing in x, you must also pass in y—and if you don’t pass in x you may not pass in y either.)

In any case, the goal of Argument Clinic is to support argument parsing for all existing CPython builtins without changing their semantics. Therefore Argument Clinic supports this alternate approach to parsing, using what are called optional groups. Optional groups are groups of arguments that must all be passed in together. They can be to the left or the right of the required arguments. They can only be used with positional-only parameters.

注解

Optional groups are only intended for use when converting functions that make multiple calls to PyArg_ParseTuple()! Functions that use any other approach for parsing arguments should almost never be converted to Argument Clinic using optional groups. Functions using optional groups currently cannot have accurate signatures in Python, because Python just doesn’t understand the concept. Please avoid using optional groups wherever possible.

To specify an optional group, add a [ on a line by itself before the parameters you wish to group together, and a ] on a line by itself after these parameters. As an example, here’s how curses.window.addch uses optional groups to make the first two parameters and the last parameter optional:

/*[clinic input]

curses.window.addch

    [
    x: int
      X-coordinate.
    y: int
      Y-coordinate.
    ]

    ch: object
      Character to add.

    [
    attr: long
      Attributes for the character.
    ]
    /

...

注释:

For every optional group, one additional parameter will be passed into the impl function representing the group. The parameter will be an int named group_{direction}_{number}, where {direction} is either right or left depending on whether the group is before or after the required parameters, and {number} is a monotonically increasing number (starting at 1) indicating how far away the group is from the required parameters. When the impl is called, this parameter will be set to zero if this group was unused, and set to non-zero if this group was used. (By used or unused, I mean whether or not the parameters received arguments in this invocation.)

If there are no required arguments, the optional groups will behave as if they’re to the right of the required arguments.

In the case of ambiguity, the argument parsing code favors parameters on the left (before the required parameters).

Optional groups can only contain positional-only parameters.

Optional groups are only intended for legacy code. Please do not use optional groups for new code.

Using real Argument Clinic converters, instead of “legacy converters”

To save time, and to minimize how much you need to learn to achieve your first port to Argument Clinic, the walkthrough above tells you to use “legacy converters”. “Legacy converters” are a convenience, designed explicitly to make porting existing code to Argument Clinic easier. And to be clear, their use is acceptable when porting code for Python 3.4.

However, in the long term we probably want all our blocks to use Argument Clinic’s real syntax for converters. Why? A couple reasons:

The proper converters are far easier to read and clearer in their intent.

There are some format units that are unsupported as “legacy converters”, because they require arguments, and the legacy converter syntax doesn’t support specifying arguments.

In the future we may have a new argument parsing library that isn’t restricted to what PyArg_ParseTuple() supports; this flexibility won’t be available to parameters using legacy converters.

Therefore, if you don’t mind a little extra effort, please use the normal converters instead of legacy converters.

In a nutshell, the syntax for Argument Clinic (non-legacy) converters looks like a Python function call. However, if there are no explicit arguments to the function (all functions take their default values), you may omit the parentheses. Thus bool and bool() are exactly the same converters.

All arguments to Argument Clinic converters are keyword-only. All Argument Clinic converters accept the following arguments:

c_default

The default value for this parameter when defined in C. Specifically, this will be the initializer for the variable declared in the “parse function”. See the section on default values for how to use this. Specified as a string.

annotation

The annotation value for this parameter. Not currently supported, because PEP 8 mandates that the Python library may not use annotations.

In addition, some converters accept additional arguments. Here is a list of these arguments, along with their meanings:

accept

A set of Python types (and possibly pseudo-types); this restricts the allowable Python argument to values of these types. (This is not a general-purpose facility; as a rule it only supports specific lists of types as shown in the legacy converter table.)

To accept None, add NoneType to this set.

bitwise

Only supported for unsigned integers. The native integer value of this Python argument will be written to the parameter without any range checking, even for negative values.

converter

Only supported by the object converter. Specifies the name of a C “converter function” to use to convert this object to a native type.

encoding

Only supported for strings. Specifies the encoding to use when converting this string from a Python str (Unicode) value into a C char * value.

subclass_of

Only supported for the object converter. Requires that the Python value be a subclass of a Python type, as expressed in C.

type

Only supported for the object and self converters. Specifies the C type that will be used to declare the variable. Default value is "PyObject *".

zeroes

Only supported for strings. If true, embedded NUL bytes ('\\0') are permitted inside the value. The length of the string will be passed in to the impl function, just after the string parameter, as a parameter named <parameter_name>_length.

Please note, not every possible combination of arguments will work. Usually these arguments are implemented by specific PyArg_ParseTuple format units, with specific behavior. For example, currently you cannot call unsigned_short without also specifying bitwise=True. Although it’s perfectly reasonable to think this would work, these semantics don’t map to any existing format unit. So Argument Clinic doesn’t support it. (Or, at least, not yet.)

下表显示了传统转换器映射到实参转换器的情况。左边是传统转换器，右边是要替换它的文本。

'B'

unsigned_char(bitwise=True)

'b'

unsigned_char

'c'

char

'C'

int(accept={str})

'd'

double

'D'

Py_complex

'es'

str(encoding='name_of_encoding')

'es#'

str(encoding='name_of_encoding', zeroes=True)

'et'

str(encoding='name_of_encoding', accept={bytes, bytearray, str})

'et#'

str(encoding='name_of_encoding', accept={bytes, bytearray, str}, zeroes=True)

'f'

float

'h'

short

'H'

unsigned_short(bitwise=True)

'i'

int

'I'

unsigned_int(bitwise=True)

'k'

unsigned_long(bitwise=True)

'K'

unsigned_long_long(bitwise=True)

'l'

long

'L'

long long

'n'

Py_ssize_t

'O'

object

'O!'

object(subclass_of='&PySomething_Type')

'O&'

object(converter='name_of_c_function')

'p'

bool

'S'

PyBytesObject

's'

str

's#'

str(zeroes=True)

's*'

Py_buffer(accept={buffer, str})

'U'

unicode

'u'

Py_UNICODE

'u#'

Py_UNICODE(zeroes=True)

'w*'

Py_buffer(accept={rwbuffer})

'Y'

PyByteArrayObject

'y'

str(accept={bytes})

'y#'

str(accept={robuffer}, zeroes=True)

'y*'

Py_buffer

'Z'

Py_UNICODE(accept={str, NoneType})

'Z#'

Py_UNICODE(accept={str, NoneType}, zeroes=True)

'z'

str(accept={str, NoneType})

'z#'

str(accept={str, NoneType}, zeroes=True)

'z*'

Py_buffer(accept={buffer, str, NoneType})

As an example, here’s our sample pickle.Pickler.dump using the proper converter:

/*[clinic input]
pickle.Pickler.dump

    obj: object
        The object to be pickled.
    /

Write a pickled representation of obj to the open file.
[clinic start generated code]*/

Argument Clinic will show you all the converters it has available. For each converter it’ll show you all the parameters it accepts, along with the default value for each parameter. Just run Tools/clinic/clinic.py --converters to see the full list.

Py_buffer

When using the Py_buffer converter (or the 's*', 'w*', '*y', or 'z*' legacy converters), you must not call PyBuffer_Release() on the provided buffer. Argument Clinic generates code that does it for you (in the parsing function).

Advanced converters

Remember those format units you skipped for your first time because they were advanced? Here’s how to handle those too.

The trick is, all those format units take arguments—either conversion functions, or types, or strings specifying an encoding. (But “legacy converters” don’t support arguments. That’s why we skipped them for your first function.) The argument you specified to the format unit is now an argument to the converter; this argument is either converter (for O&), subclass_of (for O!), or encoding (for all the format units that start with e).

When using subclass_of, you may also want to use the other custom argument for object(): type, which lets you set the type actually used for the parameter. For example, if you want to ensure that the object is a subclass of PyUnicode_Type, you probably want to use the converter object(type='PyUnicodeObject *', subclass_of='&PyUnicode_Type').

One possible problem with using Argument Clinic: it takes away some possible flexibility for the format units starting with e. When writing a PyArg_Parse call by hand, you could theoretically decide at runtime what encoding string to pass in to PyArg_ParseTuple(). But now this string must be hard-coded at Argument-Clinic-preprocessing-time. This limitation is deliberate; it made supporting this format unit much easier, and may allow for future optimizations. This restriction doesn’t seem unreasonable; CPython itself always passes in static hard-coded encoding strings for parameters whose format units start with e.

Parameter default values

Default values for parameters can be any of a number of values. At their simplest, they can be string, int, or float literals:

foo: str = "abc"
bar: int = 123
bat: float = 45.6

They can also use any of Python’s built-in constants:

yep:  bool = True
nope: bool = False
nada: object = None

There’s also special support for a default value of NULL, and for simple expressions, documented in the following sections.

The NULL default value

For string and object parameters, you can set them to None to indicate that there’s no default. However, that means the C variable will be initialized to Py_None. For convenience’s sakes, there’s a special value called NULL for just this reason: from Python’s perspective it behaves like a default value of None, but the C variable is initialized with NULL.

Expressions specified as default values

The default value for a parameter can be more than just a literal value. It can be an entire expression, using math operators and looking up attributes on objects. However, this support isn’t exactly simple, because of some non-obvious semantics.

Consider the following example:

foo: Py_ssize_t = sys.maxsize - 1

sys.maxsize can have different values on different platforms. Therefore Argument Clinic can’t simply evaluate that expression locally and hard-code it in C. So it stores the default in such a way that it will get evaluated at runtime, when the user asks for the function’s signature.

What namespace is available when the expression is evaluated? It’s evaluated in the context of the module the builtin came from. So, if your module has an attribute called “max_widgets”, you may simply use it:

foo: Py_ssize_t = max_widgets

If the symbol isn’t found in the current module, it fails over to looking in sys.modules. That’s how it can find sys.maxsize for example. (Since you don’t know in advance what modules the user will load into their interpreter, it’s best to restrict yourself to modules that are preloaded by Python itself.)

Evaluating default values only at runtime means Argument Clinic can’t compute the correct equivalent C default value. So you need to tell it explicitly. When you use an expression, you must also specify the equivalent expression in C, using the c_default parameter to the converter:

foo: Py_ssize_t(c_default="PY_SSIZE_T_MAX - 1") = sys.maxsize - 1

Another complication: Argument Clinic can’t know in advance whether or not the expression you supply is valid. It parses it to make sure it looks legal, but it can’t actually know. You must be very careful when using expressions to specify values that are guaranteed to be valid at runtime!

Finally, because expressions must be representable as static C values, there are many restrictions on legal expressions. Here’s a list of Python features you’re not permitted to use:

Function calls.

Inline if statements (3 if foo else 5).

Automatic sequence unpacking (*[1, 2, 3]).

List/set/dict comprehensions and generator expressions.

Tuple/list/set/dict literals.

Using a return converter

By default the impl function Argument Clinic generates for you returns PyObject *. But your C function often computes some C type, then converts it into the PyObject * at the last moment. Argument Clinic handles converting your inputs from Python types into native C types—why not have it convert your return value from a native C type into a Python type too?

That’s what a “return converter” does. It changes your impl function to return some C type, then adds code to the generated (non-impl) function to handle converting that value into the appropriate PyObject *.

The syntax for return converters is similar to that of parameter converters. You specify the return converter like it was a return annotation on the function itself. Return converters behave much the same as parameter converters; they take arguments, the arguments are all keyword-only, and if you’re not changing any of the default arguments you can omit the parentheses.

(If you use both "as" and a return converter for your function, the "as" should come before the return converter.)

There’s one additional complication when using return converters: how do you indicate an error has occurred? Normally, a function returns a valid (non-NULL) pointer for success, and NULL for failure. But if you use an integer return converter, all integers are valid. How can Argument Clinic detect an error? Its solution: each return converter implicitly looks for a special value that indicates an error. If you return that value, and an error has been set (PyErr_Occurred() returns a true value), then the generated code will propagate the error. Otherwise it will encode the value you return like normal.

Currently Argument Clinic supports only a few return converters:

bool
int
unsigned int
long
unsigned int
size_t
Py_ssize_t
float
double
DecodeFSDefault

None of these take parameters. For the first three, return -1 to indicate error. For DecodeFSDefault, the return type is char *; return a NULL pointer to indicate an error.

(There’s also an experimental NoneType converter, which lets you return Py_None on success or NULL on failure, without having to increment the reference count on Py_None. I’m not sure it adds enough clarity to be worth using.)

To see all the return converters Argument Clinic supports, along with their parameters (if any), just run Tools/clinic/clinic.py --converters for the full list.

Cloning existing functions

If you have a number of functions that look similar, you may be able to use Clinic’s “clone” feature. When you clone an existing function, you reuse:

its parameters, including

their names,

their converters, with all parameters,

their default values,

their per-parameter docstrings,

their kind (whether they’re positional only, positional or keyword, or keyword only), and

its return converter.

The only thing not copied from the original function is its docstring; the syntax allows you to specify a new docstring.

Here’s the syntax for cloning a function:

/*[clinic input]
module.class.new_function [as c_basename] = module.class.existing_function

Docstring for new_function goes here.
[clinic start generated code]*/

(The functions can be in different modules or classes. I wrote module.class in the sample just to illustrate that you must use the full path to both functions.)

Sorry, there’s no syntax for partially-cloning a function, or cloning a function then modifying it. Cloning is an all-or nothing proposition.

Also, the function you are cloning from must have been previously defined in the current file.

Calling Python code

The rest of the advanced topics require you to write Python code which lives inside your C file and modifies Argument Clinic’s runtime state. This is simple: you simply define a Python block.

A Python block uses different delimiter lines than an Argument Clinic function block. It looks like this:

/*[python input]
# python code goes here
[python start generated code]*/

All the code inside the Python block is executed at the time it’s parsed. All text written to stdout inside the block is redirected into the “output” after the block.

As an example, here’s a Python block that adds a static integer variable to the C code:

/*[python input]
print('static int __ignored_unused_variable__ = 0;')
[python start generated code]*/
static int __ignored_unused_variable__ = 0;
/*[python checksum:...]*/

Using a “self converter”

Argument Clinic automatically adds a “self” parameter for you using a default converter. It automatically sets the type of this parameter to the “pointer to an instance” you specified when you declared the type. However, you can override Argument Clinic’s converter and specify one yourself. Just add your own self parameter as the first parameter in a block, and ensure that its converter is an instance of self_converter or a subclass thereof.

What’s the point? This lets you override the type of self, or give it a different default name.

How do you specify the custom type you want to cast self to? If you only have one or two functions with the same type for self, you can directly use Argument Clinic’s existing self converter, passing in the type you want to use as the type parameter:

/*[clinic input]

_pickle.Pickler.dump

  self: self(type="PicklerObject *")
  obj: object
  /

Write a pickled representation of the given object to the open file.
[clinic start generated code]*/

On the other hand, if you have a lot of functions that will use the same type for self, it’s best to create your own converter, subclassing self_converter but overwriting the type member:

/*[python input]
class PicklerObject_converter(self_converter):
    type = "PicklerObject *"
[python start generated code]*/

/*[clinic input]

_pickle.Pickler.dump

  self: PicklerObject
  obj: object
  /

Write a pickled representation of the given object to the open file.
[clinic start generated code]*/

Writing a custom converter

As we hinted at in the previous section… you can write your own converters! A converter is simply a Python class that inherits from CConverter. The main purpose of a custom converter is if you have a parameter using the O& format unit—parsing this parameter means calling a PyArg_ParseTuple() “converter function”.

Your converter class should be named *something*_converter. If the name follows this convention, then your converter class will be automatically registered with Argument Clinic; its name will be the name of your class with the _converter suffix stripped off. (This is accomplished with a metaclass.)

You shouldn’t subclass CConverter.__init__. Instead, you should write a converter_init() function. converter_init() always accepts a self parameter; after that, all additional parameters must be keyword-only. Any arguments passed in to the converter in Argument Clinic will be passed along to your converter_init().

There are some additional members of CConverter you may wish to specify in your subclass. Here’s the current list:

type

The C type to use for this variable. type should be a Python string specifying the type, e.g. int. If this is a pointer type, the type string should end with ' *'.

default

The Python default value for this parameter, as a Python value. Or the magic value unspecified if there is no default.

py_default

default as it should appear in Python code, as a string. Or None if there is no default.

c_default

default as it should appear in C code, as a string. Or None if there is no default.

c_ignored_default

The default value used to initialize the C variable when there is no default, but not specifying a default may result in an “uninitialized variable” warning. This can easily happen when using option groups—although properly-written code will never actually use this value, the variable does get passed in to the impl, and the C compiler will complain about the “use” of the uninitialized value. This value should always be a non-empty string.

converter

The name of the C converter function, as a string.

impl_by_reference

A boolean value. If true, Argument Clinic will add a & in front of the name of the variable when passing it into the impl function.

parse_by_reference

A boolean value. If true, Argument Clinic will add a & in front of the name of the variable when passing it into PyArg_ParseTuple().

Here’s the simplest example of a custom converter, from Modules/zlibmodule.c:

/*[python input]

class ssize_t_converter(CConverter):
    type = 'Py_ssize_t'
    converter = 'ssize_t_converter'

[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=35521e4e733823c7]*/

This block adds a converter to Argument Clinic named ssize_t. Parameters declared as ssize_t will be declared as type Py_ssize_t, and will be parsed by the 'O&' format unit, which will call the ssize_t_converter converter function. ssize_t variables automatically support default values.

More sophisticated custom converters can insert custom C code to handle initialization and cleanup. You can see more examples of custom converters in the CPython source tree; grep the C files for the string CConverter.

Writing a custom return converter

Writing a custom return converter is much like writing a custom converter. Except it’s somewhat simpler, because return converters are themselves much simpler.

Return converters must subclass CReturnConverter. There are no examples yet of custom return converters, because they are not widely used yet. If you wish to write your own return converter, please read Tools/clinic/clinic.py, specifically the implementation of CReturnConverter and all its subclasses.

METH_O and METH_NOARGS

To convert a function using METH_O, make sure the function’s single argument is using the object converter, and mark the arguments as positional-only:

/*[clinic input]
meth_o_sample

     argument: object
     /
[clinic start generated code]*/

To convert a function using METH_NOARGS, just don’t specify any arguments.

You can still use a self converter, a return converter, and specify a type argument to the object converter for METH_O.

tp_new and tp_init functions

You can convert tp_new and tp_init functions. Just name them __new__ or __init__ as appropriate. Notes:

The function name generated for __new__ doesn’t end in __new__ like it would by default. It’s just the name of the class, converted into a valid C identifier.

No PyMethodDef #define is generated for these functions.

__init__ functions return int, not PyObject *.

Use the docstring as the class docstring.

Although __new__ and __init__ functions must always accept both the args and kwargs objects, when converting you may specify any signature for these functions that you like. (If your function doesn’t support keywords, the parsing function generated will throw an exception if it receives any.)

Changing and redirecting Clinic’s output

It can be inconvenient to have Clinic’s output interspersed with your conventional hand-edited C code. Luckily, Clinic is configurable: you can buffer up its output for printing later (or earlier!), or write its output to a separate file. You can also add a prefix or suffix to every line of Clinic’s generated output.

While changing Clinic’s output in this manner can be a boon to readability, it may result in Clinic code using types before they are defined, or your code attempting to use Clinic-generated code before it is defined. These problems can be easily solved by rearranging the declarations in your file, or moving where Clinic’s generated code goes. (This is why the default behavior of Clinic is to output everything into the current block; while many people consider this hampers readability, it will never require rearranging your code to fix definition-before-use problems.)

Let’s start with defining some terminology:

field

A field, in this context, is a subsection of Clinic’s output. For example, the #define for the PyMethodDef structure is a field, called methoddef_define. Clinic has seven different fields it can output per function definition:

docstring_prototype
docstring_definition
methoddef_define
impl_prototype
parser_prototype
parser_definition
impl_definition

All the names are of the form "<a>_<b>", where "<a>" is the semantic object represented (the parsing function, the impl function, the docstring, or the methoddef structure) and "<b>" represents what kind of statement the field is. Field names that end in "_prototype" represent forward declarations of that thing, without the actual body/data of the thing; field names that end in "_definition" represent the actual definition of the thing, with the body/data of the thing. ("methoddef" is special, it’s the only one that ends with "_define", representing that it’s a preprocessor #define.)

destination

A destination is a place Clinic can write output to. There are five built-in destinations:

block

The default destination: printed in the output section of the current Clinic block.

buffer

A text buffer where you can save text for later. Text sent here is appended to the end of any existing text. It’s an error to have any text left in the buffer when Clinic finishes processing a file.

file

A separate “clinic file” that will be created automatically by Clinic. The filename chosen for the file is {basename}.clinic{extension}, where basename and extension were assigned the output from os.path.splitext() run on the current file. (Example: the file destination for _pickle.c would be written to _pickle.clinic.c.)

Important: When using a file destination, you must check in the generated file!

two-pass

A buffer like buffer. However, a two-pass buffer can only be dumped once, and it prints out all text sent to it during all processing, even from Clinic blocks after the dumping point.

suppress

The text is suppressed—thrown away.

Clinic defines five new directives that let you reconfigure its output.

The first new directive is dump:

dump <destination>

This dumps the current contents of the named destination into the output of the current block, and empties it. This only works with buffer and two-pass destinations.

The second new directive is output. The most basic form of output is like this:

output <field> <destination>

This tells Clinic to output field to destination. output also supports a special meta-destination, called everything, which tells Clinic to output all fields to that destination.

output has a number of other functions:

output push
output pop
output preset <preset>

output push and output pop allow you to push and pop configurations on an internal configuration stack, so that you can temporarily modify the output configuration, then easily restore the previous configuration. Simply push before your change to save the current configuration, then pop when you wish to restore the previous configuration.

output preset sets Clinic’s output to one of several built-in preset configurations, as follows:

block

Clinic’s original starting configuration. Writes everything immediately after the input block.

Suppress the parser_prototype and docstring_prototype, write everything else to block.

file

Designed to write everything to the “clinic file” that it can. You then #include this file near the top of your file. You may need to rearrange your file to make this work, though usually this just means creating forward declarations for various typedef and PyTypeObject definitions.

Suppress the parser_prototype and docstring_prototype, write the impl_definition to block, and write everything else to file.

The default filename is "{dirname}/clinic/{basename}.h".

buffer

Save up most of the output from Clinic, to be written into your file near the end. For Python files implementing modules or builtin types, it’s recommended that you dump the buffer just above the static structures for your module or builtin type; these are normally very near the end. Using buffer may require even more editing than file, if your file has static PyMethodDef arrays defined in the middle of the file.

Suppress the parser_prototype, impl_prototype, and docstring_prototype, write the impl_definition to block, and write everything else to file.

two-pass

Similar to the buffer preset, but writes forward declarations to the two-pass buffer, and definitions to the buffer. This is similar to the buffer preset, but may require less editing than buffer. Dump the two-pass buffer near the top of your file, and dump the buffer near the end just like you would when using the buffer preset.

Suppresses the impl_prototype, write the impl_definition to block, write docstring_prototype, methoddef_define, and parser_prototype to two-pass, write everything else to buffer.

partial-buffer

Similar to the buffer preset, but writes more things to block, only writing the really big chunks of generated code to buffer. This avoids the definition-before-use problem of buffer completely, at the small cost of having slightly more stuff in the block’s output. Dump the buffer near the end, just like you would when using the buffer preset.

Suppresses the impl_prototype, write the docstring_definition and parser_definition to buffer, write everything else to block.

The third new directive is destination:

destination <name> <command> [...]

This performs an operation on the destination named name.

There are two defined subcommands: new and clear.

The new subcommand works like this:

destination <name> new <type>

This creates a new destination with name <name> and type <type>.

There are five destination types:

suppress

Throws the text away.

block

Writes the text to the current block. This is what Clinic originally did.

buffer

A simple text buffer, like the “buffer” builtin destination above.

file

A text file. The file destination takes an extra argument, a template to use for building the filename, like so:

destination <name> new <type> <file_template>

The template can use three strings internally that will be replaced by bits of the filename:

{path}

The full path to the file, including directory and full filename.

{dirname}

The name of the directory the file is in.

{basename}

Just the name of the file, not including the directory.

{basename_root}

Basename with the extension clipped off (everything up to but not including the last ‘.’).

{basename_extension}

The last ‘.’ and everything after it. If the basename does not contain a period, this will be the empty string.

If there are no periods in the filename, {basename} and {filename} are the same, and {extension} is empty. “{basename}{extension}” is always exactly the same as “{filename}”.”

two-pass

A two-pass buffer, like the “two-pass” builtin destination above.

The clear subcommand works like this:

destination <name> clear

It removes all the accumulated text up to this point in the destination. (I don’t know what you’d need this for, but I thought maybe it’d be useful while someone’s experimenting.)

The fourth new directive is set:

set line_prefix "string"
set line_suffix "string"

set lets you set two internal variables in Clinic. line_prefix is a string that will be prepended to every line of Clinic’s output; line_suffix is a string that will be appended to every line of Clinic’s output.

Both of these support two format strings:

{block comment start}

Turns into the string /*, the start-comment text sequence for C files.

{block comment end}

Turns into the string */, the end-comment text sequence for C files.

The final new directive is one you shouldn’t need to use directly, called preserve:

preserve

This tells Clinic that the current contents of the output should be kept, unmodified. This is used internally by Clinic when dumping output into file files; wrapping it in a Clinic block lets Clinic use its existing checksum functionality to ensure the file was not modified by hand before it gets overwritten.

The #ifdef trick

If you’re converting a function that isn’t available on all platforms, there’s a trick you can use to make life a little easier. The existing code probably looks like this:

#ifdef HAVE_FUNCTIONNAME
static module_functionname(...)
{
...
}
#endif /* HAVE_FUNCTIONNAME */

And then in the PyMethodDef structure at the bottom the existing code will have:

#ifdef HAVE_FUNCTIONNAME
{'functionname', ... },
#endif /* HAVE_FUNCTIONNAME */

In this scenario, you should enclose the body of your impl function inside the #ifdef, like so:

#ifdef HAVE_FUNCTIONNAME
/*[clinic input]
module.functionname
...
[clinic start generated code]*/
static module_functionname(...)
{
...
}
#endif /* HAVE_FUNCTIONNAME */

Then, remove those three lines from the PyMethodDef structure, replacing them with the macro Argument Clinic generated:

MODULE_FUNCTIONNAME_METHODDEF

(You can find the real name for this macro inside the generated code. Or you can calculate it yourself: it’s the name of your function as defined on the first line of your block, but with periods changed to underscores, uppercased, and "_METHODDEF" added to the end.)

Perhaps you’re wondering: what if HAVE_FUNCTIONNAME isn’t defined? The MODULE_FUNCTIONNAME_METHODDEF macro won’t be defined either!

Here’s where Argument Clinic gets very clever. It actually detects that the Argument Clinic block might be deactivated by the #ifdef. When that happens, it generates a little extra code that looks like this:

#ifndef MODULE_FUNCTIONNAME_METHODDEF
    #define MODULE_FUNCTIONNAME_METHODDEF
#endif /* !defined(MODULE_FUNCTIONNAME_METHODDEF) */

That means the macro always works. If the function is defined, this turns into the correct structure, including the trailing comma. If the function is undefined, this turns into nothing.

However, this causes one ticklish problem: where should Argument Clinic put this extra code when using the “block” output preset? It can’t go in the output block, because that could be deactivated by the #ifdef. (That’s the whole point!)

In this situation, Argument Clinic writes the extra code to the “buffer” destination. This may mean that you get a complaint from Argument Clinic:

Warning in file "Modules/posixmodule.c" on line 12357:
Destination buffer 'buffer' not empty at end of file, emptying.

When this happens, just open your file, find the dump buffer block that Argument Clinic added to your file (it’ll be at the very bottom), then move it above the PyMethodDef structure where that macro is used.

Using Argument Clinic in Python files

It’s actually possible to use Argument Clinic to preprocess Python files. There’s no point to using Argument Clinic blocks, of course, as the output wouldn’t make any sense to the Python interpreter. But using Argument Clinic to run Python blocks lets you use Python as a Python preprocessor!

Since Python comments are different from C comments, Argument Clinic blocks embedded in Python files look slightly different. They look like this:

#/*[python input]
#print("def foo(): pass")
#[python start generated code]*/
def foo(): pass
#/*[python checksum:...]*/

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

使用 DTrace 和 SystemTap 检测CPython

作者

David Malcolm

作者

Łukasz Langa

DTrace和SystemTap是监视工具，每个工具都提供了一种检查计算机系统上的进程正在执行的操作的方法。 它们都使用特定于域的语言，允许用户编写以下脚本：

进程监视的过滤器

从感兴趣的进程中收集数据

生成有关数据的报告

从Python 3.6开始，CPython可以使用嵌入式“标记”构建，也称为“探测器”，可以通过DTrace或SystemTap脚本观察，从而更容易监视系统上的CPython进程正在做什么。

CPython implementation detail: DTrace标记是CPython解释器的实现细节。 不保证CPython版本之间的探针兼容性。 更改CPython版本时，DTrace脚本可能会停止工作或无法正常工作而不会发出警告。

启用静态标记

macOS内置了对DTrace的支持。 在Linux上，为了使用SystemTap的嵌入式标记构建CPython，必须安装SystemTap开发工具。

在Linux机器上，这可以通过：

$ yum install systemtap-sdt-devel

或者：

$ sudo apt-get install systemtap-sdt-dev

然后必须将CPython配置为``–with-dtrace``：

checking for --with-dtrace... yes

在macOS上，您可以通过在后台运行Python进程列出可用的DTrace探测器，并列出Python程序提供的所有探测器：

$ python3.6 -q &
$ sudo dtrace -l -P python$!  # or: dtrace -l -m python3.6

   ID   PROVIDER            MODULE                          FUNCTION NAME
29564 python18035        python3.6          _PyEval_EvalFrameDefault function-entry
29565 python18035        python3.6             dtrace_function_entry function-entry
29566 python18035        python3.6          _PyEval_EvalFrameDefault function-return
29567 python18035        python3.6            dtrace_function_return function-return
29568 python18035        python3.6                           collect gc-done
29569 python18035        python3.6                           collect gc-start
29570 python18035        python3.6          _PyEval_EvalFrameDefault line
29571 python18035        python3.6                 maybe_dtrace_line line

在Linux上，您可以通过查看是否包含“.note.stapsdt”部分来验证构建的二进制文件中是否存在SystemTap静态标记。

$ readelf -S ./python | grep .note.stapsdt
[30] .note.stapsdt        NOTE         0000000000000000 00308d78

如果您已将Python构建为共享库（使用–enable-shared），则需要在共享库中查找。 例如：

$ readelf -S libpython3.3dm.so.1.0 | grep .note.stapsdt
[29] .note.stapsdt        NOTE         0000000000000000 00365b68

足够现代的readelf命令可以打印元数据：

$ readelf -n ./python

Displaying notes found at file offset 0x00000254 with length 0x00000020:
    Owner                 Data size          Description
    GNU                  0x00000010          NT_GNU_ABI_TAG (ABI version tag)
        OS: Linux, ABI: 2.6.32

Displaying notes found at file offset 0x00000274 with length 0x00000024:
    Owner                 Data size          Description
    GNU                  0x00000014          NT_GNU_BUILD_ID (unique build ID bitstring)
        Build ID: df924a2b08a7e89f6e11251d4602022977af2670

Displaying notes found at file offset 0x002d6c30 with length 0x00000144:
    Owner                 Data size          Description
    stapsdt              0x00000031          NT_STAPSDT (SystemTap probe descriptors)
        Provider: python
        Name: gc__start
        Location: 0x00000000004371c3, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bf6
        Arguments: -4@%ebx
    stapsdt              0x00000030          NT_STAPSDT (SystemTap probe descriptors)
        Provider: python
        Name: gc__done
        Location: 0x00000000004374e1, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bf8
        Arguments: -8@%rax
    stapsdt              0x00000045          NT_STAPSDT (SystemTap probe descriptors)
        Provider: python
        Name: function__entry
        Location: 0x000000000053db6c, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6be8
        Arguments: 8@%rbp 8@%r12 -4@%eax
    stapsdt              0x00000046          NT_STAPSDT (SystemTap probe descriptors)
        Provider: python
        Name: function__return
        Location: 0x000000000053dba8, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bea
        Arguments: 8@%rbp 8@%r12 -4@%eax

上述元数据包含SystemTap的信息，描述如何修补策略性放置的机器代码指令以启用SystemTap脚本使用的跟踪钩子。

静态DTrace探针

下面的 DTrace 脚本示例可以用来显示一个 Python 脚本的调用/返回层次结构，只在调用名为 “start” 的函数内进行跟踪。换句话说，导入时的函数调用不会被列出。

self int indent;

python$target:::function-entry
/copyinstr(arg1) == "start"/
{
        self->trace = 1;
}

python$target:::function-entry
/self->trace/
{
        printf("%d\t%*s:", timestamp, 15, probename);
        printf("%*s", self->indent, "");
        printf("%s:%s:%d\n", basename(copyinstr(arg0)), copyinstr(arg1), arg2);
        self->indent++;
}

python$target:::function-return
/self->trace/
{
        self->indent--;
        printf("%d\t%*s:", timestamp, 15, probename);
        printf("%*s", self->indent, "");
        printf("%s:%s:%d\n", basename(copyinstr(arg0)), copyinstr(arg1), arg2);
}

python$target:::function-return
/copyinstr(arg1) == "start"/
{
        self->trace = 0;
}

它可以这样调用:

$ sudo dtrace -q -s call_stack.d -c "python3.6 script.py"

输出结果会像这样:

156641360502280  function-entry:call_stack.py:start:23
156641360518804  function-entry: call_stack.py:function_1:1
156641360532797  function-entry:  call_stack.py:function_3:9
156641360546807 function-return:  call_stack.py:function_3:10
156641360563367 function-return: call_stack.py:function_1:2
156641360578365  function-entry: call_stack.py:function_2:5
156641360591757  function-entry:  call_stack.py:function_1:1
156641360605556  function-entry:   call_stack.py:function_3:9
156641360617482 function-return:   call_stack.py:function_3:10
156641360629814 function-return:  call_stack.py:function_1:2
156641360642285 function-return: call_stack.py:function_2:6
156641360656770  function-entry: call_stack.py:function_3:9
156641360669707 function-return: call_stack.py:function_3:10
156641360687853  function-entry: call_stack.py:function_4:13
156641360700719 function-return: call_stack.py:function_4:14
156641360719640  function-entry: call_stack.py:function_5:18
156641360732567 function-return: call_stack.py:function_5:21
156641360747370 function-return:call_stack.py:start:28

静态SystemTap标记

使用 SystemTap 集成的底层方法是直接使用静态标记。 这需要你显式地说明包含它们的二进制文件。

例如，这个SystemTap脚本可以用来显示Python脚本的调用/返回层次结构：

probe process("python").mark("function__entry") {
     filename = user_string($arg1);
     funcname = user_string($arg2);
     lineno = $arg3;

     printf("%s => %s in %s:%d\\n",
            thread_indent(1), funcname, filename, lineno);
}

probe process("python").mark("function__return") {
    filename = user_string($arg1);
    funcname = user_string($arg2);
    lineno = $arg3;

    printf("%s <= %s in %s:%d\\n",
           thread_indent(-1), funcname, filename, lineno);
}

它可以这样调用:

$ stap \
  show-call-hierarchy.stp \
  -c "./python test.py"

输出结果会像这样:

11408 python(8274):        => __contains__ in Lib/_abcoll.py:362
11414 python(8274):         => __getitem__ in Lib/os.py:425
11418 python(8274):          => encode in Lib/os.py:490
11424 python(8274):          <= encode in Lib/os.py:493
11428 python(8274):         <= __getitem__ in Lib/os.py:426
11433 python(8274):        <= __contains__ in Lib/_abcoll.py:366

其中的列是：

脚本开始后经过的微秒数

可执行文件的名字

进程的PID

其余部分则表示脚本执行时的调用/返回层次结构。

对于 –enable-shared 构建的CPython来说，这些标记是包含在libpython共享库中的，探针的点状路径需要反映这一点。比如上面例子中的这一行：

probe process("python").mark("function__entry") {

应改为：

probe process("python").library("libpython3.6dm.so.1.0").mark("function__entry") {

（假设是 CPython 3.6 的调试构建）

可用的静态标记

function__entry(str filename, str funcname, int lineno)

这个标记表示一个Python函数的执行已经开始。它只对纯 Python （字节码）函数触发。

文件名、函数名和行号作为定位参数提供给跟踪脚本，必须使用 $arg1, $arg2, $arg3 访问：

$arg1 : (const char *) 文件名，使用 user_string($arg1) 访问

$arg2 : (const char *) 函数名，使用 user_string($arg2) 访问

$arg3 : int 行号

function__return(str filename, str funcname, int lineno)

这个标记与 function__entry() 相反，表示Python函数的执行已经结束 (通过 return 或者异常)。 它只对纯Python (字节码) 函数触发。

参数和 function__entry() 相同

line(str filename, str funcname, int lineno)

这个标记表示一个 Python 行即将被执行。它相当于用 Python 分析器逐行追踪。它不会在C函数中触发。

参数和 function__entry() 相同

gc__start(int generation)

当Python解释器启动一个垃圾回收循环时被触发。 arg0 是要扫描的生成器，如 gc.collect()。

gc__done(long collected)

当Python解释器完成一个垃圾收集循环时被触发。arg0 是收集到的对象的数量。

SystemTap Tapsets

使用SystemTap集成的更高层次的方法是使用 “tapset” 。SystemTap 的等效库，它隐藏了静态标记的一些底层细节。

这里是一个基于 CPython 的非共享构建的 tapset 文件。

/*
   Provide a higher-level wrapping around the function__entry and
   function__return markers:
 \*/
probe python.function.entry = process("python").mark("function__entry")
{
    filename = user_string($arg1);
    funcname = user_string($arg2);
    lineno = $arg3;
    frameptr = $arg4
}
probe python.function.return = process("python").mark("function__return")
{
    filename = user_string($arg1);
    funcname = user_string($arg2);
    lineno = $arg3;
    frameptr = $arg4
}

如果这个文件安装在 SystemTap 的 tapset 目录下（例如``/usr/share/systemtap/tapset`` ），那么这些额外的探测点就会变得可用。

python.function.entry(str filename, str funcname, int lineno, frameptr)

This probe point indicates that execution of a Python function has begun. It is only triggered for pure-python (bytecode) functions.

python.function.return(str filename, str funcname, int lineno, frameptr)

This probe point is the converse of python.function.return(), and indicates that execution of a Python function has ended (either via return, or via an exception). It is only triggered for pure-python (bytecode) functions.

例子

这个SystemTap脚本使用上面的tapset来更清晰地实现上面给出的跟踪Python函数调用层次结构的例子，而不需要直接命名静态标记。

probe python.function.entry
{
  printf("%s => %s in %s:%d\n",
         thread_indent(1), funcname, filename, lineno);
}

probe python.function.return
{
  printf("%s <= %s in %s:%d\n",
         thread_indent(-1), funcname, filename, lineno);
}

下面的脚本使用上面的tapset提供了所有运行中的CPython代码的顶部视图，显示了整个系统中每一秒钟最频繁输入的前20个字节码帧。

global fn_calls;

probe python.function.entry
{
    fn_calls[pid(), filename, funcname, lineno] += 1;
}

probe timer.ms(1000) {
    printf("\033[2J\033[1;1H") /* clear screen \*/
    printf("%6s %80s %6s %30s %6s\n",
           "PID", "FILENAME", "LINE", "FUNCTION", "CALLS")
    foreach ([pid, filename, funcname, lineno] in fn_calls- limit 20) {
        printf("%6d %80s %6d %30s %6d\n",
            pid, filename, lineno, funcname,
            fn_calls[pid, filename, funcname, lineno]);
    }
    delete fn_calls;
}

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常用指引 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

Python 常见问题

Python常见问题

编程常见问题

设计和历史常见问题

代码库和插件 FAQ

扩展/嵌入常见问题

Python在Windows上的常见问题

图形用户界面（GUI）常见问题

“为什么我的电脑上安装了 Python ？”

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

Python常见问题

目录

Python常见问题

一般信息

什么是 Python？

什么是 Python 软件基金会？

使用 Python 是否存在版权限制？

创造 Python 的最初理由是什么？

Python 适合做什么？

Python 版本的编号形式是怎样的？

我应如何获取一份 Python 源代码的副本？

我应如何获取 Python 的文档？

我之前从未接触过编程。 哪里有 Python 的教程？

是否有专门针对 Python 的新闻组或邮件列表？

我应如何获取 Python 的公开测试版本？

我应如何为 Python 提交错误报告和补丁？

是否有任何公开发表的 Python 相关文章可以供我参考引用？

是否有任何 Python 相关的书籍？

www.python.org 具体位于世界上的哪个地点？

为何命名为 Python？

我必须喜欢 “Monty Python 的飞行马戏团” 吗？

现实世界中的 Python

Python 有多稳定？

有多少人在使用 Python？

有哪些重要的项目是用 Python 开发的？

在未来可以期待 Python 将有什么新进展？

提议对 Python 加入不兼容的更改是否合理？

Python 是一种对编程初学者友好的语言吗？

一般信息

什么是 Python？

Python 是一种解释性、交互式、面向对象的编程语言。 它包含了模块、异常、动态类型、非常高层级的动态数据类型以及类的概念。 Python结合了超强的功能和极清晰的语法。 它带有许多系统调用和库以及各种窗口系统的接口，并且可以用 C 或 C ++ 来进行扩展。 它还可用作需要可编程接口的应用程序的扩展语言。 最后，Python 还是可移植的：它可以在许多 Unix 变种、Mac 以及 Windows 2000 以上的操作系统中运行。

要了解更多详情，请先查看 Python 教程。 Python 新手指南 [https://wiki.python.org/moin/BeginnersGuide] 提供了学习 Python 的其他入门教程及资源的链接。

什么是 Python 软件基金会？

Python 软件基金会（Python Software Foundation，简称 PSF）是一个独立的非盈利组织，它拥有 Python 2.1 及以上各版本的版权。 PSF 的使命是推进与 Python 编程语言相关的开源技术，并推广 Python 的使用。 PSF 的主页是 https://www.python.org/psf/。

向 PSF 提供捐助在美国是免税的。 如果你在使用 Python 并且感觉它对你很有帮助，可以通过 PSF 捐助页 [https://www.python.org/psf/donations/] 进行捐助。

使用 Python 是否存在版权限制？

你可以任意使用源码，只要你保留版权信息并在你基于 Python 的产品文档中显示该版权信息。 如果你遵守此版权规则，就可以将 Python 用于商业领域，以源码或二进制码的形式（不论是否经过修改）销售 Python 的副本，或是以某种形式包含了 Python 的产品。 当然，我们仍然希望获知所有对 Python 的商业使用。

请参阅 PSF 许可页 [https://www.python.org/psf/license/] 以查看进一步的说明以及许可的完整文本内容的链接。

Python 的徽标是注册商标，在某些情况下需要获得允许方可使用。 请参阅 商标使用政策 [https://www.python.org/psf/trademarks/] 了解详情。

创造 Python 的最初理由是什么？

以下是有关最初缘起的一份 非常 简短的摘要，由 Guido van Rossum 本人撰写:

我在 CWI 的 ABC 部门时在实现解释型语言方面积累了丰富经验，通过与这个部门成员的协同工作，我学到了大量有关语言设计的知识。 这是许多 Python 特性的最初来源，包括使用缩进来组织语句以及包含非常高层级的数据结构（虽然在 Python 中具体的实现细节完全不同）。

我对 ABC 语言有过许多抱怨，但同时也很喜欢它的许多特性。 没有可能通过扩展 ABC 语言（或它的实现）来弥补我的不满 —— 实际上缺乏可扩展性就是它最大的问题之一。 我也有一些使用 Modula-2+ 的经验，并曾与 Modula-3 的设计者进行交流，还阅读了 Modula-3 的报告。 Modula-3 是 Python 中异常机制所用语法和语义，以及其他一些语言特性的最初来源。

我还曾在 CWI 的 Amoeba 分布式操作系统部门工作。 当时我们需要有一种比编写 C 程序或 Bash 脚本更好的方式来进行系统管理，因为 Amoeba 有它自己的系统调用接口，并且无法方便地通过 Bash 来访问。 我在 Amoeba 中处理错误的经验令我深刻地意识到异常处理在编程语言特性当中的重要地位。

我发现，某种具有 ABC 式的语法而又能访问 Amoeba 系统调用的脚本语言将可满足需求。 我意识到编写一种 Amoeba 专属的语言是愚蠢的，所以我决定编写一种具有全面可扩展性的语言。

在 1989 年的圣诞假期中，我手头的时间非常充裕，因此我决定开始尝试一下。 在接下来的一年里，虽然我仍然主要用我的业余时间来做这件事，但 Python 在 Amoeba 项目中的使用获得了很大的成功，来自同事的反馈让我得以增加了许多早期的改进。

到 1991 年 2 月，经过一年多的开发，我决定将其发布到 USENET。 之后的事情就都可以在 Misc/HISTORY 文件里面看了。

Python 适合做什么？

Python 是一种高层级的多用途编程语言，可用于解决许多不同门类的问题。

该语言附带一个庞大的标准库，涵盖了字符串处理（正则表达式，Unicode，比较文件间的差异等），因特网协议（HTTP，FTP，SMTP，XML-RPC，POP，IMAP，CGI 编程等），软件工程（单元测试，日志记录，性能分析，Python 代码解析等），以及操作系统接口（系统调用，文件系统，TCP/IP 套接字等）。 请查看 Python 标准库 的目录以了解所有可用的内容。 此外还可以获取到各种各样的第三方扩展。 请访问 Python 包索引 [https://pypi.org] 来查找你感兴趣的软件包。

Python 版本的编号形式是怎样的？

Python 版本的编号形式是 A.B.C 或 A.B。 A 称为大版本号 —— 它仅在对语言特性进行非常重大改变时才会递增。 B 称为小版本号，它会在语言特性发生较小改变时递增。 C 称为微版本号 —— 它会在每次发布问题修正时递增。 请参阅 PEP 6 [https://www.python.org/dev/peps/pep-0006] 了解有关问题修正发布版的详情。

发布版本并非都是问题修正版本。 在新的主要发布版本开发过程中，还会发布一系列的开发版，它们以 alpha (a), beta (b) 或 release candidate (rc) 来标示。 其中 alpha 版是早期发布的测试版，它的接口并未最终确定；在两个 alpha 发布版本间出现接口的改变并不意外。 而 beta 版更为稳定，它会保留现有的接口，但也可能增加新的模块，release candidate 版则会保持冻结状态不会再进行改变，除非有重大问题需要修正。

以上 alpha, beta 和 release candidate 版本会附加一个后缀。 用于 alpha 版本的后缀是带有一个小数字 N 的 “aN”，beta 版本的后缀是带有一个小数字 N 的“bN”，release candidate 版本的后缀是带有一个小数字 N 的“cN”。 换句话说，所有标记为 2.0aN 的版本都早于标记为 2.0bN 的版本，后者又都早于标记为 2.0cN 的版本，而 这些版本 全都早于 2.0。

你还可能看到带有“+”后缀的版本号，例如“2.2+”。 这表示未发布版本，直接基于 CPython 开发代码仓库构建。 在实际操作中，当一个小版本最终发布后，未发布版本号会递增到下一个小版本号，成为“a0”版本，例如“2.4a0”。

另请参阅 sys.version, sys.hexversion 以及 sys.version_info 的文档。

我应如何获取一份 Python 源代码的副本？

最新的 Python 发布版源代码总能从 python.org 获取，下载页链接为 https://www.python.org/downloads/。 最新的开发版源代码可以在 https://github.com/python/cpython/ 获取。

发布版源代码是一个以 gzip 压缩的 tar 文件，其中包含完整的 C 源代码、Sphinx 格式的文档、Python 库模块、示例程序以及一些有用的自由分发软件。 该源代码将可在大多数 UNIX 类平台上直接编译并运行。

请参阅 Python 开发者指南的初步上手部分 [https://devguide.python.org/setup/] 了解有关获取源代码并进行编译的更多信息。

我应如何获取 Python 的文档？

当前的 Python 稳定版本的标准文档可在 https://docs.python.org/3/ 查看。 也可在 https://docs.python.org/3/download.html 获取PDF、纯文本以及可下载的 HTML 版本。

文档以 reStructuredText 格式撰写，并使用 Sphinx 文档工具 [http://sphinx-doc.org/] 生成。 文档的 reStructuredText 源文件是 Python 源代码发布版的一部分。

我之前从未接触过编程。 哪里有 Python 的教程？

有许多可选择的教程和书籍。 标准文档中也包含有 Python 教程。

请参阅 新手指南 [https://wiki.python.org/moin/BeginnersGuide] 以获取针对 Python 编程初学者的信息，包括教程的清单。

是否有专门针对 Python 的新闻组或邮件列表？

有一个新闻组 comp.lang.python 和一个邮件列表 python-list [https://mail.python.org/mailman/listinfo/python-list]。 新闻组和邮件列表是彼此互通的 —— 如果你可以阅读新闻就不必再订阅邮件列表。 comp.lang.python 的流量很大，每天会收到数以百计的发帖，Usenet 使用者通常更擅长处理这样大的流量。

有关新软件发布和活动的公告可以在 comp.lang.python.announce 中找到，这是个严格管理的低流量列表，每天会收到五个左右的发帖。 可以在 Python 公告邮件列表 [https://mail.python.org/mailman/listinfo/python-announce-list] 页面进行订阅。

有关其他邮件列表和新闻组的更多信息可以在 https://www.python.org/community/lists/ 找到。

我应如何获取 Python 的公开测试版本？

可以从 https://www.python.org/downloads/ 下载 alpha 和 beta 发布版。 所有发布版都会在 comp.lang.python 和 comp.lang.python.announce 新闻组以及 Python 主页 https://www.python.org/ 上进行公告；并会推送到 RSS 新闻源。

你还可以通过 Git 访问 Python 的开发版。 请参阅 Python 开发者指南 [https://devguide.python.org/] 了解详情。

我应如何为 Python 提交错误报告和补丁？

要报告错误或提交补丁，请使用安装于 https://bugs.python.org/ 上的 Roundup。

你必须拥有一个 Roundup 账号才能报告错误；这样我们就可以在有后续问题时与你联系。 这也使得 Roundup 能在我们处理所报告的错误时向你发送更新消息。 如果你之前使用过 SourceForge 向 Python 报告错误，你可以通过 Roundup 的 密码重置操作 [https://bugs.python.org/user?@template=forgotten] 来获取你的 Roundup 密码。

有关 Python 开发流程的更多信息，请参阅 Python 开发者指南 [https://devguide.python.org/]。

是否有任何公开发表的 Python 相关文章可以供我参考引用？

可能作为参考文献的最好方式还是引用你喜欢的 Python 相关书籍。

第一篇有关 Python 的文章写于 1991 年，现在其内容已经相当过时了。

Guido van Rossum 与 Jelke de Boer, “使用 Python 编程语言交互式地测试远程服务器”, CWI 季刊, 第 4 卷, 第 4 期 (1991 年 12 月), 阿姆斯特丹, 第 283–303 页。

是否有任何 Python 相关的书籍？

是的，相关的书籍很多，还有更多即将发行。 请访问 python.org 的 wiki 页面 https://wiki.python.org/moin/PythonBooks 获取一份清单。

你也可以到各大在线书店搜索 “Python” 并过滤掉对 Monty Python 的引用；或者也可以搜索 “Python” 加 “language”。

www.python.org 具体位于世界上的哪个地点？

The Python project’s infrastructure is located all over the world. www.python.org [https://www.python.org] is graciously hosted by Rackspace [https://www.rackspace.com], with CDN caching provided by Fastly [https://www.fastly.com]. Upfront Systems [http://www.upfrontsystems.co.za/] hosts bugs.python.org [https://bugs.python.org]. Many other Python services like the Wiki [https://wiki.python.org] are hosted by Oregon State University Open Source Lab [https://osuosl.org].

为何命名为 Python？

在着手编写 Python 实现的时候，Guido van Rossum 同时还阅读了刚出版的 “Monty Python 的飞行马戏团” [https://en.wikipedia.org/wiki/Monty_Python] 剧本，这是一部自 1970 年代开始播出的 BBC 系列喜剧。 Van Rossum 觉得他需要选择一个简短、独特而又略显神秘的名字，于是他决定将这个新语言命名为 Python。

我必须喜欢 “Monty Python 的飞行马戏团” 吗？

不必，但这对学习会有帮助。 :)

现实世界中的 Python

Python 有多稳定？

非常稳定。 自 1991 年起大约每隔 6 到 18 个月就会推出新的稳定发布版，这种状态看来还将持续下去。 目前主要发布版本的间隔通常为 18 个月左右。

开发者也会推出旧版本的“问题修正”发布版，因此现有发布版的稳定性还会逐步提升。 问题修正发布版会以版本号第三部分的数字来标示（例如 3.5.3, 3.6.2），用于稳定性的管理；只有对已知问题的修正会包含在问题修正发布版中，同一系列的问题修正发布版中的接口确定将会始终保持一致。

The latest stable releases can always be found on the Python download page [https://www.python.org/downloads/]. There are two production-ready version of Python: 2.x and 3.x, but the recommended one at this times is Python 3.x. Although Python 2.x is still widely used, it will not be maintained after January 1, 2020 [https://www.python.org/dev/peps/pep-0373/]. Python 2.x was known for having more third-party libraries available, however, by the time of this writing, most of the widely used libraries support Python 3.x, and some are even dropping the Python 2.x support.

有多少人在使用 Python？

使用者的数量肯定非常庞大，不过想要进行精确统计则是相当困难的。

Python 可以免费下载，因此并不存在销量数据，此外它也可以从许多不同网站获取，并且包含于许多 Linux 发行版之中，因此下载量统计同样无法完全说明问题。

comp.lang.python 新闻组非常活跃，但不是所有 Python 用户都会在新闻组发帖，许多人甚至不会阅读新闻组。

有哪些重要的项目是用 Python 开发的？

请访问 https://www.python.org/about/success 查看使用了 Python 的项目列表。 阅览 历次 Python 会议 [https://www.python.org/community/workshops/] 的日程纪要可以看到许多不同公司和组织所做的贡献。

高水准的 Python 项目包括 Mailman 邮件列表管理器 [http://www.list.org] 和 Zope 应用服务器 [http://www.zope.org]。 多个 Linux 发行版，其中最著名的有 Red Hat [https://www.redhat.com] 均已使用 Python 来编写部分或全部的安装程序和系统管理软件。 在内部使用 Python 的大公司包括了 Google, Yahoo 以及 Lucasfilm 等。

在未来可以期待 Python 将有什么新进展？

请访问 https://www.python.org/dev/peps/ 查看 Python 增强提议（PEP）。 PEP 是为 Python 加入某种新特性的提议进行描述的设计文档，其中会提供简明的技术规格说明与基本原理。 可以查找标题为“Python X.Y Release Schedule”的 PEP，其中 X.Y 是某个尚未公开发布的版本。

新版本的开发会在 python-dev 邮件列表 [https://mail.python.org/mailman/listinfo/python-dev/] 中进行讨论。

提议对 Python 加入不兼容的更改是否合理？

通常来说是不合理的。 世界上已存在的 Python 代码数以亿计，因此，任何对该语言的更改即便仅会使得现有程序中极少的一部分失效也是难以令人接受的。 就算你可以提供一个转换程序，也仍然存在需要更新全部文档的问题；另外还有大量已出版的 Python 书籍，我们不希望让它们在一瞬间全部变成废纸。

如果必须更改某个特性，则应该提供渐进式的升级路径。 PEP 5 [https://www.python.org/dev/peps/pep-0005] 描述了引入向后不兼容的更改所需遵循的流程，以尽可能减少对用户的干扰。

Python 是一种对编程初学者友好的语言吗？

有的。

从过程式、静态类型的编程语言例如 Pascal, C 或者 C++ 以及 Java 的某一子集开始引导学生入门仍然是常见的做法。 但以 Python 作为第一种编程语言进行学习对学生可能更有利。Python 具有非常简单和一致的语法和庞大的标准库，而且最重要的是，在编程入门教学中使用 Python 可以让学生专注于更重要的编程技能，例如问题分解与数据类型设计。 使用 Python，可以快速向学生介绍基本概念例如循环与过程等。 他们甚至有可能在第一次课里就开始接触用户自定义对象。

对于之前从未接触过编程的学生来说，使用静态类型语言会感觉不够自然。 这会给学生带来必须掌握的额外复杂性，并减慢教学的进度。 学生需要尝试像计算机一样思考，分解问题，设计一致的接口并封装数据。 虽然从长远来看，学习和使用一种静态类型语言是很重要的，但这并不是最适宜在学生的第一次编程课上就进行探讨的主题。

还有许多其他方面的特点使得 Python 成为很好的入门语言。 像 Java 一样，Python 拥有一个庞大的标准库，因此可以在课程非常早期的阶段就给学生布置一些 实用 的编程项目。 编程作业不必仅限于标准四则运算和账目检查程序。 通过使用标准库，学生可以在学习编程基础知识的同时开发真正的应用，从而获得更大的满足感。 使用标准库还能使学生了解代码重用的概念。 而像 PyGame 这样的第三方模块同样有助于扩大学生的接触领域。

Python 的解释器使学生能够在编程时测试语言特性。 他们可以在一个窗口中输入程序源代码的同时开启一个解释器运行窗口。 如果他们不记得列表有哪些方法，他们这以这样做:

>>> L = []
>>> dir(L)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
'__dir__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__',
'__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__',
'__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__',
'__sizeof__', '__str__', '__subclasshook__', 'append', 'clear',
'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove',
'reverse', 'sort']
>>> [d for d in dir(L) if '__' not in d]
['append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

>>> help(L.append)
Help on built-in function append:

append(...)
    L.append(object) -> None -- append object to end

>>> L.append(1)
>>> L
[1]

通过使用解释器，学生编写程序时参考文档总是能伴随在他们身边。

Python 还拥有很好的 IDE。 IDLE 是一个跨平台的 Python IDE，它基于 Tkinter 库，使用 Python 语言编写。 PythonWin 是一个 Windows 专属的 IDE。 Emacs 用户将高兴地了解到 Emacs 具有非常好的 Python 模式。 所有这些编程环境都提供语法高亮，自动缩进以及在编写代码时使用交互式解释器等功能。 请访问 Python wiki [https://wiki.python.org/moin/PythonEditors] 查看 Python 编程环境的完整列表。

如果你想要讨论 Python 在教育中的使用，你可能会有兴趣加入 edu-sig 邮件列表 [https://www.python.org/community/sigs/current/edu-sig]。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

编程常见问题

目录

编程常见问题

一般问题

Python 有没有提供断点与单步调试等功能的，源码层次的调试器？

有没有工具来帮助找寻漏洞或进行静态分析？

我如何能够通过一个 Python 脚本创建一个独立运行的二进制文件？

是否有 Python 程序规范代码标准或风格指南？

核心语言

当变量有值时，为什么会出现UnboundLocalError？

Python中的局部变量和全局变量有哪些规则？

为什么在具有不同值的循环中定义的lambdas都返回相同的结果？

如何跨模块共享全局变量？

导入模块的“最佳实践”是什么？

为什么对象之间会共享默认值？

如何将可选参数或关键字参数从一个函数传递到另一个函数？

形参和实参之间有什么区别？

为什么更改列表 ‘y’ 也会更改列表 ‘x’？

如何编写带输出参数的函数（通过引用调用）？

如何在Python中创建高阶函数？

如何在Python中复制对象？

如何找到对象的方法或属性？

我的代码如何才能发现对象的名称？

逗号运算符的优先级是什么？

是否有与 C 的 “?:” 三目运算符等价的东西？

是否可以用Python编写混淆的单行程序?

数字和字符串

如何指定十六进制和八进制整数？

为什么-22 // 10返回-3？

如何将字符串转换为数字？

如何将数字转换为字符串？

如何修改字符串？

如何使用字符串调用函数/方法？

是否有与Perl 的chomp() 等效的方法，用于从字符串中删除尾随换行符？

是否有 scanf() 或 sscanf() 的对应物？

‘UnicodeDecodeError’ 或 ‘UnicodeEncodeError’ 错误是什么意思？

性能

我的程序太慢了。该如何加快速度？

将多个字符串连接在一起的最有效方法是什么？

序列（元组/列表）

如何在元组和列表之间进行转换？

什么是负数序号？

如何以相反的顺序迭代序列？

如何从列表中删除重复项？

如何在Python中创建数组？

如何创建多维列表？

如何将方法应用于一系列对象？

为什么 a_tuple[i] += [‘item’] 会在执行加法时引发异常？

Dictionaries

How can I get a dictionary to store and display its keys in a consistent order?

我想做一个复杂的排序：你能用Python做一个Schwartzian变换吗？

如何按其他列表中的值对一个列表进行排序？

对象

什么是类？

什么是方法？

什么是 self ？

如何检查对象是否为给定类或其子类的一个实例？

什么是委托？

如何从覆盖基类的派生类调用基类中定义的方法?

如何组织代码以便更改基类？

如何创建静态类数据和静态类方法？

如何在Python中重载构造函数（或方法）？

我尝试使用 __spam ，但是得到一个关于 _SomeClassName__spam 的错误信息。

类定义了 __del__ 方法，但是删除对象时没有调用它。

如何获取给定类的所有实例的列表？

为什么 id() 的结果看起来不是唯一的？

模块

如何创建 .pyc 文件？

如何找到当前模块名称？

如何让模块相互导入？

__import__(‘x.y.z’) 返回 <module ‘x’>; 如何获取z?

当我编辑了导入过的模块并重新导入它时，这些变化没有显示出来。为什么会这样？

一般问题

Python 有没有提供断点与单步调试等功能的，源码层次的调试器？

有的。

pdb 模块是一个简单但是够用的控制台模式 Python 调试器。 它是标准 Python 库的一部分，并且 已收录于库参考手册。 你也可以通过使用 pdb 代码作为样例来编写你自己的调试器。

作为标准 Python 发行版附带组件的 IDLE 交互式环境（通常位于 Tools/scripts/idle）中包含一个图形化的调试器。

PythonWin 是一个包含有基于 pdb 的 GUI 调试器的 Python IDE。 Pythonwin 调试器会为断点加上颜色，并具有许多很棒的特性，例如也可以非 Pythonwin 程序。 Pythonwin 是 Python for Windows Extensions [https://sourceforge.net/projects/pywin32/] 项目的一部分，也是 ActivePython 发行版的一部分（参见 https://www.activestate.com/activepython）。

Boa Constructor [http://boa-constructor.sourceforge.net/] 是一个使用wxWidgets的IDE和GUI构建器。它提供可视化框架创建和操作，对象检查器，源对象浏览器上的许多视图，继承层次结构，doc字符串生成的html文档，高级调试器，集成帮助和Zope支持。

Eric [http://eric-ide.python-projects.org/] 是一个基于PyQt和Scintilla编辑组件构建的IDE。

Pydb是标准Python调试器pdb的一个版本，经过修改后可与DDD（数据显示调试器）一起使用，DDD是一种流行的图形化调试器前端。 Pydb可以在 http://bashdb.sourceforge.net/pydb/ 找到，DDD可以在 https://www.gnu.org/software/ddd 找到。

有许多商业Python IDE包括图形调试器。他们包括：

Wing IDE (https://wingware.com/)

Komodo IDE (https://komodoide.com/)

PyCharm (https://www.jetbrains.com/pycharm/)

有没有工具来帮助找寻漏洞或进行静态分析？

有的。

PyChecker 是一个寻找Python代码漏洞以及对代码复杂性和风格给出警告的工具。你可以从这里获得PyChecker: http://pychecker.sourceforge.net/ 。

Pylint [https://www.pylint.org/] 是另一个检查模块是否满足编码标准的工具，也可以编写插件来添加自定义功能。除了PyChecker 执行的错误检查之外， Pylint 还提供了一些额外的功能，例如检查行长度，变量名称是否根据您的编码标准格式良好，声明的接口是否完全实现等等。 https://docs.pylint.org/ 提供了Pylint功能的完整列表。

我如何能够通过一个 Python 脚本创建一个独立运行的二进制文件？

如果你想要的只是一个独立的程序，用户可以下载和运行而不必先安装Python发行版，你就不需要将Python编译成C代码。有许多工具可以确定程序所需的模块集，并将这些模块与Python二进制文件绑定在一起以生成单个可执行文件。

一种是使用冻结工具，它包含在Python源代码树 Tools/freeze 中。它将Python字节代码转换为C数组；一个C编译器，你可以将所有模块嵌入到一个新程序中，然后将其与标准Python模块链接。

它的工作原理是递归扫描源代码以获取import语句（两种形式），并在标准Python路径和源目录（用于内置模块）中查找模块。 然后，它将用Python编写的模块的字节码转换为C代码（可以使用编组模块转换为代码对象的数组初始化器），并创建一个定制的配置文件，该文件仅包含程序中实际使用的内置模块。 然后，它编译生成的C代码并将其与Python解释器的其余部分链接，以形成一个独立的二进制文件，其行为与你的脚本完全相同。

显然， freeze 需要一个C编译器。有几个其他实用工具不需要。 一个是Thomas Heller的py2exe（仅限Windows）

http://www.py2exe.org/

Another tool is Anthony Tuininga’s cx_Freeze [http://cx-freeze.sourceforge.net/].

是否有 Python 程序规范代码标准或风格指南？

有的。 请参阅标准库模块所要求的代码风格描述文档 PEP 8 [https://www.python.org/dev/peps/pep-0008] 。

核心语言

当变量有值时，为什么会出现UnboundLocalError？

通过在函数体中的某处添加赋值语句，导致以前正常工作的代码被修改而得到 UnboundLocalError 会令人感到意外。

以下代码：

>>> x = 10
>>> def bar():
...     print(x)
>>> bar()
10

正常工作，但是以下代码

>>> x = 10
>>> def foo():
...     print(x)
...     x += 1

会得到一个 UnboundLocalError ：

>>> foo()
Traceback (most recent call last):
  ...
UnboundLocalError: local variable 'x' referenced before assignment

这是因为当你对作用域中的变量进行赋值时，该变量将成为该作用域的局部变量，并在外部作用域中隐藏任何类似命名的变量。由于foo中的最后一个语句为 x 分配了一个新值，编译器会将其识别为局部变量。因此，当先前的 print(x) 尝试打印未初始化的局部变量时会导致错误。

在上面的示例中，你可以通过将其声明为全局来访问外部作用域变量：

>>> x = 10
>>> def foobar():
...     global x
...     print(x)
...     x += 1
>>> foobar()
10

这个显式声明是必需的，以便提醒你（与类和实例变量的表面类似情况不同），你实际上是在外部作用域中修改变量的值

>>> print(x)
11

你可以使用 nonlocal 关键字在嵌套作用域中执行类似的操作：

>>> def foo():
...    x = 10
...    def bar():
...        nonlocal x
...        print(x)
...        x += 1
...    bar()
...    print(x)
>>> foo()
10
11

Python中的局部变量和全局变量有哪些规则？

在Python中，仅在函数内引用的变量是隐式全局变量。如果在函数体内的任何位置为变量赋值，则除非明确声明为全局，否则将其视为局部值。

虽然起初有点令人惊讶，但片刻考虑就可以解释。一方面，要求 global 表示已分配的变量可以防止意外的副作用。另一方面，如果所有全局引用都需要 global ，那么你一直都在使用 global 。你必须将对内置函数或导入模块的组件的每个引用声明为全局。这种杂乱会破坏 global 声明用于识别副作用的有用性。

为什么在具有不同值的循环中定义的lambdas都返回相同的结果？

假设你使用for循环来定义几个不同的 lambda （甚至是普通函数），例如：:

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda: x**2)

这给你一个包含5个lambdas的列表，它们计算 x**2 。你可能会期望，当它们被调用时，它们将分别返回 0 、 1 、 4 、 9 和 16 。但是，当你真正尝试时，你会看到它们都返回 16 。:

>>> squares[2]()
16
>>> squares[4]()
16

发生这种情况是因为 x 不是lambdas的内部变量，而是在外部作用域中定义，并且在调用lambda时访问它 - 而不是在定义它时。 在循环结束时， x 的值是 4 ，所以所有的函数现在返回 4**2 ，即 16 。你还可以通过更改 x 的值来验证这一点，并查看lambdas的结果如何变化:

>>> x = 8
>>> squares[2]()
64

为了避免这种情况，你需要将值保存在lambdas的局部变量中，这样它们就不依赖于全局``x`` 的值

>>> squares = []
>>> for x in range(5):
...     squares.append(lambda n=x: n**2)

这里， n=x 在lambda本地创建一个新的变量 n ，并在定义lambda时计算，使它具有与 x 在循环中该点相同的值。这意味着 n 的值在第一个lambda中为 0 ，在第二个lambda中为 1 ，在第三个中为 2 ，依此类推。因此每个lambda现在将返回正确的结果:

>>> squares[2]()
4
>>> squares[4]()
16

请注意，这种行为并不是lambda所特有的，但也适用于常规函数。

如何跨模块共享全局变量？

在单个程序中跨模块共享信息的规范方法是创建一个特殊模块（通常称为config或cfg）。只需在应用程序的所有模块中导入配置模块；然后该模块可用作全局名称。因为每个模块只有一个实例，所以对模块对象所做的任何更改都会在任何地方反映出来。 例如：

config.py:

x = 0   # Default value of the 'x' configuration setting

mod.py:

import config
config.x = 1

main.py:

import config
import mod
print(config.x)

请注意，出于同样的原因，使用模块也是实现Singleton设计模式的基础。

导入模块的“最佳实践”是什么？

通常，不要使用 from modulename import * 。这样做会使导入器的命名空间变得混乱，并且使得连接器更难以检测未定义的名称。

在文件的顶部导入模块。这样做可以清楚地了解代码所需的其他模块，并避免了模块名称是否在范围内的问题。每行导入一个模块可以轻松添加和删除导入的模块，但每行导入多个模块会占用更少的屏幕空间。

如果按以下顺序导入模块，这是一种很好的做法：

标准库模块 – 例如： sys, os, getopt, re

第三方库模块（安装在Python的site-packages目录中的任何内容） – 例如mx.DateTime，ZODB，PIL.Image等

本地开发的模块

有时需要将模块导入语句移动到函数或类里面，以避免循环导入问题。Gordon McMillan 说：

当两个模块都使用 “import <module>” 的导入形式时，循环导入就可以了。但是当第 2 个模块想从第 1 个模块中获取一个名称 (“from module import name”) 并且导入位于顶层时，就会出错。 这是因为第 1 个模块中的名称还不可用，因为第 1 个模块正在忙着导入第 2 个模块。

在这种情况下，如果第二个模块仅用于一个函数，则可以轻松地将模块导入语句移动到该函数中。调用导入时，第一个模块将完成初始化，第二个模块可以进行导入。

如果某些模块是特定于平台的，则可能还需要将模块导入语句移出顶级代码。在这种情况下，甚至可能无法导入文件顶部的所有模块。在这种情况下，在相应的特定于平台的代码中导入正确的模块是一个很好的选择。

只有当需要解决诸如避免循环导入或试图减少模块初始化时间的问题时，才可以将导入移动到本地范围，例如在函数定义中。如果根据程序的执行方式，许多导入是不必要的，这种技术尤其有用。如果仅在某个函数中使用模块，您还可能希望将导入移到该函数中。请注意，第一次加载模块可能会因为模块的一次初始化而代价高昂，但多次加载模块实际上是免费的，只需进行几次字典查找。即使模块名称超出了作用域，模块也可能在 sys.modules 中可用。

为什么对象之间会共享默认值？

这种类型的缺陷通常会惹恼新手程序员。考虑这个函数

def foo(mydict={}):  # Danger: shared reference to one dict for all calls
    ... compute something ...
    mydict[key] = value
    return mydict

第一次调用此函数时，mydict 包含一项。第二次，mydict 包含两项，因为当 foo() 开始执行时， mydict 中已经有一项了。

函数调用经常被期望为默认值创建新的对象。 但实际情况并非如此。 默认值会在函数定义时一次性地创建。 如果对象发生改变，就如本示例中的字典那样，则对函数的后续调用将会引用这个被改变的对象。

按照定义，不可变对象例如数字、字符串、元组和 None 因为不可变所以是安全的。 对可变对象例如字典、列表和类实例的改变则可能造成迷惑。

由于这一特性，在编程中应遵循的一项好习惯是不使用可变对象作为默认值。 而应使用 None 作为默认值和函数中的值，检查值为 None 的形参并创建相应的列表、字典或其他可变对象。 例如，不要这样写:

def foo(mydict={}):
    ...

而要这样写:

def foo(mydict=None):
    if mydict is None:
        mydict = {}  # create a new dict for local namespace

这一特性有时会很有用处。 当你有一个需要进行大量耗时计算的函数时，一个常见技巧是将每次调用函数的参数和结果值缓存起来，并在同样的值被再次请求时返回缓存的值。 这称为“记忆”，具体实现方式可以是这样的:

# Callers can only provide two parameters and optionally pass _cache by keyword
def expensive(arg1, arg2, *, _cache={}):
    if (arg1, arg2) in _cache:
        return _cache[(arg1, arg2)]

    # Calculate the value
    result = ... expensive computation ...
    _cache[(arg1, arg2)] = result           # Store result in the cache
    return result

你也可以使用包含一个字典的全局变量而不使用参数默认值；这完全取决于个人偏好。

如何将可选参数或关键字参数从一个函数传递到另一个函数？

使用函数参数列表中的 * 和 ** 说明符收集参数;这会将位置参数作为元组，将关键字参数作为字典。然后，您可以使用 * 和 ** 调用另一个函数时传递这些参数：

def f(x, *args, **kwargs):
    ...
    kwargs['width'] = '14.3c'
    ...
    g(x, *args, **kwargs)

形参和实参之间有什么区别？

形参 是指出现在函数定义中的名称，而 实参 则是在调用函数时实际传入的值。 形参定义了一个函数能接受何种类型的实参。 例如，对于以下函数定义:

def func(foo, bar=None, **kwargs):
    pass

foo, bar 和 kwargs 是 func 的形参。 但是，在调用 func 时，例如：

func(42, bar=314, extra=somevar)

实际的值 42, 314 和 somevar 则是实参。

为什么更改列表 ‘y’ 也会更改列表 ‘x’？

如果你编写的代码就像下面一样：

>>> x = []
>>> y = x
>>> y.append(10)
>>> y
[10]
>>> x
[10]

你可能想知道为什么追加一个元素也改变了x。

产生这种结果有两个因素：

变量只是指向具体对象的名称。 执行 y = x 并不会为列表创建一个副本 —— 它只是创建了一个新变量 y 指向 x 所指向的同一对象。 这意味着只存在一个对象（列表），x 和 y 都是对它的引用。

列表属于 mutable 对象，这意味着你可以改变它的内容。

在调用 append() 之后，这个可变对象的内容由 [] 变为 [10]。 由于两个变量都指向同一对象，因此使用任何一个名称所访问到的都是修改后的值 [10]。

如果我们改为将不可变对象赋值给 x:

>>> x = 5  # ints are immutable
>>> y = x
>>> x = x + 1  # 5 can't be mutated, we are creating a new object here
>>> x
6
>>> y
5

我们可以看到在此情况下 x 和 y 就不再相等了。 这是因为整数是 immutable 对象，当我们执行 x = x + 1 时我们并不是改变了 5 这个对象的值；而是创建了一个新的对象 (整数 6) 并将其赋值给 x (也就是改变了 x 所指向的对象)。 在赋值之后我们就有了两个对象 (整数 6 和 5) 以及分别指向它们的两个变量 (x 现在指向 6 而 y 仍然指向 5)。

某些操作 (例如 y.append(10) 和 y.sort()) 是改变原对象，而看上去相似的另一些操作 (例如 y = y + [10] 和 sorted(y)) 则是创建新对象。 通常在 Python 中 (以及在标准库的所有代码中) 会改变原对象的方法将返回 None 以帮助避免混淆这两种不同类型的操作。 因此如果你错误地使用了 y.sort() 并期望它将返回一个经过排序的 y 的副本，你得到的结果将会是 None，这将导致你的程序产生一个容易诊断的错误。

但是，还存在一类操作，不同的类型执行相同的操作会有不同的行为：那就是增强赋值运算符。 例如，+= 会原地改变列表，但不会改变元组或整数 (a_list += [1, 2, 3] 与 a_list.extend([1, 2, 3]) 一样都会改变 a_list，而 some_tuple += (1, 2, 3) 和 some_int += 1 则会创建新的对象)。

换而言之：

如果我们有一个可变对象 (list, dict, set 等等)，我们可以使用某些特定的操作来改变它，所有指向它的变量都会显示它的改变。

如果我们有一个不可变对象 (str, int, tuple 等等)，所有指向它的变量都将显示相同样的值，但凡是会改变这个值的操作将总是返回一个新对象。

如果你想知道两个变量是否指向相同的对象，你可以使用 is 运算符，或内置函数 id()。

如何编写带输出参数的函数（通过引用调用）？

请记住在 Python 中参数是通过赋值来传递的。 由于赋值只是创建了对象的引用，因此在调用者和被调用者的参数名称之间没有别名，所以本身是没有按引用调用的。 你可以通过多种方式实现所需的效果。

通过返回一个结果元组:

def func2(a, b):
    a = 'new-value'        # a and b are local names
    b = b + 1              # assigned to new objects
    return a, b            # return new values

x, y = 'old-value', 99
x, y = func2(x, y)
print(x, y)                # output: new-value 100

这几乎总是最清晰明了的解决方案。

通过使用全局变量。 这种方式不是线程安全的，而且也不受推荐。

通过传递一个可变 (即可原地修改的) 对象:

def func1(a):
    a[0] = 'new-value'     # 'a' references a mutable list
    a[1] = a[1] + 1        # changes a shared object

args = ['old-value', 99]
func1(args)
print(args[0], args[1])    # output: new-value 100

通过传递一个会被改变的字典:

def func3(args):
    args['a'] = 'new-value'     # args is a mutable dictionary
    args['b'] = args['b'] + 1   # change it in-place

args = {'a': 'old-value', 'b': 99}
func3(args)
print(args['a'], args['b'])

或者在一个类实例中捆绑值:

class callByRef:
    def __init__(self, **args):
        for (key, value) in args.items():
            setattr(self, key, value)

def func4(args):
    args.a = 'new-value'        # args is a mutable callByRef
    args.b = args.b + 1         # change object in-place

args = callByRef(a='old-value', b=99)
func4(args)
print(args.a, args.b)

几乎没有任何适当理由将问题如此复杂化。

你的最佳选择是返回一个包含多个结果的元组。

如何在Python中创建高阶函数？

你有两种选择：使用嵌套作用域，或者使用可调用对象。 例如，假设你想要定义 linear(a,b) 使其返回一个函数 f(x) 来设计 a*x+b 的值。 可以使用以下嵌套作用域:

def linear(a, b):
    def result(x):
        return a * x + b
    return result

或使用一个可调用对象:

class linear:

    def __init__(self, a, b):
        self.a, self.b = a, b

    def __call__(self, x):
        return self.a * x + self.b

在两种情况下，:

taxes = linear(0.3, 2)

都会给出一个可调用对象，使得 taxes(10e6) == 0.3 * 10e6 + 2.

可调用对象方式的缺点是速度略慢且生成的代码略长。 但是，请注意一组可调用对象能够通过继承来共享签名:

class exponential(linear):
    # __init__ inherited
    def __call__(self, x):
        return self.a * (x ** self.b)

对象可以封装多个方法的状态:

class counter:

    value = 0

    def set(self, x):
        self.value = x

    def up(self):
        self.value = self.value + 1

    def down(self):
        self.value = self.value - 1

count = counter()
inc, dec, reset = count.up, count.down, count.set

这里 inc(), dec() 和 reset() 将表现为共享同一计数变量的多个函数。

如何在Python中复制对象？

一般来说，通常情况下请尝试 copy.copy() 或 copy.deepcopy()。 不是所有对象都可以复制，但多数都是可以的。

某些对象可以方便地复制。 例如字典具有 copy() 方法:

newdict = olddict.copy()

序列可以通过切片来复制:

new_l = l[:]

如何找到对象的方法或属性？

对于一个用户自定义类的实例 x，dir(x) 将返回一个按字母顺序排序的包含实例属性和方法及其类所定义的属性名称的列表。

我的代码如何才能发现对象的名称？

Generally speaking, it can’t, because objects don’t really have names. Essentially, assignment always binds a name to a value; The same is true of def and class statements, but in that case the value is a callable. Consider the following code:

>>> class A:
...     pass
...
>>> B = A
>>> a = B()
>>> b = a
>>> print(b)
<__main__.A object at 0x16D07CC>
>>> print(a)
<__main__.A object at 0x16D07CC>

不严谨地讲，该类有一个名称：虽然它是绑定了两个名称并通过名称 B 发起调用，所创建的实例仍然被视为类 A 的一个实例。 但是实例的名称则无法确定地说是 a 或是 b，因为有两个名称被绑定到了同一个值。

一般来说你的代码应该没有必要“知道”特定值的名称。 除非你是在编写特殊的内省程序，出现这样的问题通常表明如果改变方式可能会更有利。

在 comp.lang.python 中，Fredrik Lundh 在回答这样的问题时曾经给出过一个绝佳的类比：

跟你找出在你家门廊见到的某只猫的名字所用的办法一样：猫（对象）自己无法告诉你它的名字，它根本就不在乎 —— 所以找出它叫什么名字的唯一办法是问你的所有邻居（命名空间）那是不是他们的猫（对象）……

……并且如果你发现它有很多名字或根本没有名字也不必觉得惊讶！

逗号运算符的优先级是什么？

逗号在 Python 中不是运算符。 考虑这个例子:

>>> "a" in "b", "a"
(False, 'a')

由于逗号不是运算符而是表达式之间的分隔符，以上代码的含义就相当于:

("a" in "b"), "a"

而不是:

"a" in ("b", "a")

对于各种赋值运算符 (=, += 等) 来说同样如此。 它们并不是真正的运算符而是赋值语句中的语法分隔符。

是否有与 C 的 “?:” 三目运算符等价的东西？

有的。 相应语法如下:

[on_true] if [expression] else [on_false]

x, y = 50, 25
small = x if x < y else y

在 Python 2.5 引入此语法之前，常见的做法是使用逻辑运算符:

[expression] and [on_true] or [on_false]

然而这种做法并不保险，因为当 on_true 具有布尔假值时将会给出错误的结果。 所以，使用 ... if ... else ... 形式总是会更好。

是否可以用Python编写混淆的单行程序?

Yes. Usually this is done by nesting lambda within lambda. See the following three examples, due to Ulf Bartelt:

from functools import reduce

# Primes < 1000
print(list(filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))))

# First 10 Fibonacci numbers
print(list(map(lambda x,f=lambda x,f:(f(x-1,f)+f(x-2,f)) if x>1 else 1:
f(x,f), range(10))))

# Mandelbrot set
print((lambda Ru,Ro,Iu,Io,IM,Sx,Sy:reduce(lambda x,y:x+y,map(lambda y,
Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,Sy=Sy,L=lambda yc,Iu=Iu,Io=Io,Ru=Ru,Ro=Ro,i=IM,
Sx=Sx,Sy=Sy:reduce(lambda x,y:x+y,map(lambda x,xc=Ru,yc=yc,Ru=Ru,Ro=Ro,
i=i,Sx=Sx,F=lambda xc,yc,x,y,k,f=lambda xc,yc,x,y,k,f:(k<=0)or (x*x+y*y
>=4.0) or 1+f(xc,yc,x*x-y*y+xc,2.0*x*y+yc,k-1,f):f(xc,yc,x,y,k,f):chr(
64+F(Ru+x*(Ro-Ru)/Sx,yc,0,0,i)),range(Sx))):L(Iu+y*(Io-Iu)/Sy),range(Sy
))))(-2.1, 0.7, -1.2, 1.2, 30, 80, 24))
#    \___ ___/  \___ ___/  |   |   |__ lines on screen
#        V          V      |   |______ columns on screen
#        |          |      |__________ maximum of "iterations"
#        |          |_________________ range on y axis
#        |____________________________ range on x axis

请不要在家里尝试，骚年！

数字和字符串

如何指定十六进制和八进制整数？

要指定一个八进制数码，则在八进制值之前加一个零和一个小写或大写字母 “o” 作为前缀。 例如，要将变量 “a” 设为八进制的 “10” (十进制的 8)，就输入:

>>> a = 0o10
>>> a
8

十六进制数也同样简单。 只要在十六进制数之前加一个零和一个小写或大写字母 “x”。 十六进制数码中的字母可以为大写或小写。 例如在 Python 解释器中输入:

>>> a = 0xa5
>>> a
165
>>> b = 0XB2
>>> b
178

为什么-22 // 10返回-3？

这主要是为了让 i % j 的正负与 j 一致，如果你想要这样的结果，并且又想要:

i == (i // j) * j + (i % j)

那么整除就必须向下取整。 C 同样要求保持一致，并且编译器在截短 i // j 的结果值时需要使 i % j 的正负与 i 一致。

对于 i % j 来说 j 为负值的应用场景实际上是非常少的。 而 j 为正值的情况则非常多，并且实际上在所有情况下让 i % j 的结果为 >= 0 会更有用处。 如果如果现在时间为 10 时，那么 200 小时前应是几时？ -190 % 12 == 2 是有用处的；-190 % 12 == -10 则是会导致意外的漏洞。

如何将字符串转换为数字？

对于整数，可使用内置的 int() 类型构造器，例如 int('144') == 144。 类似地，可使用 float() 转换为浮点数，例如 float('144') == 144.0。

默认情况下，这些操作会将数字按十进制来解读，因此 int('0144') == 144 而 int('0x144') 会引发 ValueError。 int(string, base) 接受第二个可选参数指定转换的基数，例如 int('0x144', 16) == 324。 如果指定基数为 0，则按 Python 规则解读数字：前缀 ‘0o’ 表示八进制，而 ‘0x’ 表示十六进制。

如果你只是想将字符串转为数字，请不要使用内置函数 eval()。 eval() 的速度会慢很多并且有安全风险：别人可能会传入具有你不想要的附带效果的 Python 表达式。 例如，别人可以传入 __import__('os').system("rm -rf $HOME") 这将删除你的家目录。

eval() 还具有将数字解读为 Python 表达式的效果，这样 eval('09') 将会导致语法错误，因为 Python 不允许十进制数的首位是 ‘0’ (‘0’ 除外)。

如何将数字转换为字符串？

例如要将数字 144 转换为字符串 ‘144’，可使用内置类型构造器 str()。 如果想要表示为十六进制或八进制数，可使用内置函数 hex() 或 oct()。 想要更好地格式化，请参阅 格式化字符串字面值 和 格式字符串语法 等小节，例如 "{:04d}".format(144) 生成 '0144' 而 "{:.3f}".format(1.0/3.0) 生成 '0.333'。

如何修改字符串？

无法修改，因为字符串是不可变对象。 在大多数情况下，你应该使用你想要的各种部分来构造一个新字符串。 但是，如果你想要一个可以原地修改 Unicode 数据的对象，可尝试使用 io.StringIO 对象或 array 模块:

>>> import io
>>> s = "Hello, world"
>>> sio = io.StringIO(s)
>>> sio.getvalue()
'Hello, world'
>>> sio.seek(7)
7
>>> sio.write("there!")
6
>>> sio.getvalue()
'Hello, there!'

>>> import array
>>> a = array.array('u', s)
>>> print(a)
array('u', 'Hello, world')
>>> a[0] = 'y'
>>> print(a)
array('u', 'yello, world')
>>> a.tounicode()
'yello, world'

如何使用字符串调用函数/方法？

有多种技巧可供选择。

最好的做法是使用一个将字符串映射到函数的字典。 这一技巧的主要优势在于字符串不必与函数名称一致。 这也是用于模拟其他语言中 case 结构的主要技巧:

def a():
    pass

def b():
    pass

dispatch = {'go': a, 'stop': b}  # Note lack of parens for funcs

dispatch[get_input()]()  # Note trailing parens to call function

使用内置函数 getattr()

import foo
getattr(foo, 'bar')()

请注意 getattr() 可用于任何对象，包括类、类实例、模块等等。

在标准库中多次使用了这个技巧，例如:

class Foo:
    def do_foo(self):
        ...

    def do_bar(self):
        ...

f = getattr(foo_instance, 'do_' + opname)
f()

使用 locals() 或 eval() 来解析出函数名:

def myFunc():
    print("hello")

fname = "myFunc"

f = locals()[fname]
f()

f = eval(fname)
f()

注意：使用 eval() 速度慢而且危险。 如果你不能绝对掌控字符串的内容，别人将能传入可被解析为任意函数直接执行的字符串。

是否有与Perl 的chomp() 等效的方法，用于从字符串中删除尾随换行符？

可以使用 S.rstrip("\r\n") 从字符串 S 的末尾删除所有的换行符，而不删除其他尾随空格。如果字符串 S 表示多行，且末尾有几个空行，则将删除所有空行的换行符：

>>> lines = ("line 1 \r\n"
...          "\r\n"
...          "\r\n")
>>> lines.rstrip("\n\r")
'line 1 '

由于通常只在一次读取一行文本时才需要这样做，所以使用 S.rstrip() 这种方式工作得很好。

是否有 scanf() 或 sscanf() 的对应物？

没有这样的对应物。

对于简单的输入解析，最方便的做法通常是使用字符串对象的 split() 方法将一行内容拆解为以空格分隔的单词，然后使用 int() 或 float() 将表示十进制数的字符串转换为数值。 split() 支持可选的 “sep” 形参，适用于内容行使用空格符以外的分隔符的情况。

以于更复杂的输入解析，正则表达式会比 C 的 sscanf() 更强大，也更适合此类任务。

‘UnicodeDecodeError’ 或 ‘UnicodeEncodeError’ 错误是什么意思？

见 Unicode 指南

性能

我的程序太慢了。该如何加快速度？

总的来说，这是个棘手的问题。首先，下面列出了深入了解前需要记住的事情：

Performance characteristics vary across Python implementations. This FAQ focusses on CPython.

行为可能因操作系统而异，尤其是在谈论 I / O 或多线程时。

在尝试优化任何代码 前 ，应始终找到程序中的热点（请参阅 profile 模块）。

编写基准脚本将允许您在搜索改进时快速迭代（请参阅 timeit 模块）。

强烈建议在可能引入隐藏在复杂优化中的回归之前，要有良好的代码覆盖率（通过单元测试或任何其他技术）。

话虽如此，加速Python代码有很多技巧。以下是一些可以达到可接受的性能水平的一般原则：

使您的算法更快（或更改为更快的算法）可以产生比尝试在代码中使用微优化技巧更大的好处。

使用正确的数据结构。参考文档 内置类型 和 collections 模块。

当标准库提供用于执行某些操作的原语时，可能（尽管不能保证）比您可能提出的任何替代方案更快。对于用C编写的原语，例如内置函数和一些扩展类型，这是真的。例如，请确保使用 list.sort() 内置方法或相关的 sorted() 函数进行排序（有关适度高级用法的示例，请参阅 排序指南 ）。

抽象倾向于创造间接性并迫使翻译更多地工作。如果间接级别超过完成的有用工作量，则程序将变慢。你应该避免过度抽象，特别是在微小的功能或方法的形式下（这通常也会对可读性产生不利影响）。

如果你已经达到纯 Python 允许的限制，那么有一些工具可以让你走得更远。 例如， Cython [http://cython.org] 可以将稍微修改的 Python 代码版本编译为 C 扩展，并且可以在许多不同的平台上使用。 Cython 可以利用编译（和可选的类型注释）来使代码明显快于解释运行时的速度。 如果您对 C 编程技能有信心，也可以自己 编写 C 扩展模块 。

参见

专门介绍 性能提示 [https://wiki.python.org/moin/PythonSpeed/PerformanceTips] 的wiki页面。

将多个字符串连接在一起的最有效方法是什么？

str 和 bytes 对象是不可变的，因此将多个字符串连接在一起效率很低，因为每个连接都会创建一个新对象。在一般情况下，总运行时间是总字符串长度的二次方。

要连接多个 str 对象，通常推荐的用法是将它们放入一个列表中并在结尾处调用 str.join() ：

chunks = []
for s in my_strings:
    chunks.append(s)
result = ''.join(chunks)

（另一个合理有效的惯用方法是 io.StringIO ）

要连接多个 str 对象，建议使用本地连接（ += 运算符）扩展 bytearray 对象：

result = bytearray()
for b in my_bytes_objects:
    result += b

序列（元组/列表）

如何在元组和列表之间进行转换？

类型构造器 tuple(seq) 可将任意序列（实际上是任意可迭代对象）转换为具有相同排列顺序的相同条目的元组。

例如，tuple([1, 2, 3]) 产生 (1, 2, 3) 而 tuple('abc') 产生 ('a', 'b', 'c')。 如果参数为一个元组，它不会创建副本而是返回同一对象，因此如果你不确定某个对象是否为元组时也可简单地调用 tuple()。

类型构造器 list(seq) 可将任意序列或可迭代对象转换为具有相同排列顺序的相同条目的列表。 例如，list((1, 2, 3)) 产生 [1, 2, 3] 而 list('abc') 产生 ['a', 'b', 'c']。 如果参数为一个列表，它会像 seq[:] 那样创建一个副本。

什么是负数序号？

Python 序列使用正数或负数作为序号或称索引号。 对于正数序号，第一个序号为 0 而 1 为第二个序号，依此类推。 对于负数序号，倒数第一个序号为 -1 而倒数第二个序号为 -2，依此类推。 可以认为 seq[-n] 就相当于 seq[len(seq)-n]。

使用负数序号有时会很方便。 例如 S[:-1] 就是原字符串去掉最后一个字符，这可以用来移除某个字符串末尾的换行符。

如何以相反的顺序迭代序列？

使用 reversed() 内置函数，这是Python 2.4中的新功能:

for x in reversed(sequence):
    ...  # do something with x ...

这不会修改您的原始序列，而是构建一个反向顺序的新副本以进行迭代。

在 Python 2.3 里，您可以使用扩展切片语法:

for x in sequence[::-1]:
    ...  # do something with x ...

如何从列表中删除重复项？

有关执行此操作的许多方法的详细讨论，请参阅 Python Cookbook:

https://code.activestate.com/recipes/52560/

如果您不介意重新排序列表，请对其进行排序，然后从列表末尾进行扫描，删除重复项：

if mylist:
    mylist.sort()
    last = mylist[-1]
    for i in range(len(mylist)-2, -1, -1):
        if last == mylist[i]:
            del mylist[i]
        else:
            last = mylist[i]

如果列表的所有元素都可以用作设置键（即：它们都是 hashable ），这通常会更快:

mylist = list(set(mylist))

这会将列表转换为集合，从而删除重复项，然后返回到列表中。

如何在Python中创建数组？

使用列表:

["this", 1, "is", "an", "array"]

列表在时间复杂度方面相当于C或Pascal数组；主要区别在于，python列表可以包含许多不同类型的对象。

array 模块还提供了创建具有紧凑表示的固定类型的数组的方法，但它的索引速度比列表慢。还要注意，数字扩展和其他扩展还定义了具有各种特性的类似数组的结构。

要获取Lisp样式的列表，可以使用元组模拟cons单元：

lisp_list = ("like",  ("this",  ("example", None) ) )

如果需要可变性，可以使用列表而不是元组。这里模拟lisp car的是 lisp_list[0] ，模拟cdr的是 lisp_list[1] 。只有在你确定真的需要的时候才这样做，因为它通常比使用Python列表慢得多。

如何创建多维列表？

你可能试图制作一个像这样的多维数组:

>>> A = [[None] * 2] * 3

如果你打印它，看起来是正确的：

>>> A
[[None, None], [None, None], [None, None]]

但是，当你给某一项赋值时，会同时在多个位置显示变化：

>>> A[0][0] = 5
>>> A
[[5, None], [5, None], [5, None]]

其中的原因在于使用 * 对列表执行重复操作并不是创建副本，它只是创建现有对象的引用。 *3 创建了对长度为二的同一列表的 3 个引用。 对某一行的改变会作用于所有行，通常这一定不是你所希望的。

建议的做法是先创建一个所需长度的列表，然后其中的元素再以一个新创建的列表来填充:

A = [None] * 3
for i in range(3):
    A[i] = [None] * 2

这样就生成了一个包含 3 个长度为二的不同列表的列表。 你也可以使用列表推导式:

w, h = 2, 3
A = [[None] * w for i in range(h)]

或者你还可以使用提供矩阵类型的扩展包；其中最著名的是 NumPy [http://www.numpy.org/]。

如何将方法应用于一系列对象？

可以使用列表推导式：

result = [obj.method() for obj in mylist]

为什么 a_tuple[i] += [‘item’] 会在执行加法时引发异常？

这是由两个事实共同导致的结果，一是增强赋值运算符属于 赋值 运算符，二是在 Python 中存在可变和不可变两种不同的对象。

此处的讨论在任何对元组中指向可变对象的元素使用增强赋值运算符的情况都是普遍成立的，但在此我们只以 list 和 += 来举例。

如果你写成这样:

>>> a_tuple = (1, 2)
>>> a_tuple[0] += 1
Traceback (most recent call last):
   ...
TypeError: 'tuple' object does not support item assignment

发生异常的原因是显而易见的: 1 会与对象 a_tuple[0] 相加，而该对象为 (1)，得到结果对象 2，但当我们试图将运算结果 2 赋值给元组的 0 号元素时就将报错，因为我们不能改变元组的元素所指向的对象。

在表层之处，以上增强赋值语句所做的大致是这样:

>>> result = a_tuple[0] + 1
>>> a_tuple[0] = result
Traceback (most recent call last):
  ...
TypeError: 'tuple' object does not support item assignment

由于元组是不可变的，因此操作的赋值部分会引发错误。

当你这样写的时候:

>>> a_tuple = (['foo'], 'bar')
>>> a_tuple[0] += ['item']
Traceback (most recent call last):
  ...
TypeError: 'tuple' object does not support item assignment

发生异常会令人略感吃惊，还有一个更为令人吃惊的事实：虽然有报错，但是添加操作却生效了:

>>> a_tuple[0]
['foo', 'item']

要明白为何会这样，你需要知道 (a) 如果一个对象实现了 __iadd__ 魔术方法，它会在执行 += 增强赋值时被调用，并且其返回值将用于该赋值语句； (b) 对于列表来说，__iadd__ 等价于在列表上调用 extend 并返回该列表。 因此对于列表我们可以说 += 就是 list.extend 的“快捷方式”:

>>> a_list = []
>>> a_list += [1]
>>> a_list
[1]

这相当于:

>>> result = a_list.__iadd__([1])
>>> a_list = result

a_list 所引用的对象已被修改，而引用被修改对象的指针又重新被赋值给 a_list。 赋值的最终结果没有变化，因为它是引用 a_list 之前所引用的同一对象的指针，但仍然发生了赋值操作。

因此，在我们的元组示例中，发生的事情等同于：

>>> result = a_tuple[0].__iadd__(['item'])
>>> a_tuple[0] = result
Traceback (most recent call last):
  ...
TypeError: 'tuple' object does not support item assignment

__iadd__ 成功执行，因此列表得到了扩充，但是虽然 result 指向了 a_tuple[0] 已经指向的同一对象，最后的赋值仍然导致了报错，因为元组是不可变的。

Dictionaries

How can I get a dictionary to store and display its keys in a consistent order?

Use collections.OrderedDict.

我想做一个复杂的排序：你能用Python做一个Schwartzian变换吗？

该技术归功于Perl社区的 Randal Schwartz，它通过将每个元素映射到其 “排序值（sort value）” 的度量对列表中的元素进行排序。在Python中，使用 list.sort() 方法的 key 参数：

Isorted = L[:]
Isorted.sort(key=lambda s: int(s[10:15]))

如何按其他列表中的值对一个列表进行排序？

将它们合并到元组的迭代器中，对结果列表进行排序，然后选择所需的元素。

>>> list1 = ["what", "I'm", "sorting", "by"]
>>> list2 = ["something", "else", "to", "sort"]
>>> pairs = zip(list1, list2)
>>> pairs = sorted(pairs)
>>> pairs
[("I'm", 'else'), ('by', 'sort'), ('sorting', 'to'), ('what', 'something')]
>>> result = [x[1] for x in pairs]
>>> result
['else', 'sort', 'to', 'something']

最后一步的替代方案是:

>>> result = []
>>> for p in pairs: result.append(p[1])

如果你觉得这个更容易读懂，那么你可能更喜欢使用这个而不是前面的列表推导。然而，对于长列表来说，它的速度几乎是原来的两倍。为什么？首先， append() 操作必须重新分配内存，虽然它使用了一些技巧来避免每次都这样做，但它仍然偶尔需要这样做，而且代价相当高。第二，表达式 “result.append” 需要额外的属性查找。第三，必须执行所有这些函数调用会降低速度。

对象

什么是类？

类 是通过执行类语句创建的特定对象类型。类对象 被当作模板来创建实例对象，实例对象包含了特定于数据类型的数据（属性）和代码（方法）。

类可以基于一个或多个的其他类，称之为基类（ES），它继承基类的属性和方法，这样就可以通过继承来连续地细化对象模型。例如：您可能有一个 Mailbox 类提供邮箱的基本访问方法.，它的子类 MboxMailbox, MaildirMailbox, OutlookMailbox 用于处理各种特定邮箱格式。

什么是方法？

方法 实际上就是类定义中的函数。对于某个对象 x 上的方法，通常称为 x.name(arguments...) 。

class C:
    def meth(self, arg):
        return arg * 2 + self.attribute

什么是 self ？

Self 只是 方法 的第一个参数的常规名称。例如：对于某个类的某个实例 x ，其方法 meth(self, a, b, c) 实际上应该被称为 x.meth(a, b, c) ；对于被调用的方法会被称为 meth(x, a, b, c) 。

另请参阅 为什么必须在方法定义和调用中显式使用“self”？ 。

如何检查对象是否为给定类或其子类的一个实例？

可使用内置函数 isinstance(obj, cls)。 你可以提供一个元组而不是单个类来检查某个对象是否为任意多个类当中某一个类的实例，例如 isinstance(obj, (class1, class2, ...))，也可以检查某个对象是否为 Python 内置类型当中某一个类型的对象，例如 isinstance(obj, str) 或 isinstance(obj, (int, float, complex))。

请注意大多数程序不会经常对用户自定义类使用 isinstance()。 如果是你自已开发的类，更正确的面向对象风格是在类中定义方法来封装特定的行为，而不是检查对象的类并根据它属于什么类来做不同的事。 例如，如果你有一个执行某些操作的函数:

def search(obj):
    if isinstance(obj, Mailbox):
        ...  # code to search a mailbox
    elif isinstance(obj, Document):
        ...  # code to search a document
    elif ...

更好的方法是在所有类上定义一个 search() 方法，然后调用它：

class Mailbox:
    def search(self):
        ...  # code to search a mailbox

class Document:
    def search(self):
        ...  # code to search a document

obj.search()

什么是委托？

委托是一种面向对象的技巧（也称为设计模式）。 假设您有一个对象 x 并且想要改变其中一个方法的行为。 您可以创建一个新类，它提供您感兴趣的方法的新实现，并将所有其他方法委托给 x 的相应方法。

Python程序员可以轻松实现委托。 例如，以下类实现了一个类，该类的行为类似于文件，但将所有写入的数据转换为大写：

class UpperOut:

    def __init__(self, outfile):
        self._outfile = outfile

    def write(self, s):
        self._outfile.write(s.upper())

    def __getattr__(self, name):
        return getattr(self._outfile, name)

Here the UpperOut class redefines the write() method to convert the argument string to uppercase before calling the underlying self.__outfile.write() method. All other methods are delegated to the underlying self.__outfile object. The delegation is accomplished via the __getattr__ method; consult the language reference for more information about controlling attribute access.

请注意对于更一般的情况来说，委托可能包含更多细节问题。 当某些属性既需要读取又需要设置时，类还必须定义 __setattr__() 方法，并且这样做必须小心谨慎。 __setattr__() 的基本实现大致相当于以下代码:

class X:
    ...
    def __setattr__(self, name, value):
        self.__dict__[name] = value
    ...

大多数 __setattr__() 实现必须修改 self.__dict__ 来为自身保存局部状态而又不至于造成无限递归。

如何从覆盖基类的派生类调用基类中定义的方法?

使用内置的 super() 函数：

class Derived(Base):
    def meth(self):
        super(Derived, self).meth()

对于 Python 3.0之前的版本，您可能正在使用经典类：对于诸如 class Derived(Base): ... 之类的类定义，可以将在 Base (或 Base 中的一个的基类）中定义的方法 meth() 调用为 Base.meth(self, arguments...) 。这里， Base.meth 是一个未绑定的方法，因此您需要提供 self 参数。

如何组织代码以便更改基类？

可以为基类定义别名，在类定义之前为其分配实际基类，并在整个类中使用别名。然后更改分配给别名的值，就能实现上述要求。顺便提一下，如果你想动态决定（例如，取决于资源的可用性）要使用哪个基类，这个技巧也很方便。例如：

BaseAlias = <real base class>

class Derived(BaseAlias):
    def meth(self):
        BaseAlias.meth(self)
        ...

如何创建静态类数据和静态类方法？

Python支持静态数据和静态方法（在C ++或Java的意义上）。

对于静态数据，只需定义一个类属性。要为属性分配新值，就必须在赋值中显式使用类名：

class C:
    count = 0   # number of times C.__init__ called

    def __init__(self):
        C.count = C.count + 1

    def getcount(self):
        return C.count  # or return self.count

对于任意 c 来说只要 isinstance(c, C) 为真，则 c.count 同样也指向 C.count，除非被 c 自身，或者从 c.__class__ 回到 C 的基类搜索路径上的某个类所重载。

注意：在 C 的某个方法内部，像 self.count = 42 这样的赋值将在 self 自身的字典中新建一个名为 “count” 的不相关实例。 想要重新绑定类静态数据名称就必须总是指明类名，无论是在方法内部还是外部:

C.count = 314

静态方法是可行的：

class C:
    @staticmethod
    def static(arg1, arg2, arg3):
        # No 'self' parameter!
        ...

然而，获得静态方法效果的更直接的方法是通过一个简单的模块级函数：

def getcount():
    return C.count

如果您的代码是结构化的，以便为每个模块定义一个类（或紧密相关的类层次结构），那么这就提供了所需的封装。

如何在Python中重载构造函数（或方法）？

这个答案实际上适用于所有方法，但问题通常首先出现在构造函数的上下文中。

在C ++中，你会这样写

class C {
    C() { cout << "No arguments\n"; }
    C(int i) { cout << "Argument is " << i << "\n"; }
}

在Python中，您必须编写一个构造函数，使用默认参数捕获所有情况。例如：

class C:
    def __init__(self, i=None):
        if i is None:
            print("No arguments")
        else:
            print("Argument is", i)

这不完全等同，但在实践中足够接近。

你也可以尝试一个可变长度的参数列表，例如:

def __init__(self, *args):
    ...

相同的方法适用于所有方法定义。

我尝试使用 __spam ，但是得到一个关于 _SomeClassName__spam 的错误信息。

以双下划线打头的变量会被“更名”以提供一种定义类私有变量的简单而有效的方式。 任何形式为 __spam 的标识符（至少前缀两个下划线，至多后缀一个下划线）文本会被替换为 _classname__spam，其中 classname 为去除了全部前缀下划线的当前类名称。

这并不能保证私密性：外部用户仍然可以访问 “_classname__spam” 属性，私有变量值也在对象的 __dict__ 中可见。 许多 Python 程序员从来都不使用这种私有变量名称。

类定义了 __del__ 方法，但是删除对象时没有调用它。

这有几个可能的原因。

del 语句不一定调用 __del__() —— 它只是减少对象的引用计数，如果（引用计数）达到零，才会调用 __del__()。

如果数据结构包含循环链接（例如，每个子级都有一个父级引用，每个父级都有一个子级列表的树），则引用计数将永远不会返回零。尽管Python 偶尔会运行一个算法来检测这样的循环，但在数据结构的引用计数清零后，垃圾收集器可能需要一段时间来运行，因此 __del__() 方法可能会在不方便和随机的时间被调用。这对于重现一个问题，是非常不方便的。更糟糕的是，对象 __del__() 的方法执行顺序是任意的。虽然可以运行 gc.collect() 来强制回收，但在一些病态的情况下，对象永远不会被回收。

尽管有循环收集器，但在对象上定义一个显式的 close() 方法以便在用完之后调用它仍然是一个好主意。 这样 close() 方法可以随即删除引用子对象的属性。 不要直接调用 __del__() —— 应该由 __del__() 调用 close()，并且 close() 能确保可以被同一对象多次地调用。

另一种避免循环引用的方法是使用 weakref 模块，该模块允许您指向对象而不增加其引用计数。例如，树状数据结构应该对其父级和同级引用使用弱引用（如果需要的话！）

最后，如果 __del__() 方法引发异常，会将警告消息打印到 sys.stderr 。

如何获取给定类的所有实例的列表？

Python不跟踪类（或内置类型）的所有实例。您可以对类的构造函数进行编程，以通过保留每个实例的弱引用列表来跟踪所有实例。

为什么 id() 的结果看起来不是唯一的？

id() 返回一个整数，该整数在对象的生命周期内保证是唯一的。因为在CPython中，这是对象的内存地址，所以经常发生在从内存中删除对象之后，下一个新创建的对象被分配在内存中的相同位置。这个例子说明了这一点：

>>> id(1000)
13901272
>>> id(2000)
13901272

这两个id属于之前创建的不同整数对象，并在执行 id() 调用后立即删除。要确保要检查其id的对象仍处于活动状态，请创建对该对象的另一个引用：

>>> a = 1000; b = 2000
>>> id(a)
13901272
>>> id(b)
13891296

模块

如何创建 .pyc 文件？

当一个模块首次被导入时（或自当前已编译文件创建后源文件被修改时），将会在对应 .py 文件所在目录的 __pycache__ 子目录下创建一个包含已编译代码的 .pyc 文件。 该 .pyc 文件的文件名的开头部分将与对应 .py 文件名相同，并以 .pyc 为后缀，中间部门则是基于创建它的特定 python 二进制代码版本。 （详情参见 PEP 3147 [https://www.python.org/dev/peps/pep-3147]。）

无法创建 .pyc 文件的可能原因是包含源文件的目录存在权限问题，这意味着 __pycache__ 子目录无法被创建。 举例来说，如果你以某一用户来开发程序但以另一用户身份来运行程序时就可能发生问题，测试 Web 服务器就属于这种情况。

除非设置了 PYTHONDONTWRITEBYTECODE 环境变量，否则当你导入模块并且 Python 具有创建 __pycache__ 子目录并将已编译模块写入该子目录的能力（权限、存储空间等等）时就会自动创建 .pyc 文件。

在最高层级运行的 Python 脚本不被视为导入，因此不会创建 .pyc 文件。 例如，如果你有一个最高层级模块文件 foo.py，它又导入了另一个模块 xyz.py，当你运行 foo 模块 (通过输入终端命令 python foo.py)，则将为 xyz 创建一个 .pyc，因为 xyz 是被导入的，但不会为 foo 创建 .pyc 文件，因为 foo.py 不是被导入的。

如果你需要为 foo 创建 .pyc 文件 —— 即为不是被导入的模块创建 .pyc 文件 —— 你可以使用 py_compile 和 compileall 模块。

py_compile 模块能够手动编译任意模块。 一种做法是交互式地使用该模块中的 compile() 函数:

>>> import py_compile
>>> py_compile.compile('foo.py')

这将会将 .pyc 文件写入与 foo.py 相同位置下的 __pycache__ 子目录（或者你也可以通过可选参数 cfile 来重载该行为）。

你还可以使用 compileall 模块自动编译一个目录或多个目录下的所有文件。 具体做法可以是在命令行提示符中运行 compileall.py 并提供包含要编译 Python 文件的目录路径:

python -m compileall .

如何找到当前模块名称？

模块可以通过查看预定义的全局变量 __name__ 找到自己的模块名称。如果它的值为 '__main__' ，程序将作为脚本运行。通常，通过导入使用的许多模块也提供命令行界面或自检，并且只在检查 __name__ 之后，才执行之后的代码:

def main():
    print('Running test...')
    ...

if __name__ == '__main__':
    main()

如何让模块相互导入？

假设您有以下模块：

foo.py:

from bar import bar_var
foo_var = 1

bar.py:

from foo import foo_var
bar_var = 2

问题是解释器将执行以下步骤：

首先导入foo

创建用于foo的空全局变量

foo被编译并开始执行

foo 导入 bar

创建了用于bar 的空全局变量

bar被编译并开始执行

bar导入foo（这是一个空操作（no-op ），因为已经有一个名为foo的模块）

bar.foo_var = foo.foo_var

最后一步失败了，因为Python还没有解释foo，而foo的全局符号字典仍然是空的。

当你使用 import foo ，然后尝试在全局代码中访问 foo.foo_var 时，会发生同样的事情。

这个问题有（至少）三种可能的解决方法。

Guido van Rossum 建议避免使用 from <module> import ... ，并将所有代码放在函数中。全局变量和类变量的初始化只能使用常量或内置函数。这意味着导入模块中的所有内容都被引用为 <module>.<name> 。

Jim Roskind建议在每个模块中按以下顺序执行步骤：

导出（全局变量，函数和不需要导入基类的类）

导入 声明

活动代码（包括从导入值初始化的全局变量）。

van Rossum不喜欢这种方法，因为导入出现在一个陌生的地方，但这种方法确实有效。

Matthias Urlichs建议重构代码，以便首先不需要递归导入。

这些解决方案并不相互排斥。

__import__(‘x.y.z’) 返回 <module ‘x’>; 如何获取z?

考虑使用 importlib 中的函数 import_module() ：

z = importlib.import_module('x.y.z')

当我编辑了导入过的模块并重新导入它时，这些变化没有显示出来。为什么会这样？

出于效率和一致性的原因，Python仅在第一次导入模块时读取模块文件。如果不这么做，在一个由许多模块组成的程序中，每个模块都会导入相同的基本模块，那么基本模块将被解析和重新解析多次。要强制重新读取已更改的模块，请执行以下操作:

import importlib
import modname
importlib.reload(modname)

警告：这种技术不是100％万无一失。特别是包含如下语句的模块

from modname import some_objects

will continue to work with the old version of the imported objects. If the module contains class definitions, existing class instances will not be updated to use the new class definition. This can result in the following paradoxical behaviour:

>>> import importlib
>>> import cls
>>> c = cls.C()                # Create an instance of C
>>> importlib.reload(cls)
<module 'cls' from 'cls.py'>
>>> isinstance(c, cls.C)       # isinstance is false?!?
False

The nature of the problem is made clear if you print out the “identity” of the class objects:

>>> hex(id(c.__class__))
'0x7352a0'
>>> hex(id(cls.C))
'0x4198d0'

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

设计和历史常见问题

目录

设计和历史常见问题

为什么Python使用缩进来分组语句？

为什么简单的算术运算得到奇怪的结果？

为什么浮点计算不准确？

为什么Python字符串是不可变的？

为什么必须在方法定义和调用中显式使用“self”？

为什么不能在表达式中赋值？

为什么Python对某些功能（例如list.index()）使用方法来实现，而其他功能（例如len(List)）使用函数实现？

为什么 join()是一个字符串方法而不是列表或元组方法？

异常有多快？

为什么Python中没有switch或case语句？

难道不能在解释器中模拟线程，而非得依赖特定于操作系统的线程实现吗？

为什么lambda表达式不能包含语句？

可以将Python编译为机器代码，C或其他语言吗？

Python如何管理内存？

为什么CPython不使用更传统的垃圾回收方案？

CPython退出时为什么不释放所有内存？

为什么有单独的元组和列表数据类型？

列表是如何在CPython中实现的？

字典是如何在CPython中实现的？

为什么字典key必须是不可变的？

为什么 list.sort() 没有返回排序列表？

如何在Python中指定和实施接口规范？

为什么没有goto？

为什么原始字符串（r-strings）不能以反斜杠结尾？

为什么Python没有属性赋值的“with”语句？

为什么 if/while/def/class语句需要冒号？

为什么Python在列表和元组的末尾允许使用逗号？

为什么Python使用缩进来分组语句？

Guido van Rossum 认为使用缩进进行分组非常优雅，并且大大提高了普通Python程序的清晰度。大多数人在一段时间后就学会并喜欢上这个功能。

由于没有开始/结束括号，因此解析器感知的分组与人类读者之间不会存在分歧。偶尔C程序员会遇到像这样的代码片段:

if (x <= y)
        x++;
        y--;
z++;

如果条件为真，则只执行 x++ 语句，但缩进会使你认为情况并非如此。即使是经验丰富的C程序员有时会长时间盯着它，想知道为什么即使 x > y ， y 也在减少。

因为没有开始/结束括号，所以Python不太容易发生编码式冲突。在C中，括号可以放到许多不同的位置。如果您习惯于阅读和编写使用一种风格的代码，那么在阅读（或被要求编写）另一种风格时，您至少会感到有些不安。

许多编码风格将开始/结束括号单独放在一行上。这使得程序相当长，浪费了宝贵的屏幕空间，使得更难以对程序进行全面的了解。理想情况下，函数应该适合一个屏幕（例如，20–30行）。 20行Python可以完成比20行C更多的工作。这不仅仅是由于缺少开始/结束括号 – 缺少声明和高级数据类型也是其中的原因 – 但缩进基于语法肯定有帮助。

为什么简单的算术运算得到奇怪的结果？

请看下一个问题。

为什么浮点计算不准确？

用户经常对这样的结果感到惊讶:

>>> 1.2 - 1.0
0.19999999999999996

并且认为这是 Python中的一个 bug。其实不是这样。这与 Python 关系不大，而与底层平台如何处理浮点数字关系更大。

CPython 中的 float 类型使用C语言的 double 类型进行存储。 float 对象的值是以固定的精度（通常为 53 位）存储的二进制浮点数，由于 Python 使用 C 操作，而后者依赖于处理器中的硬件实现来执行浮点运算。 这意味着就浮点运算而言，Python 的行为类似于许多流行的语言，包括 C 和 Java。

许多可以轻松地用十进制表示的数字不能用二进制浮点表示。例如，在输入以下语句后:

>>> x = 1.2

为 x 存储的值是与十进制的值 1.2 (非常接近) 的近似值，但不完全等于它。 在典型的机器上，实际存储的值是：

1.0011001100110011001100110011001100110011001100110011 (binary)

它对应于十进制数值:

1.1999999999999999555910790149937383830547332763671875 (decimal)

典型的 53 位精度为 Python 浮点数提供了 15-16 位小数的精度。

要获得更完整的解释，请参阅 Python 教程中的 浮点算术 一章。

为什么Python字符串是不可变的？

有几个优点。

一个是性能：知道字符串是不可变的，意味着我们可以在创建时为它分配空间，并且存储需求是固定不变的。这也是元组和列表之间区别的原因之一。

另一个优点是，Python 中的字符串被视为与数字一样“基本”。 任何动作都不会将值 8 更改为其他值，在 Python 中，任何动作都不会将字符串 “8” 更改为其他值。

为什么必须在方法定义和调用中显式使用“self”？

这个想法借鉴了 Modula-3 语言。 出于多种原因它被证明是非常有用的。

首先，更明显的显示出，使用的是方法或实例属性而不是局部变量。 阅读 self.x 或 self.meth() 可以清楚地表明，即使您不知道类的定义，也会使用实例变量或方法。在 C++ 中，可以通过缺少局部变量声明来判断（假设全局变量很少见或容易识别） —— 但是在 Python 中没有局部变量声明，所以必须查找类定义才能确定。 一些 C++ 和 Java 编码标准要求实例属性具有 m_ 前缀，因此这种显式性在这些语言中仍然有用。

其次，这意味着如果要显式引用或从特定类调用该方法，不需要特殊语法。 在 C++ 中，如果你想使用在派生类中重写基类中的方法，你必须使用 :: 运算符 – 在 Python 中你可以编写 baseclass.methodname(self, <argument list>)。 这对于 __init__() 方法非常有用，特别是在派生类方法想要扩展同名的基类方法，而必须以某种方式调用基类方法时。

最后，它解决了变量赋值的语法问题：为了 Python 中的局部变量（根据定义！）在函数体中赋值的那些变量（并且没有明确声明为全局）赋值，就必须以某种方式告诉解释器一个赋值是为了分配一个实例变量而不是一个局部变量，它最好是通过语法实现的（出于效率原因）。 C++ 通过声明来做到这一点，但是 Python 没有声明，仅仅为了这个目的而引入它们会很可惜。 使用显式的 self.var 很好地解决了这个问题。 类似地，对于使用实例变量，必须编写 self.var 意味着对方法内部的非限定名称的引用不必搜索实例的目录。 换句话说，局部变量和实例变量存在于两个不同的命名空间中，您需要告诉 Python 使用哪个命名空间。

为什么不能在表达式中赋值？

许多习惯于C或Perl的人抱怨，他们想要使用C 的这个特性：

while (line = readline(f)) {
    // do something with line
}

但在Python中被强制写成这样:

while True:
    line = f.readline()
    if not line:
        break
    ...  # do something with line

不允许在 Python 表达式中赋值的原因是这些其他语言中常见的、很难发现的错误，是由这个结构引起的：

if (x = 0) {
    // error handling
}
else {
    // code that only works for nonzero x
}

错误是一个简单的错字： x = 0 ，将0赋给变量 x ，而比较 x == 0 肯定是可以预期的。

已经有许多替代方案提案。 大多数是为了少打一些字的黑客方案，但使用任意或隐含的语法或关键词，并不符合语言变更提案的简单标准：它应该直观地向尚未被介绍到这一概念的人类读者提供正确的含义。

一个有趣的现象是，大多数有经验的Python程序员都认识到 while True 的习惯用法，也不太在意是否能在表达式构造中赋值; 只有新人表达了强烈的愿望希望将其添加到语言中。

有一种替代的拼写方式看起来很有吸引力，但通常不如”while True”解决方案可靠：

line = f.readline()
while line:
    ...  # do something with line...
    line = f.readline()

问题在于，如果你改变主意（例如你想把它改成 sys.stdin.readline() ），如何知道下一行。你必须记住改变程序中的两个地方 – 第二次出现隐藏在循环的底部。

最好的方法是使用迭代器，这样能通过 for 语句来循环遍历对象。例如 file objects 支持迭代器协议，因此可以简单地写成:

for line in f:
    ...  # do something with line...

为什么Python对某些功能（例如list.index()）使用方法来实现，而其他功能（例如len(List)）使用函数实现？

正如Guido所说：

(a) 对于某些操作，前缀表示法比后缀更容易阅读 – 前缀（和中缀！）运算在数学中有着悠久的传统，就像在视觉上帮助数学家思考问题的记法。比较一下我们将 x*(a+b) 这样的公式改写为 x*a+x*b 的容易程度，以及使用原始OO符号做相同事情的笨拙程度。

(b) 当读到写有len(X)的代码时，就知道它要求的是某件东西的长度。这告诉我们两件事：结果是一个整数，参数是某种容器。相反，当阅读x.len()时，必须已经知道x是某种实现接口的容器，或者是从具有标准len()的类继承的容器。当没有实现映射的类有get()或key()方法，或者不是文件的类有write()方法时，我们偶尔会感到困惑。

—https://mail.python.org/pipermail/python-3000/2006-November/004643.html

为什么 join()是一个字符串方法而不是列表或元组方法？

从Python 1.6开始，字符串变得更像其他标准类型，当添加方法时，这些方法提供的功能与始终使用String模块的函数时提供的功能相同。这些新方法中的大多数已被广泛接受，但似乎让一些程序员感到不舒服的一种方法是：

", ".join(['1', '2', '4', '8', '16'])

结果如下:

"1, 2, 4, 8, 16"

反对这种用法有两个常见的论点。

第一条是这样的：“使用字符串文本(String Constant)的方法看起来真的很难看”，答案是也许吧，但是字符串文本只是一个固定值。如果在绑定到字符串的名称上允许使用这些方法，则没有逻辑上的理由使其在文字上不可用。

第二个异议通常是这样的：“我实际上是在告诉序列使用字符串常量将其成员连接在一起”。遗憾的是并非如此。出于某种原因，把 split() 作为一个字符串方法似乎要容易得多，因为在这种情况下，很容易看到:

"1, 2, 4, 8, 16".split(", ")

是对字符串文本的指令，用于返回由给定分隔符分隔的子字符串（或在默认情况下，返回任意空格）。

join() 是字符串方法，因为在使用该方法时，您告诉分隔符字符串去迭代一个字符串序列，并在相邻元素之间插入自身。此方法的参数可以是任何遵循序列规则的对象，包括您自己定义的任何新的类。对于字节和字节数组对象也有类似的方法。

异常有多快？

如果没有引发异常，则try/except块的效率极高。实际上捕获异常是昂贵的。在2.0之前的Python版本中，通常使用这个习惯用法:

try:
    value = mydict[key]
except KeyError:
    mydict[key] = getvalue(key)
    value = mydict[key]

只有当你期望dict在任何时候都有key时，这才有意义。如果不是这样的话，你就是应该这样编码：

if key in mydict:
    value = mydict[key]
else:
    value = mydict[key] = getvalue(key)

对于这种特定的情况，您还可以使用 value = dict.setdefault(key, getvalue(key))，但前提是调用 getvalue() 足够便宜，因为在所有情况下都会对其进行评估。

为什么Python中没有switch或case语句？

你可以通过一系列 if... elif... elif... else.轻松完成这项工作。对于switch语句语法已经有了一些建议，但尚未就是否以及如何进行范围测试达成共识。有关完整的详细信息和当前状态，请参阅 PEP 275 [https://www.python.org/dev/peps/pep-0275] 。

对于需要从大量可能性中进行选择的情况，可以创建一个字典，将case 值映射到要调用的函数。例如：

def function_1(...):
    ...

functions = {'a': function_1,
             'b': function_2,
             'c': self.method_1, ...}

func = functions[value]
func()

对于对象调用方法，可以通过使用 getattr() 内置检索具有特定名称的方法来进一步简化：

def visit_a(self, ...):
    ...
...

def dispatch(self, value):
    method_name = 'visit_' + str(value)
    method = getattr(self, method_name)
    method()

建议对方法名使用前缀，例如本例中的 visit_ 。如果没有这样的前缀，如果值来自不受信任的源，攻击者将能够调用对象上的任何方法。

难道不能在解释器中模拟线程，而非得依赖特定于操作系统的线程实现吗？

答案1： 不幸的是，解释器为每个Python堆栈帧推送至少一个C堆栈帧。此外，扩展可以随时回调Python。因此，一个完整的线程实现需要对C的线程支持。

Answer 2: Fortunately, there is Stackless Python [http://www.stackless.com], which has a completely redesigned interpreter loop that avoids the C stack.

为什么lambda表达式不能包含语句？

Python的 lambda表达式不能包含语句，因为Python的语法框架不能处理嵌套在表达式内部的语句。然而，在Python中，这并不是一个严重的问题。与其他语言中添加功能的lambda表单不同，Python的 lambdas只是一种速记符号，如果您懒得定义函数的话。

函数已经是Python中的第一类对象，可以在本地范围内声明。 因此，使用lambda而不是本地定义的函数的唯一优点是你不需要为函数创建一个名称 – 这只是一个分配了函数对象(与lambda表达式生成的对象类型完全相同)的局部变量！

可以将Python编译为机器代码，C或其他语言吗？

Cython [http://cython.org/] 将带有可选注释的Python修改版本编译到C扩展中。 Nuitka [http://www.nuitka.net/] 是一个将Python编译成 C++ 代码的新兴编译器，旨在支持完整的Python语言。要编译成Java，可以考虑 VOC [https://voc.readthedocs.io] 。

Python如何管理内存？

Python 内存管理的细节取决于实现。 Python 的标准实现 CPython 使用引用计数来检测不可访问的对象，并使用另一种机制来收集引用循环，定期执行循环检测算法来查找不可访问的循环并删除所涉及的对象。 gc 模块提供了执行垃圾回收、获取调试统计信息和优化收集器参数的函数。

但是，其他实现(如 Jython [http://www.jython.org] 或 PyPy [http://www.pypy.org] )，)可以依赖不同的机制，如完全的垃圾回收器 。如果你的Python代码依赖于引用计数实现的行为，则这种差异可能会导致一些微妙的移植问题。

在一些Python实现中，以下代码（在CPython中工作的很好）可能会耗尽文件描述符:

for file in very_long_list_of_files:
    f = open(file)
    c = f.read(1)

实际上，使用CPython的引用计数和析构函数方案， 每个新赋值的 f 都会关闭前一个文件。然而，对于传统的GC，这些文件对象只能以不同的时间间隔（可能很长的时间间隔）被收集（和关闭）。

如果要编写可用于任何python实现的代码，则应显式关闭该文件或使用 with 语句；无论内存管理方案如何，这都有效：

for file in very_long_list_of_files:
    with open(file) as f:
        c = f.read(1)

为什么CPython不使用更传统的垃圾回收方案？

首先，这不是C标准特性，因此不能移植。(是的，我们知道Boehm GC库。它包含了 大多数 常见平台（但不是所有平台）的汇编代码，尽管它基本上是透明的，但也不是完全透明的; 要让Python使用它，需要使用补丁。)

当Python嵌入到其他应用程序中时，传统的GC也成为一个问题。在独立的Python中，可以用GC库提供的版本替换标准的malloc()和free()，嵌入Python的应用程序可能希望用 它自己 替代malloc()和free()，而可能不需要Python的。现在，CPython可以正确地实现malloc()和free()。

CPython退出时为什么不释放所有内存？

当Python退出时，从全局命名空间或Python模块引用的对象并不总是被释放。 如果存在循环引用，则可能发生这种情况 C库分配的某些内存也是不可能释放的（例如像Purify这样的工具会抱怨这些内容）。 但是，Python在退出时清理内存并尝试销毁每个对象。

如果要强制 Python 在释放时删除某些内容，请使用 atexit 模块运行一个函数，强制删除这些内容。

为什么有单独的元组和列表数据类型？

虽然列表和元组在许多方面是相似的，但它们的使用方式通常是完全不同的。可以认为元组类似于Pascal记录或C结构；它们是相关数据的小集合，可以是不同类型的数据，可以作为一个组进行操作。例如，笛卡尔坐标适当地表示为两个或三个数字的元组。

另一方面，列表更像其他语言中的数组。它们倾向于持有不同数量的对象，所有对象都具有相同的类型，并且逐个操作。例如， os.listdir('.') 返回表示当前目录中的文件的字符串列表。如果向目录中添加了一两个文件，对此输出进行操作的函数通常不会中断。

元组是不可变的，这意味着一旦创建了元组，就不能用新值替换它的任何元素。列表是可变的，这意味着您始终可以更改列表的元素。只有不变元素可以用作字典的key，因此只能将元组和非列表用作key。

列表是如何在CPython中实现的？

CPython的列表实际上是可变长度的数组，而不是lisp风格的链表。该实现使用对其他对象的引用的连续数组，并在列表头结构中保留指向该数组和数组长度的指针。

这使得索引列表 a[i] 的操作成本与列表的大小或索引的值无关。

当添加或插入项时，将调整引用数组的大小。并采用了一些巧妙的方法来提高重复添加项的性能; 当数组必须增长时，会分配一些额外的空间，以便在接下来的几次中不需要实际调整大小。

字典是如何在CPython中实现的？

CPython的字典实现为可调整大小的哈希表。与B-树相比，这在大多数情况下为查找（目前最常见的操作）提供了更好的性能，并且实现更简单。

Dictionaries work by computing a hash code for each key stored in the dictionary using the hash() built-in function. The hash code varies widely depending on the key and a per-process seed; for example, “Python” could hash to -539294296 while “python”, a string that differs by a single bit, could hash to 1142331976. The hash code is then used to calculate a location in an internal array where the value will be stored. Assuming that you’re storing keys that all have different hash values, this means that dictionaries take constant time – O(1), in computer science notation – to retrieve a key. It also means that no sorted order of the keys is maintained, and traversing the array as the .keys() and .items() do will output the dictionary’s content in some arbitrary jumbled order that can change with every invocation of a program.

为什么字典key必须是不可变的？

字典的哈希表实现使用从键值计算的哈希值来查找键。如果键是可变对象，则其值可能会发生变化，因此其哈希值也会发生变化。但是，由于无论谁更改键对象都无法判断它是否被用作字典键值，因此无法在字典中修改条目。然后，当你尝试在字典中查找相同的对象时，将无法找到它，因为其哈希值不同。如果你尝试查找旧值，也不会找到它，因为在该哈希表中找到的对象的值会有所不同。

如果你想要一个用列表索引的字典，只需先将列表转换为元组；用函数 tuple(L) 创建一个元组，其条目与列表 L 相同。 元组是不可变的，因此可以用作字典键。

已经提出的一些不可接受的解决方案：

哈希按其地址（对象ID）列出。这不起作用，因为如果你构造一个具有相同值的新列表，它将无法找到；例如:

mydict = {[1, 2]: '12'}
print(mydict[[1, 2]])

would raise a KeyError exception because the id of the [1, 2] used in the second line differs from that in the first line. In other words, dictionary keys should be compared using ==, not using is.

使用列表作为键时进行复制。这没有用的，因为作为可变对象的列表可以包含对自身的引用，然后复制代码将进入无限循环。

允许列表作为键，但告诉用户不要修改它们。当你意外忘记或修改列表时，这将产生程序中的一类难以跟踪的错误。它还使一个重要的字典不变量无效： d.keys() 中的每个值都可用作字典的键。

将列表用作字典键后，应标记为其只读。问题是，它不仅仅是可以改变其值的顶级对象；你可以使用包含列表作为键的元组。将任何内容作为键关联到字典中都需要将从那里可到达的所有对象标记为只读 —— 并且自引用对象可能会导致无限循环。

如果需要，可以使用以下方法来解决这个问题，但使用它需要你自担风险：你可以将一个可变结构包装在一个类实例中，该实例同时具有 __eq__() 和 __hash__() 方法。然后，你必须确保驻留在字典（或其他基于 hash 的结构）中的所有此类包装器对象的哈希值在对象位于字典（或其他结构）中时保持固定。:

class ListWrapper:
    def __init__(self, the_list):
        self.the_list = the_list

    def __eq__(self, other):
        return self.the_list == other.the_list

    def __hash__(self):
        l = self.the_list
        result = 98767 - len(l)*555
        for i, el in enumerate(l):
            try:
                result = result + (hash(el) % 9999999) * 1001 + i
            except Exception:
                result = (result % 7777777) + i * 333
        return result

注意，哈希计算由于列表的某些成员可能不可用以及算术溢出的可能性而变得复杂。

此外，必须始终如此，如果 o1 == o2 （即 o1.__eq__(o2) is True ）则 hash(o1) == hash(o2)``（即 ``o1.__hash__() == o2.__hash__() ），无论对象是否在字典中。 如果你不能满足这些限制，字典和其他基于 hash 的结构将会出错。

对于 ListWrapper ，只要包装器对象在字典中，包装列表就不能更改以避免异常。除非你准备好认真考虑需求以及不正确地满足这些需求的后果，否则不要这样做。请留意。

为什么 list.sort() 没有返回排序列表？

在性能很重要的情况下，仅仅为了排序而复制一份列表将是一种浪费。因此， list.sort() 对列表进行了适当的排序。为了提醒您这一事实，它不会返回已排序的列表。这样，当您需要排序的副本，但也需要保留未排序的版本时，就不会意外地覆盖列表。

如果要返回新列表，请使用内置 sorted() 函数。此函数从提供的可迭代列表中创建新列表，对其进行排序并返回。例如，下面是如何迭代遍历字典并按keys排序:

for key in sorted(mydict):
    ...  # do whatever with mydict[key]...

如何在Python中指定和实施接口规范？

由C++和Java等语言提供的模块接口规范描述了模块的方法和函数的原型。许多人认为接口规范的编译时强制执行有助于构建大型程序。

Python 2.6添加了一个 abc 模块，允许定义抽象基类 (ABCs)。然后可以使用 isinstance() 和 issubclass() 来检查实例或类是否实现了特定的ABC。 collections.abc 模块定义了一组有用的ABCs 例如 Iterable ， Container , 和 MutableMapping

对于Python，通过对组件进行适当的测试规程，可以获得接口规范的许多好处。还有一个工具PyChecker，可用于查找由于子类化引起的问题。

一个好的模块测试套件既可以提供回归测试，也可以作为模块接口规范和一组示例。许多Python模块可以作为脚本运行，以提供简单的“自我测试”。即使是使用复杂外部接口的模块，也常常可以使用外部接口的简单“桩代码（stub）”模拟进行隔离测试。可以使用 doctest 和 unittest 模块或第三方测试框架来构造详尽的测试套件，以运行模块中的每一行代码。

适当的测试规程可以帮助在Python中构建大型的、复杂的应用程序以及接口规范。事实上，它可能会更好，因为接口规范不能测试程序的某些属性。例如， append() 方法将向一些内部列表的末尾添加新元素；接口规范不能测试您的 append() 实现是否能够正确执行此操作，但是在测试套件中检查这个属性是很简单的。

编写测试套件非常有用，您可能希望设计代码时着眼于使其易于测试。一种日益流行的技术是面向测试的开发，它要求在编写任何实际代码之前，首先编写测试套件的各个部分。当然，Python允许您草率行事，根本不编写测试用例。

为什么没有goto？

可以使用异常捕获来提供 “goto结构” ，甚至可以跨函数调用工作的 。许多人认为异常捕获可以方便地模拟C，Fortran和其他语言的 “go” 或 “goto” 结构的所有合理用法。例如:

class label(Exception): pass  # declare a label

try:
    ...
    if condition: raise label()  # goto label
    ...
except label:  # where to goto
    pass
...

但是不允许你跳到循环的中间，这通常被认为是滥用goto。谨慎使用。

为什么原始字符串（r-strings）不能以反斜杠结尾？

更准确地说，它们不能以奇数个反斜杠结束：结尾处的不成对反斜杠会转义结束引号字符，留下未结束的字符串。

原始字符串的设计是为了方便想要执行自己的反斜杠转义处理的处理器(主要是正则表达式引擎)创建输入。此类处理器将不匹配的尾随反斜杠视为错误，因此原始字符串不允许这样做。反过来，允许通过使用引号字符转义反斜杠转义字符串。当r-string用于它们的预期目的时，这些规则工作的很好。

如果您正在尝试构建Windows路径名，请注意所有Windows系统调用都使用正斜杠:

f = open("/mydir/file.txt")  # works fine!

如果您正在尝试为DOS命令构建路径名，请尝试以下示例

dir = r"\this\is\my\dos\dir" "\\"
dir = r"\this\is\my\dos\dir\ "[:-1]
dir = "\\this\\is\\my\\dos\\dir\\"

为什么Python没有属性赋值的“with”语句？

Python有一个 ‘with’ 语句，它封装了块的执行，在块的入口和出口调用代码。有些语言的结构是这样的:

with obj:
    a = 1               # equivalent to obj.a = 1
    total = total + 1   # obj.total = obj.total + 1

在Python中，这样的结构是不明确的。

其他语言，如ObjectPascal、Delphi和C++ 使用静态类型，因此可以毫不含糊地知道分配给什么成员。这是静态类型的要点 – 编译器 总是 在编译时知道每个变量的作用域。

Python使用动态类型。事先不可能知道在运行时引用哪个属性。可以动态地在对象中添加或删除成员属性。这使得无法通过简单的阅读就知道引用的是什么属性：局部属性、全局属性还是成员属性？

例如，采用以下不完整的代码段:

def foo(a):
    with a:
        print(x)

该代码段假设 “a” 必须有一个名为 “x” 的成员属性。然而，Python中并没有告诉解释器这一点。假设 “a” 是整数，会发生什么？如果有一个名为 “x” 的全局变量，它是否会在with块中使用？如您所见，Python的动态特性使得这样的选择更加困难。

然而，Python 可以通过赋值轻松实现 “with” 和类似语言特性（减少代码量）的主要好处。代替:

function(args).mydict[index][index].a = 21
function(args).mydict[index][index].b = 42
function(args).mydict[index][index].c = 63

写成这样:

ref = function(args).mydict[index][index]
ref.a = 21
ref.b = 42
ref.c = 63

这也具有提高执行速度的副作用，因为Python在运行时解析名称绑定，而第二个版本只需要执行一次解析。

为什么 if/while/def/class语句需要冒号？

冒号主要用于增强可读性(ABC语言实验的结果之一)。考虑一下这个:

if a == b
    print(a)

与

if a == b:
    print(a)

注意第二种方法稍微容易一些。请进一步注意，在这个FAQ解答的示例中，冒号是如何设置的；这是英语中的标准用法。

另一个次要原因是冒号使带有语法突出显示的编辑器更容易工作；他们可以寻找冒号来决定何时需要增加缩进，而不必对程序文本进行更精细的解析。

为什么Python在列表和元组的末尾允许使用逗号？

Python 允许您在列表，元组和字典的末尾添加一个尾随逗号:

[1, 2, 3,]
('a', 'b', 'c',)
d = {
    "A": [1, 5],
    "B": [6, 7],  # last trailing comma is optional but good style
}

有几个理由允许这样做。

如果列表，元组或字典的字面值分布在多行中，则更容易添加更多元素，因为不必记住在上一行中添加逗号。这些行也可以重新排序，而不会产生语法错误。

不小心省略逗号会导致难以诊断的错误。例如:

x = [
  "fee",
  "fie"
  "foo",
  "fum"
]

这个列表看起来有四个元素，但实际上包含三个 : “fee”, “fiefoo” 和 “fum” 。总是加上逗号可以避免这个错误的来源。

允许尾随逗号也可以使编程代码更容易生成。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

代码库和插件 FAQ

目录

代码库和插件 FAQ

通用的代码库问题

如何找到可以用来做 XXX 的模块或应用？

math.py（socket.py，regex.py 等）的源文件在哪？

在 Unix 中怎样让 Python 脚本可执行？

Python 中有 curses/termcap 包吗？

Python 中存在类似 C 的 onexit() 函数的东西吗？

为什么我的信号处理函数不能工作？

通用任务

怎样测试 Python 程序或组件？

怎样用 docstring 创建文档？

怎样一次只获取一个按键？

线程相关

程序中怎样使用线程？

我的线程都没有运行，为什么？

如何将任务分配给多个工作线程？

怎样修改全局变量是线程安全的？

不能删除全局解释器锁吗？

输入输出

怎样删除文件？（以及其他文件相关的问题……）

怎样复制文件？

怎样读取（或写入）二进制数据？

似乎 os.popen() 创建的管道不能使用 os.read()，这是为什么？

怎样访问（RS232）串口？

为什么关闭 sys.stdout（stdin，stderr）并不会真正关掉它？

网络 / Internet 编程

Python 中的 WWW 工具是什么？

怎样模拟发送 CGI 表单（METHOD=POST）？

生成 HTML 需要使用什么模块？

怎样使用 Python 脚本发送邮件？

socket 的 connect() 方法怎样避免阻塞？

数据库

Python 中有数据库包的接口吗？

在 Python 中如何实现持久化对象？

数学和数字

Python 中怎样生成随机数？

通用的代码库问题

如何找到可以用来做 XXX 的模块或应用？

在 代码库参考 中查找是否有适合的标准库模块。（如果你已经了解标准库的内容，可以跳过这一步）

对于第三方软件包，请搜索 Python Package Index [https://pypi.org] 或是 Google [https://www.google.com] 等其他搜索引擎。用“Python”加上一两个你需要的关键字通常会找到有用的东西。

math.py（socket.py，regex.py 等）的源文件在哪？

如果找不到模块的源文件，可能它是一个内建的模块，或是使用 C，C++ 或其他编译型语言实现的动态加载模块。这种情况下可能是没有源码文件的，类似 mathmodule.c 这样的文件会存放在 C 代码目录中（但不在 Python 目录中）。

Python 中（至少）有三类模块：

使用 Python 编写的模块（.py)；

使用 C 编写的动态加载模块（.dll，.pyd，.so，.sl 等）；

使用 C 编写并链接到解释器的模块，要获取此列表，输入：

import sys
print(sys.builtin_module_names)

在 Unix 中怎样让 Python 脚本可执行？

你需要做两件事：文件必须是可执行的，并且第一行需要以 #! 开头，后面跟上 Python 解释器的路径。

第一点可以用执行 chmod +x scriptfile 或是 chmod 755 scriptfile 做到。

第二点有很多种做法，最直接的方式是：

#!/usr/local/bin/python

在文件第一行，使用你所在平台上的 Python 解释器的路径。

如果你希望脚本不依赖 Python 解释器的具体路径，你也可以使用 env 程序。假设你的 Python 解释器所在目录已经添加到了 PATH 环境变量中，几乎所有的类 Unix 系统都支持下面的写法：

#!/usr/bin/env python

不要 在 CGI 脚本中这样做。CGI 脚本的 PATH 环境变量通常会非常精简，所以你必须使用解释器的完整绝对路径。

有时候，用户的环境变量如果太长，可能会导致 /usr/bin/env 执行失败；又或者甚至根本就不存在 env 程序。在这种情况下，你可以尝试使用下面的 hack 方法（来自 Alex Rezinsky）：

#! /bin/sh
""":"
exec python $0 ${1+"$@"}
"""

这样做有一个小小的缺点，它会定义脚本的 __doc__ 字符串。不过可以这样修复：

__doc__ = """...Whatever..."""

Python 中有 curses/termcap 包吗？

对于类 Unix 系统：标准 Python 源码发行版会在 Modules [https://github.com/python/cpython/tree/3.6/Modules] 子目录中附带 curses 模块，但默认并不会编译。（注意：在 Windows 平台下不可用 —— Windows 中没有 curses 模块。）

curses 模块支持基本的 curses 特性，同时也支持 ncurses 和 SYSV curses 中的很多额外功能，比如颜色、不同的字符集支持、填充和鼠标支持。这意味着这个模块不兼容只有 BSD curses 模块的操作系统，但是目前仍在维护的系统应该都不会存在这种情况。

对于 Windows 平台：使用 consolelib 模块 [http://effbot.org/zone/console-index.htm].

Python 中存在类似 C 的 onexit() 函数的东西吗？

atexit 模块提供了一个与 C 的 onexit() 函数类似的注册函数。

为什么我的信号处理函数不能工作？

最常见的问题是信号处理函数没有正确定义参数列表。它会被这样调用：

handler(signum, frame)

因此函数应该定义两个参数：

def handler(signum, frame):
    ...

通用任务

怎样测试 Python 程序或组件？

Python 带有两个测试框架。doctest 模块从模块的 docstring 中寻找示例并执行，对比输出是否与 docstring 中给出的是否一致。

unittest 模块是一个模仿 Java 和 Smalltalk 测试框架的更棒的测试框架。

为了使测试更容易，你应该在程序中使用良好的模块化设计。程序中的绝大多数功能都应该用函数或类方法封装 —— 有时这样做会有额外惊喜，程序会运行得更快（因为局部变量比全局变量访问要快）。除此之外，程序应该避免依赖可变的局部变量，这会使得测试困难许多。

程序的“全局主逻辑”应该尽量简单：

if __name__ == "__main__":
    main_logic()

并放置在程序主模块的最后面。

一旦你的程序已经用函数和类完善地组织起来，你就应该编写测试函数来测试其行为。可以使用自动执行一系列测试函数的测试集与每个模块进行关联。听起来似乎需要大量的工作，但是因为 Python 非常简洁和灵活，所以实际上会相当简单。在编写“生产代码”的同时别忘了也要编写测试函数，你会发现编程会变得更愉快、更有趣，因为这样会使得发现 bug 和设计缺陷更加容易。

程序主模块之外的其他“辅助模块”中可以增加自测试的入口。

if __name__ == "__main__":
    self_test()

通过使用 Python 实现的“假”接口，即使是需要与复杂的外部接口交互的程序也可以在外部接口不可用时进行测试。

怎样用 docstring 创建文档？

pydoc 模块可以用 Python 源码中的 docstring 创建 HTML 文件。也可以使用 epydoc [http://epydoc.sourceforge.net/] 来只通过 docstring 创建 API 文档。Sphinx [http://sphinx-doc.org] 也可以引入 docstring 的内容。

怎样一次只获取一个按键？

在类 Unix 系统中有多种方案。最直接的方法是使用 curses，但是 curses 模块太大了，难以学习。

线程相关

程序中怎样使用线程？

一定要使用 threading 模块，不要使用 _thread 模块。threading 模块对 _thread 模块提供的底层线程原语做了更易用的抽象。

Aahz 的非常实用的 threading 教程中有一些幻灯片；可以参阅 http://www.pythoncraft.com/OSCON2001/ 。

我的线程都没有运行，为什么？

一旦主线程退出，所有的子线程都会被杀掉。你的主线程运行得太快了，子线程还没来得及工作。

简单的解决方法是在程序中加一个时间足够长的 sleep，让子线程能够完成运行。

import threading, time

def thread_task(name, n):
    for i in range(n):
        print(name, i)

for i in range(10):
    T = threading.Thread(target=thread_task, args=(str(i), i))
    T.start()

time.sleep(10)  # <---------------------------!

但目前（在许多平台上）线程不是并行运行的，而是按顺序依次执行！原因是系统线程调度器在前一个线程阻塞之前不会启动新线程。

简单的解决方法是在运行函数的开始处加一个时间很短的 sleep。

def thread_task(name, n):
    time.sleep(0.001)  # <--------------------!
    for i in range(n):
        print(name, i)

for i in range(10):
    T = threading.Thread(target=thread_task, args=(str(i), i))
    T.start()

time.sleep(10)

比起用 time.sleep() 猜一个合适的等待时间，使用信号量机制会更好些。有一个办法是使用 queue 模块创建一个 queue 对象，让每一个线程在运行结束时 append 一个令牌到 queue 对象中，主线程中从 queue 对象中读取与线程数量一致的令牌数量即可。

如何将任务分配给多个工作线程？

最简单的方法是使用新的 concurrent.futures 模块，尤其是其中的 ThreadPoolExecutor 类。

或者，如果你想更好地控制分发算法，你也可以自己写逻辑实现。使用 queue 模块来创建任务列表队列。Queue 类维护一个了一个存有对象的列表，提供了 .put(obj) 方法添加元素，并且可以用 .get() 方法获取元素。这个类会使用必要的加锁操作，以此确保每个任务只会执行一次。

这是一个简单的例子：

import threading, queue, time

# The worker thread gets jobs off the queue.  When the queue is empty, it
# assumes there will be no more work and exits.
# (Realistically workers will run until terminated.)
def worker():
    print('Running worker')
    time.sleep(0.1)
    while True:
        try:
            arg = q.get(block=False)
        except queue.Empty:
            print('Worker', threading.currentThread(), end=' ')
            print('queue empty')
            break
        else:
            print('Worker', threading.currentThread(), end=' ')
            print('running with argument', arg)
            time.sleep(0.5)

# Create queue
q = queue.Queue()

# Start a pool of 5 workers
for i in range(5):
    t = threading.Thread(target=worker, name='worker %i' % (i+1))
    t.start()

# Begin adding work to the queue
for i in range(50):
    q.put(i)

# Give threads time to run
print('Main thread sleeping')
time.sleep(5)

运行时会产生如下输出：

Running worker
Running worker
Running worker
Running worker
Running worker
Main thread sleeping
Worker <Thread(worker 1, started 130283832797456)> running with argument 0
Worker <Thread(worker 2, started 130283824404752)> running with argument 1
Worker <Thread(worker 3, started 130283816012048)> running with argument 2
Worker <Thread(worker 4, started 130283807619344)> running with argument 3
Worker <Thread(worker 5, started 130283799226640)> running with argument 4
Worker <Thread(worker 1, started 130283832797456)> running with argument 5
...

查看模块的文档以获取更多信息；Queue 类提供了多种接口。

怎样修改全局变量是线程安全的？

Python VM 内部会使用 global interpreter lock （GIL）来确保同一时间只有一个线程运行。通常 Python 只会在字节码指令之间切换线程；切换的频率可以通过设置 sys.setswitchinterval() 指定。从 Python 程序的角度来看，每一条字节码指令以及每一条指令对应的 C 代码实现都是原子的。

理论上说，具体的结果要看具体的 PVM 字节码实现对指令的解释。而实际上，对内建类型（int，list，dict 等）的共享变量的“类原子”操作都是原子的。

举例来说，下面的操作是原子的（L、L1、L2 是列表，D、D1、D2 是字典，x、y 是对象，i，j 是 int 变量）：

L.append(x)
L1.extend(L2)
x = L[i]
x = L.pop()
L1[i:j] = L2
L.sort()
x = y
x.field = y
D[x] = y
D1.update(D2)
D.keys()

这些不是原子的：

i = i+1
L.append(L[-1])
L[i] = L[j]
D[x] = D[x] + 1

覆盖其他对象的操作会在其他对象的引用计数变成 0 时触发其 __del__() 方法，这可能会产生一些影响。对字典和列表进行大量操作时尤其如此。如果有疑问的话，使用互斥锁！

不能删除全局解释器锁吗？

global interpreter lock （GIL）通常被视为 Python 在高端多核服务器上开发时的阻力，因为（几乎）所有 Python 代码只有在获取到 GIL 时才能运行，所以多线程的 Python 程序只能有效地使用一个 CPU。

Back in the days of Python 1.5, Greg Stein actually implemented a comprehensive patch set (the “free threading” patches) that removed the GIL and replaced it with fine-grained locking. Adam Olsen recently did a similar experiment in his python-safethread [http://code.google.com/p/python-safethread/] project. Unfortunately, both experiments exhibited a sharp drop in single-thread performance (at least 30% slower), due to the amount of fine-grained locking necessary to compensate for the removal of the GIL.

但这并意味着你不能在多核机器上很好地使用 Python！你只需将任务划分为多*进程*，而不是多*线程*。新的 concurrent.futures 模块中的 ProcessPoolExecutor 类提供了一个简单的方法；如果你想对任务分发做更多控制，可以使用 multiprocessing 模块提供的底层 API。

恰当地使用 C 拓展也很有用；使用 C 拓展处理耗时较久的任务时，拓展可以在线程执行 C 代码时释放 GIL，让其他线程执行。zlib 和 hashlib 等标准库模块已经这样做了。

也有建议说 GIL 应该是解释器状态锁，而不是完全的全局锁；解释器不应该共享对象。不幸的是，这也不可能发生。由于目前许多对象的实现都有全局的状态，因此这是一个艰巨的工作。举例来说，小整型数和短字符串会缓存起来，这些缓存将不得不移动到解释器状态中。其他对象类型都有自己的自由变量列表，这些自由变量列表也必须移动到解释器状态中。等等。

我甚至怀疑这些工作是否可能在优先的时间内完成，因为同样的问题在第三方拓展中也会存在。第三方拓展编写的速度可比你将它们转换为把全局状态存入解释器状态中的速度快得多。

最后，假设多个解释器不共享任何状态，那么这样做比每个进程一个解释器好在哪里呢？

输入输出

怎样删除文件？（以及其他文件相关的问题……）

使用 os.remove(filename) 或 os.unlink(filename)。查看 os 模块以获取更多文档。这两个函数是一样的，unlink() 是这个函数在 Unix 系统调用中的名字。

如果要删除目录，应该使用 os.rmdir()；使用 os.mkdir() 创建目录。os.makedirs(path) 会创建 path 中任何不存在的目录。os.removedirs(path) 则会删除其中的目录，只要它们都是空的；如果你想删除整个目录以及其中的内容，可以使用 shutil.rmtree()。

重命名文件可以使用 os.rename(old_path, new_path)。

如果需要截断文件，使用 f = open(filename, "rb+") 打开文件，然后使用 f.truncate(offset)；offset 默认是当前的搜索位置。也可以对使用 os.open() 打开的文件使用 os.ftruncate(fd, offset)，其中 fd 是文件描述符（一个小的整型数）。

shutil 模块也包含了一些处理文件的函数，包括 copyfile()，copytree() 和 rmtree()。

怎样复制文件？

shutil 模块有一个 copyfile() 函数。注意在 MacOS 9 中不会复制 resource fork 和 Finder info。

怎样读取（或写入）二进制数据？

要读写复杂的二进制数据格式，最好使用 struct 模块。该模块可以读取包含二进制数据（通常是数字）的字符串并转换为 Python 对象，反之亦然。

举例来说，下面的代码会从文件中以大端序格式读取一个 2 字节的整型和一个 4 字节的整型：

import struct

with open(filename, "rb") as f:
    s = f.read(8)
    x, y, z = struct.unpack(">hhl", s)

格式字符串中的 ‘>’ 强制以大端序读取数据；字母 ‘h’ 从字符串中读取一个“短整型”（2 字节），字母 ‘l’ 读取一个“长整型”（4 字节）。

对于更常规的数据（例如整型或浮点类型的列表），你也可以使用 array 模块。

注解

要读写二进制数据的话，需要强制以二进制模式打开文件（这里为 open() 函数传入 "rb"）。如果（默认）传入 "r" 的话，文件会以文本模式打开，f.read() 会返回 str 对象，而不是 bytes 对象。

似乎 os.popen() 创建的管道不能使用 os.read()，这是为什么？

os.read() 是一个底层函数，它接收的是文件描述符 —— 用小整型数表示的打开的文件。os.popen() 创建的是一个高级文件对象，和内建的 open() 方法返回的类型一样。因此，如果要从 os.popen() 创建的管道 p 中读取 n 个字节的话，你应该使用 p.read(n)。

怎样访问（RS232）串口？

对于 Win32，POSIX（Linux，BSD 等），Jython：

http://pyserial.sourceforge.net

对于 Unix，查看 Mitch Chapman 发布的帖子：

https://groups.google.com/groups?selm=34A04430.CF9@ohioee.com

为什么关闭 sys.stdout（stdin，stderr）并不会真正关掉它？

Python 文件对象 是一个对底层 C 文件描述符的高层抽象。

对于在 Python 中通过内建的 open() 函数创建的多数文件对象来说，f.close() 从 Python 的角度将其标记为已关闭，并且会关闭底层的 C 文件描述符。在 f 被垃圾回收的时候，析构函数中也会自动处理。

但由于 stdin，stdout 和 stderr 在 C 中的特殊地位，在 Python 中也会对它们做特殊处理。运行 sys.stdout.close() 会将 Python 的文件对象标记为已关闭，但是*不会*关闭与之关联的文件描述符。

要关闭这三者的 C 文件描述符的话，首先你应该确认确实需要关闭它（比如，这可能会影响到处理 I/O 的拓展）。如果确实需要这么做的话，使用 os.close()：

os.close(stdin.fileno())
os.close(stdout.fileno())
os.close(stderr.fileno())

或者也可以使用常量 0，1，2 代替。

网络 / Internet 编程

Python 中的 WWW 工具是什么？

参阅代码库参考手册中 互联网协议和支持 和 互联网数据处理 这两章的内容。Python 有大量模块来帮助你构建服务端和客户端 web 系统。

Paul Boddie 维护了一份可用框架的概览，见 https://wiki.python.org/moin/WebProgramming 。

Cameron Laird 维护了一份关于 Python web 技术的实用网页的集合，见 http://phaseit.net/claird/comp.lang.python/web_python 。

怎样模拟发送 CGI 表单（METHOD=POST）？

我需要通过 POST 表单获取网页，有什么代码能简单做到吗？

是的，这里有一个使用 urllib.request 的简单例子：

#!/usr/local/bin/python

import urllib.request

# build the query string
qs = "First=Josephine&MI=Q&Last=Public"

# connect and send the server a path
req = urllib.request.urlopen('http://www.some-server.out-there'
                             '/cgi-bin/some-cgi-script', data=qs)
with req:
    msg, hdrs = req.read(), req.info()

注意，通常在百分号编码的 POST 操作中，查询字符串必须使用 urllib.parse.urlencode() 处理一下。举个例子，如果要发送 name=Guy Steele, Jr. 的话：

>>> import urllib.parse
>>> urllib.parse.urlencode({'name': 'Guy Steele, Jr.'})
'name=Guy+Steele%2C+Jr.'

参见

查看 HOWTO 使用 urllib 包获取网络资源 获取更多示例。

生成 HTML 需要使用什么模块？

你可以在 Web 编程 wiki 页面 [https://wiki.python.org/moin/WebProgramming] 找到许多有用的链接。

怎样使用 Python 脚本发送邮件？

使用 smtplib 标准库模块。

下面是一个很简单的交互式发送邮件的代码。这个方法适用于任何支持 SMTP 协议的主机。

import sys, smtplib

fromaddr = input("From: ")
toaddrs  = input("To: ").split(',')
print("Enter message, end with ^D:")
msg = ''
while True:
    line = sys.stdin.readline()
    if not line:
        break
    msg += line

# The actual mail send
server = smtplib.SMTP('localhost')
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

在 Unix 系统中还可以使用 sendmail。sendmail 程序的位置在不同系统中不一样，有时是在 /usr/lib/sendmail，有时是在 /usr/sbin/sendmail。sendmail 手册页面会对你有所帮助。以下是示例代码：

import os

SENDMAIL = "/usr/sbin/sendmail"  # sendmail location
p = os.popen("%s -t -i" % SENDMAIL, "w")
p.write("To: receiver@example.com\n")
p.write("Subject: test\n")
p.write("\n")  # blank line separating headers from body
p.write("Some text\n")
p.write("some more text\n")
sts = p.close()
if sts != 0:
    print("Sendmail exit status", sts)

socket 的 connect() 方法怎样避免阻塞？

通常会用 select 模块处理 socket 异步 I/O。

要避免 TCP 连接阻塞，你可以设置将 socket 设置为非阻塞模式。此时当调用 connect() 时，要么连接会立刻建立好（几乎不可能），要么会收到一个包含了错误码 .error 的异常。errno.EINPROGRESS 表示连接正在进行，但还没有完成。不同的系统会返回不同的值，所以你需要确认你使用的系统会返回什么。

你可以使用 connect_ex() 方法来避免产生异常。这个方法只会返回错误码。如果需要轮询的话，你可以再次调用 connect_ex() —— 0 或 errno.EISCONN 表示连接已建立，或者你也可以用 select 检查这个 socket 是否可写。

注解

asyncore 模块提供了编写非阻塞网络代码框架性的方法。第三方的 Twisted [https://twistedmatrix.com/trac/] 库也很常用且功能强大。

数据库

Python 中有数据库包的接口吗？

有的。

标准 Python 还包含了基于磁盘的哈希接口例如 DBM 和 GDBM 。除此之外还有 sqlite3 模块，该模块提供了一个轻量级的基于磁盘的关系型数据库。

大多数关系型数据库都已经支持。查看 数据库编程 wiki 页面 [https://wiki.python.org/moin/DatabaseProgramming] 获取更多信息。

在 Python 中如何实现持久化对象？

pickle 库模块以一种非常通用的方式解决了这个问题（虽然你依然不能用它保存打开的文件、套接字或窗口之类的东西），此外 shelve 库模块可使用 pickle 和 (g)dbm 来创建包含任意 Python 对象的持久化映射。

数学和数字

Python 中怎样生成随机数？

random 标准库模块实现了随机数生成器，使用起来非常简单：

import random
random.random()

这个函数会返回 [0, 1) 之间的随机浮点数。

该模块中还有许多其他的专门的生成器，例如：

randrange(a, b) 返回 [a, b) 区间内的一个整型数。

uniform(a, b) 返回 [a, b) 区间之间的浮点数。

normalvariate(mean, sdev) 使用正态（高斯）分布采样。

还有一些高级函数直接对序列进行操作，例如：

choice(S) 从给定的序列中随机选择一个元素。

shuffle(L) 对列表进行原地重排，也就是说随机打乱。

还有 Random 类，你可以将其实例化，用来创建多个独立的随机数生成器。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

扩展/嵌入常见问题

目录

扩展/嵌入常见问题

可以使用C语言中创建自己的函数吗？

可以使用C++语言中创建自己的函数吗？

C很难写，有没有其他选择？

如何在 C 中执行任意 Python 语句？

如何在 C 中对任意 Python 表达式求值？

如何从Python对象中提取C的值？

如何使用Py_BuildValue()创建任意长度的元组？

如何从C调用对象的方法？

如何捕获PyErr_Print()（或打印到stdout / stderr的任何内容）的输出？

如何从C访问用Python编写的模块？

如何在 Python 中对接 C ++ 对象？

我使用Setup文件添加了一个模块，为什么make失败了？

如何调试扩展？

我想在Linux系统上编译一个Python模块，但是缺少一些文件。为什么?

如何区分“输入不完整”和“输入无效”？

如何找到未定义的g++符号__builtin_new或__pure_virtual？

能否创建一个对象类，其中部分方法在C中实现，而其他方法在Python中实现（例如通过继承）？

可以使用C语言中创建自己的函数吗？

是的，您可以在C中创建包含函数、变量、异常甚至新类型的内置模块。在文档 扩展和嵌入 Python 解释器 中有说明。

大多数中级或高级的Python书籍也涵盖这个主题。

可以使用C++语言中创建自己的函数吗？

是的，可以使用C ++中兼容C的功能。 在Python include文件周围放置` extern“C”{…}` ，并在Python解释器调用的每个函数之前放置 extern“C” 。 具有构造函数的全局或静态C ++对象可能不是一个好主意。

C很难写，有没有其他选择？

编写自己的C扩展有很多选择，具体取决于您要做的事情。

Cython [http://cython.org] 及其相关的 Pyrex [https://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/] 是接受稍微修改过的Python形式并生成相应C代码的编译器。 Cython和Pyrex可以编写扩展而无需学习Python的C API。

如果需要连接到某些当前不存在Python扩展的C或C ++库，可以尝试使用 SWIG [http://www.swig.org] 等工具包装库的数据类型和函数。 SIP [https://riverbankcomputing.com/software/sip/intro] ， CXX [http://cxx.sourceforge.net/] Boost [http://www.boost.org/libs/python/doc/index.html] , 或 Weave [https://github.com/scipy/weave] 也是包装C ++库的替代方案。

如何在 C 中执行任意 Python 语句？

The highest-level function to do this is PyRun_SimpleString() which takes a single string argument to be executed in the context of the module __main__ and returns 0 for success and -1 when an exception occurred (including SyntaxError). If you want more control, use PyRun_String(); see the source for PyRun_SimpleString() in Python/pythonrun.c.

如何在 C 中对任意 Python 表达式求值？

可以调用前一问题中介绍的函数 PyRun_String() 并附带起始标记符 Py_eval_input；它会解析表达式，对其求值并返回结果值。

如何从Python对象中提取C的值？

这取决于对象的类型。 如果是元组，PyTuple_Size() 可返回其长度而 PyTuple_GetItem() 可返回指定序号上的项。 对于列表也有类似的函数 PyListSize() 和 PyList_GetItem()。

对于字节串，PyBytes_Size() 可返回其长度而 PyBytes_AsStringAndSize() 提供一个指向其值和长度的指针。 请注意 Python 字节串可能为空，因此 C 的 strlen() 不应被使用。

To test the type of an object, first make sure it isn’t NULL, and then use PyBytes_Check(), PyTuple_Check(), PyList_Check(), etc.

还有一个针对 Python 对象的高层级 API，通过所谓的‘抽象’接口提供 —— 请参阅 Include/abstract.h 了解详情。 它允许使用 PySequence_Length(), PySequence_GetItem() 这样的调用来与任意种类的 Python 序列进行对接，此外还可使用许多其他有用的协议例如数字 (PyNumber_Index() 等) 以及 PyMapping API 中的各种映射等等。

如何使用Py_BuildValue()创建任意长度的元组？

不可以。应该使用 PyTuple_Pack() 。

如何从C调用对象的方法？

可以使用 PyObject_CallMethod() 函数来调用某个对象的任意方法。 形参为该对象、要调用的方法名、类似 Py_BuildValue() 所用的格式字符串以及要传给方法的参数值:

PyObject *
PyObject_CallMethod(PyObject *object, const char *method_name,
                    const char *arg_format, ...);

这适用于任何具有方法的对象 —— 不论是内置方法还是用户自定义方法。 你需要负责对返回值进行最终的 Py_DECREF() 处理。

例如调用某个文件对象的 “seek” 方法并传入参数 10, 0 (假定文件对象的指针为 “f”):

res = PyObject_CallMethod(f, "seek", "(ii)", 10, 0);
if (res == NULL) {
        ... an exception occurred ...
}
else {
        Py_DECREF(res);
}

请注意由于 PyObject_CallObject() 总是 接受一个元组作为参数列表，要调用不带参数的函数，则传入格式为 “()”，要调用只带一个参数的函数，则应将参数包含于圆括号中，例如 “(i)”。

如何捕获PyErr_Print()（或打印到stdout / stderr的任何内容）的输出？

在 Python 代码中，定义一个支持 write() 方法的对象。 将此对象赋值给 sys.stdout 和 sys.stderr。 调用 print_error 或者只是允许标准回溯机制生效。 在此之后，输出将转往你的 write() 方法所指向的任何地方。

做到这一点的最简单方式是使用 io.StringIO 类：

>>> import io, sys
>>> sys.stdout = io.StringIO()
>>> print('foo')
>>> print('hello world!')
>>> sys.stderr.write(sys.stdout.getvalue())
foo
hello world!

实现同样效果的自定义对象看起来是这样的：

>>> import io, sys
>>> class StdoutCatcher(io.TextIOBase):
...     def __init__(self):
...         self.data = []
...     def write(self, stuff):
...         self.data.append(stuff)
...
>>> import sys
>>> sys.stdout = StdoutCatcher()
>>> print('foo')
>>> print('hello world!')
>>> sys.stderr.write(''.join(sys.stdout.data))
foo
hello world!

如何从C访问用Python编写的模块？

你可以通过如下方式获得一个指向模块对象的指针:

module = PyImport_ImportModule("<modulename>");

如果模块尚未被导入（即它还不存在于 sys.modules 中），这会初始化该模块；否则它只是简单地返回 sys.modules["<modulename>"] 的值。 请注意它并不会将模块加入任何命名空间 —— 它只是确保模块被初始化并存在于 sys.modules 中。

之后你就可以通过如下方式来访问模块的属性（即模块中定义的任何名称）:

attr = PyObject_GetAttrString(module, "<attrname>");

调用 PyObject_SetAttrString() 为模块中的变量赋值也是可以的。

如何在 Python 中对接 C ++ 对象？

根据你的需求，可以选择许多方式。 手动的实现方式请查阅 “扩展与嵌入” 文档 来入门。 需要知道的是对于 Python 运行时系统来说，C 和 C++ 并不没有太大的区别 —— 因此围绕一个 C 结构（指针）类型构建新 Python 对象的策略同样适用于 C++ 对象。

有关C ++库，请参阅 C很难写，有没有其他选择？

我使用Setup文件添加了一个模块，为什么make失败了？

安装程序必须以换行符结束，如果没有换行符，则构建过程将失败。 （修复这个需要一些丑陋的shell脚本编程，而且这个bug很小，看起来不值得花这么大力气。)

如何调试扩展？

将GDB与动态加载的扩展名一起使用时，在加载扩展名之前，不能在扩展名中设置断点。

在您的 .gdbinit 文件中（或交互式）添加命令：

br _PyImport_LoadDynamicModule

然后运行GDB：

$ gdb /local/bin/python
gdb) run myscript.py
gdb) continue # repeat until your extension is loaded
gdb) finish   # so that your extension is loaded
gdb) br myfunction.c:50
gdb) continue

我想在Linux系统上编译一个Python模块，但是缺少一些文件。为什么?

大多数打包的Python版本不包含 /usr/lib/python2.x/config/ 目录，该目录中包含编译Python扩展所需的各种文件。

对于Red Hat，安装python-devel RPM以获取必要的文件。

对于Debian，运行 apt-get install python-dev 。

如何区分“输入不完整”和“输入无效”？

有时，希望模仿Python交互式解释器的行为，在输入不完整时(例如，您键入了“if”语句的开头，或者没有关闭括号或三个字符串引号)，给出一个延续提示，但当输入无效时，立即给出一条语法错误消息。

在Python中，您可以使用 codeop 模块，该模块非常接近解析器的行为。例如，IDLE就使用了这个。

在C中执行此操作的最简单方法是调用 PyRun_InteractiveLoop() （可能在单独的线程中）并让Python解释器为您处理输入。您还可以设置 PyOS_ReadlineFunctionPointer() 指向您的自定义输入函数。有关更多提示，请参阅 Modules/readline.c 和 Parser/myreadline.c 。

但是，有时必须在与其他应用程序相同的线程中运行嵌入式Python解释器，并且不能允许 PyRun_InteractiveLoop() 在等待用户输入时停止。那么另一个解决方案是调用 PyParser_ParseString() 并测试 e.error 等于 E_EOF ，如果等于，就意味着输入不完整。这是一个示例代码片段，未经测试，灵感来自Alex Farber的代码:

#include <Python.h>
#include <node.h>
#include <errcode.h>
#include <grammar.h>
#include <parsetok.h>
#include <compile.h>

int testcomplete(char *code)
  /* code should end in \n */
  /* return -1 for error, 0 for incomplete, 1 for complete */
{
  node *n;
  perrdetail e;

  n = PyParser_ParseString(code, &_PyParser_Grammar,
                           Py_file_input, &e);
  if (n == NULL) {
    if (e.error == E_EOF)
      return 0;
    return -1;
  }

  PyNode_Free(n);
  return 1;
}

另一个解决方案是尝试使用 Py_CompileString() 编译接收到的字符串。如果编译时没有出现错误，请尝试通过调用 PyEval_EvalCode() 来执行返回的代码对象。否则，请将输入保存到以后。如果编译失败，找出是错误还是只需要更多的输入-从异常元组中提取消息字符串，并将其与字符串 “分析时意外的EOF” 进行比较。下面是使用GNUreadline库的完整示例(您可能希望在调用readline()时忽略 SIGINT )：

#include <stdio.h>
#include <readline.h>

#include <Python.h>
#include <object.h>
#include <compile.h>
#include <eval.h>

int main (int argc, char* argv[])
{
  int i, j, done = 0;                          /* lengths of line, code */
  char ps1[] = ">>> ";
  char ps2[] = "... ";
  char *prompt = ps1;
  char *msg, *line, *code = NULL;
  PyObject *src, *glb, *loc;
  PyObject *exc, *val, *trb, *obj, *dum;

  Py_Initialize ();
  loc = PyDict_New ();
  glb = PyDict_New ();
  PyDict_SetItemString (glb, "__builtins__", PyEval_GetBuiltins ());

  while (!done)
  {
    line = readline (prompt);

    if (NULL == line)                          /* Ctrl-D pressed */
    {
      done = 1;
    }
    else
    {
      i = strlen (line);

      if (i > 0)
        add_history (line);                    /* save non-empty lines */

      if (NULL == code)                        /* nothing in code yet */
        j = 0;
      else
        j = strlen (code);

      code = realloc (code, i + j + 2);
      if (NULL == code)                        /* out of memory */
        exit (1);

      if (0 == j)                              /* code was empty, so */
        code[0] = '\0';                        /* keep strncat happy */

      strncat (code, line, i);                 /* append line to code */
      code[i + j] = '\n';                      /* append '\n' to code */
      code[i + j + 1] = '\0';

      src = Py_CompileString (code, "<stdin>", Py_single_input);

      if (NULL != src)                         /* compiled just fine - */
      {
        if (ps1  == prompt ||                  /* ">>> " or */
            '\n' == code[i + j - 1])           /* "... " and double '\n' */
        {                                               /* so execute it */
          dum = PyEval_EvalCode (src, glb, loc);
          Py_XDECREF (dum);
          Py_XDECREF (src);
          free (code);
          code = NULL;
          if (PyErr_Occurred ())
            PyErr_Print ();
          prompt = ps1;
        }
      }                                        /* syntax error or E_EOF? */
      else if (PyErr_ExceptionMatches (PyExc_SyntaxError))
      {
        PyErr_Fetch (&exc, &val, &trb);        /* clears exception! */

        if (PyArg_ParseTuple (val, "sO", &msg, &obj) &&
            !strcmp (msg, "unexpected EOF while parsing")) /* E_EOF */
        {
          Py_XDECREF (exc);
          Py_XDECREF (val);
          Py_XDECREF (trb);
          prompt = ps2;
        }
        else                                   /* some other syntax error */
        {
          PyErr_Restore (exc, val, trb);
          PyErr_Print ();
          free (code);
          code = NULL;
          prompt = ps1;
        }
      }
      else                                     /* some non-syntax error */
      {
        PyErr_Print ();
        free (code);
        code = NULL;
        prompt = ps1;
      }

      free (line);
    }
  }

  Py_XDECREF(glb);
  Py_XDECREF(loc);
  Py_Finalize();
  exit(0);
}

如何找到未定义的g++符号__builtin_new或__pure_virtual？

要动态加载g ++扩展模块，必须重新编译Python，要使用g ++重新链接（在Python Modules Makefile中更改LINKCC），及链接扩展模块（例如： g++ -shared -o mymodule.so mymodule.o ）。

能否创建一个对象类，其中部分方法在C中实现，而其他方法在Python中实现（例如通过继承）？

是的，您可以继承内置类，例如 int ， list ， dict 等。

Boost Python库（BPL，http：//www.boost.org/libs/python/doc/index.html）提供了一种从C ++执行此操作的方法（即，您可以使用BPL继承自C ++编写的扩展类 ）。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

Python在Windows上的常见问题

目录

Python在Windows上的常见问题

我怎样在Windows下运行一个Python程序？

我怎么让 Python 脚本可执行？

为什么有时候 Python 程序会启动缓慢？

我怎样使用 Python 脚本制作可执行文件？

*.pyd 文件和 DLL 文件相同吗？

我怎样将 Python 嵌入一个 Windows 程序？

如何让编辑器不要在我的 Python 源代码中插入 tab ？

如何在不阻塞的情况下检查按键？

我怎样在Windows下运行一个Python程序？

这不一定是一个简单的问题。如果你已经熟悉在Windows的命令行中运行程序的方法，一切都显而易见；不然的话，你也许需要额外获得些许指导。

除非你在使用某种集成开发环境，否则你将会在被称为“DOS 窗口”或“命令提示符窗口”的地方 输入 Windows 命令。 通常你可以在搜索栏搜索 cmd 来创建这种窗口。 你应该能够识别你何时打开了这样的窗口，因为你将看到一个 Windows“命令提示符”，通常看起来是这样：

C:\>

前面的字母可能会不同，而且后面有可能会有其他东西，所以你也许会看到类似这样的东西：

D:\YourName\Projects\Python>

出现的内容具体取决与你的电脑如何设置和最近用它做的事。 当你启动了这样一个窗口后，就可以开始运行Python程序了。

Python 脚本需要被另外一个叫做 Python 解释器 的程序来处理。 解释器读取脚本，把它编译成字节码，然后执行字节码来运行你的程序。 所以怎样安排解释器来处理你的 Python 脚本呢？

首先，确保命令窗口能够将“py”识别为指令来开启解释器。 如果你打开过一个命令窗口， 尝试输入命令 py 然后按回车：

C:\Users\YourName> py

然后你应当看见类似类似这样的东西：

Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

解释器已经以“交互模式”打开。这意味着你可以交互输入Python语句或表达式，并在等待时执行或评估它们。这是Python最强大的功能之一。输入几个表达式并看看结果：

>>> print("Hello")
Hello
>>> "Hello" * 3
'HelloHelloHello'

许多人把交互模式当作方便和高度可编程的计算器。 想结束交互式Python会话时，调用 exit() 函数，或者按住 Ctrl 键时输入 Z ，之后按 Enter 键返回Windows命令提示符。

你可能发现在开始菜单有这样一个条目 开始 ‣ 所有程序 ‣ Python 3.x ‣ Python (命令行)， 运行它后会出现一个有着 >>> 提示的新窗口。 在此之后，如果调用 exit() 函数或按 Ctrl-Z 组合键后窗口将会消失。 Windows 会在这个窗口中运行一个“python”命令，并且在你终止解释器的时候关闭它。

现在我们知道 py 命令已经被识别，可以输入 Python 脚本了。 你需要提供 Python 脚本的绝对路径或相对路径。 假设 Python 脚本位于桌面上并命名为 hello.py，并且命令提示符在用户主目录打开，那么可以看到类似于这样的东西:

C:\Users\YourName>

那么现在可以让 py 命令执行你的脚本，只需要输入 py 和脚本路径:

C:\Users\YourName> py Desktop\hello.py
hello

我怎么让 Python 脚本可执行？

在 Windows 上，标准 Python 安装程序已将 .py 扩展名与文件类型 (Python.File) 相关联，并为该文件类型提供运行解释器的打开命令 (D:\Program Files\Python\python.exe "%1" %*) 。 这足以使脚本在命令提示符下作为“foo.py”命令被执行。 如果希望通过简单地键入“foo”而无需输入文件扩展名来执行脚本，则需要将 .py 添加到 PATHEXT 环境变量中。

为什么有时候 Python 程序会启动缓慢？

通常，Python 在 Windows 上启动得很快，但偶尔会有错误报告说 Python 突然需要很长时间才能启动。更令人费解的是，在其他配置相同的 Windows 系统上，Python 却可以工作得很好。

该问题可能是由于计算机上的杀毒软件配置错误造成的。当将病毒扫描配置为监视文件系统中所有读取行为时，一些杀毒扫描程序会导致两个数量级的启动开销。请检查你系统安装的杀毒扫描程序的配置，确保两台机它们是同样的配置。已知的， McAfee 杀毒软件在将它设置为扫描所有文件系统访问时，会产生这个问题。

我怎样使用 Python 脚本制作可执行文件？

See http://cx-freeze.sourceforge.net/ for a distutils extension that allows you to create console and GUI executables from Python code. py2exe [http://www.py2exe.org/], the most popular extension for building Python 2.x-based executables, does not yet support Python 3 but a version that does is in development.

*.pyd 文件和 DLL 文件相同吗？

是的， .pyd 文件也是 dll ，但有一些差异。如果你有一个名为 foo.pyd 的DLL，那么它必须有一个函数 PyInit_foo() 。 然后你可以编写 Python 代码 “import foo” ，Python 将搜索 foo.pyd （以及 foo.py 、 foo.pyc ）。如果找到它，将尝试调用 PyInit_foo() 来初始化它。你不应将 .exe 与 foo.lib 链接，因为这会导致 Windows 要求存在 DLL 。

请注意， foo.pyd 的搜索路径是 PYTHONPATH ，与 Windows 用于搜索 foo.dll 的路径不同。此外， foo.pyd 不需要存在来运行你的程序，而如果你将程序与 dll 链接，则需要 dll 。 当然，如果你想 import foo ，则需要 foo.pyd 。在 DLL 中，链接在源代码中用 __declspec(dllexport) 声明。 在 .pyd 中，链接在可用函数列表中定义。

我怎样将 Python 嵌入一个 Windows 程序？

在 Windows 应用程序中嵌入 Python 解释器可以总结如下：

请 _不要_ 直接在你的 .exe 文件中内置 Python 。在 Windows 上， Python 必须是一个 DLL ，这样才可以处理导入的本身就是 DLL 的模块。（这是第一个未记录的关键事实。）相反，链接到 pythonNN.dll ；它通常安装在 C:\Windows\System 中。 NN 是 Python 版本，如数字“33”代表 Python 3.3 。

你可以通过两种不同的方式链接到 Python 。加载时链接意味着链接到 pythonNN.lib ，而运行时链接意味着链接 pythonNN.dll 。（一般说明： python NN.lib 是所谓的“import lib”，对应于 pythonNN.dll 。它只定义了链接器的符号。）

运行时链接极大地简化了链接选项，一切都在运行时发生。你的代码必须使用 Windows 的 LoadLibraryEx() 程序加载 pythonNN.dll 。代码还必须使用使用 Windows 的 GetProcAddress() 例程获得的指针访问 pythonNN.dll 中程序和数据（即 Python 的 C API ）。宏可以使这些指针对任何调用 Python C API 中的例程的 C 代码都是透明的。

Borland 提示：首先使用 Coff2Omf.exe 将 pythonNN.lib 转换为 OMF 格式。

如果你使用 SWIG ，很容易创建一个 Python “扩展模块”，它将使应用程序的数据和方法可供 Python 使用。SWIG将为你处理所有蹩脚的细节。结果是你将链接到 .exe 文件 中 的C代码 (!) 你不必创建 DLL 文件，这也简化了链接。

SWIG 将创建一个 init 函数（一个 C 函数），其名称取决于扩展模块的名称。例如，如果模块的名称是 leo ，则 init 函数将被称为 initleo() 。 如果您使用 SWIG 阴影类，则 init 函数将被称为 initleoc() 。这初始化了一个由阴影类使用的隐藏辅助类。

你可以将步骤 2 中的 C 代码链接到 .exe 文件的原因是调用初始化函数等同于将模块导入 Python ！ （这是第二个关键的未记载事实。）

简而言之，你可以用以下代码使用扩展模块初始化 Python 解释器。

#include "python.h"
...
Py_Initialize();  // Initialize Python.
initmyAppc();  // Initialize (import) the helper class.
PyRun_SimpleString("import myApp");  // Import the shadow class.

Python C API 存在两个问题，如果你使用除 MSVC 之外的编译器用于构建 python.dll ，这将会变得明显。

问题1：采用 FILE* 参数的所谓“极高级”函数在多编译器环境中不起作用，因为每个编译器的FILE结构体概念都不同。从实现的角度来看，这些是非常 _低_ 级的功能。

问题2：在为 void 函数生成包装器时，SWIG 会生成以下代码：

Py_INCREF(Py_None);
_resultobj = Py_None;
return _resultobj;

Py_None 是一个宏，它扩展为对 pythonNN.dll 中名为 _Py_NoneStruct 的复杂数据结构的引用。同样，此代码将在多编译器环境中失败。将此类代码替换为：

return Py_BuildValue("");

有可能使用 SWIG 的 %typemap 命令自动进行更改，但我无法使其工作（我是一个完全的SWIG新手）。

使用 Python shell 脚本从 Windows 应用程序内部建立 Python 解释器窗口并不是一个好主意；生成的窗口将独立于应用程序的窗口系统。相反，你（或 wxPythonWindow 类）应该创建一个“本机”解释器窗口。将该窗口连接到Python解释器很容易。你可以将 Python的 i/o 重定向到支持读写的 _任意_ 对象，因此你只需要一个包含 read() 和 write() 方法的 Python 对象（在扩展模块中定义）。

如何让编辑器不要在我的 Python 源代码中插入 tab ？

本 FAQ 不建议使用制表符， Python 样式指南 PEP 8 [https://www.python.org/dev/peps/pep-0008] ，为发行的 Python 代码推荐 4 个空格；这也是 Emacs python-mode 默认值。

在任何编辑器下，混合制表符和空格都是一个坏主意。 MSVC 在这方面没有什么不同，并且很容易配置为使用空格： 点击 Tools ‣ Options ‣ Tabs，对于文件类型“Default”，设置“Tab size”和“Indent size”为 4 ，并选择“插入空格”单选按钮。

如果混合制表符和空格导致前导空格出现问题， Python 会引发 IndentationError 或 TabError 。你还可以运行 tabnanny 模块以批处理模式检查目录树。

如何在不阻塞的情况下检查按键？

使用 msvcrt 模块。 是标准的 Windows 特定扩展模块。它定义了一个函数 kbhit() 来检查是否存在键盘命中，而 getch() 来获取一个字符而不回显它。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

图形用户界面（GUI）常见问题

目录

图形用户界面（GUI）常见问题

图形界面常见问题

Python 是否有平台无关的图形界面工具包？

Tkinter

wxWidgets

Qt

Gtk+

Kivy

FLTK

OpenGL

有哪些Python的GUI工具是某个平台专用的？

有关Tkinter的问题

我怎样“冻结”Tkinter程序？

在等待 I/O 操作时能够处理 Tk 事件吗？

在Tkinter中键绑定不工作：为什么？

图形界面常见问题

Python 是否有平台无关的图形界面工具包？

针对不同的（操作系统或）平台，有多种工具包可供选择。虽然有些工具包还没有移植到 Python 3 上，但至少目前 Tkinter 以及 Qt 是兼容 Python 3 的。

Tkinter

Python 的标准编译包含了 tkinter。这是一个面向对象的接口，指向 Tcl/Tk 微件包。 该接口大概是最容易安装（因为该接口包含在 Python 的大部分 二进制发行版 [https://www.python.org/downloads/] 中）和使用的工具包。要了解 Tk 的详情，比如源代码等，可访问 Tcl/Tk 项目主页 [https://www.tcl.tk]。 Tcl/Tk 可完整移植至 Mac OS X，Windows 和 Unix 操作系统上。

wxWidgets

wxWidgets (https://www.wxwidgets.org) 是一个自由、可移植的 GUI 图形用户界面类库，使用 C++ 编写。它可以在多个操作系统平台上提供原生自然的界面观感。包括Windows、Mac OS X、 GTK 和 X11 平台在内，都是wxWidgets 当前稳定支持的平台。在语言绑定适配方面，wxWidgets 类库可用于较多语言，包括 Python，Perl，Ruby等。

wxPython (http://www.wxpython.org) is the Python binding for wxwidgets. While it often lags slightly behind the official wxWidgets releases, it also offers a number of features via pure Python extensions that are not available in other language bindings. There is an active wxPython user and developer community.

wxWidgets 和 wxPython 都是自由开源库。宽松的许可证允许人们在商业软件、自由软件和共享软件中使用它们。

Qt

Qt 工具包 (可使用 PyQt [https://riverbankcomputing.com/software/pyqt/intro] 或 PySide [https://wiki.qt.io/PySide]) 及 KDE (PyKDE4 [https://techbase.kde.org/Languages/Python/Using_PyKDE_4]) 有多个绑定适配可供选择。 PyQt 当前相较 PySide 更成熟，但如果你想编写专有软件，就必须要从 Riverbank Computing [https://www.riverbankcomputing.com/commercial/license-faq] 购买 PyQt 许可证。 PySide 则可以自由使用于各类软件。

Qt 4.5 以上版本使用 LGPL 进行许可；此外，商业许可证可从 Qt 公司 [https://www.qt.io/licensing/] 那里获得。

Gtk+

The GObject introspection bindings [https://wiki.gnome.org/Projects/PyGObject] for Python allow you to write GTK+ 3 applications. There is also a Python GTK+ 3 Tutorial [https://python-gtk-3-tutorial.readthedocs.org/en/latest/].

The older PyGtk bindings for the Gtk+ 2 toolkit [http://www.gtk.org] have been implemented by James Henstridge; see <http://www.pygtk.org>.

Kivy

Kivy [https://kivy.org/] 是一种跨平台图形用户界面库，同时支持桌面操作系统（Windows，macOS 和 Linux）以及移动设备（Android，iOS）。该库使用 Python 和 Cython 编写，可以使用一系列窗口后端。

Kivy 是自由的开源软件，使用 MIT 许可证分发。

FLTK

the FLTK toolkit [http://www.fltk.org] 的Python绑定是简单却功能强大且成熟的跨平台窗口系统，可以在 the PyFLTK project [http://pyfltk.sourceforge.net] 里获得相关信息。

OpenGL

对于OpenGL绑定，请参阅 PyOpenGL [http://pyopengl.sourceforge.net]。

有哪些Python的GUI工具是某个平台专用的？

By installing the PyObjc Objective-C bridge [https://pythonhosted.org/pyobjc/], Python programs can use Mac OS X’s Cocoa libraries.

Mark Hammond的 Pythonwin 包括一个微软基础类(MFC)的接口和一个绝大多数由使用MFC类的Python写成的Python编程环境。

有关Tkinter的问题

我怎样“冻结”Tkinter程序？

Freeze （意为 “冻结”）是一个用来创建独立应用程序的工具。 当 “冻结” Tkinter 程序时，程序并不是真的能够独立运行，因为程序仍然需要 Tcl 和 Tk 库。

一种解决方法是将程序与 Tcl 和 Tk 库一同发布，并且在运行时使用环境变量 TCL_LIBRARY 和 TK_LIBRARY 指向他们的位置。

为了获得真正能独立运行的应用程序，来自库里的 Tcl 脚本也需要被整合进应用程序。 一个做这种事情的工具叫 SAM (stand-alone modules，独立模块) ，它是 Tix distribution (http://tix.sourceforge.net/) 的一部分。

在启用 SAM 时编译 Tix ，在 Python 文件 Modules/tkappinit.c 中执行对 Tclsam_init() 等的适当调用，并且将程序与 libtclsam 和 libtksam 相链接（可能也要包括 Tix 的库）。

在等待 I/O 操作时能够处理 Tk 事件吗？

在 Windows 以外的其他平台上可以，你甚至不需要使用线程！ 但是你必须稍微修改一下你的 I/O 代码。 Tk 有与 Xt 的 XtAddInput() 对应的调用，它允许你注册一个回调函数，当一个文件描述符可以进行 I/O 操作的时候，Tk 主循环将会调用这个回调函数。 参见 File Handlers。

在Tkinter中键绑定不工作：为什么？

经常听到的抱怨是：已经通过 bind() 方法绑定了事件的处理程序，但是，当按下相关的按键后，这个处理程序却没有执行。

最常见的原因是，那个绑定的控件没有“键盘焦点”。请在 Tk 文档中查找 focus 指令。通常一个控件要获得“键盘焦点”，需要点击那个控件（而不是标签；请查看 takefocus 选项）。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

“为什么我的电脑上安装了 Python ？”

什么是 Python？

Python 是一种程序语言，被许多应用程序使用。它不仅因易学而在许多高校用于编程入门，还被工作于 Google、NASA 和卢卡斯影业等公司的软件开发人员使用。

如果你想学习更多 Python，看看 Beginner’s Guide to Python [https://wiki.python.org/moin/BeginnersGuide].

为什么我的电脑上安装了 Python ？

如果你不记得你曾主动安装过 Python，但它却出现在了你的电脑上，这里有一些可能的原因。

可能是这台电脑的其他用户因想学习编程而安装了它，你得琢磨一下谁用过这台电脑并安装了 Python。

电脑上安装的第三方应用程序可能由 Python 写成并附带了一份 Python。这样的应用程序有很多，例如GUI程序、网络服务器、管理脚本等。

一些 Windows 可能预装了 Python。在撰写本文时，我们了解到 Hewlett-Packard 和 Compaq 的计算机包含Python。显然，HP/Compaq 的一些管理工具是用 Python 编写的。

许多 Unix 兼容的操作系统，例如 Mac OS X 和一些 Linux 发行版，默认安装了Python；它被包含在基本安装套件中。

我能删除 Python 吗？

这取决于所安装 Python 的来源

如果有人主动安装了 Python，你可以在不影响其它程序的情况下安全移除它。在 Windows 中，可使用“控制面板”中的“添加/删除程序”卸载。

如果 Python 来源于第三方应用程序，你也能删除它，但那些程序将不能正常工作。你应该使用那些应用程序的卸载器而不是直接删除 Python。

如果 Python 来自于你的操作系统，不推荐删除！如果删除了它，任何用 Python 写成的工具将无法工作，其中某些工具对于你来说可能十分重要。你甚至可能需要重装整个系统来修复因删除 Python 留下的烂摊子。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

Python 常见问题 »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

术语对照表

>>>

交互式终端中默认的 Python 提示符。往往会显示于能以交互方式在解释器里执行的样例代码之前。

...

The default Python prompt of the interactive shell when entering code for an indented code block, when within a pair of matching left and right delimiters (parentheses, square brackets, curly braces or triple quotes), or after specifying a decorator.

2to3

一个将 Python 2.x 代码转换为 Python 3.x 代码的工具，能够处理大部分通过解析源码并遍历解析树可检测到的不兼容问题。

2to3 包含在标准库中，模块名为 lib2to3；并提供一个独立入口点 Tools/scripts/2to3。参见 2to3 - 自动将 Python 2 代码转为 Python 3 代码。

abstract base class – 抽象基类

抽象基类简称 ABC，是对 duck-typing 的补充，它提供了一种定义接口的新方式，相比之下其他技巧例如 hasattr() 显得过于笨拙或有微妙错误（例如使用 魔术方法）。ABC 引入了虚拟子类，这种类并非继承自其他类，但却仍能被 isinstance() 和 issubclass() 所认可；详见 abc 模块文档。Python 自带许多内置的 ABC 用于实现数据结构（在 collections.abc 模块中）、数字（在 numbers 模块中）、流（在 io 模块中）、导入查找器和加载器（在 importlib.abc 模块中）。你可以使用 abc 模块来创建自己的 ABC。

annotation – 标注

关联到某个变量、类属性、函数形参或返回值的标签，被约定作为 type hint 来使用。

局部变量的标注在运行时不可访问，但全局变量、类属性和函数的标注会分别存放模块、类和函数的 __annotations__ 特殊属性中。

参见 variable annotation、function annotation、PEP 484 [https://www.python.org/dev/peps/pep-0484] 和 PEP 526 [https://www.python.org/dev/peps/pep-0526]，对此功能均有介绍。

argument – 参数

在调用函数时传给 function （或 method ）的值。参数分为两种：

关键字参数: 在函数调用中前面带有标识符（例如 name=）或者作为包含在前面带有 ** 的字典里的值传入。举例来说，3 和 5 在以下对 complex() 的调用中均属于关键字参数:

complex(real=3, imag=5)
complex(**{'real': 3, 'imag': 5})

位置参数: 不属于关键字参数的参数。位置参数可出现于参数列表的开头以及/或者作为前面带有 * 的 iterable 里的元素被传入。举例来说，3 和 5 在以下调用中均属于位置参数:

complex(3, 5)
complex(*(3, 5))

参数会被赋值给函数体中对应的局部变量。有关赋值规则参见 调用 一节。根据语法，任何表达式都可用来表示一个参数；最终算出的值会被赋给对应的局部变量。

另参见 parameter 术语表条目，常见问题中 参数与形参的区别 以及 PEP 362 [https://www.python.org/dev/peps/pep-0362]。

asynchronous context manager – 异步上下文管理器

此种对象通过定义 __aenter__() 和 __aexit__() 方法来对 async with 语句中的环境进行控制。由 PEP 492 [https://www.python.org/dev/peps/pep-0492] 引入。

asynchronous generator – 异步生成器

返回值为 asynchronous generator iterator 的函数。它与使用 async def 定义的协程函数很相似，不同之处在于它包含 yield 表达式以产生一系列可在 async for 循环中使用的值。

此术语通常是指异步生成器函数，但在某些情况下则可能是指 异步生成器迭代器。如果需要清楚表达具体含义，请使用全称以避免歧义。

一个异步生成器函数可能包含 await 表达式或者 async for 以及 async with 语句。

asynchronous generator iterator – 异步生成器迭代器

asynchronous generator 函数所创建的对象。

此对象属于 asynchronous iterator，当使用 __anext__() 方法调用时会返回一个可等待对象来执行异步生成器函数的代码直到下一个 yield 表达式。

每个 yield 会临时暂停处理，记住当前位置执行状态 (包括局部变量和挂起的 try 语句)。当该 异步生成器迭代器 与其他 __anext__() 返回的可等待对象有效恢复时，它会从离开位置继续执行。参见 PEP 492 [https://www.python.org/dev/peps/pep-0492] 和 PEP 525 [https://www.python.org/dev/peps/pep-0525]。

asynchronous iterable – 异步可迭代对象

可在 async for 语句中被使用的对象。必须通过它的 __aiter__() 方法返回一个 asynchronous iterator。由 PEP 492 [https://www.python.org/dev/peps/pep-0492] 引入。

asynchronous iterator – 异步迭代器

实现了 __aiter__() 和 __anext__() 方法的对象。__anext__ 必须返回一个 awaitable 对象。async for 会处理异步迭代器的 __anext__() 方法所返回的可等待对象，直到其引发一个 StopAsyncIteration 异常。由 PEP 492 [https://www.python.org/dev/peps/pep-0492] 引入。

attribute – 属性

关联到一个对象的值，可以使用点号表达式通过其名称来引用。例如，如果一个对象 o 具有一个属性 a，就可以用 o.a 来引用它。

awaitable – 可等待对象

能在 await 表达式中使用的对象。可以是 coroutine 或是具有 __await__() 方法的对象。参见 PEP 492 [https://www.python.org/dev/peps/pep-0492]。

BDFL

Benevolent Dictator For Life, a.k.a. Guido van Rossum [https://www.python.org/~guido/], Python’s creator.

binary file – 二进制文件

file object 能够读写 字节类对象。二进制文件的例子包括以二进制模式（'rb', 'wb' or 'rb+'）打开的文件、sys.stdin.buffer、sys.stdout.buffer 以及 io.BytesIO 和 gzip.GzipFile 的实例。

另请参见 text file 了解能够读写 str 对象的文件对象。

bytes-like object – 字节类对象

支持 缓冲协议 并且能导出 C-contiguous 缓冲的对象。这包括所有 bytes、bytearray 和 array.array 对象，以及许多普通 memoryview 对象。字节类对象可在多种二进制数据操作中使用；这些操作包括压缩、保存为二进制文件以及通过套接字发送等。

某些操作需要可变的二进制数据。这种对象在文档中常被称为“可读写字节类对象”。可变缓冲对象的例子包括 bytearray 以及 bytearray 的 memoryview。其他操作要求二进制数据存放于不可变对象 (“只读字节类对象”)；这种对象的例子包括 bytes 以及 bytes 对象的 memoryview。

bytecode – 字节码

Python 源代码会被编译为字节码，即 CPython 解释器中表示 Python 程序的内部代码。字节码还会缓存在 .pyc 文件中，这样第二次执行同一文件时速度更快（可以免去将源码重新编译为字节码）。这种 “中间语言” 运行在根据字节码执行相应机器码的 virtual machine 之上。请注意不同 Python 虚拟机上的字节码不一定通用，也不一定能在不同 Python 版本上兼容。

字节码指令列表可以在 dis 模块 的文档中查看。

class – 类

用来创建用户定义对象的模板。类定义通常包含对该类的实例进行操作的方法定义。

class variable – 类变量

在类中定义的变量，并且仅限在类的层级上修改 (而不是在类的实例中修改)。

coercion – 强制类型转换

The implicit conversion of an instance of one type to another during an operation which involves two arguments of the same type. For example, int(3.15) converts the floating point number to the integer 3, but in 3+4.5, each argument is of a different type (one int, one float), and both must be converted to the same type before they can be added or it will raise a TypeError. Without coercion, all arguments of even compatible types would have to be normalized to the same value by the programmer, e.g., float(3)+4.5 rather than just 3+4.5.

complex number – 复数

对普通实数系统的扩展，其中所有数字都被表示为一个实部和一个虚部的和。虚数是虚数单位（-1 的平方根）的实倍数，通常在数学中写为 i，在工程学中写为 j。Python 内置了对复数的支持，采用工程学标记方式；虚部带有一个 j 后缀，例如 3+1j。如果需要 math 模块内对象的对应复数版本，请使用 cmath，复数的使用是一个比较高级的数学特性。如果你感觉没有必要，忽略它们也几乎不会有任何问题。

context manager – 上下文管理器

在 with 语句中使用，通过定义 __enter__() 和 __exit__() 方法来控制环境状态的对象。参见 PEP 343 [https://www.python.org/dev/peps/pep-0343]。

contiguous – 连续

一个缓冲如果是 C 连续 或 Fortran 连续 就会被认为是连续的。零维缓冲是 C 和 Fortran 连续的。在一维数组中，所有条目必须在内存中彼此相邻地排列，采用从零开始的递增索引顺序。在多维 C-连续数组中，当按内存地址排列时用最后一个索引访问条目时速度最快。但是在 Fortran 连续数组中则是用第一个索引最快。

coroutine – 协程

Coroutines is a more generalized form of subroutines. Subroutines are entered at one point and exited at another point. Coroutines can be entered, exited, and resumed at many different points. They can be implemented with the async def statement. See also PEP 492 [https://www.python.org/dev/peps/pep-0492].

coroutine function – 协程函数

返回一个 coroutine 对象的函数。协程函数可通过 async def 语句来定义，并可能包含 await、async for 和 async with 关键字。这些特性是由 PEP 492 [https://www.python.org/dev/peps/pep-0492] 引入的。

CPython

Python 编程语言的规范实现，在 python.org [https://www.python.org] 上发布。”CPython” 一词用于在必要时将此实现与其他实现例如 Jython 或 IronPython 相区别。

decorator – 装饰器

返回值为另一个函数的函数，通常使用 @wrapper 语法形式来进行函数变换。 装饰器的常见例子包括 classmethod() 和 staticmethod()。

装饰器语法只是一种语法糖，以下两个函数定义在语义上完全等价:

def f(...):
    ...
f = staticmethod(f)

@staticmethod
def f(...):
    ...

同的样概念也适用于类，但通常较少这样使用。有关装饰器的详情可参见 函数定义 和 类定义 的文档。

descriptor – 描述器

任何定义了 __get__(), __set__() 或 __delete__() 方法的对象。当一个类属性为描述器时，它的特殊绑定行为就会在属性查找时被触发。通常情况下，使用 a.b 来获取、设置或删除一个属性时会在 a 的类字典中查找名称为 b 的对象，但如果 b 是一个描述器，则会调用对应的描述器方法。理解描述器的概念是更深层次理解 Python 的关键，因为这是许多重要特性的基础，包括函数、方法、属性、类方法、静态方法以及对超类的引用等等。

有关描述符的方法的详情可参看 实现描述器。

dictionary – 字典

一个关联数组，其中的任意键都映射到相应的值。键可以是任何具有 __hash__() 和 __eq__() 方法的对象。在 Perl 语言中称为 hash。

dictionary view – 字典视图

从 dict.keys(), dict.values() 和 dict.items() 返回的对象被称为字典视图。它们提供了字典条目的一个动态视图，这意味着当字典改变时，视图也会相应改变。要将字典视图强制转换为真正的列表，可使用 list(dictview)。参见 字典视图对象。

docstring – 文档字符串

作为类、函数或模块之内的第一个表达式出现的字符串字面值。它在代码执行时会被忽略，但会被解释器识别并放入所在类、函数或模块的 __doc__ 属性中。由于它可用于代码内省，因此是对象存放文档的规范位置。

duck-typing – 鸭子类型

指一种编程风格，它并不依靠查找对象类型来确定其是否具有正确的接口，而是直接调用或使用其方法或属性（“看起来像鸭子，叫起来也像鸭子，那么肯定就是鸭子。”）由于强调接口而非特定类型，设计良好的代码可通过允许多态替代来提升灵活性。鸭子类型避免使用 type() 或 isinstance() 检测。(但要注意鸭子类型可以使用 抽象基类 作为补充。) 而往往会采用 hasattr() 检测或是 EAFP 编程。

EAFP

“求原谅比求许可更容易”的英文缩写。这种 Python 常用代码编写风格会假定所需的键或属性存在，并在假定错误时捕获异常。这种简洁快速风格的特点就是大量运用 try 和 except 语句。于其相对的则是所谓 LBYL 风格，常见于 C 等许多其他语言。

expression – 表达式

A piece of syntax which can be evaluated to some value. In other words, an expression is an accumulation of expression elements like literals, names, attribute access, operators or function calls which all return a value. In contrast to many other languages, not all language constructs are expressions. There are also statements which cannot be used as expressions, such as if. Assignments are also statements, not expressions.

extension module – 扩展模块

以 C 或 C++ 编写的模块，使用 Python 的 C API 来与语言核心以及用户代码进行交互。

f-string – f-字符串

带有 'f' 或 'F' 前缀的字符串字面值通常被称为“f-字符串”即 格式化字符串字面值 的简写。参见 PEP 498 [https://www.python.org/dev/peps/pep-0498]。

file object – 文件对象

对外提供面向文件 API 以使用下层资源的对象（带有 read() 或 write() 这样的方法）。根据其创建方式的不同，文件对象可以处理对真实磁盘文件，对其他类型存储，或是对通讯设备的访问（例如标准输入/输出、内存缓冲区、套接字、管道等等）。文件对象也被称为 文件类对象 或 流。

实际上共有三种类别的文件对象: 原始 二进制文件, 缓冲 二进制文件 以及 文本文件。它们的接口定义均在 io 模块中。创建文件对象的规范方式是使用 open() 函数。

file-like object – 文件类对象

file object 的同义词。

finder – 查找器

一种会尝试查找被导入模块的 loader 的对象。

从 Python 3.3 起存在两种类型的查找器: 元路径查找器 配合 sys.meta_path 使用，以及 path entry finders 配合 sys.path_hooks 使用。

更多详情可参见 PEP 302 [https://www.python.org/dev/peps/pep-0302], PEP 420 [https://www.python.org/dev/peps/pep-0420] 和 PEP 451 [https://www.python.org/dev/peps/pep-0451]。

floor division – 向下取整除法

向下舍入到最接近的整数的数学除法。向下取整除法的运算符是 // 。例如，表达式 11 // 4 的计算结果是 2 ，而与之相反的是浮点数的真正除法返回 2.75 。注意 (-11) // 4 会返回 -3 因为这是 -2.75 向下 舍入得到的结果。见 PEP 238 [https://www.python.org/dev/peps/pep-0238] 。

function – 函数

可以向调用者返回某个值的一组语句。还可以向其传入零个或多个 参数 并在函数体执行中被使用。另见 parameter, method 和 函数定义 等节。

function annotation – 函数标注

即针对函数形参或返回值的 annotation 。

函数标注通常用于 类型提示：例如以下函数预期接受两个 int 参数并预期返回一个 int 值:

def sum_two_numbers(a: int, b: int) -> int:
   return a + b

函数标注语法的详解见 函数定义 一节。

请参看 variable annotation 和 PEP 484 [https://www.python.org/dev/peps/pep-0484] 对此功能的描述。

__future__

一种伪模块，可被程序员用来启用与当前解释器不兼容的新语言特性。

通过导入 __future__ 模块并对其中的变量求值，你可以查看新特性何时首次加入语言以及何时成为默认:

>>> import __future__
>>> __future__.division
_Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)

garbage collection – 垃圾回收

释放不再被使用的内存空间的过程。Python 是通过引用计数和一个能够检测和打破循环引用的循环垃圾回收器来执行垃圾回收的。可以使用 gc 模块来控制垃圾回收器。

generator – 生成器

返回一个 generator iterator 的函数。它看起来很像普通函数，不同点在于其包含 yield 表达式以便产生一系列值供给 for-循环使用或是通过 next() 函数逐一获取。

通常是指生成器函数，但在某些情况下也可能是指 生成器迭代器。如果需要清楚表达具体含义，请使用全称以避免歧义。

generator iterator – 生成器迭代器

generator 函数所创建的对象。

每个 yield 会临时暂停处理，记住当前位置执行状态（包括局部变量和挂起的 try 语句）。当该 生成器迭代器 恢复时，它会从离开位置继续执行（这与每次调用都从新开始的普通函数差别很大）。

generator expression – 生成器表达式

An expression that returns an iterator. It looks like a normal expression followed by a for expression defining a loop variable, range, and an optional if expression. The combined expression generates values for an enclosing function:

>>> sum(i*i for i in range(10))         # sum of squares 0, 1, 4, ... 81
285

generic function – 泛型函数

为不同的类型实现相同操作的多个函数所组成的函数。在调用时会由调度算法来确定应该使用哪个实现。

另请参见 single dispatch 术语表条目、functools.singledispatch() 装饰器以及 PEP 443 [https://www.python.org/dev/peps/pep-0443]。

GIL

参见 global interpreter lock。

global interpreter lock – 全局解释器锁

CPython 解释器所采用的一种机制，它确保同一时刻只有一个线程在执行 Python bytecode。此机制通过设置对象模型（包括 dict 等重要内置类型）针对并发访问的隐式安全简化了 CPython 实现。给整个解释器加锁使得解释器多线程运行更方便，其代价则是牺牲了在多处理器上的并行性。

不过，某些标准库或第三方库的扩展模块被设计为在执行计算密集型任务如压缩或哈希时释放 GIL。此外，在执行 I/O 操作时也总是会释放 GIL。

创建一个（以更精细粒度来锁定共享数据的）“自由线程”解释器的努力从未获得成功，因为这会牺牲在普通单处理器情况下的性能。据信克服这种性能问题的措施将导致实现变得更复杂，从而更难以维护。

hashable – 可哈希

一个对象的哈希值如果在其生命周期内绝不改变，就被称为 可哈希 （它需要具有 __hash__() 方法），并可以同其他对象进行比较（它需要具有 __eq__() 方法）。可哈希对象必须具有相同的哈希值比较结果才会相同。

可哈希性使得对象能够作为字典键或集合成员使用，因为这些数据结构要在内部使用哈希值。

All of Python’s immutable built-in objects are hashable; mutable containers (such as lists or dictionaries) are not. Objects which are instances of user-defined classes are hashable by default. They all compare unequal (except with themselves), and their hash value is derived from their id().

IDLE

Python 的 IDE，“集成开发与学习环境”的英文缩写。是 Python 标准发行版附带的基本编辑器和解释器环境。

immutable – 不可变

具有固定值的对象。不可变对象包括数字、字符串和元组。这样的对象不能被改变。如果必须存储一个不同的值，则必须创建新的对象。它们在需要常量哈希值的地方起着重要作用，例如作为字典中的键。

import path – 导入路径

由多个位置（或 路径条目）组成的列表，会被模块的 path based finder 用来查找导入目标。在导入时，此位置列表通常来自 sys.path，但对次级包来说也可能来自上级包的 __path__ 属性。

importing – 导入

令一个模块中的 Python 代码能为另一个模块中的 Python 代码所使用的过程。

importer – 导入器

查找并加载模块的对象；此对象既属于 finder 又属于 loader。

interactive – 交互

Python 带有一个交互式解释器，即你可以在解释器提示符后输入语句和表达式，立即执行并查看其结果。只需不带参数地启动 python 命令（也可以在你的计算机开始菜单中选择相应菜单项）。在测试新想法或检验模块和包的时候用这种方式会非常方便（请记得使用 help(x)）。

interpreted – 解释型

Python 一是种解释型语言，与之相对的是编译型语言，虽然两者的区别由于字节码编译器的存在而会有所模糊。这意味着源文件可以直接运行而不必显式地创建可执行文件再运行。解释型语言通常具有比编译型语言更短的开发/调试周期，但是其程序往往运行得更慢。参见 interactive。

interpreter shutdown – 解释器关闭

当被要求关闭时，Python 解释器将进入一个特殊运行阶段并逐步释放所有已分配资源，例如模块和各种关键内部结构等。它还会多次调用 垃圾回收器。这会触发用户定义析构器或弱引用回调中的代码执行。在关闭阶段执行的代码可能会遇到各种异常，因为其所依赖的资源已不再有效（常见的例子有库模块或警告机制等）。

解释器需要关闭的主要原因有 __main__ 模块或所运行的脚本已完成执行。

iterable – 可迭代对象

能够逐一返回其成员项的对象。可迭代对象的例子包括所有序列类型（例如 list、str 和 tuple）以及某些非序列类型例如 dict、文件对象 以及定义了 __iter__() 方法或是实现了 Sequence 语义的 __getitem__() 方法的任意自定义类对象。

可迭代对象被可用于 for 循环以及许多其他需要一个序列的地方（zip()、map() …）。当一个可迭代对象作为参数传给内置函数 iter() 时，它会返回该对象的迭代器。这种迭代器适用于对值集合的一次性遍历。在使用可迭代对象时，你通常不需要调用 iter() 或者自己处理迭代器对象。for 语句会为你自动处理那些操作，创建一个临时的未命名变量用来在循环期间保存迭代器。参见 iterator、sequence 以及 generator。

iterator – 迭代器

用来表示一连串数据流的对象。重复调用迭代器的 __next__() 方法（或将其传给内置函数 next()）将逐个返回流中的项。当没有数据可用时则将引发 StopIteration 异常。到这时迭代器对象中的数据项已耗尽，继续调用其 __next__() 方法只会再次引发 StopIteration 异常。迭代器必须具有 __iter__() 方法用来返回该迭代器对象自身，因此迭代器必定也是可迭代对象，可被用于其他可迭代对象适用的大部分场合。一个显著的例外是那些会多次重复访问迭代项的代码。容器对象（例如 list）在你每次向其传入 iter() 函数或是在 for 循环中使用它时都会产生一个新的迭代器。如果在此情况下你尝试用迭代器则会返回在之前迭代过程中被耗尽的同一迭代器对象，使其看起来就像是一个空容器。

更多信息可查看 迭代器类型。

key function – 键函数

键函数或称整理函数，是能够返回用于排序或排位的值的可调用对象。例如，locale.strxfrm() 可用于生成一个符合特定区域排序约定的排序键。

Python 中有许多工具都允许用键函数来控制元素的排位或分组方式。其中包括 min(), max(), sorted(), list.sort(), heapq.merge(), heapq.nsmallest(), heapq.nlargest() 以及 itertools.groupby()。

要创建一个键函数有多种方式。例如，str.lower() 方法可以用作忽略大小写排序的键函数。另外，键函数也可通过 lambda 表达式来创建，例如 lambda r: (r[0], r[2])。还有 operator 模块提供了三个键函数构造器：attrgetter()、itemgetter() 和 methodcaller()。请查看 如何排序 一节以获取创建和使用键函数的示例。

keyword argument – 关键字参数

参见 argument。

lambda

由一个单独 expression 构成的匿名内联函数，表达式会在调用时被求值。创建 lambda 函数的句法为 lambda [parameters]: expression

LBYL

“先查看后跳跃”的英文缩写。这种代码编写风格会在进行调用或查找之前显式地检查前提条件。此风格与 EAFP 方式恰成对比，其特点是大量使用 if 语句。

在多线程环境中，LBYL 方式会导致“查看”和“跳跃”之间发生条件竞争风险。例如，以下代码 if key in mapping: return mapping[key] 可能由于在检查操作之后其他线程从 mapping 中移除了 key 而出错。这种问题可通过加锁或使用 EAFP 方式来解决。

list – 列表

Python 内置的一种 sequence。虽然名为列表，但更类似于其他语言中的数组而非链接列表，因为访问元素的时间复杂度为 O(1)。

list comprehension – 列表推导式

处理一个序列中的所有或部分元素并返回结果列表的一种紧凑写法。result = ['{:#04x}'.format(x) for x in range(256) if x % 2 == 0] 将生成一个 0 到 255 范围内的十六进制偶数对应字符串（0x..）的列表。其中 if 子句是可选的，如果省略则 range(256) 中的所有元素都会被处理。

loader – 加载器

负责加载模块的对象。它必须定义名为 load_module() 的方法。加载器通常由一个 finder 返回。详情参见 PEP 302 [https://www.python.org/dev/peps/pep-0302]，对于 abstract base class 可参见 importlib.abc.Loader。

mapping – 映射

一种支持任意键查找并实现了 Mapping 或 MutableMapping 抽象基类 中所规定方法的容器对象。 此类对象的例子包括 dict, collections.defaultdict, collections.OrderedDict 以及 collections.Counter。

meta path finder – 元路径查找器

sys.meta_path 的搜索所返回的 finder。元路径查找器与 path entry finders 存在关联但并不相同。

请查看 importlib.abc.MetaPathFinder 了解元路径查找器所实现的方法。

metaclass – 元类

一种用于创建类的类。类定义包含类名、类字典和基类列表。元类负责接受上述三个参数并创建相应的类。大部分面向对象的编程语言都会提供一个默认实现。Python 的特别之处在于可以创建自定义元类。大部分用户永远不需要这个工具，但当需要出现时，元类可提供强大而优雅的解决方案。它们已被用于记录属性访问日志、添加线程安全性、跟踪对象创建、实现单例，以及其他许多任务。

更多详情参见 元类。

method 方法

在类内部定义的函数。如果作为该类的实例的一个属性来调用，方法将会获取实例对象作为其第一个 argument (通常命名为 self)。参见 function 和 nested scope。

method resolution order – 方法解析顺序

方法解析顺序就是在查找成员时搜索全部基类所用的先后顺序。请查看 Python 2.3 方法解析顺序 [https://www.python.org/download/releases/2.3/mro/] 了解自 2.3 版起 Python 解析器所用相关算法的详情。

module 模块

此对象是 Python 代码的一种组织单位。各模块具有独立的命名空间，可包含任意 Python 对象。模块可通过 importing 操作被加载到 Python 中。

另见 package。

module spec – 模块规格

一个命名空间，其中包含用于加载模块的相关导入信息。是 importlib.machinery.ModuleSpec 的实例。

MRO

参见 method resolution order。

mutable – 可变

可变对象可以在其 id() 保持固定的情况下改变其取值。另请参见 immutable。

named tuple – 具名元组

Any tuple-like class whose indexable elements are also accessible using named attributes (for example, time.localtime() returns a tuple-like object where the year is accessible either with an index such as t[0] or with a named attribute like t.tm_year).

A named tuple can be a built-in type such as time.struct_time, or it can be created with a regular class definition. A full featured named tuple can also be created with the factory function collections.namedtuple(). The latter approach automatically provides extra features such as a self-documenting representation like Employee(name='jones', title='programmer').

namespace – 命名空间

命名空间是存放变量的场所。命名空间有局部、全局和内置的，还有对象中的嵌套命名空间（在方法之内）。命名空间通过防止命名冲突来支持模块化。例如，函数 builtins.open 与 os.open() 可通过各自的命名空间来区分。命名空间还通过明确哪个模块实现那个函数来帮助提高可读性和可维护性。例如，random.seed() 或 itertools.islice() 这种写法明确了这些函数是由 random 与 itertools 模块分别实现的。

namespace package – 命名空间包

PEP 420 [https://www.python.org/dev/peps/pep-0420] 所引入的一种仅被用作子包的容器的 package，命名空间包可以没有实体表示物，其描述方式与 regular package 不同，因为它们没有 __init__.py 文件。

另可参见 module。

nested scope – 嵌套作用域

在一个定义范围内引用变量的能力。例如，在另一函数之内定义的函数可以引用前者的变量。请注意嵌套作用域默认只对引用有效而对赋值无效。局部变量的读写都受限于最内层作用域。类似的，全局变量的读写则作用于全局命名空间。通过 nonlocal 关键字可允许写入外层作用域。

new-style class – 新式类

对于目前已被应于所有类对象的类形式的旧称谓。在早先的 Python 版本中，只有新式类能够使用 Python 新增的更灵活特性，例如 __slots__、描述符、特征属性、__getattribute__()、类方法和静态方法等。

object – 对象

任何具有状态（属性或值）以及预定义行为（方法）的数据。object 也是任何 new-style class 的最顶层基类名。

package – 包

一种可包含子模块或递归地包含子包的 Python module。从技术上说，包是带有 __path__ 属性的 Python 模块。

另参见 regular package 和 namespace package。

parameter – 形参

function （或方法）定义中的命名实体，它指定函数可以接受的一个 argument （或在某些情况下，多个实参）。有五种形参：

positional-or-keyword：位置或关键字，指定一个可以作为 位置参数 传入也可以作为 关键字参数 传入的实参。这是默认的形参类型，例如下面的 foo 和 bar:

def func(foo, bar=None): ...

positional-only：仅限位置，指定一个只能按位置传入的参数。Python 中没有定义仅限位置形参的语法。但是一些内置函数有仅限位置形参（比如 abs()）。

keyword-only：仅限关键字，指定一个只能通过关键字传入的参数。仅限关键字形参可通过在函数定义的形参列表中包含单个可变位置形参或者在多个可变位置形参之前放一个 * 来定义，例如下面的 kw_only1 和 kw_only2:

def func(arg, *, kw_only1, kw_only2): ...

var-positional：可变位置，指定可以提供由一个任意数量的位置参数构成的序列（附加在其他形参已接受的位置参数之后）。这种形参可通过在形参名称前加缀 * 来定义，例如下面的 args:

def func(*args, **kwargs): ...

var-keyword：可变关键字，指定可以提供任意数量的关键字参数（附加在其他形参已接受的关键字参数之后）。这种形参可通过在形参名称前加缀 ** 来定义，例如上面的 kwargs。

形参可以同时指定可选和必选参数，也可以为某些可选参数指定默认值。

另参见 argument 术语表条目、参数与形参的区别 中的常见问题、inspect.Parameter 类、函数定义 一节以及 PEP 362 [https://www.python.org/dev/peps/pep-0362]。

path entry – 路径入口

import path 中的一个单独位置，会被 path based finder 用来查找要导入的模块。

path entry finder – 路径入口查找器

任一可调用对象使用 sys.path_hooks (即 path entry hook) 返回的 finder，此种对象能通过 path entry 来定位模块。

请参看 importlib.abc.PathEntryFinder 以了解路径入口查找器所实现的各个方法。

path entry hook – 路径入口钩子

一种可调用对象，在知道如何查找特定 path entry 中的模块的情况下能够使用 sys.path_hook 列表返回一个 path entry finder。

path based finder – 基于路径的查找器

默认的一种 元路径查找器，可在一个 import path 中查找模块。

path-like object – 路径类对象

代表一个文件系统路径的对象。类路径对象可以是一个表示路径的 str 或者 bytes 对象，还可以是一个实现了 os.PathLike 协议的对象。一个支持 os.PathLike 协议的对象可通过调用 os.fspath() 函数转换为 str 或者 bytes 类型的文件系统路径；os.fsdecode() 和 os.fsencode() 可被分别用来确保获得 str 或 bytes 类型的结果。此对象是由 PEP 519 [https://www.python.org/dev/peps/pep-0519] 引入的。

PEP

“Python 增强提议”的英文缩写。一个 PEP 就是一份设计文档，用来向 Python 社区提供信息，或描述一个 Python 的新增特性及其进度或环境。PEP 应当提供精确的技术规格和所提议特性的原理说明。

PEP 应被作为提出主要新特性建议、收集社区对特定问题反馈以及为必须加入 Python 的设计决策编写文档的首选机制。PEP 的作者有责任在社区内部建立共识，并应将不同意见也记入文档。

参见 PEP 1 [https://www.python.org/dev/peps/pep-0001]。

portion – 部分

构成一个命名空间包的单个目录内文件集合（也可能存放于一个 zip 文件内），具体定义见 PEP 420 [https://www.python.org/dev/peps/pep-0420]。

positional argument – 位置参数

参见 argument。

provisional API – 暂定 API

暂定 API 是指被有意排除在标准库的向后兼容性保证之外的应用编程接口。虽然此类接口通常不会再有重大改变，但只要其被标记为暂定，就可能在核心开发者确定有必要的情况下进行向后不兼容的更改（甚至包括移除该接口）。此种更改并不会随意进行 – 仅在 API 被加入之前未考虑到的严重基础性缺陷被发现时才可能会这样做。

即便是对暂定 API 来说，向后不兼容的更改也会被视为“最后的解决方案” —— 任何问题被确认时都会尽可能先尝试找到一种向后兼容的解决方案。

这种处理过程允许标准库持续不断地演进，不至于被有问题的长期性设计缺陷所困。详情见 PEP 411 [https://www.python.org/dev/peps/pep-0411]。

provisional package – 暂定包

参见 provisional API。

Python 3000

Python 3.x 发布路线的昵称（这个名字在版本 3 的发布还遥遥无期的时候就已出现了）。有时也被缩写为“Py3k”。

Pythonic

指一个思路或一段代码紧密遵循了 Python 语言最常用的风格和理念，而不是使用其他语言中通用的概念来实现代码。例如，Python 的常用风格是使用 for 语句循环来遍历一个可迭代对象中的所有元素。许多其他语言没有这样的结构，因此不熟悉 Python 的人有时会选择使用一个数字计数器:

for i in range(len(food)):
    print(food[i])

而相应的更简洁更 Pythonic 的方法是这样的:

for piece in food:
    print(piece)

qualified name – 限定名称

一个以点号分隔的名称，显示从模块的全局作用域到该模块中定义的某个类、函数或方法的“路径”，相关定义见 PEP 3155 [https://www.python.org/dev/peps/pep-3155]。对于最高层级的函数和类，限定名称与对象名称一致:

>>> class C:
...     class D:
...         def meth(self):
...             pass
...
>>> C.__qualname__
'C'
>>> C.D.__qualname__
'C.D'
>>> C.D.meth.__qualname__
'C.D.meth'

当被用于引用模块时，完整限定名称 意为标示该模块的以点号分隔的整个路径，其中包含其所有的父包，例如 email.mime.text:

>>> import email.mime.text
>>> email.mime.text.__name__
'email.mime.text'

reference count – 引用计数

对特定对象的引用的数量。当一个对象的引用计数降为零时，所分配资源将被释放。引用计数对 Python 代码来说通常是不可见的，但它是 CPython 实现的一个关键元素。sys 模块定义了一个 getrefcount() 函数，程序员可调用它来返回特定对象的引用计数。

regular package – 常规包

传统型的 package，例如包含有一个 __init__.py 文件的目录。

另参见 namespace package。

__slots__

一种写在类内部的声明，通过预先声明实例属性等对象并移除实例字典来节省内存。虽然这种技巧很流行，但想要用好却并不容易，最好是只保留在少数情况下采用，例如极耗内存的应用程序，并且其中包含大量实例。

sequence – 序列

一种 iterable，它支持通过 __getitem__() 特殊方法来使用整数索引进行高效的元素访问，并定义了一个返回序列长度的 __len__() 方法。内置的序列类型有 list、str、tuple 和 bytes。注意虽然 dict 也支持 __getitem__() 和 __len__()，但它被认为属于映射而非序列，因为它查找时使用任意的 immutable 键而非整数。

collections.abc.Sequence 抽象基类定义了一个更丰富的接口，它超越了 __getitem__() 和 __len__()，添加了 count(), index(), __contains__() 和 __reversed__() 。 可以使用 register() 显式注册实现此扩展接口的类型。

single dispatch – 单分派

一种 generic function 分派形式，其实现是基于单个参数的类型来选择的。

slice – 切片

通常只包含了特定 sequence 的一部分的对象。切片是通过使用下标标记来创建的，在 [] 中给出几个以冒号分隔的数字，例如 variable_name[1:3:5]。方括号（下标）标记在内部使用 slice 对象。

special method – 特殊方法

一种由 Python 隐式调用的方法，用来对某个类型执行特定操作例如相加等等。这种方法的名称的首尾都为双下划线。特殊方法的文档参见 特殊方法名称。

statement – 语句

语句是程序段（一个代码“块”）的组成单位。一条语句可以是一个 expression 或某个带有关键字的结构，例如 if、while 或 for。

struct sequence

A tuple with named elements. Struct sequences expose an interface similar to named tuple in that elements can be accessed either by index or as an attribute. However, they do not have any of the named tuple methods like _make() or _asdict(). Examples of struct sequences include sys.float_info and the return value of os.stat().

text encoding – 文本编码

用于将Unicode字符串编码为字节串的编码器。

text file – 文本文件

一种能够读写 str 对象的 file object。通常一个文本文件实际是访问一个面向字节的数据流并自动处理 text encoding。文本文件的例子包括以文本模式（'r' 或 'w'）打开的文件、sys.stdin、sys.stdout 以及 io.StringIO 的实例。

另请参看 binary file 了解能够读写 字节类对象 的文件对象。

triple-quoted string – 三引号字符串

首尾各带三个连续双引号（”）或者单引号（’）的字符串。它们在功能上与首尾各用一个引号标注的字符串没有什么不同，但是有多种用处。它们允许你在字符串内包含未经转义的单引号和双引号，并且可以跨越多行而无需使用连接符，在编写文档字符串时特别好用。

type – 类型

类型决定一个 Python 对象属于什么种类；每个对象都具有一种类型。要知道对象的类型，可以访问它的 __class__ 属性，或是通过 type(obj) 来获取。

type alias – 类型别名

一个类型的同义词，创建方式是把类型赋值给特定的标识符。

类型别名的作用是简化 类型提示。例如:

from typing import List, Tuple

def remove_gray_shades(
        colors: List[Tuple[int, int, int]]) -> List[Tuple[int, int, int]]:
    pass

可以这样提高可读性:

from typing import List, Tuple

Color = Tuple[int, int, int]

def remove_gray_shades(colors: List[Color]) -> List[Color]:
    pass

参见 typing 和 PEP 484 [https://www.python.org/dev/peps/pep-0484]，其中有对此功能的详细描述。

type hint – 类型提示

annotation 为变量、类属性、函数的形参或返回值指定预期的类型。

类型提示属于可选项，Python 不要求提供，但其可对静态类型分析工具起作用，并可协助 IDE 实现代码补全与重构。

全局变量、类属性和函数的类型提示可以使用 typing.get_type_hints() 来访问，但局部变量则不可以。

参见 typing 和 PEP 484 [https://www.python.org/dev/peps/pep-0484]，其中有对此功能的详细描述。

universal newlines – 通用换行

一种解读文本流的方式，将以下所有符号都识别为行结束标志：Unix 的行结束约定 '\n'、Windows 的约定 '\r\n' 以及旧版 Macintosh 的约定 '\r'。参见 PEP 278 [https://www.python.org/dev/peps/pep-0278] 和 PEP 3116 [https://www.python.org/dev/peps/pep-3116] 和 bytes.splitlines() 了解更多用法说明。

variable annotation – 变量标注

对变量或类属性的 annotation。

在标注变量或类属性时，还可选择为其赋值:

class C:
    field: 'annotation'

变量标注通常被用作 类型提示：例如以下变量预期接受 int 类型的值:

count: int = 0

变量标注语法的详细解释见 带标注的赋值语句 一节。

请参看 function annotation、PEP 484 [https://www.python.org/dev/peps/pep-0484] 和 PEP 526 [https://www.python.org/dev/peps/pep-0526]，其中对此功能有详细描述。

virtual environment – 虚拟环境

一种采用协作式隔离的运行时环境，允许 Python 用户和应用程序在安装和升级 Python 分发包时不会干扰到同一系统上运行的其他 Python 应用程序的行为。

另参见 venv。

virtual machine – 虚拟机

一台完全通过软件定义的计算机。Python 虚拟机可执行字节码编译器所生成的 bytecode。

Zen of Python – Python 之禅

列出 Python 设计的原则与哲学，有助于理解与使用这种语言。查看其具体内容可在交互模式提示符中输入 “import this”。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

文档说明

这些文档生成自 reStructuredText [http://docutils.sourceforge.net/rst.html] 原文档，由 Sphinx [http://sphinx-doc.org/] （一个专门为 Python 文档写的文档生成器）创建。

本文档和它所用工具链的开发完全是由志愿者完成的，这和 Python 本身一样。如果您想参与进来，请阅读 解决 Bug 了解如何参与。我们随时欢迎新的志愿者！

特别鸣谢：

Fred L. Drake, Jr.，创造了用于早期 Python 文档的工具链，以及撰写了非常多的文档；

Docutils软件包 [http://docutils.sourceforge.net/] 项目，创建了 reStructuredText 文本格式和 Docutils 软件套件；

Fredrik Lundh，Sphinx 从他的 Alternative Python Reference [http://effbot.org/zone/pyref.htm] 项目中获得了很多好的想法。

Python 文档的贡献者

有很多对 Python 语言，Python 标准库和 Python 文档有贡献的人，随 Python 源代码发布的 Misc/ACKS [https://github.com/python/cpython/tree/3.6/Misc/ACKS] 文件列出了部分贡献者。

有了 Python 社区的输入和贡献，Python 才有了如此出色的文档 - 谢谢你们！

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

解决 Bug

Python 是一门以稳定著称的编程语言。为了保持它的稳定性，开发者们想了解您在 Python 中发现的任何缺陷。

有时候自己修复漏洞并将补丁提交给 Python 能更快地解决问题，因为它简化了流程并且减少了参与的人力。 请参阅如何 提交补丁。

文档 Bug

If you find a bug in this documentation or would like to propose an improvement, please submit a bug report on the tracker. If you have a suggestion how to fix it, include that as well.

如果您时间有限，还可以通过电子邮件将文档的错误报告发送至 docs@python.org （代码运行错误请发送至 python-list@python.org）。“docs@”是一个由志愿者运作的邮件列表；您的请求会得到关注，但可能需要一些时间才会被处理。

参见

在 Python 的错误追踪系统上的 Documentation bugs [https://bugs.python.org/issue?@filter=status&@filter=components&components=4&status=1&@columns=id,activity,title,status&@sort=-activity]

使用 Python 的问题追踪系统

对于 Python 本身的错误报告应当通过 Python 错误追踪系统（https://bugs.python.org/）进行提交。该错误追踪系统提供了一个可以输入并提交相关信息给开发者的网页表单。

填写报告的第一步是确认是否有重复的报告。这样做除了可以节省开发者的时间，也能让您从中为了解决这个问题我们做出了怎样的努力；我们有可能我们已经在下个版本中修复了这个问题，也有可能需要更详细的信息来搞定它（在这种情况下，如果可以，非常欢迎您提供这些信息！）。要查找重复的报告，请使用错误数据库页面顶部的搜索栏进行搜索。

如果您正在报告的问题并没有记录在错误追踪系统中，请返回并登录 Python 错误追踪系统。如果您没有一个错误追踪系统的账号，请选择“注册（Register）”链接，或者如果您想使用 OpenID 的话，请选择侧边栏中任何一个 OpenID 提供方的标识。您不能匿名提交一份错误报告。

如果已经登录， 那您就可以提交错误报告了。请点击侧边栏中的“新建（Create New）”链接，打开错误报告表单。

表单中有几个空栏。请在 “标题” 栏填入一个对问题的非常简短的描述；一个好的描述应当少于十个字。在 “类型” 栏，选择您的问题的类型；同样地，选择bug相对应的 “组件” 和 “版本” 。

在”说明“栏，请详细描述问题，包括您预期的情况和实际的情况。请确保信息包含任何涉及的拓展模块，以及您当时所使用的硬件和软件平台(如果可能，请附上版本信息)。

每一份错误报告将被分派给一名开发者，并由这名开发者决定如何更正这个问题。每当有针对该错误的进展时，您都会收到一个更新通知。

参见

How to Report Bugs Effectively [http://www.chiark.greenend.org.uk/~sgtatham/bugs.html]

文章详细描述了如何创建一份有用的错误报告。同时描述了什么样的信息是有用的以及为什么是有用的。

Bug Writing Guidelines [https://developer.mozilla.org/en-US/docs/Mozilla/QA/Bug_writing_guidelines]

有关书写一份好的错误报告的信息。部分仅针对 Mozilla 项目，不过其描述了通用的恰当做法。

开始为 Python 贡献您的知识

除了仅仅报告您所发现的错误之外，同样欢迎您提交修复它们的补丁。您可以在 Python 开发者指引 [https://devguide.python.org/] 中找到更多上手为 Python 打补丁的相关信息。如果您有任何问题，核心导师邮件列表 [https://mail.python.org/mailman/listinfo/core-mentorship/] 是一个友好的去处，在那里， 你可以寻求修复 Python事宜相关问题的答案。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

Copyright

Python 与这份文档：

版权所有 © 2001-2021 Python 软件基金会。 保留所有权利。

版权所有 © 2000 BeOpen.com。保留所有权利。

版权所有 © 1995-2000 Corporation for National Research Initiatives。保留所有权利。

版权所有 © 1991-1995 Stichting Mathematisch Centrum。保留所有权利。

有关完整的许可证和许可信息，参见 历史和许可证。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

历史和许可证

该软件的历史

Python 由荷兰数学和计算机科学研究学会（CWI，见 https://www.cwi.nl/ ）的 Guido van Rossum 于 1990 年代初设计，作为一门叫做 ABC 的语言的替代品。尽管 Python 包含了许多来自其他人的贡献，Guido 仍是其主要作者。

1995 年，Guido 在弗吉尼亚州的国家创新研究公司（CNRI，见 https://www.cnri.reston.va.us/ ）继续他在 Python 上的工作，并在那里发布了该软件的多个版本。

2000 年五月，Guido 和 Python 核心开发团队转到 BeOpen.com 并组建了 BeOpen PythonLabs 团队。 同年十月，PythonLabs 团队转到 Digital Creations (现为 Zope Corporation；见 https://www.zope.org/)。 2001 年，Python 软件基金会 (PSF，见 https://www.python.org/psf/) 成立，这是一个专为拥有 Python 相关知识产权而创建的非营利组织。 Zope Corporation 现在是 PSF 的赞助成员。

所有的 Python 版本都是开源的（有关开源的定义参阅 https://opensource.org/ ）。历史上，绝大多数 Python 版本是 GPL 兼容的；下表总结了各个版本情况。

发布版本

源自

年份

所有者

GPL 兼容？

0.9.0 至 1.2

n/a

1991-1995

CWI

是

1.3 至 1.5.2

1.2

1995-1999

CNRI

是

1.6

1.5.2

2000

CNRI

否

2.0

1.6

2000

BeOpen.com

否

1.6.1

1.6

2001

CNRI

否

2.1

2.0+1.6.1

2001

PSF

否

2.0.1

2.0+1.6.1

2001

PSF

是

2.1.1

2.1+2.0.1

2001

PSF

是

2.1.2

2.1.1

2002

PSF

是

2.1.3

2.1.2

2002

PSF

是

2.2 及更高

2.1.1

2001 至今

PSF

是

注解

GPL 兼容并不意味着 Python 在 GPL 下发布。与 GPL 不同，所有 Python 许可证都允许您分发修改后的版本，而无需开源所做的更改。GPL 兼容的许可证使得 Python 可以与其它在 GPL 下发布的软件结合使用；但其它的许可证则不行。

感谢众多在 Guido 指导下工作的外部志愿者，使得这些发布成为可能。

获取或以其他方式使用 Python 的条款和条件

用于 PYTHON 3.6.13 的 PSF 许可协议

1. This LICENSE AGREEMENT is between the Python Software Foundation ("PSF"), and
   the Individual or Organization ("Licensee") accessing and otherwise using Python
   3.6.13 software in source or binary form and its associated documentation.

2. Subject to the terms and conditions of this License Agreement, PSF hereby
   grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce,
   analyze, test, perform and/or display publicly, prepare derivative works,
   distribute, and otherwise use Python 3.6.13 alone or in any derivative
   version, provided, however, that PSF's License Agreement and PSF's notice of
   copyright, i.e., "Copyright © 2001-2021 Python Software Foundation; All Rights
   Reserved" are retained in Python 3.6.13 alone or in any derivative version
   prepared by Licensee.

3. In the event Licensee prepares a derivative work that is based on or
   incorporates Python 3.6.13 or any part thereof, and wants to make the
   derivative work available to others as provided herein, then Licensee hereby
   agrees to include in any such work a brief summary of the changes made to Python
   3.6.13.

4. PSF is making Python 3.6.13 available to Licensee on an "AS IS" basis.
   PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  BY WAY OF
   EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND DISCLAIMS ANY REPRESENTATION OR
   WARRANTY OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE
   USE OF PYTHON 3.6.13 WILL NOT INFRINGE ANY THIRD PARTY RIGHTS.

5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON 3.6.13
   FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF
   MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 3.6.13, OR ANY DERIVATIVE
   THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.

6. This License Agreement will automatically terminate upon a material breach of
   its terms and conditions.

7. Nothing in this License Agreement shall be deemed to create any relationship
   of agency, partnership, or joint venture between PSF and Licensee.  This License
   Agreement does not grant permission to use PSF trademarks or trade name in a
   trademark sense to endorse or promote products or services of Licensee, or any
   third party.

8. By copying, installing or otherwise using Python 3.6.13, Licensee agrees
   to be bound by the terms and conditions of this License Agreement.

用于 PYTHON 2.0 的 BEOPEN.COM 许可协议

BEOPEN PYTHON 开源许可协议第 1 版

1. This LICENSE AGREEMENT is between BeOpen.com ("BeOpen"), having an office at
   160 Saratoga Avenue, Santa Clara, CA 95051, and the Individual or Organization
   ("Licensee") accessing and otherwise using this software in source or binary
   form and its associated documentation ("the Software").

2. Subject to the terms and conditions of this BeOpen Python License Agreement,
   BeOpen hereby grants Licensee a non-exclusive, royalty-free, world-wide license
   to reproduce, analyze, test, perform and/or display publicly, prepare derivative
   works, distribute, and otherwise use the Software alone or in any derivative
   version, provided, however, that the BeOpen Python License is retained in the
   Software, alone or in any derivative version prepared by Licensee.

3. BeOpen is making the Software available to Licensee on an "AS IS" basis.
   BEOPEN MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  BY WAY OF
   EXAMPLE, BUT NOT LIMITATION, BEOPEN MAKES NO AND DISCLAIMS ANY REPRESENTATION OR
   WARRANTY OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE
   USE OF THE SOFTWARE WILL NOT INFRINGE ANY THIRD PARTY RIGHTS.

4. BEOPEN SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF THE SOFTWARE FOR
   ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF USING,
   MODIFYING OR DISTRIBUTING THE SOFTWARE, OR ANY DERIVATIVE THEREOF, EVEN IF
   ADVISED OF THE POSSIBILITY THEREOF.

5. This License Agreement will automatically terminate upon a material breach of
   its terms and conditions.

6. This License Agreement shall be governed by and interpreted in all respects
   by the law of the State of California, excluding conflict of law provisions.
   Nothing in this License Agreement shall be deemed to create any relationship of
   agency, partnership, or joint venture between BeOpen and Licensee.  This License
   Agreement does not grant permission to use BeOpen trademarks or trade names in a
   trademark sense to endorse or promote products or services of Licensee, or any
   third party.  As an exception, the "BeOpen Python" logos available at
   http://www.pythonlabs.com/logos.html may be used according to the permissions
   granted on that web page.

7. By copying, installing or otherwise using the software, Licensee agrees to be
   bound by the terms and conditions of this License Agreement.

用于 PYTHON 1.6.1 的 CNRI 许可协议

1. This LICENSE AGREEMENT is between the Corporation for National Research
   Initiatives, having an office at 1895 Preston White Drive, Reston, VA 20191
   ("CNRI"), and the Individual or Organization ("Licensee") accessing and
   otherwise using Python 1.6.1 software in source or binary form and its
   associated documentation.

2. Subject to the terms and conditions of this License Agreement, CNRI hereby
   grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce,
   analyze, test, perform and/or display publicly, prepare derivative works,
   distribute, and otherwise use Python 1.6.1 alone or in any derivative version,
   provided, however, that CNRI's License Agreement and CNRI's notice of copyright,
   i.e., "Copyright © 1995-2001 Corporation for National Research Initiatives; All
   Rights Reserved" are retained in Python 1.6.1 alone or in any derivative version
   prepared by Licensee.  Alternately, in lieu of CNRI's License Agreement,
   Licensee may substitute the following text (omitting the quotes): "Python 1.6.1
   is made available subject to the terms and conditions in CNRI's License
   Agreement.  This Agreement together with Python 1.6.1 may be located on the
   Internet using the following unique, persistent identifier (known as a handle):
   1895.22/1013.  This Agreement may also be obtained from a proxy server on the
   Internet using the following URL: http://hdl.handle.net/1895.22/1013."

3. In the event Licensee prepares a derivative work that is based on or
   incorporates Python 1.6.1 or any part thereof, and wants to make the derivative
   work available to others as provided herein, then Licensee hereby agrees to
   include in any such work a brief summary of the changes made to Python 1.6.1.

4. CNRI is making Python 1.6.1 available to Licensee on an "AS IS" basis.  CNRI
   MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  BY WAY OF EXAMPLE,
   BUT NOT LIMITATION, CNRI MAKES NO AND DISCLAIMS ANY REPRESENTATION OR WARRANTY
   OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
   PYTHON 1.6.1 WILL NOT INFRINGE ANY THIRD PARTY RIGHTS.

5. CNRI SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON 1.6.1 FOR
   ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF
   MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 1.6.1, OR ANY DERIVATIVE
   THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.

6. This License Agreement will automatically terminate upon a material breach of
   its terms and conditions.

7. This License Agreement shall be governed by the federal intellectual property
   law of the United States, including without limitation the federal copyright
   law, and, to the extent such U.S. federal law does not apply, by the law of the
   Commonwealth of Virginia, excluding Virginia's conflict of law provisions.
   Notwithstanding the foregoing, with regard to derivative works based on Python
   1.6.1 that incorporate non-separable material that was previously distributed
   under the GNU General Public License (GPL), the law of the Commonwealth of
   Virginia shall govern this License Agreement only as to issues arising under or
   with respect to Paragraphs 4, 5, and 7 of this License Agreement.  Nothing in
   this License Agreement shall be deemed to create any relationship of agency,
   partnership, or joint venture between CNRI and Licensee.  This License Agreement
   does not grant permission to use CNRI trademarks or trade name in a trademark
   sense to endorse or promote products or services of Licensee, or any third
   party.

8. By clicking on the "ACCEPT" button where indicated, or by copying, installing
   or otherwise using Python 1.6.1, Licensee agrees to be bound by the terms and
   conditions of this License Agreement.

用于 PYTHON 0.9.0 至 1.2 的 CWI 许可协议

Copyright © 1991 - 1995, Stichting Mathematisch Centrum Amsterdam, The
Netherlands.  All rights reserved.

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided that
the above copyright notice appear in all copies and that both that copyright
notice and this permission notice appear in supporting documentation, and that
the name of Stichting Mathematisch Centrum or CWI not be used in advertising or
publicity pertaining to distribution of the software without specific, written
prior permission.

STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE FOR ANY SPECIAL, INDIRECT
OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

被收录软件的许可证与鸣谢

本节是Python发行版中收录的第三方软件的许可和致谢清单，该清单是不完整且不断增长的。

Mersenne Twister

_random 模块包含基于 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html 下载的代码。以下是原始代码的完整注释（声明）：

A C-program for MT19937, with initialization improved 2002/1/26.
Coded by Takuji Nishimura and Makoto Matsumoto.

Before using, initialize the state by using init_genrand(seed)
or init_by_array(init_key, key_length).

Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

 3. The names of its contributors may not be used to endorse or promote
    products derived from this software without specific prior written
    permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Any feedback is very welcome.
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)

套接字

socket 模块使用 getaddrinfo() 和 getnameinfo() 函数，这些函数源代码在WIDE项目（http://www.wide.ad.jp/）的单独源文件中。

Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. Neither the name of the project nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

Floating point exception control

The source for the fpectl module includes the following notice:

  ---------------------------------------------------------------------
 /                       Copyright (c) 1996.                           \
|          The Regents of the University of California.                 |
|                        All rights reserved.                           |
|                                                                       |
|   Permission to use, copy, modify, and distribute this software for   |
|   any purpose without fee is hereby granted, provided that this en-   |
|   tire notice is included in all copies of any software which is or   |
|   includes  a  copy  or  modification  of  this software and in all   |
|   copies of the supporting documentation for such software.           |
|                                                                       |
|   This  work was produced at the University of California, Lawrence   |
|   Livermore National Laboratory under  contract  no.  W-7405-ENG-48   |
|   between  the  U.S.  Department  of  Energy and The Regents of the   |
|   University of California for the operation of UC LLNL.              |
|                                                                       |
|                              DISCLAIMER                               |
|                                                                       |
|   This  software was prepared as an account of work sponsored by an   |
|   agency of the United States Government. Neither the United States   |
|   Government  nor the University of California nor any of their em-   |
|   ployees, makes any warranty, express or implied, or  assumes  any   |
|   liability  or  responsibility  for the accuracy, completeness, or   |
|   usefulness of any information,  apparatus,  product,  or  process   |
|   disclosed,   or  represents  that  its  use  would  not  infringe   |
|   privately-owned rights. Reference herein to any specific  commer-   |
|   cial  products,  process,  or  service  by trade name, trademark,   |
|   manufacturer, or otherwise, does not  necessarily  constitute  or   |
|   imply  its endorsement, recommendation, or favoring by the United   |
|   States Government or the University of California. The views  and   |
|   opinions  of authors expressed herein do not necessarily state or   |
|   reflect those of the United States Government or  the  University   |
|   of  California,  and shall not be used for advertising or product   |
 \  endorsement purposes.                                              /
  ---------------------------------------------------------------------

异步套接字服务

asynchat and asyncore 模块包含以下声明:

Copyright 1996 by Sam Rushing

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that copyright notice and this permission
notice appear in supporting documentation, and that the name of Sam
Rushing not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.

SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Cookie 管理

http.cookies 模块包含以下声明:

Copyright 2000 by Timothy O'Malley <timo@alum.mit.edu>

               All Rights Reserved

Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that copyright notice and this permission
notice appear in supporting documentation, and that the name of
Timothy O'Malley  not be used in advertising or publicity
pertaining to distribution of the software without specific, written
prior permission.

Timothy O'Malley DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS, IN NO EVENT SHALL Timothy O'Malley BE LIABLE FOR
ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.

执行追踪

trace 模块包含以下声明:

portions copyright 2001, Autonomous Zones Industries, Inc., all rights...
err...  reserved and offered to the public under the terms of the
Python 2.2 license.
Author: Zooko O'Whielacronx
http://zooko.com/
mailto:zooko@zooko.com

Copyright 2000, Mojam Media, Inc., all rights reserved.
Author: Skip Montanaro

Copyright 1999, Bioreason, Inc., all rights reserved.
Author: Andrew Dalke

Copyright 1995-1997, Automatrix, Inc., all rights reserved.
Author: Skip Montanaro

Copyright 1991-1995, Stichting Mathematisch Centrum, all rights reserved.

Permission to use, copy, modify, and distribute this Python software and
its associated documentation for any purpose without fee is hereby
granted, provided that the above copyright notice appears in all copies,
and that both that copyright notice and this permission notice appear in
supporting documentation, and that the name of neither Automatrix,
Bioreason or Mojam Media be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.

UUencode 与 UUdecode 函数

uu 模块包含以下声明:

Copyright 1994 by Lance Ellinghouse
Cathedral City, California Republic, United States of America.
                       All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Lance Ellinghouse
not be used in advertising or publicity pertaining to distribution
of the software without specific, written prior permission.
LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Modified by Jack Jansen, CWI, July 1995:
- Use binascii module to do the actual line-by-line conversion
  between ascii and binary. This results in a 1000-fold speedup. The C
  version is still 5 times faster, though.
- Arguments more compliant with Python standard

XML 远程过程调用

xmlrpc.client 模块包含以下声明:

    The XML-RPC client interface is

Copyright (c) 1999-2002 by Secret Labs AB
Copyright (c) 1999-2002 by Fredrik Lundh

By obtaining, using, and/or copying this software and/or its
associated documentation, you agree that you have read, understood,
and will comply with the following terms and conditions:

Permission to use, copy, modify, and distribute this software and
its associated documentation for any purpose and without fee is
hereby granted, provided that the above copyright notice appears in
all copies, and that both that copyright notice and this permission
notice appear in supporting documentation, and that the name of
Secret Labs AB or the author not be used in advertising or publicity
pertaining to distribution of the software without specific, written
prior permission.

SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANT-
ABILITY AND FITNESS.  IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR
BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THIS SOFTWARE.

test_epoll

test_epoll 模块包含以下声明:

Copyright (c) 2001-2006 Twisted Matrix Laboratories.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Select kqueue

select 模块关于 kqueue 的接口包含以下声明:

Copyright (c) 2000 Doug White, 2006 James Knight, 2007 Christian Heimes
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

SipHash24

The file Python/pyhash.c contains Marek Majkowski’ implementation of Dan Bernstein’s SipHash24 algorithm. The contains the following note:

<MIT License>
Copyright (c) 2013  Marek Majkowski <marek@popcount.org>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
</MIT License>

Original location:
   https://github.com/majek/csiphash/

Solution inspired by code from:
   Samuel Neves (supercop/crypto_auth/siphash24/little)
   djb (supercop/crypto_auth/siphash24/little2)
   Jean-Philippe Aumasson (https://131002.net/siphash/siphash24.c)

strtod and dtoa

Python/dtoa.c 文件提供了C语言的 dtoa 和 strtod 函数，用于将C语言的 双精度型和字符串进行转换，该文件由David M. Gay的同名文件派生而来，当前可从http://www.netlib.org/fp/ 下载。2009年3月16日检索到的原始文件包含以下版权和许可声明:

/****************************************************************
 *
 * The author of this software is David M. Gay.
 *
 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose without fee is hereby granted, provided that this entire notice
 * is included in all copies of any software which is or includes a copy
 * or modification of this software and in all copies of the supporting
 * documentation for such software.
 *
 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
 *
 ***************************************************************/

OpenSSL

如果操作系统可用，则 hashlib, posix, ssl, crypt 模块使用OpenSSL库来提高性能。此外，适用于Python的Windows和Mac OS X安装程序可能包括OpenSSL库的拷贝，所以在此处也列出了OpenSSL许可证的拷贝:

 LICENSE ISSUES
 ==============

 The OpenSSL toolkit stays under a dual license, i.e. both the conditions of
 the OpenSSL License and the original SSLeay license apply to the toolkit.
 See below for the actual license texts. Actually both licenses are BSD-style
 Open Source licenses. In case of any license issues related to OpenSSL
 please contact openssl-core@openssl.org.

 OpenSSL License
 ---------------

   /* ====================================================================
    * Copyright (c) 1998-2008 The OpenSSL Project.  All rights reserved.
    *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions
    * are met:
    *
    * 1. Redistributions of source code must retain the above copyright
    *    notice, this list of conditions and the following disclaimer.
    *
    * 2. Redistributions in binary form must reproduce the above copyright
    *    notice, this list of conditions and the following disclaimer in
    *    the documentation and/or other materials provided with the
    *    distribution.
    *
    * 3. All advertising materials mentioning features or use of this
    *    software must display the following acknowledgment:
    *    "This product includes software developed by the OpenSSL Project
    *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
    *
    * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
    *    endorse or promote products derived from this software without
    *    prior written permission. For written permission, please contact
    *    openssl-core@openssl.org.
    *
    * 5. Products derived from this software may not be called "OpenSSL"
    *    nor may "OpenSSL" appear in their names without prior written
    *    permission of the OpenSSL Project.
    *
    * 6. Redistributions of any form whatsoever must retain the following
    *    acknowledgment:
    *    "This product includes software developed by the OpenSSL Project
    *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
    *
    * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
    * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    * OF THE POSSIBILITY OF SUCH DAMAGE.
    * ====================================================================
    *
    * This product includes cryptographic software written by Eric Young
    * (eay@cryptsoft.com).  This product includes software written by Tim
    * Hudson (tjh@cryptsoft.com).
    *
    */

Original SSLeay License
-----------------------

   /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
    * All rights reserved.
    *
    * This package is an SSL implementation written
    * by Eric Young (eay@cryptsoft.com).
    * The implementation was written so as to conform with Netscapes SSL.
    *
    * This library is free for commercial and non-commercial use as long as
    * the following conditions are aheared to.  The following conditions
    * apply to all code found in this distribution, be it the RC4, RSA,
    * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    * included with this distribution is covered by the same copyright terms
    * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    *
    * Copyright remains Eric Young's, and as such any Copyright notices in
    * the code are not to be removed.
    * If this package is used in a product, Eric Young should be given attribution
    * as the author of the parts of the library used.
    * This can be in the form of a textual message at program startup or
    * in documentation (online or textual) provided with the package.
    *
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions
    * are met:
    * 1. Redistributions of source code must retain the copyright
    *    notice, this list of conditions and the following disclaimer.
    * 2. Redistributions in binary form must reproduce the above copyright
    *    notice, this list of conditions and the following disclaimer in the
    *    documentation and/or other materials provided with the distribution.
    * 3. All advertising materials mentioning features or use of this software
    *    must display the following acknowledgement:
    *    "This product includes cryptographic software written by
    *     Eric Young (eay@cryptsoft.com)"
    *    The word 'cryptographic' can be left out if the rouines from the library
    *    being used are not cryptographic related :-).
    * 4. If you include any Windows specific code (or a derivative thereof) from
    *    the apps directory (application code) you must include an acknowledgement:
    *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    *
    * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    * SUCH DAMAGE.
    *
    * The licence and distribution terms for any publically available version or
    * derivative of this code cannot be changed.  i.e. this code cannot simply be
    * copied and put under another distribution licence
    * [including the GNU Public Licence.]
    */

expat

除非使用 --with-system-expat 配置了构建，否则 pyexpat 扩展都是用包含 expat源的拷贝构建的:

Copyright (c) 1998, 1999, 2000 Thai Open Source Software Center Ltd
                               and Clark Cooper

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

libffi

除非使用 --with-system-libffi 配置了构建，否则 _ctypes 扩展都是包含 libffi源的拷贝构建的:

Copyright (c) 1996-2008  Red Hat, Inc and others.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
``Software''), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

zlib

如果系统上找到的zlib版本太旧而无法用于构建，则使用包含 zlib源代码的拷贝来构建 zlib 扩展:

Copyright (C) 1995-2011 Jean-loup Gailly and Mark Adler

This software is provided 'as-is', without any express or implied
warranty.  In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
   claim that you wrote the original software. If you use this software
   in a product, an acknowledgment in the product documentation would be
   appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.

3. This notice may not be removed or altered from any source distribution.

Jean-loup Gailly        Mark Adler
jloup@gzip.org          madler@alumni.caltech.edu

cfuhash

tracemalloc 使用的哈希表的实现基于 cfuhash项目:

Copyright (c) 2005 Don Owens
All rights reserved.

This code is released under the BSD license:

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

  * Redistributions in binary form must reproduce the above
    copyright notice, this list of conditions and the following
    disclaimer in the documentation and/or other materials provided
    with the distribution.

  * Neither the name of the author nor the names of its
    contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.

libmpdec

除非使用 --with-system-libmpdec 配置了构建，否则 _decimal 模块都是用包含 libmpdec库的拷贝构建的。

Copyright (c) 2008-2016 Stefan Krah. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

分发 Python 模块（遗留版本）

作者

Greg Ward ， Anthony Baxter

电子邮箱

distutils-sig@python.org

参见

分发 Python 模块

最新的模块分发文档

本文档从模块开发人员的角度描述了 Python Distribution Utilities（“Distutils” ，描述了如何使用 Distutils 轻松地为更广泛的受众提供 Python 模块和扩展，而构建/发布/安装机制的开销很小。

注解

This guide only covers the basic tools for building and distributing extensions that are provided as part of this version of Python. Third party tools offer easier to use and more secure alternatives. Refer to the quick recommendations section [https://packaging.python.org/en/latest/current/] in the Python Packaging User Guide for more information.

1. Distutils 模块介绍

1.1. 概念和术语

1.2. 一个简单的例子

1.3. 通用的 Python 术语

1.4. Distutils 特定的术语

2. 编写安装脚本

2.1. Listing whole packages

2.2. Listing individual modules

2.3. Describing extension modules

2.4. Relationships between Distributions and Packages

2.5. Installing Scripts

2.6. Installing Package Data

2.7. Installing Additional Files

2.8. Additional meta-data

2.9. Debugging the setup script

3. 编写设置脚本的配置文件

4. Creating a Source Distribution

4.1. Specifying the files to distribute

4.2. Manifest-related options

5. Creating Built Distributions

5.1. 创建RPM软件包

5.2. Creating Windows Installers

5.3. Cross-compiling on Windows

5.4. Vista User Access Control (UAC)

6. Python 包索引（PyPI）

6.1. PyPI overview

6.2. Distutils commands

6.3. PyPI package display

7. 例子

7.1. 纯 Python 分发（通过 module）

7.2. 纯 Python 分发（通过 包）

7.3. 单个扩展模块

7.4. 检查一个包

7.5. 读取元数据

8. 扩展 Distutils

8.1. 集成新的命令

8.2. 添加新的发布类型

9. 命令参考

9.1. 安装模块: install 命令族

9.2. 创建源码发行包: sdist 命令

10. API参考引用

10.1. distutils.core — 分发包功能的核心

10.2. distutils.ccompiler — CCompiler基类

10.3. distutils.unixccompiler — Unix C Compiler

10.4. distutils.msvccompiler — Microsoft Compiler

10.5. distutils.bcppcompiler — Borland Compiler

10.6. distutils.cygwincompiler — Cygwin Compiler

10.7. distutils.archive_util — Archiving utilities

10.8. distutils.dep_util — Dependency checking

10.9. distutils.dir_util — Directory tree operations

10.10. distutils.file_util — Single file operations

10.11. distutils.util — Miscellaneous other utility functions

10.12. distutils.dist — The Distribution class

10.13. distutils.extension — The Extension class

10.14. distutils.debug — Distutils debug mode

10.15. distutils.errors — Distutils exceptions

10.16. distutils.fancy_getopt — Wrapper around the standard getopt module

10.17. distutils.filelist — The FileList class

10.18. distutils.log — Simple PEP 282-style logging

10.19. distutils.spawn — Spawn a sub-process

10.20. distutils.sysconfig — System configuration information

10.21. distutils.text_file — The TextFile class

10.22. distutils.version — Version number classes

10.23. distutils.cmd — Abstract base class for Distutils commands

10.24. Creating a new Distutils command

10.25. distutils.command — Individual Distutils commands

10.26. distutils.command.bdist — Build a binary installer

10.27. distutils.command.bdist_packager — Abstract base class for packagers

10.28. distutils.command.bdist_dumb — Build a “dumb” installer

10.29. distutils.command.bdist_msi — Build a Microsoft Installer binary package

10.30. distutils.command.bdist_rpm — Build a binary distribution as a Redhat RPM and SRPM

10.31. distutils.command.bdist_wininst — Build a Windows installer

10.32. distutils.command.sdist — Build a source distribution

10.33. distutils.command.build — Build all files of a package

10.34. distutils.command.build_clib — Build any C libraries in a package

10.35. distutils.command.build_ext — Build any extensions in a package

10.36. distutils.command.build_py — Build the .py/.pyc files of a package

10.37. distutils.command.build_scripts — Build the scripts of a package

10.38. distutils.command.clean — Clean a package build area

10.39. distutils.command.config — Perform package configuration

10.40. distutils.command.install — Install a package

10.41. distutils.command.install_data — Install data files from a package

10.42. distutils.command.install_headers — Install C/C++ header files from a package

10.43. distutils.command.install_lib — Install library files from a package

10.44. distutils.command.install_scripts — Install script files from a package

10.45. distutils.command.register — Register a module with the Python Package Index

10.46. distutils.command.check — Check the meta-data of a package

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

1. Distutils 模块介绍

本文档介绍了使用 Distutils 模块分发你的 Python 模块，主要是针对开发者/分发者的使用的——如果你想了解如何安装 Python 模块，你应该参考这个章节： 安装Python模块（旧版）。

1.1. 概念和术语

Using the Distutils is quite simple, both for module developers and for users/administrators installing third-party modules. As a developer, your responsibilities (apart from writing solid, well-documented and well-tested code, of course!) are:

编写一个设置脚本

(可选)编写设置脚本的配置文件

创建源码的发行版

（可选）创建一个或多个编译好（二进制）的发行版

每个这些任务均已在此文档覆盖.

Not all module developers have access to a multitude of platforms, so it’s not always feasible to expect them to create a multitude of built distributions. It is hoped that a class of intermediaries, called packagers, will arise to address this need. Packagers will take source distributions released by module developers, build them on one or more platforms, and release the resulting built distributions. Thus, users on the most popular platforms will be able to install most popular Python module distributions in the most natural way for their platform, without having to run a single setup script or compile a line of code.

1.2. 一个简单的例子

The setup script is usually quite simple, although since it’s written in Python, there are no arbitrary limits to what you can do with it, though you should be careful about putting arbitrarily expensive operations in your setup script. Unlike, say, Autoconf-style configure scripts, the setup script may be run multiple times in the course of building and installing your module distribution.

If all you want to do is distribute a module called foo, contained in a file foo.py, then your setup script can be as simple as this:

from distutils.core import setup
setup(name='foo',
      version='1.0',
      py_modules=['foo'],
      )

一些观察:

most information that you supply to the Distutils is supplied as keyword arguments to the setup() function

those keyword arguments fall into two categories: package metadata (name, version number) and information about what’s in the package (a list of pure Python modules, in this case)

modules are specified by module name, not filename (the same will hold true for packages and extensions)

it’s recommended that you supply a little more metadata, in particular your name, email address and a URL for the project (see section 编写安装脚本 for an example)

To create a source distribution for this module, you would create a setup script, setup.py, containing the above code, and run this command from a terminal:

python setup.py sdist

Windows用户, 打开命令行窗口 (Start ‣ Accessories) 并且更改命令为:

setup.py sdist

sdist 将会创建文档文件 (例如Unix中的tarball,Windows的zip文件) 包含你的配置文件 setup.py, 你的模块 foo.py. 此文档文件将会命名为 foo-1.0.tar.gz (or .zip), 并且解包到此目录 foo-1.0.

If an end-user wishes to install your foo module, all they have to do is download foo-1.0.tar.gz (or .zip), unpack it, and—from the foo-1.0 directory—run

python setup.py install

which will ultimately copy foo.py to the appropriate directory for third-party modules in their Python installation.

This simple example demonstrates some fundamental concepts of the Distutils. First, both developers and installers have the same basic user interface, i.e. the setup script. The difference is which Distutils commands they use: the sdist command is almost exclusively for module developers, while install is more often for installers (although most developers will want to install their own code occasionally).

If you want to make things really easy for your users, you can create one or more built distributions for them. For instance, if you are running on a Windows machine, and want to make things easy for other Windows users, you can create an executable installer (the most appropriate type of built distribution for this platform) with the bdist_wininst command. For example:

python setup.py bdist_wininst

will create an executable installer, foo-1.0.win32.exe, in the current directory.

Other useful built distribution formats are RPM, implemented by the bdist_rpm command, Solaris pkgtool (bdist_pkgtool), and HP-UX swinstall (bdist_sdux). For example, the following command will create an RPM file called foo-1.0.noarch.rpm:

python setup.py bdist_rpm

(The bdist_rpm command uses the rpm executable, therefore this has to be run on an RPM-based system such as Red Hat Linux, SuSE Linux, or Mandrake Linux.)

You can find out what distribution formats are available at any time by running

python setup.py bdist --help-formats

1.3. 通用的 Python 术语

If you’re reading this document, you probably have a good idea of what modules, extensions, and so forth are. Nevertheless, just to be sure that everyone is operating from a common starting point, we offer the following glossary of common Python terms:

module 模块

the basic unit of code reusability in Python: a block of code imported by some other code. Three types of modules concern us here: pure Python modules, extension modules, and packages.

纯 Python 模块

a module written in Python and contained in a single .py file (and possibly associated .pyc files). Sometimes referred to as a “pure module.”

extension module – 扩展模块

a module written in the low-level language of the Python implementation: C/C++ for Python, Java for Jython. Typically contained in a single dynamically loadable pre-compiled file, e.g. a shared object (.so) file for Python extensions on Unix, a DLL (given the .pyd extension) for Python extensions on Windows, or a Java class file for Jython extensions. (Note that currently, the Distutils only handles C/C++ extensions for Python.)

package – 包

a module that contains other modules; typically contained in a directory in the filesystem and distinguished from other directories by the presence of a file __init__.py.

root package

the root of the hierarchy of packages. (This isn’t really a package, since it doesn’t have an __init__.py file. But we have to call it something.) The vast majority of the standard library is in the root package, as are many small, standalone third-party modules that don’t belong to a larger module collection. Unlike regular packages, modules in the root package can be found in many directories: in fact, every directory listed in sys.path contributes modules to the root package.

1.4. Distutils 特定的术语

The following terms apply more specifically to the domain of distributing Python modules using the Distutils:

module distribution

a collection of Python modules distributed together as a single downloadable resource and meant to be installed en masse. Examples of some well-known module distributions are NumPy, SciPy, Pillow, or mxBase. (This would be called a package, except that term is already taken in the Python context: a single module distribution may contain zero, one, or many Python packages.)

pure module distribution

a module distribution that contains only pure Python modules and packages. Sometimes referred to as a “pure distribution.”

non-pure module distribution

a module distribution that contains at least one extension module. Sometimes referred to as a “non-pure distribution.”

distribution root

the top-level directory of your source tree (or source distribution); the directory where setup.py exists. Generally setup.py will be run from this directory.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

2. 编写安装脚本

The setup script is the centre of all activity in building, distributing, and installing modules using the Distutils. The main purpose of the setup script is to describe your module distribution to the Distutils, so that the various commands that operate on your modules do the right thing. As we saw in section 一个简单的例子 above, the setup script consists mainly of a call to setup(), and most information supplied to the Distutils by the module developer is supplied as keyword arguments to setup().

Here’s a slightly more involved example, which we’ll follow for the next couple of sections: the Distutils’ own setup script. (Keep in mind that although the Distutils are included with Python 1.6 and later, they also have an independent existence so that Python 1.5.2 users can use them to install other module distributions. The Distutils’ own setup script, shown here, is used to install the package into Python 1.5.2.)

#!/usr/bin/env python

from distutils.core import setup

setup(name='Distutils',
      version='1.0',
      description='Python Distribution Utilities',
      author='Greg Ward',
      author_email='gward@python.net',
      url='https://www.python.org/sigs/distutils-sig/',
      packages=['distutils', 'distutils.command'],
     )

There are only two differences between this and the trivial one-file distribution presented in section 一个简单的例子: more metadata, and the specification of pure Python modules by package, rather than by module. This is important since the Distutils consist of a couple of dozen modules split into (so far) two packages; an explicit list of every module would be tedious to generate and difficult to maintain. For more information on the additional meta-data, see section Additional meta-data.

Note that any pathnames (files or directories) supplied in the setup script should be written using the Unix convention, i.e. slash-separated. The Distutils will take care of converting this platform-neutral representation into whatever is appropriate on your current platform before actually using the pathname. This makes your setup script portable across operating systems, which of course is one of the major goals of the Distutils. In this spirit, all pathnames in this document are slash-separated.

This, of course, only applies to pathnames given to Distutils functions. If you, for example, use standard Python functions such as glob.glob() or os.listdir() to specify files, you should be careful to write portable code instead of hardcoding path separators:

glob.glob(os.path.join('mydir', 'subdir', '*.html'))
os.listdir(os.path.join('mydir', 'subdir'))

2.1. Listing whole packages

The packages option tells the Distutils to process (build, distribute, install, etc.) all pure Python modules found in each package mentioned in the packages list. In order to do this, of course, there has to be a correspondence between package names and directories in the filesystem. The default correspondence is the most obvious one, i.e. package distutils is found in the directory distutils relative to the distribution root. Thus, when you say packages = ['foo'] in your setup script, you are promising that the Distutils will find a file foo/__init__.py (which might be spelled differently on your system, but you get the idea) relative to the directory where your setup script lives. If you break this promise, the Distutils will issue a warning but still process the broken package anyway.

If you use a different convention to lay out your source directory, that’s no problem: you just have to supply the package_dir option to tell the Distutils about your convention. For example, say you keep all Python source under lib, so that modules in the “root package” (i.e., not in any package at all) are in lib, modules in the foo package are in lib/foo, and so forth. Then you would put

package_dir = {'': 'lib'}

in your setup script. The keys to this dictionary are package names, and an empty package name stands for the root package. The values are directory names relative to your distribution root. In this case, when you say packages = ['foo'], you are promising that the file lib/foo/__init__.py exists.

Another possible convention is to put the foo package right in lib, the foo.bar package in lib/bar, etc. This would be written in the setup script as

package_dir = {'foo': 'lib'}

A package: dir entry in the package_dir dictionary implicitly applies to all packages below package, so the foo.bar case is automatically handled here. In this example, having packages = ['foo', 'foo.bar'] tells the Distutils to look for lib/__init__.py and lib/bar/__init__.py. (Keep in mind that although package_dir applies recursively, you must explicitly list all packages in packages: the Distutils will not recursively scan your source tree looking for any directory with an __init__.py file.)

2.2. Listing individual modules

For a small module distribution, you might prefer to list all modules rather than listing packages—especially the case of a single module that goes in the “root package” (i.e., no package at all). This simplest case was shown in section 一个简单的例子; here is a slightly more involved example:

py_modules = ['mod1', 'pkg.mod2']

This describes two modules, one of them in the “root” package, the other in the pkg package. Again, the default package/directory layout implies that these two modules can be found in mod1.py and pkg/mod2.py, and that pkg/__init__.py exists as well. And again, you can override the package/directory correspondence using the package_dir option.

2.3. Describing extension modules

Just as writing Python extension modules is a bit more complicated than writing pure Python modules, describing them to the Distutils is a bit more complicated. Unlike pure modules, it’s not enough just to list modules or packages and expect the Distutils to go out and find the right files; you have to specify the extension name, source file(s), and any compile/link requirements (include directories, libraries to link with, etc.).

All of this is done through another keyword argument to setup(), the ext_modules option. ext_modules is just a list of Extension instances, each of which describes a single extension module. Suppose your distribution includes a single extension, called foo and implemented by foo.c. If no additional instructions to the compiler/linker are needed, describing this extension is quite simple:

Extension('foo', ['foo.c'])

The Extension class can be imported from distutils.core along with setup(). Thus, the setup script for a module distribution that contains only this one extension and nothing else might be:

from distutils.core import setup, Extension
setup(name='foo',
      version='1.0',
      ext_modules=[Extension('foo', ['foo.c'])],
      )

The Extension class (actually, the underlying extension-building machinery implemented by the build_ext command) supports a great deal of flexibility in describing Python extensions, which is explained in the following sections.

2.3.1. 扩展名和软件包

The first argument to the Extension constructor is always the name of the extension, including any package names. For example,

Extension('foo', ['src/foo1.c', 'src/foo2.c'])

describes an extension that lives in the root package, while

Extension('pkg.foo', ['src/foo1.c', 'src/foo2.c'])

describes the same extension in the pkg package. The source files and resulting object code are identical in both cases; the only difference is where in the filesystem (and therefore where in Python’s namespace hierarchy) the resulting extension lives.

If you have a number of extensions all in the same package (or all under the same base package), use the ext_package keyword argument to setup(). For example,

setup(...,
      ext_package='pkg',
      ext_modules=[Extension('foo', ['foo.c']),
                   Extension('subpkg.bar', ['bar.c'])],
     )

will compile foo.c to the extension pkg.foo, and bar.c to pkg.subpkg.bar.

2.3.2. Extension source files

The second argument to the Extension constructor is a list of source files. Since the Distutils currently only support C, C++, and Objective-C extensions, these are normally C/C++/Objective-C source files. (Be sure to use appropriate extensions to distinguish C++ source files: .cc and .cpp seem to be recognized by both Unix and Windows compilers.)

However, you can also include SWIG interface (.i) files in the list; the build_ext command knows how to deal with SWIG extensions: it will run SWIG on the interface file and compile the resulting C/C++ file into your extension.

This warning notwithstanding, options to SWIG can be currently passed like this:

setup(...,
      ext_modules=[Extension('_foo', ['foo.i'],
                             swig_opts=['-modern', '-I../include'])],
      py_modules=['foo'],
     )

Or on the commandline like this:

> python setup.py build_ext --swig-opts="-modern -I../include"

On some platforms, you can include non-source files that are processed by the compiler and included in your extension. Currently, this just means Windows message text (.mc) files and resource definition (.rc) files for Visual C++. These will be compiled to binary resource (.res) files and linked into the executable.

2.3.3. Preprocessor options

Three optional arguments to Extension will help if you need to specify include directories to search or preprocessor macros to define/undefine: include_dirs, define_macros, and undef_macros.

For example, if your extension requires header files in the include directory under your distribution root, use the include_dirs option:

Extension('foo', ['foo.c'], include_dirs=['include'])

You can specify absolute directories there; if you know that your extension will only be built on Unix systems with X11R6 installed to /usr, you can get away with

Extension('foo', ['foo.c'], include_dirs=['/usr/include/X11'])

You should avoid this sort of non-portable usage if you plan to distribute your code: it’s probably better to write C code like

#include <X11/Xlib.h>

If you need to include header files from some other Python extension, you can take advantage of the fact that header files are installed in a consistent way by the Distutils install_headers command. For example, the Numerical Python header files are installed (on a standard Unix installation) to /usr/local/include/python1.5/Numerical. (The exact location will differ according to your platform and Python installation.) Since the Python include directory—/usr/local/include/python1.5 in this case—is always included in the search path when building Python extensions, the best approach is to write C code like

#include <Numerical/arrayobject.h>

If you must put the Numerical include directory right into your header search path, though, you can find that directory using the Distutils distutils.sysconfig module:

from distutils.sysconfig import get_python_inc
incdir = os.path.join(get_python_inc(plat_specific=1), 'Numerical')
setup(...,
      Extension(..., include_dirs=[incdir]),
      )

Even though this is quite portable—it will work on any Python installation, regardless of platform—it’s probably easier to just write your C code in the sensible way.

You can define and undefine pre-processor macros with the define_macros and undef_macros options. define_macros takes a list of (name, value) tuples, where name is the name of the macro to define (a string) and value is its value: either a string or None. (Defining a macro FOO to None is the equivalent of a bare #define FOO in your C source: with most compilers, this sets FOO to the string 1.) undef_macros is just a list of macros to undefine.

例如

Extension(...,
          define_macros=[('NDEBUG', '1'),
                         ('HAVE_STRFTIME', None)],
          undef_macros=['HAVE_FOO', 'HAVE_BAR'])

is the equivalent of having this at the top of every C source file:

#define NDEBUG 1
#define HAVE_STRFTIME
#undef HAVE_FOO
#undef HAVE_BAR

2.3.4. Library options

You can also specify the libraries to link against when building your extension, and the directories to search for those libraries. The libraries option is a list of libraries to link against, library_dirs is a list of directories to search for libraries at link-time, and runtime_library_dirs is a list of directories to search for shared (dynamically loaded) libraries at run-time.

For example, if you need to link against libraries known to be in the standard library search path on target systems

Extension(...,
          libraries=['gdbm', 'readline'])

If you need to link with libraries in a non-standard location, you’ll have to include the location in library_dirs:

Extension(...,
          library_dirs=['/usr/X11R6/lib'],
          libraries=['X11', 'Xt'])

(Again, this sort of non-portable construct should be avoided if you intend to distribute your code.)

2.3.5. 其他选项

There are still some other options which can be used to handle special cases.

The optional option is a boolean; if it is true, a build failure in the extension will not abort the build process, but instead simply not install the failing extension.

The extra_objects option is a list of object files to be passed to the linker. These files must not have extensions, as the default extension for the compiler is used.

extra_compile_args and extra_link_args can be used to specify additional command line options for the respective compiler and linker command lines.

export_symbols is only useful on Windows. It can contain a list of symbols (functions or variables) to be exported. This option is not needed when building compiled extensions: Distutils will automatically add initmodule to the list of exported symbols.

The depends option is a list of files that the extension depends on (for example header files). The build command will call the compiler on the sources to rebuild extension if any on this files has been modified since the previous build.

2.4. Relationships between Distributions and Packages

A distribution may relate to packages in three specific ways:

It can require packages or modules.

It can provide packages or modules.

It can obsolete packages or modules.

These relationships can be specified using keyword arguments to the distutils.core.setup() function.

Dependencies on other Python modules and packages can be specified by supplying the requires keyword argument to setup(). The value must be a list of strings. Each string specifies a package that is required, and optionally what versions are sufficient.

To specify that any version of a module or package is required, the string should consist entirely of the module or package name. Examples include 'mymodule' and 'xml.parsers.expat'.

If specific versions are required, a sequence of qualifiers can be supplied in parentheses. Each qualifier may consist of a comparison operator and a version number. The accepted comparison operators are:

<    >    ==
<=   >=   !=

These can be combined by using multiple qualifiers separated by commas (and optional whitespace). In this case, all of the qualifiers must be matched; a logical AND is used to combine the evaluations.

Let’s look at a bunch of examples:

Requires Expression

解释

==1.0

Only version 1.0 is compatible

>1.0, !=1.5.1, <2.0

Any version after 1.0 and before 2.0 is compatible, except 1.5.1

Now that we can specify dependencies, we also need to be able to specify what we provide that other distributions can require. This is done using the provides keyword argument to setup(). The value for this keyword is a list of strings, each of which names a Python module or package, and optionally identifies the version. If the version is not specified, it is assumed to match that of the distribution.

Some examples:

Provides Expression

解释

mypkg

Provide mypkg, using the distribution version

mypkg (1.1)

Provide mypkg version 1.1, regardless of the distribution version

A package can declare that it obsoletes other packages using the obsoletes keyword argument. The value for this is similar to that of the requires keyword: a list of strings giving module or package specifiers. Each specifier consists of a module or package name optionally followed by one or more version qualifiers. Version qualifiers are given in parentheses after the module or package name.

The versions identified by the qualifiers are those that are obsoleted by the distribution being described. If no qualifiers are given, all versions of the named module or package are understood to be obsoleted.

2.5. Installing Scripts

So far we have been dealing with pure and non-pure Python modules, which are usually not run by themselves but imported by scripts.

Scripts are files containing Python source code, intended to be started from the command line. Scripts don’t require Distutils to do anything very complicated. The only clever feature is that if the first line of the script starts with #! and contains the word “python”, the Distutils will adjust the first line to refer to the current interpreter location. By default, it is replaced with the current interpreter location. The --executable (or -e) option will allow the interpreter path to be explicitly overridden.

The scripts option simply is a list of files to be handled in this way. From the PyXML setup script:

setup(...,
      scripts=['scripts/xmlproc_parse', 'scripts/xmlproc_val']
      )

在 3.1 版更改: All the scripts will also be added to the MANIFEST file if no template is provided. See Specifying the files to distribute.

2.6. Installing Package Data

Often, additional files need to be installed into a package. These files are often data that’s closely related to the package’s implementation, or text files containing documentation that might be of interest to programmers using the package. These files are called package data.

Package data can be added to packages using the package_data keyword argument to the setup() function. The value must be a mapping from package name to a list of relative path names that should be copied into the package. The paths are interpreted as relative to the directory containing the package (information from the package_dir mapping is used if appropriate); that is, the files are expected to be part of the package in the source directories. They may contain glob patterns as well.

The path names may contain directory portions; any necessary directories will be created in the installation.

For example, if a package should contain a subdirectory with several data files, the files can be arranged like this in the source tree:

setup.py
src/
    mypkg/
        __init__.py
        module.py
        data/
            tables.dat
            spoons.dat
            forks.dat

The corresponding call to setup() might be:

setup(...,
      packages=['mypkg'],
      package_dir={'mypkg': 'src/mypkg'},
      package_data={'mypkg': ['data/*.dat']},
      )

在 3.1 版更改: All the files that match package_data will be added to the MANIFEST file if no template is provided. See Specifying the files to distribute.

2.7. Installing Additional Files

The data_files option can be used to specify additional files needed by the module distribution: configuration files, message catalogs, data files, anything which doesn’t fit in the previous categories.

data_files specifies a sequence of (directory, files) pairs in the following way:

setup(...,
      data_files=[('bitmaps', ['bm/b1.gif', 'bm/b2.gif']),
                  ('config', ['cfg/data.cfg']),
                  ('/etc/init.d', ['init-script'])]
     )

Note that you can specify the directory names where the data files will be installed, but you cannot rename the data files themselves.

Each (directory, files) pair in the sequence specifies the installation directory and the files to install there. If directory is a relative path, it is interpreted relative to the installation prefix (Python’s sys.prefix for pure-Python packages, sys.exec_prefix for packages that contain extension modules). Each file name in files is interpreted relative to the setup.py script at the top of the package source distribution. No directory information from files is used to determine the final location of the installed file; only the name of the file is used.

You can specify the data_files options as a simple sequence of files without specifying a target directory, but this is not recommended, and the install command will print a warning in this case. To install data files directly in the target directory, an empty string should be given as the directory.

在 3.1 版更改: All the files that match data_files will be added to the MANIFEST file if no template is provided. See Specifying the files to distribute.

2.8. Additional meta-data

The setup script may include additional meta-data beyond the name and version. This information includes:

元数据

描述

值

注释

name

包名称

短字符串

(1)

version

此发布的版本

短字符串

(1)(2)

author

软件包作者的姓名

短字符串

(3)

author_email

软件包的作者的电子邮件地址

电子邮件地址

(3)

maintainer

软件包维护者的名字

短字符串

(3)

maintainer_email

软件包维护者的电子邮件地址

电子邮件地址

(3)

url

软件包的网址

网址

(1)

description

软件包的简短摘要说明

短字符串

long_description

软件包的详细说明

长字符串

(5)

download_url

可以下载软件包的网址

网址

(4)

classifiers

分类列表

字符串列表

(4)

platforms

平台清单

字符串列表

license

软件包许可证

短字符串

(6)

注释:

These fields are required.

It is recommended that versions take the form major.minor[.patch[.sub]].

Either the author or the maintainer must be identified. If maintainer is provided, distutils lists it as the author in PKG-INFO.

These fields should not be used if your package is to be compatible with Python versions prior to 2.2.3 or 2.3. The list is available from the PyPI website [https://pypi.org/].

The long_description field is used by PyPI when you are registering a package, to build its home page.

The license field is a text indicating the license covering the package where the license is not a selection from the “License” Trove classifiers. See the Classifier field. Notice that there’s a licence distribution option which is deprecated but still acts as an alias for license.

‘short string’

A single line of text, not more than 200 characters.

‘long string’

Multiple lines of plain text in reStructuredText format (see http://docutils.sourceforge.net/).

‘list of strings’

See below.

Encoding the version information is an art in itself. Python packages generally adhere to the version format major.minor[.patch][sub]. The major number is 0 for initial, experimental releases of software. It is incremented for releases that represent major milestones in a package. The minor number is incremented when important new features are added to the package. The patch number increments when bug-fix releases are made. Additional trailing version information is sometimes used to indicate sub-releases. These are “a1,a2,…,aN” (for alpha releases, where functionality and API may change), “b1,b2,…,bN” (for beta releases, which only fix bugs) and “pr1,pr2,…,prN” (for final pre-release release testing). Some examples:

0.1.0

the first, experimental release of a package

1.0.1a2

the second alpha release of the first patch version of 1.0

classifiers are specified in a Python list:

setup(...,
      classifiers=[
          'Development Status :: 4 - Beta',
          'Environment :: Console',
          'Environment :: Web Environment',
          'Intended Audience :: End Users/Desktop',
          'Intended Audience :: Developers',
          'Intended Audience :: System Administrators',
          'License :: OSI Approved :: Python Software Foundation License',
          'Operating System :: MacOS :: MacOS X',
          'Operating System :: Microsoft :: Windows',
          'Operating System :: POSIX',
          'Programming Language :: Python',
          'Topic :: Communications :: Email',
          'Topic :: Office/Business',
          'Topic :: Software Development :: Bug Tracking',
          ],
      )

2.9. Debugging the setup script

Sometimes things go wrong, and the setup script doesn’t do what the developer wants.

Distutils catches any exceptions when running the setup script, and print a simple error message before the script is terminated. The motivation for this behaviour is to not confuse administrators who don’t know much about Python and are trying to install a package. If they get a big long traceback from deep inside the guts of Distutils, they may think the package or the Python installation is broken because they don’t read all the way down to the bottom and see that it’s a permission problem.

On the other hand, this doesn’t help the developer to find the cause of the failure. For this purpose, the DISTUTILS_DEBUG environment variable can be set to anything except an empty string, and distutils will now print detailed information about what it is doing, dump the full traceback when an exception occurs, and print the whole command line when an external program (like a C compiler) fails.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

3. 编写设置脚本的配置文件

通常，在 事前 就写出构建发布包所需的一切是不可能的：你可能需要从用户或者用户的系统获取一些信息，才能继续下去。 只要这些信息相当简单 —— 例如一个用于搜索 C 头文件或库的目录列表 —— 那么提供配置文件 setup.cfg 供用户配置就是一个低成本且方便的解决方式。 配置文件还允许你为任何命令选项提供默认值，而安装器可以通过命令行或编辑配置文件来覆盖这些默认值。

安装配置文件是在安装脚本和安装脚本命令行之间一个适当的折衷方式 — 安装脚本在理想情况下应当不受安装者的控制 1 — 而安装脚本命令行则在你的控制范围之外且完全取决于安装者的选择。 实际上，setup.cfg (以及目标系统上的其他任何 Distutils 配置文件) 是在配置脚本之后、命令行之前被处理。 这导致了几个有用的后果:

安装者可以通过编辑 setup.cfg 来覆盖你放在 setup.py 中的配置

你可以为无法在 setup.py 中方便设置的选项提供非标准的默认值

安装者可以使用 setup.py 的命令行选项来覆盖 setup.cfg 中的一切

配置文件的基本语法很简单:

[command]
option=value
...

其中 command 是一个 Distutils 命令 (例如 build_py, install)，而 option 是该命令所支持的某个选项。 可以为每个命令提供任意数量的选项，并且可以在文件中包括任意数量的命令组。 空白行会被忽略，以一个 '#' 开始的注释行也是如此。 长选项值可以简单地通过缩进后续行的方式被拆分为多行。

你可以用通用的 --help 选项找出特定命令所支持的选项列表，例如

$ python setup.py --help build_ext
[...]
Options for 'build_ext' command:
  --build-lib (-b)     directory for compiled extension modules
  --build-temp (-t)    directory for temporary files (build by-products)
  --inplace (-i)       ignore build-lib and put compiled extensions into the
                       source directory alongside your pure Python modules
  --include-dirs (-I)  list of directories to search for header files
  --define (-D)        C preprocessor macros to define
  --undef (-U)         C preprocessor macros to undefine
  --swig-opts          list of SWIG command line options
[...]

请注意在命令行中拼写为 --foo-bar 的选项在配置文件中会拼写为 foo_bar。

例如，假设你希望你的扩展在“原地”构建 — 就是说你有一个扩展 pkg.ext，你希望编译出的扩展文件 (例如在 Unix 上为 ext.so) 放在与你的纯 Python 模块 pkg.mod1 和 pkg.mod2 相同的源目录中。 你总是可以在命令行中使用 --inplace 选项来确保这一点:

python setup.py build_ext --inplace

但是这要求你总是显式地指定 build_ext 命令，并且记得提供 --inplace。 一个更容易的方式是通过将其编码在此发布包的配置文件 setup.cfg 中，“设置并忘记”该选项。

[build_ext]
inplace=1

这将影响此模块发布包的所有构建，不论你是否显式指定 build_ext。 如果你在你的源发布包中包括了 setup.cfg，它还将影响最终用户的构建 — 对此选项来说这可能不是个好主意，因为总是原地构建扩展会破坏模块发布包的安装。 不过在某些特殊情况下，模块是在其安装目录中被构建的，因此这可能会是个有用的功能。 （但是，发布预期在其安装目录中被构建的扩展几乎总是一个坏主意。）

另一个例子：特定的命令会接受许多在多次运行中都不发生变化的选项；例如，bdist_rpm 需要知道为创建 RPM 发布包生成 “spec” 文件所要求的所有信息。 这些信息有的来自安装脚本，有的由 Distutils 自动生成（例如已安装文件列表）。 但有的则必须作为 bdist_rpm 的选项提供，每次运行时都在命令行中完成将会非常繁琐。 因此，这里提供 Distutils 本身的 setup.cfg 中的一段代码:

[bdist_rpm]
release = 1
packager = Greg Ward <gward@python.net>
doc_files = CHANGES.txt
            README.txt
            USAGE.txt
            doc/
            examples/

请注意 doc_files 选项只是一个空格分隔以提高可读性的多行字符串。

参见

“安装 Python 模块” 中的 Syntax of config files

有关配置文件的更多信息可在系统管理员手册中查看。

备注

1

在 Distutils 完全支持自动配置之前，这一理想可能是无法实现的。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

4. Creating a Source Distribution

As shown in section 一个简单的例子, you use the sdist command to create a source distribution. In the simplest case,

python setup.py sdist

(assuming you haven’t specified any sdist options in the setup script or config file), sdist creates the archive of the default format for the current platform. The default format is a gzip’ed tar file (.tar.gz) on Unix, and ZIP file on Windows.

You can specify as many formats as you like using the --formats option, for example:

python setup.py sdist --formats=gztar,zip

to create a gzipped tarball and a zip file. The available formats are:

格式

描述

注释

zip

zip 文件 (.zip)

(1),(3)

gztar

gzip’ed tar 文件 (.tar.gz)

(2)

bztar

bzip2’ed tar 文件 (.tar.bz2)

xztar

xz’ed tar 文件 (.tar.xz)

ztar

压缩 tar 文件 (.tar.Z)

(4)

tar

tar 文件 (.tar)

在 3.5 版更改: 添加了对 xztar 格式的支持

注释:

默认Windows

默认 Unix

requires either external zip utility or zipfile module (part of the standard Python library since Python 1.6)

requires the compress program. Notice that this format is now pending for deprecation and will be removed in the future versions of Python.

When using any tar format (gztar, bztar, xztar, ztar or tar), under Unix you can specify the owner and group names that will be set for each member of the archive.

For example, if you want all files of the archive to be owned by root:

python setup.py sdist --owner=root --group=root

4.1. Specifying the files to distribute

If you don’t supply an explicit list of files (or instructions on how to generate one), the sdist command puts a minimal default set into the source distribution:

all Python source files implied by the py_modules and packages options

all C source files mentioned in the ext_modules or libraries options

scripts identified by the scripts option See Installing Scripts.

anything that looks like a test script: test/test*.py (currently, the Distutils don’t do anything with test scripts except include them in source distributions, but in the future there will be a standard for testing Python module distributions)

README.txt (or README), setup.py (or whatever you called your setup script), and setup.cfg

all files that matches the package_data metadata. See Installing Package Data.

all files that matches the data_files metadata. See Installing Additional Files.

Sometimes this is enough, but usually you will want to specify additional files to distribute. The typical way to do this is to write a manifest template, called MANIFEST.in by default. The manifest template is just a list of instructions for how to generate your manifest file, MANIFEST, which is the exact list of files to include in your source distribution. The sdist command processes this template and generates a manifest based on its instructions and what it finds in the filesystem.

If you prefer to roll your own manifest file, the format is simple: one filename per line, regular files (or symlinks to them) only. If you do supply your own MANIFEST, you must specify everything: the default set of files described above does not apply in this case.

在 3.1 版更改: An existing generated MANIFEST will be regenerated without sdist comparing its modification time to the one of MANIFEST.in or setup.py.

在 3.1.3 版更改: MANIFEST files start with a comment indicating they are generated. Files without this comment are not overwritten or removed.

在 3.2.2 版更改: sdist will read a MANIFEST file if no MANIFEST.in exists, like it used to do.

The manifest template has one command per line, where each command specifies a set of files to include or exclude from the source distribution. For an example, again we turn to the Distutils’ own manifest template:

include *.txt
recursive-include examples *.txt *.py
prune examples/sample?/build

The meanings should be fairly clear: include all files in the distribution root matching *.txt, all files anywhere under the examples directory matching *.txt or *.py, and exclude all directories matching examples/sample?/build. All of this is done after the standard include set, so you can exclude files from the standard set with explicit instructions in the manifest template. (Or, you can use the --no-defaults option to disable the standard set entirely.) There are several other commands available in the manifest template mini-language; see section 创建源码发行包: sdist 命令.

The order of commands in the manifest template matters: initially, we have the list of default files as described above, and each command in the template adds to or removes from that list of files. Once we have fully processed the manifest template, we remove files that should not be included in the source distribution:

all files in the Distutils “build” tree (default build/)

all files in directories named RCS, CVS, .svn, .hg, .git, .bzr or _darcs

Now we have our complete list of files, which is written to the manifest for future reference, and then used to build the source distribution archive(s).

You can disable the default set of included files with the --no-defaults option, and you can disable the standard exclude set with --no-prune.

Following the Distutils’ own manifest template, let’s trace how the sdist command builds the list of files to include in the Distutils source distribution:

include all Python source files in the distutils and distutils/command subdirectories (because packages corresponding to those two directories were mentioned in the packages option in the setup script—see section 编写安装脚本)

include README.txt, setup.py, and setup.cfg (standard files)

include test/test*.py (standard files)

include *.txt in the distribution root (this will find README.txt a second time, but such redundancies are weeded out later)

include anything matching *.txt or *.py in the sub-tree under examples,

exclude all files in the sub-trees starting at directories matching examples/sample?/build—this may exclude files included by the previous two steps, so it’s important that the prune command in the manifest template comes after the recursive-include command

exclude the entire build tree, and any RCS, CVS, .svn, .hg, .git, .bzr and _darcs directories

Just like in the setup script, file and directory names in the manifest template should always be slash-separated; the Distutils will take care of converting them to the standard representation on your platform. That way, the manifest template is portable across operating systems.

4.2. Manifest-related options

The normal course of operations for the sdist command is as follows:

if the manifest file (MANIFEST by default) exists and the first line does not have a comment indicating it is generated from MANIFEST.in, then it is used as is, unaltered

if the manifest file doesn’t exist or has been previously automatically generated, read MANIFEST.in and create the manifest

if neither MANIFEST nor MANIFEST.in exist, create a manifest with just the default file set

use the list of files now in MANIFEST (either just generated or read in) to create the source distribution archive(s)

There are a couple of options that modify this behaviour. First, use the --no-defaults and --no-prune to disable the standard “include” and “exclude” sets.

Second, you might just want to (re)generate the manifest, but not create a source distribution:

python setup.py sdist --manifest-only

-o is a shortcut for --manifest-only.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

5. Creating Built Distributions

A “built distribution” is what you’re probably used to thinking of either as a “binary package” or an “installer” (depending on your background). It’s not necessarily binary, though, because it might contain only Python source code and/or byte-code; and we don’t call it a package, because that word is already spoken for in Python. (And “installer” is a term specific to the world of mainstream desktop systems.)

A built distribution is how you make life as easy as possible for installers of your module distribution: for users of RPM-based Linux systems, it’s a binary RPM; for Windows users, it’s an executable installer; for Debian-based Linux users, it’s a Debian package; and so forth. Obviously, no one person will be able to create built distributions for every platform under the sun, so the Distutils are designed to enable module developers to concentrate on their specialty—writing code and creating source distributions—while an intermediary species called packagers springs up to turn source distributions into built distributions for as many platforms as there are packagers.

Of course, the module developer could be their own packager; or the packager could be a volunteer “out there” somewhere who has access to a platform which the original developer does not; or it could be software periodically grabbing new source distributions and turning them into built distributions for as many platforms as the software has access to. Regardless of who they are, a packager uses the setup script and the bdist command family to generate built distributions.

As a simple example, if I run the following command in the Distutils source tree:

python setup.py bdist

then the Distutils builds my module distribution (the Distutils itself in this case), does a “fake” installation (also in the build directory), and creates the default type of built distribution for my platform. The default format for built distributions is a “dumb” tar file on Unix, and a simple executable installer on Windows. (That tar file is considered “dumb” because it has to be unpacked in a specific location to work.)

Thus, the above command on a Unix system creates Distutils-1.0.plat.tar.gz; unpacking this tarball from the right place installs the Distutils just as though you had downloaded the source distribution and run python setup.py install. (The “right place” is either the root of the filesystem or Python’s prefix directory, depending on the options given to the bdist_dumb command; the default is to make dumb distributions relative to prefix.)

Obviously, for pure Python distributions, this isn’t any simpler than just running python setup.py install—but for non-pure distributions, which include extensions that would need to be compiled, it can mean the difference between someone being able to use your extensions or not. And creating “smart” built distributions, such as an RPM package or an executable installer for Windows, is far more convenient for users even if your distribution doesn’t include any extensions.

The bdist command has a --formats option, similar to the sdist command, which you can use to select the types of built distribution to generate: for example,

python setup.py bdist --format=zip

would, when run on a Unix system, create Distutils-1.0.plat.zip—again, this archive would be unpacked from the root directory to install the Distutils.

The available formats for built distributions are:

格式

描述

注释

gztar

gzipped tar 文件 (.tar.gz)

(1)

bztar

bzipped tar 文件 (.tar.bz2)

xztar

xzipped tar 文件 (.tar.xz)

ztar

压缩 tar 文件 (.tar.Z)

(3)

tar

tar 文件 (.tar)

zip

zip 文件 (.zip)

(2),(4)

rpm

RPM

(5)

pkgtool

Solaris pkgtool

sdux

HP-UX swinstall

wininst

Windows的自解压ZIP文件

(4)

msi

Microsoft安装程序。

在 3.5 版更改: 添加了对 xztar 格式的支持

注释:

默认 Unix

默认Windows

requires external compress utility.

requires either external zip utility or zipfile module (part of the standard Python library since Python 1.6)

requires external rpm utility, version 3.0.4 or better (use rpm --version to find out which version you have)

You don’t have to use the bdist command with the --formats option; you can also use the command that directly implements the format you’re interested in. Some of these bdist “sub-commands” actually generate several similar formats; for instance, the bdist_dumb command generates all the “dumb” archive formats (tar, gztar, bztar, xztar, ztar, and zip), and bdist_rpm generates both binary and source RPMs. The bdist sub-commands, and the formats generated by each, are:

命令

格式

bdist_dumb

tar, gztar, bztar, xztar, ztar, zip

bdist_rpm

rpm, srpm

bdist_wininst

wininst

bdist_msi

msi

The following sections give details on the individual bdist_* commands.

5.1. 创建RPM软件包

The RPM format is used by many popular Linux distributions, including Red Hat, SuSE, and Mandrake. If one of these (or any of the other RPM-based Linux distributions) is your usual environment, creating RPM packages for other users of that same distribution is trivial. Depending on the complexity of your module distribution and differences between Linux distributions, you may also be able to create RPMs that work on different RPM-based distributions.

The usual way to create an RPM of your module distribution is to run the bdist_rpm command:

python setup.py bdist_rpm

or the bdist command with the --format option:

python setup.py bdist --formats=rpm

The former allows you to specify RPM-specific options; the latter allows you to easily specify multiple formats in one run. If you need to do both, you can explicitly specify multiple bdist_* commands and their options:

python setup.py bdist_rpm --packager="John Doe <jdoe@example.org>" \
                bdist_wininst --target-version="2.0"

Creating RPM packages is driven by a .spec file, much as using the Distutils is driven by the setup script. To make your life easier, the bdist_rpm command normally creates a .spec file based on the information you supply in the setup script, on the command line, and in any Distutils configuration files. Various options and sections in the .spec file are derived from options in the setup script as follows:

RPM .spec 文件配置或选项

Distutils安装脚本选项

名称

name

摘要（在序言中）

description

版本

version

供应商

author 和 author_email, 或 — & maintainer 和 maintainer_email

Copyright

license

Url

url

%d描述（部分）

long_description

Additionally, there are many options in .spec files that don’t have corresponding options in the setup script. Most of these are handled through options to the bdist_rpm command as follows:

RPM .spec 文件配置或选项

bdist_rpm 选项

默认值

发布版本

release

“1”

组

group

“Development/Libraries”

供应商

vendor

（同上）

打包

packager

(none)

提供

provides

(none)

需求

requires

(none)

冲突

conflicts

(none)

淘汰

obsoletes

(none)

Distribution

distribution_name

(none)

构建要求

build_requires

(none)

Icon

icon

(none)

Obviously, supplying even a few of these options on the command-line would be tedious and error-prone, so it’s usually best to put them in the setup configuration file, setup.cfg—see section 编写设置脚本的配置文件. If you distribute or package many Python module distributions, you might want to put options that apply to all of them in your personal Distutils configuration file (~/.pydistutils.cfg). If you want to temporarily disable this file, you can pass the --no-user-cfg option to setup.py.

There are three steps to building a binary RPM package, all of which are handled automatically by the Distutils:

create a .spec file, which describes the package (analogous to the Distutils setup script; in fact, much of the information in the setup script winds up in the .spec file)

create the source RPM

create the “binary” RPM (which may or may not contain binary code, depending on whether your module distribution contains Python extensions)

Normally, RPM bundles the last two steps together; when you use the Distutils, all three steps are typically bundled together.

If you wish, you can separate these three steps. You can use the --spec-only option to make bdist_rpm just create the .spec file and exit; in this case, the .spec file will be written to the “distribution directory”—normally dist/, but customizable with the --dist-dir option. (Normally, the .spec file winds up deep in the “build tree,” in a temporary directory created by bdist_rpm.)

5.2. Creating Windows Installers

Executable installers are the natural format for binary distributions on Windows. They display a nice graphical user interface, display some information about the module distribution to be installed taken from the metadata in the setup script, let the user select a few options, and start or cancel the installation.

Since the metadata is taken from the setup script, creating Windows installers is usually as easy as running:

python setup.py bdist_wininst

or the bdist command with the --formats option:

python setup.py bdist --formats=wininst

If you have a pure module distribution (only containing pure Python modules and packages), the resulting installer will be version independent and have a name like foo-1.0.win32.exe. These installers can even be created on Unix platforms or Mac OS X.

If you have a non-pure distribution, the extensions can only be created on a Windows platform, and will be Python version dependent. The installer filename will reflect this and now has the form foo-1.0.win32-py2.0.exe. You have to create a separate installer for every Python version you want to support.

The installer will try to compile pure modules into bytecode after installation on the target system in normal and optimizing mode. If you don’t want this to happen for some reason, you can run the bdist_wininst command with the --no-target-compile and/or the --no-target-optimize option.

By default the installer will display the cool “Python Powered” logo when it is run, but you can also supply your own 152x261 bitmap which must be a Windows .bmp file with the --bitmap option.

The installer will also display a large title on the desktop background window when it is run, which is constructed from the name of your distribution and the version number. This can be changed to another text by using the --title option.

The installer file will be written to the “distribution directory” — normally dist/, but customizable with the --dist-dir option.

5.3. Cross-compiling on Windows

Starting with Python 2.6, distutils is capable of cross-compiling between Windows platforms. In practice, this means that with the correct tools installed, you can use a 32bit version of Windows to create 64bit extensions and vice-versa.

To build for an alternate platform, specify the --plat-name option to the build command. Valid values are currently ‘win32’, ‘win-amd64’ and ‘win-ia64’. For example, on a 32bit version of Windows, you could execute:

python setup.py build --plat-name=win-amd64

to build a 64bit version of your extension. The Windows Installers also support this option, so the command:

python setup.py build --plat-name=win-amd64 bdist_wininst

would create a 64bit installation executable on your 32bit version of Windows.

To cross-compile, you must download the Python source code and cross-compile Python itself for the platform you are targeting - it is not possible from a binary installation of Python (as the .lib etc file for other platforms are not included.) In practice, this means the user of a 32 bit operating system will need to use Visual Studio 2008 to open the PCBuild/PCbuild.sln solution in the Python source tree and build the “x64” configuration of the ‘pythoncore’ project before cross-compiling extensions is possible.

Note that by default, Visual Studio 2008 does not install 64bit compilers or tools. You may need to reexecute the Visual Studio setup process and select these tools (using Control Panel->[Add/Remove] Programs is a convenient way to check or modify your existing install.)

5.3.1. The Postinstallation script

Starting with Python 2.3, a postinstallation script can be specified with the --install-script option. The basename of the script must be specified, and the script filename must also be listed in the scripts argument to the setup function.

This script will be run at installation time on the target system after all the files have been copied, with argv[1] set to -install, and again at uninstallation time before the files are removed with argv[1] set to -remove.

The installation script runs embedded in the windows installer, every output (sys.stdout, sys.stderr) is redirected into a buffer and will be displayed in the GUI after the script has finished.

Some functions especially useful in this context are available as additional built-in functions in the installation script.

directory_created(path)

file_created(path)

These functions should be called when a directory or file is created by the postinstall script at installation time. It will register path with the uninstaller, so that it will be removed when the distribution is uninstalled. To be safe, directories are only removed if they are empty.

get_special_folder_path(csidl_string)

This function can be used to retrieve special folder locations on Windows like the Start Menu or the Desktop. It returns the full path to the folder. csidl_string must be one of the following strings:

"CSIDL_APPDATA"

"CSIDL_COMMON_STARTMENU"
"CSIDL_STARTMENU"

"CSIDL_COMMON_DESKTOPDIRECTORY"
"CSIDL_DESKTOPDIRECTORY"

"CSIDL_COMMON_STARTUP"
"CSIDL_STARTUP"

"CSIDL_COMMON_PROGRAMS"
"CSIDL_PROGRAMS"

"CSIDL_FONTS"

If the folder cannot be retrieved, OSError is raised.

Which folders are available depends on the exact Windows version, and probably also the configuration. For details refer to Microsoft’s documentation of the SHGetSpecialFolderPath() function.

create_shortcut(target, description, filename[, arguments[, workdir[, iconpath[, iconindex]]]])

This function creates a shortcut. target is the path to the program to be started by the shortcut. description is the description of the shortcut. filename is the title of the shortcut that the user will see. arguments specifies the command line arguments, if any. workdir is the working directory for the program. iconpath is the file containing the icon for the shortcut, and iconindex is the index of the icon in the file iconpath. Again, for details consult the Microsoft documentation for the IShellLink interface.

5.4. Vista User Access Control (UAC)

Starting with Python 2.6, bdist_wininst supports a --user-access-control option. The default is ‘none’ (meaning no UAC handling is done), and other valid values are ‘auto’ (meaning prompt for UAC elevation if Python was installed for all users) and ‘force’ (meaning always prompt for elevation).

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

6. Python 包索引（PyPI）

The Python Package Index (PyPI) [https://pypi.org] stores meta-data describing distributions packaged with distutils, as well as package data like distribution files if a package author wishes.

Distutils provides the register and upload commands for pushing meta-data and distribution files to PyPI, respectively. See Distutils commands for information on these commands.

6.1. PyPI overview

PyPI lets you submit any number of versions of your distribution to the index. If you alter the meta-data for a particular version, you can submit it again and the index will be updated.

PyPI holds a record for each (name, version) combination submitted. The first user to submit information for a given name is designated the Owner of that name. Changes can be submitted through the register command or through the web interface. Owners can designate other users as Owners or Maintainers. Maintainers can edit the package information, but not designate new Owners or Maintainers.

By default PyPI displays only the newest version of a given package. The web interface lets one change this default behavior and manually select which versions to display and hide.

For each version, PyPI displays a home page. The home page is created from the long_description which can be submitted via the register command. See PyPI package display for more information.

6.2. Distutils commands

Distutils exposes two commands for submitting package data to PyPI: the register command for submitting meta-data to PyPI and the upload command for submitting distribution files. Both commands read configuration data from a special file called a .pypirc file.

6.2.1. The register command

The distutils command register is used to submit your distribution’s meta-data to an index server. It is invoked as follows:

python setup.py register

Distutils will respond with the following prompt:

running register
We need to know who you are, so please choose either:
    1. use your existing login,
    2. register as a new user,
    3. have the server generate a new password for you (and email it to you), or
    4. quit
Your selection [default 1]:

Note: if your username and password are saved locally, you will not see this menu. Also, refer to The .pypirc file for how to store your credentials in a .pypirc file.

If you have not registered with PyPI, then you will need to do so now. You should choose option 2, and enter your details as required. Soon after submitting your details, you will receive an email which will be used to confirm your registration.

Once you are registered, you may choose option 1 from the menu. You will be prompted for your PyPI username and password, and register will then submit your meta-data to the index.

See Additional command options for options to the register command.

6.2.2. The upload command

The distutils command upload pushes the distribution files to PyPI.

The command is invoked immediately after building one or more distribution files. For example, the command

python setup.py sdist bdist_wininst upload

will cause the source distribution and the Windows installer to be uploaded to PyPI. Note that these will be uploaded even if they are built using an earlier invocation of setup.py, but that only distributions named on the command line for the invocation including the upload command are uploaded.

If a register command was previously called in the same command, and if the password was entered in the prompt, upload will reuse the entered password. This is useful if you do not want to store a password in clear text in a .pypirc file.

You can use the --sign option to tell upload to sign each uploaded file using GPG (GNU Privacy Guard). The gpg program must be available for execution on the system PATH. You can also specify which key to use for signing using the --identity=name option.

See Additional command options for additional options to the upload command.

6.2.3. Additional command options

This section describes options common to both the register and upload commands.

The --repository or -r option lets you specify a PyPI server different from the default. For example:

python setup.py sdist bdist_wininst upload -r https://example.com/pypi

For convenience, a name can be used in place of the URL when the .pypirc file is configured to do so. For example:

python setup.py register -r other

See The .pypirc file for more information on defining alternate servers.

The --show-response option displays the full response text from the PyPI server, which is useful when debugging problems with registering and uploading.

6.2.4. The .pypirc file

The register and upload commands both check for the existence of a .pypirc file at the location $HOME/.pypirc. If this file exists, the command uses the username, password, and repository URL configured in the file. The format of a .pypirc file is as follows:

[distutils]
index-servers =
    pypi

[pypi]
repository: <repository-url>
username: <username>
password: <password>

The distutils section defines an index-servers variable that lists the name of all sections describing a repository.

Each section describing a repository defines three variables:

repository, that defines the url of the PyPI server. Defaults to

https://upload.pypi.org/legacy/.

username, which is the registered username on the PyPI server.

password, that will be used to authenticate. If omitted the user

will be prompt to type it when needed.

If you want to define another server a new section can be created and listed in the index-servers variable:

[distutils]
index-servers =
    pypi
    other

[pypi]
repository: <repository-url>
username: <username>
password: <password>

[other]
repository: https://example.com/pypi
username: <username>
password: <password>

This allows the register and upload commands to be called with the --repository option as described in Additional command options.

Specifically, you might want to add the PyPI Test Repository [https://wiki.python.org/moin/TestPyPI] to your .pypirc to facilitate testing before doing your first upload to PyPI itself.

6.3. PyPI package display

The long_description field plays a special role at PyPI. It is used by the server to display a home page for the registered package.

If you use the reStructuredText [http://docutils.sourceforge.net/rst.html] syntax for this field, PyPI will parse it and display an HTML output for the package home page.

The long_description field can be attached to a text file located in the package:

from distutils.core import setup

with open('README.txt') as file:
    long_description = file.read()

setup(name='Distutils',
      long_description=long_description)

In that case, README.txt is a regular reStructuredText text file located in the root of the package besides setup.py.

To prevent registering broken reStructuredText content, you can use the rst2html program that is provided by the docutils package and check the long_description from the command line:

$ python setup.py --long-description | rst2html.py > output.html

docutils will display a warning if there’s something wrong with your syntax. Because PyPI applies additional checks (e.g. by passing --no-raw to rst2html.py in the command above), being able to run the command above without warnings does not guarantee that PyPI will convert the content successfully.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

7. 例子

本章节提供几个基础示例，来帮助用户入门 distutils。关于使用 distutils 的额外信息可以参考 Distutils Cookbook。

参见

Distutils Cookbook [https://wiki.python.org/moin/Distutils/Cookbook]

一套展示如何更好地控制和使用 distutils 的方法。

7.1. 纯 Python 分发（通过 module）

如果你要分发一组模块，特别是它们不在特定的包中，你可以在配置脚本中使用 py_modules 选项单独指定它们。

最简单的情况下，你只用关心两个文件：一个配置脚本，和单个你要分发的模块，这个示例中的 foo.py: ：

<root>/
        setup.py
        foo.py

（在本章节的所有图中，<root> 表示分发根目录。）这种情况下的一个最小配置脚本是：

from distutils.core import setup
setup(name='foo',
      version='1.0',
      py_modules=['foo'],
      )

注意分发的包名用 name 选项单独指定，没有规定它必须和包中单独的模块同名（虽然这也是个可以遵循的好习惯）。然而，分发名用来生成文件名，所以你应该坚持用字母、数字、下划线和连词号。

因为 py_modules 是个列表，你当然可以指定多个模块，比如，如果你要分发模块 foo 和 bar，你的配置可能是这样：

<root>/
        setup.py
        foo.py
        bar.py

并且配置脚本是：

from distutils.core import setup
setup(name='foobar',
      version='1.0',
      py_modules=['foo', 'bar'],
      )

你可以把模块源文件放进另一个目录，但是如果你有足够的模块，也许用包指定模块更简单，而不是单独列出它们。

7.2. 纯 Python 分发（通过 包）

如果你有超过一组模块要分发，特别是它们在不同的包中，也许指定整个包更简单，而不是指定单独的模块。这样即使你的模块不在一个包中也有效；你可以直接令 Distutils 来从根包来处理模块，并且这样对任何其他包也有效（除非你不需要 __init__.py 文件）。

上一个示例的配置脚本也可以写成：

from distutils.core import setup
setup(name='foobar',
      version='1.0',
      packages=[''],
      )

（空字符串表示根包。）

如果两个文件移动到子目录，但是依然在根包中，如：

<root>/
        setup.py
        src/      foo.py
                  bar.py

那么你依然需要指定根包，但是你还需要告诉 Distutils 根包中的源文件在哪：

from distutils.core import setup
setup(name='foobar',
      version='1.0',
      package_dir={'': 'src'},
      packages=[''],
      )

更一般地，你要分发多个在同一个包（或者子包）中的模块。举个例子，假设 foo 和 bar 模块属于 foobar 包，排布源文件树的一种方式是：

<root>/
        setup.py
        foobar/
                 __init__.py
                 foo.py
                 bar.py

这其实是 Distutils 默认的排布，也是你的配置脚本中需要的工作量最小的。

from distutils.core import setup
setup(name='foobar',
      version='1.0',
      packages=['foobar'],
      )

如果你要把模块放到没有按照它们的包名命名的目录里，那你需要再次使用 package_dir 选项。比如，如果 src 目录包含包 foobar 中的模块：

<root>/
        setup.py
        src/
                 __init__.py
                 foo.py
                 bar.py

一个合适的配置脚本可以是：

from distutils.core import setup
setup(name='foobar',
      version='1.0',
      package_dir={'foobar': 'src'},
      packages=['foobar'],
      )

或者，你可以把主包中的模块直接放到分发根目录：

<root>/
        setup.py
        __init__.py
        foo.py
        bar.py

这样你的配置文件是：

from distutils.core import setup
setup(name='foobar',
      version='1.0',
      package_dir={'foobar': ''},
      packages=['foobar'],
      )

（空字符串同样表示当前目录。）

如果你有子包，则它们必须显式列在 packages 中，但是 package_dir 中的任何条目会自动扩展到子包。（也就是说，Distutils 不会 扫描你的源码树，而是尝试通过查找 __init__.py 文件，来弄清哪些目录与 Python 包关联。）这样，如果默认排布产生一个子包：

<root>/
        setup.py
        foobar/
                 __init__.py
                 foo.py
                 bar.py
                 subfoo/
                           __init__.py
                           blah.py

则相应的配置脚本是：

from distutils.core import setup
setup(name='foobar',
      version='1.0',
      packages=['foobar', 'foobar.subfoo'],
      )

7.3. 单个扩展模块

扩展模块用 ext_modules 选项指定。package_dir 对在哪寻找扩展源文件无效；它只对纯 Python 模块的源文件有效。最简单的，一个用单个C源文件写的单扩展模块是：

<root>/
        setup.py
        foo.c

如果 foo 扩展属于根包，则配置脚本可以是：

from distutils.core import setup
from distutils.extension import Extension
setup(name='foobar',
      version='1.0',
      ext_modules=[Extension('foo', ['foo.c'])],
      )

如果扩展在包中，比如 foopkg，那么

使用完全相同的源文件树排布，通过改变扩展的名字，这个扩展很容易放入 foopkg 包中：

from distutils.core import setup
from distutils.extension import Extension
setup(name='foobar',
      version='1.0',
      ext_modules=[Extension('foopkg.foo', ['foo.c'])],
      )

7.4. 检查一个包

check 命令允许你校验你的包的元数据是否满足生成分发的最低要求。

直接使用你的 setup.py 脚本来运行它。如果缺了一些东西，check 会显示警告。

我们来用单个脚本举例：

from distutils.core import setup

setup(name='foobar')

运行 check 命令会显示一些警告：

$ python setup.py check
running check
warning: check: missing required meta-data: version, url
warning: check: missing meta-data: either (author and author_email) or
         (maintainer and maintainer_email) must be supplied

如果你在 long_description 域中使用 reStructuredText 语法，并且安装了 docutils [http://docutils.sourceforge.net] ，你可以用 check 命令，和 restructuredtext 选项检查语法是否正确。

比如，如果 setup.py 脚本改成：

from distutils.core import setup

desc = """\
My description
==============

This is the description of the ``foobar`` package.
"""

setup(name='foobar', version='1', author='tarek',
    author_email='tarek@ziade.org',
    url='http://example.com', long_description=desc)

长描述中有问题的地方，通过使用 docutils 解析器，check 能进行删除：

$ python setup.py check --restructuredtext
running check
warning: check: Title underline too short. (line 2)
warning: check: Could not finish the parsing.

7.5. 读取元数据

distutils.core.setup() 函数提供一个通过项目的 setup.py 脚本，来查询项目的元数据的域的命令行接口：

$ python setup.py --name
distribute

这个调用通过运行 distutils.core.setup() 函数读取 name 元数据。然而，当源文件或者二进制分发用 Distutils 创建时，元数据域写入一个名为 PKG-INFO 的静态文件。当一个基于Distutils的项目安装在 Python 中时，PKG-INFO 文件随着分发的模块和包一起复制到 NAME-VERSION-pyX.X.egg-info 中，其中 NAME 是项目的名字，VERSION 是元数据中定义的版本， pyX.X 则是 Python 的大版本和小版本，如 2.7 或者 3.2。

你可以读回静态文件，使用 distutils.dist.DistributionMetadata 类和它的 read_pkg_file() 方法：

>>> from distutils.dist import DistributionMetadata
>>> metadata = DistributionMetadata()
>>> metadata.read_pkg_file(open('distribute-0.6.8-py2.7.egg-info'))
>>> metadata.name
'distribute'
>>> metadata.version
'0.6.8'
>>> metadata.description
'Easily download, build, install, upgrade, and uninstall Python packages'

注意类也可以用元数据文件载入值来实例化：

>>> pkg_info_path = 'distribute-0.6.8-py2.7.egg-info'
>>> DistributionMetadata(pkg_info_path).name
'distribute'

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

8. 扩展 Distutils

Distutils可以通过各种方式扩展。大多数扩展都采用新命令或现有命令的替换形式。例如，可以编写新命令以支持新的特定于平台的包格式，但是可以修改现有命令的替换，以修改命令在包裹上的操作细节。

distutils 的大多数扩展都在想要修改现有命令的 setup.py 脚本中编写；其中许多只是简单地在 .py 文件以外添加了一些应当被拷贝到包中的文件后缀以便使用。

大多部 distutils 命令的实现都是 distutils.cmd.Command 类的子类。 新增命令可直接继承自 Command，而替换命令往往间接派生自 Command， 直接子类化它们所替换的命令。 所有命令都要求自 Command 派生。

8.1. 集成新的命令

有多种方法可将新的命令实现集成到 distutils 中。 最困难的一种是鼓动在 distutils 自身内部包含新特性，并等待（以及要求）某个 Python 版本提供该支持。 出于多种原因，这确实是相当难的。

对于大多数需求来说最为常见并且可能最为合理的一种则是通过你自己的 setup.py 脚本来包含新的实现，然后让 distutils.core.setup() 函数使用它们:

from distutils.command.build_py import build_py as _build_py
from distutils.core import setup

class build_py(_build_py):
    """Specialized Python source builder."""

    # implement whatever needs to be different...

setup(cmdclass={'build_py': build_py},
      ...)

如果新的实现必须通过特定的包来使用则此方式最为适宜，因为每个对这个包感兴趣的人都将会需要有新的命令实现。

从 Python 2.4 开始，还有第三个选项可用，其目标是允许添加支持现有 setup.py 脚本的新命令，而不需要修改 Python 安装包。 这预计可允许第三方扩展提供对附加打包系统的支持，而相应命令又可用于任何 distutils 命令可被使用的地方。 新的配置选项 command_packages (命令行选项为 --command-packages) 可用来指定附加包，以在其中查找实现新增命令的模块。 像所有 distutils 选项一样，这可以通过命令行或配置文件来指定。 此选项只能在配置文件的 [global] 小节之中或在命令行的任何命令之前设置。 如果是设置在配置文件中，则它可被命令行设置重载；如果在命令行中将其设为空字符串则将会使用默认值。 此选项绝不应当在随特定包提供的配置文件中设置。

这个新选项可被用来添加任意数量的包到查找命令实现的包列表；多个包名应当以逗号分隔。 当未指明时，查找将只在 distutils.command 包中进行。 但是当 setup.py 附带 --command-packages distcmds,buildcmds 选项运行时，distutils.command, distcmds 和 buildcmds 包将按此顺序被查找。 新的命令应当在与命名同名的模块中由同名的类来实现。 给定上述示例命令行选项，则命令 bdist_openpkg 可由类 distcmds.bdist_openpkg.bdist_openpkg 或 buildcmds.bdist_openpkg.bdist_openpkg 来实现。

8.2. 添加新的发布类型

创建发布（在 dist/ 目录中的文件）的命令需要将 (command, filename) 二元组添加到 self.distribution.dist_files 以便 upload 可以将其上传到 PyPI。 二元组中的 filename 不包含路径信息而只有文件名本身。 在 dry-run 模式下，二元组仍然应当被添加以表示必须创建的内容。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

9. 命令参考

9.1. 安装模块: install 命令族

install 命令会确保 build 命令已经运行，然后运行子命令 install_lib, install_data 和 install_scripts。

9.1.1. install_data

此命令会安装随发行包一同提供的所有数据文件。

9.1.2. install_scripts

此命令会安装发行包中的所有（Python）脚本。

9.2. 创建源码发行包: sdist 命令

列出的模板命令有:

命令

描述

include pat1 pat2 ...

包括与列出的模式匹配的所有文件

exclude pat1 pat2 ...

排除与列出的模式匹配的所有文件

recursive-include dir pat1 pat2 ...

包括 dir 下与列出的模式匹配的所有文件

recursive-exclude dir pat1 pat2 ...

排除 dir 下与列出的模式匹配的所有文件

global-include pat1 pat2 ...

包括与源树匹配的所有文件—和任何列出的模式

global-exclude pat1 pat2 ...

排除与源树匹配的所有文件—和任何列出的模式

prune dir

排除 dir 下的所有文件

graft dir

包括 dir 下的所有文件

此处的模式是 Unix 风格的 “glob” 模式: * 匹配任意的常规文件名字符序列，? 匹配任意单个常规文件名字符，而 [range] 匹配 range 范围内的任意字符 (例如 a-z, a-zA-Z, a-f0-9_.)。 “常规文件名字符”的定义取决于具体平台：在 Unix 上是指正斜杠以外的任何字符；在 Windows 则是指反斜杠或冒号以外的任何字符。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

10. API参考引用

10.1. distutils.core — 分发包功能的核心

The distutils.core 是为了使用 Distutils 工具唯一要安装的模块。它提供函数 setup() （该函数被 setup 脚本调用）。间接提供了类 distutils.dist.Distribution 和 distutils.cmd.Command.

distutils.core.setup(arguments)

The basic do-everything function that does most everything you could ever ask for from a Distutils method.

The setup function takes a large number of arguments. These are laid out in the following table.

参数名称

值

type – 类型

name

包的名字

字符串

version

包的版本号；参见 distutils.version

字符串

description

单行的包的描述

字符串

long_description

更长的包的描述

字符串

author

包的作者

字符串

author_email

包的作者的电子邮件

字符串

maintainer

The name of the current maintainer, if different from the author. Note that if the maintainer is provided, distutils will use it as the author in PKG-INFO

字符串

maintainer_email

当前维护者的电子邮件地址，如果不同与作者

字符串

url

包的URL（主页）

字符串

download_url

包的下载地址

字符串

packages

A list of Python packages that distutils will manipulate

字符串列表

py_modules

distutils 会操作的 Python 模块列表

字符串列表

scripts

要构建和安装的独立脚本文件的列表

字符串列表

ext_modules

要构建的 Python 扩展的列表

类 distutils.core.Extension 的实例的列表

classifiers

包的类别列表

a list of strings; valid classifiers are listed on PyPI [https://pypi.org/classifiers].

distclass

要使用的类 Distribution 类

distutils.core.Distribution 的子类

script_name

setup.py 脚本名称 —— 默认为 sys.argv[0]

字符串

script_args

提供给安装脚本的参数

字符串列表

options

安装脚本的默认选项

字典

license

包的许可证

字符串

keywords

Descriptive meta-data, see PEP 314 [https://www.python.org/dev/peps/pep-0314]

字符串列表或逗号分隔的字符串

platforms

字符串列表或逗号分隔的字符串

cmdclass

A mapping of command names to Command subclasses

字典

data_files

要安装的数据文件列表

列表

package_dir

A mapping of package to directory names

字典

distutils.core.run_setup(script_name[, script_args=None, stop_after='run'])

Run a setup script in a somewhat controlled environment, and return the distutils.dist.Distribution instance that drives things. This is useful if you need to find out the distribution meta-data (passed as keyword args from script to setup()), or the contents of the config files or command-line.

script_name is a file that will be read and run with exec(). sys.argv[0] will be replaced with script for the duration of the call. script_args is a list of strings; if supplied, sys.argv[1:] will be replaced by script_args for the duration of the call.

stop_after tells setup() when to stop processing; possible values:

值

描述

init

Stop after the Distribution instance has been created and populated with the keyword arguments to setup()

config

Stop after config files have been parsed (and their data stored in the Distribution instance)

commandline

Stop after the command-line (sys.argv[1:] or script_args) have been parsed (and the data stored in the Distribution instance.)

run

Stop after all commands have been run (the same as if setup() had been called in the usual way). This is the default value.

In addition, the distutils.core module exposed a number of classes that live elsewhere.

Extension from distutils.extension

Command from distutils.cmd

Distribution from distutils.dist

A short description of each of these follows, but see the relevant module for the full reference.

class distutils.core.Extension

The Extension class describes a single C or C++ extension module in a setup script. It accepts the following keyword arguments in its constructor:

参数名称

值

type – 类型

name

the full name of the extension, including any packages — ie. not a filename or pathname, but Python dotted name

字符串

sources

list of source filenames, relative to the distribution root (where the setup script lives), in Unix form (slash-separated) for portability. Source files may be C, C++, SWIG (.i), platform-specific resource files, or whatever else is recognized by the build_ext command as source for a Python extension.

字符串列表

include_dirs

list of directories to search for C/C++ header files (in Unix form for portability)

字符串列表

define_macros

list of macros to define; each macro is defined using a 2-tuple (name, value), where value is either the string to define it to or None to define it without a particular value (equivalent of #define FOO in source or -DFOO on Unix C compiler command line)

元组列表

undef_macros

要明确取消定义的宏列表

字符串列表

library_dirs

list of directories to search for C/C++ libraries at link time

字符串列表

libraries

要链接的库名列表（不是文件名或路径）

字符串列表

runtime_library_dirs

list of directories to search for C/C++ libraries at run time (for shared extensions, this is when the extension is loaded)

字符串列表

extra_objects

list of extra files to link with (eg. object files not implied by ‘sources’, static library that must be explicitly specified, binary resource files, etc.)

字符串列表

extra_compile_args

any extra platform- and compiler-specific information to use when compiling the source files in ‘sources’. For platforms and compilers where a command line makes sense, this is typically a list of command-line arguments, but for other platforms it could be anything.

字符串列表

extra_link_args

any extra platform- and compiler-specific information to use when linking object files together to create the extension (or to create a new static Python interpreter). Similar interpretation as for ‘extra_compile_args’.

字符串列表

export_symbols

list of symbols to be exported from a shared extension. Not used on all platforms, and not generally necessary for Python extensions, which typically export exactly one symbol: init + extension_name.

字符串列表

depends

扩展名依赖的文件列表

字符串列表

language

extension language (i.e. 'c', 'c++', 'objc'). Will be detected from the source extensions if not provided.

字符串

optional

指定扩展中的构建失败不应中止构建过程，而只是跳过扩展。

布尔

class distutils.core.Distribution

A Distribution describes how to build, install and package up a Python software package.

See the setup() function for a list of keyword arguments accepted by the Distribution constructor. setup() creates a Distribution instance.

class distutils.core.Command

A Command class (or rather, an instance of one of its subclasses) implement a single distutils command.

10.2. distutils.ccompiler — CCompiler基类

This module provides the abstract base class for the CCompiler classes. A CCompiler instance can be used for all the compile and link steps needed to build a single project. Methods are provided to set options for the compiler — macro definitions, include directories, link path, libraries and the like.

This module provides the following functions.

distutils.ccompiler.gen_lib_options(compiler, library_dirs, runtime_library_dirs, libraries)

Generate linker options for searching library directories and linking with specific libraries. libraries and library_dirs are, respectively, lists of library names (not filenames!) and search directories. Returns a list of command-line options suitable for use with some compiler (depending on the two format strings passed in).

distutils.ccompiler.gen_preprocess_options(macros, include_dirs)

Generate C pre-processor options (-D, -U, -I) as used by at least two types of compilers: the typical Unix compiler and Visual C++. macros is the usual thing, a list of 1- or 2-tuples, where (name,) means undefine (-U) macro name, and (name, value) means define (-D) macro name to value. include_dirs is just a list of directory names to be added to the header file search path (-I). Returns a list of command-line options suitable for either Unix compilers or Visual C++.

distutils.ccompiler.get_default_compiler(osname, platform)

Determine the default compiler to use for the given platform.

osname should be one of the standard Python OS names (i.e. the ones returned by os.name) and platform the common value returned by sys.platform for the platform in question.

The default values are os.name and sys.platform in case the parameters are not given.

distutils.ccompiler.new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0)

Factory function to generate an instance of some CCompiler subclass for the supplied platform/compiler combination. plat defaults to os.name (eg. 'posix', 'nt'), and compiler defaults to the default compiler for that platform. Currently only 'posix' and 'nt' are supported, and the default compilers are “traditional Unix interface” (UnixCCompiler class) and Visual C++ (MSVCCompiler class). Note that it’s perfectly possible to ask for a Unix compiler object under Windows, and a Microsoft compiler object under Unix—if you supply a value for compiler, plat is ignored.

distutils.ccompiler.show_compilers()

Print list of available compilers (used by the --help-compiler options to build, build_ext, build_clib).

class distutils.ccompiler.CCompiler([verbose=0, dry_run=0, force=0])

The abstract base class CCompiler defines the interface that must be implemented by real compiler classes. The class also has some utility methods used by several compiler classes.

The basic idea behind a compiler abstraction class is that each instance can be used for all the compile/link steps in building a single project. Thus, attributes common to all of those compile and link steps — include directories, macros to define, libraries to link against, etc. — are attributes of the compiler instance. To allow for variability in how individual files are treated, most of those attributes may be varied on a per-compilation or per-link basis.

The constructor for each subclass creates an instance of the Compiler object. Flags are verbose (show verbose output), dry_run (don’t actually execute the steps) and force (rebuild everything, regardless of dependencies). All of these flags default to 0 (off). Note that you probably don’t want to instantiate CCompiler or one of its subclasses directly - use the distutils.CCompiler.new_compiler() factory function instead.

The following methods allow you to manually alter compiler options for the instance of the Compiler class.

add_include_dir(dir)

Add dir to the list of directories that will be searched for header files. The compiler is instructed to search directories in the order in which they are supplied by successive calls to add_include_dir().

set_include_dirs(dirs)

Set the list of directories that will be searched to dirs (a list of strings). Overrides any preceding calls to add_include_dir(); subsequent calls to add_include_dir() add to the list passed to set_include_dirs(). This does not affect any list of standard include directories that the compiler may search by default.

add_library(libname)

Add libname to the list of libraries that will be included in all links driven by this compiler object. Note that libname should *not* be the name of a file containing a library, but the name of the library itself: the actual filename will be inferred by the linker, the compiler, or the compiler class (depending on the platform).

The linker will be instructed to link against libraries in the order they were supplied to add_library() and/or set_libraries(). It is perfectly valid to duplicate library names; the linker will be instructed to link against libraries as many times as they are mentioned.

set_libraries(libnames)

Set the list of libraries to be included in all links driven by this compiler object to libnames (a list of strings). This does not affect any standard system libraries that the linker may include by default.

add_library_dir(dir)

Add dir to the list of directories that will be searched for libraries specified to add_library() and set_libraries(). The linker will be instructed to search for libraries in the order they are supplied to add_library_dir() and/or set_library_dirs().

set_library_dirs(dirs)

Set the list of library search directories to dirs (a list of strings). This does not affect any standard library search path that the linker may search by default.

add_runtime_library_dir(dir)

Add dir to the list of directories that will be searched for shared libraries at runtime.

set_runtime_library_dirs(dirs)

Set the list of directories to search for shared libraries at runtime to dirs (a list of strings). This does not affect any standard search path that the runtime linker may search by default.

define_macro(name[, value=None])

Define a preprocessor macro for all compilations driven by this compiler object. The optional parameter value should be a string; if it is not supplied, then the macro will be defined without an explicit value and the exact outcome depends on the compiler used.

undefine_macro(name)

Undefine a preprocessor macro for all compilations driven by this compiler object. If the same macro is defined by define_macro() and undefined by undefine_macro() the last call takes precedence (including multiple redefinitions or undefinitions). If the macro is redefined/undefined on a per-compilation basis (ie. in the call to compile()), then that takes precedence.

add_link_object(object)

Add object to the list of object files (or analogues, such as explicitly named library files or the output of “resource compilers”) to be included in every link driven by this compiler object.

set_link_objects(objects)

Set the list of object files (or analogues) to be included in every link to objects. This does not affect any standard object files that the linker may include by default (such as system libraries).

The following methods implement methods for autodetection of compiler options, providing some functionality similar to GNU autoconf.

detect_language(sources)

Detect the language of a given file, or list of files. Uses the instance attributes language_map (a dictionary), and language_order (a list) to do the job.

find_library_file(dirs, lib[, debug=0])

Search the specified list of directories for a static or shared library file lib and return the full path to that file. If debug is true, look for a debugging version (if that makes sense on the current platform). Return None if lib wasn’t found in any of the specified directories.

has_function(funcname[, includes=None, include_dirs=None, libraries=None, library_dirs=None])

Return a boolean indicating whether funcname is supported on the current platform. The optional arguments can be used to augment the compilation environment by providing additional include files and paths and libraries and paths.

library_dir_option(dir)

Return the compiler option to add dir to the list of directories searched for libraries.

library_option(lib)

Return the compiler option to add lib to the list of libraries linked into the shared library or executable.

runtime_library_dir_option(dir)

Return the compiler option to add dir to the list of directories searched for runtime libraries.

set_executables(**args)

Define the executables (and options for them) that will be run to perform the various stages of compilation. The exact set of executables that may be specified here depends on the compiler class (via the ‘executables’ class attribute), but most will have:

attribute – 属性

描述

compiler

C/C++ 编译器

linker_so

用于创建共享对象和库的链接器

linker_exe

用于创建二进制可执行文件的链接器

archiver

静态库创建者

On platforms with a command-line (Unix, DOS/Windows), each of these is a string that will be split into executable name and (optional) list of arguments. (Splitting the string is done similarly to how Unix shells operate: words are delimited by spaces, but quotes and backslashes can override this. See distutils.util.split_quoted().)

The following methods invoke stages in the build process.

compile(sources[, output_dir=None, macros=None, include_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, depends=None])

Compile one or more source files. Generates object files (e.g. transforms a .c file to a .o file.)

sources must be a list of filenames, most likely C/C++ files, but in reality anything that can be handled by a particular compiler and compiler class (eg. MSVCCompiler can handle resource files in sources). Return a list of object filenames, one per source filename in sources. Depending on the implementation, not all source files will necessarily be compiled, but all corresponding object filenames will be returned.

If output_dir is given, object files will be put under it, while retaining their original path component. That is, foo/bar.c normally compiles to foo/bar.o (for a Unix implementation); if output_dir is build, then it would compile to build/foo/bar.o.

macros, if given, must be a list of macro definitions. A macro definition is either a (name, value) 2-tuple or a (name,) 1-tuple. The former defines a macro; if the value is None, the macro is defined without an explicit value. The 1-tuple case undefines a macro. Later definitions/redefinitions/undefinitions take precedence.

include_dirs, if given, must be a list of strings, the directories to add to the default include file search path for this compilation only.

debug is a boolean; if true, the compiler will be instructed to output debug symbols in (or alongside) the object file(s).

extra_preargs and extra_postargs are implementation-dependent. On platforms that have the notion of a command-line (e.g. Unix, DOS/Windows), they are most likely lists of strings: extra command-line arguments to prepend/append to the compiler command line. On other platforms, consult the implementation class documentation. In any event, they are intended as an escape hatch for those occasions when the abstract compiler framework doesn’t cut the mustard.

depends, if given, is a list of filenames that all targets depend on. If a source file is older than any file in depends, then the source file will be recompiled. This supports dependency tracking, but only at a coarse granularity.

Raises CompileError on failure.

create_static_lib(objects, output_libname[, output_dir=None, debug=0, target_lang=None])

Link a bunch of stuff together to create a static library file. The “bunch of stuff” consists of the list of object files supplied as objects, the extra object files supplied to add_link_object() and/or set_link_objects(), the libraries supplied to add_library() and/or set_libraries(), and the libraries supplied as libraries (if any).

output_libname should be a library name, not a filename; the filename will be inferred from the library name. output_dir is the directory where the library file will be put.

debug is a boolean; if true, debugging information will be included in the library (note that on most platforms, it is the compile step where this matters: the debug flag is included here just for consistency).

target_lang is the target language for which the given objects are being compiled. This allows specific linkage time treatment of certain languages.

Raises LibError on failure.

link(target_desc, objects, output_filename[, output_dir=None, libraries=None, library_dirs=None, runtime_library_dirs=None, export_symbols=None, debug=0, extra_preargs=None, extra_postargs=None, build_temp=None, target_lang=None])

Link a bunch of stuff together to create an executable or shared library file.

The “bunch of stuff” consists of the list of object files supplied as objects. output_filename should be a filename. If output_dir is supplied, output_filename is relative to it (i.e. output_filename can provide directory components if needed).

libraries is a list of libraries to link against. These are library names, not filenames, since they’re translated into filenames in a platform-specific way (eg. foo becomes libfoo.a on Unix and foo.lib on DOS/Windows). However, they can include a directory component, which means the linker will look in that specific directory rather than searching all the normal locations.

library_dirs, if supplied, should be a list of directories to search for libraries that were specified as bare library names (ie. no directory component). These are on top of the system default and those supplied to add_library_dir() and/or set_library_dirs(). runtime_library_dirs is a list of directories that will be embedded into the shared library and used to search for other shared libraries that *it* depends on at run-time. (This may only be relevant on Unix.)

export_symbols is a list of symbols that the shared library will export. (This appears to be relevant only on Windows.)

debug is as for compile() and create_static_lib(), with the slight distinction that it actually matters on most platforms (as opposed to create_static_lib(), which includes a debug flag mostly for form’s sake).

extra_preargs and extra_postargs are as for compile() (except of course that they supply command-line arguments for the particular linker being used).

target_lang is the target language for which the given objects are being compiled. This allows specific linkage time treatment of certain languages.

Raises LinkError on failure.

link_executable(objects, output_progname[, output_dir=None, libraries=None, library_dirs=None, runtime_library_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, target_lang=None])

Link an executable. output_progname is the name of the file executable, while objects are a list of object filenames to link in. Other arguments are as for the link() method.

link_shared_lib(objects, output_libname[, output_dir=None, libraries=None, library_dirs=None, runtime_library_dirs=None, export_symbols=None, debug=0, extra_preargs=None, extra_postargs=None, build_temp=None, target_lang=None])

Link a shared library. output_libname is the name of the output library, while objects is a list of object filenames to link in. Other arguments are as for the link() method.

link_shared_object(objects, output_filename[, output_dir=None, libraries=None, library_dirs=None, runtime_library_dirs=None, export_symbols=None, debug=0, extra_preargs=None, extra_postargs=None, build_temp=None, target_lang=None])

Link a shared object. output_filename is the name of the shared object that will be created, while objects is a list of object filenames to link in. Other arguments are as for the link() method.

preprocess(source[, output_file=None, macros=None, include_dirs=None, extra_preargs=None, extra_postargs=None])

Preprocess a single C/C++ source file, named in source. Output will be written to file named output_file, or stdout if output_file not supplied. macros is a list of macro definitions as for compile(), which will augment the macros set with define_macro() and undefine_macro(). include_dirs is a list of directory names that will be added to the default list, in the same way as add_include_dir().

Raises PreprocessError on failure.

The following utility methods are defined by the CCompiler class, for use by the various concrete subclasses.

executable_filename(basename[, strip_dir=0, output_dir=''])

Returns the filename of the executable for the given basename. Typically for non-Windows platforms this is the same as the basename, while Windows will get a .exe added.

library_filename(libname[, lib_type='static', strip_dir=0, output_dir=''])

Returns the filename for the given library name on the current platform. On Unix a library with lib_type of 'static' will typically be of the form liblibname.a, while a lib_type of 'dynamic' will be of the form liblibname.so.

object_filenames(source_filenames[, strip_dir=0, output_dir=''])

Returns the name of the object files for the given source files. source_filenames should be a list of filenames.

shared_object_filename(basename[, strip_dir=0, output_dir=''])

Returns the name of a shared object file for the given file name basename.

execute(func, args[, msg=None, level=1])

Invokes distutils.util.execute(). This method invokes a Python function func with the given arguments args, after logging and taking into account the dry_run flag.

spawn(cmd)

Invokes distutils.util.spawn(). This invokes an external process to run the given command.

mkpath(name[, mode=511])

Invokes distutils.dir_util.mkpath(). This creates a directory and any missing ancestor directories.

move_file(src, dst)

Invokes distutils.file_util.move_file(). Renames src to dst.

announce(msg[, level=1])

Write a message using distutils.log.debug().

warn(msg)

Write a warning message msg to standard error.

debug_print(msg)

If the debug flag is set on this CCompiler instance, print msg to standard output, otherwise do nothing.

10.3. distutils.unixccompiler — Unix C Compiler

This module provides the UnixCCompiler class, a subclass of CCompiler that handles the typical Unix-style command-line C compiler:

macros defined with -Dname[=value]

macros undefined with -Uname

include search directories specified with -Idir

libraries specified with -llib

library search directories specified with -Ldir

compile handled by cc (or similar) executable with -c option: compiles .c to .o

link static library handled by ar command (possibly with ranlib)

link shared library handled by cc -shared

10.4. distutils.msvccompiler — Microsoft Compiler

This module provides MSVCCompiler, an implementation of the abstract CCompiler class for Microsoft Visual Studio. Typically, extension modules need to be compiled with the same compiler that was used to compile Python. For Python 2.3 and earlier, the compiler was Visual Studio 6. For Python 2.4 and 2.5, the compiler is Visual Studio .NET 2003. The AMD64 and Itanium binaries are created using the Platform SDK.

MSVCCompiler will normally choose the right compiler, linker etc. on its own. To override this choice, the environment variables DISTUTILS_USE_SDK and MSSdk must be both set. MSSdk indicates that the current environment has been setup by the SDK’s SetEnv.Cmd script, or that the environment variables had been registered when the SDK was installed; DISTUTILS_USE_SDK indicates that the distutils user has made an explicit choice to override the compiler selection by MSVCCompiler.

10.5. distutils.bcppcompiler — Borland Compiler

This module provides BorlandCCompiler, a subclass of the abstract CCompiler class for the Borland C++ compiler.

10.6. distutils.cygwincompiler — Cygwin Compiler

This module provides the CygwinCCompiler class, a subclass of UnixCCompiler that handles the Cygwin port of the GNU C compiler to Windows. It also contains the Mingw32CCompiler class which handles the mingw32 port of GCC (same as cygwin in no-cygwin mode).

10.7. distutils.archive_util — Archiving utilities

This module provides a few functions for creating archive files, such as tarballs or zipfiles.

distutils.archive_util.make_archive(base_name, format[, root_dir=None, base_dir=None, verbose=0, dry_run=0])

Create an archive file (eg. zip or tar). base_name is the name of the file to create, minus any format-specific extension; format is the archive format: one of zip, tar, gztar, bztar, xztar, or ztar. root_dir is a directory that will be the root directory of the archive; ie. we typically chdir into root_dir before creating the archive. base_dir is the directory where we start archiving from; ie. base_dir will be the common prefix of all files and directories in the archive. root_dir and base_dir both default to the current directory. Returns the name of the archive file.

在 3.5 版更改: 添加了对 xztar 格式的支持

distutils.archive_util.make_tarball(base_name, base_dir[, compress='gzip', verbose=0, dry_run=0])

‘Create an (optional compressed) archive as a tar file from all files in and under base_dir. compress must be 'gzip' (the default), 'bzip2', 'xz', 'compress', or None. For the 'compress' method the compression utility named by compress must be on the default program search path, so this is probably Unix-specific. The output tar file will be named base_dir.tar, possibly plus the appropriate compression extension (.gz, .bz2, .xz or .Z). Return the output filename.

在 3.5 版更改: Added support for the xz compression.

distutils.archive_util.make_zipfile(base_name, base_dir[, verbose=0, dry_run=0])

Create a zip file from all files in and under base_dir. The output zip file will be named base_name + .zip. Uses either the zipfile Python module (if available) or the InfoZIP zip utility (if installed and found on the default search path). If neither tool is available, raises DistutilsExecError. Returns the name of the output zip file.

10.8. distutils.dep_util — Dependency checking

This module provides functions for performing simple, timestamp-based dependency of files and groups of files; also, functions based entirely on such timestamp dependency analysis.

distutils.dep_util.newer(source, target)

Return true if source exists and is more recently modified than target, or if source exists and target doesn’t. Return false if both exist and target is the same age or newer than source. Raise DistutilsFileError if source does not exist.

distutils.dep_util.newer_pairwise(sources, targets)

Walk two filename lists in parallel, testing if each source is newer than its corresponding target. Return a pair of lists (sources, targets) where source is newer than target, according to the semantics of newer().

distutils.dep_util.newer_group(sources, target[, missing='error'])

Return true if target is out-of-date with respect to any file listed in sources. In other words, if target exists and is newer than every file in sources, return false; otherwise return true. missing controls what we do when a source file is missing; the default ('error') is to blow up with an OSError from inside os.stat(); if it is 'ignore', we silently drop any missing source files; if it is 'newer', any missing source files make us assume that target is out-of-date (this is handy in “dry-run” mode: it’ll make you pretend to carry out commands that wouldn’t work because inputs are missing, but that doesn’t matter because you’re not actually going to run the commands).

10.9. distutils.dir_util — Directory tree operations

This module provides functions for operating on directories and trees of directories.

distutils.dir_util.mkpath(name[, mode=0o777, verbose=0, dry_run=0])

Create a directory and any missing ancestor directories. If the directory already exists (or if name is the empty string, which means the current directory, which of course exists), then do nothing. Raise DistutilsFileError if unable to create some directory along the way (eg. some sub-path exists, but is a file rather than a directory). If verbose is true, print a one-line summary of each mkdir to stdout. Return the list of directories actually created.

distutils.dir_util.create_tree(base_dir, files[, mode=0o777, verbose=0, dry_run=0])

Create all the empty directories under base_dir needed to put files there. base_dir is just the name of a directory which doesn’t necessarily exist yet; files is a list of filenames to be interpreted relative to base_dir. base_dir + the directory portion of every file in files will be created if it doesn’t already exist. mode, verbose and dry_run flags are as for mkpath().

distutils.dir_util.copy_tree(src, dst[, preserve_mode=1, preserve_times=1, preserve_symlinks=0, update=0, verbose=0, dry_run=0])

Copy an entire directory tree src to a new location dst. Both src and dst must be directory names. If src is not a directory, raise DistutilsFileError. If dst does not exist, it is created with mkpath(). The end result of the copy is that every file in src is copied to dst, and directories under src are recursively copied to dst. Return the list of files that were copied or might have been copied, using their output name. The return value is unaffected by update or dry_run: it is simply the list of all files under src, with the names changed to be under dst.

preserve_mode and preserve_times are the same as for distutils.file_util.copy_file(); note that they only apply to regular files, not to directories. If preserve_symlinks is true, symlinks will be copied as symlinks (on platforms that support them!); otherwise (the default), the destination of the symlink will be copied. update and verbose are the same as for copy_file().

Files in src that begin with .nfs are skipped (more information on these files is available in answer D2 of the NFS FAQ page [http://nfs.sourceforge.net/#section_d]).

在 3.3.1 版更改: NFS files are ignored.

distutils.dir_util.remove_tree(directory[, verbose=0, dry_run=0])

Recursively remove directory and all files and directories underneath it. Any errors are ignored (apart from being reported to sys.stdout if verbose is true).

10.10. distutils.file_util — Single file operations

This module contains some utility functions for operating on individual files.

distutils.file_util.copy_file(src, dst[, preserve_mode=1, preserve_times=1, update=0, link=None, verbose=0, dry_run=0])

Copy file src to dst. If dst is a directory, then src is copied there with the same name; otherwise, it must be a filename. (If the file exists, it will be ruthlessly clobbered.) If preserve_mode is true (the default), the file’s mode (type and permission bits, or whatever is analogous on the current platform) is copied. If preserve_times is true (the default), the last-modified and last-access times are copied as well. If update is true, src will only be copied if dst does not exist, or if dst does exist but is older than src.

link allows you to make hard links (using os.link()) or symbolic links (using os.symlink()) instead of copying: set it to 'hard' or 'sym'; if it is None (the default), files are copied. Don’t set link on systems that don’t support it: copy_file() doesn’t check if hard or symbolic linking is available. It uses _copy_file_contents() to copy file contents.

Return a tuple (dest_name, copied): dest_name is the actual name of the output file, and copied is true if the file was copied (or would have been copied, if dry_run true).

distutils.file_util.move_file(src, dst[, verbose, dry_run])

Move file src to dst. If dst is a directory, the file will be moved into it with the same name; otherwise, src is just renamed to dst. Returns the new full name of the file.

警告

Handles cross-device moves on Unix using copy_file(). What about other systems?

distutils.file_util.write_file(filename, contents)

Create a file called filename and write contents (a sequence of strings without line terminators) to it.

10.11. distutils.util — Miscellaneous other utility functions

This module contains other assorted bits and pieces that don’t fit into any other utility module.

distutils.util.get_platform()

Return a string that identifies the current platform. This is used mainly to distinguish platform-specific build directories and platform-specific built distributions. Typically includes the OS name and version and the architecture (as supplied by ‘os.uname()’), although the exact information included depends on the OS; eg. for IRIX the architecture isn’t particularly important (IRIX only runs on SGI hardware), but for Linux the kernel version isn’t particularly important.

返回值的示例：

linux-i586

linux-alpha

solaris-2.6-sun4u

irix-5.3

irix64-6.2

For non-POSIX platforms, currently just returns sys.platform.

For Mac OS X systems the OS version reflects the minimal version on which binaries will run (that is, the value of MACOSX_DEPLOYMENT_TARGET during the build of Python), not the OS version of the current system.

For universal binary builds on Mac OS X the architecture value reflects the universal binary status instead of the architecture of the current processor. For 32-bit universal binaries the architecture is fat, for 64-bit universal binaries the architecture is fat64, and for 4-way universal binaries the architecture is universal. Starting from Python 2.7 and Python 3.2 the architecture fat3 is used for a 3-way universal build (ppc, i386, x86_64) and intel is used for a universal build with the i386 and x86_64 architectures

Examples of returned values on Mac OS X:

macosx-10.3-ppc

macosx-10.3-fat

macosx-10.5-universal

macosx-10.6-intel

distutils.util.convert_path(pathname)

Return ‘pathname’ as a name that will work on the native filesystem, i.e. split it on ‘/’ and put it back together again using the current directory separator. Needed because filenames in the setup script are always supplied in Unix style, and have to be converted to the local convention before we can actually use them in the filesystem. Raises ValueError on non-Unix-ish systems if pathname either starts or ends with a slash.

distutils.util.change_root(new_root, pathname)

Return pathname with new_root prepended. If pathname is relative, this is equivalent to os.path.join(new_root,pathname) Otherwise, it requires making pathname relative and then joining the two, which is tricky on DOS/Windows.

distutils.util.check_environ()

Ensure that ‘os.environ’ has all the environment variables we guarantee that users can use in config files, command-line options, etc. Currently this includes:

HOME - user’s home directory (Unix only)

PLAT - description of the current platform, including hardware and OS (see get_platform())

distutils.util.subst_vars(s, local_vars)

Perform shell/Perl-style variable substitution on s. Every occurrence of $ followed by a name is considered a variable, and variable is substituted by the value found in the local_vars dictionary, or in os.environ if it’s not in local_vars. os.environ is first checked/augmented to guarantee that it contains certain values: see check_environ(). Raise ValueError for any variables not found in either local_vars or os.environ.

Note that this is not a fully-fledged string interpolation function. A valid $variable can consist only of upper and lower case letters, numbers and an underscore. No { } or ( ) style quoting is available.

distutils.util.split_quoted(s)

Split a string up according to Unix shell-like rules for quotes and backslashes. In short: words are delimited by spaces, as long as those spaces are not escaped by a backslash, or inside a quoted string. Single and double quotes are equivalent, and the quote characters can be backslash-escaped. The backslash is stripped from any two-character escape sequence, leaving only the escaped character. The quote characters are stripped from any quoted string. Returns a list of words.

distutils.util.execute(func, args[, msg=None, verbose=0, dry_run=0])

Perform some action that affects the outside world (for instance, writing to the filesystem). Such actions are special because they are disabled by the dry_run flag. This method takes care of all that bureaucracy for you; all you have to do is supply the function to call and an argument tuple for it (to embody the “external action” being performed), and an optional message to print.

distutils.util.strtobool(val)

Convert a string representation of truth to true (1) or false (0).

True values are y, yes, t, true, on and 1; false values are n, no, f, false, off and 0. Raises ValueError if val is anything else.

distutils.util.byte_compile(py_files[, optimize=0, force=0, prefix=None, base_dir=None, verbose=1, dry_run=0, direct=None])

Byte-compile a collection of Python source files to .pyc files in a __pycache__ subdirectory (see PEP 3147 [https://www.python.org/dev/peps/pep-3147] and PEP 488 [https://www.python.org/dev/peps/pep-0488]). py_files is a list of files to compile; any files that don’t end in .py are silently skipped. optimize must be one of the following:

0 - don’t optimize

1 - normal optimization (like python -O)

2 - extra optimization (like python -OO)

If force is true, all files are recompiled regardless of timestamps.

The source filename encoded in each bytecode file defaults to the filenames listed in py_files; you can modify these with prefix and basedir. prefix is a string that will be stripped off of each source filename, and base_dir is a directory name that will be prepended (after prefix is stripped). You can supply either or both (or neither) of prefix and base_dir, as you wish.

If dry_run is true, doesn’t actually do anything that would affect the filesystem.

Byte-compilation is either done directly in this interpreter process with the standard py_compile module, or indirectly by writing a temporary script and executing it. Normally, you should let byte_compile() figure out to use direct compilation or not (see the source for details). The direct flag is used by the script generated in indirect mode; unless you know what you’re doing, leave it set to None.

在 3.2.3 版更改: Create .pyc files with an import magic tag in their name, in a __pycache__ subdirectory instead of files without tag in the current directory.

在 3.5 版更改: Create .pyc files according to PEP 488 [https://www.python.org/dev/peps/pep-0488].

distutils.util.rfc822_escape(header)

Return a version of header escaped for inclusion in an RFC 822 [https://tools.ietf.org/html/rfc822.html] header, by ensuring there are 8 spaces space after each newline. Note that it does no other modification of the string.

10.12. distutils.dist — The Distribution class

This module provides the Distribution class, which represents the module distribution being built/installed/distributed.

10.13. distutils.extension — The Extension class

This module provides the Extension class, used to describe C/C++ extension modules in setup scripts.

10.14. distutils.debug — Distutils debug mode

本模块提供DEBUG标识。

10.15. distutils.errors — Distutils exceptions

Provides exceptions used by the Distutils modules. Note that Distutils modules may raise standard exceptions; in particular, SystemExit is usually raised for errors that are obviously the end-user’s fault (eg. bad command-line arguments).

This module is safe to use in from ... import * mode; it only exports symbols whose names start with Distutils and end with Error.

10.16. distutils.fancy_getopt — Wrapper around the standard getopt module

This module provides a wrapper around the standard getopt module that provides the following additional features:

short and long options are tied together

options have help strings, so fancy_getopt() could potentially create a complete usage summary

options set attributes of a passed-in object

boolean options can have “negative aliases” — eg. if --quiet is the “negative alias” of --verbose, then --quiet on the command line sets verbose to false.

distutils.fancy_getopt.fancy_getopt(options, negative_opt, object, args)

Wrapper function. options is a list of (long_option, short_option, help_string) 3-tuples as described in the constructor for FancyGetopt. negative_opt should be a dictionary mapping option names to option names, both the key and value should be in the options list. object is an object which will be used to store values (see the getopt() method of the FancyGetopt class). args is the argument list. Will use sys.argv[1:] if you pass None as args.

distutils.fancy_getopt.wrap_text(text, width)

Wraps text to less than width wide.

class distutils.fancy_getopt.FancyGetopt([option_table=None])

The option_table is a list of 3-tuples: (long_option, short_option, help_string)

If an option takes an argument, its long_option should have '=' appended; short_option should just be a single character, no ':' in any case. short_option should be None if a long_option doesn’t have a corresponding short_option. All option tuples must have long options.

The FancyGetopt class provides the following methods:

FancyGetopt.getopt([args=None, object=None])

Parse command-line options in args. Store as attributes on object.

If args is None or not supplied, uses sys.argv[1:]. If object is None or not supplied, creates a new OptionDummy instance, stores option values there, and returns a tuple (args, object). If object is supplied, it is modified in place and getopt() just returns args; in both cases, the returned args is a modified copy of the passed-in args list, which is left untouched.

FancyGetopt.get_option_order()

Returns the list of (option, value) tuples processed by the previous run of getopt() Raises RuntimeError if getopt() hasn’t been called yet.

FancyGetopt.generate_help([header=None])

Generate help text (a list of strings, one per suggested line of output) from the option table for this FancyGetopt object.

If supplied, prints the supplied header at the top of the help.

10.17. distutils.filelist — The FileList class

This module provides the FileList class, used for poking about the filesystem and building lists of files.

10.18. distutils.log — Simple PEP 282-style logging

10.19. distutils.spawn — Spawn a sub-process

This module provides the spawn() function, a front-end to various platform-specific functions for launching another program in a sub-process. Also provides find_executable() to search the path for a given executable name.

10.20. distutils.sysconfig — System configuration information

The distutils.sysconfig module provides access to Python’s low-level configuration information. The specific configuration variables available depend heavily on the platform and configuration. The specific variables depend on the build process for the specific version of Python being run; the variables are those found in the Makefile and configuration header that are installed with Python on Unix systems. The configuration header is called pyconfig.h for Python versions starting with 2.2, and config.h for earlier versions of Python.

Some additional functions are provided which perform some useful manipulations for other parts of the distutils package.

distutils.sysconfig.PREFIX

The result of os.path.normpath(sys.prefix).

distutils.sysconfig.EXEC_PREFIX

The result of os.path.normpath(sys.exec_prefix).

distutils.sysconfig.get_config_var(name)

Return the value of a single variable. This is equivalent to get_config_vars().get(name).

distutils.sysconfig.get_config_vars(...)

Return a set of variable definitions. If there are no arguments, this returns a dictionary mapping names of configuration variables to values. If arguments are provided, they should be strings, and the return value will be a sequence giving the associated values. If a given name does not have a corresponding value, None will be included for that variable.

distutils.sysconfig.get_config_h_filename()

Return the full path name of the configuration header. For Unix, this will be the header generated by the configure script; for other platforms the header will have been supplied directly by the Python source distribution. The file is a platform-specific text file.

distutils.sysconfig.get_makefile_filename()

Return the full path name of the Makefile used to build Python. For Unix, this will be a file generated by the configure script; the meaning for other platforms will vary. The file is a platform-specific text file, if it exists. This function is only useful on POSIX platforms.

distutils.sysconfig.get_python_inc([plat_specific[, prefix]])

Return the directory for either the general or platform-dependent C include files. If plat_specific is true, the platform-dependent include directory is returned; if false or omitted, the platform-independent directory is returned. If prefix is given, it is used as either the prefix instead of PREFIX, or as the exec-prefix instead of EXEC_PREFIX if plat_specific is true.

distutils.sysconfig.get_python_lib([plat_specific[, standard_lib[, prefix]]])

Return the directory for either the general or platform-dependent library installation. If plat_specific is true, the platform-dependent include directory is returned; if false or omitted, the platform-independent directory is returned. If prefix is given, it is used as either the prefix instead of PREFIX, or as the exec-prefix instead of EXEC_PREFIX if plat_specific is true. If standard_lib is true, the directory for the standard library is returned rather than the directory for the installation of third-party extensions.

The following function is only intended for use within the distutils package.

distutils.sysconfig.customize_compiler(compiler)

Do any platform-specific customization of a distutils.ccompiler.CCompiler instance.

This function is only needed on Unix at this time, but should be called consistently to support forward-compatibility. It inserts the information that varies across Unix flavors and is stored in Python’s Makefile. This information includes the selected compiler, compiler and linker options, and the extension used by the linker for shared objects.

This function is even more special-purpose, and should only be used from Python’s own build procedures.

distutils.sysconfig.set_python_build()

Inform the distutils.sysconfig module that it is being used as part of the build process for Python. This changes a lot of relative locations for files, allowing them to be located in the build area rather than in an installed Python.

10.21. distutils.text_file — The TextFile class

This module provides the TextFile class, which gives an interface to text files that (optionally) takes care of stripping comments, ignoring blank lines, and joining lines with backslashes.

class distutils.text_file.TextFile([filename=None, file=None, **options])

This class provides a file-like object that takes care of all the things you commonly want to do when processing a text file that has some line-by-line syntax: strip comments (as long as # is your comment character), skip blank lines, join adjacent lines by escaping the newline (ie. backslash at end of line), strip leading and/or trailing whitespace. All of these are optional and independently controllable.

The class provides a warn() method so you can generate warning messages that report physical line number, even if the logical line in question spans multiple physical lines. Also provides unreadline() for implementing line-at-a-time lookahead.

TextFile instances are create with either filename, file, or both. RuntimeError is raised if both are None. filename should be a string, and file a file object (or something that provides readline() and close() methods). It is recommended that you supply at least filename, so that TextFile can include it in warning messages. If file is not supplied, TextFile creates its own using the open() built-in function.

The options are all boolean, and affect the values returned by readline()

选项名称

描述

默认值

strip_comments

strip from '#' to end-of-line, as well as any whitespace leading up to the '#'—unless it is escaped by a backslash

true

lstrip_ws

strip leading whitespace from each line before returning it

false

rstrip_ws

strip trailing whitespace (including line terminator!) from each line before returning it.

true

skip_blanks

skip lines that are empty *after* stripping comments and whitespace. (If both lstrip_ws and rstrip_ws are false, then some lines may consist of solely whitespace: these will *not* be skipped, even if skip_blanks is true.)

true

join_lines

if a backslash is the last non-newline character on a line after stripping comments and whitespace, join the following line to it to form one logical line; if N consecutive lines end with a backslash, then N+1 physical lines will be joined to form one logical line.

false

collapse_join

strip leading whitespace from lines that are joined to their predecessor; only matters if (join_lines and not lstrip_ws)

false

Note that since rstrip_ws can strip the trailing newline, the semantics of readline() must differ from those of the built-in file object’s readline() method! In particular, readline() returns None for end-of-file: an empty string might just be a blank line (or an all-whitespace line), if rstrip_ws is true but skip_blanks is not.

open(filename)

Open a new file filename. This overrides any file or filename constructor arguments.

close()

Close the current file and forget everything we know about it (including the filename and the current line number).

warn(msg[, line=None])

Print (to stderr) a warning message tied to the current logical line in the current file. If the current logical line in the file spans multiple physical lines, the warning refers to the whole range, such as "lines 3-5". If line is supplied, it overrides the current line number; it may be a list or tuple to indicate a range of physical lines, or an integer for a single physical line.

readline()

Read and return a single logical line from the current file (or from an internal buffer if lines have previously been “unread” with unreadline()). If the join_lines option is true, this may involve reading multiple physical lines concatenated into a single string. Updates the current line number, so calling warn() after readline() emits a warning about the physical line(s) just read. Returns None on end-of-file, since the empty string can occur if rstrip_ws is true but strip_blanks is not.

readlines()

Read and return the list of all logical lines remaining in the current file. This updates the current line number to the last line of the file.

unreadline(line)

Push line (a string) onto an internal buffer that will be checked by future readline() calls. Handy for implementing a parser with line-at-a-time lookahead. Note that lines that are “unread” with unreadline() are not subsequently re-cleansed (whitespace stripped, or whatever) when read with readline(). If multiple calls are made to unreadline() before a call to readline(), the lines will be returned most in most recent first order.

10.22. distutils.version — Version number classes

10.23. distutils.cmd — Abstract base class for Distutils commands

This module supplies the abstract base class Command.

class distutils.cmd.Command(dist)

Abstract base class for defining command classes, the “worker bees” of the Distutils. A useful analogy for command classes is to think of them as subroutines with local variables called options. The options are declared in initialize_options() and defined (given their final values) in finalize_options(), both of which must be defined by every command class. The distinction between the two is necessary because option values might come from the outside world (command line, config file, …), and any options dependent on other options must be computed after these outside influences have been processed — hence finalize_options(). The body of the subroutine, where it does all its work based on the values of its options, is the run() method, which must also be implemented by every command class.

The class constructor takes a single argument dist, a Distribution instance.

10.24. Creating a new Distutils command

This section outlines the steps to create a new Distutils command.

A new command lives in a module in the distutils.command package. There is a sample template in that directory called command_template. Copy this file to a new module with the same name as the new command you’re implementing. This module should implement a class with the same name as the module (and the command). So, for instance, to create the command peel_banana (so that users can run setup.py peel_banana), you’d copy command_template to distutils/command/peel_banana.py, then edit it so that it’s implementing the class peel_banana, a subclass of distutils.cmd.Command.

Subclasses of Command must define the following methods.

Command.initialize_options()

Set default values for all the options that this command supports. Note that these defaults may be overridden by other commands, by the setup script, by config files, or by the command-line. Thus, this is not the place to code dependencies between options; generally, initialize_options() implementations are just a bunch of self.foo = None assignments.

Command.finalize_options()

Set final values for all the options that this command supports. This is always called as late as possible, ie. after any option assignments from the command-line or from other commands have been done. Thus, this is the place to code option dependencies: if foo depends on bar, then it is safe to set foo from bar as long as foo still has the same value it was assigned in initialize_options().

Command.run()

A command’s raison d’etre: carry out the action it exists to perform, controlled by the options initialized in initialize_options(), customized by other commands, the setup script, the command-line, and config files, and finalized in finalize_options(). All terminal output and filesystem interaction should be done by run().

Command.sub_commands

sub_commands formalizes the notion of a “family” of commands, e.g. install as the parent with sub-commands install_lib, install_headers, etc. The parent of a family of commands defines sub_commands as a class attribute; it’s a list of 2-tuples (command_name, predicate), with command_name a string and predicate a function, a string or None. predicate is a method of the parent command that determines whether the corresponding command is applicable in the current situation. (E.g. install_headers is only applicable if we have any C header files to install.) If predicate is None, that command is always applicable.

sub_commands is usually defined at the end of a class, because predicates can be methods of the class, so they must already have been defined. The canonical example is the install command.

10.25. distutils.command — Individual Distutils commands

10.26. distutils.command.bdist — Build a binary installer

10.27. distutils.command.bdist_packager — Abstract base class for packagers

10.28. distutils.command.bdist_dumb — Build a “dumb” installer

10.29. distutils.command.bdist_msi — Build a Microsoft Installer binary package

class distutils.command.bdist_msi.bdist_msi

Builds a Windows Installer [https://msdn.microsoft.com/en-us/library/cc185688(VS.85).aspx] (.msi) binary package.

In most cases, the bdist_msi installer is a better choice than the bdist_wininst installer, because it provides better support for Win64 platforms, allows administrators to perform non-interactive installations, and allows installation through group policies.

10.30. distutils.command.bdist_rpm — Build a binary distribution as a Redhat RPM and SRPM

10.31. distutils.command.bdist_wininst — Build a Windows installer

10.32. distutils.command.sdist — Build a source distribution

10.33. distutils.command.build — Build all files of a package

10.34. distutils.command.build_clib — Build any C libraries in a package

10.35. distutils.command.build_ext — Build any extensions in a package

10.36. distutils.command.build_py — Build the .py/.pyc files of a package

class distutils.command.build_py.build_py

class distutils.command.build_py.build_py_2to3

Alternative implementation of build_py which also runs the 2to3 conversion library on each .py file that is going to be installed. To use this in a setup.py file for a distribution that is designed to run with both Python 2.x and 3.x, add:

try:
    from distutils.command.build_py import build_py_2to3 as build_py
except ImportError:
    from distutils.command.build_py import build_py

to your setup.py, and later:

cmdclass = {'build_py': build_py}

to the invocation of setup().

10.37. distutils.command.build_scripts — Build the scripts of a package

10.38. distutils.command.clean — Clean a package build area

This command removes the temporary files created by build and its subcommands, like intermediary compiled object files. With the --all option, the complete build directory will be removed.

Extension modules built in place will not be cleaned, as they are not in the build directory.

10.39. distutils.command.config — Perform package configuration

10.40. distutils.command.install — Install a package

10.41. distutils.command.install_data — Install data files from a package

10.42. distutils.command.install_headers — Install C/C++ header files from a package

10.43. distutils.command.install_lib — Install library files from a package

10.44. distutils.command.install_scripts — Install script files from a package

10.45. distutils.command.register — Register a module with the Python Package Index

The register command registers the package with the Python Package Index. This is described in more detail in PEP 301 [https://www.python.org/dev/peps/pep-0301].

10.46. distutils.command.check — Check the meta-data of a package

The check command performs some tests on the meta-data of a package. For example, it verifies that all required meta-data are provided as the arguments passed to the setup() function.

导航

索引

模块 |

下一页 |

上一页 |

Python »

3.6.13 Documentation »

分发 Python 模块（遗留版本） »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

上一页 |

Python »

3.6.13 Documentation »

|

安装Python模块（旧版）

作者

Greg Ward

参见

安装 Python 模块

最新的模块安装文档

本文档从最终用户的角度描述了Python Distribution Utilities（“Distutils”），描述了如何通过构建和安装第三方Python模块和扩展来扩展标准Python的功能。

注解

This guide only covers the basic tools for building and distributing extensions that are provided as part of this version of Python. Third party tools offer easier to use and more secure alternatives. Refer to the quick recommendations section [https://packaging.python.org/en/latest/current/] in the Python Packaging User Guide for more information.

概述

尽管Python广泛的标准库满足了众多的程序需要，却总会有时候你需要以第三方模块的形式加入一些新函数到你的程序中。为了支持您自己的程序，或者是为了支持您想要使用的应用程序，如果恰好是用Python编写的话，这可能是必要的。

In the past, there has been little support for adding third-party modules to an existing Python installation. With the introduction of the Python Distribution Utilities (Distutils for short) in Python 2.0, this changed.

This document is aimed primarily at the people who need to install third-party Python modules: end-users and system administrators who just need to get some Python application running, and existing Python programmers who want to add some new goodies to their toolbox. You don’t need to know Python to read this document; there will be some brief forays into using Python’s interactive mode to explore your installation, but that’s it. If you’re looking for information on how to distribute your own Python modules so that others may use them, see the 分发 Python 模块（遗留版本） manual. Debugging the setup script may also be of interest.

Best case: trivial installation

In the best case, someone will have prepared a special version of the module distribution you want to install that is targeted specifically at your platform and is installed just like any other software on your platform. For example, the module developer might make an executable installer available for Windows users, an RPM package for users of RPM-based Linux systems (Red Hat, SuSE, Mandrake, and many others), a Debian package for users of Debian-based Linux systems, and so forth.

In that case, you would download the installer appropriate to your platform and do the obvious thing with it: run it if it’s an executable installer, rpm --install it if it’s an RPM, etc. You don’t need to run Python or a setup script, you don’t need to compile anything—you might not even need to read any instructions (although it’s always a good idea to do so anyway).

Of course, things will not always be that easy. You might be interested in a module distribution that doesn’t have an easy-to-use installer for your platform. In that case, you’ll have to start with the source distribution released by the module’s author/maintainer. Installing from a source distribution is not too hard, as long as the modules are packaged in the standard way. The bulk of this document is about building and installing modules from standard source distributions.

The new standard: Distutils

If you download a module source distribution, you can tell pretty quickly if it was packaged and distributed in the standard way, i.e. using the Distutils. First, the distribution’s name and version number will be featured prominently in the name of the downloaded archive, e.g. foo-1.0.tar.gz or widget-0.9.7.zip. Next, the archive will unpack into a similarly-named directory: foo-1.0 or widget-0.9.7. Additionally, the distribution will contain a setup script setup.py, and a file named README.txt or possibly just README, which should explain that building and installing the module distribution is a simple matter of running one command from a terminal:

python setup.py install

For Windows, this command should be run from a command prompt window (Start ‣ Accessories):

setup.py install

If all these things are true, then you already know how to build and install the modules you’ve just downloaded: Run the command above. Unless you need to install things in a non-standard way or customize the build process, you don’t really need this manual. Or rather, the above command is everything you need to get out of this manual.

Standard Build and Install

As described in section The new standard: Distutils, building and installing a module distribution using the Distutils is usually one simple command to run from a terminal:

python setup.py install

Platform variations

You should always run the setup command from the distribution root directory, i.e. the top-level subdirectory that the module source distribution unpacks into. For example, if you’ve just downloaded a module source distribution foo-1.0.tar.gz onto a Unix system, the normal thing to do is:

gunzip -c foo-1.0.tar.gz | tar xf -    # unpacks into directory foo-1.0
cd foo-1.0
python setup.py install

On Windows, you’d probably download foo-1.0.zip. If you downloaded the archive file to C:\Temp, then it would unpack into C:\Temp\foo-1.0; you can use either an archive manipulator with a graphical user interface (such as WinZip) or a command-line tool (such as unzip or pkunzip) to unpack the archive. Then, open a command prompt window and run:

cd c:\Temp\foo-1.0
python setup.py install

Splitting the job up

Running setup.py install builds and installs all modules in one run. If you prefer to work incrementally—especially useful if you want to customize the build process, or if things are going wrong—you can use the setup script to do one thing at a time. This is particularly helpful when the build and install will be done by different users—for example, you might want to build a module distribution and hand it off to a system administrator for installation (or do it yourself, with super-user privileges).

For example, you can build everything in one step, and then install everything in a second step, by invoking the setup script twice:

python setup.py build
python setup.py install

If you do this, you will notice that running the install command first runs the build command, which—in this case—quickly notices that it has nothing to do, since everything in the build directory is up-to-date.

You may not need this ability to break things down often if all you do is install modules downloaded off the ‘net, but it’s very handy for more advanced tasks. If you get into distributing your own Python modules and extensions, you’ll run lots of individual Distutils commands on their own.

How building works

As implied above, the build command is responsible for putting the files to install into a build directory. By default, this is build under the distribution root; if you’re excessively concerned with speed, or want to keep the source tree pristine, you can change the build directory with the --build-base option. For example:

python setup.py build --build-base=/path/to/pybuild/foo-1.0

(Or you could do this permanently with a directive in your system or personal Distutils configuration file; see section Distutils Configuration Files.) Normally, this isn’t necessary.

The default layout for the build tree is as follows:

--- build/ --- lib/
or
--- build/ --- lib.<plat>/
               temp.<plat>/

where <plat> expands to a brief description of the current OS/hardware platform and Python version. The first form, with just a lib directory, is used for “pure module distributions”—that is, module distributions that include only pure Python modules. If a module distribution contains any extensions (modules written in C/C++), then the second form, with two <plat> directories, is used. In that case, the temp.plat directory holds temporary files generated by the compile/link process that don’t actually get installed. In either case, the lib (or lib.plat) directory contains all Python modules (pure Python and extensions) that will be installed.

In the future, more directories will be added to handle Python scripts, documentation, binary executables, and whatever else is needed to handle the job of installing Python modules and applications.

How installation works

After the build command runs (whether you run it explicitly, or the install command does it for you), the work of the install command is relatively simple: all it has to do is copy everything under build/lib (or build/lib.plat) to your chosen installation directory.

If you don’t choose an installation directory—i.e., if you just run setup.py install—then the install command installs to the standard location for third-party Python modules. This location varies by platform and by how you built/installed Python itself. On Unix (and Mac OS X, which is also Unix-based), it also depends on whether the module distribution being installed is pure Python or contains extensions (“non-pure”):

平台

标准安装位置

默认值

注释

（纯）Unix

prefix/lib/pythonX.Y/site-packages

/usr/local/lib/pythonX.Y/site-packages

(1)

（类）Unix

exec-prefix/lib/pythonX.Y/site-packages

/usr/local/lib/pythonX.Y/site-packages

(1)

Windows

prefix\Lib\site-packages

C:\PythonXY\Lib\site-packages

(2)

注释:

Most Linux distributions include Python as a standard part of the system, so prefix and exec-prefix are usually both /usr on Linux. If you build Python yourself on Linux (or any Unix-like system), the default prefix and exec-prefix are /usr/local.

The default installation directory on Windows was C:\Program Files\Python under Python 1.6a1, 1.5.2, and earlier.

prefix and exec-prefix stand for the directories that Python is installed to, and where it finds its libraries at run-time. They are always the same under Windows, and very often the same under Unix and Mac OS X. You can find out what your Python installation uses for prefix and exec-prefix by running Python in interactive mode and typing a few simple commands. Under Unix, just type python at the shell prompt. Under Windows, choose Start ‣ Programs ‣ Python X.Y ‣ Python (command line). Once the interpreter is started, you type Python code at the prompt. For example, on my Linux system, I type the three Python statements shown below, and get the output as shown, to find out my prefix and exec-prefix:

Python 2.4 (#26, Aug  7 2004, 17:19:02)
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.prefix
'/usr'
>>> sys.exec_prefix
'/usr'

A few other placeholders are used in this document: X.Y stands for the version of Python, for example 3.2; abiflags will be replaced by the value of sys.abiflags or the empty string for platforms which don’t define ABI flags; distname will be replaced by the name of the module distribution being installed. Dots and capitalization are important in the paths; for example, a value that uses python3.2 on UNIX will typically use Python32 on Windows.

If you don’t want to install modules to the standard location, or if you don’t have permission to write there, then you need to read about alternate installations in section Alternate Installation. If you want to customize your installation directories more heavily, see section 自定义安装 on custom installations.

Alternate Installation

Often, it is necessary or desirable to install modules to a location other than the standard location for third-party Python modules. For example, on a Unix system you might not have permission to write to the standard third-party module directory. Or you might wish to try out a module before making it a standard part of your local Python installation. This is especially true when upgrading a distribution already present: you want to make sure your existing base of scripts still works with the new version before actually upgrading.

The Distutils install command is designed to make installing module distributions to an alternate location simple and painless. The basic idea is that you supply a base directory for the installation, and the install command picks a set of directories (called an installation scheme) under this base directory in which to install files. The details differ across platforms, so read whichever of the following sections applies to you.

Note that the various alternate installation schemes are mutually exclusive: you can pass --user, or --home, or --prefix and --exec-prefix, or --install-base and --install-platbase, but you can’t mix from these groups.

Alternate installation: the user scheme

This scheme is designed to be the most convenient solution for users that don’t have write permission to the global site-packages directory or don’t want to install into it. It is enabled with a simple option:

python setup.py install --user

Files will be installed into subdirectories of site.USER_BASE (written as userbase hereafter). This scheme installs pure Python modules and extension modules in the same location (also known as site.USER_SITE). Here are the values for UNIX, including Mac OS X:

文件类型

安装目录

modules

userbase/lib/pythonX.Y/site-packages

scripts

userbase/bin

data

userbase

C headers

userbase/include/pythonX.Yabiflags/distname

以下是Windows上使用的值：

文件类型

安装目录

modules

userbase\PythonXY\site-packages

scripts

userbase\PythonXY\Scripts

data

userbase

C headers

userbase\PythonXY\Include{distname}

The advantage of using this scheme compared to the other ones described below is that the user site-packages directory is under normal conditions always included in sys.path (see site for more information), which means that there is no additional step to perform after running the setup.py script to finalize the installation.

The build_ext command also has a --user option to add userbase/include to the compiler search path for header files and userbase/lib to the compiler search path for libraries as well as to the runtime search path for shared C libraries (rpath).

Alternate installation: the home scheme

The idea behind the “home scheme” is that you build and maintain a personal stash of Python modules. This scheme’s name is derived from the idea of a “home” directory on Unix, since it’s not unusual for a Unix user to make their home directory have a layout similar to /usr/ or /usr/local/. This scheme can be used by anyone, regardless of the operating system they are installing for.

Installing a new module distribution is as simple as

python setup.py install --home=<dir>

where you can supply any directory you like for the --home option. On Unix, lazy typists can just type a tilde (~); the install command will expand this to your home directory:

python setup.py install --home=~

To make Python find the distributions installed with this scheme, you may have to modify Python’s search path or edit sitecustomize (see site) to call site.addsitedir() or edit sys.path.

The --home option defines the installation base directory. Files are installed to the following directories under the installation base as follows:

文件类型

安装目录

modules

home/lib/python

scripts

home/bin

data

home

C headers

home/include/python/distname

(Mentally replace slashes with backslashes if you’re on Windows.)

Alternate installation: Unix (the prefix scheme)

The “prefix scheme” is useful when you wish to use one Python installation to perform the build/install (i.e., to run the setup script), but install modules into the third-party module directory of a different Python installation (or something that looks like a different Python installation). If this sounds a trifle unusual, it is—that’s why the user and home schemes come before. However, there are at least two known cases where the prefix scheme will be useful.

First, consider that many Linux distributions put Python in /usr, rather than the more traditional /usr/local. This is entirely appropriate, since in those cases Python is part of “the system” rather than a local add-on. However, if you are installing Python modules from source, you probably want them to go in /usr/local/lib/python2.X rather than /usr/lib/python2.X. This can be done with

/usr/bin/python setup.py install --prefix=/usr/local

Another possibility is a network filesystem where the name used to write to a remote directory is different from the name used to read it: for example, the Python interpreter accessed as /usr/local/bin/python might search for modules in /usr/local/lib/python2.X, but those modules would have to be installed to, say, /mnt/@server/export/lib/python2.X. This could be done with

/usr/local/bin/python setup.py install --prefix=/mnt/@server/export

In either case, the --prefix option defines the installation base, and the --exec-prefix option defines the platform-specific installation base, which is used for platform-specific files. (Currently, this just means non-pure module distributions, but could be expanded to C libraries, binary executables, etc.) If --exec-prefix is not supplied, it defaults to --prefix. Files are installed as follows:

文件类型

安装目录

Python 模块

prefix/lib/pythonX.Y/site-packages

扩展模块

exec-prefix/lib/pythonX.Y/site-packages

scripts

prefix/bin

data

prefix

C headers

prefix/include/pythonX.Yabiflags/distname

There is no requirement that --prefix or --exec-prefix actually point to an alternate Python installation; if the directories listed above do not already exist, they are created at installation time.

Incidentally, the real reason the prefix scheme is important is simply that a standard Unix installation uses the prefix scheme, but with --prefix and --exec-prefix supplied by Python itself as sys.prefix and sys.exec_prefix. Thus, you might think you’ll never use the prefix scheme, but every time you run python setup.py install without any other options, you’re using it.

Note that installing extensions to an alternate Python installation has no effect on how those extensions are built: in particular, the Python header files (Python.h and friends) installed with the Python interpreter used to run the setup script will be used in compiling extensions. It is your responsibility to ensure that the interpreter used to run extensions installed in this way is compatible with the interpreter used to build them. The best way to do this is to ensure that the two interpreters are the same version of Python (possibly different builds, or possibly copies of the same build). (Of course, if your --prefix and --exec-prefix don’t even point to an alternate Python installation, this is immaterial.)

Alternate installation: Windows (the prefix scheme)

Windows has no concept of a user’s home directory, and since the standard Python installation under Windows is simpler than under Unix, the --prefix option has traditionally been used to install additional packages in separate locations on Windows.

python setup.py install --prefix="\Temp\Python"

to install modules to the \Temp\Python directory on the current drive.

The installation base is defined by the --prefix option; the --exec-prefix option is not supported under Windows, which means that pure Python modules and extension modules are installed into the same location. Files are installed as follows:

文件类型

安装目录

modules

prefix\Lib\site-packages

scripts

prefix\Scripts

data

prefix

C headers

prefix\Include{distname}

自定义安装

Sometimes, the alternate installation schemes described in section Alternate Installation just don’t do what you want. You might want to tweak just one or two directories while keeping everything under the same base directory, or you might want to completely redefine the installation scheme. In either case, you’re creating a custom installation scheme.

To create a custom installation scheme, you start with one of the alternate schemes and override some of the installation directories used for the various types of files, using these options:

文件类型

覆盖选项

Python 模块

--install-purelib

扩展模块

--install-platlib

所有模块

--install-lib

scripts

--install-scripts

data

--install-data

C headers

--install-headers

These override options can be relative, absolute, or explicitly defined in terms of one of the installation base directories. (There are two installation base directories, and they are normally the same—they only differ when you use the Unix “prefix scheme” and supply different --prefix and --exec-prefix options; using --install-lib will override values computed or given for --install-purelib and --install-platlib, and is recommended for schemes that don’t make a difference between Python and extension modules.)

For example, say you’re installing a module distribution to your home directory under Unix—but you want scripts to go in ~/scripts rather than ~/bin. As you might expect, you can override this directory with the --install-scripts option; in this case, it makes most sense to supply a relative path, which will be interpreted relative to the installation base directory (your home directory, in this case):

python setup.py install --home=~ --install-scripts=scripts

Another Unix example: suppose your Python installation was built and installed with a prefix of /usr/local/python, so under a standard installation scripts will wind up in /usr/local/python/bin. If you want them in /usr/local/bin instead, you would supply this absolute directory for the --install-scripts option:

python setup.py install --install-scripts=/usr/local/bin

(This performs an installation using the “prefix scheme,” where the prefix is whatever your Python interpreter was installed with— /usr/local/python in this case.)

If you maintain Python on Windows, you might want third-party modules to live in a subdirectory of prefix, rather than right in prefix itself. This is almost as easy as customizing the script installation directory—you just have to remember that there are two types of modules to worry about, Python and extension modules, which can conveniently be both controlled by one option:

python setup.py install --install-lib=Site

The specified installation directory is relative to prefix. Of course, you also have to ensure that this directory is in Python’s module search path, such as by putting a .pth file in a site directory (see site). See section Modifying Python’s Search Path to find out how to modify Python’s search path.

If you want to define an entire installation scheme, you just have to supply all of the installation directory options. The recommended way to do this is to supply relative paths; for example, if you want to maintain all Python module-related files under python in your home directory, and you want a separate directory for each platform that you use your home directory from, you might define the following installation scheme:

python setup.py install --home=~ \
                        --install-purelib=python/lib \
                        --install-platlib=python/lib.$PLAT \
                        --install-scripts=python/scripts
                        --install-data=python/data

或者，等价于

python setup.py install --home=~/python \
                        --install-purelib=lib \
                        --install-platlib='lib.$PLAT' \
                        --install-scripts=scripts
                        --install-data=data

$PLAT is not (necessarily) an environment variable—it will be expanded by the Distutils as it parses your command line options, just as it does when parsing your configuration file(s).

Obviously, specifying the entire installation scheme every time you install a new module distribution would be very tedious. Thus, you can put these options into your Distutils config file (see section Distutils Configuration Files):

[install]
install-base=$HOME
install-purelib=python/lib
install-platlib=python/lib.$PLAT
install-scripts=python/scripts
install-data=python/data

或者，等价于，

[install]
install-base=$HOME/python
install-purelib=lib
install-platlib=lib.$PLAT
install-scripts=scripts
install-data=data

Note that these two are not equivalent if you supply a different installation base directory when you run the setup script. For example,

python setup.py install --install-base=/tmp

would install pure modules to /tmp/python/lib in the first case, and to /tmp/lib in the second case. (For the second case, you probably want to supply an installation base of /tmp/python.)

You probably noticed the use of $HOME and $PLAT in the sample configuration file input. These are Distutils configuration variables, which bear a strong resemblance to environment variables. In fact, you can use environment variables in config files on platforms that have such a notion but the Distutils additionally define a few extra variables that may not be in your environment, such as $PLAT. (And of course, on systems that don’t have environment variables, such as Mac OS 9, the configuration variables supplied by the Distutils are the only ones you can use.) See section Distutils Configuration Files for details.

注解

When a virtual environment is activated, any options that change the installation path will be ignored from all distutils configuration files to prevent inadvertently installing projects outside of the virtual environment.

Modifying Python’s Search Path

When the Python interpreter executes an import statement, it searches for both Python code and extension modules along a search path. A default value for the path is configured into the Python binary when the interpreter is built. You can determine the path by importing the sys module and printing the value of sys.path.

$ python
Python 2.2 (#11, Oct  3 2002, 13:31:27)
[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/local/lib/python2.3', '/usr/local/lib/python2.3/plat-linux2',
 '/usr/local/lib/python2.3/lib-tk', '/usr/local/lib/python2.3/lib-dynload',
 '/usr/local/lib/python2.3/site-packages']
>>>

The null string in sys.path represents the current working directory.

The expected convention for locally installed packages is to put them in the …/site-packages/ directory, but you may want to install Python modules into some arbitrary directory. For example, your site may have a convention of keeping all software related to the web server under /www. Add-on Python modules might then belong in /www/python, and in order to import them, this directory must be added to sys.path. There are several different ways to add the directory.

The most convenient way is to add a path configuration file to a directory that’s already on Python’s path, usually to the .../site-packages/ directory. Path configuration files have an extension of .pth, and each line must contain a single path that will be appended to sys.path. (Because the new paths are appended to sys.path, modules in the added directories will not override standard modules. This means you can’t use this mechanism for installing fixed versions of standard modules.)

Paths can be absolute or relative, in which case they’re relative to the directory containing the .pth file. See the documentation of the site module for more information.

A slightly less convenient way is to edit the site.py file in Python’s standard library, and modify sys.path. site.py is automatically imported when the Python interpreter is executed, unless the -S switch is supplied to suppress this behaviour. So you could simply edit site.py and add two lines to it:

import sys
sys.path.append('/www/python/')

However, if you reinstall the same major version of Python (perhaps when upgrading from 2.2 to 2.2.2, for example) site.py will be overwritten by the stock version. You’d have to remember that it was modified and save a copy before doing the installation.

There are two environment variables that can modify sys.path. PYTHONHOME sets an alternate value for the prefix of the Python installation. For example, if PYTHONHOME is set to /www/python, the search path will be set to ['', '/www/python/lib/pythonX.Y/', '/www/python/lib/pythonX.Y/plat-linux2', ...].

The PYTHONPATH variable can be set to a list of paths that will be added to the beginning of sys.path. For example, if PYTHONPATH is set to /www/python:/opt/py, the search path will begin with ['/www/python', '/opt/py']. (Note that directories must exist in order to be added to sys.path; the site module removes paths that don’t exist.)

Finally, sys.path is just a regular Python list, so any Python application can modify it by adding or removing entries.

Distutils Configuration Files

As mentioned above, you can use Distutils configuration files to record personal or site preferences for any Distutils options. That is, any option to any command can be stored in one of two or three (depending on your platform) configuration files, which will be consulted before the command-line is parsed. This means that configuration files will override default values, and the command-line will in turn override configuration files. Furthermore, if multiple configuration files apply, values from “earlier” files are overridden by “later” files.

Location and names of config files

The names and locations of the configuration files vary slightly across platforms. On Unix and Mac OS X, the three configuration files (in the order they are processed) are:

文件类型

位置和文件名

注释

system

prefix/lib/pythonver/distutils/distutils.cfg

(1)

personal

$HOME/.pydistutils.cfg

(2)

local

setup.cfg

(3)

And on Windows, the configuration files are:

文件类型

位置和文件名

注释

system

prefix\Lib\distutils\distutils.cfg

(4)

personal

%HOME%\pydistutils.cfg

(5)

local

setup.cfg

(3)

On all platforms, the “personal” file can be temporarily disabled by passing the –no-user-cfg option.

注释:

Strictly speaking, the system-wide configuration file lives in the directory where the Distutils are installed; under Python 1.6 and later on Unix, this is as shown. For Python 1.5.2, the Distutils will normally be installed to prefix/lib/python1.5/site-packages/distutils, so the system configuration file should be put there under Python 1.5.2.

On Unix, if the HOME environment variable is not defined, the user’s home directory will be determined with the getpwuid() function from the standard pwd module. This is done by the os.path.expanduser() function used by Distutils.

I.e., in the current directory (usually the location of the setup script).

(See also note (1).) Under Python 1.6 and later, Python’s default “installation prefix” is C:\Python, so the system configuration file is normally C:\Python\Lib\distutils\distutils.cfg. Under Python 1.5.2, the default prefix was C:\Program Files\Python, and the Distutils were not part of the standard library—so the system configuration file would be C:\Program Files\Python\distutils\distutils.cfg in a standard Python 1.5.2 installation under Windows.

On Windows, if the HOME environment variable is not defined, USERPROFILE then HOMEDRIVE and HOMEPATH will be tried. This is done by the os.path.expanduser() function used by Distutils.

Syntax of config files

The Distutils configuration files all have the same syntax. The config files are grouped into sections. There is one section for each Distutils command, plus a global section for global options that affect every command. Each section consists of one option per line, specified as option=value.

For example, the following is a complete config file that just forces all commands to run quietly by default:

[global]
verbose=0

If this is installed as the system config file, it will affect all processing of any Python module distribution by any user on the current system. If it is installed as your personal config file (on systems that support them), it will affect only module distributions processed by you. And if it is used as the setup.cfg for a particular module distribution, it affects only that distribution.

You could override the default “build base” directory and make the build* commands always forcibly rebuild all files with the following:

[build]
build-base=blib
force=1

which corresponds to the command-line arguments

python setup.py build --build-base=blib --force

except that including the build command on the command-line means that command will be run. Including a particular command in config files has no such implication; it only means that if the command is run, the options in the config file will apply. (Or if other commands that derive values from it are run, they will use the values in the config file.)

You can find out the complete list of options for any command using the --help option, e.g.:

python setup.py build --help

and you can find out the complete list of global options by using --help without a command:

python setup.py --help

See also the “Reference” section of the “Distributing Python Modules” manual.

Building Extensions: Tips and Tricks

Whenever possible, the Distutils try to use the configuration information made available by the Python interpreter used to run the setup.py script. For example, the same compiler and linker flags used to compile Python will also be used for compiling extensions. Usually this will work well, but in complicated situations this might be inappropriate. This section discusses how to override the usual Distutils behaviour.

Tweaking compiler/linker flags

Compiling a Python extension written in C or C++ will sometimes require specifying custom flags for the compiler and linker in order to use a particular library or produce a special kind of object code. This is especially true if the extension hasn’t been tested on your platform, or if you’re trying to cross-compile Python.

In the most general case, the extension author might have foreseen that compiling the extensions would be complicated, and provided a Setup file for you to edit. This will likely only be done if the module distribution contains many separate extension modules, or if they often require elaborate sets of compiler flags in order to work.

A Setup file, if present, is parsed in order to get a list of extensions to build. Each line in a Setup describes a single module. Lines have the following structure:

module ... [sourcefile ...] [cpparg ...] [library ...]

Let’s examine each of the fields in turn.

module is the name of the extension module to be built, and should be a valid Python identifier. You can’t just change this in order to rename a module (edits to the source code would also be needed), so this should be left alone.

sourcefile is anything that’s likely to be a source code file, at least judging by the filename. Filenames ending in .c are assumed to be written in C, filenames ending in .C, .cc, and .c++ are assumed to be C++, and filenames ending in .m or .mm are assumed to be in Objective C.

cpparg is an argument for the C preprocessor, and is anything starting with -I, -D, -U or -C.

library is anything ending in .a or beginning with -l or -L.

If a particular platform requires a special library on your platform, you can add it by editing the Setup file and running python setup.py build. For example, if the module defined by the line

foo foomodule.c

must be linked with the math library libm.a on your platform, simply add -lm to the line:

foo foomodule.c -lm

Arbitrary switches intended for the compiler or the linker can be supplied with the -Xcompiler arg and -Xlinker arg options:

foo foomodule.c -Xcompiler -o32 -Xlinker -shared -lm

The next option after -Xcompiler and -Xlinker will be appended to the proper command line, so in the above example the compiler will be passed the -o32 option, and the linker will be passed -shared. If a compiler option requires an argument, you’ll have to supply multiple -Xcompiler options; for example, to pass -x c++ the Setup file would have to contain -Xcompiler -x -Xcompiler c++.

Compiler flags can also be supplied through setting the CFLAGS environment variable. If set, the contents of CFLAGS will be added to the compiler flags specified in the Setup file.

Using non-Microsoft compilers on Windows

Borland/CodeGear C++

This subsection describes the necessary steps to use Distutils with the Borland C++ compiler version 5.5. First you have to know that Borland’s object file format (OMF) is different from the format used by the Python version you can download from the Python or ActiveState Web site. (Python is built with Microsoft Visual C++, which uses COFF as the object file format.) For this reason you have to convert Python’s library python25.lib into the Borland format. You can do this as follows:

coff2omf python25.lib python25_bcpp.lib

The coff2omf program comes with the Borland compiler. The file python25.lib is in the Libs directory of your Python installation. If your extension uses other libraries (zlib, …) you have to convert them too.

The converted files have to reside in the same directories as the normal libraries.

How does Distutils manage to use these libraries with their changed names? If the extension needs a library (eg. foo) Distutils checks first if it finds a library with suffix _bcpp (eg. foo_bcpp.lib) and then uses this library. In the case it doesn’t find such a special library it uses the default name (foo.lib.) 1

To let Distutils compile your extension with Borland C++ you now have to type:

python setup.py build --compiler=bcpp

If you want to use the Borland C++ compiler as the default, you could specify this in your personal or system-wide configuration file for Distutils (see section Distutils Configuration Files.)

参见

C++Builder Compiler [https://www.embarcadero.com/products]

Information about the free C++ compiler from Borland, including links to the download pages.

Creating Python Extensions Using Borland’s Free Compiler [http://www.cyberus.ca/~g_will/pyExtenDL.shtml]

Document describing how to use Borland’s free command-line C++ compiler to build Python.

GNU C / Cygwin / MinGW

This section describes the necessary steps to use Distutils with the GNU C/C++ compilers in their Cygwin and MinGW distributions. 2 For a Python interpreter that was built with Cygwin, everything should work without any of these following steps.

Not all extensions can be built with MinGW or Cygwin, but many can. Extensions most likely to not work are those that use C++ or depend on Microsoft Visual C extensions.

To let Distutils compile your extension with Cygwin you have to type:

python setup.py build --compiler=cygwin

and for Cygwin in no-cygwin mode 3 or for MinGW type:

python setup.py build --compiler=mingw32

If you want to use any of these options/compilers as default, you should consider writing it in your personal or system-wide configuration file for Distutils (see section Distutils Configuration Files.)

Older Versions of Python and MinGW

The following instructions only apply if you’re using a version of Python inferior to 2.4.1 with a MinGW inferior to 3.0.0 (with binutils-2.13.90-20030111-1).

These compilers require some special libraries. This task is more complex than for Borland’s C++, because there is no program to convert the library. First you have to create a list of symbols which the Python DLL exports. (You can find a good program for this task at https://sourceforge.net/projects/mingw/files/MinGW/Extension/pexports/).

pexports python25.dll >python25.def

The location of an installed python25.dll will depend on the installation options and the version and language of Windows. In a “just for me” installation, it will appear in the root of the installation directory. In a shared installation, it will be located in the system directory.

Then you can create from these information an import library for gcc.

/cygwin/bin/dlltool --dllname python25.dll --def python25.def --output-lib libpython25.a

The resulting library has to be placed in the same directory as python25.lib. (Should be the libs directory under your Python installation directory.)

If your extension uses other libraries (zlib,…) you might have to convert them too. The converted files have to reside in the same directories as the normal libraries do.

参见

Building Python modules on MS Windows platform with MinGW [http://old.zope.org/Members/als/tips/win32_mingw_modules]

Information about building the required libraries for the MinGW environment.

备注

1

This also means you could replace all existing COFF-libraries with OMF-libraries of the same name.

2

欲了解更多信息，请查阅 https://www.sourceware.org/cygwin/ 和 http://www.mingw.org/

3

Then you have no POSIX emulation available, but you also don’t need cygwin1.dll.

导航

索引

模块 |

上一页 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

Python 模块索引

_ | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | z

| _ |
| __future__ | Future statement definitions
| __main__ | The environment where the top-level script is run.
| _dummy_thread | Drop-in replacement for the _thread module.
| _thread | Low-level threading API.
| a |
| abc | Abstract base classes according to PEP 3119.
| aifc | Read and write audio files in AIFF or AIFC format.
| argparse | Command-line option and argument parsing library.
| array | Space efficient arrays of uniformly typed numeric values.
| ast | Abstract Syntax Tree classes and manipulation.
| asynchat | Support for asynchronous command/response protocols.
| asyncio | Asynchronous I/O, event loop, coroutines and tasks.
| asyncore | A base class for developing asynchronous socket handling services.
| atexit | Register and execute cleanup functions.
| audioop | Manipulate raw audio data.
| b |
| base64 | RFC 3548: Base16, Base32, Base64 Data Encodings; Base85 and Ascii85
| bdb | Debugger framework.
| binascii | Tools for converting between binary and various ASCII-encoded binary representations.
| binhex | Encode and decode files in binhex4 format.
| bisect | Array bisection algorithms for binary searching.
| builtins | The module that provides the built-in namespace.
| bz2 | Interfaces for bzip2 compression and decompression.
| c |
| calendar | Functions for working with calendars, including some emulation of the Unix cal program.
| cgi | Helpers for running Python scripts via the Common Gateway Interface.
| cgitb | Configurable traceback handler for CGI scripts.
| chunk | Module to read IFF chunks.
| cmath | Mathematical functions for complex numbers.
| cmd | Build line-oriented command interpreters.
| code | Facilities to implement read-eval-print loops.
| codecs | Encode and decode data and streams.
| codeop | Compile (possibly incomplete) Python code.
- | collections | Container datatypes
|     collections.abc | Abstract base classes for containers
| colorsys | Conversion functions between RGB and other color systems.
| compileall | Tools for byte-compiling all Python source files in a directory tree.
- | concurrent |
|     concurrent.futures | Execute computations concurrently using threads or processes.
| configparser | Configuration file parser.
| contextlib | Utilities for with-statement contexts.
| copy | Shallow and deep copy operations.
| copyreg | Register pickle support functions.
| cProfile |
| crypt (Unix) | The crypt() function used to check Unix passwords.
| csv | Write and read tabular data to and from delimited files.
| ctypes | A foreign function library for Python.
- | curses (Unix) | An interface to the curses library, providing portable terminal handling.
|     curses.ascii | Constants and set-membership functions for ASCII characters.
|     curses.panel | A panel stack extension that adds depth to curses windows.
|     curses.textpad | Emacs-like input editing in a curses window.
| d |
| datetime | Basic date and time types.
- | dbm | Interfaces to various Unix "database" formats.
|     dbm.dumb | Portable implementation of the simple DBM interface.
|     dbm.gnu (Unix) | GNU's reinterpretation of dbm.
|     dbm.ndbm (Unix) | The standard "database" interface, based on ndbm.
| decimal | Implementation of the General Decimal Arithmetic Specification.
| difflib | Helpers for computing differences between objects.
| dis | Disassembler for Python bytecode.
- | distutils | Support for building and installing Python modules into an existing Python installation.
|     distutils.archive_util | Utility functions for creating archive files (tarballs, zip files, ...)
|     distutils.bcppcompiler |
|     distutils.ccompiler | Abstract CCompiler class
|     distutils.cmd | This module provides the abstract base class Command. This class is subclassed by the modules in the distutils.command subpackage.
|     distutils.command | This subpackage contains one module for each standard Distutils command.
|     distutils.command.bdist | Build a binary installer for a package
|     distutils.command.bdist_dumb | Build a "dumb" installer - a simple archive of files
|     distutils.command.bdist_msi | Build a binary distribution as a Windows MSI file
|     distutils.command.bdist_packager | Abstract base class for packagers
|     distutils.command.bdist_rpm | Build a binary distribution as a Redhat RPM and SRPM
|     distutils.command.bdist_wininst | Build a Windows installer
|     distutils.command.build | Build all files of a package
|     distutils.command.build_clib | Build any C libraries in a package
|     distutils.command.build_ext | Build any extensions in a package
|     distutils.command.build_py | Build the .py/.pyc files of a package
|     distutils.command.build_scripts | Build the scripts of a package
|     distutils.command.check | Check the metadata of a package
|     distutils.command.clean | Clean a package build area
|     distutils.command.config | Perform package configuration
|     distutils.command.install | Install a package
|     distutils.command.install_data | Install data files from a package
|     distutils.command.install_headers | Install C/C++ header files from a package
|     distutils.command.install_lib | Install library files from a package
|     distutils.command.install_scripts | Install script files from a package
|     distutils.command.register | Register a module with the Python Package Index
|     distutils.command.sdist | Build a source distribution
|     distutils.core | The core Distutils functionality
|     distutils.cygwinccompiler |
|     distutils.debug | Provides the debug flag for distutils
|     distutils.dep_util | Utility functions for simple dependency checking
|     distutils.dir_util | Utility functions for operating on directories and directory trees
|     distutils.dist | Provides the Distribution class, which represents the module distribution being built/installed/distributed
|     distutils.errors | Provides standard distutils exceptions
|     distutils.extension | Provides the Extension class, used to describe C/C++ extension modules in setup scripts
|     distutils.fancy_getopt | Additional getopt functionality
|     distutils.file_util | Utility functions for operating on single files
|     distutils.filelist | The FileList class, used for poking about the file system and building lists of files.
|     distutils.log | A simple logging mechanism, 282-style
|     distutils.msvccompiler | Microsoft Compiler
|     distutils.spawn | Provides the spawn() function
|     distutils.sysconfig | Low-level access to configuration information of the Python interpreter.
|     distutils.text_file | provides the TextFile class, a simple interface to text files
|     distutils.unixccompiler | UNIX C Compiler
|     distutils.util | Miscellaneous other utility functions
|     distutils.version | implements classes that represent module version numbers.
| doctest | Test pieces of code within docstrings.
| dummy_threading | Drop-in replacement for the threading module.
| e |
- | email | Package supporting the parsing, manipulating, and generating email messages.
|     email.charset | Character Sets
|     email.contentmanager | Storing and Retrieving Content from MIME Parts
|     email.encoders | Encoders for email message payloads.
|     email.errors | The exception classes used by the email package.
|     email.generator | Generate flat text email messages from a message structure.
|     email.header | Representing non-ASCII headers
|     email.headerregistry | Automatic Parsing of headers based on the field name
|     email.iterators | Iterate over a message object tree.
|     email.message | The base class representing email messages.
|     email.mime | Build MIME messages.
|     email.parser | Parse flat text email messages to produce a message object structure.
|     email.policy | Controlling the parsing and generating of messages
|     email.utils | Miscellaneous email package utilities.
- | encodings |
|     encodings.idna | Internationalized Domain Names implementation
|     encodings.mbcs | Windows ANSI codepage
|     encodings.utf_8_sig | UTF-8 codec with BOM signature
| ensurepip | Bootstrapping the "pip" installer into an existing Python installation or virtual environment.
| enum | Implementation of an enumeration class.
| errno | Standard errno system symbols.
| f |
| faulthandler | Dump the Python traceback.
| fcntl (Unix) | The fcntl() and ioctl() system calls.
| filecmp | Compare files efficiently.
| fileinput | Loop over standard input or a list of files.
| fnmatch | Unix shell style filename pattern matching.
| formatter | 已移除: Generic output formatter and device interface.
| fpectl (Unix) | Provide control for floating point exception handling.
| fractions | Rational numbers.
| ftplib | FTP protocol client (requires sockets).
| functools | Higher-order functions and operations on callable objects.
| g |
| gc | Interface to the cycle-detecting garbage collector.
| getopt | Portable parser for command line options; support both short and long option names.
| getpass | Portable reading of passwords and retrieval of the userid.
| gettext | Multilingual internationalization services.
| glob | Unix shell style pathname pattern expansion.
| grp (Unix) | The group database (getgrnam() and friends).
| gzip | Interfaces for gzip compression and decompression using file objects.
| h |
| hashlib | Secure hash and message digest algorithms.
| heapq | Heap queue algorithm (a.k.a. priority queue).
| hmac | Keyed-Hashing for Message Authentication (HMAC) implementation
- | html | Helpers for manipulating HTML.
|     html.entities | Definitions of HTML general entities.
|     html.parser | A simple parser that can handle HTML and XHTML.
- | http | HTTP status codes and messages
|     http.client | HTTP and HTTPS protocol client (requires sockets).
|     http.cookiejar | Classes for automatic handling of HTTP cookies.
|     http.cookies | Support for HTTP state management (cookies).
|     http.server | HTTP server and request handlers.
| i |
| imaplib | IMAP4 protocol client (requires sockets).
| imghdr | Determine the type of image contained in a file or byte stream.
| imp | 已移除: Access the implementation of the import statement.
- | importlib | The implementation of the import machinery.
|     importlib.abc | Abstract base classes related to import
|     importlib.machinery | Importers and path hooks
|     importlib.util | Utility code for importers
| inspect | Extract information and source code from live objects.
| io | Core tools for working with streams.
| ipaddress | IPv4/IPv6 manipulation library.
| itertools | Functions creating iterators for efficient looping.
| j |
- | json | Encode and decode the JSON format.
|     json.tool | A command line to validate and pretty-print JSON.
| k |
| keyword | Test whether a string is a keyword in Python.
| l |
| lib2to3 | the 2to3 library
| linecache | This module provides random access to individual lines from text files.
| locale | Internationalization services.
- | logging | Flexible event logging system for applications.
|     logging.config | Configuration of the logging module.
|     logging.handlers | Handlers for the logging module.
| lzma | A Python wrapper for the liblzma compression library.
| m |
| macpath | Mac OS 9 path manipulation functions.
| mailbox | Manipulate mailboxes in various formats
| mailcap | Mailcap file handling.
| marshal | Convert Python objects to streams of bytes and back (with different constraints).
| math | Mathematical functions (sin() etc.).
| mimetypes | Mapping of filename extensions to MIME types.
| mmap | Interface to memory-mapped files for Unix and Windows.
| modulefinder | Find modules used by a script.
| msilib (Windows) | Creation of Microsoft Installer files, and CAB files.
| msvcrt (Windows) | Miscellaneous useful routines from the MS VC++ runtime.
- | multiprocessing | Process-based parallelism.
|     multiprocessing.connection | API for dealing with sockets.
|     multiprocessing.dummy | Dumb wrapper around threading.
|     multiprocessing.managers | Share data between process with shared objects.
|     multiprocessing.pool | Create pools of processes.
|     multiprocessing.sharedctypes | Allocate ctypes objects from shared memory.
| n |
| netrc | Loading of .netrc files.
| nis (Unix) | Interface to Sun's NIS (Yellow Pages) library.
| nntplib | NNTP protocol client (requires sockets).
| numbers | Numeric abstract base classes (Complex, Real, Integral, etc.).
| o |
| operator | Functions corresponding to the standard operators.
| optparse | 已移除: Command-line option parsing library.
- | os | Miscellaneous operating system interfaces.
|     os.path | Operations on pathnames.
| ossaudiodev (Linux, FreeBSD) | Access to OSS-compatible audio devices.
| p |
| parser | Access parse trees for Python source code.
| pathlib | Object-oriented filesystem paths
| pdb | The Python debugger for interactive interpreters.
| pickle | Convert Python objects to streams of bytes and back.
| pickletools | Contains extensive comments about the pickle protocols and pickle-machine opcodes, as well as some useful functions.
| pipes (Unix) | A Python interface to Unix shell pipelines.
| pkgutil | Utilities for the import system.
| platform | Retrieves as much platform identifying data as possible.
| plistlib | Generate and parse Mac OS X plist files.
| poplib | POP3 protocol client (requires sockets).
| posix (Unix) | The most common POSIX system calls (normally used via module os).
| pprint | Data pretty printer.
| profile | Python source profiler.
| pstats | Statistics object for use with the profiler.
| pty (Linux) | Pseudo-Terminal Handling for Linux.
| pwd (Unix) | The password database (getpwnam() and friends).
| py_compile | Generate byte-code files from Python source files.
| pyclbr | Supports information extraction for a Python class browser.
| pydoc | Documentation generator and online help system.
| q |
| queue | A synchronized queue class.
| quopri | Encode and decode files using the MIME quoted-printable encoding.
| r |
| random | Generate pseudo-random numbers with various common distributions.
| re | Regular expression operations.
| readline (Unix) | GNU readline support for Python.
| reprlib | Alternate repr() implementation with size limits.
| resource (Unix) | An interface to provide resource usage information on the current process.
| rlcompleter | Python identifier completion, suitable for the GNU readline library.
| runpy | Locate and run Python modules without importing them first.
| s |
| sched | General purpose event scheduler.
| secrets | Generate secure random numbers for managing secrets.
| select | Wait for I/O completion on multiple streams.
| selectors | High-level I/O multiplexing.
| shelve | Python object persistence.
| shlex | Simple lexical analysis for Unix shell-like languages.
| shutil | High-level file operations, including copying.
| signal | Set handlers for asynchronous events.
| site | Module responsible for site-specific configuration.
| smtpd | A SMTP server implementation in Python.
| smtplib | SMTP protocol client (requires sockets).
| sndhdr | Determine type of a sound file.
| socket | Low-level networking interface.
| socketserver | A framework for network servers.
| spwd (Unix) | The shadow password database (getspnam() and friends).
| sqlite3 | A DB-API 2.0 implementation using SQLite 3.x.
| ssl | TLS/SSL wrapper for socket objects
| stat | Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat().
| statistics | mathematical statistics functions
| string | Common string operations.
| stringprep | String preparation, as per RFC 3453
| struct | Interpret bytes as packed binary data.
| subprocess | Subprocess management.
| sunau | Provide an interface to the Sun AU sound format.
| symbol | Constants representing internal nodes of the parse tree.
| symtable | Interface to the compiler's internal symbol tables.
| sys | Access system-specific parameters and functions.
| sysconfig | Python's configuration information
| syslog (Unix) | An interface to the Unix syslog library routines.
| t |
| tabnanny | Tool for detecting white space related problems in Python source files in a directory tree.
| tarfile | Read and write tar-format archive files.
| telnetlib | Telnet client class.
| tempfile | Generate temporary files and directories.
| termios (Unix) | POSIX style tty control.
- | test | Regression tests package containing the testing suite for Python.
|     test.support | Support for Python's regression test suite.
| textwrap | Text wrapping and filling
| threading | Thread-based parallelism.
| time | Time access and conversions.
| timeit | Measure the execution time of small code snippets.
- | tkinter | Interface to Tcl/Tk for graphical user interfaces
|     tkinter.scrolledtext (Tk) | Text widget with a vertical scroll bar.
|     tkinter.tix | Tk Extension Widgets for Tkinter
|     tkinter.ttk | Tk themed widget set
| token | Constants representing terminal nodes of the parse tree.
| tokenize | Lexical scanner for Python source code.
| trace | Trace or track Python statement execution.
| traceback | Print or retrieve a stack traceback.
| tracemalloc | Trace memory allocations.
| tty (Unix) | Utility functions that perform common terminal control operations.
| turtle | An educational framework for simple graphics applications
| turtledemo | A viewer for example turtle scripts
| types | Names for built-in types.
| typing | Support for type hints (see PEP 484).
| u |
| unicodedata | Access the Unicode Database.
- | unittest | Unit testing framework for Python.
|     unittest.mock | Mock object library.
- | urllib |
|     urllib.error | Exception classes raised by urllib.request.
|     urllib.parse | Parse URLs into or assemble them from components.
|     urllib.request | Extensible library for opening URLs.
|     urllib.response | Response classes used by urllib.
|     urllib.robotparser | Load a robots.txt file and answer questions about fetchability of other URLs.
| uu | Encode and decode files in uuencode format.
| uuid | UUID objects (universally unique identifiers) according to RFC 4122
| v |
| venv | Creation of virtual environments.
| w |
| warnings | Issue warning messages and control their disposition.
| wave | Provide an interface to the WAV sound format.
| weakref | Support for weak references and weak dictionaries.
| webbrowser | Easy-to-use controller for Web browsers.
| winreg (Windows) | Routines and objects for manipulating the Windows registry.
| winsound (Windows) | Access to the sound-playing machinery for Windows.
- | wsgiref | WSGI Utilities and Reference Implementation.
|     wsgiref.handlers | WSGI server/gateway base classes.
|     wsgiref.headers | WSGI response header tools.
|     wsgiref.simple_server | A simple WSGI HTTP server.
|     wsgiref.util | WSGI environment utilities.
|     wsgiref.validate | WSGI conformance checker.
| x |
| xdrlib | Encoders and decoders for the External Data Representation (XDR).
- | xml | Package containing XML processing modules
|     xml.dom | Document Object Model API for Python.
|     xml.dom.minidom | Minimal Document Object Model (DOM) implementation.
|     xml.dom.pulldom | Support for building partial DOM trees from SAX events.
|     xml.etree.ElementTree | Implementation of the ElementTree API.
|     xml.parsers.expat | An interface to the Expat non-validating XML parser.
|     xml.parsers.expat.errors |
|     xml.parsers.expat.model |
|     xml.sax | Package containing SAX2 base classes and convenience functions.
|     xml.sax.handler | Base classes for SAX event handlers.
|     xml.sax.saxutils | Convenience functions and classes for use with SAX.
|     xml.sax.xmlreader | Interface which SAX-compliant XML parsers must implement.
- | xmlrpc |
|     xmlrpc.client | XML-RPC client access.
|     xmlrpc.server | Basic XML-RPC server implementations.
| z |
| zipapp | Manage executable python zip archives
| zipfile | Read and write ZIP-format archive files.
| zipimport | support for importing Python modules from ZIP archives.
| zlib | Low-level interface to compression and decompression routines compatible with gzip.

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引

按照字母的索引页:

符号 | _ | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | 例 | 內 | 关 | 对 | 模 | 环 | 语 | 运

一页的全部索引 (可能会很多)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

Download Python 3.6.13 Documentation

Last updated on: 2月 26, 2021.

To download an archive containing all the documents for this version of Python in one of various formats, follow one of links in this table. The numbers in the table are the size of the download files in megabytes.

Format | Packed as .zip | Packed as .tar.bz2
PDF (US-Letter paper size) | Download (ca. 13 MB) | Download (ca. 13 MB)
PDF (A4 paper size) | Download (ca. 13 MB) | Download (ca. 13 MB)
HTML | Download (ca. 9 MB) | Download (ca. 6 MB)
Plain Text | Download (ca. 3 MB) | Download (ca. 2 MB)
EPUB | Download (ca. 5.5 MB) |

These archives contain all the content in the documentation.

HTML Help (.chm) files are made available in the "Windows" section on the Python download page.

Unpacking

Unix users should download the .tar.bz2 archives; these are bzipped tar archives and can be handled in the usual way using tar and the bzip2 program. The InfoZIP unzip program can be used to handle the ZIP archives if desired. The .tar.bz2 archives provide the best compression and fastest download times.

Windows users can use the ZIP archives since those are customary on that platform. These are created on Unix using the InfoZIP zip program.

Problems

If you have comments or suggestions for the Python documentation, please send email to docs@python.org.

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – A

A() (在 re 模块中)

A-LAW, [1]

a-LAW

a2b_base64() (在 binascii 模块中)

a2b_hex() (在 binascii 模块中)

a2b_hqx() (在 binascii 模块中)

a2b_qp() (在 binascii 模块中)

a2b_uu() (在 binascii 模块中)

a85decode() (在 base64 模块中)

a85encode() (在 base64 模块中)

ABC (abc 中的类)

abc (模块)

ABCMeta (abc 中的类)

abiflags() (在 sys 模块中)

abort()

(asyncio.DatagramTransport 方法)

(asyncio.WriteTransport 方法)

(ftplib.FTP 方法)

(threading.Barrier 方法)

(在 os 模块中)

above() (curses.panel.Panel 方法)

abs

內置函数, [1]

abs() (decimal.Context 方法)

(內置函数)

(在 operator 模块中)

abspath() (在 os.path 模块中)

abstract base class -- 抽象基类

AbstractBasicAuthHandler (urllib.request 中的类)

abstractclassmethod() (在 abc 模块中)

AbstractContextManager (contextlib 中的类)

AbstractDigestAuthHandler (urllib.request 中的类)

AbstractEventLoop (asyncio 中的类)

AbstractEventLoopPolicy (asyncio 中的类)

AbstractFormatter (formatter 中的类)

abstractmethod() (在 abc 模块中)

abstractproperty() (在 abc 模块中)

AbstractSet (typing 中的类)

abstractstaticmethod() (在 abc 模块中)

AbstractWriter (formatter 中的类)

accept() (asyncore.dispatcher 方法)

(multiprocessing.connection.Listener 方法)

(socket.socket 方法)

access() (在 os 模块中)

accumulate() (在 itertools 模块中)

aclose() (agen 方法)

acos() (在 cmath 模块中)

(在 math 模块中)

acosh() (在 cmath 模块中)

(在 math 模块中)

acquire() (_thread.lock 方法)

(asyncio.Condition 方法)

(asyncio.Lock 方法)

(asyncio.Semaphore 方法)

(logging.Handler 方法)

(multiprocessing.Lock 方法)

(multiprocessing.RLock 方法)

(threading.Condition 方法)

(threading.Lock 方法)

(threading.RLock 方法)

(threading.Semaphore 方法)

acquire_lock() (在 imp 模块中)

Action (argparse 中的类)

action (optparse.Option 属性)

ACTIONS (optparse.Option 属性)

active_children() (在 multiprocessing 模块中)

active_count() (在 threading 模块中)

add() (decimal.Context 方法)

(frozenset 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(msilib.RadioButtonGroup 方法)

(pstats.Stats 方法)

(tarfile.TarFile 方法)

(tkinter.ttk.Notebook 方法)

(在 audioop 模块中)

(在 operator 模块中)

add_alias() (在 email.charset 模块中)

add_alternative() (email.message.EmailMessage 方法)

add_argument() (argparse.ArgumentParser 方法)

add_argument_group() (argparse.ArgumentParser 方法)

add_attachment() (email.message.EmailMessage 方法)

add_cgi_vars() (wsgiref.handlers.BaseHandler 方法)

add_charset() (在 email.charset 模块中)

add_codec() (在 email.charset 模块中)

add_cookie_header() (http.cookiejar.CookieJar 方法)

add_data() (在 msilib 模块中)

add_done_callback() (asyncio.Future 方法)

(concurrent.futures.Future 方法)

add_fallback() (gettext.NullTranslations 方法)

add_file() (msilib.Directory 方法)

add_flag() (mailbox.MaildirMessage 方法)

(mailbox.mboxMessage 方法)

(mailbox.MMDFMessage 方法)

add_flowing_data() (formatter.formatter 方法)

add_folder() (mailbox.Maildir 方法)

(mailbox.MH 方法)

add_get_handler() (email.contentmanager.ContentManager 方法)

add_handler() (urllib.request.OpenerDirector 方法)

add_header() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(urllib.request.Request 方法)

(wsgiref.headers.Headers 方法)

add_history() (在 readline 模块中)

add_hor_rule() (formatter.formatter 方法)

add_include_dir() (distutils.ccompiler.CCompiler 方法)

add_label() (mailbox.BabylMessage 方法)

add_label_data() (formatter.formatter 方法)

add_library() (distutils.ccompiler.CCompiler 方法)

add_library_dir() (distutils.ccompiler.CCompiler 方法)

add_line_break() (formatter.formatter 方法)

add_link_object() (distutils.ccompiler.CCompiler 方法)

add_literal_data() (formatter.formatter 方法)

add_mutually_exclusive_group() (argparse.ArgumentParser 方法)

add_option() (optparse.OptionParser 方法)

add_parent() (urllib.request.BaseHandler 方法)

add_password() (urllib.request.HTTPPasswordMgr 方法)

(urllib.request.HTTPPasswordMgrWithPriorAuth 方法)

add_reader() (asyncio.AbstractEventLoop 方法)

add_related() (email.message.EmailMessage 方法)

add_runtime_library_dir() (distutils.ccompiler.CCompiler 方法)

add_section() (configparser.ConfigParser 方法)

(configparser.RawConfigParser 方法)

add_sequence() (mailbox.MHMessage 方法)

add_set_handler() (email.contentmanager.ContentManager 方法)

add_signal_handler() (asyncio.AbstractEventLoop 方法)

add_stream() (在 msilib 模块中)

add_subparsers() (argparse.ArgumentParser 方法)

add_tables() (在 msilib 模块中)

add_type() (在 mimetypes 模块中)

add_unredirected_header() (urllib.request.Request 方法)

add_writer() (asyncio.AbstractEventLoop 方法)

addch() (curses.window 方法)

addCleanup() (unittest.TestCase 方法)

addcomponent() (turtle.Shape 方法)

addError() (unittest.TestResult 方法)

addExpectedFailure() (unittest.TestResult 方法)

addFailure() (unittest.TestResult 方法)

addfile() (tarfile.TarFile 方法)

addFilter() (logging.Handler 方法)

(logging.Logger 方法)

addHandler() (logging.Logger 方法)

addition

addLevelName() (在 logging 模块中)

addnstr() (curses.window 方法)

AddPackagePath() (在 modulefinder 模块中)

addr (smtpd.SMTPChannel 属性)

addr_spec (email.headerregistry.Address 属性)

Address (email.headerregistry 中的类)

address (email.headerregistry.SingleAddressHeader 属性)

(multiprocessing.connection.Listener 属性)

(multiprocessing.managers.BaseManager 属性)

address_exclude() (ipaddress.IPv4Network 方法)

(ipaddress.IPv6Network 方法)

address_family (socketserver.BaseServer 属性)

address_string() (http.server.BaseHTTPRequestHandler 方法)

addresses (email.headerregistry.AddressHeader 属性)

(email.headerregistry.Group 属性)

AddressHeader (email.headerregistry 中的类)

addressof() (在 ctypes 模块中)

AddressValueError

addshape() (在 turtle 模块中)

addsitedir() (在 site 模块中)

addSkip() (unittest.TestResult 方法)

addstr() (curses.window 方法)

addSubTest() (unittest.TestResult 方法)

addSuccess() (unittest.TestResult 方法)

addTest() (unittest.TestSuite 方法)

addTests() (unittest.TestSuite 方法)

addTypeEqualityFunc() (unittest.TestCase 方法)

addUnexpectedSuccess() (unittest.TestResult 方法)

adjusted() (decimal.Decimal 方法)

adler32() (在 zlib 模块中)

ADPCM, Intel/DVI

adpcm2lin() (在 audioop 模块中)

AF_ALG() (在 socket 模块中)

AF_CAN() (在 socket 模块中)

AF_INET() (在 socket 模块中)

AF_INET6() (在 socket 模块中)

AF_LINK() (在 socket 模块中)

AF_PACKET() (在 socket 模块中)

AF_RDS() (在 socket 模块中)

AF_UNIX() (在 socket 模块中)

aifc (模块)

aifc() (aifc.aifc 方法)

AIFF, [1]

aiff() (aifc.aifc 方法)

AIFF-C, [1]

alarm() (在 signal 模块中)

alaw2lin() (在 audioop 模块中)

ALERT_DESCRIPTION_HANDSHAKE_FAILURE() (在 ssl 模块中)

ALERT_DESCRIPTION_INTERNAL_ERROR() (在 ssl 模块中)

AlertDescription (ssl 中的类)

algorithms_available() (在 hashlib 模块中)

algorithms_guaranteed() (在 hashlib 模块中)

alias (pdb command)

alignment() (在 ctypes 模块中)

alive (weakref.finalize 属性)

all() (內置函数)

all_errors() (在 ftplib 模块中)

all_features() (在 xml.sax.handler 模块中)

all_frames (tracemalloc.Filter 属性)

all_properties() (在 xml.sax.handler 模块中)

all_suffixes() (在 importlib.machinery 模块中)

all_tasks() (asyncio.Task 类方法)

allocate_lock() (在 _thread 模块中)

allow_reuse_address (socketserver.BaseServer 属性)

allowed_domains() (http.cookiejar.DefaultCookiePolicy 方法)

alt() (在 curses.ascii 模块中)

ALT_DIGITS() (在 locale 模块中)

altsep() (在 os 模块中)

altzone() (在 time 模块中)

ALWAYS_TYPED_ACTIONS (optparse.Option 属性)

AMPER() (在 token 模块中)

AMPEREQUAL() (在 token 模块中)

and

bitwise

运算符, [1], [2]

and_() (在 operator 模块中)

annotated

assignment

annotation (inspect.Parameter 属性)

annotation -- 标注

annotations

function, [1]

announce() (distutils.ccompiler.CCompiler 方法)

anonymous

function

answer_challenge() (在 multiprocessing.connection 模块中)

anticipate_failure() (在 test.support 模块中)

any() (內置函数)

Any() (在 typing 模块中)

ANY() (在 unittest.mock 模块中)

AnyStr() (在 typing 模块中)

api_version() (在 sys 模块中)

apop() (poplib.POP3 方法)

APPDATA

append() (array.array 方法)

(collections.deque 方法)

(email.header.Header 方法)

(imaplib.IMAP4 方法)

(msilib.CAB 方法)

(pipes.Template 方法)

(sequence method)

(xml.etree.ElementTree.Element 方法)

append_history_file() (在 readline 模块中)

appendChild() (xml.dom.Node 方法)

appendleft() (collections.deque 方法)

application_uri() (在 wsgiref.util 模块中)

apply (2to3 fixer)

apply() (multiprocessing.pool.Pool 方法)

apply_async() (multiprocessing.pool.Pool 方法)

apply_defaults() (inspect.BoundArguments 方法)

architecture() (在 platform 模块中)

archive (zipimport.zipimporter 属性)

aRepr() (在 reprlib 模块中)

argparse (模块)

args (BaseException 属性)

(functools.partial 属性)

(inspect.BoundArguments 属性)

(pdb command)

(subprocess.CompletedProcess 属性)

(subprocess.Popen 属性)

argtypes (ctypes._FuncPtr 属性)

argument

call semantics

difference from parameter

function

function definition

argument -- 参数

ArgumentDefaultsHelpFormatter (argparse 中的类)

ArgumentError

ArgumentParser (argparse 中的类)

arguments (inspect.BoundArguments 属性)

argv (in module sys)

argv() (在 sys 模块中)

arithmetic

conversion

operation, binary

operation, unary

ArithmeticError

array

模块, [1]

array (array 中的类)

Array (ctypes 中的类)

array (模块)

Array() (multiprocessing.managers.SyncManager 方法)

(在 multiprocessing 模块中)

(在 multiprocessing.sharedctypes 模块中)

arrays

arraysize (sqlite3.Cursor 属性)

article() (nntplib.NNTP 方法)

as

except clause

import statement

with statement

关键字, [1], [2]

as_bytes() (email.message.EmailMessage 方法)

(email.message.Message 方法)

as_completed() (在 asyncio 模块中)

(在 concurrent.futures 模块中)

as_integer_ratio() (decimal.Decimal 方法)

(float 方法)

AS_IS() (在 formatter 模块中)

as_posix() (pathlib.PurePath 方法)

as_string() (email.message.EmailMessage 方法)

(email.message.Message 方法)

as_tuple() (decimal.Decimal 方法)

as_uri() (pathlib.PurePath 方法)

ASCII, [1]

ascii

內置函数

ascii() (內置函数)

(在 curses.ascii 模块中)

ASCII() (在 re 模块中)

ascii_letters() (在 string 模块中)

ascii_lowercase() (在 string 模块中)

ascii_uppercase() (在 string 模块中)

asctime() (在 time 模块中)

asend() (agen 方法)

asin() (在 cmath 模块中)

(在 math 模块中)

asinh() (在 cmath 模块中)

(在 math 模块中)

assert

语句, [1]

assert_any_call() (unittest.mock.Mock 方法)

assert_called() (unittest.mock.Mock 方法)

assert_called_once() (unittest.mock.Mock 方法)

assert_called_once_with() (unittest.mock.Mock 方法)

assert_called_with() (unittest.mock.Mock 方法)

assert_has_calls() (unittest.mock.Mock 方法)

assert_line_data() (formatter.formatter 方法)

assert_not_called() (unittest.mock.Mock 方法)

assertAlmostEqual() (unittest.TestCase 方法)

assertCountEqual() (unittest.TestCase 方法)

assertDictEqual() (unittest.TestCase 方法)

assertEqual() (unittest.TestCase 方法)

assertFalse() (unittest.TestCase 方法)

assertGreater() (unittest.TestCase 方法)

assertGreaterEqual() (unittest.TestCase 方法)

assertIn() (unittest.TestCase 方法)

AssertionError

例外

assertions

debugging

assertIs() (unittest.TestCase 方法)

assertIsInstance() (unittest.TestCase 方法)

assertIsNone() (unittest.TestCase 方法)

assertIsNot() (unittest.TestCase 方法)

assertIsNotNone() (unittest.TestCase 方法)

assertLess() (unittest.TestCase 方法)

assertLessEqual() (unittest.TestCase 方法)

assertListEqual() (unittest.TestCase 方法)

assertLogs() (unittest.TestCase 方法)

assertMultiLineEqual() (unittest.TestCase 方法)

assertNotAlmostEqual() (unittest.TestCase 方法)

assertNotEqual() (unittest.TestCase 方法)

assertNotIn() (unittest.TestCase 方法)

assertNotIsInstance() (unittest.TestCase 方法)

assertNotRegex() (unittest.TestCase 方法)

assertRaises() (unittest.TestCase 方法)

assertRaisesRegex() (unittest.TestCase 方法)

assertRegex() (unittest.TestCase 方法)

asserts (2to3 fixer)

assertSequenceEqual() (unittest.TestCase 方法)

assertSetEqual() (unittest.TestCase 方法)

assertTrue() (unittest.TestCase 方法)

assertTupleEqual() (unittest.TestCase 方法)

assertWarns() (unittest.TestCase 方法)

assertWarnsRegex() (unittest.TestCase 方法)

assignment

annotated

attribute, [1]

augmented

class attribute

class instance attribute

slice

slicing

statement, [1]

subscript

subscription

target list

AST (ast 中的类)

ast (模块)

astimezone() (datetime.datetime 方法)

async

关键字

async def

语句

async for

in comprehensions, [1]

语句

async with

语句

async() (在 asyncio 模块中)

ASYNC() (在 token 模块中)

async_chat (asynchat 中的类)

async_chat.ac_in_buffer_size() (在 asynchat 模块中)

async_chat.ac_out_buffer_size() (在 asynchat 模块中)

AsyncContextManager (typing 中的类)

AsyncGenerator (collections.abc 中的类)

(typing 中的类)

AsyncGeneratorType() (在 types 模块中)

asynchat (模块)

asynchronous context manager -- 异步上下文管理器

asynchronous generator

asynchronous iterator

function

asynchronous generator -- 异步生成器

asynchronous generator iterator -- 异步生成器迭代器

asynchronous iterable -- 异步可迭代对象

asynchronous iterator -- 异步迭代器

asynchronous-generator

对象

asyncio (模块)

asyncio.subprocess.DEVNULL() (在 asyncio 模块中)

asyncio.subprocess.PIPE() (在 asyncio 模块中)

asyncio.subprocess.Process (asyncio 中的类)

asyncio.subprocess.STDOUT() (在 asyncio 模块中)

AsyncIterable (collections.abc 中的类)

(typing 中的类)

AsyncIterator (collections.abc 中的类)

(typing 中的类)

asyncore (模块)

AsyncResult (multiprocessing.pool 中的类)

AT() (在 token 模块中)

at_eof() (asyncio.StreamReader 方法)

atan() (在 cmath 模块中)

(在 math 模块中)

atan2() (在 math 模块中)

atanh() (在 cmath 模块中)

(在 math 模块中)

ATEQUAL() (在 token 模块中)

atexit (weakref.finalize 属性)

(模块)

athrow() (agen 方法)

atof() (在 locale 模块中)

atoi() (在 locale 模块中)

atom

attach() (email.message.Message 方法)

attach_mock() (unittest.mock.Mock 方法)

AttlistDeclHandler() (xml.parsers.expat.xmlparser 方法)

attrgetter() (在 operator 模块中)

attrib (xml.etree.ElementTree.Element 属性)

attribute

assignment, [1]

assignment, class

assignment, class instance

class

class instance

deletion

generic special

reference

special

attribute -- 属性

AttributeError

例外

attributes (xml.dom.Node 属性)

AttributesImpl (xml.sax.xmlreader 中的类)

AttributesNSImpl (xml.sax.xmlreader 中的类)

attroff() (curses.window 方法)

attron() (curses.window 方法)

attrset() (curses.window 方法)

Audio Interchange File Format, [1]

AUDIO_FILE_ENCODING_ADPCM_G721() (在 sunau 模块中)

AUDIO_FILE_ENCODING_ADPCM_G722() (在 sunau 模块中)

AUDIO_FILE_ENCODING_ADPCM_G723_3() (在 sunau 模块中)

AUDIO_FILE_ENCODING_ADPCM_G723_5() (在 sunau 模块中)

AUDIO_FILE_ENCODING_ALAW_8() (在 sunau 模块中)

AUDIO_FILE_ENCODING_DOUBLE() (在 sunau 模块中)

AUDIO_FILE_ENCODING_FLOAT() (在 sunau 模块中)

AUDIO_FILE_ENCODING_LINEAR_16() (在 sunau 模块中)

AUDIO_FILE_ENCODING_LINEAR_24() (在 sunau 模块中)

AUDIO_FILE_ENCODING_LINEAR_32() (在 sunau 模块中)

AUDIO_FILE_ENCODING_LINEAR_8() (在 sunau 模块中)

AUDIO_FILE_ENCODING_MULAW_8() (在 sunau 模块中)

AUDIO_FILE_MAGIC() (在 sunau 模块中)

AUDIODEV

audioop (模块)

augmented

assignment

auth() (ftplib.FTP_TLS 方法)

(smtplib.SMTP 方法)

authenticate() (imaplib.IMAP4 方法)

AuthenticationError

authenticators() (netrc.netrc 方法)

authkey (multiprocessing.Process 属性)

auto (enum 中的类)

autorange() (timeit.Timer 方法)

avg() (在 audioop 模块中)

avgpp() (在 audioop 模块中)

avoids_symlink_attacks (shutil.rmtree 属性)

await

in comprehensions

关键字, [1], [2]

AWAIT() (在 token 模块中)

Awaitable (collections.abc 中的类)

(typing 中的类)

awaitable -- 可等待对象

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – B

b"

bytes literal

b'

bytes literal

b16decode() (在 base64 模块中)

b16encode() (在 base64 模块中)

b2a_base64() (在 binascii 模块中)

b2a_hex() (在 binascii 模块中)

b2a_hqx() (在 binascii 模块中)

b2a_qp() (在 binascii 模块中)

b2a_uu() (在 binascii 模块中)

b32decode() (在 base64 模块中)

b32encode() (在 base64 模块中)

b64decode() (在 base64 模块中)

b64encode() (在 base64 模块中)

b85decode() (在 base64 模块中)

b85encode() (在 base64 模块中)

Babyl (mailbox 中的类)

BabylMessage (mailbox 中的类)

back() (在 turtle 模块中)

backslash character

backslashreplace_errors() (在 codecs 模块中)

backward() (在 turtle 模块中)

BadStatusLine

BadZipFile

BadZipfile

Balloon (tkinter.tix 中的类)

Barrier (multiprocessing 中的类)

(threading 中的类)

Barrier() (multiprocessing.managers.SyncManager 方法)

base64

encoding

模块

base64 (模块)

base_exec_prefix() (在 sys 模块中)

base_prefix() (在 sys 模块中)

BaseCGIHandler (wsgiref.handlers 中的类)

BaseCookie (http.cookies 中的类)

BaseEventLoop (asyncio 中的类)

BaseException

BaseHandler (urllib.request 中的类)

(wsgiref.handlers 中的类)

BaseHeader (email.headerregistry 中的类)

BaseHTTPRequestHandler (http.server 中的类)

BaseManager (multiprocessing.managers 中的类)

basename() (在 os.path 模块中)

BaseProxy (multiprocessing.managers 中的类)

BaseRequestHandler (socketserver 中的类)

BaseRotatingHandler (logging.handlers 中的类)

BaseSelector (selectors 中的类)

BaseServer (socketserver 中的类)

basestring (2to3 fixer)

BaseSubprocessTransport (asyncio 中的类)

BaseTransport (asyncio 中的类)

basicConfig() (在 logging 模块中)

BasicContext (decimal 中的类)

BasicInterpolation (configparser 中的类)

baudrate() (在 curses 模块中)

bbox() (tkinter.ttk.Treeview 方法)

BDADDR_ANY() (在 socket 模块中)

BDADDR_LOCAL() (在 socket 模块中)

bdb

模块

Bdb (bdb 中的类)

bdb (模块)

BdbQuit

BDFL

bdist_msi (distutils.command.bdist_msi 中的类)

beep() (在 curses 模块中)

Beep() (在 winsound 模块中)

BEFORE_ASYNC_WITH (opcode)

begin_fill() (在 turtle 模块中)

begin_poly() (在 turtle 模块中)

below() (curses.panel.Panel 方法)

Benchmarking

benchmarking

betavariate() (在 random 模块中)

bgcolor() (在 turtle 模块中)

bgpic() (在 turtle 模块中)

bias() (在 audioop 模块中)

bidirectional() (在 unicodedata 模块中)

BigEndianStructure (ctypes 中的类)

bin() (內置函数)

binary

arithmetic operation

bitwise operation

data, packing

literals

Binary (msilib 中的类)

(xmlrpc.client 中的类)

binary file -- 二进制文件

binary literal

binary mode

binary semaphores

BINARY_ADD (opcode)

BINARY_AND (opcode)

BINARY_FLOOR_DIVIDE (opcode)

BINARY_LSHIFT (opcode)

BINARY_MATRIX_MULTIPLY (opcode)

BINARY_MODULO (opcode)

BINARY_MULTIPLY (opcode)

BINARY_OR (opcode)

BINARY_POWER (opcode)

BINARY_RSHIFT (opcode)

BINARY_SUBSCR (opcode)

BINARY_SUBTRACT (opcode)

BINARY_TRUE_DIVIDE (opcode)

BINARY_XOR (opcode)

BinaryIO (typing 中的类)

binascii (模块)

bind (widgets)

bind() (asyncore.dispatcher 方法)

(inspect.Signature 方法)

(socket.socket 方法)

bind_partial() (inspect.Signature 方法)

bind_port() (在 test.support 模块中)

bind_textdomain_codeset() (在 gettext 模块中)

binding

global name

name, [1], [2], [3], [4], [5], [6]

bindtextdomain() (在 gettext 模块中)

(在 locale 模块中)

binhex

模块

binhex (模块)

binhex() (在 binhex 模块中)

bisect (模块)

bisect() (在 bisect 模块中)

bisect_left() (在 bisect 模块中)

bisect_right() (在 bisect 模块中)

bit_length() (int 方法)

bitmap() (msilib.Dialog 方法)

bitwise

and

operation, binary

operation, unary

operations

or

xor

bk() (在 turtle 模块中)

bkgd() (curses.window 方法)

bkgdset() (curses.window 方法)

blake2b() (在 hashlib 模块中)

blake2b, blake2s

blake2b.MAX_DIGEST_SIZE() (在 hashlib 模块中)

blake2b.MAX_KEY_SIZE() (在 hashlib 模块中)

blake2b.PERSON_SIZE() (在 hashlib 模块中)

blake2b.SALT_SIZE() (在 hashlib 模块中)

blake2s() (在 hashlib 模块中)

blake2s.MAX_DIGEST_SIZE() (在 hashlib 模块中)

blake2s.MAX_KEY_SIZE() (在 hashlib 模块中)

blake2s.PERSON_SIZE() (在 hashlib 模块中)

blake2s.SALT_SIZE() (在 hashlib 模块中)

blank line

block

code

block_size (hmac.HMAC 属性)

blocked_domains() (http.cookiejar.DefaultCookiePolicy 方法)

BlockingIOError, [1]

BNF, [1]

body() (nntplib.NNTP 方法)

body_encode() (email.charset.Charset 方法)

body_encoding (email.charset.Charset 属性)

body_line_iterator() (在 email.iterators 模块中)

BOM() (在 codecs 模块中)

BOM_BE() (在 codecs 模块中)

BOM_LE() (在 codecs 模块中)

BOM_UTF16() (在 codecs 模块中)

BOM_UTF16_BE() (在 codecs 模块中)

BOM_UTF16_LE() (在 codecs 模块中)

BOM_UTF32() (在 codecs 模块中)

BOM_UTF32_BE() (在 codecs 模块中)

BOM_UTF32_LE() (在 codecs 模块中)

BOM_UTF8() (在 codecs 模块中)

bool (內置类)

Boolean

operation

operations, [1]

type

values

对象, [1]

BOOLEAN_STATES (configparser.ConfigParser 属性)

bootstrap() (在 ensurepip 模块中)

border() (curses.window 方法)

bottom() (curses.panel.Panel 方法)

bottom_panel() (在 curses.panel 模块中)

BoundArguments (inspect 中的类)

BoundaryError

BoundedSemaphore (asyncio 中的类)

(multiprocessing 中的类)

(threading 中的类)

BoundedSemaphore() (multiprocessing.managers.SyncManager 方法)

box() (curses.window 方法)

bpformat() (bdb.Breakpoint 方法)

bpprint() (bdb.Breakpoint 方法)

break

语句, [1], [2], [3], [4]

break (pdb command)

break_anywhere() (bdb.Bdb 方法)

break_here() (bdb.Bdb 方法)

break_long_words (textwrap.TextWrapper 属性)

BREAK_LOOP (opcode)

break_on_hyphens (textwrap.TextWrapper 属性)

Breakpoint (bdb 中的类)

breakpoints

broadcast_address (ipaddress.IPv4Network 属性)

(ipaddress.IPv6Network 属性)

broken (threading.Barrier 属性)

BrokenBarrierError

BrokenPipeError

BrokenProcessPool

BROWSER, [1]

BsdDbShelf (shelve 中的类)

buffer (2to3 fixer)

(io.TextIOBase 属性)

(unittest.TestResult 属性)

buffer interface

(see buffer protocol)

buffer object

(see buffer protocol)

buffer protocol

binary sequence types

str (built-in class)

buffer size, I/O

buffer_info() (array.array 方法)

buffer_size (xml.parsers.expat.xmlparser 属性)

buffer_text (xml.parsers.expat.xmlparser 属性)

buffer_used (xml.parsers.expat.xmlparser 属性)

BufferedIOBase (io 中的类)

BufferedRandom (io 中的类)

BufferedReader (io 中的类)

BufferedRWPair (io 中的类)

BufferedWriter (io 中的类)

BufferError

BufferingHandler (logging.handlers 中的类)

BufferTooShort

bufsize() (ossaudiodev.oss_audio_device 方法)

BUILD_CONST_KEY_MAP (opcode)

BUILD_LIST (opcode)

BUILD_LIST_UNPACK (opcode)

BUILD_MAP (opcode)

BUILD_MAP_UNPACK (opcode)

BUILD_MAP_UNPACK_WITH_CALL (opcode)

build_opener() (在 urllib.request 模块中)

build_py (distutils.command.build_py 中的类)

build_py_2to3 (distutils.command.build_py 中的类)

BUILD_SET (opcode)

BUILD_SET_UNPACK (opcode)

BUILD_SLICE (opcode)

BUILD_STRING (opcode)

BUILD_TUPLE (opcode)

BUILD_TUPLE_UNPACK (opcode)

BUILD_TUPLE_UNPACK_WITH_CALL (opcode)

built-in

method

types

built-in function

call

对象, [1]

built-in method

call

对象, [1]

builtin_module_names() (在 sys 模块中)

BuiltinFunctionType() (在 types 模块中)

BuiltinImporter (importlib.machinery 中的类)

BuiltinMethodType() (在 types 模块中)

builtins

模块, [1], [2], [3], [4]

builtins (模块)

ButtonBox (tkinter.tix 中的类)

bye() (在 turtle 模块中)

byref() (在 ctypes 模块中)

byte

byte-code

file, [1]

byte_compile() (在 distutils.util 模块中)

bytearray

formatting

interpolation

methods

对象, [1], [2], [3]

bytearray (內置类)

bytecode

Bytecode (dis 中的类)

bytecode -- 字节码

Bytecode.codeobj() (在 dis 模块中)

Bytecode.first_line() (在 dis 模块中)

BYTECODE_SUFFIXES() (在 importlib.machinery 模块中)

byteorder() (在 sys 模块中)

bytes

formatting

interpolation

methods

str (built-in class)

內置函数, [1]

对象, [1], [2]

bytes (uuid.UUID 属性)

(內置类)

bytes literal

bytes-like object -- 字节类对象

bytes_le (uuid.UUID 属性)

BytesFeedParser (email.parser 中的类)

BytesGenerator (email.generator 中的类)

BytesHeaderParser (email.parser 中的类)

BytesIO (io 中的类)

BytesParser (email.parser 中的类)

ByteString (collections.abc 中的类)

(typing 中的类)

byteswap() (array.array 方法)

(在 audioop 模块中)

BytesWarning

bz2 (模块)

BZ2Compressor (bz2 中的类)

BZ2Decompressor (bz2 中的类)

BZ2File (bz2 中的类)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – C

C

language, [1], [2], [3], [4], [5]

structures

C-contiguous, [1]

c_bool (ctypes 中的类)

C_BUILTIN() (在 imp 模块中)

c_byte (ctypes 中的类)

c_char (ctypes 中的类)

c_char_p (ctypes 中的类)

c_contiguous (memoryview 属性)

c_double (ctypes 中的类)

C_EXTENSION() (在 imp 模块中)

c_float (ctypes 中的类)

c_int (ctypes 中的类)

c_int16 (ctypes 中的类)

c_int32 (ctypes 中的类)

c_int64 (ctypes 中的类)

c_int8 (ctypes 中的类)

c_long (ctypes 中的类)

c_longdouble (ctypes 中的类)

c_longlong (ctypes 中的类)

c_short (ctypes 中的类)

c_size_t (ctypes 中的类)

c_ssize_t (ctypes 中的类)

c_ubyte (ctypes 中的类)

c_uint (ctypes 中的类)

c_uint16 (ctypes 中的类)

c_uint32 (ctypes 中的类)

c_uint64 (ctypes 中的类)

c_uint8 (ctypes 中的类)

c_ulong (ctypes 中的类)

c_ulonglong (ctypes 中的类)

c_ushort (ctypes 中的类)

c_void_p (ctypes 中的类)

c_wchar (ctypes 中的类)

c_wchar_p (ctypes 中的类)

CAB (msilib 中的类)

cache_from_source() (在 imp 模块中)

(在 importlib.util 模块中)

cached (importlib.machinery.ModuleSpec 属性)

CacheFTPHandler (urllib.request 中的类)

calcsize() (在 struct 模块中)

Calendar (calendar 中的类)

calendar (模块)

calendar() (在 calendar 模块中)

call

built-in function

built-in method

class instance

class object, [1], [2]

function, [1], [2]

instance, [1]

method

procedure

user-defined function

call() (在 subprocess 模块中)

(在 unittest.mock 模块中)

call_args (unittest.mock.Mock 属性)

call_args_list (unittest.mock.Mock 属性)

call_at() (asyncio.AbstractEventLoop 方法)

call_count (unittest.mock.Mock 属性)

call_exception_handler() (asyncio.AbstractEventLoop 方法)

CALL_FUNCTION (opcode)

CALL_FUNCTION_EX (opcode)

CALL_FUNCTION_KW (opcode)

call_later() (asyncio.AbstractEventLoop 方法)

call_list() (unittest.mock.call 方法)

call_soon() (asyncio.AbstractEventLoop 方法)

call_soon_threadsafe() (asyncio.AbstractEventLoop 方法)

call_tracing() (在 sys 模块中)

callable

对象, [1]

Callable (collections.abc 中的类)

callable() (內置函数)

Callable() (在 typing 模块中)

CallableProxyType() (在 weakref 模块中)

callback (optparse.Option 属性)

callback() (contextlib.ExitStack 方法)

callback_args (optparse.Option 属性)

callback_kwargs (optparse.Option 属性)

callbacks() (在 gc 模块中)

called (unittest.mock.Mock 属性)

CalledProcessError

calloc()

CAN_BCM() (在 socket 模块中)

can_change_color() (在 curses 模块中)

can_fetch() (urllib.robotparser.RobotFileParser 方法)

CAN_RAW_FD_FRAMES() (在 socket 模块中)

can_symlink() (在 test.support 模块中)

can_write_eof() (asyncio.StreamWriter 方法)

(asyncio.WriteTransport 方法)

cancel() (asyncio.Future 方法)

(asyncio.Handle 方法)

(asyncio.Task 方法)

(concurrent.futures.Future 方法)

(sched.scheduler 方法)

(threading.Timer 方法)

cancel_dump_traceback_later() (在 faulthandler 模块中)

cancel_join_thread() (multiprocessing.Queue 方法)

cancelled() (asyncio.Future 方法)

(concurrent.futures.Future 方法)

CancelledError

CannotSendHeader

CannotSendRequest

canonic() (bdb.Bdb 方法)

canonical() (decimal.Context 方法)

(decimal.Decimal 方法)

capa() (poplib.POP3 方法)

capitalize() (bytearray 方法)

(bytes 方法)

(str 方法)

Capsule

对象

captured_stderr() (在 test.support 模块中)

captured_stdin() (在 test.support 模块中)

captured_stdout() (在 test.support 模块中)

captureWarnings() (在 logging 模块中)

capwords() (在 string 模块中)

casefold() (str 方法)

cast() (memoryview 方法)

(在 ctypes 模块中)

(在 typing 模块中)

cat() (在 nis 模块中)

catch_warnings (warnings 中的类)

category() (在 unicodedata 模块中)

cbreak() (在 curses 模块中)

CC

ccc() (ftplib.FTP_TLS 方法)

CCompiler (distutils.ccompiler 中的类)

CDLL (ctypes 中的类)

ceil() (in module math)

(在 math 模块中)

center() (bytearray 方法)

(bytes 方法)

(str 方法)

CERT_NONE() (在 ssl 模块中)

CERT_OPTIONAL() (在 ssl 模块中)

CERT_REQUIRED() (在 ssl 模块中)

cert_store_stats() (ssl.SSLContext 方法)

cert_time_to_seconds() (在 ssl 模块中)

CertificateError

certificates

CFLAGS, [1], [2]

CFUNCTYPE() (在 ctypes 模块中)

CGI

debugging

exceptions

protocol

security

tracebacks

cgi (模块)

cgi_directories (http.server.CGIHTTPRequestHandler 属性)

CGIHandler (wsgiref.handlers 中的类)

CGIHTTPRequestHandler (http.server 中的类)

cgitb (模块)

CGIXMLRPCRequestHandler (xmlrpc.server 中的类)

chain() (在 itertools 模块中)

chaining

comparisons, [1]

exception

ChainMap (collections 中的类)

(typing 中的类)

change_cwd() (在 test.support 模块中)

change_root() (在 distutils.util 模块中)

CHANNEL_BINDING_TYPES() (在 ssl 模块中)

channel_class (smtpd.SMTPServer 属性)

channels() (ossaudiodev.oss_audio_device 方法)

CHAR_MAX() (在 locale 模块中)

character, [1], [2]

CharacterDataHandler() (xml.parsers.expat.xmlparser 方法)

characters() (xml.sax.handler.ContentHandler 方法)

characters_written (BlockingIOError 属性)

Charset (email.charset 中的类)

charset() (gettext.NullTranslations 方法)

chdir() (在 os 模块中)

check (lzma.LZMADecompressor 属性)

check() (imaplib.IMAP4 方法)

(在 tabnanny 模块中)

check__all__() (在 test.support 模块中)

check_call() (在 subprocess 模块中)

check_environ() (在 distutils.util 模块中)

check_hostname (ssl.SSLContext 属性)

check_output() (doctest.OutputChecker 方法)

(在 subprocess 模块中)

check_returncode() (subprocess.CompletedProcess 方法)

check_unused_args() (string.Formatter 方法)

check_warnings() (在 test.support 模块中)

checkbox() (msilib.Dialog 方法)

checkcache() (在 linecache 模块中)

checkfuncname() (在 bdb 模块中)

CheckList (tkinter.tix 中的类)

checksum

Cyclic Redundancy Check

chflags() (在 os 模块中)

chgat() (curses.window 方法)

childNodes (xml.dom.Node 属性)

ChildProcessError

chmod() (pathlib.Path 方法)

(在 os 模块中)

choice() (在 random 模块中)

(在 secrets 模块中)

choices (optparse.Option 属性)

choices() (在 random 模块中)

chown() (在 os 模块中)

(在 shutil 模块中)

chr

內置函数

chr() (內置函数)

chroot() (在 os 模块中)

Chunk (chunk 中的类)

chunk (模块)

cipher

DES

cipher() (ssl.SSLSocket 方法)

circle() (在 turtle 模块中)

CIRCUMFLEX() (在 token 模块中)

CIRCUMFLEXEQUAL() (在 token 模块中)

Clamped (decimal 中的类)

class

attribute

attribute assignment

body

constructor

definition, [1]

instance

name

对象, [1], [2]

语句

Class (symtable 中的类)

class -- 类

Class browser

class instance

attribute

attribute assignment

call

对象, [1], [2]

class object

call, [1], [2]

class variable -- 类变量

classmethod

內置函数

classmethod() (內置函数)

ClassVar() (在 typing 模块中)

clause

CLD_CONTINUED() (在 os 模块中)

CLD_DUMPED() (在 os 模块中)

CLD_EXITED() (在 os 模块中)

CLD_TRAPPED() (在 os 模块中)

clean() (mailbox.Maildir 方法)

cleandoc() (在 inspect 模块中)

cleanup functions

clear (pdb command)

Clear Breakpoint

clear() (asyncio.Event 方法)

(collections.deque 方法)

(curses.window 方法)

(dict 方法)

(email.message.EmailMessage 方法)

(frame 方法)

(frozenset 方法)

(http.cookiejar.CookieJar 方法)

(mailbox.Mailbox 方法)

(sequence method)

(threading.Event 方法)

(xml.etree.ElementTree.Element 方法)

(在 turtle 模块中), [1]

clear_all_breaks() (bdb.Bdb 方法)

clear_all_file_breaks() (bdb.Bdb 方法)

clear_bpbynumber() (bdb.Bdb 方法)

clear_break() (bdb.Bdb 方法)

clear_cache() (在 filecmp 模块中)

clear_content() (email.message.EmailMessage 方法)

clear_flags() (decimal.Context 方法)

clear_frames() (在 traceback 模块中)

clear_history() (在 readline 模块中)

clear_session_cookies() (http.cookiejar.CookieJar 方法)

clear_traces() (在 tracemalloc 模块中)

clear_traps() (decimal.Context 方法)

clearcache() (在 linecache 模块中)

ClearData() (msilib.Record 方法)

clearok() (curses.window 方法)

clearscreen() (在 turtle 模块中)

clearstamp() (在 turtle 模块中)

clearstamps() (在 turtle 模块中)

Client() (在 multiprocessing.connection 模块中)

client_address (http.server.BaseHTTPRequestHandler 属性)

clock() (在 time 模块中)

clock_getres() (在 time 模块中)

clock_gettime() (在 time 模块中)

CLOCK_HIGHRES() (在 time 模块中)

CLOCK_MONOTONIC() (在 time 模块中)

CLOCK_MONOTONIC_RAW() (在 time 模块中)

CLOCK_PROCESS_CPUTIME_ID() (在 time 模块中)

CLOCK_REALTIME() (在 time 模块中)

clock_settime() (在 time 模块中)

CLOCK_THREAD_CPUTIME_ID() (在 time 模块中)

clone() (email.generator.BytesGenerator 方法)

(email.generator.Generator 方法)

(email.policy.Policy 方法)

(pipes.Template 方法)

(在 turtle 模块中)

cloneNode() (xml.dom.Node 方法)

close() (aifc.aifc 方法), [1]

(asyncio.AbstractEventLoop 方法)

(asyncio.BaseSubprocessTransport 方法)

(asyncio.BaseTransport 方法)

(asyncio.Server 方法)

(asyncio.StreamWriter 方法)

(asyncore.dispatcher 方法)

(chunk.Chunk 方法)

(contextlib.ExitStack 方法)

(coroutine 方法)

(dbm.dumb.dumbdbm 方法)

(dbm.gnu.gdbm 方法)

(dbm.ndbm.ndbm 方法)

(distutils.text_file.TextFile 方法)

(email.parser.BytesFeedParser 方法)

(ftplib.FTP 方法)

(generator 方法)

(html.parser.HTMLParser 方法)

(http.client.HTTPConnection 方法)

(imaplib.IMAP4 方法)

(in module os)

(io.IOBase 方法)

(logging.FileHandler 方法)

(logging.Handler 方法)

(logging.handlers.MemoryHandler 方法)

(logging.handlers.NTEventLogHandler 方法)

(logging.handlers.SocketHandler 方法)

(logging.handlers.SysLogHandler 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(mailbox.MH 方法)

(mmap.mmap 方法)

Close() (msilib.View 方法)

close() (multiprocessing.connection.Connection 方法)

(multiprocessing.connection.Listener 方法)

(multiprocessing.pool.Pool 方法)

(multiprocessing.Queue 方法)

(os.scandir 方法)

(ossaudiodev.oss_audio_device 方法)

(ossaudiodev.oss_mixer_device 方法)

(select.devpoll 方法)

(select.epoll 方法)

(select.kqueue 方法)

(selectors.BaseSelector 方法)

(shelve.Shelf 方法)

(socket.socket 方法)

(sqlite3.Connection 方法)

(sqlite3.Cursor 方法)

(sunau.AU_read 方法)

(sunau.AU_write 方法)

(tarfile.TarFile 方法)

(telnetlib.Telnet 方法)

(urllib.request.BaseHandler 方法)

(wave.Wave_read 方法)

(wave.Wave_write 方法)

Close() (winreg.PyHKEY 方法)

close() (xml.etree.ElementTree.TreeBuilder 方法)

(xml.etree.ElementTree.XMLParser 方法)

(xml.etree.ElementTree.XMLPullParser 方法)

(xml.sax.xmlreader.IncrementalParser 方法)

(zipfile.ZipFile 方法)

(在 fileinput 模块中)

(在 os 模块中)

close_connection (http.server.BaseHTTPRequestHandler 属性)

close_when_done() (asynchat.async_chat 方法)

closed (http.client.HTTPResponse 属性)

(io.IOBase 属性)

(mmap.mmap 属性)

(ossaudiodev.oss_audio_device 属性)

(select.devpoll 属性)

(select.epoll 属性)

(select.kqueue 属性)

CloseKey() (在 winreg 模块中)

closelog() (在 syslog 模块中)

closerange() (在 os 模块中)

closing() (在 contextlib 模块中)

clrtobot() (curses.window 方法)

clrtoeol() (curses.window 方法)

cmath (模块)

cmd

模块

Cmd (cmd 中的类)

cmd (subprocess.CalledProcessError 属性)

(subprocess.TimeoutExpired 属性)

(模块)

cmdloop() (cmd.Cmd 方法)

cmdqueue (cmd.Cmd 属性)

cmp() (在 filecmp 模块中)

cmp_op() (在 dis 模块中)

cmp_to_key() (在 functools 模块中)

cmpfiles() (在 filecmp 模块中)

CMSG_LEN() (在 socket 模块中)

CMSG_SPACE() (在 socket 模块中)

co_argcount (code object attribute)

CO_ASYNC_GENERATOR() (在 inspect 模块中)

co_cellvars (code object attribute)

co_code (code object attribute)

co_consts (code object attribute)

CO_COROUTINE() (在 inspect 模块中)

co_filename (code object attribute)

co_firstlineno (code object attribute)

co_flags (code object attribute)

co_freevars (code object attribute)

CO_FUTURE_DIVISION (C 变量)

CO_GENERATOR() (在 inspect 模块中)

CO_ITERABLE_COROUTINE() (在 inspect 模块中)

co_lnotab (code object attribute)

co_name (code object attribute)

co_names (code object attribute)

CO_NESTED() (在 inspect 模块中)

CO_NEWLOCALS() (在 inspect 模块中)

co_nlocals (code object attribute)

CO_NOFREE() (在 inspect 模块中)

CO_OPTIMIZED() (在 inspect 模块中)

co_stacksize (code object attribute)

CO_VARARGS() (在 inspect 模块中)

CO_VARKEYWORDS() (在 inspect 模块中)

co_varnames (code object attribute)

code

block

code (SystemExit 属性)

(urllib.error.HTTPError 属性)

(xml.etree.ElementTree.ParseError 属性)

(xml.parsers.expat.ExpatError 属性)

(模块)

code object, [1], [2], [3]

code_info() (在 dis 模块中)

CodecInfo (codecs 中的类)

Codecs

decode

encode

codecs (模块)

coded_value (http.cookies.Morsel 属性)

codeop (模块)

codepoint2name() (在 html.entities 模块中)

codes() (在 xml.parsers.expat.errors 模块中)

CODESET() (在 locale 模块中)

CodeType() (在 types 模块中)

coding

style

coercion -- 强制类型转换

col_offset (ast.AST 属性)

collapse_addresses() (在 ipaddress 模块中)

collapse_rfc2231_value() (在 email.utils 模块中)

collect() (在 gc 模块中)

collect_incoming_data() (asynchat.async_chat 方法)

Collection (collections.abc 中的类)

(typing 中的类)

collections (模块)

collections.abc (模块)

colno (json.JSONDecodeError 属性)

(re.error 属性)

COLON() (在 token 模块中)

color() (在 turtle 模块中)

color_content() (在 curses 模块中)

color_pair() (在 curses 模块中)

colormode() (在 turtle 模块中)

colorsys (模块)

COLS, [1]

column() (tkinter.ttk.Treeview 方法)

COLUMNS, [1]

columns (os.terminal_size 属性)

combinations() (在 itertools 模块中)

combinations_with_replacement() (在 itertools 模块中)

combine() (datetime.datetime 类方法)

combining() (在 unicodedata 模块中)

ComboBox (tkinter.tix 中的类)

Combobox (tkinter.ttk 中的类)

comma

trailing

tuple display

COMMA() (在 token 模块中)

Command (distutils.cmd 中的类)

(distutils.core 中的类)

command (http.server.BaseHTTPRequestHandler 属性)

command line

command line option

--help

--version

-?

-b

-B

-c <command>

-d

-E

-h

-i

-I

-J

-m <module-name>

-O

-OO

-q

-R

-s

-S

-u

-V

-v

-W arg

-x

-X

CommandCompiler (codeop 中的类)

commands (pdb command)

comment

(http.cookiejar.Cookie 属性)

(zipfile.ZipFile 属性)

(zipfile.ZipInfo 属性)

COMMENT() (在 tokenize 模块中)

Comment() (在 xml.etree.ElementTree 模块中)

comment_url (http.cookiejar.Cookie 属性)

commenters (shlex.shlex 属性)

CommentHandler() (xml.parsers.expat.xmlparser 方法)

commit() (msilib.CAB 方法)

Commit() (msilib.Database 方法)

commit() (sqlite3.Connection 方法)

common (filecmp.dircmp 属性)

Common Gateway Interface

common_dirs (filecmp.dircmp 属性)

common_files (filecmp.dircmp 属性)

common_funny (filecmp.dircmp 属性)

common_types() (在 mimetypes 模块中)

commonpath() (在 os.path 模块中)

commonprefix() (在 os.path 模块中)

communicate() (asyncio.asyncio.subprocess.Process 方法)

(subprocess.Popen 方法)

compare() (decimal.Context 方法)

(decimal.Decimal 方法)

(difflib.Differ 方法)

compare_digest() (在 hmac 模块中)

(在 secrets 模块中)

compare_networks() (ipaddress.IPv4Network 方法)

(ipaddress.IPv6Network 方法)

COMPARE_OP (opcode)

compare_signal() (decimal.Context 方法)

(decimal.Decimal 方法)

compare_to() (tracemalloc.Snapshot 方法)

compare_total() (decimal.Context 方法)

(decimal.Decimal 方法)

compare_total_mag() (decimal.Context 方法)

(decimal.Decimal 方法)

comparing

objects

comparison

operator

COMPARISON_FLAGS() (在 doctest 模块中)

comparisons

chaining, [1]

Compat32 (email.policy 中的类)

compat32() (在 email.policy 模块中)

compile

內置函数, [1], [2], [3], [4]

Compile (codeop 中的类)

compile() (distutils.ccompiler.CCompiler 方法)

(parser.ST 方法)

(內置函数)

(在 py_compile 模块中)

(在 re 模块中)

compile_command() (在 code 模块中)

(在 codeop 模块中)

compile_dir() (在 compileall 模块中)

compile_file() (在 compileall 模块中)

compile_path() (在 compileall 模块中)

compileall (模块)

compileall command line option

-b

-d destdir

-f

-i list

-j N

-l

-q

-r

-x regex

directory ...

file ...

compilest() (在 parser 模块中)

complete() (rlcompleter.Completer 方法)

complete_statement() (在 sqlite3 模块中)

completedefault() (cmd.Cmd 方法)

CompletedProcess (subprocess 中的类)

complex

number

內置函数, [1]

对象

Complex (numbers 中的类)

complex (內置类)

complex literal

complex number

literals

对象, [1]

complex number -- 复数

compound

statement

comprehensions

list

compress() (bz2.BZ2Compressor 方法)

(lzma.LZMACompressor 方法)

(zlib.Compress 方法)

(在 bz2 模块中)

(在 gzip 模块中)

(在 itertools 模块中)

(在 lzma 模块中)

(在 zlib 模块中)

compress_size (zipfile.ZipInfo 属性)

compress_type (zipfile.ZipInfo 属性)

compressed (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

compression() (ssl.SSLSocket 方法)

CompressionError

compressobj() (在 zlib 模块中)

COMSPEC, [1]

concat() (在 operator 模块中)

concatenation

operation

concurrent.futures (模块)

Condition (asyncio 中的类)

(multiprocessing 中的类)

condition (pdb command)

Condition (threading 中的类)

condition() (msilib.Control 方法)

Condition() (multiprocessing.managers.SyncManager 方法)

Conditional

expression

conditional

expression

ConfigParser (configparser 中的类)

configparser (模块)

configuration

file

file, debugger

file, path

configuration information

configure() (tkinter.ttk.Style 方法)

configure_mock() (unittest.mock.Mock 方法)

confstr() (在 os 模块中)

confstr_names() (在 os 模块中)

conjugate() (complex number method)

(decimal.Decimal 方法)

(numbers.Complex 方法)

conn (smtpd.SMTPChannel 属性)

connect() (asyncore.dispatcher 方法)

(ftplib.FTP 方法)

(http.client.HTTPConnection 方法)

(multiprocessing.managers.BaseManager 方法)

(smtplib.SMTP 方法)

(socket.socket 方法)

(在 sqlite3 模块中)

connect_accepted_socket() (asyncio.BaseEventLoop 方法)

connect_ex() (socket.socket 方法)

connect_read_pipe() (asyncio.AbstractEventLoop 方法)

connect_write_pipe() (asyncio.AbstractEventLoop 方法)

Connection (multiprocessing.connection 中的类)

(sqlite3 中的类)

connection (sqlite3.Cursor 属性)

connection_lost() (asyncio.BaseProtocol 方法)

connection_made() (asyncio.BaseProtocol 方法)

ConnectionAbortedError

ConnectionError

ConnectionRefusedError

ConnectionResetError

ConnectRegistry() (在 winreg 模块中)

const (optparse.Option 属性)

constant

constructor

class

constructor() (在 copyreg 模块中)

consumed (asyncio.LimitOverrunError 属性)

container, [1]

iteration over

Container (collections.abc 中的类)

(typing 中的类)

contains() (在 operator 模块中)

content type

MIME

content_manager (email.policy.EmailPolicy 属性)

content_type (email.headerregistry.ContentTypeHeader 属性)

ContentDispositionHeader (email.headerregistry 中的类)

ContentHandler (xml.sax.handler 中的类)

ContentManager (email.contentmanager 中的类)

contents (ctypes._Pointer 属性)

ContentTooShortError

ContentTransferEncoding (email.headerregistry 中的类)

ContentTypeHeader (email.headerregistry 中的类)

Context (decimal 中的类)

context (ssl.SSLSocket 属性)

context management protocol

context manager, [1]

context manager -- 上下文管理器

context_diff() (在 difflib 模块中)

ContextDecorator (contextlib 中的类)

contextlib (模块)

ContextManager (typing 中的类)

contextmanager() (在 contextlib 模块中)

contiguous

(memoryview 属性)

contiguous -- 连续

continue

语句, [1], [2], [3], [4]

continue (pdb command)

CONTINUE_LOOP (opcode)

Control (msilib 中的类)

(tkinter.tix 中的类)

control() (msilib.Dialog 方法)

(select.kqueue 方法)

controlnames() (在 curses.ascii 模块中)

controls() (ossaudiodev.oss_mixer_device 方法)

conversion

arithmetic

string, [1]

ConversionError

conversions

numeric

convert_arg_line_to_args() (argparse.ArgumentParser 方法)

convert_field() (string.Formatter 方法)

convert_path() (在 distutils.util 模块中)

Cookie (http.cookiejar 中的类)

CookieError

CookieJar (http.cookiejar 中的类)

cookiejar (urllib.request.HTTPCookieProcessor 属性)

CookiePolicy (http.cookiejar 中的类)

Coordinated Universal Time

Copy

copy

protocol

模块

copy (模块)

copy() (collections.deque 方法)

(decimal.Context 方法)

(dict 方法)

(frozenset 方法)

(hashlib.hash 方法)

(hmac.HMAC 方法)

(http.cookies.Morsel 方法)

(imaplib.IMAP4 方法)

(pipes.Template 方法)

(sequence method)

(types.MappingProxyType 方法)

(zlib.Compress 方法)

(zlib.Decompress 方法)

(在 copy 模块中)

(在 multiprocessing.sharedctypes 模块中)

(在 shutil 模块中)

copy2() (在 shutil 模块中)

copy_abs() (decimal.Context 方法)

(decimal.Decimal 方法)

copy_decimal() (decimal.Context 方法)

copy_file() (在 distutils.file_util 模块中)

copy_location() (在 ast 模块中)

copy_negate() (decimal.Context 方法)

(decimal.Decimal 方法)

copy_sign() (decimal.Context 方法)

(decimal.Decimal 方法)

copy_tree() (在 distutils.dir_util 模块中)

copyfile() (在 shutil 模块中)

copyfileobj() (在 shutil 模块中)

copying files

copymode() (在 shutil 模块中)

copyreg (模块)

copyright (in module sys)

(內置变量)

copyright() (在 sys 模块中)

copysign() (在 math 模块中)

copystat() (在 shutil 模块中)

copytree() (在 shutil 模块中)

coroutine, [1]

function

Coroutine (collections.abc 中的类)

(typing 中的类)

coroutine -- 协程

coroutine function -- 协程函数

coroutine() (在 asyncio 模块中)

(在 types 模块中)

CoroutineType() (在 types 模块中)

cos() (在 cmath 模块中)

(在 math 模块中)

cosh() (在 cmath 模块中)

(在 math 模块中)

count (tracemalloc.Statistic 属性)

(tracemalloc.StatisticDiff 属性)

count() (array.array 方法)

(bytearray 方法)

(bytes 方法)

(collections.deque 方法)

(sequence method)

(str 方法)

(在 itertools 模块中)

count_diff (tracemalloc.StatisticDiff 属性)

Counter (collections 中的类)

(typing 中的类)

countOf() (在 operator 模块中)

countTestCases() (unittest.TestCase 方法)

(unittest.TestSuite 方法)

CoverageResults (trace 中的类)

CPP

CPPFLAGS

cProfile (模块)

CPU time

cpu_count() (在 multiprocessing 模块中)

(在 os 模块中)

CPython

crawl_delay() (urllib.robotparser.RobotFileParser 方法)

CRC (zipfile.ZipInfo 属性)

crc32() (在 binascii 模块中)

(在 zlib 模块中)

crc_hqx() (在 binascii 模块中)

create() (imaplib.IMAP4 方法)

(venv.EnvBuilder 方法)

(在 venv 模块中)

create_aggregate() (sqlite3.Connection 方法)

create_archive() (在 zipapp 模块中)

create_autospec() (在 unittest.mock 模块中)

create_collation() (sqlite3.Connection 方法)

create_configuration() (venv.EnvBuilder 方法)

create_connection() (asyncio.AbstractEventLoop 方法)

(在 socket 模块中)

create_datagram_endpoint() (asyncio.AbstractEventLoop 方法)

create_decimal() (decimal.Context 方法)

create_decimal_from_float() (decimal.Context 方法)

create_default_context() (在 ssl 模块中)

create_function() (sqlite3.Connection 方法)

create_future() (asyncio.AbstractEventLoop 方法)

create_module (C 函数)

create_module() (importlib.abc.Loader 方法)

(importlib.machinery.ExtensionFileLoader 方法)

CREATE_NEW_CONSOLE() (在 subprocess 模块中)

CREATE_NEW_PROCESS_GROUP() (在 subprocess 模块中)

create_server() (asyncio.AbstractEventLoop 方法)

create_shortcut() (內置函数)

create_socket() (asyncore.dispatcher 方法)

create_static_lib() (distutils.ccompiler.CCompiler 方法)

create_stats() (profile.Profile 方法)

create_string_buffer() (在 ctypes 模块中)

create_subprocess_exec() (在 asyncio 模块中)

create_subprocess_shell() (在 asyncio 模块中)

create_system (zipfile.ZipInfo 属性)

create_task() (asyncio.AbstractEventLoop 方法)

create_tree() (在 distutils.dir_util 模块中)

create_unicode_buffer() (在 ctypes 模块中)

create_unix_connection() (asyncio.AbstractEventLoop 方法)

create_unix_server() (asyncio.AbstractEventLoop 方法)

create_version (zipfile.ZipInfo 属性)

createAttribute() (xml.dom.Document 方法)

createAttributeNS() (xml.dom.Document 方法)

createComment() (xml.dom.Document 方法)

createDocument() (xml.dom.DOMImplementation 方法)

createDocumentType() (xml.dom.DOMImplementation 方法)

createElement() (xml.dom.Document 方法)

createElementNS() (xml.dom.Document 方法)

createfilehandler() (tkinter.Widget.tk 方法)

CreateKey() (在 winreg 模块中)

CreateKeyEx() (在 winreg 模块中)

createLock() (logging.Handler 方法)

(logging.NullHandler 方法)

createProcessingInstruction() (xml.dom.Document 方法)

CreateRecord() (在 msilib 模块中)

createSocket() (logging.handlers.SocketHandler 方法)

createTextNode() (xml.dom.Document 方法)

credits (內置变量)

critical() (logging.Logger 方法)

(在 logging 模块中)

CRNCYSTR() (在 locale 模块中)

cross() (在 audioop 模块中)

crypt

模块

crypt (模块)

crypt() (在 crypt 模块中)

crypt(3), [1], [2]

cryptography

csv

(模块)

cte (email.headerregistry.ContentTransferEncoding 属性)

cte_type (email.policy.Policy 属性)

ctermid() (在 os 模块中)

ctime() (datetime.date 方法)

(datetime.datetime 方法)

(在 time 模块中)

ctrl() (在 curses.ascii 模块中)

CTRL_BREAK_EVENT() (在 signal 模块中)

CTRL_C_EVENT() (在 signal 模块中)

ctypes (模块)

curdir() (在 os 模块中)

currency() (在 locale 模块中)

current() (tkinter.ttk.Combobox 方法)

current_process() (在 multiprocessing 模块中)

current_task() (asyncio.Task 类方法)

current_thread() (在 threading 模块中)

CurrentByteIndex (xml.parsers.expat.xmlparser 属性)

CurrentColumnNumber (xml.parsers.expat.xmlparser 属性)

currentframe() (在 inspect 模块中)

CurrentLineNumber (xml.parsers.expat.xmlparser 属性)

curs_set() (在 curses 模块中)

curses (模块)

curses.ascii (模块)

curses.panel (模块)

curses.textpad (模块)

Cursor (sqlite3 中的类)

cursor() (sqlite3.Connection 方法)

cursyncup() (curses.window 方法)

customize_compiler() (在 distutils.sysconfig 模块中)

Cut

cwd() (ftplib.FTP 方法)

(pathlib.Path 类方法)

cycle() (在 itertools 模块中)

Cyclic Redundancy Check

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – D

D_FMT() (在 locale 模块中)

D_T_FMT() (在 locale 模块中)

daemon (multiprocessing.Process 属性)

(threading.Thread 属性)

dangling

else

data

packing binary

tabular

type

type, immutable

data (collections.UserDict 属性)

(collections.UserList 属性)

Data (plistlib 中的类)

data (select.kevent 属性)

(selectors.SelectorKey 属性)

(urllib.request.Request 属性)

(xml.dom.Comment 属性)

(xml.dom.ProcessingInstruction 属性)

(xml.dom.Text 属性)

(xmlrpc.client.Binary 属性)

data() (xml.etree.ElementTree.TreeBuilder 方法)

data_open() (urllib.request.DataHandler 方法)

data_received() (asyncio.Protocol 方法)

database

Unicode

DatabaseError

databases

datagram_received() (asyncio.DatagramProtocol 方法)

DatagramHandler (logging.handlers 中的类)

DatagramProtocol (asyncio 中的类)

DatagramRequestHandler (socketserver 中的类)

DataHandler (urllib.request 中的类)

date (datetime 中的类)

date() (datetime.datetime 方法)

(nntplib.NNTP 方法)

date_time (zipfile.ZipInfo 属性)

date_time_string() (http.server.BaseHTTPRequestHandler 方法)

DateHeader (email.headerregistry 中的类)

datetime (datetime 中的类)

(email.headerregistry.DateHeader 属性)

DateTime (xmlrpc.client 中的类)

datetime (模块)

datum

day (datetime.date 属性)

(datetime.datetime 属性)

day_abbr() (在 calendar 模块中)

day_name() (在 calendar 模块中)

Daylight Saving Time

daylight() (在 time 模块中)

DbfilenameShelf (shelve 中的类)

dbm (模块)

dbm.dumb (模块)

dbm.gnu

模块, [1]

dbm.gnu (模块)

dbm.ndbm

模块, [1]

dbm.ndbm (模块)

dcgettext() (在 locale 模块中)

deallocation, object

debug (imaplib.IMAP4 属性)

(shlex.shlex 属性)

(zipfile.ZipFile 属性)

debug() (logging.Logger 方法)

(pipes.Template 方法)

(unittest.TestCase 方法)

(unittest.TestSuite 方法)

(在 doctest 模块中)

(在 logging 模块中)

DEBUG() (在 re 模块中)

DEBUG_BYTECODE_SUFFIXES() (在 importlib.machinery 模块中)

DEBUG_COLLECTABLE() (在 gc 模块中)

DEBUG_LEAK() (在 gc 模块中)

debug_print() (distutils.ccompiler.CCompiler 方法)

DEBUG_SAVEALL() (在 gc 模块中)

debug_src() (在 doctest 模块中)

DEBUG_STATS() (在 gc 模块中)

DEBUG_UNCOLLECTABLE() (在 gc 模块中)

debugger, [1], [2]

configuration file

debugging

assertions

CGI

DebuggingServer (smtpd 中的类)

debuglevel (http.client.HTTPResponse 属性)

DebugRunner (doctest 中的类)

Decimal (decimal 中的类)

decimal (模块)

decimal literal

decimal() (在 unicodedata 模块中)

DecimalException (decimal 中的类)

decode

Codecs

decode (codecs.CodecInfo 属性)

decode() (bytearray 方法)

(bytes 方法)

(codecs.Codec 方法)

(codecs.IncrementalDecoder 方法)

(json.JSONDecoder 方法)

(xmlrpc.client.Binary 方法)

(xmlrpc.client.DateTime 方法)

(在 base64 模块中)

(在 codecs 模块中)

(在 quopri 模块中)

(在 uu 模块中)

decode_header() (在 email.header 模块中)

(在 nntplib 模块中)

decode_params() (在 email.utils 模块中)

decode_rfc2231() (在 email.utils 模块中)

decode_source() (在 importlib.util 模块中)

decodebytes() (在 base64 模块中)

DecodedGenerator (email.generator 中的类)

decodestring() (在 base64 模块中)

(在 quopri 模块中)

decomposition() (在 unicodedata 模块中)

decompress() (bz2.BZ2Decompressor 方法)

(lzma.LZMADecompressor 方法)

(zlib.Decompress 方法)

(在 bz2 模块中)

(在 gzip 模块中)

(在 lzma 模块中)

(在 zlib 模块中)

decompressobj() (在 zlib 模块中)

decorator -- 装饰器

DEDENT token, [1]

dedent() (在 textwrap 模块中)

DEDENT() (在 token 模块中)

deepcopy() (在 copy 模块中)

def

语句

def_prog_mode() (在 curses 模块中)

def_shell_mode() (在 curses 模块中)

default

parameter value

default (inspect.Parameter 属性)

(optparse.Option 属性)

default() (cmd.Cmd 方法)

(json.JSONEncoder 方法)

(在 email.policy 模块中)

DEFAULT() (在 unittest.mock 模块中)

DEFAULT_BUFFER_SIZE() (在 io 模块中)

default_bufsize() (在 xml.dom.pulldom 模块中)

default_exception_handler() (asyncio.AbstractEventLoop 方法)

default_factory (collections.defaultdict 属性)

DEFAULT_FORMAT() (在 tarfile 模块中)

DEFAULT_IGNORES() (在 filecmp 模块中)

default_open() (urllib.request.BaseHandler 方法)

DEFAULT_PROTOCOL() (在 pickle 模块中)

default_timer() (在 timeit 模块中)

DefaultContext (decimal 中的类)

DefaultCookiePolicy (http.cookiejar 中的类)

defaultdict (collections 中的类)

DefaultDict (typing 中的类)

DefaultHandler() (xml.parsers.expat.xmlparser 方法)

DefaultHandlerExpand() (xml.parsers.expat.xmlparser 方法)

defaults() (configparser.ConfigParser 方法)

DefaultSelector (selectors 中的类)

defaultTestLoader() (在 unittest 模块中)

defaultTestResult() (unittest.TestCase 方法)

defects (email.headerregistry.BaseHeader 属性)

(email.message.EmailMessage 属性)

(email.message.Message 属性)

define_macro() (distutils.ccompiler.CCompiler 方法)

definition

class, [1]

function, [1]

defpath() (在 os 模块中)

DefragResult (urllib.parse 中的类)

DefragResultBytes (urllib.parse 中的类)

degrees() (在 math 模块中)

(在 turtle 模块中)

del

语句, [1], [2], [3]

del_param() (email.message.EmailMessage 方法)

(email.message.Message 方法)

delattr() (內置函数)

delay() (在 turtle 模块中)

delay_output() (在 curses 模块中)

delayload (http.cookiejar.FileCookieJar 属性)

delch() (curses.window 方法)

dele() (poplib.POP3 方法)

delete() (ftplib.FTP 方法)

(imaplib.IMAP4 方法)

(tkinter.ttk.Treeview 方法)

DELETE_ATTR (opcode)

DELETE_DEREF (opcode)

DELETE_FAST (opcode)

DELETE_GLOBAL (opcode)

DELETE_NAME (opcode)

DELETE_SUBSCR (opcode)

deleteacl() (imaplib.IMAP4 方法)

deletefilehandler() (tkinter.Widget.tk 方法)

DeleteKey() (在 winreg 模块中)

DeleteKeyEx() (在 winreg 模块中)

deleteln() (curses.window 方法)

deleteMe() (bdb.Breakpoint 方法)

DeleteValue() (在 winreg 模块中)

deletion

attribute

target

target list

delimiter (csv.Dialect 属性)

delimiters

delitem() (在 operator 模块中)

deliver_challenge() (在 multiprocessing.connection 模块中)

delocalize() (在 locale 模块中)

demo_app() (在 wsgiref.simple_server 模块中)

denominator (fractions.Fraction 属性)

(numbers.Rational 属性)

DeprecationWarning

deque (collections 中的类)

Deque (typing 中的类)

dequeue() (logging.handlers.QueueListener 方法)

DER_cert_to_PEM_cert() (在 ssl 模块中)

derwin() (curses.window 方法)

DES

cipher

description (sqlite3.Cursor 属性)

description() (nntplib.NNTP 方法)

descriptions() (nntplib.NNTP 方法)

descriptor -- 描述器

dest (optparse.Option 属性)

destructor, [1]

detach() (io.BufferedIOBase 方法)

(io.TextIOBase 方法)

(socket.socket 方法)

(tkinter.ttk.Treeview 方法)

(weakref.finalize 方法)

Detach() (winreg.PyHKEY 方法)

detect_api_mismatch() (在 test.support 模块中)

detect_encoding() (在 tokenize 模块中)

detect_language() (distutils.ccompiler.CCompiler 方法)

deterministic profiling

device_encoding() (在 os 模块中)

devnull() (在 os 模块中)

DEVNULL() (在 subprocess 模块中)

devpoll() (在 select 模块中)

DevpollSelector (selectors 中的类)

dgettext() (在 gettext 模块中)

(在 locale 模块中)

Dialect (csv 中的类)

dialect (csv.csvreader 属性)

(csv.csvwriter 属性)

Dialog (msilib 中的类)

dict (2to3 fixer)

Dict (typing 中的类)

dict (內置类)

dict() (multiprocessing.managers.SyncManager 方法)

dictConfig() (在 logging.config 模块中)

dictionary

display

type, operations on

对象, [1], [2], [3], [4], [5], [6], [7]

dictionary -- 字典

dictionary view -- 字典视图

DictReader (csv 中的类)

DictWriter (csv 中的类)

diff_bytes() (在 difflib 模块中)

diff_files (filecmp.dircmp 属性)

Differ (difflib 中的类), [1]

difference() (frozenset 方法)

difference_update() (frozenset 方法)

difflib (模块)

digest() (hashlib.hash 方法)

(hashlib.shake 方法)

(hmac.HMAC 方法)

digest_size (hmac.HMAC 属性)

digit() (在 unicodedata 模块中)

digits() (在 string 模块中)

dir() (ftplib.FTP 方法)

(內置函数)

dircmp (filecmp 中的类)

directory

changing

creating

deleting, [1]

site-packages

traversal, [1]

walking, [1]

Directory (msilib 中的类)

directory ...

compileall command line option

directory_created() (內置函数)

DirEntry (os 中的类)

DirList (tkinter.tix 中的类)

dirname() (在 os.path 模块中)

DirSelectBox (tkinter.tix 中的类)

DirSelectDialog (tkinter.tix 中的类)

DirTree (tkinter.tix 中的类)

dis (模块)

dis() (dis.Bytecode 方法)

(在 dis 模块中)

(在 pickletools 模块中)

disable (pdb command)

disable() (bdb.Breakpoint 方法)

(profile.Profile 方法)

(在 faulthandler 模块中)

(在 gc 模块中)

(在 logging 模块中)

disable_interspersed_args() (optparse.OptionParser 方法)

DisableReflectionKey() (在 winreg 模块中)

disassemble() (在 dis 模块中)

discard (http.cookiejar.Cookie 属性)

discard() (frozenset 方法)

(mailbox.Mailbox 方法)

(mailbox.MH 方法)

discard_buffers() (asynchat.async_chat 方法)

disco() (在 dis 模块中)

discover() (unittest.TestLoader 方法)

disk_usage() (在 shutil 模块中)

dispatch_call() (bdb.Bdb 方法)

dispatch_exception() (bdb.Bdb 方法)

dispatch_line() (bdb.Bdb 方法)

dispatch_return() (bdb.Bdb 方法)

dispatch_table (pickle.Pickler 属性)

dispatcher (asyncore 中的类)

dispatcher_with_send (asyncore 中的类)

display

dictionary

list

set

tuple

display (pdb command)

display_name (email.headerregistry.Address 属性)

(email.headerregistry.Group 属性)

displayhook() (在 sys 模块中)

dist() (在 platform 模块中)

distance() (在 turtle 模块中)

distb() (在 dis 模块中)

Distribution (distutils.core 中的类)

distutils (模块)

distutils.archive_util (模块)

distutils.bcppcompiler (模块)

distutils.ccompiler (模块)

distutils.cmd (模块)

distutils.command (模块)

distutils.command.bdist (模块)

distutils.command.bdist_dumb (模块)

distutils.command.bdist_msi (模块)

distutils.command.bdist_packager (模块)

distutils.command.bdist_rpm (模块)

distutils.command.bdist_wininst (模块)

distutils.command.build (模块)

distutils.command.build_clib (模块)

distutils.command.build_ext (模块)

distutils.command.build_py (模块)

distutils.command.build_scripts (模块)

distutils.command.check (模块)

distutils.command.clean (模块)

distutils.command.config (模块)

distutils.command.install (模块)

distutils.command.install_data (模块)

distutils.command.install_headers (模块)

distutils.command.install_lib (模块)

distutils.command.install_scripts (模块)

distutils.command.register (模块)

distutils.command.sdist (模块)

distutils.core (模块)

distutils.cygwinccompiler (模块)

distutils.debug (模块)

distutils.dep_util (模块)

distutils.dir_util (模块)

distutils.dist (模块)

distutils.errors (模块)

distutils.extension (模块)

distutils.fancy_getopt (模块)

distutils.file_util (模块)

distutils.filelist (模块)

distutils.log (模块)

distutils.msvccompiler (模块)

distutils.spawn (模块)

distutils.sysconfig (模块)

distutils.text_file (模块)

distutils.unixccompiler (模块)

distutils.util (模块)

distutils.version (模块)

DISTUTILS_DEBUG

divide() (decimal.Context 方法)

divide_int() (decimal.Context 方法)

division

DivisionByZero (decimal 中的类)

divmod

內置函数, [1], [2]

divmod() (decimal.Context 方法)

(內置函数)

DllCanUnloadNow() (在 ctypes 模块中)

DllGetClassObject() (在 ctypes 模块中)

dllhandle() (在 sys 模块中)

dngettext() (在 gettext 模块中)

do_clear() (bdb.Bdb 方法)

do_command() (curses.textpad.Textbox 方法)

do_GET() (http.server.SimpleHTTPRequestHandler 方法)

do_handshake() (ssl.SSLSocket 方法)

do_HEAD() (http.server.SimpleHTTPRequestHandler 方法)

do_POST() (http.server.CGIHTTPRequestHandler 方法)

doc (json.JSONDecodeError 属性)

doc_header (cmd.Cmd 属性)

DocCGIXMLRPCRequestHandler (xmlrpc.server 中的类)

DocFileSuite() (在 doctest 模块中)

doCleanups() (unittest.TestCase 方法)

docmd() (smtplib.SMTP 方法)

docstring

(doctest.DocTest 属性)

docstring -- 文档字符串

docstrings, [1]

DocTest (doctest 中的类)

doctest (模块)

DocTestFailure

DocTestFinder (doctest 中的类)

DocTestParser (doctest 中的类)

DocTestRunner (doctest 中的类)

DocTestSuite() (在 doctest 模块中)

doctype() (xml.etree.ElementTree.TreeBuilder 方法)

(xml.etree.ElementTree.XMLParser 方法)

documentation

generation

online

documentation string

documentation strings, [1]

documentElement (xml.dom.Document 属性)

DocXMLRPCRequestHandler (xmlrpc.server 中的类)

DocXMLRPCServer (xmlrpc.server 中的类)

domain (email.headerregistry.Address 属性)

(tracemalloc.DomainFilter 属性)

(tracemalloc.Filter 属性)

domain_initial_dot (http.cookiejar.Cookie 属性)

domain_return_ok() (http.cookiejar.CookiePolicy 方法)

domain_specified (http.cookiejar.Cookie 属性)

DomainFilter (tracemalloc 中的类)

DomainLiberal (http.cookiejar.DefaultCookiePolicy 属性)

DomainRFC2965Match (http.cookiejar.DefaultCookiePolicy 属性)

DomainStrict (http.cookiejar.DefaultCookiePolicy 属性)

DomainStrictNoDots (http.cookiejar.DefaultCookiePolicy 属性)

DomainStrictNonDomain (http.cookiejar.DefaultCookiePolicy 属性)

DOMEventStream (xml.dom.pulldom 中的类)

DOMException

DomstringSizeErr

done() (asyncio.Future 方法)

(concurrent.futures.Future 方法)

(xdrlib.Unpacker 方法)

(在 turtle 模块中)

DONT_ACCEPT_BLANKLINE() (在 doctest 模块中)

DONT_ACCEPT_TRUE_FOR_1() (在 doctest 模块中)

dont_write_bytecode() (在 sys 模块中)

doRollover() (logging.handlers.RotatingFileHandler 方法)

(logging.handlers.TimedRotatingFileHandler 方法)

DOT() (在 token 模块中)

dot() (在 turtle 模块中)

DOTALL() (在 re 模块中)

doublequote (csv.Dialect 属性)

DOUBLESLASH() (在 token 模块中)

DOUBLESLASHEQUAL() (在 token 模块中)

DOUBLESTAR() (在 token 模块中)

DOUBLESTAREQUAL() (在 token 模块中)

doupdate() (在 curses 模块中)

down (pdb command)

down() (在 turtle 模块中)

drain() (asyncio.StreamWriter 方法)

drop_whitespace (textwrap.TextWrapper 属性)

dropwhile() (在 itertools 模块中)

dst() (datetime.datetime 方法)

(datetime.time 方法)

(datetime.timezone 方法)

(datetime.tzinfo 方法)

DTDHandler (xml.sax.handler 中的类)

duck-typing -- 鸭子类型

DumbWriter (formatter 中的类)

dummy_threading (模块)

dump() (pickle.Pickler 方法)

(tracemalloc.Snapshot 方法)

(在 ast 模块中)

(在 json 模块中)

(在 marshal 模块中)

(在 pickle 模块中)

(在 plistlib 模块中)

(在 xml.etree.ElementTree 模块中)

dump_stats() (profile.Profile 方法)

(pstats.Stats 方法)

dump_traceback() (在 faulthandler 模块中)

dump_traceback_later() (在 faulthandler 模块中)

dumps() (在 json 模块中)

(在 marshal 模块中)

(在 pickle 模块中)

(在 plistlib 模块中)

(在 xmlrpc.client 模块中)

dup() (socket.socket 方法)

(在 os 模块中)

dup2() (在 os 模块中)

DUP_TOP (opcode)

DUP_TOP_TWO (opcode)

DuplicateOptionError

DuplicateSectionError

dwFlags (subprocess.STARTUPINFO 属性)

DynamicClassAttribute() (在 types 模块中)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – E

e

in numeric literal

e() (在 cmath 模块中)

(在 math 模块中)

E2BIG() (在 errno 模块中)

EACCES() (在 errno 模块中)

EADDRINUSE() (在 errno 模块中)

EADDRNOTAVAIL() (在 errno 模块中)

EADV() (在 errno 模块中)

EAFNOSUPPORT() (在 errno 模块中)

EAFP

EAGAIN() (在 errno 模块中)

EALREADY() (在 errno 模块中)

east_asian_width() (在 unicodedata 模块中)

EBADE() (在 errno 模块中)

EBADF() (在 errno 模块中)

EBADFD() (在 errno 模块中)

EBADMSG() (在 errno 模块中)

EBADR() (在 errno 模块中)

EBADRQC() (在 errno 模块中)

EBADSLT() (在 errno 模块中)

EBFONT() (在 errno 模块中)

EBUSY() (在 errno 模块中)

ECHILD() (在 errno 模块中)

echo() (在 curses 模块中)

echochar() (curses.window 方法)

ECHRNG() (在 errno 模块中)

ECOMM() (在 errno 模块中)

ECONNABORTED() (在 errno 模块中)

ECONNREFUSED() (在 errno 模块中)

ECONNRESET() (在 errno 模块中)

EDEADLK() (在 errno 模块中)

EDEADLOCK() (在 errno 模块中)

EDESTADDRREQ() (在 errno 模块中)

edit() (curses.textpad.Textbox 方法)

EDOM() (在 errno 模块中)

EDOTDOT() (在 errno 模块中)

EDQUOT() (在 errno 模块中)

EEXIST() (在 errno 模块中)

EFAULT() (在 errno 模块中)

EFBIG() (在 errno 模块中)

effective() (在 bdb 模块中)

ehlo() (smtplib.SMTP 方法)

ehlo_or_helo_if_needed() (smtplib.SMTP 方法)

EHOSTDOWN() (在 errno 模块中)

EHOSTUNREACH() (在 errno 模块中)

EIDRM() (在 errno 模块中)

EILSEQ() (在 errno 模块中)

EINPROGRESS() (在 errno 模块中)

EINTR() (在 errno 模块中)

EINVAL() (在 errno 模块中)

EIO() (在 errno 模块中)

EISCONN() (在 errno 模块中)

EISDIR() (在 errno 模块中)

EISNAM() (在 errno 模块中)

EL2HLT() (在 errno 模块中)

EL2NSYNC() (在 errno 模块中)

EL3HLT() (在 errno 模块中)

EL3RST() (在 errno 模块中)

Element (xml.etree.ElementTree 中的类)

element_create() (tkinter.ttk.Style 方法)

element_names() (tkinter.ttk.Style 方法)

element_options() (tkinter.ttk.Style 方法)

ElementDeclHandler() (xml.parsers.expat.xmlparser 方法)

elements() (collections.Counter 方法)

ElementTree (xml.etree.ElementTree 中的类)

ELIBACC() (在 errno 模块中)

ELIBBAD() (在 errno 模块中)

ELIBEXEC() (在 errno 模块中)

ELIBMAX() (在 errno 模块中)

ELIBSCN() (在 errno 模块中)

elif

关键字

Ellinghouse, Lance

Ellipsis

对象

Ellipsis (內置变量)

ELLIPSIS() (在 doctest 模块中)

(在 token 模块中)

ELNRNG() (在 errno 模块中)

ELOOP() (在 errno 模块中)

else

conditional expression

dangling

关键字, [1], [2], [3], [4], [5], [6], [7]

email (模块)

email.charset (模块)

email.contentmanager (模块)

email.encoders (模块)

email.errors (模块)

email.generator (模块)

email.header (模块)

email.headerregistry (模块)

email.iterators (模块)

email.message (模块)

email.mime (模块)

email.parser (模块)

email.policy (模块)

email.utils (模块)

EmailMessage (email.message 中的类)

EmailPolicy (email.policy 中的类)

EMFILE() (在 errno 模块中)

emit() (logging.FileHandler 方法)

(logging.Handler 方法)

(logging.handlers.BufferingHandler 方法)

(logging.handlers.DatagramHandler 方法)

(logging.handlers.HTTPHandler 方法)

(logging.handlers.NTEventLogHandler 方法)

(logging.handlers.QueueHandler 方法)

(logging.handlers.RotatingFileHandler 方法)

(logging.handlers.SMTPHandler 方法)

(logging.handlers.SocketHandler 方法)

(logging.handlers.SysLogHandler 方法)

(logging.handlers.TimedRotatingFileHandler 方法)

(logging.handlers.WatchedFileHandler 方法)

(logging.NullHandler 方法)

(logging.StreamHandler 方法)

EMLINK() (在 errno 模块中)

Empty

empty

list

tuple, [1]

empty (inspect.Parameter 属性)

(inspect.Signature 属性)

empty() (asyncio.Queue 方法)

(multiprocessing.Queue 方法)

(multiprocessing.SimpleQueue 方法)

(queue.Queue 方法)

(sched.scheduler 方法)

EMPTY_NAMESPACE() (在 xml.dom 模块中)

emptyline() (cmd.Cmd 方法)

EMSGSIZE() (在 errno 模块中)

EMULTIHOP() (在 errno 模块中)

enable (pdb command)

enable() (bdb.Breakpoint 方法)

(imaplib.IMAP4 方法)

(profile.Profile 方法)

(在 cgitb 模块中)

(在 faulthandler 模块中)

(在 gc 模块中)

enable_callback_tracebacks() (在 sqlite3 模块中)

enable_interspersed_args() (optparse.OptionParser 方法)

enable_load_extension() (sqlite3.Connection 方法)

enable_traversal() (tkinter.ttk.Notebook 方法)

ENABLE_USER_SITE() (在 site 模块中)

EnableReflectionKey() (在 winreg 模块中)

ENAMETOOLONG() (在 errno 模块中)

ENAVAIL() (在 errno 模块中)

enclose() (curses.window 方法)

encode

Codecs

encode (codecs.CodecInfo 属性)

encode() (codecs.Codec 方法)

(codecs.IncrementalEncoder 方法)

(email.header.Header 方法)

(json.JSONEncoder 方法)

(str 方法)

(xmlrpc.client.Binary 方法)

(xmlrpc.client.DateTime 方法)

(在 base64 模块中)

(在 codecs 模块中)

(在 quopri 模块中)

(在 uu 模块中)

encode_7or8bit() (在 email.encoders 模块中)

encode_base64() (在 email.encoders 模块中)

encode_noop() (在 email.encoders 模块中)

encode_quopri() (在 email.encoders 模块中)

encode_rfc2231() (在 email.utils 模块中)

encodebytes() (在 base64 模块中)

EncodedFile() (在 codecs 模块中)

encodePriority() (logging.handlers.SysLogHandler 方法)

encodestring() (在 base64 模块中)

(在 quopri 模块中)

encoding

base64

quoted-printable

encoding (curses.window 属性)

(io.TextIOBase 属性)

(UnicodeError 属性)

encoding declarations (source file)

ENCODING() (在 tarfile 模块中)

(在 tokenize 模块中)

encodings.idna (模块)

encodings.mbcs (模块)

encodings.utf_8_sig (模块)

encodings_map (mimetypes.MimeTypes 属性)

encodings_map() (在 mimetypes 模块中)

end (UnicodeError 属性)

end() (re.match 方法)

(xml.etree.ElementTree.TreeBuilder 方法)

end_fill() (在 turtle 模块中)

END_FINALLY (opcode)

end_headers() (http.server.BaseHTTPRequestHandler 方法)

end_paragraph() (formatter.formatter 方法)

end_poly() (在 turtle 模块中)

EndCdataSectionHandler() (xml.parsers.expat.xmlparser 方法)

EndDoctypeDeclHandler() (xml.parsers.expat.xmlparser 方法)

endDocument() (xml.sax.handler.ContentHandler 方法)

endElement() (xml.sax.handler.ContentHandler 方法)

EndElementHandler() (xml.parsers.expat.xmlparser 方法)

endElementNS() (xml.sax.handler.ContentHandler 方法)

endheaders() (http.client.HTTPConnection 方法)

ENDMARKER() (在 token 模块中)

EndNamespaceDeclHandler() (xml.parsers.expat.xmlparser 方法)

endpos (re.match 属性)

endPrefixMapping() (xml.sax.handler.ContentHandler 方法)

endswith() (bytearray 方法)

(bytes 方法)

(str 方法)

endwin() (在 curses 模块中)

ENETDOWN() (在 errno 模块中)

ENETRESET() (在 errno 模块中)

ENETUNREACH() (在 errno 模块中)

ENFILE() (在 errno 模块中)

ENOANO() (在 errno 模块中)

ENOBUFS() (在 errno 模块中)

ENOCSI() (在 errno 模块中)

ENODATA() (在 errno 模块中)

ENODEV() (在 errno 模块中)

ENOENT() (在 errno 模块中)

ENOEXEC() (在 errno 模块中)

ENOLCK() (在 errno 模块中)

ENOLINK() (在 errno 模块中)

ENOMEM() (在 errno 模块中)

ENOMSG() (在 errno 模块中)

ENONET() (在 errno 模块中)

ENOPKG() (在 errno 模块中)

ENOPROTOOPT() (在 errno 模块中)

ENOSPC() (在 errno 模块中)

ENOSR() (在 errno 模块中)

ENOSTR() (在 errno 模块中)

ENOSYS() (在 errno 模块中)

ENOTBLK() (在 errno 模块中)

ENOTCONN() (在 errno 模块中)

ENOTDIR() (在 errno 模块中)

ENOTEMPTY() (在 errno 模块中)

ENOTNAM() (在 errno 模块中)

ENOTSOCK() (在 errno 模块中)

ENOTTY() (在 errno 模块中)

ENOTUNIQ() (在 errno 模块中)

enqueue() (logging.handlers.QueueHandler 方法)

enqueue_sentinel() (logging.handlers.QueueListener 方法)

ensure_directories() (venv.EnvBuilder 方法)

ensure_future() (在 asyncio 模块中)

ensurepip (模块)

enter() (sched.scheduler 方法)

enter_context() (contextlib.ExitStack 方法)

enterabs() (sched.scheduler 方法)

entities (xml.dom.DocumentType 属性)

EntityDeclHandler() (xml.parsers.expat.xmlparser 方法)

entitydefs() (在 html.entities 模块中)

EntityResolver (xml.sax.handler 中的类)

Enum (enum 中的类)

enum (模块)

enum_certificates() (在 ssl 模块中)

enum_crls() (在 ssl 模块中)

enumerate() (內置函数)

(在 threading 模块中)

EnumKey() (在 winreg 模块中)

EnumValue() (在 winreg 模块中)

EnvBuilder (venv 中的类)

environ() (在 os 模块中)

(在 posix 模块中)

environb() (在 os 模块中)

environment

environment variables

deleting

setting

EnvironmentError

Environments

virtual

EnvironmentVarGuard (test.support 中的类)

ENXIO() (在 errno 模块中)

eof (bz2.BZ2Decompressor 属性)

(lzma.LZMADecompressor 属性)

(shlex.shlex 属性)

(ssl.MemoryBIO 属性)

(zlib.Decompress 属性)

eof_received() (asyncio.Protocol 方法)

EOFError

(built-in exception)

EOPNOTSUPP() (在 errno 模块中)

EOVERFLOW() (在 errno 模块中)

EPERM() (在 errno 模块中)

EPFNOSUPPORT() (在 errno 模块中)

epilogue (email.message.EmailMessage 属性)

(email.message.Message 属性)

EPIPE() (在 errno 模块中)

epoch

epoll() (在 select 模块中)

EpollSelector (selectors 中的类)

EPROTO() (在 errno 模块中)

EPROTONOSUPPORT() (在 errno 模块中)

EPROTOTYPE() (在 errno 模块中)

eq() (在 operator 模块中)

EQEQUAL() (在 token 模块中)

EQUAL() (在 token 模块中)

ERA() (在 locale 模块中)

ERA_D_FMT() (在 locale 模块中)

ERA_D_T_FMT() (在 locale 模块中)

ERA_T_FMT() (在 locale 模块中)

ERANGE() (在 errno 模块中)

erase() (curses.window 方法)

erasechar() (在 curses 模块中)

EREMCHG() (在 errno 模块中)

EREMOTE() (在 errno 模块中)

EREMOTEIO() (在 errno 模块中)

ERESTART() (在 errno 模块中)

erf() (在 math 模块中)

erfc() (在 math 模块中)

EROFS() (在 errno 模块中)

ERR() (在 curses 模块中)

errcheck (ctypes._FuncPtr 属性)

errcode (xmlrpc.client.ProtocolError 属性)

errmsg (xmlrpc.client.ProtocolError 属性)

errno

模块

errno (OSError 属性)

(模块)

Error, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]

error, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17]

error handling

error() (argparse.ArgumentParser 方法)

(logging.Logger 方法)

(urllib.request.OpenerDirector 方法)

(xml.sax.handler.ErrorHandler 方法)

(在 logging 模块中)

error_body (wsgiref.handlers.BaseHandler 属性)

error_content_type (http.server.BaseHTTPRequestHandler 属性)

error_headers (wsgiref.handlers.BaseHandler 属性)

error_leader() (shlex.shlex 方法)

error_message_format (http.server.BaseHTTPRequestHandler 属性)

error_output() (wsgiref.handlers.BaseHandler 方法)

error_perm

error_proto, [1]

error_received() (asyncio.DatagramProtocol 方法)

error_reply

error_status (wsgiref.handlers.BaseHandler 属性)

error_temp

ErrorByteIndex (xml.parsers.expat.xmlparser 属性)

ErrorCode (xml.parsers.expat.xmlparser 属性)

errorcode() (在 errno 模块中)

ErrorColumnNumber (xml.parsers.expat.xmlparser 属性)

ErrorHandler (xml.sax.handler 中的类)

ErrorLineNumber (xml.parsers.expat.xmlparser 属性)

Errors

logging

errors

(io.TextIOBase 属性)

(unittest.TestLoader 属性)

(unittest.TestResult 属性)

ErrorString() (在 xml.parsers.expat 模块中)

ERRORTOKEN() (在 token 模块中)

escape (shlex.shlex 属性)

escape sequence

escape() (在 cgi 模块中)

(在 glob 模块中)

(在 html 模块中)

(在 re 模块中)

(在 xml.sax.saxutils 模块中)

escapechar (csv.Dialect 属性)

escapedquotes (shlex.shlex 属性)

ESHUTDOWN() (在 errno 模块中)

ESOCKTNOSUPPORT() (在 errno 模块中)

ESPIPE() (在 errno 模块中)

ESRCH() (在 errno 模块中)

ESRMNT() (在 errno 模块中)

ESTALE() (在 errno 模块中)

ESTRPIPE() (在 errno 模块中)

ETIME() (在 errno 模块中)

ETIMEDOUT() (在 errno 模块中)

Etiny() (decimal.Context 方法)

ETOOMANYREFS() (在 errno 模块中)

Etop() (decimal.Context 方法)

ETXTBSY() (在 errno 模块中)

EUCLEAN() (在 errno 模块中)

EUNATCH() (在 errno 模块中)

EUSERS() (在 errno 模块中)

eval

內置函数, [1], [2], [3], [4], [5]

eval() (內置函数)

evaluation

order

Event (asyncio 中的类)

(multiprocessing 中的类)

(threading 中的类)

event scheduling

event() (msilib.Control 方法)

Event() (multiprocessing.managers.SyncManager 方法)

events (selectors.SelectorKey 属性)

(widgets)

EWOULDBLOCK() (在 errno 模块中)

EX_CANTCREAT() (在 os 模块中)

EX_CONFIG() (在 os 模块中)

EX_DATAERR() (在 os 模块中)

EX_IOERR() (在 os 模块中)

EX_NOHOST() (在 os 模块中)

EX_NOINPUT() (在 os 模块中)

EX_NOPERM() (在 os 模块中)

EX_NOTFOUND() (在 os 模块中)

EX_NOUSER() (在 os 模块中)

EX_OK() (在 os 模块中)

EX_OSERR() (在 os 模块中)

EX_OSFILE() (在 os 模块中)

EX_PROTOCOL() (在 os 模块中)

EX_SOFTWARE() (在 os 模块中)

EX_TEMPFAIL() (在 os 模块中)

EX_UNAVAILABLE() (在 os 模块中)

EX_USAGE() (在 os 模块中)

Example (doctest 中的类)

example (doctest.DocTestFailure 属性)

(doctest.UnexpectedException 属性)

examples (doctest.DocTest 属性)

exc_info (doctest.UnexpectedException 属性)

(in module sys)

exc_info() (in module sys)

(在 sys 模块中)

exc_msg (doctest.Example 属性)

exc_type (traceback.TracebackException 属性)

excel (csv 中的类)

excel_tab (csv 中的类)

except

关键字

语句

except (2to3 fixer)

excepthook() (in module sys)

(在 sys 模块中)

Exception

exception, [1]

chaining

handler

raising

exception handler

exception() (asyncio.Future 方法)

(asyncio.StreamReader 方法)

(concurrent.futures.Future 方法)

(logging.Logger 方法)

(在 logging 模块中)

EXCEPTION() (在 tkinter 模块中)

exceptions

in CGI scripts

exclusive

or

EXDEV() (在 errno 模块中)

exec

內置函数, [1], [2], [3]

exec (2to3 fixer)

exec() (內置函数)

exec_module (C 函数)

exec_module() (importlib.abc.InspectLoader 方法)

(importlib.abc.Loader 方法)

(importlib.abc.SourceLoader 方法)

(importlib.machinery.ExtensionFileLoader 方法)

exec_prefix, [1], [2]

EXEC_PREFIX() (在 distutils.sysconfig 模块中)

exec_prefix() (在 sys 模块中)

execfile (2to3 fixer)

execl() (在 os 模块中)

execle() (在 os 模块中)

execlp() (在 os 模块中)

execlpe() (在 os 模块中)

executable (in module sys)

Executable Zip Files

executable() (在 sys 模块中)

executable_filename() (distutils.ccompiler.CCompiler 方法)

execute() (distutils.ccompiler.CCompiler 方法)

Execute() (msilib.View 方法)

execute() (sqlite3.Connection 方法)

(sqlite3.Cursor 方法)

(在 distutils.util 模块中)

executemany() (sqlite3.Connection 方法)

(sqlite3.Cursor 方法)

executescript() (sqlite3.Connection 方法)

(sqlite3.Cursor 方法)

execution

frame, [1]

restricted

stack

execution model

ExecutionLoader (importlib.abc 中的类)

Executor (concurrent.futures 中的类)

execv() (在 os 模块中)

execve() (在 os 模块中)

execvp() (在 os 模块中)

execvpe() (在 os 模块中)

ExFileSelectBox (tkinter.tix 中的类)

EXFULL() (在 errno 模块中)

exists() (pathlib.Path 方法)

(tkinter.ttk.Treeview 方法)

(在 os.path 模块中)

exit (內置变量)

exit()

(argparse.ArgumentParser 方法)

(在 _thread 模块中)

(在 sys 模块中)

exitcode (multiprocessing.Process 属性)

exitfunc (2to3 fixer)

exitonclick() (在 turtle 模块中)

ExitStack (contextlib 中的类)

exp() (decimal.Context 方法)

(decimal.Decimal 方法)

(在 cmath 模块中)

(在 math 模块中)

expand() (re.match 方法)

expand_tabs (textwrap.TextWrapper 属性)

ExpandEnvironmentStrings() (在 winreg 模块中)

expandNode() (xml.dom.pulldom.DOMEventStream 方法)

expandtabs() (bytearray 方法)

(bytes 方法)

(str 方法)

expanduser() (pathlib.Path 方法)

(在 os.path 模块中)

expandvars() (在 os.path 模块中)

Expat

ExpatError

expect() (telnetlib.Telnet 方法)

expected (asyncio.IncompleteReadError 属性)

expectedFailure() (在 unittest 模块中)

expectedFailures (unittest.TestResult 属性)

expires (http.cookiejar.Cookie 属性)

exploded (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

expm1() (在 math 模块中)

expovariate() (在 random 模块中)

expr() (在 parser 模块中)

expression

Conditional

conditional

generator

lambda, [1]

list, [1], [2]

statement

yield

expression -- 表达式

expunge() (imaplib.IMAP4 方法)

extend() (array.array 方法)

(collections.deque 方法)

(sequence method)

(xml.etree.ElementTree.Element 方法)

extend_path() (在 pkgutil 模块中)

EXTENDED_ARG (opcode)

ExtendedContext (decimal 中的类)

ExtendedInterpolation (configparser 中的类)

extendleft() (collections.deque 方法)

extension

module

Extension (distutils.core 中的类)

extension module -- 扩展模块

EXTENSION_SUFFIXES() (在 importlib.machinery 模块中)

ExtensionFileLoader (importlib.machinery 中的类)

extensions_map (http.server.SimpleHTTPRequestHandler 属性)

External Data Representation, [1]

external_attr (zipfile.ZipInfo 属性)

ExternalClashError

ExternalEntityParserCreate() (xml.parsers.expat.xmlparser 方法)

ExternalEntityRefHandler() (xml.parsers.expat.xmlparser 方法)

extra (zipfile.ZipInfo 属性)

extract() (tarfile.TarFile 方法)

(traceback.StackSummary 类方法)

(zipfile.ZipFile 方法)

extract_cookies() (http.cookiejar.CookieJar 方法)

extract_stack() (在 traceback 模块中)

extract_tb() (在 traceback 模块中)

extract_version (zipfile.ZipInfo 属性)

extractall() (tarfile.TarFile 方法)

(zipfile.ZipFile 方法)

ExtractError

extractfile() (tarfile.TarFile 方法)

extsep() (在 os 模块中)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – F

f"

formatted string literal

f'

formatted string literal

f-string

f-string -- f-字符串

f_back (frame attribute)

f_builtins (frame attribute)

f_code (frame attribute)

f_contiguous (memoryview 属性)

f_globals (frame attribute)

f_lasti (frame attribute)

f_lineno (frame attribute)

f_locals (frame attribute)

F_LOCK() (在 os 模块中)

F_OK() (在 os 模块中)

F_TEST() (在 os 模块中)

F_TLOCK() (在 os 模块中)

f_trace (frame attribute)

F_ULOCK() (在 os 模块中)

fabs() (在 math 模块中)

factorial() (在 math 模块中)

factory() (importlib.util.LazyLoader 类方法)

fail() (unittest.TestCase 方法)

FAIL_FAST() (在 doctest 模块中)

failfast (unittest.TestResult 属性)

failureException (unittest.TestCase 属性)

failures (unittest.TestResult 属性)

FakePath (test.support 中的类)

False, [1], [2]

false

False (Built-in object)

(內置变量)

family (socket.socket 属性)

fancy_getopt() (在 distutils.fancy_getopt 模块中)

FancyGetopt (distutils.fancy_getopt 中的类)

FancyURLopener (urllib.request 中的类)

fast (pickle.Pickler 属性)

fatalError() (xml.sax.handler.ErrorHandler 方法)

Fault (xmlrpc.client 中的类)

faultCode (xmlrpc.client.Fault 属性)

faulthandler (模块)

faultString (xmlrpc.client.Fault 属性)

fchdir() (在 os 模块中)

fchmod() (在 os 模块中)

fchown() (在 os 模块中)

FCICreate() (在 msilib 模块中)

fcntl (模块)

fcntl() (在 fcntl 模块中)

fd (selectors.SelectorKey 属性)

fd() (在 turtle 模块中)

fdatasync() (在 os 模块中)

fdopen() (在 os 模块中)

Feature (msilib 中的类)

feature_external_ges() (在 xml.sax.handler 模块中)

feature_external_pes() (在 xml.sax.handler 模块中)

feature_namespace_prefixes() (在 xml.sax.handler 模块中)

feature_namespaces() (在 xml.sax.handler 模块中)

feature_string_interning() (在 xml.sax.handler 模块中)

feature_validation() (在 xml.sax.handler 模块中)

feed() (email.parser.BytesFeedParser 方法)

(html.parser.HTMLParser 方法)

(xml.etree.ElementTree.XMLParser 方法)

(xml.etree.ElementTree.XMLPullParser 方法)

(xml.sax.xmlreader.IncrementalParser 方法)

feed_data() (asyncio.StreamReader 方法)

feed_eof() (asyncio.StreamReader 方法)

FeedParser (email.parser 中的类)

fetch() (imaplib.IMAP4 方法)

Fetch() (msilib.View 方法)

fetchall() (sqlite3.Cursor 方法)

fetchmany() (sqlite3.Cursor 方法)

fetchone() (sqlite3.Cursor 方法)

fflags (select.kevent 属性)

field_size_limit() (在 csv 模块中)

fieldnames (csv.csvreader 属性)

fields (uuid.UUID 属性)

file

.ini

.pdbrc

byte-code, [1]

configuration

copying

debugger configuration

large files

mime.types

modes

path configuration

plist

temporary

对象, [1]

file (pyclbr.Class 属性)

(pyclbr.Function 属性)

file ...

compileall command line option

file control

UNIX

file name

temporary

file object

io module

open() built-in function

file object -- 文件对象

file-like object -- 文件类对象

FILE_ATTRIBUTE_ARCHIVE() (在 stat 模块中)

FILE_ATTRIBUTE_COMPRESSED() (在 stat 模块中)

FILE_ATTRIBUTE_DEVICE() (在 stat 模块中)

FILE_ATTRIBUTE_DIRECTORY() (在 stat 模块中)

FILE_ATTRIBUTE_ENCRYPTED() (在 stat 模块中)

FILE_ATTRIBUTE_HIDDEN() (在 stat 模块中)

FILE_ATTRIBUTE_INTEGRITY_STREAM() (在 stat 模块中)

FILE_ATTRIBUTE_NO_SCRUB_DATA() (在 stat 模块中)

FILE_ATTRIBUTE_NORMAL() (在 stat 模块中)

FILE_ATTRIBUTE_NOT_CONTENT_INDEXED() (在 stat 模块中)

FILE_ATTRIBUTE_OFFLINE() (在 stat 模块中)

FILE_ATTRIBUTE_READONLY() (在 stat 模块中)

FILE_ATTRIBUTE_REPARSE_POINT() (在 stat 模块中)

FILE_ATTRIBUTE_SPARSE_FILE() (在 stat 模块中)

FILE_ATTRIBUTE_SYSTEM() (在 stat 模块中)

FILE_ATTRIBUTE_TEMPORARY() (在 stat 模块中)

FILE_ATTRIBUTE_VIRTUAL() (在 stat 模块中)

file_created() (內置函数)

file_dispatcher (asyncore 中的类)

file_open() (urllib.request.FileHandler 方法)

file_size (zipfile.ZipInfo 属性)

file_wrapper (asyncore 中的类)

filecmp (模块)

fileConfig() (在 logging.config 模块中)

FileCookieJar (http.cookiejar 中的类)

FileEntry (tkinter.tix 中的类)

FileExistsError

FileFinder (importlib.machinery 中的类)

FileHandler (logging 中的类)

(urllib.request 中的类)

FileInput (fileinput 中的类)

fileinput (模块)

FileIO (io 中的类)

filelineno() (在 fileinput 模块中)

FileLoader (importlib.abc 中的类)

filemode() (在 stat 模块中)

filename (doctest.DocTest 属性)

(http.cookiejar.FileCookieJar 属性)

(OSError 属性)

(traceback.TracebackException 属性)

(tracemalloc.Frame 属性)

(zipfile.ZipFile 属性)

(zipfile.ZipInfo 属性)

filename() (在 fileinput 模块中)

filename2 (OSError 属性)

filename_only() (在 tabnanny 模块中)

filename_pattern (tracemalloc.Filter 属性)

filenames

pathname expansion

wildcard expansion

fileno() (http.client.HTTPResponse 方法)

(io.IOBase 方法)

(multiprocessing.connection.Connection 方法)

(ossaudiodev.oss_audio_device 方法)

(ossaudiodev.oss_mixer_device 方法)

(select.devpoll 方法)

(select.epoll 方法)

(select.kqueue 方法)

(selectors.DevpollSelector 方法)

(selectors.EpollSelector 方法)

(selectors.KqueueSelector 方法)

(socket.socket 方法)

(socketserver.BaseServer 方法)

(telnetlib.Telnet 方法)

(在 fileinput 模块中)

FileNotFoundError

fileobj (selectors.SelectorKey 属性)

FileSelectBox (tkinter.tix 中的类)

FileType (argparse 中的类)

FileWrapper (wsgiref.util 中的类)

fill() (textwrap.TextWrapper 方法)

(在 textwrap 模块中)

fillcolor() (在 turtle 模块中)

filling() (在 turtle 模块中)

filter (2to3 fixer)

Filter (logging 中的类)

filter (select.kevent 属性)

Filter (tracemalloc 中的类)

filter() (logging.Filter 方法)

(logging.Handler 方法)

(logging.Logger 方法)

(內置函数)

(在 curses 模块中)

(在 fnmatch 模块中)

FILTER_DIR() (在 unittest.mock 模块中)

filter_traces() (tracemalloc.Snapshot 方法)

filterfalse() (在 itertools 模块中)

filterwarnings() (在 warnings 模块中)

finalization, of objects

finalize (weakref 中的类)

finalize_options() (distutils.cmd.Command 方法)

finalizer

finally

关键字, [1], [2], [3], [4]

find() (bytearray 方法)

(bytes 方法)

(doctest.DocTestFinder 方法)

(mmap.mmap 方法)

(str 方法)

(xml.etree.ElementTree.Element 方法)

(xml.etree.ElementTree.ElementTree 方法)

(在 gettext 模块中)

find_class() (pickle protocol)

(pickle.Unpickler 方法)

find_library() (在 ctypes.util 模块中)

find_library_file() (distutils.ccompiler.CCompiler 方法)

find_loader() (importlib.abc.PathEntryFinder 方法)

(importlib.machinery.FileFinder 方法)

(在 importlib 模块中)

(在 pkgutil 模块中)

find_longest_match() (difflib.SequenceMatcher 方法)

find_module() (imp.NullImporter 方法)

(importlib.abc.Finder 方法)

(importlib.abc.MetaPathFinder 方法)

(importlib.abc.PathEntryFinder 方法)

(importlib.machinery.PathFinder 类方法)

(zipimport.zipimporter 方法)

(在 imp 模块中)

find_msvcrt() (在 ctypes.util 模块中)

find_spec

finder

find_spec() (importlib.abc.MetaPathFinder 方法)

(importlib.abc.PathEntryFinder 方法)

(importlib.machinery.FileFinder 方法)

(importlib.machinery.PathFinder 类方法)

(在 importlib.util 模块中)

find_unused_port() (在 test.support 模块中)

find_user_password() (urllib.request.HTTPPasswordMgr 方法), [1]

findall() (re.regex 方法)

(xml.etree.ElementTree.Element 方法)

(xml.etree.ElementTree.ElementTree 方法)

(在 re 模块中)

findCaller() (logging.Logger 方法)

finder

find_spec

Finder (importlib.abc 中的类)

finder -- 查找器

findfactor() (在 audioop 模块中)

findfile() (在 test.support 模块中)

findfit() (在 audioop 模块中)

finditer() (re.regex 方法)

(在 re 模块中)

findlabels() (在 dis 模块中)

findlinestarts() (在 dis 模块中)

findmatch() (在 mailcap 模块中)

findmax() (在 audioop 模块中)

findtext() (xml.etree.ElementTree.Element 方法)

(xml.etree.ElementTree.ElementTree 方法)

finish() (socketserver.BaseRequestHandler 方法)

finish_request() (socketserver.BaseServer 方法)

firstChild (xml.dom.Node 属性)

firstkey() (dbm.gnu.gdbm 方法)

firstweekday() (在 calendar 模块中)

fix_missing_locations() (在 ast 模块中)

fix_sentence_endings (textwrap.TextWrapper 属性)

Flag (enum 中的类)

flag_bits (zipfile.ZipInfo 属性)

flags (re.regex 属性)

(select.kevent 属性)

flags() (在 sys 模块中)

flash() (在 curses 模块中)

flatten() (email.generator.BytesGenerator 方法)

(email.generator.Generator 方法)

flattening

objects

float

內置函数, [1], [2]

float (內置类)

float_info() (在 sys 模块中)

float_repr_style() (在 sys 模块中)

floating point

literals

number

对象, [1], [2]

floating point literal

FloatingPointError, [1]

FloatOperation (decimal 中的类)

flock() (在 fcntl 模块中)

floor division -- 向下取整除法

floor() (in module math)

(在 math 模块中)

floordiv() (在 operator 模块中)

flush() (bz2.BZ2Compressor 方法)

(formatter.writer 方法)

(io.BufferedWriter 方法)

(io.IOBase 方法)

(logging.Handler 方法)

(logging.handlers.BufferingHandler 方法)

(logging.handlers.MemoryHandler 方法)

(logging.StreamHandler 方法)

(lzma.LZMACompressor 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(mailbox.MH 方法)

(mmap.mmap 方法)

(zlib.Compress 方法)

(zlib.Decompress 方法)

flush_headers() (http.server.BaseHTTPRequestHandler 方法)

flush_softspace() (formatter.formatter 方法)

flushinp() (在 curses 模块中)

FlushKey() (在 winreg 模块中)

fma() (decimal.Context 方法)

(decimal.Decimal 方法)

fmod() (在 math 模块中)

FMT_BINARY() (在 plistlib 模块中)

FMT_XML() (在 plistlib 模块中)

fnmatch (模块)

fnmatch() (在 fnmatch 模块中)

fnmatchcase() (在 fnmatch 模块中)

focus() (tkinter.ttk.Treeview 方法)

fold (datetime.datetime 属性)

(datetime.time 属性)

fold() (email.headerregistry.BaseHeader 方法)

(email.policy.Compat32 方法)

(email.policy.EmailPolicy 方法)

(email.policy.Policy 方法)

fold_binary() (email.policy.Compat32 方法)

(email.policy.EmailPolicy 方法)

(email.policy.Policy 方法)

for

in comprehensions

语句, [1], [2], [3]

FOR_ITER (opcode)

forget() (tkinter.ttk.Notebook 方法)

(在 test.support 模块中)

fork() (在 os 模块中)

(在 pty 模块中)

ForkingMixIn (socketserver 中的类)

ForkingTCPServer (socketserver 中的类)

ForkingUDPServer (socketserver 中的类)

forkpty() (在 os 模块中)

form

lambda

Form (tkinter.tix 中的类)

format (memoryview 属性)

(struct.Struct 属性)

format() (built-in function)

__str__() (object method)

format() (logging.Formatter 方法)

(logging.Handler 方法)

(pprint.PrettyPrinter 方法)

(str 方法)

(string.Formatter 方法)

(traceback.StackSummary 方法)

(traceback.TracebackException 方法)

(tracemalloc.Traceback 方法)

(內置函数)

(在 locale 模块中)

format_datetime() (在 email.utils 模块中)

format_exc() (在 traceback 模块中)

format_exception() (在 traceback 模块中)

format_exception_only() (traceback.TracebackException 方法)

(在 traceback 模块中)

format_field() (string.Formatter 方法)

format_help() (argparse.ArgumentParser 方法)

format_list() (在 traceback 模块中)

format_map() (str 方法)

format_stack() (在 traceback 模块中)

format_stack_entry() (bdb.Bdb 方法)

format_string() (在 locale 模块中)

format_tb() (在 traceback 模块中)

format_usage() (argparse.ArgumentParser 方法)

FORMAT_VALUE (opcode)

formataddr() (在 email.utils 模块中)

formatargspec() (在 inspect 模块中)

formatargvalues() (在 inspect 模块中)

formatdate() (在 email.utils 模块中)

FormatError

FormatError() (在 ctypes 模块中)

formatException() (logging.Formatter 方法)

formatmonth() (calendar.HTMLCalendar 方法)

(calendar.TextCalendar 方法)

formatStack() (logging.Formatter 方法)

formatted string literal

Formatter (logging 中的类)

(string 中的类)

formatter (模块)

formatTime() (logging.Formatter 方法)

formatting

bytearray (%)

bytes (%)

formatting, string (%)

formatwarning() (在 warnings 模块中)

formatyear() (calendar.HTMLCalendar 方法)

(calendar.TextCalendar 方法)

formatyearpage() (calendar.HTMLCalendar 方法)

Fortran contiguous, [1]

forward() (在 turtle 模块中)

found_terminator() (asynchat.async_chat 方法)

fpathconf() (在 os 模块中)

fpectl (模块)

fqdn (smtpd.SMTPChannel 属性)

Fraction (fractions 中的类)

fractions (模块)

frame

execution, [1]

对象

frame (tkinter.scrolledtext.ScrolledText 属性)

Frame (tracemalloc 中的类)

FrameSummary (traceback 中的类)

FrameType() (在 types 模块中)

free

variable

free()

freeze utility

freeze_support() (在 multiprocessing 模块中)

frexp() (在 math 模块中)

from

import statement, [1]

yield from expression

关键字, [1]

from_address() (ctypes._CData 方法)

from_buffer() (ctypes._CData 方法)

from_buffer_copy() (ctypes._CData 方法)

from_bytes() (int 类方法)

from_callable() (inspect.Signature 类方法)

from_decimal() (fractions.Fraction 方法)

from_exception() (traceback.TracebackException 类方法)

from_file() (zipfile.ZipInfo 类方法)

from_float() (decimal.Decimal 方法)

(fractions.Fraction 方法)

from_iterable() (itertools.chain 类方法)

from_list() (traceback.StackSummary 类方法)

from_param() (ctypes._CData 方法)

from_traceback() (dis.Bytecode 类方法)

frombuf() (tarfile.TarInfo 类方法)

frombytes() (array.array 方法)

fromfd() (select.epoll 方法)

(select.kqueue 方法)

(在 socket 模块中)

fromfile() (array.array 方法)

fromhex() (bytearray 类方法)

(bytes 类方法)

(float 类方法)

fromkeys() (collections.Counter 方法)

(dict 类方法)

fromlist() (array.array 方法)

fromordinal() (datetime.date 类方法)

(datetime.datetime 类方法)

fromshare() (在 socket 模块中)

fromstring() (array.array 方法)

(在 xml.etree.ElementTree 模块中)

fromstringlist() (在 xml.etree.ElementTree 模块中)

fromtarfile() (tarfile.TarInfo 类方法)

fromtimestamp() (datetime.date 类方法)

(datetime.datetime 类方法)

fromunicode() (array.array 方法)

fromutc() (datetime.timezone 方法)

(datetime.tzinfo 方法)

FrozenImporter (importlib.machinery 中的类)

frozenset

对象, [1]

FrozenSet (typing 中的类)

frozenset (內置类)

fsdecode() (在 os 模块中)

fsencode() (在 os 模块中)

fspath() (在 os 模块中)

fstat() (在 os 模块中)

fstatvfs() (在 os 模块中)

fsum() (在 math 模块中)

fsync() (在 os 模块中)

FTP

ftplib (standard module)

protocol, [1]

FTP (ftplib 中的类)

ftp_open() (urllib.request.FTPHandler 方法)

FTP_TLS (ftplib 中的类)

FTPHandler (urllib.request 中的类)

ftplib (模块)

ftruncate() (在 os 模块中)

Full

full() (asyncio.Queue 方法)

(multiprocessing.Queue 方法)

(queue.Queue 方法)

full_url (urllib.request.Request 属性)

fullmatch() (re.regex 方法)

(在 re 模块中)

func (functools.partial 属性)

funcattrs (2to3 fixer)

function

annotations, [1]

anonymous

argument

call, [1], [2]

call, user-defined

definition, [1]

generator, [1]

name, [1]

user-defined

对象, [1], [2], [3], [4], [5]

Function (symtable 中的类)

function -- 函数

function annotation -- 函数标注

function__entry (C 函数)

function__return (C 函数)

FunctionTestCase (unittest 中的类)

FunctionType() (在 types 模块中)

functools (模块)

funny_files (filecmp.dircmp 属性)

future

statement

future (2to3 fixer)

Future (asyncio 中的类)

(concurrent.futures 中的类)

FutureWarning

fwalk() (在 os 模块中)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – G

G.722

gaierror

gamma() (在 math 模块中)

gammavariate() (在 random 模块中)

garbage collection

garbage collection -- 垃圾回收

garbage() (在 gc 模块中)

gather() (curses.textpad.Textbox 方法)

(在 asyncio 模块中)

gauss() (在 random 模块中)

gc (模块)

gc__done (C 函数)

gc__start (C 函数)

gcd() (在 fractions 模块中)

(在 math 模块中)

ge() (在 operator 模块中)

gen_lib_options() (在 distutils.ccompiler 模块中)

gen_preprocess_options() (在 distutils.ccompiler 模块中)

gen_uuid() (在 msilib 模块中)

generate_help() (distutils.fancy_getopt.FancyGetopt 方法)

generator

expression

function, [1], [2]

iterator, [1]

对象, [1], [2]

Generator (collections.abc 中的类)

(email.generator 中的类)

(typing 中的类)

generator -- 生成器

generator expression

generator expression -- 生成器表达式

generator iterator -- 生成器迭代器

GeneratorExit

例外, [1]

GeneratorType() (在 types 模块中)

generic

special attribute

Generic (typing 中的类)

generic function -- 泛型函数

generic_visit() (ast.NodeVisitor 方法)

genops() (在 pickletools 模块中)

get() (asyncio.Queue 方法)

(configparser.ConfigParser 方法)

(dict 方法)

(email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(multiprocessing.pool.AsyncResult 方法)

(multiprocessing.Queue 方法)

(multiprocessing.SimpleQueue 方法)

(ossaudiodev.oss_mixer_device 方法)

(queue.Queue 方法)

(tkinter.ttk.Combobox 方法)

(types.MappingProxyType 方法)

(xml.etree.ElementTree.Element 方法)

(在 webbrowser 模块中)

GET_AITER (opcode)

get_all() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(wsgiref.headers.Headers 方法)

get_all_breaks() (bdb.Bdb 方法)

get_all_start_methods() (在 multiprocessing 模块中)

GET_ANEXT (opcode)

get_app() (wsgiref.simple_server.WSGIServer 方法)

get_archive_formats() (在 shutil 模块中)

get_asyncgen_hooks() (在 sys 模块中)

GET_AWAITABLE (opcode)

get_begidx() (在 readline 模块中)

get_blocking() (在 os 模块中)

get_body() (email.message.EmailMessage 方法)

get_body_encoding() (email.charset.Charset 方法)

get_boundary() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_bpbynumber() (bdb.Bdb 方法)

get_break() (bdb.Bdb 方法)

get_breaks() (bdb.Bdb 方法)

get_buffer() (xdrlib.Packer 方法)

(xdrlib.Unpacker 方法)

get_bytes() (mailbox.Mailbox 方法)

get_ca_certs() (ssl.SSLContext 方法)

get_cache_token() (在 abc 模块中)

get_channel_binding() (ssl.SSLSocket 方法)

get_charset() (email.message.Message 方法)

get_charsets() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_children() (symtable.SymbolTable 方法)

(tkinter.ttk.Treeview 方法)

get_ciphers() (ssl.SSLContext 方法)

get_clock_info() (在 time 模块中)

get_close_matches() (在 difflib 模块中)

get_code() (importlib.abc.InspectLoader 方法)

(importlib.abc.SourceLoader 方法)

(importlib.machinery.ExtensionFileLoader 方法)

(importlib.machinery.SourcelessFileLoader 方法)

(zipimport.zipimporter 方法)

get_completer() (在 readline 模块中)

get_completer_delims() (在 readline 模块中)

get_completion_type() (在 readline 模块中)

get_config_h_filename() (在 distutils.sysconfig 模块中)

(在 sysconfig 模块中)

get_config_var() (在 distutils.sysconfig 模块中)

(在 sysconfig 模块中)

get_config_vars() (在 distutils.sysconfig 模块中)

(在 sysconfig 模块中)

get_content() (email.contentmanager.ContentManager 方法)

(email.message.EmailMessage 方法)

(在 email.contentmanager 模块中)

get_content_charset() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_content_disposition() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_content_maintype() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_content_subtype() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_content_type() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_context() (在 multiprocessing 模块中)

get_coroutine_wrapper() (在 sys 模块中)

get_count() (在 gc 模块中)

get_current_history_length() (在 readline 模块中)

get_data() (importlib.abc.FileLoader 方法)

(importlib.abc.ResourceLoader 方法)

(zipimport.zipimporter 方法)

(在 pkgutil 模块中)

get_date() (mailbox.MaildirMessage 方法)

get_debug() (asyncio.AbstractEventLoop 方法)

(在 gc 模块中)

get_default() (argparse.ArgumentParser 方法)

get_default_compiler() (在 distutils.ccompiler 模块中)

get_default_domain() (在 nis 模块中)

get_default_type() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_default_verify_paths() (在 ssl 模块中)

get_dialect() (在 csv 模块中)

get_docstring() (在 ast 模块中)

get_doctest() (doctest.DocTestParser 方法)

get_endidx() (在 readline 模块中)

get_environ() (wsgiref.simple_server.WSGIRequestHandler 方法)

get_errno() (在 ctypes 模块中)

get_event_loop() (asyncio.AbstractEventLoopPolicy 方法)

(在 asyncio 模块中)

get_event_loop_policy() (在 asyncio 模块中)

get_examples() (doctest.DocTestParser 方法)

get_exception_handler() (asyncio.AbstractEventLoop 方法)

get_exec_path() (在 os 模块中)

get_extra_info() (asyncio.BaseTransport 方法)

(asyncio.StreamWriter 方法)

get_field() (string.Formatter 方法)

get_file() (mailbox.Babyl 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(mailbox.mbox 方法)

(mailbox.MH 方法)

(mailbox.MMDF 方法)

get_file_breaks() (bdb.Bdb 方法)

get_filename() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(importlib.abc.ExecutionLoader 方法)

(importlib.abc.FileLoader 方法)

(importlib.machinery.ExtensionFileLoader 方法)

(zipimport.zipimporter 方法)

get_flags() (mailbox.MaildirMessage 方法)

(mailbox.mboxMessage 方法)

(mailbox.MMDFMessage 方法)

get_folder() (mailbox.Maildir 方法)

(mailbox.MH 方法)

get_frees() (symtable.Function 方法)

get_from() (mailbox.mboxMessage 方法)

(mailbox.MMDFMessage 方法)

get_full_url() (urllib.request.Request 方法)

get_globals() (symtable.Function 方法)

get_grouped_opcodes() (difflib.SequenceMatcher 方法)

get_handle_inheritable() (在 os 模块中)

get_header() (urllib.request.Request 方法)

get_history_item() (在 readline 模块中)

get_history_length() (在 readline 模块中)

get_id() (symtable.SymbolTable 方法)

get_ident() (在 _thread 模块中)

(在 threading 模块中)

get_identifiers() (symtable.SymbolTable 方法)

get_importer() (在 pkgutil 模块中)

get_info() (mailbox.MaildirMessage 方法)

get_inheritable() (socket.socket 方法)

(在 os 模块中)

get_instructions() (在 dis 模块中)

get_interpreter() (在 zipapp 模块中)

GET_ITER (opcode)

get_key() (selectors.BaseSelector 方法)

get_labels() (mailbox.Babyl 方法)

(mailbox.BabylMessage 方法)

get_last_error() (在 ctypes 模块中)

get_line_buffer() (在 readline 模块中)

get_lineno() (symtable.SymbolTable 方法)

get_loader() (在 pkgutil 模块中)

get_locals() (symtable.Function 方法)

get_logger() (在 multiprocessing 模块中)

get_magic() (在 imp 模块中)

get_makefile_filename() (在 distutils.sysconfig 模块中)

(在 sysconfig 模块中)

get_map() (selectors.BaseSelector 方法)

get_matching_blocks() (difflib.SequenceMatcher 方法)

get_message() (mailbox.Mailbox 方法)

get_method() (urllib.request.Request 方法)

get_methods() (symtable.Class 方法)

get_mixed_type_key() (在 ipaddress 模块中)

get_name() (symtable.Symbol 方法)

(symtable.SymbolTable 方法)

get_namespace() (symtable.Symbol 方法)

get_namespaces() (symtable.Symbol 方法)

get_nonstandard_attr() (http.cookiejar.Cookie 方法)

get_nowait() (asyncio.Queue 方法)

(multiprocessing.Queue 方法)

(queue.Queue 方法)

get_object_traceback() (在 tracemalloc 模块中)

get_objects() (在 gc 模块中)

get_opcodes() (difflib.SequenceMatcher 方法)

get_option() (optparse.OptionParser 方法)

get_option_group() (optparse.OptionParser 方法)

get_option_order() (distutils.fancy_getopt.FancyGetopt 方法)

get_osfhandle() (在 msvcrt 模块中)

get_output_charset() (email.charset.Charset 方法)

get_param() (email.message.Message 方法)

get_parameters() (symtable.Function 方法)

get_params() (email.message.Message 方法)

get_path() (在 sysconfig 模块中)

get_path_names() (在 sysconfig 模块中)

get_paths() (在 sysconfig 模块中)

get_payload() (email.message.Message 方法)

get_pid() (asyncio.BaseSubprocessTransport 方法)

get_pipe_transport() (asyncio.BaseSubprocessTransport 方法)

get_platform() (在 distutils.util 模块中)

(在 sysconfig 模块中)

get_poly() (在 turtle 模块中)

get_position() (xdrlib.Unpacker 方法)

get_protocol() (asyncio.BaseTransport 方法)

get_python_inc() (在 distutils.sysconfig 模块中)

get_python_lib() (在 distutils.sysconfig 模块中)

get_python_version() (在 sysconfig 模块中)

get_recsrc() (ossaudiodev.oss_mixer_device 方法)

get_referents() (在 gc 模块中)

get_referrers() (在 gc 模块中)

get_request() (socketserver.BaseServer 方法)

get_returncode() (asyncio.BaseSubprocessTransport 方法)

get_scheme() (wsgiref.handlers.BaseHandler 方法)

get_scheme_names() (在 sysconfig 模块中)

get_sequences() (mailbox.MH 方法)

(mailbox.MHMessage 方法)

get_server() (multiprocessing.managers.BaseManager 方法)

get_server_certificate() (在 ssl 模块中)

get_shapepoly() (在 turtle 模块中)

get_socket() (telnetlib.Telnet 方法)

get_source() (importlib.abc.InspectLoader 方法)

(importlib.abc.SourceLoader 方法)

(importlib.machinery.ExtensionFileLoader 方法)

(importlib.machinery.SourcelessFileLoader 方法)

(zipimport.zipimporter 方法)

get_special_folder_path() (內置函数)

get_stack() (asyncio.Task 方法)

(bdb.Bdb 方法)

get_start_method() (在 multiprocessing 模块中)

get_starttag_text() (html.parser.HTMLParser 方法)

get_stats() (在 gc 模块中)

get_stderr() (wsgiref.handlers.BaseHandler 方法)

(wsgiref.simple_server.WSGIRequestHandler 方法)

get_stdin() (wsgiref.handlers.BaseHandler 方法)

get_string() (mailbox.Mailbox 方法)

get_subdir() (mailbox.MaildirMessage 方法)

get_suffixes() (在 imp 模块中)

get_symbols() (symtable.SymbolTable 方法)

get_tag() (在 imp 模块中)

get_task_factory() (asyncio.AbstractEventLoop 方法)

get_terminal_size() (在 os 模块中)

(在 shutil 模块中)

get_terminator() (asynchat.async_chat 方法)

get_threshold() (在 gc 模块中)

get_token() (shlex.shlex 方法)

get_traceback_limit() (在 tracemalloc 模块中)

get_traced_memory() (在 tracemalloc 模块中)

get_tracemalloc_memory() (在 tracemalloc 模块中)

get_type() (symtable.SymbolTable 方法)

get_type_hints() (在 typing 模块中)

get_unixfrom() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_unpack_formats() (在 shutil 模块中)

get_usage() (optparse.OptionParser 方法)

get_value() (string.Formatter 方法)

get_version() (optparse.OptionParser 方法)

get_visible() (mailbox.BabylMessage 方法)

get_wch() (curses.window 方法)

get_write_buffer_limits() (asyncio.WriteTransport 方法)

get_write_buffer_size() (asyncio.WriteTransport 方法)

GET_YIELD_FROM_ITER (opcode)

getacl() (imaplib.IMAP4 方法)

getaddresses() (在 email.utils 模块中)

getaddrinfo() (asyncio.AbstractEventLoop 方法)

(在 socket 模块中)

getallocatedblocks() (在 sys 模块中)

getannotation() (imaplib.IMAP4 方法)

getargspec() (在 inspect 模块中)

getargvalues() (在 inspect 模块中)

getatime() (在 os.path 模块中)

getattr() (內置函数)

getattr_static() (在 inspect 模块中)

getAttribute() (xml.dom.Element 方法)

getAttributeNode() (xml.dom.Element 方法)

getAttributeNodeNS() (xml.dom.Element 方法)

getAttributeNS() (xml.dom.Element 方法)

GetBase() (xml.parsers.expat.xmlparser 方法)

getbegyx() (curses.window 方法)

getbkgd() (curses.window 方法)

getboolean() (configparser.ConfigParser 方法)

getbuffer() (io.BytesIO 方法)

getByteStream() (xml.sax.xmlreader.InputSource 方法)

getcallargs() (在 inspect 模块中)

getcanvas() (在 turtle 模块中)

getcapabilities() (nntplib.NNTP 方法)

getcaps() (在 mailcap 模块中)

getch() (curses.window 方法)

(在 msvcrt 模块中)

getCharacterStream() (xml.sax.xmlreader.InputSource 方法)

getche() (在 msvcrt 模块中)

getcheckinterval() (在 sys 模块中)

getChild() (logging.Logger 方法)

getchildren() (xml.etree.ElementTree.Element 方法)

getclasstree() (在 inspect 模块中)

getclosurevars() (在 inspect 模块中)

GetColumnInfo() (msilib.View 方法)

getColumnNumber() (xml.sax.xmlreader.Locator 方法)

getcomments() (在 inspect 模块中)

getcompname() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getcomptype() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getContentHandler() (xml.sax.xmlreader.XMLReader 方法)

getcontext() (在 decimal 模块中)

getcoroutinelocals() (在 inspect 模块中)

getcoroutinestate() (在 inspect 模块中)

getctime() (在 os.path 模块中)

getcwd() (在 os 模块中)

getcwdb() (在 os 模块中)

getcwdu (2to3 fixer)

getdecoder() (在 codecs 模块中)

getdefaultencoding() (在 sys 模块中)

getdefaultlocale() (在 locale 模块中)

getdefaulttimeout() (在 socket 模块中)

getdlopenflags() (在 sys 模块中)

getdoc() (在 inspect 模块中)

getDOMImplementation() (在 xml.dom 模块中)

getDTDHandler() (xml.sax.xmlreader.XMLReader 方法)

getEffectiveLevel() (logging.Logger 方法)

getegid() (在 os 模块中)

getElementsByTagName() (xml.dom.Document 方法)

(xml.dom.Element 方法)

getElementsByTagNameNS() (xml.dom.Document 方法)

(xml.dom.Element 方法)

getencoder() (在 codecs 模块中)

getEncoding() (xml.sax.xmlreader.InputSource 方法)

getEntityResolver() (xml.sax.xmlreader.XMLReader 方法)

getenv() (在 os 模块中)

getenvb() (在 os 模块中)

getErrorHandler() (xml.sax.xmlreader.XMLReader 方法)

geteuid() (在 os 模块中)

getEvent() (xml.dom.pulldom.DOMEventStream 方法)

getEventCategory() (logging.handlers.NTEventLogHandler 方法)

getEventType() (logging.handlers.NTEventLogHandler 方法)

getException() (xml.sax.SAXException 方法)

getFeature() (xml.sax.xmlreader.XMLReader 方法)

GetFieldCount() (msilib.Record 方法)

getfile() (在 inspect 模块中)

getfilesystemencodeerrors() (在 sys 模块中)

getfilesystemencoding() (在 sys 模块中)

getfirst() (cgi.FieldStorage 方法)

getfloat() (configparser.ConfigParser 方法)

getfmts() (ossaudiodev.oss_audio_device 方法)

getfqdn() (在 socket 模块中)

getframeinfo() (在 inspect 模块中)

getframerate() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getfullargspec() (在 inspect 模块中)

getgeneratorlocals() (在 inspect 模块中)

getgeneratorstate() (在 inspect 模块中)

getgid() (在 os 模块中)

getgrall() (在 grp 模块中)

getgrgid() (在 grp 模块中)

getgrnam() (在 grp 模块中)

getgrouplist() (在 os 模块中)

getgroups() (在 os 模块中)

getheader() (http.client.HTTPResponse 方法)

getheaders() (http.client.HTTPResponse 方法)

gethostbyaddr() (in module socket)

(在 socket 模块中)

gethostbyname() (在 socket 模块中)

gethostbyname_ex() (在 socket 模块中)

gethostname() (in module socket)

(在 socket 模块中)

getincrementaldecoder() (在 codecs 模块中)

getincrementalencoder() (在 codecs 模块中)

getinfo() (zipfile.ZipFile 方法)

getinnerframes() (在 inspect 模块中)

GetInputContext() (xml.parsers.expat.xmlparser 方法)

getint() (configparser.ConfigParser 方法)

GetInteger() (msilib.Record 方法)

getitem() (在 operator 模块中)

getiterator() (xml.etree.ElementTree.Element 方法)

(xml.etree.ElementTree.ElementTree 方法)

getitimer() (在 signal 模块中)

getkey() (curses.window 方法)

GetLastError() (在 ctypes 模块中)

getLength() (xml.sax.xmlreader.Attributes 方法)

getLevelName() (在 logging 模块中)

getline() (在 linecache 模块中)

getLineNumber() (xml.sax.xmlreader.Locator 方法)

getlist() (cgi.FieldStorage 方法)

getloadavg() (在 os 模块中)

getlocale() (在 locale 模块中)

getLogger() (在 logging 模块中)

getLoggerClass() (在 logging 模块中)

getlogin() (在 os 模块中)

getLogRecordFactory() (在 logging 模块中)

getmark() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getmarkers() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getmaxyx() (curses.window 方法)

getmember() (tarfile.TarFile 方法)

getmembers() (tarfile.TarFile 方法)

(在 inspect 模块中)

getMessage() (logging.LogRecord 方法)

(xml.sax.SAXException 方法)

getMessageID() (logging.handlers.NTEventLogHandler 方法)

getmodule() (在 inspect 模块中)

getmodulename() (在 inspect 模块中)

getmouse() (在 curses 模块中)

getmro() (在 inspect 模块中)

getmtime() (在 os.path 模块中)

getname() (chunk.Chunk 方法)

getName() (threading.Thread 方法)

getNameByQName() (xml.sax.xmlreader.AttributesNS 方法)

getnameinfo() (asyncio.AbstractEventLoop 方法)

(在 socket 模块中)

getnames() (tarfile.TarFile 方法)

getNames() (xml.sax.xmlreader.Attributes 方法)

getnchannels() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getnframes() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getnode

getnode() (在 uuid 模块中)

getopt (模块)

getopt() (distutils.fancy_getopt.FancyGetopt 方法)

(在 getopt 模块中)

GetoptError

getouterframes() (在 inspect 模块中)

getoutput() (在 subprocess 模块中)

getpagesize() (在 resource 模块中)

getparams() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getparyx() (curses.window 方法)

getpass (模块)

getpass() (在 getpass 模块中)

GetPassWarning

getpeercert() (ssl.SSLSocket 方法)

getpeername() (socket.socket 方法)

getpen() (在 turtle 模块中)

getpgid() (在 os 模块中)

getpgrp() (在 os 模块中)

getpid() (在 os 模块中)

getpos() (html.parser.HTMLParser 方法)

getppid() (在 os 模块中)

getpreferredencoding() (在 locale 模块中)

getpriority() (在 os 模块中)

getprofile() (在 sys 模块中)

GetProperty() (msilib.SummaryInformation 方法)

getProperty() (xml.sax.xmlreader.XMLReader 方法)

GetPropertyCount() (msilib.SummaryInformation 方法)

getprotobyname() (在 socket 模块中)

getproxies() (在 urllib.request 模块中)

getPublicId() (xml.sax.xmlreader.InputSource 方法)

(xml.sax.xmlreader.Locator 方法)

getpwall() (在 pwd 模块中)

getpwnam() (在 pwd 模块中)

getpwuid() (在 pwd 模块中)

getQNameByName() (xml.sax.xmlreader.AttributesNS 方法)

getQNames() (xml.sax.xmlreader.AttributesNS 方法)

getquota() (imaplib.IMAP4 方法)

getquotaroot() (imaplib.IMAP4 方法)

getrandbits() (在 random 模块中)

getrandom() (在 os 模块中)

getreader() (在 codecs 模块中)

getrecursionlimit() (在 sys 模块中)

getrefcount() (在 sys 模块中)

getresgid() (在 os 模块中)

getresponse() (http.client.HTTPConnection 方法)

getresuid() (在 os 模块中)

getrlimit() (在 resource 模块中)

getroot() (xml.etree.ElementTree.ElementTree 方法)

getrusage() (在 resource 模块中)

getsample() (在 audioop 模块中)

getsampwidth() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getscreen() (在 turtle 模块中)

getservbyname() (在 socket 模块中)

getservbyport() (在 socket 模块中)

GetSetDescriptorType() (在 types 模块中)

getshapes() (在 turtle 模块中)

getsid() (在 os 模块中)

getsignal() (在 signal 模块中)

getsitepackages() (在 site 模块中)

getsize() (chunk.Chunk 方法)

(在 os.path 模块中)

getsizeof() (在 sys 模块中)

getsockname() (socket.socket 方法)

getsockopt() (socket.socket 方法)

getsource() (在 inspect 模块中)

getsourcefile() (在 inspect 模块中)

getsourcelines() (在 inspect 模块中)

getspall() (在 spwd 模块中)

getspnam() (在 spwd 模块中)

getstate() (codecs.IncrementalDecoder 方法)

(codecs.IncrementalEncoder 方法)

(在 random 模块中)

getstatusoutput() (在 subprocess 模块中)

getstr() (curses.window 方法)

GetString() (msilib.Record 方法)

getSubject() (logging.handlers.SMTPHandler 方法)

GetSummaryInformation() (msilib.Database 方法)

getswitchinterval() (在 sys 模块中)

getSystemId() (xml.sax.xmlreader.InputSource 方法)

(xml.sax.xmlreader.Locator 方法)

getsyx() (在 curses 模块中)

gettarinfo() (tarfile.TarFile 方法)

gettempdir() (在 tempfile 模块中)

gettempdirb() (在 tempfile 模块中)

gettempprefix() (在 tempfile 模块中)

gettempprefixb() (在 tempfile 模块中)

getTestCaseNames() (unittest.TestLoader 方法)

gettext (模块)

gettext() (gettext.GNUTranslations 方法)

(gettext.NullTranslations 方法)

(在 gettext 模块中)

(在 locale 模块中)

gettimeout() (socket.socket 方法)

gettrace() (在 sys 模块中)

getturtle() (在 turtle 模块中)

getType() (xml.sax.xmlreader.Attributes 方法)

getuid() (在 os 模块中)

geturl() (urllib.parse.urllib.parse.SplitResult 方法)

getuser() (在 getpass 模块中)

getuserbase() (在 site 模块中)

getusersitepackages() (在 site 模块中)

getvalue() (io.BytesIO 方法)

(io.StringIO 方法)

getValue() (xml.sax.xmlreader.Attributes 方法)

getValueByQName() (xml.sax.xmlreader.AttributesNS 方法)

getwch() (在 msvcrt 模块中)

getwche() (在 msvcrt 模块中)

getweakrefcount() (在 weakref 模块中)

getweakrefs() (在 weakref 模块中)

getwelcome() (ftplib.FTP 方法)

(nntplib.NNTP 方法)

(poplib.POP3 方法)

getwin() (在 curses 模块中)

getwindowsversion() (在 sys 模块中)

getwriter() (在 codecs 模块中)

getxattr() (在 os 模块中)

getyx() (curses.window 方法)

gid (tarfile.TarInfo 属性)

GIL

glob

模块

glob (模块)

glob() (msilib.Directory 方法)

(pathlib.Path 方法)

(在 glob 模块中)

global

name binding

namespace

语句, [1]

global interpreter lock

global interpreter lock -- 全局解释器锁

globals() (內置函数)

globs (doctest.DocTest 属性)

gmtime() (在 time 模块中)

gname (tarfile.TarInfo 属性)

GNOME

GNU_FORMAT() (在 tarfile 模块中)

gnu_getopt() (在 getopt 模块中)

GNUTranslations (gettext 中的类)

got (doctest.DocTestFailure 属性)

goto() (在 turtle 模块中)

grammar

Graphical User Interface

GREATER() (在 token 模块中)

GREATEREQUAL() (在 token 模块中)

Greenwich Mean Time

GRND_NONBLOCK() (在 os 模块中)

GRND_RANDOM() (在 os 模块中)

Group (email.headerregistry 中的类)

group() (nntplib.NNTP 方法)

(pathlib.Path 方法)

(re.match 方法)

groupby() (在 itertools 模块中)

groupdict() (re.match 方法)

groupindex (re.regex 属性)

grouping

groups (email.headerregistry.AddressHeader 属性)

(re.regex 属性)

groups() (re.match 方法)

grp (模块)

gt() (在 operator 模块中)

guess_all_extensions() (mimetypes.MimeTypes 方法)

(在 mimetypes 模块中)

guess_extension() (mimetypes.MimeTypes 方法)

(在 mimetypes 模块中)

guess_scheme() (在 wsgiref.util 模块中)

guess_type() (mimetypes.MimeTypes 方法)

(在 mimetypes 模块中)

GUI

gzip (模块)

GzipFile (gzip 中的类)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – H

halfdelay() (在 curses 模块中)

Handle (asyncio 中的类)

handle an exception

handle() (http.server.BaseHTTPRequestHandler 方法)

(logging.Handler 方法)

(logging.handlers.QueueListener 方法)

(logging.Logger 方法)

(logging.NullHandler 方法)

(socketserver.BaseRequestHandler 方法)

(wsgiref.simple_server.WSGIRequestHandler 方法)

handle_accept() (asyncore.dispatcher 方法)

handle_accepted() (asyncore.dispatcher 方法)

handle_charref() (html.parser.HTMLParser 方法)

handle_close() (asyncore.dispatcher 方法)

handle_comment() (html.parser.HTMLParser 方法)

handle_connect() (asyncore.dispatcher 方法)

handle_data() (html.parser.HTMLParser 方法)

handle_decl() (html.parser.HTMLParser 方法)

handle_defect() (email.policy.Policy 方法)

handle_endtag() (html.parser.HTMLParser 方法)

handle_entityref() (html.parser.HTMLParser 方法)

handle_error() (asyncore.dispatcher 方法)

(socketserver.BaseServer 方法)

handle_expect_100() (http.server.BaseHTTPRequestHandler 方法)

handle_expt() (asyncore.dispatcher 方法)

handle_one_request() (http.server.BaseHTTPRequestHandler 方法)

handle_pi() (html.parser.HTMLParser 方法)

handle_read() (asyncore.dispatcher 方法)

handle_request() (socketserver.BaseServer 方法)

(xmlrpc.server.CGIXMLRPCRequestHandler 方法)

handle_startendtag() (html.parser.HTMLParser 方法)

handle_starttag() (html.parser.HTMLParser 方法)

handle_timeout() (socketserver.BaseServer 方法)

handle_write() (asyncore.dispatcher 方法)

handleError() (logging.Handler 方法)

(logging.handlers.SocketHandler 方法)

handler

exception

Handler (logging 中的类)

handler() (在 cgitb 模块中)

harmonic_mean() (在 statistics 模块中)

HAS_ALPN() (在 ssl 模块中)

has_children() (symtable.SymbolTable 方法)

has_colors() (在 curses 模块中)

HAS_ECDH() (在 ssl 模块中)

has_exec() (symtable.SymbolTable 方法)

has_extn() (smtplib.SMTP 方法)

has_function() (distutils.ccompiler.CCompiler 方法)

has_header() (csv.Sniffer 方法)

(urllib.request.Request 方法)

has_ic() (在 curses 模块中)

has_il() (在 curses 模块中)

has_ipv6() (在 socket 模块中)

has_key (2to3 fixer)

has_key() (在 curses 模块中)

has_location (importlib.machinery.ModuleSpec 属性)

has_nonstandard_attr() (http.cookiejar.Cookie 方法)

HAS_NPN() (在 ssl 模块中)

has_option() (configparser.ConfigParser 方法)

(optparse.OptionParser 方法)

has_section() (configparser.ConfigParser 方法)

HAS_SNI() (在 ssl 模块中)

has_ticket (ssl.SSLSession 属性)

HAS_TLSv1_3() (在 ssl 模块中)

hasattr() (內置函数)

hasAttribute() (xml.dom.Element 方法)

hasAttributeNS() (xml.dom.Element 方法)

hasAttributes() (xml.dom.Node 方法)

hasChildNodes() (xml.dom.Node 方法)

hascompare() (在 dis 模块中)

hasconst() (在 dis 模块中)

hasFeature() (xml.dom.DOMImplementation 方法)

hasfree() (在 dis 模块中)

hash

內置函数, [1], [2], [3]

hash character

hash() (內置函数)

hash.block_size() (在 hashlib 模块中)

hash.digest_size() (在 hashlib 模块中)

hash_info() (在 sys 模块中)

hashable

Hashable (collections.abc 中的类)

(typing 中的类)

hashable -- 可哈希

hasHandlers() (logging.Logger 方法)

hashlib (模块)

hasjabs() (在 dis 模块中)

hasjrel() (在 dis 模块中)

haslocal() (在 dis 模块中)

hasname() (在 dis 模块中)

HAVE_ARGUMENT (opcode)

HAVE_THREADS() (在 decimal 模块中)

HCI_DATA_DIR() (在 socket 模块中)

HCI_FILTER() (在 socket 模块中)

HCI_TIME_STAMP() (在 socket 模块中)

head() (nntplib.NNTP 方法)

Header (email.header 中的类)

header_encode() (email.charset.Charset 方法)

header_encode_lines() (email.charset.Charset 方法)

header_encoding (email.charset.Charset 属性)

header_factory (email.policy.EmailPolicy 属性)

header_fetch_parse() (email.policy.Compat32 方法)

(email.policy.EmailPolicy 方法)

(email.policy.Policy 方法)

header_items() (urllib.request.Request 方法)

header_max_count() (email.policy.EmailPolicy 方法)

(email.policy.Policy 方法)

header_offset (zipfile.ZipInfo 属性)

header_source_parse() (email.policy.Compat32 方法)

(email.policy.EmailPolicy 方法)

(email.policy.Policy 方法)

header_store_parse() (email.policy.Compat32 方法)

(email.policy.EmailPolicy 方法)

(email.policy.Policy 方法)

HeaderError

HeaderParseError

HeaderParser (email.parser 中的类)

HeaderRegistry (email.headerregistry 中的类)

headers

MIME, [1]

headers (http.server.BaseHTTPRequestHandler 属性)

(urllib.error.HTTPError 属性)

Headers (wsgiref.headers 中的类)

headers (xmlrpc.client.ProtocolError 属性)

heading() (tkinter.ttk.Treeview 方法)

(在 turtle 模块中)

heapify() (在 heapq 模块中)

heapmin() (在 msvcrt 模块中)

heappop() (在 heapq 模块中)

heappush() (在 heapq 模块中)

heappushpop() (在 heapq 模块中)

heapq (模块)

heapreplace() (在 heapq 模块中)

helo() (smtplib.SMTP 方法)

help

online

內置函数

help (optparse.Option 属性)

(pdb command)

help() (nntplib.NNTP 方法)

(內置函数)

herror

hex (uuid.UUID 属性)

hex() (bytearray 方法)

(bytes 方法)

(float 方法)

(memoryview 方法)

(內置函数)

hexadecimal

literals

hexadecimal literal

hexbin() (在 binhex 模块中)

hexdigest() (hashlib.hash 方法)

(hashlib.shake 方法)

(hmac.HMAC 方法)

hexdigits() (在 string 模块中)

hexlify() (在 binascii 模块中)

hexversion() (在 sys 模块中)

hidden() (curses.panel.Panel 方法)

hide() (curses.panel.Panel 方法)

(tkinter.ttk.Notebook 方法)

hide_cookie2 (http.cookiejar.CookiePolicy 属性)

hideturtle() (在 turtle 模块中)

hierarchy

type

HierarchyRequestErr

HIGHEST_PROTOCOL() (在 pickle 模块中)

HKEY_CLASSES_ROOT() (在 winreg 模块中)

HKEY_CURRENT_CONFIG() (在 winreg 模块中)

HKEY_CURRENT_USER() (在 winreg 模块中)

HKEY_DYN_DATA() (在 winreg 模块中)

HKEY_LOCAL_MACHINE() (在 winreg 模块中)

HKEY_PERFORMANCE_DATA() (在 winreg 模块中)

HKEY_USERS() (在 winreg 模块中)

hline() (curses.window 方法)

HList (tkinter.tix 中的类)

hls_to_rgb() (在 colorsys 模块中)

hmac (模块)

HOME, [1], [2], [3], [4]

home() (pathlib.Path 类方法)

(在 turtle 模块中)

HOMEDRIVE, [1]

HOMEPATH, [1]

hook_compressed() (在 fileinput 模块中)

hook_encoded() (在 fileinput 模块中)

hooks

import

meta

path

host (urllib.request.Request 属性)

hostmask (ipaddress.IPv4Network 属性)

(ipaddress.IPv6Network 属性)

hosts (netrc.netrc 属性)

hosts() (ipaddress.IPv4Network 方法)

(ipaddress.IPv6Network 方法)

hour (datetime.datetime 属性)

(datetime.time 属性)

HRESULT (ctypes 中的类)

hStdError (subprocess.STARTUPINFO 属性)

hStdInput (subprocess.STARTUPINFO 属性)

hStdOutput (subprocess.STARTUPINFO 属性)

hsv_to_rgb() (在 colorsys 模块中)

ht() (在 turtle 模块中)

HTML, [1]

html (模块)

html.entities (模块)

html.parser (模块)

html5() (在 html.entities 模块中)

HTMLCalendar (calendar 中的类)

HtmlDiff (difflib 中的类)

HTMLParser (html.parser 中的类)

htonl() (在 socket 模块中)

htons() (在 socket 模块中)

HTTP

http (standard module)

http.client (standard module)

protocol, [1], [2], [3], [4], [5]

http (模块)

HTTP() (在 email.policy 模块中)

http.client (模块)

http.cookiejar (模块)

http.cookies (模块)

http.server (模块)

http_error_301() (urllib.request.HTTPRedirectHandler 方法)

http_error_302() (urllib.request.HTTPRedirectHandler 方法)

http_error_303() (urllib.request.HTTPRedirectHandler 方法)

http_error_307() (urllib.request.HTTPRedirectHandler 方法)

http_error_401() (urllib.request.HTTPBasicAuthHandler 方法)

(urllib.request.HTTPDigestAuthHandler 方法)

http_error_407() (urllib.request.ProxyBasicAuthHandler 方法)

(urllib.request.ProxyDigestAuthHandler 方法)

http_error_auth_reqed() (urllib.request.AbstractBasicAuthHandler 方法)

(urllib.request.AbstractDigestAuthHandler 方法)

http_error_default() (urllib.request.BaseHandler 方法)

http_error_nnn() (urllib.request.BaseHandler 方法)

http_open() (urllib.request.HTTPHandler 方法)

HTTP_PORT() (在 http.client 模块中)

http_proxy, [1], [2]

http_response() (urllib.request.HTTPErrorProcessor 方法)

http_version (wsgiref.handlers.BaseHandler 属性)

HTTPBasicAuthHandler (urllib.request 中的类)

HTTPConnection (http.client 中的类)

HTTPCookieProcessor (urllib.request 中的类)

httpd

HTTPDefaultErrorHandler (urllib.request 中的类)

HTTPDigestAuthHandler (urllib.request 中的类)

HTTPError

HTTPErrorProcessor (urllib.request 中的类)

HTTPException

HTTPHandler (logging.handlers 中的类)

(urllib.request 中的类)

HTTPPasswordMgr (urllib.request 中的类)

HTTPPasswordMgrWithDefaultRealm (urllib.request 中的类)

HTTPPasswordMgrWithPriorAuth (urllib.request 中的类)

HTTPRedirectHandler (urllib.request 中的类)

HTTPResponse (http.client 中的类)

https_open() (urllib.request.HTTPSHandler 方法)

HTTPS_PORT() (在 http.client 模块中)

https_response() (urllib.request.HTTPErrorProcessor 方法)

HTTPSConnection (http.client 中的类)

HTTPServer (http.server 中的类)

HTTPSHandler (urllib.request 中的类)

HTTPStatus (http 中的类)

hypot() (在 math 模块中)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – I

I() (在 re 模块中)

I/O control

buffering, [1]

POSIX

tty

UNIX

iadd() (在 operator 模块中)

iand() (在 operator 模块中)

iconcat() (在 operator 模块中)

id

內置函数

id (ssl.SSLSession 属性)

id() (unittest.TestCase 方法)

(內置函数)

idcok() (curses.window 方法)

ident (select.kevent 属性)

(threading.Thread 属性)

identchars (cmd.Cmd 属性)

identifier, [1]

identify() (tkinter.ttk.Notebook 方法)

(tkinter.ttk.Treeview 方法)

(tkinter.ttk.Widget 方法)

identify_column() (tkinter.ttk.Treeview 方法)

identify_element() (tkinter.ttk.Treeview 方法)

identify_region() (tkinter.ttk.Treeview 方法)

identify_row() (tkinter.ttk.Treeview 方法)

identity

test

identity of an object

idioms (2to3 fixer)

IDLE, [1]

IDLESTARTUP, [1]

idlok() (curses.window 方法)

IEEE-754

if

conditional expression

in comprehensions

语句, [1]

if_indextoname() (在 socket 模块中)

if_nameindex() (在 socket 模块中)

if_nametoindex() (在 socket 模块中)

ifloordiv() (在 operator 模块中)

iglob() (在 glob 模块中)

ignorableWhitespace() (xml.sax.handler.ContentHandler 方法)

ignore (pdb command)

ignore_errors() (在 codecs 模块中)

IGNORE_EXCEPTION_DETAIL() (在 doctest 模块中)

ignore_patterns() (在 shutil 模块中)

IGNORECASE() (在 re 模块中)

ihave() (nntplib.NNTP 方法)

IISCGIHandler (wsgiref.handlers 中的类)

ilshift() (在 operator 模块中)

imag (numbers.Complex 属性)

imaginary literal

imap() (multiprocessing.pool.Pool 方法)

IMAP4

protocol

IMAP4 (imaplib 中的类)

IMAP4.abort

IMAP4.error

IMAP4.readonly

IMAP4_SSL

protocol

IMAP4_SSL (imaplib 中的类)

IMAP4_stream

protocol

IMAP4_stream (imaplib 中的类)

imap_unordered() (multiprocessing.pool.Pool 方法)

imaplib (模块)

imatmul() (在 operator 模块中)

imghdr (模块)

immedok() (curses.window 方法)

immutable

data type

object, [1]

sequence types

对象

immutable -- 不可变

immutable object

immutable sequence

对象

immutable types

subclassing

imod() (在 operator 模块中)

imp

模块

imp (模块)

ImpImporter (pkgutil 中的类)

implementation() (在 sys 模块中)

ImpLoader (pkgutil 中的类)

import

hooks

语句, [1], [2], [3], [4]

import (2to3 fixer)

import hooks

import machinery

import path -- 导入路径

import_fresh_module() (在 test.support 模块中)

IMPORT_FROM (opcode)

import_module() (在 importlib 模块中)

(在 test.support 模块中)

IMPORT_NAME (opcode)

IMPORT_STAR (opcode)

importer -- 导入器

ImportError

例外

importing -- 导入

importlib (模块)

importlib.abc (模块)

importlib.machinery (模块)

importlib.util (模块)

imports (2to3 fixer)

imports2 (2to3 fixer)

ImportWarning

ImproperConnectionState

imul() (在 operator 模块中)

in

关键字, [1]

运算符, [1], [2]

in_dll() (ctypes._CData 方法)

in_table_a1() (在 stringprep 模块中)

in_table_b1() (在 stringprep 模块中)

in_table_c11() (在 stringprep 模块中)

in_table_c11_c12() (在 stringprep 模块中)

in_table_c12() (在 stringprep 模块中)

in_table_c21() (在 stringprep 模块中)

in_table_c21_c22() (在 stringprep 模块中)

in_table_c22() (在 stringprep 模块中)

in_table_c3() (在 stringprep 模块中)

in_table_c4() (在 stringprep 模块中)

in_table_c5() (在 stringprep 模块中)

in_table_c6() (在 stringprep 模块中)

in_table_c7() (在 stringprep 模块中)

in_table_c8() (在 stringprep 模块中)

in_table_c9() (在 stringprep 模块中)

in_table_d1() (在 stringprep 模块中)

in_table_d2() (在 stringprep 模块中)

in_transaction (sqlite3.Connection 属性)

inch() (curses.window 方法)

inclusive

or

inclusive (tracemalloc.DomainFilter 属性)

(tracemalloc.Filter 属性)

Incomplete

IncompleteRead

IncompleteReadError

incr_item(), [1]

increment_lineno() (在 ast 模块中)

IncrementalDecoder (codecs 中的类)

incrementaldecoder (codecs.CodecInfo 属性)

IncrementalEncoder (codecs 中的类)

incrementalencoder (codecs.CodecInfo 属性)

IncrementalNewlineDecoder (io 中的类)

IncrementalParser (xml.sax.xmlreader 中的类)

indent (doctest.Example 属性)

INDENT token

indent() (在 textwrap 模块中)

INDENT() (在 token 模块中)

indentation

IndentationError

index operation

index() (array.array 方法)

(bytearray 方法)

(bytes 方法)

(collections.deque 方法)

(sequence method)

(str 方法)

(tkinter.ttk.Notebook 方法)

(tkinter.ttk.Treeview 方法)

(在 operator 模块中)

IndexError

indexOf() (在 operator 模块中)

IndexSizeErr

indices() (slice 方法)

inet_aton() (在 socket 模块中)

inet_ntoa() (在 socket 模块中)

inet_ntop() (在 socket 模块中)

inet_pton() (在 socket 模块中)

Inexact (decimal 中的类)

inf() (在 cmath 模块中)

(在 math 模块中)

infile

json.tool command line option

infile (shlex.shlex 属性)

Infinity

infj() (在 cmath 模块中)

info() (dis.Bytecode 方法)

(gettext.NullTranslations 方法)

(logging.Logger 方法)

(在 logging 模块中)

infolist() (zipfile.ZipFile 方法)

inheritance

ini file

init() (在 mimetypes 模块中)

init_color() (在 curses 模块中)

init_database() (在 msilib 模块中)

init_pair() (在 curses 模块中)

inited() (在 mimetypes 模块中)

initgroups() (在 os 模块中)

initial_indent (textwrap.TextWrapper 属性)

initialize_options() (distutils.cmd.Command 方法)

initscr() (在 curses 模块中)

inode() (os.DirEntry 方法)

INPLACE_ADD (opcode)

INPLACE_AND (opcode)

INPLACE_FLOOR_DIVIDE (opcode)

INPLACE_LSHIFT (opcode)

INPLACE_MATRIX_MULTIPLY (opcode)

INPLACE_MODULO (opcode)

INPLACE_MULTIPLY (opcode)

INPLACE_OR (opcode)

INPLACE_POWER (opcode)

INPLACE_RSHIFT (opcode)

INPLACE_SUBTRACT (opcode)

INPLACE_TRUE_DIVIDE (opcode)

INPLACE_XOR (opcode)

input

(2to3 fixer)

input() (內置函数)

(在 fileinput 模块中)

input_charset (email.charset.Charset 属性)

input_codec (email.charset.Charset 属性)

InputOnly (tkinter.tix 中的类)

InputSource (xml.sax.xmlreader 中的类)

inquiry (C 类型)

insch() (curses.window 方法)

insdelln() (curses.window 方法)

insert() (array.array 方法)

(collections.deque 方法)

(sequence method)

(tkinter.ttk.Notebook 方法)

(tkinter.ttk.Treeview 方法)

(xml.etree.ElementTree.Element 方法)

insert_text() (在 readline 模块中)

insertBefore() (xml.dom.Node 方法)

insertln() (curses.window 方法)

insnstr() (curses.window 方法)

insort() (在 bisect 模块中)

insort_left() (在 bisect 模块中)

insort_right() (在 bisect 模块中)

inspect (模块)

inspect command line option

--details

InspectLoader (importlib.abc 中的类)

insstr() (curses.window 方法)

install() (gettext.NullTranslations 方法)

(在 gettext 模块中)

install_opener() (在 urllib.request 模块中)

install_scripts() (venv.EnvBuilder 方法)

installHandler() (在 unittest 模块中)

instance

call, [1]

class

对象, [1], [2]

instancemethod

对象

instate() (tkinter.ttk.Widget 方法)

instr() (curses.window 方法)

instream (shlex.shlex 属性)

Instruction (dis 中的类)

Instruction.arg() (在 dis 模块中)

Instruction.argrepr() (在 dis 模块中)

Instruction.argval() (在 dis 模块中)

Instruction.is_jump_target() (在 dis 模块中)

Instruction.offset() (在 dis 模块中)

Instruction.opcode() (在 dis 模块中)

Instruction.opname() (在 dis 模块中)

Instruction.starts_line() (在 dis 模块中)

int

內置函数, [1], [2]

int (uuid.UUID 属性)

(內置类)

Int2AP() (在 imaplib 模块中)

int_info() (在 sys 模块中)

integer

literals

representation

types, operations on

对象, [1], [2]

integer literal

Integral (numbers 中的类)

Integrated Development Environment

IntegrityError

Intel/DVI ADPCM

IntEnum (enum 中的类)

interact (pdb command)

interact() (code.InteractiveConsole 方法)

(telnetlib.Telnet 方法)

(在 code 模块中)

interactive -- 交互

interactive mode

InteractiveConsole (code 中的类)

InteractiveInterpreter (code 中的类)

intern (2to3 fixer)

intern() (在 sys 模块中)

internal type

internal_attr (zipfile.ZipInfo 属性)

Internaldate2tuple() (在 imaplib 模块中)

internalSubset (xml.dom.DocumentType 属性)

Internet

interpolated string literal

interpolation

bytearray (%)

bytes (%)

interpolation, string (%)

InterpolationDepthError

InterpolationError

InterpolationMissingOptionError

InterpolationSyntaxError

interpreted -- 解释型

interpreter

interpreter lock

interpreter prompts

interpreter shutdown -- 解释器关闭

interrupt() (sqlite3.Connection 方法)

interrupt_main() (在 _thread 模块中)

InterruptedError

intersection() (frozenset 方法)

intersection_update() (frozenset 方法)

IntFlag (enum 中的类)

intro (cmd.Cmd 属性)

InuseAttributeErr

inv() (在 operator 模块中)

InvalidAccessErr

invalidate_caches() (importlib.abc.MetaPathFinder 方法)

(importlib.abc.PathEntryFinder 方法)

(importlib.machinery.FileFinder 方法)

(importlib.machinery.PathFinder 类方法)

(在 importlib 模块中)

InvalidCharacterErr

InvalidModificationErr

InvalidOperation (decimal 中的类)

InvalidStateErr

InvalidStateError

InvalidURL

inversion

invert() (在 operator 模块中)

invocation

io

模块

IO (typing 中的类)

io (模块)

io.StringIO

对象

IOBase (io 中的类)

ioctl() (socket.socket 方法)

(在 fcntl 模块中)

IOError

ior() (在 operator 模块中)

ip (ipaddress.IPv4Interface 属性)

(ipaddress.IPv6Interface 属性)

ip_address() (在 ipaddress 模块中)

ip_interface() (在 ipaddress 模块中)

ip_network() (在 ipaddress 模块中)

ipaddress (模块)

ipow() (在 operator 模块中)

ipv4_mapped (ipaddress.IPv6Address 属性)

IPv4Address (ipaddress 中的类)

IPv4Interface (ipaddress 中的类)

IPv4Network (ipaddress 中的类)

IPv6Address (ipaddress 中的类)

IPv6Interface (ipaddress 中的类)

IPv6Network (ipaddress 中的类)

irshift() (在 operator 模块中)

is

运算符, [1]

is not

运算符, [1]

is_() (在 operator 模块中)

is_absolute() (pathlib.PurePath 方法)

is_alive() (multiprocessing.Process 方法)

(threading.Thread 方法)

is_assigned() (symtable.Symbol 方法)

is_attachment() (email.message.EmailMessage 方法)

is_authenticated() (urllib.request.HTTPPasswordMgrWithPriorAuth 方法)

is_block_device() (pathlib.Path 方法)

is_blocked() (http.cookiejar.DefaultCookiePolicy 方法)

is_canonical() (decimal.Context 方法)

(decimal.Decimal 方法)

is_char_device() (pathlib.Path 方法)

IS_CHARACTER_JUNK() (在 difflib 模块中)

is_check_supported() (在 lzma 模块中)

is_closed() (asyncio.AbstractEventLoop 方法)

is_closing() (asyncio.BaseTransport 方法)

is_declared_global() (symtable.Symbol 方法)

is_dir() (os.DirEntry 方法)

(pathlib.Path 方法)

(zipfile.ZipInfo 方法)

is_enabled() (在 faulthandler 模块中)

is_expired() (http.cookiejar.Cookie 方法)

is_fifo() (pathlib.Path 方法)

is_file() (os.DirEntry 方法)

(pathlib.Path 方法)

is_finalizing() (在 sys 模块中)

is_finite() (decimal.Context 方法)

(decimal.Decimal 方法)

is_free() (symtable.Symbol 方法)

is_global (ipaddress.IPv4Address 属性)

(ipaddress.IPv6Address 属性)

is_global() (symtable.Symbol 方法)

is_hop_by_hop() (在 wsgiref.util 模块中)

is_imported() (symtable.Symbol 方法)

is_infinite() (decimal.Context 方法)

(decimal.Decimal 方法)

is_integer() (float 方法)

is_jython() (在 test.support 模块中)

IS_LINE_JUNK() (在 difflib 模块中)

is_linetouched() (curses.window 方法)

is_link_local (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

is_local() (symtable.Symbol 方法)

is_loopback (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

is_multicast (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

is_multipart() (email.message.EmailMessage 方法)

(email.message.Message 方法)

is_namespace() (symtable.Symbol 方法)

is_nan() (decimal.Context 方法)

(decimal.Decimal 方法)

is_nested() (symtable.SymbolTable 方法)

is_normal() (decimal.Context 方法)

(decimal.Decimal 方法)

is_not() (在 operator 模块中)

is_not_allowed() (http.cookiejar.DefaultCookiePolicy 方法)

is_optimized() (symtable.SymbolTable 方法)

is_package() (importlib.abc.InspectLoader 方法)

(importlib.abc.SourceLoader 方法)

(importlib.machinery.ExtensionFileLoader 方法)

(importlib.machinery.SourceFileLoader 方法)

(importlib.machinery.SourcelessFileLoader 方法)

(zipimport.zipimporter 方法)

is_parameter() (symtable.Symbol 方法)

is_private (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

is_python_build() (在 sysconfig 模块中)

is_qnan() (decimal.Context 方法)

(decimal.Decimal 方法)

is_referenced() (symtable.Symbol 方法)

is_reserved (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

is_reserved() (pathlib.PurePath 方法)

is_resource_enabled() (在 test.support 模块中)

is_running() (asyncio.AbstractEventLoop 方法)

is_set() (asyncio.Event 方法)

(threading.Event 方法)

is_signed() (decimal.Context 方法)

(decimal.Decimal 方法)

is_site_local (ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

is_snan() (decimal.Context 方法)

(decimal.Decimal 方法)

is_socket() (pathlib.Path 方法)

is_subnormal() (decimal.Context 方法)

(decimal.Decimal 方法)

is_symlink() (os.DirEntry 方法)

(pathlib.Path 方法)

is_tarfile() (在 tarfile 模块中)

is_term_resized() (在 curses 模块中)

is_tracing() (在 tracemalloc 模块中)

is_tracked() (在 gc 模块中)

is_unspecified (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

is_wintouched() (curses.window 方法)

is_zero() (decimal.Context 方法)

(decimal.Decimal 方法)

is_zipfile() (在 zipfile 模块中)

isabs() (在 os.path 模块中)

isabstract() (在 inspect 模块中)

IsADirectoryError

isalnum() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 curses.ascii 模块中)

isalpha() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 curses.ascii 模块中)

isascii() (在 curses.ascii 模块中)

isasyncgen() (在 inspect 模块中)

isasyncgenfunction() (在 inspect 模块中)

isatty() (chunk.Chunk 方法)

(io.IOBase 方法)

(在 os 模块中)

isawaitable() (在 inspect 模块中)

isblank() (在 curses.ascii 模块中)

isblk() (tarfile.TarInfo 方法)

isbuiltin() (在 inspect 模块中)

ischr() (tarfile.TarInfo 方法)

isclass() (在 inspect 模块中)

isclose() (在 cmath 模块中)

(在 math 模块中)

iscntrl() (在 curses.ascii 模块中)

iscode() (在 inspect 模块中)

iscoroutine() (在 asyncio 模块中)

(在 inspect 模块中)

iscoroutinefunction() (在 asyncio 模块中)

(在 inspect 模块中)

isctrl() (在 curses.ascii 模块中)

isDaemon() (threading.Thread 方法)

isdatadescriptor() (在 inspect 模块中)

isdecimal() (str 方法)

isdev() (tarfile.TarInfo 方法)

isdigit() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 curses.ascii 模块中)

isdir() (tarfile.TarInfo 方法)

(在 os.path 模块中)

isdisjoint() (frozenset 方法)

isdown() (在 turtle 模块中)

iselement() (在 xml.etree.ElementTree 模块中)

isenabled() (在 gc 模块中)

isEnabledFor() (logging.Logger 方法)

isendwin() (在 curses 模块中)

ISEOF() (在 token 模块中)

isexpr() (parser.ST 方法)

(在 parser 模块中)

isfifo() (tarfile.TarInfo 方法)

isfile() (tarfile.TarInfo 方法)

(在 os.path 模块中)

isfinite() (在 cmath 模块中)

(在 math 模块中)

isfirstline() (在 fileinput 模块中)

isframe() (在 inspect 模块中)

isfunction() (在 inspect 模块中)

isgenerator() (在 inspect 模块中)

isgeneratorfunction() (在 inspect 模块中)

isgetsetdescriptor() (在 inspect 模块中)

isgraph() (在 curses.ascii 模块中)

isidentifier() (str 方法)

isinf() (在 cmath 模块中)

(在 math 模块中)

isinstance (2to3 fixer)

isinstance() (內置函数)

iskeyword() (在 keyword 模块中)

isleap() (在 calendar 模块中)

islice() (在 itertools 模块中)

islink() (在 os.path 模块中)

islnk() (tarfile.TarInfo 方法)

islower() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 curses.ascii 模块中)

ismemberdescriptor() (在 inspect 模块中)

ismeta() (在 curses.ascii 模块中)

ismethod() (在 inspect 模块中)

ismethoddescriptor() (在 inspect 模块中)

ismodule() (在 inspect 模块中)

ismount() (在 os.path 模块中)

isnan() (在 cmath 模块中)

(在 math 模块中)

ISNONTERMINAL() (在 token 模块中)

isnumeric() (str 方法)

isocalendar() (datetime.date 方法)

(datetime.datetime 方法)

isoformat() (datetime.date 方法)

(datetime.datetime 方法)

(datetime.time 方法)

isolation_level (sqlite3.Connection 属性)

isoweekday() (datetime.date 方法)

(datetime.datetime 方法)

isprint() (在 curses.ascii 模块中)

isprintable() (str 方法)

ispunct() (在 curses.ascii 模块中)

isreadable() (pprint.PrettyPrinter 方法)

(在 pprint 模块中)

isrecursive() (pprint.PrettyPrinter 方法)

(在 pprint 模块中)

isreg() (tarfile.TarInfo 方法)

isReservedKey() (http.cookies.Morsel 方法)

isroutine() (在 inspect 模块中)

isSameNode() (xml.dom.Node 方法)

isspace() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 curses.ascii 模块中)

isstdin() (在 fileinput 模块中)

issubclass() (內置函数)

issubset() (frozenset 方法)

issuite() (parser.ST 方法)

(在 parser 模块中)

issuperset() (frozenset 方法)

issym() (tarfile.TarInfo 方法)

ISTERMINAL() (在 token 模块中)

istitle() (bytearray 方法)

(bytes 方法)

(str 方法)

istraceback() (在 inspect 模块中)

isub() (在 operator 模块中)

isupper() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 curses.ascii 模块中)

isvisible() (在 turtle 模块中)

isxdigit() (在 curses.ascii 模块中)

item

sequence

string

item selection

item() (tkinter.ttk.Treeview 方法)

(xml.dom.NamedNodeMap 方法)

(xml.dom.NodeList 方法)

itemgetter() (在 operator 模块中)

items() (configparser.ConfigParser 方法)

(dict 方法)

(email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(types.MappingProxyType 方法)

(xml.etree.ElementTree.Element 方法)

itemsize (array.array 属性)

(memoryview 属性)

ItemsView (collections.abc 中的类)

(typing 中的类)

iter() (xml.etree.ElementTree.Element 方法)

(xml.etree.ElementTree.ElementTree 方法)

(內置函数)

iter_attachments() (email.message.EmailMessage 方法)

iter_child_nodes() (在 ast 模块中)

iter_fields() (在 ast 模块中)

iter_importers() (在 pkgutil 模块中)

iter_modules() (在 pkgutil 模块中)

iter_parts() (email.message.EmailMessage 方法)

iter_unpack() (struct.Struct 方法)

(在 struct 模块中)

iterable

unpacking

Iterable (collections.abc 中的类)

(typing 中的类)

iterable -- 可迭代对象

Iterator (collections.abc 中的类)

(typing 中的类)

iterator -- 迭代器

iterator protocol

iterdecode() (在 codecs 模块中)

iterdir() (pathlib.Path 方法)

iterdump() (sqlite3.Connection 方法)

iterencode() (json.JSONEncoder 方法)

(在 codecs 模块中)

iterfind() (xml.etree.ElementTree.Element 方法)

(xml.etree.ElementTree.ElementTree 方法)

iteritems() (mailbox.Mailbox 方法)

iterkeys() (mailbox.Mailbox 方法)

itermonthdates() (calendar.Calendar 方法)

itermonthdays() (calendar.Calendar 方法)

itermonthdays2() (calendar.Calendar 方法)

iterparse() (在 xml.etree.ElementTree 模块中)

itertext() (xml.etree.ElementTree.Element 方法)

itertools (2to3 fixer)

(模块)

itertools_imports (2to3 fixer)

itervalues() (mailbox.Mailbox 方法)

iterweekdays() (calendar.Calendar 方法)

ITIMER_PROF() (在 signal 模块中)

ITIMER_REAL() (在 signal 模块中)

ITIMER_VIRTUAL() (在 signal 模块中)

ItimerError

itruediv() (在 operator 模块中)

ixor() (在 operator 模块中)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – J

j

in numeric literal

Jansen, Jack

Java

language

java_ver() (在 platform 模块中)

join() (asyncio.Queue 方法)

(bytearray 方法)

(bytes 方法)

(multiprocessing.JoinableQueue 方法)

(multiprocessing.pool.Pool 方法)

(multiprocessing.Process 方法)

(queue.Queue 方法)

(str 方法)

(threading.Thread 方法)

(在 os.path 模块中)

join_thread() (multiprocessing.Queue 方法)

JoinableQueue (multiprocessing 中的类)

joinpath() (pathlib.PurePath 方法)

js_output() (http.cookies.BaseCookie 方法)

(http.cookies.Morsel 方法)

json

模块

json (模块)

json.tool (模块)

json.tool command line option

--help

--sort-keys

-h

infile

outfile

JSONDecodeError

JSONDecoder (json 中的类)

JSONEncoder (json 中的类)

jump (pdb command)

JUMP_ABSOLUTE (opcode)

JUMP_FORWARD (opcode)

JUMP_IF_FALSE_OR_POP (opcode)

JUMP_IF_TRUE_OR_POP (opcode)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – K

kbhit() (在 msvcrt 模块中)

KDEDIR

kevent() (在 select 模块中)

key

(http.cookies.Morsel 属性)

key function -- 键函数

key/datum pair

KEY_ALL_ACCESS() (在 winreg 模块中)

KEY_CREATE_LINK() (在 winreg 模块中)

KEY_CREATE_SUB_KEY() (在 winreg 模块中)

KEY_ENUMERATE_SUB_KEYS() (在 winreg 模块中)

KEY_EXECUTE() (在 winreg 模块中)

KEY_NOTIFY() (在 winreg 模块中)

KEY_QUERY_VALUE() (在 winreg 模块中)

KEY_READ() (在 winreg 模块中)

KEY_SET_VALUE() (在 winreg 模块中)

KEY_WOW64_32KEY() (在 winreg 模块中)

KEY_WOW64_64KEY() (在 winreg 模块中)

KEY_WRITE() (在 winreg 模块中)

KeyboardInterrupt

(built-in exception), [1]

KeyError

keyname() (在 curses 模块中)

keypad() (curses.window 方法)

keyrefs() (weakref.WeakKeyDictionary 方法)

keys() (dict 方法)

(email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(sqlite3.Row 方法)

(types.MappingProxyType 方法)

(xml.etree.ElementTree.Element 方法)

KeysView (collections.abc 中的类)

(typing 中的类)

keyword

(模块)

keyword argument -- 关键字参数

keywords (functools.partial 属性)

kill() (asyncio.asyncio.subprocess.Process 方法)

(asyncio.BaseSubprocessTransport 方法)

(subprocess.Popen 方法)

(在 os 模块中)

killchar() (在 curses 模块中)

killpg() (在 os 模块中)

kind (inspect.Parameter 属性)

knownfiles() (在 mimetypes 模块中)

kqueue() (在 select 模块中)

KqueueSelector (selectors 中的类)

kwargs (inspect.BoundArguments 属性)

kwlist() (在 keyword 模块中)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – L

L() (在 re 模块中)

LabelEntry (tkinter.tix 中的类)

LabelFrame (tkinter.tix 中的类)

lambda

expression, [1]

form

LambdaType() (在 types 模块中)

LANG, [1], [2], [3], [4]

LANGUAGE, [1]

language

C, [1], [2], [3], [4], [5]

Java

large files

LargeZipFile

last() (nntplib.NNTP 方法)

last_accepted (multiprocessing.connection.Listener 属性)

last_traceback (in module sys)

last_traceback() (在 sys 模块中)

last_type() (在 sys 模块中)

last_value() (在 sys 模块中)

lastChild (xml.dom.Node 属性)

lastcmd (cmd.Cmd 属性)

lastgroup (re.match 属性)

lastindex (re.match 属性)

lastResort() (在 logging 模块中)

lastrowid (sqlite3.Cursor 属性)

layout() (tkinter.ttk.Style 方法)

lazycache() (在 linecache 模块中)

LazyLoader (importlib.util 中的类)

LBRACE() (在 token 模块中)

LBYL

LC_ALL, [1]

LC_ALL() (在 locale 模块中)

LC_COLLATE() (在 locale 模块中)

LC_CTYPE() (在 locale 模块中)

LC_MESSAGES, [1]

LC_MESSAGES() (在 locale 模块中)

LC_MONETARY() (在 locale 模块中)

LC_NUMERIC() (在 locale 模块中)

LC_TIME() (在 locale 模块中)

lchflags() (在 os 模块中)

lchmod() (pathlib.Path 方法)

(在 os 模块中)

lchown() (在 os 模块中)

LDCXXSHARED

ldexp() (在 math 模块中)

LDFLAGS

ldgettext() (在 gettext 模块中)

ldngettext() (在 gettext 模块中)

le() (在 operator 模块中)

leading whitespace

leapdays() (在 calendar 模块中)

leaveok() (curses.window 方法)

left (filecmp.dircmp 属性)

left() (在 turtle 模块中)

left_list (filecmp.dircmp 属性)

left_only (filecmp.dircmp 属性)

LEFTSHIFT() (在 token 模块中)

LEFTSHIFTEQUAL() (在 token 模块中)

len

內置函数, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11]

len() (內置函数)

length (xml.dom.NamedNodeMap 属性)

(xml.dom.NodeList 属性)

length_hint() (在 operator 模块中)

LESS() (在 token 模块中)

LESSEQUAL() (在 token 模块中)

lexical analysis

lexical definitions

lexists() (在 os.path 模块中)

lgamma() (在 math 模块中)

lgettext() (gettext.GNUTranslations 方法)

(gettext.NullTranslations 方法)

(在 gettext 模块中)

lib2to3 (模块)

libc_ver() (在 platform 模块中)

library (ssl.SSLError 属性)

library() (在 dbm.ndbm 模块中)

library_dir_option() (distutils.ccompiler.CCompiler 方法)

library_filename() (distutils.ccompiler.CCompiler 方法)

library_option() (distutils.ccompiler.CCompiler 方法)

LibraryLoader (ctypes 中的类)

license (內置变量)

LifoQueue (asyncio 中的类)

(queue 中的类)

light-weight processes

limit_denominator() (fractions.Fraction 方法)

LimitOverrunError

lin2adpcm() (在 audioop 模块中)

lin2alaw() (在 audioop 模块中)

lin2lin() (在 audioop 模块中)

lin2ulaw() (在 audioop 模块中)

line (C 函数)

line continuation

line joining, [1]

line structure

line() (msilib.Dialog 方法)

line-buffered I/O

line_buffering (io.TextIOWrapper 属性)

line_num (csv.csvreader 属性)

linecache (模块)

lineno (ast.AST 属性)

(doctest.DocTest 属性)

(doctest.Example 属性)

(json.JSONDecodeError 属性)

(pyclbr.Class 属性)

(pyclbr.Function 属性)

(re.error 属性)

(shlex.shlex 属性)

(traceback.TracebackException 属性)

(tracemalloc.Filter 属性)

(tracemalloc.Frame 属性)

(xml.parsers.expat.ExpatError 属性)

lineno() (在 fileinput 模块中)

LINES, [1], [2], [3], [4]

lines (os.terminal_size 属性)

linesep (email.policy.Policy 属性)

linesep() (在 os 模块中)

lineterminator (csv.Dialect 属性)

LineTooLong

link() (distutils.ccompiler.CCompiler 方法)

(在 os 模块中)

link_executable() (distutils.ccompiler.CCompiler 方法)

link_shared_lib() (distutils.ccompiler.CCompiler 方法)

link_shared_object() (distutils.ccompiler.CCompiler 方法)

linkname (tarfile.TarInfo 属性)

linux_distribution() (在 platform 模块中)

list

assignment, target

comprehensions

deletion target

display

empty

expression, [1], [2]

target, [1], [2]

type, operations on

对象, [1], [2], [3], [4], [5], [6], [7], [8]

list (pdb command)

List (typing 中的类)

list (內置类)

list -- 列表

list comprehension -- 列表推导式

list() (imaplib.IMAP4 方法)

(multiprocessing.managers.SyncManager 方法)

(nntplib.NNTP 方法)

(poplib.POP3 方法)

(tarfile.TarFile 方法)

LIST_APPEND (opcode)

list_dialects() (在 csv 模块中)

list_folders() (mailbox.Maildir 方法)

(mailbox.MH 方法)

listdir() (在 os 模块中)

listen() (asyncore.dispatcher 方法)

(socket.socket 方法)

(在 logging.config 模块中)

(在 turtle 模块中)

Listener (multiprocessing.connection 中的类)

listMethods() (xmlrpc.client.ServerProxy.system 方法)

ListNoteBook (tkinter.tix 中的类)

listxattr() (在 os 模块中)

literal, [1]

literal_eval() (在 ast 模块中)

literals

binary

complex number

floating point

hexadecimal

integer

numeric

octal

LittleEndianStructure (ctypes 中的类)

ljust() (bytearray 方法)

(bytes 方法)

(str 方法)

LK_LOCK() (在 msvcrt 模块中)

LK_NBLCK() (在 msvcrt 模块中)

LK_NBRLCK() (在 msvcrt 模块中)

LK_RLCK() (在 msvcrt 模块中)

LK_UNLCK() (在 msvcrt 模块中)

ll (pdb command)

LMTP (smtplib 中的类)

ln() (decimal.Context 方法)

(decimal.Decimal 方法)

LNAME

lngettext() (gettext.GNUTranslations 方法)

(gettext.NullTranslations 方法)

(在 gettext 模块中)

load() (http.cookiejar.FileCookieJar 方法)

(http.cookies.BaseCookie 方法)

(pickle.Unpickler 方法)

(tracemalloc.Snapshot 类方法)

(在 json 模块中)

(在 marshal 模块中)

(在 pickle 模块中)

(在 plistlib 模块中)

LOAD_ATTR (opcode)

LOAD_BUILD_CLASS (opcode)

load_cert_chain() (ssl.SSLContext 方法)

LOAD_CLASSDEREF (opcode)

LOAD_CLOSURE (opcode)

LOAD_CONST (opcode)

load_default_certs() (ssl.SSLContext 方法)

LOAD_DEREF (opcode)

load_dh_params() (ssl.SSLContext 方法)

load_extension() (sqlite3.Connection 方法)

LOAD_FAST (opcode)

LOAD_GLOBAL (opcode)

load_module() (importlib.abc.FileLoader 方法)

(importlib.abc.InspectLoader 方法)

(importlib.abc.Loader 方法)

(importlib.abc.SourceLoader 方法)

(importlib.machinery.SourceFileLoader 方法)

(importlib.machinery.SourcelessFileLoader 方法)

(zipimport.zipimporter 方法)

(在 imp 模块中)

LOAD_NAME (opcode)

load_package_tests() (在 test.support 模块中)

load_verify_locations() (ssl.SSLContext 方法)

loader

Loader (importlib.abc 中的类)

loader (importlib.machinery.ModuleSpec 属性)

loader -- 加载器

loader_state (importlib.machinery.ModuleSpec 属性)

LoadError

LoadKey() (在 winreg 模块中)

LoadLibrary() (ctypes.LibraryLoader 方法)

loads() (在 json 模块中)

(在 marshal 模块中)

(在 pickle 模块中)

(在 plistlib 模块中)

(在 xmlrpc.client 模块中)

loadTestsFromModule() (unittest.TestLoader 方法)

loadTestsFromName() (unittest.TestLoader 方法)

loadTestsFromNames() (unittest.TestLoader 方法)

loadTestsFromTestCase() (unittest.TestLoader 方法)

local (threading 中的类)

localcontext() (在 decimal 模块中)

locale (模块)

LOCALE() (在 re 模块中)

localeconv() (在 locale 模块中)

LocaleHTMLCalendar (calendar 中的类)

LocaleTextCalendar (calendar 中的类)

localName (xml.dom.Attr 属性)

(xml.dom.Node 属性)

locals() (內置函数)

localtime() (在 email.utils 模块中)

(在 time 模块中)

Locator (xml.sax.xmlreader 中的类)

Lock (asyncio 中的类)

(multiprocessing 中的类)

(threading 中的类)

lock() (mailbox.Babyl 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(mailbox.mbox 方法)

(mailbox.MH 方法)

(mailbox.MMDF 方法)

Lock() (multiprocessing.managers.SyncManager 方法)

lock, interpreter

lock_held() (在 imp 模块中)

locked() (_thread.lock 方法)

(asyncio.Condition 方法)

(asyncio.Lock 方法)

(asyncio.Semaphore 方法)

lockf() (在 fcntl 模块中)

(在 os 模块中)

locking() (在 msvcrt 模块中)

LockType() (在 _thread 模块中)

log() (logging.Logger 方法)

(在 cmath 模块中)

(在 logging 模块中)

(在 math 模块中)

log10() (decimal.Context 方法)

(decimal.Decimal 方法)

(在 cmath 模块中)

(在 math 模块中)

log1p() (在 math 模块中)

log2() (在 math 模块中)

log_date_time_string() (http.server.BaseHTTPRequestHandler 方法)

log_error() (http.server.BaseHTTPRequestHandler 方法)

log_exception() (wsgiref.handlers.BaseHandler 方法)

log_message() (http.server.BaseHTTPRequestHandler 方法)

log_request() (http.server.BaseHTTPRequestHandler 方法)

log_to_stderr() (在 multiprocessing 模块中)

logb() (decimal.Context 方法)

(decimal.Decimal 方法)

Logger (logging 中的类)

LoggerAdapter (logging 中的类)

logging

Errors

logging (模块)

logging.config (模块)

logging.handlers (模块)

logical line

logical_and() (decimal.Context 方法)

(decimal.Decimal 方法)

logical_invert() (decimal.Context 方法)

(decimal.Decimal 方法)

logical_or() (decimal.Context 方法)

(decimal.Decimal 方法)

logical_xor() (decimal.Context 方法)

(decimal.Decimal 方法)

login() (ftplib.FTP 方法)

(imaplib.IMAP4 方法)

(nntplib.NNTP 方法)

(smtplib.SMTP 方法)

login_cram_md5() (imaplib.IMAP4 方法)

LOGNAME, [1]

lognormvariate() (在 random 模块中)

logout() (imaplib.IMAP4 方法)

LogRecord (logging 中的类)

long (2to3 fixer)

long integer

对象

LONG_MAX

longMessage (unittest.TestCase 属性)

longname() (在 curses 模块中)

lookup() (symtable.SymbolTable 方法)

(tkinter.ttk.Style 方法)

(在 codecs 模块中)

(在 unicodedata 模块中)

lookup_error() (在 codecs 模块中)

LookupError

loop

over mutable sequence

statement, [1], [2], [3]

loop control

target

loop() (在 asyncore 模块中)

lower() (bytearray 方法)

(bytes 方法)

(str 方法)

LPAR() (在 token 模块中)

lru_cache() (在 functools 模块中)

lseek() (在 os 模块中)

lshift() (在 operator 模块中)

LSQB() (在 token 模块中)

lstat() (pathlib.Path 方法)

(在 os 模块中)

lstrip() (bytearray 方法)

(bytes 方法)

(str 方法)

lsub() (imaplib.IMAP4 方法)

lt() (在 operator 模块中)

(在 turtle 模块中)

LWPCookieJar (http.cookiejar 中的类)

lzma (模块)

LZMACompressor (lzma 中的类)

LZMADecompressor (lzma 中的类)

LZMAError

LZMAFile (lzma 中的类)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – M

M() (在 re 模块中)

mac_ver() (在 platform 模块中)

machine() (在 platform 模块中)

macpath (模块)

macros (netrc.netrc 属性)

MAGIC_NUMBER() (在 importlib.util 模块中)

MagicMock (unittest.mock 中的类)

Mailbox (mailbox 中的类)

mailbox (模块)

mailcap (模块)

Maildir (mailbox 中的类)

MaildirMessage (mailbox 中的类)

mailfrom (smtpd.SMTPChannel 属性)

MailmanProxy (smtpd 中的类)

main(), [1], [2]

(在 py_compile 模块中)

(在 site 模块中)

(在 unittest 模块中)

main_thread() (在 threading 模块中)

mainloop() (在 turtle 模块中)

maintype (email.headerregistry.ContentTypeHeader 属性)

major (email.headerregistry.MIMEVersionHeader 属性)

major() (在 os 模块中)

make_alternative() (email.message.EmailMessage 方法)

make_archive() (在 distutils.archive_util 模块中)

(在 shutil 模块中)

make_bad_fd() (在 test.support 模块中)

make_cookies() (http.cookiejar.CookieJar 方法)

make_file() (difflib.HtmlDiff 方法)

MAKE_FUNCTION (opcode)

make_header() (在 email.header 模块中)

make_mixed() (email.message.EmailMessage 方法)

make_msgid() (在 email.utils 模块中)

make_parser() (在 xml.sax 模块中)

make_related() (email.message.EmailMessage 方法)

make_server() (在 wsgiref.simple_server 模块中)

make_table() (difflib.HtmlDiff 方法)

make_tarball() (在 distutils.archive_util 模块中)

make_zipfile() (在 distutils.archive_util 模块中)

makedev() (在 os 模块中)

makedirs() (在 os 模块中)

makeelement() (xml.etree.ElementTree.Element 方法)

makefile() (socket method)

(socket.socket 方法)

makeLogRecord() (在 logging 模块中)

makePickle() (logging.handlers.SocketHandler 方法)

makeRecord() (logging.Logger 方法)

makeSocket() (logging.handlers.DatagramHandler 方法)

(logging.handlers.SocketHandler 方法)

maketrans() (bytearray 静态方法)

(bytes 静态方法)

(str 静态方法)

malloc()

mangle_from_ (email.policy.Compat32 属性)

(email.policy.Policy 属性)

mangling

name, [1]

map (2to3 fixer)

map() (concurrent.futures.Executor 方法)

(multiprocessing.pool.Pool 方法)

(tkinter.ttk.Style 方法)

(內置函数)

MAP_ADD (opcode)

map_async() (multiprocessing.pool.Pool 方法)

map_table_b2() (在 stringprep 模块中)

map_table_b3() (在 stringprep 模块中)

map_to_type() (email.headerregistry.HeaderRegistry 方法)

mapLogRecord() (logging.handlers.HTTPHandler 方法)

mapping

types, operations on

对象, [1], [2], [3], [4], [5]

Mapping (collections.abc 中的类)

(typing 中的类)

mapping -- 映射

mapping() (msilib.Control 方法)

MappingProxyType (types 中的类)

MappingView (collections.abc 中的类)

(typing 中的类)

mapPriority() (logging.handlers.SysLogHandler 方法)

maps (collections.ChainMap 属性)

maps() (在 nis 模块中)

marshal (模块)

marshalling

objects

masking

operations

Match (typing 中的类)

match() (pathlib.PurePath 方法)

(re.regex 方法)

(在 nis 模块中)

(在 re 模块中)

match_hostname() (在 ssl 模块中)

math

模块, [1]

math (模块)

matmul() (在 operator 模块中)

matrix multiplication

max

內置函数

max (datetime.date 属性)

(datetime.datetime 属性)

(datetime.time 属性)

(datetime.timedelta 属性)

max() (decimal.Context 方法)

(decimal.Decimal 方法)

(內置函数)

(在 audioop 模块中)

max_count (email.headerregistry.BaseHeader 属性)

MAX_EMAX() (在 decimal 模块中)

MAX_INTERPOLATION_DEPTH() (在 configparser 模块中)

max_line_length (email.policy.Policy 属性)

max_lines (textwrap.TextWrapper 属性)

max_mag() (decimal.Context 方法)

(decimal.Decimal 方法)

MAX_PREC() (在 decimal 模块中)

max_prefixlen (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

maxarray (reprlib.Repr 属性)

maxdeque (reprlib.Repr 属性)

maxdict (reprlib.Repr 属性)

maxDiff (unittest.TestCase 属性)

maxfrozenset (reprlib.Repr 属性)

maxlen (collections.deque 属性)

maxlevel (reprlib.Repr 属性)

maxlist (reprlib.Repr 属性)

maxlong (reprlib.Repr 属性)

maxother (reprlib.Repr 属性)

maxpp() (在 audioop 模块中)

maxset (reprlib.Repr 属性)

maxsize (asyncio.Queue 属性)

maxsize() (在 sys 模块中)

maxstring (reprlib.Repr 属性)

maxtuple (reprlib.Repr 属性)

maxunicode() (在 sys 模块中)

MAXYEAR() (在 datetime 模块中)

MB_ICONASTERISK() (在 winsound 模块中)

MB_ICONEXCLAMATION() (在 winsound 模块中)

MB_ICONHAND() (在 winsound 模块中)

MB_ICONQUESTION() (在 winsound 模块中)

MB_OK() (在 winsound 模块中)

mbox (mailbox 中的类)

mboxMessage (mailbox 中的类)

mean() (在 statistics 模块中)

median() (在 statistics 模块中)

median_grouped() (在 statistics 模块中)

median_high() (在 statistics 模块中)

median_low() (在 statistics 模块中)

MemberDescriptorType() (在 types 模块中)

membership

test

memmove() (在 ctypes 模块中)

MemoryBIO (ssl 中的类)

MemoryError

MemoryHandler (logging.handlers 中的类)

memoryview

对象, [1]

memoryview (內置类)

memset() (在 ctypes 模块中)

merge() (在 heapq 模块中)

Message (email.message 中的类)

(mailbox 中的类)

message digest, MD5

message_factory (email.policy.Policy 属性)

message_from_binary_file() (在 email 模块中)

message_from_bytes() (在 email 模块中)

message_from_file() (在 email 模块中)

message_from_string() (在 email 模块中)

MessageBeep() (在 winsound 模块中)

MessageClass (http.server.BaseHTTPRequestHandler 属性)

MessageError

MessageParseError

messages() (在 xml.parsers.expat.errors 模块中)

meta

hooks

meta hooks

meta path finder -- 元路径查找器

meta() (在 curses 模块中)

meta_path() (在 sys 模块中)

metaclass

(2to3 fixer)

metaclass -- 元类

metaclass hint

MetaPathFinder (importlib.abc 中的类)

metavar (optparse.Option 属性)

MetavarTypeHelpFormatter (argparse 中的类)

Meter (tkinter.tix 中的类)

METH_CLASS (內置变量)

METH_COEXIST (內置变量)

METH_KEYWORDS (內置变量)

METH_NOARGS (內置变量)

METH_O (內置变量)

METH_STATIC (內置变量)

METH_VARARGS (內置变量)

method

built-in

call

user-defined

对象, [1], [2], [3], [4], [5]

method (urllib.request.Request 属性)

method resolution order -- 方法解析顺序

method 方法

method_calls (unittest.mock.Mock 属性)

METHOD_CRYPT() (在 crypt 模块中)

METHOD_MD5() (在 crypt 模块中)

METHOD_SHA256() (在 crypt 模块中)

METHOD_SHA512() (在 crypt 模块中)

methodattrs (2to3 fixer)

methodcaller() (在 operator 模块中)

methodHelp() (xmlrpc.client.ServerProxy.system 方法)

methods

bytearray

bytes

string

methods (pyclbr.Class 属性)

methods() (在 crypt 模块中)

methodSignature() (xmlrpc.client.ServerProxy.system 方法)

MethodType (in module types), [1]

MethodType() (在 types 模块中)

MH (mailbox 中的类)

MHMessage (mailbox 中的类)

microsecond (datetime.datetime 属性)

(datetime.time 属性)

MIME

base64 encoding

content type

headers, [1]

quoted-printable encoding

MIMEApplication (email.mime.application 中的类)

MIMEAudio (email.mime.audio 中的类)

MIMEBase (email.mime.base 中的类)

MIMEImage (email.mime.image 中的类)

MIMEMessage (email.mime.message 中的类)

MIMEMultipart (email.mime.multipart 中的类)

MIMENonMultipart (email.mime.nonmultipart 中的类)

MIMEPart (email.message 中的类)

MIMEText (email.mime.text 中的类)

MimeTypes (mimetypes 中的类)

mimetypes (模块)

MIMEVersionHeader (email.headerregistry 中的类)

min

內置函数

min (datetime.date 属性)

(datetime.datetime 属性)

(datetime.time 属性)

(datetime.timedelta 属性)

min() (decimal.Context 方法)

(decimal.Decimal 方法)

(內置函数)

MIN_EMIN() (在 decimal 模块中)

MIN_ETINY() (在 decimal 模块中)

min_mag() (decimal.Context 方法)

(decimal.Decimal 方法)

MINEQUAL() (在 token 模块中)

minmax() (在 audioop 模块中)

minor (email.headerregistry.MIMEVersionHeader 属性)

minor() (在 os 模块中)

minus

minus() (decimal.Context 方法)

MINUS() (在 token 模块中)

minute (datetime.datetime 属性)

(datetime.time 属性)

MINYEAR() (在 datetime 模块中)

mirrored() (在 unicodedata 模块中)

misc_header (cmd.Cmd 属性)

MissingSectionHeaderError

MIXERDEV

mkd() (ftplib.FTP 方法)

mkdir() (pathlib.Path 方法)

(在 os 模块中)

mkdtemp() (在 tempfile 模块中)

mkfifo() (在 os 模块中)

mknod() (在 os 模块中)

mkpath() (distutils.ccompiler.CCompiler 方法)

(在 distutils.dir_util 模块中)

mksalt() (在 crypt 模块中)

mkstemp() (在 tempfile 模块中)

mktemp() (在 tempfile 模块中)

mktime() (在 time 模块中)

mktime_tz() (在 email.utils 模块中)

mlsd() (ftplib.FTP 方法)

mmap (mmap 中的类)

(模块)

MMDF (mailbox 中的类)

MMDFMessage (mailbox 中的类)

Mock (unittest.mock 中的类)

mock_add_spec() (unittest.mock.Mock 方法)

mock_calls (unittest.mock.Mock 属性)

mock_open() (在 unittest.mock 模块中)

mod() (在 operator 模块中)

mode (io.FileIO 属性)

(ossaudiodev.oss_audio_device 属性)

(tarfile.TarInfo 属性)

mode() (在 statistics 模块中)

(在 turtle 模块中)

modes

file

modf() (在 math 模块中)

modified() (urllib.robotparser.RobotFileParser 方法)

Modify() (msilib.View 方法)

modify() (select.devpoll 方法)

(select.epoll 方法)

(select.poll 方法)

(selectors.BaseSelector 方法)

module

extension

importing

namespace

search path, [1], [2], [3], [4], [5], [6], [7]

对象, [1], [2]

module (pyclbr.Class 属性)

(pyclbr.Function 属性)

module spec

module spec -- 模块规格

module 模块

module_for_loader() (在 importlib.util 模块中)

module_from_spec() (在 importlib.util 模块中)

module_repr() (importlib.abc.Loader 方法)

ModuleFinder (modulefinder 中的类)

modulefinder (模块)

ModuleInfo (pkgutil 中的类)

ModuleNotFoundError

modules (in module sys), [1]

(modulefinder.ModuleFinder 属性)

modules() (在 sys 模块中)

ModuleSpec (importlib.machinery 中的类)

ModuleType (in module types)

(types 中的类)

modulo

monotonic() (在 time 模块中)

month (datetime.date 属性)

(datetime.datetime 属性)

month() (在 calendar 模块中)

month_abbr() (在 calendar 模块中)

month_name() (在 calendar 模块中)

monthcalendar() (在 calendar 模块中)

monthdatescalendar() (calendar.Calendar 方法)

monthdays2calendar() (calendar.Calendar 方法)

monthdayscalendar() (calendar.Calendar 方法)

monthrange() (在 calendar 模块中)

Morsel (http.cookies 中的类)

most_common() (collections.Counter 方法)

mouseinterval() (在 curses 模块中)

mousemask() (在 curses 模块中)

move() (curses.panel.Panel 方法)

(curses.window 方法)

(mmap.mmap 方法)

(tkinter.ttk.Treeview 方法)

(在 shutil 模块中)

move_file() (distutils.ccompiler.CCompiler 方法)

(在 distutils.file_util 模块中)

move_to_end() (collections.OrderedDict 方法)

MozillaCookieJar (http.cookiejar 中的类)

MRO

mro() (class 方法)

msg (http.client.HTTPResponse 属性)

(json.JSONDecodeError 属性)

(re.error 属性)

(traceback.TracebackException 属性)

msg() (telnetlib.Telnet 方法)

msi

msilib (模块)

msvcrt (模块)

mt_interact() (telnetlib.Telnet 方法)

mtime (gzip.GzipFile 属性)

(tarfile.TarInfo 属性)

mtime() (urllib.robotparser.RobotFileParser 方法)

mul() (在 audioop 模块中)

(在 operator 模块中)

MultiCall (xmlrpc.client 中的类)

MULTILINE() (在 re 模块中)

MultipartConversionError

multiplication

multiply() (decimal.Context 方法)

multiprocessing (模块)

multiprocessing.connection (模块)

multiprocessing.dummy (模块)

multiprocessing.Manager() (在 multiprocessing.sharedctypes 模块中)

multiprocessing.managers (模块)

multiprocessing.pool (模块)

multiprocessing.sharedctypes (模块)

mutable

sequence types

对象, [1], [2]

mutable -- 可变

mutable object

mutable sequence

loop over

对象

MutableMapping (collections.abc 中的类)

(typing 中的类)

MutableSequence (collections.abc 中的类)

(typing 中的类)

MutableSet (collections.abc 中的类)

(typing 中的类)

mvderwin() (curses.window 方法)

mvwin() (curses.window 方法)

myrights() (imaplib.IMAP4 方法)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – N

N_TOKENS() (在 token 模块中)

n_waiting (threading.Barrier 属性)

name, [1], [2]

binding, [1], [2], [3], [4], [5], [6]

binding, global

class

function, [1]

mangling, [1]

rebinding

unbinding

name (codecs.CodecInfo 属性)

(doctest.DocTest 属性)

(email.headerregistry.BaseHeader 属性)

(hashlib.hash 属性)

(hmac.HMAC 属性)

(http.cookiejar.Cookie 属性)

(importlib.abc.FileLoader 属性)

(importlib.machinery.ExtensionFileLoader 属性)

(importlib.machinery.ModuleSpec 属性)

(importlib.machinery.SourceFileLoader 属性)

(importlib.machinery.SourcelessFileLoader 属性)

(inspect.Parameter 属性)

(io.FileIO 属性)

(multiprocessing.Process 属性)

(os.DirEntry 属性)

(ossaudiodev.oss_audio_device 属性)

(pyclbr.Class 属性)

(pyclbr.Function 属性)

(tarfile.TarInfo 属性)

(threading.Thread 属性)

(xml.dom.Attr 属性)

(xml.dom.DocumentType 属性)

name() (在 os 模块中)

NAME() (在 token 模块中)

name() (在 unicodedata 模块中)

name2codepoint() (在 html.entities 模块中)

named tuple -- 具名元组

NamedTemporaryFile() (在 tempfile 模块中)

NamedTuple (typing 中的类)

namedtuple() (在 collections 模块中)

NameError

例外

NameError (built-in exception)

namelist() (zipfile.ZipFile 方法)

nameprep() (在 encodings.idna 模块中)

namer (logging.handlers.BaseRotatingHandler 属性)

namereplace_errors() (在 codecs 模块中)

names

private

namespace

global

module

package

Namespace (argparse 中的类)

(multiprocessing.managers 中的类)

namespace -- 命名空间

namespace package -- 命名空间包

namespace() (imaplib.IMAP4 方法)

Namespace() (multiprocessing.managers.SyncManager 方法)

NAMESPACE_DNS() (在 uuid 模块中)

NAMESPACE_OID() (在 uuid 模块中)

NAMESPACE_URL() (在 uuid 模块中)

NAMESPACE_X500() (在 uuid 模块中)

NamespaceErr

namespaceURI (xml.dom.Node 属性)

NaN

nan() (在 cmath 模块中)

(在 math 模块中)

nanj() (在 cmath 模块中)

NannyNag

napms() (在 curses 模块中)

nargs (optparse.Option 属性)

nbytes (memoryview 属性)

ndiff() (在 difflib 模块中)

ndim (memoryview 属性)

ne (2to3 fixer)

ne() (在 operator 模块中)

needs_input (bz2.BZ2Decompressor 属性)

(lzma.LZMADecompressor 属性)

neg() (在 operator 模块中)

negation

nested scope -- 嵌套作用域

netmask (ipaddress.IPv4Network 属性)

(ipaddress.IPv6Network 属性)

NetmaskValueError

netrc (netrc 中的类)

(模块)

NetrcParseError

netscape (http.cookiejar.CookiePolicy 属性)

network (ipaddress.IPv4Interface 属性)

(ipaddress.IPv6Interface 属性)

Network News Transfer Protocol

network_address (ipaddress.IPv4Network 属性)

(ipaddress.IPv6Network 属性)

new() (在 hashlib 模块中)

(在 hmac 模块中)

new-style class -- 新式类

new_alignment() (formatter.writer 方法)

new_child() (collections.ChainMap 方法)

new_class() (在 types 模块中)

new_compiler() (在 distutils.ccompiler 模块中)

new_event_loop() (asyncio.AbstractEventLoopPolicy 方法)

(在 asyncio 模块中)

new_font() (formatter.writer 方法)

new_margin() (formatter.writer 方法)

new_module() (在 imp 模块中)

new_panel() (在 curses.panel 模块中)

new_spacing() (formatter.writer 方法)

new_styles() (formatter.writer 方法)

newer() (在 distutils.dep_util 模块中)

newer_group() (在 distutils.dep_util 模块中)

newer_pairwise() (在 distutils.dep_util 模块中)

newgroups() (nntplib.NNTP 方法)

NEWLINE token, [1]

NEWLINE() (在 token 模块中)

newlines (io.TextIOBase 属性)

newnews() (nntplib.NNTP 方法)

newpad() (在 curses 模块中)

NewType() (在 typing 模块中)

newwin() (在 curses 模块中)

next (2to3 fixer)

(pdb command)

next() (nntplib.NNTP 方法)

(tarfile.TarFile 方法)

(tkinter.ttk.Treeview 方法)

(內置函数)

next_minus() (decimal.Context 方法)

(decimal.Decimal 方法)

next_plus() (decimal.Context 方法)

(decimal.Decimal 方法)

next_toward() (decimal.Context 方法)

(decimal.Decimal 方法)

nextfile() (在 fileinput 模块中)

nextkey() (dbm.gnu.gdbm 方法)

nextSibling (xml.dom.Node 属性)

ngettext() (gettext.GNUTranslations 方法)

(gettext.NullTranslations 方法)

(在 gettext 模块中)

nice() (在 os 模块中)

nis (模块)

nl() (在 curses 模块中)

NL() (在 tokenize 模块中)

nl_langinfo() (在 locale 模块中)

nlargest() (在 heapq 模块中)

nlst() (ftplib.FTP 方法)

NNTP

protocol

NNTP (nntplib 中的类)

nntp_implementation (nntplib.NNTP 属性)

NNTP_SSL (nntplib 中的类)

nntp_version (nntplib.NNTP 属性)

NNTPDataError

NNTPError

nntplib (模块)

NNTPPermanentError

NNTPProtocolError

NNTPReplyError

NNTPTemporaryError

no_proxy

no_type_check() (在 typing 模块中)

no_type_check_decorator() (在 typing 模块中)

nocbreak() (在 curses 模块中)

NoDataAllowedErr

node() (在 platform 模块中)

nodelay() (curses.window 方法)

nodeName (xml.dom.Node 属性)

NodeTransformer (ast 中的类)

nodeType (xml.dom.Node 属性)

nodeValue (xml.dom.Node 属性)

NodeVisitor (ast 中的类)

noecho() (在 curses 模块中)

NOEXPR() (在 locale 模块中)

NoModificationAllowedErr

nonblock() (ossaudiodev.oss_audio_device 方法)

NonCallableMagicMock (unittest.mock 中的类)

NonCallableMock (unittest.mock 中的类)

None

对象, [1], [2]

None (Built-in object)

(內置变量)

nonl() (在 curses 模块中)

nonlocal

语句

nonzero (2to3 fixer)

noop() (imaplib.IMAP4 方法)

(poplib.POP3 方法)

NoOptionError

NOP (opcode)

noqiflush() (在 curses 模块中)

noraw() (在 curses 模块中)

NoReturn() (在 typing 模块中)

normalize() (decimal.Context 方法)

(decimal.Decimal 方法)

(xml.dom.Node 方法)

(在 locale 模块中)

(在 unicodedata 模块中)

NORMALIZE_WHITESPACE() (在 doctest 模块中)

normalvariate() (在 random 模块中)

normcase() (在 os.path 模块中)

normpath() (在 os.path 模块中)

NoSectionError

NoSuchMailboxError

not

运算符, [1]

not in

运算符, [1], [2]

not_() (在 operator 模块中)

NotADirectoryError

notation

notationDecl() (xml.sax.handler.DTDHandler 方法)

NotationDeclHandler() (xml.parsers.expat.xmlparser 方法)

notations (xml.dom.DocumentType 属性)

NotConnected

NoteBook (tkinter.tix 中的类)

Notebook (tkinter.ttk 中的类)

NotEmptyError

NOTEQUAL() (在 token 模块中)

NotFoundErr

notify() (asyncio.Condition 方法)

(threading.Condition 方法)

notify_all() (asyncio.Condition 方法)

(threading.Condition 方法)

notimeout() (curses.window 方法)

NotImplemented

对象

NotImplemented (內置变量)

NotImplementedError

NotStandaloneHandler() (xml.parsers.expat.xmlparser 方法)

NotSupportedErr

NotSupportedError

noutrefresh() (curses.window 方法)

now() (datetime.datetime 类方法)

NSIG() (在 signal 模块中)

nsmallest() (在 heapq 模块中)

NT_OFFSET() (在 token 模块中)

NTEventLogHandler (logging.handlers 中的类)

ntohl() (在 socket 模块中)

ntohs() (在 socket 模块中)

ntransfercmd() (ftplib.FTP 方法)

null

operation, [1]

NullFormatter (formatter 中的类)

NullHandler (logging 中的类)

NullImporter (imp 中的类)

NullTranslations (gettext 中的类)

NullWriter (formatter 中的类)

num_addresses (ipaddress.IPv4Network 属性)

(ipaddress.IPv6Network 属性)

number

complex

floating point

Number (numbers 中的类)

NUMBER() (在 token 模块中)

number_class() (decimal.Context 方法)

(decimal.Decimal 方法)

numbers (模块)

numerator (fractions.Fraction 属性)

(numbers.Rational 属性)

numeric

conversions

literals

object

types, operations on

对象, [1], [2], [3]

numeric literal

numeric() (在 unicodedata 模块中)

Numerical Python

numinput() (在 turtle 模块中)

numliterals (2to3 fixer)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – O

O_APPEND() (在 os 模块中)

O_ASYNC() (在 os 模块中)

O_BINARY() (在 os 模块中)

O_CLOEXEC() (在 os 模块中)

O_CREAT() (在 os 模块中)

O_DIRECT() (在 os 模块中)

O_DIRECTORY() (在 os 模块中)

O_DSYNC() (在 os 模块中)

O_EXCL() (在 os 模块中)

O_EXLOCK() (在 os 模块中)

O_NDELAY() (在 os 模块中)

O_NOATIME() (在 os 模块中)

O_NOCTTY() (在 os 模块中)

O_NOFOLLOW() (在 os 模块中)

O_NOINHERIT() (在 os 模块中)

O_NONBLOCK() (在 os 模块中)

O_PATH() (在 os 模块中)

O_RANDOM() (在 os 模块中)

O_RDONLY() (在 os 模块中)

O_RDWR() (在 os 模块中)

O_RSYNC() (在 os 模块中)

O_SEQUENTIAL() (在 os 模块中)

O_SHLOCK() (在 os 模块中)

O_SHORT_LIVED() (在 os 模块中)

O_SYNC() (在 os 模块中)

O_TEMPORARY() (在 os 模块中)

O_TEXT() (在 os 模块中)

O_TMPFILE() (在 os 模块中)

O_TRUNC() (在 os 模块中)

O_WRONLY() (在 os 模块中)

obj (memoryview 属性)

object

code, [1], [2], [3]

deallocation

finalization

immutable, [1]

numeric

object (UnicodeError 属性)

(內置类)

object -- 对象

object.__slots__ (內置变量)

object_filenames() (distutils.ccompiler.CCompiler 方法)

objects

comparing

flattening

marshalling

persistent

pickling

serializing

obufcount() (ossaudiodev.oss_audio_device 方法)

obuffree() (ossaudiodev.oss_audio_device 方法)

oct() (內置函数)

octal

literals

octal literal

octdigits() (在 string 模块中)

offset (traceback.TracebackException 属性)

(xml.parsers.expat.ExpatError 属性)

OK() (在 curses 模块中)

OleDLL (ctypes 中的类)

onclick() (在 turtle 模块中), [1]

ondrag() (在 turtle 模块中)

onecmd() (cmd.Cmd 方法)

onkey() (在 turtle 模块中)

onkeypress() (在 turtle 模块中)

onkeyrelease() (在 turtle 模块中)

onrelease() (在 turtle 模块中)

onscreenclick() (在 turtle 模块中)

ontimer() (在 turtle 模块中)

OP() (在 token 模块中)

OP_ALL() (在 ssl 模块中)

OP_CIPHER_SERVER_PREFERENCE() (在 ssl 模块中)

OP_ENABLE_MIDDLEBOX_COMPAT() (在 ssl 模块中)

OP_NO_COMPRESSION() (在 ssl 模块中)

OP_NO_SSLv2() (在 ssl 模块中)

OP_NO_SSLv3() (在 ssl 模块中)

OP_NO_TICKET() (在 ssl 模块中)

OP_NO_TLSv1() (在 ssl 模块中)

OP_NO_TLSv1_1() (在 ssl 模块中)

OP_NO_TLSv1_2() (在 ssl 模块中)

OP_NO_TLSv1_3() (在 ssl 模块中)

OP_SINGLE_DH_USE() (在 ssl 模块中)

OP_SINGLE_ECDH_USE() (在 ssl 模块中)

open

內置函数, [1]

open() (distutils.text_file.TextFile 方法)

(imaplib.IMAP4 方法)

(pathlib.Path 方法)

(pipes.Template 方法)

(tarfile.TarFile 类方法)

(telnetlib.Telnet 方法)

(urllib.request.OpenerDirector 方法)

(urllib.request.URLopener 方法)

(webbrowser.controller 方法)

(zipfile.ZipFile 方法)

(內置函数)

(在 aifc 模块中)

(在 bz2 模块中)

(在 codecs 模块中)

(在 dbm 模块中)

(在 dbm.dumb 模块中)

(在 dbm.gnu 模块中)

(在 dbm.ndbm 模块中)

(在 gzip 模块中)

(在 io 模块中)

(在 lzma 模块中)

(在 os 模块中)

(在 ossaudiodev 模块中)

(在 shelve 模块中)

(在 sunau 模块中)

(在 tarfile 模块中)

(在 tokenize 模块中)

(在 wave 模块中)

(在 webbrowser 模块中)

open_connection() (在 asyncio 模块中)

open_new() (webbrowser.controller 方法)

(在 webbrowser 模块中)

open_new_tab() (webbrowser.controller 方法)

(在 webbrowser 模块中)

open_osfhandle() (在 msvcrt 模块中)

open_unix_connection() (在 asyncio 模块中)

open_unknown() (urllib.request.URLopener 方法)

OpenDatabase() (在 msilib 模块中)

OpenerDirector (urllib.request 中的类)

openfp() (在 sunau 模块中)

(在 wave 模块中)

OpenKey() (在 winreg 模块中)

OpenKeyEx() (在 winreg 模块中)

openlog() (在 syslog 模块中)

openmixer() (在 ossaudiodev 模块中)

openpty() (在 os 模块中)

(在 pty 模块中)

OpenSSL

(use in module hashlib)

(use in module ssl)

OPENSSL_VERSION() (在 ssl 模块中)

OPENSSL_VERSION_INFO() (在 ssl 模块中)

OPENSSL_VERSION_NUMBER() (在 ssl 模块中)

OpenView() (msilib.Database 方法)

operation

binary arithmetic

binary bitwise

Boolean

concatenation

null, [1]

power

repetition

shifting

slice

subscript

unary arithmetic

unary bitwise

OperationalError

operations

bitwise

Boolean, [1]

masking

shifting

operations on

dictionary type

integer types

list type

mapping types

numeric types

sequence types, [1]

operator

+ (plus), [1], [2]

- (minus), [1], [2]

comparison

overloading

precedence

ternary

operator (2to3 fixer)

(模块)

operators

opmap() (在 dis 模块中)

opname() (在 dis 模块中)

optimize() (在 pickletools 模块中)

OPTIMIZED_BYTECODE_SUFFIXES() (在 importlib.machinery 模块中)

Optional() (在 typing 模块中)

OptionGroup (optparse 中的类)

OptionMenu (tkinter.tix 中的类)

OptionParser (optparse 中的类)

options (doctest.Example 属性)

Options (ssl 中的类)

options (ssl.SSLContext 属性)

options() (configparser.ConfigParser 方法)

optionxform() (configparser.ConfigParser 方法), [1]

optparse (模块)

or

bitwise

exclusive

inclusive

运算符, [1], [2]

or_() (在 operator 模块中)

ord

內置函数

ord() (內置函数)

order

evaluation

ordered_attributes (xml.parsers.expat.xmlparser 属性)

OrderedDict (collections 中的类)

origin (importlib.machinery.ModuleSpec 属性)

origin_req_host (urllib.request.Request 属性)

origin_server (wsgiref.handlers.BaseHandler 属性)

os

模块

os (模块)

os.path (模块)

os_environ (wsgiref.handlers.BaseHandler 属性)

OSError

ossaudiodev (模块)

OSSAudioError

outfile

json.tool command line option

output

standard

output (subprocess.CalledProcessError 属性)

(subprocess.TimeoutExpired 属性)

(unittest.TestCase 属性)

output() (http.cookies.BaseCookie 方法)

(http.cookies.Morsel 方法)

output_charset (email.charset.Charset 属性)

output_charset() (gettext.NullTranslations 方法)

output_codec (email.charset.Charset 属性)

output_difference() (doctest.OutputChecker 方法)

OutputChecker (doctest 中的类)

OutputString() (http.cookies.Morsel 方法)

over() (nntplib.NNTP 方法)

Overflow (decimal 中的类)

OverflowError

(built-in exception), [1], [2], [3], [4], [5]

overlaps() (ipaddress.IPv4Network 方法)

(ipaddress.IPv6Network 方法)

overlay() (curses.window 方法)

overload() (在 typing 模块中)

overloading

operator

overwrite() (curses.window 方法)

owner() (pathlib.Path 方法)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – P

p (pdb command)

P_ALL() (在 os 模块中)

P_DETACH() (在 os 模块中)

P_NOWAIT() (在 os 模块中)

P_NOWAITO() (在 os 模块中)

P_OVERLAY() (在 os 模块中)

P_PGID() (在 os 模块中)

P_PID() (在 os 模块中)

P_WAIT() (在 os 模块中)

pack() (mailbox.MH 方法)

(struct.Struct 方法)

(在 struct 模块中)

pack_array() (xdrlib.Packer 方法)

pack_bytes() (xdrlib.Packer 方法)

pack_double() (xdrlib.Packer 方法)

pack_farray() (xdrlib.Packer 方法)

pack_float() (xdrlib.Packer 方法)

pack_fopaque() (xdrlib.Packer 方法)

pack_fstring() (xdrlib.Packer 方法)

pack_into() (struct.Struct 方法)

(在 struct 模块中)

pack_list() (xdrlib.Packer 方法)

pack_opaque() (xdrlib.Packer 方法)

pack_string() (xdrlib.Packer 方法)

package, [1]

namespace

portion

regular

package -- 包

package variable

__all__

packed (ipaddress.IPv4Address 属性)

(ipaddress.IPv6Address 属性)

Packer (xdrlib 中的类)

packing

binary data

packing (widgets)

PAGER

pair_content() (在 curses 模块中)

pair_number() (在 curses 模块中)

PanedWindow (tkinter.tix 中的类)

parameter

call semantics

difference from argument

function definition

value, default

Parameter (inspect 中的类)

parameter -- 形参

ParameterizedMIMEHeader (email.headerregistry 中的类)

parameters (inspect.Signature 属性)

params (email.headerregistry.ParameterizedMIMEHeader 属性)

pardir() (在 os 模块中)

paren (2to3 fixer)

parent (importlib.machinery.ModuleSpec 属性)

(urllib.request.BaseHandler 属性)

parent() (tkinter.ttk.Treeview 方法)

parenthesized form

parentNode (xml.dom.Node 属性)

parents (collections.ChainMap 属性)

paretovariate() (在 random 模块中)

parse() (doctest.DocTestParser 方法)

(email.parser.BytesParser 方法)

(email.parser.Parser 方法)

(string.Formatter 方法)

(urllib.robotparser.RobotFileParser 方法)

(xml.etree.ElementTree.ElementTree 方法)

Parse() (xml.parsers.expat.xmlparser 方法)

parse() (xml.sax.xmlreader.XMLReader 方法)

(在 ast 模块中)

(在 cgi 模块中)

(在 xml.dom.minidom 模块中)

(在 xml.dom.pulldom 模块中)

(在 xml.etree.ElementTree 模块中)

(在 xml.sax 模块中)

parse_and_bind() (在 readline 模块中)

parse_args() (argparse.ArgumentParser 方法)

PARSE_COLNAMES() (在 sqlite3 模块中)

parse_config_h() (在 sysconfig 模块中)

PARSE_DECLTYPES() (在 sqlite3 模块中)

parse_header() (在 cgi 模块中)

parse_known_args() (argparse.ArgumentParser 方法)

parse_multipart() (在 cgi 模块中)

parse_qs() (在 cgi 模块中)

(在 urllib.parse 模块中)

parse_qsl() (在 cgi 模块中)

(在 urllib.parse 模块中)

parseaddr() (在 email.utils 模块中)

parsebytes() (email.parser.BytesParser 方法)

parsedate() (在 email.utils 模块中)

parsedate_to_datetime() (在 email.utils 模块中)

parsedate_tz() (在 email.utils 模块中)

ParseError (xml.etree.ElementTree 中的类)

ParseFile() (xml.parsers.expat.xmlparser 方法)

ParseFlags() (在 imaplib 模块中)

parser

Parser (email.parser 中的类)

parser (模块)

ParserCreate() (在 xml.parsers.expat 模块中)

ParserError

ParseResult (urllib.parse 中的类)

ParseResultBytes (urllib.parse 中的类)

parsestr() (email.parser.Parser 方法)

parseString() (在 xml.dom.minidom 模块中)

(在 xml.dom.pulldom 模块中)

(在 xml.sax 模块中)

parsing

Python source code

URL

ParsingError

partial (asyncio.IncompleteReadError 属性)

partial() (imaplib.IMAP4 方法)

(在 functools 模块中)

partialmethod (functools 中的类)

parties (threading.Barrier 属性)

partition() (bytearray 方法)

(bytes 方法)

(str 方法)

pass

语句

pass_() (poplib.POP3 方法)

Paste

patch() (在 unittest.mock 模块中)

patch.dict() (在 unittest.mock 模块中)

patch.multiple() (在 unittest.mock 模块中)

patch.object() (在 unittest.mock 模块中)

patch.stopall() (在 unittest.mock 模块中)

PATH, [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], [26], [27], [28], [29], [30], [31], [32], [33], [34]

path

configuration file

hooks

module search, [1], [2], [3], [4], [5], [6], [7]

operations, [1]

path (http.cookiejar.Cookie 属性)

(http.server.BaseHTTPRequestHandler 属性)

(importlib.abc.FileLoader 属性)

(importlib.machinery.ExtensionFileLoader 属性)

(importlib.machinery.FileFinder 属性)

(importlib.machinery.SourceFileLoader 属性)

(importlib.machinery.SourcelessFileLoader 属性)

(in module sys), [1], [2], [3]

(os.DirEntry 属性)

Path (pathlib 中的类)

path based finder

path based finder -- 基于路径的查找器

Path browser

path entry -- 路径入口

path entry finder -- 路径入口查找器

path entry hook -- 路径入口钩子

path hooks

path() (在 sys 模块中)

path-like object -- 路径类对象

path_hook() (importlib.machinery.FileFinder 类方法)

path_hooks() (在 sys 模块中)

path_importer_cache() (在 sys 模块中)

path_mtime() (importlib.abc.SourceLoader 方法)

path_return_ok() (http.cookiejar.CookiePolicy 方法)

path_stats() (importlib.abc.SourceLoader 方法)

(importlib.machinery.SourceFileLoader 方法)

pathconf() (在 os 模块中)

pathconf_names() (在 os 模块中)

PathEntryFinder (importlib.abc 中的类)

PATHEXT, [1]

PathFinder (importlib.machinery 中的类)

pathlib (模块)

PathLike (os 中的类)

pathname2url() (在 urllib.request 模块中)

pathsep() (在 os 模块中)

pattern (re.error 属性)

(re.regex 属性)

Pattern (typing 中的类)

pause() (在 signal 模块中)

pause_reading() (asyncio.ReadTransport 方法)

pause_writing() (asyncio.BaseProtocol 方法)

PAX_FORMAT() (在 tarfile 模块中)

pax_headers (tarfile.TarFile 属性)

(tarfile.TarInfo 属性)

pbkdf2_hmac() (在 hashlib 模块中)

pd() (在 turtle 模块中)

Pdb (class in pdb)

(pdb 中的类)

pdb (模块)

peek() (bz2.BZ2File 方法)

(gzip.GzipFile 方法)

(io.BufferedReader 方法)

(lzma.LZMAFile 方法)

(weakref.finalize 方法)

peer (smtpd.SMTPChannel 属性)

PEM_cert_to_DER_cert() (在 ssl 模块中)

pen() (在 turtle 模块中)

pencolor() (在 turtle 模块中)

pending (ssl.MemoryBIO 属性)

pending() (ssl.SSLSocket 方法)

PendingDeprecationWarning

pendown() (在 turtle 模块中)

pensize() (在 turtle 模块中)

penup() (在 turtle 模块中)

PEP

PERCENT() (在 token 模块中)

PERCENTEQUAL() (在 token 模块中)

perf_counter() (在 time 模块中)

Performance

PermissionError

permutations() (在 itertools 模块中)

Persist() (msilib.SummaryInformation 方法)

persistence

persistent

objects

persistent_id (pickle protocol)

persistent_id() (pickle.Pickler 方法)

persistent_load (pickle protocol)

persistent_load() (pickle.Unpickler 方法)

PF_CAN() (在 socket 模块中)

PF_PACKET() (在 socket 模块中)

PF_RDS() (在 socket 模块中)

pformat() (pprint.PrettyPrinter 方法)

(在 pprint 模块中)

phase() (在 cmath 模块中)

Philbrick, Geoff

physical line, [1], [2]

pi() (在 cmath 模块中)

(在 math 模块中)

pickle

模块, [1], [2], [3]

pickle (模块)

pickle() (在 copyreg 模块中)

PickleError

Pickler (pickle 中的类)

pickletools (模块)

pickletools command line option

--annotate

--indentlevel=<num>

--memo

--output=<file>

--preamble=<preamble>

-a

-l

-m

-o

-p

pickling

objects

PicklingError

pid (asyncio.asyncio.subprocess.Process 属性)

(multiprocessing.Process 属性)

(subprocess.Popen 属性)

Pipe() (在 multiprocessing 模块中)

pipe() (在 os 模块中)

PIPE() (在 subprocess 模块中)

pipe2() (在 os 模块中)

PIPE_BUF() (在 select 模块中)

pipe_connection_lost() (asyncio.SubprocessProtocol 方法)

pipe_data_received() (asyncio.SubprocessProtocol 方法)

pipes (模块)

PKG_DIRECTORY() (在 imp 模块中)

pkgutil (模块)

placeholder (textwrap.TextWrapper 属性)

PLAT

platform (in module sys)

(模块)

platform() (在 platform 模块中)

(在 sys 模块中)

PlaySound() (在 winsound 模块中)

plist

file

plistlib (模块)

plock() (在 os 模块中)

plus

plus() (decimal.Context 方法)

PLUS() (在 token 模块中)

PLUSEQUAL() (在 token 模块中)

pm() (在 pdb 模块中)

POINTER() (在 ctypes 模块中)

pointer() (在 ctypes 模块中)

polar() (在 cmath 模块中)

Policy (email.policy 中的类)

poll() (multiprocessing.connection.Connection 方法)

(select.devpoll 方法)

(select.epoll 方法)

(select.poll 方法)

(subprocess.Popen 方法)

(在 select 模块中)

PollSelector (selectors 中的类)

Pool (multiprocessing.pool 中的类)

pop() (array.array 方法)

(collections.deque 方法)

(dict 方法)

(frozenset 方法)

(mailbox.Mailbox 方法)

(sequence method)

POP3

protocol

POP3 (poplib 中的类)

POP3_SSL (poplib 中的类)

pop_alignment() (formatter.formatter 方法)

pop_all() (contextlib.ExitStack 方法)

POP_BLOCK (opcode)

POP_EXCEPT (opcode)

pop_font() (formatter.formatter 方法)

POP_JUMP_IF_FALSE (opcode)

POP_JUMP_IF_TRUE (opcode)

pop_margin() (formatter.formatter 方法)

pop_source() (shlex.shlex 方法)

pop_style() (formatter.formatter 方法)

POP_TOP (opcode)

Popen (subprocess 中的类)

popen() (in module os), [1]

(在 os 模块中)

(在 platform 模块中)

popitem() (collections.OrderedDict 方法)

(dict 方法)

(mailbox.Mailbox 方法)

popleft() (collections.deque 方法)

poplib (模块)

PopupMenu (tkinter.tix 中的类)

port (http.cookiejar.Cookie 属性)

port_specified (http.cookiejar.Cookie 属性)

portion

package

portion -- 部分

pos (json.JSONDecodeError 属性)

(re.error 属性)

(re.match 属性)

pos() (在 operator 模块中)

(在 turtle 模块中)

position (xml.etree.ElementTree.ParseError 属性)

position() (在 turtle 模块中)

positional argument -- 位置参数

POSIX

I/O control

threads

posix (模块)

POSIX_FADV_DONTNEED() (在 os 模块中)

POSIX_FADV_NOREUSE() (在 os 模块中)

POSIX_FADV_NORMAL() (在 os 模块中)

POSIX_FADV_RANDOM() (在 os 模块中)

POSIX_FADV_SEQUENTIAL() (在 os 模块中)

POSIX_FADV_WILLNEED() (在 os 模块中)

posix_fadvise() (在 os 模块中)

posix_fallocate() (在 os 模块中)

POSIXLY_CORRECT

PosixPath (pathlib 中的类)

post() (nntplib.NNTP 方法)

(ossaudiodev.oss_audio_device 方法)

post_handshake_auth (ssl.SSLContext 属性)

post_mortem() (在 pdb 模块中)

post_setup() (venv.EnvBuilder 方法)

postcmd() (cmd.Cmd 方法)

postloop() (cmd.Cmd 方法)

pow

內置函数, [1], [2], [3], [4], [5]

pow() (內置函数)

(在 math 模块中)

(在 operator 模块中)

power

operation

power() (decimal.Context 方法)

pp (pdb command)

pprint (模块)

pprint() (pprint.PrettyPrinter 方法)

(在 pprint 模块中)

prcal() (在 calendar 模块中)

pread() (在 os 模块中)

preamble (email.message.EmailMessage 属性)

(email.message.Message 属性)

precedence

operator

precmd() (cmd.Cmd 方法)

prefix, [1], [2], [3]

(xml.dom.Attr 属性)

(xml.dom.Node 属性)

(zipimport.zipimporter 属性)

PREFIX() (在 distutils.sysconfig 模块中)

prefix() (在 sys 模块中)

PREFIXES() (在 site 模块中)

prefixlen (ipaddress.IPv4Network 属性)

(ipaddress.IPv6Network 属性)

preloop() (cmd.Cmd 方法)

prepare() (logging.handlers.QueueHandler 方法)

(logging.handlers.QueueListener 方法)

prepare_class() (在 types 模块中)

prepare_input_source() (在 xml.sax.saxutils 模块中)

prepend() (pipes.Template 方法)

preprocess() (distutils.ccompiler.CCompiler 方法)

PrettyPrinter (pprint 中的类)

prev() (tkinter.ttk.Treeview 方法)

previousSibling (xml.dom.Node 属性)

primary

print

內置函数

print (2to3 fixer)

print() (built-in function)

__str__() (object method)

print() (內置函数)

print_callees() (pstats.Stats 方法)

print_callers() (pstats.Stats 方法)

print_directory() (在 cgi 模块中)

print_environ() (在 cgi 模块中)

print_environ_usage() (在 cgi 模块中)

print_exc() (timeit.Timer 方法)

(在 traceback 模块中)

print_exception() (在 traceback 模块中)

PRINT_EXPR (opcode)

print_form() (在 cgi 模块中)

print_help() (argparse.ArgumentParser 方法)

print_last() (在 traceback 模块中)

print_stack() (asyncio.Task 方法)

(在 traceback 模块中)

print_stats() (profile.Profile 方法)

(pstats.Stats 方法)

print_tb() (在 traceback 模块中)

print_usage() (argparse.ArgumentParser 方法)

(optparse.OptionParser 方法)

print_version() (optparse.OptionParser 方法)

printable() (在 string 模块中)

printdir() (zipfile.ZipFile 方法)

printf-style formatting, [1]

PRIO_PGRP() (在 os 模块中)

PRIO_PROCESS() (在 os 模块中)

PRIO_USER() (在 os 模块中)

PriorityQueue (asyncio 中的类)

(queue 中的类)

private

names

prlimit() (在 resource 模块中)

prmonth() (calendar.TextCalendar 方法)

(在 calendar 模块中)

ProactorEventLoop (asyncio 中的类)

procedure

call

process

group, [1]

id

id of parent

killing, [1]

scheduling priority, [1]

signalling, [1]

Process (multiprocessing 中的类)

process() (logging.LoggerAdapter 方法)

process_exited() (asyncio.SubprocessProtocol 方法)

process_message() (smtpd.SMTPServer 方法)

process_request() (socketserver.BaseServer 方法)

process_time() (在 time 模块中)

process_tokens() (在 tabnanny 模块中)

ProcessError

processes, light-weight

processingInstruction() (xml.sax.handler.ContentHandler 方法)

ProcessingInstruction() (在 xml.etree.ElementTree 模块中)

ProcessingInstructionHandler() (xml.parsers.expat.xmlparser 方法)

ProcessLookupError

processor time

processor() (在 platform 模块中)

ProcessPoolExecutor (concurrent.futures 中的类)

product() (在 itertools 模块中)

Profile (profile 中的类)

profile (模块)

profile function, [1], [2]

profiler, [1]

profiling, deterministic

program

ProgrammingError

Progressbar (tkinter.ttk 中的类)

prompt (cmd.Cmd 属性)

prompt_user_passwd() (urllib.request.FancyURLopener 方法)

prompts, interpreter

propagate (logging.Logger 属性)

property (內置类)

property list

property_declaration_handler() (在 xml.sax.handler 模块中)

property_dom_node() (在 xml.sax.handler 模块中)

property_lexical_handler() (在 xml.sax.handler 模块中)

property_xml_string() (在 xml.sax.handler 模块中)

PropertyMock (unittest.mock 中的类)

prot_c() (ftplib.FTP_TLS 方法)

prot_p() (ftplib.FTP_TLS 方法)

proto (socket.socket 属性)

protocol

CGI

context management

copy

FTP, [1]

HTTP, [1], [2], [3], [4], [5]

IMAP4

IMAP4_SSL

IMAP4_stream

iterator

NNTP

POP3

SMTP

Telnet

Protocol (asyncio 中的类)

protocol (ssl.SSLContext 属性)

PROTOCOL_SSLv2() (在 ssl 模块中)

PROTOCOL_SSLv23() (在 ssl 模块中)

PROTOCOL_SSLv3() (在 ssl 模块中)

PROTOCOL_TLS() (在 ssl 模块中)

PROTOCOL_TLS_CLIENT() (在 ssl 模块中)

PROTOCOL_TLS_SERVER() (在 ssl 模块中)

PROTOCOL_TLSv1() (在 ssl 模块中)

PROTOCOL_TLSv1_1() (在 ssl 模块中)

PROTOCOL_TLSv1_2() (在 ssl 模块中)

protocol_version (http.server.BaseHTTPRequestHandler 属性)

PROTOCOL_VERSION (imaplib.IMAP4 属性)

ProtocolError (xmlrpc.client 中的类)

provisional API -- 暂定 API

provisional package -- 暂定包

proxy() (在 weakref 模块中)

proxyauth() (imaplib.IMAP4 方法)

ProxyBasicAuthHandler (urllib.request 中的类)

ProxyDigestAuthHandler (urllib.request 中的类)

ProxyHandler (urllib.request 中的类)

ProxyType() (在 weakref 模块中)

ProxyTypes() (在 weakref 模块中)

pryear() (calendar.TextCalendar 方法)

ps1() (在 sys 模块中)

ps2() (在 sys 模块中)

pstats (模块)

pstdev() (在 statistics 模块中)

pthread_kill() (在 signal 模块中)

pthread_sigmask() (在 signal 模块中)

pthreads

pty

模块

pty (模块)

pu() (在 turtle 模块中)

publicId (xml.dom.DocumentType 属性)

PullDom (xml.dom.pulldom 中的类)

punctuation() (在 string 模块中)

punctuation_chars (shlex.shlex 属性)

PurePath (pathlib 中的类)

PurePath.anchor() (在 pathlib 模块中)

PurePath.drive() (在 pathlib 模块中)

PurePath.name() (在 pathlib 模块中)

PurePath.parent() (在 pathlib 模块中)

PurePath.parents() (在 pathlib 模块中)

PurePath.parts() (在 pathlib 模块中)

PurePath.root() (在 pathlib 模块中)

PurePath.stem() (在 pathlib 模块中)

PurePath.suffix() (在 pathlib 模块中)

PurePath.suffixes() (在 pathlib 模块中)

PurePosixPath (pathlib 中的类)

PureProxy (smtpd 中的类)

PureWindowsPath (pathlib 中的类)

purge() (在 re 模块中)

Purpose.CLIENT_AUTH() (在 ssl 模块中)

Purpose.SERVER_AUTH() (在 ssl 模块中)

push() (asynchat.async_chat 方法)

(code.InteractiveConsole 方法)

(contextlib.ExitStack 方法)

push_alignment() (formatter.formatter 方法)

push_font() (formatter.formatter 方法)

push_margin() (formatter.formatter 方法)

push_source() (shlex.shlex 方法)

push_style() (formatter.formatter 方法)

push_token() (shlex.shlex 方法)

push_with_producer() (asynchat.async_chat 方法)

pushbutton() (msilib.Dialog 方法)

put() (asyncio.Queue 方法)

(multiprocessing.Queue 方法)

(multiprocessing.SimpleQueue 方法)

(queue.Queue 方法)

put_nowait() (asyncio.Queue 方法)

(multiprocessing.Queue 方法)

(queue.Queue 方法)

putch() (在 msvcrt 模块中)

putenv() (在 os 模块中)

putheader() (http.client.HTTPConnection 方法)

putp() (在 curses 模块中)

putrequest() (http.client.HTTPConnection 方法)

putwch() (在 msvcrt 模块中)

putwin() (curses.window 方法)

pvariance() (在 statistics 模块中)

pwd

模块

pwd (模块)

pwd() (ftplib.FTP 方法)

pwrite() (在 os 模块中)

Py_AddPendingCall (C 函数)

Py_AddPendingCall()

Py_AtExit (C 函数)

Py_BEGIN_ALLOW_THREADS

(C 宏)

Py_BLOCK_THREADS (C 宏)

Py_buffer (C 类型)

Py_buffer.buf (C 成员)

Py_buffer.format (C 成员)

Py_buffer.internal (C 成员)

Py_buffer.itemsize (C 成员)

Py_buffer.len (C 成员)

Py_buffer.ndim (C 成员)

Py_buffer.obj (C 成员)

Py_buffer.readonly (C 成员)

Py_buffer.shape (C 成员)

Py_buffer.strides (C 成员)

Py_buffer.suboffsets (C 成员)

Py_BuildValue (C 函数)

Py_CLEAR (C 函数)

py_compile (模块)

PY_COMPILED() (在 imp 模块中)

Py_CompileString (C 函数)

Py_CompileString(), [1], [2]

Py_CompileStringExFlags (C 函数)

Py_CompileStringFlags (C 函数)

Py_CompileStringObject (C 函数)

Py_complex (C 类型)

Py_DecodeLocale (C 函数)

Py_DECREF (C 函数)

Py_DECREF()

Py_Ellipsis (C 变量)

Py_EncodeLocale (C 函数)

Py_END_ALLOW_THREADS

(C 宏)

Py_EndInterpreter (C 函数)

Py_EnterRecursiveCall (C 函数)

Py_eval_input (C 变量)

Py_Exit (C 函数)

Py_False (C 变量)

Py_FatalError (C 函数)

Py_FatalError()

Py_FdIsInteractive (C 函数)

Py_file_input (C 变量)

Py_Finalize (C 函数)

Py_FinalizeEx (C 函数)

Py_FinalizeEx(), [1], [2], [3], [4]

PY_FROZEN() (在 imp 模块中)

Py_GetBuildInfo (C 函数)

Py_GetCompiler (C 函数)

Py_GetCopyright (C 函数)

Py_GetExecPrefix (C 函数)

Py_GetExecPrefix()

Py_GetPath (C 函数)

Py_GetPath(), [1], [2]

Py_GetPlatform (C 函数)

Py_GetPrefix (C 函数)

Py_GetPrefix()

Py_GetProgramFullPath (C 函数)

Py_GetProgramFullPath()

Py_GetProgramName (C 函数)

Py_GetPythonHome (C 函数)

Py_GetVersion (C 函数)

Py_INCREF (C 函数)

Py_INCREF()

Py_Initialize (C 函数)

Py_Initialize(), [1], [2], [3]

Py_InitializeEx (C 函数)

Py_IsInitialized (C 函数)

Py_IsInitialized()

Py_LeaveRecursiveCall (C 函数)

Py_Main (C 函数)

Py_mod_create (C 变量)

Py_mod_exec (C 变量)

Py_NewInterpreter (C 函数)

Py_None (C 变量)

Py_NotImplemented (C 变量)

py_object (ctypes 中的类)

Py_PRINT_RAW

Py_REFCNT (C 宏)

Py_ReprEnter (C 函数)

Py_ReprLeave (C 函数)

Py_RETURN_FALSE (C 宏)

Py_RETURN_NONE (C 宏)

Py_RETURN_NOTIMPLEMENTED (C 宏)

Py_RETURN_TRUE (C 宏)

Py_SetPath (C 函数)

Py_SetPath()

Py_SetProgramName (C 函数)

Py_SetProgramName(), [1], [2], [3]

Py_SetPythonHome (C 函数)

Py_SetStandardStreamEncoding (C 函数)

Py_single_input (C 变量)

Py_SIZE (C 宏)

PY_SOURCE() (在 imp 模块中)

PY_SSIZE_T_MAX

Py_TPFLAGS_BASE_EXC_SUBCLASS (內置变量)

Py_TPFLAGS_BASETYPE (內置变量)

Py_TPFLAGS_BYTES_SUBCLASS (內置变量)

Py_TPFLAGS_DEFAULT (內置变量)

Py_TPFLAGS_DICT_SUBCLASS (內置变量)

Py_TPFLAGS_HAVE_FINALIZE (內置变量)

Py_TPFLAGS_HAVE_GC (內置变量)

Py_TPFLAGS_HEAPTYPE (內置变量)

Py_TPFLAGS_LIST_SUBCLASS (內置变量)

Py_TPFLAGS_LONG_SUBCLASS (內置变量)

Py_TPFLAGS_READY (內置变量)

Py_TPFLAGS_READYING (內置变量)

Py_TPFLAGS_TUPLE_SUBCLASS (內置变量)

Py_TPFLAGS_TYPE_SUBCLASS (內置变量)

Py_TPFLAGS_UNICODE_SUBCLASS (內置变量)

Py_tracefunc (C 类型)

Py_True (C 变量)

Py_TYPE (C 宏)

Py_UCS1 (C 类型)

Py_UCS2 (C 类型)

Py_UCS4 (C 类型)

Py_UNBLOCK_THREADS (C 宏)

Py_UNICODE (C 类型)

Py_UNICODE_IS_HIGH_SURROGATE (C 宏)

Py_UNICODE_IS_LOW_SURROGATE (C 宏)

Py_UNICODE_IS_SURROGATE (C 宏)

Py_UNICODE_ISALNUM (C 函数)

Py_UNICODE_ISALPHA (C 函数)

Py_UNICODE_ISDECIMAL (C 函数)

Py_UNICODE_ISDIGIT (C 函数)

Py_UNICODE_ISLINEBREAK (C 函数)

Py_UNICODE_ISLOWER (C 函数)

Py_UNICODE_ISNUMERIC (C 函数)

Py_UNICODE_ISPRINTABLE (C 函数)

Py_UNICODE_ISSPACE (C 函数)

Py_UNICODE_ISTITLE (C 函数)

Py_UNICODE_ISUPPER (C 函数)

Py_UNICODE_JOIN_SURROGATES (C 宏)

Py_UNICODE_TODECIMAL (C 函数)

Py_UNICODE_TODIGIT (C 函数)

Py_UNICODE_TOLOWER (C 函数)

Py_UNICODE_TONUMERIC (C 函数)

Py_UNICODE_TOTITLE (C 函数)

Py_UNICODE_TOUPPER (C 函数)

Py_VaBuildValue (C 函数)

Py_VISIT (C 函数)

Py_XDECREF (C 函数)

Py_XDECREF()

Py_XINCREF (C 函数)

PyAnySet_Check (C 函数)

PyAnySet_CheckExact (C 函数)

PyArg_Parse (C 函数)

PyArg_ParseTuple (C 函数)

PyArg_ParseTuple()

PyArg_ParseTupleAndKeywords (C 函数)

PyArg_ParseTupleAndKeywords()

PyArg_UnpackTuple (C 函数)

PyArg_ValidateKeywordArguments (C 函数)

PyArg_VaParse (C 函数)

PyArg_VaParseTupleAndKeywords (C 函数)

PyASCIIObject (C 类型)

PyAsyncMethods (C 类型)

PyAsyncMethods.am_aiter (C 成员)

PyAsyncMethods.am_anext (C 成员)

PyAsyncMethods.am_await (C 成员)

PyBool_Check (C 函数)

PyBool_FromLong (C 函数)

PyBUF_ANY_CONTIGUOUS (C 宏)

PyBUF_C_CONTIGUOUS (C 宏)

PyBUF_CONTIG (C 宏)

PyBUF_CONTIG_RO (C 宏)

PyBUF_F_CONTIGUOUS (C 宏)

PyBUF_FORMAT (C 宏)

PyBUF_FULL (C 宏)

PyBUF_FULL_RO (C 宏)

PyBUF_INDIRECT (C 宏)

PyBUF_ND (C 宏), [1]

PyBUF_RECORDS (C 宏)

PyBUF_RECORDS_RO (C 宏)

PyBUF_SIMPLE (C 宏)

PyBUF_STRIDED (C 宏)

PyBUF_STRIDED_RO (C 宏)

PyBUF_STRIDES (C 宏)

PyBUF_WRITABLE (C 宏)

PyBuffer_FillContiguousStrides (C 函数)

PyBuffer_FillInfo (C 函数)

PyBuffer_IsContiguous (C 函数)

PyBuffer_Release (C 函数)

PyBuffer_SizeFromFormat (C 函数)

PyBuffer_ToContiguous (C 函数)

PyBufferProcs

(C 类型)

PyBufferProcs.bf_getbuffer (C 成员)

PyBufferProcs.bf_releasebuffer (C 成员)

PyByteArray_AS_STRING (C 函数)

PyByteArray_AsString (C 函数)

PyByteArray_Check (C 函数)

PyByteArray_CheckExact (C 函数)

PyByteArray_Concat (C 函数)

PyByteArray_FromObject (C 函数)

PyByteArray_FromStringAndSize (C 函数)

PyByteArray_GET_SIZE (C 函数)

PyByteArray_Resize (C 函数)

PyByteArray_Size (C 函数)

PyByteArray_Type (C 变量)

PyByteArrayObject (C 类型)

PyBytes_AS_STRING (C 函数)

PyBytes_AsString (C 函数)

PyBytes_AsStringAndSize (C 函数)

PyBytes_Check (C 函数)

PyBytes_CheckExact (C 函数)

PyBytes_Concat (C 函数)

PyBytes_ConcatAndDel (C 函数)

PyBytes_FromFormat (C 函数)

PyBytes_FromFormatV (C 函数)

PyBytes_FromObject (C 函数)

PyBytes_FromString (C 函数)

PyBytes_FromStringAndSize (C 函数)

PyBytes_GET_SIZE (C 函数)

PyBytes_Size (C 函数)

PyBytes_Type (C 变量)

PyBytesObject (C 类型)

PyCallable_Check (C 函数)

PyCallIter_Check (C 函数)

PyCallIter_New (C 函数)

PyCallIter_Type (C 变量)

PyCapsule (C 类型)

PyCapsule_CheckExact (C 函数)

PyCapsule_Destructor (C 类型)

PyCapsule_GetContext (C 函数)

PyCapsule_GetDestructor (C 函数)

PyCapsule_GetName (C 函数)

PyCapsule_GetPointer (C 函数)

PyCapsule_Import (C 函数)

PyCapsule_IsValid (C 函数)

PyCapsule_New (C 函数)

PyCapsule_SetContext (C 函数)

PyCapsule_SetDestructor (C 函数)

PyCapsule_SetName (C 函数)

PyCapsule_SetPointer (C 函数)

PyCell_Check (C 函数)

PyCell_GET (C 函数)

PyCell_Get (C 函数)

PyCell_New (C 函数)

PyCell_SET (C 函数)

PyCell_Set (C 函数)

PyCell_Type (C 变量)

PyCellObject (C 类型)

PyCFunction (C 类型)

PyCFunctionWithKeywords (C 类型)

pyclbr (模块)

PyCode_Check (C 函数)

PyCode_GetNumFree (C 函数)

PyCode_New (C 函数)

PyCode_NewEmpty (C 函数)

PyCode_Type (C 变量)

PyCodec_BackslashReplaceErrors (C 函数)

PyCodec_Decode (C 函数)

PyCodec_Decoder (C 函数)

PyCodec_Encode (C 函数)

PyCodec_Encoder (C 函数)

PyCodec_IgnoreErrors (C 函数)

PyCodec_IncrementalDecoder (C 函数)

PyCodec_IncrementalEncoder (C 函数)

PyCodec_KnownEncoding (C 函数)

PyCodec_LookupError (C 函数)

PyCodec_NameReplaceErrors (C 函数)

PyCodec_Register (C 函数)

PyCodec_RegisterError (C 函数)

PyCodec_ReplaceErrors (C 函数)

PyCodec_StreamReader (C 函数)

PyCodec_StreamWriter (C 函数)

PyCodec_StrictErrors (C 函数)

PyCodec_XMLCharRefReplaceErrors (C 函数)

PyCodeObject (C 类型)

PyCompactUnicodeObject (C 类型)

PyCompileError

PyCompilerFlags (C 类型)

PyComplex_AsCComplex (C 函数)

PyComplex_Check (C 函数)

PyComplex_CheckExact (C 函数)

PyComplex_FromCComplex (C 函数)

PyComplex_FromDoubles (C 函数)

PyComplex_ImagAsDouble (C 函数)

PyComplex_RealAsDouble (C 函数)

PyComplex_Type (C 变量)

PyComplexObject (C 类型)

PyCoro_CheckExact (C 函数)

PyCoro_New (C 函数)

PyCoro_Type (C 变量)

PyCoroObject (C 类型)

PyDate_Check (C 函数)

PyDate_CheckExact (C 函数)

PyDate_FromDate (C 函数)

PyDate_FromTimestamp (C 函数)

PyDateTime_Check (C 函数)

PyDateTime_CheckExact (C 函数)

PyDateTime_DATE_GET_HOUR (C 函数)

PyDateTime_DATE_GET_MICROSECOND (C 函数)

PyDateTime_DATE_GET_MINUTE (C 函数)

PyDateTime_DATE_GET_SECOND (C 函数)

PyDateTime_DELTA_GET_DAYS (C 函数)

PyDateTime_DELTA_GET_MICROSECONDS (C 函数)

PyDateTime_DELTA_GET_SECONDS (C 函数)

PyDateTime_FromDateAndTime (C 函数)

PyDateTime_FromTimestamp (C 函数)

PyDateTime_GET_DAY (C 函数)

PyDateTime_GET_MONTH (C 函数)

PyDateTime_GET_YEAR (C 函数)

PyDateTime_TIME_GET_HOUR (C 函数)

PyDateTime_TIME_GET_MICROSECOND (C 函数)

PyDateTime_TIME_GET_MINUTE (C 函数)

PyDateTime_TIME_GET_SECOND (C 函数)

PyDelta_Check (C 函数)

PyDelta_CheckExact (C 函数)

PyDelta_FromDSU (C 函数)

PyDescr_IsData (C 函数)

PyDescr_NewClassMethod (C 函数)

PyDescr_NewGetSet (C 函数)

PyDescr_NewMember (C 函数)

PyDescr_NewMethod (C 函数)

PyDescr_NewWrapper (C 函数)

PyDict_Check (C 函数)

PyDict_CheckExact (C 函数)

PyDict_Clear (C 函数)

PyDict_ClearFreeList (C 函数)

PyDict_Contains (C 函数)

PyDict_Copy (C 函数)

PyDict_DelItem (C 函数)

PyDict_DelItemString (C 函数)

PyDict_GetItem (C 函数)

PyDict_GetItemString (C 函数)

PyDict_GetItemWithError (C 函数)

PyDict_Items (C 函数)

PyDict_Keys (C 函数)

PyDict_Merge (C 函数)

PyDict_MergeFromSeq2 (C 函数)

PyDict_New (C 函数)

PyDict_Next (C 函数)

PyDict_SetDefault (C 函数)

PyDict_SetItem (C 函数)

PyDict_SetItemString (C 函数)

PyDict_Size (C 函数)

PyDict_Type (C 变量)

PyDict_Update (C 函数)

PyDict_Values (C 函数)

PyDictObject (C 类型)

PyDictProxy_New (C 函数)

PyDLL (ctypes 中的类)

pydoc (模块)

PyErr_BadArgument (C 函数)

PyErr_BadInternalCall (C 函数)

PyErr_CheckSignals (C 函数)

PyErr_Clear (C 函数)

PyErr_Clear(), [1]

PyErr_ExceptionMatches (C 函数)

PyErr_ExceptionMatches()

PyErr_Fetch (C 函数)

PyErr_Fetch()

PyErr_Format (C 函数)

PyErr_FormatV (C 函数)

PyErr_GetExcInfo (C 函数)

PyErr_GivenExceptionMatches (C 函数)

PyErr_NewException (C 函数)

PyErr_NewExceptionWithDoc (C 函数)

PyErr_NoMemory (C 函数)

PyErr_NormalizeException (C 函数)

PyErr_Occurred (C 函数)

PyErr_Occurred()

PyErr_Print (C 函数)

PyErr_PrintEx (C 函数)

PyErr_ResourceWarning (C 函数)

PyErr_Restore (C 函数)

PyErr_Restore()

PyErr_SetExcFromWindowsErr (C 函数)

PyErr_SetExcFromWindowsErrWithFilename (C 函数)

PyErr_SetExcFromWindowsErrWithFilenameObject (C 函数)

PyErr_SetExcFromWindowsErrWithFilenameObjects (C 函数)

PyErr_SetExcInfo (C 函数)

PyErr_SetFromErrno (C 函数)

PyErr_SetFromErrnoWithFilename (C 函数)

PyErr_SetFromErrnoWithFilenameObject (C 函数)

PyErr_SetFromErrnoWithFilenameObjects (C 函数)

PyErr_SetFromWindowsErr (C 函数)

PyErr_SetFromWindowsErrWithFilename (C 函数)

PyErr_SetImportError (C 函数)

PyErr_SetImportErrorSubclass (C 函数)

PyErr_SetInterrupt (C 函数)

PyErr_SetNone (C 函数)

PyErr_SetObject (C 函数)

PyErr_SetString (C 函数)

PyErr_SetString()

PyErr_SyntaxLocation (C 函数)

PyErr_SyntaxLocationEx (C 函数)

PyErr_SyntaxLocationObject (C 函数)

PyErr_WarnEx (C 函数)

PyErr_WarnExplicit (C 函数)

PyErr_WarnExplicitObject (C 函数)

PyErr_WarnFormat (C 函数)

PyErr_WriteUnraisable (C 函数)

PyEval_AcquireLock (C 函数)

PyEval_AcquireThread (C 函数)

PyEval_AcquireThread()

PyEval_EvalCode (C 函数)

PyEval_EvalCodeEx (C 函数)

PyEval_EvalFrame (C 函数)

PyEval_EvalFrameEx (C 函数)

PyEval_GetBuiltins (C 函数)

PyEval_GetCallStats (C 函数)

PyEval_GetFrame (C 函数)

PyEval_GetFuncDesc (C 函数)

PyEval_GetFuncName (C 函数)

PyEval_GetGlobals (C 函数)

PyEval_GetLocals (C 函数)

PyEval_InitThreads (C 函数)

PyEval_InitThreads()

PyEval_MergeCompilerFlags (C 函数)

PyEval_ReInitThreads (C 函数)

PyEval_ReleaseLock (C 函数)

PyEval_ReleaseThread (C 函数)

PyEval_ReleaseThread()

PyEval_RestoreThread (C 函数)

PyEval_RestoreThread(), [1]

PyEval_SaveThread (C 函数)

PyEval_SaveThread(), [1]

PyEval_SetProfile (C 函数)

PyEval_SetTrace (C 函数)

PyEval_ThreadsInitialized (C 函数)

PyExc_ArithmeticError

PyExc_AssertionError

PyExc_AttributeError

PyExc_BaseException

PyExc_BlockingIOError

PyExc_BrokenPipeError

PyExc_BufferError

PyExc_BytesWarning

PyExc_ChildProcessError

PyExc_ConnectionAbortedError

PyExc_ConnectionError

PyExc_ConnectionRefusedError

PyExc_ConnectionResetError

PyExc_DeprecationWarning

PyExc_EnvironmentError

PyExc_EOFError

PyExc_Exception

PyExc_FileExistsError

PyExc_FileNotFoundError

PyExc_FloatingPointError

PyExc_FutureWarning

PyExc_GeneratorExit

PyExc_ImportError

PyExc_ImportWarning

PyExc_IndentationError

PyExc_IndexError

PyExc_InterruptedError

PyExc_IOError

PyExc_IsADirectoryError

PyExc_KeyboardInterrupt

PyExc_KeyError

PyExc_LookupError

PyExc_MemoryError

PyExc_ModuleNotFoundError

PyExc_NameError

PyExc_NotADirectoryError

PyExc_NotImplementedError

PyExc_OSError

PyExc_OverflowError

PyExc_PendingDeprecationWarning

PyExc_PermissionError

PyExc_ProcessLookupError

PyExc_RecursionError

PyExc_ReferenceError

PyExc_ResourceWarning

PyExc_RuntimeError

PyExc_RuntimeWarning

PyExc_StopAsyncIteration

PyExc_StopIteration

PyExc_SyntaxError

PyExc_SyntaxWarning

PyExc_SystemError

PyExc_SystemExit

PyExc_TabError

PyExc_TimeoutError

PyExc_TypeError

PyExc_UnboundLocalError

PyExc_UnicodeDecodeError

PyExc_UnicodeEncodeError

PyExc_UnicodeError

PyExc_UnicodeTranslateError

PyExc_UnicodeWarning

PyExc_UserWarning

PyExc_ValueError

PyExc_Warning

PyExc_WindowsError

PyExc_ZeroDivisionError

PyException_GetCause (C 函数)

PyException_GetContext (C 函数)

PyException_GetTraceback (C 函数)

PyException_SetCause (C 函数)

PyException_SetContext (C 函数)

PyException_SetTraceback (C 函数)

pyexpat

模块

PyFile_FromFd (C 函数)

PyFile_GetLine (C 函数)

PyFile_WriteObject (C 函数)

PyFile_WriteString (C 函数)

PyFloat_AS_DOUBLE (C 函数)

PyFloat_AsDouble (C 函数)

PyFloat_Check (C 函数)

PyFloat_CheckExact (C 函数)

PyFloat_ClearFreeList (C 函数)

PyFloat_FromDouble (C 函数)

PyFloat_FromString (C 函数)

PyFloat_GetInfo (C 函数)

PyFloat_GetMax (C 函数)

PyFloat_GetMin (C 函数)

PyFloat_Type (C 变量)

PyFloatObject (C 类型)

PyFrame_GetLineNumber (C 函数)

PyFrameObject (C 类型)

PyFrozenSet_Check (C 函数)

PyFrozenSet_CheckExact (C 函数)

PyFrozenSet_New (C 函数)

PyFrozenSet_Type (C 变量)

PyFunction_Check (C 函数)

PyFunction_GetAnnotations (C 函数)

PyFunction_GetClosure (C 函数)

PyFunction_GetCode (C 函数)

PyFunction_GetDefaults (C 函数)

PyFunction_GetGlobals (C 函数)

PyFunction_GetModule (C 函数)

PyFunction_New (C 函数)

PyFunction_NewWithQualName (C 函数)

PyFunction_SetAnnotations (C 函数)

PyFunction_SetClosure (C 函数)

PyFunction_SetDefaults (C 函数)

PyFunction_Type (C 变量)

PyFunctionObject (C 类型)

PYFUNCTYPE() (在 ctypes 模块中)

PyGen_Check (C 函数)

PyGen_CheckExact (C 函数)

PyGen_New (C 函数)

PyGen_NewWithQualName (C 函数)

PyGen_Type (C 变量)

PyGenObject (C 类型)

PyGetSetDef (C 类型)

PyGILState_Check (C 函数)

PyGILState_Ensure (C 函数)

PyGILState_GetThisThreadState (C 函数)

PyGILState_Release (C 函数)

PyImport_AddModule (C 函数)

PyImport_AddModuleObject (C 函数)

PyImport_AppendInittab (C 函数)

PyImport_Cleanup (C 函数)

PyImport_ExecCodeModule (C 函数)

PyImport_ExecCodeModuleEx (C 函数)

PyImport_ExecCodeModuleObject (C 函数)

PyImport_ExecCodeModuleWithPathnames (C 函数)

PyImport_ExtendInittab (C 函数)

PyImport_FrozenModules (C 变量)

PyImport_GetImporter (C 函数)

PyImport_GetMagicNumber (C 函数)

PyImport_GetMagicTag (C 函数)

PyImport_GetModuleDict (C 函数)

PyImport_Import (C 函数)

PyImport_ImportFrozenModule (C 函数)

PyImport_ImportFrozenModuleObject (C 函数)

PyImport_ImportModule (C 函数)

PyImport_ImportModuleEx (C 函数)

PyImport_ImportModuleLevel (C 函数)

PyImport_ImportModuleLevelObject (C 函数)

PyImport_ImportModuleNoBlock (C 函数)

PyImport_ReloadModule (C 函数)

PyIndex_Check (C 函数)

PyInit_modulename (C 函数)

PyInstanceMethod_Check (C 函数)

PyInstanceMethod_Function (C 函数)

PyInstanceMethod_GET_FUNCTION (C 函数)

PyInstanceMethod_New (C 函数)

PyInstanceMethod_Type (C 变量)

PyInterpreterState (C 类型)

PyInterpreterState_Clear (C 函数)

PyInterpreterState_Delete (C 函数)

PyInterpreterState_Head (C 函数)

PyInterpreterState_New (C 函数)

PyInterpreterState_Next (C 函数)

PyInterpreterState_ThreadHead (C 函数)

PyIter_Check (C 函数)

PyIter_Next (C 函数)

PyList_Append (C 函数)

PyList_AsTuple (C 函数)

PyList_Check (C 函数)

PyList_CheckExact (C 函数)

PyList_ClearFreeList (C 函数)

PyList_GET_ITEM (C 函数)

PyList_GET_SIZE (C 函数)

PyList_GetItem (C 函数)

PyList_GetItem()

PyList_GetSlice (C 函数)

PyList_Insert (C 函数)

PyList_New (C 函数)

PyList_Reverse (C 函数)

PyList_SET_ITEM (C 函数)

PyList_SetItem (C 函数)

PyList_SetItem()

PyList_SetSlice (C 函数)

PyList_Size (C 函数)

PyList_Sort (C 函数)

PyList_Type (C 变量)

PyListObject (C 类型)

PyLong_AsDouble (C 函数)

PyLong_AsLong (C 函数)

PyLong_AsLongAndOverflow (C 函数)

PyLong_AsLongLong (C 函数)

PyLong_AsLongLongAndOverflow (C 函数)

PyLong_AsSize_t (C 函数)

PyLong_AsSsize_t (C 函数)

PyLong_AsUnsignedLong (C 函数)

PyLong_AsUnsignedLongLong (C 函数)

PyLong_AsUnsignedLongLongMask (C 函数)

PyLong_AsUnsignedLongMask (C 函数)

PyLong_AsVoidPtr (C 函数)

PyLong_Check (C 函数)

PyLong_CheckExact (C 函数)

PyLong_FromDouble (C 函数)

PyLong_FromLong (C 函数)

PyLong_FromLongLong (C 函数)

PyLong_FromSize_t (C 函数)

PyLong_FromSsize_t (C 函数)

PyLong_FromString (C 函数)

PyLong_FromUnicode (C 函数)

PyLong_FromUnicodeObject (C 函数)

PyLong_FromUnsignedLong (C 函数)

PyLong_FromUnsignedLongLong (C 函数)

PyLong_FromVoidPtr (C 函数)

PyLong_Type (C 变量)

PyLongObject (C 类型)

PyMapping_Check (C 函数)

PyMapping_DelItem (C 函数)

PyMapping_DelItemString (C 函数)

PyMapping_GetItemString (C 函数)

PyMapping_HasKey (C 函数)

PyMapping_HasKeyString (C 函数)

PyMapping_Items (C 函数)

PyMapping_Keys (C 函数)

PyMapping_Length (C 函数)

PyMapping_SetItemString (C 函数)

PyMapping_Size (C 函数)

PyMapping_Values (C 函数)

PyMappingMethods (C 类型)

PyMappingMethods.mp_ass_subscript (C 成员)

PyMappingMethods.mp_length (C 成员)

PyMappingMethods.mp_subscript (C 成员)

PyMarshal_ReadLastObjectFromFile (C 函数)

PyMarshal_ReadLongFromFile (C 函数)

PyMarshal_ReadObjectFromFile (C 函数)

PyMarshal_ReadObjectFromString (C 函数)

PyMarshal_ReadShortFromFile (C 函数)

PyMarshal_WriteLongToFile (C 函数)

PyMarshal_WriteObjectToFile (C 函数)

PyMarshal_WriteObjectToString (C 函数)

PyMem_Calloc (C 函数)

PyMem_Del (C 函数)

PYMEM_DOMAIN_MEM (C 变量)

PYMEM_DOMAIN_OBJ (C 变量)

PYMEM_DOMAIN_RAW (C 变量)

PyMem_Free (C 函数)

PyMem_GetAllocator (C 函数)

PyMem_Malloc (C 函数)

PyMem_New (C 函数)

PyMem_RawCalloc (C 函数)

PyMem_RawFree (C 函数)

PyMem_RawMalloc (C 函数)

PyMem_RawRealloc (C 函数)

PyMem_Realloc (C 函数)

PyMem_Resize (C 函数)

PyMem_SetAllocator (C 函数)

PyMem_SetupDebugHooks (C 函数)

PyMemAllocatorDomain (C 类型)

PyMemAllocatorEx (C 类型)

PyMemberDef (C 类型)

PyMemoryView_Check (C 函数)

PyMemoryView_FromBuffer (C 函数)

PyMemoryView_FromMemory (C 函数)

PyMemoryView_FromObject (C 函数)

PyMemoryView_GET_BASE (C 函数)

PyMemoryView_GET_BUFFER (C 函数)

PyMemoryView_GetContiguous (C 函数)

PyMethod_Check (C 函数)

PyMethod_ClearFreeList (C 函数)

PyMethod_Function (C 函数)

PyMethod_GET_FUNCTION (C 函数)

PyMethod_GET_SELF (C 函数)

PyMethod_New (C 函数)

PyMethod_Self (C 函数)

PyMethod_Type (C 变量)

PyMethodDef (C 类型)

PyModule_AddFunctions (C 函数)

PyModule_AddIntConstant (C 函数)

PyModule_AddIntMacro (C 函数)

PyModule_AddObject (C 函数)

PyModule_AddStringConstant (C 函数)

PyModule_AddStringMacro (C 函数)

PyModule_Check (C 函数)

PyModule_CheckExact (C 函数)

PyModule_Create (C 函数)

PyModule_Create2 (C 函数)

PyModule_ExecDef (C 函数)

PyModule_FromDefAndSpec (C 函数)

PyModule_FromDefAndSpec2 (C 函数)

PyModule_GetDef (C 函数)

PyModule_GetDict (C 函数)

PyModule_GetFilename (C 函数)

PyModule_GetFilenameObject (C 函数)

PyModule_GetName (C 函数)

PyModule_GetNameObject (C 函数)

PyModule_GetState (C 函数)

PyModule_New (C 函数)

PyModule_NewObject (C 函数)

PyModule_SetDocString (C 函数)

PyModule_Type (C 变量)

PyModuleDef (C 类型)

PyModuleDef.m_base (C 成员)

PyModuleDef.m_clear (C 成员)

PyModuleDef.m_doc (C 成员)

PyModuleDef.m_free (C 成员)

PyModuleDef.m_methods (C 成员)

PyModuleDef.m_name (C 成员)

PyModuleDef.m_reload (C 成员)

PyModuleDef.m_size (C 成员)

PyModuleDef.m_slots (C 成员)

PyModuleDef.m_traverse (C 成员)

PyModuleDef_Init (C 函数)

PyModuleDef_Slot (C 类型)

PyModuleDef_Slot.slot (C 成员)

PyModuleDef_Slot.value (C 成员)

PyNumber_Absolute (C 函数)

PyNumber_Add (C 函数)

PyNumber_And (C 函数)

PyNumber_AsSsize_t (C 函数)

PyNumber_Check (C 函数)

PyNumber_Divmod (C 函数)

PyNumber_Float (C 函数)

PyNumber_FloorDivide (C 函数)

PyNumber_Index (C 函数)

PyNumber_InPlaceAdd (C 函数)

PyNumber_InPlaceAnd (C 函数)

PyNumber_InPlaceFloorDivide (C 函数)

PyNumber_InPlaceLshift (C 函数)

PyNumber_InPlaceMatrixMultiply (C 函数)

PyNumber_InPlaceMultiply (C 函数)

PyNumber_InPlaceOr (C 函数)

PyNumber_InPlacePower (C 函数)

PyNumber_InPlaceRemainder (C 函数)

PyNumber_InPlaceRshift (C 函数)

PyNumber_InPlaceSubtract (C 函数)

PyNumber_InPlaceTrueDivide (C 函数)

PyNumber_InPlaceXor (C 函数)

PyNumber_Invert (C 函数)

PyNumber_Long (C 函数)

PyNumber_Lshift (C 函数)

PyNumber_MatrixMultiply (C 函数)

PyNumber_Multiply (C 函数)

PyNumber_Negative (C 函数)

PyNumber_Or (C 函数)

PyNumber_Positive (C 函数)

PyNumber_Power (C 函数)

PyNumber_Remainder (C 函数)

PyNumber_Rshift (C 函数)

PyNumber_Subtract (C 函数)

PyNumber_ToBase (C 函数)

PyNumber_TrueDivide (C 函数)

PyNumber_Xor (C 函数)

PyNumberMethods (C 类型)

PyObject (C 类型)

PyObject._ob_next (C 成员)

PyObject._ob_prev (C 成员)

PyObject.ob_refcnt (C 成员)

PyObject.ob_type (C 成员)

PyObject_AsCharBuffer (C 函数)

PyObject_ASCII (C 函数)

PyObject_AsFileDescriptor (C 函数)

PyObject_AsReadBuffer (C 函数)

PyObject_AsWriteBuffer (C 函数)

PyObject_Bytes (C 函数)

PyObject_Call (C 函数)

PyObject_CallFunction (C 函数)

PyObject_CallFunctionObjArgs (C 函数)

PyObject_CallMethod (C 函数)

PyObject_CallMethodObjArgs (C 函数)

PyObject_CallObject (C 函数)

PyObject_CallObject()

PyObject_Calloc (C 函数)

PyObject_CheckBuffer (C 函数)

PyObject_CheckReadBuffer (C 函数)

PyObject_Del (C 函数)

PyObject_DelAttr (C 函数)

PyObject_DelAttrString (C 函数)

PyObject_DelItem (C 函数)

PyObject_Dir (C 函数)

PyObject_Free (C 函数)

PyObject_GC_Del (C 函数)

PyObject_GC_New (C 函数)

PyObject_GC_NewVar (C 函数)

PyObject_GC_Resize (C 函数)

PyObject_GC_Track (C 函数)

PyObject_GC_UnTrack (C 函数)

PyObject_GenericGetAttr (C 函数)

PyObject_GenericGetDict (C 函数)

PyObject_GenericSetAttr (C 函数)

PyObject_GenericSetDict (C 函数)

PyObject_GetArenaAllocator (C 函数)

PyObject_GetAttr (C 函数)

PyObject_GetAttrString (C 函数)

PyObject_GetBuffer (C 函数)

PyObject_GetItem (C 函数)

PyObject_GetIter (C 函数)

PyObject_HasAttr (C 函数)

PyObject_HasAttrString (C 函数)

PyObject_Hash (C 函数)

PyObject_HashNotImplemented (C 函数)

PyObject_HEAD (C 宏)

PyObject_HEAD_INIT (C 宏)

PyObject_Init (C 函数)

PyObject_InitVar (C 函数)

PyObject_IsInstance (C 函数)

PyObject_IsSubclass (C 函数)

PyObject_IsTrue (C 函数)

PyObject_Length (C 函数)

PyObject_LengthHint (C 函数)

PyObject_Malloc (C 函数)

PyObject_New (C 函数)

PyObject_NewVar (C 函数)

PyObject_Not (C 函数)

PyObject_Print (C 函数)

PyObject_Realloc (C 函数)

PyObject_Repr (C 函数)

PyObject_RichCompare (C 函数)

PyObject_RichCompareBool (C 函数)

PyObject_SetArenaAllocator (C 函数)

PyObject_SetAttr (C 函数)

PyObject_SetAttrString (C 函数)

PyObject_SetItem (C 函数)

PyObject_Size (C 函数)

PyObject_Str (C 函数)

PyObject_Type (C 函数)

PyObject_TypeCheck (C 函数)

PyObject_VAR_HEAD (C 宏)

PyObjectArenaAllocator (C 类型)

PyOS_AfterFork (C 函数)

PyOS_CheckStack (C 函数)

PyOS_double_to_string (C 函数)

PyOS_FSPath (C 函数)

PyOS_getsig (C 函数)

PyOS_InputHook (C 变量)

PyOS_ReadlineFunctionPointer (C 变量)

PyOS_setsig (C 函数)

PyOS_snprintf (C 函数)

PyOS_stricmp (C 函数)

PyOS_string_to_double (C 函数)

PyOS_strnicmp (C 函数)

PyOS_vsnprintf (C 函数)

PyParser_SimpleParseFile (C 函数)

PyParser_SimpleParseFileFlags (C 函数)

PyParser_SimpleParseString (C 函数)

PyParser_SimpleParseStringFlags (C 函数)

PyParser_SimpleParseStringFlagsFilename (C 函数)

PyPI

(see Python Package Index (PyPI))

PyProperty_Type (C 变量)

PyRun_AnyFile (C 函数)

PyRun_AnyFileEx (C 函数)

PyRun_AnyFileExFlags (C 函数)

PyRun_AnyFileFlags (C 函数)

PyRun_File (C 函数)

PyRun_FileEx (C 函数)

PyRun_FileExFlags (C 函数)

PyRun_FileFlags (C 函数)

PyRun_InteractiveLoop (C 函数)

PyRun_InteractiveLoopFlags (C 函数)

PyRun_InteractiveOne (C 函数)

PyRun_InteractiveOneFlags (C 函数)

PyRun_SimpleFile (C 函数)

PyRun_SimpleFileEx (C 函数)

PyRun_SimpleFileExFlags (C 函数)

PyRun_SimpleString (C 函数)

PyRun_SimpleStringFlags (C 函数)

PyRun_String (C 函数)

PyRun_StringFlags (C 函数)

PySeqIter_Check (C 函数)

PySeqIter_New (C 函数)

PySeqIter_Type (C 变量)

PySequence_Check (C 函数)

PySequence_Concat (C 函数)

PySequence_Contains (C 函数)

PySequence_Count (C 函数)

PySequence_DelItem (C 函数)

PySequence_DelSlice (C 函数)

PySequence_Fast (C 函数)

PySequence_Fast_GET_ITEM (C 函数)

PySequence_Fast_GET_SIZE (C 函数)

PySequence_Fast_ITEMS (C 函数)

PySequence_GetItem (C 函数)

PySequence_GetItem()

PySequence_GetSlice (C 函数)

PySequence_Index (C 函数)

PySequence_InPlaceConcat (C 函数)

PySequence_InPlaceRepeat (C 函数)

PySequence_ITEM (C 函数)

PySequence_Length (C 函数)

PySequence_List (C 函数)

PySequence_Repeat (C 函数)

PySequence_SetItem (C 函数)

PySequence_SetSlice (C 函数)

PySequence_Size (C 函数)

PySequence_Tuple (C 函数)

PySequenceMethods (C 类型)

PySequenceMethods.sq_ass_item (C 成员)

PySequenceMethods.sq_concat (C 成员)

PySequenceMethods.sq_contains (C 成员)

PySequenceMethods.sq_inplace_concat (C 成员)

PySequenceMethods.sq_inplace_repeat (C 成员)

PySequenceMethods.sq_item (C 成员)

PySequenceMethods.sq_length (C 成员)

PySequenceMethods.sq_repeat (C 成员)

PySet_Add (C 函数)

PySet_Check (C 函数)

PySet_Clear (C 函数)

PySet_ClearFreeList (C 函数)

PySet_Contains (C 函数)

PySet_Discard (C 函数)

PySet_GET_SIZE (C 函数)

PySet_New (C 函数)

PySet_Pop (C 函数)

PySet_Size (C 函数)

PySet_Type (C 变量)

PySetObject (C 类型)

PySignal_SetWakeupFd (C 函数)

PySlice_Check (C 函数)

PySlice_GetIndices (C 函数)

PySlice_GetIndicesEx (C 函数)

PySlice_New (C 函数)

PySlice_Type (C 变量)

PyState_AddModule (C 函数)

PyState_FindModule (C 函数)

PyState_RemoveModule (C 函数)

PyStructSequence_Desc (C 类型)

PyStructSequence_Field (C 类型)

PyStructSequence_GET_ITEM (C 函数)

PyStructSequence_GetItem (C 函数)

PyStructSequence_InitType (C 函数)

PyStructSequence_InitType2 (C 函数)

PyStructSequence_New (C 函数)

PyStructSequence_NewType (C 函数)

PyStructSequence_SET_ITEM (C 函数)

PyStructSequence_SetItem (C 函数)

PyStructSequence_UnnamedField (C 变量)

PySys_AddWarnOption (C 函数)

PySys_AddWarnOptionUnicode (C 函数)

PySys_AddXOption (C 函数)

PySys_FormatStderr (C 函数)

PySys_FormatStdout (C 函数)

PySys_GetObject (C 函数)

PySys_GetXOptions (C 函数)

PySys_ResetWarnOptions (C 函数)

PySys_SetArgv (C 函数)

PySys_SetArgv()

PySys_SetArgvEx (C 函数)

PySys_SetArgvEx(), [1]

PySys_SetObject (C 函数)

PySys_SetPath (C 函数)

PySys_WriteStderr (C 函数)

PySys_WriteStdout (C 函数)

Python 3000

Python Editor

Python Package Index (PyPI)

.pypirc file

Python 提高建议

PEP 1

PEP 100

PEP 11, [1], [2], [3]

PEP 205, [1]

PEP 207

PEP 208

PEP 217

PEP 218, [1], [2]

PEP 227, [1], [2]

PEP 229

PEP 230, [1]

PEP 232

PEP 234

PEP 235

PEP 236, [1]

PEP 237, [1], [2], [3], [4], [5]

PEP 238, [1], [2], [3], [4], [5], [6]

PEP 241

PEP 243

PEP 247

PEP 249, [1], [2], [3]

PEP 252, [1]

PEP 253, [1], [2], [3], [4]

PEP 255, [1], [2], [3], [4], [5]

PEP 261, [1]

PEP 263, [1], [2], [3], [4], [5]

PEP 264

PEP 273, [1], [2]

PEP 274

PEP 275

PEP 277

PEP 278, [1]

PEP 279

PEP 282, [1], [2], [3]

PEP 285, [1]

PEP 288

PEP 289, [1], [2]

PEP 292, [1]

PEP 293

PEP 3000

PEP 301, [1]

PEP 302, [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], [26]

PEP 307, [1], [2], [3]

PEP 308, [1], [2]

PEP 309

PEP 3100

PEP 3101, [1], [2], [3], [4]

PEP 3102

PEP 3104, [1]

PEP 3105, [1], [2]

PEP 3106

PEP 3107, [1], [2], [3]

PEP 3108, [1], [2]

PEP 3109, [1]

PEP 3110, [1], [2]

PEP 3111

PEP 3112, [1]

PEP 3113

PEP 3114

PEP 3115, [1], [2], [3]

PEP 3116, [1], [2]

PEP 3118, [1], [2], [3], [4], [5]

PEP 3119, [1], [2], [3], [4], [5]

PEP 3120, [1], [2]

PEP 3121, [1], [2]

PEP 3123

PEP 3127

PEP 3129, [1]

PEP 3131, [1], [2]

PEP 3132, [1]

PEP 3134, [1], [2]

PEP 3135, [1]

PEP 3137

PEP 3138

PEP 314, [1]

PEP 3141, [1], [2]

PEP 3147, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21]

PEP 3148, [1]

PEP 3149, [1]

PEP 3151, [1], [2], [3], [4], [5]

PEP 3153

PEP 3154, [1], [2]

PEP 3155, [1]

PEP 3156, [1], [2], [3], [4], [5]

PEP 318, [1], [2], [3]

PEP 322, [1]

PEP 324, [1]

PEP 325

PEP 327

PEP 328, [1], [2], [3], [4], [5], [6], [7], [8], [9]

PEP 331

PEP 333, [1]

PEP 3333, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14]

PEP 338, [1], [2], [3]

PEP 339

PEP 341

PEP 342, [1], [2], [3], [4], [5], [6]

PEP 343, [1], [2], [3], [4], [5], [6]

PEP 347

PEP 352, [1], [2]

PEP 353, [1], [2]

PEP 356

PEP 357

PEP 361

PEP 362, [1], [2], [3]

PEP 366, [1], [2], [3], [4], [5], [6]

PEP 370, [1], [2], [3], [4]

PEP 371

PEP 372, [1]

PEP 373

PEP 378, [1], [2]

PEP 380, [1], [2], [3]

PEP 383, [1], [2], [3], [4]

PEP 384, [1]

PEP 385

PEP 389, [1]

PEP 391, [1]

PEP 392

PEP 393, [1], [2], [3], [4], [5], [6], [7], [8], [9]

PEP 395

PEP 397, [1], [2], [3]

PEP 398

PEP 4

PEP 405, [1]

PEP 409

PEP 411, [1], [2], [3], [4]

PEP 412

PEP 414, [1]

PEP 418

PEP 420, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]

PEP 421, [1]

PEP 424, [1]

PEP 427

PEP 428, [1], [2]

PEP 429

PEP 434

PEP 435, [1], [2]

PEP 436, [1], [2]

PEP 441, [1]

PEP 442, [1], [2], [3], [4], [5], [6]

PEP 443, [1], [2]

PEP 445, [1], [2], [3]

PEP 446, [1], [2], [3]

PEP 448, [1], [2], [3], [4]

PEP 450, [1], [2]

PEP 451, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]

PEP 453, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10]

PEP 454, [1], [2]

PEP 456, [1]

PEP 461, [1], [2]

PEP 465, [1], [2]

PEP 466, [1], [2], [3]

PEP 468, [1]

PEP 471, [1]

PEP 475, [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], [26]

PEP 476

PEP 477

PEP 478

PEP 479, [1], [2]

PEP 483, [1]

PEP 484, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16]

PEP 485, [1], [2], [3]

PEP 486, [1]

PEP 487, [1], [2], [3]

PEP 488, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]

PEP 489, [1], [2], [3], [4], [5], [6], [7], [8], [9]

PEP 492, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20]

PEP 493

PEP 494

PEP 495, [1]

PEP 498, [1], [2], [3]

PEP 5, [1]

PEP 506, [1]

PEP 511

PEP 515, [1], [2]

PEP 519, [1]

PEP 520

PEP 523, [1]

PEP 524, [1], [2], [3], [4], [5]

PEP 525, [1], [2], [3], [4], [5], [6]

PEP 526, [1], [2], [3], [4], [5], [6], [7]

PEP 528

PEP 529, [1], [2], [3], [4]

PEP 530, [1], [2]

PEP 6

PEP 628, [1]

PEP 7

PEP 8, [1], [2], [3], [4]

PYTHON*, [1], [2]

python.function.entry (C 函数)

python.function.return (C 函数)

python_branch() (在 platform 模块中)

python_build() (在 platform 模块中)

python_compiler() (在 platform 模块中)

PYTHON_DOM

python_implementation() (在 platform 模块中)

python_revision() (在 platform 模块中)

python_version() (在 platform 模块中)

python_version_tuple() (在 platform 模块中)

PYTHONASYNCIODEBUG, [1]

PYTHONCASEOK

PYTHONDEBUG

PYTHONDOCS

PYTHONDONTWRITEBYTECODE, [1], [2], [3], [4]

PYTHONDUMPREFS

PYTHONFAULTHANDLER, [1]

PYTHONHASHSEED, [1], [2], [3], [4]

PYTHONHOME, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13]

Pythonic

PYTHONINSPECT, [1]

PYTHONIOENCODING, [1], [2], [3], [4]

PYTHONLEGACYWINDOWSFSENCODING, [1]

PYTHONLEGACYWINDOWSSTDIO, [1]

PYTHONMALLOC, [1], [2], [3], [4], [5]

PYTHONMALLOCSTATS, [1]

PYTHONNOUSERSITE, [1], [2]

PYTHONOPTIMIZE

PYTHONPATH, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22]

PYTHONSHOWALLOCCOUNT

PYTHONSHOWREFCOUNT

PYTHONSTARTUP, [1], [2], [3], [4], [5], [6], [7], [8]

PYTHONTRACEMALLOC, [1], [2]

PYTHONUNBUFFERED

PYTHONUSERBASE, [1], [2]

PYTHONVERBOSE

PYTHONWARNINGS, [1], [2], [3]

PyThreadState, [1]

(C 类型)

PyThreadState_Clear (C 函数)

PyThreadState_Delete (C 函数)

PyThreadState_Get (C 函数)

PyThreadState_GetDict (C 函数)

PyThreadState_New (C 函数)

PyThreadState_Next (C 函数)

PyThreadState_SetAsyncExc (C 函数)

PyThreadState_Swap (C 函数)

PyTime_Check (C 函数)

PyTime_CheckExact (C 函数)

PyTime_FromTime (C 函数)

PyTrace_C_CALL (C 变量)

PyTrace_C_EXCEPTION (C 变量)

PyTrace_C_RETURN (C 变量)

PyTrace_CALL (C 变量)

PyTrace_EXCEPTION (C 变量)

PyTrace_LINE (C 变量)

PyTrace_RETURN (C 变量)

PyTuple_Check (C 函数)

PyTuple_CheckExact (C 函数)

PyTuple_ClearFreeList (C 函数)

PyTuple_GET_ITEM (C 函数)

PyTuple_GET_SIZE (C 函数)

PyTuple_GetItem (C 函数)

PyTuple_GetSlice (C 函数)

PyTuple_New (C 函数)

PyTuple_Pack (C 函数)

PyTuple_SET_ITEM (C 函数)

PyTuple_SetItem (C 函数)

PyTuple_SetItem()

PyTuple_Size (C 函数)

PyTuple_Type (C 变量)

PyTupleObject (C 类型)

PyType_Check (C 函数)

PyType_CheckExact (C 函数)

PyType_ClearCache (C 函数)

PyType_FromSpec (C 函数)

PyType_FromSpecWithBases (C 函数)

PyType_GenericAlloc (C 函数)

PyType_GenericNew (C 函数)

PyType_GetFlags (C 函数)

PyType_GetSlot (C 函数)

PyType_HasFeature (C 函数)

PyType_IS_GC (C 函数)

PyType_IsSubtype (C 函数)

PyType_Modified (C 函数)

PyType_Ready (C 函数)

PyType_Type (C 变量)

PyTypeObject (C 类型)

PyTypeObject.tp_alloc (C 成员)

PyTypeObject.tp_allocs (C 成员)

PyTypeObject.tp_as_buffer (C 成员)

PyTypeObject.tp_base (C 成员)

PyTypeObject.tp_bases (C 成员)

PyTypeObject.tp_basicsize (C 成员)

PyTypeObject.tp_cache (C 成员)

PyTypeObject.tp_call (C 成员)

PyTypeObject.tp_clear (C 成员)

PyTypeObject.tp_dealloc (C 成员)

PyTypeObject.tp_descr_get (C 成员)

PyTypeObject.tp_descr_set (C 成员)

PyTypeObject.tp_dict (C 成员)

PyTypeObject.tp_dictoffset (C 成员)

PyTypeObject.tp_doc (C 成员)

PyTypeObject.tp_finalize (C 成员)

PyTypeObject.tp_flags (C 成员)

PyTypeObject.tp_free (C 成员)

PyTypeObject.tp_frees (C 成员)

PyTypeObject.tp_getattr (C 成员)

PyTypeObject.tp_getattro (C 成员)

PyTypeObject.tp_getset (C 成员)

PyTypeObject.tp_hash (C 成员)

PyTypeObject.tp_init (C 成员)

PyTypeObject.tp_is_gc (C 成员)

PyTypeObject.tp_itemsize (C 成员)

PyTypeObject.tp_iter (C 成员)

PyTypeObject.tp_iternext (C 成员)

PyTypeObject.tp_maxalloc (C 成员)

PyTypeObject.tp_members (C 成员)

PyTypeObject.tp_methods (C 成员)

PyTypeObject.tp_mro (C 成员)

PyTypeObject.tp_name (C 成员)

PyTypeObject.tp_new (C 成员)

PyTypeObject.tp_next (C 成员)

PyTypeObject.tp_print (C 成员)

PyTypeObject.tp_repr (C 成员)

PyTypeObject.tp_richcompare (C 成员)

PyTypeObject.tp_setattr (C 成员)

PyTypeObject.tp_setattro (C 成员)

PyTypeObject.tp_str (C 成员)

PyTypeObject.tp_subclasses (C 成员)

PyTypeObject.tp_traverse (C 成员)

PyTypeObject.tp_weaklist (C 成员)

PyTypeObject.tp_weaklistoffset (C 成员)

PyTZInfo_Check (C 函数)

PyTZInfo_CheckExact (C 函数)

PyUnicode_1BYTE_DATA (C 函数)

PyUnicode_1BYTE_KIND (C 宏)

PyUnicode_2BYTE_DATA (C 函数)

PyUnicode_2BYTE_KIND (C 宏)

PyUnicode_4BYTE_DATA (C 函数)

PyUnicode_4BYTE_KIND (C 宏)

PyUnicode_AS_DATA (C 函数)

PyUnicode_AS_UNICODE (C 函数)

PyUnicode_AsASCIIString (C 函数)

PyUnicode_AsCharmapString (C 函数)

PyUnicode_AsEncodedString (C 函数)

PyUnicode_AsLatin1String (C 函数)

PyUnicode_AsMBCSString (C 函数)

PyUnicode_AsRawUnicodeEscapeString (C 函数)

PyUnicode_AsUCS4 (C 函数)

PyUnicode_AsUCS4Copy (C 函数)

PyUnicode_AsUnicode (C 函数)

PyUnicode_AsUnicodeAndSize (C 函数)

PyUnicode_AsUnicodeCopy (C 函数)

PyUnicode_AsUnicodeEscapeString (C 函数)

PyUnicode_AsUTF16String (C 函数)

PyUnicode_AsUTF32String (C 函数)

PyUnicode_AsUTF8 (C 函数)

PyUnicode_AsUTF8AndSize (C 函数)

PyUnicode_AsUTF8String (C 函数)

PyUnicode_AsWideChar (C 函数)

PyUnicode_AsWideCharString (C 函数)

PyUnicode_Check (C 函数)

PyUnicode_CheckExact (C 函数)

PyUnicode_ClearFreeList (C 函数)

PyUnicode_Compare (C 函数)

PyUnicode_CompareWithASCIIString (C 函数)

PyUnicode_Concat (C 函数)

PyUnicode_Contains (C 函数)

PyUnicode_CopyCharacters (C 函数)

PyUnicode_Count (C 函数)

PyUnicode_DATA (C 函数)

PyUnicode_Decode (C 函数)

PyUnicode_DecodeASCII (C 函数)

PyUnicode_DecodeCharmap (C 函数)

PyUnicode_DecodeFSDefault (C 函数)

PyUnicode_DecodeFSDefaultAndSize (C 函数)

PyUnicode_DecodeLatin1 (C 函数)

PyUnicode_DecodeLocale (C 函数)

PyUnicode_DecodeLocaleAndSize (C 函数)

PyUnicode_DecodeMBCS (C 函数)

PyUnicode_DecodeMBCSStateful (C 函数)

PyUnicode_DecodeRawUnicodeEscape (C 函数)

PyUnicode_DecodeUnicodeEscape (C 函数)

PyUnicode_DecodeUTF16 (C 函数)

PyUnicode_DecodeUTF16Stateful (C 函数)

PyUnicode_DecodeUTF32 (C 函数)

PyUnicode_DecodeUTF32Stateful (C 函数)

PyUnicode_DecodeUTF7 (C 函数)

PyUnicode_DecodeUTF7Stateful (C 函数)

PyUnicode_DecodeUTF8 (C 函数)

PyUnicode_DecodeUTF8Stateful (C 函数)

PyUnicode_Encode (C 函数)

PyUnicode_EncodeASCII (C 函数)

PyUnicode_EncodeCharmap (C 函数)

PyUnicode_EncodeCodePage (C 函数)

PyUnicode_EncodeFSDefault (C 函数)

PyUnicode_EncodeLatin1 (C 函数)

PyUnicode_EncodeLocale (C 函数)

PyUnicode_EncodeMBCS (C 函数)

PyUnicode_EncodeRawUnicodeEscape (C 函数)

PyUnicode_EncodeUnicodeEscape (C 函数)

PyUnicode_EncodeUTF16 (C 函数)

PyUnicode_EncodeUTF32 (C 函数)

PyUnicode_EncodeUTF7 (C 函数)

PyUnicode_EncodeUTF8 (C 函数)

PyUnicode_Fill (C 函数)

PyUnicode_Find (C 函数)

PyUnicode_FindChar (C 函数)

PyUnicode_Format (C 函数)

PyUnicode_FromEncodedObject (C 函数)

PyUnicode_FromFormat (C 函数)

PyUnicode_FromFormatV (C 函数)

PyUnicode_FromKindAndData (C 函数)

PyUnicode_FromObject (C 函数)

PyUnicode_FromString (C 函数)

PyUnicode_FromString()

PyUnicode_FromStringAndSize (C 函数)

PyUnicode_FromUnicode (C 函数)

PyUnicode_FromWideChar (C 函数)

PyUnicode_FSConverter (C 函数)

PyUnicode_FSDecoder (C 函数)

PyUnicode_GET_DATA_SIZE (C 函数)

PyUnicode_GET_LENGTH (C 函数)

PyUnicode_GET_SIZE (C 函数)

PyUnicode_GetLength (C 函数)

PyUnicode_GetSize (C 函数)

PyUnicode_InternFromString (C 函数)

PyUnicode_InternInPlace (C 函数)

PyUnicode_Join (C 函数)

PyUnicode_KIND (C 函数)

PyUnicode_MAX_CHAR_VALUE (C 函数)

PyUnicode_New (C 函数)

PyUnicode_READ (C 函数)

PyUnicode_READ_CHAR (C 函数)

PyUnicode_ReadChar (C 函数)

PyUnicode_READY (C 函数)

PyUnicode_Replace (C 函数)

PyUnicode_RichCompare (C 函数)

PyUnicode_Split (C 函数)

PyUnicode_Splitlines (C 函数)

PyUnicode_Substring (C 函数)

PyUnicode_Tailmatch (C 函数)

PyUnicode_TransformDecimalToASCII (C 函数)

PyUnicode_Translate (C 函数), [1]

PyUnicode_TranslateCharmap (C 函数)

PyUnicode_Type (C 变量)

PyUnicode_WCHAR_KIND (C 宏)

PyUnicode_WRITE (C 函数)

PyUnicode_WriteChar (C 函数)

PyUnicodeDecodeError_Create (C 函数)

PyUnicodeDecodeError_GetEncoding (C 函数)

PyUnicodeDecodeError_GetEnd (C 函数)

PyUnicodeDecodeError_GetObject (C 函数)

PyUnicodeDecodeError_GetReason (C 函数)

PyUnicodeDecodeError_GetStart (C 函数)

PyUnicodeDecodeError_SetEnd (C 函数)

PyUnicodeDecodeError_SetReason (C 函数)

PyUnicodeDecodeError_SetStart (C 函数)

PyUnicodeEncodeError_Create (C 函数)

PyUnicodeEncodeError_GetEncoding (C 函数)

PyUnicodeEncodeError_GetEnd (C 函数)

PyUnicodeEncodeError_GetObject (C 函数)

PyUnicodeEncodeError_GetReason (C 函数)

PyUnicodeEncodeError_GetStart (C 函数)

PyUnicodeEncodeError_SetEnd (C 函数)

PyUnicodeEncodeError_SetReason (C 函数)

PyUnicodeEncodeError_SetStart (C 函数)

PyUnicodeObject (C 类型)

PyUnicodeTranslateError_Create (C 函数)

PyUnicodeTranslateError_GetEnd (C 函数)

PyUnicodeTranslateError_GetObject (C 函数)

PyUnicodeTranslateError_GetReason (C 函数)

PyUnicodeTranslateError_GetStart (C 函数)

PyUnicodeTranslateError_SetEnd (C 函数)

PyUnicodeTranslateError_SetReason (C 函数)

PyUnicodeTranslateError_SetStart (C 函数)

PyVarObject (C 类型)

PyVarObject.ob_size (C 成员)

PyVarObject_HEAD_INIT (C 宏)

PyWeakref_Check (C 函数)

PyWeakref_CheckProxy (C 函数)

PyWeakref_CheckRef (C 函数)

PyWeakref_GET_OBJECT (C 函数)

PyWeakref_GetObject (C 函数)

PyWeakref_NewProxy (C 函数)

PyWeakref_NewRef (C 函数)

PyWrapper_New (C 函数)

PyZipFile (zipfile 中的类)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – Q

qiflush() (在 curses 模块中)

QName (xml.etree.ElementTree 中的类)

qsize() (asyncio.Queue 方法)

(multiprocessing.Queue 方法)

(queue.Queue 方法)

qualified name -- 限定名称

quantize() (decimal.Context 方法)

(decimal.Decimal 方法)

QueryInfoKey() (在 winreg 模块中)

QueryReflectionKey() (在 winreg 模块中)

QueryValue() (在 winreg 模块中)

QueryValueEx() (在 winreg 模块中)

Queue (asyncio 中的类)

(multiprocessing 中的类)

(queue 中的类)

queue (sched.scheduler 属性)

(模块)

Queue() (multiprocessing.managers.SyncManager 方法)

QueueEmpty

QueueFull

QueueHandler (logging.handlers 中的类)

QueueListener (logging.handlers 中的类)

quick_ratio() (difflib.SequenceMatcher 方法)

quit (pdb command)

(內置变量)

quit() (ftplib.FTP 方法)

(nntplib.NNTP 方法)

(poplib.POP3 方法)

(smtplib.SMTP 方法)

quopri (模块)

quote() (在 email.utils 模块中)

(在 shlex 模块中)

(在 urllib.parse 模块中)

QUOTE_ALL() (在 csv 模块中)

quote_from_bytes() (在 urllib.parse 模块中)

QUOTE_MINIMAL() (在 csv 模块中)

QUOTE_NONE() (在 csv 模块中)

QUOTE_NONNUMERIC() (在 csv 模块中)

quote_plus() (在 urllib.parse 模块中)

quoteattr() (在 xml.sax.saxutils 模块中)

quotechar (csv.Dialect 属性)

quoted-printable

encoding

quotes (shlex.shlex 属性)

quoting (csv.Dialect 属性)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – R

r"

raw string literal

r'

raw string literal

R_OK() (在 os 模块中)

radians() (在 math 模块中)

(在 turtle 模块中)

RadioButtonGroup (msilib 中的类)

radiogroup() (msilib.Dialog 方法)

radix() (decimal.Context 方法)

(decimal.Decimal 方法)

RADIXCHAR() (在 locale 模块中)

raise

语句, [1]

raise (2to3 fixer)

raise an exception

raise_on_defect (email.policy.Policy 属性)

RAISE_VARARGS (opcode)

raising

exception

RAND_add() (在 ssl 模块中)

RAND_bytes() (在 ssl 模块中)

RAND_egd() (在 ssl 模块中)

RAND_pseudo_bytes() (在 ssl 模块中)

RAND_status() (在 ssl 模块中)

randbelow() (在 secrets 模块中)

randbits() (在 secrets 模块中)

randint() (在 random 模块中)

random (模块)

random() (在 random 模块中)

randrange() (在 random 模块中)

range

內置函数

对象

range (內置类)

RARROW() (在 token 模块中)

ratecv() (在 audioop 模块中)

ratio() (difflib.SequenceMatcher 方法)

Rational (numbers 中的类)

raw (io.BufferedIOBase 属性)

raw string

raw() (在 curses 模块中)

raw_data_manager() (在 email.contentmanager 模块中)

raw_decode() (json.JSONDecoder 方法)

raw_input (2to3 fixer)

raw_input() (code.InteractiveConsole 方法)

RawArray() (在 multiprocessing.sharedctypes 模块中)

RawConfigParser (configparser 中的类)

RawDescriptionHelpFormatter (argparse 中的类)

RawIOBase (io 中的类)

RawPen (turtle 中的类)

RawTextHelpFormatter (argparse 中的类)

RawTurtle (turtle 中的类)

RawValue() (在 multiprocessing.sharedctypes 模块中)

RBRACE() (在 token 模块中)

rcpttos (smtpd.SMTPChannel 属性)

re

模块, [1]

re (re.match 属性)

(模块)

read() (asyncio.StreamReader 方法)

(chunk.Chunk 方法)

(codecs.StreamReader 方法)

(configparser.ConfigParser 方法)

(http.client.HTTPResponse 方法)

(imaplib.IMAP4 方法)

(io.BufferedIOBase 方法)

(io.BufferedReader 方法)

(io.RawIOBase 方法)

(io.TextIOBase 方法)

(mimetypes.MimeTypes 方法)

(mmap.mmap 方法)

(ossaudiodev.oss_audio_device 方法)

(ssl.MemoryBIO 方法)

(ssl.SSLSocket 方法)

(urllib.robotparser.RobotFileParser 方法)

(zipfile.ZipFile 方法)

(在 os 模块中)

read1() (io.BufferedIOBase 方法)

(io.BufferedReader 方法)

(io.BytesIO 方法)

read_all() (telnetlib.Telnet 方法)

read_byte() (mmap.mmap 方法)

read_bytes() (pathlib.Path 方法)

read_dict() (configparser.ConfigParser 方法)

read_eager() (telnetlib.Telnet 方法)

read_environ() (在 wsgiref.handlers 模块中)

read_events() (xml.etree.ElementTree.XMLPullParser 方法)

read_file() (configparser.ConfigParser 方法)

read_history_file() (在 readline 模块中)

read_init_file() (在 readline 模块中)

read_lazy() (telnetlib.Telnet 方法)

read_mime_types() (在 mimetypes 模块中)

READ_RESTRICTED

read_sb_data() (telnetlib.Telnet 方法)

read_some() (telnetlib.Telnet 方法)

read_string() (configparser.ConfigParser 方法)

read_text() (pathlib.Path 方法)

read_token() (shlex.shlex 方法)

read_until() (telnetlib.Telnet 方法)

read_very_eager() (telnetlib.Telnet 方法)

read_very_lazy() (telnetlib.Telnet 方法)

read_windows_registry() (mimetypes.MimeTypes 方法)

readable() (asyncore.dispatcher 方法)

(io.IOBase 方法)

READABLE() (在 tkinter 模块中)

readall() (io.RawIOBase 方法)

reader() (在 csv 模块中)

ReadError

readexactly() (asyncio.StreamReader 方法)

readfp() (configparser.ConfigParser 方法)

(mimetypes.MimeTypes 方法)

readframes() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

readinto() (http.client.HTTPResponse 方法)

(io.BufferedIOBase 方法)

(io.RawIOBase 方法)

readinto1() (io.BufferedIOBase 方法)

(io.BytesIO 方法)

readline (模块)

readline() (asyncio.StreamReader 方法)

(codecs.StreamReader 方法)

(distutils.text_file.TextFile 方法)

(imaplib.IMAP4 方法)

(io.IOBase 方法)

(io.TextIOBase 方法)

(mmap.mmap 方法)

readlines() (codecs.StreamReader 方法)

(distutils.text_file.TextFile 方法)

(io.IOBase 方法)

readlink() (在 os 模块中)

readmodule() (在 pyclbr 模块中)

readmodule_ex() (在 pyclbr 模块中)

READONLY

readonly (memoryview 属性)

readPlist() (在 plistlib 模块中)

readPlistFromBytes() (在 plistlib 模块中)

ReadTransport (asyncio 中的类)

readuntil() (asyncio.StreamReader 方法)

readv() (在 os 模块中)

ready() (multiprocessing.pool.AsyncResult 方法)

Real (numbers 中的类)

real (numbers.Complex 属性)

Real Media File Format

real_quick_ratio() (difflib.SequenceMatcher 方法)

realloc()

realpath() (在 os.path 模块中)

reason (http.client.HTTPResponse 属性)

(ssl.SSLError 属性)

(UnicodeError 属性)

(urllib.error.HTTPError 属性)

(urllib.error.URLError 属性)

reattach() (tkinter.ttk.Treeview 方法)

rebinding

name

reccontrols() (ossaudiodev.oss_mixer_device 方法)

received_data (smtpd.SMTPChannel 属性)

received_lines (smtpd.SMTPChannel 属性)

recent() (imaplib.IMAP4 方法)

records (unittest.TestCase 属性)

rect() (在 cmath 模块中)

rectangle() (在 curses.textpad 模块中)

RecursionError

recursive_repr() (在 reprlib 模块中)

recv() (asyncore.dispatcher 方法)

(multiprocessing.connection.Connection 方法)

(socket.socket 方法)

recv_bytes() (multiprocessing.connection.Connection 方法)

recv_bytes_into() (multiprocessing.connection.Connection 方法)

recv_into() (socket.socket 方法)

recvfrom() (socket.socket 方法)

recvfrom_into() (socket.socket 方法)

recvmsg() (socket.socket 方法)

recvmsg_into() (socket.socket 方法)

redirect_request() (urllib.request.HTTPRedirectHandler 方法)

redirect_stderr() (在 contextlib 模块中)

redirect_stdout() (在 contextlib 模块中)

redisplay() (在 readline 模块中)

redrawln() (curses.window 方法)

redrawwin() (curses.window 方法)

reduce (2to3 fixer)

reduce() (在 functools 模块中)

ref (weakref 中的类)

reference

attribute

reference count -- 引用计数

reference counting

ReferenceError, [1]

ReferenceType() (在 weakref 模块中)

refold_source (email.policy.EmailPolicy 属性)

refresh() (curses.window 方法)

REG_BINARY() (在 winreg 模块中)

REG_DWORD() (在 winreg 模块中)

REG_DWORD_BIG_ENDIAN() (在 winreg 模块中)

REG_DWORD_LITTLE_ENDIAN() (在 winreg 模块中)

REG_EXPAND_SZ() (在 winreg 模块中)

REG_FULL_RESOURCE_DESCRIPTOR() (在 winreg 模块中)

REG_LINK() (在 winreg 模块中)

REG_MULTI_SZ() (在 winreg 模块中)

REG_NONE() (在 winreg 模块中)

REG_QWORD() (在 winreg 模块中)

REG_QWORD_LITTLE_ENDIAN() (在 winreg 模块中)

REG_RESOURCE_LIST() (在 winreg 模块中)

REG_RESOURCE_REQUIREMENTS_LIST() (在 winreg 模块中)

REG_SZ() (在 winreg 模块中)

register() (abc.ABCMeta 方法)

(multiprocessing.managers.BaseManager 方法)

(select.devpoll 方法)

(select.epoll 方法)

(select.poll 方法)

(selectors.BaseSelector 方法)

(在 atexit 模块中)

(在 codecs 模块中)

(在 faulthandler 模块中)

(在 webbrowser 模块中)

register_adapter() (在 sqlite3 模块中)

register_archive_format() (在 shutil 模块中)

register_converter() (在 sqlite3 模块中)

register_defect() (email.policy.Policy 方法)

register_dialect() (在 csv 模块中)

register_error() (在 codecs 模块中)

register_function() (xmlrpc.server.CGIXMLRPCRequestHandler 方法)

(xmlrpc.server.SimpleXMLRPCServer 方法)

register_instance() (xmlrpc.server.CGIXMLRPCRequestHandler 方法)

(xmlrpc.server.SimpleXMLRPCServer 方法)

register_introspection_functions() (xmlrpc.server.CGIXMLRPCRequestHandler 方法)

(xmlrpc.server.SimpleXMLRPCServer 方法)

register_multicall_functions() (xmlrpc.server.CGIXMLRPCRequestHandler 方法)

(xmlrpc.server.SimpleXMLRPCServer 方法)

register_namespace() (在 xml.etree.ElementTree 模块中)

register_optionflag() (在 doctest 模块中)

register_shape() (在 turtle 模块中)

register_unpack_format() (在 shutil 模块中)

registerDOMImplementation() (在 xml.dom 模块中)

registerResult() (在 unittest 模块中)

regular

package

regular package -- 常规包

relative

import

URL

relative_to() (pathlib.PurePath 方法)

release() (_thread.lock 方法)

(asyncio.Condition 方法)

(asyncio.Lock 方法)

(asyncio.Semaphore 方法)

(logging.Handler 方法)

(memoryview 方法)

(multiprocessing.Lock 方法)

(multiprocessing.RLock 方法)

(threading.Condition 方法)

(threading.Lock 方法)

(threading.RLock 方法)

(threading.Semaphore 方法)

(在 platform 模块中)

release_lock() (在 imp 模块中)

reload (2to3 fixer)

reload() (在 imp 模块中)

(在 importlib 模块中)

relpath() (在 os.path 模块中)

remainder() (decimal.Context 方法)

remainder_near() (decimal.Context 方法)

(decimal.Decimal 方法)

RemoteDisconnected

remove() (array.array 方法)

(collections.deque 方法)

(frozenset 方法)

(mailbox.Mailbox 方法)

(mailbox.MH 方法)

(sequence method)

(xml.etree.ElementTree.Element 方法)

(在 os 模块中)

remove_done_callback() (asyncio.Future 方法)

remove_flag() (mailbox.MaildirMessage 方法)

(mailbox.mboxMessage 方法)

(mailbox.MMDFMessage 方法)

remove_folder() (mailbox.Maildir 方法)

(mailbox.MH 方法)

remove_header() (urllib.request.Request 方法)

remove_history_item() (在 readline 模块中)

remove_label() (mailbox.BabylMessage 方法)

remove_option() (configparser.ConfigParser 方法)

(optparse.OptionParser 方法)

remove_pyc() (msilib.Directory 方法)

remove_reader() (asyncio.AbstractEventLoop 方法)

remove_section() (configparser.ConfigParser 方法)

remove_sequence() (mailbox.MHMessage 方法)

remove_signal_handler() (asyncio.AbstractEventLoop 方法)

remove_tree() (在 distutils.dir_util 模块中)

remove_writer() (asyncio.AbstractEventLoop 方法)

removeAttribute() (xml.dom.Element 方法)

removeAttributeNode() (xml.dom.Element 方法)

removeAttributeNS() (xml.dom.Element 方法)

removeChild() (xml.dom.Node 方法)

removedirs() (在 os 模块中)

removeFilter() (logging.Handler 方法)

(logging.Logger 方法)

removeHandler() (logging.Logger 方法)

(在 unittest 模块中)

removeResult() (在 unittest 模块中)

removexattr() (在 os 模块中)

rename() (ftplib.FTP 方法)

(imaplib.IMAP4 方法)

(pathlib.Path 方法)

(在 os 模块中)

renames (2to3 fixer)

renames() (在 os 模块中)

reopenIfNeeded() (logging.handlers.WatchedFileHandler 方法)

reorganize() (dbm.gnu.gdbm 方法)

repeat() (timeit.Timer 方法)

(在 itertools 模块中)

(在 timeit 模块中)

repetition

operation

replace() (bytearray 方法)

(bytes 方法)

(curses.panel.Panel 方法)

(datetime.date 方法)

(datetime.datetime 方法)

(datetime.time 方法)

(inspect.Parameter 方法)

(inspect.Signature 方法)

(pathlib.Path 方法)

(str 方法)

(在 os 模块中)

replace_errors() (在 codecs 模块中)

replace_header() (email.message.EmailMessage 方法)

(email.message.Message 方法)

replace_history_item() (在 readline 模块中)

replace_whitespace (textwrap.TextWrapper 属性)

replaceChild() (xml.dom.Node 方法)

ReplacePackage() (在 modulefinder 模块中)

report() (filecmp.dircmp 方法)

(modulefinder.ModuleFinder 方法)

REPORT_CDIFF() (在 doctest 模块中)

report_failure() (doctest.DocTestRunner 方法)

report_full_closure() (filecmp.dircmp 方法)

REPORT_NDIFF() (在 doctest 模块中)

REPORT_ONLY_FIRST_FAILURE() (在 doctest 模块中)

report_partial_closure() (filecmp.dircmp 方法)

report_start() (doctest.DocTestRunner 方法)

report_success() (doctest.DocTestRunner 方法)

REPORT_UDIFF() (在 doctest 模块中)

report_unexpected_exception() (doctest.DocTestRunner 方法)

REPORTING_FLAGS() (在 doctest 模块中)

repr

內置函数, [1], [2], [3]

repr (2to3 fixer)

Repr (reprlib 中的类)

repr() (built-in function)

__repr__() (object method)

repr() (reprlib.Repr 方法)

(內置函数)

(在 reprlib 模块中)

repr1() (reprlib.Repr 方法)

representation

integer

reprlib (模块)

Request (urllib.request 中的类)

request() (http.client.HTTPConnection 方法)

request_queue_size (socketserver.BaseServer 属性)

request_rate() (urllib.robotparser.RobotFileParser 方法)

request_uri() (在 wsgiref.util 模块中)

request_version (http.server.BaseHTTPRequestHandler 属性)

RequestHandlerClass (socketserver.BaseServer 属性)

requestline (http.server.BaseHTTPRequestHandler 属性)

requires() (在 test.support 模块中)

reserved (zipfile.ZipInfo 属性)

reserved word

RESERVED_FUTURE() (在 uuid 模块中)

RESERVED_MICROSOFT() (在 uuid 模块中)

RESERVED_NCS() (在 uuid 模块中)

reset() (bdb.Bdb 方法)

(codecs.IncrementalDecoder 方法)

(codecs.IncrementalEncoder 方法)

(codecs.StreamReader 方法)

(codecs.StreamWriter 方法)

(html.parser.HTMLParser 方法)

(ossaudiodev.oss_audio_device 方法)

(pipes.Template 方法)

(threading.Barrier 方法)

(xdrlib.Packer 方法)

(xdrlib.Unpacker 方法)

(xml.dom.pulldom.DOMEventStream 方法)

(xml.sax.xmlreader.IncrementalParser 方法)

(在 turtle 模块中), [1]

reset_mock() (unittest.mock.Mock 方法)

reset_prog_mode() (在 curses 模块中)

reset_shell_mode() (在 curses 模块中)

resetbuffer() (code.InteractiveConsole 方法)

resetlocale() (在 locale 模块中)

resetscreen() (在 turtle 模块中)

resetty() (在 curses 模块中)

resetwarnings() (在 warnings 模块中)

resize() (curses.window 方法)

(mmap.mmap 方法)

(在 ctypes 模块中)

resize_term() (在 curses 模块中)

resizemode() (在 turtle 模块中)

resizeterm() (在 curses 模块中)

resolution (datetime.date 属性)

(datetime.datetime 属性)

(datetime.time 属性)

(datetime.timedelta 属性)

resolve() (pathlib.Path 方法)

resolve_name() (在 importlib.util 模块中)

resolveEntity() (xml.sax.handler.EntityResolver 方法)

resource (模块)

ResourceDenied

ResourceLoader (importlib.abc 中的类)

ResourceWarning

response (nntplib.NNTPError 属性)

response() (imaplib.IMAP4 方法)

ResponseNotReady

responses (http.server.BaseHTTPRequestHandler 属性)

responses() (在 http.client 模块中)

restart (pdb command)

restore() (在 difflib 模块中)

RESTRICTED

restricted

execution

restype (ctypes._FuncPtr 属性)

result() (asyncio.Future 方法)

(concurrent.futures.Future 方法)

results() (trace.Trace 方法)

resume_reading() (asyncio.ReadTransport 方法)

resume_writing() (asyncio.BaseProtocol 方法)

retr() (poplib.POP3 方法)

retrbinary() (ftplib.FTP 方法)

retrieve() (urllib.request.URLopener 方法)

retrlines() (ftplib.FTP 方法)

return

语句, [1], [2]

return (pdb command)

return_annotation (inspect.Signature 属性)

return_ok() (http.cookiejar.CookiePolicy 方法)

RETURN_VALUE (opcode)

return_value (unittest.mock.Mock 属性)

returncode (asyncio.asyncio.subprocess.Process 属性)

(subprocess.CalledProcessError 属性)

(subprocess.CompletedProcess 属性)

(subprocess.Popen 属性)

reverse() (array.array 方法)

(collections.deque 方法)

(sequence method)

(在 audioop 模块中)

reverse_order() (pstats.Stats 方法)

reverse_pointer (ipaddress.IPv4Address 属性)

(ipaddress.IPv6Address 属性)

reversed() (內置函数)

Reversible (collections.abc 中的类)

(typing 中的类)

revert() (http.cookiejar.FileCookieJar 方法)

rewind() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

RFC

RFC 1014, [1]

RFC 1123

RFC 1321

RFC 1422, [1]

RFC 1521, [1], [2]

RFC 1522, [1], [2]

RFC 1524, [1]

RFC 1730

RFC 1738

RFC 1750

RFC 1766, [1]

RFC 1808, [1], [2]

RFC 1832, [1]

RFC 1869, [1]

RFC 1870, [1], [2]

RFC 1939, [1]

RFC 2033

RFC 2045, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14]

RFC 2045#section-6.8

RFC 2046, [1], [2]

RFC 2047, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]

RFC 2060, [1]

RFC 2068

RFC 2104, [1]

RFC 2109, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14]

RFC 2183, [1], [2]

RFC 2231, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13]

RFC 2295

RFC 2342, [1]

RFC 2368

RFC 2373, [1], [2]

RFC 2396, [1], [2]

RFC 2397

RFC 2449

RFC 2487

RFC 2595, [1]

RFC 2616, [1], [2], [3], [4], [5], [6], [7], [8], [9]

RFC 2732, [1], [2]

RFC 2774

RFC 2818, [1]

RFC 2821

RFC 2822, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17]

RFC 2964

RFC 2965, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19]

RFC 2980, [1]

RFC 3056

RFC 3171

RFC 3207

RFC 3280

RFC 3330

RFC 3339

RFC 3454, [1]

RFC 3490, [1], [2], [3]

RFC 3490#section-3.1

RFC 3492, [1]

RFC 3493

RFC 3501

RFC 3542, [1]

RFC 3548, [1], [2], [3], [4]

RFC 3659

RFC 3879

RFC 3927

RFC 3977, [1], [2], [3], [4]

RFC 3986, [1], [2], [3], [4], [5]

RFC 4086

RFC 4122, [1], [2], [3], [4], [5], [6], [7], [8]

RFC 4180

RFC 4193

RFC 4217

RFC 4291

RFC 4380

RFC 4627, [1]

RFC 4642

RFC 4954, [1]

RFC 5161, [1]

RFC 5233, [1]

RFC 5246, [1]

RFC 5280, [1], [2], [3], [4]

RFC 5321, [1], [2], [3], [4]

RFC 5322, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21]

RFC 5424, [1], [2], [3]

RFC 5424#section-6

RFC 5735

RFC 5929

RFC 6066, [1], [2]

RFC 6125, [1]

RFC 6152, [1]

RFC 6531, [1], [2], [3], [4], [5], [6], [7], [8]

RFC 6532, [1], [2], [3], [4]

RFC 6585

RFC 6855, [1], [2], [3]

RFC 6856, [1]

RFC 7159, [1], [2]

RFC 7230, [1]

RFC 7231

RFC 7238

RFC 7301, [1], [2]

RFC 7525

RFC 7693

RFC 7914

RFC 821, [1]

RFC 822, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11]

RFC 854, [1]

RFC 959, [1]

RFC 977

rfc2109 (http.cookiejar.Cookie 属性)

rfc2109_as_netscape (http.cookiejar.DefaultCookiePolicy 属性)

rfc2965 (http.cookiejar.CookiePolicy 属性)

rfc822_escape() (在 distutils.util 模块中)

RFC_4122() (在 uuid 模块中)

rfile (http.server.BaseHTTPRequestHandler 属性)

rfind() (bytearray 方法)

(bytes 方法)

(mmap.mmap 方法)

(str 方法)

rgb_to_hls() (在 colorsys 模块中)

rgb_to_hsv() (在 colorsys 模块中)

rgb_to_yiq() (在 colorsys 模块中)

rglob() (pathlib.Path 方法)

right (filecmp.dircmp 属性)

right() (在 turtle 模块中)

right_list (filecmp.dircmp 属性)

right_only (filecmp.dircmp 属性)

RIGHTSHIFT() (在 token 模块中)

RIGHTSHIFTEQUAL() (在 token 模块中)

rindex() (bytearray 方法)

(bytes 方法)

(str 方法)

rjust() (bytearray 方法)

(bytes 方法)

(str 方法)

rlcompleter (模块)

rlecode_hqx() (在 binascii 模块中)

rledecode_hqx() (在 binascii 模块中)

RLIM_INFINITY() (在 resource 模块中)

RLIMIT_AS() (在 resource 模块中)

RLIMIT_CORE() (在 resource 模块中)

RLIMIT_CPU() (在 resource 模块中)

RLIMIT_DATA() (在 resource 模块中)

RLIMIT_FSIZE() (在 resource 模块中)

RLIMIT_MEMLOCK() (在 resource 模块中)

RLIMIT_MSGQUEUE() (在 resource 模块中)

RLIMIT_NICE() (在 resource 模块中)

RLIMIT_NOFILE() (在 resource 模块中)

RLIMIT_NPROC() (在 resource 模块中)

RLIMIT_NPTS() (在 resource 模块中)

RLIMIT_OFILE() (在 resource 模块中)

RLIMIT_RSS() (在 resource 模块中)

RLIMIT_RTPRIO() (在 resource 模块中)

RLIMIT_RTTIME() (在 resource 模块中)

RLIMIT_SBSIZE() (在 resource 模块中)

RLIMIT_SIGPENDING() (在 resource 模块中)

RLIMIT_STACK() (在 resource 模块中)

RLIMIT_SWAP() (在 resource 模块中)

RLIMIT_VMEM() (在 resource 模块中)

RLock (multiprocessing 中的类)

(threading 中的类)

RLock() (multiprocessing.managers.SyncManager 方法)

rmd() (ftplib.FTP 方法)

rmdir() (pathlib.Path 方法)

(在 os 模块中)

RMFF

rms() (在 audioop 模块中)

rmtree() (在 shutil 模块中)

RobotFileParser (urllib.robotparser 中的类)

robots.txt

rollback() (sqlite3.Connection 方法)

ROT_THREE (opcode)

ROT_TWO (opcode)

rotate() (collections.deque 方法)

(decimal.Context 方法)

(decimal.Decimal 方法)

(logging.handlers.BaseRotatingHandler 方法)

RotatingFileHandler (logging.handlers 中的类)

rotation_filename() (logging.handlers.BaseRotatingHandler 方法)

rotator (logging.handlers.BaseRotatingHandler 属性)

round

內置函数

round() (內置函数)

ROUND_05UP() (在 decimal 模块中)

ROUND_CEILING() (在 decimal 模块中)

ROUND_DOWN() (在 decimal 模块中)

ROUND_FLOOR() (在 decimal 模块中)

ROUND_HALF_DOWN() (在 decimal 模块中)

ROUND_HALF_EVEN() (在 decimal 模块中)

ROUND_HALF_UP() (在 decimal 模块中)

ROUND_UP() (在 decimal 模块中)

Rounded (decimal 中的类)

Row (sqlite3 中的类)

row_factory (sqlite3.Connection 属性)

rowcount (sqlite3.Cursor 属性)

RPAR() (在 token 模块中)

rpartition() (bytearray 方法)

(bytes 方法)

(str 方法)

rpc_paths (xmlrpc.server.SimpleXMLRPCRequestHandler 属性)

rpop() (poplib.POP3 方法)

rset() (poplib.POP3 方法)

rshift() (在 operator 模块中)

rsplit() (bytearray 方法)

(bytes 方法)

(str 方法)

RSQB() (在 token 模块中)

rstrip() (bytearray 方法)

(bytes 方法)

(str 方法)

rt() (在 turtle 模块中)

RTLD_DEEPBIND() (在 os 模块中)

RTLD_GLOBAL() (在 os 模块中)

RTLD_LAZY() (在 os 模块中)

RTLD_LOCAL() (在 os 模块中)

RTLD_NODELETE() (在 os 模块中)

RTLD_NOLOAD() (在 os 模块中)

RTLD_NOW() (在 os 模块中)

ruler (cmd.Cmd 属性)

run (pdb command)

Run script

run() (bdb.Bdb 方法)

(distutils.cmd.Command 方法)

(doctest.DocTestRunner 方法)

(multiprocessing.Process 方法)

(pdb.Pdb 方法)

(profile.Profile 方法)

(sched.scheduler 方法)

(threading.Thread 方法)

(trace.Trace 方法)

(unittest.TestCase 方法)

(unittest.TestSuite 方法)

(unittest.TextTestRunner 方法)

(wsgiref.handlers.BaseHandler 方法)

(在 pdb 模块中)

(在 profile 模块中)

(在 subprocess 模块中)

run_coroutine_threadsafe() (在 asyncio 模块中)

run_docstring_examples() (在 doctest 模块中)

run_doctest() (在 test.support 模块中)

run_forever() (asyncio.AbstractEventLoop 方法)

run_in_executor() (asyncio.AbstractEventLoop 方法)

run_module() (在 runpy 模块中)

run_path() (在 runpy 模块中)

run_script() (modulefinder.ModuleFinder 方法)

run_setup() (在 distutils.core 模块中)

run_unittest() (在 test.support 模块中)

run_until_complete() (asyncio.AbstractEventLoop 方法)

run_with_locale() (在 test.support 模块中)

runcall() (bdb.Bdb 方法)

(pdb.Pdb 方法)

(profile.Profile 方法)

(在 pdb 模块中)

runcode() (code.InteractiveInterpreter 方法)

runctx() (bdb.Bdb 方法)

(profile.Profile 方法)

(trace.Trace 方法)

(在 profile 模块中)

runeval() (bdb.Bdb 方法)

(pdb.Pdb 方法)

(在 pdb 模块中)

runfunc() (trace.Trace 方法)

running() (concurrent.futures.Future 方法)

runpy (模块)

runsource() (code.InteractiveInterpreter 方法)

runtime_library_dir_option() (distutils.ccompiler.CCompiler 方法)

RuntimeError

RuntimeWarning

RUSAGE_BOTH() (在 resource 模块中)

RUSAGE_CHILDREN() (在 resource 模块中)

RUSAGE_SELF() (在 resource 模块中)

RUSAGE_THREAD() (在 resource 模块中)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – S

S() (在 re 模块中)

S_ENFMT() (在 stat 模块中)

S_IEXEC() (在 stat 模块中)

S_IFBLK() (在 stat 模块中)

S_IFCHR() (在 stat 模块中)

S_IFDIR() (在 stat 模块中)

S_IFDOOR() (在 stat 模块中)

S_IFIFO() (在 stat 模块中)

S_IFLNK() (在 stat 模块中)

S_IFMT() (在 stat 模块中)

S_IFPORT() (在 stat 模块中)

S_IFREG() (在 stat 模块中)

S_IFSOCK() (在 stat 模块中)

S_IFWHT() (在 stat 模块中)

S_IMODE() (在 stat 模块中)

S_IREAD() (在 stat 模块中)

S_IRGRP() (在 stat 模块中)

S_IROTH() (在 stat 模块中)

S_IRUSR() (在 stat 模块中)

S_IRWXG() (在 stat 模块中)

S_IRWXO() (在 stat 模块中)

S_IRWXU() (在 stat 模块中)

S_ISBLK() (在 stat 模块中)

S_ISCHR() (在 stat 模块中)

S_ISDIR() (在 stat 模块中)

S_ISDOOR() (在 stat 模块中)

S_ISFIFO() (在 stat 模块中)

S_ISGID() (在 stat 模块中)

S_ISLNK() (在 stat 模块中)

S_ISPORT() (在 stat 模块中)

S_ISREG() (在 stat 模块中)

S_ISSOCK() (在 stat 模块中)

S_ISUID() (在 stat 模块中)

S_ISVTX() (在 stat 模块中)

S_ISWHT() (在 stat 模块中)

S_IWGRP() (在 stat 模块中)

S_IWOTH() (在 stat 模块中)

S_IWRITE() (在 stat 模块中)

S_IWUSR() (在 stat 模块中)

S_IXGRP() (在 stat 模块中)

S_IXOTH() (在 stat 模块中)

S_IXUSR() (在 stat 模块中)

safe_substitute() (string.Template 方法)

saferepr() (在 pprint 模块中)

same_files (filecmp.dircmp 属性)

same_quantum() (decimal.Context 方法)

(decimal.Decimal 方法)

samefile() (pathlib.Path 方法)

(在 os.path 模块中)

SameFileError

sameopenfile() (在 os.path 模块中)

samestat() (在 os.path 模块中)

sample() (在 random 模块中)

save() (http.cookiejar.FileCookieJar 方法)

SaveKey() (在 winreg 模块中)

savetty() (在 curses 模块中)

SAX2DOM (xml.dom.pulldom 中的类)

SAXException

SAXNotRecognizedException

SAXNotSupportedException

SAXParseException

scaleb() (decimal.Context 方法)

(decimal.Decimal 方法)

scandir() (在 os 模块中)

scanf()

sched (模块)

SCHED_BATCH() (在 os 模块中)

SCHED_FIFO() (在 os 模块中)

sched_get_priority_max() (在 os 模块中)

sched_get_priority_min() (在 os 模块中)

sched_getaffinity() (在 os 模块中)

sched_getparam() (在 os 模块中)

sched_getscheduler() (在 os 模块中)

SCHED_IDLE() (在 os 模块中)

SCHED_OTHER() (在 os 模块中)

sched_param (os 中的类)

sched_priority (os.sched_param 属性)

SCHED_RESET_ON_FORK() (在 os 模块中)

SCHED_RR() (在 os 模块中)

sched_rr_get_interval() (在 os 模块中)

sched_setaffinity() (在 os 模块中)

sched_setparam() (在 os 模块中)

sched_setscheduler() (在 os 模块中)

SCHED_SPORADIC() (在 os 模块中)

sched_yield() (在 os 模块中)

scheduler (sched 中的类)

schema() (在 msilib 模块中)

scope, [1]

Screen (turtle 中的类)

screensize() (在 turtle 模块中)

script_from_examples() (在 doctest 模块中)

scroll() (curses.window 方法)

ScrolledCanvas (turtle 中的类)

scrollok() (curses.window 方法)

scrypt() (在 hashlib 模块中)

sdterr

stdin stdout

search

path, module, [1], [2], [3], [4], [5], [6], [7]

search() (imaplib.IMAP4 方法)

(re.regex 方法)

(在 re 模块中)

second (datetime.datetime 属性)

(datetime.time 属性)

seconds since the epoch

secrets (模块)

SECTCRE (configparser.ConfigParser 属性)

sections() (configparser.ConfigParser 方法)

secure (http.cookiejar.Cookie 属性)

secure hash algorithm, SHA1, SHA224, SHA256, SHA384, SHA512

Secure Sockets Layer

security

CGI

see() (tkinter.ttk.Treeview 方法)

seed() (在 random 模块中)

seek() (chunk.Chunk 方法)

(io.IOBase 方法)

(io.TextIOBase 方法)

(mmap.mmap 方法)

SEEK_CUR() (在 os 模块中)

SEEK_END() (在 os 模块中)

SEEK_SET() (在 os 模块中)

seekable() (io.IOBase 方法)

seen_greeting (smtpd.SMTPChannel 属性)

Select (tkinter.tix 中的类)

select (模块)

select() (imaplib.IMAP4 方法)

(selectors.BaseSelector 方法)

(tkinter.ttk.Notebook 方法)

(在 select 模块中)

selected_alpn_protocol() (ssl.SSLSocket 方法)

selected_npn_protocol() (ssl.SSLSocket 方法)

selection() (tkinter.ttk.Treeview 方法)

selection_add() (tkinter.ttk.Treeview 方法)

selection_remove() (tkinter.ttk.Treeview 方法)

selection_set() (tkinter.ttk.Treeview 方法)

selection_toggle() (tkinter.ttk.Treeview 方法)

selector (urllib.request.Request 属性)

SelectorEventLoop (asyncio 中的类)

SelectorKey (selectors 中的类)

selectors (模块)

SelectSelector (selectors 中的类)

Semaphore (asyncio 中的类)

(multiprocessing 中的类)

(threading 中的类)

Semaphore() (multiprocessing.managers.SyncManager 方法)

semaphores, binary

SEMI() (在 token 模块中)

send() (asyncore.dispatcher 方法)

(coroutine 方法)

(generator 方法)

(http.client.HTTPConnection 方法)

(imaplib.IMAP4 方法)

(logging.handlers.DatagramHandler 方法)

(logging.handlers.SocketHandler 方法)

(multiprocessing.connection.Connection 方法)

(socket.socket 方法)

send_bytes() (multiprocessing.connection.Connection 方法)

send_error() (http.server.BaseHTTPRequestHandler 方法)

send_flowing_data() (formatter.writer 方法)

send_header() (http.server.BaseHTTPRequestHandler 方法)

send_hor_rule() (formatter.writer 方法)

send_label_data() (formatter.writer 方法)

send_line_break() (formatter.writer 方法)

send_literal_data() (formatter.writer 方法)

send_message() (smtplib.SMTP 方法)

send_paragraph() (formatter.writer 方法)

send_response() (http.server.BaseHTTPRequestHandler 方法)

send_response_only() (http.server.BaseHTTPRequestHandler 方法)

send_signal() (asyncio.asyncio.subprocess.Process 方法)

(asyncio.BaseSubprocessTransport 方法)

(subprocess.Popen 方法)

sendall() (socket.socket 方法)

sendcmd() (ftplib.FTP 方法)

sendfile() (socket.socket 方法)

(wsgiref.handlers.BaseHandler 方法)

(在 os 模块中)

sendmail() (smtplib.SMTP 方法)

sendmsg() (socket.socket 方法)

sendmsg_afalg() (socket.socket 方法)

sendto() (asyncio.DatagramTransport 方法)

(socket.socket 方法)

sentinel (multiprocessing.Process 属性)

sentinel() (在 unittest.mock 模块中)

sep() (在 os 模块中)

sequence

item

iteration

types, immutable

types, mutable

types, operations on, [1]

对象, [1], [2], [3], [4], [5], [6], [7], [8]

Sequence (collections.abc 中的类)

(typing 中的类)

sequence -- 序列

sequence() (在 msilib 模块中)

sequence2st() (在 parser 模块中)

SequenceMatcher (difflib 中的类), [1]

serializing

objects

serve_forever() (socketserver.BaseServer 方法)

server

WWW, [1]

Server (asyncio 中的类)

server (http.server.BaseHTTPRequestHandler 属性)

server_activate() (socketserver.BaseServer 方法)

server_address (socketserver.BaseServer 属性)

server_bind() (socketserver.BaseServer 方法)

server_close() (socketserver.BaseServer 方法)

server_hostname (ssl.SSLSocket 属性)

server_side (ssl.SSLSocket 属性)

server_software (wsgiref.handlers.BaseHandler 属性)

server_version (http.server.BaseHTTPRequestHandler 属性)

(http.server.SimpleHTTPRequestHandler 属性)

ServerProxy (xmlrpc.client 中的类)

service_actions() (socketserver.BaseServer 方法)

session (ssl.SSLSocket 属性)

session_reused (ssl.SSLSocket 属性)

session_stats() (ssl.SSLContext 方法)

set

display

对象, [1], [2], [3]

Set (collections.abc 中的类)

(typing 中的类)

set (內置类)

Set Breakpoint

set type

对象

set() (asyncio.Event 方法)

(configparser.ConfigParser 方法)

(configparser.RawConfigParser 方法)

(http.cookies.Morsel 方法)

(ossaudiodev.oss_mixer_device 方法)

(test.support.EnvironmentVarGuard 方法)

(threading.Event 方法)

(tkinter.ttk.Combobox 方法)

(tkinter.ttk.Treeview 方法)

(xml.etree.ElementTree.Element 方法)

SET_ADD (opcode)

set_all()

set_allowed_domains() (http.cookiejar.DefaultCookiePolicy 方法)

set_alpn_protocols() (ssl.SSLContext 方法)

set_app() (wsgiref.simple_server.WSGIServer 方法)

set_asyncgen_hooks() (在 sys 模块中)

set_authorizer() (sqlite3.Connection 方法)

set_auto_history() (在 readline 模块中)

set_blocked_domains() (http.cookiejar.DefaultCookiePolicy 方法)

set_blocking() (在 os 模块中)

set_boundary() (email.message.EmailMessage 方法)

(email.message.Message 方法)

set_break() (bdb.Bdb 方法)

set_charset() (email.message.Message 方法)

set_children() (tkinter.ttk.Treeview 方法)

set_ciphers() (ssl.SSLContext 方法)

set_completer() (在 readline 模块中)

set_completer_delims() (在 readline 模块中)

set_completion_display_matches_hook() (在 readline 模块中)

set_content() (email.contentmanager.ContentManager 方法)

(email.message.EmailMessage 方法)

(在 email.contentmanager 模块中)

set_continue() (bdb.Bdb 方法)

set_cookie() (http.cookiejar.CookieJar 方法)

set_cookie_if_ok() (http.cookiejar.CookieJar 方法)

set_coroutine_wrapper() (在 sys 模块中)

set_current() (msilib.Feature 方法)

set_data() (importlib.abc.SourceLoader 方法)

(importlib.machinery.SourceFileLoader 方法)

set_date() (mailbox.MaildirMessage 方法)

set_debug() (asyncio.AbstractEventLoop 方法)

(在 gc 模块中)

set_debuglevel() (ftplib.FTP 方法)

(http.client.HTTPConnection 方法)

(nntplib.NNTP 方法)

(poplib.POP3 方法)

(smtplib.SMTP 方法)

(telnetlib.Telnet 方法)

set_default_executor() (asyncio.AbstractEventLoop 方法)

set_default_type() (email.message.EmailMessage 方法)

(email.message.Message 方法)

set_default_verify_paths() (ssl.SSLContext 方法)

set_defaults() (argparse.ArgumentParser 方法)

(optparse.OptionParser 方法)

set_ecdh_curve() (ssl.SSLContext 方法)

set_errno() (在 ctypes 模块中)

set_event_loop() (asyncio.AbstractEventLoopPolicy 方法)

(在 asyncio 模块中)

set_event_loop_policy() (在 asyncio 模块中)

set_exception() (asyncio.Future 方法)

(asyncio.StreamReader 方法)

(concurrent.futures.Future 方法)

set_exception_handler() (asyncio.AbstractEventLoop 方法)

set_executable() (在 multiprocessing 模块中)

set_executables() (distutils.ccompiler.CCompiler 方法)

set_flags() (mailbox.MaildirMessage 方法)

(mailbox.mboxMessage 方法)

(mailbox.MMDFMessage 方法)

set_from() (mailbox.mboxMessage 方法)

(mailbox.MMDFMessage 方法)

set_handle_inheritable() (在 os 模块中)

set_history_length() (在 readline 模块中)

set_include_dirs() (distutils.ccompiler.CCompiler 方法)

set_info() (mailbox.MaildirMessage 方法)

set_inheritable() (socket.socket 方法)

(在 os 模块中)

set_labels() (mailbox.BabylMessage 方法)

set_last_error() (在 ctypes 模块中)

set_libraries() (distutils.ccompiler.CCompiler 方法)

set_library_dirs() (distutils.ccompiler.CCompiler 方法)

set_link_objects() (distutils.ccompiler.CCompiler 方法)

set_literal (2to3 fixer)

set_loader() (在 importlib.util 模块中)

set_next() (bdb.Bdb 方法)

set_nonstandard_attr() (http.cookiejar.Cookie 方法)

set_npn_protocols() (ssl.SSLContext 方法)

set_ok() (http.cookiejar.CookiePolicy 方法)

set_option_negotiation_callback() (telnetlib.Telnet 方法)

set_output_charset() (gettext.NullTranslations 方法)

set_package() (在 importlib.util 模块中)

set_param() (email.message.EmailMessage 方法)

(email.message.Message 方法)

set_pasv() (ftplib.FTP 方法)

set_payload() (email.message.Message 方法)

set_policy() (http.cookiejar.CookieJar 方法)

set_position() (xdrlib.Unpacker 方法)

set_pre_input_hook() (在 readline 模块中)

set_progress_handler() (sqlite3.Connection 方法)

set_protocol() (asyncio.BaseTransport 方法)

set_proxy() (urllib.request.Request 方法)

set_python_build() (在 distutils.sysconfig 模块中)

set_quit() (bdb.Bdb 方法)

set_recsrc() (ossaudiodev.oss_mixer_device 方法)

set_result() (asyncio.Future 方法)

(concurrent.futures.Future 方法)

set_return() (bdb.Bdb 方法)

set_running_or_notify_cancel() (concurrent.futures.Future 方法)

set_runtime_library_dirs() (distutils.ccompiler.CCompiler 方法)

set_seq1() (difflib.SequenceMatcher 方法)

set_seq2() (difflib.SequenceMatcher 方法)

set_seqs() (difflib.SequenceMatcher 方法)

set_sequences() (mailbox.MH 方法)

(mailbox.MHMessage 方法)

set_server_documentation() (xmlrpc.server.DocCGIXMLRPCRequestHandler 方法)

(xmlrpc.server.DocXMLRPCServer 方法)

set_server_name() (xmlrpc.server.DocCGIXMLRPCRequestHandler 方法)

(xmlrpc.server.DocXMLRPCServer 方法)

set_server_title() (xmlrpc.server.DocCGIXMLRPCRequestHandler 方法)

(xmlrpc.server.DocXMLRPCServer 方法)

set_servername_callback() (ssl.SSLContext 方法)

set_spacing() (formatter.formatter 方法)

set_start_method() (在 multiprocessing 模块中)

set_startup_hook() (在 readline 模块中)

set_step() (bdb.Bdb 方法)

set_subdir() (mailbox.MaildirMessage 方法)

set_task_factory() (asyncio.AbstractEventLoop 方法)

set_terminator() (asynchat.async_chat 方法)

set_threshold() (在 gc 模块中)

set_trace() (bdb.Bdb 方法)

(pdb.Pdb 方法)

(在 bdb 模块中)

(在 pdb 模块中)

set_trace_callback() (sqlite3.Connection 方法)

set_transport() (asyncio.StreamReader 方法)

set_tunnel() (http.client.HTTPConnection 方法)

set_type() (email.message.Message 方法)

set_unittest_reportflags() (在 doctest 模块中)

set_unixfrom() (email.message.EmailMessage 方法)

(email.message.Message 方法)

set_until() (bdb.Bdb 方法)

set_url() (urllib.robotparser.RobotFileParser 方法)

set_usage() (optparse.OptionParser 方法)

set_userptr() (curses.panel.Panel 方法)

set_visible() (mailbox.BabylMessage 方法)

set_wakeup_fd() (在 signal 模块中)

set_write_buffer_limits() (asyncio.WriteTransport 方法)

setacl() (imaplib.IMAP4 方法)

setannotation() (imaplib.IMAP4 方法)

setattr() (內置函数)

setAttribute() (xml.dom.Element 方法)

setAttributeNode() (xml.dom.Element 方法)

setAttributeNodeNS() (xml.dom.Element 方法)

setAttributeNS() (xml.dom.Element 方法)

SetBase() (xml.parsers.expat.xmlparser 方法)

setblocking() (socket.socket 方法)

setByteStream() (xml.sax.xmlreader.InputSource 方法)

setcbreak() (在 tty 模块中)

setCharacterStream() (xml.sax.xmlreader.InputSource 方法)

setcheckinterval() (在 sys 模块中)

setcomptype() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

setContentHandler() (xml.sax.xmlreader.XMLReader 方法)

setcontext() (在 decimal 模块中)

setDaemon() (threading.Thread 方法)

setdefault() (dict 方法)

(http.cookies.Morsel 方法)

setdefaulttimeout() (在 socket 模块中)

setdlopenflags() (在 sys 模块中)

setDocumentLocator() (xml.sax.handler.ContentHandler 方法)

setDTDHandler() (xml.sax.xmlreader.XMLReader 方法)

setegid() (在 os 模块中)

setEncoding() (xml.sax.xmlreader.InputSource 方法)

setEntityResolver() (xml.sax.xmlreader.XMLReader 方法)

setErrorHandler() (xml.sax.xmlreader.XMLReader 方法)

seteuid() (在 os 模块中)

setFeature() (xml.sax.xmlreader.XMLReader 方法)

setfirstweekday() (在 calendar 模块中)

setfmt() (ossaudiodev.oss_audio_device 方法)

setFormatter() (logging.Handler 方法)

setframerate() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

setgid() (在 os 模块中)

setgroups() (在 os 模块中)

seth() (在 turtle 模块中)

setheading() (在 turtle 模块中)

sethostname() (在 socket 模块中)

SetInteger() (msilib.Record 方法)

setitem() (在 operator 模块中)

setitimer() (在 signal 模块中)

setLevel() (logging.Handler 方法)

(logging.Logger 方法)

setLocale() (xml.sax.xmlreader.XMLReader 方法)

setlocale() (在 locale 模块中)

setLoggerClass() (在 logging 模块中)

setlogmask() (在 syslog 模块中)

setLogRecordFactory() (在 logging 模块中)

setmark() (aifc.aifc 方法)

setMaxConns() (urllib.request.CacheFTPHandler 方法)

setmode() (在 msvcrt 模块中)

setName() (threading.Thread 方法)

setnchannels() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

setnframes() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

SetParamEntityParsing() (xml.parsers.expat.xmlparser 方法)

setparameters() (ossaudiodev.oss_audio_device 方法)

setparams() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

setpassword() (zipfile.ZipFile 方法)

setpgid() (在 os 模块中)

setpgrp() (在 os 模块中)

setpos() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

(在 turtle 模块中)

setposition() (在 turtle 模块中)

setpriority() (在 os 模块中)

setprofile() (在 sys 模块中)

(在 threading 模块中)

SetProperty() (msilib.SummaryInformation 方法)

setProperty() (xml.sax.xmlreader.XMLReader 方法)

setPublicId() (xml.sax.xmlreader.InputSource 方法)

setquota() (imaplib.IMAP4 方法)

setraw() (在 tty 模块中)

setrecursionlimit() (在 sys 模块中)

setregid() (在 os 模块中)

setresgid() (在 os 模块中)

setresuid() (在 os 模块中)

setreuid() (在 os 模块中)

setrlimit() (在 resource 模块中)

setsampwidth() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

setscrreg() (curses.window 方法)

setsid() (在 os 模块中)

setsockopt() (socket.socket 方法), [1], [2]

setstate() (codecs.IncrementalDecoder 方法)

(codecs.IncrementalEncoder 方法)

(在 random 模块中)

SetStream() (msilib.Record 方法)

SetString() (msilib.Record 方法)

setswitchinterval() (in module sys)

(在 sys 模块中)

setSystemId() (xml.sax.xmlreader.InputSource 方法)

setsyx() (在 curses 模块中)

setTarget() (logging.handlers.MemoryHandler 方法)

settiltangle() (在 turtle 模块中)

settimeout() (socket.socket 方法)

setTimeout() (urllib.request.CacheFTPHandler 方法)

settrace() (在 sys 模块中)

(在 threading 模块中)

setuid() (在 os 模块中)

setundobuffer() (在 turtle 模块中)

setup() (socketserver.BaseRequestHandler 方法)

setUp() (unittest.TestCase 方法)

setup() (在 distutils.core 模块中)

(在 turtle 模块中)

SETUP_ANNOTATIONS (opcode)

SETUP_ASYNC_WITH (opcode)

setup_environ() (wsgiref.handlers.BaseHandler 方法)

SETUP_EXCEPT (opcode)

SETUP_FINALLY (opcode)

SETUP_LOOP (opcode)

setup_python() (venv.EnvBuilder 方法)

setup_scripts() (venv.EnvBuilder 方法)

setup_testing_defaults() (在 wsgiref.util 模块中)

SETUP_WITH (opcode)

setUpClass() (unittest.TestCase 方法)

setupterm() (在 curses 模块中)

SetValue() (在 winreg 模块中)

SetValueEx() (在 winreg 模块中)

setworldcoordinates() (在 turtle 模块中)

setx() (在 turtle 模块中)

setxattr() (在 os 模块中)

sety() (在 turtle 模块中)

SF_APPEND() (在 stat 模块中)

SF_ARCHIVED() (在 stat 模块中)

SF_IMMUTABLE() (在 stat 模块中)

SF_MNOWAIT() (在 os 模块中)

SF_NODISKIO() (在 os 模块中)

SF_NOUNLINK() (在 stat 模块中)

SF_SNAPSHOT() (在 stat 模块中)

SF_SYNC() (在 os 模块中)

shape (memoryview 属性)

Shape (turtle 中的类)

shape() (在 turtle 模块中)

shapesize() (在 turtle 模块中)

shapetransform() (在 turtle 模块中)

share() (socket.socket 方法)

shared_ciphers() (ssl.SSLSocket 方法)

shared_object_filename() (distutils.ccompiler.CCompiler 方法)

shearfactor() (在 turtle 模块中)

Shelf (shelve 中的类)

shelve

模块

shelve (模块)

shield() (在 asyncio 模块中)

shift() (decimal.Context 方法)

(decimal.Decimal 方法)

shift_path_info() (在 wsgiref.util 模块中)

shifting

operation

operations

shlex (shlex 中的类)

(模块)

shortDescription() (unittest.TestCase 方法)

shorten() (在 textwrap 模块中)

shouldFlush() (logging.handlers.BufferingHandler 方法)

(logging.handlers.MemoryHandler 方法)

shouldStop (unittest.TestResult 属性)

show() (curses.panel.Panel 方法)

show_code() (在 dis 模块中)

show_compilers() (在 distutils.ccompiler 模块中)

showsyntaxerror() (code.InteractiveInterpreter 方法)

showtraceback() (code.InteractiveInterpreter 方法)

showturtle() (在 turtle 模块中)

showwarning() (在 warnings 模块中)

shuffle() (在 random 模块中)

shutdown() (concurrent.futures.Executor 方法)

(imaplib.IMAP4 方法)

(multiprocessing.managers.BaseManager 方法)

(socket.socket 方法)

(socketserver.BaseServer 方法)

(在 logging 模块中)

shutdown_asyncgens() (asyncio.AbstractEventLoop 方法)

shutil (模块)

side_effect (unittest.mock.Mock 属性)

SIG_BLOCK() (在 signal 模块中)

SIG_DFL() (在 signal 模块中)

SIG_IGN() (在 signal 模块中)

SIG_SETMASK() (在 signal 模块中)

SIG_UNBLOCK() (在 signal 模块中)

SIGINT, [1]

siginterrupt() (在 signal 模块中)

signal

模块, [1]

signal (模块)

signal() (在 signal 模块中)

Signature (inspect 中的类)

signature (inspect.BoundArguments 属性)

signature() (在 inspect 模块中)

sigpending() (在 signal 模块中)

sigtimedwait() (在 signal 模块中)

sigwait() (在 signal 模块中)

sigwaitinfo() (在 signal 模块中)

simple

statement

Simple Mail Transfer Protocol

SimpleCookie (http.cookies 中的类)

simplefilter() (在 warnings 模块中)

SimpleHandler (wsgiref.handlers 中的类)

SimpleHTTPRequestHandler (http.server 中的类)

SimpleNamespace (types 中的类)

SimpleQueue (multiprocessing 中的类)

SimpleXMLRPCRequestHandler (xmlrpc.server 中的类)

SimpleXMLRPCServer (xmlrpc.server 中的类)

sin() (在 cmath 模块中)

(在 math 模块中)

single dispatch -- 单分派

SingleAddressHeader (email.headerregistry 中的类)

singledispatch() (在 functools 模块中)

singleton

tuple

sinh() (在 cmath 模块中)

(在 math 模块中)

SIO_KEEPALIVE_VALS() (在 socket 模块中)

SIO_LOOPBACK_FAST_PATH() (在 socket 模块中)

SIO_RCVALL() (在 socket 模块中)

site (模块)

site command line option

--user-base

--user-site

site-packages

directory

sitecustomize

模块

sixtofour (ipaddress.IPv6Address 属性)

size (struct.Struct 属性)

(tarfile.TarInfo 属性)

(tracemalloc.Statistic 属性)

(tracemalloc.StatisticDiff 属性)

(tracemalloc.Trace 属性)

size() (ftplib.FTP 方法)

(mmap.mmap 方法)

size_diff (tracemalloc.StatisticDiff 属性)

SIZE_MAX

Sized (collections.abc 中的类)

(typing 中的类)

sizeof() (在 ctypes 模块中)

skip() (chunk.Chunk 方法)

SKIP() (在 doctest 模块中)

skip() (在 unittest 模块中)

skip_unless_symlink() (在 test.support 模块中)

skipIf() (在 unittest 模块中)

skipinitialspace (csv.Dialect 属性)

skipped (unittest.TestResult 属性)

skippedEntity() (xml.sax.handler.ContentHandler 方法)

SkipTest

skipTest() (unittest.TestCase 方法)

skipUnless() (在 unittest 模块中)

SLASH() (在 token 模块中)

SLASHEQUAL() (在 token 模块中)

slave() (nntplib.NNTP 方法)

sleep() (在 asyncio 模块中)

(在 time 模块中)

slice

assignment

operation

內置函数, [1]

对象

slice (內置类)

slice -- 切片

slicing, [1], [2]

assignment

SMTP

protocol

SMTP (smtplib 中的类)

SMTP() (在 email.policy 模块中)

smtp_server (smtpd.SMTPChannel 属性)

SMTP_SSL (smtplib 中的类)

smtp_state (smtpd.SMTPChannel 属性)

SMTPAuthenticationError

SMTPChannel (smtpd 中的类)

SMTPConnectError

smtpd (模块)

SMTPDataError

SMTPException

SMTPHandler (logging.handlers 中的类)

SMTPHeloError

smtplib (模块)

SMTPNotSupportedError

SMTPRecipientsRefused

SMTPResponseException

SMTPSenderRefused

SMTPServer (smtpd 中的类)

SMTPServerDisconnected

SMTPUTF8() (在 email.policy 模块中)

Snapshot (tracemalloc 中的类)

SND_ALIAS() (在 winsound 模块中)

SND_ASYNC() (在 winsound 模块中)

SND_FILENAME() (在 winsound 模块中)

SND_LOOP() (在 winsound 模块中)

SND_MEMORY() (在 winsound 模块中)

SND_NODEFAULT() (在 winsound 模块中)

SND_NOSTOP() (在 winsound 模块中)

SND_NOWAIT() (在 winsound 模块中)

SND_PURGE() (在 winsound 模块中)

sndhdr (模块)

sniff() (csv.Sniffer 方法)

Sniffer (csv 中的类)

sock_accept() (asyncio.AbstractEventLoop 方法)

SOCK_CLOEXEC() (在 socket 模块中)

sock_connect() (asyncio.AbstractEventLoop 方法)

SOCK_DGRAM() (在 socket 模块中)

SOCK_NONBLOCK() (在 socket 模块中)

SOCK_RAW() (在 socket 模块中)

SOCK_RDM() (在 socket 模块中)

sock_recv() (asyncio.AbstractEventLoop 方法)

sock_sendall() (asyncio.AbstractEventLoop 方法)

SOCK_SEQPACKET() (在 socket 模块中)

SOCK_STREAM() (在 socket 模块中)

socket

对象

模块

socket (socketserver.BaseServer 属性)

(模块)

socket() (imaplib.IMAP4 方法)

(in module socket)

(在 socket 模块中)

socket_type (socketserver.BaseServer 属性)

SocketHandler (logging.handlers 中的类)

socketpair() (在 socket 模块中)

sockets (asyncio.Server 属性)

socketserver (模块)

SocketType() (在 socket 模块中)

SOL_ALG() (在 socket 模块中)

SOL_RDS() (在 socket 模块中)

SOMAXCONN() (在 socket 模块中)

sort() (imaplib.IMAP4 方法)

(list 方法)

sort_stats() (pstats.Stats 方法)

sorted() (內置函数)

sortTestMethodsUsing (unittest.TestLoader 属性)

source (doctest.Example 属性)

(pdb command)

(shlex.shlex 属性)

source character set

source_from_cache() (在 imp 模块中)

(在 importlib.util 模块中)

SOURCE_SUFFIXES() (在 importlib.machinery 模块中)

source_to_code() (importlib.abc.InspectLoader 静态方法)

SourceFileLoader (importlib.machinery 中的类)

sourcehook() (shlex.shlex 方法)

SourcelessFileLoader (importlib.machinery 中的类)

SourceLoader (importlib.abc 中的类)

space

in printf-style formatting, [1]

in string formatting

span() (re.match 方法)

spawn() (distutils.ccompiler.CCompiler 方法)

(在 pty 模块中)

spawnl() (在 os 模块中)

spawnle() (在 os 模块中)

spawnlp() (在 os 模块中)

spawnlpe() (在 os 模块中)

spawnv() (在 os 模块中)

spawnve() (在 os 模块中)

spawnvp() (在 os 模块中)

spawnvpe() (在 os 模块中)

spec_from_file_location() (在 importlib.util 模块中)

spec_from_loader() (在 importlib.util 模块中)

special

attribute

attribute, generic

special method -- 特殊方法

specified_attributes (xml.parsers.expat.xmlparser 属性)

speed() (ossaudiodev.oss_audio_device 方法)

(在 turtle 模块中)

split() (bytearray 方法)

(bytes 方法)

(re.regex 方法)

(str 方法)

(在 os.path 模块中)

(在 re 模块中)

(在 shlex 模块中)

split_quoted() (在 distutils.util 模块中)

splitdrive() (在 os.path 模块中)

splitext() (在 os.path 模块中)

splitlines() (bytearray 方法)

(bytes 方法)

(str 方法)

SplitResult (urllib.parse 中的类)

SplitResultBytes (urllib.parse 中的类)

splitunc() (在 os.path 模块中)

SpooledTemporaryFile() (在 tempfile 模块中)

sprintf-style formatting, [1]

spwd (模块)

sqlite3 (模块)

sqlite_version() (在 sqlite3 模块中)

sqlite_version_info() (在 sqlite3 模块中)

sqrt() (decimal.Context 方法)

(decimal.Decimal 方法)

(在 cmath 模块中)

(在 math 模块中)

SSL

ssl (模块)

SSL_CERT_FILE

SSL_CERT_PATH

ssl_version (ftplib.FTP_TLS 属性)

SSLContext (ssl 中的类)

SSLEOFError

SSLError

SSLErrorNumber (ssl 中的类)

SSLObject (ssl 中的类)

SSLSession (ssl 中的类)

SSLSocket (ssl 中的类)

SSLSyscallError

SSLWantReadError

SSLWantWriteError

SSLZeroReturnError

st() (在 turtle 模块中)

st2list() (在 parser 模块中)

st2tuple() (在 parser 模块中)

st_atime (os.stat_result 属性)

ST_ATIME() (在 stat 模块中)

st_atime_ns (os.stat_result 属性)

st_birthtime (os.stat_result 属性)

st_blksize (os.stat_result 属性)

st_blocks (os.stat_result 属性)

st_creator (os.stat_result 属性)

st_ctime (os.stat_result 属性)

ST_CTIME() (在 stat 模块中)

st_ctime_ns (os.stat_result 属性)

st_dev (os.stat_result 属性)

ST_DEV() (在 stat 模块中)

st_file_attributes (os.stat_result 属性)

st_flags (os.stat_result 属性)

st_gen (os.stat_result 属性)

st_gid (os.stat_result 属性)

ST_GID() (在 stat 模块中)

st_ino (os.stat_result 属性)

ST_INO() (在 stat 模块中)

st_mode (os.stat_result 属性)

ST_MODE() (在 stat 模块中)

st_mtime (os.stat_result 属性)

ST_MTIME() (在 stat 模块中)

st_mtime_ns (os.stat_result 属性)

st_nlink (os.stat_result 属性)

ST_NLINK() (在 stat 模块中)

st_rdev (os.stat_result 属性)

st_rsize (os.stat_result 属性)

st_size (os.stat_result 属性)

ST_SIZE() (在 stat 模块中)

st_type (os.stat_result 属性)

st_uid (os.stat_result 属性)

ST_UID() (在 stat 模块中)

stack

execution

trace

stack (traceback.TracebackException 属性)

stack viewer

stack() (在 inspect 模块中)

stack_effect() (在 dis 模块中)

stack_size() (在 _thread 模块中)

(在 threading 模块中)

stackable

streams

StackSummary (traceback 中的类)

stamp() (在 turtle 模块中)

standard

output

Standard C

standard input

standard_b64decode() (在 base64 模块中)

standard_b64encode() (在 base64 模块中)

standarderror (2to3 fixer)

standend() (curses.window 方法)

standout() (curses.window 方法)

STAR() (在 token 模块中)

STAREQUAL() (在 token 模块中)

starmap() (multiprocessing.pool.Pool 方法)

(在 itertools 模块中)

starmap_async() (multiprocessing.pool.Pool 方法)

start (range 属性)

(slice object attribute), [1]

(UnicodeError 属性)

start() (logging.handlers.QueueListener 方法)

(multiprocessing.managers.BaseManager 方法)

(multiprocessing.Process 方法)

(re.match 方法)

(threading.Thread 方法)

(tkinter.ttk.Progressbar 方法)

(xml.etree.ElementTree.TreeBuilder 方法)

(在 tracemalloc 模块中)

start_color() (在 curses 模块中)

start_component() (msilib.Directory 方法)

start_new_thread() (在 _thread 模块中)

start_server() (在 asyncio 模块中)

start_unix_server() (在 asyncio 模块中)

StartCdataSectionHandler() (xml.parsers.expat.xmlparser 方法)

StartDoctypeDeclHandler() (xml.parsers.expat.xmlparser 方法)

startDocument() (xml.sax.handler.ContentHandler 方法)

startElement() (xml.sax.handler.ContentHandler 方法)

StartElementHandler() (xml.parsers.expat.xmlparser 方法)

startElementNS() (xml.sax.handler.ContentHandler 方法)

STARTF_USESHOWWINDOW() (在 subprocess 模块中)

STARTF_USESTDHANDLES() (在 subprocess 模块中)

startfile() (在 os 模块中)

StartNamespaceDeclHandler() (xml.parsers.expat.xmlparser 方法)

startPrefixMapping() (xml.sax.handler.ContentHandler 方法)

startswith() (bytearray 方法)

(bytes 方法)

(str 方法)

startTest() (unittest.TestResult 方法)

startTestRun() (unittest.TestResult 方法)

starttls() (imaplib.IMAP4 方法)

(nntplib.NNTP 方法)

(smtplib.SMTP 方法)

STARTUPINFO (subprocess 中的类)

stat

模块

stat (模块)

stat() (nntplib.NNTP 方法)

(os.DirEntry 方法)

(pathlib.Path 方法)

(poplib.POP3 方法)

(在 os 模块中)

stat_float_times() (在 os 模块中)

stat_result (os 中的类)

state() (tkinter.ttk.Widget 方法)

statement

assignment, [1]

assignment, annotated

assignment, augmented

compound

expression

future

loop, [1], [2], [3]

simple

statement -- 语句

statement grouping

staticmethod

內置函数

staticmethod() (內置函数)

Statistic (tracemalloc 中的类)

StatisticDiff (tracemalloc 中的类)

statistics (模块)

statistics() (tracemalloc.Snapshot 方法)

StatisticsError

Stats (pstats 中的类)

status (http.client.HTTPResponse 属性)

status() (imaplib.IMAP4 方法)

statvfs() (在 os 模块中)

STD_ERROR_HANDLE() (在 subprocess 模块中)

STD_INPUT_HANDLE() (在 subprocess 模块中)

STD_OUTPUT_HANDLE() (在 subprocess 模块中)

StdButtonBox (tkinter.tix 中的类)

stderr (asyncio.asyncio.subprocess.Process 属性)

(in module sys), [1]

(subprocess.CalledProcessError 属性)

(subprocess.CompletedProcess 属性)

(subprocess.Popen 属性)

(subprocess.TimeoutExpired 属性)

stderr() (在 sys 模块中)

stdev() (在 statistics 模块中)

stdin

stdout sdterr

stdin (asyncio.asyncio.subprocess.Process 属性)

(in module sys), [1]

(subprocess.Popen 属性)

stdin() (在 sys 模块中)

stdio

stdout

sdterr, stdin

stdout (asyncio.asyncio.subprocess.Process 属性)

(in module sys), [1]

(subprocess.CalledProcessError 属性)

(subprocess.CompletedProcess 属性)

(subprocess.Popen 属性)

(subprocess.TimeoutExpired 属性)

STDOUT() (在 subprocess 模块中)

stdout() (在 sys 模块中)

step (pdb command)

(range 属性)

(slice object attribute), [1]

step() (tkinter.ttk.Progressbar 方法)

stereocontrols() (ossaudiodev.oss_mixer_device 方法)

stls() (poplib.POP3 方法)

stop (range 属性)

(slice object attribute), [1]

stop() (asyncio.AbstractEventLoop 方法)

(logging.handlers.QueueListener 方法)

(tkinter.ttk.Progressbar 方法)

(unittest.TestResult 方法)

(在 tracemalloc 模块中)

stop_here() (bdb.Bdb 方法)

StopAsyncIteration

例外

StopIteration

例外, [1]

stopListening() (在 logging.config 模块中)

stopTest() (unittest.TestResult 方法)

stopTestRun() (unittest.TestResult 方法)

storbinary() (ftplib.FTP 方法)

store() (imaplib.IMAP4 方法)

STORE_ACTIONS (optparse.Option 属性)

STORE_ANNOTATION (opcode)

STORE_ATTR (opcode)

STORE_DEREF (opcode)

STORE_FAST (opcode)

STORE_GLOBAL (opcode)

STORE_NAME (opcode)

STORE_SUBSCR (opcode)

storlines() (ftplib.FTP 方法)

str (built-in class)

(see also string)

str (內置类)

str() (在 locale 模块中)

strcoll() (在 locale 模块中)

StreamError

StreamHandler (logging 中的类)

StreamReader (asyncio 中的类)

(codecs 中的类)

streamreader (codecs.CodecInfo 属性)

StreamReaderProtocol (asyncio 中的类)

StreamReaderWriter (codecs 中的类)

StreamRecoder (codecs 中的类)

StreamRequestHandler (socketserver 中的类)

streams

stackable

StreamWriter (asyncio 中的类)

(codecs 中的类)

streamwriter (codecs.CodecInfo 属性)

strerror (OSError 属性)

strerror()

(在 os 模块中)

strftime() (datetime.date 方法)

(datetime.datetime 方法)

(datetime.time 方法)

(在 time 模块中)

strict (csv.Dialect 属性)

strict() (在 email.policy 模块中)

strict_domain (http.cookiejar.DefaultCookiePolicy 属性)

strict_errors() (在 codecs 模块中)

strict_ns_domain (http.cookiejar.DefaultCookiePolicy 属性)

strict_ns_set_initial_dollar (http.cookiejar.DefaultCookiePolicy 属性)

strict_ns_set_path (http.cookiejar.DefaultCookiePolicy 属性)

strict_ns_unverifiable (http.cookiejar.DefaultCookiePolicy 属性)

strict_rfc2965_unverifiable (http.cookiejar.DefaultCookiePolicy 属性)

strides (memoryview 属性)

string

__format__() (object method)

__str__() (object method)

conversion, [1]

format() (built-in function)

formatted literal

formatting, printf

immutable sequences

interpolated literal

interpolation, printf

item

methods

object representation

PyObject_Str (C function)

str (built-in class)

str() (built-in function)

text sequence type

对象, [1], [2]

模块

string (re.match 属性)

(模块)

string literal

STRING() (在 token 模块中)

string_at() (在 ctypes 模块中)

StringIO (io 中的类)

stringprep (模块)

strings, documentation, [1]

strip() (bytearray 方法)

(bytes 方法)

(str 方法)

strip_dirs() (pstats.Stats 方法)

stripspaces (curses.textpad.Textbox 属性)

strptime() (datetime.datetime 类方法)

(在 time 模块中)

strtobool() (在 distutils.util 模块中)

struct

模块

Struct (struct 中的类)

struct (模块)

struct sequence

struct_time (time 中的类)

Structure (ctypes 中的类)

structures

C

strxfrm() (在 locale 模块中)

STType() (在 parser 模块中)

style

coding

Style (tkinter.ttk 中的类)

sub() (re.regex 方法)

(在 operator 模块中)

(在 re 模块中)

sub_commands (distutils.cmd.Command 属性)

subclassing

immutable types

subdirs (filecmp.dircmp 属性)

SubElement() (在 xml.etree.ElementTree 模块中)

submit() (concurrent.futures.Executor 方法)

submodule_search_locations (importlib.machinery.ModuleSpec 属性)

subn() (re.regex 方法)

(在 re 模块中)

subnets() (ipaddress.IPv4Network 方法)

(ipaddress.IPv6Network 方法)

Subnormal (decimal 中的类)

suboffsets (memoryview 属性)

subpad() (curses.window 方法)

subprocess (模块)

subprocess_exec() (asyncio.AbstractEventLoop 方法)

subprocess_shell() (asyncio.AbstractEventLoop 方法)

SubprocessError

SubprocessProtocol (asyncio 中的类)

subscribe() (imaplib.IMAP4 方法)

subscript

assignment

operation

subscription, [1], [2], [3]

assignment

subsequent_indent (textwrap.TextWrapper 属性)

subst_vars() (在 distutils.util 模块中)

substitute() (string.Template 方法)

subTest() (unittest.TestCase 方法)

subtract() (collections.Counter 方法)

(decimal.Context 方法)

subtraction

subtype (email.headerregistry.ContentTypeHeader 属性)

subwin() (curses.window 方法)

successful() (multiprocessing.pool.AsyncResult 方法)

suffix_map (mimetypes.MimeTypes 属性)

suffix_map() (在 mimetypes 模块中)

suite

suite() (在 parser 模块中)

suiteClass (unittest.TestLoader 属性)

sum() (內置函数)

sum_list()

sum_sequence(), [1]

summarize() (doctest.DocTestRunner 方法)

summarize_address_range() (在 ipaddress 模块中)

sunau (模块)

super (pyclbr.Class 属性)

super() (內置函数)

supernet() (ipaddress.IPv4Network 方法)

(ipaddress.IPv6Network 方法)

supports_bytes_environ() (在 os 模块中)

supports_dir_fd() (在 os 模块中)

supports_effective_ids() (在 os 模块中)

supports_fd() (在 os 模块中)

supports_follow_symlinks() (在 os 模块中)

supports_unicode_filenames() (在 os.path 模块中)

SupportsAbs (typing 中的类)

SupportsBytes (typing 中的类)

SupportsComplex (typing 中的类)

SupportsFloat (typing 中的类)

SupportsInt (typing 中的类)

SupportsRound (typing 中的类)

suppress() (在 contextlib 模块中)

SuppressCrashReport (test.support 中的类)

SW_HIDE() (在 subprocess 模块中)

swapcase() (bytearray 方法)

(bytes 方法)

(str 方法)

sym_name() (在 symbol 模块中)

Symbol (symtable 中的类)

symbol (模块)

SymbolTable (symtable 中的类)

symlink() (在 os 模块中)

symlink_to() (pathlib.Path 方法)

symmetric_difference() (frozenset 方法)

symmetric_difference_update() (frozenset 方法)

symtable (模块)

symtable() (在 symtable 模块中)

sync() (dbm.dumb.dumbdbm 方法)

(dbm.gnu.gdbm 方法)

(ossaudiodev.oss_audio_device 方法)

(shelve.Shelf 方法)

(在 os 模块中)

syncdown() (curses.window 方法)

synchronized() (在 multiprocessing.sharedctypes 模块中)

SyncManager (multiprocessing.managers 中的类)

syncok() (curses.window 方法)

syncup() (curses.window 方法)

syntax

SyntaxErr

SyntaxError

SyntaxWarning

sys

模块, [1], [2], [3], [4], [5], [6]

sys (模块)

sys.exc_info

sys.last_traceback

sys.meta_path

sys.modules

sys.path

sys.path_hooks

sys.path_importer_cache

sys.stderr

sys.stdin

sys.stdout

sys_exc (2to3 fixer)

sys_version (http.server.BaseHTTPRequestHandler 属性)

sysconf() (在 os 模块中)

sysconf_names() (在 os 模块中)

sysconfig (模块)

syslog (模块)

syslog() (在 syslog 模块中)

SysLogHandler (logging.handlers 中的类)

system() (在 os 模块中)

(在 platform 模块中)

system_alias() (在 platform 模块中)

SystemError

(built-in exception), [1]

SystemExit

(built-in exception)

systemId (xml.dom.DocumentType 属性)

SystemRandom (random 中的类)

(secrets 中的类)

SystemRoot

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – T

T_FMT() (在 locale 模块中)

T_FMT_AMPM() (在 locale 模块中)

tab

tab() (tkinter.ttk.Notebook 方法)

TabError

tabnanny (模块)

tabs() (tkinter.ttk.Notebook 方法)

tabsize (textwrap.TextWrapper 属性)

tabular

data

tag (xml.etree.ElementTree.Element 属性)

tag_bind() (tkinter.ttk.Treeview 方法)

tag_configure() (tkinter.ttk.Treeview 方法)

tag_has() (tkinter.ttk.Treeview 方法)

tagName (xml.dom.Element 属性)

tail (xml.etree.ElementTree.Element 属性)

take_snapshot() (在 tracemalloc 模块中)

takewhile() (在 itertools 模块中)

tan() (在 cmath 模块中)

(在 math 模块中)

tanh() (在 cmath 模块中)

(在 math 模块中)

TarError

TarFile (tarfile 中的类), [1]

tarfile (模块)

tarfile command line option

--create <tarfile> <source1> ... <sourceN>

--extract <tarfile> [<output_dir>]

--list <tarfile>

--test <tarfile>

--verbose

-c <tarfile> <source1> ... <sourceN>

-e <tarfile> [<output_dir>]

-l <tarfile>

-t <tarfile>

-v

target

deletion

list, [1], [2]

list assignment

list, deletion

loop control

target (xml.dom.ProcessingInstruction 属性)

TarInfo (tarfile 中的类)

Task (asyncio 中的类)

task_done() (asyncio.Queue 方法)

(multiprocessing.JoinableQueue 方法)

(queue.Queue 方法)

tau() (在 cmath 模块中)

(在 math 模块中)

tb_frame (traceback attribute)

tb_lasti (traceback attribute)

tb_lineno (traceback attribute)

tb_locals (unittest.TestResult 属性)

tb_next (traceback attribute)

tbreak (pdb command)

tcdrain() (在 termios 模块中)

tcflow() (在 termios 模块中)

tcflush() (在 termios 模块中)

tcgetattr() (在 termios 模块中)

tcgetpgrp() (在 os 模块中)

Tcl() (在 tkinter 模块中)

TCL_LIBRARY

TCPServer (socketserver 中的类)

tcsendbreak() (在 termios 模块中)

tcsetattr() (在 termios 模块中)

tcsetpgrp() (在 os 模块中)

tearDown() (unittest.TestCase 方法)

tearDownClass() (unittest.TestCase 方法)

tee() (在 itertools 模块中)

tell() (aifc.aifc 方法), [1]

(chunk.Chunk 方法)

(io.IOBase 方法)

(io.TextIOBase 方法)

(mmap.mmap 方法)

(sunau.AU_read 方法)

(sunau.AU_write 方法)

(wave.Wave_read 方法)

(wave.Wave_write 方法)

Telnet (telnetlib 中的类)

telnetlib (模块)

TEMP

temp_cwd() (在 test.support 模块中)

temp_dir() (在 test.support 模块中)

temp_umask() (在 test.support 模块中)

tempdir() (在 tempfile 模块中)

tempfile (模块)

Template (pipes 中的类)

(string 中的类)

template (string.Template 属性)

temporary

file

file name

TemporaryDirectory() (在 tempfile 模块中)

TemporaryFile() (在 tempfile 模块中)

teredo (ipaddress.IPv6Address 属性)

TERM, [1]

termattrs() (在 curses 模块中)

terminal_size (os 中的类)

terminate() (asyncio.asyncio.subprocess.Process 方法)

(asyncio.BaseSubprocessTransport 方法)

(multiprocessing.pool.Pool 方法)

(multiprocessing.Process 方法)

(subprocess.Popen 方法)

termination model

termios (模块)

termname() (在 curses 模块中)

ternary

operator

test

identity

membership

test (doctest.DocTestFailure 属性)

(doctest.UnexpectedException 属性)

(模块)

test() (在 cgi 模块中)

test.support (模块)

TestCase (unittest 中的类)

TestFailed

testfile() (在 doctest 模块中)

TESTFN() (在 test.support 模块中)

TestLoader (unittest 中的类)

testMethodPrefix (unittest.TestLoader 属性)

testmod() (在 doctest 模块中)

TestResult (unittest 中的类)

tests() (在 imghdr 模块中)

testsource() (在 doctest 模块中)

testsRun (unittest.TestResult 属性)

TestSuite (unittest 中的类)

testzip() (zipfile.ZipFile 方法)

text (traceback.TracebackException 属性)

Text (typing 中的类)

text (xml.etree.ElementTree.Element 属性)

text encoding -- 文本编码

text file -- 文本文件

text mode

text() (msilib.Dialog 方法)

(在 msilib 模块中)

text_factory (sqlite3.Connection 属性)

Textbox (curses.textpad 中的类)

TextCalendar (calendar 中的类)

textdomain() (在 gettext 模块中)

(在 locale 模块中)

TextFile (distutils.text_file 中的类)

textinput() (在 turtle 模块中)

TextIO (typing 中的类)

TextIOBase (io 中的类)

TextIOWrapper (io 中的类)

TextTestResult (unittest 中的类)

TextTestRunner (unittest 中的类)

textwrap (模块)

TextWrapper (textwrap 中的类)

theme_create() (tkinter.ttk.Style 方法)

theme_names() (tkinter.ttk.Style 方法)

theme_settings() (tkinter.ttk.Style 方法)

theme_use() (tkinter.ttk.Style 方法)

THOUSEP() (在 locale 模块中)

Thread (threading 中的类)

thread() (imaplib.IMAP4 方法)

thread_info() (在 sys 模块中)

threading (模块)

ThreadingMixIn (socketserver 中的类)

ThreadingTCPServer (socketserver 中的类)

ThreadingUDPServer (socketserver 中的类)

ThreadPoolExecutor (concurrent.futures 中的类)

threads

POSIX

throw (2to3 fixer)

throw() (coroutine 方法)

(generator 方法)

ticket_lifetime_hint (ssl.SSLSession 属性)

tigetflag() (在 curses 模块中)

tigetnum() (在 curses 模块中)

tigetstr() (在 curses 模块中)

TILDE() (在 token 模块中)

tilt() (在 turtle 模块中)

tiltangle() (在 turtle 模块中)

time (datetime 中的类)

(ssl.SSLSession 属性)

(模块)

time() (asyncio.AbstractEventLoop 方法)

(datetime.datetime 方法)

(在 time 模块中)

Time2Internaldate() (在 imaplib 模块中)

timedelta (datetime 中的类)

TimedRotatingFileHandler (logging.handlers 中的类)

timegm() (在 calendar 模块中)

timeit (模块)

timeit command line option

--clock

--help

--number=N

--process

--repeat=N

--setup=S

--time

--unit=U

--verbose

-c

-h

-n N

-p

-r N

-s S

-t

-u

-v

timeit() (timeit.Timer 方法)

(在 timeit 模块中)

timeout

(socketserver.BaseServer 属性)

(ssl.SSLSession 属性)

(subprocess.TimeoutExpired 属性)

timeout() (curses.window 方法)

TIMEOUT_MAX() (在 _thread 模块中)

(在 threading 模块中)

TimeoutError, [1], [2], [3]

TimeoutExpired

Timer (threading 中的类)

(timeit 中的类)

times() (在 os 模块中)

timestamp() (datetime.datetime 方法)

timetuple() (datetime.date 方法)

(datetime.datetime 方法)

timetz() (datetime.datetime 方法)

timezone (datetime 中的类)

timezone() (在 time 模块中)

title() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 turtle 模块中)

Tix

tix_addbitmapdir() (tkinter.tix.tixCommand 方法)

tix_cget() (tkinter.tix.tixCommand 方法)

tix_configure() (tkinter.tix.tixCommand 方法)

tix_filedialog() (tkinter.tix.tixCommand 方法)

tix_getbitmap() (tkinter.tix.tixCommand 方法)

tix_getimage() (tkinter.tix.tixCommand 方法)

TIX_LIBRARY

tix_option_get() (tkinter.tix.tixCommand 方法)

tix_resetoptions() (tkinter.tix.tixCommand 方法)

tixCommand (tkinter.tix 中的类)

Tk

(tkinter 中的类)

(tkinter.tix 中的类)

Tk Option Data Types

TK_LIBRARY

Tkinter

tkinter (模块)

tkinter.scrolledtext (模块)

tkinter.tix (模块)

tkinter.ttk (模块)

TList (tkinter.tix 中的类)

TLS

TMP

TMPDIR

to_bytes() (int 方法)

to_eng_string() (decimal.Context 方法)

(decimal.Decimal 方法)

to_integral() (decimal.Decimal 方法)

to_integral_exact() (decimal.Context 方法)

(decimal.Decimal 方法)

to_integral_value() (decimal.Decimal 方法)

to_sci_string() (decimal.Context 方法)

ToASCII() (在 encodings.idna 模块中)

tobuf() (tarfile.TarInfo 方法)

tobytes() (array.array 方法)

(memoryview 方法)

today() (datetime.date 类方法)

(datetime.datetime 类方法)

tofile() (array.array 方法)

tok_name() (在 token 模块中)

token

(shlex.shlex 属性)

(模块)

token_bytes() (在 secrets 模块中)

token_hex() (在 secrets 模块中)

token_urlsafe() (在 secrets 模块中)

TokenError

tokenize (模块)

tokenize command line option

--exact

--help

-e

-h

tokenize() (在 tokenize 模块中)

tolist() (array.array 方法)

(memoryview 方法)

(parser.ST 方法)

tomono() (在 audioop 模块中)

toordinal() (datetime.date 方法)

(datetime.datetime 方法)

top() (curses.panel.Panel 方法)

(poplib.POP3 方法)

top_panel() (在 curses.panel 模块中)

toprettyxml() (xml.dom.minidom.Node 方法)

tostereo() (在 audioop 模块中)

tostring() (array.array 方法)

(在 xml.etree.ElementTree 模块中)

tostringlist() (在 xml.etree.ElementTree 模块中)

total_changes (sqlite3.Connection 属性)

total_ordering() (在 functools 模块中)

total_seconds() (datetime.timedelta 方法)

totuple() (parser.ST 方法)

touch() (pathlib.Path 方法)

touchline() (curses.window 方法)

touchwin() (curses.window 方法)

tounicode() (array.array 方法)

ToUnicode() (在 encodings.idna 模块中)

towards() (在 turtle 模块中)

toxml() (xml.dom.minidom.Node 方法)

tp_as_async (C 成员)

tp_as_mapping (C 成员)

tp_as_number (C 成员)

tp_as_sequence (C 成员)

tparm() (在 curses 模块中)

trace

stack

Trace (trace 中的类)

(tracemalloc 中的类)

trace (模块)

trace command line option

--count

--coverdir=<dir>

--file=<file>

--help

--ignore-dir=<dir>

--ignore-module=<mod>

--listfuncs

--missing

--no-report

--report

--summary

--timing

--trace

--trackcalls

--version

-c

-C

-f

-g

-l

-m

-r

-R

-s

-t

-T

trace function, [1], [2]

trace() (在 inspect 模块中)

trace_dispatch() (bdb.Bdb 方法)

traceback

对象, [1], [2], [3], [4]

Traceback (tracemalloc 中的类)

traceback (tracemalloc.Statistic 属性)

(tracemalloc.StatisticDiff 属性)

(tracemalloc.Trace 属性)

(模块)

traceback_limit (tracemalloc.Snapshot 属性)

(wsgiref.handlers.BaseHandler 属性)

TracebackException (traceback 中的类)

tracebacklimit() (在 sys 模块中)

tracebacks

in CGI scripts

TracebackType() (在 types 模块中)

tracemalloc (模块)

tracer() (在 turtle 模块中)

traces (tracemalloc.Snapshot 属性)

trailing

comma

transfercmd() (ftplib.FTP 方法)

TransientResource (test.support 中的类)

translate() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 fnmatch 模块中)

translation() (在 gettext 模块中)

transport (asyncio.StreamWriter 属性)

Transport Layer Security

traverseproc (C 类型)

Tree (tkinter.tix 中的类)

TreeBuilder (xml.etree.ElementTree 中的类)

Treeview (tkinter.ttk 中的类)

triangular() (在 random 模块中)

triple-quoted string

triple-quoted string -- 三引号字符串

True, [1], [2]

true

True (內置变量)

truediv() (在 operator 模块中)

trunc() (in module math)

(在 math 模块中)

truncate() (io.IOBase 方法)

(在 os 模块中)

truth

value

truth() (在 operator 模块中)

try

语句, [1], [2]

ttk

tty

I/O control

tty (模块)

ttyname() (在 os 模块中)

tuple

display

empty, [1]

singleton

內置函数, [1]

对象, [1], [2], [3], [4], [5], [6]

tuple (內置类)

Tuple() (在 typing 模块中)

tuple2st() (在 parser 模块中)

tuple_params (2to3 fixer)

turnoff_sigfpe() (在 fpectl 模块中)

turnon_sigfpe() (在 fpectl 模块中)

Turtle (turtle 中的类)

turtle (模块)

turtledemo (模块)

turtles() (在 turtle 模块中)

TurtleScreen (turtle 中的类)

turtlesize() (在 turtle 模块中)

type

Boolean

data

hierarchy

immutable data

operations on dictionary

operations on list

內置函数, [1], [2], [3]

对象, [1], [2]

type (optparse.Option 属性)

(socket.socket 属性)

(tarfile.TarInfo 属性)

Type (typing 中的类)

type (urllib.request.Request 属性)

(內置类)

type -- 类型

type alias -- 类型别名

type hint -- 类型提示

type of an object

TYPE_CHECKER (optparse.Option 属性)

TYPE_CHECKING() (在 typing 模块中)

typeahead() (在 curses 模块中)

typecode (array.array 属性)

typecodes() (在 array 模块中)

TYPED_ACTIONS (optparse.Option 属性)

typed_subpart_iterator() (在 email.iterators 模块中)

TypeError

例外

types

built-in

immutable sequence

mutable sequence

operations on integer

operations on mapping

operations on numeric

operations on sequence, [1]

模块

types (2to3 fixer)

TYPES (optparse.Option 属性)

types (模块)

types, internal

types_map (mimetypes.MimeTypes 属性)

types_map() (在 mimetypes 模块中)

types_map_inv (mimetypes.MimeTypes 属性)

TypeVar (typing 中的类)

typing (模块)

TZ, [1], [2], [3], [4]

tzinfo (datetime 中的类)

(datetime.datetime 属性)

(datetime.time 属性)

tzname() (datetime.datetime 方法)

(datetime.time 方法)

(datetime.timezone 方法)

(datetime.tzinfo 方法)

(在 time 模块中)

tzset() (在 time 模块中)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – U

u"

string literal

u'

string literal

u-LAW, [1], [2]

ucd_3_2_0() (在 unicodedata 模块中)

udata (select.kevent 属性)

UDPServer (socketserver 中的类)

UF_APPEND() (在 stat 模块中)

UF_COMPRESSED() (在 stat 模块中)

UF_HIDDEN() (在 stat 模块中)

UF_IMMUTABLE() (在 stat 模块中)

UF_NODUMP() (在 stat 模块中)

UF_NOUNLINK() (在 stat 模块中)

UF_OPAQUE() (在 stat 模块中)

uid (tarfile.TarInfo 属性)

uid() (imaplib.IMAP4 方法)

uidl() (poplib.POP3 方法)

ulaw2lin() (在 audioop 模块中)

ULONG_MAX

umask() (在 os 模块中)

unalias (pdb command)

uname (tarfile.TarInfo 属性)

uname() (在 os 模块中)

(在 platform 模块中)

unary

arithmetic operation

bitwise operation

UNARY_INVERT (opcode)

UNARY_NEGATIVE (opcode)

UNARY_NOT (opcode)

UNARY_POSITIVE (opcode)

unbinding

name

UnboundLocalError, [1]

unbuffered I/O

UNC paths

and os.makedirs()

unconsumed_tail (zlib.Decompress 属性)

unctrl() (在 curses 模块中)

(在 curses.ascii 模块中)

undefine_macro() (distutils.ccompiler.CCompiler 方法)

Underflow (decimal 中的类)

undisplay (pdb command)

undo() (在 turtle 模块中)

undobufferentries() (在 turtle 模块中)

undoc_header (cmd.Cmd 属性)

unescape() (在 html 模块中)

(在 xml.sax.saxutils 模块中)

UnexpectedException

unexpectedSuccesses (unittest.TestResult 属性)

unget_wch() (在 curses 模块中)

ungetch() (在 curses 模块中)

(在 msvcrt 模块中)

ungetmouse() (在 curses 模块中)

ungetwch() (在 msvcrt 模块中)

unhexlify() (在 binascii 模块中)

Unicode, [1], [2]

database

unicode (2to3 fixer)

Unicode Consortium

unicodedata (模块)

UnicodeDecodeError

UnicodeEncodeError

UnicodeError

UnicodeTranslateError

UnicodeWarning

unidata_version() (在 unicodedata 模块中)

unified_diff() (在 difflib 模块中)

uniform() (在 random 模块中)

UnimplementedFileMode

Union (ctypes 中的类)

union() (frozenset 方法)

Union() (在 typing 模块中)

unique() (在 enum 模块中), [1]

unittest (模块)

unittest command line option

--buffer

--catch

--failfast

--locals

-b

-c

-f

unittest-discover command line option

--pattern pattern

--start-directory directory

--top-level-directory directory

--verbose

-p

-s

-t

-v

unittest.mock (模块)

universal newlines

bytearray.splitlines method

bytes.splitlines method

csv.reader function

importlib.abc.InspectLoader.get_source method

io.IncrementalNewlineDecoder class

io.TextIOWrapper class

open() built-in function

str.splitlines method

subprocess module

What's new, [1], [2], [3]

universal newlines -- 通用换行

UNIX

file control

I/O control

unix_dialect (csv 中的类)

UnixDatagramServer (socketserver 中的类)

UnixStreamServer (socketserver 中的类)

unknown_decl() (html.parser.HTMLParser 方法)

unknown_open() (urllib.request.BaseHandler 方法)

(urllib.request.UnknownHandler 方法)

UnknownHandler (urllib.request 中的类)

UnknownProtocol

UnknownTransferEncoding

unlink() (pathlib.Path 方法)

(xml.dom.minidom.Node 方法)

(在 os 模块中)

unlock() (mailbox.Babyl 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(mailbox.mbox 方法)

(mailbox.MH 方法)

(mailbox.MMDF 方法)

unpack() (struct.Struct 方法)

(在 struct 模块中)

unpack_archive() (在 shutil 模块中)

unpack_array() (xdrlib.Unpacker 方法)

unpack_bytes() (xdrlib.Unpacker 方法)

unpack_double() (xdrlib.Unpacker 方法)

UNPACK_EX (opcode)

unpack_farray() (xdrlib.Unpacker 方法)

unpack_float() (xdrlib.Unpacker 方法)

unpack_fopaque() (xdrlib.Unpacker 方法)

unpack_from() (struct.Struct 方法)

(在 struct 模块中)

unpack_fstring() (xdrlib.Unpacker 方法)

unpack_list() (xdrlib.Unpacker 方法)

unpack_opaque() (xdrlib.Unpacker 方法)

UNPACK_SEQUENCE (opcode)

unpack_string() (xdrlib.Unpacker 方法)

Unpacker (xdrlib 中的类)

unpacking

dictionary

in function calls

iterable

unparsedEntityDecl() (xml.sax.handler.DTDHandler 方法)

UnparsedEntityDeclHandler() (xml.parsers.expat.xmlparser 方法)

Unpickler (pickle 中的类)

UnpicklingError

unquote() (在 email.utils 模块中)

(在 urllib.parse 模块中)

unquote_plus() (在 urllib.parse 模块中)

unquote_to_bytes() (在 urllib.parse 模块中)

unreachable object

unreadline() (distutils.text_file.TextFile 方法)

unrecognized escape sequence

unregister() (select.devpoll 方法)

(select.epoll 方法)

(select.poll 方法)

(selectors.BaseSelector 方法)

(在 atexit 模块中)

(在 faulthandler 模块中)

unregister_archive_format() (在 shutil 模块中)

unregister_dialect() (在 csv 模块中)

unregister_unpack_format() (在 shutil 模块中)

unset() (test.support.EnvironmentVarGuard 方法)

unsetenv() (在 os 模块中)

UnstructuredHeader (email.headerregistry 中的类)

unsubscribe() (imaplib.IMAP4 方法)

UnsupportedOperation

until (pdb command)

untokenize() (在 tokenize 模块中)

untouchwin() (curses.window 方法)

unused_data (bz2.BZ2Decompressor 属性)

(lzma.LZMADecompressor 属性)

(zlib.Decompress 属性)

unverifiable (urllib.request.Request 属性)

unwrap() (ssl.SSLSocket 方法)

(在 inspect 模块中)

up (pdb command)

up() (在 turtle 模块中)

update() (collections.Counter 方法)

(dict 方法)

(frozenset 方法)

(hashlib.hash 方法)

(hmac.HMAC 方法)

(http.cookies.Morsel 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(trace.CoverageResults 方法)

(在 turtle 模块中)

update_authenticated() (urllib.request.HTTPPasswordMgrWithPriorAuth 方法)

update_lines_cols() (在 curses 模块中)

update_panels() (在 curses.panel 模块中)

update_visible() (mailbox.BabylMessage 方法)

update_wrapper() (在 functools 模块中)

upper() (bytearray 方法)

(bytes 方法)

(str 方法)

urandom() (在 os 模块中)

URL, [1], [2], [3]

parsing

relative

url (xmlrpc.client.ProtocolError 属性)

url2pathname() (在 urllib.request 模块中)

urlcleanup() (在 urllib.request 模块中)

urldefrag() (在 urllib.parse 模块中)

urlencode() (在 urllib.parse 模块中)

URLError

urljoin() (在 urllib.parse 模块中)

urllib (2to3 fixer)

(模块)

urllib.error (模块)

urllib.parse (模块)

urllib.request

模块

urllib.request (模块)

urllib.response (模块)

urllib.robotparser (模块)

urlopen() (在 urllib.request 模块中)

URLopener (urllib.request 中的类)

urlparse() (在 urllib.parse 模块中)

urlretrieve() (在 urllib.request 模块中)

urlsafe_b64decode() (在 base64 模块中)

urlsafe_b64encode() (在 base64 模块中)

urlsplit() (在 urllib.parse 模块中)

urlunparse() (在 urllib.parse 模块中)

urlunsplit() (在 urllib.parse 模块中)

urn (uuid.UUID 属性)

use_default_colors() (在 curses 模块中)

use_env() (在 curses 模块中)

use_rawinput (cmd.Cmd 属性)

UseForeignDTD() (xml.parsers.expat.xmlparser 方法)

USER

user

effective id

id

id, setting

user() (poplib.POP3 方法)

user-defined

function

function call

method

user-defined function

对象, [1], [2]

user-defined method

对象

USER_BASE

USER_BASE() (在 site 模块中)

user_call() (bdb.Bdb 方法)

user_exception() (bdb.Bdb 方法)

user_line() (bdb.Bdb 方法)

user_return() (bdb.Bdb 方法)

USER_SITE() (在 site 模块中)

usercustomize

模块

UserDict (collections 中的类)

UserList (collections 中的类)

USERNAME, [1]

username (email.headerregistry.Address 属性)

USERPROFILE, [1]

userptr() (curses.panel.Panel 方法)

UserString (collections 中的类)

UserWarning

USTAR_FORMAT() (在 tarfile 模块中)

UTC

utc (datetime.timezone 属性)

utcfromtimestamp() (datetime.datetime 类方法)

utcnow() (datetime.datetime 类方法)

utcoffset() (datetime.datetime 方法)

(datetime.time 方法)

(datetime.timezone 方法)

(datetime.tzinfo 方法)

utctimetuple() (datetime.datetime 方法)

utf8 (email.policy.EmailPolicy 属性)

utf8() (poplib.POP3 方法)

utf8_enabled (imaplib.IMAP4 属性)

utime() (在 os 模块中)

uu

模块

uu (模块)

UUID (uuid 中的类)

uuid (模块)

uuid1

uuid1() (在 uuid 模块中)

uuid3

uuid3() (在 uuid 模块中)

uuid4

uuid4() (在 uuid 模块中)

uuid5

uuid5() (在 uuid 模块中)

UuidCreate() (在 msilib 模块中)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – V

v4_int_to_packed() (在 ipaddress 模块中)

v6_int_to_packed() (在 ipaddress 模块中)

validator() (在 wsgiref.validate 模块中)

value

default parameter

truth

value (ctypes._SimpleCData 属性)

(http.cookiejar.Cookie 属性)

(http.cookies.Morsel 属性)

(xml.dom.Attr 属性)

value of an object

Value() (multiprocessing.managers.SyncManager 方法)

(在 multiprocessing 模块中)

(在 multiprocessing.sharedctypes 模块中)

value_decode() (http.cookies.BaseCookie 方法)

value_encode() (http.cookies.BaseCookie 方法)

ValueError

例外

valuerefs() (weakref.WeakValueDictionary 方法)

values

Boolean

writing

values() (dict 方法)

(email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(types.MappingProxyType 方法)

ValuesView (collections.abc 中的类)

(typing 中的类)

variable

free

variable annotation -- 变量标注

variance() (在 statistics 模块中)

variant (uuid.UUID 属性)

vars() (內置函数)

vbar (tkinter.scrolledtext.ScrolledText 属性)

VBAR() (在 token 模块中)

VBAREQUAL() (在 token 模块中)

Vec2D (turtle 中的类)

venv (模块)

VERBOSE() (在 re 模块中)

verbose() (在 tabnanny 模块中)

(在 test.support 模块中)

verify() (smtplib.SMTP 方法)

verify_client_post_handshake() (ssl.SSLSocket 方法)

VERIFY_CRL_CHECK_CHAIN() (在 ssl 模块中)

VERIFY_CRL_CHECK_LEAF() (在 ssl 模块中)

VERIFY_DEFAULT() (在 ssl 模块中)

verify_flags (ssl.SSLContext 属性)

verify_mode (ssl.SSLContext 属性)

verify_request() (socketserver.BaseServer 方法)

VERIFY_X509_STRICT() (在 ssl 模块中)

VERIFY_X509_TRUSTED_FIRST() (在 ssl 模块中)

VerifyFlags (ssl 中的类)

VerifyMode (ssl 中的类)

version (email.headerregistry.MIMEVersionHeader 属性)

(http.client.HTTPResponse 属性)

(http.cookiejar.Cookie 属性)

(in module sys), [1], [2]

(ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

(urllib.request.URLopener 属性)

(uuid.UUID 属性)

version() (ssl.SSLSocket 方法)

(在 curses 模块中)

(在 ensurepip 模块中)

(在 marshal 模块中)

(在 platform 模块中)

(在 sqlite3 模块中)

(在 sys 模块中)

version_info() (在 sqlite3 模块中)

(在 sys 模块中)

version_string() (http.server.BaseHTTPRequestHandler 方法)

vformat() (string.Formatter 方法)

virtual

Environments

virtual environment -- 虚拟环境

virtual machine -- 虚拟机

visit() (ast.NodeVisitor 方法)

visitproc (C 类型)

vline() (curses.window 方法)

voidcmd() (ftplib.FTP 方法)

volume (zipfile.ZipInfo 属性)

vonmisesvariate() (在 random 模块中)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – W

W_OK() (在 os 模块中)

wait() (asyncio.asyncio.subprocess.Process 方法)

(asyncio.Condition 方法)

(asyncio.Event 方法)

(multiprocessing.pool.AsyncResult 方法)

(subprocess.Popen 方法)

(threading.Barrier 方法)

(threading.Condition 方法)

(threading.Event 方法)

(在 asyncio 模块中)

(在 concurrent.futures 模块中)

(在 multiprocessing.connection 模块中)

(在 os 模块中)

wait3() (在 os 模块中)

wait4() (在 os 模块中)

wait_closed() (asyncio.Server 方法)

wait_for() (asyncio.Condition 方法)

(threading.Condition 方法)

(在 asyncio 模块中)

waitid() (在 os 模块中)

waitpid() (在 os 模块中)

walk() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(在 ast 模块中)

(在 os 模块中)

walk_packages() (在 pkgutil 模块中)

walk_stack() (在 traceback 模块中)

walk_tb() (在 traceback 模块中)

want (doctest.Example 属性)

warn() (distutils.ccompiler.CCompiler 方法)

(distutils.text_file.TextFile 方法)

(在 warnings 模块中)

warn_explicit() (在 warnings 模块中)

Warning, [1]

warning() (logging.Logger 方法)

(xml.sax.handler.ErrorHandler 方法)

(在 logging 模块中)

warnings

(模块)

WarningsRecorder (test.support 中的类)

warnoptions() (在 sys 模块中)

wasSuccessful() (unittest.TestResult 方法)

WatchedFileHandler (logging.handlers 中的类)

wave (模块)

WCONTINUED() (在 os 模块中)

WCOREDUMP() (在 os 模块中)

WeakKeyDictionary (weakref 中的类)

WeakMethod (weakref 中的类)

weakref (模块)

WeakSet (weakref 中的类)

WeakValueDictionary (weakref 中的类)

webbrowser (模块)

weekday() (datetime.date 方法)

(datetime.datetime 方法)

(在 calendar 模块中)

weekheader() (在 calendar 模块中)

weibullvariate() (在 random 模块中)

WEXITED() (在 os 模块中)

WEXITSTATUS() (在 os 模块中)

wfile (http.server.BaseHTTPRequestHandler 属性)

what() (在 imghdr 模块中)

(在 sndhdr 模块中)

whathdr() (在 sndhdr 模块中)

whatis (pdb command)

where (pdb command)

which() (在 shutil 模块中)

whichdb() (在 dbm 模块中)

while

语句, [1], [2], [3]

whitespace (shlex.shlex 属性)

whitespace() (在 string 模块中)

whitespace_split (shlex.shlex 属性)

Widget (tkinter.ttk 中的类)

width (textwrap.TextWrapper 属性)

width() (在 turtle 模块中)

WIFCONTINUED() (在 os 模块中)

WIFEXITED() (在 os 模块中)

WIFSIGNALED() (在 os 模块中)

WIFSTOPPED() (在 os 模块中)

win32_ver() (在 platform 模块中)

WinDLL (ctypes 中的类)

window manager (widgets)

window() (curses.panel.Panel 方法)

window_height() (在 turtle 模块中)

window_width() (在 turtle 模块中)

Windows

Windows ini file

WindowsError

WindowsPath (pathlib 中的类)

WindowsRegistryFinder (importlib.machinery 中的类)

winerror (OSError 属性)

WinError() (在 ctypes 模块中)

WINFUNCTYPE() (在 ctypes 模块中)

winreg (模块)

WinSock

winsound (模块)

winver() (在 sys 模块中)

with

语句, [1]

WITH_CLEANUP_FINISH (opcode)

WITH_CLEANUP_START (opcode)

with_hostmask (ipaddress.IPv4Interface 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Interface 属性)

(ipaddress.IPv6Network 属性)

with_name() (pathlib.PurePath 方法)

with_netmask (ipaddress.IPv4Interface 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Interface 属性)

(ipaddress.IPv6Network 属性)

with_prefixlen (ipaddress.IPv4Interface 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Interface 属性)

(ipaddress.IPv6Network 属性)

with_suffix() (pathlib.PurePath 方法)

with_traceback() (BaseException 方法)

WNOHANG() (在 os 模块中)

WNOWAIT() (在 os 模块中)

wordchars (shlex.shlex 属性)

World Wide Web, [1], [2]

wrap() (textwrap.TextWrapper 方法)

(在 textwrap 模块中)

wrap_bio() (ssl.SSLContext 方法)

wrap_future() (在 asyncio 模块中)

wrap_socket() (ssl.SSLContext 方法)

(在 ssl 模块中)

wrap_text() (在 distutils.fancy_getopt 模块中)

wrapper() (在 curses 模块中)

wraps() (在 functools 模块中)

writable() (asyncore.dispatcher 方法)

(io.IOBase 方法)

WRITABLE() (在 tkinter 模块中)

write() (asyncio.StreamWriter 方法)

(asyncio.WriteTransport 方法)

(code.InteractiveInterpreter 方法)

(codecs.StreamWriter 方法)

(configparser.ConfigParser 方法)

(email.generator.BytesGenerator 方法)

(email.generator.Generator 方法)

(io.BufferedIOBase 方法)

(io.BufferedWriter 方法)

(io.RawIOBase 方法)

(io.TextIOBase 方法)

(mmap.mmap 方法)

(ossaudiodev.oss_audio_device 方法)

(ssl.MemoryBIO 方法)

(ssl.SSLSocket 方法)

(telnetlib.Telnet 方法)

(xml.etree.ElementTree.ElementTree 方法)

(zipfile.ZipFile 方法)

(在 os 模块中)

(在 turtle 模块中)

write_byte() (mmap.mmap 方法)

write_bytes() (pathlib.Path 方法)

write_docstringdict() (在 turtle 模块中)

write_eof() (asyncio.StreamWriter 方法)

(asyncio.WriteTransport 方法)

(ssl.MemoryBIO 方法)

write_file() (在 distutils.file_util 模块中)

write_history_file() (在 readline 模块中)

WRITE_RESTRICTED

write_results() (trace.CoverageResults 方法)

write_text() (pathlib.Path 方法)

writeall() (ossaudiodev.oss_audio_device 方法)

writeframes() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

writeframesraw() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

writeheader() (csv.DictWriter 方法)

writelines() (asyncio.StreamWriter 方法)

(asyncio.WriteTransport 方法)

(codecs.StreamWriter 方法)

(io.IOBase 方法)

writePlist() (在 plistlib 模块中)

writePlistToBytes() (在 plistlib 模块中)

writepy() (zipfile.PyZipFile 方法)

writer (formatter.formatter 属性)

writer() (在 csv 模块中)

writerow() (csv.csvwriter 方法)

writerows() (csv.csvwriter 方法)

writestr() (zipfile.ZipFile 方法)

WriteTransport (asyncio 中的类)

writev() (在 os 模块中)

writexml() (xml.dom.minidom.Node 方法)

writing

values

WrongDocumentErr

ws_comma (2to3 fixer)

wsgi_file_wrapper (wsgiref.handlers.BaseHandler 属性)

wsgi_multiprocess (wsgiref.handlers.BaseHandler 属性)

wsgi_multithread (wsgiref.handlers.BaseHandler 属性)

wsgi_run_once (wsgiref.handlers.BaseHandler 属性)

wsgiref (模块)

wsgiref.handlers (模块)

wsgiref.headers (模块)

wsgiref.simple_server (模块)

wsgiref.util (模块)

wsgiref.validate (模块)

WSGIRequestHandler (wsgiref.simple_server 中的类)

WSGIServer (wsgiref.simple_server 中的类)

wShowWindow (subprocess.STARTUPINFO 属性)

WSTOPPED() (在 os 模块中)

WSTOPSIG() (在 os 模块中)

wstring_at() (在 ctypes 模块中)

WTERMSIG() (在 os 模块中)

WUNTRACED() (在 os 模块中)

WWW, [1], [2]

server, [1]

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – X

X() (在 re 模块中)

X509 certificate

X_OK() (在 os 模块中)

xatom() (imaplib.IMAP4 方法)

XATTR_CREATE() (在 os 模块中)

XATTR_REPLACE() (在 os 模块中)

XATTR_SIZE_MAX() (在 os 模块中)

xcor() (在 turtle 模块中)

XDR

xdrlib (模块)

xhdr() (nntplib.NNTP 方法)

XHTML

XHTML_NAMESPACE() (在 xml.dom 模块中)

xml (模块)

XML() (在 xml.etree.ElementTree 模块中)

xml.dom (模块)

xml.dom.minidom (模块)

xml.dom.pulldom (模块)

xml.etree.ElementTree (模块)

xml.parsers.expat (模块)

xml.parsers.expat.errors (模块)

xml.parsers.expat.model (模块)

xml.sax (模块)

xml.sax.handler (模块)

xml.sax.saxutils (模块)

xml.sax.xmlreader (模块)

XML_ERROR_ABORTED() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_ASYNC_ENTITY() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_BAD_CHAR_REF() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_BINARY_ENTITY_REF() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_DUPLICATE_ATTRIBUTE() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_ENTITY_DECLARED_IN_PE() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_EXTERNAL_ENTITY_HANDLING() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_FEATURE_REQUIRES_XML_DTD() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_FINISHED() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_INCOMPLETE_PE() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_INCORRECT_ENCODING() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_INVALID_TOKEN() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_JUNK_AFTER_DOC_ELEMENT() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_MISPLACED_XML_PI() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_NO_ELEMENTS() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_NO_MEMORY() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_NOT_STANDALONE() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_NOT_SUSPENDED() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_PARAM_ENTITY_REF() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_PARTIAL_CHAR() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_PUBLICID() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_RECURSIVE_ENTITY_REF() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_SUSPEND_PE() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_SUSPENDED() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_SYNTAX() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_TAG_MISMATCH() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_TEXT_DECL() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_UNBOUND_PREFIX() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_UNCLOSED_CDATA_SECTION() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_UNCLOSED_TOKEN() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_UNDECLARING_PREFIX() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_UNDEFINED_ENTITY() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_UNEXPECTED_STATE() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_UNKNOWN_ENCODING() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_XML_DECL() (在 xml.parsers.expat.errors 模块中)

XML_NAMESPACE() (在 xml.dom 模块中)

xmlcharrefreplace_errors() (在 codecs 模块中)

XmlDeclHandler() (xml.parsers.expat.xmlparser 方法)

XMLFilterBase (xml.sax.saxutils 中的类)

XMLGenerator (xml.sax.saxutils 中的类)

XMLID() (在 xml.etree.ElementTree 模块中)

XMLNS_NAMESPACE() (在 xml.dom 模块中)

XMLParser (xml.etree.ElementTree 中的类)

XMLParserType() (在 xml.parsers.expat 模块中)

XMLPullParser (xml.etree.ElementTree 中的类)

XMLReader (xml.sax.xmlreader 中的类)

xmlrpc.client (模块)

xmlrpc.server (模块)

xor

bitwise

xor() (在 operator 模块中)

xover() (nntplib.NNTP 方法)

xpath() (nntplib.NNTP 方法)

xrange (2to3 fixer)

xreadlines (2to3 fixer)

xview() (tkinter.ttk.Treeview 方法)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – Y

Y2K

ycor() (在 turtle 模块中)

year (datetime.date 属性)

(datetime.datetime 属性)

Year 2000

Year 2038

yeardatescalendar() (calendar.Calendar 方法)

yeardays2calendar() (calendar.Calendar 方法)

yeardayscalendar() (calendar.Calendar 方法)

YESEXPR() (在 locale 模块中)

yield

examples

expression

yield from (in What's New)

关键字

语句

YIELD_FROM (opcode)

YIELD_VALUE (opcode)

yiq_to_rgb() (在 colorsys 模块中)

yview() (tkinter.ttk.Treeview 方法)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – Z

Zen of Python -- Python 之禅

ZeroDivisionError

例外

zfill() (bytearray 方法)

(bytes 方法)

(str 方法)

zip (2to3 fixer)

zip() (內置函数)

ZIP_BZIP2() (在 zipfile 模块中)

ZIP_DEFLATED() (在 zipfile 模块中)

zip_longest() (在 itertools 模块中)

ZIP_LZMA() (在 zipfile 模块中)

ZIP_STORED() (在 zipfile 模块中)

zipapp (模块)

zipapp command line option

--help

--info

--main=<mainfn>

--output=<output>

--python=<interpreter>

-h

-m <mainfn>

-o <output>

-p <interpreter>

ZipFile (zipfile 中的类)

zipfile (模块)

zipfile command line option

-c <zipfile> <source1> ... <sourceN>

-e <zipfile> <output_dir>

-l <zipfile>

-t <zipfile>

zipimport (模块)

zipimporter (zipimport 中的类)

ZipImportError

ZipInfo (zipfile 中的类)

zlib (模块)

ZLIB_RUNTIME_VERSION() (在 zlib 模块中)

ZLIB_VERSION() (在 zlib 模块中)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – _

_ (underscore)

gettext

in numeric literal, [1]

in string formatting

_, identifiers

__, identifiers

__abs__() (object 方法)

(在 operator 模块中)

__add__() (object 方法)

(在 operator 模块中)

__aenter__() (object 方法)

__aexit__() (object 方法)

__aiter__() (object 方法)

__all__

(optional module attribute)

(package variable)

__and__() (object 方法)

(在 operator 模块中)

__anext__() (agen 方法)

(object 方法)

__annotations__ (class attribute)

(function attribute)

(module attribute)

__await__() (object 方法)

__bases__ (class attribute)

(class 属性)

__bool__() (object method)

(object 方法)

__bytes__() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(object 方法)

__cached__

__call__() (email.headerregistry.HeaderRegistry 方法)

(object method)

(object 方法)

(weakref.finalize 方法)

__callback__ (weakref.ref 属性)

__cause__ (exception attribute)

(traceback.TracebackException 属性)

__ceil__() (fractions.Fraction 方法)

(object 方法)

__class__ (instance attribute)

(instance 属性)

(method cell)

(module attribute)

(unittest.mock.Mock 属性)

__classcell__ (class namespace entry)

__closure__ (function attribute)

__code__ (function attribute)

(function object attribute)

__complex__() (object 方法)

__concat__() (在 operator 模块中)

__contains__() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(object 方法)

(在 operator 模块中)

__context__ (exception attribute)

(traceback.TracebackException 属性)

__copy__() (copy protocol)

__debug__

(內置变量)

__deepcopy__() (copy protocol)

__defaults__ (function attribute)

__del__() (io.IOBase 方法)

(object 方法)

__delattr__() (object 方法)

__delete__() (object 方法)

__delitem__() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(mailbox.MH 方法)

(object 方法)

(在 operator 模块中)

__dict__ (class attribute)

(function attribute)

(instance attribute)

(module attribute), [1]

(object 属性)

__dir__() (object 方法)

(unittest.mock.Mock 方法)

__displayhook__() (在 sys 模块中)

__divmod__() (object 方法)

__doc__ (class attribute)

(function attribute)

(method attribute)

(module attribute), [1]

(types.ModuleType 属性)

__enter__() (contextmanager 方法)

(object 方法)

(winreg.PyHKEY 方法)

__eq__() (email.charset.Charset 方法)

(email.header.Header 方法)

(instance method)

(memoryview 方法)

(object 方法)

(在 operator 模块中)

__excepthook__() (在 sys 模块中)

__exit__() (contextmanager 方法)

(object 方法)

(winreg.PyHKEY 方法)

__file__

(module attribute), [1], [2]

__float__() (object 方法)

__floor__() (fractions.Fraction 方法)

(object 方法)

__floordiv__() (object 方法)

(在 operator 模块中)

__format__

__format__() (datetime.date 方法)

(datetime.datetime 方法)

(datetime.time 方法)

(object 方法)

__fspath__() (os.PathLike 方法)

__func__ (method attribute)

__future__

future statement

__future__ (模块)

__ge__() (instance method)

(object 方法)

(在 operator 模块中)

__get__() (object 方法)

__getattr__() (object 方法)

__getattribute__() (object 方法)

__getitem__() (email.headerregistry.HeaderRegistry 方法)

(email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(mapping object method)

(object 方法)

(re.match 方法)

(在 operator 模块中)

__getnewargs__() (object 方法)

__getnewargs_ex__() (object 方法)

__getstate__() (copy protocol)

(object 方法)

__globals__ (function attribute)

__gt__() (instance method)

(object 方法)

(在 operator 模块中)

__hash__() (object 方法)

__iadd__() (object 方法)

(在 operator 模块中)

__iand__() (object 方法)

(在 operator 模块中)

__iconcat__() (在 operator 模块中)

__ifloordiv__() (object 方法)

(在 operator 模块中)

__ilshift__() (object 方法)

(在 operator 模块中)

__imatmul__() (object 方法)

(在 operator 模块中)

__imod__() (object 方法)

(在 operator 模块中)

__import__

內置函数

__import__() (內置函数)

(在 importlib 模块中)

__imul__() (object 方法)

(在 operator 模块中)

__index__() (object 方法)

(在 operator 模块中)

__init__() (difflib.HtmlDiff 方法)

(logging.Handler 方法)

(logging.logging.Formatter 方法)

(object 方法)

__init_subclass__() (object 类方法)

__instancecheck__() (class 方法)

__int__() (object 方法)

__interactivehook__() (在 sys 模块中)

__inv__() (在 operator 模块中)

__invert__() (object 方法)

(在 operator 模块中)

__ior__() (object 方法)

(在 operator 模块中)

__ipow__() (object 方法)

(在 operator 模块中)

__irshift__() (object 方法)

(在 operator 模块中)

__isub__() (object 方法)

(在 operator 模块中)

__iter__() (container 方法)

(iterator 方法)

(mailbox.Mailbox 方法)

(object 方法)

(unittest.TestSuite 方法)

__itruediv__() (object 方法)

(在 operator 模块中)

__ixor__() (object 方法)

(在 operator 模块中)

__kwdefaults__ (function attribute)

__le__() (instance method)

(object 方法)

(在 operator 模块中)

__len__() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(mapping object method)

(object 方法)

__length_hint__() (object 方法)

__loader__

(module attribute)

(types.ModuleType 属性)

__lshift__() (object 方法)

(在 operator 模块中)

__lt__() (instance method)

(object 方法)

(在 operator 模块中)

__main__

模块, [1], [2], [3], [4], [5], [6], [7]

__main__ (模块)

__matmul__() (object 方法)

(在 operator 模块中)

__missing__()

(collections.defaultdict 方法)

(object 方法)

__mod__() (object 方法)

(在 operator 模块中)

__module__ (class attribute)

(function attribute)

(method attribute)

__mro__ (class 属性)

__mul__() (object 方法)

(在 operator 模块中)

__name__

(class attribute)

(definition 属性)

(function attribute)

(method attribute)

(module attribute), [1], [2]

(types.ModuleType 属性)

__ne__() (email.charset.Charset 方法)

(email.header.Header 方法)

(instance method)

(object 方法)

(在 operator 模块中)

__neg__() (object 方法)

(在 operator 模块中)

__new__() (object 方法)

__next__() (csv.csvreader 方法)

(generator 方法)

(iterator 方法)

__not__() (在 operator 模块中)

__or__() (object 方法)

(在 operator 模块中)

__package__

(module attribute)

(types.ModuleType 属性)

__path__

__pos__() (object 方法)

(在 operator 模块中)

__pow__() (object 方法)

(在 operator 模块中)

__prepare__ (metaclass method)

__qualname__ (definition 属性)

__radd__() (object 方法)

__rand__() (object 方法)

__rdivmod__() (object 方法)

__reduce__() (object 方法)

__reduce_ex__() (object 方法)

__repr__() (multiprocessing.managers.BaseProxy 方法)

(netrc.netrc 方法)

(object 方法)

__reversed__() (object 方法)

__rfloordiv__() (object 方法)

__rlshift__() (object 方法)

__rmatmul__() (object 方法)

__rmod__() (object 方法)

__rmul__() (object 方法)

__ror__() (object 方法)

__round__() (fractions.Fraction 方法)

(object 方法)

__rpow__() (object 方法)

__rrshift__() (object 方法)

__rshift__() (object 方法)

(在 operator 模块中)

__rsub__() (object 方法)

__rtruediv__() (object 方法)

__rxor__() (object 方法)

__self__ (method attribute)

__set__() (object 方法)

__set_name__() (object 方法)

__setattr__() (object 方法)

__setitem__() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(object 方法)

(在 operator 模块中)

__setstate__() (copy protocol)

(object 方法)

__slots__

__spec__

__stderr__() (在 sys 模块中)

__stdin__() (在 sys 模块中)

__stdout__() (在 sys 模块中)

__str__() (datetime.date 方法)

(datetime.datetime 方法)

(datetime.time 方法)

(email.charset.Charset 方法)

(email.header.Header 方法)

(email.headerregistry.Address 方法)

(email.headerregistry.Group 方法)

(email.message.EmailMessage 方法)

(email.message.Message 方法)

(multiprocessing.managers.BaseProxy 方法)

(object 方法)

__sub__() (object 方法)

(在 operator 模块中)

__subclasscheck__() (class 方法)

__subclasses__() (class 方法)

__subclasshook__() (abc.ABCMeta 方法)

__suppress_context__ (traceback.TracebackException 属性)

__traceback__ (exception attribute)

__truediv__() (object 方法)

(在 operator 模块中)

__trunc__() (object 方法)

__xor__() (object 方法)

(在 operator 模块中)

_anonymous_ (ctypes.Structure 属性)

_asdict() (collections.somenamedtuple 方法)

_b_base_ (ctypes._CData 属性)

_b_needsfree_ (ctypes._CData 属性)

_callmethod() (multiprocessing.managers.BaseProxy 方法)

_CData (ctypes 中的类)

_clear_type_cache() (在 sys 模块中)

_current_frames() (在 sys 模块中)

_debugmallocstats() (在 sys 模块中)

_dummy_thread (模块)

_enablelegacywindowsfsencoding() (在 sys 模块中)

_exit() (在 os 模块中)

_fields (ast.AST 属性)

(collections.somenamedtuple 属性)

_fields_ (ctypes.Structure 属性)

_flush() (wsgiref.handlers.BaseHandler 方法)

_frozen (C 类型)

_FuncPtr (ctypes 中的类)

_get_child_mock() (unittest.mock.Mock 方法)

_getframe() (在 sys 模块中)

_getvalue() (multiprocessing.managers.BaseProxy 方法)

_handle (ctypes.PyDLL 属性)

_inittab (C 类型)

_length_ (ctypes.Array 属性)

_locale

模块

_make() (collections.somenamedtuple 类方法)

_makeResult() (unittest.TextTestRunner 方法)

_name (ctypes.PyDLL 属性)

_objects (ctypes._CData 属性)

_pack_ (ctypes.Structure 属性)

_parse() (gettext.NullTranslations 方法)

_Pointer (ctypes 中的类)

_Py_c_diff (C 函数)

_Py_c_neg (C 函数)

_Py_c_pow (C 函数)

_Py_c_prod (C 函数)

_Py_c_quot (C 函数)

_Py_c_sum (C 函数)

_Py_NoneStruct (C 变量)

_PyBytes_Resize (C 函数)

_PyImport_FindExtension (C 函数)

_PyImport_Fini (C 函数)

_PyImport_Init (C 函数)

_PyObject_GC_TRACK (C 函数)

_PyObject_GC_UNTRACK (C 函数)

_PyObject_New (C 函数)

_PyObject_NewVar (C 函数)

_PyTuple_Resize (C 函数)

_replace() (collections.somenamedtuple 方法)

_setroot() (xml.etree.ElementTree.ElementTree 方法)

_SimpleCData (ctypes 中的类)

_source (collections.somenamedtuple 属性)

_structure() (在 email.iterators 模块中)

_thread

模块

_thread (模块)

_type_ (ctypes._Pointer 属性)

(ctypes.Array 属性)

_write() (wsgiref.handlers.BaseHandler 方法)

_xoptions() (在 sys 模块中)

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引

符号 | _ | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | 例 | 內 | 关 | 对 | 模 | 环 | 语 | 运

符号

! (exclamation)

in a command interpreter

in curses module

in formatted string literal

in glob-style wildcards, [1]

in string formatting

in struct format strings

! (pdb command)

!=

运算符, [1]

" (double quote)

string literal

"""

string literal

# (hash)

comment, [1], [2]

in doctests

in printf-style formatting, [1]

in regular expressions

in string formatting

source encoding declaration

$ (dollar)

environment variables expansion

in regular expressions

in template strings

interpolation in configuration files

% (percent)

datetime format, [1], [2]

environment variables expansion (Windows), [1]

interpolation in configuration files

printf-style formatting, [1]

运算符, [1]

%=

augmented assignment

& (ampersand)

运算符, [1]

&=

augmented assignment

' (single quote)

string literal

'''

string literal

() (parentheses)

call

class definition

function definition

generator expression

in assignment target list

in printf-style formatting, [1]

in regular expressions

tuple display

(?

in regular expressions

(?!

in regular expressions

(?#

in regular expressions

(?:

in regular expressions

(?<!

in regular expressions

(?<=

in regular expressions

(?P<

in regular expressions

(?P=

in regular expressions

* (asterisk)

function definition

import statement

in argparse module

in assignment target list

in AST grammar

in expression lists

in function calls, [1]

in glob-style wildcards, [1]

in printf-style formatting, [1]

in regular expressions

运算符, [1]

**

function definition

in dictionary displays

in function calls, [1]

in glob-style wildcards

运算符, [1]

**=

augmented assignment

*=

augmented assignment

*?

in regular expressions

+ (plus)

binary operator, [1]

in argparse module

in doctests

in printf-style formatting, [1]

in regular expressions

in string formatting

unary operator, [1]

+=

augmented assignment

+?

in regular expressions

, (comma)

argument list

expression list, [1], [2], [3], [4]

identifier list, [1]

import statement

in dictionary displays

in string formatting

in target list

parameter list

slicing

tuple display

with statement

- (minus)

binary operator, [1]

in doctests

in glob-style wildcards, [1]

in printf-style formatting, [1]

in regular expressions

in string formatting

unary operator, [1]

--annotate

pickletools command line option

--buffer

unittest command line option

--catch

unittest command line option

--clock

timeit command line option

--count

trace command line option

--coverdir=<dir>

trace command line option

--create <tarfile> <source1> ... <sourceN>

tarfile command line option

--details

inspect command line option

--exact

tokenize command line option

--extract <tarfile> [<output_dir>]

tarfile command line option

--failfast

unittest command line option

--file=<file>

trace command line option

--help

command line option

json.tool command line option

timeit command line option

tokenize command line option

trace command line option

zipapp command line option

--ignore-dir=<dir>

trace command line option

--ignore-module=<mod>

trace command line option

--indentlevel=<num>

pickletools command line option

--info

zipapp command line option

--list <tarfile>

tarfile command line option

--listfuncs

trace command line option

--locals

unittest command line option

--main=<mainfn>

zipapp command line option

--memo

pickletools command line option

--missing

trace command line option

--no-report

trace command line option

--number=N

timeit command line option

--output=<file>

pickletools command line option

--output=<output>

zipapp command line option

--pattern pattern

unittest-discover command line option

--preamble=<preamble>

pickletools command line option

--process

timeit command line option

--python=<interpreter>

zipapp command line option

--repeat=N

timeit command line option

--report

trace command line option

--setup=S

timeit command line option

--sort-keys

json.tool command line option

--start-directory directory

unittest-discover command line option

--summary

trace command line option

--test <tarfile>

tarfile command line option

--time

timeit command line option

--timing

trace command line option

--top-level-directory directory

unittest-discover command line option

--trace

trace command line option

--trackcalls

trace command line option

--unit=U

timeit command line option

--user-base

site command line option

--user-site

site command line option

--verbose

tarfile command line option

timeit command line option

unittest-discover command line option

--version

command line option

trace command line option

-=

augmented assignment

->

function annotations, [1]

-?

command line option

-a

pickletools command line option

-B

command line option

-b

command line option

compileall command line option

unittest command line option

-C

trace command line option

-c

timeit command line option

trace command line option

unittest command line option

-c <command>

command line option

-c <tarfile> <source1> ... <sourceN>

tarfile command line option

-c <zipfile> <source1> ... <sourceN>

zipfile command line option

-d

command line option

-d destdir

compileall command line option

-E

command line option

-e

tokenize command line option

-e <tarfile> [<output_dir>]

tarfile command line option

-e <zipfile> <output_dir>

zipfile command line option

-f

compileall command line option

trace command line option

unittest command line option

-g

trace command line option

-h

command line option

json.tool command line option

timeit command line option

tokenize command line option

zipapp command line option

-I

command line option

-i

command line option

-i list

compileall command line option

-J

command line option

-j N

compileall command line option

-l

compileall command line option

pickletools command line option

trace command line option

-l <tarfile>

tarfile command line option

-l <zipfile>

zipfile command line option

-m

pickletools command line option

trace command line option

-m <mainfn>

zipapp command line option

-m <module-name>

command line option

-n N

timeit command line option

-O

command line option

-o

pickletools command line option

-o <output>

zipapp command line option

-OO

command line option

-p

pickletools command line option

timeit command line option

unittest-discover command line option

-p <interpreter>

zipapp command line option

-q

command line option

compileall command line option

-R

command line option

trace command line option

-r

compileall command line option

trace command line option

-r N

timeit command line option

-S

command line option

-s

command line option

trace command line option

unittest-discover command line option

-s S

timeit command line option

-T

trace command line option

-t

timeit command line option

trace command line option

unittest-discover command line option

-t <tarfile>

tarfile command line option

-t <zipfile>

zipfile command line option

-u

command line option

timeit command line option

-V

command line option

-v

command line option

tarfile command line option

timeit command line option

unittest-discover command line option

-W arg

command line option

-X

command line option

-x

command line option

-x regex

compileall command line option

. (dot)

attribute reference

in glob-style wildcards

in numeric literal

in pathnames, [1]

in printf-style formatting, [1]

in regular expressions

in string formatting

in Tkinter

..

in pathnames

...

ellipsis literal, [1], [2]

in doctests

interpreter prompt, [1]

placeholder, [1], [2]

.ini

file

.pdbrc

file

.pypirc file

/ (slash)

in pathnames, [1]

运算符, [1]

//

运算符, [1]

//=

augmented assignment

/=

augmented assignment

0b

integer literal

0o

integer literal

0x

integer literal

2to3

: (colon)

annotated variable

compound statement, [1], [2], [3], [4], [5], [6]

function annotations, [1]

in dictionary expressions

in formatted string literal

in SQL statements

in string formatting

lambda expression

path separator (POSIX)

slicing

; (semicolon), [1]

< (less)

in string formatting

in struct format strings

运算符, [1]

<<

运算符, [1]

<<=

augmented assignment

<=

运算符, [1]

<BLANKLINE>

= (equals)

assignment statement

class definition

function definition

in function calls

in string formatting

in struct format strings

==

运算符, [1]

> (greater)

in string formatting

in struct format strings

运算符, [1]

>=

运算符, [1]

>>

运算符, [1]

>>=

augmented assignment

>>>

interpreter prompt, [1]

? (question mark)

in a command interpreter

in argparse module

in AST grammar

in glob-style wildcards, [1]

in regular expressions

in SQL statements

in struct format strings, [1]

replacement character

??

in regular expressions

@ (at)

class definition

function definition

in struct format strings

运算符

[] (square brackets)

in assignment target list

in glob-style wildcards, [1]

in regular expressions

in string formatting

list expression

subscription

\ (backslash)

escape sequence, [1]

in pathnames (Windows)

in regular expressions, [1], [2]

\\

escape sequence

in regular expressions

\A

in regular expressions

\a

escape sequence

in regular expressions

\B

in regular expressions

\b

escape sequence

in regular expressions, [1]

\D

in regular expressions

\d

in regular expressions

\f

escape sequence

in regular expressions

\g

in regular expressions

\N

escape sequence, [1]

in regular expressions

\n

escape sequence

in regular expressions

\r

escape sequence

in regular expressions

\S

in regular expressions

\s

in regular expressions

\t

escape sequence

in regular expressions

\U

escape sequence, [1]

in regular expressions

\u

escape sequence, [1]

in regular expressions

\v

escape sequence

in regular expressions

\W

in regular expressions

\w

in regular expressions

\x

escape sequence, [1]

in regular expressions

\Z

in regular expressions

^ (caret)

in curses module

in regular expressions, [1]

in string formatting

marker, [1]

运算符, [1]

^=

augmented assignment

{} (curly brackets)

dictionary expression

in formatted string literal

in regular expressions

in string formatting

set expression

| (vertical bar)

in regular expressions

运算符, [1]

|=

augmented assignment

~ (tilde)

home directory expansion

运算符, [1]

_

_ (underscore)

gettext

in numeric literal, [1]

in string formatting

_, identifiers

__, identifiers

__abs__() (object 方法)

(在 operator 模块中)

__add__() (object 方法)

(在 operator 模块中)

__aenter__() (object 方法)

__aexit__() (object 方法)

__aiter__() (object 方法)

__all__

(optional module attribute)

(package variable)

__and__() (object 方法)

(在 operator 模块中)

__anext__() (agen 方法)

(object 方法)

__annotations__ (class attribute)

(function attribute)

(module attribute)

__await__() (object 方法)

__bases__ (class attribute)

(class 属性)

__bool__() (object method)

(object 方法)

__bytes__() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(object 方法)

__cached__

__call__() (email.headerregistry.HeaderRegistry 方法)

(object method)

(object 方法)

(weakref.finalize 方法)

__callback__ (weakref.ref 属性)

__cause__ (exception attribute)

(traceback.TracebackException 属性)

__ceil__() (fractions.Fraction 方法)

(object 方法)

__class__ (instance attribute)

(instance 属性)

(method cell)

(module attribute)

(unittest.mock.Mock 属性)

__classcell__ (class namespace entry)

__closure__ (function attribute)

__code__ (function attribute)

(function object attribute)

__complex__() (object 方法)

__concat__() (在 operator 模块中)

__contains__() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(object 方法)

(在 operator 模块中)

__context__ (exception attribute)

(traceback.TracebackException 属性)

__copy__() (copy protocol)

__debug__

(內置变量)

__deepcopy__() (copy protocol)

__defaults__ (function attribute)

__del__() (io.IOBase 方法)

(object 方法)

__delattr__() (object 方法)

__delete__() (object 方法)

__delitem__() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(mailbox.MH 方法)

(object 方法)

(在 operator 模块中)

__dict__ (class attribute)

(function attribute)

(instance attribute)

(module attribute), [1]

(object 属性)

__dir__() (object 方法)

(unittest.mock.Mock 方法)

__displayhook__() (在 sys 模块中)

__divmod__() (object 方法)

__doc__ (class attribute)

(function attribute)

(method attribute)

(module attribute), [1]

(types.ModuleType 属性)

__enter__() (contextmanager 方法)

(object 方法)

(winreg.PyHKEY 方法)

__eq__() (email.charset.Charset 方法)

(email.header.Header 方法)

(instance method)

(memoryview 方法)

(object 方法)

(在 operator 模块中)

__excepthook__() (在 sys 模块中)

__exit__() (contextmanager 方法)

(object 方法)

(winreg.PyHKEY 方法)

__file__

(module attribute), [1], [2]

__float__() (object 方法)

__floor__() (fractions.Fraction 方法)

(object 方法)

__floordiv__() (object 方法)

(在 operator 模块中)

__format__

__format__() (datetime.date 方法)

(datetime.datetime 方法)

(datetime.time 方法)

(object 方法)

__fspath__() (os.PathLike 方法)

__func__ (method attribute)

__future__

future statement

__future__ (模块)

__ge__() (instance method)

(object 方法)

(在 operator 模块中)

__get__() (object 方法)

__getattr__() (object 方法)

__getattribute__() (object 方法)

__getitem__() (email.headerregistry.HeaderRegistry 方法)

(email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(mapping object method)

(object 方法)

(re.match 方法)

(在 operator 模块中)

__getnewargs__() (object 方法)

__getnewargs_ex__() (object 方法)

__getstate__() (copy protocol)

(object 方法)

__globals__ (function attribute)

__gt__() (instance method)

(object 方法)

(在 operator 模块中)

__hash__() (object 方法)

__iadd__() (object 方法)

(在 operator 模块中)

__iand__() (object 方法)

(在 operator 模块中)

__iconcat__() (在 operator 模块中)

__ifloordiv__() (object 方法)

(在 operator 模块中)

__ilshift__() (object 方法)

(在 operator 模块中)

__imatmul__() (object 方法)

(在 operator 模块中)

__imod__() (object 方法)

(在 operator 模块中)

__import__

內置函数

__import__() (內置函数)

(在 importlib 模块中)

__imul__() (object 方法)

(在 operator 模块中)

__index__() (object 方法)

(在 operator 模块中)

__init__() (difflib.HtmlDiff 方法)

(logging.Handler 方法)

(logging.logging.Formatter 方法)

(object 方法)

__init_subclass__() (object 类方法)

__instancecheck__() (class 方法)

__int__() (object 方法)

__interactivehook__() (在 sys 模块中)

__inv__() (在 operator 模块中)

__invert__() (object 方法)

(在 operator 模块中)

__ior__() (object 方法)

(在 operator 模块中)

__ipow__() (object 方法)

(在 operator 模块中)

__irshift__() (object 方法)

(在 operator 模块中)

__isub__() (object 方法)

(在 operator 模块中)

__iter__() (container 方法)

(iterator 方法)

(mailbox.Mailbox 方法)

(object 方法)

(unittest.TestSuite 方法)

__itruediv__() (object 方法)

(在 operator 模块中)

__ixor__() (object 方法)

(在 operator 模块中)

__kwdefaults__ (function attribute)

__le__() (instance method)

(object 方法)

(在 operator 模块中)

__len__() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(mapping object method)

(object 方法)

__length_hint__() (object 方法)

__loader__

(module attribute)

(types.ModuleType 属性)

__lshift__() (object 方法)

(在 operator 模块中)

__lt__() (instance method)

(object 方法)

(在 operator 模块中)

__main__

模块, [1], [2], [3], [4], [5], [6], [7]

__main__ (模块)

__matmul__() (object 方法)

(在 operator 模块中)

__missing__()

(collections.defaultdict 方法)

(object 方法)

__mod__() (object 方法)

(在 operator 模块中)

__module__ (class attribute)

(function attribute)

(method attribute)

__mro__ (class 属性)

__mul__() (object 方法)

(在 operator 模块中)

__name__

(class attribute)

(definition 属性)

(function attribute)

(method attribute)

(module attribute), [1], [2]

(types.ModuleType 属性)

__ne__() (email.charset.Charset 方法)

(email.header.Header 方法)

(instance method)

(object 方法)

(在 operator 模块中)

__neg__() (object 方法)

(在 operator 模块中)

__new__() (object 方法)

__next__() (csv.csvreader 方法)

(generator 方法)

(iterator 方法)

__not__() (在 operator 模块中)

__or__() (object 方法)

(在 operator 模块中)

__package__

(module attribute)

(types.ModuleType 属性)

__path__

__pos__() (object 方法)

(在 operator 模块中)

__pow__() (object 方法)

(在 operator 模块中)

__prepare__ (metaclass method)

__qualname__ (definition 属性)

__radd__() (object 方法)

__rand__() (object 方法)

__rdivmod__() (object 方法)

__reduce__() (object 方法)

__reduce_ex__() (object 方法)

__repr__() (multiprocessing.managers.BaseProxy 方法)

(netrc.netrc 方法)

(object 方法)

__reversed__() (object 方法)

__rfloordiv__() (object 方法)

__rlshift__() (object 方法)

__rmatmul__() (object 方法)

__rmod__() (object 方法)

__rmul__() (object 方法)

__ror__() (object 方法)

__round__() (fractions.Fraction 方法)

(object 方法)

__rpow__() (object 方法)

__rrshift__() (object 方法)

__rshift__() (object 方法)

(在 operator 模块中)

__rsub__() (object 方法)

__rtruediv__() (object 方法)

__rxor__() (object 方法)

__self__ (method attribute)

__set__() (object 方法)

__set_name__() (object 方法)

__setattr__() (object 方法)

__setitem__() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(object 方法)

(在 operator 模块中)

__setstate__() (copy protocol)

(object 方法)

__slots__

__spec__

__stderr__() (在 sys 模块中)

__stdin__() (在 sys 模块中)

__stdout__() (在 sys 模块中)

__str__() (datetime.date 方法)

(datetime.datetime 方法)

(datetime.time 方法)

(email.charset.Charset 方法)

(email.header.Header 方法)

(email.headerregistry.Address 方法)

(email.headerregistry.Group 方法)

(email.message.EmailMessage 方法)

(email.message.Message 方法)

(multiprocessing.managers.BaseProxy 方法)

(object 方法)

__sub__() (object 方法)

(在 operator 模块中)

__subclasscheck__() (class 方法)

__subclasses__() (class 方法)

__subclasshook__() (abc.ABCMeta 方法)

__suppress_context__ (traceback.TracebackException 属性)

__traceback__ (exception attribute)

__truediv__() (object 方法)

(在 operator 模块中)

__trunc__() (object 方法)

__xor__() (object 方法)

(在 operator 模块中)

_anonymous_ (ctypes.Structure 属性)

_asdict() (collections.somenamedtuple 方法)

_b_base_ (ctypes._CData 属性)

_b_needsfree_ (ctypes._CData 属性)

_callmethod() (multiprocessing.managers.BaseProxy 方法)

_CData (ctypes 中的类)

_clear_type_cache() (在 sys 模块中)

_current_frames() (在 sys 模块中)

_debugmallocstats() (在 sys 模块中)

_dummy_thread (模块)

_enablelegacywindowsfsencoding() (在 sys 模块中)

_exit() (在 os 模块中)

_fields (ast.AST 属性)

(collections.somenamedtuple 属性)

_fields_ (ctypes.Structure 属性)

_flush() (wsgiref.handlers.BaseHandler 方法)

_frozen (C 类型)

_FuncPtr (ctypes 中的类)

_get_child_mock() (unittest.mock.Mock 方法)

_getframe() (在 sys 模块中)

_getvalue() (multiprocessing.managers.BaseProxy 方法)

_handle (ctypes.PyDLL 属性)

_inittab (C 类型)

_length_ (ctypes.Array 属性)

_locale

模块

_make() (collections.somenamedtuple 类方法)

_makeResult() (unittest.TextTestRunner 方法)

_name (ctypes.PyDLL 属性)

_objects (ctypes._CData 属性)

_pack_ (ctypes.Structure 属性)

_parse() (gettext.NullTranslations 方法)

_Pointer (ctypes 中的类)

_Py_c_diff (C 函数)

_Py_c_neg (C 函数)

_Py_c_pow (C 函数)

_Py_c_prod (C 函数)

_Py_c_quot (C 函数)

_Py_c_sum (C 函数)

_Py_NoneStruct (C 变量)

_PyBytes_Resize (C 函数)

_PyImport_FindExtension (C 函数)

_PyImport_Fini (C 函数)

_PyImport_Init (C 函数)

_PyObject_GC_TRACK (C 函数)

_PyObject_GC_UNTRACK (C 函数)

_PyObject_New (C 函数)

_PyObject_NewVar (C 函数)

_PyTuple_Resize (C 函数)

_replace() (collections.somenamedtuple 方法)

_setroot() (xml.etree.ElementTree.ElementTree 方法)

_SimpleCData (ctypes 中的类)

_source (collections.somenamedtuple 属性)

_structure() (在 email.iterators 模块中)

_thread

模块

_thread (模块)

_type_ (ctypes._Pointer 属性)

(ctypes.Array 属性)

_write() (wsgiref.handlers.BaseHandler 方法)

_xoptions() (在 sys 模块中)

A

A() (在 re 模块中)

A-LAW, [1]

a-LAW

a2b_base64() (在 binascii 模块中)

a2b_hex() (在 binascii 模块中)

a2b_hqx() (在 binascii 模块中)

a2b_qp() (在 binascii 模块中)

a2b_uu() (在 binascii 模块中)

a85decode() (在 base64 模块中)

a85encode() (在 base64 模块中)

ABC (abc 中的类)

abc (模块)

ABCMeta (abc 中的类)

abiflags() (在 sys 模块中)

abort()

(asyncio.DatagramTransport 方法)

(asyncio.WriteTransport 方法)

(ftplib.FTP 方法)

(threading.Barrier 方法)

(在 os 模块中)

above() (curses.panel.Panel 方法)

abs

內置函数, [1]

abs() (decimal.Context 方法)

(內置函数)

(在 operator 模块中)

abspath() (在 os.path 模块中)

abstract base class -- 抽象基类

AbstractBasicAuthHandler (urllib.request 中的类)

abstractclassmethod() (在 abc 模块中)

AbstractContextManager (contextlib 中的类)

AbstractDigestAuthHandler (urllib.request 中的类)

AbstractEventLoop (asyncio 中的类)

AbstractEventLoopPolicy (asyncio 中的类)

AbstractFormatter (formatter 中的类)

abstractmethod() (在 abc 模块中)

abstractproperty() (在 abc 模块中)

AbstractSet (typing 中的类)

abstractstaticmethod() (在 abc 模块中)

AbstractWriter (formatter 中的类)

accept() (asyncore.dispatcher 方法)

(multiprocessing.connection.Listener 方法)

(socket.socket 方法)

access() (在 os 模块中)

accumulate() (在 itertools 模块中)

aclose() (agen 方法)

acos() (在 cmath 模块中)

(在 math 模块中)

acosh() (在 cmath 模块中)

(在 math 模块中)

acquire() (_thread.lock 方法)

(asyncio.Condition 方法)

(asyncio.Lock 方法)

(asyncio.Semaphore 方法)

(logging.Handler 方法)

(multiprocessing.Lock 方法)

(multiprocessing.RLock 方法)

(threading.Condition 方法)

(threading.Lock 方法)

(threading.RLock 方法)

(threading.Semaphore 方法)

acquire_lock() (在 imp 模块中)

Action (argparse 中的类)

action (optparse.Option 属性)

ACTIONS (optparse.Option 属性)

active_children() (在 multiprocessing 模块中)

active_count() (在 threading 模块中)

add() (decimal.Context 方法)

(frozenset 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(msilib.RadioButtonGroup 方法)

(pstats.Stats 方法)

(tarfile.TarFile 方法)

(tkinter.ttk.Notebook 方法)

(在 audioop 模块中)

(在 operator 模块中)

add_alias() (在 email.charset 模块中)

add_alternative() (email.message.EmailMessage 方法)

add_argument() (argparse.ArgumentParser 方法)

add_argument_group() (argparse.ArgumentParser 方法)

add_attachment() (email.message.EmailMessage 方法)

add_cgi_vars() (wsgiref.handlers.BaseHandler 方法)

add_charset() (在 email.charset 模块中)

add_codec() (在 email.charset 模块中)

add_cookie_header() (http.cookiejar.CookieJar 方法)

add_data() (在 msilib 模块中)

add_done_callback() (asyncio.Future 方法)

(concurrent.futures.Future 方法)

add_fallback() (gettext.NullTranslations 方法)

add_file() (msilib.Directory 方法)

add_flag() (mailbox.MaildirMessage 方法)

(mailbox.mboxMessage 方法)

(mailbox.MMDFMessage 方法)

add_flowing_data() (formatter.formatter 方法)

add_folder() (mailbox.Maildir 方法)

(mailbox.MH 方法)

add_get_handler() (email.contentmanager.ContentManager 方法)

add_handler() (urllib.request.OpenerDirector 方法)

add_header() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(urllib.request.Request 方法)

(wsgiref.headers.Headers 方法)

add_history() (在 readline 模块中)

add_hor_rule() (formatter.formatter 方法)

add_include_dir() (distutils.ccompiler.CCompiler 方法)

add_label() (mailbox.BabylMessage 方法)

add_label_data() (formatter.formatter 方法)

add_library() (distutils.ccompiler.CCompiler 方法)

add_library_dir() (distutils.ccompiler.CCompiler 方法)

add_line_break() (formatter.formatter 方法)

add_link_object() (distutils.ccompiler.CCompiler 方法)

add_literal_data() (formatter.formatter 方法)

add_mutually_exclusive_group() (argparse.ArgumentParser 方法)

add_option() (optparse.OptionParser 方法)

add_parent() (urllib.request.BaseHandler 方法)

add_password() (urllib.request.HTTPPasswordMgr 方法)

(urllib.request.HTTPPasswordMgrWithPriorAuth 方法)

add_reader() (asyncio.AbstractEventLoop 方法)

add_related() (email.message.EmailMessage 方法)

add_runtime_library_dir() (distutils.ccompiler.CCompiler 方法)

add_section() (configparser.ConfigParser 方法)

(configparser.RawConfigParser 方法)

add_sequence() (mailbox.MHMessage 方法)

add_set_handler() (email.contentmanager.ContentManager 方法)

add_signal_handler() (asyncio.AbstractEventLoop 方法)

add_stream() (在 msilib 模块中)

add_subparsers() (argparse.ArgumentParser 方法)

add_tables() (在 msilib 模块中)

add_type() (在 mimetypes 模块中)

add_unredirected_header() (urllib.request.Request 方法)

add_writer() (asyncio.AbstractEventLoop 方法)

addch() (curses.window 方法)

addCleanup() (unittest.TestCase 方法)

addcomponent() (turtle.Shape 方法)

addError() (unittest.TestResult 方法)

addExpectedFailure() (unittest.TestResult 方法)

addFailure() (unittest.TestResult 方法)

addfile() (tarfile.TarFile 方法)

addFilter() (logging.Handler 方法)

(logging.Logger 方法)

addHandler() (logging.Logger 方法)

addition

addLevelName() (在 logging 模块中)

addnstr() (curses.window 方法)

AddPackagePath() (在 modulefinder 模块中)

addr (smtpd.SMTPChannel 属性)

addr_spec (email.headerregistry.Address 属性)

Address (email.headerregistry 中的类)

address (email.headerregistry.SingleAddressHeader 属性)

(multiprocessing.connection.Listener 属性)

(multiprocessing.managers.BaseManager 属性)

address_exclude() (ipaddress.IPv4Network 方法)

(ipaddress.IPv6Network 方法)

address_family (socketserver.BaseServer 属性)

address_string() (http.server.BaseHTTPRequestHandler 方法)

addresses (email.headerregistry.AddressHeader 属性)

(email.headerregistry.Group 属性)

AddressHeader (email.headerregistry 中的类)

addressof() (在 ctypes 模块中)

AddressValueError

addshape() (在 turtle 模块中)

addsitedir() (在 site 模块中)

addSkip() (unittest.TestResult 方法)

addstr() (curses.window 方法)

addSubTest() (unittest.TestResult 方法)

addSuccess() (unittest.TestResult 方法)

addTest() (unittest.TestSuite 方法)

addTests() (unittest.TestSuite 方法)

addTypeEqualityFunc() (unittest.TestCase 方法)

addUnexpectedSuccess() (unittest.TestResult 方法)

adjusted() (decimal.Decimal 方法)

adler32() (在 zlib 模块中)

ADPCM, Intel/DVI

adpcm2lin() (在 audioop 模块中)

AF_ALG() (在 socket 模块中)

AF_CAN() (在 socket 模块中)

AF_INET() (在 socket 模块中)

AF_INET6() (在 socket 模块中)

AF_LINK() (在 socket 模块中)

AF_PACKET() (在 socket 模块中)

AF_RDS() (在 socket 模块中)

AF_UNIX() (在 socket 模块中)

aifc (模块)

aifc() (aifc.aifc 方法)

AIFF, [1]

aiff() (aifc.aifc 方法)

AIFF-C, [1]

alarm() (在 signal 模块中)

alaw2lin() (在 audioop 模块中)

ALERT_DESCRIPTION_HANDSHAKE_FAILURE() (在 ssl 模块中)

ALERT_DESCRIPTION_INTERNAL_ERROR() (在 ssl 模块中)

AlertDescription (ssl 中的类)

algorithms_available() (在 hashlib 模块中)

algorithms_guaranteed() (在 hashlib 模块中)

alias (pdb command)

alignment() (在 ctypes 模块中)

alive (weakref.finalize 属性)

all() (內置函数)

all_errors() (在 ftplib 模块中)

all_features() (在 xml.sax.handler 模块中)

all_frames (tracemalloc.Filter 属性)

all_properties() (在 xml.sax.handler 模块中)

all_suffixes() (在 importlib.machinery 模块中)

all_tasks() (asyncio.Task 类方法)

allocate_lock() (在 _thread 模块中)

allow_reuse_address (socketserver.BaseServer 属性)

allowed_domains() (http.cookiejar.DefaultCookiePolicy 方法)

alt() (在 curses.ascii 模块中)

ALT_DIGITS() (在 locale 模块中)

altsep() (在 os 模块中)

altzone() (在 time 模块中)

ALWAYS_TYPED_ACTIONS (optparse.Option 属性)

AMPER() (在 token 模块中)

AMPEREQUAL() (在 token 模块中)

and

bitwise

运算符, [1], [2]

and_() (在 operator 模块中)

annotated

assignment

annotation (inspect.Parameter 属性)

annotation -- 标注

annotations

function, [1]

announce() (distutils.ccompiler.CCompiler 方法)

anonymous

function

answer_challenge() (在 multiprocessing.connection 模块中)

anticipate_failure() (在 test.support 模块中)

any() (內置函数)

Any() (在 typing 模块中)

ANY() (在 unittest.mock 模块中)

AnyStr() (在 typing 模块中)

api_version() (在 sys 模块中)

apop() (poplib.POP3 方法)

APPDATA

append() (array.array 方法)

(collections.deque 方法)

(email.header.Header 方法)

(imaplib.IMAP4 方法)

(msilib.CAB 方法)

(pipes.Template 方法)

(sequence method)

(xml.etree.ElementTree.Element 方法)

append_history_file() (在 readline 模块中)

appendChild() (xml.dom.Node 方法)

appendleft() (collections.deque 方法)

application_uri() (在 wsgiref.util 模块中)

apply (2to3 fixer)

apply() (multiprocessing.pool.Pool 方法)

apply_async() (multiprocessing.pool.Pool 方法)

apply_defaults() (inspect.BoundArguments 方法)

architecture() (在 platform 模块中)

archive (zipimport.zipimporter 属性)

aRepr() (在 reprlib 模块中)

argparse (模块)

args (BaseException 属性)

(functools.partial 属性)

(inspect.BoundArguments 属性)

(pdb command)

(subprocess.CompletedProcess 属性)

(subprocess.Popen 属性)

argtypes (ctypes._FuncPtr 属性)

argument

call semantics

difference from parameter

function

function definition

argument -- 参数

ArgumentDefaultsHelpFormatter (argparse 中的类)

ArgumentError

ArgumentParser (argparse 中的类)

arguments (inspect.BoundArguments 属性)

argv (in module sys)

argv() (在 sys 模块中)

arithmetic

conversion

operation, binary

operation, unary

ArithmeticError

array

模块, [1]

array (array 中的类)

Array (ctypes 中的类)

array (模块)

Array() (multiprocessing.managers.SyncManager 方法)

(在 multiprocessing 模块中)

(在 multiprocessing.sharedctypes 模块中)

arrays

arraysize (sqlite3.Cursor 属性)

article() (nntplib.NNTP 方法)

as

except clause

import statement

with statement

关键字, [1], [2]

as_bytes() (email.message.EmailMessage 方法)

(email.message.Message 方法)

as_completed() (在 asyncio 模块中)

(在 concurrent.futures 模块中)

as_integer_ratio() (decimal.Decimal 方法)

(float 方法)

AS_IS() (在 formatter 模块中)

as_posix() (pathlib.PurePath 方法)

as_string() (email.message.EmailMessage 方法)

(email.message.Message 方法)

as_tuple() (decimal.Decimal 方法)

as_uri() (pathlib.PurePath 方法)

ASCII, [1]

ascii

內置函数

ascii() (內置函数)

(在 curses.ascii 模块中)

ASCII() (在 re 模块中)

ascii_letters() (在 string 模块中)

ascii_lowercase() (在 string 模块中)

ascii_uppercase() (在 string 模块中)

asctime() (在 time 模块中)

asend() (agen 方法)

asin() (在 cmath 模块中)

(在 math 模块中)

asinh() (在 cmath 模块中)

(在 math 模块中)

assert

语句, [1]

assert_any_call() (unittest.mock.Mock 方法)

assert_called() (unittest.mock.Mock 方法)

assert_called_once() (unittest.mock.Mock 方法)

assert_called_once_with() (unittest.mock.Mock 方法)

assert_called_with() (unittest.mock.Mock 方法)

assert_has_calls() (unittest.mock.Mock 方法)

assert_line_data() (formatter.formatter 方法)

assert_not_called() (unittest.mock.Mock 方法)

assertAlmostEqual() (unittest.TestCase 方法)

assertCountEqual() (unittest.TestCase 方法)

assertDictEqual() (unittest.TestCase 方法)

assertEqual() (unittest.TestCase 方法)

assertFalse() (unittest.TestCase 方法)

assertGreater() (unittest.TestCase 方法)

assertGreaterEqual() (unittest.TestCase 方法)

assertIn() (unittest.TestCase 方法)

AssertionError

例外

assertions

debugging

assertIs() (unittest.TestCase 方法)

assertIsInstance() (unittest.TestCase 方法)

assertIsNone() (unittest.TestCase 方法)

assertIsNot() (unittest.TestCase 方法)

assertIsNotNone() (unittest.TestCase 方法)

assertLess() (unittest.TestCase 方法)

assertLessEqual() (unittest.TestCase 方法)

assertListEqual() (unittest.TestCase 方法)

assertLogs() (unittest.TestCase 方法)

assertMultiLineEqual() (unittest.TestCase 方法)

assertNotAlmostEqual() (unittest.TestCase 方法)

assertNotEqual() (unittest.TestCase 方法)

assertNotIn() (unittest.TestCase 方法)

assertNotIsInstance() (unittest.TestCase 方法)

assertNotRegex() (unittest.TestCase 方法)

assertRaises() (unittest.TestCase 方法)

assertRaisesRegex() (unittest.TestCase 方法)

assertRegex() (unittest.TestCase 方法)

asserts (2to3 fixer)

assertSequenceEqual() (unittest.TestCase 方法)

assertSetEqual() (unittest.TestCase 方法)

assertTrue() (unittest.TestCase 方法)

assertTupleEqual() (unittest.TestCase 方法)

assertWarns() (unittest.TestCase 方法)

assertWarnsRegex() (unittest.TestCase 方法)

assignment

annotated

attribute, [1]

augmented

class attribute

class instance attribute

slice

slicing

statement, [1]

subscript

subscription

target list

AST (ast 中的类)

ast (模块)

astimezone() (datetime.datetime 方法)

async

关键字

async def

语句

async for

in comprehensions, [1]

语句

async with

语句

async() (在 asyncio 模块中)

ASYNC() (在 token 模块中)

async_chat (asynchat 中的类)

async_chat.ac_in_buffer_size() (在 asynchat 模块中)

async_chat.ac_out_buffer_size() (在 asynchat 模块中)

AsyncContextManager (typing 中的类)

AsyncGenerator (collections.abc 中的类)

(typing 中的类)

AsyncGeneratorType() (在 types 模块中)

asynchat (模块)

asynchronous context manager -- 异步上下文管理器

asynchronous generator

asynchronous iterator

function

asynchronous generator -- 异步生成器

asynchronous generator iterator -- 异步生成器迭代器

asynchronous iterable -- 异步可迭代对象

asynchronous iterator -- 异步迭代器

asynchronous-generator

对象

asyncio (模块)

asyncio.subprocess.DEVNULL() (在 asyncio 模块中)

asyncio.subprocess.PIPE() (在 asyncio 模块中)

asyncio.subprocess.Process (asyncio 中的类)

asyncio.subprocess.STDOUT() (在 asyncio 模块中)

AsyncIterable (collections.abc 中的类)

(typing 中的类)

AsyncIterator (collections.abc 中的类)

(typing 中的类)

asyncore (模块)

AsyncResult (multiprocessing.pool 中的类)

AT() (在 token 模块中)

at_eof() (asyncio.StreamReader 方法)

atan() (在 cmath 模块中)

(在 math 模块中)

atan2() (在 math 模块中)

atanh() (在 cmath 模块中)

(在 math 模块中)

ATEQUAL() (在 token 模块中)

atexit (weakref.finalize 属性)

(模块)

athrow() (agen 方法)

atof() (在 locale 模块中)

atoi() (在 locale 模块中)

atom

attach() (email.message.Message 方法)

attach_mock() (unittest.mock.Mock 方法)

AttlistDeclHandler() (xml.parsers.expat.xmlparser 方法)

attrgetter() (在 operator 模块中)

attrib (xml.etree.ElementTree.Element 属性)

attribute

assignment, [1]

assignment, class

assignment, class instance

class

class instance

deletion

generic special

reference

special

attribute -- 属性

AttributeError

例外

attributes (xml.dom.Node 属性)

AttributesImpl (xml.sax.xmlreader 中的类)

AttributesNSImpl (xml.sax.xmlreader 中的类)

attroff() (curses.window 方法)

attron() (curses.window 方法)

attrset() (curses.window 方法)

Audio Interchange File Format, [1]

AUDIO_FILE_ENCODING_ADPCM_G721() (在 sunau 模块中)

AUDIO_FILE_ENCODING_ADPCM_G722() (在 sunau 模块中)

AUDIO_FILE_ENCODING_ADPCM_G723_3() (在 sunau 模块中)

AUDIO_FILE_ENCODING_ADPCM_G723_5() (在 sunau 模块中)

AUDIO_FILE_ENCODING_ALAW_8() (在 sunau 模块中)

AUDIO_FILE_ENCODING_DOUBLE() (在 sunau 模块中)

AUDIO_FILE_ENCODING_FLOAT() (在 sunau 模块中)

AUDIO_FILE_ENCODING_LINEAR_16() (在 sunau 模块中)

AUDIO_FILE_ENCODING_LINEAR_24() (在 sunau 模块中)

AUDIO_FILE_ENCODING_LINEAR_32() (在 sunau 模块中)

AUDIO_FILE_ENCODING_LINEAR_8() (在 sunau 模块中)

AUDIO_FILE_ENCODING_MULAW_8() (在 sunau 模块中)

AUDIO_FILE_MAGIC() (在 sunau 模块中)

AUDIODEV

audioop (模块)

augmented

assignment

auth() (ftplib.FTP_TLS 方法)

(smtplib.SMTP 方法)

authenticate() (imaplib.IMAP4 方法)

AuthenticationError

authenticators() (netrc.netrc 方法)

authkey (multiprocessing.Process 属性)

auto (enum 中的类)

autorange() (timeit.Timer 方法)

avg() (在 audioop 模块中)

avgpp() (在 audioop 模块中)

avoids_symlink_attacks (shutil.rmtree 属性)

await

in comprehensions

关键字, [1], [2]

AWAIT() (在 token 模块中)

Awaitable (collections.abc 中的类)

(typing 中的类)

awaitable -- 可等待对象

B

b"

bytes literal

b'

bytes literal

b16decode() (在 base64 模块中)

b16encode() (在 base64 模块中)

b2a_base64() (在 binascii 模块中)

b2a_hex() (在 binascii 模块中)

b2a_hqx() (在 binascii 模块中)

b2a_qp() (在 binascii 模块中)

b2a_uu() (在 binascii 模块中)

b32decode() (在 base64 模块中)

b32encode() (在 base64 模块中)

b64decode() (在 base64 模块中)

b64encode() (在 base64 模块中)

b85decode() (在 base64 模块中)

b85encode() (在 base64 模块中)

Babyl (mailbox 中的类)

BabylMessage (mailbox 中的类)

back() (在 turtle 模块中)

backslash character

backslashreplace_errors() (在 codecs 模块中)

backward() (在 turtle 模块中)

BadStatusLine

BadZipFile

BadZipfile

Balloon (tkinter.tix 中的类)

Barrier (multiprocessing 中的类)

(threading 中的类)

Barrier() (multiprocessing.managers.SyncManager 方法)

base64

encoding

模块

base64 (模块)

base_exec_prefix() (在 sys 模块中)

base_prefix() (在 sys 模块中)

BaseCGIHandler (wsgiref.handlers 中的类)

BaseCookie (http.cookies 中的类)

BaseEventLoop (asyncio 中的类)

BaseException

BaseHandler (urllib.request 中的类)

(wsgiref.handlers 中的类)

BaseHeader (email.headerregistry 中的类)

BaseHTTPRequestHandler (http.server 中的类)

BaseManager (multiprocessing.managers 中的类)

basename() (在 os.path 模块中)

BaseProxy (multiprocessing.managers 中的类)

BaseRequestHandler (socketserver 中的类)

BaseRotatingHandler (logging.handlers 中的类)

BaseSelector (selectors 中的类)

BaseServer (socketserver 中的类)

basestring (2to3 fixer)

BaseSubprocessTransport (asyncio 中的类)

BaseTransport (asyncio 中的类)

basicConfig() (在 logging 模块中)

BasicContext (decimal 中的类)

BasicInterpolation (configparser 中的类)

baudrate() (在 curses 模块中)

bbox() (tkinter.ttk.Treeview 方法)

BDADDR_ANY() (在 socket 模块中)

BDADDR_LOCAL() (在 socket 模块中)

bdb

模块

Bdb (bdb 中的类)

bdb (模块)

BdbQuit

BDFL

bdist_msi (distutils.command.bdist_msi 中的类)

beep() (在 curses 模块中)

Beep() (在 winsound 模块中)

BEFORE_ASYNC_WITH (opcode)

begin_fill() (在 turtle 模块中)

begin_poly() (在 turtle 模块中)

below() (curses.panel.Panel 方法)

Benchmarking

benchmarking

betavariate() (在 random 模块中)

bgcolor() (在 turtle 模块中)

bgpic() (在 turtle 模块中)

bias() (在 audioop 模块中)

bidirectional() (在 unicodedata 模块中)

BigEndianStructure (ctypes 中的类)

bin() (內置函数)

binary

arithmetic operation

bitwise operation

data, packing

literals

Binary (msilib 中的类)

(xmlrpc.client 中的类)

binary file -- 二进制文件

binary literal

binary mode

binary semaphores

BINARY_ADD (opcode)

BINARY_AND (opcode)

BINARY_FLOOR_DIVIDE (opcode)

BINARY_LSHIFT (opcode)

BINARY_MATRIX_MULTIPLY (opcode)

BINARY_MODULO (opcode)

BINARY_MULTIPLY (opcode)

BINARY_OR (opcode)

BINARY_POWER (opcode)

BINARY_RSHIFT (opcode)

BINARY_SUBSCR (opcode)

BINARY_SUBTRACT (opcode)

BINARY_TRUE_DIVIDE (opcode)

BINARY_XOR (opcode)

BinaryIO (typing 中的类)

binascii (模块)

bind (widgets)

bind() (asyncore.dispatcher 方法)

(inspect.Signature 方法)

(socket.socket 方法)

bind_partial() (inspect.Signature 方法)

bind_port() (在 test.support 模块中)

bind_textdomain_codeset() (在 gettext 模块中)

binding

global name

name, [1], [2], [3], [4], [5], [6]

bindtextdomain() (在 gettext 模块中)

(在 locale 模块中)

binhex

模块

binhex (模块)

binhex() (在 binhex 模块中)

bisect (模块)

bisect() (在 bisect 模块中)

bisect_left() (在 bisect 模块中)

bisect_right() (在 bisect 模块中)

bit_length() (int 方法)

bitmap() (msilib.Dialog 方法)

bitwise

and

operation, binary

operation, unary

operations

or

xor

bk() (在 turtle 模块中)

bkgd() (curses.window 方法)

bkgdset() (curses.window 方法)

blake2b() (在 hashlib 模块中)

blake2b, blake2s

blake2b.MAX_DIGEST_SIZE() (在 hashlib 模块中)

blake2b.MAX_KEY_SIZE() (在 hashlib 模块中)

blake2b.PERSON_SIZE() (在 hashlib 模块中)

blake2b.SALT_SIZE() (在 hashlib 模块中)

blake2s() (在 hashlib 模块中)

blake2s.MAX_DIGEST_SIZE() (在 hashlib 模块中)

blake2s.MAX_KEY_SIZE() (在 hashlib 模块中)

blake2s.PERSON_SIZE() (在 hashlib 模块中)

blake2s.SALT_SIZE() (在 hashlib 模块中)

blank line

block

code

block_size (hmac.HMAC 属性)

blocked_domains() (http.cookiejar.DefaultCookiePolicy 方法)

BlockingIOError, [1]

BNF, [1]

body() (nntplib.NNTP 方法)

body_encode() (email.charset.Charset 方法)

body_encoding (email.charset.Charset 属性)

body_line_iterator() (在 email.iterators 模块中)

BOM() (在 codecs 模块中)

BOM_BE() (在 codecs 模块中)

BOM_LE() (在 codecs 模块中)

BOM_UTF16() (在 codecs 模块中)

BOM_UTF16_BE() (在 codecs 模块中)

BOM_UTF16_LE() (在 codecs 模块中)

BOM_UTF32() (在 codecs 模块中)

BOM_UTF32_BE() (在 codecs 模块中)

BOM_UTF32_LE() (在 codecs 模块中)

BOM_UTF8() (在 codecs 模块中)

bool (內置类)

Boolean

operation

operations, [1]

type

values

对象, [1]

BOOLEAN_STATES (configparser.ConfigParser 属性)

bootstrap() (在 ensurepip 模块中)

border() (curses.window 方法)

bottom() (curses.panel.Panel 方法)

bottom_panel() (在 curses.panel 模块中)

BoundArguments (inspect 中的类)

BoundaryError

BoundedSemaphore (asyncio 中的类)

(multiprocessing 中的类)

(threading 中的类)

BoundedSemaphore() (multiprocessing.managers.SyncManager 方法)

box() (curses.window 方法)

bpformat() (bdb.Breakpoint 方法)

bpprint() (bdb.Breakpoint 方法)

break

语句, [1], [2], [3], [4]

break (pdb command)

break_anywhere() (bdb.Bdb 方法)

break_here() (bdb.Bdb 方法)

break_long_words (textwrap.TextWrapper 属性)

BREAK_LOOP (opcode)

break_on_hyphens (textwrap.TextWrapper 属性)

Breakpoint (bdb 中的类)

breakpoints

broadcast_address (ipaddress.IPv4Network 属性)

(ipaddress.IPv6Network 属性)

broken (threading.Barrier 属性)

BrokenBarrierError

BrokenPipeError

BrokenProcessPool

BROWSER, [1]

BsdDbShelf (shelve 中的类)

buffer (2to3 fixer)

(io.TextIOBase 属性)

(unittest.TestResult 属性)

buffer interface

(see buffer protocol)

buffer object

(see buffer protocol)

buffer protocol

binary sequence types

str (built-in class)

buffer size, I/O

buffer_info() (array.array 方法)

buffer_size (xml.parsers.expat.xmlparser 属性)

buffer_text (xml.parsers.expat.xmlparser 属性)

buffer_used (xml.parsers.expat.xmlparser 属性)

BufferedIOBase (io 中的类)

BufferedRandom (io 中的类)

BufferedReader (io 中的类)

BufferedRWPair (io 中的类)

BufferedWriter (io 中的类)

BufferError

BufferingHandler (logging.handlers 中的类)

BufferTooShort

bufsize() (ossaudiodev.oss_audio_device 方法)

BUILD_CONST_KEY_MAP (opcode)

BUILD_LIST (opcode)

BUILD_LIST_UNPACK (opcode)

BUILD_MAP (opcode)

BUILD_MAP_UNPACK (opcode)

BUILD_MAP_UNPACK_WITH_CALL (opcode)

build_opener() (在 urllib.request 模块中)

build_py (distutils.command.build_py 中的类)

build_py_2to3 (distutils.command.build_py 中的类)

BUILD_SET (opcode)

BUILD_SET_UNPACK (opcode)

BUILD_SLICE (opcode)

BUILD_STRING (opcode)

BUILD_TUPLE (opcode)

BUILD_TUPLE_UNPACK (opcode)

BUILD_TUPLE_UNPACK_WITH_CALL (opcode)

built-in

method

types

built-in function

call

对象, [1]

built-in method

call

对象, [1]

builtin_module_names() (在 sys 模块中)

BuiltinFunctionType() (在 types 模块中)

BuiltinImporter (importlib.machinery 中的类)

BuiltinMethodType() (在 types 模块中)

builtins

模块, [1], [2], [3], [4]

builtins (模块)

ButtonBox (tkinter.tix 中的类)

bye() (在 turtle 模块中)

byref() (在 ctypes 模块中)

byte

byte-code

file, [1]

byte_compile() (在 distutils.util 模块中)

bytearray

formatting

interpolation

methods

对象, [1], [2], [3]

bytearray (內置类)

bytecode

Bytecode (dis 中的类)

bytecode -- 字节码

Bytecode.codeobj() (在 dis 模块中)

Bytecode.first_line() (在 dis 模块中)

BYTECODE_SUFFIXES() (在 importlib.machinery 模块中)

byteorder() (在 sys 模块中)

bytes

formatting

interpolation

methods

str (built-in class)

內置函数, [1]

对象, [1], [2]

bytes (uuid.UUID 属性)

(內置类)

bytes literal

bytes-like object -- 字节类对象

bytes_le (uuid.UUID 属性)

BytesFeedParser (email.parser 中的类)

BytesGenerator (email.generator 中的类)

BytesHeaderParser (email.parser 中的类)

BytesIO (io 中的类)

BytesParser (email.parser 中的类)

ByteString (collections.abc 中的类)

(typing 中的类)

byteswap() (array.array 方法)

(在 audioop 模块中)

BytesWarning

bz2 (模块)

BZ2Compressor (bz2 中的类)

BZ2Decompressor (bz2 中的类)

BZ2File (bz2 中的类)

C

C

language, [1], [2], [3], [4], [5]

structures

C-contiguous, [1]

c_bool (ctypes 中的类)

C_BUILTIN() (在 imp 模块中)

c_byte (ctypes 中的类)

c_char (ctypes 中的类)

c_char_p (ctypes 中的类)

c_contiguous (memoryview 属性)

c_double (ctypes 中的类)

C_EXTENSION() (在 imp 模块中)

c_float (ctypes 中的类)

c_int (ctypes 中的类)

c_int16 (ctypes 中的类)

c_int32 (ctypes 中的类)

c_int64 (ctypes 中的类)

c_int8 (ctypes 中的类)

c_long (ctypes 中的类)

c_longdouble (ctypes 中的类)

c_longlong (ctypes 中的类)

c_short (ctypes 中的类)

c_size_t (ctypes 中的类)

c_ssize_t (ctypes 中的类)

c_ubyte (ctypes 中的类)

c_uint (ctypes 中的类)

c_uint16 (ctypes 中的类)

c_uint32 (ctypes 中的类)

c_uint64 (ctypes 中的类)

c_uint8 (ctypes 中的类)

c_ulong (ctypes 中的类)

c_ulonglong (ctypes 中的类)

c_ushort (ctypes 中的类)

c_void_p (ctypes 中的类)

c_wchar (ctypes 中的类)

c_wchar_p (ctypes 中的类)

CAB (msilib 中的类)

cache_from_source() (在 imp 模块中)

(在 importlib.util 模块中)

cached (importlib.machinery.ModuleSpec 属性)

CacheFTPHandler (urllib.request 中的类)

calcsize() (在 struct 模块中)

Calendar (calendar 中的类)

calendar (模块)

calendar() (在 calendar 模块中)

call

built-in function

built-in method

class instance

class object, [1], [2]

function, [1], [2]

instance, [1]

method

procedure

user-defined function

call() (在 subprocess 模块中)

(在 unittest.mock 模块中)

call_args (unittest.mock.Mock 属性)

call_args_list (unittest.mock.Mock 属性)

call_at() (asyncio.AbstractEventLoop 方法)

call_count (unittest.mock.Mock 属性)

call_exception_handler() (asyncio.AbstractEventLoop 方法)

CALL_FUNCTION (opcode)

CALL_FUNCTION_EX (opcode)

CALL_FUNCTION_KW (opcode)

call_later() (asyncio.AbstractEventLoop 方法)

call_list() (unittest.mock.call 方法)

call_soon() (asyncio.AbstractEventLoop 方法)

call_soon_threadsafe() (asyncio.AbstractEventLoop 方法)

call_tracing() (在 sys 模块中)

callable

对象, [1]

Callable (collections.abc 中的类)

callable() (內置函数)

Callable() (在 typing 模块中)

CallableProxyType() (在 weakref 模块中)

callback (optparse.Option 属性)

callback() (contextlib.ExitStack 方法)

callback_args (optparse.Option 属性)

callback_kwargs (optparse.Option 属性)

callbacks() (在 gc 模块中)

called (unittest.mock.Mock 属性)

CalledProcessError

calloc()

CAN_BCM() (在 socket 模块中)

can_change_color() (在 curses 模块中)

can_fetch() (urllib.robotparser.RobotFileParser 方法)

CAN_RAW_FD_FRAMES() (在 socket 模块中)

can_symlink() (在 test.support 模块中)

can_write_eof() (asyncio.StreamWriter 方法)

(asyncio.WriteTransport 方法)

cancel() (asyncio.Future 方法)

(asyncio.Handle 方法)

(asyncio.Task 方法)

(concurrent.futures.Future 方法)

(sched.scheduler 方法)

(threading.Timer 方法)

cancel_dump_traceback_later() (在 faulthandler 模块中)

cancel_join_thread() (multiprocessing.Queue 方法)

cancelled() (asyncio.Future 方法)

(concurrent.futures.Future 方法)

CancelledError

CannotSendHeader

CannotSendRequest

canonic() (bdb.Bdb 方法)

canonical() (decimal.Context 方法)

(decimal.Decimal 方法)

capa() (poplib.POP3 方法)

capitalize() (bytearray 方法)

(bytes 方法)

(str 方法)

Capsule

对象

captured_stderr() (在 test.support 模块中)

captured_stdin() (在 test.support 模块中)

captured_stdout() (在 test.support 模块中)

captureWarnings() (在 logging 模块中)

capwords() (在 string 模块中)

casefold() (str 方法)

cast() (memoryview 方法)

(在 ctypes 模块中)

(在 typing 模块中)

cat() (在 nis 模块中)

catch_warnings (warnings 中的类)

category() (在 unicodedata 模块中)

cbreak() (在 curses 模块中)

CC

ccc() (ftplib.FTP_TLS 方法)

CCompiler (distutils.ccompiler 中的类)

CDLL (ctypes 中的类)

ceil() (in module math)

(在 math 模块中)

center() (bytearray 方法)

(bytes 方法)

(str 方法)

CERT_NONE() (在 ssl 模块中)

CERT_OPTIONAL() (在 ssl 模块中)

CERT_REQUIRED() (在 ssl 模块中)

cert_store_stats() (ssl.SSLContext 方法)

cert_time_to_seconds() (在 ssl 模块中)

CertificateError

certificates

CFLAGS, [1], [2]

CFUNCTYPE() (在 ctypes 模块中)

CGI

debugging

exceptions

protocol

security

tracebacks

cgi (模块)

cgi_directories (http.server.CGIHTTPRequestHandler 属性)

CGIHandler (wsgiref.handlers 中的类)

CGIHTTPRequestHandler (http.server 中的类)

cgitb (模块)

CGIXMLRPCRequestHandler (xmlrpc.server 中的类)

chain() (在 itertools 模块中)

chaining

comparisons, [1]

exception

ChainMap (collections 中的类)

(typing 中的类)

change_cwd() (在 test.support 模块中)

change_root() (在 distutils.util 模块中)

CHANNEL_BINDING_TYPES() (在 ssl 模块中)

channel_class (smtpd.SMTPServer 属性)

channels() (ossaudiodev.oss_audio_device 方法)

CHAR_MAX() (在 locale 模块中)

character, [1], [2]

CharacterDataHandler() (xml.parsers.expat.xmlparser 方法)

characters() (xml.sax.handler.ContentHandler 方法)

characters_written (BlockingIOError 属性)

Charset (email.charset 中的类)

charset() (gettext.NullTranslations 方法)

chdir() (在 os 模块中)

check (lzma.LZMADecompressor 属性)

check() (imaplib.IMAP4 方法)

(在 tabnanny 模块中)

check__all__() (在 test.support 模块中)

check_call() (在 subprocess 模块中)

check_environ() (在 distutils.util 模块中)

check_hostname (ssl.SSLContext 属性)

check_output() (doctest.OutputChecker 方法)

(在 subprocess 模块中)

check_returncode() (subprocess.CompletedProcess 方法)

check_unused_args() (string.Formatter 方法)

check_warnings() (在 test.support 模块中)

checkbox() (msilib.Dialog 方法)

checkcache() (在 linecache 模块中)

checkfuncname() (在 bdb 模块中)

CheckList (tkinter.tix 中的类)

checksum

Cyclic Redundancy Check

chflags() (在 os 模块中)

chgat() (curses.window 方法)

childNodes (xml.dom.Node 属性)

ChildProcessError

chmod() (pathlib.Path 方法)

(在 os 模块中)

choice() (在 random 模块中)

(在 secrets 模块中)

choices (optparse.Option 属性)

choices() (在 random 模块中)

chown() (在 os 模块中)

(在 shutil 模块中)

chr

內置函数

chr() (內置函数)

chroot() (在 os 模块中)

Chunk (chunk 中的类)

chunk (模块)

cipher

DES

cipher() (ssl.SSLSocket 方法)

circle() (在 turtle 模块中)

CIRCUMFLEX() (在 token 模块中)

CIRCUMFLEXEQUAL() (在 token 模块中)

Clamped (decimal 中的类)

class

attribute

attribute assignment

body

constructor

definition, [1]

instance

name

对象, [1], [2]

语句

Class (symtable 中的类)

class -- 类

Class browser

class instance

attribute

attribute assignment

call

对象, [1], [2]

class object

call, [1], [2]

class variable -- 类变量

classmethod

內置函数

classmethod() (內置函数)

ClassVar() (在 typing 模块中)

clause

CLD_CONTINUED() (在 os 模块中)

CLD_DUMPED() (在 os 模块中)

CLD_EXITED() (在 os 模块中)

CLD_TRAPPED() (在 os 模块中)

clean() (mailbox.Maildir 方法)

cleandoc() (在 inspect 模块中)

cleanup functions

clear (pdb command)

Clear Breakpoint

clear() (asyncio.Event 方法)

(collections.deque 方法)

(curses.window 方法)

(dict 方法)

(email.message.EmailMessage 方法)

(frame 方法)

(frozenset 方法)

(http.cookiejar.CookieJar 方法)

(mailbox.Mailbox 方法)

(sequence method)

(threading.Event 方法)

(xml.etree.ElementTree.Element 方法)

(在 turtle 模块中), [1]

clear_all_breaks() (bdb.Bdb 方法)

clear_all_file_breaks() (bdb.Bdb 方法)

clear_bpbynumber() (bdb.Bdb 方法)

clear_break() (bdb.Bdb 方法)

clear_cache() (在 filecmp 模块中)

clear_content() (email.message.EmailMessage 方法)

clear_flags() (decimal.Context 方法)

clear_frames() (在 traceback 模块中)

clear_history() (在 readline 模块中)

clear_session_cookies() (http.cookiejar.CookieJar 方法)

clear_traces() (在 tracemalloc 模块中)

clear_traps() (decimal.Context 方法)

clearcache() (在 linecache 模块中)

ClearData() (msilib.Record 方法)

clearok() (curses.window 方法)

clearscreen() (在 turtle 模块中)

clearstamp() (在 turtle 模块中)

clearstamps() (在 turtle 模块中)

Client() (在 multiprocessing.connection 模块中)

client_address (http.server.BaseHTTPRequestHandler 属性)

clock() (在 time 模块中)

clock_getres() (在 time 模块中)

clock_gettime() (在 time 模块中)

CLOCK_HIGHRES() (在 time 模块中)

CLOCK_MONOTONIC() (在 time 模块中)

CLOCK_MONOTONIC_RAW() (在 time 模块中)

CLOCK_PROCESS_CPUTIME_ID() (在 time 模块中)

CLOCK_REALTIME() (在 time 模块中)

clock_settime() (在 time 模块中)

CLOCK_THREAD_CPUTIME_ID() (在 time 模块中)

clone() (email.generator.BytesGenerator 方法)

(email.generator.Generator 方法)

(email.policy.Policy 方法)

(pipes.Template 方法)

(在 turtle 模块中)

cloneNode() (xml.dom.Node 方法)

close() (aifc.aifc 方法), [1]

(asyncio.AbstractEventLoop 方法)

(asyncio.BaseSubprocessTransport 方法)

(asyncio.BaseTransport 方法)

(asyncio.Server 方法)

(asyncio.StreamWriter 方法)

(asyncore.dispatcher 方法)

(chunk.Chunk 方法)

(contextlib.ExitStack 方法)

(coroutine 方法)

(dbm.dumb.dumbdbm 方法)

(dbm.gnu.gdbm 方法)

(dbm.ndbm.ndbm 方法)

(distutils.text_file.TextFile 方法)

(email.parser.BytesFeedParser 方法)

(ftplib.FTP 方法)

(generator 方法)

(html.parser.HTMLParser 方法)

(http.client.HTTPConnection 方法)

(imaplib.IMAP4 方法)

(in module os)

(io.IOBase 方法)

(logging.FileHandler 方法)

(logging.Handler 方法)

(logging.handlers.MemoryHandler 方法)

(logging.handlers.NTEventLogHandler 方法)

(logging.handlers.SocketHandler 方法)

(logging.handlers.SysLogHandler 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(mailbox.MH 方法)

(mmap.mmap 方法)

Close() (msilib.View 方法)

close() (multiprocessing.connection.Connection 方法)

(multiprocessing.connection.Listener 方法)

(multiprocessing.pool.Pool 方法)

(multiprocessing.Queue 方法)

(os.scandir 方法)

(ossaudiodev.oss_audio_device 方法)

(ossaudiodev.oss_mixer_device 方法)

(select.devpoll 方法)

(select.epoll 方法)

(select.kqueue 方法)

(selectors.BaseSelector 方法)

(shelve.Shelf 方法)

(socket.socket 方法)

(sqlite3.Connection 方法)

(sqlite3.Cursor 方法)

(sunau.AU_read 方法)

(sunau.AU_write 方法)

(tarfile.TarFile 方法)

(telnetlib.Telnet 方法)

(urllib.request.BaseHandler 方法)

(wave.Wave_read 方法)

(wave.Wave_write 方法)

Close() (winreg.PyHKEY 方法)

close() (xml.etree.ElementTree.TreeBuilder 方法)

(xml.etree.ElementTree.XMLParser 方法)

(xml.etree.ElementTree.XMLPullParser 方法)

(xml.sax.xmlreader.IncrementalParser 方法)

(zipfile.ZipFile 方法)

(在 fileinput 模块中)

(在 os 模块中)

close_connection (http.server.BaseHTTPRequestHandler 属性)

close_when_done() (asynchat.async_chat 方法)

closed (http.client.HTTPResponse 属性)

(io.IOBase 属性)

(mmap.mmap 属性)

(ossaudiodev.oss_audio_device 属性)

(select.devpoll 属性)

(select.epoll 属性)

(select.kqueue 属性)

CloseKey() (在 winreg 模块中)

closelog() (在 syslog 模块中)

closerange() (在 os 模块中)

closing() (在 contextlib 模块中)

clrtobot() (curses.window 方法)

clrtoeol() (curses.window 方法)

cmath (模块)

cmd

模块

Cmd (cmd 中的类)

cmd (subprocess.CalledProcessError 属性)

(subprocess.TimeoutExpired 属性)

(模块)

cmdloop() (cmd.Cmd 方法)

cmdqueue (cmd.Cmd 属性)

cmp() (在 filecmp 模块中)

cmp_op() (在 dis 模块中)

cmp_to_key() (在 functools 模块中)

cmpfiles() (在 filecmp 模块中)

CMSG_LEN() (在 socket 模块中)

CMSG_SPACE() (在 socket 模块中)

co_argcount (code object attribute)

CO_ASYNC_GENERATOR() (在 inspect 模块中)

co_cellvars (code object attribute)

co_code (code object attribute)

co_consts (code object attribute)

CO_COROUTINE() (在 inspect 模块中)

co_filename (code object attribute)

co_firstlineno (code object attribute)

co_flags (code object attribute)

co_freevars (code object attribute)

CO_FUTURE_DIVISION (C 变量)

CO_GENERATOR() (在 inspect 模块中)

CO_ITERABLE_COROUTINE() (在 inspect 模块中)

co_lnotab (code object attribute)

co_name (code object attribute)

co_names (code object attribute)

CO_NESTED() (在 inspect 模块中)

CO_NEWLOCALS() (在 inspect 模块中)

co_nlocals (code object attribute)

CO_NOFREE() (在 inspect 模块中)

CO_OPTIMIZED() (在 inspect 模块中)

co_stacksize (code object attribute)

CO_VARARGS() (在 inspect 模块中)

CO_VARKEYWORDS() (在 inspect 模块中)

co_varnames (code object attribute)

code

block

code (SystemExit 属性)

(urllib.error.HTTPError 属性)

(xml.etree.ElementTree.ParseError 属性)

(xml.parsers.expat.ExpatError 属性)

(模块)

code object, [1], [2], [3]

code_info() (在 dis 模块中)

CodecInfo (codecs 中的类)

Codecs

decode

encode

codecs (模块)

coded_value (http.cookies.Morsel 属性)

codeop (模块)

codepoint2name() (在 html.entities 模块中)

codes() (在 xml.parsers.expat.errors 模块中)

CODESET() (在 locale 模块中)

CodeType() (在 types 模块中)

coding

style

coercion -- 强制类型转换

col_offset (ast.AST 属性)

collapse_addresses() (在 ipaddress 模块中)

collapse_rfc2231_value() (在 email.utils 模块中)

collect() (在 gc 模块中)

collect_incoming_data() (asynchat.async_chat 方法)

Collection (collections.abc 中的类)

(typing 中的类)

collections (模块)

collections.abc (模块)

colno (json.JSONDecodeError 属性)

(re.error 属性)

COLON() (在 token 模块中)

color() (在 turtle 模块中)

color_content() (在 curses 模块中)

color_pair() (在 curses 模块中)

colormode() (在 turtle 模块中)

colorsys (模块)

COLS, [1]

column() (tkinter.ttk.Treeview 方法)

COLUMNS, [1]

columns (os.terminal_size 属性)

combinations() (在 itertools 模块中)

combinations_with_replacement() (在 itertools 模块中)

combine() (datetime.datetime 类方法)

combining() (在 unicodedata 模块中)

ComboBox (tkinter.tix 中的类)

Combobox (tkinter.ttk 中的类)

comma

trailing

tuple display

COMMA() (在 token 模块中)

Command (distutils.cmd 中的类)

(distutils.core 中的类)

command (http.server.BaseHTTPRequestHandler 属性)

command line

command line option

--help

--version

-?

-b

-B

-c <command>

-d

-E

-h

-i

-I

-J

-m <module-name>

-O

-OO

-q

-R

-s

-S

-u

-V

-v

-W arg

-x

-X

CommandCompiler (codeop 中的类)

commands (pdb command)

comment

(http.cookiejar.Cookie 属性)

(zipfile.ZipFile 属性)

(zipfile.ZipInfo 属性)

COMMENT() (在 tokenize 模块中)

Comment() (在 xml.etree.ElementTree 模块中)

comment_url (http.cookiejar.Cookie 属性)

commenters (shlex.shlex 属性)

CommentHandler() (xml.parsers.expat.xmlparser 方法)

commit() (msilib.CAB 方法)

Commit() (msilib.Database 方法)

commit() (sqlite3.Connection 方法)

common (filecmp.dircmp 属性)

Common Gateway Interface

common_dirs (filecmp.dircmp 属性)

common_files (filecmp.dircmp 属性)

common_funny (filecmp.dircmp 属性)

common_types() (在 mimetypes 模块中)

commonpath() (在 os.path 模块中)

commonprefix() (在 os.path 模块中)

communicate() (asyncio.asyncio.subprocess.Process 方法)

(subprocess.Popen 方法)

compare() (decimal.Context 方法)

(decimal.Decimal 方法)

(difflib.Differ 方法)

compare_digest() (在 hmac 模块中)

(在 secrets 模块中)

compare_networks() (ipaddress.IPv4Network 方法)

(ipaddress.IPv6Network 方法)

COMPARE_OP (opcode)

compare_signal() (decimal.Context 方法)

(decimal.Decimal 方法)

compare_to() (tracemalloc.Snapshot 方法)

compare_total() (decimal.Context 方法)

(decimal.Decimal 方法)

compare_total_mag() (decimal.Context 方法)

(decimal.Decimal 方法)

comparing

objects

comparison

operator

COMPARISON_FLAGS() (在 doctest 模块中)

comparisons

chaining, [1]

Compat32 (email.policy 中的类)

compat32() (在 email.policy 模块中)

compile

內置函数, [1], [2], [3], [4]

Compile (codeop 中的类)

compile() (distutils.ccompiler.CCompiler 方法)

(parser.ST 方法)

(內置函数)

(在 py_compile 模块中)

(在 re 模块中)

compile_command() (在 code 模块中)

(在 codeop 模块中)

compile_dir() (在 compileall 模块中)

compile_file() (在 compileall 模块中)

compile_path() (在 compileall 模块中)

compileall (模块)

compileall command line option

-b

-d destdir

-f

-i list

-j N

-l

-q

-r

-x regex

directory ...

file ...

compilest() (在 parser 模块中)

complete() (rlcompleter.Completer 方法)

complete_statement() (在 sqlite3 模块中)

completedefault() (cmd.Cmd 方法)

CompletedProcess (subprocess 中的类)

complex

number

內置函数, [1]

对象

Complex (numbers 中的类)

complex (內置类)

complex literal

complex number

literals

对象, [1]

complex number -- 复数

compound

statement

comprehensions

list

compress() (bz2.BZ2Compressor 方法)

(lzma.LZMACompressor 方法)

(zlib.Compress 方法)

(在 bz2 模块中)

(在 gzip 模块中)

(在 itertools 模块中)

(在 lzma 模块中)

(在 zlib 模块中)

compress_size (zipfile.ZipInfo 属性)

compress_type (zipfile.ZipInfo 属性)

compressed (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

compression() (ssl.SSLSocket 方法)

CompressionError

compressobj() (在 zlib 模块中)

COMSPEC, [1]

concat() (在 operator 模块中)

concatenation

operation

concurrent.futures (模块)

Condition (asyncio 中的类)

(multiprocessing 中的类)

condition (pdb command)

Condition (threading 中的类)

condition() (msilib.Control 方法)

Condition() (multiprocessing.managers.SyncManager 方法)

Conditional

expression

conditional

expression

ConfigParser (configparser 中的类)

configparser (模块)

configuration

file

file, debugger

file, path

configuration information

configure() (tkinter.ttk.Style 方法)

configure_mock() (unittest.mock.Mock 方法)

confstr() (在 os 模块中)

confstr_names() (在 os 模块中)

conjugate() (complex number method)

(decimal.Decimal 方法)

(numbers.Complex 方法)

conn (smtpd.SMTPChannel 属性)

connect() (asyncore.dispatcher 方法)

(ftplib.FTP 方法)

(http.client.HTTPConnection 方法)

(multiprocessing.managers.BaseManager 方法)

(smtplib.SMTP 方法)

(socket.socket 方法)

(在 sqlite3 模块中)

connect_accepted_socket() (asyncio.BaseEventLoop 方法)

connect_ex() (socket.socket 方法)

connect_read_pipe() (asyncio.AbstractEventLoop 方法)

connect_write_pipe() (asyncio.AbstractEventLoop 方法)

Connection (multiprocessing.connection 中的类)

(sqlite3 中的类)

connection (sqlite3.Cursor 属性)

connection_lost() (asyncio.BaseProtocol 方法)

connection_made() (asyncio.BaseProtocol 方法)

ConnectionAbortedError

ConnectionError

ConnectionRefusedError

ConnectionResetError

ConnectRegistry() (在 winreg 模块中)

const (optparse.Option 属性)

constant

constructor

class

constructor() (在 copyreg 模块中)

consumed (asyncio.LimitOverrunError 属性)

container, [1]

iteration over

Container (collections.abc 中的类)

(typing 中的类)

contains() (在 operator 模块中)

content type

MIME

content_manager (email.policy.EmailPolicy 属性)

content_type (email.headerregistry.ContentTypeHeader 属性)

ContentDispositionHeader (email.headerregistry 中的类)

ContentHandler (xml.sax.handler 中的类)

ContentManager (email.contentmanager 中的类)

contents (ctypes._Pointer 属性)

ContentTooShortError

ContentTransferEncoding (email.headerregistry 中的类)

ContentTypeHeader (email.headerregistry 中的类)

Context (decimal 中的类)

context (ssl.SSLSocket 属性)

context management protocol

context manager, [1]

context manager -- 上下文管理器

context_diff() (在 difflib 模块中)

ContextDecorator (contextlib 中的类)

contextlib (模块)

ContextManager (typing 中的类)

contextmanager() (在 contextlib 模块中)

contiguous

(memoryview 属性)

contiguous -- 连续

continue

语句, [1], [2], [3], [4]

continue (pdb command)

CONTINUE_LOOP (opcode)

Control (msilib 中的类)

(tkinter.tix 中的类)

control() (msilib.Dialog 方法)

(select.kqueue 方法)

controlnames() (在 curses.ascii 模块中)

controls() (ossaudiodev.oss_mixer_device 方法)

conversion

arithmetic

string, [1]

ConversionError

conversions

numeric

convert_arg_line_to_args() (argparse.ArgumentParser 方法)

convert_field() (string.Formatter 方法)

convert_path() (在 distutils.util 模块中)

Cookie (http.cookiejar 中的类)

CookieError

CookieJar (http.cookiejar 中的类)

cookiejar (urllib.request.HTTPCookieProcessor 属性)

CookiePolicy (http.cookiejar 中的类)

Coordinated Universal Time

Copy

copy

protocol

模块

copy (模块)

copy() (collections.deque 方法)

(decimal.Context 方法)

(dict 方法)

(frozenset 方法)

(hashlib.hash 方法)

(hmac.HMAC 方法)

(http.cookies.Morsel 方法)

(imaplib.IMAP4 方法)

(pipes.Template 方法)

(sequence method)

(types.MappingProxyType 方法)

(zlib.Compress 方法)

(zlib.Decompress 方法)

(在 copy 模块中)

(在 multiprocessing.sharedctypes 模块中)

(在 shutil 模块中)

copy2() (在 shutil 模块中)

copy_abs() (decimal.Context 方法)

(decimal.Decimal 方法)

copy_decimal() (decimal.Context 方法)

copy_file() (在 distutils.file_util 模块中)

copy_location() (在 ast 模块中)

copy_negate() (decimal.Context 方法)

(decimal.Decimal 方法)

copy_sign() (decimal.Context 方法)

(decimal.Decimal 方法)

copy_tree() (在 distutils.dir_util 模块中)

copyfile() (在 shutil 模块中)

copyfileobj() (在 shutil 模块中)

copying files

copymode() (在 shutil 模块中)

copyreg (模块)

copyright (in module sys)

(內置变量)

copyright() (在 sys 模块中)

copysign() (在 math 模块中)

copystat() (在 shutil 模块中)

copytree() (在 shutil 模块中)

coroutine, [1]

function

Coroutine (collections.abc 中的类)

(typing 中的类)

coroutine -- 协程

coroutine function -- 协程函数

coroutine() (在 asyncio 模块中)

(在 types 模块中)

CoroutineType() (在 types 模块中)

cos() (在 cmath 模块中)

(在 math 模块中)

cosh() (在 cmath 模块中)

(在 math 模块中)

count (tracemalloc.Statistic 属性)

(tracemalloc.StatisticDiff 属性)

count() (array.array 方法)

(bytearray 方法)

(bytes 方法)

(collections.deque 方法)

(sequence method)

(str 方法)

(在 itertools 模块中)

count_diff (tracemalloc.StatisticDiff 属性)

Counter (collections 中的类)

(typing 中的类)

countOf() (在 operator 模块中)

countTestCases() (unittest.TestCase 方法)

(unittest.TestSuite 方法)

CoverageResults (trace 中的类)

CPP

CPPFLAGS

cProfile (模块)

CPU time

cpu_count() (在 multiprocessing 模块中)

(在 os 模块中)

CPython

crawl_delay() (urllib.robotparser.RobotFileParser 方法)

CRC (zipfile.ZipInfo 属性)

crc32() (在 binascii 模块中)

(在 zlib 模块中)

crc_hqx() (在 binascii 模块中)

create() (imaplib.IMAP4 方法)

(venv.EnvBuilder 方法)

(在 venv 模块中)

create_aggregate() (sqlite3.Connection 方法)

create_archive() (在 zipapp 模块中)

create_autospec() (在 unittest.mock 模块中)

create_collation() (sqlite3.Connection 方法)

create_configuration() (venv.EnvBuilder 方法)

create_connection() (asyncio.AbstractEventLoop 方法)

(在 socket 模块中)

create_datagram_endpoint() (asyncio.AbstractEventLoop 方法)

create_decimal() (decimal.Context 方法)

create_decimal_from_float() (decimal.Context 方法)

create_default_context() (在 ssl 模块中)

create_function() (sqlite3.Connection 方法)

create_future() (asyncio.AbstractEventLoop 方法)

create_module (C 函数)

create_module() (importlib.abc.Loader 方法)

(importlib.machinery.ExtensionFileLoader 方法)

CREATE_NEW_CONSOLE() (在 subprocess 模块中)

CREATE_NEW_PROCESS_GROUP() (在 subprocess 模块中)

create_server() (asyncio.AbstractEventLoop 方法)

create_shortcut() (內置函数)

create_socket() (asyncore.dispatcher 方法)

create_static_lib() (distutils.ccompiler.CCompiler 方法)

create_stats() (profile.Profile 方法)

create_string_buffer() (在 ctypes 模块中)

create_subprocess_exec() (在 asyncio 模块中)

create_subprocess_shell() (在 asyncio 模块中)

create_system (zipfile.ZipInfo 属性)

create_task() (asyncio.AbstractEventLoop 方法)

create_tree() (在 distutils.dir_util 模块中)

create_unicode_buffer() (在 ctypes 模块中)

create_unix_connection() (asyncio.AbstractEventLoop 方法)

create_unix_server() (asyncio.AbstractEventLoop 方法)

create_version (zipfile.ZipInfo 属性)

createAttribute() (xml.dom.Document 方法)

createAttributeNS() (xml.dom.Document 方法)

createComment() (xml.dom.Document 方法)

createDocument() (xml.dom.DOMImplementation 方法)

createDocumentType() (xml.dom.DOMImplementation 方法)

createElement() (xml.dom.Document 方法)

createElementNS() (xml.dom.Document 方法)

createfilehandler() (tkinter.Widget.tk 方法)

CreateKey() (在 winreg 模块中)

CreateKeyEx() (在 winreg 模块中)

createLock() (logging.Handler 方法)

(logging.NullHandler 方法)

createProcessingInstruction() (xml.dom.Document 方法)

CreateRecord() (在 msilib 模块中)

createSocket() (logging.handlers.SocketHandler 方法)

createTextNode() (xml.dom.Document 方法)

credits (內置变量)

critical() (logging.Logger 方法)

(在 logging 模块中)

CRNCYSTR() (在 locale 模块中)

cross() (在 audioop 模块中)

crypt

模块

crypt (模块)

crypt() (在 crypt 模块中)

crypt(3), [1], [2]

cryptography

csv

(模块)

cte (email.headerregistry.ContentTransferEncoding 属性)

cte_type (email.policy.Policy 属性)

ctermid() (在 os 模块中)

ctime() (datetime.date 方法)

(datetime.datetime 方法)

(在 time 模块中)

ctrl() (在 curses.ascii 模块中)

CTRL_BREAK_EVENT() (在 signal 模块中)

CTRL_C_EVENT() (在 signal 模块中)

ctypes (模块)

curdir() (在 os 模块中)

currency() (在 locale 模块中)

current() (tkinter.ttk.Combobox 方法)

current_process() (在 multiprocessing 模块中)

current_task() (asyncio.Task 类方法)

current_thread() (在 threading 模块中)

CurrentByteIndex (xml.parsers.expat.xmlparser 属性)

CurrentColumnNumber (xml.parsers.expat.xmlparser 属性)

currentframe() (在 inspect 模块中)

CurrentLineNumber (xml.parsers.expat.xmlparser 属性)

curs_set() (在 curses 模块中)

curses (模块)

curses.ascii (模块)

curses.panel (模块)

curses.textpad (模块)

Cursor (sqlite3 中的类)

cursor() (sqlite3.Connection 方法)

cursyncup() (curses.window 方法)

customize_compiler() (在 distutils.sysconfig 模块中)

Cut

cwd() (ftplib.FTP 方法)

(pathlib.Path 类方法)

cycle() (在 itertools 模块中)

Cyclic Redundancy Check

D

D_FMT() (在 locale 模块中)

D_T_FMT() (在 locale 模块中)

daemon (multiprocessing.Process 属性)

(threading.Thread 属性)

dangling

else

data

packing binary

tabular

type

type, immutable

data (collections.UserDict 属性)

(collections.UserList 属性)

Data (plistlib 中的类)

data (select.kevent 属性)

(selectors.SelectorKey 属性)

(urllib.request.Request 属性)

(xml.dom.Comment 属性)

(xml.dom.ProcessingInstruction 属性)

(xml.dom.Text 属性)

(xmlrpc.client.Binary 属性)

data() (xml.etree.ElementTree.TreeBuilder 方法)

data_open() (urllib.request.DataHandler 方法)

data_received() (asyncio.Protocol 方法)

database

Unicode

DatabaseError

databases

datagram_received() (asyncio.DatagramProtocol 方法)

DatagramHandler (logging.handlers 中的类)

DatagramProtocol (asyncio 中的类)

DatagramRequestHandler (socketserver 中的类)

DataHandler (urllib.request 中的类)

date (datetime 中的类)

date() (datetime.datetime 方法)

(nntplib.NNTP 方法)

date_time (zipfile.ZipInfo 属性)

date_time_string() (http.server.BaseHTTPRequestHandler 方法)

DateHeader (email.headerregistry 中的类)

datetime (datetime 中的类)

(email.headerregistry.DateHeader 属性)

DateTime (xmlrpc.client 中的类)

datetime (模块)

datum

day (datetime.date 属性)

(datetime.datetime 属性)

day_abbr() (在 calendar 模块中)

day_name() (在 calendar 模块中)

Daylight Saving Time

daylight() (在 time 模块中)

DbfilenameShelf (shelve 中的类)

dbm (模块)

dbm.dumb (模块)

dbm.gnu

模块, [1]

dbm.gnu (模块)

dbm.ndbm

模块, [1]

dbm.ndbm (模块)

dcgettext() (在 locale 模块中)

deallocation, object

debug (imaplib.IMAP4 属性)

(shlex.shlex 属性)

(zipfile.ZipFile 属性)

debug() (logging.Logger 方法)

(pipes.Template 方法)

(unittest.TestCase 方法)

(unittest.TestSuite 方法)

(在 doctest 模块中)

(在 logging 模块中)

DEBUG() (在 re 模块中)

DEBUG_BYTECODE_SUFFIXES() (在 importlib.machinery 模块中)

DEBUG_COLLECTABLE() (在 gc 模块中)

DEBUG_LEAK() (在 gc 模块中)

debug_print() (distutils.ccompiler.CCompiler 方法)

DEBUG_SAVEALL() (在 gc 模块中)

debug_src() (在 doctest 模块中)

DEBUG_STATS() (在 gc 模块中)

DEBUG_UNCOLLECTABLE() (在 gc 模块中)

debugger, [1], [2]

configuration file

debugging

assertions

CGI

DebuggingServer (smtpd 中的类)

debuglevel (http.client.HTTPResponse 属性)

DebugRunner (doctest 中的类)

Decimal (decimal 中的类)

decimal (模块)

decimal literal

decimal() (在 unicodedata 模块中)

DecimalException (decimal 中的类)

decode

Codecs

decode (codecs.CodecInfo 属性)

decode() (bytearray 方法)

(bytes 方法)

(codecs.Codec 方法)

(codecs.IncrementalDecoder 方法)

(json.JSONDecoder 方法)

(xmlrpc.client.Binary 方法)

(xmlrpc.client.DateTime 方法)

(在 base64 模块中)

(在 codecs 模块中)

(在 quopri 模块中)

(在 uu 模块中)

decode_header() (在 email.header 模块中)

(在 nntplib 模块中)

decode_params() (在 email.utils 模块中)

decode_rfc2231() (在 email.utils 模块中)

decode_source() (在 importlib.util 模块中)

decodebytes() (在 base64 模块中)

DecodedGenerator (email.generator 中的类)

decodestring() (在 base64 模块中)

(在 quopri 模块中)

decomposition() (在 unicodedata 模块中)

decompress() (bz2.BZ2Decompressor 方法)

(lzma.LZMADecompressor 方法)

(zlib.Decompress 方法)

(在 bz2 模块中)

(在 gzip 模块中)

(在 lzma 模块中)

(在 zlib 模块中)

decompressobj() (在 zlib 模块中)

decorator -- 装饰器

DEDENT token, [1]

dedent() (在 textwrap 模块中)

DEDENT() (在 token 模块中)

deepcopy() (在 copy 模块中)

def

语句

def_prog_mode() (在 curses 模块中)

def_shell_mode() (在 curses 模块中)

default

parameter value

default (inspect.Parameter 属性)

(optparse.Option 属性)

default() (cmd.Cmd 方法)

(json.JSONEncoder 方法)

(在 email.policy 模块中)

DEFAULT() (在 unittest.mock 模块中)

DEFAULT_BUFFER_SIZE() (在 io 模块中)

default_bufsize() (在 xml.dom.pulldom 模块中)

default_exception_handler() (asyncio.AbstractEventLoop 方法)

default_factory (collections.defaultdict 属性)

DEFAULT_FORMAT() (在 tarfile 模块中)

DEFAULT_IGNORES() (在 filecmp 模块中)

default_open() (urllib.request.BaseHandler 方法)

DEFAULT_PROTOCOL() (在 pickle 模块中)

default_timer() (在 timeit 模块中)

DefaultContext (decimal 中的类)

DefaultCookiePolicy (http.cookiejar 中的类)

defaultdict (collections 中的类)

DefaultDict (typing 中的类)

DefaultHandler() (xml.parsers.expat.xmlparser 方法)

DefaultHandlerExpand() (xml.parsers.expat.xmlparser 方法)

defaults() (configparser.ConfigParser 方法)

DefaultSelector (selectors 中的类)

defaultTestLoader() (在 unittest 模块中)

defaultTestResult() (unittest.TestCase 方法)

defects (email.headerregistry.BaseHeader 属性)

(email.message.EmailMessage 属性)

(email.message.Message 属性)

define_macro() (distutils.ccompiler.CCompiler 方法)

definition

class, [1]

function, [1]

defpath() (在 os 模块中)

DefragResult (urllib.parse 中的类)

DefragResultBytes (urllib.parse 中的类)

degrees() (在 math 模块中)

(在 turtle 模块中)

del

语句, [1], [2], [3]

del_param() (email.message.EmailMessage 方法)

(email.message.Message 方法)

delattr() (內置函数)

delay() (在 turtle 模块中)

delay_output() (在 curses 模块中)

delayload (http.cookiejar.FileCookieJar 属性)

delch() (curses.window 方法)

dele() (poplib.POP3 方法)

delete() (ftplib.FTP 方法)

(imaplib.IMAP4 方法)

(tkinter.ttk.Treeview 方法)

DELETE_ATTR (opcode)

DELETE_DEREF (opcode)

DELETE_FAST (opcode)

DELETE_GLOBAL (opcode)

DELETE_NAME (opcode)

DELETE_SUBSCR (opcode)

deleteacl() (imaplib.IMAP4 方法)

deletefilehandler() (tkinter.Widget.tk 方法)

DeleteKey() (在 winreg 模块中)

DeleteKeyEx() (在 winreg 模块中)

deleteln() (curses.window 方法)

deleteMe() (bdb.Breakpoint 方法)

DeleteValue() (在 winreg 模块中)

deletion

attribute

target

target list

delimiter (csv.Dialect 属性)

delimiters

delitem() (在 operator 模块中)

deliver_challenge() (在 multiprocessing.connection 模块中)

delocalize() (在 locale 模块中)

demo_app() (在 wsgiref.simple_server 模块中)

denominator (fractions.Fraction 属性)

(numbers.Rational 属性)

DeprecationWarning

deque (collections 中的类)

Deque (typing 中的类)

dequeue() (logging.handlers.QueueListener 方法)

DER_cert_to_PEM_cert() (在 ssl 模块中)

derwin() (curses.window 方法)

DES

cipher

description (sqlite3.Cursor 属性)

description() (nntplib.NNTP 方法)

descriptions() (nntplib.NNTP 方法)

descriptor -- 描述器

dest (optparse.Option 属性)

destructor, [1]

detach() (io.BufferedIOBase 方法)

(io.TextIOBase 方法)

(socket.socket 方法)

(tkinter.ttk.Treeview 方法)

(weakref.finalize 方法)

Detach() (winreg.PyHKEY 方法)

detect_api_mismatch() (在 test.support 模块中)

detect_encoding() (在 tokenize 模块中)

detect_language() (distutils.ccompiler.CCompiler 方法)

deterministic profiling

device_encoding() (在 os 模块中)

devnull() (在 os 模块中)

DEVNULL() (在 subprocess 模块中)

devpoll() (在 select 模块中)

DevpollSelector (selectors 中的类)

dgettext() (在 gettext 模块中)

(在 locale 模块中)

Dialect (csv 中的类)

dialect (csv.csvreader 属性)

(csv.csvwriter 属性)

Dialog (msilib 中的类)

dict (2to3 fixer)

Dict (typing 中的类)

dict (內置类)

dict() (multiprocessing.managers.SyncManager 方法)

dictConfig() (在 logging.config 模块中)

dictionary

display

type, operations on

对象, [1], [2], [3], [4], [5], [6], [7]

dictionary -- 字典

dictionary view -- 字典视图

DictReader (csv 中的类)

DictWriter (csv 中的类)

diff_bytes() (在 difflib 模块中)

diff_files (filecmp.dircmp 属性)

Differ (difflib 中的类), [1]

difference() (frozenset 方法)

difference_update() (frozenset 方法)

difflib (模块)

digest() (hashlib.hash 方法)

(hashlib.shake 方法)

(hmac.HMAC 方法)

digest_size (hmac.HMAC 属性)

digit() (在 unicodedata 模块中)

digits() (在 string 模块中)

dir() (ftplib.FTP 方法)

(內置函数)

dircmp (filecmp 中的类)

directory

changing

creating

deleting, [1]

site-packages

traversal, [1]

walking, [1]

Directory (msilib 中的类)

directory ...

compileall command line option

directory_created() (內置函数)

DirEntry (os 中的类)

DirList (tkinter.tix 中的类)

dirname() (在 os.path 模块中)

DirSelectBox (tkinter.tix 中的类)

DirSelectDialog (tkinter.tix 中的类)

DirTree (tkinter.tix 中的类)

dis (模块)

dis() (dis.Bytecode 方法)

(在 dis 模块中)

(在 pickletools 模块中)

disable (pdb command)

disable() (bdb.Breakpoint 方法)

(profile.Profile 方法)

(在 faulthandler 模块中)

(在 gc 模块中)

(在 logging 模块中)

disable_interspersed_args() (optparse.OptionParser 方法)

DisableReflectionKey() (在 winreg 模块中)

disassemble() (在 dis 模块中)

discard (http.cookiejar.Cookie 属性)

discard() (frozenset 方法)

(mailbox.Mailbox 方法)

(mailbox.MH 方法)

discard_buffers() (asynchat.async_chat 方法)

disco() (在 dis 模块中)

discover() (unittest.TestLoader 方法)

disk_usage() (在 shutil 模块中)

dispatch_call() (bdb.Bdb 方法)

dispatch_exception() (bdb.Bdb 方法)

dispatch_line() (bdb.Bdb 方法)

dispatch_return() (bdb.Bdb 方法)

dispatch_table (pickle.Pickler 属性)

dispatcher (asyncore 中的类)

dispatcher_with_send (asyncore 中的类)

display

dictionary

list

set

tuple

display (pdb command)

display_name (email.headerregistry.Address 属性)

(email.headerregistry.Group 属性)

displayhook() (在 sys 模块中)

dist() (在 platform 模块中)

distance() (在 turtle 模块中)

distb() (在 dis 模块中)

Distribution (distutils.core 中的类)

distutils (模块)

distutils.archive_util (模块)

distutils.bcppcompiler (模块)

distutils.ccompiler (模块)

distutils.cmd (模块)

distutils.command (模块)

distutils.command.bdist (模块)

distutils.command.bdist_dumb (模块)

distutils.command.bdist_msi (模块)

distutils.command.bdist_packager (模块)

distutils.command.bdist_rpm (模块)

distutils.command.bdist_wininst (模块)

distutils.command.build (模块)

distutils.command.build_clib (模块)

distutils.command.build_ext (模块)

distutils.command.build_py (模块)

distutils.command.build_scripts (模块)

distutils.command.check (模块)

distutils.command.clean (模块)

distutils.command.config (模块)

distutils.command.install (模块)

distutils.command.install_data (模块)

distutils.command.install_headers (模块)

distutils.command.install_lib (模块)

distutils.command.install_scripts (模块)

distutils.command.register (模块)

distutils.command.sdist (模块)

distutils.core (模块)

distutils.cygwinccompiler (模块)

distutils.debug (模块)

distutils.dep_util (模块)

distutils.dir_util (模块)

distutils.dist (模块)

distutils.errors (模块)

distutils.extension (模块)

distutils.fancy_getopt (模块)

distutils.file_util (模块)

distutils.filelist (模块)

distutils.log (模块)

distutils.msvccompiler (模块)

distutils.spawn (模块)

distutils.sysconfig (模块)

distutils.text_file (模块)

distutils.unixccompiler (模块)

distutils.util (模块)

distutils.version (模块)

DISTUTILS_DEBUG

divide() (decimal.Context 方法)

divide_int() (decimal.Context 方法)

division

DivisionByZero (decimal 中的类)

divmod

內置函数, [1], [2]

divmod() (decimal.Context 方法)

(內置函数)

DllCanUnloadNow() (在 ctypes 模块中)

DllGetClassObject() (在 ctypes 模块中)

dllhandle() (在 sys 模块中)

dngettext() (在 gettext 模块中)

do_clear() (bdb.Bdb 方法)

do_command() (curses.textpad.Textbox 方法)

do_GET() (http.server.SimpleHTTPRequestHandler 方法)

do_handshake() (ssl.SSLSocket 方法)

do_HEAD() (http.server.SimpleHTTPRequestHandler 方法)

do_POST() (http.server.CGIHTTPRequestHandler 方法)

doc (json.JSONDecodeError 属性)

doc_header (cmd.Cmd 属性)

DocCGIXMLRPCRequestHandler (xmlrpc.server 中的类)

DocFileSuite() (在 doctest 模块中)

doCleanups() (unittest.TestCase 方法)

docmd() (smtplib.SMTP 方法)

docstring

(doctest.DocTest 属性)

docstring -- 文档字符串

docstrings, [1]

DocTest (doctest 中的类)

doctest (模块)

DocTestFailure

DocTestFinder (doctest 中的类)

DocTestParser (doctest 中的类)

DocTestRunner (doctest 中的类)

DocTestSuite() (在 doctest 模块中)

doctype() (xml.etree.ElementTree.TreeBuilder 方法)

(xml.etree.ElementTree.XMLParser 方法)

documentation

generation

online

documentation string

documentation strings, [1]

documentElement (xml.dom.Document 属性)

DocXMLRPCRequestHandler (xmlrpc.server 中的类)

DocXMLRPCServer (xmlrpc.server 中的类)

domain (email.headerregistry.Address 属性)

(tracemalloc.DomainFilter 属性)

(tracemalloc.Filter 属性)

domain_initial_dot (http.cookiejar.Cookie 属性)

domain_return_ok() (http.cookiejar.CookiePolicy 方法)

domain_specified (http.cookiejar.Cookie 属性)

DomainFilter (tracemalloc 中的类)

DomainLiberal (http.cookiejar.DefaultCookiePolicy 属性)

DomainRFC2965Match (http.cookiejar.DefaultCookiePolicy 属性)

DomainStrict (http.cookiejar.DefaultCookiePolicy 属性)

DomainStrictNoDots (http.cookiejar.DefaultCookiePolicy 属性)

DomainStrictNonDomain (http.cookiejar.DefaultCookiePolicy 属性)

DOMEventStream (xml.dom.pulldom 中的类)

DOMException

DomstringSizeErr

done() (asyncio.Future 方法)

(concurrent.futures.Future 方法)

(xdrlib.Unpacker 方法)

(在 turtle 模块中)

DONT_ACCEPT_BLANKLINE() (在 doctest 模块中)

DONT_ACCEPT_TRUE_FOR_1() (在 doctest 模块中)

dont_write_bytecode() (在 sys 模块中)

doRollover() (logging.handlers.RotatingFileHandler 方法)

(logging.handlers.TimedRotatingFileHandler 方法)

DOT() (在 token 模块中)

dot() (在 turtle 模块中)

DOTALL() (在 re 模块中)

doublequote (csv.Dialect 属性)

DOUBLESLASH() (在 token 模块中)

DOUBLESLASHEQUAL() (在 token 模块中)

DOUBLESTAR() (在 token 模块中)

DOUBLESTAREQUAL() (在 token 模块中)

doupdate() (在 curses 模块中)

down (pdb command)

down() (在 turtle 模块中)

drain() (asyncio.StreamWriter 方法)

drop_whitespace (textwrap.TextWrapper 属性)

dropwhile() (在 itertools 模块中)

dst() (datetime.datetime 方法)

(datetime.time 方法)

(datetime.timezone 方法)

(datetime.tzinfo 方法)

DTDHandler (xml.sax.handler 中的类)

duck-typing -- 鸭子类型

DumbWriter (formatter 中的类)

dummy_threading (模块)

dump() (pickle.Pickler 方法)

(tracemalloc.Snapshot 方法)

(在 ast 模块中)

(在 json 模块中)

(在 marshal 模块中)

(在 pickle 模块中)

(在 plistlib 模块中)

(在 xml.etree.ElementTree 模块中)

dump_stats() (profile.Profile 方法)

(pstats.Stats 方法)

dump_traceback() (在 faulthandler 模块中)

dump_traceback_later() (在 faulthandler 模块中)

dumps() (在 json 模块中)

(在 marshal 模块中)

(在 pickle 模块中)

(在 plistlib 模块中)

(在 xmlrpc.client 模块中)

dup() (socket.socket 方法)

(在 os 模块中)

dup2() (在 os 模块中)

DUP_TOP (opcode)

DUP_TOP_TWO (opcode)

DuplicateOptionError

DuplicateSectionError

dwFlags (subprocess.STARTUPINFO 属性)

DynamicClassAttribute() (在 types 模块中)

E

e

in numeric literal

e() (在 cmath 模块中)

(在 math 模块中)

E2BIG() (在 errno 模块中)

EACCES() (在 errno 模块中)

EADDRINUSE() (在 errno 模块中)

EADDRNOTAVAIL() (在 errno 模块中)

EADV() (在 errno 模块中)

EAFNOSUPPORT() (在 errno 模块中)

EAFP

EAGAIN() (在 errno 模块中)

EALREADY() (在 errno 模块中)

east_asian_width() (在 unicodedata 模块中)

EBADE() (在 errno 模块中)

EBADF() (在 errno 模块中)

EBADFD() (在 errno 模块中)

EBADMSG() (在 errno 模块中)

EBADR() (在 errno 模块中)

EBADRQC() (在 errno 模块中)

EBADSLT() (在 errno 模块中)

EBFONT() (在 errno 模块中)

EBUSY() (在 errno 模块中)

ECHILD() (在 errno 模块中)

echo() (在 curses 模块中)

echochar() (curses.window 方法)

ECHRNG() (在 errno 模块中)

ECOMM() (在 errno 模块中)

ECONNABORTED() (在 errno 模块中)

ECONNREFUSED() (在 errno 模块中)

ECONNRESET() (在 errno 模块中)

EDEADLK() (在 errno 模块中)

EDEADLOCK() (在 errno 模块中)

EDESTADDRREQ() (在 errno 模块中)

edit() (curses.textpad.Textbox 方法)

EDOM() (在 errno 模块中)

EDOTDOT() (在 errno 模块中)

EDQUOT() (在 errno 模块中)

EEXIST() (在 errno 模块中)

EFAULT() (在 errno 模块中)

EFBIG() (在 errno 模块中)

effective() (在 bdb 模块中)

ehlo() (smtplib.SMTP 方法)

ehlo_or_helo_if_needed() (smtplib.SMTP 方法)

EHOSTDOWN() (在 errno 模块中)

EHOSTUNREACH() (在 errno 模块中)

EIDRM() (在 errno 模块中)

EILSEQ() (在 errno 模块中)

EINPROGRESS() (在 errno 模块中)

EINTR() (在 errno 模块中)

EINVAL() (在 errno 模块中)

EIO() (在 errno 模块中)

EISCONN() (在 errno 模块中)

EISDIR() (在 errno 模块中)

EISNAM() (在 errno 模块中)

EL2HLT() (在 errno 模块中)

EL2NSYNC() (在 errno 模块中)

EL3HLT() (在 errno 模块中)

EL3RST() (在 errno 模块中)

Element (xml.etree.ElementTree 中的类)

element_create() (tkinter.ttk.Style 方法)

element_names() (tkinter.ttk.Style 方法)

element_options() (tkinter.ttk.Style 方法)

ElementDeclHandler() (xml.parsers.expat.xmlparser 方法)

elements() (collections.Counter 方法)

ElementTree (xml.etree.ElementTree 中的类)

ELIBACC() (在 errno 模块中)

ELIBBAD() (在 errno 模块中)

ELIBEXEC() (在 errno 模块中)

ELIBMAX() (在 errno 模块中)

ELIBSCN() (在 errno 模块中)

elif

关键字

Ellinghouse, Lance

Ellipsis

对象

Ellipsis (內置变量)

ELLIPSIS() (在 doctest 模块中)

(在 token 模块中)

ELNRNG() (在 errno 模块中)

ELOOP() (在 errno 模块中)

else

conditional expression

dangling

关键字, [1], [2], [3], [4], [5], [6], [7]

email (模块)

email.charset (模块)

email.contentmanager (模块)

email.encoders (模块)

email.errors (模块)

email.generator (模块)

email.header (模块)

email.headerregistry (模块)

email.iterators (模块)

email.message (模块)

email.mime (模块)

email.parser (模块)

email.policy (模块)

email.utils (模块)

EmailMessage (email.message 中的类)

EmailPolicy (email.policy 中的类)

EMFILE() (在 errno 模块中)

emit() (logging.FileHandler 方法)

(logging.Handler 方法)

(logging.handlers.BufferingHandler 方法)

(logging.handlers.DatagramHandler 方法)

(logging.handlers.HTTPHandler 方法)

(logging.handlers.NTEventLogHandler 方法)

(logging.handlers.QueueHandler 方法)

(logging.handlers.RotatingFileHandler 方法)

(logging.handlers.SMTPHandler 方法)

(logging.handlers.SocketHandler 方法)

(logging.handlers.SysLogHandler 方法)

(logging.handlers.TimedRotatingFileHandler 方法)

(logging.handlers.WatchedFileHandler 方法)

(logging.NullHandler 方法)

(logging.StreamHandler 方法)

EMLINK() (在 errno 模块中)

Empty

empty

list

tuple, [1]

empty (inspect.Parameter 属性)

(inspect.Signature 属性)

empty() (asyncio.Queue 方法)

(multiprocessing.Queue 方法)

(multiprocessing.SimpleQueue 方法)

(queue.Queue 方法)

(sched.scheduler 方法)

EMPTY_NAMESPACE() (在 xml.dom 模块中)

emptyline() (cmd.Cmd 方法)

EMSGSIZE() (在 errno 模块中)

EMULTIHOP() (在 errno 模块中)

enable (pdb command)

enable() (bdb.Breakpoint 方法)

(imaplib.IMAP4 方法)

(profile.Profile 方法)

(在 cgitb 模块中)

(在 faulthandler 模块中)

(在 gc 模块中)

enable_callback_tracebacks() (在 sqlite3 模块中)

enable_interspersed_args() (optparse.OptionParser 方法)

enable_load_extension() (sqlite3.Connection 方法)

enable_traversal() (tkinter.ttk.Notebook 方法)

ENABLE_USER_SITE() (在 site 模块中)

EnableReflectionKey() (在 winreg 模块中)

ENAMETOOLONG() (在 errno 模块中)

ENAVAIL() (在 errno 模块中)

enclose() (curses.window 方法)

encode

Codecs

encode (codecs.CodecInfo 属性)

encode() (codecs.Codec 方法)

(codecs.IncrementalEncoder 方法)

(email.header.Header 方法)

(json.JSONEncoder 方法)

(str 方法)

(xmlrpc.client.Binary 方法)

(xmlrpc.client.DateTime 方法)

(在 base64 模块中)

(在 codecs 模块中)

(在 quopri 模块中)

(在 uu 模块中)

encode_7or8bit() (在 email.encoders 模块中)

encode_base64() (在 email.encoders 模块中)

encode_noop() (在 email.encoders 模块中)

encode_quopri() (在 email.encoders 模块中)

encode_rfc2231() (在 email.utils 模块中)

encodebytes() (在 base64 模块中)

EncodedFile() (在 codecs 模块中)

encodePriority() (logging.handlers.SysLogHandler 方法)

encodestring() (在 base64 模块中)

(在 quopri 模块中)

encoding

base64

quoted-printable

encoding (curses.window 属性)

(io.TextIOBase 属性)

(UnicodeError 属性)

encoding declarations (source file)

ENCODING() (在 tarfile 模块中)

(在 tokenize 模块中)

encodings.idna (模块)

encodings.mbcs (模块)

encodings.utf_8_sig (模块)

encodings_map (mimetypes.MimeTypes 属性)

encodings_map() (在 mimetypes 模块中)

end (UnicodeError 属性)

end() (re.match 方法)

(xml.etree.ElementTree.TreeBuilder 方法)

end_fill() (在 turtle 模块中)

END_FINALLY (opcode)

end_headers() (http.server.BaseHTTPRequestHandler 方法)

end_paragraph() (formatter.formatter 方法)

end_poly() (在 turtle 模块中)

EndCdataSectionHandler() (xml.parsers.expat.xmlparser 方法)

EndDoctypeDeclHandler() (xml.parsers.expat.xmlparser 方法)

endDocument() (xml.sax.handler.ContentHandler 方法)

endElement() (xml.sax.handler.ContentHandler 方法)

EndElementHandler() (xml.parsers.expat.xmlparser 方法)

endElementNS() (xml.sax.handler.ContentHandler 方法)

endheaders() (http.client.HTTPConnection 方法)

ENDMARKER() (在 token 模块中)

EndNamespaceDeclHandler() (xml.parsers.expat.xmlparser 方法)

endpos (re.match 属性)

endPrefixMapping() (xml.sax.handler.ContentHandler 方法)

endswith() (bytearray 方法)

(bytes 方法)

(str 方法)

endwin() (在 curses 模块中)

ENETDOWN() (在 errno 模块中)

ENETRESET() (在 errno 模块中)

ENETUNREACH() (在 errno 模块中)

ENFILE() (在 errno 模块中)

ENOANO() (在 errno 模块中)

ENOBUFS() (在 errno 模块中)

ENOCSI() (在 errno 模块中)

ENODATA() (在 errno 模块中)

ENODEV() (在 errno 模块中)

ENOENT() (在 errno 模块中)

ENOEXEC() (在 errno 模块中)

ENOLCK() (在 errno 模块中)

ENOLINK() (在 errno 模块中)

ENOMEM() (在 errno 模块中)

ENOMSG() (在 errno 模块中)

ENONET() (在 errno 模块中)

ENOPKG() (在 errno 模块中)

ENOPROTOOPT() (在 errno 模块中)

ENOSPC() (在 errno 模块中)

ENOSR() (在 errno 模块中)

ENOSTR() (在 errno 模块中)

ENOSYS() (在 errno 模块中)

ENOTBLK() (在 errno 模块中)

ENOTCONN() (在 errno 模块中)

ENOTDIR() (在 errno 模块中)

ENOTEMPTY() (在 errno 模块中)

ENOTNAM() (在 errno 模块中)

ENOTSOCK() (在 errno 模块中)

ENOTTY() (在 errno 模块中)

ENOTUNIQ() (在 errno 模块中)

enqueue() (logging.handlers.QueueHandler 方法)

enqueue_sentinel() (logging.handlers.QueueListener 方法)

ensure_directories() (venv.EnvBuilder 方法)

ensure_future() (在 asyncio 模块中)

ensurepip (模块)

enter() (sched.scheduler 方法)

enter_context() (contextlib.ExitStack 方法)

enterabs() (sched.scheduler 方法)

entities (xml.dom.DocumentType 属性)

EntityDeclHandler() (xml.parsers.expat.xmlparser 方法)

entitydefs() (在 html.entities 模块中)

EntityResolver (xml.sax.handler 中的类)

Enum (enum 中的类)

enum (模块)

enum_certificates() (在 ssl 模块中)

enum_crls() (在 ssl 模块中)

enumerate() (內置函数)

(在 threading 模块中)

EnumKey() (在 winreg 模块中)

EnumValue() (在 winreg 模块中)

EnvBuilder (venv 中的类)

environ() (在 os 模块中)

(在 posix 模块中)

environb() (在 os 模块中)

environment

environment variables

deleting

setting

EnvironmentError

Environments

virtual

EnvironmentVarGuard (test.support 中的类)

ENXIO() (在 errno 模块中)

eof (bz2.BZ2Decompressor 属性)

(lzma.LZMADecompressor 属性)

(shlex.shlex 属性)

(ssl.MemoryBIO 属性)

(zlib.Decompress 属性)

eof_received() (asyncio.Protocol 方法)

EOFError

(built-in exception)

EOPNOTSUPP() (在 errno 模块中)

EOVERFLOW() (在 errno 模块中)

EPERM() (在 errno 模块中)

EPFNOSUPPORT() (在 errno 模块中)

epilogue (email.message.EmailMessage 属性)

(email.message.Message 属性)

EPIPE() (在 errno 模块中)

epoch

epoll() (在 select 模块中)

EpollSelector (selectors 中的类)

EPROTO() (在 errno 模块中)

EPROTONOSUPPORT() (在 errno 模块中)

EPROTOTYPE() (在 errno 模块中)

eq() (在 operator 模块中)

EQEQUAL() (在 token 模块中)

EQUAL() (在 token 模块中)

ERA() (在 locale 模块中)

ERA_D_FMT() (在 locale 模块中)

ERA_D_T_FMT() (在 locale 模块中)

ERA_T_FMT() (在 locale 模块中)

ERANGE() (在 errno 模块中)

erase() (curses.window 方法)

erasechar() (在 curses 模块中)

EREMCHG() (在 errno 模块中)

EREMOTE() (在 errno 模块中)

EREMOTEIO() (在 errno 模块中)

ERESTART() (在 errno 模块中)

erf() (在 math 模块中)

erfc() (在 math 模块中)

EROFS() (在 errno 模块中)

ERR() (在 curses 模块中)

errcheck (ctypes._FuncPtr 属性)

errcode (xmlrpc.client.ProtocolError 属性)

errmsg (xmlrpc.client.ProtocolError 属性)

errno

模块

errno (OSError 属性)

(模块)

Error, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]

error, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17]

error handling

error() (argparse.ArgumentParser 方法)

(logging.Logger 方法)

(urllib.request.OpenerDirector 方法)

(xml.sax.handler.ErrorHandler 方法)

(在 logging 模块中)

error_body (wsgiref.handlers.BaseHandler 属性)

error_content_type (http.server.BaseHTTPRequestHandler 属性)

error_headers (wsgiref.handlers.BaseHandler 属性)

error_leader() (shlex.shlex 方法)

error_message_format (http.server.BaseHTTPRequestHandler 属性)

error_output() (wsgiref.handlers.BaseHandler 方法)

error_perm

error_proto, [1]

error_received() (asyncio.DatagramProtocol 方法)

error_reply

error_status (wsgiref.handlers.BaseHandler 属性)

error_temp

ErrorByteIndex (xml.parsers.expat.xmlparser 属性)

ErrorCode (xml.parsers.expat.xmlparser 属性)

errorcode() (在 errno 模块中)

ErrorColumnNumber (xml.parsers.expat.xmlparser 属性)

ErrorHandler (xml.sax.handler 中的类)

ErrorLineNumber (xml.parsers.expat.xmlparser 属性)

Errors

logging

errors

(io.TextIOBase 属性)

(unittest.TestLoader 属性)

(unittest.TestResult 属性)

ErrorString() (在 xml.parsers.expat 模块中)

ERRORTOKEN() (在 token 模块中)

escape (shlex.shlex 属性)

escape sequence

escape() (在 cgi 模块中)

(在 glob 模块中)

(在 html 模块中)

(在 re 模块中)

(在 xml.sax.saxutils 模块中)

escapechar (csv.Dialect 属性)

escapedquotes (shlex.shlex 属性)

ESHUTDOWN() (在 errno 模块中)

ESOCKTNOSUPPORT() (在 errno 模块中)

ESPIPE() (在 errno 模块中)

ESRCH() (在 errno 模块中)

ESRMNT() (在 errno 模块中)

ESTALE() (在 errno 模块中)

ESTRPIPE() (在 errno 模块中)

ETIME() (在 errno 模块中)

ETIMEDOUT() (在 errno 模块中)

Etiny() (decimal.Context 方法)

ETOOMANYREFS() (在 errno 模块中)

Etop() (decimal.Context 方法)

ETXTBSY() (在 errno 模块中)

EUCLEAN() (在 errno 模块中)

EUNATCH() (在 errno 模块中)

EUSERS() (在 errno 模块中)

eval

內置函数, [1], [2], [3], [4], [5]

eval() (內置函数)

evaluation

order

Event (asyncio 中的类)

(multiprocessing 中的类)

(threading 中的类)

event scheduling

event() (msilib.Control 方法)

Event() (multiprocessing.managers.SyncManager 方法)

events (selectors.SelectorKey 属性)

(widgets)

EWOULDBLOCK() (在 errno 模块中)

EX_CANTCREAT() (在 os 模块中)

EX_CONFIG() (在 os 模块中)

EX_DATAERR() (在 os 模块中)

EX_IOERR() (在 os 模块中)

EX_NOHOST() (在 os 模块中)

EX_NOINPUT() (在 os 模块中)

EX_NOPERM() (在 os 模块中)

EX_NOTFOUND() (在 os 模块中)

EX_NOUSER() (在 os 模块中)

EX_OK() (在 os 模块中)

EX_OSERR() (在 os 模块中)

EX_OSFILE() (在 os 模块中)

EX_PROTOCOL() (在 os 模块中)

EX_SOFTWARE() (在 os 模块中)

EX_TEMPFAIL() (在 os 模块中)

EX_UNAVAILABLE() (在 os 模块中)

EX_USAGE() (在 os 模块中)

Example (doctest 中的类)

example (doctest.DocTestFailure 属性)

(doctest.UnexpectedException 属性)

examples (doctest.DocTest 属性)

exc_info (doctest.UnexpectedException 属性)

(in module sys)

exc_info() (in module sys)

(在 sys 模块中)

exc_msg (doctest.Example 属性)

exc_type (traceback.TracebackException 属性)

excel (csv 中的类)

excel_tab (csv 中的类)

except

关键字

语句

except (2to3 fixer)

excepthook() (in module sys)

(在 sys 模块中)

Exception

exception, [1]

chaining

handler

raising

exception handler

exception() (asyncio.Future 方法)

(asyncio.StreamReader 方法)

(concurrent.futures.Future 方法)

(logging.Logger 方法)

(在 logging 模块中)

EXCEPTION() (在 tkinter 模块中)

exceptions

in CGI scripts

exclusive

or

EXDEV() (在 errno 模块中)

exec

內置函数, [1], [2], [3]

exec (2to3 fixer)

exec() (內置函数)

exec_module (C 函数)

exec_module() (importlib.abc.InspectLoader 方法)

(importlib.abc.Loader 方法)

(importlib.abc.SourceLoader 方法)

(importlib.machinery.ExtensionFileLoader 方法)

exec_prefix, [1], [2]

EXEC_PREFIX() (在 distutils.sysconfig 模块中)

exec_prefix() (在 sys 模块中)

execfile (2to3 fixer)

execl() (在 os 模块中)

execle() (在 os 模块中)

execlp() (在 os 模块中)

execlpe() (在 os 模块中)

executable (in module sys)

Executable Zip Files

executable() (在 sys 模块中)

executable_filename() (distutils.ccompiler.CCompiler 方法)

execute() (distutils.ccompiler.CCompiler 方法)

Execute() (msilib.View 方法)

execute() (sqlite3.Connection 方法)

(sqlite3.Cursor 方法)

(在 distutils.util 模块中)

executemany() (sqlite3.Connection 方法)

(sqlite3.Cursor 方法)

executescript() (sqlite3.Connection 方法)

(sqlite3.Cursor 方法)

execution

frame, [1]

restricted

stack

execution model

ExecutionLoader (importlib.abc 中的类)

Executor (concurrent.futures 中的类)

execv() (在 os 模块中)

execve() (在 os 模块中)

execvp() (在 os 模块中)

execvpe() (在 os 模块中)

ExFileSelectBox (tkinter.tix 中的类)

EXFULL() (在 errno 模块中)

exists() (pathlib.Path 方法)

(tkinter.ttk.Treeview 方法)

(在 os.path 模块中)

exit (內置变量)

exit()

(argparse.ArgumentParser 方法)

(在 _thread 模块中)

(在 sys 模块中)

exitcode (multiprocessing.Process 属性)

exitfunc (2to3 fixer)

exitonclick() (在 turtle 模块中)

ExitStack (contextlib 中的类)

exp() (decimal.Context 方法)

(decimal.Decimal 方法)

(在 cmath 模块中)

(在 math 模块中)

expand() (re.match 方法)

expand_tabs (textwrap.TextWrapper 属性)

ExpandEnvironmentStrings() (在 winreg 模块中)

expandNode() (xml.dom.pulldom.DOMEventStream 方法)

expandtabs() (bytearray 方法)

(bytes 方法)

(str 方法)

expanduser() (pathlib.Path 方法)

(在 os.path 模块中)

expandvars() (在 os.path 模块中)

Expat

ExpatError

expect() (telnetlib.Telnet 方法)

expected (asyncio.IncompleteReadError 属性)

expectedFailure() (在 unittest 模块中)

expectedFailures (unittest.TestResult 属性)

expires (http.cookiejar.Cookie 属性)

exploded (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

expm1() (在 math 模块中)

expovariate() (在 random 模块中)

expr() (在 parser 模块中)

expression

Conditional

conditional

generator

lambda, [1]

list, [1], [2]

statement

yield

expression -- 表达式

expunge() (imaplib.IMAP4 方法)

extend() (array.array 方法)

(collections.deque 方法)

(sequence method)

(xml.etree.ElementTree.Element 方法)

extend_path() (在 pkgutil 模块中)

EXTENDED_ARG (opcode)

ExtendedContext (decimal 中的类)

ExtendedInterpolation (configparser 中的类)

extendleft() (collections.deque 方法)

extension

module

Extension (distutils.core 中的类)

extension module -- 扩展模块

EXTENSION_SUFFIXES() (在 importlib.machinery 模块中)

ExtensionFileLoader (importlib.machinery 中的类)

extensions_map (http.server.SimpleHTTPRequestHandler 属性)

External Data Representation, [1]

external_attr (zipfile.ZipInfo 属性)

ExternalClashError

ExternalEntityParserCreate() (xml.parsers.expat.xmlparser 方法)

ExternalEntityRefHandler() (xml.parsers.expat.xmlparser 方法)

extra (zipfile.ZipInfo 属性)

extract() (tarfile.TarFile 方法)

(traceback.StackSummary 类方法)

(zipfile.ZipFile 方法)

extract_cookies() (http.cookiejar.CookieJar 方法)

extract_stack() (在 traceback 模块中)

extract_tb() (在 traceback 模块中)

extract_version (zipfile.ZipInfo 属性)

extractall() (tarfile.TarFile 方法)

(zipfile.ZipFile 方法)

ExtractError

extractfile() (tarfile.TarFile 方法)

extsep() (在 os 模块中)

F

f"

formatted string literal

f'

formatted string literal

f-string

f-string -- f-字符串

f_back (frame attribute)

f_builtins (frame attribute)

f_code (frame attribute)

f_contiguous (memoryview 属性)

f_globals (frame attribute)

f_lasti (frame attribute)

f_lineno (frame attribute)

f_locals (frame attribute)

F_LOCK() (在 os 模块中)

F_OK() (在 os 模块中)

F_TEST() (在 os 模块中)

F_TLOCK() (在 os 模块中)

f_trace (frame attribute)

F_ULOCK() (在 os 模块中)

fabs() (在 math 模块中)

factorial() (在 math 模块中)

factory() (importlib.util.LazyLoader 类方法)

fail() (unittest.TestCase 方法)

FAIL_FAST() (在 doctest 模块中)

failfast (unittest.TestResult 属性)

failureException (unittest.TestCase 属性)

failures (unittest.TestResult 属性)

FakePath (test.support 中的类)

False, [1], [2]

false

False (Built-in object)

(內置变量)

family (socket.socket 属性)

fancy_getopt() (在 distutils.fancy_getopt 模块中)

FancyGetopt (distutils.fancy_getopt 中的类)

FancyURLopener (urllib.request 中的类)

fast (pickle.Pickler 属性)

fatalError() (xml.sax.handler.ErrorHandler 方法)

Fault (xmlrpc.client 中的类)

faultCode (xmlrpc.client.Fault 属性)

faulthandler (模块)

faultString (xmlrpc.client.Fault 属性)

fchdir() (在 os 模块中)

fchmod() (在 os 模块中)

fchown() (在 os 模块中)

FCICreate() (在 msilib 模块中)

fcntl (模块)

fcntl() (在 fcntl 模块中)

fd (selectors.SelectorKey 属性)

fd() (在 turtle 模块中)

fdatasync() (在 os 模块中)

fdopen() (在 os 模块中)

Feature (msilib 中的类)

feature_external_ges() (在 xml.sax.handler 模块中)

feature_external_pes() (在 xml.sax.handler 模块中)

feature_namespace_prefixes() (在 xml.sax.handler 模块中)

feature_namespaces() (在 xml.sax.handler 模块中)

feature_string_interning() (在 xml.sax.handler 模块中)

feature_validation() (在 xml.sax.handler 模块中)

feed() (email.parser.BytesFeedParser 方法)

(html.parser.HTMLParser 方法)

(xml.etree.ElementTree.XMLParser 方法)

(xml.etree.ElementTree.XMLPullParser 方法)

(xml.sax.xmlreader.IncrementalParser 方法)

feed_data() (asyncio.StreamReader 方法)

feed_eof() (asyncio.StreamReader 方法)

FeedParser (email.parser 中的类)

fetch() (imaplib.IMAP4 方法)

Fetch() (msilib.View 方法)

fetchall() (sqlite3.Cursor 方法)

fetchmany() (sqlite3.Cursor 方法)

fetchone() (sqlite3.Cursor 方法)

fflags (select.kevent 属性)

field_size_limit() (在 csv 模块中)

fieldnames (csv.csvreader 属性)

fields (uuid.UUID 属性)

file

.ini

.pdbrc

byte-code, [1]

configuration

copying

debugger configuration

large files

mime.types

modes

path configuration

plist

temporary

对象, [1]

file (pyclbr.Class 属性)

(pyclbr.Function 属性)

file ...

compileall command line option

file control

UNIX

file name

temporary

file object

io module

open() built-in function

file object -- 文件对象

file-like object -- 文件类对象

FILE_ATTRIBUTE_ARCHIVE() (在 stat 模块中)

FILE_ATTRIBUTE_COMPRESSED() (在 stat 模块中)

FILE_ATTRIBUTE_DEVICE() (在 stat 模块中)

FILE_ATTRIBUTE_DIRECTORY() (在 stat 模块中)

FILE_ATTRIBUTE_ENCRYPTED() (在 stat 模块中)

FILE_ATTRIBUTE_HIDDEN() (在 stat 模块中)

FILE_ATTRIBUTE_INTEGRITY_STREAM() (在 stat 模块中)

FILE_ATTRIBUTE_NO_SCRUB_DATA() (在 stat 模块中)

FILE_ATTRIBUTE_NORMAL() (在 stat 模块中)

FILE_ATTRIBUTE_NOT_CONTENT_INDEXED() (在 stat 模块中)

FILE_ATTRIBUTE_OFFLINE() (在 stat 模块中)

FILE_ATTRIBUTE_READONLY() (在 stat 模块中)

FILE_ATTRIBUTE_REPARSE_POINT() (在 stat 模块中)

FILE_ATTRIBUTE_SPARSE_FILE() (在 stat 模块中)

FILE_ATTRIBUTE_SYSTEM() (在 stat 模块中)

FILE_ATTRIBUTE_TEMPORARY() (在 stat 模块中)

FILE_ATTRIBUTE_VIRTUAL() (在 stat 模块中)

file_created() (內置函数)

file_dispatcher (asyncore 中的类)

file_open() (urllib.request.FileHandler 方法)

file_size (zipfile.ZipInfo 属性)

file_wrapper (asyncore 中的类)

filecmp (模块)

fileConfig() (在 logging.config 模块中)

FileCookieJar (http.cookiejar 中的类)

FileEntry (tkinter.tix 中的类)

FileExistsError

FileFinder (importlib.machinery 中的类)

FileHandler (logging 中的类)

(urllib.request 中的类)

FileInput (fileinput 中的类)

fileinput (模块)

FileIO (io 中的类)

filelineno() (在 fileinput 模块中)

FileLoader (importlib.abc 中的类)

filemode() (在 stat 模块中)

filename (doctest.DocTest 属性)

(http.cookiejar.FileCookieJar 属性)

(OSError 属性)

(traceback.TracebackException 属性)

(tracemalloc.Frame 属性)

(zipfile.ZipFile 属性)

(zipfile.ZipInfo 属性)

filename() (在 fileinput 模块中)

filename2 (OSError 属性)

filename_only() (在 tabnanny 模块中)

filename_pattern (tracemalloc.Filter 属性)

filenames

pathname expansion

wildcard expansion

fileno() (http.client.HTTPResponse 方法)

(io.IOBase 方法)

(multiprocessing.connection.Connection 方法)

(ossaudiodev.oss_audio_device 方法)

(ossaudiodev.oss_mixer_device 方法)

(select.devpoll 方法)

(select.epoll 方法)

(select.kqueue 方法)

(selectors.DevpollSelector 方法)

(selectors.EpollSelector 方法)

(selectors.KqueueSelector 方法)

(socket.socket 方法)

(socketserver.BaseServer 方法)

(telnetlib.Telnet 方法)

(在 fileinput 模块中)

FileNotFoundError

fileobj (selectors.SelectorKey 属性)

FileSelectBox (tkinter.tix 中的类)

FileType (argparse 中的类)

FileWrapper (wsgiref.util 中的类)

fill() (textwrap.TextWrapper 方法)

(在 textwrap 模块中)

fillcolor() (在 turtle 模块中)

filling() (在 turtle 模块中)

filter (2to3 fixer)

Filter (logging 中的类)

filter (select.kevent 属性)

Filter (tracemalloc 中的类)

filter() (logging.Filter 方法)

(logging.Handler 方法)

(logging.Logger 方法)

(內置函数)

(在 curses 模块中)

(在 fnmatch 模块中)

FILTER_DIR() (在 unittest.mock 模块中)

filter_traces() (tracemalloc.Snapshot 方法)

filterfalse() (在 itertools 模块中)

filterwarnings() (在 warnings 模块中)

finalization, of objects

finalize (weakref 中的类)

finalize_options() (distutils.cmd.Command 方法)

finalizer

finally

关键字, [1], [2], [3], [4]

find() (bytearray 方法)

(bytes 方法)

(doctest.DocTestFinder 方法)

(mmap.mmap 方法)

(str 方法)

(xml.etree.ElementTree.Element 方法)

(xml.etree.ElementTree.ElementTree 方法)

(在 gettext 模块中)

find_class() (pickle protocol)

(pickle.Unpickler 方法)

find_library() (在 ctypes.util 模块中)

find_library_file() (distutils.ccompiler.CCompiler 方法)

find_loader() (importlib.abc.PathEntryFinder 方法)

(importlib.machinery.FileFinder 方法)

(在 importlib 模块中)

(在 pkgutil 模块中)

find_longest_match() (difflib.SequenceMatcher 方法)

find_module() (imp.NullImporter 方法)

(importlib.abc.Finder 方法)

(importlib.abc.MetaPathFinder 方法)

(importlib.abc.PathEntryFinder 方法)

(importlib.machinery.PathFinder 类方法)

(zipimport.zipimporter 方法)

(在 imp 模块中)

find_msvcrt() (在 ctypes.util 模块中)

find_spec

finder

find_spec() (importlib.abc.MetaPathFinder 方法)

(importlib.abc.PathEntryFinder 方法)

(importlib.machinery.FileFinder 方法)

(importlib.machinery.PathFinder 类方法)

(在 importlib.util 模块中)

find_unused_port() (在 test.support 模块中)

find_user_password() (urllib.request.HTTPPasswordMgr 方法), [1]

findall() (re.regex 方法)

(xml.etree.ElementTree.Element 方法)

(xml.etree.ElementTree.ElementTree 方法)

(在 re 模块中)

findCaller() (logging.Logger 方法)

finder

find_spec

Finder (importlib.abc 中的类)

finder -- 查找器

findfactor() (在 audioop 模块中)

findfile() (在 test.support 模块中)

findfit() (在 audioop 模块中)

finditer() (re.regex 方法)

(在 re 模块中)

findlabels() (在 dis 模块中)

findlinestarts() (在 dis 模块中)

findmatch() (在 mailcap 模块中)

findmax() (在 audioop 模块中)

findtext() (xml.etree.ElementTree.Element 方法)

(xml.etree.ElementTree.ElementTree 方法)

finish() (socketserver.BaseRequestHandler 方法)

finish_request() (socketserver.BaseServer 方法)

firstChild (xml.dom.Node 属性)

firstkey() (dbm.gnu.gdbm 方法)

firstweekday() (在 calendar 模块中)

fix_missing_locations() (在 ast 模块中)

fix_sentence_endings (textwrap.TextWrapper 属性)

Flag (enum 中的类)

flag_bits (zipfile.ZipInfo 属性)

flags (re.regex 属性)

(select.kevent 属性)

flags() (在 sys 模块中)

flash() (在 curses 模块中)

flatten() (email.generator.BytesGenerator 方法)

(email.generator.Generator 方法)

flattening

objects

float

內置函数, [1], [2]

float (內置类)

float_info() (在 sys 模块中)

float_repr_style() (在 sys 模块中)

floating point

literals

number

对象, [1], [2]

floating point literal

FloatingPointError, [1]

FloatOperation (decimal 中的类)

flock() (在 fcntl 模块中)

floor division -- 向下取整除法

floor() (in module math)

(在 math 模块中)

floordiv() (在 operator 模块中)

flush() (bz2.BZ2Compressor 方法)

(formatter.writer 方法)

(io.BufferedWriter 方法)

(io.IOBase 方法)

(logging.Handler 方法)

(logging.handlers.BufferingHandler 方法)

(logging.handlers.MemoryHandler 方法)

(logging.StreamHandler 方法)

(lzma.LZMACompressor 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(mailbox.MH 方法)

(mmap.mmap 方法)

(zlib.Compress 方法)

(zlib.Decompress 方法)

flush_headers() (http.server.BaseHTTPRequestHandler 方法)

flush_softspace() (formatter.formatter 方法)

flushinp() (在 curses 模块中)

FlushKey() (在 winreg 模块中)

fma() (decimal.Context 方法)

(decimal.Decimal 方法)

fmod() (在 math 模块中)

FMT_BINARY() (在 plistlib 模块中)

FMT_XML() (在 plistlib 模块中)

fnmatch (模块)

fnmatch() (在 fnmatch 模块中)

fnmatchcase() (在 fnmatch 模块中)

focus() (tkinter.ttk.Treeview 方法)

fold (datetime.datetime 属性)

(datetime.time 属性)

fold() (email.headerregistry.BaseHeader 方法)

(email.policy.Compat32 方法)

(email.policy.EmailPolicy 方法)

(email.policy.Policy 方法)

fold_binary() (email.policy.Compat32 方法)

(email.policy.EmailPolicy 方法)

(email.policy.Policy 方法)

for

in comprehensions

语句, [1], [2], [3]

FOR_ITER (opcode)

forget() (tkinter.ttk.Notebook 方法)

(在 test.support 模块中)

fork() (在 os 模块中)

(在 pty 模块中)

ForkingMixIn (socketserver 中的类)

ForkingTCPServer (socketserver 中的类)

ForkingUDPServer (socketserver 中的类)

forkpty() (在 os 模块中)

form

lambda

Form (tkinter.tix 中的类)

format (memoryview 属性)

(struct.Struct 属性)

format() (built-in function)

__str__() (object method)

format() (logging.Formatter 方法)

(logging.Handler 方法)

(pprint.PrettyPrinter 方法)

(str 方法)

(string.Formatter 方法)

(traceback.StackSummary 方法)

(traceback.TracebackException 方法)

(tracemalloc.Traceback 方法)

(內置函数)

(在 locale 模块中)

format_datetime() (在 email.utils 模块中)

format_exc() (在 traceback 模块中)

format_exception() (在 traceback 模块中)

format_exception_only() (traceback.TracebackException 方法)

(在 traceback 模块中)

format_field() (string.Formatter 方法)

format_help() (argparse.ArgumentParser 方法)

format_list() (在 traceback 模块中)

format_map() (str 方法)

format_stack() (在 traceback 模块中)

format_stack_entry() (bdb.Bdb 方法)

format_string() (在 locale 模块中)

format_tb() (在 traceback 模块中)

format_usage() (argparse.ArgumentParser 方法)

FORMAT_VALUE (opcode)

formataddr() (在 email.utils 模块中)

formatargspec() (在 inspect 模块中)

formatargvalues() (在 inspect 模块中)

formatdate() (在 email.utils 模块中)

FormatError

FormatError() (在 ctypes 模块中)

formatException() (logging.Formatter 方法)

formatmonth() (calendar.HTMLCalendar 方法)

(calendar.TextCalendar 方法)

formatStack() (logging.Formatter 方法)

formatted string literal

Formatter (logging 中的类)

(string 中的类)

formatter (模块)

formatTime() (logging.Formatter 方法)

formatting

bytearray (%)

bytes (%)

formatting, string (%)

formatwarning() (在 warnings 模块中)

formatyear() (calendar.HTMLCalendar 方法)

(calendar.TextCalendar 方法)

formatyearpage() (calendar.HTMLCalendar 方法)

Fortran contiguous, [1]

forward() (在 turtle 模块中)

found_terminator() (asynchat.async_chat 方法)

fpathconf() (在 os 模块中)

fpectl (模块)

fqdn (smtpd.SMTPChannel 属性)

Fraction (fractions 中的类)

fractions (模块)

frame

execution, [1]

对象

frame (tkinter.scrolledtext.ScrolledText 属性)

Frame (tracemalloc 中的类)

FrameSummary (traceback 中的类)

FrameType() (在 types 模块中)

free

variable

free()

freeze utility

freeze_support() (在 multiprocessing 模块中)

frexp() (在 math 模块中)

from

import statement, [1]

yield from expression

关键字, [1]

from_address() (ctypes._CData 方法)

from_buffer() (ctypes._CData 方法)

from_buffer_copy() (ctypes._CData 方法)

from_bytes() (int 类方法)

from_callable() (inspect.Signature 类方法)

from_decimal() (fractions.Fraction 方法)

from_exception() (traceback.TracebackException 类方法)

from_file() (zipfile.ZipInfo 类方法)

from_float() (decimal.Decimal 方法)

(fractions.Fraction 方法)

from_iterable() (itertools.chain 类方法)

from_list() (traceback.StackSummary 类方法)

from_param() (ctypes._CData 方法)

from_traceback() (dis.Bytecode 类方法)

frombuf() (tarfile.TarInfo 类方法)

frombytes() (array.array 方法)

fromfd() (select.epoll 方法)

(select.kqueue 方法)

(在 socket 模块中)

fromfile() (array.array 方法)

fromhex() (bytearray 类方法)

(bytes 类方法)

(float 类方法)

fromkeys() (collections.Counter 方法)

(dict 类方法)

fromlist() (array.array 方法)

fromordinal() (datetime.date 类方法)

(datetime.datetime 类方法)

fromshare() (在 socket 模块中)

fromstring() (array.array 方法)

(在 xml.etree.ElementTree 模块中)

fromstringlist() (在 xml.etree.ElementTree 模块中)

fromtarfile() (tarfile.TarInfo 类方法)

fromtimestamp() (datetime.date 类方法)

(datetime.datetime 类方法)

fromunicode() (array.array 方法)

fromutc() (datetime.timezone 方法)

(datetime.tzinfo 方法)

FrozenImporter (importlib.machinery 中的类)

frozenset

对象, [1]

FrozenSet (typing 中的类)

frozenset (內置类)

fsdecode() (在 os 模块中)

fsencode() (在 os 模块中)

fspath() (在 os 模块中)

fstat() (在 os 模块中)

fstatvfs() (在 os 模块中)

fsum() (在 math 模块中)

fsync() (在 os 模块中)

FTP

ftplib (standard module)

protocol, [1]

FTP (ftplib 中的类)

ftp_open() (urllib.request.FTPHandler 方法)

FTP_TLS (ftplib 中的类)

FTPHandler (urllib.request 中的类)

ftplib (模块)

ftruncate() (在 os 模块中)

Full

full() (asyncio.Queue 方法)

(multiprocessing.Queue 方法)

(queue.Queue 方法)

full_url (urllib.request.Request 属性)

fullmatch() (re.regex 方法)

(在 re 模块中)

func (functools.partial 属性)

funcattrs (2to3 fixer)

function

annotations, [1]

anonymous

argument

call, [1], [2]

call, user-defined

definition, [1]

generator, [1]

name, [1]

user-defined

对象, [1], [2], [3], [4], [5]

Function (symtable 中的类)

function -- 函数

function annotation -- 函数标注

function__entry (C 函数)

function__return (C 函数)

FunctionTestCase (unittest 中的类)

FunctionType() (在 types 模块中)

functools (模块)

funny_files (filecmp.dircmp 属性)

future

statement

future (2to3 fixer)

Future (asyncio 中的类)

(concurrent.futures 中的类)

FutureWarning

fwalk() (在 os 模块中)

G

G.722

gaierror

gamma() (在 math 模块中)

gammavariate() (在 random 模块中)

garbage collection

garbage collection -- 垃圾回收

garbage() (在 gc 模块中)

gather() (curses.textpad.Textbox 方法)

(在 asyncio 模块中)

gauss() (在 random 模块中)

gc (模块)

gc__done (C 函数)

gc__start (C 函数)

gcd() (在 fractions 模块中)

(在 math 模块中)

ge() (在 operator 模块中)

gen_lib_options() (在 distutils.ccompiler 模块中)

gen_preprocess_options() (在 distutils.ccompiler 模块中)

gen_uuid() (在 msilib 模块中)

generate_help() (distutils.fancy_getopt.FancyGetopt 方法)

generator

expression

function, [1], [2]

iterator, [1]

对象, [1], [2]

Generator (collections.abc 中的类)

(email.generator 中的类)

(typing 中的类)

generator -- 生成器

generator expression

generator expression -- 生成器表达式

generator iterator -- 生成器迭代器

GeneratorExit

例外, [1]

GeneratorType() (在 types 模块中)

generic

special attribute

Generic (typing 中的类)

generic function -- 泛型函数

generic_visit() (ast.NodeVisitor 方法)

genops() (在 pickletools 模块中)

get() (asyncio.Queue 方法)

(configparser.ConfigParser 方法)

(dict 方法)

(email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(multiprocessing.pool.AsyncResult 方法)

(multiprocessing.Queue 方法)

(multiprocessing.SimpleQueue 方法)

(ossaudiodev.oss_mixer_device 方法)

(queue.Queue 方法)

(tkinter.ttk.Combobox 方法)

(types.MappingProxyType 方法)

(xml.etree.ElementTree.Element 方法)

(在 webbrowser 模块中)

GET_AITER (opcode)

get_all() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(wsgiref.headers.Headers 方法)

get_all_breaks() (bdb.Bdb 方法)

get_all_start_methods() (在 multiprocessing 模块中)

GET_ANEXT (opcode)

get_app() (wsgiref.simple_server.WSGIServer 方法)

get_archive_formats() (在 shutil 模块中)

get_asyncgen_hooks() (在 sys 模块中)

GET_AWAITABLE (opcode)

get_begidx() (在 readline 模块中)

get_blocking() (在 os 模块中)

get_body() (email.message.EmailMessage 方法)

get_body_encoding() (email.charset.Charset 方法)

get_boundary() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_bpbynumber() (bdb.Bdb 方法)

get_break() (bdb.Bdb 方法)

get_breaks() (bdb.Bdb 方法)

get_buffer() (xdrlib.Packer 方法)

(xdrlib.Unpacker 方法)

get_bytes() (mailbox.Mailbox 方法)

get_ca_certs() (ssl.SSLContext 方法)

get_cache_token() (在 abc 模块中)

get_channel_binding() (ssl.SSLSocket 方法)

get_charset() (email.message.Message 方法)

get_charsets() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_children() (symtable.SymbolTable 方法)

(tkinter.ttk.Treeview 方法)

get_ciphers() (ssl.SSLContext 方法)

get_clock_info() (在 time 模块中)

get_close_matches() (在 difflib 模块中)

get_code() (importlib.abc.InspectLoader 方法)

(importlib.abc.SourceLoader 方法)

(importlib.machinery.ExtensionFileLoader 方法)

(importlib.machinery.SourcelessFileLoader 方法)

(zipimport.zipimporter 方法)

get_completer() (在 readline 模块中)

get_completer_delims() (在 readline 模块中)

get_completion_type() (在 readline 模块中)

get_config_h_filename() (在 distutils.sysconfig 模块中)

(在 sysconfig 模块中)

get_config_var() (在 distutils.sysconfig 模块中)

(在 sysconfig 模块中)

get_config_vars() (在 distutils.sysconfig 模块中)

(在 sysconfig 模块中)

get_content() (email.contentmanager.ContentManager 方法)

(email.message.EmailMessage 方法)

(在 email.contentmanager 模块中)

get_content_charset() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_content_disposition() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_content_maintype() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_content_subtype() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_content_type() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_context() (在 multiprocessing 模块中)

get_coroutine_wrapper() (在 sys 模块中)

get_count() (在 gc 模块中)

get_current_history_length() (在 readline 模块中)

get_data() (importlib.abc.FileLoader 方法)

(importlib.abc.ResourceLoader 方法)

(zipimport.zipimporter 方法)

(在 pkgutil 模块中)

get_date() (mailbox.MaildirMessage 方法)

get_debug() (asyncio.AbstractEventLoop 方法)

(在 gc 模块中)

get_default() (argparse.ArgumentParser 方法)

get_default_compiler() (在 distutils.ccompiler 模块中)

get_default_domain() (在 nis 模块中)

get_default_type() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_default_verify_paths() (在 ssl 模块中)

get_dialect() (在 csv 模块中)

get_docstring() (在 ast 模块中)

get_doctest() (doctest.DocTestParser 方法)

get_endidx() (在 readline 模块中)

get_environ() (wsgiref.simple_server.WSGIRequestHandler 方法)

get_errno() (在 ctypes 模块中)

get_event_loop() (asyncio.AbstractEventLoopPolicy 方法)

(在 asyncio 模块中)

get_event_loop_policy() (在 asyncio 模块中)

get_examples() (doctest.DocTestParser 方法)

get_exception_handler() (asyncio.AbstractEventLoop 方法)

get_exec_path() (在 os 模块中)

get_extra_info() (asyncio.BaseTransport 方法)

(asyncio.StreamWriter 方法)

get_field() (string.Formatter 方法)

get_file() (mailbox.Babyl 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(mailbox.mbox 方法)

(mailbox.MH 方法)

(mailbox.MMDF 方法)

get_file_breaks() (bdb.Bdb 方法)

get_filename() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(importlib.abc.ExecutionLoader 方法)

(importlib.abc.FileLoader 方法)

(importlib.machinery.ExtensionFileLoader 方法)

(zipimport.zipimporter 方法)

get_flags() (mailbox.MaildirMessage 方法)

(mailbox.mboxMessage 方法)

(mailbox.MMDFMessage 方法)

get_folder() (mailbox.Maildir 方法)

(mailbox.MH 方法)

get_frees() (symtable.Function 方法)

get_from() (mailbox.mboxMessage 方法)

(mailbox.MMDFMessage 方法)

get_full_url() (urllib.request.Request 方法)

get_globals() (symtable.Function 方法)

get_grouped_opcodes() (difflib.SequenceMatcher 方法)

get_handle_inheritable() (在 os 模块中)

get_header() (urllib.request.Request 方法)

get_history_item() (在 readline 模块中)

get_history_length() (在 readline 模块中)

get_id() (symtable.SymbolTable 方法)

get_ident() (在 _thread 模块中)

(在 threading 模块中)

get_identifiers() (symtable.SymbolTable 方法)

get_importer() (在 pkgutil 模块中)

get_info() (mailbox.MaildirMessage 方法)

get_inheritable() (socket.socket 方法)

(在 os 模块中)

get_instructions() (在 dis 模块中)

get_interpreter() (在 zipapp 模块中)

GET_ITER (opcode)

get_key() (selectors.BaseSelector 方法)

get_labels() (mailbox.Babyl 方法)

(mailbox.BabylMessage 方法)

get_last_error() (在 ctypes 模块中)

get_line_buffer() (在 readline 模块中)

get_lineno() (symtable.SymbolTable 方法)

get_loader() (在 pkgutil 模块中)

get_locals() (symtable.Function 方法)

get_logger() (在 multiprocessing 模块中)

get_magic() (在 imp 模块中)

get_makefile_filename() (在 distutils.sysconfig 模块中)

(在 sysconfig 模块中)

get_map() (selectors.BaseSelector 方法)

get_matching_blocks() (difflib.SequenceMatcher 方法)

get_message() (mailbox.Mailbox 方法)

get_method() (urllib.request.Request 方法)

get_methods() (symtable.Class 方法)

get_mixed_type_key() (在 ipaddress 模块中)

get_name() (symtable.Symbol 方法)

(symtable.SymbolTable 方法)

get_namespace() (symtable.Symbol 方法)

get_namespaces() (symtable.Symbol 方法)

get_nonstandard_attr() (http.cookiejar.Cookie 方法)

get_nowait() (asyncio.Queue 方法)

(multiprocessing.Queue 方法)

(queue.Queue 方法)

get_object_traceback() (在 tracemalloc 模块中)

get_objects() (在 gc 模块中)

get_opcodes() (difflib.SequenceMatcher 方法)

get_option() (optparse.OptionParser 方法)

get_option_group() (optparse.OptionParser 方法)

get_option_order() (distutils.fancy_getopt.FancyGetopt 方法)

get_osfhandle() (在 msvcrt 模块中)

get_output_charset() (email.charset.Charset 方法)

get_param() (email.message.Message 方法)

get_parameters() (symtable.Function 方法)

get_params() (email.message.Message 方法)

get_path() (在 sysconfig 模块中)

get_path_names() (在 sysconfig 模块中)

get_paths() (在 sysconfig 模块中)

get_payload() (email.message.Message 方法)

get_pid() (asyncio.BaseSubprocessTransport 方法)

get_pipe_transport() (asyncio.BaseSubprocessTransport 方法)

get_platform() (在 distutils.util 模块中)

(在 sysconfig 模块中)

get_poly() (在 turtle 模块中)

get_position() (xdrlib.Unpacker 方法)

get_protocol() (asyncio.BaseTransport 方法)

get_python_inc() (在 distutils.sysconfig 模块中)

get_python_lib() (在 distutils.sysconfig 模块中)

get_python_version() (在 sysconfig 模块中)

get_recsrc() (ossaudiodev.oss_mixer_device 方法)

get_referents() (在 gc 模块中)

get_referrers() (在 gc 模块中)

get_request() (socketserver.BaseServer 方法)

get_returncode() (asyncio.BaseSubprocessTransport 方法)

get_scheme() (wsgiref.handlers.BaseHandler 方法)

get_scheme_names() (在 sysconfig 模块中)

get_sequences() (mailbox.MH 方法)

(mailbox.MHMessage 方法)

get_server() (multiprocessing.managers.BaseManager 方法)

get_server_certificate() (在 ssl 模块中)

get_shapepoly() (在 turtle 模块中)

get_socket() (telnetlib.Telnet 方法)

get_source() (importlib.abc.InspectLoader 方法)

(importlib.abc.SourceLoader 方法)

(importlib.machinery.ExtensionFileLoader 方法)

(importlib.machinery.SourcelessFileLoader 方法)

(zipimport.zipimporter 方法)

get_special_folder_path() (內置函数)

get_stack() (asyncio.Task 方法)

(bdb.Bdb 方法)

get_start_method() (在 multiprocessing 模块中)

get_starttag_text() (html.parser.HTMLParser 方法)

get_stats() (在 gc 模块中)

get_stderr() (wsgiref.handlers.BaseHandler 方法)

(wsgiref.simple_server.WSGIRequestHandler 方法)

get_stdin() (wsgiref.handlers.BaseHandler 方法)

get_string() (mailbox.Mailbox 方法)

get_subdir() (mailbox.MaildirMessage 方法)

get_suffixes() (在 imp 模块中)

get_symbols() (symtable.SymbolTable 方法)

get_tag() (在 imp 模块中)

get_task_factory() (asyncio.AbstractEventLoop 方法)

get_terminal_size() (在 os 模块中)

(在 shutil 模块中)

get_terminator() (asynchat.async_chat 方法)

get_threshold() (在 gc 模块中)

get_token() (shlex.shlex 方法)

get_traceback_limit() (在 tracemalloc 模块中)

get_traced_memory() (在 tracemalloc 模块中)

get_tracemalloc_memory() (在 tracemalloc 模块中)

get_type() (symtable.SymbolTable 方法)

get_type_hints() (在 typing 模块中)

get_unixfrom() (email.message.EmailMessage 方法)

(email.message.Message 方法)

get_unpack_formats() (在 shutil 模块中)

get_usage() (optparse.OptionParser 方法)

get_value() (string.Formatter 方法)

get_version() (optparse.OptionParser 方法)

get_visible() (mailbox.BabylMessage 方法)

get_wch() (curses.window 方法)

get_write_buffer_limits() (asyncio.WriteTransport 方法)

get_write_buffer_size() (asyncio.WriteTransport 方法)

GET_YIELD_FROM_ITER (opcode)

getacl() (imaplib.IMAP4 方法)

getaddresses() (在 email.utils 模块中)

getaddrinfo() (asyncio.AbstractEventLoop 方法)

(在 socket 模块中)

getallocatedblocks() (在 sys 模块中)

getannotation() (imaplib.IMAP4 方法)

getargspec() (在 inspect 模块中)

getargvalues() (在 inspect 模块中)

getatime() (在 os.path 模块中)

getattr() (內置函数)

getattr_static() (在 inspect 模块中)

getAttribute() (xml.dom.Element 方法)

getAttributeNode() (xml.dom.Element 方法)

getAttributeNodeNS() (xml.dom.Element 方法)

getAttributeNS() (xml.dom.Element 方法)

GetBase() (xml.parsers.expat.xmlparser 方法)

getbegyx() (curses.window 方法)

getbkgd() (curses.window 方法)

getboolean() (configparser.ConfigParser 方法)

getbuffer() (io.BytesIO 方法)

getByteStream() (xml.sax.xmlreader.InputSource 方法)

getcallargs() (在 inspect 模块中)

getcanvas() (在 turtle 模块中)

getcapabilities() (nntplib.NNTP 方法)

getcaps() (在 mailcap 模块中)

getch() (curses.window 方法)

(在 msvcrt 模块中)

getCharacterStream() (xml.sax.xmlreader.InputSource 方法)

getche() (在 msvcrt 模块中)

getcheckinterval() (在 sys 模块中)

getChild() (logging.Logger 方法)

getchildren() (xml.etree.ElementTree.Element 方法)

getclasstree() (在 inspect 模块中)

getclosurevars() (在 inspect 模块中)

GetColumnInfo() (msilib.View 方法)

getColumnNumber() (xml.sax.xmlreader.Locator 方法)

getcomments() (在 inspect 模块中)

getcompname() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getcomptype() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getContentHandler() (xml.sax.xmlreader.XMLReader 方法)

getcontext() (在 decimal 模块中)

getcoroutinelocals() (在 inspect 模块中)

getcoroutinestate() (在 inspect 模块中)

getctime() (在 os.path 模块中)

getcwd() (在 os 模块中)

getcwdb() (在 os 模块中)

getcwdu (2to3 fixer)

getdecoder() (在 codecs 模块中)

getdefaultencoding() (在 sys 模块中)

getdefaultlocale() (在 locale 模块中)

getdefaulttimeout() (在 socket 模块中)

getdlopenflags() (在 sys 模块中)

getdoc() (在 inspect 模块中)

getDOMImplementation() (在 xml.dom 模块中)

getDTDHandler() (xml.sax.xmlreader.XMLReader 方法)

getEffectiveLevel() (logging.Logger 方法)

getegid() (在 os 模块中)

getElementsByTagName() (xml.dom.Document 方法)

(xml.dom.Element 方法)

getElementsByTagNameNS() (xml.dom.Document 方法)

(xml.dom.Element 方法)

getencoder() (在 codecs 模块中)

getEncoding() (xml.sax.xmlreader.InputSource 方法)

getEntityResolver() (xml.sax.xmlreader.XMLReader 方法)

getenv() (在 os 模块中)

getenvb() (在 os 模块中)

getErrorHandler() (xml.sax.xmlreader.XMLReader 方法)

geteuid() (在 os 模块中)

getEvent() (xml.dom.pulldom.DOMEventStream 方法)

getEventCategory() (logging.handlers.NTEventLogHandler 方法)

getEventType() (logging.handlers.NTEventLogHandler 方法)

getException() (xml.sax.SAXException 方法)

getFeature() (xml.sax.xmlreader.XMLReader 方法)

GetFieldCount() (msilib.Record 方法)

getfile() (在 inspect 模块中)

getfilesystemencodeerrors() (在 sys 模块中)

getfilesystemencoding() (在 sys 模块中)

getfirst() (cgi.FieldStorage 方法)

getfloat() (configparser.ConfigParser 方法)

getfmts() (ossaudiodev.oss_audio_device 方法)

getfqdn() (在 socket 模块中)

getframeinfo() (在 inspect 模块中)

getframerate() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getfullargspec() (在 inspect 模块中)

getgeneratorlocals() (在 inspect 模块中)

getgeneratorstate() (在 inspect 模块中)

getgid() (在 os 模块中)

getgrall() (在 grp 模块中)

getgrgid() (在 grp 模块中)

getgrnam() (在 grp 模块中)

getgrouplist() (在 os 模块中)

getgroups() (在 os 模块中)

getheader() (http.client.HTTPResponse 方法)

getheaders() (http.client.HTTPResponse 方法)

gethostbyaddr() (in module socket)

(在 socket 模块中)

gethostbyname() (在 socket 模块中)

gethostbyname_ex() (在 socket 模块中)

gethostname() (in module socket)

(在 socket 模块中)

getincrementaldecoder() (在 codecs 模块中)

getincrementalencoder() (在 codecs 模块中)

getinfo() (zipfile.ZipFile 方法)

getinnerframes() (在 inspect 模块中)

GetInputContext() (xml.parsers.expat.xmlparser 方法)

getint() (configparser.ConfigParser 方法)

GetInteger() (msilib.Record 方法)

getitem() (在 operator 模块中)

getiterator() (xml.etree.ElementTree.Element 方法)

(xml.etree.ElementTree.ElementTree 方法)

getitimer() (在 signal 模块中)

getkey() (curses.window 方法)

GetLastError() (在 ctypes 模块中)

getLength() (xml.sax.xmlreader.Attributes 方法)

getLevelName() (在 logging 模块中)

getline() (在 linecache 模块中)

getLineNumber() (xml.sax.xmlreader.Locator 方法)

getlist() (cgi.FieldStorage 方法)

getloadavg() (在 os 模块中)

getlocale() (在 locale 模块中)

getLogger() (在 logging 模块中)

getLoggerClass() (在 logging 模块中)

getlogin() (在 os 模块中)

getLogRecordFactory() (在 logging 模块中)

getmark() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getmarkers() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getmaxyx() (curses.window 方法)

getmember() (tarfile.TarFile 方法)

getmembers() (tarfile.TarFile 方法)

(在 inspect 模块中)

getMessage() (logging.LogRecord 方法)

(xml.sax.SAXException 方法)

getMessageID() (logging.handlers.NTEventLogHandler 方法)

getmodule() (在 inspect 模块中)

getmodulename() (在 inspect 模块中)

getmouse() (在 curses 模块中)

getmro() (在 inspect 模块中)

getmtime() (在 os.path 模块中)

getname() (chunk.Chunk 方法)

getName() (threading.Thread 方法)

getNameByQName() (xml.sax.xmlreader.AttributesNS 方法)

getnameinfo() (asyncio.AbstractEventLoop 方法)

(在 socket 模块中)

getnames() (tarfile.TarFile 方法)

getNames() (xml.sax.xmlreader.Attributes 方法)

getnchannels() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getnframes() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getnode

getnode() (在 uuid 模块中)

getopt (模块)

getopt() (distutils.fancy_getopt.FancyGetopt 方法)

(在 getopt 模块中)

GetoptError

getouterframes() (在 inspect 模块中)

getoutput() (在 subprocess 模块中)

getpagesize() (在 resource 模块中)

getparams() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getparyx() (curses.window 方法)

getpass (模块)

getpass() (在 getpass 模块中)

GetPassWarning

getpeercert() (ssl.SSLSocket 方法)

getpeername() (socket.socket 方法)

getpen() (在 turtle 模块中)

getpgid() (在 os 模块中)

getpgrp() (在 os 模块中)

getpid() (在 os 模块中)

getpos() (html.parser.HTMLParser 方法)

getppid() (在 os 模块中)

getpreferredencoding() (在 locale 模块中)

getpriority() (在 os 模块中)

getprofile() (在 sys 模块中)

GetProperty() (msilib.SummaryInformation 方法)

getProperty() (xml.sax.xmlreader.XMLReader 方法)

GetPropertyCount() (msilib.SummaryInformation 方法)

getprotobyname() (在 socket 模块中)

getproxies() (在 urllib.request 模块中)

getPublicId() (xml.sax.xmlreader.InputSource 方法)

(xml.sax.xmlreader.Locator 方法)

getpwall() (在 pwd 模块中)

getpwnam() (在 pwd 模块中)

getpwuid() (在 pwd 模块中)

getQNameByName() (xml.sax.xmlreader.AttributesNS 方法)

getQNames() (xml.sax.xmlreader.AttributesNS 方法)

getquota() (imaplib.IMAP4 方法)

getquotaroot() (imaplib.IMAP4 方法)

getrandbits() (在 random 模块中)

getrandom() (在 os 模块中)

getreader() (在 codecs 模块中)

getrecursionlimit() (在 sys 模块中)

getrefcount() (在 sys 模块中)

getresgid() (在 os 模块中)

getresponse() (http.client.HTTPConnection 方法)

getresuid() (在 os 模块中)

getrlimit() (在 resource 模块中)

getroot() (xml.etree.ElementTree.ElementTree 方法)

getrusage() (在 resource 模块中)

getsample() (在 audioop 模块中)

getsampwidth() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

getscreen() (在 turtle 模块中)

getservbyname() (在 socket 模块中)

getservbyport() (在 socket 模块中)

GetSetDescriptorType() (在 types 模块中)

getshapes() (在 turtle 模块中)

getsid() (在 os 模块中)

getsignal() (在 signal 模块中)

getsitepackages() (在 site 模块中)

getsize() (chunk.Chunk 方法)

(在 os.path 模块中)

getsizeof() (在 sys 模块中)

getsockname() (socket.socket 方法)

getsockopt() (socket.socket 方法)

getsource() (在 inspect 模块中)

getsourcefile() (在 inspect 模块中)

getsourcelines() (在 inspect 模块中)

getspall() (在 spwd 模块中)

getspnam() (在 spwd 模块中)

getstate() (codecs.IncrementalDecoder 方法)

(codecs.IncrementalEncoder 方法)

(在 random 模块中)

getstatusoutput() (在 subprocess 模块中)

getstr() (curses.window 方法)

GetString() (msilib.Record 方法)

getSubject() (logging.handlers.SMTPHandler 方法)

GetSummaryInformation() (msilib.Database 方法)

getswitchinterval() (在 sys 模块中)

getSystemId() (xml.sax.xmlreader.InputSource 方法)

(xml.sax.xmlreader.Locator 方法)

getsyx() (在 curses 模块中)

gettarinfo() (tarfile.TarFile 方法)

gettempdir() (在 tempfile 模块中)

gettempdirb() (在 tempfile 模块中)

gettempprefix() (在 tempfile 模块中)

gettempprefixb() (在 tempfile 模块中)

getTestCaseNames() (unittest.TestLoader 方法)

gettext (模块)

gettext() (gettext.GNUTranslations 方法)

(gettext.NullTranslations 方法)

(在 gettext 模块中)

(在 locale 模块中)

gettimeout() (socket.socket 方法)

gettrace() (在 sys 模块中)

getturtle() (在 turtle 模块中)

getType() (xml.sax.xmlreader.Attributes 方法)

getuid() (在 os 模块中)

geturl() (urllib.parse.urllib.parse.SplitResult 方法)

getuser() (在 getpass 模块中)

getuserbase() (在 site 模块中)

getusersitepackages() (在 site 模块中)

getvalue() (io.BytesIO 方法)

(io.StringIO 方法)

getValue() (xml.sax.xmlreader.Attributes 方法)

getValueByQName() (xml.sax.xmlreader.AttributesNS 方法)

getwch() (在 msvcrt 模块中)

getwche() (在 msvcrt 模块中)

getweakrefcount() (在 weakref 模块中)

getweakrefs() (在 weakref 模块中)

getwelcome() (ftplib.FTP 方法)

(nntplib.NNTP 方法)

(poplib.POP3 方法)

getwin() (在 curses 模块中)

getwindowsversion() (在 sys 模块中)

getwriter() (在 codecs 模块中)

getxattr() (在 os 模块中)

getyx() (curses.window 方法)

gid (tarfile.TarInfo 属性)

GIL

glob

模块

glob (模块)

glob() (msilib.Directory 方法)

(pathlib.Path 方法)

(在 glob 模块中)

global

name binding

namespace

语句, [1]

global interpreter lock

global interpreter lock -- 全局解释器锁

globals() (內置函数)

globs (doctest.DocTest 属性)

gmtime() (在 time 模块中)

gname (tarfile.TarInfo 属性)

GNOME

GNU_FORMAT() (在 tarfile 模块中)

gnu_getopt() (在 getopt 模块中)

GNUTranslations (gettext 中的类)

got (doctest.DocTestFailure 属性)

goto() (在 turtle 模块中)

grammar

Graphical User Interface

GREATER() (在 token 模块中)

GREATEREQUAL() (在 token 模块中)

Greenwich Mean Time

GRND_NONBLOCK() (在 os 模块中)

GRND_RANDOM() (在 os 模块中)

Group (email.headerregistry 中的类)

group() (nntplib.NNTP 方法)

(pathlib.Path 方法)

(re.match 方法)

groupby() (在 itertools 模块中)

groupdict() (re.match 方法)

groupindex (re.regex 属性)

grouping

groups (email.headerregistry.AddressHeader 属性)

(re.regex 属性)

groups() (re.match 方法)

grp (模块)

gt() (在 operator 模块中)

guess_all_extensions() (mimetypes.MimeTypes 方法)

(在 mimetypes 模块中)

guess_extension() (mimetypes.MimeTypes 方法)

(在 mimetypes 模块中)

guess_scheme() (在 wsgiref.util 模块中)

guess_type() (mimetypes.MimeTypes 方法)

(在 mimetypes 模块中)

GUI

gzip (模块)

GzipFile (gzip 中的类)

H

halfdelay() (在 curses 模块中)

Handle (asyncio 中的类)

handle an exception

handle() (http.server.BaseHTTPRequestHandler 方法)

(logging.Handler 方法)

(logging.handlers.QueueListener 方法)

(logging.Logger 方法)

(logging.NullHandler 方法)

(socketserver.BaseRequestHandler 方法)

(wsgiref.simple_server.WSGIRequestHandler 方法)

handle_accept() (asyncore.dispatcher 方法)

handle_accepted() (asyncore.dispatcher 方法)

handle_charref() (html.parser.HTMLParser 方法)

handle_close() (asyncore.dispatcher 方法)

handle_comment() (html.parser.HTMLParser 方法)

handle_connect() (asyncore.dispatcher 方法)

handle_data() (html.parser.HTMLParser 方法)

handle_decl() (html.parser.HTMLParser 方法)

handle_defect() (email.policy.Policy 方法)

handle_endtag() (html.parser.HTMLParser 方法)

handle_entityref() (html.parser.HTMLParser 方法)

handle_error() (asyncore.dispatcher 方法)

(socketserver.BaseServer 方法)

handle_expect_100() (http.server.BaseHTTPRequestHandler 方法)

handle_expt() (asyncore.dispatcher 方法)

handle_one_request() (http.server.BaseHTTPRequestHandler 方法)

handle_pi() (html.parser.HTMLParser 方法)

handle_read() (asyncore.dispatcher 方法)

handle_request() (socketserver.BaseServer 方法)

(xmlrpc.server.CGIXMLRPCRequestHandler 方法)

handle_startendtag() (html.parser.HTMLParser 方法)

handle_starttag() (html.parser.HTMLParser 方法)

handle_timeout() (socketserver.BaseServer 方法)

handle_write() (asyncore.dispatcher 方法)

handleError() (logging.Handler 方法)

(logging.handlers.SocketHandler 方法)

handler

exception

Handler (logging 中的类)

handler() (在 cgitb 模块中)

harmonic_mean() (在 statistics 模块中)

HAS_ALPN() (在 ssl 模块中)

has_children() (symtable.SymbolTable 方法)

has_colors() (在 curses 模块中)

HAS_ECDH() (在 ssl 模块中)

has_exec() (symtable.SymbolTable 方法)

has_extn() (smtplib.SMTP 方法)

has_function() (distutils.ccompiler.CCompiler 方法)

has_header() (csv.Sniffer 方法)

(urllib.request.Request 方法)

has_ic() (在 curses 模块中)

has_il() (在 curses 模块中)

has_ipv6() (在 socket 模块中)

has_key (2to3 fixer)

has_key() (在 curses 模块中)

has_location (importlib.machinery.ModuleSpec 属性)

has_nonstandard_attr() (http.cookiejar.Cookie 方法)

HAS_NPN() (在 ssl 模块中)

has_option() (configparser.ConfigParser 方法)

(optparse.OptionParser 方法)

has_section() (configparser.ConfigParser 方法)

HAS_SNI() (在 ssl 模块中)

has_ticket (ssl.SSLSession 属性)

HAS_TLSv1_3() (在 ssl 模块中)

hasattr() (內置函数)

hasAttribute() (xml.dom.Element 方法)

hasAttributeNS() (xml.dom.Element 方法)

hasAttributes() (xml.dom.Node 方法)

hasChildNodes() (xml.dom.Node 方法)

hascompare() (在 dis 模块中)

hasconst() (在 dis 模块中)

hasFeature() (xml.dom.DOMImplementation 方法)

hasfree() (在 dis 模块中)

hash

內置函数, [1], [2], [3]

hash character

hash() (內置函数)

hash.block_size() (在 hashlib 模块中)

hash.digest_size() (在 hashlib 模块中)

hash_info() (在 sys 模块中)

hashable

Hashable (collections.abc 中的类)

(typing 中的类)

hashable -- 可哈希

hasHandlers() (logging.Logger 方法)

hashlib (模块)

hasjabs() (在 dis 模块中)

hasjrel() (在 dis 模块中)

haslocal() (在 dis 模块中)

hasname() (在 dis 模块中)

HAVE_ARGUMENT (opcode)

HAVE_THREADS() (在 decimal 模块中)

HCI_DATA_DIR() (在 socket 模块中)

HCI_FILTER() (在 socket 模块中)

HCI_TIME_STAMP() (在 socket 模块中)

head() (nntplib.NNTP 方法)

Header (email.header 中的类)

header_encode() (email.charset.Charset 方法)

header_encode_lines() (email.charset.Charset 方法)

header_encoding (email.charset.Charset 属性)

header_factory (email.policy.EmailPolicy 属性)

header_fetch_parse() (email.policy.Compat32 方法)

(email.policy.EmailPolicy 方法)

(email.policy.Policy 方法)

header_items() (urllib.request.Request 方法)

header_max_count() (email.policy.EmailPolicy 方法)

(email.policy.Policy 方法)

header_offset (zipfile.ZipInfo 属性)

header_source_parse() (email.policy.Compat32 方法)

(email.policy.EmailPolicy 方法)

(email.policy.Policy 方法)

header_store_parse() (email.policy.Compat32 方法)

(email.policy.EmailPolicy 方法)

(email.policy.Policy 方法)

HeaderError

HeaderParseError

HeaderParser (email.parser 中的类)

HeaderRegistry (email.headerregistry 中的类)

headers

MIME, [1]

headers (http.server.BaseHTTPRequestHandler 属性)

(urllib.error.HTTPError 属性)

Headers (wsgiref.headers 中的类)

headers (xmlrpc.client.ProtocolError 属性)

heading() (tkinter.ttk.Treeview 方法)

(在 turtle 模块中)

heapify() (在 heapq 模块中)

heapmin() (在 msvcrt 模块中)

heappop() (在 heapq 模块中)

heappush() (在 heapq 模块中)

heappushpop() (在 heapq 模块中)

heapq (模块)

heapreplace() (在 heapq 模块中)

helo() (smtplib.SMTP 方法)

help

online

內置函数

help (optparse.Option 属性)

(pdb command)

help() (nntplib.NNTP 方法)

(內置函数)

herror

hex (uuid.UUID 属性)

hex() (bytearray 方法)

(bytes 方法)

(float 方法)

(memoryview 方法)

(內置函数)

hexadecimal

literals

hexadecimal literal

hexbin() (在 binhex 模块中)

hexdigest() (hashlib.hash 方法)

(hashlib.shake 方法)

(hmac.HMAC 方法)

hexdigits() (在 string 模块中)

hexlify() (在 binascii 模块中)

hexversion() (在 sys 模块中)

hidden() (curses.panel.Panel 方法)

hide() (curses.panel.Panel 方法)

(tkinter.ttk.Notebook 方法)

hide_cookie2 (http.cookiejar.CookiePolicy 属性)

hideturtle() (在 turtle 模块中)

hierarchy

type

HierarchyRequestErr

HIGHEST_PROTOCOL() (在 pickle 模块中)

HKEY_CLASSES_ROOT() (在 winreg 模块中)

HKEY_CURRENT_CONFIG() (在 winreg 模块中)

HKEY_CURRENT_USER() (在 winreg 模块中)

HKEY_DYN_DATA() (在 winreg 模块中)

HKEY_LOCAL_MACHINE() (在 winreg 模块中)

HKEY_PERFORMANCE_DATA() (在 winreg 模块中)

HKEY_USERS() (在 winreg 模块中)

hline() (curses.window 方法)

HList (tkinter.tix 中的类)

hls_to_rgb() (在 colorsys 模块中)

hmac (模块)

HOME, [1], [2], [3], [4]

home() (pathlib.Path 类方法)

(在 turtle 模块中)

HOMEDRIVE, [1]

HOMEPATH, [1]

hook_compressed() (在 fileinput 模块中)

hook_encoded() (在 fileinput 模块中)

hooks

import

meta

path

host (urllib.request.Request 属性)

hostmask (ipaddress.IPv4Network 属性)

(ipaddress.IPv6Network 属性)

hosts (netrc.netrc 属性)

hosts() (ipaddress.IPv4Network 方法)

(ipaddress.IPv6Network 方法)

hour (datetime.datetime 属性)

(datetime.time 属性)

HRESULT (ctypes 中的类)

hStdError (subprocess.STARTUPINFO 属性)

hStdInput (subprocess.STARTUPINFO 属性)

hStdOutput (subprocess.STARTUPINFO 属性)

hsv_to_rgb() (在 colorsys 模块中)

ht() (在 turtle 模块中)

HTML, [1]

html (模块)

html.entities (模块)

html.parser (模块)

html5() (在 html.entities 模块中)

HTMLCalendar (calendar 中的类)

HtmlDiff (difflib 中的类)

HTMLParser (html.parser 中的类)

htonl() (在 socket 模块中)

htons() (在 socket 模块中)

HTTP

http (standard module)

http.client (standard module)

protocol, [1], [2], [3], [4], [5]

http (模块)

HTTP() (在 email.policy 模块中)

http.client (模块)

http.cookiejar (模块)

http.cookies (模块)

http.server (模块)

http_error_301() (urllib.request.HTTPRedirectHandler 方法)

http_error_302() (urllib.request.HTTPRedirectHandler 方法)

http_error_303() (urllib.request.HTTPRedirectHandler 方法)

http_error_307() (urllib.request.HTTPRedirectHandler 方法)

http_error_401() (urllib.request.HTTPBasicAuthHandler 方法)

(urllib.request.HTTPDigestAuthHandler 方法)

http_error_407() (urllib.request.ProxyBasicAuthHandler 方法)

(urllib.request.ProxyDigestAuthHandler 方法)

http_error_auth_reqed() (urllib.request.AbstractBasicAuthHandler 方法)

(urllib.request.AbstractDigestAuthHandler 方法)

http_error_default() (urllib.request.BaseHandler 方法)

http_error_nnn() (urllib.request.BaseHandler 方法)

http_open() (urllib.request.HTTPHandler 方法)

HTTP_PORT() (在 http.client 模块中)

http_proxy, [1], [2]

http_response() (urllib.request.HTTPErrorProcessor 方法)

http_version (wsgiref.handlers.BaseHandler 属性)

HTTPBasicAuthHandler (urllib.request 中的类)

HTTPConnection (http.client 中的类)

HTTPCookieProcessor (urllib.request 中的类)

httpd

HTTPDefaultErrorHandler (urllib.request 中的类)

HTTPDigestAuthHandler (urllib.request 中的类)

HTTPError

HTTPErrorProcessor (urllib.request 中的类)

HTTPException

HTTPHandler (logging.handlers 中的类)

(urllib.request 中的类)

HTTPPasswordMgr (urllib.request 中的类)

HTTPPasswordMgrWithDefaultRealm (urllib.request 中的类)

HTTPPasswordMgrWithPriorAuth (urllib.request 中的类)

HTTPRedirectHandler (urllib.request 中的类)

HTTPResponse (http.client 中的类)

https_open() (urllib.request.HTTPSHandler 方法)

HTTPS_PORT() (在 http.client 模块中)

https_response() (urllib.request.HTTPErrorProcessor 方法)

HTTPSConnection (http.client 中的类)

HTTPServer (http.server 中的类)

HTTPSHandler (urllib.request 中的类)

HTTPStatus (http 中的类)

hypot() (在 math 模块中)

I

I() (在 re 模块中)

I/O control

buffering, [1]

POSIX

tty

UNIX

iadd() (在 operator 模块中)

iand() (在 operator 模块中)

iconcat() (在 operator 模块中)

id

內置函数

id (ssl.SSLSession 属性)

id() (unittest.TestCase 方法)

(內置函数)

idcok() (curses.window 方法)

ident (select.kevent 属性)

(threading.Thread 属性)

identchars (cmd.Cmd 属性)

identifier, [1]

identify() (tkinter.ttk.Notebook 方法)

(tkinter.ttk.Treeview 方法)

(tkinter.ttk.Widget 方法)

identify_column() (tkinter.ttk.Treeview 方法)

identify_element() (tkinter.ttk.Treeview 方法)

identify_region() (tkinter.ttk.Treeview 方法)

identify_row() (tkinter.ttk.Treeview 方法)

identity

test

identity of an object

idioms (2to3 fixer)

IDLE, [1]

IDLESTARTUP, [1]

idlok() (curses.window 方法)

IEEE-754

if

conditional expression

in comprehensions

语句, [1]

if_indextoname() (在 socket 模块中)

if_nameindex() (在 socket 模块中)

if_nametoindex() (在 socket 模块中)

ifloordiv() (在 operator 模块中)

iglob() (在 glob 模块中)

ignorableWhitespace() (xml.sax.handler.ContentHandler 方法)

ignore (pdb command)

ignore_errors() (在 codecs 模块中)

IGNORE_EXCEPTION_DETAIL() (在 doctest 模块中)

ignore_patterns() (在 shutil 模块中)

IGNORECASE() (在 re 模块中)

ihave() (nntplib.NNTP 方法)

IISCGIHandler (wsgiref.handlers 中的类)

ilshift() (在 operator 模块中)

imag (numbers.Complex 属性)

imaginary literal

imap() (multiprocessing.pool.Pool 方法)

IMAP4

protocol

IMAP4 (imaplib 中的类)

IMAP4.abort

IMAP4.error

IMAP4.readonly

IMAP4_SSL

protocol

IMAP4_SSL (imaplib 中的类)

IMAP4_stream

protocol

IMAP4_stream (imaplib 中的类)

imap_unordered() (multiprocessing.pool.Pool 方法)

imaplib (模块)

imatmul() (在 operator 模块中)

imghdr (模块)

immedok() (curses.window 方法)

immutable

data type

object, [1]

sequence types

对象

immutable -- 不可变

immutable object

immutable sequence

对象

immutable types

subclassing

imod() (在 operator 模块中)

imp

模块

imp (模块)

ImpImporter (pkgutil 中的类)

implementation() (在 sys 模块中)

ImpLoader (pkgutil 中的类)

import

hooks

语句, [1], [2], [3], [4]

import (2to3 fixer)

import hooks

import machinery

import path -- 导入路径

import_fresh_module() (在 test.support 模块中)

IMPORT_FROM (opcode)

import_module() (在 importlib 模块中)

(在 test.support 模块中)

IMPORT_NAME (opcode)

IMPORT_STAR (opcode)

importer -- 导入器

ImportError

例外

importing -- 导入

importlib (模块)

importlib.abc (模块)

importlib.machinery (模块)

importlib.util (模块)

imports (2to3 fixer)

imports2 (2to3 fixer)

ImportWarning

ImproperConnectionState

imul() (在 operator 模块中)

in

关键字, [1]

运算符, [1], [2]

in_dll() (ctypes._CData 方法)

in_table_a1() (在 stringprep 模块中)

in_table_b1() (在 stringprep 模块中)

in_table_c11() (在 stringprep 模块中)

in_table_c11_c12() (在 stringprep 模块中)

in_table_c12() (在 stringprep 模块中)

in_table_c21() (在 stringprep 模块中)

in_table_c21_c22() (在 stringprep 模块中)

in_table_c22() (在 stringprep 模块中)

in_table_c3() (在 stringprep 模块中)

in_table_c4() (在 stringprep 模块中)

in_table_c5() (在 stringprep 模块中)

in_table_c6() (在 stringprep 模块中)

in_table_c7() (在 stringprep 模块中)

in_table_c8() (在 stringprep 模块中)

in_table_c9() (在 stringprep 模块中)

in_table_d1() (在 stringprep 模块中)

in_table_d2() (在 stringprep 模块中)

in_transaction (sqlite3.Connection 属性)

inch() (curses.window 方法)

inclusive

or

inclusive (tracemalloc.DomainFilter 属性)

(tracemalloc.Filter 属性)

Incomplete

IncompleteRead

IncompleteReadError

incr_item(), [1]

increment_lineno() (在 ast 模块中)

IncrementalDecoder (codecs 中的类)

incrementaldecoder (codecs.CodecInfo 属性)

IncrementalEncoder (codecs 中的类)

incrementalencoder (codecs.CodecInfo 属性)

IncrementalNewlineDecoder (io 中的类)

IncrementalParser (xml.sax.xmlreader 中的类)

indent (doctest.Example 属性)

INDENT token

indent() (在 textwrap 模块中)

INDENT() (在 token 模块中)

indentation

IndentationError

index operation

index() (array.array 方法)

(bytearray 方法)

(bytes 方法)

(collections.deque 方法)

(sequence method)

(str 方法)

(tkinter.ttk.Notebook 方法)

(tkinter.ttk.Treeview 方法)

(在 operator 模块中)

IndexError

indexOf() (在 operator 模块中)

IndexSizeErr

indices() (slice 方法)

inet_aton() (在 socket 模块中)

inet_ntoa() (在 socket 模块中)

inet_ntop() (在 socket 模块中)

inet_pton() (在 socket 模块中)

Inexact (decimal 中的类)

inf() (在 cmath 模块中)

(在 math 模块中)

infile

json.tool command line option

infile (shlex.shlex 属性)

Infinity

infj() (在 cmath 模块中)

info() (dis.Bytecode 方法)

(gettext.NullTranslations 方法)

(logging.Logger 方法)

(在 logging 模块中)

infolist() (zipfile.ZipFile 方法)

inheritance

ini file

init() (在 mimetypes 模块中)

init_color() (在 curses 模块中)

init_database() (在 msilib 模块中)

init_pair() (在 curses 模块中)

inited() (在 mimetypes 模块中)

initgroups() (在 os 模块中)

initial_indent (textwrap.TextWrapper 属性)

initialize_options() (distutils.cmd.Command 方法)

initscr() (在 curses 模块中)

inode() (os.DirEntry 方法)

INPLACE_ADD (opcode)

INPLACE_AND (opcode)

INPLACE_FLOOR_DIVIDE (opcode)

INPLACE_LSHIFT (opcode)

INPLACE_MATRIX_MULTIPLY (opcode)

INPLACE_MODULO (opcode)

INPLACE_MULTIPLY (opcode)

INPLACE_OR (opcode)

INPLACE_POWER (opcode)

INPLACE_RSHIFT (opcode)

INPLACE_SUBTRACT (opcode)

INPLACE_TRUE_DIVIDE (opcode)

INPLACE_XOR (opcode)

input

(2to3 fixer)

input() (內置函数)

(在 fileinput 模块中)

input_charset (email.charset.Charset 属性)

input_codec (email.charset.Charset 属性)

InputOnly (tkinter.tix 中的类)

InputSource (xml.sax.xmlreader 中的类)

inquiry (C 类型)

insch() (curses.window 方法)

insdelln() (curses.window 方法)

insert() (array.array 方法)

(collections.deque 方法)

(sequence method)

(tkinter.ttk.Notebook 方法)

(tkinter.ttk.Treeview 方法)

(xml.etree.ElementTree.Element 方法)

insert_text() (在 readline 模块中)

insertBefore() (xml.dom.Node 方法)

insertln() (curses.window 方法)

insnstr() (curses.window 方法)

insort() (在 bisect 模块中)

insort_left() (在 bisect 模块中)

insort_right() (在 bisect 模块中)

inspect (模块)

inspect command line option

--details

InspectLoader (importlib.abc 中的类)

insstr() (curses.window 方法)

install() (gettext.NullTranslations 方法)

(在 gettext 模块中)

install_opener() (在 urllib.request 模块中)

install_scripts() (venv.EnvBuilder 方法)

installHandler() (在 unittest 模块中)

instance

call, [1]

class

对象, [1], [2]

instancemethod

对象

instate() (tkinter.ttk.Widget 方法)

instr() (curses.window 方法)

instream (shlex.shlex 属性)

Instruction (dis 中的类)

Instruction.arg() (在 dis 模块中)

Instruction.argrepr() (在 dis 模块中)

Instruction.argval() (在 dis 模块中)

Instruction.is_jump_target() (在 dis 模块中)

Instruction.offset() (在 dis 模块中)

Instruction.opcode() (在 dis 模块中)

Instruction.opname() (在 dis 模块中)

Instruction.starts_line() (在 dis 模块中)

int

內置函数, [1], [2]

int (uuid.UUID 属性)

(內置类)

Int2AP() (在 imaplib 模块中)

int_info() (在 sys 模块中)

integer

literals

representation

types, operations on

对象, [1], [2]

integer literal

Integral (numbers 中的类)

Integrated Development Environment

IntegrityError

Intel/DVI ADPCM

IntEnum (enum 中的类)

interact (pdb command)

interact() (code.InteractiveConsole 方法)

(telnetlib.Telnet 方法)

(在 code 模块中)

interactive -- 交互

interactive mode

InteractiveConsole (code 中的类)

InteractiveInterpreter (code 中的类)

intern (2to3 fixer)

intern() (在 sys 模块中)

internal type

internal_attr (zipfile.ZipInfo 属性)

Internaldate2tuple() (在 imaplib 模块中)

internalSubset (xml.dom.DocumentType 属性)

Internet

interpolated string literal

interpolation

bytearray (%)

bytes (%)

interpolation, string (%)

InterpolationDepthError

InterpolationError

InterpolationMissingOptionError

InterpolationSyntaxError

interpreted -- 解释型

interpreter

interpreter lock

interpreter prompts

interpreter shutdown -- 解释器关闭

interrupt() (sqlite3.Connection 方法)

interrupt_main() (在 _thread 模块中)

InterruptedError

intersection() (frozenset 方法)

intersection_update() (frozenset 方法)

IntFlag (enum 中的类)

intro (cmd.Cmd 属性)

InuseAttributeErr

inv() (在 operator 模块中)

InvalidAccessErr

invalidate_caches() (importlib.abc.MetaPathFinder 方法)

(importlib.abc.PathEntryFinder 方法)

(importlib.machinery.FileFinder 方法)

(importlib.machinery.PathFinder 类方法)

(在 importlib 模块中)

InvalidCharacterErr

InvalidModificationErr

InvalidOperation (decimal 中的类)

InvalidStateErr

InvalidStateError

InvalidURL

inversion

invert() (在 operator 模块中)

invocation

io

模块

IO (typing 中的类)

io (模块)

io.StringIO

对象

IOBase (io 中的类)

ioctl() (socket.socket 方法)

(在 fcntl 模块中)

IOError

ior() (在 operator 模块中)

ip (ipaddress.IPv4Interface 属性)

(ipaddress.IPv6Interface 属性)

ip_address() (在 ipaddress 模块中)

ip_interface() (在 ipaddress 模块中)

ip_network() (在 ipaddress 模块中)

ipaddress (模块)

ipow() (在 operator 模块中)

ipv4_mapped (ipaddress.IPv6Address 属性)

IPv4Address (ipaddress 中的类)

IPv4Interface (ipaddress 中的类)

IPv4Network (ipaddress 中的类)

IPv6Address (ipaddress 中的类)

IPv6Interface (ipaddress 中的类)

IPv6Network (ipaddress 中的类)

irshift() (在 operator 模块中)

is

运算符, [1]

is not

运算符, [1]

is_() (在 operator 模块中)

is_absolute() (pathlib.PurePath 方法)

is_alive() (multiprocessing.Process 方法)

(threading.Thread 方法)

is_assigned() (symtable.Symbol 方法)

is_attachment() (email.message.EmailMessage 方法)

is_authenticated() (urllib.request.HTTPPasswordMgrWithPriorAuth 方法)

is_block_device() (pathlib.Path 方法)

is_blocked() (http.cookiejar.DefaultCookiePolicy 方法)

is_canonical() (decimal.Context 方法)

(decimal.Decimal 方法)

is_char_device() (pathlib.Path 方法)

IS_CHARACTER_JUNK() (在 difflib 模块中)

is_check_supported() (在 lzma 模块中)

is_closed() (asyncio.AbstractEventLoop 方法)

is_closing() (asyncio.BaseTransport 方法)

is_declared_global() (symtable.Symbol 方法)

is_dir() (os.DirEntry 方法)

(pathlib.Path 方法)

(zipfile.ZipInfo 方法)

is_enabled() (在 faulthandler 模块中)

is_expired() (http.cookiejar.Cookie 方法)

is_fifo() (pathlib.Path 方法)

is_file() (os.DirEntry 方法)

(pathlib.Path 方法)

is_finalizing() (在 sys 模块中)

is_finite() (decimal.Context 方法)

(decimal.Decimal 方法)

is_free() (symtable.Symbol 方法)

is_global (ipaddress.IPv4Address 属性)

(ipaddress.IPv6Address 属性)

is_global() (symtable.Symbol 方法)

is_hop_by_hop() (在 wsgiref.util 模块中)

is_imported() (symtable.Symbol 方法)

is_infinite() (decimal.Context 方法)

(decimal.Decimal 方法)

is_integer() (float 方法)

is_jython() (在 test.support 模块中)

IS_LINE_JUNK() (在 difflib 模块中)

is_linetouched() (curses.window 方法)

is_link_local (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

is_local() (symtable.Symbol 方法)

is_loopback (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

is_multicast (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

is_multipart() (email.message.EmailMessage 方法)

(email.message.Message 方法)

is_namespace() (symtable.Symbol 方法)

is_nan() (decimal.Context 方法)

(decimal.Decimal 方法)

is_nested() (symtable.SymbolTable 方法)

is_normal() (decimal.Context 方法)

(decimal.Decimal 方法)

is_not() (在 operator 模块中)

is_not_allowed() (http.cookiejar.DefaultCookiePolicy 方法)

is_optimized() (symtable.SymbolTable 方法)

is_package() (importlib.abc.InspectLoader 方法)

(importlib.abc.SourceLoader 方法)

(importlib.machinery.ExtensionFileLoader 方法)

(importlib.machinery.SourceFileLoader 方法)

(importlib.machinery.SourcelessFileLoader 方法)

(zipimport.zipimporter 方法)

is_parameter() (symtable.Symbol 方法)

is_private (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

is_python_build() (在 sysconfig 模块中)

is_qnan() (decimal.Context 方法)

(decimal.Decimal 方法)

is_referenced() (symtable.Symbol 方法)

is_reserved (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

is_reserved() (pathlib.PurePath 方法)

is_resource_enabled() (在 test.support 模块中)

is_running() (asyncio.AbstractEventLoop 方法)

is_set() (asyncio.Event 方法)

(threading.Event 方法)

is_signed() (decimal.Context 方法)

(decimal.Decimal 方法)

is_site_local (ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

is_snan() (decimal.Context 方法)

(decimal.Decimal 方法)

is_socket() (pathlib.Path 方法)

is_subnormal() (decimal.Context 方法)

(decimal.Decimal 方法)

is_symlink() (os.DirEntry 方法)

(pathlib.Path 方法)

is_tarfile() (在 tarfile 模块中)

is_term_resized() (在 curses 模块中)

is_tracing() (在 tracemalloc 模块中)

is_tracked() (在 gc 模块中)

is_unspecified (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

is_wintouched() (curses.window 方法)

is_zero() (decimal.Context 方法)

(decimal.Decimal 方法)

is_zipfile() (在 zipfile 模块中)

isabs() (在 os.path 模块中)

isabstract() (在 inspect 模块中)

IsADirectoryError

isalnum() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 curses.ascii 模块中)

isalpha() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 curses.ascii 模块中)

isascii() (在 curses.ascii 模块中)

isasyncgen() (在 inspect 模块中)

isasyncgenfunction() (在 inspect 模块中)

isatty() (chunk.Chunk 方法)

(io.IOBase 方法)

(在 os 模块中)

isawaitable() (在 inspect 模块中)

isblank() (在 curses.ascii 模块中)

isblk() (tarfile.TarInfo 方法)

isbuiltin() (在 inspect 模块中)

ischr() (tarfile.TarInfo 方法)

isclass() (在 inspect 模块中)

isclose() (在 cmath 模块中)

(在 math 模块中)

iscntrl() (在 curses.ascii 模块中)

iscode() (在 inspect 模块中)

iscoroutine() (在 asyncio 模块中)

(在 inspect 模块中)

iscoroutinefunction() (在 asyncio 模块中)

(在 inspect 模块中)

isctrl() (在 curses.ascii 模块中)

isDaemon() (threading.Thread 方法)

isdatadescriptor() (在 inspect 模块中)

isdecimal() (str 方法)

isdev() (tarfile.TarInfo 方法)

isdigit() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 curses.ascii 模块中)

isdir() (tarfile.TarInfo 方法)

(在 os.path 模块中)

isdisjoint() (frozenset 方法)

isdown() (在 turtle 模块中)

iselement() (在 xml.etree.ElementTree 模块中)

isenabled() (在 gc 模块中)

isEnabledFor() (logging.Logger 方法)

isendwin() (在 curses 模块中)

ISEOF() (在 token 模块中)

isexpr() (parser.ST 方法)

(在 parser 模块中)

isfifo() (tarfile.TarInfo 方法)

isfile() (tarfile.TarInfo 方法)

(在 os.path 模块中)

isfinite() (在 cmath 模块中)

(在 math 模块中)

isfirstline() (在 fileinput 模块中)

isframe() (在 inspect 模块中)

isfunction() (在 inspect 模块中)

isgenerator() (在 inspect 模块中)

isgeneratorfunction() (在 inspect 模块中)

isgetsetdescriptor() (在 inspect 模块中)

isgraph() (在 curses.ascii 模块中)

isidentifier() (str 方法)

isinf() (在 cmath 模块中)

(在 math 模块中)

isinstance (2to3 fixer)

isinstance() (內置函数)

iskeyword() (在 keyword 模块中)

isleap() (在 calendar 模块中)

islice() (在 itertools 模块中)

islink() (在 os.path 模块中)

islnk() (tarfile.TarInfo 方法)

islower() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 curses.ascii 模块中)

ismemberdescriptor() (在 inspect 模块中)

ismeta() (在 curses.ascii 模块中)

ismethod() (在 inspect 模块中)

ismethoddescriptor() (在 inspect 模块中)

ismodule() (在 inspect 模块中)

ismount() (在 os.path 模块中)

isnan() (在 cmath 模块中)

(在 math 模块中)

ISNONTERMINAL() (在 token 模块中)

isnumeric() (str 方法)

isocalendar() (datetime.date 方法)

(datetime.datetime 方法)

isoformat() (datetime.date 方法)

(datetime.datetime 方法)

(datetime.time 方法)

isolation_level (sqlite3.Connection 属性)

isoweekday() (datetime.date 方法)

(datetime.datetime 方法)

isprint() (在 curses.ascii 模块中)

isprintable() (str 方法)

ispunct() (在 curses.ascii 模块中)

isreadable() (pprint.PrettyPrinter 方法)

(在 pprint 模块中)

isrecursive() (pprint.PrettyPrinter 方法)

(在 pprint 模块中)

isreg() (tarfile.TarInfo 方法)

isReservedKey() (http.cookies.Morsel 方法)

isroutine() (在 inspect 模块中)

isSameNode() (xml.dom.Node 方法)

isspace() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 curses.ascii 模块中)

isstdin() (在 fileinput 模块中)

issubclass() (內置函数)

issubset() (frozenset 方法)

issuite() (parser.ST 方法)

(在 parser 模块中)

issuperset() (frozenset 方法)

issym() (tarfile.TarInfo 方法)

ISTERMINAL() (在 token 模块中)

istitle() (bytearray 方法)

(bytes 方法)

(str 方法)

istraceback() (在 inspect 模块中)

isub() (在 operator 模块中)

isupper() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 curses.ascii 模块中)

isvisible() (在 turtle 模块中)

isxdigit() (在 curses.ascii 模块中)

item

sequence

string

item selection

item() (tkinter.ttk.Treeview 方法)

(xml.dom.NamedNodeMap 方法)

(xml.dom.NodeList 方法)

itemgetter() (在 operator 模块中)

items() (configparser.ConfigParser 方法)

(dict 方法)

(email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(types.MappingProxyType 方法)

(xml.etree.ElementTree.Element 方法)

itemsize (array.array 属性)

(memoryview 属性)

ItemsView (collections.abc 中的类)

(typing 中的类)

iter() (xml.etree.ElementTree.Element 方法)

(xml.etree.ElementTree.ElementTree 方法)

(內置函数)

iter_attachments() (email.message.EmailMessage 方法)

iter_child_nodes() (在 ast 模块中)

iter_fields() (在 ast 模块中)

iter_importers() (在 pkgutil 模块中)

iter_modules() (在 pkgutil 模块中)

iter_parts() (email.message.EmailMessage 方法)

iter_unpack() (struct.Struct 方法)

(在 struct 模块中)

iterable

unpacking

Iterable (collections.abc 中的类)

(typing 中的类)

iterable -- 可迭代对象

Iterator (collections.abc 中的类)

(typing 中的类)

iterator -- 迭代器

iterator protocol

iterdecode() (在 codecs 模块中)

iterdir() (pathlib.Path 方法)

iterdump() (sqlite3.Connection 方法)

iterencode() (json.JSONEncoder 方法)

(在 codecs 模块中)

iterfind() (xml.etree.ElementTree.Element 方法)

(xml.etree.ElementTree.ElementTree 方法)

iteritems() (mailbox.Mailbox 方法)

iterkeys() (mailbox.Mailbox 方法)

itermonthdates() (calendar.Calendar 方法)

itermonthdays() (calendar.Calendar 方法)

itermonthdays2() (calendar.Calendar 方法)

iterparse() (在 xml.etree.ElementTree 模块中)

itertext() (xml.etree.ElementTree.Element 方法)

itertools (2to3 fixer)

(模块)

itertools_imports (2to3 fixer)

itervalues() (mailbox.Mailbox 方法)

iterweekdays() (calendar.Calendar 方法)

ITIMER_PROF() (在 signal 模块中)

ITIMER_REAL() (在 signal 模块中)

ITIMER_VIRTUAL() (在 signal 模块中)

ItimerError

itruediv() (在 operator 模块中)

ixor() (在 operator 模块中)

J

j

in numeric literal

Jansen, Jack

Java

language

java_ver() (在 platform 模块中)

join() (asyncio.Queue 方法)

(bytearray 方法)

(bytes 方法)

(multiprocessing.JoinableQueue 方法)

(multiprocessing.pool.Pool 方法)

(multiprocessing.Process 方法)

(queue.Queue 方法)

(str 方法)

(threading.Thread 方法)

(在 os.path 模块中)

join_thread() (multiprocessing.Queue 方法)

JoinableQueue (multiprocessing 中的类)

joinpath() (pathlib.PurePath 方法)

js_output() (http.cookies.BaseCookie 方法)

(http.cookies.Morsel 方法)

json

模块

json (模块)

json.tool (模块)

json.tool command line option

--help

--sort-keys

-h

infile

outfile

JSONDecodeError

JSONDecoder (json 中的类)

JSONEncoder (json 中的类)

jump (pdb command)

JUMP_ABSOLUTE (opcode)

JUMP_FORWARD (opcode)

JUMP_IF_FALSE_OR_POP (opcode)

JUMP_IF_TRUE_OR_POP (opcode)

K

kbhit() (在 msvcrt 模块中)

KDEDIR

kevent() (在 select 模块中)

key

(http.cookies.Morsel 属性)

key function -- 键函数

key/datum pair

KEY_ALL_ACCESS() (在 winreg 模块中)

KEY_CREATE_LINK() (在 winreg 模块中)

KEY_CREATE_SUB_KEY() (在 winreg 模块中)

KEY_ENUMERATE_SUB_KEYS() (在 winreg 模块中)

KEY_EXECUTE() (在 winreg 模块中)

KEY_NOTIFY() (在 winreg 模块中)

KEY_QUERY_VALUE() (在 winreg 模块中)

KEY_READ() (在 winreg 模块中)

KEY_SET_VALUE() (在 winreg 模块中)

KEY_WOW64_32KEY() (在 winreg 模块中)

KEY_WOW64_64KEY() (在 winreg 模块中)

KEY_WRITE() (在 winreg 模块中)

KeyboardInterrupt

(built-in exception), [1]

KeyError

keyname() (在 curses 模块中)

keypad() (curses.window 方法)

keyrefs() (weakref.WeakKeyDictionary 方法)

keys() (dict 方法)

(email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(sqlite3.Row 方法)

(types.MappingProxyType 方法)

(xml.etree.ElementTree.Element 方法)

KeysView (collections.abc 中的类)

(typing 中的类)

keyword

(模块)

keyword argument -- 关键字参数

keywords (functools.partial 属性)

kill() (asyncio.asyncio.subprocess.Process 方法)

(asyncio.BaseSubprocessTransport 方法)

(subprocess.Popen 方法)

(在 os 模块中)

killchar() (在 curses 模块中)

killpg() (在 os 模块中)

kind (inspect.Parameter 属性)

knownfiles() (在 mimetypes 模块中)

kqueue() (在 select 模块中)

KqueueSelector (selectors 中的类)

kwargs (inspect.BoundArguments 属性)

kwlist() (在 keyword 模块中)

L

L() (在 re 模块中)

LabelEntry (tkinter.tix 中的类)

LabelFrame (tkinter.tix 中的类)

lambda

expression, [1]

form

LambdaType() (在 types 模块中)

LANG, [1], [2], [3], [4]

LANGUAGE, [1]

language

C, [1], [2], [3], [4], [5]

Java

large files

LargeZipFile

last() (nntplib.NNTP 方法)

last_accepted (multiprocessing.connection.Listener 属性)

last_traceback (in module sys)

last_traceback() (在 sys 模块中)

last_type() (在 sys 模块中)

last_value() (在 sys 模块中)

lastChild (xml.dom.Node 属性)

lastcmd (cmd.Cmd 属性)

lastgroup (re.match 属性)

lastindex (re.match 属性)

lastResort() (在 logging 模块中)

lastrowid (sqlite3.Cursor 属性)

layout() (tkinter.ttk.Style 方法)

lazycache() (在 linecache 模块中)

LazyLoader (importlib.util 中的类)

LBRACE() (在 token 模块中)

LBYL

LC_ALL, [1]

LC_ALL() (在 locale 模块中)

LC_COLLATE() (在 locale 模块中)

LC_CTYPE() (在 locale 模块中)

LC_MESSAGES, [1]

LC_MESSAGES() (在 locale 模块中)

LC_MONETARY() (在 locale 模块中)

LC_NUMERIC() (在 locale 模块中)

LC_TIME() (在 locale 模块中)

lchflags() (在 os 模块中)

lchmod() (pathlib.Path 方法)

(在 os 模块中)

lchown() (在 os 模块中)

LDCXXSHARED

ldexp() (在 math 模块中)

LDFLAGS

ldgettext() (在 gettext 模块中)

ldngettext() (在 gettext 模块中)

le() (在 operator 模块中)

leading whitespace

leapdays() (在 calendar 模块中)

leaveok() (curses.window 方法)

left (filecmp.dircmp 属性)

left() (在 turtle 模块中)

left_list (filecmp.dircmp 属性)

left_only (filecmp.dircmp 属性)

LEFTSHIFT() (在 token 模块中)

LEFTSHIFTEQUAL() (在 token 模块中)

len

內置函数, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11]

len() (內置函数)

length (xml.dom.NamedNodeMap 属性)

(xml.dom.NodeList 属性)

length_hint() (在 operator 模块中)

LESS() (在 token 模块中)

LESSEQUAL() (在 token 模块中)

lexical analysis

lexical definitions

lexists() (在 os.path 模块中)

lgamma() (在 math 模块中)

lgettext() (gettext.GNUTranslations 方法)

(gettext.NullTranslations 方法)

(在 gettext 模块中)

lib2to3 (模块)

libc_ver() (在 platform 模块中)

library (ssl.SSLError 属性)

library() (在 dbm.ndbm 模块中)

library_dir_option() (distutils.ccompiler.CCompiler 方法)

library_filename() (distutils.ccompiler.CCompiler 方法)

library_option() (distutils.ccompiler.CCompiler 方法)

LibraryLoader (ctypes 中的类)

license (內置变量)

LifoQueue (asyncio 中的类)

(queue 中的类)

light-weight processes

limit_denominator() (fractions.Fraction 方法)

LimitOverrunError

lin2adpcm() (在 audioop 模块中)

lin2alaw() (在 audioop 模块中)

lin2lin() (在 audioop 模块中)

lin2ulaw() (在 audioop 模块中)

line (C 函数)

line continuation

line joining, [1]

line structure

line() (msilib.Dialog 方法)

line-buffered I/O

line_buffering (io.TextIOWrapper 属性)

line_num (csv.csvreader 属性)

linecache (模块)

lineno (ast.AST 属性)

(doctest.DocTest 属性)

(doctest.Example 属性)

(json.JSONDecodeError 属性)

(pyclbr.Class 属性)

(pyclbr.Function 属性)

(re.error 属性)

(shlex.shlex 属性)

(traceback.TracebackException 属性)

(tracemalloc.Filter 属性)

(tracemalloc.Frame 属性)

(xml.parsers.expat.ExpatError 属性)

lineno() (在 fileinput 模块中)

LINES, [1], [2], [3], [4]

lines (os.terminal_size 属性)

linesep (email.policy.Policy 属性)

linesep() (在 os 模块中)

lineterminator (csv.Dialect 属性)

LineTooLong

link() (distutils.ccompiler.CCompiler 方法)

(在 os 模块中)

link_executable() (distutils.ccompiler.CCompiler 方法)

link_shared_lib() (distutils.ccompiler.CCompiler 方法)

link_shared_object() (distutils.ccompiler.CCompiler 方法)

linkname (tarfile.TarInfo 属性)

linux_distribution() (在 platform 模块中)

list

assignment, target

comprehensions

deletion target

display

empty

expression, [1], [2]

target, [1], [2]

type, operations on

对象, [1], [2], [3], [4], [5], [6], [7], [8]

list (pdb command)

List (typing 中的类)

list (內置类)

list -- 列表

list comprehension -- 列表推导式

list() (imaplib.IMAP4 方法)

(multiprocessing.managers.SyncManager 方法)

(nntplib.NNTP 方法)

(poplib.POP3 方法)

(tarfile.TarFile 方法)

LIST_APPEND (opcode)

list_dialects() (在 csv 模块中)

list_folders() (mailbox.Maildir 方法)

(mailbox.MH 方法)

listdir() (在 os 模块中)

listen() (asyncore.dispatcher 方法)

(socket.socket 方法)

(在 logging.config 模块中)

(在 turtle 模块中)

Listener (multiprocessing.connection 中的类)

listMethods() (xmlrpc.client.ServerProxy.system 方法)

ListNoteBook (tkinter.tix 中的类)

listxattr() (在 os 模块中)

literal, [1]

literal_eval() (在 ast 模块中)

literals

binary

complex number

floating point

hexadecimal

integer

numeric

octal

LittleEndianStructure (ctypes 中的类)

ljust() (bytearray 方法)

(bytes 方法)

(str 方法)

LK_LOCK() (在 msvcrt 模块中)

LK_NBLCK() (在 msvcrt 模块中)

LK_NBRLCK() (在 msvcrt 模块中)

LK_RLCK() (在 msvcrt 模块中)

LK_UNLCK() (在 msvcrt 模块中)

ll (pdb command)

LMTP (smtplib 中的类)

ln() (decimal.Context 方法)

(decimal.Decimal 方法)

LNAME

lngettext() (gettext.GNUTranslations 方法)

(gettext.NullTranslations 方法)

(在 gettext 模块中)

load() (http.cookiejar.FileCookieJar 方法)

(http.cookies.BaseCookie 方法)

(pickle.Unpickler 方法)

(tracemalloc.Snapshot 类方法)

(在 json 模块中)

(在 marshal 模块中)

(在 pickle 模块中)

(在 plistlib 模块中)

LOAD_ATTR (opcode)

LOAD_BUILD_CLASS (opcode)

load_cert_chain() (ssl.SSLContext 方法)

LOAD_CLASSDEREF (opcode)

LOAD_CLOSURE (opcode)

LOAD_CONST (opcode)

load_default_certs() (ssl.SSLContext 方法)

LOAD_DEREF (opcode)

load_dh_params() (ssl.SSLContext 方法)

load_extension() (sqlite3.Connection 方法)

LOAD_FAST (opcode)

LOAD_GLOBAL (opcode)

load_module() (importlib.abc.FileLoader 方法)

(importlib.abc.InspectLoader 方法)

(importlib.abc.Loader 方法)

(importlib.abc.SourceLoader 方法)

(importlib.machinery.SourceFileLoader 方法)

(importlib.machinery.SourcelessFileLoader 方法)

(zipimport.zipimporter 方法)

(在 imp 模块中)

LOAD_NAME (opcode)

load_package_tests() (在 test.support 模块中)

load_verify_locations() (ssl.SSLContext 方法)

loader

Loader (importlib.abc 中的类)

loader (importlib.machinery.ModuleSpec 属性)

loader -- 加载器

loader_state (importlib.machinery.ModuleSpec 属性)

LoadError

LoadKey() (在 winreg 模块中)

LoadLibrary() (ctypes.LibraryLoader 方法)

loads() (在 json 模块中)

(在 marshal 模块中)

(在 pickle 模块中)

(在 plistlib 模块中)

(在 xmlrpc.client 模块中)

loadTestsFromModule() (unittest.TestLoader 方法)

loadTestsFromName() (unittest.TestLoader 方法)

loadTestsFromNames() (unittest.TestLoader 方法)

loadTestsFromTestCase() (unittest.TestLoader 方法)

local (threading 中的类)

localcontext() (在 decimal 模块中)

locale (模块)

LOCALE() (在 re 模块中)

localeconv() (在 locale 模块中)

LocaleHTMLCalendar (calendar 中的类)

LocaleTextCalendar (calendar 中的类)

localName (xml.dom.Attr 属性)

(xml.dom.Node 属性)

locals() (內置函数)

localtime() (在 email.utils 模块中)

(在 time 模块中)

Locator (xml.sax.xmlreader 中的类)

Lock (asyncio 中的类)

(multiprocessing 中的类)

(threading 中的类)

lock() (mailbox.Babyl 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(mailbox.mbox 方法)

(mailbox.MH 方法)

(mailbox.MMDF 方法)

Lock() (multiprocessing.managers.SyncManager 方法)

lock, interpreter

lock_held() (在 imp 模块中)

locked() (_thread.lock 方法)

(asyncio.Condition 方法)

(asyncio.Lock 方法)

(asyncio.Semaphore 方法)

lockf() (在 fcntl 模块中)

(在 os 模块中)

locking() (在 msvcrt 模块中)

LockType() (在 _thread 模块中)

log() (logging.Logger 方法)

(在 cmath 模块中)

(在 logging 模块中)

(在 math 模块中)

log10() (decimal.Context 方法)

(decimal.Decimal 方法)

(在 cmath 模块中)

(在 math 模块中)

log1p() (在 math 模块中)

log2() (在 math 模块中)

log_date_time_string() (http.server.BaseHTTPRequestHandler 方法)

log_error() (http.server.BaseHTTPRequestHandler 方法)

log_exception() (wsgiref.handlers.BaseHandler 方法)

log_message() (http.server.BaseHTTPRequestHandler 方法)

log_request() (http.server.BaseHTTPRequestHandler 方法)

log_to_stderr() (在 multiprocessing 模块中)

logb() (decimal.Context 方法)

(decimal.Decimal 方法)

Logger (logging 中的类)

LoggerAdapter (logging 中的类)

logging

Errors

logging (模块)

logging.config (模块)

logging.handlers (模块)

logical line

logical_and() (decimal.Context 方法)

(decimal.Decimal 方法)

logical_invert() (decimal.Context 方法)

(decimal.Decimal 方法)

logical_or() (decimal.Context 方法)

(decimal.Decimal 方法)

logical_xor() (decimal.Context 方法)

(decimal.Decimal 方法)

login() (ftplib.FTP 方法)

(imaplib.IMAP4 方法)

(nntplib.NNTP 方法)

(smtplib.SMTP 方法)

login_cram_md5() (imaplib.IMAP4 方法)

LOGNAME, [1]

lognormvariate() (在 random 模块中)

logout() (imaplib.IMAP4 方法)

LogRecord (logging 中的类)

long (2to3 fixer)

long integer

对象

LONG_MAX

longMessage (unittest.TestCase 属性)

longname() (在 curses 模块中)

lookup() (symtable.SymbolTable 方法)

(tkinter.ttk.Style 方法)

(在 codecs 模块中)

(在 unicodedata 模块中)

lookup_error() (在 codecs 模块中)

LookupError

loop

over mutable sequence

statement, [1], [2], [3]

loop control

target

loop() (在 asyncore 模块中)

lower() (bytearray 方法)

(bytes 方法)

(str 方法)

LPAR() (在 token 模块中)

lru_cache() (在 functools 模块中)

lseek() (在 os 模块中)

lshift() (在 operator 模块中)

LSQB() (在 token 模块中)

lstat() (pathlib.Path 方法)

(在 os 模块中)

lstrip() (bytearray 方法)

(bytes 方法)

(str 方法)

lsub() (imaplib.IMAP4 方法)

lt() (在 operator 模块中)

(在 turtle 模块中)

LWPCookieJar (http.cookiejar 中的类)

lzma (模块)

LZMACompressor (lzma 中的类)

LZMADecompressor (lzma 中的类)

LZMAError

LZMAFile (lzma 中的类)

M

M() (在 re 模块中)

mac_ver() (在 platform 模块中)

machine() (在 platform 模块中)

macpath (模块)

macros (netrc.netrc 属性)

MAGIC_NUMBER() (在 importlib.util 模块中)

MagicMock (unittest.mock 中的类)

Mailbox (mailbox 中的类)

mailbox (模块)

mailcap (模块)

Maildir (mailbox 中的类)

MaildirMessage (mailbox 中的类)

mailfrom (smtpd.SMTPChannel 属性)

MailmanProxy (smtpd 中的类)

main(), [1], [2]

(在 py_compile 模块中)

(在 site 模块中)

(在 unittest 模块中)

main_thread() (在 threading 模块中)

mainloop() (在 turtle 模块中)

maintype (email.headerregistry.ContentTypeHeader 属性)

major (email.headerregistry.MIMEVersionHeader 属性)

major() (在 os 模块中)

make_alternative() (email.message.EmailMessage 方法)

make_archive() (在 distutils.archive_util 模块中)

(在 shutil 模块中)

make_bad_fd() (在 test.support 模块中)

make_cookies() (http.cookiejar.CookieJar 方法)

make_file() (difflib.HtmlDiff 方法)

MAKE_FUNCTION (opcode)

make_header() (在 email.header 模块中)

make_mixed() (email.message.EmailMessage 方法)

make_msgid() (在 email.utils 模块中)

make_parser() (在 xml.sax 模块中)

make_related() (email.message.EmailMessage 方法)

make_server() (在 wsgiref.simple_server 模块中)

make_table() (difflib.HtmlDiff 方法)

make_tarball() (在 distutils.archive_util 模块中)

make_zipfile() (在 distutils.archive_util 模块中)

makedev() (在 os 模块中)

makedirs() (在 os 模块中)

makeelement() (xml.etree.ElementTree.Element 方法)

makefile() (socket method)

(socket.socket 方法)

makeLogRecord() (在 logging 模块中)

makePickle() (logging.handlers.SocketHandler 方法)

makeRecord() (logging.Logger 方法)

makeSocket() (logging.handlers.DatagramHandler 方法)

(logging.handlers.SocketHandler 方法)

maketrans() (bytearray 静态方法)

(bytes 静态方法)

(str 静态方法)

malloc()

mangle_from_ (email.policy.Compat32 属性)

(email.policy.Policy 属性)

mangling

name, [1]

map (2to3 fixer)

map() (concurrent.futures.Executor 方法)

(multiprocessing.pool.Pool 方法)

(tkinter.ttk.Style 方法)

(內置函数)

MAP_ADD (opcode)

map_async() (multiprocessing.pool.Pool 方法)

map_table_b2() (在 stringprep 模块中)

map_table_b3() (在 stringprep 模块中)

map_to_type() (email.headerregistry.HeaderRegistry 方法)

mapLogRecord() (logging.handlers.HTTPHandler 方法)

mapping

types, operations on

对象, [1], [2], [3], [4], [5]

Mapping (collections.abc 中的类)

(typing 中的类)

mapping -- 映射

mapping() (msilib.Control 方法)

MappingProxyType (types 中的类)

MappingView (collections.abc 中的类)

(typing 中的类)

mapPriority() (logging.handlers.SysLogHandler 方法)

maps (collections.ChainMap 属性)

maps() (在 nis 模块中)

marshal (模块)

marshalling

objects

masking

operations

Match (typing 中的类)

match() (pathlib.PurePath 方法)

(re.regex 方法)

(在 nis 模块中)

(在 re 模块中)

match_hostname() (在 ssl 模块中)

math

模块, [1]

math (模块)

matmul() (在 operator 模块中)

matrix multiplication

max

內置函数

max (datetime.date 属性)

(datetime.datetime 属性)

(datetime.time 属性)

(datetime.timedelta 属性)

max() (decimal.Context 方法)

(decimal.Decimal 方法)

(內置函数)

(在 audioop 模块中)

max_count (email.headerregistry.BaseHeader 属性)

MAX_EMAX() (在 decimal 模块中)

MAX_INTERPOLATION_DEPTH() (在 configparser 模块中)

max_line_length (email.policy.Policy 属性)

max_lines (textwrap.TextWrapper 属性)

max_mag() (decimal.Context 方法)

(decimal.Decimal 方法)

MAX_PREC() (在 decimal 模块中)

max_prefixlen (ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

maxarray (reprlib.Repr 属性)

maxdeque (reprlib.Repr 属性)

maxdict (reprlib.Repr 属性)

maxDiff (unittest.TestCase 属性)

maxfrozenset (reprlib.Repr 属性)

maxlen (collections.deque 属性)

maxlevel (reprlib.Repr 属性)

maxlist (reprlib.Repr 属性)

maxlong (reprlib.Repr 属性)

maxother (reprlib.Repr 属性)

maxpp() (在 audioop 模块中)

maxset (reprlib.Repr 属性)

maxsize (asyncio.Queue 属性)

maxsize() (在 sys 模块中)

maxstring (reprlib.Repr 属性)

maxtuple (reprlib.Repr 属性)

maxunicode() (在 sys 模块中)

MAXYEAR() (在 datetime 模块中)

MB_ICONASTERISK() (在 winsound 模块中)

MB_ICONEXCLAMATION() (在 winsound 模块中)

MB_ICONHAND() (在 winsound 模块中)

MB_ICONQUESTION() (在 winsound 模块中)

MB_OK() (在 winsound 模块中)

mbox (mailbox 中的类)

mboxMessage (mailbox 中的类)

mean() (在 statistics 模块中)

median() (在 statistics 模块中)

median_grouped() (在 statistics 模块中)

median_high() (在 statistics 模块中)

median_low() (在 statistics 模块中)

MemberDescriptorType() (在 types 模块中)

membership

test

memmove() (在 ctypes 模块中)

MemoryBIO (ssl 中的类)

MemoryError

MemoryHandler (logging.handlers 中的类)

memoryview

对象, [1]

memoryview (內置类)

memset() (在 ctypes 模块中)

merge() (在 heapq 模块中)

Message (email.message 中的类)

(mailbox 中的类)

message digest, MD5

message_factory (email.policy.Policy 属性)

message_from_binary_file() (在 email 模块中)

message_from_bytes() (在 email 模块中)

message_from_file() (在 email 模块中)

message_from_string() (在 email 模块中)

MessageBeep() (在 winsound 模块中)

MessageClass (http.server.BaseHTTPRequestHandler 属性)

MessageError

MessageParseError

messages() (在 xml.parsers.expat.errors 模块中)

meta

hooks

meta hooks

meta path finder -- 元路径查找器

meta() (在 curses 模块中)

meta_path() (在 sys 模块中)

metaclass

(2to3 fixer)

metaclass -- 元类

metaclass hint

MetaPathFinder (importlib.abc 中的类)

metavar (optparse.Option 属性)

MetavarTypeHelpFormatter (argparse 中的类)

Meter (tkinter.tix 中的类)

METH_CLASS (內置变量)

METH_COEXIST (內置变量)

METH_KEYWORDS (內置变量)

METH_NOARGS (內置变量)

METH_O (內置变量)

METH_STATIC (內置变量)

METH_VARARGS (內置变量)

method

built-in

call

user-defined

对象, [1], [2], [3], [4], [5]

method (urllib.request.Request 属性)

method resolution order -- 方法解析顺序

method 方法

method_calls (unittest.mock.Mock 属性)

METHOD_CRYPT() (在 crypt 模块中)

METHOD_MD5() (在 crypt 模块中)

METHOD_SHA256() (在 crypt 模块中)

METHOD_SHA512() (在 crypt 模块中)

methodattrs (2to3 fixer)

methodcaller() (在 operator 模块中)

methodHelp() (xmlrpc.client.ServerProxy.system 方法)

methods

bytearray

bytes

string

methods (pyclbr.Class 属性)

methods() (在 crypt 模块中)

methodSignature() (xmlrpc.client.ServerProxy.system 方法)

MethodType (in module types), [1]

MethodType() (在 types 模块中)

MH (mailbox 中的类)

MHMessage (mailbox 中的类)

microsecond (datetime.datetime 属性)

(datetime.time 属性)

MIME

base64 encoding

content type

headers, [1]

quoted-printable encoding

MIMEApplication (email.mime.application 中的类)

MIMEAudio (email.mime.audio 中的类)

MIMEBase (email.mime.base 中的类)

MIMEImage (email.mime.image 中的类)

MIMEMessage (email.mime.message 中的类)

MIMEMultipart (email.mime.multipart 中的类)

MIMENonMultipart (email.mime.nonmultipart 中的类)

MIMEPart (email.message 中的类)

MIMEText (email.mime.text 中的类)

MimeTypes (mimetypes 中的类)

mimetypes (模块)

MIMEVersionHeader (email.headerregistry 中的类)

min

內置函数

min (datetime.date 属性)

(datetime.datetime 属性)

(datetime.time 属性)

(datetime.timedelta 属性)

min() (decimal.Context 方法)

(decimal.Decimal 方法)

(內置函数)

MIN_EMIN() (在 decimal 模块中)

MIN_ETINY() (在 decimal 模块中)

min_mag() (decimal.Context 方法)

(decimal.Decimal 方法)

MINEQUAL() (在 token 模块中)

minmax() (在 audioop 模块中)

minor (email.headerregistry.MIMEVersionHeader 属性)

minor() (在 os 模块中)

minus

minus() (decimal.Context 方法)

MINUS() (在 token 模块中)

minute (datetime.datetime 属性)

(datetime.time 属性)

MINYEAR() (在 datetime 模块中)

mirrored() (在 unicodedata 模块中)

misc_header (cmd.Cmd 属性)

MissingSectionHeaderError

MIXERDEV

mkd() (ftplib.FTP 方法)

mkdir() (pathlib.Path 方法)

(在 os 模块中)

mkdtemp() (在 tempfile 模块中)

mkfifo() (在 os 模块中)

mknod() (在 os 模块中)

mkpath() (distutils.ccompiler.CCompiler 方法)

(在 distutils.dir_util 模块中)

mksalt() (在 crypt 模块中)

mkstemp() (在 tempfile 模块中)

mktemp() (在 tempfile 模块中)

mktime() (在 time 模块中)

mktime_tz() (在 email.utils 模块中)

mlsd() (ftplib.FTP 方法)

mmap (mmap 中的类)

(模块)

MMDF (mailbox 中的类)

MMDFMessage (mailbox 中的类)

Mock (unittest.mock 中的类)

mock_add_spec() (unittest.mock.Mock 方法)

mock_calls (unittest.mock.Mock 属性)

mock_open() (在 unittest.mock 模块中)

mod() (在 operator 模块中)

mode (io.FileIO 属性)

(ossaudiodev.oss_audio_device 属性)

(tarfile.TarInfo 属性)

mode() (在 statistics 模块中)

(在 turtle 模块中)

modes

file

modf() (在 math 模块中)

modified() (urllib.robotparser.RobotFileParser 方法)

Modify() (msilib.View 方法)

modify() (select.devpoll 方法)

(select.epoll 方法)

(select.poll 方法)

(selectors.BaseSelector 方法)

module

extension

importing

namespace

search path, [1], [2], [3], [4], [5], [6], [7]

对象, [1], [2]

module (pyclbr.Class 属性)

(pyclbr.Function 属性)

module spec

module spec -- 模块规格

module 模块

module_for_loader() (在 importlib.util 模块中)

module_from_spec() (在 importlib.util 模块中)

module_repr() (importlib.abc.Loader 方法)

ModuleFinder (modulefinder 中的类)

modulefinder (模块)

ModuleInfo (pkgutil 中的类)

ModuleNotFoundError

modules (in module sys), [1]

(modulefinder.ModuleFinder 属性)

modules() (在 sys 模块中)

ModuleSpec (importlib.machinery 中的类)

ModuleType (in module types)

(types 中的类)

modulo

monotonic() (在 time 模块中)

month (datetime.date 属性)

(datetime.datetime 属性)

month() (在 calendar 模块中)

month_abbr() (在 calendar 模块中)

month_name() (在 calendar 模块中)

monthcalendar() (在 calendar 模块中)

monthdatescalendar() (calendar.Calendar 方法)

monthdays2calendar() (calendar.Calendar 方法)

monthdayscalendar() (calendar.Calendar 方法)

monthrange() (在 calendar 模块中)

Morsel (http.cookies 中的类)

most_common() (collections.Counter 方法)

mouseinterval() (在 curses 模块中)

mousemask() (在 curses 模块中)

move() (curses.panel.Panel 方法)

(curses.window 方法)

(mmap.mmap 方法)

(tkinter.ttk.Treeview 方法)

(在 shutil 模块中)

move_file() (distutils.ccompiler.CCompiler 方法)

(在 distutils.file_util 模块中)

move_to_end() (collections.OrderedDict 方法)

MozillaCookieJar (http.cookiejar 中的类)

MRO

mro() (class 方法)

msg (http.client.HTTPResponse 属性)

(json.JSONDecodeError 属性)

(re.error 属性)

(traceback.TracebackException 属性)

msg() (telnetlib.Telnet 方法)

msi

msilib (模块)

msvcrt (模块)

mt_interact() (telnetlib.Telnet 方法)

mtime (gzip.GzipFile 属性)

(tarfile.TarInfo 属性)

mtime() (urllib.robotparser.RobotFileParser 方法)

mul() (在 audioop 模块中)

(在 operator 模块中)

MultiCall (xmlrpc.client 中的类)

MULTILINE() (在 re 模块中)

MultipartConversionError

multiplication

multiply() (decimal.Context 方法)

multiprocessing (模块)

multiprocessing.connection (模块)

multiprocessing.dummy (模块)

multiprocessing.Manager() (在 multiprocessing.sharedctypes 模块中)

multiprocessing.managers (模块)

multiprocessing.pool (模块)

multiprocessing.sharedctypes (模块)

mutable

sequence types

对象, [1], [2]

mutable -- 可变

mutable object

mutable sequence

loop over

对象

MutableMapping (collections.abc 中的类)

(typing 中的类)

MutableSequence (collections.abc 中的类)

(typing 中的类)

MutableSet (collections.abc 中的类)

(typing 中的类)

mvderwin() (curses.window 方法)

mvwin() (curses.window 方法)

myrights() (imaplib.IMAP4 方法)

N

N_TOKENS() (在 token 模块中)

n_waiting (threading.Barrier 属性)

name, [1], [2]

binding, [1], [2], [3], [4], [5], [6]

binding, global

class

function, [1]

mangling, [1]

rebinding

unbinding

name (codecs.CodecInfo 属性)

(doctest.DocTest 属性)

(email.headerregistry.BaseHeader 属性)

(hashlib.hash 属性)

(hmac.HMAC 属性)

(http.cookiejar.Cookie 属性)

(importlib.abc.FileLoader 属性)

(importlib.machinery.ExtensionFileLoader 属性)

(importlib.machinery.ModuleSpec 属性)

(importlib.machinery.SourceFileLoader 属性)

(importlib.machinery.SourcelessFileLoader 属性)

(inspect.Parameter 属性)

(io.FileIO 属性)

(multiprocessing.Process 属性)

(os.DirEntry 属性)

(ossaudiodev.oss_audio_device 属性)

(pyclbr.Class 属性)

(pyclbr.Function 属性)

(tarfile.TarInfo 属性)

(threading.Thread 属性)

(xml.dom.Attr 属性)

(xml.dom.DocumentType 属性)

name() (在 os 模块中)

NAME() (在 token 模块中)

name() (在 unicodedata 模块中)

name2codepoint() (在 html.entities 模块中)

named tuple -- 具名元组

NamedTemporaryFile() (在 tempfile 模块中)

NamedTuple (typing 中的类)

namedtuple() (在 collections 模块中)

NameError

例外

NameError (built-in exception)

namelist() (zipfile.ZipFile 方法)

nameprep() (在 encodings.idna 模块中)

namer (logging.handlers.BaseRotatingHandler 属性)

namereplace_errors() (在 codecs 模块中)

names

private

namespace

global

module

package

Namespace (argparse 中的类)

(multiprocessing.managers 中的类)

namespace -- 命名空间

namespace package -- 命名空间包

namespace() (imaplib.IMAP4 方法)

Namespace() (multiprocessing.managers.SyncManager 方法)

NAMESPACE_DNS() (在 uuid 模块中)

NAMESPACE_OID() (在 uuid 模块中)

NAMESPACE_URL() (在 uuid 模块中)

NAMESPACE_X500() (在 uuid 模块中)

NamespaceErr

namespaceURI (xml.dom.Node 属性)

NaN

nan() (在 cmath 模块中)

(在 math 模块中)

nanj() (在 cmath 模块中)

NannyNag

napms() (在 curses 模块中)

nargs (optparse.Option 属性)

nbytes (memoryview 属性)

ndiff() (在 difflib 模块中)

ndim (memoryview 属性)

ne (2to3 fixer)

ne() (在 operator 模块中)

needs_input (bz2.BZ2Decompressor 属性)

(lzma.LZMADecompressor 属性)

neg() (在 operator 模块中)

negation

nested scope -- 嵌套作用域

netmask (ipaddress.IPv4Network 属性)

(ipaddress.IPv6Network 属性)

NetmaskValueError

netrc (netrc 中的类)

(模块)

NetrcParseError

netscape (http.cookiejar.CookiePolicy 属性)

network (ipaddress.IPv4Interface 属性)

(ipaddress.IPv6Interface 属性)

Network News Transfer Protocol

network_address (ipaddress.IPv4Network 属性)

(ipaddress.IPv6Network 属性)

new() (在 hashlib 模块中)

(在 hmac 模块中)

new-style class -- 新式类

new_alignment() (formatter.writer 方法)

new_child() (collections.ChainMap 方法)

new_class() (在 types 模块中)

new_compiler() (在 distutils.ccompiler 模块中)

new_event_loop() (asyncio.AbstractEventLoopPolicy 方法)

(在 asyncio 模块中)

new_font() (formatter.writer 方法)

new_margin() (formatter.writer 方法)

new_module() (在 imp 模块中)

new_panel() (在 curses.panel 模块中)

new_spacing() (formatter.writer 方法)

new_styles() (formatter.writer 方法)

newer() (在 distutils.dep_util 模块中)

newer_group() (在 distutils.dep_util 模块中)

newer_pairwise() (在 distutils.dep_util 模块中)

newgroups() (nntplib.NNTP 方法)

NEWLINE token, [1]

NEWLINE() (在 token 模块中)

newlines (io.TextIOBase 属性)

newnews() (nntplib.NNTP 方法)

newpad() (在 curses 模块中)

NewType() (在 typing 模块中)

newwin() (在 curses 模块中)

next (2to3 fixer)

(pdb command)

next() (nntplib.NNTP 方法)

(tarfile.TarFile 方法)

(tkinter.ttk.Treeview 方法)

(內置函数)

next_minus() (decimal.Context 方法)

(decimal.Decimal 方法)

next_plus() (decimal.Context 方法)

(decimal.Decimal 方法)

next_toward() (decimal.Context 方法)

(decimal.Decimal 方法)

nextfile() (在 fileinput 模块中)

nextkey() (dbm.gnu.gdbm 方法)

nextSibling (xml.dom.Node 属性)

ngettext() (gettext.GNUTranslations 方法)

(gettext.NullTranslations 方法)

(在 gettext 模块中)

nice() (在 os 模块中)

nis (模块)

nl() (在 curses 模块中)

NL() (在 tokenize 模块中)

nl_langinfo() (在 locale 模块中)

nlargest() (在 heapq 模块中)

nlst() (ftplib.FTP 方法)

NNTP

protocol

NNTP (nntplib 中的类)

nntp_implementation (nntplib.NNTP 属性)

NNTP_SSL (nntplib 中的类)

nntp_version (nntplib.NNTP 属性)

NNTPDataError

NNTPError

nntplib (模块)

NNTPPermanentError

NNTPProtocolError

NNTPReplyError

NNTPTemporaryError

no_proxy

no_type_check() (在 typing 模块中)

no_type_check_decorator() (在 typing 模块中)

nocbreak() (在 curses 模块中)

NoDataAllowedErr

node() (在 platform 模块中)

nodelay() (curses.window 方法)

nodeName (xml.dom.Node 属性)

NodeTransformer (ast 中的类)

nodeType (xml.dom.Node 属性)

nodeValue (xml.dom.Node 属性)

NodeVisitor (ast 中的类)

noecho() (在 curses 模块中)

NOEXPR() (在 locale 模块中)

NoModificationAllowedErr

nonblock() (ossaudiodev.oss_audio_device 方法)

NonCallableMagicMock (unittest.mock 中的类)

NonCallableMock (unittest.mock 中的类)

None

对象, [1], [2]

None (Built-in object)

(內置变量)

nonl() (在 curses 模块中)

nonlocal

语句

nonzero (2to3 fixer)

noop() (imaplib.IMAP4 方法)

(poplib.POP3 方法)

NoOptionError

NOP (opcode)

noqiflush() (在 curses 模块中)

noraw() (在 curses 模块中)

NoReturn() (在 typing 模块中)

normalize() (decimal.Context 方法)

(decimal.Decimal 方法)

(xml.dom.Node 方法)

(在 locale 模块中)

(在 unicodedata 模块中)

NORMALIZE_WHITESPACE() (在 doctest 模块中)

normalvariate() (在 random 模块中)

normcase() (在 os.path 模块中)

normpath() (在 os.path 模块中)

NoSectionError

NoSuchMailboxError

not

运算符, [1]

not in

运算符, [1], [2]

not_() (在 operator 模块中)

NotADirectoryError

notation

notationDecl() (xml.sax.handler.DTDHandler 方法)

NotationDeclHandler() (xml.parsers.expat.xmlparser 方法)

notations (xml.dom.DocumentType 属性)

NotConnected

NoteBook (tkinter.tix 中的类)

Notebook (tkinter.ttk 中的类)

NotEmptyError

NOTEQUAL() (在 token 模块中)

NotFoundErr

notify() (asyncio.Condition 方法)

(threading.Condition 方法)

notify_all() (asyncio.Condition 方法)

(threading.Condition 方法)

notimeout() (curses.window 方法)

NotImplemented

对象

NotImplemented (內置变量)

NotImplementedError

NotStandaloneHandler() (xml.parsers.expat.xmlparser 方法)

NotSupportedErr

NotSupportedError

noutrefresh() (curses.window 方法)

now() (datetime.datetime 类方法)

NSIG() (在 signal 模块中)

nsmallest() (在 heapq 模块中)

NT_OFFSET() (在 token 模块中)

NTEventLogHandler (logging.handlers 中的类)

ntohl() (在 socket 模块中)

ntohs() (在 socket 模块中)

ntransfercmd() (ftplib.FTP 方法)

null

operation, [1]

NullFormatter (formatter 中的类)

NullHandler (logging 中的类)

NullImporter (imp 中的类)

NullTranslations (gettext 中的类)

NullWriter (formatter 中的类)

num_addresses (ipaddress.IPv4Network 属性)

(ipaddress.IPv6Network 属性)

number

complex

floating point

Number (numbers 中的类)

NUMBER() (在 token 模块中)

number_class() (decimal.Context 方法)

(decimal.Decimal 方法)

numbers (模块)

numerator (fractions.Fraction 属性)

(numbers.Rational 属性)

numeric

conversions

literals

object

types, operations on

对象, [1], [2], [3]

numeric literal

numeric() (在 unicodedata 模块中)

Numerical Python

numinput() (在 turtle 模块中)

numliterals (2to3 fixer)

O

O_APPEND() (在 os 模块中)

O_ASYNC() (在 os 模块中)

O_BINARY() (在 os 模块中)

O_CLOEXEC() (在 os 模块中)

O_CREAT() (在 os 模块中)

O_DIRECT() (在 os 模块中)

O_DIRECTORY() (在 os 模块中)

O_DSYNC() (在 os 模块中)

O_EXCL() (在 os 模块中)

O_EXLOCK() (在 os 模块中)

O_NDELAY() (在 os 模块中)

O_NOATIME() (在 os 模块中)

O_NOCTTY() (在 os 模块中)

O_NOFOLLOW() (在 os 模块中)

O_NOINHERIT() (在 os 模块中)

O_NONBLOCK() (在 os 模块中)

O_PATH() (在 os 模块中)

O_RANDOM() (在 os 模块中)

O_RDONLY() (在 os 模块中)

O_RDWR() (在 os 模块中)

O_RSYNC() (在 os 模块中)

O_SEQUENTIAL() (在 os 模块中)

O_SHLOCK() (在 os 模块中)

O_SHORT_LIVED() (在 os 模块中)

O_SYNC() (在 os 模块中)

O_TEMPORARY() (在 os 模块中)

O_TEXT() (在 os 模块中)

O_TMPFILE() (在 os 模块中)

O_TRUNC() (在 os 模块中)

O_WRONLY() (在 os 模块中)

obj (memoryview 属性)

object

code, [1], [2], [3]

deallocation

finalization

immutable, [1]

numeric

object (UnicodeError 属性)

(內置类)

object -- 对象

object.__slots__ (內置变量)

object_filenames() (distutils.ccompiler.CCompiler 方法)

objects

comparing

flattening

marshalling

persistent

pickling

serializing

obufcount() (ossaudiodev.oss_audio_device 方法)

obuffree() (ossaudiodev.oss_audio_device 方法)

oct() (內置函数)

octal

literals

octal literal

octdigits() (在 string 模块中)

offset (traceback.TracebackException 属性)

(xml.parsers.expat.ExpatError 属性)

OK() (在 curses 模块中)

OleDLL (ctypes 中的类)

onclick() (在 turtle 模块中), [1]

ondrag() (在 turtle 模块中)

onecmd() (cmd.Cmd 方法)

onkey() (在 turtle 模块中)

onkeypress() (在 turtle 模块中)

onkeyrelease() (在 turtle 模块中)

onrelease() (在 turtle 模块中)

onscreenclick() (在 turtle 模块中)

ontimer() (在 turtle 模块中)

OP() (在 token 模块中)

OP_ALL() (在 ssl 模块中)

OP_CIPHER_SERVER_PREFERENCE() (在 ssl 模块中)

OP_ENABLE_MIDDLEBOX_COMPAT() (在 ssl 模块中)

OP_NO_COMPRESSION() (在 ssl 模块中)

OP_NO_SSLv2() (在 ssl 模块中)

OP_NO_SSLv3() (在 ssl 模块中)

OP_NO_TICKET() (在 ssl 模块中)

OP_NO_TLSv1() (在 ssl 模块中)

OP_NO_TLSv1_1() (在 ssl 模块中)

OP_NO_TLSv1_2() (在 ssl 模块中)

OP_NO_TLSv1_3() (在 ssl 模块中)

OP_SINGLE_DH_USE() (在 ssl 模块中)

OP_SINGLE_ECDH_USE() (在 ssl 模块中)

open

內置函数, [1]

open() (distutils.text_file.TextFile 方法)

(imaplib.IMAP4 方法)

(pathlib.Path 方法)

(pipes.Template 方法)

(tarfile.TarFile 类方法)

(telnetlib.Telnet 方法)

(urllib.request.OpenerDirector 方法)

(urllib.request.URLopener 方法)

(webbrowser.controller 方法)

(zipfile.ZipFile 方法)

(內置函数)

(在 aifc 模块中)

(在 bz2 模块中)

(在 codecs 模块中)

(在 dbm 模块中)

(在 dbm.dumb 模块中)

(在 dbm.gnu 模块中)

(在 dbm.ndbm 模块中)

(在 gzip 模块中)

(在 io 模块中)

(在 lzma 模块中)

(在 os 模块中)

(在 ossaudiodev 模块中)

(在 shelve 模块中)

(在 sunau 模块中)

(在 tarfile 模块中)

(在 tokenize 模块中)

(在 wave 模块中)

(在 webbrowser 模块中)

open_connection() (在 asyncio 模块中)

open_new() (webbrowser.controller 方法)

(在 webbrowser 模块中)

open_new_tab() (webbrowser.controller 方法)

(在 webbrowser 模块中)

open_osfhandle() (在 msvcrt 模块中)

open_unix_connection() (在 asyncio 模块中)

open_unknown() (urllib.request.URLopener 方法)

OpenDatabase() (在 msilib 模块中)

OpenerDirector (urllib.request 中的类)

openfp() (在 sunau 模块中)

(在 wave 模块中)

OpenKey() (在 winreg 模块中)

OpenKeyEx() (在 winreg 模块中)

openlog() (在 syslog 模块中)

openmixer() (在 ossaudiodev 模块中)

openpty() (在 os 模块中)

(在 pty 模块中)

OpenSSL

(use in module hashlib)

(use in module ssl)

OPENSSL_VERSION() (在 ssl 模块中)

OPENSSL_VERSION_INFO() (在 ssl 模块中)

OPENSSL_VERSION_NUMBER() (在 ssl 模块中)

OpenView() (msilib.Database 方法)

operation

binary arithmetic

binary bitwise

Boolean

concatenation

null, [1]

power

repetition

shifting

slice

subscript

unary arithmetic

unary bitwise

OperationalError

operations

bitwise

Boolean, [1]

masking

shifting

operations on

dictionary type

integer types

list type

mapping types

numeric types

sequence types, [1]

operator

+ (plus), [1], [2]

- (minus), [1], [2]

comparison

overloading

precedence

ternary

operator (2to3 fixer)

(模块)

operators

opmap() (在 dis 模块中)

opname() (在 dis 模块中)

optimize() (在 pickletools 模块中)

OPTIMIZED_BYTECODE_SUFFIXES() (在 importlib.machinery 模块中)

Optional() (在 typing 模块中)

OptionGroup (optparse 中的类)

OptionMenu (tkinter.tix 中的类)

OptionParser (optparse 中的类)

options (doctest.Example 属性)

Options (ssl 中的类)

options (ssl.SSLContext 属性)

options() (configparser.ConfigParser 方法)

optionxform() (configparser.ConfigParser 方法), [1]

optparse (模块)

or

bitwise

exclusive

inclusive

运算符, [1], [2]

or_() (在 operator 模块中)

ord

內置函数

ord() (內置函数)

order

evaluation

ordered_attributes (xml.parsers.expat.xmlparser 属性)

OrderedDict (collections 中的类)

origin (importlib.machinery.ModuleSpec 属性)

origin_req_host (urllib.request.Request 属性)

origin_server (wsgiref.handlers.BaseHandler 属性)

os

模块

os (模块)

os.path (模块)

os_environ (wsgiref.handlers.BaseHandler 属性)

OSError

ossaudiodev (模块)

OSSAudioError

outfile

json.tool command line option

output

standard

output (subprocess.CalledProcessError 属性)

(subprocess.TimeoutExpired 属性)

(unittest.TestCase 属性)

output() (http.cookies.BaseCookie 方法)

(http.cookies.Morsel 方法)

output_charset (email.charset.Charset 属性)

output_charset() (gettext.NullTranslations 方法)

output_codec (email.charset.Charset 属性)

output_difference() (doctest.OutputChecker 方法)

OutputChecker (doctest 中的类)

OutputString() (http.cookies.Morsel 方法)

over() (nntplib.NNTP 方法)

Overflow (decimal 中的类)

OverflowError

(built-in exception), [1], [2], [3], [4], [5]

overlaps() (ipaddress.IPv4Network 方法)

(ipaddress.IPv6Network 方法)

overlay() (curses.window 方法)

overload() (在 typing 模块中)

overloading

operator

overwrite() (curses.window 方法)

owner() (pathlib.Path 方法)

P

p (pdb command)

P_ALL() (在 os 模块中)

P_DETACH() (在 os 模块中)

P_NOWAIT() (在 os 模块中)

P_NOWAITO() (在 os 模块中)

P_OVERLAY() (在 os 模块中)

P_PGID() (在 os 模块中)

P_PID() (在 os 模块中)

P_WAIT() (在 os 模块中)

pack() (mailbox.MH 方法)

(struct.Struct 方法)

(在 struct 模块中)

pack_array() (xdrlib.Packer 方法)

pack_bytes() (xdrlib.Packer 方法)

pack_double() (xdrlib.Packer 方法)

pack_farray() (xdrlib.Packer 方法)

pack_float() (xdrlib.Packer 方法)

pack_fopaque() (xdrlib.Packer 方法)

pack_fstring() (xdrlib.Packer 方法)

pack_into() (struct.Struct 方法)

(在 struct 模块中)

pack_list() (xdrlib.Packer 方法)

pack_opaque() (xdrlib.Packer 方法)

pack_string() (xdrlib.Packer 方法)

package, [1]

namespace

portion

regular

package -- 包

package variable

__all__

packed (ipaddress.IPv4Address 属性)

(ipaddress.IPv6Address 属性)

Packer (xdrlib 中的类)

packing

binary data

packing (widgets)

PAGER

pair_content() (在 curses 模块中)

pair_number() (在 curses 模块中)

PanedWindow (tkinter.tix 中的类)

parameter

call semantics

difference from argument

function definition

value, default

Parameter (inspect 中的类)

parameter -- 形参

ParameterizedMIMEHeader (email.headerregistry 中的类)

parameters (inspect.Signature 属性)

params (email.headerregistry.ParameterizedMIMEHeader 属性)

pardir() (在 os 模块中)

paren (2to3 fixer)

parent (importlib.machinery.ModuleSpec 属性)

(urllib.request.BaseHandler 属性)

parent() (tkinter.ttk.Treeview 方法)

parenthesized form

parentNode (xml.dom.Node 属性)

parents (collections.ChainMap 属性)

paretovariate() (在 random 模块中)

parse() (doctest.DocTestParser 方法)

(email.parser.BytesParser 方法)

(email.parser.Parser 方法)

(string.Formatter 方法)

(urllib.robotparser.RobotFileParser 方法)

(xml.etree.ElementTree.ElementTree 方法)

Parse() (xml.parsers.expat.xmlparser 方法)

parse() (xml.sax.xmlreader.XMLReader 方法)

(在 ast 模块中)

(在 cgi 模块中)

(在 xml.dom.minidom 模块中)

(在 xml.dom.pulldom 模块中)

(在 xml.etree.ElementTree 模块中)

(在 xml.sax 模块中)

parse_and_bind() (在 readline 模块中)

parse_args() (argparse.ArgumentParser 方法)

PARSE_COLNAMES() (在 sqlite3 模块中)

parse_config_h() (在 sysconfig 模块中)

PARSE_DECLTYPES() (在 sqlite3 模块中)

parse_header() (在 cgi 模块中)

parse_known_args() (argparse.ArgumentParser 方法)

parse_multipart() (在 cgi 模块中)

parse_qs() (在 cgi 模块中)

(在 urllib.parse 模块中)

parse_qsl() (在 cgi 模块中)

(在 urllib.parse 模块中)

parseaddr() (在 email.utils 模块中)

parsebytes() (email.parser.BytesParser 方法)

parsedate() (在 email.utils 模块中)

parsedate_to_datetime() (在 email.utils 模块中)

parsedate_tz() (在 email.utils 模块中)

ParseError (xml.etree.ElementTree 中的类)

ParseFile() (xml.parsers.expat.xmlparser 方法)

ParseFlags() (在 imaplib 模块中)

parser

Parser (email.parser 中的类)

parser (模块)

ParserCreate() (在 xml.parsers.expat 模块中)

ParserError

ParseResult (urllib.parse 中的类)

ParseResultBytes (urllib.parse 中的类)

parsestr() (email.parser.Parser 方法)

parseString() (在 xml.dom.minidom 模块中)

(在 xml.dom.pulldom 模块中)

(在 xml.sax 模块中)

parsing

Python source code

URL

ParsingError

partial (asyncio.IncompleteReadError 属性)

partial() (imaplib.IMAP4 方法)

(在 functools 模块中)

partialmethod (functools 中的类)

parties (threading.Barrier 属性)

partition() (bytearray 方法)

(bytes 方法)

(str 方法)

pass

语句

pass_() (poplib.POP3 方法)

Paste

patch() (在 unittest.mock 模块中)

patch.dict() (在 unittest.mock 模块中)

patch.multiple() (在 unittest.mock 模块中)

patch.object() (在 unittest.mock 模块中)

patch.stopall() (在 unittest.mock 模块中)

PATH, [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], [26], [27], [28], [29], [30], [31], [32], [33], [34]

path

configuration file

hooks

module search, [1], [2], [3], [4], [5], [6], [7]

operations, [1]

path (http.cookiejar.Cookie 属性)

(http.server.BaseHTTPRequestHandler 属性)

(importlib.abc.FileLoader 属性)

(importlib.machinery.ExtensionFileLoader 属性)

(importlib.machinery.FileFinder 属性)

(importlib.machinery.SourceFileLoader 属性)

(importlib.machinery.SourcelessFileLoader 属性)

(in module sys), [1], [2], [3]

(os.DirEntry 属性)

Path (pathlib 中的类)

path based finder

path based finder -- 基于路径的查找器

Path browser

path entry -- 路径入口

path entry finder -- 路径入口查找器

path entry hook -- 路径入口钩子

path hooks

path() (在 sys 模块中)

path-like object -- 路径类对象

path_hook() (importlib.machinery.FileFinder 类方法)

path_hooks() (在 sys 模块中)

path_importer_cache() (在 sys 模块中)

path_mtime() (importlib.abc.SourceLoader 方法)

path_return_ok() (http.cookiejar.CookiePolicy 方法)

path_stats() (importlib.abc.SourceLoader 方法)

(importlib.machinery.SourceFileLoader 方法)

pathconf() (在 os 模块中)

pathconf_names() (在 os 模块中)

PathEntryFinder (importlib.abc 中的类)

PATHEXT, [1]

PathFinder (importlib.machinery 中的类)

pathlib (模块)

PathLike (os 中的类)

pathname2url() (在 urllib.request 模块中)

pathsep() (在 os 模块中)

pattern (re.error 属性)

(re.regex 属性)

Pattern (typing 中的类)

pause() (在 signal 模块中)

pause_reading() (asyncio.ReadTransport 方法)

pause_writing() (asyncio.BaseProtocol 方法)

PAX_FORMAT() (在 tarfile 模块中)

pax_headers (tarfile.TarFile 属性)

(tarfile.TarInfo 属性)

pbkdf2_hmac() (在 hashlib 模块中)

pd() (在 turtle 模块中)

Pdb (class in pdb)

(pdb 中的类)

pdb (模块)

peek() (bz2.BZ2File 方法)

(gzip.GzipFile 方法)

(io.BufferedReader 方法)

(lzma.LZMAFile 方法)

(weakref.finalize 方法)

peer (smtpd.SMTPChannel 属性)

PEM_cert_to_DER_cert() (在 ssl 模块中)

pen() (在 turtle 模块中)

pencolor() (在 turtle 模块中)

pending (ssl.MemoryBIO 属性)

pending() (ssl.SSLSocket 方法)

PendingDeprecationWarning

pendown() (在 turtle 模块中)

pensize() (在 turtle 模块中)

penup() (在 turtle 模块中)

PEP

PERCENT() (在 token 模块中)

PERCENTEQUAL() (在 token 模块中)

perf_counter() (在 time 模块中)

Performance

PermissionError

permutations() (在 itertools 模块中)

Persist() (msilib.SummaryInformation 方法)

persistence

persistent

objects

persistent_id (pickle protocol)

persistent_id() (pickle.Pickler 方法)

persistent_load (pickle protocol)

persistent_load() (pickle.Unpickler 方法)

PF_CAN() (在 socket 模块中)

PF_PACKET() (在 socket 模块中)

PF_RDS() (在 socket 模块中)

pformat() (pprint.PrettyPrinter 方法)

(在 pprint 模块中)

phase() (在 cmath 模块中)

Philbrick, Geoff

physical line, [1], [2]

pi() (在 cmath 模块中)

(在 math 模块中)

pickle

模块, [1], [2], [3]

pickle (模块)

pickle() (在 copyreg 模块中)

PickleError

Pickler (pickle 中的类)

pickletools (模块)

pickletools command line option

--annotate

--indentlevel=<num>

--memo

--output=<file>

--preamble=<preamble>

-a

-l

-m

-o

-p

pickling

objects

PicklingError

pid (asyncio.asyncio.subprocess.Process 属性)

(multiprocessing.Process 属性)

(subprocess.Popen 属性)

Pipe() (在 multiprocessing 模块中)

pipe() (在 os 模块中)

PIPE() (在 subprocess 模块中)

pipe2() (在 os 模块中)

PIPE_BUF() (在 select 模块中)

pipe_connection_lost() (asyncio.SubprocessProtocol 方法)

pipe_data_received() (asyncio.SubprocessProtocol 方法)

pipes (模块)

PKG_DIRECTORY() (在 imp 模块中)

pkgutil (模块)

placeholder (textwrap.TextWrapper 属性)

PLAT

platform (in module sys)

(模块)

platform() (在 platform 模块中)

(在 sys 模块中)

PlaySound() (在 winsound 模块中)

plist

file

plistlib (模块)

plock() (在 os 模块中)

plus

plus() (decimal.Context 方法)

PLUS() (在 token 模块中)

PLUSEQUAL() (在 token 模块中)

pm() (在 pdb 模块中)

POINTER() (在 ctypes 模块中)

pointer() (在 ctypes 模块中)

polar() (在 cmath 模块中)

Policy (email.policy 中的类)

poll() (multiprocessing.connection.Connection 方法)

(select.devpoll 方法)

(select.epoll 方法)

(select.poll 方法)

(subprocess.Popen 方法)

(在 select 模块中)

PollSelector (selectors 中的类)

Pool (multiprocessing.pool 中的类)

pop() (array.array 方法)

(collections.deque 方法)

(dict 方法)

(frozenset 方法)

(mailbox.Mailbox 方法)

(sequence method)

POP3

protocol

POP3 (poplib 中的类)

POP3_SSL (poplib 中的类)

pop_alignment() (formatter.formatter 方法)

pop_all() (contextlib.ExitStack 方法)

POP_BLOCK (opcode)

POP_EXCEPT (opcode)

pop_font() (formatter.formatter 方法)

POP_JUMP_IF_FALSE (opcode)

POP_JUMP_IF_TRUE (opcode)

pop_margin() (formatter.formatter 方法)

pop_source() (shlex.shlex 方法)

pop_style() (formatter.formatter 方法)

POP_TOP (opcode)

Popen (subprocess 中的类)

popen() (in module os), [1]

(在 os 模块中)

(在 platform 模块中)

popitem() (collections.OrderedDict 方法)

(dict 方法)

(mailbox.Mailbox 方法)

popleft() (collections.deque 方法)

poplib (模块)

PopupMenu (tkinter.tix 中的类)

port (http.cookiejar.Cookie 属性)

port_specified (http.cookiejar.Cookie 属性)

portion

package

portion -- 部分

pos (json.JSONDecodeError 属性)

(re.error 属性)

(re.match 属性)

pos() (在 operator 模块中)

(在 turtle 模块中)

position (xml.etree.ElementTree.ParseError 属性)

position() (在 turtle 模块中)

positional argument -- 位置参数

POSIX

I/O control

threads

posix (模块)

POSIX_FADV_DONTNEED() (在 os 模块中)

POSIX_FADV_NOREUSE() (在 os 模块中)

POSIX_FADV_NORMAL() (在 os 模块中)

POSIX_FADV_RANDOM() (在 os 模块中)

POSIX_FADV_SEQUENTIAL() (在 os 模块中)

POSIX_FADV_WILLNEED() (在 os 模块中)

posix_fadvise() (在 os 模块中)

posix_fallocate() (在 os 模块中)

POSIXLY_CORRECT

PosixPath (pathlib 中的类)

post() (nntplib.NNTP 方法)

(ossaudiodev.oss_audio_device 方法)

post_handshake_auth (ssl.SSLContext 属性)

post_mortem() (在 pdb 模块中)

post_setup() (venv.EnvBuilder 方法)

postcmd() (cmd.Cmd 方法)

postloop() (cmd.Cmd 方法)

pow

內置函数, [1], [2], [3], [4], [5]

pow() (內置函数)

(在 math 模块中)

(在 operator 模块中)

power

operation

power() (decimal.Context 方法)

pp (pdb command)

pprint (模块)

pprint() (pprint.PrettyPrinter 方法)

(在 pprint 模块中)

prcal() (在 calendar 模块中)

pread() (在 os 模块中)

preamble (email.message.EmailMessage 属性)

(email.message.Message 属性)

precedence

operator

precmd() (cmd.Cmd 方法)

prefix, [1], [2], [3]

(xml.dom.Attr 属性)

(xml.dom.Node 属性)

(zipimport.zipimporter 属性)

PREFIX() (在 distutils.sysconfig 模块中)

prefix() (在 sys 模块中)

PREFIXES() (在 site 模块中)

prefixlen (ipaddress.IPv4Network 属性)

(ipaddress.IPv6Network 属性)

preloop() (cmd.Cmd 方法)

prepare() (logging.handlers.QueueHandler 方法)

(logging.handlers.QueueListener 方法)

prepare_class() (在 types 模块中)

prepare_input_source() (在 xml.sax.saxutils 模块中)

prepend() (pipes.Template 方法)

preprocess() (distutils.ccompiler.CCompiler 方法)

PrettyPrinter (pprint 中的类)

prev() (tkinter.ttk.Treeview 方法)

previousSibling (xml.dom.Node 属性)

primary

print

內置函数

print (2to3 fixer)

print() (built-in function)

__str__() (object method)

print() (內置函数)

print_callees() (pstats.Stats 方法)

print_callers() (pstats.Stats 方法)

print_directory() (在 cgi 模块中)

print_environ() (在 cgi 模块中)

print_environ_usage() (在 cgi 模块中)

print_exc() (timeit.Timer 方法)

(在 traceback 模块中)

print_exception() (在 traceback 模块中)

PRINT_EXPR (opcode)

print_form() (在 cgi 模块中)

print_help() (argparse.ArgumentParser 方法)

print_last() (在 traceback 模块中)

print_stack() (asyncio.Task 方法)

(在 traceback 模块中)

print_stats() (profile.Profile 方法)

(pstats.Stats 方法)

print_tb() (在 traceback 模块中)

print_usage() (argparse.ArgumentParser 方法)

(optparse.OptionParser 方法)

print_version() (optparse.OptionParser 方法)

printable() (在 string 模块中)

printdir() (zipfile.ZipFile 方法)

printf-style formatting, [1]

PRIO_PGRP() (在 os 模块中)

PRIO_PROCESS() (在 os 模块中)

PRIO_USER() (在 os 模块中)

PriorityQueue (asyncio 中的类)

(queue 中的类)

private

names

prlimit() (在 resource 模块中)

prmonth() (calendar.TextCalendar 方法)

(在 calendar 模块中)

ProactorEventLoop (asyncio 中的类)

procedure

call

process

group, [1]

id

id of parent

killing, [1]

scheduling priority, [1]

signalling, [1]

Process (multiprocessing 中的类)

process() (logging.LoggerAdapter 方法)

process_exited() (asyncio.SubprocessProtocol 方法)

process_message() (smtpd.SMTPServer 方法)

process_request() (socketserver.BaseServer 方法)

process_time() (在 time 模块中)

process_tokens() (在 tabnanny 模块中)

ProcessError

processes, light-weight

processingInstruction() (xml.sax.handler.ContentHandler 方法)

ProcessingInstruction() (在 xml.etree.ElementTree 模块中)

ProcessingInstructionHandler() (xml.parsers.expat.xmlparser 方法)

ProcessLookupError

processor time

processor() (在 platform 模块中)

ProcessPoolExecutor (concurrent.futures 中的类)

product() (在 itertools 模块中)

Profile (profile 中的类)

profile (模块)

profile function, [1], [2]

profiler, [1]

profiling, deterministic

program

ProgrammingError

Progressbar (tkinter.ttk 中的类)

prompt (cmd.Cmd 属性)

prompt_user_passwd() (urllib.request.FancyURLopener 方法)

prompts, interpreter

propagate (logging.Logger 属性)

property (內置类)

property list

property_declaration_handler() (在 xml.sax.handler 模块中)

property_dom_node() (在 xml.sax.handler 模块中)

property_lexical_handler() (在 xml.sax.handler 模块中)

property_xml_string() (在 xml.sax.handler 模块中)

PropertyMock (unittest.mock 中的类)

prot_c() (ftplib.FTP_TLS 方法)

prot_p() (ftplib.FTP_TLS 方法)

proto (socket.socket 属性)

protocol

CGI

context management

copy

FTP, [1]

HTTP, [1], [2], [3], [4], [5]

IMAP4

IMAP4_SSL

IMAP4_stream

iterator

NNTP

POP3

SMTP

Telnet

Protocol (asyncio 中的类)

protocol (ssl.SSLContext 属性)

PROTOCOL_SSLv2() (在 ssl 模块中)

PROTOCOL_SSLv23() (在 ssl 模块中)

PROTOCOL_SSLv3() (在 ssl 模块中)

PROTOCOL_TLS() (在 ssl 模块中)

PROTOCOL_TLS_CLIENT() (在 ssl 模块中)

PROTOCOL_TLS_SERVER() (在 ssl 模块中)

PROTOCOL_TLSv1() (在 ssl 模块中)

PROTOCOL_TLSv1_1() (在 ssl 模块中)

PROTOCOL_TLSv1_2() (在 ssl 模块中)

protocol_version (http.server.BaseHTTPRequestHandler 属性)

PROTOCOL_VERSION (imaplib.IMAP4 属性)

ProtocolError (xmlrpc.client 中的类)

provisional API -- 暂定 API

provisional package -- 暂定包

proxy() (在 weakref 模块中)

proxyauth() (imaplib.IMAP4 方法)

ProxyBasicAuthHandler (urllib.request 中的类)

ProxyDigestAuthHandler (urllib.request 中的类)

ProxyHandler (urllib.request 中的类)

ProxyType() (在 weakref 模块中)

ProxyTypes() (在 weakref 模块中)

pryear() (calendar.TextCalendar 方法)

ps1() (在 sys 模块中)

ps2() (在 sys 模块中)

pstats (模块)

pstdev() (在 statistics 模块中)

pthread_kill() (在 signal 模块中)

pthread_sigmask() (在 signal 模块中)

pthreads

pty

模块

pty (模块)

pu() (在 turtle 模块中)

publicId (xml.dom.DocumentType 属性)

PullDom (xml.dom.pulldom 中的类)

punctuation() (在 string 模块中)

punctuation_chars (shlex.shlex 属性)

PurePath (pathlib 中的类)

PurePath.anchor() (在 pathlib 模块中)

PurePath.drive() (在 pathlib 模块中)

PurePath.name() (在 pathlib 模块中)

PurePath.parent() (在 pathlib 模块中)

PurePath.parents() (在 pathlib 模块中)

PurePath.parts() (在 pathlib 模块中)

PurePath.root() (在 pathlib 模块中)

PurePath.stem() (在 pathlib 模块中)

PurePath.suffix() (在 pathlib 模块中)

PurePath.suffixes() (在 pathlib 模块中)

PurePosixPath (pathlib 中的类)

PureProxy (smtpd 中的类)

PureWindowsPath (pathlib 中的类)

purge() (在 re 模块中)

Purpose.CLIENT_AUTH() (在 ssl 模块中)

Purpose.SERVER_AUTH() (在 ssl 模块中)

push() (asynchat.async_chat 方法)

(code.InteractiveConsole 方法)

(contextlib.ExitStack 方法)

push_alignment() (formatter.formatter 方法)

push_font() (formatter.formatter 方法)

push_margin() (formatter.formatter 方法)

push_source() (shlex.shlex 方法)

push_style() (formatter.formatter 方法)

push_token() (shlex.shlex 方法)

push_with_producer() (asynchat.async_chat 方法)

pushbutton() (msilib.Dialog 方法)

put() (asyncio.Queue 方法)

(multiprocessing.Queue 方法)

(multiprocessing.SimpleQueue 方法)

(queue.Queue 方法)

put_nowait() (asyncio.Queue 方法)

(multiprocessing.Queue 方法)

(queue.Queue 方法)

putch() (在 msvcrt 模块中)

putenv() (在 os 模块中)

putheader() (http.client.HTTPConnection 方法)

putp() (在 curses 模块中)

putrequest() (http.client.HTTPConnection 方法)

putwch() (在 msvcrt 模块中)

putwin() (curses.window 方法)

pvariance() (在 statistics 模块中)

pwd

模块

pwd (模块)

pwd() (ftplib.FTP 方法)

pwrite() (在 os 模块中)

Py_AddPendingCall (C 函数)

Py_AddPendingCall()

Py_AtExit (C 函数)

Py_BEGIN_ALLOW_THREADS

(C 宏)

Py_BLOCK_THREADS (C 宏)

Py_buffer (C 类型)

Py_buffer.buf (C 成员)

Py_buffer.format (C 成员)

Py_buffer.internal (C 成员)

Py_buffer.itemsize (C 成员)

Py_buffer.len (C 成员)

Py_buffer.ndim (C 成员)

Py_buffer.obj (C 成员)

Py_buffer.readonly (C 成员)

Py_buffer.shape (C 成员)

Py_buffer.strides (C 成员)

Py_buffer.suboffsets (C 成员)

Py_BuildValue (C 函数)

Py_CLEAR (C 函数)

py_compile (模块)

PY_COMPILED() (在 imp 模块中)

Py_CompileString (C 函数)

Py_CompileString(), [1], [2]

Py_CompileStringExFlags (C 函数)

Py_CompileStringFlags (C 函数)

Py_CompileStringObject (C 函数)

Py_complex (C 类型)

Py_DecodeLocale (C 函数)

Py_DECREF (C 函数)

Py_DECREF()

Py_Ellipsis (C 变量)

Py_EncodeLocale (C 函数)

Py_END_ALLOW_THREADS

(C 宏)

Py_EndInterpreter (C 函数)

Py_EnterRecursiveCall (C 函数)

Py_eval_input (C 变量)

Py_Exit (C 函数)

Py_False (C 变量)

Py_FatalError (C 函数)

Py_FatalError()

Py_FdIsInteractive (C 函数)

Py_file_input (C 变量)

Py_Finalize (C 函数)

Py_FinalizeEx (C 函数)

Py_FinalizeEx(), [1], [2], [3], [4]

PY_FROZEN() (在 imp 模块中)

Py_GetBuildInfo (C 函数)

Py_GetCompiler (C 函数)

Py_GetCopyright (C 函数)

Py_GetExecPrefix (C 函数)

Py_GetExecPrefix()

Py_GetPath (C 函数)

Py_GetPath(), [1], [2]

Py_GetPlatform (C 函数)

Py_GetPrefix (C 函数)

Py_GetPrefix()

Py_GetProgramFullPath (C 函数)

Py_GetProgramFullPath()

Py_GetProgramName (C 函数)

Py_GetPythonHome (C 函数)

Py_GetVersion (C 函数)

Py_INCREF (C 函数)

Py_INCREF()

Py_Initialize (C 函数)

Py_Initialize(), [1], [2], [3]

Py_InitializeEx (C 函数)

Py_IsInitialized (C 函数)

Py_IsInitialized()

Py_LeaveRecursiveCall (C 函数)

Py_Main (C 函数)

Py_mod_create (C 变量)

Py_mod_exec (C 变量)

Py_NewInterpreter (C 函数)

Py_None (C 变量)

Py_NotImplemented (C 变量)

py_object (ctypes 中的类)

Py_PRINT_RAW

Py_REFCNT (C 宏)

Py_ReprEnter (C 函数)

Py_ReprLeave (C 函数)

Py_RETURN_FALSE (C 宏)

Py_RETURN_NONE (C 宏)

Py_RETURN_NOTIMPLEMENTED (C 宏)

Py_RETURN_TRUE (C 宏)

Py_SetPath (C 函数)

Py_SetPath()

Py_SetProgramName (C 函数)

Py_SetProgramName(), [1], [2], [3]

Py_SetPythonHome (C 函数)

Py_SetStandardStreamEncoding (C 函数)

Py_single_input (C 变量)

Py_SIZE (C 宏)

PY_SOURCE() (在 imp 模块中)

PY_SSIZE_T_MAX

Py_TPFLAGS_BASE_EXC_SUBCLASS (內置变量)

Py_TPFLAGS_BASETYPE (內置变量)

Py_TPFLAGS_BYTES_SUBCLASS (內置变量)

Py_TPFLAGS_DEFAULT (內置变量)

Py_TPFLAGS_DICT_SUBCLASS (內置变量)

Py_TPFLAGS_HAVE_FINALIZE (內置变量)

Py_TPFLAGS_HAVE_GC (內置变量)

Py_TPFLAGS_HEAPTYPE (內置变量)

Py_TPFLAGS_LIST_SUBCLASS (內置变量)

Py_TPFLAGS_LONG_SUBCLASS (內置变量)

Py_TPFLAGS_READY (內置变量)

Py_TPFLAGS_READYING (內置变量)

Py_TPFLAGS_TUPLE_SUBCLASS (內置变量)

Py_TPFLAGS_TYPE_SUBCLASS (內置变量)

Py_TPFLAGS_UNICODE_SUBCLASS (內置变量)

Py_tracefunc (C 类型)

Py_True (C 变量)

Py_TYPE (C 宏)

Py_UCS1 (C 类型)

Py_UCS2 (C 类型)

Py_UCS4 (C 类型)

Py_UNBLOCK_THREADS (C 宏)

Py_UNICODE (C 类型)

Py_UNICODE_IS_HIGH_SURROGATE (C 宏)

Py_UNICODE_IS_LOW_SURROGATE (C 宏)

Py_UNICODE_IS_SURROGATE (C 宏)

Py_UNICODE_ISALNUM (C 函数)

Py_UNICODE_ISALPHA (C 函数)

Py_UNICODE_ISDECIMAL (C 函数)

Py_UNICODE_ISDIGIT (C 函数)

Py_UNICODE_ISLINEBREAK (C 函数)

Py_UNICODE_ISLOWER (C 函数)

Py_UNICODE_ISNUMERIC (C 函数)

Py_UNICODE_ISPRINTABLE (C 函数)

Py_UNICODE_ISSPACE (C 函数)

Py_UNICODE_ISTITLE (C 函数)

Py_UNICODE_ISUPPER (C 函数)

Py_UNICODE_JOIN_SURROGATES (C 宏)

Py_UNICODE_TODECIMAL (C 函数)

Py_UNICODE_TODIGIT (C 函数)

Py_UNICODE_TOLOWER (C 函数)

Py_UNICODE_TONUMERIC (C 函数)

Py_UNICODE_TOTITLE (C 函数)

Py_UNICODE_TOUPPER (C 函数)

Py_VaBuildValue (C 函数)

Py_VISIT (C 函数)

Py_XDECREF (C 函数)

Py_XDECREF()

Py_XINCREF (C 函数)

PyAnySet_Check (C 函数)

PyAnySet_CheckExact (C 函数)

PyArg_Parse (C 函数)

PyArg_ParseTuple (C 函数)

PyArg_ParseTuple()

PyArg_ParseTupleAndKeywords (C 函数)

PyArg_ParseTupleAndKeywords()

PyArg_UnpackTuple (C 函数)

PyArg_ValidateKeywordArguments (C 函数)

PyArg_VaParse (C 函数)

PyArg_VaParseTupleAndKeywords (C 函数)

PyASCIIObject (C 类型)

PyAsyncMethods (C 类型)

PyAsyncMethods.am_aiter (C 成员)

PyAsyncMethods.am_anext (C 成员)

PyAsyncMethods.am_await (C 成员)

PyBool_Check (C 函数)

PyBool_FromLong (C 函数)

PyBUF_ANY_CONTIGUOUS (C 宏)

PyBUF_C_CONTIGUOUS (C 宏)

PyBUF_CONTIG (C 宏)

PyBUF_CONTIG_RO (C 宏)

PyBUF_F_CONTIGUOUS (C 宏)

PyBUF_FORMAT (C 宏)

PyBUF_FULL (C 宏)

PyBUF_FULL_RO (C 宏)

PyBUF_INDIRECT (C 宏)

PyBUF_ND (C 宏), [1]

PyBUF_RECORDS (C 宏)

PyBUF_RECORDS_RO (C 宏)

PyBUF_SIMPLE (C 宏)

PyBUF_STRIDED (C 宏)

PyBUF_STRIDED_RO (C 宏)

PyBUF_STRIDES (C 宏)

PyBUF_WRITABLE (C 宏)

PyBuffer_FillContiguousStrides (C 函数)

PyBuffer_FillInfo (C 函数)

PyBuffer_IsContiguous (C 函数)

PyBuffer_Release (C 函数)

PyBuffer_SizeFromFormat (C 函数)

PyBuffer_ToContiguous (C 函数)

PyBufferProcs

(C 类型)

PyBufferProcs.bf_getbuffer (C 成员)

PyBufferProcs.bf_releasebuffer (C 成员)

PyByteArray_AS_STRING (C 函数)

PyByteArray_AsString (C 函数)

PyByteArray_Check (C 函数)

PyByteArray_CheckExact (C 函数)

PyByteArray_Concat (C 函数)

PyByteArray_FromObject (C 函数)

PyByteArray_FromStringAndSize (C 函数)

PyByteArray_GET_SIZE (C 函数)

PyByteArray_Resize (C 函数)

PyByteArray_Size (C 函数)

PyByteArray_Type (C 变量)

PyByteArrayObject (C 类型)

PyBytes_AS_STRING (C 函数)

PyBytes_AsString (C 函数)

PyBytes_AsStringAndSize (C 函数)

PyBytes_Check (C 函数)

PyBytes_CheckExact (C 函数)

PyBytes_Concat (C 函数)

PyBytes_ConcatAndDel (C 函数)

PyBytes_FromFormat (C 函数)

PyBytes_FromFormatV (C 函数)

PyBytes_FromObject (C 函数)

PyBytes_FromString (C 函数)

PyBytes_FromStringAndSize (C 函数)

PyBytes_GET_SIZE (C 函数)

PyBytes_Size (C 函数)

PyBytes_Type (C 变量)

PyBytesObject (C 类型)

PyCallable_Check (C 函数)

PyCallIter_Check (C 函数)

PyCallIter_New (C 函数)

PyCallIter_Type (C 变量)

PyCapsule (C 类型)

PyCapsule_CheckExact (C 函数)

PyCapsule_Destructor (C 类型)

PyCapsule_GetContext (C 函数)

PyCapsule_GetDestructor (C 函数)

PyCapsule_GetName (C 函数)

PyCapsule_GetPointer (C 函数)

PyCapsule_Import (C 函数)

PyCapsule_IsValid (C 函数)

PyCapsule_New (C 函数)

PyCapsule_SetContext (C 函数)

PyCapsule_SetDestructor (C 函数)

PyCapsule_SetName (C 函数)

PyCapsule_SetPointer (C 函数)

PyCell_Check (C 函数)

PyCell_GET (C 函数)

PyCell_Get (C 函数)

PyCell_New (C 函数)

PyCell_SET (C 函数)

PyCell_Set (C 函数)

PyCell_Type (C 变量)

PyCellObject (C 类型)

PyCFunction (C 类型)

PyCFunctionWithKeywords (C 类型)

pyclbr (模块)

PyCode_Check (C 函数)

PyCode_GetNumFree (C 函数)

PyCode_New (C 函数)

PyCode_NewEmpty (C 函数)

PyCode_Type (C 变量)

PyCodec_BackslashReplaceErrors (C 函数)

PyCodec_Decode (C 函数)

PyCodec_Decoder (C 函数)

PyCodec_Encode (C 函数)

PyCodec_Encoder (C 函数)

PyCodec_IgnoreErrors (C 函数)

PyCodec_IncrementalDecoder (C 函数)

PyCodec_IncrementalEncoder (C 函数)

PyCodec_KnownEncoding (C 函数)

PyCodec_LookupError (C 函数)

PyCodec_NameReplaceErrors (C 函数)

PyCodec_Register (C 函数)

PyCodec_RegisterError (C 函数)

PyCodec_ReplaceErrors (C 函数)

PyCodec_StreamReader (C 函数)

PyCodec_StreamWriter (C 函数)

PyCodec_StrictErrors (C 函数)

PyCodec_XMLCharRefReplaceErrors (C 函数)

PyCodeObject (C 类型)

PyCompactUnicodeObject (C 类型)

PyCompileError

PyCompilerFlags (C 类型)

PyComplex_AsCComplex (C 函数)

PyComplex_Check (C 函数)

PyComplex_CheckExact (C 函数)

PyComplex_FromCComplex (C 函数)

PyComplex_FromDoubles (C 函数)

PyComplex_ImagAsDouble (C 函数)

PyComplex_RealAsDouble (C 函数)

PyComplex_Type (C 变量)

PyComplexObject (C 类型)

PyCoro_CheckExact (C 函数)

PyCoro_New (C 函数)

PyCoro_Type (C 变量)

PyCoroObject (C 类型)

PyDate_Check (C 函数)

PyDate_CheckExact (C 函数)

PyDate_FromDate (C 函数)

PyDate_FromTimestamp (C 函数)

PyDateTime_Check (C 函数)

PyDateTime_CheckExact (C 函数)

PyDateTime_DATE_GET_HOUR (C 函数)

PyDateTime_DATE_GET_MICROSECOND (C 函数)

PyDateTime_DATE_GET_MINUTE (C 函数)

PyDateTime_DATE_GET_SECOND (C 函数)

PyDateTime_DELTA_GET_DAYS (C 函数)

PyDateTime_DELTA_GET_MICROSECONDS (C 函数)

PyDateTime_DELTA_GET_SECONDS (C 函数)

PyDateTime_FromDateAndTime (C 函数)

PyDateTime_FromTimestamp (C 函数)

PyDateTime_GET_DAY (C 函数)

PyDateTime_GET_MONTH (C 函数)

PyDateTime_GET_YEAR (C 函数)

PyDateTime_TIME_GET_HOUR (C 函数)

PyDateTime_TIME_GET_MICROSECOND (C 函数)

PyDateTime_TIME_GET_MINUTE (C 函数)

PyDateTime_TIME_GET_SECOND (C 函数)

PyDelta_Check (C 函数)

PyDelta_CheckExact (C 函数)

PyDelta_FromDSU (C 函数)

PyDescr_IsData (C 函数)

PyDescr_NewClassMethod (C 函数)

PyDescr_NewGetSet (C 函数)

PyDescr_NewMember (C 函数)

PyDescr_NewMethod (C 函数)

PyDescr_NewWrapper (C 函数)

PyDict_Check (C 函数)

PyDict_CheckExact (C 函数)

PyDict_Clear (C 函数)

PyDict_ClearFreeList (C 函数)

PyDict_Contains (C 函数)

PyDict_Copy (C 函数)

PyDict_DelItem (C 函数)

PyDict_DelItemString (C 函数)

PyDict_GetItem (C 函数)

PyDict_GetItemString (C 函数)

PyDict_GetItemWithError (C 函数)

PyDict_Items (C 函数)

PyDict_Keys (C 函数)

PyDict_Merge (C 函数)

PyDict_MergeFromSeq2 (C 函数)

PyDict_New (C 函数)

PyDict_Next (C 函数)

PyDict_SetDefault (C 函数)

PyDict_SetItem (C 函数)

PyDict_SetItemString (C 函数)

PyDict_Size (C 函数)

PyDict_Type (C 变量)

PyDict_Update (C 函数)

PyDict_Values (C 函数)

PyDictObject (C 类型)

PyDictProxy_New (C 函数)

PyDLL (ctypes 中的类)

pydoc (模块)

PyErr_BadArgument (C 函数)

PyErr_BadInternalCall (C 函数)

PyErr_CheckSignals (C 函数)

PyErr_Clear (C 函数)

PyErr_Clear(), [1]

PyErr_ExceptionMatches (C 函数)

PyErr_ExceptionMatches()

PyErr_Fetch (C 函数)

PyErr_Fetch()

PyErr_Format (C 函数)

PyErr_FormatV (C 函数)

PyErr_GetExcInfo (C 函数)

PyErr_GivenExceptionMatches (C 函数)

PyErr_NewException (C 函数)

PyErr_NewExceptionWithDoc (C 函数)

PyErr_NoMemory (C 函数)

PyErr_NormalizeException (C 函数)

PyErr_Occurred (C 函数)

PyErr_Occurred()

PyErr_Print (C 函数)

PyErr_PrintEx (C 函数)

PyErr_ResourceWarning (C 函数)

PyErr_Restore (C 函数)

PyErr_Restore()

PyErr_SetExcFromWindowsErr (C 函数)

PyErr_SetExcFromWindowsErrWithFilename (C 函数)

PyErr_SetExcFromWindowsErrWithFilenameObject (C 函数)

PyErr_SetExcFromWindowsErrWithFilenameObjects (C 函数)

PyErr_SetExcInfo (C 函数)

PyErr_SetFromErrno (C 函数)

PyErr_SetFromErrnoWithFilename (C 函数)

PyErr_SetFromErrnoWithFilenameObject (C 函数)

PyErr_SetFromErrnoWithFilenameObjects (C 函数)

PyErr_SetFromWindowsErr (C 函数)

PyErr_SetFromWindowsErrWithFilename (C 函数)

PyErr_SetImportError (C 函数)

PyErr_SetImportErrorSubclass (C 函数)

PyErr_SetInterrupt (C 函数)

PyErr_SetNone (C 函数)

PyErr_SetObject (C 函数)

PyErr_SetString (C 函数)

PyErr_SetString()

PyErr_SyntaxLocation (C 函数)

PyErr_SyntaxLocationEx (C 函数)

PyErr_SyntaxLocationObject (C 函数)

PyErr_WarnEx (C 函数)

PyErr_WarnExplicit (C 函数)

PyErr_WarnExplicitObject (C 函数)

PyErr_WarnFormat (C 函数)

PyErr_WriteUnraisable (C 函数)

PyEval_AcquireLock (C 函数)

PyEval_AcquireThread (C 函数)

PyEval_AcquireThread()

PyEval_EvalCode (C 函数)

PyEval_EvalCodeEx (C 函数)

PyEval_EvalFrame (C 函数)

PyEval_EvalFrameEx (C 函数)

PyEval_GetBuiltins (C 函数)

PyEval_GetCallStats (C 函数)

PyEval_GetFrame (C 函数)

PyEval_GetFuncDesc (C 函数)

PyEval_GetFuncName (C 函数)

PyEval_GetGlobals (C 函数)

PyEval_GetLocals (C 函数)

PyEval_InitThreads (C 函数)

PyEval_InitThreads()

PyEval_MergeCompilerFlags (C 函数)

PyEval_ReInitThreads (C 函数)

PyEval_ReleaseLock (C 函数)

PyEval_ReleaseThread (C 函数)

PyEval_ReleaseThread()

PyEval_RestoreThread (C 函数)

PyEval_RestoreThread(), [1]

PyEval_SaveThread (C 函数)

PyEval_SaveThread(), [1]

PyEval_SetProfile (C 函数)

PyEval_SetTrace (C 函数)

PyEval_ThreadsInitialized (C 函数)

PyExc_ArithmeticError

PyExc_AssertionError

PyExc_AttributeError

PyExc_BaseException

PyExc_BlockingIOError

PyExc_BrokenPipeError

PyExc_BufferError

PyExc_BytesWarning

PyExc_ChildProcessError

PyExc_ConnectionAbortedError

PyExc_ConnectionError

PyExc_ConnectionRefusedError

PyExc_ConnectionResetError

PyExc_DeprecationWarning

PyExc_EnvironmentError

PyExc_EOFError

PyExc_Exception

PyExc_FileExistsError

PyExc_FileNotFoundError

PyExc_FloatingPointError

PyExc_FutureWarning

PyExc_GeneratorExit

PyExc_ImportError

PyExc_ImportWarning

PyExc_IndentationError

PyExc_IndexError

PyExc_InterruptedError

PyExc_IOError

PyExc_IsADirectoryError

PyExc_KeyboardInterrupt

PyExc_KeyError

PyExc_LookupError

PyExc_MemoryError

PyExc_ModuleNotFoundError

PyExc_NameError

PyExc_NotADirectoryError

PyExc_NotImplementedError

PyExc_OSError

PyExc_OverflowError

PyExc_PendingDeprecationWarning

PyExc_PermissionError

PyExc_ProcessLookupError

PyExc_RecursionError

PyExc_ReferenceError

PyExc_ResourceWarning

PyExc_RuntimeError

PyExc_RuntimeWarning

PyExc_StopAsyncIteration

PyExc_StopIteration

PyExc_SyntaxError

PyExc_SyntaxWarning

PyExc_SystemError

PyExc_SystemExit

PyExc_TabError

PyExc_TimeoutError

PyExc_TypeError

PyExc_UnboundLocalError

PyExc_UnicodeDecodeError

PyExc_UnicodeEncodeError

PyExc_UnicodeError

PyExc_UnicodeTranslateError

PyExc_UnicodeWarning

PyExc_UserWarning

PyExc_ValueError

PyExc_Warning

PyExc_WindowsError

PyExc_ZeroDivisionError

PyException_GetCause (C 函数)

PyException_GetContext (C 函数)

PyException_GetTraceback (C 函数)

PyException_SetCause (C 函数)

PyException_SetContext (C 函数)

PyException_SetTraceback (C 函数)

pyexpat

模块

PyFile_FromFd (C 函数)

PyFile_GetLine (C 函数)

PyFile_WriteObject (C 函数)

PyFile_WriteString (C 函数)

PyFloat_AS_DOUBLE (C 函数)

PyFloat_AsDouble (C 函数)

PyFloat_Check (C 函数)

PyFloat_CheckExact (C 函数)

PyFloat_ClearFreeList (C 函数)

PyFloat_FromDouble (C 函数)

PyFloat_FromString (C 函数)

PyFloat_GetInfo (C 函数)

PyFloat_GetMax (C 函数)

PyFloat_GetMin (C 函数)

PyFloat_Type (C 变量)

PyFloatObject (C 类型)

PyFrame_GetLineNumber (C 函数)

PyFrameObject (C 类型)

PyFrozenSet_Check (C 函数)

PyFrozenSet_CheckExact (C 函数)

PyFrozenSet_New (C 函数)

PyFrozenSet_Type (C 变量)

PyFunction_Check (C 函数)

PyFunction_GetAnnotations (C 函数)

PyFunction_GetClosure (C 函数)

PyFunction_GetCode (C 函数)

PyFunction_GetDefaults (C 函数)

PyFunction_GetGlobals (C 函数)

PyFunction_GetModule (C 函数)

PyFunction_New (C 函数)

PyFunction_NewWithQualName (C 函数)

PyFunction_SetAnnotations (C 函数)

PyFunction_SetClosure (C 函数)

PyFunction_SetDefaults (C 函数)

PyFunction_Type (C 变量)

PyFunctionObject (C 类型)

PYFUNCTYPE() (在 ctypes 模块中)

PyGen_Check (C 函数)

PyGen_CheckExact (C 函数)

PyGen_New (C 函数)

PyGen_NewWithQualName (C 函数)

PyGen_Type (C 变量)

PyGenObject (C 类型)

PyGetSetDef (C 类型)

PyGILState_Check (C 函数)

PyGILState_Ensure (C 函数)

PyGILState_GetThisThreadState (C 函数)

PyGILState_Release (C 函数)

PyImport_AddModule (C 函数)

PyImport_AddModuleObject (C 函数)

PyImport_AppendInittab (C 函数)

PyImport_Cleanup (C 函数)

PyImport_ExecCodeModule (C 函数)

PyImport_ExecCodeModuleEx (C 函数)

PyImport_ExecCodeModuleObject (C 函数)

PyImport_ExecCodeModuleWithPathnames (C 函数)

PyImport_ExtendInittab (C 函数)

PyImport_FrozenModules (C 变量)

PyImport_GetImporter (C 函数)

PyImport_GetMagicNumber (C 函数)

PyImport_GetMagicTag (C 函数)

PyImport_GetModuleDict (C 函数)

PyImport_Import (C 函数)

PyImport_ImportFrozenModule (C 函数)

PyImport_ImportFrozenModuleObject (C 函数)

PyImport_ImportModule (C 函数)

PyImport_ImportModuleEx (C 函数)

PyImport_ImportModuleLevel (C 函数)

PyImport_ImportModuleLevelObject (C 函数)

PyImport_ImportModuleNoBlock (C 函数)

PyImport_ReloadModule (C 函数)

PyIndex_Check (C 函数)

PyInit_modulename (C 函数)

PyInstanceMethod_Check (C 函数)

PyInstanceMethod_Function (C 函数)

PyInstanceMethod_GET_FUNCTION (C 函数)

PyInstanceMethod_New (C 函数)

PyInstanceMethod_Type (C 变量)

PyInterpreterState (C 类型)

PyInterpreterState_Clear (C 函数)

PyInterpreterState_Delete (C 函数)

PyInterpreterState_Head (C 函数)

PyInterpreterState_New (C 函数)

PyInterpreterState_Next (C 函数)

PyInterpreterState_ThreadHead (C 函数)

PyIter_Check (C 函数)

PyIter_Next (C 函数)

PyList_Append (C 函数)

PyList_AsTuple (C 函数)

PyList_Check (C 函数)

PyList_CheckExact (C 函数)

PyList_ClearFreeList (C 函数)

PyList_GET_ITEM (C 函数)

PyList_GET_SIZE (C 函数)

PyList_GetItem (C 函数)

PyList_GetItem()

PyList_GetSlice (C 函数)

PyList_Insert (C 函数)

PyList_New (C 函数)

PyList_Reverse (C 函数)

PyList_SET_ITEM (C 函数)

PyList_SetItem (C 函数)

PyList_SetItem()

PyList_SetSlice (C 函数)

PyList_Size (C 函数)

PyList_Sort (C 函数)

PyList_Type (C 变量)

PyListObject (C 类型)

PyLong_AsDouble (C 函数)

PyLong_AsLong (C 函数)

PyLong_AsLongAndOverflow (C 函数)

PyLong_AsLongLong (C 函数)

PyLong_AsLongLongAndOverflow (C 函数)

PyLong_AsSize_t (C 函数)

PyLong_AsSsize_t (C 函数)

PyLong_AsUnsignedLong (C 函数)

PyLong_AsUnsignedLongLong (C 函数)

PyLong_AsUnsignedLongLongMask (C 函数)

PyLong_AsUnsignedLongMask (C 函数)

PyLong_AsVoidPtr (C 函数)

PyLong_Check (C 函数)

PyLong_CheckExact (C 函数)

PyLong_FromDouble (C 函数)

PyLong_FromLong (C 函数)

PyLong_FromLongLong (C 函数)

PyLong_FromSize_t (C 函数)

PyLong_FromSsize_t (C 函数)

PyLong_FromString (C 函数)

PyLong_FromUnicode (C 函数)

PyLong_FromUnicodeObject (C 函数)

PyLong_FromUnsignedLong (C 函数)

PyLong_FromUnsignedLongLong (C 函数)

PyLong_FromVoidPtr (C 函数)

PyLong_Type (C 变量)

PyLongObject (C 类型)

PyMapping_Check (C 函数)

PyMapping_DelItem (C 函数)

PyMapping_DelItemString (C 函数)

PyMapping_GetItemString (C 函数)

PyMapping_HasKey (C 函数)

PyMapping_HasKeyString (C 函数)

PyMapping_Items (C 函数)

PyMapping_Keys (C 函数)

PyMapping_Length (C 函数)

PyMapping_SetItemString (C 函数)

PyMapping_Size (C 函数)

PyMapping_Values (C 函数)

PyMappingMethods (C 类型)

PyMappingMethods.mp_ass_subscript (C 成员)

PyMappingMethods.mp_length (C 成员)

PyMappingMethods.mp_subscript (C 成员)

PyMarshal_ReadLastObjectFromFile (C 函数)

PyMarshal_ReadLongFromFile (C 函数)

PyMarshal_ReadObjectFromFile (C 函数)

PyMarshal_ReadObjectFromString (C 函数)

PyMarshal_ReadShortFromFile (C 函数)

PyMarshal_WriteLongToFile (C 函数)

PyMarshal_WriteObjectToFile (C 函数)

PyMarshal_WriteObjectToString (C 函数)

PyMem_Calloc (C 函数)

PyMem_Del (C 函数)

PYMEM_DOMAIN_MEM (C 变量)

PYMEM_DOMAIN_OBJ (C 变量)

PYMEM_DOMAIN_RAW (C 变量)

PyMem_Free (C 函数)

PyMem_GetAllocator (C 函数)

PyMem_Malloc (C 函数)

PyMem_New (C 函数)

PyMem_RawCalloc (C 函数)

PyMem_RawFree (C 函数)

PyMem_RawMalloc (C 函数)

PyMem_RawRealloc (C 函数)

PyMem_Realloc (C 函数)

PyMem_Resize (C 函数)

PyMem_SetAllocator (C 函数)

PyMem_SetupDebugHooks (C 函数)

PyMemAllocatorDomain (C 类型)

PyMemAllocatorEx (C 类型)

PyMemberDef (C 类型)

PyMemoryView_Check (C 函数)

PyMemoryView_FromBuffer (C 函数)

PyMemoryView_FromMemory (C 函数)

PyMemoryView_FromObject (C 函数)

PyMemoryView_GET_BASE (C 函数)

PyMemoryView_GET_BUFFER (C 函数)

PyMemoryView_GetContiguous (C 函数)

PyMethod_Check (C 函数)

PyMethod_ClearFreeList (C 函数)

PyMethod_Function (C 函数)

PyMethod_GET_FUNCTION (C 函数)

PyMethod_GET_SELF (C 函数)

PyMethod_New (C 函数)

PyMethod_Self (C 函数)

PyMethod_Type (C 变量)

PyMethodDef (C 类型)

PyModule_AddFunctions (C 函数)

PyModule_AddIntConstant (C 函数)

PyModule_AddIntMacro (C 函数)

PyModule_AddObject (C 函数)

PyModule_AddStringConstant (C 函数)

PyModule_AddStringMacro (C 函数)

PyModule_Check (C 函数)

PyModule_CheckExact (C 函数)

PyModule_Create (C 函数)

PyModule_Create2 (C 函数)

PyModule_ExecDef (C 函数)

PyModule_FromDefAndSpec (C 函数)

PyModule_FromDefAndSpec2 (C 函数)

PyModule_GetDef (C 函数)

PyModule_GetDict (C 函数)

PyModule_GetFilename (C 函数)

PyModule_GetFilenameObject (C 函数)

PyModule_GetName (C 函数)

PyModule_GetNameObject (C 函数)

PyModule_GetState (C 函数)

PyModule_New (C 函数)

PyModule_NewObject (C 函数)

PyModule_SetDocString (C 函数)

PyModule_Type (C 变量)

PyModuleDef (C 类型)

PyModuleDef.m_base (C 成员)

PyModuleDef.m_clear (C 成员)

PyModuleDef.m_doc (C 成员)

PyModuleDef.m_free (C 成员)

PyModuleDef.m_methods (C 成员)

PyModuleDef.m_name (C 成员)

PyModuleDef.m_reload (C 成员)

PyModuleDef.m_size (C 成员)

PyModuleDef.m_slots (C 成员)

PyModuleDef.m_traverse (C 成员)

PyModuleDef_Init (C 函数)

PyModuleDef_Slot (C 类型)

PyModuleDef_Slot.slot (C 成员)

PyModuleDef_Slot.value (C 成员)

PyNumber_Absolute (C 函数)

PyNumber_Add (C 函数)

PyNumber_And (C 函数)

PyNumber_AsSsize_t (C 函数)

PyNumber_Check (C 函数)

PyNumber_Divmod (C 函数)

PyNumber_Float (C 函数)

PyNumber_FloorDivide (C 函数)

PyNumber_Index (C 函数)

PyNumber_InPlaceAdd (C 函数)

PyNumber_InPlaceAnd (C 函数)

PyNumber_InPlaceFloorDivide (C 函数)

PyNumber_InPlaceLshift (C 函数)

PyNumber_InPlaceMatrixMultiply (C 函数)

PyNumber_InPlaceMultiply (C 函数)

PyNumber_InPlaceOr (C 函数)

PyNumber_InPlacePower (C 函数)

PyNumber_InPlaceRemainder (C 函数)

PyNumber_InPlaceRshift (C 函数)

PyNumber_InPlaceSubtract (C 函数)

PyNumber_InPlaceTrueDivide (C 函数)

PyNumber_InPlaceXor (C 函数)

PyNumber_Invert (C 函数)

PyNumber_Long (C 函数)

PyNumber_Lshift (C 函数)

PyNumber_MatrixMultiply (C 函数)

PyNumber_Multiply (C 函数)

PyNumber_Negative (C 函数)

PyNumber_Or (C 函数)

PyNumber_Positive (C 函数)

PyNumber_Power (C 函数)

PyNumber_Remainder (C 函数)

PyNumber_Rshift (C 函数)

PyNumber_Subtract (C 函数)

PyNumber_ToBase (C 函数)

PyNumber_TrueDivide (C 函数)

PyNumber_Xor (C 函数)

PyNumberMethods (C 类型)

PyObject (C 类型)

PyObject._ob_next (C 成员)

PyObject._ob_prev (C 成员)

PyObject.ob_refcnt (C 成员)

PyObject.ob_type (C 成员)

PyObject_AsCharBuffer (C 函数)

PyObject_ASCII (C 函数)

PyObject_AsFileDescriptor (C 函数)

PyObject_AsReadBuffer (C 函数)

PyObject_AsWriteBuffer (C 函数)

PyObject_Bytes (C 函数)

PyObject_Call (C 函数)

PyObject_CallFunction (C 函数)

PyObject_CallFunctionObjArgs (C 函数)

PyObject_CallMethod (C 函数)

PyObject_CallMethodObjArgs (C 函数)

PyObject_CallObject (C 函数)

PyObject_CallObject()

PyObject_Calloc (C 函数)

PyObject_CheckBuffer (C 函数)

PyObject_CheckReadBuffer (C 函数)

PyObject_Del (C 函数)

PyObject_DelAttr (C 函数)

PyObject_DelAttrString (C 函数)

PyObject_DelItem (C 函数)

PyObject_Dir (C 函数)

PyObject_Free (C 函数)

PyObject_GC_Del (C 函数)

PyObject_GC_New (C 函数)

PyObject_GC_NewVar (C 函数)

PyObject_GC_Resize (C 函数)

PyObject_GC_Track (C 函数)

PyObject_GC_UnTrack (C 函数)

PyObject_GenericGetAttr (C 函数)

PyObject_GenericGetDict (C 函数)

PyObject_GenericSetAttr (C 函数)

PyObject_GenericSetDict (C 函数)

PyObject_GetArenaAllocator (C 函数)

PyObject_GetAttr (C 函数)

PyObject_GetAttrString (C 函数)

PyObject_GetBuffer (C 函数)

PyObject_GetItem (C 函数)

PyObject_GetIter (C 函数)

PyObject_HasAttr (C 函数)

PyObject_HasAttrString (C 函数)

PyObject_Hash (C 函数)

PyObject_HashNotImplemented (C 函数)

PyObject_HEAD (C 宏)

PyObject_HEAD_INIT (C 宏)

PyObject_Init (C 函数)

PyObject_InitVar (C 函数)

PyObject_IsInstance (C 函数)

PyObject_IsSubclass (C 函数)

PyObject_IsTrue (C 函数)

PyObject_Length (C 函数)

PyObject_LengthHint (C 函数)

PyObject_Malloc (C 函数)

PyObject_New (C 函数)

PyObject_NewVar (C 函数)

PyObject_Not (C 函数)

PyObject_Print (C 函数)

PyObject_Realloc (C 函数)

PyObject_Repr (C 函数)

PyObject_RichCompare (C 函数)

PyObject_RichCompareBool (C 函数)

PyObject_SetArenaAllocator (C 函数)

PyObject_SetAttr (C 函数)

PyObject_SetAttrString (C 函数)

PyObject_SetItem (C 函数)

PyObject_Size (C 函数)

PyObject_Str (C 函数)

PyObject_Type (C 函数)

PyObject_TypeCheck (C 函数)

PyObject_VAR_HEAD (C 宏)

PyObjectArenaAllocator (C 类型)

PyOS_AfterFork (C 函数)

PyOS_CheckStack (C 函数)

PyOS_double_to_string (C 函数)

PyOS_FSPath (C 函数)

PyOS_getsig (C 函数)

PyOS_InputHook (C 变量)

PyOS_ReadlineFunctionPointer (C 变量)

PyOS_setsig (C 函数)

PyOS_snprintf (C 函数)

PyOS_stricmp (C 函数)

PyOS_string_to_double (C 函数)

PyOS_strnicmp (C 函数)

PyOS_vsnprintf (C 函数)

PyParser_SimpleParseFile (C 函数)

PyParser_SimpleParseFileFlags (C 函数)

PyParser_SimpleParseString (C 函数)

PyParser_SimpleParseStringFlags (C 函数)

PyParser_SimpleParseStringFlagsFilename (C 函数)

PyPI

(see Python Package Index (PyPI))

PyProperty_Type (C 变量)

PyRun_AnyFile (C 函数)

PyRun_AnyFileEx (C 函数)

PyRun_AnyFileExFlags (C 函数)

PyRun_AnyFileFlags (C 函数)

PyRun_File (C 函数)

PyRun_FileEx (C 函数)

PyRun_FileExFlags (C 函数)

PyRun_FileFlags (C 函数)

PyRun_InteractiveLoop (C 函数)

PyRun_InteractiveLoopFlags (C 函数)

PyRun_InteractiveOne (C 函数)

PyRun_InteractiveOneFlags (C 函数)

PyRun_SimpleFile (C 函数)

PyRun_SimpleFileEx (C 函数)

PyRun_SimpleFileExFlags (C 函数)

PyRun_SimpleString (C 函数)

PyRun_SimpleStringFlags (C 函数)

PyRun_String (C 函数)

PyRun_StringFlags (C 函数)

PySeqIter_Check (C 函数)

PySeqIter_New (C 函数)

PySeqIter_Type (C 变量)

PySequence_Check (C 函数)

PySequence_Concat (C 函数)

PySequence_Contains (C 函数)

PySequence_Count (C 函数)

PySequence_DelItem (C 函数)

PySequence_DelSlice (C 函数)

PySequence_Fast (C 函数)

PySequence_Fast_GET_ITEM (C 函数)

PySequence_Fast_GET_SIZE (C 函数)

PySequence_Fast_ITEMS (C 函数)

PySequence_GetItem (C 函数)

PySequence_GetItem()

PySequence_GetSlice (C 函数)

PySequence_Index (C 函数)

PySequence_InPlaceConcat (C 函数)

PySequence_InPlaceRepeat (C 函数)

PySequence_ITEM (C 函数)

PySequence_Length (C 函数)

PySequence_List (C 函数)

PySequence_Repeat (C 函数)

PySequence_SetItem (C 函数)

PySequence_SetSlice (C 函数)

PySequence_Size (C 函数)

PySequence_Tuple (C 函数)

PySequenceMethods (C 类型)

PySequenceMethods.sq_ass_item (C 成员)

PySequenceMethods.sq_concat (C 成员)

PySequenceMethods.sq_contains (C 成员)

PySequenceMethods.sq_inplace_concat (C 成员)

PySequenceMethods.sq_inplace_repeat (C 成员)

PySequenceMethods.sq_item (C 成员)

PySequenceMethods.sq_length (C 成员)

PySequenceMethods.sq_repeat (C 成员)

PySet_Add (C 函数)

PySet_Check (C 函数)

PySet_Clear (C 函数)

PySet_ClearFreeList (C 函数)

PySet_Contains (C 函数)

PySet_Discard (C 函数)

PySet_GET_SIZE (C 函数)

PySet_New (C 函数)

PySet_Pop (C 函数)

PySet_Size (C 函数)

PySet_Type (C 变量)

PySetObject (C 类型)

PySignal_SetWakeupFd (C 函数)

PySlice_Check (C 函数)

PySlice_GetIndices (C 函数)

PySlice_GetIndicesEx (C 函数)

PySlice_New (C 函数)

PySlice_Type (C 变量)

PyState_AddModule (C 函数)

PyState_FindModule (C 函数)

PyState_RemoveModule (C 函数)

PyStructSequence_Desc (C 类型)

PyStructSequence_Field (C 类型)

PyStructSequence_GET_ITEM (C 函数)

PyStructSequence_GetItem (C 函数)

PyStructSequence_InitType (C 函数)

PyStructSequence_InitType2 (C 函数)

PyStructSequence_New (C 函数)

PyStructSequence_NewType (C 函数)

PyStructSequence_SET_ITEM (C 函数)

PyStructSequence_SetItem (C 函数)

PyStructSequence_UnnamedField (C 变量)

PySys_AddWarnOption (C 函数)

PySys_AddWarnOptionUnicode (C 函数)

PySys_AddXOption (C 函数)

PySys_FormatStderr (C 函数)

PySys_FormatStdout (C 函数)

PySys_GetObject (C 函数)

PySys_GetXOptions (C 函数)

PySys_ResetWarnOptions (C 函数)

PySys_SetArgv (C 函数)

PySys_SetArgv()

PySys_SetArgvEx (C 函数)

PySys_SetArgvEx(), [1]

PySys_SetObject (C 函数)

PySys_SetPath (C 函数)

PySys_WriteStderr (C 函数)

PySys_WriteStdout (C 函数)

Python 3000

Python Editor

Python Package Index (PyPI)

.pypirc file

Python 提高建议

PEP 1

PEP 100

PEP 11, [1], [2], [3]

PEP 205, [1]

PEP 207

PEP 208

PEP 217

PEP 218, [1], [2]

PEP 227, [1], [2]

PEP 229

PEP 230, [1]

PEP 232

PEP 234

PEP 235

PEP 236, [1]

PEP 237, [1], [2], [3], [4], [5]

PEP 238, [1], [2], [3], [4], [5], [6]

PEP 241

PEP 243

PEP 247

PEP 249, [1], [2], [3]

PEP 252, [1]

PEP 253, [1], [2], [3], [4]

PEP 255, [1], [2], [3], [4], [5]

PEP 261, [1]

PEP 263, [1], [2], [3], [4], [5]

PEP 264

PEP 273, [1], [2]

PEP 274

PEP 275

PEP 277

PEP 278, [1]

PEP 279

PEP 282, [1], [2], [3]

PEP 285, [1]

PEP 288

PEP 289, [1], [2]

PEP 292, [1]

PEP 293

PEP 3000

PEP 301, [1]

PEP 302, [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], [26]

PEP 307, [1], [2], [3]

PEP 308, [1], [2]

PEP 309

PEP 3100

PEP 3101, [1], [2], [3], [4]

PEP 3102

PEP 3104, [1]

PEP 3105, [1], [2]

PEP 3106

PEP 3107, [1], [2], [3]

PEP 3108, [1], [2]

PEP 3109, [1]

PEP 3110, [1], [2]

PEP 3111

PEP 3112, [1]

PEP 3113

PEP 3114

PEP 3115, [1], [2], [3]

PEP 3116, [1], [2]

PEP 3118, [1], [2], [3], [4], [5]

PEP 3119, [1], [2], [3], [4], [5]

PEP 3120, [1], [2]

PEP 3121, [1], [2]

PEP 3123

PEP 3127

PEP 3129, [1]

PEP 3131, [1], [2]

PEP 3132, [1]

PEP 3134, [1], [2]

PEP 3135, [1]

PEP 3137

PEP 3138

PEP 314, [1]

PEP 3141, [1], [2]

PEP 3147, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21]

PEP 3148, [1]

PEP 3149, [1]

PEP 3151, [1], [2], [3], [4], [5]

PEP 3153

PEP 3154, [1], [2]

PEP 3155, [1]

PEP 3156, [1], [2], [3], [4], [5]

PEP 318, [1], [2], [3]

PEP 322, [1]

PEP 324, [1]

PEP 325

PEP 327

PEP 328, [1], [2], [3], [4], [5], [6], [7], [8], [9]

PEP 331

PEP 333, [1]

PEP 3333, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14]

PEP 338, [1], [2], [3]

PEP 339

PEP 341

PEP 342, [1], [2], [3], [4], [5], [6]

PEP 343, [1], [2], [3], [4], [5], [6]

PEP 347

PEP 352, [1], [2]

PEP 353, [1], [2]

PEP 356

PEP 357

PEP 361

PEP 362, [1], [2], [3]

PEP 366, [1], [2], [3], [4], [5], [6]

PEP 370, [1], [2], [3], [4]

PEP 371

PEP 372, [1]

PEP 373

PEP 378, [1], [2]

PEP 380, [1], [2], [3]

PEP 383, [1], [2], [3], [4]

PEP 384, [1]

PEP 385

PEP 389, [1]

PEP 391, [1]

PEP 392

PEP 393, [1], [2], [3], [4], [5], [6], [7], [8], [9]

PEP 395

PEP 397, [1], [2], [3]

PEP 398

PEP 4

PEP 405, [1]

PEP 409

PEP 411, [1], [2], [3], [4]

PEP 412

PEP 414, [1]

PEP 418

PEP 420, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]

PEP 421, [1]

PEP 424, [1]

PEP 427

PEP 428, [1], [2]

PEP 429

PEP 434

PEP 435, [1], [2]

PEP 436, [1], [2]

PEP 441, [1]

PEP 442, [1], [2], [3], [4], [5], [6]

PEP 443, [1], [2]

PEP 445, [1], [2], [3]

PEP 446, [1], [2], [3]

PEP 448, [1], [2], [3], [4]

PEP 450, [1], [2]

PEP 451, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]

PEP 453, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10]

PEP 454, [1], [2]

PEP 456, [1]

PEP 461, [1], [2]

PEP 465, [1], [2]

PEP 466, [1], [2], [3]

PEP 468, [1]

PEP 471, [1]

PEP 475, [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], [26]

PEP 476

PEP 477

PEP 478

PEP 479, [1], [2]

PEP 483, [1]

PEP 484, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16]

PEP 485, [1], [2], [3]

PEP 486, [1]

PEP 487, [1], [2], [3]

PEP 488, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]

PEP 489, [1], [2], [3], [4], [5], [6], [7], [8], [9]

PEP 492, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20]

PEP 493

PEP 494

PEP 495, [1]

PEP 498, [1], [2], [3]

PEP 5, [1]

PEP 506, [1]

PEP 511

PEP 515, [1], [2]

PEP 519, [1]

PEP 520

PEP 523, [1]

PEP 524, [1], [2], [3], [4], [5]

PEP 525, [1], [2], [3], [4], [5], [6]

PEP 526, [1], [2], [3], [4], [5], [6], [7]

PEP 528

PEP 529, [1], [2], [3], [4]

PEP 530, [1], [2]

PEP 6

PEP 628, [1]

PEP 7

PEP 8, [1], [2], [3], [4]

PYTHON*, [1], [2]

python.function.entry (C 函数)

python.function.return (C 函数)

python_branch() (在 platform 模块中)

python_build() (在 platform 模块中)

python_compiler() (在 platform 模块中)

PYTHON_DOM

python_implementation() (在 platform 模块中)

python_revision() (在 platform 模块中)

python_version() (在 platform 模块中)

python_version_tuple() (在 platform 模块中)

PYTHONASYNCIODEBUG, [1]

PYTHONCASEOK

PYTHONDEBUG

PYTHONDOCS

PYTHONDONTWRITEBYTECODE, [1], [2], [3], [4]

PYTHONDUMPREFS

PYTHONFAULTHANDLER, [1]

PYTHONHASHSEED, [1], [2], [3], [4]

PYTHONHOME, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13]

Pythonic

PYTHONINSPECT, [1]

PYTHONIOENCODING, [1], [2], [3], [4]

PYTHONLEGACYWINDOWSFSENCODING, [1]

PYTHONLEGACYWINDOWSSTDIO, [1]

PYTHONMALLOC, [1], [2], [3], [4], [5]

PYTHONMALLOCSTATS, [1]

PYTHONNOUSERSITE, [1], [2]

PYTHONOPTIMIZE

PYTHONPATH, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22]

PYTHONSHOWALLOCCOUNT

PYTHONSHOWREFCOUNT

PYTHONSTARTUP, [1], [2], [3], [4], [5], [6], [7], [8]

PYTHONTRACEMALLOC, [1], [2]

PYTHONUNBUFFERED

PYTHONUSERBASE, [1], [2]

PYTHONVERBOSE

PYTHONWARNINGS, [1], [2], [3]

PyThreadState, [1]

(C 类型)

PyThreadState_Clear (C 函数)

PyThreadState_Delete (C 函数)

PyThreadState_Get (C 函数)

PyThreadState_GetDict (C 函数)

PyThreadState_New (C 函数)

PyThreadState_Next (C 函数)

PyThreadState_SetAsyncExc (C 函数)

PyThreadState_Swap (C 函数)

PyTime_Check (C 函数)

PyTime_CheckExact (C 函数)

PyTime_FromTime (C 函数)

PyTrace_C_CALL (C 变量)

PyTrace_C_EXCEPTION (C 变量)

PyTrace_C_RETURN (C 变量)

PyTrace_CALL (C 变量)

PyTrace_EXCEPTION (C 变量)

PyTrace_LINE (C 变量)

PyTrace_RETURN (C 变量)

PyTuple_Check (C 函数)

PyTuple_CheckExact (C 函数)

PyTuple_ClearFreeList (C 函数)

PyTuple_GET_ITEM (C 函数)

PyTuple_GET_SIZE (C 函数)

PyTuple_GetItem (C 函数)

PyTuple_GetSlice (C 函数)

PyTuple_New (C 函数)

PyTuple_Pack (C 函数)

PyTuple_SET_ITEM (C 函数)

PyTuple_SetItem (C 函数)

PyTuple_SetItem()

PyTuple_Size (C 函数)

PyTuple_Type (C 变量)

PyTupleObject (C 类型)

PyType_Check (C 函数)

PyType_CheckExact (C 函数)

PyType_ClearCache (C 函数)

PyType_FromSpec (C 函数)

PyType_FromSpecWithBases (C 函数)

PyType_GenericAlloc (C 函数)

PyType_GenericNew (C 函数)

PyType_GetFlags (C 函数)

PyType_GetSlot (C 函数)

PyType_HasFeature (C 函数)

PyType_IS_GC (C 函数)

PyType_IsSubtype (C 函数)

PyType_Modified (C 函数)

PyType_Ready (C 函数)

PyType_Type (C 变量)

PyTypeObject (C 类型)

PyTypeObject.tp_alloc (C 成员)

PyTypeObject.tp_allocs (C 成员)

PyTypeObject.tp_as_buffer (C 成员)

PyTypeObject.tp_base (C 成员)

PyTypeObject.tp_bases (C 成员)

PyTypeObject.tp_basicsize (C 成员)

PyTypeObject.tp_cache (C 成员)

PyTypeObject.tp_call (C 成员)

PyTypeObject.tp_clear (C 成员)

PyTypeObject.tp_dealloc (C 成员)

PyTypeObject.tp_descr_get (C 成员)

PyTypeObject.tp_descr_set (C 成员)

PyTypeObject.tp_dict (C 成员)

PyTypeObject.tp_dictoffset (C 成员)

PyTypeObject.tp_doc (C 成员)

PyTypeObject.tp_finalize (C 成员)

PyTypeObject.tp_flags (C 成员)

PyTypeObject.tp_free (C 成员)

PyTypeObject.tp_frees (C 成员)

PyTypeObject.tp_getattr (C 成员)

PyTypeObject.tp_getattro (C 成员)

PyTypeObject.tp_getset (C 成员)

PyTypeObject.tp_hash (C 成员)

PyTypeObject.tp_init (C 成员)

PyTypeObject.tp_is_gc (C 成员)

PyTypeObject.tp_itemsize (C 成员)

PyTypeObject.tp_iter (C 成员)

PyTypeObject.tp_iternext (C 成员)

PyTypeObject.tp_maxalloc (C 成员)

PyTypeObject.tp_members (C 成员)

PyTypeObject.tp_methods (C 成员)

PyTypeObject.tp_mro (C 成员)

PyTypeObject.tp_name (C 成员)

PyTypeObject.tp_new (C 成员)

PyTypeObject.tp_next (C 成员)

PyTypeObject.tp_print (C 成员)

PyTypeObject.tp_repr (C 成员)

PyTypeObject.tp_richcompare (C 成员)

PyTypeObject.tp_setattr (C 成员)

PyTypeObject.tp_setattro (C 成员)

PyTypeObject.tp_str (C 成员)

PyTypeObject.tp_subclasses (C 成员)

PyTypeObject.tp_traverse (C 成员)

PyTypeObject.tp_weaklist (C 成员)

PyTypeObject.tp_weaklistoffset (C 成员)

PyTZInfo_Check (C 函数)

PyTZInfo_CheckExact (C 函数)

PyUnicode_1BYTE_DATA (C 函数)

PyUnicode_1BYTE_KIND (C 宏)

PyUnicode_2BYTE_DATA (C 函数)

PyUnicode_2BYTE_KIND (C 宏)

PyUnicode_4BYTE_DATA (C 函数)

PyUnicode_4BYTE_KIND (C 宏)

PyUnicode_AS_DATA (C 函数)

PyUnicode_AS_UNICODE (C 函数)

PyUnicode_AsASCIIString (C 函数)

PyUnicode_AsCharmapString (C 函数)

PyUnicode_AsEncodedString (C 函数)

PyUnicode_AsLatin1String (C 函数)

PyUnicode_AsMBCSString (C 函数)

PyUnicode_AsRawUnicodeEscapeString (C 函数)

PyUnicode_AsUCS4 (C 函数)

PyUnicode_AsUCS4Copy (C 函数)

PyUnicode_AsUnicode (C 函数)

PyUnicode_AsUnicodeAndSize (C 函数)

PyUnicode_AsUnicodeCopy (C 函数)

PyUnicode_AsUnicodeEscapeString (C 函数)

PyUnicode_AsUTF16String (C 函数)

PyUnicode_AsUTF32String (C 函数)

PyUnicode_AsUTF8 (C 函数)

PyUnicode_AsUTF8AndSize (C 函数)

PyUnicode_AsUTF8String (C 函数)

PyUnicode_AsWideChar (C 函数)

PyUnicode_AsWideCharString (C 函数)

PyUnicode_Check (C 函数)

PyUnicode_CheckExact (C 函数)

PyUnicode_ClearFreeList (C 函数)

PyUnicode_Compare (C 函数)

PyUnicode_CompareWithASCIIString (C 函数)

PyUnicode_Concat (C 函数)

PyUnicode_Contains (C 函数)

PyUnicode_CopyCharacters (C 函数)

PyUnicode_Count (C 函数)

PyUnicode_DATA (C 函数)

PyUnicode_Decode (C 函数)

PyUnicode_DecodeASCII (C 函数)

PyUnicode_DecodeCharmap (C 函数)

PyUnicode_DecodeFSDefault (C 函数)

PyUnicode_DecodeFSDefaultAndSize (C 函数)

PyUnicode_DecodeLatin1 (C 函数)

PyUnicode_DecodeLocale (C 函数)

PyUnicode_DecodeLocaleAndSize (C 函数)

PyUnicode_DecodeMBCS (C 函数)

PyUnicode_DecodeMBCSStateful (C 函数)

PyUnicode_DecodeRawUnicodeEscape (C 函数)

PyUnicode_DecodeUnicodeEscape (C 函数)

PyUnicode_DecodeUTF16 (C 函数)

PyUnicode_DecodeUTF16Stateful (C 函数)

PyUnicode_DecodeUTF32 (C 函数)

PyUnicode_DecodeUTF32Stateful (C 函数)

PyUnicode_DecodeUTF7 (C 函数)

PyUnicode_DecodeUTF7Stateful (C 函数)

PyUnicode_DecodeUTF8 (C 函数)

PyUnicode_DecodeUTF8Stateful (C 函数)

PyUnicode_Encode (C 函数)

PyUnicode_EncodeASCII (C 函数)

PyUnicode_EncodeCharmap (C 函数)

PyUnicode_EncodeCodePage (C 函数)

PyUnicode_EncodeFSDefault (C 函数)

PyUnicode_EncodeLatin1 (C 函数)

PyUnicode_EncodeLocale (C 函数)

PyUnicode_EncodeMBCS (C 函数)

PyUnicode_EncodeRawUnicodeEscape (C 函数)

PyUnicode_EncodeUnicodeEscape (C 函数)

PyUnicode_EncodeUTF16 (C 函数)

PyUnicode_EncodeUTF32 (C 函数)

PyUnicode_EncodeUTF7 (C 函数)

PyUnicode_EncodeUTF8 (C 函数)

PyUnicode_Fill (C 函数)

PyUnicode_Find (C 函数)

PyUnicode_FindChar (C 函数)

PyUnicode_Format (C 函数)

PyUnicode_FromEncodedObject (C 函数)

PyUnicode_FromFormat (C 函数)

PyUnicode_FromFormatV (C 函数)

PyUnicode_FromKindAndData (C 函数)

PyUnicode_FromObject (C 函数)

PyUnicode_FromString (C 函数)

PyUnicode_FromString()

PyUnicode_FromStringAndSize (C 函数)

PyUnicode_FromUnicode (C 函数)

PyUnicode_FromWideChar (C 函数)

PyUnicode_FSConverter (C 函数)

PyUnicode_FSDecoder (C 函数)

PyUnicode_GET_DATA_SIZE (C 函数)

PyUnicode_GET_LENGTH (C 函数)

PyUnicode_GET_SIZE (C 函数)

PyUnicode_GetLength (C 函数)

PyUnicode_GetSize (C 函数)

PyUnicode_InternFromString (C 函数)

PyUnicode_InternInPlace (C 函数)

PyUnicode_Join (C 函数)

PyUnicode_KIND (C 函数)

PyUnicode_MAX_CHAR_VALUE (C 函数)

PyUnicode_New (C 函数)

PyUnicode_READ (C 函数)

PyUnicode_READ_CHAR (C 函数)

PyUnicode_ReadChar (C 函数)

PyUnicode_READY (C 函数)

PyUnicode_Replace (C 函数)

PyUnicode_RichCompare (C 函数)

PyUnicode_Split (C 函数)

PyUnicode_Splitlines (C 函数)

PyUnicode_Substring (C 函数)

PyUnicode_Tailmatch (C 函数)

PyUnicode_TransformDecimalToASCII (C 函数)

PyUnicode_Translate (C 函数), [1]

PyUnicode_TranslateCharmap (C 函数)

PyUnicode_Type (C 变量)

PyUnicode_WCHAR_KIND (C 宏)

PyUnicode_WRITE (C 函数)

PyUnicode_WriteChar (C 函数)

PyUnicodeDecodeError_Create (C 函数)

PyUnicodeDecodeError_GetEncoding (C 函数)

PyUnicodeDecodeError_GetEnd (C 函数)

PyUnicodeDecodeError_GetObject (C 函数)

PyUnicodeDecodeError_GetReason (C 函数)

PyUnicodeDecodeError_GetStart (C 函数)

PyUnicodeDecodeError_SetEnd (C 函数)

PyUnicodeDecodeError_SetReason (C 函数)

PyUnicodeDecodeError_SetStart (C 函数)

PyUnicodeEncodeError_Create (C 函数)

PyUnicodeEncodeError_GetEncoding (C 函数)

PyUnicodeEncodeError_GetEnd (C 函数)

PyUnicodeEncodeError_GetObject (C 函数)

PyUnicodeEncodeError_GetReason (C 函数)

PyUnicodeEncodeError_GetStart (C 函数)

PyUnicodeEncodeError_SetEnd (C 函数)

PyUnicodeEncodeError_SetReason (C 函数)

PyUnicodeEncodeError_SetStart (C 函数)

PyUnicodeObject (C 类型)

PyUnicodeTranslateError_Create (C 函数)

PyUnicodeTranslateError_GetEnd (C 函数)

PyUnicodeTranslateError_GetObject (C 函数)

PyUnicodeTranslateError_GetReason (C 函数)

PyUnicodeTranslateError_GetStart (C 函数)

PyUnicodeTranslateError_SetEnd (C 函数)

PyUnicodeTranslateError_SetReason (C 函数)

PyUnicodeTranslateError_SetStart (C 函数)

PyVarObject (C 类型)

PyVarObject.ob_size (C 成员)

PyVarObject_HEAD_INIT (C 宏)

PyWeakref_Check (C 函数)

PyWeakref_CheckProxy (C 函数)

PyWeakref_CheckRef (C 函数)

PyWeakref_GET_OBJECT (C 函数)

PyWeakref_GetObject (C 函数)

PyWeakref_NewProxy (C 函数)

PyWeakref_NewRef (C 函数)

PyWrapper_New (C 函数)

PyZipFile (zipfile 中的类)

Q

qiflush() (在 curses 模块中)

QName (xml.etree.ElementTree 中的类)

qsize() (asyncio.Queue 方法)

(multiprocessing.Queue 方法)

(queue.Queue 方法)

qualified name -- 限定名称

quantize() (decimal.Context 方法)

(decimal.Decimal 方法)

QueryInfoKey() (在 winreg 模块中)

QueryReflectionKey() (在 winreg 模块中)

QueryValue() (在 winreg 模块中)

QueryValueEx() (在 winreg 模块中)

Queue (asyncio 中的类)

(multiprocessing 中的类)

(queue 中的类)

queue (sched.scheduler 属性)

(模块)

Queue() (multiprocessing.managers.SyncManager 方法)

QueueEmpty

QueueFull

QueueHandler (logging.handlers 中的类)

QueueListener (logging.handlers 中的类)

quick_ratio() (difflib.SequenceMatcher 方法)

quit (pdb command)

(內置变量)

quit() (ftplib.FTP 方法)

(nntplib.NNTP 方法)

(poplib.POP3 方法)

(smtplib.SMTP 方法)

quopri (模块)

quote() (在 email.utils 模块中)

(在 shlex 模块中)

(在 urllib.parse 模块中)

QUOTE_ALL() (在 csv 模块中)

quote_from_bytes() (在 urllib.parse 模块中)

QUOTE_MINIMAL() (在 csv 模块中)

QUOTE_NONE() (在 csv 模块中)

QUOTE_NONNUMERIC() (在 csv 模块中)

quote_plus() (在 urllib.parse 模块中)

quoteattr() (在 xml.sax.saxutils 模块中)

quotechar (csv.Dialect 属性)

quoted-printable

encoding

quotes (shlex.shlex 属性)

quoting (csv.Dialect 属性)

R

r"

raw string literal

r'

raw string literal

R_OK() (在 os 模块中)

radians() (在 math 模块中)

(在 turtle 模块中)

RadioButtonGroup (msilib 中的类)

radiogroup() (msilib.Dialog 方法)

radix() (decimal.Context 方法)

(decimal.Decimal 方法)

RADIXCHAR() (在 locale 模块中)

raise

语句, [1]

raise (2to3 fixer)

raise an exception

raise_on_defect (email.policy.Policy 属性)

RAISE_VARARGS (opcode)

raising

exception

RAND_add() (在 ssl 模块中)

RAND_bytes() (在 ssl 模块中)

RAND_egd() (在 ssl 模块中)

RAND_pseudo_bytes() (在 ssl 模块中)

RAND_status() (在 ssl 模块中)

randbelow() (在 secrets 模块中)

randbits() (在 secrets 模块中)

randint() (在 random 模块中)

random (模块)

random() (在 random 模块中)

randrange() (在 random 模块中)

range

內置函数

对象

range (內置类)

RARROW() (在 token 模块中)

ratecv() (在 audioop 模块中)

ratio() (difflib.SequenceMatcher 方法)

Rational (numbers 中的类)

raw (io.BufferedIOBase 属性)

raw string

raw() (在 curses 模块中)

raw_data_manager() (在 email.contentmanager 模块中)

raw_decode() (json.JSONDecoder 方法)

raw_input (2to3 fixer)

raw_input() (code.InteractiveConsole 方法)

RawArray() (在 multiprocessing.sharedctypes 模块中)

RawConfigParser (configparser 中的类)

RawDescriptionHelpFormatter (argparse 中的类)

RawIOBase (io 中的类)

RawPen (turtle 中的类)

RawTextHelpFormatter (argparse 中的类)

RawTurtle (turtle 中的类)

RawValue() (在 multiprocessing.sharedctypes 模块中)

RBRACE() (在 token 模块中)

rcpttos (smtpd.SMTPChannel 属性)

re

模块, [1]

re (re.match 属性)

(模块)

read() (asyncio.StreamReader 方法)

(chunk.Chunk 方法)

(codecs.StreamReader 方法)

(configparser.ConfigParser 方法)

(http.client.HTTPResponse 方法)

(imaplib.IMAP4 方法)

(io.BufferedIOBase 方法)

(io.BufferedReader 方法)

(io.RawIOBase 方法)

(io.TextIOBase 方法)

(mimetypes.MimeTypes 方法)

(mmap.mmap 方法)

(ossaudiodev.oss_audio_device 方法)

(ssl.MemoryBIO 方法)

(ssl.SSLSocket 方法)

(urllib.robotparser.RobotFileParser 方法)

(zipfile.ZipFile 方法)

(在 os 模块中)

read1() (io.BufferedIOBase 方法)

(io.BufferedReader 方法)

(io.BytesIO 方法)

read_all() (telnetlib.Telnet 方法)

read_byte() (mmap.mmap 方法)

read_bytes() (pathlib.Path 方法)

read_dict() (configparser.ConfigParser 方法)

read_eager() (telnetlib.Telnet 方法)

read_environ() (在 wsgiref.handlers 模块中)

read_events() (xml.etree.ElementTree.XMLPullParser 方法)

read_file() (configparser.ConfigParser 方法)

read_history_file() (在 readline 模块中)

read_init_file() (在 readline 模块中)

read_lazy() (telnetlib.Telnet 方法)

read_mime_types() (在 mimetypes 模块中)

READ_RESTRICTED

read_sb_data() (telnetlib.Telnet 方法)

read_some() (telnetlib.Telnet 方法)

read_string() (configparser.ConfigParser 方法)

read_text() (pathlib.Path 方法)

read_token() (shlex.shlex 方法)

read_until() (telnetlib.Telnet 方法)

read_very_eager() (telnetlib.Telnet 方法)

read_very_lazy() (telnetlib.Telnet 方法)

read_windows_registry() (mimetypes.MimeTypes 方法)

readable() (asyncore.dispatcher 方法)

(io.IOBase 方法)

READABLE() (在 tkinter 模块中)

readall() (io.RawIOBase 方法)

reader() (在 csv 模块中)

ReadError

readexactly() (asyncio.StreamReader 方法)

readfp() (configparser.ConfigParser 方法)

(mimetypes.MimeTypes 方法)

readframes() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

readinto() (http.client.HTTPResponse 方法)

(io.BufferedIOBase 方法)

(io.RawIOBase 方法)

readinto1() (io.BufferedIOBase 方法)

(io.BytesIO 方法)

readline (模块)

readline() (asyncio.StreamReader 方法)

(codecs.StreamReader 方法)

(distutils.text_file.TextFile 方法)

(imaplib.IMAP4 方法)

(io.IOBase 方法)

(io.TextIOBase 方法)

(mmap.mmap 方法)

readlines() (codecs.StreamReader 方法)

(distutils.text_file.TextFile 方法)

(io.IOBase 方法)

readlink() (在 os 模块中)

readmodule() (在 pyclbr 模块中)

readmodule_ex() (在 pyclbr 模块中)

READONLY

readonly (memoryview 属性)

readPlist() (在 plistlib 模块中)

readPlistFromBytes() (在 plistlib 模块中)

ReadTransport (asyncio 中的类)

readuntil() (asyncio.StreamReader 方法)

readv() (在 os 模块中)

ready() (multiprocessing.pool.AsyncResult 方法)

Real (numbers 中的类)

real (numbers.Complex 属性)

Real Media File Format

real_quick_ratio() (difflib.SequenceMatcher 方法)

realloc()

realpath() (在 os.path 模块中)

reason (http.client.HTTPResponse 属性)

(ssl.SSLError 属性)

(UnicodeError 属性)

(urllib.error.HTTPError 属性)

(urllib.error.URLError 属性)

reattach() (tkinter.ttk.Treeview 方法)

rebinding

name

reccontrols() (ossaudiodev.oss_mixer_device 方法)

received_data (smtpd.SMTPChannel 属性)

received_lines (smtpd.SMTPChannel 属性)

recent() (imaplib.IMAP4 方法)

records (unittest.TestCase 属性)

rect() (在 cmath 模块中)

rectangle() (在 curses.textpad 模块中)

RecursionError

recursive_repr() (在 reprlib 模块中)

recv() (asyncore.dispatcher 方法)

(multiprocessing.connection.Connection 方法)

(socket.socket 方法)

recv_bytes() (multiprocessing.connection.Connection 方法)

recv_bytes_into() (multiprocessing.connection.Connection 方法)

recv_into() (socket.socket 方法)

recvfrom() (socket.socket 方法)

recvfrom_into() (socket.socket 方法)

recvmsg() (socket.socket 方法)

recvmsg_into() (socket.socket 方法)

redirect_request() (urllib.request.HTTPRedirectHandler 方法)

redirect_stderr() (在 contextlib 模块中)

redirect_stdout() (在 contextlib 模块中)

redisplay() (在 readline 模块中)

redrawln() (curses.window 方法)

redrawwin() (curses.window 方法)

reduce (2to3 fixer)

reduce() (在 functools 模块中)

ref (weakref 中的类)

reference

attribute

reference count -- 引用计数

reference counting

ReferenceError, [1]

ReferenceType() (在 weakref 模块中)

refold_source (email.policy.EmailPolicy 属性)

refresh() (curses.window 方法)

REG_BINARY() (在 winreg 模块中)

REG_DWORD() (在 winreg 模块中)

REG_DWORD_BIG_ENDIAN() (在 winreg 模块中)

REG_DWORD_LITTLE_ENDIAN() (在 winreg 模块中)

REG_EXPAND_SZ() (在 winreg 模块中)

REG_FULL_RESOURCE_DESCRIPTOR() (在 winreg 模块中)

REG_LINK() (在 winreg 模块中)

REG_MULTI_SZ() (在 winreg 模块中)

REG_NONE() (在 winreg 模块中)

REG_QWORD() (在 winreg 模块中)

REG_QWORD_LITTLE_ENDIAN() (在 winreg 模块中)

REG_RESOURCE_LIST() (在 winreg 模块中)

REG_RESOURCE_REQUIREMENTS_LIST() (在 winreg 模块中)

REG_SZ() (在 winreg 模块中)

register() (abc.ABCMeta 方法)

(multiprocessing.managers.BaseManager 方法)

(select.devpoll 方法)

(select.epoll 方法)

(select.poll 方法)

(selectors.BaseSelector 方法)

(在 atexit 模块中)

(在 codecs 模块中)

(在 faulthandler 模块中)

(在 webbrowser 模块中)

register_adapter() (在 sqlite3 模块中)

register_archive_format() (在 shutil 模块中)

register_converter() (在 sqlite3 模块中)

register_defect() (email.policy.Policy 方法)

register_dialect() (在 csv 模块中)

register_error() (在 codecs 模块中)

register_function() (xmlrpc.server.CGIXMLRPCRequestHandler 方法)

(xmlrpc.server.SimpleXMLRPCServer 方法)

register_instance() (xmlrpc.server.CGIXMLRPCRequestHandler 方法)

(xmlrpc.server.SimpleXMLRPCServer 方法)

register_introspection_functions() (xmlrpc.server.CGIXMLRPCRequestHandler 方法)

(xmlrpc.server.SimpleXMLRPCServer 方法)

register_multicall_functions() (xmlrpc.server.CGIXMLRPCRequestHandler 方法)

(xmlrpc.server.SimpleXMLRPCServer 方法)

register_namespace() (在 xml.etree.ElementTree 模块中)

register_optionflag() (在 doctest 模块中)

register_shape() (在 turtle 模块中)

register_unpack_format() (在 shutil 模块中)

registerDOMImplementation() (在 xml.dom 模块中)

registerResult() (在 unittest 模块中)

regular

package

regular package -- 常规包

relative

import

URL

relative_to() (pathlib.PurePath 方法)

release() (_thread.lock 方法)

(asyncio.Condition 方法)

(asyncio.Lock 方法)

(asyncio.Semaphore 方法)

(logging.Handler 方法)

(memoryview 方法)

(multiprocessing.Lock 方法)

(multiprocessing.RLock 方法)

(threading.Condition 方法)

(threading.Lock 方法)

(threading.RLock 方法)

(threading.Semaphore 方法)

(在 platform 模块中)

release_lock() (在 imp 模块中)

reload (2to3 fixer)

reload() (在 imp 模块中)

(在 importlib 模块中)

relpath() (在 os.path 模块中)

remainder() (decimal.Context 方法)

remainder_near() (decimal.Context 方法)

(decimal.Decimal 方法)

RemoteDisconnected

remove() (array.array 方法)

(collections.deque 方法)

(frozenset 方法)

(mailbox.Mailbox 方法)

(mailbox.MH 方法)

(sequence method)

(xml.etree.ElementTree.Element 方法)

(在 os 模块中)

remove_done_callback() (asyncio.Future 方法)

remove_flag() (mailbox.MaildirMessage 方法)

(mailbox.mboxMessage 方法)

(mailbox.MMDFMessage 方法)

remove_folder() (mailbox.Maildir 方法)

(mailbox.MH 方法)

remove_header() (urllib.request.Request 方法)

remove_history_item() (在 readline 模块中)

remove_label() (mailbox.BabylMessage 方法)

remove_option() (configparser.ConfigParser 方法)

(optparse.OptionParser 方法)

remove_pyc() (msilib.Directory 方法)

remove_reader() (asyncio.AbstractEventLoop 方法)

remove_section() (configparser.ConfigParser 方法)

remove_sequence() (mailbox.MHMessage 方法)

remove_signal_handler() (asyncio.AbstractEventLoop 方法)

remove_tree() (在 distutils.dir_util 模块中)

remove_writer() (asyncio.AbstractEventLoop 方法)

removeAttribute() (xml.dom.Element 方法)

removeAttributeNode() (xml.dom.Element 方法)

removeAttributeNS() (xml.dom.Element 方法)

removeChild() (xml.dom.Node 方法)

removedirs() (在 os 模块中)

removeFilter() (logging.Handler 方法)

(logging.Logger 方法)

removeHandler() (logging.Logger 方法)

(在 unittest 模块中)

removeResult() (在 unittest 模块中)

removexattr() (在 os 模块中)

rename() (ftplib.FTP 方法)

(imaplib.IMAP4 方法)

(pathlib.Path 方法)

(在 os 模块中)

renames (2to3 fixer)

renames() (在 os 模块中)

reopenIfNeeded() (logging.handlers.WatchedFileHandler 方法)

reorganize() (dbm.gnu.gdbm 方法)

repeat() (timeit.Timer 方法)

(在 itertools 模块中)

(在 timeit 模块中)

repetition

operation

replace() (bytearray 方法)

(bytes 方法)

(curses.panel.Panel 方法)

(datetime.date 方法)

(datetime.datetime 方法)

(datetime.time 方法)

(inspect.Parameter 方法)

(inspect.Signature 方法)

(pathlib.Path 方法)

(str 方法)

(在 os 模块中)

replace_errors() (在 codecs 模块中)

replace_header() (email.message.EmailMessage 方法)

(email.message.Message 方法)

replace_history_item() (在 readline 模块中)

replace_whitespace (textwrap.TextWrapper 属性)

replaceChild() (xml.dom.Node 方法)

ReplacePackage() (在 modulefinder 模块中)

report() (filecmp.dircmp 方法)

(modulefinder.ModuleFinder 方法)

REPORT_CDIFF() (在 doctest 模块中)

report_failure() (doctest.DocTestRunner 方法)

report_full_closure() (filecmp.dircmp 方法)

REPORT_NDIFF() (在 doctest 模块中)

REPORT_ONLY_FIRST_FAILURE() (在 doctest 模块中)

report_partial_closure() (filecmp.dircmp 方法)

report_start() (doctest.DocTestRunner 方法)

report_success() (doctest.DocTestRunner 方法)

REPORT_UDIFF() (在 doctest 模块中)

report_unexpected_exception() (doctest.DocTestRunner 方法)

REPORTING_FLAGS() (在 doctest 模块中)

repr

內置函数, [1], [2], [3]

repr (2to3 fixer)

Repr (reprlib 中的类)

repr() (built-in function)

__repr__() (object method)

repr() (reprlib.Repr 方法)

(內置函数)

(在 reprlib 模块中)

repr1() (reprlib.Repr 方法)

representation

integer

reprlib (模块)

Request (urllib.request 中的类)

request() (http.client.HTTPConnection 方法)

request_queue_size (socketserver.BaseServer 属性)

request_rate() (urllib.robotparser.RobotFileParser 方法)

request_uri() (在 wsgiref.util 模块中)

request_version (http.server.BaseHTTPRequestHandler 属性)

RequestHandlerClass (socketserver.BaseServer 属性)

requestline (http.server.BaseHTTPRequestHandler 属性)

requires() (在 test.support 模块中)

reserved (zipfile.ZipInfo 属性)

reserved word

RESERVED_FUTURE() (在 uuid 模块中)

RESERVED_MICROSOFT() (在 uuid 模块中)

RESERVED_NCS() (在 uuid 模块中)

reset() (bdb.Bdb 方法)

(codecs.IncrementalDecoder 方法)

(codecs.IncrementalEncoder 方法)

(codecs.StreamReader 方法)

(codecs.StreamWriter 方法)

(html.parser.HTMLParser 方法)

(ossaudiodev.oss_audio_device 方法)

(pipes.Template 方法)

(threading.Barrier 方法)

(xdrlib.Packer 方法)

(xdrlib.Unpacker 方法)

(xml.dom.pulldom.DOMEventStream 方法)

(xml.sax.xmlreader.IncrementalParser 方法)

(在 turtle 模块中), [1]

reset_mock() (unittest.mock.Mock 方法)

reset_prog_mode() (在 curses 模块中)

reset_shell_mode() (在 curses 模块中)

resetbuffer() (code.InteractiveConsole 方法)

resetlocale() (在 locale 模块中)

resetscreen() (在 turtle 模块中)

resetty() (在 curses 模块中)

resetwarnings() (在 warnings 模块中)

resize() (curses.window 方法)

(mmap.mmap 方法)

(在 ctypes 模块中)

resize_term() (在 curses 模块中)

resizemode() (在 turtle 模块中)

resizeterm() (在 curses 模块中)

resolution (datetime.date 属性)

(datetime.datetime 属性)

(datetime.time 属性)

(datetime.timedelta 属性)

resolve() (pathlib.Path 方法)

resolve_name() (在 importlib.util 模块中)

resolveEntity() (xml.sax.handler.EntityResolver 方法)

resource (模块)

ResourceDenied

ResourceLoader (importlib.abc 中的类)

ResourceWarning

response (nntplib.NNTPError 属性)

response() (imaplib.IMAP4 方法)

ResponseNotReady

responses (http.server.BaseHTTPRequestHandler 属性)

responses() (在 http.client 模块中)

restart (pdb command)

restore() (在 difflib 模块中)

RESTRICTED

restricted

execution

restype (ctypes._FuncPtr 属性)

result() (asyncio.Future 方法)

(concurrent.futures.Future 方法)

results() (trace.Trace 方法)

resume_reading() (asyncio.ReadTransport 方法)

resume_writing() (asyncio.BaseProtocol 方法)

retr() (poplib.POP3 方法)

retrbinary() (ftplib.FTP 方法)

retrieve() (urllib.request.URLopener 方法)

retrlines() (ftplib.FTP 方法)

return

语句, [1], [2]

return (pdb command)

return_annotation (inspect.Signature 属性)

return_ok() (http.cookiejar.CookiePolicy 方法)

RETURN_VALUE (opcode)

return_value (unittest.mock.Mock 属性)

returncode (asyncio.asyncio.subprocess.Process 属性)

(subprocess.CalledProcessError 属性)

(subprocess.CompletedProcess 属性)

(subprocess.Popen 属性)

reverse() (array.array 方法)

(collections.deque 方法)

(sequence method)

(在 audioop 模块中)

reverse_order() (pstats.Stats 方法)

reverse_pointer (ipaddress.IPv4Address 属性)

(ipaddress.IPv6Address 属性)

reversed() (內置函数)

Reversible (collections.abc 中的类)

(typing 中的类)

revert() (http.cookiejar.FileCookieJar 方法)

rewind() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

RFC

RFC 1014, [1]

RFC 1123

RFC 1321

RFC 1422, [1]

RFC 1521, [1], [2]

RFC 1522, [1], [2]

RFC 1524, [1]

RFC 1730

RFC 1738

RFC 1750

RFC 1766, [1]

RFC 1808, [1], [2]

RFC 1832, [1]

RFC 1869, [1]

RFC 1870, [1], [2]

RFC 1939, [1]

RFC 2033

RFC 2045, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14]

RFC 2045#section-6.8

RFC 2046, [1], [2]

RFC 2047, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12]

RFC 2060, [1]

RFC 2068

RFC 2104, [1]

RFC 2109, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14]

RFC 2183, [1], [2]

RFC 2231, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13]

RFC 2295

RFC 2342, [1]

RFC 2368

RFC 2373, [1], [2]

RFC 2396, [1], [2]

RFC 2397

RFC 2449

RFC 2487

RFC 2595, [1]

RFC 2616, [1], [2], [3], [4], [5], [6], [7], [8], [9]

RFC 2732, [1], [2]

RFC 2774

RFC 2818, [1]

RFC 2821

RFC 2822, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17]

RFC 2964

RFC 2965, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19]

RFC 2980, [1]

RFC 3056

RFC 3171

RFC 3207

RFC 3280

RFC 3330

RFC 3339

RFC 3454, [1]

RFC 3490, [1], [2], [3]

RFC 3490#section-3.1

RFC 3492, [1]

RFC 3493

RFC 3501

RFC 3542, [1]

RFC 3548, [1], [2], [3], [4]

RFC 3659

RFC 3879

RFC 3927

RFC 3977, [1], [2], [3], [4]

RFC 3986, [1], [2], [3], [4], [5]

RFC 4086

RFC 4122, [1], [2], [3], [4], [5], [6], [7], [8]

RFC 4180

RFC 4193

RFC 4217

RFC 4291

RFC 4380

RFC 4627, [1]

RFC 4642

RFC 4954, [1]

RFC 5161, [1]

RFC 5233, [1]

RFC 5246, [1]

RFC 5280, [1], [2], [3], [4]

RFC 5321, [1], [2], [3], [4]

RFC 5322, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21]

RFC 5424, [1], [2], [3]

RFC 5424#section-6

RFC 5735

RFC 5929

RFC 6066, [1], [2]

RFC 6125, [1]

RFC 6152, [1]

RFC 6531, [1], [2], [3], [4], [5], [6], [7], [8]

RFC 6532, [1], [2], [3], [4]

RFC 6585

RFC 6855, [1], [2], [3]

RFC 6856, [1]

RFC 7159, [1], [2]

RFC 7230, [1]

RFC 7231

RFC 7238

RFC 7301, [1], [2]

RFC 7525

RFC 7693

RFC 7914

RFC 821, [1]

RFC 822, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11]

RFC 854, [1]

RFC 959, [1]

RFC 977

rfc2109 (http.cookiejar.Cookie 属性)

rfc2109_as_netscape (http.cookiejar.DefaultCookiePolicy 属性)

rfc2965 (http.cookiejar.CookiePolicy 属性)

rfc822_escape() (在 distutils.util 模块中)

RFC_4122() (在 uuid 模块中)

rfile (http.server.BaseHTTPRequestHandler 属性)

rfind() (bytearray 方法)

(bytes 方法)

(mmap.mmap 方法)

(str 方法)

rgb_to_hls() (在 colorsys 模块中)

rgb_to_hsv() (在 colorsys 模块中)

rgb_to_yiq() (在 colorsys 模块中)

rglob() (pathlib.Path 方法)

right (filecmp.dircmp 属性)

right() (在 turtle 模块中)

right_list (filecmp.dircmp 属性)

right_only (filecmp.dircmp 属性)

RIGHTSHIFT() (在 token 模块中)

RIGHTSHIFTEQUAL() (在 token 模块中)

rindex() (bytearray 方法)

(bytes 方法)

(str 方法)

rjust() (bytearray 方法)

(bytes 方法)

(str 方法)

rlcompleter (模块)

rlecode_hqx() (在 binascii 模块中)

rledecode_hqx() (在 binascii 模块中)

RLIM_INFINITY() (在 resource 模块中)

RLIMIT_AS() (在 resource 模块中)

RLIMIT_CORE() (在 resource 模块中)

RLIMIT_CPU() (在 resource 模块中)

RLIMIT_DATA() (在 resource 模块中)

RLIMIT_FSIZE() (在 resource 模块中)

RLIMIT_MEMLOCK() (在 resource 模块中)

RLIMIT_MSGQUEUE() (在 resource 模块中)

RLIMIT_NICE() (在 resource 模块中)

RLIMIT_NOFILE() (在 resource 模块中)

RLIMIT_NPROC() (在 resource 模块中)

RLIMIT_NPTS() (在 resource 模块中)

RLIMIT_OFILE() (在 resource 模块中)

RLIMIT_RSS() (在 resource 模块中)

RLIMIT_RTPRIO() (在 resource 模块中)

RLIMIT_RTTIME() (在 resource 模块中)

RLIMIT_SBSIZE() (在 resource 模块中)

RLIMIT_SIGPENDING() (在 resource 模块中)

RLIMIT_STACK() (在 resource 模块中)

RLIMIT_SWAP() (在 resource 模块中)

RLIMIT_VMEM() (在 resource 模块中)

RLock (multiprocessing 中的类)

(threading 中的类)

RLock() (multiprocessing.managers.SyncManager 方法)

rmd() (ftplib.FTP 方法)

rmdir() (pathlib.Path 方法)

(在 os 模块中)

RMFF

rms() (在 audioop 模块中)

rmtree() (在 shutil 模块中)

RobotFileParser (urllib.robotparser 中的类)

robots.txt

rollback() (sqlite3.Connection 方法)

ROT_THREE (opcode)

ROT_TWO (opcode)

rotate() (collections.deque 方法)

(decimal.Context 方法)

(decimal.Decimal 方法)

(logging.handlers.BaseRotatingHandler 方法)

RotatingFileHandler (logging.handlers 中的类)

rotation_filename() (logging.handlers.BaseRotatingHandler 方法)

rotator (logging.handlers.BaseRotatingHandler 属性)

round

內置函数

round() (內置函数)

ROUND_05UP() (在 decimal 模块中)

ROUND_CEILING() (在 decimal 模块中)

ROUND_DOWN() (在 decimal 模块中)

ROUND_FLOOR() (在 decimal 模块中)

ROUND_HALF_DOWN() (在 decimal 模块中)

ROUND_HALF_EVEN() (在 decimal 模块中)

ROUND_HALF_UP() (在 decimal 模块中)

ROUND_UP() (在 decimal 模块中)

Rounded (decimal 中的类)

Row (sqlite3 中的类)

row_factory (sqlite3.Connection 属性)

rowcount (sqlite3.Cursor 属性)

RPAR() (在 token 模块中)

rpartition() (bytearray 方法)

(bytes 方法)

(str 方法)

rpc_paths (xmlrpc.server.SimpleXMLRPCRequestHandler 属性)

rpop() (poplib.POP3 方法)

rset() (poplib.POP3 方法)

rshift() (在 operator 模块中)

rsplit() (bytearray 方法)

(bytes 方法)

(str 方法)

RSQB() (在 token 模块中)

rstrip() (bytearray 方法)

(bytes 方法)

(str 方法)

rt() (在 turtle 模块中)

RTLD_DEEPBIND() (在 os 模块中)

RTLD_GLOBAL() (在 os 模块中)

RTLD_LAZY() (在 os 模块中)

RTLD_LOCAL() (在 os 模块中)

RTLD_NODELETE() (在 os 模块中)

RTLD_NOLOAD() (在 os 模块中)

RTLD_NOW() (在 os 模块中)

ruler (cmd.Cmd 属性)

run (pdb command)

Run script

run() (bdb.Bdb 方法)

(distutils.cmd.Command 方法)

(doctest.DocTestRunner 方法)

(multiprocessing.Process 方法)

(pdb.Pdb 方法)

(profile.Profile 方法)

(sched.scheduler 方法)

(threading.Thread 方法)

(trace.Trace 方法)

(unittest.TestCase 方法)

(unittest.TestSuite 方法)

(unittest.TextTestRunner 方法)

(wsgiref.handlers.BaseHandler 方法)

(在 pdb 模块中)

(在 profile 模块中)

(在 subprocess 模块中)

run_coroutine_threadsafe() (在 asyncio 模块中)

run_docstring_examples() (在 doctest 模块中)

run_doctest() (在 test.support 模块中)

run_forever() (asyncio.AbstractEventLoop 方法)

run_in_executor() (asyncio.AbstractEventLoop 方法)

run_module() (在 runpy 模块中)

run_path() (在 runpy 模块中)

run_script() (modulefinder.ModuleFinder 方法)

run_setup() (在 distutils.core 模块中)

run_unittest() (在 test.support 模块中)

run_until_complete() (asyncio.AbstractEventLoop 方法)

run_with_locale() (在 test.support 模块中)

runcall() (bdb.Bdb 方法)

(pdb.Pdb 方法)

(profile.Profile 方法)

(在 pdb 模块中)

runcode() (code.InteractiveInterpreter 方法)

runctx() (bdb.Bdb 方法)

(profile.Profile 方法)

(trace.Trace 方法)

(在 profile 模块中)

runeval() (bdb.Bdb 方法)

(pdb.Pdb 方法)

(在 pdb 模块中)

runfunc() (trace.Trace 方法)

running() (concurrent.futures.Future 方法)

runpy (模块)

runsource() (code.InteractiveInterpreter 方法)

runtime_library_dir_option() (distutils.ccompiler.CCompiler 方法)

RuntimeError

RuntimeWarning

RUSAGE_BOTH() (在 resource 模块中)

RUSAGE_CHILDREN() (在 resource 模块中)

RUSAGE_SELF() (在 resource 模块中)

RUSAGE_THREAD() (在 resource 模块中)

S

S() (在 re 模块中)

S_ENFMT() (在 stat 模块中)

S_IEXEC() (在 stat 模块中)

S_IFBLK() (在 stat 模块中)

S_IFCHR() (在 stat 模块中)

S_IFDIR() (在 stat 模块中)

S_IFDOOR() (在 stat 模块中)

S_IFIFO() (在 stat 模块中)

S_IFLNK() (在 stat 模块中)

S_IFMT() (在 stat 模块中)

S_IFPORT() (在 stat 模块中)

S_IFREG() (在 stat 模块中)

S_IFSOCK() (在 stat 模块中)

S_IFWHT() (在 stat 模块中)

S_IMODE() (在 stat 模块中)

S_IREAD() (在 stat 模块中)

S_IRGRP() (在 stat 模块中)

S_IROTH() (在 stat 模块中)

S_IRUSR() (在 stat 模块中)

S_IRWXG() (在 stat 模块中)

S_IRWXO() (在 stat 模块中)

S_IRWXU() (在 stat 模块中)

S_ISBLK() (在 stat 模块中)

S_ISCHR() (在 stat 模块中)

S_ISDIR() (在 stat 模块中)

S_ISDOOR() (在 stat 模块中)

S_ISFIFO() (在 stat 模块中)

S_ISGID() (在 stat 模块中)

S_ISLNK() (在 stat 模块中)

S_ISPORT() (在 stat 模块中)

S_ISREG() (在 stat 模块中)

S_ISSOCK() (在 stat 模块中)

S_ISUID() (在 stat 模块中)

S_ISVTX() (在 stat 模块中)

S_ISWHT() (在 stat 模块中)

S_IWGRP() (在 stat 模块中)

S_IWOTH() (在 stat 模块中)

S_IWRITE() (在 stat 模块中)

S_IWUSR() (在 stat 模块中)

S_IXGRP() (在 stat 模块中)

S_IXOTH() (在 stat 模块中)

S_IXUSR() (在 stat 模块中)

safe_substitute() (string.Template 方法)

saferepr() (在 pprint 模块中)

same_files (filecmp.dircmp 属性)

same_quantum() (decimal.Context 方法)

(decimal.Decimal 方法)

samefile() (pathlib.Path 方法)

(在 os.path 模块中)

SameFileError

sameopenfile() (在 os.path 模块中)

samestat() (在 os.path 模块中)

sample() (在 random 模块中)

save() (http.cookiejar.FileCookieJar 方法)

SaveKey() (在 winreg 模块中)

savetty() (在 curses 模块中)

SAX2DOM (xml.dom.pulldom 中的类)

SAXException

SAXNotRecognizedException

SAXNotSupportedException

SAXParseException

scaleb() (decimal.Context 方法)

(decimal.Decimal 方法)

scandir() (在 os 模块中)

scanf()

sched (模块)

SCHED_BATCH() (在 os 模块中)

SCHED_FIFO() (在 os 模块中)

sched_get_priority_max() (在 os 模块中)

sched_get_priority_min() (在 os 模块中)

sched_getaffinity() (在 os 模块中)

sched_getparam() (在 os 模块中)

sched_getscheduler() (在 os 模块中)

SCHED_IDLE() (在 os 模块中)

SCHED_OTHER() (在 os 模块中)

sched_param (os 中的类)

sched_priority (os.sched_param 属性)

SCHED_RESET_ON_FORK() (在 os 模块中)

SCHED_RR() (在 os 模块中)

sched_rr_get_interval() (在 os 模块中)

sched_setaffinity() (在 os 模块中)

sched_setparam() (在 os 模块中)

sched_setscheduler() (在 os 模块中)

SCHED_SPORADIC() (在 os 模块中)

sched_yield() (在 os 模块中)

scheduler (sched 中的类)

schema() (在 msilib 模块中)

scope, [1]

Screen (turtle 中的类)

screensize() (在 turtle 模块中)

script_from_examples() (在 doctest 模块中)

scroll() (curses.window 方法)

ScrolledCanvas (turtle 中的类)

scrollok() (curses.window 方法)

scrypt() (在 hashlib 模块中)

sdterr

stdin stdout

search

path, module, [1], [2], [3], [4], [5], [6], [7]

search() (imaplib.IMAP4 方法)

(re.regex 方法)

(在 re 模块中)

second (datetime.datetime 属性)

(datetime.time 属性)

seconds since the epoch

secrets (模块)

SECTCRE (configparser.ConfigParser 属性)

sections() (configparser.ConfigParser 方法)

secure (http.cookiejar.Cookie 属性)

secure hash algorithm, SHA1, SHA224, SHA256, SHA384, SHA512

Secure Sockets Layer

security

CGI

see() (tkinter.ttk.Treeview 方法)

seed() (在 random 模块中)

seek() (chunk.Chunk 方法)

(io.IOBase 方法)

(io.TextIOBase 方法)

(mmap.mmap 方法)

SEEK_CUR() (在 os 模块中)

SEEK_END() (在 os 模块中)

SEEK_SET() (在 os 模块中)

seekable() (io.IOBase 方法)

seen_greeting (smtpd.SMTPChannel 属性)

Select (tkinter.tix 中的类)

select (模块)

select() (imaplib.IMAP4 方法)

(selectors.BaseSelector 方法)

(tkinter.ttk.Notebook 方法)

(在 select 模块中)

selected_alpn_protocol() (ssl.SSLSocket 方法)

selected_npn_protocol() (ssl.SSLSocket 方法)

selection() (tkinter.ttk.Treeview 方法)

selection_add() (tkinter.ttk.Treeview 方法)

selection_remove() (tkinter.ttk.Treeview 方法)

selection_set() (tkinter.ttk.Treeview 方法)

selection_toggle() (tkinter.ttk.Treeview 方法)

selector (urllib.request.Request 属性)

SelectorEventLoop (asyncio 中的类)

SelectorKey (selectors 中的类)

selectors (模块)

SelectSelector (selectors 中的类)

Semaphore (asyncio 中的类)

(multiprocessing 中的类)

(threading 中的类)

Semaphore() (multiprocessing.managers.SyncManager 方法)

semaphores, binary

SEMI() (在 token 模块中)

send() (asyncore.dispatcher 方法)

(coroutine 方法)

(generator 方法)

(http.client.HTTPConnection 方法)

(imaplib.IMAP4 方法)

(logging.handlers.DatagramHandler 方法)

(logging.handlers.SocketHandler 方法)

(multiprocessing.connection.Connection 方法)

(socket.socket 方法)

send_bytes() (multiprocessing.connection.Connection 方法)

send_error() (http.server.BaseHTTPRequestHandler 方法)

send_flowing_data() (formatter.writer 方法)

send_header() (http.server.BaseHTTPRequestHandler 方法)

send_hor_rule() (formatter.writer 方法)

send_label_data() (formatter.writer 方法)

send_line_break() (formatter.writer 方法)

send_literal_data() (formatter.writer 方法)

send_message() (smtplib.SMTP 方法)

send_paragraph() (formatter.writer 方法)

send_response() (http.server.BaseHTTPRequestHandler 方法)

send_response_only() (http.server.BaseHTTPRequestHandler 方法)

send_signal() (asyncio.asyncio.subprocess.Process 方法)

(asyncio.BaseSubprocessTransport 方法)

(subprocess.Popen 方法)

sendall() (socket.socket 方法)

sendcmd() (ftplib.FTP 方法)

sendfile() (socket.socket 方法)

(wsgiref.handlers.BaseHandler 方法)

(在 os 模块中)

sendmail() (smtplib.SMTP 方法)

sendmsg() (socket.socket 方法)

sendmsg_afalg() (socket.socket 方法)

sendto() (asyncio.DatagramTransport 方法)

(socket.socket 方法)

sentinel (multiprocessing.Process 属性)

sentinel() (在 unittest.mock 模块中)

sep() (在 os 模块中)

sequence

item

iteration

types, immutable

types, mutable

types, operations on, [1]

对象, [1], [2], [3], [4], [5], [6], [7], [8]

Sequence (collections.abc 中的类)

(typing 中的类)

sequence -- 序列

sequence() (在 msilib 模块中)

sequence2st() (在 parser 模块中)

SequenceMatcher (difflib 中的类), [1]

serializing

objects

serve_forever() (socketserver.BaseServer 方法)

server

WWW, [1]

Server (asyncio 中的类)

server (http.server.BaseHTTPRequestHandler 属性)

server_activate() (socketserver.BaseServer 方法)

server_address (socketserver.BaseServer 属性)

server_bind() (socketserver.BaseServer 方法)

server_close() (socketserver.BaseServer 方法)

server_hostname (ssl.SSLSocket 属性)

server_side (ssl.SSLSocket 属性)

server_software (wsgiref.handlers.BaseHandler 属性)

server_version (http.server.BaseHTTPRequestHandler 属性)

(http.server.SimpleHTTPRequestHandler 属性)

ServerProxy (xmlrpc.client 中的类)

service_actions() (socketserver.BaseServer 方法)

session (ssl.SSLSocket 属性)

session_reused (ssl.SSLSocket 属性)

session_stats() (ssl.SSLContext 方法)

set

display

对象, [1], [2], [3]

Set (collections.abc 中的类)

(typing 中的类)

set (內置类)

Set Breakpoint

set type

对象

set() (asyncio.Event 方法)

(configparser.ConfigParser 方法)

(configparser.RawConfigParser 方法)

(http.cookies.Morsel 方法)

(ossaudiodev.oss_mixer_device 方法)

(test.support.EnvironmentVarGuard 方法)

(threading.Event 方法)

(tkinter.ttk.Combobox 方法)

(tkinter.ttk.Treeview 方法)

(xml.etree.ElementTree.Element 方法)

SET_ADD (opcode)

set_all()

set_allowed_domains() (http.cookiejar.DefaultCookiePolicy 方法)

set_alpn_protocols() (ssl.SSLContext 方法)

set_app() (wsgiref.simple_server.WSGIServer 方法)

set_asyncgen_hooks() (在 sys 模块中)

set_authorizer() (sqlite3.Connection 方法)

set_auto_history() (在 readline 模块中)

set_blocked_domains() (http.cookiejar.DefaultCookiePolicy 方法)

set_blocking() (在 os 模块中)

set_boundary() (email.message.EmailMessage 方法)

(email.message.Message 方法)

set_break() (bdb.Bdb 方法)

set_charset() (email.message.Message 方法)

set_children() (tkinter.ttk.Treeview 方法)

set_ciphers() (ssl.SSLContext 方法)

set_completer() (在 readline 模块中)

set_completer_delims() (在 readline 模块中)

set_completion_display_matches_hook() (在 readline 模块中)

set_content() (email.contentmanager.ContentManager 方法)

(email.message.EmailMessage 方法)

(在 email.contentmanager 模块中)

set_continue() (bdb.Bdb 方法)

set_cookie() (http.cookiejar.CookieJar 方法)

set_cookie_if_ok() (http.cookiejar.CookieJar 方法)

set_coroutine_wrapper() (在 sys 模块中)

set_current() (msilib.Feature 方法)

set_data() (importlib.abc.SourceLoader 方法)

(importlib.machinery.SourceFileLoader 方法)

set_date() (mailbox.MaildirMessage 方法)

set_debug() (asyncio.AbstractEventLoop 方法)

(在 gc 模块中)

set_debuglevel() (ftplib.FTP 方法)

(http.client.HTTPConnection 方法)

(nntplib.NNTP 方法)

(poplib.POP3 方法)

(smtplib.SMTP 方法)

(telnetlib.Telnet 方法)

set_default_executor() (asyncio.AbstractEventLoop 方法)

set_default_type() (email.message.EmailMessage 方法)

(email.message.Message 方法)

set_default_verify_paths() (ssl.SSLContext 方法)

set_defaults() (argparse.ArgumentParser 方法)

(optparse.OptionParser 方法)

set_ecdh_curve() (ssl.SSLContext 方法)

set_errno() (在 ctypes 模块中)

set_event_loop() (asyncio.AbstractEventLoopPolicy 方法)

(在 asyncio 模块中)

set_event_loop_policy() (在 asyncio 模块中)

set_exception() (asyncio.Future 方法)

(asyncio.StreamReader 方法)

(concurrent.futures.Future 方法)

set_exception_handler() (asyncio.AbstractEventLoop 方法)

set_executable() (在 multiprocessing 模块中)

set_executables() (distutils.ccompiler.CCompiler 方法)

set_flags() (mailbox.MaildirMessage 方法)

(mailbox.mboxMessage 方法)

(mailbox.MMDFMessage 方法)

set_from() (mailbox.mboxMessage 方法)

(mailbox.MMDFMessage 方法)

set_handle_inheritable() (在 os 模块中)

set_history_length() (在 readline 模块中)

set_include_dirs() (distutils.ccompiler.CCompiler 方法)

set_info() (mailbox.MaildirMessage 方法)

set_inheritable() (socket.socket 方法)

(在 os 模块中)

set_labels() (mailbox.BabylMessage 方法)

set_last_error() (在 ctypes 模块中)

set_libraries() (distutils.ccompiler.CCompiler 方法)

set_library_dirs() (distutils.ccompiler.CCompiler 方法)

set_link_objects() (distutils.ccompiler.CCompiler 方法)

set_literal (2to3 fixer)

set_loader() (在 importlib.util 模块中)

set_next() (bdb.Bdb 方法)

set_nonstandard_attr() (http.cookiejar.Cookie 方法)

set_npn_protocols() (ssl.SSLContext 方法)

set_ok() (http.cookiejar.CookiePolicy 方法)

set_option_negotiation_callback() (telnetlib.Telnet 方法)

set_output_charset() (gettext.NullTranslations 方法)

set_package() (在 importlib.util 模块中)

set_param() (email.message.EmailMessage 方法)

(email.message.Message 方法)

set_pasv() (ftplib.FTP 方法)

set_payload() (email.message.Message 方法)

set_policy() (http.cookiejar.CookieJar 方法)

set_position() (xdrlib.Unpacker 方法)

set_pre_input_hook() (在 readline 模块中)

set_progress_handler() (sqlite3.Connection 方法)

set_protocol() (asyncio.BaseTransport 方法)

set_proxy() (urllib.request.Request 方法)

set_python_build() (在 distutils.sysconfig 模块中)

set_quit() (bdb.Bdb 方法)

set_recsrc() (ossaudiodev.oss_mixer_device 方法)

set_result() (asyncio.Future 方法)

(concurrent.futures.Future 方法)

set_return() (bdb.Bdb 方法)

set_running_or_notify_cancel() (concurrent.futures.Future 方法)

set_runtime_library_dirs() (distutils.ccompiler.CCompiler 方法)

set_seq1() (difflib.SequenceMatcher 方法)

set_seq2() (difflib.SequenceMatcher 方法)

set_seqs() (difflib.SequenceMatcher 方法)

set_sequences() (mailbox.MH 方法)

(mailbox.MHMessage 方法)

set_server_documentation() (xmlrpc.server.DocCGIXMLRPCRequestHandler 方法)

(xmlrpc.server.DocXMLRPCServer 方法)

set_server_name() (xmlrpc.server.DocCGIXMLRPCRequestHandler 方法)

(xmlrpc.server.DocXMLRPCServer 方法)

set_server_title() (xmlrpc.server.DocCGIXMLRPCRequestHandler 方法)

(xmlrpc.server.DocXMLRPCServer 方法)

set_servername_callback() (ssl.SSLContext 方法)

set_spacing() (formatter.formatter 方法)

set_start_method() (在 multiprocessing 模块中)

set_startup_hook() (在 readline 模块中)

set_step() (bdb.Bdb 方法)

set_subdir() (mailbox.MaildirMessage 方法)

set_task_factory() (asyncio.AbstractEventLoop 方法)

set_terminator() (asynchat.async_chat 方法)

set_threshold() (在 gc 模块中)

set_trace() (bdb.Bdb 方法)

(pdb.Pdb 方法)

(在 bdb 模块中)

(在 pdb 模块中)

set_trace_callback() (sqlite3.Connection 方法)

set_transport() (asyncio.StreamReader 方法)

set_tunnel() (http.client.HTTPConnection 方法)

set_type() (email.message.Message 方法)

set_unittest_reportflags() (在 doctest 模块中)

set_unixfrom() (email.message.EmailMessage 方法)

(email.message.Message 方法)

set_until() (bdb.Bdb 方法)

set_url() (urllib.robotparser.RobotFileParser 方法)

set_usage() (optparse.OptionParser 方法)

set_userptr() (curses.panel.Panel 方法)

set_visible() (mailbox.BabylMessage 方法)

set_wakeup_fd() (在 signal 模块中)

set_write_buffer_limits() (asyncio.WriteTransport 方法)

setacl() (imaplib.IMAP4 方法)

setannotation() (imaplib.IMAP4 方法)

setattr() (內置函数)

setAttribute() (xml.dom.Element 方法)

setAttributeNode() (xml.dom.Element 方法)

setAttributeNodeNS() (xml.dom.Element 方法)

setAttributeNS() (xml.dom.Element 方法)

SetBase() (xml.parsers.expat.xmlparser 方法)

setblocking() (socket.socket 方法)

setByteStream() (xml.sax.xmlreader.InputSource 方法)

setcbreak() (在 tty 模块中)

setCharacterStream() (xml.sax.xmlreader.InputSource 方法)

setcheckinterval() (在 sys 模块中)

setcomptype() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

setContentHandler() (xml.sax.xmlreader.XMLReader 方法)

setcontext() (在 decimal 模块中)

setDaemon() (threading.Thread 方法)

setdefault() (dict 方法)

(http.cookies.Morsel 方法)

setdefaulttimeout() (在 socket 模块中)

setdlopenflags() (在 sys 模块中)

setDocumentLocator() (xml.sax.handler.ContentHandler 方法)

setDTDHandler() (xml.sax.xmlreader.XMLReader 方法)

setegid() (在 os 模块中)

setEncoding() (xml.sax.xmlreader.InputSource 方法)

setEntityResolver() (xml.sax.xmlreader.XMLReader 方法)

setErrorHandler() (xml.sax.xmlreader.XMLReader 方法)

seteuid() (在 os 模块中)

setFeature() (xml.sax.xmlreader.XMLReader 方法)

setfirstweekday() (在 calendar 模块中)

setfmt() (ossaudiodev.oss_audio_device 方法)

setFormatter() (logging.Handler 方法)

setframerate() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

setgid() (在 os 模块中)

setgroups() (在 os 模块中)

seth() (在 turtle 模块中)

setheading() (在 turtle 模块中)

sethostname() (在 socket 模块中)

SetInteger() (msilib.Record 方法)

setitem() (在 operator 模块中)

setitimer() (在 signal 模块中)

setLevel() (logging.Handler 方法)

(logging.Logger 方法)

setLocale() (xml.sax.xmlreader.XMLReader 方法)

setlocale() (在 locale 模块中)

setLoggerClass() (在 logging 模块中)

setlogmask() (在 syslog 模块中)

setLogRecordFactory() (在 logging 模块中)

setmark() (aifc.aifc 方法)

setMaxConns() (urllib.request.CacheFTPHandler 方法)

setmode() (在 msvcrt 模块中)

setName() (threading.Thread 方法)

setnchannels() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

setnframes() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

SetParamEntityParsing() (xml.parsers.expat.xmlparser 方法)

setparameters() (ossaudiodev.oss_audio_device 方法)

setparams() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

setpassword() (zipfile.ZipFile 方法)

setpgid() (在 os 模块中)

setpgrp() (在 os 模块中)

setpos() (aifc.aifc 方法)

(sunau.AU_read 方法)

(wave.Wave_read 方法)

(在 turtle 模块中)

setposition() (在 turtle 模块中)

setpriority() (在 os 模块中)

setprofile() (在 sys 模块中)

(在 threading 模块中)

SetProperty() (msilib.SummaryInformation 方法)

setProperty() (xml.sax.xmlreader.XMLReader 方法)

setPublicId() (xml.sax.xmlreader.InputSource 方法)

setquota() (imaplib.IMAP4 方法)

setraw() (在 tty 模块中)

setrecursionlimit() (在 sys 模块中)

setregid() (在 os 模块中)

setresgid() (在 os 模块中)

setresuid() (在 os 模块中)

setreuid() (在 os 模块中)

setrlimit() (在 resource 模块中)

setsampwidth() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

setscrreg() (curses.window 方法)

setsid() (在 os 模块中)

setsockopt() (socket.socket 方法), [1], [2]

setstate() (codecs.IncrementalDecoder 方法)

(codecs.IncrementalEncoder 方法)

(在 random 模块中)

SetStream() (msilib.Record 方法)

SetString() (msilib.Record 方法)

setswitchinterval() (in module sys)

(在 sys 模块中)

setSystemId() (xml.sax.xmlreader.InputSource 方法)

setsyx() (在 curses 模块中)

setTarget() (logging.handlers.MemoryHandler 方法)

settiltangle() (在 turtle 模块中)

settimeout() (socket.socket 方法)

setTimeout() (urllib.request.CacheFTPHandler 方法)

settrace() (在 sys 模块中)

(在 threading 模块中)

setuid() (在 os 模块中)

setundobuffer() (在 turtle 模块中)

setup() (socketserver.BaseRequestHandler 方法)

setUp() (unittest.TestCase 方法)

setup() (在 distutils.core 模块中)

(在 turtle 模块中)

SETUP_ANNOTATIONS (opcode)

SETUP_ASYNC_WITH (opcode)

setup_environ() (wsgiref.handlers.BaseHandler 方法)

SETUP_EXCEPT (opcode)

SETUP_FINALLY (opcode)

SETUP_LOOP (opcode)

setup_python() (venv.EnvBuilder 方法)

setup_scripts() (venv.EnvBuilder 方法)

setup_testing_defaults() (在 wsgiref.util 模块中)

SETUP_WITH (opcode)

setUpClass() (unittest.TestCase 方法)

setupterm() (在 curses 模块中)

SetValue() (在 winreg 模块中)

SetValueEx() (在 winreg 模块中)

setworldcoordinates() (在 turtle 模块中)

setx() (在 turtle 模块中)

setxattr() (在 os 模块中)

sety() (在 turtle 模块中)

SF_APPEND() (在 stat 模块中)

SF_ARCHIVED() (在 stat 模块中)

SF_IMMUTABLE() (在 stat 模块中)

SF_MNOWAIT() (在 os 模块中)

SF_NODISKIO() (在 os 模块中)

SF_NOUNLINK() (在 stat 模块中)

SF_SNAPSHOT() (在 stat 模块中)

SF_SYNC() (在 os 模块中)

shape (memoryview 属性)

Shape (turtle 中的类)

shape() (在 turtle 模块中)

shapesize() (在 turtle 模块中)

shapetransform() (在 turtle 模块中)

share() (socket.socket 方法)

shared_ciphers() (ssl.SSLSocket 方法)

shared_object_filename() (distutils.ccompiler.CCompiler 方法)

shearfactor() (在 turtle 模块中)

Shelf (shelve 中的类)

shelve

模块

shelve (模块)

shield() (在 asyncio 模块中)

shift() (decimal.Context 方法)

(decimal.Decimal 方法)

shift_path_info() (在 wsgiref.util 模块中)

shifting

operation

operations

shlex (shlex 中的类)

(模块)

shortDescription() (unittest.TestCase 方法)

shorten() (在 textwrap 模块中)

shouldFlush() (logging.handlers.BufferingHandler 方法)

(logging.handlers.MemoryHandler 方法)

shouldStop (unittest.TestResult 属性)

show() (curses.panel.Panel 方法)

show_code() (在 dis 模块中)

show_compilers() (在 distutils.ccompiler 模块中)

showsyntaxerror() (code.InteractiveInterpreter 方法)

showtraceback() (code.InteractiveInterpreter 方法)

showturtle() (在 turtle 模块中)

showwarning() (在 warnings 模块中)

shuffle() (在 random 模块中)

shutdown() (concurrent.futures.Executor 方法)

(imaplib.IMAP4 方法)

(multiprocessing.managers.BaseManager 方法)

(socket.socket 方法)

(socketserver.BaseServer 方法)

(在 logging 模块中)

shutdown_asyncgens() (asyncio.AbstractEventLoop 方法)

shutil (模块)

side_effect (unittest.mock.Mock 属性)

SIG_BLOCK() (在 signal 模块中)

SIG_DFL() (在 signal 模块中)

SIG_IGN() (在 signal 模块中)

SIG_SETMASK() (在 signal 模块中)

SIG_UNBLOCK() (在 signal 模块中)

SIGINT, [1]

siginterrupt() (在 signal 模块中)

signal

模块, [1]

signal (模块)

signal() (在 signal 模块中)

Signature (inspect 中的类)

signature (inspect.BoundArguments 属性)

signature() (在 inspect 模块中)

sigpending() (在 signal 模块中)

sigtimedwait() (在 signal 模块中)

sigwait() (在 signal 模块中)

sigwaitinfo() (在 signal 模块中)

simple

statement

Simple Mail Transfer Protocol

SimpleCookie (http.cookies 中的类)

simplefilter() (在 warnings 模块中)

SimpleHandler (wsgiref.handlers 中的类)

SimpleHTTPRequestHandler (http.server 中的类)

SimpleNamespace (types 中的类)

SimpleQueue (multiprocessing 中的类)

SimpleXMLRPCRequestHandler (xmlrpc.server 中的类)

SimpleXMLRPCServer (xmlrpc.server 中的类)

sin() (在 cmath 模块中)

(在 math 模块中)

single dispatch -- 单分派

SingleAddressHeader (email.headerregistry 中的类)

singledispatch() (在 functools 模块中)

singleton

tuple

sinh() (在 cmath 模块中)

(在 math 模块中)

SIO_KEEPALIVE_VALS() (在 socket 模块中)

SIO_LOOPBACK_FAST_PATH() (在 socket 模块中)

SIO_RCVALL() (在 socket 模块中)

site (模块)

site command line option

--user-base

--user-site

site-packages

directory

sitecustomize

模块

sixtofour (ipaddress.IPv6Address 属性)

size (struct.Struct 属性)

(tarfile.TarInfo 属性)

(tracemalloc.Statistic 属性)

(tracemalloc.StatisticDiff 属性)

(tracemalloc.Trace 属性)

size() (ftplib.FTP 方法)

(mmap.mmap 方法)

size_diff (tracemalloc.StatisticDiff 属性)

SIZE_MAX

Sized (collections.abc 中的类)

(typing 中的类)

sizeof() (在 ctypes 模块中)

skip() (chunk.Chunk 方法)

SKIP() (在 doctest 模块中)

skip() (在 unittest 模块中)

skip_unless_symlink() (在 test.support 模块中)

skipIf() (在 unittest 模块中)

skipinitialspace (csv.Dialect 属性)

skipped (unittest.TestResult 属性)

skippedEntity() (xml.sax.handler.ContentHandler 方法)

SkipTest

skipTest() (unittest.TestCase 方法)

skipUnless() (在 unittest 模块中)

SLASH() (在 token 模块中)

SLASHEQUAL() (在 token 模块中)

slave() (nntplib.NNTP 方法)

sleep() (在 asyncio 模块中)

(在 time 模块中)

slice

assignment

operation

內置函数, [1]

对象

slice (內置类)

slice -- 切片

slicing, [1], [2]

assignment

SMTP

protocol

SMTP (smtplib 中的类)

SMTP() (在 email.policy 模块中)

smtp_server (smtpd.SMTPChannel 属性)

SMTP_SSL (smtplib 中的类)

smtp_state (smtpd.SMTPChannel 属性)

SMTPAuthenticationError

SMTPChannel (smtpd 中的类)

SMTPConnectError

smtpd (模块)

SMTPDataError

SMTPException

SMTPHandler (logging.handlers 中的类)

SMTPHeloError

smtplib (模块)

SMTPNotSupportedError

SMTPRecipientsRefused

SMTPResponseException

SMTPSenderRefused

SMTPServer (smtpd 中的类)

SMTPServerDisconnected

SMTPUTF8() (在 email.policy 模块中)

Snapshot (tracemalloc 中的类)

SND_ALIAS() (在 winsound 模块中)

SND_ASYNC() (在 winsound 模块中)

SND_FILENAME() (在 winsound 模块中)

SND_LOOP() (在 winsound 模块中)

SND_MEMORY() (在 winsound 模块中)

SND_NODEFAULT() (在 winsound 模块中)

SND_NOSTOP() (在 winsound 模块中)

SND_NOWAIT() (在 winsound 模块中)

SND_PURGE() (在 winsound 模块中)

sndhdr (模块)

sniff() (csv.Sniffer 方法)

Sniffer (csv 中的类)

sock_accept() (asyncio.AbstractEventLoop 方法)

SOCK_CLOEXEC() (在 socket 模块中)

sock_connect() (asyncio.AbstractEventLoop 方法)

SOCK_DGRAM() (在 socket 模块中)

SOCK_NONBLOCK() (在 socket 模块中)

SOCK_RAW() (在 socket 模块中)

SOCK_RDM() (在 socket 模块中)

sock_recv() (asyncio.AbstractEventLoop 方法)

sock_sendall() (asyncio.AbstractEventLoop 方法)

SOCK_SEQPACKET() (在 socket 模块中)

SOCK_STREAM() (在 socket 模块中)

socket

对象

模块

socket (socketserver.BaseServer 属性)

(模块)

socket() (imaplib.IMAP4 方法)

(in module socket)

(在 socket 模块中)

socket_type (socketserver.BaseServer 属性)

SocketHandler (logging.handlers 中的类)

socketpair() (在 socket 模块中)

sockets (asyncio.Server 属性)

socketserver (模块)

SocketType() (在 socket 模块中)

SOL_ALG() (在 socket 模块中)

SOL_RDS() (在 socket 模块中)

SOMAXCONN() (在 socket 模块中)

sort() (imaplib.IMAP4 方法)

(list 方法)

sort_stats() (pstats.Stats 方法)

sorted() (內置函数)

sortTestMethodsUsing (unittest.TestLoader 属性)

source (doctest.Example 属性)

(pdb command)

(shlex.shlex 属性)

source character set

source_from_cache() (在 imp 模块中)

(在 importlib.util 模块中)

SOURCE_SUFFIXES() (在 importlib.machinery 模块中)

source_to_code() (importlib.abc.InspectLoader 静态方法)

SourceFileLoader (importlib.machinery 中的类)

sourcehook() (shlex.shlex 方法)

SourcelessFileLoader (importlib.machinery 中的类)

SourceLoader (importlib.abc 中的类)

space

in printf-style formatting, [1]

in string formatting

span() (re.match 方法)

spawn() (distutils.ccompiler.CCompiler 方法)

(在 pty 模块中)

spawnl() (在 os 模块中)

spawnle() (在 os 模块中)

spawnlp() (在 os 模块中)

spawnlpe() (在 os 模块中)

spawnv() (在 os 模块中)

spawnve() (在 os 模块中)

spawnvp() (在 os 模块中)

spawnvpe() (在 os 模块中)

spec_from_file_location() (在 importlib.util 模块中)

spec_from_loader() (在 importlib.util 模块中)

special

attribute

attribute, generic

special method -- 特殊方法

specified_attributes (xml.parsers.expat.xmlparser 属性)

speed() (ossaudiodev.oss_audio_device 方法)

(在 turtle 模块中)

split() (bytearray 方法)

(bytes 方法)

(re.regex 方法)

(str 方法)

(在 os.path 模块中)

(在 re 模块中)

(在 shlex 模块中)

split_quoted() (在 distutils.util 模块中)

splitdrive() (在 os.path 模块中)

splitext() (在 os.path 模块中)

splitlines() (bytearray 方法)

(bytes 方法)

(str 方法)

SplitResult (urllib.parse 中的类)

SplitResultBytes (urllib.parse 中的类)

splitunc() (在 os.path 模块中)

SpooledTemporaryFile() (在 tempfile 模块中)

sprintf-style formatting, [1]

spwd (模块)

sqlite3 (模块)

sqlite_version() (在 sqlite3 模块中)

sqlite_version_info() (在 sqlite3 模块中)

sqrt() (decimal.Context 方法)

(decimal.Decimal 方法)

(在 cmath 模块中)

(在 math 模块中)

SSL

ssl (模块)

SSL_CERT_FILE

SSL_CERT_PATH

ssl_version (ftplib.FTP_TLS 属性)

SSLContext (ssl 中的类)

SSLEOFError

SSLError

SSLErrorNumber (ssl 中的类)

SSLObject (ssl 中的类)

SSLSession (ssl 中的类)

SSLSocket (ssl 中的类)

SSLSyscallError

SSLWantReadError

SSLWantWriteError

SSLZeroReturnError

st() (在 turtle 模块中)

st2list() (在 parser 模块中)

st2tuple() (在 parser 模块中)

st_atime (os.stat_result 属性)

ST_ATIME() (在 stat 模块中)

st_atime_ns (os.stat_result 属性)

st_birthtime (os.stat_result 属性)

st_blksize (os.stat_result 属性)

st_blocks (os.stat_result 属性)

st_creator (os.stat_result 属性)

st_ctime (os.stat_result 属性)

ST_CTIME() (在 stat 模块中)

st_ctime_ns (os.stat_result 属性)

st_dev (os.stat_result 属性)

ST_DEV() (在 stat 模块中)

st_file_attributes (os.stat_result 属性)

st_flags (os.stat_result 属性)

st_gen (os.stat_result 属性)

st_gid (os.stat_result 属性)

ST_GID() (在 stat 模块中)

st_ino (os.stat_result 属性)

ST_INO() (在 stat 模块中)

st_mode (os.stat_result 属性)

ST_MODE() (在 stat 模块中)

st_mtime (os.stat_result 属性)

ST_MTIME() (在 stat 模块中)

st_mtime_ns (os.stat_result 属性)

st_nlink (os.stat_result 属性)

ST_NLINK() (在 stat 模块中)

st_rdev (os.stat_result 属性)

st_rsize (os.stat_result 属性)

st_size (os.stat_result 属性)

ST_SIZE() (在 stat 模块中)

st_type (os.stat_result 属性)

st_uid (os.stat_result 属性)

ST_UID() (在 stat 模块中)

stack

execution

trace

stack (traceback.TracebackException 属性)

stack viewer

stack() (在 inspect 模块中)

stack_effect() (在 dis 模块中)

stack_size() (在 _thread 模块中)

(在 threading 模块中)

stackable

streams

StackSummary (traceback 中的类)

stamp() (在 turtle 模块中)

standard

output

Standard C

standard input

standard_b64decode() (在 base64 模块中)

standard_b64encode() (在 base64 模块中)

standarderror (2to3 fixer)

standend() (curses.window 方法)

standout() (curses.window 方法)

STAR() (在 token 模块中)

STAREQUAL() (在 token 模块中)

starmap() (multiprocessing.pool.Pool 方法)

(在 itertools 模块中)

starmap_async() (multiprocessing.pool.Pool 方法)

start (range 属性)

(slice object attribute), [1]

(UnicodeError 属性)

start() (logging.handlers.QueueListener 方法)

(multiprocessing.managers.BaseManager 方法)

(multiprocessing.Process 方法)

(re.match 方法)

(threading.Thread 方法)

(tkinter.ttk.Progressbar 方法)

(xml.etree.ElementTree.TreeBuilder 方法)

(在 tracemalloc 模块中)

start_color() (在 curses 模块中)

start_component() (msilib.Directory 方法)

start_new_thread() (在 _thread 模块中)

start_server() (在 asyncio 模块中)

start_unix_server() (在 asyncio 模块中)

StartCdataSectionHandler() (xml.parsers.expat.xmlparser 方法)

StartDoctypeDeclHandler() (xml.parsers.expat.xmlparser 方法)

startDocument() (xml.sax.handler.ContentHandler 方法)

startElement() (xml.sax.handler.ContentHandler 方法)

StartElementHandler() (xml.parsers.expat.xmlparser 方法)

startElementNS() (xml.sax.handler.ContentHandler 方法)

STARTF_USESHOWWINDOW() (在 subprocess 模块中)

STARTF_USESTDHANDLES() (在 subprocess 模块中)

startfile() (在 os 模块中)

StartNamespaceDeclHandler() (xml.parsers.expat.xmlparser 方法)

startPrefixMapping() (xml.sax.handler.ContentHandler 方法)

startswith() (bytearray 方法)

(bytes 方法)

(str 方法)

startTest() (unittest.TestResult 方法)

startTestRun() (unittest.TestResult 方法)

starttls() (imaplib.IMAP4 方法)

(nntplib.NNTP 方法)

(smtplib.SMTP 方法)

STARTUPINFO (subprocess 中的类)

stat

模块

stat (模块)

stat() (nntplib.NNTP 方法)

(os.DirEntry 方法)

(pathlib.Path 方法)

(poplib.POP3 方法)

(在 os 模块中)

stat_float_times() (在 os 模块中)

stat_result (os 中的类)

state() (tkinter.ttk.Widget 方法)

statement

assignment, [1]

assignment, annotated

assignment, augmented

compound

expression

future

loop, [1], [2], [3]

simple

statement -- 语句

statement grouping

staticmethod

內置函数

staticmethod() (內置函数)

Statistic (tracemalloc 中的类)

StatisticDiff (tracemalloc 中的类)

statistics (模块)

statistics() (tracemalloc.Snapshot 方法)

StatisticsError

Stats (pstats 中的类)

status (http.client.HTTPResponse 属性)

status() (imaplib.IMAP4 方法)

statvfs() (在 os 模块中)

STD_ERROR_HANDLE() (在 subprocess 模块中)

STD_INPUT_HANDLE() (在 subprocess 模块中)

STD_OUTPUT_HANDLE() (在 subprocess 模块中)

StdButtonBox (tkinter.tix 中的类)

stderr (asyncio.asyncio.subprocess.Process 属性)

(in module sys), [1]

(subprocess.CalledProcessError 属性)

(subprocess.CompletedProcess 属性)

(subprocess.Popen 属性)

(subprocess.TimeoutExpired 属性)

stderr() (在 sys 模块中)

stdev() (在 statistics 模块中)

stdin

stdout sdterr

stdin (asyncio.asyncio.subprocess.Process 属性)

(in module sys), [1]

(subprocess.Popen 属性)

stdin() (在 sys 模块中)

stdio

stdout

sdterr, stdin

stdout (asyncio.asyncio.subprocess.Process 属性)

(in module sys), [1]

(subprocess.CalledProcessError 属性)

(subprocess.CompletedProcess 属性)

(subprocess.Popen 属性)

(subprocess.TimeoutExpired 属性)

STDOUT() (在 subprocess 模块中)

stdout() (在 sys 模块中)

step (pdb command)

(range 属性)

(slice object attribute), [1]

step() (tkinter.ttk.Progressbar 方法)

stereocontrols() (ossaudiodev.oss_mixer_device 方法)

stls() (poplib.POP3 方法)

stop (range 属性)

(slice object attribute), [1]

stop() (asyncio.AbstractEventLoop 方法)

(logging.handlers.QueueListener 方法)

(tkinter.ttk.Progressbar 方法)

(unittest.TestResult 方法)

(在 tracemalloc 模块中)

stop_here() (bdb.Bdb 方法)

StopAsyncIteration

例外

StopIteration

例外, [1]

stopListening() (在 logging.config 模块中)

stopTest() (unittest.TestResult 方法)

stopTestRun() (unittest.TestResult 方法)

storbinary() (ftplib.FTP 方法)

store() (imaplib.IMAP4 方法)

STORE_ACTIONS (optparse.Option 属性)

STORE_ANNOTATION (opcode)

STORE_ATTR (opcode)

STORE_DEREF (opcode)

STORE_FAST (opcode)

STORE_GLOBAL (opcode)

STORE_NAME (opcode)

STORE_SUBSCR (opcode)

storlines() (ftplib.FTP 方法)

str (built-in class)

(see also string)

str (內置类)

str() (在 locale 模块中)

strcoll() (在 locale 模块中)

StreamError

StreamHandler (logging 中的类)

StreamReader (asyncio 中的类)

(codecs 中的类)

streamreader (codecs.CodecInfo 属性)

StreamReaderProtocol (asyncio 中的类)

StreamReaderWriter (codecs 中的类)

StreamRecoder (codecs 中的类)

StreamRequestHandler (socketserver 中的类)

streams

stackable

StreamWriter (asyncio 中的类)

(codecs 中的类)

streamwriter (codecs.CodecInfo 属性)

strerror (OSError 属性)

strerror()

(在 os 模块中)

strftime() (datetime.date 方法)

(datetime.datetime 方法)

(datetime.time 方法)

(在 time 模块中)

strict (csv.Dialect 属性)

strict() (在 email.policy 模块中)

strict_domain (http.cookiejar.DefaultCookiePolicy 属性)

strict_errors() (在 codecs 模块中)

strict_ns_domain (http.cookiejar.DefaultCookiePolicy 属性)

strict_ns_set_initial_dollar (http.cookiejar.DefaultCookiePolicy 属性)

strict_ns_set_path (http.cookiejar.DefaultCookiePolicy 属性)

strict_ns_unverifiable (http.cookiejar.DefaultCookiePolicy 属性)

strict_rfc2965_unverifiable (http.cookiejar.DefaultCookiePolicy 属性)

strides (memoryview 属性)

string

__format__() (object method)

__str__() (object method)

conversion, [1]

format() (built-in function)

formatted literal

formatting, printf

immutable sequences

interpolated literal

interpolation, printf

item

methods

object representation

PyObject_Str (C function)

str (built-in class)

str() (built-in function)

text sequence type

对象, [1], [2]

模块

string (re.match 属性)

(模块)

string literal

STRING() (在 token 模块中)

string_at() (在 ctypes 模块中)

StringIO (io 中的类)

stringprep (模块)

strings, documentation, [1]

strip() (bytearray 方法)

(bytes 方法)

(str 方法)

strip_dirs() (pstats.Stats 方法)

stripspaces (curses.textpad.Textbox 属性)

strptime() (datetime.datetime 类方法)

(在 time 模块中)

strtobool() (在 distutils.util 模块中)

struct

模块

Struct (struct 中的类)

struct (模块)

struct sequence

struct_time (time 中的类)

Structure (ctypes 中的类)

structures

C

strxfrm() (在 locale 模块中)

STType() (在 parser 模块中)

style

coding

Style (tkinter.ttk 中的类)

sub() (re.regex 方法)

(在 operator 模块中)

(在 re 模块中)

sub_commands (distutils.cmd.Command 属性)

subclassing

immutable types

subdirs (filecmp.dircmp 属性)

SubElement() (在 xml.etree.ElementTree 模块中)

submit() (concurrent.futures.Executor 方法)

submodule_search_locations (importlib.machinery.ModuleSpec 属性)

subn() (re.regex 方法)

(在 re 模块中)

subnets() (ipaddress.IPv4Network 方法)

(ipaddress.IPv6Network 方法)

Subnormal (decimal 中的类)

suboffsets (memoryview 属性)

subpad() (curses.window 方法)

subprocess (模块)

subprocess_exec() (asyncio.AbstractEventLoop 方法)

subprocess_shell() (asyncio.AbstractEventLoop 方法)

SubprocessError

SubprocessProtocol (asyncio 中的类)

subscribe() (imaplib.IMAP4 方法)

subscript

assignment

operation

subscription, [1], [2], [3]

assignment

subsequent_indent (textwrap.TextWrapper 属性)

subst_vars() (在 distutils.util 模块中)

substitute() (string.Template 方法)

subTest() (unittest.TestCase 方法)

subtract() (collections.Counter 方法)

(decimal.Context 方法)

subtraction

subtype (email.headerregistry.ContentTypeHeader 属性)

subwin() (curses.window 方法)

successful() (multiprocessing.pool.AsyncResult 方法)

suffix_map (mimetypes.MimeTypes 属性)

suffix_map() (在 mimetypes 模块中)

suite

suite() (在 parser 模块中)

suiteClass (unittest.TestLoader 属性)

sum() (內置函数)

sum_list()

sum_sequence(), [1]

summarize() (doctest.DocTestRunner 方法)

summarize_address_range() (在 ipaddress 模块中)

sunau (模块)

super (pyclbr.Class 属性)

super() (內置函数)

supernet() (ipaddress.IPv4Network 方法)

(ipaddress.IPv6Network 方法)

supports_bytes_environ() (在 os 模块中)

supports_dir_fd() (在 os 模块中)

supports_effective_ids() (在 os 模块中)

supports_fd() (在 os 模块中)

supports_follow_symlinks() (在 os 模块中)

supports_unicode_filenames() (在 os.path 模块中)

SupportsAbs (typing 中的类)

SupportsBytes (typing 中的类)

SupportsComplex (typing 中的类)

SupportsFloat (typing 中的类)

SupportsInt (typing 中的类)

SupportsRound (typing 中的类)

suppress() (在 contextlib 模块中)

SuppressCrashReport (test.support 中的类)

SW_HIDE() (在 subprocess 模块中)

swapcase() (bytearray 方法)

(bytes 方法)

(str 方法)

sym_name() (在 symbol 模块中)

Symbol (symtable 中的类)

symbol (模块)

SymbolTable (symtable 中的类)

symlink() (在 os 模块中)

symlink_to() (pathlib.Path 方法)

symmetric_difference() (frozenset 方法)

symmetric_difference_update() (frozenset 方法)

symtable (模块)

symtable() (在 symtable 模块中)

sync() (dbm.dumb.dumbdbm 方法)

(dbm.gnu.gdbm 方法)

(ossaudiodev.oss_audio_device 方法)

(shelve.Shelf 方法)

(在 os 模块中)

syncdown() (curses.window 方法)

synchronized() (在 multiprocessing.sharedctypes 模块中)

SyncManager (multiprocessing.managers 中的类)

syncok() (curses.window 方法)

syncup() (curses.window 方法)

syntax

SyntaxErr

SyntaxError

SyntaxWarning

sys

模块, [1], [2], [3], [4], [5], [6]

sys (模块)

sys.exc_info

sys.last_traceback

sys.meta_path

sys.modules

sys.path

sys.path_hooks

sys.path_importer_cache

sys.stderr

sys.stdin

sys.stdout

sys_exc (2to3 fixer)

sys_version (http.server.BaseHTTPRequestHandler 属性)

sysconf() (在 os 模块中)

sysconf_names() (在 os 模块中)

sysconfig (模块)

syslog (模块)

syslog() (在 syslog 模块中)

SysLogHandler (logging.handlers 中的类)

system() (在 os 模块中)

(在 platform 模块中)

system_alias() (在 platform 模块中)

SystemError

(built-in exception), [1]

SystemExit

(built-in exception)

systemId (xml.dom.DocumentType 属性)

SystemRandom (random 中的类)

(secrets 中的类)

SystemRoot

T

T_FMT() (在 locale 模块中)

T_FMT_AMPM() (在 locale 模块中)

tab

tab() (tkinter.ttk.Notebook 方法)

TabError

tabnanny (模块)

tabs() (tkinter.ttk.Notebook 方法)

tabsize (textwrap.TextWrapper 属性)

tabular

data

tag (xml.etree.ElementTree.Element 属性)

tag_bind() (tkinter.ttk.Treeview 方法)

tag_configure() (tkinter.ttk.Treeview 方法)

tag_has() (tkinter.ttk.Treeview 方法)

tagName (xml.dom.Element 属性)

tail (xml.etree.ElementTree.Element 属性)

take_snapshot() (在 tracemalloc 模块中)

takewhile() (在 itertools 模块中)

tan() (在 cmath 模块中)

(在 math 模块中)

tanh() (在 cmath 模块中)

(在 math 模块中)

TarError

TarFile (tarfile 中的类), [1]

tarfile (模块)

tarfile command line option

--create <tarfile> <source1> ... <sourceN>

--extract <tarfile> [<output_dir>]

--list <tarfile>

--test <tarfile>

--verbose

-c <tarfile> <source1> ... <sourceN>

-e <tarfile> [<output_dir>]

-l <tarfile>

-t <tarfile>

-v

target

deletion

list, [1], [2]

list assignment

list, deletion

loop control

target (xml.dom.ProcessingInstruction 属性)

TarInfo (tarfile 中的类)

Task (asyncio 中的类)

task_done() (asyncio.Queue 方法)

(multiprocessing.JoinableQueue 方法)

(queue.Queue 方法)

tau() (在 cmath 模块中)

(在 math 模块中)

tb_frame (traceback attribute)

tb_lasti (traceback attribute)

tb_lineno (traceback attribute)

tb_locals (unittest.TestResult 属性)

tb_next (traceback attribute)

tbreak (pdb command)

tcdrain() (在 termios 模块中)

tcflow() (在 termios 模块中)

tcflush() (在 termios 模块中)

tcgetattr() (在 termios 模块中)

tcgetpgrp() (在 os 模块中)

Tcl() (在 tkinter 模块中)

TCL_LIBRARY

TCPServer (socketserver 中的类)

tcsendbreak() (在 termios 模块中)

tcsetattr() (在 termios 模块中)

tcsetpgrp() (在 os 模块中)

tearDown() (unittest.TestCase 方法)

tearDownClass() (unittest.TestCase 方法)

tee() (在 itertools 模块中)

tell() (aifc.aifc 方法), [1]

(chunk.Chunk 方法)

(io.IOBase 方法)

(io.TextIOBase 方法)

(mmap.mmap 方法)

(sunau.AU_read 方法)

(sunau.AU_write 方法)

(wave.Wave_read 方法)

(wave.Wave_write 方法)

Telnet (telnetlib 中的类)

telnetlib (模块)

TEMP

temp_cwd() (在 test.support 模块中)

temp_dir() (在 test.support 模块中)

temp_umask() (在 test.support 模块中)

tempdir() (在 tempfile 模块中)

tempfile (模块)

Template (pipes 中的类)

(string 中的类)

template (string.Template 属性)

temporary

file

file name

TemporaryDirectory() (在 tempfile 模块中)

TemporaryFile() (在 tempfile 模块中)

teredo (ipaddress.IPv6Address 属性)

TERM, [1]

termattrs() (在 curses 模块中)

terminal_size (os 中的类)

terminate() (asyncio.asyncio.subprocess.Process 方法)

(asyncio.BaseSubprocessTransport 方法)

(multiprocessing.pool.Pool 方法)

(multiprocessing.Process 方法)

(subprocess.Popen 方法)

termination model

termios (模块)

termname() (在 curses 模块中)

ternary

operator

test

identity

membership

test (doctest.DocTestFailure 属性)

(doctest.UnexpectedException 属性)

(模块)

test() (在 cgi 模块中)

test.support (模块)

TestCase (unittest 中的类)

TestFailed

testfile() (在 doctest 模块中)

TESTFN() (在 test.support 模块中)

TestLoader (unittest 中的类)

testMethodPrefix (unittest.TestLoader 属性)

testmod() (在 doctest 模块中)

TestResult (unittest 中的类)

tests() (在 imghdr 模块中)

testsource() (在 doctest 模块中)

testsRun (unittest.TestResult 属性)

TestSuite (unittest 中的类)

testzip() (zipfile.ZipFile 方法)

text (traceback.TracebackException 属性)

Text (typing 中的类)

text (xml.etree.ElementTree.Element 属性)

text encoding -- 文本编码

text file -- 文本文件

text mode

text() (msilib.Dialog 方法)

(在 msilib 模块中)

text_factory (sqlite3.Connection 属性)

Textbox (curses.textpad 中的类)

TextCalendar (calendar 中的类)

textdomain() (在 gettext 模块中)

(在 locale 模块中)

TextFile (distutils.text_file 中的类)

textinput() (在 turtle 模块中)

TextIO (typing 中的类)

TextIOBase (io 中的类)

TextIOWrapper (io 中的类)

TextTestResult (unittest 中的类)

TextTestRunner (unittest 中的类)

textwrap (模块)

TextWrapper (textwrap 中的类)

theme_create() (tkinter.ttk.Style 方法)

theme_names() (tkinter.ttk.Style 方法)

theme_settings() (tkinter.ttk.Style 方法)

theme_use() (tkinter.ttk.Style 方法)

THOUSEP() (在 locale 模块中)

Thread (threading 中的类)

thread() (imaplib.IMAP4 方法)

thread_info() (在 sys 模块中)

threading (模块)

ThreadingMixIn (socketserver 中的类)

ThreadingTCPServer (socketserver 中的类)

ThreadingUDPServer (socketserver 中的类)

ThreadPoolExecutor (concurrent.futures 中的类)

threads

POSIX

throw (2to3 fixer)

throw() (coroutine 方法)

(generator 方法)

ticket_lifetime_hint (ssl.SSLSession 属性)

tigetflag() (在 curses 模块中)

tigetnum() (在 curses 模块中)

tigetstr() (在 curses 模块中)

TILDE() (在 token 模块中)

tilt() (在 turtle 模块中)

tiltangle() (在 turtle 模块中)

time (datetime 中的类)

(ssl.SSLSession 属性)

(模块)

time() (asyncio.AbstractEventLoop 方法)

(datetime.datetime 方法)

(在 time 模块中)

Time2Internaldate() (在 imaplib 模块中)

timedelta (datetime 中的类)

TimedRotatingFileHandler (logging.handlers 中的类)

timegm() (在 calendar 模块中)

timeit (模块)

timeit command line option

--clock

--help

--number=N

--process

--repeat=N

--setup=S

--time

--unit=U

--verbose

-c

-h

-n N

-p

-r N

-s S

-t

-u

-v

timeit() (timeit.Timer 方法)

(在 timeit 模块中)

timeout

(socketserver.BaseServer 属性)

(ssl.SSLSession 属性)

(subprocess.TimeoutExpired 属性)

timeout() (curses.window 方法)

TIMEOUT_MAX() (在 _thread 模块中)

(在 threading 模块中)

TimeoutError, [1], [2], [3]

TimeoutExpired

Timer (threading 中的类)

(timeit 中的类)

times() (在 os 模块中)

timestamp() (datetime.datetime 方法)

timetuple() (datetime.date 方法)

(datetime.datetime 方法)

timetz() (datetime.datetime 方法)

timezone (datetime 中的类)

timezone() (在 time 模块中)

title() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 turtle 模块中)

Tix

tix_addbitmapdir() (tkinter.tix.tixCommand 方法)

tix_cget() (tkinter.tix.tixCommand 方法)

tix_configure() (tkinter.tix.tixCommand 方法)

tix_filedialog() (tkinter.tix.tixCommand 方法)

tix_getbitmap() (tkinter.tix.tixCommand 方法)

tix_getimage() (tkinter.tix.tixCommand 方法)

TIX_LIBRARY

tix_option_get() (tkinter.tix.tixCommand 方法)

tix_resetoptions() (tkinter.tix.tixCommand 方法)

tixCommand (tkinter.tix 中的类)

Tk

(tkinter 中的类)

(tkinter.tix 中的类)

Tk Option Data Types

TK_LIBRARY

Tkinter

tkinter (模块)

tkinter.scrolledtext (模块)

tkinter.tix (模块)

tkinter.ttk (模块)

TList (tkinter.tix 中的类)

TLS

TMP

TMPDIR

to_bytes() (int 方法)

to_eng_string() (decimal.Context 方法)

(decimal.Decimal 方法)

to_integral() (decimal.Decimal 方法)

to_integral_exact() (decimal.Context 方法)

(decimal.Decimal 方法)

to_integral_value() (decimal.Decimal 方法)

to_sci_string() (decimal.Context 方法)

ToASCII() (在 encodings.idna 模块中)

tobuf() (tarfile.TarInfo 方法)

tobytes() (array.array 方法)

(memoryview 方法)

today() (datetime.date 类方法)

(datetime.datetime 类方法)

tofile() (array.array 方法)

tok_name() (在 token 模块中)

token

(shlex.shlex 属性)

(模块)

token_bytes() (在 secrets 模块中)

token_hex() (在 secrets 模块中)

token_urlsafe() (在 secrets 模块中)

TokenError

tokenize (模块)

tokenize command line option

--exact

--help

-e

-h

tokenize() (在 tokenize 模块中)

tolist() (array.array 方法)

(memoryview 方法)

(parser.ST 方法)

tomono() (在 audioop 模块中)

toordinal() (datetime.date 方法)

(datetime.datetime 方法)

top() (curses.panel.Panel 方法)

(poplib.POP3 方法)

top_panel() (在 curses.panel 模块中)

toprettyxml() (xml.dom.minidom.Node 方法)

tostereo() (在 audioop 模块中)

tostring() (array.array 方法)

(在 xml.etree.ElementTree 模块中)

tostringlist() (在 xml.etree.ElementTree 模块中)

total_changes (sqlite3.Connection 属性)

total_ordering() (在 functools 模块中)

total_seconds() (datetime.timedelta 方法)

totuple() (parser.ST 方法)

touch() (pathlib.Path 方法)

touchline() (curses.window 方法)

touchwin() (curses.window 方法)

tounicode() (array.array 方法)

ToUnicode() (在 encodings.idna 模块中)

towards() (在 turtle 模块中)

toxml() (xml.dom.minidom.Node 方法)

tp_as_async (C 成员)

tp_as_mapping (C 成员)

tp_as_number (C 成员)

tp_as_sequence (C 成员)

tparm() (在 curses 模块中)

trace

stack

Trace (trace 中的类)

(tracemalloc 中的类)

trace (模块)

trace command line option

--count

--coverdir=<dir>

--file=<file>

--help

--ignore-dir=<dir>

--ignore-module=<mod>

--listfuncs

--missing

--no-report

--report

--summary

--timing

--trace

--trackcalls

--version

-c

-C

-f

-g

-l

-m

-r

-R

-s

-t

-T

trace function, [1], [2]

trace() (在 inspect 模块中)

trace_dispatch() (bdb.Bdb 方法)

traceback

对象, [1], [2], [3], [4]

Traceback (tracemalloc 中的类)

traceback (tracemalloc.Statistic 属性)

(tracemalloc.StatisticDiff 属性)

(tracemalloc.Trace 属性)

(模块)

traceback_limit (tracemalloc.Snapshot 属性)

(wsgiref.handlers.BaseHandler 属性)

TracebackException (traceback 中的类)

tracebacklimit() (在 sys 模块中)

tracebacks

in CGI scripts

TracebackType() (在 types 模块中)

tracemalloc (模块)

tracer() (在 turtle 模块中)

traces (tracemalloc.Snapshot 属性)

trailing

comma

transfercmd() (ftplib.FTP 方法)

TransientResource (test.support 中的类)

translate() (bytearray 方法)

(bytes 方法)

(str 方法)

(在 fnmatch 模块中)

translation() (在 gettext 模块中)

transport (asyncio.StreamWriter 属性)

Transport Layer Security

traverseproc (C 类型)

Tree (tkinter.tix 中的类)

TreeBuilder (xml.etree.ElementTree 中的类)

Treeview (tkinter.ttk 中的类)

triangular() (在 random 模块中)

triple-quoted string

triple-quoted string -- 三引号字符串

True, [1], [2]

true

True (內置变量)

truediv() (在 operator 模块中)

trunc() (in module math)

(在 math 模块中)

truncate() (io.IOBase 方法)

(在 os 模块中)

truth

value

truth() (在 operator 模块中)

try

语句, [1], [2]

ttk

tty

I/O control

tty (模块)

ttyname() (在 os 模块中)

tuple

display

empty, [1]

singleton

內置函数, [1]

对象, [1], [2], [3], [4], [5], [6]

tuple (內置类)

Tuple() (在 typing 模块中)

tuple2st() (在 parser 模块中)

tuple_params (2to3 fixer)

turnoff_sigfpe() (在 fpectl 模块中)

turnon_sigfpe() (在 fpectl 模块中)

Turtle (turtle 中的类)

turtle (模块)

turtledemo (模块)

turtles() (在 turtle 模块中)

TurtleScreen (turtle 中的类)

turtlesize() (在 turtle 模块中)

type

Boolean

data

hierarchy

immutable data

operations on dictionary

operations on list

內置函数, [1], [2], [3]

对象, [1], [2]

type (optparse.Option 属性)

(socket.socket 属性)

(tarfile.TarInfo 属性)

Type (typing 中的类)

type (urllib.request.Request 属性)

(內置类)

type -- 类型

type alias -- 类型别名

type hint -- 类型提示

type of an object

TYPE_CHECKER (optparse.Option 属性)

TYPE_CHECKING() (在 typing 模块中)

typeahead() (在 curses 模块中)

typecode (array.array 属性)

typecodes() (在 array 模块中)

TYPED_ACTIONS (optparse.Option 属性)

typed_subpart_iterator() (在 email.iterators 模块中)

TypeError

例外

types

built-in

immutable sequence

mutable sequence

operations on integer

operations on mapping

operations on numeric

operations on sequence, [1]

模块

types (2to3 fixer)

TYPES (optparse.Option 属性)

types (模块)

types, internal

types_map (mimetypes.MimeTypes 属性)

types_map() (在 mimetypes 模块中)

types_map_inv (mimetypes.MimeTypes 属性)

TypeVar (typing 中的类)

typing (模块)

TZ, [1], [2], [3], [4]

tzinfo (datetime 中的类)

(datetime.datetime 属性)

(datetime.time 属性)

tzname() (datetime.datetime 方法)

(datetime.time 方法)

(datetime.timezone 方法)

(datetime.tzinfo 方法)

(在 time 模块中)

tzset() (在 time 模块中)

U

u"

string literal

u'

string literal

u-LAW, [1], [2]

ucd_3_2_0() (在 unicodedata 模块中)

udata (select.kevent 属性)

UDPServer (socketserver 中的类)

UF_APPEND() (在 stat 模块中)

UF_COMPRESSED() (在 stat 模块中)

UF_HIDDEN() (在 stat 模块中)

UF_IMMUTABLE() (在 stat 模块中)

UF_NODUMP() (在 stat 模块中)

UF_NOUNLINK() (在 stat 模块中)

UF_OPAQUE() (在 stat 模块中)

uid (tarfile.TarInfo 属性)

uid() (imaplib.IMAP4 方法)

uidl() (poplib.POP3 方法)

ulaw2lin() (在 audioop 模块中)

ULONG_MAX

umask() (在 os 模块中)

unalias (pdb command)

uname (tarfile.TarInfo 属性)

uname() (在 os 模块中)

(在 platform 模块中)

unary

arithmetic operation

bitwise operation

UNARY_INVERT (opcode)

UNARY_NEGATIVE (opcode)

UNARY_NOT (opcode)

UNARY_POSITIVE (opcode)

unbinding

name

UnboundLocalError, [1]

unbuffered I/O

UNC paths

and os.makedirs()

unconsumed_tail (zlib.Decompress 属性)

unctrl() (在 curses 模块中)

(在 curses.ascii 模块中)

undefine_macro() (distutils.ccompiler.CCompiler 方法)

Underflow (decimal 中的类)

undisplay (pdb command)

undo() (在 turtle 模块中)

undobufferentries() (在 turtle 模块中)

undoc_header (cmd.Cmd 属性)

unescape() (在 html 模块中)

(在 xml.sax.saxutils 模块中)

UnexpectedException

unexpectedSuccesses (unittest.TestResult 属性)

unget_wch() (在 curses 模块中)

ungetch() (在 curses 模块中)

(在 msvcrt 模块中)

ungetmouse() (在 curses 模块中)

ungetwch() (在 msvcrt 模块中)

unhexlify() (在 binascii 模块中)

Unicode, [1], [2]

database

unicode (2to3 fixer)

Unicode Consortium

unicodedata (模块)

UnicodeDecodeError

UnicodeEncodeError

UnicodeError

UnicodeTranslateError

UnicodeWarning

unidata_version() (在 unicodedata 模块中)

unified_diff() (在 difflib 模块中)

uniform() (在 random 模块中)

UnimplementedFileMode

Union (ctypes 中的类)

union() (frozenset 方法)

Union() (在 typing 模块中)

unique() (在 enum 模块中), [1]

unittest (模块)

unittest command line option

--buffer

--catch

--failfast

--locals

-b

-c

-f

unittest-discover command line option

--pattern pattern

--start-directory directory

--top-level-directory directory

--verbose

-p

-s

-t

-v

unittest.mock (模块)

universal newlines

bytearray.splitlines method

bytes.splitlines method

csv.reader function

importlib.abc.InspectLoader.get_source method

io.IncrementalNewlineDecoder class

io.TextIOWrapper class

open() built-in function

str.splitlines method

subprocess module

What's new, [1], [2], [3]

universal newlines -- 通用换行

UNIX

file control

I/O control

unix_dialect (csv 中的类)

UnixDatagramServer (socketserver 中的类)

UnixStreamServer (socketserver 中的类)

unknown_decl() (html.parser.HTMLParser 方法)

unknown_open() (urllib.request.BaseHandler 方法)

(urllib.request.UnknownHandler 方法)

UnknownHandler (urllib.request 中的类)

UnknownProtocol

UnknownTransferEncoding

unlink() (pathlib.Path 方法)

(xml.dom.minidom.Node 方法)

(在 os 模块中)

unlock() (mailbox.Babyl 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(mailbox.mbox 方法)

(mailbox.MH 方法)

(mailbox.MMDF 方法)

unpack() (struct.Struct 方法)

(在 struct 模块中)

unpack_archive() (在 shutil 模块中)

unpack_array() (xdrlib.Unpacker 方法)

unpack_bytes() (xdrlib.Unpacker 方法)

unpack_double() (xdrlib.Unpacker 方法)

UNPACK_EX (opcode)

unpack_farray() (xdrlib.Unpacker 方法)

unpack_float() (xdrlib.Unpacker 方法)

unpack_fopaque() (xdrlib.Unpacker 方法)

unpack_from() (struct.Struct 方法)

(在 struct 模块中)

unpack_fstring() (xdrlib.Unpacker 方法)

unpack_list() (xdrlib.Unpacker 方法)

unpack_opaque() (xdrlib.Unpacker 方法)

UNPACK_SEQUENCE (opcode)

unpack_string() (xdrlib.Unpacker 方法)

Unpacker (xdrlib 中的类)

unpacking

dictionary

in function calls

iterable

unparsedEntityDecl() (xml.sax.handler.DTDHandler 方法)

UnparsedEntityDeclHandler() (xml.parsers.expat.xmlparser 方法)

Unpickler (pickle 中的类)

UnpicklingError

unquote() (在 email.utils 模块中)

(在 urllib.parse 模块中)

unquote_plus() (在 urllib.parse 模块中)

unquote_to_bytes() (在 urllib.parse 模块中)

unreachable object

unreadline() (distutils.text_file.TextFile 方法)

unrecognized escape sequence

unregister() (select.devpoll 方法)

(select.epoll 方法)

(select.poll 方法)

(selectors.BaseSelector 方法)

(在 atexit 模块中)

(在 faulthandler 模块中)

unregister_archive_format() (在 shutil 模块中)

unregister_dialect() (在 csv 模块中)

unregister_unpack_format() (在 shutil 模块中)

unset() (test.support.EnvironmentVarGuard 方法)

unsetenv() (在 os 模块中)

UnstructuredHeader (email.headerregistry 中的类)

unsubscribe() (imaplib.IMAP4 方法)

UnsupportedOperation

until (pdb command)

untokenize() (在 tokenize 模块中)

untouchwin() (curses.window 方法)

unused_data (bz2.BZ2Decompressor 属性)

(lzma.LZMADecompressor 属性)

(zlib.Decompress 属性)

unverifiable (urllib.request.Request 属性)

unwrap() (ssl.SSLSocket 方法)

(在 inspect 模块中)

up (pdb command)

up() (在 turtle 模块中)

update() (collections.Counter 方法)

(dict 方法)

(frozenset 方法)

(hashlib.hash 方法)

(hmac.HMAC 方法)

(http.cookies.Morsel 方法)

(mailbox.Mailbox 方法)

(mailbox.Maildir 方法)

(trace.CoverageResults 方法)

(在 turtle 模块中)

update_authenticated() (urllib.request.HTTPPasswordMgrWithPriorAuth 方法)

update_lines_cols() (在 curses 模块中)

update_panels() (在 curses.panel 模块中)

update_visible() (mailbox.BabylMessage 方法)

update_wrapper() (在 functools 模块中)

upper() (bytearray 方法)

(bytes 方法)

(str 方法)

urandom() (在 os 模块中)

URL, [1], [2], [3]

parsing

relative

url (xmlrpc.client.ProtocolError 属性)

url2pathname() (在 urllib.request 模块中)

urlcleanup() (在 urllib.request 模块中)

urldefrag() (在 urllib.parse 模块中)

urlencode() (在 urllib.parse 模块中)

URLError

urljoin() (在 urllib.parse 模块中)

urllib (2to3 fixer)

(模块)

urllib.error (模块)

urllib.parse (模块)

urllib.request

模块

urllib.request (模块)

urllib.response (模块)

urllib.robotparser (模块)

urlopen() (在 urllib.request 模块中)

URLopener (urllib.request 中的类)

urlparse() (在 urllib.parse 模块中)

urlretrieve() (在 urllib.request 模块中)

urlsafe_b64decode() (在 base64 模块中)

urlsafe_b64encode() (在 base64 模块中)

urlsplit() (在 urllib.parse 模块中)

urlunparse() (在 urllib.parse 模块中)

urlunsplit() (在 urllib.parse 模块中)

urn (uuid.UUID 属性)

use_default_colors() (在 curses 模块中)

use_env() (在 curses 模块中)

use_rawinput (cmd.Cmd 属性)

UseForeignDTD() (xml.parsers.expat.xmlparser 方法)

USER

user

effective id

id

id, setting

user() (poplib.POP3 方法)

user-defined

function

function call

method

user-defined function

对象, [1], [2]

user-defined method

对象

USER_BASE

USER_BASE() (在 site 模块中)

user_call() (bdb.Bdb 方法)

user_exception() (bdb.Bdb 方法)

user_line() (bdb.Bdb 方法)

user_return() (bdb.Bdb 方法)

USER_SITE() (在 site 模块中)

usercustomize

模块

UserDict (collections 中的类)

UserList (collections 中的类)

USERNAME, [1]

username (email.headerregistry.Address 属性)

USERPROFILE, [1]

userptr() (curses.panel.Panel 方法)

UserString (collections 中的类)

UserWarning

USTAR_FORMAT() (在 tarfile 模块中)

UTC

utc (datetime.timezone 属性)

utcfromtimestamp() (datetime.datetime 类方法)

utcnow() (datetime.datetime 类方法)

utcoffset() (datetime.datetime 方法)

(datetime.time 方法)

(datetime.timezone 方法)

(datetime.tzinfo 方法)

utctimetuple() (datetime.datetime 方法)

utf8 (email.policy.EmailPolicy 属性)

utf8() (poplib.POP3 方法)

utf8_enabled (imaplib.IMAP4 属性)

utime() (在 os 模块中)

uu

模块

uu (模块)

UUID (uuid 中的类)

uuid (模块)

uuid1

uuid1() (在 uuid 模块中)

uuid3

uuid3() (在 uuid 模块中)

uuid4

uuid4() (在 uuid 模块中)

uuid5

uuid5() (在 uuid 模块中)

UuidCreate() (在 msilib 模块中)

V

v4_int_to_packed() (在 ipaddress 模块中)

v6_int_to_packed() (在 ipaddress 模块中)

validator() (在 wsgiref.validate 模块中)

value

default parameter

truth

value (ctypes._SimpleCData 属性)

(http.cookiejar.Cookie 属性)

(http.cookies.Morsel 属性)

(xml.dom.Attr 属性)

value of an object

Value() (multiprocessing.managers.SyncManager 方法)

(在 multiprocessing 模块中)

(在 multiprocessing.sharedctypes 模块中)

value_decode() (http.cookies.BaseCookie 方法)

value_encode() (http.cookies.BaseCookie 方法)

ValueError

例外

valuerefs() (weakref.WeakValueDictionary 方法)

values

Boolean

writing

values() (dict 方法)

(email.message.EmailMessage 方法)

(email.message.Message 方法)

(mailbox.Mailbox 方法)

(types.MappingProxyType 方法)

ValuesView (collections.abc 中的类)

(typing 中的类)

variable

free

variable annotation -- 变量标注

variance() (在 statistics 模块中)

variant (uuid.UUID 属性)

vars() (內置函数)

vbar (tkinter.scrolledtext.ScrolledText 属性)

VBAR() (在 token 模块中)

VBAREQUAL() (在 token 模块中)

Vec2D (turtle 中的类)

venv (模块)

VERBOSE() (在 re 模块中)

verbose() (在 tabnanny 模块中)

(在 test.support 模块中)

verify() (smtplib.SMTP 方法)

verify_client_post_handshake() (ssl.SSLSocket 方法)

VERIFY_CRL_CHECK_CHAIN() (在 ssl 模块中)

VERIFY_CRL_CHECK_LEAF() (在 ssl 模块中)

VERIFY_DEFAULT() (在 ssl 模块中)

verify_flags (ssl.SSLContext 属性)

verify_mode (ssl.SSLContext 属性)

verify_request() (socketserver.BaseServer 方法)

VERIFY_X509_STRICT() (在 ssl 模块中)

VERIFY_X509_TRUSTED_FIRST() (在 ssl 模块中)

VerifyFlags (ssl 中的类)

VerifyMode (ssl 中的类)

version (email.headerregistry.MIMEVersionHeader 属性)

(http.client.HTTPResponse 属性)

(http.cookiejar.Cookie 属性)

(in module sys), [1], [2]

(ipaddress.IPv4Address 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Address 属性)

(ipaddress.IPv6Network 属性)

(urllib.request.URLopener 属性)

(uuid.UUID 属性)

version() (ssl.SSLSocket 方法)

(在 curses 模块中)

(在 ensurepip 模块中)

(在 marshal 模块中)

(在 platform 模块中)

(在 sqlite3 模块中)

(在 sys 模块中)

version_info() (在 sqlite3 模块中)

(在 sys 模块中)

version_string() (http.server.BaseHTTPRequestHandler 方法)

vformat() (string.Formatter 方法)

virtual

Environments

virtual environment -- 虚拟环境

virtual machine -- 虚拟机

visit() (ast.NodeVisitor 方法)

visitproc (C 类型)

vline() (curses.window 方法)

voidcmd() (ftplib.FTP 方法)

volume (zipfile.ZipInfo 属性)

vonmisesvariate() (在 random 模块中)

W

W_OK() (在 os 模块中)

wait() (asyncio.asyncio.subprocess.Process 方法)

(asyncio.Condition 方法)

(asyncio.Event 方法)

(multiprocessing.pool.AsyncResult 方法)

(subprocess.Popen 方法)

(threading.Barrier 方法)

(threading.Condition 方法)

(threading.Event 方法)

(在 asyncio 模块中)

(在 concurrent.futures 模块中)

(在 multiprocessing.connection 模块中)

(在 os 模块中)

wait3() (在 os 模块中)

wait4() (在 os 模块中)

wait_closed() (asyncio.Server 方法)

wait_for() (asyncio.Condition 方法)

(threading.Condition 方法)

(在 asyncio 模块中)

waitid() (在 os 模块中)

waitpid() (在 os 模块中)

walk() (email.message.EmailMessage 方法)

(email.message.Message 方法)

(在 ast 模块中)

(在 os 模块中)

walk_packages() (在 pkgutil 模块中)

walk_stack() (在 traceback 模块中)

walk_tb() (在 traceback 模块中)

want (doctest.Example 属性)

warn() (distutils.ccompiler.CCompiler 方法)

(distutils.text_file.TextFile 方法)

(在 warnings 模块中)

warn_explicit() (在 warnings 模块中)

Warning, [1]

warning() (logging.Logger 方法)

(xml.sax.handler.ErrorHandler 方法)

(在 logging 模块中)

warnings

(模块)

WarningsRecorder (test.support 中的类)

warnoptions() (在 sys 模块中)

wasSuccessful() (unittest.TestResult 方法)

WatchedFileHandler (logging.handlers 中的类)

wave (模块)

WCONTINUED() (在 os 模块中)

WCOREDUMP() (在 os 模块中)

WeakKeyDictionary (weakref 中的类)

WeakMethod (weakref 中的类)

weakref (模块)

WeakSet (weakref 中的类)

WeakValueDictionary (weakref 中的类)

webbrowser (模块)

weekday() (datetime.date 方法)

(datetime.datetime 方法)

(在 calendar 模块中)

weekheader() (在 calendar 模块中)

weibullvariate() (在 random 模块中)

WEXITED() (在 os 模块中)

WEXITSTATUS() (在 os 模块中)

wfile (http.server.BaseHTTPRequestHandler 属性)

what() (在 imghdr 模块中)

(在 sndhdr 模块中)

whathdr() (在 sndhdr 模块中)

whatis (pdb command)

where (pdb command)

which() (在 shutil 模块中)

whichdb() (在 dbm 模块中)

while

语句, [1], [2], [3]

whitespace (shlex.shlex 属性)

whitespace() (在 string 模块中)

whitespace_split (shlex.shlex 属性)

Widget (tkinter.ttk 中的类)

width (textwrap.TextWrapper 属性)

width() (在 turtle 模块中)

WIFCONTINUED() (在 os 模块中)

WIFEXITED() (在 os 模块中)

WIFSIGNALED() (在 os 模块中)

WIFSTOPPED() (在 os 模块中)

win32_ver() (在 platform 模块中)

WinDLL (ctypes 中的类)

window manager (widgets)

window() (curses.panel.Panel 方法)

window_height() (在 turtle 模块中)

window_width() (在 turtle 模块中)

Windows

Windows ini file

WindowsError

WindowsPath (pathlib 中的类)

WindowsRegistryFinder (importlib.machinery 中的类)

winerror (OSError 属性)

WinError() (在 ctypes 模块中)

WINFUNCTYPE() (在 ctypes 模块中)

winreg (模块)

WinSock

winsound (模块)

winver() (在 sys 模块中)

with

语句, [1]

WITH_CLEANUP_FINISH (opcode)

WITH_CLEANUP_START (opcode)

with_hostmask (ipaddress.IPv4Interface 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Interface 属性)

(ipaddress.IPv6Network 属性)

with_name() (pathlib.PurePath 方法)

with_netmask (ipaddress.IPv4Interface 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Interface 属性)

(ipaddress.IPv6Network 属性)

with_prefixlen (ipaddress.IPv4Interface 属性)

(ipaddress.IPv4Network 属性)

(ipaddress.IPv6Interface 属性)

(ipaddress.IPv6Network 属性)

with_suffix() (pathlib.PurePath 方法)

with_traceback() (BaseException 方法)

WNOHANG() (在 os 模块中)

WNOWAIT() (在 os 模块中)

wordchars (shlex.shlex 属性)

World Wide Web, [1], [2]

wrap() (textwrap.TextWrapper 方法)

(在 textwrap 模块中)

wrap_bio() (ssl.SSLContext 方法)

wrap_future() (在 asyncio 模块中)

wrap_socket() (ssl.SSLContext 方法)

(在 ssl 模块中)

wrap_text() (在 distutils.fancy_getopt 模块中)

wrapper() (在 curses 模块中)

wraps() (在 functools 模块中)

writable() (asyncore.dispatcher 方法)

(io.IOBase 方法)

WRITABLE() (在 tkinter 模块中)

write() (asyncio.StreamWriter 方法)

(asyncio.WriteTransport 方法)

(code.InteractiveInterpreter 方法)

(codecs.StreamWriter 方法)

(configparser.ConfigParser 方法)

(email.generator.BytesGenerator 方法)

(email.generator.Generator 方法)

(io.BufferedIOBase 方法)

(io.BufferedWriter 方法)

(io.RawIOBase 方法)

(io.TextIOBase 方法)

(mmap.mmap 方法)

(ossaudiodev.oss_audio_device 方法)

(ssl.MemoryBIO 方法)

(ssl.SSLSocket 方法)

(telnetlib.Telnet 方法)

(xml.etree.ElementTree.ElementTree 方法)

(zipfile.ZipFile 方法)

(在 os 模块中)

(在 turtle 模块中)

write_byte() (mmap.mmap 方法)

write_bytes() (pathlib.Path 方法)

write_docstringdict() (在 turtle 模块中)

write_eof() (asyncio.StreamWriter 方法)

(asyncio.WriteTransport 方法)

(ssl.MemoryBIO 方法)

write_file() (在 distutils.file_util 模块中)

write_history_file() (在 readline 模块中)

WRITE_RESTRICTED

write_results() (trace.CoverageResults 方法)

write_text() (pathlib.Path 方法)

writeall() (ossaudiodev.oss_audio_device 方法)

writeframes() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

writeframesraw() (aifc.aifc 方法)

(sunau.AU_write 方法)

(wave.Wave_write 方法)

writeheader() (csv.DictWriter 方法)

writelines() (asyncio.StreamWriter 方法)

(asyncio.WriteTransport 方法)

(codecs.StreamWriter 方法)

(io.IOBase 方法)

writePlist() (在 plistlib 模块中)

writePlistToBytes() (在 plistlib 模块中)

writepy() (zipfile.PyZipFile 方法)

writer (formatter.formatter 属性)

writer() (在 csv 模块中)

writerow() (csv.csvwriter 方法)

writerows() (csv.csvwriter 方法)

writestr() (zipfile.ZipFile 方法)

WriteTransport (asyncio 中的类)

writev() (在 os 模块中)

writexml() (xml.dom.minidom.Node 方法)

writing

values

WrongDocumentErr

ws_comma (2to3 fixer)

wsgi_file_wrapper (wsgiref.handlers.BaseHandler 属性)

wsgi_multiprocess (wsgiref.handlers.BaseHandler 属性)

wsgi_multithread (wsgiref.handlers.BaseHandler 属性)

wsgi_run_once (wsgiref.handlers.BaseHandler 属性)

wsgiref (模块)

wsgiref.handlers (模块)

wsgiref.headers (模块)

wsgiref.simple_server (模块)

wsgiref.util (模块)

wsgiref.validate (模块)

WSGIRequestHandler (wsgiref.simple_server 中的类)

WSGIServer (wsgiref.simple_server 中的类)

wShowWindow (subprocess.STARTUPINFO 属性)

WSTOPPED() (在 os 模块中)

WSTOPSIG() (在 os 模块中)

wstring_at() (在 ctypes 模块中)

WTERMSIG() (在 os 模块中)

WUNTRACED() (在 os 模块中)

WWW, [1], [2]

server, [1]

X

X() (在 re 模块中)

X509 certificate

X_OK() (在 os 模块中)

xatom() (imaplib.IMAP4 方法)

XATTR_CREATE() (在 os 模块中)

XATTR_REPLACE() (在 os 模块中)

XATTR_SIZE_MAX() (在 os 模块中)

xcor() (在 turtle 模块中)

XDR

xdrlib (模块)

xhdr() (nntplib.NNTP 方法)

XHTML

XHTML_NAMESPACE() (在 xml.dom 模块中)

xml (模块)

XML() (在 xml.etree.ElementTree 模块中)

xml.dom (模块)

xml.dom.minidom (模块)

xml.dom.pulldom (模块)

xml.etree.ElementTree (模块)

xml.parsers.expat (模块)

xml.parsers.expat.errors (模块)

xml.parsers.expat.model (模块)

xml.sax (模块)

xml.sax.handler (模块)

xml.sax.saxutils (模块)

xml.sax.xmlreader (模块)

XML_ERROR_ABORTED() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_ASYNC_ENTITY() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_BAD_CHAR_REF() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_BINARY_ENTITY_REF() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_DUPLICATE_ATTRIBUTE() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_ENTITY_DECLARED_IN_PE() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_EXTERNAL_ENTITY_HANDLING() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_FEATURE_REQUIRES_XML_DTD() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_FINISHED() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_INCOMPLETE_PE() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_INCORRECT_ENCODING() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_INVALID_TOKEN() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_JUNK_AFTER_DOC_ELEMENT() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_MISPLACED_XML_PI() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_NO_ELEMENTS() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_NO_MEMORY() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_NOT_STANDALONE() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_NOT_SUSPENDED() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_PARAM_ENTITY_REF() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_PARTIAL_CHAR() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_PUBLICID() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_RECURSIVE_ENTITY_REF() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_SUSPEND_PE() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_SUSPENDED() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_SYNTAX() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_TAG_MISMATCH() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_TEXT_DECL() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_UNBOUND_PREFIX() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_UNCLOSED_CDATA_SECTION() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_UNCLOSED_TOKEN() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_UNDECLARING_PREFIX() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_UNDEFINED_ENTITY() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_UNEXPECTED_STATE() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_UNKNOWN_ENCODING() (在 xml.parsers.expat.errors 模块中)

XML_ERROR_XML_DECL() (在 xml.parsers.expat.errors 模块中)

XML_NAMESPACE() (在 xml.dom 模块中)

xmlcharrefreplace_errors() (在 codecs 模块中)

XmlDeclHandler() (xml.parsers.expat.xmlparser 方法)

XMLFilterBase (xml.sax.saxutils 中的类)

XMLGenerator (xml.sax.saxutils 中的类)

XMLID() (在 xml.etree.ElementTree 模块中)

XMLNS_NAMESPACE() (在 xml.dom 模块中)

XMLParser (xml.etree.ElementTree 中的类)

XMLParserType() (在 xml.parsers.expat 模块中)

XMLPullParser (xml.etree.ElementTree 中的类)

XMLReader (xml.sax.xmlreader 中的类)

xmlrpc.client (模块)

xmlrpc.server (模块)

xor

bitwise

xor() (在 operator 模块中)

xover() (nntplib.NNTP 方法)

xpath() (nntplib.NNTP 方法)

xrange (2to3 fixer)

xreadlines (2to3 fixer)

xview() (tkinter.ttk.Treeview 方法)

Y

Y2K

ycor() (在 turtle 模块中)

year (datetime.date 属性)

(datetime.datetime 属性)

Year 2000

Year 2038

yeardatescalendar() (calendar.Calendar 方法)

yeardays2calendar() (calendar.Calendar 方法)

yeardayscalendar() (calendar.Calendar 方法)

YESEXPR() (在 locale 模块中)

yield

examples

expression

yield from (in What's New)

关键字

语句

YIELD_FROM (opcode)

YIELD_VALUE (opcode)

yiq_to_rgb() (在 colorsys 模块中)

yview() (tkinter.ttk.Treeview 方法)

Z

Zen of Python -- Python 之禅

ZeroDivisionError

例外

zfill() (bytearray 方法)

(bytes 方法)

(str 方法)

zip (2to3 fixer)

zip() (內置函数)

ZIP_BZIP2() (在 zipfile 模块中)

ZIP_DEFLATED() (在 zipfile 模块中)

zip_longest() (在 itertools 模块中)

ZIP_LZMA() (在 zipfile 模块中)

ZIP_STORED() (在 zipfile 模块中)

zipapp (模块)

zipapp command line option

--help

--info

--main=<mainfn>

--output=<output>

--python=<interpreter>

-h

-m <mainfn>

-o <output>

-p <interpreter>

ZipFile (zipfile 中的类)

zipfile (模块)

zipfile command line option

-c <zipfile> <source1> ... <sourceN>

-e <zipfile> <output_dir>

-l <zipfile>

-t <zipfile>

zipimport (模块)

zipimporter (zipimport 中的类)

ZipImportError

ZipInfo (zipfile 中的类)

zlib (模块)

ZLIB_RUNTIME_VERSION() (在 zlib 模块中)

ZLIB_VERSION() (在 zlib 模块中)

例

例外

AssertionError

AttributeError

GeneratorExit, [1]

ImportError

NameError

StopAsyncIteration

StopIteration, [1]

TypeError

ValueError

ZeroDivisionError

內

內置函数

__import__

abs, [1]

ascii

bytes, [1]

chr

classmethod

compile, [1], [2], [3], [4]

complex, [1]

divmod, [1], [2]

eval, [1], [2], [3], [4], [5]

exec, [1], [2], [3]

float, [1], [2]

hash, [1], [2], [3]

help

id

int, [1], [2]

len, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11]

max

min

open, [1]

ord

pow, [1], [2], [3], [4], [5]

print

range

repr, [1], [2], [3]

round

slice, [1]

staticmethod

tuple, [1]

type, [1], [2], [3]

关

关键字

as, [1], [2]

async

await, [1], [2]

elif

else, [1], [2], [3], [4], [5], [6], [7]

except

finally, [1], [2], [3], [4]

from, [1]

in, [1]

yield

对

对象

asynchronous-generator

Boolean, [1]

built-in function, [1]

built-in method, [1]

bytearray, [1], [2], [3]

bytes, [1], [2]

callable, [1]

Capsule

class, [1], [2]

class instance, [1], [2]

complex

complex number, [1]

dictionary, [1], [2], [3], [4], [5], [6], [7]

Ellipsis

file, [1]

floating point, [1], [2]

frame

frozenset, [1]

function, [1], [2], [3], [4], [5]

generator, [1], [2]

immutable

immutable sequence

instance, [1], [2]

instancemethod

integer, [1], [2]

io.StringIO

list, [1], [2], [3], [4], [5], [6], [7], [8]

long integer

mapping, [1], [2], [3], [4], [5]

memoryview, [1]

method, [1], [2], [3], [4], [5]

module, [1], [2]

mutable, [1], [2]

mutable sequence

None, [1], [2]

NotImplemented

numeric, [1], [2], [3]

range

sequence, [1], [2], [3], [4], [5], [6], [7], [8]

set, [1], [2], [3]

set type

slice

socket

string, [1], [2]

traceback, [1], [2], [3], [4]

tuple, [1], [2], [3], [4], [5], [6]

type, [1], [2]

user-defined function, [1], [2]

user-defined method

模

模块

__main__, [1], [2], [3], [4], [5], [6], [7]

_locale

_thread

array, [1]

base64

bdb

binhex

builtins, [1], [2], [3], [4]

cmd

copy

crypt

dbm.gnu, [1]

dbm.ndbm, [1]

errno

glob

imp

io

json

math, [1]

os

pickle, [1], [2], [3]

pty

pwd

pyexpat

re, [1]

shelve

signal, [1]

sitecustomize

socket

stat

string

struct

sys, [1], [2], [3], [4], [5], [6]

types

urllib.request

usercustomize

uu

环

环境变量

APPDATA

AUDIODEV

BROWSER, [1]

CC

CFLAGS, [1], [2]

COLS, [1]

COLUMNS, [1]

COMSPEC, [1]

CPP

CPPFLAGS

DISTUTILS_DEBUG

exec_prefix, [1], [2]

HOME, [1], [2], [3], [4]

HOMEDRIVE, [1]

HOMEPATH, [1]

http_proxy, [1], [2]

IDLESTARTUP, [1]

KDEDIR

LANG, [1], [2], [3], [4]

LANGUAGE, [1]

LC_ALL, [1]

LC_MESSAGES, [1]

LDCXXSHARED

LDFLAGS

LINES, [1], [2], [3], [4]

LNAME

LOGNAME, [1]

MIXERDEV

no_proxy

PAGER

PATH, [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], [26], [27], [28], [29], [30], [31], [32], [33], [34]

PATHEXT, [1]

PLAT

POSIXLY_CORRECT

prefix, [1], [2], [3]

PYTHON*, [1], [2]

PYTHON_DOM

PYTHONASYNCIODEBUG, [1], [2]

PYTHONCASEOK, [1]

PYTHONDEBUG, [1]

PYTHONDOCS

PYTHONDONTWRITEBYTECODE, [1], [2], [3], [4], [5]

PYTHONDUMPREFS, [1]

PYTHONEXECUTABLE

PYTHONFAULTHANDLER, [1], [2]

PYTHONHASHSEED, [1], [2], [3], [4], [5]

PYTHONHOME, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14]

PYTHONINSPECT, [1], [2]

PYTHONIOENCODING, [1], [2], [3], [4], [5]

PYTHONLEGACYWINDOWSFSENCODING, [1], [2]

PYTHONLEGACYWINDOWSSTDIO, [1], [2]

PYTHONMALLOC, [1], [2], [3], [4], [5], [6]

PYTHONMALLOCSTATS, [1], [2]

PYTHONNOUSERSITE, [1], [2], [3]

PYTHONOPTIMIZE, [1]

PYTHONPATH, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22]

PYTHONSHOWALLOCCOUNT

PYTHONSHOWREFCOUNT

PYTHONSTARTUP, [1], [2], [3], [4], [5], [6], [7], [8], [9]

PYTHONTHREADDEBUG

PYTHONTRACEMALLOC, [1], [2], [3]

PYTHONUNBUFFERED, [1]

PYTHONUSERBASE, [1], [2], [3]

PYTHONVERBOSE, [1]

PYTHONWARNINGS, [1], [2], [3], [4]

SSL_CERT_FILE

SSL_CERT_PATH

SystemRoot

TCL_LIBRARY

TEMP

TERM, [1]

TIX_LIBRARY

TK_LIBRARY

TMP

TMPDIR

TZ, [1], [2], [3], [4]

USER

USER_BASE

USERNAME, [1]

USERPROFILE, [1]

语

语句

assert, [1]

async def

async for

async with

break, [1], [2], [3], [4]

class

continue, [1], [2], [3], [4]

def

del, [1], [2], [3]

except

for, [1], [2], [3]

global, [1]

if, [1]

import, [1], [2], [3], [4]

nonlocal

pass

raise, [1]

return, [1], [2]

try, [1], [2]

while, [1], [2], [3]

with, [1]

yield

运

运算符

!=, [1]

% (percent), [1]

& (ampersand), [1]

* (asterisk), [1]

**, [1]

/ (slash), [1]

//, [1]

< (less), [1]

<<, [1]

<=, [1]

==, [1]

> (greater), [1]

>=, [1]

>>, [1]

@ (at)

^ (caret), [1]

| (vertical bar), [1]

~ (tilde), [1]

and, [1], [2]

in, [1], [2]

is, [1]

is not, [1]

not, [1]

not in, [1], [2]

or, [1], [2]

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – 例

例外

AssertionError

AttributeError

GeneratorExit, [1]

ImportError

NameError

StopAsyncIteration

StopIteration, [1]

TypeError

ValueError

ZeroDivisionError

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – 內

內置函数

__import__

abs, [1]

ascii

bytes, [1]

chr

classmethod

compile, [1], [2], [3], [4]

complex, [1]

divmod, [1], [2]

eval, [1], [2], [3], [4], [5]

exec, [1], [2], [3]

float, [1], [2]

hash, [1], [2], [3]

help

id

int, [1], [2]

len, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11]

max

min

open, [1]

ord

pow, [1], [2], [3], [4], [5]

print

range

repr, [1], [2], [3]

round

slice, [1]

staticmethod

tuple, [1]

type, [1], [2], [3]

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – 关

关键字

as, [1], [2]

async

await, [1], [2]

elif

else, [1], [2], [3], [4], [5], [6], [7]

except

finally, [1], [2], [3], [4]

from, [1]

in, [1]

yield

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – 对

对象

asynchronous-generator

Boolean, [1]

built-in function, [1]

built-in method, [1]

bytearray, [1], [2], [3]

bytes, [1], [2]

callable, [1]

Capsule

class, [1], [2]

class instance, [1], [2]

complex

complex number, [1]

dictionary, [1], [2], [3], [4], [5], [6], [7]

Ellipsis

file, [1]

floating point, [1], [2]

frame

frozenset, [1]

function, [1], [2], [3], [4], [5]

generator, [1], [2]

immutable

immutable sequence

instance, [1], [2]

instancemethod

integer, [1], [2]

io.StringIO

list, [1], [2], [3], [4], [5], [6], [7], [8]

long integer

mapping, [1], [2], [3], [4], [5]

memoryview, [1]

method, [1], [2], [3], [4], [5]

module, [1], [2]

mutable, [1], [2]

mutable sequence

None, [1], [2]

NotImplemented

numeric, [1], [2], [3]

range

sequence, [1], [2], [3], [4], [5], [6], [7], [8]

set, [1], [2], [3]

set type

slice

socket

string, [1], [2]

traceback, [1], [2], [3], [4]

tuple, [1], [2], [3], [4], [5], [6]

type, [1], [2]

user-defined function, [1], [2]

user-defined method

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – 模

模块

__main__, [1], [2], [3], [4], [5], [6], [7]

_locale

_thread

array, [1]

base64

bdb

binhex

builtins, [1], [2], [3], [4]

cmd

copy

crypt

dbm.gnu, [1]

dbm.ndbm, [1]

errno

glob

imp

io

json

math, [1]

os

pickle, [1], [2], [3]

pty

pwd

pyexpat

re, [1]

shelve

signal, [1]

sitecustomize

socket

stat

string

struct

sys, [1], [2], [3], [4], [5], [6]

types

urllib.request

usercustomize

uu

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – 环

环境变量

APPDATA

AUDIODEV

BROWSER, [1]

CC

CFLAGS, [1], [2]

COLS, [1]

COLUMNS, [1]

COMSPEC, [1]

CPP

CPPFLAGS

DISTUTILS_DEBUG

exec_prefix, [1], [2]

HOME, [1], [2], [3], [4]

HOMEDRIVE, [1]

HOMEPATH, [1]

http_proxy, [1], [2]

IDLESTARTUP, [1]

KDEDIR

LANG, [1], [2], [3], [4]

LANGUAGE, [1]

LC_ALL, [1]

LC_MESSAGES, [1]

LDCXXSHARED

LDFLAGS

LINES, [1], [2], [3], [4]

LNAME

LOGNAME, [1]

MIXERDEV

no_proxy

PAGER

PATH, [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], [26], [27], [28], [29], [30], [31], [32], [33], [34]

PATHEXT, [1]

PLAT

POSIXLY_CORRECT

prefix, [1], [2], [3]

PYTHON*, [1], [2]

PYTHON_DOM

PYTHONASYNCIODEBUG, [1], [2]

PYTHONCASEOK, [1]

PYTHONDEBUG, [1]

PYTHONDOCS

PYTHONDONTWRITEBYTECODE, [1], [2], [3], [4], [5]

PYTHONDUMPREFS, [1]

PYTHONEXECUTABLE

PYTHONFAULTHANDLER, [1], [2]

PYTHONHASHSEED, [1], [2], [3], [4], [5]

PYTHONHOME, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14]

PYTHONINSPECT, [1], [2]

PYTHONIOENCODING, [1], [2], [3], [4], [5]

PYTHONLEGACYWINDOWSFSENCODING, [1], [2]

PYTHONLEGACYWINDOWSSTDIO, [1], [2]

PYTHONMALLOC, [1], [2], [3], [4], [5], [6]

PYTHONMALLOCSTATS, [1], [2]

PYTHONNOUSERSITE, [1], [2], [3]

PYTHONOPTIMIZE, [1]

PYTHONPATH, [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22]

PYTHONSHOWALLOCCOUNT

PYTHONSHOWREFCOUNT

PYTHONSTARTUP, [1], [2], [3], [4], [5], [6], [7], [8], [9]

PYTHONTHREADDEBUG

PYTHONTRACEMALLOC, [1], [2], [3]

PYTHONUNBUFFERED, [1]

PYTHONUSERBASE, [1], [2], [3]

PYTHONVERBOSE, [1]

PYTHONWARNINGS, [1], [2], [3], [4]

SSL_CERT_FILE

SSL_CERT_PATH

SystemRoot

TCL_LIBRARY

TEMP

TERM, [1]

TIX_LIBRARY

TK_LIBRARY

TMP

TMPDIR

TZ, [1], [2], [3], [4]

USER

USER_BASE

USERNAME, [1]

USERPROFILE, [1]

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – 符号

! (exclamation)

in a command interpreter

in curses module

in formatted string literal

in glob-style wildcards, [1]

in string formatting

in struct format strings

! (pdb command)

!=

运算符, [1]

" (double quote)

string literal

"""

string literal

# (hash)

comment, [1], [2]

in doctests

in printf-style formatting, [1]

in regular expressions

in string formatting

source encoding declaration

$ (dollar)

environment variables expansion

in regular expressions

in template strings

interpolation in configuration files

% (percent)

datetime format, [1], [2]

environment variables expansion (Windows), [1]

interpolation in configuration files

printf-style formatting, [1]

运算符, [1]

%=

augmented assignment

& (ampersand)

运算符, [1]

&=

augmented assignment

' (single quote)

string literal

'''

string literal

() (parentheses)

call

class definition

function definition

generator expression

in assignment target list

in printf-style formatting, [1]

in regular expressions

tuple display

(?

in regular expressions

(?!

in regular expressions

(?#

in regular expressions

(?:

in regular expressions

(?<!

in regular expressions

(?<=

in regular expressions

(?P<

in regular expressions

(?P=

in regular expressions

* (asterisk)

function definition

import statement

in argparse module

in assignment target list

in AST grammar

in expression lists

in function calls, [1]

in glob-style wildcards, [1]

in printf-style formatting, [1]

in regular expressions

运算符, [1]

**

function definition

in dictionary displays

in function calls, [1]

in glob-style wildcards

运算符, [1]

**=

augmented assignment

*=

augmented assignment

*?

in regular expressions

+ (plus)

binary operator, [1]

in argparse module

in doctests

in printf-style formatting, [1]

in regular expressions

in string formatting

unary operator, [1]

+=

augmented assignment

+?

in regular expressions

, (comma)

argument list

expression list, [1], [2], [3], [4]

identifier list, [1]

import statement

in dictionary displays

in string formatting

in target list

parameter list

slicing

tuple display

with statement

- (minus)

binary operator, [1]

in doctests

in glob-style wildcards, [1]

in printf-style formatting, [1]

in regular expressions

in string formatting

unary operator, [1]

--annotate

pickletools command line option

--buffer

unittest command line option

--catch

unittest command line option

--clock

timeit command line option

--count

trace command line option

--coverdir=<dir>

trace command line option

--create <tarfile> <source1> ... <sourceN>

tarfile command line option

--details

inspect command line option

--exact

tokenize command line option

--extract <tarfile> [<output_dir>]

tarfile command line option

--failfast

unittest command line option

--file=<file>

trace command line option

--help

command line option

json.tool command line option

timeit command line option

tokenize command line option

trace command line option

zipapp command line option

--ignore-dir=<dir>

trace command line option

--ignore-module=<mod>

trace command line option

--indentlevel=<num>

pickletools command line option

--info

zipapp command line option

--list <tarfile>

tarfile command line option

--listfuncs

trace command line option

--locals

unittest command line option

--main=<mainfn>

zipapp command line option

--memo

pickletools command line option

--missing

trace command line option

--no-report

trace command line option

--number=N

timeit command line option

--output=<file>

pickletools command line option

--output=<output>

zipapp command line option

--pattern pattern

unittest-discover command line option

--preamble=<preamble>

pickletools command line option

--process

timeit command line option

--python=<interpreter>

zipapp command line option

--repeat=N

timeit command line option

--report

trace command line option

--setup=S

timeit command line option

--sort-keys

json.tool command line option

--start-directory directory

unittest-discover command line option

--summary

trace command line option

--test <tarfile>

tarfile command line option

--time

timeit command line option

--timing

trace command line option

--top-level-directory directory

unittest-discover command line option

--trace

trace command line option

--trackcalls

trace command line option

--unit=U

timeit command line option

--user-base

site command line option

--user-site

site command line option

--verbose

tarfile command line option

timeit command line option

unittest-discover command line option

--version

command line option

trace command line option

-=

augmented assignment

->

function annotations, [1]

-?

command line option

-a

pickletools command line option

-B

command line option

-b

command line option

compileall command line option

unittest command line option

-C

trace command line option

-c

timeit command line option

trace command line option

unittest command line option

-c <command>

command line option

-c <tarfile> <source1> ... <sourceN>

tarfile command line option

-c <zipfile> <source1> ... <sourceN>

zipfile command line option

-d

command line option

-d destdir

compileall command line option

-E

command line option

-e

tokenize command line option

-e <tarfile> [<output_dir>]

tarfile command line option

-e <zipfile> <output_dir>

zipfile command line option

-f

compileall command line option

trace command line option

unittest command line option

-g

trace command line option

-h

command line option

json.tool command line option

timeit command line option

tokenize command line option

zipapp command line option

-I

command line option

-i

command line option

-i list

compileall command line option

-J

command line option

-j N

compileall command line option

-l

compileall command line option

pickletools command line option

trace command line option

-l <tarfile>

tarfile command line option

-l <zipfile>

zipfile command line option

-m

pickletools command line option

trace command line option

-m <mainfn>

zipapp command line option

-m <module-name>

command line option

-n N

timeit command line option

-O

command line option

-o

pickletools command line option

-o <output>

zipapp command line option

-OO

command line option

-p

pickletools command line option

timeit command line option

unittest-discover command line option

-p <interpreter>

zipapp command line option

-q

command line option

compileall command line option

-R

command line option

trace command line option

-r

compileall command line option

trace command line option

-r N

timeit command line option

-S

command line option

-s

command line option

trace command line option

unittest-discover command line option

-s S

timeit command line option

-T

trace command line option

-t

timeit command line option

trace command line option

unittest-discover command line option

-t <tarfile>

tarfile command line option

-t <zipfile>

zipfile command line option

-u

command line option

timeit command line option

-V

command line option

-v

command line option

tarfile command line option

timeit command line option

unittest-discover command line option

-W arg

command line option

-X

command line option

-x

command line option

-x regex

compileall command line option

. (dot)

attribute reference

in glob-style wildcards

in numeric literal

in pathnames, [1]

in printf-style formatting, [1]

in regular expressions

in string formatting

in Tkinter

..

in pathnames

...

ellipsis literal, [1], [2]

in doctests

interpreter prompt, [1]

placeholder, [1], [2]

.ini

file

.pdbrc

file

.pypirc file

/ (slash)

in pathnames, [1]

运算符, [1]

//

运算符, [1]

//=

augmented assignment

/=

augmented assignment

0b

integer literal

0o

integer literal

0x

integer literal

2to3

: (colon)

annotated variable

compound statement, [1], [2], [3], [4], [5], [6]

function annotations, [1]

in dictionary expressions

in formatted string literal

in SQL statements

in string formatting

lambda expression

path separator (POSIX)

slicing

; (semicolon), [1]

< (less)

in string formatting

in struct format strings

运算符, [1]

<<

运算符, [1]

<<=

augmented assignment

<=

运算符, [1]

<BLANKLINE>

= (equals)

assignment statement

class definition

function definition

in function calls

in string formatting

in struct format strings

==

运算符, [1]

> (greater)

in string formatting

in struct format strings

运算符, [1]

>=

运算符, [1]

>>

运算符, [1]

>>=

augmented assignment

>>>

interpreter prompt, [1]

? (question mark)

in a command interpreter

in argparse module

in AST grammar

in glob-style wildcards, [1]

in regular expressions

in SQL statements

in struct format strings, [1]

replacement character

??

in regular expressions

@ (at)

class definition

function definition

in struct format strings

运算符

[] (square brackets)

in assignment target list

in glob-style wildcards, [1]

in regular expressions

in string formatting

list expression

subscription

\ (backslash)

escape sequence, [1]

in pathnames (Windows)

in regular expressions, [1], [2]

\\

escape sequence

in regular expressions

\A

in regular expressions

\a

escape sequence

in regular expressions

\B

in regular expressions

\b

escape sequence

in regular expressions, [1]

\D

in regular expressions

\d

in regular expressions

\f

escape sequence

in regular expressions

\g

in regular expressions

\N

escape sequence, [1]

in regular expressions

\n

escape sequence

in regular expressions

\r

escape sequence

in regular expressions

\S

in regular expressions

\s

in regular expressions

\t

escape sequence

in regular expressions

\U

escape sequence, [1]

in regular expressions

\u

escape sequence, [1]

in regular expressions

\v

escape sequence

in regular expressions

\W

in regular expressions

\w

in regular expressions

\x

escape sequence, [1]

in regular expressions

\Z

in regular expressions

^ (caret)

in curses module

in regular expressions, [1]

in string formatting

marker, [1]

运算符, [1]

^=

augmented assignment

{} (curly brackets)

dictionary expression

in formatted string literal

in regular expressions

in string formatting

set expression

| (vertical bar)

in regular expressions

运算符, [1]

|=

augmented assignment

~ (tilde)

home directory expansion

运算符, [1]

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – 语

语句

assert, [1]

async def

async for

async with

break, [1], [2], [3], [4]

class

continue, [1], [2], [3], [4]

def

del, [1], [2], [3]

except

for, [1], [2], [3]

global, [1]

if, [1]

import, [1], [2], [3], [4]

nonlocal

pass

raise, [1]

return, [1], [2]

try, [1], [2]

while, [1], [2], [3]

with, [1]

yield

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

索引 – 运

运算符

!=, [1]

% (percent), [1]

& (ampersand), [1]

* (asterisk), [1]

**, [1]

/ (slash), [1]

//, [1]

< (less), [1]

<<, [1]

<=, [1]

==, [1]

> (greater), [1]

>=, [1]

>>, [1]

@ (at)

^ (caret), [1]

| (vertical bar), [1]

~ (tilde), [1]

and, [1], [2]

in, [1], [2]

is, [1]

is not, [1]

not, [1]

not in, [1], [2]

or, [1], [2]

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

Python 3.6.13 文档

欢迎！这里是 Python 3.6.13 的中文文档。

按章节浏览文档：

Python 3.6 有什么新变化？
或显示自 2.0 以来的全部新变化

教程
从这里看起

标准库参考
放在枕边作为参考

语言参考
讲解基础内容和基本语法

Python安装和使用
各种操作系统的介绍都有

Python 常用指引
针对特定主题的深入文档

安装 Python 模块
从官方的 PyPI 或者其他来源安装模块

分发 Python 模块
发布模块，供其他人安装

扩展和嵌入
给 C/C++ 程序员的示例教程

Python/C API 接口
给 C/C++ 程序员的参考手册

常见问题
经常被问到的问题（答案也有！）

索引和表格：

全局模块索引
快速查看所有的模块

总目录
所的函数，类，术语

术语对照表
解释最重要的术语

搜索页面
搜索文档

完整的内容表
列出所有的章节和部分

元信息：

报告 Bug

关于此文档

Python 的历史和授权

版权所有

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

上传包到包索引

The contents of this page have moved to the section Python 包索引（PyPI）.

导航

索引

模块 |

Python »

3.6.13 Documentation »

|

© 版权所有 2001-2021, Python Software Foundation.
Python 软件基金会是一个非盈利组织。请捐助。
最后更新于 2月 26, 2021. 发现了问题？
使用Sphinx2.3.1 创建。