__init__.py in packages

F. Petitjean littlejohn.75 at news.free.fr
Wed Jun 8 19:49:33 CEST 2005


Le Wed, 08 Jun 2005 10:34:38 -0500, Gary Wilson Jr a écrit :
> I'm creating a python package foo.
> 
> What is intended use for __init__.py files?
> Well, I found this: http://www.python.org/doc/essays/packages.html
>>From what I can gather it is for initialization of the package when doing an
> import, but I would really like to see an example or situation that makes good
> use of the __init__.py file.
> 
> Looking at the distutils __init__.py file, it only defines things like
> __version__.  However, looking at the logging __init__.py file, it is a
> 1196-line monster with functions and classes defined throughout.
> 
> What is the RightThing?

The RightThing is to not name your package "foo" :-)

> 
> from foo import MyClass
> 
> Or is is better if I were to put these classes and/or functions in foo/core.py?
> Whereby I would do something like:
> 
> from foo.core import MyClass

Naming matters. First find good names, second write down good
docstrings. In case you have difficulties to find good names, read "How
to write unmaintainable code" and you end up with :
from Mary.Poppins import Pinochio  # the electromagnetics simulator.

-- 
Cent fois sur le métier, remettez votre ouvrage.
Polissez-le et le repolissez
    Boileau Despréaux.



More information about the Python-list mailing list