0

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?

asked Oct 28, 2015 at 15:13

1 Answer 1

1

An alternative that doesn't abuse python so much, is to put the following in your __init__.py

from .main import *

Its not quite the same as replacing the module, but I would suspect it will work in most cases.

However, I would take the theory that you shouldn't do it. Pretending that your code is in the root of the alpha module when it is isn't just confusing.

answered Oct 28, 2015 at 15:25
2
  • That's actually what I'm doing now, because a few minutes after I posted the question I noticed that I couldn't import other submodules anymore. I got the error "alpha is not a package", which totally makes sense. I tried deleting the question but you had already answered :P Commented Oct 28, 2015 at 15:46
  • Ah, yes, that would break. Commented Oct 28, 2015 at 15:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.