I have a function that I want to use to manipulate an input parameter, however, in the process I must copy the input parameter. I do not want to return the modified object (because multiprocessing may or may not be involved). How can I reassign the input parameter to the copied value? Here's a representation of the function:
from copy import deepcopy, copy
def myFunc(myObj,i):
myObj.x = 'start'
# I can't avoid this step:
modObj = deepcopy(myObj)
modObj.x = 'modified'
if i == 0:
myObj.x = modObj.x
elif i == 1:
myObj = modObj
elif i == 2:
myObj = copy(modObj)
elif i == 3:
myObj.__dict__ = modObj.__dict__
class MyClass(object):
def __init__(self):
self.x = "construct"
for i in range(4):
temp = MyClass()
myFunc(temp,i)
print i,temp.x
And here's the output:
0 modified
1 start
2 start
3 modified
Since option #3 is so close to what I want to do, is there an "official" way to do it? E.g.
myObj.shallowcopy(modObj)
where this shallowcopy behaves similarly to the shallow copy in copy. One alternative would be to embed MyClass as a member of some other class, and pass that class to the function... but if I can do that, why can't python just save me that step by copying over member data? I've read this, but they didn't seem to come up with an answer for me.