有没有办法限制特定功能调用的函数?
def a():
private() # okay
def b():
private() # raises error
def private():
print "private"
答案 0 :(得分:3)
import inspect
def private():
cframe = inspect.currentframe()
func = inspect.getframeinfo(cframe.f_back).function
if func != 'a':
print 'not allowed from ', func
print "private"