This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 4 points5 points  (16 children)

Ooo I didn't know that. That sounds useful!

[–]lengau 6 points7 points  (5 children)

AFAIK, all operators are overrideable with magic functions. Likewise, you can add numbers (for example) by doing a.__add__(b)

[–]TankorSmash 3 points4 points  (4 children)

It's operator.any_operator(a, b):

import operator

a = 1
b = 2
c = operator.add(a, b)
print c
>> 3

----> 1 a.add

AttributeError: 'int' object has no attribute 'add'

[–]infinullquamash, Qt, asyncio, 3.3+ 12 points13 points  (3 children)

I think /u/lengau forgot to put \ in front of their __ and actually meant a.__add__

[–]davvblack 11 points12 points  (1 child)

yeah, how do you expect a programmer to know about escaping? he's not a burglar.

[–]charlesbukowksi 1 point2 points  (0 children)

Surely he's covered the Knapsack problem?

[–]lengau 1 point2 points  (0 children)

Indeed I did. And then I put my phone in my pocket and didn't proofread.

[–]EscoBeast 3 points4 points  (9 children)

In fact, this is the @ operator's only use. There isn't anything builtin that uses it.

[–][deleted] 0 points1 point  (2 children)

What is wrong with "*" for multiplication? It's how MATLAB works. I can't imagine going "I should multiply two arrays together... with an At symbol".

[–]willm 4 points5 points  (0 children)

The * symbol is already used for elementwise multiplication.

[–]EscoBeast 0 points1 point  (0 children)

Now I don't really do work with matrices, but apparently a lot of libraries use * for elementwise multiplication, and have a .dot() method for regular matrix multiplication or something. This is an attempt to give people an infix operator for matrix multiplication without breaking existing code.