Discussion on the use of variable parameters of custom functions in Python and the points for attention

Source: Internet
Author: User
variable Parameters

There are two types of variable parameters for Python, one for the list type and one for the dictionary type. The list type is like a mutable parameter in C, defined in the form

def test_list_param (*args): for  arg in args:    print arg

Where args is a tuple.
Variable parameters for dictionary types:

def test_dict_param (**args):  for K, V in Args.iteritems ():    print K, V

Where args is a dictionary
Each tuple and dictionary can be passed to the corresponding variable parameter, in the following format

A = (1, 2, 3) b = {"A": 1, "B": 2, "msg": "Hello"}test_list_param (*a) Test_dict_param (**b)

Functions with default parameters

The function with the default parameters can be very convenient for us to use: In general, you can omit the parameter using the default value of parameters, you can also take the initiative to pass the parameter, when the call does not care about the order of the parameters convenient to use, and direct, explicit, and even used as a magic value, to do some logical control.

However, since Python's default parameter is parsed only once at the function definition, the default value parameter will be the value each time the function is called. Encountered some immutable data types such as: Integer, String, meta-ancestor, such as OK, but if you encounter mutable type of data such as arrays, there will be some unexpected things happen.
Let's give a simple example to illustrate:

def add_to (num, target=[]):  target.append (num)  print ID (target), targetadd_to (1) # output:39003656, [1]add_to (2) # output:39003656, [1, 2]add_to (3) # output:39003656, [1, 2, 3]

Obviously if you want to get a new array with the desired result every time you call the function, you can't do it. The parameter target of the function add_to is assigned to an empty array when the function is parsed for the first time, because it will be parsed only once, and will be manipulated on the basis of the target variable each time it is called, and the ID value of the variable is exactly the same. To get the expected results, you can specify a none for the parameters of this mutable data type to represent null values:

A = (1, 2, 3) b = {"A": 1, "B": 2, "msg": "Hello"}test_list_param (*a) Test_dict_param (**b)

In Python's world, parameters are passed by identifier (brute-point interpretation is passed by reference), and you need to worry about whether the type of the parameter is mutable:

>>> def Test (param1, param2): ...   Print ID (param1), id (param2)   ... param1 + = 1   ... Param2 + = 1   ... Print ID (param1), id (param2) ...>>> var1 = 1>>> var2 = 2>>> Print ID (var1), id (var2) 36862728 368 62704>>> Test (var1, var2) 36862728 3686270436862704 36862680

Variable data types, any changes in the function's local scope will remain on the data, and any changes to the immutable data types will only be reflected in the newly generated local variables, as shown in the sake above, and the reader can compare them.

  • Related Article

    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.