https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner

1.classmethod还可以调用内部的classmethod和staticmethod(是的可以调用staticmethod,cls.staticmethod)。

2.staticmethod不能调用内部的任何方法。

3.classmethod第一个参数cls表示这个类,staticmethod不需要这个参数,staticmethod只是一个函数,不能调用类内部的其它方法(可以被调用)。

#!/usr/bin/python
# -*- coding: utf-8 -*- class DateProcessor(object):
def __init__(self):
pass def parse_date(self, date_str):
print date_str
self.print_date(date_str) # 调用了calssmethod
self.print_date_1(date_str) # 调用了staticmethod @classmethod
def print_date(cls, date_str):
cls.print_date_2(date_str) # classmethod可以调用类里面其它classmethod
cls.print_date_1(date_str) # 也可以调用staticmethod
print date_str @classmethod
def print_date_2(cls, date_str):
# 可以被其它方法调用
print "date_str_2:", date_str @staticmethod
def print_date_1(date_str):
# 可以被其它方法调用,但是不能调用其它方法
print "date_str_1:", date_str if __name__ == "__main__":
date_process = DateProcessor()
date_process.parse_date("2017-06-29") # 实例化 DateProcessor.print_date("2017-06-30") # 不用实例化类,调用classmethod
DateProcessor.print_date_1("2017-06-30") # 不用实例化类,调用staticmethod

python classmethod 和 staticmethod的区别的更多相关文章

  1. python @classmethod和@staticmethod区别

    python 类方法和静态方法区别 python @classmethod和@staticmethod区别 Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢 ...

  2. Fluent Python: Classmethod vs Staticmethod

    Fluent Python一书9.4节比较了 Classmethod 和 Staticmethod 两个装饰器的区别: 给出的结论是一个非常有用(Classmethod), 一个不太有用(Static ...

  3. Python @classmethod和@staticmethod装饰器使用介绍

    @classmethod和@staticmethod装饰器使用介绍 by:授客 QQ:1033553122 简介 静态方法:类中用 @staticmethod装饰的不带 self 参数的方法.类的静态 ...

  4. Python中classmethod和staticmethod的区别

    学习python中经常会出现一些相近或者相似的语法模块等,需要对比分析才能加深记忆,熟练运用. staticmethod:静态方法 classmethod:类方法 在python中,静态方法和类方法都 ...

  5. python 3全栈开发-面向对象之绑定方法(classmethod与staticmethod的区别)、多态、封装的特性property

    一.面向对象绑定方法 一.类中定义的函数分成两大类 1.绑定方法(绑定给谁,谁来调用就自动将它本身当作第一个参数传入): 1. 绑定到类的方法:用classmethod装饰器装饰的方法. 为类量身定制 ...

  6. Python中的实例方法、classmethod和staticmethod的区别

    class NewsPaper(object): # 类属性 __print_times = 0 # 下划线表示私有属性 # 实例方法 def __init__(self, title, conten ...

  7. python3 @classmethod 和 @staticmethod 的区别

    如果您将某个东西定义为classmethod,这可能是因为您打算从类而不是类实例中调用它. 定义类方法的几种方式: 常规方式                       : 需要self隐士传递当前类 ...

  8. 洗礼灵魂,修炼python(47)--巩固篇—定义类的方法之@classmethod,@staticmethod

    定义类的方法,相信你会说,不就是在class语句下使用def () 就是定义类的方法了嘛,是的,这是定义的方法的一种,而且是最普通的方式 首先,我们已经知道有两种方式: 1.普通方法: 1)与类无关的 ...

  9. python -- @classmethod @staticmethod区别和使用

    python中的定义: class MyClass: ... @classmethod  # classmethod的修饰符 def class_method(cls, arg1, arg2, ... ...

随机推荐

  1. log4j的格式化打印

    log4j.properties的文件内容如下 log4j.rootLogger=INFO, stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleA ...

  2. Openssl将crt证书和key私钥合成pfx证书

    下载OpenSSL地址:http://slproweb.com/products/Win32OpenSSL.html 下载安装openssl 选择对应OpenSSL版本进行下载下载. 运行安装程序Wi ...

  3. RAC7——vip的理解

    VIP特点: 1 VIP是在clusterware安装最后阶段,通过脚本VIPCA创建的: 2 VIP作为一个Nodeapps类型的CRS Resource注册到OCR中,并由CRS维护状态: 3 V ...

  4. POJ2777(线段树裸题)

    题目:http://poj.org/problem?id=2777 别忘了各地的return: 有可能输入的L<R,手动swap: 似乎是多组输入? pushup和pushdown的位置. (原 ...

  5. 【linux】crontab命令

    一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动cro ...

  6. 关于 eclipse startexplorer插件 快速打开文件夹

    转自:http://basti1302.github.io/startexplorer/ Just drag-and-drop the button to the Eclipse menu bar t ...

  7. 关于seo优化的核心思想

    简单说下,针对网页检索结果进行评估,主要是围绕精确率和召回率进行,具体如下:1.相关性:query与结果说的是不是一回事2.需求强度:抓住主要需求3.丰富程度:详细全面4.有效性:能否真正满足5.时效 ...

  8. LwIP raw api下使用tcp keep alive

    // The following code is implemented after tcp_new() or in tcp_connected call back... xxx_connected( ...

  9. JS URL 使用base64加密与解密和MD5解密

    JS编码方式: <script type="text/javascript"> document.write(encodeURI("http://www.w3 ...

  10. springMVC学习(6)-包装pojo类型、数组、list、Map类型参数绑定

    一.包装类型pojo参数绑定: 需求:商品查询controller方法中实现商品查询条件传入. 实现方法: 1)在形参中 添加HttpServletRequest request参数,通过reques ...