"from module import data; print(data)" vs "import module; print(module.data)"

Dan Stromberg drsalists at gmail.com
Wed Feb 24 20:07:33 EST 2016


Could people please compare and contrast the two ways of doing imports
in the Subject line?

I've long favored the latter, but I'm working in a code base that
prefers the former.

Is it fair to say that the former increases the surface area of your
shared (sometimes mutable) state?

It's clear that the former saves keystrokes.

I find the latter a little more clear, because you don't have to go
look for where a symbol came from.

Anything else?

Thanks!

PS: Haskell seems better at the former than Python; Haskell tells you
if you import two identical symbols from two different places, when
you try to use one of them - not at import time.  I believe in Python,
whichever symbol you import last, wins.  Haskell does not warn you at
import time, which is fine.  Not sure about OCaml or whatever else.



More information about the Python-list mailing list