静态方法(Static Method):

一种简单函数,符合以下要求:

1.嵌套在类中。

2.没有self参数。

特点:

1.类调用、实例调用,静态方法都不会接受自动的self参数。

2.会记录所有实例的信息,而不是为实例提供行为。

简单说staticmethod 无法访问类属性、实例属性,相当于一个相对独立的方法,跟类其实没什么关系,换个角度来讲,其实就是放在一个类的作用域里的函数而已。

#!python2
#-*- coding:utf-8 -*- class A:
v1="class argu" def __init__(self):
self.v1="instance argu" def fun1():
print "Static fun" fun1=staticmethod(fun1) a=A() #实例调用
a.v1 'instance argu' #类调用
A.v1 'class argu' a.fun1() A.fun1()

另一种写法是在python2.4以后用装饰器decorator

#!python2
#-*- coding:utf-8 -*- class A:
v1="class argu" def __init__(self):
self.v1="instance argu" @staticmethod def fun1():
print "Static fun" a=A() #实例调用
a.v1 'instance argu' #类调用
A.v1 'class argu' a.fun1() A.fun1()

类方法(Class Method):

一种函数,符合以下特征

1.类调用、或实例调用,传递的参数是一个类对象。

注意classmethod 可以访问类属性,无法访问实例属性。上述的变量val1,在类里是类变量,在实例中又是实例变量,所以容易混淆

适用于:

程序需要处理与类而不是与实例相关的数据。也就是说这种数据信息通常存储在类自身上,不需要任何实例也可以处理。

例如:

1.记录有一个类创建的实例的数目。

2.维护当前内存中一个类的所有实例的列表。

以上情况也可以同以下方式解决:

在类定义之外生成一个的简单函数

#记录实例创建数目

def amountinstance():
print "Amount of instances is : %d" %(c1.numInstance) class c1:
numInstance=0
def __init__(self):
c1.numInstance+=1 a=c1()
b=c1()
c=c1()
amountinstance() 3

因为类名称对简单函数而言,是可读取的全局变量,所以看到上例可以正常工作。

此外,函数名变成了全局变量,故仅适用于这个单一模块。

这样处理的缺点如下:

1.给文件的作用域添加了一个额外的名称,该名称仅能处理单个的类,不能应付多个类需要处理的情况。

2.该函数与类的直接关联很小,函数的定义可能在数百行代码之外的位置。

3.该函数位于类的命名空间之外,子类不能通过重新定义来代替或扩展它。

class MyClass:
val1 = 'Value 1'
def __init__(self):
self.val2 = 'Value 2'
def staticmd():
print '静态方法,无法访问val1和val2'
smd = staticmethod(staticmd) def classmd(cls):
print '类方法,类:' + str(cls) + ',val1:' + cls.val1 + ',无法访问val2的值'
cmd = classmethod(classmd) mc = MyClass()
mc.cmd() 类方法,类:__main__.MyClass,val1:Value 1,无法访问val2的值 MyClass.cmd() 类方法,类:__main__.MyClass,val1:Value 1,无法访问val2的值

另一种写法是在python2.4以后用装饰器decorator

class MyClass:
val1 = 'Value 1'
def __init__(self):
self.val2 = 'Value 2' @staticmethod def staticmd():
print '静态方法,无法访问val1和val2' @classmethod def classmd(cls):
print '类方法,类:' + str(cls) + ',val1:' + cls.val1 + ',无法访问val2的值' mc = MyClass()
mc.classmd() 类方法,类:__main__.MyClass,val1:Value 1,无法访问val2的值 MyClass.classmd() 类方法,类:__main__.MyClass,val1:Value 1,无法访问val2的值 #实例的val1与类的val1是不一样的,类方法可以访问的是类的val1 mc.val1 = 'Value changed'
mc.classmd()
类方法,类:__main__.MyClass,val1:Value 1,无法访问val2的值 MyClass.classmd()
类方法,类:__main__.MyClass,val1:Value 1,无法访问val2的值 MyClass.val1='Value changed'
mc.classmd() 类方法,类:__main__.MyClass,val1:Value changed,无法访问val2的值 MyClass.classmd() 类方法,类:__main__.MyClass,val1:Value changed,无法访问val2的值

最后汇总instance method, static method 和class method 的实现和调用

#!python2
#-*- coding:utf-8 -*- class Methods(): def im(self,v2):
self.v2=v2
print "Call instance method: %d " % v2 @staticmethod def sm(v2):
print "Call static method: %d " % v2 @classmethod def cm(cls,v2):
print "Call class method: %d " % v2 obj=Methods() #instance method call
#实例方法调用一定要将类实例化,方可通过实例调用 obj.im(1) Call instance method: 1 Methods.im(obj,1) Call instance method: 1 #static method call
#静态方法调用时不需要实例参数
obj.sm(2) Call static method: 2 Methods.sm(2) Call static method: 2 #class method call
# 类方法调用时,Python会把类(不是实例)传入类方法第一个(最左侧)参数cls(默认) obj.cm(3) Call class method: 3 Methods.cm(3) Call class method: 3

Python OOP(2)-static method,class method and instance method的更多相关文章

  1. Python OOP(1):从基础开始

    本文旨在Python复习和总结: 1.如何创建类和实例? # 创建类 class ClassName(object): """docstring for ClassNam ...

  2. <转>Python OOP(1):从基础开始

    转自  http://www.cnblogs.com/BeginMan/p/3510786.html 本文旨在Python复习和总结: 1.如何创建类和实例? # 创建类 class ClassNam ...

  3. (细节控)swift3.0与融云IMKIT开发问题(一部分) override func onSelectedTableRow Method does not override any method from its superclass

    原官网文档方案如下,在swift3.0的情况下出现 override func onSelectedTableRow  Method does not override any method from ...

  4. org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method test() on null context object

    前言 本文中提到的解决方案,源码地址在:springboot-thymeleaf,希望可以帮你解决问题. 至于为什么已经写了一篇文章thymeleaf模板引擎调用java类中的方法,又多此一举的单独整 ...

  5. Day04 -玩弄Ruby的方法:instance method与class method

    前情提要在第三天时,我们解说了如何在class里用include与extend,去使用module的method. Include is for adding methods to an instan ...

  6. iOS升级swift3 遇到Overriding non-open instance method outside of its defining module的解决方案

    最近将我之前的一个swift项目升级swift3,说多了都是泪... 其中,遇到这样一个错误: 这是用的三方:ENSwiftSideMenu时引出的 报了两个错: 1.Cannot inherit f ...

  7. Swift protocol extension method is called instead of method implemented in subclass

    Swift protocol extension method is called instead of method implemented in subclass protocol MyProto ...

  8. Python OOP知识积累

    目录 目录 前言 对象 类 面向对象 Python 面向对象编程三个基本特征 封装 继承 继承的作用 泛化与特化 实现继承的方式 多重继承 多态 方法多态 最后 前言 Python是一个功能非常强大的 ...

  9. Python OOP面向对象

    一.什么是面向对象的程序设计 1.面向过程 程序设计:核心是过程二字,过程指的是解决问题的步骤,即先干什么再干什么......面向过程的设计就好比精心设计好一条流水线,是一种机械式的思维方式. 优点是 ...

随机推荐

  1. Chrome 完整版官方下载

    Chrome下载默认不是完整版本,需要长久等等.so... 在下载地址后加参数:?standalone=1  解决问题.

  2. Smarty Caching缓存 ¥ 我要打赏

    Smarty Caching缓存 https://www.yiibai.com/smarty/smarty_caching.html https://www.w3cschool.cn/smarty/s ...

  3. 深入探析 Rational AppScan Standard Edition 新特性之 Glass Box 扫描

    众所周知,Web 应用安全测试通常有黑盒安全测试和白盒安全测试两种方法.这两种方法孰优孰劣一直众议纷纷.广为公认的是,这两种测试方法有着良好地互补性,两种测试方法的结合是未来安全测试技术的发展趋势.G ...

  4. 【HBase基础教程】1、HBase之单机模式与伪分布式模式安装(转)

    在这篇blog中,我们将介绍Hbase的单机模式安装与伪分布式的安装方式,以及通过浏览器查看Hbase的用户界面.搭建hbase伪分布式环境的前提是我们已经搭建好了hadoop完全分布式环境,搭建ha ...

  5. Android的View 事件传递

    欢迎转载,请附出处: http://blog.csdn.net/as02446418/article/details/47422891 1.基础知识 (1) 全部 Touch 事件都被封装成了 Mot ...

  6. 一个可以模拟GET,POST,PUT,DELET请求的HTTP在线工具

    一个简陋的HTTP请求工具,UI比较丑陋.0.0,可以用于接口调试. 之前在调试公司的远程接口的时候用的是curl,后来也在网上找到几种Http请求模拟的客户端程序.当时后来发现google app ...

  7. linux把某个文件拷贝到不同的目录下面

    find -name '7*' -type d|xargs -n 1 cp PBClassname.properties

  8. shiro集成encache

    针对多频次或者几乎不变的大数量的数据,我们可以通过缓存来实现,具体的比如说权限认证,这个,每次操作都需要权限认证,所以,这里添加encache注解.具体的认证过程是: 1,用户第一次访问用户权限信息, ...

  9. 微服务网关哪家强?一文看懂Zuul, Nginx, Spring Cloud, Linkerd性能差异

      导语:API Gateway是实现微服务重要的组件之一.面对诸多的开源API Gateway,如何进行选择也是架构师需要关注的焦点.本文作者对几个较大的开源API Gateway进行了压力测试,对 ...

  10. linux kernel的cmdline參数解析原理分析

    利用工作之便,今天研究了kernel下cmdline參数解析过程.记录在此.与大家共享.转载请注明出处.谢谢. Kernel 版本:3.4.55 Kernel启动时会解析cmdline,然后依据这些參 ...