Circular imports

Discussion in 'Python' started by Edward Diener, May 19, 2004.

  1. Although it may not be good design, I have a situation such that module A
    imports module B and module B imports module A. This appears to be causing
    problems when I use Python with mod_python/Apache and PSP programming. Can
    anybody tell me if there are any situations where circular imports cause
    problems and, other than a redesign to eliminate it, if there are any other
    ways around those problems ?
     
    Edward Diener, May 19, 2004
    #1
    1. Advertising

  2. Edward Diener

    Greg Ewing Guest

    Edward Diener wrote:
    > Can
    > anybody tell me if there are any situations where circular imports cause
    > problems and, other than a redesign to eliminate it, if there are any other
    > ways around those problems ?


    Circular imports can cause problems in this situation:

    # module A
    import B

    def foo():
    print "Fooey"

    # module B
    from A import foo

    def blarg():
    foo()


    If module A gets imported first, it immediately
    imports B, which tries to import foo from A before
    it's been defined.

    The problem can be avoided by rewriting B as follows:

    # module B
    import A

    def blarg():
    A.foo()

    This defers the lookup of foo in A until B.blarg
    is called, by which time A will hopefully have
    finished initialising itself.

    In general, if circular imports are involved, always
    use "import X" and refer to "X.n", rather than using
    "from X import n". This will avoid most circular
    import problems.

    --
    Greg Ewing, Computer Science Dept,
    University of Canterbury,
    Christchurch, New Zealand
    http://www.cosc.canterbury.ac.nz/~greg
     
    Greg Ewing, May 19, 2004
    #2
    1. Advertising

  3. Greg Ewing wrote:
    > Edward Diener wrote:
    >> Can
    >> anybody tell me if there are any situations where circular imports
    >> cause problems and, other than a redesign to eliminate it, if there
    >> are any other ways around those problems ?

    >
    > Circular imports can cause problems in this situation:
    >
    > # module A
    > import B
    >
    > def foo():
    > print "Fooey"
    >
    > # module B
    > from A import foo
    >
    > def blarg():
    > foo()
    >
    >
    > If module A gets imported first, it immediately
    > imports B, which tries to import foo from A before
    > it's been defined.
    >
    > The problem can be avoided by rewriting B as follows:
    >
    > # module B
    > import A
    >
    > def blarg():
    > A.foo()
    >
    > This defers the lookup of foo in A until B.blarg
    > is called, by which time A will hopefully have
    > finished initialising itself.
    >
    > In general, if circular imports are involved, always
    > use "import X" and refer to "X.n", rather than using
    > "from X import n". This will avoid most circular
    > import problems.


    Thanks for the info. I was definitely doing some of this and causing myself
    problems. Now I understand why.
     
    Edward Diener, May 20, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kiuhnm
    Replies:
    16
    Views:
    757
    Jonathan Mcdougall
    Jan 3, 2005
  2. circular imports

    , May 20, 2005, in forum: Python
    Replies:
    5
    Views:
    343
  3. Frank Millman

    Circular imports (again)

    Frank Millman, Aug 9, 2010, in forum: Python
    Replies:
    2
    Views:
    287
    Michael Torrie
    Aug 9, 2010
  4. Spencer Pearson
    Replies:
    7
    Views:
    294
    Spencer Pearson
    Sep 9, 2010
  5. Frank Millman
    Replies:
    5
    Views:
    678
    Frank Millman
    Mar 6, 2011
Loading...

Share This Page