Python中property属性实例解析
作者:ITxiaoke 时间:2022-02-04 20:38:12
本文主要讲述的是对Python中property属性(特性)的理解,具体如下。
定义及作用:
在property类中,有三个成员方法和三个装饰器函数。
三个成员方法分别是:fget、fset、fdel,它们分别用来管理属性访问;
三个装饰器函数分别是:getter、setter、deleter,它们分别用来把三个同名的类方法装饰成property。
fget方法用来管理类实例属性的获取,fset方法用来管理类实例属性的赋值,fdel方法用来管理类实例属性的删除;
getter装饰器把一个自定义类方法装饰成fget操作,setter装饰器把一个自定义类方法装饰成fset操作,deleter装饰器把一个自定义类方法装饰成fdel操作。
只要在获取自定义类实例的属性时就会自动调用fget成员方法,给自定义类实例的属性赋值时就会自动调用fset成员方法,在删除自定义类实例的属性时就会自动调用fdel成员方法。
下面从三个方面加以说明
Num01–>原始的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 = Money()
print(money.getmoney())
# 结果是:0
print("====修改钱的大小值====")
money.setmoney(100)
print(money.getmoney())
# 结果是:100
Num02–>使用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 = property(getmoney, setmoney)
money = Money()
print(money.getmoney())
# 结果是:0
print("====修改钱的大小值====")
money.setmoney(100)
print(money.getmoney())
# 结果是:100
#最后特别需要注意一点:实际钱的值是存在私有便令__money中。而属性money是一个property对象,
是用来为这个私有变量__money提供接口的。
#如果二者的名字相同,那么就会出现递归调用,最终报错。
Num03–>使用property取代getter和setter
@property成为属性函数,可以对属性赋值时做必要的检查,并保证代码的清晰短小
# 定义一个钱的类
class Money(object):
def __init__(self):
self._money = 0
@property
# 注意使用@property装饰器对money函数进行装饰,就会自动生成一个money属性,
当调用获取money的值时,就调用该函数
def money(self):
return self._money
@money.setter
# 使用生成的money属性,调用@money.setter装饰器,设置money的值
def money(self, value):
if isinstance(value, int):
self._money = value
else:
print("error:不是整型数字")
aa = Money()
print(aa.money)
# 结果是:0
print("====修改钱的大小值====")
aa.money = 100
print(aa.money)
# 结果是:100
来源:http://blog.csdn.net/u014745194/article/details/70432673
标签:python,property
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何制作K线图?
2010-06-29 17:25:00
js取得当前鼠标的X,Y坐标
2007-09-27 19:52:00
简单谈谈Python中的闭包
2021-10-20 12:50:46
分享2个方便调试Python代码的实用工具
2021-08-26 18:50:08
![](https://img.aspxhome.com/file/2023/6/65856_0s.png)
Python基于socket实现TCP客户端和服务端
2023-09-29 15:30:05
XML入门的常见问题(二)
2008-09-05 17:20:00
详尽解析javascript的event对象
2008-01-16 11:27:00
Python学习之路安装pycharm的教程详解
2021-11-15 23:07:51
![](https://img.aspxhome.com/file/2023/7/75267_0s.jpg)
Android基于TCP和URL协议的网络编程示例【附demo源码下载】
2021-08-26 03:18:55
![](https://img.aspxhome.com/file/2023/0/84090_0s.gif)
边框样式的写法总结
2009-01-18 13:00:00
查看ASP详细错误提示信息的图文设置方法
2011-02-05 11:02:00
![](https://img.aspxhome.com/file/UploadPic/20112/5/20112511339472s.jpg)
Python必备技能之debug调试教程详解
2023-03-01 19:26:10
![](https://img.aspxhome.com/file/2023/0/68380_0s.png)
在MySQL中获得更好的全文搜索结果
2008-05-09 10:38:00
python中单下划线(_)和双下划线(__)的特殊用法
2022-08-09 22:23:22
Python中标准库OS的常用方法总结大全
2021-04-29 06:43:41
Python编程tkinter库Canvas实现涂鸦颜色表及围棋盘示例
2023-08-02 13:21:02
![](https://img.aspxhome.com/file/2023/3/92073_0s.png)
对python 多个分隔符split 的实例详解
2022-12-26 11:24:27
bootstrap自定义样式之bootstrap实现侧边导航栏功能
2023-09-06 20:41:36
![](https://img.aspxhome.com/file/2023/4/56084_0s.png)
相同记录行如何取最大值
2008-07-26 12:32:00
python使用redis模块来跟redis实现交互
2023-05-06 04:13:40
![](https://img.aspxhome.com/file/2023/6/78476_0s.png)