爲什麼python設計時要有__init__.py才視爲包

this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.

大致的理解是:
可能會有一些文件夾,比如有個專門放字符串的文件夾叫string,如果所有目錄都視爲包的話,import string可能就會覆蓋python的string,而產生錯誤.而只有帶init.py的才視爲包的話,專門放字符串的文件夾string裏肯定不會放一個init.py,就不會覆蓋python的string.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章