[Python-ideas] Adding an "export" decorator in (e.g.) functools
Chris Angelico
rosuav at gmail.com
Fri May 9 07:59:35 CEST 2014
On Fri, May 9, 2014 at 3:38 PM, Bill Winslow <bunslow at gmail.com> wrote:
> 2) Proper maintenance becomes easier. Attaching a small decorator next to
> each public function is easier to remember than remembering to add an
> arbitrary string to an arbitrary global constant. There is also the added
> benefit that renaming/refactoring also doesn't require modifying the magic
> global when you're done.
+1 for this reason. Attaching info to code is the purpose of
docstrings, and it makes very good sense to implement __all__ the same
way. But your given implementation seems to have a problem: how can
you import that into another module? It looks at "global __all__",
which will look at the module it's implemented in. Would it work like
this, perhaps?
# This is starting to read a little oddly, but oh well :)
from eustace_scrubb import export, government, drain
# ... define your functions with @export, as above ...
__all__ = export.get_all()
The get_all() function would return the list, and empty it in
readiness for the next module. It's non-reentrant, so you'd have to
make sure you don't import any other modules in the middle of defining
your own exports.
Or is there something I'm not seeing about your original export() that
makes it work?
ChrisA
More information about the Python-ideas
mailing list