Python function parameter change problem

Source: Internet
Author: User

The problem of changing the parameters of Python function

A few days ago in the process of doing the project found a problem, into the function of a list, in the function body to assign it to list, the List1 operation after the discovery list has changed, Ah! Unexpected. Check for a reason, the original Python has a variable object and immutable object points. Only when an immutable object is passed in does the value not change, and if mutable, it should be noted when acting as a function parameter.

Immutable object: Number, String, Tuple,bool
mutable objects: List, Set, dictionary are elements that can be changed inside

Here's a summary:

First look at the example:

def changestr (str):    str = "inside"    print "这是function中 , 值为:",strmystr = "outside"changestr(mystr)print "这是函数外边 , 值为:",mystr

Output Result:

这是function中 , 值为: inside这是函数外边 , 值为: outside

That is, the immutable object string is passed in, and its operation within the function does not affect the value of the string after the call ends, that is, no change occurs.

Ps:number and tuple results are the same, and these three types can only be re-assigned to change the value of an object.

def changestr (str):    str.append(3)    print "这是function中 , 值为:",strmystr = [1,2]changestr(mystr)print "这是函数外边 , 值为:",mystr

Results:

这是function中 , 值为: [1, 2, 3]这是函数外边 , 值为: [1, 2, 3]
    • For mutable objects, changes in the body of the function, the value of the object itself has changed, outside the function, the contents of the list is still changed, this is the result can be guessed beforehand, because the parameters in Python, the input is a copy of the variable reference, and the variable points to the same value.

3

def change2(list):    list = [1,2,3,4]mylist = ["aa",21]print(mylist)change2(mylist)print(mylist)

Output Result:

[‘aa‘, 21][‘aa‘, 21]

The re-assignment of mutable objects in the body of a function does not affect the value of the external variable, but it is reasonable to think carefully.

    • That is, the variable is stored in a reference, is the memory address of the real content (of course, the eight basic data types in Java, variable names and values are stored in the stack), the variable is the equivalent of modifying the memory address of the variable copy storage, and then the variable is not the same as the variables outside the function, the letter Variables outside of the number, still store the original memory address, its value has not changed naturally.

3

def change2(list):    list1 =list    list1.append(34)mylist = ["aa",21]print mylistchange2(mylist)print mylist

Output Result:

[‘aa‘, 21][‘aa‘, 21, 34]
    • The function body passes in the parameter, which is a copy of the function's out-of-body variable reference.
    • Changes the value in the heap that the variable points to in the function body, and is valid for the out-of-function variable.
    • Changing the reference of a variable in the body of a function that is not valid for an out-of-function variable

If you want to not change yuanlist, use Copy.deepcopy ()

import copydef change2(list):    list1=copy.deepcopy(list)    list1.append(34)mylist = ["aa",21]print mylistchange2(mylist)print mylist

Results:

[‘aa‘, 21][‘aa‘, 21]

Python function parameter change problem

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.