python-Shared References

Source: Internet
Author: User

Will a change?

The following three small pieces of code, the value of a will change?

" spam ">>> B = A"shrubbery"

>>> a = ["spam"]>>> B = a"shrubbery "

>>> A = ["spam"]>>> B = a[:]"  Shrubbery"

Analysis:

"Everything in Python is an object"

integers, or strings, are objects. Here, the string "spam", the list ["spam"], the string "shrubbery" are all objects, and A and B are references to objects here, references can be interpreted as the address of the object, or the name of the object.

Objects are also classified as mutable objects and immutable objects:

    • An immutable object in which the object points to an in-memory value that cannot be changed. when a variable is changed, because the value it refers to cannot be changed, it is equivalent to copy the original value and then change, this will open a new address, the variable point to the new address.
    • A mutable object in which the object points to an in-memory value that can be changed. variables (accurately referred to) after the change, is actually the value of its direct change, and did not occur replication behavior, nor open up new addresses, popular point is to change in situ .

In Python, numeric types ( int and float ), strings str, tuple tuples are immutable types. The list, dictionary dict, and set set are mutable types.


OK, with the above understanding, we can open the sleeve analysis.

工欲善其事, its prerequisite. A function can help us to analyze whether A and B refer to the same object--id (), and theID () function is used to get the memory address of the object .

python-Shared References

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.