I would like to write a decorator for a python class method that can determine if a method is called from a public context or a private context. For example, given the following code
def public_check_decorator(f):
def wrapper(self):
if self.f is `called publicly`:
print 'called publicly'
else:
print 'called privately'
return f(self)
return wrapper
class C(object):
@public_check_decorator
def public_method(self):
pass
def calls_public_method(self):
self.public_method()
Executing an execution would ideally look something like this:
>>> c = C()
>>> c.public_method()
called publicly
>>> c.calls_public_method()
called privately
Is there any way to do this in python? That is, change the line
if self.f is `called publicly`: # <-- how do I make this line work correctly?
to give the desired result?
source
share