在Python 3.5,你可以檢查內置函數的__text_signature__
:
>>> eval.__text_signature__
'($module, source, globals=None, locals=None, /)'
或
>>> abs.__text_signature__
'($module, x, /)'
>>> abs(x=5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: abs() takes no keyword arguments
(x
不能被用作關鍵字參數)
的/
意味着接下來的參數可以用作關鍵字參數。 C.F.
>>> compile.__text_signature__
'($module, /, source, filename, mode, flags=0,\n dont_inherit=False, optimize=-1)'
>>> compile(source='foo', filename='bar', mode='exec')
<code object <module> at 0x7f41c58f0030, file "bar", line 1>
當然也有缺陷,甚至在3.5:
>>> sorted.__text_signature__
'($module, iterable, key=None, reverse=False)'
雖然根據issue 26729 in the Python bug tracker,就必須有/
後iterable
作爲iterable
不能被用作關鍵字參數。
不幸的是,這些信息在Python文檔本身中尚不可用。
不僅如此,但有這個錯誤,嘗試'排序(迭代= [])'與'排序(迭代=無)'。 –
我想所有這些「陷阱」實際上都是bug,應該被報告:「sorted()」和關鍵字參數都被記錄但不被接受。 (Fwiw PyPy通常接受關鍵字參數。) –
據報道IIRC。 (我想我從錯誤跟蹤器中讀取它) –