python-property
来源:互联网 发布:淘宝联盟分享赚 实操 编辑:程序博客网 时间:2024/06/05 19:37
在说property之前,我们需要理解描述符,因为不管property还是classmethod都是构建在描述符的基础上,那么到底什么是描述符呢?
描述符,用一句话来说,就是将某种特殊类型的类的实例指派给另一个类的属性(注意:这里是类属性,而不是对象属性)。而这种特殊类型的类就是实现了__get__,__set__,__delete__的新式类(即继承object)。
看下面一段代码
- class Descriptor(object):
- def __get__(self,object,type):
- print 'get',self,object,type
- def __set__(self,object,value):
- print 'set',self,object,value
- class Demo(object):
- desc= Descriptor()
- demo=Demo()
- demo.desc # get <__main__.Descriptor object at 0x0269BC90> <__main__.Demo object at 0x0269BD50> <class '__main__.Demo'>
- demo.desc='my descriptor' #set <__main__.Descriptor object at 0x0269BC90> <__main__.Demo object at 0x0269BD50> my descripto
从输出结果我们可以看出,__get__方法中的object就是调用描述符对象的实例,即对象demo,type就是demo的类。
阅读全文
0 0
- python -- property()/@property
- python @property
- Python @property
- Python @property
- Python @property
- python @property
- python Property
- Python @property
- python property
- python property
- python @property
- Python @property
- python property
- python property
- Python @property
- python-property
- python @property
- python @property
- java小算法—将阿拉伯字母转为汉字
- 呵呵
- Java集合——HashMap(四)扩容机制——resize()
- linux文件重定向问题
- 多层limit查询 mysql出现异常This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
- python-property
- BZOJ 3942: [Usaco2015 Feb]Censoring KMP
- ubuntu安装python3.5并改默认python为3.5
- shared_ptr智能指针源码剖析
- Docker windows安装及遇到的坑爹问题
- windows系统用anaconda配置安装TensorFlow (2017.4.13)tensorflow支持python3.5版本 1.打开anaconda依次输入: 1 conda con
- 解决Ubuntu16.04网络图标启用wifi不见的问题
- Python中继承、多态、多继承、判断类型、json.load()解析、获取/设置对象信息
- 06-09 16:15:24.109 25819-25819/net.iusky.yijiayou E/CrashReport: java.lang.IllegalStateException: Th