Python function parameter passing mechanism

Source: Internet
Author: User

First of all, we need to declare that python does not use the parameter passing by value as in C and C ++. Everything in python is an object, which is also its power. It has no basic type.

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.

Objects in python can be divided into one or two categories, namely mutable and immutale ).

Objects of the immutable class mainly include strings, tuples, and numbers, while other objects such as lists and dictionaries belong to the MUTALE class.

Since everything in python is an object, the function passing method in Python is to pass Parameters by reference (it can be understood as passing a pointer). For details, I don't need to talk about it anymore. I believe everyone is familiar with the reference-based parameter passing method.

Supplement: I have carefully read the above content just now. It may be based on the following case, but some new users may not understand it yet. Therefore, I will make two more personal summaries here.

Since all functions are passed by reference, there is a possibility. If I pass in a parameter Arg, however, I do not want this parameter Arg to be modified in the function to affect the variable Arg outside the function. Is it possible to transfer a copy of the variable in other languages to achieve this effect, right? But there is no duplicate in Python, but it provides a class of objects (immutable objects cannot be changed. In my personal estimation, such objects are probably created for this reason, haha ). Now let's assume that we pass in an immutable variable object imvar to the function and modify imvar in the function. Since imvar is an object that cannot be changed, when we modify it, the compiler will first create a copy object copy_imvar for it and then modify it. Therefore, the modified value in the function is not actually the original object imvar, so its value is still the value before function processing. On the contrary, if it is a variable of the variable type, it is directly modified on the reference, so the operation inside and outside the function is the same object, therefore, operations in a function directly affect the values of the same variables outside the function. I don't know if I can understand it well now. If I still don't understand it well, I should understand it with the example below.

The following is a test case:

#testnum=10string='test'tupleset=(1,2,3)listset=[9,8,7]def change(num,string,tupleset,listset):    num+=1    string+=' into new words!'    #tupleset.add(12)   error    tupleset=(12,3,4,4)    listset.append(10000)change(num,string,tupleset,listset)   print num,string,tupleset,listset------------------------------------------------ans:10 test (1, 2, 3) [9, 8, 7, 10000]

 

Do you have a better understanding of the objects and references in Python when you see the results?

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.