python使用@property @x.setter @x.deleter - CSDN博客
文章推薦指數: 80 %
@property可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/deleter也是需要的。
1》只有@property表示只读。
python使用@[email protected]@x.deleter
快递小可
于 2016-10-2511:07:19 发布
12884
收藏
13
分类专栏:
python
文章标签:
python
property
x.setter
x.deleter
新式类和经典类
版权声明:本文为博主原创文章,遵循CC4.0BY-SA版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sxingming/article/details/52916249
版权
python
专栏收录该内容
167篇文章
8订阅
订阅专栏
@property可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/deleter也是需要的。
1》只有@property表示
只读。
2》同时有@property和@x.setter表示
可读可写。
3》同时有@property和@x.setter和@x.deleter表示可读可写可删除。
classstudent(object):#新式类
def__init__(self,id):
self.__id=id
@property#读
defscore(self):
returnself._score
@score.setter#写
defscore(self,value):
ifnotisinstance(value,int):
raiseValueError('scoremustbeaninteger!')
ifvalue<0orvalue>100:
raiseValueError('scoremustbetween0and100')
self._score=value
@property#读(只能读,不能写)
defget_id(self):
returnself.__id
s=student('123456')
s.score=60#写
prints.score#读
#s.score=-2#ValueError:scoremustbetween0and100
#s.score=32.6#ValueError:scoremustbeaninteger!
s.score=100#写
prints.score#读
prints.get_id#读(只能读,不可写)
#s.get_id=456#只能读,不可写:AttributeError:can'tsetattribute
运行结果:60100123456
classA(object):#新式类(继承自object类)
def__init__(self):
self.__name=None
defgetName(self):
returnself.__name
defsetName(self,value):
self.__name=value
defdelName(self):
delself.__name
name=property(getName,setName,delName)
a=A()
printa.name#读
a.name='python'#写
printa.name#读
dela.name#删除
#printa.name#a.name已经被删除AttributeError:'A'objecthasnoattribute'_A__name'运行结果:
None
python
classA(object):#要求继承object
def__init__(self):
self.__name=None
#下面开始定义属性,3个函数的名字要一样!
@property#读
defname(self):
returnself.__name
@name.setter#写
defname(self,value):
self.__name=value
@name.deleter#删除
defname(self):
delself.__name
a=A()
printa.name#读
a.name='python'#写
printa.name#读
dela.name#删除
#printa.name#a.name已经被删除AttributeError:'A'objecthasnoattribute'_A__name'运行结果:
None
python
classperson(object):
def__init__(self,first_name,last_name):
self.first_name=first_name
self.last_name=last_name
@property#读
deffull_name(self):
return'%s%s'%(self.first_name,self.last_name)
p=person('wu','song')
printp.full_name#读
#p.full_name='songming'#只读,不可修改AttributeError:can'tsetattribute
p.first_name='zhang'
printp.full_name#读运行结果:
wusong
zhangsong
上面都是以新式类为例子,下面我们看一个包含经典类的例子:
#!/usr/bin/envpython
#coding:utf-8
classtest1:#经典类:没有继承object
def__init__(self):
self.__private='alex1'#私有属性以2个下划线开头
#读私有属性
@property
defprivate(self):
returnself.__private
#尝试去写私有属性(对于经典类而言,“写”是做不到的,注意看后边的代码和注释!)
@private.setter
defprivate(self,value):
self.__private=value
#尝试去删除私有属性(对于经典类而言,“删除”也是做不到的,具体看后边的代码和注释!)
@private.deleter
defprivate(self):
delself.__private
classtest2(object):#新式类:继承了object
def__init__(self):
self.__private='alex2'#私有属性以2个下划线开头
#读私有属性
@property
defprivate(self):
returnself.__private
#写私有属性
@private.setter
defprivate(self,value):
self.__private=value
#删除私有属性
@private.deleter
defprivate(self):
delself.__private
t1=test1()
#printt1.__private#外界不可直接访问私有属性
printt1.private#读私有属性
printt1.__dict__
t1.private='change1'#对于经典类来说,该语句实际上是为实例t1添加了一个实例变量private
printt1.__dict__
printt1.private#输出刚刚添加的实例变量private
t1.private='change2'
printt1.__dict__
delt1.private#删除刚刚添加的实例变量private
printt1.__dict__
printt1.private#读私有属性
#delt1.private#无法通过这种方式删除私有属性:AttributeError:test1instancehasnoattribute'private'
#对于经典类而言,我们无法通过上面的语句,对实例的私有变量__private进行修改或删除!
print'-------------------------------------------------------'
t2=test2()
printt2.__dict__
printt2.private#继承了object,添加@private.setter后,才可以写
t2.private='change2'#修改私有属性
printt2.__dict__
printt2.private
delt2.private#删除私有变量
#printt2.private#私有变量已经被删除,执行“读”操作会报错:AttributeError:'test2'objecthasnoattribute'_test2__private'
printt2.__dict__
#对于新式类而言,我们可以通过上面的语句,对实例的私有变量__private进行修改或删除运行结果:
alex1{'_test1__private':'alex1'}{'_test1__private':'alex1','private':'change1'}change1{'_test1__private':'alex1','private':'change2'}{'_test1__private':'alex1'}alex1-------------------------------------------------------{'_test2__private':'alex2'}alex2{'_test2__private':'change2'}change2{}
(完)
快递小可
关注
关注
2
点赞
踩
4
评论
13
收藏
打赏
扫一扫,分享内容
点击复制链接
专栏目录
Python如何使用@[email protected]及@x.deleter
08-19
主要介绍了Python如何使用@[email protected]及@x.deleter,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
python内置装饰器
mofei
08-02
1104
python内置装饰器有属性(property),类方法(classmethod),静态方法(staticmethod)
属性(property)
property可以将python定义的函数当做属性访问,从而提供更加友好访问方式,但是有时候setter/deleter也是需要的
只有@property表示只读。
同时有@property和@x.setter表示可读可写。
同时有@...
评论 4
您还未登录,请先
登录
后发表或查看评论
Python自学——构造器@[email protected]@x.deleter
Old.Wang的博客
07-28
151
@property装饰器详解
既要保护类的封装特性,又要让开发者可以使用“对象.属性”的方式操作操作类属性,除了使用property()函数,Python还提供了@property装饰器。
通过@property装饰器,可以直接通过方法名来访问方法,不需要在方法名后添加一对“()”小括号。
@property的语法格式如下
@property
defshow(self):
returnself.__show
@x.setter
而要想实现修改show属性的值...
python(@property,setter,deleter)
博客小白
02-02
452
classWomen(object):
def__init__(self,age):
#执行age函数,给_age属性赋值
self.age=age
defset_age(self,value):
ifnotisinstance(value,int):
raiseTypeError('age
pythondeleter_Python如何使用@[email protected]及@x.deleter
最新发布
weixin_39836063的博客
01-15
37
@property可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/deleter也是需要的。
只有@property表示只读。
同时有@property和@x.setter表示可读可写。
同时有@property和@x.setter和@x.deleter表示可读可写可删除。
代码如下classstudent(object):#新式类def__init_...
(1)用encode("utf8")把unicode编码变成str/(2)python中@property,@x.setter和@x.deleter/(3)MD5加密编码
sinat_26566137的博客
08-07
143
(1)用encode(“utf8”)把unicode编码变成str,
ifisinstance(s,unicode):
s=s.encode("utf8")
(2)python中@property,@x.setter和@x.deleter
@property可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/dele...
python中@property、@setter和@deleter
姜亚轲的博客
05-11
4911
上一次我给大家讲解了装饰器,它能让函数在不做更多变动的情况下增加某些额外的功能而今天我们来了解一下python中几个内置的装饰器通常我们写代码的时候,都不希望外部代码能够轻易地修改内部属性的参数因为要在外部改变参数的时候,我们必须想办法通过内部函数去检验参数的正确性,以确保设置正确但是我们不让外部轻易地修改数据,反过来我们自己也不能很方便地从外部获取数据那么,怎么样才能既在外部轻易地修改数据,又能...
Python中的属性Property-getter、setter、deleter
weixin_40500230的博客
06-05
936
转自:https://www.cnblogs.com/crwy/p/6852347.html
通常,我们这样定义属性:
classAnimal(object):
def__init__(self,name,age):
self.name=name
self.age=age
即在实例初始化时,__init__函数中赋值,其中值可为实例访...
@[email protected]@func.deleter用法
宇锅锅的博客
01-11
69
@[email protected]@func.deleter用法
在面向对象中,通过这三个装饰器,将一个方法伪装成一个功能,在调用过程中更加方便.
@property:添加之后,调用函数时就可以不加括号.
@func.setter:在使用@property的前提下,更改这个方法,在下面可以做出判断.
@func.deleter:删除时,执行下面的内容,一般将delfunc写在下面,进行真的删除
#求圆的周长
classCircle:
def__init__(se
Python:使用@property和@xxx.setter存取内部属性
caoxinjian423的博客
09-29
970
#!/usr/bin/envpython
#coding:UTF-8
"""
@version:python3.x
@author:曹新健
@contact:[email protected]
@software:PyCharm
@file:property和setter.py
@time:2018/9/2915:08
"""
classPerson():
def...
python︱函数、for、if、_name_、迭代器、防范报错、类定义、装饰器、argparse模块、yield
素质云笔记
01-09
1577
新手入门python,开始写一些简单函数,慢慢来,加油~
一、函数+三个内建函数filter,map和reduce+if
1、def/lambda
defmyadd(a=1,b=100):
result=0
i=a
whilei#默认值为1+2+3+……+100
result+=i
i+
python入门(@property,@*.setter)
trb331617的博客
05-31
8482
@property可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/deleter也是需要的。
OC中@property的各种属性的使用详解
iteye_18817的博客
08-10
1770
1、在obj2.0中可以声明属性让编译器自动合成setter和getter方法:
所用关键字:@property和@synthesize。
二者需要配对使用。
@property是在头文件的类中对setter和getter方法进行声明的,而@synthesize是在.m文件内对声明的方法进行实现的,格式如下:
@synthesize成员名1,成员名2,,,
而在对应的.h文件中@pro...
python方法属性化,限制属性@property,@x.setter
BGONE的博客
03-24
551
classStudent(object):
@property#函数属性化
defscore(self):#获取属性
returnself._score#返回处理后的属性值,一定要有_
@score.setter#设置属性
defscore(self,value):
ifnotisinstance(va...
python@property和@xx.setter
weixin_42364322的博客
02-14
388
https://www.cnblogs.com/mzc1997/p/7663052.html
python装饰器@property
weixin_34337265的博客
01-16
104
@property可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/deleter也是需要的。
1、只有@property表示只读。
2、同时有@property和@*.setter表示可读可写。
3、同时有@property和@*.setter和@*.deleter表示可读可写可删除。
代码:
[pyt...
pythonproperty/getter/setter/deleter/_/__
Neil的博客
12-14
75
描述:property/getter/setter/deleter,_,__
property:readonly
setter:readandwrite
getter:read
deleter:delete
属性特征:
public:无标记
protected:_(单下划线)
private:__(双下划线)
#-*-coding:ut...
©️2022CSDN
皮肤主题:编程工作室
设计师:CSDN官方博客
返回首页
快递小可
CSDN认证博客专家
CSDN认证企业博客
码龄7年
暂无认证
183
原创
3万+
周排名
56万+
总排名
200万+
访问
等级
1万+
积分
299
粉丝
847
获赞
75
评论
1526
收藏
私信
关注
热门文章
Pythonpickle模块学习(超级详细)
155421
linux返回上一级目录和返回根目录
86275
pythonsys.argv[]用法
81033
python如何输出百分数(如23%)
78988
python中的split()函数和os.path.split()函数
63742
分类专栏
python
167篇
leetcodepython
16篇
linux
26篇
regularexpression
9篇
MySQL
26篇
networkprotocol
21篇
ELK
最新评论
python列表复制(浅拷贝and深拷贝)
皮皮鲁与鲁西西�:
图很好!
python统计词频
Phoenix600:
turtle写柱形图,
Pythonpickle模块学习(超级详细)
啃铁好辛苦:
不懂就问,不是有引用吗
python列表复制(浅拷贝and深拷贝)
Tsring拉措:
🐂呀
python中模块的__all__属性
Zen890:
写的好全面,看懂了!赞
您愿意向朋友推荐“博客详情页”吗?
强烈不推荐
不推荐
一般般
推荐
强烈推荐
提交
最新文章
Python3如何优雅地使用正则表达式(详解五)
Python3如何优雅地使用正则表达式(详解四)
Python3如何优雅地使用正则表达式(详解三)
2017年21篇
2016年244篇
目录
目录
分类专栏
python
167篇
leetcodepython
16篇
linux
26篇
regularexpression
9篇
MySQL
26篇
networkprotocol
21篇
ELK
目录
打赏作者
快递小可
你的鼓励将是我创作的最大动力
¥2
¥4
¥6
¥10
¥20
输入1-500的整数
余额支付
(余额:--)
扫码支付
扫码支付:¥2
获取中
扫码支付
您的余额不足,请更换扫码支付或充值
打赏作者
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额
0
抵扣说明:
1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。
余额充值
延伸文章資訊
- 1python使用@property @x.setter @x.deleter - CSDN博客
@property可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/deleter也是需要的。 1》只有@property表示只读。
- 2How to Delete a Property from an Object in Python
By using the deleter, you can delete a property from an object. Notice that the deleter() method ...
- 3python学习笔记-使用@property、setter、deleter - SegmentFault
- 4Python property() - Programiz
a deleter method del_name() to delete the name of the person. Now, we set a new property attribut...
- 5The @property Decorator in Python: Its Use Cases ...
How you can use @property to define getters, setters, and deleters. 1️⃣ Advantages of Properties ...