Discuss the usage and notes of variable parameters of custom functions in Python

Source: Internet
Author: User
The default value parameter of a Python function will only be parsed once in the function definition. the default value will be the same for later use. this will cause confusion when used together with variable parameters, the following describes how to use the variable parameters of custom functions in Python and points of attention. Variable parameters

Python has two types of variable parameters: list and dictionary. The list type is similar to the variable parameter in C. It is defined

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

Args is a tuple.
Variable parameters of the dictionary type:

def test_dict_param(**args) :  for k, v in args.iteritems() :    print k, v

Args is a dictionary.
Tuple and dictionary can be passed to the corresponding variable parameters, 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 parameter with default values of a function can be used to a large extent: Generally, the default values of parameters can be omitted or automatically transmitted; during the call, you do not need to worry about the order of parameters for easy use, and can directly and explicitly use them as magic values for logical control.

However, because the default value of python is parsed only once in the function definition, the default value will be used for each function call. Some immutable data types, such as integer, string, and ancestor, are good. However, if a variable type of data, such as an array, some unexpected things may occur.
Let's take 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 containing the expected results every time you call a function, you certainly cannot. The target parameter of the add_to function will be assigned an empty array when the function is parsed for the first time, because it will only be parsed once, in the future, each call will be performed based on the target variable, and the id value of the variable will be the same. To get the expected results, you can specify None for this variable data type parameter to indicate a null value:

a = (1, 2, 3)b = {"a":1, "b":2, "msg":"hello"}test_list_param(*a)test_dict_param(**b)

In the world of python, parameters are passed by identifiers (roughly interpreted as passing by reference). you need to worry about whether the parameter type is variable:

>>> 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 36862704>>> test(var1, var2)36862728 3686270436862704 36862680

For a variable data type, any changes in the local scope of the function will be stored in the data. for an immutable data type, any changes will only be reflected in the newly generated local variables, as shown in the preceding columns, you can compare them.

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.