This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author barry
Recipients barry
Date 2016-03-24.02:33:15
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1458786797.31.0.0749407715279.issue26632@psf.upfronthosting.co.za>
In-reply-to
Content
This is probably terrible, but given how difficult it is to keep __all__'s up to date, maybe something like this can be useful.  I literally whipped this up in about 5 minutes, so sit back and watch the bikeshedding!

import sys

def public(thing):
    if isinstance(thing, str):
        mdict = sys._getframe(1).f_globals
        name = thing
    else:
        mdict = sys.modules[thing.__module__].__dict__
        name = thing.__name__
    dunder_all = mdict.setdefault('__all__', [])
    dunder_all.append(name)
    return thing


Then:

@public
def baz(a, b):
    return a + b

@public
def buz(c, d):
    return c / d

def qux(e, f):
    return e * f

class zup:
    pass

@public
class zap:
    pass

public('CONST1')
CONST1 = 3

CONST2 = 4

public('CONST3')
CONST3 = 5

Normally for any callable with an __name__, you can just decorate it with @public to add it to __all__.  Of course that doesn't worth with things like constants, thus the str argument.  But of course that also requires sys._getframe() so blech.
History
Date User Action Args
2016-03-24 02:33:17barrysetrecipients: + barry
2016-03-24 02:33:17barrysetmessageid: <1458786797.31.0.0749407715279.issue26632@psf.upfronthosting.co.za>
2016-03-24 02:33:17barrylinkissue26632 messages
2016-03-24 02:33:15barrycreate