4 ms·
This is a fun numpy quirk: >>> 'x'*3.5 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can't multiply seque
by ngoldbaum 11y ago
This is a fun numpy quirk:
>>> 'x'*3.5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
>>> import numpy as np
>>> print 'x'*np.float64(3.5)
xxx
- iaw 11y agoI use R predominantly, so forgive the lack of adequate Python-ese. Is this behavior because numpy overloads the multiplication operation with a string as string repetition and then implicitly casts the float64 down to an integer of 3? I'm curious why this behavior manifests. When I get a chance I'll test 'xyz'*np.float(3.5)
- lmm 11y agoIt's "normal" in python for this to work with integers: >>> "x" * 3 xxx The only weird part (and it's not that weird IMO) is that numpy's "float" can be implicitly coerced to integer.
- shoyer 11y agoAs an occasional numpy contributor, I would call this a bug.
- ngoldbaum 11y agoGood point - I found it after it caused a really weird bug in a library I use. I'll open an issue on github...
- ngoldbaum 11y agoTurns out it's deprecated: >>> import warnings >>> warnings.simplefilter('always') >>> 'x'*np.float64(3.5) __main__:1: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future 'xxx'
- p1mrx 11y agoYes, the expected output is xx›
- deleted 11y ago[deleted]