Python: Pickle derived classes as if they were an instance of the base class
Question: 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).


Answer: 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](http://docs.python.org/library/pickle.html#pickling-
and-unpickling-extension-types), [Python
3](http://docs.python.org/py3k/library/pickle#pickling-class-instances)) 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.

