Question by someone coming from C...

Lie Lie.1296 at gmail.com
Mon Jun 9 23:21:49 CEST 2008


On Jun 10, 4:00 am, 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?

Global variables is like this:

-- module.py --
someglobal = 1
def func():
    print someglobal
    # This makes someglobal readonly,
    # any attempt to write to someglobal
    # would create a new local variable.

def func2():
    global someglobal
    someglobal = 2
    # this allows you to manipulate the global
    # variable
--           --

> 2. any  special voodoo to use bitfields in Python?

That's a not a good idea in Python. In C, it might worth saving some
few bytes of memory especially for embedded application, in python
it's just not worth it, the virtual machine is way huge compared to
the few bytes of space saved.

OK, it is possible to do bitfield manipulation in python:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/113799 but
it's not really worth it

>
> Thanks!
> Skye




More information about the Python-list mailing list