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. 哇哦!恍然大悟般的“share”功能的实现!

    有一个问题一直困扰着我,也是我一直没有时间去了解和学习的,那就是前端(移动端)实现分享到微信.QQ好友.QQ空间.新浪微博等等平台的功能实现,虽然之前有做过,但是都是上一个领导自己写好的,我直接拿来用 ...

  2. IBM Rational Appscan使用之扫描结果分析

    转自:http://www.nxadmin.com/penetration/825.html 之前有IBM Rational Appscan使用详细说明的一篇文章,主要是针对扫描过程中配置设置等.本文 ...

  3. 盘古分词demo,盘古分词怎么用

    1.下载PanGu.dll dll地址:http://download.csdn.net/detail/dhfekl/7493687 2.将PanGu.dll和词库引入到项目 最新词库地址:http: ...

  4. Atitit.软件硕士  博士课程 一览表 attilax 总结

    Atitit.软件硕士  博士课程 一览表 attilax 总结 1. Attilax聚焦的领域1 2. 研究生硕士博士课程汇总表1 3. 博士课程3 4. Attilax额外的4 5. 参考4 1. ...

  5. gitlab配置smtp时,总是提示需要鉴权,记录一下爬坑过程。

    配置好smtp,然后发送邮件时总是提示 Net::SMTPFatalError: 550 5.7.1 authentication is required 最后发现是因为在gitlab web界面上配 ...

  6. Junit 内部解密之二: TestResult + TestListener + Assert

    转自:http://blog.sina.com.cn/s/blog_6cf812be0100wbhw.html 之前我们看到了Test接口里面的run方法有个TestResult的参数,不错,这个类就 ...

  7. Android Studio 使用笔记:查看类结构和继承关系

    选中类 ,按下F4,可以打开类的源代码 在 Eclipse 中我们可以使用 Ctrl + O 组合热键查看类的结构,Android Studio 中也可以做到. View -> Tool Win ...

  8. GreenDao学习

    官方文档地址:http://greenrobot.org/greendao/documentation//introduction/ 英语不好的可以使用谷歌翻译,很轻松的看懂文档,不需要FQ. 看不懂 ...

  9. UFLDL深度学习笔记 (三)无监督特征学习

    UFLDL深度学习笔记 (三)无监督特征学习 1. 主题思路 "UFLDL 无监督特征学习"本节全称为自我学习与无监督特征学习,和前一节softmax回归很类似,所以本篇笔记会比较 ...

  10. shell脚本57问

    [1]交互方式.非交互方式.Shell脚本是什么? 经常与linux打交道,肯定对shell这个词不陌生.不明白shell意思的,可以自行翻译:外壳.去壳. 这个翻译结果怎么可以与计算机系统联系起来呢 ...