Parametrized module import

Oliver Fromme olli at
Thu Jul 8 18:32:34 CEST 2004

Jacek Generowicz <jacek.generowicz at> wrote:
 > But this doesn't quite solve the problem I'm trying to solve, which is
 > to warn, at the time the import statement is executed, that the
 > configuration which is in place is not being respected.

I see.

 > There's nothing wrong with the user changing the mode twenty times
 > before the first import. I guess the imported module could block
 > further configuration changes, and the warning can come when you try
 > to change the configuration _after_ the first import.

In that case, why don't you let the imported module change
the definition of the function in the "config" module?
That's exactly the place after which modifications should
be disallowed, if I understand you correctly.

Third try.

    #---  File  ---
    foo_interface_mode = 0      # default
    def set_foo_interface_mode (mode):
        foo_interface_mode = mode

    #---  File  ---
    import config

    def config_warn():
        print "foo_interface_mode may only be set once!"

    config.set_foo_interface_mode = config_warn

    if config.foo_interface_mode == 0:
        ... this interface
        ... that interface

    #---  File  ---
    import config
    config.set_foo_interface_mode (1)
    import your_module

Best regards

Oliver Fromme, Konrad-Celtis-Str. 72, 81369 Munich, Germany

``All that we see or seem is just a dream within a dream.''
(E. A. Poe)

More information about the Python-list mailing list