4 ms·
I'll modify the example slightly to something which doesn't have a type error: [1,2,'q',[1,('a',2)]] * 4 With element-by-element operations, that would be
by dalke 12y ago
I'll modify the example slightly to something which doesn't have a type error:
[1,2,'q',[1,('a',2)]] * 4
With element-by-element operations, that would be
[1 * 4,2 * 4,'q' * 4,[1,('a',2)] * 4]
giving
[4, 8, 'qqqq', [1 * 4, ('a', 2) * 4]]
applying that again, assuming that tuple * scalar is also applied element-wise gives:
[4, 8, 'qqqq', [4, ('a' * 4, 2 * 4)]]
and ends up with
[4, 8, 'qqqq', [4, ('aaaa', 8)]]
I can't think of any case where that's meaningful.
Also, what should this do:
x = [1]
x.append(x)
print(x + x)
print(x * 4)
? Currently these print:
[1, [1, [...]], 1, [1, [...]]]
[1, [1, [...]], 1, [1, [...]], 1, [1, [...]], 1, [1, [...]]]
because the print function knows how to handle recursive definitions. Do all of the element-wise operations need to handle cyclical cases like this? I think numpy can get away with not worrying about this precisely because, as
wodenokoto pointed out, it can assume a flat structure.