素材牛VIP会员
Python中__init__.py如何使用?
 zh***23  分类:Python  人气:882  回帖:4  发布于6年前 收藏

2015/8/26
一直以来对__init__.py在Python中的作用比较模糊,一致认为它大概的作用是: 让一个目录(directory)编程一个包(package),google之后的答案是:

The __init__.py files are required to make Python treat the directories as 
containing packages; 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. In the simplest case, __init__.py can just be an empty 
file, but it can also execute initialization code for the package or set the 
__all__ variable, described later.

==>http://stackoverflow.com/questions/448271/what-is-init-py-for
==>https://docs.python.org/2/tutorial/modules.html#packages

大家一般是是如何使用的(在该文件中写些什么)?

 标签:python

讨论这个帖子(4)垃圾回帖将一律封号处理……

Lv4 码徒
那***n 产品经理 6年前#1

把他当做class中的__init__()就好啦

Lv2 入门
高***售 JAVA开发工程师 6年前#2

公共变量,公共函数,初始化一些环境,或者环境条件检查之类的

Lv5 码农
许***焱 Web前端工程师 6年前#3

在它其中的代码会在导入包的时候执行一次,所以除了表示是个包之外还可以作为初始化作用或者用all来指名那些可以导出

Lv2 入门
雨***3 页面重构设计 6年前#4

导出包别名?

 文明上网,理性发言!   😉 阿里云幸运券,戳我领取