Unexpected behavior for numpy self division Unexpected behavior for numpy self division numpy numpy

Unexpected behavior for numpy self division


I always thought x/=5. was equivalent to x=x/5

It is, unless the class overrides the __idiv__ operator, like numpy.ndarray does.numpy.ndarray overrides it to modify the array in-place, which is good because it avoids creating a new copy of the array, when copying is not required. As you can guess, it also overrides the rest of the __i*__ operators.


Python's in-place operators allow the operation to modify the object on the left side of the equation rather than creating a new one. You'll see the same behavior with lists and other built-in mutable types:

x = []y = xx += [1]print y   # prints [1] because x still refers to the same object as yx = x + [2]print x   # prints [1, 2] for obvious reasonsprint y   # prints [1] again because x has been rebound to a new list          # while y still refers to the original one

So this is expected behavior.

When dealing with immutable types, of course, a new object is created, because the existing one can't be modified.


I always thought x/=5. was equivalent to x=x/5.

It is not. When you do x=x/5., you're in fact performing two separate operations below the hood:

temporary = x/5.x = temporary

whereas when you're using the /= operator, you're explicitly asking Python to modify your object x in place, without the creation of a temporary object. As mentioned in other answers, the / operator calls the __div__ method of your object (if any), while /= calls its __idiv__ method.

The in-place modification is quite useful if the object you're modifying is large: no need to create an as-large temporary object. However, it can bite you if you're not careful. In particular, it's usually not that great an idea to have a function modify its input parameters without warning...