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

Luke Paireepinart rabidpoobear at gmail.com
Fri Jan 8 20:45:11 CET 2010

Yes, it depends how the library was designed.  Some are designed so that you
can import some of the library into your global namespace.  For example, in
Pygame library, it's accepted to do
import pygame
from pygame.locals import *

This keeps the methods like

but it lets you avoid having to do
for event in pygame.event.get():
    if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:

instead you can do

for event in pygame.event.get():
    if event.type == KEYDOWN and event.key == K_UP:

But I don't know of any case where it's preferable to use the "from" syntax,
it's mostly just a laziness thing.


On Fri, Jan 8, 2010 at 1:39 PM, Rob Cherry <pythontutor at lxrb.com> wrote:

> Extending on this advice somewhat - is it *ever* correct to "import
> foobar".
> There are countless examples of
> import os,sys
> etc,etc.  Strictly speaking should we always be using "from" to only
> get what we know we need?
> Thanks,
> Rob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100108/c840cb0a/attachment.htm>

More information about the Tutor mailing list