__init__.py的作用是什么? [英] What is __init__.py for?

查看:105
本文介绍了__init__.py的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

它曾经是软件包的必需部分(,而不是).

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的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆