• Python中*args 和**kwargs作为形参和实参时的功能详解


    *args 和**kwargs作为形参

    *args 和**kwargs作为形参被称为不定长参数,用来处理超出必备参数部分的参数。注意:args和kwargs可以修改为其它变量名。

    必备参数就是在定义函数时传入的形参,在调用函数时,传入的参数须以正确的顺序传入,传入的数量必须和声明时的一样,不然会出现语法错误。

    以下为必备参数语法错误实例:

    def student(name, age):
      print(name, age)
    student() """ Traceback (most recent call last): File "D:/test.py", line 5, in <module> student() TypeError: student() missing 2 required positional arguments: 'name' and 'age' """ student("张三", 11, 99) """ Traceback (most recent call last): File "D:/test.py", line 5, in <module> student("张三",11, 99) TypeError: student() takes 2 positional arguments but 3 were given """

    当传入的参数超出必备参数时,若不想出现语法错误,可以使用不定长参数来接收这些多余的参数,若多余的参数类型为值(值为自定义说法,指的是没有”=“连接,单独的一个值,这个值可以是任何类型str,list,dict等),则被*args以元组类型接收

    若多余的参数类型为键值(键值为自定义说法,指的是一个参数用”=“连接一个值,这个值可以是任何类型str,list,dict等),则被**kwargs以字典类型接收。

    def student(name, age, *args, **kwargs):
        print("必备参数:", name, age)
        print("多余的“值”参数:", args)
        print("多余的“键值”参数:", kwargs)
    
    
    student("张三", 11, 22, [33, 44], {"语文": 55}, score=66, evaluation={"数学": ""})
    
    """
    输出结果如下:
    必备参数: 张三 11
    多余的“值”参数: (22, [33, 44], {'语文': 55})
    多余的“键值”参数: {'score': 66, 'evaluation': {'数学': '优'}}
    """

    *args 和**kwargs作为实参

    接收到这些多余的参数,怎么传递给其它函数用呢?传递时是使用带星的还是不带星呢?

    先看不带*的实例:

    def summary_student(name, *score, **evaluation):
        print("-"*20)
        print("必备参数:", name)
        print("多余的“值”参数:", score)
        print("多余的“键值”参数:", evaluation)
    
    
    def student(name, age, *args, **kwargs):
        print("args变量的值:", args)
        print("kwargs变量的值:", kwargs)
        summary_student(name, age, args, kwargs)
    
    
    student("张三", 11, 22, [33, 44], {"语文": 55}, score=66, evaluation={"数学": ""})
    
    """
    输出结果如下:
    args变量的值: (22, [33, 44], {'语文': 55})
    kwargs变量的值: {'score': 66, 'evaluation': {'数学': '优'}}
    --------------------
    必备参数: 张三
    多余的“值”参数: (11, (22, [33, 44], {'语文': 55}), {'score': 66, 'evaluation': {'数学': '优'}})
    多余的“键值”参数: {}
    """

    从上面实例多余的参数分析,多余参数传递给其它函数使用时,若不带*,实际上就是把变量args整个元组当成一个值传递给其它函数使用,同理kwargs也会被当做一个值处理。

    在看下带*的实例:

    def summary_student(name, *score, **evaluation):
        print("-"*20)
        print("必备参数:", name)
        print("多余的“值”参数:", score)
        print("多余的“键值”参数:", evaluation)
    
    
    def student(name, age, *args, **kwargs):
        print("args变量的值:", args)
        print("kwargs变量的值:", kwargs)
        summary_student(name, age, *args, **kwargs)
    
    
    student("张三", 11, 22, [33, 44], {"语文": 55}, score=66, evaluation={"数学": ""})
    
    """
    输出结果如下:
    args变量的值: (22, [33, 44], {'语文': 55})
    kwargs变量的值: {'score': 66, 'evaluation': {'数学': '优'}}
    --------------------
    必备参数: 张三
    多余的“值”参数: (11, 22, [33, 44], {'语文': 55})
    多余的“键值”参数: {'score': 66, 'evaluation': {'数学': '优'}}
    """

    从上面实例多余的参数分析,多余参数传递给其它函数使用时,若带*,其它函数处理时,全部按原传入参数的类型处理,原传入参数为”值“类型就让其它函数的*args接收,原传入参数为"键值"类型就让其它函数的**kwargs接收。

    也就是说 *args 和**kwargs作为实参时,具有拆包的作用。

  • 相关阅读:
    caffe: compile error: Could not open or find file your path~~/resized_data/0 and a total of 2 images .
    caffe: compile error : undefined reference to `cv::imread(cv::String const&, int)' et al.
    caffe: test code for Deep Learning approach
    C++ little errors , Big problem
    VGG_19 train_vali.prototxt file
    matlab 工具之各种降维方法工具包,下载及使用教程,有PCA, LDA, 等等。。。
    利用caffe生成 lmdb 格式的文件,并对网络进行FineTuning
    matlab 相关代码记录
    论文阅读之 Inferring Analogous Attributes CVPR 2014
    布局的几种方式(静态布局、自适应布局、流式布局、响应式布局、弹性布局)
  • 原文地址:https://www.cnblogs.com/testlearn/p/12394540.html
Copyright © 2020-2023  润新知