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


Groups > comp.lang.python > #110624

Re: __all__ attribute: bug and proposal

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: __all__ attribute: bug and proposal
Date Tue, 28 Jun 2016 10:32:36 +1000
Lines 48
Message-ID <mailman.52.1467073963.2358.python-list@python.org> (permalink)
References <d5e97a5e-6f10-4e3f-ab24-113632f16f29@googlegroups.com> <CAPTjJmqLmg5tPhLnvpMOg_+NhYiNOZW_RG2yDbBiqD6MYqtxSg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de cabg3+v5l0uiR1Vmfz9/Vg0YOO7BNt9uQCHeyvPI0qgA==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:bug': 0.05; 'cc:addr:python-list': 0.09; '__all__': 0.09; 'imports': 0.09; 'namespace': 0.09; 'pavel': 0.09; 'tuple': 0.09; 'bug': 0.10; 'def': 0.13; 'argument': 0.15; "'a',": 0.16; "'b'": 0.16; "'c'": 0.16; "'c',": 0.16; '2016': 0.16; '6:56': 0.16; '__all__.': 0.16; 'comma': 0.16; 'concatenated': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'obviously': 0.16; 'attribute': 0.18; 'module,': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'decorator': 0.22; 'pass': 0.22; 'am,': 0.23; '(you': 0.23; 'forgot': 0.23; 'import': 0.24; 'header :In-Reply-To:1': 0.24; 'module': 0.25; "doesn't": 0.26; 'message- id:@mail.gmail.com': 0.27; 'module.': 0.27; 'actual': 0.28; '(it': 0.29; '(optional)': 0.29; 'raise': 0.29; 'classes': 0.30; "can't": 0.32; 'class': 0.33; 'skip:_ 30': 0.33; 'foo': 0.33; 'tue,': 0.34; 'received:google.com': 0.35; 'primarily': 0.35; 'problem.': 0.35; 'but': 0.36; 'subject:: ': 0.37; 'missing': 0.37; "won't": 0.38; 'anything': 0.38; 'someone': 0.38; 'some': 0.40; 'your': 0.60; 'skip:n 10': 0.62; "'bc'": 0.84; 'captures': 0.84; 'chrisa': 0.84; 'decorate': 0.84; 'worried': 0.84; 'to:none': 0.91; 'canonical': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc; bh=2LDLD5r3cccrHChxbKl2hquNIOpDZ738vcoyhZlF28I=; b=rV0cqvnThIA3lLq+taJKynq0XtJ2AD5R1lAnsHV+VAxeyVpIlZc4s1zXBgVMoVgyr3 84Ks231aEPlYHbDti2fGoSw2hdmWCrMkOnqh2h/iO6Y0YAPyy+husT7KBGPcCP7TnoNc evo6HTP0tW2OfHiWsTX7JnJYk8qL+uszDMDpjtdL6oqhdphEOpsviFaquCnHaKV83rII AaEXVmpM84gwrt1nACDjS4jwCJdohRQJYNequoO59XXBpqL9xji+j1NYGM44cX9+NbPK CIJbcKtNYYI6fRs1C6BL78nDjna5EP4TTrdfjlZrZukuDiaTzxVxpe6p3PseRnLIB9ZW DjNw==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:cc; bh=2LDLD5r3cccrHChxbKl2hquNIOpDZ738vcoyhZlF28I=; b=jyMqhIV4PI+UC0PiRniChyBtYVvJIisC/N69laeEKSoyFT1FjUhn0K3AwLphj41KOo wItlKiDrTLvpVodyWjki5l92JDw1IBKi2HiQN+MPp6nNF8hUmlRKWbRT69jRhcY8gpIQ BbTZGO0n04PgiiapAZaTuxtAV8w772DJ+QtiP5XAePmsApb5AU+y3+DkSf8WQS8gXtES eVI4H88RO+K+d5FZpaEbA0Xca4+hDREKyUW492qjCm7nQbyul+GXH8xMjvdQ56+xPN2N Y9D89tDDzgmTK26EMxVvBXOdjAKFlQ0IV2NcMWmGlcp+UercGwOt5X7xd7Nf/oB6gSV2 oABQ==
X-Gm-Message-State ALyK8tK78ml39jxFleoGwPoVb+1p1K0o2UVTiER2hCsb1gMpmztMyE1NNAl4Hfc2r3sTiH/IKa60CyOOSnINNw==
X-Received by 10.194.34.202 with SMTP id b10mr80612wjj.47.1467073956944; Mon, 27 Jun 2016 17:32:36 -0700 (PDT)
In-Reply-To <d5e97a5e-6f10-4e3f-ab24-113632f16f29@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <CAPTjJmqLmg5tPhLnvpMOg_+NhYiNOZW_RG2yDbBiqD6MYqtxSg@mail.gmail.com>
X-Mailman-Original-References <d5e97a5e-6f10-4e3f-ab24-113632f16f29@googlegroups.com>
Xref csiph.com comp.lang.python:110624

Show key headers only | View raw


On Tue, Jun 28, 2016 at 6:56 AM, Pavel S <pavel@schon.cz> wrote:
> By a mistake, I forgot to put comma into '__all__' tuple of some module. Notice missing comma after 'B'.
>
> # module foo.py
> __all__ = (
>     'A',
>     'B'
>     'C',
> )
>
> class A: pass
> class B: pass
> class C: pass
>
> If you try to import * from the module, it will raise an error, because 'B' and 'C' will be concatenated into 'BC'.
>
>>>> from foo import *
> AttributeError: 'module' object has no attribute 'BC'
>
> The bug won't be found until someone imports *.

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

@all
class A: pass
@all
class B: pass
@all
class C: pass
@all
def d(): pass

del all # clean up the namespace (optional)


The decorator doesn't change anything (it returns its argument as-is),
but it captures the canonical name into __all__. Obviously you can't
use this if you want a non-canonical name, and you can't use it for
anything other than classes and functions (you can't decorate "pi =
3.14159"), but it might help with your actual problem.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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