By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,767 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,767 IT Pros & Developers. It's quick & easy.

Cyclic class definations

P: n/a
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(self)

Class_A_SubClass(Class_A_Main):
def ...
In file B:

Class B_Class():
def __init__(self,parent):
...
def SomeMethod(self):
res=C_Class(self)
In file C:

from file A import Class_A_SubClass

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
Share this Question
Share on Google+
3 Replies


P: n/a
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(self)

Class_A_SubClass(Class_A_Main):
def ...
In file B:

Class B_Class():
def __init__(self,parent):
...
def SomeMethod(self):
res=C_Class(self)
In file C:

from file A import Class_A_SubClass

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

P: n/a
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->ClassASubclass->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(self)

Class_A_SubClass(Class_A_Main):
def ...
In file B:

Class B_Class():
def __init__(self,parent):
...
def SomeMethod(self):
res=C_Class(self)
In file C:

from file A import Class_A_SubClass

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

P: n/a
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->ClassASubclass->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(self)

Class_A_SubClass(Class_A_Main):
def ...
In file B:

Class B_Class():
def __init__(self,parent):
...
def SomeMethod(self):
res=C_Class(self)
In file C:

from file A import Class_A_SubClass

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 discussion thread is closed

Replies have been disabled for this discussion.