python: Decorators

®Geovin Du Dream Park™ / 2023-06-11 / 原文

 

 

#装饰器
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()