"from module import data; print(data)" vs "import module; print(module.data)"
ben+python at benfinney.id.au
Thu Feb 25 18:38:36 EST 2016
Gregory Ewing <greg.ewing at canterbury.ac.nz> writes:
> sohcahtoa82 at gmail.com wrote:
> > Now, I've noticed people talking about importing os.path. Is there any
> > reason to use "import os.path" rather than "import os"? Both of them will
> > still put the "os" module into the global namespace.
> In the case of os.path it doesn't matter, because the
> os module imports the appropriate path module automatically.
My position is that behaviour violates one of the principles of the Zen
of Python: “Special cases aren't special enough to break the rules.”
> But in general, importing a package won't necessarily
> import submodules under it, so sometimes you need to
> import somepackage.somemodule explicitly.
Because that's normally the case, I choose not to rely on that special
behaviour of ‘os’. If I need ‘os.path’, I import it explicitly so no
reader needs to guess::
\ “Crime is contagious… if the government becomes a lawbreaker, |
`\ it breeds contempt for the law.” —Justice Louis Brandeis |
More information about the Python-list