Magic Module Methods?

Martin von Loewis loewis at informatik.hu-berlin.de
Sun Oct 21 09:28:20 EDT 2001


"Magnus Lie Hetland" <mlh at idi.ntnu.no> writes:

> OK - bad example (and terse explanation). The situation was this: I
> was using a module as a config file, and at the same time as an
> object containing defaults at some other point in the program
> (available for users of the library). Therefore, the defaults should
> be accessible from the module, but the people writing the config
> file might not want to set absolutely all the values... I could of
> course do an update() on the module __dict__, and that might be
> clearer, but possibly not near-infinitely? ;)

I'm still not sure what you are trying to do:

You have a module that your users want to customize, by putting values
in it. You don't want to require them to fill in all values, and you
don't want to give them a template that already has all supported
values filled out. Correct?

If so, where do the default values come from, instead? They are not in
the config module, but you want to access them from the config module?

Even if you had an _getattr__ for modules, wouldn't your users have to
put the __getattr__ definition (which surely would look magic) in
their configuration module?

I'd propose the following solution: Make a DEFAULTS.py containing the
defaults, and a config.py that start with

from DEFAULTS import *

Then your users could customize the configuration by putting
assignments after the import statement.

Regards,
Martin



More information about the Python-list mailing list