Python's Inspect module

Source: Internet
Author: User
Tags generator

I. Type and members

1. Inspect. GetMembers (object[, predicate])

The second parameter can usually call the following 16 methods as required;

Returns a list of all members that have a value of object, in (name,value) pairs

Inspect.ismodule (object): Whether it is a module
Inspect.isclass (object): Whether it is a class
Inspect.ismethod (object): Whether it is a method (bound written in Python)
Inspect.isfunction (object): Whether it is a function (Python function, including lambda expression)
Inspect.isgeneratorfunction (object): Whether it is a Python generator function
Inspect.isgenerator (object): Whether it is a generator
Inspect.istraceback (object): Whether it is traceback
Inspect.isframe (object): Whether it is a frame
Inspect.iscode (object): Whether it is a code
Inspect.isbuiltin (object): Whether it is a built-in function or a built-in method
Inspect.isroutine (object): Whether the user has customized or built-in functions or methods
Inspect.isabstract (object): Whether it is an abstract base class
Inspect.ismethoddescriptor (object): is the method identifier
Inspect.isdatadescriptor (object): Whether it is a numeric identifier, a numeric identifier__get__And__set__ properties; __name__ and __doc__ properties are also common
Inspect.isgetsetdescriptor (object): Whether it is Getset descriptor
Inspect.ismemberdescriptor (object): Whether it is member descriptor

The inspect GetMembers () method can get the following properties of the object (module, class, method, and so on):

Type Attribute Description Notes
Module __doc__ Documentation string
__file__ FileName (missing for built-in modules)
Class __doc__ Documentation string
__module__ Name of module in which this class is defined
Method __doc__ Documentation string
__name__ Name with which this method is defined
Im_class Class object, asked for this method (1)
Im_func or __func__ function object containing implementation of method
Im_self or __self__ instance to which this method is bound, or None
function __doc__ Documentation string
__name__ Name with which this function is defined
Func_code Code object containing compiled function bytecode
Func_defaults Tuple of any default values for arguments
Func_doc (Same as __DOC__)
Func_globals Global namespace in which this function is defined
Func_name (Same as __NAME__)
Generator __iter__ Defined to support iteration over container
Close Raises new Generatorexit exception inside the generator to terminate the iteration
Gi_code Code Object
Gi_frame Frame object or possibly None once the generator has been exhausted
Gi_running Set to 1 when generator is executing, 0 otherwise
Next Return the next item from the container
Send Resumes the generator and "sends" a value that becomes the result of the current yield-expression
Throw Used to raise an exception inside the generator
Traceback Tb_frame Frame object at the This level
Tb_lasti Index of last attempted instruction in bytecode
Tb_lineno Current line number in Python source code
Tb_next Next inner Traceback object (called by)
Frame F_back Next Outer frame object (this frame ' s caller)
F_builtins Builtins namespace seen by this frame
F_code Code object being executed in the This frame
F_exc_traceback Traceback if raised in the this frame, or None
F_exc_type Exception type if raised in the this frame, or None
F_exc_value Exception value if raised in the this frame, or None
F_globals Global namespace seen by this frame
F_lasti Index of last attempted instruction in bytecode
F_lineno Current line number in Python source code
F_locals Local namespace seen by this frame
f_restricted 0 or 1 if frame is in restricted execution mode
F_trace Tracing function for the this frame, or None
Code Co_argcount Number of arguments (not including * or * * args)
Co_code String of raw compiled bytecode
Co_consts Tuple of constants used in the bytecode
Co_filename Name of file in which this code object is created
Co_firstlineno Number of first line in Python source code
Co_flags bitmap:1=optimized | 2=newlocals | 4=*arg | 8=**arg
Co_lnotab Encoded mapping of line numbers to bytecode indices
Co_name Name with which this code object is defined
Co_names Tuple of names of local variables
Co_nlocals Number of local variables
Co_stacksize Virtual Machine stack space required
Co_varnames Tuple of names of arguments and local variables
Builtin __doc__ Documentation string
__name__ Original name of this function or method
__self__ instance to which a method is bound, or None

2. Inspect. Getmoduleinf o (path): Returns a named tuple <named tuple> (name, suffix, mode, module_type)

Name: module name (excluding the package it resides in)

Suffix

Mode of the Mode:open () method, such as: ' R ', ' a ', etc.

Module_type: Integer that represents the type of the module

3. Inspect. Getmodulename (path): Returns the module name based on path (excluding the package it resides in)

Second, retrieving source code

1. Inspect. Getdoc (object): Gets the documentation information for object

2. Inspect. getcomments (object)

3. Inspect. GetFile (object): Returns the file name of the object

4. Inspect. GetModule (object): Returns the module name to which the object belongs

5. Inspect. Getsourcefile (object): Returns the Python source file name of object, object cannot make built-in module, class, Mothod

6. Inspect. Getsourcelines (object): Returns the contents of the Python source file code for object, line number + line of code

7. Inspect. GetSource (object): Returns the source code of object as a string

8. Inspect. Cleandoc (doc):

Iii. class and functions

1. Inspect. Getclasstree (classes[, unique])

2. Inspect. Getargspec (func)

3. Inspect. getargvalues (frame)

4. Inspect. Formatargspec (args[, varargs, varkw, defaults, Formatarg, Formatvarargs, formatvarkw, Formatvalue, join])

5. Inspect. formatargvalues (args[, VarArgs, varkw, locals, Formatarg, Formatvarargs, formatvarkw, Formatvalue, join])

6. Inspect. Getmro (cls): The tuple form returns the base class of the CLS class (including the CLS Class), in method resolution order, usually the CLS class as the first element of the element

7. Inspect. Getcallargs (func[, *args][, **kwds]): Converts the args and Kwds parameters to the name of the parameter that is bound to Func, and the bound method, Also binds the first argument (usually self) to the corresponding instance, and returns the dictionary, corresponding to the parameter name and its value;

>>>FromInspectImportGetcallargs>>>DefF(A,B=1,*Pos,**Named):...Pass>>>Getcallargs(F123) {' a ': 1, ' named ': {}, ' B ': 2, ' POS ': (3,)}>>>  Getcallargs (fa= 2x=4 {' a ': 2, ' named ': {' x ': 4}, ' B ': 1, ' pos ': ()}>>> getcallargs (f) traceback (most recent call last):  ... typeerror: f () takes at least 1 argument (0 given)     

Iv. The Interpreter stack

1. Inspect. Getframeinfo (frame[, context])

2. Inspect. Getouterframes (frame[, context])

3. Inspect. Getinnerframes (traceback[, Context])

4. Inspect. Currentframe ()

5. Inspect. Stack ([context])

6. Inspect. Trace ([context])

Turn from: 45670805

Python's Inspect module

Related Article

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.