On Sat, Oct 23, 2021 at 5:16 PM Chris Angelico <ros...@gmail.com> wrote:
> # Very common: Use None and replace it in the function > def bisect_right(a, x, lo=0, hi=None, *, key=None): > if hi is None: > hi = len(a) > Note that if this is changed to def bisect_right(a, x, lo=0, hi[whatever]len(a), *, key=None): then you'll lose the ability to pass hi=None to mean the end of the array. For this argument of bisect_right, None actually makes sense since it's used for that purpose in slices, and it would probably be a backward-compatibility problem to drop support for it also. So you will still have to write def bisect_right(a, x, lo=0, hi[whatever]len(a), *, key=None): if hi is None: hi = len(a) Self-referential expressions will result in UnboundLocalError:: > > def spam(eggs=>eggs): # Nope > That seems like not a rule of its own, but a special case of this rule: deferred arguments that haven't been assigned to yet are unbound (and not, say, set to some implementation-specific value analogous to _USE_GLOBAL_DEFAULT). > def bisect(a, hi=>len(a)): > That looks like an anonymous function that takes a single parameter named hi, ignores it, and returns the length of a free variable named a. It may even mean that in Python someday in expression contexts. But it's definitely not what it means here. hi<=len(a) would make a bit more sense. > def bisect(a, hi=:len(a)): > This one annoys me the least, though I can't say why. def bisect(a, hi?=len(a)): > ? seems reasonable for omitted/defaulted arguments, but that's not what this PEP is about. I can't see why call-time evaluation would have a ? and def-time evaluation wouldn't. def bisect(a, hi!=len(a)): > Same basic problem as => (and <=). Is hi equal to len(a), or not? def bisect(a, hi=`len(a)`): > It looks like the backticks are part of the expression rather than the function-parameter syntax, but they aren't, and never could be - even if there was a deferred-evaluation syntax at the expression level, it couldn't be used here, because the lexical environment would be wrong. > Since default arguments behave largely the same whether they're early or > late > bound, They seem very different to me. They're evaluated in a different lexical scope, and many use cases for this extension depend on the changed scoping.
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/22DEQDF35SOBW6OC2Z2GGBVAQKIKHMPM/ Code of Conduct: http://python.org/psf/codeofconduct/