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