staticmethod classmethod property方法
阿新 • • 發佈:2017-08-25
修飾 attr 不能 div 接口 截斷 light cnblogs 單純
@staticmethod 靜態方法
函數修飾符,用來修飾一個函數,類似於裝飾器
class Dog(object): def __init__(self,name): self.name = name def eat(self,food): print(‘%s is eating %s‘%(self.name,food)) d = Dog(‘二哈‘) d.eat(‘包子‘) #二哈 is eating 包子
eat()方法上面加上 @staticmethod
class Dog(object): def __init__(self,name): self.name = name @staticmethod def eat(self,food): print(‘%s is eating %s‘%(self.name,food)) d = Dog(‘二哈‘) d.eat(‘包子‘) #TypeError: eat() missing 1 required positional argument: ‘food‘
提示 food 少傳遞一個參數
刪除food參數嘗試下
class Dog(object): def __init__(self,name): self.name = name @staticmethod def eat(self): print(‘%s is eating %s‘%(self.name,‘包子‘)) d = Dog(‘二哈‘) d.eat() #TypeError: eat() missing 1 required positional argument: ‘self‘
提示少一個位置參數self ,self默認不是自動傳進去的嗎,靜態方法就是截斷方法與類的聯系,就是說eat在這裏就只是一個單純的函數。
調用方式:類名。靜態方法() 也可以用對象調用方法的方式。
class Dog(object): def __init__(self,name): self.name = name @staticmethod def eat(): print(‘%s is eating %s‘%(‘5545‘,‘包子‘)) d = Dog(‘二哈‘) d.eat() #5545 is eating 包子 Dog.eat() #5545 is eating 包子
要在靜態方法裏面調用self,就必須把self本身傳進去
class Dog(object): def __init__(self,name): self.name = name @staticmethod def eat(self): print(‘%s is eating %s‘%(self.name,‘包子‘)) d = Dog(‘二哈‘) d.eat(d) #二哈 is eating 包子
靜態方法名義上歸類管理,實際上調用不了類或者實例的任何屬性。
@classmethod 類方法
函數修飾符,同樣用來裝飾函數
class Dog(object): def __init__(self,name): self.name = name @classmethod def eat(self): print(‘%s is eating %s‘%(self.name,‘包子‘)) d = Dog(‘二哈‘) d.eat() #AttributeError: type object ‘Dog‘ has no attribute ‘name‘
嘗試訪問類變量
class Dog(object): name= ‘秋田‘ def __init__(self,name): self.name = name @classmethod def eat(self): print(‘%s is eating %s‘%(self.name,‘包子‘)) d = Dog(‘二哈‘) d.eat() #秋田 is eating 包子
類方法只能訪問類變量,不能訪問實例變量。
property 屬性方法
class Dog(object): name= ‘秋田‘ def __init__(self,name): self.name = name @property def eat(self): print(‘%s is eating %s‘%(self.name,‘包子‘)) d = Dog(‘二哈‘) d.eat() #TypeError: ‘NoneType‘ object is not callable
去掉括號
class Dog(object): name= ‘秋田‘ def __init__(self,name): self.name = name @property def eat(self): print(‘%s is eating %s‘%(self.name,‘包子‘)) d = Dog(‘二哈‘) d.eat #二哈 is eating 包子
把一個方法變成靜態屬性
作為一個屬性,如果有參數要怎麽傳遞?
既然是屬性就是可以賦值的。
class Dog(object): name= ‘秋田‘ def __init__(self,name): self.name = name @property def eat(self,food): print(‘%s is eating %s‘%(self.name,food)) d = Dog(‘二哈‘) d.eat = ‘包子‘ #AttributeError: can‘t set attribute
直接賦值也是不可以的,同樣需要裝飾下
class Dog(object): name= ‘秋田‘ def __init__(self,name): self.name = name self._food = None @property def eat(self): print(‘%s is eating %s‘%(self.name,self._food)) @eat.setter def eat(self,food): print(‘set food is‘,food) self._food = food d = Dog(‘二哈‘) d.eat = ‘包子‘ #set food is 包子 d.eat #二哈 is eating 包子
屬性賦值會觸發 @eat.setter 下的 eat()方法。
刪除屬性
class Dog(object): name= ‘秋田‘ def __init__(self,name): self.name = name self.__food = None @property def eat(self): print(‘%s is eating %s‘%(self.name,self._food)) @eat.setter def eat(self,food): print(‘set food is‘,food) self.__food = food d = Dog(‘二哈‘) d.eat = ‘包子‘ d.eat del d.eat #AttributeError: ‘Dog‘ object has no attribute ‘_food‘
默認是不能刪除的,如果非要刪除就要重寫
class Dog(object): name= ‘秋田‘ def __init__(self,name): self.name = name self.__food = None @property def eat(self): print(‘%s is eating %s‘%(self.name,self.__food)) @eat.setter def eat(self,food): print(‘set food is‘,food) self.__food = food @eat.deleter def eat(self): del self.__food print(‘刪除成功‘) d = Dog(‘二哈‘) d.eat = ‘包子‘ set food is 包子 d.eat # 二哈 is eating 包子 del d.eat # 刪除成功 d.eat # AttributeError: ‘Dog‘ object has no attribute ‘_Dog__food‘
d.eat調用了@property下的eat 因為這個方法裏面有調用self.__food 而這個屬性被刪除了
有些場景不能簡單的通過定義靜態屬性來實現的。所有要把方法做成屬性。比如有些接口的API。
staticmethod classmethod property方法