Python programming decorator

  • About the author: A computer student at school, sharing Python learning experience and study notes every day. 

  •  Motto: Keep your head down and hurry on your way, be respectful

  • Personal homepage: b Network Dou's homepage

Table of contents

 foreword

1. Function

1. Introduction of decorators

(1) Time module

Wrapper function (calcu_time)

2. Introduction to decorators

 3. Use decorator optimization


 foreword

This chapter will explain decorators in Python programming.


1. Function

1. Introduction of decorators

think:
  • Calculate the running time of test1
  • Calculate the running time of test2
  • Calculate the running time of test3
  • Calculate the running time of test4

(1) Time module

The time module provides various functions for manipulating time

Explanation: There are generally two ways to express time:

  1. The first is the way of timestamp (the offset calculated in seconds relative to 1970.1.1 00:00:00), the timestamp is unique
  2. The second is expressed in the form of an array (struct_time), which has nine elements in total, which respectively represent that the struct_time of the same timestamp will be different due to different time zones
import time  # python内置模块,时间模块

Wrapper function (calcu_time)

# 计算时间得函数
def calcu_time(func):
    start = time.time()
    func()
    end = time.time()
    print(f"spend {end - start}")
# 计算时间得函数
def calcu_time(func):
    start = time.time()
    func()
    end = time.time()
    print(f"spend {end - start}")


def test1():
    print("--test1--")
    time.sleep(2)


def test2():
    print("--test2--")
    time.sleep(2)


calcu_time(test1)
calcu_time(test2)


"""
就是在不改变函数源代码得情况下为函数添加新得功能
"""

2. Introduction to decorators

Decorator is an important part of Python. It is essentially a function. Unlike ordinary functions, the return value of a decorator is a function object. By using decorators, we can add additional functionality to other functions without any code changes, and at the same time make the code more concise.

 

# 需要给各个函数添加上打印hello world得功能
def print_hw(f):
    print("hello world")
    return f


@print_hw           # @装饰器函数名称     test2 = print_hw(test)
def test():
    sum_li = sum([12, 3, 4])
    print(sum_li)
    return sum_li


@print_hw
def test2():
    print("我是网络豆")


# test2 = print_hw(test)  # test函数引用作为实参 test2 = f = test
# test2()     # test2() = f() = test()


test()
test2()


"""
注意:
1. test()函数未调用得时候,装饰器就已经执行
"""



 3. Use decorator optimization

import time

"""
此处是问题代码,因为重复调用了test1()
"""


 # 计算时间得函数
def calcu_time(func):    # func = test1
     start = time.time()
     func()      # test1()
     end = time.time()
     print(f"spend {end - start}")
     return func


 def test1():
     print("--test1--")
     time.sleep(2)

 test = calcu_time(test1)    # test = func = test1
 test()


# 计算时间得函数
def calcu_time(func):
    def test_in():               # 1.外函数里面有内函数
        start = time.time()
        func()      # test1()    # 2.内函数引用了外函数得临时变量
        end = time.time()
        print(f"spend {end - start}")
    return test_in               # 3.内函数的引用作为外函数的返回值


@calcu_time             # test = calcu_time(test1)
def test1():
    print("--test1--")
    time.sleep(2)


@calcu_time
def test2():
    print("--test2--")
    time.sleep(2)


 test = calcu_time(test1)    # test = test_in
 test()     # test_in()

 test = calcu_time(test2)    # test = test_in
 test()     # test_in()

test1()


"""
此处,形成闭包的现象。
1.嵌套函数
2.内层函数引用外层函数变量-->函数名本质也是变量
3.内函数的引用作为外层函数的返回值
"""

 Creation is not easy, please pay attention, like, collect, thank you~  

Guess you like

Origin blog.csdn.net/yj11290301/article/details/128793026