Any adv. in importing a module and some objects in the same module, into the same file?

alex23 wuwei23 at gmail.com
Mon Apr 20 07:22:08 CEST 2009


On Apr 17, 7:19 pm, Visco Shaun <visc... at 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.

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. 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.



More information about the Python-list mailing list