##language:zh #pragma section-numbers off ##含有章节索引导航的 ZPyUG 文章通用模板 <> ## 默许导航,请保留 <> = who call me? = ##startInc 来自:: http://hi.baidu.com/limodou/blog/item/83f4b21937ed174043a9adb5.html {{{ #!python #PyOO magic usage @who_called_me def who_called_me(show_filename=False, out=None, indent=' '): def _wrapper(fn): def _inner_wrapper(*args, **kwargs): import sys import inspect output = out or sys.stdout assert hasattr(output, 'write'), \ 'argument \'out\' of function \'who_called_me\' must have a write method' index = 0 stack = inspect.stack() stack.reverse() # remove ourself from the stack list stack.pop() for record in stack: frame = record[0] line = frame.f_lineno func_name = frame.f_code.co_name if show_filename: descr = frame.f_code.co_filename else: descr = frame.f_globals["__name__"] print >>output, '%s%s@%s:%d' % (indent*index, descr, func_name, line) # to be safe explicitly delete the stack frame reference # @see http://docs.python.org/lib/inspect-stack.html del frame index += 1 del stack if hasattr(output, 'flush'): output.flush() return fn(*args, **kwargs) return _inner_wrapper return _wrapper }}} ##endInc ---- '''反馈'''