0
点赞
收藏
分享

微信扫一扫

Python编程:decorator装饰器


装饰器

定义:本质是函数,装饰其他函数,为其他函数添加附加功能

原则:

1、不能修改被装饰的函数源代码

2、不能修改被装饰的函数的调用方式

原理

1.函数即“变量”

2.高阶函数


  • a.把函数名当做实参传递给函数
  • b.返回一个函数名
    3.嵌套函数

总结

高阶函数 + 嵌套函数 =》 装饰器

import time

def timer(arg): # 可以接收参数
print("arg:", arg)
def outerWrapper(func): # 接收函数参数
def wrapper(*args, **kwargs): # 装饰器
start_time = time.time()
func(*args, **kwargs)
end_time = time.time()
print("run time of func is %s " % (end_time - start_time))
return wrapper
return outerWrapper

@timer("foo") # 等价于: foo = timer(foo)
def foo(): # 无参
time.sleep(3)
print("this is foo")

@timer("foo2")
def foo2(name): # 带参
time.sleep(3)
print("this is foo2,name = ", name)

foo()
foo2("Tom")

"""OUT:
arg: foo
arg: foo2
this is foo
run time of func is 3.00019907951355
this is foo2,name = Tom
run time of func is 3.000049114227295
"""

保持原有属性不变

# -*- coding: utf-8 -*-

# @File : decorator装饰器.py.py
# @Date : 2018-05-30
# @Author : Peng Shiyu

from functools import wraps

# 带参装饰器
def log(text):
print(text)

# 无参的时候只需要以下代码
def outter(func):
print("call outter")
print(func.__name__)

# 把原始函数的__name__等属性复制到 inner() 函数中
@wraps(func)
def inner(*args, **kwargs):
print("call inner")
return func(*args, **kwargs)

return inner

return outter

@log("new function")
def hello():
print("hello world")


hello()
print(hello.__name__)
"""
new function
call outter
hello
call inner
hello world
hello
"""

基于类的装饰器

class Counter:
def __init__(self, func):
self.func = func
self.count = 0

def __call__(self, *args, **kwargs):
self.count += 1
return self.func(*args, **kwargs)

@Counter
def foo():
pass

for i in range(10):
foo()

print(foo.count) # 10


参考:
​​简述 initnewcall 方法​​




举报

相关推荐

0 条评论