You are right, I apologize.
There is no need (like ever), we're all learning from this. I actually though that your code will work as I've tried something like that in IDLE 4 or 5 month ago for training and it did
![Huh ???](https://www.pinkpetal.org/Smileys/DarkB/huh.gif)
(due to a single namespace...)
What I should have said is this: It is not possible to pickle functions, classes, methods and so on themselves. The are pickled by reference, not by "value".
If you do this
import pickle
def somefunction():
print "hello"
f = open("delme.bin", "wb")
pickle.dump(somefunction, f)
f.close()
and then, in another file, do this
import pickle
f = open("delme.bin", "rb")
somefunction = pickle.load(f)
f.close()
you will get this
Traceback (most recent call last):
File "/home/rudi/Desktop/delme2.py", line 4, in <module>
somefunction = pickle.load(f)
File "/usr/lib/python2.7/pickle.py", line 1378, in load
return Unpickler(file).load()
File "/usr/lib/python2.7/pickle.py", line 858, in load
dispatch[key](self)
File "/usr/lib/python2.7/pickle.py", line 1090, in load_global
klass = self.find_class(module, name)
File "/usr/lib/python2.7/pickle.py", line 1126, in find_class
klass = getattr(mod, name)
AttributeError: 'module' object has no attribute 'somefunction'
So, if pickle cannot import the pickled function, you can't unpickle it. However, if pickle can import the function, so can you (usually), so why pickle it in the first place? That's why I generally think of functions, classes, ... as unpickleable.
That's kinda a theoretical debate... pickle will store the reference so it will not fail, therefor it can store functions... It's far from useful, as you've said, import of module1 would be required to unpickle in module2, but the same module would import the original function, so the whole thing would be kinda pointless. I am forgetting this very often as I work with IDLE and RenPy where imports of modules you've wrote yourself are rare.
I would not put it like that. Most data storage methods I know don't allow you to store custom objects. For example, JSON, XML and SQL work like that. Those are far from useless ![Wink ;)](https://www.pinkpetal.org/Smileys/DarkB/wink.gif)
And non of those were developed by Python devs, pickle was, just mimicking those three would be a waste...
------------------------------------------------
We were talking about an error in RenPy and a number of things has to be taken into account:
- RenPy does seem to
pickle save functions (
even those defined during the game runtime) (everything in init is being loaded every time game is started (new game or load) so it is obvious that all functions defined there are available at all times). Maybe it's because of their "global" namespace or they are using cloud module (that allows to pickle code and therefor anything, but if they did use cloud, lambdas and nested functions should have been saved as well ?
![Sad :(](https://www.pinkpetal.org/Smileys/DarkB/sad.gif)
) I am not really that interested in how it works at the moment, I just made damn sure that I know what does work and what doesn't.
- Error occurred because of lambda that cannot be pickled by Pickle/cPickle at all (if you try to pickle lambda with the exact same code as you provided above, it will fail in the first module, not the second).