Global utility module/package
email at christoph-haas.de
Tue May 9 00:05:32 CEST 2006
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. Like this:
def __init__(self, debugFlag=False, vibranceLevel='good'):
self.debugFlag = debugFlag
self.vibranceLevel = vibranceLevel
util = Util.Util()
However this feels like I'm abusing classes because I don't really need
several instances of an object. I just need one instance. Even worse is
that I'm starting to use "global util" to get access to the "alibi
instance" I created. (I'm not sure whether I could even omit it due to
As I know that importing packages from multiple modules always keeps it a
singleton I thought of something like this:
debugFlag = False
vibranceLevel = 'good'
Util.debugFlag = True
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.
What would be a good practice here? All I want is a utility package that I
can import from everywhere (as a singleton) and that I can use even in
subroutines (def) without needing to write "global" here. (The "global"
could probably even be omitted because unless I define a variable in a
"def" scope the global variable should be visible.)
Please enlighten me. :)
P.S.: Code parts untested. More a schema than something that would actually run.
Please reply to the list - not to me personally.
More information about the Python-list