def foo(var, new_value):
var = new_value
x = 1
multiprocessing.Process(target=foo, args=(x,2))
p.start()
p.join()
print(x)
I would expect the output of the print(x) is 2, but in fact I got 1. How could I assign new values to an existing variable?
foo()function doesn't changex, becausexis fully evaluated before being passed to the function.varreceives the value ofxinvar. You then changevarto a different value.varis a local variable, so it goes away whenfoo()ends.multiprocessing; this wouldn't work if you just didfoo(x, 2)with nomultiprocessinginvolved at all.