码迷,mamicode.com
首页 > 编程语言 > 详细

python注解

时间:2015-05-26 18:58:55      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:python   注解   装饰器   

http://blog.csdn.net/pipisorry/article/details/46009031

注解是 Python 3 中的一个新元素。下面类型检查机制将会以一个装饰器的形式实现。

[装饰器]

类型检查器的一个例子

def gcd(a, b):
    '''Return the greatest common divisor of a and b.'''
    a = abs(a)
    b = abs(b)
    if a < b:
        a, b = b, a
    while b != 0:
        a, b = b, a % b
    return a
在上面的示例中,参数 a 和 b 以及返回值应该是 int 类型的。预期的类型将会以函数注解的形式来表达

将上面函数第一行改为defgcd(a:int, b:int)-> int:
使用“gcd.__annotations__”可以获得一个包含注解的字典:

>>> gcd.__annotations__
{'return': <class 'int'>, 'b': <class 'int'>, 'a': <class 'int'>}
Note:返回值的注解存储在键“return”下。

检查返回值类型

返回值注解存储在字典“__annotations__”中的“return”键下。我们将使用这个值来检查返回值(假设注解存在)。我们将参数传递给原始函数,如果存在注解,我们将通过注解中的值来验证其类型:

1
2
3
4
5
6
7
8
def typecheck(f):
    defwrapper(*args,**kwargs):
        result=f(*args,**kwargs)
        return_type=f.__annotations__.get(‘return‘,None)
        ifreturn_typeandnotisinstance(result, return_type):
            raiseRuntimeError("{} should return {}".format(f.__name__, return_type.__name__))
        returnresult
    returnwrapper


检查参数类型

函数的参数存在于关联代码对象的“co_varnames”属性中,在我们的例子中是“gcd.__code__.co_varnames”。元组包含了所有局部变量的名称,并且该元组以参数开始.

参数数量存储在“co_nlocals”中。

我们需要遍历包括索引在内的所有变量,并从参数“args”中获取参数值,最后对其进行类型检查。

为了对关键字参数进行类型检查,我们需要遍历参数kwargs。此时的类型检查几乎与第一个循环中相同:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def typecheck(f):
    defwrapper(*args,**kwargs):
        fori, argin enumerate(args[:f.__code__.co_nlocals]):
            name=f.__code__.co_varnames[i]
            expected_type=f.__annotations__.get(name,None)
            ifexpected_typeandnotisinstance(arg, expected_type):
                raiseRuntimeError("{} should be of type {}; {} specified".format(name, expected_type.__name__,type(arg).__name__))
        forname, arginkwargs.items():
            expected_type=f.__annotations__.get(name,None)
            ifexpected_typeandnotisinstance(arg, expected_type):
                raiseRuntimeError("{} should be of type {}; {} specified".format(name, expected_type.__name__,type(arg).__name__))
        result=f(*args,**kwargs)
        return_type=f.__annotations__.get(‘return‘,None)
        ifreturn_typeandnotisinstance(result, return_type):
            raiseRuntimeError("{} should return {}".format(f.__name__, return_type.__name__))
        returnresult
    returnwrapper

在上面的循环中,i是数组args中参数的以0起始的索引,arg是包含其值的字符串。可以利用“f.__code__.co_varnames[i]”读取到参数的名称。类型检查代码与返回值类型检查完全一样(包括错误消息的异常)。

将类型检查代码写成一个函数将会使代码更加清晰。为了简化代码,我们修改错误信息,而当返回值是无效的类型时,将会使用到这些错误信息。我们也可以利用 functools 模块中的 wraps 方法,将包装函数的一些属性复制到 wrapper 中(这使得 wrapper 看起来更像原来的函数):

def typecheck(f):
    def do_typecheck(name, arg):
        expected_type = f.__annotations__.get(name, None)
        if expected_type and not isinstance(arg, expected_type):
            raise RuntimeError("{} should be of type {} instead of {}".format(name, expected_type.__name__, type(arg).__name__))

    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        for i, arg in enumerate(args[:f.__code__.co_nlocals]):
            do_typecheck(f.__code__.co_varnames[i], arg)
        for name, arg in kwargs.items():
            do_typecheck(name, arg)

        result = f(*args, **kwargs)

        do_typecheck(‘return‘, result)
        return result
    return wrapper
from:http://blog.csdn.net/pipisorry/article/details/46009031

ref:How to Implement a Type Checker in Python 3

在Python 3中实现类型检查器


python注解

标签:python   注解   装饰器   

原文地址:http://blog.csdn.net/pipisorry/article/details/46009031

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!