Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old July 21st, 2003, 01:20 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear Send a message via MSN to SolarBear
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 ?

Reply With Quote
  #2  
Old July 21st, 2003, 01:37 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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...

Reply With Quote
  #3  
Old July 21st, 2003, 01:37 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 51 m 47 sec
Reputation Power: 797
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.

Reply With Quote
  #4  
Old July 21st, 2003, 01:41 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #5  
Old August 7th, 2003, 06:57 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
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").

Reply With Quote
  #6  
Old August 7th, 2003, 01:34 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Personally, I would do:
Code:
import Tkinter as tk

to make the the code a little less excessively expressive.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > import vs. from


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway