I encountered this error "SyntaxError: can't assign to function call" while experimenting with the pickle method. What I was trying to do was store something simple like the string "x = 5"
and have python eval() the string later to bring the variable x back into existence in a later script. I understand why the code fails but I was wondering if there was any workaround for assigning the result of a function an object. As in have a function return the name of a variable and be able to assign that variable an object. Here is the code that fails:
import pickle y = "x = 5" open_file = open("pickle_file.pkl", "wb") pickle.dump(y, open_file, -1) open_file.close() del y newly_opened = open("pickle_file.pkl", "rb") loaded = pickle.load(newly_opened) loaded.split('=') = eval(loaded.split('=')[-1]) #this fails because of the function on the #variable side print loaded.split("="), 'should equal', eval(loaded.split('=')[-1])