En Mon, 20 Apr 2009 02:22:08 -0300, alex23 <wuwe...@gmail.com> escribió:
On Apr 17, 7:19 pm, Visco Shaun <visc...@gmail.com> wrote:
What is the use of second import as the first import will be
enough(AFAIK) to access anything intended by the second import?
Is there any kind of advantage?
While Piet's explanation is correct for the logging module, you'll
also see examples like:
import os
import os.path
Where os.path _is_ accessible via the original os import.
logging is a package, and os is just a module. Usually you *cannot* write
`import foo.bar` when foo is not a package, so `import os.path` would
normally be an error. But the os module explicitely adds "os.path" to
sys.modules so it becomes valid.
That really confused me a lot when I started using Python.
I believe that in this case, os.path is imported and stored against
'os.path', so any further references to it will be handled by the
standard module lookup, rather than having to look up the 'os' import
and then use getattr to reach path.
(os.path is *not* a typical example!)
If you write `print os.path.dirname(...)` then the name "path" is looked
into the os module as any other attribute. But if you use `import os.path
as path` or `from os import path` then the name "path" can be used
directly as in "print path.dirname(...)"
I would expect that this is mostly
of interest if you were using os.path.methods in an inner loop, to
reduce the number of lookups.
In that case I'd assign the desired function to a local name, to avoid any
name lookup inside the loop.
--
Gabriel Genellina
--
http://mail.python.org/mailman/listinfo/python-list