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. gphoto2 canon eos450d

    hjs@ubuntu:~$ gphoto2 --capture-image-and-download                                                   ...

  2. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境

    1.资源准备 最近,在VmwareStation 10虚拟机上,基于CentOS5.4安装Oracle 11g RAC,并把过程记录下来.刚开始时,是基于CentOS 6.4安装Oracle 11g ...

  3. django 获取前端获取render模板渲染后的html

    function GetProxyServerByGroup(ths, action){ var _html = $.ajax({ url: "/nginx/get_proxy_server ...

  4. gitlab使用外部的postgresql、外部的redis服务器

    postgresql创建用户 su - postgres psql create role gitlab login encrypted password 'pass';\du ;显示用户 postg ...

  5. print 出来的信息添加到text文件里

  6. ASP.NET Web Pages:WebGrid 帮助器

    ylbtech-.Net-ASP.NET Web Pages:WebGrid 帮助器 1.返回顶部 1. ASP.NET Web Pages - WebGrid 帮助器 WebGrid - 众多有用的 ...

  7. restclient 访问 springmvc java工程接口

    一.tomcat和nginx 配置 /etc/nginx/conf.d/default.conf location /nsx{ proxy_pass http://nsx; proxy_connect ...

  8. Jquery判断IE6等浏览器的代码

    这好像是由几篇代码接在一起的,原文均来自网络,不记得出处了~ jquery中利用navigator.userAgent.indexOf来判断浏览器类型,并进行了一下处理,如果不想使用jquery,稍为 ...

  9. 1112 Stucked Keyboard (20 分)

    1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when you ...

  10. [UE4]C++调用蓝图函数:BlueprintImplementableEvent函数说明符用法

    用BlueprintImplementableEvent标明的函数在C++代码中不需要有方法体,方法体在蓝图中实现. 用法: 1,现在C++头文件中定义函数名 UFUNCTION(BlueprintI ...