Results 1 to 4 of 4

Thread: Importing modules in Python? Share/Save - My123World.Com!

  1. #1

    Importing modules in Python?

    Hi,

    I am new to python coding and aware of following ways of importing modules (sys in my case):

    1. import sys
    All methods/functions of sys would get imported and the way of accessing them would be - sys.version

    2. from sys import *
    All methods/functions of sys would get imported and the way of accessing them would be - version

    3. from sys import version
    Just the method 'version' would get imported and the way of accessing it would be - version

    Further, I think the best way to avoid collisions is to use the way 1 so that method/function could be associated with module name (sys.version) and collision could be avoided with other local or user methods.

    Is my perception clear about importing and using them? Or are there more ways of doing it?

    Thanks
    Last edited by RingZzer0; 11-24-2011 at 05:15 AM.

  2. #2
    Super Commando Dhruv abhaythehero's Avatar
    Join Date
    Sep 2010
    Location
    Lucknow/Pune,India
    Posts
    466
    Blog Entries
    2
    Quote Originally Posted by RingZzer0 View Post


    Is my perception clear about importing and using them? Or are there more ways of doing it?

    Thanks
    Yes you are on right track and all of the points you mentioned are correct
    In the world of 0s and 1s, are you a zero or The One !

  3. #3
    Hey thanks for endorsing mate.

  4. #4
    Excerpt from Dive into Python -

    -------------------------------------------------------

    When should you use from module import?
    * If you will be accessing attributes and methods often and don't want to type the module name over and over, use from module import.
    * If you want to selectively import some attributes and methods but not others, use from module import.
    * If the module contains attributes or functions whti the same name as onces in your module, you must use import module to avoid name conflicts.

    Use from module import * sparingly, because it makes it difficult to determine where a particular function or attribute came from, and that makes dubugging and refactoring more difficult.

    -------------------------------------------------------

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •