[Numpy-discussion] Multiplying numpy floats and python lists

Tony S Yu tsyu80 at gmail.com
Sun Jun 20 13:56:48 EDT 2010


I came across some strange behavior when multiplying numpy floats and python lists: the list is returned unchanged:

> In [18]: np.float64(1.2) * [1, 2]
> 
> Out[18]: [1, 2]

On the other hand, multiplying an array scalar and a python list gives the expected answer

> In [19]: np.array(1.2) * [1, 2]
> 
> Out[19]: array([ 1.2,  2.4])

Finally, multiplying a python float and a python list gives an error:

> In [20]: 1.2 * [1, 2]
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call last)
> 
> /Users/Tony/<ipython console> in <module>()
> 
> TypeError: can't multiply sequence by non-int of type 'float'

These last two results (with the array scalar and the python float) both seem appropriate, but the numpy-float behavior is surprising. Is this a bug?

-Tony


More information about the NumPy-Discussion mailing list