Code organization

Kent Johnson kent at kentsjohnson.com
Wed Feb 15 09:29:51 EST 2006


bruno at modulix wrote:
> Thomas Girod wrote:
>>
>>I found a lot of documentation about how to code in Python, but not
>>much about how you organize your code in various modules / packages ...
>>As I am not yet used to python, this puzzle me a bit.
> 
> Now for best practices and whatnots, this isn't really specific to
> Python. Try to have modules with high cohesion and low coupling, and
> it'll be fine. Eventually try to provide a facade class or function for
> complex packages (this is a current pattern in the standard lib).

Also, be aware of dependencies between packages and make sure they are 
acyclic - if a module in package A imports a module in package B, then A 
depends on B and B should not be allowed to also depend on A either 
directly (module in B imports module in A) or indirectly (module in B 
imports module in C which imports module in A).

If you follow this rule your packages will be more testable and 
reusable. The alternative tends to devolve into 
everything-depends-on-everything-else which makes testing harder and 
reuse nearly impossible.

Kent



More information about the Python-list mailing list