A simple question, perhaps, but I can't quite phrase my Google query to find the answer here. I've had the habit of making copies of objects when I pass them into object constructors, like so:
...
def __init__(self, name):
self._name = name[:]
...
However, when I ran the following test code, it appears to not be necessary, that Python is making deep copies of the object values upon object instantiation:
>>> class Candy(object):
... def __init__(self, flavor):
... self.flavor = flavor
...
>>> flav = "cherry"
>>> a = Candy(flav)
>>> a
<__main__.Candy object at 0x00CA4670>
>>> a.flavor
'cherry'
>>> flav += ' and grape'
>>> flav
'cherry and grape'
>>> a.flavor
'cherry'
So, what's the real story here? Thanks!
EDIT:
Thanks to @Olivier for his great answer. The following code documents a better example that Python does copy by reference:
>>> flav = ['a','b']
>>> a = Candy(flav)
>>> a.flavor
['a', 'b']
>>> flav[1] = 'c'
>>> flav
['a', 'c']
>>> a.flavor
['a', 'c']
It is because strings are immutable.
The operator +=
, rather confusingly, actually reassigns the variable it is applied to, if the object is immutable:
s = 'a'
ids = id(s)
s += 'b'
ids == id(s) # False, because s was reassigned to a new object
So, in your case, in the beginning, both flav
and a.flavor
point to the same string object:
flav --------\
'cherry'
a.flavor ----/
But when you write flav += 'and grape'
the variable flav
gets reassigned to a new string object:
flav --------> 'cherry and grape'
a.flavor ----> 'cherry' # <-- that string object never changes
It is confusing, because usually, when you call an operator on a variable, it doesn't change the variable. But just in the case of an immutable object, it does reassign the variable.
So the final answer to your question is, yes, it makes sense to copy the objects upon instantiation, especially if you are expecting a mutable object (which is often the case). It the object was immutable, it will not harm to copy it anyway.
it appears to not be necessary
Appears? Your question is entirely about design and meaning. This is not a preference or habit question.
Does the class contract include the ability modify a mutable argument? If so, do NOT make a copy.
Does the class contract assert that a mutable argument will not be modified? If so, you MUST make a copy.
Your questions is answered entirely by the contract definition for the class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With