【經驗隨手記】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'}
相關文章
相關標籤/搜索