The vast majority of __init__.py files that I write are empty, because many packages have nothing to initialize.
One example in which I can start initialization is when, during the download of a package, I want to read a bunch of data once and for all (from files, a database or a network, say) - in this case it is much better to do this reading in a private function in the __init__.py package instead of having a separate “initialization module” and redundantly importing this module from each separate real module in the package (uselessly repeating and -prone error: this is obviously the case when relying on the language, ensuring that the package __init__.py loaded once before m any module in the package is obviously much larger than Pythonic!).
For other specific and authoritative expressions of opinion, see the various approaches adopted in the various packages that are part of the Python standard library.
Alex Martelli Mar 02 '10 at 6:26 2010-03-02 06:26
source share