views:

401

answers:

8

This is one of those semi-religious Python questions that I suspect has well reasoned responses lurking in the community. 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.

+4  A: 

With the existence of the following syntax:

import some_other_module as some_module

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.

Greg Hewgill
+1  A: 

I usually use a threshold to decide this. If I want to use a lot of things within some_module, I'll use:

import some_module as sm
x = sm.whatever

If there's only one or two things I need:

from some_module import whatever
x = whatever

That's assuming I don't need a whatever from some_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.

paxdiablo
A: 

I believe in newer pythons (2.5+? must check my facts...) you can even do:

import some_other_module as some_module

So you could still go with style 1 and swap in a different module later on.

I think it generally maps to how much you want to clutter up your namespace. Will you just be using one or two names in the module? Or all of them (from x import * is not allways bad, just generally)?

Daren Thomas
+1  A: 

I find that the notation

from some_module import some_symbol

works best in most cases. Also, in case of name clash for the symbol, you can use:

from some_module import some_symbol as other_symbol

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:

import  module [as other_module]

Only in two cases:

  1. I use too many of the module functions/objects to import them all
  2. The module defines some symbol that may change during execution
PierreBdR
+1  A: 

I prefer to import X and then use X.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 typing django.conf.settings.DEBUG everywhere.

If the module name is deeply nested, then the exception is to use from X.Y.Z import a.

S.Lott
A: 

I personally try not to mess too much with my namespace, so in most situations I just do

import module

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 use

from SuperImprovedListOverloadedWithFeatures import NewLIst
nl = NewList()

etc.

kender
+3  A: 

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.

Brian
A: 

I tend to use only a few members of each module, so there's a lot of

from john import cleese
from terry import jones, gilliam

in my code. I'll import whole modules (such as os or wx) if I expect to be using most of the module and the module name is short. I'll also import whole modules if there is a name conflict or I want to remind the reader what that function is associated with.

import michael
import sarah

import wave

gov_speech = wave.open(sarah.palin.speechfile)
parrot_sketch = wave.open(michael.palin.justresting)

(I could use from wave import open as wave_open, but I figure that wave.open will be more familiar to the reader.

giltay