Global utility module/package

Kent Johnson kent at kentsjohnson.com
Tue May 9 14:19:24 EDT 2006


Christoph Haas wrote:
> Evening,
> 
> I'm currently working on a larger Python project that consists of multiple
> programs and packages. As I need a few utility functions time and again I
> moved them all into a Utility package and created a class there.
...
> As I know that importing packages from multiple modules always keeps it a
> singleton I thought of something like this:
> 
> Util.py:
> ~~~~~~~~
> debugFlag = False
> vibranceLevel = 'good'
> 
> def function1():
>    global debugFlag
>    print debugFlag
> 
> main.py:
> ~~~~~~~~
> import Util
> Util.debugFlag = True
> Util.function1(whatever)
> 
> def doThis():
>    Util.function1(42)
> 
> Here I don't use classes any longer. Good. But to access the "package
> variables" I probably need to use "global" again which just moved the
> ugliness to another position.

This is fine. You don't need 'global' statements to read global 
variables, function1() can be simply
def function1():
     print debugFlag

Kent



More information about the Python-list mailing list