Module loading trickery

Jean-Michel Pichavant jeanmichel at sequans.com
Tue Oct 5 17:49:09 CEST 2010


Jonas Galvez wrote:
> Is there a way to "inject" something into a module right before it's 
> loaded?
>
> For instance, a.py defines "foo". b.py print()s "foo". 
>
> I want to load b.py into a.py, but I need to let b.py know about "foo" 
> before it can execute.
>
> Is this any way to achieve this?
>
> -- Jonas
No that would mean that a imports b which imports a => circular imports.

Usually it's a cue for bad design.
People solve it by either merging the 2 modules into 1 or defining 'foo' 
in a 3rd module.

You can do also something like :

a.py:

import b
foo = 'I am foo'
b.printThat(foo)

b.py:

def printThat(something):
    print something
   

JM



More information about the Python-list mailing list