Osama Dar
Osama Dar

Reputation: 454

How to specify return type of a function when returning multiple values?

In Python, we can specify the return type of function as

def foo() -> int:
    some_int = get_some_int()
    return some_int

How to specify return types when returning multiple values? For example, how would one specify return types for the following function?

def bar(): # -> ?
    some_float = get_some_float()
    some_int = get_some_int()
    return some_float,some_int

Upvotes: 6

Views: 2018

Answers (1)

Chris Doyle
Chris Doyle

Reputation: 11992

You are only returning one type whcih is a tuple which then contains your float and your int. but the function its self returns a tuple. You can annotate this with type hint like.

from typing import Tuple

def bar() -> Tuple[float, int]:
    some_float = get_some_float()
    some_int = get_some_int()
    return some_float,some_int

Upvotes: 10

Related Questions