[Python-Dev] Should inspect.getargspec take any callable?
Hello :)This is my first mail here, I had an idea that I'd like to propose.
The `getargspec` function in the `inspect` module enforces the input parameter
to be either a method or a function.
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
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Should inspect.getargspec take any callable?
On Jan 16, 2016, at 08:05, Aviv Cohn via Python-Dev
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
> [email protected]
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/abarnert%40yahoo.com
___
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] [Python-ideas] Boolean value of an Enum member
[resending to lists -- sorry, Greg] On 01/15/2016 12:36 PM, Greg Ewing wrote: Ethan Furman wrote: So the question now is: for a standard Enum (meaning no other type besides Enum is involved) should __bool__ look to the value of the Enum member to determine True/False, or should we always be True by default and make the Enum creator add their own __bool__ if they want something different? Can't you just specify a starting value of 0 if you want the enum to have a false value? That doesn't seem too onerous to me. You can start with zero, but unless the Enum is mixed with a numeric type it will evaluate to True. Also, but there are other falsey values that a pure Enum member could have: False, None, '', etc., to name a few. However, as Barry said, writing your own is a whopping two lines of code: def __bool__(self): return bool(self._value_) With Barry and Guido's feedback this issue is closed. Thanks everyone! -- ~Ethan~ ___ Python-Dev mailing list [email protected] https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Should inspect.getargspec take any callable?
On 17 January 2016 at 03:23, Andrew Barnert via Python-Dev wrote: > On Jan 16, 2016, at 08:05, Aviv Cohn via Python-Dev > 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. As Andrew states here, the limitations of getargspec() and getfullargspec() are why they were deprecated in favour of inspect.signature() in Python 3.3: https://docs.python.org/3/library/inspect.html#introspecting-callables-with-the-signature-object The funcsigs project provides a backport of much of that functionality to earlier Python versions: https://pypi.python.org/pypi/funcsigs Cheers, Nick. -- Nick Coghlan | [email protected] | Brisbane, Australia ___ Python-Dev mailing list [email protected] https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
