Is there a complete list of built-in functions that cannot be called using the keyword argument?

People mentioned in answers a1 , a2 that

Due to the way Python C-level APIs were developed, many built-in functions and methods have virtually no names for their arguments.

I realized that it’s really annoying because I can’t find out by looking at the document. For example, eval

eval (expression, globals = None, locals = None)

Then I wrote this line of code

print(eval('a+b', globals={'a':1, 'b':2})) 

and got TypeError: eval() takes no keyword arguments . So, is there a complete list of functions of this kind? How do I know if a function is allowed for keyword arguments?

+8
python arguments python-c-api
source share
1 answer

In Python 3.5, you can check the __text_signature__ inline function:

 >>> eval.__text_signature__ '($module, source, globals=None, locals=None, /)' 

or

 >>> 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 cannot be used as a keyword argument)

/ means that the following arguments can be used as keyword arguments. CF

 >>> 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> 

Of course, there are errors even in 3.5:

 >>> sorted.__text_signature__ '($module, iterable, key=None, reverse=False)' 

although according to issue 26729 in the Python tracker , after iterable should be / as iterable it cannot be used as a keyword argument.


Unfortunately, this information is not yet available in the Python documentation itself.

+4
source share

All Articles