#bim(property) class People: def __init__(self,name,weight,height): self.name=name self.weight=weight self.height=height @property def bim(self): return self.weight/(self.height**2)
#property setter deleter class Foo: def __init__(self,val): self.__NAME=val @property def name(self): return self.__NAME @name.setter def name(self,value): self.__NAME=value @name.deleter def name(self): del self.__NAME
#classmethod class Goods: __discount=0.8 def __init__(self,name,price): self.name = name self.__price = price @property def price(self): return self.__price * Goods.__discount @classmethod def change_discount(cls,new_discount): Goods.__discount=new_discount apple=Goods("apple",10) print(apple.price) Goods.change_discount(0.9) print(apple.price)
#反射 class Teacher: dic={"查看学生信息":"","查看老师信息":""} def __init__(self):pass @classmethod def class_method(cls): print("test") if hasattr(Teacher,"class_method"): getattr(Teacher,"class_method")() print(getattr(Teacher,"dic"))