[Python-ideas] Adding an "export" decorator in (e.g.) functools

dw+python-ideas at hmmz.org dw+python-ideas at hmmz.org
Sat May 10 20:04:36 CEST 2014


On Fri, May 09, 2014 at 10:45:33AM +0200, Peter Otten wrote:

> I rarely use star imports, and the decorator is mostly noise in my eyes, so 
> that's a clear -1 from me. 

__all__ also prettifies e.g. pydoc output, which I'd consider a +1.

I'm overall not fond of hiding simple mechanisms using unecessary magic,
especially when the result doesn't generalize to all possible uses of
taht mechanism (e.g. this approach doesn't work for exporting simple
variables).


> def export(f):
>     sys._getframe(1).f_globals.setdefault("__all__", []).append(f.__name__)
>     return f

How about:

    def export(fn):
        mod = sys.modules[fn.__module__]
        lst = vars(mod).setdefault('__all__', [])
        lst.append(fn)
        return fn



More information about the Python-ideas mailing list