[Python-ideas] proposal: module-level __init__

Daniel Holth dholth at gmail.com
Sat May 7 19:49:58 CEST 2011


__all__ is very useful when doing import *, which is frowned upon. As an
alternative, allow modules to contain a function called __init__ that
defines that module's exported symbols by way of the global statement. By
importing modules that are used, but not intended to be exported, inside the
__init__ function, programmers avoid cases such as the unintentional
'somemodule.sys' (referring to a module by its non-canonical name) that
makes it harder to refactor larger projects.

Before:

__all__ = ['a', 'b']
import sys
def a(): pass
def b(): pass
def c(): pass

After:

def __init__():
    global a, b
    import sys
    def a(): pass
    def b(): pass
    def c(): pass

__init__()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110507/7c3ca413/attachment.html>


More information about the Python-ideas mailing list