python 中的@property的用法详解
作者:lbaihao 时间:2023-03-03 16:57:57
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
标签:python,@property,用法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中如何使用Matplotlib库绘制图形
2021-09-27 11:59:04
![](https://img.aspxhome.com/file/2023/9/97699_0s.png)
Python数据可视化处理库PyEcharts柱状图,饼图,线性图,词云图常用实例详解
2022-03-02 10:43:08
![](https://img.aspxhome.com/file/2023/9/112609_0s.png)
python2.7读取文件夹下所有文件名称及内容的方法
2023-12-16 03:27:07
go doudou开发gRPC服务快速上手实现详解
2024-05-05 09:33:11
![](https://img.aspxhome.com/file/2023/7/127467_0s.png)
JavaScript面向对象中的封装和继承你了解吗
2024-06-07 16:00:16
![](https://img.aspxhome.com/file/2023/5/122145_0s.png)
python抓取京东小米8手机配置信息
2021-10-12 15:55:32
![](https://img.aspxhome.com/file/2023/4/108134_0s.png)
用户不需要信息快餐
2009-02-25 12:34:00
原生Javascript插件开发实践
2024-04-17 09:43:45
![](https://img.aspxhome.com/file/2023/6/136766_0s.png)
python实现从本地摄像头和网络摄像头截取图片功能
2022-04-22 22:32:39
python面向对象之类属性和类方法案例分析
2022-01-18 06:00:36
![](https://img.aspxhome.com/file/2023/1/118761_0s.png)
PYTHON发送邮件YAGMAIL的简单实现解析
2022-04-25 09:23:08
Python torch.flatten()函数案例详解
2022-11-11 16:04:27
基于node打包可执行文件工具_Pkg使用心得分享
2024-05-08 09:37:47
![](https://img.aspxhome.com/file/2023/7/129457_0s.png)
在Golang中使用Redis的方法示例
2024-04-28 09:16:01
10分钟用Python快速搭建全文搜索引擎详解流程
2023-11-06 16:13:41
![](https://img.aspxhome.com/file/2023/1/67321_0s.png)
python判断字符串是否包含子字符串的方法
2021-01-04 12:48:03
Python使用Selenium实现淘宝抢单的流程分析
2022-01-31 04:04:18
![](https://img.aspxhome.com/file/2023/4/68894_0s.png)
MySQL 的 21 个规范、优化最佳实践!
2024-01-24 13:19:29
![](https://img.aspxhome.com/file/2023/6/70446_0s.png)
mysql limit 分页的用法及注意要点
2024-01-21 06:44:50
PHP实现获取第一个中文首字母并进行排序的方法
2023-10-30 12:29:08