zoukankan      html  css  js  c++  java
  • python中函数的收集参数

    1、

    >>> def a(*xxx):
        print("total length is %d" % len(xxx))
        print("the second element is :",xxx[1])
    
        
    >>> a("aaa","bbb","ccc","ddd","eee","fff")
    total length is 6
    the second element is : bbb
    >>> def a(*xxx,m):
        print("collection parameter is:",xxx)
        print("keyword parameter is :",m)
    
        
    >>> a(1,2,3,4,5)
    Traceback (most recent call last):
      File "<pyshell#616>", line 1, in <module>
        a(1,2,3,4,5)
    TypeError: a() missing 1 required keyword-only argument: 'm'
    >>> a(1,2,3,4,m = 5)      ## 收集参数之外的参数使用关键字参数
    collection parameter is: (1, 2, 3, 4)
    keyword parameter is : 5
    >>> def a(*xxx,m = 5):         ## 收集参数之外的参数使用默认参数
        print("collection parameter is :",xxx)
        print("keyword parameter is :", m)
    
        
    >>> a(1,2,3,4,5,6)
    collection parameter is : (1, 2, 3, 4, 5, 6)
    keyword parameter is : 5

    2、

    >>> def a(*xxx):   ## 单星号收集参数打包为元组
        print(type(xxx))
    
        
    >>> a(111,222,333,444)   
    <class 'tuple'>
    >>> def b(**xxx):   ## 双型号收集参数打包为字典
        print(type(xxx))
    
        
    >>> b(a = "1111",b = "2222",c = "3333")
    <class 'dict'>
  • 相关阅读:
    oracle第四天笔记
    oracle第三天笔记
    oracle第二天笔记
    select min from 连接
    decode 函数用法
    服务器
    婚姻
    黑马2018年JavaEE课程大纲
    Kubernetes本地私有仓库配置
    ELK系统分析nginx日志
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14480415.html
Copyright © 2011-2022 走看看