Is it a good idea to use a closure instead of __all__
to limit the names exposed by a Python module? This would prevent programmers from accidentally using the wrong name for a module (import urllib; urllib.os.getlogin()
) as well as avoiding "from x import *
" namespace pollution as __all__
.
def _init_module():
global foo
import bar
def foo():
return bar.baz.operation()
class Quux(bar.baz.Splort): pass
_init_module(); del _init_module
vs. the same module using __all__
:
__all__ = ['foo']
import bar
def foo():
return bar.baz.operation()
class Quux(bar.baz.Splort): pass
Functions could just adopt this style to avoid polluting the module namespace:
def foo():
import bar
bar.baz.operation()
This might be helpful for a large package that wants to help users distinguish its API from the package's use of its and other modules' API during interactive introspection. On the other hand, maybe IPython should simply distinguish names in __all__
during tab completion, and more users should use an IDE that allows them to jump between files to see the definition of each name.