Python what is a value or reference function parameter

Source: Internet
Author: User

This article is for Python to encounter questionable features.

The following paragraph is the original basic tutorial Python function.

passing parameters by value and passing parameters by reference

All parameters (arguments) are passed by reference in Python. Assuming that you have changed the parameters in the function, the original parameter is changed in the function that called the function. Like what:

#!/usr/bin/python # writable function Description def changeme (mylist):   "Change incoming list"   mylist.append ([1,2,3,4]);   Print "Function inside value:", MyList   return # call changeme function mylist = [10,20,30];changeme (mylist);p rint "function outside the value:", MyList

The same reference is used for incoming functions and objects that add new content at the end. Therefore, the output results are as follows:

Values in function:  [10, 20, 30, [1, 2, 3, 4]] values outside the function:  [10, 20, 30, [1, 2, 3, 4]]


good. See here. Try it Yourself, code:

def printme (AGE,STR):    str = ' str changed! '    Print age,str    returnstr = ' ori str ' age = 23printme (age,str) Print str
Output Result:

23°c str Changed!ori STR

It seems to be wrong! Does not say that the function changes the value of the parameter, then the actual number of parameters will change?!

Is it not equal to strings and lists, etc.?
experienced people know that in Python, strings, tuples, and numbers are immutable objects, while list,dict are objects that can be changed.


So, remember the words in red, when you want to change an immutable object. In fact, it opens up a new object's storage space, which is why there is a global scope and a local scope for the problem.

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Python what is a value or reference function parameter

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.