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

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>

 

posted on 2015-06-15 16:51  Newbie wang  阅读(179)  评论(0编辑  收藏  举报