python类装饰器如何使用?
python类装饰器如何使用?
1、类H1的两个方法:
__init__:接收一个函数作为参数,就是待被装饰的函数;
__call__:让类对象可以调用,类似函数调用,触发点是被装饰的函数调用时触发。
2、实例
基于类的装饰器与基于函数的基本用法一致。
classH1(object):
def__init__(self,func):
self.func=func
def__call__(self,*args,**kwargs):
return'
'+self.func(*args,**kwargs)+'
'
@H1
deftext(name):
returnf'text{name}'
s=text('class')
print(s)
以上就是python类装饰器的使用,希望对大家有所帮助。更多Python学习推荐:请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python中numpy.concatenate()函数怎么用?
python中numpy.concatenate()函数怎么用?本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、numpy.concatenate函数主要作用:沿详情>>
2023-11-13 23:20:14
python如何捕获正在运行的CMD窗口内容?
python如何捕获正在运行的CMD窗口内容?本文介绍需要捕获已经在运行的CMD窗口内容,并且需要根据指定输出内容来判断下一步的行动(输入指定内容)...详情>>
2023-11-13 20:46:21
pythonpip是什么
python的pip是什么一、pip是什么pip是Python的包管理器。这意味着它是一个工具,允许你安装和管理不属于标准库的其他库和依赖。软件包管理极其...详情>>
2023-11-13 19:36:56
pythontruncate是什么
pythontruncate是什么1、说明从文件的首行首字符开始截断,截断文件为n个字符;无n表示从当前位置起截断;截断之后n后面的所有字符被删除。2、语...详情>>
2023-11-13 16:42:48热门推荐
技术干货






