Python趣谈1-cast

在看aiohttp原码的时候,发现一句代码:

app = cast(Application, app)

有些不解这个cast函数是干什么使的,看了源码吓了一跳:

def cast(typ, val):
    """Cast a value to a type.

    This returns the value unchanged.  To the type checker this
    signals that the return value has the designated type, but at
    runtime we intentionally don't check anything (we want this
    to be as fast as possible).
    """
    return val

大意是:这个函数是为了检查val变量是typ类型的,但是这个cast函数不做检查,直接返回val,那是为什么呢?

哇塞!多此一举!我干嘛要用这个函数!

哈哈,主要是为了给看代码的人知道我这个val是typ类型的,但是为了让程序运行的更快呢,我就不做检查了,看代码的人知道就行!

你可能感兴趣的:(Python-趣谈,Python)