Newbie on importing

Tim Rowe tim at remove_if_not_spam.digitig.cix.co.uk
Fri Jun 20 21:15:21 CEST 2003


On Fri, 20 Jun 2003 14:11:41 -0400, jwsacksteder at ramprecision.com
wrote:

>That example is still less than clear...
>
>>import sys
>>
>>Imports the module named 'sys'.
>
>Which get me access to what in 'sys'?

It gets you access to sys, and through sys to what's in sys.  so if
you want to use argv() then you have to type sys.argv().  But "sys" is
the only new identifier in your namespace, so it's nice and easy to
make sure it doesn't clash with other libraries and identifiers you
invent.

>
>>from sys import *
>>
>>Imports all names from sys (including names referring to functions).
>
>Likewise, what additional thing are available when doing this? 
>Is 'import sys' a subset of 'from sys import *'?

Not a subset, no.  That one imports everything that's /in/ sys into
your namespace.  So now if you want argv() you can just type argv().
But /all/ the identifiers in sys are now in your namespace, so you've
got to worry about possible interactions with other libraries and your
own identifiers.

Think of "import sys" as getting your toolbag out of the cupboard and
putting it on your workbench.  Think of "from sys import *" as getting
your toolbag out of the cupboard, emptying the contents all over your
workbench and putting the bag itself back into the cupboard.




More information about the Python-list mailing list