10

我有一个接受 args 和 kwargs 的函数,我需要根据函数中第二个arg 的值在我的装饰器中做一些事情,如下面的代码:

def workaround_func():
    def decorator(fn):
        def case_decorator(*args, **kwargs):
            if args[1] == 2:
                print('The second argument is a 2!')
            return fn(*args, **kwargs)
        return case_decorator
    return decorator

@workaround_func()
def my_func(arg1, arg2, kwarg1=None):
    print('arg1: {} arg2: {}, kwargs: {}'.format(arg1, arg2, kwarg1))

问题是 python 允许用户使用第二个参数作为常规参数或关键字参数调用函数,所以如果用户调用my_funcwitharg2作为 kwarg,它会引发一个IndexError,见下文:

In [8]: d.my_func(1, 2, kwarg1=3)
The second argument is a 2!
arg1: 1 arg2: 2, kwargs: 3

In [9]: d.my_func(1, arg2=2, kwarg1=3)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-9-87dc89222a9e> in <module>()
----> 1 d.my_func(1, arg2=2, kwarg1=3)

/home/camsparr/decoratorargs.py in case_decorator(*args, **kwargs)
      2     def decorator(fn):
      3         def case_decorator(*args, **kwargs):
----> 4             if args[1] == 2:
      5                 print('The second argument is a 2!')
      6             return fn(*args, **kwargs)

IndexError: tuple index out of range

有没有办法解决这个问题,而不仅仅是做 atry/except并抓住IndexError

4

2 回答 2

10

decorator我使用 python包找到了答案。这个包的一个特点是,无论用户如何传递它们,它都会保留位置/关键字参数。它具有减少大量代码的额外好处,所以我的原始代码:

def workaround_func():
    def decorator(fn):
        def case_decorator(*args, **kwargs):
            if args[1] == 2:
                print('The second argument is a 2!')
            return fn(*args, **kwargs)
        return case_decorator
    return decorator

@workaround_func()
def my_func(arg1, arg2, kwarg1=None):
    print('arg1: {} arg2: {}, kwargs: {}'.format(arg1, arg2, kwarg1))

变成:

from decorator import decorator

@decorator
def workaround_decorator(f, *args, **kwargs):
    if args[1] == 2:
        print('The second argument is 2!')
    return f(*args, **kwargs)

@workaround_decorator
def my_func(arg1, arg2, kwarg1=None):
    print('arg1: {} arg2: {}, kwargs: {}'.format(arg1, arg2, kwarg1))
于 2013-09-20T15:25:44.740 回答
6

这是我能想到的最可靠的处理方式……诀窍是检查第二个参数的名称。然后,在装饰器中,您检查该名称是否存在于kwargs. 如果是,那么您就使用它。如果没有,那么您使用args.

from inspect import getargspec    

def decorate(fn):
    argspec = getargspec(fn)
    second_argname = argspec[0][1]
    def inner(*args, **kwargs):
        special_value = (kwargs[second_argname] 
                         if second_argname in kwargs else args[1])
        if special_value == 2:
            print "foo"
        else:
            print "no foo for you"
        return fn(*args, **kwargs)
    return inner

@decorate
def foo(a, b, c=3):
    pass

foo(1,2,3)
foo(1,b=2,c=4)
foo(1,3,5)
foo(1,b=6,c=5)

运行此结果:

foo
foo
no foo for you
no foo for you

正如预期的那样。

于 2013-09-20T00:00:49.243 回答