Python Glossary Русский

Python 3.0+inspectfunction

inspect.getfullargspec()

signatures

The legacy way to read a function's parameters, as a FullArgSpec named tuple. Kept for compatibility; new code uses inspect.signature().

Syntax

inspect.getfullargspec(func)

It is not deprecated (the 3.5 decision was reversed in 3.6), but it misreports modern code: positional-only parameters silently land in args and the '/' is lost; __wrapped__ is ignored, so a function behind functools.wraps shows the wrapper's (*args, **kwargs); a bound method still lists self. signature() has none of these flaws. Its predecessor getargspec was removed in 3.11.

Examples

import inspect
def g(a, /, b, *, c=2): pass
spec = inspect.getfullargspec(g)
print(spec.args, spec.kwonlyargs, spec.kwonlydefaults)  # → ['a', 'b'] ['c'] {'c': 2} — «/» потерян
print(inspect.signature(g))  # → (a, /, b, *, c=2)
import functools, inspect
def deco(fn):
    @functools.wraps(fn)
    def wrapper(*args, **kwargs):
        return fn(*args, **kwargs)
    return wrapper
@deco
def h(x, y=2): pass
spec = inspect.getfullargspec(h)
print(spec.args, spec.varargs, spec.varkw)  # → [] args kwargs
print(inspect.signature(h))  # → (x, y=2)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy