473,809 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cyclic class definations

Hi list,

I am trying to understand better Python packaging. This might be a
messed up class hierachy but how would I break this cyclic relatioship?

In file A:

from B import B_Class

Class_A_Main():
def ....
def SomeMethod(self ):
res=B_Class(sel f)

Class_A_SubClas s(Class_A_Main) :
def ...
In file B:

Class B_Class():
def __init__(self,p arent):
...
def SomeMethod(self ):
res=C_Class(sel f)
In file C:

from file A import Class_A_SubClas s

Class C_Class(Class_A _SubClass):
def __init__(self, parent):
...
As you can see, the cyclic relationship exists because C_Class is a
sub-class of SubClass which is in turn a sub-class of Class_A_Main and
in one of the methods of Class_A, it has a need to create a C_Class
object.

I tried to merge the files A and C (by placing C_Class behind A_Class)
and have the method in A_Class create C_Class directly but that doesn't
work because Python says C_Class is not defined when it's processing
the A_Class method. May be somebody can tell me how to "forward
declare" a class?

Regards,

Jul 18 '06 #1
3 1410
I think I got the answer by playing around a bit. It appears you
*don't* need to forward declare things. For example, the following
code works:

class a:
def run(self):
new_a=a_sub()
new_a.print_it( )

class a_sub(a):
def print_it(self):
print "Hi"

b=a().run()

Regards,
John Henry wrote:
Hi list,

I am trying to understand better Python packaging. This might be a
messed up class hierachy but how would I break this cyclic relatioship?

In file A:

from B import B_Class

Class_A_Main():
def ....
def SomeMethod(self ):
res=B_Class(sel f)

Class_A_SubClas s(Class_A_Main) :
def ...
In file B:

Class B_Class():
def __init__(self,p arent):
...
def SomeMethod(self ):
res=C_Class(sel f)
In file C:

from file A import Class_A_SubClas s

Class C_Class(Class_A _SubClass):
def __init__(self, parent):
...
As you can see, the cyclic relationship exists because C_Class is a
sub-class of SubClass which is in turn a sub-class of Class_A_Main and
in one of the methods of Class_A, it has a need to create a C_Class
object.

I tried to merge the files A and C (by placing C_Class behind A_Class)
and have the method in A_Class create C_Class directly but that doesn't
work because Python says C_Class is not defined when it's processing
the A_Class method. May be somebody can tell me how to "forward
declare" a class?

Regards,
Jul 18 '06 #2
John,
Cycles are tricky. Python is an interpreted dynamic language, whatever
object you instantiate in your methods is a different thing than class
hierarchy. Your class hierarchy is fine: ClassA->ClassASubcla ss->ClassC
and it should work. If it doesn't, create a quick mock example and
post it along with the error.

In general try to model your problem to avoid cycles if possible, I
know sometimes they are un-avoidable, but more often then not, they
are. Python might or might not allow cycles in certain situations
(packages, imports, class hierarchy and during usage i.e. in your
semantics) but regardless, if they make your code hard to understand so
try to not introduce them if possible.

Hope this helps,
Nick V.
John Henry wrote:
Hi list,

I am trying to understand better Python packaging. This might be a
messed up class hierachy but how would I break this cyclic relatioship?

In file A:

from B import B_Class

Class_A_Main():
def ....
def SomeMethod(self ):
res=B_Class(sel f)

Class_A_SubClas s(Class_A_Main) :
def ...
In file B:

Class B_Class():
def __init__(self,p arent):
...
def SomeMethod(self ):
res=C_Class(sel f)
In file C:

from file A import Class_A_SubClas s

Class C_Class(Class_A _SubClass):
def __init__(self, parent):
...
As you can see, the cyclic relationship exists because C_Class is a
sub-class of SubClass which is in turn a sub-class of Class_A_Main and
in one of the methods of Class_A, it has a need to create a C_Class
object.

I tried to merge the files A and C (by placing C_Class behind A_Class)
and have the method in A_Class create C_Class directly but that doesn't
work because Python says C_Class is not defined when it's processing
the A_Class method. May be somebody can tell me how to "forward
declare" a class?

Regards,
Jul 18 '06 #3
Thanks for the note, Nick.

I ended up with cycles as a historical reason. Some code that were
written long time ago did not anticipate that part of it will get
sub-classed.

Thanks again.

Nick Vatamaniuc wrote:
John,
Cycles are tricky. Python is an interpreted dynamic language, whatever
object you instantiate in your methods is a different thing than class
hierarchy. Your class hierarchy is fine: ClassA->ClassASubcla ss->ClassC
and it should work. If it doesn't, create a quick mock example and
post it along with the error.

In general try to model your problem to avoid cycles if possible, I
know sometimes they are un-avoidable, but more often then not, they
are. Python might or might not allow cycles in certain situations
(packages, imports, class hierarchy and during usage i.e. in your
semantics) but regardless, if they make your code hard to understand so
try to not introduce them if possible.

Hope this helps,
Nick V.
John Henry wrote:
Hi list,

I am trying to understand better Python packaging. This might be a
messed up class hierachy but how would I break this cyclic relatioship?

In file A:

from B import B_Class

Class_A_Main():
def ....
def SomeMethod(self ):
res=B_Class(sel f)

Class_A_SubClas s(Class_A_Main) :
def ...
In file B:

Class B_Class():
def __init__(self,p arent):
...
def SomeMethod(self ):
res=C_Class(sel f)
In file C:

from file A import Class_A_SubClas s

Class C_Class(Class_A _SubClass):
def __init__(self, parent):
...
As you can see, the cyclic relationship exists because C_Class is a
sub-class of SubClass which is in turn a sub-class of Class_A_Main and
in one of the methods of Class_A, it has a need to create a C_Class
object.

I tried to merge the files A and C (by placing C_Class behind A_Class)
and have the method in A_Class create C_Class directly but that doesn't
work because Python says C_Class is not defined when it's processing
the A_Class method. May be somebody can tell me how to "forward
declare" a class?

Regards,
Jul 18 '06 #4

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

Similar topics

3
2242
by: Dennis Lerche | last post by:
Hi I have a problem regarding cyclic dependency, yeahh I know bad design. But right at this moment I can't see how it should be redesigned to avoid this. The problem is that I just can't get it to compile ..... I have two classes each having their own header files, including each other. A forward decleration doesn't seem to be enough because they also call function calls within the classes. How do I solve this ???
10
1769
by: toton | last post by:
Hi I have a class called Session, which stores a vector of Page, like vector<PageAlso each Page need's to know the Session to which it is attached. Thus I have, (the classes has many other things, I copied only the portion i needed) in Session.hpp #include "Page.hpp" class Session {
1
2273
by: Joe Peterson | last post by:
I've been doing a lot of searching on the topic of one of Python's more disturbing issues (at least to me): the fact that if a __del__ finalizer is defined and a cyclic (circular) reference is made, the garbage collector cannot clean it up. First of all, it seems that it's best to avoid using __del__. So far, I have never used it in my Python programming. So I am safe there. Or am I? Also, to my knowledge, I have never created a...
1
677
by: pallav | last post by:
I have to header files, circuit.h and latch.h that reference each other and are causing a cyclic dependency. latch.h file #include "circuit.h" typedef boost::shared_ptr<struct LatchLatchPtr; class Latch {
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10387
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.