zoukankan      html  css  js  c++  java
  • python进阶之装饰器之3利用装饰器强制函数上的类型检查

    # 让装饰器对函数的参数进行强制执行检查
    from inspect import signature
    from functools import wraps
    
    #定义装饰器@typeassert
    def typeassert(*ty_args, **ty_kwargs):
        def decorate(func):
            # If in optimized mode, disable type checking
            if not __debug__:
                return func
    
            # Map function argument names to supplied types
            sig = signature(func)
            bound_types = sig.bind_partial(*ty_args, **ty_kwargs).arguments
    
            @wraps(func)
            def wrapper(*args, **kwargs):
                bound_values = sig.bind(*args, **kwargs)
                # Enforce type assertions across supplied arguments
                for name, value in bound_values.arguments.items():
                    if name in bound_types:
                        if not isinstance(value, bound_types[name]):
                            raise TypeError(
                                'Argument {} must be {}'.format(name, bound_types[name])
                                )
                return func(*args, **kwargs)
            return wrapper
        return decorate
    
    @typeassert(int, z=int)
    def spam(x, y, z=42):
        print(x, y, z)
    
    print(spam(1, 2, 3))
    # 1 2 3
    print(spam(1, 'hello', 3))
    # 1 hello 3
    print(spam(1, 'hello', 'world'))
    # ypeError: Argument z must be <class 'int'>
    仙衣眠云碧岚袍,一襟潇洒,两袖飘飘。玉墨舒心春酝瓢,行也逍遥,坐也逍遥。
  • 相关阅读:
    数据类面试题
    java二进制文件复制
    String源码
    集合类题目
    OBJ-C
    java文件(文件夹)操作
    java中输入方式Scanner和BufferedReader
    二次分发举例
    Eclipse常用快捷键
    c#获取新浪微博登录cookie
  • 原文地址:https://www.cnblogs.com/max520liuhu/p/9350516.html
Copyright © 2011-2022 走看看