473,387 Members | 3,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Circular imports

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 ?
Jul 18 '05 #1
2 1798
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

Jul 18 '05 #2
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.
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris S. | last post by:
Consider the sample case: ## a.py import d import b b.App() ## b.py from c import C B = 'B'
5
by: qhfgva | last post by:
I'm working with a large code base that I'm slowly trying to fix "unpythonic" features of. One feature I'm trying to fix is the use of: # how things are now...
2
by: ernesto basc?n pantoja | last post by:
Hi everybody: I'm implementing a general C++ framework and I have a basic question about circular dependencies: I am creating a base class Object, my Object class has a method defined as:...
16
by: Kiuhnm | last post by:
Is there an elegant way to deal with semi-circular definitions? Semi-circular definition: A { B }; B { *A }; Circular reference: A { *B }; B { *A }; The problems arise when there are more...
1
by: Learning Python | last post by:
An example in the book I didn't understood well two modules files recursively import/from each other in recur1.py,we have: x=1 import recur2 y=1
1
by: dotnetnewbie | last post by:
Hi all I am new to .NET and webservice so I wish someone can shed some light on me. I have a Project class and a Product class, the Project can contain multiple Products (as an ArrayList). In...
16
by: Dave S | last post by:
Anyone know how to get round the problem of circular references in VB.NET (sorry I'm a .NET newbie). I create one project which has 2 classes in it, MyHandler and MyObject. MyHandler just needs...
7
by: barias | last post by:
Although circular dependencies are something developers should normally avoid, unfortunately they are very easy to create accidentally between classes in a VS project (i.e. circular compile-time...
6
by: bvdp | last post by:
I'm going quite nutty here with an import problem. I've got a fairly complicated program (about 12,000 lines in 34 modules). I just made some "improvements" and get the following error: bob$ mma...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.