转载自[1]

一般要用某个类的方法,先要实例这个类。

但是可以通过@staticmethod和@classmethod,直接用“类.方法()”来调用这个方法。

而 @staticmethod和@classmethod 区别是

@staticmethod不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样。

@classmethod也不需要self参数,但第一个参数需要是表示自身类的cls参数。

@staticmethod中要调用到这个类的一些属性方法,可以直接类名.属性名或类名.方法名。

@classmethod有cls参数,可以来调用类的属性,类的方法,实例化对象等,避免硬编码。

下面上代码

class A(object):
bar = 1
def foo(self):
print 'foo' @staticmethod
def static_foo():
print 'static_foo'
print A.bar @classmethod
def class_foo(cls):
print 'class_foo'
print cls.bar
cls().foo() A.static_foo()
#输出
#static_foo
# A.class_foo()
#输出
#class_foo
#
#foo

参考:

[1] @staticmethod和@classmethod 作用和区别

http://blog.csdn.net/qq_15037231/article/details/77943109

python(三)@staticmethod和@classmethod使用和区别的更多相关文章

  1. 面试题:python 中 staticmethod 和 classmethod有什么区别

    面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...

  2. python 中 staticmethod 和 classmethod有什么区别

    面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...

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

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

  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中 staticmethod与classmethod区别

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

  6. 【Python】@staticmethod和@classmethod的作用与区别

    前言 Python其实有3个方法,即静态方法(staticmethod),类方法(classmethod)和实例方法,一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法.而使用@static ...

  7. python中@staticmethod与@classmethod

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

  8. python 之@staticmethod和@classmethod

    在python中,要调用一个类中的方法,一般的操作步骤如下: 1.实例化此类 2.调用此类中的方法 而@staticmethod和@classmethod则打破了这种引用方式,可以在不实例化类的情况下 ...

  9. python中 staticmethod与classmethod

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

随机推荐

  1. R语言绘图(FZ)

    P-Value Central Lmit Theorem(CLT) mean(null>diff) hist(null) qqnorm(null) qqline(null) pops<-r ...

  2. MySQL填充字符串函数 LPAD(str,len,padstr),RPAD(str,len,padstr)

    转: MySQL填充字符串函数 LPAD(str,len,padstr),RPAD(str,len,padstr) LPAD(str,len,padstr) 用字符串 padstr对 str进行左边填 ...

  3. java的集合

    Collection: 1.list ArrayList.Vector.LinkedList ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. Vector是 ...

  4. hdu 4279"Number"(数论)

    传送门 参考资料: [1]:https://www.2cto.com/kf/201308/233613.html 题意,题解在上述参考资料中已经介绍的非常详细了,接下来的内容只是记录一下我的理解: 我 ...

  5. shell基础之bash

    一直单单知道部署服务器等命令,shell语言还没有用心学习过,简单的学习下以供不时之需 .sh:bash脚本文件 很多时候需要多个命令来完成一项工作,而这个工作又常常是重复的,这个时候我们自然会想到将 ...

  6. Linux系统下DNS主从配置详解

    一.DNS概述DNS(Domain Name System),即域名系统.因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串. ...

  7. IE缓存查看的方法

    选择设置中的Internet选项中, 然后点击查看文件: 最终缓存目录:

  8. 新买苹果电脑,mac系统中小白应该了解哪些东西?

    本文旨在分享新买了mac电脑,应该做哪些设置,帮助苹果电脑小白轻松上手使用mac电脑,当然,新电脑肯定是需要安装各种软件,这里,小编推荐一下可以看看小编写的mac软件装机必备Mac 装机必备软件推荐, ...

  9. springmvc拦截器说明

    一般 我们在spring mvc的配置文件中 这样配置拦截器 <!--拦截器 --> <mvc:interceptors> <!--多个拦截器,顺序执行 --> & ...

  10. linux服务器,发现大量TIME_WAIT

    linux服务器,发现大量TIME_WAIT 今天登陆linux服务器,发现大量TIME_WAIT参考资料:http://coolnull.com/3605.html 酷喃|coolnull| » 大 ...