1. 什么是property属性

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

# ############### 定义 ###############class Foo: def func(self): pass # 定义property属性 @property def prop(self): pass# ############### 调用 ###############foo_obj = Foo()foo_obj.func() # 调用实例方法foo_obj.prop # 调用property属性

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

定义时,在实例方法的基础上添加 @property 装饰器;并且仅有一个self参数

调用时,无需括号

 方法:foo_obj.func() property属性:foo_obj.prop

2. 简单的实例

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

根据用户请求的当前页和总数据条数计算出 m 和 n

根据m 和 n 去数据库中请求数据

# ############### 定义 ###############class Pager: 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# ############### 调用 ###############p = Pager(1)p.start # 就是起始值,即:mp.end # 就是结束值,即:n

从上述可见

Python的property属性的功能是:property属性内部进行一系列的逻辑计算,最终将计算结果返回。

3. property属性的有两种方式

装饰器 即:在方法上应用装饰器

类属性 即:在类中定义值为property对象的类属性

3.1 装饰器方式

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

Python中的类有经典类和新式类,新式类的属性比经典类的属性丰富。( 如果类继object,那么该类是新式类 )

经典类,具有一种@property装饰器

# ############### 定义 ############### class Goods: @property def price(self): return "laowang"# ############### 调用 ###############obj = Goods()result = obj.price # 自动执行 @property 修饰的 price 方法,并获取方法的返回值print(result)

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

#coding=utf-8# ############### 定义 ###############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')# ############### 调用 ###############obj = Goods()obj.price # 自动执行 @property 修饰的 price 方法,并获取方法的返回值obj.price = 123 # 自动执行 @price.setter 修饰的 price 方法,并将 123 赋值给方法的参数del obj.price # 自动执行 @price.deleter 修饰的 price 方法

注意

经典类中的属性只有一种访问方式,其对应被 @property 修饰的方法

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

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

class Goods(object): def __init__(self): # 原价 self.original_price = 100 # 折扣 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): del self.original_priceobj = Goods()obj.price # 获取商品价格obj.price = 200 # 修改商品原价del obj.price # 删除商品原价

3.2 类属性方式,创建值为property对象的类属性

当使用类属性的方式创建property属性时,经典类和新式类无区别

class Foo: def get_bar(self): return 'laowang' BAR = property(get_bar)obj = Foo()reuslt = obj.BAR # 自动调用get_bar方法,并获取方法的返回值print(reuslt)

property方法中有个四个参数

第一个参数是方法名,调用 对象.属性 时自动触发执行方法

第二个参数是方法名,调用 对象.属性 = XXX 时自动触发执行方法

第三个参数是方法名,调用 del 对象.属性 时自动触发执行方法

第四个参数是字符串,调用 对象.属性.doc ,此参数是该属性的描述信息

#coding=utf-8class Foo(object): def get_bar(self): print("getter...") return 'laowang' def set_bar(self, value):  """必须两个参数""" print("setter...") return 'set value' + value def del_bar(self): print("deleter...") return 'laowang' BAR = property(get_bar, set_bar, del_bar, "description...")obj = Foo()obj.BAR # 自动调用第一个参数中定义的方法:get_barobj.BAR = "alex" # 自动调用第二个参数中定义的方法:set_bar方法,并将“alex”当作参数传入desc = Foo.BAR.__doc__ # 自动获取第四个参数中设置的值:description...print(desc)del obj.BAR # 自动调用第三个参数中定义的方法:del_bar方法

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

class Goods(object): def __init__(self): # 原价 self.original_price = 100 # 折扣 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): del self.original_price PRICE = property(get_price, set_price, del_price, '价格属性描述...')obj = Goods()obj.PRICE # 获取商品价格obj.PRICE = 200 # 修改商品原价del obj.PRICE # 删除商品原价

4. Django框架中应用了property属性(了解)

WEB框架 Django 的视图中 request.POST 就是使用的类属性的方式创建的属性

class WSGIRequest(http.HttpRequest): def __init__(self, environ): script_name = get_script_name(environ) path_info = get_path_info(environ) if not path_info: # Sometimes PATH_INFO exists, but is empty (e.g. accessing # the SCRIPT_NAME URL without a trailing slash). We really need to # operate as if they'd requested '/'. Not amazingly nice to force # the path like this, but should be harmless. path_info = '/' self.environ = environ self.path_info = path_info self.path = '%s/%s' % (script_name.rstrip('/'), path_info.lstrip('/')) self.META = environ self.META['PATH_INFO'] = path_info self.META['SCRIPT_NAME'] = script_name self.method = environ['REQUEST_METHOD'].upper() _, content_params = cgi.parse_header(environ.get('CONTENT_TYPE', '')) if 'charset' in content_params: try: codecs.lookup(content_params['charset']) except LookupError: pass else: self.encoding = content_params['charset'] self._post_parse_error = False try: content_length = int(environ.get('CONTENT_LENGTH')) except (ValueError, TypeError): content_length = 0 self._stream = LimitedStream(self.environ['wsgi.input'], content_length) self._read_started = False self.resolver_match = None def _get_scheme(self): return self.environ.get('wsgi.url_scheme') def _get_request(self): warnings.warn('`request.REQUEST` is deprecated, use `request.GET` or ' '`request.POST` instead.', RemovedInDjango19Warning, 2) if not hasattr(self, '_request'): self._request = datastructures.MergeDict(self.POST, self.GET) return self._request @cached_property def GET(self): # The WSGI spec says 'QUERY_STRING' may be absent. raw_query_string = get_bytes_from_wsgi(self.environ, 'QUERY_STRING', '') return http.QueryDict(raw_query_string, encoding=self._encoding) # ############### 看这里看这里 ############### def _get_post(self): if not hasattr(self, '_post'): self._load_post_and_files() return self._post # ############### 看这里看这里 ############### def _set_post(self, post): self._post = post @cached_property def COOKIES(self): raw_cookie = get_str_from_wsgi(self.environ, 'HTTP_COOKIE', '') return http.parse_cookie(raw_cookie) def _get_files(self): if not hasattr(self, '_files'): self._load_post_and_files() return self._files # ############### 看这里看这里 ############### POST = property(_get_post, _set_post) FILES = property(_get_files) REQUEST = property(_get_request)

综上所述:

定义property属性共有两种方式,分别是【装饰器】和【类属性】,而【装饰器】方式针对经典类和新式类又有所不同。

通过使用property属性,能够简化调用者在获取数据的流程

property属性-应用

1. 私有属性添加getter和setter方法

class Money(object): def __init__(self): self.__money = 0 def getMoney(self): return self.__money def setMoney(self, value): if isinstance(value, int): self.__money = value else: print("error:不是整型数字")

2. 使用property升级getter和setter方法

class Money(object): def __init__(self): self.__money = 0 def getMoney(self): return self.__money def setMoney(self, value): if isinstance(value, int): self.__money = value else: print("error:不是整型数字") # 定义一个属性,当对这个money设置值时调用setMoney,当获取值时调用getMoney money = property(getMoney, setMoney) a = Money()a.money = 100 # 调用setMoney方法print(a.money) # 调用getMoney方法#100

3. 使用property取代getter和setter方法

重新实现一个属性的设置和读取方法,可做边界判定

class Money(object): def __init__(self): self.__money = 0 # 使用装饰器对money进行装饰,那么会自动添加一个叫money的属性,当调用获取money的值时,调用装饰的方法 @property def money(self): return self.__money # 使用装饰器对money进行装饰,当对money设置值时,调用装饰的方法 @money.setter def money(self, value): if isinstance(value, int): self.__money = value else: print("error:不是整型数字")a = Money()a.money = 100print(a.money)

定义一个属性_Python property属性相关推荐

  1. java定义一个类计算圆的半径,C++编程:定义一个圆类要求属性为半径,操作为计算圆的周长和面积...,java编程:定义一个圆类,属性为半径,方法为对输入的半径计...

    导航:网站首页 > C++编程:定义一个圆类要求属性为半径,操作为计算圆的周长和面积...,java编程:定义一个圆类,属性为半径,方法为对输入的半径计 C++编程:定义一个圆类要求属性为半径, ...

  2. Java编程题:定义一个Box类,属性有:长,宽,高,并初始化数据

    Java编程题:定义一个Box类,属性有:长,宽,高,并初始化数据 Box类: /*** Java编程题:定义一个Box类,属性有:长,宽,高,并初始化数据*/package Box;public c ...

  3. python定义一个dog类 类属性有名字_python 基础 12 初识类,类方法,类属性

    python 基础 12 初识类,类方法,类属性 # 面向过程 : 想要一个结果 写代码 实现计算结果 # 面向对象开发 : 有哪些角色 角色的属性和技能 两个角色之间是如何交互的 # 复杂的 拥有开 ...

  4. python定义一个dog类 类属性有名字毛色体重_面向对象实践,练习,Python

    1.定义一个汽车类(Car),属性有颜色,品牌,车牌号,并实例化两个对象 2.定义一个球员类(Player),属性有身高.体重.姓名,实例化两个球员,分别是姚明和科比 3.定义一个僵尸类(Zombie ...

  5. html定义一个集合,HTML标签属性集合

    HTML标签属性集合 更新时间:2017/2/8 10:28:00  浏览量:607  手机版 图象热点映射范围 锚,为文档定义连接 首字母缩写词 地址 块引用 放大字体 为文档中的其他锚定义基本UR ...

  6. python定义一个dog类 类属性有名字毛色体重_全面了解python中的类,对象,方法,属性...

    python中一切皆为对象,所谓对象:我自己就是一个对象,我玩的电脑就是对象,坐着的椅子就是对象,家里养的小狗也是一个对象...... 我们通过描述属性(特征)和行为来描述一个对象的.比如家里的小狗, ...

  7. python对象的三个属性_Python 对象属性的访问

    在 Python 中,一切皆对象.属性访问可以理解为是从一个已有的对象中获得另一个对象的方法.对象属性的访问涉及到对象的 __dict__ 属性.描述符等概念,以及 __getattribute__. ...

  8. python定义一个人类_Python类的定义、继承及类对象使用方法简明教程

    Python编程中类的概念可以比作是某种类型集合的描述,如"人类"可以被看作一个类,然后用人类这个类定义出每个具体的人--你.我.他等作为其对象.类还拥有属性和功能,属性即类本身的 ...

  9. python属于私有属性_Python私有属性和受保护的属性原理解析

    前言: Python不能像Java那样使用 private 和 protected 修饰符创建私有属性和受保护的属性,但是Python有个简单的机制,能避免子类意外覆盖"私有"属性 ...

最新文章

  1. 健康的身体是一切的保证 - 保护偶的颈椎!
  2. 《深入浅出Nodejs》笔记——模块机制(2)
  3. MySQL 数据库性能优化,看这篇就够了
  4. 丹麦奥尔堡大学计算机系博士,丹麦奥尔堡大学招收计算机全奖PHD
  5. UVA - 10340 ​​​​​​​All in All
  6. pip安装cinrad
  7. mysql 利用触发器(Trigger)让代码更简单
  8. 李佳琦谈公益:时代成就了我 我也要回馈社会
  9. 机器学习第三回——正则化
  10. mysql innodb缓冲池_InnoDB 缓冲池
  11. 和is哪个好_眼霜哪个牌子好用?这些品牌的眼霜睡前涂一涂,黑眼圈细纹没有了...
  12. 祝广大运维人:2020新年快乐!
  13. 转:C语言中如何将二维数组作为函数的参数传递
  14. 明明管理失败,跟距离远有什么关系?
  15. 如何搭建tftp服务器
  16. 海康威视摄像头rtsp推流至H5总结
  17. CSS实现播放暂停按钮样式
  18. [集训队作业2018]喂鸽子
  19. Waited long enough for: ServiceRecord 问题解决
  20. java文件批量下载打包成zip

热门文章

  1. Angular应用开发中遇到的问题
  2. Bootstrap select 多选并获取选中的值
  3. 秋色园QBlog技术原理解析:性能优化篇:用户和文章计数器方案(十七)
  4. 想转行学python过来人提醒大家几点
  5. Shell 函数、数组与正则表达式
  6. 使用VM虚拟机的一点小技巧
  7. iOS 一些基础的方法
  8. 嵌入式课程安排 嵌入式培训课程大纲参考
  9. 基于shiro的权限设计
  10. 利用Redis进行全页面缓存的简单Demo