The inspect module provides four main uses:
1. Type check for module, frame, function
2. Get the source code
3. Get parameter information for a class or function
4. Parsing the Stack
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 has __get__ and __set__ properties, and usually __name__ and __doc __ Property
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)
span>
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])
Python--inspect Module