Go Value passing and reference passing of functions in Python

Source: Internet
Author: User

First of all, the function parameter transfer mechanism should be popular , what does the value and the reference mean?

The problem of function parameter passing mechanism is essentially the method that calls the function (procedure) and the called function (procedure) to communicate when the call occurs. There are two basic parameter passing mechanisms: value passing and reference passing.

In the process of value passing (passl-by-value), the formal parameters of the called function are treated as local variables of the called function, that is, the memory space is opened up in the stack to hold the value of the arguments put in by the key function, thus becoming a copy of the argument. The characteristic of value passing is that any operation of the function on the formal parameter is done as a local variable, without affecting the value of the argument variable of the main key function.

In the process of reference passing (pass-by-reference), the formal parameters of the called function, while also opening up memory space in the stack as local variables, are stored in the address of the argument variable that is put in by the main key function. Any operation of the modulated function on the formal parameter is handled as an indirection, that is, the argument variable in the keynote function is accessed through the address stored in the stack. Because of this, any action taken by the modulated function on the parameter affects the argument variables in the keynote function.

What is actually going on in python?

Let's look at a simple example:

 fromcTYPESImport*ImportOs.pathImportSYSdefTest (c):Print "Test before"    PrintID (c) C+=2Print "Test after +"    PrintID (c)returnCdefPrintIt (t): forIinchRange (len (t)):PrintT[i]if __name__=="__main__": A=2Print "main before invoke test"    PrintID (a) n=Test (a)Print "main Afterf Invoke test"    PrintaPrintID (a)
View Code

After the run, the results are as follows:

>>>
Main before Invoke test
Test before
Test after +
Main Afterf Invoke Test
39601564

The ID function can get the memory address of the object. It is obvious from the above example that the a variable is passed as a parameter to the test function, passing a reference to a, the address of a is passed, so the address of the variable C obtained in the function is the same as the address of the variable a, but within the function, the C is assigned to the operation, the value of C from 2 to 4 , in fact, 2 and 4 of the memory space is still present, after the assignment operation, C points to the memory of 4. And a still points to 2 of the memory, so after printing a, its value is still 2.

If you don't understand it, let's look at the following example

>>> a=1>>> b=1>>> ID (a)>>> ID (b)>>> a=2>>> ID (a)

Both A and B are of type int, the value is 1, and the memory address is the same, which already indicates that in Python, there can be multiple references to the same memory (drawing a very lame picture, forgive me), after assigning a value of 2, see the memory address of a again, has changed

And based on the first example, it might be possible to describe:

What is a Python function that is passed by reference? Then the value of the argument is modified within the function and does not affect the value of the actual argument variable of the keynote function. Let's look at an example.

 fromcTYPESImport*ImportOs.pathImportSYSdefTest (LIST2):Print "Test before"    PrintID (list2) list2[1]=30Print "Test after +"    PrintID (list2)returnList2defPrintIt (t): forIinchRange (len (t)):PrintT[i]if __name__=="__main__": List1=["Loleina", 25,'female']    Print "main before invoke test"    PrintID (list1) list3=Test (List1)Print "main Afterf Invoke test"    PrintList1PrintID (list1)
View Code

The actual values are:

>>>
Main before Invoke test
Test before
Test after +
Main Afterf Invoke Test
[' Loleina ', +, ' female ']

Find the same value, and the second variable actually changes, why?

In fact, because of the sequence in Python: The list is a Mutable object, based on list1=[1,2] list1[0]=[0] so the front and back of the view List1 memory address, is the same.

>>> list1=[1,2]>>> ID (list1)>>> list1[0]=[0]>>>  2]>>> ID (list1)

conclusion:Python does not allow programmers to choose whether to use a value or to pass a reference. 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".

Transferred from: http://www.cnblogs.com/loleina/p/5276918.html

Go Value passing and reference passing of functions in Python

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.