In a current project of mine, I have decided to not put any significant amount of code in __init__.py
files, simply because I don't like them. In my head, an __init__.py
file is just there to inform Python that the folder is a module. I keep forgetting that they might contain lots of code just like any other Python module.
In this project I have decided to create a "main" submodule whenever I'm tempted to put significant amounts of code in an __ini__.py
file, then I import the "main" submodule in __init__.py
and replace the module.
For example, say I have a module named alpha
:
alpha/ __init__.py
And I want to put a few constants and helper methods under the alpha
module. Instead of putting the code into __init__.py
I create a new module called (for example) main
:
alpha/ __init__.py main.py
Then I put my stuff in that module. Then I just put this into __init__.py
:
import sys
import alpha.main
sys.modules["alpha"] = alpha.main
Now I can put stuff into alpha/main.py
:
author = "John Hancock"
maintainer = "John Hancock"
And access it like this:
import alpha
print(alpha.maintainer)
It works perfectly, and I'm loving that I don't have to edit __init__.py
files anymore.
However, this kind of "magic" always gives me the feeling that a more experienced Python ninja would chop me in the face if he caught me.
This convention seems completely innocuous to me, but could it come back to bite me in the ass later? Are there any pitfalls I should look out for?