你可以列出关键字参数一个Python函数接收? [英] Can you list the keyword arguments a Python function receives?

查看:245
本文介绍了你可以列出关键字参数一个Python函数接收?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的字典,这是我需要传递键/值作为关键字参数。例如..

  d_args = {'KW1':'值1','KW2':'值'}
例如(** d_args)

这工作得很好,的的如果有在d_args字典未由例如函数接受的价值观念,它显然死..再说了,如果示例函数定义为高清例如(KW2):

这是一个问题,因为我不控制 d_args 例如函数的任何一代..他们都来自外部模块,和例如只接受一些来自字典..

的关键字参数

在理想情况下,我只想做

  parsed_kwargs = feedparser.parse(THE_URL)
valid_kwargs = get_valid_kwargs(parsed_kwargs,VALID_FOR = PyRSS2Gen.RSS2)
PyRSS2Gen.RSS2(** valid_kwargs)

我可能只是过滤字典,从有效的关键字参数的列表,但我想知道:有没有一种方法以编程方式列出关键字参数的特定功能需要

解决方案

不是直接检查code对象,并制定出变量更好一点是使用检查模块。

 >>>进口检查
>>> DEF FUNC(A,B,C = 42,* ARGS,** kwargs):通
>>> inspect.getargspec(FUNC)
(['一','B','C'],'ARGS','kwargs',(42))

如果你想知道它是否与调用一组特定参数的个数,你需要的不ARGS已指定一个默认的。这些可以通过了:

 高清getRequiredArgs(FUNC):
    ARGS,可变参数,varkw,默认= inspect.getargspec(FUNC)
    如果默认值:
        ARGS = ARGS [: - LEN(默认)
    返回ARGS#*指定参数和** kwargs不是必需的,所以忽略它们。

然后一个函数来告诉你,从你的字典尤其缺少的是:

 高清missingArgs(FUNC,argdict):
    返回设置(getRequiredArgs(FUNC))。差(argdict)

同样,要检查是否有无效指定参数时,使用方法:

 高清invalidArgs(FUNC,argdict):
    ARGS,可变参数,varkw,默认= inspect.getargspec(FUNC)
    如果varkw:返回的集合()#全部接受
    返回设置(argdict) - 设置(参数)

所以,一个完整的测试,如果它是可调用的是:

 高清isCallableWithArgs(FUNC,argdict):
    返回不missingArgs(FUNC,argdict),而不是invalidArgs(FUNC,argdict)

(这是好事,只有尽可能Python的ARG解析。无法检测到的任何运行时检查在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?

解决方案

A little nicer than inspecting the code object directly and working out the variables is to use the inspect module.

>>> import inspect
>>> def func(a,b,c=42, *args, **kwargs): pass
>>> inspect.getargspec(func)
(['a', 'b', 'c'], 'args', 'kwargs', (42,))

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:

def getRequiredArgs(func):
    args, varargs, varkw, defaults = inspect.getargspec(func)
    if defaults:
        args = args[:-len(defaults)]
    return args   # *args and **kwargs are not required, so ignore them.

Then a function to tell what you are missing from your particular dict is:

def missingArgs(func, argdict):
    return set(getRequiredArgs(func)).difference(argdict)

Similarly, to check for invalid args, use:

def invalidArgs(func, argdict):
    args, varargs, varkw, defaults = inspect.getargspec(func)
    if varkw: return set()  # All accepted
    return set(argdict) - set(args)

And so a full test if it is callable is :

def isCallableWithArgs(func, argdict):
    return not missingArgs(func, argdict) and not invalidArgs(func, argdict)

(This is good only as far as python's arg parsing. Any runtime checks for invalid values in kwargs obviously can't be detected.)

这篇关于你可以列出关键字参数一个Python函数接收?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆