Question by someone coming from C...
pavlovevidence at gmail.com
Tue Jun 10 00:33:40 CEST 2008
On Jun 9, 5:00 pm, Skye <spo... at gmail.com> wrote:
> Writing this app in Python, not sure what the "best practice" would
> I want a bitfield global logging level that allows me to turn specific
> debugging modules on and off. If I was doing this in C, I'd just use
> some globals like:
> unsigned int debug_level = 0;
> #define DEBUG_GENERAL 0x0001
> #define DEBUG_CONFIG 0x0002
> #define DEBUG_OPTIONS 0x0004
> etc etc
> So I guess my questions are:
> 1. there doesn't seem to be a way to define global constants like in
> other languages?
> 2. any special voodoo to use bitfields in Python?
Apart from the good advice "use the logging module", here is the
Pythonic way you'd do this sort of thing in general. (There's not
always a spiffy built-in module for anything you want to do; just
The lack of globals is a minor issue; you can get globally accessible
values by storing them in a module and importing that module.
The way I'd do the above is to define a module, say config.py, to hold
configuration options. Then I'd define each condition in its own
debug_general = False
debug_config = False
debug_options = False
I could then enable debugging by changing the value:
config.debug_general = True
And I could use print debugging output based on the config settings
if config.debug_general or config.debug_options:
But again, the logging modules handles all this for you so no point
for this particular task.
P.S. I'd do it more or less this way in C, too.
More information about the Python-list