Cross-module imports?

M

Matthias Kaeppler

Hi,

I am getting strange errors when I am importing modules cross-wise, like
this:

# module A
import B
var1 = "x"
B.var2 = "y"

# module B
import A
var2 = "z"
print A.var1

The error messages are not even sane, for example the interpreter
complains about not finding 'var1' in module A.

Is there a solution to this problem which doesn't involve introducing a
new module holding the shared objects?

Thanks,
Matthias
 
A

Ant

You are always likely to have problems of one sort or another if you
are using circular references like this. It would be a much better
situation redesigning your modules so that they didn't depend on each
other.

The interpreter is complaining, since it is trying to compile A, hits
the line 'import B', goes straight off into B to compile that, where it
encounters the line 'print A.var1', of course there is no 'A.var1' yet,
since the compiler hasn't yet got that far. So the error message is
perfectly sane (the codes author on the other hand... ;-) )

I would imagine that you *could* get it to work if you first tried to
run the example with line 3 of module B commented out (the thing should
compile fine then) and then run it again with line 3 uncommented again.
This way A will be already compliled when B comes to import it.

If you have to trick the compiler like this though, I'd take a good
look at *why* you want to couple the modules so tightly in the first
place!
 
M

Matthias Kaeppler

Ant said:
If you have to trick the compiler like this though, I'd take a good
look at *why* you want to couple the modules so tightly in the first
place!

Yeah, you're right. I think loosening up the coupling by introducing a
module which holds common shared data is probably a good idea anyway.

Thanks for clearing up,
Matthias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top