說我有一個模塊,像這樣:得到我自己的模塊定義的所有方法
def print_hello(name):
" prints a greeting "
print("Hello, {}!".format(name.title()))
我想成立一個sys.settrace我的模塊上,所以每當從我的模塊功能被稱爲,它打印到標準輸出的塊,像這樣
CALLED FUNCTION: say_hello()
Hello, Alex!
預見的是,跟蹤方法將拿起這是所謂的所有方法,這導致這樣的:
$ python3 trace_example.py
Called function: print_hello
Hello, Alex!
Called function: _remove
如何判斷某個方法是否在當前模塊中?
(這裏是我的跟蹤功能,如果你想知道:)
import sys
def tracefunc(frame, event, args):
if event == 'call':
print("Called function: {}()".format(frame.f_code.co_name))
sys.settrace(tracefunc)
也許這樣,使用'''inspect''' - [從棧幀中檢索模塊對象](http://stackoverflow.com/questions/2000861/retrieve-module-object -from-stack-frame) – wwii 2014-12-02 14:45:21
雖然有幫助,但仍有以下問題: print(dir(this_module)) '['__builtins__','__cached__','__doc__','__file__','__loader__ '','__name__','__package__','__spec__','inspect','print_hello','sys','tracefunc']' 理想情況下,它只有'print_hello'和'tracefunc' – corvid 2014-12-02 14:50:46