Pass Python function parameters: Pass the value? Reference?

Source: Internet
Author: User

Http://blog.csdn.net/winterttr/article/details/2590741

Author: winterttr (reprinted, please note)

I think this title may be a problem for beginners. Especially those who are familiar with C/C ++ like me, just entering the python Hall

. The method of passing C/C ++ function parameters is deeply rooted in our thinking-reference? Pass value? What is it.

The features of the language determine the method used. Now let's look at the python function parameter passing method.

Before we start, we need to clarify some basic concepts of Python.

The first thing to say is: variables and objects

In python, the type is an object, and the variable has no type. This is exactly the language feature of Python and attracts many pythoner. All variables can be understood as the "reference" of an object in the memory, or they can also look like void * in C. Therefore, we hope that when you see a python variable, you can separate the variable from the real memory object.

The type belongs to the object, not the variable. In this way, many questions are easy to think about.

For example:

Nfoo = 1 # A nfoo pointing to the int data type (again, nfoo has no type)

Lstfoo = [1] # An lstfoo pointing to the list type. This list contains an integer of 1.

Corresponding to the previous concept, another concept must be introduced. This is the mutable and immutable objects.

People familiar with Python should understand this fact. In python, strings, tuples, and numbers are unchangeable objects, while list and dict are modifiable objects. So what are the impacts of these so-called changeable and unchangeable changes?

Or the above example:

Nfoo = 2

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

Lstfoo [0] = 2

Change the value of the first element in the list. Because list can be changed, the first element is changed to 2, in fact, it should be said that there is a new int object that is specified to the first value of the object pointed to by lstfoo, but for lstfoo, the object pointed to has not changed, that is, the object pointed to by this seemingly void * variable is still the list with an int object just now. (Look a little dizzy. You'll understand it after careful consideration, hey)

Well, I have reviewed the basic knowledge of Python like this and changed it back to the question. The Python function parameter is passed: Pass the value? Reference?

For variables (concepts relative to objects), in fact, passing Python function parameters can be understood as passing values through variables (note that I am talking about variables, not objects = _ =)

The example is as follows:

Def changeint ():

A = 10 # change the number

Nfoo = 2

Changeint (nfoo)

Print nfoo # The result is 2.

What happened then, there is an int object 2, and the nfoo variable pointing to it. When it is passed to changeint, The nfoo value of the variable is copied in the way of passing the value. In this way, A means that nfoo points to the same int object. What happens when a = 10 in the function?

(Do you still 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 time, the object pointed to by the variable nfoo has not changed). Therefore, the function does not change the value of nfoo. It looks like the value passing method in C ++.

Def changelist ():

A [0] = 10 # change the number

Lstfoo = [2]

Changelist (lstfoo)

Print nfoo # The result is [10].

When passed to changelist, the variable still copies the value of the variable lstfoo in the "pass value" mode, so a and lstfoo point to the same object. However, list is an object that can be changed. The operation on a [0] is the operation on the content of the object pointed to by lstfoo. Therefore, a [0] = 10, this is to change the first element of the object to which lstfoo points. Therefore, when lstfoo is output again, [10] is displayed, and the content is changed. It looks like passing by reference in C ++.

Well, do you have a deeper understanding of the concepts of variables and objects in Python?

Through my above explanation, I think you can solve the transfer problem of other types of objects by yourself.

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.