python自带装饰器详解_学习笔记-Python装饰器详解

1.什么是装饰器

装饰器实际上就是为了给某程序增添功能,但该程序已经上线或已经被使用,那么就不能大批量的修改源代码,这样是不科学的也是不现实的,因为就产生了装饰器,使得其满足:

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

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

满足1、2的情况下给程序增添功能

2.装饰器的组成

3.一切皆对象

首先我们来理解下 Python 中的函数:

def hi(name="yasoob"):

return "hi " + name

print(hi())

# output: 'hi yasoob'

# 我们甚至可以将一个函数赋值给一个变量,比如

greet = hi

# 我们这里没有在使用小括号,因为我们并不是在调用hi函数

# 而是在将它放在greet变量里头。我们尝试运行下这个

print(greet())

# output: 'hi yasoob'

# 如果我们删掉旧的hi函数,看看会发生什么!

del hi

print(hi())

#outputs: NameError

print(greet())

#outputs: 'hi yasoob'

在函数中定义函数:

def hi(name="yasoob"):

print("now you are inside the hi() function")

def greet():

return "now you are in the greet() function"

def welcome():

return "now you are in the welcome() function"

print(greet())

print(welcome())

print("now you are back in the hi() function")

hi()

#output:now you are inside the hi() function

# now you are in the greet() function

# now you are in the welcome() function

# now you are back in the hi() function

# 上面展示了无论何时你调用hi(), greet()和welcome()将会同时被调用。

# 然后greet()和welcome()函数在hi()函数之外是不能访问的,比如:

greet()

#outputs: NameError: name 'greet' is not defined

从函数中返回函数:

def hi(name="yasoob"):

def greet():

return "now you are in the greet() function"

def welcome():

return "now you are in the welcome() function"

if name == "yasoob":

return greet

else:

return welcome

a = hi()

print(a)

#outputs:

#上面清晰地展示了`a`现在指向到hi()函数中的greet()函数

#现在试试这个

print(a())

#outputs: now you are in the greet() function

在if和else后面返回的是greet 和 welcome,而不是 greet() 和 welcome()。为什么那样?这是因为当你把一对小括号放在后面,这个函数就会执行;然而如果你不放括号在它后面,那它可以被到处传递,并且可以赋值给别的变量而不去执行它

将函数作为参数传给另一个函数:

def hi():

return "hi yasoob!"

def doSomethingBeforeHi(func):

print("I am doing some boring work before executing hi()")

print(func())

doSomethingBeforeHi(hi)

#outputs:I am doing some boring work before executing hi()

# hi yasoob!

4.编辑第一个装饰器

创建第一个装饰器:

def a_new_decorator(a_func):

def wrapTheFunction():

print("I am doing some boring work before executing a_func()")

a_func()

print("I am doing some boring work after executing a_func()")

return wrapTheFunction

def a_function_requiring_decoration():

print("I am the function which needs some decoration to remove my foul smell")

a_function_requiring_decoration()

#outputs: "I am the function which needs some decoration to remove my foul smell"

a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)

#now a_function_requiring_decoration is wrapped by wrapTheFunction()

a_function_requiring_decoration()

#outputs:I am doing some boring work before executing a_func()

# I am the function which needs some decoration to remove my foul smell

# I am doing some boring work after executing a_func()

如上代码所示,它们封装一个函数,并且用这样或者那样的方式来修改它的行为。现在你也许疑惑,我们在代码里并没有使用 @ 符号?那只是一个简短的方式来生成一个被装饰的函数。这里是我们如何使用 @ 来运行之前的代码:

@a_new_decorator

def a_function_requiring_decoration():

"""Hey you! Decorate me!"""

print("I am the function which needs some decoration to "

"remove my foul smell")

a_function_requiring_decoration()

#outputs: I am doing some boring work before executing a_func()

# I am the function which needs some decoration to remove my foul smell

# I am doing some boring work after executing a_func()

#the @a_new_decorator is just a short way of saying:

a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)

如果我们运行如下代码会存在一个问题:

print(a_function_requiring_decoration.__name__)

# Output: wrapTheFunction

Python提供给我们一个简单的函数来解决这个问题,那就是functools.wraps。我们修改上一个例子来使用functools.wraps:

from functools import wraps

def a_new_decorator(a_func):

@wraps(a_func)

def wrapTheFunction():

print("I am doing some boring work before executing a_func()")

a_func()

print("I am doing some boring work after executing a_func()")

return wrapTheFunction

@a_new_decorator

def a_function_requiring_decoration():

"""Hey yo! Decorate me!"""

print("I am the function which needs some decoration to "

"remove my foul smell")

print(a_function_requiring_decoration.__name__)

# Output: a_function_requiring_decoration

5.我的第一个装饰器

from functools import wraps

def decorator_name(f):

@wraps(f)

def decorated(*args, **kwargs):

if not can_run:

return "Function will not run"

return f(*args, **kwargs)

return decorated

@decorator_name

def func():

return("Function is running")

can_run = True

print(func())

# Output: Function is running

can_run = False

print(func())

# Output: Function will not run

注意:@wraps接受一个函数来进行装饰,并加入了复制函数名称、注释文档、参数列表等等的功能。这可以让我们在装饰器里面访问在装饰之前的函数的属性。

6.使用场景

授权(Authorization):

from functools import wraps

def requires_auth(f):

@wraps(f)

def decorated(*args, **kwargs):

auth = request.authorization

if not auth or not check_auth(auth.username, auth.password):

authenticate()

return f(*args, **kwargs)

return decorated

日志(Logging):

from functools import wraps

def logit(func):

@wraps(func)

def with_logging(*args, **kwargs):

print(func.__name__ + " was called")

return func(*args, **kwargs)

return with_logging

@logit

def addition_func(x):

"""Do some math."""

return x + x

result = addition_func(4)

# Output: addition_func was called

在函数中嵌入装饰器:

from functools import wraps

def logit(logfile='out.log'):

def logging_decorator(func):

@wraps(func)

def wrapped_function(*args, **kwargs):

log_string = func.__name__ + " was called"

print(log_string)

# 打开logfile,并写入内容

with open(logfile, 'a') as opened_file:

# 现在将日志打到指定的logfile

opened_file.write(log_string + '\n')

return func(*args, **kwargs)

return wrapped_function

return logging_decorator

@logit()

def myfunc1():

pass

myfunc1()

# Output: myfunc1 was called

# 现在一个叫做 out.log 的文件出现了,里面的内容就是上面的字符串

@logit(logfile='func2.log')

def myfunc2():

pass

myfunc2()

# Output: myfunc2 was called

# 现在一个叫做 func2.log 的文件出现了,里面的内容就是上面的字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值