Python 静态方法、类方法和属性方法

时间:2023-03-09 17:06:40
Python 静态方法、类方法和属性方法

Python 静态方法、类方法和属性方法

静态方法(staticmethod)

staticmethod不与类或者对象绑定,类和实例对象都可以调用,没有自动传值效果,Python内置函数staticmethod将类中的函数定义成静态方法(特点:定义在类命名空间,与类无直接关系,不能访问实例变量或类变量)。

应用场景:编写类时需要采用不同的方式来创建实例,但是__init__()只有一个,此时静态方法就可以派上用场:

import time

class Date:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day @staticmethod
def now(): # 用Date.now()的形式去产生实例,该实例用的是当前时间
t = time.localtime() # 获取结构化时间格式
return Date(t.tm_year, t.tm_mon, t.tm_mday) # 新建实例并返回 @staticmethod
def tomorrow(): # 用Date.tomorrow()的形式去产生实例,该实例用的是明天的时间
t = time.localtime(time.time() + 86400)
return Date(t.tm_year, t.tm_mon, t.tm_mday) a = Date(2018, 5, 13) # 自定义时间
b = Date.now() # 采用当前时间
c = Date.tomorrow() # 采用明天的时间 print(a.year, a.month, a.day) # 2018 5 13
print(b.year, b.month, b.day) # 2018 5 13
print(c.year, c.month, c.day) # 2018 5 14

类方法(classmethod)

classmethod是给类用的,即绑定到类,类在使用时会将类本身当作参数传给类方法的第一个参数(即便是对象来调用也会将类当作第一个参数传入),类和实例都可以调用(特点:方法的第一个参数是类对象而不是实例对象,只能访问类变量,不能访问实例变量);Python内置函数classmethod将类中的方法定义成类方法。

应用场景:

import time

class Date:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day # @staticmethod
# def now(): # 用Date.now()的形式去产生实例,该实例用的是当前时间
# t = time.localtime() # 获取结构化时间格式
# return Date(t.tm_year, t.tm_mon, t.tm_mday) # 新建实例并返回 # @staticmethod
# def tomorrow(): # 用Date.tomorrow()的形式去产生实例,该实例用的是明天的时间
# t = time.localtime(time.time() + 86400)
# return Date(t.tm_year, t.tm_mon, t.tm_mday)
@classmethod
def now(cls):
t = time.localtime()
return cls(t.tm_year, t.tm_mon, t.tm_mday) # 哪个类调用,即用哪个类cls来实例化 class EuroDate(Date):
def __str__(self):
return "year: %s month: %s day: %s" % (self.year, self.month, self.day) euro = EuroDate.now()
print(euro) # year: 2018 month: 5 day: 13

注意:静态方法和类方法虽然是类里面的,但是如果实例使用也是可以的。只不过实例调用时容易让人混淆,不知道要干啥

属性方法(property)

property作用就是把一个方法变成静态属性,如下实例:

class Dog(object):

    def __init__(self,name):
self.name = name @property
def eat(self):
print(" %s is eating" %self.name) d = Dog("Golden")
d.eat()

运行程序结果如下:

Traceback (most recent call last):
Golden is eating
File "D:/PythonCode/oop directory/middle rank.py", line 44, in <module>
d.eat()
TypeError: 'NoneType' object is not callable

根据错误我们可以发现,此时eat已经变成静态属性了,不是方法,所以不能通过加()的方式进行调用,直接d.eat访问其属性就可,如下所示:

d = Dog("Golden")
d.eat # Golden is eating

为甚要使用property

将一个类的函数定义成静态属性之后,对象再去使用的时候直接使用obj.attribute,根本无法察觉自己的attribute是执行了一个函数之后得到的,这种特性的使用遵循了统一访问的原则

如果我们想要修改和删除该property生成的静态变量只能使用下面的方式:

class Foo:
def __init__(self, val):
self.__name = val @property
def name(self):
return self.__name @name.setter # 修改
def name(self, value):
if not isinstance(value, str):
raise TypeError("%s must be str" % value)
self.__name = value @name.deleter # 删除
def name(self):
print("name removed...")
# raise TypeError("can not delete") f = Foo("Husky")
print(f.name)
f.name = "Golden"
print(f.name)
del f.name