A brief discussion on the use and precautions of variable parameters of custom functions in Python

WBOY
Release: 2016-07-21 14:53:14
Original
1265 people have browsed it

Variable parameters

Python has two types of variable parameters, one is the list type and the other is the dictionary type. The list type is similar to the variable parameter in C and is defined as

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

Copy after login

where args is a tuple.
Variable parameters of dictionary type:

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

where args is a dictionary
You can pass tuple and dictionary to the corresponding variable parameters respectively, the format is as follows

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

Function with default parameters

The parameters with default values ​​​​of the function can be very convenient for us to use: Under normal circumstances, you can omit the parameter passing and use the default value of the parameter, or you can actively pass the parameter; when calling, you don’t need to care about the order of the parameters for convenience, and Direct and explicit; it can even be used as a magic value to perform some logical control.

But since Python’s default value parameter will only be parsed once at the function definition, every time the function is called thereafter, the default value parameter will be this value. It's okay when you encounter some immutable data types such as integers, strings, ancestors, etc., but if you encounter mutable types of data such as arrays, some unexpected things will happen.
Let’s give a simple example to illustrate:

def add_to(num, target=[]):
  target.append(num)
  print id(target), target

add_to(1)
# Output: 39003656, [1]
add_to(2)
# Output: 39003656, [1, 2]
add_to(3)
# Output: 39003656, [1, 2, 3]

Copy after login

Obviously, if you want to get a new array containing the expected results every time you call the function, you will definitely not be able to do so. The parameter target of the function add_to will be assigned to an empty array when the function is parsed for the first time, because it will only be parsed once. Every subsequent call will be based on the target variable and the id value of the variable. Exactly the same. To get the expected results, you can specify a None to represent a null value for this variable data type parameter:

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

In the python world, parameters are passed by identifier (a rough explanation is that they are passed by reference). What you need to worry about is whether the type of the parameter 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 36862704
36862704 36862680
Copy after login

For mutable data types, any changes in the local scope of the function will be retained in the data; for immutable data types, any changes will only be reflected in newly generated local variables, as in the above example Readers can compare the effects shown.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!