[Python-ideas] Avoid circular import

Oleg Broytman phd at phdru.name
Tue Apr 10 14:03:15 CEST 2012

On Tue, Apr 10, 2012 at 02:24:18PM +0300, Victor Varvariuc <victor.varvariuc at gmail.com> wrote:
> On Tue, Apr 10, 2012 at 12:55 PM, Oleg Broytman <phd at phdru.name> wrote:
> > My opinion is - restructure code to avoid circular import instead of
> hacking import machinery.
> It's not feasible sometimes.
> See:
> http://stackoverflow.com/questions/1556387/circular-import-dependency-in-python

   I don't see anything interesting there. Without deeper knowledge
about the code I'd recommend to import b.d in a/__init__.py before
importing c.

> Yes, they could be considered the same package. But if this results in a
> massively huge file then it's impractical. I agree that frequently,
> circular dependencies mean the design should be thought through again. But
> there ARE some design patterns where it's appropriate (and where merging
> the files together would result in a huge file) so I think it's dogmatic to
> say that the packages should either be combined or the design should be
> re-evaluated. ? Matthew Lund Dec 1 '11 at 21:49

   I didn't and do not recommend merging code into one huge module. Call
me dogmatic but I recommend to refactor and move common parts to avoid
circular import.

     Oleg Broytman            http://phdru.name/            phd at phdru.name
           Programmers don't die, they just GOSUB without RETURN.

More information about the Python-ideas mailing list