Python Static Method
How to define a static method in Python?
Demo:
#!/usr/bin/python2.7
#coding:utf-8
# FileName: test.py
# Author: lxw
# Date: 2015-07-03 #Inside a class, we can define attributes and methods
class Robot:
'''Robot class. Attributes and Methods'''
population = 0
def __init__(self, name):
self.name = name
Robot.population += 1
print('(Initialize {0})'.format(self.name)) def __del__(self):
Robot.population -= 1
if Robot.population == 0:
print('{0} was the last one.'.format(self.name))
else:
print('There are still {0:d} robots working.'.format(Robot.population)) def sayHi(self):
print('Greetings, my master call me {0}.'.format(self.name)) '''
#The following class method is OK.
@classmethod
def howMany(cls): #cls is essential.
print('We have {0:d} robots.'.format(cls.population))
''' '''
#The following static method is OK.
@staticmethod
def howMany():
print('We have {0:d} robots.'.format(Robot.population))
''' #The following class method is OK.
def howMany(cls): #cls is essential.
print('We have {0:d} robots.'.format(cls.population))
howMany = classmethod(howMany) '''
#The following static method is OK.
def howMany():
print('We have {0:d} robots.'.format(Robot.population))
howMany = staticmethod(howMany)
''' def main():
robot1 = Robot("lxw1")
robot1.sayHi()
#staticmethod/classmethod 都既可以使用类名访问,也可以使用对象名访问, 但classmethod在定义时需要cls参数
Robot.howMany()
robot1.howMany() robot2 = Robot("lxw2")
robot2.sayHi()
Robot.howMany()
robot2.howMany() if __name__ == '__main__':
main()
else:
print("Being imported as a module.")
Differences between staticmethod and classmethod:
classmethod:
Its definition is mutable via inheritance, Its definition follows subclass, not parent class, via inheritance, can be
overridden by subclass. It is important when you want to write a factory method and by this custom attribute(s)
can be attached in a class.
staticmethod:
Its definition is immutable via inheritance. 类似其他语言中的static方法。
Python Static Method的更多相关文章
- Python OOP(2)-static method,class method and instance method
静态方法(Static Method): 一种简单函数,符合以下要求: 1.嵌套在类中. 2.没有self参数. 特点: 1.类调用.实例调用,静态方法都不会接受自动的self参数. 2.会记录所有实 ...
- java.lang.NoSuchMethodError: No static method setLayoutDirection(Landroid/graphics/drawable/Drawable;I)V in class Landroid/support/v4/graphics/drawable/DrawableCompat
Bug: java.lang.NoSuchMethodError: No static method setLayoutDirection(Landroid/graphics/drawable/Dra ...
- java.lang.NoSuchMethodError: No static method getFont
最近在Android Studio升级3.0后,在AlertDialog弹窗时报出了如下问题: java.lang.NoSuchMethodError: No static method getFon ...
- When to use static method in a java class
First , please understand its feature : * no need to instantiate a instance, i.e. simply you can jus ...
- How to call getClass() from a static method in Java?
刚才在学习Java 使用properties类,遇到这样的错误: Cannot make a static reference to the non-static method getClass() ...
- 【转】 Java虚拟机内存的堆区(heap),栈区(stack)和静态区(static/method)
JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method) 堆区:1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令 ...
- python dataframe (method,partial,dir,hasattr,setattr,getarrt)
# * _*_ coding:utf-8 _*___author__:'denny 20170730'from functools import reduceimport functoolsimpor ...
- python logging method 02
基本用法 下面的代码展示了logging最基本的用法. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...
- python string method
嗯,学习其它语言没这样全练过,嘻嘻 //test.py 1 # -*- coding: UTF-8 -*- 2 3 str = "i am worker" 4 print str. ...
随机推荐
- ZABBIX监控原理
zabbix实现原理及架构详解 想要用好zabbix进行监控,那么我们首要需要了解下zabbix这个软件的实现原理及它的架构.建议多阅读官方文档. 一.总体上zabbix的整体架构如下图所示: 重 ...
- jquery Fancybox使用教程
Fancybox是一款基于jquery的对图片展示播放的插件,当然,它html文本.flash动画.iframe以及ajax也予以支持.还可以通过css自定义外观,阴影效果超级赞! 演示效果:http ...
- 腾讯课堂1:使用Jmeter内置的录制功能进行录制
1.设置http代理服务器 打开火狐——点击选项——高级——网络——设置 设置完成点击确定 2.查看端口是否被占用的命令 netstat -ano 3.排除模式 .*\.gif .*\.css .* ...
- 拉格朗日乘子法(Lagrange multiplier)和KKT条件
拉格朗日乘子法: KKT条件:
- 与HttpSessionListener接口有关的方法是。
与HttpSessionListener接口有关的方法是. A.sessionInitialized() B.sessionCreated() C.sessionFinialized() D.sess ...
- Eclipse中的build path详解
http://blog.csdn.net/qqqqqq654/article/details/53043742
- 到底什么是hash
1.什么是hash算法 Hash(散列.杂凑)算法,是把任意长度的输入通过特定的算法变换成固定长度的输出,输出的值就是hash值.这个特定的算法就叫hash算法,hash算法并不是一个固定不变的算法. ...
- 利用Fiddler或Charles进行mock数据
使用场景:服务器数据不符合测试条件时,我们可以通过在本地创建虚拟数据来打到测试用例所描述的条件. fiddler使用方法 1.首先在本地创建txt数据:将抓到的response中的json数据拷贝到记 ...
- Java基础数据类型的默认值
1.整数类型(byte.short.int.long)的基本类型变量的默认值为0. 2.单精度浮点型(float)的基本类型变量的默认值为0.0f. 3.双精度浮点型(double)的基本类型变量的默 ...
- web.xml配置文件详解
笔者从大学毕业一直从事网上银行的开发,都是一些web开发项目.接下来会写一些关于web开发相关的东西,也是自己工作以来经常用到的内容.本篇先从web.xml文件开始介绍,笔者接触到的项目中都有这个文件 ...