Python function parameter Reference (value/pass)/copy/deepcopy

Source: Internet
Author: User

Lite version:

Transfer value: Local variable of the function is changed without affecting the local variables of the keynote function

Address: Local variable of the modulated function will affect local variables of the keynote function

Python parameter passing method: Pass the object reference (the mixed method of the value and the address), if it is a number, string, the tuple is passed the value, if it is a list, the dictionary is transmitted;

Copy usage scenario: List or dictionary with inner elements as numbers, strings or tuples

Deepcopy using a scene: A list or dictionary, and an inner element that contains a list or dictionary

Full version:

1. The difference between the value of the transmission and the address

Passing a value is the value of passing in a parameter, which is the address of a parameter, that is, the address of the memory (equivalent to a pointer). Their difference is that if the function in the face of the passed parameter re-assignment, the global variable outside the function is changed, the parameter passed by the value will not be changed, the pass-through will change.

1 a=12def f (b):3     b=24F (a)5 Print a

For example, in this code, first, the value of a is 1, the A as a parameter into the function F, the function f in the re-assignment of B to 2, if it is passed the form of a value of a, the value of a will not change, still 1, if in the form of a communication (but this is not the programmer can determine) the incoming a,a will become 2. This is the difference between the value of the transmission and the address.

2. What is the URL and the value of the transfer in Python?

Python does not allow programmers to choose whether to use a value or a pass-through. Python parameter passing is definitely a way to " pass an object reference ". In fact, this approach is equivalent to a synthesis of the value of the transmission and the address.

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 the address. 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 a pass value.

So python's values and addresses are selected based on the type of parameters passed in.

parameter types for values: numbers, strings, tuples

Type of parameter to address: list, dictionary

1 a=12def  F (a):3     a+=14F (a)5Print A

In this code, because a is a numeric type, so is the way to pass a value, the value of a does not change, the output is 1

1 a=[1]2def  F (a):3     a[0]+=14 F (a) 5 Print A

In this code, because the type of a is a list, so it is the form of the address, the value of a[0] will change, the output is [2]

3. Copy and Deepcopy

More than just functions, references outside the function also follow this rule:

1 a=12 b=a3 a=24print a b5 a=[1] 6 b=a7 a[0]=28print a b

The first output is 2, 1, and the second output is [2] [2]

B=a

So in Python, when running the above code, if A is a dictionary or a list, the program does not create a new B variable, then the value of a is copied to B, but a new B variable, the value of B points to a, which is equivalent to the C language in a new pointer to a.
So when the value of a changes, the value of B changes accordingly.

But what do we do when we want to create a B variable that is equal to the value of a, while the value of B is not associated with the value of a? This is where copy and deepcopy are used.

1 Import Copy 2 3 a=[1,2,3]  4 b=a  5 a.append (4)  6Print A, b 7 8 a=[1,2,3]  9 b=copy.copy (a) a.append (4)  Print A, a, b

The above output is:

1 [1, 2, 3, 4] [1, 2, 3, 4]2 [1, 2, 3, 4] [1, 2, 3]

Copy is used here to make B equal to a, and if the value of a is modified, the value of B does not change. It seems that copy has been able to implement the above mentioned requirements, then what is the use of deepcopy?

If we're in this situation, copy's not going to work.

1 a=[1,[1,2],3]2 b=copy.copy (a)3 a[1].append (4)4 print A, b

The results are as follows: [1, [1, 2, 4], 3] [1, [1, 2, 4], 3], the result is obviously not what we want.

When the value in the list or dictionary parameter is a list or dictionary, copy does not copy the list or dictionary in the argument, and the deepcopy is used.

1 a=[1,[1,2],3]2 b=copy.deepcopy (a)3 a[1].append (4)4  Print A, b

The results of the output are: [1, [1, 2, 4], 3] [1, [1, 2], 3]

Reference: http://www.cnblogs.com/Xjng/p/3829368.html

Python function parameter Reference (value/pass)/copy/deepcopy

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.