[Python-ideas] PEP 505: None-aware operators
Steven D'Aprano
steve at pearwood.info
Thu Jul 26 01:04:51 EDT 2018
On Wed, Jul 25, 2018 at 11:09:35PM -0400, David Mertz wrote:
> Chris Angelica provided a more accurate translation. Do you not see that
> the fact that your *second* try at understanding the actual behavior is
> still wrong suggest that this operator is a HUGE bug magnet?!
Oh what a load of FUD.
The whole point of having an operator do this means that we don't have
to duplicate the semantics of the operator in plain Python.
Very few people get the semantics of x + y right. If (generic) you think
that it is:
return x.__add__(y)
you're wrong. If you think it is:
try:
return x.__add__(y)
except AttributeError:
return y.__radd__(x)
you're still wrong. If you think it is something like this:
try:
adder = type(x).__add__
operands = (x, y)
except AttributeError:
try:
adder = type(y).__radd__
operands = (y, x)
except AttributeError:
raise TypeError from None
return adder(*operands)
you're still wrong. But *nobody cares* because outside of some
incredibly rare and specialised uses, nobody needs to emulate the
semantics of + in pure Python. They just call the + operator, or at
worst call the operator.add function.
We have the syntax "yield from" because it is *really complicated* for
one generator to delegate to another generator. Emulating the yield from
call isn't necessary, because we have yield from.
(If you think that "yield from" is equivalent to "for x in subgenerator:
yield x", you're wrong too.)
And likewise, if we have these new operators, that means that those who
need them won't have to emulate them in pure Python. They can just use
the operators.
The idea that using the operators will be "a bug magnet" because the
implementation is (allegedly) complex is ludicrous.
--
Steve
More information about the Python-ideas
mailing list