Home  >  Article  >  Backend Development  >  Python's value types and reference types and the difference between value passing and reference passing

Python's value types and reference types and the difference between value passing and reference passing

巴扎黑
巴扎黑Original
2017-07-20 09:28:142048browse

Value type:

Contains: string, tuple, value, itself is not allowed to be modified

Reference type:

Contains: list, dictionary, itself is allowed Modify

a = 2

b = a

a = 3

Modify the value of the value type, just let it point to a The new memory address will not change the value of variable b

lista = [1,2]

listb = lista

lista[0 ] = 3

Modify the value of the reference type. Because the address of listb is consistent with lista, it will also be modified

Generally just to copy the value, you can use the sharding operation

listb = lista[:]

Value passing only passes the value
Reference passing passes the memory address. After modification, the value stored corresponding to the memory address will be changed.
It is clearest to use an array as an example. For example, if we define an array a[]={1,2};
then a[0]=1,a[1]=2.
If we pass the element value in the array a as a parameter, it is actually just a value transfer and has no effect on the array itself.
If we pass the pointer of the array a as a parameter, then the processing function can Directly modify the value in array a.

The above is the detailed content of Python's value types and reference types and the difference between value passing and reference passing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn