Skip to main content

8.15) Copying Immutable Objects


Sometimes, you don’t want yet another reference to the same object, all you want is a copy of it and you want to be able to modify the initial object safely without affecting copies.

In the case of immutable objects, since they cannot be modified, referencing has the effect of creating a copy (see previous example). Let’s have another look at our previous example:

>>> a=2
>>> b=a
>>> a='two'
>>> print a,b
two 2

In this case, b=a has the effect of creating a copy of a, since b is not modified when a is re-assigned.