Python中关于property使用的小技巧

 更新时间:2021年09月29日 16:19:14   作者:忆想不到的晖  
俗话说条条大路通罗马,同样是完成一件事,Python 其实提供了好几个方式供你选择。property() 是一个比较奇葩的BIF,它的作用把方法当作属性来访问,从而提供更加友好访问方式

property属性

一种用起来像是使用实例属性一样的特殊属性,可以对应于某个方法

既要保护类的封装特性,又要让开发者可以使用 对象.属性 的方式操作方法,@property 装饰器,可以直接通过方法名来访问方法,不需要在方法名后添加一对 () 小括号。

来看下求圆的面积的例子

class Circle(object):

    PI = 3.14

    def __init__(self, r):
        # r圆的半径
        self.r = r
        self.__area = self.PI * self.r * self.r
    
    @property
    def area(self):
        return self.__area

    def get_area(self):
        return self.__area


In [2]: c = Circle(10)

In [3]: c.area
Out[3]: 314.0

In [4]: c.get_area()
Out[4]: 314.0

property属性的定义和调用要注意一下几点:

  • 定义时,在实例方法的基础上添加 @property 装饰器;并且仅有一个 self 参数
  • 调用时,无需括号 ()
  实例方法:c.get_area()
  property装饰的方法:c.area

具体实例

对于某商城中显示电脑主机的列表页面,每次请求不可能把数据库中的所有内容都显示到页面上,而是通过分页的功能局部显示,所以在向数据库中请求数据时就要显示的指定获取从第 m 条到第 n条的所有数据 这个分页的功能包括:

  • 根据用户请求的当前页和总数据条数计算出 m 和 n
  • 根据 m 和 n 去数据库中请求数据
class Pager(object):
    
    def __init__(self, current_page):
        
        # 用户当前请求的页码(第一页、第二页...)
        self.current_page = current_page
        
        # 每页默认显示10条数据
        self.per_items = 10 

    @property
    def start(self):
        val = (self.current_page - 1) * self.per_items
        return val

    @property
    def end(self):
        val = self.current_page * self.per_items
        return val

# ipython测验
In [2]: p = Pager(1)


In [3]: p.start		# 就是起始值,即:m
Out[3]: 0

In [4]: p.end		# 就是结束值,即:n
Out[4]: 10

In [5]: p = Pager(2)

In [6]: p.start
Out[6]: 10

In [7]: p.end
Out[7]: 20

property属性的有两种方式

  • 装饰器 即:在方法上应用装饰器 @property
  • 类属性 即:在类中定义值为 property 对象的类属性 property()

装饰器方式

在类的实例方法上应用 @property 装饰器

Python中的类有旧式类新式类新式类 的属性比 旧式类的属性丰富。

旧式类

旧式类,具有一种 @property 装饰器

   
class Goods:
    
    def __init__(self, name):
        self.name = name
        
    @property
    def price(self):
        return 100
    
# ipython测验
In [10]: g = Goods('手表')

In [11]: g.price
Out[11]: 100

新式类

新式类,具有三种 @property 装饰器

class Goods:
    """
    python3中默认继承object类
    以python2、3执行此程序的结果不同,因为只有在python3中才有@xxx.setter  @xxx.deleter
    """
    @property
    def price(self):
        print('@property')

    @price.setter
    def price(self, value):
        print('@price.setter')

    @price.deleter
    def price(self):
        print('@price.deleter')

        
# ipython测验
In [13]: g = Goods()

In [14]: g.price
@property

In [15]: g.price = 100
@price.setter

In [16]: del g.price
@price.deleter

  • g.price 单独调用自动执行 @property 修饰的 price 方法,并获取方法的返回值
  • g.price = 100 赋值自动执行 @price.setter 修饰的 price 方法,并将 100 赋值给方法的参数
  • del g.price 删除自动执行 @price.deleter 修饰的 price 方法

注意

  • 旧式类中的属性只有一种访问方式,其对应被 @property 修饰的方法
  • 新式类中的属性有三种访问方式,并分别对应了三个被@property@方法名.setter@方法名.deleter 修饰的方法

由于新式类中具有三种访问方式,我们可以根据它们几个属性的访问特点,分别将三个方法定义为对同一个属性:获取、修改、删除。

# Goods类@property应用

class Goods(object):

    def __init__(self, name, price):
        # 原价
        self.original_price = price

        # 折扣
        self.discount = 0.8

    @property
    def price(self):
        # 实际价格 = 原价 * 折扣
        new_price = self.original_price * self.discount
        return new_price

    @price.setter
    def price(self, value):
        self.original_price = value

    @price.deleter
    def price(self):
        print('删除商品原价')
        del self.original_price

        
# ipython测验
In [22]: g = Goods('小米手机', 2000)

In [23]: g.price
Out[23]: 1600.0

In [24]: g.price = 3000

In [25]: g.price
Out[25]: 2400.0

In [26]: del g.price
删除商品原价

In [27]: g.price
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-27-38ee45b469f2> in <module>
----> 1 g.price

<ipython-input-18-d5ea66eb7ece> in price(self)
     12     def price(self):
     13         # 实际价格 = 原价 * 折扣
---> 14         new_price = self.original_price * self.discount
     15         return new_price
     16

AttributeError: 'Goods' object has no attribute 'original_price'

类属性方式

创建值为 property 对象的类属性,当使用类属性的方式创建 property 属性时,旧式类新式类无区别

class Foo:
    
    def get_bar(self):
        return 'get_bar'

    BAR = property(get_bar)
    
    
# ipython 测验
In [32]: f = Foo()

In [33]: f.BAR
Out[33]: 'get_bar'

f.BAR 自动调用 get_bar() 方法,并获取方法的返回值

property() 中有个四个参数

  • 第一个参数是方法名,调用 对象.属性 时自动触发执行方法
  • 第二个参数是方法名,调用 对象.属性 = XXX 时自动触发执行方法
  • 第三个参数是方法名,调用 del 对象.属性 时自动触发执行方法
  • 第四个参数是字符串,调用 对象.属性._doc_ ,此参数是该属性的描述信息
class Foo(object):

    def __init__(self, bar):
        self.bar = bar
    
    def get_bar(self):
        print('get_bar')
        return self.bar

    def set_bar(self, value): 
        """必须要有两个参数"""
        print('set bar ' + value)
        self.bar = value

    def del_bar(self):
        print('del bar')
        del self.bar

    BAR = property(get_bar, set_bar, del_bar, "bar description...")

    
# ipython测验
In [50]: f = Foo('python')

In [51]: f.BAR
get_bar
Out[51]: 'python'

In [52]: f.BAR = 'Java'
set bar Java

In [53]: f.BAR
get_bar
Out[53]: 'Java'

In [54]: del f.BAR
del bar

property对象与@property装饰器对比

由于 类属性方式 创建 property 对象属性具有3种访问方式,我们可以根据它们几个属性的访问特点,分别将三个方法定义为对 同一个属性:获取、修改、删除 ,跟 @property 装饰器对比。

property对象类属性

# Goods类 property对象类属性 应用

class Goods(object):

    def __init__(self, name, price):
        # 原价
        self.original_price = price

        # 折扣
        self.discount = 0.8

    def get_price(self):
        # 实际价格 = 原价 * 折扣
        new_price = self.original_price * self.discount
        return new_price

    def set_price(self, value):
        self.original_price = value

    def del_price(self):
        print('删除商品原价')
        del self.original_price

    PRICE = property(get_price, set_price, del_price, "price description")

    
# ipython测验
In [59]: g = Goods('Mac电脑', 9000)

In [60]: g.PRICE
Out[60]: 7200.0

In [61]: g.PRICE = 10000

In [62]: g.PRICE
Out[62]: 8000.0

In [63]: del g.PRICE
删除商品原价

@property装饰器

# Goods类 @property装饰器 应用

class Goods(object):

    def __init__(self, name, price):
        # 原价
        self.original_price = price

        # 折扣
        self.discount = 0.8

    @property
    def price(self):
        # 实际价格 = 原价 * 折扣
        new_price = self.original_price * self.discount
        return new_price

    @price.setter
    def price(self, value):
        self.original_price = value

    @price.deleter
    def price(self):
        print('删除商品原价')
        del self.original_price
        
        
# ipython测验
In [59]: g = Goods('Mac电脑', 9000)

In [60]: g.PRICE
Out[60]: 7200.0

In [61]: g.PRICE = 10000

In [62]: g.PRICE
Out[62]: 8000.0

In [63]: del g.PRICE
删除商品原价

可以发现两种都可以实现但 @property 装饰器的在 旧式类中只有 @property , 没有@method.setter@method.deleter,新式类则两种都可以使用。因此看大家的习惯,选一种。

大自然用数百亿年创造出我们现实世界,而程序员用几百年创造出一个完全不同的虚拟世界。我们用键盘敲出一砖一瓦,用大脑构建一切。人们把1000视为权威,我们反其道行之,捍卫1024的地位。我们不是键盘侠,我们只是平凡世界中不凡的缔造者 。

到此这篇关于Python中关于property使用的小技巧的文章就介绍到这了,更多相关Python property 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python实现二分查找算法

    python实现二分查找算法

    这篇文章主要介绍了python如何实现二分查找算法,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下。
    2020-09-09
  • wxPython电子表格功能wx.grid实例教程

    wxPython电子表格功能wx.grid实例教程

    这篇文章主要介绍了wxPython电子表格功能wx.grid实例教程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • Python编程求质数实例代码

    Python编程求质数实例代码

    这篇文章主要介绍了Python编程求质数实例代码,对求200,1000和100000以内的质数进行了测试,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01
  • 为2021年的第一场雪锦上添花:用matplotlib绘制雪花和雪景

    为2021年的第一场雪锦上添花:用matplotlib绘制雪花和雪景

    这篇文章主要介绍了为2021年的第一场雪锦上添花:用matplotlib绘制雪花和雪景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Python使用设计模式中的责任链模式与迭代器模式的示例

    Python使用设计模式中的责任链模式与迭代器模式的示例

    这篇文章主要介绍了Python使用设计模式中的责任链模式与迭代器模式的示例,责任链模式与迭代器模式都可以被看作为行为型的设计模式,需要的朋友可以参考下
    2016-03-03
  • Python新手学习装饰器

    Python新手学习装饰器

    在本篇文章里小编给大家整理的是一篇关于Python装饰器的相关知识点内容,需要的朋友们可以学习下。
    2020-06-06
  • Python何时应该使用Lambda函数

    Python何时应该使用Lambda函数

    这篇文章主要介绍了Python何时应该使用Lambda函数,Python 中定义函数有两种方法,一种是用常规方式 def 定义,函数要指定名字,第二种是用 lambda 定义,不需要指定名字,称为 Lambda 函数,需要的朋友可以参考下
    2019-07-07
  • 如何利用python给图片添加半透明水印

    如何利用python给图片添加半透明水印

    这篇文章主要给大家介绍了关于如何利用python给图片添加半透明水印的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-09-09
  • Python文件的操作示例的详细讲解

    Python文件的操作示例的详细讲解

    本问重点讲解了Python文件的读写原理、常用文件打开模式、文件对象的常用方法和目录的相关操作详细讲解,对Python文件操作相关知识感兴趣的朋友跟随小编一起看看吧
    2021-04-04
  • python如何统计序列中元素

    python如何统计序列中元素

    这篇文章主要为大家详细介绍了python如何统计序列中的元素,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03

最新评论