954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

confused by import command

what are the differences between these two type of commands:
1- >>> import socket
2- >>> from socket import *

i mean when you get the object list from the first command it gives different object from when you get objects from the second command.
so does it mean these two command doesnt do the same job,if yes which one does what.

s_jmp
Newbie Poster
14 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

what are the differences between these two type of commands: 1- >>> import socket 2- >>> from socket import *

i mean when you get the object list from the first command it gives different object from when you get objects from the second command. so does it mean these two command doesnt do the same job,if yes which one does what.


If you use "import socket", you will import everything from the "socket" module whether you need it or not, but with "from socket import *", you can actually specify what you want to import. However, the "*" means "everything", so in this case, they have the same effect, in terms of what's imported.

The difference is in how you use what you import. For example, "socket.py" has a function called "getfqdn". With the first method, you would access this function as "socket.getfqdn", while with the second you would simply access it as "getfqdn". The technical difference has to do with namespaces and scope - the link in the previous post has some good information.

kdoiron
Light Poster
37 posts since May 2008
Reputation Points: 39
Solved Threads: 6
 

If your code gets a little longer and more complex, you need to establish a namespace for the different imported functions. It helps you understand the code better and avoids collisions. For instance Tkinter and PIL both have Image.

For example, to save on typing I always use:

import Tkinter as tk
import pygame as pg
import visual as vs
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You