Python 官方文档:入门教程 => 点击学习
目录前言限制值使用 @property 的方式代替。动态属性的好处动态显示附:用property代替getter和setter方法总结前言 python 动态属性的概念可能会被面试问
python 动态属性的概念可能会被面试问到,在项目当中也非常实用,但是在一般的编程教程中不会提到,可以进修一下。
先看一个简单的例子。创建一个 Student 类,我希望通过实例来获取每个学生的一些情况,包括名字,成绩等。成绩只有等到考试结束以后才会有,所以实例化的时候不会给它赋值。
class Student:
def __init__(self, name):
self.name = name
self.score = None
mike = Student('mike')
考试完以后,准备给 mike 打分:
mike.score = 999
在这里,老师一不小心多打了个 9 ,通常来说打分都是 100 分值,999 是一个非法数据,不应该赋值成功。学生一多,老师打分出现手误的情况肯定会越来越多,所以我们必须想办法修改程序,限制 score 的值必须在 0-100 分。
我们定义一个方法,如果输入的不是 0-100 的整数,就让程序报错,数据合法,我们就把 score 属性修改成功。
def set_score(self, new_score):
if not isinstance(new_score, int):
raise ValueError('score must be int')
if 0 <= new_score <= 100:
self.score = new_score
return self.score
else:
raise ValueError('score invalid')
这样我们每次需要获取成绩的时候使用 self.score 获取,修改成绩的时候调用函数来修改:
mike.set_score(999)
调用以后会报错,因为 999 是非法数据。注意,这个时候我使用 self.score 还是可以进行设置,而且不报错:
self.score = 999
这显然是不行的。所以我们要提供一种机制,把 score 变成私有属性,不能让外部访问。很遗憾,Python 的私有属性是伪私有。通常我们把 _
开头的属性叫私有属性,但是这只是一种协议和规定,你看到下划线开头的属性,不要去访问了。你硬要访问,是可以的,python 并不会禁止。
上面的方法虽然实现了功能,但是改变了属性的使用方式。平常是这样使用的:
# 获取属性
a = mike.score
# 设置属性
mike.score = 99
@property
def score(self):
return self._score
@score.setter
def score(self, new_score):
if not isinstance(new_score, int):
raise ValueError('score must be int')
if 0 <= new_score <= 100:
self._score = new_score
return self._score
else:
raise ValueError('score invalid')
_score
我们就不直接去使用了。你要用也可以,不建议。现在我们来完善这个类,添加 birth 属性和年龄属性:
from datetime import datetime
class Student:
def __init__(self, name, birth=1920):
self.name = name
self._score = None
self.birth = birth
self.age = datetime.now().year - self.birth
mike = Student('mike')
print(mike.birth)
print(mike.age)
birth 和 age 这两个是可以根据一个求出另外一个的。存在数据冗余问题。
age 属性这样是有问题的。mike 初始化的时候,age 已经被求出来了,如果我在下一年再去访问 age 属性,那他就是个错误的值。可以通过把 age 设成现在的秒数来验证:
self.age = datetime.now().second
mike = Student('mike')
time.sleep(5)
print(mike.age)
print(datetime.now().second)
@property
def age(self):
return datetime.now().year - self.birth
注意,这里不要去设置 @age.setter ,因为他是动态变化的,你修改了会造成数据不一致,它只能作为一个只读属性。
@property 作用和应用场景:
>>>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
到此这篇关于Python中property属性用处的文章就介绍到这了,更多相关Python中property属性内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Python中property属性的用处详解
本文链接: https://www.lsjlt.com/news/117268.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0