Skip to content

Instantly share code, notes, and snippets.

@namper
Forked from latsa/private.py
Created April 21, 2021 20:40
Show Gist options
  • Save namper/2fb994d9db568b827db0f2fbfae66525 to your computer and use it in GitHub Desktop.
Save namper/2fb994d9db568b827db0f2fbfae66525 to your computer and use it in GitHub Desktop.
Python @Private decorator
import sys, functools
def private(member):
@functools.wraps(member)
def wrapper(*function_args):
myself = member.__name__
caller = sys._getframe(1).f_code.co_name
if (not caller in dir(function_args[0]) and not caller is myself):
raise Exception("%s called by %s is private"%(myself,caller))
return member(*function_args)
return wrapper
'''
class test:
def public_method(self):
print('public method called')
@private
def private_method(self):
print('private method called')
t = test()
t.public_method()
t.private_method()
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment