473,388 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

pickling lambdas?

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
Jul 18 '05 #1
4 2507

"gong" <go****@cutey.com> wrote in message
news:9a**************************@posting.google.c om...
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
Jul 18 '05 #2
go****@cutey.com (gong) writes:
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
Jul 18 '05 #3
"John Roth" <ne********@jhrothjr.com> writes:
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?


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.
Jul 18 '05 #4

"Paul Rubin" <http://ph****@NOSPAM.invalid> wrote in message
news:7x************@ruckus.brouhaha.com...
"John Roth" <ne********@jhrothjr.com> writes:
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?


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.


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

John Roth
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marc | last post by:
Hi all, After some research I've decided that my previous question (Confusing problem between Tkinter.Intvar...) was headed in the wrong direction. Partly because I think I have a greater...
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
1
by: Edward Loper | last post by:
I'm having trouble pickling subclasses of dict when they contain cycles. In particular: >>> import pickle >>> class D(dict): pass >>> d = D() >>> d = d # add a cycle. >>> print d {1: {...}}...
8
by: Hans Georg Krauthaeuser | last post by:
Dear all, I have a long running application (electromagnetic compatibility measurements in mode-stirred chambers over GPIB) that use pickle (cPickle) to autosave a class instance with all the...
1
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can...
2
by: Kirk Strauser | last post by:
I have a module that defines a Search class and a SearchResult class. I use these classes by writing other modules that subclass both of them as needed to interface with particular search engines....
0
by: jkn | last post by:
Hi all this is my first go at pickling and I'm having trouble with variables of type VT_DATE returned from a COM application. I don't seem to be successfully pickling/depickling to the the same...
0
by: jeanphilippe.aumasson | last post by:
Hi, I have some problems when pickling an instance of a class, i don't retrieve all its attributes instances after loading. I'm quite a beginner in Python, so it may be a stupid error... Here...
0
by: Irmen de Jong | last post by:
I'm having troubles pickling classes that extend Exception. Given the following source: class Foo(object): def __init__(self, m): self.m=m class Bar(Exception): def __init__(self, m):
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.