Style for modules with lots of constants

Neil Cerutti horpner at
Wed Nov 1 18:17:00 CET 2006

On 2006-11-01, Paul McGuire <ptmcg at> wrote:
> I recently had to add some new constants to pyparsing,
> representing LEFT and RIGHT, but I didn't want to define such
> generic and likely-to-collide-with-user-code variable names.
> I settled on defining my own flavor of the Bag class, which I
> named Constants since it is there specifically to define
> constants (although it does nothing to enforce the
> "constant-ness" of the values).
> In your example, this would look something like:
> fileusage = Constants()
> fileusage.Transcript = 1
> fileusage.TextMode = 2
> filemode = Constants()
> filemode.Read = 1
> filemode.Write = 2
> filemode.Append = 4
> filemode.WriteAppend = filemode.Write | filemode.Append
> and so on.  In the client modules they would simply enter "from
> glk import fileusage, filemode".  Or if they just "import glk",
> the references to the constants look like
> "glk.filemode.Append", "flk.fileusage.TextMode", etc., and
> those garish and unsightly '_' separators are reduced to svelte
> little '.'s.
> I think this is a reasonable compromise in avoiding namespace
> pollution, without inflicting unseemly text entry overhead on
> your module clients.

That's looks quite tempting. Thanks!

It will be nice to provide a short way to import all the
constants at once, instead of having to write 'from glk import {a
long list}' in the common case that they want all the constants.

Neil Cerutti
Potluck supper: prayer and medication to follow. --Church Bulletin

More information about the Python-list mailing list