The replication problem for Python objects, passed by value? Reference delivery?

Source: Internet
Author: User

This part of this blog is very clear, turn around

WINTERTTR (reprint please specify) http://blog.csdn.net/winterttr/article/details/2590741#0-tsina-1-71597-397232819ff9a47a7b7e80a40613cfe1

I think this title may be a problem for many beginners. Especially as I am familiar with the C + +, just enter the Python Hall of Friends

。 How is the transfer of function parameters in C + + a deep-rooted influence of our thinking--reference? Pass the value? What the hell is that?

Hehe, the nature of language determines the method used, so now let's explore Python's function parameter passing mode.

Before we begin, it is necessary to distinguish some of the basic concepts of Python.

The first thing to say is: Variables and objects

In Python, a type belongs to an object, and the variable is of no type, which is the language feature of Python and a point that attracts a lot of pythoner. All variables can be understood as "references" to an object in memory, or it can look like void* in C. So, hopefully, when you see a python variable, separate the variable from the real memory object.

a type belongs to an object, not a variable. in this way, many problems are easy to think about.

For example:

Nfoo = 1 #一个指向int数据类型的nfoo (remind again, Nfoo no type)

Lstfoo = [1] #一个指向list类型的lstFoo, this list contains an integer 1.

Corresponding to the previous concept, you must elicit another concept, which is the "mutable" and the "immutable" (immutable) object .

People familiar with Python should be aware of the fact that in python, strings, tuples, and numbers are immutable objects, while list,dict and so on are objects that can be modified. So what are these so-called changes and immutable influences?

Or the above example:

Nfoo = 2

In this case, the original 1 object in memory cannot be changed, so it is "discarded" and the other Nfoo points to a new int object with a value of 2

Lstfoo[0] = 2

Change the value of the first element in the list, because list is changeable, so the first element is changed to 2, and it should be said that a new int object is assigned to the first value of the object pointed to by Lstfoo, but for Lstfoo, the object to which it is pointing does not change. The object that appears to be the void* variable is still the one that has just the list of an int object. (Listen a little dizzy, just think it over and see, hey)

Well, I'm so spoon. Review the basics of Python and change back to the question, Python's function parameter pass: value? Reference?

For variables (the concept of relative to objects), in fact, the python function parameter passing can be understood as the variable value operation (Note Oh, I'm talking about variables, not object =_=)

Then say the example:

Def changeint (a):

A = ten # change the number

Nfoo = 2

Changeint (Nfoo)

Print Nfoo #结果是2

What happens now, there is an int object 2, and the variable that points to it nfoo, when passed to Changeint, according to the way of passing value, copy the value of the variable Nfoo, so that a is Nfoo point to the same int object, when the function a=10, what happens?

(Remember the concepts I mentioned above), int is an object that cannot be changed, so a new int object is made, and a points to it (but at this point, the object that is pointed to by the variable Nfoo does not change), so the outside feeling is that the function does not change the value of Nfoo, it looks like C + + The method of passing values in the

Def changelist (a):

A[0] = Ten # change the number

Lstfoo = [2]

Changelist (Lstfoo)

Print Nfoo #结果是 [10]

When passed to Changelist, the variable still copied the value of the variable Lstfoo in the way of "pass value", so that a and lstfoo point to the same object, but the list is the object that can be changed, and the operation of A[0] is the operation of the contents of the object that the Lstfoo points to. So, a[0] = 10, that is, changed the Lstfoo point to the first element of the object, so, when the output Lstfoo again, the display [10], the content has been changed, it looks like in C + + by reference to pass.

To add a copy problem, copy it, and give the mutable variable a pointer to another object:

Copy problem for list: 1,
1 >>> a  2 [1, 2]  3 >>> b=a[:]  4 >> ;> b  5 [1, 2]  6 >>> b[0]=20 7 >>> b 8 [2]  9 >>> a [1, 2]

2,
1 Import Copy 2 >>> c=copy.copy (a)  3 >>> C  4 [1, 2]  5 >>> c[1]=30 6 >>> C  7 [1,]  8 >>& Gt a 9 [1, 2] >>> b [20, 2]

Dictionary

1 >>> a=[(‘He', 1), (‘Wo‘,‘Jia‘)]2 >>> p=Dict (a)3 >>>P4 {‘Wo‘:‘Jia‘,‘He': 1}5 >>> b=P.copy ()6 >>>B7 {‘Wo‘:‘Jia‘,‘He': 1}8 >>> b[‘Wo']=‘Ja‘9 >>> B10 { wo ":  ja ',  ' he ": 1}11 >>> P12 {  ' wo ":  ' 

The replication problem for Python objects, passed by value? Reference delivery?

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.