Python Developer Interview Question

How would you write a decorator that takes its own arguments?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

You add a third layer. The outer function takes the decorator arguments and returns the actual decorator, which takes the function and returns a wrapper that calls it. Apply functools.wraps to the wrapper so the name, docstring, and signature metadata survive. For example, a retry decorator takes the attempt count at the outer layer and the wrapper implements the loop.

Why interviewers ask this

Decorators test whether you are comfortable treating functions as values and closing over state. The parameterized version raises the difficulty because you have to keep three levels straight while explaining out loud. Interviewers also listen for functools.wraps, since forgetting it breaks introspection, help output, and anything that keys off the function name, which is a real problem in frameworks that register routes or tasks.

How to structure your answer

  • Describe the three nested layers in order.
  • Say what each layer receives and returns.
  • Mention functools.wraps and why it matters.
  • Ground it in a decorator you have actually written.

Example answer

Spoken example, first person

Three layers, and I say them out loud in order so I do not lose track. The outermost function takes the configuration, say the number of retries. It returns the decorator, which takes the target function. That returns the wrapper, which takes star args and star star kwargs, does the work around the call, and returns the result. I always put functools.wraps on the wrapper, otherwise the decorated function reports itself as wrapper everywhere, and Flask or Celery will happily register two routes under the same name and fail in a confusing way. The one I have written most often is a retry with exponential backoff for flaky upstream calls: three attempts, sleeping a doubling interval, re raising on the last one. A useful refinement is accepting the function directly as well, so it works with and without parentheses, though honestly I usually just require the parentheses because the branching logic costs more clarity than it saves.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • How would you make that decorator work on async functions?
  • What does functools.wraps actually copy?
  • When would a class based decorator be better?

Related python developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot