您的位置:首页 > 其它

How to inspect who is caller of func and who is the class of instance

2015-06-15 16:51 585 查看
1. Who is the class of self instance ?

class aa(object):
def a(self):

if self.__class__.__name__ == 'aa':
print "aa, a func()"
elif self.__class__.__name__ == "bb":
print "bb, a func()"

class bb(aa):
def b(self):
print "aa, b func()"

aa().a()
bb().a()

---------------------------------------------------------------------
result :
aa, a func()
bb, a func()


2.Who is the caller of function

import inspect

class aa(object):
def a(self):
frame = inspect.currentframe()

print "The caller is %s" %frame.f_back.f_code.co_name

def callerOfa(self):
self.a()

aa().callerOfa()
aa().a()

---------------------------------------------------------------
result:
The caller is callerOfa
The caller is <module>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: