Python Inspect Module Learning

Source: Internet
Author: User
Tags stack trace

Today we found an interesting feature in the python inspect module that allows us to easily view the source code in the Python library. know how the module is implemented , satisfying people like me who are peeping-.-

That's the GetSource in inspect.

Its usage is as follows :

For example, to view python 's 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 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 is one--and preferably only one--obvious the-do it.

Although that is 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 could be a good idea.

Namespaces is one honking great idea – let's do more than 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)% + C)

Print "". Join ([D.get (c, c) for C in S])

Oh, I found the original python The way a little poem is realized is this. , it creates a dictionary and translates the original string into a poem . ,  sure is . Geek the practice Ah 2333333

Think the inspect module should have other powerful features , so I looked under and he had :

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

which

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

For example, check if this is a module

It is

in [+]: I.ismodule (this)

OUT[16]: True

GetXXX and the like is getting the XXX property of the object.

For example

in [+]: I.getmodule (this)

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


There's something else that looks like a stack trace, and it's not going to work.

In this view , the inspect module function is mainly to see the source code , type check , Get object properties and Stack resolution ... Have not been able to learn in detail , just understand the basic role , in the future to use the words to see the document is good (escape ...

Python Inspect Module Learning

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.