码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-06-15 18:05:28      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

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>

 

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

标签:

原文地址:http://www.cnblogs.com/root-wang/p/4578639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!