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) 取代。

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