On 6 March 2014 11:50, Brian May <[email protected]> wrote:
> Seems to be that there is more to it then that.
>
> For example. if in module/__init__.py I have:
>
> import module.something
>
> I get an circular import loop. The import "module.something" seems to
> imply an import of "module".
>
> However, if instead, I do:
>
> from module.something import somethingelse
>
> Then the circular import loop disappears.
>
> I have read that there are certain other cases where 'import package.name'
> will generate an error, but 'from package import name' won't.
>
Well, there is one common situation during import cycles where the reverse
is true.
Attempting to import a specific name before the module has bound it may
generate an error, but importing the (partially-initialised) module is fine.
For example, the following fails no matter what order your application
imports them in:
### mod2.py
from mod1 import f1
def f2():
pass
### mod1.py
from mod2 import f2
def f1():
pass
wheras, the following works fine regardless of the order the user
application imports them:
### mod3.py
import mod4
def f1(): mod4.f2()
def f3(): print 'woo'
### mod4.py
import mod3
def f2(): mod3.f3()
this is an uncommon enough problem that you don't often have to worry
about it. certainly at package level - __init__.py should contain almost
nothing, perhaps some documentation and an API import if you like.
--
William Leslie
Notice:
Likely much of this email is, by the nature of copyright, covered under
copyright law. You absolutely may reproduce any part of it in accordance
with the copyright law of the nation you are reading this in. Any attempt
to deny you those rights would be illegal without prior contractual
agreement.
_______________________________________________
melbourne-pug mailing list
[email protected]
https://mail.python.org/mailman/listinfo/melbourne-pug