python自带修饰符有哪些,带可选参数的Python修饰符(它是函数)

由于很难区分decorator(func)和{},因此请制作两个装饰器:from functools import wraps

class MyCustomError(Exception):

def __init__(self):

print('in MyCustomError')

# Common implementation

def wrap(func,cb=None):

@wraps(func)

def wrapper(*args, **kwargs):

try:

return func(*args, **kwargs)

except:

if cb is not None:

cb()

raise MyCustomError()

return wrapper

# No parameters version

def wrap_error(func):

return wrap(func)

# callback parameter version

def wrap_error_cb(cb):

def deco(func):

return wrap(func,cb)

return deco

@wrap_error

def foo(a,b):

print('in foo',a,b)

raise Exception('foo exception')

def callback():

print('in callback')

@wrap_error_cb(callback)

def bar(a):

print('in bar',a)

raise Exception('bar exception')

检查foo和bar是否正确使用functools.wraps:

^{pr2}$

检查包装的函数是否正常工作:>>> foo(1,2)

in foo 1 2

in MyCustomError

Traceback (most recent call last):

File "", line 1, in

File "C:\test.py", line 16, in wrapper

raise MyCustomError()

MyCustomError

>>> bar(3)

in bar 3

in callback

in MyCustomError

Traceback (most recent call last):

File "", line 1, in

File "C:\test.py", line 16, in wrapper

raise MyCustomError()

MyCustomError

更新

这里有一种方法可以用您要求的语法来完成,但我认为上面的答案更清楚。在from functools import wraps

class MyCustomError(Exception):

def __init__(self):

print('in MyCustomError')

# Common implementation

def wrap(func,cb=None):

@wraps(func)

def wrapper(*args, **kwargs):

try:

return func(*args, **kwargs)

except:

if cb is not None:

cb()

raise MyCustomError()

return wrapper

def wrap_error(func_or_cb):

# If the function is tagged as a wrap_error_callback

# return a decorator that returns the wrapped function

# with a callback.

if hasattr(func_or_cb,'cb'):

def deco(func):

return wrap(func,func_or_cb)

return deco

# Otherwise, return a wrapped function without a callback.

return wrap(func_or_cb)

# decorator to tag callbacks so wrap_error can distinguish them

# from *regular* functions.

def wrap_error_callback(func):

func.cb = True

return func

### Examples of use

@wrap_error

def foo(a,b):

print('in foo',a,b)

raise Exception('foo exception')

@wrap_error_callback

def callback():

print('in callback')

@wrap_error(callback)

def bar(a):

print('in bar',a)

raise Exception('bar exception')

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值