Python標準庫--inspect
這個模塊是針對模塊,類,方法,功能等對象提供些有用的方法。例如可以幫助我們檢查類的內容,檢查方法的代碼,提取和格式化方法的參數等。
#coding:utf8 import inspect import os class Test(object): """Test Class """ def test(self): self.fuc = lambda x:x class Testone(Test): pass #檢查類型,模塊,類,方法,生成器,代碼等都可以 print inspect.ismodule(os) print inspect.isclass(Test) print inspect.getdoc(Test) print inspect.getsourcefile(Test) #文件路徑 print inspect.getsourcelines(Test) #代碼塊,每行一個元素,組成數組 print inspect.getsource(Test) #代碼塊 帶縮進 #打印全局變量中的模塊對象 myglobals = {} myglobals.update(globals()) modules = [value for key, value in myglobals.items() if inspect.ismodule(value)] print modules #查看類的可調用方法 for name, value in inspect.getmembers(Test, callable): print " Callable:", name for name, value in inspect.getmembers(Test(), callable): print " Instance Callable:", name def hello(): print inspect.stack()[0][3] print inspect.stack() hello()
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!