packages, globals, and __builtin__

David Bolen db3l at fitlinxx.com
Thu May 10 16:49:54 EDT 2001


Laura Creighton <lac at cd.chalmers.se> writes:

> I'm making a package, with one class per file name.  I want a global
> variable to keep state in which I can access from any module.  So I
> import __builtin__ and assign something to __builtin__.StateVariable.
> This works great.  But I am feeling squeamish about this.  Should I be?

Why not have a module within the package whose purpose is to maintain
your state, and the other modules simply import it to access the state
via whatever mechanism you want to provide.  I think it would be much
cleaner to use a module namespace for your package state than the
__builtin__ namespace.

If you don't want a distinct module, you could place your package-wide
state in the package file itself (__init__.py), and then have your
individual package modules import their own package to access and/or
manipulate it.  This should work fine unless you name an individual
module in the package the same as the package itself.  It does mean
that the individual modules contain a dependency on the overall
package name.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list