开发者

Python: Ensuring my class gets pickled only with the latest protocol

开发者 https://www.devze.com 2023-02-23 01:56 出处:网络
I\'m developing a class which can be meaningfully pickled only using protocol 2 (and upwards on Python 3.) When an older protocol is used, hard-to-trace bugs happen. I want to save the 开发者_如何学Go

I'm developing a class which can be meaningfully pickled only using protocol 2 (and upwards on Python 3.) When an older protocol is used, hard-to-trace bugs happen. I want to save the 开发者_如何学Gousers of the class some debugging pain, so I want the class to immediately raise an exception if it's being pickled with a protocol of less than 2.

How can I do that?


You can implement the __reduce_ex__() method on your class. It receives one parameter, which is the protocol version. Simply raise an exception if the version is not 2.

0

精彩评论

暂无评论...
验证码 换一张
取 消