Differences between python function parameters and python Functions

Source: Internet
Author: User

Differences between python function parameters and python Functions

When using python, it is found that when the function parameter is list, the function calls list. append () will change the shape parameter, which is not the same as C/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)4299190320>>> id(num3)4299190320

We can see that the num1, num2, and num3 objects point to an address. python uses a reference counting method here, similar to the smart pointer of C ++ zhong, the variable assigned to the variable is equivalent to referencing counter + 1 of the same object, rather than allocating space again.

The following result is displayed for the list object:

>>> list1 = [0,1]>>> list2 = [0,1]>>> id(list1)4302498128>>> id(list2)4302498056
>>> list3 = list1
>>>id(list3)
4302498128

List1 and list3 point to the same space, and list2 points to another address.

In python, parameter passing through functions is an object, and there are also local and global problems. There are two rules during parameter passing:

  • Copying a parameter to an object with a local scope means that the variable used to access the function parameter is irrelevant to the object raised to the function. Because of a replication problem, this is the same as that in C language. In addition, modifying a local object does not change the original data.
  • You can modify a variable object at an appropriate location. The variable object mainly includes the list and dictionary. The proper position is essentially the modification of the local sub-object analyzed earlier. It does not change 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 result is as follows:

 python test.py42991819044299181904429918188010433697991243369799124336979912[0, 2, 1]

You can see that inside the function, if the int value is changed, num points to another memory address, while modifying the list is the same memory address.

In python, objects can be divided into mutable and immutable types. tuple, number, and string are immutable objects, the dictionary and list objects are variable objects.

Therefore, pay attention to the parameter passing process.

Reference: http://blog.chinaunix.net/uid-20937170-id-3275808.html

 

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.