問題描述
Python 源目錄中的 __init__.py
是什么?
What is __init__.py
for in a Python source directory?
推薦答案
它曾經(jīng)是包的必需部分(舊的,3.3 之前的常規(guī)包",而不是 較新的 3.3+ "命名空間包").
It used to be a required part of a package (old, pre-3.3 "regular package", not newer 3.3+ "namespace package").
這是文檔.
Python 定義了兩種類型的包,常規(guī)包和命名空間包.常規(guī)包是 Python 3.2 及更早版本中存在的傳統(tǒng)包.常規(guī)包通常實(shí)現(xiàn)為包含 __init__.py
文件的目錄.當(dāng)一個常規(guī)包被導(dǎo)入時,這個 __init__.py
文件被隱式執(zhí)行,并且它定義的對象被綁定到包的命名空間中的名稱.__init__.py
文件可以包含任何其他模塊可以包含的相同 Python 代碼,并且 Python 會在模塊被導(dǎo)入時添加一些額外的屬性.
Python defines two types of packages, regular packages and namespace packages. Regular packages are traditional packages as they existed in Python 3.2 and earlier. A regular package is typically implemented as a directory containing an
__init__.py
file. When a regular package is imported, this__init__.py
file is implicitly executed, and the objects it defines are bound to names in the package’s namespace. The__init__.py
file can contain the same Python code that any other module can contain, and Python will add some additional attributes to the module when it is imported.
但是只需點(diǎn)擊鏈接,它包含一個示例,更多信息,以及命名空間包的解釋,沒有__init__.py
的那種包.
But just click the link, it contains an example, more information, and an explanation of namespace packages, the kind of packages without __init__.py
.
這篇關(guān)于__init__.py 是干什么用的?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!