19

我一直在尝试创建一个可以与 python 中的函数和方法一起使用的装饰器。这本身并不难,但是当创建一个接受参数的装饰器时,它似乎是。

class methods(object):
    def __init__(self, *_methods):
        self.methods = _methods

    def __call__(self, func): 
        def inner(request, *args, **kwargs):
            print request
            return func(request, *args, **kwargs)
        return inner

    def __get__(self, obj, type=None):
        if obj is None:
            return self
        new_func = self.func.__get__(obj, type)
        return self.__class__(new_func)

上面的代码正确地包装了函数/方法,但在方法的情况下,request参数是它正在操作的实例,而不是第一个非 self 参数。

有没有办法判断装饰器是否应用于函数而不是方法,并进行相应处理?

4

5 回答 5

20

扩展__get__方法。这可以概括为一个装饰器装饰器。

class _MethodDecoratorAdaptor(object):
    def __init__(self, decorator, func):
        self.decorator = decorator
        self.func = func
    def __call__(self, *args, **kwargs):
        return self.decorator(self.func)(*args, **kwargs)
    def __get__(self, instance, owner):
        return self.decorator(self.func.__get__(instance, owner))

def auto_adapt_to_methods(decorator):
    """Allows you to use the same decorator on methods and functions,
    hiding the self argument from the decorator."""
    def adapt(func):
        return _MethodDecoratorAdaptor(decorator, func)
    return adapt

通过这种方式,您可以让您的装饰器自动适应它的使用条件。

def allowed(*allowed_methods):
    @auto_adapt_to_methods
    def wrapper(func):
        def wrapped(request):
            if request not in allowed_methods:
                raise ValueError("Invalid method %s" % request)
            return func(request)
        return wrapped
    return wrapper

请注意,包装函数在所有函数调用上都会被调用,所以不要在那里做任何昂贵的事情。

装饰器的用法:

class Foo(object):
    @allowed('GET', 'POST')
    def do(self, request):
        print "Request %s on %s" % (request, self)

@allowed('GET')
def do(request):
    print "Plain request %s" % request

Foo().do('GET')  # Works
Foo().do('POST') # Raises
于 2009-08-17T16:15:33.957 回答
5

装饰器始终应用于函数对象——让装饰器print具有其参数的类型,您将能够确认这一点;它通常也应该返回一个函数对象(它已经是一个带有正确的装饰器__get__!-),尽管后者有例外。

即,在代码中:

class X(object):

  @deco
  def f(self): pass

deco(f)在类体内被调用,并且,当你还在的时候,f它是一个函数,而不是方法类型的实例。(该方法在f's中制造并返回,__get__当稍后f作为其属性X或其实例访问时)。

也许您可以更好地解释您想要用于装饰者的玩具用途,以便我们可以提供更多帮助...?

编辑:这也适用于带参数的装饰器,即

class X(object):

  @deco(23)
  def f(self): pass

那么它deco(23)(f)在类主体中f被调用,当作为参数传递给任何可调用deco(23)返回时仍然是一个函数对象,并且该可调用仍然应该返回一个函数对象(通常 - 有异常;-)。

于 2009-08-17T15:16:03.843 回答
5

由于您已经定义了__get__在绑定方法上使用您的装饰器,您可以传递一个标志来告诉它是否正在用于方法或函数。

class methods(object):
    def __init__(self, *_methods, called_on_method=False):
        self.methods = _methods
        self.called_on_method

    def __call__(self, func):
        if self.called_on_method:
            def inner(self, request, *args, **kwargs):
                print request
                return func(request, *args, **kwargs)
        else:
            def inner(request, *args, **kwargs):
                print request
                return func(request, *args, **kwargs)
        return inner

    def __get__(self, obj, type=None):
        if obj is None:
            return self
        new_func = self.func.__get__(obj, type)
        return self.__class__(new_func, called_on_method=True)
于 2009-08-17T15:24:01.223 回答
2

我提出的部分(特定)解决方案依赖于异常处理。我正在尝试创建一个装饰器以仅允许某些 HttpRequest 方法,但使其适用于作为视图的函数和作为视图的方法。

所以,这门课会做我想做的事:

class methods(object):
    def __init__(self, *_methods):
        self.methods = _methods

    def __call__(self, func): 
        @wraps(func)
        def inner(*args, **kwargs):
            try:
                if args[0].method in self.methods:
                    return func(*args, **kwargs)
            except AttributeError:
                if args[1].method in self.methods:
                    return func(*args, **kwargs)
            return HttpResponseMethodNotAllowed(self.methods)
        return inner

以下是两个用例: 装饰函数:

@methods("GET")
def view_func(request, *args, **kwargs):
    pass

和装饰类的方法:

class ViewContainer(object):
    # ...

    @methods("GET", "PUT")
    def object(self, request, pk, *args, **kwargs):
        # stuff that needs a reference to self...
        pass

有比使用异常处理更好的解决方案吗?

于 2009-08-17T15:28:51.560 回答
2

这是我发现检测修饰的可调用对象是函数还是方法的一般方法:

import functools

class decorator(object):

  def __init__(self, func):
    self._func = func
    self._obj = None
    self._wrapped = None

  def __call__(self, *args, **kwargs):
    if not self._wrapped:
      if self._obj:
        self._wrapped = self._wrap_method(self._func)
        self._wrapped = functools.partial(self._wrapped, self._obj)
      else:
        self._wrapped = self._wrap_function(self._func)
    return self._wrapped(*args, **kwargs)

  def __get__(self, obj, type=None):
    self._obj = obj
    return self

  def _wrap_method(self, method):
    @functools.wraps(method)
    def inner(self, *args, **kwargs):
      print('Method called on {}:'.format(type(self).__name__))
      return method(self, *args, **kwargs)
    return inner

  def _wrap_function(self, function):
    @functools.wraps(function)
    def inner(*args, **kwargs):
      print('Function called:')
      return function(*args, **kwargs)
    return inner

示例用法:

class Foo(object):
  @decorator
  def foo(self, foo, bar):
    print(foo, bar)

@decorator
def foo(foo, bar):
  print(foo, bar)

foo(12, bar=42)      # Function called: 12 42
foo(12, 42)          # Function called: 12 42
obj = Foo()
obj.foo(12, bar=42)  # Method called on Foo: 12 42
obj.foo(12, 42)      # Method called on Foo: 12 42
于 2018-01-13T13:54:22.753 回答