1

Besides the syntactic differences, I do not understand what the difference between import turtle and from turtle import * is, as both methods appear to produce the same result:

Method 1

import turtle

t = turtle.Pen()

# rest of program...

Method 2

# Method 2:

from turtle import *

t = Turtle()

# rest of program...

One Internet tutorial I followed used Method 1 and another used Method 2. What is the difference between the two and when should I use each one?

unpossible
  • 303
  • 1
  • 8
  • 20
  • The difference is, well, obvious -- one of them keeps your namespaces separate, the other doesn't. In general, use the `import turtle` approach -- that way you always know which module something refers to, which can get extremely confusing when there are multiple `from something import *`s. – Charles Duffy Jun 09 '15 at 21:56
  • method one only loads `turtle` into name space. Thus you have to say `turtle.Pen()` Method 2 loads 'everything' from turtle into namespace. Generally considered bad practice. – Christopher Pearson Jun 09 '15 at 21:56
  • 1
    @CharlesDuffy: thanks for your input. It's only obvious if you know the answer. – unpossible Jun 09 '15 at 22:17

3 Answers3

4

Method 1

You are simply importing the package turtle and as you may already know, you are unable to use a variable that has not been declared. Thus, you must reference every item with the package name as a prefix like turtle.Pen or turtle.Turtle.


Method 2

You are not importing the package turtle and thus you can not use it at all. In fact, you are actually importing every member from the namespace and thus you can just use the names of the items as is as seen in Pen or Turtle which are defined to be inclusive of the namespace.

Community
  • 1
  • 1
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
  • Not strictly true. https://docs.python.org/2/tutorial/modules.html#importing-from-a-package "If __all__ is not defined, the statement from sound.effects import * does not import all submodules from the package sound.effects into the current namespace; it only ensures that the package sound.effects has been imported (possibly running any initialization code in __init__.py) and then imports whatever names are defined in the package." If __all__ is defined, however, the submodules are actually imported directly. – Alex Huszagh Jun 09 '15 at 22:02
  • In this scenario yes, as it has been defined in that way in `__init__.py` – Malik Brahimi Jun 09 '15 at 22:04
  • @AlexanderHuszagh See edits to answer. – Malik Brahimi Jun 09 '15 at 22:05
1

Besides purely semantics, there's very good reason to not do:

from module import *

It pollutes the namespace and you have no easy way of finding what you just included.

By stating:

import module

You load the module into the namespace but restrict everything within. This lets you know precisely what you imported, and so if you have module.a and a defined, you don't have to worry about over-writing module.a.

It has the same import statement, as the link above.

The rest depends on the package structure: specifically whether the module leaves a blank init.py or defines the modules via all = [...]

If all is defined, it imports as those submodules, if not, it imports the module and then renames the submodules.

As shown here, the import * variety is highly discouraged (some Pythonistas have even stated they wished it was never allowed): What exactly does "import *" import?

Community
  • 1
  • 1
Alex Huszagh
  • 13,272
  • 3
  • 39
  • 67
1

"import turtle brings" the module and "from turtle import *" brings objects from the module.

If I do

import sys

I type

sys.exit()

if I do

from sys import *

I type just

exit()
Alex Ivanov
  • 695
  • 4
  • 6