Python中staticmethod方法和classmethod方法区别
来源:互联网 发布:coc气球升级数据 编辑:程序博客网 时间:2024/06/10 10:11
(转载)http://www.firefoxbug.com/index.php/archives/2818/
前言
staticmethod和classmethod两个方法在python里是通过装饰器来实现的,语法分别是@staticmethod和@classmethod,本文就讨论下这两种方法的区别以及使用场景
定义方式差异
@classmethod和@staticmethod装饰方法时,对于被装饰方法本身定义有差异,主要体现在形参上。
@classmethod#第一个参数是类本身def class_method(cls, data):@staticmethod#不存在任何与类、实例相关的参数,包括cls、selfdef static_method(data):
调用方式差异
看看下面这个类:
class MyClass(object): def __init__(self): pass def normal_method(self, data): print "normal method: %s %s" % (self, data) @classmethod def class_method(cls, data): print "class method: %s %s" % (cls, data) @staticmethod def static_method(data): print "static method: %s " % (data)
- 正常调用方法
正常情况下,调用类的方法之前必须实例化
>>> mc = MyClass()>>> mc.normal_method("Hello World!")normal method: <__main__.MyClass object at 0x10667c590> hello world!
可以看到def normal_method(self, data)第一个参数是self,Python解释器在运行时会自动把运行实例传递给被调用方法,所以方法调用输出的结果是实例化后的object的内容。
- @classmethod调用
@classmethod 装饰器实现的功能是:类可以直接调用@classmethod装饰的方法,无需实例化。
>>> MyClass.class_method("hello world!")class method: <class '__main__.MyClass'> hello world!
可以看到被@classmethod装饰的函数cls变量被传递成类名
- @staticmethod调用
@staticmethod 装饰器实现的功能是:无论类是否实例化,可以直接调用@staticmethod修饰的函数
#类直接调用>>> MyClass.static_method("hello world!")static method: hello world!#类实例化后调用>>>mc.static_method("hello world!")static method: hello world!
使用场景
- @classmethod使用场景
由于Python本身是不支持函数的重载(顶多只能实现函数的取代。。。)但是classmethod可以实现类似重载的功能(尽管我个人认为还是比较丑陋)
class Date(object): def __init__(self, year, month, day): self.year = year self.month = month self.day = day def today(self): print self.year + '-' + self.month + '-' + self.daydate = Date('2016', '05', '29')date.today()#2016-05-29
假设现在Date实例化的参数需要支持一个list类型:[2016, 05, 29],再看看下面这段代码:
class Date(object): def __init__(self, year, month, day): self.year = year self.month = month self.day = day @classmethod def in_as_list(self, day_as_list): assert isinstance(day_as_list, list) (year, month, day) = (day_as_list[0], day_as_list[1], day_as_list[2]) return Date(year, month, day) def today(self): print self.year + '-' + self.month + '-' + self.daydate = Date.in_as_list(['2016', '05', '29'])date.today()#2016-05-29
- @staticmethod使用场景
被@staticmethod修饰的方法一般用于:跟类有关系的功能但在运行时又不需要实例和类参与的情况,比如更改环境变量或者修改其他类的属性等能用到静态方法。这种情况可以直接用函数解决,但这样同样会扩散类内部的代码,造成维护困难。比如Tornado代码中类的单例化:
@staticmethod def instance(): """Returns a global `IOLoop` instance. Most applications have a single, global `IOLoop` running on the main thread. Use this method to get this instance from another thread. In most other cases, it is better to use `current()` to get the current thread's `IOLoop`. """ if not hasattr(IOLoop, "_instance"): with IOLoop._instance_lock: if not hasattr(IOLoop, "_instance"): # New instance after double check IOLoop._instance = IOLoop() return IOLoop._instance
总结
@classmethod: 被装饰的方法被调用时,第一个实参是类名而不是类的实例,这就意味着可以用类直接调用被装饰的方法而不强依赖类实例化
@staticmethod: 被装饰的方法被调用时,不会传递类的实例或者类命,意味着可以把一个函数放在类里,但是在这个函数里是不能访问类的实例的,在函数实现的功能和类实例无关时候会比较有用。
- Python中staticmethod方法和classmethod方法区别
- python 中 @classmethod 、staticmethod 和 普通方法 的区别
- python中 classmethod、staticmethod和普通方法的区别
- python中@staticmethod 、@classmethod 与一般方法的区别
- python 类中@staticmethod,@classmethod和实例方法
- python中staticmethod和classmethod的区别
- python中classmethod和staticmethod的区别
- Python 中classmethod和staticmethod区别
- python中staticmethod和classmethod的区别
- Python 实例方法、@staticmethod和@classmethod
- Python 实例方法、@staticmethod和@classmethod
- Python 实例方法、@staticmethod和@classmethod
- python的classmethod和staticmethod方法
- Python 实例方法、@staticmethod和@classmethod
- python中@classmethod @staticmethod区别
- Python @classmethod和@staticmethod区别
- python 中staticmethod和classmethod
- python 静态方法staticmethod和类方法classmethod
- 数据库导入、导出操作
- 工具推荐 | 分析大数据最需要的Top 10数据挖掘工具
- 图书管理系统数据库版
- 根据车牌获取信息 pom 文件
- 最全的前端入门资源汇总
- Python中staticmethod方法和classmethod方法区别
- 阿里云 CodePipeline 持续集成之发布静态网站到OSS
- ZXing二维码、条形码生成和扫描
- Python:Relative import 相对路径 ValueError: Attempted relative import in non-package
- 人群计数--Mixture of Counting CNNs
- C++学习之--QT一些基本知识
- bzoj 3368: [Usaco2004]Farmer John's View 约翰看山
- 网站XML格式的网站地图(sitemap)格式
- OpenStack Ocata Releasenotes