Python inspect module learning, pythoninspect Module

Source: Internet
Author: User

Python inspect module learning, pythoninspect Module

Today we found an interesting feature in the Python inspect module, which allows us to conveniently view the source code in the Python library and know how the module is implemented, meet the needs of people like me who have a peek -.-

That is, getsource in inspect.

Its usage is as follows:

For example, to view The Zen of Python

We can:

In [1]: import inspect

In [2]: import this

The Zen of Python, by Tim Peters

Beautiful is better than uugly.

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 shoshould never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There shoshould 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 orfgchevgl.

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])

Oh, I found that the implementation method of this python little poem was like this. It created a dictionary and translated the strings that could not be understood into poems. It was actually geek's practice. 2333333

I think the inspect module should have other powerful functions, so I have read the following:

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. Duplicate tree

Where

Isxxx is a function that checks whether the object is xxx.

For example, check whether this is a module.

Yes

In [16]: I. ismodule (this)

Out [16]: True

Getxxx is the xxx attribute of the object.

For example

In [17]: I. getmodule (this)

Out [17]: <module 'c' from '/usr/lib/python2.7/this. pyc'>


There are some other things that seem to be related to stack tracing and so on.

In this way, the inspect module mainly provides functions such as viewing source code, checking types, obtaining object attributes, and stack parsing... I have not learned in detail yet, but I have understood the basic functions. If I want to use it in the future, I just need to check the document again (escape...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.