forcing future re-import from with an imported module

rdmurray at bitdance.com rdmurray at bitdance.com
Wed Dec 10 18:43:28 EST 2008


On Wed, 10 Dec 2008 at 14:53, _wolf wrote:
> thanks for your answer. i am aware that imports are not designed to
> have side-effects, but this is exactly what i want: to trigger an
> action with `import foo`. you get foo, and doing this can have a side-
> effect for the module, in roughly the way that a `from __future__
> import with_statement` changes the interpretation of the current
> module (of course, i do not intend to effect syntactic changes---my
> idea is to look into the module namespace and modify it). think of it
> as ‘metamodule programming’ (à la metaclass programming).
>
> maybe import hooks are the way to go? somtimes it would be good if
> there was a signalling system that broadcasts all kinds of system
> state change.
>
> cheers & ~flow
>
> ok so the question is: how to make it so each import of a given module
> has a side-effect, even repeated imports?

Why can't you have the code that is doing the import subsequently
call a function from the module to produce whatever side effect it
is you want?  Explicit is better than implicit.  A python
programmer is going to expect that importing a module is idempotent,
and breaking that assumption strikes me as a bad idea.

Maybe you should give us more details about what problem it is
you are trying to solve.  There might be a good pythonic way to
solve it.

--RDM


More information about the Python-list mailing list