Module Level __magic__ Variables

10 PM September 18, 2003

Getting ready to dress up a Python module for release, I got to asking myself which __magic__ variable names the module should declare. Some of these are part of the language, while others are defined by convention. There are more than a handful and they don't seem to be documented in one place anywhere on the web, so I made a list for future reference.

Unless otherwise noted, this information is cribbed from the Modules entry of the Language Reference.

__name__
A String. The name the module. This is typically the name you used to refer to the module in the import statement.
__doc__
A String. The docstring. Just like a function or class docstring, it may be defined by placing a string as the first statement in the module. PEPs 257 and 258 have plenty of good info on docstring conventions and processing.
__file__
A String. The name of the file from which the module was imported - it will have a .pyc, .pyd, .so or .dll extension.
__dict__
A read-only dictionary, keyed by String. The module global dictionary.
__all__
Set to a "sequence of strings". When an import statement of the form from module import * is executed, the public names defined in module are imported. If it exists, the __all__ variable defines the public names, otherwise the public names are all the ones without a leading underscore.
__version__
String. PEP-8 defines this as the place to put revision control system version numbers; for example: __version__ = "$Revision: 1.18 $". The ever-amazing pydoc module / script / search-engine / webserver processes this variable specially.
__date__
String. The date of the code. Special processing by pydoc. I could not find any comment on the semantics of this one, though I would expect the __date__ and __revision__ to be kept in step.
__author__
String. The name(s) of each author. Special processing by pydoc.
__credits__
String. Public credits and kudos. Special processing by pydoc. The March 2001 email thread where Guido first noticed then blessed the new __author__ and __credits__ variables starts here.
By alang | # | Comments (9)
(Posted to Software Development and Python)
© 2003-2006 Alan Green