__float__ is not called when instance can not evaluated.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

__float__ is not called when instance can not evaluated.

Sebastian Krämer
Hi all,

I'm currently working on a function that converts a
sympy (http://code.google.com/p/sympy) expression to a lambda-function.
In this lambda-function all sympy builtin functions are replaced by
numpy functions, since they are faster.
Now it may happen that users pass sympy-symbols like pi to these lambda
functions and so it is possible that numpy-functions get these symbols.
The same functionality is implemented using python's math module, and it
works because the math functions call the __float__ method and therefor
get a number they can work with.
However, numpy doesn't do this, it only looks if there is a method with
the same name as the called function.
>> numpy.cos(sympy.pi)
<type 'exceptions.AttributeError'>: cos

>> math.cos(sympy.pi)

Would it be possible to change numpys behaviour so that x.__float__() is
tried after x.cos() has failed? Or are there any other possible

Thanks in advance,

Numpy-discussion mailing list
[hidden email]