In python, whether the function transfer parameter is value transfer or reference transfer, and then python

Source: Internet
Author: User

In python, whether the function transfer parameter is value transfer or reference transfer, and then python

At present, most blogs on the internet come to the following conclusions:

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. In fact, 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.

You can find the above in many blogs discussing this issue.

However, in practice, I found a problem:

l=[1,2,3]def a(x):    x=x+[4]a(l)print(l)

The output of this Code is:

[1,2,3]

Why is that? list is a mutable object. According to the above conclusion, the transfer mode is reference transfer. I should be able to modify it in the function? Shouldn't I output [1, 2, 3, 4?

I think most of the conclusions I have cited above are actually not easy to understand, and I have not talked about the essence.

After many tests, I have come to the following conclusions:

In fact, it is meaningless to discuss value transfer or reference transfer in python. To really explain these situations, we should understand python (for mutable objects and immutable objects) how to allocate memory addresses during the assignment process.

Next, we will not discuss the value transfer and reference transfer issues.

Let's do a very simple small experiment, where id () can view the address of the variable in the memory:

l1=[1,2,3]l2=[1,2,3]a=1b=1print(id(l1))print(id(l2))print(id(a))print(id(b))

Running results on my computer:

128565945041285691508016436433441643643344

It can be found that for a mutable Object list, python will assign them different addresses even if the list content is the same.

However, for an immutable object int, there is only one 1 in the memory. Even if another variable c = 1 is defined, it also points to the same 1 in memory. In other words, the address of immutable object 1 isShare.

Next, let's take a look at the situation where variable objects and immutable objects are called in functions and their values are modified.

 

For immutable object int, let's look at the simplest situation:

a=1print(id(a))def x(a):    print(id(a))    b=a    print(id(b))x(a)

Run the following command:

164364334416436433441643643344

This seems to be a reference transfer. a and B outside the function point to the same address.

But let's look at an extreme situation:

 

a=1print(id(a))def x():    b=1    print(id(b))x()

Run the following command:

16436433441643643344

It's amazing, isn't it? The a defined outside the function has nothing to do with the B defined in the function, but they point to the same address!

So how do you determine whether it is a value transfer or a reference transfer? It is meaningless to discuss this problem because there is only one memory. When I pass the value 1 to a variable in the function, I actually pass the address because there is only one 1 in the memory.

Even if I assign a value of 1 to B in the function, a outside the function and B in the function can point to the same address.

Let's take a look at the situation of passing the variable Object list:

l=[1,2,3]print(id(l))def a(x):    print(id(x))    x.pop()    print(x)    print(id(x))    x=x+[3]    print(x)    print(id(x))a(l)

Run to get

883142451528883142451528[1, 2]883142451528[1, 2, 3]883142588232

As you can see, when we pass the list L outside the function to the function, the address of x is the same as that of L, which seems to be a reference transfer, no problem.

Continue, after we call the pop method of x itself, x becomes [1, 2], and the address of x remains unchanged, which is no problem.

However, after we assign a value to x, the address of x changes.

That is to say, python allocates a new address as long as a new variable object is created. Even if the new mutable object we created is exactly the same as the existing mutable object, python still allocates a new address (see the 'very simple lab 'in the upper part of this Article ')

Pop does not create a new variable object. pop modifies an existing variable object.

So it can be summarized:

In python, immutable objects are shared, and creating a mutable object always allocates a new address.

 

At this time, we will go back and think about the value transfer and reference transfer issues, and we will find it meaningless to discuss this in python.

We can say:Python has its own set of special parameter passing methods, which are determined by the nature of the python Dynamic Language.

 

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.