[Python-ideas] Fast sum() for non-numbers
Ethan Furman
ethan at stoneleaf.us
Thu Jul 11 22:30:42 CEST 2013
On 07/11/2013 11:29 AM, Ron Adam wrote:
>
>
> On 07/11/2013 12:21 PM, Oscar Benjamin wrote:
>> On 11 July 2013 18:12, Ron Adam <ron3200 at gmail.com> wrote:
>>> I think this is what it should do.
>>>
>>> I tried overiding lists __add__, but that didn't work as nice. It needs to
>>> have a starting list with all zeros in it. How does numpy get around that?
>>
>> Like so:
>>
>>>>> import numpy
>>>>> a = numpy.array([1, 2, 3])
>>>>> a
>> array([1, 2, 3])
>>>>> a + 0
>> array([1, 2, 3])
>>>>> a + 1
>> array([2, 3, 4])
>>
>>
>> Oscar
>
> Right answer to the wrong question.
>
> I was asking how numpy it gets around sum() needing a starting 'array' argument? Not how numpy arrays work with '+'.
Because the default start is 0, and when you add 0 to a numpy array you get back the same* numpy array.
*At least, a numpy array with all the same values.
--
~Ethan~
More information about the Python-ideas
mailing list