Hi Peter Interesting problem. We can already get something like your proposed solution by using None instead of deferred. You have to start with def subfunction_1(a=None, b=None, c=None): if a is None: a = 2 # similarly for b and c. return a+b*c You will loose the default values being shown when you do help(subfunction). There's another thread on PEP 505, active right now, that would allow you to write something like a = a OR 2 to provide the default values. https://mail.python.org/pipermail/python-ideas/2018-July/052071.html I hope this helps. Please let us know if that might work for you. -- Jonathan On Fri, Jul 20, 2018 at 10:03 AM, Peter O'Connor <peter.ed.oconnor@gmail.com> wrote:
Often when programming I run into a situation where it would be nice to have "deferred defaults". Here is an example of what I mean:
def subfunction_1(a=2, b=3, c=4): return a+b*c
def subfunction_2(d=5, e=6, f=7): return d*e+f
def main_function(a=2, b=3, c=4, d=5, e=6, f=7): return subfunction_1(a=a, b=b, c=c) + subfunction_2(d=d, e=e, f=f)
Here you can see that I had to redefine the defaults in the main_function. In larger codebases, I find bugs often arise because defaults are defined in multiple places, and somebody changes them in a lower-level function but fails to realize that they are still defined differently in a higher function.
The only way I currently see to achieve this is not very nice at all, and completely obfuscates the signature of the function:
def main_function(**kwargs): return subfunction_1(**{k: v for k, v in kwargs.items() if k in ['a', 'b', 'c']}) + subfunction_2(**{k: v for k, v in kwargs.items() if k in ['d', 'e', 'f']})
What I was thinking was a "deferred" builtin that would just allow a lower function to define the value (and raise an exception if anyone tried to use it before it was defined)
def main_function(a=deferred, b=deferred, c=deferred, d=deferred, e=deferred, f=deferred): return subfunction_1(a=a, b=b, c=c) + subfunction_2(d=d, e=e, f=f)
I assume this has been discussed before somewhere, but couldn't find anything on it, so please feel free to point me towards any previous discussion on the topic.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/