On Tue, May 26, 2020 at 4:12 PM Greg Ewing <greg.ewing@canterbury.ac.nz> wrote:
Wild idea: Instead of sentinels, have a way of declaring optional arguments with no default, and a way of conditionally assigning a value to them if they are not bound.
E.g.
def order(eggs = 4, spam =): spam ?= Spam()
Here the '?=' means "if spam is not bound, then evaluate the rhs and assign it, otherwise do nothing."
That's interesting. I suppose the semantics would be the same as any other unbound local, then? That would actually be useful in other contexts, like where you conditionally assign in a loop, and then might not have it assigned at the end. I don't know of any current way to say "if spam is unset". It definitely needs good syntax though. The loose equals sign would be too confusing, and there's no good keyword available. It'd be kinda elegant to surround the name in square brackets: def order(eggs=4, [spam]): spam ?= Spam() but that would be confusing for other reasons. ChrisA