Python 静态方法和类方法的区别
python staticmethod and classmethod
Though classmethod and staticmethod are quite similar, there’s a slight difference in usage for both entities: classmethod must have a reference to a class object as the first parameter, whereas staticmethod can have no parameters at all.
Let’s look at all that was said in real examples.
尽管 classmethod 和 staticmethod 非常相似,但在用法上依然有一些明显的区别。classmethod 必须有一个指向 类对象 的引用作为第一个参数,而 staticmethod 可以没有任何参数。
让我们看几个例子。
例子 – Boilerplate
Let’s assume an example of a class, dealing with date information (this is what will be our boilerplate to cook on):
Python
1
2
3
4
5
6
|
class Date(object):
def __init__(self, day=0, month=0, year=0):
self.day = day
self.month = month
self.year = year
|
This class obviously could be used to store information about certain dates (without timezone information; let’s assume all dates are presented in UTC).
很明显,这个类的对象可以存储日期信息(不包括时区,假设他们都存储在 UTC)。
Here we have __init__, a typical initializer of Python class instances, which receives arguments as a typical instancemethod, having the first non-optional argument (self) that holds reference to a newly created instance.
这里的 init 方法用于初始化对象的属性,它的第一个参数一定是 self,用于指向已经创建好的对象。
Class Method
We have some tasks that can be nicely done using classmethods.
Let’s assume that we want to create a lot of Date class instances having date information coming from outer source encoded as a string of next format (‘dd-mm-yyyy’). We have to do that in different places of our source code in project.
利用 classmethod 可以做一些很棒的东西。
比如我们可以支持从特定格式的日期字符串来创建对象,它的格式是 (‘dd-mm-yyyy’)。很明显,我们只能在其他地方而不是 init 方法里实现这个功能。
So what we must do here is:
Parse a string to receive day, month and year as three integer variables or a 3-item tuple consisting of that variable.
Instantiate Date by passing those values to initialization call.
This will look like:
大概步骤:
- 解析字符串,得到整数 day, month, year。
- 使用得到的信息初始化对象
代码如下
Python
1
2
|
day, month, year = map(int, string_date.split('-'))
date1 = Date(day, month, year)
|
理想的情况是 Date 类本身可以具备处理字符串时间的能力,解决了重用性问题,比如添加一个额外的方法。
For this purpose, C++ has such feature as overloading, but Python lacks that feature- so here’s when classmethod applies. Lets create another “constructor”.
C++ 可以方便的使用重载来解决这个问题,但是 python 不具备类似的特性。 所以接下来我们要使用 classmethod 来帮我们实现。
Python
1
2
3
4
5
6
7
8
|
@classmethod
def from_string(cls, date_as_string):
day, month, year = map(int, date_as_string.split('-'))
date1 = cls(day, month, year)
return date1
date2 = Date.from_string('11-09-2012')
|
Let’s look more carefully at the above implementation, and review what advantages we have here:
We’ve implemented date string parsing in one place and it’s reusable now.
Encapsulation works fine here (if you think that you could implement string parsing as a single function elsewhere, this solution fits OOP paradigm far better).
cls is an object that holds class itself, not an instance of the class. It’s pretty cool because if we inherit our Date class, all children will have from_string defined also.
让我们在仔细的分析下上面的实现,看看它的好处。
我们在一个方法中实现了功能,因此它是可重用的。 这里的封装处理的不错(如果你发现还可以在代码的任意地方添加一个不属于 Date 的函数来实现类似的功能,那很显然上面的办法更符合 OOP 规范)。 cls 是一个保存了 class 的对象(所有的一切都是对象)。 更妙的是, Date 类的衍生类都会具有 from_string 这个有用的方法。
Static method
What about staticmethod? It’s pretty similar to classmethod but doesn’t take any obligatory parameters (like a class method or instance method does).
Let’s look at the next use case.
We have a date string that we want to validate somehow. This task is also logically bound to Date class we’ve used so far, but still doesn’t require instantiation of it.
Here is where staticmethod can be useful. Let’s look at the next piece of code:
staticmethod 没有任何必选参数,而 classmethod 第一个参数永远是 cls, instancemethod 第一个参数永远是 self。
Python
1
2
3
4
5
6
7
|
@staticmethod
def is_date_valid(date_as_string):
day, month, year = map(int, date_as_string.split('-'))
return day <= 31 and month <= 12 and year <= 3999
# usage:
is_date = Date.is_date_valid('11-09-2012')
|
So, as we can see from usage of staticmethod, we don’t have any access to what the class is- it’s basically just a function, called syntactically like a method, but without access to the object and it’s internals (fields and another methods), while classmethod does.
所以,从静态方法的使用中可以看出,我们不会访问到 class 本身 – 它基本上只是一个函数,在语法上就像一个方法一样,但是没有访问对象和它的内部(字段和其他方法),相反 classmethod 会访问 cls, instancemethod 会访问 self。
参考
Python 静态方法和类方法的区别的更多相关文章
- python 静态方法、类方法(二)
<Python静态方法.类方法>一文中曾用在类之外生成函数的方式,来计算类的实例的个数.本文将探讨用静态方法和类方法来实现此功能. 一使用静态方法统计实例 例1.static.py # - ...
- Python 静态方法、类方法和属性方法
Python 静态方法.类方法和属性方法 静态方法(staticmethod) staticmethod不与类或者对象绑定,类和实例对象都可以调用,没有自动传值效果,Python内置函数staticm ...
- [Python3 填坑] 017 实例方法、静态方法、类方法的区别
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 先上例子 2.2 分析 1. print( 坑的信息 ) 挖坑时间:2019/04/07 明细 坑的编码 内容 Py024-1 实例方法 ...
- python静态方法和类方法
静态方法和类方法在python2.2中被引用,经典类和新式类都可以使用.同时,一对内建函数:staticmethod和classmethod被引入,用来转化类中某一方法为这两种方法之一. 静态方法: ...
- Python 静态方法、类方法
今天我们来讨论一下Python类中所存在的特殊方法--静态方法.类方法. 一.定义 静态方法: 一种简单函数,符合以下要求: 1.嵌套在类中. 2.没有self参数. 特点: 1.类调用.实例调用,静 ...
- python 静态方法,类方法,类下面的函数区别
@clssmenthod(类方法) 与 @staticmethod(静态方法) 与类下面的函数的区别: 1.@classmethod修饰的方法def name(cls)需要通过cls参数传递当前类本身 ...
- python 静态方法,类方法 ,类的继承
转自: http://cowboy.1988.blog.163.com/blog/static/75105798201091141521583/ 1.关于定义类的一些奇特之处 今天在Python中 ...
- Python 静态方法,类方法,属性方法
方法的使用 静态方法 - 只是名义上归类管理,实际上在静态方法里访问不了类或实例中的任何属性. class Dog(object): def __init__(self,name): self.nam ...
- python静态方法、类方法
常规: class Dog(object): def __init__(self,name): self.name=name def eat(self): print('%s is eating'%s ...
随机推荐
- erlang中通过ip和子网掩码,计算地址范围 【二进制和十进制的转换】
在程序中,难免用的二进制和十进制之间的转换.遇到一个场景,通过ip和子网掩码,计算地址范围. 而地址范围为:网络地址+1—— 广播地址 -1 . 网络地址即ip和子网掩码的与的位运算.广播地址为:网 ...
- python循环导入的解决方案
解决循环import的方法主要有几种: 1.延迟导入(lazy import) 即把import语句写在方法或函数里面,将它的作用域限制在局部. 这种方法的缺点就是会有性能问题. 2. ...
- a positive definite matrix
https://en.wikipedia.org/wiki/Definite_quadratic_form https://www.math.utah.edu/~zwick/Classes/Fall2 ...
- java ClassLoader类加载器
原文 首先来了解一下字节码和class文件的区别: 我们知道,新建一个java对象的时候,JVM要将这个对象对应的字节码加载到内存中,这个字节码的原始信息存放在classpath(就是我们新建Java ...
- Qt & MySQL
Qt中如何进行MySQL连接与操作步骤: 1.向工程中的.pro文件增加QT += sql; 2.写一个通用的数据库连接类(Connect),一个static方法(CreateConnection), ...
- 7-4 汉密尔顿回路(25 分) 【STL】
7-4 汉密尔顿回路(25 分) 著名的"汉密尔顿(Hamilton)回路问题"是要找一个能遍历图中所有顶点的简单回路(即每个顶点只访问 1 次).本题就要求你判断任一给定的回路是 ...
- HBase存储方案设计
需求描述 将数据记录持久化存储在HBase中,需要支持如下功能: 支持高吞吐量读写操作,实时采集10,000条/秒: 支持动态添加字段: 支持服务端过滤: 支持部分字段修改. 设计方案 按列存储 优点 ...
- 【Leetcode-easy】Roman to Integer
罗马数字转化为整数 * 1.基本数字 Ⅰ.X .C 中的任何一个.自身连用构成数目.或者放在大数的右边连用构成数目.都不能超过三个:放在大数的左边只能用一个: * 2.不能把基本数字 V .L .D ...
- linux系统调用mount全过程分析【转】
本文转载自:https://blog.csdn.net/skyflying2012/article/details/9748133 系统调用本身是软中断,使用系统调用,内核也陷入内核态,异常处理,找到 ...
- golang字符串拼接性能对比
对比 +(运算符).strings.Join.sprintf.bytes.Buffer对字符串拼接的性能 package main import ( "bytes" "f ...