Skip to content

Instantly share code, notes, and snippets.

@un1tz3r0
Last active February 9, 2021 09:26
Show Gist options
  • Save un1tz3r0/4cda0988a1655745bb636988fc4eae87 to your computer and use it in GitHub Desktop.
Save un1tz3r0/4cda0988a1655745bb636988fc4eae87 to your computer and use it in GitHub Desktop.
Private/Public dispatch decorators in Python
import inspect
from functools import wraps
def privatemethod(func):
"""decorator for making an instance method private by dispatching on whether the name 'self' in the local namespace of the calling frame is a reference to the same object as the first positional argument (the self argument when invoking an instance method.) the decorated function will have a 'public' member which is a decorator for specifying an alternate implementation to dispatch calls from outside of the instance."""
pubfunc = None
privfunc = func
@wraps(func)
def func_wrapper(*args, **kwargs):
"""decorator wrapper function which checks whether the calling frames 'self' refers to the same instance as the first argument. if the check fails, by default an exception is thrown, but this can be overridden by providing an alternate public implementation (see privatemethod.public decorator)."""
nonlocal pubfunc
nonlocal privfunc
outer_frame = inspect.stack()[1][0]
if 'self' not in outer_frame.f_locals or outer_frame.f_locals['self'] is not args[0]:
if pubfunc is None:
raise Exception('%s.%s is a private method' % (args[0].__class__.__name__, func.__name__))
else:
return pubfunc(*args, **kwargs)
else:
return privfunc(*args, **kwargs)
def publicdecorator(secondfunc):
"""A decorator for providing a public implementation of the method decorated by @privatemethod, which by default will throw an exception if called from anywhere other than a method of the same instance."""
nonlocal pubfunc
pubfunc = secondfunc
return func_wrapper
func_wrapper.public = publicdecorator
return func_wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment