Object serialization: transfer from a to b (non-implemented code on b)

Andreas Löscher andreas.loescher at s2005.tu-chemnitz.de
Thu Apr 15 17:59:05 CEST 2010


> >>>> import types
> >>>> import marshal
> >>>> def a(): pass
> >>>>         
> > ... 
> >   
> >>>> s=marshal.dumps(a.__code__)
> >>>> f=types.FunctionType(marshal.loads(s), {})
> >>>> f
> >>>>         
> > <function a at 0x7f6308a66de8>
> >   
> 
> What version of python do you have? If I try your code above I get :
> 
>  >>> import types
>  >>> import marshal
>  >>> def a(): pass
> ...
>  >>> s=marshal.dumps(a.__code__)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> AttributeError: 'function' object has no attribute '__code__'
> 

I used Version 2.6 for this. __code__ is just an alias for func_code.

>>> def a(): pass
...
>>> s=marshal.dumps(a.func_code)

This should allways work, unless the implementation of the function
object changes. (I have tested it under 2.4, 2.5 and 2.6, but it should
also work in further versions)

Best,
Andreas




More information about the Python-list mailing list