[Numpy-discussion] Ransom Proposals

Tim Hochberg tim.hochberg at cox.net
Mon Mar 27 13:29:03 CST 2006

Charles R Harris wrote:

> How about functions always return a copy unless they are carefully 
> documented interface helper functions like asarray. That's how I 
> generally think of them anyway. 

I don't really care as long as they (a) stop being evil and (b) there's 
some way to do stuff efficiently. Functions that always return copies 
seem sort of useless to me, but I'd be happy to move to methods if that 
was the case. However, I suspect you won't make much headway with this 
since my impression is that many of the people who do care about 
functions also care passionately about efficiency as well.

> Of course, the main virtue of asarray is that it helps write functions 
> that do precisely what Tim is arguing against: mixing list and array 
> types and returning copies in the first case, views in the second. So 
> if we throw out functions we should throw out asarray also and make a 
> rule that numpy functions don't take lists. 

This isn't right. I think it's perfectly fine, desirable in fact, for 
functions to operate on both list and array types. The problem only 
arises when you return ether a view or copy of one of the original 
inputs depending on what it was. In my experience, this is relatively 
hard to do in all but the most trivial of functions since most 
operations return a new array. However, it is true that I think people 
should be strongly discouraged from doing this. For example:

    def add(a, b):
        a = asarray(a)
        b = asarray(b)
        return a + b

is fine, but:

    def iadd(a, b):
        a = asarray(a)
        b = asarray(b)
        a += b
        return a

is not and should be rewritten. Possibly like:

    def iadd(a, b):
        if not isinstance(a, ndarray): raise TypeError("'a' must be an
        b = asarray(b)
        a += b
        return a

since += is a bit squirley. (Try somelist += somearray, to see what I mean).

> And then there are scalars...

I'm curious what problems you forsee with scalars.



More information about the Numpy-discussion mailing list