I've seen some Python programmers use the following style fairly consistently (we'll call it style 1):
import some_module
# Use some_module.some_identifier in various places.
For support of this style, you can cite the "explicit is better than implicit" maxim. I've seen other programmers use this style (style 2):
from some_module import some_identifier
# Use some_identifier in various places.
The primary benefit that I see in style 2 is maintainability -- especially with duck typing ideals I may want to swap some_module for some_other_module. I also feel style 2 wins points with the "readability counts" maxim. Although I tend to disagree, one can always argue that search-and-replace is just as good an option when using the first style.
Addendum: It was noted that you could use as
to solve the switch from some_module
to some_other_module
in style 1. I forgot to mention that it is also common to decide to implement some_identifier
in your current module, which makes creation of an equivalent some_module
container slightly awkward.
I personally try not to mess too much with my namespace, so in most situations I just do
or import module as mod
Only real diffrence is when I have a module with a single class that's used a lot. If I had sublclassed a
list
type to add some funcionality there, I'd useetc.
I usually use a threshold to decide this. If I want to use a lot of things within
some_module
, I'll use:If there's only one or two things I need:
That's assuming I don't need a
whatever
fromsome_other_module
, of course.I tend to use the
as
clause on the imports so that I can reduce my typing and substitue another module quite easily in the future.I find that the notation
works best in most cases. Also, in case of name clash for the symbol, you can use:
As the question states, it avoids rewriting the module name all the time, each time with a risk of mistyping it. I use the syntax:
Only in two cases:
I prefer to
import X
and then useX.a
as much as possible.My exception centers on the deeply nested modules in a big framework like Django. Their module names tend to get lengthy, and their examples all say
from django.conf import settings
to save you typingdjango.conf.settings.DEBUG
everywhere.If the module name is deeply nested, then the exception is to use
from X.Y.Z import a
.With the existence of the following syntax:
the maintainability argument of style 2 is no longer relevant.
I tend to use style 1. Normally, I find that I explicitly reference the imported package name only a few times in a typical Python program. Everything else is methods on the object, which of course don't need to reference the imported package.
There are uses for both cases, so I don't think this is an either-or issue. I'd consider using from module
import x,y,z
when:There are a fairly small number of things to import
The purpose of the functions imported is obvious when divorced from the module name. If the names are fairly generic, they may clash with others and tell you little. eg. seeing
remove
tells you little, butos.remove
will probably hint that you're dealing with files.The names don't clash. Similar to the above, but more important. Never do something like:
import module [as renamed_module]
has the advantage that it gives a bit more context about what is being called when you use it. It has the disadvantage that this is a bit more cluttered when the module isn't really giving more information, and is slightly less performant (2 lookups instead of 1).It also has advantages when testing however (eg. replacing os.open with a mock object, without having to change every module), and should be used when using mutable modules, e.g.
If in doubt, I'd always go with the
import module
style.