Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Which is better? from module import * or import module.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
robie1373
n00b
n00b


Joined: 03 Nov 2002
Posts: 13
Location: Fort Fun, Co.

PostPosted: Tue Nov 12, 2002 8:11 am    Post subject: Which is better? from module import * or import module. Reply with quote

I've seen both of these touted as the only way to import modules in Python.
What are the pros and cons of the two?
Does it matter if you're writing script-level or fancy gui programs?
_________________
Hippy Hiaku, Brother Trucker! --the scud
Back to top
View user's profile Send private message
fuxored
n00b
n00b


Joined: 29 May 2002
Posts: 71

PostPosted: Tue Nov 12, 2002 8:58 am    Post subject: Reply with quote

Ok I'm not the best at explaining so bare with me..:) I use the term function loosely, I'm not necessarly referring to a python funtion.

Doing a normal `import module' only imports the module name into the local symbol table. So to access any functions contained in the module you have to use its name as the qualifier eg;
Code:

>>>import os
>>>os.system("echo 1234")
1234
0
>>>system
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'system' is not defined
>>>from os import system
>>>system
<built-in function system>


Using the 'from module import SOMEFUNCTION' will import all the defintiions inside the function you imported. You can then access them directly as you see in the last part of my example. If you do 'from module import *' will import all definitions from that module.

Take a look at http://www.python.org/doc/current/tut/node8.html for a much better explanation.
Back to top
View user's profile Send private message
fuxored
n00b
n00b


Joined: 29 May 2002
Posts: 71

PostPosted: Tue Nov 12, 2002 9:19 am    Post subject: Reply with quote

I guess kinda forgot to answer the Which is better part. The answer would be neither, they both have there uses. Depending on the situation one may be better than the other htough.

For example if I only need one class definition from a module theres no point in importing everything with 'from module import *', instead I would do 'from module import "the class I want"'.

But if I want to be able to have access to the entire module then 'import module', or to get everything from the module and to use it just like it was written locally then ' from module import *'

Hope that helps some
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum