我想确定在Python中调用函数的拥有类。例如,我有两个类,ClassA和ClassB。我想知道什么时候classb_instance.call_class_a_method()是class_a.class_a_method的调用者(),使得:如何找到在python中调用函数的类?
class ClassA(object):
def class_a_method(self):
# Some unknown process would occur here to
# define caller.
if caller.__class__ == ClassB:
print 'ClassB is calling.'
else:
print 'ClassB is not calling.'
class ClassB(object):
def __init__(self):
self.class_a_instance = ClassA()
def call_class_a_method(self):
self.class_a_instance.class_a_method()
classa_instance = ClassA()
classa_instance.class_a_method()
classb_instance = ClassB()
classb_instance.call_class_a_method()
输出将是:
'ClassB is not calling.'
'ClassB is calling.'
它好像检查应能够做到这一点,但我不能解决问题。
为什么不把类作为变量传递? – kennytm 2010-08-01 19:29:59
如果您觉得您需要此解决方案,我强烈怀疑您做错了什么。 – 2010-08-01 19:31:42
什么是用例?这很奇怪..你为什么不使用继承?为什么你需要知道谁在打电话?你可以检查堆栈跟踪,但它不漂亮。 – mpen 2010-08-01 19:33:58