__init__.py 是干什么用的?

What is __init__.py for?(__init__.py 是干什么用的?)
本文介绍了__init__.py 是干什么用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

What is __init__.py for in a Python source directory?

推荐答案

它曾经是包的必需部分(旧的,3.3 之前的常规包",而不是 较新的 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 定义了两种类型的包,常规包和命名空间包.常规包是 Python 3.2 及更早版本中存在的传统包.常规包通常实现为包含 __init__.py 文件的目录.当一个常规包被导入时,这个 __init__.py 文件被隐式执行,并且它定义的对象被绑定到包的命名空间中的名称.__init__.py 文件可以包含任何其他模块可以包含的相同 Python 代码,并且 Python 会在模块被导入时添加一些额外的属性.

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.

但是只需点击链接,它包含一个示例,更多信息,以及命名空间包的解释,没有__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.

这篇关于__init__.py 是干什么用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)