|
| 1 | +# This file showcases how imports work with `__all__`. |
| 2 | +# |
| 3 | +# TL;DR; in `from <module> import *`, if `__all__` is defined in `<module>`, then only |
| 4 | +# the names in `__all__` will be imported -- otherwise all names that doesn't begin with |
| 5 | +# an underscore will be imported. |
| 6 | +# |
| 7 | +# If `__all__` is defined, `__all__` must be a sequence for `from <module> import *` to work. |
| 8 | +# |
| 9 | +# https://docs.python.org/3/reference/simple_stmts.html#the-import-statement |
| 10 | +# https://docs.python.org/3/glossary.html#term-sequence |
| 11 | + |
| 12 | +print("import *") |
| 13 | +print("---") |
| 14 | + |
| 15 | +from no_all import * |
| 16 | +print("no_all.py") |
| 17 | +print(" foo={!r}".format(foo)) |
| 18 | +print(" bar={!r}".format(bar)) |
| 19 | +print(" baz={!r}".format(baz)) |
| 20 | +try: |
| 21 | + print(" _qux={!r}".format(_qux)) |
| 22 | +except NameError: |
| 23 | + print(" _qux not imported") |
| 24 | +del foo, bar, baz |
| 25 | + |
| 26 | +from all_list import * |
| 27 | +print("all_list.py") |
| 28 | +print(" foo={!r}".format(foo)) |
| 29 | +print(" bar={!r}".format(bar)) |
| 30 | +try: |
| 31 | + print(" baz={!r}".format(baz)) |
| 32 | +except NameError: |
| 33 | + print(" baz not imported") |
| 34 | +del foo, bar |
| 35 | + |
| 36 | +from all_tuple import * |
| 37 | +print("all_tuple.py") |
| 38 | +print(" foo={!r}".format(foo)) |
| 39 | +print(" bar={!r}".format(bar)) |
| 40 | +try: |
| 41 | + print(" baz={!r}".format(baz)) |
| 42 | +except NameError: |
| 43 | + print(" baz not imported") |
| 44 | +del foo, bar |
| 45 | + |
| 46 | +from all_dynamic import * |
| 47 | +print("all_dynamic.py") |
| 48 | +print(" foo={!r}".format(foo)) |
| 49 | +print(" bar={!r}".format(bar)) |
| 50 | +try: |
| 51 | + print(" baz={!r}".format(baz)) |
| 52 | +except NameError: |
| 53 | + print(" baz not imported") |
| 54 | +del foo, bar |
| 55 | + |
| 56 | +from all_indirect import * |
| 57 | +print("all_indirect.py") |
| 58 | +print(" foo={!r}".format(foo)) |
| 59 | +print(" bar={!r}".format(bar)) |
| 60 | +try: |
| 61 | + print(" baz={!r}".format(baz)) |
| 62 | +except NameError: |
| 63 | + print(" baz not imported") |
| 64 | +del foo, bar |
| 65 | + |
| 66 | +# Example of wrong definition of `__all__`, where it is not a sequence. |
| 67 | +try: |
| 68 | + from all_set import * |
| 69 | +except TypeError as e: |
| 70 | + assert str(e) == "'set' object does not support indexing" |
| 71 | + print("from all_set import * could not be imported:", e) |
| 72 | + |
| 73 | +print("") |
| 74 | +print("Direct reference on module") |
| 75 | +print("---") |
| 76 | +# Direct reference always works, no matter how `__all__` is set. |
| 77 | +import no_all |
| 78 | +import all_list |
| 79 | +import all_tuple |
| 80 | +import all_dynamic |
| 81 | +import all_indirect |
| 82 | +import all_set |
| 83 | + |
| 84 | +for mod in [no_all, all_list, all_tuple, all_dynamic, all_indirect, all_set]: |
| 85 | + print("{}.py".format(mod.__name__)) |
| 86 | + print(" foo={!r}".format(mod.foo)) |
| 87 | + print(" bar={!r}".format(mod.bar)) |
| 88 | + print(" baz={!r}".format(mod.baz)) |
| 89 | + |
| 90 | +print("\nspecial: no_all._qux={!r}".format(no_all._qux)) |
0 commit comments