资讯专栏INFORMATION COLUMN

python装饰器、描述符模拟源码实现

Luosunce / 1397人阅读

摘要:如果对象定义和,则它被认为是数据描述符。属性查找优先级为类属性数据描述符实例属性非数据描述符默认为内部是用实现的,这里用模拟实现功能代码参考官方文档原理代码实现原理代码实现原理参考资料,知乎如何理解装饰器,

概要

本人python理论知识远达不到传授级别,写文章主要目的是自我总结,并不能照顾所有人,请见谅,文章结尾贴有相关链接可以作为补充

全文分为三个部分装饰器理论知识、装饰器应用、装饰器延申

装饰理基础:无参装饰器、有参装饰器、functiontools、装饰器链

装饰器进阶:property、staticmethod、classmethod源码分析(python代码实现)


装饰器基础 无参装饰器

</>复制代码

  1. """
  2. 假定有一个需求是:打印程序函数运行顺序
  3. 此案例打印的结果为:
  4. foo1 function is starting
  5. foo2 function is starting
  6. """
  7. from functools import wraps
  8. def NoParamDec(func):
  9. #函数在被装饰器装时后,其函数属性也会改变,wraps作用就是保证被装饰函数属性不变
  10. @wraps(func)
  11. def warpper(*args, **kwargs):
  12. print("{} function is starting".format(func.__name__))
  13. return func(*args, **kwargs)
  14. return warpper
  15. #python黑魔法省略了NoParamDec=NoParamDec(foo1)
  16. @NoParamDec
  17. def foo1():
  18. foo2()
  19. @NoParamDec
  20. def foo2():
  21. pass
  22. if __name__ == "__main__":
  23. foo1()
有参装饰器

</>复制代码

  1. """
  2. 假定有一个需求是:检查函数参数的类型,只允许匹配正确的函数通过程序
  3. 此案例打印结果为:
  4. ("a", "b", "c")
  5. -----------------------分割线------------------------
  6. ERROS!!!!b must be
  7. ERROS!!!!c must be
  8. ("a", 2, ["b", "d"])
  9. """
  10. from functools import wraps
  11. from inspect import signature
  12. def typeAssert(*args, **kwargs):
  13. deco_args = args
  14. deco_kwargs = kwargs
  15. def factor(func):
  16. #python标准模块类,可以用来检查函数参数类型,只允许特定类型通过
  17. sig = signature(func)
  18. #将函数形式参数和规定类型进行绑定
  19. check_bind_args = sig.bind_partial(*deco_args, **deco_kwargs).arguments
  20. @wraps(func)
  21. def wrapper(*args, **kwargs):
  22. #将实际参数值和形式参数进行绑定
  23. wrapper_bind_args = sig.bind(*args, **kwargs).arguments.items()
  24. for name, obj in wrapper_bind_args:
  25. #遍历判断是否实际参数值是规定参数的实例
  26. if not isinstance(obj, check_bind_args[name]):
  27. try:
  28. raise TypeError("ERROS!!!!{arg} must be {obj} ".format(**{"arg": name, "obj": check_bind_args[name]}))
  29. except Exception as e:
  30. print(e)
  31. return func(*args, **kwargs)
  32. return wrapper
  33. return factor
  34. @typeAssert(str, str, str)
  35. def inspect_type(a, b, c):
  36. return (a, b, c)
  37. if __name__ == "__main__":
  38. print(inspect_type("a", "b", "c"))
  39. print("{:-^50}".format("分割线"))
  40. print(inspect_type("a", 2, ["b", "d"]))
装饰器链

</>复制代码

  1. """
  2. 假定有一个需求是:
  3. 输入类似代码:
  4. @makebold
  5. @makeitalic
  6. def say():
  7. return "Hello"
  8. 输出:
  9. Hello
  10. """
  11. from functools import wraps
  12. def html_deco(tag):
  13. def decorator(fn):
  14. @wraps(fn)
  15. def wrapped(*args, **kwargs):
  16. return "<{tag}>{fn_result}<{tag}>".format(**{"tag": tag, "fn_result": fn(*args, **kwargs)})
  17. return wrapped
  18. return decorator
  19. @html_deco("b")
  20. @html_deco("i")
  21. def greet(whom=""):
  22. # 等价于 geet=html_deco("b")(html_deco("i)(geet))
  23. return "Hello" + (" " + whom) if whom else ""
  24. if __name__ == "__main__":
  25. print(greet("world")) # -> Hello world
装饰器进阶 property 原理

通常,描述符是具有“绑定行为”的对象属性,其属性访问已经被描述符协议中的方法覆盖。这些方法是__get__()、__set__()和__delete__()。如果一个对象定义这些方法中的任何一个,它被称为一个描述符。如果对象定义__get__()和__set__(),则它被认为是数据描述符。仅定义__get__()的描述器称为非数据描述符(它们通常用于方法,但是其他用途也是可能的)。

属性查找优先级为:

类属性

数据描述符

实例属性

非数据描述符

默认为__getattr__()

</>复制代码

  1. class Property(object):
  2. """
  3. 内部property是用c实现的,这里用python模拟实现property功能
  4. 代码参考官方doc文档
  5. """
  6. def __init__(self, fget=None, fset=None, fdel=None, doc=None):
  7. self.fget = fget
  8. self.fset = fset
  9. self.fdel = fdel
  10. self.__doc__ = doc
  11. def __get__(self, obj, objtype=None):
  12. if obj is None:
  13. return self
  14. if self.fget is None:
  15. raise (AttributeError, "unreadable attribute")
  16. print("self={},obj={},objtype={}".format(self,obj,objtype))
  17. return self.fget(obj)
  18. def __set__(self, obj, value):
  19. if self.fset is None:
  20. raise (AttributeError, "can"t set attribute")
  21. self.fset(obj, value)
  22. def __delete__(self, obj):
  23. if self.fdel is None:
  24. raise (AttributeError, "can"t delete attribute")
  25. self.fdel(obj)
  26. def getter(self, fget):
  27. return type(self)(fget, self.fset, self.fdel, self.__doc__)
  28. def setter(self, fset):
  29. return type(self)(self.fget, fset, self.fdel, self.__doc__)
  30. def deleter(self, fdel):
  31. return type(self)(self.fget, self.fset, fdel, self.__doc__)
  32. class Student( object ):
  33. @Property
  34. def score( self ):
  35. return self._score
  36. @score.setter
  37. def score( self, val ):
  38. if not isinstance( val, int ):
  39. raise ValueError( "score must be an integer!" )
  40. if val > 100 or val < 0:
  41. raise ValueError( "score must between 0 ~ 100!" )
  42. self._score = val
  43. if __name__ == "__main__":
  44. s = Student()
  45. s.score = 60
  46. 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).

</>复制代码

  1. class StaticMethod(object):
  2. "python代码实现staticmethod原理"
  3. def __init__(self, f):
  4. self.f = f
  5. def __get__(self, obj, objtype=None):
  6. return self.f
  7. class E(object):
  8. #StaticMethod=StaticMethod(f)
  9. @StaticMethod
  10. def f( x):
  11. return x
  12. if __name__ == "__main__":
  13. 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).

</>复制代码

  1. class ClassMethod(object):
  2. "python代码实现classmethod原理"
  3. def __init__(self, f):
  4. self.f = f
  5. def __get__(self, obj, klass=None):
  6. if klass is None:
  7. klass = type(obj)
  8. def newfunc(*args):
  9. return self.f(klass, *args)
  10. return newfunc
  11. class E(object):
  12. #ClassMethod=ClassMethod(f)
  13. @ClassMethod
  14. def f(cls,x):
  15. return x
  16. if __name__ == "__main__":
  17. print(E().f("classMethod Test"))
参考资料

1, statckoverflow: how to make a chain of decorators

2, python doc:how to descriptor

3,知乎:如何理解装饰器

4, difference-between-staticmethod-and-classmethod-in-python

5,meaning-of-classmethod-and-staticmethod-for-beginner

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/41438.html

相关文章

  • Bottle框架中的装饰类和描述应用

    摘要:最近在阅读微型框架的源码,发现了中有一个既是装饰器类又是描述符的有趣实现。所以第三版的代码可以这样写第三版的代码没有使用装饰器,而是使用了描述符这个技巧。更大的问题来自如何将描述符与装饰器结合起来,因为是一个类而不是方法。 最近在阅读Python微型Web框架Bottle的源码,发现了Bottle中有一个既是装饰器类又是描述符的有趣实现。刚好这两个点是Python比较的难理解,又混合在...

    Panda 评论0 收藏0
  • 【用故事解读 MobX 源码(四)】装饰 和 Enhancer

    摘要:所以这是一篇插队的文章,用于去理解中的装饰器和概念。因此,该的作用就是根据入参返回具体的描述符。其次局部来看,装饰器具体应用表达式是,其函数签名和是一模一样。等装饰器语法,是和直接使用是等效等价的。 ================前言=================== 初衷:以系列故事的方式展现 MobX 源码逻辑,尽可能以易懂的方式讲解源码; 本系列文章: 《【用故事解...

    maybe_009 评论0 收藏0
  • JS 装饰,一篇就够

    摘要:的装饰器中的同样借鉴了这个语法糖,不过依赖于的方法。等同于也就是说,装饰器是一个对类进行处理的函数。别名或装饰器在控制台显示一条警告,表示该方法将废除。有了装饰器,就可以改写上面的代码。 更多文章,请在Github blog查看 在 ES6 中增加了对类对象的相关定义和操作(比如 class 和 extends ),这就使得我们在多个不同类之间共享或者扩展一些方法或者行为的时候,变得并...

    learning 评论0 收藏0
  • Python - 装饰使用过程中的误区

    摘要:然而,当我们想要获取被包装函数的参数或源代码时,同样不能得到我们想要的结果。这是在中的,版本已被修复,参考。如同上面我们所看到的,可以帮我们解决和的问题,但对于获取函数的参数或源代码则束手无策。 装饰器基本概念 大家都知道装饰器是一个很著名的设计模式,经常被用于 AOP (面向切面编程)的场景,较为经典的有插入日志,性能测试,事务处理,Web权限校验, Cache等。 Python...

    1fe1se 评论0 收藏0
  • 深入理解Python中的ThreadLocal变量(下)

    摘要:具体怎么实现的呢,思想其实特别简单,我们在深入理解中的变量上一文的最后有提起过,就是创建一个全局字典,然后将线程或者协程标识符作为,相应线程或协程的局部数据作为。 在上篇我们看到了 ThreadLocal 变量的简单使用,中篇对python中 ThreadLocal 的实现进行了分析,但故事还没有结束。本篇我们一起来看下Werkzeug中ThreadLocal的设计。 Werkzeug...

    dadong 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<