Python type hints for function returning multiple return values

我们两清 提交于 2020-06-14 06:02:08

问题


How do I write the function declaration using Python type hints for function returning multiple return values?

Is the below syntax allowed?

def greeting(name: str) -> str, List[float], int :

// do something

return a,b,c

回答1:


You can use a typing.Tuple type hint (to specify the type of the content of the tuple, if it is not necessary, the built-in class tuple can be used instead):

from typing import Tuple

def greeting(name: str) -> Tuple[str, List[float], int]:
    # do something
    return a, b, c



回答2:


Multiple return values in python are returned as a tuple, and the type hint for a tuple is not the tuple class, but typing.Tuple.

import typing

def greeting(name: str) -> typing.Tuple[str, List[float], int]:

    # do something

    return a,b,c


来源:https://stackoverflow.com/questions/58101021/python-type-hints-for-function-returning-multiple-return-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!