您的位置:首页 > 编程语言 > Python开发

为什么python设计时要有__init__.py才视为包

2017-12-13 20:20 405 查看
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.

大致的理解是:

可能会有一些文件夹,比如有个专门放字符串的文件夹叫string,如果所有目录都视为包的话,import string可能就会覆盖python的string,而产生错误.而只有带init.py的才视为包的话,专门放字符串的文件夹string里肯定不会放一个init.py,就不会覆盖python的string.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python