Import Issue

Steve Holden steve at holdenweb.com
Thu Jun 29 08:29:16 EDT 2006


praveenkumar.117 at gmail.com wrote:
> Hi All,
> 
>           What is the difference between
>            import string
>               and 
>            from string import *
> 
sholden at bigboy ~/Projects/dbimp
$ python
Python 2.4.1 (#1, May 27 2005, 18:02:40)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
Started with C:/Steve/.pythonrc
  >>> dirnow = dir()
  >>> import string
  >>> print [x for x in dir() if x not in dirnow]
['_[1]', 'dirnow', 'string']
  >>>

In the above case something in string is accessed as string.something.

sholden at bigboy ~/Projects/dbimp
$ python
Python 2.4.1 (#1, May 27 2005, 18:02:40)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
Started with C:/Steve/.pythonrc
  >>> dirnow = dir()
  >>> from string import *
  >>> print [x for x in dir() if x not in dirnow]
['Template', '_[1]', 'ascii_letters', 'ascii_lowercase', 
'ascii_uppercase', 'atof', 'atof_error', 'atoi', 'atoi_error', 'atol', 
'atol_error', 'capitalize', 'capwords', 'center', 'count', 'digits', 
'dirnow', 'expandtabs', 'find', 'hexdigits', 'index', 'index_error', 
'join', 'joinfields', 'letters', 'ljust', 'lower', 'lowercase', 
'lstrip', 'maketrans', 'octdigits', 'printable', 'punctuation', 
'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 
'splitfields', 'strip', 'swapcase', 'translate', 'upper', 'uppercase', 
'whitespace', 'zfill']
  >>>

In the second case something from string is inserted directly into the 
current module's namespace, possibly rebinding the name "something" if 
it already had a value, but you get the convenience of being able to 
access it as something. Not generally regarded as a good idea unless you 
know the package and know it has been designed to be used in this way.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Love me, love my blog  http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list