import parent

Gary Herron gherron at digipen.edu
Tue Feb 27 19:50:27 EST 2007


Greg at bag.python.org wrote:
> How does one get access to the class that imported a module.  For example:
> foo imports bar -- how does bar access foo?
>
> Thanks.
>
>   

If bar wants to access foo, it just imports it. If foo imports bar as 
well, then you have circular imports, but Python can handle this with no 
problem.

There can be a problem with circular imports if you try to import 
specific names from the module, and the circularity happens before those 
names are defined. But just:
foo:
import bar
...

bar:
import foo
...
works fine.

However... as several others have pointed out, circular imports are 
often a sign that your software design is not very well thought out. 
It's usually better to have your thought process, your design and your 
imports organized in a hierarchal fashion.

Gary Herron







More information about the Python-list mailing list