python: Decorators

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
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;

  

调用:

1
2
3
4
5
6
#调用
Decorators.timeFuncCounter(Decorators.drawFourRect)
Decorators.drawFourRect(-100,200,200)
 
Decorators.timeFuncCounter(Decorators.add)
Decorators.add(300,400)

  

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'''
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)

  

调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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()

  

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'''
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

  

调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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()

  

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#装饰器
def printpy(func):
    def inner_func():
        func()
        print("hello python! Geovin Du")
    return inner_func
# @装饰器
@printpy
def printhello():
    print("hello world!")
 
 
#调用
printhello()

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'''
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!")

  

调用:

1
2
3
4
5
6
7
8
9
10
11
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()

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'''
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*"*")

  

调用:

1
2
3
4
5
import MultiplicationTable
 
#调用multiplication table
MultiplicationTable.decotrjiujiu(MultiplicationTable.jiujiu)
MultiplicationTable.jiujiu()

  

如果一个函数内部定义了另外一个函数,那么函数内部的称为内部函数,另一个称为外部函数;
闭包:外部函数里定义了一个内部函数,并且把内部函数名作为返回值返回;
装饰器:外部函数的参数是函数;
# 外部函数里定义内部函数,外部函数的参数是函数,并且返回内部函数---外部函数称为装饰器
# 内部的函数参数要跟被装饰的函数参数保持一致;
 
 

 

posted @   ®Geovin Du Dream Park™  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2014-06-11 高德地图 API JavaScript API
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示