Python 装饰器

1.首先普及语法@:

python 2.4以后,增加了@符号修饰函数对函数进行修饰,python3.0/2.6又增加了对类的修饰。

@符号的意义:

1.1函数修饰:

@dec1
@dec2
def test(arg):
       pass

等同于

def test(arg):
    pass
dec1(dec2(test(arg)))

1.2 函数修饰(带参):

@dec1(arg1,arg2)
def test(arg):pass

等同于

def test(arg):pass
dec1(arg1,arg2)(test(arg))

 

1.3 注意点:

由于@的粗暴性,如果在装饰器中返回需要装饰的对象的话,定义目标函数时装饰器就会被调用一次(执行这个定义事),且之后永远不被调用。

例子:

def func_deco1(func):
  print("before %s" % func.__name__)
  func(object)
  print("after %s" % func.__name__)
  return func

class A(object):
  def f1(self):
    print("A: f1")
  def f2(self):
    print("A: f2")
  pass

class B(object):
  @func_deco1
  def f1(self):
    print("B: f1")
  @func_deco1
  def f2(sllf):
    print("B: f2")
  pass

print("------------------------You are a pig-------------------------------")
o1=A()
o1.f1()
o1.f2()
print("------------------------You are a pig-------------------------------")
o2=B()
o2.f1()
o2.f2()


 注意,定义类B的时候,装饰器函数执行,以后不执行

before f1
B: f1
after f1
before f2
B: f2
after f2
------------------------You are a pig-------------------------------
A: f1
A: f2
------------------------You are a pig-------------------------------
B: f1
B: f2

2.将@用于装饰器

2.1 直接用于函数

def deco(func):
    def _deco():
        print("before %s" % func.__name__)
        func()
        print("after %s" % func.__name__)
        pass
    return _deco
#注意这里返回值是内部函数,包装的时候返回值是一个带有装饰器的函数,而不是函数本身,且不调用任何函数
@deco
def f1():
    print("f1")

print("------------------------You are a pig-------------------------------")
f1()
f1()

print("------------------------You are a pig-------------------------------")
print("------------------------You are a pig-------------------------------")
def f2():
  print("f2")

print("------------------------You are a pig-------------------------------")
f=deco(f2)
print("------------------------You are a pig-------------------------------")
f()
print("------------------------You are a pig-------------------------------")
f()
------------------------You are a pig-------------------------------
before f1
f1
after f1
before f1
f1
after f1
------------------------You are a pig-------------------------------
------------------------You are a pig-------------------------------
------------------------You are a pig-------------------------------
------------------------You are a pig-------------------------------
before f2
f2
after f2
------------------------You are a pig-------------------------------
before f2
f2
after f2



 2.2 装饰器用于类函数(与1.3区分 注意)

def func_deco(func):
  def _func_deco(object):
    print("before %s" % func.__name__)
    func(object)
    print("after %s" % func.__name__)
  return _func_deco

class A(object):
  def f1(self):
    print("A: f1")
  def f2(self):
    print("A: f2")
  pass

class B(object):
  @func_deco
  def f1(self):
    print("B: f1")
  @func_deco
  def f2(sllf):
    print("B: f2")
  pass

print("------------------------You are a pig-------------------------------")
o1=A()
o1.f1()
o1.f2()
print("------------------------You are a pig-------------------------------")
o2=B()
o2.f1()
o2.f2()
 
------------------------You are a pig-------------------------------
before f1
B: f1
after f1
before f2
B: f2
after f2


2.3 装饰器用于类


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值