__all__ attribute: bug and proposal

Chris Angelico rosuav at gmail.com
Mon Jun 27 22:20:48 EDT 2016


On Tue, Jun 28, 2016 at 11:55 AM, MRAB <python at mrabarnett.plus.com> wrote:
> On 2016-06-28 01:32, Chris Angelico wrote:
> [snip]
>
>> 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
>>
> Err... won't that hide the 'all' builtin?
>

Yep. So? If you're not using it *at top level*, it's not a problem. I
clean up the namespace at the end, so if you use it inside a function,
it'll still be available. And most of my scripts don't use 'all'
anyway... it's no worse than using 'id' for a database ID.

ChrisA



More information about the Python-list mailing list