@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。
时间: 2024-10-25 02:48:24