• 技术文章 >Python技术 >Python基础教程

    python可变参数的使用注意

    小妮浅浅小妮浅浅2021-05-28 09:50:08原创3384

    1、函数传入实参,可变参数(*)之前的参数不能指定参数名。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    >>> def myfun(a, *b):

    ...     print(a

    )...     print(b)

    ...

    >>> myfun(a=1, 2,3,4) 

    File "<stdin>", line 1

    SyntaxError: positional argument follows keyword argument

    >>> myfun(1, 2,3,4)

    1

    (2, 3, 4)

    2、函数传入实参,可变参数(*)之后的参数必须指定参数名,否则就会被归到可变参数之中。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    >>> def myfun(a, *b, c=None):

    ...     print(a)

    ...     print(b)

    ...     print(c)

    ...

    >>> myfun(1, 2,3,4)

    1

    (2, 3, 4)

    None

    >>> myfun(1, 2,3,c=4)

    1

    (2, 3)

    4

    以上就是python可变参数的使用注意,希望对大家有所帮助。更多Python学习指路:python基础教程

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python可变参数
    上一篇:dir()函数在python模块的使用 下一篇:python新式类是什么

    相关文章推荐

    • python中fork()的调用• python from…import的导入注意• python中__name__调用模块• dir()函数在python模块的使用

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网