Cyclic class definations
John Henry
john106henry at hotmail.com
Tue Jul 18 14:17:10 EDT 2006
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,
More information about the Python-list
mailing list