从函数签名中删除所有绑定参数。

huangapple go评论93阅读模式
英文:

Remove all bound arguments from function signature?

问题

Here's the translation of the code part you provided:

  1. inspect 模块导入 signature
  2. functools 模块导入 partial
  3. def remove_default_args(func, **kwargs):
  4. func = partial(func, **kwargs)
  5. sg = signature(func)
  6. unbound_parameters =

  7. func.__signature__ = sg.replace(parameters=unbound_parameters)

  8. return func

Let me know if you need any further assistance with this code.

英文:

Say, we have a function foo, which takes another function bar as an argument and does something based on bar's signature, and, sadly, throws an error when bar has default arguments. I want to use foo with functions, say, bar1,bar2,...bar100, all of which have default arguments. All of the aforementioned functions are from an outer library which I cannot change. There are too many bars to rewrite each of them as a lambda statement. The only way is to remove all bound arguments from these functions. And there's exec, but I'm not that desperate yet.

I know there's a module called inspect in python which works with function signatures, I came up with the following code:

  1. from inspect import signature
  2. from functools import partial
  3. def remove_default_args(func, **kwargs):
  4. func = partial(func, **kwargs)
  5. sg = signature(func)
  6. unbound_parameters =

  7. func.__signature__ = sg.replace(parameters=unbound_parameters)

  8. return func

Is there any better way to do this, without inspect? I ask since I know inspect is generally frowned upon, as it only changes the metadata.

答案1

得分: 1

Default argument values are stored in the __defaults__ and __kwdefaults__ attributes, which are writable. You can simply set them "back" to None to remove all defaults.

  1. >>> def foo(x=3, *, y=5):
  2. ... pass
  3. ...
  4. >>> foo()
  5. >>> foo.__defaults__, foo.__kwdefaults__
  6. ((3,), {'y': 5})
  7. >>> foo.__defaults__ = None
  8. >>> foo.__kwdefaults__ = None
  9. >>> foo()
  10. Traceback (most recent call last):
  11. File "<stdin>", line 1, in <module>
  12. TypeError: foo() missing 1 required positional argument: 'x'
英文:

Default argument values are stored in the __defaults__ and __kwdefaults__ attributes, which are writable. You can simply set them "back" to None to remove all defaults.

  1. &gt;&gt;&gt; def foo(x=3, *, y=5):
  2. ... pass
  3. ...
  4. &gt;&gt;&gt; foo()
  5. &gt;&gt;&gt; foo.__defaults__, foo.__kwdefaults__
  6. ((3,), {&#39;y&#39;: 5})
  7. &gt;&gt;&gt; foo.__defaults__ = None
  8. &gt;&gt;&gt; foo.__kwdefaults__ = None
  9. &gt;&gt;&gt; foo()
  10. Traceback (most recent call last):
  11. File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  12. TypeError: foo() missing 1 required positional argument: &#39;x&#39;

huangapple
  • 本文由 发表于 2023年5月11日 01:58:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定