Property
在python中,数据的属性和处理方法统称属性(attribute),其实,方法只是可调用的属性,除了二者之外,我们还可以创建特性(property)
现在举一个例子:
class LineItem: def __init__(self, description, weight, price): self.description = description self.weight = weight self.price = price def subtotal(self): return self.weight * self.price @property def weight(self): return self.__weight @weight.setter def weight(self, value): if value > 0: self.__weight = value else: raise ValueError('weight必须大于0') test = LineItem("test",1,1) a=test.subtotal() print(a)
解释:在为weight赋值的时候,会去执行@Property装饰的weight方法,会去检测weight的值大小
应用:在博客案例中。我对密码进行赋值的时候。当然不能用明文赋值
啊?在赋值的时候会去执行password 然后把hash过后的值存入
扩展
- 经典形式
class LineItem1: def __init__(self, description, weight, price): self.description = description self.weight = weight self.price = price def subtotal(self): return self.weight * self.price def get_weight(self): return self.__weight def set_weight(self,value): if value > 0: self.__weight = value else: raise ValueError('weight必须大于0') weigth = property(get_weight,set_weight) # 这种方法也是可以实现同样的效果,在python中这种方法叫做经典形式 # 说实话这个看着特别好理解,但是python用过语法糖@之后 发现好简洁,就喜欢用@property了,大家只要知道这个方法就可以了。 # 我目前了解的就是这两个,可能优势很多知识我没体会出来,但是不妨碍我使用啊,等要用的时候再去查查
- 属性的覆盖
class A: def __init__(self): self.a = 1 self.b = 2 B = A() print(B.a) print(B.b) print("-"*10) B.a=10 print(B.a) print(A().a) 1 2 ---------- 10 1 # 可以看出实例属性会覆盖(遮盖可能更加好一点,毕竟没有把原本的数据改了)类属性 # 下面在写一个property特性的 看看会不会被遮盖 class A: def __init__(self): self.a = 1 @property def data(self): return "i am superman" 1. B = A() 2. print(B.data) 3. print(vars(B)) 4. B.__dict__['data'] = '2017' 5. print(vars(B)) 6. print(B.data) 7. A.data="ppp" 8. print(B.data) 9. print(A.data) i am superman {'a': 1} {'data': '2017', 'a': 1} i am superman 2017 ppp # 可以看到在我们执行4的时候 返回的字典中多了data 的key # 运行6的时候任然看到"i am superman"->这个说明了特性没有被实例属性覆盖 # 但是我现在销毁了A中的property 直接给传入值,B.data读取的则是2017
总结:是不是感觉好奇怪,我明明都已经给B. dict ['data']赋值了啊,为什么读取的还是i am superman ,那和上面的例子不是冲突么。
说明下:如果实例属性和类属性同名的话会被覆盖掉。xx.data 无论怎么说都会优先去xx.class 中读取的。当我把data(propery)销毁之后,后面实例属性给他赋值了2017 所以会读取2017的
进群:960410445 即可获取数十套PDF!