广告合作
  • 今日头条

    今日头条

  • 百度一下

    百度一下,你就知道

  • 新浪网

    新浪网 - 提供新闻线索,重大新闻爆料

  • 搜狐

    搜狐

  • 豆瓣

    豆瓣

  • 百度贴吧

    百度贴吧——全球领先的中文社区

  • 首页 尚未审核订阅工具 订阅

    python系列:[3]如何定义不定参数函数

    来源:网络收集  点击:  时间:2024-04-30
    【导读】:
    python构建参数个数不定的函数工具/原料morepython方法/步骤1/6分步阅读

    元组形式:

    1、定义函数

    def test1(*args):

    print(################test1################)

    print(type(args))

    print(args)

    2、调用函数

    正确调用:

    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/6

    字典形式:

    1、定义函数

    def test2(**kargs):

    print(################test2################)

    print(type(kargs))

    print(kargs)

    2、调用函数

    正确调用:

    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/6

    混合形式:

    1、定义函数

    def test3(*args, **kargs):

    print(################test3################)

    print(type(args))

    print(args)

    print(type(kargs))

    print(kargs)

    2、调用函数

    正确调用:

    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/6

    其他形式1:

    1、定义函数

    def test4(a = ()):

    print(################test4################)

    print(type(a))

    print(a)

    2、调用函数

    正确调用:

    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/6

    其他形式2:

    1、定义函数

    def test5(b = {}):

    print(################test5################)

    print(type(b))

    print(b)

    2、调用函数

    正确调用:

    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/6

    其他形式3:

    1、定义函数

    def test6(a = (), b = {}):

    print(################test6################)

    print(type(a))

    print(a)

    print(type(b))

    print(b)

    2、调用函数

    正确调用:

    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

    注意事项

    本篇经验系本人依照真实经历原创,未经许可,谢绝转载

    python
    本文关键词:

    版权声明:

    1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

    2、本站仅提供信息发布平台,不承担相关法律责任。

    3、若侵犯您的版权或隐私,请联系本站管理员删除。

    4、文章链接:http://www.1haoku.cn/art_633921.html

    相关资讯

    ©2019-2020 http://www.1haoku.cn/ 国ICP备20009186号05-07 04:33:02  耗时:0.028
    0.0283s