Sai*_*aza 6 python pickle python-2.7 python-3.x
Python 2 文档说:
2.3 版后已弃用:使用 function(*args, **keywords) 而不是 apply(function, args, keyword)(请参阅解包参数列表)。
Pickle模块需要以下语法来定义__reduce__转储对象的方法:
def __reduce__():
return (<A callable object>, <A tuple of arguments for the callable object.>)
Run Code Online (Sandbox Code Playgroud)
(我知道从返回的元组长度__reduce__可以 >2,但需要 <=5。在当前问题的上下文中考虑长度 2 的情况。)
这意味着无法将关键字参数传递给可调用对象。在 Python 2 中,我有以下解决方法:
def __reduce__():
return (apply, (<A callable object>, ((<args>,), <kwargs>))
Run Code Online (Sandbox Code Playgroud)
但是,builtins.apply已在Python 3 中删除。builtins.apply在Python 3 中实现我自己的版本还有其他选择吗?
您可以functools.partial为此使用:
from functools import partial
def __reduce__():
return (partial(<A callable object>, <kwargs>), ())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
408 次 |
| 最近记录: |