[Python-ideas] proposal: module-level __init__

M.-A. Lemburg mal at egenix.com
Sat May 7 19:56:50 CEST 2011


Daniel Holth wrote:
> __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__()

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/



More information about the Python-ideas mailing list