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=20 r=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=20 r=Rectangle() print(r.width,r.height) r.width=1.0 print(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_height s = Rectangle() #与方法名一致 s.width = 1024 s.height = 768 print(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_height s = Rectangle() # 与方法名一致 s.width = 1024 s.height = 768 print(s.width,s.height) |
此时就可以对“属性”进行赋值操作,同样的方法还del,用处是删除属性,写法如下,具体实现不在赘述。
1
2
3
|
@height.deleter def 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的修饰,当解释器读到@的这样的修饰符之后,会先解析@后的内容,直接就把@下一行的函数或者类作为@后边的 ...
随机推荐
- 背包九讲PDF
本资料仅限个人学习交流使用,不得用于商业用途. 背包九讲PDF:https://pan.baidu.com/s/17rTxMwCo9iSTOW77yucdXQ 提取码:xbqa
- 第十二节:WebApi自动生成在线Api文档的两种方式
一. WebApi自带生成api文档 1. 说明 通过观察,发现WebApi项目中Area文件夹下有一个HelpPage文件夹,如下图,该文件夹就是WebApi自带的生成Api的方式,如果该文件夹没了 ...
- 爬一下国家统计局行政区划代码C#
目前NBS上有2015-2018四个年度的代码信息,写一个控制台程序爬一下县级行政区下的代码. 使用HttpWebRequest+HttpWebResponse获取html,使用HtmlAgility ...
- Docker实践之02-使用镜像及定制
目录 一.获取镜像 二.使用镜像启动容器实例 三.列出镜像 四.删除本地镜像 五.定制镜像 通过commit命令定制镜像 通过Dockerfile定制镜像 docker build的工作原理 dock ...
- D3 learning notes
D3 https://d3js.org/ 数据驱动文档显示, 利用 SVG HTML CSS技术. D3.js is a JavaScript library for manipulating doc ...
- luogu P3810 三维偏序(陌上花开)cdq分治
题目链接 思路 对一维排序后,使用$cdq$分治,以类似归并排序的方法处理的二维,对于满足$a[i].b \leq a[j].b$的点对,用树状数组维护$a[i].c$的数量.当遇到$a[i].b&g ...
- orcle数据库表中字段值含有单引号,如何模糊搜索?
例如:T_table表中,name字段值为:字符串含有‘单引号’: SQL模糊搜索语句应该如下:select * from T_table where name like '%含有''单引号''%'
- 机器学习-kmeans的使用
import numpy as np import pandas as pd import matplotlib from matplotlib import pyplot as plt %matpl ...
- Python自动化中的元素定位(一)
1.使用selenium中的webdriver模块对浏览器进行操作 1)from selenium import webdriver 加载模块 2)b = webdriver.Friefox() 打开 ...
- 读spring源码(一)-ClassPathXmlApplicationContext-初始化
工作来几乎所有的项目都用到了spring,却一直没有系统的读下源码,从头开始系统的读下吧,分章也不那么明确,读到哪里记到哪里,仅仅作为个笔记吧. 先看ClassPathXmlApplicationCo ...