Python's variable passing

Source: Internet
Author: User

Variables in Python are treated as references to objects. When a Python function call passes a parameter, the programmer is not allowed to choose whether to pass a value or to pass a reference, and the Python parameter is passed in the way of "object reference".
This is equivalent to the combination of a value and a reference, and if the function receives a reference to a mutable object (such as a dictionary or a list), it can modify the original value of the object-the equivalent of passing the object through a "pass-through", if the function receives an immutable object (such as a number, string, or tuple), you cannot directly modify the original object-the equivalent of a "pass value" to pass the object.  

def Func (x):    x = 20a = 10Func (a) print (a) #输出10, when Func (a) is called, the x variable inside the func points to an integer object 10,# (that is, a reference to integer object 10). In the Func internal attempt to modify an immutable object, #会使得Func内部的x指向一个新的对象20, while the external variable a still points to the immutable object 10.def FUNC2 (x):    x[0] = 20a = [1,2,3]func2[a]print (a) #结果为 [20, 2, 3].


Python General internal assignment variables, is a reference variable, and the C language is similar to the concept of the transfer address. The memory address of X can be queried by ID (x).
If a=b, the address of A and B is the same; if you just want to copy it, use a = b[:]

Python's variable passing

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.