[Python-ideas] Idea: Deferred Default Arguments?
Brice Parent
contact at brice.xyz
Fri Jul 20 10:41:02 EDT 2018
It might be stupid, but how about solving this problem using the following:
from . import other_func, SomeClass
def my_func(a=other_func.defaults.a, b=other_func.defaults.b,
c=SomeClass.some_method.defaults.c):
...
or
def my_func(a=None, b=None, c=None): # or use some sentinel value
instead of None
if a is None:
a = other_func.defaults.a
if b is None:
b = other_func.defaults.b
if c is None:
c = SomeClass.some_method.defaults.c
...
or even
def my_func(a=None, b=None, c=None):
if a is None:
a = default(other_func, "a")
if b is None:
b = default(other_func, "b")
if c is None:
c = default(SomeClass.some_method, "c")
...
I used *.defaults.* but it might be something else, as well as the
function I named 'default' which might be anything else.
I prefer the first, as it's both short and easy to read, but I'm not
sure about the implications about such a thing. And it probably has
already been proposed for other use cases.
The second and third versions are more verbose, but probably easier to
implement, specially the third which should already be doable using
something like
import inspect
def default(function, argument):
return inspect.signature(function).parameters[argument].default.
More information about the Python-ideas
mailing list