from-import vs. import 
Author Message
 from-import vs. import

What's the difference between

        import strop
and
        from strop import *

If I do:
        line = file.readline()
        fields = split(line, '\t')

when I run it, I get:
        NameError: split

when using 'import', but no error using 'from-import'.

== Michael Tibbs



Sun, 29 Dec 1996 00:18:00 GMT  
 from-import vs. import
Mike Tibbs asks:

Quote:
> What's the difference between

>    import strop
> and
>    from strop import *

If you were to enter these statements interactively and inspect the
namespace with dir(), you would see the difference immediately:

(1)

Quote:
>>> import strop
>>> dir()

['__name__', 'strop']

(2)

Quote:
>>> from strop import *
>>> dir()

['__name__', 'atof', 'atoi', 'atol', 'index', 'joinfields', 'lower', 'lowercase', 'rindex', 'split', 'splitfields', 'strip', 'swapcase', 'upper', 'uppercase', 'whitespace']

In other words, (1) "import strop" introduces the name "strop" into
the namespace, while (2) "from strop import *" introduces each
individual object into it.

In case (1), to use the split() function, you can call
"strop.split(...)".

('__name__' is a variable that contains the name of the current
module, and is predefined in each module.)

Note that, as a matter of style, you shouldn't be importing "strop" --
this is really an "internal" module.  The official interface is
"string", which provides some additional functions and compatibilities
with python versions that don't implement "strop" yet.  Once you have
imported string, there is NO additional performance penalty over using
strop -- whether you use "from string import *" or "import string".


<URL:http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>



Sun, 29 Dec 1996 17:35:30 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. import Image vs from PIL import Image vs import PIL.Image

2. Import Data With File Import

3. Super Import/Import Template from Mitten

4. IMPORT IO, FROM IO IMPORT ?

5. IMPORT IO, FROM IO IMPORT ?

6. IMPORT IO, FROM IO IMPORT ?

7. Import error depending on where importing from

8. Accessing variables created after import in imported files

9. import A.B works, from A import B breaks

10. import & __import__

11. Import , __import__, ihooks question

 

 
Powered by phpBB® Forum Software