[Numpy-discussion] [help needed] associativity and precedence of '@'

Alexander Belopolsky ndarray at mac.com
Mon Mar 17 12:50:05 EDT 2014


On Mon, Mar 17, 2014 at 12:13 PM, Nathaniel Smith <njs at pobox.com> wrote:

> In practice all
> well-behaved classes have to make sure that they implement __special__
> methods in such a way that all the different variations work, no
> matter which class ends up actually handling the operation.
>

"Well-behaved classes" are hard to come by in practice.  The @ operator may
fix the situation with np.matrix, so take a look at MaskedArray with its
40-line __array_wrap__ and no end of bugs.

Requiring superclass __method__ to handle creation of subclass results
correctly is turning Liskov principle on its head.  With enough clever
tricks and tight control over the full class hierarchy you can make it work
in some cases, but it is not a good design.

I am afraid that making @ special among other binary operators that
implement mathematically associative operations will create a lot of
confusion.  (The pow operator is special because the corresponding
mathematical operation is non-associative.)

Imagine teaching someone that a % b % c = (a % b) % c, but a @ b @ c = a @
(b @ c).  What are the chances that they will correctly figure out what a
// b // c means after this?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140317/412d7671/attachment.html>


More information about the NumPy-Discussion mailing list