当前位置:网站首页>@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功能。
边栏推荐
- Prepare for 2022 csp-j1 2022 csp-s1 preliminaries video set
- Gym安装、调用以及注册
- Brpc source code analysis (III) -- the mechanism of requesting other servers and writing data to sockets
- 详解浮点数的精度问题
- Jupyter Notebook介绍
- Hcip day 9 notes
- Pycharm cannot input Chinese solution
- Shell common script: get the IP address of the network card
- Date and time function of MySQL function summary
- Mujoco+spinningup for intensive learning training quick start
猜你喜欢

Shell common script: get the IP address of the network card
![Detailed explanation of the training and prediction process of deep learning [taking lenet model and cifar10 data set as examples]](/img/70/2b5130be16d7699ef7db58d9065253.png)
Detailed explanation of the training and prediction process of deep learning [taking lenet model and cifar10 data set as examples]

Django 2 ----- database and admin

Any time, any place, super detective, seriously handle the case!

Install mujoco and report an error: distutils.errors DistutilsExecError: command ‘gcc‘ failed with exit status 1

Numpy简介和特点(一)
![[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing](/img/6e/9e0abf8db5ec93080033bd89605ac2.jpg)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing

Excel record macro
详解浮点数的精度问题

Redis visualizer RDM installation package sharing
随机推荐
0715RHCSA
基于百问网IMX6ULL_PRO开发板驱动AP3216实验
从输入网址到网页显示
Canvas judgment content is empty
好友让我看这段代码
6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
Django 2 ----- 数据库与Admin
Online Learning and Pricing with Reusable Resources: Linear Bandits with Sub-Exponential Rewards: Li
2022年下半年软考信息安全工程师如何备考?
My creation anniversary
0715RHCSA
Design and principle of thread pool
The whole process of 6w+ word recording experiment | explore the economical data storage strategy of alluxio
【AI4Code】《Pythia: AI-assisted Code Completion System》(KDD 2019)
若依如何实现用户免密登录配置方法?
hcip第六天笔记
Shell common script: get the IP address of the network card
ThreadLocal&Fork/Join
刷题-洛谷-P1151 子数整数
【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22