Get "code object" of class

Okko Willeboordse trash at
Sat Oct 11 09:45:46 CEST 2008


"Why do you even need the classes code object anyway?"

I need to instantiate and use the class in another process.
This other process doesn't has access to the py or pyc file
holding the m_class (source) code so I can't use pickle.

Something like;
In the first process (that has access to my_class (source) code) I do;

c = compile(inspect.getsource(my_class), "<script>", "exec")
s = marshal.dumps(c)
# Send s to other process

In the other process, lacking access, I do;

c = marshal.loads(s)
exec c in my_dict
o = my_dict["my_class"]()
# Use o

More information about the Python-list mailing list