I am trying to run the following code:
import pickle
def foo():
print("i am foo")
pickle_foo = pickle.dumps(foo)
def foo():
print("i am the new foo")
fkt = pickle.loads(pickle_foo)
return fkt()
foo()
The expected behavior would be:
- the new defined function "foo" is called
- in the new function the old function gets unpickeled and then called
output:
i am the new foo
i am foo
What actually happens is:
- the new function foo gets called, and then recursively calls itself until a Recursion Error gets thrown:
RecursionError: maximum recursion depth exceeded while calling a Python object
The error does not occur, when the two functions are named differently, but that would be very unpractical for my project. Could anyone explain, why this behavior occurs and how to avoid it (without changing the function names)?