I wrote this simple python program to help me with a bug in another program. It clearly illustrates the problem.
import copy
class Obj(object):
def __init__(self, name):
self.name = name
def one(o):
print("1: o.name:", o.name) # "foo"
obackup = copy.deepcopy(o)
o.name = "bar"
print("2: o.name:", o.name) # "bar"
print("3: obackup.name:", obackup.name) # "foo"
o = obackup
print("4: o.name:", o.name) # "foo"
def two(o):
print("5: o.name:", o.name) # "bar"!
def main():
o = Obj("foo")
one(o)
two(o)
main()
My guess is that o
is being overwritten somehow as a local variable to the function one()
. But I have no idea how to fix that.