Re: Any adv. in importing a module and some objects in the same module, into the same file?
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
Re: Any adv. in importing a module and some objects in the same module, into the same file?
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. 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Any adv. in importing a module and some objects in the same module, into the same file?
Visco Shaun visc...@gmail.com (VS) wrote: VS http://docs.python.org/library/logging.html VS While going thr' the above link i came across import statements VS import logging VS import logging.handlers VS What is the use of second import as the first import will be VS enough(AFAIK) to access anything intended by the second import? VS Is there any kind of advantage? Have you tried? import logging logging.handlers Traceback (most recent call last): File stdin, line 1, in module AttributeError: 'module' object has no attribute 'handlers' import logging.handlers logging.handlers module 'logging.handlers' from '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/handlers.pyc' 'logging' is a package. The 'logging' module does not contain a definition of 'handlers' but there is a module 'handlers' in the 'logging' directory. -- Piet van Oostrum p...@cs.uu.nl URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: p...@vanoostrum.org -- http://mail.python.org/mailman/listinfo/python-list