Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #110711

Re: __all__ attribute: bug and proposal

From Ethan Furman <ethan@stoneleaf.us>
Newsgroups comp.lang.python
Subject Re: __all__ attribute: bug and proposal
Date 2016-06-28 08:29 -0700
Message-ID <mailman.86.1467127758.2358.python-list@python.org> (permalink)
References <d5e97a5e-6f10-4e3f-ab24-113632f16f29@googlegroups.com> <CAPTjJmqLmg5tPhLnvpMOg_+NhYiNOZW_RG2yDbBiqD6MYqtxSg@mail.gmail.com> <CAKJDb-PTja9Wo6SdYGPvvxjDRDGiVWmkw-MhRC2ixNrZMB5Nxw@mail.gmail.com> <577297CA.7000209@stoneleaf.us>

Show all headers | View raw


On 06/27/2016 09:31 PM, Zachary Ware wrote:
> On Mon, Jun 27, 2016 at 7:32 PM, Chris Angelico 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).

I agree.  But lets all do:

   from atpublic import public as api

as using 'public' for the name is bound to lead to confusion with other 
languages that use public to mean something else, and __all__ is really 
defining the public _API_ of that module.

--
~Ethan~

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

__all__ attribute: bug and proposal Pavel S <pavel@schon.cz> - 2016-06-27 13:56 -0700
  Re: __all__ attribute: bug and proposal Chris Angelico <rosuav@gmail.com> - 2016-06-28 07:32 +1000
    Re: __all__ attribute: bug and proposal Pavel S <pavel@schon.cz> - 2016-06-27 15:29 -0700
      Re: __all__ attribute: bug and proposal Chris Angelico <rosuav@gmail.com> - 2016-06-28 08:41 +1000
      Re: __all__ attribute: bug and proposal Terry Reedy <tjreedy@udel.edu> - 2016-06-27 22:46 -0400
        Re: __all__ attribute: bug and proposal Steven D'Aprano <steve@pearwood.info> - 2016-06-28 13:24 +1000
  Re: __all__ attribute: bug and proposal Random832 <random832@fastmail.com> - 2016-06-27 20:25 -0400
  Re: __all__ attribute: bug and proposal Chris Angelico <rosuav@gmail.com> - 2016-06-28 10:32 +1000
  Re: __all__ attribute: bug and proposal MRAB <python@mrabarnett.plus.com> - 2016-06-28 02:55 +0100
  Re: __all__ attribute: bug and proposal Steven D'Aprano <steve@pearwood.info> - 2016-06-28 11:56 +1000
  Re: __all__ attribute: bug and proposal Chris Angelico <rosuav@gmail.com> - 2016-06-28 12:20 +1000
  Re: __all__ attribute: bug and proposal Random832 <random832@fastmail.com> - 2016-06-27 22:27 -0400
  Re: __all__ attribute: bug and proposal Zachary Ware <zachary.ware+pylist@gmail.com> - 2016-06-27 23:31 -0500
  Re: __all__ attribute: bug and proposal Ethan Furman <ethan@stoneleaf.us> - 2016-06-28 08:29 -0700

csiph-web