[Python-ideas] proposal: module-level __init__

3 views
Skip to first unread message

Daniel Holth

unread,
May 7, 2011, 1:49:58 PM5/7/11
to python...@python.org
__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__()

M.-A. Lemburg

unread,
May 7, 2011, 1:56:50 PM5/7/11
to Daniel Holth, python...@python.org

This is already possible and used in modules where you don't
want to clutter up the global namespace. Where's the novelty ?

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, May 07 2011)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________
2011-06-20: EuroPython 2011, Florence, Italy 44 days to go

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
_______________________________________________
Python-ideas mailing list
Python...@python.org
http://mail.python.org/mailman/listinfo/python-ideas

Reply all
Reply to author
Forward
0 new messages