All jobs / Automation developer
Automation developer interview question · 1–3 years
What is a decorator, really?
The strongest answer
A function that takes a function and returns a replacement — @log above def f is just f = log(f), which is why the wrapper should carry functools.wraps
Why it works
Right, and wraps is the part people leave out until a name or docstring goes missing.
What a good answer covers
- @d over f means f = d(f).
- The decorator runs at definition time.
- functools.wraps keeps __name__ and __doc__.
- A decorator with arguments is one more layer.
Answers that fall short
- Close, but not the strongest: Special syntax the interpreter handles; it is not an ordinary function
It is plain function application with sugar over it. - Weak: A way to mark a function for later, like an annotation
It runs at definition time and replaces the name. - Red flag: A class feature that does not apply to plain functions
It works on any callable.
Who gets asked this
- Backend developer
- Full-stack developer
- Automation developer
- DevOps / SRE / cloud
- Data analyst / BI
- Data engineer
- Data science / ML / AI
- AI engineer
More Automation developer questions
- You need setup and guaranteed teardown around a block. What do you write?
- Can you rely on a dict keeping insertion order?
- One synchronous database call inside an async handler makes the whole service slow. Why?
- Three independent API calls run one after another in an async function. How do you overlap them?
- How do you compose several transformation stages over a huge stream without buffering?
- A Python service does heavy numeric work per request and latency is unacceptable. What options do you weigh?
- Under load your async service exhausts database connections. What went wrong?