简体   繁体   English

Introspect函数用于确定使用哪个参数解包(位置或关键字)

[英]Introspect function to determine which argument unpacking (positional or keyword) was used

I was searching for a way to identify whether some argument is used for unpacking and I have found this: 我正在寻找一种方法来确定是否有一些参数用于解包,我发现了这个:

>>> def func_has_positional_args(func):
    std_args = func.func_code.co_argcount
    wildcard_args = len(func.func_code.co_varnames) - std_args
    if wildcard_args == 2:
        return True  # yes, has both positional and keyword args
    elif wildcard_args == 0:
        return False  # has neither positional, nor keyword args
    else:
        raise NotImplementedError('Unable to tell')


>>> func_has_keyword_args = func_has_positional_args
>>> def test1(a, b, *args, **kwargs): pass

>>> func_has_positional_args(test1), func_has_keyword_args(test1)
(True, True)
>>> def test2(a, b): pass

>>> func_has_positional_args(test2), func_has_keyword_args(test2)
(False, False)
>>> def test3(a, b, *args): pass

>>> func_has_positional_args(test3)

Traceback (most recent call last):
  File "<pyshell#52>", line 1, in <module>
    func_has_positional_args(test3)
  File "<pyshell#41>", line 9, in func_has_positional_args
    raise NotImplementedError('Unable to tell')
NotImplementedError: Unable to tell

So I am able to tell, if there are is no positional, nor keyword arguments unpacking. 所以我能够告诉,如果没有位置,也没有关键字参数解包。 I am also able to tell if there are both, but I am unable to distinguish which "wildcard" type argument is implemented, if there is only one "wildcard". 我也能够判断是否存在两者,但是如果只有一个“通配符”,我无法区分实现了哪个“通配符”类型参数。

Could you help me achieve the following result? 你能帮我实现以下结果吗?

# Already satisfied with above code:
assert func_has_positional_args(test1) == True
assert func_has_keyword_args(test1) == True
assert func_has_positional_args(test2) == False
assert func_has_keyword_args(test2) == False

# Missing functionality (tests are failing):
assert func_has_positional_args(test3) == True
assert func_has_keyword_args(test3) == False

Also, does Python 3 change anything with regard to this functionality or its behavior? 此外,Python 3是否会针对此功能或其行为进行更改?

As mgilson commented, use inspect.getargspec (more preferably inspect.getfullargspec in Python 3.x). 正如mgilson所评论的那样,使用inspect.getargspec (更好的是Python 3.x中的inspect.getfullargspec )。

import inspect

def func_has_positional_args(func):
    spec = inspect.getfullargspec(func)
    return bool(spec.varargs) # varargs: name of the * argument or None
def func_has_keyword_args(func):
    spec = inspect.getfullargspec(func)
    return bool(spec.varkw)   # varkw: name of the ** argument or None

Example: 例:

>>> def test1(a, b, *args, **kwargs): pass
...
>>> def test2(a, b): pass
...
>>> def test3(a, b, *args): pass
...
>>> func_has_positional_args(test1)
True
>>> func_has_keyword_args(test1)
True
>>> func_has_positional_args(test2)
False
>>> func_has_keyword_args(test2)
False
>>> func_has_positional_args(test3)
True
>>> func_has_keyword_args(test3)
False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM