Modifying the value of a float-like object

Dan Goodman dg.gmane at thesamovar.net
Wed Apr 15 15:44:48 EDT 2009


Eric.Le.Bigot at spectro.jussieu.fr wrote:
> I initially tried to create a Float_ref class that inherits from
> numpy.array, so that objects of the new class behave like numpy.array
> in calculations, but also contain an "uncertainty" atribute, but this
> is apparently not allowed ("cannot create 'builtin_function_or_method'
> instances").
> 
> So I thought I'd directly add an attribute to a numpy.array: "x =
> numpy.array(3.14); x.uncertainty = 0.01", but this is not allowed
> either.
> 
> Thus, it is not obvious for me to extend the 1x1 numpy.array object so
> that it also contains an "uncertainty" attribute.

Subclassing numpy arrays is possible but it can be a bit fiddly. You 
can't add attributes to a numpy array unless it's a subclass.

The first place to look is:

http://www.scipy.org/Subclasses

Actually, I haven't read this (new) page in the numpy docs, but it looks 
better:

http://docs.scipy.org/doc/numpy/user/basics.subclassing.html

It also seems to include a simple example of how to write a subclass 
that just adds a single attribute.

Dan




More information about the Python-list mailing list