我一般很少用到。

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. angular select2 ng-model 取值 ng-change调用方法

    页面: 引入文件 '/select2.css', '/select2-bootstrap.css', '/select2.min.js', '/ui-select2.js' html: <div ...

  2. K8S dashboard

    kubernetes-dashboard有两种认证方式,一个token认证,另一个是Kubeconfig文件的认证.这个时候的认证不是UserAccount而是获取kubernetes集群资源信息的s ...

  3. 2018-12-03 VS Code英汉词典插件v0.0.7-尝试词性搭配

    续上文VS Code英汉词典插件v0.0.6-改为TS实现, 加测试后, 继续重构(提取常量, 避免var, 添加类型等等), 并完善测试. 测试方法参考: Testing Visual Studio ...

  4. 在PeopleSoft中,什么是AlterAudit,Sysaudit和DDDAudit报告

    Alter Audit-是一个进程,它标识任何需要SQL Alter process的记录.即:如果AD中定义的record与数据库的中定义不匹配则标识该记录为应该修改. SQL Alter-AD中的 ...

  5. Spring学习之旅(六)Spring AOP工作原理初探

    AOP(Aspect-Oriented  Programming,面向切面编程)是Spring提供的关键技术之一. AOP基于IoC,是对OOP(Object-Oriented Programming ...

  6. 性能测试 CentOS下结合InfluxDB及Grafana图表实时展示JMeter相关性能数据

    CentOS下结合InfluxDB及Grafana图表实时展示JMeter相关性能数据   by:授客 QQ:1033553122 实现功能 1 测试环境 1 环境搭建 2 1.安装influxdb ...

  7. Python对象相关内置函数

    针对一个对象,通过以下几个函数,可以获取到该对象的一些信息. 1.type() ,返回某个值的类型 >>> type() <class 'int'> >>&g ...

  8. verilog实现两个数的最大公因数

    module gcd(clk,clr,go_i,x_i,y_i,d_o); input clk,clr; input go_i; :] x_i,y_i; :] d_o; :] x,y,r; alway ...

  9. DataGridView的单元格如何嵌入多个按钮控件

    前段时间我有一个朋友面试公司的时候遇到这个面试题,他也给了份原题给我瞧瞧,并没有什么特别的要点,关于这一类问题,如何在网格上的单元格嵌入多个控件(如按钮.超链接等)问题,我在网上搜索了下这类问题,发现 ...

  10. MySQL线程处于Waiting for table flush的分析

      最近遇到一个案例,很多查询被阻塞没有返回结果,使用show processlist查看,发现不少MySQL线程处于Waiting for table flush状态,查询语句一直被阻塞,只能通过K ...