• 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'>
  • 相关阅读:
    luogu 3388 【模板】割点(割顶)
    bzoj 3624 免费道路
    bzoj 1179 Atm
    bzoj 2428 均分数据
    luogu 4429 染色
    luogu 4427 求和
    luogu 1121 环状最大两段子段和
    hdu 4777 Queue
    hdu 5492 Find a path
    hdu 5441 Travel
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14480415.html
Copyright © 2020-2023  润新知