0

Using the following pseudo-code, would it be possible to use import * for multiple sources, in one line?*

from foo, bar, buzz import *

*Or just a few lines

Graviton
  • 371
  • 1
  • 4
  • 14
  • 2
    Using `import *` is generally a bad idea... – Julien Jul 27 '17 at 08:07
  • @Julien Most of the sources are small personal files, so it shouldn't be too horrendous in my case – Graviton Jul 27 '17 at 08:08
  • afaik you can't. in this case thought it would only be 3 lines. Any particular reason or you're just curious? – Stael Jul 27 '17 at 08:09
  • @Stael Mostly curious, though in my actual program there's more like 9 - so it would be a lot cleaner. – Graviton Jul 27 '17 at 08:10
  • If that worked, what would you expect to happen if there were duplicated names present? Please see [Why is “import *” bad?](https://stackoverflow.com/questions/2386714/why-is-import-bad) – PM 2Ring Jul 27 '17 at 08:12
  • @PM2Ring There aren't any duplicate names, all the sources are also made by me. However total chaos might endure if they were not. – Graviton Jul 27 '17 at 08:13
  • As mentioned in some of the answers to the question I linked previously, there are _some_ situations where star imports are ok, eg in an interactive sessions, or to import a bunch of constants (which should be written in ALL_UPPER_CASE). Or where you want to unify several modules to make it look like they are all a single module. Otherwise, avoid star imports like the plague. They let you save a little bit of typing, but they make a massive negative impact on code readability, since you can no longer see where the imported name comes from. – PM 2Ring Jul 27 '17 at 08:20
  • @PM2Ring Right, right. you mentioned module unification? – Graviton Jul 27 '17 at 08:22
  • 1
    Yes. So you create a file to act as the "master" module, eg 'master.py', and in that file you have _one_ star import per line: `from foo import *`, `from baz import *`, etc. And then in your program you can do `import master` or something like `import master as mm`. That way you aren't dumping all of those names into your program. – PM 2Ring Jul 27 '17 at 08:35
  • 1
    However, if your project is so big that you need multiple modules, then you should probably be arranging it as a [package](https://docs.python.org/3/tutorial/modules.html#packages), so you'd probably put those star imports into an `__init__.py` file, and you'd probably define [`__all__`](https://docs.python.org/3/tutorial/modules.html#importing-from-a-package) to have some control over what gets imported. – PM 2Ring Jul 27 '17 at 08:36
  • @PM2Ring All right, interesting. Thanks for all that! I'll look into that for sure. – Graviton Jul 27 '17 at 08:37

0 Answers0