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.

Recommended Answers

All 3 Replies

Member Avatar for kdoiron

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.

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.