1

I've found a number of questions and answers dealing with determining the parameters/arguments needed for functions* in python. Python has a number of callable objects (using callable) that will return a value for which inspect.getfullargspec returns an error (inspect.Signature seems less reliable by my informal testing?).

For example:

inspect.getfullargspec(max) Returns an Error: TypeError: unsupported callable

Is there an alternative to inspect that will work for otherwise "unsupported callable" [functions/objects?]

Looking at a number of builtin callable objects it seems like many are in the "unsupported" category, i can only guess at objects in other modules/libraries.

*I may be mincing my terms here, apologies - any way to determine specifically which is which?

Richard W
  • 302
  • 3
  • 11

1 Answers1

1

In CPython, builtin functions are implemented in C, and thus have many quirks. This is one of them. Taking some examples from PEP 570:

In current versions of Python, many CPython "builtin" and standard library functions only accept positional-only parameters. The resulting semantics can be easily observed by calling one of these functions using keyword arguments:

>>> help(pow)
...
pow(x, y, z=None, /)
...

>>> pow(x=5, y=3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: pow() takes no keyword arguments

pow() expresses that its parameters are positional-only via the / marker. However, this is only a documentation convention; Python developers cannot use this syntax in code.

There are functions with other interesting semantics:

  • range(), an overloaded function, accepts an optional parameter to the left of its required parameter.
  • dict(), whose mapping/iterator parameter is optional and semantically must be positional-only. Any externally visible name for this parameter would occlude that name going into the **kwarg keyword variadic parameter dict.

Currently (while the PEP is not implemented) Python has no official way to express these unusual semantics such as positional only arguments.

Alex Hall
  • 31,431
  • 4
  • 39
  • 71