pickling lambdas?

G

gong

hi

i would like to pickle a lambda; according to the library docs in 2.3,
i believe this shouldnt be possible, since a lambda is not a function
defined at the top level of a module (?)

however, upon google searching for "python lambda pickle" i find 2
posts, one including gvr, which apparently demonstrate that this was
being attempted and even suggest that it is feasible. has this become
available yet, or will it be soon?

thanks
gong
 
J

John Roth

gong said:
hi

i would like to pickle a lambda; according to the library docs in 2.3,
i believe this shouldnt be possible, since a lambda is not a function
defined at the top level of a module (?)

however, upon google searching for "python lambda pickle" i find 2
posts, one including gvr, which apparently demonstrate that this was
being attempted and even suggest that it is feasible. has this become
available yet, or will it be soon?

thanks
gong

Why do you want to do this? According to the docs, all that
is saved for a function or a class is the name. Code, data and
so forth is not saved, so it makes no sense to try to pickle
a lambda unless you've bound it to a name at the module
level. See 3.14.4 (Python 2.3.3 Library Reference.)

If you really want to serialize a function's code, look at
the marshal module.

John Roth
 
J

John J. Lee

i would like to pickle a lambda; according to the library docs in 2.3,
i believe this shouldnt be possible, since a lambda is not a function
defined at the top level of a module (?)
Yes.


however, upon google searching for "python lambda pickle" i find 2
posts, one including gvr, which apparently demonstrate that this was
being attempted and even suggest that it is feasible. has this become
[...]

No, read it again. What you found was Guido figuring out how to
*prevent* people pickling lambdas. Functions are pickled by saving
the function name. Lambdas have no function name, so pickle complains
if you try to pickle them.


John
 
P

Paul Rubin

John Roth said:
Why do you want to do this? According to the docs, all that
is saved for a function or a class is the name. Code, data and
so forth is not saved, so it makes no sense to try to pickle
a lambda unless you've bound it to a name at the module level.

I think the idea may be to pickle a closure, to save the data inside
it. I do remember some mention about pickling generators. Anyway, in
Python 2.x the closest you can come to that is pickling class
instances.
 
J

John Roth

Paul Rubin said:
I think the idea may be to pickle a closure, to save the data inside
it. I do remember some mention about pickling generators. Anyway, in
Python 2.x the closest you can come to that is pickling class
instances.

That ought to work as long as you unpickle it into the same module
so you can get the original class definition.

John Roth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top