Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In case a non-Python programmer is reading this and misunderstood your comment, I'll point out that having code in __init__.py and having import-time side effects are completely unrelated.

When I put any code in __init__.py, it's usually just to make the import path shorter for the programmer using my library. So instead of this:

from mypackage.models import Foo

...users can do this:

from mypackage import Foo

And all it takes to support that is putting this in mypackage/__init__.py:

from mypackage.models import Foo

A quick Googling of "Should I put code in __init__.py" shows a lot of people doing that same pattern. It doesn't show a consensus of people saying that more substantial code in __init__.py is an anti-pattern.



To me you're using __init__.py right. The person who wrote the library I mentioned really isn't.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: