How does Python 3 know how to pickle extension types, especially Numpy arrays?

梦想与她 提交于 2019-12-04 13:02:41

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

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'))

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__().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!