技术背景
在python的类型设计中,有时候会遇到一个cls
参数。其实cls
参数就是一个约定俗成的名称,用其他的名字也能正常运行但不建议这么用。它的作用类似于实例方法中的self
参数,代表的是类本身,可以用于访问类的参数和类的方法。本文通过一些具体示例,来演示cls
参数的功能和用法。
简单类实现
首先我们用普通的方法做一个最基本的测试案例:
class test: def __init__(self): self.prefix = "hello\t" def excute(self, x): print (self.prefix+x) t = test() x = "bob" t.excute(x) x = "alice" t.excute(x)
这里test
类型的操作逻辑是,在初始化函数中初始化一个prefix变量,然后在excute中调用打印函数,打印prefix变量和一个外部输入变量的整合字符串,执行效果如下:
hello bob
hello alice
这个方法的一个局限性在于,类test中的函数,如excute函数,必须要新建一个实例t之后,才能够调用到它的excute方法。如果跳过初始化的步骤直接调用excute方法:
class test: def __init__(self): self.prefix = "hello\t" def excute(self, x): print (self.prefix+x) test.excute()
运行结果会告诉你,这需要两个变量的输入才能够正常的运行:
traceback (most recent call last): file "/home/test_cls.py", line 8, in <module> test.excute() typeerror: test.excute() missing 2 required positional arguments: 'self' and 'x'
例如,我们先初始化一个t实例,但是方法调用我们不调用t中的excute函数,而是直接调用test类中的函数:
class test: def __init__(self): self.prefix = "hello\t" def excute(self, x): print (self.prefix+x) t = test() x = "bob" test.excute(t, x)
这样也是可以正常运行的:
hello bob
classmethod方法
通过classmethod方法,可以允许我们不需要在外部对类初始化,而直接访问到类的内部属性、参数和函数。也就是对于classmethod装饰的函数,约定使用cls
变量作为开头。
class test: prefix = "hello\t" @classmethod def excute(cls, x): print (cls.prefix+x) x = "bob" test.excute(x)
这样就可以直接在外部调用到类的内部函数:
hello bob
当然,前面提到过,这里即使换一个变量名,也是可以正常运行的:
class test: prefix = "hello\t" @classmethod def excute(self, x): print (self.prefix+x) x = "bob" test.excute(x)
因为第一个参数代表的是类本身,因此可以执行成功:
hello bob
这里需要说明的是,classmethod装饰器的作用,就是把函数的第一个参数相关的内容给省去了,如果不使用classmethod进行装饰,例如:
class test: prefix = "hello\t" def excute(cls, x): print (cls.prefix+x) x = "bob" test.excute(x)
这样运行会报错:
traceback (most recent call last):
file "/home/test_cls.py", line 7, in <module>
test.excute(x)
typeerror: test.excute() missing 1 required positional argument: 'x'
提示的内容是参数缺失,其实也就是少了一个初始化的步骤。那么有一种情况是,类似于prefix这种的类属性是在__init__函数中定义的,这是比较常见的情况。在这种情况下,如果不初始化一个实例,就无法访问到初始化参数。但是前面也提到了,cls就代表类本身,那么自然可以通过cls来访问类中的函数,包括初始化的函数:
class test: def __init__(self): self.prefix = "hello\t" @classmethod def excute(cls, x): cls.__init__(cls) print (cls.prefix+x) x = "bob" test.excute(x)
这个代码可以被正确执行:
hello bob
同时,通过classmethod,可以修改类的属性:
class test: prefix = "hello\t" @classmethod def excute(cls, x): print (cls.prefix+x) cls.prefix = cls.prefix+x+"\t" x = "bob" test.excute(x) x = "alice" test.excute(x)
这里在excute函数中,每次打印之后,都会修改一下prefix参数,所以打印输出结果如下:
hello bob
hello bob alice
当然,修改属性这样的操作,在普通的类实现中也是可以操作的:
class test: def __init__(self): self.prefix = "hello\t" def excute(self, x): print (self.prefix+x) self.prefix = self.prefix+x+"\t" t = test() x = "bob" t.excute(x) x = "alice" t.excute(x)
用self得到的结果是一样的:
hello bob
hello bob alice
如果不使用classmethod,也可以通过staticmethod来实现一个类似功能:
class test: def __init__(self): self.prefix = "hello\t" @staticmethod def excute(self, x): print (self.prefix+x) self.prefix = self.prefix+x+"\t" t = test() x = "bob" test.excute(t, x) x = "alice" test.excute(t, x)
但是staticmethod不对参数进行初始化,虽然可以在外部直接调用类函数,但是需要手动初始化一个实例。输出结果是一致的:
hello bob
hello bob alice
总结概要
本文介绍了在python的classmethod装饰的类方法的cls变量的意义,通过几个不同的示例对比,凸显cls变量在python编程中的应用场景。对于大多数的场景来说,使用普通的python类和函数定义即可。如果需要在类的外部使用类的内部函数,但是可能有多个不同初始化的类输入,那么可以使用staticmethod进行装饰。如果只有一个类,而有多种不同的输入场景下,可以使用classmethod进行装饰。
版权声明
本文首发链接为:https://www.cnblogs.com/dechinphy/p/cls.html
作者id:dechinphy
更多原著文章:https://www.cnblogs.com/dechinphy/
请博主喝咖啡:https://www.cnblogs.com/dechinphy/gallery/image/379634.html
到此这篇关于python中的cls变量的文章就介绍到这了,更多相关python中的cls变量内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论