__init__.py in packages

Jarek Zgoda jzgoda at gazeta.usun.pl
Wed Jun 8 15:50:03 EDT 2005


Gary Wilson Jr napisał(a):

> 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?

I don't know The Right Thing, I just use __init__.py as namespace 
shortcut -- everything you define there (functions, classes, names), 
will be available at module level, not deeper.

> Should I only define things in __init__.py that need to be defined when
> importing a subpackage and/or module of package foo?
> 
> Is it ok for me to define classes in foo/__init__.py?
> Whereby I could do something like:
> 
> 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

It's mostly a matter of taste. And logic.

-- 
Jarek Zgoda
http://jpa.berlios.de/ | http://www.zgodowie.org/



More information about the Python-list mailing list