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 ...
随机推荐
- node起步
首先,在项目目录下创建一个叫 app.js 的文件,并写如以下代码: app.js const http = require('http'); const hostname = '127.0.0.1' ...
- K8S 部署 ingress-nginx (三) 启用 https
部署 https 证书 cd ~/ingress # 生成私钥 tls.key, 密钥位数是 2048 openssl genrsa -out tls.key 2048 # 使用 tls.key 生成 ...
- es6 语法 (模块化)
//export export let A=123; //导出 //导出函数 export function test(){ console.log('test'); } //导出类 export c ...
- loj#6032. 「雅礼集训 2017 Day2」水箱(并查集 贪心 扫描线)
题意 链接 Sol 神仙题+神仙做法%%%%%%%% 我再来复述一遍.. 首先按照\(y\)坐标排序,然后维护一个扫描线从低处往高处考虑. 一个连通块的内状态使用两个变量即可维护\(ans\)表示联通 ...
- [DOM基础]offsetHeight,clientHeight,scrollHeight,innerHeight,outerHeight等属性的解释
由于经常搞混这几个属性,所以查找资料总结一下,方便以后翻出来温习. 一.偏移量-以offset开头的 1.offsetHeight:元素在垂直方向上占用的空间大小,像素.包括元素的高度.可见的水平滚动 ...
- 照葫芦画瓢系列之Java --- Maven的配置
一.Maven仓库分类 Maven中,仓库只分为两类:本地仓库和远程仓库.当Maven根据坐标寻找构件的时候,它首先去查看本地仓库,如果本地仓库有此构件,则直接使用,如果本地仓库不存在此构件,或者需要 ...
- Linux网络编程--socket
1.socket的核心思想是,作为服务器间的进程间通信的最底层的实现,常用的大部分网络协议都是基于socket实现. 2.socket 是如何与最终的低层收发包建立联系的? 3.socket 是如何与 ...
- Android为TV端助力 apk静默安装
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/47803149 之前有很多朋友都问过我,在Android系统中怎样才能实现静默安装呢 ...
- centos7安装jdk环境
有时候安装一些软件或者服务都需要jdk环境,今天就在centos上安装最新的jdk环境. 检测历时安装 1.查看Linux自带的JDK是否已安装 # java -version 2.查看JDK信息 # ...
- Oracle解锁scott用户
解决: (1)conn sys/sys as sysdba;//以DBA的身份登录 (2)alter user scott account unlock;// 然后解锁 (3)conn scott/t ...