protocolbuffers/protobuf

Python: Use multi-phase initialisation (PEP 489)

开放

#21,995 创建于 2025年5月28日

 (3 条评论) (0 个反应) (1 位负责人)C++ (16,128 个派生)batch import
help wantedkeep openpython

仓库指标

星标
 (71,223 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

What language does this apply to?

Python

Describe the solution you'd like

Hello,

Please consider adopting multi-phase init for your Python extension modules. In short, multi-phase makes extension modules behave more like pure-Python modules and means that the import system can determine what a module supports before executing it.

Multi-phase initialisation was introduced in PEP 489 for Python 3.5. It replaces the previous 'single-phase' mechanism by splitting the module creation process into creation and execution phases. Quoting from the documentation:

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.

It is the preferred mechanism for extension module initialisation, and we are considering explicitly deprecating single phase init.

Describe alternatives you've considered

The status quo.

A

贡献者指南