kivk02
kivk02

Reputation: 661

Python type hints for function returning multiple return values

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

Upvotes: 40

Views: 34125

Answers (3)

Djaouad
Djaouad

Reputation: 22776

EDIT: Since Python 3.9 and the acceptance of PEP 585, you should use the built-in tuple class to typehint tuples.


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, List

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

Upvotes: 52

Luke Miles
Luke Miles

Reputation: 1160

Indexing on builtin tuple and list is now supported.

def greeting(name: str) -> tuple[str, list[float], int]:
    pass

Upvotes: 9

kosayoda
kosayoda

Reputation: 398

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

Upvotes: 12

Related Questions