• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python property的使用技巧分享

python 搞代码 4年前 (2022-01-09) 23次浏览 已收录 0个评论
文章目录[隐藏]

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:
    
  <mark>本文来源gaodaimacom搞#代%码@网-</mark>  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 修饰的方法

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python property的使用技巧分享

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址