[Python-Dev] Should inspect.getargspec take any callable?

Andrew Barnert abarnert at yahoo.com
Sat Jan 16 12:23:10 EST 2016


On Jan 16, 2016, at 08:05, Aviv Cohn via Python-Dev <python-dev at python.org> wrote:
> 
> The `getargspec` function in the `inspect` module enforces the input parameter to be either a method or a function.

The `getargspec` already works with classes, callable objects, and some builtins.

It's also deprecated, in part because its API can't handle various features (like keyword-only arguments). There is an extended version that can handle some of those features, but as of 3.5 that one is deprecated as well.

The `signature` function is much easier to use, as well as being more powerful.

> 
>     def getargspec(func):
>         """Get the names and default values of a function's arguments.
>     
>         A tuple of four things is returned: (args, varargs, varkw, defaults).
>         'args' is a list of the argument names (it may contain nested lists).
>         'varargs' and 'varkw' are the names of the * and ** arguments or None.
>         'defaults' is an n-tuple of the default values of the last n arguments.
>         """
>     
>         if ismethod(func):
>             func = func.im_func
>         if not isfunction(func):
>             raise TypeError('{!r} is not a Python function'.format(func))
>         args, varargs, varkw = getargs(func.func_code)
>         return ArgSpec(args, varargs, varkw, func.func_defaults)
> 
> Passing in a callable which is not a function causes a TypeError to be raised.
> 
> I think in this case any callable should be allowed, allowing classes and callable objects as well.
> We can switch on whether `func` is a function, a class or a callable object, and pass into `getargs` the appropriate value.
> 
> What is your opinion?
> Thank you
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/abarnert%40yahoo.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20160116/d61faad2/attachment.html>


More information about the Python-Dev mailing list