python基础之类的静态方法和类方法

时间:2023-03-09 16:25:58
python基础之类的静态方法和类方法

一 静态方法

通常情况下,在类中定义的所有函数都是对象的绑定方法,对象再调用绑定方法时会自动将自己作为参数传递给方法的第一个参数。除此之外还有两种常见的方法:静态方法和类方法,二者是为类量身定制的,但是实例非要使用,也不会报错。

# class Foo:
# def spam(self):
# print('----->',self)
#
# # Foo.spam(123123)
#
# f1=Foo()
# f1.spam() class Foo:
@staticmethod #装饰器 spam=staticmethod(spam)
def spam(x,y,z):
print(x,y,z) # Foo.spam(1,2,3)
f2=Foo()
f2.spam(1,2,3)

应用场景:

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('1987',11,27) #自己定义时间
b=Date.now() #采用当前时间
c=Date.tomorrow() #采用明天的时间 #但凡是定义在类的内部,并且被staticmethod装饰器修饰过的方法,都是解除绑定的方法,实际上就函数:就没有自动传值功能了 print(a.year,a.month,a.day)
print(b.year,b.month,b.day)
print(c.year,c.month,c.day) #但凡是定义在类的内部,并且没有被任何装饰器修饰过的方法,都是绑定方法:有自动传值功能

二 类方法

类方法是给类用的,类再使用时会将类本身当做参数传给类方法的第一个参数,Python为我们内置了函数classmethod来把类中的函数定义成类方法。

补充:

__str__定义在类内部,必须返回一个字符串类型。

什么时候会触发它的执行呢?打印由这个类产生的对象时,会触发执行。

class People:
def __init__(self,name,age):
self.name = name
self.age = age
def __str__(self):
return '<name:%s,age:%s>'%(self.name,self.age) p1=People('egon',18)
print(p1)

应用场景:

import time
class Date:
def __init__(self,year,month,day):
self.year=year
self.month=month
self.day=day
@classmethod #把一个方法绑定给类:类.绑定到类的方法(),会把类本身当做第一个参数自动传给绑定到类的方法
def now(cls): #用Date.now()的形式去产生实例,该实例用的是当前时间
print(cls)
t=time.localtime() #获取结构化的时间格式
obj=cls(t.tm_year,t.tm_mon,t.tm_mday) #新建实例并且返回
return obj @classmethod
def tomorrow(cls):#用Date.tomorrow()的形式去产生实例,该实例用的是明天的时间
t=time.localtime(time.time()+86400)
return cls(t.tm_year,t.tm_mon,t.tm_mday) class EuroDate(Date):
def __str__(self):
return '年:%s,月:%s,日:%s' %(self.year,self.month,self.day) # e1=EuroDate.now()
# print(e1) e1=EuroDate(1,1,1)
print(e1)

总结

在类内部定义的函数无非三种用途
一:绑定到对象的方法
只要是在类内部定义的,并且没有被任何装饰器修饰过的方法,都是绑定到对象的 class Foo:
def test(self): #绑定到对象的方法
pass
def test1(): #也是绑定到对象的方法,只是对象.test1(),会把对象本身自动传给test1,因test1没有参数所以会抛出异常
pass 绑定到对象,指的是:就给对象去用,
使用方式:对象.对象的绑定方法(),不用为self传值
特性:调用时会把对象本身当做第一个参数传给对象的绑定方法 二:绑定到类的方法:classmethod
在类内部定义的,并且被装饰器@classmethod修饰过的方法,都是绑定到类的 class Foo:
def test(self): #绑定到对象的方法
pass
def test1(): #也是绑定到对象的方法,只是对象.test1(),会把对象本身自动传给test1,因test1没有参数所以会抛出异常
pass 绑定到对象,指的是:就给对象去用,
使用方式:对象.对象的绑定方法()
特性:调用时会把对象本身当做第一个参数传给对象的绑定方法 三:解除绑定的方法:staticmethod
既不与类绑定,也不与对象绑定,不与任何事物绑定
绑定的特性:自动传值(绑定到类的就是自动传类,绑定到对象的就自动传对象)
解除绑定的特性:不管是类还是对象来调用,都没有自动传值这么一说了 所以说staticmethod就是相当于一个普通的工具包 class Foo:
def test1(self):
pass
def test2():
pass @classmethod
def test3(cls):
pass
@classmethod
def test4():
pass @staticmethod
def test5():
pass test1与test2都是绑定到对象方法:调用时就是操作对象本身
<function Foo.test1 at 0x0000000000D8E488>
<function Foo.test2 at 0x0000000000D8E510>
test3与test4都是绑定到类的方法:调用时就是操作类本身
<bound method Foo.test3 of <class '__main__.Foo'>>
<bound method Foo.test4 of <class '__main__.Foo'>>
test5是不与任何事物绑定的:就是一个工具包,谁来都可以用,没说专门操作谁这么一说
<function Foo.test5 at 0x0000000000D8E6A8>