When pickling a function that references a global variable, the global variable is no longer referenced.
import cloudpickle
global x
x = 123
def increment_x():
global x
x += 1
return x
copy_of_increment_x = cloudpickle.loads(cloudpickle.dumps(increment_x))
print('Expecting 124 - got ' + str(increment_x()))
print('Expecting 125 - got ' + str(copy_of_increment_x()))
The following will print
Expecting 124 - got 124
Expecting 125 - got 124
When pickling a function that references a global variable, the global variable is no longer referenced.
The following will print Expecting 124 - got 124 Expecting 125 - got 124
Is there a way to reference globals?