By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,636 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

object pickling

P: n/a
i understand than most objects are pickable, but i cannot get a function
pickled, or a class for that matter:

$ python
Python 2.3.3 (#2, Jan 13 2004, 00:47:05)
[GCC 3.3.3 20040110 (prerelease) (Debian)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import pickle
def myFun(): .... return "Hi there! I am myFun..."
.... fd = open("myFile.pck", "w")
pickle.dump(myFun, fd)
fd.close()
^D $ python import pickle
fd = open("myFile.pck", "r")
aFun = pickle.load(fd) Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.3/pickle.py", line 1390, in load
return Unpickler(file).load()
File "/usr/lib/python2.3/pickle.py", line 872, in load
dispatch[key](self)
File "/usr/lib/python2.3/pickle.py", line 1104, in load_global
klass = self.find_class(module, name)
File "/usr/lib/python2.3/pickle.py", line 1140, in find_class
klass = getattr(mod, name)
AttributeError: 'module' object has no attribute 'myFun'


how come?

bye

max
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
max(01)* wrote:
i understand than most objects are pickable, but i cannot get a function
pickled


also i would like to know: why in the world would anyone want to pickle
a builtin function, as the documentation says it's possible?

m
Jul 18 '05 #2

P: n/a
On Sat, 24 Jul 2004 17:54:09 GMT, max(01)* <ma*@fisso.casa> wrote:
max(01)* wrote:
i understand than most objects are pickable, but i cannot get a
function pickled


also i would like to know: why in the world would anyone want to pickle
a builtin function, as the documentation says it's possible?

m

actually docs explain well that you can't ,at least that way.

from python2.3/lib/node64.html

Note that functions (built-in and user-defined) are pickled by ``fully
qualified'' name reference, not by value. This means that only the
function name is pickled, along with the name of module the function is
defined in. Neither the function's code, nor any of its function
attributes are pickled. Thus the defining module must be importable in the
unpickling environment, and the module must contain the named object,
otherwise an exception will be raised.

follow there an explanation of this choice

ciao Paolino
--
.....lotta dura per la verdura

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.