3

If I am working on a project, say it has this file structure:

car/
 body/
 __init__.py
 doors.py
 bonnet.py
 engine/
 cyclinderhead/
 __init__.py
 pistons.py
 __init__.py
 crankshaft.py
 engconstants.py
 utilities.py 

Now invariabley the pistons.py file will need some sort of constant (engconstants.py) or utility function (utilities.py). The pistons.py is a very nested deep file. Is it okay that it depends on files higher up in the file structure? I got the impression that the less nested files should only depend on the more nested files, and depencies going the other way are a code smell? I often get myself into circular import predicaments and am trying to implement a set of rules/guidelines to prevent that.

asked Jun 9, 2022 at 12:58
1
  • Are we to assume that each python file has only one class? For example pistons.py has class Pistons() and so on, or can you give an examples of how the classes are structured? Commented Sep 12, 2022 at 20:33

1 Answer 1

0

No it's all right when subpackages depend on higher modules or packages depend on subpackages. The circular import problem not related to that. It can appear even if files have the same nesting level.

Try to make each module the most independent to not encounter circular import. But sometimes independence can lead to violating DRY and Single Source of Truth.

If, after all, you encounter circular import, the fastest and sometimes the best solution is merging dependencies into one module.

answered Nov 21, 2024 at 22:20
1
  • Dependencies Inversion principle is applicable for class design. Subpackages are designed for structuring related modules. Commented Nov 21, 2024 at 22:25

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.