【经验随手记】Python中一些有趣的,很酷的,或者奇怪的功能

1. 函数的可变参数:python

*args and **kwargs——This is a Python feature that allows a function to accept a dynamic, arbitrarynumber of arguments whose names aren’t known until runtime.ide

*把位置参数们转为一个tuple:
函数

(If you put a singleasterisk in front of a parameter in a function definition, any positional arguments to that function will be rolled up into a single tuple. )spa

**把关键字参数们转为一个dictionary:it

If you puttwo asterisks in front of a parameter in a function definition, any keyword arguments to that function will be rolled up into a single dictionary.io


好比:ast

>>> foo(1, 2, 3)
Positional arguments are:
(1, 2, 3)
Keyword arguments are:
{}
>>> foo(1, 2, name='Adrian', framework='Django')
Positional arguments are:
(1, 2)
Keyword arguments are:
{'framework': 'Django', 'name': 'Adrian'}
相关文章
相关标签/搜索