Thread: Import question

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    Smile Import question


    I'm looking at some code examples for sockets, and find one example that shows:

    import socket

    and another that shows:

    from socket import *

    I don't understand the difference, and find that I can't do both because in one condition the 'import socket' instruction works for one example, but if I do both imports, it no longer works. Can someone please tell me what's going on here.

    Thanks much
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    The first is a standard import.
    Code:
    import module
    anytime you want something from that module you have to qualify it with the module name; ie:
    Code:
    module.whatever
    The star import on the other hand
    Code:
    from module import *
    dumps the entire contents of the module into your global namespace and you could access all of its contents unqualified.

    Star imports are bad; especially for neophytes that don't understand why they are bad. Don't use them and try to avoid any tutorials that do. It is much easier to never start using than to quit once already addicted.

    -Mek

IMN logo majestic logo threadwatch logo seochat tools logo