In-depth understanding of Python function transfer mechanism

Source: Internet
Author: User
The first thing to say is that Python does not have the same value-based arguments as in C and C + +. Everything in Python is an object, and that's what's great about it, it doesn't have a basic type.

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.

The objects in Python can be divided into one or two categories, which can be changed (mutable) and immutable (Immutale).

The objects belonging to the immutable class are mainly strings,tuples,numbers, while the list, dictionary and other objects belong to the Mutale class object.

Since everything in Python is an object, the way in which the function is transmitted in Python is referenced by reference (which can be understood as a pointer), and I think I don't need to say it, and I believe that we are already familiar with the way the reference is passed.

Add: I just looked at the above content, may be based on the following case, some novice may not quite understand, so I am here to do a two-point personal summary.

Since the functions are all referenced by reference, there is a possibility that if I pass a parameter arg in, but I do not want the parameter Arg in the function after the modification and affect the variable arg outside the function, if you want to achieve this effect is not a bit like the other language of the variable copy, right? But there is no duplicate in Python, but it gives a class of objects (immutable immutable objects, personal estimates, this kind of object is likely to be created for this reason, hehe). Now let's assume that a immutable variable object is passed into the function, and the Imvar is modified within the function, because Imvar is immutable, so when you modify it, the compiler first creates a copy object for it Copy_ Imvar modifies it so that the function is not actually imvar the original object, so its value is still the value before the function is processed. Conversely, if the pass is a mutable type variable, it is modified directly on the reference, so the same object is manipulated inside and outside the function, so the operation within the function directly affects the value of the same variable outside the function. Do not know whether it is better to understand now, if not very clear, combined with the following example to see should understand it.

A test case is given directly below:

#testnum =10string= ' Test ' tupleset= (listset=[9,8,7]def) Change (num,string,tupleset,listset):    num+=1    string+= ' into new words! '    #tupleset. Add ()   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]

See if the result is a deeper understanding of the objects and references in Python?

  • 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.