Structuring Modules with a Ubiquitous Base Class (CircularDependencies)

A

andrew cooke

Is there a good solution to the following problem?

I have a library whose components I would like to separate into
distinct modules. These components inherit from a common base class
that provides common functionality (the inheritance is important only
for implementation; there's a separate ABC mechanism for typing).

Now as that stands, there is no problem. Each module could import the
common base class.

Unfortunately the base class itself references many of the components
(the reason for this is that supplies operator implementations (like
__add__) which are shortcuts for the components themselves).

This leads to a circular dependency - the base class wants to import
the components, which in turn want to import the base class.

Is there any standard solution to this?

Thanks,
Andrew
 
A

andrew cooke

This leads to a circular dependency - the base class wants to import
the components, which in turn want to import the base class.

Is there any standard solution to this?

well, to partially answer my own question, this is certainly
possible. in the general case it might get quite complex, but for the
structure described it is quite simple. the 'trick' is to import the
component in the *method* of the common base class.

for example:

class Base(object):

def __init__(self):
from first import First
from second import Second
self.first = lambda *args: First(*args)
self.second = lambda *args: Second(*args)

where First, defined in first, subclasses Base in the normal way.

however, i suspect this will have a performance hit, since "linking"
is being done at "run time" rather than "compile time". anyone have
any guidance on how serious that would be? i guess it could be
ameliorated by doing the work in the constructor (as above, which is
just that way for a compact example - in "real life" the components
are used in a more complex manner).

and i still suspect there is a more efficient metaclass or similar
approach.

andrew
 
G

Gabriel Genellina

well, to partially answer my own question, this is certainly
possible. in the general case it might get quite complex, but for the
structure described it is quite simple. the 'trick' is to import the
component in the *method* of the common base class.

for example:

class Base(object):

def __init__(self):
from first import First
from second import Second
self.first = lambda *args: First(*args)
self.second = lambda *args: Second(*args)

where First, defined in first, subclasses Base in the normal way.

however, i suspect this will have a performance hit, since "linking"
is being done at "run time" rather than "compile time". anyone have
any guidance on how serious that would be? i guess it could be
ameliorated by doing the work in the constructor (as above, which is
just that way for a compact example - in "real life" the components
are used in a more complex manner).

There is no "linking" step in Python, all is done at run time.
Once a module is imported the first time, any subsequent import is very
cheap.
and i still suspect there is a more efficient metaclass or similar
approach.

This approach is fine to me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top