LoginSignup
12

More than 5 years have passed since last update.

[python]__debug__についてのメモ

Posted at

メモ

*ただのメモです

__debug__

これは組み込み定数ひとつで、python実行の際に-OをつけるとFalse、じゃなければTrueとなる定数。(これ、 大文字のオー,ゼロじゃない)

debug_print.py
def debug_print(s):
    if not __debug__:
        print(s)

こんな感じで作ると、

test.output
$ python -c '(__import__)("debug_print").debug_print(__debug__)'
$ python -O -c '(__import__)("debug_print").debug_print(__debug__)'
False

のように、-Oオプションをつけた時だけprintされます。

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
12