Dear Experts,
I love the pickle module, but I occasionally have problems pickling a
function. For example, if I create an instance g of class f and assign
g.xto a function, then I cannot pickle g (example code below). I know
that I
can pickle f separately if I want to, and I understand why I get the
pickling error.
But is there a way to assign functions to instances of a class without
preventing pickleability? It doesn't seem unreasonable to me to want to
assign functions to instances of a class (after all functions are first
class objects, so why shouldn't I be able to pass them around?) Is there a
better way or is this just a limitation of pickle?
Example code illustrating problem is below:
>>> class f: pass
>>> g = f()
>>> g.x = ','.join
>>> import pickle; pickle.dumps(g)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python25\lib\pickle.py", line 1366, in dumps
Pickler(file, protocol).dump(obj)
File "C:\Python25\lib\pickle.py", line 224, in dump
self.save(obj)
File "C:\Python25\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python25\lib\pickle.py", line 725, in save_inst
save(stuff)
File "C:\Python25\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python25\lib\pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "C:\Python25\lib\pickle.py", line 663, in _batch_setitems
save(v)
File "C:\Python25\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python25\lib\pickle.py", line 748, in save_global
(obj, module, name))
pickle.PicklingError: Can't pickle <built-in method join of str object at
0x00A87FE0>: it's not found as __main__.join
--
http://mail.python.org/mailman/listinfo/python-list