如果我在 bash 上运行python3 -O:
(base) [xyx@xyz python_utils]$ python3 -O Python 3.6.4 (default, Mar 28 2018, 11:00:11) [GCC 6.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> if __debug__:print("hello")
...
>>> exit()
Run Code Online (Sandbox Code Playgroud)
我看到__debug__变量设置为0,因为未到达 `print("hello") 调用。但是如果我在 python 文件中编写上面相同的行并以通常的方式运行它,例如
(base) [xyx@xyz python_utils]$ python3 -O Python 3.6.4 (default, Mar 28 2018, 11:00:11) [GCC 6.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> if __debug__:print("hello")
...
>>> exit()
Run Code Online (Sandbox Code Playgroud)
然后我们看到文本"hello",这意味着 …