mirror of
https://github.com/Brandon-Rozek/website.git
synced 2024-11-22 08:16:29 -05:00
753 B
753 B
title | date | draft | tags | medium_enabled | |
---|---|---|---|---|---|
Quick Python: Export Decorator | 2020-06-14T22:15:38-04:00 | false |
|
true |
A great StackOverflow post by Aaron Hall that shows how you can create an export
decorator in order to not have to specify all the names you want to expose via __all__
.
The Decorator:
import sys
def export(fn):
mod = sys.modules[fn.__module__]
if hasattr(mod, '__all__'):
mod.__all__.append(fn.__name__)
else:
mod.__all__ = [fn.__name__]
return fn
Usage:
__all__ = []
@export # otherwise __all__ = ['test']
def test():
print("test")