Python functional closures without performance hit
Question: In my python program, I have a ton of functions that are really wrappers for
more complicated functions (the more complicated functions take more
arguments, so the simple functions calculate the extra arguments and pass them
along with the original arguments to the complex functions). I don't want the
more complicated functions to be visible from the outer scope. However, my
understanding is that if you define a function inside a function every time
the outer function gets called it redefines the inner function, which is
wasteful. How can I hide my inner functions without redefining them over and
over again? There must be some way for the interpreter to parse my file and
just do the definitions once but still keep them in the inner scope.


Answer: Rather than controlling access to your _"inner functions"_ by nesting them,
use either or both of:

  * naming conventions (a leading underscore on a name means private-by-convention, see [the style guide](https://www.python.org/dev/peps/pep-0008/#method-names-and-instance-variables)); and 
  * defining a list named `__all__` to specify what gets imported from the package by default (see [the tutorial on modules](https://docs.python.org/2/tutorial/modules.html#importing-from-a-package)).

In use:

    
    
    # define the names that get imported from this package
    __all__ = ['outer_func']`
    
    def _inner_func(...):
        """Private-by-convention inner function.""" 
        ...
    
    def outer_func(...):
        """Public outer function to call _inner_func."""
        ...
    

This makes testing much easier, too, as you can still get direct access to
`_inner_func` when necessary.

