Usage of NotImplemented in Numexpr

Ivan Vilata i Balaguer ivilata at carabos.com
Mon Oct 16 05:47:33 CDT 2006


Looking at the ``ophelper()`` decorator in the ``expressions`` module of
Numexpr, I see the following code is used to check/replace arguments of
operators::

    for i, x in enumerate(args):
        if isConstant(x):
            args[i] = x = ConstantNode(x)
        if not isinstance(x, ExpressionNode):
            return NotImplemented

This looks like operations on unknown kinds of arguments use the default
Python behaviour.  However, this yields some strange results:

>>> import numpy
>>> a = numpy.array([1,1,1])
>>> import numexpr
>>> numexpr.evaluate('a < [0,0,0]')
array(True, dtype=bool)

This is odd because the comparison was not element-wise, but object-wise
(between a VariableNode and an -unsupported- python list).  Since
Numexpr only understands scalar constants, variables and some functions
(the last two are expression nodes), it seems more correct to me to
simply forbid unsupported objects to avoid surprises, so the previous
code may look like this::

    for i, x in enumerate(args):
        if isConstant(x):
            args[i] = ConstantNode(x)
        elif not isinstance(x, ExpressionNode):
            raise TypeError( "unsupported object type: %s",
                             type(x) )

Do you think this is OK, or am I wrong or missing something?

Cheers,

::

	Ivan Vilata i Balaguer   >qo<   http://www.carabos.com/
	       Cárabos Coop. V.  V  V   Enjoy Data
	                          ""

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 309 bytes
Desc: OpenPGP digital signature
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061016/63dc6849/attachment.bin 
-------------- next part --------------
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
-------------- next part --------------
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion


More information about the Numpy-discussion mailing list