Summary of Python object reference issues

Source: Internet
Author: User

For the object reference problem, has been the state of smattering, is now ready for use.

When you add and subtract operations to an immutable object, a new immutable instance is created in memory without affecting the original reference
>>> c=12
>>> D=c
>>> c+=1
>>> C
13
>>> D
12
>>>
>>> a= "345a"
>>> B=a
>>> a+= "Dfger"
>>> A
' 345adfger '
>>> b
' 345a '
>>> (a=)
>>> B=a
>>> b
(1, 2, 3)
>>> a= ' Wert '
>>> A
' Wert '
>>> b
(1, 2, 3)
>>>

When manipulating a Mutable object, the objects in memory change, affecting all references, and the difference from the previous case is that the original object is changed in memory when the object is altered or a new object is created.
>>> a=[1,2,3]
>>> B=a
>>> A.insert (0, "Ertwer")
>>> A
[' Ertwer ', 1, 2, 3]
>>> b
[' Ertwer ', 1, 2, 3]
>>>
>>> a={1:2, ' a ': ' B ', ' 4 ': 5464}
>>> B=a
>>> a["678"]= "F**k"
>>> A
{' A ': ' B ', 1:2, ' 678 ': ' F**k ', ' 4 ': 5464}
>>> b
{' A ': ' B ', 1:2, ' 678 ': ' F**k ', ' 4 ': 5464}
>>>

The crux of the problem is whether the object is mutable. In contrast, the immutable object in Python is only numbers, strings, tuples, frozensets Four, changing one of the references, without affecting other references; other list, dict, and both of the subclass objects , all belong to mutable, change any reference, and all other references will be affected.

Summary of Python object reference issues

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.