I come from a javascript background and while working in Python recently I'm having a bit of trouble with what (in JS) I would have called objects.
I've created a class with an init function that works as expected:
class TestClass(object):
def __init__(self,var1,var2,...):
self.var1 = var1
self.var2 = var2
...
It may be worth knowing at this point, most of the variables that will be passed in here are actually arrays.
Everything is awesome, I can create instances of this object at will and it works as expected. For background, I'm doing this:
def make_object(var1,var2,...):
object = TestClass(var1,var2,...)
return object
object1 = make_object(var1, var2,...)
var1
, var2
and so on are all arrays which I will work on in-situ later.
Later in the script I do this:
object2 = object1
object3 = object1
As the values stored in these objects are arrays, I use another variable (worked out in another part of the script) to reduce the arrays down to just the element I need:
object2.var1 = object2.var1[requiredIndex]
object2.var2 = object2.var2[requiredIndex]
...
I do this for each array in the object and it sorts the data out perfectly - it was only when I added object3
into the whole equation and ran a similar command on it, that I discovered these lines:
object3.var1 = object3.var1[requiredIndex]
object3.var2 = object3.var2[requiredindex]
...
... discovered these lines were operating on the data I'd already refined using the same commands on object2
, which I have tested and found has also affected object1
so as far as I can tell the three object names are all just references to the same set of data.
I need object2
and object3
to be independent of each other (and ideally, independent of object1
) so how do I go about this? I hope I've provided enough detail to fully explain my issue. Cheers in advance!