You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The global variable are not correctly handeled in the __main__ module as they do not remain global.
Script
fromcloudpickleimportdumps, loadsVARIABLE="uninitialized"deffunc1():
globalVARIABLEprint("[func1] global var id{}: {}".format(id(VARIABLE), VARIABLE))
VARIABLE="initialized"returnVARIABLE# Dump and reload the functionreloaded_f1=loads(dumps(func1))
# The function call does not change the value of the global VARIABLEassertreloaded_f1() =="initialized"print(VARIABLE)
# Changing the value of VARIABLE does not affect the functionVARIABLE='test'assertreloaded_f1() =="initialized"# But redumping it changes the valueassertloads(dumps(func1))() =="initialized"print(VARIABLE)
Output
[func1] global var id140197480377584: uninitialized
uninitialized
[func1] global var id140197483839984: initialized
[func1] global var id140197480295592: testtest
I think a way to avoid this is to pass the global variable to _fill_function and use globals():
If the global variable is already present in globals, do not do anything.
If it is not present, add it with the given value.
Activity
FIX globals for functions defined in __main__ (#188)
py-cloudpickle: updated to 2.0.0