caught in the import web again

Charles T. Smith cts.private.yahoo at gmail.com
Tue Nov 18 10:13:20 CET 2014


On Tue, 18 Nov 2014 00:00:42 -0700, Michael Torrie wrote:

> On 11/17/2014 03:45 PM, Steven D'Aprano wrote:
> 
>> Circular dependencies are not just a problem in Python, they are a
>> problem throughout most of software design.
> 
> Personally I find that duck typing eliminates a lot of the circular
> dependency problems.  Class A doesn't necessarily have to know about
> Class B to work with instances of Class B.  All that has to be known is
> what methods are going to be available on those instances.  The
> interface pattern can help here if you wanted a bit more safety (like
> what Zope provides).  Either way it breaks up the dependency cycle
> nicely.
> 
> If Class A needs to instantiate instances of Class B (which depends on
> Class A), then it could call a factory method to do so, perhaps set up
> by a third party after both modules have been imported.
> 
> There are lots of methods of avoiding the issue entirely.
> 
> In the C or C++ world, I find that signalling frameworks can also be
> used to break a dependency cycle.  For example, if Class A needs to call
> a method on an instance of Class B (which in turn depends on Class A),
> that could be done instead as a signal, which is later connected to
> Class B's method after everything is successfully instantiated.  Also
> makes testing a bit easier, because you can simply hook up the signals
> to a test harness.



A bunch of useful thoughts.  Thanks, folks!



More information about the Python-list mailing list