Question by someone coming from C...

Matimus mccredie at gmail.com
Mon Jun 9 17:35:23 EDT 2008


On Jun 9, 2:00 pm, Skye <spo... at gmail.com> wrote:
> Writing this app in Python, not sure what the "best practice" would
> be.
>
> 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?
>
> Thanks!
> Skye

There is no conventional way to define a constant. There may be some
tricks if you look around, but I've been coding Python for a long time
and never actually needed one. The side effect is that you could
redefine one of the values at run-time. By naming convention though,
the user will know not to do that, just like they know not to mess
with attributes of an object that begin with "_". If they do mess with
it, well, they better know what they are doing.

There is no special bitfield voodoo that I know of.

Generally, naming conventions are the same as they would be in C (all
caps):

FOO = 0x01
BAR = 0x02
BAZ = 0x04

val = FOO | BAR | BAZ

if val & BAR:
  #do bar stuff

The only time to do that sort of thing (in python) is when interacting
with something else that isn't written in Python though. In general,
for logging, just use the standard logging module:
http://docs.python.org/lib/module-logging.html


Matt



More information about the Python-list mailing list