V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
leisurelylicht
V2EX  ›  Python

关于__init__.py 文件

  •  
  •   leisurelylicht · 2016-05-26 23:12:03 +08:00 · 3212 次点击
    这是一个创建于 2895 天前的主题,其中的信息可能已经有所发展或是发生改变。
    __init__.py 文件应该是在包被导入的时候会自动调用,那可不可以把一些对包内模块的检查什么的放在__init__.py 中,如果检查出错就直接 assert ,停止整个程序。

    还是说__init__.py 里只能写模块导入,看网上大家的各种博客和文章也都只讲了模块导入的功能。
    6 条回复    2016-05-27 17:15:50 +08:00
    jason0916
        1
    jason0916  
       2016-05-26 23:17:40 +08:00
    可以的啊
    czheo
        2
    czheo  
       2016-05-27 00:10:38 +08:00
    据说还有人在__init__.py 里写找不到包用 pip 自动安装的呢
    Mark24
        3
    Mark24  
       2016-05-27 00:28:20 +08:00
    可以的
    cwlmxwb
        4
    cwlmxwb  
       2016-05-27 08:25:34 +08:00 via iPhone
    测试一下就好了嘛
    ayaseangle
        5
    ayaseangle  
       2016-05-27 08:31:08 +08:00 via Android
    就是个普通文件而已,你没发现有些包虽然是个文件夹,但仅仅只有一个 initpy
    leisurelylicht
        6
    leisurelylicht  
    OP
       2016-05-27 17:15:50 +08:00
    @czheo 还可以这样的,这个我得试试
    @ayaseangle 原来如此,没仔细研究过其他包
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2604 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:48 · PVG 21:48 · LAX 06:48 · JFK 09:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.