http://www.cnblogs.com/linhaifeng/articles/6204014.html#_label12

描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实现了__get__(),__set__(),__delete__()中的一个,这也被称为描述符协议
__get__():调用一个属性时,触发
__set__():为一个属性赋值时,触发
__delete__():采用del删除属性时,触发

定义一个描述符
class Foo: #在python3中Foo是新式类,它实现了三种方法,这个类就被称作一个描述符
def __get__(self, instance, owner):
pass
def __set__(self, instance, value):
pass
def __delete__(self, instance):
pass

描述符是干什么的:描述符的作用是用来代理另外一个类的属性的(必须把描述符定义成这个类的类属性,不能定义到构造函数中)

#描述符Str
class Str:
def __get__(self, instance, owner):
print('Str调用')
def __set__(self, instance, value):
print('Str设置...')
def __delete__(self, instance):
print('Str删除...') #描述符Int
class Int:
def __get__(self, instance, owner):
print('Int调用')
def __set__(self, instance, value):
print('Int设置...')
def __delete__(self, instance):
print('Int删除...') class People:
name=Str()
age=Int()
def __init__(self,name,age): #name被Str类代理,age被Int类代理,
self.name=name
self.age=age #何地?:定义成另外一个类的类属性 #何时?:且看下列演示 p1=People('alex',18)
'''
Str设置...
Int设置...
''' # #描述符Str的使用
p1.name # Str调用
p1.name='zhangsan' # Str设置...
del p1.name # Str删除...
#
# #描述符Int的使用
p1.age # Int调用
p1.age=18 # Int设置...
del p1.age # Int删除... # #我们来瞅瞅到底发生了什么
print(p1.__dict__) # {}
print(People.__dict__) # {'__weakref__': <attribute '__weakref__' of 'People' objects>,
# '__dict__': <attribute '__dict__' of 'People' objects>, '__doc__': None,
# '__init__': <function People.__init__ at 0x0000013E94AC5E18>, '__module__': '__main__',
# 'name': <__main__.Str object at 0x0000013E94AD81D0>, 'age': <__main__.Int object at 0x0000013E94AD8198>} # #补充
print(type(p1) == People) #type(obj)其实是查看obj是由哪个类实例化来的 True
print(type(p1).__dict__ == People.__dict__) # True
#描述符Str
class Str:
def __get__(self, instance, owner):
print('Str调用')
def __set__(self, instance, value):
print('Str设置...')
instance.__dict__['name'] = value
def __delete__(self, instance):
print('Str删除...') #描述符Int
class Int:
def __get__(self, instance, owner):
print('Int调用')
def __set__(self, instance, value):
print('Int设置...')
instance.__dict__['age'] = value
def __delete__(self, instance):
print('Int删除...') class People:
name=Str()
age=Int()
def __init__(self,name,age): #name被Str类代理,age被Int类代理,
self.name=name
self.age=age p1=People('alex',18)
print(p1.__dict__) # {'name': 'alex', 'age': 18}

描述符分两种
一 数据描述符:至少实现了__get__()和__set__()

 class Foo:
def __set__(self, instance, value):
print('set')
def __get__(self, instance, owner):
print('get')

非数据描述符:没有实现__set__()

class Foo:
def __get__(self, instance, owner):
print('get')

注意事项:

一 描述符本身应该定义成新式类,被代理的类也应该是新式类
二 必须把描述符定义成这个类的类属性,不能为定义到构造函数中
三 要严格遵循该优先级,优先级由高到底分别是
1.类属性
2.数据描述符
3.实例属性
4.非数据描述符
5.找不到的属性触发__getattr__()

#描述符Str
class Str:
def __init__(self,key):
self.key = key
def __get__(self, instance, owner):
print('Str调用')
return instance.__dict__[self.key]
def __set__(self, instance, value):
print('Str设置...')
if type(value) is not str:
raise TypeError("类型错误")
instance.__dict__[self.key] = value
def __delete__(self, instance):
print('Str删除...')
instance.__dict__.pop(self.key) #描述符Int
class Int:
def __init__(self,key):
self.key = key
def __get__(self, instance, owner):
print('Int调用')
return instance.__dict__[self.key]
def __set__(self, instance, value):
print('Int设置...')
if type(value) is not int:
raise TypeError("请输入int型")
instance.__dict__['age'] = value
def __delete__(self, instance):
print('Int删除...')
instance.__dict__.pop(self.key) class People:
name=Str("name")
age=Int("age")
def __init__(self,name,age): #name被Str类代理,age被Int类代理,
self.name=name
self.age=age p1=People("zhangsan",18)
print(p1.__dict__) # {'age': 18, 'name': 'zhangsan'}
print(p1.age)
del p1.age
print(p1.__dict__) # {'age': 18}

上面代码改进:

class Typed:
def __init__(self,key, expected_type):
self.key = key
self.expected_type = expected_type
def __get__(self, instance, owner):
return instance.__dict__[self.key]
def __set__(self, instance, value):
if not isinstance(value, self.expected_type):
raise TypeError("%s is not %s" %(value, self.expected_type))
instance.__dict__[self.key] = value
def __delete__(self, instance):
instance.__dict__.pop(self.key) class People:
name=Typed("name", str)
age=Typed("age", int)
def __init__(self,name,age): #name被Str类代理,age被Int类代理,
self.name=name
self.age=age #p1=People(99,18) # TypeError: 99 is not <class 'str'>
p1=People("zhangsan",'') # TypeError: 18 is not <class 'int'>

描述符__get__(),__set__(),__delete__()(三十七)的更多相关文章

  1. python基础----再看property、描述符(__get__,__set__,__delete__)

    一.再看property                                                                          一个静态属性property ...

  2. 描述符__get__,__set__,__delete__和析构方法__del__

    描述符__get__,__set__,__delete__ 1.描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实现了__get__(),__set__(),__delete__()中的一 ...

  3. 描述符__get__,__set__,__delete__

    描述符__get__,__set__,__delete__ # 描述符:1用来代理另外一个类的属性 # __get__():调用一个属性时,触发 # __set__():为一个属性赋值时触发 # __ ...

  4. Python类总结-描述符__get__(),__set__(),__delete__()

    1 描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实现了__get__(),set(),delete()中的一个,这也被称为描述符协议 get():调用一个属性时,触发 set():为一 ...

  5. python小知识-__call__和类装饰器的结合使用,数据描述符__get__\__set__\__delete__(描述符类是Python中一种用于储存类属性值的对象)

    class Decorator(): def __init__(self, f): print('run in init......') self.f = f def __call__(self, a ...

  6. Python描述符(__get__,__set__,__delete__)简介

    先说定义,这里直接翻译官方英文文档: 一般来说,描述符是具有“绑定行为”的对象属性,该对象的属性访问将会被描述符协议中的方法覆盖.这些方法是__get__(),__set__(),和__delete_ ...

  7. __get__ __set__ __delete__描述符

    描述符就是一个新式类,这个类至少要实现__get__ __set__ __delete__方法中的一种class Foo: def __get__(self, instance, owner): pr ...

  8. 描述符(__get__和__set__和__delete__)

    目录 一.描述符 二.描述符的作用 2.1 何时,何地,会触发这三个方法的执行 三.两种描述符 3.1 数据描述符 3.2 非数据描述符 四.描述符注意事项 五.使用描述符 5.1 牛刀小试 5.2 ...

  9. Python之路(第二十七篇) 面向对象进阶:内置方法、描述符

    一.__call__ 对象后面加括号,触发执行类下面的__call__方法. 创建对象时,对象 = 类名() :而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()( ...

随机推荐

  1. zabbix中配置当memory剩余不足20%时触发报警

    在zabbix中默认当内存剩余量不足2G的时候触发报警,并没有使用百分比来触发如下: 现在需要配置:当memory剩余不足20%时触发报警,具体操作方法如下: 1)创建itemConfiguratio ...

  2. Ansible之playbook的使用总结 - 运维笔记

    之前详细介绍了Ansible的安装, 配置, 以及Ansible常用模块的使用. 下面对Ansible的playbook用法做一小结. 为什么引入playbook?一般运维人员完成一个任务, 比如安装 ...

  3. 硬编码转换单位||vue

    //测试单位 formatUnit:function (id) { var val; switch(id){ case 4: return val="圈" break; } } / ...

  4. 自己搭建的一个react脚手架

    包括了: react.react router(v4), webpack(v4),echarts, google的组件库material ui, 后期会加上redux但是这些做中小型系统已经够了,de ...

  5. 毕设之c#多线程学习(官方+转载)

    官方文档,原址:打开 如何:对 Windows 窗体控件进行线程安全调用   使用多线程提高 Windows 窗体应用程序的性能时,必须注意以线程安全方式调用控件. 示例 访问 Windows 窗体控 ...

  6. Python学习笔记 --第二章

    Python语法基础 "#"号为注释符,建议缩进四个空格,Python大小写敏感. 数据类型 整数 0,2等等,以0x开头的为十六进制数 浮点数 1.58e9 字符串 用'或"括起来的任意文 ...

  7. 开源通用爬虫框架YayCrawler-框架的运行机制

    这一节我将向大家介绍一下YayCrawler的运行机制,首先允许我上一张图: 首先各个组件的启动顺序建议是Master.Worker.Admin,其实不按这个顺序也没关系,我们为了讲解方便假定是这个启 ...

  8. python 协程库gevent学习--gevent数据结构及实战(四)

    一不留神已经到第四部分了,这一部分继续总结数据结构和常用的gevent类,废话不多说继续. 1.Timeout错误类 晚上在调试调用第三方接口的时候,发现有些接口耗时非常多,觉得应该有个超时接口来限制 ...

  9. calico实现docker容器内部的网络链接

    calico官网 https://www.projectcalico.org// calico介绍 http://www.sdnlab.com/17161.html calico网络 环境 系统   ...

  10. 下载系统已经安装的rpm包

    下载系统已经安装的rpm包 yum -y install yum-utils 安装yum下载工具 yumdownloader mysql 用yum下载到当前目录 实例:查询mysql安装包[root@ ...