美文网首页
任意数量的位置参数

任意数量的位置参数

作者: ZChao_b56b | 来源:发表于2019-07-27 13:18 被阅读0次
    ## var-positional parameter
    ## 定义的时候,我们需要添加单个星号作为前缀
    >>def func(arg1, arg2, *args):print (arg1, arg2, args)
    
    ## 调用的时候,前面两个必须在前面
    ## 前两个参数是位置或关键字参数的形式
    ## 所以你可以使用这种参数的任一合法的传递方法
    >>func("hello", "Tuple, values is:", 2, 3, 3, 4)
    hello Tuple, values is: (2, 3, 3, 4)
    ## 多余的参数将自动被放入元组中提供给函数使用
     
    ## 如果你需要传递元组给函数
    ## 你需要在传递的过程中添加*号
    ## 请看下面例子中的输出差异:
     
    >>func("hello", "Tuple, values is:", (2, 3, 3, 4))
    hello Tuple, values is: ((2, 3, 3, 4),)
    >>func("hello", "Tuple, values is:", *(2, 3, 3, 4)) 
    hello Tuple, values is: (2, 3, 3, 4)
    

    相关文章

      网友评论

          本文标题:任意数量的位置参数

          本文链接:https://www.haomeiwen.com/subject/jqterctx.html