Python中__init__.py如何使用?
怪我咯
怪我咯 2017-04-17 15:47:08
[Python讨论组]

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)
大家讲道理

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

怪我咯

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

阿神

导出包别名?

怪我咯

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号