python经典作品案例分析_Python3.5装饰器典型案例分析

本文实例讲述了Python3.5装饰器。分享给大家供大家参考,具体如下:

#!/usr/bin/env python

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

# Author:ZhengzhengLiu

#高阶函数+嵌套函数==>装饰器

import time

def timer(func): #timer(test1)-->func=test1

def decor():

start_time = time.time()

func() #run test1

stop_time = time.time()

print("the run time of func is %s" %(stop_time-start_time))

return decor

@timer #test1 = timer(test1)

def test1():

time.sleep(3)

print("in the test1")

@timer #test2 = timer(test2)

def test2():

time.sleep(3)

print("in the test2")

print(timer(test1)) #打印deco的地址

#test1 = timer(test1)

#test2 = timer(test2)

test1() #-->执行decor

test2()

运行结果:

.decor at 0x00B720C0>

in the test1

the run time of func is 3.000171661376953

in the test2

the run time of func is 3.000171661376953

1、装饰器修饰有参数函数

#高阶函数+嵌套函数==>装饰器

import time

def timer(func): #timer(test1)-->func=test1

def decor(arg1,arg2):

start_time = time.time()

func(arg1,arg2) #run test2

stop_time = time.time()

print("the run time of func is %s" %(stop_time-start_time))

return decor

@timer #test2 = timer(test2) = decor test2(name)==>decor(name)

def test2(name,age):

print("test2:",name,age)

test2("liu",23)

运行结果 :

test2: liu 23

the run time of func is 0.0

2、装饰器修饰多个函数,有的函数带参数,有的函数不带参数的情况(采用参数组)

#高阶函数+嵌套函数==>装饰器

import time

def timer(func): #timer(test1)-->func=test1

def decor(*args,**kwargs):

start_time = time.time()

func(*args,**kwargs) #run test1

stop_time = time.time()

print("the run time of func is %s" %(stop_time-start_time))

return decor

@timer #test1 = timer(test1)

def test1():

time.sleep(3)

print("in the test1")

@timer #test2 = timer(test2) = decor test2(name)==>decor(name)

def test2(name,age):

time.sleep(1)

print("test2:",name,age)

#test1 = timer(test1)

#test2 = timer(test2)

test1() #-->执行decor

test2("liu",23)

运行结果:

in the test1

the run time of func is 3.0036065578460693

test2: liu 23

the run time of func is 1.0084023475646973

希望本文所述对大家Python程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
50个程序案例 程序1:数字组合 2 程序2:猜猜这个数是多少? 3 程序3:判断这是一年中的第几天? 3 程序4:判断整数大小 4 程序5:斐波那契数列 5 程序6:水仙花数 6 程序7:数字求和 6 程序8:平方根 7 程序9:if语句 8 程序10:阿姆斯特朗数 8 程序11:输出指定范围的素数(else语句) 9 程序12:生成日历 10 程序13:统计字符串中的字符 11 程序14:文件的读取 12 程序15:十进制转二进制、八进制、十六进制 13 程序16: 生成10个两位的随机素食 13 程序17: 计算三角形面积 14 程序18:字符串大小写转换 15 程序19: 获取昨天日期 16 程序20: 变量交换 17 程序21: 质数判断 17 程序22: 简单计算器 18 程序23: 最大公约数 20 程序24: 最小公倍数 21 程序25: 字符串判断 22 程序26: 合并文件数据 23 程序27: 猜数游戏 24 程序28:为数据加密 25 程序29:平方运算 26 程序30: 计算0-7组成的奇数个数 27 程序31:求值 27 程序32:猴子分桃 28 程序33:淘汰游戏 28 程序34:分解质因子 29 程序35:取整数右端的4~7位 30 程序36:判断4位回文数 31 程序37:汉诺塔(递归函数) 32 程序38:判断闰年 33 程序39:寻找自幂数(38) 34 程序40:多维数据 35 程序41: 验证哥德巴赫猜想 36 程序42: 解方程 37 程序43: 异常处理 38 程序44: 统计不及格人数(continue语句) 39 程序45: 求直角三角形的斜边 39 程序46: 问年龄 40 程序47:求相同数字的和 41 程序48:找出1000以内的所有完数 42 程序49:公民类 42 程序50:复数类 43

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值