[Numpy-discussion] ndarray newbie question

Robert Kern robert.kern at gmail.com
Thu Jan 4 13:47:35 CST 2007


belinda thom wrote:
> Hello,
> 
> I wrote a "display obj" method for viewing instance data:
> 
> def dobj(obj) :
>      """extended object viewer that displays arg"""
>      print 'Class: ', obj.__class__.__name__, ' id:', id(obj)
>      for k in obj.__dict__.keys() :
>          print '  ', k, ': ', obj.__dict__[k], ' id:', id(obj.__dict__ 
> [k])
> 
> 
> and I'm wondering why it doesn't work on ndarrays:
> 
>  >>> m = numpy.zeros(3)
>  >>> dobj(m)
> 
> complains because:
> 
> AttributeError: 'numpy.ndarray' object has no attribute '__dict__'
> 
> I am also fairly new to python, but would have thought all objects  
> had dictionaries, especially when they are comprised of other things,  
> e.g.
> 
> dir(m)
> 
> produces a slew of components, e.g.
> 
> tofile, ..., transpose, ..., view, etc.

Most types defined in C do not have a .__dict__ .


In [1631]: s = 1

In [1632]: s.__dict__
---------------------------------------------------------------------------
<type 'exceptions.AttributeError'>        Traceback (most recent call last)

/Users/rkern/svn/mt-data/<ipython console> in <module>()

<type 'exceptions.AttributeError'>: 'int' object has no attribute '__dict__'


-- 
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