019第11章 TESTING YOUR CODE_續1__name__方法
💛20230119晨讀感言:
💔今天卡在 `__name__` 這個方法了,因為 `__name__` 的內容明明就是字串 `__main__` , 可是書上 還是 將之 作為 if 的判斷式,即 if `__name__` == `__main__` , 這不是永遠都是 True 嗎? 難道 還有 其它的方法 讓 上面的判斷式 為 False ?
💔上面的問題,直到我在 《[The Python Standard Library](https://docs.python.org/3/library/index.html)》裡 搜尋到 兩篇文章 《 [`__main__`]— [Top-level code environment](https://docs.python.org/3/library/__main__.html?highlight=__name__)》、《 `__name__` — [Import-related module attributes](https://docs.python.org/3/reference/import.html?highlight=__name__#name__)》後,才稍稍解答我的疑問,換句話說,看完解說後,還不是很理解,還需要消化一下。
💔真心覺得,《[The Python Standard Library]》確實好用。
💛晨讀摘要:
💚212 We’re going to run this file directly, but it’s important to note that many testing frameworks import your test files before running them. When a file is imported, the interpreter executes the file as it’s being imported. The if block at 4 looks at a special variable, `__name__` , which is set when the program is executed. If this file is being run as the main program, the value of `__name__` is set to `__main__`. In this case, we call unittest.main(), which runs the test case. ==When a testing framework imports this file, the value of `__name__` won’t be `__main__` and this block will not be executed==.
from :《python crash course》