Basic importing question

Scott David Daniels Scott.Daniels at Acm.Org
Wed Aug 20 08:48:49 EDT 2008


Hussein B wrote:
> On Aug 20, 6:39 am, Bruno Desthuilliers <bruno.
> 42.desthuilli... at websiteburo.invalid> wrote:
>> Hussein B a écrit :
>>> One more question:
>>> If I have this structure:
>>> orig/com/domain/project/Klass1.py
>>>                         Klass2.py
>>>                         __init__.py
>>> Why com, domain, project should have __init__.py also?
>> Yes, why should they ? Unless you want to mimic Java's package system so
>> you do "import com.domain.project.stuff" - which is IMHO a pretty bad
>> idea -, there's just no reason to turn all your filesystem into a python
>> package.
>>
>> Python's philosophy here is that flat is better than nested.
> 
> Sorry I don't follow you :(
> Module package is also nested.

Essentially, "flatter is better than more nested" -- in other words
the things you see after "import this" are principles to trend towards,
not commandments to never be violated.  Some structure is better
than no structure, but that doesn't mean more structure is better
ad infinitum.  Lists of length 1, modules containing a single class,
dictionaries with a single entry, functions or methods that simply
return a variable or instance are "code smells" for over-exercise of
structuring for its own sake.  Each layer should have enough structure
that it is "worth" replacing the more straightforward access with a
name.
Similarly, the name should be associated with few enough things
that you might reasonably expect to hold its scope in your head.
A layer with fifty elements "smells" as bad as one with a single
element, just in a different way.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list