Whether the Python pass is a value or a reference

Source: Internet
Author: User

Let's look at the relationship between variables and objects: Everything in Python is an object, a number is an object, a list is an object, a function is an object, everything is an object. While a variable is a reference to an object (also known as a name or a label), the object's operation is done by reference. For example, a = [] is an empty list object, and variable A is a reference to that objectExample 1
def Test (c): C.append ("helloWorld"printreturn = []test (list) print (List,id (list)) 
Output [1, 2, ' Hello World '] 2463790879240[1, 2, ' Hello World ' 2463790879240 before executing the test function, the list and parameter C both point to the same object, the test is executed without re-assignment, there is no new pointing procedure, the Append method simply inserts an element into the list object, the object is the original object, but the contents of the object are changed, Because both the parameter C and list are bound on the same object, the C.append and List.append methods are essentially manipulating an object, so the list is changed after the function is called, but the ID does not change, or the original object. Therefore, if a function receives a reference to a mutable object (such as a dictionary or a list), it can modify the original value of the object-the equivalent of passing the object through a "pass reference"Example 2
def"i in test2"print"Hello word  "test2 (str)print(Str,id (str))
Output: I in test2 2885210784112hello word 2885210784048 ID is different, so it is not the same object, that is, we pass the reference. Therefore, if a function receives a reference to an immutable object (such as a number, character, or tuple), it cannot directly modify the original object-the equivalent of passing the object through a "pass value"。Summary:python parameter passing is definitely a way to "pass an object reference". This approach is equivalent to a synthesis of the value of the pass and the reference. If a function receives a reference to a mutable object (such as a dictionary or a list), it can modify the original value of the object-the equivalent of passing the object through a "pass reference". If a function receives a reference to an immutable object (such as a number, character, or tuple), it cannot directly modify the original object-the equivalent of passing the object through a "pass value". Reference: http://www.cnblogs.com/loleina/p/5276918.html

Whether the Python pass is a value or a reference

Related Article

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.