类 (装饰器)
property
把一个方法伪装为一个属性
class Foo: def __init__(self,name): self.__name = name @property def name(self): return self.__name @name.setter #修改属性时使用 def name(self,newname): if type(newname) == str: self.__name = newname @name.deleter #属删除性时使用 def name(self): del self.__name dog = Foo("xiaohei") #赋值 dog.name = 11 #删除 del dog.name
classmethod
将一个对象方法装换为一个类的方法
class Goods: __discount = 0.8 def __init__(self,good,price): self.good = good self.price = price @classmethod def change_discount(cls,new_discount): cls.__discount = new_discount obj = Goods("houzi",10) obj.change_discount(2)
staticmethod
把一个动态方法转换为一个函数
class Student: def __init__(self,name): self.name = name @staticmethod def login(): pass obj = Student.login()