• python函数不定长参数


    def fun(a, b, *args):
        print(a)
        print(b)
        print(args)
        print("="*30)
        ret = a + b
        for i in args:
            ret += i
        return ret
    
    print(fun(1,2,3,4))
    
    结果:
    1
    2
    (3, 4)
    ==============================
    10

    1,2分别赋值给a,b,剩下的参数以元组的形式赋值给args

    字典形式参数:

    def fun(a, b, *args, **kwargs):
        print(a)
        print(b)
        print(args)
        print(kwargs)
    
    fun(1, 2, 3, 4, name = "hello", age = 20)
    
    结果:
    1
    2
    (3, 4)
    {'name': 'hello', 'age': 20}

    传入元组和字典:

    def fun(a, b, *args, **kwargs):
        print(a)
        print(b)
        print(args)
        print(kwargs)
    
    tup = (11,22,33)
    dic = {"name":"hello", "age":20}
    fun(1, 2, *tup, **dic)
    
    结果:
    1
    2
    (11, 22, 33)
    {'name': 'hello', 'age': 20}
  • 相关阅读:
    线程同步锁的使用方式
    EventBus简单封装
    Dagger2不自动生成daggerXXXcomponent
    android mvp模式
    第八天
    单词统计续
    学习进度第十一周
    第七天
    第六天
    第五天
  • 原文地址:https://www.cnblogs.com/xhcdream/p/8159231.html
Copyright © 2020-2023  润新知