Question presented to candidate:
"Walk me through this signature: def connect(host, port=5432, /, timeout=10, *, retries=3). What do the slash and the star mean, how are the defaults stored, and why would an API designer choose positional-only or keyword-only parameters?"
What a strong answer should cover:
- The five parameter kinds: positional-only (before
/, Python 3.8+, PEP 570), positional-or-keyword,*args, keyword-only (after*or*args, Python 3.0+, PEP 3102),**kwargs - Defaults are evaluated once when
defruns and stored on the function (__defaults__,__kwdefaults__); that is why mutable defaults are shared - Ordering rules: a parameter without a default cannot follow one with a default among positional parameters, but keyword-only parameters can be required (no default) in any order
- Why keyword-only: readable call sites for flags and options (
retries=3instead of a mystery3); why positional-only: the name is not part of the API, so it can be renamed, and the name stays free for**kwargs, matching builtins likelen()anddivmod() - The exact
TypeErrormessages for each misuse, and thatinspect.signature/Signature.bindshow how Python maps arguments to parameters
Code / implementation expected: Yes β write a signature that uses / and *, and show which calls succeed and which raise TypeError.