Un/serialize functions?

Jonathan Hogg jonathan at onegoodidea.com
Tue Aug 6 19:32:05 CEST 2002

On 6/8/2002 17:13, in article sAS39.31845$eH2.15177597 at ruti.visi.com, "Grant
Edwards" <grante at visi.com> wrote:

> In article <c73a070e.0208052111.798f21d5 at posting.google.com>, d2002xx wrote:
>> Is it possible to serialize a complete function/method, and
>> unserialize without involving its source file?
> I've always been curious why converting a data structure into a
> system-independent representation is called "serializing". I've
> looked around on Google and haven't really found anything.
> Anybody know the etymology of "serializing" a data structure?

I'd always presumed that it is related to taking a random (as in access)
data structure and writing it out in a serial form suitable for saving to a
serial device like a tape (or a network stream, or a file, etc., etc.).

[I didn't notice, did someone answer the original question? If not, try
taking a look at the 'marshal' module - though I think you can only marshal
'compile'd code:

>>> import marshal
>>> code = compile( """
... def hello( x ):
...     print 'Hello', x
... """, '<nowhere>', 'exec' )
>>> s = marshal.dumps( code )
>>> open( 'foo', 'wb' ).write( s )
>>> s = open( 'foo', 'rb' ).read()
>>> code = marshal.loads( s )
>>> exec code
>>> hello( 'World!' )
Hello World!

Not ideal, but a start.]


More information about the Python-list mailing list