[issue19272] Can't pickle lambda (while named functions are ok)

Jesús Cea Avión report at bugs.python.org
Thu Oct 17 00:00:39 CEST 2013


Jesús Cea Avión added the comment:

Lambdas are "anonymous" functions, by definition. And, usually, they are not top level functions, but defined inside others.

If at your "top level" (module) you do:

"""
a = lambda x: 2*x
"""

You don't have an anonymous function, but a function called "a". You can argue why "def a() : return 2*x" can be picked, but "a = lambda x: 2*x" can not. They look similar.

What the poster actually wanted (tell me if I am wrong), I guess, is to be able to serialize the function code and send it to other process to be executed there. Something like "mobile" code. As is, pickle doesn't allow it (that is the reason I was brainstorming about being able to pickle "function.__code__" objects), and it is good because pickle guarantees compatibilities between Python versions, but "__code__" objects are particular to a certain Python virtual machine and certain version of it.

That said, projects like PYRO provide "mobile code". I think that a recipe for that would be nice in the documentation, with a big warning saying "be sure you have the same implementation in both sides", and explicit versioning in the client and version checking in the server, in the recipe (to make it clear that same version is something you need).

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19272>
_______________________________________


More information about the Python-bugs-list mailing list