[Numpy-discussion] request for new array method: arr.abs()

Sebastian Haase haase at msg.ucsf.edu
Wed Aug 23 18:22:52 CDT 2006


On Wednesday 23 August 2006 16:12, Bill Baxter wrote:
> The thing that I find I keep forgetting is that abs() is a built-in, but
> other simple functions are not.  So it's abs(foo), but numpy.floor(foo) and
> numpy.ceil(foo).  And then there's round() which is a built-in but can't be
> used with arrays, so numpy.round_(foo).    Seems like it would be more
> consistent to just add a numpy.abs() and numpy.round().
>
> But I guess there's nothing numpy can do about it...  you can't name a
> method the same as a built-in function, right?  That's why we have
> numpy.round_() instead of numpy.round(), no?
> [...goes and checks]
> Oh, you *can* name a module function the same as a built-in.  Hmm... so
> then why isn't numpy.round_() just numpy.round()?  Is it just so "from
> numpy import *" won't hide the built-in?
>
That is my theory...
Even tough I try to advertise 
import numpy as N
a) "N."  is not *that* much  extra typing
b) it much clearer to read code and see what is special from numpy vs. what is 
builtin
c)  (most important for me): I use PyShell/PyCrust and when I type the '.' 
after 'N'  I get a nice pop-up list reminding me of all the function in 
numy ;-)

Regarding the original subject: 
a) "absolute" is impractically too much typing and 
b) I just thought some (module-) functions might be "forgotten" to be put in 
 as (object-) methods ... !?

Cheers,
Sebastian


> --bill
>
> On 8/24/06, David M. Cooke <cookedm at physics.mcmaster.ca> wrote:
> > On Wed, 23 Aug 2006 13:51:02 -0700
> >
> > Sebastian Haase <haase at msg.ucsf.edu> wrote:
> > > Hi!
> > > numpy renamed the *function* abs to absolute.
> > > Most functions like mean, min, max, average, ...
> > > have an equivalent array *method*.
> > >
> > > Why is absolute left out ?
> > > I think it should be added .
> >
> > We've got __abs__ :-)
> >
> > > Furthermore, looking at some line  of code that have multiple calls to
> > > absolute [ like f(absolute(a), absolute(b), absolute(c)) ]
> > > I think "some people" might prefer less typing and less reading,
> > > like f( a.abs(), b.abs(), c.abs() ).
> > >
> > > One could even consider not requiring the "function call" parenthesis
> >
> > '()'
> >
> > > at all - but I don't know about further implications that might have.
> >
> > eh, no. things that return new arrays should be functions. (As opposed to
> > views of existing arrays, like a.T)
> >
> > > PS: is there any performace hit in using the built-in abs function ?
> >
> > Shouldn't be: abs(x) looks for the x.__abs__() method (which arrays
> > have).




More information about the Numpy-discussion mailing list