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" ...
随机推荐
- HTML特殊字符编码对照表
HTML特殊字符编码对照表 特殊符号 命名实体 十进制编码 特殊符号 命名实体 十进制编码 特殊符号 命名实体 十进制编码 Α Α Α Β Β Β Γ Γ Γ Δ Δ Δ Ε Ε Ε Ζ Ζ Ζ Η ...
- MapReduce工作原理讲解
第一部分:MapReduce工作原理 MapReduce 角色•Client :作业提交发起者.•JobTracker: 初始化作业,分配作业,与TaskTracker通信,协调整个作业.•TaskT ...
- Maximo-删除应用程序
执行如下SQL: delete from maxapps where app='<APPLICATION NAME>';delete from maxpresentation where ...
- C语言typedef的用法(转)
http://www.cnblogs.com/afarmer/archive/2011/05/05/2038201.html 一.基本概念剖析 int* (*a[5])(int, char*); ...
- iOS内存管理
iOS内存管理的方式是引用计数机制.分为MRC(人式引用计数)和ARC(自动引用计数). 为什么要学习内存管理? 内存管理方式是引用计数机制,通过控制对象的引用计数来实现操作对象的功能.一个对象的生命 ...
- 浅析NRF51822合并文件之app_valid_setting_apply
[原创出品§转载请注明出处] 出处:http://www.cnblogs.com/libra13179/p/5787084.html 我们打开app_valid_setting_apply.hex如下 ...
- information_schema系列十二
1: INNODB_SYS_VIRTUAL 表存储的是INNODB表的虚拟列的信息,当然这个还是比较简单的,我们直接通过SHOW CREATE TABLE 或者DESC TABLE就能看得到. Col ...
- javascript算法
代码运行环境: nodejs + mochajs /* *选择排序 *每次查找数组最小数据 *将最小数据排到左侧 */ var assert = require('assert'); describe ...
- 【英语魔法俱乐部——读书笔记】 1 初级句型-简单句(Simple Sentences)
第一部分 1 初级句型-简单句(Simple Sentences):(1.1)基本句型&补语.(1.2)名词短语&冠词.(1.3)动词时态.(1.4)不定式短语.(1.5)动名词.(1 ...
- 关于Onvif的event
昨天又仔细研究了一下camera的alarm功能,发现原来很简单,首先订阅一下,即create,拿到订阅号后直接pull,一旦收到信息就再次用订阅号pull.参考http://www.doc88.co ...