2

The following two statements appear to be equivalent (assuming b is a submodule):

import a.b as c

and

from a import b as c

Both allow me to refer to a.b.foo as c.foo.

Which one is preferred?

sds
  • 58,617
  • 29
  • 161
  • 278
  • Generally, I use the second form if I absolutely have to, but I don't think there's any real consensus here... Also, generally, I'd prefer if you _didn't_ alias your imports without a good reason (the only "good reason" that I can think of off the top of my head being providing a compatable alternative to something else) – mgilson Mar 01 '17 at 16:31
  • from a import b as c is preferred! – Keerthana Prabhakaran Mar 01 '17 at 16:31
  • They are not equivalent: compare `import itertools.product as c` with `from itertools import product as c`. – chepner Mar 01 '17 at 16:35
  • @chepner: `product` is not a submodule. this is not what my question is about. – sds Mar 01 '17 at 16:37
  • @chepner `import itertools.product as c` results in an error as you cannot import anything other than `modules` and `packages` with the `import` syntax, only with `from .. import ..` syntax – Marius Mucenicu Apr 15 '18 at 10:25

0 Answers0