DDR爱好者之家 Design By 杰米
一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法。
而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用。
这有利于组织代码,把某些应该属于某个类的函数给放到那个类里去,同时有利于命名空间的整洁。
既然@staticmethod和@classmethod都可以直接类名.方法名()来调用,那他们有什么区别呢
从它们的使用上来看
- @staticmethod不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样。
- @classmethod也不需要self参数,但第一个参数需要是表示自身类的cls参数。
如果在@staticmethod中要调用到这个类的一些属性方法,只能直接类名.属性名或类名.方法名。
而@classmethod因为持有cls参数,可以来调用类的属性,类的方法,实例化对象等,避免硬编码。
要明白,什么是实例方法、静态方法和类方法:
class Demo(object): def instance_method(self, your_para): """ this is an instance_method you should call it like the follow: a = Demo() a.instance_method(your_para) plus: in python, we denote 'cls' as latent para of Class while 'self' as latent para of the instance of the Class :param your_para: :return: """ print("call instance_method and get:", your_para) @classmethod def class_method(cls, your_para): """ this is a class_method you can call it like the follow: method1: a = Demo() a.class_method(your_para) method2: Demo.class_method plus: in python, we denote 'cls' as latent para of Class while 'self' as latent para of the instance of the Class :param your_para: :return: """ print("call class_method and get:", your_para) @staticmethod def static_method(your_para): """ this is a static_method and you can call it like the methods of class_method :param your_para: :return: """ print("call static_method and get:", your_para)
虽然类方法在调用的时候没有显式声明cls,但实际上类本身是作为隐含参数传入的。这就像实例方法在调用的时候也没有显式声明self,但实际上实例本身是作为隐含参数传入的。
对于静态函数,我们一般把与类无关也与实例无关的函数定义为静态函数。例如入口检查的函数就最好定义成静态函数。
类方法的妙处, 在继承中的作用:
class Fruit(object): total = 0 # 这是一个类属性 @classmethod def print_total(cls): print('this is the ', cls, '.total:', cls.total, ' and its id: ', id(cls.total)) # cls是类本身,打印类属性total的值 print('this is the Fruit.total:', Fruit.total, 'and its id: ', id(Fruit.total)) print("=======================") @classmethod def set(cls, value): cls.total = value class Apple(Fruit): pass class Orange(Fruit): pass app1 = Apple() app1.set(10) app1.print_total() Apple.print_total() Fruit.set(2) app1.print_total() Fruit.print_total() """ output: this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264 this is the Fruit.total: 0 and its id: 1355200944 ======================= this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264 this is the Fruit.total: 0 and its id: 1355200944 ======================= this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264 this is the Fruit.total: 2 and its id: 1355201008 ======================= this is the <class '__main__.Fruit'> .total: 2 and its id: 1355201008 this is the Fruit.total: 2 and its id: 1355201008 ======================= """
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2025年02月21日
2025年02月21日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]