|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
||||
|
||||
|
import vs. from
I was reading a Tkinter tutorial and I've noticed they use
Code:
from Tkinter import * instead of the usual Code:
import Tkinter Is there any difference, or is it equivalent ? |
|
#2
|
|||
|
|||
|
When you do 'import Tkinter' you will access all the data objects through the Tkinter namespace. When you do a 'from Tkinter import *', you import all non-private data objects into your current namespace. Normally you shouldn't use 'from <something> import *', because it pollutes the namespace; but practicality beats purity...
|
|
#3
|
||||
|
||||
|
I haven't hacked in python for a while, but IIRC, the difference between this statement:
from somemodule import * and this statement: import somemodule is that the first one imports all the functions implicitly within the namespace, whereas with the second one only public functions/objects are imported, so you may still have to qualify functions and objects with the module name, when calling them. For example: Code:
#!/usr/bin/python from sys import * print "Hello world" exit(1) and Code:
#!/usr/bin/python import sys print "Hello world" sys.exit(1) Notice that in the first case, I don't have to qualify exit(), but in the second case, I do.
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest Down with Sharon Osbourne Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month Last edited by Scorpions4ever : July 21st, 2003 at 01:39 PM. |
|
#4
|
||||
|
||||
|
where they both do roughly the same thing (program wise) its really just a personal choice.
from module import functions - imports the module and allows access to its functions/classes without the use of the module name prefix. import module - import the module for use. My personal favourate simply because its easier to read. Hope this helps, Mark. |
|
#5
|
||||
|
||||
|
Just thought I'd add my own opinion here
![]() I'd always go with "import module", since it makes reading the code easier (because you always know that "sys.exit()" is a sys method, not a builtin or one of your own), and because it means you don't accidentally overwrite your own functions/methods when importing lots of modules. Keeps namespace nice and tidy ![]() And if you do use "from module import *", try to explictly import particular functions/methods instead, since it will take up less memory and again be safer/nicer (i.e. "from module import x, y, z"). |
|
#6
|
|||
|
|||
|
Personally, I would do:
Code:
import Tkinter as tk to make the the code a little less excessively expressive. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > import vs. from |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|