Application-global "switches"?

Nick Craig-Wood nick at craig-wood.com
Mon Sep 7 04:29:55 EDT 2009


kj <no.email at please.post> wrote:
>  I'm looking for the "best-practice" way to define application-global
>  read-only switches, settable from the command line.  The best
>  example I can think of of such global switch is the built-in variable
>  __debug__.  This variable is visible everywhere in a program, and
>  broadly affects its operation.

This is what I've done in the past...

Create a Config class in a module called config.py and instantiate it

class Config:
    def __init__(self):
        self.debug = True
        # etc

config = Config()

Then where you want to use it you can then use

from config import config

if config.debug:
    # blah

This has the advantage that you can define some methods on your config
object (eg save).

I don't know whether this is best practice but it works for me!

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list