类的装饰器

# def deco(func):
# print('==========')
# return func
#
# # @deco #test=deco(test)
# # def test():
# # print('test函数运行')
# # test()
#
# @deco #Foo=deco(Foo)
# class Foo:
# pass def deco(obj):
print('==========',obj)
obj.x=1
obj.y=2
obj.z=3
return obj
# @deco #Foo=deco(Foo)
# class Foo:
# pass
#
# print(Foo.__dict__) #一切皆对象
# # @deco #test=deco(test)
# def test():
# print('test函数')
# test.x=1
# test.y=1
# print(test.__dict__)

类的装饰器加强版

def Typed(**kwargs):
def deco(obj):
for key,val in kwargs.items():
# obj.key=val
setattr(obj,key,val)
return obj
return deco @Typed(x=1,y=2,z=3) #1.Typed(x=1,y=2,z=3) --->deco 2.@deco---->Foo=deco(Foo)
class Foo:
pass
print(Foo.__dict__) # @Typed(name='egon') #@deco ---->Bar=deco(Bar)
# class Bar:
# pass
# print(Bar.name)

类的装饰器的应用

class Typed:
def __init__(self,key,expected_type):
self.key=key
self.expected_type=expected_type
def __get__(self, instance, owner):
print('get方法')
# print('instance参数【%s】' %instance)
# print('owner参数【%s】' %owner)
return instance.__dict__[self.key]
def __set__(self, instance, value):
print('set方法')
# print('instance参数【%s】' % instance)
# print('value参数【%s】' % value)
# print('====>',self)
if not isinstance(value,self.expected_type):
# print('你传入的类型不是字符串,错误')
# return
raise TypeError('%s 传入的类型不是%s' %(self.key,self.expected_type))
instance.__dict__[self.key]=value
def __delete__(self, instance):
print('delete方法')
# print('instance参数【%s】' % instance)
instance.__dict__.pop(self.key) def deco(**kwargs): #kwargs={'name':str,'age':int}
def wrapper(obj): #obj=People
for key,val in kwargs.items():#(('name',str),('age',int))
setattr(obj,key,Typed(key,val))
# setattr(People,'name',Typed('name',str)) #People.name=Typed('name',str)
return obj
return wrapper
@deco(name=str,age=int) #@wrapper ===>People=wrapper(People)
class People:
name='alex'
# name=Typed('name',str)
# age=Typed('age',int)
def __init__(self,name,age,salary,gender,heigth):
self.name=name
self.age=age
self.salary=salary
# p1=People('213',13.3,13.3,'x','y')
print(People.__dict__)

Python学习第十九课——类的装饰器的更多相关文章

  1. Python学习第十五课——类的基本思想(实例化对象,类对象)

    类的基本思想 类:把一类事物的相同的特征和动作整合到一起就是类类是一个抽象的概念 对象:就是基于类而创建的一个具体的事物(具体存在的)也是特征和动作整合到一块 对象写法 # 对象写法 def scho ...

  2. Python学习第十五篇——类继承和类实例化

    学习Python类时,我们明白了类的本质,以及所谓的面向对象编程思想强调的对事物本身的属性,我们对某一类事物进行描述——采用了很多方法,这些方法描述了类的属性(比如猫科动物的眼睛,四肢,是否哺乳类等等 ...

  3. python 学习笔记十九 django深入学习四 cookie,session

    缓存 一个动态网站的基本权衡点就是,它是动态的. 每次用户请求一个页面,Web服务器将进行所有涵盖数据库查询到模版渲染到业务逻辑的请求,用来创建浏览者需要的页面.当程序访问量大时,耗时必然会更加明显, ...

  4. Python学习第十六课——静态属性(property, classmethod, staticmethod)

    计算所居住房子的面积 普通写法 class Room: def __init__(self,name,owner,width,length,heigh): self.name=name self.ow ...

  5. Python学习(三):迭代器、生成器、装饰器、递归、算法、正则

    1.迭代器 迭代器是访问集合的一种方式,迭代对象从集合的第一个元素开始访问,直到元素被访问结束,迭代器只能往前不能后退,最大的优点是不要求事先准备好整个迭代过程中的元素,这个特点使得它特别适合用于遍历 ...

  6. python学习之类和实例的属性;装饰器@property

    无论是类还是实例,一切皆是对象. Python是强动态语言,和java在这点上有所不同. class Ab(): a = 666 # 定义类对象Ab,自带属性a,值为666 # 使用Ab.__dict ...

  7. Python学习笔记之生成器、迭代器和装饰器

    这篇文章主要介绍 Python 中几个常用的高级特性,用好这几个特性可以让自己的代码更加 Pythonnic 哦 1.生成器 什么是生成器呢?简单来说,在 Python 中一边循环一边计算的机制称为 ...

  8. python学习笔记四 迭代器,生成器,装饰器(基础篇)

    迭代器 __iter__方法返回一个迭代器,它是具有__next__方法的对象.在调用__next__方法时,迭代器会返回它的下一个值,若__next__方法调用迭代器 没有值返回,就会引发一个Sto ...

  9. python 学习2:生成器,迭代器,装饰器

    1.生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万  个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那 ...

随机推荐

  1. Dubbo监控中心搭建-dubbo-monitor-simple的使用

    场景 Dubbo环境搭建-管理控制台dubbo-admin实现服务监控: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10362 ...

  2. sshpass远程登陆

    1,ssh ssh 端口为默认22的时候: sshpass -p 888888 scp -o StrictHostKeyChecking=no /root/images.zip root@21.1.9 ...

  3. js克隆一个对象

    我们知道,对象类型在赋值的过程中其实是复制了地址,所以如果改变了一方,其他都会被改变.我们应该如何克隆一个对象,并且避免这种现象的发生呢? 方法一:Object.assign function cop ...

  4. opencv:通道的分离与合并

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  5. CSS 绝对定位时,水平居中而不影响原文档中其它元素

    div.absolutemiddle { position: absolute; left: 50%; transform: translate(-50%); /* 平移50%为自身尺寸的一半,实现水 ...

  6. 浅谈hover用法

    在前端页面制作中,我们时常要用到移动显示.隐藏的动态效果,我们一般采用js来实现此效果.不过在大部分情况下,我们也可以使用hover来实现此动态效果. 在此,我谈一谈我对hover的用法,请看以下代码 ...

  7. fastjson数据返回配置

    阿里的fastjson 包升级后,可能导致返回的json 数据,字段为null时不显示等问题 <dependency> <groupId>com.alibaba</gro ...

  8. 2020qbxt D1T3 停车

    嗯... 题目: [问题描述] 市中心有一个环形的停车场,编号1到n,现在有m个车要停,停在每个位置会有不同的费用.为了方便,不允许两辆车停在相邻的位置,请问停好所有车的最小花费是多少? [输入格式] ...

  9. Hadoop学习笔记(三):分布式文件系统的写和读流程

    写流程:怎么将文件切割成块,上传到服务器 读流程:怎么从不同的服务器来读取数据块 写流程 图一 图二 写的过程中:NameNode会给块分配存储块的位置,每次想要存储文件的时候都会在NameNode创 ...

  10. 【硬核教程】只需1秒—你也可以有自己的API文档

    Nothing is true. Everything is permitted. 写在前面 先聊聊为什么想到了要用Vuepress来代替原来写在Confluence上的文档. 大意是有个需要其他部门 ...