• 理解 Python 中的可变参数 *args 和 **kwargs:


      默认参数:

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

     def getSum(x,y=5):
        print "x:", x
        print "y:", y
        print "x+y :", x + y
    
    
    getSum(1)
    
    # result:
    # x: 1
    # y: 5
    # x+y : 6
    
    getSum(1,7)
    
    # result:
    # x: 1
    # y: 7
    # x+y : 8

      可变参数:

    另外一种达到可变参数 (Variable Argument) 的方法:使用*args和**kwargs语法。其中,*args是可变的positional arguments列表,**kwargs是可变的keyword arguments列表。并且,*args必须位于**kwargs之前,因为positional arguments必须位于keyword arguments之前。

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

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

     1 def test_args(first, *args):
     2    print 'Required argument: ', first
     3    for v in args:
     4       print 'Optional argument: ', v
     5 
     6 test_args(1, 2, 3, 4)
     7 # result:
     8 # Required argument: 1
     9 # Optional argument:  2
    10 # Optional argument:  3
    11 # Optional argument:  4

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

     1 def test_kwargs(first, *args, **kwargs):
     2    print 'Required argument: ', first
     3    for v in args:
     4       print 'Optional argument (*args): ', v
     5    for k, v in kwargs.items():
     6       print 'Optional argument %s (*kwargs): %s' % (k, v)
     7 
     8 test_kwargs(1, 2, 3, 4, k1=5, k2=6)
     9 # results:
    10 # Required argument:  1
    11 # Optional argument (*args):  2
    12 # Optional argument (*args):  3
    13 # Optional argument (*args):  4
    14 # Optional argument k2 (*kwargs): 6
    15 # Optional argument k1 (*kwargs): 5

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

     1 def test_args(first, second, third, fourth, fifth):
     2     print 'First argument: ', first
     3     print 'Second argument: ', second
     4     print 'Third argument: ', third
     5     print 'Fourth argument: ', fourth
     6     print 'Fifth argument: ', fifth
     7 
     8 # Use *args
     9 args = [1, 2, 3, 4, 5]
    10 test_args(*args)
    11 # results:
    12 # First argument:  1
    13 # Second argument:  2
    14 # Third argument:  3
    15 # Fourth argument:  4
    16 # Fifth argument:  5
    17 
    18 # Use **kwargs
    19 kwargs = {
    20     'first': 1,
    21     'second': 2,
    22     'third': 3,
    23     'fourth': 4,
    24     'fifth': 5
    25 }
    26 
    27 test_args(**kwargs)
    28 # results:
    29 # First argument:  1
    30 # Second argument:  2
    31 # Third argument:  3
    32 # Fourth argument:  4
    33 # Fifth argument:  5

    版权所有,文章来源:http://www.cnblogs.com/sagecheng/p/5968762.html

    个人能力有限,本文内容仅供学习、探讨,欢迎指正、交流。

    .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引

      参考资料:

    理解 Python 中的 *args 和 **kwargs:http://kodango.com/variable-arguments-in-python

     
  • 相关阅读:
    SpringBoot 如何生成接口文档,老鸟们都这么玩的!
    ELK 外网访问
    Elasticsearch 7.x配置用户名密码访问 开启x-pack验证
    在centos7 中安装Kibana
    在centos7 安装Elasticsearch 步骤:
    cuda-pytorch-gpu快速配置
    Face 2 to 3 D
    PointNet++
    PointNet:Deep Learning on Point Sets for 3D Classification and Segmentation
    3D Face Modeling From Diverse Raw Scan Data
  • 原文地址:https://www.cnblogs.com/sagecheng/p/5968762.html
Copyright © 2020-2023  润新知