Python inspect Module

Source: Internet
Author: User

I found that python has an easy-to-use check module-inspect. Looking at the source file, I found that it provides a lot of useful methods:
""
Here are some of the useful functions provided by this module:

Ismodule (), isclass (), ismethod (), isfunction (), isgeneratorfunction (),
Isgenerator (), istraceback (), isframe (), iscode (), isbuiltin (), isroutine ()-check object types

Getmembers ()-Get members of an object that satisfy a given condition

GetFile (), getsourcefile (), getsource ()-find an object's source code
Getdoc (), getcomments ()-Get documentation on an object
Getmodule ()-determine the module that an object came from
Getclasstree ()-arrange classes so as to represent their hierarchy

Getargspec (), getargvalues ()-Get info about function arguments
Formatargspec (), formatargvalues ()-format an argument spec
Getouterframes (), getinnerframes ()-Get info about Frames
Currentframe ()-Get the current stack frame
Stack (), trace ()-Get info about frames on the stack or in a traceback
""

Example:

import inspectinspect.isbuiltin(abs)Trueimport osinspect.ismodule(os)Trueclass C:passinspect.isclass(C)Trueclass C(object):    def foo(self):        print 'here is foo'    def bez(self):        print 'here is bez'inspect.getmembers(C, inspect.ismethod)[('bez', "<"unbound method C.bez">"), ('foo', "<"unbound method C.foo">")]

If inspect. ismethod is not added, all default attributes of the object will be listed.

Other methods are listed one by one.

Original article: http://justpy.com/archives/tag/%E5%88%A4%E6%96%AD%E5%AF%B9%E8%B1%A1%E6%98%AF%E5%90%A6%E6%98%AF%E7%B1%BB

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.