Question by someone coming from C...

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Tue Jun 10 11:12:15 CEST 2008

Skye a écrit :
> 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?

Others already gave you the best advises (namely: using the logging 
module). But let's answer your questions anyway:

1/ by convention, anything named ALL_UPPER is considered a constant. 
Anyone breaking your code by messing with a PSEUDO_CONSTANT is on it's 
own and has no right to complain. Consider it as a "warranty void if 
unsealed" warning.

2/ just use plain integers and bitwise operators. But we're usually more 
concerned about readability than about saving bits, and I've rarely 
(maybe twice ?) seen bitfields used that way in Python.

More information about the Python-list mailing list