你能列出函数接收的关键字参数吗?
我有一个字典,我需要将键/值作为关键字参数传递。例如......
d_args = {'kw1': 'value1', 'kw2': 'value2'}
example(**d_args)
这工作正常,但是如果 d_args 字典中存在 example
函数,它显然死了.. 比如说,如果示例函数被定义为 def example(kw2):
这是一个问题,因为我不控制d_args
或 example
函数。它们都来自外部模块,并且 example
只接受字典中的一些关键字参数。理想
情况下,
parsed_kwargs = feedparser.parse(the_url)
valid_kwargs = get_valid_kwargs(parsed_kwargs, valid_for = PyRSS2Gen.RSS2)
PyRSS2Gen.RSS2(**valid_kwargs)
我可能只是从有效关键字参数列表中过滤字典,但我想知道:是否有一种方法可以以编程方式列出特定函数所采用的关键字参数? >
I have a dict, which I need to pass key/values as keyword arguments.. For example..
d_args = {'kw1': 'value1', 'kw2': 'value2'}
example(**d_args)
This works fine, but if there are values in the d_args dict that are not accepted by the example
function, it obviously dies.. Say, if the example function is defined as def example(kw2):
This is a problem since I don't control either the generation of the d_args
, or the example
function.. They both come from external modules, and example
only accepts some of the keyword-arguments from the dict..
Ideally I would just do
parsed_kwargs = feedparser.parse(the_url)
valid_kwargs = get_valid_kwargs(parsed_kwargs, valid_for = PyRSS2Gen.RSS2)
PyRSS2Gen.RSS2(**valid_kwargs)
I will probably just filter the dict, from a list of valid keyword-arguments, but I was wondering: Is there a way to programatically list the keyword arguments the a specific function takes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
比直接检查代码对象并计算变量更好一点的是使用检查模块。
如果您想知道它是否可以使用一组特定的参数进行调用,则需要未指定默认值的参数。 这些可以通过以下方式获得:
然后,一个告诉您特定字典中缺少什么的函数是:
类似地,要检查无效参数,请使用:
因此,如果它可调用,则完整的测试是:(
这仅在以下情况下才有效python 的 arg 解析。显然无法检测到
kwargs
中的任何运行时检查。)A little nicer than inspecting the code object directly and working out the variables is to use the inspect module.
If you want to know if its callable with a particular set of args, you need the args without a default already specified. These can be got by:
Then a function to tell what you are missing from your particular dict is:
Similarly, to check for invalid args, use:
And so a full test if it is callable is :
(This is good only as far as python's arg parsing. Any runtime checks for invalid values in
kwargs
obviously can't be detected.)这将打印所有可传递参数的名称,关键字和非关键字参数:
这是因为第一个
co_varnames
始终是参数(接下来是局部变量,如上例中的y
)。所以现在你可以有一个函数:
然后你可以像这样使用它:
如果你真的只需要函数的关键字参数,你可以使用
func_defaults
属性来提取它们:您现在可以使用已知的参数调用函数,但提取了 kwargs,例如:
这假设
func
不使用*args
或**kwargs
它的签名充满魔力。This will print names of all passable arguments, keyword and non-keyword ones:
This is because first
co_varnames
are always parameters (next are local variables, likey
in the example above).So now you could have a function:
Which you then could use like this:
if you really need only keyword arguments of a function, you can use the
func_defaults
attribute to extract them:You could now call your function with known args, but extracted kwargs, e.g.:
This assumes that
func
uses no*args
or**kwargs
magic in its signature.对于 Python 3 解决方案,您可以使用
inspect.signature
并根据 参数类型进行过滤< /a> 你想知道的。
获取带有位置或关键字、仅关键字、var 位置和 var 关键字参数的示例函数:
您可以为其创建一个签名对象:
然后使用列表理解进行过滤以找出您需要的详细信息:
同样,对于 var使用
p.VAR_POSITIONAL
的位置和使用VAR_KEYWORD
的 var 关键字。此外,您可以在 if 中添加一个子句,通过检查
p.default
是否等于p.empty
来检查是否存在默认值。For a Python 3 solution, you can use
inspect.signature
and filter according to the kind of parameters you'd like to know about.Taking a sample function with positional or keyword, keyword-only, var positional and var keyword parameters:
You can create a signature object for it:
and then filter with a list comprehension to find out the details you need:
and, similarly, for var positionals using
p.VAR_POSITIONAL
and var keyword withVAR_KEYWORD
.In addition, you can add a clause to the if to check if a default value exists by checking if
p.default
equalsp.empty
.在Python 3.0中:
In Python 3.0:
只需将其用作函数名称“myfun”:
Just use this for a function name 'myfun':
扩展 DzinX 的答案:
Extending DzinX's answer:
没有经过太多测试,但这适用于我的情况:
Not tested a lot but this will work for my case: