python装饰器的原理--装饰器过程-创新互联

装饰器的定义

创新互联主营肇东网站建设的网络公司,主营网站建设方案,app软件开发公司,肇东h5微信小程序开发搭建,肇东网站营销推广欢迎肇东等地区企业咨询

就是给已有函数增加额外功能的函数,它本质上就是一个闭包函数。

装饰器的功能特点:

不修改已有函数的源代码

不修改已有函数的调用方式

给已有函数增加额外的功能

在这里我们使用装饰器实现已有函数执行时间的统计的代码来讲解一些它的原理

方式一:

import time

def show():

start = time.time()

print("show run...")

stop = time.time()

print("一共耗时 %s 秒" % (stop-start))

def count_number():

start = time.time()

s = 0

for i in range(1000000):

s += i

print(s)

stop = time.time()

print("一共耗时 %s 秒" % (stop-start))

show()

count_number()

方式一的代码虽然可以实现功能,但我们会发现代码太冗余,定义的两个函数都要写一遍计时功能,代码的重用性不高,所以这方法不好.

方式二:

import time

def count_time(func):

start = time.time()

func()

stop = time.time()

print("一共耗时 %s 秒" % (stop-start))

def show():

print("show run...")

def count_number():

s = 0

for i in range(1000000):

s += i

print(s)

count_time(show)

count_time(count_number)

方式二 封装了一个计时的函数,代码量虽然减少了很多,但是你会发现,我们在调用函数的时候,调用方式不一样,所以这个方法也不好,它修改了函数的使用方式,如果在实际开发中,你不需要计时功能了.那你还需要改,就很麻烦.

方式三:

def count_time(func):

def inner():

start = time.time()

func()

stop = time.time()

print("一共耗时 %s 秒" % (stop-start))

return inner

def show():

print("show run...")

def count_number():

s = 0

for i in range(1000000):

s += i

print(s)

# 这句话就是装饰器的原理

show = count_time(show)

# count_time(show) 单看这就是一个函数调用,

# 我们把show传给了count_time也就是show = func

# 所以count_time 函数中调用的 func()就相当于调用

# show(),因此这个闭包既帮我们完成了计时又完成了show

# 函数的调用,下面的count_number是一样的原理

count_number = count_time(count_number)

show()

count_number()

show = count_time(show)

count_number = count_time(count_number)

以上的代码,是装饰器的原理.我们必须能够理解,这是把 show 函数在内存当中的引用给了count_time

方式三的简写:郑州妇科医院 http://www.sptdfk.com/

如果有多个函数都需要添加登录验证的功能,每次都需要编写show = count_time(show)这样代码对已有函数进行装饰,这种做法还是比较麻烦。

Python给提供了一个装饰函数更加简单的写法,那就是语法糖,语法糖的书写格式是: @装饰器名字,通过语法糖的方式也可以完成对已有函数的装饰

def count_time(func):

def inner():

start = time.time()

func()

stop = time.time()

print("一共耗时 %s 秒" % (stop-start))

return inner

# 使用语法糖方式来装饰函数

@count_time # 等价于 show = count_time(show)

def show():

print("show run...")

@count_time

# 等价于 count_number = count_time(count_number)

def count_number():

s = 0

for i in range(1000000):

s += i

print(s)

show()

count_number()

装饰器的执行时间是加载模块时立即执行

我们需要注意:

闭包函数有且只有一个参数,必须是函数类型,这样定义的函 数才是装饰器。

写代码要遵循开放封闭原则,它规定已经实现的功能代码不允许被修改,但可以被扩展。

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻标题:python装饰器的原理--装饰器过程-创新互联
标题URL:http://myzitong.com/article/hoijp.html