Python inspect模組學習,pythoninspect模組
今天發現Python inspect模組中一個有趣的功能, 可以讓我們方便地檢視Python庫中的原始碼, 知道模組具體是怎樣實現的, 滿足了像我這樣有偷窺欲的人-.-
那就是inspect中的getsource
它的用法如下:
例如要檢視Python的The Zen of Python
我們可以:
In [1]: import inspect
In [2]: import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
In [3]: print inspect.getsource(this)
s = """Gur Mra bs Clguba, ol Gvz Crgref
Ornhgvshy vf orggre guna htyl.
Rkcyvpvg vf orggre guna vzcyvpvg.
Fvzcyr vf orggre guna pbzcyrk.
Pbzcyrk vf orggre guna pbzcyvpngrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
Nygubhtu cenpgvpnyvgl orngf chevgl.
Reebef fubhyq arire cnff fvyragyl.
Hayrff rkcyvpvgyl fvyraprq.
Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.
Abj vf orggre guna arire.
Nygubhtu arire vf bsgra orggre guna *evtug* abj.
Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.
Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!"""
d = {}
for c in (65, 97):
for i in range(26):
d[chr(i+c)] = chr((i+13) % 26 + c)
print "".join([d.get(c, c) for c in s])
噢, 我發現原來這首python小詩的實現方式是這樣的,它建立了一個字典然後把原本的看不懂的字串翻譯成詩, 果然是geek的做法啊2333333
覺得inspect模組應該還有其他強大的功能,所以我看了下, 他有:
inspect.ArgInfo inspect.getmoduleinfo
inspect.ArgSpec inspect.getmodulename
inspect.Arguments inspect.getmro
inspect.Attribute inspect.getouterframes
inspect.BlockFinder inspect.getsource
inspect.CO_GENERATOR inspect.getsourcefile
inspect.CO_NESTED inspect.getsourcelines
inspect.CO_NEWLOCALS inspect.imp
inspect.CO_NOFREE inspect.indentsize
inspect.CO_OPTIMIZED inspect.isabstract
inspect.CO_VARARGS inspect.isbuiltin
inspect.CO_VARKEYWORDS inspect.isclass
inspect.EndOfBlock inspect.iscode
inspect.ModuleInfo inspect.isdatadescriptor
inspect.TPFLAGS_IS_ABSTRACT inspect.isframe
inspect.Traceback inspect.isfunction
inspect.attrgetter inspect.isgenerator
inspect.classify_class_attrs inspect.isgeneratorfunction
inspect.cleandoc inspect.isgetsetdescriptor
inspect.currentframe inspect.ismemberdescriptor
inspect.dis inspect.ismethod
inspect.findsource inspect.ismethoddescriptor
inspect.formatargspec inspect.ismodule
inspect.formatargvalues inspect.isroutine
inspect.getabsfile inspect.istraceback
inspect.getargs inspect.joinseq
inspect.getargspec inspect.linecache
inspect.getargvalues inspect.modulesbyfile
inspect.getblock inspect.namedtuple
inspect.getcallargs inspect.os
inspect.getclasstree inspect.re
inspect.getcomments inspect.stack
inspect.getdoc inspect.string
inspect.getfile inspect.strseq
inspect.getframeinfo inspect.sys
inspect.getinnerframes inspect.tokenize
inspect.getlineno inspect.trace
inspect.getmembers inspect.types
inspect.getmodule inspect.walktree
其中
isxxx之類的就是檢查對象是否為xxx的函數吧
例如 檢查this是否是一個模組
就是
In [16]: i.ismodule(this)
Out[16]: True
getxxx之類的就是擷取對象的xxx屬性吧
例如
In [17]: i.getmodule(this)
Out[17]: <module 'this' from '/usr/lib/python2.7/this.pyc'>
還有一些其他的, 看起來跟堆疊追蹤之類有關的啦, 沒有去用
這樣看來, inspect模組功能主要是有查看原始碼,類型檢查,擷取對象屬性和堆棧解析等作用...還未能詳細地學習, 只是瞭解了基本的作用, 日後要用到的話再查看文檔就好啦(逃...