Represent object type as

Jason jason.heeris at gmail.com
Thu Mar 25 11:39:01 EDT 2010


Hi,

I want to send objects (new style) over DBUS. DBUS can only send
fairly primitive types[1] so I turn my objects into dicts and send
that. I'm reusing the __getstate__ function I wrote for pickling like
so:

    def __getstate__(self):
        attrs = self.__dict__.copy()
        return attrs

...which is perfectly adequate to dict-ify and reconstitute this
simple class. That works just fine over DBUS.

The trouble is, the object could actually be of some slightly
different types, so I'd like to include that information as well. I
tried something like:

    def __getstate__(self):
        attrs = self.__dict__.copy()
        attrs.update({'type': type(self)})
        return attrs

...but then realised that "type" is not primitive enough for DBUS to
pickle.

So, (a) can I get the type name from the type object, or (b) is there
a better way to do this?

(This pertains to Python 2.5.4.)

Cheers,
— Jason

[1] http://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html#data-types



More information about the Python-list mailing list