Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

python - __init__.py的作用是什么?(What is __init__.py for?)

Python源目录中的__init__.py是什么?

  ask by Mat translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It used to be a required part of a package ( old, pre-3.3 "regular package" , not newer 3.3+ "namespace package" ).

(它曾经是软件包的必需部分( 旧的3.3之前的“常规软件包” ,而不是新的3.3+“命名空间软件包” )。)

Here's the documentation.

(这是文档。)

Python defines two types of packages, regular packages and namespace packages.

(Python定义了两种类型的程序包,常规程序包和名称空间程序包。)

Regular packages are traditional packages as they existed in Python 3.2 and earlier.

(常规软件包是Python 3.2及更早版本中存在的传统软件包。)

A regular package is typically implemented as a directory containing an __init__.py file.

(常规软件包通常实现为包含__init__.py文件的目录。)

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.

(导入常规程序包时,将隐式执行此__init__.py文件,并将其定义的对象绑定到程序包名称空间中的名称。)

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.

(__init__.py文件可以包含任何其他模块可以包含的相同Python代码,并且在导入时Python将向该模块添加一些其他属性。)

But just click the link, it contains an example, more information, and an explanation of namespace packages, the kind of packages without __init__.py .

(但是只需单击链接,它包含一个示例,更多信息以及名称空间包的说明,即不带__init__.py的包的类型。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...