当前位置:网站首页>@Classmethod decorator
@Classmethod decorator
2022-07-25 13:42:00 【Muxiatong】
Catalog
Example
General create class ( It contains N A way ) after , You need to use the methods inside to achieve a specific function , Need to instantiate class , Then access its methods through the class after the instance , for example :
class test_classmethod(object):
def printd(self,a ,b):
c = a+b
print (c)
if __name__ == '__main__':
c =test_classmethod()
c.printd(3,5)The running result is 8
If you don't want to instantiate , Can be in def printd(self,a ,b) Add... To it @classmethod, as follows :
class test_classmethod(object):
@classmethod
def printd(cls,a ,b):
c = a+b
print (c)
if __name__ == '__main__':
test_classmethod.printd(3,5)classmethod The function corresponding to the modifier does not need to be instantiated , Unwanted self Parameters , But the first parameter needs to represent the class itself cls Parameters , You can call the properties of a class , Class method , Instanced objects, etc . as follows :
class A():
a = 1
def def1(self):
print("foo")
@classmethod
def def2(cls):
print('def2')
print(cls.a)
cls().def1() # call foo Method
A.def2()Running results :
def2
1
fooThe variables defined in the class are required , If it is written in the following way, an error will be reported :
class A():
def __init__(self):
a = 1
def def1(self):
print("foo")
@classmethod
def def2(cls):
print('def2')
print(cls.a)
cls().def1() # call foo Method
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() # call foo Method
A.def2()The main role
Decorators are mainly completely different from them , The main function is outside this method , Add some extra features ( Without changing the original code ), for example , I write some basic methods ,a function ( Belong to A A group of programmers )、b function ( Belong to B A group of programmers )、c function ( Belong to C A group of programmers ) You need to call this method , One day the company added D Group 、E Group 、F Group . But the leadership rules , Can only A、B、C Group call , Other groups cannot call this function , Risk exists . Then I need to write a verification method , Decorate this function
Decorator is the method to be called a Wrapped in another way W1 in , adopt w1 Execute after passing the function execution of a, Otherwise, the called... Will not be executed a function .
边栏推荐
- Based on Baiwen imx6ull_ Pro development board transplants LCD multi touch driver (gt911)
- 刷题-洛谷-P1146 硬币翻转
- 安装mujoco报错:distutils.errors.DistutilsExecError: command ‘gcc‘ failed with exit status 1
- AQS of concurrent programming
- Excel record macro
- @classmethod 装饰器
- QGIS加载在线地图:高德、天地图等
- 移动端网站,独立APP,网站排名策略有哪些?
- LabVIEW的内部错误
- pytest.mark.parametrize及mock使用
猜你喜欢

Vscode plug-in development
![【Platform IO编译Hifive1-revB】*** [.pio\build\hifive1-revb\src\setupGPIO.o] Error 1的解决办法](/img/a1/de197464e2a735d7ef012cea780da5.png)
【Platform IO编译Hifive1-revB】*** [.pio\build\hifive1-revb\src\setupGPIO.o] Error 1的解决办法

刷题-洛谷-P1059 明明的随机数

Uniapp handles background transfer pictures

Generate SQL script file by initializing the latest warehousing time of vehicle attributes

刷题-洛谷-P1152 欢乐的跳

6.27 uniapp项目历程

The interviewer asked me: how much do you know about MySQL's storage engine?

pytest.mark.parametrize及mock使用

移动端网站,独立APP,网站排名策略有哪些?
随机推荐
Hcip day 10 notes
二叉树基本知识
0716RHCSA
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
【服务器数据恢复】HP EVA服务器存储RAID信息断电丢失的数据恢复
uniapp处理后台传输图片
From input URL to web page display
How to realize the configuration method of user password free login?
【Platform IO编译Hifive1-revB】*** [.pio\build\hifive1-revb\src\setupGPIO.o] Error 1的解决办法
What is your revenue rank among global developers in 2022?
hcip第六天笔记
刷题-洛谷-P1047 校门外的树
pytest.mark.parametrize及mock使用
G027-op-ins-rhel-04 RedHat openstack creates a customized qcow2 format image
包管理 apt,dpkg
IM系统-消息流化一些常见问题
How can information security engineers prepare for the soft exam in the second half of 2022?
2022年下半年软考信息安全工程师如何备考?
刷题-洛谷-P1152 欢乐的跳
Design and principle of thread pool