staticmethod 统计实例

#!python2
#-*- coding:utf-8 -*- class c1: amount_instance=0 def __init__(self):
c1.amount_instance+=1 @staticmethod def printresult():
print "Amount of Instance: " ,c1.amount_instance class sub1(c1): #此处定义没有作用,因为只会引用c1中amount_instance amount_instance=0 @staticmethod def printresult(): print "call by sub1" c1.printresult() class sub2(c1): #此处定义没有作用,因为只会引用c1中amount_instance amount_instance=0 i1,i2=c1(),c1() i1.amount_instance
Amount of Instance: 2 c1.amount_instance Amount of Instance: 2 s1,s2=sub1(),sub1() s1.printresult() call by sub1
Amount of Instance: 4 sub1.printresult() call by sub1
Amount of Instance: 4 s3=sub2() s3.printresult() Amount of Instance: 5

classmethod 统计实例

#!python2
#-*- coding:utf-8 -*-
#利用类方法管理每个类的实例计数器
#父类使用一个类方法来管理状态信息,该信息根据树中的类不同而不同,而且储存在类上
#类方法调用的是当前类的amount_instance而不是父类中的amount_instance class c1: amount_instance=0 @classmethod def printresult(cls): cls.amount_instance+=1 def __init__(self): self.printresult() class sub1(c1): amount_instance=0 def __init__(self): c1.__init__(self) class sub2(c1): amount_instance=0 i1=c1() i1.amount_instance
1 c1.amount_instance
1 s1,s2=sub1(),sub1() s1.amount_instance
2
sub1.amount_instance
2
c1.amount_instance
1 s3,s4,s5=sub2(),sub2(),sub2() s3.amount_instance
3 c1.amount_instance
1

所以

1)静态方法:适用于处理一个类的本地数据

2)类方法:适用于处理类层级中每个类的数据

Python OOP(3) staticmethod和classmethod统计实例的更多相关文章

  1. Python中的staticmethod和classmethod

    谈谈Python中的staticmethod和classmethod 首先值得说明的是staticmethod和classmethod都是python中定义的装饰器,用的时候需要在前面加@ 即@sta ...

  2. (译文)Python中的staticmethod与classmethod

    原文是stackoverflow的一则高票回答,原文链接 可能之前也有人翻译过,但是刚好自己也有疑惑,所以搬运一下,个人水平有限所以可能翻译存在误差,欢迎指正(如侵删). 尽管classmethod和 ...

  3. Python中的@staticmethod和@classmethod的区别

    一直搞不明白,类方法和静态方法的区别,特意研究了一下,跟大家分享一下. 为了方便大家了解两者的差别,以下的示例代码将有助于发现其中的差别: class A(object): def foo(self, ...

  4. 基于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 ...

  5. Python语言特性之3:@staticmethod和@classmethod

    问题:Python中@staticmethod和@classmethod两种装饰器装饰的函数有什么不同? 原地址:http://stackoverflow.com/questions/136097/w ...

  6. python中@staticmethod与@classmethod

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

  7. python中 staticmethod与classmethod区别

    staticmethod与classmethod区别 参考 https://stackoverflow.com/questions/136097/what-is-the-difference-betw ...

  8. python中 staticmethod与classmethod

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

  9. Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数 区别(完全解析)

    原文地址:http://blog.csdn.net/caroline_wendy/article/details/23383995 还有一篇:http://blog.csdn.net/carolzha ...

随机推荐

  1. zabbix客户端安装shadowscoks客户端监控访问google网站

    配置zabbix客户端配置文件 vim /etc/zabbix/zabbix_agentd.conf 添加  Include=/etc/zabbix/zabbix_agentd.d/ 添加脚本探测访问 ...

  2. Linux如何查看进程、杀死进程、查看端口等常用命令

    查看进程号 1.ps 命令用于查看当前正在运行的进程.grep 是搜索 例如: ps -ef | grep java表示查看所有进程里 CMD 是 java 的进程信息2.ps -aux | grep ...

  3. mongoDB: cursor not found on server

    查询mongoDB集合数据更新,数据有400w多.我一次用cursor(游标)取1w,处理更新.程序在某段时间运行中遍历游标时发生异常! DBCursor cursor = tabColl.find( ...

  4. C#写csv文件

    1.在项目中经常需要把报表下载为csv格式的文件,如何在C#中写csv文件,以下为一个简化的例子,不使用任何控件,旨在说明用法. 前端view 下载结果 2.创建一个MVC项目(Intranet Ap ...

  5. .Net 异步调用

    .NET异步编程之新利器——Task与Await.Async   一.  FrameWork 4.0之前的线程世界    在.NET FrameWork 4.0之前,如果我们使用线程.一般有以下几种方 ...

  6. 解决scrollView中嵌套viewPager的冲突问题

    很简单,在外层ScrollView中添加android:fillViewport="true"属性,然后给viewPager添加一个固定高度

  7. Junit的各种断言

    JUnit为我们提供了一些辅助函数,他们用来帮助我们确定被测试的方法是否按照预期的效果正常工作,通常,把这些辅助函数称为断言.下面我们来介绍一下JUnit的各种断言. 1.assertEquals 函 ...

  8. DirectShow使用心得

    用了3天时间,将DShow加入到了游戏中,记录下心得,方便要使用的童鞋以及以后的自己查看.1. Video Mixing Renderer 9,内部使用Direct3D 9,需要Windows XP或 ...

  9. linux epoll机制对TCP 客户端和服务端的监听C代码通用框架实现

    1 TCP简介 tcp是一种基于流的应用层协议,其“可靠的数据传输”实现的原理就是,“拥塞控制”的滑动窗口机制,该机制包含的算法主要有“慢启动”,“拥塞避免”,“快速重传”. 2 TCP socket ...

  10. freemarker 开始时间与当前时间进行比较

    <#if startTime?datetime lt .now?datetime>:年月日时分秒比较 <#if startTime?date lt .now?date>:年月日 ...