Unpickle 接收端未定义的类
如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些类必须以某种方式存在于接收端,因为方法不与对象一起存储。所以,我认为不幸的是,使用特定的元类没有帮助,在这里......
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…