【Py】函数参数\返回值多类型注解以及参数类型注解+默认值以及多返回值注解

参数\返回值多类型注解

单类型:

def data_process(payload_data: dict) -> dict:
    ...

多类型:

from typing import Union

def data_process(payload_data: Union[dict, str]) -> Union[dict, str]:
    ...

参考:https://stackoverflow.com/a/33945518/7151777

参数类型注解+默认值

变量类型注解

def data_process(payload_data: dict) -> dict:
    ...

变量默认值

def data_process(payload_data = {}) -> dict:
    ...

变量类型注解+默认值:

def data_process(payload_data: dict = {}) -> dict:
    ...

参考:https://stackoverflow.com/q/38727520/7151777

多返回值注解

单返回值

def data_process(payload_data = {}) -> dict:
    ...
    return a

多返回值(Python 3.8- )

from typing import Tuple

def data_process(payload_data = {}) -> Tuple[dict, str]:
    ...
    return a, b

多返回值(Python 3.9+ )

def data_process(payload_data = {}) -> tuple[dict, str]:
    ...
    return a, b

参考:https://stackoverflow.com/a/40181387/7151777

你可能感兴趣的:(Python,python,类型注解,annotation,typehint,类型提示)