__all__ attribute: bug and proposal

Zachary Ware zachary.ware+pylist at gmail.com
Tue Jun 28 00:31:55 EDT 2016


On Mon, Jun 27, 2016 at 7:32 PM, Chris Angelico <rosuav at gmail.com> wrote:
> If you're primarily worried about classes and functions, here's a neat
> trick you can use:
>
> __all__ = []
> def all(thing):
>     __all__.append(thing.__name__)
>     return thing

Barry Warsaw has written a nice decorator called 'public' that can be
installed from PyPI as 'atpublic'[0].  It was proposed for inclusion
as a builtin in 3.6 [1], but a less-than-enthusiastic response at the
2016 Language Summit has put that off indefinitely.  I, for one, would
like to see it happen anyway [2], and public support may make it
possible.

The '@public' decorator works like so:

"""
# spam.py

@public
def spam():
    return ' '.join(['spam']*10)

@public
def eggs():
    return 'bacon'

public(HAM=4)
assert HAM == 4

assert sorted(__all__) == sorted(['spam', 'eggs', 'HAM'])
"""

This strikes me as being a cleaner approach than what the OP
suggested, easier to use than Chris' simple decorator, and a nice way
to bring an end to outdated __all__ lists (which is a problem
currently plaguing the standard library, and probably many other
libraries).

[0] https://pypi.python.org/pypi/atpublic
[1] https://bugs.python.org/issue26632
[2] https://bugs.python.org/issue26632#msg267305

--
Zach


More information about the Python-list mailing list