问题
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