Python 3如何知道如何腌制扩展类型,尤其是Numpy数组? [英] How does Python 3 know how to pickle extension types, especially Numpy arrays?

查看:96
本文介绍了Python 3如何知道如何腌制扩展类型,尤其是Numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为扩展类型的Numpy数组(也称为C API的扩展名),声明了Python解释器范围之外的其他字段(例如,data属性(即Buffer Structure),如Numpy的文档所述) 数组接口.
为了能够对其进行序列化,Python 2曾经将__reduce__函数用作pickle协议的一部分,如此处.

Numpy arrays, being extension types (aka defined using in extensions the C API), declare additional fields outside the scope of the Python interpreter (for example the data attribute, which is a Buffer Structure, as documented in Numpy's array interface.
To be able to serialize it, Python 2 used to use the __reduce__ function as part of the pickle protocol, as stated in the doc, and explained here.

但是,即使Python 3中仍然存在__reduce__,也会从文档中删除Pickle protocol部分(以及Pickling and unpickling extension types一个替身),因此目前尚不清楚该做什么. 此外,还有其他与酸洗扩展类型有关的条目:

But, even if __reduce__ still exists in Python 3, the Pickle protocol section (and Pickling and unpickling extension types a fortiori) was removed from the doc, so it is unclear what does what.
Moreover, there are additional entries that relate to pickling extension types:

  • copyreg ,描述为Pickle interface constructor registration for extension types,但是在copyreg模块本身中没有提及扩展类型.
  • PEP 3118-修改了缓冲协议,它发布了新的适用于Python 3的缓冲协议.(也许可以自动执行该缓冲协议的酸洗).
  • 新型类:人们可以假设新型类对酸洗过程有影响.
  • copyreg, described as a Pickle interface constructor registration for extension types, but there's no mention of extension types in the copyreg module itself.
  • PEP 3118 -- Revising the buffer protocol which released a new buffer protocol for Python 3. (and maybe automates pickling for this buffer protocol).
  • New-style class: One can assume that the new-style classes have an influence on the pickling process.

所以,所有这些与Numpy数组有何关系:

So, how does all of this relate to Numpy arrays:

  1. Numpy数组是否实现了特殊方法,例如__reduce__来通知Python如何腌制它们(或copyreg)?多个对象仍然公开__reduce__方法,但这可能是出于兼容性原因.
  2. Numpy是否使用Pickle提供的开箱即用支持的Python的C-API结构(例如新的buffer protocol),所以要腌制numpy数组不需要任何补充吗?
  1. Does Numpy array implement special methods, such as __reduce__ to inform Python on how to pickle them (or copyreg)? Numpy objects still expose a __reduce__ method, but it may be for compatibility reasons.
  2. Does Numpy uses Python's C-API structures that are supported out of the box by Pickle (like the new buffer protocol), so nothing supplementary is necessary in order to pickle numpy arrays?

推荐答案

Python 3 pickle支持__reduce__ ,在 腌制类实例部分.

Python 3 pickle still supports __reduce__, it is covered under the Pickling Class Instances section.

在这方面,Numpy的支持并没有改变;它在数组上实现__reduce__以支持Python 2或3中的酸洗.

Numpy's support has not changed in this regard; it implements __reduce__ on arrays to support pickling in either Python 2 or 3:

>>> import numpy
>>> numpy.array(0).__reduce__()
(<built-in function _reconstruct>, (<class 'numpy.ndarray'>, (0,), b'b'), (1, (), dtype('int64'), False, b'\x00\x00\x00\x00\x00\x00\x00\x00'))

返回一个三元素元组,该元组由一个用于重新创建值的函数对象,该函数的参数元组和一个不传递newinstance.__setstate__()的状态元组组成.

A three-element tuple is returned, consisting of a function object to recreate the value, a tuple of arguments for that function, and a state tuple to pass no newinstance.__setstate__().

这篇关于Python 3如何知道如何腌制扩展类型,尤其是Numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆