今天发现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 pbzcygrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
Nygubhtu cenpgyvgl 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]:
还有一些其他的, 看起来跟堆栈跟踪之类有关的啦, 没有去用
这样看来, inspect模块功能主要是有查看源代码,类型检查,获取对象属性和堆栈解析等作用...还未能详细地学习, 只是了解了基本的作用, 日后要用到的话再查看文档就好啦(逃...