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. Win10一直弹出 用户账户控制 解决

    目录 问题 解决方法一 将appwiz.cpl加入UAC白名单 解决方法二 问题 自从更新了Windows后,就出现了一个问题,隔一段时间就弹出一次下面的对话框,而且如果时间稍长,会弹出多个. 解决方 ...

  2. CMD 命令2

    cd  %~dp0 切换到当前脚本所有目录 批处理常用命令总结 - 批处理命令简介 目录 echo 打开回显或关闭请求回显功能,或显示消息.如果没有任何参数,echo 命令将显示当前回显设置. ech ...

  3. Effective Java 第三版——65. 接口优于反射

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  4. Python PhantomJS 爬虫 示例

    from selenium import webdriver# 请求url url = "https://auctions.freemansauction.com/auction-lot-d ...

  5. 【iCore4 双核心板_ARM】例程二十四:LWIP_DHCP实验——动态分配IP地址

    实验现象: 核心代码: int main(void) { system_clock.initialize(); led.initialize(); adc.initialize(); delay.in ...

  6. 会使用基本的Render函数后,就会想,这怎么用 v-for/v-if/v-model;我写个vue Render函数进阶

    https://blog.csdn.net/wngzhem/article/details/54291024

  7. git常见用法介绍

    1. git help:帮助 git help xxx git xxx --help 2. git init:初始化 git init 3. git config:配置 常见用法 git config ...

  8. 在 windows 上安装免安装版的mysql

    在 windows 上安装免安装版的 1. 下载路径 https://downloads.mysql.com/archives/community/ 2. 解压 假设解压后的路径为:E:\Enviro ...

  9. ubuntu14.04安装MATLAB R2017b步骤详解

    转载:https://blog.csdn.net/qq_32892383/article/details/79670871 1. 前言最近由于项目原因,需要在ubuntu上安装MATLAB,在网上找了 ...

  10. 在windows下编写shell脚本

    注意两点: 1.第一行:#!/bin/bash 2.将文档格式转换为unix,因为在windows下编写shell脚本回车符是\n\r,而linux下的回车符是\n,所以在linux下运行脚本的时候, ...