Parametrized module import

Heather Coppersmith me at privacy.net
Thu Jul 8 13:51:15 CEST 2004


On 08 Jul 2004 12:58:51 +0200,
Jacek Generowicz <jacek.generowicz at cern.ch> wrote:

> Oliver Fromme <olli at haluter.fromme.com> writes:

>> You could create another module called "config" or "cfg" which
>> contains some global variables.  You import it into your
>> configurable module as well as into your main program.  Then
>> you can configure the module's behaviour via those global
>> variables before importing the module.

> Yes, my initial crappy prototype idea was to add configuration
> information to the sys module, but this variation is much neater
> ... in fact, after the first 2 minutes of thinking about it, it
> looks perfect :-)

> However, one thing which keeps bothering me about the whole
> business, is the possibilty of importing the module (with your
> chosen configuration) after it has already been imported,
> without you knowing it, with a different configuration. Ideally
> there should be some warning about the fact that the
> configuration you specified is being ignored as a result of the
> module already being imported ... and I don't see how to achieve
> this.

Upon import, a module's "top level" code is executed, so try a
variation on this theme at the top level of your module:

    i_am_configured = False

    if i_am_configured:
        print 'i am already configured'
    else:
        import my_configuration_module
        configure_myself( )
        i_am_configured = True

HTH,
Heather

-- 
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli



More information about the Python-list mailing list