python确定关键字函数_内省函数,以确定使用了哪个参数解包(位置或关键字)...

我正在寻找一种方法来确定是否有一些参数用于解包,我发现了:>>> 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 "", line 1, in

func_has_positional_args(test3)

File "", line 9, in func_has_positional_args

raise NotImplementedError('Unable to tell')

NotImplementedError: Unable to tell

所以我可以判断,如果没有位置参数,也没有关键字参数解包。我还可以判断是否两者都有,但如果只有一个“通配符”,我就无法区分实现了哪个“通配符”类型参数。在

你能帮我达到以下结果吗?在

^{pr2}$

另外,Python3是否会对该功能或其行为进行任何更改?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值