0
0

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 5 years have passed since last update.

空のリストのTypeHint

Posted at

Pythonで空のリストを作成する時に、Typeをどのように書くか考えていました。例えば、

from typing import List, Any

x: list = []
x: List[Any] = []
x: List[str] = []
x: List[int] = []

といくつかのパターンがありそうです。そこで、PEP8 484 Type Commentを見ると、

x = []                # type: List[Employee]
x, y, z = [], [], []  # type: List[int], List[int], List[str]
x, y, z = [], [], []  # type: (List[int], List[int], List[str])
a, b, *c = range(5)   # type: float, float, List[float]
x = [1, 2]            # type: List[int]

と記載がありました。空のリストは、List[str]List[int]と明示的に表現するのが適切なようです。配列に何を入れる想定で作成しているかを明示的に宣言するようなイメージみたいです。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?