3

I have been looking into __init__.py files, and see the recommendation is to handle the imports for the package there, and/or some initialization.

Then there is a package like Python Collections. That __init__.py contains the implementations for different Collection objects.

Why would you place implementations in the __init__.py instead of dedicated .py files within the package? Is this a good idea? Is it a design pattern?

asked May 22, 2019 at 11:31

1 Answer 1

2

In this particular case, there's no need to have a directory with an __init__.py file at all, since the only other file in that module is abc.py, which defines nothing itself. So if it weren't for abc.py, the entire thing should just be collections.py instead of collections/__init__.py. So why is it what it is? Purely for backwards compatibility reasons. They did not want to break from collections.abc import ..., but wanted to move stuff around internally.

answered May 22, 2019 at 11:38
Sign up to request clarification or add additional context in comments.

2 Comments

Do you have any thoughts in general, not about this particular case of collections?
It's generally not a terrible idea IMO. You only want to be careful with circular imports, e.g. when a sub-module imports some base classes from __init__.py, but __init__.py also imports those submodules to expose them.

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.