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的修饰,当解释器读到@的这样的修饰符之后,会先解析@后的内容,直接就把@下一行的函数或者类作为@后边的 ...
随机推荐
- 0. Java虚拟机系列备忘预览图
打算把Java虚拟机这块单独弄一个主题出来,做做备忘,结构如图所示: 后面还有一部分待更新...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- 光刻技术的原理和EUV光刻技术前景
本文转载自微信公众号 半导体技术天地, 链接 https://mp.weixin.qq.com/s/EEBkSQ_Yc8RYFO18VpO8ow
- ioctl函数
一.函数原型 #include <unistd.h> int ioctl(int fd, int request, .../* void *arg */); 二.和网络相关的请求(requ ...
- 3DMAX中坐标解析
World:世界坐标系,又称世界空间.位于各视口左下角的图标,显示了世界坐标系的方向,其坐标原点位于视口中心.该坐标系永远不会变化. Screen:屏幕坐标系,此时将使用活动视口屏幕作为坐标系.在活动 ...
- Android AVD启动报错: This AVD's configuration is missing a kernel file! Please ensure the file "kernel-qemu" is in the same location as your system image.
启动Android SDK manager重新下载安装
- 20164305 徐广皓 Exp5 MSF基础应用
一.知识点总结 二.攻击实例 主动攻击的实践 ms08_067(win7) payload/generic/shell_reverse_tcp(失败) payload/windows/meterpre ...
- SpringMVC+Apache Shiro+JPA(hibernate)案例教学(三)给Shiro登录验证加上验证码
序: 给Shiro加入验证码,有多种方式,当然你也可以通过继承修改FormAuthenticationFilter类,通过Shiro去验证验证码.具体实现请百度: 应用Shiro到Web Applic ...
- java学习笔记11-static关键字
如果在类中使用static关键字创建方法,这种方法称为类方法,可以在这个类中直接引用.而不是用static创建的方法.这种方法称为对象方法(实例方法),需要创建对象后才能使用. package les ...
- dash视频服务器本地搭建 (初探)
2019-4-17 15:54:17 星期三 技术说明: dash: 将一个大视频分解成不同分辨率, 不同清晰度的小视频, 以及一个描述文件(后缀: mpd), 根据网络带宽自动调整视频流, 看起来更 ...