装饰器方法
类的另外的特性,装饰器方法:静态方法(staticmethod)、类方法(classmethod)、属性方法(property)
一、静态方法
在方法名前加上@staticmethod装饰器,表示此方法为静态方法
class Dog(object): def __init__(self,name): self.name = name @staticmethod #在方法前加上staticmethod 装饰器定义静态方法 def eat(): print("dog is eating"
静态方法特性
特性:只是名义上归类管理,实际上在静态方法里访问不了类或实例中的任何属性
1、静态方法,是不可以传入self参数的,但是想传也可以,调用时必须传入实例本身
class Dog(object): def __init__(self,name): self.name = name @staticmethod #定义静态方法 def eat(self,food): #可以定义,但是需传入实例本身 print("{0} is eating {1}".format(self.name,food)) d = Dog("shabi") d.eat(d,"hotdog") #传入实例d本身,否则会报错 #输出 shabi is eating hotdog
2、静态方法可以用类直接调用,直接调用时,不可以直接传入self,否则会报错
class Dog(object): def __init__(self,name): self.name = name @staticmethod def eat(food): print("is eating {0}".format(food)) Dog.eat("hotdog") #输出 is eating hotdog
3、静态方法的使用场景,这种场景挺常见,就像os模块的中某些函数一样,都是使用了静态方法
import os os.system() os.mkdir()
二、类方法
在方法名前加上@classmethod装饰器,表示此方法为类方法
class Dog(object): name = "honggege" #定义静态属性 def __init__(self,name): self.name = name @classmethod #定义类方法 def eat(self,food): print("{0} is eating {1}".format(self.name,food))
类方法特性
特性:只能访问类变量(又叫静态属性),不能访问实例变量
1、访问实例变量
class Dog(object): def __init__(self,name): self.name = name @classmethod #定义类方法 def eat(self,food): print("{0} is eating {1}".format(self.name,food)) d = Dog("shabihong") d.eat("hotdog") #输出 File "D:/PycharmProjects/pyhomework/day7/类方法.py", line 11, in <module> d.eat("hotdog") File "D:/PycharmProjects/pyhomework/day7/类方法.py", line 8, in eat print("{0} is eating {1}".format(self.name,food)) AttributeError: type object 'Dog' has no attribute 'name'
2、访问类变量(又叫静态属性)
class Dog(object): name = "honggege" #定义类变量 def __init__(self,name): self.name = name @classmethod def eat(self,food): print("{0} is eating {1}".format(self.name,food)) d = Dog("shabihong") d.eat("hotdog") #输出 honggege is eating hotdog #调用的是类变量
3、使用场景:一般是需要去访问写死的变量,才会用到类方法装饰器
三、属性方法
在方法名前加上@property装饰器,表示此方法为属性方法
class Dog(object): def __init__(self,name): self.name = name @property #定义属性方法 def eat(self): print("{0} is eating".format(self.name))
属性方法特性
特性:把一个方法变成一个静态属性
1、静态属性的调用
class Dog(object): def __init__(self,name): self.name = name @property #定义属性方法 def eat(self): print("{0} is eating".format(self.name)) d = Dog("shabihong") d.eat #把方法变成静态属性调用 #输出 shabihong is eating
2、给转成的静态属性赋值
用@静态方法名.setter(属性装饰器)去装饰方法,来给转换后的静态属性赋值
class Dog(object): def __init__(self,name): self.name = name @property #定义属性方法 def eat(self): print("{0} is eating {1}".format(self.name,"honggege")) @eat.setter #定义一个可以传参的方法 def eat(self,food): print("set to food:",food) # self.__food = food d = Dog("shabihong") d.eat = "hotdog" #给转成的静态变量赋值 d.eat #输出 set to food: hotdog shabihong is eating honggege
上面代码没有把food传上去,那是因为传参方法,没有把接收之前的food的赋值,修改成如下代码就能成功上传:
class Dog(object): def __init__(self,name): self.name = name self.__food = None @property #定义属性方法 def eat(self): print("{0} is eating {1}".format(self.name,self.__food)) @eat.setter #定义可以设置变量赋值 def eat(self,food): print("set to food:",food) self.__food = food d = Dog("shabihong") d.eat #第一份赋值的是None d.eat = "hotdog" d.eat #第二个赋值是hotdog #输出 shabihong is eating None set to food: hotdog shabihong is eating hotdog #说明赋值成功
3、删除转变的静态属性
用@静态方法名.deleter(属性装饰器)去装饰,表明可以删除转化后的静态属性
class Dog(object): def __init__(self,name): self.name = name self.__food = None @property def eat(self): print("{0} is eating {1}".format(self.name,self.__food)) @eat.setter def eat(self,food): print("set to food:",food) self.__food = food @eat.deleter #定义可以删除eat这个静态属性 def eat(self): del self.__food print("food 变量删除完毕") d = Dog("shabihong") del d.eat #删除静态属性eat #输出 food 变量删除完毕
4、静态属性使用场景
一个航班当前的状态,是到达了、延迟了、取消了、还是已经飞走了, 想知道这种状态必须经历以下几步:
1. 连接航空公司API查询
2. 对查询结果进行解析
3. 返回结果给你的用户
因此这个status属性的值是一系列动作后才得到的结果,所以你每次调用时,其实它都要经过一系列的动作才返回你结果,但这些动作过程不需要用户关心, 用户只需要调用这个属性就可以
class Flight(object): def __init__(self,name): self.flight_name = name def checking_status(self): print("checking flight %s status " % self.flight_name) return 1 @property def flight_status(self): status = self.checking_status() if status == 0 : print("flight got canceled...") elif status == 1 : print("flight is arrived...") elif status == 2: print("flight has departured already...") else: print("cannot confirm the flight status...,please check later") @flight_status.setter #修改 def flight_status(self,status): status_dic = { 0 : "canceled", 1 :"arrived", 2 : "departured" } print("