Why functools.wraps Matters

Easy ⏱ 8 min 75% acceptance ★★★★★ 4.9
Write two decorators, bare(func) (plain wrapper, no wraps) and proper(func) (with @wraps(func)), each just delegating. Then names(f) returns f.__name__ after decoration: bare-decorated functions report 'wrapper', proper ones keep their own name — the debugging/tooling reason wraps exists.

Examples

Example 1
Input
names(bare(greet)), names(proper(greet))
Output
('wrapper', 'greet')
Explanation

wraps copied the metadata; the bare one lost it.

Constraints

  • Both decorators must delegate correctly.
  • Only the wraps usage differs.

Topics

Decoratorsfunctools.wraps

Companies

MicrosoftJetBrainsGitLab

Hints

Hint 1

@wraps(func) sits on the inner wrapper.

Hint 2

__doc__ and __qualname__ are copied too.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.