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

Gabriel Rossetti gabriel.rossetti at arimaz.com
Fri Apr 16 03:25:34 EDT 2010


Andreas Löscher wrote:
>>>>>> 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
>
>   
Yes, it works, thank you,
Gabriel



More information about the Python-list mailing list