[Numpy-discussion] Unexpected attribute error
Robert Kern
robert.kern at gmail.com
Tue Nov 17 21:53:46 EST 2009
On Tue, Nov 17, 2009 at 20:48, James Bergstra <bergstrj at iro.umontreal.ca> wrote:
> Is it by design that "numpy.sqrt(None)" raises an "AttributeError: sqrt"?
Yes. numpy.sqrt() is a ufunc. Ufuncs take their arguments and try to
convert them to numpy arrays; the manual equivalent is
numpy.asarray(None). In this case, you get a rank-0 array with
dtype=object. The way unary ufuncs work on object arrays is to call
the method of the same name on the object. None.sqrt doesn't exist, so
the AttributeError gets raised.
> This was confusing because there was an attribute lookup of 'sqrt' in
> numpy right there in the expression I typed, but that was not the
> attribute that python was complaining about. I presume that numpy.sqrt
> didn't know what to do with None, so it looked up a .sqrt in it or
> something... but I presume this only in hindsight now that I figured
> out the problem--I didn't mean to take the sqrt of None in the first place.
>
> How about adding some information to the AttributeError, such
> as the object on which the lookup failed (here None, as opposed to the
> numpy module).
Patches welcome.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion
mailing list