append to non-existing list
mwm at mired.org
Wed Nov 9 18:40:41 CET 2005
"bonono at gmail.com" <bonono at gmail.com> writes:
> Fredrik Lundh wrote:
>> x = "10" + 20 # should this be 30 or 1020 or "1020" or ...?
> I think Perl handles this case pretty well and sane.
> In fact, this strict but dynamic type checking is quite painful to work
> with, especially the new decimal class.
> I can do a :
> decimal + int
> but not
> decimal + float
> But then when I use some built-in math functions, it can convert
> decimal to float and return a float which then cannot be operate with
> another decimal.
Interesting probllem. Decimal + float can't return a decimal - we
don't do implicit conversion from float to decimal, because there's no
obvious correct conversion. So decimal.__add__ returns NotImplemented,
which I believe is the correct thing to do. IIRC, this defers the
operation to the float type. Float doesn't handle implicit conversion
to anything but but builtin types. In particular, it doesn't check to
see if the object beinng added has a __float__ method, and invoke that
to do the conversion if it does.
This looks like a bug. Either decimal.__add__ should return a float in
these circumstances (not pretty), or float should check for the
__float__ attribute and deal with it. On the other hand, the secton in
the manual says that in Python 3.0 coercion won't be supported, so
this may be a reflection of how things are going to be in the future.
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list