Python学习第十九课——类的装饰器
类的装饰器
- # 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学习第十九课——类的装饰器的更多相关文章
- Python学习第十五课——类的基本思想(实例化对象,类对象)
类的基本思想 类:把一类事物的相同的特征和动作整合到一起就是类类是一个抽象的概念 对象:就是基于类而创建的一个具体的事物(具体存在的)也是特征和动作整合到一块 对象写法 # 对象写法 def scho ...
- Python学习第十五篇——类继承和类实例化
学习Python类时,我们明白了类的本质,以及所谓的面向对象编程思想强调的对事物本身的属性,我们对某一类事物进行描述——采用了很多方法,这些方法描述了类的属性(比如猫科动物的眼睛,四肢,是否哺乳类等等 ...
- python 学习笔记十九 django深入学习四 cookie,session
缓存 一个动态网站的基本权衡点就是,它是动态的. 每次用户请求一个页面,Web服务器将进行所有涵盖数据库查询到模版渲染到业务逻辑的请求,用来创建浏览者需要的页面.当程序访问量大时,耗时必然会更加明显, ...
- Python学习第十六课——静态属性(property, classmethod, staticmethod)
计算所居住房子的面积 普通写法 class Room: def __init__(self,name,owner,width,length,heigh): self.name=name self.ow ...
- Python学习(三):迭代器、生成器、装饰器、递归、算法、正则
1.迭代器 迭代器是访问集合的一种方式,迭代对象从集合的第一个元素开始访问,直到元素被访问结束,迭代器只能往前不能后退,最大的优点是不要求事先准备好整个迭代过程中的元素,这个特点使得它特别适合用于遍历 ...
- python学习之类和实例的属性;装饰器@property
无论是类还是实例,一切皆是对象. Python是强动态语言,和java在这点上有所不同. class Ab(): a = 666 # 定义类对象Ab,自带属性a,值为666 # 使用Ab.__dict ...
- Python学习笔记之生成器、迭代器和装饰器
这篇文章主要介绍 Python 中几个常用的高级特性,用好这几个特性可以让自己的代码更加 Pythonnic 哦 1.生成器 什么是生成器呢?简单来说,在 Python 中一边循环一边计算的机制称为 ...
- python学习笔记四 迭代器,生成器,装饰器(基础篇)
迭代器 __iter__方法返回一个迭代器,它是具有__next__方法的对象.在调用__next__方法时,迭代器会返回它的下一个值,若__next__方法调用迭代器 没有值返回,就会引发一个Sto ...
- python 学习2:生成器,迭代器,装饰器
1.生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万 个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那 ...
随机推荐
- nginx配置 yii2 URL重写规则 SSI配置使shtml
location / { // 加上红色部分 重写url try_files $uri $uri/ /index.php?$args; if (!-e $request_filename){ rewr ...
- jmeter实现对Oracle数据库的操作
实现目的 有时候,根据业务需要,可能需要直接对数据库进行性能测试,此时可利用jmeter对Oracle.MySQL等数据库进行相关测试. 脚本实现 添加JDBC Connection Configur ...
- docker容器虚拟化网络
linux内核支持六种名称空间 1.主机名和域名 -------> UTS 2.用户 --------> User 3.文件挂载系统 -------> mount 4. ...
- Python中的参数解包:`*`表达式和 `**`表达式
目录 1.参数解包:方法调用中的*表达式和**表达式 2.参数解包:方法定义中的*表达式和**表达式 3.在元组,列表,集合和字典中解包 4.Extended Unpacking:赋值表达式左边的*表 ...
- GitBook的账号注册和安装使用(一)
1.GitBook账号注册 GitBook原注册地址 https://legacy.gitbook.com/join 改为: https://www.gitbook.com/ (1)注册账号:htt ...
- ‘\0’的ASCII码
1.'\0'的ASCII码为0 2.用串口发送字符串时,可以通过'\0'判断字符串是否结束,但发送数字数组的时候不能通过'\0'判断数组是否结束,因为数字0与'\0'的ASCII码值相同.
- The Preliminary Contest for ICPC Asia Xuzhou 2019 G Colorful String(回文自动机+dfs)
这题建立一棵回文树,然后用dfs搜索答案,但是有一点需要注意,就是打vis的标记时,如果标记为1,那么在好几个节点都对同一个字符i打过标记,此时的搜索从字符i点回溯,回到它的父亲节点,搜索其它的字符, ...
- 获取度量数据:创建服务账户获取访问token
kubectl create clusterrolebinding kubelet-api-test --clusterrole=system:kubelet-api-admin --servicea ...
- 【PAT甲级】1097 Deduplication on a Linked List (25 分)
题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点 ...
- ubuntu 18.04中nginx不能访问软链接目录中的内容
解决办法:以root权限运行nginx,即修改/etc/nginx/nginx.conf中的user www-data为root