一、property用法

  1. property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
  2.  
  3. fget is a function to be used for getting an attribute value, and likewise
  4. fset is a function for setting, and fdel a function for del'ing, an
  5. attribute. Typical use is to define a managed attribute x:
  6.  
  7. class C(object):
  8. def getx(self): return self._x
  9. def setx(self, value): self._x = value
  10. def delx(self): del self._x
  11. x = property(getx, setx, delx, "I'm the 'x' property.")
  12.  
  13. Decorators make defining new properties or modifying existing ones easy:
  14.  
  15. class C(object):
  16. @property
  17. def x(self):
  18. "I am the 'x' property."
  19. return self._x
  20. @x.setter
  21. def x(self, value):
  22. self._x = value
  23. @x.deleter
  24. def x(self):
  25. del self._x

二、__get__, __set__(对于属性的方法)

  1. class RevealAccess(object):
  2. """A data descriptor that sets and returns values
  3. normally and prints a message logging their access.
  4. """
  5.  
  6. def __init__(self, initval=None, name='var'):
  7. self.val = initval
  8. self.name = name
  9.  
  10. def __get__(self, obj, objtype):
  11. print('Retrieving', self.name)
  12. return self.val
  13.  
  14. def __set__(self, obj, val):
  15. print('Updating', self.name)
  16. self.val = val
  17.  
  18. >>> class MyClass(object):
  19. x = RevealAccess(10, 'var "x"')
  20. y = 5
  21.  
  22. >>> m = MyClass()
  23. >>> m.x
  24. Retrieving var "x"
  25. 10
  26. >>> m.x = 20
  27. Updating var "x"
  28. >>> m.x
  29. Retrieving var "x"
  30. 20
  31. >>> m.y
  32. 5

三、property模拟实现

  1. class Property(object):
  2. "Emulate PyProperty_Type() in Objects/descrobject.c"
  3.  
  4. def __init__(self, fget=None, fset=None, fdel=None, doc=None):
  5. self.fget = fget
  6. self.fset = fset
  7. self.fdel = fdel
  8. if doc is None and fget is not None:
  9. doc = fget.__doc__
  10. self.__doc__ = doc
  11.  
  12. def __get__(self, obj, objtype=None):
  13. if obj is None:
  14. return self
  15. if self.fget is None:
  16. raise AttributeError("unreadable attribute")
  17. return self.fget(obj)
  18.  
  19. def __set__(self, obj, value):
  20. if self.fset is None:
  21. raise AttributeError("can't set attribute")
  22. self.fset(obj, value)
  23.  
  24. def __delete__(self, obj):
  25. if self.fdel is None:
  26. raise AttributeError("can't delete attribute")
  27. self.fdel(obj)
  28.  
  29. def getter(self, fget):
  30. return type(self)(fget, self.fset, self.fdel, self.__doc__)
  31.  
  32. def setter(self, fset):
  33. return type(self)(self.fget, fset, self.fdel, self.__doc__)
  34.  
  35. def deleter(self, fdel):
  36. return type(self)(self.fget, self.fset, fdel, self.__doc__)

property的更多相关文章

  1. 探究@property申明对象属性时copy与strong的区别

    一.问题来源 一直没有搞清楚NSString.NSArray.NSDictionary--属性描述关键字copy和strong的区别,看别人的项目中属性定义有的用copy,有的用strong.自己在开 ...

  2. JavaScript特性(attribute)、属性(property)和样式(style)

    最近在研读一本巨著<JavaScript忍者秘籍>,里面有一篇文章提到了这3个概念. 书中的源码可以在此下载.我将源码放到了线上,如果不想下载,可以直接访问在线网址,修改页面名就能访问到相 ...

  3. -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HO 解决办法

    最近在使用maven,项目测试的时候出现了这么一个错.-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2 ...

  4. python property理解

    一般情况下我这样使用property: @property def foo(self): return self._foo # 下面的两个decrator由@property创建 @foo.sette ...

  5. Android动画效果之Property Animation进阶(属性动画)

    前言: 前面初步认识了Android的Property Animation(属性动画)Android动画效果之初识Property Animation(属性动画)(三),并且利用属性动画简单了补间动画 ...

  6. Android动画效果之初识Property Animation(属性动画)

    前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...

  7. # ios开发 @property 和 Ivar 的区别

    ios开发 @property 和 Ivar 的区别 @property 属性其实是对成员变量的一种封装.我们先大概这样理解: @property = Ivar + setter + getter I ...

  8. Cesium原理篇:Property

    之前主要是Entity的一个大概流程,本文主要介绍Cesium的属性,比如defineProperties,Property(ConstantProperty,CallbackProperty,Con ...

  9. 为Guid数据类型的属性(property)赋值

    先来看看数据库表中的字段设计: 在数据库的数据类型为uniqueidentifier. 而在程序中对应的数据类型为GUID. property有get和set,也就是说能获取值也可以赋值.

  10. @property中的copy.strong.weak总结

    1.NSString类型的属性为什么用copy NSString类型的属性可以用strong修饰,但会造成一些问题,请看下面代码 #import "ViewController.h" ...

随机推荐

  1. HTML特殊字符编码对照表

    HTML特殊字符编码对照表 特殊符号 命名实体 十进制编码 特殊符号 命名实体 十进制编码 特殊符号 命名实体 十进制编码 Α Α Α Β Β Β Γ Γ Γ Δ Δ Δ Ε Ε Ε Ζ Ζ Ζ Η ...

  2. MapReduce工作原理讲解

    第一部分:MapReduce工作原理 MapReduce 角色•Client :作业提交发起者.•JobTracker: 初始化作业,分配作业,与TaskTracker通信,协调整个作业.•TaskT ...

  3. Maximo-删除应用程序

    执行如下SQL: delete from maxapps where app='<APPLICATION NAME>';delete from maxpresentation where  ...

  4. C语言typedef的用法(转)

    http://www.cnblogs.com/afarmer/archive/2011/05/05/2038201.html 一.基本概念剖析 int* (*a[5])(int, char*);    ...

  5. iOS内存管理

    iOS内存管理的方式是引用计数机制.分为MRC(人式引用计数)和ARC(自动引用计数). 为什么要学习内存管理? 内存管理方式是引用计数机制,通过控制对象的引用计数来实现操作对象的功能.一个对象的生命 ...

  6. 浅析NRF51822合并文件之app_valid_setting_apply

    [原创出品§转载请注明出处] 出处:http://www.cnblogs.com/libra13179/p/5787084.html 我们打开app_valid_setting_apply.hex如下 ...

  7. information_schema系列十二

    1: INNODB_SYS_VIRTUAL 表存储的是INNODB表的虚拟列的信息,当然这个还是比较简单的,我们直接通过SHOW CREATE TABLE 或者DESC TABLE就能看得到. Col ...

  8. javascript算法

    代码运行环境: nodejs + mochajs /* *选择排序 *每次查找数组最小数据 *将最小数据排到左侧 */ var assert = require('assert'); describe ...

  9. 【英语魔法俱乐部——读书笔记】 1 初级句型-简单句(Simple Sentences)

    第一部分 1 初级句型-简单句(Simple Sentences):(1.1)基本句型&补语.(1.2)名词短语&冠词.(1.3)动词时态.(1.4)不定式短语.(1.5)动名词.(1 ...

  10. 关于Onvif的event

    昨天又仔细研究了一下camera的alarm功能,发现原来很简单,首先订阅一下,即create,拿到订阅号后直接pull,一旦收到信息就再次用订阅号pull.参考http://www.doc88.co ...