[Python] standard library-inspect

Source: Internet
Author: User

Inspect Module

On infoq yesterday, I saw this module mentioned in the speech of Douban architect. I am looking for a document today.

DOC: write it like this

The inspect module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, Frame Objects, and code objects.

This module provides some useful methods for objects such as modules, classes, methods, and functions. For example, it can help us check the content of the class andCodeParameters of the extraction and formatting methods.

 

# Coding: utf8 import inspectimport OS class test (object): "test class" "def test (Self): Self. FUC = Lambda X: X class testone (TEST): pass # Check type, module, class, method, generator, code, etc. can all print inspect. ismodule (OS) print inspect. isclass (TEST) print inspect. getdoc (TEST) print inspect. getsourcefile (TEST) # file path print inspect. getsourcelines (TEST) # code block. Each line contains one element to form an array of print inspect. getsource (TEST) # code block indent # print the module object in the global variable myglobals ={} my Globals. update (globals () Modules = [value for key, value in myglobals. items () If inspect. ismodule (value)] print modules # callable Method for viewing classes for name, value in inspect. getmembers (test, callable): Print "callable:", namefor name, value in inspect. getmembers (test (), callable): Print "instance callable:", namedef Hello (): Print inspect. stack () [0] [3] print inspect. the usage of stack () Hello () in a specific project is unknown.

 

 

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.