是否有不能用关键字参数调用的内置函数的完整列表?
Is there a complete list of built-in functions that cannot be called with keyword argument?
回答中提到的人 a1, a2
Due to the way the Python C-level APIs developed, a lot of built-in
functions and methods don't actually have names for their arguments.
我发现它真的很烦人,因为我无法通过查看文档来了解它。例如,eval
eval(expression, globals=None, locals=None)
然后我写了这行代码
print(eval('a+b', globals={'a':1, 'b':2}))
得到了TypeError: eval() takes no keyword arguments
。那么是否有此类功能的完整列表?我怎么知道一个函数是否允许有关键字参数?
在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也有bug:
>>> sorted.__text_signature__
'($module, iterable, key=None, reverse=False)'
尽管根据 issue 26729 in the Python bug tracker,iterable
之后应该有 /
,因为 iterable
不能用作关键字参数。
遗憾的是,此信息在 Python 文档本身中尚不可用。
回答中提到的人 a1, a2
Due to the way the Python C-level APIs developed, a lot of built-in functions and methods don't actually have names for their arguments.
我发现它真的很烦人,因为我无法通过查看文档来了解它。例如,eval
eval(expression, globals=None, locals=None)
然后我写了这行代码
print(eval('a+b', globals={'a':1, 'b':2}))
得到了TypeError: eval() takes no keyword arguments
。那么是否有此类功能的完整列表?我怎么知道一个函数是否允许有关键字参数?
在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也有bug:
>>> sorted.__text_signature__
'($module, iterable, key=None, reverse=False)'
尽管根据 issue 26729 in the Python bug tracker,iterable
之后应该有 /
,因为 iterable
不能用作关键字参数。
遗憾的是,此信息在 Python 文档本身中尚不可用。