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

wenwen
·
·
IPFS
·
真心覺得,《[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

Like my work? Don't forget to support and clap, let me know that you are with me on the road of creation. Keep this enthusiasm together!

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

033第12章 A SHIP THAT FIRES BULLETS_續10_pygame.sprite.Group class

032第12章 A SHIP THAT FIRES BULLETS_續9_pygame.Rect() class

031第12章 A SHIP THAT FIRES BULLETS_續8_super().init()