Created
January 8, 2026 03:14
-
-
Save codewings/680dffc2289065037d18da884417a06e to your computer and use it in GitHub Desktop.
Python private module function decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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