概要
本人python理论知识远达不到传授级别,写文章主要目的是自我总结,并不能照顾所有人,请见谅,文章结尾贴有相关链接可以作为补充
全文分为三个部分装饰器理论知识、装饰器应用、装饰器延申
- 装饰理基础:无参装饰器、有参装饰器、functiontools、装饰器链
- 装饰器进阶:property、staticmethod、classmethod源码分析(python代码实现)
装饰器基础
无参装饰器
''' 假定有一个需求是:打印程序函数运行顺序 此案例打印的结果为: foo1 function is starting foo2 function is starting ''' from functools import wraps def NoParamDec(func): #函数在被装饰器装时后,其函数属性也会改变,wraps作用就是保证被装饰函数属性不变 @wraps(func) def warpper(*args, **kwargs): print('{} function is starting'.format(func.__name__)) return func(*args, **kwargs) return warpper #python黑魔法省略了NoParamDec=NoParamDec(foo1) @NoParamDec def foo1(): foo2() @NoParamDec def foo2(): pass if __name__ == "__main__": foo1()
有参装饰器
''' 假定有一个需求是:检查函数参数的类型,只允许匹配正确的函数通过程序 此案例打印结果为: ('a', 'b', 'c') -----------------------分割线------------------------ ERROS!!!!b must be <class 'str'> ERROS!!!!c must be <class 'str'> ('a', 2, ['b', 'd']) ''' from functools import wraps from inspect import signature def typeAssert(*args, **kwargs): deco_args = args deco_kwargs = kwargs def factor(func): #python标准模块类,可以用来检查函数参数类型,只允许特定类型通过 sig = signature(func) #将函数形式参数和规定类型进行绑定 check_bind_args = sig.bind_partial(*deco_args, **deco_kwargs).arguments @wraps(func) def wrapper(*args, **kwargs): #将实际参数值和形式参数进行绑定 wrapper_bind_args = sig.bind(*args, **kwargs).arguments.items() for name, obj in wrapper_bind_args: #遍历判断是否实际参数值是规定参数的实例 if not isinstance(obj, check_bind_args[name]): try: raise TypeError('ERROS!!!!{arg} must be {obj} '.format(**{'arg': name, 'obj': check_bind_args[name]})) except Exception as e: print(e) return func(*args, **kwargs) return wrapper return factor @typeAssert(str, str, str) def inspect_type(a, b, c): return (a, b, c) if __name__ == "__main__": print(inspect_type('a', 'b', 'c')) print('{:-^50}'.format('分割线')) print(inspect_type('a', 2, ['b', 'd']))
装饰器链
''' 假定有一个需求是: 输入类似代码: @makebold @makeitalic def say(): return "Hello" 输出: <b><i>Hello</i></b> ''' from functools import wraps def html_deco(tag): def decorator(fn): @wraps(fn) def wrapped(*args, **kwargs): return '<{tag}>{fn_result}<{tag}>'.format(**{'tag': tag, 'fn_result': fn(*args, **kwargs)}) return wrapped return decorator @html_deco('b') @html_deco('i') def greet(whom=''): # 等价于 geet=html_deco('b')(html_deco('i)(geet)) return 'Hello' + (' ' + whom) if whom else '' if __name__ == "__main__": print(greet('world')) # -> <b><i>Hello world</i></b>
装饰器进阶
property 原理
通常,描述符是具有“绑定行为”的对象属性,其属性访问已经被描述符协议中的方法覆盖。这些方法是__get__()、__set__()和__delete__()。如果一个对象定义这些方法中的任何一个,它被称为一个描述符。如果对象定义__get__()和__set__(),则它被认为是数据描述符。仅定义__get__()的描述器称为非数据描述符(它们通常用于方法,但是其他用途也是可能的)。
属性查找优先级为:
- 类属性
- 数据描述符
- 实例属性
- 非数据描述符
- 默认为__getattr__()
class Property(object): ''' 内部property是用c实现的,这里用python模拟实现property功能 代码参考官方doc文档 ''' def __init__(self, fget=None, fset=None, fdel=None, doc=None): self.fget = fget self.fset = fset self.fdel = fdel self.__doc__ = doc def __get__(self, obj, objtype=None): if obj is None: return self if self.fget is None: raise (AttributeError, "unreadable attribute") print('self={},obj={},objtype={}'.format(self,obj,objtype)) return self.fget(obj) def __set__(self, obj, value): if self.fset is None: raise (AttributeError, "can't set attribute") self.fset(obj, value) def __delete__(self, obj): if self.fdel is None: raise (AttributeError, "can't delete attribute") self.fdel(obj) def getter(self, fget): return type(self)(fget, self.fset, self.fdel, self.__doc__) def setter(self, fset): return type(self)(self.fget, fset, self.fdel, self.__doc__) def deleter(self, fdel): return type(self)(self.fget, self.fset, fdel, self.__doc__) class Student( object ): @Property def score( self ): return self._score @score.setter def score( self, val ): if not isinstance( val, int ): raise ValueError( 'score must be an integer!' ) if val > 100 or val < 0: raise ValueError( 'score must between 0 ~ 100!' ) self._score = val if __name__ == "__main__": s = Student() s.score = 60 s.score
staticmethod 原理
@staticmethod means: when this method is called, we don't pass an instance of the class to it (as we normally do with methods). This means you can put a function inside a class but you can't access the instance of that class (this is useful when your method does not use the instance).
class StaticMethod(object): "python代码实现staticmethod原理" def __init__(self, f): self.f = f def __get__(self, obj, objtype=None): return self.f class E(object): #StaticMethod=StaticMethod(f) @StaticMethod def f( x): return x if __name__ == "__main__": print(E.f('staticMethod Test'))
classmethod
@staticmethod means: when this method is called, we don't pass an instance of the class to it (as we normally do with methods). This means you can put a function inside a class but you can't access the instance of that class (this is useful when your method does not use the instance).
class ClassMethod(object): "python代码实现classmethod原理" def __init__(self, f): self.f = f def __get__(self, obj, klass=None): if klass is None: klass = type(obj) def newfunc(*args): return self.f(klass, *args) return newfunc class E(object): #ClassMethod=ClassMethod(f) @ClassMethod def f(cls,x): return x if __name__ == "__main__": print(E().f('classMethod Test'))
python,装饰器,描述符
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新动态
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]