Differences in Python function parameters

Source: Internet
Author: User
In the process of using php.cn/wiki/1514.html "target=" _blank ">python, it is found that when the function parameter is list, the call to List.append () inside the function changes the formal parameter, which is not the same as that of C + +. Check the relevant information and record it here.

The ID in Python can get the memory address of the object


>>> NUM1 = 10>>> num2 = num1>>> num3 = 10>>> ID (NUM1) 4299190320>>> ID (num2) 4 299190320>>> ID (num3) 4299190320


You can see that num1, Num2, num3 three objects point to an address, Python here is a method called reference counting to complete, very similar to the C++zhong smart pointer, variable assignment to the variable equivalent to the same object reference counter +1, rather than reallocate space.

You can see the following results for the list object:


>>> List1 = [,>>> List2 = [,>>> >>> 4302498128


List1 and List3 point to the same space, List2 points to an additional address.

function parameters in Python are object passing, and there are local and global problems, and there are two rules in the process of parameter transfer:

    • Copying a parameter to a local scope object by reference means that the variable being used to access the function argument is independent of the object that is raised to the function because there is a replication problem, which is the same as the C language. and modifying a local object does not alter the original data.

    • Mutable objects can be modified in the appropriate location. mutable objects are mostly lists and dictionaries, which is essentially a modification of the local sub-object that was previously parsed without altering the ID of the Dictionary object or List object


def incrint (num):    pId (num)    num + = 1;    PID (NUM) def incrlist (listarg):    pid (Listarg)    listarg.append (1);    PID (LISTARG) def PID (ARG):    print ID (arg) NUM1 = 10pId (NUM1) incrint (NUM1) print (NUM1) List1 = [0,2]pid (list1) incrlist (list1) print (List1)


The results are as follows:


Python test.py42991819044299181904429918188010433697991243369799124336979912[0, 2, 1]


You can see that the value of int is changed inside the function, then num points to another memory address, and the list is modified by the same memory address.

In Python, objects can be divided into variable (mutable) and immutable (immutable) types, tuples (tuple), numeric (number), string (strings) are immutable objects, and word typical (dictionary) and lists (list ) object is a Mutable object.

So be careful in the process of transmitting the parameters.

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.