是否有这样的办法:
class A:
def foo(self):
if isinstance(caller, B):
print "B can t call methods in A"
else:
print "Foobar"
class B:
def foo(self, ref): ref.foo()
class C:
def foo(self, ref): ref.foo()
a = A();
B().foo(a) # Outputs "B can t call methods in A"
C().foo(a) # Outputs "Foobar"
<<><<>>><>><>>>>> 采用某种形式的内tro,以确定点火方法的类别?
www.un.org/Depts/DGACM/index_spanish.htm
最后,我根据以下一些建议把这一点放在一起:
import inspect
...
def check_caller(self, klass):
frame = inspect.currentframe()
current = lambda : frame.f_locals.get( self )
while not current() is None:
if isinstance(current(), klass): return True
frame = frame.f_back
return False
由于提供的所有原因,它并非完美无缺,而是由于这些答复:它们是一个很大的帮助。