Skip to content

Instantly share code, notes, and snippets.

@didip
Created January 2, 2013 03:45
Show Gist options
  • Save didip/4431964 to your computer and use it in GitHub Desktop.
Save didip/4431964 to your computer and use it in GitHub Desktop.
Use decorator to avoid retyping function/class names into __all__ module variable. It's an effective way to cut down large number of unnecessary imports caused by "from some_module import *". Works on Python 2.6 and greater.
import sys
def add_to__all__(func_or_class):
""""Use decorator to avoid retyping function/class names into __all__ module level variable."""
all_list = sys.modules[func_or_class.__module__].__dict__.setdefault('__all__', [])
if func_or_class.__name__ not in all_list:
all_list.append(func_or_class.__name__)
return func_or_class
#
# Class Decoration Example:
#
# @add_to__all__
# class Application(object):
# pass
#
# Function Decoration Example:
#
# @add_to__all__
# def fabric_deploy_app():
# pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment