wenwen
wenwen

探索網路世界和進入區塊鏈的過程或許很平凡,但我就是想分享給大家,若能幫助到有需要幫助的人,那就更好了。 有幫到你的話,記得按下方拍手圖,不需花你任何金錢,免費註冊Liker ID,就可對我化讚為賞:https://button.like.co/willie5068

019第11章 TESTING YOUR CODE_續1__name__方法

真心覺得,《[The Python Standard Library]》確實好用。

💛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》

CC BY-NC-ND 2.0 版权声明

喜欢我的文章吗?
别忘了给点支持与赞赏,让我知道创作的路上有你陪伴。

加载中…
加载中…

发布评论