# pydoc > inspect

---
type: CommandReference
command: inspect
mode: pydoc
section: 
source: pydoc3
---

## Quick Reference
- `inspect.ismodule(obj)` — check if object is a module
- `inspect.isclass(obj)` — check if object is a class
- `inspect.ismethod(obj)` — check if object is an instance method
- `inspect.isfunction(obj)` — check if object is a user-defined function
- `inspect.getmembers(obj)` — get sorted list of (name, value) pairs for object members
- `inspect.getsource(obj)` — return source code text of an object
- `inspect.getdoc(obj)` — get cleaned documentation string
- `inspect.signature(obj)` — get a `Signature` object for a callable
- `inspect.getfullargspec(func)` — get parameter names, defaults, annotations
- `inspect.currentframe()` — return the frame of the caller
- `inspect.stack()` — return list of records for the stack above caller
- `inspect.trace()` — return list of records for the stack below current exception

## Name
Get useful information from live Python objects.

## Synopsis
python
import inspect
## Functions

### Type Checking
- `ismodule(object)` — true if object is a module (has `__cached__`, `__doc__`, `__file__`)
- `isclass(object)` — true if object is a class (has `__doc__`, `__module__`)
- `ismethod(object)` — true if object is an instance method (has `__doc__`, `__name__`, `__func__`, `__self__`)
- `isfunction(object)` — true if user-defined function (has `__doc__`, `__name__`, `__code__`, `__defaults__`, `__globals__`, `__annotations__`, `__kwdefaults__`)
- `isgeneratorfunction(object)` — true if user-defined generator function
- `isgenerator(object)` — true if generator (has `__iter__`, `close`, `gi_code`, `gi_frame`, `gi_running`, `next`, `send`, `throw`)
- `iscoroutinefunction(object)` — true if defined with `async def`
- `iscoroutine(object)` — true if coroutine object
- `isasyncgenfunction(object)` — true if async generator function (`async def` with `yield`)
- `isasyncgen(object)` — true if async generator
- `istraceback(object)` — true if traceback (has `tb_frame`, `tb_lasti`, `tb_lineno`, `tb_next`)
- `isframe(object)` — true if frame (has `f_back`, `f_builtins`, `f_code`, `f_globals`, `f_lasti`, `f_lineno`, `f_locals`, `f_trace`)
- `iscode(object)` — true if code object (has `co_argcount`, `co_code`, `co_cellvars`, `co_consts`, `co_filename`, `co_firstlineno`, `co_flags`, `co_freevars`, `co_posonlyargcount`, `co_kwonlyargcount`, `co_lnotab`, `co_name`, `co_names`, `co_nlocals`, `co_stacksize`, `co_varnames`)
- `isbuiltin(object)` — true if built-in function or method (has `__doc__`, `__name__`, `__self__`)
- `isroutine(object)` — true if any kind of function or method
- `isabstract(object)` — true if abstract base class (ABC)
- `ismethoddescriptor(object)` — true if method descriptor (not `ismethod`, `isclass`, `isfunction`; has `__get__` but not `__set__`)
- `isdatadescriptor(object)` — true if data descriptor (has `__set__` or `__delete__`)
- `isgetsetdescriptor(object)` — true if getset descriptor (extension modules)
- `ismemberdescriptor(object)` — true if member descriptor (extension modules)
- `isawaitable(object)` — true if can be passed to `await`

### Member and Attribute Access
- `getmembers(object[, predicate])` — list of `(name, value)` pairs sorted by name; optionally filtered by predicate
- `getattr_static(obj, attr)` — retrieve attribute without triggering descriptor protocol, `__getattr__`, or `__getattribute__`
- `classify_class_attrs(cls)` — list of `(name, kind, defining_class, object)` tuples for each attribute in `dir(cls)`; kinds: `'class method'`, `'static method'`, `'property'`, `'method'`, `'data'`

### Source Code and File Location
- `getfile(object)` — return source or compiled file where object was defined
- `getabsfile(object)` — return absolute path to source or compiled file
- `getsourcefile(object)` — return filename that can locate object's source (or `None`)
- `getmodulename(file)` — return module name from a file path (or `None`)
- `getmodule(object)` — return module where object was defined (or `None`)
- `getsource(object)` — return source code text as a single string; raises `OSError` if not found
- `getsourcelines(object)` — return `(lines_list, starting_line_number)`
- `findsource(object)` — return `(lines_list, starting_line_number)` for the entire source file
- `getcomments(object)` — return lines of comments immediately preceding object's source (or `None`)
- `getdoc(object)` — return cleaned documentation string (tabs expanded, uniform indentation removed)
- `cleandoc(doc)` — clean up indentation from a docstring
- `getblock(lines)` — extract the block of code at the top of a list of lines
- `indentsize(line)` — return indent size in spaces at start of a line

### Function Signatures and Arguments
- `signature(callable, *, follow_wrapped=True, globals=None, locals=None, eval_str=False)` — return a `Signature` object
- `getfullargspec(func)` — return `FullArgSpec(args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations)`
- `getargspec(func)` — deprecated, use `getfullargspec`; returns `ArgSpec(args, varargs, keywords, defaults)`
- `getargs(code)` — for a code object, return `(args, varargs, varkw)`
- `getargvalues(frame)` — return `ArgInfo(args, varargs, keywords, locals)`
- `getcallargs(func, *args, **kwds)` — return dict mapping argument names to bound values
- `formatargspec(args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations, ...)` — format an argument spec from `getfullargspec` values
- `formatargvalues(args, varargs, varkw, locals, ...)` — format argument values from `getargvalues`
- `formatannotation(annotation)` — format an annotation
- `formatannotationrelativeto(annotation)` — format annotation relative to module

### Frame and Stack Inspection
- `currentframe()` — return frame of the caller (or `None`)
- `getframeinfo(frame_or_traceback, context=1)` — return `Traceback/FrameInfo` with filename, lineno, function, code_context, index
- `getouterframes(frame, context=1)` — list of `FrameInfo` for frame and all higher (calling) frames
- `getinnerframes(traceback, context=1)` — list of `FrameInfo` for traceback and all lower frames
- `stack(context=1)` — list of `FrameInfo` for stack above caller's frame
- `trace(context=1)` — list of `FrameInfo` for stack below current exception
- `getlineno(frame)` — get line number from frame, allowing for optimization

### Generator and Coroutine State
- `getgeneratorstate(generator)` — return `'GEN_CREATED'`, `'GEN_RUNNING'`, `'GEN_SUSPENDED'`, or `'GEN_CLOSED'`
- `getgeneratorlocals(generator)` — dict of local variable names to current values
- `getcoroutinestate(coroutine)` — return `'CORO_CREATED'`, `'CORO_RUNNING'`, `'CORO_SUSPENDED'`, or `'CORO_CLOSED'`
- `getcoroutinelocals(coroutine)` — dict of local variable names to current values

### Closure Variables
- `getclosurevars(func)` — return `ClosureVars(nonlocals, globals, builtins, unbound)`

### Annotations
- `get_annotations(obj, *, globals=None, locals=None, eval_str=False)` — safely compute annotations dict; handles stringized annotations, inherited annotations, and unwrapping

### Class Hierarchy
- `getclasstree(classes, unique=False)` — arrange list of classes into nested list hierarchy
- `getmro(cls)` — return tuple of base classes in method resolution order
- `walktree(tree)` — recursive helper for `getclasstree`

### Miscellaneous
- `unwrap(func, *, stop=None)` — follow `__wrapped__` chain to the last object; `stop` callback can terminate early; raises `ValueError` on cycle

## Classes

### `ArgInfo(args, varargs, keywords, locals)`
Named tuple with fields: `args`, `varargs`, `keywords`, `locals`. Methods: `_asdict()`, `_replace()`, `_make()`.

### `ArgSpec(args, varargs, keywords, defaults)`
Named tuple with fields: `args`, `varargs`, `keywords`, `defaults`. Methods: `_asdict()`, `_replace()`, `_make()`.

### `Arguments(args, varargs, varkw)`
Named tuple with fields: `args`, `varargs`, `varkw`. Methods: `_asdict()`, `_replace()`, `_make()`.

### `Attribute(name, kind, defining_class, object)`
Named tuple with fields: `name`, `kind`, `defining_class`, `object`. Methods: `_asdict()`, `_replace()`, `_make()`.

### `BlockFinder`
Provides `tokeneater()` method to detect end of a code block. Attributes: `__dict__`, `__weakref__`.

### `BoundArguments(signature, arguments)`
Result of `Signature.bind()`. Attributes: `arguments` (dict), `signature` (Signature), `args` (tuple), `kwargs` (dict). Methods: `apply_defaults()` (sets missing arguments to their defaults). Properties: `args`, `kwargs`, `signature` (readonly). `__hash__` is `None`.

### `ClassFoundException`
Exception class. Inherits `Exception`.

### `ClosureVars(nonlocals, globals, builtins, unbound)`
Named tuple with fields: `nonlocals`, `globals`, `builtins`, `unbound`. Methods: `_asdict()`, `_replace()`, `_make()`.

### `EndOfBlock`
Exception class. Inherits `Exception`.

### `FrameInfo(frame, filename, lineno, function, code_context, index)`
Named tuple with fields: `frame`, `filename`, `lineno`, `function`, `code_context`, `index`. Methods: `_asdict()`, `_replace()`, `_make()`.

### `FullArgSpec(args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations)`
Named tuple with fields: `args`, `varargs`, `varkw`, `defaults`, `kwonlyargs`, `kwonlydefaults`, `annotations`. Methods: `_asdict()`, `_replace()`, `_make()`.

### `Parameter(name, kind, *, default, annotation)`
Represents a function parameter. Attributes: `name` (str), `default` (object or `Parameter.empty`), `annotation` (object or `Parameter.empty`), `kind` (one of `POSITIONAL_ONLY`, `POSITIONAL_OR_KEYWORD`, `VAR_POSITIONAL`, `KEYWORD_ONLY`, `VAR_KEYWORD`). Class attributes: `POSITIONAL_ONLY`, `POSITIONAL_OR_KEYWORD`, `VAR_POSITIONAL`, `KEYWORD_ONLY`, `VAR_KEYWORD`, `empty`. Methods: `replace(name=..., kind=..., annotation=..., default=...)` (creates copy). Properties: `name`, `kind`, `default`, `annotation` (readonly). Supports `__eq__`, `__hash__`, `__reduce__`, `__setstate__`, `__repr__`, `__str__`.

### `Signature(parameters=None, *, return_annotation)`
Represents a function signature. Attributes: `parameters` (OrderedDict of `Parameter` objects), `return_annotation` (object or `Signature.empty`). Methods: `bind(*args, **kwargs)` -> `BoundArguments`, `bind_partial(*args, **kwargs)` -> `BoundArguments`, `replace(parameters=..., return_annotation=...)` (creates copy). Class methods: `from_callable(obj, *, follow_wrapped=True, globals=None, locals=None, eval_str=False)` (constructs Signature for any callable), `from_function(func)` (deprecated), `from_builtin(func)` (deprecated). Properties: `parameters`, `return_annotation` (readonly). Class attribute: `empty`.

### `Traceback(filename, lineno, function, code_context, index)`
Named tuple with fields: `filename`, `lineno`, `function`, `code_context`, `index`. Methods: `_asdict()`, `_replace()`, `_make()`.

## Data
- `CORO_CLOSED`, `CORO_CREATED`, `CORO_RUNNING`, `CORO_SUSPENDED` — coroutine state strings
- `GEN_CLOSED`, `GEN_CREATED`, `GEN_RUNNING`, `GEN_SUSPENDED` — generator state strings
- `CO_OPTIMIZED`, `CO_NEWLOCALS`, `CO_VARARGS`, `CO_VARKEYWORDS`, `CO_NESTED`, `CO_GENERATOR`, `CO_NOFREE`, `CO_COROUTINE`, `CO_ITERABLE_COROUTINE`, `CO_ASYNC_GENERATOR` — code object flag bitmasks
- `TPFLAGS_IS_ABSTRACT` — type flag for abstract classes

## See Also
- [Python documentation for inspect](https://docs.python.org/3.10/library/inspect.html)
- `functools` — for `partial` and `wraps` utilities
- `traceback` — for working with tracebacks
- `types` — for type utilities