11
5

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.

Pythonの空配列とNoneの同時判定

Last updated at Posted at 2018-11-14

空配列とNoneの判定

Pythonで
Objectが空配列かNoneの場合、後続の処理をしたくないことがあるとする。

素直に書くと
こんな感じ

a = []

# Noneを弾く
if a is None:
  return
# 空配列を弾く
if len(a) == 0:
  return

do_process()

今日下記で書けることを知った。

a = []

if not a:
  return

do_process()

参考URL:

Why does “[] == False” evaluate to False when “if not []” succeeds?

所感

もちろん判定一回の後者の方がよいが、これを数ヶ月後まで覚えておける自信がない。

11
5
1

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
11
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?