Python中__init__.py如何使用?

 youxiang574传奇_257 发布于 2022-11-04 10:37

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

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

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

    2022-11-07 18:33 回答
  • 导出包别名?

    2022-11-07 18:43 回答
  • 把他当做class中的__init__()就好啦

    2022-11-07 18:44 回答
  • 公共变量,公共函数,初始化一些环境,或者环境条件检查之类的

    2022-11-07 18:47 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有