3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pythonで複数の値を返り値に持つときの型指定

Last updated at Posted at 2022-07-18

やりたいこと

以下のような関数があったとき、
返り値の型指定はどうすれば良いのか?

def test():
  return 1, "a", {3, 5}

解決法

from typing import Tuple

def test() -> Tuple[int, str, set]:
  return 1, "a", {3, 5}

setの型までちゃんと指定したい場合

from typing import Tuple, Set

def test() -> Tuple[int, str, Set[int]]:
  return 1, "a", {3, 5}

解説

python関数で複数の返り値がある場合、原則としてtupleとして返される。

from typing import Tuple, Union
def test() -> Tuple[Union[int, str], int, set]:
  return "1", 2, {1,64, 4}
print(test())  # ("1", 2, {1,64, 4})
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?