I am defining a class like this:
>>> class foo(object):
... def __init__(self,x):
... self.x = x
...
>>>
I next define an instance of this class:
>>> inst1 = foo(10)
>>> inst1.x
10
Now, I would like to copy the same instance into a new variable and then change the value of x:
>>> inst2 = inst1
>>> inst2.x = 20
>>> inst2.x
20
The problem, however, is that this will change the value of x in inst1 as well:
>>> inst1.x
20
and I don't know why. I do know that an alternative method is to say:
>>> inst2 = foo(20)
However, I don't like to do this because my actual class takes a lot of input arguments out of which I need to change only one or two specific data attribute(s) when creating different instances. It is thus easier to just create one instance, copy it into new variables and then change the specific data attribute that I like for each instance, like I said above. Any suggestions is greatly appreciated!
0 answers
No answers yet.
Log in to answer this question.
This is a pure programming question. You should post it on stackoverflow or a python forum. I'll close it and reopen it if you make it relevant to the site.