469,579 Members | 1,102 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

__init__.py in packages

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?

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
Jul 19 '05 #1
3 5101
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.
Jul 19 '05 #2
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/
Jul 19 '05 #3
Gary Wilson Jr wrote:
I would really like to see an example or situation that makes good
use of the __init__.py file.


I've attached a non-trivial example, from my
PyGUI package. It uses some trickery to switch
in one of a number of subdirectories of platform
specific code, and then imports a bunch of names
from submodules into the top-level package, so
the user can pretend he's just using a single
top-level module, e.g.

from GUI import Window

even though Window is actually defined in some
submodule.

This is a rather extreme example, though -- most
__init__.py files are much simpler!

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

_versions = [
("Carbon", "Mac"),
("gtk", "Gtk"),
]

from os import environ as _env
_platdir = _env.get("PYGUI_IMPLEMENTATION")
if not _platdir:
for _testmod, _platdir in _versions:
try:
__import__(_testmod)
break
except ImportError:
continue
else:
raise ImportError("Unable to find an implementation of PyGUI for this installation")

print "PyGUI: Using implementation:", _platdir

from os.path import join as _join
_here = __path__[0]
__path__.append(_join(_here, _platdir))
__path__.append(_join(_here, "Generic"))

from Version import version

from Actions import Action
from AlertFunctions import alert, alert2, alert3, \
stop_alert, note_alert, confirm, ask, confirm_or_cancel, ask_or_cancel
from Applications import Application, application
from Buttons import Button
from CheckBoxes import CheckBox
from Colors import Color, rgb
from Components import Component
from Dialogs import Dialog
from Documents import Document
from Events import Event
from Exceptions import Cancel
from FileDialogs import request_old_file, request_new_file
from Files import FileRef, DirRef
from Fonts import Font
from Containers import Frame
from Images import Image
from Labels import Label
from Menus import Menu
from MessageHandlers import MessageHandler
from ModalDialogs import ModalDialog
from Models import Model
from Pixmaps import Pixmap
from Properties import Properties, overridable_property
from RadioButtons import RadioButton
from RadioGroups import RadioGroup
from ScrollBars import ScrollBar
from ScrollFrames import ScrollFrame
import StdColors
import StdFonts
from Tasks import Task
from TextFields import TextField
##from TextModels import TextModel
##from TextViews import TextView
from Views import View
from Windows import Window

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Robert Clayton | last post: by
4 posts views Thread by Christopher J. Bottaro | last post: by
2 posts views Thread by Peter Saffrey | last post: by
6 posts views Thread by zelzel.zsu | last post: by
1 post views Thread by Kwikrick | last post: by
3 posts views Thread by Srikanth | last post: by
1 post views Thread by Johannes Bauer | last post: by
7 posts views Thread by Daniel | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.