Cross-module imports?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthias Kaeppler

    #1

    Cross-module imports?

    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
  • Ant

    #2
    Re: Cross-module imports?

    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!

    Comment

    • Matthias Kaeppler

      #3
      Re: Cross-module imports?

      Ant wrote:[color=blue]
      > 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![/color]

      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

      Comment

      Working...