Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Circular imports

Reply
Thread Tools

Circular imports

 
 
Edward Diener
Guest
Posts: n/a
 
      05-19-2004
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 ?


 
Reply With Quote
 
 
 
 
Greg Ewing
Guest
Posts: n/a
 
      05-19-2004
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

 
Reply With Quote
 
 
 
 
Edward Diener
Guest
Posts: n/a
 
      05-19-2004
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic imports + relative imports in Python 3 zildjohn01 Python 0 02-22-2011 05:24 PM
Class changes in circular imports when __name__ == '__main__' Spencer Pearson Python 7 09-08-2010 11:42 PM
Circular imports (again) Frank Millman Python 2 08-09-2010 08:06 PM
Imports System.Data or Imports System.Data.SqlClient? Albert ASP .Net 4 07-10-2008 09:00 AM
circular imports qhfgva@gmail.com Python 5 05-20-2005 10:25 PM



Advertisments