[Numpy-discussion] proper use of __new__
Pierre GM
pgmdevlist@gmail....
Tue Jul 21 12:26:49 CDT 2009
On Jul 21, 2009, at 10:58 AM, Darren Dale wrote:
> http://docs.scipy.org/doc/numpy/user/basics.subclassing.html#simple-example-adding-an-extra-attribute-to-ndarray
> includes an example where an instance attribute is set in __new__ .
> However, there is a warning at http://www.scipy.org/Subclasses:
(Note to self: I should add the information in Subclasses to the
documentation and make it consistent. </sigh>).
The words of caution were intended to remind that setting the value
of new array attributes in __new__ only could lead to problems, as
it's possible to transform a ndarray into a subclass with a view (that
is, calling __array_finalize__ without calling __new__). In the Simple
Example of the doc, if .info cannot be None but must have a default
DEFAULT, you should have a
>>> self.info = getattr(obj, 'info', DEFAULT)
in your __array_finalize__
Without access to your code, I'm likely to speak beyond myself here
(could you send me a link to the latest version ?), but if you need to
rescale the units, make sure it's done in __array_finalize__ as well,
with something like
units = getattr(obj, 'units', None)
if units is not None:
self.rescale(units)
else:
self.units = units
Once again, browsing your code would let me give you some more
adequate answers...
More information about the NumPy-Discussion
mailing list