Importing a class without knowing the module

Mike Meyer mwm at mired.org
Thu Nov 17 19:10:39 EST 2005


Franck PEREZ <franck.perez at gmail.com> writes:
> ########### My test application ############
> class Foo(object):
>   #The class I'd like to serialize
>   pass
>
> import myMarshaller
> foo = Foo()
> s = myMarshaller.dumps(foo) #works fine, spits something like <object
> class = "Foo"...>
> another_foo = loads(s) #fails, see below
>
> ########### My marshaller (in its own module) ############
> def loads(s):
>   #First, get class name (here "Foo")
>   klass = eval(className) #fails because "Foo" is not in the
> marshaller's namespace !
>
> How could I tell the marshaller to locate the Foo class and any other
> class I try to deserialize ?

How about adding Foo.__file__ to the serialized data?

    <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list