我一般很少用到。

Talk is cheap, show you the code.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
##############################################################################
# To explain how to use classmethod and staticmethod.
############################################################################### # ordinary class
class Date(object): def __init__(self, year=0, month=0, day=0):
self.year = year
self.month = month
self.day = day def __str__(self):
return '/'.join([str(x) for x in (self.year, self.month, self.day)]) # class with classmethod and staticmethod
class DateV2(object): def __init__(self, year=0, month=0, day=0):
self.year = year
self.month = month
self.day = day def __str__(self):
return '/'.join([str(x) for x in (self.year, self.month, self.day)]) @classmethod
def from_string(cls, str_): # Note, `cls`
y, m, d = str_.split('-')
date = cls(y, m, d)
return date @staticmethod
def data_str_valid(str_): # note no `self` or `cls`
y, m, d = str_.split('-')
return (0 < int(y) <= 9999) and (1 <= int(m) <= 12) and (1 <= int(d) <= 31) # derive
class DateV3(DateV2):
pass if __name__ == '__main__': ################################################### test ordinary class
d = Date(2018, 6, 25)
print d y, m, d = "2018-6-25".split('-')
d2 = Date(y, m, d)
print d2 ################################################## test class with classmethod and staticmethod
d3 = DateV2(2018, 6, 25)
print d3 #dstr = '2018-6-25'
dstr = '2018-6-32'
if DateV2.data_str_valid(dstr):
d4 = DateV2.from_string('2018-6-25')
print d4
else:
print 'dstr invalid!' ################################################# test derive
d5 = DateV3.from_string('2018-6-6')
print d5 #dstr = '2018-6-25'
dstr = '2018-6-32'
if DateV3.data_str_valid(dstr):
d6 = DateV3.from_string('2018-6-25')
print d6
else:
print 'dstr invalid!'

Output is,

2018/6/25
2018/6/25
2018/6/25
dstr invalid!
2018/6/6
dstr invalid!

完。

classmethod 和 staticmethod的更多相关文章

  1. python基础知识讲解——@classmethod和@staticmethod的作用

    python基础知识讲解——@classmethod和@staticmethod的作用 在类的成员函数中,可以添加@classmethod和@staticmethod修饰符,这两者有一定的差异,简单来 ...

  2. Python中的classmethod与staticmethod

    首先,这是一个经典的问题. 我们首先做一个比较: classmethod的第一个参数是cls,即调用的时候要把类传入 这意味着我们我们可以在classmethod里使用类的属性,而不是类的实例的属性( ...

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

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

  4. python基础-abstractmethod、__属性、property、setter、deleter、classmethod、staticmethod

    python基础-abstractmethod.__属性.property.setter.deleter.classmethod.staticmethod

  5. 【python】Python 中的 classmethod 和 staticmethod

    Python 中的 classmethod 和 staticmethod 有什么具体用途? 推荐地址:http://www.cnblogs.com/wangyongsong/p/6750454.htm ...

  6. python classmethod 和 staticmethod的区别

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

  7. python的@classmethod和@staticmethod

    本文是对StackOverflow上的一篇高赞回答的不完全翻译,原文链接:meaning-of-classmethod-and-staticmethod-for-beginner Python面向对象 ...

  8. python 封装,隐藏属性,绑定方法classmethod和staticmethod

    [封装] 隐藏对象的属性和实现细节,仅对外提供公共访问方式. [好处] 1. 将变化隔离: 2. 便于使用: 3. 提高复用性: 4. 提高安全性: [封装原则] 1. 将不需要对外提供的内容都隐藏起 ...

  9. Fluent Python: Classmethod vs Staticmethod

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

随机推荐

  1. es6 语法 (let 和const)

    一.let 和const 1.let 只在自己声明的块作用域中有效: function test(){ let a = 'a'; var b = 'b'; for(let i =1;i<3;i+ ...

  2. 如何用ABP框架快速完成项目(8) - 用ABP一个人快速完成项目(4) - 能自动化就不要手动 - 使用自动化测试(BDD/TDD)

    做为一个程序员, 深深知道计算机自动化的速度是比人手动的速度快的, 所以”快速”完成项目的一个重要武器就是: 能自动化就不要手动.   BDD/TDD有很多优势, 其中之一就是自动化, 我们这节文章先 ...

  3. 如何用ABP框架快速完成项目(11) - ABP只要加人即可马上加快项目进展- 全栈篇(2) - 不推荐模块组件化, 推荐微服务

    一个人写代码不需要担心会和别人的代码冲突, 不需要做代码合并, 不需要担心自己的代码被覆盖. 但是多个人一起写代码就需要担心这些问题.   解决这些问题的方法很多, 比如用AzureDevOps(TF ...

  4. 如何将web项目部署到weblogic

    在Eclipse中配置weblogic11g服务器: 下载并安装Eclipse:www.eclipse.org 下载并安装Weblogic Server Plugin for Eclipse:http ...

  5. java内存分配与垃圾回收

    JVM的内存分配主要基于两种,堆和栈. 我们来看一下java程序运行时候的内存分配策略: 1):静态存储区(方法区): 2):栈区: 3):堆区: 1):主要存放静态数据,全局static数据和常量. ...

  6. MySQL 性能优化-数据库死锁监控

    MySQL性能优化-数据库死锁监控 by:授客 QQ:1033553122 1)表锁定 通过检查 table_locks_waited 和 table_locks_immediate 状态变量来分析表 ...

  7. C#多线程图片爬虫

    写了个简单的多线程图片爬虫,整理一下.数据已经爬下来了,图片URL需要自行拼接,首先从Lawyers表中取的RawData字段,RawData中有一个list字段是json格式的数据,需要的只是lis ...

  8. DPA从DPA 10.0.352升级到DPA 11.0.373

    1: 解压安装文件SolarWinds-DPA-11.0.373-64bit.tar.gz [root@lnxmonitor tmp]# tar -xzvf SolarWinds-DPA-11.0.3 ...

  9. jQuery实现画面的展开、收起和停止

    主要用到动画效果中的三个操作 ("#id").slideDown(3000): // 后面的数字表示效果的时长 ("#id").stop(); ("# ...

  10. sqlserver——cube:多维数据集

    1.cube:生成多维数据集,包含各维度可能组合的交叉表格,使用with 关键字连接 with cube 根据需要使用union all 拼接 判断 某一列的null值来自源数据还是 cube 使用G ...