1

I'm exploring python.

If have now lerned from the answers, that when I use *,

a = [1, 2, 3]
b = a*2
c = 2*a

a.__mul__() is called to compute b and a.__rmul__() to compute c (and this is more expensive, since the integer is tried to be called first :))

So, in general:

How can I see which function is called for some shortcut operator like *, and, or +=, ...

4
  • This kind of multiplication is commutative, so it comes out to the same thing, so both are right. Commented Mar 21, 2016 at 7:51
  • aaah! does r mean reverse or right or something? Commented Mar 21, 2016 at 7:52
  • 3
    stackoverflow.com/questions/5181320/… Commented Mar 21, 2016 at 7:53
  • 1
    Also rafekettler.com/magicmethods.html. Commented Mar 21, 2016 at 7:57

2 Answers 2

2

Python language reference 3.3.7: Emulating numeric types says this of the __rXXX__ magic methods:

These methods are called to implement the binary arithmetic operations (+, -, *, /, //, %, divmod(), pow(), **, <<, >>, &, ^, |) with reflected (swapped) operands. These functions are only called if the left operand does not support the corresponding operation and the operands are of different types. For instance, to evaluate the expression x - y, where y is an instance of a class that has an __rsub__() method, y.__rsub__(x) is called if x.__sub__(y) returns NotImplemented.


Thus for your case: at first for a * b Python tries to call a.__mul__(b). If this method does not exist or returns NotImplemented, Python calls b.__rmul__(a). If that returns a value that is not NotImplemented, that shall be the result of multiplication.

That way only one of a or b needs to support multiplication by the other.


In this specific case of multiplying a list (or any sequence) by an integer, it is slightly more efficient to do [1, 2, 3] * 2 than 2 * [1, 2, 3]; both give the same result, but [1, 2, 3] * 2 calls list.__mul__ first:

>>> [1,2,3].__mul__(2)
[1, 2, 3, 1, 2, 3]

The latter calls int.__mul__ first, which, given a list, returns NotImplemented:

>>> (2).__mul__([1,2,3])
NotImplemented

Then Python proceeds to calling [1, 2, 3].__rmul__(2):

>>> [1,2,3].__rmul__(2)
[1, 2, 3, 1, 2, 3]

And of course multiplication is not generally commutative; think about matrix multiplication

Sign up to request clarification or add additional context in comments.

7 Comments

Tigerhawks links were faster, but thank you nonetheless! :) so the second part is left, how this is in general...
thanks for the edit, I had just thought of it myself, haha :)
The link answers my question, I have to confess :) ... but still it would be nice to be able to see this "from within", not by reading documentation, but e.g. by getting a list of all called methods. But probably this is not possible...?
This all happens in C code in CPython, so not really, except with a custom class
I see. But this means: if I would overwrite the __mul__ method and add a print statement inside to see when it is executed, it would be much slower because I overwrite in python? But I don't see the connection. I heard of profiling, which should list all function calls? Would this not list these because they are built-in? Thank you for your patience :)
|
1

__mul__ is called when the object itself is on the left, and __rmul__ is for when it's on the right. For these particular objects, 2*a has the same result as a*2 because this kind of multiplication is commutative, so it comes out to the same thing, so both docstrings are right.

4 Comments

Yes, thank you, especially for the links, now I understand the 'r'. But it still doesn't explain, how I can figure this out, what is called. I will read through your second link first, this will take some time, and then I can maybe accept this :)
@Ilja - For the object on the left, __mul__ is called. For the object on the right, __rmul__ is called. For cases where it's not commutative, the two methods may be defined differently.
ah, no sorry, I understood that, of course. I meant about any other operators, different from '*'
You'll have to look in the documentation for that object/type to read about its defined behaviors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.