Intermediate Course
Lesson 4 of 100

@functools.wraps — Preserving Function Identity

Fix the hidden problem with basic decorators — they disguise the function they wrap. functools.wraps restores the function's true identity.

Pyfessor

Lesson Briefing

Pyfessor

"Your decorators from lesson 3 work perfectly — but they have a hidden flaw. When you decorate add_expense, the result's __name__ is 'wrapper', not 'add_expense'. Its __doc__ is None. Its __module__ is wrong. This matters: your logging shows the wrong name, help() shows nothing useful, debugging is confusing, and testing frameworks get confused. functools.wraps fixes all of this with a single line. It's so important that every real decorator in Python uses it."

Pyfessor

"Hi! I'm Pyfessor. Ready to s-s-s-tart our coding journey?"

Pyfessor
    Pyfessor AI — Learn Python with Your AI Mentor