The difference between a Python's value type and reference type and value passing and reference passing

Source: Internet
Author: User
Value type:

Contains: strings, tuples, numeric values, which are not allowed to be modified by themselves

Reference type:

Include: list, dictionary, itself allows modification

A = 2

b = A

A = 3

Modifies the value of a value type to point to a new memory address and does not change the value of the variable B

Lista = [up]

Listb = Lista

Lista[0] = 3

Modify the value of the reference type because the address of the LISTB is consistent with the Lista, so it is also modified

Typically only to replicate values, you can use the Shard operation

Listb = lista[:]

Value passing is only passing values
The reference is passed, the memory address is passed, and the memory address corresponding to the stored value is changed after the modification.
Using arrays to illustrate the clearest, for example, we define an array a[]={1,2};
So a[0]=1,a[1]=2.
If we pass the value of an element in array A as an argument, it is actually just a value pass, and the array itself has no effect
If we pass the pointer of array A as a parameter, then if the function is processed, it can directly modify the value in array a.

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.