Dear Experts,<br><br>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.x to 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.
<br><br>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?
<br><br>Example code illustrating problem is below:<br><br>>>> class f: pass<br>>>> g = f()<br>>>> g.x = ','.join<br>>>> import pickle; pickle.dumps(g)<br>Traceback (most recent call last):
<br>  File "<stdin>", line 1, in <module><br>  File "C:\Python25\lib\pickle.py", line 1366, in dumps<br>    Pickler(file, protocol).dump(obj)<br>  File "C:\Python25\lib\pickle.py", line 224, in dump
<br>    self.save(obj)<br>  File "C:\Python25\lib\pickle.py", line 286, in save<br>    f(self, obj) # Call unbound method with explicit self<br>  File "C:\Python25\lib\pickle.py", line 725, in save_inst
<br>    save(stuff)<br>  File "C:\Python25\lib\pickle.py", line 286, in save<br>    f(self, obj) # Call unbound method with explicit self<br>  File "C:\Python25\lib\pickle.py", line 649, in save_dict<br>
    self._batch_setitems(obj.iteritems())<br>  File "C:\Python25\lib\pickle.py", line 663, in _batch_setitems<br>    save(v)<br>  File "C:\Python25\lib\pickle.py", line 286, in save<br>    f(self, obj) # Call unbound method with explicit self
<br>  File "C:\Python25\lib\pickle.py", line 748, in save_global<br>    (obj, module, name))<br>pickle.PicklingError: Can't pickle <built-in method join of str object at 0x00A87FE0>: it's not found as __main__.join
<br><br>