inspect
Inspects an object and returns a structured representation of its attributes and methods.
This function analyzes the given object and returns either a Function, Class, or Method which
encapsulates the object's structure, including its name, parameters, docstring, and other relevant information.
The inspection can be customized to include or exclude certain types of attributes and methods
based on the provided boolean flags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
object
|
The object to be inspected. |
required |
init
|
bool
|
Whether to include the init method for classes. |
True
|
public
|
bool
|
Whether to include public attributes and methods. |
True
|
inherited
|
bool
|
Whether to include inherited attributes and methods. |
True
|
static_methods
|
bool
|
Whether to include static methods. |
True
|
protected
|
bool
|
Whether to include protected attributes and methods (prefixed with _). |
False
|
private
|
bool
|
Whether to include private attributes and methods (prefixed with __). |
False
|
Returns:
| Type | Description |
|---|---|
Function | Class | Method
|
An object representing the structure of the inspected object. |
Example:
>>> from objinspect import inspect, pdir
>>> import math
>>> inspect(math.pow)
Function(name='pow', parameters=2, description='Return x**y (x to the power of y).')
>>> inspect(math.pow).dict
{
'name': 'pow',
'parameters': [
{'name': 'x', 'kind': <_ParameterKind.POSITIONAL_ONLY: 0>, 'type': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>, 'description': None},
{'name': 'y', 'kind': <_ParameterKind.POSITIONAL_ONLY: 0>, 'type': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>, 'description': None}],
'docstring': 'Return x**y (x to the power of y).'
}
>>> inspect(inspect)
Function(name='inspect', parameters=7, description='Inspects an object and returns a structured representation of its attributes and methods.')