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'>
  • 相关阅读:
    Tarjan-割点&桥&双连通
    树状数组
    loli的搜索测试-5
    思维题
    生成树
    贪心
    loli的搜索测试-4
    树链剖分
    基于AHK的上课自动签到
    用列表实现栈
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14480415.html
Copyright © 2011-2022 走看看