Details about whether to pass a value or reference to a python function, and details about python

Source: Internet
Author: User

Details about whether to pass a value or reference to a python function, and details about python

First, let's talk about the function parameter transfer mechanism in the popular science environment. What does it mean to pass the value and reference?

In essence, the function parameter transfer mechanism is a problem where the call function (process) and the called function (process) communicate when the call occurs. There are two basic parameter transfer mechanisms: value transfer and reference transfer.

In the passl-by-value process, the formal parameters of the called function are processed as local variables of the called function, that is, the memory space is opened in the stack to store the values of the real parameters put in by the main function, thus becoming a copy of the real parameters. The feature of value transfer is that any operation of the form parameter of the called function is performed as a local variable, without affecting the value of the real parameter variable of the main function.

During the pass-by-reference process, the form parameter of the called function opens up the memory space in the stack as a local variable, however, the address of the Real Variable put in by the main function is stored. Any operation of the called function on the form parameter is processed as indirect addressing. That is, the address stored in the stack is used to access the real variable in the main function. Because of this, any operation performed by the called function on the form parameter affects the real parameter variables in the main function.

What is actually in python?

Let's take a look at a simple example:

from ctypes import *import os.path import sysdef test(c):  print "test before "  print id(c)  c+=2  print "test after +"  print id(c)  return cdef printIt(t):  for i in range(len(t)):    print t[i]if __name__=="__main__":  a=2  print "main before invoke test"  print id(a)  n=test(a)  print "main afterf invoke test"  print a  print id(a)

The result is as follows:

>>> main before invoke test39601564test before 39601564test after +39601540main afterf invoke test239601564

The id function obtains the memory address of the object. obviously, from the above example, we can see that the variable is passed to the test function as a parameter, a reference of a is passed, and the address of a is passed, therefore, the address of variable C obtained in the function is the same as that of variable a. However, in the function, the value of C is changed from 2 to 4, in fact, the memory space occupied by 2 and 4 still exists. After the value assignment operation, C points to the memory occupied by 4. And a still points to the memory of 2, so the value of a is still 2 after printing.

If you still cannot understand it, let's take a look at the following example.

>>> a=1>>> b=1>>> id(a)40650152>>> id(b)40650152>>> a=2>>> id(a)40650140

Both a and B are int-type values. The values are both 1 and the memory address is the same. This indicates that in python, there can be multiple references pointing to the same memory (drawing a very frustrating figure, forgive me). After assigning a value to 2, check the memory address of a again, all of which have changed.

Based on the preceding example, we can describe it like this:

So the python function transmits parameters as references? Then the value of the passed parameter is modified in the called function, and does not affect the value of the real parameter variable of the main function? Let's look at an example.

from ctypes import *import os.path import sysdef test(list2):  print "test before "  print id(list2)  list2[1]=30  print "test after +"  print id(list2)  return list2def printIt(t):  for i in range(len(t)):    print t[i]if __name__=="__main__":  list1=["loleina",25,'female']  print "main before invoke test"  print id(list1)  list3=test(list1)  print "main afterf invoke test"  print list1  print id(list1)

The actual value is:

>>> main before invoke test64129944test before 64129944test after +64129944main afterf invoke test['loleina', 30, 'female']64129944

I found that the same value was passed, and the second variable actually changed. Why?

In fact, it is because of the sequence in python: the list is a variable object, and the memory address of list1 is viewed based on list1 = [1, 2] list1 [0] = [0, is the same.

>>> list1=[1,2]>>> id(list1)64185208>>> list1[0]=[0]>>> list1[[0], 2]>>> id(list1)64185208

Conclusion:Python does not allow programmers to choose whether to pass values or pass references. Python parameters must be passed in the "passing object reference" method. This method is equivalent to a combination of passing values and passing references. If the function receives a reference to a variable object (such as a dictionary or list), it can modify the original value of the object, which is equivalent to passing the object through "passing reference. If the function receives a reference to an immutable object (such as a number, character, or tuples), it cannot directly modify the original object-it is equivalent to passing the object by "passing the value.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.