0

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!

  • 1
    But this is exactly the same behaviour as JS, surely? Assigning a var from one name to another then mutating it will carry over to the first name in JS too. Example: `var a = {foo: 'bar'}; var b = a; b[foo] = 'baz'; alert(a['foo']);` will show "baz". – Daniel Roseman May 15 '14 at 14:28
  • So it is. I have to admit I hadn't ever bumped into this behaviour (more through luck than anything I guess) - I only mentioned JS as I wasn't sure what the correct nomenclature for what I was diddling with in Python was, while in JS I was well aware it was referred to as an object. – Chris Rimmer May 15 '14 at 14:32
  • Fair enough. It's a dict. – Daniel Roseman May 15 '14 at 14:36
  • Yup, having now seen the answer can confirm duplicate. Any way I can mark it as such or is that a job for a mod? – Chris Rimmer May 15 '14 at 14:46

1 Answers1

2

You are probably looking for copy.deepcopy

object2 = copy.deepcopy(object1)
object3 = copy.deepcopy(object1)

-> Now you have three independent objects with no shared data.

tpatja
  • 690
  • 4
  • 11
  • Brilliant, that sorted it. I'd come across deepcopy while Google'ing the issue but I didn't understand the pages I read well enough to actually realise it was a solution. Appreciated! – Chris Rimmer May 15 '14 at 14:35
  • Appreciation on stackoverflow == upvote + accept answer ;) – tpatja May 15 '14 at 14:38
  • Can't upvote because noob, and there was a timer on accepting :) I did accept as soon a it'd let me though! – Chris Rimmer May 15 '14 at 14:42