python variable length parameters * argc, ** kargcs (19)

In  a statement and call python functions  , we use a simple understanding of the relevant function, but when the function parameter passing, we planted a pit, about passing a variable length parameters we have not spoken today, this article is mainly explain the problem.

Out to mix sooner or later have to repay

 

A. Function variable length parameter syntax

Comprising a variable-length argument function * args and ** kwargs , complete the following examples:

1

2

3

4

5

def function_print (* args, ** kwargs): # pass variable length parameter, i.e. the number of parameters is not fixed
    print(args)
    print(kwargs)
 
function_print()


Output:

1

2

()

{}

Code analysis: The results can be seen from the output, the first parameter is a tuple * args tuple type, the second parameter is a dictionary dict ** kwargs type.

 

1. * args use:

. a parameter  * args  type tuple tuple, the parameters passed when calling function whether an external BOOL value or integer or string str, tuple data are actually transmitted;

. B if the function parameter is a variable length parameter, a plurality of external call transfer function parameters, arguments match the default order parameter, as all the remaining parameters (component) variable length parameter transfer;

. C If no arguments are passed to variable length parameter of the function, the default is the null set ();

Sample code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

Ordinary parameter #
def function_print1(arg):
    print ( "Common parameter: arg =", arg)
 
# Variable length parameter
def function_print2(*args):
    print ( "variable length parameter: args =", args)
 
+ # Ordinary variable length parameter parameter
def function_print3(arg,*args):
    print ( "Normal + parameter variable length parameter: arg =", arg)
    print ( "Normal + parameter variable length parameter: args =", args)
 
function_print1(False)
function_print1("hello world")
print("***"*20)
 
function_print2(False)
function_print2("hello world")
print("***"*20)
 
function_print3 (False) # only as a function of a parameter passed to form a reference variable length parameter defaults to an empty tuple ()
function_print3("hello world")
print("***"*20)
 
# If the function parameter is a variable length parameter, when a plurality of external call transfer function parameters, in order to match the default parameter, all of the remaining parameters passed as a variable length argument
function_print3(False,1,23,4,5) 
function_print3("hello world",False,0,True,"python教程")


Output:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15


Common parameter: arg = False
Common parameter: arg = hello world
************************************************************
Variable length parameter: args = (False,)
Variable length parameter: args = ( 'hello world',)
************************************************************
+ Common parameter variable length parameter: arg = False
+ Common parameter variable length parameter: args = ()
普通形参 + 不定长形参 : arg= hello world
普通形参 + 不定长形参 : args= ()
************************************************************
普通形参 + 不定长形参 : arg= False
普通形参 + 不定长形参 : args= (1, 23, 4, 5)
普通形参 + 不定长形参 : arg= hello world
普通形参 + 不定长形参 : args= (False, 0, True, 'python教程')


 


2.**kwargs的使用方法

a.形参 **kwargs 类型是字典dict,函数外部调用函数传递参数时需要使用关键字参数,实参写法:a=1,b=2,c=False,d=”hello”;

b.如果函数形参是不定长参数,外部调用函数传递多个参数时,默认按顺序实参匹配形参,关键字参数全部作为(字典)不定长参数传递;

c.如果没有为函数的不定长参数传递参数,默认为空字典{};

示例代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

#普通函数
def function_print1(arg):
    print("普通函数形参 : arg=",arg)
 
#普通函数不定长形参
def function_print2(**kwargs):
    print("不定长形参 : args=",kwargs)
 
#普通函数形参 + 不定长形参
def function_print3(arg,**kwargs):
    print("普通函数形参 + 不定长形参 : arg=",arg)
    print("普通函数形参 + 不定长形参 : args=",kwargs)
 
function_print1(False)
function_print1("hello world")
print("***"*20)
 
function_print2(a=False)
function_print2(c="hello world")
print("***"*20)
 
function_print3(False)
function_print3("hello world")
print("***"*20)
 
function_print3(False,a=1,b=23,h=4,v=5)
function_print3("hello world",y=False,i=0,a=True,j="python教程")


输出结果:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

普通函数形参 : arg= False
普通函数形参 : arg= hello world
************************************************************
不定长形参 : args= {'a': False}
不定长形参 : args= {'c': 'hello world'}
************************************************************
普通函数形参 + 不定长形参 : arg= False
普通函数形参 + 不定长形参 : args= {}
普通函数形参 + 不定长形参 : arg= hello world
普通函数形参 + 不定长形参 : args= {}
************************************************************
普通函数形参 + 不定长形参 : arg= False
普通函数形参 + 不定长形参 : args= {'a': 1, 'b': 23, 'h': 4, 'v': 5}
普通函数形参 + 不定长形参 : arg= hello world
普通函数形参 + 不定长形参 : args= {'y': False, 'i': 0, 'a': True, 'j': 'python教程'}


 

3.函数不定长参数*args和**kwargs只能放在形参的末尾,顺序不能错.

1

2

def function_print(arg1,*args,**kwargs): # *args,**kwargs 必须在形参的末尾,顺序不能乱
    pass


Do not panic

二.函数不定长参数实战


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:何以解忧
@Blog(个人博客地址): shuopython.com
@WeChat Official Account(微信公众号):猿说python
@Github:www.github.com
 
@File:python_args.py
@Time:2019/10/3 21:48
 
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
 
'''
普通函数形参 + 不定长形参:
    arg1:普通形参
    arg1:普通形参
    args:不定长参数-元组
    kwargs:不定长参数-字典
'''
 
def function_print(arg1,arg2,*args,**kwargs):
    print(arg1,arg2,args,kwargs)
 
function_print(False,1,b=23,h=4,v=5)
function_print("hello world",False,0,True,j="python教程",a=True)


输出结果:

1

2

False 1 () {'b': 23, 'h': 4, 'v': 5}
hello world False (0, True) {'j': 'python教程', 'a': True}


Note: Note that the order of parameters match

 

III. Key summary

Note that the function parameter passing in three forms:

1. The general parameter passing

2. The default parameters if no external arguments passed to the default parameters, parameters to the default value

3. Note that variable length parameter unpacking, common argument is packaged as a tuple tuple type, packaged as keyword arguments dictionary dict type

 

you may also like:

1.python tuple

2.python dictionary

3.python function declaration and call

 

Reproduced please specify : ape say Python  »  Python function of variable length parameters * argc, ** kargcs


Guess you like

Origin blog.51cto.com/14531342/2458704