Python功能的部分包装器

发布于 2025-01-21 05:40:08 字数 504 浏览 3 评论 0原文

给定以下代码,

def myfunc(a=None, b=None, c=None, **kw):
    func(arga=a, argb=b, **kw)
    #do something with c

def func(arga=None, argb=None, argc=None):
    ....

我可以复制func的签名的一部分,即丢失的args,而无需模仿func的每个缺失的ARG?

简而言之,我想在myfunc的关键字中查看argc,以使myfunc?会有所不同。它将包含ARGC。 myFunc(a = none,b = none,c = none,argc = none)

@functools.wraps允许包装完整的功能。使用部分可以减去args。但不知道要添加。

Given the following code,

def myfunc(a=None, b=None, c=None, **kw):
    func(arga=a, argb=b, **kw)
    #do something with c

def func(arga=None, argb=None, argc=None):
    ....

Can I replicate part of the signature of func, namely the missing args, without imitating every missing arg of func manually?

Put it more simply, I want to see argc in keywords of myfunc such that myfunc? would be different. It would contain argc. myfunc(a=None,b=None,c=None,argc=None)

@functools.wraps allows for wrapping a complete functions. Using partial can subtract args. But don't know to add.

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

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

发布评论

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

评论(2

绳情 2025-01-28 05:40:08

是的,虽然不是微不足道的 -
Python的内省功能允许您检查目标函数声明的所有参数,并且可以以编程方式构建一个新功能,该功能将自动包含这些属性。

I have written this for a project of mine, and had exposed the relevant code as my answer here:

我不会将其标记为重复,因为另一个问题更担心记录新功能。

如果您想尝试使用代码,也许使用更简单的内容,则可以检查ixply.signature从标准库中调用,这使人们可以发现有关参数和默认参数的所有内容目标功能。

通过此信息构建新功能有点棘手,但可能会遇到 - 但始终可以诉诸exec调用,该调用可以从字符串模板中创建一个新功能。那里的答案遵循这一行。

yes, it is possible, though not trivial -
Python's introspection capabilities allow you to check all parameters the target function declares, and it is possible to build a new function programmatically that will include those attributes automatically.

I have written this for a project of mine, and had exposed the relevant code as my answer here: Signature-changing decorator: properly documenting additional argument

I will not mark this as duplicate, since the other question is more worried about documenting the new function.

If you want to give a try, with your code, maybe with something simpler, you can check the inspect.signature call from the standard library, which allows one to discover everything about parameters and default arguments of the target function.

Building a new function from this information is a bit more tricky, but possible - but one can always resort to a exec call which will can create a new function from a string template. The answer there follows this line.

无法言说的痛 2025-01-28 05:40:08

我不确定这里还要求什么,但是我在这里有functools.partial的替代代码可能会适用???

(编辑>)
此处的区别与部分mkcall参数是字符串,而不是一系列参数。然后,可以根据需要在调用目标功能之前根据需要的任何适当要求对该字符串进行格式和分析。
(<编辑)

def mkcall(fs, globals=None,locals=None):
    class func:
        def __init__(f,fcnm=None,params=None,globals=None,locals=None):
            f.nm = fcnm
            f.pm = params
            f.globals = globals
            f.locals = locals

        def __call__(f):
            s = f.nm + f.pm
            eval(s,f.globals,f.locals)    

    if '(' in fs:
        funcn,lbr,r = fs.partition('(')
        tp = lbr + r
        newf = func(funcn,tp,globals,locals)
        callf = newf.__call__
    else:
        callf = eval(fs,globals,locals)
    return callf

#call examples
# mkcall("func(arg)")
# mkcall("func")

I'm not sure what is being asked here either but I have here alternative code to functools.partial that might be adapted ???

(edit>)
The difference here from partial is that the mkcall argument is a string rather than a series of arguments. This string can then be formatted and analysed according to whatever appropriate requirements are needed before the target function is called.
(<edit)

def mkcall(fs, globals=None,locals=None):
    class func:
        def __init__(f,fcnm=None,params=None,globals=None,locals=None):
            f.nm = fcnm
            f.pm = params
            f.globals = globals
            f.locals = locals

        def __call__(f):
            s = f.nm + f.pm
            eval(s,f.globals,f.locals)    

    if '(' in fs:
        funcn,lbr,r = fs.partition('(')
        tp = lbr + r
        newf = func(funcn,tp,globals,locals)
        callf = newf.__call__
    else:
        callf = eval(fs,globals,locals)
    return callf

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