Unpickle 接收端未定义的类

发布于 2024-11-19 20:22:25 字数 399 浏览 2 评论 0原文

pickle 文档,类通常以这样的方式进行腌制,即它们要求接收端的模块中存在完全相同的类。但是,我确实注意到,类还有一些 __getstate__() 和 __setstate__() 方法,这些方法会影响它们的实例的 pickle 方式...

这有多可行创建一个元类,该元类允许对从该元类创建的类(换句话说,该元类的实例)进行酸洗和取消酸洗,即使这些类不存在于接收端? (尽管我认为元类可能必须存在。)


在类或元类中使用 __reduce__() 方法也值得研究吗?

As stated in the pickle documentation, classes are normally pickled in such a way that they require the exact same class to be present in a module on the receiving end. However, I do note that there's also some __getstate__() and __setstate__() methods for classes, which affect how their instances are pickled...

How feasible would it be to create a metaclass that would allow pickling and unpickling of the classes created from that metaclass (in other words, the instances of that metaclass) even without the classes being present on the receiving end? (Though I think the metaclass would probably have to be present.)


Would utilizing a __reduce__() method in the class or metaclass also be something to look into?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白鸥掠海 2024-11-26 20:22:25

这些类必须以某种方式存在于接收端,因为方法不与对象一起存储。所以,我认为不幸的是,使用特定的元类没有帮助,在这里......

The classes have to be somehow present on the receiving end, because methods are not stored with the objects. So, I think that using a specific metaclass unfortunately can't help, here…

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文