Simple Modules Question

Sean Blakey sblakey at freei.com
Fri Apr 28 01:30:48 CEST 2000


Akira Kiyomiya wrote:
> 
> I see NameError when I try to do string.atoi("45").
> 
> Is it because 'string' is at a different location??
> 
> Thanks
> 
> Akira
> 
> from string import atoi    # imports 'string'
>                                      # places atoi in current namespace
> print atoi("12345")         # invokes 'atoi' without module name
> 
> string.atoi("45")             # NameError: string
> 
> --
> http://www.python.org/mailman/listinfo/python-list

When you run "from string import atoi" python remembers the name "atoi",
but does not remember where it got atoi from (string).  Python only
keeps access to the string module long enough to get the names you
selected.  This confusing weirdness is one of the many reasons I prefer
to use a simple "import string".

You seem to expect "from string import atoi" to be essentially like the
following code:
>>>import string
>>>atoi = string.atoi

The more accurate equivalent of "from string import atoi" is
>>>import string
>>>atoi = string.atoi
>>>del string

I could be wrong about this (haven't looked at the source in a while),
but thinking about it this way has worked for me so far.
-- 
Sean Blakey
FreeInternet.com
sblakey at freei.com
(253)796-6500x1025




More information about the Python-list mailing list