Python Glossary Русский

all Python 3until 3.11Removed from the standard libraryfunction

inspect.getargspec()

removed

Described a function's parameters as an ArgSpec tuple. Removed in Python 3.11; use inspect.signature() or inspect.getfullargspec().

Syntax

inspect.getargspec(func)  # до 3.11

getargspec() returned ArgSpec(args, varargs, keywords, defaults) and could not describe annotations or keyword-only parameters introduced in Python 3; it raised ValueError on such a function. So it was deprecated as early as 3.0 and removed in 3.11. How to rewrite: the main path is sig = inspect.signature(f) and walking sig.parameters (name, kind, default, annotation). If the code expects a tuple, switch to inspect.getfullargspec(f): the keywords field is named varkw there, plus kwonlyargs, kwonlydefaults and annotations.

Examples

# Python 3.11+
import inspect
try:
    inspect.getargspec
except AttributeError as e:
    print(type(e).__name__)  # → AttributeError
# Python 3.11+
import inspect
def f(a, b=1, *args, **kw):
    pass
print(list(inspect.signature(f).parameters))  # → ['a', 'b', 'args', 'kw']
spec = inspect.getfullargspec(f)
print(spec.args, spec.varkw, spec.defaults)  # → ['a', 'b'] kw (1,)
import inspect
def f(a, b=1, *args, **kw):
    pass
spec = inspect.getargspec(f)
print(spec.args, spec.keywords, spec.defaults)  # → ['a', 'b'] kw (1,)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy