Python function Parameter problems

Source: Internet
Author: User

Is the delivery of Python a value pass or a reference pass?

Before you answer this question, you need to know that a variable in Python is simply a reference to an object. The assignment does not change what the object points to, but instead changes the variable from one object's reference to the reference to another object. After copying a variable, the ID of the variable before and after the copy changes. In the case of variable direct operation, the variable object (list dict ... ) and the immutable object (int str tuple ... )。 The former does not change, while the latter changes, because the original cannot be changed, only a new value is returned and assigned to the variable.

In [1]: i = 1In [2]: ID (i) out[2]: 31122120LIn [3]: i = 5In [4]: ID (i) out[4]: 31122024LIn [5]: a = [A.]in [6]: ID (a) out[6]: 62749960LIn [7]: a = [23]in [8]: ID (a) out[8]: 62757576LIn [9]: i = 5In [10]: ID (i) out[Ten]: 31122024LIn [ALL]: i + = 1In [12]: ID (i) out[]: 31122000LIn [13]: ID (a) out[]: 62757576LIn [+]: A.append (5) in [15]: ID (a) out[]: 62757576L

An assignment cannot change an object that refers to an address within a function or outside a function. So a python function is passed by reference, passing a reference to the object. As for whether or not to modify this parameter, it depends on the operation of the variable and whether the variable is mutable or immutable.

deff (n,x): N= 3X.append (7)    #x = [4,5,6]    Print "In F ()", N,xif __name__=="__main__": N= 1x= [0,1,2]    Print "Before:", N,x F (n,x)Print "After :", N,x>>>Before:1 [0, 1, 2]in F ()3 [0, 1, 2, 7]after:1 [0, 1, 2, 7]deff (n,x): N+ = 3X.append (7)    #x = [4,5,6]    Print "In F ()", N,xif __name__=="__main__": N= 1x= [0,1,2]    Print "Before:", N,x F (n,x)Print "After :", N,xbefore:1 [0, 1, 2]in F ()4 [0, 1, 2, 7]after:1 [0, 1, 2, 7]deff (n,x): N= 3#X.append (7)x = [4,5,6]    Print "In F ()", N,xif __name__=="__main__": N= 1x= [0,1,2]    Print "Before:", N,x F (n,x)Print "After :", N,xbefore:1 [0, 1, 2]in F ()3 [4, 5, 6]after:1 [0, 1, 2]

Python function Parameter problems

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.