classmethod、staticmethod、property三个装饰器理解学习

python自带的三个装饰器,classmethod、staticmethod和property理解学习

在stackoverflow上有个高回答的问题,

Meaning of @classmethod and @staticmethod for beginner?

我就直接参考Rostyslav Dzinko 的回答改写了下

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
# -*-coding: utf-8 -*-
"""
@author: kyle
@time: 2021/3/24 13:08
"""
# classmethod、staticmethod、property


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

@classmethod
def from_string(cls, date_as_string):
day, month, year = map(int, date_as_string.split('-'))
date_1 = cls(day, month, year)
return date_1

@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 <= 9999

@property
def get_month(self):
return self.month

def __repr__(self):
return 'year: {}, month: {}, day: {}'.format(self.year, self.month, self.day)


date1 = Date(24, 3, 2021)
print(date1)

print(Date.is_date_valid('24-03-2021'))
date2 = Date.from_string('24-03-2021')
print(date2)
print(date2.get_month)

classmethod

classmethod是类装饰器,官方解释是指定一个类的方法为类方法,标致就是无需实例化就可以调用该方法。

然后我是这么理解的,比如上面的代码,一个Date类,最基本的用法就是实例化Date并且给三个参数:日、月、年,也就是这样:

1
2
date1 = Date(24, 3, 2021)
print(date1)

输出:

1
year: 2021, month: 3, day: 24

但是我们在实际的使用过程中,可能数据源那边的日期格式各种各样,不是所有的都是拆分成日月年这样一个个给你的,比如都是’24-03-2021’这样的格式,那么想用Date就要先对输入的日期进行一个格式化处理,先用一个方法把’24-03-2021’拆分成日月年。

classmethod装饰器就可以实现这个功能,还不需要重构函数,直接加进去一个日期解析的函数即可

1
2
3
4
5
@classmethod
def from_string(cls, date_as_string):
day, month, year = map(int, date_as_string.split('-'))
date_1 = cls(day, month, year)
return date_1

staticmethod

staticmethod我的理解就是,这个装饰器修饰的方法,其实在业务上和类本身没什么关系,就是为了给这个类加某个功能,比如对某个字段进行校验,获取当前日期等等,和业务其实没什么关联,调用的时候直接类就能调用,不需要实例化。
比如上面的代码,用staticmethod修饰了一个对输入日期合法值校验的方法,其实要不要这个对业务(输入固定格式年月日)都没什么影响,完全是扩充了功能。

1
2
3
4
@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 <= 9999

使用:

1
print(Date.is_date_valid('24-03-2021'))

输出:

1
True

property

用property装饰的方法,可以直接当成类的属性来使用,比如示例代码:

1
2
3
@property
def get_month(self):
return self.month

使用:

1
2
3
date2 = Date.from_string('24-03-2021')
print(date2)
print(date2.get_month)

输出:

1
2
year: 2021, month: 3, day: 24
3
文章目录
  1. classmethod
  2. staticmethod
  3. property
|