有没有办法在不调用 TypeError 的情况下调用参数数量错误的 Python 函数?

发布于 2024-07-21 15:07:23 字数 559 浏览 6 评论 0原文

当您使用错误数量的参数或使用不在其定义中的关键字参数调用函数时,您会收到 TypeError。 我想要一段代码来接受回调并根据回调支持的内容使用变量参数调用它。 一种方法是,对于回调cb,使用cb.__code__.cb_argcountcb.__code__.co_varnames,但我宁愿将其抽象为诸如 apply 之类的东西,但这仅适用于“适合”的参数。

例如:

 def foo(x,y,z):
   pass

 cleanvoke(foo, 1)         # should call foo(1, None, None)
 cleanvoke(foo, y=2)       # should call foo(None, 2, None)
 cleanvoke(foo, 1,2,3,4,5) # should call foo(1, 2, 3)
                           # etc.

Python 中已经有类似的东西吗?还是我应该从头开始编写?

When you invoke a function with the wrong number of arguments, or with a keyword argument that isn't in its definition, you get a TypeError. I'd like a piece of code to take a callback and invoke it with variable arguments, based on what the callback supports. One way of doing it would be to, for a callback cb, use cb.__code__.cb_argcount and cb.__code__.co_varnames, but I would rather abstract that into something like apply, but that only applies the arguments which "fit".

For example:

 def foo(x,y,z):
   pass

 cleanvoke(foo, 1)         # should call foo(1, None, None)
 cleanvoke(foo, y=2)       # should call foo(None, 2, None)
 cleanvoke(foo, 1,2,3,4,5) # should call foo(1, 2, 3)
                           # etc.

Is there anything like this already in Python, or is it something I should write from scratch?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美煞众生 2024-07-28 15:07:23

您可以检查函数的签名,而不是自己深入研究细节 - 您可能需要inspect.getargspec(cb)

我并不完全清楚您想要如何使用该信息以及您拥有的参数来“正确”调用该函数。 为简单起见,假设您只关心简单的命名参数,并且您想要传递的值位于 dict d 中...

args = inspect.getargspec(cb)[0]
cb( **dict((a,d.get(a)) for a in args) )

也许您想要一些更奇特的东西,并且可以详细说明到底是什么?

Rather than digging down into the details yourself, you can inspect the function's signature -- you probably want inspect.getargspec(cb).

Exactly how you want to use that info, and the args you have, to call the function "properly", is not completely clear to me. Assuming for simplicity that you only care about simple named args, and the values you'd like to pass are in dict d...

args = inspect.getargspec(cb)[0]
cb( **dict((a,d.get(a)) for a in args) )

Maybe you want something fancier, and can elaborate on exactly what?

情定在深秋 2024-07-28 15:07:23

这也许?

def fnVariableArgLength(*args, **kwargs):
    """
    - args is a list of non keywords arguments
    - kwargs is a dict of keywords arguments (keyword, arg) pairs
    """
    print args, kwargs


fnVariableArgLength() # () {}
fnVariableArgLength(1, 2, 3) # (1, 2, 3) {}
fnVariableArgLength(foo='bar') # () {'foo': 'bar'}
fnVariableArgLength(1, 2, 3, foo='bar') # (1, 2, 3) {'foo': 'bar'}

编辑您的用例

def foo(*args,*kw):
    x= kw.get('x',None if len(args) < 1 else args[0])
    y= kw.get('y',None if len(args) < 2 else args[1])
    z= kw.get('z',None if len(args) < 3 else args[2])
    # the rest of foo

foo(1)         # should call foo(1, None, None)
foo(y=2)       # should call foo(None, 2, None)
foo(1,2,3,4,5) # should call foo(1, 2, 3)

This maybe?

def fnVariableArgLength(*args, **kwargs):
    """
    - args is a list of non keywords arguments
    - kwargs is a dict of keywords arguments (keyword, arg) pairs
    """
    print args, kwargs


fnVariableArgLength() # () {}
fnVariableArgLength(1, 2, 3) # (1, 2, 3) {}
fnVariableArgLength(foo='bar') # () {'foo': 'bar'}
fnVariableArgLength(1, 2, 3, foo='bar') # (1, 2, 3) {'foo': 'bar'}

Edit Your use cases

def foo(*args,*kw):
    x= kw.get('x',None if len(args) < 1 else args[0])
    y= kw.get('y',None if len(args) < 2 else args[1])
    z= kw.get('z',None if len(args) < 3 else args[2])
    # the rest of foo

foo(1)         # should call foo(1, None, None)
foo(y=2)       # should call foo(None, 2, None)
foo(1,2,3,4,5) # should call foo(1, 2, 3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文