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.
The difference between import and from import in Python is: import imports the whole library. from import imports a specific member or members of the library.
There is a difference, because in the import x version there are two name lookups: one for the module name, and the second for the function name; on the other hand, using from x import y , you have only one lookup. As you can see, using the form from x import y is a bit faster.
from x import y will only import y from module x . This way, you won't have to use dot-notation. (Bonus: from x import * will refer to the module in the current namespace while also replacing any names that are the same) import x as y will import module x which can be called by referring to it as y.
“import” in Python loads a module into its own namespace whereas “from” imports a module into the current namespace. When “from” is used we don't need to mention the module name whereas, when the “import” is used we should mention the module name.
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, but os.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:
from os import open
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.
import config
config.dburl = 'sqlite:///test.db'
If in doubt, I'd always go with the import module
style.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With