Fluent Python一书9.4节比较了 Classmethod 和 Staticmethod 两个装饰器的区别:

给出的结论是一个非常有用(Classmethod), 一个不太有用(Staticmethod).

今天我们就对这两个装饰器做更深入的了解和比较,

(一) Classmethod:

(1)什么时候使用Classmethod?

classmethod最常见的用途是定义备选构造方法

(2)如何使用Classmethod?

下面我们用一个示例来展示如何使用classmethod,

假如我们设计了一个日期类:

class Date:
def __init__(self, day=0, month=0, year=0):
self.day = day
self.month = month
self.year = year

显然这个类是可以用来存储日期的(不包含时区)

现在我们需要从格式"dd-mm-yyyy"的字符串创建很多Date类的实例,我们可能会在类外部先处理字符串,然后创建Date的实例:

string_date = "27-09-2017"
day, month, year = map(int, string_date.split('-'))
date1 = Date(day, month, year)

以上的方式可行,然而一个更为Pythonic的方式是使用classmethod:

class Date:
def __init__(self, day=0, month=0, year=0):
self.day = day
self.month = month
self.year = year @classmethod
def from_string(cls, string_date):
day, month, year = map(int, string_date.split('-'))
return cls(day, month, year)

在上面的Code里,from_string() 方法可以看作除了__init__之外的另一个构造方法,这个构造方法可以从日期格式字符直接创建实例:

string_date = "27-09-2017"
date2 = Date.from_string(string_date)

对于以上的Date类实现,我们需要看到classmethod装饰器的三个优点:

(a) 日期格式字符串处理是在一个地方,是可重用的

(b)比起在Date类外实现日期格式字符串处理,这里的封装更符合面向对象思想

(c)对于Date类可能有的子类,他们自动继承了from_string()方法

(二) Staticmethod:

(1)什么时候使用Staticmethod?

我从未见过不得不用staticmethod的情况, 如果想定义不需要与类交互的函数,那么在模块中定义也是完全可以的

(2)如何使用Staticmethod?

假如我们设计了一个Server类,有地址和端口两个属性:

class Server:
def __init__(self, ip_address, port):
self.ip_address = ip_address
self.port = port

现在我需要一个函数检查用户输入的是否是一个ipv4地址,这就可以用staticmethod来实现,因为检查ipv4地址的合法性和类本身并不需要交互:

import re

class Server:
def __init__(self, ip_address, port):
self.ip_address = ip_address
self.port = port @staticmethod
def is_ipv4(ip_string):
p = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
if p.match(ip_string):
return True
else:
return False

那么在该Server类中,只要涉及到检查ipv4的地方,我们都可以用到这个is_ipv4()方法了

其实这个方法也完全可以定义在模块里,效果是一样的

最后我们回到Fluent Python一书中对classmethod和staticmetho在行为上做的对比:

书中定义了Demo类,实现了一个classmethod,一个staticmethod:

class Demo:
@classmethod
def classmethod_demo(*args):
return args @staticmethod
def staticmethod_demo(*args):
return args

然后我们在控制台观察输出:

>>> import Example9_4
<Example9_4.Demo object at 0x7fd0b6dd6cf8>
>>> Example9_4.Demo.classmethod_demo()
(<class 'Example9_4.Demo'>,)
>>> Example9_4.Demo.staticmethod_demo()
()
>>> Example9_4.Demo.classmethod_demo('foo')
(<class 'Example9_4.Demo'>, 'foo')
>>> Example9_4.Demo.staticmethod_demo('foo')
('foo',)

我们可以看到无论怎么调用classmethod,第一个参数总是类名Demo,而staticmethod的行为与普通的函数类似。

Fluent Python: Classmethod vs Staticmethod的更多相关文章

  1. python @classmethod和@staticmethod区别

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

  2. python classmethod 和 staticmethod的区别

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

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

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

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

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

  5. (译文)Python中的staticmethod与classmethod

    原文是stackoverflow的一则高票回答,原文链接 可能之前也有人翻译过,但是刚好自己也有疑惑,所以搬运一下,个人水平有限所以可能翻译存在误差,欢迎指正(如侵删). 尽管classmethod和 ...

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

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

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

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

  8. 【python】Python 中的 classmethod 和 staticmethod

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

  9. python的@classmethod和@staticmethod

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

随机推荐

  1. 使用js函数格式化xml字符串带缩进

    遇到了一个做soap的API的操作,中途需要说明xml的组装模式等, 如上图,组装产生的mxl代码药格式化并展示.由于是在前端做的,所以需要将字符串将xml进行格式化并输出,找到别人写的算法稍加更改并 ...

  2. python人工智能爬虫系列:怎么查看python版本_电脑计算机编程入门教程自学

    首发于:python人工智能爬虫系列:怎么查看python版本_电脑计算机编程入门教程自学 http://jianma123.com/viewthread.aardio?threadid=431 本文 ...

  3. DOCTYPE导致MyEclipse无法正常格式化HTML的问题

    今天遇到在JSP代码中Ctrl+F无法正常格式化HTML代码,经过排查是DOCTYPE的原因. 之前写的是: <!DOCTYPE html PUBLIC "-//W3C//DTD XH ...

  4. java并发实战:连接池实现

    池化技术简介 在我们使用数据库的过程中,我们往往使用数据库连接池而不是直接使用数据库连接进行操作,这是因为每一个数据库连接的创建和销毁的代价是昂贵的,而池化技术则预先创建了资源,这些资源是可复用的,这 ...

  5. 剑指Offer-二维数组查找

    题目:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. ...

  6. C# IL DASM 使用-破解c#软件方法

    IL DASM反编译工具 使用C#的猿人或多或少都会对微软的IL反编译工具(ildasm.exe)有所认识.我最早接触到这工具是公司同事使用他反编译exe程序,进行研读和修改.感觉他还是很强大. IL ...

  7. Python知乎热门话题爬取

    本例子是参考崔老师的Python3网络爬虫开发实战写的 看网页界面: 热门话题都在 explore-feed feed-item的div里面 源码如下: import requests from py ...

  8. mysql库地址

    https://dev.mysql.com/downloads/connector/

  9. Codeforces Round #482 (Div. 2) :B - Treasure Hunt

    题目链接:http://codeforces.com/contest/979/problem/B 解题心得: 这个题题意就是三个人玩游戏,每个人都有一个相同长度的字符串,一共有n轮游戏,每一轮三个人必 ...

  10. eclipse注释任务标记

     一.概述 TODO: + 说明: 如果代码中有该标识,说明在标识处有功能代码待编写,待实现的功能在说明中会简略说明. FIXME: + 说明: 如果代码中有该标识,说明标识处代码需要修正,甚至代码是 ...