References and copies of objects in Python, and python object references

Source: Internet
Author: User

References and copies of objects in Python, and python object references

When python assigns a value like B = a, only a new reference to a will be created, so that the reference count of a is increased by 1, and no new object will be created:

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

In this way, when the referenced object is a mutable object (list, Dictionary, variable set, etc.), unexpected behavior will occur:

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

Because a and B reference the same object, changing one of them also changes. When we want to create a copy instead of a reference, we can copy the object.

The copy module is generally used for copying objects:

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

This is fine, but this kind of replication is a kind of shallow replication. The new object to be copied contains references to the items in the original object. If the items of the object are variable objects, uncontrollable behavior will also occur:

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

This requires deep replication. Deep replication creates a new object and Recursively copies all the objects it contains:

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

For unchangeable objects (strings, numbers, tuples), there is no need to copy them, because they are unchangeable and don't worry about changing them inadvertently. The copy operation only obtains the original object:

>>> a = (1, 2, 3)>>> b = copy.copy(a)>>> a is bTrue

For mutable objects (lists, dictionaries, and variable sets), you can use the built-in Functions list (), dict (), and set () for shortest, the speed is faster than that of the copy module.

You can also use slice for shortest copy in the list:

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

When you perform the * operation on the sequence data type (string, list, And tuples), you only copy the reference of the items in the object. If you use * to create a multi-dimensional list:

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

It is best to use the shortest copy in list derivation to create a multi-dimensional list to avoid implicit reference sharing:

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

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.