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

    python参数解包的实现

    小妮浅浅小妮浅浅2021-05-17 09:57:04原创1965

    在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解包的功能。

    1、* 操作符 可以用来解包列表和元组。

    >>> list(range(3, 6))            # normal call with separate arguments
    [3, 4, 5]
    >>> args = [3, 6]
    >>> list(range(*args))            # call with arguments unpacked from a list
    [3, 4, 5]

    2、** 操作符 可以用来解包字典。

    >>> def parrot(voltage, state='a stiff', action='voom'):
    ...     print("-- This parrot wouldn't", action, end=' ')
    ...     print("if you put", voltage, "volts through it.", end=' ')
    ...     print("E's", state, "!")
    ...
    >>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
    >>> parrot(**d)

    以上就是python参数解包的实现,希望能对大家有所帮助。更多Python学习指路:python基础教程

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

    专题推荐:python参数
    品易云
    上一篇:python函数传递参数的两种方式 下一篇:python函数标注是什么

    相关文章推荐

    • python类装饰器的使用注意• python如何修饰带参数的装饰器• python装饰器管理函数和类的注意点• python序列的数学运算• python参数中默认值的执行• python函数传递参数的两种方式

    全部评论我要评论

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

  • 取消发布评论
  • 

    Python学习网