python: Decorators
def timeFuncCounter(func): """ 装饰器 写一个装饰器,统计任何函数执行完所需要消耗的时间 :param func: 输入函数 :return: 返回函数名 """ def innerfunc(*args,**kwargs): stat = time.perf_counter() time.sleep(3) func(*args,**kwargs) end = time.perf_counter() ctime = end -stat print("函数 {} 执行时间:{:.6f}s:".format(func.__name__,ctime)) print() return innerfunc
""" file Decorators.py edit geovindu Geovin Du date 2023-06-11 """ import sys import time import turtle as t # 写一个装饰器,统计任何函数执行完所需要消耗的时间 def timeFuncCounter(func): """ 装饰器 写一个装饰器,统计任何函数执行完所需要消耗的时间 :param func: 输入函数 :return: 返回函数名 """ def innerfunc(*args,**kwargs): stat = time.perf_counter() time.sleep(3) func(*args,**kwargs) end = time.perf_counter() ctime = end -stat print("time:", ctime," 秒") print() return innerfunc def drawOneRect(x, y, side): """ 画正方形 :param x: :param y: :param side: :return: """ t.penup() t.goto(x, y) t.pendown() for _ in range(4): t.fd(side) t.right(90) @timeFuncCounter def drawFourRect(x, y, side): """ 画四个正方形 :param x: :param y: :param side: :return: """ side /= 2 plist = [(x, y), (x + side, y), (x, y - side), (x + side, y - side)] # 元祖拆包 for a in plist: drawOneRect(*a, side) @timeFuncCounter def add(a:int, b:int ): """ 和 :param a: :param b: :return: """ #print(a+b,end="/n") return a+b;
调用:
#调用 Decorators.timeFuncCounter(Decorators.drawFourRect) Decorators.drawFourRect(-100,200,200) Decorators.timeFuncCounter(Decorators.add) Decorators.add(300,400)
''' Decorators.py file 装饰器 无参数,有参数 通用装饰器 (*args, **kwargs) editor: geovindu, Geovin Du date: 2023-06-11 ''' import sys # setup 1: printpy(printhello), 调用printpy,把printhello当成参数传递进去:func=printhello # setup 2: 定义内部函数inner_func # setup 3: 返回内部函数inner_func # setup 4: printhello=inner_func # setup 5: printhello(),实际上调用内部函数inner_func #装饰器 def printpy(func): """ :param func: :return: """ def inner_func(): func() print("hello python! Geovin Du") return inner_func # @装饰器 Decorators @printpy def printhello(): """ :return: """ print("hello world!") #带参数 def decotrAge(func): """ 得到年龄 带参数 装饰器 :param func: 函数名 :return: 返回正常的年龄 """ def innerfunc(iAge): if iAge<0: return 0 return func(iAge) #func(iAge) return innerfunc @decotrAge def getAge(age): """ 得到年龄 带参数 :param age: 输入参数年龄 :return:年龄 """ #print(age) return age # 通用装饰器 (*args, **kwargs) def commonDecred(func): def innerfunc(*args,**kwargs): func(*args,**kwargs) print("ICT CopytRight") return innerfunc @commonDecred def printdemo(): print("我是打印阳光灿烂的日子函数") @commonDecred def printcom(a,b): print("a,b的和是:",a+b)
调用:
import Decorators #调用 带参数装饰器 Decorators.decotrAge(Decorators.getAge) print("age:",Decorators.getAge(-10)) #调用 不带参数装饰器 #Decorators.printhello() # setup 1: printpy(printhello), 调用printpy,把printhello当成参数传递进去:func=printhello # setup 2: 定义内部函数innerfunc # setup 3: 返回内部函数innerfunc # setup 4: printhello=innerfunc # setup 5: printhello(),实际上调用内部函数inner_func printhello=Decorators.printpy(Decorators.printhello) printhello() #通用的装饰器 Decorators.commonDecred(Decorators.printdemo) Decorators.commonDecred(Decorators.printcom) Decorators.printcom(10,20) Decorators.printdemo()
''' Decorators.py file 装饰器 无参数,有参数 editor: geovindu, Geovin Du date: 2023-06-11 ''' import sys # setup 1: printpy(printhello), 调用printpy,把printhello当成参数传递进去:func=printhello # setup 2: 定义内部函数inner_func # setup 3: 返回内部函数inner_func # setup 4: printhello=inner_func # setup 5: printhello(),实际上调用内部函数inner_func #装饰器 def printpy(func): """ :param func: :return: """ def inner_func(): func() print("hello python! Geovin Du") return inner_func # @装饰器 Decorators @printpy def printhello(): """ :return: """ print("hello world!") #带参数 def decotrAge(func): """ 得到年龄 带参数 装饰器 :param func: 函数名 :return: 返回正常的年龄 """ def innerfunc(iAge): if iAge<0: return 0 return func(iAge) #func(iAge) return innerfunc @decotrAge def getAge(age): """ 得到年龄 带参数 :param age: 输入参数年龄 :return:年龄 """ #print(age) return age
调用:
import Decorators #调用 带参数装饰器 Decorators.decotrAge(Decorators.getAge) print("age:",Decorators.getAge(-10)) #调用 不带参数装饰器 #Decorators.printhello() # setup 1: printpy(printhello), 调用printpy,把printhello当成参数传递进去:func=printhello # setup 2: 定义内部函数innerfunc # setup 3: 返回内部函数innerfunc # setup 4: printhello=innerfunc # setup 5: printhello(),实际上调用内部函数inner_func printhello=Decorators.printpy(Decorators.printhello) printhello()
#装饰器 def printpy(func): def inner_func(): func() print("hello python! Geovin Du") return inner_func # @装饰器 @printpy def printhello(): print("hello world!") #调用 printhello()
''' Decorators.py file 装饰器 editor: geovindu, Geovin Du date: 2023-06-11 ''' import sys # setup 1: printpy(printhello), 调用printpy,把printhello当成参数传递进去:func=printhello # setup 2: 定义内部函数inner_func # setup 3: 返回内部函数inner_func # setup 4: printhello=inner_func # setup 5: printhello(),实际上调用内部函数inner_func #装饰器 def printpy(func): def inner_func(): func() print("hello python! Geovin Du") return inner_func # @装饰器 Decorators @printpy def printhello(): print("hello world!")
调用:
import Decorators #调用 Decorators.printhello() # setup 1: printpy(printhello), 调用printpy,把printhello当成参数传递进去:func=printhello # setup 2: 定义内部函数inner_func # setup 3: 返回内部函数inner_func # setup 4: printhello=inner_func # setup 5: printhello(),实际上调用内部函数inner_func printhello=Decorators.printpy(Decorators.printhello) printhello()
''' MultiplicationTable.py file 装饰器 九九乘法表 editor: geovindu, Geovin Du date: 2023-06-11 ''' import sys def decotrjiujiu(func): """ 装饰器模式函数 :param func: 函数名称 :return: 返回函数名称 """ def innerfunc(): func() for row in range(1,10): for col in range(1,row+1): print("{0}*{1}={2:2d}".format(row,col,row*col),end=" ") print(" ") return innerfunc @decotrjiujiu def jiujiu(): """ 九九乘法表 :return: none """ print("*"*10+"九九乘法表"+10*"*")
调用:
import MultiplicationTable #调用multiplication table MultiplicationTable.decotrjiujiu(MultiplicationTable.jiujiu) MultiplicationTable.jiujiu()
如果一个函数内部定义了另外一个函数,那么函数内部的称为内部函数,另一个称为外部函数;
闭包:外部函数里定义了一个内部函数,并且把内部函数名作为返回值返回;
装饰器:外部函数的参数是函数;
# 外部函数里定义内部函数,外部函数的参数是函数,并且返回内部函数---外部函数称为装饰器
# 内部的函数参数要跟被装饰的函数参数保持一致;
闭包:外部函数里定义了一个内部函数,并且把内部函数名作为返回值返回;
装饰器:外部函数的参数是函数;
# 外部函数里定义内部函数,外部函数的参数是函数,并且返回内部函数---外部函数称为装饰器
# 内部的函数参数要跟被装饰的函数参数保持一致;
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)