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