To avoid circular imports, one can use import x
and then use x.func
each time. (cf. Circular (or cyclic) imports in Python ). (In my case, I have no other choice actually)
However, when x
is for example a.b.c.d.e
, then you get a non-negligible overhead (in addition to the readability problems), especially if your function is called repetitively. I would like to know if there is some kind of mechanism to define shortcuts like E = a.b.c.d.e
when the imports are resolved ? (or something to avoid the lookup eachtime)
Note : on pypy, there is no performance differences but on CPython, you can get ×2 improvement between a.A()
and a.b.c.d.e.f.g.h.A()
EDIT
about the import ... as ...
solution,
this is the trace I get (I think it will be clearer than any explication I could give) :
File "MyApp/__init__.py", line 3, in <module>
import MyApp.core
File "MyApp/core.py", line 3, in <module>
import MyApp.formatters.ASCIIFormatter as _F
File "MyApp/formatters/__init__.py", line 2, in <module>
import MyApp.formatters.ASCIIFormatter as ASCIIFormatter
File "MyApp/formatters/ASCIIFormatter.py", line 2, in <module>
import MyApp.core as _C
AttributeError: module 'MyApp' has no attribute 'core'