Reputation: 778
I have a function:
def foo(a, b):
return [a, b]
I want to add type hinting for return value, as you can see my function can return [srt, int]
or [str, str]
or [int, int]
etc. for example.
I tried:
def foo(a, b) -> List[str, int]:
return [a, b]
but it not working.
How can I specify possible value in list that my function can return?
Upvotes: 0
Views: 623
Reputation: 15384
You can use Union
. Specifically, Union[X, Y]
means either X or Y.
from typing import List, Union
def foo(a, b) -> List[Union[str, int]]:
return [a, b]
If your function can return a list containing any element, then use Any
(special type indicating an unconstrained type):
from typing import Any, List
def foo(a, b) -> List[Any]:
return [a, b]
Upvotes: 3