zoukankan      html  css  js  c++  java
  • Python不定参数函数

    1. 元组形式

    def test1(*args):
    
        print('################test1################')
        print(type(args))
        print(args)

    正确调用:

    test1(1, 2)          #args在函数体内部为tuple类型

    错误调用:

    test1(1, b=2)      #TypeError: test1() got an unexpected keyword argument 'b'
    test1(a=1, b=2)  #TypeError: test1() got an unexpected keyword argument 'a'
    test1(a=1, 2)      #TypeError: test1() got an unexpected keyword argument 'a'

    2. 字典形式

    def test2(**kargs):
    
        print('################test2################')
        print(type(kargs))
        print(kargs)

    正确调用:

    test2(a=1, b=2)  #kargs在函数体内部为dict类型

    错误调用:

    test2(1, 2)          #TypeError: test2() takes exactly 0 arguments (2 given)
    test2(1, b=2)      #TypeError: test2() takes exactly 0 arguments (2 given)
    test2(a=1, 2)      #SyntaxError: non-keyword arg after keyword arg

    3. 混合形式

    def test3(*args, **kargs):
    
        print('################test3################')
        print(type(args))
        print(args)
        print(type(kargs))
        print(kargs)

    正确调用:

    test3(1, 2)          #args在函数体内部为tuple类型,kargs为空dict类型
    test3(1, b=2)      #args在函数体内部为tuple类型,kargs为dict类型
    test3(a=1, b=2)  #args在函数体内部为空tuple类型,kargs为dict类型

    错误调用:

    test3(a=1, 2)      #SyntaxError: non-keyword arg after keyword arg

    4. 其他形式1

    def test4(a = ()):
    
        print('################test4################')
        print(type(a))
        print(a)

    正确调用:

    test4((1, 2))        #a在函数体内部为tuple类型

    test4(a=(1, 2))    #a在函数体内部为tuple类型

    test4((1,))          #a在函数体内部为tuple类型

    test4(a=(1,))      #a在函数体内部为tuple类型

    test4((1))           #a在函数体内部为int类型,非tuple类型

    test4(a=(1))       #a在函数体内部为int类型,非tuple类型

    test4(1)             #a在函数体内部为int类型,非tuple类型

    test4(a=1)         #a在函数体内部为int类型,非tuple类型

    错误调用:

    test4(1, 2)          #TypeError: test4() takes at most 1 argument (2 given)

    test4(1, b=2)      #TypeError: test4() got an unexpected keyword argument 'b'

    test4(a=1, b=2)  #TypeError: test4() got an unexpected keyword argument 'b'

    5. 其他形式2

    def test5(b = {}):
    
        print('################test5################')
        print(type(b))
        print(b)

    正确调用:

    test5({'a':2})       #b在函数体内部为dict类型

    test5(b={'a':2})

    test5({'a':2,'b':3})#b在函数体内部为dict类型

    test5(b={'a':2,'b':3})

    test5(b=2)           #b在函数体内部为int类型,非dict类型

    错误调用:

    test5(a=1, b=2)   #TypeError: test5() got an unexpected keyword argument 'a'

    test5(1, 2)           #TypeError: test5() takes at most 1 argument (2 given)

    test5(1, b=2)       #TypeError: test5() got multiple values for keyword argument 'b'

    6. 其他形式3

    def test6(a = (), b = {}):
    
        print('################test6################')
        print(type(a))
        print(a)
        print(type(b))
        print(b)

    正确调用:

    test6(1, 2)

    test6(a=1, b=2)

    test6(a=1, b=2)

    test6((1, 2), {'c':8})

    test6({'c':8})

    test6(b={'c':8})

    test6((1, 2), b=2)

    test6((1, 2), b=2)

    错误调用:

    test6(a=1, 2)       #SyntaxError: non-keyword arg after keyword arg

    test6(1, 2, b=2)   #TypeError: test6() got multiple values for keyword argument 'b'

    关于不定参数函数中使用传入参数调用其他固定参数函数的使用请移驾至:http://www.cnblogs.com/doudongchun/p/3704123.html

  • 相关阅读:
    学Python要避免哪些坑,如何巩固好基础
    Python爬虫:现学现用xpath爬取豆瓣音乐
    福州大学软件工程1816 | W班 第10次作业[软件工程实践总结]
    福州大学软件工程1816 | W班 第10次作业[个人作业——软件产品案例分析]
    福州大学软件工程1816 | W班 第8次作业[团队作业,随堂小测——校友录]
    福州大学软件工程1816 | W班 第6次作业WordCount成绩排名
    福州大学软件工程1816 | W班 第4次作业(团队展示)成绩排名
    福州大学软件工程1816 | W班 第2次作业成绩排名
    软件工程github使用小结
    2018北航软工教学培训小结
  • 原文地址:https://www.cnblogs.com/doudongchun/p/3704108.html
Copyright © 2011-2022 走看看