~/Blog

Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI studying Automated Reasoning in AI and Linux Enthusiast.

Quick Python: Export Decorator

Published on

Updated on

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")
Reply via Email Buy me a Coffee
Was this useful? Feel free to share: Hacker News Reddit Twitter