Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ethan Furman Newsgroups: comp.lang.python Subject: Re: __all__ attribute: bug and proposal Date: Tue, 28 Jun 2016 08:29:14 -0700 Lines: 54 Message-ID: References: <577297CA.7000209@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de QyclURRFf/KeMmEbT/Sc8Aj95UkV1NpW2X0gO4y0gMPA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"""': 0.05; 'subject:bug': 0.05; 'builtin': 0.07; 'pypi': 0.07; 'api': 0.09; '__all__': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'inclusion': 0.09; 'message- id:@stoneleaf.us': 0.09; 'anyway': 0.11; 'def': 0.13; '2016': 0.16; 'agree.': 0.16; "chris'": 0.16; 'cleaner': 0.16; 'confusion': 0.16; 'outdated': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'suggested,': 0.16; 'wrote:': 0.16; 'else,': 0.18; 'library,': 0.18; 'language': 0.19; 'proposed': 0.20; 'decorator': 0.22; 'import': 0.24; 'written': 0.24; 'header :In-Reply-To:1': 0.24; 'mon,': 0.24; 'header:User-Agent:1': 0.26; 'installed': 0.26; '(which': 0.26; 'chris': 0.26; 'defining': 0.27; 'module.': 0.27; "skip:' 10": 0.28; '~ethan~': 0.29; 'classes': 0.30; 'probably': 0.31; 'problem': 0.33; 'skip:_ 30': 0.33; 'lets': 0.33; 'languages': 0.34; 'lists': 0.34; 'primarily': 0.35; 'something': 0.35; 'but': 0.36; 'possible.': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'being': 0.37; 'one,': 0.37; 'mean': 0.38; 'end': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'bring': 0.62; 'charset:windows-1252': 0.62; '3.6': 0.84; 'decorator,': 0.84; 'worried': 0.84; 'do:': 0.91; 'ware': 0.91 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <577297CA.7000209@stoneleaf.us> X-Mailman-Original-References: Xref: csiph.com comp.lang.python:110711 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~