How do I write good/correct package __init__.py files

前端 未结 3 966
不知归路
不知归路 2020-11-28 00:34

My package has the following structure:

mobilescouter/
    __init__.py #1
    mapper/
        __init__.py  #2
        lxml/
            __init__.py #3
               


        
3条回答
  •  一向
    一向 (楼主)
    2020-11-28 01:07

    __all__ is very good - it helps guide import statements without automatically importing modules http://docs.python.org/tutorial/modules.html#importing-from-a-package

    using __all__ and import * is redundant, only __all__ is needed

    I think one of the most powerful reasons to use import * in an __init__.py to import packages is to be able to refactor a script that has grown into multiple scripts without breaking an existing application. But if you're designing a package from the start. I think it's best to leave __init__.py files empty.

    for example:

    foo.py - contains classes related to foo such as fooFactory, tallFoo, shortFoo
    

    then the app grows and now it's a whole folder

    foo/
        __init__.py
        foofactories.py
        tallFoos.py
        shortfoos.py
        mediumfoos.py
        santaslittlehelperfoo.py
        superawsomefoo.py
        anotherfoo.py
    

    then the init script can say

    __all__ = ['foofactories', 'tallFoos', 'shortfoos', 'medumfoos',
               'santaslittlehelperfoo', 'superawsomefoo', 'anotherfoo']
    # deprecated to keep older scripts who import this from breaking
    from foo.foofactories import fooFactory
    from foo.tallfoos import tallFoo
    from foo.shortfoos import shortFoo
    

    so that a script written to do the following does not break during the change:

    from foo import fooFactory, tallFoo, shortFoo
    

提交回复
热议问题