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描述符的更多相关文章

  1. 杂项之python描述符协议

    杂项之python描述符协议 本节内容 由来 描述符协议概念 类的静态方法及类方法实现原理 类作为装饰器使用 1. 由来 闲来无事去看了看django中的内置分页方法,发现里面用到了类作为装饰器来使用 ...

  2. python描述符(descriptor)、属性(property)、函数(类)装饰器(decorator )原理实例详解

     1.前言 Python的描述符是接触到Python核心编程中一个比较难以理解的内容,自己在学习的过程中也遇到过很多的疑惑,通过google和阅读源码,现将自己的理解和心得记录下来,也为正在为了该问题 ...

  3. 【转载】Python 描述符简介

    来源:Alex Starostin 链接:www.ibm.com/developerworks/cn/opensource/os-pythondescriptors/ 关于Python@修饰符的文章可 ...

  4. python描述符descriptor(一)

    Python 描述符是一种创建托管属性的方法.每当一个属性被查询时,一个动作就会发生.这个动作默认是get,set或者delete.不过,有时候某个应用可能会有 更多的需求,需要你设计一些更复杂的动作 ...

  5. python描述符 descriptor

    descriptor 在python中,如果一个新式类定义了__get__, __set__, __delete__方法中的一个或者多个,那么称之为descriptor.descriptor通常用来改 ...

  6. Python描述符的使用

    Python描述符的使用 前言 作为一位python的使用者,你可能使用python有一段时间了,但是对于python中的描述符却未必使用过,接下来是对描述符使用的介绍 场景介绍 为了引入描述符的使用 ...

  7. Python描述符 (descriptor) 详解

    1.什么是描述符? python描述符是一个“绑定行为”的对象属性,在描述符协议中,它可以通过方法重写属性的访问.这些方法有 __get__(), __set__(), 和__delete__().如 ...

  8. python描述符和属性查找

    python描述符 定义 一般说来,描述符是一种访问对象属性时候的绑定行为,如果这个对象属性定义了__get__(),__set__(), and __delete__()一种或者几种,那么就称之为描 ...

  9. Iterator Protocol - Python 描述符协议

    Iterator Protocol - Python 描述符协议 先看几个有关概念, iterator 迭代器, 一个实现了无参数的 __next__ 方法, 并返回 '序列'中下一个元素,在没有更多 ...

  10. Python描述符以及Property方法的实现原理

    Python描述符以及Property方法的实现原理 描述符的定义: 描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实了__get__(),__set__(),__delete__()中 ...

随机推荐

  1. cpu使用过高的一次处理方法

    1.top查看使用情况 2.查看mysql里的线程,观察是否有长期运行或阻塞的sql: show full processlist 原因找到,处理方法,添加索引,搞定

  2. Chrome 控制台报错Unchecked runtime.lastError: The message port closed before a response was received

    Chrome浏览器控制台报错提示 Unchecked runtime.lastError: The message port closed before a response was received ...

  3. Why does deep learning work?

    Learning Deep Architectures for AI By Yoshua Bengio http://www.iro.umontreal.ca/~bengioy/papers/ftml ...

  4. python时间模块time详解

    在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. 在开始之前,首先要说明这几点: ...

  5. MySQL查询where条件的顺序对查询效率的影响<转>

    看到有资料说,where条件的顺序会影响查询的效率,根据的逻辑是: where条件的运行是从右到左的,将选择性强的条件放到最右边,可以先过滤掉大部分的数据(而选择性不强的条件过滤后的结果集仍然很大), ...

  6. linux和windows强制用户第一次登陆修改密码

    linux: passwd -e root windows: 计算机右键->管理->本地用户和组->用户->administrator->下一次登陆修改密码 如果密码复杂 ...

  7. (原)kenel开机logo的制作

    今天项目需要,需要制作一个kernel的开机logo,所以在rk3288的平台上进行测试一番. 第一步:配置kernel:选上CONFIG_LOGO_LINUX_CLUT224选项 make menu ...

  8. node-sass 安装失败 Failed at the node-sass@4.9.2 postinstall script的解决

    控制台运行npm install时报错,报错信息如下: npm ERR! code ELIFECYCLEnpm ERR! errno 1npm ERR! node-sass@4.9.2 postins ...

  9. python什么时候加self,什么时候不加self

    1.self是什么,一般都说指对象本身,这样说了没了用,说了后还是很难懂,因为这样说了后,仍然完全搞不清楚,什么时候变量前需要加self,什么时候不需要加self. 造成很多人,已经怕了self,不停 ...

  10. python 记录linux网速到文件。

    import timefrom app.utils_ydf import LogManager logger = LogManager('network_monitor').get_logger_an ...