python函数参数收集及其反转
2017-07-13 13:30
387 查看
*<-->tuple和
**<-->dict的使用分为两种情况,一种是函数调用,另外一种是函数定义。
Function call
*tuple means “treat the elements of this tuple as positional arguments to this function call.”
def foo(x, y): print(x, y)
>>> t = (1, 2) >>> foo(*t) 1 2
**dict means “treat the key-value pairs in the dictionary as additional named arguments to this function call.”
def foo(x, y): print(x, y)
>>> d = {'x':1, 'y':2} >>> foo(**d) 1 2
Function signature
*tuple means “take all additional positional aruments to this function and pack them into this parameter as a tuple.”
def foo(*x): print(x)
>>> foo(1, 2) (1, 2)
**dict means “take all additional named arguments to this function and insert them into this parameter as dictionary entries.”
def foo(**d): print(d)
>>> foo(x=1, y=2) {'y': 2, 'x': 1}
相关文章推荐
- 详解Python函数可变参数定义及其参数传递方式
- Python 语言及其应用 Chapter_4_Note_2 函数_参数_
- Python3基础 函数 收集参数+普通参数 的示例
- python 点滴记录11:函数参数的收集
- 【python】函数:函数文档、关键字参数、默认参数、收集参数
- Python函数可变参数定义及其参数传递方式实例详解
- Python函数可变参数定义及其参数传递方式实例详解
- Python 语言及其应用 Chapter_4_Note_2 函数_参数_
- Python函数可变参数定义及其参数传递方式详解
- Python 中函数的 收集参数 机制
- Python 中函数的 收集参数 机制
- 函数及其参数
- python函数参数默认值
- Python 函数的参数
- Python进阶04 函数的参数对应
- Python学习札记(十二) Function3 函数参数一
- Python 语言及其应用 Chapter_6_Note 3 Seek函数功能
- Python 函数动态参数
- Python函数参数传递以及变量作用域
- Python定义函数默认参数