That won't work with at least some builtins written in C, and maybe
extension modules. I just checked 3.9 and str.count, and
inspect.signature fails with ValueError: no signature found for builtin
.
I don't know if Argument Clinic (AC) would improve this, or maybe it's
outside of what AC can
I wouldn't call it tricky, it's actually quite straightforward:
import inspect
def extract_default(function, parameter):
sig = inspect.signature(function)
param = sig.parameters[parameter]
return param.default
def do_something(count=5):
print(count)
I've been toying with a similar idea myself. I've felt the pain
described by Brian, and I share Marco's dislike for the suggested
syntax. Moreover, I dislike the idea that the conditional should
somehow refer to the function's default arguments.
My half-baked idea is along the lines of
I had many times the same idea: why can't we just "say" to the called
function "use your own default"? I'm quite sure this is possible in a
tricky way, since defaults are stored in the function object.
Anyway, honestly I don't like your syntax proposal.