
On Mon, Oct 25, 2021 at 12:45:03PM -0700, Mike Miller wrote:
On 2021-10-23 17:13, Chris Angelico wrote:
def bisect_right(a, x, lo=0, hi=>len(a), *, key=None):
Sounds like deferred execution could be useful, but I wanted to complain about the example above. I realize it is "just an example" but if I saw this in code, the first thing I'd do is ask for it to be changed.
Changed to what?
Why? The same variable (or simple variant) shouldn't be passed twice in a signature, when it can be operated on inside the body of the function to get that variant. i.e.: DRY.
I'm sorry, I don't understand your objection here. The function parameters are: a, x, lo, hi, key None of them are passed twice. I'm not clear what you consider to be a DRY violation here. Is this also a DRY violation? def bisect_right(a, x, lo=0, hi=None, *, key=None): if hi is None: hi = len(a) If not, then how is it a DRY violation to lift the initialisation of the default `hi = len(a)` from a manual operation inside the body of the function to a late-bound default argument? def bisect_right(a, x, lo=0, @hi=len(a), *, key=None): *Moving* code from one place to another isn't *repeating* the code. And moving initialisation code for defaults from the body of the function to the signature is the point of the exercise. Sometimes that default initialisation code refers to other parameters. -- Steve