摘要
python装饰器是一个用于封装函数、方法或类的代码的工具,用来显式指定管理它们的代码。一次编写,可用于多种不同的情况。在python的流行框架中,装饰器应用越来越广泛。用类设计装饰器,可以利用实例属性保持装饰逻辑提供的状态信息及原方法,通过重写类的一些特殊方法实现装饰功能。本文首先探讨了实现装饰方法的类装饰器应具有的基本结构,接着对通用的两种编码模型进行了语法和流程的深入分析,并总结了各自的特点,为编写类装饰器装饰方法提供扎实的理论基础。
Python decorator is a tool used to encapsulate the code of functions,methods or classes.It is used to explicitly specify the code to manage them.It can be written at one time and can be used in many different situations.In the popular framework of python,decorators are used more and more widely.Using class to design decorator,we can use instance attribute to keep the state information provided by decorating logic and the original method,and rewrite some special methods of class to realize decorating function.Firstly,this paper discusses the basic structure of the decorator based on class definition,and then analyzes the syntax and flow of program of the two general coding models,and summarizes their characteristics.It provides a solid theoretical basis for programming decorators based on class definition to decorate methods.
作者
李德水
LI De-shui(Computer College,Weinan Normal University,Weinan 714099,China)
出处
《电子设计工程》
2020年第13期41-44,49,共5页
Electronic Design Engineering
关键词
装饰器
类装饰器
重绑定
函数
绑定方法对象
decorator
decorator based on class definition
rebind
function
bound method object