理解 Python 中的 *args 和 **kwargs

Python是支持可变参数的,最简单的方法莫过于使用默认参数,例如:

def test_defargs(one, two = 2):
   print 'Required argument: ', one
   print 'Optional argument: ', two

test_defargs(1)
# result:
# Required argument: 1
# Optional argument: 2

test_defargs(1, 3)
# result:
# Required argument: 1
# Optional argument: 3

当然,本文章的主题并不是讲默认参数,而是另外一种达到可变参数 (Variable Argument) 的方法:使用*args和**kwargs语法。其中,*args是可变的positional arguments列表,**kwargs是可变的keyword arguments列表。并且,*args必须位于**kwargs之前,因为positional arguments必须位于keyword arguments之前。

首先介绍两者的基本用法。

下面一个例子使用*args,同时包含一个必须的参数:

def test_args(first, *args):
   print 'Required argument: ', first
   for v in args:
      print 'Optional argument: ', v

test_args(1, 2, 3, 4)
# result:
# Required argument: 1
# Optional argument:  2
# Optional argument:  3
# Optional argument:  4

下面一个例子使用*kwargs, 同时包含一个必须的参数和*args列表:

def test_kwargs(first, *args, **kwargs):
   print 'Required argument: ', first
   for v in args:
      print 'Optional argument (*args): ', v
   for k, v in kwargs.items():
      print 'Optional argument %s (*kwargs): %s' % (k, v)

test_kwargs(1, 2, 3, 4, k1=5, k2=6)
# results:
# Required argument:  1
# Optional argument (*args):  2
# Optional argument (*args):  3
# Optional argument (*args):  4
# Optional argument k2 (*kwargs): 6
# Optional argument k1 (*kwargs): 5

*args和**kwargs语法不仅可以在函数定义中使用,同样可以在函数调用的时候使用。不同的是,如果说在函数定义的位置使用*args和**kwargs是一个将参数pack的过程,那么在函数调用的时候就是一个将参数unpack的过程了。下面使用一个例子来加深理解:

def test_args(first, second, third, fourth, fifth):
    print 'First argument: ', first
    print 'Second argument: ', second
    print 'Third argument: ', third
    print 'Fourth argument: ', fourth
    print 'Fifth argument: ', fifth

# Use *args
args = [1, 2, 3, 4, 5]
test_args(*args)
# results:
# First argument:  1
# Second argument:  2
# Third argument:  3
# Fourth argument:  4
# Fifth argument:  5

# Use **kwargs
kwargs = {
    'first': 1,
    'second': 2,
    'third': 3,
    'fourth': 4,
    'fifth': 5
}

test_args(**kwargs)
# results:
# First argument:  1
# Second argument:  2
# Third argument:  3
# Fourth argument:  4
# Fifth argument:  5

使用*args和**kwargs可以非常方便的定义函数,同时可以加强扩展性,以便日后的代码维护。

 转载请注明转自: 团子的小窝 , 本文固定链接: 理解 Python 中的 *args 和 **kwargs

/**--------------------------------------------2--------------------------------/

python中*args **kw到底是什么意思?求指教!

*args是非关键字参数,用于元组,**kw是关键字参数,用于字典

举例:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
1. * args
 
def  tupleArgs(arg1, arg2 =  'B' * arg3):
     print ( 'arg 1:%s '  %  arg1)
     print ( 'arg 2:%s '  %  arg2)
     for  eachArgNum  in  range ( len (arg3)):
         print ( 'the %d in arg 3 :%s '  %  (eachArgNum,arg3[eachArgNum]))
if  __name__  = =  '__main__' :
     tupleArgs( 'A' )      
     #   arg 1:A 
     #   arg 2:B 
     tupleArgs( '23' , 'C' )
     #   arg 1:23 
     #   arg 2:C
     tupleArgs( '12' , 'A' , 'GF' , 'L' )
     #   arg 1:12 
     #   arg 2:A 
     #   the 0 in arg 3 :GF 
     #   the 1 in arg 3 :L 
     
     * * kw
     
     def  dictArgs(kw1, kw2 =  'B' * * kw3):
     print ( 'kw 1:%s '  %  kw1)
     print ( 'kw 2:%s '  %  kw2)
     for  eachKw  in  kw3:
         print ( 'the %s ---->:%s '  %  (eachKw,kw3[eachKw]))
if  __name__  = =  '__main__' :
     dictArgs( 'A' )
     #   kw 1:A 
     #   kw 2:B 
     dictArgs( '23' , 'C' )
     #   kw 1:23 
     #   kw 2:C 
     dictArgs( '12' , 'A' , c  =  'C' ,d  =  '12121212' )
     #   kw 1:12 
     #   kw 2:A 
     #   the d ---->:12121212 
     #   the c ---->:C 
     dictArgs( 'kw' ,c  =  'C' ,d  =  '12121212' ,kw  =  'KW' )
     #   kw 1:kw 
     #   kw 2:B 
     #   the kw ---->:KW 
     #   the d ---->:12121212 
     #   the c ---->:C

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值