[Tutor] Question on "import foobar" vs "from foobar import *"

Lie Ryan lie.1296 at gmail.com
Sat Jan 9 08:03:50 CET 2010


On 1/9/2010 6:39 AM, Rob Cherry wrote:
> Extending on this advice somewhat - is it *ever* correct to "import foobar".
>
> There are countless examples of
>
> import os,sys

The rule of thumb is:
Use the "from module import something" if you're only going to use one 
or two functions from the module; only use "from module import *" if the 
module was designed for such use (e.g. Tkconstants, pygame.constants) or 
if you're in the interactive interpreter. When in doubt, use the "import 
module" form.

>
> etc,etc.  Strictly speaking should we always be using "from" to only
> get what we know we need?

Not necessarily, the "from module..." actually does a full "import 
module" in the back; you don't actually save anything by 
micro-controlling. In fact, it is quite a work to update the from-list 
every time you want to access another class/function in the module. 
However, the "from module import *" form is considered a bad form 
because it introduces namespace pollution.



More information about the Tutor mailing list