Luke McCarthy
Luke McCarthy

Reputation: 919

Python: Pickle derived classes as if they were an instance of the base class

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).

Upvotes: 7

Views: 3449

Answers (2)

unutbu
unutbu

Reputation: 879739

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.

Upvotes: 1

Thomas K
Thomas K

Reputation: 40340

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.

Upvotes: 2

Related Questions