如何在Python 2.7中为包装函数添加关键字参数?

问题描述 投票:5回答:2

我首先要强调的是,我已经非常广泛地搜索了Web和Python文档+ StackOverflow,并且没有设法找到这个问题的答案。我还要感谢任何花时间阅读本文的人。

正如标题所示,我正在用Python编写装饰器,我希望它为包装函数添加关键字参数(请注意:我知道如何向装饰器本身添加参数,这不是我要求的)。

下面是我编写的一段代码的工作示例,它完全适用于Python 3(特别是Python 3.5)。它使用装饰器参数,为包装函数添加关键字参数,还定义并向包装函数添加新函数。

from functools import wraps

def my_decorator(decorator_arg1=None, decorator_arg2=False):
    # Inside the wrapper maker

    def _decorator(func):
        # Do Something 1

        @wraps(func)
        def func_wrapper(
                *args,
                new_arg1=False,
                new_arg2=None,
                **kwds):
            # Inside the wrapping function
            # Calling the wrapped function
            if new_arg1:
                return func(*args, **kwds)
            else:
                # do something with new_arg2
                return func(*args, **kwds)

        def added_function():
            print("Do Something 2")

        func_wrapper.added_function = added_function
        return func_wrapper

    return _decorator

现在这个装饰器可以按以下方式使用:

@my_decorator(decorator_arg1=4, decorator_arg2=True)
def foo(a, b):
    print("a={}, b={}".format(a,b))

def bar():
    foo(a=1, b=2, new_arg1=True, new_arg2=7)
    foo.added_function()

现在,虽然这适用于Python 3.5(我假设任何3.x),但我还没有设法让它适用于Python 2.7。我在第一行尝试为SyntaxError: invalid syntax定义一个新的关键字参数的func_wrapper,意思是在导入包含此代码的模块时说明new_arg1=False,的行。

将新关键字移动到func_wrapper的参数列表的开头,解决了SyntaxError,但似乎与包装函数的签名相混淆;我现在在调用TypeError: foo() takes exactly 2 arguments (0 given)时得到错误foo(1, 2)。如果我明确地分配参数,如foo(a=1, b=2),这个错误就会消失,但这显然是不够的 - 不出所料,我的新关键字参数似乎是“窃取”发送给包装函数的前两个位置参数。这是Python 3没有发生的事情。

我很想得到你的帮助。感谢您抽出时间来阅读。

吉文

python-2.7 decorator python-decorators functools
2个回答
4
投票

如果您只将其他参数指定为关键字,则可以将它们从kw字典中删除(参见下文)。如果你需要它们作为位置AND关键字参数,那么我认为你应该能够在原始函数上使用inspect.getargspec,然后在func_wrapper中处理args和kw。

下面的代码在Ubuntu 14.04上使用Python 2.7,3.4(Ubuntu提供)和3.5(来自Continuum)进行了测试。

from functools import wraps

def my_decorator(decorator_arg1=None, decorator_arg2=False):
    # Inside the wrapper maker

    def _decorator(func):
        # Do Something 1
        @wraps(func)
        def func_wrapper(
                *args,
                **kwds):
            # new_arg1, new_arg2 *CANNOT* be positional args with this technique
            new_arg1 = kwds.pop('new_arg1',False)
            new_arg2 = kwds.pop('new_arg2',None)
            # Inside the wrapping function
            # Calling the wrapped function
            if new_arg1:
                print("new_arg1 True branch; new_arg2 is {}".format(new_arg2))
                return func(*args, **kwds)
            else:
                print("new_arg1 False branch; new_arg2 is {}".format(new_arg2))
                # do something with new_arg2
                return func(*args, **kwds)

        def added_function():
            # Do Something 2
            print('added_function')

        func_wrapper.added_function = added_function
        return func_wrapper

    return _decorator

@my_decorator(decorator_arg1=4, decorator_arg2=True)
def foo(a, b):
    print("a={}, b={}".format(a,b))

def bar():
    pass
    #foo(1,2,True,7) # won't work
    foo(1, 2, new_arg1=True, new_arg2=7)
    foo(a=3, b=4, new_arg1=False, new_arg2=42)
    foo(new_arg2=-1,b=100,a='AAA')
    foo(b=100,new_arg1=True,a='AAA')
    foo.added_function()

if __name__=='__main__':
    import sys
    sys.stdout.flush()
    bar()

输出是

new_arg1 True branch; new_arg2 is 7
a=1, b=2
new_arg1 False branch; new_arg2 is 42
a=3, b=4
new_arg1 False branch; new_arg2 is -1
a=AAA, b=100
new_arg1 True branch; new_arg2 is None
a=AAA, b=100
added_function

0
投票

要向现有函数的签名添加参数,同时使该函数的行为类似于普通的python函数(正确的帮助,签名和TypeError在提供错误参数的情况下提升),您可以使用makefun,我专门开发它来解决此用例。

特别是makefun@wraps提供了替代,new_sig具有try: # python 3.3+ from inspect import signature, Parameter except ImportError: from funcsigs import signature, Parameter from makefun import wraps def my_decorator(decorator_arg1=None, decorator_arg2=False): # Inside the wrapper maker def _decorator(func): # (1) capture the signature of the function to wrap ... func_sig = signature(func) # ... and modify it to add new optional parameters 'new_arg1' and 'new_arg2'. # (if they are optional that's where you provide their defaults) new_arg1 = Parameter('new_arg1', kind=Parameter.POSITIONAL_OR_KEYWORD, default=False) new_arg2 = Parameter('new_arg2', kind=Parameter.POSITIONAL_OR_KEYWORD, default=None) new_sig = add_signature_parameters(func_sig, last=[new_arg1, new_arg2]) # (2) create a wrapper with the new signature @wraps(func, new_sig=new_sig) def func_wrapper(*args, **kwds): # Inside the wrapping function # Pop the extra args (they will always be there, no need to provide default) new_arg1 = kwds.pop('new_arg1') new_arg2 = kwds.pop('new_arg2') # Calling the wrapped function if new_arg1: print("new_arg1 True branch; new_arg2 is {}".format(new_arg2)) return func(*args, **kwds) else: print("new_arg1 False branch; new_arg2 is {}".format(new_arg2)) # do something with new_arg2 return func(*args, **kwds) # (3) add an attribute to the wrapper def added_function(): # Do Something 2 print('added_function') func_wrapper.added_function = added_function return func_wrapper return _decorator @my_decorator(decorator_arg1=4, decorator_arg2=True) def foo(a, b): """This is my foo function""" print("a={}, b={}".format(a,b)) foo(1, 2, True, 7) # works, except if you use kind=Parameter.KEYWORD_ONLY above (py3 only) foo(1, 2, new_arg1=True, new_arg2=7) foo(a=3, b=4, new_arg1=False, new_arg2=42) foo(new_arg2=-1,b=100,a='AAA') foo(b=100,new_arg1=True,a='AAA') foo.added_function() help(foo) 参数,您可以在其中指定新签名。以下是您的示例的编写方式:

new_arg1 True branch; new_arg2 is 7
a=1, b=2
new_arg1 True branch; new_arg2 is 7
a=1, b=2
new_arg1 False branch; new_arg2 is 42
a=3, b=4
new_arg1 False branch; new_arg2 is -1
a=AAA, b=100
new_arg1 True branch; new_arg2 is None
a=AAA, b=100
added_function
Help on function foo in module <...>:

foo(a, b, new_arg1=False, new_arg2=None)
    This is my foo function

它按预期工作:

kind=Parameter.KEYWORD_ONLY

因此,您可以看到公开的签名是预期的,并且您的用户看不到内部。请注意,您可以通过在新签名中设置decopatch来使两个新参数“仅限关键字”,但是您已经知道这在python 2中不起作用。

最后,您可能有兴趣使用from decopatch import function_decorator, DECORATED @function_decorator def my_decorator(decorator_arg1=None, decorator_arg2=False, func=DECORATED): # (1) capture the signature of the function to wrap ... func_sig = signature(func) # ... # (2) create a wrapper with the new signature @wraps(func, new_sig=new_sig) def func_wrapper(*args, **kwds): # Inside the wrapping function ... # (3) add an attribute to the wrapper def added_function(): # Do Something 2 print('added_function') func_wrapper.added_function = added_function return func_wrapper 使装饰器代码对于无括号用法更具可读性和鲁棒性。除此之外,它支持“扁平”风格,非常适合您的情况,因为它删除了一个级别的嵌套:

qazxswpoi

(我也是这个的作者,因为我厌倦了嵌套和无括号处理而创建了它)

© www.soinside.com 2019 - 2024. All rights reserved.