Parametrized module import

Jacek Generowicz jacek.generowicz at
Thu Jul 8 15:14:10 CEST 2004

Oliver Fromme <olli at> writes:

> However, the problem can be solved by not modifying a
> global variable in the "config" module directly, but by
> using a function which allows only one call.
>     #---  File  ---
>     foo_interface_mode = None
>     def set_foo_interface_mode (mode):
>         if foo_interface_mode is None:
>             foo_interface_mode = mode
>         else:
>             raise "foo_interface_mode may only be set once"

But there must be a default configuration, so you can't check for
None. I typically deal with these situations by using functions which
replace themselves when called:

def configure():
    print "Configuring ..."
    global configure
    def configure():
        print "Sorry, already configured."

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.

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.

More information about the Python-list mailing list