1.形式上的异同点:

在形式上,Python中:实例方法必须有self,类方法用@classmethod装饰必须有cls,静态方法用@staticmethod装饰不必加cls或self,如下代码所示:

 class A(object):
def __init__(self, name):
self.name = name def get_a_object(self):
return "get object method:{}".format(self.name) @staticmethod
def get_b_static():
return "get static method" @classmethod
def get_c_class(cls):
return "get class method" a = A(name="method")
print("{} by object method".format(a.get_a_object()))
try:
print(A.get_a_object())
except Exception:
print("Class can not call the instance method directly by class method")
print("{} by class method".format(A.get_b_static()))
print("{} by object method".format(a.get_b_static()))
print("{} by class method".format(A.get_c_class()))
print("{} by object method".format(a.get_c_class()))

执行结果:

get object method:method by object method
Class can not call the instance method directly by class method
get static method by class method
get static method by object method
get class method by class method
get class method by object method

从执行结果可以看出,实例方法必须实例化后调用不可用类点方法直接调用,静态方法和类方法既可以用实例点方法调用也可用类点方法直接调用

2.应用场景及本质上的异同点

对于应用场景上异同点主要是比较类方法和静态方法,先看一段代码:

 class Date:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day @staticmethod
def today_static():
t = time.localtime()
return Date(t.tm_year, t.tm_mon, t.tm_mday) @classmethod
def today(cls):
t = time.localtime()
return cls(t.tm_year, t.tm_mon, t.tm_mday) class NewDate(Date):
pass date = Date(2018, 12, 31)
new_data = NewDate(2018,12,31) print("class method -------------")
print(date.today().__class__)
print(Date.today().__class__)
print(NewDate.today().__class__)
print(new_data.today().__class__)
print("\n")
print("static method ------------")
print(date.today_static().__class__)
print(Date.today_static().__class__)
print(NewDate.today_static().__class__)
print(new_data.today_static().__class__)
Date类有year、month、day三个属性,静态方法today_static,类方法today,
NewDate类继承于Date,分别打印调用类方法和静态方法时属于哪个类
class method -------------
<class '__main__.Date'>
<class '__main__.Date'>
<class '__main__.NewDate'>
<class '__main__.NewDate'> static method ------------
<class '__main__.Date'>
<class '__main__.Date'>
<class '__main__.Date'>
<class '__main__.Date'>

通过结果可以看出:对于类方法today,调用Date.today()时cls=Date,调用NewDate.today()时,cls=NewDate,cls跟随调用类的变化而变化;对于静态方法today_static,指定了Date(t.tm_year, t.tm_mon, t.tm_mday),所以所属的类始终是Data,如果将Date改为:

class Date1:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day

此时对于@classmethod不会有任何变化,但是对于@staticmethod 如果today_static 返回值不是Date1(t.tm_year, t.tm_mon, t.tm_mday),则会报错

python中@staticmethod、@classmethod和实例方法的更多相关文章

  1. python中staticmethod classmethod及普通函数的区别

    staticmethod 基本上和一个全局函数差不多,只不过可以通过类或类的实例对象 (python里光说对象总是容易产生混淆, 因为什么都是对象,包括类,而实际上 类实例对象才是对应静态语言中所谓对 ...

  2. 【python】Python 中的 classmethod 和 staticmethod

    Python 中的 classmethod 和 staticmethod 有什么具体用途? 推荐地址:http://www.cnblogs.com/wangyongsong/p/6750454.htm ...

  3. 基于python中staticmethod和classmethod的区别(详解)

    例子 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 class A(object):   def foo(self,x):     print "executing foo ...

  4. 关于Python中的classmethod

    Python 中的 classmethod classmethod: 作用是直接将自己的类对象,传给类方法. 一.classmethod 1)不用classmethod的时候 你的代码可能是这样写的, ...

  5. python中 staticmethod与classmethod

    原文地址https://blog.csdn.net/youngbit007/article/details/68957848 原文地址https://blog.csdn.net/weixin_3565 ...

  6. python 类中staticmethod,classmethod,普通方法

    1.staticmethod:静态方法和全局函数类似,但是通过类和对象调用. 2.classmethod:类方法和类相关的方法,第一个参数是class对象(不是实例对象).在python中class也 ...

  7. Python中的类方法、实例方法、静态方法

    类方法 @classmethod 在python中使用较少,类方法传入的第一个参数是 cls,是类本身: 类方法可以通过类直接调用或者通过实例直接调用,但无论哪种调用方式,最左侧传入的参数一定是类本身 ...

  8. python中@staticmethod与@classmethod

    @ 首先这里介绍一下‘@’的作用,‘@’用作函数的修饰符,是python2.4新增的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.只可以对模块或者类定义的函数进行修饰,不允许修饰一个 ...

  9. Python 中的 classmethod 和 staticmethod 有什么具体用途?

    作者:李保银链接:https://www.zhihu.com/question/20021164/answer/18224953来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

随机推荐

  1. [PHP]session回收机制及php.ini session生命期

    由于PHP的工作机制,它并没有一个daemon线程,来定时地扫描session信息并判断其是否失效.当一个有效请求发生时,PHP会根据全局变量 session.gc_probability/sessi ...

  2. axis1 创建service服务端 , axis1 客户端

    axis1 服务端配置 1.首先建立一个项目 axisTest 不需多说 2.在lib下放入需要的jar包  点击下载 :axis所需的jar包下载 3.然后需要在web.xml里面加入: <s ...

  3. SwipeRefreshLayout详解和自定义上拉加载更多

    个人主页 演示Demo下载 本文重点介绍了SwipeRefreshLayout的使用和自定View继承SwipeRefreshLayout添加上拉加载更多的功能. 介绍之前,先来看一下SwipeRef ...

  4. Java学习——面向对象【3】

    1. 继承  java的类是单继承(一个子类只能继承一个父类),不能多继承(多个父类):A继承自B,A也继承自C,可以多重继承,就是A继承自B,B继承自C(A->B->C), 所有的类都继 ...

  5. Mysql8.0安装步骤

    Mysql8.0安装步骤 2018年05月10日 14:39:05 93年的香槟 阅读数:19628 标签: mysql 更多 个人分类: 数据库   版权声明:本文为博主原创文章,未经博主允许不得转 ...

  6. VM_Centos7.3_X64_安装Oracle12C 总结笔记

    声明:本文居多内容参考原文来之网络: 一:安装Centos7.3 虚拟机 1:操作系统下载 CentOS 7官方下载地址:https://www.centos.org/download/ 说明:本案例 ...

  7. Confluence 6 针对大数据量备份

    XML 站点备份的方式只针对 Confluence 包含有几千页面的情况,XML 备份所需要的时间随着数据量的变化而增加.另外的一个问题是 XML 站点的备份将会包含上 G 的附件数据,随着数据量的增 ...

  8. Confluence 6 编辑站点欢迎消息使用模板编辑器的小提示

    站点欢迎消息是一个模板而不是一个页面,所以你需要使用模板编辑器来对你的消息进行编辑. 你可以和在你 Confluence 中其他页面中一样,在站点欢迎消息模板中添加文本,连接和宏.但是添加图片的话会有 ...

  9. java多线程快速入门(二十二)

    线程池的好处: 避免我们过多的去new线程,new是占资源的(GC主要堆内存) 提高效率 避免浪费资源 提高响应速度 作用:会把之前执行某个线程完毕的线程不会释放掉会留到线程池中给下一个调用的线程直接 ...

  10. css样式之属性操作

    一.文本属性 1.text-align:cnter 文本居中 2.line heigth 垂直居中 :行高,和高度对应 3.设置图片与文本的距离:vertical-align 4.text-decor ...