473,383 Members | 1,789 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,383 software developers and data experts.

Circular Inheritance

Hi All,

I have one question regarding circular inheritance

I have 3 files

1) A.py , having module A and some other modules
2) B.py having module B and some other modules
3) C.py having module C and some other modules

Now I want to import Module C in B.py

but C requires A.py
and A requires B.py

so

B requires C
C requires A
A requires B

and when I try to do this using

from ...import....
it tells me that you cannot import this.

So any suggestions on this?

thank you
Jinal

Jul 18 '05 #1
12 5209
On Tue, 01 Jul 2003 15:36:11 -0700, jinal jhaveri wrote:
B requires C
C requires A
A requires B

and when I try to do this using
from ...import....
it tells me that you cannot import this.


Don't Do That Then.

Re-design the modules so that inheritance is a hierarchy. A band-aid
solution may be to create a new class that mediates between two others,
breaking the circle. Much better is to figure out why the classes need
to know so much about each other, and redesign them with smaller,
simpler interfaces.

Even if there were a way to do circular inheritance, it would be a
nightmare to understand, so needs to be redesigned anyway.

--
\ "I'm beginning to think that life is just one long Yoko Ono |
`\ album; no rhyme or reason, just a lot of incoherent shrieks and |
_o__) then it's over." -- Ian Wolff |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B
Jul 18 '05 #2

"jinal jhaveri" <jh*****@usc.edu> wrote in message
news:ma**********************************@python.o rg...
Hi All,

I have one question regarding circular inheritance

I have 3 files

1) A.py , having module A and some other modules
2) B.py having module B and some other modules
3) C.py having module C and some other modules

Now I want to import Module C in B.py

but C requires A.py
and A requires B.py

so

B requires C
C requires A
A requires B

and when I try to do this using

from ...import....
it tells me that you cannot import this.

So any suggestions on this?
You've run into one one of those things that is only
understandable if you remember that Python *executes*
the module during the import process.

When you say "import" something, Python suspends
importing the first module, and begins importing the
second.

Then if the second one tries to import the first, it sees
that it's in the module table, and tries to find what you
requested; but it's incomplete since it's stalled at the
first "import" statement so it probably won't find
whatever it was looking for.

The best way around this is to eliminate the circular
dependency. The result is clearer and easier to
maintain.

Otherwise, you have to be very specific about what
goes where in each module so that any resources that
one wants have actually been loaded when the other
executes. In particular, "from foo import *" simply
doesn't work with a circular import. If it's not in
the incomplete module, it won't get imported.

John Roth


thank you
Jinal

Jul 18 '05 #3
On Tue, 2003-07-01 at 19:00, Ben Finney wrote:
Re-design the modules so that inheritance is a hierarchy. A band-aid
solution may be to create a new class that mediates between two others,
breaking the circle. Much better is to figure out why the classes need
to know so much about each other, and redesign them with smaller,
simpler interfaces.
It seems quite reasonable to have circular dependencies between two or
more classes. The most common case being that one class references the
other, and the other class has back references. Very common, and it
doesn't imply that the classes know too much about each other.

You might encounter less problems if those classes go together in a
single module, but module boundaries are just there to help the
programmer organize code, they have little formal meaning.
Even if there were a way to do circular inheritance, it would be a
nightmare to understand, so needs to be redesigned anyway.


He probably just meant circular dependency, since obviously inheritance
doesn't make sense.

Ian

Jul 18 '05 #4

"Ian Bicking" <ia**@colorstudy.com> wrote in message
news:ma**********************************@python.o rg...
On Tue, 2003-07-01 at 19:00, Ben Finney wrote:
Re-design the modules so that inheritance is a hierarchy. A band-aid solution may be to create a new class that mediates between two others, breaking the circle. Much better is to figure out why the classes need to know so much about each other, and redesign them with smaller,
simpler interfaces.
It seems quite reasonable to have circular dependencies between two or
more classes. The most common case being that one class references

the other, and the other class has back references. Very common, and it
doesn't imply that the classes know too much about each other.
However, this wouldn't normally cause import problems. Import
problems are very specific: they are caused by the two modules
referencing classes or identifiers in the other module ***while they
are being loaded***. If all the modules contain are class and
function definitions, and all the class definitions contain is method
definitions, the only possible problems I could see would be the
inheritance part of the class statement, and defaults in the function
and method definitions. Both of these need to be defined at import
time.

Since inheritance is strictly hierarchical, it isn't too difficult to
import in the right order.
You might encounter less problems if those classes go together in a
single module, but module boundaries are just there to help the
programmer organize code, they have little formal meaning.
Even if there were a way to do circular inheritance, it would be a
nightmare to understand, so needs to be redesigned anyway.
He probably just meant circular dependency, since obviously

inheritance doesn't make sense.
And circular dependencies are difficult as well. The best policy,
as several posters have already said, is to redesign to eliminate it.

John Roth
Ian

Jul 18 '05 #5
In article <ma**********************************@python.org >,
Ian Bicking <ia**@colorstudy.com> wrote:

You might encounter less problems if those classes go together in a
single module, but module boundaries are just there to help the
programmer organize code, they have little formal meaning.


That's not true. Modules define a namespace, and Python's execution
model makes heavy use of the "global" (read, current module's) namespace
for name resolution.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

If you're familiar with the song "Woad", try reading the Subject: line
again. ;-)
Jul 18 '05 #6
In article <ma**********************************@python.org >,
Ian Bicking <ia**@colorstudy.com> wrote:

You might encounter less problems if those classes go together in a
single module, but module boundaries are just there to help the
programmer organize code, they have little formal meaning.


That's not true. Modules define a namespace, and Python's execution
model makes heavy use of the "global" (read, current module's) namespace
for name resolution.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

If you're familiar with the song "Woad", try reading the Subject: line
again. ;-)
Jul 18 '05 #7
On Wed, 2003-07-02 at 10:03, Aahz wrote:
In article <ma**********************************@python.org >,
Ian Bicking <ia**@colorstudy.com> wrote:

You might encounter less problems if those classes go together in a
single module, but module boundaries are just there to help the
programmer organize code, they have little formal meaning.


That's not true. Modules define a namespace, and Python's execution
model makes heavy use of the "global" (read, current module's) namespace
for name resolution.


Certainly modules have considerable *semantics* and effect execution.
But they have little *meaning*. There's all sorts of semantics
associated with classes, but that's incidental to the meaning of a class
-- a class is a set up behaviors common to a kind of object. A module
is just a pile of stuff the programmer likes to keep together. It's
essentially a clerical feature.

Ian

Jul 18 '05 #8
In article <ma*********************************@python.org> ,
Ian Bicking <ia**@colorstudy.com> wrote:
On Wed, 2003-07-02 at 10:03, Aahz wrote:
In article <ma**********************************@python.org >,
Ian Bicking <ia**@colorstudy.com> wrote:

You might encounter less problems if those classes go together in a
single module, but module boundaries are just there to help the
programmer organize code, they have little formal meaning.


That's not true. Modules define a namespace, and Python's execution
model makes heavy use of the "global" (read, current module's) namespace
for name resolution.


Certainly modules have considerable *semantics* and effect execution.
But they have little *meaning*. There's all sorts of semantics
associated with classes, but that's incidental to the meaning of a class
-- a class is a set up behaviors common to a kind of object. A module
is just a pile of stuff the programmer likes to keep together. It's
essentially a clerical feature.


You say that as if the organizing power of clerical work has little
intrinsic meaning. I disagree. It's precisely that organizing power
that lends value.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

Usenet is not a democracy. It is a weird cross between an anarchy and a
dictatorship.
Jul 18 '05 #9
At 02:43 PM 7/2/2003 -0500, Ian Bicking wrote:
On Wed, 2003-07-02 at 10:03, Aahz wrote:
In article <ma**********************************@python.org >,
Ian Bicking <ia**@colorstudy.com> wrote:

You might encounter less problems if those classes go together in a
single module, but module boundaries are just there to help the
programmer organize code, they have little formal meaning.


That's not true. Modules define a namespace, and Python's execution
model makes heavy use of the "global" (read, current module's) namespace
for name resolution.


Certainly modules have considerable *semantics* and effect execution.
But they have little *meaning*. There's all sorts of semantics
associated with classes, but that's incidental to the meaning of a class
-- a class is a set up behaviors common to a kind of object. A module
is just a pile of stuff the programmer likes to keep together. It's
essentially a clerical feature.


Reminds me of the question: "What's the function of mortar." Most will say
"To hold bricks together." But it ALSO keeps them apart!

I prefer to think of modules as a tool to keep various parts of a complex
application apart, rather than having all of them in one module. This
improves readability, maintenance and testing.

Bob Gailer
bg*****@alum.rpi.edu
303 442 2625
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.492 / Virus Database: 291 - Release Date: 6/24/2003

Jul 18 '05 #10
On Wed, 2003-07-02 at 10:03, Aahz wrote:
In article <ma**********************************@python.org >,
Ian Bicking <ia**@colorstudy.com> wrote:

You might encounter less problems if those classes go together in a
single module, but module boundaries are just there to help the
programmer organize code, they have little formal meaning.


That's not true. Modules define a namespace, and Python's execution
model makes heavy use of the "global" (read, current module's) namespace
for name resolution.


Certainly modules have considerable *semantics* and effect execution.
But they have little *meaning*. There's all sorts of semantics
associated with classes, but that's incidental to the meaning of a class
-- a class is a set up behaviors common to a kind of object. A module
is just a pile of stuff the programmer likes to keep together. It's
essentially a clerical feature.

Ian

Jul 18 '05 #11
In article <ma*********************************@python.org> ,
Ian Bicking <ia**@colorstudy.com> wrote:
On Wed, 2003-07-02 at 10:03, Aahz wrote:
In article <ma**********************************@python.org >,
Ian Bicking <ia**@colorstudy.com> wrote:

You might encounter less problems if those classes go together in a
single module, but module boundaries are just there to help the
programmer organize code, they have little formal meaning.


That's not true. Modules define a namespace, and Python's execution
model makes heavy use of the "global" (read, current module's) namespace
for name resolution.


Certainly modules have considerable *semantics* and effect execution.
But they have little *meaning*. There's all sorts of semantics
associated with classes, but that's incidental to the meaning of a class
-- a class is a set up behaviors common to a kind of object. A module
is just a pile of stuff the programmer likes to keep together. It's
essentially a clerical feature.


You say that as if the organizing power of clerical work has little
intrinsic meaning. I disagree. It's precisely that organizing power
that lends value.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

Usenet is not a democracy. It is a weird cross between an anarchy and a
dictatorship.
Jul 18 '05 #12
At 02:43 PM 7/2/2003 -0500, Ian Bicking wrote:
On Wed, 2003-07-02 at 10:03, Aahz wrote:
In article <ma**********************************@python.org >,
Ian Bicking <ia**@colorstudy.com> wrote:

You might encounter less problems if those classes go together in a
single module, but module boundaries are just there to help the
programmer organize code, they have little formal meaning.


That's not true. Modules define a namespace, and Python's execution
model makes heavy use of the "global" (read, current module's) namespace
for name resolution.


Certainly modules have considerable *semantics* and effect execution.
But they have little *meaning*. There's all sorts of semantics
associated with classes, but that's incidental to the meaning of a class
-- a class is a set up behaviors common to a kind of object. A module
is just a pile of stuff the programmer likes to keep together. It's
essentially a clerical feature.


Reminds me of the question: "What's the function of mortar." Most will say
"To hold bricks together." But it ALSO keeps them apart!

I prefer to think of modules as a tool to keep various parts of a complex
application apart, rather than having all of them in one module. This
improves readability, maintenance and testing.

Bob Gailer
bg*****@alum.rpi.edu
303 442 2625
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.492 / Virus Database: 291 - Release Date: 6/24/2003

Jul 18 '05 #13

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

Similar topics

3
by: crichmon | last post by:
Any general advice for dealing with circular dependencies? For example, I have a situation which, when simplified, is similar to: ///////////// // A.h class A { public: int x;
6
by: Markus Dehmann | last post by:
I have a circular dependency between two classes. The FAQ hint about a forward declaration does not help in my case ( How can I create two classes that both know about each other?) Error:...
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...
3
by: JCB | last post by:
Hi, I have two C file which are referencing in a circular way. To simplify I use an example with car : Suppose I have a program for cars. I have car.c, structure car and functions...
12
by: Frank Rizzo | last post by:
I have a circular reference between 2 classes in the same project (i.e. each class refers to the other). The app runs fine and I am seeing no issues, which kind of surprised me. Are there any...
3
by: Keith F. | last post by:
Visual Studio doesn't allow circular references between projects. I have a situation where I need to allow 2 projects to reference each other. Is there any way to make Visual Studio allow this? ...
6
by: Stephen Robertson | last post by:
We are currently in a dead end with a circular reference issue using vb.net, and are hoping someone might help us resolve it. Idea... We have frmmain calling frmperson (dim f as new frmperson)...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.