471,318 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,318 software developers and data experts.

Imports in Packages

While working within a package...what is the 'best practice' way to do
your imports.

a/__init__.py
a/one.py
a/two.py
a/b/__init__.py
a/b/cat.py
a/b/dog.py
a/c/cow.py
Suppose I am working in a/c/cow.py and I need something from a/b/
dog.py. If a/b/__init__.py contains what I need from dog.py, should I
do:

"from a.b import desiredfunction"

or

"from a.b.dog import desiredfunction"

What are your reasons for preferring one over the other (performance,
readability, portability)? Also, the same can be said of functions
from other packages...

I know that
>>from math import cos
x = cos(3)
is preferred for performance reasons over
>>import math
x = math.cos(3)
because of the required lookup. Does this mean that that I should
import as far down as possible as well? For example, "from a.b.c.mod
import func" versus "from a import fun".
Dec 17 '07 #1
1 1117
tj*****@gmail.com a écrit :
While working within a package...what is the 'best practice' way to do
your imports.

a/__init__.py
a/one.py
a/two.py
a/b/__init__.py
a/b/cat.py
a/b/dog.py
a/c/cow.py
Suppose I am working in a/c/cow.py and I need something from a/b/
dog.py. If a/b/__init__.py contains what I need from dog.py, should I
do:

"from a.b import desiredfunction"

or

"from a.b.dog import desiredfunction"
What would be the point of exposing desiredfunction in a/b/__init__ if
you still import it from a/b/dog ?-)
What are your reasons for preferring one over the other (performance,
readability, portability)?
What about encapsulation of the package's internal organisation ?

(snip)
I know that

>>>>from math import cos
x = cos(3)


is preferred for performance reasons over

>>>>import math
x = math.cos(3)

because of the required lookup.
If you only use it once, it won't make such a difference wrt/ lookup
time. The "make names local" trick is mostly useful for tight loops in
functions. Else, better to go for readability and favor the first form
IMHO - at least you don't wonder where this 'cos' stuff come from !-)
Does this mean that that I should
import as far down as possible as well? For example, "from a.b.c.mod
import func" versus "from a import fun".
This won't save anything wrt/ lookup. And I stronly suggest that you
read more about namespaces and lookup rules in Python.
Dec 17 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Randall Smith | last post: by
3 posts views Thread by Ron_Adam | last post: by
9 posts views Thread by chapolim-colorado | last post: by
2 posts views Thread by Matthias Kramm | last post: by
4 posts views Thread by Martin Blais | last post: by
3 posts views Thread by Joshua J. Kugler | last post: by
3 posts views Thread by Juha S. | last post: by
3 posts views Thread by Jugdish | last post: by
reply views Thread by Terry Reedy | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.