python @property的用法及含义全面解析
在接触python时最开始接触的代码,取长方形的长和宽,定义一个长方形类,然后设置长方形的长宽属性,通过实例化的方式调用长和宽,像如下代码一样。
class Rectangle(object):
def __init__(self):
self.width =10
self.height=20
r=Rectangle()
print(r.width,r.height)
此时输出结果为10 20
但是这样在实际使用中会产生一个严重的问题,__init__ 中定义的属性是可变的,换句话说,是使用一个系统的所有开发人员在知道属性名的情况下,可以进行随意的更改(尽管可能是在无意识的情况下),但这很容易造成严重的后果。
class Rectangle(object):
def __init__(self):
self.width =10
self.height=20
r=Rectangle()
print(r.width,r.height)
r.width=1.0
print(r.width,r.height)
以上代码结果会输出宽1.0,可能是开发人员不小心点了一个小数点上去,但是会系统的数据错误,并且在一些情况下很难排查。
这是生产中很不情愿遇到的情况,这时候就考虑能不能将width属性设置为私有的,其他人不能随意更改的属性,如果想要更改只能依照我的方法来修改,@property就起到这种作用(类似于java中的private)
class Rectangle(object):
@property
def width(self):
#变量名不与方法名重复,改为true_width,下同
return self.true_width @property
def height(self):
return self.true_height
s = Rectangle()
#与方法名一致
s.width = 1024
s.height = 768
print(s.width,s.height)
(@property使方法像属性一样调用,就像是一种特殊的属性)
此时,如果在外部想要给width重新直接赋值就会报AttributeError: can't set attribute的错误,这样就保证的属性的安全性。
同样为了解决对属性的操作,提供了封装方法的方式进行属性的修改
class Rectangle(object):
@property
def width(self):
# 变量名不与方法名重复,改为true_width,下同
return self.true_width
@width.setter
def width(self, input_width):
self.true_width = input_width
@property
def height(self):
return self.true_height
@height.setter
#与property定义的方法名要一致
def height(self, input_height):
self.true_height = input_height
s = Rectangle()
# 与方法名一致
s.width = 1024
s.height = 768
print(s.width,s.height)
此时就可以对“属性”进行赋值操作,同样的方法还del,用处是删除属性,写法如下,具体实现不在赘述
@height.deleter
def height(self):
del self.true_height
总结一下@property提供了可读可写可删除的操作,如果像只读效果,就只需要定义@property就可以,不定义代表禁止其他操作。
本文转自:https://www.jb51.net/article/134148.htm
在接触python时最开始接触的代码,取长方形的长和宽,定义一个长方形类,然后设置长方形的长宽属性,通过实例化的方式调用长和宽,像如下代码一样。
|
1
2
3
4
5
6
|
class Rectangle(object): def __init__(self): self.width =10 self.height=20r=Rectangle()print(r.width,r.height) |
此时输出结果为10 20
但是这样在实际使用中会产生一个严重的问题,__init__ 中定义的属性是可变的,换句话说,是使用一个系统的所有开发人员在知道属性名的情况下,可以进行随意的更改(尽管可能是在无意识的情况下),但这很容易造成严重的后果。
|
1
2
3
4
5
6
7
8
|
class Rectangle(object): def __init__(self): self.width =10 self.height=20r=Rectangle()print(r.width,r.height)r.width=1.0print(r.width,r.height) |
以上代码结果会输出宽1.0,可能是开发人员不小心点了一个小数点上去,但是会系统的数据错误,并且在一些情况下很难排查。
这是生产中很不情愿遇到的情况,这时候就考虑能不能将width属性设置为私有的,其他人不能随意更改的属性,如果想要更改只能依照我的方法来修改,@property就起到这种作用(类似于java中的private)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Rectangle(object): @property def width(self): #变量名不与方法名重复,改为true_width,下同 return self.true_width @property def height(self): return self.true_heights = Rectangle()#与方法名一致s.width = 1024s.height = 768print(s.width,s.height) |
(@property使方法像属性一样调用,就像是一种特殊的属性)
此时,如果在外部想要给width重新直接赋值就会报AttributeError: can't set attribute的错误,这样就保证的属性的安全性。
同样为了解决对属性的操作,提供了封装方法的方式进行属性的修改
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
class Rectangle(object): @property def width(self): # 变量名不与方法名重复,改为true_width,下同 return self.true_width @width.setter def width(self, input_width): self.true_width = input_width @property def height(self): return self.true_height @height.setter #与property定义的方法名要一致 def height(self, input_height): self.true_height = input_heights = Rectangle()# 与方法名一致s.width = 1024s.height = 768print(s.width,s.height) |
此时就可以对“属性”进行赋值操作,同样的方法还del,用处是删除属性,写法如下,具体实现不在赘述。
|
1
2
3
|
@height.deleterdef height(self): del self.true_height |
总结一下@property提供了可读可写可删除的操作,如果像只读效果,就只需要定义@property就可以,不定义代表禁止其他操作。
python @property的用法及含义全面解析的更多相关文章
- python property的用法
用法一: class Test(object): def __init__(self): # 私有化 self.__num = 100 #名字重整_Test__num def setNum(self, ...
- python的super用法及含义
注释:以下都是在python2.7版本验证的 总括:1.python解决二义性问题,经历了深度优先算法.广度优先算法.拓扑排序算法,目前python的版本都是使用拓扑算法(C3) 2.严谨sup ...
- Python中“*”和“**”的用法 || yield的用法 || ‘$in’和'$nin' || python @property的含义
一.单星号 * 采用 * 可将列表或元祖中的元素直接取出,作为随机数的上下限: import random a = [1,4] print(random.randrange(*a)) 或者for循环输 ...
- python基础学习 Day19 面向对象的三大特性之多态、封装 property的用法(1)
一.课前内容回顾 继承作用:提高代码的重用性(要继承父类的子类都实现相同的方法:抽象类.接口) 继承解释:当你开始编写两个类的时候,出现了重复的代码,通过继承来简化代码,把重复的代码放在父类中. 单继 ...
- python property用法
参考 http://openhome.cc/Gossip/Python/Property.html http://pyiner.com/2014/03/09/Python-property.html ...
- python property
python property 在2.6版本中,添加了一种新的类成员函数的访问方式--property. 原型 class property([fget[, fset[, fdel[, doc]]]] ...
- day01-day04总结- Python 数据类型及其用法
Python 数据类型及其用法: 本文总结一下Python中用到的各种数据类型,以及如何使用可以使得我们的代码变得简洁. 基本结构 我们首先要看的是几乎任何语言都具有的数据类型,包括字符串.整型.浮点 ...
- 聊聊属性方法property的用法
写之前随便百度了一下博客,又看到廖雪峰的博客了.果然置顶的能力很强. 我想说其实property的用法并不是主要用来做类型检查.反而更多应该是用于简化操作的目的. 写之前想聊一个古老的话题.年初的时候 ...
- python 中@ 的用法【转】
这只是我的个人理解: 在Python的函数中偶尔会看到函数定义的上一行有@functionName的修饰,当解释器读到@的这样的修饰符之后,会先解析@后的内容,直接就把@下一行的函数或者类作为@后边的 ...
随机推荐
- thinkphp5: 循环输出表格,并固定表格单元宽度(过长省略号)
html: <table class="table table-striped" style='table-layout:fixed;'> <thead clas ...
- org.apache.catalina.core.StandardContext.startInternal Context [/test] startup failed due to previou
解决方法: WEB-INF/classes目录下新建一个文件叫logging.properties,截图如下: 代码如下: handlers=org.apache.juli.FileHandler,j ...
- mysql全局权限账户%登录不上ERROR 1045 (28000): Access denied for user 'mhz'@'localhost' (using password: YES)
mysql全局权限账户%登录不上ERROR 1045 (28000): Access denied for user 'mhz'@'localhost' (using password: YES) 解 ...
- \t \r \n \f
\t 的意思是 :水平制表符.将当前位置移到下一个tab位置. \r 的意思是: 回车.将当前位置移到本行的开头. \n 的意思是:回车换行.将当前位置移到下一行的开头. \f的意思是:换页.将当前位 ...
- Centos环境下安装mongoDB
安装前注意: 此教程是通过yum安装的.仅限64位centos系统 安装步骤: 1.创建仓库文件: vi /etc/yum.repos.d/mongodb-org-3.4.repo 然后复制下面配置, ...
- 浅入深出Vue:工具准备之WebStorm安装配置
浅入深出Vue之工具准备(一):WebStorm安装配置 工欲善其事必先利其器,让我们先做好准备工作吧 导航篇 WebStorm安装配置 所有工具的下载地址都可以在导航篇中找到,这里我们下载的是最新版 ...
- word20170102日用家电 household appliances
1. Vacuum cleaner: 吸尘器 2.Cordless vacuum cleaner: 无线吸尘器 3.Robotic vacuum cleaner: 机器人吸尘器 动词:to vacuu ...
- 423 重温Java Script and jQuery 葵花宝典 Bootstrap
Bootstrap需要引的三个文件 <link rel="stylesheet" href="css/bootstrap.css"> 表格元素 ...
- Springboot项目打包后的页面丢失问题(thymeleaf报错)
遇到的问题目前找到两种: 返回视图路径以/开头,例如 /test/hello 在thymeleaf页面中,引入的页面以/开头,例如:<footer th:replace="/index ...
- 清北学堂学习总结day2
今天是钟皓曦大佬讲课,先来膜一波 %%%%% •数论 数论是这次培训的一个重点,那么什么是数论呢? 数论是研究整数性质的东西,所以理论上day2不会涉及小数QwQ (切入正题) •整除性: 设a, ...