zoukankan      html  css  js  c++  java
  • Python函数的带星号*参数

    1. 三种类型的函数参数

    def func(arg, *args, **kwargs):
        print (arg, type(arg))
        print (args, type(args))
        print (kwargs, type(kwargs))
    
    #arg     --  固定参数,必填
    #args    --  位置参数,可选
    #kwargs  --  关键字参数,可选

    如果同时出现(两两,或全部),三种类型的参数必须按序排列:

    (arg, *args, **kwargs)

    否则函数定义或者函数调用的时候都会出错。

    2. 固定参数arg

    >>> def func(arg):
    ...     print arg
    ... 
    >>> func()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: func() takes exactly 1 argument (0 given)
    >>> func(1)
    1

    3. 位置参数args

    1) 函数定义 -- tuple打包

    >>> def func1(*args):
    ...     print (args, type(args))
    ... 
    >>> func1()
    ((), <type 'tuple'>)
    >>> func1(1)
    ((1,), <type 'tuple'>)
    >>> func1(1,2,3)
    ((1, 2, 3), <type 'tuple'>)

    2) 函数调用 -- tuple解包

    >>> def func2(arg):
    ...     func1(*arg)
    ... 
    >>> func2()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: func2() takes exactly 1 argument (0 given)
    >>> func2(1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in func2
    TypeError: func1() argument after * must be a sequence, not int
    >>> func2((1,))
    ((1,), <type 'tuple'>)
    >>> func2((1,2,3)) ((1, 2, 3), <type 'tuple'>)

    4. 关键字参数kwargs

    1) 函数定义 -- dict打包

    >>> def func1(**kwargs):
    ...     print (kwargs, type(kwargs))
    ... 
    >>> func1()
    ({}, <type 'dict'>)
    >>> func1(1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: func1() takes exactly 0 arguments (1 given)
    >>> func1(a=1)
    ({'a': 1}, <type 'dict'>)
    >>> func1(a=1,b=2,c=3)
    ({'a': 1, 'c': 3, 'b': 2}, <type 'dict'>)

    2) 函数调用 -- dict解包

    >>> def func2(arg):
    ...     func1(**arg)
    ... 
    >>> func2()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: func2() takes exactly 1 argument (0 given)
    >>> func2(1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 2, in func2
    TypeError: func1() argument after ** must be a mapping, not int
    >>> func2(a=1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: func2() got an unexpected keyword argument 'a'
    >>> func2({'a': 1})
    ({'a': 1}, <type 'dict'>)
    >>> func2({'a': 1, 'b': 2, 'c': 3})
    ({'a': 1, 'c': 3, 'b': 2}, <type 'dict'>)
  • 相关阅读:
    java spring boot- freemarker 配置 yml使用流程
    layer 漂亮的弹窗
    react-native 打包apk 更新js和常见问题
    mysql 运行中 偶尔 报错 2002 也许是这个问题,内存不足导致的
    关于rsa公钥格式的处理,一行纯内容进行换行格式化
    第十篇、让UIScrollView的滚动条常显
    第九篇、自定义底部UITabBar
    第八篇、封装NSURLSession网络请求框架
    第二篇、Swift_自定义 tabbar 的 badgeValue显示样式
    第七篇、OC_图片的裁剪基于SDWebImage
  • 原文地址:https://www.cnblogs.com/russellluo/p/3137270.html
Copyright © 2011-2022 走看看