[Python-ideas] Proposal for an inplace else (?=) operator

Bruce Leban bruce at leban.us
Sat Sep 22 08:30:36 EDT 2018

On Saturday, September 22, 2018, Lee Braiden <leebraid at 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 =>

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

--- Bruce

--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180922/91f73ab0/attachment.html>

More information about the Python-ideas mailing list