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

巴扎黑
Release: 2017-07-20 09:28:14
Original
2038 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!