¡Hi everybody!
What I have to do is this:
import pickle
exec "def f(): print 'Hi!'"
code = pickle.dumps(f)
print code
As you can see, it works fine.
But, in order to avoid defining global variables, (as for example f in previous code), I'd like to do something like this:
import pickle
d = {}
exec "def f(): print 'Hi!'" in d
d['f']()
code = pickle.dumps(d['f'])
print code
It prints 'Hi!', it means that python recognizes d['f'] as a function, but it can't pickle it.
Do you have any idea of how to do this? I think that it is because in the former case, python knew where to find the function (it is in '__main__') but in the latter case, it's inside a dictionary located in '__main__' I think that if you can tell python the real module direction of this function, it will work.
Thanks!