Skip to content

Instantly share code, notes, and snippets.

@codewings
Created January 8, 2026 03:14
Show Gist options
  • Select an option

  • Save codewings/680dffc2289065037d18da884417a06e to your computer and use it in GitHub Desktop.

Select an option

Save codewings/680dffc2289065037d18da884417a06e to your computer and use it in GitHub Desktop.
Python private module function decorator
def module_private(func):
@functools.wraps(func)
def wrapper(*function_args):
myself_module = func.__code__.co_filename
caller_module = sys._getframe(1).f_code.co_filename
if myself_module != caller_module:
raise Exception("Function %s cannot be called from external module %s" % (func.__name__, caller_module))
return func(*function_args)
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment