Python Glossary Русский

all Python 3inspectterm

inspect

module

Looks inside live objects: function signatures, source code, class members and stack frames.

Syntax

import inspect

Reach for inspect when code needs to learn something about other code that dir() and type() do not show: a function's parameters, their defaults, its source. Its centrepiece is inspect.signature, not the outdated getargspec and formatargspec. The trap is getsource: functions written in C and code typed into an interactive session have no source, and the call raises OSError or TypeError.

Examples

import inspect
def greet(name, greeting='Hi', *, loud=False):
    return f'{greeting}, {name}'
sig = inspect.signature(greet)
print(sig)  # → (name, greeting='Hi', *, loud=False)
print(list(sig.parameters))  # → ['name', 'greeting', 'loud']

See also

Open in the full glossarydocs.python.orgReport an inaccuracy