classmethod 和 staticmethod
我一般很少用到。
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的更多相关文章
- python基础知识讲解——@classmethod和@staticmethod的作用
python基础知识讲解——@classmethod和@staticmethod的作用 在类的成员函数中,可以添加@classmethod和@staticmethod修饰符,这两者有一定的差异,简单来 ...
- Python中的classmethod与staticmethod
首先,这是一个经典的问题. 我们首先做一个比较: classmethod的第一个参数是cls,即调用的时候要把类传入 这意味着我们我们可以在classmethod里使用类的属性,而不是类的实例的属性( ...
- 洗礼灵魂,修炼python(47)--巩固篇—定义类的方法之@classmethod,@staticmethod
定义类的方法,相信你会说,不就是在class语句下使用def () 就是定义类的方法了嘛,是的,这是定义的方法的一种,而且是最普通的方式 首先,我们已经知道有两种方式: 1.普通方法: 1)与类无关的 ...
- python基础-abstractmethod、__属性、property、setter、deleter、classmethod、staticmethod
python基础-abstractmethod.__属性.property.setter.deleter.classmethod.staticmethod
- 【python】Python 中的 classmethod 和 staticmethod
Python 中的 classmethod 和 staticmethod 有什么具体用途? 推荐地址:http://www.cnblogs.com/wangyongsong/p/6750454.htm ...
- python classmethod 和 staticmethod的区别
https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner 1. ...
- python的@classmethod和@staticmethod
本文是对StackOverflow上的一篇高赞回答的不完全翻译,原文链接:meaning-of-classmethod-and-staticmethod-for-beginner Python面向对象 ...
- python 封装,隐藏属性,绑定方法classmethod和staticmethod
[封装] 隐藏对象的属性和实现细节,仅对外提供公共访问方式. [好处] 1. 将变化隔离: 2. 便于使用: 3. 提高复用性: 4. 提高安全性: [封装原则] 1. 将不需要对外提供的内容都隐藏起 ...
- Fluent Python: Classmethod vs Staticmethod
Fluent Python一书9.4节比较了 Classmethod 和 Staticmethod 两个装饰器的区别: 给出的结论是一个非常有用(Classmethod), 一个不太有用(Static ...
随机推荐
- ChartControl ViewType.Pie3D 用法测试
效果图一. public partial class Form3 : Form { public Form3() { InitializeComponent(); } private void For ...
- iOS----------关于UDID和UUID的一些理解
一.UDID(Unique Device Identifier) UDID是Unique Device Identifier的缩写,中文意思是设备唯一标识. 在很多需要限制一台设备一个账号的应用中经 ...
- Android为TV端助力 关于android的一些基础知识
怕自己以后忘了,所以在这里先写写! equal和==的区别是,一个用于判断字符串,一个用于判断int是否相等 equal比较的是对象,==比较的是值
- Keystone, Start, Failed to Load Bson
If you have installed the Keystone.js, and properly installed mongodb, but when tried to start the k ...
- springboot 学习之路 1(简单入门)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- MyBatis(傻瓜式)框架
log4j的配置文件: 使用一个log4j.properties的配置文件,会设定log4j的设置信息,例如日志级别,日志输出方式,日志格式等等: # Set root category priori ...
- TURN Server Windows 安装程序
有了OfficeSIP TURN Server 安装包,记录一下. http://www.onlinedown.net/soft/94746.htm 开源代码(C#)和应用地址:https://sou ...
- springmvc复习笔记----springmvc姓名年龄例子:RequestParam 试水
继续 继上节http://www.cnblogs.com/tk55/p/6652394.html 重要部分颜色突出 结构 包 web.xml 乱码处理方面设置 <url-pattern>* ...
- python高级(1)—— 基础回顾1
Python基础回顾 认识变量 在学习了之前的Python零基础入门系列[洗礼灵魂,修炼Python](说明一下,这个系列现在回过来再来看这个名字确实好土啊,然后有些知识点感觉还不太精准,后期看如果有 ...
- SpringBoot自定义属性配置以及@ConfigurationProperties注解与@Value注解区别
我们可以在application.properties中配置自定义的属性值,为了获取这些值,我们可以使用spring提供的@value注解,还可以使用springboot提供的@Configurati ...