python描述符的应用
使用描述符为python实现类型检测
class Typed:
def __get__(self, instance, owner):
print(instance)
print(owner)
def __set__(self, instance, value):
pass
class Girl:
name = Typed()
def __init__(self, name, age):
self.name = name # 我希望传入的name是str
self.age = age # 我希望传入的age是int
g = Girl("satori", 18)
g.name
'''
<__main__.Girl object at 0x03D28430>
<class '__main__.Girl'>
'''
# 可以看到当我们获取被代理的值,会触发get方法,instance就是Girl的实例对象
# owner则是Girl这个类
class Typed:
def __get__(self, instance, owner):
pass
def __set__(self, instance, value):
print(instance)
print(value)
class Girl:
name = Typed()
def __init__(self, name, age):
self.name = name # 我希望传入的name是str
self.age = age # 我希望传入的age是int
g = Girl("satori", 18)
'''
<__main__.Girl object at 0x03D28430>
satori
'''
# 可以看到当我们给被代理的值赋值的时候,会触发set方法,instance就是Girl的实例对象
# value则是我们赋的值
class Typed:
def __init__(self, key):
self.key = key
def __get__(self, instance, owner):
return instance.__dict__[self.key]
def __set__(self, instance, value):
instance.__dict__[self.key] = value
def __delete__(self, instance):
instance.__dict__.pop(self.key)
class Girl:
name = Typed("name")
age = Typed("age")
def __init__(self, name, age):
self.name = name
self.age = age
g = Girl("satori", 18)
print(g.__dict__)
g.name = "mashiro"
print(g.__dict__)
del g.name
print(g.__dict__)
'''
{'name': 'satori', 'age': 18}
{'name': 'mashiro', 'age': 18}
{'age': 18}
'''
# 等于说 我饶了一圈,并没有直接将属性添加的字典里面,而是向上找了代理,让代理帮我把属性添加的字典里面去
那么,接下来就可以实现类型检测了
class Typed:
def __init__(self, key, except_type):
self.key = key
self.except_type = except_type
def __get__(self, instance, owner):
return instance.__dict__[self.key]
def __set__(self, instance, value):
if isinstance(value, self.except_type):
instance.__dict__[self.key] = value
else:
raise Exception(f"{self.key} must be type {self.except_type}")
def __delete__(self, instance):
instance.__dict__.pop(self.key)
class Girl:
name = Typed("name", str)
age = Typed("age", int)
def __init__(self, name, age):
self.name = name
self.age = age
g = Girl("satori", 18)
print(g.__dict__) # {'name': 'satori', 'age': 18}
try:
import traceback
g = Girl("satori", "18")
except Exception:
print(traceback.format_exc())
'''
Exception: age must be type <class 'int'>
'''
# 于是我们便手动实现了python的类型检测
类的装饰器
# 装饰器不仅可以给函数用,还可以给类用
def deco(obj):
print("======")
return obj
@deco
class Girl:
def __init__(self, name, age):
self.name = name
self.age = age
g = Girl("satori", 18)
print(g.__dict__)
'''
======
{'name': 'satori', 'age': 18}
'''
描述符实现property
class LazeProperty:
def __init__(self, func):
self.func = func
def __get__(self, instance, owner):
# 如果是类调用,那么instance为空
if instance is None:
return self
res = self.func(instance)
setattr(instance, self.func.__name__, res) # 将值设进去,如果有从字典里面找,没有再计算
return res
class Girl:
def __init__(self, name, age):
self.name = name
self.age = age
@LazeProperty # info = LazeProperty(info)
def info(self):
return f"my name is {self.name},age is {self.age}"
g = Girl("satori", 18)
print(g.info) # my name is satori,age is 18
print(Girl.info) # <__main__.LazeProperty object at 0x04EF3910>
python描述符的应用的更多相关文章
- 杂项之python描述符协议
杂项之python描述符协议 本节内容 由来 描述符协议概念 类的静态方法及类方法实现原理 类作为装饰器使用 1. 由来 闲来无事去看了看django中的内置分页方法,发现里面用到了类作为装饰器来使用 ...
- python描述符(descriptor)、属性(property)、函数(类)装饰器(decorator )原理实例详解
1.前言 Python的描述符是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题 ...
- 【转载】Python 描述符简介
来源:Alex Starostin 链接:www.ibm.com/developerworks/cn/opensource/os-pythondescriptors/ 关于Python@修饰符的文章可 ...
- python描述符descriptor(一)
Python 描述符是一种创建托管属性的方法.每当一个属性被查询时,一个动作就会发生.这个动作默认是get,set或者delete.不过,有时候某个应用可能会有 更多的需求,需要你设计一些更复杂的动作 ...
- python描述符 descriptor
descriptor 在python中,如果一个新式类定义了__get__, __set__, __delete__方法中的一个或者多个,那么称之为descriptor.descriptor通常用来改 ...
- Python描述符的使用
Python描述符的使用 前言 作为一位python的使用者,你可能使用python有一段时间了,但是对于python中的描述符却未必使用过,接下来是对描述符使用的介绍 场景介绍 为了引入描述符的使用 ...
- Python描述符 (descriptor) 详解
1.什么是描述符? python描述符是一个“绑定行为”的对象属性,在描述符协议中,它可以通过方法重写属性的访问.这些方法有 __get__(), __set__(), 和__delete__().如 ...
- python描述符和属性查找
python描述符 定义 一般说来,描述符是一种访问对象属性时候的绑定行为,如果这个对象属性定义了__get__(),__set__(), and __delete__()一种或者几种,那么就称之为描 ...
- Iterator Protocol - Python 描述符协议
Iterator Protocol - Python 描述符协议 先看几个有关概念, iterator 迭代器, 一个实现了无参数的 __next__ 方法, 并返回 '序列'中下一个元素,在没有更多 ...
- Python描述符以及Property方法的实现原理
Python描述符以及Property方法的实现原理 描述符的定义: 描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实了__get__(),__set__(),__delete__()中 ...
随机推荐
- 使用系统的某些block api(如UIView的block版本写动画时),是否也考虑循环引用问题?
系统的某些block api中,UIView的block版本写动画时不需要考虑,但也有一些api 需要考虑 以下这些使用方式不会引起循环引用的问题 [UIView animateWithDuratio ...
- github+git提交 基础用法
git版本管理基本用法: 安装就不用说了 随便一搜 安装完 妥妥的.下边说的是在github从新建一个项目开始: 1.首先打开自己的github地址,如下图所示 点加号 选 New repositor ...
- appium-手势密码实现-automationName 是Appium的情况
1. 红色区域的范围为:[66,575][1014,1523], 由于这块是一个整块,所以无法使用每个点的数据:因此只能使用LockPatternView对象拿到左上角的坐标值 2. 原理, 将九宫 ...
- NGUI注册事件的三种方式
1.第一种方式 当一个元素要执行某个方法,而这个方法在此元素赋予的脚本上有,那么直接会调用此方法,但此方法的名称必须是内置的固定名称,例如OnClick,OnMouseOver,OnMouseOut等 ...
- Vim常用指令总结(持续更新中)
1 模式变更 命令 说明 a(append)/i(insert) 普通模式→插入模式 : 普通模式→命令行模式 ESC或者Ctrl 插入模式→普通模式 R(Replace)/Insert两次 普通模式 ...
- 课时21:函数:lambda表达式
目录: 一.lambda表达式 二.介绍两个BIF:filter()和map() 三.课时21课后习题及答案 ********************* 一.lambda表达式 *********** ...
- CodeBlocks X64 SVN 编译版
CodeBlocks X64 SVN 编译版 采用官方最新的SVN源码编译而来,纯64位的,所以32位系统是不能使用的.字体使用的是微软的YaHei UI字体,如果有更好的字节建议,可以留言. 由于直 ...
- 聊聊、Spring WebApplicationInitializer
说到 WebApplicationInitializer,这个接口是为了实现代码配置 Web 功能.只要实现了这个接口,那么就可以实现 Filter,Servlet,Listener 等配置,跟在 x ...
- Python——开篇之词
我也断断续续的用Python挺长时间了.但是一直都没有系统的学习过Python.很多东西都是现用现学.这样感觉对Python的理解太浅,完完全全就是搬砖的. 因此,我专门找了一个比较完整的老男孩的Py ...
- Limeng:Individual Project: Word frequency program -BUAA Advanced Software Engineering
11061190-李孟 Implement a console application to tally the frequency of words under a directory (2 mod ...