SoFunction
Updated on 2024-11-07

Object referencing and copying code examples in Python

You could say that Python has no assignments, only references. You're doing the equivalent of creating a structure that references itself, so that leads to an infinite loop. In order to understand this, there's a basic concept that needs to be gotten right.

Python doesn't have 'variables', what we usually call variables are really just 'labels', references.

In python, "a=b" means that object a references object b. Object a itself does not have a separate allocation of memory space (important: not a copy!) , which points to the memory in the computer where object b is stored. Therefore, to copy an object to another object, you can't simply use the equal sign operation, you have to use other methods. For example, objects of the sequence class are (lists, tuples) have to use the slice operator (i.e. ':') to do the copying.

When python makes an assignment like b = a, it only creates a new reference to a, which adds 1 to a's reference count, and does not create a new object:

>>> a = 'xyz'
>>> import sys
>>> (a)
3
>>> b = a
>>> (b)
4
>>> id(a)
88292288L
>>> id(b)
88292288L

This results in unexpected behavior when the referenced object is a mutable object (list, dictionary, mutable collection, etc.):

>>> a = [1, 2, 3, 4]
>>> b = a
>>> (5)
>>> a
[1, 2, 3, 4, 5]

Because a and b reference the same object, changing one changes the other. When we want to create a copy instead of a reference, we can duplicate the object.

Copying objects is generally done using the copy module:

>>> a = [1, 2, 3, 4]
>>> import copy
>>> b = (a)
>>> (5)
>>> b
[1, 2, 3, 4, 5]
>>> a
[1, 2, 3, 4]

This works, but this copy is a shallow copy, and the new object copied contains references to items in the original object, which can also result in uncontrollable behavior if the items of the object are mutable:

>>> a = [1, [1, 2]]
>>> b = (a)
>>> b[1].append(3)
>>> b
[1, [1, 2, 3]]
>>> a
[1, [1, 2, 3]]

This is where deep copying comes into play. A deep copy will create a new object and recursively copy all the objects it contains:

>>> a = [1, [1, 2]]
>>> b = (a)
>>> b[1].append(3)
>>> b
[1, [1, 2, 3]]
>>> a
[1, [1, 2]]

For immutable objects (strings, numbers, tuples), etc., there is no need to copy them because they are immutable and you don't have to worry about inadvertently changing them. The copy operation will also only get the original object:

>>> a = (1, 2, 3)
>>> b = (a)
>>> a is b
True

For variable objects to (lists, dictionaries, variable sets), you can use the built-in functions list (), dict (), set () to shallow copy, the speed is faster than using the copy module.

Lists can also be shallowly copied using slices:

>>> a = [1, 2, 3, 4]
>>> b = a[:]
>>> a is b
False
>>> b
[1, 2, 3, 4]

The * operation on sequential data types (strings, lists, tuples) also simply copies the references to the items in the object, if a multidimensional list is created using *:

>>> a = [1, 2, 3]
>>> b = [a]
>>> c = b * 3
>>> (4)
>>> c
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]

It is better to use shallow copying in list derivation to create multidimensional lists, which can avoid implicit reference sharing:

>>> a = [1, 2, 3]
>>> c = [list(a) for i in range(3)]
>>> (4)
>>> c
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]

summarize

Above is this article on Python object reference and copy code example of the entire content, I hope to help you. Interested friends can continue to refer to other related topics on this site, if there are inadequacies, welcome to leave a message to point out.