On Saturday, September 22, 2018, Lee Braiden <leebr...@gmail.com> wrote:
> > Proposal: > > The addition of a ?= operator could provide an elegant solution: > > > def teleport(from, to, hitchiker=None, food_accessory=None, > comfort_accessory=None): > > hitchhiker ?= Fly() > > food_accessory ?= Cheeseburger() > > comfort_accessory ?= Towel() > > > Would be equivalent to (assuming ?= was called __ielse__): > > > class ExtendedNone(NoneType) > > def __ielse__(self, other): > > return other > > > > class ielse_list(list): > > def __ielse__(self, other): > > return self > > > > None = ExtendedNone() > > This is attacking the workaround not the actual problem. The real problem is that default parameters are evaluated at function definition time not call time. Setting the default to None and then replacing it is a workaround to that problem (and one that doesn't work if None is an allowable value). If Python were going to address this problem, I think it better to do something like: def teleport(from, to, hitchiker => Fly(), accessory => Towel(hitchhiker)): etc. Where => specifies an expression thst is evaluated inside the function and Is approximately equivalent to your code except it works even if the caller passes None. (I don't know what the right syntax for this would be. I just picked => as something that is suggestive, not legal today and isn't ?= to avoid confusion with your proposal.) Note that I shortened your example and modified it slightly to show that I would have the order of the parameters be significant. The call to Towel can use the previous hitchiker parameter and it will do what you expect. Clearly that would work with your code; you just weren't esplicit about it. My alternative doesn't allow arbitrary None replacement, but I'm not sure that's a prevalent pattern other than in this case notwithstanding your examples. --- Bruce -- --- Bruce
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/