Python prevent copying object as reference -
is possible copy object in python without copying reference?
for example, if define class
class someclass: def __init__(self): self.value = 0
and create instance
someobject = someclass() someobject.value = 12
and try copy instance:
anotherobject = someobject
and try modify property,
anotherobject.value = 10
the original property gets modified:
print someobject.value #prints 10
is there way prevent happening? clarify, want anotherobject.value
contain 10
, want someobject.value
still contain original 12
. possible in python?
thanks in advance.
the problem is,
anotherobject = someobject
you don't copy object, add reference it. copy object, try this:
from copy import copy anotherobject = copy(someobject)
Comments
Post a Comment