转载自[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. java利用线程池处理集合

    java利用线程池处理集合 2018年07月23日 17:21:19 衍夏成歌 阅读数:866   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/s ...

  2. spring MVC 如何接收前台传入的JSON对象数组并处理

    spring MVC 如何接收前台传入的JSON对象数组 主要方法: (主要用到的包是 net.sf.json  即:json-lib-2.3-jdk15.jar 完整相关jar包: commons- ...

  3. (链表 双指针) leetcode 142. Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...

  4. node(基础三)_模块系统基础

      一.前言                                                                                         这篇文章主 ...

  5. 【Mac进销存管理软件】Daily Sales Pro Mac

        [简介] Daily Sales Mac版是Mac平台上的一款进销存软件,库存管理系统.Daily Sales Mac版是一款易于使用的进出库存管理软件,让您及时了解库存状况.销售收入.采购成 ...

  6. 三台机器之间ssh互信配置

    三台机器之间ssh互信配置 环境介绍:192.168.65.128    my1-222192.168.65.129  my2-223192.168.65.130    web224 # 步骤一:# ...

  7. OpenDayLight——HelloWorld

    OpenDayLight——HelloWorld 既然学习OpenDayLight编程这个鬼,就得像学语言一样来一个HelloWorld来试试水,经过几天的折腾,总算成功输出HelloWorld了,这 ...

  8. springcloud配置需要主要的地方

    Eureka服务端 注册中心 <!-- Eureka服务端 --> <dependency> <groupId>org.springframework.cloud& ...

  9. 在.NET程序中实现HttpServer功能

    亲爱的下午茶   博客园 首页 新随笔 联系 订阅 管理 随笔-6  文章-0  评论-10  在.NET程序中实现HttpServer功能   最近在实现一个可视化数据解析工具,需要在Wpf程序中实 ...

  10. Linux 内核中的数据结构:基数树(radix tree)

    转自:https://www.cnblogs.com/wuchanming/p/3824990.html   基数(radix)树 Linux基数树(radix tree)是将指针与long整数键值相 ...