Python:Pickle 派生类,就好像它们是基类的实例一样

发布于 2024-10-08 19:08:02 字数 207 浏览 6 评论 0原文

我想定义一个基类,以便在对派生类实例进行腌制时,将它们像基类的实例一样进行腌制。这是因为派生类可能存在于酸洗的客户端,但不存在于服务器端,但这对服务器来说并不重要,因为它只需要来自基类的信息。我不想为每个客户端动态创建类。

基类只是一个“对象句柄”,其中包含一个 ID,以及在服务器上定义的方法,但我希望客户端能够对服务器类进行子类化并定义新方法(只有客户端才能看到,但这并不重要)。

I want to define a base class so that when derived class instances are pickled, they are pickled as if they are instances of the base class. This is because the derived classes may exist on the client side of the pickling but not on the server side, but this is not important to the server since it only needs information from the base class. I don't want to have to dynamically create classes for every client.

The base class is simply an "object handle" which contains an ID, with methods defined on the server, but I would like the client to be able to subclass the server classes and define new methods (which would only be seen by the client, but that doesn't matter).

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

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

发布评论

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

评论(2

鸢与 2024-10-15 19:08:02

相信你可以通过给对象一个__reduce__方法来做到这一点,返回一个元组,其第一部分应该是BaseClass.__new__(这将在 unpickle 中加载对象时调用)。请参阅 pickle 文档 (Python 2,< a href="http://docs.python.org/py3k/library/pickle#pickling-class-instances" rel="nofollow">Python 3)了解完整详细信息。我没有尝试过这个。

根据您正在执行的操作,使用更简单的序列化格式(例如 JSON)可能会更容易,并且在每一侧都有代码来重建相关对象。

I believe you can do it by giving the object a __reduce__ method, returning a tuple, the first part of which should be BaseClass.__new__ (this will be called when loading the object in unpickling). See the pickle documentation (Python 2, Python 3) for the full details. I haven't attempted this.

Depending on what you're doing, it might be easier to use a simpler serialisation format like JSON, and have code on each side to reconstruct the relevant objects.

冷清清 2024-10-15 19:08:02

您可以在 Python 中动态更改对象的类:

import cPickle as pickle

class Foo(object):
    def __init__(self):
        self.id=1
class Bar(Foo):
    def derived_class_method(self): pass

bar=Bar()
bar.id=2
bar.__class__=Foo                # changes `bar`'s class to Foo
bar_pickled=pickle.dumps(bar)
bar2=pickle.loads(bar_pickled)
bar.__class__=Bar                # reset `bar`'s class to Bar
print(repr(bar2))
# <__main__.Foo object at 0xb76b08ec>
print(bar2.id)
# 2

但是,我不确定使用它是否是最佳设计决策。我喜欢Thomas K 使用 JSON 的想法

You can change an object's class dynamically in Python:

import cPickle as pickle

class Foo(object):
    def __init__(self):
        self.id=1
class Bar(Foo):
    def derived_class_method(self): pass

bar=Bar()
bar.id=2
bar.__class__=Foo                # changes `bar`'s class to Foo
bar_pickled=pickle.dumps(bar)
bar2=pickle.loads(bar_pickled)
bar.__class__=Bar                # reset `bar`'s class to Bar
print(repr(bar2))
# <__main__.Foo object at 0xb76b08ec>
print(bar2.id)
# 2

I'm not sure using this is the best design decision, however. I like Thomas K's idea of using JSON.

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