The all special variable

suggest change

Modules can have a special variable named __all__ to restrict what variables are imported when using from mymodule import *.

Given the following module:

# mymodule.py

__all__ = ['imported_by_star']

imported_by_star = 42
not_imported_by_star = 21

Only imported_by_star is imported when using from mymodule import *:

>>> from mymodule import *
>>> imported_by_star
42
>>> not_imported_by_star
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'not_imported_by_star' is not defined

However, not_imported_by_star can be imported explicitly:

>>> from mymodule import not_imported_by_star
>>> not_imported_by_star
21

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Importing modules:
* The all special variable

Table Of Contents
2 Filter
3 List
7 Loops
10 Importing modules
22 Reduce
27 Classes
31 Set
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve