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

int()の例外がTypeErrorになるケース、ValueErrorになるケース

Last updated at Posted at 2021-05-30

int()でstring型をint型に変換したときの例外が、TypeErrorになるケース、ValueErrorになるケースを忘れがちなので、メモとして残します。

正常動作

int("100")
# 以下もオーバーロードなので正常動作
int(100)
int(1.6) # 1 になる
int(True) # 1 になる

TypeError

  • 型が違う
  • 引数の数がオーバー
int(["100", "101"])
int(None)
int("100", 10, "100")

ValueError

  • 型は合っているが、変換できない
int("")
int("abc")

以下を参考にしました。
https://stackoverflow.com/questions/48343387/valueerror-and-typeerror-in-python

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?