Circular import problem

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Jul 14 05:27:12 CEST 2007


En Fri, 13 Jul 2007 13:24:57 -0300, bvdp <bob at mellowood.ca> escribió:

>
>> Seehttp://effbot.org/zone/import-confusion.htm
>> Try to move the circular references later in the code (maybe inside a
>> function, when it is required), or much better, refactor it so there is  
>> no
>> circularity.
>>
>> --
>> Gabriel Genellina
>
> Yes, thanks. I'd read that page before posting. Helpful.
>
> But, I still don't understand how python can access a function in a
> file I have NOT included. In this case, to get things to work, I DO
> NOT "import MMA.grooves" but later in the module I access a function
> with "xx=MMA.grooves.somefunc()" and it finds the function, and works
> just fine. It shouldn't work.

That depends a bit on what is "MMA" and what is "grooves".
MMA.grooves means "look for an attribute named grooves inside MMA". If MMA  
is a module, and MMA.grooves is a class/function defined inside the  
module, you don't need to import it before using it.
But if MMA is a package, and MMA.grooves is a module inside that package,  
you need to import it first (unless MMA/__init__.py does that already)

> I have tried to delay the import, and that does work. But, from a
> stylistic view I really to like to have all my imports at the top of
> the module. Maybe some old assembler/C habits on my part.

Sure, it's considered good style.

-- 
Gabriel Genellina




More information about the Python-list mailing list