The DRY alternative to __all__
To install the exports package, you can use
python-exports on PyPI:
pip install python-exports>>> from exports import exportNow you can use it to add to __all__ as
-
function decorator
>>> @export ... def spam(): ... ...
-
class decorator:
>>> @export ... class Ham: ... ...
-
by name:
>>> from functools import reduce as fold >>> export('fold')
If the module has no __all__, it is created.
Otherwise, __all__ is converted to a list, and the export is appended.
Exporting a function or class directly relies on the __name__ attribute,
so consider the following example:
>>> def eggs():
... ...
>>> fake_eggs = eggsIf we want to export fake_eggs, then this will not work:
>>> export(fake_eggs) # BAD: this will add `'eggs'` to `__all__`In such cases, use the name instead:
>>> export('fake_eggs') # GOODYou'll be safe if you either
- decorate a function or a class directly with
@export, - pass the name string when using plain
export('...')calls.