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:



Table Of Contents