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


Groups > comp.lang.python > #110624

Re: __all__ attribute: bug and proposal

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: __all__ attribute: bug and proposal
Date 2016-06-28 10:32 +1000
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>

Show all headers | 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