当前位置:网站首页>@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 .
边栏推荐
- 刷题-洛谷-P1085 不高兴的津津
- uniapp处理后台传输图片
- Hcip day 6 notes
- Hcip day 8 notes
- pycharm不能输入中文解决方法
- 刷题-洛谷-P1035 级数求和
- Explain the precision of floating point numbers in detail
- Prepare for 2022 csp-j1 2022 csp-s1 preliminaries video set
- MXNet对DenseNet(稠密连接网络)的实现
- Based on Baiwen imx6ull_ Ap3216 experiment driven by Pro development board
猜你喜欢

Discussion on principle and application technology of MLIR

刷题-洛谷-P1046 陶陶摘苹果

uniapp处理后台传输图片
[email protected] (using password:YES)"/>The simplest solution of the whole network 1045 access denied for user [email protected] (using password:YES)

为提高效率使用ParallelStream竟出现各种问题
![Error of Tencent cloud [100007] this env is not enable anonymous login](/img/a2/a209a0d94e3fbf607242c28d87e2dd.png)
Error of Tencent cloud [100007] this env is not enable anonymous login

QGIS加载在线地图:高德、天地图等

2022全球开发者中,你的收入排多少?

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

安装mujoco报错:distutils.errors.DistutilsExecError: command ‘gcc‘ failed with exit status 1
随机推荐
0719RHCSA
Leetcode 113. path sum II
备战2022 CSP-J1 2022 CSP-S1 初赛 视频集
【力扣】1030.距离顺序排列矩阵单元格
百度搜索打击盗版网文站点,SEOer应该关注哪些问题?
Azure Devops (XIV) use azure's private nuget warehouse
[server data recovery] HP EVA server storage raid information power loss data recovery
「数字安全」警惕 NFT的七大骗局
Canvas判断内容为空
Business visualization - make your flowchart'run'(3. Branch selection & cross language distributed operation node)
hcip第十天笔记
Blocking queue for concurrent programming
How to solve the problem of taking up too much space when recording and editing videos?
pycharm不能输入中文解决方法
刷题-洛谷-P1089 津津的储蓄计划
Uncaught SyntaxError: Octal literals are not allowed in strict mode.
Explain the precision of floating point numbers in detail
6.27 uniapp项目历程
Hcip seventh day notes
Django 2 ----- database and admin