内容表

  • copyreg — 注册 pickle 支持函数
    • 范例

上一话题

pickle — Python 对象序列化

下一话题
就业培训     下载中心     Wiki     联络
登录   注册

Log
  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. 数据持久性
  9. copyreg — 注册 pickle 支持函数

copyreg — 注册 pickle 支持函数 ¶

源代码: Lib/copyreg.py


The copyreg 模块提供腌制特定对象时,定义使用函数的方式。 pickle and copy 模块使用这些函数,当腌制/拷贝这些对象时。模块提供有关对象构造函数 (不是类) 的配置信息。这种构造函数可以是工厂函数 (或类实例)。

copyreg. constructor ( object ) ¶

声明 object 成为有效构造函数。若 object 不可调用 (因此作为构造函数无效),引发 TypeError .

copyreg. pickle ( type , function , constructor_ob = None ) ¶

声明 function should be used as a “reduction” function for objects of type type . function must return either a string or a tuple containing between two and six elements. See the dispatch_table for more details on the interface of function .

The constructor_ob parameter is a legacy feature and is now ignored, but if passed it must be a callable.

注意, dispatch_table attribute of a pickler object or subclass of pickle.Pickler can also be used for declaring reduction functions.

范例 ¶

以下范例愿意展示如何注册 pickle 函数及如何使用它:

>>> import copyreg, copy, pickle
>>> class C:
...     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...
											

内容表

  • copyreg — 注册 pickle 支持函数
    • 范例

上一话题

pickle — Python 对象序列化

下一话题

shelve — Python 对象持久性

本页

  • 报告 Bug
  • 展示源

快速搜索

键入搜索术语或模块、类、函数名称。

  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. 数据持久性
  9. copyreg — 注册 pickle 支持函数
  10. 版权所有  © 2014-2026 乐数软件    

    工业和信息化部: 粤ICP备14079481号-1