python @classmethod 的使用场合

官方的说法: 
classmethod(function)
中文说明:
classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法,使用方法如下:

class C:
@classmethod
def f(cls, arg1, arg2, ...): ...

看后之后真是一头雾水。说的啥子东西呢???
 
自己到国外的论坛看其他的例子和解释,顿时就很明朗。 下面自己用例子来说明。
 
看下面的定义的一个时间类:

class Data_test(object):
day=0
month=0
year=0
def __init__(self,year=0,month=0,day=0):
self.day=day
self.month=month
self.year=year def out_date(self):
print "year :"
print self.year
print "month :"
print self.month
print "day :"
print self.day
t=Data_test(2016,8,1)
t.out_date()

输出:

year :
2016
month :
8
day :
1

符合期望。
 
如果用户输入的是 "2016-8-1" 这样的字符格式,那么就需要调用Date_test 类前做一下处理:

string_date='2016-8-1'
year,month,day=map(int,string_date.split('-'))
s=Data_test(year,month,day)

先把‘2016-8-1’ 分解成 year,month,day 三个变量,然后转成int,再调用Date_test(year,month,day)函数。 也很符合期望。
 
那我可不可以把这个字符串处理的函数放到 Date_test 类当中呢?
 
那么@classmethod 就开始出场了

class Data_test2(object):
day=0
month=0
year=0
def __init__(self,year=0,month=0,day=0):
self.day=day
self.month=month
self.year=year @classmethod
def get_date(cls,
string_date):
#这里第一个参数是cls, 表示调用当前的类名
year,month,day=map(int,string_date.split('-'))
date1=cls(year,month,day)
#返回的是一个初始化后的类
return date1 def out_date(self):
print "year :"
print self.year
print "month :"
print self.month
print "day :"
print self.day

在Date_test类里面创建一个成员函数, 前面用了@classmethod装饰。 它的作用就是有点像静态类,比静态类不一样的就是它可以传进来一个当前类作为第一个参数。
 
那么如何调用呢?

r=Data_test2.get_date("2016-8-6")
r.out_date()

输出:

year :
2016
month :
8
day :
1

这样子等于先调用get_date()对字符串进行处理,然后才使用Data_test的构造函数初始化。
 
这样的好处就是你以后重构类的时候不必要修改构造函数,只需要额外添加你要处理的函数,然后使用装饰符 @classmethod 就可以了。

python @classmethod 的使用场合的更多相关文章

  1. python @classmethod和@staticmethod区别

    python 类方法和静态方法区别 python @classmethod和@staticmethod区别 Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢 ...

  2. Python classmethod 修饰符

    描述 classmethod修饰符对应的函数不需要实例化,不需要self参数,但第一个参数需要是表示自身类的cls参数,可以调用类的属性,类的方法,实例化对象等. 语法 classmethod语法: ...

  3. python classmethod 和 staticmethod的区别

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

  4. python -- @classmethod @staticmethod区别和使用

    python中的定义: class MyClass: ... @classmethod  # classmethod的修饰符 def class_method(cls, arg1, arg2, ... ...

  5. Fluent Python: Classmethod vs Staticmethod

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

  6. python @classmethod

    写在前面 写博客的时候,我发现拖延症很严重,本来昨天要开始写的,结果东看看,西翻翻,啥也没落实下来.时间过去了,口袋里的收获却寥寥无几.讨厌这样的自己.我要戒掉这个不好的毛病. 拖延症的底层原因之一是 ...

  7. python3 @classmethod 的使用场合

    官方的说法: classmethod(function)中文说明:classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法,使用方法如下: class C: @clas ...

  8. python classmethod方法 和 staticmethod

    classmethod() 是一个类方法,用来装饰对应的函数.被classmethod 装饰之后就无需实例化,也不需要在函数中传self,但是被装饰的函数第一个参数需要是cls来表示自身类.可以用来调 ...

  9. Python @classmethod和@staticmethod装饰器使用介绍

    @classmethod和@staticmethod装饰器使用介绍 by:授客 QQ:1033553122 简介 静态方法:类中用 @staticmethod装饰的不带 self 参数的方法.类的静态 ...

随机推荐

  1. React Native 入门笔记一 -- Windows下基本环境配置

    一.准备工作 首先,需要安装nodejs,可以从nodejs官网下载,注意,React Native 要求node版本在4.0或以上:否则会出错,我建议把node版本升到最新版本,防止后面出现各种莫名 ...

  2. 使用CSS3创建文字颜色渐变(CSS3 Text Gradient)

    考虑一下,如何在网页中达到类似以下文字渐变的效果? 传统的实现中,是用一副透明渐变的图片覆盖在文字上.具体实现方式可参考 http://www.qianduan.net/css-gradient-te ...

  3. POJ 3734 Blocks (矩阵快速幂)

    题目链接 Description Panda has received an assignment of painting a line of blocks. Since Panda is such ...

  4. 深入理解Spring系列之十:DispatcherServlet请求分发源码分析

    转载 https://mp.weixin.qq.com/s/-kEjAeQFBYIGb0zRpST4UQ DispatcherServlet是SpringMVC的核心分发器,它实现了请求分发,是处理请 ...

  5. redis基础之redis-cluster(集群)(七)

    前言 redis的主流高可用集群模式为redis-cluster.从redis3.0+版本后开始支持,自带集群管理工具redis-trib.rb. 安装redis 参考:https://www.cnb ...

  6. python脚本-实现自动按规则创建指定大小和指定个数的文件案例

    # -*- coding: cp936 -*-#---------------------------------------------------------------------------- ...

  7. NFS生产场景优化

    1.硬件上多块网卡bond,增加吞吐量,至少千兆.sas/ssd磁盘组raid5或raid10 2.服务端配置:/data 172.16.1.0/24(rw,sync,all_squash,anonu ...

  8. TCP的状态兼谈Close_Wait和Time_Wait的状态

    原文链接: http://www.2cto.com/net/201208/147485.html TCP的状态兼谈Close_Wait和Time_Wait的状态   一 TCP的状态: 1).LIST ...

  9. 百度2017春招<空间中最大三角形面积的问题>

    题目: 三维空间中有N个点,每个点可能是三种颜色的其中之一,三种颜色分别是红绿蓝,分别用'R', 'G', 'B'表示. 现在要找出三个点,并组成一个三角形,使得这个三角形的面积最大.但是三角形必须满 ...

  10. mysql视图学习总结(转)

    一.使用视图的理由是什么?1.安全性.一般是这样做的:创建一个视图,定义好该视图所操作的数据.之后将用户权限与视图绑定.这样的方式是使用到 了一个特性:grant语句可以针对视图进行授予权限.2.查询 ...