在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。 (我想我从错误跟踪器中读取它) –