Python module import loop issue

Kottiyath n.kottiyath at gmail.com
Mon Dec 29 11:51:01 EST 2008


This might not be  pure python question. Sorry about that. I couldnt
think of any other place to post the same.
I am creating a _medium_complex_ application, and I am facing issues
with creating the proper module structure.
This is my first application and since this is a run-of-the-mill
application, I hope someone would be able to help me.

Base Module:
Contains definitions for Class A1, Class A2

Module 1.1:
Class B1 (refines A1)
Module 1.2:
Class C1 (refines A1)
Module 1.3:
Class D1 (refines A1)

Module 2.1:
Class B2 (refines A2):
	Uses objects of B1, C1, D1
Module 2.2:
Class C2 (refines A2)
Module 2.3:
Class D2 (refines A2)

-->Python Entry Module : Module EN<--
Calls objects of B1, C1 and D1

Module EN and also Module 2 creates and calls the objects during run
time - and so calls cannot be hardcoded.
So, I want to use Factory methods to create everything.

Module Factory:
import 1.1,1.2,1.3,  2.1,2.2,2.3
A1Factory: {'B1Tag':1.1.B1, 'C1Tag':1.2.C1, 'D1Tag':1.3.D1'}
A2Factory: {'B2Tag':2.1.B2, 'C2Tag':2.2.C2, 'D2Tag':2.3.D2'}

But, since Module requires objects of B1, C1 etc, it has to import
Factory.
Module 2.1:
import Factory.

Now, there is a import loop. How can we avoid this loop?

The following ways I could think of
1. Automatic updation of factory inside superclass whenever a subclass
is created. But, since there is no object created,  I cannot think of
a way of doing this.
2. Update A1Factory in each module which implements refinements.
_Very_important_, how do I make sure each module is hit - so that the
factory is updated? The module EN will be looking only at base module,
so the other modules is not hit. I will have to import every module in
EN - just to make sure that the A1Factory updation code is hit. This
looks in-elegent.

If somebody could help me out, I would be very thankful.



More information about the Python-list mailing list