__reduce__和__reduce_ex__有什么区别? [英] What is the difference between __reduce__ and __reduce_ex__?

查看:422
本文介绍了__reduce__和__reduce_ex__有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这些方法是用于酸洗/酸洗的,与reduce内置函数无关,但是两者之间有什么区别,为什么我们需要两者?

I understand that these methods are for pickling/unpickling and have no relation to the reduce built-in function, but what's the difference between the 2 and why do we need both?

推荐答案

文档表示

如果提供的话,在酸洗时 __reduce__()将被调用而没有 参数,并且它必须返回一个 字符串或元组.

If provided, at pickling time __reduce__() will be called with no arguments, and it must return either a string or a tuple.

另一方面

有时候了解 实施时的协议版本 __reduce__.这可以通过 实现一个名为 __reduce_ex__代替 __reduce__. __reduce_ex__,当它 存在,优先于 __reduce__(您仍然可以提供 __reduce__向后 兼容性). __reduce_ex__ 方法将被单个调用 整数参数,协议 版本.

It is sometimes useful to know the protocol version when implementing __reduce__. This can be done by implementing a method named __reduce_ex__ instead of __reduce__. __reduce_ex__, when it exists, is called in preference over __reduce__ (you may still provide __reduce__ for backwards compatibility). The __reduce_ex__ method will be called with a single integer argument, the protocol version.

在令人发指的手上, Guido说这是一个可以清理的区域.

On the gripping hand, Guido says that this is an area that could be cleaned up.

这篇关于__reduce__和__reduce_ex__有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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