python怎么用类装饰器,python – 使用类作为方法装饰器

通常当一个方法被访问为some_instance.some_method()时,python的

descriptor protocol会启动并调用some_method .__ get __(),它返回一个绑定方法.但是,因为该方法已被Deco类的实例替换,所以不会发生这种情况 – 因为Deco不是描述符.为了使Deco按预期工作,它必须实现一个__get__方法,该方法返回自身的绑定副本.

履行

这是基本的“无所事事”装饰类:

import inspect

import functools

from copy import copy

class Deco(object):

def __init__(self, func):

self.__self__ = None # "__self__" is also used by bound methods

self.__wrapped__ = func

functools.update_wrapper(self, func)

def __call__(self, *args, **kwargs):

# if bound to an object, pass it as the first argument

if self.__self__ is not None:

args = (self.__self__,) + args

#== change the following line to make the decorator do something ==

return self.__wrapped__(*args, **kwargs)

def __get__(self, instance, owner):

if instance is None:

return self

# create a bound copy

bound = copy(self)

bound.__self__ = instance

# update __doc__ and similar attributes

functools.update_wrapper(bound, self.__wrapped__)

# add the bound instance to the object's dict so that

# __get__ won't be called a 2nd time

setattr(instance, self.__wrapped__.__name__, bound)

return bound

要使装饰器执行某些操作,请在__call__方法中添加代码.

这是一个带参数的:

class DecoWithArgs(object):

#== change the constructor's parameters to fit your needs ==

def __init__(self, *args):

self.args = args

self.__wrapped__ = None

self.__self__ = None

def __call__(self, *args, **kwargs):

if self.__wrapped__ is None:

return self.__wrap(*args, **kwargs)

else:

return self.__call_wrapped_function(*args, **kwargs)

def __wrap(self, func):

# update __doc__ and similar attributes

functools.update_wrapper(self, func)

return self

def __call_wrapped_function(self, *args, **kwargs):

# if bound to an object, pass it as the first argument

if self.__self__ is not None:

args = (self.__self__,) + args

#== change the following line to make the decorator do something ==

return self.__wrapped__(*args, **kwargs)

def __get__(self, instance, owner):

if instance is None:

return self

# create a bound copy of this object

bound = copy(self)

bound.__self__ = instance

bound.__wrap(self.__wrapped__)

# add the bound decorator to the object's dict so that

# __get__ won't be called a 2nd time

setattr(instance, self.__wrapped__.__name__, bound)

return bound

像这样的实现让我们在方法和函数上使用装饰器,所以我认为它应该被认为是一种好的做法.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值