property
一、property用法
property(fget=None, fset=None, fdel=None, doc=None) -> property attribute fget is a function to be used for getting an attribute value, and likewise
fset is a function for setting, and fdel a function for del'ing, an
attribute. Typical use is to define a managed attribute x: class C(object):
def getx(self): return self._x
def setx(self, value): self._x = value
def delx(self): del self._x
x = property(getx, setx, delx, "I'm the 'x' property.") Decorators make defining new properties or modifying existing ones easy: class C(object):
@property
def x(self):
"I am the 'x' property."
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
二、__get__, __set__(对于属性的方法)
class RevealAccess(object):
"""A data descriptor that sets and returns values
normally and prints a message logging their access.
""" def __init__(self, initval=None, name='var'):
self.val = initval
self.name = name def __get__(self, obj, objtype):
print('Retrieving', self.name)
return self.val def __set__(self, obj, val):
print('Updating', self.name)
self.val = val >>> class MyClass(object):
x = RevealAccess(10, 'var "x"')
y = 5 >>> m = MyClass()
>>> m.x
Retrieving var "x"
10
>>> m.x = 20
Updating var "x"
>>> m.x
Retrieving var "x"
20
>>> m.y
5
三、property模拟实现
class Property(object):
"Emulate PyProperty_Type() in Objects/descrobject.c" def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
if doc is None and fget is not None:
doc = fget.__doc__
self.__doc__ = doc def __get__(self, obj, objtype=None):
if obj is None:
return self
if self.fget is None:
raise AttributeError("unreadable attribute")
return self.fget(obj) def __set__(self, obj, value):
if self.fset is None:
raise AttributeError("can't set attribute")
self.fset(obj, value) def __delete__(self, obj):
if self.fdel is None:
raise AttributeError("can't delete attribute")
self.fdel(obj) def getter(self, fget):
return type(self)(fget, self.fset, self.fdel, self.__doc__) def setter(self, fset):
return type(self)(self.fget, fset, self.fdel, self.__doc__) def deleter(self, fdel):
return type(self)(self.fget, self.fset, fdel, self.__doc__)
property的更多相关文章
- 探究@property申明对象属性时copy与strong的区别
一.问题来源 一直没有搞清楚NSString.NSArray.NSDictionary--属性描述关键字copy和strong的区别,看别人的项目中属性定义有的用copy,有的用strong.自己在开 ...
- JavaScript特性(attribute)、属性(property)和样式(style)
最近在研读一本巨著<JavaScript忍者秘籍>,里面有一篇文章提到了这3个概念. 书中的源码可以在此下载.我将源码放到了线上,如果不想下载,可以直接访问在线网址,修改页面名就能访问到相 ...
- -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HO 解决办法
最近在使用maven,项目测试的时候出现了这么一个错.-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2 ...
- python property理解
一般情况下我这样使用property: @property def foo(self): return self._foo # 下面的两个decrator由@property创建 @foo.sette ...
- Android动画效果之Property Animation进阶(属性动画)
前言: 前面初步认识了Android的Property Animation(属性动画)Android动画效果之初识Property Animation(属性动画)(三),并且利用属性动画简单了补间动画 ...
- Android动画效果之初识Property Animation(属性动画)
前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...
- # ios开发 @property 和 Ivar 的区别
ios开发 @property 和 Ivar 的区别 @property 属性其实是对成员变量的一种封装.我们先大概这样理解: @property = Ivar + setter + getter I ...
- Cesium原理篇:Property
之前主要是Entity的一个大概流程,本文主要介绍Cesium的属性,比如defineProperties,Property(ConstantProperty,CallbackProperty,Con ...
- 为Guid数据类型的属性(property)赋值
先来看看数据库表中的字段设计: 在数据库的数据类型为uniqueidentifier. 而在程序中对应的数据类型为GUID. property有get和set,也就是说能获取值也可以赋值.
- @property中的copy.strong.weak总结
1.NSString类型的属性为什么用copy NSString类型的属性可以用strong修饰,但会造成一些问题,请看下面代码 #import "ViewController.h" ...
随机推荐
- AndroidStudio中activity实现去掉标题栏
1.在代码中实现 this.requestWindowFeature(Window.FEATURE_NO_TITLE) 这段代码需要放在setContentView()前面 2.设置在Manifest ...
- 【Python】pymongo使用
官方文档:http://api.mongodb.com/python/current/index.html MongoReplicaSetClient:http://api.mongodb.com/p ...
- maven基本用法
一.下载及安装 1.1 下载maven 3.1.1 先到官网http://maven.apache.org/download.cgi 下载最新版本(目前是3.1.1 ),下载完成后,解压到某个目录(本 ...
- 19.dnw打不开
用dnw.exe烧写文件时,突然出现电脑死机,重启后,dnw就打不开了,dnw.ini的原因,该文件在c盘目录下,只需要删除它,就可以继续使用dnw了.
- NSSortDescriptor对象进行数组排序
//创建一个数组 NSArray *array = @[@"zhangsan", @"lisi", @"zhonger", @"z ...
- SpringMVC无法获取请求中的参数的问题的调查与解决(1)
*更新:本文第一版中犯了比较大的错误,无论@RequestBody还是@RequestParam注解一样,都会使用全局的Encoding进行解码,会导致特殊编码的参数值丢失. 只要抛弃掉注解,就完全可 ...
- Backbone框架浅析
Backbone是前端mvc开发模式的框架.它能够让view和model相分离,让代码结构更清晰简答,开发进度加快,维护代码方便.但是,现在出了一种mvvm框架,它是下一代前端mvc开发模式的框架,代 ...
- Highcharts 在低版本 IE 上使用注意事项
来源:https://segmentfault.com/a/1190000004272693 很多人经常遇到图表在主流浏览器上运行正常,在低版本IE(包括IE6.IE7.IE8等)下运行出错(图表显示 ...
- A股各概念板块龙头股大全
A股各概念板块龙头股大全一.新兴信息产业龙头 1.车联网龙头——软控股份002073 启明信息002232.荣之联002642 2.通信网络龙头——恒宝股份002104 3.通信设备制造龙头——新海宜 ...
- bool 类型存在数据库中为 0 和 1
bool 类型存在数据库中为 0 和 1 但是在程序中应该使用 true 和 false 查询. 例如: bIsStart = 0 在数据中bIsStart为 0 sql 查询的时候,使用:sele ...