当前位置:网站首页>@classmethod 装饰器
@classmethod 装饰器
2022-07-25 13:32:00 【木下瞳】
目录
示例
一般创建类(里面包含N个方法)后,需要使用里面的方法实现某种特定功能,需要实例化类,然后再通过实例后的类访问它里面的方法,例如:
class test_classmethod(object):
def printd(self,a ,b):
c = a+b
print (c)
if __name__ == '__main__':
c =test_classmethod()
c.printd(3,5)运行结果为 8
如果不想做实例化这一步,可以在 def printd(self,a ,b) 上面加上 @classmethod,如下:
class test_classmethod(object):
@classmethod
def printd(cls,a ,b):
c = a+b
print (c)
if __name__ == '__main__':
test_classmethod.printd(3,5)classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等。如下:
class A():
a = 1
def def1(self):
print("foo")
@classmethod
def def2(cls):
print('def2')
print(cls.a)
cls().def1() # 调用 foo 方法
A.def2()运行结果:
def2
1
foo其中类中定义的变量是有要求的,如果是以下的写法会报错:
class A():
def __init__(self):
a = 1
def def1(self):
print("foo")
@classmethod
def def2(cls):
print('def2')
print(cls.a)
cls().def1() # 调用 foo 方法
A.def2()class A():
def __init__(self):
self.a = 1
def def1(self):
print("foo")
@classmethod
def def2(cls):
print('def2')
print(cls.a)
cls().def1() # 调用 foo 方法
A.def2()主要作用
装饰器主要是和他们完全不同,主要作用是在本方法之外,附加一些额外功能(在不改变原代码的基础上),例如,我写一些基础的方法,a功能(属于A组程序员)、b功能(属于B组程序员)、c功能(属于C组程序员)都需要调用这个方法,有天公司加了D组、E组、F组。但领导规定,只能A、B、C组调用,其他组不能调用该功能,存在风险。那么就需要我来写一个验证的方法,装饰到该功能上
装饰器就是将要调用的方法a包裹在另一个方法中W1中,通过w1的功能执行通过后再执行a,否则就不执行调用的a功能。
边栏推荐
- 6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
- ThreadLocal&Fork/Join
- 【AI4Code】《Pythia: AI-assisted Code Completion System》(KDD 2019)
- Leetcode 113. 路径总和 II
- vim基础操作汇总
- Redis visualizer RDM installation package sharing
- QingChuang technology joined dragon lizard community to build a new ecosystem of intelligent operation and maintenance platform
- 从输入网址到网页显示
- Introduction to jupyter notebook
- ESP32-C3 基于Arduino框架下Blinker点灯控制10路开关或继电器组
猜你喜欢

Docker learning - redis cluster -3 master and 3 slave - capacity expansion - capacity reduction building

The migration of arm architecture to alsa lib and alsa utils is smooth

【AI4Code】《Unified Pre-training for Program Understanding and Generation》 NAACL 2021

Redis visualizer RDM installation package sharing

【AI4Code】《IntelliCode Compose: Code Generation using Transformer》 ESEC/FSE 2020

Esp32-c3 is based on blinker lighting control 10 way switch or relay group under Arduino framework

Convolutional neural network model -- lenet network structure and code implementation

程序员成长第二十七篇:如何评估需求优先级?

arm架构移植alsa-lib和alsa-utils一路畅通

IM系统-消息流化一些常见问题
随机推荐
C # basic learning (XXIII)_ Forms and events
并发编程之阻塞队列
【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)
【AI4Code】CodeX:《Evaluating Large Language Models Trained on Code》(OpenAI)
arm架构移植alsa-lib和alsa-utils一路畅通
stable_ Baselines quick start
刷题-洛谷-P1152 欢乐的跳
0720RHCSA
In order to improve efficiency, there are various problems when using parallelstream
0716RHCSA
【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)
Blocking queue for concurrent programming
运动豪华还是安全豪华?亚洲龙与沃尔沃S60该入手哪款?
JS array indexof includes sort() colon sort quick sort de duplication and random sample random
Mu Changchun, data Research Institute of the central bank: controllable anonymity of digital RMB is an objective need to safeguard public interests and financial security
My creation anniversary
安装mujoco报错:distutils.errors.DistutilsExecError: command ‘gcc‘ failed with exit status 1
hcip第九天笔记
uniapp处理后台传输图片
【服务器数据恢复】HP EVA服务器存储RAID信息断电丢失的数据恢复