python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python @property装饰器

Python深入分析@property装饰器的应用

作者:Flyme awei

这篇文章主要介绍了Python @property装饰器的用法,在Python中,可以通过@property装饰器将一个方法转换为属性,从而实现用于计算的属性,下面文章围绕主题展开更多相关详情,感兴趣的小伙伴可以参考一下

什么是property

简单地说就是一个类里面的方法一旦被@property装饰,就可以像调用属性一样地去调用这个方法,它能够简化调用者获取数据的流程,而且不用担心将属性暴露出来,有人对其进行赋值操作(避免使用者的不合理操作)。需要注意的两点是

>>> class Goods():
        def __init__(self,unit_price,weight):
            self.unit_price = unit_price
            self.weight = weight
        @property
        def price(self):
            return self.unit_price * self.weight
>>> lemons = Goods(7,4)
>>>
>>> lemons.price
28

上面通过调用属性的方式直接调用到 price 方法,property把复杂的处理过程封装到了方法里面去,取值的时候调用相应的方法名即可。

property属性定义的两种方式

A、装饰器方式

在类的方法上应用@property装饰器,即上面那种方式。

B、类属性方式

创建一个实例对象赋值给类属性

>>> class Lemons():
        def __init__(self,unit_price=7):
            self.unit_price = unit_price
        def get_unit_price(self):
            return self.unit_price
        def set_unit_price(self,new_unit_price):
            self.unit_price = new_unit_price
        def del_unit_price(self):
            del self.unit_price
        x = property(get_unit_price, set_unit_price, del_unit_price)
>>> fruit = Lemons()
>>> 
>>> fruit.x                         #调用 fruit.x 触发 get_unit_price
7
>>> 
>>> fruit.x = 9                     #调用 fruit.x = 9 触发 set_unit_price
>>> 
>>> fruit.x
9
>>> 
>>> fruit.unit_price                #调用 fruit.unit_price 触发 get_unit_price
9
>>> del fruit.x                     #调用 del fruit.x 触发 del_unit_price 
>>> 
>>> fruit.unit_price
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    l.unit_price
AttributeError: 'Lemons' object has no attribute 'unit_price'

property方法可以接收四个参数

@property属性值的限制

在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把属性值随便改

class Person(object):
    pass
p = Person()
p.age = 1000

我们可以看到类中的属性被随意修改,很明显不能这样操作,为了限制age的范围,可以通过一个 set_age()方法来设置成绩,再通过一个 get_age() 来获取年龄,这样,在 set_score() 方法里,就可以检查参数

# -*- coding: utf-8 -*-
class Person(object):
    # score属性的值 限制范围为:0-88
    def get_age(self):
        return self.__age
    def set_age(self, value):
        if 0 <= value <= 88:
            self.__age = value
        else:
            self.__age = 0  # 给age一个初始值0
            raise ValueError('age的值必须在0-88之间')  # 自动抛异常
if __name__ == '__main__':
    p = Person()
    p.set_age(100)
    p.get_age()

ValueError('age的值必须在0-88之间')

我们发现,当我们设置限定age的范围的时候,超过对应的范围set_age设置值时raise方法会自动给我们抛出异常,但是上面的方法太过于复杂,所以引入我们的一个装饰器,装饰器是可以给函数动态加上功能。对于类的方法,装饰器一样起作用。Python内置的 @property 装饰器就是负责把一个方法变成属性调用的

# -*- coding: utf-8 -*-
class Person(object):
    # score属性的值 限制范围为:0-88
    @property  # 把 age 属性暴露出去
    def age(self):
        return self.__age
    @age.setter  # 当前 age 属性可以允许赋值
    def age(self, value):
        if 0 <= value <= 88:
            self.__age = value
        else:
            self.__age = 0  # 给age一个初始值0
            raise ValueError('age的值必须在0-88之间')  # 自动抛异常
    @property
    def name(self):
        self.__name = '张三'
        return self.__name
if __name__ == '__main__':
    p = Person()
    p.age = 18
    print(p.age)  # age属性可以读,可以写
    print(p.name)  # name 只读属性

@property 的实现比较复杂,先观察使用。

把一个getter方法变成属性,只需要加上 @property 就可以了,此时, @property 本身又创建了另一个装饰器 @age.setter ,负责把一个setter方法变成属性赋值,于是,我们就拥有一个可控的属性操作.

到此这篇关于Python深入分析@property装饰器的应用的文章就介绍到这了,更多相关Python @property装饰器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文