python 中的@property的用法详解
作者:lbaihao 发布时间:2023-03-03 16:57:57
标签:python,@property,用法
1.什么是property
简单地说就是一个类里面的方法一旦被@property装饰,就可以像调用属性一样地去调用这个方法,它能够简化调用者获取数据的流程,而且不用担心将属性暴露出来,有人对其进行赋值操作(避免使用者的不合理操作)。需要注意的两点是
调用被装饰方法的时候是不用加括号的
方法定义的时候有且只能有self一个参数
>>> 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把复杂的处理过程封装到了方法里面去,取值的时候调用相应的方法名即可。
2.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方法可以接收四个参数
第一个参数是获得属性的方法名,调用 对象.属性时自动触发
第二个参数是设置属性的方法名, 给属性赋值时自动触发
第三个参数是删除属性的方法名,删除属性时自动触发
第四个参数是字符串,是属性的描述文档,调用对象.属性.doc时触发
3.用property代替getter和setter方法
>>>class Watermelon():
def __init__(self,price):
self._price = price #私有属性,外部无法修改和访问
def get_price(self):
return self._price
def set_price(self,new_price):
if new_price > 0:
self._price = new_price
else:
raise 'error:价格必须大于零'
用property代替getter和setter
>>>class Watermelon():
def __init__(self,price):
self._price = price
@property #使用@property装饰price方法
def price(self):
return self._price
@price.setter #使用@property装饰方法,当对price赋值时,调用装饰方法
def price(self,new_price):
if new_price > 0:
self._price = new_price
else:
raise 'error:价格必须大于零'
>>> watermelon = Watermelon(4)
>>>
>>> watermelon.price
4
>>>
>>> watermelon.price = 7
>>>
>>> watermelon.price
7
来源:https://blog.csdn.net/lbaihao/article/details/125351090


猜你喜欢
- 默认vue项目中已经使用vue-cli生成,安装axios,基于element-ui开发,axiosconfig目录和api目录是同级,主要
- 此方法创建了请求另一个URL的HTML超文本链接。语法string.link( hrefname )下面是参数的详细信息:&nb
- Nodejs 的大部分核心 API 都是基于异步事件驱动设计的,事件驱动核心是通过 node 中 Events 对象来实现事件的发送和监听回
- 引言在利用Python解决各种实际问题的过程中,经常会遇到从某个对象中抽取部分值的情况,切片操作正是专门用于完成这一操作的有力武器。理论上而
- 具体代码如下所述:__author__ = 'Yue Qingxuan'# -*- coding: utf-8 -*-#求质
- 改变conda虚拟环境的默认路径conda环境默认安装在用户目录C:\Users\username.conda\envs下,如果选择默认路径
- 今天以一个表单的自动提交,来进一步学习selenium的用法练习目标0)运用selenium启动firefox并载入指定页面(这部分可查看本
- 一个ASPJPEG组件综合操作的asp类CLASS相关文章:ASP怎么谈到应用到类的?ASP中类的详细介绍(class Property G
- 实际工作经历中,免不了有时候需要连接数据库进行问题排查分析的场景,之前一直习惯通过 mysql -uxxx -hxxxx -P1234 ..
- 我就废话不多说了,大家还是直接看代码吧!# -*- coding:utf-8 -*- # File: ceshitianqiimport u
- 本文章采用的是Qt4,是python(x,y) 套件中集成的,为啥不集成Qt5呢,懒得装啊:)正文:首先看成品:这个程序的功能是输入原价和降
- Python错误SyntaxError: unexpected EOF while parsing含义是解释器到底了都没找到它要找到的东西出
- json.dumps将一个Python数据结构转换为JSONimport jsondata = { 'na
- 本文针对安装mysql5.7.21的笔记进行了总结,分享给大家1、将下载好的mysql压缩包解压到安装目录下2、新建文件 my.ini,放置
- 好记星不如烂笔头,适时的总结梳理知识让人更轻松愉快。今天总结下学习和开发中遇到的JavaScript执行顺序的问题,今天挖个坑,以后会慢慢填
- 前言2015年,HTTP/2 发布,直到2021年公司的项目才开始在实践中应用;自己对http2诸多特点的理解只存在于字面上,于是尝试在no
- 关于 pynput pynput 可以监控我们的键盘和鼠标。目前具有此类功能的库有很多,比如 pygame 等游戏库,但是当我们只需要
- 项目github地址:bitcarmanlee easy-algorithm-interview-and-practice1.Python中
- SQLite3数据库的介绍和使用(面向业务编程-数据库)SQLite3介绍SQLite是一种用C语言实现的的SQL数据库它的特点有:轻量级、
- 一、性能度量性能度量目的是对学习期的泛华能力进行评估,性能度量反映了任务需求,在对比不同算法的泛华能力时,使用不同的性能度量往往会导致不同的