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万 个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那 ...
随机推荐
- python闯关之路二(模块的应用)
1.有如下字符串:n = "路飞学城"(编程题) - 将字符串转换成utf-8的字符编码的字节,再将转换的字节重新转换为utf-8的字符编码的字符串 - 将字符串转换成gbk的字符 ...
- 不起眼,但是足以让你收获的JVM内存案例
今天的这个案例我觉得应该会让你涨姿势吧,不管你对JVM有多熟悉,看到这篇文章,应该还是会有点小惊讶的,不过我觉得这个案例我分享出来,是想表达不管多么奇怪的现象请一定要追究下去,会让你慢慢变得强大起来, ...
- WEB前后端约定接口
- opencv python:边缘保留滤波(EPF)
EPF:E边缘,P保留,F滤波 import cv2 as cv import numpy as np def bi_demo(image): # bilateralFilter(src, d, si ...
- JenKins docker 集群
//tag 桉树有时间来搞 **阿斯蒂 啊 阿斯蒂
- scrapy import CrawlSpider 报错
from scrapy.spider import CrawlSpider 报错 import module CrawlSpider error 看了下以前一直用的scrapy0.14.1 使用的是B ...
- mcast_set_ttl函数
#include <errno.h> #include <net/if.h> #include <sys/socket.h> #include <netine ...
- Linux - Zip乱码问题
1. 可以通过解压后使用convmv来解决文件名乱码问题,通过iconv来解决文件内容的乱码问题 2. 用unar命令
- 在Linux系统中使用ntfs、fat32格式的存储设备
在Linux系统中使用ntfs.fat32格式的存储设备 我们通常使用的移动硬盘或U盘一般都是ntfs或fat32的文件系统,作为一名运维工程师,经常会遇到把移动硬盘或者U盘上的内容拷贝的Linu ...
- django之路由分组,路由分发,FBV,CBV,ORM框架
今日的内容: a. 路由系统 1. 创建app 2. 路由的分组 3. 路由的分发 - 正则表达式匹配 b. django的orm(模型model) 1. 创建模型的步骤 2. orm基本的增删改查 ...