Derek
Derek

Reputation: 12378

Is there a way to limit a function to be called by a specific function?

Is there a way to limit a function to be called by specific function(s)?

def a():
    private() # okay

def b():
    private() # raises error

def private():
    print "private"

Upvotes: 3

Views: 80

Answers (1)

perreal
perreal

Reputation: 97948

import inspect
def private():
    cframe = inspect.currentframe()
    func = inspect.getframeinfo(cframe.f_back).function
    if func != 'a':
        print 'not allowed from ', func
    print "private"

Upvotes: 3

Related Questions