Initializing defaults to module variables

Luke Plant L.Plant.98 at cantab.net
Thu Apr 13 05:26:40 EDT 2006


Burton Samograd wrote:

> My question is, how can I setup my program defaults so that they can
> be overwritten by the configuration variables in the user file (and so
> I don't have to scatter default values all over my code in try/catch
> blocks)?

The Django web framework happens to do something very like this.
Instead of 'import config' you have to do 'from django.conf import
settings', and you have an environment variable that allows you to
specify the user settings file, but otherwise I think it is pretty much
the same.

The guts of the mechanism is here:

http://code.djangoproject.com/browser/django/branches/magic-removal/django/conf/__init__.py

It has quite a bit of custom Django stuff in there that you can ignore,
but I think the principles should apply.

Luke




More information about the Python-list mailing list