当前位置 博文首页 > Python-typing: 类型标注与支持 Any类型详解

    Python-typing: 类型标注与支持 Any类型详解

    作者:G____G 时间:2021-06-08 18:29

    Any docs

    Any 是一种特殊的类型。

    静态类型检查器将所有类型视为与 Any 兼容,反之亦然, Any 也与所有类型相兼容。

    这意味着可对类型为 Any 的值执行任何操作或方法调用,并将其赋值给任何变量:

    from typing import Any
    a = None    # type: Any
    a = []      # OK
    a = 2       # OK
    s = ''      # type: str
    s = a       # OK
    def foo(item: Any) -> int:
        # Typechecks; 'item' could be any type,
        # and that type might have a 'bar' method
        item.bar()
        ...
    

    需要注意的是,将 Any 类型的值赋值给另一个更具体的类型时,Python不会执行类型检查。例如,当把 a 赋值给 s 时,即使 s 被声明为 str 类型,在运行时接收到的是 int 值,静态类型检查器也不会报错。

    此外,所有返回值无类型或形参无类型的函数将隐式地默认使用 Any 类型:

    def legacy_parser(text):
        ...
        return data
    # A static type checker will treat the above
    # as having the same signature as:
    def legacy_parser(text: Any) -> Any:
        ...
        return data
    

    当需要混用动态类型和静态类型的代码时,上述行为可以让 Any 被用作 应急出口 。

    Any 和 object 的行为对比。

    与 Any 相似,所有的类型都是 object 的子类型。然而不同于 Any,反之并不成立: object 不是 其他所有类型的子类型。

    这意味着当一个值的类型是 object 的时候,类型检查器会拒绝对它的几乎所有的操作。把它赋值给一个指定了类型的变量(或者当作返回值)是一个类型错误。

    比如说:

    def hash_a(item: object) -> int:
        # Fails; an object does not have a 'magic' method.
        item.magic()
        ...
    def hash_b(item: Any) -> int:
        # Typechecks
        item.magic()
        ...
    # Typechecks, since ints and strs are subclasses of object
    hash_a(42)
    hash_a("foo")
    # Typechecks, since Any is compatible with all types
    hash_b(42)
    hash_b("foo")
    

    使用 object 示意一个值可以类型安全地兼容任何类型。使用 Any 示意一个值地类型是动态定义的。

    补充:python3.5 typing — 类型标注支持

    函数接受并返回一个字符串,注释像下面这样:

    def greeting(name: str) -> str:
        return 'Hello' + name

    在函数 greeting 中,参数 name 预期是 str 类型,并且返回 str 类型。子类型允许作为参数。

    1.1. 类型别名

    型别名通过将类型分配给别名来定义。在这个例子中, Vector 和 List[float] 将被视为可互换的同义词:

    from typing import List
    Vector = List[float]
    def scale(scalar: float, vector: Vector) -> Vector:
        return [scalar * num for num in vector]
    # typechecks; a list of floats qualifies as a Vector.
    new_vector = scale(2.0, [1.0, -4.2, 5.4])
    

    类型别名可用于简化复杂类型签名。

    例如:

    from typing import Dict, Tuple, List
    ConnectionOptions = Dict[str, str]
    Address = Tuple[str, int]
    Server = Tuple[Address, ConnectionOptions]
    def broadcast_message(message: str, servers: List[Server]) -> None:
        ...
    # The static type checker will treat the previous type signature as
    # being exactly equivalent to this one.
    def broadcast_message(
            message: str,
            servers: List[Tuple[Tuple[str, int], Dict[str, str]]]) -> None:
        ...
    

    请注意,None 作为类型提示是一种特殊情况,并且由 type(None) 取代。

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持站长博客。如有错误或未考虑完全的地方,望不吝赐教。

    js