-2

I need my class to create new copy of object when referenced.

For example:

obj1 = MyClass(1,2)
obj2 = obj1
obj2.1st_att = 5

>>> print obj1
(5,2)

I want obj1 to remain unlinked to obj2

Rocobop
  • 1
  • 4
  • 1
    Are you asking how to automatically make a copy of an object every time it is referenced? That's not at all how Float or Int work. – Scott Hunter Dec 21 '17 at 13:09
  • duplicate of duplicate : [See my answer on this thread, that also points to another answer](https://stackoverflow.com/questions/47871806/how-do-i-change-a-variable-inside-a-variable/47872098#47872098) – IMCoins Dec 21 '17 at 13:13
  • @ScottHunter So how do they work? I want my objects to be immutable basically, so that when iterated upon or referenced any changes made to new object isn't linked to original – Rocobop Dec 21 '17 at 13:16
  • @Rocobop What do you want exactly ? Please write your input, the code you've eventually tried, and the expected output. – IMCoins Dec 21 '17 at 13:21
  • @ScottHunter Its too long to post, but this is the jist of it: I made a list of objects from my class, and I iterated on the list and added each item to a dictionary. the key is the first attribute from my class. Now - the items in the dictionary are linked to the ones in the list. This is *Not* what i want, I need them to be unlinked. In the code I posted the same behavior is demonstrated, I wish to change this. – Rocobop Dec 21 '17 at 13:28
  • Maybe not post the whole code but a snippet that will help understand. You could post a bit portion of code with like 2 objects, the dictionnary in which you tried, the output you are getting that is not right, and the expected output. :) it should be mandatory to provide a [Minimal, complete and Verifiable Example.](https://stackoverflow.com/help/mcve) – IMCoins Dec 21 '17 at 13:31

2 Answers2

1

You should copy object in cases like this.

from copy import copy
obj1 = MyClass(1,2)
obj2 = copy(obj1)
obj2.1st_att = 5

Or deepcopy if your class is complicated and has lots of references.

Nestor Yanchuk
  • 1,186
  • 8
  • 10
0

Python isn't making a copy of ints or floats every time you assign them to a different variable. The thing is that they're not mutable; there's nothing you could do to an int that would change it, so however many variables you assign it to, you won't ever get any surprising behaviour.

With obj2.1st_att = 5, you're explicitly modifying an attribute of an object. There's no analog operation to that for an int. It is expected that this operation modifies the object, and that this change will be visible to anyone else who holds a reference to that object.

You should not try to work around that behaviour in any way, as that would break a lot of expectations and cause bugs or surprising behaviour in itself. It is good that making a copy of an object is an explicit action you need to take. Get used to it.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • I know... you are getting me wrong - I want every instance of my class to be a copy. Or in essence I want my class's objects to be immutable – Rocobop Dec 21 '17 at 13:22
  • but the only answers I found were on how to block changes made to attributes and that doesn't solve my problem. – Rocobop Dec 21 '17 at 13:23
  • Well, do you want to be able to assign to `obj2.1st_att = 5`? Is that an operation the object should support? Or do you merely want to raise an error when anyone tries to assign anything to your object? That's a different discussion than automatically making a copy. – deceze Dec 21 '17 at 13:25
  • I just want it to automatically make a new copy when object is referenced, I dont want to block changes to attributes – Rocobop Dec 21 '17 at 13:29
  • So you do not want immutable objects then, okay. Then read my answer again, you also *do not want* automagic copying behaviour. No you don't. You might think that you do, but you don't. Nu-uh. Seriously. [Let it go.](https://media.tenor.com/images/c75bedf16959bc8576f8ead56d94d863/raw) – deceze Dec 21 '17 at 13:30
  • But I want changes made to obj2 made only to obj2. Lets put it another way, I want the *entire* object copied, only when the *entire* object is referenced to. – Rocobop Dec 21 '17 at 13:34
  • No, what you want is to explicitly make a `copy` of the object when you want to make a copy of the object. Believe me, you do not want Python to guess what you want it to do. Right now you have encountered one situation where you want a copy. Then make that copy. This does not mean that you will want a copy in every single other situation henceforth. Sometimes you do want the reference, sometimes you don't. – deceze Dec 21 '17 at 13:36
  • Listen, I dont want to use copy every time, I want a copy made when I reference the object. can you just please tell me how to accomplish this if you know? – Rocobop Dec 21 '17 at 13:41
  • You can't. You shouldn't. You should really not approach the problem this way. [That's not how this works.](https://imgur.com/qGhiEIe) – deceze Dec 21 '17 at 13:43