python描述符
class Type:
def __init__(self, key, expect_type):
self.key = key
self.expect_type = expect_type def __get__(self, instance, owner):
print('执行get方法')
return instance.__dict__[self.key] def __set__(self, instance, value):
print('执行set方法')
if not isinstance(value, self.expect_type):
raise TypeError('你传入的不是',self.expect_type) instance.__dict__[self.key]=value def __delete__(self, instance):
print('执行delete方法')
instance.__dict__.pop(self.key) class People:
name = Type('name', str)
age = Type('age', int) def __init__(self, name, age):
self.name = name
self.age = age p = People('alex', 11)
print(p.name)
class Type:
def __init__(self, key, expect_type):
self.key = key
self.expect_type = expect_type def __get__(self, instance, owner):
print('执行get方法')
return instance.__dict__[self.key] def __set__(self, instance, value):
print('执行set方法')
if not isinstance(value, self.expect_type):
raise TypeError('你传入的不是',self.expect_type) instance.__dict__[self.key]=value def __delete__(self, instance):
print('执行delete方法')
instance.__dict__.pop(self.key) def deco(**kwargs): # kwargs = {'name':str, 'age': int}
def wrapper(obj): # obj = People
print('--->',kwargs)
print('类名',obj)
for key, val in kwargs.items(): # ('name',str),('age',int) setattr(obj, key, Type(key, val))
return obj
print(kwargs)
return wrapper @deco(name=str, age=int) # @wrapper ==> People= wrapper(People)
class People:
def __init__(self, name, age):
self.name = name
self.age = age p = People('alex', 11) print(p.__dict__)
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__()中 ...
随机推荐
- 【Python】 解析Python中的运算符
Python中的运算符相比较于传统的C/C++差别不是很大,主要是一些个别的运算符上的差别.包括:算术.比较.赋值.位.逻辑.成员.身份等.它们的优先级: 符号 说明 ** 指数(最高优先级) ~,+ ...
- canvas 水滴图、液体进度条、仿加速球、圆球水波图
传送门:https://github.com/guoyoujin/WaterMoire <!DOCTYPE html> <html lang="en"> & ...
- bootstrap 前端模板
https://colorlib.com/wp/free-bootstrap-admin-dashboard-templates/
- 移动开发常用meta设置
<!-- 视图窗口,移动端特属的标签. --> <meta name="viewport" content="width=device-width,in ...
- ES容易忽视的集群配置
一 前言 目前生产系统由Solr转ES了,在这边就记录下在使用过程中容易忽视的配置吧,其实我也是才用,如果有什么错误的地方,多指正. 二.配置 1.ES的段合并是限速设置 默认是20MB/s ,如果是 ...
- 基于vue-cli配置手淘的lib-flexible + rem,实现移动端自适应
没接触过flexible的建议先看看大漠的这篇文章这样你才会知道长度为什么用rem,而字体要用px 安装flexible npm install lib-flexible --save 引入flexi ...
- JAVA中有一个特殊的类: Object。它是JAVA体系中所有类的父类(直接父类或者间接父类)。
接口往往被我们定义成一类XX的东西. 接口实际上是定义一个规范.标准. ① 通过接口可以实现不同层次.不同体系对象的共同属性: 通过接口实现write once as anywhere. 以JA ...
- 项目中phpexcel的基本用法
前提:要下载PHPEXCEL库文件 如:phpexcel官方下载 ,本人使用下载 情形一:对于将数据写入EXCEL表中的用法 header("content-type:text/htm ...
- struts表单域模型注入
表单使用struts标签,表单中每一个字段都可以这样来赋值 类(action).成员变量 这个叫域模型注入 <s:form action="orders" method=&q ...
- laravel5.8笔记五:基类控制器和基类模型
建立基类的目的就是为了方便继承.比如:Admin模块访问,是否登陆.检测登陆可以写到基类里面 控制器基类 原始基类:app\Http\Controllers\Controller.php,我们下面要做 ...